]> code.delx.au - gnu-emacs/blob - lisp/progmodes/cc-engine.el
Merge from emacs-24; up to 2012-12-13T09:45:54Z!lekktu@gmail.com
[gnu-emacs] / lisp / progmodes / cc-engine.el
1 ;;; cc-engine.el --- core syntax guessing engine for CC mode
2
3 ;; Copyright (C) 1985, 1987, 1992-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 START-POINT) - scan forward from START-POINT,
2549 ;; which is not less than the highest position in `c-state-cache' below here.
2550 ;; o - ('backward nil) - scan backwards (from HERE).
2551 ;; o - ('BOD START-POINT) - scan forwards from START-POINT, which is at the
2552 ;; top level.
2553 ;; o - ('IN-LIT nil) - point is inside the literal containing point-min.
2554 (let ((cache-pos (c-get-cache-scan-pos here)) ; highest position below HERE in cache (or 1)
2555 BOD-pos ; position of 2nd BOD before HERE.
2556 strategy ; 'forward, 'backward, 'BOD, or 'IN-LIT.
2557 start-point
2558 how-far) ; putative scanning distance.
2559 (setq good-pos (or good-pos (c-state-get-min-scan-pos)))
2560 (cond
2561 ((< here (c-state-get-min-scan-pos))
2562 (setq strategy 'IN-LIT
2563 start-point nil
2564 cache-pos nil
2565 how-far 0))
2566 ((<= good-pos here)
2567 (setq strategy 'forward
2568 start-point (max good-pos cache-pos)
2569 how-far (- here start-point)))
2570 ((< (- good-pos here) (- here cache-pos)) ; FIXME!!! ; apply some sort of weighting.
2571 (setq strategy 'backward
2572 how-far (- good-pos here)))
2573 (t
2574 (setq strategy 'forward
2575 how-far (- here cache-pos)
2576 start-point cache-pos)))
2577
2578 ;; Might we be better off starting from the top level, two defuns back,
2579 ;; instead? This heuristic no longer works well in C++, where
2580 ;; declarations inside namespace brace blocks are frequently placed at
2581 ;; column zero.
2582 (when (and (not (c-major-mode-is 'c++-mode))
2583 (> how-far c-state-cache-too-far))
2584 (setq BOD-pos (c-get-fallback-scan-pos here)) ; somewhat EXPENSIVE!!!
2585 (if (< (- here BOD-pos) how-far)
2586 (setq strategy 'BOD
2587 start-point BOD-pos)))
2588
2589 (list
2590 strategy
2591 (and (memq strategy '(forward BOD)) start-point))))
2592
2593
2594 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2595 ;; Routines which change `c-state-cache' and associated values.
2596 (defun c-renarrow-state-cache ()
2597 ;; The region (more precisely, point-min) has changed since we
2598 ;; calculated `c-state-cache'. Amend `c-state-cache' accordingly.
2599 (if (< (point-min) c-state-point-min)
2600 ;; If point-min has MOVED BACKWARDS then we drop the state completely.
2601 ;; It would be possible to do a better job here and recalculate the top
2602 ;; only.
2603 (progn
2604 (c-state-mark-point-min-literal)
2605 (setq c-state-cache nil
2606 c-state-cache-good-pos c-state-min-scan-pos
2607 c-state-brace-pair-desert nil))
2608
2609 ;; point-min has MOVED FORWARD.
2610
2611 ;; Is the new point-min inside a (different) literal?
2612 (unless (and c-state-point-min-lit-start ; at prev. point-min
2613 (< (point-min) (c-state-get-min-scan-pos)))
2614 (c-state-mark-point-min-literal))
2615
2616 ;; Cut off a bit of the tail from `c-state-cache'.
2617 (let ((ptr (cons nil c-state-cache))
2618 pa)
2619 (while (and (setq pa (c-state-cache-top-lparen (cdr ptr)))
2620 (>= pa (point-min)))
2621 (setq ptr (cdr ptr)))
2622
2623 (when (consp ptr)
2624 (if (eq (cdr ptr) c-state-cache)
2625 (setq c-state-cache nil
2626 c-state-cache-good-pos c-state-min-scan-pos)
2627 (setcdr ptr nil)
2628 (setq c-state-cache-good-pos (1+ (c-state-cache-top-lparen))))
2629 )))
2630
2631 (setq c-state-point-min (point-min)))
2632
2633 (defun c-append-lower-brace-pair-to-state-cache (from &optional upper-lim)
2634 ;; If there is a brace pair preceding FROM in the buffer, at the same level
2635 ;; of nesting (not necessarily immediately preceding), push a cons onto
2636 ;; `c-state-cache' to represent it. FROM must not be inside a literal. If
2637 ;; UPPER-LIM is non-nil, we append the highest brace pair whose "}" is below
2638 ;; UPPER-LIM.
2639 ;;
2640 ;; Return non-nil when this has been done.
2641 ;;
2642 ;; The situation it copes with is this transformation:
2643 ;;
2644 ;; OLD: { (.) {...........}
2645 ;; ^ ^
2646 ;; FROM HERE
2647 ;;
2648 ;; NEW: { {....} (.) {.........
2649 ;; ^ ^ ^
2650 ;; LOWER BRACE PAIR HERE or HERE
2651 ;;
2652 ;; This routine should be fast. Since it can get called a LOT, we maintain
2653 ;; `c-state-brace-pair-desert', a small cache of "failures", such that we
2654 ;; reduce the time wasted in repeated fruitless searches in brace deserts.
2655 (save-excursion
2656 (save-restriction
2657 (let* ((here (point-max))
2658 new-cons
2659 (cache-pos (c-state-cache-top-lparen)) ; might be nil.
2660 (macro-start-or-from
2661 (progn (goto-char from)
2662 (c-beginning-of-macro)
2663 (point)))
2664 (bra ; Position of "{".
2665 ;; Don't start scanning in the middle of a CPP construct unless
2666 ;; it contains HERE - these constructs, in Emacs, are "commented
2667 ;; out" with category properties.
2668 (if (eq (c-get-char-property macro-start-or-from 'category)
2669 'c-cpp-delimiter)
2670 macro-start-or-from
2671 from))
2672 ce) ; Position of "}"
2673 (or upper-lim (setq upper-lim from))
2674
2675 ;; If we're essentially repeating a fruitless search, just give up.
2676 (unless (and c-state-brace-pair-desert
2677 (eq cache-pos (car c-state-brace-pair-desert))
2678 (or (null (car c-state-brace-pair-desert))
2679 (> from (car c-state-brace-pair-desert)))
2680 (<= from (cdr c-state-brace-pair-desert)))
2681 ;; DESERT-LIM. Avoid repeated searching through the cached desert.
2682 (let ((desert-lim
2683 (and c-state-brace-pair-desert
2684 (eq cache-pos (car c-state-brace-pair-desert))
2685 (>= from (cdr c-state-brace-pair-desert))
2686 (cdr c-state-brace-pair-desert)))
2687 ;; CACHE-LIM. This limit will be necessary when an opening
2688 ;; paren at `cache-pos' has just had its matching close paren
2689 ;; inserted into the buffer. `cache-pos' continues to be a
2690 ;; search bound, even though the algorithm below would skip
2691 ;; over the new paren pair.
2692 (cache-lim (and cache-pos (< cache-pos from) cache-pos)))
2693 (widen)
2694 (narrow-to-region
2695 (cond
2696 ((and desert-lim cache-lim)
2697 (max desert-lim cache-lim))
2698 (desert-lim)
2699 (cache-lim)
2700 ((point-min)))
2701 (point-max)))
2702
2703 ;; In the next pair of nested loops, the inner one moves back past a
2704 ;; pair of (mis-)matching parens or brackets; the outer one moves
2705 ;; back over a sequence of unmatched close brace/paren/bracket each
2706 ;; time round.
2707 (while
2708 (progn
2709 (c-safe
2710 (while
2711 (and (setq ce (scan-lists bra -1 -1)) ; back past )/]/}; might signal
2712 (setq bra (scan-lists ce -1 1)) ; back past (/[/{; might signal
2713 (or (> bra here) ;(> ce here)
2714 (and
2715 (< ce here)
2716 (or (not (eq (char-after bra) ?\{))
2717 (and (goto-char bra)
2718 (c-beginning-of-macro)
2719 (< (point) macro-start-or-from))))))))
2720 (and ce (< ce bra)))
2721 (setq bra ce)) ; If we just backed over an unbalanced closing
2722 ; brace, ignore it.
2723
2724 (if (and ce (< ce here) (< bra ce) (eq (char-after bra) ?\{))
2725 ;; We've found the desired brace-pair.
2726 (progn
2727 (setq new-cons (cons bra (1+ ce)))
2728 (cond
2729 ((consp (car c-state-cache))
2730 (setcar c-state-cache new-cons))
2731 ((and (numberp (car c-state-cache)) ; probably never happens
2732 (< ce (car c-state-cache)))
2733 (setcdr c-state-cache
2734 (cons new-cons (cdr c-state-cache))))
2735 (t (setq c-state-cache (cons new-cons c-state-cache)))))
2736
2737 ;; We haven't found a brace pair. Record this in the cache.
2738 (setq c-state-brace-pair-desert
2739 (cons (if (and ce (< bra ce) (> ce here)) ; {..} straddling HERE?
2740 bra
2741 (point-min))
2742 (min here from)))))))))
2743
2744 (defsubst c-state-push-any-brace-pair (bra+1 macro-start-or-here)
2745 ;; If BRA+1 is nil, do nothing. Otherwise, BRA+1 is the buffer position
2746 ;; following a {, and that brace has a (mis-)matching } (or ]), and we
2747 ;; "push" "a" brace pair onto `c-state-cache'.
2748 ;;
2749 ;; Here "push" means overwrite the top element if it's itself a brace-pair,
2750 ;; otherwise push it normally.
2751 ;;
2752 ;; The brace pair we push is normally the one surrounding BRA+1, but if the
2753 ;; latter is inside a macro, not being a macro containing
2754 ;; MACRO-START-OR-HERE, we scan backwards through the buffer for a non-macro
2755 ;; base pair. This latter case is assumed to be rare.
2756 ;;
2757 ;; Note: POINT is not preserved in this routine.
2758 (if bra+1
2759 (if (or (> bra+1 macro-start-or-here)
2760 (progn (goto-char bra+1)
2761 (not (c-beginning-of-macro))))
2762 (setq c-state-cache
2763 (cons (cons (1- bra+1)
2764 (scan-lists bra+1 1 1))
2765 (if (consp (car c-state-cache))
2766 (cdr c-state-cache)
2767 c-state-cache)))
2768 ;; N.B. This defsubst codes one method for the simple, normal case,
2769 ;; and a more sophisticated, slower way for the general case. Don't
2770 ;; eliminate this defsubst - it's a speed optimization.
2771 (c-append-lower-brace-pair-to-state-cache (1- bra+1)))))
2772
2773 (defun c-append-to-state-cache (from)
2774 ;; Scan the buffer from FROM to (point-max), adding elements into
2775 ;; `c-state-cache' for braces etc. Return a candidate for
2776 ;; `c-state-cache-good-pos'.
2777 ;;
2778 ;; FROM must be after the latest brace/paren/bracket in `c-state-cache', if
2779 ;; any. Typically, it is immediately after it. It must not be inside a
2780 ;; literal.
2781 (let ((here-bol (c-point 'bol (point-max)))
2782 (macro-start-or-here
2783 (save-excursion (goto-char (point-max))
2784 (if (c-beginning-of-macro)
2785 (point)
2786 (point-max))))
2787 pa+1 ; pos just after an opening PAren (or brace).
2788 (ren+1 from) ; usually a pos just after an closing paREN etc.
2789 ; Is actually the pos. to scan for a (/{/[ from,
2790 ; which sometimes is after a silly )/}/].
2791 paren+1 ; Pos after some opening or closing paren.
2792 paren+1s ; A list of `paren+1's; used to determine a
2793 ; good-pos.
2794 bra+1 ce+1 ; just after L/R bra-ces.
2795 bra+1s ; list of OLD values of bra+1.
2796 mstart) ; start of a macro.
2797
2798 (save-excursion
2799 ;; Each time round the following loop, we enter a successively deeper
2800 ;; level of brace/paren nesting. (Except sometimes we "continue at
2801 ;; the existing level".) `pa+1' is a pos inside an opening
2802 ;; brace/paren/bracket, usually just after it.
2803 (while
2804 (progn
2805 ;; Each time round the next loop moves forward over an opening then
2806 ;; a closing brace/bracket/paren. This loop is white hot, so it
2807 ;; plays ugly tricks to go fast. DON'T PUT ANYTHING INTO THIS
2808 ;; LOOP WHICH ISN'T ABSOLUTELY NECESSARY!!! It terminates when a
2809 ;; call of `scan-lists' signals an error, which happens when there
2810 ;; are no more b/b/p's to scan.
2811 (c-safe
2812 (while t
2813 (setq pa+1 (scan-lists ren+1 1 -1) ; Into (/{/[; might signal
2814 paren+1s (cons pa+1 paren+1s))
2815 (setq ren+1 (scan-lists pa+1 1 1)) ; Out of )/}/]; might signal
2816 (if (and (eq (char-before pa+1) ?{)) ; Check for a macro later.
2817 (setq bra+1 pa+1))
2818 (setcar paren+1s ren+1)))
2819
2820 (if (and pa+1 (> pa+1 ren+1))
2821 ;; We've just entered a deeper nesting level.
2822 (progn
2823 ;; Insert the brace pair (if present) and the single open
2824 ;; paren/brace/bracket into `c-state-cache' It cannot be
2825 ;; inside a macro, except one around point, because of what
2826 ;; `c-neutralize-syntax-in-CPP' has done.
2827 (c-state-push-any-brace-pair bra+1 macro-start-or-here)
2828 ;; Insert the opening brace/bracket/paren position.
2829 (setq c-state-cache (cons (1- pa+1) c-state-cache))
2830 ;; Clear admin stuff for the next more nested part of the scan.
2831 (setq ren+1 pa+1 pa+1 nil bra+1 nil bra+1s nil)
2832 t) ; Carry on the loop
2833
2834 ;; All open p/b/b's at this nesting level, if any, have probably
2835 ;; been closed by matching/mismatching ones. We're probably
2836 ;; finished - we just need to check for having found an
2837 ;; unmatched )/}/], which we ignore. Such a )/}/] can't be in a
2838 ;; macro, due the action of `c-neutralize-syntax-in-CPP'.
2839 (c-safe (setq ren+1 (scan-lists ren+1 1 1)))))) ; acts as loop control.
2840
2841 ;; Record the final, innermost, brace-pair if there is one.
2842 (c-state-push-any-brace-pair bra+1 macro-start-or-here)
2843
2844 ;; Determine a good pos
2845 (while (and (setq paren+1 (car paren+1s))
2846 (> (if (> paren+1 macro-start-or-here)
2847 paren+1
2848 (goto-char paren+1)
2849 (setq mstart (and (c-beginning-of-macro)
2850 (point)))
2851 (or mstart paren+1))
2852 here-bol))
2853 (setq paren+1s (cdr paren+1s)))
2854 (cond
2855 ((and paren+1 mstart)
2856 (min paren+1 mstart))
2857 (paren+1)
2858 (t from)))))
2859
2860 (defun c-remove-stale-state-cache (start-point pps-point)
2861 ;; Remove stale entries from the `c-cache-state', i.e. those which will
2862 ;; not be in it when it is amended for position (point-max).
2863 ;; Additionally, the "outermost" open-brace entry before (point-max)
2864 ;; will be converted to a cons if the matching close-brace is scanned.
2865 ;;
2866 ;; START-POINT is a "maximal" "safe position" - there must be no open
2867 ;; parens/braces/brackets between START-POINT and (point-max).
2868 ;;
2869 ;; As a second thing, calculate the result of parse-partial-sexp at
2870 ;; PPS-POINT, w.r.t. START-POINT. The motivation here is that
2871 ;; `c-state-cache-good-pos' may become PPS-POINT, but the caller may need to
2872 ;; adjust it to get outside a string/comment. (Sorry about this! The code
2873 ;; needs to be FAST).
2874 ;;
2875 ;; Return a list (GOOD-POS SCAN-BACK-POS PPS-STATE), where
2876 ;; o - GOOD-POS is a position where the new value `c-state-cache' is known
2877 ;; to be good (we aim for this to be as high as possible);
2878 ;; o - SCAN-BACK-POS, if not nil, indicates there may be a brace pair
2879 ;; preceding POS which needs to be recorded in `c-state-cache'. It is a
2880 ;; position to scan backwards from. It is the position of the "{" of the
2881 ;; last element to be removed from `c-state-cache', when that elt is a
2882 ;; cons, otherwise nil.
2883 ;; o - PPS-STATE is the parse-partial-sexp state at PPS-POINT.
2884 (save-restriction
2885 (narrow-to-region 1 (point-max))
2886 (save-excursion
2887 (let* ((in-macro-start ; start of macro containing (point-max) or nil.
2888 (save-excursion
2889 (goto-char (point-max))
2890 (and (c-beginning-of-macro)
2891 (point))))
2892 (start-point-actual-macro-start ; Start of macro containing
2893 ; start-point or nil
2894 (and (< start-point (point-max))
2895 (save-excursion
2896 (goto-char start-point)
2897 (and (c-beginning-of-macro)
2898 (point)))))
2899 (start-point-actual-macro-end ; End of this macro, (maybe
2900 ; (point-max)), or nil.
2901 (and start-point-actual-macro-start
2902 (save-excursion
2903 (goto-char start-point-actual-macro-start)
2904 (c-end-of-macro)
2905 (point))))
2906 pps-state ; Will be 9 or 10 elements long.
2907 pos
2908 upper-lim ; ,beyond which `c-state-cache' entries are removed
2909 scan-back-pos
2910 pair-beg pps-point-state target-depth)
2911
2912 ;; Remove entries beyond (point-max). Also remove any entries inside
2913 ;; a macro, unless (point-max) is in the same macro.
2914 (setq upper-lim
2915 (if (or (null c-state-old-cpp-beg)
2916 (and (> (point-max) c-state-old-cpp-beg)
2917 (< (point-max) c-state-old-cpp-end)))
2918 (point-max)
2919 (min (point-max) c-state-old-cpp-beg)))
2920 (while (and c-state-cache (>= (c-state-cache-top-lparen) upper-lim))
2921 (setq scan-back-pos (car-safe (car c-state-cache)))
2922 (setq c-state-cache (cdr c-state-cache)))
2923
2924 ;; If `upper-lim' is inside the last recorded brace pair, remove its
2925 ;; RBrace and indicate we'll need to search backwards for a previous
2926 ;; brace pair.
2927 (when (and c-state-cache
2928 (consp (car c-state-cache))
2929 (> (cdar c-state-cache) upper-lim))
2930 (setcar c-state-cache (caar c-state-cache))
2931 (setq scan-back-pos (car c-state-cache)))
2932
2933 ;; The next loop jumps forward out of a nested level of parens each
2934 ;; time round; the corresponding elements in `c-state-cache' are
2935 ;; removed. `pos' is just after the brace-pair or the open paren at
2936 ;; (car c-state-cache). There can be no open parens/braces/brackets
2937 ;; between `start-point'/`start-point-actual-macro-start' and (point-max),
2938 ;; due to the interface spec to this function.
2939 (setq pos (if (and start-point-actual-macro-end
2940 (not (eq start-point-actual-macro-start
2941 in-macro-start)))
2942 (1+ start-point-actual-macro-end) ; get outside the macro as
2943 ; marked by a `category' text property.
2944 start-point))
2945 (goto-char pos)
2946 (while (and c-state-cache
2947 (< (point) (point-max)))
2948 (cond
2949 ((null pps-state) ; first time through
2950 (setq target-depth -1))
2951 ((eq (car pps-state) target-depth) ; found closing ),},]
2952 (setq target-depth (1- (car pps-state))))
2953 ;; Do nothing when we've merely reached pps-point.
2954 )
2955
2956 ;; Scan!
2957 (setq pps-state
2958 (parse-partial-sexp
2959 (point) (if (< (point) pps-point) pps-point (point-max))
2960 target-depth
2961 nil pps-state))
2962
2963 (if (= (point) pps-point)
2964 (setq pps-point-state pps-state))
2965
2966 (when (eq (car pps-state) target-depth)
2967 (setq pos (point)) ; POS is now just after an R-paren/brace.
2968 (cond
2969 ((and (consp (car c-state-cache))
2970 (eq (point) (cdar c-state-cache)))
2971 ;; We've just moved out of the paren pair containing the brace-pair
2972 ;; at (car c-state-cache). `pair-beg' is where the open paren is,
2973 ;; and is potentially where the open brace of a cons in
2974 ;; c-state-cache will be.
2975 (setq pair-beg (car-safe (cdr c-state-cache))
2976 c-state-cache (cdr-safe (cdr c-state-cache)))) ; remove {}pair + containing Lparen.
2977 ((numberp (car c-state-cache))
2978 (setq pair-beg (car c-state-cache)
2979 c-state-cache (cdr c-state-cache))) ; remove this
2980 ; containing Lparen
2981 ((numberp (cadr c-state-cache))
2982 (setq pair-beg (cadr c-state-cache)
2983 c-state-cache (cddr c-state-cache))) ; Remove a paren pair
2984 ; together with enclosed brace pair.
2985 ;; (t nil) ; Ignore an unmated Rparen.
2986 )))
2987
2988 (if (< (point) pps-point)
2989 (setq pps-state (parse-partial-sexp (point) pps-point
2990 nil nil ; TARGETDEPTH, STOPBEFORE
2991 pps-state)))
2992
2993 ;; If the last paren pair we moved out of was actually a brace pair,
2994 ;; insert it into `c-state-cache'.
2995 (when (and pair-beg (eq (char-after pair-beg) ?{))
2996 (if (consp (car-safe c-state-cache))
2997 (setq c-state-cache (cdr c-state-cache)))
2998 (setq c-state-cache (cons (cons pair-beg pos)
2999 c-state-cache)))
3000
3001 (list pos scan-back-pos pps-state)))))
3002
3003 (defun c-remove-stale-state-cache-backwards (here)
3004 ;; Strip stale elements of `c-state-cache' by moving backwards through the
3005 ;; buffer, and inform the caller of the scenario detected.
3006 ;;
3007 ;; HERE is the position we're setting `c-state-cache' for.
3008 ;; CACHE-POS (a locally bound variable) is just after the latest recorded
3009 ;; position in `c-state-cache' before HERE, or a position at or near
3010 ;; point-min which isn't in a literal.
3011 ;;
3012 ;; This function must only be called only when (> `c-state-cache-good-pos'
3013 ;; HERE). Usually the gap between CACHE-POS and HERE is large. It is thus
3014 ;; optimized to eliminate (or minimize) scanning between these two
3015 ;; positions.
3016 ;;
3017 ;; Return a three element list (GOOD-POS SCAN-BACK-POS FWD-FLAG), where:
3018 ;; o - GOOD-POS is a "good position", where `c-state-cache' is valid, or
3019 ;; could become so after missing elements are inserted into
3020 ;; `c-state-cache'. This is JUST AFTER an opening or closing
3021 ;; brace/paren/bracket which is already in `c-state-cache' or just before
3022 ;; one otherwise. exceptionally (when there's no such b/p/b handy) the BOL
3023 ;; before `here''s line, or the start of the literal containing it.
3024 ;; o - SCAN-BACK-POS, if non-nil, indicates there may be a brace pair
3025 ;; preceding POS which isn't recorded in `c-state-cache'. It is a position
3026 ;; to scan backwards from.
3027 ;; o - FWD-FLAG, if non-nil, indicates there may be parens/braces between
3028 ;; POS and HERE which aren't recorded in `c-state-cache'.
3029 ;;
3030 ;; The comments in this defun use "paren" to mean parenthesis or square
3031 ;; bracket (as contrasted with a brace), and "(" and ")" likewise.
3032 ;;
3033 ;; . {..} (..) (..) ( .. { } ) (...) ( .... . ..)
3034 ;; | | | | | |
3035 ;; CP E here D C good
3036 (let ((cache-pos (c-get-cache-scan-pos here)) ; highest position below HERE in cache (or 1)
3037 (pos c-state-cache-good-pos)
3038 pa ren ; positions of "(" and ")"
3039 dropped-cons ; whether the last element dropped from `c-state-cache'
3040 ; was a cons (representing a brace-pair)
3041 good-pos ; see above.
3042 lit ; (START . END) of a literal containing some point.
3043 here-lit-start here-lit-end ; bounds of literal containing `here'
3044 ; or `here' itself.
3045 here- here+ ; start/end of macro around HERE, or HERE
3046 (here-bol (c-point 'bol here))
3047 (too-far-back (max (- here c-state-cache-too-far) (point-min))))
3048
3049 ;; Remove completely irrelevant entries from `c-state-cache'.
3050 (while (and c-state-cache
3051 (>= (setq pa (c-state-cache-top-lparen)) here))
3052 (setq dropped-cons (consp (car c-state-cache)))
3053 (setq c-state-cache (cdr c-state-cache))
3054 (setq pos pa))
3055 ;; At this stage, (> pos here);
3056 ;; (< (c-state-cache-top-lparen) here) (or is nil).
3057
3058 (cond
3059 ((and (consp (car c-state-cache))
3060 (> (cdar c-state-cache) here))
3061 ;; CASE 1: The top of the cache is a brace pair which now encloses
3062 ;; `here'. As good-pos, return the address. of the "{". Since we've no
3063 ;; knowledge of what's inside these braces, we have no alternative but
3064 ;; to direct the caller to scan the buffer from the opening brace.
3065 (setq pos (caar c-state-cache))
3066 (setcar c-state-cache pos)
3067 (list (1+ pos) pos t)) ; return value. We've just converted a brace pair
3068 ; entry into a { entry, so the caller needs to
3069 ; search for a brace pair before the {.
3070
3071 ;; `here' might be inside a literal. Check for this.
3072 ((progn
3073 (setq lit (c-state-literal-at here)
3074 here-lit-start (or (car lit) here)
3075 here-lit-end (or (cdr lit) here))
3076 ;; Has `here' just "newly entered" a macro?
3077 (save-excursion
3078 (goto-char here-lit-start)
3079 (if (and (c-beginning-of-macro)
3080 (or (null c-state-old-cpp-beg)
3081 (not (= (point) c-state-old-cpp-beg))))
3082 (progn
3083 (setq here- (point))
3084 (c-end-of-macro)
3085 (setq here+ (point)))
3086 (setq here- here-lit-start
3087 here+ here-lit-end)))
3088
3089 ;; `here' might be nested inside any depth of parens (or brackets but
3090 ;; not braces). Scan backwards to find the outermost such opening
3091 ;; paren, if there is one. This will be the scan position to return.
3092 (save-restriction
3093 (narrow-to-region cache-pos (point-max))
3094 (setq pos (c-state-balance-parens-backwards here- here+ pos)))
3095 nil)) ; for the cond
3096
3097 ((< pos here-lit-start)
3098 ;; CASE 2: Address of outermost ( or [ which now encloses `here', but
3099 ;; didn't enclose the (previous) `c-state-cache-good-pos'. If there is
3100 ;; a brace pair preceding this, it will already be in `c-state-cache',
3101 ;; unless there was a brace pair after it, i.e. there'll only be one to
3102 ;; scan for if we've just deleted one.
3103 (list pos (and dropped-cons pos) t)) ; Return value.
3104
3105 ;; `here' isn't enclosed in a (previously unrecorded) bracket/paren.
3106 ;; Further forward scanning isn't needed, but we still need to find a
3107 ;; GOOD-POS. Step out of all enclosing "("s on HERE's line.
3108 ((progn
3109 (save-restriction
3110 (narrow-to-region here-bol (point-max))
3111 (setq pos here-lit-start)
3112 (c-safe (while (setq pa (scan-lists pos -1 1))
3113 (setq pos pa)))) ; might signal
3114 nil)) ; for the cond
3115
3116 ((setq ren (c-safe-scan-lists pos -1 -1 too-far-back))
3117 ;; CASE 3: After a }/)/] before `here''s BOL.
3118 (list (1+ ren) (and dropped-cons pos) nil)) ; Return value
3119
3120 (t
3121 ;; CASE 4; Best of a bad job: BOL before `here-bol', or beginning of
3122 ;; literal containing it.
3123 (setq good-pos (c-state-lit-beg (c-point 'bopl here-bol)))
3124 (list good-pos (and dropped-cons good-pos) nil)))))
3125
3126
3127 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
3128 ;; Externally visible routines.
3129
3130 (defun c-state-cache-init ()
3131 (setq c-state-cache nil
3132 c-state-cache-good-pos 1
3133 c-state-nonlit-pos-cache nil
3134 c-state-nonlit-pos-cache-limit 1
3135 c-state-semi-nonlit-pos-cache nil
3136 c-state-semi-nonlit-pos-cache-limit 1
3137 c-state-brace-pair-desert nil
3138 c-state-point-min 1
3139 c-state-point-min-lit-type nil
3140 c-state-point-min-lit-start nil
3141 c-state-min-scan-pos 1
3142 c-state-old-cpp-beg nil
3143 c-state-old-cpp-end nil)
3144 (c-state-mark-point-min-literal))
3145
3146 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
3147 ;; Debugging routines to dump `c-state-cache' in a "replayable" form.
3148 ;; (defmacro c-sc-de (elt) ; "c-state-cache-dump-element"
3149 ;; `(format ,(concat "(setq " (symbol-name elt) " %s) ") ,elt))
3150 ;; (defmacro c-sc-qde (elt) ; "c-state-cache-quote-dump-element"
3151 ;; `(format ,(concat "(setq " (symbol-name elt) " '%s) ") ,elt))
3152 ;; (defun c-state-dump ()
3153 ;; ;; For debugging.
3154 ;; ;(message
3155 ;; (concat
3156 ;; (c-sc-qde c-state-cache)
3157 ;; (c-sc-de c-state-cache-good-pos)
3158 ;; (c-sc-qde c-state-nonlit-pos-cache)
3159 ;; (c-sc-de c-state-nonlit-pos-cache-limit)
3160 ;; (c-sc-qde c-state-brace-pair-desert)
3161 ;; (c-sc-de c-state-point-min)
3162 ;; (c-sc-de c-state-point-min-lit-type)
3163 ;; (c-sc-de c-state-point-min-lit-start)
3164 ;; (c-sc-de c-state-min-scan-pos)
3165 ;; (c-sc-de c-state-old-cpp-beg)
3166 ;; (c-sc-de c-state-old-cpp-end)))
3167 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
3168
3169 (defun c-invalidate-state-cache-1 (here)
3170 ;; Invalidate all info on `c-state-cache' that applies to the buffer at HERE
3171 ;; or higher and set `c-state-cache-good-pos' accordingly. The cache is
3172 ;; left in a consistent state.
3173 ;;
3174 ;; This is much like `c-whack-state-after', but it never changes a paren
3175 ;; pair element into an open paren element. Doing that would mean that the
3176 ;; new open paren wouldn't have the required preceding paren pair element.
3177 ;;
3178 ;; This function is called from c-after-change.
3179
3180 ;; The caches of non-literals:
3181 (if (< here c-state-nonlit-pos-cache-limit)
3182 (setq c-state-nonlit-pos-cache-limit here))
3183 (if (< here c-state-semi-nonlit-pos-cache-limit)
3184 (setq c-state-semi-nonlit-pos-cache-limit here))
3185
3186 ;; `c-state-cache':
3187 ;; Case 1: if `here' is in a literal containing point-min, everything
3188 ;; becomes (or is already) nil.
3189 (if (or (null c-state-cache-good-pos)
3190 (< here (c-state-get-min-scan-pos)))
3191 (setq c-state-cache nil
3192 c-state-cache-good-pos nil
3193 c-state-min-scan-pos nil)
3194
3195 ;; Truncate `c-state-cache' and set `c-state-cache-good-pos' to a value
3196 ;; below `here'. To maintain its consistency, we may need to insert a new
3197 ;; brace pair.
3198 (let ((here-bol (c-point 'bol here))
3199 too-high-pa ; recorded {/(/[ next above here, or nil.
3200 dropped-cons ; was the last removed element a brace pair?
3201 pa)
3202 ;; The easy bit - knock over-the-top bits off `c-state-cache'.
3203 (while (and c-state-cache
3204 (>= (setq pa (c-state-cache-top-paren)) here))
3205 (setq dropped-cons (consp (car c-state-cache))
3206 too-high-pa (c-state-cache-top-lparen)
3207 c-state-cache (cdr c-state-cache)))
3208
3209 ;; Do we need to add in an earlier brace pair, having lopped one off?
3210 (if (and dropped-cons
3211 (< too-high-pa (+ here c-state-cache-too-far)))
3212 (c-append-lower-brace-pair-to-state-cache too-high-pa here-bol))
3213 (setq c-state-cache-good-pos (or (c-state-cache-after-top-paren)
3214 (c-state-get-min-scan-pos)))))
3215
3216 ;; The brace-pair desert marker:
3217 (when (car c-state-brace-pair-desert)
3218 (if (< here (car c-state-brace-pair-desert))
3219 (setq c-state-brace-pair-desert nil)
3220 (if (< here (cdr c-state-brace-pair-desert))
3221 (setcdr c-state-brace-pair-desert here)))))
3222
3223 (defun c-parse-state-1 ()
3224 ;; Find and record all noteworthy parens between some good point earlier in
3225 ;; the file and point. That good point is at least the beginning of the
3226 ;; top-level construct we are in, or the beginning of the preceding
3227 ;; top-level construct if we aren't in one.
3228 ;;
3229 ;; The returned value is a list of the noteworthy parens with the last one
3230 ;; first. If an element in the list is an integer, it's the position of an
3231 ;; open paren (of any type) which has not been closed before the point. If
3232 ;; an element is a cons, it gives the position of a closed BRACE paren
3233 ;; pair[*]; the car is the start brace position and the cdr is the position
3234 ;; following the closing brace. Only the last closed brace paren pair
3235 ;; before each open paren and before the point is recorded, and thus the
3236 ;; state never contains two cons elements in succession. When a close brace
3237 ;; has no matching open brace (e.g., the matching brace is outside the
3238 ;; visible region), it is not represented in the returned value.
3239 ;;
3240 ;; [*] N.B. The close "brace" might be a mismatching close bracket or paren.
3241 ;; This defun explicitly treats mismatching parens/braces/brackets as
3242 ;; matching. It is the open brace which makes it a "brace" pair.
3243 ;;
3244 ;; If POINT is within a macro, open parens and brace pairs within
3245 ;; THIS macro MIGHT be recorded. This depends on whether their
3246 ;; syntactic properties have been suppressed by
3247 ;; `c-neutralize-syntax-in-CPP'. This might need fixing (2008-12-11).
3248 ;;
3249 ;; Currently no characters which are given paren syntax with the
3250 ;; syntax-table property are recorded, i.e. angle bracket arglist
3251 ;; parens are never present here. Note that this might change.
3252 ;;
3253 ;; BUG: This function doesn't cope entirely well with unbalanced
3254 ;; parens in macros. (2008-12-11: this has probably been resolved
3255 ;; by the function `c-neutralize-syntax-in-CPP'.) E.g. in the
3256 ;; following case the brace before the macro isn't balanced with the
3257 ;; one after it:
3258 ;;
3259 ;; {
3260 ;; #define X {
3261 ;; }
3262 ;;
3263 ;; Note to maintainers: this function DOES get called with point
3264 ;; within comments and strings, so don't assume it doesn't!
3265 ;;
3266 ;; This function might do hidden buffer changes.
3267 (let* ((here (point))
3268 (here-bopl (c-point 'bopl))
3269 strategy ; 'forward, 'backward etc..
3270 ;; Candidate positions to start scanning from:
3271 cache-pos ; highest position below HERE already existing in
3272 ; cache (or 1).
3273 good-pos
3274 start-point ; (when scanning forward) a place below HERE where there
3275 ; are no open parens/braces between it and HERE.
3276 bopl-state
3277 res
3278 scan-backward-pos scan-forward-p) ; used for 'backward.
3279 ;; If POINT-MIN has changed, adjust the cache
3280 (unless (= (point-min) c-state-point-min)
3281 (c-renarrow-state-cache))
3282
3283 ;; Strategy?
3284 (setq res (c-parse-state-get-strategy here c-state-cache-good-pos)
3285 strategy (car res)
3286 start-point (cadr res))
3287
3288 (when (eq strategy 'BOD)
3289 (setq c-state-cache nil
3290 c-state-cache-good-pos start-point))
3291
3292 ;; SCAN!
3293 (save-restriction
3294 (cond
3295 ((memq strategy '(forward BOD))
3296 (narrow-to-region (point-min) here)
3297 (setq res (c-remove-stale-state-cache start-point here-bopl))
3298 (setq cache-pos (car res)
3299 scan-backward-pos (cadr res)
3300 bopl-state (car (cddr res))) ; will be nil if (< here-bopl
3301 ; start-point)
3302 (if scan-backward-pos
3303 (c-append-lower-brace-pair-to-state-cache scan-backward-pos))
3304 (setq good-pos
3305 (c-append-to-state-cache cache-pos))
3306 (setq c-state-cache-good-pos
3307 (if (and bopl-state
3308 (< good-pos (- here c-state-cache-too-far)))
3309 (c-state-cache-non-literal-place here-bopl bopl-state)
3310 good-pos)))
3311
3312 ((eq strategy 'backward)
3313 (setq res (c-remove-stale-state-cache-backwards here)
3314 good-pos (car res)
3315 scan-backward-pos (cadr res)
3316 scan-forward-p (car (cddr res)))
3317 (if scan-backward-pos
3318 (c-append-lower-brace-pair-to-state-cache
3319 scan-backward-pos))
3320 (setq c-state-cache-good-pos
3321 (if scan-forward-p
3322 (progn (narrow-to-region (point-min) here)
3323 (c-append-to-state-cache good-pos))
3324 good-pos)))
3325
3326 (t ; (eq strategy 'IN-LIT)
3327 (setq c-state-cache nil
3328 c-state-cache-good-pos nil)))))
3329
3330 c-state-cache)
3331
3332 (defun c-invalidate-state-cache (here)
3333 ;; This is a wrapper over `c-invalidate-state-cache-1'.
3334 ;;
3335 ;; It suppresses the syntactic effect of the < and > (template) brackets and
3336 ;; of all parens in preprocessor constructs, except for any such construct
3337 ;; containing point. We can then call `c-invalidate-state-cache-1' without
3338 ;; worrying further about macros and template delimiters.
3339 (c-with-<->-as-parens-suppressed
3340 (if (and c-state-old-cpp-beg
3341 (< c-state-old-cpp-beg here))
3342 (c-with-all-but-one-cpps-commented-out
3343 c-state-old-cpp-beg
3344 (min c-state-old-cpp-end here)
3345 (c-invalidate-state-cache-1 here))
3346 (c-with-cpps-commented-out
3347 (c-invalidate-state-cache-1 here)))))
3348
3349 (defmacro c-state-maybe-marker (place marker)
3350 ;; If PLACE is non-nil, return a marker marking it, otherwise nil.
3351 ;; We (re)use MARKER.
3352 `(and ,place
3353 (or ,marker (setq ,marker (make-marker)))
3354 (set-marker ,marker ,place)))
3355
3356 (defun c-parse-state ()
3357 ;; This is a wrapper over `c-parse-state-1'. See that function for a
3358 ;; description of the functionality and return value.
3359 ;;
3360 ;; It suppresses the syntactic effect of the < and > (template) brackets and
3361 ;; of all parens in preprocessor constructs, except for any such construct
3362 ;; containing point. We can then call `c-parse-state-1' without worrying
3363 ;; further about macros and template delimiters.
3364 (let (here-cpp-beg here-cpp-end)
3365 (save-excursion
3366 (when (c-beginning-of-macro)
3367 (setq here-cpp-beg (point))
3368 (unless
3369 (> (setq here-cpp-end (c-syntactic-end-of-macro))
3370 here-cpp-beg)
3371 (setq here-cpp-beg nil here-cpp-end nil))))
3372 ;; FIXME!!! Put in a `condition-case' here to protect the integrity of the
3373 ;; subsystem.
3374 (prog1
3375 (c-with-<->-as-parens-suppressed
3376 (if (and here-cpp-beg (> here-cpp-end here-cpp-beg))
3377 (c-with-all-but-one-cpps-commented-out
3378 here-cpp-beg here-cpp-end
3379 (c-parse-state-1))
3380 (c-with-cpps-commented-out
3381 (c-parse-state-1))))
3382 (setq c-state-old-cpp-beg
3383 (c-state-maybe-marker here-cpp-beg c-state-old-cpp-beg-marker)
3384 c-state-old-cpp-end
3385 (c-state-maybe-marker here-cpp-end c-state-old-cpp-end-marker)))))
3386
3387 ;; Debug tool to catch cache inconsistencies. This is called from
3388 ;; 000tests.el.
3389 (defvar c-debug-parse-state nil)
3390 (unless (fboundp 'c-real-parse-state)
3391 (fset 'c-real-parse-state (symbol-function 'c-parse-state)))
3392 (cc-bytecomp-defun c-real-parse-state)
3393
3394 (defvar c-parse-state-point nil)
3395 (defvar c-parse-state-state nil)
3396 (defun c-record-parse-state-state ()
3397 (setq c-parse-state-point (point))
3398 (setq c-parse-state-state
3399 (mapcar
3400 (lambda (arg)
3401 (let ((val (symbol-value arg)))
3402 (cons arg
3403 (if (consp val)
3404 (copy-tree val)
3405 val))))
3406 '(c-state-cache
3407 c-state-cache-good-pos
3408 c-state-nonlit-pos-cache
3409 c-state-nonlit-pos-cache-limit
3410 c-state-semi-nonlit-pos-cache
3411 c-state-semi-nonlit-pos-cache-limit
3412 c-state-brace-pair-desert
3413 c-state-point-min
3414 c-state-point-min-lit-type
3415 c-state-point-min-lit-start
3416 c-state-min-scan-pos
3417 c-state-old-cpp-beg
3418 c-state-old-cpp-end
3419 c-parse-state-point))))
3420 (defun c-replay-parse-state-state ()
3421 (message
3422 (concat "(setq "
3423 (mapconcat
3424 (lambda (arg)
3425 (format "%s %s%s" (car arg) (if (atom (cdr arg)) "" "'") (cdr arg)))
3426 c-parse-state-state " ")
3427 ")")))
3428
3429 (defun c-debug-parse-state-double-cons (state)
3430 (let (state-car conses-not-ok)
3431 (while state
3432 (setq state-car (car state)
3433 state (cdr state))
3434 (if (and (consp state-car)
3435 (consp (car state)))
3436 (setq conses-not-ok t)))
3437 conses-not-ok))
3438
3439 (defun c-debug-parse-state ()
3440 (let ((here (point)) (res1 (c-real-parse-state)) res2)
3441 (let ((c-state-cache nil)
3442 (c-state-cache-good-pos 1)
3443 (c-state-nonlit-pos-cache nil)
3444 (c-state-nonlit-pos-cache-limit 1)
3445 (c-state-brace-pair-desert nil)
3446 (c-state-point-min 1)
3447 (c-state-point-min-lit-type nil)
3448 (c-state-point-min-lit-start nil)
3449 (c-state-min-scan-pos 1)
3450 (c-state-old-cpp-beg nil)
3451 (c-state-old-cpp-end nil))
3452 (setq res2 (c-real-parse-state)))
3453 (unless (equal res1 res2)
3454 ;; The cache can actually go further back due to the ad-hoc way
3455 ;; the first paren is found, so try to whack off a bit of its
3456 ;; start before complaining.
3457 ;; (save-excursion
3458 ;; (goto-char (or (c-least-enclosing-brace res2) (point)))
3459 ;; (c-beginning-of-defun-1)
3460 ;; (while (not (or (bobp) (eq (char-after) ?{)))
3461 ;; (c-beginning-of-defun-1))
3462 ;; (unless (equal (c-whack-state-before (point) res1) res2)
3463 ;; (message (concat "c-parse-state inconsistency at %s: "
3464 ;; "using cache: %s, from scratch: %s")
3465 ;; here res1 res2)))
3466 (message (concat "c-parse-state inconsistency at %s: "
3467 "using cache: %s, from scratch: %s")
3468 here res1 res2)
3469 (message "Old state:")
3470 (c-replay-parse-state-state))
3471
3472 (when (c-debug-parse-state-double-cons res1)
3473 (message "c-parse-state INVALIDITY at %s: %s"
3474 here res1)
3475 (message "Old state:")
3476 (c-replay-parse-state-state))
3477
3478 (c-record-parse-state-state)
3479 res2 ; res1 correct a cascading series of errors ASAP
3480 ))
3481
3482 (defun c-toggle-parse-state-debug (&optional arg)
3483 (interactive "P")
3484 (setq c-debug-parse-state (c-calculate-state arg c-debug-parse-state))
3485 (fset 'c-parse-state (symbol-function (if c-debug-parse-state
3486 'c-debug-parse-state
3487 'c-real-parse-state)))
3488 (c-keep-region-active)
3489 (message "c-debug-parse-state %sabled"
3490 (if c-debug-parse-state "en" "dis")))
3491 (when c-debug-parse-state
3492 (c-toggle-parse-state-debug 1))
3493
3494 \f
3495 (defun c-whack-state-before (bufpos paren-state)
3496 ;; Whack off any state information from PAREN-STATE which lies
3497 ;; before BUFPOS. Not destructive on PAREN-STATE.
3498 (let* ((newstate (list nil))
3499 (ptr newstate)
3500 car)
3501 (while paren-state
3502 (setq car (car paren-state)
3503 paren-state (cdr paren-state))
3504 (if (< (if (consp car) (car car) car) bufpos)
3505 (setq paren-state nil)
3506 (setcdr ptr (list car))
3507 (setq ptr (cdr ptr))))
3508 (cdr newstate)))
3509
3510 (defun c-whack-state-after (bufpos paren-state)
3511 ;; Whack off any state information from PAREN-STATE which lies at or
3512 ;; after BUFPOS. Not destructive on PAREN-STATE.
3513 (catch 'done
3514 (while paren-state
3515 (let ((car (car paren-state)))
3516 (if (consp car)
3517 ;; just check the car, because in a balanced brace
3518 ;; expression, it must be impossible for the corresponding
3519 ;; close brace to be before point, but the open brace to
3520 ;; be after.
3521 (if (<= bufpos (car car))
3522 nil ; whack it off
3523 (if (< bufpos (cdr car))
3524 ;; its possible that the open brace is before
3525 ;; bufpos, but the close brace is after. In that
3526 ;; case, convert this to a non-cons element. The
3527 ;; rest of the state is before bufpos, so we're
3528 ;; done.
3529 (throw 'done (cons (car car) (cdr paren-state)))
3530 ;; we know that both the open and close braces are
3531 ;; before bufpos, so we also know that everything else
3532 ;; on state is before bufpos.
3533 (throw 'done paren-state)))
3534 (if (<= bufpos car)
3535 nil ; whack it off
3536 ;; it's before bufpos, so everything else should too.
3537 (throw 'done paren-state)))
3538 (setq paren-state (cdr paren-state)))
3539 nil)))
3540
3541 (defun c-most-enclosing-brace (paren-state &optional bufpos)
3542 ;; Return the bufpos of the innermost enclosing open paren before
3543 ;; bufpos, or nil if none was found.
3544 (let (enclosingp)
3545 (or bufpos (setq bufpos 134217727))
3546 (while paren-state
3547 (setq enclosingp (car paren-state)
3548 paren-state (cdr paren-state))
3549 (if (or (consp enclosingp)
3550 (>= enclosingp bufpos))
3551 (setq enclosingp nil)
3552 (setq paren-state nil)))
3553 enclosingp))
3554
3555 (defun c-least-enclosing-brace (paren-state)
3556 ;; Return the bufpos of the outermost enclosing open paren, or nil
3557 ;; if none was found.
3558 (let (pos elem)
3559 (while paren-state
3560 (setq elem (car paren-state)
3561 paren-state (cdr paren-state))
3562 (if (integerp elem)
3563 (setq pos elem)))
3564 pos))
3565
3566 (defun c-safe-position (bufpos paren-state)
3567 ;; Return the closest "safe" position recorded on PAREN-STATE that
3568 ;; is higher up than BUFPOS. Return nil if PAREN-STATE doesn't
3569 ;; contain any. Return nil if BUFPOS is nil, which is useful to
3570 ;; find the closest limit before a given limit that might be nil.
3571 ;;
3572 ;; A "safe" position is a position at or after a recorded open
3573 ;; paren, or after a recorded close paren. The returned position is
3574 ;; thus either the first position after a close brace, or the first
3575 ;; position after an enclosing paren, or at the enclosing paren in
3576 ;; case BUFPOS is immediately after it.
3577 (when bufpos
3578 (let (elem)
3579 (catch 'done
3580 (while paren-state
3581 (setq elem (car paren-state))
3582 (if (consp elem)
3583 (cond ((< (cdr elem) bufpos)
3584 (throw 'done (cdr elem)))
3585 ((< (car elem) bufpos)
3586 ;; See below.
3587 (throw 'done (min (1+ (car elem)) bufpos))))
3588 (if (< elem bufpos)
3589 ;; elem is the position at and not after the opening paren, so
3590 ;; we can go forward one more step unless it's equal to
3591 ;; bufpos. This is useful in some cases avoid an extra paren
3592 ;; level between the safe position and bufpos.
3593 (throw 'done (min (1+ elem) bufpos))))
3594 (setq paren-state (cdr paren-state)))))))
3595
3596 (defun c-beginning-of-syntax ()
3597 ;; This is used for `font-lock-beginning-of-syntax-function'. It
3598 ;; goes to the closest previous point that is known to be outside
3599 ;; any string literal or comment. `c-state-cache' is used if it has
3600 ;; a position in the vicinity.
3601 (let* ((paren-state c-state-cache)
3602 elem
3603
3604 (pos (catch 'done
3605 ;; Note: Similar code in `c-safe-position'. The
3606 ;; difference is that we accept a safe position at
3607 ;; the point and don't bother to go forward past open
3608 ;; parens.
3609 (while paren-state
3610 (setq elem (car paren-state))
3611 (if (consp elem)
3612 (cond ((<= (cdr elem) (point))
3613 (throw 'done (cdr elem)))
3614 ((<= (car elem) (point))
3615 (throw 'done (car elem))))
3616 (if (<= elem (point))
3617 (throw 'done elem)))
3618 (setq paren-state (cdr paren-state)))
3619 (point-min))))
3620
3621 (if (> pos (- (point) 4000))
3622 (goto-char pos)
3623 ;; The position is far back. Try `c-beginning-of-defun-1'
3624 ;; (although we can't be entirely sure it will go to a position
3625 ;; outside a comment or string in current emacsen). FIXME:
3626 ;; Consult `syntax-ppss' here.
3627 (c-beginning-of-defun-1)
3628 (if (< (point) pos)
3629 (goto-char pos)))))
3630
3631 \f
3632 ;; Tools for scanning identifiers and other tokens.
3633
3634 (defun c-on-identifier ()
3635 "Return non-nil if the point is on or directly after an identifier.
3636 Keywords are recognized and not considered identifiers. If an
3637 identifier is detected, the returned value is its starting position.
3638 If an identifier ends at the point and another begins at it \(can only
3639 happen in Pike) then the point for the preceding one is returned.
3640
3641 Note that this function might do hidden buffer changes. See the
3642 comment at the start of cc-engine.el for more info."
3643
3644 ;; FIXME: Shouldn't this function handle "operator" in C++?
3645
3646 (save-excursion
3647 (skip-syntax-backward "w_")
3648
3649 (or
3650
3651 ;; Check for a normal (non-keyword) identifier.
3652 (and (looking-at c-symbol-start)
3653 (not (looking-at c-keywords-regexp))
3654 (point))
3655
3656 (when (c-major-mode-is 'pike-mode)
3657 ;; Handle the `<operator> syntax in Pike.
3658 (let ((pos (point)))
3659 (skip-chars-backward "-!%&*+/<=>^|~[]()")
3660 (and (if (< (skip-chars-backward "`") 0)
3661 t
3662 (goto-char pos)
3663 (eq (char-after) ?\`))
3664 (looking-at c-symbol-key)
3665 (>= (match-end 0) pos)
3666 (point))))
3667
3668 ;; Handle the "operator +" syntax in C++.
3669 (when (and c-overloadable-operators-regexp
3670 (= (c-backward-token-2 0) 0))
3671
3672 (cond ((and (looking-at c-overloadable-operators-regexp)
3673 (or (not c-opt-op-identifier-prefix)
3674 (and (= (c-backward-token-2 1) 0)
3675 (looking-at c-opt-op-identifier-prefix))))
3676 (point))
3677
3678 ((save-excursion
3679 (and c-opt-op-identifier-prefix
3680 (looking-at c-opt-op-identifier-prefix)
3681 (= (c-forward-token-2 1) 0)
3682 (looking-at c-overloadable-operators-regexp)))
3683 (point))))
3684
3685 )))
3686
3687 (defsubst c-simple-skip-symbol-backward ()
3688 ;; If the point is at the end of a symbol then skip backward to the
3689 ;; beginning of it. Don't move otherwise. Return non-nil if point
3690 ;; moved.
3691 ;;
3692 ;; This function might do hidden buffer changes.
3693 (or (< (skip-syntax-backward "w_") 0)
3694 (and (c-major-mode-is 'pike-mode)
3695 ;; Handle the `<operator> syntax in Pike.
3696 (let ((pos (point)))
3697 (if (and (< (skip-chars-backward "-!%&*+/<=>^|~[]()") 0)
3698 (< (skip-chars-backward "`") 0)
3699 (looking-at c-symbol-key)
3700 (>= (match-end 0) pos))
3701 t
3702 (goto-char pos)
3703 nil)))))
3704
3705 (defun c-beginning-of-current-token (&optional back-limit)
3706 ;; Move to the beginning of the current token. Do not move if not
3707 ;; in the middle of one. BACK-LIMIT may be used to bound the
3708 ;; backward search; if given it's assumed to be at the boundary
3709 ;; between two tokens. Return non-nil if the point is moved, nil
3710 ;; otherwise.
3711 ;;
3712 ;; This function might do hidden buffer changes.
3713 (let ((start (point)))
3714 (if (looking-at "\\w\\|\\s_")
3715 (skip-syntax-backward "w_" back-limit)
3716 (when (< (skip-syntax-backward ".()" back-limit) 0)
3717 (while (let ((pos (or (and (looking-at c-nonsymbol-token-regexp)
3718 (match-end 0))
3719 ;; `c-nonsymbol-token-regexp' should always match
3720 ;; since we've skipped backward over punctuator
3721 ;; or paren syntax, but consume one char in case
3722 ;; it doesn't so that we don't leave point before
3723 ;; some earlier incorrect token.
3724 (1+ (point)))))
3725 (if (<= pos start)
3726 (goto-char pos))))))
3727 (< (point) start)))
3728
3729 (defun c-end-of-current-token (&optional back-limit)
3730 ;; Move to the end of the current token. Do not move if not in the
3731 ;; middle of one. BACK-LIMIT may be used to bound the backward
3732 ;; search; if given it's assumed to be at the boundary between two
3733 ;; tokens. Return non-nil if the point is moved, nil otherwise.
3734 ;;
3735 ;; This function might do hidden buffer changes.
3736 (let ((start (point)))
3737 (cond ((< (skip-syntax-backward "w_" (1- start)) 0)
3738 (skip-syntax-forward "w_"))
3739 ((< (skip-syntax-backward ".()" back-limit) 0)
3740 (while (progn
3741 (if (looking-at c-nonsymbol-token-regexp)
3742 (goto-char (match-end 0))
3743 ;; `c-nonsymbol-token-regexp' should always match since
3744 ;; we've skipped backward over punctuator or paren
3745 ;; syntax, but move forward in case it doesn't so that
3746 ;; we don't leave point earlier than we started with.
3747 (forward-char))
3748 (< (point) start)))))
3749 (> (point) start)))
3750
3751 (defconst c-jump-syntax-balanced
3752 (if (memq 'gen-string-delim c-emacs-features)
3753 "\\w\\|\\s_\\|\\s\(\\|\\s\)\\|\\s\"\\|\\s|"
3754 "\\w\\|\\s_\\|\\s\(\\|\\s\)\\|\\s\""))
3755
3756 (defconst c-jump-syntax-unbalanced
3757 (if (memq 'gen-string-delim c-emacs-features)
3758 "\\w\\|\\s_\\|\\s\"\\|\\s|"
3759 "\\w\\|\\s_\\|\\s\""))
3760
3761 (defun c-forward-token-2 (&optional count balanced limit)
3762 "Move forward by tokens.
3763 A token is defined as all symbols and identifiers which aren't
3764 syntactic whitespace \(note that multicharacter tokens like \"==\" are
3765 treated properly). Point is always either left at the beginning of a
3766 token or not moved at all. COUNT specifies the number of tokens to
3767 move; a negative COUNT moves in the opposite direction. A COUNT of 0
3768 moves to the next token beginning only if not already at one. If
3769 BALANCED is true, move over balanced parens, otherwise move into them.
3770 Also, if BALANCED is true, never move out of an enclosing paren.
3771
3772 LIMIT sets the limit for the movement and defaults to the point limit.
3773 The case when LIMIT is set in the middle of a token, comment or macro
3774 is handled correctly, i.e. the point won't be left there.
3775
3776 Return the number of tokens left to move \(positive or negative). If
3777 BALANCED is true, a move over a balanced paren counts as one. Note
3778 that if COUNT is 0 and no appropriate token beginning is found, 1 will
3779 be returned. Thus, a return value of 0 guarantees that point is at
3780 the requested position and a return value less \(without signs) than
3781 COUNT guarantees that point is at the beginning of some token.
3782
3783 Note that this function might do hidden buffer changes. See the
3784 comment at the start of cc-engine.el for more info."
3785
3786 (or count (setq count 1))
3787 (if (< count 0)
3788 (- (c-backward-token-2 (- count) balanced limit))
3789
3790 (let ((jump-syntax (if balanced
3791 c-jump-syntax-balanced
3792 c-jump-syntax-unbalanced))
3793 (last (point))
3794 (prev (point)))
3795
3796 (if (zerop count)
3797 ;; If count is zero we should jump if in the middle of a token.
3798 (c-end-of-current-token))
3799
3800 (save-restriction
3801 (if limit (narrow-to-region (point-min) limit))
3802 (if (/= (point)
3803 (progn (c-forward-syntactic-ws) (point)))
3804 ;; Skip whitespace. Count this as a move if we did in
3805 ;; fact move.
3806 (setq count (max (1- count) 0)))
3807
3808 (if (eobp)
3809 ;; Moved out of bounds. Make sure the returned count isn't zero.
3810 (progn
3811 (if (zerop count) (setq count 1))
3812 (goto-char last))
3813
3814 ;; Use `condition-case' to avoid having the limit tests
3815 ;; inside the loop.
3816 (condition-case nil
3817 (while (and
3818 (> count 0)
3819 (progn
3820 (setq last (point))
3821 (cond ((looking-at jump-syntax)
3822 (goto-char (scan-sexps (point) 1))
3823 t)
3824 ((looking-at c-nonsymbol-token-regexp)
3825 (goto-char (match-end 0))
3826 t)
3827 ;; `c-nonsymbol-token-regexp' above should always
3828 ;; match if there are correct tokens. Try to
3829 ;; widen to see if the limit was set in the
3830 ;; middle of one, else fall back to treating
3831 ;; the offending thing as a one character token.
3832 ((and limit
3833 (save-restriction
3834 (widen)
3835 (looking-at c-nonsymbol-token-regexp)))
3836 nil)
3837 (t
3838 (forward-char)
3839 t))))
3840 (c-forward-syntactic-ws)
3841 (setq prev last
3842 count (1- count)))
3843 (error (goto-char last)))
3844
3845 (when (eobp)
3846 (goto-char prev)
3847 (setq count (1+ count)))))
3848
3849 count)))
3850
3851 (defun c-backward-token-2 (&optional count balanced limit)
3852 "Move backward by tokens.
3853 See `c-forward-token-2' for details."
3854
3855 (or count (setq count 1))
3856 (if (< count 0)
3857 (- (c-forward-token-2 (- count) balanced limit))
3858
3859 (or limit (setq limit (point-min)))
3860 (let ((jump-syntax (if balanced
3861 c-jump-syntax-balanced
3862 c-jump-syntax-unbalanced))
3863 (last (point)))
3864
3865 (if (zerop count)
3866 ;; The count is zero so try to skip to the beginning of the
3867 ;; current token.
3868 (if (> (point)
3869 (progn (c-beginning-of-current-token) (point)))
3870 (if (< (point) limit)
3871 ;; The limit is inside the same token, so return 1.
3872 (setq count 1))
3873
3874 ;; We're not in the middle of a token. If there's
3875 ;; whitespace after the point then we must move backward,
3876 ;; so set count to 1 in that case.
3877 (and (looking-at c-syntactic-ws-start)
3878 ;; If we're looking at a '#' that might start a cpp
3879 ;; directive then we have to do a more elaborate check.
3880 (or (/= (char-after) ?#)
3881 (not c-opt-cpp-prefix)
3882 (save-excursion
3883 (and (= (point)
3884 (progn (beginning-of-line)
3885 (looking-at "[ \t]*")
3886 (match-end 0)))
3887 (or (bobp)
3888 (progn (backward-char)
3889 (not (eq (char-before) ?\\)))))))
3890 (setq count 1))))
3891
3892 ;; Use `condition-case' to avoid having to check for buffer
3893 ;; limits in `backward-char', `scan-sexps' and `goto-char' below.
3894 (condition-case nil
3895 (while (and
3896 (> count 0)
3897 (progn
3898 (c-backward-syntactic-ws)
3899 (backward-char)
3900 (if (looking-at jump-syntax)
3901 (goto-char (scan-sexps (1+ (point)) -1))
3902 ;; This can be very inefficient if there's a long
3903 ;; sequence of operator tokens without any separation.
3904 ;; That doesn't happen in practice, anyway.
3905 (c-beginning-of-current-token))
3906 (>= (point) limit)))
3907 (setq last (point)
3908 count (1- count)))
3909 (error (goto-char last)))
3910
3911 (if (< (point) limit)
3912 (goto-char last))
3913
3914 count)))
3915
3916 (defun c-forward-token-1 (&optional count balanced limit)
3917 "Like `c-forward-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-forward-token-2'."
3921 (let ((c-nonsymbol-token-regexp "\\s.\\|\\s\(\\|\\s\)"))
3922 (c-forward-token-2 count balanced limit)))
3923
3924 (defun c-backward-token-1 (&optional count balanced limit)
3925 "Like `c-backward-token-2' but doesn't treat multicharacter operator
3926 tokens like \"==\" as single tokens, i.e. all sequences of symbol
3927 characters are jumped over character by character. This function is
3928 for compatibility only; it's only a wrapper over `c-backward-token-2'."
3929 (let ((c-nonsymbol-token-regexp "\\s.\\|\\s\(\\|\\s\)"))
3930 (c-backward-token-2 count balanced limit)))
3931
3932 \f
3933 ;; Tools for doing searches restricted to syntactically relevant text.
3934
3935 (defun c-syntactic-re-search-forward (regexp &optional bound noerror
3936 paren-level not-inside-token
3937 lookbehind-submatch)
3938 "Like `re-search-forward', but only report matches that are found
3939 in syntactically significant text. I.e. matches in comments, macros
3940 or string literals are ignored. The start point is assumed to be
3941 outside any comment, macro or string literal, or else the content of
3942 that region is taken as syntactically significant text.
3943
3944 If PAREN-LEVEL is non-nil, an additional restriction is added to
3945 ignore matches in nested paren sexps. The search will also not go
3946 outside the current list sexp, which has the effect that if the point
3947 should be moved to BOUND when no match is found \(i.e. NOERROR is
3948 neither nil nor t), then it will be at the closing paren if the end of
3949 the current list sexp is encountered first.
3950
3951 If NOT-INSIDE-TOKEN is non-nil, matches in the middle of tokens are
3952 ignored. Things like multicharacter operators and special symbols
3953 \(e.g. \"`()\" in Pike) are handled but currently not floating point
3954 constants.
3955
3956 If LOOKBEHIND-SUBMATCH is non-nil, it's taken as a number of a
3957 subexpression in REGEXP. The end of that submatch is used as the
3958 position to check for syntactic significance. If LOOKBEHIND-SUBMATCH
3959 isn't used or if that subexpression didn't match then the start
3960 position of the whole match is used instead. The \"look behind\"
3961 subexpression is never tested before the starting position, so it
3962 might be a good idea to include \\=\\= as a match alternative in it.
3963
3964 Optimization note: Matches might be missed if the \"look behind\"
3965 subexpression can match the end of nonwhite syntactic whitespace,
3966 i.e. the end of comments or cpp directives. This since the function
3967 skips over such things before resuming the search. It's on the other
3968 hand not safe to assume that the \"look behind\" subexpression never
3969 matches syntactic whitespace.
3970
3971 Bug: Unbalanced parens inside cpp directives are currently not handled
3972 correctly \(i.e. they don't get ignored as they should) when
3973 PAREN-LEVEL is set.
3974
3975 Note that this function might do hidden buffer changes. See the
3976 comment at the start of cc-engine.el for more info."
3977
3978 (or bound (setq bound (point-max)))
3979 (if paren-level (setq paren-level -1))
3980
3981 ;;(message "c-syntactic-re-search-forward %s %s %S" (point) bound regexp)
3982
3983 (let ((start (point))
3984 tmp
3985 ;; Start position for the last search.
3986 search-pos
3987 ;; The `parse-partial-sexp' state between the start position
3988 ;; and the point.
3989 state
3990 ;; The current position after the last state update. The next
3991 ;; `parse-partial-sexp' continues from here.
3992 (state-pos (point))
3993 ;; The position at which to check the state and the state
3994 ;; there. This is separate from `state-pos' since we might
3995 ;; need to back up before doing the next search round.
3996 check-pos check-state
3997 ;; Last position known to end a token.
3998 (last-token-end-pos (point-min))
3999 ;; Set when a valid match is found.
4000 found)
4001
4002 (condition-case err
4003 (while
4004 (and
4005 (progn
4006 (setq search-pos (point))
4007 (re-search-forward regexp bound noerror))
4008
4009 (progn
4010 (setq state (parse-partial-sexp
4011 state-pos (match-beginning 0) paren-level nil state)
4012 state-pos (point))
4013 (if (setq check-pos (and lookbehind-submatch
4014 (or (not paren-level)
4015 (>= (car state) 0))
4016 (match-end lookbehind-submatch)))
4017 (setq check-state (parse-partial-sexp
4018 state-pos check-pos paren-level nil state))
4019 (setq check-pos state-pos
4020 check-state state))
4021
4022 ;; NOTE: If we got a look behind subexpression and get
4023 ;; an insignificant match in something that isn't
4024 ;; syntactic whitespace (i.e. strings or in nested
4025 ;; parentheses), then we can never skip more than a
4026 ;; single character from the match start position
4027 ;; (i.e. `state-pos' here) before continuing the
4028 ;; search. That since the look behind subexpression
4029 ;; might match the end of the insignificant region in
4030 ;; the next search.
4031
4032 (cond
4033 ((elt check-state 7)
4034 ;; Match inside a line comment. Skip to eol. Use
4035 ;; `re-search-forward' instead of `skip-chars-forward' to get
4036 ;; the right bound behavior.
4037 (re-search-forward "[\n\r]" bound noerror))
4038
4039 ((elt check-state 4)
4040 ;; Match inside a block comment. Skip to the '*/'.
4041 (search-forward "*/" bound noerror))
4042
4043 ((and (not (elt check-state 5))
4044 (eq (char-before check-pos) ?/)
4045 (not (c-get-char-property (1- check-pos) 'syntax-table))
4046 (memq (char-after check-pos) '(?/ ?*)))
4047 ;; Match in the middle of the opener of a block or line
4048 ;; comment.
4049 (if (= (char-after check-pos) ?/)
4050 (re-search-forward "[\n\r]" bound noerror)
4051 (search-forward "*/" bound noerror)))
4052
4053 ;; The last `parse-partial-sexp' above might have
4054 ;; stopped short of the real check position if the end
4055 ;; of the current sexp was encountered in paren-level
4056 ;; mode. The checks above are always false in that
4057 ;; case, and since they can do better skipping in
4058 ;; lookbehind-submatch mode, we do them before
4059 ;; checking the paren level.
4060
4061 ((and paren-level
4062 (/= (setq tmp (car check-state)) 0))
4063 ;; Check the paren level first since we're short of the
4064 ;; syntactic checking position if the end of the
4065 ;; current sexp was encountered by `parse-partial-sexp'.
4066 (if (> tmp 0)
4067
4068 ;; Inside a nested paren sexp.
4069 (if lookbehind-submatch
4070 ;; See the NOTE above.
4071 (progn (goto-char state-pos) t)
4072 ;; Skip out of the paren quickly.
4073 (setq state (parse-partial-sexp state-pos bound 0 nil state)
4074 state-pos (point)))
4075
4076 ;; Have exited the current paren sexp.
4077 (if noerror
4078 (progn
4079 ;; The last `parse-partial-sexp' call above
4080 ;; has left us just after the closing paren
4081 ;; in this case, so we can modify the bound
4082 ;; to leave the point at the right position
4083 ;; upon return.
4084 (setq bound (1- (point)))
4085 nil)
4086 (signal 'search-failed (list regexp)))))
4087
4088 ((setq tmp (elt check-state 3))
4089 ;; Match inside a string.
4090 (if (or lookbehind-submatch
4091 (not (integerp tmp)))
4092 ;; See the NOTE above.
4093 (progn (goto-char state-pos) t)
4094 ;; Skip to the end of the string before continuing.
4095 (let ((ender (make-string 1 tmp)) (continue t))
4096 (while (if (search-forward ender bound noerror)
4097 (progn
4098 (setq state (parse-partial-sexp
4099 state-pos (point) nil nil state)
4100 state-pos (point))
4101 (elt state 3))
4102 (setq continue nil)))
4103 continue)))
4104
4105 ((save-excursion
4106 (save-match-data
4107 (c-beginning-of-macro start)))
4108 ;; Match inside a macro. Skip to the end of it.
4109 (c-end-of-macro)
4110 (cond ((<= (point) bound) t)
4111 (noerror nil)
4112 (t (signal 'search-failed (list regexp)))))
4113
4114 ((and not-inside-token
4115 (or (< check-pos last-token-end-pos)
4116 (< check-pos
4117 (save-excursion
4118 (goto-char check-pos)
4119 (save-match-data
4120 (c-end-of-current-token last-token-end-pos))
4121 (setq last-token-end-pos (point))))))
4122 ;; Inside a token.
4123 (if lookbehind-submatch
4124 ;; See the NOTE above.
4125 (goto-char state-pos)
4126 (goto-char (min last-token-end-pos bound))))
4127
4128 (t
4129 ;; A real match.
4130 (setq found t)
4131 nil)))
4132
4133 ;; Should loop to search again, but take care to avoid
4134 ;; looping on the same spot.
4135 (or (/= search-pos (point))
4136 (if (= (point) bound)
4137 (if noerror
4138 nil
4139 (signal 'search-failed (list regexp)))
4140 (forward-char)
4141 t))))
4142
4143 (error
4144 (goto-char start)
4145 (signal (car err) (cdr err))))
4146
4147 ;;(message "c-syntactic-re-search-forward done %s" (or (match-end 0) (point)))
4148
4149 (if found
4150 (progn
4151 (goto-char (match-end 0))
4152 (match-end 0))
4153
4154 ;; Search failed. Set point as appropriate.
4155 (if (eq noerror t)
4156 (goto-char start)
4157 (goto-char bound))
4158 nil)))
4159
4160 (defvar safe-pos-list) ; bound in c-syntactic-skip-backward
4161
4162 (defsubst c-ssb-lit-begin ()
4163 ;; Return the start of the literal point is in, or nil.
4164 ;; We read and write the variables `safe-pos', `safe-pos-list', `state'
4165 ;; bound in the caller.
4166
4167 ;; Use `parse-partial-sexp' from a safe position down to the point to check
4168 ;; if it's outside comments and strings.
4169 (save-excursion
4170 (let ((pos (point)) safe-pos state pps-end-pos)
4171 ;; Pick a safe position as close to the point as possible.
4172 ;;
4173 ;; FIXME: Consult `syntax-ppss' here if our cache doesn't give a good
4174 ;; position.
4175
4176 (while (and safe-pos-list
4177 (> (car safe-pos-list) (point)))
4178 (setq safe-pos-list (cdr safe-pos-list)))
4179 (unless (setq safe-pos (car-safe safe-pos-list))
4180 (setq safe-pos (max (or (c-safe-position
4181 (point) (or c-state-cache
4182 (c-parse-state)))
4183 0)
4184 (point-min))
4185 safe-pos-list (list safe-pos)))
4186
4187 ;; Cache positions along the way to use if we have to back up more. We
4188 ;; cache every closing paren on the same level. If the paren cache is
4189 ;; relevant in this region then we're typically already on the same
4190 ;; level as the target position. Note that we might cache positions
4191 ;; after opening parens in case safe-pos is in a nested list. That's
4192 ;; both uncommon and harmless.
4193 (while (progn
4194 (setq state (parse-partial-sexp
4195 safe-pos pos 0))
4196 (< (point) pos))
4197 (setq safe-pos (point)
4198 safe-pos-list (cons safe-pos safe-pos-list)))
4199
4200 ;; If the state contains the start of the containing sexp we cache that
4201 ;; position too, so that parse-partial-sexp in the next run has a bigger
4202 ;; chance of starting at the same level as the target position and thus
4203 ;; will get more good safe positions into the list.
4204 (if (elt state 1)
4205 (setq safe-pos (1+ (elt state 1))
4206 safe-pos-list (cons safe-pos safe-pos-list)))
4207
4208 (if (or (elt state 3) (elt state 4))
4209 ;; Inside string or comment. Continue search at the
4210 ;; beginning of it.
4211 (elt state 8)))))
4212
4213 (defun c-syntactic-skip-backward (skip-chars &optional limit paren-level)
4214 "Like `skip-chars-backward' but only look at syntactically relevant chars,
4215 i.e. don't stop at positions inside syntactic whitespace or string
4216 literals. Preprocessor directives are also ignored, with the exception
4217 of the one that the point starts within, if any. If LIMIT is given,
4218 it's assumed to be at a syntactically relevant position.
4219
4220 If PAREN-LEVEL is non-nil, the function won't stop in nested paren
4221 sexps, and the search will also not go outside the current paren sexp.
4222 However, if LIMIT or the buffer limit is reached inside a nested paren
4223 then the point will be left at the limit.
4224
4225 Non-nil is returned if the point moved, nil otherwise.
4226
4227 Note that this function might do hidden buffer changes. See the
4228 comment at the start of cc-engine.el for more info."
4229
4230 (let ((start (point))
4231 state-2
4232 ;; A list of syntactically relevant positions in descending
4233 ;; order. It's used to avoid scanning repeatedly over
4234 ;; potentially large regions with `parse-partial-sexp' to verify
4235 ;; each position. Used in `c-ssb-lit-begin'
4236 safe-pos-list
4237 ;; The result from `c-beginning-of-macro' at the start position or the
4238 ;; start position itself if it isn't within a macro. Evaluated on
4239 ;; demand.
4240 start-macro-beg
4241 ;; The earliest position after the current one with the same paren
4242 ;; level. Used only when `paren-level' is set.
4243 lit-beg
4244 (paren-level-pos (point)))
4245
4246 (while
4247 (progn
4248 ;; The next loop "tries" to find the end point each time round,
4249 ;; loops when it hasn't succeeded.
4250 (while
4251 (and
4252 (< (skip-chars-backward skip-chars limit) 0)
4253
4254 (let ((pos (point)) state-2 pps-end-pos)
4255
4256 (cond
4257 ;; Don't stop inside a literal
4258 ((setq lit-beg (c-ssb-lit-begin))
4259 (goto-char lit-beg)
4260 t)
4261
4262 ((and paren-level
4263 (save-excursion
4264 (setq state-2 (parse-partial-sexp
4265 pos paren-level-pos -1)
4266 pps-end-pos (point))
4267 (/= (car state-2) 0)))
4268 ;; Not at the right level.
4269
4270 (if (and (< (car state-2) 0)
4271 ;; We stop above if we go out of a paren.
4272 ;; Now check whether it precedes or is
4273 ;; nested in the starting sexp.
4274 (save-excursion
4275 (setq state-2
4276 (parse-partial-sexp
4277 pps-end-pos paren-level-pos
4278 nil nil state-2))
4279 (< (car state-2) 0)))
4280
4281 ;; We've stopped short of the starting position
4282 ;; so the hit was inside a nested list. Go up
4283 ;; until we are at the right level.
4284 (condition-case nil
4285 (progn
4286 (goto-char (scan-lists pos -1
4287 (- (car state-2))))
4288 (setq paren-level-pos (point))
4289 (if (and limit (>= limit paren-level-pos))
4290 (progn
4291 (goto-char limit)
4292 nil)
4293 t))
4294 (error
4295 (goto-char (or limit (point-min)))
4296 nil))
4297
4298 ;; The hit was outside the list at the start
4299 ;; position. Go to the start of the list and exit.
4300 (goto-char (1+ (elt state-2 1)))
4301 nil))
4302
4303 ((c-beginning-of-macro limit)
4304 ;; Inside a macro.
4305 (if (< (point)
4306 (or start-macro-beg
4307 (setq start-macro-beg
4308 (save-excursion
4309 (goto-char start)
4310 (c-beginning-of-macro limit)
4311 (point)))))
4312 t
4313
4314 ;; It's inside the same macro we started in so it's
4315 ;; a relevant match.
4316 (goto-char pos)
4317 nil))))))
4318
4319 (> (point)
4320 (progn
4321 ;; Skip syntactic ws afterwards so that we don't stop at the
4322 ;; end of a comment if `skip-chars' is something like "^/".
4323 (c-backward-syntactic-ws)
4324 (point)))))
4325
4326 ;; We might want to extend this with more useful return values in
4327 ;; the future.
4328 (/= (point) start)))
4329
4330 ;; The following is an alternative implementation of
4331 ;; `c-syntactic-skip-backward' that uses backward movement to keep
4332 ;; track of the syntactic context. It turned out to be generally
4333 ;; slower than the one above which uses forward checks from earlier
4334 ;; safe positions.
4335 ;;
4336 ;;(defconst c-ssb-stop-re
4337 ;; ;; The regexp matching chars `c-syntactic-skip-backward' needs to
4338 ;; ;; stop at to avoid going into comments and literals.
4339 ;; (concat
4340 ;; ;; Match comment end syntax and string literal syntax. Also match
4341 ;; ;; '/' for block comment endings (not covered by comment end
4342 ;; ;; syntax).
4343 ;; "\\s>\\|/\\|\\s\""
4344 ;; (if (memq 'gen-string-delim c-emacs-features)
4345 ;; "\\|\\s|"
4346 ;; "")
4347 ;; (if (memq 'gen-comment-delim c-emacs-features)
4348 ;; "\\|\\s!"
4349 ;; "")))
4350 ;;
4351 ;;(defconst c-ssb-stop-paren-re
4352 ;; ;; Like `c-ssb-stop-re' but also stops at paren chars.
4353 ;; (concat c-ssb-stop-re "\\|\\s(\\|\\s)"))
4354 ;;
4355 ;;(defconst c-ssb-sexp-end-re
4356 ;; ;; Regexp matching the ending syntax of a complex sexp.
4357 ;; (concat c-string-limit-regexp "\\|\\s)"))
4358 ;;
4359 ;;(defun c-syntactic-skip-backward (skip-chars &optional limit paren-level)
4360 ;; "Like `skip-chars-backward' but only look at syntactically relevant chars,
4361 ;;i.e. don't stop at positions inside syntactic whitespace or string
4362 ;;literals. Preprocessor directives are also ignored. However, if the
4363 ;;point is within a comment, string literal or preprocessor directory to
4364 ;;begin with, its contents is treated as syntactically relevant chars.
4365 ;;If LIMIT is given, it limits the backward search and the point will be
4366 ;;left there if no earlier position is found.
4367 ;;
4368 ;;If PAREN-LEVEL is non-nil, the function won't stop in nested paren
4369 ;;sexps, and the search will also not go outside the current paren sexp.
4370 ;;However, if LIMIT or the buffer limit is reached inside a nested paren
4371 ;;then the point will be left at the limit.
4372 ;;
4373 ;;Non-nil is returned if the point moved, nil otherwise.
4374 ;;
4375 ;;Note that this function might do hidden buffer changes. See the
4376 ;;comment at the start of cc-engine.el for more info."
4377 ;;
4378 ;; (save-restriction
4379 ;; (when limit
4380 ;; (narrow-to-region limit (point-max)))
4381 ;;
4382 ;; (let ((start (point)))
4383 ;; (catch 'done
4384 ;; (while (let ((last-pos (point))
4385 ;; (stop-pos (progn
4386 ;; (skip-chars-backward skip-chars)
4387 ;; (point))))
4388 ;;
4389 ;; ;; Skip back over the same region as
4390 ;; ;; `skip-chars-backward' above, but keep to
4391 ;; ;; syntactically relevant positions.
4392 ;; (goto-char last-pos)
4393 ;; (while (and
4394 ;; ;; `re-search-backward' with a single char regexp
4395 ;; ;; should be fast.
4396 ;; (re-search-backward
4397 ;; (if paren-level c-ssb-stop-paren-re c-ssb-stop-re)
4398 ;; stop-pos 'move)
4399 ;;
4400 ;; (progn
4401 ;; (cond
4402 ;; ((looking-at "\\s(")
4403 ;; ;; `paren-level' is set and we've found the
4404 ;; ;; start of the containing paren.
4405 ;; (forward-char)
4406 ;; (throw 'done t))
4407 ;;
4408 ;; ((looking-at c-ssb-sexp-end-re)
4409 ;; ;; We're at the end of a string literal or paren
4410 ;; ;; sexp (if `paren-level' is set).
4411 ;; (forward-char)
4412 ;; (condition-case nil
4413 ;; (c-backward-sexp)
4414 ;; (error
4415 ;; (goto-char limit)
4416 ;; (throw 'done t))))
4417 ;;
4418 ;; (t
4419 ;; (forward-char)
4420 ;; ;; At the end of some syntactic ws or possibly
4421 ;; ;; after a plain '/' operator.
4422 ;; (let ((pos (point)))
4423 ;; (c-backward-syntactic-ws)
4424 ;; (if (= pos (point))
4425 ;; ;; Was a plain '/' operator. Go past it.
4426 ;; (backward-char)))))
4427 ;;
4428 ;; (> (point) stop-pos))))
4429 ;;
4430 ;; ;; Now the point is either at `stop-pos' or at some
4431 ;; ;; position further back if `stop-pos' was at a
4432 ;; ;; syntactically irrelevant place.
4433 ;;
4434 ;; ;; Skip additional syntactic ws so that we don't stop
4435 ;; ;; at the end of a comment if `skip-chars' is
4436 ;; ;; something like "^/".
4437 ;; (c-backward-syntactic-ws)
4438 ;;
4439 ;; (< (point) stop-pos))))
4440 ;;
4441 ;; ;; We might want to extend this with more useful return values
4442 ;; ;; in the future.
4443 ;; (/= (point) start))))
4444
4445 \f
4446 ;; Tools for handling comments and string literals.
4447
4448 (defun c-in-literal (&optional lim detect-cpp)
4449 "Return the type of literal point is in, if any.
4450 The return value is `c' if in a C-style comment, `c++' if in a C++
4451 style comment, `string' if in a string literal, `pound' if DETECT-CPP
4452 is non-nil and in a preprocessor line, or nil if somewhere else.
4453 Optional LIM is used as the backward limit of the search. If omitted,
4454 or nil, `c-beginning-of-defun' is used.
4455
4456 The last point calculated is cached if the cache is enabled, i.e. if
4457 `c-in-literal-cache' is bound to a two element vector.
4458
4459 Note that this function might do hidden buffer changes. See the
4460 comment at the start of cc-engine.el for more info."
4461 (save-restriction
4462 (widen)
4463 (let* ((safe-place (c-state-semi-safe-place (point)))
4464 (lit (c-state-pp-to-literal safe-place (point))))
4465 (or (cadr lit)
4466 (and detect-cpp
4467 (save-excursion (c-beginning-of-macro))
4468 'pound)))))
4469
4470 (defun c-literal-limits (&optional lim near not-in-delimiter)
4471 "Return a cons of the beginning and end positions of the comment or
4472 string surrounding point (including both delimiters), or nil if point
4473 isn't in one. If LIM is non-nil, it's used as the \"safe\" position
4474 to start parsing from. If NEAR is non-nil, then the limits of any
4475 literal next to point is returned. \"Next to\" means there's only
4476 spaces and tabs between point and the literal. The search for such a
4477 literal is done first in forward direction. If NOT-IN-DELIMITER is
4478 non-nil, the case when point is inside a starting delimiter won't be
4479 recognized. This only has effect for comments which have starting
4480 delimiters with more than one character.
4481
4482 Note that this function might do hidden buffer changes. See the
4483 comment at the start of cc-engine.el for more info."
4484
4485 (save-excursion
4486 (let* ((pos (point))
4487 (lim (or lim (c-state-semi-safe-place pos)))
4488 (pp-to-lit (save-restriction
4489 (widen)
4490 (c-state-pp-to-literal lim pos)))
4491 (state (car pp-to-lit))
4492 (lit-limits (car (cddr pp-to-lit))))
4493
4494 (cond
4495 (lit-limits)
4496 ((and (not not-in-delimiter)
4497 (not (elt state 5))
4498 (eq (char-before) ?/)
4499 (looking-at "[/*]")) ; FIXME!!! use c-line/block-comment-starter. 2008-09-28.
4500 ;; We're standing in a comment starter.
4501 (backward-char 1)
4502 (cons (point) (progn (c-forward-single-comment) (point))))
4503
4504 (near
4505 (goto-char pos)
4506 ;; Search forward for a literal.
4507 (skip-chars-forward " \t")
4508 (cond
4509 ((looking-at c-string-limit-regexp) ; String.
4510 (cons (point) (or (c-safe (c-forward-sexp 1) (point))
4511 (point-max))))
4512
4513 ((looking-at c-comment-start-regexp) ; Line or block comment.
4514 (cons (point) (progn (c-forward-single-comment) (point))))
4515
4516 (t
4517 ;; Search backward.
4518 (skip-chars-backward " \t")
4519
4520 (let ((end (point)) beg)
4521 (cond
4522 ((save-excursion
4523 (< (skip-syntax-backward c-string-syntax) 0)) ; String.
4524 (setq beg (c-safe (c-backward-sexp 1) (point))))
4525
4526 ((and (c-safe (forward-char -2) t)
4527 (looking-at "*/"))
4528 ;; Block comment. Due to the nature of line
4529 ;; comments, they will always be covered by the
4530 ;; normal case above.
4531 (goto-char end)
4532 (c-backward-single-comment)
4533 ;; If LIM is bogus, beg will be bogus.
4534 (setq beg (point))))
4535
4536 (if beg (cons beg end))))))
4537 ))))
4538
4539 ;; In case external callers use this; it did have a docstring.
4540 (defalias 'c-literal-limits-fast 'c-literal-limits)
4541
4542 (defun c-collect-line-comments (range)
4543 "If the argument is a cons of two buffer positions (such as returned by
4544 `c-literal-limits'), and that range contains a C++ style line comment,
4545 then an extended range is returned that contains all adjacent line
4546 comments (i.e. all comments that starts in the same column with no
4547 empty lines or non-whitespace characters between them). Otherwise the
4548 argument is returned.
4549
4550 Note that this function might do hidden buffer changes. See the
4551 comment at the start of cc-engine.el for more info."
4552
4553 (save-excursion
4554 (condition-case nil
4555 (if (and (consp range) (progn
4556 (goto-char (car range))
4557 (looking-at c-line-comment-starter)))
4558 (let ((col (current-column))
4559 (beg (point))
4560 (bopl (c-point 'bopl))
4561 (end (cdr range)))
4562 ;; Got to take care in the backward direction to handle
4563 ;; comments which are preceded by code.
4564 (while (and (c-backward-single-comment)
4565 (>= (point) bopl)
4566 (looking-at c-line-comment-starter)
4567 (= col (current-column)))
4568 (setq beg (point)
4569 bopl (c-point 'bopl)))
4570 (goto-char end)
4571 (while (and (progn (skip-chars-forward " \t")
4572 (looking-at c-line-comment-starter))
4573 (= col (current-column))
4574 (prog1 (zerop (forward-line 1))
4575 (setq end (point)))))
4576 (cons beg end))
4577 range)
4578 (error range))))
4579
4580 (defun c-literal-type (range)
4581 "Convenience function that given the result of `c-literal-limits',
4582 returns nil or the type of literal that the range surrounds, one
4583 of the symbols 'c, 'c++ or 'string. It's much faster than using
4584 `c-in-literal' and is intended to be used when you need both the
4585 type of a literal and its limits.
4586
4587 Note that this function might do hidden buffer changes. See the
4588 comment at the start of cc-engine.el for more info."
4589
4590 (if (consp range)
4591 (save-excursion
4592 (goto-char (car range))
4593 (cond ((looking-at c-string-limit-regexp) 'string)
4594 ((or (looking-at "//") ; c++ line comment
4595 (and (looking-at "\\s<") ; comment starter
4596 (looking-at "#"))) ; awk comment.
4597 'c++)
4598 (t 'c))) ; Assuming the range is valid.
4599 range))
4600
4601 (defsubst c-determine-limit-get-base (start try-size)
4602 ;; Get a "safe place" approximately TRY-SIZE characters before START.
4603 ;; This doesn't preserve point.
4604 (let* ((pos (max (- start try-size) (point-min)))
4605 (base (c-state-semi-safe-place pos))
4606 (s (parse-partial-sexp base pos)))
4607 (if (or (nth 4 s) (nth 3 s)) ; comment or string
4608 (nth 8 s)
4609 (point))))
4610
4611 (defun c-determine-limit (how-far-back &optional start try-size)
4612 ;; Return a buffer position HOW-FAR-BACK non-literal characters from START
4613 ;; (default point). This is done by going back further in the buffer then
4614 ;; searching forward for literals. The position found won't be in a
4615 ;; literal. We start searching for the sought position TRY-SIZE (default
4616 ;; twice HOW-FAR-BACK) bytes back from START. This function must be fast.
4617 ;; :-)
4618 (save-excursion
4619 (let* ((start (or start (point)))
4620 (try-size (or try-size (* 2 how-far-back)))
4621 (base (c-determine-limit-get-base start try-size))
4622 (pos base)
4623
4624 (s (parse-partial-sexp pos pos)) ; null state.
4625 stack elt size
4626 (count 0))
4627 (while (< pos start)
4628 ;; Move forward one literal each time round this loop.
4629 ;; Move forward to the start of a comment or string.
4630 (setq s (parse-partial-sexp
4631 pos
4632 start
4633 nil ; target-depth
4634 nil ; stop-before
4635 s ; state
4636 'syntax-table)) ; stop-comment
4637
4638 ;; Gather details of the non-literal-bit - starting pos and size.
4639 (setq size (- (if (or (nth 4 s) (nth 3 s))
4640 (nth 8 s)
4641 (point))
4642 pos))
4643 (if (> size 0)
4644 (setq stack (cons (cons pos size) stack)))
4645
4646 ;; Move forward to the end of the comment/string.
4647 (if (or (nth 4 s) (nth 3 s))
4648 (setq s (parse-partial-sexp
4649 (point)
4650 start
4651 nil ; target-depth
4652 nil ; stop-before
4653 s ; state
4654 'syntax-table))) ; stop-comment
4655 (setq pos (point)))
4656
4657 ;; Now try and find enough non-literal characters recorded on the stack.
4658 ;; Go back one recorded literal each time round this loop.
4659 (while (and (< count how-far-back)
4660 stack)
4661 (setq elt (car stack)
4662 stack (cdr stack))
4663 (setq count (+ count (cdr elt))))
4664
4665 ;; Have we found enough yet?
4666 (cond
4667 ((>= count how-far-back)
4668 (+ (car elt) (- count how-far-back)))
4669 ((eq base (point-min))
4670 (point-min))
4671 (t
4672 (c-determine-limit (- how-far-back count) base try-size))))))
4673
4674 (defun c-determine-+ve-limit (how-far &optional start-pos)
4675 ;; Return a buffer position about HOW-FAR non-literal characters forward
4676 ;; from START-POS (default point), which must not be inside a literal.
4677 (save-excursion
4678 (let ((pos (or start-pos (point)))
4679 (count how-far)
4680 (s (parse-partial-sexp (point) (point)))) ; null state
4681 (while (and (not (eobp))
4682 (> count 0))
4683 ;; Scan over counted characters.
4684 (setq s (parse-partial-sexp
4685 pos
4686 (min (+ pos count) (point-max))
4687 nil ; target-depth
4688 nil ; stop-before
4689 s ; state
4690 'syntax-table)) ; stop-comment
4691 (setq count (- count (- (point) pos) 1)
4692 pos (point))
4693 ;; Scan over literal characters.
4694 (if (nth 8 s)
4695 (setq s (parse-partial-sexp
4696 pos
4697 (point-max)
4698 nil ; target-depth
4699 nil ; stop-before
4700 s ; state
4701 'syntax-table) ; stop-comment
4702 pos (point))))
4703 (point))))
4704
4705 \f
4706 ;; `c-find-decl-spots' and accompanying stuff.
4707
4708 ;; Variables used in `c-find-decl-spots' to cache the search done for
4709 ;; the first declaration in the last call. When that function starts,
4710 ;; it needs to back up over syntactic whitespace to look at the last
4711 ;; token before the region being searched. That can sometimes cause
4712 ;; moves back and forth over a quite large region of comments and
4713 ;; macros, which would be repeated for each changed character when
4714 ;; we're called during fontification, since font-lock refontifies the
4715 ;; current line for each change. Thus it's worthwhile to cache the
4716 ;; first match.
4717 ;;
4718 ;; `c-find-decl-syntactic-pos' is a syntactically relevant position in
4719 ;; the syntactic whitespace less or equal to some start position.
4720 ;; There's no cached value if it's nil.
4721 ;;
4722 ;; `c-find-decl-match-pos' is the match position if
4723 ;; `c-find-decl-prefix-search' matched before the syntactic whitespace
4724 ;; at `c-find-decl-syntactic-pos', or nil if there's no such match.
4725 (defvar c-find-decl-syntactic-pos nil)
4726 (make-variable-buffer-local 'c-find-decl-syntactic-pos)
4727 (defvar c-find-decl-match-pos nil)
4728 (make-variable-buffer-local 'c-find-decl-match-pos)
4729
4730 (defsubst c-invalidate-find-decl-cache (change-min-pos)
4731 (and c-find-decl-syntactic-pos
4732 (< change-min-pos c-find-decl-syntactic-pos)
4733 (setq c-find-decl-syntactic-pos nil)))
4734
4735 ; (defface c-debug-decl-spot-face
4736 ; '((t (:background "Turquoise")))
4737 ; "Debug face to mark the spots where `c-find-decl-spots' stopped.")
4738 ; (defface c-debug-decl-sws-face
4739 ; '((t (:background "Khaki")))
4740 ; "Debug face to mark the syntactic whitespace between the declaration
4741 ; spots and the preceding token end.")
4742
4743 (defmacro c-debug-put-decl-spot-faces (match-pos decl-pos)
4744 (when (facep 'c-debug-decl-spot-face)
4745 `(c-save-buffer-state ((match-pos ,match-pos) (decl-pos ,decl-pos))
4746 (c-debug-add-face (max match-pos (point-min)) decl-pos
4747 'c-debug-decl-sws-face)
4748 (c-debug-add-face decl-pos (min (1+ decl-pos) (point-max))
4749 'c-debug-decl-spot-face))))
4750 (defmacro c-debug-remove-decl-spot-faces (beg end)
4751 (when (facep 'c-debug-decl-spot-face)
4752 `(c-save-buffer-state ()
4753 (c-debug-remove-face ,beg ,end 'c-debug-decl-spot-face)
4754 (c-debug-remove-face ,beg ,end 'c-debug-decl-sws-face))))
4755
4756 (defmacro c-find-decl-prefix-search ()
4757 ;; Macro used inside `c-find-decl-spots'. It ought to be a defun,
4758 ;; but it contains lots of free variables that refer to things
4759 ;; inside `c-find-decl-spots'. The point is left at `cfd-match-pos'
4760 ;; if there is a match, otherwise at `cfd-limit'.
4761 ;;
4762 ;; This macro might do hidden buffer changes.
4763
4764 '(progn
4765 ;; Find the next property match position if we haven't got one already.
4766 (unless cfd-prop-match
4767 (save-excursion
4768 (while (progn
4769 (goto-char (next-single-property-change
4770 (point) 'c-type nil cfd-limit))
4771 (and (< (point) cfd-limit)
4772 (not (eq (c-get-char-property (1- (point)) 'c-type)
4773 'c-decl-end)))))
4774 (setq cfd-prop-match (point))))
4775
4776 ;; Find the next `c-decl-prefix-or-start-re' match if we haven't
4777 ;; got one already.
4778 (unless cfd-re-match
4779
4780 (if (> cfd-re-match-end (point))
4781 (goto-char cfd-re-match-end))
4782
4783 (while (if (setq cfd-re-match-end
4784 (re-search-forward c-decl-prefix-or-start-re
4785 cfd-limit 'move))
4786
4787 ;; Match. Check if it's inside a comment or string literal.
4788 (c-got-face-at
4789 (if (setq cfd-re-match (match-end 1))
4790 ;; Matched the end of a token preceding a decl spot.
4791 (progn
4792 (goto-char cfd-re-match)
4793 (1- cfd-re-match))
4794 ;; Matched a token that start a decl spot.
4795 (goto-char (match-beginning 0))
4796 (point))
4797 c-literal-faces)
4798
4799 ;; No match. Finish up and exit the loop.
4800 (setq cfd-re-match cfd-limit)
4801 nil)
4802
4803 ;; Skip out of comments and string literals.
4804 (while (progn
4805 (goto-char (next-single-property-change
4806 (point) 'face nil cfd-limit))
4807 (and (< (point) cfd-limit)
4808 (c-got-face-at (point) c-literal-faces)))))
4809
4810 ;; If we matched at the decl start, we have to back up over the
4811 ;; preceding syntactic ws to set `cfd-match-pos' and to catch
4812 ;; any decl spots in the syntactic ws.
4813 (unless cfd-re-match
4814 (c-backward-syntactic-ws)
4815 (setq cfd-re-match (point))))
4816
4817 ;; Choose whichever match is closer to the start.
4818 (if (< cfd-re-match cfd-prop-match)
4819 (setq cfd-match-pos cfd-re-match
4820 cfd-re-match nil)
4821 (setq cfd-match-pos cfd-prop-match
4822 cfd-prop-match nil))
4823
4824 (goto-char cfd-match-pos)
4825
4826 (when (< cfd-match-pos cfd-limit)
4827 ;; Skip forward past comments only so we don't skip macros.
4828 (c-forward-comments)
4829 ;; Set the position to continue at. We can avoid going over
4830 ;; the comments skipped above a second time, but it's possible
4831 ;; that the comment skipping has taken us past `cfd-prop-match'
4832 ;; since the property might be used inside comments.
4833 (setq cfd-continue-pos (if cfd-prop-match
4834 (min cfd-prop-match (point))
4835 (point))))))
4836
4837 (defun c-find-decl-spots (cfd-limit cfd-decl-re cfd-face-checklist cfd-fun)
4838 ;; Call CFD-FUN for each possible spot for a declaration, cast or
4839 ;; label from the point to CFD-LIMIT.
4840 ;;
4841 ;; CFD-FUN is called with point at the start of the spot. It's passed two
4842 ;; arguments: The first is the end position of the token preceding the spot,
4843 ;; or 0 for the implicit match at bob. The second is a flag that is t when
4844 ;; the match is inside a macro. Point should be moved forward by at least
4845 ;; one token.
4846 ;;
4847 ;; If CFD-FUN adds `c-decl-end' properties somewhere below the current spot,
4848 ;; it should return non-nil to ensure that the next search will find them.
4849 ;;
4850 ;; Such a spot is:
4851 ;; o The first token after bob.
4852 ;; o The first token after the end of submatch 1 in
4853 ;; `c-decl-prefix-or-start-re' when that submatch matches.
4854 ;; o The start of each `c-decl-prefix-or-start-re' match when
4855 ;; submatch 1 doesn't match.
4856 ;; o The first token after the end of each occurrence of the
4857 ;; `c-type' text property with the value `c-decl-end', provided
4858 ;; `c-type-decl-end-used' is set.
4859 ;;
4860 ;; Only a spot that match CFD-DECL-RE and whose face is in the
4861 ;; CFD-FACE-CHECKLIST list causes CFD-FUN to be called. The face
4862 ;; check is disabled if CFD-FACE-CHECKLIST is nil.
4863 ;;
4864 ;; If the match is inside a macro then the buffer is narrowed to the
4865 ;; end of it, so that CFD-FUN can investigate the following tokens
4866 ;; without matching something that begins inside a macro and ends
4867 ;; outside it. It's to avoid this work that the CFD-DECL-RE and
4868 ;; CFD-FACE-CHECKLIST checks exist.
4869 ;;
4870 ;; The spots are visited approximately in order from top to bottom.
4871 ;; It's however the positions where `c-decl-prefix-or-start-re'
4872 ;; matches and where `c-decl-end' properties are found that are in
4873 ;; order. Since the spots often are at the following token, they
4874 ;; might be visited out of order insofar as more spots are reported
4875 ;; later on within the syntactic whitespace between the match
4876 ;; positions and their spots.
4877 ;;
4878 ;; It's assumed that comments and strings are fontified in the
4879 ;; searched range.
4880 ;;
4881 ;; This is mainly used in fontification, and so has an elaborate
4882 ;; cache to handle repeated calls from the same start position; see
4883 ;; the variables above.
4884 ;;
4885 ;; All variables in this function begin with `cfd-' to avoid name
4886 ;; collision with the (dynamically bound) variables used in CFD-FUN.
4887 ;;
4888 ;; This function might do hidden buffer changes.
4889
4890 (let ((cfd-start-pos (point))
4891 (cfd-buffer-end (point-max))
4892 ;; The end of the token preceding the decl spot last found
4893 ;; with `c-decl-prefix-or-start-re'. `cfd-limit' if there's
4894 ;; no match.
4895 cfd-re-match
4896 ;; The end position of the last `c-decl-prefix-or-start-re'
4897 ;; match. If this is greater than `cfd-continue-pos', the
4898 ;; next regexp search is started here instead.
4899 (cfd-re-match-end (point-min))
4900 ;; The end of the last `c-decl-end' found by
4901 ;; `c-find-decl-prefix-search'. `cfd-limit' if there's no
4902 ;; match. If searching for the property isn't needed then we
4903 ;; disable it by setting it to `cfd-limit' directly.
4904 (cfd-prop-match (unless c-type-decl-end-used cfd-limit))
4905 ;; The end of the token preceding the decl spot last found by
4906 ;; `c-find-decl-prefix-search'. 0 for the implicit match at
4907 ;; bob. `cfd-limit' if there's no match. In other words,
4908 ;; this is the minimum of `cfd-re-match' and `cfd-prop-match'.
4909 (cfd-match-pos cfd-limit)
4910 ;; The position to continue searching at.
4911 cfd-continue-pos
4912 ;; The position of the last "real" token we've stopped at.
4913 ;; This can be greater than `cfd-continue-pos' when we get
4914 ;; hits inside macros or at `c-decl-end' positions inside
4915 ;; comments.
4916 (cfd-token-pos 0)
4917 ;; The end position of the last entered macro.
4918 (cfd-macro-end 0))
4919
4920 ;; Initialize by finding a syntactically relevant start position
4921 ;; before the point, and do the first `c-decl-prefix-or-start-re'
4922 ;; search unless we're at bob.
4923
4924 (let (start-in-literal start-in-macro syntactic-pos)
4925 ;; Must back up a bit since we look for the end of the previous
4926 ;; statement or declaration, which is earlier than the first
4927 ;; returned match.
4928
4929 (cond
4930 ;; First we need to move to a syntactically relevant position.
4931 ;; Begin by backing out of comment or string literals.
4932 ((and
4933 (when (c-got-face-at (point) c-literal-faces)
4934 ;; Try to use the faces to back up to the start of the
4935 ;; literal. FIXME: What if the point is on a declaration
4936 ;; inside a comment?
4937 (while (and (not (bobp))
4938 (c-got-face-at (1- (point)) c-literal-faces))
4939 (goto-char (previous-single-property-change
4940 (point) 'face nil (point-min))))
4941
4942 ;; XEmacs doesn't fontify the quotes surrounding string
4943 ;; literals.
4944 (and (featurep 'xemacs)
4945 (eq (get-text-property (point) 'face)
4946 'font-lock-string-face)
4947 (not (bobp))
4948 (progn (backward-char)
4949 (not (looking-at c-string-limit-regexp)))
4950 (forward-char))
4951
4952 ;; Don't trust the literal to contain only literal faces
4953 ;; (the font lock package might not have fontified the
4954 ;; start of it at all, for instance) so check that we have
4955 ;; arrived at something that looks like a start or else
4956 ;; resort to `c-literal-limits'.
4957 (unless (looking-at c-literal-start-regexp)
4958 (let ((range (c-literal-limits)))
4959 (if range (goto-char (car range)))))
4960
4961 (setq start-in-literal (point)))
4962
4963 ;; The start is in a literal. If the limit is in the same
4964 ;; one we don't have to find a syntactic position etc. We
4965 ;; only check that if the limit is at or before bonl to save
4966 ;; time; it covers the by far most common case when font-lock
4967 ;; refontifies the current line only.
4968 (<= cfd-limit (c-point 'bonl cfd-start-pos))
4969 (save-excursion
4970 (goto-char cfd-start-pos)
4971 (while (progn
4972 (goto-char (next-single-property-change
4973 (point) 'face nil cfd-limit))
4974 (and (< (point) cfd-limit)
4975 (c-got-face-at (point) c-literal-faces))))
4976 (= (point) cfd-limit)))
4977
4978 ;; Completely inside a literal. Set up variables to trig the
4979 ;; (< cfd-continue-pos cfd-start-pos) case below and it'll
4980 ;; find a suitable start position.
4981 (setq cfd-continue-pos start-in-literal))
4982
4983 ;; Check if the region might be completely inside a macro, to
4984 ;; optimize that like the completely-inside-literal above.
4985 ((save-excursion
4986 (and (= (forward-line 1) 0)
4987 (bolp) ; forward-line has funny behavior at eob.
4988 (>= (point) cfd-limit)
4989 (progn (backward-char)
4990 (eq (char-before) ?\\))))
4991 ;; (Maybe) completely inside a macro. Only need to trig the
4992 ;; (< cfd-continue-pos cfd-start-pos) case below to make it
4993 ;; set things up.
4994 (setq cfd-continue-pos (1- cfd-start-pos)
4995 start-in-macro t))
4996
4997 (t
4998 ;; Back out of any macro so we don't miss any declaration
4999 ;; that could follow after it.
5000 (when (c-beginning-of-macro)
5001 (setq start-in-macro t))
5002
5003 ;; Now we're at a proper syntactically relevant position so we
5004 ;; can use the cache. But first clear it if it applied
5005 ;; further down.
5006 (c-invalidate-find-decl-cache cfd-start-pos)
5007
5008 (setq syntactic-pos (point))
5009 (unless (eq syntactic-pos c-find-decl-syntactic-pos)
5010 ;; Don't have to do this if the cache is relevant here,
5011 ;; typically if the same line is refontified again. If
5012 ;; we're just some syntactic whitespace further down we can
5013 ;; still use the cache to limit the skipping.
5014 (c-backward-syntactic-ws c-find-decl-syntactic-pos))
5015
5016 ;; If we hit `c-find-decl-syntactic-pos' and
5017 ;; `c-find-decl-match-pos' is set then we install the cached
5018 ;; values. If we hit `c-find-decl-syntactic-pos' and
5019 ;; `c-find-decl-match-pos' is nil then we know there's no decl
5020 ;; prefix in the whitespace before `c-find-decl-syntactic-pos'
5021 ;; and so we can continue the search from this point. If we
5022 ;; didn't hit `c-find-decl-syntactic-pos' then we're now in
5023 ;; the right spot to begin searching anyway.
5024 (if (and (eq (point) c-find-decl-syntactic-pos)
5025 c-find-decl-match-pos)
5026 (setq cfd-match-pos c-find-decl-match-pos
5027 cfd-continue-pos syntactic-pos)
5028
5029 (setq c-find-decl-syntactic-pos syntactic-pos)
5030
5031 (when (if (bobp)
5032 ;; Always consider bob a match to get the first
5033 ;; declaration in the file. Do this separately instead of
5034 ;; letting `c-decl-prefix-or-start-re' match bob, so that
5035 ;; regexp always can consume at least one character to
5036 ;; ensure that we won't get stuck in an infinite loop.
5037 (setq cfd-re-match 0)
5038 (backward-char)
5039 (c-beginning-of-current-token)
5040 (< (point) cfd-limit))
5041 ;; Do an initial search now. In the bob case above it's
5042 ;; only done to search for a `c-decl-end' spot.
5043 (c-find-decl-prefix-search))
5044
5045 (setq c-find-decl-match-pos (and (< cfd-match-pos cfd-start-pos)
5046 cfd-match-pos)))))
5047
5048 ;; Advance `cfd-continue-pos' if it's before the start position.
5049 ;; The closest continue position that might have effect at or
5050 ;; after the start depends on what we started in. This also
5051 ;; finds a suitable start position in the special cases when the
5052 ;; region is completely within a literal or macro.
5053 (when (and cfd-continue-pos (< cfd-continue-pos cfd-start-pos))
5054
5055 (cond
5056 (start-in-macro
5057 ;; If we're in a macro then it's the closest preceding token
5058 ;; in the macro. Check this before `start-in-literal',
5059 ;; since if we're inside a literal in a macro, the preceding
5060 ;; token is earlier than any `c-decl-end' spot inside the
5061 ;; literal (comment).
5062 (goto-char (or start-in-literal cfd-start-pos))
5063 ;; The only syntactic ws in macros are comments.
5064 (c-backward-comments)
5065 (backward-char)
5066 (c-beginning-of-current-token))
5067
5068 (start-in-literal
5069 ;; If we're in a comment it can only be the closest
5070 ;; preceding `c-decl-end' position within that comment, if
5071 ;; any. Go back to the beginning of such a property so that
5072 ;; `c-find-decl-prefix-search' will find the end of it.
5073 ;; (Can't stop at the end and install it directly on
5074 ;; `cfd-prop-match' since that variable might be cleared
5075 ;; after `cfd-fun' below.)
5076 ;;
5077 ;; Note that if the literal is a string then the property
5078 ;; search will simply skip to the beginning of it right
5079 ;; away.
5080 (if (not c-type-decl-end-used)
5081 (goto-char start-in-literal)
5082 (goto-char cfd-start-pos)
5083 (while (progn
5084 (goto-char (previous-single-property-change
5085 (point) 'c-type nil start-in-literal))
5086 (and (> (point) start-in-literal)
5087 (not (eq (c-get-char-property (point) 'c-type)
5088 'c-decl-end))))))
5089
5090 (when (= (point) start-in-literal)
5091 ;; Didn't find any property inside the comment, so we can
5092 ;; skip it entirely. (This won't skip past a string, but
5093 ;; that'll be handled quickly by the next
5094 ;; `c-find-decl-prefix-search' anyway.)
5095 (c-forward-single-comment)
5096 (if (> (point) cfd-limit)
5097 (goto-char cfd-limit))))
5098
5099 (t
5100 ;; If we started in normal code, the only match that might
5101 ;; apply before the start is what we already got in
5102 ;; `cfd-match-pos' so we can continue at the start position.
5103 ;; (Note that we don't get here if the first match is below
5104 ;; it.)
5105 (goto-char cfd-start-pos)))
5106
5107 ;; Delete found matches if they are before our new continue
5108 ;; position, so that `c-find-decl-prefix-search' won't back up
5109 ;; to them later on.
5110 (setq cfd-continue-pos (point))
5111 (when (and cfd-re-match (< cfd-re-match cfd-continue-pos))
5112 (setq cfd-re-match nil))
5113 (when (and cfd-prop-match (< cfd-prop-match cfd-continue-pos))
5114 (setq cfd-prop-match nil)))
5115
5116 (if syntactic-pos
5117 ;; This is the normal case and we got a proper syntactic
5118 ;; position. If there's a match then it's always outside
5119 ;; macros and comments, so advance to the next token and set
5120 ;; `cfd-token-pos'. The loop below will later go back using
5121 ;; `cfd-continue-pos' to fix declarations inside the
5122 ;; syntactic ws.
5123 (when (and cfd-match-pos (< cfd-match-pos syntactic-pos))
5124 (goto-char syntactic-pos)
5125 (c-forward-syntactic-ws)
5126 (and cfd-continue-pos
5127 (< cfd-continue-pos (point))
5128 (setq cfd-token-pos (point))))
5129
5130 ;; Have one of the special cases when the region is completely
5131 ;; within a literal or macro. `cfd-continue-pos' is set to a
5132 ;; good start position for the search, so do it.
5133 (c-find-decl-prefix-search)))
5134
5135 ;; Now loop. Round what? (ACM, 2006/7/5). We already got the first match.
5136
5137 (while (progn
5138 (while (and
5139 (< cfd-match-pos cfd-limit)
5140
5141 (or
5142 ;; Kludge to filter out matches on the "<" that
5143 ;; aren't open parens, for the sake of languages
5144 ;; that got `c-recognize-<>-arglists' set.
5145 (and (eq (char-before cfd-match-pos) ?<)
5146 (not (c-get-char-property (1- cfd-match-pos)
5147 'syntax-table)))
5148
5149 ;; If `cfd-continue-pos' is less or equal to
5150 ;; `cfd-token-pos', we've got a hit inside a macro
5151 ;; that's in the syntactic whitespace before the last
5152 ;; "real" declaration we've checked. If they're equal
5153 ;; we've arrived at the declaration a second time, so
5154 ;; there's nothing to do.
5155 (= cfd-continue-pos cfd-token-pos)
5156
5157 (progn
5158 ;; If `cfd-continue-pos' is less than `cfd-token-pos'
5159 ;; we're still searching for declarations embedded in
5160 ;; the syntactic whitespace. In that case we need
5161 ;; only to skip comments and not macros, since they
5162 ;; can't be nested, and that's already been done in
5163 ;; `c-find-decl-prefix-search'.
5164 (when (> cfd-continue-pos cfd-token-pos)
5165 (c-forward-syntactic-ws)
5166 (setq cfd-token-pos (point)))
5167
5168 ;; Continue if the following token fails the
5169 ;; CFD-DECL-RE and CFD-FACE-CHECKLIST checks.
5170 (when (or (>= (point) cfd-limit)
5171 (not (looking-at cfd-decl-re))
5172 (and cfd-face-checklist
5173 (not (c-got-face-at
5174 (point) cfd-face-checklist))))
5175 (goto-char cfd-continue-pos)
5176 t)))
5177
5178 (< (point) cfd-limit))
5179 (c-find-decl-prefix-search))
5180
5181 (< (point) cfd-limit))
5182
5183 (when (and
5184 (>= (point) cfd-start-pos)
5185
5186 (progn
5187 ;; Narrow to the end of the macro if we got a hit inside
5188 ;; one, to avoid recognizing things that start inside the
5189 ;; macro and end outside it.
5190 (when (> cfd-match-pos cfd-macro-end)
5191 ;; Not in the same macro as in the previous round.
5192 (save-excursion
5193 (goto-char cfd-match-pos)
5194 (setq cfd-macro-end
5195 (if (save-excursion (and (c-beginning-of-macro)
5196 (< (point) cfd-match-pos)))
5197 (progn (c-end-of-macro)
5198 (point))
5199 0))))
5200
5201 (if (zerop cfd-macro-end)
5202 t
5203 (if (> cfd-macro-end (point))
5204 (progn (narrow-to-region (point-min) cfd-macro-end)
5205 t)
5206 ;; The matched token was the last thing in the macro,
5207 ;; so the whole match is bogus.
5208 (setq cfd-macro-end 0)
5209 nil))))
5210
5211 (c-debug-put-decl-spot-faces cfd-match-pos (point))
5212 (if (funcall cfd-fun cfd-match-pos (/= cfd-macro-end 0))
5213 (setq cfd-prop-match nil))
5214
5215 (when (/= cfd-macro-end 0)
5216 ;; Restore limits if we did macro narrowing above.
5217 (narrow-to-region (point-min) cfd-buffer-end)))
5218
5219 (goto-char cfd-continue-pos)
5220 (if (= cfd-continue-pos cfd-limit)
5221 (setq cfd-match-pos cfd-limit)
5222 (c-find-decl-prefix-search))))) ; Moves point, sets cfd-continue-pos,
5223 ; cfd-match-pos, etc.
5224
5225 \f
5226 ;; A cache for found types.
5227
5228 ;; Buffer local variable that contains an obarray with the types we've
5229 ;; found. If a declaration is recognized somewhere we record the
5230 ;; fully qualified identifier in it to recognize it as a type
5231 ;; elsewhere in the file too. This is not accurate since we do not
5232 ;; bother with the scoping rules of the languages, but in practice the
5233 ;; same name is seldom used as both a type and something else in a
5234 ;; file, and we only use this as a last resort in ambiguous cases (see
5235 ;; `c-forward-decl-or-cast-1').
5236 ;;
5237 ;; Not every type need be in this cache. However, things which have
5238 ;; ceased to be types must be removed from it.
5239 ;;
5240 ;; Template types in C++ are added here too but with the template
5241 ;; arglist replaced with "<>" in references or "<" for the one in the
5242 ;; primary type. E.g. the type "Foo<A,B>::Bar<C>" is stored as
5243 ;; "Foo<>::Bar<". This avoids storing very long strings (since C++
5244 ;; template specs can be fairly sized programs in themselves) and
5245 ;; improves the hit ratio (it's a type regardless of the template
5246 ;; args; it's just not the same type, but we're only interested in
5247 ;; recognizing types, not telling distinct types apart). Note that
5248 ;; template types in references are added here too; from the example
5249 ;; above there will also be an entry "Foo<".
5250 (defvar c-found-types nil)
5251 (make-variable-buffer-local 'c-found-types)
5252
5253 (defsubst c-clear-found-types ()
5254 ;; Clears `c-found-types'.
5255 (setq c-found-types (make-vector 53 0)))
5256
5257 (defun c-add-type (from to)
5258 ;; Add the given region as a type in `c-found-types'. If the region
5259 ;; doesn't match an existing type but there is a type which is equal
5260 ;; to the given one except that the last character is missing, then
5261 ;; the shorter type is removed. That's done to avoid adding all
5262 ;; prefixes of a type as it's being entered and font locked. This
5263 ;; doesn't cover cases like when characters are removed from a type
5264 ;; or added in the middle. We'd need the position of point when the
5265 ;; font locking is invoked to solve this well.
5266 ;;
5267 ;; This function might do hidden buffer changes.
5268 (let ((type (c-syntactic-content from to c-recognize-<>-arglists)))
5269 (unless (intern-soft type c-found-types)
5270 (unintern (substring type 0 -1) c-found-types)
5271 (intern type c-found-types))))
5272
5273 (defun c-unfind-type (name)
5274 ;; Remove the "NAME" from c-found-types, if present.
5275 (unintern name c-found-types))
5276
5277 (defsubst c-check-type (from to)
5278 ;; Return non-nil if the given region contains a type in
5279 ;; `c-found-types'.
5280 ;;
5281 ;; This function might do hidden buffer changes.
5282 (intern-soft (c-syntactic-content from to c-recognize-<>-arglists)
5283 c-found-types))
5284
5285 (defun c-list-found-types ()
5286 ;; Return all the types in `c-found-types' as a sorted list of
5287 ;; strings.
5288 (let (type-list)
5289 (mapatoms (lambda (type)
5290 (setq type-list (cons (symbol-name type)
5291 type-list)))
5292 c-found-types)
5293 (sort type-list 'string-lessp)))
5294
5295 ;; Shut up the byte compiler.
5296 (defvar c-maybe-stale-found-type)
5297
5298 (defun c-trim-found-types (beg end old-len)
5299 ;; An after change function which, in conjunction with the info in
5300 ;; c-maybe-stale-found-type (set in c-before-change), removes a type
5301 ;; from `c-found-types', should this type have become stale. For
5302 ;; example, this happens to "foo" when "foo \n bar();" becomes
5303 ;; "foo(); \n bar();". Such stale types, if not removed, foul up
5304 ;; the fontification.
5305 ;;
5306 ;; Have we, perhaps, added non-ws characters to the front/back of a found
5307 ;; type?
5308 (when (> end beg)
5309 (save-excursion
5310 (when (< end (point-max))
5311 (goto-char end)
5312 (if (and (c-beginning-of-current-token) ; only moves when we started in the middle
5313 (progn (goto-char end)
5314 (c-end-of-current-token)))
5315 (c-unfind-type (buffer-substring-no-properties
5316 end (point)))))
5317 (when (> beg (point-min))
5318 (goto-char beg)
5319 (if (and (c-end-of-current-token) ; only moves when we started in the middle
5320 (progn (goto-char beg)
5321 (c-beginning-of-current-token)))
5322 (c-unfind-type (buffer-substring-no-properties
5323 (point) beg))))))
5324
5325 (if c-maybe-stale-found-type ; e.g. (c-decl-id-start "foo" 97 107 " (* ooka) " "o")
5326 (cond
5327 ;; Changing the amount of (already existing) whitespace - don't do anything.
5328 ((and (c-partial-ws-p beg end)
5329 (or (= beg end) ; removal of WS
5330 (string-match "^[ \t\n\r\f\v]*$" (nth 5 c-maybe-stale-found-type)))))
5331
5332 ;; The syntactic relationship which defined a "found type" has been
5333 ;; destroyed.
5334 ((eq (car c-maybe-stale-found-type) 'c-decl-id-start)
5335 (c-unfind-type (cadr c-maybe-stale-found-type)))
5336 ;; ((eq (car c-maybe-stale-found-type) 'c-decl-type-start) FIXME!!!
5337 )))
5338
5339 \f
5340 ;; Setting and removing syntax properties on < and > in languages (C++
5341 ;; and Java) where they can be template/generic delimiters as well as
5342 ;; their normal meaning of "less/greater than".
5343
5344 ;; Normally, < and > have syntax 'punctuation'. When they are found to
5345 ;; be delimiters, they are marked as such with the category properties
5346 ;; c-<-as-paren-syntax, c->-as-paren-syntax respectively.
5347
5348 ;; STRATEGY:
5349 ;;
5350 ;; It is impossible to determine with certainty whether a <..> pair in
5351 ;; C++ is two comparison operators or is template delimiters, unless
5352 ;; one duplicates a lot of a C++ compiler. For example, the following
5353 ;; code fragment:
5354 ;;
5355 ;; foo (a < b, c > d) ;
5356 ;;
5357 ;; could be a function call with two integer parameters (each a
5358 ;; relational expression), or it could be a constructor for class foo
5359 ;; taking one parameter d of templated type "a < b, c >". They are
5360 ;; somewhat easier to distinguish in Java.
5361 ;;
5362 ;; The strategy now (2010-01) adopted is to mark and unmark < and
5363 ;; > IN MATCHING PAIRS ONLY. [Previously, they were marked
5364 ;; individually when their context so indicated. This gave rise to
5365 ;; intractable problems when one of a matching pair was deleted, or
5366 ;; pulled into a literal.]
5367 ;;
5368 ;; At each buffer change, the syntax-table properties are removed in a
5369 ;; before-change function and reapplied, when needed, in an
5370 ;; after-change function. It is far more important that the
5371 ;; properties get removed when they they are spurious than that they
5372 ;; be present when wanted.
5373 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
5374 (defun c-clear-<-pair-props (&optional pos)
5375 ;; POS (default point) is at a < character. If it is marked with
5376 ;; open paren syntax-table text property, remove the property,
5377 ;; together with the close paren property on the matching > (if
5378 ;; any).
5379 (save-excursion
5380 (if pos
5381 (goto-char pos)
5382 (setq pos (point)))
5383 (when (equal (c-get-char-property (point) 'syntax-table)
5384 c-<-as-paren-syntax)
5385 (with-syntax-table c-no-parens-syntax-table ; ignore unbalanced [,{,(,..
5386 (c-go-list-forward))
5387 (when (equal (c-get-char-property (1- (point)) 'syntax-table)
5388 c->-as-paren-syntax) ; should always be true.
5389 (c-clear-char-property (1- (point)) 'category))
5390 (c-clear-char-property pos 'category))))
5391
5392 (defun c-clear->-pair-props (&optional pos)
5393 ;; POS (default point) is at a > character. If it is marked with
5394 ;; close paren syntax-table property, remove the property, together
5395 ;; with the open paren property on the matching < (if any).
5396 (save-excursion
5397 (if pos
5398 (goto-char pos)
5399 (setq pos (point)))
5400 (when (equal (c-get-char-property (point) 'syntax-table)
5401 c->-as-paren-syntax)
5402 (with-syntax-table c-no-parens-syntax-table ; ignore unbalanced [,{,(,..
5403 (c-go-up-list-backward))
5404 (when (equal (c-get-char-property (point) 'syntax-table)
5405 c-<-as-paren-syntax) ; should always be true.
5406 (c-clear-char-property (point) 'category))
5407 (c-clear-char-property pos 'category))))
5408
5409 (defun c-clear-<>-pair-props (&optional pos)
5410 ;; POS (default point) is at a < or > character. If it has an
5411 ;; open/close paren syntax-table property, remove this property both
5412 ;; from the current character and its partner (which will also be
5413 ;; thusly marked).
5414 (cond
5415 ((eq (char-after) ?\<)
5416 (c-clear-<-pair-props pos))
5417 ((eq (char-after) ?\>)
5418 (c-clear->-pair-props pos))
5419 (t (c-benign-error
5420 "c-clear-<>-pair-props called from wrong position"))))
5421
5422 (defun c-clear-<-pair-props-if-match-after (lim &optional pos)
5423 ;; POS (default point) is at a < character. If it is both marked
5424 ;; with open/close paren syntax-table property, and has a matching >
5425 ;; (also marked) which is after LIM, remove the property both from
5426 ;; the current > and its partner. Return t when this happens, nil
5427 ;; when it doesn't.
5428 (save-excursion
5429 (if pos
5430 (goto-char pos)
5431 (setq pos (point)))
5432 (when (equal (c-get-char-property (point) 'syntax-table)
5433 c-<-as-paren-syntax)
5434 (with-syntax-table c-no-parens-syntax-table ; ignore unbalanced [,{,(,..
5435 (c-go-list-forward))
5436 (when (and (>= (point) lim)
5437 (equal (c-get-char-property (1- (point)) 'syntax-table)
5438 c->-as-paren-syntax)) ; should always be true.
5439 (c-unmark-<->-as-paren (1- (point)))
5440 (c-unmark-<->-as-paren pos))
5441 t)))
5442
5443 (defun c-clear->-pair-props-if-match-before (lim &optional pos)
5444 ;; POS (default point) is at a > character. If it is both marked
5445 ;; with open/close paren syntax-table property, and has a matching <
5446 ;; (also marked) which is before LIM, remove the property both from
5447 ;; the current < and its partner. Return t when this happens, nil
5448 ;; when it doesn't.
5449 (save-excursion
5450 (if pos
5451 (goto-char pos)
5452 (setq pos (point)))
5453 (when (equal (c-get-char-property (point) 'syntax-table)
5454 c->-as-paren-syntax)
5455 (with-syntax-table c-no-parens-syntax-table ; ignore unbalanced [,{,(,..
5456 (c-go-up-list-backward))
5457 (when (and (<= (point) lim)
5458 (equal (c-get-char-property (point) 'syntax-table)
5459 c-<-as-paren-syntax)) ; should always be true.
5460 (c-unmark-<->-as-paren (point))
5461 (c-unmark-<->-as-paren pos))
5462 t)))
5463
5464 ;; Set by c-common-init in cc-mode.el.
5465 (defvar c-new-BEG)
5466 (defvar c-new-END)
5467
5468 (defun c-before-change-check-<>-operators (beg end)
5469 ;; Unmark certain pairs of "< .... >" which are currently marked as
5470 ;; template/generic delimiters. (This marking is via syntax-table
5471 ;; text properties).
5472 ;;
5473 ;; These pairs are those which are in the current "statement" (i.e.,
5474 ;; the region between the {, }, or ; before BEG and the one after
5475 ;; END), and which enclose any part of the interval (BEG END).
5476 ;;
5477 ;; Note that in C++ (?and Java), template/generic parens cannot
5478 ;; enclose a brace or semicolon, so we use these as bounds on the
5479 ;; region we must work on.
5480 ;;
5481 ;; This function is called from before-change-functions (via
5482 ;; c-get-state-before-change-functions). Thus the buffer is widened,
5483 ;; and point is undefined, both at entry and exit.
5484 ;;
5485 ;; FIXME!!! This routine ignores the possibility of macros entirely.
5486 ;; 2010-01-29.
5487 (save-excursion
5488 (let ((beg-lit-limits (progn (goto-char beg) (c-literal-limits)))
5489 (end-lit-limits (progn (goto-char end) (c-literal-limits)))
5490 new-beg new-end need-new-beg need-new-end)
5491 ;; Locate the barrier before the changed region
5492 (goto-char (if beg-lit-limits (car beg-lit-limits) beg))
5493 (c-syntactic-skip-backward "^;{}" (c-determine-limit 512))
5494 (setq new-beg (point))
5495
5496 ;; Remove the syntax-table properties from each pertinent <...> pair.
5497 ;; Firsly, the ones with the < before beg and > after beg.
5498 (while (c-search-forward-char-property 'category 'c-<-as-paren-syntax beg)
5499 (if (c-clear-<-pair-props-if-match-after beg (1- (point)))
5500 (setq need-new-beg t)))
5501
5502 ;; Locate the barrier after END.
5503 (goto-char (if end-lit-limits (cdr end-lit-limits) end))
5504 (c-syntactic-re-search-forward "[;{}]" (c-determine-+ve-limit 512) 'end)
5505 (setq new-end (point))
5506
5507 ;; Remove syntax-table properties from the remaining pertinent <...>
5508 ;; pairs, those with a > after end and < before end.
5509 (while (c-search-backward-char-property 'category 'c->-as-paren-syntax end)
5510 (if (c-clear->-pair-props-if-match-before end)
5511 (setq need-new-end t)))
5512
5513 ;; Extend the fontification region, if needed.
5514 (when need-new-beg
5515 (goto-char new-beg)
5516 (c-forward-syntactic-ws)
5517 (and (< (point) c-new-BEG) (setq c-new-BEG (point))))
5518
5519 (when need-new-end
5520 (and (> new-end c-new-END) (setq c-new-END new-end))))))
5521
5522
5523
5524 (defun c-after-change-check-<>-operators (beg end)
5525 ;; This is called from `after-change-functions' when
5526 ;; c-recognize-<>-arglists' is set. It ensures that no "<" or ">"
5527 ;; chars with paren syntax become part of another operator like "<<"
5528 ;; or ">=".
5529 ;;
5530 ;; This function might do hidden buffer changes.
5531
5532 (save-excursion
5533 (goto-char beg)
5534 (when (or (looking-at "[<>]")
5535 (< (skip-chars-backward "<>") 0))
5536
5537 (goto-char beg)
5538 (c-beginning-of-current-token)
5539 (when (and (< (point) beg)
5540 (looking-at c-<>-multichar-token-regexp)
5541 (< beg (setq beg (match-end 0))))
5542 (while (progn (skip-chars-forward "^<>" beg)
5543 (< (point) beg))
5544 (c-clear-<>-pair-props)
5545 (forward-char))))
5546
5547 (when (< beg end)
5548 (goto-char end)
5549 (when (or (looking-at "[<>]")
5550 (< (skip-chars-backward "<>") 0))
5551
5552 (goto-char end)
5553 (c-beginning-of-current-token)
5554 (when (and (< (point) end)
5555 (looking-at c-<>-multichar-token-regexp)
5556 (< end (setq end (match-end 0))))
5557 (while (progn (skip-chars-forward "^<>" end)
5558 (< (point) end))
5559 (c-clear-<>-pair-props)
5560 (forward-char)))))))
5561
5562
5563 \f
5564 ;; Handling of small scale constructs like types and names.
5565
5566 ;; Dynamically bound variable that instructs `c-forward-type' to also
5567 ;; treat possible types (i.e. those that it normally returns 'maybe or
5568 ;; 'found for) as actual types (and always return 'found for them).
5569 ;; This means that it records them in `c-record-type-identifiers' if
5570 ;; that is set, and that it adds them to `c-found-types'.
5571 (defvar c-promote-possible-types nil)
5572
5573 ;; Dynamically bound variable that instructs `c-forward-<>-arglist' to
5574 ;; mark up successfully parsed arglists with paren syntax properties on
5575 ;; the surrounding angle brackets and with `c-<>-arg-sep' in the
5576 ;; `c-type' property of each argument separating comma.
5577 ;;
5578 ;; Setting this variable also makes `c-forward-<>-arglist' recurse into
5579 ;; all arglists for side effects (i.e. recording types), otherwise it
5580 ;; exploits any existing paren syntax properties to quickly jump to the
5581 ;; end of already parsed arglists.
5582 ;;
5583 ;; Marking up the arglists is not the default since doing that correctly
5584 ;; depends on a proper value for `c-restricted-<>-arglists'.
5585 (defvar c-parse-and-markup-<>-arglists nil)
5586
5587 ;; Dynamically bound variable that instructs `c-forward-<>-arglist' to
5588 ;; not accept arglists that contain binary operators.
5589 ;;
5590 ;; This is primarily used to handle C++ template arglists. C++
5591 ;; disambiguates them by checking whether the preceding name is a
5592 ;; template or not. We can't do that, so we assume it is a template
5593 ;; if it can be parsed as one. That usually works well since
5594 ;; comparison expressions on the forms "a < b > c" or "a < b, c > d"
5595 ;; in almost all cases would be pointless.
5596 ;;
5597 ;; However, in function arglists, e.g. in "foo (a < b, c > d)", we
5598 ;; should let the comma separate the function arguments instead. And
5599 ;; in a context where the value of the expression is taken, e.g. in
5600 ;; "if (a < b || c > d)", it's probably not a template.
5601 (defvar c-restricted-<>-arglists nil)
5602
5603 ;; Dynamically bound variables that instructs
5604 ;; `c-forward-keyword-clause', `c-forward-<>-arglist',
5605 ;; `c-forward-name', `c-forward-type', `c-forward-decl-or-cast-1', and
5606 ;; `c-forward-label' to record the ranges of all the type and
5607 ;; reference identifiers they encounter. They will build lists on
5608 ;; these variables where each element is a cons of the buffer
5609 ;; positions surrounding each identifier. This recording is only
5610 ;; activated when `c-record-type-identifiers' is non-nil.
5611 ;;
5612 ;; All known types that can't be identifiers are recorded, and also
5613 ;; other possible types if `c-promote-possible-types' is set.
5614 ;; Recording is however disabled inside angle bracket arglists that
5615 ;; are encountered inside names and other angle bracket arglists.
5616 ;; Such occurrences are taken care of by `c-font-lock-<>-arglists'
5617 ;; instead.
5618 ;;
5619 ;; Only the names in C++ template style references (e.g. "tmpl" in
5620 ;; "tmpl<a,b>::foo") are recorded as references, other references
5621 ;; aren't handled here.
5622 ;;
5623 ;; `c-forward-label' records the label identifier(s) on
5624 ;; `c-record-ref-identifiers'.
5625 (defvar c-record-type-identifiers nil)
5626 (defvar c-record-ref-identifiers nil)
5627
5628 ;; This variable will receive a cons cell of the range of the last
5629 ;; single identifier symbol stepped over by `c-forward-name' if it's
5630 ;; successful. This is the range that should be put on one of the
5631 ;; record lists above by the caller. It's assigned nil if there's no
5632 ;; such symbol in the name.
5633 (defvar c-last-identifier-range nil)
5634
5635 (defmacro c-record-type-id (range)
5636 (if (eq (car-safe range) 'cons)
5637 ;; Always true.
5638 `(setq c-record-type-identifiers
5639 (cons ,range c-record-type-identifiers))
5640 `(let ((range ,range))
5641 (if range
5642 (setq c-record-type-identifiers
5643 (cons range c-record-type-identifiers))))))
5644
5645 (defmacro c-record-ref-id (range)
5646 (if (eq (car-safe range) 'cons)
5647 ;; Always true.
5648 `(setq c-record-ref-identifiers
5649 (cons ,range c-record-ref-identifiers))
5650 `(let ((range ,range))
5651 (if range
5652 (setq c-record-ref-identifiers
5653 (cons range c-record-ref-identifiers))))))
5654
5655 ;; Dynamically bound variable that instructs `c-forward-type' to
5656 ;; record the ranges of types that only are found. Behaves otherwise
5657 ;; like `c-record-type-identifiers'.
5658 (defvar c-record-found-types nil)
5659
5660 (defmacro c-forward-keyword-prefixed-id (type)
5661 ;; Used internally in `c-forward-keyword-clause' to move forward
5662 ;; over a type (if TYPE is 'type) or a name (otherwise) which
5663 ;; possibly is prefixed by keywords and their associated clauses.
5664 ;; Try with a type/name first to not trip up on those that begin
5665 ;; with a keyword. Return t if a known or found type is moved
5666 ;; over. The point is clobbered if nil is returned. If range
5667 ;; recording is enabled, the identifier is recorded on as a type
5668 ;; if TYPE is 'type or as a reference if TYPE is 'ref.
5669 ;;
5670 ;; This macro might do hidden buffer changes.
5671 `(let (res)
5672 (while (if (setq res ,(if (eq type 'type)
5673 `(c-forward-type)
5674 `(c-forward-name)))
5675 nil
5676 (and (looking-at c-keywords-regexp)
5677 (c-forward-keyword-clause 1))))
5678 (when (memq res '(t known found prefix))
5679 ,(when (eq type 'ref)
5680 `(when c-record-type-identifiers
5681 (c-record-ref-id c-last-identifier-range)))
5682 t)))
5683
5684 (defmacro c-forward-id-comma-list (type update-safe-pos)
5685 ;; Used internally in `c-forward-keyword-clause' to move forward
5686 ;; over a comma separated list of types or names using
5687 ;; `c-forward-keyword-prefixed-id'.
5688 ;;
5689 ;; This macro might do hidden buffer changes.
5690 `(while (and (progn
5691 ,(when update-safe-pos
5692 `(setq safe-pos (point)))
5693 (eq (char-after) ?,))
5694 (progn
5695 (forward-char)
5696 (c-forward-syntactic-ws)
5697 (c-forward-keyword-prefixed-id ,type)))))
5698
5699 (defun c-forward-keyword-clause (match)
5700 ;; Submatch MATCH in the current match data is assumed to surround a
5701 ;; token. If it's a keyword, move over it and any immediately
5702 ;; following clauses associated with it, stopping at the start of
5703 ;; the next token. t is returned in that case, otherwise the point
5704 ;; stays and nil is returned. The kind of clauses that are
5705 ;; recognized are those specified by `c-type-list-kwds',
5706 ;; `c-ref-list-kwds', `c-colon-type-list-kwds',
5707 ;; `c-paren-nontype-kwds', `c-paren-type-kwds', `c-<>-type-kwds',
5708 ;; and `c-<>-arglist-kwds'.
5709 ;;
5710 ;; This function records identifier ranges on
5711 ;; `c-record-type-identifiers' and `c-record-ref-identifiers' if
5712 ;; `c-record-type-identifiers' is non-nil.
5713 ;;
5714 ;; Note that for `c-colon-type-list-kwds', which doesn't necessary
5715 ;; apply directly after the keyword, the type list is moved over
5716 ;; only when there is no unaccounted token before it (i.e. a token
5717 ;; that isn't moved over due to some other keyword list). The
5718 ;; identifier ranges in the list are still recorded if that should
5719 ;; be done, though.
5720 ;;
5721 ;; This function might do hidden buffer changes.
5722
5723 (let ((kwd-sym (c-keyword-sym (match-string match))) safe-pos pos
5724 ;; The call to `c-forward-<>-arglist' below is made after
5725 ;; `c-<>-sexp-kwds' keywords, so we're certain they actually
5726 ;; are angle bracket arglists and `c-restricted-<>-arglists'
5727 ;; should therefore be nil.
5728 (c-parse-and-markup-<>-arglists t)
5729 c-restricted-<>-arglists)
5730
5731 (when kwd-sym
5732 (goto-char (match-end match))
5733 (c-forward-syntactic-ws)
5734 (setq safe-pos (point))
5735
5736 (cond
5737 ((and (c-keyword-member kwd-sym 'c-type-list-kwds)
5738 (c-forward-keyword-prefixed-id type))
5739 ;; There's a type directly after a keyword in `c-type-list-kwds'.
5740 (c-forward-id-comma-list type t))
5741
5742 ((and (c-keyword-member kwd-sym 'c-ref-list-kwds)
5743 (c-forward-keyword-prefixed-id ref))
5744 ;; There's a name directly after a keyword in `c-ref-list-kwds'.
5745 (c-forward-id-comma-list ref t))
5746
5747 ((and (c-keyword-member kwd-sym 'c-paren-any-kwds)
5748 (eq (char-after) ?\())
5749 ;; There's an open paren after a keyword in `c-paren-any-kwds'.
5750
5751 (forward-char)
5752 (when (and (setq pos (c-up-list-forward))
5753 (eq (char-before pos) ?\)))
5754 (when (and c-record-type-identifiers
5755 (c-keyword-member kwd-sym 'c-paren-type-kwds))
5756 ;; Use `c-forward-type' on every identifier we can find
5757 ;; inside the paren, to record the types.
5758 (while (c-syntactic-re-search-forward c-symbol-start pos t)
5759 (goto-char (match-beginning 0))
5760 (unless (c-forward-type)
5761 (looking-at c-symbol-key) ; Always matches.
5762 (goto-char (match-end 0)))))
5763
5764 (goto-char pos)
5765 (c-forward-syntactic-ws)
5766 (setq safe-pos (point))))
5767
5768 ((and (c-keyword-member kwd-sym 'c-<>-sexp-kwds)
5769 (eq (char-after) ?<)
5770 (c-forward-<>-arglist (c-keyword-member kwd-sym 'c-<>-type-kwds)))
5771 (c-forward-syntactic-ws)
5772 (setq safe-pos (point)))
5773
5774 ((and (c-keyword-member kwd-sym 'c-nonsymbol-sexp-kwds)
5775 (not (looking-at c-symbol-start))
5776 (c-safe (c-forward-sexp) t))
5777 (c-forward-syntactic-ws)
5778 (setq safe-pos (point))))
5779
5780 (when (c-keyword-member kwd-sym 'c-colon-type-list-kwds)
5781 (if (eq (char-after) ?:)
5782 ;; If we are at the colon already, we move over the type
5783 ;; list after it.
5784 (progn
5785 (forward-char)
5786 (c-forward-syntactic-ws)
5787 (when (c-forward-keyword-prefixed-id type)
5788 (c-forward-id-comma-list type t)))
5789 ;; Not at the colon, so stop here. But the identifier
5790 ;; ranges in the type list later on should still be
5791 ;; recorded.
5792 (and c-record-type-identifiers
5793 (progn
5794 ;; If a keyword matched both one of the types above and
5795 ;; this one, we match `c-colon-type-list-re' after the
5796 ;; clause matched above.
5797 (goto-char safe-pos)
5798 (looking-at c-colon-type-list-re))
5799 (progn
5800 (goto-char (match-end 0))
5801 (c-forward-syntactic-ws)
5802 (c-forward-keyword-prefixed-id type))
5803 ;; There's a type after the `c-colon-type-list-re' match
5804 ;; after a keyword in `c-colon-type-list-kwds'.
5805 (c-forward-id-comma-list type nil))))
5806
5807 (goto-char safe-pos)
5808 t)))
5809
5810 ;; cc-mode requires cc-fonts.
5811 (declare-function c-fontify-recorded-types-and-refs "cc-fonts" ())
5812
5813 (defun c-forward-<>-arglist (all-types)
5814 ;; The point is assumed to be at a "<". Try to treat it as the open
5815 ;; paren of an angle bracket arglist and move forward to the
5816 ;; corresponding ">". If successful, the point is left after the
5817 ;; ">" and t is returned, otherwise the point isn't moved and nil is
5818 ;; returned. If ALL-TYPES is t then all encountered arguments in
5819 ;; the arglist that might be types are treated as found types.
5820 ;;
5821 ;; The variable `c-parse-and-markup-<>-arglists' controls how this
5822 ;; function handles text properties on the angle brackets and argument
5823 ;; separating commas.
5824 ;;
5825 ;; `c-restricted-<>-arglists' controls how lenient the template
5826 ;; arglist recognition should be.
5827 ;;
5828 ;; This function records identifier ranges on
5829 ;; `c-record-type-identifiers' and `c-record-ref-identifiers' if
5830 ;; `c-record-type-identifiers' is non-nil.
5831 ;;
5832 ;; This function might do hidden buffer changes.
5833
5834 (let ((start (point))
5835 ;; If `c-record-type-identifiers' is set then activate
5836 ;; recording of any found types that constitute an argument in
5837 ;; the arglist.
5838 (c-record-found-types (if c-record-type-identifiers t)))
5839 (if (catch 'angle-bracket-arglist-escape
5840 (setq c-record-found-types
5841 (c-forward-<>-arglist-recur all-types)))
5842 (progn
5843 (when (consp c-record-found-types)
5844 (setq c-record-type-identifiers
5845 ;; `nconc' doesn't mind that the tail of
5846 ;; `c-record-found-types' is t.
5847 (nconc c-record-found-types c-record-type-identifiers)))
5848 (if (c-major-mode-is 'java-mode) (c-fontify-recorded-types-and-refs))
5849 t)
5850
5851 (goto-char start)
5852 nil)))
5853
5854 (defun c-forward-<>-arglist-recur (all-types)
5855 ;; Recursive part of `c-forward-<>-arglist'.
5856 ;;
5857 ;; This function might do hidden buffer changes.
5858
5859 (let ((start (point)) res pos tmp
5860 ;; Cover this so that any recorded found type ranges are
5861 ;; automatically lost if it turns out to not be an angle
5862 ;; bracket arglist. It's propagated through the return value
5863 ;; on successful completion.
5864 (c-record-found-types c-record-found-types)
5865 ;; List that collects the positions after the argument
5866 ;; separating ',' in the arglist.
5867 arg-start-pos)
5868 ;; If the '<' has paren open syntax then we've marked it as an angle
5869 ;; bracket arglist before, so skip to the end.
5870 (if (and (not c-parse-and-markup-<>-arglists)
5871 (c-get-char-property (point) 'syntax-table))
5872
5873 (progn
5874 (forward-char)
5875 (if (and (c-go-up-list-forward)
5876 (eq (char-before) ?>))
5877 t
5878 ;; Got unmatched paren angle brackets. We don't clear the paren
5879 ;; syntax properties and retry, on the basis that it's very
5880 ;; unlikely that paren angle brackets become operators by code
5881 ;; manipulation. It's far more likely that it doesn't match due
5882 ;; to narrowing or some temporary change.
5883 (goto-char start)
5884 nil))
5885
5886 (forward-char) ; Forward over the opening '<'.
5887
5888 (unless (looking-at c-<-op-cont-regexp)
5889 ;; go forward one non-alphanumeric character (group) per iteration of
5890 ;; this loop.
5891 (while (and
5892 (progn
5893 (c-forward-syntactic-ws)
5894 (let ((orig-record-found-types c-record-found-types))
5895 (when (or (and c-record-type-identifiers all-types)
5896 (c-major-mode-is 'java-mode))
5897 ;; All encountered identifiers are types, so set the
5898 ;; promote flag and parse the type.
5899 (progn
5900 (c-forward-syntactic-ws)
5901 (if (looking-at "\\?")
5902 (forward-char)
5903 (when (looking-at c-identifier-start)
5904 (let ((c-promote-possible-types t)
5905 (c-record-found-types t))
5906 (c-forward-type))))
5907
5908 (c-forward-syntactic-ws)
5909
5910 (when (or (looking-at "extends")
5911 (looking-at "super"))
5912 (forward-word)
5913 (c-forward-syntactic-ws)
5914 (let ((c-promote-possible-types t)
5915 (c-record-found-types t))
5916 (c-forward-type)
5917 (c-forward-syntactic-ws))))))
5918
5919 (setq pos (point)) ; e.g. first token inside the '<'
5920
5921 ;; Note: These regexps exploit the match order in \| so
5922 ;; that "<>" is matched by "<" rather than "[^>:-]>".
5923 (c-syntactic-re-search-forward
5924 ;; Stop on ',', '|', '&', '+' and '-' to catch
5925 ;; common binary operators that could be between
5926 ;; two comparison expressions "a<b" and "c>d".
5927 "[<;{},|+&-]\\|[>)]"
5928 nil t t))
5929
5930 (cond
5931 ((eq (char-before) ?>)
5932 ;; Either an operator starting with '>' or the end of
5933 ;; the angle bracket arglist.
5934
5935 (if (looking-at c->-op-cont-regexp)
5936 (progn
5937 (goto-char (match-end 0))
5938 t) ; Continue the loop.
5939
5940 ;; The angle bracket arglist is finished.
5941 (when c-parse-and-markup-<>-arglists
5942 (while arg-start-pos
5943 (c-put-c-type-property (1- (car arg-start-pos))
5944 'c-<>-arg-sep)
5945 (setq arg-start-pos (cdr arg-start-pos)))
5946 (c-mark-<-as-paren start)
5947 (c-mark->-as-paren (1- (point))))
5948 (setq res t)
5949 nil)) ; Exit the loop.
5950
5951 ((eq (char-before) ?<)
5952 ;; Either an operator starting with '<' or a nested arglist.
5953 (setq pos (point))
5954 (let (id-start id-end subres keyword-match)
5955 (cond
5956 ;; The '<' begins a multi-char operator.
5957 ((looking-at c-<-op-cont-regexp)
5958 (setq tmp (match-end 0))
5959 (goto-char (match-end 0)))
5960 ;; We're at a nested <.....>
5961 ((progn
5962 (setq tmp pos)
5963 (backward-char) ; to the '<'
5964 (and
5965 (save-excursion
5966 ;; There's always an identifier before an angle
5967 ;; bracket arglist, or a keyword in `c-<>-type-kwds'
5968 ;; or `c-<>-arglist-kwds'.
5969 (c-backward-syntactic-ws)
5970 (setq id-end (point))
5971 (c-simple-skip-symbol-backward)
5972 (when (or (setq keyword-match
5973 (looking-at c-opt-<>-sexp-key))
5974 (not (looking-at c-keywords-regexp)))
5975 (setq id-start (point))))
5976 (setq subres
5977 (let ((c-promote-possible-types t)
5978 (c-record-found-types t))
5979 (c-forward-<>-arglist-recur
5980 (and keyword-match
5981 (c-keyword-member
5982 (c-keyword-sym (match-string 1))
5983 'c-<>-type-kwds)))))))
5984
5985 ;; It was an angle bracket arglist.
5986 (setq c-record-found-types subres)
5987
5988 ;; Record the identifier before the template as a type
5989 ;; or reference depending on whether the arglist is last
5990 ;; in a qualified identifier.
5991 (when (and c-record-type-identifiers
5992 (not keyword-match))
5993 (if (and c-opt-identifier-concat-key
5994 (progn
5995 (c-forward-syntactic-ws)
5996 (looking-at c-opt-identifier-concat-key)))
5997 (c-record-ref-id (cons id-start id-end))
5998 (c-record-type-id (cons id-start id-end)))))
5999
6000 ;; At a "less than" operator.
6001 (t
6002 (forward-char)
6003 )))
6004 t) ; carry on looping.
6005
6006 ((and (not c-restricted-<>-arglists)
6007 (or (and (eq (char-before) ?&)
6008 (not (eq (char-after) ?&)))
6009 (eq (char-before) ?,)))
6010 ;; Just another argument. Record the position. The
6011 ;; type check stuff that made us stop at it is at
6012 ;; the top of the loop.
6013 (setq arg-start-pos (cons (point) arg-start-pos)))
6014
6015 (t
6016 ;; Got a character that can't be in an angle bracket
6017 ;; arglist argument. Abort using `throw', since
6018 ;; it's useless to try to find a surrounding arglist
6019 ;; if we're nested.
6020 (throw 'angle-bracket-arglist-escape nil))))))
6021 (if res
6022 (or c-record-found-types t)))))
6023
6024 (defun c-backward-<>-arglist (all-types &optional limit)
6025 ;; The point is assumed to be directly after a ">". Try to treat it
6026 ;; as the close paren of an angle bracket arglist and move back to
6027 ;; the corresponding "<". If successful, the point is left at
6028 ;; the "<" and t is returned, otherwise the point isn't moved and
6029 ;; nil is returned. ALL-TYPES is passed on to
6030 ;; `c-forward-<>-arglist'.
6031 ;;
6032 ;; If the optional LIMIT is given, it bounds the backward search.
6033 ;; It's then assumed to be at a syntactically relevant position.
6034 ;;
6035 ;; This is a wrapper around `c-forward-<>-arglist'. See that
6036 ;; function for more details.
6037
6038 (let ((start (point)))
6039 (backward-char)
6040 (if (and (not c-parse-and-markup-<>-arglists)
6041 (c-get-char-property (point) 'syntax-table))
6042
6043 (if (and (c-go-up-list-backward)
6044 (eq (char-after) ?<))
6045 t
6046 ;; See corresponding note in `c-forward-<>-arglist'.
6047 (goto-char start)
6048 nil)
6049
6050 (while (progn
6051 (c-syntactic-skip-backward "^<;{}" limit t)
6052
6053 (and
6054 (if (eq (char-before) ?<)
6055 t
6056 ;; Stopped at bob or a char that isn't allowed in an
6057 ;; arglist, so we've failed.
6058 (goto-char start)
6059 nil)
6060
6061 (if (> (point)
6062 (progn (c-beginning-of-current-token)
6063 (point)))
6064 ;; If we moved then the "<" was part of some
6065 ;; multicharacter token.
6066 t
6067
6068 (backward-char)
6069 (let ((beg-pos (point)))
6070 (if (c-forward-<>-arglist all-types)
6071 (cond ((= (point) start)
6072 ;; Matched the arglist. Break the while.
6073 (goto-char beg-pos)
6074 nil)
6075 ((> (point) start)
6076 ;; We started from a non-paren ">" inside an
6077 ;; arglist.
6078 (goto-char start)
6079 nil)
6080 (t
6081 ;; Matched a shorter arglist. Can be a nested
6082 ;; one so continue looking.
6083 (goto-char beg-pos)
6084 t))
6085 t))))))
6086
6087 (/= (point) start))))
6088
6089 (defun c-forward-name ()
6090 ;; Move forward over a complete name if at the beginning of one,
6091 ;; stopping at the next following token. A keyword, as such,
6092 ;; doesn't count as a name. If the point is not at something that
6093 ;; is recognized as a name then it stays put.
6094 ;;
6095 ;; A name could be something as simple as "foo" in C or something as
6096 ;; complex as "X<Y<class A<int>::B, BIT_MAX >> b>, ::operator<> ::
6097 ;; Z<(a>b)> :: operator const X<&foo>::T Q::G<unsigned short
6098 ;; int>::*volatile const" in C++ (this function is actually little
6099 ;; more than a `looking-at' call in all modes except those that,
6100 ;; like C++, have `c-recognize-<>-arglists' set).
6101 ;;
6102 ;; Return
6103 ;; o - nil if no name is found;
6104 ;; o - 'template if it's an identifier ending with an angle bracket
6105 ;; arglist;
6106 ;; o - 'operator of it's an operator identifier;
6107 ;; o - t if it's some other kind of name.
6108 ;;
6109 ;; This function records identifier ranges on
6110 ;; `c-record-type-identifiers' and `c-record-ref-identifiers' if
6111 ;; `c-record-type-identifiers' is non-nil.
6112 ;;
6113 ;; This function might do hidden buffer changes.
6114
6115 (let ((pos (point)) (start (point)) res id-start id-end
6116 ;; Turn off `c-promote-possible-types' here since we might
6117 ;; call `c-forward-<>-arglist' and we don't want it to promote
6118 ;; every suspect thing in the arglist to a type. We're
6119 ;; typically called from `c-forward-type' in this case, and
6120 ;; the caller only wants the top level type that it finds to
6121 ;; be promoted.
6122 c-promote-possible-types)
6123 (while
6124 (and
6125 (looking-at c-identifier-key)
6126
6127 (progn
6128 ;; Check for keyword. We go to the last symbol in
6129 ;; `c-identifier-key' first.
6130 (goto-char (setq id-end (match-end 0)))
6131 (c-simple-skip-symbol-backward)
6132 (setq id-start (point))
6133
6134 (if (looking-at c-keywords-regexp)
6135 (when (and (c-major-mode-is 'c++-mode)
6136 (looking-at
6137 (cc-eval-when-compile
6138 (concat "\\(operator\\|\\(template\\)\\)"
6139 "\\(" (c-lang-const c-nonsymbol-key c++)
6140 "\\|$\\)")))
6141 (if (match-beginning 2)
6142 ;; "template" is only valid inside an
6143 ;; identifier if preceded by "::".
6144 (save-excursion
6145 (c-backward-syntactic-ws)
6146 (and (c-safe (backward-char 2) t)
6147 (looking-at "::")))
6148 t))
6149
6150 ;; Handle a C++ operator or template identifier.
6151 (goto-char id-end)
6152 (c-forward-syntactic-ws)
6153 (cond ((eq (char-before id-end) ?e)
6154 ;; Got "... ::template".
6155 (let ((subres (c-forward-name)))
6156 (when subres
6157 (setq pos (point)
6158 res subres))))
6159
6160 ((looking-at c-identifier-start)
6161 ;; Got a cast operator.
6162 (when (c-forward-type)
6163 (setq pos (point)
6164 res 'operator)
6165 ;; Now we should match a sequence of either
6166 ;; '*', '&' or a name followed by ":: *",
6167 ;; where each can be followed by a sequence
6168 ;; of `c-opt-type-modifier-key'.
6169 (while (cond ((looking-at "[*&]")
6170 (goto-char (match-end 0))
6171 t)
6172 ((looking-at c-identifier-start)
6173 (and (c-forward-name)
6174 (looking-at "::")
6175 (progn
6176 (goto-char (match-end 0))
6177 (c-forward-syntactic-ws)
6178 (eq (char-after) ?*))
6179 (progn
6180 (forward-char)
6181 t))))
6182 (while (progn
6183 (c-forward-syntactic-ws)
6184 (setq pos (point))
6185 (looking-at c-opt-type-modifier-key))
6186 (goto-char (match-end 1))))))
6187
6188 ((looking-at c-overloadable-operators-regexp)
6189 ;; Got some other operator.
6190 (setq c-last-identifier-range
6191 (cons (point) (match-end 0)))
6192 (goto-char (match-end 0))
6193 (c-forward-syntactic-ws)
6194 (setq pos (point)
6195 res 'operator)))
6196
6197 nil)
6198
6199 ;; `id-start' is equal to `id-end' if we've jumped over
6200 ;; an identifier that doesn't end with a symbol token.
6201 ;; That can occur e.g. for Java import directives on the
6202 ;; form "foo.bar.*".
6203 (when (and id-start (/= id-start id-end))
6204 (setq c-last-identifier-range
6205 (cons id-start id-end)))
6206 (goto-char id-end)
6207 (c-forward-syntactic-ws)
6208 (setq pos (point)
6209 res t)))
6210
6211 (progn
6212 (goto-char pos)
6213 (when (or c-opt-identifier-concat-key
6214 c-recognize-<>-arglists)
6215
6216 (cond
6217 ((and c-opt-identifier-concat-key
6218 (looking-at c-opt-identifier-concat-key))
6219 ;; Got a concatenated identifier. This handles the
6220 ;; cases with tricky syntactic whitespace that aren't
6221 ;; covered in `c-identifier-key'.
6222 (goto-char (match-end 0))
6223 (c-forward-syntactic-ws)
6224 t)
6225
6226 ((and c-recognize-<>-arglists
6227 (eq (char-after) ?<))
6228 ;; Maybe an angle bracket arglist.
6229 (when (let ((c-record-type-identifiers t)
6230 (c-record-found-types t))
6231 (c-forward-<>-arglist nil))
6232
6233 (c-add-type start (1+ pos))
6234 (c-forward-syntactic-ws)
6235 (setq pos (point)
6236 c-last-identifier-range nil)
6237
6238 (if (and c-opt-identifier-concat-key
6239 (looking-at c-opt-identifier-concat-key))
6240
6241 ;; Continue if there's an identifier concatenation
6242 ;; operator after the template argument.
6243 (progn
6244 (when (and c-record-type-identifiers id-start)
6245 (c-record-ref-id (cons id-start id-end)))
6246 (forward-char 2)
6247 (c-forward-syntactic-ws)
6248 t)
6249
6250 (when (and c-record-type-identifiers id-start)
6251 (c-record-type-id (cons id-start id-end)))
6252 (setq res 'template)
6253 nil)))
6254 )))))
6255
6256 (goto-char pos)
6257 res))
6258
6259 (defun c-forward-type (&optional brace-block-too)
6260 ;; Move forward over a type spec if at the beginning of one,
6261 ;; stopping at the next following token. The keyword "typedef"
6262 ;; isn't part of a type spec here.
6263 ;;
6264 ;; BRACE-BLOCK-TOO, when non-nil, means move over the brace block in
6265 ;; constructs like "struct foo {...} bar ;" or "struct {...} bar;".
6266 ;; The current (2009-03-10) intention is to convert all uses of
6267 ;; `c-forward-type' to call with this parameter set, then to
6268 ;; eliminate it.
6269 ;;
6270 ;; Return
6271 ;; o - t if it's a known type that can't be a name or other
6272 ;; expression;
6273 ;; o - 'known if it's an otherwise known type (according to
6274 ;; `*-font-lock-extra-types');
6275 ;; o - 'prefix if it's a known prefix of a type;
6276 ;; o - 'found if it's a type that matches one in `c-found-types';
6277 ;; o - 'maybe if it's an identifier that might be a type; or
6278 ;; o - nil if it can't be a type (the point isn't moved then).
6279 ;;
6280 ;; The point is assumed to be at the beginning of a token.
6281 ;;
6282 ;; Note that this function doesn't skip past the brace definition
6283 ;; that might be considered part of the type, e.g.
6284 ;; "enum {a, b, c} foo".
6285 ;;
6286 ;; This function records identifier ranges on
6287 ;; `c-record-type-identifiers' and `c-record-ref-identifiers' if
6288 ;; `c-record-type-identifiers' is non-nil.
6289 ;;
6290 ;; This function might do hidden buffer changes.
6291 (when (and c-recognize-<>-arglists
6292 (looking-at "<"))
6293 (c-forward-<>-arglist t)
6294 (c-forward-syntactic-ws))
6295
6296 (let ((start (point)) pos res name-res id-start id-end id-range)
6297
6298 ;; Skip leading type modifiers. If any are found we know it's a
6299 ;; prefix of a type.
6300 (when c-opt-type-modifier-key ; e.g. "const" "volatile", but NOT "typedef"
6301 (while (looking-at c-opt-type-modifier-key)
6302 (goto-char (match-end 1))
6303 (c-forward-syntactic-ws)
6304 (setq res 'prefix)))
6305
6306 (cond
6307 ((looking-at c-type-prefix-key) ; e.g. "struct", "class", but NOT
6308 ; "typedef".
6309 (goto-char (match-end 1))
6310 (c-forward-syntactic-ws)
6311 (setq pos (point))
6312
6313 (setq name-res (c-forward-name))
6314 (setq res (not (null name-res)))
6315 (when (eq name-res t)
6316 ;; In many languages the name can be used without the
6317 ;; prefix, so we add it to `c-found-types'.
6318 (c-add-type pos (point))
6319 (when (and c-record-type-identifiers
6320 c-last-identifier-range)
6321 (c-record-type-id c-last-identifier-range)))
6322 (when (and brace-block-too
6323 (memq res '(t nil))
6324 (eq (char-after) ?\{)
6325 (save-excursion
6326 (c-safe
6327 (progn (c-forward-sexp)
6328 (c-forward-syntactic-ws)
6329 (setq pos (point))))))
6330 (goto-char pos)
6331 (setq res t))
6332 (unless res (goto-char start))) ; invalid syntax
6333
6334 ((progn
6335 (setq pos nil)
6336 (if (looking-at c-identifier-start)
6337 (save-excursion
6338 (setq id-start (point)
6339 name-res (c-forward-name))
6340 (when name-res
6341 (setq id-end (point)
6342 id-range c-last-identifier-range))))
6343 (and (cond ((looking-at c-primitive-type-key)
6344 (setq res t))
6345 ((c-with-syntax-table c-identifier-syntax-table
6346 (looking-at c-known-type-key))
6347 (setq res 'known)))
6348 (or (not id-end)
6349 (>= (save-excursion
6350 (save-match-data
6351 (goto-char (match-end 1))
6352 (c-forward-syntactic-ws)
6353 (setq pos (point))))
6354 id-end)
6355 (setq res nil))))
6356 ;; Looking at a primitive or known type identifier. We've
6357 ;; checked for a name first so that we don't go here if the
6358 ;; known type match only is a prefix of another name.
6359
6360 (setq id-end (match-end 1))
6361
6362 (when (and c-record-type-identifiers
6363 (or c-promote-possible-types (eq res t)))
6364 (c-record-type-id (cons (match-beginning 1) (match-end 1))))
6365
6366 (if (and c-opt-type-component-key
6367 (save-match-data
6368 (looking-at c-opt-type-component-key)))
6369 ;; There might be more keywords for the type.
6370 (let (safe-pos)
6371 (c-forward-keyword-clause 1)
6372 (while (progn
6373 (setq safe-pos (point))
6374 (looking-at c-opt-type-component-key))
6375 (when (and c-record-type-identifiers
6376 (looking-at c-primitive-type-key))
6377 (c-record-type-id (cons (match-beginning 1)
6378 (match-end 1))))
6379 (c-forward-keyword-clause 1))
6380 (if (looking-at c-primitive-type-key)
6381 (progn
6382 (when c-record-type-identifiers
6383 (c-record-type-id (cons (match-beginning 1)
6384 (match-end 1))))
6385 (c-forward-keyword-clause 1)
6386 (setq res t))
6387 (goto-char safe-pos)
6388 (setq res 'prefix)))
6389 (unless (save-match-data (c-forward-keyword-clause 1))
6390 (if pos
6391 (goto-char pos)
6392 (goto-char (match-end 1))
6393 (c-forward-syntactic-ws)))))
6394
6395 (name-res
6396 (cond ((eq name-res t)
6397 ;; A normal identifier.
6398 (goto-char id-end)
6399 (if (or res c-promote-possible-types)
6400 (progn
6401 (c-add-type id-start id-end)
6402 (when (and c-record-type-identifiers id-range)
6403 (c-record-type-id id-range))
6404 (unless res
6405 (setq res 'found)))
6406 (setq res (if (c-check-type id-start id-end)
6407 ;; It's an identifier that has been used as
6408 ;; a type somewhere else.
6409 'found
6410 ;; It's an identifier that might be a type.
6411 'maybe))))
6412 ((eq name-res 'template)
6413 ;; A template is a type.
6414 (goto-char id-end)
6415 (setq res t))
6416 (t
6417 ;; Otherwise it's an operator identifier, which is not a type.
6418 (goto-char start)
6419 (setq res nil)))))
6420
6421 (when res
6422 ;; Skip trailing type modifiers. If any are found we know it's
6423 ;; a type.
6424 (when c-opt-type-modifier-key
6425 (while (looking-at c-opt-type-modifier-key) ; e.g. "const", "volatile"
6426 (goto-char (match-end 1))
6427 (c-forward-syntactic-ws)
6428 (setq res t)))
6429 ;; Step over any type suffix operator. Do not let the existence
6430 ;; of these alter the classification of the found type, since
6431 ;; these operators typically are allowed in normal expressions
6432 ;; too.
6433 (when c-opt-type-suffix-key
6434 (while (looking-at c-opt-type-suffix-key)
6435 (goto-char (match-end 1))
6436 (c-forward-syntactic-ws)))
6437
6438 (when c-opt-type-concat-key ; Only/mainly for pike.
6439 ;; Look for a trailing operator that concatenates the type
6440 ;; with a following one, and if so step past that one through
6441 ;; a recursive call. Note that we don't record concatenated
6442 ;; types in `c-found-types' - it's the component types that
6443 ;; are recorded when appropriate.
6444 (setq pos (point))
6445 (let* ((c-promote-possible-types (or (memq res '(t known))
6446 c-promote-possible-types))
6447 ;; If we can't promote then set `c-record-found-types' so that
6448 ;; we can merge in the types from the second part afterwards if
6449 ;; it turns out to be a known type there.
6450 (c-record-found-types (and c-record-type-identifiers
6451 (not c-promote-possible-types)))
6452 subres)
6453 (if (and (looking-at c-opt-type-concat-key)
6454
6455 (progn
6456 (goto-char (match-end 1))
6457 (c-forward-syntactic-ws)
6458 (setq subres (c-forward-type))))
6459
6460 (progn
6461 ;; If either operand certainly is a type then both are, but we
6462 ;; don't let the existence of the operator itself promote two
6463 ;; uncertain types to a certain one.
6464 (cond ((eq res t))
6465 ((eq subres t)
6466 (unless (eq name-res 'template)
6467 (c-add-type id-start id-end))
6468 (when (and c-record-type-identifiers id-range)
6469 (c-record-type-id id-range))
6470 (setq res t))
6471 ((eq res 'known))
6472 ((eq subres 'known)
6473 (setq res 'known))
6474 ((eq res 'found))
6475 ((eq subres 'found)
6476 (setq res 'found))
6477 (t
6478 (setq res 'maybe)))
6479
6480 (when (and (eq res t)
6481 (consp c-record-found-types))
6482 ;; Merge in the ranges of any types found by the second
6483 ;; `c-forward-type'.
6484 (setq c-record-type-identifiers
6485 ;; `nconc' doesn't mind that the tail of
6486 ;; `c-record-found-types' is t.
6487 (nconc c-record-found-types
6488 c-record-type-identifiers))))
6489
6490 (goto-char pos))))
6491
6492 (when (and c-record-found-types (memq res '(known found)) id-range)
6493 (setq c-record-found-types
6494 (cons id-range c-record-found-types))))
6495
6496 ;;(message "c-forward-type %s -> %s: %s" start (point) res)
6497
6498 res))
6499
6500 (defun c-forward-annotation ()
6501 ;; Used for Java code only at the moment. Assumes point is on the
6502 ;; @, moves forward an annotation. returns nil if there is no
6503 ;; annotation at point.
6504 (and (looking-at "@")
6505 (progn (forward-char) t)
6506 (c-forward-type)
6507 (progn (c-forward-syntactic-ws) t)
6508 (if (looking-at "(")
6509 (c-go-list-forward)
6510 t)))
6511
6512 \f
6513 ;; Handling of large scale constructs like statements and declarations.
6514
6515 ;; Macro used inside `c-forward-decl-or-cast-1'. It ought to be a
6516 ;; defsubst or perhaps even a defun, but it contains lots of free
6517 ;; variables that refer to things inside `c-forward-decl-or-cast-1'.
6518 (defmacro c-fdoc-shift-type-backward (&optional short)
6519 ;; `c-forward-decl-or-cast-1' can consume an arbitrary length list
6520 ;; of types when parsing a declaration, which means that it
6521 ;; sometimes consumes the identifier in the declaration as a type.
6522 ;; This is used to "backtrack" and make the last type be treated as
6523 ;; an identifier instead.
6524 `(progn
6525 ,(unless short
6526 ;; These identifiers are bound only in the inner let.
6527 '(setq identifier-type at-type
6528 identifier-start type-start
6529 got-parens nil
6530 got-identifier t
6531 got-suffix t
6532 got-suffix-after-parens id-start
6533 paren-depth 0))
6534
6535 (if (setq at-type (if (eq backup-at-type 'prefix)
6536 t
6537 backup-at-type))
6538 (setq type-start backup-type-start
6539 id-start backup-id-start)
6540 (setq type-start start-pos
6541 id-start start-pos))
6542
6543 ;; When these flags already are set we've found specifiers that
6544 ;; unconditionally signal these attributes - backtracking doesn't
6545 ;; change that. So keep them set in that case.
6546 (or at-type-decl
6547 (setq at-type-decl backup-at-type-decl))
6548 (or maybe-typeless
6549 (setq maybe-typeless backup-maybe-typeless))
6550
6551 ,(unless short
6552 ;; This identifier is bound only in the inner let.
6553 '(setq start id-start))))
6554
6555 (defun c-forward-decl-or-cast-1 (preceding-token-end context last-cast-end)
6556 ;; Move forward over a declaration or a cast if at the start of one.
6557 ;; The point is assumed to be at the start of some token. Nil is
6558 ;; returned if no declaration or cast is recognized, and the point
6559 ;; is clobbered in that case.
6560 ;;
6561 ;; If a declaration is parsed:
6562 ;;
6563 ;; The point is left at the first token after the first complete
6564 ;; declarator, if there is one. The return value is a cons where
6565 ;; the car is the position of the first token in the declarator. (See
6566 ;; below for the cdr.)
6567 ;; Some examples:
6568 ;;
6569 ;; void foo (int a, char *b) stuff ...
6570 ;; car ^ ^ point
6571 ;; float (*a)[], b;
6572 ;; car ^ ^ point
6573 ;; unsigned int a = c_style_initializer, b;
6574 ;; car ^ ^ point
6575 ;; unsigned int a (cplusplus_style_initializer), b;
6576 ;; car ^ ^ point (might change)
6577 ;; class Foo : public Bar {}
6578 ;; car ^ ^ point
6579 ;; class PikeClass (int a, string b) stuff ...
6580 ;; car ^ ^ point
6581 ;; enum bool;
6582 ;; car ^ ^ point
6583 ;; enum bool flag;
6584 ;; car ^ ^ point
6585 ;; void cplusplus_function (int x) throw (Bad);
6586 ;; car ^ ^ point
6587 ;; Foo::Foo (int b) : Base (b) {}
6588 ;; car ^ ^ point
6589 ;;
6590 ;; The cdr of the return value is non-nil when a
6591 ;; `c-typedef-decl-kwds' specifier is found in the declaration.
6592 ;; Specifically it is a dotted pair (A . B) where B is t when a
6593 ;; `c-typedef-kwds' ("typedef") is present, and A is t when some
6594 ;; other `c-typedef-decl-kwds' (e.g. class, struct, enum)
6595 ;; specifier is present. I.e., (some of) the declared
6596 ;; identifier(s) are types.
6597 ;;
6598 ;; If a cast is parsed:
6599 ;;
6600 ;; The point is left at the first token after the closing paren of
6601 ;; the cast. The return value is `cast'. Note that the start
6602 ;; position must be at the first token inside the cast parenthesis
6603 ;; to recognize it.
6604 ;;
6605 ;; PRECEDING-TOKEN-END is the first position after the preceding
6606 ;; token, i.e. on the other side of the syntactic ws from the point.
6607 ;; Use a value less than or equal to (point-min) if the point is at
6608 ;; the first token in (the visible part of) the buffer.
6609 ;;
6610 ;; CONTEXT is a symbol that describes the context at the point:
6611 ;; 'decl In a comma-separated declaration context (typically
6612 ;; inside a function declaration arglist).
6613 ;; '<> In an angle bracket arglist.
6614 ;; 'arglist Some other type of arglist.
6615 ;; nil Some other context or unknown context. Includes
6616 ;; within the parens of an if, for, ... construct.
6617 ;;
6618 ;; LAST-CAST-END is the first token after the closing paren of a
6619 ;; preceding cast, or nil if none is known. If
6620 ;; `c-forward-decl-or-cast-1' is used in succession, it should be
6621 ;; the position after the closest preceding call where a cast was
6622 ;; matched. In that case it's used to discover chains of casts like
6623 ;; "(a) (b) c".
6624 ;;
6625 ;; This function records identifier ranges on
6626 ;; `c-record-type-identifiers' and `c-record-ref-identifiers' if
6627 ;; `c-record-type-identifiers' is non-nil.
6628 ;;
6629 ;; This function might do hidden buffer changes.
6630
6631 (let (;; `start-pos' is used below to point to the start of the
6632 ;; first type, i.e. after any leading specifiers. It might
6633 ;; also point at the beginning of the preceding syntactic
6634 ;; whitespace.
6635 (start-pos (point))
6636 ;; Set to the result of `c-forward-type'.
6637 at-type
6638 ;; The position of the first token in what we currently
6639 ;; believe is the type in the declaration or cast, after any
6640 ;; specifiers and their associated clauses.
6641 type-start
6642 ;; The position of the first token in what we currently
6643 ;; believe is the declarator for the first identifier. Set
6644 ;; when the type is found, and moved forward over any
6645 ;; `c-decl-hangon-kwds' and their associated clauses that
6646 ;; occurs after the type.
6647 id-start
6648 ;; These store `at-type', `type-start' and `id-start' of the
6649 ;; identifier before the one in those variables. The previous
6650 ;; identifier might turn out to be the real type in a
6651 ;; declaration if the last one has to be the declarator in it.
6652 ;; If `backup-at-type' is nil then the other variables have
6653 ;; undefined values.
6654 backup-at-type backup-type-start backup-id-start
6655 ;; Set if we've found a specifier (apart from "typedef") that makes
6656 ;; the defined identifier(s) types.
6657 at-type-decl
6658 ;; Set if we've a "typedef" keyword.
6659 at-typedef
6660 ;; Set if we've found a specifier that can start a declaration
6661 ;; where there's no type.
6662 maybe-typeless
6663 ;; If a specifier is found that also can be a type prefix,
6664 ;; these flags are set instead of those above. If we need to
6665 ;; back up an identifier, they are copied to the real flag
6666 ;; variables. Thus they only take effect if we fail to
6667 ;; interpret it as a type.
6668 backup-at-type-decl backup-maybe-typeless
6669 ;; Whether we've found a declaration or a cast. We might know
6670 ;; this before we've found the type in it. It's 'ids if we've
6671 ;; found two consecutive identifiers (usually a sure sign, but
6672 ;; we should allow that in labels too), and t if we've found a
6673 ;; specifier keyword (a 100% sure sign).
6674 at-decl-or-cast
6675 ;; Set when we need to back up to parse this as a declaration
6676 ;; but not as a cast.
6677 backup-if-not-cast
6678 ;; For casts, the return position.
6679 cast-end
6680 ;; Save `c-record-type-identifiers' and
6681 ;; `c-record-ref-identifiers' since ranges are recorded
6682 ;; speculatively and should be thrown away if it turns out
6683 ;; that it isn't a declaration or cast.
6684 (save-rec-type-ids c-record-type-identifiers)
6685 (save-rec-ref-ids c-record-ref-identifiers))
6686
6687 (while (c-forward-annotation)
6688 (c-forward-syntactic-ws))
6689
6690 ;; Check for a type. Unknown symbols are treated as possible
6691 ;; types, but they could also be specifiers disguised through
6692 ;; macros like __INLINE__, so we recognize both types and known
6693 ;; specifiers after them too.
6694 (while
6695 (let* ((start (point)) kwd-sym kwd-clause-end found-type)
6696
6697 ;; Look for a specifier keyword clause.
6698 (when (or (looking-at c-prefix-spec-kwds-re)
6699 (and (c-major-mode-is 'java-mode)
6700 (looking-at "@[A-Za-z0-9]+")))
6701 (if (looking-at c-typedef-key)
6702 (setq at-typedef t))
6703 (setq kwd-sym (c-keyword-sym (match-string 1)))
6704 (save-excursion
6705 (c-forward-keyword-clause 1)
6706 (setq kwd-clause-end (point))))
6707
6708 (when (setq found-type (c-forward-type t)) ; brace-block-too
6709 ;; Found a known or possible type or a prefix of a known type.
6710
6711 (when at-type
6712 ;; Got two identifiers with nothing but whitespace
6713 ;; between them. That can only happen in declarations.
6714 (setq at-decl-or-cast 'ids)
6715
6716 (when (eq at-type 'found)
6717 ;; If the previous identifier is a found type we
6718 ;; record it as a real one; it might be some sort of
6719 ;; alias for a prefix like "unsigned".
6720 (save-excursion
6721 (goto-char type-start)
6722 (let ((c-promote-possible-types t))
6723 (c-forward-type)))))
6724
6725 (setq backup-at-type at-type
6726 backup-type-start type-start
6727 backup-id-start id-start
6728 at-type found-type
6729 type-start start
6730 id-start (point)
6731 ;; The previous ambiguous specifier/type turned out
6732 ;; to be a type since we've parsed another one after
6733 ;; it, so clear these backup flags.
6734 backup-at-type-decl nil
6735 backup-maybe-typeless nil))
6736
6737 (if kwd-sym
6738 (progn
6739 ;; Handle known specifier keywords and
6740 ;; `c-decl-hangon-kwds' which can occur after known
6741 ;; types.
6742
6743 (if (c-keyword-member kwd-sym 'c-decl-hangon-kwds)
6744 ;; It's a hang-on keyword that can occur anywhere.
6745 (progn
6746 (setq at-decl-or-cast t)
6747 (if at-type
6748 ;; Move the identifier start position if
6749 ;; we've passed a type.
6750 (setq id-start kwd-clause-end)
6751 ;; Otherwise treat this as a specifier and
6752 ;; move the fallback position.
6753 (setq start-pos kwd-clause-end))
6754 (goto-char kwd-clause-end))
6755
6756 ;; It's an ordinary specifier so we know that
6757 ;; anything before this can't be the type.
6758 (setq backup-at-type nil
6759 start-pos kwd-clause-end)
6760
6761 (if found-type
6762 ;; It's ambiguous whether this keyword is a
6763 ;; specifier or a type prefix, so set the backup
6764 ;; flags. (It's assumed that `c-forward-type'
6765 ;; moved further than `c-forward-keyword-clause'.)
6766 (progn
6767 (when (c-keyword-member kwd-sym 'c-typedef-decl-kwds)
6768 (setq backup-at-type-decl t))
6769 (when (c-keyword-member kwd-sym 'c-typeless-decl-kwds)
6770 (setq backup-maybe-typeless t)))
6771
6772 (when (c-keyword-member kwd-sym 'c-typedef-decl-kwds)
6773 ;; This test only happens after we've scanned a type.
6774 ;; So, with valid syntax, kwd-sym can't be 'typedef.
6775 (setq at-type-decl t))
6776 (when (c-keyword-member kwd-sym 'c-typeless-decl-kwds)
6777 (setq maybe-typeless t))
6778
6779 ;; Haven't matched a type so it's an unambiguous
6780 ;; specifier keyword and we know we're in a
6781 ;; declaration.
6782 (setq at-decl-or-cast t)
6783
6784 (goto-char kwd-clause-end))))
6785
6786 ;; If the type isn't known we continue so that we'll jump
6787 ;; over all specifiers and type identifiers. The reason
6788 ;; to do this for a known type prefix is to make things
6789 ;; like "unsigned INT16" work.
6790 (and found-type (not (eq found-type t))))))
6791
6792 (cond
6793 ((eq at-type t)
6794 ;; If a known type was found, we still need to skip over any
6795 ;; hangon keyword clauses after it. Otherwise it has already
6796 ;; been done in the loop above.
6797 (while (looking-at c-decl-hangon-key)
6798 (c-forward-keyword-clause 1))
6799 (setq id-start (point)))
6800
6801 ((eq at-type 'prefix)
6802 ;; A prefix type is itself a primitive type when it's not
6803 ;; followed by another type.
6804 (setq at-type t))
6805
6806 ((not at-type)
6807 ;; Got no type but set things up to continue anyway to handle
6808 ;; the various cases when a declaration doesn't start with a
6809 ;; type.
6810 (setq id-start start-pos))
6811
6812 ((and (eq at-type 'maybe)
6813 (c-major-mode-is 'c++-mode))
6814 ;; If it's C++ then check if the last "type" ends on the form
6815 ;; "foo::foo" or "foo::~foo", i.e. if it's the name of a
6816 ;; (con|de)structor.
6817 (save-excursion
6818 (let (name end-2 end-1)
6819 (goto-char id-start)
6820 (c-backward-syntactic-ws)
6821 (setq end-2 (point))
6822 (when (and
6823 (c-simple-skip-symbol-backward)
6824 (progn
6825 (setq name
6826 (buffer-substring-no-properties (point) end-2))
6827 ;; Cheating in the handling of syntactic ws below.
6828 (< (skip-chars-backward ":~ \t\n\r\v\f") 0))
6829 (progn
6830 (setq end-1 (point))
6831 (c-simple-skip-symbol-backward))
6832 (>= (point) type-start)
6833 (equal (buffer-substring-no-properties (point) end-1)
6834 name))
6835 ;; It is a (con|de)structor name. In that case the
6836 ;; declaration is typeless so zap out any preceding
6837 ;; identifier(s) that we might have taken as types.
6838 (goto-char type-start)
6839 (setq at-type nil
6840 backup-at-type nil
6841 id-start type-start))))))
6842
6843 ;; Check for and step over a type decl expression after the thing
6844 ;; that is or might be a type. This can't be skipped since we
6845 ;; need the correct end position of the declarator for
6846 ;; `max-type-decl-end-*'.
6847 (let ((start (point)) (paren-depth 0) pos
6848 ;; True if there's a non-open-paren match of
6849 ;; `c-type-decl-prefix-key'.
6850 got-prefix
6851 ;; True if the declarator is surrounded by a parenthesis pair.
6852 got-parens
6853 ;; True if there is an identifier in the declarator.
6854 got-identifier
6855 ;; True if there's a non-close-paren match of
6856 ;; `c-type-decl-suffix-key'.
6857 got-suffix
6858 ;; True if there's a prefix match outside the outermost
6859 ;; paren pair that surrounds the declarator.
6860 got-prefix-before-parens
6861 ;; True if there's a suffix match outside the outermost
6862 ;; paren pair that surrounds the declarator. The value is
6863 ;; the position of the first suffix match.
6864 got-suffix-after-parens
6865 ;; True if we've parsed the type decl to a token that is
6866 ;; known to end declarations in this context.
6867 at-decl-end
6868 ;; The earlier values of `at-type' and `type-start' if we've
6869 ;; shifted the type backwards.
6870 identifier-type identifier-start
6871 ;; If `c-parse-and-markup-<>-arglists' is set we need to
6872 ;; turn it off during the name skipping below to avoid
6873 ;; getting `c-type' properties that might be bogus. That
6874 ;; can happen since we don't know if
6875 ;; `c-restricted-<>-arglists' will be correct inside the
6876 ;; arglist paren that gets entered.
6877 c-parse-and-markup-<>-arglists)
6878
6879 (goto-char id-start)
6880
6881 ;; Skip over type decl prefix operators. (Note similar code in
6882 ;; `c-font-lock-declarators'.)
6883 (while (and (looking-at c-type-decl-prefix-key)
6884 (if (and (c-major-mode-is 'c++-mode)
6885 (match-beginning 3))
6886 ;; If the second submatch matches in C++ then
6887 ;; we're looking at an identifier that's a
6888 ;; prefix only if it specifies a member pointer.
6889 (when (setq got-identifier (c-forward-name))
6890 (if (looking-at "\\(::\\)")
6891 ;; We only check for a trailing "::" and
6892 ;; let the "*" that should follow be
6893 ;; matched in the next round.
6894 (progn (setq got-identifier nil) t)
6895 ;; It turned out to be the real identifier,
6896 ;; so stop.
6897 nil))
6898 t))
6899
6900 (if (eq (char-after) ?\()
6901 (progn
6902 (setq paren-depth (1+ paren-depth))
6903 (forward-char))
6904 (unless got-prefix-before-parens
6905 (setq got-prefix-before-parens (= paren-depth 0)))
6906 (setq got-prefix t)
6907 (goto-char (match-end 1)))
6908 (c-forward-syntactic-ws))
6909
6910 (setq got-parens (> paren-depth 0))
6911
6912 ;; Skip over an identifier.
6913 (or got-identifier
6914 (and (looking-at c-identifier-start)
6915 (setq got-identifier (c-forward-name))))
6916
6917 ;; Skip over type decl suffix operators.
6918 (while (if (looking-at c-type-decl-suffix-key)
6919
6920 (if (eq (char-after) ?\))
6921 (when (> paren-depth 0)
6922 (setq paren-depth (1- paren-depth))
6923 (forward-char)
6924 t)
6925 (when (if (save-match-data (looking-at "\\s\("))
6926 (c-safe (c-forward-sexp 1) t)
6927 (goto-char (match-end 1))
6928 t)
6929 (when (and (not got-suffix-after-parens)
6930 (= paren-depth 0))
6931 (setq got-suffix-after-parens (match-beginning 0)))
6932 (setq got-suffix t)))
6933
6934 ;; No suffix matched. We might have matched the
6935 ;; identifier as a type and the open paren of a
6936 ;; function arglist as a type decl prefix. In that
6937 ;; case we should "backtrack": Reinterpret the last
6938 ;; type as the identifier, move out of the arglist and
6939 ;; continue searching for suffix operators.
6940 ;;
6941 ;; Do this even if there's no preceding type, to cope
6942 ;; with old style function declarations in K&R C,
6943 ;; (con|de)structors in C++ and `c-typeless-decl-kwds'
6944 ;; style declarations. That isn't applicable in an
6945 ;; arglist context, though.
6946 (when (and (= paren-depth 1)
6947 (not got-prefix-before-parens)
6948 (not (eq at-type t))
6949 (or backup-at-type
6950 maybe-typeless
6951 backup-maybe-typeless
6952 (when c-recognize-typeless-decls
6953 (not context)))
6954 (setq pos (c-up-list-forward (point)))
6955 (eq (char-before pos) ?\)))
6956 (c-fdoc-shift-type-backward)
6957 (goto-char pos)
6958 t))
6959
6960 (c-forward-syntactic-ws))
6961
6962 (when (and (or maybe-typeless backup-maybe-typeless)
6963 (not got-identifier)
6964 (not got-prefix)
6965 at-type)
6966 ;; Have found no identifier but `c-typeless-decl-kwds' has
6967 ;; matched so we know we're inside a declaration. The
6968 ;; preceding type must be the identifier instead.
6969 (c-fdoc-shift-type-backward))
6970
6971 (setq
6972 at-decl-or-cast
6973 (catch 'at-decl-or-cast
6974
6975 ;; CASE 1
6976 (when (> paren-depth 0)
6977 ;; Encountered something inside parens that isn't matched by
6978 ;; the `c-type-decl-*' regexps, so it's not a type decl
6979 ;; expression. Try to skip out to the same paren depth to
6980 ;; not confuse the cast check below.
6981 (c-safe (goto-char (scan-lists (point) 1 paren-depth)))
6982 ;; If we've found a specifier keyword then it's a
6983 ;; declaration regardless.
6984 (throw 'at-decl-or-cast (eq at-decl-or-cast t)))
6985
6986 (setq at-decl-end
6987 (looking-at (cond ((eq context '<>) "[,>]")
6988 (context "[,\)]")
6989 (t "[,;]"))))
6990
6991 ;; Now we've collected info about various characteristics of
6992 ;; the construct we're looking at. Below follows a decision
6993 ;; tree based on that. It's ordered to check more certain
6994 ;; signs before less certain ones.
6995
6996 (if got-identifier
6997 (progn
6998
6999 ;; CASE 2
7000 (when (and (or at-type maybe-typeless)
7001 (not (or got-prefix got-parens)))
7002 ;; Got another identifier directly after the type, so it's a
7003 ;; declaration.
7004 (throw 'at-decl-or-cast t))
7005
7006 (when (and got-parens
7007 (not got-prefix)
7008 (not got-suffix-after-parens)
7009 (or backup-at-type
7010 maybe-typeless
7011 backup-maybe-typeless))
7012 ;; Got a declaration of the form "foo bar (gnu);" where we've
7013 ;; recognized "bar" as the type and "gnu" as the declarator.
7014 ;; In this case it's however more likely that "bar" is the
7015 ;; declarator and "gnu" a function argument or initializer (if
7016 ;; `c-recognize-paren-inits' is set), since the parens around
7017 ;; "gnu" would be superfluous if it's a declarator. Shift the
7018 ;; type one step backward.
7019 (c-fdoc-shift-type-backward)))
7020
7021 ;; Found no identifier.
7022
7023 (if backup-at-type
7024 (progn
7025
7026
7027 ;; CASE 3
7028 (when (= (point) start)
7029 ;; Got a plain list of identifiers. If a colon follows it's
7030 ;; a valid label, or maybe a bitfield. Otherwise the last
7031 ;; one probably is the declared identifier and we should
7032 ;; back up to the previous type, providing it isn't a cast.
7033 (if (and (eq (char-after) ?:)
7034 (not (c-major-mode-is 'java-mode)))
7035 (cond
7036 ;; If we've found a specifier keyword then it's a
7037 ;; declaration regardless.
7038 ((eq at-decl-or-cast t)
7039 (throw 'at-decl-or-cast t))
7040 ((and c-has-bitfields
7041 (eq at-decl-or-cast 'ids)) ; bitfield.
7042 (setq backup-if-not-cast t)
7043 (throw 'at-decl-or-cast t)))
7044
7045 (setq backup-if-not-cast t)
7046 (throw 'at-decl-or-cast t)))
7047
7048 ;; CASE 4
7049 (when (and got-suffix
7050 (not got-prefix)
7051 (not got-parens))
7052 ;; Got a plain list of identifiers followed by some suffix.
7053 ;; If this isn't a cast then the last identifier probably is
7054 ;; the declared one and we should back up to the previous
7055 ;; type.
7056 (setq backup-if-not-cast t)
7057 (throw 'at-decl-or-cast t)))
7058
7059 ;; CASE 5
7060 (when (eq at-type t)
7061 ;; If the type is known we know that there can't be any
7062 ;; identifier somewhere else, and it's only in declarations in
7063 ;; e.g. function prototypes and in casts that the identifier may
7064 ;; be left out.
7065 (throw 'at-decl-or-cast t))
7066
7067 (when (= (point) start)
7068 ;; Only got a single identifier (parsed as a type so far).
7069 ;; CASE 6
7070 (if (and
7071 ;; Check that the identifier isn't at the start of an
7072 ;; expression.
7073 at-decl-end
7074 (cond
7075 ((eq context 'decl)
7076 ;; Inside an arglist that contains declarations. If K&R
7077 ;; style declarations and parenthesis style initializers
7078 ;; aren't allowed then the single identifier must be a
7079 ;; type, else we require that it's known or found
7080 ;; (primitive types are handled above).
7081 (or (and (not c-recognize-knr-p)
7082 (not c-recognize-paren-inits))
7083 (memq at-type '(known found))))
7084 ((eq context '<>)
7085 ;; Inside a template arglist. Accept known and found
7086 ;; types; other identifiers could just as well be
7087 ;; constants in C++.
7088 (memq at-type '(known found)))))
7089 (throw 'at-decl-or-cast t)
7090 ;; CASE 7
7091 ;; Can't be a valid declaration or cast, but if we've found a
7092 ;; specifier it can't be anything else either, so treat it as
7093 ;; an invalid/unfinished declaration or cast.
7094 (throw 'at-decl-or-cast at-decl-or-cast))))
7095
7096 (if (and got-parens
7097 (not got-prefix)
7098 (not context)
7099 (not (eq at-type t))
7100 (or backup-at-type
7101 maybe-typeless
7102 backup-maybe-typeless
7103 (when c-recognize-typeless-decls
7104 (or (not got-suffix)
7105 (not (looking-at
7106 c-after-suffixed-type-maybe-decl-key))))))
7107 ;; Got an empty paren pair and a preceding type that probably
7108 ;; really is the identifier. Shift the type backwards to make
7109 ;; the last one the identifier. This is analogous to the
7110 ;; "backtracking" done inside the `c-type-decl-suffix-key' loop
7111 ;; above.
7112 ;;
7113 ;; Exception: In addition to the conditions in that
7114 ;; "backtracking" code, do not shift backward if we're not
7115 ;; looking at either `c-after-suffixed-type-decl-key' or "[;,]".
7116 ;; Since there's no preceding type, the shift would mean that
7117 ;; the declaration is typeless. But if the regexp doesn't match
7118 ;; then we will simply fall through in the tests below and not
7119 ;; recognize it at all, so it's better to try it as an abstract
7120 ;; declarator instead.
7121 (c-fdoc-shift-type-backward)
7122
7123 ;; Still no identifier.
7124 ;; CASE 8
7125 (when (and got-prefix (or got-parens got-suffix))
7126 ;; Require `got-prefix' together with either `got-parens' or
7127 ;; `got-suffix' to recognize it as an abstract declarator:
7128 ;; `got-parens' only is probably an empty function call.
7129 ;; `got-suffix' only can build an ordinary expression together
7130 ;; with the preceding identifier which we've taken as a type.
7131 ;; We could actually accept on `got-prefix' only, but that can
7132 ;; easily occur temporarily while writing an expression so we
7133 ;; avoid that case anyway. We could do a better job if we knew
7134 ;; the point when the fontification was invoked.
7135 (throw 'at-decl-or-cast t))
7136
7137 ;; CASE 9
7138 (when (and at-type
7139 (not got-prefix)
7140 (not got-parens)
7141 got-suffix-after-parens
7142 (eq (char-after got-suffix-after-parens) ?\())
7143 ;; Got a type, no declarator but a paren suffix. I.e. it's a
7144 ;; normal function call after all (or perhaps a C++ style object
7145 ;; instantiation expression).
7146 (throw 'at-decl-or-cast nil))))
7147
7148 ;; CASE 10
7149 (when at-decl-or-cast
7150 ;; By now we've located the type in the declaration that we know
7151 ;; we're in.
7152 (throw 'at-decl-or-cast t))
7153
7154 ;; CASE 11
7155 (when (and got-identifier
7156 (not context)
7157 (looking-at c-after-suffixed-type-decl-key)
7158 (if (and got-parens
7159 (not got-prefix)
7160 (not got-suffix)
7161 (not (eq at-type t)))
7162 ;; Shift the type backward in the case that there's a
7163 ;; single identifier inside parens. That can only
7164 ;; occur in K&R style function declarations so it's
7165 ;; more likely that it really is a function call.
7166 ;; Therefore we only do this after
7167 ;; `c-after-suffixed-type-decl-key' has matched.
7168 (progn (c-fdoc-shift-type-backward) t)
7169 got-suffix-after-parens))
7170 ;; A declaration according to `c-after-suffixed-type-decl-key'.
7171 (throw 'at-decl-or-cast t))
7172
7173 ;; CASE 12
7174 (when (and (or got-prefix (not got-parens))
7175 (memq at-type '(t known)))
7176 ;; It's a declaration if a known type precedes it and it can't be a
7177 ;; function call.
7178 (throw 'at-decl-or-cast t))
7179
7180 ;; If we get here we can't tell if this is a type decl or a normal
7181 ;; expression by looking at it alone. (That's under the assumption
7182 ;; that normal expressions always can look like type decl expressions,
7183 ;; which isn't really true but the cases where it doesn't hold are so
7184 ;; uncommon (e.g. some placements of "const" in C++) it's not worth
7185 ;; the effort to look for them.)
7186
7187 (unless (or at-decl-end (looking-at "=[^=]"))
7188 ;; If this is a declaration it should end here or its initializer(*)
7189 ;; should start here, so check for allowed separation tokens. Note
7190 ;; that this rule doesn't work e.g. with a K&R arglist after a
7191 ;; function header.
7192 ;;
7193 ;; *) Don't check for C++ style initializers using parens
7194 ;; since those already have been matched as suffixes.
7195 ;;
7196 ;; If `at-decl-or-cast' is then we've found some other sign that
7197 ;; it's a declaration or cast, so then it's probably an
7198 ;; invalid/unfinished one.
7199 (throw 'at-decl-or-cast at-decl-or-cast))
7200
7201 ;; Below are tests that only should be applied when we're certain to
7202 ;; not have parsed halfway through an expression.
7203
7204 ;; CASE 14
7205 (when (memq at-type '(t known))
7206 ;; The expression starts with a known type so treat it as a
7207 ;; declaration.
7208 (throw 'at-decl-or-cast t))
7209
7210 ;; CASE 15
7211 (when (and (c-major-mode-is 'c++-mode)
7212 ;; In C++ we check if the identifier is a known type, since
7213 ;; (con|de)structors use the class name as identifier.
7214 ;; We've always shifted over the identifier as a type and
7215 ;; then backed up again in this case.
7216 identifier-type
7217 (or (memq identifier-type '(found known))
7218 (and (eq (char-after identifier-start) ?~)
7219 ;; `at-type' probably won't be 'found for
7220 ;; destructors since the "~" is then part of the
7221 ;; type name being checked against the list of
7222 ;; known types, so do a check without that
7223 ;; operator.
7224 (or (save-excursion
7225 (goto-char (1+ identifier-start))
7226 (c-forward-syntactic-ws)
7227 (c-with-syntax-table
7228 c-identifier-syntax-table
7229 (looking-at c-known-type-key)))
7230 (save-excursion
7231 (goto-char (1+ identifier-start))
7232 ;; We have already parsed the type earlier,
7233 ;; so it'd be possible to cache the end
7234 ;; position instead of redoing it here, but
7235 ;; then we'd need to keep track of another
7236 ;; position everywhere.
7237 (c-check-type (point)
7238 (progn (c-forward-type)
7239 (point))))))))
7240 (throw 'at-decl-or-cast t))
7241
7242 (if got-identifier
7243 (progn
7244 ;; CASE 16
7245 (when (and got-prefix-before-parens
7246 at-type
7247 (or at-decl-end (looking-at "=[^=]"))
7248 (not context)
7249 (not got-suffix))
7250 ;; Got something like "foo * bar;". Since we're not inside an
7251 ;; arglist it would be a meaningless expression because the
7252 ;; result isn't used. We therefore choose to recognize it as
7253 ;; a declaration. Do not allow a suffix since it could then
7254 ;; be a function call.
7255 (throw 'at-decl-or-cast t))
7256
7257 ;; CASE 17
7258 (when (and (or got-suffix-after-parens
7259 (looking-at "=[^=]"))
7260 (eq at-type 'found)
7261 (not (eq context 'arglist)))
7262 ;; Got something like "a (*b) (c);" or "a (b) = c;". It could
7263 ;; be an odd expression or it could be a declaration. Treat
7264 ;; it as a declaration if "a" has been used as a type
7265 ;; somewhere else (if it's a known type we won't get here).
7266 (throw 'at-decl-or-cast t)))
7267
7268 ;; CASE 18
7269 (when (and context
7270 (or got-prefix
7271 (and (eq context 'decl)
7272 (not c-recognize-paren-inits)
7273 (or got-parens got-suffix))))
7274 ;; Got a type followed by an abstract declarator. If `got-prefix'
7275 ;; is set it's something like "a *" without anything after it. If
7276 ;; `got-parens' or `got-suffix' is set it's "a()", "a[]", "a()[]",
7277 ;; or similar, which we accept only if the context rules out
7278 ;; expressions.
7279 (throw 'at-decl-or-cast t)))
7280
7281 ;; If we had a complete symbol table here (which rules out
7282 ;; `c-found-types') we should return t due to the disambiguation rule
7283 ;; (in at least C++) that anything that can be parsed as a declaration
7284 ;; is a declaration. Now we're being more defensive and prefer to
7285 ;; highlight things like "foo (bar);" as a declaration only if we're
7286 ;; inside an arglist that contains declarations.
7287 (eq context 'decl))))
7288
7289 ;; The point is now after the type decl expression.
7290
7291 (cond
7292 ;; Check for a cast.
7293 ((save-excursion
7294 (and
7295 c-cast-parens
7296
7297 ;; Should be the first type/identifier in a cast paren.
7298 (> preceding-token-end (point-min))
7299 (memq (char-before preceding-token-end) c-cast-parens)
7300
7301 ;; The closing paren should follow.
7302 (progn
7303 (c-forward-syntactic-ws)
7304 (looking-at "\\s\)"))
7305
7306 ;; There should be a primary expression after it.
7307 (let (pos)
7308 (forward-char)
7309 (c-forward-syntactic-ws)
7310 (setq cast-end (point))
7311 (and (looking-at c-primary-expr-regexp)
7312 (progn
7313 (setq pos (match-end 0))
7314 (or
7315 ;; Check if the expression begins with a prefix keyword.
7316 (match-beginning 2)
7317 (if (match-beginning 1)
7318 ;; Expression begins with an ambiguous operator. Treat
7319 ;; it as a cast if it's a type decl or if we've
7320 ;; recognized the type somewhere else.
7321 (or at-decl-or-cast
7322 (memq at-type '(t known found)))
7323 ;; Unless it's a keyword, it's the beginning of a primary
7324 ;; expression.
7325 (not (looking-at c-keywords-regexp)))))
7326 ;; If `c-primary-expr-regexp' matched a nonsymbol token, check
7327 ;; that it matched a whole one so that we don't e.g. confuse
7328 ;; the operator '-' with '->'. It's ok if it matches further,
7329 ;; though, since it e.g. can match the float '.5' while the
7330 ;; operator regexp only matches '.'.
7331 (or (not (looking-at c-nonsymbol-token-regexp))
7332 (<= (match-end 0) pos))))
7333
7334 ;; There should either be a cast before it or something that isn't an
7335 ;; identifier or close paren.
7336 (> preceding-token-end (point-min))
7337 (progn
7338 (goto-char (1- preceding-token-end))
7339 (or (eq (point) last-cast-end)
7340 (progn
7341 (c-backward-syntactic-ws)
7342 (if (< (skip-syntax-backward "w_") 0)
7343 ;; It's a symbol. Accept it only if it's one of the
7344 ;; keywords that can precede an expression (without
7345 ;; surrounding parens).
7346 (looking-at c-simple-stmt-key)
7347 (and
7348 ;; Check that it isn't a close paren (block close is ok,
7349 ;; though).
7350 (not (memq (char-before) '(?\) ?\])))
7351 ;; Check that it isn't a nonsymbol identifier.
7352 (not (c-on-identifier)))))))))
7353
7354 ;; Handle the cast.
7355 (when (and c-record-type-identifiers at-type (not (eq at-type t)))
7356 (let ((c-promote-possible-types t))
7357 (goto-char type-start)
7358 (c-forward-type)))
7359
7360 (goto-char cast-end)
7361 'cast)
7362
7363 (at-decl-or-cast
7364 ;; We're at a declaration. Highlight the type and the following
7365 ;; declarators.
7366
7367 (when backup-if-not-cast
7368 (c-fdoc-shift-type-backward t))
7369
7370 (when (and (eq context 'decl) (looking-at ","))
7371 ;; Make sure to propagate the `c-decl-arg-start' property to
7372 ;; the next argument if it's set in this one, to cope with
7373 ;; interactive refontification.
7374 (c-put-c-type-property (point) 'c-decl-arg-start))
7375
7376 (when (and c-record-type-identifiers at-type (not (eq at-type t)))
7377 (let ((c-promote-possible-types t))
7378 (save-excursion
7379 (goto-char type-start)
7380 (c-forward-type))))
7381
7382 (cons id-start
7383 (and (or at-type-decl at-typedef)
7384 (cons at-type-decl at-typedef))))
7385
7386 (t
7387 ;; False alarm. Restore the recorded ranges.
7388 (setq c-record-type-identifiers save-rec-type-ids
7389 c-record-ref-identifiers save-rec-ref-ids)
7390 nil))))
7391
7392 (defun c-forward-label (&optional assume-markup preceding-token-end limit)
7393 ;; Assuming that point is at the beginning of a token, check if it starts a
7394 ;; label and if so move over it and return non-nil (t in default situations,
7395 ;; specific symbols (see below) for interesting situations), otherwise don't
7396 ;; move and return nil. "Label" here means "most things with a colon".
7397 ;;
7398 ;; More precisely, a "label" is regarded as one of:
7399 ;; (i) a goto target like "foo:" - returns the symbol `goto-target';
7400 ;; (ii) A case label - either the entire construct "case FOO:", or just the
7401 ;; bare "case", should the colon be missing. We return t;
7402 ;; (iii) a keyword which needs a colon, like "default:" or "private:"; We
7403 ;; return t;
7404 ;; (iv) One of QT's "extended" C++ variants of
7405 ;; "private:"/"protected:"/"public:"/"more:" looking like "public slots:".
7406 ;; Returns the symbol `qt-2kwds-colon'.
7407 ;; (v) QT's construct "signals:". Returns the symbol `qt-1kwd-colon'.
7408 ;; (vi) One of the keywords matched by `c-opt-extra-label-key' (without any
7409 ;; colon). Currently (2006-03), this applies only to Objective C's
7410 ;; keywords "@private", "@protected", and "@public". Returns t.
7411 ;;
7412 ;; One of the things which will NOT be recognized as a label is a bit-field
7413 ;; element of a struct, something like "int foo:5".
7414 ;;
7415 ;; The end of the label is taken to be just after the colon, or the end of
7416 ;; the first submatch in `c-opt-extra-label-key'. The point is directly
7417 ;; after the end on return. The terminating char gets marked with
7418 ;; `c-decl-end' to improve recognition of the following declaration or
7419 ;; statement.
7420 ;;
7421 ;; If ASSUME-MARKUP is non-nil, it's assumed that the preceding
7422 ;; label, if any, has already been marked up like that.
7423 ;;
7424 ;; If PRECEDING-TOKEN-END is given, it should be the first position
7425 ;; after the preceding token, i.e. on the other side of the
7426 ;; syntactic ws from the point. Use a value less than or equal to
7427 ;; (point-min) if the point is at the first token in (the visible
7428 ;; part of) the buffer.
7429 ;;
7430 ;; The optional LIMIT limits the forward scan for the colon.
7431 ;;
7432 ;; This function records the ranges of the label symbols on
7433 ;; `c-record-ref-identifiers' if `c-record-type-identifiers' (!) is
7434 ;; non-nil.
7435 ;;
7436 ;; This function might do hidden buffer changes.
7437
7438 (let ((start (point))
7439 label-end
7440 qt-symbol-idx
7441 macro-start ; if we're in one.
7442 label-type
7443 kwd)
7444 (cond
7445 ;; "case" or "default" (Doesn't apply to AWK).
7446 ((looking-at c-label-kwds-regexp)
7447 (let ((kwd-end (match-end 1)))
7448 ;; Record only the keyword itself for fontification, since in
7449 ;; case labels the following is a constant expression and not
7450 ;; a label.
7451 (when c-record-type-identifiers
7452 (c-record-ref-id (cons (match-beginning 1) kwd-end)))
7453
7454 ;; Find the label end.
7455 (goto-char kwd-end)
7456 (setq label-type
7457 (if (and (c-syntactic-re-search-forward
7458 ;; Stop on chars that aren't allowed in expressions,
7459 ;; and on operator chars that would be meaningless
7460 ;; there. FIXME: This doesn't cope with ?: operators.
7461 "[;{=,@]\\|\\(\\=\\|[^:]\\):\\([^:]\\|\\'\\)"
7462 limit t t nil 1)
7463 (match-beginning 2))
7464
7465 (progn ; there's a proper :
7466 (goto-char (match-beginning 2)) ; just after the :
7467 (c-put-c-type-property (1- (point)) 'c-decl-end)
7468 t)
7469
7470 ;; It's an unfinished label. We consider the keyword enough
7471 ;; to recognize it as a label, so that it gets fontified.
7472 ;; Leave the point at the end of it, but don't put any
7473 ;; `c-decl-end' marker.
7474 (goto-char kwd-end)
7475 t))))
7476
7477 ;; @private, @protected, @public, in Objective C, or similar.
7478 ((and c-opt-extra-label-key
7479 (looking-at c-opt-extra-label-key))
7480 ;; For a `c-opt-extra-label-key' match, we record the whole
7481 ;; thing for fontification. That's to get the leading '@' in
7482 ;; Objective-C protection labels fontified.
7483 (goto-char (match-end 1))
7484 (when c-record-type-identifiers
7485 (c-record-ref-id (cons (match-beginning 1) (point))))
7486 (c-put-c-type-property (1- (point)) 'c-decl-end)
7487 (setq label-type t))
7488
7489 ;; All other cases of labels.
7490 ((and c-recognize-colon-labels ; nil for AWK and IDL, otherwise t.
7491
7492 ;; A colon label must have something before the colon.
7493 (not (eq (char-after) ?:))
7494
7495 ;; Check that we're not after a token that can't precede a label.
7496 (or
7497 ;; Trivially succeeds when there's no preceding token.
7498 ;; Succeeds when we're at a virtual semicolon.
7499 (if preceding-token-end
7500 (<= preceding-token-end (point-min))
7501 (save-excursion
7502 (c-backward-syntactic-ws)
7503 (setq preceding-token-end (point))
7504 (or (bobp)
7505 (c-at-vsemi-p))))
7506
7507 ;; Check if we're after a label, if we're after a closing
7508 ;; paren that belong to statement, and with
7509 ;; `c-label-prefix-re'. It's done in different order
7510 ;; depending on `assume-markup' since the checks have
7511 ;; different expensiveness.
7512 (if assume-markup
7513 (or
7514 (eq (c-get-char-property (1- preceding-token-end) 'c-type)
7515 'c-decl-end)
7516
7517 (save-excursion
7518 (goto-char (1- preceding-token-end))
7519 (c-beginning-of-current-token)
7520 (or (looking-at c-label-prefix-re)
7521 (looking-at c-block-stmt-1-key)))
7522
7523 (and (eq (char-before preceding-token-end) ?\))
7524 (c-after-conditional)))
7525
7526 (or
7527 (save-excursion
7528 (goto-char (1- preceding-token-end))
7529 (c-beginning-of-current-token)
7530 (or (looking-at c-label-prefix-re)
7531 (looking-at c-block-stmt-1-key)))
7532
7533 (cond
7534 ((eq (char-before preceding-token-end) ?\))
7535 (c-after-conditional))
7536
7537 ((eq (char-before preceding-token-end) ?:)
7538 ;; Might be after another label, so check it recursively.
7539 (save-restriction
7540 (save-excursion
7541 (goto-char (1- preceding-token-end))
7542 ;; Essentially the same as the
7543 ;; `c-syntactic-re-search-forward' regexp below.
7544 (setq macro-start
7545 (save-excursion (and (c-beginning-of-macro)
7546 (point))))
7547 (if macro-start (narrow-to-region macro-start (point-max)))
7548 (c-syntactic-skip-backward "^-]:?;}=*/%&|,<>!@+" nil t)
7549 ;; Note: the following should work instead of the
7550 ;; narrow-to-region above. Investigate why not,
7551 ;; sometime. ACM, 2006-03-31.
7552 ;; (c-syntactic-skip-backward "^-]:?;}=*/%&|,<>!@+"
7553 ;; macro-start t)
7554 (let ((pte (point))
7555 ;; If the caller turned on recording for us,
7556 ;; it shouldn't apply when we check the
7557 ;; preceding label.
7558 c-record-type-identifiers)
7559 ;; A label can't start at a cpp directive. Check for
7560 ;; this, since c-forward-syntactic-ws would foul up on it.
7561 (unless (and c-opt-cpp-prefix (looking-at c-opt-cpp-prefix))
7562 (c-forward-syntactic-ws)
7563 (c-forward-label nil pte start))))))))))
7564
7565 ;; Point is still at the beginning of the possible label construct.
7566 ;;
7567 ;; Check that the next nonsymbol token is ":", or that we're in one
7568 ;; of QT's "slots" declarations. Allow '(' for the sake of macro
7569 ;; arguments. FIXME: Should build this regexp from the language
7570 ;; constants.
7571 (cond
7572 ;; public: protected: private:
7573 ((and
7574 (c-major-mode-is 'c++-mode)
7575 (search-forward-regexp
7576 "\\=p\\(r\\(ivate\\|otected\\)\\|ublic\\)\\>[^_]" nil t)
7577 (progn (backward-char)
7578 (c-forward-syntactic-ws limit)
7579 (looking-at ":\\([^:]\\|\\'\\)"))) ; A single colon.
7580 (forward-char)
7581 (setq label-type t))
7582 ;; QT double keyword like "protected slots:" or goto target.
7583 ((progn (goto-char start) nil))
7584 ((when (c-syntactic-re-search-forward
7585 "[ \t\n[:?;{=*/%&|,<>!@+-]" limit t t) ; not at EOB
7586 (backward-char)
7587 (setq label-end (point))
7588 (setq qt-symbol-idx
7589 (and (c-major-mode-is 'c++-mode)
7590 (string-match
7591 "\\(p\\(r\\(ivate\\|otected\\)\\|ublic\\)\\|more\\)\\>"
7592 (buffer-substring start (point)))))
7593 (c-forward-syntactic-ws limit)
7594 (cond
7595 ((looking-at ":\\([^:]\\|\\'\\)") ; A single colon.
7596 (forward-char)
7597 (setq label-type
7598 (if (or (string= "signals" ; Special QT macro
7599 (setq kwd (buffer-substring-no-properties start label-end)))
7600 (string= "Q_SIGNALS" kwd))
7601 'qt-1kwd-colon
7602 'goto-target)))
7603 ((and qt-symbol-idx
7604 (search-forward-regexp "\\=\\(slots\\|Q_SLOTS\\)\\>" limit t)
7605 (progn (c-forward-syntactic-ws limit)
7606 (looking-at ":\\([^:]\\|\\'\\)"))) ; A single colon
7607 (forward-char)
7608 (setq label-type 'qt-2kwds-colon)))))))
7609
7610 (save-restriction
7611 (narrow-to-region start (point))
7612
7613 ;; Check that `c-nonlabel-token-key' doesn't match anywhere.
7614 (catch 'check-label
7615 (goto-char start)
7616 (while (progn
7617 (when (looking-at c-nonlabel-token-key)
7618 (goto-char start)
7619 (setq label-type nil)
7620 (throw 'check-label nil))
7621 (and (c-safe (c-forward-sexp)
7622 (c-forward-syntactic-ws)
7623 t)
7624 (not (eobp)))))
7625
7626 ;; Record the identifiers in the label for fontification, unless
7627 ;; it begins with `c-label-kwds' in which case the following
7628 ;; identifiers are part of a (constant) expression that
7629 ;; shouldn't be fontified.
7630 (when (and c-record-type-identifiers
7631 (progn (goto-char start)
7632 (not (looking-at c-label-kwds-regexp))))
7633 (while (c-syntactic-re-search-forward c-symbol-key nil t)
7634 (c-record-ref-id (cons (match-beginning 0)
7635 (match-end 0)))))
7636
7637 (c-put-c-type-property (1- (point-max)) 'c-decl-end)
7638 (goto-char (point-max)))))
7639
7640 (t
7641 ;; Not a label.
7642 (goto-char start)))
7643 label-type))
7644
7645 (defun c-forward-objc-directive ()
7646 ;; Assuming the point is at the beginning of a token, try to move
7647 ;; forward to the end of the Objective-C directive that starts
7648 ;; there. Return t if a directive was fully recognized, otherwise
7649 ;; the point is moved as far as one could be successfully parsed and
7650 ;; nil is returned.
7651 ;;
7652 ;; This function records identifier ranges on
7653 ;; `c-record-type-identifiers' and `c-record-ref-identifiers' if
7654 ;; `c-record-type-identifiers' is non-nil.
7655 ;;
7656 ;; This function might do hidden buffer changes.
7657
7658 (let ((start (point))
7659 start-char
7660 (c-promote-possible-types t)
7661 lim
7662 ;; Turn off recognition of angle bracket arglists while parsing
7663 ;; types here since the protocol reference list might then be
7664 ;; considered part of the preceding name or superclass-name.
7665 c-recognize-<>-arglists)
7666
7667 (if (or
7668 (when (looking-at
7669 (eval-when-compile
7670 (c-make-keywords-re t
7671 (append (c-lang-const c-protection-kwds objc)
7672 '("@end"))
7673 'objc-mode)))
7674 (goto-char (match-end 1))
7675 t)
7676
7677 (and
7678 (looking-at
7679 (eval-when-compile
7680 (c-make-keywords-re t
7681 '("@interface" "@implementation" "@protocol")
7682 'objc-mode)))
7683
7684 ;; Handle the name of the class itself.
7685 (progn
7686 ; (c-forward-token-2) ; 2006/1/13 This doesn't move if the token's
7687 ; at EOB.
7688 (goto-char (match-end 0))
7689 (setq lim (point))
7690 (c-skip-ws-forward)
7691 (c-forward-type))
7692
7693 (catch 'break
7694 ;; Look for ": superclass-name" or "( category-name )".
7695 (when (looking-at "[:\(]")
7696 (setq start-char (char-after))
7697 (forward-char)
7698 (c-forward-syntactic-ws)
7699 (unless (c-forward-type) (throw 'break nil))
7700 (when (eq start-char ?\()
7701 (unless (eq (char-after) ?\)) (throw 'break nil))
7702 (forward-char)
7703 (c-forward-syntactic-ws)))
7704
7705 ;; Look for a protocol reference list.
7706 (if (eq (char-after) ?<)
7707 (let ((c-recognize-<>-arglists t)
7708 (c-parse-and-markup-<>-arglists t)
7709 c-restricted-<>-arglists)
7710 (c-forward-<>-arglist t))
7711 t))))
7712
7713 (progn
7714 (c-backward-syntactic-ws lim)
7715 (c-clear-c-type-property start (1- (point)) 'c-decl-end)
7716 (c-put-c-type-property (1- (point)) 'c-decl-end)
7717 t)
7718
7719 (c-clear-c-type-property start (point) 'c-decl-end)
7720 nil)))
7721
7722 (defun c-beginning-of-inheritance-list (&optional lim)
7723 ;; Go to the first non-whitespace after the colon that starts a
7724 ;; multiple inheritance introduction. Optional LIM is the farthest
7725 ;; back we should search.
7726 ;;
7727 ;; This function might do hidden buffer changes.
7728 (c-with-syntax-table c++-template-syntax-table
7729 (c-backward-token-2 0 t lim)
7730 (while (and (or (looking-at c-symbol-start)
7731 (looking-at "[<,]\\|::"))
7732 (zerop (c-backward-token-2 1 t lim))))))
7733
7734 (defun c-in-method-def-p ()
7735 ;; Return nil if we aren't in a method definition, otherwise the
7736 ;; position of the initial [+-].
7737 ;;
7738 ;; This function might do hidden buffer changes.
7739 (save-excursion
7740 (beginning-of-line)
7741 (and c-opt-method-key
7742 (looking-at c-opt-method-key)
7743 (point))
7744 ))
7745
7746 ;; Contributed by Kevin Ryde <user42@zip.com.au>.
7747 (defun c-in-gcc-asm-p ()
7748 ;; Return non-nil if point is within a gcc \"asm\" block.
7749 ;;
7750 ;; This should be called with point inside an argument list.
7751 ;;
7752 ;; Only one level of enclosing parentheses is considered, so for
7753 ;; instance `nil' is returned when in a function call within an asm
7754 ;; operand.
7755 ;;
7756 ;; This function might do hidden buffer changes.
7757
7758 (and c-opt-asm-stmt-key
7759 (save-excursion
7760 (beginning-of-line)
7761 (backward-up-list 1)
7762 (c-beginning-of-statement-1 (point-min) nil t)
7763 (looking-at c-opt-asm-stmt-key))))
7764
7765 (defun c-at-toplevel-p ()
7766 "Return a determination as to whether point is \"at the top level\".
7767 Informally, \"at the top level\" is anywhere where you can write
7768 a function.
7769
7770 More precisely, being at the top-level means that point is either
7771 outside any enclosing block (such as a function definition), or
7772 directly inside a class, namespace or other block that contains
7773 another declaration level.
7774
7775 If point is not at the top-level (e.g. it is inside a method
7776 definition), then nil is returned. Otherwise, if point is at a
7777 top-level not enclosed within a class definition, t is returned.
7778 Otherwise, a 2-vector is returned where the zeroth element is the
7779 buffer position of the start of the class declaration, and the first
7780 element is the buffer position of the enclosing class's opening
7781 brace.
7782
7783 Note that this function might do hidden buffer changes. See the
7784 comment at the start of cc-engine.el for more info."
7785 (let ((paren-state (c-parse-state)))
7786 (or (not (c-most-enclosing-brace paren-state))
7787 (c-search-uplist-for-classkey paren-state))))
7788
7789 (defun c-just-after-func-arglist-p (&optional lim)
7790 ;; Return non-nil if the point is in the region after the argument
7791 ;; list of a function and its opening brace (or semicolon in case it
7792 ;; got no body). If there are K&R style argument declarations in
7793 ;; that region, the point has to be inside the first one for this
7794 ;; function to recognize it.
7795 ;;
7796 ;; If successful, the point is moved to the first token after the
7797 ;; function header (see `c-forward-decl-or-cast-1' for details) and
7798 ;; the position of the opening paren of the function arglist is
7799 ;; returned.
7800 ;;
7801 ;; The point is clobbered if not successful.
7802 ;;
7803 ;; LIM is used as bound for backward buffer searches.
7804 ;;
7805 ;; This function might do hidden buffer changes.
7806
7807 (let ((beg (point)) end id-start)
7808 (and
7809 (eq (c-beginning-of-statement-1 lim) 'same)
7810
7811 (not (and (c-major-mode-is 'objc-mode)
7812 (c-forward-objc-directive)))
7813
7814 (setq id-start
7815 (car-safe (c-forward-decl-or-cast-1 (c-point 'bosws) nil nil)))
7816 (< id-start beg)
7817
7818 ;; There should not be a '=' or ',' between beg and the
7819 ;; start of the declaration since that means we were in the
7820 ;; "expression part" of the declaration.
7821 (or (> (point) beg)
7822 (not (looking-at "[=,]")))
7823
7824 (save-excursion
7825 ;; Check that there's an arglist paren in the
7826 ;; declaration.
7827 (goto-char id-start)
7828 (cond ((eq (char-after) ?\()
7829 ;; The declarator is a paren expression, so skip past it
7830 ;; so that we don't get stuck on that instead of the
7831 ;; function arglist.
7832 (c-forward-sexp))
7833 ((and c-opt-op-identifier-prefix
7834 (looking-at c-opt-op-identifier-prefix))
7835 ;; Don't trip up on "operator ()".
7836 (c-forward-token-2 2 t)))
7837 (and (< (point) beg)
7838 (c-syntactic-re-search-forward "(" beg t t)
7839 (1- (point)))))))
7840
7841 (defun c-in-knr-argdecl (&optional lim)
7842 ;; Return the position of the first argument declaration if point is
7843 ;; inside a K&R style argument declaration list, nil otherwise.
7844 ;; `c-recognize-knr-p' is not checked. If LIM is non-nil, it's a
7845 ;; position that bounds the backward search for the argument list.
7846 ;;
7847 ;; Point must be within a possible K&R region, e.g. just before a top-level
7848 ;; "{". It must be outside of parens and brackets. The test can return
7849 ;; false positives otherwise.
7850 ;;
7851 ;; This function might do hidden buffer changes.
7852
7853 (save-excursion
7854 (save-restriction
7855 ;; If we're in a macro, our search range is restricted to it. Narrow to
7856 ;; the searchable range.
7857 (let* ((macro-start (save-excursion (and (c-beginning-of-macro) (point))))
7858 (macro-end (save-excursion (and macro-start (c-end-of-macro) (point))))
7859 (low-lim (max (or lim (point-min)) (or macro-start (point-min))))
7860 before-lparen after-rparen
7861 (pp-count-out 20)) ; Max number of paren/brace constructs before
7862 ; we give up
7863 (narrow-to-region low-lim (or macro-end (point-max)))
7864
7865 ;; Search backwards for the defun's argument list. We give up if we
7866 ;; encounter a "}" (end of a previous defun) an "=" (which can't be in
7867 ;; a knr region) or BOB.
7868 ;;
7869 ;; The criterion for a paren structure being the arg list is:
7870 ;; o - there is non-WS stuff after it but before any "{"; AND
7871 ;; o - the token after it isn't a ";" AND
7872 ;; o - it is preceded by either an identifier (the function name) or
7873 ;; a macro expansion like "DEFUN (...)"; AND
7874 ;; o - its content is a non-empty comma-separated list of identifiers
7875 ;; (an empty arg list won't have a knr region).
7876 ;;
7877 ;; The following snippet illustrates these rules:
7878 ;; int foo (bar, baz, yuk)
7879 ;; int bar [] ;
7880 ;; int (*baz) (my_type) ;
7881 ;; int (*) (void) (*yuk) (void) ;
7882 ;; {
7883
7884 (catch 'knr
7885 (while (> pp-count-out 0) ; go back one paren/bracket pair each time.
7886 (setq pp-count-out (1- pp-count-out))
7887 (c-syntactic-skip-backward "^)]}=")
7888 (cond ((eq (char-before) ?\))
7889 (setq after-rparen (point)))
7890 ((eq (char-before) ?\])
7891 (setq after-rparen nil))
7892 (t ; either } (hit previous defun) or = or no more
7893 ; parens/brackets.
7894 (throw 'knr nil)))
7895
7896 (if after-rparen
7897 ;; We're inside a paren. Could it be our argument list....?
7898 (if
7899 (and
7900 (progn
7901 (goto-char after-rparen)
7902 (unless (c-go-list-backward) (throw 'knr nil)) ;
7903 ;; FIXME!!! What about macros between the parens? 2007/01/20
7904 (setq before-lparen (point)))
7905
7906 ;; It can't be the arg list if next token is ; or {
7907 (progn (goto-char after-rparen)
7908 (c-forward-syntactic-ws)
7909 (not (memq (char-after) '(?\; ?\{ ?\=))))
7910
7911 ;; Is the thing preceding the list an identifier (the
7912 ;; function name), or a macro expansion?
7913 (progn
7914 (goto-char before-lparen)
7915 (eq (c-backward-token-2) 0)
7916 (or (eq (c-on-identifier) (point))
7917 (and (eq (char-after) ?\))
7918 (c-go-up-list-backward)
7919 (eq (c-backward-token-2) 0)
7920 (eq (c-on-identifier) (point)))))
7921
7922 ;; Have we got a non-empty list of comma-separated
7923 ;; identifiers?
7924 (progn
7925 (goto-char before-lparen)
7926 (c-forward-token-2) ; to first token inside parens
7927 (and
7928 (c-on-identifier)
7929 (c-forward-token-2)
7930 (catch 'id-list
7931 (while (eq (char-after) ?\,)
7932 (c-forward-token-2)
7933 (unless (c-on-identifier) (throw 'id-list nil))
7934 (c-forward-token-2))
7935 (eq (char-after) ?\))))))
7936
7937 ;; ...Yes. We've identified the function's argument list.
7938 (throw 'knr
7939 (progn (goto-char after-rparen)
7940 (c-forward-syntactic-ws)
7941 (point)))
7942
7943 ;; ...No. The current parens aren't the function's arg list.
7944 (goto-char before-lparen))
7945
7946 (or (c-go-list-backward) ; backwards over [ .... ]
7947 (throw 'knr nil)))))))))
7948
7949 (defun c-skip-conditional ()
7950 ;; skip forward over conditional at point, including any predicate
7951 ;; statements in parentheses. No error checking is performed.
7952 ;;
7953 ;; This function might do hidden buffer changes.
7954 (c-forward-sexp (cond
7955 ;; else if()
7956 ((looking-at (concat "\\<else"
7957 "\\([ \t\n]\\|\\\\\n\\)+"
7958 "if\\>\\([^_]\\|$\\)"))
7959 3)
7960 ;; do, else, try, finally
7961 ((looking-at (concat "\\<\\("
7962 "do\\|else\\|try\\|finally"
7963 "\\)\\>\\([^_]\\|$\\)"))
7964 1)
7965 ;; for, if, while, switch, catch, synchronized, foreach
7966 (t 2))))
7967
7968 (defun c-after-conditional (&optional lim)
7969 ;; If looking at the token after a conditional then return the
7970 ;; position of its start, otherwise return nil.
7971 ;;
7972 ;; This function might do hidden buffer changes.
7973 (save-excursion
7974 (and (zerop (c-backward-token-2 1 t lim))
7975 (or (looking-at c-block-stmt-1-key)
7976 (and (eq (char-after) ?\()
7977 (zerop (c-backward-token-2 1 t lim))
7978 (looking-at c-block-stmt-2-key)))
7979 (point))))
7980
7981 (defun c-after-special-operator-id (&optional lim)
7982 ;; If the point is after an operator identifier that isn't handled
7983 ;; like an ordinary symbol (i.e. like "operator =" in C++) then the
7984 ;; position of the start of that identifier is returned. nil is
7985 ;; returned otherwise. The point may be anywhere in the syntactic
7986 ;; whitespace after the last token of the operator identifier.
7987 ;;
7988 ;; This function might do hidden buffer changes.
7989 (save-excursion
7990 (and c-overloadable-operators-regexp
7991 (zerop (c-backward-token-2 1 nil lim))
7992 (looking-at c-overloadable-operators-regexp)
7993 (or (not c-opt-op-identifier-prefix)
7994 (and
7995 (zerop (c-backward-token-2 1 nil lim))
7996 (looking-at c-opt-op-identifier-prefix)))
7997 (point))))
7998
7999 (defsubst c-backward-to-block-anchor (&optional lim)
8000 ;; Assuming point is at a brace that opens a statement block of some
8001 ;; kind, move to the proper anchor point for that block. It might
8002 ;; need to be adjusted further by c-add-stmt-syntax, but the
8003 ;; position at return is suitable as start position for that
8004 ;; function.
8005 ;;
8006 ;; This function might do hidden buffer changes.
8007 (unless (= (point) (c-point 'boi))
8008 (let ((start (c-after-conditional lim)))
8009 (if start
8010 (goto-char start)))))
8011
8012 (defsubst c-backward-to-decl-anchor (&optional lim)
8013 ;; Assuming point is at a brace that opens the block of a top level
8014 ;; declaration of some kind, move to the proper anchor point for
8015 ;; that block.
8016 ;;
8017 ;; This function might do hidden buffer changes.
8018 (unless (= (point) (c-point 'boi))
8019 (c-beginning-of-statement-1 lim)))
8020
8021 (defun c-search-decl-header-end ()
8022 ;; Search forward for the end of the "header" of the current
8023 ;; declaration. That's the position where the definition body
8024 ;; starts, or the first variable initializer, or the ending
8025 ;; semicolon. I.e. search forward for the closest following
8026 ;; (syntactically relevant) '{', '=' or ';' token. Point is left
8027 ;; _after_ the first found token, or at point-max if none is found.
8028 ;;
8029 ;; This function might do hidden buffer changes.
8030
8031 (let ((base (point)))
8032 (if (c-major-mode-is 'c++-mode)
8033
8034 ;; In C++ we need to take special care to handle operator
8035 ;; tokens and those pesky template brackets.
8036 (while (and
8037 (c-syntactic-re-search-forward "[;{<=]" nil 'move t t)
8038 (or
8039 (c-end-of-current-token base)
8040 ;; Handle operator identifiers, i.e. ignore any
8041 ;; operator token preceded by "operator".
8042 (save-excursion
8043 (and (c-safe (c-backward-sexp) t)
8044 (looking-at c-opt-op-identifier-prefix)))
8045 (and (eq (char-before) ?<)
8046 (c-with-syntax-table c++-template-syntax-table
8047 (if (c-safe (goto-char (c-up-list-forward (point))))
8048 t
8049 (goto-char (point-max))
8050 nil)))))
8051 (setq base (point)))
8052
8053 (while (and
8054 (c-syntactic-re-search-forward "[;{=]" nil 'move t t)
8055 (c-end-of-current-token base))
8056 (setq base (point))))))
8057
8058 (defun c-beginning-of-decl-1 (&optional lim)
8059 ;; Go to the beginning of the current declaration, or the beginning
8060 ;; of the previous one if already at the start of it. Point won't
8061 ;; be moved out of any surrounding paren. Return a cons cell of the
8062 ;; form (MOVE . KNR-POS). MOVE is like the return value from
8063 ;; `c-beginning-of-statement-1'. If point skipped over some K&R
8064 ;; style argument declarations (and they are to be recognized) then
8065 ;; KNR-POS is set to the start of the first such argument
8066 ;; declaration, otherwise KNR-POS is nil. If LIM is non-nil, it's a
8067 ;; position that bounds the backward search.
8068 ;;
8069 ;; NB: Cases where the declaration continues after the block, as in
8070 ;; "struct foo { ... } bar;", are currently recognized as two
8071 ;; declarations, e.g. "struct foo { ... }" and "bar;" in this case.
8072 ;;
8073 ;; This function might do hidden buffer changes.
8074 (catch 'return
8075 (let* ((start (point))
8076 (last-stmt-start (point))
8077 (move (c-beginning-of-statement-1 lim nil t)))
8078
8079 ;; `c-beginning-of-statement-1' stops at a block start, but we
8080 ;; want to continue if the block doesn't begin a top level
8081 ;; construct, i.e. if it isn't preceded by ';', '}', ':', bob,
8082 ;; or an open paren.
8083 (let ((beg (point)) tentative-move)
8084 ;; Go back one "statement" each time round the loop until we're just
8085 ;; after a ;, }, or :, or at BOB or the start of a macro or start of
8086 ;; an ObjC method. This will move over a multiple declaration whose
8087 ;; components are comma separated.
8088 (while (and
8089 ;; Must check with c-opt-method-key in ObjC mode.
8090 (not (and c-opt-method-key
8091 (looking-at c-opt-method-key)))
8092 (/= last-stmt-start (point))
8093 (progn
8094 (c-backward-syntactic-ws lim)
8095 (not (memq (char-before) '(?\; ?} ?: nil))))
8096 (save-excursion
8097 (backward-char)
8098 (not (looking-at "\\s(")))
8099 ;; Check that we don't move from the first thing in a
8100 ;; macro to its header.
8101 (not (eq (setq tentative-move
8102 (c-beginning-of-statement-1 lim nil t))
8103 'macro)))
8104 (setq last-stmt-start beg
8105 beg (point)
8106 move tentative-move))
8107 (goto-char beg))
8108
8109 (when c-recognize-knr-p
8110 (let ((fallback-pos (point)) knr-argdecl-start)
8111 ;; Handle K&R argdecls. Back up after the "statement" jumped
8112 ;; over by `c-beginning-of-statement-1', unless it was the
8113 ;; function body, in which case we're sitting on the opening
8114 ;; brace now. Then test if we're in a K&R argdecl region and
8115 ;; that we started at the other side of the first argdecl in
8116 ;; it.
8117 (unless (eq (char-after) ?{)
8118 (goto-char last-stmt-start))
8119 (if (and (setq knr-argdecl-start (c-in-knr-argdecl lim))
8120 (< knr-argdecl-start start)
8121 (progn
8122 (goto-char knr-argdecl-start)
8123 (not (eq (c-beginning-of-statement-1 lim nil t) 'macro))))
8124 (throw 'return
8125 (cons (if (eq (char-after fallback-pos) ?{)
8126 'previous
8127 'same)
8128 knr-argdecl-start))
8129 (goto-char fallback-pos))))
8130
8131 ;; `c-beginning-of-statement-1' counts each brace block as a separate
8132 ;; statement, so the result will be 'previous if we've moved over any.
8133 ;; So change our result back to 'same if necessary.
8134 ;;
8135 ;; If they were brace list initializers we might not have moved over a
8136 ;; declaration boundary though, so change it to 'same if we've moved
8137 ;; past a '=' before '{', but not ';'. (This ought to be integrated
8138 ;; into `c-beginning-of-statement-1', so we avoid this extra pass which
8139 ;; potentially can search over a large amount of text.). Take special
8140 ;; pains not to get mislead by C++'s "operator=", and the like.
8141 (if (and (eq move 'previous)
8142 (c-with-syntax-table (if (c-major-mode-is 'c++-mode)
8143 c++-template-syntax-table
8144 (syntax-table))
8145 (save-excursion
8146 (and
8147 (progn
8148 (while ; keep going back to "[;={"s until we either find
8149 ; no more, or get to one which isn't an "operator ="
8150 (and (c-syntactic-re-search-forward "[;={]" start t t t)
8151 (eq (char-before) ?=)
8152 c-overloadable-operators-regexp
8153 c-opt-op-identifier-prefix
8154 (save-excursion
8155 (eq (c-backward-token-2) 0)
8156 (looking-at c-overloadable-operators-regexp)
8157 (eq (c-backward-token-2) 0)
8158 (looking-at c-opt-op-identifier-prefix))))
8159 (eq (char-before) ?=))
8160 (c-syntactic-re-search-forward "[;{]" start t t)
8161 (eq (char-before) ?{)
8162 (c-safe (goto-char (c-up-list-forward (point))) t)
8163 (not (c-syntactic-re-search-forward ";" start t t))))))
8164 (cons 'same nil)
8165 (cons move nil)))))
8166
8167 (defun c-end-of-decl-1 ()
8168 ;; Assuming point is at the start of a declaration (as detected by
8169 ;; e.g. `c-beginning-of-decl-1'), go to the end of it. Unlike
8170 ;; `c-beginning-of-decl-1', this function handles the case when a
8171 ;; block is followed by identifiers in e.g. struct declarations in C
8172 ;; or C++. If a proper end was found then t is returned, otherwise
8173 ;; point is moved as far as possible within the current sexp and nil
8174 ;; is returned. This function doesn't handle macros; use
8175 ;; `c-end-of-macro' instead in those cases.
8176 ;;
8177 ;; This function might do hidden buffer changes.
8178 (let ((start (point))
8179 (decl-syntax-table (if (c-major-mode-is 'c++-mode)
8180 c++-template-syntax-table
8181 (syntax-table))))
8182 (catch 'return
8183 (c-search-decl-header-end)
8184
8185 (when (and c-recognize-knr-p
8186 (eq (char-before) ?\;)
8187 (c-in-knr-argdecl start))
8188 ;; Stopped at the ';' in a K&R argdecl section which is
8189 ;; detected using the same criteria as in
8190 ;; `c-beginning-of-decl-1'. Move to the following block
8191 ;; start.
8192 (c-syntactic-re-search-forward "{" nil 'move t))
8193
8194 (when (eq (char-before) ?{)
8195 ;; Encountered a block in the declaration. Jump over it.
8196 (condition-case nil
8197 (goto-char (c-up-list-forward (point)))
8198 (error (goto-char (point-max))
8199 (throw 'return nil)))
8200 (if (or (not c-opt-block-decls-with-vars-key)
8201 (save-excursion
8202 (c-with-syntax-table decl-syntax-table
8203 (let ((lim (point)))
8204 (goto-char start)
8205 (not (and
8206 ;; Check for `c-opt-block-decls-with-vars-key'
8207 ;; before the first paren.
8208 (c-syntactic-re-search-forward
8209 (concat "[;=\(\[{]\\|\\("
8210 c-opt-block-decls-with-vars-key
8211 "\\)")
8212 lim t t t)
8213 (match-beginning 1)
8214 (not (eq (char-before) ?_))
8215 ;; Check that the first following paren is
8216 ;; the block.
8217 (c-syntactic-re-search-forward "[;=\(\[{]"
8218 lim t t t)
8219 (eq (char-before) ?{)))))))
8220 ;; The declaration doesn't have any of the
8221 ;; `c-opt-block-decls-with-vars' keywords in the
8222 ;; beginning, so it ends here at the end of the block.
8223 (throw 'return t)))
8224
8225 (c-with-syntax-table decl-syntax-table
8226 (while (progn
8227 (if (eq (char-before) ?\;)
8228 (throw 'return t))
8229 (c-syntactic-re-search-forward ";" nil 'move t))))
8230 nil)))
8231
8232 (defun c-looking-at-decl-block (containing-sexp goto-start &optional limit)
8233 ;; Assuming the point is at an open brace, check if it starts a
8234 ;; block that contains another declaration level, i.e. that isn't a
8235 ;; statement block or a brace list, and if so return non-nil.
8236 ;;
8237 ;; If the check is successful, the return value is the start of the
8238 ;; keyword that tells what kind of construct it is, i.e. typically
8239 ;; what `c-decl-block-key' matched. Also, if GOTO-START is set then
8240 ;; the point will be at the start of the construct, before any
8241 ;; leading specifiers, otherwise it's at the returned position.
8242 ;;
8243 ;; The point is clobbered if the check is unsuccessful.
8244 ;;
8245 ;; CONTAINING-SEXP is the position of the open of the surrounding
8246 ;; paren, or nil if none.
8247 ;;
8248 ;; The optional LIMIT limits the backward search for the start of
8249 ;; the construct. It's assumed to be at a syntactically relevant
8250 ;; position.
8251 ;;
8252 ;; If any template arglists are found in the searched region before
8253 ;; the open brace, they get marked with paren syntax.
8254 ;;
8255 ;; This function might do hidden buffer changes.
8256
8257 (let ((open-brace (point)) kwd-start first-specifier-pos)
8258 (c-syntactic-skip-backward c-block-prefix-charset limit t)
8259
8260 (when (and c-recognize-<>-arglists
8261 (eq (char-before) ?>))
8262 ;; Could be at the end of a template arglist.
8263 (let ((c-parse-and-markup-<>-arglists t)
8264 (c-disallow-comma-in-<>-arglists
8265 (and containing-sexp
8266 (not (eq (char-after containing-sexp) ?{)))))
8267 (while (and
8268 (c-backward-<>-arglist nil limit)
8269 (progn
8270 (c-syntactic-skip-backward c-block-prefix-charset limit t)
8271 (eq (char-before) ?>))))))
8272
8273 ;; Note: Can't get bogus hits inside template arglists below since they
8274 ;; have gotten paren syntax above.
8275 (when (and
8276 ;; If `goto-start' is set we begin by searching for the
8277 ;; first possible position of a leading specifier list.
8278 ;; The `c-decl-block-key' search continues from there since
8279 ;; we know it can't match earlier.
8280 (if goto-start
8281 (when (c-syntactic-re-search-forward c-symbol-start
8282 open-brace t t)
8283 (goto-char (setq first-specifier-pos (match-beginning 0)))
8284 t)
8285 t)
8286
8287 (cond
8288 ((c-syntactic-re-search-forward c-decl-block-key open-brace t t t)
8289 (goto-char (setq kwd-start (match-beginning 0)))
8290 (or
8291
8292 ;; Found a keyword that can't be a type?
8293 (match-beginning 1)
8294
8295 ;; Can be a type too, in which case it's the return type of a
8296 ;; function (under the assumption that no declaration level
8297 ;; block construct starts with a type).
8298 (not (c-forward-type))
8299
8300 ;; Jumped over a type, but it could be a declaration keyword
8301 ;; followed by the declared identifier that we've jumped over
8302 ;; instead (e.g. in "class Foo {"). If it indeed is a type
8303 ;; then we should be at the declarator now, so check for a
8304 ;; valid declarator start.
8305 ;;
8306 ;; Note: This doesn't cope with the case when a declared
8307 ;; identifier is followed by e.g. '(' in a language where '('
8308 ;; also might be part of a declarator expression. Currently
8309 ;; there's no such language.
8310 (not (or (looking-at c-symbol-start)
8311 (looking-at c-type-decl-prefix-key)))))
8312
8313 ;; In Pike a list of modifiers may be followed by a brace
8314 ;; to make them apply to many identifiers. Note that the
8315 ;; match data will be empty on return in this case.
8316 ((and (c-major-mode-is 'pike-mode)
8317 (progn
8318 (goto-char open-brace)
8319 (= (c-backward-token-2) 0))
8320 (looking-at c-specifier-key)
8321 ;; Use this variant to avoid yet another special regexp.
8322 (c-keyword-member (c-keyword-sym (match-string 1))
8323 'c-modifier-kwds))
8324 (setq kwd-start (point))
8325 t)))
8326
8327 ;; Got a match.
8328
8329 (if goto-start
8330 ;; Back up over any preceding specifiers and their clauses
8331 ;; by going forward from `first-specifier-pos', which is the
8332 ;; earliest possible position where the specifier list can
8333 ;; start.
8334 (progn
8335 (goto-char first-specifier-pos)
8336
8337 (while (< (point) kwd-start)
8338 (if (looking-at c-symbol-key)
8339 ;; Accept any plain symbol token on the ground that
8340 ;; it's a specifier masked through a macro (just
8341 ;; like `c-forward-decl-or-cast-1' skip forward over
8342 ;; such tokens).
8343 ;;
8344 ;; Could be more restrictive wrt invalid keywords,
8345 ;; but that'd only occur in invalid code so there's
8346 ;; no use spending effort on it.
8347 (let ((end (match-end 0)))
8348 (unless (c-forward-keyword-clause 0)
8349 (goto-char end)
8350 (c-forward-syntactic-ws)))
8351
8352 ;; Can't parse a declaration preamble and is still
8353 ;; before `kwd-start'. That means `first-specifier-pos'
8354 ;; was in some earlier construct. Search again.
8355 (if (c-syntactic-re-search-forward c-symbol-start
8356 kwd-start 'move t)
8357 (goto-char (setq first-specifier-pos (match-beginning 0)))
8358 ;; Got no preamble before the block declaration keyword.
8359 (setq first-specifier-pos kwd-start))))
8360
8361 (goto-char first-specifier-pos))
8362 (goto-char kwd-start))
8363
8364 kwd-start)))
8365
8366 (defun c-search-uplist-for-classkey (paren-state)
8367 ;; Check if the closest containing paren sexp is a declaration
8368 ;; block, returning a 2 element vector in that case. Aref 0
8369 ;; contains the bufpos at boi of the class key line, and aref 1
8370 ;; contains the bufpos of the open brace. This function is an
8371 ;; obsolete wrapper for `c-looking-at-decl-block'.
8372 ;;
8373 ;; This function might do hidden buffer changes.
8374 (let ((open-paren-pos (c-most-enclosing-brace paren-state)))
8375 (when open-paren-pos
8376 (save-excursion
8377 (goto-char open-paren-pos)
8378 (when (and (eq (char-after) ?{)
8379 (c-looking-at-decl-block
8380 (c-safe-position open-paren-pos paren-state)
8381 nil))
8382 (back-to-indentation)
8383 (vector (point) open-paren-pos))))))
8384
8385 (defmacro c-pull-open-brace (ps)
8386 ;; Pull the next open brace from PS (which has the form of paren-state),
8387 ;; skipping over any brace pairs. Returns NIL when PS is exhausted.
8388 `(progn
8389 (while (consp (car ,ps))
8390 (setq ,ps (cdr ,ps)))
8391 (prog1 (car ,ps)
8392 (setq ,ps (cdr ,ps)))))
8393
8394 (defun c-most-enclosing-decl-block (paren-state)
8395 ;; Return the buffer position of the most enclosing decl-block brace (in the
8396 ;; sense of c-looking-at-decl-block) in the PAREN-STATE structure, or nil if
8397 ;; none was found.
8398 (let* ((open-brace (c-pull-open-brace paren-state))
8399 (next-open-brace (c-pull-open-brace paren-state)))
8400 (while (and open-brace
8401 (save-excursion
8402 (goto-char open-brace)
8403 (not (c-looking-at-decl-block next-open-brace nil))))
8404 (setq open-brace next-open-brace
8405 next-open-brace (c-pull-open-brace paren-state)))
8406 open-brace))
8407
8408 (defun c-cheap-inside-bracelist-p (paren-state)
8409 ;; Return the position of the L-brace if point is inside a brace list
8410 ;; initialization of an array, etc. This is an approximate function,
8411 ;; designed for speed over accuracy. It will not find every bracelist, but
8412 ;; a non-nil result is reliable. We simply search for "= {" (naturally with
8413 ;; syntactic whitespace allowed). PAREN-STATE is the normal thing that it
8414 ;; is everywhere else.
8415 (let (b-pos)
8416 (save-excursion
8417 (while
8418 (and (setq b-pos (c-pull-open-brace paren-state))
8419 (progn (goto-char b-pos)
8420 (c-backward-sws)
8421 (c-backward-token-2)
8422 (not (looking-at "=")))))
8423 b-pos)))
8424
8425 (defun c-inside-bracelist-p (containing-sexp paren-state)
8426 ;; return the buffer position of the beginning of the brace list
8427 ;; statement if we're inside a brace list, otherwise return nil.
8428 ;; CONTAINING-SEXP is the buffer pos of the innermost containing
8429 ;; paren. PAREN-STATE is the remainder of the state of enclosing
8430 ;; braces
8431 ;;
8432 ;; N.B.: This algorithm can potentially get confused by cpp macros
8433 ;; placed in inconvenient locations. It's a trade-off we make for
8434 ;; speed.
8435 ;;
8436 ;; This function might do hidden buffer changes.
8437 (or
8438 ;; This will pick up brace list declarations.
8439 (c-safe
8440 (save-excursion
8441 (goto-char containing-sexp)
8442 (c-forward-sexp -1)
8443 (let (bracepos)
8444 (if (and (or (looking-at c-brace-list-key)
8445 (progn (c-forward-sexp -1)
8446 (looking-at c-brace-list-key)))
8447 (setq bracepos (c-down-list-forward (point)))
8448 (not (c-crosses-statement-barrier-p (point)
8449 (- bracepos 2))))
8450 (point)))))
8451 ;; this will pick up array/aggregate init lists, even if they are nested.
8452 (save-excursion
8453 (let ((class-key
8454 ;; Pike can have class definitions anywhere, so we must
8455 ;; check for the class key here.
8456 (and (c-major-mode-is 'pike-mode)
8457 c-decl-block-key))
8458 bufpos braceassignp lim next-containing)
8459 (while (and (not bufpos)
8460 containing-sexp)
8461 (when paren-state
8462 (if (consp (car paren-state))
8463 (setq lim (cdr (car paren-state))
8464 paren-state (cdr paren-state))
8465 (setq lim (car paren-state)))
8466 (when paren-state
8467 (setq next-containing (car paren-state)
8468 paren-state (cdr paren-state))))
8469 (goto-char containing-sexp)
8470 (if (c-looking-at-inexpr-block next-containing next-containing)
8471 ;; We're in an in-expression block of some kind. Do not
8472 ;; check nesting. We deliberately set the limit to the
8473 ;; containing sexp, so that c-looking-at-inexpr-block
8474 ;; doesn't check for an identifier before it.
8475 (setq containing-sexp nil)
8476 ;; see if the open brace is preceded by = or [...] in
8477 ;; this statement, but watch out for operator=
8478 (setq braceassignp 'dontknow)
8479 (c-backward-token-2 1 t lim)
8480 ;; Checks to do only on the first sexp before the brace.
8481 (when (and c-opt-inexpr-brace-list-key
8482 (eq (char-after) ?\[))
8483 ;; In Java, an initialization brace list may follow
8484 ;; directly after "new Foo[]", so check for a "new"
8485 ;; earlier.
8486 (while (eq braceassignp 'dontknow)
8487 (setq braceassignp
8488 (cond ((/= (c-backward-token-2 1 t lim) 0) nil)
8489 ((looking-at c-opt-inexpr-brace-list-key) t)
8490 ((looking-at "\\sw\\|\\s_\\|[.[]")
8491 ;; Carry on looking if this is an
8492 ;; identifier (may contain "." in Java)
8493 ;; or another "[]" sexp.
8494 'dontknow)
8495 (t nil)))))
8496 ;; Checks to do on all sexps before the brace, up to the
8497 ;; beginning of the statement.
8498 (while (eq braceassignp 'dontknow)
8499 (cond ((eq (char-after) ?\;)
8500 (setq braceassignp nil))
8501 ((and class-key
8502 (looking-at class-key))
8503 (setq braceassignp nil))
8504 ((eq (char-after) ?=)
8505 ;; We've seen a =, but must check earlier tokens so
8506 ;; that it isn't something that should be ignored.
8507 (setq braceassignp 'maybe)
8508 (while (and (eq braceassignp 'maybe)
8509 (zerop (c-backward-token-2 1 t lim)))
8510 (setq braceassignp
8511 (cond
8512 ;; Check for operator =
8513 ((and c-opt-op-identifier-prefix
8514 (looking-at c-opt-op-identifier-prefix))
8515 nil)
8516 ;; Check for `<opchar>= in Pike.
8517 ((and (c-major-mode-is 'pike-mode)
8518 (or (eq (char-after) ?`)
8519 ;; Special case for Pikes
8520 ;; `[]=, since '[' is not in
8521 ;; the punctuation class.
8522 (and (eq (char-after) ?\[)
8523 (eq (char-before) ?`))))
8524 nil)
8525 ((looking-at "\\s.") 'maybe)
8526 ;; make sure we're not in a C++ template
8527 ;; argument assignment
8528 ((and
8529 (c-major-mode-is 'c++-mode)
8530 (save-excursion
8531 (let ((here (point))
8532 (pos< (progn
8533 (skip-chars-backward "^<>")
8534 (point))))
8535 (and (eq (char-before) ?<)
8536 (not (c-crosses-statement-barrier-p
8537 pos< here))
8538 (not (c-in-literal))
8539 ))))
8540 nil)
8541 (t t))))))
8542 (if (and (eq braceassignp 'dontknow)
8543 (/= (c-backward-token-2 1 t lim) 0))
8544 (setq braceassignp nil)))
8545 (if (not braceassignp)
8546 (if (eq (char-after) ?\;)
8547 ;; Brace lists can't contain a semicolon, so we're done.
8548 (setq containing-sexp nil)
8549 ;; Go up one level.
8550 (setq containing-sexp next-containing
8551 lim nil
8552 next-containing nil))
8553 ;; we've hit the beginning of the aggregate list
8554 (c-beginning-of-statement-1
8555 (c-most-enclosing-brace paren-state))
8556 (setq bufpos (point))))
8557 )
8558 bufpos))
8559 ))
8560
8561 (defun c-looking-at-special-brace-list (&optional lim)
8562 ;; If we're looking at the start of a pike-style list, ie `({ })',
8563 ;; `([ ])', `(< >)' etc, a cons of a cons of its starting and ending
8564 ;; positions and its entry in c-special-brace-lists is returned, nil
8565 ;; otherwise. The ending position is nil if the list is still open.
8566 ;; LIM is the limit for forward search. The point may either be at
8567 ;; the `(' or at the following paren character. Tries to check the
8568 ;; matching closer, but assumes it's correct if no balanced paren is
8569 ;; found (i.e. the case `({ ... } ... )' is detected as _not_ being
8570 ;; a special brace list).
8571 ;;
8572 ;; This function might do hidden buffer changes.
8573 (if c-special-brace-lists
8574 (condition-case ()
8575 (save-excursion
8576 (let ((beg (point))
8577 inner-beg end type)
8578 (c-forward-syntactic-ws)
8579 (if (eq (char-after) ?\()
8580 (progn
8581 (forward-char 1)
8582 (c-forward-syntactic-ws)
8583 (setq inner-beg (point))
8584 (setq type (assq (char-after) c-special-brace-lists)))
8585 (if (setq type (assq (char-after) c-special-brace-lists))
8586 (progn
8587 (setq inner-beg (point))
8588 (c-backward-syntactic-ws)
8589 (forward-char -1)
8590 (setq beg (if (eq (char-after) ?\()
8591 (point)
8592 nil)))))
8593 (if (and beg type)
8594 (if (and (c-safe
8595 (goto-char beg)
8596 (c-forward-sexp 1)
8597 (setq end (point))
8598 (= (char-before) ?\)))
8599 (c-safe
8600 (goto-char inner-beg)
8601 (if (looking-at "\\s(")
8602 ;; Check balancing of the inner paren
8603 ;; below.
8604 (progn
8605 (c-forward-sexp 1)
8606 t)
8607 ;; If the inner char isn't a paren then
8608 ;; we can't check balancing, so just
8609 ;; check the char before the outer
8610 ;; closing paren.
8611 (goto-char end)
8612 (backward-char)
8613 (c-backward-syntactic-ws)
8614 (= (char-before) (cdr type)))))
8615 (if (or (/= (char-syntax (char-before)) ?\))
8616 (= (progn
8617 (c-forward-syntactic-ws)
8618 (point))
8619 (1- end)))
8620 (cons (cons beg end) type))
8621 (cons (list beg) type)))))
8622 (error nil))))
8623
8624 (defun c-looking-at-bos (&optional lim)
8625 ;; Return non-nil if between two statements or declarations, assuming
8626 ;; point is not inside a literal or comment.
8627 ;;
8628 ;; Obsolete - `c-at-statement-start-p' or `c-at-expression-start-p'
8629 ;; are recommended instead.
8630 ;;
8631 ;; This function might do hidden buffer changes.
8632 (c-at-statement-start-p))
8633 (make-obsolete 'c-looking-at-bos 'c-at-statement-start-p "22.1")
8634
8635 (defun c-looking-at-inexpr-block (lim containing-sexp &optional check-at-end)
8636 ;; Return non-nil if we're looking at the beginning of a block
8637 ;; inside an expression. The value returned is actually a cons of
8638 ;; either 'inlambda, 'inexpr-statement or 'inexpr-class and the
8639 ;; position of the beginning of the construct.
8640 ;;
8641 ;; LIM limits the backward search. CONTAINING-SEXP is the start
8642 ;; position of the closest containing list. If it's nil, the
8643 ;; containing paren isn't used to decide whether we're inside an
8644 ;; expression or not. If both LIM and CONTAINING-SEXP are used, LIM
8645 ;; needs to be farther back.
8646 ;;
8647 ;; If CHECK-AT-END is non-nil then extra checks at the end of the
8648 ;; brace block might be done. It should only be used when the
8649 ;; construct can be assumed to be complete, i.e. when the original
8650 ;; starting position was further down than that.
8651 ;;
8652 ;; This function might do hidden buffer changes.
8653
8654 (save-excursion
8655 (let ((res 'maybe) passed-paren
8656 (closest-lim (or containing-sexp lim (point-min)))
8657 ;; Look at the character after point only as a last resort
8658 ;; when we can't disambiguate.
8659 (block-follows (and (eq (char-after) ?{) (point))))
8660
8661 (while (and (eq res 'maybe)
8662 (progn (c-backward-syntactic-ws)
8663 (> (point) closest-lim))
8664 (not (bobp))
8665 (progn (backward-char)
8666 (looking-at "[\]\).]\\|\\w\\|\\s_"))
8667 (c-safe (forward-char)
8668 (goto-char (scan-sexps (point) -1))))
8669
8670 (setq res
8671 (if (looking-at c-keywords-regexp)
8672 (let ((kw-sym (c-keyword-sym (match-string 1))))
8673 (cond
8674 ((and block-follows
8675 (c-keyword-member kw-sym 'c-inexpr-class-kwds))
8676 (and (not (eq passed-paren ?\[))
8677 (or (not (looking-at c-class-key))
8678 ;; If the class definition is at the start of
8679 ;; a statement, we don't consider it an
8680 ;; in-expression class.
8681 (let ((prev (point)))
8682 (while (and
8683 (= (c-backward-token-2 1 nil closest-lim) 0)
8684 (eq (char-syntax (char-after)) ?w))
8685 (setq prev (point)))
8686 (goto-char prev)
8687 (not (c-at-statement-start-p)))
8688 ;; Also, in Pike we treat it as an
8689 ;; in-expression class if it's used in an
8690 ;; object clone expression.
8691 (save-excursion
8692 (and check-at-end
8693 (c-major-mode-is 'pike-mode)
8694 (progn (goto-char block-follows)
8695 (zerop (c-forward-token-2 1 t)))
8696 (eq (char-after) ?\())))
8697 (cons 'inexpr-class (point))))
8698 ((c-keyword-member kw-sym 'c-inexpr-block-kwds)
8699 (when (not passed-paren)
8700 (cons 'inexpr-statement (point))))
8701 ((c-keyword-member kw-sym 'c-lambda-kwds)
8702 (when (or (not passed-paren)
8703 (eq passed-paren ?\())
8704 (cons 'inlambda (point))))
8705 ((c-keyword-member kw-sym 'c-block-stmt-kwds)
8706 nil)
8707 (t
8708 'maybe)))
8709
8710 (if (looking-at "\\s(")
8711 (if passed-paren
8712 (if (and (eq passed-paren ?\[)
8713 (eq (char-after) ?\[))
8714 ;; Accept several square bracket sexps for
8715 ;; Java array initializations.
8716 'maybe)
8717 (setq passed-paren (char-after))
8718 'maybe)
8719 'maybe))))
8720
8721 (if (eq res 'maybe)
8722 (when (and c-recognize-paren-inexpr-blocks
8723 block-follows
8724 containing-sexp
8725 (eq (char-after containing-sexp) ?\())
8726 (goto-char containing-sexp)
8727 (if (or (save-excursion
8728 (c-backward-syntactic-ws lim)
8729 (and (> (point) (or lim (point-min)))
8730 (c-on-identifier)))
8731 (and c-special-brace-lists
8732 (c-looking-at-special-brace-list)))
8733 nil
8734 (cons 'inexpr-statement (point))))
8735
8736 res))))
8737
8738 (defun c-looking-at-inexpr-block-backward (paren-state)
8739 ;; Returns non-nil if we're looking at the end of an in-expression
8740 ;; block, otherwise the same as `c-looking-at-inexpr-block'.
8741 ;; PAREN-STATE is the paren state relevant at the current position.
8742 ;;
8743 ;; This function might do hidden buffer changes.
8744 (save-excursion
8745 ;; We currently only recognize a block.
8746 (let ((here (point))
8747 (elem (car-safe paren-state))
8748 containing-sexp)
8749 (when (and (consp elem)
8750 (progn (goto-char (cdr elem))
8751 (c-forward-syntactic-ws here)
8752 (= (point) here)))
8753 (goto-char (car elem))
8754 (if (setq paren-state (cdr paren-state))
8755 (setq containing-sexp (car-safe paren-state)))
8756 (c-looking-at-inexpr-block (c-safe-position containing-sexp
8757 paren-state)
8758 containing-sexp)))))
8759
8760 (defun c-at-macro-vsemi-p (&optional pos)
8761 ;; Is there a "virtual semicolon" at POS or point?
8762 ;; (See cc-defs.el for full details of "virtual semicolons".)
8763 ;;
8764 ;; This is true when point is at the last non syntactic WS position on the
8765 ;; line, there is a macro call last on the line, and this particular macro's
8766 ;; name is defined by the regexp `c-vs-macro-regexp' as not needing a
8767 ;; semicolon.
8768 (save-excursion
8769 (save-restriction
8770 (widen)
8771 (if pos
8772 (goto-char pos)
8773 (setq pos (point)))
8774 (and
8775 c-macro-with-semi-re
8776 (eq (skip-chars-backward " \t") 0)
8777
8778 ;; Check we've got nothing after this except comments and empty lines
8779 ;; joined by escaped EOLs.
8780 (skip-chars-forward " \t") ; always returns non-nil.
8781 (progn
8782 (while ; go over 1 block comment per iteration.
8783 (and
8784 (looking-at "\\(\\\\[\n\r][ \t]*\\)*")
8785 (goto-char (match-end 0))
8786 (cond
8787 ((looking-at c-block-comment-start-regexp)
8788 (and (forward-comment 1)
8789 (skip-chars-forward " \t"))) ; always returns non-nil
8790 ((looking-at c-line-comment-start-regexp)
8791 (end-of-line)
8792 nil)
8793 (t nil))))
8794 (eolp))
8795
8796 (goto-char pos)
8797 (progn (c-backward-syntactic-ws)
8798 (eq (point) pos))
8799
8800 ;; Check for one of the listed macros being before point.
8801 (or (not (eq (char-before) ?\)))
8802 (when (c-go-list-backward)
8803 (c-backward-syntactic-ws)
8804 t))
8805 (c-simple-skip-symbol-backward)
8806 (looking-at c-macro-with-semi-re)
8807 (goto-char pos)
8808 (not (c-in-literal)))))) ; The most expensive check last.
8809
8810 (defun c-macro-vsemi-status-unknown-p () t) ; See cc-defs.el.
8811
8812 \f
8813 ;; `c-guess-basic-syntax' and the functions that precedes it below
8814 ;; implements the main decision tree for determining the syntactic
8815 ;; analysis of the current line of code.
8816
8817 ;; Dynamically bound to t when `c-guess-basic-syntax' is called during
8818 ;; auto newline analysis.
8819 (defvar c-auto-newline-analysis nil)
8820
8821 (defun c-brace-anchor-point (bracepos)
8822 ;; BRACEPOS is the position of a brace in a construct like "namespace
8823 ;; Bar {". Return the anchor point in this construct; this is the
8824 ;; earliest symbol on the brace's line which isn't earlier than
8825 ;; "namespace".
8826 ;;
8827 ;; Currently (2007-08-17), "like namespace" means "matches
8828 ;; c-other-block-decl-kwds". It doesn't work with "class" or "struct"
8829 ;; or anything like that.
8830 (save-excursion
8831 (let ((boi (c-point 'boi bracepos)))
8832 (goto-char bracepos)
8833 (while (and (> (point) boi)
8834 (not (looking-at c-other-decl-block-key)))
8835 (c-backward-token-2))
8836 (if (> (point) boi) (point) boi))))
8837
8838 (defsubst c-add-syntax (symbol &rest args)
8839 ;; A simple function to prepend a new syntax element to
8840 ;; `c-syntactic-context'. Using `setq' on it is unsafe since it
8841 ;; should always be dynamically bound but since we read it first
8842 ;; we'll fail properly anyway if this function is misused.
8843 (setq c-syntactic-context (cons (cons symbol args)
8844 c-syntactic-context)))
8845
8846 (defsubst c-append-syntax (symbol &rest args)
8847 ;; Like `c-add-syntax' but appends to the end of the syntax list.
8848 ;; (Normally not necessary.)
8849 (setq c-syntactic-context (nconc c-syntactic-context
8850 (list (cons symbol args)))))
8851
8852 (defun c-add-stmt-syntax (syntax-symbol
8853 syntax-extra-args
8854 stop-at-boi-only
8855 containing-sexp
8856 paren-state)
8857 ;; Add the indicated SYNTAX-SYMBOL to `c-syntactic-context', extending it as
8858 ;; needed with further syntax elements of the types `substatement',
8859 ;; `inexpr-statement', `arglist-cont-nonempty', `statement-block-intro', and
8860 ;; `defun-block-intro'.
8861 ;;
8862 ;; Do the generic processing to anchor the given syntax symbol on
8863 ;; the preceding statement: Skip over any labels and containing
8864 ;; statements on the same line, and then search backward until we
8865 ;; find a statement or block start that begins at boi without a
8866 ;; label or comment.
8867 ;;
8868 ;; Point is assumed to be at the prospective anchor point for the
8869 ;; given SYNTAX-SYMBOL. More syntax entries are added if we need to
8870 ;; skip past open parens and containing statements. Most of the added
8871 ;; syntax elements will get the same anchor point - the exception is
8872 ;; for an anchor in a construct like "namespace"[*] - this is as early
8873 ;; as possible in the construct but on the same line as the {.
8874 ;;
8875 ;; [*] i.e. with a keyword matching c-other-block-decl-kwds.
8876 ;;
8877 ;; SYNTAX-EXTRA-ARGS are a list of the extra arguments for the
8878 ;; syntax symbol. They are appended after the anchor point.
8879 ;;
8880 ;; If STOP-AT-BOI-ONLY is nil, we can stop in the middle of the line
8881 ;; if the current statement starts there.
8882 ;;
8883 ;; Note: It's not a problem if PAREN-STATE "overshoots"
8884 ;; CONTAINING-SEXP, i.e. contains info about parens further down.
8885 ;;
8886 ;; This function might do hidden buffer changes.
8887
8888 (if (= (point) (c-point 'boi))
8889 ;; This is by far the most common case, so let's give it special
8890 ;; treatment.
8891 (apply 'c-add-syntax syntax-symbol (point) syntax-extra-args)
8892
8893 (let ((syntax-last c-syntactic-context)
8894 (boi (c-point 'boi))
8895 ;; Set when we're on a label, so that we don't stop there.
8896 ;; FIXME: To be complete we should check if we're on a label
8897 ;; now at the start.
8898 on-label)
8899
8900 ;; Use point as the anchor point for "namespace", "extern", etc.
8901 (apply 'c-add-syntax syntax-symbol
8902 (if (rassq syntax-symbol c-other-decl-block-key-in-symbols-alist)
8903 (point) nil)
8904 syntax-extra-args)
8905
8906 ;; Loop while we have to back out of containing blocks.
8907 (while
8908 (and
8909 (catch 'back-up-block
8910
8911 ;; Loop while we have to back up statements.
8912 (while (or (/= (point) boi)
8913 on-label
8914 (looking-at c-comment-start-regexp))
8915
8916 ;; Skip past any comments that stands between the
8917 ;; statement start and boi.
8918 (let ((savepos (point)))
8919 (while (and (/= savepos boi)
8920 (c-backward-single-comment))
8921 (setq savepos (point)
8922 boi (c-point 'boi)))
8923 (goto-char savepos))
8924
8925 ;; Skip to the beginning of this statement or backward
8926 ;; another one.
8927 (let ((old-pos (point))
8928 (old-boi boi)
8929 (step-type (c-beginning-of-statement-1 containing-sexp)))
8930 (setq boi (c-point 'boi)
8931 on-label (eq step-type 'label))
8932
8933 (cond ((= (point) old-pos)
8934 ;; If we didn't move we're at the start of a block and
8935 ;; have to continue outside it.
8936 (throw 'back-up-block t))
8937
8938 ((and (eq step-type 'up)
8939 (>= (point) old-boi)
8940 (looking-at "else\\>[^_]")
8941 (save-excursion
8942 (goto-char old-pos)
8943 (looking-at "if\\>[^_]")))
8944 ;; Special case to avoid deeper and deeper indentation
8945 ;; of "else if" clauses.
8946 )
8947
8948 ((and (not stop-at-boi-only)
8949 (/= old-pos old-boi)
8950 (memq step-type '(up previous)))
8951 ;; If stop-at-boi-only is nil, we shouldn't back up
8952 ;; over previous or containing statements to try to
8953 ;; reach boi, so go back to the last position and
8954 ;; exit.
8955 (goto-char old-pos)
8956 (throw 'back-up-block nil))
8957
8958 (t
8959 (if (and (not stop-at-boi-only)
8960 (memq step-type '(up previous beginning)))
8961 ;; If we've moved into another statement then we
8962 ;; should no longer try to stop in the middle of a
8963 ;; line.
8964 (setq stop-at-boi-only t))
8965
8966 ;; Record this as a substatement if we skipped up one
8967 ;; level.
8968 (when (eq step-type 'up)
8969 (c-add-syntax 'substatement nil))))
8970 )))
8971
8972 containing-sexp)
8973
8974 ;; Now we have to go out of this block.
8975 (goto-char containing-sexp)
8976
8977 ;; Don't stop in the middle of a special brace list opener
8978 ;; like "({".
8979 (when c-special-brace-lists
8980 (let ((special-list (c-looking-at-special-brace-list)))
8981 (when (and special-list
8982 (< (car (car special-list)) (point)))
8983 (setq containing-sexp (car (car special-list)))
8984 (goto-char containing-sexp))))
8985
8986 (setq paren-state (c-whack-state-after containing-sexp paren-state)
8987 containing-sexp (c-most-enclosing-brace paren-state)
8988 boi (c-point 'boi))
8989
8990 ;; Analyze the construct in front of the block we've stepped out
8991 ;; from and add the right syntactic element for it.
8992 (let ((paren-pos (point))
8993 (paren-char (char-after))
8994 step-type)
8995
8996 (if (eq paren-char ?\()
8997 ;; Stepped out of a parenthesis block, so we're in an
8998 ;; expression now.
8999 (progn
9000 (when (/= paren-pos boi)
9001 (if (and c-recognize-paren-inexpr-blocks
9002 (progn
9003 (c-backward-syntactic-ws containing-sexp)
9004 (or (not (looking-at "\\>"))
9005 (not (c-on-identifier))))
9006 (save-excursion
9007 (goto-char (1+ paren-pos))
9008 (c-forward-syntactic-ws)
9009 (eq (char-after) ?{)))
9010 ;; Stepped out of an in-expression statement. This
9011 ;; syntactic element won't get an anchor pos.
9012 (c-add-syntax 'inexpr-statement)
9013
9014 ;; A parenthesis normally belongs to an arglist.
9015 (c-add-syntax 'arglist-cont-nonempty nil paren-pos)))
9016
9017 (goto-char (max boi
9018 (if containing-sexp
9019 (1+ containing-sexp)
9020 (point-min))))
9021 (setq step-type 'same
9022 on-label nil))
9023
9024 ;; Stepped out of a brace block.
9025 (setq step-type (c-beginning-of-statement-1 containing-sexp)
9026 on-label (eq step-type 'label))
9027
9028 (if (and (eq step-type 'same)
9029 (/= paren-pos (point)))
9030 (let (inexpr)
9031 (cond
9032 ((save-excursion
9033 (goto-char paren-pos)
9034 (setq inexpr (c-looking-at-inexpr-block
9035 (c-safe-position containing-sexp paren-state)
9036 containing-sexp)))
9037 (c-add-syntax (if (eq (car inexpr) 'inlambda)
9038 'defun-block-intro
9039 'statement-block-intro)
9040 nil))
9041 ((looking-at c-other-decl-block-key)
9042 (c-add-syntax
9043 (cdr (assoc (match-string 1)
9044 c-other-decl-block-key-in-symbols-alist))
9045 (max (c-point 'boi paren-pos) (point))))
9046 (t (c-add-syntax 'defun-block-intro nil))))
9047
9048 (c-add-syntax 'statement-block-intro nil)))
9049
9050 (if (= paren-pos boi)
9051 ;; Always done if the open brace was at boi. The
9052 ;; c-beginning-of-statement-1 call above is necessary
9053 ;; anyway, to decide the type of block-intro to add.
9054 (goto-char paren-pos)
9055 (setq boi (c-point 'boi)))
9056 ))
9057
9058 ;; Fill in the current point as the anchor for all the symbols
9059 ;; added above.
9060 (let ((p c-syntactic-context) q)
9061 (while (not (eq p syntax-last))
9062 (setq q (cdr (car p))) ; e.g. (nil 28) [from (arglist-cont-nonempty nil 28)]
9063 (while q
9064 (unless (car q)
9065 (setcar q (point)))
9066 (setq q (cdr q)))
9067 (setq p (cdr p))))
9068 )))
9069
9070 (defun c-add-class-syntax (symbol
9071 containing-decl-open
9072 containing-decl-start
9073 containing-decl-kwd
9074 paren-state)
9075 ;; The inclass and class-close syntactic symbols are added in
9076 ;; several places and some work is needed to fix everything.
9077 ;; Therefore it's collected here.
9078 ;;
9079 ;; This function might do hidden buffer changes.
9080 (goto-char containing-decl-open)
9081 (if (and (eq symbol 'inclass) (= (point) (c-point 'boi)))
9082 (progn
9083 (c-add-syntax symbol containing-decl-open)
9084 containing-decl-open)
9085 (goto-char containing-decl-start)
9086 ;; Ought to use `c-add-stmt-syntax' instead of backing up to boi
9087 ;; here, but we have to do like this for compatibility.
9088 (back-to-indentation)
9089 (c-add-syntax symbol (point))
9090 (if (and (c-keyword-member containing-decl-kwd
9091 'c-inexpr-class-kwds)
9092 (/= containing-decl-start (c-point 'boi containing-decl-start)))
9093 (c-add-syntax 'inexpr-class))
9094 (point)))
9095
9096 (defun c-guess-continued-construct (indent-point
9097 char-after-ip
9098 beg-of-same-or-containing-stmt
9099 containing-sexp
9100 paren-state)
9101 ;; This function contains the decision tree reached through both
9102 ;; cases 18 and 10. It's a continued statement or top level
9103 ;; construct of some kind.
9104 ;;
9105 ;; This function might do hidden buffer changes.
9106
9107 (let (special-brace-list placeholder)
9108 (goto-char indent-point)
9109 (skip-chars-forward " \t")
9110
9111 (cond
9112 ;; (CASE A removed.)
9113 ;; CASE B: open braces for class or brace-lists
9114 ((setq special-brace-list
9115 (or (and c-special-brace-lists
9116 (c-looking-at-special-brace-list))
9117 (eq char-after-ip ?{)))
9118
9119 (cond
9120 ;; CASE B.1: class-open
9121 ((save-excursion
9122 (and (eq (char-after) ?{)
9123 (c-looking-at-decl-block containing-sexp t)
9124 (setq beg-of-same-or-containing-stmt (point))))
9125 (c-add-syntax 'class-open beg-of-same-or-containing-stmt))
9126
9127 ;; CASE B.2: brace-list-open
9128 ((or (consp special-brace-list)
9129 (save-excursion
9130 (goto-char beg-of-same-or-containing-stmt)
9131 (c-syntactic-re-search-forward "=\\([^=]\\|$\\)"
9132 indent-point t t t)))
9133 ;; The most semantically accurate symbol here is
9134 ;; brace-list-open, but we normally report it simply as a
9135 ;; statement-cont. The reason is that one normally adjusts
9136 ;; brace-list-open for brace lists as top-level constructs,
9137 ;; and brace lists inside statements is a completely different
9138 ;; context. C.f. case 5A.3.
9139 (c-beginning-of-statement-1 containing-sexp)
9140 (c-add-stmt-syntax (if c-auto-newline-analysis
9141 ;; Turn off the dwim above when we're
9142 ;; analyzing the nature of the brace
9143 ;; for the auto newline feature.
9144 'brace-list-open
9145 'statement-cont)
9146 nil nil
9147 containing-sexp paren-state))
9148
9149 ;; CASE B.3: The body of a function declared inside a normal
9150 ;; block. Can occur e.g. in Pike and when using gcc
9151 ;; extensions, but watch out for macros followed by blocks.
9152 ;; C.f. cases E, 16F and 17G.
9153 ((and (not (c-at-statement-start-p))
9154 (eq (c-beginning-of-statement-1 containing-sexp nil nil t)
9155 'same)
9156 (save-excursion
9157 (let ((c-recognize-typeless-decls nil))
9158 ;; Turn off recognition of constructs that lacks a
9159 ;; type in this case, since that's more likely to be
9160 ;; a macro followed by a block.
9161 (c-forward-decl-or-cast-1 (c-point 'bosws) nil nil))))
9162 (c-add-stmt-syntax 'defun-open nil t
9163 containing-sexp paren-state))
9164
9165 ;; CASE B.4: Continued statement with block open. The most
9166 ;; accurate analysis is perhaps `statement-cont' together with
9167 ;; `block-open' but we play DWIM and use `substatement-open'
9168 ;; instead. The rationale is that this typically is a macro
9169 ;; followed by a block which makes it very similar to a
9170 ;; statement with a substatement block.
9171 (t
9172 (c-add-stmt-syntax 'substatement-open nil nil
9173 containing-sexp paren-state))
9174 ))
9175
9176 ;; CASE C: iostream insertion or extraction operator
9177 ((and (looking-at "\\(<<\\|>>\\)\\([^=]\\|$\\)")
9178 (save-excursion
9179 (goto-char beg-of-same-or-containing-stmt)
9180 ;; If there is no preceding streamop in the statement
9181 ;; then indent this line as a normal statement-cont.
9182 (when (c-syntactic-re-search-forward
9183 "\\(<<\\|>>\\)\\([^=]\\|$\\)" indent-point 'move t t)
9184 (c-add-syntax 'stream-op (c-point 'boi))
9185 t))))
9186
9187 ;; CASE E: In the "K&R region" of a function declared inside a
9188 ;; normal block. C.f. case B.3.
9189 ((and (save-excursion
9190 ;; Check that the next token is a '{'. This works as
9191 ;; long as no language that allows nested function
9192 ;; definitions allows stuff like member init lists, K&R
9193 ;; declarations or throws clauses there.
9194 ;;
9195 ;; Note that we do a forward search for something ahead
9196 ;; of the indentation line here. That's not good since
9197 ;; the user might not have typed it yet. Unfortunately
9198 ;; it's exceedingly tricky to recognize a function
9199 ;; prototype in a code block without resorting to this.
9200 (c-forward-syntactic-ws)
9201 (eq (char-after) ?{))
9202 (not (c-at-statement-start-p))
9203 (eq (c-beginning-of-statement-1 containing-sexp nil nil t)
9204 'same)
9205 (save-excursion
9206 (let ((c-recognize-typeless-decls nil))
9207 ;; Turn off recognition of constructs that lacks a
9208 ;; type in this case, since that's more likely to be
9209 ;; a macro followed by a block.
9210 (c-forward-decl-or-cast-1 (c-point 'bosws) nil nil))))
9211 (c-add-stmt-syntax 'func-decl-cont nil t
9212 containing-sexp paren-state))
9213
9214 ;;CASE F: continued statement and the only preceding items are
9215 ;;annotations.
9216 ((and (c-major-mode-is 'java-mode)
9217 (setq placeholder (point))
9218 (c-beginning-of-statement-1)
9219 (progn
9220 (while (and (c-forward-annotation)
9221 (< (point) placeholder))
9222 (c-forward-syntactic-ws))
9223 t)
9224 (prog1
9225 (>= (point) placeholder)
9226 (goto-char placeholder)))
9227 (c-beginning-of-statement-1 containing-sexp)
9228 (c-add-syntax 'annotation-var-cont (point)))
9229
9230 ;; CASE G: a template list continuation?
9231 ;; Mostly a duplication of case 5D.3 to fix templates-19:
9232 ((and (c-major-mode-is 'c++-mode)
9233 (save-excursion
9234 (goto-char indent-point)
9235 (c-with-syntax-table c++-template-syntax-table
9236 (setq placeholder (c-up-list-backward)))
9237 (and placeholder
9238 (eq (char-after placeholder) ?<)
9239 (/= (char-before placeholder) ?<)
9240 (progn
9241 (goto-char (1+ placeholder))
9242 (not (looking-at c-<-op-cont-regexp))))))
9243 (c-with-syntax-table c++-template-syntax-table
9244 (goto-char placeholder)
9245 (c-beginning-of-statement-1 containing-sexp t)
9246 (if (save-excursion
9247 (c-backward-syntactic-ws containing-sexp)
9248 (eq (char-before) ?<))
9249 ;; In a nested template arglist.
9250 (progn
9251 (goto-char placeholder)
9252 (c-syntactic-skip-backward "^,;" containing-sexp t)
9253 (c-forward-syntactic-ws))
9254 (back-to-indentation)))
9255 ;; FIXME: Should use c-add-stmt-syntax, but it's not yet
9256 ;; template aware.
9257 (c-add-syntax 'template-args-cont (point) placeholder))
9258
9259 ;; CASE D: continued statement.
9260 (t
9261 (c-beginning-of-statement-1 containing-sexp)
9262 (c-add-stmt-syntax 'statement-cont nil nil
9263 containing-sexp paren-state))
9264 )))
9265
9266 ;; The next autoload was added by RMS on 2005/8/9 - don't know why (ACM,
9267 ;; 2005/11/29).
9268 ;;;###autoload
9269 (defun c-guess-basic-syntax ()
9270 "Return the syntactic context of the current line."
9271 (save-excursion
9272 (beginning-of-line)
9273 (c-save-buffer-state
9274 ((indent-point (point))
9275 (case-fold-search nil)
9276 ;; A whole ugly bunch of various temporary variables. Have
9277 ;; to declare them here since it's not possible to declare
9278 ;; a variable with only the scope of a cond test and the
9279 ;; following result clauses, and most of this function is a
9280 ;; single gigantic cond. :P
9281 literal char-before-ip before-ws-ip char-after-ip macro-start
9282 in-macro-expr c-syntactic-context placeholder c-in-literal-cache
9283 step-type tmpsymbol keyword injava-inher special-brace-list tmp-pos
9284 containing-<
9285 ;; The following record some positions for the containing
9286 ;; declaration block if we're directly within one:
9287 ;; `containing-decl-open' is the position of the open
9288 ;; brace. `containing-decl-start' is the start of the
9289 ;; declaration. `containing-decl-kwd' is the keyword
9290 ;; symbol of the keyword that tells what kind of block it
9291 ;; is.
9292 containing-decl-open
9293 containing-decl-start
9294 containing-decl-kwd
9295 ;; The open paren of the closest surrounding sexp or nil if
9296 ;; there is none.
9297 containing-sexp
9298 ;; The position after the closest preceding brace sexp
9299 ;; (nested sexps are ignored), or the position after
9300 ;; `containing-sexp' if there is none, or (point-min) if
9301 ;; `containing-sexp' is nil.
9302 lim
9303 ;; The paren state outside `containing-sexp', or at
9304 ;; `indent-point' if `containing-sexp' is nil.
9305 (paren-state (c-parse-state))
9306 ;; There's always at most one syntactic element which got
9307 ;; an anchor pos. It's stored in syntactic-relpos.
9308 syntactic-relpos
9309 (c-stmt-delim-chars c-stmt-delim-chars))
9310
9311 ;; Check if we're directly inside an enclosing declaration
9312 ;; level block.
9313 (when (and (setq containing-sexp
9314 (c-most-enclosing-brace paren-state))
9315 (progn
9316 (goto-char containing-sexp)
9317 (eq (char-after) ?{))
9318 (setq placeholder
9319 (c-looking-at-decl-block
9320 (c-most-enclosing-brace paren-state
9321 containing-sexp)
9322 t)))
9323 (setq containing-decl-open containing-sexp
9324 containing-decl-start (point)
9325 containing-sexp nil)
9326 (goto-char placeholder)
9327 (setq containing-decl-kwd (and (looking-at c-keywords-regexp)
9328 (c-keyword-sym (match-string 1)))))
9329
9330 ;; Init some position variables.
9331 (if c-state-cache
9332 (progn
9333 (setq containing-sexp (car paren-state)
9334 paren-state (cdr paren-state))
9335 (if (consp containing-sexp)
9336 (progn
9337 (setq lim (cdr containing-sexp))
9338 (if (cdr c-state-cache)
9339 ;; Ignore balanced paren. The next entry
9340 ;; can't be another one.
9341 (setq containing-sexp (car (cdr c-state-cache))
9342 paren-state (cdr paren-state))
9343 ;; If there is no surrounding open paren then
9344 ;; put the last balanced pair back on paren-state.
9345 (setq paren-state (cons containing-sexp paren-state)
9346 containing-sexp nil)))
9347 (setq lim (1+ containing-sexp))))
9348 (setq lim (point-min)))
9349 (when (c-beginning-of-macro)
9350 (goto-char indent-point)
9351 (let ((lim1 (c-determine-limit 2000)))
9352 (setq lim (max lim lim1))))
9353
9354 ;; If we're in a parenthesis list then ',' delimits the
9355 ;; "statements" rather than being an operator (with the
9356 ;; exception of the "for" clause). This difference is
9357 ;; typically only noticeable when statements are used in macro
9358 ;; arglists.
9359 (when (and containing-sexp
9360 (eq (char-after containing-sexp) ?\())
9361 (setq c-stmt-delim-chars c-stmt-delim-chars-with-comma))
9362 ;; cache char before and after indent point, and move point to
9363 ;; the most likely position to perform the majority of tests
9364 (goto-char indent-point)
9365 (c-backward-syntactic-ws lim)
9366 (setq before-ws-ip (point)
9367 char-before-ip (char-before))
9368 (goto-char indent-point)
9369 (skip-chars-forward " \t")
9370 (setq char-after-ip (char-after))
9371
9372 ;; are we in a literal?
9373 (setq literal (c-in-literal lim))
9374
9375 ;; now figure out syntactic qualities of the current line
9376 (cond
9377
9378 ;; CASE 1: in a string.
9379 ((eq literal 'string)
9380 (c-add-syntax 'string (c-point 'bopl)))
9381
9382 ;; CASE 2: in a C or C++ style comment.
9383 ((and (memq literal '(c c++))
9384 ;; This is a kludge for XEmacs where we use
9385 ;; `buffer-syntactic-context', which doesn't correctly
9386 ;; recognize "\*/" to end a block comment.
9387 ;; `parse-partial-sexp' which is used by
9388 ;; `c-literal-limits' will however do that in most
9389 ;; versions, which results in that we get nil from
9390 ;; `c-literal-limits' even when `c-in-literal' claims
9391 ;; we're inside a comment.
9392 (setq placeholder (c-literal-limits lim)))
9393 (c-add-syntax literal (car placeholder)))
9394
9395 ;; CASE 3: in a cpp preprocessor macro continuation.
9396 ((and (save-excursion
9397 (when (c-beginning-of-macro)
9398 (setq macro-start (point))))
9399 (/= macro-start (c-point 'boi))
9400 (progn
9401 (setq tmpsymbol 'cpp-macro-cont)
9402 (or (not c-syntactic-indentation-in-macros)
9403 (save-excursion
9404 (goto-char macro-start)
9405 ;; If at the beginning of the body of a #define
9406 ;; directive then analyze as cpp-define-intro
9407 ;; only. Go on with the syntactic analysis
9408 ;; otherwise. in-macro-expr is set if we're in a
9409 ;; cpp expression, i.e. before the #define body
9410 ;; or anywhere in a non-#define directive.
9411 (if (c-forward-to-cpp-define-body)
9412 (let ((indent-boi (c-point 'boi indent-point)))
9413 (setq in-macro-expr (> (point) indent-boi)
9414 tmpsymbol 'cpp-define-intro)
9415 (= (point) indent-boi))
9416 (setq in-macro-expr t)
9417 nil)))))
9418 (c-add-syntax tmpsymbol macro-start)
9419 (setq macro-start nil))
9420
9421 ;; CASE 11: an else clause?
9422 ((looking-at "else\\>[^_]")
9423 (c-beginning-of-statement-1 containing-sexp)
9424 (c-add-stmt-syntax 'else-clause nil t
9425 containing-sexp paren-state))
9426
9427 ;; CASE 12: while closure of a do/while construct?
9428 ((and (looking-at "while\\>[^_]")
9429 (save-excursion
9430 (prog1 (eq (c-beginning-of-statement-1 containing-sexp)
9431 'beginning)
9432 (setq placeholder (point)))))
9433 (goto-char placeholder)
9434 (c-add-stmt-syntax 'do-while-closure nil t
9435 containing-sexp paren-state))
9436
9437 ;; CASE 13: A catch or finally clause? This case is simpler
9438 ;; than if-else and do-while, because a block is required
9439 ;; after every try, catch and finally.
9440 ((save-excursion
9441 (and (cond ((c-major-mode-is 'c++-mode)
9442 (looking-at "catch\\>[^_]"))
9443 ((c-major-mode-is 'java-mode)
9444 (looking-at "\\(catch\\|finally\\)\\>[^_]")))
9445 (and (c-safe (c-backward-syntactic-ws)
9446 (c-backward-sexp)
9447 t)
9448 (eq (char-after) ?{)
9449 (c-safe (c-backward-syntactic-ws)
9450 (c-backward-sexp)
9451 t)
9452 (if (eq (char-after) ?\()
9453 (c-safe (c-backward-sexp) t)
9454 t))
9455 (looking-at "\\(try\\|catch\\)\\>[^_]")
9456 (setq placeholder (point))))
9457 (goto-char placeholder)
9458 (c-add-stmt-syntax 'catch-clause nil t
9459 containing-sexp paren-state))
9460
9461 ;; CASE 18: A substatement we can recognize by keyword.
9462 ((save-excursion
9463 (and c-opt-block-stmt-key
9464 (not (eq char-before-ip ?\;))
9465 (not (c-at-vsemi-p before-ws-ip))
9466 (not (memq char-after-ip '(?\) ?\] ?,)))
9467 (or (not (eq char-before-ip ?}))
9468 (c-looking-at-inexpr-block-backward c-state-cache))
9469 (> (point)
9470 (progn
9471 ;; Ought to cache the result from the
9472 ;; c-beginning-of-statement-1 calls here.
9473 (setq placeholder (point))
9474 (while (eq (setq step-type
9475 (c-beginning-of-statement-1 lim))
9476 'label))
9477 (if (eq step-type 'previous)
9478 (goto-char placeholder)
9479 (setq placeholder (point))
9480 (if (and (eq step-type 'same)
9481 (not (looking-at c-opt-block-stmt-key)))
9482 ;; Step up to the containing statement if we
9483 ;; stayed in the same one.
9484 (let (step)
9485 (while (eq
9486 (setq step
9487 (c-beginning-of-statement-1 lim))
9488 'label))
9489 (if (eq step 'up)
9490 (setq placeholder (point))
9491 ;; There was no containing statement after all.
9492 (goto-char placeholder)))))
9493 placeholder))
9494 (if (looking-at c-block-stmt-2-key)
9495 ;; Require a parenthesis after these keywords.
9496 ;; Necessary to catch e.g. synchronized in Java,
9497 ;; which can be used both as statement and
9498 ;; modifier.
9499 (and (zerop (c-forward-token-2 1 nil))
9500 (eq (char-after) ?\())
9501 (looking-at c-opt-block-stmt-key))))
9502
9503 (if (eq step-type 'up)
9504 ;; CASE 18A: Simple substatement.
9505 (progn
9506 (goto-char placeholder)
9507 (cond
9508 ((eq char-after-ip ?{)
9509 (c-add-stmt-syntax 'substatement-open nil nil
9510 containing-sexp paren-state))
9511 ((save-excursion
9512 (goto-char indent-point)
9513 (back-to-indentation)
9514 (c-forward-label))
9515 (c-add-stmt-syntax 'substatement-label nil nil
9516 containing-sexp paren-state))
9517 (t
9518 (c-add-stmt-syntax 'substatement nil nil
9519 containing-sexp paren-state))))
9520
9521 ;; CASE 18B: Some other substatement. This is shared
9522 ;; with case 10.
9523 (c-guess-continued-construct indent-point
9524 char-after-ip
9525 placeholder
9526 lim
9527 paren-state)))
9528
9529 ;; CASE 14: A case or default label
9530 ((looking-at c-label-kwds-regexp)
9531 (if containing-sexp
9532 (progn
9533 (goto-char containing-sexp)
9534 (setq lim (c-most-enclosing-brace c-state-cache
9535 containing-sexp))
9536 (c-backward-to-block-anchor lim)
9537 (c-add-stmt-syntax 'case-label nil t lim paren-state))
9538 ;; Got a bogus label at the top level. In lack of better
9539 ;; alternatives, anchor it on (point-min).
9540 (c-add-syntax 'case-label (point-min))))
9541
9542 ;; CASE 15: any other label
9543 ((save-excursion
9544 (back-to-indentation)
9545 (and (not (looking-at c-syntactic-ws-start))
9546 (c-forward-label)))
9547 (cond (containing-decl-open
9548 (setq placeholder (c-add-class-syntax 'inclass
9549 containing-decl-open
9550 containing-decl-start
9551 containing-decl-kwd
9552 paren-state))
9553 ;; Append access-label with the same anchor point as
9554 ;; inclass gets.
9555 (c-append-syntax 'access-label placeholder))
9556
9557 (containing-sexp
9558 (goto-char containing-sexp)
9559 (setq lim (c-most-enclosing-brace c-state-cache
9560 containing-sexp))
9561 (save-excursion
9562 (setq tmpsymbol
9563 (if (and (eq (c-beginning-of-statement-1 lim) 'up)
9564 (looking-at "switch\\>[^_]"))
9565 ;; If the surrounding statement is a switch then
9566 ;; let's analyze all labels as switch labels, so
9567 ;; that they get lined up consistently.
9568 'case-label
9569 'label)))
9570 (c-backward-to-block-anchor lim)
9571 (c-add-stmt-syntax tmpsymbol nil t lim paren-state))
9572
9573 (t
9574 ;; A label on the top level. Treat it as a class
9575 ;; context. (point-min) is the closest we get to the
9576 ;; class open brace.
9577 (c-add-syntax 'access-label (point-min)))))
9578
9579 ;; CASE 4: In-expression statement. C.f. cases 7B, 16A and
9580 ;; 17E.
9581 ((setq placeholder (c-looking-at-inexpr-block
9582 (c-safe-position containing-sexp paren-state)
9583 containing-sexp
9584 ;; Have to turn on the heuristics after
9585 ;; the point even though it doesn't work
9586 ;; very well. C.f. test case class-16.pike.
9587 t))
9588 (setq tmpsymbol (assq (car placeholder)
9589 '((inexpr-class . class-open)
9590 (inexpr-statement . block-open))))
9591 (if tmpsymbol
9592 ;; It's a statement block or an anonymous class.
9593 (setq tmpsymbol (cdr tmpsymbol))
9594 ;; It's a Pike lambda. Check whether we are between the
9595 ;; lambda keyword and the argument list or at the defun
9596 ;; opener.
9597 (setq tmpsymbol (if (eq char-after-ip ?{)
9598 'inline-open
9599 'lambda-intro-cont)))
9600 (goto-char (cdr placeholder))
9601 (back-to-indentation)
9602 (c-add-stmt-syntax tmpsymbol nil t
9603 (c-most-enclosing-brace c-state-cache (point))
9604 paren-state)
9605 (unless (eq (point) (cdr placeholder))
9606 (c-add-syntax (car placeholder))))
9607
9608 ;; CASE 5: Line is inside a declaration level block or at top level.
9609 ((or containing-decl-open (null containing-sexp))
9610 (cond
9611
9612 ;; CASE 5A: we are looking at a defun, brace list, class,
9613 ;; or inline-inclass method opening brace
9614 ((setq special-brace-list
9615 (or (and c-special-brace-lists
9616 (c-looking-at-special-brace-list))
9617 (eq char-after-ip ?{)))
9618 (cond
9619
9620 ;; CASE 5A.1: Non-class declaration block open.
9621 ((save-excursion
9622 (let (tmp)
9623 (and (eq char-after-ip ?{)
9624 (setq tmp (c-looking-at-decl-block containing-sexp t))
9625 (progn
9626 (setq placeholder (point))
9627 (goto-char tmp)
9628 (looking-at c-symbol-key))
9629 (c-keyword-member
9630 (c-keyword-sym (setq keyword (match-string 0)))
9631 'c-other-block-decl-kwds))))
9632 (goto-char placeholder)
9633 (c-add-stmt-syntax
9634 (if (string-equal keyword "extern")
9635 ;; Special case for extern-lang-open.
9636 'extern-lang-open
9637 (intern (concat keyword "-open")))
9638 nil t containing-sexp paren-state))
9639
9640 ;; CASE 5A.2: we are looking at a class opening brace
9641 ((save-excursion
9642 (goto-char indent-point)
9643 (skip-chars-forward " \t")
9644 (and (eq (char-after) ?{)
9645 (c-looking-at-decl-block containing-sexp t)
9646 (setq placeholder (point))))
9647 (c-add-syntax 'class-open placeholder))
9648
9649 ;; CASE 5A.3: brace list open
9650 ((save-excursion
9651 (c-beginning-of-decl-1 lim)
9652 (while (looking-at c-specifier-key)
9653 (goto-char (match-end 1))
9654 (c-forward-syntactic-ws indent-point))
9655 (setq placeholder (c-point 'boi))
9656 (or (consp special-brace-list)
9657 (and (or (save-excursion
9658 (goto-char indent-point)
9659 (setq tmpsymbol nil)
9660 (while (and (> (point) placeholder)
9661 (zerop (c-backward-token-2 1 t))
9662 (not (looking-at "=\\([^=]\\|$\\)")))
9663 (and c-opt-inexpr-brace-list-key
9664 (not tmpsymbol)
9665 (looking-at c-opt-inexpr-brace-list-key)
9666 (setq tmpsymbol 'topmost-intro-cont)))
9667 (looking-at "=\\([^=]\\|$\\)"))
9668 (looking-at c-brace-list-key))
9669 (save-excursion
9670 (while (and (< (point) indent-point)
9671 (zerop (c-forward-token-2 1 t))
9672 (not (memq (char-after) '(?\; ?\()))))
9673 (not (memq (char-after) '(?\; ?\()))
9674 ))))
9675 (if (and (not c-auto-newline-analysis)
9676 (c-major-mode-is 'java-mode)
9677 (eq tmpsymbol 'topmost-intro-cont))
9678 ;; We're in Java and have found that the open brace
9679 ;; belongs to a "new Foo[]" initialization list,
9680 ;; which means the brace list is part of an
9681 ;; expression and not a top level definition. We
9682 ;; therefore treat it as any topmost continuation
9683 ;; even though the semantically correct symbol still
9684 ;; is brace-list-open, on the same grounds as in
9685 ;; case B.2.
9686 (progn
9687 (c-beginning-of-statement-1 lim)
9688 (c-add-syntax 'topmost-intro-cont (c-point 'boi)))
9689 (c-add-syntax 'brace-list-open placeholder)))
9690
9691 ;; CASE 5A.4: inline defun open
9692 ((and containing-decl-open
9693 (not (c-keyword-member containing-decl-kwd
9694 'c-other-block-decl-kwds)))
9695 (c-add-syntax 'inline-open)
9696 (c-add-class-syntax 'inclass
9697 containing-decl-open
9698 containing-decl-start
9699 containing-decl-kwd
9700 paren-state))
9701
9702 ;; CASE 5A.5: ordinary defun open
9703 (t
9704 (save-excursion
9705 (c-beginning-of-decl-1 lim)
9706 (while (looking-at c-specifier-key)
9707 (goto-char (match-end 1))
9708 (c-forward-syntactic-ws indent-point))
9709 (c-add-syntax 'defun-open (c-point 'boi))
9710 ;; Bogus to use bol here, but it's the legacy. (Resolved,
9711 ;; 2007-11-09)
9712 ))))
9713
9714 ;; CASE 5B: After a function header but before the body (or
9715 ;; the ending semicolon if there's no body).
9716 ((save-excursion
9717 (when (setq placeholder (c-just-after-func-arglist-p
9718 (max lim (c-determine-limit 500))))
9719 (setq tmp-pos (point))))
9720 (cond
9721
9722 ;; CASE 5B.1: Member init list.
9723 ((eq (char-after tmp-pos) ?:)
9724 (if (or (>= tmp-pos indent-point)
9725 (= (c-point 'bosws) (1+ tmp-pos)))
9726 (progn
9727 ;; There is no preceding member init clause.
9728 ;; Indent relative to the beginning of indentation
9729 ;; for the topmost-intro line that contains the
9730 ;; prototype's open paren.
9731 (goto-char placeholder)
9732 (c-add-syntax 'member-init-intro (c-point 'boi)))
9733 ;; Indent relative to the first member init clause.
9734 (goto-char (1+ tmp-pos))
9735 (c-forward-syntactic-ws)
9736 (c-add-syntax 'member-init-cont (point))))
9737
9738 ;; CASE 5B.2: K&R arg decl intro
9739 ((and c-recognize-knr-p
9740 (c-in-knr-argdecl lim))
9741 (c-beginning-of-statement-1 lim)
9742 (c-add-syntax 'knr-argdecl-intro (c-point 'boi))
9743 (if containing-decl-open
9744 (c-add-class-syntax 'inclass
9745 containing-decl-open
9746 containing-decl-start
9747 containing-decl-kwd
9748 paren-state)))
9749
9750 ;; CASE 5B.4: Nether region after a C++ or Java func
9751 ;; decl, which could include a `throws' declaration.
9752 (t
9753 (c-beginning-of-statement-1 lim)
9754 (c-add-syntax 'func-decl-cont (c-point 'boi))
9755 )))
9756
9757 ;; CASE 5C: inheritance line. could be first inheritance
9758 ;; line, or continuation of a multiple inheritance
9759 ((or (and (c-major-mode-is 'c++-mode)
9760 (progn
9761 (when (eq char-after-ip ?,)
9762 (skip-chars-forward " \t")
9763 (forward-char))
9764 (looking-at c-opt-postfix-decl-spec-key)))
9765 (and (or (eq char-before-ip ?:)
9766 ;; watch out for scope operator
9767 (save-excursion
9768 (and (eq char-after-ip ?:)
9769 (c-safe (forward-char 1) t)
9770 (not (eq (char-after) ?:))
9771 )))
9772 (save-excursion
9773 (c-backward-syntactic-ws lim)
9774 (if (eq char-before-ip ?:)
9775 (progn
9776 (forward-char -1)
9777 (c-backward-syntactic-ws lim)))
9778 (back-to-indentation)
9779 (looking-at c-class-key)))
9780 ;; for Java
9781 (and (c-major-mode-is 'java-mode)
9782 (let ((fence (save-excursion
9783 (c-beginning-of-statement-1 lim)
9784 (point)))
9785 cont done)
9786 (save-excursion
9787 (while (not done)
9788 (cond ((looking-at c-opt-postfix-decl-spec-key)
9789 (setq injava-inher (cons cont (point))
9790 done t))
9791 ((or (not (c-safe (c-forward-sexp -1) t))
9792 (<= (point) fence))
9793 (setq done t))
9794 )
9795 (setq cont t)))
9796 injava-inher)
9797 (not (c-crosses-statement-barrier-p (cdr injava-inher)
9798 (point)))
9799 ))
9800 (cond
9801
9802 ;; CASE 5C.1: non-hanging colon on an inher intro
9803 ((eq char-after-ip ?:)
9804 (c-beginning-of-statement-1 lim)
9805 (c-add-syntax 'inher-intro (c-point 'boi))
9806 ;; don't add inclass symbol since relative point already
9807 ;; contains any class offset
9808 )
9809
9810 ;; CASE 5C.2: hanging colon on an inher intro
9811 ((eq char-before-ip ?:)
9812 (c-beginning-of-statement-1 lim)
9813 (c-add-syntax 'inher-intro (c-point 'boi))
9814 (if containing-decl-open
9815 (c-add-class-syntax 'inclass
9816 containing-decl-open
9817 containing-decl-start
9818 containing-decl-kwd
9819 paren-state)))
9820
9821 ;; CASE 5C.3: in a Java implements/extends
9822 (injava-inher
9823 (let ((where (cdr injava-inher))
9824 (cont (car injava-inher)))
9825 (goto-char where)
9826 (cond ((looking-at "throws\\>[^_]")
9827 (c-add-syntax 'func-decl-cont
9828 (progn (c-beginning-of-statement-1 lim)
9829 (c-point 'boi))))
9830 (cont (c-add-syntax 'inher-cont where))
9831 (t (c-add-syntax 'inher-intro
9832 (progn (goto-char (cdr injava-inher))
9833 (c-beginning-of-statement-1 lim)
9834 (point))))
9835 )))
9836
9837 ;; CASE 5C.4: a continued inheritance line
9838 (t
9839 (c-beginning-of-inheritance-list lim)
9840 (c-add-syntax 'inher-cont (point))
9841 ;; don't add inclass symbol since relative point already
9842 ;; contains any class offset
9843 )))
9844
9845 ;; CASE 5P: AWK pattern or function or continuation
9846 ;; thereof.
9847 ((c-major-mode-is 'awk-mode)
9848 (setq placeholder (point))
9849 (c-add-stmt-syntax
9850 (if (and (eq (c-beginning-of-statement-1) 'same)
9851 (/= (point) placeholder))
9852 'topmost-intro-cont
9853 'topmost-intro)
9854 nil nil
9855 containing-sexp paren-state))
9856
9857 ;; CASE 5D: this could be a top-level initialization, a
9858 ;; member init list continuation, or a template argument
9859 ;; list continuation.
9860 ((save-excursion
9861 ;; Note: We use the fact that lim is always after any
9862 ;; preceding brace sexp.
9863 (if c-recognize-<>-arglists
9864 (while (and
9865 (progn
9866 (c-syntactic-skip-backward "^;,=<>" lim t)
9867 (> (point) lim))
9868 (or
9869 (when c-overloadable-operators-regexp
9870 (when (setq placeholder (c-after-special-operator-id lim))
9871 (goto-char placeholder)
9872 t))
9873 (cond
9874 ((eq (char-before) ?>)
9875 (or (c-backward-<>-arglist nil lim)
9876 (backward-char))
9877 t)
9878 ((eq (char-before) ?<)
9879 (backward-char)
9880 (if (save-excursion
9881 (c-forward-<>-arglist nil))
9882 (progn (forward-char)
9883 nil)
9884 t))
9885 (t nil)))))
9886 ;; NB: No c-after-special-operator-id stuff in this
9887 ;; clause - we assume only C++ needs it.
9888 (c-syntactic-skip-backward "^;,=" lim t))
9889 (memq (char-before) '(?, ?= ?<)))
9890 (cond
9891
9892 ;; CASE 5D.3: perhaps a template list continuation?
9893 ((and (c-major-mode-is 'c++-mode)
9894 (save-excursion
9895 (save-restriction
9896 (c-with-syntax-table c++-template-syntax-table
9897 (goto-char indent-point)
9898 (setq placeholder (c-up-list-backward))
9899 (and placeholder
9900 (eq (char-after placeholder) ?<))))))
9901 (c-with-syntax-table c++-template-syntax-table
9902 (goto-char placeholder)
9903 (c-beginning-of-statement-1 lim t)
9904 (if (save-excursion
9905 (c-backward-syntactic-ws lim)
9906 (eq (char-before) ?<))
9907 ;; In a nested template arglist.
9908 (progn
9909 (goto-char placeholder)
9910 (c-syntactic-skip-backward "^,;" lim t)
9911 (c-forward-syntactic-ws))
9912 (back-to-indentation)))
9913 ;; FIXME: Should use c-add-stmt-syntax, but it's not yet
9914 ;; template aware.
9915 (c-add-syntax 'template-args-cont (point) placeholder))
9916
9917 ;; CASE 5D.4: perhaps a multiple inheritance line?
9918 ((and (c-major-mode-is 'c++-mode)
9919 (save-excursion
9920 (c-beginning-of-statement-1 lim)
9921 (setq placeholder (point))
9922 (if (looking-at "static\\>[^_]")
9923 (c-forward-token-2 1 nil indent-point))
9924 (and (looking-at c-class-key)
9925 (zerop (c-forward-token-2 2 nil indent-point))
9926 (if (eq (char-after) ?<)
9927 (c-with-syntax-table c++-template-syntax-table
9928 (zerop (c-forward-token-2 1 t indent-point)))
9929 t)
9930 (eq (char-after) ?:))))
9931 (goto-char placeholder)
9932 (c-add-syntax 'inher-cont (c-point 'boi)))
9933
9934 ;; CASE 5D.5: Continuation of the "expression part" of a
9935 ;; top level construct. Or, perhaps, an unrecognized construct.
9936 (t
9937 (while (and (setq placeholder (point))
9938 (eq (car (c-beginning-of-decl-1 containing-sexp)) ; Can't use `lim' here.
9939 'same)
9940 (save-excursion
9941 (c-backward-syntactic-ws)
9942 (eq (char-before) ?}))
9943 (< (point) placeholder)))
9944 (c-add-stmt-syntax
9945 (cond
9946 ((eq (point) placeholder) 'statement) ; unrecognized construct
9947 ;; A preceding comma at the top level means that a
9948 ;; new variable declaration starts here. Use
9949 ;; topmost-intro-cont for it, for consistency with
9950 ;; the first variable declaration. C.f. case 5N.
9951 ((eq char-before-ip ?,) 'topmost-intro-cont)
9952 (t 'statement-cont))
9953 nil nil containing-sexp paren-state))
9954 ))
9955
9956 ;; CASE 5F: Close of a non-class declaration level block.
9957 ((and (eq char-after-ip ?})
9958 (c-keyword-member containing-decl-kwd
9959 'c-other-block-decl-kwds))
9960 ;; This is inconsistent: Should use `containing-decl-open'
9961 ;; here if it's at boi, like in case 5J.
9962 (goto-char containing-decl-start)
9963 (c-add-stmt-syntax
9964 (if (string-equal (symbol-name containing-decl-kwd) "extern")
9965 ;; Special case for compatibility with the
9966 ;; extern-lang syntactic symbols.
9967 'extern-lang-close
9968 (intern (concat (symbol-name containing-decl-kwd)
9969 "-close")))
9970 nil t
9971 (c-most-enclosing-brace paren-state (point))
9972 paren-state))
9973
9974 ;; CASE 5G: we are looking at the brace which closes the
9975 ;; enclosing nested class decl
9976 ((and containing-sexp
9977 (eq char-after-ip ?})
9978 (eq containing-decl-open containing-sexp))
9979 (c-add-class-syntax 'class-close
9980 containing-decl-open
9981 containing-decl-start
9982 containing-decl-kwd
9983 paren-state))
9984
9985 ;; CASE 5H: we could be looking at subsequent knr-argdecls
9986 ((and c-recognize-knr-p
9987 (not containing-sexp) ; can't be knr inside braces.
9988 (not (eq char-before-ip ?}))
9989 (save-excursion
9990 (setq placeholder (cdr (c-beginning-of-decl-1 lim)))
9991 (and placeholder
9992 ;; Do an extra check to avoid tripping up on
9993 ;; statements that occur in invalid contexts
9994 ;; (e.g. in macro bodies where we don't really
9995 ;; know the context of what we're looking at).
9996 (not (and c-opt-block-stmt-key
9997 (looking-at c-opt-block-stmt-key)))))
9998 (< placeholder indent-point))
9999 (goto-char placeholder)
10000 (c-add-syntax 'knr-argdecl (point)))
10001
10002 ;; CASE 5I: ObjC method definition.
10003 ((and c-opt-method-key
10004 (looking-at c-opt-method-key))
10005 (c-beginning-of-statement-1 nil t)
10006 (if (= (point) indent-point)
10007 ;; Handle the case when it's the first (non-comment)
10008 ;; thing in the buffer. Can't look for a 'same return
10009 ;; value from cbos1 since ObjC directives currently
10010 ;; aren't recognized fully, so that we get 'same
10011 ;; instead of 'previous if it moved over a preceding
10012 ;; directive.
10013 (goto-char (point-min)))
10014 (c-add-syntax 'objc-method-intro (c-point 'boi)))
10015
10016 ;; CASE 5N: At a variable declaration that follows a class
10017 ;; definition or some other block declaration that doesn't
10018 ;; end at the closing '}'. C.f. case 5D.5.
10019 ((progn
10020 (c-backward-syntactic-ws lim)
10021 (and (eq (char-before) ?})
10022 (save-excursion
10023 (let ((start (point)))
10024 (if (and c-state-cache
10025 (consp (car c-state-cache))
10026 (eq (cdar c-state-cache) (point)))
10027 ;; Speed up the backward search a bit.
10028 (goto-char (caar c-state-cache)))
10029 (c-beginning-of-decl-1 containing-sexp) ; Can't use `lim' here.
10030 (setq placeholder (point))
10031 (if (= start (point))
10032 ;; The '}' is unbalanced.
10033 nil
10034 (c-end-of-decl-1)
10035 (>= (point) indent-point))))))
10036 (goto-char placeholder)
10037 (c-add-stmt-syntax 'topmost-intro-cont nil nil
10038 containing-sexp paren-state))
10039
10040 ;; NOTE: The point is at the end of the previous token here.
10041
10042 ;; CASE 5J: we are at the topmost level, make
10043 ;; sure we skip back past any access specifiers
10044 ((and
10045 ;; A macro continuation line is never at top level.
10046 (not (and macro-start
10047 (> indent-point macro-start)))
10048 (save-excursion
10049 (setq placeholder (point))
10050 (or (memq char-before-ip '(?\; ?{ ?} nil))
10051 (c-at-vsemi-p before-ws-ip)
10052 (when (and (eq char-before-ip ?:)
10053 (eq (c-beginning-of-statement-1 lim)
10054 'label))
10055 (c-backward-syntactic-ws lim)
10056 (setq placeholder (point)))
10057 (and (c-major-mode-is 'objc-mode)
10058 (catch 'not-in-directive
10059 (c-beginning-of-statement-1 lim)
10060 (setq placeholder (point))
10061 (while (and (c-forward-objc-directive)
10062 (< (point) indent-point))
10063 (c-forward-syntactic-ws)
10064 (if (>= (point) indent-point)
10065 (throw 'not-in-directive t))
10066 (setq placeholder (point)))
10067 nil)))))
10068 ;; For historic reasons we anchor at bol of the last
10069 ;; line of the previous declaration. That's clearly
10070 ;; highly bogus and useless, and it makes our lives hard
10071 ;; to remain compatible. :P
10072 (goto-char placeholder)
10073 (c-add-syntax 'topmost-intro (c-point 'bol))
10074 (if containing-decl-open
10075 (if (c-keyword-member containing-decl-kwd
10076 'c-other-block-decl-kwds)
10077 (progn
10078 (goto-char (c-brace-anchor-point containing-decl-open))
10079 (c-add-stmt-syntax
10080 (if (string-equal (symbol-name containing-decl-kwd)
10081 "extern")
10082 ;; Special case for compatibility with the
10083 ;; extern-lang syntactic symbols.
10084 'inextern-lang
10085 (intern (concat "in"
10086 (symbol-name containing-decl-kwd))))
10087 nil t
10088 (c-most-enclosing-brace paren-state (point))
10089 paren-state))
10090 (c-add-class-syntax 'inclass
10091 containing-decl-open
10092 containing-decl-start
10093 containing-decl-kwd
10094 paren-state)))
10095 (when (and c-syntactic-indentation-in-macros
10096 macro-start
10097 (/= macro-start (c-point 'boi indent-point)))
10098 (c-add-syntax 'cpp-define-intro)
10099 (setq macro-start nil)))
10100
10101 ;; CASE 5K: we are at an ObjC method definition
10102 ;; continuation line.
10103 ((and c-opt-method-key
10104 (save-excursion
10105 (c-beginning-of-statement-1 lim)
10106 (beginning-of-line)
10107 (when (looking-at c-opt-method-key)
10108 (setq placeholder (point)))))
10109 (c-add-syntax 'objc-method-args-cont placeholder))
10110
10111 ;; CASE 5L: we are at the first argument of a template
10112 ;; arglist that begins on the previous line.
10113 ((and c-recognize-<>-arglists
10114 (eq (char-before) ?<)
10115 (not (and c-overloadable-operators-regexp
10116 (c-after-special-operator-id lim))))
10117 (c-beginning-of-statement-1 (c-safe-position (point) paren-state))
10118 (c-add-syntax 'template-args-cont (c-point 'boi)))
10119
10120 ;; CASE 5Q: we are at a statement within a macro.
10121 (macro-start
10122 (c-beginning-of-statement-1 containing-sexp)
10123 (c-add-stmt-syntax 'statement nil t containing-sexp paren-state))
10124
10125 ;;CASE 5N: We are at a topmost continuation line and the only
10126 ;;preceding items are annotations.
10127 ((and (c-major-mode-is 'java-mode)
10128 (setq placeholder (point))
10129 (c-beginning-of-statement-1)
10130 (progn
10131 (while (and (c-forward-annotation))
10132 (c-forward-syntactic-ws))
10133 t)
10134 (prog1
10135 (>= (point) placeholder)
10136 (goto-char placeholder)))
10137 (c-add-syntax 'annotation-top-cont (c-point 'boi)))
10138
10139 ;; CASE 5M: we are at a topmost continuation line
10140 (t
10141 (c-beginning-of-statement-1 (c-safe-position (point) paren-state))
10142 (when (c-major-mode-is 'objc-mode)
10143 (setq placeholder (point))
10144 (while (and (c-forward-objc-directive)
10145 (< (point) indent-point))
10146 (c-forward-syntactic-ws)
10147 (setq placeholder (point)))
10148 (goto-char placeholder))
10149 (c-add-syntax 'topmost-intro-cont (c-point 'boi)))
10150 ))
10151
10152
10153 ;; (CASE 6 has been removed.)
10154
10155 ;; CASE 7: line is an expression, not a statement. Most
10156 ;; likely we are either in a function prototype or a function
10157 ;; call argument list
10158 ((not (or (and c-special-brace-lists
10159 (save-excursion
10160 (goto-char containing-sexp)
10161 (c-looking-at-special-brace-list)))
10162 (eq (char-after containing-sexp) ?{)))
10163 (cond
10164
10165 ;; CASE 7A: we are looking at the arglist closing paren.
10166 ;; C.f. case 7F.
10167 ((memq char-after-ip '(?\) ?\]))
10168 (goto-char containing-sexp)
10169 (setq placeholder (c-point 'boi))
10170 (if (and (c-safe (backward-up-list 1) t)
10171 (>= (point) placeholder))
10172 (progn
10173 (forward-char)
10174 (skip-chars-forward " \t"))
10175 (goto-char placeholder))
10176 (c-add-stmt-syntax 'arglist-close (list containing-sexp) t
10177 (c-most-enclosing-brace paren-state (point))
10178 paren-state))
10179
10180 ;; CASE 7B: Looking at the opening brace of an
10181 ;; in-expression block or brace list. C.f. cases 4, 16A
10182 ;; and 17E.
10183 ((and (eq char-after-ip ?{)
10184 (progn
10185 (setq placeholder (c-inside-bracelist-p (point)
10186 paren-state))
10187 (if placeholder
10188 (setq tmpsymbol '(brace-list-open . inexpr-class))
10189 (setq tmpsymbol '(block-open . inexpr-statement)
10190 placeholder
10191 (cdr-safe (c-looking-at-inexpr-block
10192 (c-safe-position containing-sexp
10193 paren-state)
10194 containing-sexp)))
10195 ;; placeholder is nil if it's a block directly in
10196 ;; a function arglist. That makes us skip out of
10197 ;; this case.
10198 )))
10199 (goto-char placeholder)
10200 (back-to-indentation)
10201 (c-add-stmt-syntax (car tmpsymbol) nil t
10202 (c-most-enclosing-brace paren-state (point))
10203 paren-state)
10204 (if (/= (point) placeholder)
10205 (c-add-syntax (cdr tmpsymbol))))
10206
10207 ;; CASE 7C: we are looking at the first argument in an empty
10208 ;; argument list. Use arglist-close if we're actually
10209 ;; looking at a close paren or bracket.
10210 ((memq char-before-ip '(?\( ?\[))
10211 (goto-char containing-sexp)
10212 (setq placeholder (c-point 'boi))
10213 (if (and (c-safe (backward-up-list 1) t)
10214 (>= (point) placeholder))
10215 (progn
10216 (forward-char)
10217 (skip-chars-forward " \t"))
10218 (goto-char placeholder))
10219 (c-add-stmt-syntax 'arglist-intro (list containing-sexp) t
10220 (c-most-enclosing-brace paren-state (point))
10221 paren-state))
10222
10223 ;; CASE 7D: we are inside a conditional test clause. treat
10224 ;; these things as statements
10225 ((progn
10226 (goto-char containing-sexp)
10227 (and (c-safe (c-forward-sexp -1) t)
10228 (looking-at "\\<for\\>[^_]")))
10229 (goto-char (1+ containing-sexp))
10230 (c-forward-syntactic-ws indent-point)
10231 (if (eq char-before-ip ?\;)
10232 (c-add-syntax 'statement (point))
10233 (c-add-syntax 'statement-cont (point))
10234 ))
10235
10236 ;; CASE 7E: maybe a continued ObjC method call. This is the
10237 ;; case when we are inside a [] bracketed exp, and what
10238 ;; precede the opening bracket is not an identifier.
10239 ((and c-opt-method-key
10240 (eq (char-after containing-sexp) ?\[)
10241 (progn
10242 (goto-char (1- containing-sexp))
10243 (c-backward-syntactic-ws (c-point 'bod))
10244 (if (not (looking-at c-symbol-key))
10245 (c-add-syntax 'objc-method-call-cont containing-sexp))
10246 )))
10247
10248 ;; CASE 7F: we are looking at an arglist continuation line,
10249 ;; but the preceding argument is on the same line as the
10250 ;; opening paren. This case includes multi-line
10251 ;; mathematical paren groupings, but we could be on a
10252 ;; for-list continuation line. C.f. case 7A.
10253 ((progn
10254 (goto-char (1+ containing-sexp))
10255 (< (save-excursion
10256 (c-forward-syntactic-ws)
10257 (point))
10258 (c-point 'bonl)))
10259 (goto-char containing-sexp) ; paren opening the arglist
10260 (setq placeholder (c-point 'boi))
10261 (if (and (c-safe (backward-up-list 1) t)
10262 (>= (point) placeholder))
10263 (progn
10264 (forward-char)
10265 (skip-chars-forward " \t"))
10266 (goto-char placeholder))
10267 (c-add-stmt-syntax 'arglist-cont-nonempty (list containing-sexp) t
10268 (c-most-enclosing-brace c-state-cache (point))
10269 paren-state))
10270
10271 ;; CASE 7G: we are looking at just a normal arglist
10272 ;; continuation line
10273 (t (c-forward-syntactic-ws indent-point)
10274 (c-add-syntax 'arglist-cont (c-point 'boi)))
10275 ))
10276
10277 ;; CASE 8: func-local multi-inheritance line
10278 ((and (c-major-mode-is 'c++-mode)
10279 (save-excursion
10280 (goto-char indent-point)
10281 (skip-chars-forward " \t")
10282 (looking-at c-opt-postfix-decl-spec-key)))
10283 (goto-char indent-point)
10284 (skip-chars-forward " \t")
10285 (cond
10286
10287 ;; CASE 8A: non-hanging colon on an inher intro
10288 ((eq char-after-ip ?:)
10289 (c-backward-syntactic-ws lim)
10290 (c-add-syntax 'inher-intro (c-point 'boi)))
10291
10292 ;; CASE 8B: hanging colon on an inher intro
10293 ((eq char-before-ip ?:)
10294 (c-add-syntax 'inher-intro (c-point 'boi)))
10295
10296 ;; CASE 8C: a continued inheritance line
10297 (t
10298 (c-beginning-of-inheritance-list lim)
10299 (c-add-syntax 'inher-cont (point))
10300 )))
10301
10302 ;; CASE 9: we are inside a brace-list
10303 ((and (not (c-major-mode-is 'awk-mode)) ; Maybe this isn't needed (ACM, 2002/3/29)
10304 (setq special-brace-list
10305 (or (and c-special-brace-lists ;;;; ALWAYS NIL FOR AWK!!
10306 (save-excursion
10307 (goto-char containing-sexp)
10308 (c-looking-at-special-brace-list)))
10309 (c-inside-bracelist-p containing-sexp paren-state))))
10310 (cond
10311
10312 ;; CASE 9A: In the middle of a special brace list opener.
10313 ((and (consp special-brace-list)
10314 (save-excursion
10315 (goto-char containing-sexp)
10316 (eq (char-after) ?\())
10317 (eq char-after-ip (car (cdr special-brace-list))))
10318 (goto-char (car (car special-brace-list)))
10319 (skip-chars-backward " \t")
10320 (if (and (bolp)
10321 (assoc 'statement-cont
10322 (setq placeholder (c-guess-basic-syntax))))
10323 (setq c-syntactic-context placeholder)
10324 (c-beginning-of-statement-1
10325 (c-safe-position (1- containing-sexp) paren-state))
10326 (c-forward-token-2 0)
10327 (while (looking-at c-specifier-key)
10328 (goto-char (match-end 1))
10329 (c-forward-syntactic-ws))
10330 (c-add-syntax 'brace-list-open (c-point 'boi))))
10331
10332 ;; CASE 9B: brace-list-close brace
10333 ((if (consp special-brace-list)
10334 ;; Check special brace list closer.
10335 (progn
10336 (goto-char (car (car special-brace-list)))
10337 (save-excursion
10338 (goto-char indent-point)
10339 (back-to-indentation)
10340 (or
10341 ;; We were between the special close char and the `)'.
10342 (and (eq (char-after) ?\))
10343 (eq (1+ (point)) (cdr (car special-brace-list))))
10344 ;; We were before the special close char.
10345 (and (eq (char-after) (cdr (cdr special-brace-list)))
10346 (zerop (c-forward-token-2))
10347 (eq (1+ (point)) (cdr (car special-brace-list)))))))
10348 ;; Normal brace list check.
10349 (and (eq char-after-ip ?})
10350 (c-safe (goto-char (c-up-list-backward (point))) t)
10351 (= (point) containing-sexp)))
10352 (if (eq (point) (c-point 'boi))
10353 (c-add-syntax 'brace-list-close (point))
10354 (setq lim (c-most-enclosing-brace c-state-cache (point)))
10355 (c-beginning-of-statement-1 lim)
10356 (c-add-stmt-syntax 'brace-list-close nil t lim paren-state)))
10357
10358 (t
10359 ;; Prepare for the rest of the cases below by going to the
10360 ;; token following the opening brace
10361 (if (consp special-brace-list)
10362 (progn
10363 (goto-char (car (car special-brace-list)))
10364 (c-forward-token-2 1 nil indent-point))
10365 (goto-char containing-sexp))
10366 (forward-char)
10367 (let ((start (point)))
10368 (c-forward-syntactic-ws indent-point)
10369 (goto-char (max start (c-point 'bol))))
10370 (c-skip-ws-forward indent-point)
10371 (cond
10372
10373 ;; CASE 9C: we're looking at the first line in a brace-list
10374 ((= (point) indent-point)
10375 (if (consp special-brace-list)
10376 (goto-char (car (car special-brace-list)))
10377 (goto-char containing-sexp))
10378 (if (eq (point) (c-point 'boi))
10379 (c-add-syntax 'brace-list-intro (point))
10380 (setq lim (c-most-enclosing-brace c-state-cache (point)))
10381 (c-beginning-of-statement-1 lim)
10382 (c-add-stmt-syntax 'brace-list-intro nil t lim paren-state)))
10383
10384 ;; CASE 9D: this is just a later brace-list-entry or
10385 ;; brace-entry-open
10386 (t (if (or (eq char-after-ip ?{)
10387 (and c-special-brace-lists
10388 (save-excursion
10389 (goto-char indent-point)
10390 (c-forward-syntactic-ws (c-point 'eol))
10391 (c-looking-at-special-brace-list (point)))))
10392 (c-add-syntax 'brace-entry-open (point))
10393 (c-add-syntax 'brace-list-entry (point))
10394 ))
10395 ))))
10396
10397 ;; CASE 10: A continued statement or top level construct.
10398 ((and (not (memq char-before-ip '(?\; ?:)))
10399 (not (c-at-vsemi-p before-ws-ip))
10400 (or (not (eq char-before-ip ?}))
10401 (c-looking-at-inexpr-block-backward c-state-cache))
10402 (> (point)
10403 (save-excursion
10404 (c-beginning-of-statement-1 containing-sexp)
10405 (setq placeholder (point))))
10406 (/= placeholder containing-sexp))
10407 ;; This is shared with case 18.
10408 (c-guess-continued-construct indent-point
10409 char-after-ip
10410 placeholder
10411 containing-sexp
10412 paren-state))
10413
10414 ;; CASE 16: block close brace, possibly closing the defun or
10415 ;; the class
10416 ((eq char-after-ip ?})
10417 ;; From here on we have the next containing sexp in lim.
10418 (setq lim (c-most-enclosing-brace paren-state))
10419 (goto-char containing-sexp)
10420 (cond
10421
10422 ;; CASE 16E: Closing a statement block? This catches
10423 ;; cases where it's preceded by a statement keyword,
10424 ;; which works even when used in an "invalid" context,
10425 ;; e.g. a macro argument.
10426 ((c-after-conditional)
10427 (c-backward-to-block-anchor lim)
10428 (c-add-stmt-syntax 'block-close nil t lim paren-state))
10429
10430 ;; CASE 16A: closing a lambda defun or an in-expression
10431 ;; block? C.f. cases 4, 7B and 17E.
10432 ((setq placeholder (c-looking-at-inexpr-block
10433 (c-safe-position containing-sexp paren-state)
10434 nil))
10435 (setq tmpsymbol (if (eq (car placeholder) 'inlambda)
10436 'inline-close
10437 'block-close))
10438 (goto-char containing-sexp)
10439 (back-to-indentation)
10440 (if (= containing-sexp (point))
10441 (c-add-syntax tmpsymbol (point))
10442 (goto-char (cdr placeholder))
10443 (back-to-indentation)
10444 (c-add-stmt-syntax tmpsymbol nil t
10445 (c-most-enclosing-brace paren-state (point))
10446 paren-state)
10447 (if (/= (point) (cdr placeholder))
10448 (c-add-syntax (car placeholder)))))
10449
10450 ;; CASE 16B: does this close an inline or a function in
10451 ;; a non-class declaration level block?
10452 ((save-excursion
10453 (and lim
10454 (progn
10455 (goto-char lim)
10456 (c-looking-at-decl-block
10457 (c-most-enclosing-brace paren-state lim)
10458 nil))
10459 (setq placeholder (point))))
10460 (c-backward-to-decl-anchor lim)
10461 (back-to-indentation)
10462 (if (save-excursion
10463 (goto-char placeholder)
10464 (looking-at c-other-decl-block-key))
10465 (c-add-syntax 'defun-close (point))
10466 (c-add-syntax 'inline-close (point))))
10467
10468 ;; CASE 16F: Can be a defun-close of a function declared
10469 ;; in a statement block, e.g. in Pike or when using gcc
10470 ;; extensions, but watch out for macros followed by
10471 ;; blocks. Let it through to be handled below.
10472 ;; C.f. cases B.3 and 17G.
10473 ((save-excursion
10474 (and (not (c-at-statement-start-p))
10475 (eq (c-beginning-of-statement-1 lim nil nil t) 'same)
10476 (setq placeholder (point))
10477 (let ((c-recognize-typeless-decls nil))
10478 ;; Turn off recognition of constructs that
10479 ;; lacks a type in this case, since that's more
10480 ;; likely to be a macro followed by a block.
10481 (c-forward-decl-or-cast-1 (c-point 'bosws) nil nil))))
10482 (back-to-indentation)
10483 (if (/= (point) containing-sexp)
10484 (goto-char placeholder))
10485 (c-add-stmt-syntax 'defun-close nil t lim paren-state))
10486
10487 ;; CASE 16C: If there is an enclosing brace then this is
10488 ;; a block close since defun closes inside declaration
10489 ;; level blocks have been handled above.
10490 (lim
10491 ;; If the block is preceded by a case/switch label on
10492 ;; the same line, we anchor at the first preceding label
10493 ;; at boi. The default handling in c-add-stmt-syntax
10494 ;; really fixes it better, but we do like this to keep
10495 ;; the indentation compatible with version 5.28 and
10496 ;; earlier. C.f. case 17H.
10497 (while (and (/= (setq placeholder (point)) (c-point 'boi))
10498 (eq (c-beginning-of-statement-1 lim) 'label)))
10499 (goto-char placeholder)
10500 (if (looking-at c-label-kwds-regexp)
10501 (c-add-syntax 'block-close (point))
10502 (goto-char containing-sexp)
10503 ;; c-backward-to-block-anchor not necessary here; those
10504 ;; situations are handled in case 16E above.
10505 (c-add-stmt-syntax 'block-close nil t lim paren-state)))
10506
10507 ;; CASE 16D: Only top level defun close left.
10508 (t
10509 (goto-char containing-sexp)
10510 (c-backward-to-decl-anchor lim)
10511 (c-add-stmt-syntax 'defun-close nil nil
10512 (c-most-enclosing-brace paren-state)
10513 paren-state))
10514 ))
10515
10516 ;; CASE 19: line is an expression, not a statement, and is directly
10517 ;; contained by a template delimiter. Most likely, we are in a
10518 ;; template arglist within a statement. This case is based on CASE
10519 ;; 7. At some point in the future, we may wish to create more
10520 ;; syntactic symbols such as `template-intro',
10521 ;; `template-cont-nonempty', etc., and distinguish between them as we
10522 ;; do for `arglist-intro' etc. (2009-12-07).
10523 ((and c-recognize-<>-arglists
10524 (setq containing-< (c-up-list-backward indent-point containing-sexp))
10525 (eq (char-after containing-<) ?\<))
10526 (setq placeholder (c-point 'boi containing-<))
10527 (goto-char containing-sexp) ; Most nested Lbrace/Lparen (but not
10528 ; '<') before indent-point.
10529 (if (>= (point) placeholder)
10530 (progn
10531 (forward-char)
10532 (skip-chars-forward " \t"))
10533 (goto-char placeholder))
10534 (c-add-stmt-syntax 'template-args-cont (list containing-<) t
10535 (c-most-enclosing-brace c-state-cache (point))
10536 paren-state))
10537
10538 ;; CASE 17: Statement or defun catchall.
10539 (t
10540 (goto-char indent-point)
10541 ;; Back up statements until we find one that starts at boi.
10542 (while (let* ((prev-point (point))
10543 (last-step-type (c-beginning-of-statement-1
10544 containing-sexp)))
10545 (if (= (point) prev-point)
10546 (progn
10547 (setq step-type (or step-type last-step-type))
10548 nil)
10549 (setq step-type last-step-type)
10550 (/= (point) (c-point 'boi)))))
10551 (cond
10552
10553 ;; CASE 17B: continued statement
10554 ((and (eq step-type 'same)
10555 (/= (point) indent-point))
10556 (c-add-stmt-syntax 'statement-cont nil nil
10557 containing-sexp paren-state))
10558
10559 ;; CASE 17A: After a case/default label?
10560 ((progn
10561 (while (and (eq step-type 'label)
10562 (not (looking-at c-label-kwds-regexp)))
10563 (setq step-type
10564 (c-beginning-of-statement-1 containing-sexp)))
10565 (eq step-type 'label))
10566 (c-add-stmt-syntax (if (eq char-after-ip ?{)
10567 'statement-case-open
10568 'statement-case-intro)
10569 nil t containing-sexp paren-state))
10570
10571 ;; CASE 17D: any old statement
10572 ((progn
10573 (while (eq step-type 'label)
10574 (setq step-type
10575 (c-beginning-of-statement-1 containing-sexp)))
10576 (eq step-type 'previous))
10577 (c-add-stmt-syntax 'statement nil t
10578 containing-sexp paren-state)
10579 (if (eq char-after-ip ?{)
10580 (c-add-syntax 'block-open)))
10581
10582 ;; CASE 17I: Inside a substatement block.
10583 ((progn
10584 ;; The following tests are all based on containing-sexp.
10585 (goto-char containing-sexp)
10586 ;; From here on we have the next containing sexp in lim.
10587 (setq lim (c-most-enclosing-brace paren-state containing-sexp))
10588 (c-after-conditional))
10589 (c-backward-to-block-anchor lim)
10590 (c-add-stmt-syntax 'statement-block-intro nil t
10591 lim paren-state)
10592 (if (eq char-after-ip ?{)
10593 (c-add-syntax 'block-open)))
10594
10595 ;; CASE 17E: first statement in an in-expression block.
10596 ;; C.f. cases 4, 7B and 16A.
10597 ((setq placeholder (c-looking-at-inexpr-block
10598 (c-safe-position containing-sexp paren-state)
10599 nil))
10600 (setq tmpsymbol (if (eq (car placeholder) 'inlambda)
10601 'defun-block-intro
10602 'statement-block-intro))
10603 (back-to-indentation)
10604 (if (= containing-sexp (point))
10605 (c-add-syntax tmpsymbol (point))
10606 (goto-char (cdr placeholder))
10607 (back-to-indentation)
10608 (c-add-stmt-syntax tmpsymbol nil t
10609 (c-most-enclosing-brace c-state-cache (point))
10610 paren-state)
10611 (if (/= (point) (cdr placeholder))
10612 (c-add-syntax (car placeholder))))
10613 (if (eq char-after-ip ?{)
10614 (c-add-syntax 'block-open)))
10615
10616 ;; CASE 17F: first statement in an inline, or first
10617 ;; statement in a top-level defun. we can tell this is it
10618 ;; if there are no enclosing braces that haven't been
10619 ;; narrowed out by a class (i.e. don't use bod here).
10620 ((save-excursion
10621 (or (not (setq placeholder (c-most-enclosing-brace
10622 paren-state)))
10623 (and (progn
10624 (goto-char placeholder)
10625 (eq (char-after) ?{))
10626 (c-looking-at-decl-block (c-most-enclosing-brace
10627 paren-state (point))
10628 nil))))
10629 (c-backward-to-decl-anchor lim)
10630 (back-to-indentation)
10631 (c-add-syntax 'defun-block-intro (point)))
10632
10633 ;; CASE 17G: First statement in a function declared inside
10634 ;; a normal block. This can occur in Pike and with
10635 ;; e.g. the gcc extensions, but watch out for macros
10636 ;; followed by blocks. C.f. cases B.3 and 16F.
10637 ((save-excursion
10638 (and (not (c-at-statement-start-p))
10639 (eq (c-beginning-of-statement-1 lim nil nil t) 'same)
10640 (setq placeholder (point))
10641 (let ((c-recognize-typeless-decls nil))
10642 ;; Turn off recognition of constructs that lacks
10643 ;; a type in this case, since that's more likely
10644 ;; to be a macro followed by a block.
10645 (c-forward-decl-or-cast-1 (c-point 'bosws) nil nil))))
10646 (back-to-indentation)
10647 (if (/= (point) containing-sexp)
10648 (goto-char placeholder))
10649 (c-add-stmt-syntax 'defun-block-intro nil t
10650 lim paren-state))
10651
10652 ;; CASE 17H: First statement in a block.
10653 (t
10654 ;; If the block is preceded by a case/switch label on the
10655 ;; same line, we anchor at the first preceding label at
10656 ;; boi. The default handling in c-add-stmt-syntax is
10657 ;; really fixes it better, but we do like this to keep the
10658 ;; indentation compatible with version 5.28 and earlier.
10659 ;; C.f. case 16C.
10660 (while (and (/= (setq placeholder (point)) (c-point 'boi))
10661 (eq (c-beginning-of-statement-1 lim) 'label)))
10662 (goto-char placeholder)
10663 (if (looking-at c-label-kwds-regexp)
10664 (c-add-syntax 'statement-block-intro (point))
10665 (goto-char containing-sexp)
10666 ;; c-backward-to-block-anchor not necessary here; those
10667 ;; situations are handled in case 17I above.
10668 (c-add-stmt-syntax 'statement-block-intro nil t
10669 lim paren-state))
10670 (if (eq char-after-ip ?{)
10671 (c-add-syntax 'block-open)))
10672 ))
10673 )
10674
10675 ;; now we need to look at any modifiers
10676 (goto-char indent-point)
10677 (skip-chars-forward " \t")
10678
10679 ;; are we looking at a comment only line?
10680 (when (and (looking-at c-comment-start-regexp)
10681 (/= (c-forward-token-2 0 nil (c-point 'eol)) 0))
10682 (c-append-syntax 'comment-intro))
10683
10684 ;; we might want to give additional offset to friends (in C++).
10685 (when (and c-opt-friend-key
10686 (looking-at c-opt-friend-key))
10687 (c-append-syntax 'friend))
10688
10689 ;; Set syntactic-relpos.
10690 (let ((p c-syntactic-context))
10691 (while (and p
10692 (if (integerp (c-langelem-pos (car p)))
10693 (progn
10694 (setq syntactic-relpos (c-langelem-pos (car p)))
10695 nil)
10696 t))
10697 (setq p (cdr p))))
10698
10699 ;; Start of or a continuation of a preprocessor directive?
10700 (if (and macro-start
10701 (eq macro-start (c-point 'boi))
10702 (not (and (c-major-mode-is 'pike-mode)
10703 (eq (char-after (1+ macro-start)) ?\"))))
10704 (c-append-syntax 'cpp-macro)
10705 (when (and c-syntactic-indentation-in-macros macro-start)
10706 (if in-macro-expr
10707 (when (or
10708 (< syntactic-relpos macro-start)
10709 (not (or
10710 (assq 'arglist-intro c-syntactic-context)
10711 (assq 'arglist-cont c-syntactic-context)
10712 (assq 'arglist-cont-nonempty c-syntactic-context)
10713 (assq 'arglist-close c-syntactic-context))))
10714 ;; If inside a cpp expression, i.e. anywhere in a
10715 ;; cpp directive except a #define body, we only let
10716 ;; through the syntactic analysis that is internal
10717 ;; in the expression. That means the arglist
10718 ;; elements, if they are anchored inside the cpp
10719 ;; expression.
10720 (setq c-syntactic-context nil)
10721 (c-add-syntax 'cpp-macro-cont macro-start))
10722 (when (and (eq macro-start syntactic-relpos)
10723 (not (assq 'cpp-define-intro c-syntactic-context))
10724 (save-excursion
10725 (goto-char macro-start)
10726 (or (not (c-forward-to-cpp-define-body))
10727 (<= (point) (c-point 'boi indent-point)))))
10728 ;; Inside a #define body and the syntactic analysis is
10729 ;; anchored on the start of the #define. In this case
10730 ;; we add cpp-define-intro to get the extra
10731 ;; indentation of the #define body.
10732 (c-add-syntax 'cpp-define-intro)))))
10733
10734 ;; return the syntax
10735 c-syntactic-context)))
10736
10737 \f
10738 ;; Indentation calculation.
10739
10740 (defun c-evaluate-offset (offset langelem symbol)
10741 ;; offset can be a number, a function, a variable, a list, or one of
10742 ;; the symbols + or -
10743 ;;
10744 ;; This function might do hidden buffer changes.
10745 (let ((res
10746 (cond
10747 ((numberp offset) offset)
10748 ((vectorp offset) offset)
10749 ((null offset) nil)
10750
10751 ((eq offset '+) c-basic-offset)
10752 ((eq offset '-) (- c-basic-offset))
10753 ((eq offset '++) (* 2 c-basic-offset))
10754 ((eq offset '--) (* 2 (- c-basic-offset)))
10755 ((eq offset '*) (/ c-basic-offset 2))
10756 ((eq offset '/) (/ (- c-basic-offset) 2))
10757
10758 ((functionp offset)
10759 (c-evaluate-offset
10760 (funcall offset
10761 (cons (c-langelem-sym langelem)
10762 (c-langelem-pos langelem)))
10763 langelem symbol))
10764
10765 ((listp offset)
10766 (cond
10767 ((eq (car offset) 'quote)
10768 (c-benign-error "The offset %S for %s was mistakenly quoted"
10769 offset symbol)
10770 nil)
10771
10772 ((memq (car offset) '(min max))
10773 (let (res val (method (car offset)))
10774 (setq offset (cdr offset))
10775 (while offset
10776 (setq val (c-evaluate-offset (car offset) langelem symbol))
10777 (cond
10778 ((not val))
10779 ((not res)
10780 (setq res val))
10781 ((integerp val)
10782 (if (vectorp res)
10783 (c-benign-error "\
10784 Error evaluating offset %S for %s: \
10785 Cannot combine absolute offset %S with relative %S in `%s' method"
10786 (car offset) symbol res val method)
10787 (setq res (funcall method res val))))
10788 (t
10789 (if (integerp res)
10790 (c-benign-error "\
10791 Error evaluating offset %S for %s: \
10792 Cannot combine relative offset %S with absolute %S in `%s' method"
10793 (car offset) symbol res val method)
10794 (setq res (vector (funcall method (aref res 0)
10795 (aref val 0)))))))
10796 (setq offset (cdr offset)))
10797 res))
10798
10799 ((eq (car offset) 'add)
10800 (let (res val)
10801 (setq offset (cdr offset))
10802 (while offset
10803 (setq val (c-evaluate-offset (car offset) langelem symbol))
10804 (cond
10805 ((not val))
10806 ((not res)
10807 (setq res val))
10808 ((integerp val)
10809 (if (vectorp res)
10810 (setq res (vector (+ (aref res 0) val)))
10811 (setq res (+ res val))))
10812 (t
10813 (if (vectorp res)
10814 (c-benign-error "\
10815 Error evaluating offset %S for %s: \
10816 Cannot combine absolute offsets %S and %S in `add' method"
10817 (car offset) symbol res val)
10818 (setq res val)))) ; Override.
10819 (setq offset (cdr offset)))
10820 res))
10821
10822 (t
10823 (let (res)
10824 (when (eq (car offset) 'first)
10825 (setq offset (cdr offset)))
10826 (while (and (not res) offset)
10827 (setq res (c-evaluate-offset (car offset) langelem symbol)
10828 offset (cdr offset)))
10829 res))))
10830
10831 ((and (symbolp offset) (boundp offset))
10832 (symbol-value offset))
10833
10834 (t
10835 (c-benign-error "Unknown offset format %S for %s" offset symbol)
10836 nil))))
10837
10838 (if (or (null res) (integerp res)
10839 (and (vectorp res) (= (length res) 1) (integerp (aref res 0))))
10840 res
10841 (c-benign-error "Error evaluating offset %S for %s: Got invalid value %S"
10842 offset symbol res)
10843 nil)))
10844
10845 (defun c-calc-offset (langelem)
10846 ;; Get offset from LANGELEM which is a list beginning with the
10847 ;; syntactic symbol and followed by any analysis data it provides.
10848 ;; That data may be zero or more elements, but if at least one is
10849 ;; given then the first is the anchor position (or nil). The symbol
10850 ;; is matched against `c-offsets-alist' and the offset calculated
10851 ;; from that is returned.
10852 ;;
10853 ;; This function might do hidden buffer changes.
10854 (let* ((symbol (c-langelem-sym langelem))
10855 (match (assq symbol c-offsets-alist))
10856 (offset (cdr-safe match)))
10857 (if match
10858 (setq offset (c-evaluate-offset offset langelem symbol))
10859 (if c-strict-syntax-p
10860 (c-benign-error "No offset found for syntactic symbol %s" symbol))
10861 (setq offset 0))
10862 (if (vectorp offset)
10863 offset
10864 (or (and (numberp offset) offset)
10865 (and (symbolp offset) (symbol-value offset))
10866 0))
10867 ))
10868
10869 (defun c-get-offset (langelem)
10870 ;; This is a compatibility wrapper for `c-calc-offset' in case
10871 ;; someone is calling it directly. It takes an old style syntactic
10872 ;; element on the form (SYMBOL . ANCHOR-POS) and converts it to the
10873 ;; new list form.
10874 ;;
10875 ;; This function might do hidden buffer changes.
10876 (if (c-langelem-pos langelem)
10877 (c-calc-offset (list (c-langelem-sym langelem)
10878 (c-langelem-pos langelem)))
10879 (c-calc-offset langelem)))
10880
10881 (defun c-get-syntactic-indentation (langelems)
10882 ;; Calculate the syntactic indentation from a syntactic description
10883 ;; as returned by `c-guess-syntax'.
10884 ;;
10885 ;; Note that topmost-intro always has an anchor position at bol, for
10886 ;; historical reasons. It's often used together with other symbols
10887 ;; that has more sane positions. Since we always use the first
10888 ;; found anchor position, we rely on that these other symbols always
10889 ;; precede topmost-intro in the LANGELEMS list.
10890 ;;
10891 ;; This function might do hidden buffer changes.
10892 (let ((indent 0) anchor)
10893
10894 (while langelems
10895 (let* ((c-syntactic-element (car langelems))
10896 (res (c-calc-offset c-syntactic-element)))
10897
10898 (if (vectorp res)
10899 ;; Got an absolute column that overrides any indentation
10900 ;; we've collected so far, but not the relative
10901 ;; indentation we might get for the nested structures
10902 ;; further down the langelems list.
10903 (setq indent (elt res 0)
10904 anchor (point-min)) ; A position at column 0.
10905
10906 ;; Got a relative change of the current calculated
10907 ;; indentation.
10908 (setq indent (+ indent res))
10909
10910 ;; Use the anchor position from the first syntactic
10911 ;; element with one.
10912 (unless anchor
10913 (setq anchor (c-langelem-pos (car langelems)))))
10914
10915 (setq langelems (cdr langelems))))
10916
10917 (if anchor
10918 (+ indent (save-excursion
10919 (goto-char anchor)
10920 (current-column)))
10921 indent)))
10922
10923 \f
10924 (cc-provide 'cc-engine)
10925
10926 ;;; cc-engine.el ends here