Fix bugs in the c-parse-state mechanism. Reuse some markers instead of
[bpt/emacs.git] / lisp / progmodes / cc-engine.el
1 ;;; cc-engine.el --- core syntax guessing engine for CC mode
2
3 ;; Copyright (C) 1985, 1987, 1992-2013 Free Software Foundation, Inc.
4
5 ;; Authors: 2001- Alan Mackenzie
6 ;; 1998- Martin Stjernholm
7 ;; 1992-1999 Barry A. Warsaw
8 ;; 1987 Dave Detlefs
9 ;; 1987 Stewart Clamen
10 ;; 1985 Richard M. Stallman
11 ;; Maintainer: bug-cc-mode@gnu.org
12 ;; Created: 22-Apr-1997 (split from cc-mode.el)
13 ;; Keywords: c languages
14 ;; Package: cc-mode
15
16 ;; This file is part of GNU Emacs.
17
18 ;; GNU Emacs is free software: you can redistribute it and/or modify
19 ;; it under the terms of the GNU General Public License as published by
20 ;; the Free Software Foundation, either version 3 of the License, or
21 ;; (at your option) any later version.
22
23 ;; GNU Emacs is distributed in the hope that it will be useful,
24 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
25 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26 ;; GNU General Public License for more details.
27
28 ;; You should have received a copy of the GNU General Public License
29 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
30
31 ;;; Commentary:
32
33 ;; The functions which have docstring documentation can be considered
34 ;; part of an API which other packages can use in CC Mode buffers.
35 ;; Otoh, undocumented functions and functions with the documentation
36 ;; in comments are considered purely internal and can change semantics
37 ;; or even disappear in the future.
38 ;;
39 ;; (This policy applies to CC Mode as a whole, not just this file. It
40 ;; probably also applies to many other Emacs packages, but here it's
41 ;; clearly spelled out.)
42
43 ;; Hidden buffer changes
44 ;;
45 ;; Various functions in CC Mode use text properties for caching and
46 ;; syntactic markup purposes, and those of them that might modify such
47 ;; properties but still don't modify the buffer in a visible way are
48 ;; said to do "hidden buffer changes". They should be used within
49 ;; `c-save-buffer-state' or a similar function that saves and restores
50 ;; buffer modifiedness, disables buffer change hooks, etc.
51 ;;
52 ;; Interactive functions are assumed to not do hidden buffer changes,
53 ;; except in the specific parts of them that do real changes.
54 ;;
55 ;; Lineup functions are assumed to do hidden buffer changes. They
56 ;; must not do real changes, though.
57 ;;
58 ;; All other functions that do hidden buffer changes have that noted
59 ;; in their doc string or comment.
60 ;;
61 ;; The intention with this system is to avoid wrapping every leaf
62 ;; function that do hidden buffer changes inside
63 ;; `c-save-buffer-state'. It should be used as near the top of the
64 ;; interactive functions as possible.
65 ;;
66 ;; Functions called during font locking are allowed to do hidden
67 ;; buffer changes since the font-lock package run them in a context
68 ;; similar to `c-save-buffer-state' (in fact, that function is heavily
69 ;; inspired by `save-buffer-state' in the font-lock package).
70
71 ;; Use of text properties
72 ;;
73 ;; CC Mode uses several text properties internally to mark up various
74 ;; positions, e.g. to improve speed and to eliminate glitches in
75 ;; interactive refontification.
76 ;;
77 ;; Note: This doc is for internal use only. Other packages should not
78 ;; assume that these text properties are used as described here.
79 ;;
80 ;; 'category
81 ;; Used for "indirection". With its help, some other property can
82 ;; be cheaply and easily switched on or off everywhere it occurs.
83 ;;
84 ;; 'syntax-table
85 ;; Used to modify the syntax of some characters. It is used to
86 ;; mark the "<" and ">" of angle bracket parens with paren syntax, and
87 ;; to "hide" obtrusive characters in preprocessor lines.
88 ;;
89 ;; This property is used on single characters and is therefore
90 ;; always treated as front and rear nonsticky (or start and end open
91 ;; in XEmacs vocabulary). It's therefore installed on
92 ;; `text-property-default-nonsticky' if that variable exists (Emacs
93 ;; >= 21).
94 ;;
95 ;; 'c-is-sws and 'c-in-sws
96 ;; Used by `c-forward-syntactic-ws' and `c-backward-syntactic-ws' to
97 ;; speed them up. See the comment blurb before `c-put-is-sws'
98 ;; below for further details.
99 ;;
100 ;; 'c-type
101 ;; This property is used on single characters to mark positions with
102 ;; special syntactic relevance of various sorts. Its primary use is
103 ;; to avoid glitches when multiline constructs are refontified
104 ;; interactively (on font lock decoration level 3). It's cleared in
105 ;; a region before it's fontified and is then put on relevant chars
106 ;; in that region as they are encountered during the fontification.
107 ;; The value specifies the kind of position:
108 ;;
109 ;; 'c-decl-arg-start
110 ;; Put on the last char of the token preceding each declaration
111 ;; inside a declaration style arglist (typically in a function
112 ;; prototype).
113 ;;
114 ;; 'c-decl-end
115 ;; Put on the last char of the token preceding a declaration.
116 ;; This is used in cases where declaration boundaries can't be
117 ;; recognized simply by looking for a token like ";" or "}".
118 ;; `c-type-decl-end-used' must be set if this is used (see also
119 ;; `c-find-decl-spots').
120 ;;
121 ;; 'c-<>-arg-sep
122 ;; Put on the commas that separate arguments in angle bracket
123 ;; arglists like C++ template arglists.
124 ;;
125 ;; 'c-decl-id-start and 'c-decl-type-start
126 ;; Put on the last char of the token preceding each declarator
127 ;; in the declarator list of a declaration. They are also used
128 ;; between the identifiers cases like enum declarations.
129 ;; 'c-decl-type-start is used when the declarators are types,
130 ;; 'c-decl-id-start otherwise.
131 ;;
132 ;; 'c-awk-NL-prop
133 ;; Used in AWK mode to mark the various kinds of newlines. See
134 ;; cc-awk.el.
135
136 ;;; Code:
137
138 (eval-when-compile
139 (let ((load-path
140 (if (and (boundp 'byte-compile-dest-file)
141 (stringp byte-compile-dest-file))
142 (cons (file-name-directory byte-compile-dest-file) load-path)
143 load-path)))
144 (load "cc-bytecomp" nil t)))
145
146 (cc-require 'cc-defs)
147 (cc-require-when-compile 'cc-langs)
148 (cc-require 'cc-vars)
149
150 ;; Silence the compiler.
151 (cc-bytecomp-defun buffer-syntactic-context) ; XEmacs
152
153 \f
154 ;; Make declarations for all the `c-lang-defvar' variables in cc-langs.
155
156 (defmacro c-declare-lang-variables ()
157 `(progn
158 ,@(apply 'nconc
159 (mapcar (lambda (init)
160 `(,(if (elt init 2)
161 `(defvar ,(car init) nil ,(elt init 2))
162 `(defvar ,(car init) nil))
163 (make-variable-buffer-local ',(car init))))
164 (cdr c-lang-variable-inits)))))
165 (c-declare-lang-variables)
166
167 \f
168 ;;; Internal state variables.
169
170 ;; Internal state of hungry delete key feature
171 (defvar c-hungry-delete-key nil)
172 (make-variable-buffer-local 'c-hungry-delete-key)
173
174 ;; The electric flag (toggled by `c-toggle-electric-state').
175 ;; If t, electric actions (like automatic reindentation, and (if
176 ;; c-auto-newline is also set) auto newlining) will happen when an electric
177 ;; key like `{' is pressed (or an electric keyword like `else').
178 (defvar c-electric-flag t)
179 (make-variable-buffer-local 'c-electric-flag)
180
181 ;; Internal state of auto newline feature.
182 (defvar c-auto-newline nil)
183 (make-variable-buffer-local 'c-auto-newline)
184
185 ;; Included in the mode line to indicate the active submodes.
186 ;; (defvar c-submode-indicators nil)
187 ;; (make-variable-buffer-local 'c-submode-indicators)
188
189 (defun c-calculate-state (arg prevstate)
190 ;; Calculate the new state of PREVSTATE, t or nil, based on arg. If
191 ;; arg is nil or zero, toggle the state. If arg is negative, turn
192 ;; the state off, and if arg is positive, turn the state on
193 (if (or (not arg)
194 (zerop (setq arg (prefix-numeric-value arg))))
195 (not prevstate)
196 (> arg 0)))
197
198 \f
199 ;; Basic handling of preprocessor directives.
200
201 ;; This is a dynamically bound cache used together with
202 ;; `c-query-macro-start' and `c-query-and-set-macro-start'. It only
203 ;; works as long as point doesn't cross a macro boundary.
204 (defvar c-macro-start 'unknown)
205
206 (defsubst c-query-and-set-macro-start ()
207 (if (symbolp c-macro-start)
208 (setq c-macro-start (save-excursion
209 (c-save-buffer-state ()
210 (and (c-beginning-of-macro)
211 (point)))))
212 c-macro-start))
213
214 (defsubst c-query-macro-start ()
215 (if (symbolp c-macro-start)
216 (save-excursion
217 (c-save-buffer-state ()
218 (and (c-beginning-of-macro)
219 (point))))
220 c-macro-start))
221
222 ;; One element macro cache to cope with continual movement within very large
223 ;; CPP macros.
224 (defvar c-macro-cache nil)
225 (make-variable-buffer-local 'c-macro-cache)
226 ;; Nil or cons of the bounds of the most recent CPP form probed by
227 ;; `c-beginning-of-macro', `c-end-of-macro' or `c-syntactic-end-of-macro'.
228 ;; The cdr will be nil if we know only the start of the CPP form.
229 (defvar c-macro-cache-start-pos nil)
230 (make-variable-buffer-local 'c-macro-cache-start-pos)
231 ;; The starting position from where we determined `c-macro-cache'.
232 (defvar c-macro-cache-syntactic nil)
233 (make-variable-buffer-local 'c-macro-cache-syntactic)
234 ;; non-nil iff `c-macro-cache' has both elements set AND the cdr is at a
235 ;; syntactic end of macro, not merely an apparent one.
236
237 (defun c-invalidate-macro-cache (beg end)
238 ;; Called from a before-change function. If the change region is before or
239 ;; in the macro characterized by `c-macro-cache' etc., nullify it
240 ;; appropriately. BEG and END are the standard before-change-functions
241 ;; parameters. END isn't used.
242 (cond
243 ((null c-macro-cache))
244 ((< beg (car c-macro-cache))
245 (setq c-macro-cache nil
246 c-macro-cache-start-pos nil
247 c-macro-cache-syntactic nil))
248 ((and (cdr c-macro-cache)
249 (< beg (cdr c-macro-cache)))
250 (setcdr c-macro-cache nil)
251 (setq c-macro-cache-start-pos beg
252 c-macro-cache-syntactic nil))))
253
254 (defun c-beginning-of-macro (&optional lim)
255 "Go to the beginning of a preprocessor directive.
256 Leave point at the beginning of the directive and return t if in one,
257 otherwise return nil and leave point unchanged.
258
259 Note that this function might do hidden buffer changes. See the
260 comment at the start of cc-engine.el for more info."
261 (let ((here (point)))
262 (when c-opt-cpp-prefix
263 (if (and (car c-macro-cache)
264 (>= (point) (car c-macro-cache))
265 (or (and (cdr c-macro-cache)
266 (<= (point) (cdr c-macro-cache)))
267 (<= (point) c-macro-cache-start-pos)))
268 (unless (< (car c-macro-cache) (or lim (point-min)))
269 (progn (goto-char (max (or lim (point-min)) (car c-macro-cache)))
270 (setq c-macro-cache-start-pos
271 (max c-macro-cache-start-pos here))
272 t))
273 (setq c-macro-cache nil
274 c-macro-cache-start-pos nil
275 c-macro-cache-syntactic nil)
276
277 (save-restriction
278 (if lim (narrow-to-region lim (point-max)))
279 (beginning-of-line)
280 (while (eq (char-before (1- (point))) ?\\)
281 (forward-line -1))
282 (back-to-indentation)
283 (if (and (<= (point) here)
284 (looking-at c-opt-cpp-start))
285 (progn
286 (setq c-macro-cache (cons (point) nil)
287 c-macro-cache-start-pos here)
288 t)
289 (goto-char here)
290 nil))))))
291
292 (defun c-end-of-macro ()
293 "Go to the end of a preprocessor directive.
294 More accurately, move the point to the end of the closest following
295 line that doesn't end with a line continuation backslash - no check is
296 done that the point is inside a cpp directive to begin with.
297
298 Note that this function might do hidden buffer changes. See the
299 comment at the start of cc-engine.el for more info."
300 (if (and (cdr c-macro-cache)
301 (<= (point) (cdr c-macro-cache))
302 (>= (point) (car c-macro-cache)))
303 (goto-char (cdr c-macro-cache))
304 (unless (and (car c-macro-cache)
305 (<= (point) c-macro-cache-start-pos)
306 (>= (point) (car c-macro-cache)))
307 (setq c-macro-cache nil
308 c-macro-cache-start-pos nil
309 c-macro-cache-syntactic nil))
310 (while (progn
311 (end-of-line)
312 (when (and (eq (char-before) ?\\)
313 (not (eobp)))
314 (forward-char)
315 t)))
316 (when (car c-macro-cache)
317 (setcdr c-macro-cache (point)))))
318
319 (defun c-syntactic-end-of-macro ()
320 ;; Go to the end of a CPP directive, or a "safe" pos just before.
321 ;;
322 ;; This is normally the end of the next non-escaped line. A "safe"
323 ;; position is one not within a string or comment. (The EOL on a line
324 ;; comment is NOT "safe").
325 ;;
326 ;; This function must only be called from the beginning of a CPP construct.
327 ;;
328 ;; Note that this function might do hidden buffer changes. See the comment
329 ;; at the start of cc-engine.el for more info.
330 (let* ((here (point))
331 (there (progn (c-end-of-macro) (point)))
332 s)
333 (unless c-macro-cache-syntactic
334 (setq s (parse-partial-sexp here there))
335 (while (and (or (nth 3 s) ; in a string
336 (nth 4 s)) ; in a comment (maybe at end of line comment)
337 (> there here)) ; No infinite loops, please.
338 (setq there (1- (nth 8 s)))
339 (setq s (parse-partial-sexp here there)))
340 (setq c-macro-cache-syntactic (car c-macro-cache)))
341 (point)))
342
343 (defun c-forward-over-cpp-define-id ()
344 ;; Assuming point is at the "#" that introduces a preprocessor
345 ;; directive, it's moved forward to the end of the identifier which is
346 ;; "#define"d (or whatever c-opt-cpp-macro-define specifies). Non-nil
347 ;; is returned in this case, in all other cases nil is returned and
348 ;; point isn't moved.
349 ;;
350 ;; This function might do hidden buffer changes.
351 (when (and c-opt-cpp-macro-define-id
352 (looking-at c-opt-cpp-macro-define-id))
353 (goto-char (match-end 0))))
354
355 (defun c-forward-to-cpp-define-body ()
356 ;; Assuming point is at the "#" that introduces a preprocessor
357 ;; directive, it's moved forward to the start of the definition body
358 ;; if it's a "#define" (or whatever c-opt-cpp-macro-define
359 ;; specifies). Non-nil is returned in this case, in all other cases
360 ;; nil is returned and point isn't moved.
361 ;;
362 ;; This function might do hidden buffer changes.
363 (when (and c-opt-cpp-macro-define-start
364 (looking-at c-opt-cpp-macro-define-start)
365 (not (= (match-end 0) (c-point 'eol))))
366 (goto-char (match-end 0))))
367
368 \f
369 ;;; Basic utility functions.
370
371 (defun c-syntactic-content (from to paren-level)
372 ;; Return the given region as a string where all syntactic
373 ;; whitespace is removed or, where necessary, replaced with a single
374 ;; space. If PAREN-LEVEL is given then all parens in the region are
375 ;; collapsed to "()", "[]" etc.
376 ;;
377 ;; This function might do hidden buffer changes.
378
379 (save-excursion
380 (save-restriction
381 (narrow-to-region from to)
382 (goto-char from)
383 (let* ((parts (list nil)) (tail parts) pos in-paren)
384
385 (while (re-search-forward c-syntactic-ws-start to t)
386 (goto-char (setq pos (match-beginning 0)))
387 (c-forward-syntactic-ws)
388 (if (= (point) pos)
389 (forward-char)
390
391 (when paren-level
392 (save-excursion
393 (setq in-paren (= (car (parse-partial-sexp from pos 1)) 1)
394 pos (point))))
395
396 (if (and (> pos from)
397 (< (point) to)
398 (looking-at "\\w\\|\\s_")
399 (save-excursion
400 (goto-char (1- pos))
401 (looking-at "\\w\\|\\s_")))
402 (progn
403 (setcdr tail (list (buffer-substring-no-properties from pos)
404 " "))
405 (setq tail (cddr tail)))
406 (setcdr tail (list (buffer-substring-no-properties from pos)))
407 (setq tail (cdr tail)))
408
409 (when in-paren
410 (when (= (car (parse-partial-sexp pos to -1)) -1)
411 (setcdr tail (list (buffer-substring-no-properties
412 (1- (point)) (point))))
413 (setq tail (cdr tail))))
414
415 (setq from (point))))
416
417 (setcdr tail (list (buffer-substring-no-properties from to)))
418 (apply 'concat (cdr parts))))))
419
420 (defun c-shift-line-indentation (shift-amt)
421 ;; Shift the indentation of the current line with the specified
422 ;; amount (positive inwards). The buffer is modified only if
423 ;; SHIFT-AMT isn't equal to zero.
424 (let ((pos (- (point-max) (point)))
425 (c-macro-start c-macro-start)
426 tmp-char-inserted)
427 (if (zerop shift-amt)
428 nil
429 ;; If we're on an empty line inside a macro, we take the point
430 ;; to be at the current indentation and shift it to the
431 ;; appropriate column. This way we don't treat the extra
432 ;; whitespace out to the line continuation as indentation.
433 (when (and (c-query-and-set-macro-start)
434 (looking-at "[ \t]*\\\\$")
435 (save-excursion
436 (skip-chars-backward " \t")
437 (bolp)))
438 (insert ?x)
439 (backward-char)
440 (setq tmp-char-inserted t))
441 (unwind-protect
442 (let ((col (current-indentation)))
443 (delete-region (c-point 'bol) (c-point 'boi))
444 (beginning-of-line)
445 (indent-to (+ col shift-amt)))
446 (when tmp-char-inserted
447 (delete-char 1))))
448 ;; If initial point was within line's indentation and we're not on
449 ;; a line with a line continuation in a macro, position after the
450 ;; indentation. Else stay at same point in text.
451 (if (and (< (point) (c-point 'boi))
452 (not tmp-char-inserted))
453 (back-to-indentation)
454 (if (> (- (point-max) pos) (point))
455 (goto-char (- (point-max) pos))))))
456
457 (defsubst c-keyword-sym (keyword)
458 ;; Return non-nil if the string KEYWORD is a known keyword. More
459 ;; precisely, the value is the symbol for the keyword in
460 ;; `c-keywords-obarray'.
461 (intern-soft keyword c-keywords-obarray))
462
463 (defsubst c-keyword-member (keyword-sym lang-constant)
464 ;; Return non-nil if the symbol KEYWORD-SYM, as returned by
465 ;; `c-keyword-sym', is a member of LANG-CONSTANT, which is the name
466 ;; of a language constant that ends with "-kwds". If KEYWORD-SYM is
467 ;; nil then the result is nil.
468 (get keyword-sym lang-constant))
469
470 ;; String syntax chars, suitable for skip-syntax-(forward|backward).
471 (defconst c-string-syntax (if (memq 'gen-string-delim c-emacs-features)
472 "\"|"
473 "\""))
474
475 ;; Regexp matching string limit syntax.
476 (defconst c-string-limit-regexp (if (memq 'gen-string-delim c-emacs-features)
477 "\\s\"\\|\\s|"
478 "\\s\""))
479
480 ;; Regexp matching WS followed by string limit syntax.
481 (defconst c-ws*-string-limit-regexp
482 (concat "[ \t]*\\(" c-string-limit-regexp "\\)"))
483
484 ;; Holds formatted error strings for the few cases where parse errors
485 ;; are reported.
486 (defvar c-parsing-error nil)
487 (make-variable-buffer-local 'c-parsing-error)
488
489 (defun c-echo-parsing-error (&optional quiet)
490 (when (and c-report-syntactic-errors c-parsing-error (not quiet))
491 (c-benign-error "%s" c-parsing-error))
492 c-parsing-error)
493
494 ;; Faces given to comments and string literals. This is used in some
495 ;; situations to speed up recognition; it isn't mandatory that font
496 ;; locking is in use. This variable is extended with the face in
497 ;; `c-doc-face-name' when fontification is activated in cc-fonts.el.
498 (defvar c-literal-faces
499 (append '(font-lock-comment-face font-lock-string-face)
500 (when (facep 'font-lock-comment-delimiter-face)
501 ;; New in Emacs 22.
502 '(font-lock-comment-delimiter-face))))
503
504 (defsubst c-put-c-type-property (pos value)
505 ;; Put a c-type property with the given value at POS.
506 (c-put-char-property pos 'c-type value))
507
508 (defun c-clear-c-type-property (from to value)
509 ;; Remove all occurrences of the c-type property that has the given
510 ;; value in the region between FROM and TO. VALUE is assumed to not
511 ;; be nil.
512 ;;
513 ;; Note: This assumes that c-type is put on single chars only; it's
514 ;; very inefficient if matching properties cover large regions.
515 (save-excursion
516 (goto-char from)
517 (while (progn
518 (when (eq (get-text-property (point) 'c-type) value)
519 (c-clear-char-property (point) 'c-type))
520 (goto-char (next-single-property-change (point) 'c-type nil to))
521 (< (point) to)))))
522
523 \f
524 ;; Some debug tools to visualize various special positions. This
525 ;; debug code isn't as portable as the rest of CC Mode.
526
527 (cc-bytecomp-defun overlays-in)
528 (cc-bytecomp-defun overlay-get)
529 (cc-bytecomp-defun overlay-start)
530 (cc-bytecomp-defun overlay-end)
531 (cc-bytecomp-defun delete-overlay)
532 (cc-bytecomp-defun overlay-put)
533 (cc-bytecomp-defun make-overlay)
534
535 (defun c-debug-add-face (beg end face)
536 (c-save-buffer-state ((overlays (overlays-in beg end)) overlay)
537 (while overlays
538 (setq overlay (car overlays)
539 overlays (cdr overlays))
540 (when (eq (overlay-get overlay 'face) face)
541 (setq beg (min beg (overlay-start overlay))
542 end (max end (overlay-end overlay)))
543 (delete-overlay overlay)))
544 (overlay-put (make-overlay beg end) 'face face)))
545
546 (defun c-debug-remove-face (beg end face)
547 (c-save-buffer-state ((overlays (overlays-in beg end)) overlay
548 (ol-beg beg) (ol-end end))
549 (while overlays
550 (setq overlay (car overlays)
551 overlays (cdr overlays))
552 (when (eq (overlay-get overlay 'face) face)
553 (setq ol-beg (min ol-beg (overlay-start overlay))
554 ol-end (max ol-end (overlay-end overlay)))
555 (delete-overlay overlay)))
556 (when (< ol-beg beg)
557 (overlay-put (make-overlay ol-beg beg) 'face face))
558 (when (> ol-end end)
559 (overlay-put (make-overlay end ol-end) 'face face))))
560
561 \f
562 ;; `c-beginning-of-statement-1' and accompanying stuff.
563
564 ;; KLUDGE ALERT: c-maybe-labelp is used to pass information between
565 ;; c-crosses-statement-barrier-p and c-beginning-of-statement-1. A
566 ;; better way should be implemented, but this will at least shut up
567 ;; the byte compiler.
568 (defvar c-maybe-labelp)
569
570 ;; New awk-compatible version of c-beginning-of-statement-1, ACM 2002/6/22
571
572 ;; Macros used internally in c-beginning-of-statement-1 for the
573 ;; automaton actions.
574 (defmacro c-bos-push-state ()
575 '(setq stack (cons (cons state saved-pos)
576 stack)))
577 (defmacro c-bos-pop-state (&optional do-if-done)
578 `(if (setq state (car (car stack))
579 saved-pos (cdr (car stack))
580 stack (cdr stack))
581 t
582 ,do-if-done
583 (throw 'loop nil)))
584 (defmacro c-bos-pop-state-and-retry ()
585 '(throw 'loop (setq state (car (car stack))
586 saved-pos (cdr (car stack))
587 ;; Throw nil if stack is empty, else throw non-nil.
588 stack (cdr stack))))
589 (defmacro c-bos-save-pos ()
590 '(setq saved-pos (vector pos tok ptok pptok)))
591 (defmacro c-bos-restore-pos ()
592 '(unless (eq (elt saved-pos 0) start)
593 (setq pos (elt saved-pos 0)
594 tok (elt saved-pos 1)
595 ptok (elt saved-pos 2)
596 pptok (elt saved-pos 3))
597 (goto-char pos)
598 (setq sym nil)))
599 (defmacro c-bos-save-error-info (missing got)
600 `(setq saved-pos (vector pos ,missing ,got)))
601 (defmacro c-bos-report-error ()
602 '(unless noerror
603 (setq c-parsing-error
604 (format "No matching `%s' found for `%s' on line %d"
605 (elt saved-pos 1)
606 (elt saved-pos 2)
607 (1+ (count-lines (point-min)
608 (c-point 'bol (elt saved-pos 0))))))))
609
610 (defun c-beginning-of-statement-1 (&optional lim ignore-labels
611 noerror comma-delim)
612 "Move to the start of the current statement or declaration, or to
613 the previous one if already at the beginning of one. Only
614 statements/declarations on the same level are considered, i.e. don't
615 move into or out of sexps (not even normal expression parentheses).
616
617 If point is already at the earliest statement within braces or parens,
618 this function doesn't move back into any whitespace preceding it; it
619 returns 'same in this case.
620
621 Stop at statement continuation tokens like \"else\", \"catch\",
622 \"finally\" and the \"while\" in \"do ... while\" if the start point
623 is within the continuation. If starting at such a token, move to the
624 corresponding statement start. If at the beginning of a statement,
625 move to the closest containing statement if there is any. This might
626 also stop at a continuation clause.
627
628 Labels are treated as part of the following statements if
629 IGNORE-LABELS is non-nil. (FIXME: Doesn't work if we stop at a known
630 statement start keyword.) Otherwise, each label is treated as a
631 separate statement.
632
633 Macros are ignored \(i.e. skipped over) unless point is within one, in
634 which case the content of the macro is treated as normal code. Aside
635 from any normal statement starts found in it, stop at the first token
636 of the content in the macro, i.e. the expression of an \"#if\" or the
637 start of the definition in a \"#define\". Also stop at start of
638 macros before leaving them.
639
640 Return:
641 'label if stopped at a label or \"case...:\" or \"default:\";
642 'same if stopped at the beginning of the current statement;
643 'up if stepped to a containing statement;
644 'previous if stepped to a preceding statement;
645 'beginning if stepped from a statement continuation clause to
646 its start clause; or
647 'macro if stepped to a macro start.
648 Note that 'same and not 'label is returned if stopped at the same
649 label without crossing the colon character.
650
651 LIM may be given to limit the search. If the search hits the limit,
652 point will be left at the closest following token, or at the start
653 position if that is less ('same is returned in this case).
654
655 NOERROR turns off error logging to `c-parsing-error'.
656
657 Normally only ';' and virtual semicolons are considered to delimit
658 statements, but if COMMA-DELIM is non-nil then ',' is treated
659 as a delimiter too.
660
661 Note that this function might do hidden buffer changes. See the
662 comment at the start of cc-engine.el for more info."
663
664 ;; The bulk of this function is a pushdown automaton that looks at statement
665 ;; boundaries and the tokens (such as "while") in c-opt-block-stmt-key. Its
666 ;; purpose is to keep track of nested statements, ensuring that such
667 ;; statements are skipped over in their entirety (somewhat akin to what C-M-p
668 ;; does with nested braces/brackets/parentheses).
669 ;;
670 ;; Note: The position of a boundary is the following token.
671 ;;
672 ;; Beginning with the current token (the one following point), move back one
673 ;; sexp at a time (where a sexp is, more or less, either a token or the
674 ;; entire contents of a brace/bracket/paren pair). Each time a statement
675 ;; boundary is crossed or a "while"-like token is found, update the state of
676 ;; the PDA. Stop at the beginning of a statement when the stack (holding
677 ;; nested statement info) is empty and the position has been moved.
678 ;;
679 ;; The following variables constitute the PDA:
680 ;;
681 ;; sym: This is either the "while"-like token (e.g. 'for) we've just
682 ;; scanned back over, 'boundary if we've just gone back over a
683 ;; statement boundary, or nil otherwise.
684 ;; state: takes one of the values (nil else else-boundary while
685 ;; while-boundary catch catch-boundary).
686 ;; nil means "no "while"-like token yet scanned".
687 ;; 'else, for example, means "just gone back over an else".
688 ;; 'else-boundary means "just gone back over a statement boundary
689 ;; immediately after having gone back over an else".
690 ;; saved-pos: A vector of either saved positions (tok ptok pptok, etc.) or
691 ;; of error reporting information.
692 ;; stack: The stack onto which the PDA pushes its state. Each entry
693 ;; consists of a saved value of state and saved-pos. An entry is
694 ;; pushed when we move back over a "continuation" token (e.g. else)
695 ;; and popped when we encounter the corresponding opening token
696 ;; (e.g. if).
697 ;;
698 ;;
699 ;; The following diagram briefly outlines the PDA.
700 ;;
701 ;; Common state:
702 ;; "else": Push state, goto state `else'.
703 ;; "while": Push state, goto state `while'.
704 ;; "catch" or "finally": Push state, goto state `catch'.
705 ;; boundary: Pop state.
706 ;; other: Do nothing special.
707 ;;
708 ;; State `else':
709 ;; boundary: Goto state `else-boundary'.
710 ;; other: Error, pop state, retry token.
711 ;;
712 ;; State `else-boundary':
713 ;; "if": Pop state.
714 ;; boundary: Error, pop state.
715 ;; other: See common state.
716 ;;
717 ;; State `while':
718 ;; boundary: Save position, goto state `while-boundary'.
719 ;; other: Pop state, retry token.
720 ;;
721 ;; State `while-boundary':
722 ;; "do": Pop state.
723 ;; boundary: Restore position if it's not at start, pop state. [*see below]
724 ;; other: See common state.
725 ;;
726 ;; State `catch':
727 ;; boundary: Goto state `catch-boundary'.
728 ;; other: Error, pop state, retry token.
729 ;;
730 ;; State `catch-boundary':
731 ;; "try": Pop state.
732 ;; "catch": Goto state `catch'.
733 ;; boundary: Error, pop state.
734 ;; other: See common state.
735 ;;
736 ;; [*] In the `while-boundary' state, we had pushed a 'while state, and were
737 ;; searching for a "do" which would have opened a do-while. If we didn't
738 ;; find it, we discard the analysis done since the "while", go back to this
739 ;; token in the buffer and restart the scanning there, this time WITHOUT
740 ;; pushing the 'while state onto the stack.
741 ;;
742 ;; In addition to the above there is some special handling of labels
743 ;; and macros.
744
745 (let ((case-fold-search nil)
746 (start (point))
747 macro-start
748 (delims (if comma-delim '(?\; ?,) '(?\;)))
749 (c-stmt-delim-chars (if comma-delim
750 c-stmt-delim-chars-with-comma
751 c-stmt-delim-chars))
752 c-in-literal-cache c-maybe-labelp after-case:-pos saved
753 ;; Current position.
754 pos
755 ;; Position of last stmt boundary character (e.g. ;).
756 boundary-pos
757 ;; The position of the last sexp or bound that follows the
758 ;; first found colon, i.e. the start of the nonlabel part of
759 ;; the statement. It's `start' if a colon is found just after
760 ;; the start.
761 after-labels-pos
762 ;; Like `after-labels-pos', but the first such position inside
763 ;; a label, i.e. the start of the last label before the start
764 ;; of the nonlabel part of the statement.
765 last-label-pos
766 ;; The last position where a label is possible provided the
767 ;; statement started there. It's nil as long as no invalid
768 ;; label content has been found (according to
769 ;; `c-nonlabel-token-key'). It's `start' if no valid label
770 ;; content was found in the label. Note that we might still
771 ;; regard it a label if it starts with `c-label-kwds'.
772 label-good-pos
773 ;; Putative positions of the components of a bitfield declaration,
774 ;; e.g. "int foo : NUM_FOO_BITS ;"
775 bitfield-type-pos bitfield-id-pos bitfield-size-pos
776 ;; Symbol just scanned back over (e.g. 'while or 'boundary).
777 ;; See above.
778 sym
779 ;; Current state in the automaton. See above.
780 state
781 ;; Current saved positions. See above.
782 saved-pos
783 ;; Stack of conses (state . saved-pos).
784 stack
785 ;; Regexp which matches "for", "if", etc.
786 (cond-key (or c-opt-block-stmt-key
787 "\\<\\>")) ; Matches nothing.
788 ;; Return value.
789 (ret 'same)
790 ;; Positions of the last three sexps or bounds we've stopped at.
791 tok ptok pptok)
792
793 (save-restriction
794 (if lim (narrow-to-region lim (point-max)))
795
796 (if (save-excursion
797 (and (c-beginning-of-macro)
798 (/= (point) start)))
799 (setq macro-start (point)))
800
801 ;; Try to skip back over unary operator characters, to register
802 ;; that we've moved.
803 (while (progn
804 (setq pos (point))
805 (c-backward-syntactic-ws)
806 ;; Protect post-++/-- operators just before a virtual semicolon.
807 (and (not (c-at-vsemi-p))
808 (/= (skip-chars-backward "-+!*&~@`#") 0))))
809
810 ;; Skip back over any semicolon here. If it was a bare semicolon, we're
811 ;; done. Later on we ignore the boundaries for statements that don't
812 ;; contain any sexp. The only thing that is affected is that the error
813 ;; checking is a little less strict, and we really don't bother.
814 (if (and (memq (char-before) delims)
815 (progn (forward-char -1)
816 (setq saved (point))
817 (c-backward-syntactic-ws)
818 (or (memq (char-before) delims)
819 (memq (char-before) '(?: nil))
820 (eq (char-syntax (char-before)) ?\()
821 (c-at-vsemi-p))))
822 (setq ret 'previous
823 pos saved)
824
825 ;; Begin at start and not pos to detect macros if we stand
826 ;; directly after the #.
827 (goto-char start)
828 (if (looking-at "\\<\\|\\W")
829 ;; Record this as the first token if not starting inside it.
830 (setq tok start))
831
832
833 ;; The following while loop goes back one sexp (balanced parens,
834 ;; etc. with contents, or symbol or suchlike) each iteration. This
835 ;; movement is accomplished with a call to c-backward-sexp approx 170
836 ;; lines below.
837 ;;
838 ;; The loop is exited only by throwing nil to the (catch 'loop ...):
839 ;; 1. On reaching the start of a macro;
840 ;; 2. On having passed a stmt boundary with the PDA stack empty;
841 ;; 3. On reaching the start of an Objective C method def;
842 ;; 4. From macro `c-bos-pop-state'; when the stack is empty;
843 ;; 5. From macro `c-bos-pop-state-and-retry' when the stack is empty.
844 (while
845 (catch 'loop ;; Throw nil to break, non-nil to continue.
846 (cond
847 ;; Are we in a macro, just after the opening #?
848 ((save-excursion
849 (and macro-start ; Always NIL for AWK.
850 (progn (skip-chars-backward " \t")
851 (eq (char-before) ?#))
852 (progn (setq saved (1- (point)))
853 (beginning-of-line)
854 (not (eq (char-before (1- (point))) ?\\)))
855 (looking-at c-opt-cpp-start)
856 (progn (skip-chars-forward " \t")
857 (eq (point) saved))))
858 (goto-char saved)
859 (if (and (c-forward-to-cpp-define-body)
860 (progn (c-forward-syntactic-ws start)
861 (< (point) start)))
862 ;; Stop at the first token in the content of the macro.
863 (setq pos (point)
864 ignore-labels t) ; Avoid the label check on exit.
865 (setq pos saved
866 ret 'macro
867 ignore-labels t))
868 (throw 'loop nil)) ; 1. Start of macro.
869
870 ;; Do a round through the automaton if we've just passed a
871 ;; statement boundary or passed a "while"-like token.
872 ((or sym
873 (and (looking-at cond-key)
874 (setq sym (intern (match-string 1)))))
875
876 (when (and (< pos start) (null stack))
877 (throw 'loop nil)) ; 2. Statement boundary.
878
879 ;; The PDA state handling.
880 ;;
881 ;; Refer to the description of the PDA in the opening
882 ;; comments. In the following OR form, the first leaf
883 ;; attempts to handles one of the specific actions detailed
884 ;; (e.g., finding token "if" whilst in state `else-boundary').
885 ;; We drop through to the second leaf (which handles common
886 ;; state) if no specific handler is found in the first cond.
887 ;; If a parsing error is detected (e.g. an "else" with no
888 ;; preceding "if"), we throw to the enclosing catch.
889 ;;
890 ;; Note that the (eq state 'else) means
891 ;; "we've just passed an else", NOT "we're looking for an
892 ;; else".
893 (or (cond
894 ((eq state 'else)
895 (if (eq sym 'boundary)
896 (setq state 'else-boundary)
897 (c-bos-report-error)
898 (c-bos-pop-state-and-retry)))
899
900 ((eq state 'else-boundary)
901 (cond ((eq sym 'if)
902 (c-bos-pop-state (setq ret 'beginning)))
903 ((eq sym 'boundary)
904 (c-bos-report-error)
905 (c-bos-pop-state))))
906
907 ((eq state 'while)
908 (if (and (eq sym 'boundary)
909 ;; Since this can cause backtracking we do a
910 ;; little more careful analysis to avoid it:
911 ;; If there's a label in front of the while
912 ;; it can't be part of a do-while.
913 (not after-labels-pos))
914 (progn (c-bos-save-pos)
915 (setq state 'while-boundary))
916 (c-bos-pop-state-and-retry))) ; Can't be a do-while
917
918 ((eq state 'while-boundary)
919 (cond ((eq sym 'do)
920 (c-bos-pop-state (setq ret 'beginning)))
921 ((eq sym 'boundary) ; isn't a do-while
922 (c-bos-restore-pos) ; the position of the while
923 (c-bos-pop-state)))) ; no longer searching for do.
924
925 ((eq state 'catch)
926 (if (eq sym 'boundary)
927 (setq state 'catch-boundary)
928 (c-bos-report-error)
929 (c-bos-pop-state-and-retry)))
930
931 ((eq state 'catch-boundary)
932 (cond
933 ((eq sym 'try)
934 (c-bos-pop-state (setq ret 'beginning)))
935 ((eq sym 'catch)
936 (setq state 'catch))
937 ((eq sym 'boundary)
938 (c-bos-report-error)
939 (c-bos-pop-state)))))
940
941 ;; This is state common. We get here when the previous
942 ;; cond statement found no particular state handler.
943 (cond ((eq sym 'boundary)
944 ;; If we have a boundary at the start
945 ;; position we push a frame to go to the
946 ;; previous statement.
947 (if (>= pos start)
948 (c-bos-push-state)
949 (c-bos-pop-state)))
950 ((eq sym 'else)
951 (c-bos-push-state)
952 (c-bos-save-error-info 'if 'else)
953 (setq state 'else))
954 ((eq sym 'while)
955 ;; Is this a real while, or a do-while?
956 ;; The next `when' triggers unless we are SURE that
957 ;; the `while' is not the tail end of a `do-while'.
958 (when (or (not pptok)
959 (memq (char-after pptok) delims)
960 ;; The following kludge is to prevent
961 ;; infinite recursion when called from
962 ;; c-awk-after-if-for-while-condition-p,
963 ;; or the like.
964 (and (eq (point) start)
965 (c-vsemi-status-unknown-p))
966 (c-at-vsemi-p pptok))
967 ;; Since this can cause backtracking we do a
968 ;; little more careful analysis to avoid it: If
969 ;; the while isn't followed by a (possibly
970 ;; virtual) semicolon it can't be a do-while.
971 (c-bos-push-state)
972 (setq state 'while)))
973 ((memq sym '(catch finally))
974 (c-bos-push-state)
975 (c-bos-save-error-info 'try sym)
976 (setq state 'catch))))
977
978 (when c-maybe-labelp
979 ;; We're either past a statement boundary or at the
980 ;; start of a statement, so throw away any label data
981 ;; for the previous one.
982 (setq after-labels-pos nil
983 last-label-pos nil
984 c-maybe-labelp nil))))
985
986 ;; Step to the previous sexp, but not if we crossed a
987 ;; boundary, since that doesn't consume an sexp.
988 (if (eq sym 'boundary)
989 (setq ret 'previous)
990
991 ;; HERE IS THE SINGLE PLACE INSIDE THE PDA LOOP WHERE WE MOVE
992 ;; BACKWARDS THROUGH THE SOURCE.
993
994 (c-backward-syntactic-ws)
995 (let ((before-sws-pos (point))
996 ;; The end position of the area to search for statement
997 ;; barriers in this round.
998 (maybe-after-boundary-pos pos))
999
1000 ;; Go back over exactly one logical sexp, taking proper
1001 ;; account of macros and escaped EOLs.
1002 (while
1003 (progn
1004 (unless (c-safe (c-backward-sexp) t)
1005 ;; Give up if we hit an unbalanced block. Since the
1006 ;; stack won't be empty the code below will report a
1007 ;; suitable error.
1008 (throw 'loop nil))
1009 (cond
1010 ;; Have we moved into a macro?
1011 ((and (not macro-start)
1012 (c-beginning-of-macro))
1013 ;; Have we crossed a statement boundary? If not,
1014 ;; keep going back until we find one or a "real" sexp.
1015 (and
1016 (save-excursion
1017 (c-end-of-macro)
1018 (not (c-crosses-statement-barrier-p
1019 (point) maybe-after-boundary-pos)))
1020 (setq maybe-after-boundary-pos (point))))
1021 ;; Have we just gone back over an escaped NL? This
1022 ;; doesn't count as a sexp.
1023 ((looking-at "\\\\$")))))
1024
1025 ;; Have we crossed a statement boundary?
1026 (setq boundary-pos
1027 (cond
1028 ;; Are we at a macro beginning?
1029 ((and (not macro-start)
1030 c-opt-cpp-prefix
1031 (looking-at c-opt-cpp-prefix))
1032 (save-excursion
1033 (c-end-of-macro)
1034 (c-crosses-statement-barrier-p
1035 (point) maybe-after-boundary-pos)))
1036 ;; Just gone back over a brace block?
1037 ((and
1038 (eq (char-after) ?{)
1039 (not (c-looking-at-inexpr-block lim nil t)))
1040 (save-excursion
1041 (c-forward-sexp) (point)))
1042 ;; Just gone back over some paren block?
1043 ((looking-at "\\s\(")
1044 (save-excursion
1045 (goto-char (1+ (c-down-list-backward
1046 before-sws-pos)))
1047 (c-crosses-statement-barrier-p
1048 (point) maybe-after-boundary-pos)))
1049 ;; Just gone back over an ordinary symbol of some sort?
1050 (t (c-crosses-statement-barrier-p
1051 (point) maybe-after-boundary-pos))))
1052
1053 (when boundary-pos
1054 (setq pptok ptok
1055 ptok tok
1056 tok boundary-pos
1057 sym 'boundary)
1058 ;; Like a C "continue". Analyze the next sexp.
1059 (throw 'loop t))))
1060
1061 ;; ObjC method def?
1062 (when (and c-opt-method-key
1063 (setq saved (c-in-method-def-p)))
1064 (setq pos saved
1065 ignore-labels t) ; Avoid the label check on exit.
1066 (throw 'loop nil)) ; 3. ObjC method def.
1067
1068 ;; Might we have a bitfield declaration, "<type> <id> : <size>"?
1069 (if c-has-bitfields
1070 (cond
1071 ;; The : <size> and <id> fields?
1072 ((and (numberp c-maybe-labelp)
1073 (not bitfield-size-pos)
1074 (save-excursion
1075 (goto-char (or tok start))
1076 (not (looking-at c-keywords-regexp)))
1077 (not (looking-at c-keywords-regexp))
1078 (not (c-punctuation-in (point) c-maybe-labelp)))
1079 (setq bitfield-size-pos (or tok start)
1080 bitfield-id-pos (point)))
1081 ;; The <type> field?
1082 ((and bitfield-id-pos
1083 (not bitfield-type-pos))
1084 (if (and (looking-at c-symbol-key) ; Can only be an integer type. :-)
1085 (not (looking-at c-not-primitive-type-keywords-regexp))
1086 (not (c-punctuation-in (point) tok)))
1087 (setq bitfield-type-pos (point))
1088 (setq bitfield-size-pos nil
1089 bitfield-id-pos nil)))))
1090
1091 ;; Handle labels.
1092 (unless (eq ignore-labels t)
1093 (when (numberp c-maybe-labelp)
1094 ;; `c-crosses-statement-barrier-p' has found a colon, so we
1095 ;; might be in a label now. Have we got a real label
1096 ;; (including a case label) or something like C++'s "public:"?
1097 ;; A case label might use an expression rather than a token.
1098 (setq after-case:-pos (or tok start))
1099 (if (or (looking-at c-nonlabel-token-key) ; e.g. "while" or "'a'"
1100 ;; Catch C++'s inheritance construct "class foo : bar".
1101 (save-excursion
1102 (and
1103 (c-safe (c-backward-sexp) t)
1104 (looking-at c-nonlabel-token-2-key))))
1105 (setq c-maybe-labelp nil)
1106 (if after-labels-pos ; Have we already encountered a label?
1107 (if (not last-label-pos)
1108 (setq last-label-pos (or tok start)))
1109 (setq after-labels-pos (or tok start)))
1110 (setq c-maybe-labelp t
1111 label-good-pos nil))) ; bogus "label"
1112
1113 (when (and (not label-good-pos) ; i.e. no invalid "label"'s yet
1114 ; been found.
1115 (looking-at c-nonlabel-token-key)) ; e.g. "while :"
1116 ;; We're in a potential label and it's the first
1117 ;; time we've found something that isn't allowed in
1118 ;; one.
1119 (setq label-good-pos (or tok start))))
1120
1121 ;; We've moved back by a sexp, so update the token positions.
1122 (setq sym nil
1123 pptok ptok
1124 ptok tok
1125 tok (point)
1126 pos tok) ; always non-nil
1127 ) ; end of (catch loop ....)
1128 ) ; end of sexp-at-a-time (while ....)
1129
1130 ;; If the stack isn't empty there might be errors to report.
1131 (while stack
1132 (if (and (vectorp saved-pos) (eq (length saved-pos) 3))
1133 (c-bos-report-error))
1134 (setq saved-pos (cdr (car stack))
1135 stack (cdr stack)))
1136
1137 (when (and (eq ret 'same)
1138 (not (memq sym '(boundary ignore nil))))
1139 ;; Need to investigate closer whether we've crossed
1140 ;; between a substatement and its containing statement.
1141 (if (setq saved (if (looking-at c-block-stmt-1-key)
1142 ptok
1143 pptok))
1144 (cond ((> start saved) (setq pos saved))
1145 ((= start saved) (setq ret 'up)))))
1146
1147 (when (and (not ignore-labels)
1148 (eq c-maybe-labelp t)
1149 (not (eq ret 'beginning))
1150 after-labels-pos
1151 (not bitfield-type-pos) ; Bitfields take precedence over labels.
1152 (or (not label-good-pos)
1153 (<= label-good-pos pos)
1154 (progn
1155 (goto-char (if (and last-label-pos
1156 (< last-label-pos start))
1157 last-label-pos
1158 pos))
1159 (looking-at c-label-kwds-regexp))))
1160 ;; We're in a label. Maybe we should step to the statement
1161 ;; after it.
1162 (if (< after-labels-pos start)
1163 (setq pos after-labels-pos)
1164 (setq ret 'label)
1165 (if (and last-label-pos (< last-label-pos start))
1166 ;; Might have jumped over several labels. Go to the last one.
1167 (setq pos last-label-pos)))))
1168
1169 ;; Have we got "case <expression>:"?
1170 (goto-char pos)
1171 (when (and after-case:-pos
1172 (not (eq ret 'beginning))
1173 (looking-at c-case-kwds-regexp))
1174 (if (< after-case:-pos start)
1175 (setq pos after-case:-pos))
1176 (if (eq ret 'same)
1177 (setq ret 'label)))
1178
1179 ;; Skip over the unary operators that can start the statement.
1180 (while (progn
1181 (c-backward-syntactic-ws)
1182 ;; protect AWK post-inc/decrement operators, etc.
1183 (and (not (c-at-vsemi-p (point)))
1184 (/= (skip-chars-backward "-+!*&~@`#") 0)))
1185 (setq pos (point)))
1186 (goto-char pos)
1187 ret)))
1188
1189 (defun c-punctuation-in (from to)
1190 "Return non-nil if there is a non-comment non-macro punctuation character
1191 between FROM and TO. FROM must not be in a string or comment. The returned
1192 value is the position of the first such character."
1193 (save-excursion
1194 (goto-char from)
1195 (let ((pos (point)))
1196 (while (progn (skip-chars-forward c-symbol-chars to)
1197 (c-forward-syntactic-ws to)
1198 (> (point) pos))
1199 (setq pos (point))))
1200 (and (< (point) to) (point))))
1201
1202 (defun c-crosses-statement-barrier-p (from to)
1203 "Return non-nil if buffer positions FROM to TO cross one or more
1204 statement or declaration boundaries. The returned value is actually
1205 the position of the earliest boundary char. FROM must not be within
1206 a string or comment.
1207
1208 The variable `c-maybe-labelp' is set to the position of the first `:' that
1209 might start a label (i.e. not part of `::' and not preceded by `?'). If a
1210 single `?' is found, then `c-maybe-labelp' is cleared.
1211
1212 For AWK, a statement which is terminated by an EOL (not a \; or a }) is
1213 regarded as having a \"virtual semicolon\" immediately after the last token on
1214 the line. If this virtual semicolon is _at_ from, the function recognizes it.
1215
1216 Note that this function might do hidden buffer changes. See the
1217 comment at the start of cc-engine.el for more info."
1218 (let* ((skip-chars
1219 ;; If the current language has CPP macros, insert # into skip-chars.
1220 (if c-opt-cpp-symbol
1221 (concat (substring c-stmt-delim-chars 0 1) ; "^"
1222 c-opt-cpp-symbol ; usually "#"
1223 (substring c-stmt-delim-chars 1)) ; e.g. ";{}?:"
1224 c-stmt-delim-chars))
1225 (non-skip-list
1226 (append (substring skip-chars 1) nil)) ; e.g. (?# ?\; ?{ ?} ?? ?:)
1227 lit-range vsemi-pos)
1228 (save-restriction
1229 (widen)
1230 (save-excursion
1231 (catch 'done
1232 (goto-char from)
1233 (while (progn (skip-chars-forward
1234 skip-chars
1235 (min to (c-point 'bonl)))
1236 (< (point) to))
1237 (cond
1238 ;; Virtual semicolon?
1239 ((and (bolp)
1240 (save-excursion
1241 (progn
1242 (if (setq lit-range (c-literal-limits from)) ; Have we landed in a string/comment?
1243 (goto-char (car lit-range)))
1244 (c-backward-syntactic-ws) ; ? put a limit here, maybe?
1245 (setq vsemi-pos (point))
1246 (c-at-vsemi-p))))
1247 (throw 'done vsemi-pos))
1248 ;; In a string/comment?
1249 ((setq lit-range (c-literal-limits from))
1250 (goto-char (cdr lit-range)))
1251 ((eq (char-after) ?:)
1252 (forward-char)
1253 (if (and (eq (char-after) ?:)
1254 (< (point) to))
1255 ;; Ignore scope operators.
1256 (forward-char)
1257 (setq c-maybe-labelp (1- (point)))))
1258 ((eq (char-after) ??)
1259 ;; A question mark. Can't be a label, so stop
1260 ;; looking for more : and ?.
1261 (setq c-maybe-labelp nil
1262 skip-chars (substring c-stmt-delim-chars 0 -2)))
1263 ;; At a CPP construct?
1264 ((and c-opt-cpp-symbol (looking-at c-opt-cpp-symbol)
1265 (save-excursion
1266 (forward-line 0)
1267 (looking-at c-opt-cpp-prefix)))
1268 (c-end-of-macro))
1269 ((memq (char-after) non-skip-list)
1270 (throw 'done (point)))))
1271 ;; In trailing space after an as yet undetected virtual semicolon?
1272 (c-backward-syntactic-ws from)
1273 (if (and (< (point) to)
1274 (c-at-vsemi-p))
1275 (point)
1276 nil))))))
1277
1278 (defun c-at-statement-start-p ()
1279 "Return non-nil if the point is at the first token in a statement
1280 or somewhere in the syntactic whitespace before it.
1281
1282 A \"statement\" here is not restricted to those inside code blocks.
1283 Any kind of declaration-like construct that occur outside function
1284 bodies is also considered a \"statement\".
1285
1286 Note that this function might do hidden buffer changes. See the
1287 comment at the start of cc-engine.el for more info."
1288
1289 (save-excursion
1290 (let ((end (point))
1291 c-maybe-labelp)
1292 (c-syntactic-skip-backward (substring c-stmt-delim-chars 1) nil t)
1293 (or (bobp)
1294 (eq (char-before) ?})
1295 (and (eq (char-before) ?{)
1296 (not (and c-special-brace-lists
1297 (progn (backward-char)
1298 (c-looking-at-special-brace-list)))))
1299 (c-crosses-statement-barrier-p (point) end)))))
1300
1301 (defun c-at-expression-start-p ()
1302 "Return non-nil if the point is at the first token in an expression or
1303 statement, or somewhere in the syntactic whitespace before it.
1304
1305 An \"expression\" here is a bit different from the normal language
1306 grammar sense: It's any sequence of expression tokens except commas,
1307 unless they are enclosed inside parentheses of some kind. Also, an
1308 expression never continues past an enclosing parenthesis, but it might
1309 contain parenthesis pairs of any sort except braces.
1310
1311 Since expressions never cross statement boundaries, this function also
1312 recognizes statement beginnings, just like `c-at-statement-start-p'.
1313
1314 Note that this function might do hidden buffer changes. See the
1315 comment at the start of cc-engine.el for more info."
1316
1317 (save-excursion
1318 (let ((end (point))
1319 (c-stmt-delim-chars c-stmt-delim-chars-with-comma)
1320 c-maybe-labelp)
1321 (c-syntactic-skip-backward (substring c-stmt-delim-chars 1) nil t)
1322 (or (bobp)
1323 (memq (char-before) '(?{ ?}))
1324 (save-excursion (backward-char)
1325 (looking-at "\\s("))
1326 (c-crosses-statement-barrier-p (point) end)))))
1327
1328 \f
1329 ;; A set of functions that covers various idiosyncrasies in
1330 ;; implementations of `forward-comment'.
1331
1332 ;; Note: Some emacsen considers incorrectly that any line comment
1333 ;; ending with a backslash continues to the next line. I can't think
1334 ;; of any way to work around that in a reliable way without changing
1335 ;; the buffer, though. Suggestions welcome. ;) (No, temporarily
1336 ;; changing the syntax for backslash doesn't work since we must treat
1337 ;; escapes in string literals correctly.)
1338
1339 (defun c-forward-single-comment ()
1340 "Move forward past whitespace and the closest following comment, if any.
1341 Return t if a comment was found, nil otherwise. In either case, the
1342 point is moved past the following whitespace. Line continuations,
1343 i.e. a backslashes followed by line breaks, are treated as whitespace.
1344 The line breaks that end line comments are considered to be the
1345 comment enders, so the point will be put on the beginning of the next
1346 line if it moved past a line comment.
1347
1348 This function does not do any hidden buffer changes."
1349
1350 (let ((start (point)))
1351 (when (looking-at "\\([ \t\n\r\f\v]\\|\\\\[\n\r]\\)+")
1352 (goto-char (match-end 0)))
1353
1354 (when (forward-comment 1)
1355 (if (eobp)
1356 ;; Some emacsen (e.g. XEmacs 21) return t when moving
1357 ;; forwards at eob.
1358 nil
1359
1360 ;; Emacs includes the ending newline in a b-style (c++)
1361 ;; comment, but XEmacs doesn't. We depend on the Emacs
1362 ;; behavior (which also is symmetric).
1363 (if (and (eolp) (elt (parse-partial-sexp start (point)) 7))
1364 (condition-case nil (forward-char 1)))
1365
1366 t))))
1367
1368 (defsubst c-forward-comments ()
1369 "Move forward past all following whitespace and comments.
1370 Line continuations, i.e. a backslashes followed by line breaks, are
1371 treated as whitespace.
1372
1373 Note that this function might do hidden buffer changes. See the
1374 comment at the start of cc-engine.el for more info."
1375
1376 (while (or
1377 ;; If forward-comment in at least XEmacs 21 is given a large
1378 ;; positive value, it'll loop all the way through if it hits
1379 ;; eob.
1380 (and (forward-comment 5)
1381 ;; Some emacsen (e.g. XEmacs 21) return t when moving
1382 ;; forwards at eob.
1383 (not (eobp)))
1384
1385 (when (looking-at "\\\\[\n\r]")
1386 (forward-char 2)
1387 t))))
1388
1389 (defun c-backward-single-comment ()
1390 "Move backward past whitespace and the closest preceding comment, if any.
1391 Return t if a comment was found, nil otherwise. In either case, the
1392 point is moved past the preceding whitespace. Line continuations,
1393 i.e. a backslashes followed by line breaks, are treated as whitespace.
1394 The line breaks that end line comments are considered to be the
1395 comment enders, so the point cannot be at the end of the same line to
1396 move over a line comment.
1397
1398 This function does not do any hidden buffer changes."
1399
1400 (let ((start (point)))
1401 ;; When we got newline terminated comments, forward-comment in all
1402 ;; supported emacsen so far will stop at eol of each line not
1403 ;; ending with a comment when moving backwards. This corrects for
1404 ;; that, and at the same time handles line continuations.
1405 (while (progn
1406 (skip-chars-backward " \t\n\r\f\v")
1407 (and (looking-at "[\n\r]")
1408 (eq (char-before) ?\\)))
1409 (backward-char))
1410
1411 (if (bobp)
1412 ;; Some emacsen (e.g. Emacs 19.34) return t when moving
1413 ;; backwards at bob.
1414 nil
1415
1416 ;; Leave point after the closest following newline if we've
1417 ;; backed up over any above, since forward-comment won't move
1418 ;; backward over a line comment if point is at the end of the
1419 ;; same line.
1420 (re-search-forward "\\=\\s *[\n\r]" start t)
1421
1422 (if (if (let (open-paren-in-column-0-is-defun-start) (forward-comment -1))
1423 (if (eolp)
1424 ;; If forward-comment above succeeded and we're at eol
1425 ;; then the newline we moved over above didn't end a
1426 ;; line comment, so we give it another go.
1427 (let (open-paren-in-column-0-is-defun-start)
1428 (forward-comment -1))
1429 t))
1430
1431 ;; Emacs <= 20 and XEmacs move back over the closer of a
1432 ;; block comment that lacks an opener.
1433 (if (looking-at "\\*/")
1434 (progn (forward-char 2) nil)
1435 t)))))
1436
1437 (defsubst c-backward-comments ()
1438 "Move backward past all preceding whitespace and comments.
1439 Line continuations, i.e. a backslashes followed by line breaks, are
1440 treated as whitespace. The line breaks that end line comments are
1441 considered to be the comment enders, so the point cannot be at the end
1442 of the same line to move over a line comment. Unlike
1443 c-backward-syntactic-ws, this function doesn't move back over
1444 preprocessor directives.
1445
1446 Note that this function might do hidden buffer changes. See the
1447 comment at the start of cc-engine.el for more info."
1448
1449 (let ((start (point)))
1450 (while (and
1451 ;; `forward-comment' in some emacsen (e.g. XEmacs 21.4)
1452 ;; return t when moving backwards at bob.
1453 (not (bobp))
1454
1455 (if (let (open-paren-in-column-0-is-defun-start moved-comment)
1456 (while
1457 (and (not (setq moved-comment (forward-comment -1)))
1458 ;; Cope specifically with ^M^J here -
1459 ;; forward-comment sometimes gets stuck after ^Ms,
1460 ;; sometimes after ^M^J.
1461 (or
1462 (when (eq (char-before) ?\r)
1463 (backward-char)
1464 t)
1465 (when (and (eq (char-before) ?\n)
1466 (eq (char-before (1- (point))) ?\r))
1467 (backward-char 2)
1468 t))))
1469 moved-comment)
1470 (if (looking-at "\\*/")
1471 ;; Emacs <= 20 and XEmacs move back over the
1472 ;; closer of a block comment that lacks an opener.
1473 (progn (forward-char 2) nil)
1474 t)
1475
1476 ;; XEmacs treats line continuations as whitespace but
1477 ;; only in the backward direction, which seems a bit
1478 ;; odd. Anyway, this is necessary for Emacs.
1479 (when (and (looking-at "[\n\r]")
1480 (eq (char-before) ?\\)
1481 (< (point) start))
1482 (backward-char)
1483 t))))))
1484
1485 \f
1486 ;; Tools for skipping over syntactic whitespace.
1487
1488 ;; The following functions use text properties to cache searches over
1489 ;; large regions of syntactic whitespace. It works as follows:
1490 ;;
1491 ;; o If a syntactic whitespace region contains anything but simple
1492 ;; whitespace (i.e. space, tab and line breaks), the text property
1493 ;; `c-in-sws' is put over it. At places where we have stopped
1494 ;; within that region there's also a `c-is-sws' text property.
1495 ;; That since there typically are nested whitespace inside that
1496 ;; must be handled separately, e.g. whitespace inside a comment or
1497 ;; cpp directive. Thus, from one point with `c-is-sws' it's safe
1498 ;; to jump to another point with that property within the same
1499 ;; `c-in-sws' region. It can be likened to a ladder where
1500 ;; `c-in-sws' marks the bars and `c-is-sws' the rungs.
1501 ;;
1502 ;; o The `c-is-sws' property is put on the simple whitespace chars at
1503 ;; a "rung position" and also maybe on the first following char.
1504 ;; As many characters as can be conveniently found in this range
1505 ;; are marked, but no assumption can be made that the whole range
1506 ;; is marked (it could be clobbered by later changes, for
1507 ;; instance).
1508 ;;
1509 ;; Note that some part of the beginning of a sequence of simple
1510 ;; whitespace might be part of the end of a preceding line comment
1511 ;; or cpp directive and must not be considered part of the "rung".
1512 ;; Such whitespace is some amount of horizontal whitespace followed
1513 ;; by a newline. In the case of cpp directives it could also be
1514 ;; two newlines with horizontal whitespace between them.
1515 ;;
1516 ;; The reason to include the first following char is to cope with
1517 ;; "rung positions" that doesn't have any ordinary whitespace. If
1518 ;; `c-is-sws' is put on a token character it does not have
1519 ;; `c-in-sws' set simultaneously. That's the only case when that
1520 ;; can occur, and the reason for not extending the `c-in-sws'
1521 ;; region to cover it is that the `c-in-sws' region could then be
1522 ;; accidentally merged with a following one if the token is only
1523 ;; one character long.
1524 ;;
1525 ;; o On buffer changes the `c-in-sws' and `c-is-sws' properties are
1526 ;; removed in the changed region. If the change was inside
1527 ;; syntactic whitespace that means that the "ladder" is broken, but
1528 ;; a later call to `c-forward-sws' or `c-backward-sws' will use the
1529 ;; parts on either side and use an ordinary search only to "repair"
1530 ;; the gap.
1531 ;;
1532 ;; Special care needs to be taken if a region is removed: If there
1533 ;; are `c-in-sws' on both sides of it which do not connect inside
1534 ;; the region then they can't be joined. If e.g. a marked macro is
1535 ;; broken, syntactic whitespace inside the new text might be
1536 ;; marked. If those marks would become connected with the old
1537 ;; `c-in-sws' range around the macro then we could get a ladder
1538 ;; with one end outside the macro and the other at some whitespace
1539 ;; within it.
1540 ;;
1541 ;; The main motivation for this system is to increase the speed in
1542 ;; skipping over the large whitespace regions that can occur at the
1543 ;; top level in e.g. header files that contain a lot of comments and
1544 ;; cpp directives. For small comments inside code it's probably
1545 ;; slower than using `forward-comment' straightforwardly, but speed is
1546 ;; not a significant factor there anyway.
1547
1548 ; (defface c-debug-is-sws-face
1549 ; '((t (:background "GreenYellow")))
1550 ; "Debug face to mark the `c-is-sws' property.")
1551 ; (defface c-debug-in-sws-face
1552 ; '((t (:underline t)))
1553 ; "Debug face to mark the `c-in-sws' property.")
1554
1555 ; (defun c-debug-put-sws-faces ()
1556 ; ;; Put the sws debug faces on all the `c-is-sws' and `c-in-sws'
1557 ; ;; properties in the buffer.
1558 ; (interactive)
1559 ; (save-excursion
1560 ; (c-save-buffer-state (in-face)
1561 ; (goto-char (point-min))
1562 ; (setq in-face (if (get-text-property (point) 'c-is-sws)
1563 ; (point)))
1564 ; (while (progn
1565 ; (goto-char (next-single-property-change
1566 ; (point) 'c-is-sws nil (point-max)))
1567 ; (if in-face
1568 ; (progn
1569 ; (c-debug-add-face in-face (point) 'c-debug-is-sws-face)
1570 ; (setq in-face nil))
1571 ; (setq in-face (point)))
1572 ; (not (eobp))))
1573 ; (goto-char (point-min))
1574 ; (setq in-face (if (get-text-property (point) 'c-in-sws)
1575 ; (point)))
1576 ; (while (progn
1577 ; (goto-char (next-single-property-change
1578 ; (point) 'c-in-sws nil (point-max)))
1579 ; (if in-face
1580 ; (progn
1581 ; (c-debug-add-face in-face (point) 'c-debug-in-sws-face)
1582 ; (setq in-face nil))
1583 ; (setq in-face (point)))
1584 ; (not (eobp)))))))
1585
1586 (defmacro c-debug-sws-msg (&rest args)
1587 ;;`(message ,@args)
1588 )
1589
1590 (defmacro c-put-is-sws (beg end)
1591 ;; This macro does a hidden buffer change.
1592 `(let ((beg ,beg) (end ,end))
1593 (put-text-property beg end 'c-is-sws t)
1594 ,@(when (facep 'c-debug-is-sws-face)
1595 `((c-debug-add-face beg end 'c-debug-is-sws-face)))))
1596
1597 (defmacro c-put-in-sws (beg end)
1598 ;; This macro does a hidden buffer change.
1599 `(let ((beg ,beg) (end ,end))
1600 (put-text-property beg end 'c-in-sws t)
1601 ,@(when (facep 'c-debug-is-sws-face)
1602 `((c-debug-add-face beg end 'c-debug-in-sws-face)))))
1603
1604 (defmacro c-remove-is-sws (beg end)
1605 ;; This macro does a hidden buffer change.
1606 `(let ((beg ,beg) (end ,end))
1607 (remove-text-properties beg end '(c-is-sws nil))
1608 ,@(when (facep 'c-debug-is-sws-face)
1609 `((c-debug-remove-face beg end 'c-debug-is-sws-face)))))
1610
1611 (defmacro c-remove-in-sws (beg end)
1612 ;; This macro does a hidden buffer change.
1613 `(let ((beg ,beg) (end ,end))
1614 (remove-text-properties beg end '(c-in-sws nil))
1615 ,@(when (facep 'c-debug-is-sws-face)
1616 `((c-debug-remove-face beg end 'c-debug-in-sws-face)))))
1617
1618 (defmacro c-remove-is-and-in-sws (beg end)
1619 ;; This macro does a hidden buffer change.
1620 `(let ((beg ,beg) (end ,end))
1621 (remove-text-properties beg end '(c-is-sws nil c-in-sws nil))
1622 ,@(when (facep 'c-debug-is-sws-face)
1623 `((c-debug-remove-face beg end 'c-debug-is-sws-face)
1624 (c-debug-remove-face beg end 'c-debug-in-sws-face)))))
1625
1626 (defsubst c-invalidate-sws-region-after (beg end)
1627 ;; Called from `after-change-functions'. Note that if
1628 ;; `c-forward-sws' or `c-backward-sws' are used outside
1629 ;; `c-save-buffer-state' or similar then this will remove the cache
1630 ;; properties right after they're added.
1631 ;;
1632 ;; This function does hidden buffer changes.
1633
1634 (save-excursion
1635 ;; Adjust the end to remove the properties in any following simple
1636 ;; ws up to and including the next line break, if there is any
1637 ;; after the changed region. This is necessary e.g. when a rung
1638 ;; marked empty line is converted to a line comment by inserting
1639 ;; "//" before the line break. In that case the line break would
1640 ;; keep the rung mark which could make a later `c-backward-sws'
1641 ;; move into the line comment instead of over it.
1642 (goto-char end)
1643 (skip-chars-forward " \t\f\v")
1644 (when (and (eolp) (not (eobp)))
1645 (setq end (1+ (point)))))
1646
1647 (when (and (= beg end)
1648 (get-text-property beg 'c-in-sws)
1649 (> beg (point-min))
1650 (get-text-property (1- beg) 'c-in-sws))
1651 ;; Ensure that an `c-in-sws' range gets broken. Note that it isn't
1652 ;; safe to keep a range that was continuous before the change. E.g:
1653 ;;
1654 ;; #define foo
1655 ;; \
1656 ;; bar
1657 ;;
1658 ;; There can be a "ladder" between "#" and "b". Now, if the newline
1659 ;; after "foo" is removed then "bar" will become part of the cpp
1660 ;; directive instead of a syntactically relevant token. In that
1661 ;; case there's no longer syntactic ws from "#" to "b".
1662 (setq beg (1- beg)))
1663
1664 (c-debug-sws-msg "c-invalidate-sws-region-after [%s..%s]" beg end)
1665 (c-remove-is-and-in-sws beg end))
1666
1667 (defun c-forward-sws ()
1668 ;; Used by `c-forward-syntactic-ws' to implement the unbounded search.
1669 ;;
1670 ;; This function might do hidden buffer changes.
1671
1672 (let (;; `rung-pos' is set to a position as early as possible in the
1673 ;; unmarked part of the simple ws region.
1674 (rung-pos (point)) next-rung-pos rung-end-pos last-put-in-sws-pos
1675 rung-is-marked next-rung-is-marked simple-ws-end
1676 ;; `safe-start' is set when it's safe to cache the start position.
1677 ;; It's not set if we've initially skipped over comments and line
1678 ;; continuations since we might have gone out through the end of a
1679 ;; macro then. This provision makes `c-forward-sws' not populate the
1680 ;; cache in the majority of cases, but otoh is `c-backward-sws' by far
1681 ;; more common.
1682 safe-start)
1683
1684 ;; Skip simple ws and do a quick check on the following character to see
1685 ;; if it's anything that can't start syntactic ws, so we can bail out
1686 ;; early in the majority of cases when there just are a few ws chars.
1687 (skip-chars-forward " \t\n\r\f\v")
1688 (when (looking-at c-syntactic-ws-start)
1689
1690 (setq rung-end-pos (min (1+ (point)) (point-max)))
1691 (if (setq rung-is-marked (text-property-any rung-pos rung-end-pos
1692 'c-is-sws t))
1693 ;; Find the last rung position to avoid setting properties in all
1694 ;; the cases when the marked rung is complete.
1695 ;; (`next-single-property-change' is certain to move at least one
1696 ;; step forward.)
1697 (setq rung-pos (1- (next-single-property-change
1698 rung-is-marked 'c-is-sws nil rung-end-pos)))
1699 ;; Got no marked rung here. Since the simple ws might have started
1700 ;; inside a line comment or cpp directive we must set `rung-pos' as
1701 ;; high as possible.
1702 (setq rung-pos (point)))
1703
1704 (with-silent-modifications
1705 (while
1706 (progn
1707 (while
1708 (when (and rung-is-marked
1709 (get-text-property (point) 'c-in-sws))
1710
1711 ;; The following search is the main reason that `c-in-sws'
1712 ;; and `c-is-sws' aren't combined to one property.
1713 (goto-char (next-single-property-change
1714 (point) 'c-in-sws nil (point-max)))
1715 (unless (get-text-property (point) 'c-is-sws)
1716 ;; If the `c-in-sws' region extended past the last
1717 ;; `c-is-sws' char we have to go back a bit.
1718 (or (get-text-property (1- (point)) 'c-is-sws)
1719 (goto-char (previous-single-property-change
1720 (point) 'c-is-sws)))
1721 (backward-char))
1722
1723 (c-debug-sws-msg
1724 "c-forward-sws cached move %s -> %s (max %s)"
1725 rung-pos (point) (point-max))
1726
1727 (setq rung-pos (point))
1728 (and (> (skip-chars-forward " \t\n\r\f\v") 0)
1729 (not (eobp))))
1730
1731 ;; We'll loop here if there is simple ws after the last rung.
1732 ;; That means that there's been some change in it and it's
1733 ;; possible that we've stepped into another ladder, so extend
1734 ;; the previous one to join with it if there is one, and try to
1735 ;; use the cache again.
1736 (c-debug-sws-msg
1737 "c-forward-sws extending rung with [%s..%s] (max %s)"
1738 (1+ rung-pos) (1+ (point)) (point-max))
1739 (unless (get-text-property (point) 'c-is-sws)
1740 ;; Remove any `c-in-sws' property from the last char of
1741 ;; the rung before we mark it with `c-is-sws', so that we
1742 ;; won't connect with the remains of a broken "ladder".
1743 (c-remove-in-sws (point) (1+ (point))))
1744 (c-put-is-sws (1+ rung-pos)
1745 (1+ (point)))
1746 (c-put-in-sws rung-pos
1747 (setq rung-pos (point)
1748 last-put-in-sws-pos rung-pos)))
1749
1750 (setq simple-ws-end (point))
1751 (c-forward-comments)
1752
1753 (cond
1754 ((/= (point) simple-ws-end)
1755 ;; Skipped over comments. Don't cache at eob in case the buffer
1756 ;; is narrowed.
1757 (not (eobp)))
1758
1759 ((save-excursion
1760 (and c-opt-cpp-prefix
1761 (looking-at c-opt-cpp-start)
1762 (progn (skip-chars-backward " \t")
1763 (bolp))
1764 (or (bobp)
1765 (progn (backward-char)
1766 (not (eq (char-before) ?\\))))))
1767 ;; Skip a preprocessor directive.
1768 (end-of-line)
1769 (while (and (eq (char-before) ?\\)
1770 (= (forward-line 1) 0))
1771 (end-of-line))
1772 (forward-line 1)
1773 (setq safe-start t)
1774 ;; Don't cache at eob in case the buffer is narrowed.
1775 (not (eobp)))))
1776
1777 ;; We've searched over a piece of non-white syntactic ws. See if this
1778 ;; can be cached.
1779 (setq next-rung-pos (point))
1780 (skip-chars-forward " \t\n\r\f\v")
1781 (setq rung-end-pos (min (1+ (point)) (point-max)))
1782
1783 (if (or
1784 ;; Cache if we haven't skipped comments only, and if we started
1785 ;; either from a marked rung or from a completely uncached
1786 ;; position.
1787 (and safe-start
1788 (or rung-is-marked
1789 (not (get-text-property simple-ws-end 'c-in-sws))))
1790
1791 ;; See if there's a marked rung in the encountered simple ws. If
1792 ;; so then we can cache, unless `safe-start' is nil. Even then
1793 ;; we need to do this to check if the cache can be used for the
1794 ;; next step.
1795 (and (setq next-rung-is-marked
1796 (text-property-any next-rung-pos rung-end-pos
1797 'c-is-sws t))
1798 safe-start))
1799
1800 (progn
1801 (c-debug-sws-msg
1802 "c-forward-sws caching [%s..%s] - [%s..%s] (max %s)"
1803 rung-pos (1+ simple-ws-end) next-rung-pos rung-end-pos
1804 (point-max))
1805
1806 ;; Remove the properties for any nested ws that might be cached.
1807 ;; Only necessary for `c-is-sws' since `c-in-sws' will be set
1808 ;; anyway.
1809 (c-remove-is-sws (1+ simple-ws-end) next-rung-pos)
1810 (unless (and rung-is-marked (= rung-pos simple-ws-end))
1811 (c-put-is-sws rung-pos
1812 (1+ simple-ws-end))
1813 (setq rung-is-marked t))
1814 (c-put-in-sws rung-pos
1815 (setq rung-pos (point)
1816 last-put-in-sws-pos rung-pos))
1817 (unless (get-text-property (1- rung-end-pos) 'c-is-sws)
1818 ;; Remove any `c-in-sws' property from the last char of
1819 ;; the rung before we mark it with `c-is-sws', so that we
1820 ;; won't connect with the remains of a broken "ladder".
1821 (c-remove-in-sws (1- rung-end-pos) rung-end-pos))
1822 (c-put-is-sws next-rung-pos
1823 rung-end-pos))
1824
1825 (c-debug-sws-msg
1826 "c-forward-sws not caching [%s..%s] - [%s..%s] (max %s)"
1827 rung-pos (1+ simple-ws-end) next-rung-pos rung-end-pos
1828 (point-max))
1829
1830 ;; Set `rung-pos' for the next rung. It's the same thing here as
1831 ;; initially, except that the rung position is set as early as
1832 ;; possible since we can't be in the ending ws of a line comment or
1833 ;; cpp directive now.
1834 (if (setq rung-is-marked next-rung-is-marked)
1835 (setq rung-pos (1- (next-single-property-change
1836 rung-is-marked 'c-is-sws nil rung-end-pos)))
1837 (setq rung-pos next-rung-pos))
1838 (setq safe-start t)))
1839
1840 ;; Make sure that the newly marked `c-in-sws' region doesn't connect to
1841 ;; another one after the point (which might occur when editing inside a
1842 ;; comment or macro).
1843 (when (eq last-put-in-sws-pos (point))
1844 (cond ((< last-put-in-sws-pos (point-max))
1845 (c-debug-sws-msg
1846 "c-forward-sws clearing at %s for cache separation"
1847 last-put-in-sws-pos)
1848 (c-remove-in-sws last-put-in-sws-pos
1849 (1+ last-put-in-sws-pos)))
1850 (t
1851 ;; If at eob we have to clear the last character before the end
1852 ;; instead since the buffer might be narrowed and there might
1853 ;; be a `c-in-sws' after (point-max). In this case it's
1854 ;; necessary to clear both properties.
1855 (c-debug-sws-msg
1856 "c-forward-sws clearing thoroughly at %s for cache separation"
1857 (1- last-put-in-sws-pos))
1858 (c-remove-is-and-in-sws (1- last-put-in-sws-pos)
1859 last-put-in-sws-pos))))
1860 ))))
1861
1862 (defun c-backward-sws ()
1863 ;; Used by `c-backward-syntactic-ws' to implement the unbounded search.
1864 ;;
1865 ;; This function might do hidden buffer changes.
1866
1867 (let (;; `rung-pos' is set to a position as late as possible in the unmarked
1868 ;; part of the simple ws region.
1869 (rung-pos (point)) next-rung-pos last-put-in-sws-pos
1870 rung-is-marked simple-ws-beg cmt-skip-pos)
1871
1872 ;; Skip simple horizontal ws and do a quick check on the preceding
1873 ;; character to see if it's anything that can't end syntactic ws, so we can
1874 ;; bail out early in the majority of cases when there just are a few ws
1875 ;; chars. Newlines are complicated in the backward direction, so we can't
1876 ;; skip over them.
1877 (skip-chars-backward " \t\f")
1878 (when (and (not (bobp))
1879 (save-excursion
1880 (backward-char)
1881 (looking-at c-syntactic-ws-end)))
1882
1883 ;; Try to find a rung position in the simple ws preceding point, so that
1884 ;; we can get a cache hit even if the last bit of the simple ws has
1885 ;; changed recently.
1886 (setq simple-ws-beg (point))
1887 (skip-chars-backward " \t\n\r\f\v")
1888 (if (setq rung-is-marked (text-property-any
1889 (point) (min (1+ rung-pos) (point-max))
1890 'c-is-sws t))
1891 ;; `rung-pos' will be the earliest marked position, which means that
1892 ;; there might be later unmarked parts in the simple ws region.
1893 ;; It's not worth the effort to fix that; the last part of the
1894 ;; simple ws is also typically edited often, so it could be wasted.
1895 (goto-char (setq rung-pos rung-is-marked))
1896 (goto-char simple-ws-beg))
1897
1898 (with-silent-modifications
1899 (while
1900 (progn
1901 (while
1902 (when (and rung-is-marked
1903 (not (bobp))
1904 (get-text-property (1- (point)) 'c-in-sws))
1905
1906 ;; The following search is the main reason that `c-in-sws'
1907 ;; and `c-is-sws' aren't combined to one property.
1908 (goto-char (previous-single-property-change
1909 (point) 'c-in-sws nil (point-min)))
1910 (unless (get-text-property (point) 'c-is-sws)
1911 ;; If the `c-in-sws' region extended past the first
1912 ;; `c-is-sws' char we have to go forward a bit.
1913 (goto-char (next-single-property-change
1914 (point) 'c-is-sws)))
1915
1916 (c-debug-sws-msg
1917 "c-backward-sws cached move %s <- %s (min %s)"
1918 (point) rung-pos (point-min))
1919
1920 (setq rung-pos (point))
1921 (if (and (< (min (skip-chars-backward " \t\f\v")
1922 (progn
1923 (setq simple-ws-beg (point))
1924 (skip-chars-backward " \t\n\r\f\v")))
1925 0)
1926 (setq rung-is-marked
1927 (text-property-any (point) rung-pos
1928 'c-is-sws t)))
1929 t
1930 (goto-char simple-ws-beg)
1931 nil))
1932
1933 ;; We'll loop here if there is simple ws before the first rung.
1934 ;; That means that there's been some change in it and it's
1935 ;; possible that we've stepped into another ladder, so extend
1936 ;; the previous one to join with it if there is one, and try to
1937 ;; use the cache again.
1938 (c-debug-sws-msg
1939 "c-backward-sws extending rung with [%s..%s] (min %s)"
1940 rung-is-marked rung-pos (point-min))
1941 (unless (get-text-property (1- rung-pos) 'c-is-sws)
1942 ;; Remove any `c-in-sws' property from the last char of
1943 ;; the rung before we mark it with `c-is-sws', so that we
1944 ;; won't connect with the remains of a broken "ladder".
1945 (c-remove-in-sws (1- rung-pos) rung-pos))
1946 (c-put-is-sws rung-is-marked
1947 rung-pos)
1948 (c-put-in-sws rung-is-marked
1949 (1- rung-pos))
1950 (setq rung-pos rung-is-marked
1951 last-put-in-sws-pos rung-pos))
1952
1953 (c-backward-comments)
1954 (setq cmt-skip-pos (point))
1955
1956 (cond
1957 ((and c-opt-cpp-prefix
1958 (/= cmt-skip-pos simple-ws-beg)
1959 (c-beginning-of-macro))
1960 ;; Inside a cpp directive. See if it should be skipped over.
1961 (let ((cpp-beg (point)))
1962
1963 ;; Move back over all line continuations in the region skipped
1964 ;; over by `c-backward-comments'. If we go past it then we
1965 ;; started inside the cpp directive.
1966 (goto-char simple-ws-beg)
1967 (beginning-of-line)
1968 (while (and (> (point) cmt-skip-pos)
1969 (progn (backward-char)
1970 (eq (char-before) ?\\)))
1971 (beginning-of-line))
1972
1973 (if (< (point) cmt-skip-pos)
1974 ;; Don't move past the cpp directive if we began inside
1975 ;; it. Note that the position at the end of the last line
1976 ;; of the macro is also considered to be within it.
1977 (progn (goto-char cmt-skip-pos)
1978 nil)
1979
1980 ;; It's worthwhile to spend a little bit of effort on finding
1981 ;; the end of the macro, to get a good `simple-ws-beg'
1982 ;; position for the cache. Note that `c-backward-comments'
1983 ;; could have stepped over some comments before going into
1984 ;; the macro, and then `simple-ws-beg' must be kept on the
1985 ;; same side of those comments.
1986 (goto-char simple-ws-beg)
1987 (skip-chars-backward " \t\n\r\f\v")
1988 (if (eq (char-before) ?\\)
1989 (forward-char))
1990 (forward-line 1)
1991 (if (< (point) simple-ws-beg)
1992 ;; Might happen if comments after the macro were skipped
1993 ;; over.
1994 (setq simple-ws-beg (point)))
1995
1996 (goto-char cpp-beg)
1997 t)))
1998
1999 ((/= (save-excursion
2000 (skip-chars-forward " \t\n\r\f\v" simple-ws-beg)
2001 (setq next-rung-pos (point)))
2002 simple-ws-beg)
2003 ;; Skipped over comments. Must put point at the end of
2004 ;; the simple ws at point since we might be after a line
2005 ;; comment or cpp directive that's been partially
2006 ;; narrowed out, and we can't risk marking the simple ws
2007 ;; at the end of it.
2008 (goto-char next-rung-pos)
2009 t)))
2010
2011 ;; We've searched over a piece of non-white syntactic ws. See if this
2012 ;; can be cached.
2013 (setq next-rung-pos (point))
2014 (skip-chars-backward " \t\f\v")
2015
2016 (if (or
2017 ;; Cache if we started either from a marked rung or from a
2018 ;; completely uncached position.
2019 rung-is-marked
2020 (not (get-text-property (1- simple-ws-beg) 'c-in-sws))
2021
2022 ;; Cache if there's a marked rung in the encountered simple ws.
2023 (save-excursion
2024 (skip-chars-backward " \t\n\r\f\v")
2025 (text-property-any (point) (min (1+ next-rung-pos) (point-max))
2026 'c-is-sws t)))
2027
2028 (progn
2029 (c-debug-sws-msg
2030 "c-backward-sws caching [%s..%s] - [%s..%s] (min %s)"
2031 (point) (1+ next-rung-pos)
2032 simple-ws-beg (min (1+ rung-pos) (point-max))
2033 (point-min))
2034
2035 ;; Remove the properties for any nested ws that might be cached.
2036 ;; Only necessary for `c-is-sws' since `c-in-sws' will be set
2037 ;; anyway.
2038 (c-remove-is-sws (1+ next-rung-pos) simple-ws-beg)
2039 (unless (and rung-is-marked (= simple-ws-beg rung-pos))
2040 (let ((rung-end-pos (min (1+ rung-pos) (point-max))))
2041 (unless (get-text-property (1- rung-end-pos) 'c-is-sws)
2042 ;; Remove any `c-in-sws' property from the last char of
2043 ;; the rung before we mark it with `c-is-sws', so that we
2044 ;; won't connect with the remains of a broken "ladder".
2045 (c-remove-in-sws (1- rung-end-pos) rung-end-pos))
2046 (c-put-is-sws simple-ws-beg
2047 rung-end-pos)
2048 (setq rung-is-marked t)))
2049 (c-put-in-sws (setq simple-ws-beg (point)
2050 last-put-in-sws-pos simple-ws-beg)
2051 rung-pos)
2052 (c-put-is-sws (setq rung-pos simple-ws-beg)
2053 (1+ next-rung-pos)))
2054
2055 (c-debug-sws-msg
2056 "c-backward-sws not caching [%s..%s] - [%s..%s] (min %s)"
2057 (point) (1+ next-rung-pos)
2058 simple-ws-beg (min (1+ rung-pos) (point-max))
2059 (point-min))
2060 (setq rung-pos next-rung-pos
2061 simple-ws-beg (point))
2062 ))
2063
2064 ;; Make sure that the newly marked `c-in-sws' region doesn't connect to
2065 ;; another one before the point (which might occur when editing inside a
2066 ;; comment or macro).
2067 (when (eq last-put-in-sws-pos (point))
2068 (cond ((< (point-min) last-put-in-sws-pos)
2069 (c-debug-sws-msg
2070 "c-backward-sws clearing at %s for cache separation"
2071 (1- last-put-in-sws-pos))
2072 (c-remove-in-sws (1- last-put-in-sws-pos)
2073 last-put-in-sws-pos))
2074 ((> (point-min) 1)
2075 ;; If at bob and the buffer is narrowed, we have to clear the
2076 ;; character we're standing on instead since there might be a
2077 ;; `c-in-sws' before (point-min). In this case it's necessary
2078 ;; to clear both properties.
2079 (c-debug-sws-msg
2080 "c-backward-sws clearing thoroughly at %s for cache separation"
2081 last-put-in-sws-pos)
2082 (c-remove-is-and-in-sws last-put-in-sws-pos
2083 (1+ last-put-in-sws-pos)))))
2084 ))))
2085
2086 \f
2087 ;; Other whitespace tools
2088 (defun c-partial-ws-p (beg end)
2089 ;; Is the region (beg end) WS, and is there WS (or BOB/EOB) next to the
2090 ;; region? This is a "heuristic" function. .....
2091 ;;
2092 ;; The motivation for the second bit is to check whether removing this
2093 ;; region would coalesce two symbols.
2094 ;;
2095 ;; FIXME!!! This function doesn't check virtual semicolons in any way. Be
2096 ;; careful about using this function for, e.g. AWK. (2007/3/7)
2097 (save-excursion
2098 (let ((end+1 (min (1+ end) (point-max))))
2099 (or (progn (goto-char (max (point-min) (1- beg)))
2100 (c-skip-ws-forward end)
2101 (eq (point) end))
2102 (progn (goto-char beg)
2103 (c-skip-ws-forward end+1)
2104 (eq (point) end+1))))))
2105 \f
2106 ;; A system for finding noteworthy parens before the point.
2107
2108 (defconst c-state-cache-too-far 5000)
2109 ;; A maximum comfortable scanning distance, e.g. between
2110 ;; `c-state-cache-good-pos' and "HERE" (where we call c-parse-state). When
2111 ;; this distance is exceeded, we take "emergency measures", e.g. by clearing
2112 ;; the cache and starting again from point-min or a beginning of defun. This
2113 ;; value can be tuned for efficiency or set to a lower value for testing.
2114
2115 (defvar c-state-cache nil)
2116 (make-variable-buffer-local 'c-state-cache)
2117 ;; The state cache used by `c-parse-state' to cut down the amount of
2118 ;; searching. It's the result from some earlier `c-parse-state' call. See
2119 ;; `c-parse-state''s doc string for details of its structure.
2120 ;;
2121 ;; The use of the cached info is more effective if the next
2122 ;; `c-parse-state' call is on a line close by the one the cached state
2123 ;; was made at; the cache can actually slow down a little if the
2124 ;; cached state was made very far back in the buffer. The cache is
2125 ;; most effective if `c-parse-state' is used on each line while moving
2126 ;; forward.
2127
2128 (defvar c-state-cache-good-pos 1)
2129 (make-variable-buffer-local 'c-state-cache-good-pos)
2130 ;; This is a position where `c-state-cache' is known to be correct, or
2131 ;; nil (see below). It's a position inside one of the recorded unclosed
2132 ;; parens or the top level, but not further nested inside any literal or
2133 ;; subparen that is closed before the last recorded position.
2134 ;;
2135 ;; The exact position is chosen to try to be close to yet earlier than
2136 ;; the position where `c-state-cache' will be called next. Right now
2137 ;; the heuristic is to set it to the position after the last found
2138 ;; closing paren (of any type) before the line on which
2139 ;; `c-parse-state' was called. That is chosen primarily to work well
2140 ;; with refontification of the current line.
2141 ;;
2142 ;; 2009-07-28: When `c-state-point-min' and the last position where
2143 ;; `c-parse-state' or for which `c-invalidate-state-cache' was called, are
2144 ;; both in the same literal, there is no such "good position", and
2145 ;; c-state-cache-good-pos is then nil. This is the ONLY circumstance in which
2146 ;; it can be nil. In this case, `c-state-point-min-literal' will be non-nil.
2147 ;;
2148 ;; 2009-06-12: In a brace desert, c-state-cache-good-pos may also be in
2149 ;; the middle of the desert, as long as it is not within a brace pair
2150 ;; recorded in `c-state-cache' or a paren/bracket pair.
2151
2152
2153 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2154 ;; We maintain a simple cache of positions which aren't in a literal, so as to
2155 ;; speed up testing for non-literality.
2156 (defconst c-state-nonlit-pos-interval 3000)
2157 ;; The approximate interval between entries in `c-state-nonlit-pos-cache'.
2158
2159 (defvar c-state-nonlit-pos-cache nil)
2160 (make-variable-buffer-local 'c-state-nonlit-pos-cache)
2161 ;; A list of buffer positions which are known not to be in a literal or a cpp
2162 ;; construct. This is ordered with higher positions at the front of the list.
2163 ;; Only those which are less than `c-state-nonlit-pos-cache-limit' are valid.
2164
2165 (defvar c-state-nonlit-pos-cache-limit 1)
2166 (make-variable-buffer-local 'c-state-nonlit-pos-cache-limit)
2167 ;; An upper limit on valid entries in `c-state-nonlit-pos-cache'. This is
2168 ;; reduced by buffer changes, and increased by invocations of
2169 ;; `c-state-literal-at'.
2170
2171 (defvar c-state-semi-nonlit-pos-cache nil)
2172 (make-variable-buffer-local 'c-state-semi-nonlit-pos-cache)
2173 ;; A list of buffer positions which are known not to be in a literal. This is
2174 ;; ordered with higher positions at the front of the list. Only those which
2175 ;; are less than `c-state-semi-nonlit-pos-cache-limit' are valid.
2176
2177 (defvar c-state-semi-nonlit-pos-cache-limit 1)
2178 (make-variable-buffer-local 'c-state-semi-nonlit-pos-cache-limit)
2179 ;; An upper limit on valid entries in `c-state-semi-nonlit-pos-cache'. This is
2180 ;; reduced by buffer changes, and increased by invocations of
2181 ;; `c-state-literal-at'. FIXME!!!
2182
2183 (defsubst c-state-pp-to-literal (from to)
2184 ;; Do a parse-partial-sexp from FROM to TO, returning either
2185 ;; (STATE TYPE (BEG . END)) if TO is in a literal; or
2186 ;; (STATE) otherwise,
2187 ;; where STATE is the parsing state at TO, TYPE is the type of the literal
2188 ;; (one of 'c, 'c++, 'string) and (BEG . END) is the boundaries of the literal.
2189 ;;
2190 ;; Only elements 3 (in a string), 4 (in a comment), 5 (following a quote),
2191 ;; 7 (comment type) and 8 (start of comment/string) (and possibly 9) of
2192 ;; STATE are valid.
2193 (save-excursion
2194 (let ((s (parse-partial-sexp from to))
2195 ty)
2196 (when (or (nth 3 s) (nth 4 s)) ; in a string or comment
2197 (setq ty (cond
2198 ((nth 3 s) 'string)
2199 ((eq (nth 7 s) t) 'c++)
2200 (t 'c)))
2201 (parse-partial-sexp (point) (point-max)
2202 nil ; TARGETDEPTH
2203 nil ; STOPBEFORE
2204 s ; OLDSTATE
2205 'syntax-table)) ; stop at end of literal
2206 (if ty
2207 `(,s ,ty (,(nth 8 s) . ,(point)))
2208 `(,s)))))
2209
2210 (defun c-state-safe-place (here)
2211 ;; Return a buffer position before HERE which is "safe", i.e. outside any
2212 ;; string, comment, or macro.
2213 ;;
2214 ;; NOTE: This function manipulates `c-state-nonlit-pos-cache'. This cache
2215 ;; MAY NOT contain any positions within macros, since macros are frequently
2216 ;; turned into comments by use of the `c-cpp-delimiter' category properties.
2217 ;; We cannot rely on this mechanism whilst determining a cache pos since
2218 ;; this function is also called from outwith `c-parse-state'.
2219 (save-restriction
2220 (widen)
2221 (save-excursion
2222 (let ((c c-state-nonlit-pos-cache)
2223 pos npos high-pos lit macro-beg macro-end)
2224 ;; Trim the cache to take account of buffer changes.
2225 (while (and c (> (car c) c-state-nonlit-pos-cache-limit))
2226 (setq c (cdr c)))
2227 (setq c-state-nonlit-pos-cache c)
2228
2229 (while (and c (> (car c) here))
2230 (setq high-pos (car c))
2231 (setq c (cdr c)))
2232 (setq pos (or (car c) (point-min)))
2233
2234 (unless high-pos
2235 (while
2236 ;; Add an element to `c-state-nonlit-pos-cache' each iteration.
2237 (and
2238 (<= (setq npos (+ pos c-state-nonlit-pos-interval)) here)
2239
2240 ;; Test for being in a literal. If so, go to after it.
2241 (progn
2242 (setq lit (car (cddr (c-state-pp-to-literal pos npos))))
2243 (or (null lit)
2244 (prog1 (<= (cdr lit) here)
2245 (setq npos (cdr lit)))))
2246
2247 ;; Test for being in a macro. If so, go to after it.
2248 (progn
2249 (goto-char npos)
2250 (setq macro-beg
2251 (and (c-beginning-of-macro) (/= (point) npos) (point)))
2252 (when macro-beg
2253 (c-syntactic-end-of-macro)
2254 (or (eobp) (forward-char))
2255 (setq macro-end (point)))
2256 (or (null macro-beg)
2257 (prog1 (<= macro-end here)
2258 (setq npos macro-end)))))
2259
2260 (setq pos npos)
2261 (setq c-state-nonlit-pos-cache (cons pos c-state-nonlit-pos-cache)))
2262 ;; Add one extra element above HERE so as to to avoid the previous
2263 ;; expensive calculation when the next call is close to the current
2264 ;; one. This is especially useful when inside a large macro.
2265 (setq c-state-nonlit-pos-cache (cons npos c-state-nonlit-pos-cache)))
2266
2267 (if (> pos c-state-nonlit-pos-cache-limit)
2268 (setq c-state-nonlit-pos-cache-limit pos))
2269 pos))))
2270
2271 (defun c-state-semi-safe-place (here)
2272 ;; Return a buffer position before HERE which is "safe", i.e. outside any
2273 ;; string or comment. It may be in a macro.
2274 (save-restriction
2275 (widen)
2276 (save-excursion
2277 (let ((c c-state-semi-nonlit-pos-cache)
2278 pos npos high-pos lit macro-beg macro-end)
2279 ;; Trim the cache to take account of buffer changes.
2280 (while (and c (> (car c) c-state-semi-nonlit-pos-cache-limit))
2281 (setq c (cdr c)))
2282 (setq c-state-semi-nonlit-pos-cache c)
2283
2284 (while (and c (> (car c) here))
2285 (setq high-pos (car c))
2286 (setq c (cdr c)))
2287 (setq pos (or (car c) (point-min)))
2288
2289 (unless high-pos
2290 (while
2291 ;; Add an element to `c-state-semi-nonlit-pos-cache' each iteration.
2292 (and
2293 (<= (setq npos (+ pos c-state-nonlit-pos-interval)) here)
2294
2295 ;; Test for being in a literal. If so, go to after it.
2296 (progn
2297 (setq lit (car (cddr (c-state-pp-to-literal pos npos))))
2298 (or (null lit)
2299 (prog1 (<= (cdr lit) here)
2300 (setq npos (cdr lit))))))
2301
2302 (setq pos npos)
2303 (setq c-state-semi-nonlit-pos-cache
2304 (cons pos c-state-semi-nonlit-pos-cache))))
2305
2306 (if (> pos c-state-semi-nonlit-pos-cache-limit)
2307 (setq c-state-semi-nonlit-pos-cache-limit pos))
2308 pos))))
2309
2310 (defun c-state-literal-at (here)
2311 ;; If position HERE is inside a literal, return (START . END), the
2312 ;; boundaries of the literal (which may be outside the accessible bit of the
2313 ;; buffer). Otherwise, return nil.
2314 ;;
2315 ;; This function is almost the same as `c-literal-limits'. Previously, it
2316 ;; differed in that it was a lower level function, and that it rigorously
2317 ;; followed the syntax from BOB. `c-literal-limits' is now (2011-12)
2318 ;; virtually identical to this function.
2319 (save-restriction
2320 (widen)
2321 (save-excursion
2322 (let ((pos (c-state-safe-place here)))
2323 (car (cddr (c-state-pp-to-literal pos here)))))))
2324
2325 (defsubst c-state-lit-beg (pos)
2326 ;; Return the start of the literal containing POS, or POS itself.
2327 (or (car (c-state-literal-at pos))
2328 pos))
2329
2330 (defsubst c-state-cache-non-literal-place (pos state)
2331 ;; Return a position outside of a string/comment/macro at or before POS.
2332 ;; STATE is the parse-partial-sexp state at POS.
2333 (let ((res (if (or (nth 3 state) ; in a string?
2334 (nth 4 state)) ; in a comment?
2335 (nth 8 state)
2336 pos)))
2337 (save-excursion
2338 (goto-char res)
2339 (if (c-beginning-of-macro)
2340 (point)
2341 res))))
2342
2343 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2344 ;; Stuff to do with point-min, and coping with any literal there.
2345 (defvar c-state-point-min 1)
2346 (make-variable-buffer-local 'c-state-point-min)
2347 ;; This is (point-min) when `c-state-cache' was last calculated. A change of
2348 ;; narrowing is likely to affect the parens that are visible before the point.
2349
2350 (defvar c-state-point-min-lit-type nil)
2351 (make-variable-buffer-local 'c-state-point-min-lit-type)
2352 (defvar c-state-point-min-lit-start nil)
2353 (make-variable-buffer-local 'c-state-point-min-lit-start)
2354 ;; These two variables define the literal, if any, containing point-min.
2355 ;; Their values are, respectively, 'string, c, or c++, and the start of the
2356 ;; literal. If there's no literal there, they're both nil.
2357
2358 (defvar c-state-min-scan-pos 1)
2359 (make-variable-buffer-local 'c-state-min-scan-pos)
2360 ;; This is the earliest buffer-pos from which scanning can be done. It is
2361 ;; either the end of the literal containing point-min, or point-min itself.
2362 ;; It becomes nil if the buffer is changed earlier than this point.
2363 (defun c-state-get-min-scan-pos ()
2364 ;; Return the lowest valid scanning pos. This will be the end of the
2365 ;; literal enclosing point-min, or point-min itself.
2366 (or c-state-min-scan-pos
2367 (save-restriction
2368 (save-excursion
2369 (widen)
2370 (goto-char c-state-point-min-lit-start)
2371 (if (eq c-state-point-min-lit-type 'string)
2372 (forward-sexp)
2373 (forward-comment 1))
2374 (setq c-state-min-scan-pos (point))))))
2375
2376 (defun c-state-mark-point-min-literal ()
2377 ;; Determine the properties of any literal containing POINT-MIN, setting the
2378 ;; variables `c-state-point-min-lit-type', `c-state-point-min-lit-start',
2379 ;; and `c-state-min-scan-pos' accordingly. The return value is meaningless.
2380 (let ((p-min (point-min))
2381 lit)
2382 (save-restriction
2383 (widen)
2384 (setq lit (c-state-literal-at p-min))
2385 (if lit
2386 (setq c-state-point-min-lit-type
2387 (save-excursion
2388 (goto-char (car lit))
2389 (cond
2390 ((looking-at c-block-comment-start-regexp) 'c)
2391 ((looking-at c-line-comment-starter) 'c++)
2392 (t 'string)))
2393 c-state-point-min-lit-start (car lit)
2394 c-state-min-scan-pos (cdr lit))
2395 (setq c-state-point-min-lit-type nil
2396 c-state-point-min-lit-start nil
2397 c-state-min-scan-pos p-min)))))
2398
2399
2400 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2401 ;; A variable which signals a brace dessert - helpful for reducing the number
2402 ;; of fruitless backward scans.
2403 (defvar c-state-brace-pair-desert nil)
2404 (make-variable-buffer-local 'c-state-brace-pair-desert)
2405 ;; Used only in `c-append-lower-brace-pair-to-state-cache'. It is set when
2406 ;; that defun has searched backwards for a brace pair and not found one. Its
2407 ;; value is either nil or a cons (PA . FROM), where PA is the position of the
2408 ;; enclosing opening paren/brace/bracket which bounds the backwards search (or
2409 ;; nil when at top level) and FROM is where the backward search started. It
2410 ;; is reset to nil in `c-invalidate-state-cache'.
2411
2412
2413 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2414 ;; Lowish level functions/macros which work directly on `c-state-cache', or a
2415 ;; list of like structure.
2416 (defmacro c-state-cache-top-lparen (&optional cache)
2417 ;; Return the address of the top left brace/bracket/paren recorded in CACHE
2418 ;; (default `c-state-cache') (or nil).
2419 (let ((cash (or cache 'c-state-cache)))
2420 `(if (consp (car ,cash))
2421 (caar ,cash)
2422 (car ,cash))))
2423
2424 (defmacro c-state-cache-top-paren (&optional cache)
2425 ;; Return the address of the latest brace/bracket/paren (whether left or
2426 ;; right) recorded in CACHE (default `c-state-cache') or nil.
2427 (let ((cash (or cache 'c-state-cache)))
2428 `(if (consp (car ,cash))
2429 (cdar ,cash)
2430 (car ,cash))))
2431
2432 (defmacro c-state-cache-after-top-paren (&optional cache)
2433 ;; Return the position just after the latest brace/bracket/paren (whether
2434 ;; left or right) recorded in CACHE (default `c-state-cache') or nil.
2435 (let ((cash (or cache 'c-state-cache)))
2436 `(if (consp (car ,cash))
2437 (cdar ,cash)
2438 (and (car ,cash)
2439 (1+ (car ,cash))))))
2440
2441 (defun c-get-cache-scan-pos (here)
2442 ;; From the state-cache, determine the buffer position from which we might
2443 ;; scan forward to HERE to update this cache. This position will be just
2444 ;; after a paren/brace/bracket recorded in the cache, if possible, otherwise
2445 ;; return the earliest position in the accessible region which isn't within
2446 ;; a literal. If the visible portion of the buffer is entirely within a
2447 ;; literal, return NIL.
2448 (let ((c c-state-cache) elt)
2449 ;(while (>= (or (c-state-cache-top-lparen c) 1) here)
2450 (while (and c
2451 (>= (c-state-cache-top-lparen c) here))
2452 (setq c (cdr c)))
2453
2454 (setq elt (car c))
2455 (cond
2456 ((consp elt)
2457 (if (> (cdr elt) here)
2458 (1+ (car elt))
2459 (cdr elt)))
2460 (elt (1+ elt))
2461 ((<= (c-state-get-min-scan-pos) here)
2462 (c-state-get-min-scan-pos))
2463 (t nil))))
2464
2465 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2466 ;; Variables which keep track of preprocessor constructs.
2467 (defvar c-state-old-cpp-beg-marker nil)
2468 (make-variable-buffer-local 'c-state-old-cpp-beg-marker)
2469 (defvar c-state-old-cpp-beg nil)
2470 (make-variable-buffer-local 'c-state-old-cpp-beg)
2471 (defvar c-state-old-cpp-end-marker nil)
2472 (make-variable-buffer-local 'c-state-old-cpp-end-marker)
2473 (defvar c-state-old-cpp-end nil)
2474 (make-variable-buffer-local 'c-state-old-cpp-end)
2475 ;; These are the limits of the macro containing point at the previous call of
2476 ;; `c-parse-state', or nil.
2477
2478 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2479 ;; Defuns which analyze the buffer, yet don't change `c-state-cache'.
2480 (defun c-get-fallback-scan-pos (here)
2481 ;; Return a start position for building `c-state-cache' from
2482 ;; scratch. This will be at the top level, 2 defuns back.
2483 (save-excursion
2484 ;; Go back 2 bods, but ignore any bogus positions returned by
2485 ;; beginning-of-defun (i.e. open paren in column zero).
2486 (goto-char here)
2487 (let ((cnt 2))
2488 (while (not (or (bobp) (zerop cnt)))
2489 (c-beginning-of-defun-1) ; Pure elisp BOD.
2490 (if (eq (char-after) ?\{)
2491 (setq cnt (1- cnt)))))
2492 (point)))
2493
2494 (defun c-state-balance-parens-backwards (here- here+ top)
2495 ;; Return the position of the opening paren/brace/bracket before HERE- which
2496 ;; matches the outermost close p/b/b between HERE+ and TOP. Except when
2497 ;; there's a macro, HERE- and HERE+ are the same. Like this:
2498 ;;
2499 ;; ............................................
2500 ;; | |
2501 ;; ( [ ( .........#macro.. ) ( ) ] )
2502 ;; ^ ^ ^ ^
2503 ;; | | | |
2504 ;; return HERE- HERE+ TOP
2505 ;;
2506 ;; If there aren't enough opening paren/brace/brackets, return the position
2507 ;; of the outermost one found, or HERE- if there are none. If there are no
2508 ;; closing p/b/bs between HERE+ and TOP, return HERE-. HERE-/+ and TOP
2509 ;; must not be inside literals. Only the accessible portion of the buffer
2510 ;; will be scanned.
2511
2512 ;; PART 1: scan from `here+' up to `top', accumulating ")"s which enclose
2513 ;; `here'. Go round the next loop each time we pass over such a ")". These
2514 ;; probably match "("s before `here-'.
2515 (let (pos pa ren+1 lonely-rens)
2516 (save-excursion
2517 (save-restriction
2518 (narrow-to-region (point-min) top) ; This can move point, sometimes.
2519 (setq pos here+)
2520 (c-safe
2521 (while
2522 (setq ren+1 (scan-lists pos 1 1)) ; might signal
2523 (setq lonely-rens (cons ren+1 lonely-rens)
2524 pos ren+1)))))
2525
2526 ;; PART 2: Scan back before `here-' searching for the "("s
2527 ;; matching/mismatching the ")"s found above. We only need to direct the
2528 ;; caller to scan when we've encountered unmatched right parens.
2529 (setq pos here-)
2530 (when lonely-rens
2531 (c-safe
2532 (while
2533 (and lonely-rens ; actual values aren't used.
2534 (setq pa (scan-lists pos -1 1)))
2535 (setq pos pa)
2536 (setq lonely-rens (cdr lonely-rens)))))
2537 pos))
2538
2539 (defun c-parse-state-get-strategy (here good-pos)
2540 ;; Determine the scanning strategy for adjusting `c-parse-state', attempting
2541 ;; to minimize the amount of scanning. HERE is the pertinent position in
2542 ;; the buffer, GOOD-POS is a position where `c-state-cache' (possibly with
2543 ;; its head trimmed) is known to be good, or nil if there is no such
2544 ;; position.
2545 ;;
2546 ;; The return value is a list, one of the following:
2547 ;;
2548 ;; o - ('forward CACHE-POS START-POINT) - scan forward from START-POINT,
2549 ;; which is not less than CACHE-POS.
2550 ;; o - ('backward CACHE-POS nil) - scan backwards (from HERE).
2551 ;; o - ('BOD nil START-POINT) - scan forwards from START-POINT, which is at the
2552 ;; top level.
2553 ;; o - ('IN-LIT nil nil) - point is inside the literal containing point-min.
2554 ;; , where CACHE-POS is the highest position recorded in `c-state-cache' at
2555 ;; or below HERE.
2556 (let ((cache-pos (c-get-cache-scan-pos here)) ; highest position below HERE in cache (or 1)
2557 BOD-pos ; position of 2nd BOD before HERE.
2558 strategy ; 'forward, 'backward, 'BOD, or 'IN-LIT.
2559 start-point
2560 how-far) ; putative scanning distance.
2561 (setq good-pos (or good-pos (c-state-get-min-scan-pos)))
2562 (cond
2563 ((< here (c-state-get-min-scan-pos))
2564 (setq strategy 'IN-LIT
2565 start-point nil
2566 cache-pos nil
2567 how-far 0))
2568 ((<= good-pos here)
2569 (setq strategy 'forward
2570 start-point (max good-pos cache-pos)
2571 how-far (- here start-point)))
2572 ((< (- good-pos here) (- here cache-pos)) ; FIXME!!! ; apply some sort of weighting.
2573 (setq strategy 'backward
2574 how-far (- good-pos here)))
2575 (t
2576 (setq strategy 'forward
2577 how-far (- here cache-pos)
2578 start-point cache-pos)))
2579
2580 ;; Might we be better off starting from the top level, two defuns back,
2581 ;; instead? This heuristic no longer works well in C++, where
2582 ;; declarations inside namespace brace blocks are frequently placed at
2583 ;; column zero.
2584 (when (and (not (c-major-mode-is 'c++-mode))
2585 (> how-far c-state-cache-too-far))
2586 (setq BOD-pos (c-get-fallback-scan-pos here)) ; somewhat EXPENSIVE!!!
2587 (if (< (- here BOD-pos) how-far)
2588 (setq strategy 'BOD
2589 start-point BOD-pos)))
2590
2591 (list
2592 strategy
2593 (and (memq strategy '(forward backward)) cache-pos)
2594 (and (memq strategy '(forward BOD)) start-point))))
2595
2596
2597 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2598 ;; Routines which change `c-state-cache' and associated values.
2599 (defun c-renarrow-state-cache ()
2600 ;; The region (more precisely, point-min) has changed since we
2601 ;; calculated `c-state-cache'. Amend `c-state-cache' accordingly.
2602 (if (< (point-min) c-state-point-min)
2603 ;; If point-min has MOVED BACKWARDS then we drop the state completely.
2604 ;; It would be possible to do a better job here and recalculate the top
2605 ;; only.
2606 (progn
2607 (c-state-mark-point-min-literal)
2608 (setq c-state-cache nil
2609 c-state-cache-good-pos c-state-min-scan-pos
2610 c-state-brace-pair-desert nil))
2611
2612 ;; point-min has MOVED FORWARD.
2613
2614 ;; Is the new point-min inside a (different) literal?
2615 (unless (and c-state-point-min-lit-start ; at prev. point-min
2616 (< (point-min) (c-state-get-min-scan-pos)))
2617 (c-state-mark-point-min-literal))
2618
2619 ;; Cut off a bit of the tail from `c-state-cache'.
2620 (let ((ptr (cons nil c-state-cache))
2621 pa)
2622 (while (and (setq pa (c-state-cache-top-lparen (cdr ptr)))
2623 (>= pa (point-min)))
2624 (setq ptr (cdr ptr)))
2625
2626 (when (consp ptr)
2627 (if (eq (cdr ptr) c-state-cache)
2628 (setq c-state-cache nil
2629 c-state-cache-good-pos c-state-min-scan-pos)
2630 (setcdr ptr nil)
2631 (setq c-state-cache-good-pos (1+ (c-state-cache-top-lparen))))
2632 )))
2633
2634 (setq c-state-point-min (point-min)))
2635
2636 (defun c-append-lower-brace-pair-to-state-cache (from &optional upper-lim)
2637 ;; If there is a brace pair preceding FROM in the buffer, at the same level
2638 ;; of nesting (not necessarily immediately preceding), push a cons onto
2639 ;; `c-state-cache' to represent it. FROM must not be inside a literal. If
2640 ;; UPPER-LIM is non-nil, we append the highest brace pair whose "}" is below
2641 ;; UPPER-LIM.
2642 ;;
2643 ;; Return non-nil when this has been done.
2644 ;;
2645 ;; The situation it copes with is this transformation:
2646 ;;
2647 ;; OLD: { (.) {...........}
2648 ;; ^ ^
2649 ;; FROM HERE
2650 ;;
2651 ;; NEW: { {....} (.) {.........
2652 ;; ^ ^ ^
2653 ;; LOWER BRACE PAIR HERE or HERE
2654 ;;
2655 ;; This routine should be fast. Since it can get called a LOT, we maintain
2656 ;; `c-state-brace-pair-desert', a small cache of "failures", such that we
2657 ;; reduce the time wasted in repeated fruitless searches in brace deserts.
2658 (save-excursion
2659 (save-restriction
2660 (let* (new-cons
2661 (cache-pos (c-state-cache-top-lparen)) ; might be nil.
2662 (macro-start-or-from
2663 (progn (goto-char from)
2664 (c-beginning-of-macro)
2665 (point)))
2666 (bra ; Position of "{".
2667 ;; Don't start scanning in the middle of a CPP construct unless
2668 ;; it contains HERE - these constructs, in Emacs, are "commented
2669 ;; out" with category properties.
2670 (if (eq (c-get-char-property macro-start-or-from 'category)
2671 'c-cpp-delimiter)
2672 macro-start-or-from
2673 from))
2674 ce) ; Position of "}"
2675 (or upper-lim (setq upper-lim from))
2676
2677 ;; If we're essentially repeating a fruitless search, just give up.
2678 (unless (and c-state-brace-pair-desert
2679 (eq cache-pos (car c-state-brace-pair-desert))
2680 (or (null (car c-state-brace-pair-desert))
2681 (> from (car c-state-brace-pair-desert)))
2682 (<= from (cdr c-state-brace-pair-desert)))
2683 ;; DESERT-LIM. Avoid repeated searching through the cached desert.
2684 (let ((desert-lim
2685 (and c-state-brace-pair-desert
2686 (eq cache-pos (car c-state-brace-pair-desert))
2687 (>= from (cdr c-state-brace-pair-desert))
2688 (cdr c-state-brace-pair-desert)))
2689 ;; CACHE-LIM. This limit will be necessary when an opening
2690 ;; paren at `cache-pos' has just had its matching close paren
2691 ;; inserted into the buffer. `cache-pos' continues to be a
2692 ;; search bound, even though the algorithm below would skip
2693 ;; over the new paren pair.
2694 (cache-lim (and cache-pos (< cache-pos from) cache-pos)))
2695 (narrow-to-region
2696 (cond
2697 ((and desert-lim cache-lim)
2698 (max desert-lim cache-lim))
2699 (desert-lim)
2700 (cache-lim)
2701 ((point-min)))
2702 (point-max)))
2703
2704 ;; In the next pair of nested loops, the inner one moves back past a
2705 ;; pair of (mis-)matching parens or brackets; the outer one moves
2706 ;; back over a sequence of unmatched close brace/paren/bracket each
2707 ;; time round.
2708 (while
2709 (progn
2710 (c-safe
2711 (while
2712 (and (setq ce (scan-lists bra -1 -1)) ; back past )/]/}; might signal
2713 (setq bra (scan-lists ce -1 1)) ; back past (/[/{; might signal
2714 (or (> ce upper-lim)
2715 (not (eq (char-after bra) ?\{))
2716 (and (goto-char bra)
2717 (c-beginning-of-macro)
2718 (< (point) macro-start-or-from))))))
2719 (and ce (< ce bra)))
2720 (setq bra ce)) ; If we just backed over an unbalanced closing
2721 ; brace, ignore it.
2722
2723 (if (and ce (< bra ce) (eq (char-after bra) ?\{))
2724 ;; We've found the desired brace-pair.
2725 (progn
2726 (setq new-cons (cons bra (1+ ce)))
2727 (cond
2728 ((consp (car c-state-cache))
2729 (setcar c-state-cache new-cons))
2730 ((and (numberp (car c-state-cache)) ; probably never happens
2731 (< ce (car c-state-cache)))
2732 (setcdr c-state-cache
2733 (cons new-cons (cdr c-state-cache))))
2734 (t (setq c-state-cache (cons new-cons c-state-cache)))))
2735
2736 ;; We haven't found a brace pair. Record this in the cache.
2737 (setq c-state-brace-pair-desert (cons cache-pos from))))))))
2738
2739 (defsubst c-state-push-any-brace-pair (bra+1 macro-start-or-here)
2740 ;; If BRA+1 is nil, do nothing. Otherwise, BRA+1 is the buffer position
2741 ;; following a {, and that brace has a (mis-)matching } (or ]), and we
2742 ;; "push" "a" brace pair onto `c-state-cache'.
2743 ;;
2744 ;; Here "push" means overwrite the top element if it's itself a brace-pair,
2745 ;; otherwise push it normally.
2746 ;;
2747 ;; The brace pair we push is normally the one surrounding BRA+1, but if the
2748 ;; latter is inside a macro, not being a macro containing
2749 ;; MACRO-START-OR-HERE, we scan backwards through the buffer for a non-macro
2750 ;; base pair. This latter case is assumed to be rare.
2751 ;;
2752 ;; Note: POINT is not preserved in this routine.
2753 (if bra+1
2754 (if (or (> bra+1 macro-start-or-here)
2755 (progn (goto-char bra+1)
2756 (not (c-beginning-of-macro))))
2757 (setq c-state-cache
2758 (cons (cons (1- bra+1)
2759 (scan-lists bra+1 1 1))
2760 (if (consp (car c-state-cache))
2761 (cdr c-state-cache)
2762 c-state-cache)))
2763 ;; N.B. This defsubst codes one method for the simple, normal case,
2764 ;; and a more sophisticated, slower way for the general case. Don't
2765 ;; eliminate this defsubst - it's a speed optimization.
2766 (c-append-lower-brace-pair-to-state-cache (1- bra+1)))))
2767
2768 (defun c-append-to-state-cache (from)
2769 ;; Scan the buffer from FROM to (point-max), adding elements into
2770 ;; `c-state-cache' for braces etc. Return a candidate for
2771 ;; `c-state-cache-good-pos'.
2772 ;;
2773 ;; FROM must be after the latest brace/paren/bracket in `c-state-cache', if
2774 ;; any. Typically, it is immediately after it. It must not be inside a
2775 ;; literal.
2776 (let ((here-bol (c-point 'bol (point-max)))
2777 (macro-start-or-here
2778 (save-excursion (goto-char (point-max))
2779 (if (c-beginning-of-macro)
2780 (point)
2781 (point-max))))
2782 pa+1 ; pos just after an opening PAren (or brace).
2783 (ren+1 from) ; usually a pos just after an closing paREN etc.
2784 ; Is actually the pos. to scan for a (/{/[ from,
2785 ; which sometimes is after a silly )/}/].
2786 paren+1 ; Pos after some opening or closing paren.
2787 paren+1s ; A list of `paren+1's; used to determine a
2788 ; good-pos.
2789 bra+1 ce+1 ; just after L/R bra-ces.
2790 bra+1s ; list of OLD values of bra+1.
2791 mstart) ; start of a macro.
2792
2793 (save-excursion
2794 ;; Each time round the following loop, we enter a successively deeper
2795 ;; level of brace/paren nesting. (Except sometimes we "continue at
2796 ;; the existing level".) `pa+1' is a pos inside an opening
2797 ;; brace/paren/bracket, usually just after it.
2798 (while
2799 (progn
2800 ;; Each time round the next loop moves forward over an opening then
2801 ;; a closing brace/bracket/paren. This loop is white hot, so it
2802 ;; plays ugly tricks to go fast. DON'T PUT ANYTHING INTO THIS
2803 ;; LOOP WHICH ISN'T ABSOLUTELY NECESSARY!!! It terminates when a
2804 ;; call of `scan-lists' signals an error, which happens when there
2805 ;; are no more b/b/p's to scan.
2806 (c-safe
2807 (while t
2808 (setq pa+1 (scan-lists ren+1 1 -1) ; Into (/{/[; might signal
2809 paren+1s (cons pa+1 paren+1s))
2810 (setq ren+1 (scan-lists pa+1 1 1)) ; Out of )/}/]; might signal
2811 (if (and (eq (char-before pa+1) ?{)) ; Check for a macro later.
2812 (setq bra+1 pa+1))
2813 (setcar paren+1s ren+1)))
2814
2815 (if (and pa+1 (> pa+1 ren+1))
2816 ;; We've just entered a deeper nesting level.
2817 (progn
2818 ;; Insert the brace pair (if present) and the single open
2819 ;; paren/brace/bracket into `c-state-cache' It cannot be
2820 ;; inside a macro, except one around point, because of what
2821 ;; `c-neutralize-syntax-in-CPP' has done.
2822 (c-state-push-any-brace-pair bra+1 macro-start-or-here)
2823 ;; Insert the opening brace/bracket/paren position.
2824 (setq c-state-cache (cons (1- pa+1) c-state-cache))
2825 ;; Clear admin stuff for the next more nested part of the scan.
2826 (setq ren+1 pa+1 pa+1 nil bra+1 nil bra+1s nil)
2827 t) ; Carry on the loop
2828
2829 ;; All open p/b/b's at this nesting level, if any, have probably
2830 ;; been closed by matching/mismatching ones. We're probably
2831 ;; finished - we just need to check for having found an
2832 ;; unmatched )/}/], which we ignore. Such a )/}/] can't be in a
2833 ;; macro, due the action of `c-neutralize-syntax-in-CPP'.
2834 (c-safe (setq ren+1 (scan-lists ren+1 1 1)))))) ; acts as loop control.
2835
2836 ;; Record the final, innermost, brace-pair if there is one.
2837 (c-state-push-any-brace-pair bra+1 macro-start-or-here)
2838
2839 ;; Determine a good pos
2840 (while (and (setq paren+1 (car paren+1s))
2841 (> (if (> paren+1 macro-start-or-here)
2842 paren+1
2843 (goto-char paren+1)
2844 (setq mstart (and (c-beginning-of-macro)
2845 (point)))
2846 (or mstart paren+1))
2847 here-bol))
2848 (setq paren+1s (cdr paren+1s)))
2849 (cond
2850 ((and paren+1 mstart)
2851 (min paren+1 mstart))
2852 (paren+1)
2853 (t from)))))
2854
2855 (defun c-remove-stale-state-cache (good-pos pps-point)
2856 ;; Remove stale entries from the `c-cache-state', i.e. those which will
2857 ;; not be in it when it is amended for position (point-max).
2858 ;; Additionally, the "outermost" open-brace entry before (point-max)
2859 ;; will be converted to a cons if the matching close-brace is scanned.
2860 ;;
2861 ;; GOOD-POS is a "maximal" "safe position" - there must be no open
2862 ;; parens/braces/brackets between GOOD-POS and (point-max).
2863 ;;
2864 ;; As a second thing, calculate the result of parse-partial-sexp at
2865 ;; PPS-POINT, w.r.t. GOOD-POS. The motivation here is that
2866 ;; `c-state-cache-good-pos' may become PPS-POINT, but the caller may need to
2867 ;; adjust it to get outside a string/comment. (Sorry about this! The code
2868 ;; needs to be FAST).
2869 ;;
2870 ;; Return a list (GOOD-POS SCAN-BACK-POS PPS-STATE), where
2871 ;; o - GOOD-POS is a position where the new value `c-state-cache' is known
2872 ;; to be good (we aim for this to be as high as possible);
2873 ;; o - SCAN-BACK-POS, if not nil, indicates there may be a brace pair
2874 ;; preceding POS which needs to be recorded in `c-state-cache'. It is a
2875 ;; position to scan backwards from.
2876 ;; o - PPS-STATE is the parse-partial-sexp state at PPS-POINT.
2877 (save-restriction
2878 (narrow-to-region 1 (point-max))
2879 (save-excursion
2880 (let* ((in-macro-start ; start of macro containing (point-max) or nil.
2881 (save-excursion
2882 (goto-char (point-max))
2883 (and (c-beginning-of-macro)
2884 (point))))
2885 (good-pos-actual-macro-start ; Start of macro containing good-pos
2886 ; or nil
2887 (and (< good-pos (point-max))
2888 (save-excursion
2889 (goto-char good-pos)
2890 (and (c-beginning-of-macro)
2891 (point)))))
2892 (good-pos-actual-macro-end ; End of this macro, (maybe
2893 ; (point-max)), or nil.
2894 (and good-pos-actual-macro-start
2895 (save-excursion
2896 (goto-char good-pos-actual-macro-start)
2897 (c-end-of-macro)
2898 (point))))
2899 pps-state ; Will be 9 or 10 elements long.
2900 pos
2901 upper-lim ; ,beyond which `c-state-cache' entries are removed
2902 scan-back-pos
2903 pair-beg pps-point-state target-depth)
2904
2905 ;; Remove entries beyond (point-max). Also remove any entries inside
2906 ;; a macro, unless (point-max) is in the same macro.
2907 (setq upper-lim
2908 (if (or (null c-state-old-cpp-beg)
2909 (and (> (point-max) c-state-old-cpp-beg)
2910 (< (point-max) c-state-old-cpp-end)))
2911 (point-max)
2912 (min (point-max) c-state-old-cpp-beg)))
2913 (while (and c-state-cache (>= (c-state-cache-top-lparen) upper-lim))
2914 (setq scan-back-pos (car-safe (car c-state-cache)))
2915 (setq c-state-cache (cdr c-state-cache)))
2916
2917 ;; If `upper-lim' is inside the last recorded brace pair, remove its
2918 ;; RBrace and indicate we'll need to search backwards for a previous
2919 ;; brace pair.
2920 (when (and c-state-cache
2921 (consp (car c-state-cache))
2922 (> (cdar c-state-cache) upper-lim))
2923 (setcar c-state-cache (caar c-state-cache))
2924 (setq scan-back-pos (car c-state-cache)))
2925
2926 ;; The next loop jumps forward out of a nested level of parens each
2927 ;; time round; the corresponding elements in `c-state-cache' are
2928 ;; removed. `pos' is just after the brace-pair or the open paren at
2929 ;; (car c-state-cache). There can be no open parens/braces/brackets
2930 ;; between `good-pos'/`good-pos-actual-macro-start' and (point-max),
2931 ;; due to the interface spec to this function.
2932 (setq pos (if (and good-pos-actual-macro-end
2933 (not (eq good-pos-actual-macro-start
2934 in-macro-start)))
2935 (1+ good-pos-actual-macro-end) ; get outside the macro as
2936 ; marked by a `category' text property.
2937 good-pos))
2938 (goto-char pos)
2939 (while (and c-state-cache
2940 (< (point) (point-max)))
2941 (cond
2942 ((null pps-state) ; first time through
2943 (setq target-depth -1))
2944 ((eq (car pps-state) target-depth) ; found closing ),},]
2945 (setq target-depth (1- (car pps-state))))
2946 ;; Do nothing when we've merely reached pps-point.
2947 )
2948
2949 ;; Scan!
2950 (setq pps-state
2951 (parse-partial-sexp
2952 (point) (if (< (point) pps-point) pps-point (point-max))
2953 target-depth
2954 nil pps-state))
2955
2956 (if (= (point) pps-point)
2957 (setq pps-point-state pps-state))
2958
2959 (when (eq (car pps-state) target-depth)
2960 (setq pos (point)) ; POS is now just after an R-paren/brace.
2961 (cond
2962 ((and (consp (car c-state-cache))
2963 (eq (point) (cdar c-state-cache)))
2964 ;; We've just moved out of the paren pair containing the brace-pair
2965 ;; at (car c-state-cache). `pair-beg' is where the open paren is,
2966 ;; and is potentially where the open brace of a cons in
2967 ;; c-state-cache will be.
2968 (setq pair-beg (car-safe (cdr c-state-cache))
2969 c-state-cache (cdr-safe (cdr c-state-cache)))) ; remove {}pair + containing Lparen.
2970 ((numberp (car c-state-cache))
2971 (setq pair-beg (car c-state-cache)
2972 c-state-cache (cdr c-state-cache))) ; remove this
2973 ; containing Lparen
2974 ((numberp (cadr c-state-cache))
2975 (setq pair-beg (cadr c-state-cache)
2976 c-state-cache (cddr c-state-cache))) ; Remove a paren pair
2977 ; together with enclosed brace pair.
2978 ;; (t nil) ; Ignore an unmated Rparen.
2979 )))
2980
2981 (if (< (point) pps-point)
2982 (setq pps-state (parse-partial-sexp (point) pps-point
2983 nil nil ; TARGETDEPTH, STOPBEFORE
2984 pps-state)))
2985
2986 ;; If the last paren pair we moved out of was actually a brace pair,
2987 ;; insert it into `c-state-cache'.
2988 (when (and pair-beg (eq (char-after pair-beg) ?{))
2989 (if (consp (car-safe c-state-cache))
2990 (setq c-state-cache (cdr c-state-cache)))
2991 (setq c-state-cache (cons (cons pair-beg pos)
2992 c-state-cache)))
2993
2994 (list pos scan-back-pos pps-state)))))
2995
2996 (defun c-remove-stale-state-cache-backwards (here cache-pos)
2997 ;; Strip stale elements of `c-state-cache' by moving backwards through the
2998 ;; buffer, and inform the caller of the scenario detected.
2999 ;;
3000 ;; HERE is the position we're setting `c-state-cache' for.
3001 ;; CACHE-POS is just after the latest recorded position in `c-state-cache'
3002 ;; before HERE, or a position at or near point-min which isn't in a
3003 ;; literal.
3004 ;;
3005 ;; This function must only be called only when (> `c-state-cache-good-pos'
3006 ;; HERE). Usually the gap between CACHE-POS and HERE is large. It is thus
3007 ;; optimized to eliminate (or minimize) scanning between these two
3008 ;; positions.
3009 ;;
3010 ;; Return a three element list (GOOD-POS SCAN-BACK-POS FWD-FLAG), where:
3011 ;; o - GOOD-POS is a "good position", where `c-state-cache' is valid, or
3012 ;; could become so after missing elements are inserted into
3013 ;; `c-state-cache'. This is JUST AFTER an opening or closing
3014 ;; brace/paren/bracket which is already in `c-state-cache' or just before
3015 ;; one otherwise. exceptionally (when there's no such b/p/b handy) the BOL
3016 ;; before `here''s line, or the start of the literal containing it.
3017 ;; o - SCAN-BACK-POS, if non-nil, indicates there may be a brace pair
3018 ;; preceding POS which isn't recorded in `c-state-cache'. It is a position
3019 ;; to scan backwards from.
3020 ;; o - FWD-FLAG, if non-nil, indicates there may be parens/braces between
3021 ;; POS and HERE which aren't recorded in `c-state-cache'.
3022 ;;
3023 ;; The comments in this defun use "paren" to mean parenthesis or square
3024 ;; bracket (as contrasted with a brace), and "(" and ")" likewise.
3025 ;;
3026 ;; . {..} (..) (..) ( .. { } ) (...) ( .... . ..)
3027 ;; | | | | | |
3028 ;; CP E here D C good
3029 (let ((pos c-state-cache-good-pos)
3030 pa ren ; positions of "(" and ")"
3031 dropped-cons ; whether the last element dropped from `c-state-cache'
3032 ; was a cons (representing a brace-pair)
3033 good-pos ; see above.
3034 lit ; (START . END) of a literal containing some point.
3035 here-lit-start here-lit-end ; bounds of literal containing `here'
3036 ; or `here' itself.
3037 here- here+ ; start/end of macro around HERE, or HERE
3038 (here-bol (c-point 'bol here))
3039 (too-far-back (max (- here c-state-cache-too-far) (point-min))))
3040
3041 ;; Remove completely irrelevant entries from `c-state-cache'.
3042 (while (and c-state-cache
3043 (>= (setq pa (c-state-cache-top-lparen)) here))
3044 (setq dropped-cons (consp (car c-state-cache)))
3045 (setq c-state-cache (cdr c-state-cache))
3046 (setq pos pa))
3047 ;; At this stage, (> pos here);
3048 ;; (< (c-state-cache-top-lparen) here) (or is nil).
3049
3050 (cond
3051 ((and (consp (car c-state-cache))
3052 (> (cdar c-state-cache) here))
3053 ;; CASE 1: The top of the cache is a brace pair which now encloses
3054 ;; `here'. As good-pos, return the address. of the "{". Since we've no
3055 ;; knowledge of what's inside these braces, we have no alternative but
3056 ;; to direct the caller to scan the buffer from the opening brace.
3057 (setq pos (caar c-state-cache))
3058 (setcar c-state-cache pos)
3059 (list (1+ pos) pos t)) ; return value. We've just converted a brace pair
3060 ; entry into a { entry, so the caller needs to
3061 ; search for a brace pair before the {.
3062
3063 ;; `here' might be inside a literal. Check for this.
3064 ((progn
3065 (setq lit (c-state-literal-at here)
3066 here-lit-start (or (car lit) here)
3067 here-lit-end (or (cdr lit) here))
3068 ;; Has `here' just "newly entered" a macro?
3069 (save-excursion
3070 (goto-char here-lit-start)
3071 (if (and (c-beginning-of-macro)
3072 (or (null c-state-old-cpp-beg)
3073 (not (= (point) c-state-old-cpp-beg))))
3074 (progn
3075 (setq here- (point))
3076 (c-end-of-macro)
3077 (setq here+ (point)))
3078 (setq here- here-lit-start
3079 here+ here-lit-end)))
3080
3081 ;; `here' might be nested inside any depth of parens (or brackets but
3082 ;; not braces). Scan backwards to find the outermost such opening
3083 ;; paren, if there is one. This will be the scan position to return.
3084 (save-restriction
3085 (narrow-to-region cache-pos (point-max))
3086 (setq pos (c-state-balance-parens-backwards here- here+ pos)))
3087 nil)) ; for the cond
3088
3089 ((< pos here-lit-start)
3090 ;; CASE 2: Address of outermost ( or [ which now encloses `here', but
3091 ;; didn't enclose the (previous) `c-state-cache-good-pos'. If there is
3092 ;; a brace pair preceding this, it will already be in `c-state-cache',
3093 ;; unless there was a brace pair after it, i.e. there'll only be one to
3094 ;; scan for if we've just deleted one.
3095 (list pos (and dropped-cons pos) t)) ; Return value.
3096
3097 ;; `here' isn't enclosed in a (previously unrecorded) bracket/paren.
3098 ;; Further forward scanning isn't needed, but we still need to find a
3099 ;; GOOD-POS. Step out of all enclosing "("s on HERE's line.
3100 ((progn
3101 (save-restriction
3102 (narrow-to-region here-bol (point-max))
3103 (setq pos here-lit-start)
3104 (c-safe (while (setq pa (scan-lists pos -1 1))
3105 (setq pos pa)))) ; might signal
3106 nil)) ; for the cond
3107
3108 ((setq ren (c-safe-scan-lists pos -1 -1 too-far-back))
3109 ;; CASE 3: After a }/)/] before `here''s BOL.
3110 (list (1+ ren) (and dropped-cons pos) nil)) ; Return value
3111
3112 (t
3113 ;; CASE 4; Best of a bad job: BOL before `here-bol', or beginning of
3114 ;; literal containing it.
3115 (setq good-pos (c-state-lit-beg (c-point 'bopl here-bol)))
3116 (list good-pos (and dropped-cons good-pos) nil)))))
3117
3118
3119 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
3120 ;; Externally visible routines.
3121
3122 (defun c-state-cache-init ()
3123 (setq c-state-cache nil
3124 c-state-cache-good-pos 1
3125 c-state-nonlit-pos-cache nil
3126 c-state-nonlit-pos-cache-limit 1
3127 c-state-semi-nonlit-pos-cache nil
3128 c-state-semi-nonlit-pos-cache-limit 1
3129 c-state-brace-pair-desert nil
3130 c-state-point-min 1
3131 c-state-point-min-lit-type nil
3132 c-state-point-min-lit-start nil
3133 c-state-min-scan-pos 1
3134 c-state-old-cpp-beg nil
3135 c-state-old-cpp-end nil)
3136 (c-state-mark-point-min-literal))
3137
3138 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
3139 ;; Debugging routines to dump `c-state-cache' in a "replayable" form.
3140 ;; (defmacro c-sc-de (elt) ; "c-state-cache-dump-element"
3141 ;; `(format ,(concat "(setq " (symbol-name elt) " %s) ") ,elt))
3142 ;; (defmacro c-sc-qde (elt) ; "c-state-cache-quote-dump-element"
3143 ;; `(format ,(concat "(setq " (symbol-name elt) " '%s) ") ,elt))
3144 ;; (defun c-state-dump ()
3145 ;; ;; For debugging.
3146 ;; ;(message
3147 ;; (concat
3148 ;; (c-sc-qde c-state-cache)
3149 ;; (c-sc-de c-state-cache-good-pos)
3150 ;; (c-sc-qde c-state-nonlit-pos-cache)
3151 ;; (c-sc-de c-state-nonlit-pos-cache-limit)
3152 ;; (c-sc-qde c-state-brace-pair-desert)
3153 ;; (c-sc-de c-state-point-min)
3154 ;; (c-sc-de c-state-point-min-lit-type)
3155 ;; (c-sc-de c-state-point-min-lit-start)
3156 ;; (c-sc-de c-state-min-scan-pos)
3157 ;; (c-sc-de c-state-old-cpp-beg)
3158 ;; (c-sc-de c-state-old-cpp-end)))
3159 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
3160
3161 (defun c-invalidate-state-cache-1 (here)
3162 ;; Invalidate all info on `c-state-cache' that applies to the buffer at HERE
3163 ;; or higher and set `c-state-cache-good-pos' accordingly. The cache is
3164 ;; left in a consistent state.
3165 ;;
3166 ;; This is much like `c-whack-state-after', but it never changes a paren
3167 ;; pair element into an open paren element. Doing that would mean that the
3168 ;; new open paren wouldn't have the required preceding paren pair element.
3169 ;;
3170 ;; This function is called from c-after-change.
3171
3172 ;; The caches of non-literals:
3173 (if (< here c-state-nonlit-pos-cache-limit)
3174 (setq c-state-nonlit-pos-cache-limit here))
3175 (if (< here c-state-semi-nonlit-pos-cache-limit)
3176 (setq c-state-semi-nonlit-pos-cache-limit here))
3177
3178 ;; `c-state-cache':
3179 ;; Case 1: if `here' is in a literal containing point-min, everything
3180 ;; becomes (or is already) nil.
3181 (if (or (null c-state-cache-good-pos)
3182 (< here (c-state-get-min-scan-pos)))
3183 (setq c-state-cache nil
3184 c-state-cache-good-pos nil
3185 c-state-min-scan-pos nil)
3186
3187 ;; Truncate `c-state-cache' and set `c-state-cache-good-pos' to a value
3188 ;; below `here'. To maintain its consistency, we may need to insert a new
3189 ;; brace pair.
3190 (let ((here-bol (c-point 'bol here))
3191 too-high-pa ; recorded {/(/[ next above here, or nil.
3192 dropped-cons ; was the last removed element a brace pair?
3193 pa)
3194 ;; The easy bit - knock over-the-top bits off `c-state-cache'.
3195 (while (and c-state-cache
3196 (>= (setq pa (c-state-cache-top-paren)) here))
3197 (setq dropped-cons (consp (car c-state-cache))
3198 too-high-pa (c-state-cache-top-lparen)
3199 c-state-cache (cdr c-state-cache)))
3200
3201 ;; Do we need to add in an earlier brace pair, having lopped one off?
3202 (if (and dropped-cons
3203 (< too-high-pa (+ here c-state-cache-too-far)))
3204 (c-append-lower-brace-pair-to-state-cache too-high-pa here-bol))
3205 (setq c-state-cache-good-pos (or (c-state-cache-after-top-paren)
3206 (c-state-get-min-scan-pos)))))
3207
3208 ;; The brace-pair desert marker:
3209 (when (car c-state-brace-pair-desert)
3210 (if (< here (car c-state-brace-pair-desert))
3211 (setq c-state-brace-pair-desert nil)
3212 (if (< here (cdr c-state-brace-pair-desert))
3213 (setcdr c-state-brace-pair-desert here)))))
3214
3215 (defun c-parse-state-1 ()
3216 ;; Find and record all noteworthy parens between some good point earlier in
3217 ;; the file and point. That good point is at least the beginning of the
3218 ;; top-level construct we are in, or the beginning of the preceding
3219 ;; top-level construct if we aren't in one.
3220 ;;
3221 ;; The returned value is a list of the noteworthy parens with the last one
3222 ;; first. If an element in the list is an integer, it's the position of an
3223 ;; open paren (of any type) which has not been closed before the point. If
3224 ;; an element is a cons, it gives the position of a closed BRACE paren
3225 ;; pair[*]; the car is the start brace position and the cdr is the position
3226 ;; following the closing brace. Only the last closed brace paren pair
3227 ;; before each open paren and before the point is recorded, and thus the
3228 ;; state never contains two cons elements in succession. When a close brace
3229 ;; has no matching open brace (e.g., the matching brace is outside the
3230 ;; visible region), it is not represented in the returned value.
3231 ;;
3232 ;; [*] N.B. The close "brace" might be a mismatching close bracket or paren.
3233 ;; This defun explicitly treats mismatching parens/braces/brackets as
3234 ;; matching. It is the open brace which makes it a "brace" pair.
3235 ;;
3236 ;; If POINT is within a macro, open parens and brace pairs within
3237 ;; THIS macro MIGHT be recorded. This depends on whether their
3238 ;; syntactic properties have been suppressed by
3239 ;; `c-neutralize-syntax-in-CPP'. This might need fixing (2008-12-11).
3240 ;;
3241 ;; Currently no characters which are given paren syntax with the
3242 ;; syntax-table property are recorded, i.e. angle bracket arglist
3243 ;; parens are never present here. Note that this might change.
3244 ;;
3245 ;; BUG: This function doesn't cope entirely well with unbalanced
3246 ;; parens in macros. (2008-12-11: this has probably been resolved
3247 ;; by the function `c-neutralize-syntax-in-CPP'.) E.g. in the
3248 ;; following case the brace before the macro isn't balanced with the
3249 ;; one after it:
3250 ;;
3251 ;; {
3252 ;; #define X {
3253 ;; }
3254 ;;
3255 ;; Note to maintainers: this function DOES get called with point
3256 ;; within comments and strings, so don't assume it doesn't!
3257 ;;
3258 ;; This function might do hidden buffer changes.
3259 (let* ((here (point))
3260 (here-bopl (c-point 'bopl))
3261 strategy ; 'forward, 'backward etc..
3262 ;; Candidate positions to start scanning from:
3263 cache-pos ; highest position below HERE already existing in
3264 ; cache (or 1).
3265 good-pos
3266 start-point
3267 bopl-state
3268 res
3269 scan-backward-pos scan-forward-p) ; used for 'backward.
3270 ;; If POINT-MIN has changed, adjust the cache
3271 (unless (= (point-min) c-state-point-min)
3272 (c-renarrow-state-cache))
3273
3274 ;; Strategy?
3275 (setq res (c-parse-state-get-strategy here c-state-cache-good-pos)
3276 strategy (car res)
3277 cache-pos (cadr res)
3278 start-point (nth 2 res))
3279
3280 (when (eq strategy 'BOD)
3281 (setq c-state-cache nil
3282 c-state-cache-good-pos start-point))
3283
3284 ;; SCAN!
3285 (save-restriction
3286 (cond
3287 ((memq strategy '(forward BOD))
3288 (narrow-to-region (point-min) here)
3289 (setq res (c-remove-stale-state-cache start-point here-bopl))
3290 (setq cache-pos (car res)
3291 scan-backward-pos (cadr res)
3292 bopl-state (car (cddr res))) ; will be nil if (< here-bopl
3293 ; start-point)
3294 (if scan-backward-pos
3295 (c-append-lower-brace-pair-to-state-cache scan-backward-pos))
3296 (setq good-pos
3297 (c-append-to-state-cache cache-pos))
3298 (setq c-state-cache-good-pos
3299 (if (and bopl-state
3300 (< good-pos (- here c-state-cache-too-far)))
3301 (c-state-cache-non-literal-place here-bopl bopl-state)
3302 good-pos)))
3303
3304 ((eq strategy 'backward)
3305 (setq res (c-remove-stale-state-cache-backwards here cache-pos)
3306 good-pos (car res)
3307 scan-backward-pos (cadr res)
3308 scan-forward-p (car (cddr res)))
3309 (if scan-backward-pos
3310 (c-append-lower-brace-pair-to-state-cache
3311 scan-backward-pos))
3312 (setq c-state-cache-good-pos
3313 (if scan-forward-p
3314 (progn (narrow-to-region (point-min) here)
3315 (c-append-to-state-cache good-pos))
3316 good-pos)))
3317
3318 (t ; (eq strategy 'IN-LIT)
3319 (setq c-state-cache nil
3320 c-state-cache-good-pos nil)))))
3321
3322 c-state-cache)
3323
3324 (defun c-invalidate-state-cache (here)
3325 ;; This is a wrapper over `c-invalidate-state-cache-1'.
3326 ;;
3327 ;; It suppresses the syntactic effect of the < and > (template) brackets and
3328 ;; of all parens in preprocessor constructs, except for any such construct
3329 ;; containing point. We can then call `c-invalidate-state-cache-1' without
3330 ;; worrying further about macros and template delimiters.
3331 (c-with-<->-as-parens-suppressed
3332 (if (and c-state-old-cpp-beg
3333 (< c-state-old-cpp-beg here))
3334 (c-with-all-but-one-cpps-commented-out
3335 c-state-old-cpp-beg
3336 (min c-state-old-cpp-end here)
3337 (c-invalidate-state-cache-1 here))
3338 (c-with-cpps-commented-out
3339 (c-invalidate-state-cache-1 here)))))
3340
3341 (defmacro c-state-maybe-marker (place marker)
3342 ;; If PLACE is non-nil, return a marker marking it, otherwise nil.
3343 ;; We (re)use MARKER.
3344 `(and ,place
3345 (or ,marker (setq ,marker (make-marker)))
3346 (set-marker ,marker ,place)))
3347
3348 (defun c-parse-state ()
3349 ;; This is a wrapper over `c-parse-state-1'. See that function for a
3350 ;; description of the functionality and return value.
3351 ;;
3352 ;; It suppresses the syntactic effect of the < and > (template) brackets and
3353 ;; of all parens in preprocessor constructs, except for any such construct
3354 ;; containing point. We can then call `c-parse-state-1' without worrying
3355 ;; further about macros and template delimiters.
3356 (let (here-cpp-beg here-cpp-end)
3357 (save-excursion
3358 (when (c-beginning-of-macro)
3359 (setq here-cpp-beg (point))
3360 (unless
3361 (> (setq here-cpp-end (c-syntactic-end-of-macro))
3362 here-cpp-beg)
3363 (setq here-cpp-beg nil here-cpp-end nil))))
3364 ;; FIXME!!! Put in a `condition-case' here to protect the integrity of the
3365 ;; subsystem.
3366 (prog1
3367 (c-with-<->-as-parens-suppressed
3368 (if (and here-cpp-beg (> here-cpp-end here-cpp-beg))
3369 (c-with-all-but-one-cpps-commented-out
3370 here-cpp-beg here-cpp-end
3371 (c-parse-state-1))
3372 (c-with-cpps-commented-out
3373 (c-parse-state-1))))
3374 (setq c-state-old-cpp-beg
3375 (c-state-maybe-marker here-cpp-beg c-state-old-cpp-beg-marker)
3376 c-state-old-cpp-end
3377 (c-state-maybe-marker here-cpp-end c-state-old-cpp-end-marker)))))
3378
3379 ;; Debug tool to catch cache inconsistencies. This is called from
3380 ;; 000tests.el.
3381 (defvar c-debug-parse-state nil)
3382 (unless (fboundp 'c-real-parse-state)
3383 (fset 'c-real-parse-state (symbol-function 'c-parse-state)))
3384 (cc-bytecomp-defun c-real-parse-state)
3385
3386 (defvar c-parse-state-point nil)
3387 (defvar c-parse-state-state nil)
3388 (defun c-record-parse-state-state ()
3389 (setq c-parse-state-point (point))
3390 (setq c-parse-state-state
3391 (mapcar
3392 (lambda (arg)
3393 (let ((val (symbol-value arg)))
3394 (cons arg
3395 (if (consp val)
3396 (copy-tree val)
3397 val))))
3398 '(c-state-cache
3399 c-state-cache-good-pos
3400 c-state-nonlit-pos-cache
3401 c-state-nonlit-pos-cache-limit
3402 c-state-semi-nonlit-pos-cache
3403 c-state-semi-nonlit-pos-cache-limit
3404 c-state-brace-pair-desert
3405 c-state-point-min
3406 c-state-point-min-lit-type
3407 c-state-point-min-lit-start
3408 c-state-min-scan-pos
3409 c-state-old-cpp-beg
3410 c-state-old-cpp-end
3411 c-parse-state-point))))
3412 (defun c-replay-parse-state-state ()
3413 (message
3414 (concat "(setq "
3415 (mapconcat
3416 (lambda (arg)
3417 (format "%s %s%s" (car arg) (if (atom (cdr arg)) "" "'") (cdr arg)))
3418 c-parse-state-state " ")
3419 ")")))
3420
3421 (defun c-debug-parse-state-double-cons (state)
3422 (let (state-car conses-not-ok)
3423 (while state
3424 (setq state-car (car state)
3425 state (cdr state))
3426 (if (and (consp state-car)
3427 (consp (car state)))
3428 (setq conses-not-ok t)))
3429 conses-not-ok))
3430
3431 (defun c-debug-parse-state ()
3432 (let ((here (point)) (res1 (c-real-parse-state)) res2)
3433 (let ((c-state-cache nil)
3434 (c-state-cache-good-pos 1)
3435 (c-state-nonlit-pos-cache nil)
3436 (c-state-nonlit-pos-cache-limit 1)
3437 (c-state-brace-pair-desert nil)
3438 (c-state-point-min 1)
3439 (c-state-point-min-lit-type nil)
3440 (c-state-point-min-lit-start nil)
3441 (c-state-min-scan-pos 1)
3442 (c-state-old-cpp-beg nil)
3443 (c-state-old-cpp-end nil))
3444 (setq res2 (c-real-parse-state)))
3445 (unless (equal res1 res2)
3446 ;; The cache can actually go further back due to the ad-hoc way
3447 ;; the first paren is found, so try to whack off a bit of its
3448 ;; start before complaining.
3449 ;; (save-excursion
3450 ;; (goto-char (or (c-least-enclosing-brace res2) (point)))
3451 ;; (c-beginning-of-defun-1)
3452 ;; (while (not (or (bobp) (eq (char-after) ?{)))
3453 ;; (c-beginning-of-defun-1))
3454 ;; (unless (equal (c-whack-state-before (point) res1) res2)
3455 ;; (message (concat "c-parse-state inconsistency at %s: "
3456 ;; "using cache: %s, from scratch: %s")
3457 ;; here res1 res2)))
3458 (message (concat "c-parse-state inconsistency at %s: "
3459 "using cache: %s, from scratch: %s")
3460 here res1 res2)
3461 (message "Old state:")
3462 (c-replay-parse-state-state))
3463
3464 (when (c-debug-parse-state-double-cons res1)
3465 (message "c-parse-state INVALIDITY at %s: %s"
3466 here res1)
3467 (message "Old state:")
3468 (c-replay-parse-state-state))
3469
3470 (c-record-parse-state-state)
3471 res2 ; res1 correct a cascading series of errors ASAP
3472 ))
3473
3474 (defun c-toggle-parse-state-debug (&optional arg)
3475 (interactive "P")
3476 (setq c-debug-parse-state (c-calculate-state arg c-debug-parse-state))
3477 (fset 'c-parse-state (symbol-function (if c-debug-parse-state
3478 'c-debug-parse-state
3479 'c-real-parse-state)))
3480 (c-keep-region-active)
3481 (message "c-debug-parse-state %sabled"
3482 (if c-debug-parse-state "en" "dis")))
3483 (when c-debug-parse-state
3484 (c-toggle-parse-state-debug 1))
3485
3486 \f
3487 (defun c-whack-state-before (bufpos paren-state)
3488 ;; Whack off any state information from PAREN-STATE which lies
3489 ;; before BUFPOS. Not destructive on PAREN-STATE.
3490 (let* ((newstate (list nil))
3491 (ptr newstate)
3492 car)
3493 (while paren-state
3494 (setq car (car paren-state)
3495 paren-state (cdr paren-state))
3496 (if (< (if (consp car) (car car) car) bufpos)
3497 (setq paren-state nil)
3498 (setcdr ptr (list car))
3499 (setq ptr (cdr ptr))))
3500 (cdr newstate)))
3501
3502 (defun c-whack-state-after (bufpos paren-state)
3503 ;; Whack off any state information from PAREN-STATE which lies at or
3504 ;; after BUFPOS. Not destructive on PAREN-STATE.
3505 (catch 'done
3506 (while paren-state
3507 (let ((car (car paren-state)))
3508 (if (consp car)
3509 ;; just check the car, because in a balanced brace
3510 ;; expression, it must be impossible for the corresponding
3511 ;; close brace to be before point, but the open brace to
3512 ;; be after.
3513 (if (<= bufpos (car car))
3514 nil ; whack it off
3515 (if (< bufpos (cdr car))
3516 ;; its possible that the open brace is before
3517 ;; bufpos, but the close brace is after. In that
3518 ;; case, convert this to a non-cons element. The
3519 ;; rest of the state is before bufpos, so we're
3520 ;; done.
3521 (throw 'done (cons (car car) (cdr paren-state)))
3522 ;; we know that both the open and close braces are
3523 ;; before bufpos, so we also know that everything else
3524 ;; on state is before bufpos.
3525 (throw 'done paren-state)))
3526 (if (<= bufpos car)
3527 nil ; whack it off
3528 ;; it's before bufpos, so everything else should too.
3529 (throw 'done paren-state)))
3530 (setq paren-state (cdr paren-state)))
3531 nil)))
3532
3533 (defun c-most-enclosing-brace (paren-state &optional bufpos)
3534 ;; Return the bufpos of the innermost enclosing open paren before
3535 ;; bufpos, or nil if none was found.
3536 (let (enclosingp)
3537 (or bufpos (setq bufpos 134217727))
3538 (while paren-state
3539 (setq enclosingp (car paren-state)
3540 paren-state (cdr paren-state))
3541 (if (or (consp enclosingp)
3542 (>= enclosingp bufpos))
3543 (setq enclosingp nil)
3544 (setq paren-state nil)))
3545 enclosingp))
3546
3547 (defun c-least-enclosing-brace (paren-state)
3548 ;; Return the bufpos of the outermost enclosing open paren, or nil
3549 ;; if none was found.
3550 (let (pos elem)
3551 (while paren-state
3552 (setq elem (car paren-state)
3553 paren-state (cdr paren-state))
3554 (if (integerp elem)
3555 (setq pos elem)))
3556 pos))
3557
3558 (defun c-safe-position (bufpos paren-state)
3559 ;; Return the closest "safe" position recorded on PAREN-STATE that
3560 ;; is higher up than BUFPOS. Return nil if PAREN-STATE doesn't
3561 ;; contain any. Return nil if BUFPOS is nil, which is useful to
3562 ;; find the closest limit before a given limit that might be nil.
3563 ;;
3564 ;; A "safe" position is a position at or after a recorded open
3565 ;; paren, or after a recorded close paren. The returned position is
3566 ;; thus either the first position after a close brace, or the first
3567 ;; position after an enclosing paren, or at the enclosing paren in
3568 ;; case BUFPOS is immediately after it.
3569 (when bufpos
3570 (let (elem)
3571 (catch 'done
3572 (while paren-state
3573 (setq elem (car paren-state))
3574 (if (consp elem)
3575 (cond ((< (cdr elem) bufpos)
3576 (throw 'done (cdr elem)))
3577 ((< (car elem) bufpos)
3578 ;; See below.
3579 (throw 'done (min (1+ (car elem)) bufpos))))
3580 (if (< elem bufpos)
3581 ;; elem is the position at and not after the opening paren, so
3582 ;; we can go forward one more step unless it's equal to
3583 ;; bufpos. This is useful in some cases avoid an extra paren
3584 ;; level between the safe position and bufpos.
3585 (throw 'done (min (1+ elem) bufpos))))
3586 (setq paren-state (cdr paren-state)))))))
3587
3588 (defun c-beginning-of-syntax ()
3589 ;; This is used for `font-lock-beginning-of-syntax-function'. It
3590 ;; goes to the closest previous point that is known to be outside
3591 ;; any string literal or comment. `c-state-cache' is used if it has
3592 ;; a position in the vicinity.
3593 (let* ((paren-state c-state-cache)
3594 elem
3595
3596 (pos (catch 'done
3597 ;; Note: Similar code in `c-safe-position'. The
3598 ;; difference is that we accept a safe position at
3599 ;; the point and don't bother to go forward past open
3600 ;; parens.
3601 (while paren-state
3602 (setq elem (car paren-state))
3603 (if (consp elem)
3604 (cond ((<= (cdr elem) (point))
3605 (throw 'done (cdr elem)))
3606 ((<= (car elem) (point))
3607 (throw 'done (car elem))))
3608 (if (<= elem (point))
3609 (throw 'done elem)))
3610 (setq paren-state (cdr paren-state)))
3611 (point-min))))
3612
3613 (if (> pos (- (point) 4000))
3614 (goto-char pos)
3615 ;; The position is far back. Try `c-beginning-of-defun-1'
3616 ;; (although we can't be entirely sure it will go to a position
3617 ;; outside a comment or string in current emacsen). FIXME:
3618 ;; Consult `syntax-ppss' here.
3619 (c-beginning-of-defun-1)
3620 (if (< (point) pos)
3621 (goto-char pos)))))
3622
3623 \f
3624 ;; Tools for scanning identifiers and other tokens.
3625
3626 (defun c-on-identifier ()
3627 "Return non-nil if the point is on or directly after an identifier.
3628 Keywords are recognized and not considered identifiers. If an
3629 identifier is detected, the returned value is its starting position.
3630 If an identifier ends at the point and another begins at it \(can only
3631 happen in Pike) then the point for the preceding one is returned.
3632
3633 Note that this function might do hidden buffer changes. See the
3634 comment at the start of cc-engine.el for more info."
3635
3636 ;; FIXME: Shouldn't this function handle "operator" in C++?
3637
3638 (save-excursion
3639 (skip-syntax-backward "w_")
3640
3641 (or
3642
3643 ;; Check for a normal (non-keyword) identifier.
3644 (and (looking-at c-symbol-start)
3645 (not (looking-at c-keywords-regexp))
3646 (point))
3647
3648 (when (c-major-mode-is 'pike-mode)
3649 ;; Handle the `<operator> syntax in Pike.
3650 (let ((pos (point)))
3651 (skip-chars-backward "-!%&*+/<=>^|~[]()")
3652 (and (if (< (skip-chars-backward "`") 0)
3653 t
3654 (goto-char pos)
3655 (eq (char-after) ?\`))
3656 (looking-at c-symbol-key)
3657 (>= (match-end 0) pos)
3658 (point))))
3659
3660 ;; Handle the "operator +" syntax in C++.
3661 (when (and c-overloadable-operators-regexp
3662 (= (c-backward-token-2 0) 0))
3663
3664 (cond ((and (looking-at c-overloadable-operators-regexp)
3665 (or (not c-opt-op-identifier-prefix)
3666 (and (= (c-backward-token-2 1) 0)
3667 (looking-at c-opt-op-identifier-prefix))))
3668 (point))
3669
3670 ((save-excursion
3671 (and c-opt-op-identifier-prefix
3672 (looking-at c-opt-op-identifier-prefix)
3673 (= (c-forward-token-2 1) 0)
3674 (looking-at c-overloadable-operators-regexp)))
3675 (point))))
3676
3677 )))
3678
3679 (defsubst c-simple-skip-symbol-backward ()
3680 ;; If the point is at the end of a symbol then skip backward to the
3681 ;; beginning of it. Don't move otherwise. Return non-nil if point
3682 ;; moved.
3683 ;;
3684 ;; This function might do hidden buffer changes.
3685 (or (< (skip-syntax-backward "w_") 0)
3686 (and (c-major-mode-is 'pike-mode)
3687 ;; Handle the `<operator> syntax in Pike.
3688 (let ((pos (point)))
3689 (if (and (< (skip-chars-backward "-!%&*+/<=>^|~[]()") 0)
3690 (< (skip-chars-backward "`") 0)
3691 (looking-at c-symbol-key)
3692 (>= (match-end 0) pos))
3693 t
3694 (goto-char pos)
3695 nil)))))
3696
3697 (defun c-beginning-of-current-token (&optional back-limit)
3698 ;; Move to the beginning of the current token. Do not move if not
3699 ;; in the middle of one. BACK-LIMIT may be used to bound the
3700 ;; backward search; if given it's assumed to be at the boundary
3701 ;; between two tokens. Return non-nil if the point is moved, nil
3702 ;; otherwise.
3703 ;;
3704 ;; This function might do hidden buffer changes.
3705 (let ((start (point)))
3706 (if (looking-at "\\w\\|\\s_")
3707 (skip-syntax-backward "w_" back-limit)
3708 (when (< (skip-syntax-backward ".()" back-limit) 0)
3709 (while (let ((pos (or (and (looking-at c-nonsymbol-token-regexp)
3710 (match-end 0))
3711 ;; `c-nonsymbol-token-regexp' should always match
3712 ;; since we've skipped backward over punctuator
3713 ;; or paren syntax, but consume one char in case
3714 ;; it doesn't so that we don't leave point before
3715 ;; some earlier incorrect token.
3716 (1+ (point)))))
3717 (if (<= pos start)
3718 (goto-char pos))))))
3719 (< (point) start)))
3720
3721 (defun c-end-of-current-token (&optional back-limit)
3722 ;; Move to the end of the current token. Do not move if not in the
3723 ;; middle of one. BACK-LIMIT may be used to bound the backward
3724 ;; search; if given it's assumed to be at the boundary between two
3725 ;; tokens. Return non-nil if the point is moved, nil otherwise.
3726 ;;
3727 ;; This function might do hidden buffer changes.
3728 (let ((start (point)))
3729 (cond ((< (skip-syntax-backward "w_" (1- start)) 0)
3730 (skip-syntax-forward "w_"))
3731 ((< (skip-syntax-backward ".()" back-limit) 0)
3732 (while (progn
3733 (if (looking-at c-nonsymbol-token-regexp)
3734 (goto-char (match-end 0))
3735 ;; `c-nonsymbol-token-regexp' should always match since
3736 ;; we've skipped backward over punctuator or paren
3737 ;; syntax, but move forward in case it doesn't so that
3738 ;; we don't leave point earlier than we started with.
3739 (forward-char))
3740 (< (point) start)))))
3741 (> (point) start)))
3742
3743 (defconst c-jump-syntax-balanced
3744 (if (memq 'gen-string-delim c-emacs-features)
3745 "\\w\\|\\s_\\|\\s\(\\|\\s\)\\|\\s\"\\|\\s|"
3746 "\\w\\|\\s_\\|\\s\(\\|\\s\)\\|\\s\""))
3747
3748 (defconst c-jump-syntax-unbalanced
3749 (if (memq 'gen-string-delim c-emacs-features)
3750 "\\w\\|\\s_\\|\\s\"\\|\\s|"
3751 "\\w\\|\\s_\\|\\s\""))
3752
3753 (defun c-forward-token-2 (&optional count balanced limit)
3754 "Move forward by tokens.
3755 A token is defined as all symbols and identifiers which aren't
3756 syntactic whitespace \(note that multicharacter tokens like \"==\" are
3757 treated properly). Point is always either left at the beginning of a
3758 token or not moved at all. COUNT specifies the number of tokens to
3759 move; a negative COUNT moves in the opposite direction. A COUNT of 0
3760 moves to the next token beginning only if not already at one. If
3761 BALANCED is true, move over balanced parens, otherwise move into them.
3762 Also, if BALANCED is true, never move out of an enclosing paren.
3763
3764 LIMIT sets the limit for the movement and defaults to the point limit.
3765 The case when LIMIT is set in the middle of a token, comment or macro
3766 is handled correctly, i.e. the point won't be left there.
3767
3768 Return the number of tokens left to move \(positive or negative). If
3769 BALANCED is true, a move over a balanced paren counts as one. Note
3770 that if COUNT is 0 and no appropriate token beginning is found, 1 will
3771 be returned. Thus, a return value of 0 guarantees that point is at
3772 the requested position and a return value less \(without signs) than
3773 COUNT guarantees that point is at the beginning of some token.
3774
3775 Note that this function might do hidden buffer changes. See the
3776 comment at the start of cc-engine.el for more info."
3777
3778 (or count (setq count 1))
3779 (if (< count 0)
3780 (- (c-backward-token-2 (- count) balanced limit))
3781
3782 (let ((jump-syntax (if balanced
3783 c-jump-syntax-balanced
3784 c-jump-syntax-unbalanced))
3785 (last (point))
3786 (prev (point)))
3787
3788 (if (zerop count)
3789 ;; If count is zero we should jump if in the middle of a token.
3790 (c-end-of-current-token))
3791
3792 (save-restriction
3793 (if limit (narrow-to-region (point-min) limit))
3794 (if (/= (point)
3795 (progn (c-forward-syntactic-ws) (point)))
3796 ;; Skip whitespace. Count this as a move if we did in
3797 ;; fact move.
3798 (setq count (max (1- count) 0)))
3799
3800 (if (eobp)
3801 ;; Moved out of bounds. Make sure the returned count isn't zero.
3802 (progn
3803 (if (zerop count) (setq count 1))
3804 (goto-char last))
3805
3806 ;; Use `condition-case' to avoid having the limit tests
3807 ;; inside the loop.
3808 (condition-case nil
3809 (while (and
3810 (> count 0)
3811 (progn
3812 (setq last (point))
3813 (cond ((looking-at jump-syntax)
3814 (goto-char (scan-sexps (point) 1))
3815 t)
3816 ((looking-at c-nonsymbol-token-regexp)
3817 (goto-char (match-end 0))
3818 t)
3819 ;; `c-nonsymbol-token-regexp' above should always
3820 ;; match if there are correct tokens. Try to
3821 ;; widen to see if the limit was set in the
3822 ;; middle of one, else fall back to treating
3823 ;; the offending thing as a one character token.
3824 ((and limit
3825 (save-restriction
3826 (widen)
3827 (looking-at c-nonsymbol-token-regexp)))
3828 nil)
3829 (t
3830 (forward-char)
3831 t))))
3832 (c-forward-syntactic-ws)
3833 (setq prev last
3834 count (1- count)))
3835 (error (goto-char last)))
3836
3837 (when (eobp)
3838 (goto-char prev)
3839 (setq count (1+ count)))))
3840
3841 count)))
3842
3843 (defun c-backward-token-2 (&optional count balanced limit)
3844 "Move backward by tokens.
3845 See `c-forward-token-2' for details."
3846
3847 (or count (setq count 1))
3848 (if (< count 0)
3849 (- (c-forward-token-2 (- count) balanced limit))
3850
3851 (or limit (setq limit (point-min)))
3852 (let ((jump-syntax (if balanced
3853 c-jump-syntax-balanced
3854 c-jump-syntax-unbalanced))
3855 (last (point)))
3856
3857 (if (zerop count)
3858 ;; The count is zero so try to skip to the beginning of the
3859 ;; current token.
3860 (if (> (point)
3861 (progn (c-beginning-of-current-token) (point)))
3862 (if (< (point) limit)
3863 ;; The limit is inside the same token, so return 1.
3864 (setq count 1))
3865
3866 ;; We're not in the middle of a token. If there's
3867 ;; whitespace after the point then we must move backward,
3868 ;; so set count to 1 in that case.
3869 (and (looking-at c-syntactic-ws-start)
3870 ;; If we're looking at a '#' that might start a cpp
3871 ;; directive then we have to do a more elaborate check.
3872 (or (/= (char-after) ?#)
3873 (not c-opt-cpp-prefix)
3874 (save-excursion
3875 (and (= (point)
3876 (progn (beginning-of-line)
3877 (looking-at "[ \t]*")
3878 (match-end 0)))
3879 (or (bobp)
3880 (progn (backward-char)
3881 (not (eq (char-before) ?\\)))))))
3882 (setq count 1))))
3883
3884 ;; Use `condition-case' to avoid having to check for buffer
3885 ;; limits in `backward-char', `scan-sexps' and `goto-char' below.
3886 (condition-case nil
3887 (while (and
3888 (> count 0)
3889 (progn
3890 (c-backward-syntactic-ws)
3891 (backward-char)
3892 (if (looking-at jump-syntax)
3893 (goto-char (scan-sexps (1+ (point)) -1))
3894 ;; This can be very inefficient if there's a long
3895 ;; sequence of operator tokens without any separation.
3896 ;; That doesn't happen in practice, anyway.
3897 (c-beginning-of-current-token))
3898 (>= (point) limit)))
3899 (setq last (point)
3900 count (1- count)))
3901 (error (goto-char last)))
3902
3903 (if (< (point) limit)
3904 (goto-char last))
3905
3906 count)))
3907
3908 (defun c-forward-token-1 (&optional count balanced limit)
3909 "Like `c-forward-token-2' but doesn't treat multicharacter operator
3910 tokens like \"==\" as single tokens, i.e. all sequences of symbol
3911 characters are jumped over character by character. This function is
3912 for compatibility only; it's only a wrapper over `c-forward-token-2'."
3913 (let ((c-nonsymbol-token-regexp "\\s.\\|\\s\(\\|\\s\)"))
3914 (c-forward-token-2 count balanced limit)))
3915
3916 (defun c-backward-token-1 (&optional count balanced limit)
3917 "Like `c-backward-token-2' but doesn't treat multicharacter operator
3918 tokens like \"==\" as single tokens, i.e. all sequences of symbol
3919 characters are jumped over character by character. This function is
3920 for compatibility only; it's only a wrapper over `c-backward-token-2'."
3921 (let ((c-nonsymbol-token-regexp "\\s.\\|\\s\(\\|\\s\)"))
3922 (c-backward-token-2 count balanced limit)))
3923
3924 \f
3925 ;; Tools for doing searches restricted to syntactically relevant text.
3926
3927 (defun c-syntactic-re-search-forward (regexp &optional bound noerror
3928 paren-level not-inside-token
3929 lookbehind-submatch)
3930 "Like `re-search-forward', but only report matches that are found
3931 in syntactically significant text. I.e. matches in comments, macros
3932 or string literals are ignored. The start point is assumed to be
3933 outside any comment, macro or string literal, or else the content of
3934 that region is taken as syntactically significant text.
3935
3936 If PAREN-LEVEL is non-nil, an additional restriction is added to
3937 ignore matches in nested paren sexps. The search will also not go
3938 outside the current list sexp, which has the effect that if the point
3939 should be moved to BOUND when no match is found \(i.e. NOERROR is
3940 neither nil nor t), then it will be at the closing paren if the end of
3941 the current list sexp is encountered first.
3942
3943 If NOT-INSIDE-TOKEN is non-nil, matches in the middle of tokens are
3944 ignored. Things like multicharacter operators and special symbols
3945 \(e.g. \"`()\" in Pike) are handled but currently not floating point
3946 constants.
3947
3948 If LOOKBEHIND-SUBMATCH is non-nil, it's taken as a number of a
3949 subexpression in REGEXP. The end of that submatch is used as the
3950 position to check for syntactic significance. If LOOKBEHIND-SUBMATCH
3951 isn't used or if that subexpression didn't match then the start
3952 position of the whole match is used instead. The \"look behind\"
3953 subexpression is never tested before the starting position, so it
3954 might be a good idea to include \\=\\= as a match alternative in it.
3955
3956 Optimization note: Matches might be missed if the \"look behind\"
3957 subexpression can match the end of nonwhite syntactic whitespace,
3958 i.e. the end of comments or cpp directives. This since the function
3959 skips over such things before resuming the search. It's on the other
3960 hand not safe to assume that the \"look behind\" subexpression never
3961 matches syntactic whitespace.
3962
3963 Bug: Unbalanced parens inside cpp directives are currently not handled
3964 correctly \(i.e. they don't get ignored as they should) when
3965 PAREN-LEVEL is set.
3966
3967 Note that this function might do hidden buffer changes. See the
3968 comment at the start of cc-engine.el for more info."
3969
3970 (or bound (setq bound (point-max)))
3971 (if paren-level (setq paren-level -1))
3972
3973 ;;(message "c-syntactic-re-search-forward %s %s %S" (point) bound regexp)
3974
3975 (let ((start (point))
3976 tmp
3977 ;; Start position for the last search.
3978 search-pos
3979 ;; The `parse-partial-sexp' state between the start position
3980 ;; and the point.
3981 state
3982 ;; The current position after the last state update. The next
3983 ;; `parse-partial-sexp' continues from here.
3984 (state-pos (point))
3985 ;; The position at which to check the state and the state
3986 ;; there. This is separate from `state-pos' since we might
3987 ;; need to back up before doing the next search round.
3988 check-pos check-state
3989 ;; Last position known to end a token.
3990 (last-token-end-pos (point-min))
3991 ;; Set when a valid match is found.
3992 found)
3993
3994 (condition-case err
3995 (while
3996 (and
3997 (progn
3998 (setq search-pos (point))
3999 (re-search-forward regexp bound noerror))
4000
4001 (progn
4002 (setq state (parse-partial-sexp
4003 state-pos (match-beginning 0) paren-level nil state)
4004 state-pos (point))
4005 (if (setq check-pos (and lookbehind-submatch
4006 (or (not paren-level)
4007 (>= (car state) 0))
4008 (match-end lookbehind-submatch)))
4009 (setq check-state (parse-partial-sexp
4010 state-pos check-pos paren-level nil state))
4011 (setq check-pos state-pos
4012 check-state state))
4013
4014 ;; NOTE: If we got a look behind subexpression and get
4015 ;; an insignificant match in something that isn't
4016 ;; syntactic whitespace (i.e. strings or in nested
4017 ;; parentheses), then we can never skip more than a
4018 ;; single character from the match start position
4019 ;; (i.e. `state-pos' here) before continuing the
4020 ;; search. That since the look behind subexpression
4021 ;; might match the end of the insignificant region in
4022 ;; the next search.
4023
4024 (cond
4025 ((elt check-state 7)
4026 ;; Match inside a line comment. Skip to eol. Use
4027 ;; `re-search-forward' instead of `skip-chars-forward' to get
4028 ;; the right bound behavior.
4029 (re-search-forward "[\n\r]" bound noerror))
4030
4031 ((elt check-state 4)
4032 ;; Match inside a block comment. Skip to the '*/'.
4033 (search-forward "*/" bound noerror))
4034
4035 ((and (not (elt check-state 5))
4036 (eq (char-before check-pos) ?/)
4037 (not (c-get-char-property (1- check-pos) 'syntax-table))
4038 (memq (char-after check-pos) '(?/ ?*)))
4039 ;; Match in the middle of the opener of a block or line
4040 ;; comment.
4041 (if (= (char-after check-pos) ?/)
4042 (re-search-forward "[\n\r]" bound noerror)
4043 (search-forward "*/" bound noerror)))
4044
4045 ;; The last `parse-partial-sexp' above might have
4046 ;; stopped short of the real check position if the end
4047 ;; of the current sexp was encountered in paren-level
4048 ;; mode. The checks above are always false in that
4049 ;; case, and since they can do better skipping in
4050 ;; lookbehind-submatch mode, we do them before
4051 ;; checking the paren level.
4052
4053 ((and paren-level
4054 (/= (setq tmp (car check-state)) 0))
4055 ;; Check the paren level first since we're short of the
4056 ;; syntactic checking position if the end of the
4057 ;; current sexp was encountered by `parse-partial-sexp'.
4058 (if (> tmp 0)
4059
4060 ;; Inside a nested paren sexp.
4061 (if lookbehind-submatch
4062 ;; See the NOTE above.
4063 (progn (goto-char state-pos) t)
4064 ;; Skip out of the paren quickly.
4065 (setq state (parse-partial-sexp state-pos bound 0 nil state)
4066 state-pos (point)))
4067
4068 ;; Have exited the current paren sexp.
4069 (if noerror
4070 (progn
4071 ;; The last `parse-partial-sexp' call above
4072 ;; has left us just after the closing paren
4073 ;; in this case, so we can modify the bound
4074 ;; to leave the point at the right position
4075 ;; upon return.
4076 (setq bound (1- (point)))
4077 nil)
4078 (signal 'search-failed (list regexp)))))
4079
4080 ((setq tmp (elt check-state 3))
4081 ;; Match inside a string.
4082 (if (or lookbehind-submatch
4083 (not (integerp tmp)))
4084 ;; See the NOTE above.
4085 (progn (goto-char state-pos) t)
4086 ;; Skip to the end of the string before continuing.
4087 (let ((ender (make-string 1 tmp)) (continue t))
4088 (while (if (search-forward ender bound noerror)
4089 (progn
4090 (setq state (parse-partial-sexp
4091 state-pos (point) nil nil state)
4092 state-pos (point))
4093 (elt state 3))
4094 (setq continue nil)))
4095 continue)))
4096
4097 ((save-excursion
4098 (save-match-data
4099 (c-beginning-of-macro start)))
4100 ;; Match inside a macro. Skip to the end of it.
4101 (c-end-of-macro)
4102 (cond ((<= (point) bound) t)
4103 (noerror nil)
4104 (t (signal 'search-failed (list regexp)))))
4105
4106 ((and not-inside-token
4107 (or (< check-pos last-token-end-pos)
4108 (< check-pos
4109 (save-excursion
4110 (goto-char check-pos)
4111 (save-match-data
4112 (c-end-of-current-token last-token-end-pos))
4113 (setq last-token-end-pos (point))))))
4114 ;; Inside a token.
4115 (if lookbehind-submatch
4116 ;; See the NOTE above.
4117 (goto-char state-pos)
4118 (goto-char (min last-token-end-pos bound))))
4119
4120 (t
4121 ;; A real match.
4122 (setq found t)
4123 nil)))
4124
4125 ;; Should loop to search again, but take care to avoid
4126 ;; looping on the same spot.
4127 (or (/= search-pos (point))
4128 (if (= (point) bound)
4129 (if noerror
4130 nil
4131 (signal 'search-failed (list regexp)))
4132 (forward-char)
4133 t))))
4134
4135 (error
4136 (goto-char start)
4137 (signal (car err) (cdr err))))
4138
4139 ;;(message "c-syntactic-re-search-forward done %s" (or (match-end 0) (point)))
4140
4141 (if found
4142 (progn
4143 (goto-char (match-end 0))
4144 (match-end 0))
4145
4146 ;; Search failed. Set point as appropriate.
4147 (if (eq noerror t)
4148 (goto-char start)
4149 (goto-char bound))
4150 nil)))
4151
4152 (defvar safe-pos-list) ; bound in c-syntactic-skip-backward
4153
4154 (defsubst c-ssb-lit-begin ()
4155 ;; Return the start of the literal point is in, or nil.
4156 ;; We read and write the variables `safe-pos', `safe-pos-list', `state'
4157 ;; bound in the caller.
4158
4159 ;; Use `parse-partial-sexp' from a safe position down to the point to check
4160 ;; if it's outside comments and strings.
4161 (save-excursion
4162 (let ((pos (point)) safe-pos state pps-end-pos)
4163 ;; Pick a safe position as close to the point as possible.
4164 ;;
4165 ;; FIXME: Consult `syntax-ppss' here if our cache doesn't give a good
4166 ;; position.
4167
4168 (while (and safe-pos-list
4169 (> (car safe-pos-list) (point)))
4170 (setq safe-pos-list (cdr safe-pos-list)))
4171 (unless (setq safe-pos (car-safe safe-pos-list))
4172 (setq safe-pos (max (or (c-safe-position
4173 (point) (or c-state-cache
4174 (c-parse-state)))
4175 0)
4176 (point-min))
4177 safe-pos-list (list safe-pos)))
4178
4179 ;; Cache positions along the way to use if we have to back up more. We
4180 ;; cache every closing paren on the same level. If the paren cache is
4181 ;; relevant in this region then we're typically already on the same
4182 ;; level as the target position. Note that we might cache positions
4183 ;; after opening parens in case safe-pos is in a nested list. That's
4184 ;; both uncommon and harmless.
4185 (while (progn
4186 (setq state (parse-partial-sexp
4187 safe-pos pos 0))
4188 (< (point) pos))
4189 (setq safe-pos (point)
4190 safe-pos-list (cons safe-pos safe-pos-list)))
4191
4192 ;; If the state contains the start of the containing sexp we cache that
4193 ;; position too, so that parse-partial-sexp in the next run has a bigger
4194 ;; chance of starting at the same level as the target position and thus
4195 ;; will get more good safe positions into the list.
4196 (if (elt state 1)
4197 (setq safe-pos (1+ (elt state 1))
4198 safe-pos-list (cons safe-pos safe-pos-list)))
4199
4200 (if (or (elt state 3) (elt state 4))
4201 ;; Inside string or comment. Continue search at the
4202 ;; beginning of it.
4203 (elt state 8)))))
4204
4205 (defun c-syntactic-skip-backward (skip-chars &optional limit paren-level)
4206 "Like `skip-chars-backward' but only look at syntactically relevant chars,
4207 i.e. don't stop at positions inside syntactic whitespace or string
4208 literals. Preprocessor directives are also ignored, with the exception
4209 of the one that the point starts within, if any. If LIMIT is given,
4210 it's assumed to be at a syntactically relevant position.
4211
4212 If PAREN-LEVEL is non-nil, the function won't stop in nested paren
4213 sexps, and the search will also not go outside the current paren sexp.
4214 However, if LIMIT or the buffer limit is reached inside a nested paren
4215 then the point will be left at the limit.
4216
4217 Non-nil is returned if the point moved, nil otherwise.
4218
4219 Note that this function might do hidden buffer changes. See the
4220 comment at the start of cc-engine.el for more info."
4221
4222 (let ((start (point))
4223 state-2
4224 ;; A list of syntactically relevant positions in descending
4225 ;; order. It's used to avoid scanning repeatedly over
4226 ;; potentially large regions with `parse-partial-sexp' to verify
4227 ;; each position. Used in `c-ssb-lit-begin'
4228 safe-pos-list
4229 ;; The result from `c-beginning-of-macro' at the start position or the
4230 ;; start position itself if it isn't within a macro. Evaluated on
4231 ;; demand.
4232 start-macro-beg
4233 ;; The earliest position after the current one with the same paren
4234 ;; level. Used only when `paren-level' is set.
4235 lit-beg
4236 (paren-level-pos (point)))
4237
4238 (while
4239 (progn
4240 ;; The next loop "tries" to find the end point each time round,
4241 ;; loops when it hasn't succeeded.
4242 (while
4243 (and
4244 (< (skip-chars-backward skip-chars limit) 0)
4245
4246 (let ((pos (point)) state-2 pps-end-pos)
4247
4248 (cond
4249 ;; Don't stop inside a literal
4250 ((setq lit-beg (c-ssb-lit-begin))
4251 (goto-char lit-beg)
4252 t)
4253
4254 ((and paren-level
4255 (save-excursion
4256 (setq state-2 (parse-partial-sexp
4257 pos paren-level-pos -1)
4258 pps-end-pos (point))
4259 (/= (car state-2) 0)))
4260 ;; Not at the right level.
4261
4262 (if (and (< (car state-2) 0)
4263 ;; We stop above if we go out of a paren.
4264 ;; Now check whether it precedes or is
4265 ;; nested in the starting sexp.
4266 (save-excursion
4267 (setq state-2
4268 (parse-partial-sexp
4269 pps-end-pos paren-level-pos
4270 nil nil state-2))
4271 (< (car state-2) 0)))
4272
4273 ;; We've stopped short of the starting position
4274 ;; so the hit was inside a nested list. Go up
4275 ;; until we are at the right level.
4276 (condition-case nil
4277 (progn
4278 (goto-char (scan-lists pos -1
4279 (- (car state-2))))
4280 (setq paren-level-pos (point))
4281 (if (and limit (>= limit paren-level-pos))
4282 (progn
4283 (goto-char limit)
4284 nil)
4285 t))
4286 (error
4287 (goto-char (or limit (point-min)))
4288 nil))
4289
4290 ;; The hit was outside the list at the start
4291 ;; position. Go to the start of the list and exit.
4292 (goto-char (1+ (elt state-2 1)))
4293 nil))
4294
4295 ((c-beginning-of-macro limit)
4296 ;; Inside a macro.
4297 (if (< (point)
4298 (or start-macro-beg
4299 (setq start-macro-beg
4300 (save-excursion
4301 (goto-char start)
4302 (c-beginning-of-macro limit)
4303 (point)))))
4304 t
4305
4306 ;; It's inside the same macro we started in so it's
4307 ;; a relevant match.
4308 (goto-char pos)
4309 nil))))))
4310
4311 (> (point)
4312 (progn
4313 ;; Skip syntactic ws afterwards so that we don't stop at the
4314 ;; end of a comment if `skip-chars' is something like "^/".
4315 (c-backward-syntactic-ws)
4316 (point)))))
4317
4318 ;; We might want to extend this with more useful return values in
4319 ;; the future.
4320 (/= (point) start)))
4321
4322 ;; The following is an alternative implementation of
4323 ;; `c-syntactic-skip-backward' that uses backward movement to keep
4324 ;; track of the syntactic context. It turned out to be generally
4325 ;; slower than the one above which uses forward checks from earlier
4326 ;; safe positions.
4327 ;;
4328 ;;(defconst c-ssb-stop-re
4329 ;; ;; The regexp matching chars `c-syntactic-skip-backward' needs to
4330 ;; ;; stop at to avoid going into comments and literals.
4331 ;; (concat
4332 ;; ;; Match comment end syntax and string literal syntax. Also match
4333 ;; ;; '/' for block comment endings (not covered by comment end
4334 ;; ;; syntax).
4335 ;; "\\s>\\|/\\|\\s\""
4336 ;; (if (memq 'gen-string-delim c-emacs-features)
4337 ;; "\\|\\s|"
4338 ;; "")
4339 ;; (if (memq 'gen-comment-delim c-emacs-features)
4340 ;; "\\|\\s!"
4341 ;; "")))
4342 ;;
4343 ;;(defconst c-ssb-stop-paren-re
4344 ;; ;; Like `c-ssb-stop-re' but also stops at paren chars.
4345 ;; (concat c-ssb-stop-re "\\|\\s(\\|\\s)"))
4346 ;;
4347 ;;(defconst c-ssb-sexp-end-re
4348 ;; ;; Regexp matching the ending syntax of a complex sexp.
4349 ;; (concat c-string-limit-regexp "\\|\\s)"))
4350 ;;
4351 ;;(defun c-syntactic-skip-backward (skip-chars &optional limit paren-level)
4352 ;; "Like `skip-chars-backward' but only look at syntactically relevant chars,
4353 ;;i.e. don't stop at positions inside syntactic whitespace or string
4354 ;;literals. Preprocessor directives are also ignored. However, if the
4355 ;;point is within a comment, string literal or preprocessor directory to
4356 ;;begin with, its contents is treated as syntactically relevant chars.
4357 ;;If LIMIT is given, it limits the backward search and the point will be
4358 ;;left there if no earlier position is found.
4359 ;;
4360 ;;If PAREN-LEVEL is non-nil, the function won't stop in nested paren
4361 ;;sexps, and the search will also not go outside the current paren sexp.
4362 ;;However, if LIMIT or the buffer limit is reached inside a nested paren
4363 ;;then the point will be left at the limit.
4364 ;;
4365 ;;Non-nil is returned if the point moved, nil otherwise.
4366 ;;
4367 ;;Note that this function might do hidden buffer changes. See the
4368 ;;comment at the start of cc-engine.el for more info."
4369 ;;
4370 ;; (save-restriction
4371 ;; (when limit
4372 ;; (narrow-to-region limit (point-max)))
4373 ;;
4374 ;; (let ((start (point)))
4375 ;; (catch 'done
4376 ;; (while (let ((last-pos (point))
4377 ;; (stop-pos (progn
4378 ;; (skip-chars-backward skip-chars)
4379 ;; (point))))
4380 ;;
4381 ;; ;; Skip back over the same region as
4382 ;; ;; `skip-chars-backward' above, but keep to
4383 ;; ;; syntactically relevant positions.
4384 ;; (goto-char last-pos)
4385 ;; (while (and
4386 ;; ;; `re-search-backward' with a single char regexp
4387 ;; ;; should be fast.
4388 ;; (re-search-backward
4389 ;; (if paren-level c-ssb-stop-paren-re c-ssb-stop-re)
4390 ;; stop-pos 'move)
4391 ;;
4392 ;; (progn
4393 ;; (cond
4394 ;; ((looking-at "\\s(")
4395 ;; ;; `paren-level' is set and we've found the
4396 ;; ;; start of the containing paren.
4397 ;; (forward-char)
4398 ;; (throw 'done t))
4399 ;;
4400 ;; ((looking-at c-ssb-sexp-end-re)
4401 ;; ;; We're at the end of a string literal or paren
4402 ;; ;; sexp (if `paren-level' is set).
4403 ;; (forward-char)
4404 ;; (condition-case nil
4405 ;; (c-backward-sexp)
4406 ;; (error
4407 ;; (goto-char limit)
4408 ;; (throw 'done t))))
4409 ;;
4410 ;; (t
4411 ;; (forward-char)
4412 ;; ;; At the end of some syntactic ws or possibly
4413 ;; ;; after a plain '/' operator.
4414 ;; (let ((pos (point)))
4415 ;; (c-backward-syntactic-ws)
4416 ;; (if (= pos (point))
4417 ;; ;; Was a plain '/' operator. Go past it.
4418 ;; (backward-char)))))
4419 ;;
4420 ;; (> (point) stop-pos))))
4421 ;;
4422 ;; ;; Now the point is either at `stop-pos' or at some
4423 ;; ;; position further back if `stop-pos' was at a
4424 ;; ;; syntactically irrelevant place.
4425 ;;
4426 ;; ;; Skip additional syntactic ws so that we don't stop
4427 ;; ;; at the end of a comment if `skip-chars' is
4428 ;; ;; something like "^/".
4429 ;; (c-backward-syntactic-ws)
4430 ;;
4431 ;; (< (point) stop-pos))))
4432 ;;
4433 ;; ;; We might want to extend this with more useful return values
4434 ;; ;; in the future.
4435 ;; (/= (point) start))))
4436
4437 \f
4438 ;; Tools for handling comments and string literals.
4439
4440 (defun c-in-literal (&optional lim detect-cpp)
4441 "Return the type of literal point is in, if any.
4442 The return value is `c' if in a C-style comment, `c++' if in a C++
4443 style comment, `string' if in a string literal, `pound' if DETECT-CPP
4444 is non-nil and in a preprocessor line, or nil if somewhere else.
4445 Optional LIM is used as the backward limit of the search. If omitted,
4446 or nil, `c-beginning-of-defun' is used.
4447
4448 The last point calculated is cached if the cache is enabled, i.e. if
4449 `c-in-literal-cache' is bound to a two element vector.
4450
4451 Note that this function might do hidden buffer changes. See the
4452 comment at the start of cc-engine.el for more info."
4453 (save-restriction
4454 (widen)
4455 (let* ((safe-place (c-state-semi-safe-place (point)))
4456 (lit (c-state-pp-to-literal safe-place (point))))
4457 (or (cadr lit)
4458 (and detect-cpp
4459 (save-excursion (c-beginning-of-macro))
4460 'pound)))))
4461
4462 (defun c-literal-limits (&optional lim near not-in-delimiter)
4463 "Return a cons of the beginning and end positions of the comment or
4464 string surrounding point (including both delimiters), or nil if point
4465 isn't in one. If LIM is non-nil, it's used as the \"safe\" position
4466 to start parsing from. If NEAR is non-nil, then the limits of any
4467 literal next to point is returned. \"Next to\" means there's only
4468 spaces and tabs between point and the literal. The search for such a
4469 literal is done first in forward direction. If NOT-IN-DELIMITER is
4470 non-nil, the case when point is inside a starting delimiter won't be
4471 recognized. This only has effect for comments which have starting
4472 delimiters with more than one character.
4473
4474 Note that this function might do hidden buffer changes. See the
4475 comment at the start of cc-engine.el for more info."
4476
4477 (save-excursion
4478 (let* ((pos (point))
4479 (lim (or lim (c-state-semi-safe-place pos)))
4480 (pp-to-lit (save-restriction
4481 (widen)
4482 (c-state-pp-to-literal lim pos)))
4483 (state (car pp-to-lit))
4484 (lit-limits (car (cddr pp-to-lit))))
4485
4486 (cond
4487 (lit-limits)
4488 ((and (not not-in-delimiter)
4489 (not (elt state 5))
4490 (eq (char-before) ?/)
4491 (looking-at "[/*]")) ; FIXME!!! use c-line/block-comment-starter. 2008-09-28.
4492 ;; We're standing in a comment starter.
4493 (backward-char 1)
4494 (cons (point) (progn (c-forward-single-comment) (point))))
4495
4496 (near
4497 (goto-char pos)
4498 ;; Search forward for a literal.
4499 (skip-chars-forward " \t")
4500 (cond
4501 ((looking-at c-string-limit-regexp) ; String.
4502 (cons (point) (or (c-safe (c-forward-sexp 1) (point))
4503 (point-max))))
4504
4505 ((looking-at c-comment-start-regexp) ; Line or block comment.
4506 (cons (point) (progn (c-forward-single-comment) (point))))
4507
4508 (t
4509 ;; Search backward.
4510 (skip-chars-backward " \t")
4511
4512 (let ((end (point)) beg)
4513 (cond
4514 ((save-excursion
4515 (< (skip-syntax-backward c-string-syntax) 0)) ; String.
4516 (setq beg (c-safe (c-backward-sexp 1) (point))))
4517
4518 ((and (c-safe (forward-char -2) t)
4519 (looking-at "*/"))
4520 ;; Block comment. Due to the nature of line
4521 ;; comments, they will always be covered by the
4522 ;; normal case above.
4523 (goto-char end)
4524 (c-backward-single-comment)
4525 ;; If LIM is bogus, beg will be bogus.
4526 (setq beg (point))))
4527
4528 (if beg (cons beg end))))))
4529 ))))
4530
4531 ;; In case external callers use this; it did have a docstring.
4532 (defalias 'c-literal-limits-fast 'c-literal-limits)
4533
4534 (defun c-collect-line-comments (range)
4535 "If the argument is a cons of two buffer positions (such as returned by
4536 `c-literal-limits'), and that range contains a C++ style line comment,
4537 then an extended range is returned that contains all adjacent line
4538 comments (i.e. all comments that starts in the same column with no
4539 empty lines or non-whitespace characters between them). Otherwise the
4540 argument is returned.
4541
4542 Note that this function might do hidden buffer changes. See the
4543 comment at the start of cc-engine.el for more info."
4544
4545 (save-excursion
4546 (condition-case nil
4547 (if (and (consp range) (progn
4548 (goto-char (car range))
4549 (looking-at c-line-comment-starter)))
4550 (let ((col (current-column))
4551 (beg (point))
4552 (bopl (c-point 'bopl))
4553 (end (cdr range)))
4554 ;; Got to take care in the backward direction to handle
4555 ;; comments which are preceded by code.
4556 (while (and (c-backward-single-comment)
4557 (>= (point) bopl)
4558 (looking-at c-line-comment-starter)
4559 (= col (current-column)))
4560 (setq beg (point)
4561 bopl (c-point 'bopl)))
4562 (goto-char end)
4563 (while (and (progn (skip-chars-forward " \t")
4564 (looking-at c-line-comment-starter))
4565 (= col (current-column))
4566 (prog1 (zerop (forward-line 1))
4567 (setq end (point)))))
4568 (cons beg end))
4569 range)
4570 (error range))))
4571
4572 (defun c-literal-type (range)
4573 "Convenience function that given the result of `c-literal-limits',
4574 returns nil or the type of literal that the range surrounds, one
4575 of the symbols 'c, 'c++ or 'string. It's much faster than using
4576 `c-in-literal' and is intended to be used when you need both the
4577 type of a literal and its limits.
4578
4579 Note that this function might do hidden buffer changes. See the
4580 comment at the start of cc-engine.el for more info."
4581
4582 (if (consp range)
4583 (save-excursion
4584 (goto-char (car range))
4585 (cond ((looking-at c-string-limit-regexp) 'string)
4586 ((or (looking-at "//") ; c++ line comment
4587 (and (looking-at "\\s<") ; comment starter
4588 (looking-at "#"))) ; awk comment.
4589 'c++)
4590 (t 'c))) ; Assuming the range is valid.
4591 range))
4592
4593 (defsubst c-determine-limit-get-base (start try-size)
4594 ;; Get a "safe place" approximately TRY-SIZE characters before START.
4595 ;; This doesn't preserve point.
4596 (let* ((pos (max (- start try-size) (point-min)))
4597 (base (c-state-semi-safe-place pos))
4598 (s (parse-partial-sexp base pos)))
4599 (if (or (nth 4 s) (nth 3 s)) ; comment or string
4600 (nth 8 s)
4601 (point))))
4602
4603 (defun c-determine-limit (how-far-back &optional start try-size)
4604 ;; Return a buffer position HOW-FAR-BACK non-literal characters from START
4605 ;; (default point). This is done by going back further in the buffer then
4606 ;; searching forward for literals. The position found won't be in a
4607 ;; literal. We start searching for the sought position TRY-SIZE (default
4608 ;; twice HOW-FAR-BACK) bytes back from START. This function must be fast.
4609 ;; :-)
4610 (save-excursion
4611 (let* ((start (or start (point)))
4612 (try-size (or try-size (* 2 how-far-back)))
4613 (base (c-determine-limit-get-base start try-size))
4614 (pos base)
4615
4616 (s (parse-partial-sexp pos pos)) ; null state.
4617 stack elt size
4618 (count 0))
4619 (while (< pos start)
4620 ;; Move forward one literal each time round this loop.
4621 ;; Move forward to the start of a comment or string.
4622 (setq s (parse-partial-sexp
4623 pos
4624 start
4625 nil ; target-depth
4626 nil ; stop-before
4627 s ; state
4628 'syntax-table)) ; stop-comment
4629
4630 ;; Gather details of the non-literal-bit - starting pos and size.
4631 (setq size (- (if (or (nth 4 s) (nth 3 s))
4632 (nth 8 s)
4633 (point))
4634 pos))
4635 (if (> size 0)
4636 (setq stack (cons (cons pos size) stack)))
4637
4638 ;; Move forward to the end of the comment/string.
4639 (if (or (nth 4 s) (nth 3 s))
4640 (setq s (parse-partial-sexp
4641 (point)
4642 start
4643 nil ; target-depth
4644 nil ; stop-before
4645 s ; state
4646 'syntax-table))) ; stop-comment
4647 (setq pos (point)))
4648
4649 ;; Now try and find enough non-literal characters recorded on the stack.
4650 ;; Go back one recorded literal each time round this loop.
4651 (while (and (< count how-far-back)
4652 stack)
4653 (setq elt (car stack)
4654 stack (cdr stack))
4655 (setq count (+ count (cdr elt))))
4656
4657 ;; Have we found enough yet?
4658 (cond
4659 ((>= count how-far-back)
4660 (+ (car elt) (- count how-far-back)))
4661 ((eq base (point-min))
4662 (point-min))
4663 (t
4664 (c-determine-limit (- how-far-back count) base try-size))))))
4665
4666 (defun c-determine-+ve-limit (how-far &optional start-pos)
4667 ;; Return a buffer position about HOW-FAR non-literal characters forward
4668 ;; from START-POS (default point), which must not be inside a literal.
4669 (save-excursion
4670 (let ((pos (or start-pos (point)))
4671 (count how-far)
4672 (s (parse-partial-sexp (point) (point)))) ; null state
4673 (while (and (not (eobp))
4674 (> count 0))
4675 ;; Scan over counted characters.
4676 (setq s (parse-partial-sexp
4677 pos
4678 (min (+ pos count) (point-max))
4679 nil ; target-depth
4680 nil ; stop-before
4681 s ; state
4682 'syntax-table)) ; stop-comment
4683 (setq count (- count (- (point) pos) 1)
4684 pos (point))
4685 ;; Scan over literal characters.
4686 (if (nth 8 s)
4687 (setq s (parse-partial-sexp
4688 pos
4689 (point-max)
4690 nil ; target-depth
4691 nil ; stop-before
4692 s ; state
4693 'syntax-table) ; stop-comment
4694 pos (point))))
4695 (point))))
4696
4697 \f
4698 ;; `c-find-decl-spots' and accompanying stuff.
4699
4700 ;; Variables used in `c-find-decl-spots' to cache the search done for
4701 ;; the first declaration in the last call. When that function starts,
4702 ;; it needs to back up over syntactic whitespace to look at the last
4703 ;; token before the region being searched. That can sometimes cause
4704 ;; moves back and forth over a quite large region of comments and
4705 ;; macros, which would be repeated for each changed character when
4706 ;; we're called during fontification, since font-lock refontifies the
4707 ;; current line for each change. Thus it's worthwhile to cache the
4708 ;; first match.
4709 ;;
4710 ;; `c-find-decl-syntactic-pos' is a syntactically relevant position in
4711 ;; the syntactic whitespace less or equal to some start position.
4712 ;; There's no cached value if it's nil.
4713 ;;
4714 ;; `c-find-decl-match-pos' is the match position if
4715 ;; `c-find-decl-prefix-search' matched before the syntactic whitespace
4716 ;; at `c-find-decl-syntactic-pos', or nil if there's no such match.
4717 (defvar c-find-decl-syntactic-pos nil)
4718 (make-variable-buffer-local 'c-find-decl-syntactic-pos)
4719 (defvar c-find-decl-match-pos nil)
4720 (make-variable-buffer-local 'c-find-decl-match-pos)
4721
4722 (defsubst c-invalidate-find-decl-cache (change-min-pos)
4723 (and c-find-decl-syntactic-pos
4724 (< change-min-pos c-find-decl-syntactic-pos)
4725 (setq c-find-decl-syntactic-pos nil)))
4726
4727 ; (defface c-debug-decl-spot-face
4728 ; '((t (:background "Turquoise")))
4729 ; "Debug face to mark the spots where `c-find-decl-spots' stopped.")
4730 ; (defface c-debug-decl-sws-face
4731 ; '((t (:background "Khaki")))
4732 ; "Debug face to mark the syntactic whitespace between the declaration
4733 ; spots and the preceding token end.")
4734
4735 (defmacro c-debug-put-decl-spot-faces (match-pos decl-pos)
4736 (when (facep 'c-debug-decl-spot-face)
4737 `(c-save-buffer-state ((match-pos ,match-pos) (decl-pos ,decl-pos))
4738 (c-debug-add-face (max match-pos (point-min)) decl-pos
4739 'c-debug-decl-sws-face)
4740 (c-debug-add-face decl-pos (min (1+ decl-pos) (point-max))
4741 'c-debug-decl-spot-face))))
4742 (defmacro c-debug-remove-decl-spot-faces (beg end)
4743 (when (facep 'c-debug-decl-spot-face)
4744 `(c-save-buffer-state ()
4745 (c-debug-remove-face ,beg ,end 'c-debug-decl-spot-face)
4746 (c-debug-remove-face ,beg ,end 'c-debug-decl-sws-face))))
4747
4748 (defmacro c-find-decl-prefix-search ()
4749 ;; Macro used inside `c-find-decl-spots'. It ought to be a defun,
4750 ;; but it contains lots of free variables that refer to things
4751 ;; inside `c-find-decl-spots'. The point is left at `cfd-match-pos'
4752 ;; if there is a match, otherwise at `cfd-limit'.
4753 ;;
4754 ;; This macro might do hidden buffer changes.
4755
4756 '(progn
4757 ;; Find the next property match position if we haven't got one already.
4758 (unless cfd-prop-match
4759 (save-excursion
4760 (while (progn
4761 (goto-char (next-single-property-change
4762 (point) 'c-type nil cfd-limit))
4763 (and (< (point) cfd-limit)
4764 (not (eq (c-get-char-property (1- (point)) 'c-type)
4765 'c-decl-end)))))
4766 (setq cfd-prop-match (point))))
4767
4768 ;; Find the next `c-decl-prefix-or-start-re' match if we haven't
4769 ;; got one already.
4770 (unless cfd-re-match
4771
4772 (if (> cfd-re-match-end (point))
4773 (goto-char cfd-re-match-end))
4774
4775 (while (if (setq cfd-re-match-end
4776 (re-search-forward c-decl-prefix-or-start-re
4777 cfd-limit 'move))
4778
4779 ;; Match. Check if it's inside a comment or string literal.
4780 (c-got-face-at
4781 (if (setq cfd-re-match (match-end 1))
4782 ;; Matched the end of a token preceding a decl spot.
4783 (progn
4784 (goto-char cfd-re-match)
4785 (1- cfd-re-match))
4786 ;; Matched a token that start a decl spot.
4787 (goto-char (match-beginning 0))
4788 (point))
4789 c-literal-faces)
4790
4791 ;; No match. Finish up and exit the loop.
4792 (setq cfd-re-match cfd-limit)
4793 nil)
4794
4795 ;; Skip out of comments and string literals.
4796 (while (progn
4797 (goto-char (next-single-property-change
4798 (point) 'face nil cfd-limit))
4799 (and (< (point) cfd-limit)
4800 (c-got-face-at (point) c-literal-faces)))))
4801
4802 ;; If we matched at the decl start, we have to back up over the
4803 ;; preceding syntactic ws to set `cfd-match-pos' and to catch
4804 ;; any decl spots in the syntactic ws.
4805 (unless cfd-re-match
4806 (c-backward-syntactic-ws)
4807 (setq cfd-re-match (point))))
4808
4809 ;; Choose whichever match is closer to the start.
4810 (if (< cfd-re-match cfd-prop-match)
4811 (setq cfd-match-pos cfd-re-match
4812 cfd-re-match nil)
4813 (setq cfd-match-pos cfd-prop-match
4814 cfd-prop-match nil))
4815
4816 (goto-char cfd-match-pos)
4817
4818 (when (< cfd-match-pos cfd-limit)
4819 ;; Skip forward past comments only so we don't skip macros.
4820 (c-forward-comments)
4821 ;; Set the position to continue at. We can avoid going over
4822 ;; the comments skipped above a second time, but it's possible
4823 ;; that the comment skipping has taken us past `cfd-prop-match'
4824 ;; since the property might be used inside comments.
4825 (setq cfd-continue-pos (if cfd-prop-match
4826 (min cfd-prop-match (point))
4827 (point))))))
4828
4829 (defun c-find-decl-spots (cfd-limit cfd-decl-re cfd-face-checklist cfd-fun)
4830 ;; Call CFD-FUN for each possible spot for a declaration, cast or
4831 ;; label from the point to CFD-LIMIT.
4832 ;;
4833 ;; CFD-FUN is called with point at the start of the spot. It's passed two
4834 ;; arguments: The first is the end position of the token preceding the spot,
4835 ;; or 0 for the implicit match at bob. The second is a flag that is t when
4836 ;; the match is inside a macro. Point should be moved forward by at least
4837 ;; one token.
4838 ;;
4839 ;; If CFD-FUN adds `c-decl-end' properties somewhere below the current spot,
4840 ;; it should return non-nil to ensure that the next search will find them.
4841 ;;
4842 ;; Such a spot is:
4843 ;; o The first token after bob.
4844 ;; o The first token after the end of submatch 1 in
4845 ;; `c-decl-prefix-or-start-re' when that submatch matches.
4846 ;; o The start of each `c-decl-prefix-or-start-re' match when
4847 ;; submatch 1 doesn't match.
4848 ;; o The first token after the end of each occurrence of the
4849 ;; `c-type' text property with the value `c-decl-end', provided
4850 ;; `c-type-decl-end-used' is set.
4851 ;;
4852 ;; Only a spot that match CFD-DECL-RE and whose face is in the
4853 ;; CFD-FACE-CHECKLIST list causes CFD-FUN to be called. The face
4854 ;; check is disabled if CFD-FACE-CHECKLIST is nil.
4855 ;;
4856 ;; If the match is inside a macro then the buffer is narrowed to the
4857 ;; end of it, so that CFD-FUN can investigate the following tokens
4858 ;; without matching something that begins inside a macro and ends
4859 ;; outside it. It's to avoid this work that the CFD-DECL-RE and
4860 ;; CFD-FACE-CHECKLIST checks exist.
4861 ;;
4862 ;; The spots are visited approximately in order from top to bottom.
4863 ;; It's however the positions where `c-decl-prefix-or-start-re'
4864 ;; matches and where `c-decl-end' properties are found that are in
4865 ;; order. Since the spots often are at the following token, they
4866 ;; might be visited out of order insofar as more spots are reported
4867 ;; later on within the syntactic whitespace between the match
4868 ;; positions and their spots.
4869 ;;
4870 ;; It's assumed that comments and strings are fontified in the
4871 ;; searched range.
4872 ;;
4873 ;; This is mainly used in fontification, and so has an elaborate
4874 ;; cache to handle repeated calls from the same start position; see
4875 ;; the variables above.
4876 ;;
4877 ;; All variables in this function begin with `cfd-' to avoid name
4878 ;; collision with the (dynamically bound) variables used in CFD-FUN.
4879 ;;
4880 ;; This function might do hidden buffer changes.
4881
4882 (let ((cfd-start-pos (point))
4883 (cfd-buffer-end (point-max))
4884 ;; The end of the token preceding the decl spot last found
4885 ;; with `c-decl-prefix-or-start-re'. `cfd-limit' if there's
4886 ;; no match.
4887 cfd-re-match
4888 ;; The end position of the last `c-decl-prefix-or-start-re'
4889 ;; match. If this is greater than `cfd-continue-pos', the
4890 ;; next regexp search is started here instead.
4891 (cfd-re-match-end (point-min))
4892 ;; The end of the last `c-decl-end' found by
4893 ;; `c-find-decl-prefix-search'. `cfd-limit' if there's no
4894 ;; match. If searching for the property isn't needed then we
4895 ;; disable it by setting it to `cfd-limit' directly.
4896 (cfd-prop-match (unless c-type-decl-end-used cfd-limit))
4897 ;; The end of the token preceding the decl spot last found by
4898 ;; `c-find-decl-prefix-search'. 0 for the implicit match at
4899 ;; bob. `cfd-limit' if there's no match. In other words,
4900 ;; this is the minimum of `cfd-re-match' and `cfd-prop-match'.
4901 (cfd-match-pos cfd-limit)
4902 ;; The position to continue searching at.
4903 cfd-continue-pos
4904 ;; The position of the last "real" token we've stopped at.
4905 ;; This can be greater than `cfd-continue-pos' when we get
4906 ;; hits inside macros or at `c-decl-end' positions inside
4907 ;; comments.
4908 (cfd-token-pos 0)
4909 ;; The end position of the last entered macro.
4910 (cfd-macro-end 0))
4911
4912 ;; Initialize by finding a syntactically relevant start position
4913 ;; before the point, and do the first `c-decl-prefix-or-start-re'
4914 ;; search unless we're at bob.
4915
4916 (let (start-in-literal start-in-macro syntactic-pos)
4917 ;; Must back up a bit since we look for the end of the previous
4918 ;; statement or declaration, which is earlier than the first
4919 ;; returned match.
4920
4921 (cond
4922 ;; First we need to move to a syntactically relevant position.
4923 ;; Begin by backing out of comment or string literals.
4924 ((and
4925 (when (c-got-face-at (point) c-literal-faces)
4926 ;; Try to use the faces to back up to the start of the
4927 ;; literal. FIXME: What if the point is on a declaration
4928 ;; inside a comment?
4929 (while (and (not (bobp))
4930 (c-got-face-at (1- (point)) c-literal-faces))
4931 (goto-char (previous-single-property-change
4932 (point) 'face nil (point-min))))
4933
4934 ;; XEmacs doesn't fontify the quotes surrounding string
4935 ;; literals.
4936 (and (featurep 'xemacs)
4937 (eq (get-text-property (point) 'face)
4938 'font-lock-string-face)
4939 (not (bobp))
4940 (progn (backward-char)
4941 (not (looking-at c-string-limit-regexp)))
4942 (forward-char))
4943
4944 ;; Don't trust the literal to contain only literal faces
4945 ;; (the font lock package might not have fontified the
4946 ;; start of it at all, for instance) so check that we have
4947 ;; arrived at something that looks like a start or else
4948 ;; resort to `c-literal-limits'.
4949 (unless (looking-at c-literal-start-regexp)
4950 (let ((range (c-literal-limits)))
4951 (if range (goto-char (car range)))))
4952
4953 (setq start-in-literal (point)))
4954
4955 ;; The start is in a literal. If the limit is in the same
4956 ;; one we don't have to find a syntactic position etc. We
4957 ;; only check that if the limit is at or before bonl to save
4958 ;; time; it covers the by far most common case when font-lock
4959 ;; refontifies the current line only.
4960 (<= cfd-limit (c-point 'bonl cfd-start-pos))
4961 (save-excursion
4962 (goto-char cfd-start-pos)
4963 (while (progn
4964 (goto-char (next-single-property-change
4965 (point) 'face nil cfd-limit))
4966 (and (< (point) cfd-limit)
4967 (c-got-face-at (point) c-literal-faces))))
4968 (= (point) cfd-limit)))
4969
4970 ;; Completely inside a literal. Set up variables to trig the
4971 ;; (< cfd-continue-pos cfd-start-pos) case below and it'll
4972 ;; find a suitable start position.
4973 (setq cfd-continue-pos start-in-literal))
4974
4975 ;; Check if the region might be completely inside a macro, to
4976 ;; optimize that like the completely-inside-literal above.
4977 ((save-excursion
4978 (and (= (forward-line 1) 0)
4979 (bolp) ; forward-line has funny behavior at eob.
4980 (>= (point) cfd-limit)
4981 (progn (backward-char)
4982 (eq (char-before) ?\\))))
4983 ;; (Maybe) completely inside a macro. Only need to trig the
4984 ;; (< cfd-continue-pos cfd-start-pos) case below to make it
4985 ;; set things up.
4986 (setq cfd-continue-pos (1- cfd-start-pos)
4987 start-in-macro t))
4988
4989 (t
4990 ;; Back out of any macro so we don't miss any declaration
4991 ;; that could follow after it.
4992 (when (c-beginning-of-macro)
4993 (setq start-in-macro t))
4994
4995 ;; Now we're at a proper syntactically relevant position so we
4996 ;; can use the cache. But first clear it if it applied
4997 ;; further down.
4998 (c-invalidate-find-decl-cache cfd-start-pos)
4999
5000 (setq syntactic-pos (point))
5001 (unless (eq syntactic-pos c-find-decl-syntactic-pos)
5002 ;; Don't have to do this if the cache is relevant here,
5003 ;; typically if the same line is refontified again. If
5004 ;; we're just some syntactic whitespace further down we can
5005 ;; still use the cache to limit the skipping.
5006 (c-backward-syntactic-ws c-find-decl-syntactic-pos))
5007
5008 ;; If we hit `c-find-decl-syntactic-pos' and
5009 ;; `c-find-decl-match-pos' is set then we install the cached
5010 ;; values. If we hit `c-find-decl-syntactic-pos' and
5011 ;; `c-find-decl-match-pos' is nil then we know there's no decl
5012 ;; prefix in the whitespace before `c-find-decl-syntactic-pos'
5013 ;; and so we can continue the search from this point. If we
5014 ;; didn't hit `c-find-decl-syntactic-pos' then we're now in
5015 ;; the right spot to begin searching anyway.
5016 (if (and (eq (point) c-find-decl-syntactic-pos)
5017 c-find-decl-match-pos)
5018 (setq cfd-match-pos c-find-decl-match-pos
5019 cfd-continue-pos syntactic-pos)
5020
5021 (setq c-find-decl-syntactic-pos syntactic-pos)
5022
5023 (when (if (bobp)
5024 ;; Always consider bob a match to get the first
5025 ;; declaration in the file. Do this separately instead of
5026 ;; letting `c-decl-prefix-or-start-re' match bob, so that
5027 ;; regexp always can consume at least one character to
5028 ;; ensure that we won't get stuck in an infinite loop.
5029 (setq cfd-re-match 0)
5030 (backward-char)
5031 (c-beginning-of-current-token)
5032 (< (point) cfd-limit))
5033 ;; Do an initial search now. In the bob case above it's
5034 ;; only done to search for a `c-decl-end' spot.
5035 (c-find-decl-prefix-search))
5036
5037 (setq c-find-decl-match-pos (and (< cfd-match-pos cfd-start-pos)
5038 cfd-match-pos)))))
5039
5040 ;; Advance `cfd-continue-pos' if it's before the start position.
5041 ;; The closest continue position that might have effect at or
5042 ;; after the start depends on what we started in. This also
5043 ;; finds a suitable start position in the special cases when the
5044 ;; region is completely within a literal or macro.
5045 (when (and cfd-continue-pos (< cfd-continue-pos cfd-start-pos))
5046
5047 (cond
5048 (start-in-macro
5049 ;; If we're in a macro then it's the closest preceding token
5050 ;; in the macro. Check this before `start-in-literal',
5051 ;; since if we're inside a literal in a macro, the preceding
5052 ;; token is earlier than any `c-decl-end' spot inside the
5053 ;; literal (comment).
5054 (goto-char (or start-in-literal cfd-start-pos))
5055 ;; The only syntactic ws in macros are comments.
5056 (c-backward-comments)
5057 (backward-char)
5058 (c-beginning-of-current-token))
5059
5060 (start-in-literal
5061 ;; If we're in a comment it can only be the closest
5062 ;; preceding `c-decl-end' position within that comment, if
5063 ;; any. Go back to the beginning of such a property so that
5064 ;; `c-find-decl-prefix-search' will find the end of it.
5065 ;; (Can't stop at the end and install it directly on
5066 ;; `cfd-prop-match' since that variable might be cleared
5067 ;; after `cfd-fun' below.)
5068 ;;
5069 ;; Note that if the literal is a string then the property
5070 ;; search will simply skip to the beginning of it right
5071 ;; away.
5072 (if (not c-type-decl-end-used)
5073 (goto-char start-in-literal)
5074 (goto-char cfd-start-pos)
5075 (while (progn
5076 (goto-char (previous-single-property-change
5077 (point) 'c-type nil start-in-literal))
5078 (and (> (point) start-in-literal)
5079 (not (eq (c-get-char-property (point) 'c-type)
5080 'c-decl-end))))))
5081
5082 (when (= (point) start-in-literal)
5083 ;; Didn't find any property inside the comment, so we can
5084 ;; skip it entirely. (This won't skip past a string, but
5085 ;; that'll be handled quickly by the next
5086 ;; `c-find-decl-prefix-search' anyway.)
5087 (c-forward-single-comment)
5088 (if (> (point) cfd-limit)
5089 (goto-char cfd-limit))))
5090
5091 (t
5092 ;; If we started in normal code, the only match that might
5093 ;; apply before the start is what we already got in
5094 ;; `cfd-match-pos' so we can continue at the start position.
5095 ;; (Note that we don't get here if the first match is below
5096 ;; it.)
5097 (goto-char cfd-start-pos)))
5098
5099 ;; Delete found matches if they are before our new continue
5100 ;; position, so that `c-find-decl-prefix-search' won't back up
5101 ;; to them later on.
5102 (setq cfd-continue-pos (point))
5103 (when (and cfd-re-match (< cfd-re-match cfd-continue-pos))
5104 (setq cfd-re-match nil))
5105 (when (and cfd-prop-match (< cfd-prop-match cfd-continue-pos))
5106 (setq cfd-prop-match nil)))
5107
5108 (if syntactic-pos
5109 ;; This is the normal case and we got a proper syntactic
5110 ;; position. If there's a match then it's always outside
5111 ;; macros and comments, so advance to the next token and set
5112 ;; `cfd-token-pos'. The loop below will later go back using
5113 ;; `cfd-continue-pos' to fix declarations inside the
5114 ;; syntactic ws.
5115 (when (and cfd-match-pos (< cfd-match-pos syntactic-pos))
5116 (goto-char syntactic-pos)
5117 (c-forward-syntactic-ws)
5118 (and cfd-continue-pos
5119 (< cfd-continue-pos (point))
5120 (setq cfd-token-pos (point))))
5121
5122 ;; Have one of the special cases when the region is completely
5123 ;; within a literal or macro. `cfd-continue-pos' is set to a
5124 ;; good start position for the search, so do it.
5125 (c-find-decl-prefix-search)))
5126
5127 ;; Now loop. Round what? (ACM, 2006/7/5). We already got the first match.
5128
5129 (while (progn
5130 (while (and
5131 (< cfd-match-pos cfd-limit)
5132
5133 (or
5134 ;; Kludge to filter out matches on the "<" that
5135 ;; aren't open parens, for the sake of languages
5136 ;; that got `c-recognize-<>-arglists' set.
5137 (and (eq (char-before cfd-match-pos) ?<)
5138 (not (c-get-char-property (1- cfd-match-pos)
5139 'syntax-table)))
5140
5141 ;; If `cfd-continue-pos' is less or equal to
5142 ;; `cfd-token-pos', we've got a hit inside a macro
5143 ;; that's in the syntactic whitespace before the last
5144 ;; "real" declaration we've checked. If they're equal
5145 ;; we've arrived at the declaration a second time, so
5146 ;; there's nothing to do.
5147 (= cfd-continue-pos cfd-token-pos)
5148
5149 (progn
5150 ;; If `cfd-continue-pos' is less than `cfd-token-pos'
5151 ;; we're still searching for declarations embedded in
5152 ;; the syntactic whitespace. In that case we need
5153 ;; only to skip comments and not macros, since they
5154 ;; can't be nested, and that's already been done in
5155 ;; `c-find-decl-prefix-search'.
5156 (when (> cfd-continue-pos cfd-token-pos)
5157 (c-forward-syntactic-ws)
5158 (setq cfd-token-pos (point)))
5159
5160 ;; Continue if the following token fails the
5161 ;; CFD-DECL-RE and CFD-FACE-CHECKLIST checks.
5162 (when (or (>= (point) cfd-limit)
5163 (not (looking-at cfd-decl-re))
5164 (and cfd-face-checklist
5165 (not (c-got-face-at
5166 (point) cfd-face-checklist))))
5167 (goto-char cfd-continue-pos)
5168 t)))
5169
5170 (< (point) cfd-limit))
5171 (c-find-decl-prefix-search))
5172
5173 (< (point) cfd-limit))
5174
5175 (when (and
5176 (>= (point) cfd-start-pos)
5177
5178 (progn
5179 ;; Narrow to the end of the macro if we got a hit inside
5180 ;; one, to avoid recognizing things that start inside the
5181 ;; macro and end outside it.
5182 (when (> cfd-match-pos cfd-macro-end)
5183 ;; Not in the same macro as in the previous round.
5184 (save-excursion
5185 (goto-char cfd-match-pos)
5186 (setq cfd-macro-end
5187 (if (save-excursion (and (c-beginning-of-macro)
5188 (< (point) cfd-match-pos)))
5189 (progn (c-end-of-macro)
5190 (point))
5191 0))))
5192
5193 (if (zerop cfd-macro-end)
5194 t
5195 (if (> cfd-macro-end (point))
5196 (progn (narrow-to-region (point-min) cfd-macro-end)
5197 t)
5198 ;; The matched token was the last thing in the macro,
5199 ;; so the whole match is bogus.
5200 (setq cfd-macro-end 0)
5201 nil))))
5202
5203 (c-debug-put-decl-spot-faces cfd-match-pos (point))
5204 (if (funcall cfd-fun cfd-match-pos (/= cfd-macro-end 0))
5205 (setq cfd-prop-match nil))
5206
5207 (when (/= cfd-macro-end 0)
5208 ;; Restore limits if we did macro narrowing above.
5209 (narrow-to-region (point-min) cfd-buffer-end)))
5210
5211 (goto-char cfd-continue-pos)
5212 (if (= cfd-continue-pos cfd-limit)
5213 (setq cfd-match-pos cfd-limit)
5214 (c-find-decl-prefix-search))))) ; Moves point, sets cfd-continue-pos,
5215 ; cfd-match-pos, etc.
5216
5217 \f
5218 ;; A cache for found types.
5219
5220 ;; Buffer local variable that contains an obarray with the types we've
5221 ;; found. If a declaration is recognized somewhere we record the
5222 ;; fully qualified identifier in it to recognize it as a type
5223 ;; elsewhere in the file too. This is not accurate since we do not
5224 ;; bother with the scoping rules of the languages, but in practice the
5225 ;; same name is seldom used as both a type and something else in a
5226 ;; file, and we only use this as a last resort in ambiguous cases (see
5227 ;; `c-forward-decl-or-cast-1').
5228 ;;
5229 ;; Not every type need be in this cache. However, things which have
5230 ;; ceased to be types must be removed from it.
5231 ;;
5232 ;; Template types in C++ are added here too but with the template
5233 ;; arglist replaced with "<>" in references or "<" for the one in the
5234 ;; primary type. E.g. the type "Foo<A,B>::Bar<C>" is stored as
5235 ;; "Foo<>::Bar<". This avoids storing very long strings (since C++
5236 ;; template specs can be fairly sized programs in themselves) and
5237 ;; improves the hit ratio (it's a type regardless of the template
5238 ;; args; it's just not the same type, but we're only interested in
5239 ;; recognizing types, not telling distinct types apart). Note that
5240 ;; template types in references are added here too; from the example
5241 ;; above there will also be an entry "Foo<".
5242 (defvar c-found-types nil)
5243 (make-variable-buffer-local 'c-found-types)
5244
5245 (defsubst c-clear-found-types ()
5246 ;; Clears `c-found-types'.
5247 (setq c-found-types (make-vector 53 0)))
5248
5249 (defun c-add-type (from to)
5250 ;; Add the given region as a type in `c-found-types'. If the region
5251 ;; doesn't match an existing type but there is a type which is equal
5252 ;; to the given one except that the last character is missing, then
5253 ;; the shorter type is removed. That's done to avoid adding all
5254 ;; prefixes of a type as it's being entered and font locked. This
5255 ;; doesn't cover cases like when characters are removed from a type
5256 ;; or added in the middle. We'd need the position of point when the
5257 ;; font locking is invoked to solve this well.
5258 ;;
5259 ;; This function might do hidden buffer changes.
5260 (let ((type (c-syntactic-content from to c-recognize-<>-arglists)))
5261 (unless (intern-soft type c-found-types)
5262 (unintern (substring type 0 -1) c-found-types)
5263 (intern type c-found-types))))
5264
5265 (defun c-unfind-type (name)
5266 ;; Remove the "NAME" from c-found-types, if present.
5267 (unintern name c-found-types))
5268
5269 (defsubst c-check-type (from to)
5270 ;; Return non-nil if the given region contains a type in
5271 ;; `c-found-types'.
5272 ;;
5273 ;; This function might do hidden buffer changes.
5274 (intern-soft (c-syntactic-content from to c-recognize-<>-arglists)
5275 c-found-types))
5276
5277 (defun c-list-found-types ()
5278 ;; Return all the types in `c-found-types' as a sorted list of
5279 ;; strings.
5280 (let (type-list)
5281 (mapatoms (lambda (type)
5282 (setq type-list (cons (symbol-name type)
5283 type-list)))
5284 c-found-types)
5285 (sort type-list 'string-lessp)))
5286
5287 ;; Shut up the byte compiler.
5288 (defvar c-maybe-stale-found-type)
5289
5290 (defun c-trim-found-types (beg end old-len)
5291 ;; An after change function which, in conjunction with the info in
5292 ;; c-maybe-stale-found-type (set in c-before-change), removes a type
5293 ;; from `c-found-types', should this type have become stale. For
5294 ;; example, this happens to "foo" when "foo \n bar();" becomes
5295 ;; "foo(); \n bar();". Such stale types, if not removed, foul up
5296 ;; the fontification.
5297 ;;
5298 ;; Have we, perhaps, added non-ws characters to the front/back of a found
5299 ;; type?
5300 (when (> end beg)
5301 (save-excursion
5302 (when (< end (point-max))
5303 (goto-char end)
5304 (if (and (c-beginning-of-current-token) ; only moves when we started in the middle
5305 (progn (goto-char end)
5306 (c-end-of-current-token)))
5307 (c-unfind-type (buffer-substring-no-properties
5308 end (point)))))
5309 (when (> beg (point-min))
5310 (goto-char beg)
5311 (if (and (c-end-of-current-token) ; only moves when we started in the middle
5312 (progn (goto-char beg)
5313 (c-beginning-of-current-token)))
5314 (c-unfind-type (buffer-substring-no-properties
5315 (point) beg))))))
5316
5317 (if c-maybe-stale-found-type ; e.g. (c-decl-id-start "foo" 97 107 " (* ooka) " "o")
5318 (cond
5319 ;; Changing the amount of (already existing) whitespace - don't do anything.
5320 ((and (c-partial-ws-p beg end)
5321 (or (= beg end) ; removal of WS
5322 (string-match "^[ \t\n\r\f\v]*$" (nth 5 c-maybe-stale-found-type)))))
5323
5324 ;; The syntactic relationship which defined a "found type" has been
5325 ;; destroyed.
5326 ((eq (car c-maybe-stale-found-type) 'c-decl-id-start)
5327 (c-unfind-type (cadr c-maybe-stale-found-type)))
5328 ;; ((eq (car c-maybe-stale-found-type) 'c-decl-type-start) FIXME!!!
5329 )))
5330
5331 \f
5332 ;; Setting and removing syntax properties on < and > in languages (C++
5333 ;; and Java) where they can be template/generic delimiters as well as
5334 ;; their normal meaning of "less/greater than".
5335
5336 ;; Normally, < and > have syntax 'punctuation'. When they are found to
5337 ;; be delimiters, they are marked as such with the category properties
5338 ;; c-<-as-paren-syntax, c->-as-paren-syntax respectively.
5339
5340 ;; STRATEGY:
5341 ;;
5342 ;; It is impossible to determine with certainty whether a <..> pair in
5343 ;; C++ is two comparison operators or is template delimiters, unless
5344 ;; one duplicates a lot of a C++ compiler. For example, the following
5345 ;; code fragment:
5346 ;;
5347 ;; foo (a < b, c > d) ;
5348 ;;
5349 ;; could be a function call with two integer parameters (each a
5350 ;; relational expression), or it could be a constructor for class foo
5351 ;; taking one parameter d of templated type "a < b, c >". They are
5352 ;; somewhat easier to distinguish in Java.
5353 ;;
5354 ;; The strategy now (2010-01) adopted is to mark and unmark < and
5355 ;; > IN MATCHING PAIRS ONLY. [Previously, they were marked
5356 ;; individually when their context so indicated. This gave rise to
5357 ;; intractable problems when one of a matching pair was deleted, or
5358 ;; pulled into a literal.]
5359 ;;
5360 ;; At each buffer change, the syntax-table properties are removed in a
5361 ;; before-change function and reapplied, when needed, in an
5362 ;; after-change function. It is far more important that the
5363 ;; properties get removed when they they are spurious than that they
5364 ;; be present when wanted.
5365 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
5366 (defun c-clear-<-pair-props (&optional pos)
5367 ;; POS (default point) is at a < character. If it is marked with
5368 ;; open paren syntax-table text property, remove the property,
5369 ;; together with the close paren property on the matching > (if
5370 ;; any).
5371 (save-excursion
5372 (if pos
5373 (goto-char pos)
5374 (setq pos (point)))
5375 (when (equal (c-get-char-property (point) 'syntax-table)
5376 c-<-as-paren-syntax)
5377 (with-syntax-table c-no-parens-syntax-table ; ignore unbalanced [,{,(,..
5378 (c-go-list-forward))
5379 (when (equal (c-get-char-property (1- (point)) 'syntax-table)
5380 c->-as-paren-syntax) ; should always be true.
5381 (c-clear-char-property (1- (point)) 'category))
5382 (c-clear-char-property pos 'category))))
5383
5384 (defun c-clear->-pair-props (&optional pos)
5385 ;; POS (default point) is at a > character. If it is marked with
5386 ;; close paren syntax-table property, remove the property, together
5387 ;; with the open paren property on the matching < (if any).
5388 (save-excursion
5389 (if pos
5390 (goto-char pos)
5391 (setq pos (point)))
5392 (when (equal (c-get-char-property (point) 'syntax-table)
5393 c->-as-paren-syntax)
5394 (with-syntax-table c-no-parens-syntax-table ; ignore unbalanced [,{,(,..
5395 (c-go-up-list-backward))
5396 (when (equal (c-get-char-property (point) 'syntax-table)
5397 c-<-as-paren-syntax) ; should always be true.
5398 (c-clear-char-property (point) 'category))
5399 (c-clear-char-property pos 'category))))
5400
5401 (defun c-clear-<>-pair-props (&optional pos)
5402 ;; POS (default point) is at a < or > character. If it has an
5403 ;; open/close paren syntax-table property, remove this property both
5404 ;; from the current character and its partner (which will also be
5405 ;; thusly marked).
5406 (cond
5407 ((eq (char-after) ?\<)
5408 (c-clear-<-pair-props pos))
5409 ((eq (char-after) ?\>)
5410 (c-clear->-pair-props pos))
5411 (t (c-benign-error
5412 "c-clear-<>-pair-props called from wrong position"))))
5413
5414 (defun c-clear-<-pair-props-if-match-after (lim &optional pos)
5415 ;; POS (default point) is at a < character. If it is both marked
5416 ;; with open/close paren syntax-table property, and has a matching >
5417 ;; (also marked) which is after LIM, remove the property both from
5418 ;; the current > and its partner. Return t when this happens, nil
5419 ;; when it doesn't.
5420 (save-excursion
5421 (if pos
5422 (goto-char pos)
5423 (setq pos (point)))
5424 (when (equal (c-get-char-property (point) 'syntax-table)
5425 c-<-as-paren-syntax)
5426 (with-syntax-table c-no-parens-syntax-table ; ignore unbalanced [,{,(,..
5427 (c-go-list-forward))
5428 (when (and (>= (point) lim)
5429 (equal (c-get-char-property (1- (point)) 'syntax-table)
5430 c->-as-paren-syntax)) ; should always be true.
5431 (c-unmark-<->-as-paren (1- (point)))
5432 (c-unmark-<->-as-paren pos))
5433 t)))
5434
5435 (defun c-clear->-pair-props-if-match-before (lim &optional pos)
5436 ;; POS (default point) is at a > character. If it is both marked
5437 ;; with open/close paren syntax-table property, and has a matching <
5438 ;; (also marked) which is before LIM, remove the property both from
5439 ;; the current < and its partner. Return t when this happens, nil
5440 ;; when it doesn't.
5441 (save-excursion
5442 (if pos
5443 (goto-char pos)
5444 (setq pos (point)))
5445 (when (equal (c-get-char-property (point) 'syntax-table)
5446 c->-as-paren-syntax)
5447 (with-syntax-table c-no-parens-syntax-table ; ignore unbalanced [,{,(,..
5448 (c-go-up-list-backward))
5449 (when (and (<= (point) lim)
5450 (equal (c-get-char-property (point) 'syntax-table)
5451 c-<-as-paren-syntax)) ; should always be true.
5452 (c-unmark-<->-as-paren (point))
5453 (c-unmark-<->-as-paren pos))
5454 t)))
5455
5456 ;; Set by c-common-init in cc-mode.el.
5457 (defvar c-new-BEG)
5458 (defvar c-new-END)
5459
5460 (defun c-before-change-check-<>-operators (beg end)
5461 ;; Unmark certain pairs of "< .... >" which are currently marked as
5462 ;; template/generic delimiters. (This marking is via syntax-table
5463 ;; text properties).
5464 ;;
5465 ;; These pairs are those which are in the current "statement" (i.e.,
5466 ;; the region between the {, }, or ; before BEG and the one after
5467 ;; END), and which enclose any part of the interval (BEG END).
5468 ;;
5469 ;; Note that in C++ (?and Java), template/generic parens cannot
5470 ;; enclose a brace or semicolon, so we use these as bounds on the
5471 ;; region we must work on.
5472 ;;
5473 ;; This function is called from before-change-functions (via
5474 ;; c-get-state-before-change-functions). Thus the buffer is widened,
5475 ;; and point is undefined, both at entry and exit.
5476 ;;
5477 ;; FIXME!!! This routine ignores the possibility of macros entirely.
5478 ;; 2010-01-29.
5479 (save-excursion
5480 (let ((beg-lit-limits (progn (goto-char beg) (c-literal-limits)))
5481 (end-lit-limits (progn (goto-char end) (c-literal-limits)))
5482 new-beg new-end need-new-beg need-new-end)
5483 ;; Locate the barrier before the changed region
5484 (goto-char (if beg-lit-limits (car beg-lit-limits) beg))
5485 (c-syntactic-skip-backward "^;{}" (c-determine-limit 512))
5486 (setq new-beg (point))
5487
5488 ;; Remove the syntax-table properties from each pertinent <...> pair.
5489 ;; Firsly, the ones with the < before beg and > after beg.
5490 (while (c-search-forward-char-property 'category 'c-<-as-paren-syntax beg)
5491 (if (c-clear-<-pair-props-if-match-after beg (1- (point)))
5492 (setq need-new-beg t)))
5493
5494 ;; Locate the barrier after END.
5495 (goto-char (if end-lit-limits (cdr end-lit-limits) end))
5496 (c-syntactic-re-search-forward "[;{}]" (c-determine-+ve-limit 512) 'end)
5497 (setq new-end (point))
5498
5499 ;; Remove syntax-table properties from the remaining pertinent <...>
5500 ;; pairs, those with a > after end and < before end.
5501 (while (c-search-backward-char-property 'category 'c->-as-paren-syntax end)
5502 (if (c-clear->-pair-props-if-match-before end)
5503 (setq need-new-end t)))
5504
5505 ;; Extend the fontification region, if needed.
5506 (when need-new-beg
5507 (goto-char new-beg)
5508 (c-forward-syntactic-ws)
5509 (and (< (point) c-new-BEG) (setq c-new-BEG (point))))
5510
5511 (when need-new-end
5512 (and (> new-end c-new-END) (setq c-new-END new-end))))))
5513
5514
5515
5516 (defun c-after-change-check-<>-operators (beg end)
5517 ;; This is called from `after-change-functions' when
5518 ;; c-recognize-<>-arglists' is set. It ensures that no "<" or ">"
5519 ;; chars with paren syntax become part of another operator like "<<"
5520 ;; or ">=".
5521 ;;
5522 ;; This function might do hidden buffer changes.
5523
5524 (save-excursion
5525 (goto-char beg)
5526 (when (or (looking-at "[<>]")
5527 (< (skip-chars-backward "<>") 0))
5528
5529 (goto-char beg)
5530 (c-beginning-of-current-token)
5531 (when (and (< (point) beg)
5532 (looking-at c-<>-multichar-token-regexp)
5533 (< beg (setq beg (match-end 0))))
5534 (while (progn (skip-chars-forward "^<>" beg)
5535 (< (point) beg))
5536 (c-clear-<>-pair-props)
5537 (forward-char))))
5538
5539 (when (< beg end)
5540 (goto-char end)
5541 (when (or (looking-at "[<>]")
5542 (< (skip-chars-backward "<>") 0))
5543
5544 (goto-char end)
5545 (c-beginning-of-current-token)
5546 (when (and (< (point) end)
5547 (looking-at c-<>-multichar-token-regexp)
5548 (< end (setq end (match-end 0))))
5549 (while (progn (skip-chars-forward "^<>" end)
5550 (< (point) end))
5551 (c-clear-<>-pair-props)
5552 (forward-char)))))))
5553
5554
5555 \f
5556 ;; Handling of small scale constructs like types and names.
5557
5558 ;; Dynamically bound variable that instructs `c-forward-type' to also
5559 ;; treat possible types (i.e. those that it normally returns 'maybe or
5560 ;; 'found for) as actual types (and always return 'found for them).
5561 ;; This means that it records them in `c-record-type-identifiers' if
5562 ;; that is set, and that it adds them to `c-found-types'.
5563 (defvar c-promote-possible-types nil)
5564
5565 ;; Dynamically bound variable that instructs `c-forward-<>-arglist' to
5566 ;; mark up successfully parsed arglists with paren syntax properties on
5567 ;; the surrounding angle brackets and with `c-<>-arg-sep' in the
5568 ;; `c-type' property of each argument separating comma.
5569 ;;
5570 ;; Setting this variable also makes `c-forward-<>-arglist' recurse into
5571 ;; all arglists for side effects (i.e. recording types), otherwise it
5572 ;; exploits any existing paren syntax properties to quickly jump to the
5573 ;; end of already parsed arglists.
5574 ;;
5575 ;; Marking up the arglists is not the default since doing that correctly
5576 ;; depends on a proper value for `c-restricted-<>-arglists'.
5577 (defvar c-parse-and-markup-<>-arglists nil)
5578
5579 ;; Dynamically bound variable that instructs `c-forward-<>-arglist' to
5580 ;; not accept arglists that contain binary operators.
5581 ;;
5582 ;; This is primarily used to handle C++ template arglists. C++
5583 ;; disambiguates them by checking whether the preceding name is a
5584 ;; template or not. We can't do that, so we assume it is a template
5585 ;; if it can be parsed as one. That usually works well since
5586 ;; comparison expressions on the forms "a < b > c" or "a < b, c > d"
5587 ;; in almost all cases would be pointless.
5588 ;;
5589 ;; However, in function arglists, e.g. in "foo (a < b, c > d)", we
5590 ;; should let the comma separate the function arguments instead. And
5591 ;; in a context where the value of the expression is taken, e.g. in
5592 ;; "if (a < b || c > d)", it's probably not a template.
5593 (defvar c-restricted-<>-arglists nil)
5594
5595 ;; Dynamically bound variables that instructs
5596 ;; `c-forward-keyword-clause', `c-forward-<>-arglist',
5597 ;; `c-forward-name', `c-forward-type', `c-forward-decl-or-cast-1', and
5598 ;; `c-forward-label' to record the ranges of all the type and
5599 ;; reference identifiers they encounter. They will build lists on
5600 ;; these variables where each element is a cons of the buffer
5601 ;; positions surrounding each identifier. This recording is only
5602 ;; activated when `c-record-type-identifiers' is non-nil.
5603 ;;
5604 ;; All known types that can't be identifiers are recorded, and also
5605 ;; other possible types if `c-promote-possible-types' is set.
5606 ;; Recording is however disabled inside angle bracket arglists that
5607 ;; are encountered inside names and other angle bracket arglists.
5608 ;; Such occurrences are taken care of by `c-font-lock-<>-arglists'
5609 ;; instead.
5610 ;;
5611 ;; Only the names in C++ template style references (e.g. "tmpl" in
5612 ;; "tmpl<a,b>::foo") are recorded as references, other references
5613 ;; aren't handled here.
5614 ;;
5615 ;; `c-forward-label' records the label identifier(s) on
5616 ;; `c-record-ref-identifiers'.
5617 (defvar c-record-type-identifiers nil)
5618 (defvar c-record-ref-identifiers nil)
5619
5620 ;; This variable will receive a cons cell of the range of the last
5621 ;; single identifier symbol stepped over by `c-forward-name' if it's
5622 ;; successful. This is the range that should be put on one of the
5623 ;; record lists above by the caller. It's assigned nil if there's no
5624 ;; such symbol in the name.
5625 (defvar c-last-identifier-range nil)
5626
5627 (defmacro c-record-type-id (range)
5628 (if (eq (car-safe range) 'cons)
5629 ;; Always true.
5630 `(setq c-record-type-identifiers
5631 (cons ,range c-record-type-identifiers))
5632 `(let ((range ,range))
5633 (if range
5634 (setq c-record-type-identifiers
5635 (cons range c-record-type-identifiers))))))
5636
5637 (defmacro c-record-ref-id (range)
5638 (if (eq (car-safe range) 'cons)
5639 ;; Always true.
5640 `(setq c-record-ref-identifiers
5641 (cons ,range c-record-ref-identifiers))
5642 `(let ((range ,range))
5643 (if range
5644 (setq c-record-ref-identifiers
5645 (cons range c-record-ref-identifiers))))))
5646
5647 ;; Dynamically bound variable that instructs `c-forward-type' to
5648 ;; record the ranges of types that only are found. Behaves otherwise
5649 ;; like `c-record-type-identifiers'.
5650 (defvar c-record-found-types nil)
5651
5652 (defmacro c-forward-keyword-prefixed-id (type)
5653 ;; Used internally in `c-forward-keyword-clause' to move forward
5654 ;; over a type (if TYPE is 'type) or a name (otherwise) which
5655 ;; possibly is prefixed by keywords and their associated clauses.
5656 ;; Try with a type/name first to not trip up on those that begin
5657 ;; with a keyword. Return t if a known or found type is moved
5658 ;; over. The point is clobbered if nil is returned. If range
5659 ;; recording is enabled, the identifier is recorded on as a type
5660 ;; if TYPE is 'type or as a reference if TYPE is 'ref.
5661 ;;
5662 ;; This macro might do hidden buffer changes.
5663 `(let (res)
5664 (while (if (setq res ,(if (eq type 'type)
5665 `(c-forward-type)
5666 `(c-forward-name)))
5667 nil
5668 (and (looking-at c-keywords-regexp)
5669 (c-forward-keyword-clause 1))))
5670 (when (memq res '(t known found prefix))
5671 ,(when (eq type 'ref)
5672 `(when c-record-type-identifiers
5673 (c-record-ref-id c-last-identifier-range)))
5674 t)))
5675
5676 (defmacro c-forward-id-comma-list (type update-safe-pos)
5677 ;; Used internally in `c-forward-keyword-clause' to move forward
5678 ;; over a comma separated list of types or names using
5679 ;; `c-forward-keyword-prefixed-id'.
5680 ;;
5681 ;; This macro might do hidden buffer changes.
5682 `(while (and (progn
5683 ,(when update-safe-pos
5684 `(setq safe-pos (point)))
5685 (eq (char-after) ?,))
5686 (progn
5687 (forward-char)
5688 (c-forward-syntactic-ws)
5689 (c-forward-keyword-prefixed-id ,type)))))
5690
5691 (defun c-forward-keyword-clause (match)
5692 ;; Submatch MATCH in the current match data is assumed to surround a
5693 ;; token. If it's a keyword, move over it and any immediately
5694 ;; following clauses associated with it, stopping at the start of
5695 ;; the next token. t is returned in that case, otherwise the point
5696 ;; stays and nil is returned. The kind of clauses that are
5697 ;; recognized are those specified by `c-type-list-kwds',
5698 ;; `c-ref-list-kwds', `c-colon-type-list-kwds',
5699 ;; `c-paren-nontype-kwds', `c-paren-type-kwds', `c-<>-type-kwds',
5700 ;; and `c-<>-arglist-kwds'.
5701 ;;
5702 ;; This function records identifier ranges on
5703 ;; `c-record-type-identifiers' and `c-record-ref-identifiers' if
5704 ;; `c-record-type-identifiers' is non-nil.
5705 ;;
5706 ;; Note that for `c-colon-type-list-kwds', which doesn't necessary
5707 ;; apply directly after the keyword, the type list is moved over
5708 ;; only when there is no unaccounted token before it (i.e. a token
5709 ;; that isn't moved over due to some other keyword list). The
5710 ;; identifier ranges in the list are still recorded if that should
5711 ;; be done, though.
5712 ;;
5713 ;; This function might do hidden buffer changes.
5714
5715 (let ((kwd-sym (c-keyword-sym (match-string match))) safe-pos pos
5716 ;; The call to `c-forward-<>-arglist' below is made after
5717 ;; `c-<>-sexp-kwds' keywords, so we're certain they actually
5718 ;; are angle bracket arglists and `c-restricted-<>-arglists'
5719 ;; should therefore be nil.
5720 (c-parse-and-markup-<>-arglists t)
5721 c-restricted-<>-arglists)
5722
5723 (when kwd-sym
5724 (goto-char (match-end match))
5725 (c-forward-syntactic-ws)
5726 (setq safe-pos (point))
5727
5728 (cond
5729 ((and (c-keyword-member kwd-sym 'c-type-list-kwds)
5730 (c-forward-keyword-prefixed-id type))
5731 ;; There's a type directly after a keyword in `c-type-list-kwds'.
5732 (c-forward-id-comma-list type t))
5733
5734 ((and (c-keyword-member kwd-sym 'c-ref-list-kwds)
5735 (c-forward-keyword-prefixed-id ref))
5736 ;; There's a name directly after a keyword in `c-ref-list-kwds'.
5737 (c-forward-id-comma-list ref t))
5738
5739 ((and (c-keyword-member kwd-sym 'c-paren-any-kwds)
5740 (eq (char-after) ?\())
5741 ;; There's an open paren after a keyword in `c-paren-any-kwds'.
5742
5743 (forward-char)
5744 (when (and (setq pos (c-up-list-forward))
5745 (eq (char-before pos) ?\)))
5746 (when (and c-record-type-identifiers
5747 (c-keyword-member kwd-sym 'c-paren-type-kwds))
5748 ;; Use `c-forward-type' on every identifier we can find
5749 ;; inside the paren, to record the types.
5750 (while (c-syntactic-re-search-forward c-symbol-start pos t)
5751 (goto-char (match-beginning 0))
5752 (unless (c-forward-type)
5753 (looking-at c-symbol-key) ; Always matches.
5754 (goto-char (match-end 0)))))
5755
5756 (goto-char pos)
5757 (c-forward-syntactic-ws)
5758 (setq safe-pos (point))))
5759
5760 ((and (c-keyword-member kwd-sym 'c-<>-sexp-kwds)
5761 (eq (char-after) ?<)
5762 (c-forward-<>-arglist (c-keyword-member kwd-sym 'c-<>-type-kwds)))
5763 (c-forward-syntactic-ws)
5764 (setq safe-pos (point)))
5765
5766 ((and (c-keyword-member kwd-sym 'c-nonsymbol-sexp-kwds)
5767 (not (looking-at c-symbol-start))
5768 (c-safe (c-forward-sexp) t))
5769 (c-forward-syntactic-ws)
5770 (setq safe-pos (point))))
5771
5772 (when (c-keyword-member kwd-sym 'c-colon-type-list-kwds)
5773 (if (eq (char-after) ?:)
5774 ;; If we are at the colon already, we move over the type
5775 ;; list after it.
5776 (progn
5777 (forward-char)
5778 (c-forward-syntactic-ws)
5779 (when (c-forward-keyword-prefixed-id type)
5780 (c-forward-id-comma-list type t)))
5781 ;; Not at the colon, so stop here. But the identifier
5782 ;; ranges in the type list later on should still be
5783 ;; recorded.
5784 (and c-record-type-identifiers
5785 (progn
5786 ;; If a keyword matched both one of the types above and
5787 ;; this one, we match `c-colon-type-list-re' after the
5788 ;; clause matched above.
5789 (goto-char safe-pos)
5790 (looking-at c-colon-type-list-re))
5791 (progn
5792 (goto-char (match-end 0))
5793 (c-forward-syntactic-ws)
5794 (c-forward-keyword-prefixed-id type))
5795 ;; There's a type after the `c-colon-type-list-re' match
5796 ;; after a keyword in `c-colon-type-list-kwds'.
5797 (c-forward-id-comma-list type nil))))
5798
5799 (goto-char safe-pos)
5800 t)))
5801
5802 ;; cc-mode requires cc-fonts.
5803 (declare-function c-fontify-recorded-types-and-refs "cc-fonts" ())
5804
5805 (defun c-forward-<>-arglist (all-types)
5806 ;; The point is assumed to be at a "<". Try to treat it as the open
5807 ;; paren of an angle bracket arglist and move forward to the
5808 ;; corresponding ">". If successful, the point is left after the
5809 ;; ">" and t is returned, otherwise the point isn't moved and nil is
5810 ;; returned. If ALL-TYPES is t then all encountered arguments in
5811 ;; the arglist that might be types are treated as found types.
5812 ;;
5813 ;; The variable `c-parse-and-markup-<>-arglists' controls how this
5814 ;; function handles text properties on the angle brackets and argument
5815 ;; separating commas.
5816 ;;
5817 ;; `c-restricted-<>-arglists' controls how lenient the template
5818 ;; arglist recognition should be.
5819 ;;
5820 ;; This function records identifier ranges on
5821 ;; `c-record-type-identifiers' and `c-record-ref-identifiers' if
5822 ;; `c-record-type-identifiers' is non-nil.
5823 ;;
5824 ;; This function might do hidden buffer changes.
5825
5826 (let ((start (point))
5827 ;; If `c-record-type-identifiers' is set then activate
5828 ;; recording of any found types that constitute an argument in
5829 ;; the arglist.
5830 (c-record-found-types (if c-record-type-identifiers t)))
5831 (if (catch 'angle-bracket-arglist-escape
5832 (setq c-record-found-types
5833 (c-forward-<>-arglist-recur all-types)))
5834 (progn
5835 (when (consp c-record-found-types)
5836 (setq c-record-type-identifiers
5837 ;; `nconc' doesn't mind that the tail of
5838 ;; `c-record-found-types' is t.
5839 (nconc c-record-found-types c-record-type-identifiers)))
5840 (if (c-major-mode-is 'java-mode) (c-fontify-recorded-types-and-refs))
5841 t)
5842
5843 (goto-char start)
5844 nil)))
5845
5846 (defun c-forward-<>-arglist-recur (all-types)
5847 ;; Recursive part of `c-forward-<>-arglist'.
5848 ;;
5849 ;; This function might do hidden buffer changes.
5850
5851 (let ((start (point)) res pos tmp
5852 ;; Cover this so that any recorded found type ranges are
5853 ;; automatically lost if it turns out to not be an angle
5854 ;; bracket arglist. It's propagated through the return value
5855 ;; on successful completion.
5856 (c-record-found-types c-record-found-types)
5857 ;; List that collects the positions after the argument
5858 ;; separating ',' in the arglist.
5859 arg-start-pos)
5860 ;; If the '<' has paren open syntax then we've marked it as an angle
5861 ;; bracket arglist before, so skip to the end.
5862 (if (and (not c-parse-and-markup-<>-arglists)
5863 (c-get-char-property (point) 'syntax-table))
5864
5865 (progn
5866 (forward-char)
5867 (if (and (c-go-up-list-forward)
5868 (eq (char-before) ?>))
5869 t
5870 ;; Got unmatched paren angle brackets. We don't clear the paren
5871 ;; syntax properties and retry, on the basis that it's very
5872 ;; unlikely that paren angle brackets become operators by code
5873 ;; manipulation. It's far more likely that it doesn't match due
5874 ;; to narrowing or some temporary change.
5875 (goto-char start)
5876 nil))
5877
5878 (forward-char) ; Forward over the opening '<'.
5879
5880 (unless (looking-at c-<-op-cont-regexp)
5881 ;; go forward one non-alphanumeric character (group) per iteration of
5882 ;; this loop.
5883 (while (and
5884 (progn
5885 (c-forward-syntactic-ws)
5886 (let ((orig-record-found-types c-record-found-types))
5887 (when (or (and c-record-type-identifiers all-types)
5888 (c-major-mode-is 'java-mode))
5889 ;; All encountered identifiers are types, so set the
5890 ;; promote flag and parse the type.
5891 (progn
5892 (c-forward-syntactic-ws)
5893 (if (looking-at "\\?")
5894 (forward-char)
5895 (when (looking-at c-identifier-start)
5896 (let ((c-promote-possible-types t)
5897 (c-record-found-types t))
5898 (c-forward-type))))
5899
5900 (c-forward-syntactic-ws)
5901
5902 (when (or (looking-at "extends")
5903 (looking-at "super"))
5904 (forward-word)
5905 (c-forward-syntactic-ws)
5906 (let ((c-promote-possible-types t)
5907 (c-record-found-types t))
5908 (c-forward-type)
5909 (c-forward-syntactic-ws))))))
5910
5911 (setq pos (point)) ; e.g. first token inside the '<'
5912
5913 ;; Note: These regexps exploit the match order in \| so
5914 ;; that "<>" is matched by "<" rather than "[^>:-]>".
5915 (c-syntactic-re-search-forward
5916 ;; Stop on ',', '|', '&', '+' and '-' to catch
5917 ;; common binary operators that could be between
5918 ;; two comparison expressions "a<b" and "c>d".
5919 "[<;{},|+&-]\\|[>)]"
5920 nil t t))
5921
5922 (cond
5923 ((eq (char-before) ?>)
5924 ;; Either an operator starting with '>' or the end of
5925 ;; the angle bracket arglist.
5926
5927 (if (looking-at c->-op-cont-regexp)
5928 (progn
5929 (goto-char (match-end 0))
5930 t) ; Continue the loop.
5931
5932 ;; The angle bracket arglist is finished.
5933 (when c-parse-and-markup-<>-arglists
5934 (while arg-start-pos
5935 (c-put-c-type-property (1- (car arg-start-pos))
5936 'c-<>-arg-sep)
5937 (setq arg-start-pos (cdr arg-start-pos)))
5938 (c-mark-<-as-paren start)
5939 (c-mark->-as-paren (1- (point))))
5940 (setq res t)
5941 nil)) ; Exit the loop.
5942
5943 ((eq (char-before) ?<)
5944 ;; Either an operator starting with '<' or a nested arglist.
5945 (setq pos (point))
5946 (let (id-start id-end subres keyword-match)
5947 (cond
5948 ;; The '<' begins a multi-char operator.
5949 ((looking-at c-<-op-cont-regexp)
5950 (setq tmp (match-end 0))
5951 (goto-char (match-end 0)))
5952 ;; We're at a nested <.....>
5953 ((progn
5954 (setq tmp pos)
5955 (backward-char) ; to the '<'
5956 (and
5957 (save-excursion
5958 ;; There's always an identifier before an angle
5959 ;; bracket arglist, or a keyword in `c-<>-type-kwds'
5960 ;; or `c-<>-arglist-kwds'.
5961 (c-backward-syntactic-ws)
5962 (setq id-end (point))
5963 (c-simple-skip-symbol-backward)
5964 (when (or (setq keyword-match
5965 (looking-at c-opt-<>-sexp-key))
5966 (not (looking-at c-keywords-regexp)))
5967 (setq id-start (point))))
5968 (setq subres
5969 (let ((c-promote-possible-types t)
5970 (c-record-found-types t))
5971 (c-forward-<>-arglist-recur
5972 (and keyword-match
5973 (c-keyword-member
5974 (c-keyword-sym (match-string 1))
5975 'c-<>-type-kwds)))))))
5976
5977 ;; It was an angle bracket arglist.
5978 (setq c-record-found-types subres)
5979
5980 ;; Record the identifier before the template as a type
5981 ;; or reference depending on whether the arglist is last
5982 ;; in a qualified identifier.
5983 (when (and c-record-type-identifiers
5984 (not keyword-match))
5985 (if (and c-opt-identifier-concat-key
5986 (progn
5987 (c-forward-syntactic-ws)
5988 (looking-at c-opt-identifier-concat-key)))
5989 (c-record-ref-id (cons id-start id-end))
5990 (c-record-type-id (cons id-start id-end)))))
5991
5992 ;; At a "less than" operator.
5993 (t
5994 (forward-char)
5995 )))
5996 t) ; carry on looping.
5997
5998 ((and (not c-restricted-<>-arglists)
5999 (or (and (eq (char-before) ?&)
6000 (not (eq (char-after) ?&)))
6001 (eq (char-before) ?,)))
6002 ;; Just another argument. Record the position. The
6003 ;; type check stuff that made us stop at it is at
6004 ;; the top of the loop.
6005 (setq arg-start-pos (cons (point) arg-start-pos)))
6006
6007 (t
6008 ;; Got a character that can't be in an angle bracket
6009 ;; arglist argument. Abort using `throw', since
6010 ;; it's useless to try to find a surrounding arglist
6011 ;; if we're nested.
6012 (throw 'angle-bracket-arglist-escape nil))))))
6013 (if res
6014 (or c-record-found-types t)))))
6015
6016 (defun c-backward-<>-arglist (all-types &optional limit)
6017 ;; The point is assumed to be directly after a ">". Try to treat it
6018 ;; as the close paren of an angle bracket arglist and move back to
6019 ;; the corresponding "<". If successful, the point is left at
6020 ;; the "<" and t is returned, otherwise the point isn't moved and
6021 ;; nil is returned. ALL-TYPES is passed on to
6022 ;; `c-forward-<>-arglist'.
6023 ;;
6024 ;; If the optional LIMIT is given, it bounds the backward search.
6025 ;; It's then assumed to be at a syntactically relevant position.
6026 ;;
6027 ;; This is a wrapper around `c-forward-<>-arglist'. See that
6028 ;; function for more details.
6029
6030 (let ((start (point)))
6031 (backward-char)
6032 (if (and (not c-parse-and-markup-<>-arglists)
6033 (c-get-char-property (point) 'syntax-table))
6034
6035 (if (and (c-go-up-list-backward)
6036 (eq (char-after) ?<))
6037 t
6038 ;; See corresponding note in `c-forward-<>-arglist'.
6039 (goto-char start)
6040 nil)
6041
6042 (while (progn
6043 (c-syntactic-skip-backward "^<;{}" limit t)
6044
6045 (and
6046 (if (eq (char-before) ?<)
6047 t
6048 ;; Stopped at bob or a char that isn't allowed in an
6049 ;; arglist, so we've failed.
6050 (goto-char start)
6051 nil)
6052
6053 (if (> (point)
6054 (progn (c-beginning-of-current-token)
6055 (point)))
6056 ;; If we moved then the "<" was part of some
6057 ;; multicharacter token.
6058 t
6059
6060 (backward-char)
6061 (let ((beg-pos (point)))
6062 (if (c-forward-<>-arglist all-types)
6063 (cond ((= (point) start)
6064 ;; Matched the arglist. Break the while.
6065 (goto-char beg-pos)
6066 nil)
6067 ((> (point) start)
6068 ;; We started from a non-paren ">" inside an
6069 ;; arglist.
6070 (goto-char start)
6071 nil)
6072 (t
6073 ;; Matched a shorter arglist. Can be a nested
6074 ;; one so continue looking.
6075 (goto-char beg-pos)
6076 t))
6077 t))))))
6078
6079 (/= (point) start))))
6080
6081 (defun c-forward-name ()
6082 ;; Move forward over a complete name if at the beginning of one,
6083 ;; stopping at the next following token. A keyword, as such,
6084 ;; doesn't count as a name. If the point is not at something that
6085 ;; is recognized as a name then it stays put.
6086 ;;
6087 ;; A name could be something as simple as "foo" in C or something as
6088 ;; complex as "X<Y<class A<int>::B, BIT_MAX >> b>, ::operator<> ::
6089 ;; Z<(a>b)> :: operator const X<&foo>::T Q::G<unsigned short
6090 ;; int>::*volatile const" in C++ (this function is actually little
6091 ;; more than a `looking-at' call in all modes except those that,
6092 ;; like C++, have `c-recognize-<>-arglists' set).
6093 ;;
6094 ;; Return
6095 ;; o - nil if no name is found;
6096 ;; o - 'template if it's an identifier ending with an angle bracket
6097 ;; arglist;
6098 ;; o - 'operator of it's an operator identifier;
6099 ;; o - t if it's some other kind of name.
6100 ;;
6101 ;; This function records identifier ranges on
6102 ;; `c-record-type-identifiers' and `c-record-ref-identifiers' if
6103 ;; `c-record-type-identifiers' is non-nil.
6104 ;;
6105 ;; This function might do hidden buffer changes.
6106
6107 (let ((pos (point)) (start (point)) res id-start id-end
6108 ;; Turn off `c-promote-possible-types' here since we might
6109 ;; call `c-forward-<>-arglist' and we don't want it to promote
6110 ;; every suspect thing in the arglist to a type. We're
6111 ;; typically called from `c-forward-type' in this case, and
6112 ;; the caller only wants the top level type that it finds to
6113 ;; be promoted.
6114 c-promote-possible-types)
6115 (while
6116 (and
6117 (looking-at c-identifier-key)
6118
6119 (progn
6120 ;; Check for keyword. We go to the last symbol in
6121 ;; `c-identifier-key' first.
6122 (goto-char (setq id-end (match-end 0)))
6123 (c-simple-skip-symbol-backward)
6124 (setq id-start (point))
6125
6126 (if (looking-at c-keywords-regexp)
6127 (when (and (c-major-mode-is 'c++-mode)
6128 (looking-at
6129 (cc-eval-when-compile
6130 (concat "\\(operator\\|\\(template\\)\\)"
6131 "\\(" (c-lang-const c-nonsymbol-key c++)
6132 "\\|$\\)")))
6133 (if (match-beginning 2)
6134 ;; "template" is only valid inside an
6135 ;; identifier if preceded by "::".
6136 (save-excursion
6137 (c-backward-syntactic-ws)
6138 (and (c-safe (backward-char 2) t)
6139 (looking-at "::")))
6140 t))
6141
6142 ;; Handle a C++ operator or template identifier.
6143 (goto-char id-end)
6144 (c-forward-syntactic-ws)
6145 (cond ((eq (char-before id-end) ?e)
6146 ;; Got "... ::template".
6147 (let ((subres (c-forward-name)))
6148 (when subres
6149 (setq pos (point)
6150 res subres))))
6151
6152 ((looking-at c-identifier-start)
6153 ;; Got a cast operator.
6154 (when (c-forward-type)
6155 (setq pos (point)
6156 res 'operator)
6157 ;; Now we should match a sequence of either
6158 ;; '*', '&' or a name followed by ":: *",
6159 ;; where each can be followed by a sequence
6160 ;; of `c-opt-type-modifier-key'.
6161 (while (cond ((looking-at "[*&]")
6162 (goto-char (match-end 0))
6163 t)
6164 ((looking-at c-identifier-start)
6165 (and (c-forward-name)
6166 (looking-at "::")
6167 (progn
6168 (goto-char (match-end 0))
6169 (c-forward-syntactic-ws)
6170 (eq (char-after) ?*))
6171 (progn
6172 (forward-char)
6173 t))))
6174 (while (progn
6175 (c-forward-syntactic-ws)
6176 (setq pos (point))
6177 (looking-at c-opt-type-modifier-key))
6178 (goto-char (match-end 1))))))
6179
6180 ((looking-at c-overloadable-operators-regexp)
6181 ;; Got some other operator.
6182 (setq c-last-identifier-range
6183 (cons (point) (match-end 0)))
6184 (goto-char (match-end 0))
6185 (c-forward-syntactic-ws)
6186 (setq pos (point)
6187 res 'operator)))
6188
6189 nil)
6190
6191 ;; `id-start' is equal to `id-end' if we've jumped over
6192 ;; an identifier that doesn't end with a symbol token.
6193 ;; That can occur e.g. for Java import directives on the
6194 ;; form "foo.bar.*".
6195 (when (and id-start (/= id-start id-end))
6196 (setq c-last-identifier-range
6197 (cons id-start id-end)))
6198 (goto-char id-end)
6199 (c-forward-syntactic-ws)
6200 (setq pos (point)
6201 res t)))
6202
6203 (progn
6204 (goto-char pos)
6205 (when (or c-opt-identifier-concat-key
6206 c-recognize-<>-arglists)
6207
6208 (cond
6209 ((and c-opt-identifier-concat-key
6210 (looking-at c-opt-identifier-concat-key))
6211 ;; Got a concatenated identifier. This handles the
6212 ;; cases with tricky syntactic whitespace that aren't
6213 ;; covered in `c-identifier-key'.
6214 (goto-char (match-end 0))
6215 (c-forward-syntactic-ws)
6216 t)
6217
6218 ((and c-recognize-<>-arglists
6219 (eq (char-after) ?<))
6220 ;; Maybe an angle bracket arglist.
6221 (when (let ((c-record-type-identifiers t)
6222 (c-record-found-types t))
6223 (c-forward-<>-arglist nil))
6224
6225 (c-add-type start (1+ pos))
6226 (c-forward-syntactic-ws)
6227 (setq pos (point)
6228 c-last-identifier-range nil)
6229
6230 (if (and c-opt-identifier-concat-key
6231 (looking-at c-opt-identifier-concat-key))
6232
6233 ;; Continue if there's an identifier concatenation
6234 ;; operator after the template argument.
6235 (progn
6236 (when (and c-record-type-identifiers id-start)
6237 (c-record-ref-id (cons id-start id-end)))
6238 (forward-char 2)
6239 (c-forward-syntactic-ws)
6240 t)
6241
6242 (when (and c-record-type-identifiers id-start)
6243 (c-record-type-id (cons id-start id-end)))
6244 (setq res 'template)
6245 nil)))
6246 )))))
6247
6248 (goto-char pos)
6249 res))
6250
6251 (defun c-forward-type (&optional brace-block-too)
6252 ;; Move forward over a type spec if at the beginning of one,
6253 ;; stopping at the next following token. The keyword "typedef"
6254 ;; isn't part of a type spec here.
6255 ;;
6256 ;; BRACE-BLOCK-TOO, when non-nil, means move over the brace block in
6257 ;; constructs like "struct foo {...} bar ;" or "struct {...} bar;".
6258 ;; The current (2009-03-10) intention is to convert all uses of
6259 ;; `c-forward-type' to call with this parameter set, then to
6260 ;; eliminate it.
6261 ;;
6262 ;; Return
6263 ;; o - t if it's a known type that can't be a name or other
6264 ;; expression;
6265 ;; o - 'known if it's an otherwise known type (according to
6266 ;; `*-font-lock-extra-types');
6267 ;; o - 'prefix if it's a known prefix of a type;
6268 ;; o - 'found if it's a type that matches one in `c-found-types';
6269 ;; o - 'maybe if it's an identifier that might be a type; or
6270 ;; o - nil if it can't be a type (the point isn't moved then).
6271 ;;
6272 ;; The point is assumed to be at the beginning of a token.
6273 ;;
6274 ;; Note that this function doesn't skip past the brace definition
6275 ;; that might be considered part of the type, e.g.
6276 ;; "enum {a, b, c} foo".
6277 ;;
6278 ;; This function records identifier ranges on
6279 ;; `c-record-type-identifiers' and `c-record-ref-identifiers' if
6280 ;; `c-record-type-identifiers' is non-nil.
6281 ;;
6282 ;; This function might do hidden buffer changes.
6283 (when (and c-recognize-<>-arglists
6284 (looking-at "<"))
6285 (c-forward-<>-arglist t)
6286 (c-forward-syntactic-ws))
6287
6288 (let ((start (point)) pos res name-res id-start id-end id-range)
6289
6290 ;; Skip leading type modifiers. If any are found we know it's a
6291 ;; prefix of a type.
6292 (when c-opt-type-modifier-key ; e.g. "const" "volatile", but NOT "typedef"
6293 (while (looking-at c-opt-type-modifier-key)
6294 (goto-char (match-end 1))
6295 (c-forward-syntactic-ws)
6296 (setq res 'prefix)))
6297
6298 (cond
6299 ((looking-at c-type-prefix-key) ; e.g. "struct", "class", but NOT
6300 ; "typedef".
6301 (goto-char (match-end 1))
6302 (c-forward-syntactic-ws)
6303 (setq pos (point))
6304
6305 (setq name-res (c-forward-name))
6306 (setq res (not (null name-res)))
6307 (when (eq name-res t)
6308 ;; In many languages the name can be used without the
6309 ;; prefix, so we add it to `c-found-types'.
6310 (c-add-type pos (point))
6311 (when (and c-record-type-identifiers
6312 c-last-identifier-range)
6313 (c-record-type-id c-last-identifier-range)))
6314 (when (and brace-block-too
6315 (memq res '(t nil))
6316 (eq (char-after) ?\{)
6317 (save-excursion
6318 (c-safe
6319 (progn (c-forward-sexp)
6320 (c-forward-syntactic-ws)
6321 (setq pos (point))))))
6322 (goto-char pos)
6323 (setq res t))
6324 (unless res (goto-char start))) ; invalid syntax
6325
6326 ((progn
6327 (setq pos nil)
6328 (if (looking-at c-identifier-start)
6329 (save-excursion
6330 (setq id-start (point)
6331 name-res (c-forward-name))
6332 (when name-res
6333 (setq id-end (point)
6334 id-range c-last-identifier-range))))
6335 (and (cond ((looking-at c-primitive-type-key)
6336 (setq res t))
6337 ((c-with-syntax-table c-identifier-syntax-table
6338 (looking-at c-known-type-key))
6339 (setq res 'known)))
6340 (or (not id-end)
6341 (>= (save-excursion
6342 (save-match-data
6343 (goto-char (match-end 1))
6344 (c-forward-syntactic-ws)
6345 (setq pos (point))))
6346 id-end)
6347 (setq res nil))))
6348 ;; Looking at a primitive or known type identifier. We've
6349 ;; checked for a name first so that we don't go here if the
6350 ;; known type match only is a prefix of another name.
6351
6352 (setq id-end (match-end 1))
6353
6354 (when (and c-record-type-identifiers
6355 (or c-promote-possible-types (eq res t)))
6356 (c-record-type-id (cons (match-beginning 1) (match-end 1))))
6357
6358 (if (and c-opt-type-component-key
6359 (save-match-data
6360 (looking-at c-opt-type-component-key)))
6361 ;; There might be more keywords for the type.
6362 (let (safe-pos)
6363 (c-forward-keyword-clause 1)
6364 (while (progn
6365 (setq safe-pos (point))
6366 (looking-at c-opt-type-component-key))
6367 (when (and c-record-type-identifiers
6368 (looking-at c-primitive-type-key))
6369 (c-record-type-id (cons (match-beginning 1)
6370 (match-end 1))))
6371 (c-forward-keyword-clause 1))
6372 (if (looking-at c-primitive-type-key)
6373 (progn
6374 (when c-record-type-identifiers
6375 (c-record-type-id (cons (match-beginning 1)
6376 (match-end 1))))
6377 (c-forward-keyword-clause 1)
6378 (setq res t))
6379 (goto-char safe-pos)
6380 (setq res 'prefix)))
6381 (unless (save-match-data (c-forward-keyword-clause 1))
6382 (if pos
6383 (goto-char pos)
6384 (goto-char (match-end 1))
6385 (c-forward-syntactic-ws)))))
6386
6387 (name-res
6388 (cond ((eq name-res t)
6389 ;; A normal identifier.
6390 (goto-char id-end)
6391 (if (or res c-promote-possible-types)
6392 (progn
6393 (c-add-type id-start id-end)
6394 (when (and c-record-type-identifiers id-range)
6395 (c-record-type-id id-range))
6396 (unless res
6397 (setq res 'found)))
6398 (setq res (if (c-check-type id-start id-end)
6399 ;; It's an identifier that has been used as
6400 ;; a type somewhere else.
6401 'found
6402 ;; It's an identifier that might be a type.
6403 'maybe))))
6404 ((eq name-res 'template)
6405 ;; A template is a type.
6406 (goto-char id-end)
6407 (setq res t))
6408 (t
6409 ;; Otherwise it's an operator identifier, which is not a type.
6410 (goto-char start)
6411 (setq res nil)))))
6412
6413 (when res
6414 ;; Skip trailing type modifiers. If any are found we know it's
6415 ;; a type.
6416 (when c-opt-type-modifier-key
6417 (while (looking-at c-opt-type-modifier-key) ; e.g. "const", "volatile"
6418 (goto-char (match-end 1))
6419 (c-forward-syntactic-ws)
6420 (setq res t)))
6421 ;; Step over any type suffix operator. Do not let the existence
6422 ;; of these alter the classification of the found type, since
6423 ;; these operators typically are allowed in normal expressions
6424 ;; too.
6425 (when c-opt-type-suffix-key
6426 (while (looking-at c-opt-type-suffix-key)
6427 (goto-char (match-end 1))
6428 (c-forward-syntactic-ws)))
6429
6430 (when c-opt-type-concat-key ; Only/mainly for pike.
6431 ;; Look for a trailing operator that concatenates the type
6432 ;; with a following one, and if so step past that one through
6433 ;; a recursive call. Note that we don't record concatenated
6434 ;; types in `c-found-types' - it's the component types that
6435 ;; are recorded when appropriate.
6436 (setq pos (point))
6437 (let* ((c-promote-possible-types (or (memq res '(t known))
6438 c-promote-possible-types))
6439 ;; If we can't promote then set `c-record-found-types' so that
6440 ;; we can merge in the types from the second part afterwards if
6441 ;; it turns out to be a known type there.
6442 (c-record-found-types (and c-record-type-identifiers
6443 (not c-promote-possible-types)))
6444 subres)
6445 (if (and (looking-at c-opt-type-concat-key)
6446
6447 (progn
6448 (goto-char (match-end 1))
6449 (c-forward-syntactic-ws)
6450 (setq subres (c-forward-type))))
6451
6452 (progn
6453 ;; If either operand certainly is a type then both are, but we
6454 ;; don't let the existence of the operator itself promote two
6455 ;; uncertain types to a certain one.
6456 (cond ((eq res t))
6457 ((eq subres t)
6458 (unless (eq name-res 'template)
6459 (c-add-type id-start id-end))
6460 (when (and c-record-type-identifiers id-range)
6461 (c-record-type-id id-range))
6462 (setq res t))
6463 ((eq res 'known))
6464 ((eq subres 'known)
6465 (setq res 'known))
6466 ((eq res 'found))
6467 ((eq subres 'found)
6468 (setq res 'found))
6469 (t
6470 (setq res 'maybe)))
6471
6472 (when (and (eq res t)
6473 (consp c-record-found-types))
6474 ;; Merge in the ranges of any types found by the second
6475 ;; `c-forward-type'.
6476 (setq c-record-type-identifiers
6477 ;; `nconc' doesn't mind that the tail of
6478 ;; `c-record-found-types' is t.
6479 (nconc c-record-found-types
6480 c-record-type-identifiers))))
6481
6482 (goto-char pos))))
6483
6484 (when (and c-record-found-types (memq res '(known found)) id-range)
6485 (setq c-record-found-types
6486 (cons id-range c-record-found-types))))
6487
6488 ;;(message "c-forward-type %s -> %s: %s" start (point) res)
6489
6490 res))
6491
6492 (defun c-forward-annotation ()
6493 ;; Used for Java code only at the moment. Assumes point is on the
6494 ;; @, moves forward an annotation. returns nil if there is no
6495 ;; annotation at point.
6496 (and (looking-at "@")
6497 (progn (forward-char) t)
6498 (c-forward-type)
6499 (progn (c-forward-syntactic-ws) t)
6500 (if (looking-at "(")
6501 (c-go-list-forward)
6502 t)))
6503
6504 \f
6505 ;; Handling of large scale constructs like statements and declarations.
6506
6507 ;; Macro used inside `c-forward-decl-or-cast-1'. It ought to be a
6508 ;; defsubst or perhaps even a defun, but it contains lots of free
6509 ;; variables that refer to things inside `c-forward-decl-or-cast-1'.
6510 (defmacro c-fdoc-shift-type-backward (&optional short)
6511 ;; `c-forward-decl-or-cast-1' can consume an arbitrary length list
6512 ;; of types when parsing a declaration, which means that it
6513 ;; sometimes consumes the identifier in the declaration as a type.
6514 ;; This is used to "backtrack" and make the last type be treated as
6515 ;; an identifier instead.
6516 `(progn
6517 ,(unless short
6518 ;; These identifiers are bound only in the inner let.
6519 '(setq identifier-type at-type
6520 identifier-start type-start
6521 got-parens nil
6522 got-identifier t
6523 got-suffix t
6524 got-suffix-after-parens id-start
6525 paren-depth 0))
6526
6527 (if (setq at-type (if (eq backup-at-type 'prefix)
6528 t
6529 backup-at-type))
6530 (setq type-start backup-type-start
6531 id-start backup-id-start)
6532 (setq type-start start-pos
6533 id-start start-pos))
6534
6535 ;; When these flags already are set we've found specifiers that
6536 ;; unconditionally signal these attributes - backtracking doesn't
6537 ;; change that. So keep them set in that case.
6538 (or at-type-decl
6539 (setq at-type-decl backup-at-type-decl))
6540 (or maybe-typeless
6541 (setq maybe-typeless backup-maybe-typeless))
6542
6543 ,(unless short
6544 ;; This identifier is bound only in the inner let.
6545 '(setq start id-start))))
6546
6547 (defun c-forward-decl-or-cast-1 (preceding-token-end context last-cast-end)
6548 ;; Move forward over a declaration or a cast if at the start of one.
6549 ;; The point is assumed to be at the start of some token. Nil is
6550 ;; returned if no declaration or cast is recognized, and the point
6551 ;; is clobbered in that case.
6552 ;;
6553 ;; If a declaration is parsed:
6554 ;;
6555 ;; The point is left at the first token after the first complete
6556 ;; declarator, if there is one. The return value is a cons where
6557 ;; the car is the position of the first token in the declarator. (See
6558 ;; below for the cdr.)
6559 ;; Some examples:
6560 ;;
6561 ;; void foo (int a, char *b) stuff ...
6562 ;; car ^ ^ point
6563 ;; float (*a)[], b;
6564 ;; car ^ ^ point
6565 ;; unsigned int a = c_style_initializer, b;
6566 ;; car ^ ^ point
6567 ;; unsigned int a (cplusplus_style_initializer), b;
6568 ;; car ^ ^ point (might change)
6569 ;; class Foo : public Bar {}
6570 ;; car ^ ^ point
6571 ;; class PikeClass (int a, string b) stuff ...
6572 ;; car ^ ^ point
6573 ;; enum bool;
6574 ;; car ^ ^ point
6575 ;; enum bool flag;
6576 ;; car ^ ^ point
6577 ;; void cplusplus_function (int x) throw (Bad);
6578 ;; car ^ ^ point
6579 ;; Foo::Foo (int b) : Base (b) {}
6580 ;; car ^ ^ point
6581 ;;
6582 ;; The cdr of the return value is non-nil when a
6583 ;; `c-typedef-decl-kwds' specifier is found in the declaration.
6584 ;; Specifically it is a dotted pair (A . B) where B is t when a
6585 ;; `c-typedef-kwds' ("typedef") is present, and A is t when some
6586 ;; other `c-typedef-decl-kwds' (e.g. class, struct, enum)
6587 ;; specifier is present. I.e., (some of) the declared
6588 ;; identifier(s) are types.
6589 ;;
6590 ;; If a cast is parsed:
6591 ;;
6592 ;; The point is left at the first token after the closing paren of
6593 ;; the cast. The return value is `cast'. Note that the start
6594 ;; position must be at the first token inside the cast parenthesis
6595 ;; to recognize it.
6596 ;;
6597 ;; PRECEDING-TOKEN-END is the first position after the preceding
6598 ;; token, i.e. on the other side of the syntactic ws from the point.
6599 ;; Use a value less than or equal to (point-min) if the point is at
6600 ;; the first token in (the visible part of) the buffer.
6601 ;;
6602 ;; CONTEXT is a symbol that describes the context at the point:
6603 ;; 'decl In a comma-separated declaration context (typically
6604 ;; inside a function declaration arglist).
6605 ;; '<> In an angle bracket arglist.
6606 ;; 'arglist Some other type of arglist.
6607 ;; nil Some other context or unknown context. Includes
6608 ;; within the parens of an if, for, ... construct.
6609 ;;
6610 ;; LAST-CAST-END is the first token after the closing paren of a
6611 ;; preceding cast, or nil if none is known. If
6612 ;; `c-forward-decl-or-cast-1' is used in succession, it should be
6613 ;; the position after the closest preceding call where a cast was
6614 ;; matched. In that case it's used to discover chains of casts like
6615 ;; "(a) (b) c".
6616 ;;
6617 ;; This function records identifier ranges on
6618 ;; `c-record-type-identifiers' and `c-record-ref-identifiers' if
6619 ;; `c-record-type-identifiers' is non-nil.
6620 ;;
6621 ;; This function might do hidden buffer changes.
6622
6623 (let (;; `start-pos' is used below to point to the start of the
6624 ;; first type, i.e. after any leading specifiers. It might
6625 ;; also point at the beginning of the preceding syntactic
6626 ;; whitespace.
6627 (start-pos (point))
6628 ;; Set to the result of `c-forward-type'.
6629 at-type
6630 ;; The position of the first token in what we currently
6631 ;; believe is the type in the declaration or cast, after any
6632 ;; specifiers and their associated clauses.
6633 type-start
6634 ;; The position of the first token in what we currently
6635 ;; believe is the declarator for the first identifier. Set
6636 ;; when the type is found, and moved forward over any
6637 ;; `c-decl-hangon-kwds' and their associated clauses that
6638 ;; occurs after the type.
6639 id-start
6640 ;; These store `at-type', `type-start' and `id-start' of the
6641 ;; identifier before the one in those variables. The previous
6642 ;; identifier might turn out to be the real type in a
6643 ;; declaration if the last one has to be the declarator in it.
6644 ;; If `backup-at-type' is nil then the other variables have
6645 ;; undefined values.
6646 backup-at-type backup-type-start backup-id-start
6647 ;; Set if we've found a specifier (apart from "typedef") that makes
6648 ;; the defined identifier(s) types.
6649 at-type-decl
6650 ;; Set if we've a "typedef" keyword.
6651 at-typedef
6652 ;; Set if we've found a specifier that can start a declaration
6653 ;; where there's no type.
6654 maybe-typeless
6655 ;; If a specifier is found that also can be a type prefix,
6656 ;; these flags are set instead of those above. If we need to
6657 ;; back up an identifier, they are copied to the real flag
6658 ;; variables. Thus they only take effect if we fail to
6659 ;; interpret it as a type.
6660 backup-at-type-decl backup-maybe-typeless
6661 ;; Whether we've found a declaration or a cast. We might know
6662 ;; this before we've found the type in it. It's 'ids if we've
6663 ;; found two consecutive identifiers (usually a sure sign, but
6664 ;; we should allow that in labels too), and t if we've found a
6665 ;; specifier keyword (a 100% sure sign).
6666 at-decl-or-cast
6667 ;; Set when we need to back up to parse this as a declaration
6668 ;; but not as a cast.
6669 backup-if-not-cast
6670 ;; For casts, the return position.
6671 cast-end
6672 ;; Save `c-record-type-identifiers' and
6673 ;; `c-record-ref-identifiers' since ranges are recorded
6674 ;; speculatively and should be thrown away if it turns out
6675 ;; that it isn't a declaration or cast.
6676 (save-rec-type-ids c-record-type-identifiers)
6677 (save-rec-ref-ids c-record-ref-identifiers))
6678
6679 (while (c-forward-annotation)
6680 (c-forward-syntactic-ws))
6681
6682 ;; Check for a type. Unknown symbols are treated as possible
6683 ;; types, but they could also be specifiers disguised through
6684 ;; macros like __INLINE__, so we recognize both types and known
6685 ;; specifiers after them too.
6686 (while
6687 (let* ((start (point)) kwd-sym kwd-clause-end found-type)
6688
6689 ;; Look for a specifier keyword clause.
6690 (when (or (looking-at c-prefix-spec-kwds-re)
6691 (and (c-major-mode-is 'java-mode)
6692 (looking-at "@[A-Za-z0-9]+")))
6693 (if (looking-at c-typedef-key)
6694 (setq at-typedef t))
6695 (setq kwd-sym (c-keyword-sym (match-string 1)))
6696 (save-excursion
6697 (c-forward-keyword-clause 1)
6698 (setq kwd-clause-end (point))))
6699
6700 (when (setq found-type (c-forward-type t)) ; brace-block-too
6701 ;; Found a known or possible type or a prefix of a known type.
6702
6703 (when at-type
6704 ;; Got two identifiers with nothing but whitespace
6705 ;; between them. That can only happen in declarations.
6706 (setq at-decl-or-cast 'ids)
6707
6708 (when (eq at-type 'found)
6709 ;; If the previous identifier is a found type we
6710 ;; record it as a real one; it might be some sort of
6711 ;; alias for a prefix like "unsigned".
6712 (save-excursion
6713 (goto-char type-start)
6714 (let ((c-promote-possible-types t))
6715 (c-forward-type)))))
6716
6717 (setq backup-at-type at-type
6718 backup-type-start type-start
6719 backup-id-start id-start
6720 at-type found-type
6721 type-start start
6722 id-start (point)
6723 ;; The previous ambiguous specifier/type turned out
6724 ;; to be a type since we've parsed another one after
6725 ;; it, so clear these backup flags.
6726 backup-at-type-decl nil
6727 backup-maybe-typeless nil))
6728
6729 (if kwd-sym
6730 (progn
6731 ;; Handle known specifier keywords and
6732 ;; `c-decl-hangon-kwds' which can occur after known
6733 ;; types.
6734
6735 (if (c-keyword-member kwd-sym 'c-decl-hangon-kwds)
6736 ;; It's a hang-on keyword that can occur anywhere.
6737 (progn
6738 (setq at-decl-or-cast t)
6739 (if at-type
6740 ;; Move the identifier start position if
6741 ;; we've passed a type.
6742 (setq id-start kwd-clause-end)
6743 ;; Otherwise treat this as a specifier and
6744 ;; move the fallback position.
6745 (setq start-pos kwd-clause-end))
6746 (goto-char kwd-clause-end))
6747
6748 ;; It's an ordinary specifier so we know that
6749 ;; anything before this can't be the type.
6750 (setq backup-at-type nil
6751 start-pos kwd-clause-end)
6752
6753 (if found-type
6754 ;; It's ambiguous whether this keyword is a
6755 ;; specifier or a type prefix, so set the backup
6756 ;; flags. (It's assumed that `c-forward-type'
6757 ;; moved further than `c-forward-keyword-clause'.)
6758 (progn
6759 (when (c-keyword-member kwd-sym 'c-typedef-decl-kwds)
6760 (setq backup-at-type-decl t))
6761 (when (c-keyword-member kwd-sym 'c-typeless-decl-kwds)
6762 (setq backup-maybe-typeless t)))
6763
6764 (when (c-keyword-member kwd-sym 'c-typedef-decl-kwds)
6765 ;; This test only happens after we've scanned a type.
6766 ;; So, with valid syntax, kwd-sym can't be 'typedef.
6767 (setq at-type-decl t))
6768 (when (c-keyword-member kwd-sym 'c-typeless-decl-kwds)
6769 (setq maybe-typeless t))
6770
6771 ;; Haven't matched a type so it's an unambiguous
6772 ;; specifier keyword and we know we're in a
6773 ;; declaration.
6774 (setq at-decl-or-cast t)
6775
6776 (goto-char kwd-clause-end))))
6777
6778 ;; If the type isn't known we continue so that we'll jump
6779 ;; over all specifiers and type identifiers. The reason
6780 ;; to do this for a known type prefix is to make things
6781 ;; like "unsigned INT16" work.
6782 (and found-type (not (eq found-type t))))))
6783
6784 (cond
6785 ((eq at-type t)
6786 ;; If a known type was found, we still need to skip over any
6787 ;; hangon keyword clauses after it. Otherwise it has already
6788 ;; been done in the loop above.
6789 (while (looking-at c-decl-hangon-key)
6790 (c-forward-keyword-clause 1))
6791 (setq id-start (point)))
6792
6793 ((eq at-type 'prefix)
6794 ;; A prefix type is itself a primitive type when it's not
6795 ;; followed by another type.
6796 (setq at-type t))
6797
6798 ((not at-type)
6799 ;; Got no type but set things up to continue anyway to handle
6800 ;; the various cases when a declaration doesn't start with a
6801 ;; type.
6802 (setq id-start start-pos))
6803
6804 ((and (eq at-type 'maybe)
6805 (c-major-mode-is 'c++-mode))
6806 ;; If it's C++ then check if the last "type" ends on the form
6807 ;; "foo::foo" or "foo::~foo", i.e. if it's the name of a
6808 ;; (con|de)structor.
6809 (save-excursion
6810 (let (name end-2 end-1)
6811 (goto-char id-start)
6812 (c-backward-syntactic-ws)
6813 (setq end-2 (point))
6814 (when (and
6815 (c-simple-skip-symbol-backward)
6816 (progn
6817 (setq name
6818 (buffer-substring-no-properties (point) end-2))
6819 ;; Cheating in the handling of syntactic ws below.
6820 (< (skip-chars-backward ":~ \t\n\r\v\f") 0))
6821 (progn
6822 (setq end-1 (point))
6823 (c-simple-skip-symbol-backward))
6824 (>= (point) type-start)
6825 (equal (buffer-substring-no-properties (point) end-1)
6826 name))
6827 ;; It is a (con|de)structor name. In that case the
6828 ;; declaration is typeless so zap out any preceding
6829 ;; identifier(s) that we might have taken as types.
6830 (goto-char type-start)
6831 (setq at-type nil
6832 backup-at-type nil
6833 id-start type-start))))))
6834
6835 ;; Check for and step over a type decl expression after the thing
6836 ;; that is or might be a type. This can't be skipped since we
6837 ;; need the correct end position of the declarator for
6838 ;; `max-type-decl-end-*'.
6839 (let ((start (point)) (paren-depth 0) pos
6840 ;; True if there's a non-open-paren match of
6841 ;; `c-type-decl-prefix-key'.
6842 got-prefix
6843 ;; True if the declarator is surrounded by a parenthesis pair.
6844 got-parens
6845 ;; True if there is an identifier in the declarator.
6846 got-identifier
6847 ;; True if there's a non-close-paren match of
6848 ;; `c-type-decl-suffix-key'.
6849 got-suffix
6850 ;; True if there's a prefix match outside the outermost
6851 ;; paren pair that surrounds the declarator.
6852 got-prefix-before-parens
6853 ;; True if there's a suffix match outside the outermost
6854 ;; paren pair that surrounds the declarator. The value is
6855 ;; the position of the first suffix match.
6856 got-suffix-after-parens
6857 ;; True if we've parsed the type decl to a token that is
6858 ;; known to end declarations in this context.
6859 at-decl-end
6860 ;; The earlier values of `at-type' and `type-start' if we've
6861 ;; shifted the type backwards.
6862 identifier-type identifier-start
6863 ;; If `c-parse-and-markup-<>-arglists' is set we need to
6864 ;; turn it off during the name skipping below to avoid
6865 ;; getting `c-type' properties that might be bogus. That
6866 ;; can happen since we don't know if
6867 ;; `c-restricted-<>-arglists' will be correct inside the
6868 ;; arglist paren that gets entered.
6869 c-parse-and-markup-<>-arglists)
6870
6871 (goto-char id-start)
6872
6873 ;; Skip over type decl prefix operators. (Note similar code in
6874 ;; `c-font-lock-declarators'.)
6875 (while (and (looking-at c-type-decl-prefix-key)
6876 (if (and (c-major-mode-is 'c++-mode)
6877 (match-beginning 3))
6878 ;; If the second submatch matches in C++ then
6879 ;; we're looking at an identifier that's a
6880 ;; prefix only if it specifies a member pointer.
6881 (when (setq got-identifier (c-forward-name))
6882 (if (looking-at "\\(::\\)")
6883 ;; We only check for a trailing "::" and
6884 ;; let the "*" that should follow be
6885 ;; matched in the next round.
6886 (progn (setq got-identifier nil) t)
6887 ;; It turned out to be the real identifier,
6888 ;; so stop.
6889 nil))
6890 t))
6891
6892 (if (eq (char-after) ?\()
6893 (progn
6894 (setq paren-depth (1+ paren-depth))
6895 (forward-char))
6896 (unless got-prefix-before-parens
6897 (setq got-prefix-before-parens (= paren-depth 0)))
6898 (setq got-prefix t)
6899 (goto-char (match-end 1)))
6900 (c-forward-syntactic-ws))
6901
6902 (setq got-parens (> paren-depth 0))
6903
6904 ;; Skip over an identifier.
6905 (or got-identifier
6906 (and (looking-at c-identifier-start)
6907 (setq got-identifier (c-forward-name))))
6908
6909 ;; Skip over type decl suffix operators.
6910 (while (if (looking-at c-type-decl-suffix-key)
6911
6912 (if (eq (char-after) ?\))
6913 (when (> paren-depth 0)
6914 (setq paren-depth (1- paren-depth))
6915 (forward-char)
6916 t)
6917 (when (if (save-match-data (looking-at "\\s\("))
6918 (c-safe (c-forward-sexp 1) t)
6919 (goto-char (match-end 1))
6920 t)
6921 (when (and (not got-suffix-after-parens)
6922 (= paren-depth 0))
6923 (setq got-suffix-after-parens (match-beginning 0)))
6924 (setq got-suffix t)))
6925
6926 ;; No suffix matched. We might have matched the
6927 ;; identifier as a type and the open paren of a
6928 ;; function arglist as a type decl prefix. In that
6929 ;; case we should "backtrack": Reinterpret the last
6930 ;; type as the identifier, move out of the arglist and
6931 ;; continue searching for suffix operators.
6932 ;;
6933 ;; Do this even if there's no preceding type, to cope
6934 ;; with old style function declarations in K&R C,
6935 ;; (con|de)structors in C++ and `c-typeless-decl-kwds'
6936 ;; style declarations. That isn't applicable in an
6937 ;; arglist context, though.
6938 (when (and (= paren-depth 1)
6939 (not got-prefix-before-parens)
6940 (not (eq at-type t))
6941 (or backup-at-type
6942 maybe-typeless
6943 backup-maybe-typeless
6944 (when c-recognize-typeless-decls
6945 (not context)))
6946 (setq pos (c-up-list-forward (point)))
6947 (eq (char-before pos) ?\)))
6948 (c-fdoc-shift-type-backward)
6949 (goto-char pos)
6950 t))
6951
6952 (c-forward-syntactic-ws))
6953
6954 (when (and (or maybe-typeless backup-maybe-typeless)
6955 (not got-identifier)
6956 (not got-prefix)
6957 at-type)
6958 ;; Have found no identifier but `c-typeless-decl-kwds' has
6959 ;; matched so we know we're inside a declaration. The
6960 ;; preceding type must be the identifier instead.
6961 (c-fdoc-shift-type-backward))
6962
6963 (setq
6964 at-decl-or-cast
6965 (catch 'at-decl-or-cast
6966
6967 ;; CASE 1
6968 (when (> paren-depth 0)
6969 ;; Encountered something inside parens that isn't matched by
6970 ;; the `c-type-decl-*' regexps, so it's not a type decl
6971 ;; expression. Try to skip out to the same paren depth to
6972 ;; not confuse the cast check below.
6973 (c-safe (goto-char (scan-lists (point) 1 paren-depth)))
6974 ;; If we've found a specifier keyword then it's a
6975 ;; declaration regardless.
6976 (throw 'at-decl-or-cast (eq at-decl-or-cast t)))
6977
6978 (setq at-decl-end
6979 (looking-at (cond ((eq context '<>) "[,>]")
6980 (context "[,\)]")
6981 (t "[,;]"))))
6982
6983 ;; Now we've collected info about various characteristics of
6984 ;; the construct we're looking at. Below follows a decision
6985 ;; tree based on that. It's ordered to check more certain
6986 ;; signs before less certain ones.
6987
6988 (if got-identifier
6989 (progn
6990
6991 ;; CASE 2
6992 (when (and (or at-type maybe-typeless)
6993 (not (or got-prefix got-parens)))
6994 ;; Got another identifier directly after the type, so it's a
6995 ;; declaration.
6996 (throw 'at-decl-or-cast t))
6997
6998 (when (and got-parens
6999 (not got-prefix)
7000 (not got-suffix-after-parens)
7001 (or backup-at-type
7002 maybe-typeless
7003 backup-maybe-typeless))
7004 ;; Got a declaration of the form "foo bar (gnu);" where we've
7005 ;; recognized "bar" as the type and "gnu" as the declarator.
7006 ;; In this case it's however more likely that "bar" is the
7007 ;; declarator and "gnu" a function argument or initializer (if
7008 ;; `c-recognize-paren-inits' is set), since the parens around
7009 ;; "gnu" would be superfluous if it's a declarator. Shift the
7010 ;; type one step backward.
7011 (c-fdoc-shift-type-backward)))
7012
7013 ;; Found no identifier.
7014
7015 (if backup-at-type
7016 (progn
7017
7018
7019 ;; CASE 3
7020 (when (= (point) start)
7021 ;; Got a plain list of identifiers. If a colon follows it's
7022 ;; a valid label, or maybe a bitfield. Otherwise the last
7023 ;; one probably is the declared identifier and we should
7024 ;; back up to the previous type, providing it isn't a cast.
7025 (if (and (eq (char-after) ?:)
7026 (not (c-major-mode-is 'java-mode)))
7027 (cond
7028 ;; If we've found a specifier keyword then it's a
7029 ;; declaration regardless.
7030 ((eq at-decl-or-cast t)
7031 (throw 'at-decl-or-cast t))
7032 ((and c-has-bitfields
7033 (eq at-decl-or-cast 'ids)) ; bitfield.
7034 (setq backup-if-not-cast t)
7035 (throw 'at-decl-or-cast t)))
7036
7037 (setq backup-if-not-cast t)
7038 (throw 'at-decl-or-cast t)))
7039
7040 ;; CASE 4
7041 (when (and got-suffix
7042 (not got-prefix)
7043 (not got-parens))
7044 ;; Got a plain list of identifiers followed by some suffix.
7045 ;; If this isn't a cast then the last identifier probably is
7046 ;; the declared one and we should back up to the previous
7047 ;; type.
7048 (setq backup-if-not-cast t)
7049 (throw 'at-decl-or-cast t)))
7050
7051 ;; CASE 5
7052 (when (eq at-type t)
7053 ;; If the type is known we know that there can't be any
7054 ;; identifier somewhere else, and it's only in declarations in
7055 ;; e.g. function prototypes and in casts that the identifier may
7056 ;; be left out.
7057 (throw 'at-decl-or-cast t))
7058
7059 (when (= (point) start)
7060 ;; Only got a single identifier (parsed as a type so far).
7061 ;; CASE 6
7062 (if (and
7063 ;; Check that the identifier isn't at the start of an
7064 ;; expression.
7065 at-decl-end
7066 (cond
7067 ((eq context 'decl)
7068 ;; Inside an arglist that contains declarations. If K&R
7069 ;; style declarations and parenthesis style initializers
7070 ;; aren't allowed then the single identifier must be a
7071 ;; type, else we require that it's known or found
7072 ;; (primitive types are handled above).
7073 (or (and (not c-recognize-knr-p)
7074 (not c-recognize-paren-inits))
7075 (memq at-type '(known found))))
7076 ((eq context '<>)
7077 ;; Inside a template arglist. Accept known and found
7078 ;; types; other identifiers could just as well be
7079 ;; constants in C++.
7080 (memq at-type '(known found)))))
7081 (throw 'at-decl-or-cast t)
7082 ;; CASE 7
7083 ;; Can't be a valid declaration or cast, but if we've found a
7084 ;; specifier it can't be anything else either, so treat it as
7085 ;; an invalid/unfinished declaration or cast.
7086 (throw 'at-decl-or-cast at-decl-or-cast))))
7087
7088 (if (and got-parens
7089 (not got-prefix)
7090 (not context)
7091 (not (eq at-type t))
7092 (or backup-at-type
7093 maybe-typeless
7094 backup-maybe-typeless
7095 (when c-recognize-typeless-decls
7096 (or (not got-suffix)
7097 (not (looking-at
7098 c-after-suffixed-type-maybe-decl-key))))))
7099 ;; Got an empty paren pair and a preceding type that probably
7100 ;; really is the identifier. Shift the type backwards to make
7101 ;; the last one the identifier. This is analogous to the
7102 ;; "backtracking" done inside the `c-type-decl-suffix-key' loop
7103 ;; above.
7104 ;;
7105 ;; Exception: In addition to the conditions in that
7106 ;; "backtracking" code, do not shift backward if we're not
7107 ;; looking at either `c-after-suffixed-type-decl-key' or "[;,]".
7108 ;; Since there's no preceding type, the shift would mean that
7109 ;; the declaration is typeless. But if the regexp doesn't match
7110 ;; then we will simply fall through in the tests below and not
7111 ;; recognize it at all, so it's better to try it as an abstract
7112 ;; declarator instead.
7113 (c-fdoc-shift-type-backward)
7114
7115 ;; Still no identifier.
7116 ;; CASE 8
7117 (when (and got-prefix (or got-parens got-suffix))
7118 ;; Require `got-prefix' together with either `got-parens' or
7119 ;; `got-suffix' to recognize it as an abstract declarator:
7120 ;; `got-parens' only is probably an empty function call.
7121 ;; `got-suffix' only can build an ordinary expression together
7122 ;; with the preceding identifier which we've taken as a type.
7123 ;; We could actually accept on `got-prefix' only, but that can
7124 ;; easily occur temporarily while writing an expression so we
7125 ;; avoid that case anyway. We could do a better job if we knew
7126 ;; the point when the fontification was invoked.
7127 (throw 'at-decl-or-cast t))
7128
7129 ;; CASE 9
7130 (when (and at-type
7131 (not got-prefix)
7132 (not got-parens)
7133 got-suffix-after-parens
7134 (eq (char-after got-suffix-after-parens) ?\())
7135 ;; Got a type, no declarator but a paren suffix. I.e. it's a
7136 ;; normal function call after all (or perhaps a C++ style object
7137 ;; instantiation expression).
7138 (throw 'at-decl-or-cast nil))))
7139
7140 ;; CASE 10
7141 (when at-decl-or-cast
7142 ;; By now we've located the type in the declaration that we know
7143 ;; we're in.
7144 (throw 'at-decl-or-cast t))
7145
7146 ;; CASE 11
7147 (when (and got-identifier
7148 (not context)
7149 (looking-at c-after-suffixed-type-decl-key)
7150 (if (and got-parens
7151 (not got-prefix)
7152 (not got-suffix)
7153 (not (eq at-type t)))
7154 ;; Shift the type backward in the case that there's a
7155 ;; single identifier inside parens. That can only
7156 ;; occur in K&R style function declarations so it's
7157 ;; more likely that it really is a function call.
7158 ;; Therefore we only do this after
7159 ;; `c-after-suffixed-type-decl-key' has matched.
7160 (progn (c-fdoc-shift-type-backward) t)
7161 got-suffix-after-parens))
7162 ;; A declaration according to `c-after-suffixed-type-decl-key'.
7163 (throw 'at-decl-or-cast t))
7164
7165 ;; CASE 12
7166 (when (and (or got-prefix (not got-parens))
7167 (memq at-type '(t known)))
7168 ;; It's a declaration if a known type precedes it and it can't be a
7169 ;; function call.
7170 (throw 'at-decl-or-cast t))
7171
7172 ;; If we get here we can't tell if this is a type decl or a normal
7173 ;; expression by looking at it alone. (That's under the assumption
7174 ;; that normal expressions always can look like type decl expressions,
7175 ;; which isn't really true but the cases where it doesn't hold are so
7176 ;; uncommon (e.g. some placements of "const" in C++) it's not worth
7177 ;; the effort to look for them.)
7178
7179 (unless (or at-decl-end (looking-at "=[^=]"))
7180 ;; If this is a declaration it should end here or its initializer(*)
7181 ;; should start here, so check for allowed separation tokens. Note
7182 ;; that this rule doesn't work e.g. with a K&R arglist after a
7183 ;; function header.
7184 ;;
7185 ;; *) Don't check for C++ style initializers using parens
7186 ;; since those already have been matched as suffixes.
7187 ;;
7188 ;; If `at-decl-or-cast' is then we've found some other sign that
7189 ;; it's a declaration or cast, so then it's probably an
7190 ;; invalid/unfinished one.
7191 (throw 'at-decl-or-cast at-decl-or-cast))
7192
7193 ;; Below are tests that only should be applied when we're certain to
7194 ;; not have parsed halfway through an expression.
7195
7196 ;; CASE 14
7197 (when (memq at-type '(t known))
7198 ;; The expression starts with a known type so treat it as a
7199 ;; declaration.
7200 (throw 'at-decl-or-cast t))
7201
7202 ;; CASE 15
7203 (when (and (c-major-mode-is 'c++-mode)
7204 ;; In C++ we check if the identifier is a known type, since
7205 ;; (con|de)structors use the class name as identifier.
7206 ;; We've always shifted over the identifier as a type and
7207 ;; then backed up again in this case.
7208 identifier-type
7209 (or (memq identifier-type '(found known))
7210 (and (eq (char-after identifier-start) ?~)
7211 ;; `at-type' probably won't be 'found for
7212 ;; destructors since the "~" is then part of the
7213 ;; type name being checked against the list of
7214 ;; known types, so do a check without that
7215 ;; operator.
7216 (or (save-excursion
7217 (goto-char (1+ identifier-start))
7218 (c-forward-syntactic-ws)
7219 (c-with-syntax-table
7220 c-identifier-syntax-table
7221 (looking-at c-known-type-key)))
7222 (save-excursion
7223 (goto-char (1+ identifier-start))
7224 ;; We have already parsed the type earlier,
7225 ;; so it'd be possible to cache the end
7226 ;; position instead of redoing it here, but
7227 ;; then we'd need to keep track of another
7228 ;; position everywhere.
7229 (c-check-type (point)
7230 (progn (c-forward-type)
7231 (point))))))))
7232 (throw 'at-decl-or-cast t))
7233
7234 (if got-identifier
7235 (progn
7236 ;; CASE 16
7237 (when (and got-prefix-before-parens
7238 at-type
7239 (or at-decl-end (looking-at "=[^=]"))
7240 (not context)
7241 (not got-suffix))
7242 ;; Got something like "foo * bar;". Since we're not inside an
7243 ;; arglist it would be a meaningless expression because the
7244 ;; result isn't used. We therefore choose to recognize it as
7245 ;; a declaration. Do not allow a suffix since it could then
7246 ;; be a function call.
7247 (throw 'at-decl-or-cast t))
7248
7249 ;; CASE 17
7250 (when (and (or got-suffix-after-parens
7251 (looking-at "=[^=]"))
7252 (eq at-type 'found)
7253 (not (eq context 'arglist)))
7254 ;; Got something like "a (*b) (c);" or "a (b) = c;". It could
7255 ;; be an odd expression or it could be a declaration. Treat
7256 ;; it as a declaration if "a" has been used as a type
7257 ;; somewhere else (if it's a known type we won't get here).
7258 (throw 'at-decl-or-cast t)))
7259
7260 ;; CASE 18
7261 (when (and context
7262 (or got-prefix
7263 (and (eq context 'decl)
7264 (not c-recognize-paren-inits)
7265 (or got-parens got-suffix))))
7266 ;; Got a type followed by an abstract declarator. If `got-prefix'
7267 ;; is set it's something like "a *" without anything after it. If
7268 ;; `got-parens' or `got-suffix' is set it's "a()", "a[]", "a()[]",
7269 ;; or similar, which we accept only if the context rules out
7270 ;; expressions.
7271 (throw 'at-decl-or-cast t)))
7272
7273 ;; If we had a complete symbol table here (which rules out
7274 ;; `c-found-types') we should return t due to the disambiguation rule
7275 ;; (in at least C++) that anything that can be parsed as a declaration
7276 ;; is a declaration. Now we're being more defensive and prefer to
7277 ;; highlight things like "foo (bar);" as a declaration only if we're
7278 ;; inside an arglist that contains declarations.
7279 (eq context 'decl))))
7280
7281 ;; The point is now after the type decl expression.
7282
7283 (cond
7284 ;; Check for a cast.
7285 ((save-excursion
7286 (and
7287 c-cast-parens
7288
7289 ;; Should be the first type/identifier in a cast paren.
7290 (> preceding-token-end (point-min))
7291 (memq (char-before preceding-token-end) c-cast-parens)
7292
7293 ;; The closing paren should follow.
7294 (progn
7295 (c-forward-syntactic-ws)
7296 (looking-at "\\s\)"))
7297
7298 ;; There should be a primary expression after it.
7299 (let (pos)
7300 (forward-char)
7301 (c-forward-syntactic-ws)
7302 (setq cast-end (point))
7303 (and (looking-at c-primary-expr-regexp)
7304 (progn
7305 (setq pos (match-end 0))
7306 (or
7307 ;; Check if the expression begins with a prefix keyword.
7308 (match-beginning 2)
7309 (if (match-beginning 1)
7310 ;; Expression begins with an ambiguous operator. Treat
7311 ;; it as a cast if it's a type decl or if we've
7312 ;; recognized the type somewhere else.
7313 (or at-decl-or-cast
7314 (memq at-type '(t known found)))
7315 ;; Unless it's a keyword, it's the beginning of a primary
7316 ;; expression.
7317 (not (looking-at c-keywords-regexp)))))
7318 ;; If `c-primary-expr-regexp' matched a nonsymbol token, check
7319 ;; that it matched a whole one so that we don't e.g. confuse
7320 ;; the operator '-' with '->'. It's ok if it matches further,
7321 ;; though, since it e.g. can match the float '.5' while the
7322 ;; operator regexp only matches '.'.
7323 (or (not (looking-at c-nonsymbol-token-regexp))
7324 (<= (match-end 0) pos))))
7325
7326 ;; There should either be a cast before it or something that isn't an
7327 ;; identifier or close paren.
7328 (> preceding-token-end (point-min))
7329 (progn
7330 (goto-char (1- preceding-token-end))
7331 (or (eq (point) last-cast-end)
7332 (progn
7333 (c-backward-syntactic-ws)
7334 (if (< (skip-syntax-backward "w_") 0)
7335 ;; It's a symbol. Accept it only if it's one of the
7336 ;; keywords that can precede an expression (without
7337 ;; surrounding parens).
7338 (looking-at c-simple-stmt-key)
7339 (and
7340 ;; Check that it isn't a close paren (block close is ok,
7341 ;; though).
7342 (not (memq (char-before) '(?\) ?\])))
7343 ;; Check that it isn't a nonsymbol identifier.
7344 (not (c-on-identifier)))))))))
7345
7346 ;; Handle the cast.
7347 (when (and c-record-type-identifiers at-type (not (eq at-type t)))
7348 (let ((c-promote-possible-types t))
7349 (goto-char type-start)
7350 (c-forward-type)))
7351
7352 (goto-char cast-end)
7353 'cast)
7354
7355 (at-decl-or-cast
7356 ;; We're at a declaration. Highlight the type and the following
7357 ;; declarators.
7358
7359 (when backup-if-not-cast
7360 (c-fdoc-shift-type-backward t))
7361
7362 (when (and (eq context 'decl) (looking-at ","))
7363 ;; Make sure to propagate the `c-decl-arg-start' property to
7364 ;; the next argument if it's set in this one, to cope with
7365 ;; interactive refontification.
7366 (c-put-c-type-property (point) 'c-decl-arg-start))
7367
7368 (when (and c-record-type-identifiers at-type (not (eq at-type t)))
7369 (let ((c-promote-possible-types t))
7370 (save-excursion
7371 (goto-char type-start)
7372 (c-forward-type))))
7373
7374 (cons id-start
7375 (and (or at-type-decl at-typedef)
7376 (cons at-type-decl at-typedef))))
7377
7378 (t
7379 ;; False alarm. Restore the recorded ranges.
7380 (setq c-record-type-identifiers save-rec-type-ids
7381 c-record-ref-identifiers save-rec-ref-ids)
7382 nil))))
7383
7384 (defun c-forward-label (&optional assume-markup preceding-token-end limit)
7385 ;; Assuming that point is at the beginning of a token, check if it starts a
7386 ;; label and if so move over it and return non-nil (t in default situations,
7387 ;; specific symbols (see below) for interesting situations), otherwise don't
7388 ;; move and return nil. "Label" here means "most things with a colon".
7389 ;;
7390 ;; More precisely, a "label" is regarded as one of:
7391 ;; (i) a goto target like "foo:" - returns the symbol `goto-target';
7392 ;; (ii) A case label - either the entire construct "case FOO:", or just the
7393 ;; bare "case", should the colon be missing. We return t;
7394 ;; (iii) a keyword which needs a colon, like "default:" or "private:"; We
7395 ;; return t;
7396 ;; (iv) One of QT's "extended" C++ variants of
7397 ;; "private:"/"protected:"/"public:"/"more:" looking like "public slots:".
7398 ;; Returns the symbol `qt-2kwds-colon'.
7399 ;; (v) QT's construct "signals:". Returns the symbol `qt-1kwd-colon'.
7400 ;; (vi) One of the keywords matched by `c-opt-extra-label-key' (without any
7401 ;; colon). Currently (2006-03), this applies only to Objective C's
7402 ;; keywords "@private", "@protected", and "@public". Returns t.
7403 ;;
7404 ;; One of the things which will NOT be recognized as a label is a bit-field
7405 ;; element of a struct, something like "int foo:5".
7406 ;;
7407 ;; The end of the label is taken to be just after the colon, or the end of
7408 ;; the first submatch in `c-opt-extra-label-key'. The point is directly
7409 ;; after the end on return. The terminating char gets marked with
7410 ;; `c-decl-end' to improve recognition of the following declaration or
7411 ;; statement.
7412 ;;
7413 ;; If ASSUME-MARKUP is non-nil, it's assumed that the preceding
7414 ;; label, if any, has already been marked up like that.
7415 ;;
7416 ;; If PRECEDING-TOKEN-END is given, it should be the first position
7417 ;; after the preceding token, i.e. on the other side of the
7418 ;; syntactic ws from the point. Use a value less than or equal to
7419 ;; (point-min) if the point is at the first token in (the visible
7420 ;; part of) the buffer.
7421 ;;
7422 ;; The optional LIMIT limits the forward scan for the colon.
7423 ;;
7424 ;; This function records the ranges of the label symbols on
7425 ;; `c-record-ref-identifiers' if `c-record-type-identifiers' (!) is
7426 ;; non-nil.
7427 ;;
7428 ;; This function might do hidden buffer changes.
7429
7430 (let ((start (point))
7431 label-end
7432 qt-symbol-idx
7433 macro-start ; if we're in one.
7434 label-type
7435 kwd)
7436 (cond
7437 ;; "case" or "default" (Doesn't apply to AWK).
7438 ((looking-at c-label-kwds-regexp)
7439 (let ((kwd-end (match-end 1)))
7440 ;; Record only the keyword itself for fontification, since in
7441 ;; case labels the following is a constant expression and not
7442 ;; a label.
7443 (when c-record-type-identifiers
7444 (c-record-ref-id (cons (match-beginning 1) kwd-end)))
7445
7446 ;; Find the label end.
7447 (goto-char kwd-end)
7448 (setq label-type
7449 (if (and (c-syntactic-re-search-forward
7450 ;; Stop on chars that aren't allowed in expressions,
7451 ;; and on operator chars that would be meaningless
7452 ;; there. FIXME: This doesn't cope with ?: operators.
7453 "[;{=,@]\\|\\(\\=\\|[^:]\\):\\([^:]\\|\\'\\)"
7454 limit t t nil 1)
7455 (match-beginning 2))
7456
7457 (progn ; there's a proper :
7458 (goto-char (match-beginning 2)) ; just after the :
7459 (c-put-c-type-property (1- (point)) 'c-decl-end)
7460 t)
7461
7462 ;; It's an unfinished label. We consider the keyword enough
7463 ;; to recognize it as a label, so that it gets fontified.
7464 ;; Leave the point at the end of it, but don't put any
7465 ;; `c-decl-end' marker.
7466 (goto-char kwd-end)
7467 t))))
7468
7469 ;; @private, @protected, @public, in Objective C, or similar.
7470 ((and c-opt-extra-label-key
7471 (looking-at c-opt-extra-label-key))
7472 ;; For a `c-opt-extra-label-key' match, we record the whole
7473 ;; thing for fontification. That's to get the leading '@' in
7474 ;; Objective-C protection labels fontified.
7475 (goto-char (match-end 1))
7476 (when c-record-type-identifiers
7477 (c-record-ref-id (cons (match-beginning 1) (point))))
7478 (c-put-c-type-property (1- (point)) 'c-decl-end)
7479 (setq label-type t))
7480
7481 ;; All other cases of labels.
7482 ((and c-recognize-colon-labels ; nil for AWK and IDL, otherwise t.
7483
7484 ;; A colon label must have something before the colon.
7485 (not (eq (char-after) ?:))
7486
7487 ;; Check that we're not after a token that can't precede a label.
7488 (or
7489 ;; Trivially succeeds when there's no preceding token.
7490 ;; Succeeds when we're at a virtual semicolon.
7491 (if preceding-token-end
7492 (<= preceding-token-end (point-min))
7493 (save-excursion
7494 (c-backward-syntactic-ws)
7495 (setq preceding-token-end (point))
7496 (or (bobp)
7497 (c-at-vsemi-p))))
7498
7499 ;; Check if we're after a label, if we're after a closing
7500 ;; paren that belong to statement, and with
7501 ;; `c-label-prefix-re'. It's done in different order
7502 ;; depending on `assume-markup' since the checks have
7503 ;; different expensiveness.
7504 (if assume-markup
7505 (or
7506 (eq (c-get-char-property (1- preceding-token-end) 'c-type)
7507 'c-decl-end)
7508
7509 (save-excursion
7510 (goto-char (1- preceding-token-end))
7511 (c-beginning-of-current-token)
7512 (or (looking-at c-label-prefix-re)
7513 (looking-at c-block-stmt-1-key)))
7514
7515 (and (eq (char-before preceding-token-end) ?\))
7516 (c-after-conditional)))
7517
7518 (or
7519 (save-excursion
7520 (goto-char (1- preceding-token-end))
7521 (c-beginning-of-current-token)
7522 (or (looking-at c-label-prefix-re)
7523 (looking-at c-block-stmt-1-key)))
7524
7525 (cond
7526 ((eq (char-before preceding-token-end) ?\))
7527 (c-after-conditional))
7528
7529 ((eq (char-before preceding-token-end) ?:)
7530 ;; Might be after another label, so check it recursively.
7531 (save-restriction
7532 (save-excursion
7533 (goto-char (1- preceding-token-end))
7534 ;; Essentially the same as the
7535 ;; `c-syntactic-re-search-forward' regexp below.
7536 (setq macro-start
7537 (save-excursion (and (c-beginning-of-macro)
7538 (point))))
7539 (if macro-start (narrow-to-region macro-start (point-max)))
7540 (c-syntactic-skip-backward "^-]:?;}=*/%&|,<>!@+" nil t)
7541 ;; Note: the following should work instead of the
7542 ;; narrow-to-region above. Investigate why not,
7543 ;; sometime. ACM, 2006-03-31.
7544 ;; (c-syntactic-skip-backward "^-]:?;}=*/%&|,<>!@+"
7545 ;; macro-start t)
7546 (let ((pte (point))
7547 ;; If the caller turned on recording for us,
7548 ;; it shouldn't apply when we check the
7549 ;; preceding label.
7550 c-record-type-identifiers)
7551 ;; A label can't start at a cpp directive. Check for
7552 ;; this, since c-forward-syntactic-ws would foul up on it.
7553 (unless (and c-opt-cpp-prefix (looking-at c-opt-cpp-prefix))
7554 (c-forward-syntactic-ws)
7555 (c-forward-label nil pte start))))))))))
7556
7557 ;; Point is still at the beginning of the possible label construct.
7558 ;;
7559 ;; Check that the next nonsymbol token is ":", or that we're in one
7560 ;; of QT's "slots" declarations. Allow '(' for the sake of macro
7561 ;; arguments. FIXME: Should build this regexp from the language
7562 ;; constants.
7563 (cond
7564 ;; public: protected: private:
7565 ((and
7566 (c-major-mode-is 'c++-mode)
7567 (search-forward-regexp
7568 "\\=p\\(r\\(ivate\\|otected\\)\\|ublic\\)\\>[^_]" nil t)
7569 (progn (backward-char)
7570 (c-forward-syntactic-ws limit)
7571 (looking-at ":\\([^:]\\|\\'\\)"))) ; A single colon.
7572 (forward-char)
7573 (setq label-type t))
7574 ;; QT double keyword like "protected slots:" or goto target.
7575 ((progn (goto-char start) nil))
7576 ((when (c-syntactic-re-search-forward
7577 "[ \t\n[:?;{=*/%&|,<>!@+-]" limit t t) ; not at EOB
7578 (backward-char)
7579 (setq label-end (point))
7580 (setq qt-symbol-idx
7581 (and (c-major-mode-is 'c++-mode)
7582 (string-match
7583 "\\(p\\(r\\(ivate\\|otected\\)\\|ublic\\)\\|more\\)\\>"
7584 (buffer-substring start (point)))))
7585 (c-forward-syntactic-ws limit)
7586 (cond
7587 ((looking-at ":\\([^:]\\|\\'\\)") ; A single colon.
7588 (forward-char)
7589 (setq label-type
7590 (if (or (string= "signals" ; Special QT macro
7591 (setq kwd (buffer-substring-no-properties start label-end)))
7592 (string= "Q_SIGNALS" kwd))
7593 'qt-1kwd-colon
7594 'goto-target)))
7595 ((and qt-symbol-idx
7596 (search-forward-regexp "\\=\\(slots\\|Q_SLOTS\\)\\>" limit t)
7597 (progn (c-forward-syntactic-ws limit)
7598 (looking-at ":\\([^:]\\|\\'\\)"))) ; A single colon
7599 (forward-char)
7600 (setq label-type 'qt-2kwds-colon)))))))
7601
7602 (save-restriction
7603 (narrow-to-region start (point))
7604
7605 ;; Check that `c-nonlabel-token-key' doesn't match anywhere.
7606 (catch 'check-label
7607 (goto-char start)
7608 (while (progn
7609 (when (looking-at c-nonlabel-token-key)
7610 (goto-char start)
7611 (setq label-type nil)
7612 (throw 'check-label nil))
7613 (and (c-safe (c-forward-sexp)
7614 (c-forward-syntactic-ws)
7615 t)
7616 (not (eobp)))))
7617
7618 ;; Record the identifiers in the label for fontification, unless
7619 ;; it begins with `c-label-kwds' in which case the following
7620 ;; identifiers are part of a (constant) expression that
7621 ;; shouldn't be fontified.
7622 (when (and c-record-type-identifiers
7623 (progn (goto-char start)
7624 (not (looking-at c-label-kwds-regexp))))
7625 (while (c-syntactic-re-search-forward c-symbol-key nil t)
7626 (c-record-ref-id (cons (match-beginning 0)
7627 (match-end 0)))))
7628
7629 (c-put-c-type-property (1- (point-max)) 'c-decl-end)
7630 (goto-char (point-max)))))
7631
7632 (t
7633 ;; Not a label.
7634 (goto-char start)))
7635 label-type))
7636
7637 (defun c-forward-objc-directive ()
7638 ;; Assuming the point is at the beginning of a token, try to move
7639 ;; forward to the end of the Objective-C directive that starts
7640 ;; there. Return t if a directive was fully recognized, otherwise
7641 ;; the point is moved as far as one could be successfully parsed and
7642 ;; nil is returned.
7643 ;;
7644 ;; This function records identifier ranges on
7645 ;; `c-record-type-identifiers' and `c-record-ref-identifiers' if
7646 ;; `c-record-type-identifiers' is non-nil.
7647 ;;
7648 ;; This function might do hidden buffer changes.
7649
7650 (let ((start (point))
7651 start-char
7652 (c-promote-possible-types t)
7653 lim
7654 ;; Turn off recognition of angle bracket arglists while parsing
7655 ;; types here since the protocol reference list might then be
7656 ;; considered part of the preceding name or superclass-name.
7657 c-recognize-<>-arglists)
7658
7659 (if (or
7660 (when (looking-at
7661 (eval-when-compile
7662 (c-make-keywords-re t
7663 (append (c-lang-const c-protection-kwds objc)
7664 '("@end"))
7665 'objc-mode)))
7666 (goto-char (match-end 1))
7667 t)
7668
7669 (and
7670 (looking-at
7671 (eval-when-compile
7672 (c-make-keywords-re t
7673 '("@interface" "@implementation" "@protocol")
7674 'objc-mode)))
7675
7676 ;; Handle the name of the class itself.
7677 (progn
7678 ; (c-forward-token-2) ; 2006/1/13 This doesn't move if the token's
7679 ; at EOB.
7680 (goto-char (match-end 0))
7681 (setq lim (point))
7682 (c-skip-ws-forward)
7683 (c-forward-type))
7684
7685 (catch 'break
7686 ;; Look for ": superclass-name" or "( category-name )".
7687 (when (looking-at "[:\(]")
7688 (setq start-char (char-after))
7689 (forward-char)
7690 (c-forward-syntactic-ws)
7691 (unless (c-forward-type) (throw 'break nil))
7692 (when (eq start-char ?\()
7693 (unless (eq (char-after) ?\)) (throw 'break nil))
7694 (forward-char)
7695 (c-forward-syntactic-ws)))
7696
7697 ;; Look for a protocol reference list.
7698 (if (eq (char-after) ?<)
7699 (let ((c-recognize-<>-arglists t)
7700 (c-parse-and-markup-<>-arglists t)
7701 c-restricted-<>-arglists)
7702 (c-forward-<>-arglist t))
7703 t))))
7704
7705 (progn
7706 (c-backward-syntactic-ws lim)
7707 (c-clear-c-type-property start (1- (point)) 'c-decl-end)
7708 (c-put-c-type-property (1- (point)) 'c-decl-end)
7709 t)
7710
7711 (c-clear-c-type-property start (point) 'c-decl-end)
7712 nil)))
7713
7714 (defun c-beginning-of-inheritance-list (&optional lim)
7715 ;; Go to the first non-whitespace after the colon that starts a
7716 ;; multiple inheritance introduction. Optional LIM is the farthest
7717 ;; back we should search.
7718 ;;
7719 ;; This function might do hidden buffer changes.
7720 (c-with-syntax-table c++-template-syntax-table
7721 (c-backward-token-2 0 t lim)
7722 (while (and (or (looking-at c-symbol-start)
7723 (looking-at "[<,]\\|::"))
7724 (zerop (c-backward-token-2 1 t lim))))))
7725
7726 (defun c-in-method-def-p ()
7727 ;; Return nil if we aren't in a method definition, otherwise the
7728 ;; position of the initial [+-].
7729 ;;
7730 ;; This function might do hidden buffer changes.
7731 (save-excursion
7732 (beginning-of-line)
7733 (and c-opt-method-key
7734 (looking-at c-opt-method-key)
7735 (point))
7736 ))
7737
7738 ;; Contributed by Kevin Ryde <user42@zip.com.au>.
7739 (defun c-in-gcc-asm-p ()
7740 ;; Return non-nil if point is within a gcc \"asm\" block.
7741 ;;
7742 ;; This should be called with point inside an argument list.
7743 ;;
7744 ;; Only one level of enclosing parentheses is considered, so for
7745 ;; instance `nil' is returned when in a function call within an asm
7746 ;; operand.
7747 ;;
7748 ;; This function might do hidden buffer changes.
7749
7750 (and c-opt-asm-stmt-key
7751 (save-excursion
7752 (beginning-of-line)
7753 (backward-up-list 1)
7754 (c-beginning-of-statement-1 (point-min) nil t)
7755 (looking-at c-opt-asm-stmt-key))))
7756
7757 (defun c-at-toplevel-p ()
7758 "Return a determination as to whether point is \"at the top level\".
7759 Informally, \"at the top level\" is anywhere where you can write
7760 a function.
7761
7762 More precisely, being at the top-level means that point is either
7763 outside any enclosing block (such as a function definition), or
7764 directly inside a class, namespace or other block that contains
7765 another declaration level.
7766
7767 If point is not at the top-level (e.g. it is inside a method
7768 definition), then nil is returned. Otherwise, if point is at a
7769 top-level not enclosed within a class definition, t is returned.
7770 Otherwise, a 2-vector is returned where the zeroth element is the
7771 buffer position of the start of the class declaration, and the first
7772 element is the buffer position of the enclosing class's opening
7773 brace.
7774
7775 Note that this function might do hidden buffer changes. See the
7776 comment at the start of cc-engine.el for more info."
7777 (let ((paren-state (c-parse-state)))
7778 (or (not (c-most-enclosing-brace paren-state))
7779 (c-search-uplist-for-classkey paren-state))))
7780
7781 (defun c-just-after-func-arglist-p (&optional lim)
7782 ;; Return non-nil if the point is in the region after the argument
7783 ;; list of a function and its opening brace (or semicolon in case it
7784 ;; got no body). If there are K&R style argument declarations in
7785 ;; that region, the point has to be inside the first one for this
7786 ;; function to recognize it.
7787 ;;
7788 ;; If successful, the point is moved to the first token after the
7789 ;; function header (see `c-forward-decl-or-cast-1' for details) and
7790 ;; the position of the opening paren of the function arglist is
7791 ;; returned.
7792 ;;
7793 ;; The point is clobbered if not successful.
7794 ;;
7795 ;; LIM is used as bound for backward buffer searches.
7796 ;;
7797 ;; This function might do hidden buffer changes.
7798
7799 (let ((beg (point)) end id-start)
7800 (and
7801 (eq (c-beginning-of-statement-1 lim) 'same)
7802
7803 (not (and (c-major-mode-is 'objc-mode)
7804 (c-forward-objc-directive)))
7805
7806 (setq id-start
7807 (car-safe (c-forward-decl-or-cast-1 (c-point 'bosws) nil nil)))
7808 (< id-start beg)
7809
7810 ;; There should not be a '=' or ',' between beg and the
7811 ;; start of the declaration since that means we were in the
7812 ;; "expression part" of the declaration.
7813 (or (> (point) beg)
7814 (not (looking-at "[=,]")))
7815
7816 (save-excursion
7817 ;; Check that there's an arglist paren in the
7818 ;; declaration.
7819 (goto-char id-start)
7820 (cond ((eq (char-after) ?\()
7821 ;; The declarator is a paren expression, so skip past it
7822 ;; so that we don't get stuck on that instead of the
7823 ;; function arglist.
7824 (c-forward-sexp))
7825 ((and c-opt-op-identifier-prefix
7826 (looking-at c-opt-op-identifier-prefix))
7827 ;; Don't trip up on "operator ()".
7828 (c-forward-token-2 2 t)))
7829 (and (< (point) beg)
7830 (c-syntactic-re-search-forward "(" beg t t)
7831 (1- (point)))))))
7832
7833 (defun c-in-knr-argdecl (&optional lim)
7834 ;; Return the position of the first argument declaration if point is
7835 ;; inside a K&R style argument declaration list, nil otherwise.
7836 ;; `c-recognize-knr-p' is not checked. If LIM is non-nil, it's a
7837 ;; position that bounds the backward search for the argument list.
7838 ;;
7839 ;; Point must be within a possible K&R region, e.g. just before a top-level
7840 ;; "{". It must be outside of parens and brackets. The test can return
7841 ;; false positives otherwise.
7842 ;;
7843 ;; This function might do hidden buffer changes.
7844
7845 (save-excursion
7846 (save-restriction
7847 ;; If we're in a macro, our search range is restricted to it. Narrow to
7848 ;; the searchable range.
7849 (let* ((macro-start (save-excursion (and (c-beginning-of-macro) (point))))
7850 (macro-end (save-excursion (and macro-start (c-end-of-macro) (point))))
7851 (low-lim (max (or lim (point-min)) (or macro-start (point-min))))
7852 before-lparen after-rparen
7853 (pp-count-out 20)) ; Max number of paren/brace constructs before
7854 ; we give up
7855 (narrow-to-region low-lim (or macro-end (point-max)))
7856
7857 ;; Search backwards for the defun's argument list. We give up if we
7858 ;; encounter a "}" (end of a previous defun) an "=" (which can't be in
7859 ;; a knr region) or BOB.
7860 ;;
7861 ;; The criterion for a paren structure being the arg list is:
7862 ;; o - there is non-WS stuff after it but before any "{"; AND
7863 ;; o - the token after it isn't a ";" AND
7864 ;; o - it is preceded by either an identifier (the function name) or
7865 ;; a macro expansion like "DEFUN (...)"; AND
7866 ;; o - its content is a non-empty comma-separated list of identifiers
7867 ;; (an empty arg list won't have a knr region).
7868 ;;
7869 ;; The following snippet illustrates these rules:
7870 ;; int foo (bar, baz, yuk)
7871 ;; int bar [] ;
7872 ;; int (*baz) (my_type) ;
7873 ;; int (*) (void) (*yuk) (void) ;
7874 ;; {
7875
7876 (catch 'knr
7877 (while (> pp-count-out 0) ; go back one paren/bracket pair each time.
7878 (setq pp-count-out (1- pp-count-out))
7879 (c-syntactic-skip-backward "^)]}=")
7880 (cond ((eq (char-before) ?\))
7881 (setq after-rparen (point)))
7882 ((eq (char-before) ?\])
7883 (setq after-rparen nil))
7884 (t ; either } (hit previous defun) or = or no more
7885 ; parens/brackets.
7886 (throw 'knr nil)))
7887
7888 (if after-rparen
7889 ;; We're inside a paren. Could it be our argument list....?
7890 (if
7891 (and
7892 (progn
7893 (goto-char after-rparen)
7894 (unless (c-go-list-backward) (throw 'knr nil)) ;
7895 ;; FIXME!!! What about macros between the parens? 2007/01/20
7896 (setq before-lparen (point)))
7897
7898 ;; It can't be the arg list if next token is ; or {
7899 (progn (goto-char after-rparen)
7900 (c-forward-syntactic-ws)
7901 (not (memq (char-after) '(?\; ?\{ ?\=))))
7902
7903 ;; Is the thing preceding the list an identifier (the
7904 ;; function name), or a macro expansion?
7905 (progn
7906 (goto-char before-lparen)
7907 (eq (c-backward-token-2) 0)
7908 (or (eq (c-on-identifier) (point))
7909 (and (eq (char-after) ?\))
7910 (c-go-up-list-backward)
7911 (eq (c-backward-token-2) 0)
7912 (eq (c-on-identifier) (point)))))
7913
7914 ;; Have we got a non-empty list of comma-separated
7915 ;; identifiers?
7916 (progn
7917 (goto-char before-lparen)
7918 (c-forward-token-2) ; to first token inside parens
7919 (and
7920 (c-on-identifier)
7921 (c-forward-token-2)
7922 (catch 'id-list
7923 (while (eq (char-after) ?\,)
7924 (c-forward-token-2)
7925 (unless (c-on-identifier) (throw 'id-list nil))
7926 (c-forward-token-2))
7927 (eq (char-after) ?\))))))
7928
7929 ;; ...Yes. We've identified the function's argument list.
7930 (throw 'knr
7931 (progn (goto-char after-rparen)
7932 (c-forward-syntactic-ws)
7933 (point)))
7934
7935 ;; ...No. The current parens aren't the function's arg list.
7936 (goto-char before-lparen))
7937
7938 (or (c-go-list-backward) ; backwards over [ .... ]
7939 (throw 'knr nil)))))))))
7940
7941 (defun c-skip-conditional ()
7942 ;; skip forward over conditional at point, including any predicate
7943 ;; statements in parentheses. No error checking is performed.
7944 ;;
7945 ;; This function might do hidden buffer changes.
7946 (c-forward-sexp (cond
7947 ;; else if()
7948 ((looking-at (concat "\\<else"
7949 "\\([ \t\n]\\|\\\\\n\\)+"
7950 "if\\>\\([^_]\\|$\\)"))
7951 3)
7952 ;; do, else, try, finally
7953 ((looking-at (concat "\\<\\("
7954 "do\\|else\\|try\\|finally"
7955 "\\)\\>\\([^_]\\|$\\)"))
7956 1)
7957 ;; for, if, while, switch, catch, synchronized, foreach
7958 (t 2))))
7959
7960 (defun c-after-conditional (&optional lim)
7961 ;; If looking at the token after a conditional then return the
7962 ;; position of its start, otherwise return nil.
7963 ;;
7964 ;; This function might do hidden buffer changes.
7965 (save-excursion
7966 (and (zerop (c-backward-token-2 1 t lim))
7967 (or (looking-at c-block-stmt-1-key)
7968 (and (eq (char-after) ?\()
7969 (zerop (c-backward-token-2 1 t lim))
7970 (looking-at c-block-stmt-2-key)))
7971 (point))))
7972
7973 (defun c-after-special-operator-id (&optional lim)
7974 ;; If the point is after an operator identifier that isn't handled
7975 ;; like an ordinary symbol (i.e. like "operator =" in C++) then the
7976 ;; position of the start of that identifier is returned. nil is
7977 ;; returned otherwise. The point may be anywhere in the syntactic
7978 ;; whitespace after the last token of the operator identifier.
7979 ;;
7980 ;; This function might do hidden buffer changes.
7981 (save-excursion
7982 (and c-overloadable-operators-regexp
7983 (zerop (c-backward-token-2 1 nil lim))
7984 (looking-at c-overloadable-operators-regexp)
7985 (or (not c-opt-op-identifier-prefix)
7986 (and
7987 (zerop (c-backward-token-2 1 nil lim))
7988 (looking-at c-opt-op-identifier-prefix)))
7989 (point))))
7990
7991 (defsubst c-backward-to-block-anchor (&optional lim)
7992 ;; Assuming point is at a brace that opens a statement block of some
7993 ;; kind, move to the proper anchor point for that block. It might
7994 ;; need to be adjusted further by c-add-stmt-syntax, but the
7995 ;; position at return is suitable as start position for that
7996 ;; function.
7997 ;;
7998 ;; This function might do hidden buffer changes.
7999 (unless (= (point) (c-point 'boi))
8000 (let ((start (c-after-conditional lim)))
8001 (if start
8002 (goto-char start)))))
8003
8004 (defsubst c-backward-to-decl-anchor (&optional lim)
8005 ;; Assuming point is at a brace that opens the block of a top level
8006 ;; declaration of some kind, move to the proper anchor point for
8007 ;; that block.
8008 ;;
8009 ;; This function might do hidden buffer changes.
8010 (unless (= (point) (c-point 'boi))
8011 (c-beginning-of-statement-1 lim)))
8012
8013 (defun c-search-decl-header-end ()
8014 ;; Search forward for the end of the "header" of the current
8015 ;; declaration. That's the position where the definition body
8016 ;; starts, or the first variable initializer, or the ending
8017 ;; semicolon. I.e. search forward for the closest following
8018 ;; (syntactically relevant) '{', '=' or ';' token. Point is left
8019 ;; _after_ the first found token, or at point-max if none is found.
8020 ;;
8021 ;; This function might do hidden buffer changes.
8022
8023 (let ((base (point)))
8024 (if (c-major-mode-is 'c++-mode)
8025
8026 ;; In C++ we need to take special care to handle operator
8027 ;; tokens and those pesky template brackets.
8028 (while (and
8029 (c-syntactic-re-search-forward "[;{<=]" nil 'move t t)
8030 (or
8031 (c-end-of-current-token base)
8032 ;; Handle operator identifiers, i.e. ignore any
8033 ;; operator token preceded by "operator".
8034 (save-excursion
8035 (and (c-safe (c-backward-sexp) t)
8036 (looking-at c-opt-op-identifier-prefix)))
8037 (and (eq (char-before) ?<)
8038 (c-with-syntax-table c++-template-syntax-table
8039 (if (c-safe (goto-char (c-up-list-forward (point))))
8040 t
8041 (goto-char (point-max))
8042 nil)))))
8043 (setq base (point)))
8044
8045 (while (and
8046 (c-syntactic-re-search-forward "[;{=]" nil 'move t t)
8047 (c-end-of-current-token base))
8048 (setq base (point))))))
8049
8050 (defun c-beginning-of-decl-1 (&optional lim)
8051 ;; Go to the beginning of the current declaration, or the beginning
8052 ;; of the previous one if already at the start of it. Point won't
8053 ;; be moved out of any surrounding paren. Return a cons cell of the
8054 ;; form (MOVE . KNR-POS). MOVE is like the return value from
8055 ;; `c-beginning-of-statement-1'. If point skipped over some K&R
8056 ;; style argument declarations (and they are to be recognized) then
8057 ;; KNR-POS is set to the start of the first such argument
8058 ;; declaration, otherwise KNR-POS is nil. If LIM is non-nil, it's a
8059 ;; position that bounds the backward search.
8060 ;;
8061 ;; NB: Cases where the declaration continues after the block, as in
8062 ;; "struct foo { ... } bar;", are currently recognized as two
8063 ;; declarations, e.g. "struct foo { ... }" and "bar;" in this case.
8064 ;;
8065 ;; This function might do hidden buffer changes.
8066 (catch 'return
8067 (let* ((start (point))
8068 (last-stmt-start (point))
8069 (move (c-beginning-of-statement-1 lim nil t)))
8070
8071 ;; `c-beginning-of-statement-1' stops at a block start, but we
8072 ;; want to continue if the block doesn't begin a top level
8073 ;; construct, i.e. if it isn't preceded by ';', '}', ':', bob,
8074 ;; or an open paren.
8075 (let ((beg (point)) tentative-move)
8076 ;; Go back one "statement" each time round the loop until we're just
8077 ;; after a ;, }, or :, or at BOB or the start of a macro or start of
8078 ;; an ObjC method. This will move over a multiple declaration whose
8079 ;; components are comma separated.
8080 (while (and
8081 ;; Must check with c-opt-method-key in ObjC mode.
8082 (not (and c-opt-method-key
8083 (looking-at c-opt-method-key)))
8084 (/= last-stmt-start (point))
8085 (progn
8086 (c-backward-syntactic-ws lim)
8087 (not (memq (char-before) '(?\; ?} ?: nil))))
8088 (save-excursion
8089 (backward-char)
8090 (not (looking-at "\\s(")))
8091 ;; Check that we don't move from the first thing in a
8092 ;; macro to its header.
8093 (not (eq (setq tentative-move
8094 (c-beginning-of-statement-1 lim nil t))
8095 'macro)))
8096 (setq last-stmt-start beg
8097 beg (point)
8098 move tentative-move))
8099 (goto-char beg))
8100
8101 (when c-recognize-knr-p
8102 (let ((fallback-pos (point)) knr-argdecl-start)
8103 ;; Handle K&R argdecls. Back up after the "statement" jumped
8104 ;; over by `c-beginning-of-statement-1', unless it was the
8105 ;; function body, in which case we're sitting on the opening
8106 ;; brace now. Then test if we're in a K&R argdecl region and
8107 ;; that we started at the other side of the first argdecl in
8108 ;; it.
8109 (unless (eq (char-after) ?{)
8110 (goto-char last-stmt-start))
8111 (if (and (setq knr-argdecl-start (c-in-knr-argdecl lim))
8112 (< knr-argdecl-start start)
8113 (progn
8114 (goto-char knr-argdecl-start)
8115 (not (eq (c-beginning-of-statement-1 lim nil t) 'macro))))
8116 (throw 'return
8117 (cons (if (eq (char-after fallback-pos) ?{)
8118 'previous
8119 'same)
8120 knr-argdecl-start))
8121 (goto-char fallback-pos))))
8122
8123 ;; `c-beginning-of-statement-1' counts each brace block as a separate
8124 ;; statement, so the result will be 'previous if we've moved over any.
8125 ;; So change our result back to 'same if necessary.
8126 ;;
8127 ;; If they were brace list initializers we might not have moved over a
8128 ;; declaration boundary though, so change it to 'same if we've moved
8129 ;; past a '=' before '{', but not ';'. (This ought to be integrated
8130 ;; into `c-beginning-of-statement-1', so we avoid this extra pass which
8131 ;; potentially can search over a large amount of text.). Take special
8132 ;; pains not to get mislead by C++'s "operator=", and the like.
8133 (if (and (eq move 'previous)
8134 (c-with-syntax-table (if (c-major-mode-is 'c++-mode)
8135 c++-template-syntax-table
8136 (syntax-table))
8137 (save-excursion
8138 (and
8139 (progn
8140 (while ; keep going back to "[;={"s until we either find
8141 ; no more, or get to one which isn't an "operator ="
8142 (and (c-syntactic-re-search-forward "[;={]" start t t t)
8143 (eq (char-before) ?=)
8144 c-overloadable-operators-regexp
8145 c-opt-op-identifier-prefix
8146 (save-excursion
8147 (eq (c-backward-token-2) 0)
8148 (looking-at c-overloadable-operators-regexp)
8149 (eq (c-backward-token-2) 0)
8150 (looking-at c-opt-op-identifier-prefix))))
8151 (eq (char-before) ?=))
8152 (c-syntactic-re-search-forward "[;{]" start t t)
8153 (eq (char-before) ?{)
8154 (c-safe (goto-char (c-up-list-forward (point))) t)
8155 (not (c-syntactic-re-search-forward ";" start t t))))))
8156 (cons 'same nil)
8157 (cons move nil)))))
8158
8159 (defun c-end-of-decl-1 ()
8160 ;; Assuming point is at the start of a declaration (as detected by
8161 ;; e.g. `c-beginning-of-decl-1'), go to the end of it. Unlike
8162 ;; `c-beginning-of-decl-1', this function handles the case when a
8163 ;; block is followed by identifiers in e.g. struct declarations in C
8164 ;; or C++. If a proper end was found then t is returned, otherwise
8165 ;; point is moved as far as possible within the current sexp and nil
8166 ;; is returned. This function doesn't handle macros; use
8167 ;; `c-end-of-macro' instead in those cases.
8168 ;;
8169 ;; This function might do hidden buffer changes.
8170 (let ((start (point))
8171 (decl-syntax-table (if (c-major-mode-is 'c++-mode)
8172 c++-template-syntax-table
8173 (syntax-table))))
8174 (catch 'return
8175 (c-search-decl-header-end)
8176
8177 (when (and c-recognize-knr-p
8178 (eq (char-before) ?\;)
8179 (c-in-knr-argdecl start))
8180 ;; Stopped at the ';' in a K&R argdecl section which is
8181 ;; detected using the same criteria as in
8182 ;; `c-beginning-of-decl-1'. Move to the following block
8183 ;; start.
8184 (c-syntactic-re-search-forward "{" nil 'move t))
8185
8186 (when (eq (char-before) ?{)
8187 ;; Encountered a block in the declaration. Jump over it.
8188 (condition-case nil
8189 (goto-char (c-up-list-forward (point)))
8190 (error (goto-char (point-max))
8191 (throw 'return nil)))
8192 (if (or (not c-opt-block-decls-with-vars-key)
8193 (save-excursion
8194 (c-with-syntax-table decl-syntax-table
8195 (let ((lim (point)))
8196 (goto-char start)
8197 (not (and
8198 ;; Check for `c-opt-block-decls-with-vars-key'
8199 ;; before the first paren.
8200 (c-syntactic-re-search-forward
8201 (concat "[;=\(\[{]\\|\\("
8202 c-opt-block-decls-with-vars-key
8203 "\\)")
8204 lim t t t)
8205 (match-beginning 1)
8206 (not (eq (char-before) ?_))
8207 ;; Check that the first following paren is
8208 ;; the block.
8209 (c-syntactic-re-search-forward "[;=\(\[{]"
8210 lim t t t)
8211 (eq (char-before) ?{)))))))
8212 ;; The declaration doesn't have any of the
8213 ;; `c-opt-block-decls-with-vars' keywords in the
8214 ;; beginning, so it ends here at the end of the block.
8215 (throw 'return t)))
8216
8217 (c-with-syntax-table decl-syntax-table
8218 (while (progn
8219 (if (eq (char-before) ?\;)
8220 (throw 'return t))
8221 (c-syntactic-re-search-forward ";" nil 'move t))))
8222 nil)))
8223
8224 (defun c-looking-at-decl-block (containing-sexp goto-start &optional limit)
8225 ;; Assuming the point is at an open brace, check if it starts a
8226 ;; block that contains another declaration level, i.e. that isn't a
8227 ;; statement block or a brace list, and if so return non-nil.
8228 ;;
8229 ;; If the check is successful, the return value is the start of the
8230 ;; keyword that tells what kind of construct it is, i.e. typically
8231 ;; what `c-decl-block-key' matched. Also, if GOTO-START is set then
8232 ;; the point will be at the start of the construct, before any
8233 ;; leading specifiers, otherwise it's at the returned position.
8234 ;;
8235 ;; The point is clobbered if the check is unsuccessful.
8236 ;;
8237 ;; CONTAINING-SEXP is the position of the open of the surrounding
8238 ;; paren, or nil if none.
8239 ;;
8240 ;; The optional LIMIT limits the backward search for the start of
8241 ;; the construct. It's assumed to be at a syntactically relevant
8242 ;; position.
8243 ;;
8244 ;; If any template arglists are found in the searched region before
8245 ;; the open brace, they get marked with paren syntax.
8246 ;;
8247 ;; This function might do hidden buffer changes.
8248
8249 (let ((open-brace (point)) kwd-start first-specifier-pos)
8250 (c-syntactic-skip-backward c-block-prefix-charset limit t)
8251
8252 (when (and c-recognize-<>-arglists
8253 (eq (char-before) ?>))
8254 ;; Could be at the end of a template arglist.
8255 (let ((c-parse-and-markup-<>-arglists t)
8256 (c-disallow-comma-in-<>-arglists
8257 (and containing-sexp
8258 (not (eq (char-after containing-sexp) ?{)))))
8259 (while (and
8260 (c-backward-<>-arglist nil limit)
8261 (progn
8262 (c-syntactic-skip-backward c-block-prefix-charset limit t)
8263 (eq (char-before) ?>))))))
8264
8265 ;; Note: Can't get bogus hits inside template arglists below since they
8266 ;; have gotten paren syntax above.
8267 (when (and
8268 ;; If `goto-start' is set we begin by searching for the
8269 ;; first possible position of a leading specifier list.
8270 ;; The `c-decl-block-key' search continues from there since
8271 ;; we know it can't match earlier.
8272 (if goto-start
8273 (when (c-syntactic-re-search-forward c-symbol-start
8274 open-brace t t)
8275 (goto-char (setq first-specifier-pos (match-beginning 0)))
8276 t)
8277 t)
8278
8279 (cond
8280 ((c-syntactic-re-search-forward c-decl-block-key open-brace t t t)
8281 (goto-char (setq kwd-start (match-beginning 0)))
8282 (or
8283
8284 ;; Found a keyword that can't be a type?
8285 (match-beginning 1)
8286
8287 ;; Can be a type too, in which case it's the return type of a
8288 ;; function (under the assumption that no declaration level
8289 ;; block construct starts with a type).
8290 (not (c-forward-type))
8291
8292 ;; Jumped over a type, but it could be a declaration keyword
8293 ;; followed by the declared identifier that we've jumped over
8294 ;; instead (e.g. in "class Foo {"). If it indeed is a type
8295 ;; then we should be at the declarator now, so check for a
8296 ;; valid declarator start.
8297 ;;
8298 ;; Note: This doesn't cope with the case when a declared
8299 ;; identifier is followed by e.g. '(' in a language where '('
8300 ;; also might be part of a declarator expression. Currently
8301 ;; there's no such language.
8302 (not (or (looking-at c-symbol-start)
8303 (looking-at c-type-decl-prefix-key)))))
8304
8305 ;; In Pike a list of modifiers may be followed by a brace
8306 ;; to make them apply to many identifiers. Note that the
8307 ;; match data will be empty on return in this case.
8308 ((and (c-major-mode-is 'pike-mode)
8309 (progn
8310 (goto-char open-brace)
8311 (= (c-backward-token-2) 0))
8312 (looking-at c-specifier-key)
8313 ;; Use this variant to avoid yet another special regexp.
8314 (c-keyword-member (c-keyword-sym (match-string 1))
8315 'c-modifier-kwds))
8316 (setq kwd-start (point))
8317 t)))
8318
8319 ;; Got a match.
8320
8321 (if goto-start
8322 ;; Back up over any preceding specifiers and their clauses
8323 ;; by going forward from `first-specifier-pos', which is the
8324 ;; earliest possible position where the specifier list can
8325 ;; start.
8326 (progn
8327 (goto-char first-specifier-pos)
8328
8329 (while (< (point) kwd-start)
8330 (if (looking-at c-symbol-key)
8331 ;; Accept any plain symbol token on the ground that
8332 ;; it's a specifier masked through a macro (just
8333 ;; like `c-forward-decl-or-cast-1' skip forward over
8334 ;; such tokens).
8335 ;;
8336 ;; Could be more restrictive wrt invalid keywords,
8337 ;; but that'd only occur in invalid code so there's
8338 ;; no use spending effort on it.
8339 (let ((end (match-end 0)))
8340 (unless (c-forward-keyword-clause 0)
8341 (goto-char end)
8342 (c-forward-syntactic-ws)))
8343
8344 ;; Can't parse a declaration preamble and is still
8345 ;; before `kwd-start'. That means `first-specifier-pos'
8346 ;; was in some earlier construct. Search again.
8347 (if (c-syntactic-re-search-forward c-symbol-start
8348 kwd-start 'move t)
8349 (goto-char (setq first-specifier-pos (match-beginning 0)))
8350 ;; Got no preamble before the block declaration keyword.
8351 (setq first-specifier-pos kwd-start))))
8352
8353 (goto-char first-specifier-pos))
8354 (goto-char kwd-start))
8355
8356 kwd-start)))
8357
8358 (defun c-search-uplist-for-classkey (paren-state)
8359 ;; Check if the closest containing paren sexp is a declaration
8360 ;; block, returning a 2 element vector in that case. Aref 0
8361 ;; contains the bufpos at boi of the class key line, and aref 1
8362 ;; contains the bufpos of the open brace. This function is an
8363 ;; obsolete wrapper for `c-looking-at-decl-block'.
8364 ;;
8365 ;; This function might do hidden buffer changes.
8366 (let ((open-paren-pos (c-most-enclosing-brace paren-state)))
8367 (when open-paren-pos
8368 (save-excursion
8369 (goto-char open-paren-pos)
8370 (when (and (eq (char-after) ?{)
8371 (c-looking-at-decl-block
8372 (c-safe-position open-paren-pos paren-state)
8373 nil))
8374 (back-to-indentation)
8375 (vector (point) open-paren-pos))))))
8376
8377 (defmacro c-pull-open-brace (ps)
8378 ;; Pull the next open brace from PS (which has the form of paren-state),
8379 ;; skipping over any brace pairs. Returns NIL when PS is exhausted.
8380 `(progn
8381 (while (consp (car ,ps))
8382 (setq ,ps (cdr ,ps)))
8383 (prog1 (car ,ps)
8384 (setq ,ps (cdr ,ps)))))
8385
8386 (defun c-most-enclosing-decl-block (paren-state)
8387 ;; Return the buffer position of the most enclosing decl-block brace (in the
8388 ;; sense of c-looking-at-decl-block) in the PAREN-STATE structure, or nil if
8389 ;; none was found.
8390 (let* ((open-brace (c-pull-open-brace paren-state))
8391 (next-open-brace (c-pull-open-brace paren-state)))
8392 (while (and open-brace
8393 (save-excursion
8394 (goto-char open-brace)
8395 (not (c-looking-at-decl-block next-open-brace nil))))
8396 (setq open-brace next-open-brace
8397 next-open-brace (c-pull-open-brace paren-state)))
8398 open-brace))
8399
8400 (defun c-cheap-inside-bracelist-p (paren-state)
8401 ;; Return the position of the L-brace if point is inside a brace list
8402 ;; initialization of an array, etc. This is an approximate function,
8403 ;; designed for speed over accuracy. It will not find every bracelist, but
8404 ;; a non-nil result is reliable. We simply search for "= {" (naturally with
8405 ;; syntactic whitespace allowed). PAREN-STATE is the normal thing that it
8406 ;; is everywhere else.
8407 (let (b-pos)
8408 (save-excursion
8409 (while
8410 (and (setq b-pos (c-pull-open-brace paren-state))
8411 (progn (goto-char b-pos)
8412 (c-backward-sws)
8413 (c-backward-token-2)
8414 (not (looking-at "=")))))
8415 b-pos)))
8416
8417 (defun c-inside-bracelist-p (containing-sexp paren-state)
8418 ;; return the buffer position of the beginning of the brace list
8419 ;; statement if we're inside a brace list, otherwise return nil.
8420 ;; CONTAINING-SEXP is the buffer pos of the innermost containing
8421 ;; paren. PAREN-STATE is the remainder of the state of enclosing
8422 ;; braces
8423 ;;
8424 ;; N.B.: This algorithm can potentially get confused by cpp macros
8425 ;; placed in inconvenient locations. It's a trade-off we make for
8426 ;; speed.
8427 ;;
8428 ;; This function might do hidden buffer changes.
8429 (or
8430 ;; This will pick up brace list declarations.
8431 (c-safe
8432 (save-excursion
8433 (goto-char containing-sexp)
8434 (c-forward-sexp -1)
8435 (let (bracepos)
8436 (if (and (or (looking-at c-brace-list-key)
8437 (progn (c-forward-sexp -1)
8438 (looking-at c-brace-list-key)))
8439 (setq bracepos (c-down-list-forward (point)))
8440 (not (c-crosses-statement-barrier-p (point)
8441 (- bracepos 2))))
8442 (point)))))
8443 ;; this will pick up array/aggregate init lists, even if they are nested.
8444 (save-excursion
8445 (let ((class-key
8446 ;; Pike can have class definitions anywhere, so we must
8447 ;; check for the class key here.
8448 (and (c-major-mode-is 'pike-mode)
8449 c-decl-block-key))
8450 bufpos braceassignp lim next-containing)
8451 (while (and (not bufpos)
8452 containing-sexp)
8453 (when paren-state
8454 (if (consp (car paren-state))
8455 (setq lim (cdr (car paren-state))
8456 paren-state (cdr paren-state))
8457 (setq lim (car paren-state)))
8458 (when paren-state
8459 (setq next-containing (car paren-state)
8460 paren-state (cdr paren-state))))
8461 (goto-char containing-sexp)
8462 (if (c-looking-at-inexpr-block next-containing next-containing)
8463 ;; We're in an in-expression block of some kind. Do not
8464 ;; check nesting. We deliberately set the limit to the
8465 ;; containing sexp, so that c-looking-at-inexpr-block
8466 ;; doesn't check for an identifier before it.
8467 (setq containing-sexp nil)
8468 ;; see if the open brace is preceded by = or [...] in
8469 ;; this statement, but watch out for operator=
8470 (setq braceassignp 'dontknow)
8471 (c-backward-token-2 1 t lim)
8472 ;; Checks to do only on the first sexp before the brace.
8473 (when (and c-opt-inexpr-brace-list-key
8474 (eq (char-after) ?\[))
8475 ;; In Java, an initialization brace list may follow
8476 ;; directly after "new Foo[]", so check for a "new"
8477 ;; earlier.
8478 (while (eq braceassignp 'dontknow)
8479 (setq braceassignp
8480 (cond ((/= (c-backward-token-2 1 t lim) 0) nil)
8481 ((looking-at c-opt-inexpr-brace-list-key) t)
8482 ((looking-at "\\sw\\|\\s_\\|[.[]")
8483 ;; Carry on looking if this is an
8484 ;; identifier (may contain "." in Java)
8485 ;; or another "[]" sexp.
8486 'dontknow)
8487 (t nil)))))
8488 ;; Checks to do on all sexps before the brace, up to the
8489 ;; beginning of the statement.
8490 (while (eq braceassignp 'dontknow)
8491 (cond ((eq (char-after) ?\;)
8492 (setq braceassignp nil))
8493 ((and class-key
8494 (looking-at class-key))
8495 (setq braceassignp nil))
8496 ((eq (char-after) ?=)
8497 ;; We've seen a =, but must check earlier tokens so
8498 ;; that it isn't something that should be ignored.
8499 (setq braceassignp 'maybe)
8500 (while (and (eq braceassignp 'maybe)
8501 (zerop (c-backward-token-2 1 t lim)))
8502 (setq braceassignp
8503 (cond
8504 ;; Check for operator =
8505 ((and c-opt-op-identifier-prefix
8506 (looking-at c-opt-op-identifier-prefix))
8507 nil)
8508 ;; Check for `<opchar>= in Pike.
8509 ((and (c-major-mode-is 'pike-mode)
8510 (or (eq (char-after) ?`)
8511 ;; Special case for Pikes
8512 ;; `[]=, since '[' is not in
8513 ;; the punctuation class.
8514 (and (eq (char-after) ?\[)
8515 (eq (char-before) ?`))))
8516 nil)
8517 ((looking-at "\\s.") 'maybe)
8518 ;; make sure we're not in a C++ template
8519 ;; argument assignment
8520 ((and
8521 (c-major-mode-is 'c++-mode)
8522 (save-excursion
8523 (let ((here (point))
8524 (pos< (progn
8525 (skip-chars-backward "^<>")
8526 (point))))
8527 (and (eq (char-before) ?<)
8528 (not (c-crosses-statement-barrier-p
8529 pos< here))
8530 (not (c-in-literal))
8531 ))))
8532 nil)
8533 (t t))))))
8534 (if (and (eq braceassignp 'dontknow)
8535 (/= (c-backward-token-2 1 t lim) 0))
8536 (setq braceassignp nil)))
8537 (if (not braceassignp)
8538 (if (eq (char-after) ?\;)
8539 ;; Brace lists can't contain a semicolon, so we're done.
8540 (setq containing-sexp nil)
8541 ;; Go up one level.
8542 (setq containing-sexp next-containing
8543 lim nil
8544 next-containing nil))
8545 ;; we've hit the beginning of the aggregate list
8546 (c-beginning-of-statement-1
8547 (c-most-enclosing-brace paren-state))
8548 (setq bufpos (point))))
8549 )
8550 bufpos))
8551 ))
8552
8553 (defun c-looking-at-special-brace-list (&optional lim)
8554 ;; If we're looking at the start of a pike-style list, ie `({ })',
8555 ;; `([ ])', `(< >)' etc, a cons of a cons of its starting and ending
8556 ;; positions and its entry in c-special-brace-lists is returned, nil
8557 ;; otherwise. The ending position is nil if the list is still open.
8558 ;; LIM is the limit for forward search. The point may either be at
8559 ;; the `(' or at the following paren character. Tries to check the
8560 ;; matching closer, but assumes it's correct if no balanced paren is
8561 ;; found (i.e. the case `({ ... } ... )' is detected as _not_ being
8562 ;; a special brace list).
8563 ;;
8564 ;; This function might do hidden buffer changes.
8565 (if c-special-brace-lists
8566 (condition-case ()
8567 (save-excursion
8568 (let ((beg (point))
8569 inner-beg end type)
8570 (c-forward-syntactic-ws)
8571 (if (eq (char-after) ?\()
8572 (progn
8573 (forward-char 1)
8574 (c-forward-syntactic-ws)
8575 (setq inner-beg (point))
8576 (setq type (assq (char-after) c-special-brace-lists)))
8577 (if (setq type (assq (char-after) c-special-brace-lists))
8578 (progn
8579 (setq inner-beg (point))
8580 (c-backward-syntactic-ws)
8581 (forward-char -1)
8582 (setq beg (if (eq (char-after) ?\()
8583 (point)
8584 nil)))))
8585 (if (and beg type)
8586 (if (and (c-safe
8587 (goto-char beg)
8588 (c-forward-sexp 1)
8589 (setq end (point))
8590 (= (char-before) ?\)))
8591 (c-safe
8592 (goto-char inner-beg)
8593 (if (looking-at "\\s(")
8594 ;; Check balancing of the inner paren
8595 ;; below.
8596 (progn
8597 (c-forward-sexp 1)
8598 t)
8599 ;; If the inner char isn't a paren then
8600 ;; we can't check balancing, so just
8601 ;; check the char before the outer
8602 ;; closing paren.
8603 (goto-char end)
8604 (backward-char)
8605 (c-backward-syntactic-ws)
8606 (= (char-before) (cdr type)))))
8607 (if (or (/= (char-syntax (char-before)) ?\))
8608 (= (progn
8609 (c-forward-syntactic-ws)
8610 (point))
8611 (1- end)))
8612 (cons (cons beg end) type))
8613 (cons (list beg) type)))))
8614 (error nil))))
8615
8616 (defun c-looking-at-bos (&optional lim)
8617 ;; Return non-nil if between two statements or declarations, assuming
8618 ;; point is not inside a literal or comment.
8619 ;;
8620 ;; Obsolete - `c-at-statement-start-p' or `c-at-expression-start-p'
8621 ;; are recommended instead.
8622 ;;
8623 ;; This function might do hidden buffer changes.
8624 (c-at-statement-start-p))
8625 (make-obsolete 'c-looking-at-bos 'c-at-statement-start-p "22.1")
8626
8627 (defun c-looking-at-inexpr-block (lim containing-sexp &optional check-at-end)
8628 ;; Return non-nil if we're looking at the beginning of a block
8629 ;; inside an expression. The value returned is actually a cons of
8630 ;; either 'inlambda, 'inexpr-statement or 'inexpr-class and the
8631 ;; position of the beginning of the construct.
8632 ;;
8633 ;; LIM limits the backward search. CONTAINING-SEXP is the start
8634 ;; position of the closest containing list. If it's nil, the
8635 ;; containing paren isn't used to decide whether we're inside an
8636 ;; expression or not. If both LIM and CONTAINING-SEXP are used, LIM
8637 ;; needs to be farther back.
8638 ;;
8639 ;; If CHECK-AT-END is non-nil then extra checks at the end of the
8640 ;; brace block might be done. It should only be used when the
8641 ;; construct can be assumed to be complete, i.e. when the original
8642 ;; starting position was further down than that.
8643 ;;
8644 ;; This function might do hidden buffer changes.
8645
8646 (save-excursion
8647 (let ((res 'maybe) passed-paren
8648 (closest-lim (or containing-sexp lim (point-min)))
8649 ;; Look at the character after point only as a last resort
8650 ;; when we can't disambiguate.
8651 (block-follows (and (eq (char-after) ?{) (point))))
8652
8653 (while (and (eq res 'maybe)
8654 (progn (c-backward-syntactic-ws)
8655 (> (point) closest-lim))
8656 (not (bobp))
8657 (progn (backward-char)
8658 (looking-at "[\]\).]\\|\\w\\|\\s_"))
8659 (c-safe (forward-char)
8660 (goto-char (scan-sexps (point) -1))))
8661
8662 (setq res
8663 (if (looking-at c-keywords-regexp)
8664 (let ((kw-sym (c-keyword-sym (match-string 1))))
8665 (cond
8666 ((and block-follows
8667 (c-keyword-member kw-sym 'c-inexpr-class-kwds))
8668 (and (not (eq passed-paren ?\[))
8669 (or (not (looking-at c-class-key))
8670 ;; If the class definition is at the start of
8671 ;; a statement, we don't consider it an
8672 ;; in-expression class.
8673 (let ((prev (point)))
8674 (while (and
8675 (= (c-backward-token-2 1 nil closest-lim) 0)
8676 (eq (char-syntax (char-after)) ?w))
8677 (setq prev (point)))
8678 (goto-char prev)
8679 (not (c-at-statement-start-p)))
8680 ;; Also, in Pike we treat it as an
8681 ;; in-expression class if it's used in an
8682 ;; object clone expression.
8683 (save-excursion
8684 (and check-at-end
8685 (c-major-mode-is 'pike-mode)
8686 (progn (goto-char block-follows)
8687 (zerop (c-forward-token-2 1 t)))
8688 (eq (char-after) ?\())))
8689 (cons 'inexpr-class (point))))
8690 ((c-keyword-member kw-sym 'c-inexpr-block-kwds)
8691 (when (not passed-paren)
8692 (cons 'inexpr-statement (point))))
8693 ((c-keyword-member kw-sym 'c-lambda-kwds)
8694 (when (or (not passed-paren)
8695 (eq passed-paren ?\())
8696 (cons 'inlambda (point))))
8697 ((c-keyword-member kw-sym 'c-block-stmt-kwds)
8698 nil)
8699 (t
8700 'maybe)))
8701
8702 (if (looking-at "\\s(")
8703 (if passed-paren
8704 (if (and (eq passed-paren ?\[)
8705 (eq (char-after) ?\[))
8706 ;; Accept several square bracket sexps for
8707 ;; Java array initializations.
8708 'maybe)
8709 (setq passed-paren (char-after))
8710 'maybe)
8711 'maybe))))
8712
8713 (if (eq res 'maybe)
8714 (when (and c-recognize-paren-inexpr-blocks
8715 block-follows
8716 containing-sexp
8717 (eq (char-after containing-sexp) ?\())
8718 (goto-char containing-sexp)
8719 (if (or (save-excursion
8720 (c-backward-syntactic-ws lim)
8721 (and (> (point) (or lim (point-min)))
8722 (c-on-identifier)))
8723 (and c-special-brace-lists
8724 (c-looking-at-special-brace-list)))
8725 nil
8726 (cons 'inexpr-statement (point))))
8727
8728 res))))
8729
8730 (defun c-looking-at-inexpr-block-backward (paren-state)
8731 ;; Returns non-nil if we're looking at the end of an in-expression
8732 ;; block, otherwise the same as `c-looking-at-inexpr-block'.
8733 ;; PAREN-STATE is the paren state relevant at the current position.
8734 ;;
8735 ;; This function might do hidden buffer changes.
8736 (save-excursion
8737 ;; We currently only recognize a block.
8738 (let ((here (point))
8739 (elem (car-safe paren-state))
8740 containing-sexp)
8741 (when (and (consp elem)
8742 (progn (goto-char (cdr elem))
8743 (c-forward-syntactic-ws here)
8744 (= (point) here)))
8745 (goto-char (car elem))
8746 (if (setq paren-state (cdr paren-state))
8747 (setq containing-sexp (car-safe paren-state)))
8748 (c-looking-at-inexpr-block (c-safe-position containing-sexp
8749 paren-state)
8750 containing-sexp)))))
8751
8752 (defun c-at-macro-vsemi-p (&optional pos)
8753 ;; Is there a "virtual semicolon" at POS or point?
8754 ;; (See cc-defs.el for full details of "virtual semicolons".)
8755 ;;
8756 ;; This is true when point is at the last non syntactic WS position on the
8757 ;; line, there is a macro call last on the line, and this particular macro's
8758 ;; name is defined by the regexp `c-vs-macro-regexp' as not needing a
8759 ;; semicolon.
8760 (save-excursion
8761 (save-restriction
8762 (widen)
8763 (if pos
8764 (goto-char pos)
8765 (setq pos (point)))
8766 (and
8767 c-macro-with-semi-re
8768 (eq (skip-chars-backward " \t") 0)
8769
8770 ;; Check we've got nothing after this except comments and empty lines
8771 ;; joined by escaped EOLs.
8772 (skip-chars-forward " \t") ; always returns non-nil.
8773 (progn
8774 (while ; go over 1 block comment per iteration.
8775 (and
8776 (looking-at "\\(\\\\[\n\r][ \t]*\\)*")
8777 (goto-char (match-end 0))
8778 (cond
8779 ((looking-at c-block-comment-start-regexp)
8780 (and (forward-comment 1)
8781 (skip-chars-forward " \t"))) ; always returns non-nil
8782 ((looking-at c-line-comment-start-regexp)
8783 (end-of-line)
8784 nil)
8785 (t nil))))
8786 (eolp))
8787
8788 (goto-char pos)
8789 (progn (c-backward-syntactic-ws)
8790 (eq (point) pos))
8791
8792 ;; Check for one of the listed macros being before point.
8793 (or (not (eq (char-before) ?\)))
8794 (when (c-go-list-backward)
8795 (c-backward-syntactic-ws)
8796 t))
8797 (c-simple-skip-symbol-backward)
8798 (looking-at c-macro-with-semi-re)
8799 (goto-char pos)
8800 (not (c-in-literal)))))) ; The most expensive check last.
8801
8802 (defun c-macro-vsemi-status-unknown-p () t) ; See cc-defs.el.
8803
8804 \f
8805 ;; `c-guess-basic-syntax' and the functions that precedes it below
8806 ;; implements the main decision tree for determining the syntactic
8807 ;; analysis of the current line of code.
8808
8809 ;; Dynamically bound to t when `c-guess-basic-syntax' is called during
8810 ;; auto newline analysis.
8811 (defvar c-auto-newline-analysis nil)
8812
8813 (defun c-brace-anchor-point (bracepos)
8814 ;; BRACEPOS is the position of a brace in a construct like "namespace
8815 ;; Bar {". Return the anchor point in this construct; this is the
8816 ;; earliest symbol on the brace's line which isn't earlier than
8817 ;; "namespace".
8818 ;;
8819 ;; Currently (2007-08-17), "like namespace" means "matches
8820 ;; c-other-block-decl-kwds". It doesn't work with "class" or "struct"
8821 ;; or anything like that.
8822 (save-excursion
8823 (let ((boi (c-point 'boi bracepos)))
8824 (goto-char bracepos)
8825 (while (and (> (point) boi)
8826 (not (looking-at c-other-decl-block-key)))
8827 (c-backward-token-2))
8828 (if (> (point) boi) (point) boi))))
8829
8830 (defsubst c-add-syntax (symbol &rest args)
8831 ;; A simple function to prepend a new syntax element to
8832 ;; `c-syntactic-context'. Using `setq' on it is unsafe since it
8833 ;; should always be dynamically bound but since we read it first
8834 ;; we'll fail properly anyway if this function is misused.
8835 (setq c-syntactic-context (cons (cons symbol args)
8836 c-syntactic-context)))
8837
8838 (defsubst c-append-syntax (symbol &rest args)
8839 ;; Like `c-add-syntax' but appends to the end of the syntax list.
8840 ;; (Normally not necessary.)
8841 (setq c-syntactic-context (nconc c-syntactic-context
8842 (list (cons symbol args)))))
8843
8844 (defun c-add-stmt-syntax (syntax-symbol
8845 syntax-extra-args
8846 stop-at-boi-only
8847 containing-sexp
8848 paren-state)
8849 ;; Add the indicated SYNTAX-SYMBOL to `c-syntactic-context', extending it as
8850 ;; needed with further syntax elements of the types `substatement',
8851 ;; `inexpr-statement', `arglist-cont-nonempty', `statement-block-intro', and
8852 ;; `defun-block-intro'.
8853 ;;
8854 ;; Do the generic processing to anchor the given syntax symbol on
8855 ;; the preceding statement: Skip over any labels and containing
8856 ;; statements on the same line, and then search backward until we
8857 ;; find a statement or block start that begins at boi without a
8858 ;; label or comment.
8859 ;;
8860 ;; Point is assumed to be at the prospective anchor point for the
8861 ;; given SYNTAX-SYMBOL. More syntax entries are added if we need to
8862 ;; skip past open parens and containing statements. Most of the added
8863 ;; syntax elements will get the same anchor point - the exception is
8864 ;; for an anchor in a construct like "namespace"[*] - this is as early
8865 ;; as possible in the construct but on the same line as the {.
8866 ;;
8867 ;; [*] i.e. with a keyword matching c-other-block-decl-kwds.
8868 ;;
8869 ;; SYNTAX-EXTRA-ARGS are a list of the extra arguments for the
8870 ;; syntax symbol. They are appended after the anchor point.
8871 ;;
8872 ;; If STOP-AT-BOI-ONLY is nil, we can stop in the middle of the line
8873 ;; if the current statement starts there.
8874 ;;
8875 ;; Note: It's not a problem if PAREN-STATE "overshoots"
8876 ;; CONTAINING-SEXP, i.e. contains info about parens further down.
8877 ;;
8878 ;; This function might do hidden buffer changes.
8879
8880 (if (= (point) (c-point 'boi))
8881 ;; This is by far the most common case, so let's give it special
8882 ;; treatment.
8883 (apply 'c-add-syntax syntax-symbol (point) syntax-extra-args)
8884
8885 (let ((syntax-last c-syntactic-context)
8886 (boi (c-point 'boi))
8887 ;; Set when we're on a label, so that we don't stop there.
8888 ;; FIXME: To be complete we should check if we're on a label
8889 ;; now at the start.
8890 on-label)
8891
8892 ;; Use point as the anchor point for "namespace", "extern", etc.
8893 (apply 'c-add-syntax syntax-symbol
8894 (if (rassq syntax-symbol c-other-decl-block-key-in-symbols-alist)
8895 (point) nil)
8896 syntax-extra-args)
8897
8898 ;; Loop while we have to back out of containing blocks.
8899 (while
8900 (and
8901 (catch 'back-up-block
8902
8903 ;; Loop while we have to back up statements.
8904 (while (or (/= (point) boi)
8905 on-label
8906 (looking-at c-comment-start-regexp))
8907
8908 ;; Skip past any comments that stands between the
8909 ;; statement start and boi.
8910 (let ((savepos (point)))
8911 (while (and (/= savepos boi)
8912 (c-backward-single-comment))
8913 (setq savepos (point)
8914 boi (c-point 'boi)))
8915 (goto-char savepos))
8916
8917 ;; Skip to the beginning of this statement or backward
8918 ;; another one.
8919 (let ((old-pos (point))
8920 (old-boi boi)
8921 (step-type (c-beginning-of-statement-1 containing-sexp)))
8922 (setq boi (c-point 'boi)
8923 on-label (eq step-type 'label))
8924
8925 (cond ((= (point) old-pos)
8926 ;; If we didn't move we're at the start of a block and
8927 ;; have to continue outside it.
8928 (throw 'back-up-block t))
8929
8930 ((and (eq step-type 'up)
8931 (>= (point) old-boi)
8932 (looking-at "else\\>[^_]")
8933 (save-excursion
8934 (goto-char old-pos)
8935 (looking-at "if\\>[^_]")))
8936 ;; Special case to avoid deeper and deeper indentation
8937 ;; of "else if" clauses.
8938 )
8939
8940 ((and (not stop-at-boi-only)
8941 (/= old-pos old-boi)
8942 (memq step-type '(up previous)))
8943 ;; If stop-at-boi-only is nil, we shouldn't back up
8944 ;; over previous or containing statements to try to
8945 ;; reach boi, so go back to the last position and
8946 ;; exit.
8947 (goto-char old-pos)
8948 (throw 'back-up-block nil))
8949
8950 (t
8951 (if (and (not stop-at-boi-only)
8952 (memq step-type '(up previous beginning)))
8953 ;; If we've moved into another statement then we
8954 ;; should no longer try to stop in the middle of a
8955 ;; line.
8956 (setq stop-at-boi-only t))
8957
8958 ;; Record this as a substatement if we skipped up one
8959 ;; level.
8960 (when (eq step-type 'up)
8961 (c-add-syntax 'substatement nil))))
8962 )))
8963
8964 containing-sexp)
8965
8966 ;; Now we have to go out of this block.
8967 (goto-char containing-sexp)
8968
8969 ;; Don't stop in the middle of a special brace list opener
8970 ;; like "({".
8971 (when c-special-brace-lists
8972 (let ((special-list (c-looking-at-special-brace-list)))
8973 (when (and special-list
8974 (< (car (car special-list)) (point)))
8975 (setq containing-sexp (car (car special-list)))
8976 (goto-char containing-sexp))))
8977
8978 (setq paren-state (c-whack-state-after containing-sexp paren-state)
8979 containing-sexp (c-most-enclosing-brace paren-state)
8980 boi (c-point 'boi))
8981
8982 ;; Analyze the construct in front of the block we've stepped out
8983 ;; from and add the right syntactic element for it.
8984 (let ((paren-pos (point))
8985 (paren-char (char-after))
8986 step-type)
8987
8988 (if (eq paren-char ?\()
8989 ;; Stepped out of a parenthesis block, so we're in an
8990 ;; expression now.
8991 (progn
8992 (when (/= paren-pos boi)
8993 (if (and c-recognize-paren-inexpr-blocks
8994 (progn
8995 (c-backward-syntactic-ws containing-sexp)
8996 (or (not (looking-at "\\>"))
8997 (not (c-on-identifier))))
8998 (save-excursion
8999 (goto-char (1+ paren-pos))
9000 (c-forward-syntactic-ws)
9001 (eq (char-after) ?{)))
9002 ;; Stepped out of an in-expression statement. This
9003 ;; syntactic element won't get an anchor pos.
9004 (c-add-syntax 'inexpr-statement)
9005
9006 ;; A parenthesis normally belongs to an arglist.
9007 (c-add-syntax 'arglist-cont-nonempty nil paren-pos)))
9008
9009 (goto-char (max boi
9010 (if containing-sexp
9011 (1+ containing-sexp)
9012 (point-min))))
9013 (setq step-type 'same
9014 on-label nil))
9015
9016 ;; Stepped out of a brace block.
9017 (setq step-type (c-beginning-of-statement-1 containing-sexp)
9018 on-label (eq step-type 'label))
9019
9020 (if (and (eq step-type 'same)
9021 (/= paren-pos (point)))
9022 (let (inexpr)
9023 (cond
9024 ((save-excursion
9025 (goto-char paren-pos)
9026 (setq inexpr (c-looking-at-inexpr-block
9027 (c-safe-position containing-sexp paren-state)
9028 containing-sexp)))
9029 (c-add-syntax (if (eq (car inexpr) 'inlambda)
9030 'defun-block-intro
9031 'statement-block-intro)
9032 nil))
9033 ((looking-at c-other-decl-block-key)
9034 (c-add-syntax
9035 (cdr (assoc (match-string 1)
9036 c-other-decl-block-key-in-symbols-alist))
9037 (max (c-point 'boi paren-pos) (point))))
9038 (t (c-add-syntax 'defun-block-intro nil))))
9039
9040 (c-add-syntax 'statement-block-intro nil)))
9041
9042 (if (= paren-pos boi)
9043 ;; Always done if the open brace was at boi. The
9044 ;; c-beginning-of-statement-1 call above is necessary
9045 ;; anyway, to decide the type of block-intro to add.
9046 (goto-char paren-pos)
9047 (setq boi (c-point 'boi)))
9048 ))
9049
9050 ;; Fill in the current point as the anchor for all the symbols
9051 ;; added above.
9052 (let ((p c-syntactic-context) q)
9053 (while (not (eq p syntax-last))
9054 (setq q (cdr (car p))) ; e.g. (nil 28) [from (arglist-cont-nonempty nil 28)]
9055 (while q
9056 (unless (car q)
9057 (setcar q (point)))
9058 (setq q (cdr q)))
9059 (setq p (cdr p))))
9060 )))
9061
9062 (defun c-add-class-syntax (symbol
9063 containing-decl-open
9064 containing-decl-start
9065 containing-decl-kwd
9066 paren-state)
9067 ;; The inclass and class-close syntactic symbols are added in
9068 ;; several places and some work is needed to fix everything.
9069 ;; Therefore it's collected here.
9070 ;;
9071 ;; This function might do hidden buffer changes.
9072 (goto-char containing-decl-open)
9073 (if (and (eq symbol 'inclass) (= (point) (c-point 'boi)))
9074 (progn
9075 (c-add-syntax symbol containing-decl-open)
9076 containing-decl-open)
9077 (goto-char containing-decl-start)
9078 ;; Ought to use `c-add-stmt-syntax' instead of backing up to boi
9079 ;; here, but we have to do like this for compatibility.
9080 (back-to-indentation)
9081 (c-add-syntax symbol (point))
9082 (if (and (c-keyword-member containing-decl-kwd
9083 'c-inexpr-class-kwds)
9084 (/= containing-decl-start (c-point 'boi containing-decl-start)))
9085 (c-add-syntax 'inexpr-class))
9086 (point)))
9087
9088 (defun c-guess-continued-construct (indent-point
9089 char-after-ip
9090 beg-of-same-or-containing-stmt
9091 containing-sexp
9092 paren-state)
9093 ;; This function contains the decision tree reached through both
9094 ;; cases 18 and 10. It's a continued statement or top level
9095 ;; construct of some kind.
9096 ;;
9097 ;; This function might do hidden buffer changes.
9098
9099 (let (special-brace-list placeholder)
9100 (goto-char indent-point)
9101 (skip-chars-forward " \t")
9102
9103 (cond
9104 ;; (CASE A removed.)
9105 ;; CASE B: open braces for class or brace-lists
9106 ((setq special-brace-list
9107 (or (and c-special-brace-lists
9108 (c-looking-at-special-brace-list))
9109 (eq char-after-ip ?{)))
9110
9111 (cond
9112 ;; CASE B.1: class-open
9113 ((save-excursion
9114 (and (eq (char-after) ?{)
9115 (c-looking-at-decl-block containing-sexp t)
9116 (setq beg-of-same-or-containing-stmt (point))))
9117 (c-add-syntax 'class-open beg-of-same-or-containing-stmt))
9118
9119 ;; CASE B.2: brace-list-open
9120 ((or (consp special-brace-list)
9121 (save-excursion
9122 (goto-char beg-of-same-or-containing-stmt)
9123 (c-syntactic-re-search-forward "=\\([^=]\\|$\\)"
9124 indent-point t t t)))
9125 ;; The most semantically accurate symbol here is
9126 ;; brace-list-open, but we normally report it simply as a
9127 ;; statement-cont. The reason is that one normally adjusts
9128 ;; brace-list-open for brace lists as top-level constructs,
9129 ;; and brace lists inside statements is a completely different
9130 ;; context. C.f. case 5A.3.
9131 (c-beginning-of-statement-1 containing-sexp)
9132 (c-add-stmt-syntax (if c-auto-newline-analysis
9133 ;; Turn off the dwim above when we're
9134 ;; analyzing the nature of the brace
9135 ;; for the auto newline feature.
9136 'brace-list-open
9137 'statement-cont)
9138 nil nil
9139 containing-sexp paren-state))
9140
9141 ;; CASE B.3: The body of a function declared inside a normal
9142 ;; block. Can occur e.g. in Pike and when using gcc
9143 ;; extensions, but watch out for macros followed by blocks.
9144 ;; C.f. cases E, 16F and 17G.
9145 ((and (not (c-at-statement-start-p))
9146 (eq (c-beginning-of-statement-1 containing-sexp nil nil t)
9147 'same)
9148 (save-excursion
9149 (let ((c-recognize-typeless-decls nil))
9150 ;; Turn off recognition of constructs that lacks a
9151 ;; type in this case, since that's more likely to be
9152 ;; a macro followed by a block.
9153 (c-forward-decl-or-cast-1 (c-point 'bosws) nil nil))))
9154 (c-add-stmt-syntax 'defun-open nil t
9155 containing-sexp paren-state))
9156
9157 ;; CASE B.4: Continued statement with block open. The most
9158 ;; accurate analysis is perhaps `statement-cont' together with
9159 ;; `block-open' but we play DWIM and use `substatement-open'
9160 ;; instead. The rationale is that this typically is a macro
9161 ;; followed by a block which makes it very similar to a
9162 ;; statement with a substatement block.
9163 (t
9164 (c-add-stmt-syntax 'substatement-open nil nil
9165 containing-sexp paren-state))
9166 ))
9167
9168 ;; CASE C: iostream insertion or extraction operator
9169 ((and (looking-at "\\(<<\\|>>\\)\\([^=]\\|$\\)")
9170 (save-excursion
9171 (goto-char beg-of-same-or-containing-stmt)
9172 ;; If there is no preceding streamop in the statement
9173 ;; then indent this line as a normal statement-cont.
9174 (when (c-syntactic-re-search-forward
9175 "\\(<<\\|>>\\)\\([^=]\\|$\\)" indent-point 'move t t)
9176 (c-add-syntax 'stream-op (c-point 'boi))
9177 t))))
9178
9179 ;; CASE E: In the "K&R region" of a function declared inside a
9180 ;; normal block. C.f. case B.3.
9181 ((and (save-excursion
9182 ;; Check that the next token is a '{'. This works as
9183 ;; long as no language that allows nested function
9184 ;; definitions allows stuff like member init lists, K&R
9185 ;; declarations or throws clauses there.
9186 ;;
9187 ;; Note that we do a forward search for something ahead
9188 ;; of the indentation line here. That's not good since
9189 ;; the user might not have typed it yet. Unfortunately
9190 ;; it's exceedingly tricky to recognize a function
9191 ;; prototype in a code block without resorting to this.
9192 (c-forward-syntactic-ws)
9193 (eq (char-after) ?{))
9194 (not (c-at-statement-start-p))
9195 (eq (c-beginning-of-statement-1 containing-sexp nil nil t)
9196 'same)
9197 (save-excursion
9198 (let ((c-recognize-typeless-decls nil))
9199 ;; Turn off recognition of constructs that lacks a
9200 ;; type in this case, since that's more likely to be
9201 ;; a macro followed by a block.
9202 (c-forward-decl-or-cast-1 (c-point 'bosws) nil nil))))
9203 (c-add-stmt-syntax 'func-decl-cont nil t
9204 containing-sexp paren-state))
9205
9206 ;;CASE F: continued statement and the only preceding items are
9207 ;;annotations.
9208 ((and (c-major-mode-is 'java-mode)
9209 (setq placeholder (point))
9210 (c-beginning-of-statement-1)
9211 (progn
9212 (while (and (c-forward-annotation)
9213 (< (point) placeholder))
9214 (c-forward-syntactic-ws))
9215 t)
9216 (prog1
9217 (>= (point) placeholder)
9218 (goto-char placeholder)))
9219 (c-beginning-of-statement-1 containing-sexp)
9220 (c-add-syntax 'annotation-var-cont (point)))
9221
9222 ;; CASE G: a template list continuation?
9223 ;; Mostly a duplication of case 5D.3 to fix templates-19:
9224 ((and (c-major-mode-is 'c++-mode)
9225 (save-excursion
9226 (goto-char indent-point)
9227 (c-with-syntax-table c++-template-syntax-table
9228 (setq placeholder (c-up-list-backward)))
9229 (and placeholder
9230 (eq (char-after placeholder) ?<)
9231 (/= (char-before placeholder) ?<)
9232 (progn
9233 (goto-char (1+ placeholder))
9234 (not (looking-at c-<-op-cont-regexp))))))
9235 (c-with-syntax-table c++-template-syntax-table
9236 (goto-char placeholder)
9237 (c-beginning-of-statement-1 containing-sexp t)
9238 (if (save-excursion
9239 (c-backward-syntactic-ws containing-sexp)
9240 (eq (char-before) ?<))
9241 ;; In a nested template arglist.
9242 (progn
9243 (goto-char placeholder)
9244 (c-syntactic-skip-backward "^,;" containing-sexp t)
9245 (c-forward-syntactic-ws))
9246 (back-to-indentation)))
9247 ;; FIXME: Should use c-add-stmt-syntax, but it's not yet
9248 ;; template aware.
9249 (c-add-syntax 'template-args-cont (point) placeholder))
9250
9251 ;; CASE D: continued statement.
9252 (t
9253 (c-beginning-of-statement-1 containing-sexp)
9254 (c-add-stmt-syntax 'statement-cont nil nil
9255 containing-sexp paren-state))
9256 )))
9257
9258 ;; The next autoload was added by RMS on 2005/8/9 - don't know why (ACM,
9259 ;; 2005/11/29).
9260 ;;;###autoload
9261 (defun c-guess-basic-syntax ()
9262 "Return the syntactic context of the current line."
9263 (save-excursion
9264 (beginning-of-line)
9265 (c-save-buffer-state
9266 ((indent-point (point))
9267 (case-fold-search nil)
9268 ;; A whole ugly bunch of various temporary variables. Have
9269 ;; to declare them here since it's not possible to declare
9270 ;; a variable with only the scope of a cond test and the
9271 ;; following result clauses, and most of this function is a
9272 ;; single gigantic cond. :P
9273 literal char-before-ip before-ws-ip char-after-ip macro-start
9274 in-macro-expr c-syntactic-context placeholder c-in-literal-cache
9275 step-type tmpsymbol keyword injava-inher special-brace-list tmp-pos
9276 containing-<
9277 ;; The following record some positions for the containing
9278 ;; declaration block if we're directly within one:
9279 ;; `containing-decl-open' is the position of the open
9280 ;; brace. `containing-decl-start' is the start of the
9281 ;; declaration. `containing-decl-kwd' is the keyword
9282 ;; symbol of the keyword that tells what kind of block it
9283 ;; is.
9284 containing-decl-open
9285 containing-decl-start
9286 containing-decl-kwd
9287 ;; The open paren of the closest surrounding sexp or nil if
9288 ;; there is none.
9289 containing-sexp
9290 ;; The position after the closest preceding brace sexp
9291 ;; (nested sexps are ignored), or the position after
9292 ;; `containing-sexp' if there is none, or (point-min) if
9293 ;; `containing-sexp' is nil.
9294 lim
9295 ;; The paren state outside `containing-sexp', or at
9296 ;; `indent-point' if `containing-sexp' is nil.
9297 (paren-state (c-parse-state))
9298 ;; There's always at most one syntactic element which got
9299 ;; an anchor pos. It's stored in syntactic-relpos.
9300 syntactic-relpos
9301 (c-stmt-delim-chars c-stmt-delim-chars))
9302
9303 ;; Check if we're directly inside an enclosing declaration
9304 ;; level block.
9305 (when (and (setq containing-sexp
9306 (c-most-enclosing-brace paren-state))
9307 (progn
9308 (goto-char containing-sexp)
9309 (eq (char-after) ?{))
9310 (setq placeholder
9311 (c-looking-at-decl-block
9312 (c-most-enclosing-brace paren-state
9313 containing-sexp)
9314 t)))
9315 (setq containing-decl-open containing-sexp
9316 containing-decl-start (point)
9317 containing-sexp nil)
9318 (goto-char placeholder)
9319 (setq containing-decl-kwd (and (looking-at c-keywords-regexp)
9320 (c-keyword-sym (match-string 1)))))
9321
9322 ;; Init some position variables.
9323 (if c-state-cache
9324 (progn
9325 (setq containing-sexp (car paren-state)
9326 paren-state (cdr paren-state))
9327 (if (consp containing-sexp)
9328 (progn
9329 (setq lim (cdr containing-sexp))
9330 (if (cdr c-state-cache)
9331 ;; Ignore balanced paren. The next entry
9332 ;; can't be another one.
9333 (setq containing-sexp (car (cdr c-state-cache))
9334 paren-state (cdr paren-state))
9335 ;; If there is no surrounding open paren then
9336 ;; put the last balanced pair back on paren-state.
9337 (setq paren-state (cons containing-sexp paren-state)
9338 containing-sexp nil)))
9339 (setq lim (1+ containing-sexp))))
9340 (setq lim (point-min)))
9341 (when (c-beginning-of-macro)
9342 (goto-char indent-point)
9343 (let ((lim1 (c-determine-limit 2000)))
9344 (setq lim (max lim lim1))))
9345
9346 ;; If we're in a parenthesis list then ',' delimits the
9347 ;; "statements" rather than being an operator (with the
9348 ;; exception of the "for" clause). This difference is
9349 ;; typically only noticeable when statements are used in macro
9350 ;; arglists.
9351 (when (and containing-sexp
9352 (eq (char-after containing-sexp) ?\())
9353 (setq c-stmt-delim-chars c-stmt-delim-chars-with-comma))
9354 ;; cache char before and after indent point, and move point to
9355 ;; the most likely position to perform the majority of tests
9356 (goto-char indent-point)
9357 (c-backward-syntactic-ws lim)
9358 (setq before-ws-ip (point)
9359 char-before-ip (char-before))
9360 (goto-char indent-point)
9361 (skip-chars-forward " \t")
9362 (setq char-after-ip (char-after))
9363
9364 ;; are we in a literal?
9365 (setq literal (c-in-literal lim))
9366
9367 ;; now figure out syntactic qualities of the current line
9368 (cond
9369
9370 ;; CASE 1: in a string.
9371 ((eq literal 'string)
9372 (c-add-syntax 'string (c-point 'bopl)))
9373
9374 ;; CASE 2: in a C or C++ style comment.
9375 ((and (memq literal '(c c++))
9376 ;; This is a kludge for XEmacs where we use
9377 ;; `buffer-syntactic-context', which doesn't correctly
9378 ;; recognize "\*/" to end a block comment.
9379 ;; `parse-partial-sexp' which is used by
9380 ;; `c-literal-limits' will however do that in most
9381 ;; versions, which results in that we get nil from
9382 ;; `c-literal-limits' even when `c-in-literal' claims
9383 ;; we're inside a comment.
9384 (setq placeholder (c-literal-limits lim)))
9385 (c-add-syntax literal (car placeholder)))
9386
9387 ;; CASE 3: in a cpp preprocessor macro continuation.
9388 ((and (save-excursion
9389 (when (c-beginning-of-macro)
9390 (setq macro-start (point))))
9391 (/= macro-start (c-point 'boi))
9392 (progn
9393 (setq tmpsymbol 'cpp-macro-cont)
9394 (or (not c-syntactic-indentation-in-macros)
9395 (save-excursion
9396 (goto-char macro-start)
9397 ;; If at the beginning of the body of a #define
9398 ;; directive then analyze as cpp-define-intro
9399 ;; only. Go on with the syntactic analysis
9400 ;; otherwise. in-macro-expr is set if we're in a
9401 ;; cpp expression, i.e. before the #define body
9402 ;; or anywhere in a non-#define directive.
9403 (if (c-forward-to-cpp-define-body)
9404 (let ((indent-boi (c-point 'boi indent-point)))
9405 (setq in-macro-expr (> (point) indent-boi)
9406 tmpsymbol 'cpp-define-intro)
9407 (= (point) indent-boi))
9408 (setq in-macro-expr t)
9409 nil)))))
9410 (c-add-syntax tmpsymbol macro-start)
9411 (setq macro-start nil))
9412
9413 ;; CASE 11: an else clause?
9414 ((looking-at "else\\>[^_]")
9415 (c-beginning-of-statement-1 containing-sexp)
9416 (c-add-stmt-syntax 'else-clause nil t
9417 containing-sexp paren-state))
9418
9419 ;; CASE 12: while closure of a do/while construct?
9420 ((and (looking-at "while\\>[^_]")
9421 (save-excursion
9422 (prog1 (eq (c-beginning-of-statement-1 containing-sexp)
9423 'beginning)
9424 (setq placeholder (point)))))
9425 (goto-char placeholder)
9426 (c-add-stmt-syntax 'do-while-closure nil t
9427 containing-sexp paren-state))
9428
9429 ;; CASE 13: A catch or finally clause? This case is simpler
9430 ;; than if-else and do-while, because a block is required
9431 ;; after every try, catch and finally.
9432 ((save-excursion
9433 (and (cond ((c-major-mode-is 'c++-mode)
9434 (looking-at "catch\\>[^_]"))
9435 ((c-major-mode-is 'java-mode)
9436 (looking-at "\\(catch\\|finally\\)\\>[^_]")))
9437 (and (c-safe (c-backward-syntactic-ws)
9438 (c-backward-sexp)
9439 t)
9440 (eq (char-after) ?{)
9441 (c-safe (c-backward-syntactic-ws)
9442 (c-backward-sexp)
9443 t)
9444 (if (eq (char-after) ?\()
9445 (c-safe (c-backward-sexp) t)
9446 t))
9447 (looking-at "\\(try\\|catch\\)\\>[^_]")
9448 (setq placeholder (point))))
9449 (goto-char placeholder)
9450 (c-add-stmt-syntax 'catch-clause nil t
9451 containing-sexp paren-state))
9452
9453 ;; CASE 18: A substatement we can recognize by keyword.
9454 ((save-excursion
9455 (and c-opt-block-stmt-key
9456 (not (eq char-before-ip ?\;))
9457 (not (c-at-vsemi-p before-ws-ip))
9458 (not (memq char-after-ip '(?\) ?\] ?,)))
9459 (or (not (eq char-before-ip ?}))
9460 (c-looking-at-inexpr-block-backward c-state-cache))
9461 (> (point)
9462 (progn
9463 ;; Ought to cache the result from the
9464 ;; c-beginning-of-statement-1 calls here.
9465 (setq placeholder (point))
9466 (while (eq (setq step-type
9467 (c-beginning-of-statement-1 lim))
9468 'label))
9469 (if (eq step-type 'previous)
9470 (goto-char placeholder)
9471 (setq placeholder (point))
9472 (if (and (eq step-type 'same)
9473 (not (looking-at c-opt-block-stmt-key)))
9474 ;; Step up to the containing statement if we
9475 ;; stayed in the same one.
9476 (let (step)
9477 (while (eq
9478 (setq step
9479 (c-beginning-of-statement-1 lim))
9480 'label))
9481 (if (eq step 'up)
9482 (setq placeholder (point))
9483 ;; There was no containing statement after all.
9484 (goto-char placeholder)))))
9485 placeholder))
9486 (if (looking-at c-block-stmt-2-key)
9487 ;; Require a parenthesis after these keywords.
9488 ;; Necessary to catch e.g. synchronized in Java,
9489 ;; which can be used both as statement and
9490 ;; modifier.
9491 (and (zerop (c-forward-token-2 1 nil))
9492 (eq (char-after) ?\())
9493 (looking-at c-opt-block-stmt-key))))
9494
9495 (if (eq step-type 'up)
9496 ;; CASE 18A: Simple substatement.
9497 (progn
9498 (goto-char placeholder)
9499 (cond
9500 ((eq char-after-ip ?{)
9501 (c-add-stmt-syntax 'substatement-open nil nil
9502 containing-sexp paren-state))
9503 ((save-excursion
9504 (goto-char indent-point)
9505 (back-to-indentation)
9506 (c-forward-label))
9507 (c-add-stmt-syntax 'substatement-label nil nil
9508 containing-sexp paren-state))
9509 (t
9510 (c-add-stmt-syntax 'substatement nil nil
9511 containing-sexp paren-state))))
9512
9513 ;; CASE 18B: Some other substatement. This is shared
9514 ;; with case 10.
9515 (c-guess-continued-construct indent-point
9516 char-after-ip
9517 placeholder
9518 lim
9519 paren-state)))
9520
9521 ;; CASE 14: A case or default label
9522 ((looking-at c-label-kwds-regexp)
9523 (if containing-sexp
9524 (progn
9525 (goto-char containing-sexp)
9526 (setq lim (c-most-enclosing-brace c-state-cache
9527 containing-sexp))
9528 (c-backward-to-block-anchor lim)
9529 (c-add-stmt-syntax 'case-label nil t lim paren-state))
9530 ;; Got a bogus label at the top level. In lack of better
9531 ;; alternatives, anchor it on (point-min).
9532 (c-add-syntax 'case-label (point-min))))
9533
9534 ;; CASE 15: any other label
9535 ((save-excursion
9536 (back-to-indentation)
9537 (and (not (looking-at c-syntactic-ws-start))
9538 (c-forward-label)))
9539 (cond (containing-decl-open
9540 (setq placeholder (c-add-class-syntax 'inclass
9541 containing-decl-open
9542 containing-decl-start
9543 containing-decl-kwd
9544 paren-state))
9545 ;; Append access-label with the same anchor point as
9546 ;; inclass gets.
9547 (c-append-syntax 'access-label placeholder))
9548
9549 (containing-sexp
9550 (goto-char containing-sexp)
9551 (setq lim (c-most-enclosing-brace c-state-cache
9552 containing-sexp))
9553 (save-excursion
9554 (setq tmpsymbol
9555 (if (and (eq (c-beginning-of-statement-1 lim) 'up)
9556 (looking-at "switch\\>[^_]"))
9557 ;; If the surrounding statement is a switch then
9558 ;; let's analyze all labels as switch labels, so
9559 ;; that they get lined up consistently.
9560 'case-label
9561 'label)))
9562 (c-backward-to-block-anchor lim)
9563 (c-add-stmt-syntax tmpsymbol nil t lim paren-state))
9564
9565 (t
9566 ;; A label on the top level. Treat it as a class
9567 ;; context. (point-min) is the closest we get to the
9568 ;; class open brace.
9569 (c-add-syntax 'access-label (point-min)))))
9570
9571 ;; CASE 4: In-expression statement. C.f. cases 7B, 16A and
9572 ;; 17E.
9573 ((setq placeholder (c-looking-at-inexpr-block
9574 (c-safe-position containing-sexp paren-state)
9575 containing-sexp
9576 ;; Have to turn on the heuristics after
9577 ;; the point even though it doesn't work
9578 ;; very well. C.f. test case class-16.pike.
9579 t))
9580 (setq tmpsymbol (assq (car placeholder)
9581 '((inexpr-class . class-open)
9582 (inexpr-statement . block-open))))
9583 (if tmpsymbol
9584 ;; It's a statement block or an anonymous class.
9585 (setq tmpsymbol (cdr tmpsymbol))
9586 ;; It's a Pike lambda. Check whether we are between the
9587 ;; lambda keyword and the argument list or at the defun
9588 ;; opener.
9589 (setq tmpsymbol (if (eq char-after-ip ?{)
9590 'inline-open
9591 'lambda-intro-cont)))
9592 (goto-char (cdr placeholder))
9593 (back-to-indentation)
9594 (c-add-stmt-syntax tmpsymbol nil t
9595 (c-most-enclosing-brace c-state-cache (point))
9596 paren-state)
9597 (unless (eq (point) (cdr placeholder))
9598 (c-add-syntax (car placeholder))))
9599
9600 ;; CASE 5: Line is inside a declaration level block or at top level.
9601 ((or containing-decl-open (null containing-sexp))
9602 (cond
9603
9604 ;; CASE 5A: we are looking at a defun, brace list, class,
9605 ;; or inline-inclass method opening brace
9606 ((setq special-brace-list
9607 (or (and c-special-brace-lists
9608 (c-looking-at-special-brace-list))
9609 (eq char-after-ip ?{)))
9610 (cond
9611
9612 ;; CASE 5A.1: Non-class declaration block open.
9613 ((save-excursion
9614 (let (tmp)
9615 (and (eq char-after-ip ?{)
9616 (setq tmp (c-looking-at-decl-block containing-sexp t))
9617 (progn
9618 (setq placeholder (point))
9619 (goto-char tmp)
9620 (looking-at c-symbol-key))
9621 (c-keyword-member
9622 (c-keyword-sym (setq keyword (match-string 0)))
9623 'c-other-block-decl-kwds))))
9624 (goto-char placeholder)
9625 (c-add-stmt-syntax
9626 (if (string-equal keyword "extern")
9627 ;; Special case for extern-lang-open.
9628 'extern-lang-open
9629 (intern (concat keyword "-open")))
9630 nil t containing-sexp paren-state))
9631
9632 ;; CASE 5A.2: we are looking at a class opening brace
9633 ((save-excursion
9634 (goto-char indent-point)
9635 (skip-chars-forward " \t")
9636 (and (eq (char-after) ?{)
9637 (c-looking-at-decl-block containing-sexp t)
9638 (setq placeholder (point))))
9639 (c-add-syntax 'class-open placeholder))
9640
9641 ;; CASE 5A.3: brace list open
9642 ((save-excursion
9643 (c-beginning-of-decl-1 lim)
9644 (while (looking-at c-specifier-key)
9645 (goto-char (match-end 1))
9646 (c-forward-syntactic-ws indent-point))
9647 (setq placeholder (c-point 'boi))
9648 (or (consp special-brace-list)
9649 (and (or (save-excursion
9650 (goto-char indent-point)
9651 (setq tmpsymbol nil)
9652 (while (and (> (point) placeholder)
9653 (zerop (c-backward-token-2 1 t))
9654 (not (looking-at "=\\([^=]\\|$\\)")))
9655 (and c-opt-inexpr-brace-list-key
9656 (not tmpsymbol)
9657 (looking-at c-opt-inexpr-brace-list-key)
9658 (setq tmpsymbol 'topmost-intro-cont)))
9659 (looking-at "=\\([^=]\\|$\\)"))
9660 (looking-at c-brace-list-key))
9661 (save-excursion
9662 (while (and (< (point) indent-point)
9663 (zerop (c-forward-token-2 1 t))
9664 (not (memq (char-after) '(?\; ?\()))))
9665 (not (memq (char-after) '(?\; ?\()))
9666 ))))
9667 (if (and (not c-auto-newline-analysis)
9668 (c-major-mode-is 'java-mode)
9669 (eq tmpsymbol 'topmost-intro-cont))
9670 ;; We're in Java and have found that the open brace
9671 ;; belongs to a "new Foo[]" initialization list,
9672 ;; which means the brace list is part of an
9673 ;; expression and not a top level definition. We
9674 ;; therefore treat it as any topmost continuation
9675 ;; even though the semantically correct symbol still
9676 ;; is brace-list-open, on the same grounds as in
9677 ;; case B.2.
9678 (progn
9679 (c-beginning-of-statement-1 lim)
9680 (c-add-syntax 'topmost-intro-cont (c-point 'boi)))
9681 (c-add-syntax 'brace-list-open placeholder)))
9682
9683 ;; CASE 5A.4: inline defun open
9684 ((and containing-decl-open
9685 (not (c-keyword-member containing-decl-kwd
9686 'c-other-block-decl-kwds)))
9687 (c-add-syntax 'inline-open)
9688 (c-add-class-syntax 'inclass
9689 containing-decl-open
9690 containing-decl-start
9691 containing-decl-kwd
9692 paren-state))
9693
9694 ;; CASE 5A.5: ordinary defun open
9695 (t
9696 (save-excursion
9697 (c-beginning-of-decl-1 lim)
9698 (while (looking-at c-specifier-key)
9699 (goto-char (match-end 1))
9700 (c-forward-syntactic-ws indent-point))
9701 (c-add-syntax 'defun-open (c-point 'boi))
9702 ;; Bogus to use bol here, but it's the legacy. (Resolved,
9703 ;; 2007-11-09)
9704 ))))
9705
9706 ;; CASE 5B: After a function header but before the body (or
9707 ;; the ending semicolon if there's no body).
9708 ((save-excursion
9709 (when (setq placeholder (c-just-after-func-arglist-p
9710 (max lim (c-determine-limit 500))))
9711 (setq tmp-pos (point))))
9712 (cond
9713
9714 ;; CASE 5B.1: Member init list.
9715 ((eq (char-after tmp-pos) ?:)
9716 (if (or (>= tmp-pos indent-point)
9717 (= (c-point 'bosws) (1+ tmp-pos)))
9718 (progn
9719 ;; There is no preceding member init clause.
9720 ;; Indent relative to the beginning of indentation
9721 ;; for the topmost-intro line that contains the
9722 ;; prototype's open paren.
9723 (goto-char placeholder)
9724 (c-add-syntax 'member-init-intro (c-point 'boi)))
9725 ;; Indent relative to the first member init clause.
9726 (goto-char (1+ tmp-pos))
9727 (c-forward-syntactic-ws)
9728 (c-add-syntax 'member-init-cont (point))))
9729
9730 ;; CASE 5B.2: K&R arg decl intro
9731 ((and c-recognize-knr-p
9732 (c-in-knr-argdecl lim))
9733 (c-beginning-of-statement-1 lim)
9734 (c-add-syntax 'knr-argdecl-intro (c-point 'boi))
9735 (if containing-decl-open
9736 (c-add-class-syntax 'inclass
9737 containing-decl-open
9738 containing-decl-start
9739 containing-decl-kwd
9740 paren-state)))
9741
9742 ;; CASE 5B.4: Nether region after a C++ or Java func
9743 ;; decl, which could include a `throws' declaration.
9744 (t
9745 (c-beginning-of-statement-1 lim)
9746 (c-add-syntax 'func-decl-cont (c-point 'boi))
9747 )))
9748
9749 ;; CASE 5C: inheritance line. could be first inheritance
9750 ;; line, or continuation of a multiple inheritance
9751 ((or (and (c-major-mode-is 'c++-mode)
9752 (progn
9753 (when (eq char-after-ip ?,)
9754 (skip-chars-forward " \t")
9755 (forward-char))
9756 (looking-at c-opt-postfix-decl-spec-key)))
9757 (and (or (eq char-before-ip ?:)
9758 ;; watch out for scope operator
9759 (save-excursion
9760 (and (eq char-after-ip ?:)
9761 (c-safe (forward-char 1) t)
9762 (not (eq (char-after) ?:))
9763 )))
9764 (save-excursion
9765 (c-backward-syntactic-ws lim)
9766 (if (eq char-before-ip ?:)
9767 (progn
9768 (forward-char -1)
9769 (c-backward-syntactic-ws lim)))
9770 (back-to-indentation)
9771 (looking-at c-class-key)))
9772 ;; for Java
9773 (and (c-major-mode-is 'java-mode)
9774 (let ((fence (save-excursion
9775 (c-beginning-of-statement-1 lim)
9776 (point)))
9777 cont done)
9778 (save-excursion
9779 (while (not done)
9780 (cond ((looking-at c-opt-postfix-decl-spec-key)
9781 (setq injava-inher (cons cont (point))
9782 done t))
9783 ((or (not (c-safe (c-forward-sexp -1) t))
9784 (<= (point) fence))
9785 (setq done t))
9786 )
9787 (setq cont t)))
9788 injava-inher)
9789 (not (c-crosses-statement-barrier-p (cdr injava-inher)
9790 (point)))
9791 ))
9792 (cond
9793
9794 ;; CASE 5C.1: non-hanging colon on an inher intro
9795 ((eq char-after-ip ?:)
9796 (c-beginning-of-statement-1 lim)
9797 (c-add-syntax 'inher-intro (c-point 'boi))
9798 ;; don't add inclass symbol since relative point already
9799 ;; contains any class offset
9800 )
9801
9802 ;; CASE 5C.2: hanging colon on an inher intro
9803 ((eq char-before-ip ?:)
9804 (c-beginning-of-statement-1 lim)
9805 (c-add-syntax 'inher-intro (c-point 'boi))
9806 (if containing-decl-open
9807 (c-add-class-syntax 'inclass
9808 containing-decl-open
9809 containing-decl-start
9810 containing-decl-kwd
9811 paren-state)))
9812
9813 ;; CASE 5C.3: in a Java implements/extends
9814 (injava-inher
9815 (let ((where (cdr injava-inher))
9816 (cont (car injava-inher)))
9817 (goto-char where)
9818 (cond ((looking-at "throws\\>[^_]")
9819 (c-add-syntax 'func-decl-cont
9820 (progn (c-beginning-of-statement-1 lim)
9821 (c-point 'boi))))
9822 (cont (c-add-syntax 'inher-cont where))
9823 (t (c-add-syntax 'inher-intro
9824 (progn (goto-char (cdr injava-inher))
9825 (c-beginning-of-statement-1 lim)
9826 (point))))
9827 )))
9828
9829 ;; CASE 5C.4: a continued inheritance line
9830 (t
9831 (c-beginning-of-inheritance-list lim)
9832 (c-add-syntax 'inher-cont (point))
9833 ;; don't add inclass symbol since relative point already
9834 ;; contains any class offset
9835 )))
9836
9837 ;; CASE 5D: this could be a top-level initialization, a
9838 ;; member init list continuation, or a template argument
9839 ;; list continuation.
9840 ((save-excursion
9841 ;; Note: We use the fact that lim is always after any
9842 ;; preceding brace sexp.
9843 (if c-recognize-<>-arglists
9844 (while (and
9845 (progn
9846 (c-syntactic-skip-backward "^;,=<>" lim t)
9847 (> (point) lim))
9848 (or
9849 (when c-overloadable-operators-regexp
9850 (when (setq placeholder (c-after-special-operator-id lim))
9851 (goto-char placeholder)
9852 t))
9853 (cond
9854 ((eq (char-before) ?>)
9855 (or (c-backward-<>-arglist nil lim)
9856 (backward-char))
9857 t)
9858 ((eq (char-before) ?<)
9859 (backward-char)
9860 (if (save-excursion
9861 (c-forward-<>-arglist nil))
9862 (progn (forward-char)
9863 nil)
9864 t))
9865 (t nil)))))
9866 ;; NB: No c-after-special-operator-id stuff in this
9867 ;; clause - we assume only C++ needs it.
9868 (c-syntactic-skip-backward "^;,=" lim t))
9869 (memq (char-before) '(?, ?= ?<)))
9870 (cond
9871
9872 ;; CASE 5D.3: perhaps a template list continuation?
9873 ((and (c-major-mode-is 'c++-mode)
9874 (save-excursion
9875 (save-restriction
9876 (c-with-syntax-table c++-template-syntax-table
9877 (goto-char indent-point)
9878 (setq placeholder (c-up-list-backward))
9879 (and placeholder
9880 (eq (char-after placeholder) ?<))))))
9881 (c-with-syntax-table c++-template-syntax-table
9882 (goto-char placeholder)
9883 (c-beginning-of-statement-1 lim t)
9884 (if (save-excursion
9885 (c-backward-syntactic-ws lim)
9886 (eq (char-before) ?<))
9887 ;; In a nested template arglist.
9888 (progn
9889 (goto-char placeholder)
9890 (c-syntactic-skip-backward "^,;" lim t)
9891 (c-forward-syntactic-ws))
9892 (back-to-indentation)))
9893 ;; FIXME: Should use c-add-stmt-syntax, but it's not yet
9894 ;; template aware.
9895 (c-add-syntax 'template-args-cont (point) placeholder))
9896
9897 ;; CASE 5D.4: perhaps a multiple inheritance line?
9898 ((and (c-major-mode-is 'c++-mode)
9899 (save-excursion
9900 (c-beginning-of-statement-1 lim)
9901 (setq placeholder (point))
9902 (if (looking-at "static\\>[^_]")
9903 (c-forward-token-2 1 nil indent-point))
9904 (and (looking-at c-class-key)
9905 (zerop (c-forward-token-2 2 nil indent-point))
9906 (if (eq (char-after) ?<)
9907 (c-with-syntax-table c++-template-syntax-table
9908 (zerop (c-forward-token-2 1 t indent-point)))
9909 t)
9910 (eq (char-after) ?:))))
9911 (goto-char placeholder)
9912 (c-add-syntax 'inher-cont (c-point 'boi)))
9913
9914 ;; CASE 5D.5: Continuation of the "expression part" of a
9915 ;; top level construct. Or, perhaps, an unrecognized construct.
9916 (t
9917 (while (and (setq placeholder (point))
9918 (eq (car (c-beginning-of-decl-1 containing-sexp)) ; Can't use `lim' here.
9919 'same)
9920 (save-excursion
9921 (c-backward-syntactic-ws)
9922 (eq (char-before) ?}))
9923 (< (point) placeholder)))
9924 (c-add-stmt-syntax
9925 (cond
9926 ((eq (point) placeholder) 'statement) ; unrecognized construct
9927 ;; A preceding comma at the top level means that a
9928 ;; new variable declaration starts here. Use
9929 ;; topmost-intro-cont for it, for consistency with
9930 ;; the first variable declaration. C.f. case 5N.
9931 ((eq char-before-ip ?,) 'topmost-intro-cont)
9932 (t 'statement-cont))
9933 nil nil containing-sexp paren-state))
9934 ))
9935
9936 ;; CASE 5F: Close of a non-class declaration level block.
9937 ((and (eq char-after-ip ?})
9938 (c-keyword-member containing-decl-kwd
9939 'c-other-block-decl-kwds))
9940 ;; This is inconsistent: Should use `containing-decl-open'
9941 ;; here if it's at boi, like in case 5J.
9942 (goto-char containing-decl-start)
9943 (c-add-stmt-syntax
9944 (if (string-equal (symbol-name containing-decl-kwd) "extern")
9945 ;; Special case for compatibility with the
9946 ;; extern-lang syntactic symbols.
9947 'extern-lang-close
9948 (intern (concat (symbol-name containing-decl-kwd)
9949 "-close")))
9950 nil t
9951 (c-most-enclosing-brace paren-state (point))
9952 paren-state))
9953
9954 ;; CASE 5G: we are looking at the brace which closes the
9955 ;; enclosing nested class decl
9956 ((and containing-sexp
9957 (eq char-after-ip ?})
9958 (eq containing-decl-open containing-sexp))
9959 (c-add-class-syntax 'class-close
9960 containing-decl-open
9961 containing-decl-start
9962 containing-decl-kwd
9963 paren-state))
9964
9965 ;; CASE 5H: we could be looking at subsequent knr-argdecls
9966 ((and c-recognize-knr-p
9967 (not containing-sexp) ; can't be knr inside braces.
9968 (not (eq char-before-ip ?}))
9969 (save-excursion
9970 (setq placeholder (cdr (c-beginning-of-decl-1 lim)))
9971 (and placeholder
9972 ;; Do an extra check to avoid tripping up on
9973 ;; statements that occur in invalid contexts
9974 ;; (e.g. in macro bodies where we don't really
9975 ;; know the context of what we're looking at).
9976 (not (and c-opt-block-stmt-key
9977 (looking-at c-opt-block-stmt-key)))))
9978 (< placeholder indent-point))
9979 (goto-char placeholder)
9980 (c-add-syntax 'knr-argdecl (point)))
9981
9982 ;; CASE 5I: ObjC method definition.
9983 ((and c-opt-method-key
9984 (looking-at c-opt-method-key))
9985 (c-beginning-of-statement-1 nil t)
9986 (if (= (point) indent-point)
9987 ;; Handle the case when it's the first (non-comment)
9988 ;; thing in the buffer. Can't look for a 'same return
9989 ;; value from cbos1 since ObjC directives currently
9990 ;; aren't recognized fully, so that we get 'same
9991 ;; instead of 'previous if it moved over a preceding
9992 ;; directive.
9993 (goto-char (point-min)))
9994 (c-add-syntax 'objc-method-intro (c-point 'boi)))
9995
9996 ;; CASE 5P: AWK pattern or function or continuation
9997 ;; thereof.
9998 ((c-major-mode-is 'awk-mode)
9999 (setq placeholder (point))
10000 (c-add-stmt-syntax
10001 (if (and (eq (c-beginning-of-statement-1) 'same)
10002 (/= (point) placeholder))
10003 'topmost-intro-cont
10004 'topmost-intro)
10005 nil nil
10006 containing-sexp paren-state))
10007
10008 ;; CASE 5N: At a variable declaration that follows a class
10009 ;; definition or some other block declaration that doesn't
10010 ;; end at the closing '}'. C.f. case 5D.5.
10011 ((progn
10012 (c-backward-syntactic-ws lim)
10013 (and (eq (char-before) ?})
10014 (save-excursion
10015 (let ((start (point)))
10016 (if (and c-state-cache
10017 (consp (car c-state-cache))
10018 (eq (cdar c-state-cache) (point)))
10019 ;; Speed up the backward search a bit.
10020 (goto-char (caar c-state-cache)))
10021 (c-beginning-of-decl-1 containing-sexp) ; Can't use `lim' here.
10022 (setq placeholder (point))
10023 (if (= start (point))
10024 ;; The '}' is unbalanced.
10025 nil
10026 (c-end-of-decl-1)
10027 (>= (point) indent-point))))))
10028 (goto-char placeholder)
10029 (c-add-stmt-syntax 'topmost-intro-cont nil nil
10030 containing-sexp paren-state))
10031
10032 ;; NOTE: The point is at the end of the previous token here.
10033
10034 ;; CASE 5J: we are at the topmost level, make
10035 ;; sure we skip back past any access specifiers
10036 ((and
10037 ;; A macro continuation line is never at top level.
10038 (not (and macro-start
10039 (> indent-point macro-start)))
10040 (save-excursion
10041 (setq placeholder (point))
10042 (or (memq char-before-ip '(?\; ?{ ?} nil))
10043 (c-at-vsemi-p before-ws-ip)
10044 (when (and (eq char-before-ip ?:)
10045 (eq (c-beginning-of-statement-1 lim)
10046 'label))
10047 (c-backward-syntactic-ws lim)
10048 (setq placeholder (point)))
10049 (and (c-major-mode-is 'objc-mode)
10050 (catch 'not-in-directive
10051 (c-beginning-of-statement-1 lim)
10052 (setq placeholder (point))
10053 (while (and (c-forward-objc-directive)
10054 (< (point) indent-point))
10055 (c-forward-syntactic-ws)
10056 (if (>= (point) indent-point)
10057 (throw 'not-in-directive t))
10058 (setq placeholder (point)))
10059 nil)))))
10060 ;; For historic reasons we anchor at bol of the last
10061 ;; line of the previous declaration. That's clearly
10062 ;; highly bogus and useless, and it makes our lives hard
10063 ;; to remain compatible. :P
10064 (goto-char placeholder)
10065 (c-add-syntax 'topmost-intro (c-point 'bol))
10066 (if containing-decl-open
10067 (if (c-keyword-member containing-decl-kwd
10068 'c-other-block-decl-kwds)
10069 (progn
10070 (goto-char (c-brace-anchor-point containing-decl-open))
10071 (c-add-stmt-syntax
10072 (if (string-equal (symbol-name containing-decl-kwd)
10073 "extern")
10074 ;; Special case for compatibility with the
10075 ;; extern-lang syntactic symbols.
10076 'inextern-lang
10077 (intern (concat "in"
10078 (symbol-name containing-decl-kwd))))
10079 nil t
10080 (c-most-enclosing-brace paren-state (point))
10081 paren-state))
10082 (c-add-class-syntax 'inclass
10083 containing-decl-open
10084 containing-decl-start
10085 containing-decl-kwd
10086 paren-state)))
10087 (when (and c-syntactic-indentation-in-macros
10088 macro-start
10089 (/= macro-start (c-point 'boi indent-point)))
10090 (c-add-syntax 'cpp-define-intro)
10091 (setq macro-start nil)))
10092
10093 ;; CASE 5K: we are at an ObjC method definition
10094 ;; continuation line.
10095 ((and c-opt-method-key
10096 (save-excursion
10097 (c-beginning-of-statement-1 lim)
10098 (beginning-of-line)
10099 (when (looking-at c-opt-method-key)
10100 (setq placeholder (point)))))
10101 (c-add-syntax 'objc-method-args-cont placeholder))
10102
10103 ;; CASE 5L: we are at the first argument of a template
10104 ;; arglist that begins on the previous line.
10105 ((and c-recognize-<>-arglists
10106 (eq (char-before) ?<)
10107 (not (and c-overloadable-operators-regexp
10108 (c-after-special-operator-id lim))))
10109 (c-beginning-of-statement-1 (c-safe-position (point) paren-state))
10110 (c-add-syntax 'template-args-cont (c-point 'boi)))
10111
10112 ;; CASE 5Q: we are at a statement within a macro.
10113 (macro-start
10114 (c-beginning-of-statement-1 containing-sexp)
10115 (c-add-stmt-syntax 'statement nil t containing-sexp paren-state))
10116
10117 ;;CASE 5N: We are at a topmost continuation line and the only
10118 ;;preceding items are annotations.
10119 ((and (c-major-mode-is 'java-mode)
10120 (setq placeholder (point))
10121 (c-beginning-of-statement-1)
10122 (progn
10123 (while (and (c-forward-annotation))
10124 (c-forward-syntactic-ws))
10125 t)
10126 (prog1
10127 (>= (point) placeholder)
10128 (goto-char placeholder)))
10129 (c-add-syntax 'annotation-top-cont (c-point 'boi)))
10130
10131 ;; CASE 5M: we are at a topmost continuation line
10132 (t
10133 (c-beginning-of-statement-1 (c-safe-position (point) paren-state))
10134 (when (c-major-mode-is 'objc-mode)
10135 (setq placeholder (point))
10136 (while (and (c-forward-objc-directive)
10137 (< (point) indent-point))
10138 (c-forward-syntactic-ws)
10139 (setq placeholder (point)))
10140 (goto-char placeholder))
10141 (c-add-syntax 'topmost-intro-cont (c-point 'boi)))
10142 ))
10143
10144
10145 ;; (CASE 6 has been removed.)
10146
10147 ;; CASE 7: line is an expression, not a statement. Most
10148 ;; likely we are either in a function prototype or a function
10149 ;; call argument list
10150 ((not (or (and c-special-brace-lists
10151 (save-excursion
10152 (goto-char containing-sexp)
10153 (c-looking-at-special-brace-list)))
10154 (eq (char-after containing-sexp) ?{)))
10155 (cond
10156
10157 ;; CASE 7A: we are looking at the arglist closing paren.
10158 ;; C.f. case 7F.
10159 ((memq char-after-ip '(?\) ?\]))
10160 (goto-char containing-sexp)
10161 (setq placeholder (c-point 'boi))
10162 (if (and (c-safe (backward-up-list 1) t)
10163 (>= (point) placeholder))
10164 (progn
10165 (forward-char)
10166 (skip-chars-forward " \t"))
10167 (goto-char placeholder))
10168 (c-add-stmt-syntax 'arglist-close (list containing-sexp) t
10169 (c-most-enclosing-brace paren-state (point))
10170 paren-state))
10171
10172 ;; CASE 7B: Looking at the opening brace of an
10173 ;; in-expression block or brace list. C.f. cases 4, 16A
10174 ;; and 17E.
10175 ((and (eq char-after-ip ?{)
10176 (progn
10177 (setq placeholder (c-inside-bracelist-p (point)
10178 paren-state))
10179 (if placeholder
10180 (setq tmpsymbol '(brace-list-open . inexpr-class))
10181 (setq tmpsymbol '(block-open . inexpr-statement)
10182 placeholder
10183 (cdr-safe (c-looking-at-inexpr-block
10184 (c-safe-position containing-sexp
10185 paren-state)
10186 containing-sexp)))
10187 ;; placeholder is nil if it's a block directly in
10188 ;; a function arglist. That makes us skip out of
10189 ;; this case.
10190 )))
10191 (goto-char placeholder)
10192 (back-to-indentation)
10193 (c-add-stmt-syntax (car tmpsymbol) nil t
10194 (c-most-enclosing-brace paren-state (point))
10195 paren-state)
10196 (if (/= (point) placeholder)
10197 (c-add-syntax (cdr tmpsymbol))))
10198
10199 ;; CASE 7C: we are looking at the first argument in an empty
10200 ;; argument list. Use arglist-close if we're actually
10201 ;; looking at a close paren or bracket.
10202 ((memq char-before-ip '(?\( ?\[))
10203 (goto-char containing-sexp)
10204 (setq placeholder (c-point 'boi))
10205 (if (and (c-safe (backward-up-list 1) t)
10206 (>= (point) placeholder))
10207 (progn
10208 (forward-char)
10209 (skip-chars-forward " \t"))
10210 (goto-char placeholder))
10211 (c-add-stmt-syntax 'arglist-intro (list containing-sexp) t
10212 (c-most-enclosing-brace paren-state (point))
10213 paren-state))
10214
10215 ;; CASE 7D: we are inside a conditional test clause. treat
10216 ;; these things as statements
10217 ((progn
10218 (goto-char containing-sexp)
10219 (and (c-safe (c-forward-sexp -1) t)
10220 (looking-at "\\<for\\>[^_]")))
10221 (goto-char (1+ containing-sexp))
10222 (c-forward-syntactic-ws indent-point)
10223 (if (eq char-before-ip ?\;)
10224 (c-add-syntax 'statement (point))
10225 (c-add-syntax 'statement-cont (point))
10226 ))
10227
10228 ;; CASE 7E: maybe a continued ObjC method call. This is the
10229 ;; case when we are inside a [] bracketed exp, and what
10230 ;; precede the opening bracket is not an identifier.
10231 ((and c-opt-method-key
10232 (eq (char-after containing-sexp) ?\[)
10233 (progn
10234 (goto-char (1- containing-sexp))
10235 (c-backward-syntactic-ws (c-point 'bod))
10236 (if (not (looking-at c-symbol-key))
10237 (c-add-syntax 'objc-method-call-cont containing-sexp))
10238 )))
10239
10240 ;; CASE 7F: we are looking at an arglist continuation line,
10241 ;; but the preceding argument is on the same line as the
10242 ;; opening paren. This case includes multi-line
10243 ;; mathematical paren groupings, but we could be on a
10244 ;; for-list continuation line. C.f. case 7A.
10245 ((progn
10246 (goto-char (1+ containing-sexp))
10247 (< (save-excursion
10248 (c-forward-syntactic-ws)
10249 (point))
10250 (c-point 'bonl)))
10251 (goto-char containing-sexp) ; paren opening the arglist
10252 (setq placeholder (c-point 'boi))
10253 (if (and (c-safe (backward-up-list 1) t)
10254 (>= (point) placeholder))
10255 (progn
10256 (forward-char)
10257 (skip-chars-forward " \t"))
10258 (goto-char placeholder))
10259 (c-add-stmt-syntax 'arglist-cont-nonempty (list containing-sexp) t
10260 (c-most-enclosing-brace c-state-cache (point))
10261 paren-state))
10262
10263 ;; CASE 7G: we are looking at just a normal arglist
10264 ;; continuation line
10265 (t (c-forward-syntactic-ws indent-point)
10266 (c-add-syntax 'arglist-cont (c-point 'boi)))
10267 ))
10268
10269 ;; CASE 8: func-local multi-inheritance line
10270 ((and (c-major-mode-is 'c++-mode)
10271 (save-excursion
10272 (goto-char indent-point)
10273 (skip-chars-forward " \t")
10274 (looking-at c-opt-postfix-decl-spec-key)))
10275 (goto-char indent-point)
10276 (skip-chars-forward " \t")
10277 (cond
10278
10279 ;; CASE 8A: non-hanging colon on an inher intro
10280 ((eq char-after-ip ?:)
10281 (c-backward-syntactic-ws lim)
10282 (c-add-syntax 'inher-intro (c-point 'boi)))
10283
10284 ;; CASE 8B: hanging colon on an inher intro
10285 ((eq char-before-ip ?:)
10286 (c-add-syntax 'inher-intro (c-point 'boi)))
10287
10288 ;; CASE 8C: a continued inheritance line
10289 (t
10290 (c-beginning-of-inheritance-list lim)
10291 (c-add-syntax 'inher-cont (point))
10292 )))
10293
10294 ;; CASE 9: we are inside a brace-list
10295 ((and (not (c-major-mode-is 'awk-mode)) ; Maybe this isn't needed (ACM, 2002/3/29)
10296 (setq special-brace-list
10297 (or (and c-special-brace-lists ;;;; ALWAYS NIL FOR AWK!!
10298 (save-excursion
10299 (goto-char containing-sexp)
10300 (c-looking-at-special-brace-list)))
10301 (c-inside-bracelist-p containing-sexp paren-state))))
10302 (cond
10303
10304 ;; CASE 9A: In the middle of a special brace list opener.
10305 ((and (consp special-brace-list)
10306 (save-excursion
10307 (goto-char containing-sexp)
10308 (eq (char-after) ?\())
10309 (eq char-after-ip (car (cdr special-brace-list))))
10310 (goto-char (car (car special-brace-list)))
10311 (skip-chars-backward " \t")
10312 (if (and (bolp)
10313 (assoc 'statement-cont
10314 (setq placeholder (c-guess-basic-syntax))))
10315 (setq c-syntactic-context placeholder)
10316 (c-beginning-of-statement-1
10317 (c-safe-position (1- containing-sexp) paren-state))
10318 (c-forward-token-2 0)
10319 (while (looking-at c-specifier-key)
10320 (goto-char (match-end 1))
10321 (c-forward-syntactic-ws))
10322 (c-add-syntax 'brace-list-open (c-point 'boi))))
10323
10324 ;; CASE 9B: brace-list-close brace
10325 ((if (consp special-brace-list)
10326 ;; Check special brace list closer.
10327 (progn
10328 (goto-char (car (car special-brace-list)))
10329 (save-excursion
10330 (goto-char indent-point)
10331 (back-to-indentation)
10332 (or
10333 ;; We were between the special close char and the `)'.
10334 (and (eq (char-after) ?\))
10335 (eq (1+ (point)) (cdr (car special-brace-list))))
10336 ;; We were before the special close char.
10337 (and (eq (char-after) (cdr (cdr special-brace-list)))
10338 (zerop (c-forward-token-2))
10339 (eq (1+ (point)) (cdr (car special-brace-list)))))))
10340 ;; Normal brace list check.
10341 (and (eq char-after-ip ?})
10342 (c-safe (goto-char (c-up-list-backward (point))) t)
10343 (= (point) containing-sexp)))
10344 (if (eq (point) (c-point 'boi))
10345 (c-add-syntax 'brace-list-close (point))
10346 (setq lim (c-most-enclosing-brace c-state-cache (point)))
10347 (c-beginning-of-statement-1 lim)
10348 (c-add-stmt-syntax 'brace-list-close nil t lim paren-state)))
10349
10350 (t
10351 ;; Prepare for the rest of the cases below by going to the
10352 ;; token following the opening brace
10353 (if (consp special-brace-list)
10354 (progn
10355 (goto-char (car (car special-brace-list)))
10356 (c-forward-token-2 1 nil indent-point))
10357 (goto-char containing-sexp))
10358 (forward-char)
10359 (let ((start (point)))
10360 (c-forward-syntactic-ws indent-point)
10361 (goto-char (max start (c-point 'bol))))
10362 (c-skip-ws-forward indent-point)
10363 (cond
10364
10365 ;; CASE 9C: we're looking at the first line in a brace-list
10366 ((= (point) indent-point)
10367 (if (consp special-brace-list)
10368 (goto-char (car (car special-brace-list)))
10369 (goto-char containing-sexp))
10370 (if (eq (point) (c-point 'boi))
10371 (c-add-syntax 'brace-list-intro (point))
10372 (setq lim (c-most-enclosing-brace c-state-cache (point)))
10373 (c-beginning-of-statement-1 lim)
10374 (c-add-stmt-syntax 'brace-list-intro nil t lim paren-state)))
10375
10376 ;; CASE 9D: this is just a later brace-list-entry or
10377 ;; brace-entry-open
10378 (t (if (or (eq char-after-ip ?{)
10379 (and c-special-brace-lists
10380 (save-excursion
10381 (goto-char indent-point)
10382 (c-forward-syntactic-ws (c-point 'eol))
10383 (c-looking-at-special-brace-list (point)))))
10384 (c-add-syntax 'brace-entry-open (point))
10385 (c-add-syntax 'brace-list-entry (point))
10386 ))
10387 ))))
10388
10389 ;; CASE 10: A continued statement or top level construct.
10390 ((and (not (memq char-before-ip '(?\; ?:)))
10391 (not (c-at-vsemi-p before-ws-ip))
10392 (or (not (eq char-before-ip ?}))
10393 (c-looking-at-inexpr-block-backward c-state-cache))
10394 (> (point)
10395 (save-excursion
10396 (c-beginning-of-statement-1 containing-sexp)
10397 (setq placeholder (point))))
10398 (/= placeholder containing-sexp))
10399 ;; This is shared with case 18.
10400 (c-guess-continued-construct indent-point
10401 char-after-ip
10402 placeholder
10403 containing-sexp
10404 paren-state))
10405
10406 ;; CASE 16: block close brace, possibly closing the defun or
10407 ;; the class
10408 ((eq char-after-ip ?})
10409 ;; From here on we have the next containing sexp in lim.
10410 (setq lim (c-most-enclosing-brace paren-state))
10411 (goto-char containing-sexp)
10412 (cond
10413
10414 ;; CASE 16E: Closing a statement block? This catches
10415 ;; cases where it's preceded by a statement keyword,
10416 ;; which works even when used in an "invalid" context,
10417 ;; e.g. a macro argument.
10418 ((c-after-conditional)
10419 (c-backward-to-block-anchor lim)
10420 (c-add-stmt-syntax 'block-close nil t lim paren-state))
10421
10422 ;; CASE 16A: closing a lambda defun or an in-expression
10423 ;; block? C.f. cases 4, 7B and 17E.
10424 ((setq placeholder (c-looking-at-inexpr-block
10425 (c-safe-position containing-sexp paren-state)
10426 nil))
10427 (setq tmpsymbol (if (eq (car placeholder) 'inlambda)
10428 'inline-close
10429 'block-close))
10430 (goto-char containing-sexp)
10431 (back-to-indentation)
10432 (if (= containing-sexp (point))
10433 (c-add-syntax tmpsymbol (point))
10434 (goto-char (cdr placeholder))
10435 (back-to-indentation)
10436 (c-add-stmt-syntax tmpsymbol nil t
10437 (c-most-enclosing-brace paren-state (point))
10438 paren-state)
10439 (if (/= (point) (cdr placeholder))
10440 (c-add-syntax (car placeholder)))))
10441
10442 ;; CASE 16B: does this close an inline or a function in
10443 ;; a non-class declaration level block?
10444 ((save-excursion
10445 (and lim
10446 (progn
10447 (goto-char lim)
10448 (c-looking-at-decl-block
10449 (c-most-enclosing-brace paren-state lim)
10450 nil))
10451 (setq placeholder (point))))
10452 (c-backward-to-decl-anchor lim)
10453 (back-to-indentation)
10454 (if (save-excursion
10455 (goto-char placeholder)
10456 (looking-at c-other-decl-block-key))
10457 (c-add-syntax 'defun-close (point))
10458 (c-add-syntax 'inline-close (point))))
10459
10460 ;; CASE 16F: Can be a defun-close of a function declared
10461 ;; in a statement block, e.g. in Pike or when using gcc
10462 ;; extensions, but watch out for macros followed by
10463 ;; blocks. Let it through to be handled below.
10464 ;; C.f. cases B.3 and 17G.
10465 ((save-excursion
10466 (and (not (c-at-statement-start-p))
10467 (eq (c-beginning-of-statement-1 lim nil nil t) 'same)
10468 (setq placeholder (point))
10469 (let ((c-recognize-typeless-decls nil))
10470 ;; Turn off recognition of constructs that
10471 ;; lacks a type in this case, since that's more
10472 ;; likely to be a macro followed by a block.
10473 (c-forward-decl-or-cast-1 (c-point 'bosws) nil nil))))
10474 (back-to-indentation)
10475 (if (/= (point) containing-sexp)
10476 (goto-char placeholder))
10477 (c-add-stmt-syntax 'defun-close nil t lim paren-state))
10478
10479 ;; CASE 16C: If there is an enclosing brace then this is
10480 ;; a block close since defun closes inside declaration
10481 ;; level blocks have been handled above.
10482 (lim
10483 ;; If the block is preceded by a case/switch label on
10484 ;; the same line, we anchor at the first preceding label
10485 ;; at boi. The default handling in c-add-stmt-syntax
10486 ;; really fixes it better, but we do like this to keep
10487 ;; the indentation compatible with version 5.28 and
10488 ;; earlier. C.f. case 17H.
10489 (while (and (/= (setq placeholder (point)) (c-point 'boi))
10490 (eq (c-beginning-of-statement-1 lim) 'label)))
10491 (goto-char placeholder)
10492 (if (looking-at c-label-kwds-regexp)
10493 (c-add-syntax 'block-close (point))
10494 (goto-char containing-sexp)
10495 ;; c-backward-to-block-anchor not necessary here; those
10496 ;; situations are handled in case 16E above.
10497 (c-add-stmt-syntax 'block-close nil t lim paren-state)))
10498
10499 ;; CASE 16D: Only top level defun close left.
10500 (t
10501 (goto-char containing-sexp)
10502 (c-backward-to-decl-anchor lim)
10503 (c-add-stmt-syntax 'defun-close nil nil
10504 (c-most-enclosing-brace paren-state)
10505 paren-state))
10506 ))
10507
10508 ;; CASE 19: line is an expression, not a statement, and is directly
10509 ;; contained by a template delimiter. Most likely, we are in a
10510 ;; template arglist within a statement. This case is based on CASE
10511 ;; 7. At some point in the future, we may wish to create more
10512 ;; syntactic symbols such as `template-intro',
10513 ;; `template-cont-nonempty', etc., and distinguish between them as we
10514 ;; do for `arglist-intro' etc. (2009-12-07).
10515 ((and c-recognize-<>-arglists
10516 (setq containing-< (c-up-list-backward indent-point containing-sexp))
10517 (eq (char-after containing-<) ?\<))
10518 (setq placeholder (c-point 'boi containing-<))
10519 (goto-char containing-sexp) ; Most nested Lbrace/Lparen (but not
10520 ; '<') before indent-point.
10521 (if (>= (point) placeholder)
10522 (progn
10523 (forward-char)
10524 (skip-chars-forward " \t"))
10525 (goto-char placeholder))
10526 (c-add-stmt-syntax 'template-args-cont (list containing-<) t
10527 (c-most-enclosing-brace c-state-cache (point))
10528 paren-state))
10529
10530 ;; CASE 17: Statement or defun catchall.
10531 (t
10532 (goto-char indent-point)
10533 ;; Back up statements until we find one that starts at boi.
10534 (while (let* ((prev-point (point))
10535 (last-step-type (c-beginning-of-statement-1
10536 containing-sexp)))
10537 (if (= (point) prev-point)
10538 (progn
10539 (setq step-type (or step-type last-step-type))
10540 nil)
10541 (setq step-type last-step-type)
10542 (/= (point) (c-point 'boi)))))
10543 (cond
10544
10545 ;; CASE 17B: continued statement
10546 ((and (eq step-type 'same)
10547 (/= (point) indent-point))
10548 (c-add-stmt-syntax 'statement-cont nil nil
10549 containing-sexp paren-state))
10550
10551 ;; CASE 17A: After a case/default label?
10552 ((progn
10553 (while (and (eq step-type 'label)
10554 (not (looking-at c-label-kwds-regexp)))
10555 (setq step-type
10556 (c-beginning-of-statement-1 containing-sexp)))
10557 (eq step-type 'label))
10558 (c-add-stmt-syntax (if (eq char-after-ip ?{)
10559 'statement-case-open
10560 'statement-case-intro)
10561 nil t containing-sexp paren-state))
10562
10563 ;; CASE 17D: any old statement
10564 ((progn
10565 (while (eq step-type 'label)
10566 (setq step-type
10567 (c-beginning-of-statement-1 containing-sexp)))
10568 (eq step-type 'previous))
10569 (c-add-stmt-syntax 'statement nil t
10570 containing-sexp paren-state)
10571 (if (eq char-after-ip ?{)
10572 (c-add-syntax 'block-open)))
10573
10574 ;; CASE 17I: Inside a substatement block.
10575 ((progn
10576 ;; The following tests are all based on containing-sexp.
10577 (goto-char containing-sexp)
10578 ;; From here on we have the next containing sexp in lim.
10579 (setq lim (c-most-enclosing-brace paren-state containing-sexp))
10580 (c-after-conditional))
10581 (c-backward-to-block-anchor lim)
10582 (c-add-stmt-syntax 'statement-block-intro nil t
10583 lim paren-state)
10584 (if (eq char-after-ip ?{)
10585 (c-add-syntax 'block-open)))
10586
10587 ;; CASE 17E: first statement in an in-expression block.
10588 ;; C.f. cases 4, 7B and 16A.
10589 ((setq placeholder (c-looking-at-inexpr-block
10590 (c-safe-position containing-sexp paren-state)
10591 nil))
10592 (setq tmpsymbol (if (eq (car placeholder) 'inlambda)
10593 'defun-block-intro
10594 'statement-block-intro))
10595 (back-to-indentation)
10596 (if (= containing-sexp (point))
10597 (c-add-syntax tmpsymbol (point))
10598 (goto-char (cdr placeholder))
10599 (back-to-indentation)
10600 (c-add-stmt-syntax tmpsymbol nil t
10601 (c-most-enclosing-brace c-state-cache (point))
10602 paren-state)
10603 (if (/= (point) (cdr placeholder))
10604 (c-add-syntax (car placeholder))))
10605 (if (eq char-after-ip ?{)
10606 (c-add-syntax 'block-open)))
10607
10608 ;; CASE 17F: first statement in an inline, or first
10609 ;; statement in a top-level defun. we can tell this is it
10610 ;; if there are no enclosing braces that haven't been
10611 ;; narrowed out by a class (i.e. don't use bod here).
10612 ((save-excursion
10613 (or (not (setq placeholder (c-most-enclosing-brace
10614 paren-state)))
10615 (and (progn
10616 (goto-char placeholder)
10617 (eq (char-after) ?{))
10618 (c-looking-at-decl-block (c-most-enclosing-brace
10619 paren-state (point))
10620 nil))))
10621 (c-backward-to-decl-anchor lim)
10622 (back-to-indentation)
10623 (c-add-syntax 'defun-block-intro (point)))
10624
10625 ;; CASE 17G: First statement in a function declared inside
10626 ;; a normal block. This can occur in Pike and with
10627 ;; e.g. the gcc extensions, but watch out for macros
10628 ;; followed by blocks. C.f. cases B.3 and 16F.
10629 ((save-excursion
10630 (and (not (c-at-statement-start-p))
10631 (eq (c-beginning-of-statement-1 lim nil nil t) 'same)
10632 (setq placeholder (point))
10633 (let ((c-recognize-typeless-decls nil))
10634 ;; Turn off recognition of constructs that lacks
10635 ;; a type in this case, since that's more likely
10636 ;; to be a macro followed by a block.
10637 (c-forward-decl-or-cast-1 (c-point 'bosws) nil nil))))
10638 (back-to-indentation)
10639 (if (/= (point) containing-sexp)
10640 (goto-char placeholder))
10641 (c-add-stmt-syntax 'defun-block-intro nil t
10642 lim paren-state))
10643
10644 ;; CASE 17H: First statement in a block.
10645 (t
10646 ;; If the block is preceded by a case/switch label on the
10647 ;; same line, we anchor at the first preceding label at
10648 ;; boi. The default handling in c-add-stmt-syntax is
10649 ;; really fixes it better, but we do like this to keep the
10650 ;; indentation compatible with version 5.28 and earlier.
10651 ;; C.f. case 16C.
10652 (while (and (/= (setq placeholder (point)) (c-point 'boi))
10653 (eq (c-beginning-of-statement-1 lim) 'label)))
10654 (goto-char placeholder)
10655 (if (looking-at c-label-kwds-regexp)
10656 (c-add-syntax 'statement-block-intro (point))
10657 (goto-char containing-sexp)
10658 ;; c-backward-to-block-anchor not necessary here; those
10659 ;; situations are handled in case 17I above.
10660 (c-add-stmt-syntax 'statement-block-intro nil t
10661 lim paren-state))
10662 (if (eq char-after-ip ?{)
10663 (c-add-syntax 'block-open)))
10664 ))
10665 )
10666
10667 ;; now we need to look at any modifiers
10668 (goto-char indent-point)
10669 (skip-chars-forward " \t")
10670
10671 ;; are we looking at a comment only line?
10672 (when (and (looking-at c-comment-start-regexp)
10673 (/= (c-forward-token-2 0 nil (c-point 'eol)) 0))
10674 (c-append-syntax 'comment-intro))
10675
10676 ;; we might want to give additional offset to friends (in C++).
10677 (when (and c-opt-friend-key
10678 (looking-at c-opt-friend-key))
10679 (c-append-syntax 'friend))
10680
10681 ;; Set syntactic-relpos.
10682 (let ((p c-syntactic-context))
10683 (while (and p
10684 (if (integerp (c-langelem-pos (car p)))
10685 (progn
10686 (setq syntactic-relpos (c-langelem-pos (car p)))
10687 nil)
10688 t))
10689 (setq p (cdr p))))
10690
10691 ;; Start of or a continuation of a preprocessor directive?
10692 (if (and macro-start
10693 (eq macro-start (c-point 'boi))
10694 (not (and (c-major-mode-is 'pike-mode)
10695 (eq (char-after (1+ macro-start)) ?\"))))
10696 (c-append-syntax 'cpp-macro)
10697 (when (and c-syntactic-indentation-in-macros macro-start)
10698 (if in-macro-expr
10699 (when (or
10700 (< syntactic-relpos macro-start)
10701 (not (or
10702 (assq 'arglist-intro c-syntactic-context)
10703 (assq 'arglist-cont c-syntactic-context)
10704 (assq 'arglist-cont-nonempty c-syntactic-context)
10705 (assq 'arglist-close c-syntactic-context))))
10706 ;; If inside a cpp expression, i.e. anywhere in a
10707 ;; cpp directive except a #define body, we only let
10708 ;; through the syntactic analysis that is internal
10709 ;; in the expression. That means the arglist
10710 ;; elements, if they are anchored inside the cpp
10711 ;; expression.
10712 (setq c-syntactic-context nil)
10713 (c-add-syntax 'cpp-macro-cont macro-start))
10714 (when (and (eq macro-start syntactic-relpos)
10715 (not (assq 'cpp-define-intro c-syntactic-context))
10716 (save-excursion
10717 (goto-char macro-start)
10718 (or (not (c-forward-to-cpp-define-body))
10719 (<= (point) (c-point 'boi indent-point)))))
10720 ;; Inside a #define body and the syntactic analysis is
10721 ;; anchored on the start of the #define. In this case
10722 ;; we add cpp-define-intro to get the extra
10723 ;; indentation of the #define body.
10724 (c-add-syntax 'cpp-define-intro)))))
10725
10726 ;; return the syntax
10727 c-syntactic-context)))
10728
10729 \f
10730 ;; Indentation calculation.
10731
10732 (defun c-evaluate-offset (offset langelem symbol)
10733 ;; offset can be a number, a function, a variable, a list, or one of
10734 ;; the symbols + or -
10735 ;;
10736 ;; This function might do hidden buffer changes.
10737 (let ((res
10738 (cond
10739 ((numberp offset) offset)
10740 ((vectorp offset) offset)
10741 ((null offset) nil)
10742
10743 ((eq offset '+) c-basic-offset)
10744 ((eq offset '-) (- c-basic-offset))
10745 ((eq offset '++) (* 2 c-basic-offset))
10746 ((eq offset '--) (* 2 (- c-basic-offset)))
10747 ((eq offset '*) (/ c-basic-offset 2))
10748 ((eq offset '/) (/ (- c-basic-offset) 2))
10749
10750 ((functionp offset)
10751 (c-evaluate-offset
10752 (funcall offset
10753 (cons (c-langelem-sym langelem)
10754 (c-langelem-pos langelem)))
10755 langelem symbol))
10756
10757 ((listp offset)
10758 (cond
10759 ((eq (car offset) 'quote)
10760 (c-benign-error "The offset %S for %s was mistakenly quoted"
10761 offset symbol)
10762 nil)
10763
10764 ((memq (car offset) '(min max))
10765 (let (res val (method (car offset)))
10766 (setq offset (cdr offset))
10767 (while offset
10768 (setq val (c-evaluate-offset (car offset) langelem symbol))
10769 (cond
10770 ((not val))
10771 ((not res)
10772 (setq res val))
10773 ((integerp val)
10774 (if (vectorp res)
10775 (c-benign-error "\
10776 Error evaluating offset %S for %s: \
10777 Cannot combine absolute offset %S with relative %S in `%s' method"
10778 (car offset) symbol res val method)
10779 (setq res (funcall method res val))))
10780 (t
10781 (if (integerp res)
10782 (c-benign-error "\
10783 Error evaluating offset %S for %s: \
10784 Cannot combine relative offset %S with absolute %S in `%s' method"
10785 (car offset) symbol res val method)
10786 (setq res (vector (funcall method (aref res 0)
10787 (aref val 0)))))))
10788 (setq offset (cdr offset)))
10789 res))
10790
10791 ((eq (car offset) 'add)
10792 (let (res val)
10793 (setq offset (cdr offset))
10794 (while offset
10795 (setq val (c-evaluate-offset (car offset) langelem symbol))
10796 (cond
10797 ((not val))
10798 ((not res)
10799 (setq res val))
10800 ((integerp val)
10801 (if (vectorp res)
10802 (setq res (vector (+ (aref res 0) val)))
10803 (setq res (+ res val))))
10804 (t
10805 (if (vectorp res)
10806 (c-benign-error "\
10807 Error evaluating offset %S for %s: \
10808 Cannot combine absolute offsets %S and %S in `add' method"
10809 (car offset) symbol res val)
10810 (setq res val)))) ; Override.
10811 (setq offset (cdr offset)))
10812 res))
10813
10814 (t
10815 (let (res)
10816 (when (eq (car offset) 'first)
10817 (setq offset (cdr offset)))
10818 (while (and (not res) offset)
10819 (setq res (c-evaluate-offset (car offset) langelem symbol)
10820 offset (cdr offset)))
10821 res))))
10822
10823 ((and (symbolp offset) (boundp offset))
10824 (symbol-value offset))
10825
10826 (t
10827 (c-benign-error "Unknown offset format %S for %s" offset symbol)
10828 nil))))
10829
10830 (if (or (null res) (integerp res)
10831 (and (vectorp res) (= (length res) 1) (integerp (aref res 0))))
10832 res
10833 (c-benign-error "Error evaluating offset %S for %s: Got invalid value %S"
10834 offset symbol res)
10835 nil)))
10836
10837 (defun c-calc-offset (langelem)
10838 ;; Get offset from LANGELEM which is a list beginning with the
10839 ;; syntactic symbol and followed by any analysis data it provides.
10840 ;; That data may be zero or more elements, but if at least one is
10841 ;; given then the first is the anchor position (or nil). The symbol
10842 ;; is matched against `c-offsets-alist' and the offset calculated
10843 ;; from that is returned.
10844 ;;
10845 ;; This function might do hidden buffer changes.
10846 (let* ((symbol (c-langelem-sym langelem))
10847 (match (assq symbol c-offsets-alist))
10848 (offset (cdr-safe match)))
10849 (if match
10850 (setq offset (c-evaluate-offset offset langelem symbol))
10851 (if c-strict-syntax-p
10852 (c-benign-error "No offset found for syntactic symbol %s" symbol))
10853 (setq offset 0))
10854 (if (vectorp offset)
10855 offset
10856 (or (and (numberp offset) offset)
10857 (and (symbolp offset) (symbol-value offset))
10858 0))
10859 ))
10860
10861 (defun c-get-offset (langelem)
10862 ;; This is a compatibility wrapper for `c-calc-offset' in case
10863 ;; someone is calling it directly. It takes an old style syntactic
10864 ;; element on the form (SYMBOL . ANCHOR-POS) and converts it to the
10865 ;; new list form.
10866 ;;
10867 ;; This function might do hidden buffer changes.
10868 (if (c-langelem-pos langelem)
10869 (c-calc-offset (list (c-langelem-sym langelem)
10870 (c-langelem-pos langelem)))
10871 (c-calc-offset langelem)))
10872
10873 (defun c-get-syntactic-indentation (langelems)
10874 ;; Calculate the syntactic indentation from a syntactic description
10875 ;; as returned by `c-guess-syntax'.
10876 ;;
10877 ;; Note that topmost-intro always has an anchor position at bol, for
10878 ;; historical reasons. It's often used together with other symbols
10879 ;; that has more sane positions. Since we always use the first
10880 ;; found anchor position, we rely on that these other symbols always
10881 ;; precede topmost-intro in the LANGELEMS list.
10882 ;;
10883 ;; This function might do hidden buffer changes.
10884 (let ((indent 0) anchor)
10885
10886 (while langelems
10887 (let* ((c-syntactic-element (car langelems))
10888 (res (c-calc-offset c-syntactic-element)))
10889
10890 (if (vectorp res)
10891 ;; Got an absolute column that overrides any indentation
10892 ;; we've collected so far, but not the relative
10893 ;; indentation we might get for the nested structures
10894 ;; further down the langelems list.
10895 (setq indent (elt res 0)
10896 anchor (point-min)) ; A position at column 0.
10897
10898 ;; Got a relative change of the current calculated
10899 ;; indentation.
10900 (setq indent (+ indent res))
10901
10902 ;; Use the anchor position from the first syntactic
10903 ;; element with one.
10904 (unless anchor
10905 (setq anchor (c-langelem-pos (car langelems)))))
10906
10907 (setq langelems (cdr langelems))))
10908
10909 (if anchor
10910 (+ indent (save-excursion
10911 (goto-char anchor)
10912 (current-column)))
10913 indent)))
10914
10915 \f
10916 (cc-provide 'cc-engine)
10917
10918 ;;; cc-engine.el ends here