Merge branch 'master' into comment-cache
[emacs.git] / lisp / progmodes / cc-engine.el
blob7f49557c7a65469017bd853a4e44cd3189006c40
1 ;;; cc-engine.el --- core syntax guessing engine for CC mode -*- coding: utf-8 -*-
3 ;; Copyright (C) 1985, 1987, 1992-2017 Free Software Foundation, Inc.
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
16 ;; This file is part of GNU Emacs.
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.
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.
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/>.
31 ;;; Commentary:
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.
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.)
43 ;; Hidden buffer changes
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.
52 ;; Interactive functions are assumed to not do hidden buffer changes,
53 ;; except in the specific parts of them that do real changes.
55 ;; Lineup functions are assumed to do hidden buffer changes. They
56 ;; must not do real changes, though.
58 ;; All other functions that do hidden buffer changes have that noted
59 ;; in their doc string or comment.
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.
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).
71 ;; Use of text properties
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.
77 ;; Note: This doc is for internal use only. Other packages should not
78 ;; assume that these text properties are used as described here.
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.
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, to
87 ;; "hide" obtrusive characters in preprocessor lines, and to mark C++
88 ;; raw strings to enable their fontification.
90 ;; This property is used on single characters and is therefore
91 ;; always treated as front and rear nonsticky (or start and end open
92 ;; in XEmacs vocabulary). It's therefore installed on
93 ;; `text-property-default-nonsticky' if that variable exists (Emacs
94 ;; >= 21).
96 ;; 'c-is-sws and 'c-in-sws
97 ;; Used by `c-forward-syntactic-ws' and `c-backward-syntactic-ws' to
98 ;; speed them up. See the comment blurb before `c-put-is-sws'
99 ;; below for further details.
101 ;; 'c-type
102 ;; This property is used on single characters to mark positions with
103 ;; special syntactic relevance of various sorts. Its primary use is
104 ;; to avoid glitches when multiline constructs are refontified
105 ;; interactively (on font lock decoration level 3). It's cleared in
106 ;; a region before it's fontified and is then put on relevant chars
107 ;; in that region as they are encountered during the fontification.
108 ;; The value specifies the kind of position:
110 ;; 'c-decl-arg-start
111 ;; Put on the last char of the token preceding each declaration
112 ;; inside a declaration style arglist (typically in a function
113 ;; prototype).
115 ;; 'c-decl-end
116 ;; Put on the last char of the token preceding a declaration.
117 ;; This is used in cases where declaration boundaries can't be
118 ;; recognized simply by looking for a token like ";" or "}".
119 ;; `c-type-decl-end-used' must be set if this is used (see also
120 ;; `c-find-decl-spots').
122 ;; 'c-<>-arg-sep
123 ;; Put on the commas that separate arguments in angle bracket
124 ;; arglists like C++ template arglists.
126 ;; 'c-decl-id-start and 'c-decl-type-start
127 ;; Put on the last char of the token preceding each declarator
128 ;; in the declarator list of a declaration. They are also used
129 ;; between the identifiers cases like enum declarations.
130 ;; 'c-decl-type-start is used when the declarators are types,
131 ;; 'c-decl-id-start otherwise.
133 ;; 'c-not-decl
134 ;; Put on the brace which introduces a brace list and on the commas
135 ;; which separate the element within it.
137 ;; 'c-awk-NL-prop
138 ;; Used in AWK mode to mark the various kinds of newlines. See
139 ;; cc-awk.el.
141 ;;; Code:
143 (eval-when-compile
144 (let ((load-path
145 (if (and (boundp 'byte-compile-dest-file)
146 (stringp byte-compile-dest-file))
147 (cons (file-name-directory byte-compile-dest-file) load-path)
148 load-path)))
149 (load "cc-bytecomp" nil t)))
151 (cc-require 'cc-defs)
152 (cc-require-when-compile 'cc-langs)
153 (cc-require 'cc-vars)
155 (eval-when-compile (require 'cl))
158 ;; Make declarations for all the `c-lang-defvar' variables in cc-langs.
160 (defmacro c-declare-lang-variables ()
161 `(progn
162 ,@(c--mapcan (lambda (init)
163 `(,(if (elt init 2)
164 `(defvar ,(car init) nil ,(elt init 2))
165 `(defvar ,(car init) nil))
166 (make-variable-buffer-local ',(car init))))
167 (cdr c-lang-variable-inits))))
168 (c-declare-lang-variables)
171 ;;; Internal state variables.
173 ;; Internal state of hungry delete key feature
174 (defvar c-hungry-delete-key nil)
175 (make-variable-buffer-local 'c-hungry-delete-key)
177 ;; The electric flag (toggled by `c-toggle-electric-state').
178 ;; If t, electric actions (like automatic reindentation, and (if
179 ;; c-auto-newline is also set) auto newlining) will happen when an electric
180 ;; key like `{' is pressed (or an electric keyword like `else').
181 (defvar c-electric-flag t)
182 (make-variable-buffer-local 'c-electric-flag)
184 ;; Internal state of auto newline feature.
185 (defvar c-auto-newline nil)
186 (make-variable-buffer-local 'c-auto-newline)
188 ;; Included in the mode line to indicate the active submodes.
189 ;; (defvar c-submode-indicators nil)
190 ;; (make-variable-buffer-local 'c-submode-indicators)
192 (defun c-calculate-state (arg prevstate)
193 ;; Calculate the new state of PREVSTATE, t or nil, based on arg. If
194 ;; arg is nil or zero, toggle the state. If arg is negative, turn
195 ;; the state off, and if arg is positive, turn the state on
196 (if (or (not arg)
197 (zerop (setq arg (prefix-numeric-value arg))))
198 (not prevstate)
199 (> arg 0)))
202 ;; Basic handling of preprocessor directives.
204 ;; This is a dynamically bound cache used together with
205 ;; `c-query-macro-start' and `c-query-and-set-macro-start'. It only
206 ;; works as long as point doesn't cross a macro boundary.
207 (defvar c-macro-start 'unknown)
209 (defsubst c-query-and-set-macro-start ()
210 (if (symbolp c-macro-start)
211 (setq c-macro-start (save-excursion
212 (c-save-buffer-state ()
213 (and (c-beginning-of-macro)
214 (point)))))
215 c-macro-start))
217 (defsubst c-query-macro-start ()
218 (if (symbolp c-macro-start)
219 (save-excursion
220 (c-save-buffer-state ()
221 (and (c-beginning-of-macro)
222 (point))))
223 c-macro-start))
225 ;; One element macro cache to cope with continual movement within very large
226 ;; CPP macros.
227 (defvar c-macro-cache nil)
228 (make-variable-buffer-local 'c-macro-cache)
229 ;; Nil or cons of the bounds of the most recent CPP form probed by
230 ;; `c-beginning-of-macro', `c-end-of-macro' or `c-syntactic-end-of-macro'.
231 ;; The cdr will be nil if we know only the start of the CPP form.
232 (defvar c-macro-cache-start-pos nil)
233 (make-variable-buffer-local 'c-macro-cache-start-pos)
234 ;; The starting position from where we determined `c-macro-cache'.
235 (defvar c-macro-cache-syntactic nil)
236 (make-variable-buffer-local 'c-macro-cache-syntactic)
237 ;; Either nil, or the syntactic end of the macro currently represented by
238 ;; `c-macro-cache'.
239 (defvar c-macro-cache-no-comment nil)
240 (make-variable-buffer-local 'c-macro-cache-no-comment)
241 ;; Either nil, or the last character of the macro currently represented by
242 ;; `c-macro-cache' which isn't in a comment. */
244 (defun c-invalidate-macro-cache (beg end)
245 ;; Called from a before-change function. If the change region is before or
246 ;; in the macro characterized by `c-macro-cache' etc., nullify it
247 ;; appropriately. BEG and END are the standard before-change-functions
248 ;; parameters. END isn't used.
249 (cond
250 ((null c-macro-cache))
251 ((< beg (car c-macro-cache))
252 (setq c-macro-cache nil
253 c-macro-cache-start-pos nil
254 c-macro-cache-syntactic nil
255 c-macro-cache-no-comment nil))
256 ((and (cdr c-macro-cache)
257 (< beg (cdr c-macro-cache)))
258 (setcdr c-macro-cache nil)
259 (setq c-macro-cache-start-pos beg
260 c-macro-cache-syntactic nil
261 c-macro-cache-no-comment nil))))
263 (defun c-macro-is-genuine-p ()
264 ;; Check that the ostensible CPP construct at point is a real one. In
265 ;; particular, if point is on the first line of a narrowed buffer, make sure
266 ;; that the "#" isn't, say, the second character of a "##" operator. Return
267 ;; t when the macro is real, nil otherwise.
268 (let ((here (point)))
269 (beginning-of-line)
270 (prog1
271 (if (and (eq (point) (point-min))
272 (/= (point) 1))
273 (save-restriction
274 (widen)
275 (beginning-of-line)
276 (and (looking-at c-anchored-cpp-prefix)
277 (eq (match-beginning 1) here)))
279 (goto-char here))))
281 (defun c-beginning-of-macro (&optional lim)
282 "Go to the beginning of a preprocessor directive.
283 Leave point at the beginning of the directive and return t if in one,
284 otherwise return nil and leave point unchanged.
286 Note that this function might do hidden buffer changes. See the
287 comment at the start of cc-engine.el for more info."
288 (let ((here (point)))
289 (when c-opt-cpp-prefix
290 (if (and (car c-macro-cache)
291 (>= (point) (car c-macro-cache))
292 (or (and (cdr c-macro-cache)
293 (<= (point) (cdr c-macro-cache)))
294 (<= (point) c-macro-cache-start-pos)))
295 (unless (< (car c-macro-cache) (or lim (point-min)))
296 (progn (goto-char (max (or lim (point-min)) (car c-macro-cache)))
297 (setq c-macro-cache-start-pos
298 (max c-macro-cache-start-pos here))
300 (setq c-macro-cache nil
301 c-macro-cache-start-pos nil
302 c-macro-cache-syntactic nil
303 c-macro-cache-no-comment nil)
305 (save-restriction
306 (if lim (narrow-to-region lim (point-max)))
307 (beginning-of-line)
308 (while (eq (char-before (1- (point))) ?\\)
309 (forward-line -1))
310 (back-to-indentation)
311 (if (and (<= (point) here)
312 (save-match-data (looking-at c-opt-cpp-start))
313 (c-macro-is-genuine-p))
314 (progn
315 (setq c-macro-cache (cons (point) nil)
316 c-macro-cache-start-pos here
317 c-macro-cache-syntactic nil)
319 (goto-char here)
320 nil))))))
322 (defun c-end-of-macro ()
323 "Go to the end of a preprocessor directive.
324 More accurately, move the point to the end of the closest following
325 line that doesn't end with a line continuation backslash - no check is
326 done that the point is inside a cpp directive to begin with.
328 Note that this function might do hidden buffer changes. See the
329 comment at the start of cc-engine.el for more info."
330 (if (and (cdr c-macro-cache)
331 (<= (point) (cdr c-macro-cache))
332 (>= (point) (car c-macro-cache)))
333 (goto-char (cdr c-macro-cache))
334 (unless (and (car c-macro-cache)
335 (<= (point) c-macro-cache-start-pos)
336 (>= (point) (car c-macro-cache)))
337 (setq c-macro-cache nil
338 c-macro-cache-start-pos nil
339 c-macro-cache-syntactic nil
340 c-macro-cache-no-comment nil))
341 (while (progn
342 (end-of-line)
343 (when (and (eq (char-before) ?\\)
344 (not (eobp)))
345 (forward-char)
346 t)))
347 (when (car c-macro-cache)
348 (setcdr c-macro-cache (point))
349 (setq c-macro-cache-syntactic nil))))
351 (defun c-syntactic-end-of-macro ()
352 ;; Go to the end of a CPP directive, or a "safe" pos just before.
354 ;; This is normally the end of the next non-escaped line. A "safe"
355 ;; position is one not within a string or comment. (The EOL on a line
356 ;; comment is NOT "safe").
358 ;; This function must only be called from the beginning of a CPP construct.
360 ;; Note that this function might do hidden buffer changes. See the comment
361 ;; at the start of cc-engine.el for more info.
362 (let* ((here (point))
363 (there (progn (c-end-of-macro) (point)))
365 (if c-macro-cache-syntactic
366 (goto-char c-macro-cache-syntactic)
367 (setq s (parse-partial-sexp here there))
368 (while (and (or (nth 3 s) ; in a string
369 (and (nth 4 s) ; in a comment (maybe at end of line comment)
370 (not (eq (nth 7 s) 'syntax-table)))) ; Not a pseudo comment
371 (> there here)) ; No infinite loops, please.
372 (setq there (1- (nth 8 s)))
373 (setq s (parse-partial-sexp here there)))
374 (setq c-macro-cache-syntactic (point)))
375 (point)))
377 (defun c-no-comment-end-of-macro ()
378 ;; Go to the end of a CPP directive, or a pos just before which isn't in a
379 ;; comment. For this purpose, open strings are ignored.
381 ;; This function must only be called from the beginning of a CPP construct.
383 ;; Note that this function might do hidden buffer changes. See the comment
384 ;; at the start of cc-engine.el for more info.
385 (let* ((here (point))
386 (there (progn (c-end-of-macro) (point)))
388 (if c-macro-cache-no-comment
389 (goto-char c-macro-cache-no-comment)
390 (setq s (parse-partial-sexp here there))
391 (while (and (nth 3 s) ; in a string
392 (> there here)) ; No infinite loops, please.
393 (setq here (1+ (nth 8 s)))
394 (setq s (parse-partial-sexp here there)))
395 (when (and (nth 4 s)
396 (not (eq (nth 7 s) 'syntax-table))) ; no pseudo comments.
397 (goto-char (1- (nth 8 s))))
398 (setq c-macro-cache-no-comment (point)))
399 (point)))
401 (defun c-forward-over-cpp-define-id ()
402 ;; Assuming point is at the "#" that introduces a preprocessor
403 ;; directive, it's moved forward to the end of the identifier which is
404 ;; "#define"d (or whatever c-opt-cpp-macro-define specifies). Non-nil
405 ;; is returned in this case, in all other cases nil is returned and
406 ;; point isn't moved.
408 ;; This function might do hidden buffer changes.
409 (when (and c-opt-cpp-macro-define-id
410 (looking-at c-opt-cpp-macro-define-id))
411 (goto-char (match-end 0))))
413 (defun c-forward-to-cpp-define-body ()
414 ;; Assuming point is at the "#" that introduces a preprocessor
415 ;; directive, it's moved forward to the start of the definition body
416 ;; if it's a "#define" (or whatever c-opt-cpp-macro-define
417 ;; specifies). Non-nil is returned in this case, in all other cases
418 ;; nil is returned and point isn't moved.
420 ;; This function might do hidden buffer changes.
421 (when (and c-opt-cpp-macro-define-start
422 (looking-at c-opt-cpp-macro-define-start)
423 (not (= (match-end 0) (c-point 'eol))))
424 (goto-char (match-end 0))))
427 ;;; Basic utility functions.
429 (defun c-delq-from-dotted-list (elt dlist)
430 ;; If ELT is a member of the (possibly dotted) list DLIST, remove all
431 ;; occurrences of it (except for any in the last cdr of DLIST).
433 ;; Call this as (setq DLIST (c-delq-from-dotted-list ELT DLIST)), as
434 ;; sometimes the original structure is changed, sometimes it's not.
436 ;; This function is needed in Emacs < 24.5, and possibly XEmacs, because
437 ;; `delq' throws an error in these versions when given a dotted list.
438 (let ((tail dlist) prev)
439 (while (consp tail)
440 (if (eq (car tail) elt)
441 (if prev
442 (setcdr prev (cdr tail))
443 (setq dlist (cdr dlist)))
444 (setq prev tail))
445 (setq tail (cdr tail)))
446 dlist))
448 (defun c-syntactic-content (from to paren-level)
449 ;; Return the given region as a string where all syntactic
450 ;; whitespace is removed or, where necessary, replaced with a single
451 ;; space. If PAREN-LEVEL is given then all parens in the region are
452 ;; collapsed to "()", "[]" etc.
454 ;; This function might do hidden buffer changes.
456 (save-excursion
457 (save-restriction
458 (narrow-to-region from to)
459 (goto-char from)
460 (let* ((parts (list nil)) (tail parts) pos in-paren)
462 (while (re-search-forward c-syntactic-ws-start to t)
463 (goto-char (setq pos (match-beginning 0)))
464 (c-forward-syntactic-ws)
465 (if (= (point) pos)
466 (forward-char)
468 (when paren-level
469 (save-excursion
470 (setq in-paren (= (car (parse-partial-sexp from pos 1)) 1)
471 pos (point))))
473 (if (and (> pos from)
474 (< (point) to)
475 (looking-at "\\w\\|\\s_")
476 (save-excursion
477 (goto-char (1- pos))
478 (looking-at "\\w\\|\\s_")))
479 (progn
480 (setcdr tail (list (buffer-substring-no-properties from pos)
481 " "))
482 (setq tail (cddr tail)))
483 (setcdr tail (list (buffer-substring-no-properties from pos)))
484 (setq tail (cdr tail)))
486 (when in-paren
487 (when (= (car (parse-partial-sexp pos to -1)) -1)
488 (setcdr tail (list (buffer-substring-no-properties
489 (1- (point)) (point))))
490 (setq tail (cdr tail))))
492 (setq from (point))))
494 (setcdr tail (list (buffer-substring-no-properties from to)))
495 (apply 'concat (cdr parts))))))
497 (defun c-shift-line-indentation (shift-amt)
498 ;; Shift the indentation of the current line with the specified
499 ;; amount (positive inwards). The buffer is modified only if
500 ;; SHIFT-AMT isn't equal to zero.
501 (let ((pos (- (point-max) (point)))
502 (c-macro-start c-macro-start)
503 tmp-char-inserted)
504 (if (zerop shift-amt)
506 ;; If we're on an empty line inside a macro, we take the point
507 ;; to be at the current indentation and shift it to the
508 ;; appropriate column. This way we don't treat the extra
509 ;; whitespace out to the line continuation as indentation.
510 (when (and (c-query-and-set-macro-start)
511 (looking-at "[ \t]*\\\\$")
512 (save-excursion
513 (skip-chars-backward " \t")
514 (bolp)))
515 (insert ?x)
516 (backward-char)
517 (setq tmp-char-inserted t))
518 (unwind-protect
519 (let ((col (current-indentation)))
520 (delete-region (c-point 'bol) (c-point 'boi))
521 (beginning-of-line)
522 (indent-to (+ col shift-amt)))
523 (when tmp-char-inserted
524 (delete-char 1))))
525 ;; If initial point was within line's indentation and we're not on
526 ;; a line with a line continuation in a macro, position after the
527 ;; indentation. Else stay at same point in text.
528 (if (and (< (point) (c-point 'boi))
529 (not tmp-char-inserted))
530 (back-to-indentation)
531 (if (> (- (point-max) pos) (point))
532 (goto-char (- (point-max) pos))))))
534 (defsubst c-keyword-sym (keyword)
535 ;; Return non-nil if the string KEYWORD is a known keyword. More
536 ;; precisely, the value is the symbol for the keyword in
537 ;; `c-keywords-obarray'.
538 (intern-soft keyword c-keywords-obarray))
540 (defsubst c-keyword-member (keyword-sym lang-constant)
541 ;; Return non-nil if the symbol KEYWORD-SYM, as returned by
542 ;; `c-keyword-sym', is a member of LANG-CONSTANT, which is the name
543 ;; of a language constant that ends with "-kwds". If KEYWORD-SYM is
544 ;; nil then the result is nil.
545 (get keyword-sym lang-constant))
547 ;; String syntax chars, suitable for skip-syntax-(forward|backward).
548 (defconst c-string-syntax (if (memq 'gen-string-delim c-emacs-features)
549 "\"|"
550 "\""))
552 ;; Regexp matching string limit syntax.
553 (defconst c-string-limit-regexp (if (memq 'gen-string-delim c-emacs-features)
554 "\\s\"\\|\\s|"
555 "\\s\""))
557 ;; Regexp matching WS followed by string limit syntax.
558 (defconst c-ws*-string-limit-regexp
559 (concat "[ \t]*\\(" c-string-limit-regexp "\\)"))
561 ;; Holds formatted error strings for the few cases where parse errors
562 ;; are reported.
563 (defvar c-parsing-error nil)
564 (make-variable-buffer-local 'c-parsing-error)
566 (defun c-echo-parsing-error (&optional quiet)
567 (when (and c-report-syntactic-errors c-parsing-error (not quiet))
568 (c-benign-error "%s" c-parsing-error))
569 c-parsing-error)
571 ;; Faces given to comments and string literals. This is used in some
572 ;; situations to speed up recognition; it isn't mandatory that font
573 ;; locking is in use. This variable is extended with the face in
574 ;; `c-doc-face-name' when fontification is activated in cc-fonts.el.
575 (defvar c-literal-faces
576 (append '(font-lock-comment-face font-lock-string-face)
577 (when (facep 'font-lock-comment-delimiter-face)
578 ;; New in Emacs 22.
579 '(font-lock-comment-delimiter-face))))
581 (defsubst c-put-c-type-property (pos value)
582 ;; Put a c-type property with the given value at POS.
583 (c-put-char-property pos 'c-type value))
585 (defun c-clear-c-type-property (from to value)
586 ;; Remove all occurrences of the c-type property that has the given
587 ;; value in the region between FROM and TO. VALUE is assumed to not
588 ;; be nil.
590 ;; Note: This assumes that c-type is put on single chars only; it's
591 ;; very inefficient if matching properties cover large regions.
592 (save-excursion
593 (goto-char from)
594 (while (progn
595 (when (eq (get-text-property (point) 'c-type) value)
596 (c-clear-char-property (point) 'c-type))
597 (goto-char (c-next-single-property-change (point) 'c-type nil to))
598 (< (point) to)))))
601 ;; Some debug tools to visualize various special positions. This
602 ;; debug code isn't as portable as the rest of CC Mode.
604 (cc-bytecomp-defun overlays-in)
605 (cc-bytecomp-defun overlay-get)
606 (cc-bytecomp-defun overlay-start)
607 (cc-bytecomp-defun overlay-end)
608 (cc-bytecomp-defun delete-overlay)
609 (cc-bytecomp-defun overlay-put)
610 (cc-bytecomp-defun make-overlay)
612 (defun c-debug-add-face (beg end face)
613 (c-save-buffer-state ((overlays (overlays-in beg end)) overlay)
614 (while overlays
615 (setq overlay (car overlays)
616 overlays (cdr overlays))
617 (when (eq (overlay-get overlay 'face) face)
618 (setq beg (min beg (overlay-start overlay))
619 end (max end (overlay-end overlay)))
620 (delete-overlay overlay)))
621 (overlay-put (make-overlay beg end) 'face face)))
623 (defun c-debug-remove-face (beg end face)
624 (c-save-buffer-state ((overlays (overlays-in beg end)) overlay
625 (ol-beg beg) (ol-end end))
626 (while overlays
627 (setq overlay (car overlays)
628 overlays (cdr overlays))
629 (when (eq (overlay-get overlay 'face) face)
630 (setq ol-beg (min ol-beg (overlay-start overlay))
631 ol-end (max ol-end (overlay-end overlay)))
632 (delete-overlay overlay)))
633 (when (< ol-beg beg)
634 (overlay-put (make-overlay ol-beg beg) 'face face))
635 (when (> ol-end end)
636 (overlay-put (make-overlay end ol-end) 'face face))))
639 ;; `c-beginning-of-statement-1' and accompanying stuff.
641 ;; KLUDGE ALERT: c-maybe-labelp is used to pass information between
642 ;; c-crosses-statement-barrier-p and c-beginning-of-statement-1. A
643 ;; better way should be implemented, but this will at least shut up
644 ;; the byte compiler.
645 (defvar c-maybe-labelp)
647 ;; New awk-compatible version of c-beginning-of-statement-1, ACM 2002/6/22
649 ;; Macros used internally in c-beginning-of-statement-1 for the
650 ;; automaton actions.
651 (defmacro c-bos-push-state ()
652 '(setq stack (cons (cons state saved-pos)
653 stack)))
654 (defmacro c-bos-pop-state (&optional do-if-done)
655 `(if (setq state (car (car stack))
656 saved-pos (cdr (car stack))
657 stack (cdr stack))
659 ,do-if-done
660 (throw 'loop nil)))
661 (defmacro c-bos-pop-state-and-retry ()
662 '(throw 'loop (setq state (car (car stack))
663 saved-pos (cdr (car stack))
664 ;; Throw nil if stack is empty, else throw non-nil.
665 stack (cdr stack))))
666 (defmacro c-bos-save-pos ()
667 '(setq saved-pos (vector pos tok ptok pptok)))
668 (defmacro c-bos-restore-pos ()
669 '(unless (eq (elt saved-pos 0) start)
670 (setq pos (elt saved-pos 0)
671 tok (elt saved-pos 1)
672 ptok (elt saved-pos 2)
673 pptok (elt saved-pos 3))
674 (goto-char pos)
675 (setq sym nil)))
676 (defmacro c-bos-save-error-info (missing got)
677 `(setq saved-pos (vector pos ,missing ,got)))
678 (defmacro c-bos-report-error ()
679 '(unless noerror
680 (setq c-parsing-error
681 (format-message
682 "No matching `%s' found for `%s' on line %d"
683 (elt saved-pos 1)
684 (elt saved-pos 2)
685 (1+ (count-lines (point-min)
686 (c-point 'bol (elt saved-pos 0))))))))
688 (defun c-beginning-of-statement-1 (&optional lim ignore-labels
689 noerror comma-delim)
690 "Move to the start of the current statement or declaration, or to
691 the previous one if already at the beginning of one. Only
692 statements/declarations on the same level are considered, i.e. don't
693 move into or out of sexps (not even normal expression parentheses).
695 If point is already at the earliest statement within braces or parens,
696 this function doesn't move back into any whitespace preceding it; it
697 returns `same' in this case.
699 Stop at statement continuation tokens like \"else\", \"catch\",
700 \"finally\" and the \"while\" in \"do ... while\" if the start point
701 is within the continuation. If starting at such a token, move to the
702 corresponding statement start. If at the beginning of a statement,
703 move to the closest containing statement if there is any. This might
704 also stop at a continuation clause.
706 Labels are treated as part of the following statements if
707 IGNORE-LABELS is non-nil. (FIXME: Doesn't work if we stop at a known
708 statement start keyword.) Otherwise, each label is treated as a
709 separate statement.
711 Macros are ignored \(i.e. skipped over) unless point is within one, in
712 which case the content of the macro is treated as normal code. Aside
713 from any normal statement starts found in it, stop at the first token
714 of the content in the macro, i.e. the expression of an \"#if\" or the
715 start of the definition in a \"#define\". Also stop at start of
716 macros before leaving them.
718 Return:
719 `label' if stopped at a label or \"case...:\" or \"default:\";
720 `same' if stopped at the beginning of the current statement;
721 `up' if stepped to a containing statement;
722 `previous' if stepped to a preceding statement;
723 `beginning' if stepped from a statement continuation clause to
724 its start clause; or
725 `macro' if stepped to a macro start.
726 Note that `same' and not `label' is returned if stopped at the same
727 label without crossing the colon character.
729 LIM may be given to limit the search. If the search hits the limit,
730 point will be left at the closest following token, or at the start
731 position if that is less (`same' is returned in this case).
733 NOERROR turns off error logging to `c-parsing-error'.
735 Normally only `;' and virtual semicolons are considered to delimit
736 statements, but if COMMA-DELIM is non-nil then `,' is treated
737 as a delimiter too.
739 Note that this function might do hidden buffer changes. See the
740 comment at the start of cc-engine.el for more info."
742 ;; The bulk of this function is a pushdown automaton that looks at statement
743 ;; boundaries and the tokens (such as "while") in c-opt-block-stmt-key. Its
744 ;; purpose is to keep track of nested statements, ensuring that such
745 ;; statements are skipped over in their entirety (somewhat akin to what C-M-p
746 ;; does with nested braces/brackets/parentheses).
748 ;; Note: The position of a boundary is the following token.
750 ;; Beginning with the current token (the one following point), move back one
751 ;; sexp at a time (where a sexp is, more or less, either a token or the
752 ;; entire contents of a brace/bracket/paren pair). Each time a statement
753 ;; boundary is crossed or a "while"-like token is found, update the state of
754 ;; the PDA. Stop at the beginning of a statement when the stack (holding
755 ;; nested statement info) is empty and the position has been moved.
757 ;; The following variables constitute the PDA:
759 ;; sym: This is either the "while"-like token (e.g. 'for) we've just
760 ;; scanned back over, 'boundary if we've just gone back over a
761 ;; statement boundary, or nil otherwise.
762 ;; state: takes one of the values (nil else else-boundary while
763 ;; while-boundary catch catch-boundary).
764 ;; nil means "no "while"-like token yet scanned".
765 ;; 'else, for example, means "just gone back over an else".
766 ;; 'else-boundary means "just gone back over a statement boundary
767 ;; immediately after having gone back over an else".
768 ;; saved-pos: A vector of either saved positions (tok ptok pptok, etc.) or
769 ;; of error reporting information.
770 ;; stack: The stack onto which the PDA pushes its state. Each entry
771 ;; consists of a saved value of state and saved-pos. An entry is
772 ;; pushed when we move back over a "continuation" token (e.g. else)
773 ;; and popped when we encounter the corresponding opening token
774 ;; (e.g. if).
777 ;; The following diagram briefly outlines the PDA.
779 ;; Common state:
780 ;; "else": Push state, goto state `else'.
781 ;; "while": Push state, goto state `while'.
782 ;; "catch" or "finally": Push state, goto state `catch'.
783 ;; boundary: Pop state.
784 ;; other: Do nothing special.
786 ;; State `else':
787 ;; boundary: Goto state `else-boundary'.
788 ;; other: Error, pop state, retry token.
790 ;; State `else-boundary':
791 ;; "if": Pop state.
792 ;; boundary: Error, pop state.
793 ;; other: See common state.
795 ;; State `while':
796 ;; boundary: Save position, goto state `while-boundary'.
797 ;; other: Pop state, retry token.
799 ;; State `while-boundary':
800 ;; "do": Pop state.
801 ;; boundary: Restore position if it's not at start, pop state. [*see below]
802 ;; other: See common state.
804 ;; State `catch':
805 ;; boundary: Goto state `catch-boundary'.
806 ;; other: Error, pop state, retry token.
808 ;; State `catch-boundary':
809 ;; "try": Pop state.
810 ;; "catch": Goto state `catch'.
811 ;; boundary: Error, pop state.
812 ;; other: See common state.
814 ;; [*] In the `while-boundary' state, we had pushed a 'while state, and were
815 ;; searching for a "do" which would have opened a do-while. If we didn't
816 ;; find it, we discard the analysis done since the "while", go back to this
817 ;; token in the buffer and restart the scanning there, this time WITHOUT
818 ;; pushing the 'while state onto the stack.
820 ;; In addition to the above there is some special handling of labels
821 ;; and macros.
823 (let ((case-fold-search nil)
824 (start (point))
825 macro-start
826 (delims (if comma-delim '(?\; ?,) '(?\;)))
827 (c-stmt-delim-chars (if comma-delim
828 c-stmt-delim-chars-with-comma
829 c-stmt-delim-chars))
830 c-in-literal-cache c-maybe-labelp after-case:-pos saved
831 ;; Current position.
833 ;; Position of last stmt boundary character (e.g. ;).
834 boundary-pos
835 ;; The position of the last sexp or bound that follows the
836 ;; first found colon, i.e. the start of the nonlabel part of
837 ;; the statement. It's `start' if a colon is found just after
838 ;; the start.
839 after-labels-pos
840 ;; Like `after-labels-pos', but the first such position inside
841 ;; a label, i.e. the start of the last label before the start
842 ;; of the nonlabel part of the statement.
843 last-label-pos
844 ;; The last position where a label is possible provided the
845 ;; statement started there. It's nil as long as no invalid
846 ;; label content has been found (according to
847 ;; `c-nonlabel-token-key'). It's `start' if no valid label
848 ;; content was found in the label. Note that we might still
849 ;; regard it a label if it starts with `c-label-kwds'.
850 label-good-pos
851 ;; Putative positions of the components of a bitfield declaration,
852 ;; e.g. "int foo : NUM_FOO_BITS ;"
853 bitfield-type-pos bitfield-id-pos bitfield-size-pos
854 ;; Symbol just scanned back over (e.g. 'while or 'boundary).
855 ;; See above.
857 ;; Current state in the automaton. See above.
858 state
859 ;; Current saved positions. See above.
860 saved-pos
861 ;; Stack of conses (state . saved-pos).
862 stack
863 ;; Regexp which matches "for", "if", etc.
864 (cond-key (or c-opt-block-stmt-key
865 "\\<\\>")) ; Matches nothing.
866 ;; Return value.
867 (ret 'same)
868 ;; Positions of the last three sexps or bounds we've stopped at.
869 tok ptok pptok)
871 (save-restriction
872 (if lim (narrow-to-region lim (point-max)))
874 (if (save-excursion
875 (and (c-beginning-of-macro)
876 (/= (point) start)))
877 (setq macro-start (point)))
879 ;; Try to skip back over unary operator characters, to register
880 ;; that we've moved.
881 (while (progn
882 (setq pos (point))
883 (c-backward-syntactic-ws)
884 ;; Protect post-++/-- operators just before a virtual semicolon.
885 (and (not (c-at-vsemi-p))
886 (/= (skip-chars-backward "-+!*&~@`#") 0))))
888 ;; Skip back over any semicolon here. If it was a bare semicolon, we're
889 ;; done. Later on we ignore the boundaries for statements that don't
890 ;; contain any sexp. The only thing that is affected is that the error
891 ;; checking is a little less strict, and we really don't bother.
892 (if (and (memq (char-before) delims)
893 (progn (forward-char -1)
894 (setq saved (point))
895 (c-backward-syntactic-ws)
896 (or (memq (char-before) delims)
897 (memq (char-before) '(?: nil))
898 (eq (char-syntax (char-before)) ?\()
899 (c-at-vsemi-p))))
900 (setq ret 'previous
901 pos saved)
903 ;; Begin at start and not pos to detect macros if we stand
904 ;; directly after the #.
905 (goto-char start)
906 (if (looking-at "\\<\\|\\W")
907 ;; Record this as the first token if not starting inside it.
908 (setq tok start))
910 ;; The following while loop goes back one sexp (balanced parens,
911 ;; etc. with contents, or symbol or suchlike) each iteration. This
912 ;; movement is accomplished with a call to c-backward-sexp approx 170
913 ;; lines below.
915 ;; The loop is exited only by throwing nil to the (catch 'loop ...):
916 ;; 1. On reaching the start of a macro;
917 ;; 2. On having passed a stmt boundary with the PDA stack empty;
918 ;; 3. On reaching the start of an Objective C method def;
919 ;; 4. From macro `c-bos-pop-state'; when the stack is empty;
920 ;; 5. From macro `c-bos-pop-state-and-retry' when the stack is empty.
921 (while
922 (catch 'loop ;; Throw nil to break, non-nil to continue.
923 (cond
924 ;; Are we in a macro, just after the opening #?
925 ((save-excursion
926 (and macro-start ; Always NIL for AWK.
927 (progn (skip-chars-backward " \t")
928 (eq (char-before) ?#))
929 (progn (setq saved (1- (point)))
930 (beginning-of-line)
931 (not (eq (char-before (1- (point))) ?\\)))
932 (looking-at c-opt-cpp-start)
933 (progn (skip-chars-forward " \t")
934 (eq (point) saved))))
935 (goto-char saved)
936 (if (and (c-forward-to-cpp-define-body)
937 (progn (c-forward-syntactic-ws start)
938 (< (point) start)))
939 ;; Stop at the first token in the content of the macro.
940 (setq pos (point)
941 ignore-labels t) ; Avoid the label check on exit.
942 (setq pos saved
943 ret 'macro
944 ignore-labels t))
945 (throw 'loop nil)) ; 1. Start of macro.
947 ;; Do a round through the automaton if we've just passed a
948 ;; statement boundary or passed a "while"-like token.
949 ((or sym
950 (and (looking-at cond-key)
951 (setq sym (intern (match-string 1)))))
953 (when (and (< pos start) (null stack))
954 (throw 'loop nil)) ; 2. Statement boundary.
956 ;; The PDA state handling.
958 ;; Refer to the description of the PDA in the opening
959 ;; comments. In the following OR form, the first leaf
960 ;; attempts to handles one of the specific actions detailed
961 ;; (e.g., finding token "if" whilst in state `else-boundary').
962 ;; We drop through to the second leaf (which handles common
963 ;; state) if no specific handler is found in the first cond.
964 ;; If a parsing error is detected (e.g. an "else" with no
965 ;; preceding "if"), we throw to the enclosing catch.
967 ;; Note that the (eq state 'else) means
968 ;; "we've just passed an else", NOT "we're looking for an
969 ;; else".
970 (or (cond
971 ((eq state 'else)
972 (if (eq sym 'boundary)
973 (setq state 'else-boundary)
974 (c-bos-report-error)
975 (c-bos-pop-state-and-retry)))
977 ((eq state 'else-boundary)
978 (cond ((eq sym 'if)
979 (c-bos-pop-state (setq ret 'beginning)))
980 ((eq sym 'boundary)
981 (c-bos-report-error)
982 (c-bos-pop-state))))
984 ((eq state 'while)
985 (if (and (eq sym 'boundary)
986 ;; Since this can cause backtracking we do a
987 ;; little more careful analysis to avoid it:
988 ;; If there's a label in front of the while
989 ;; it can't be part of a do-while.
990 (not after-labels-pos))
991 (progn (c-bos-save-pos)
992 (setq state 'while-boundary))
993 (c-bos-pop-state-and-retry))) ; Can't be a do-while
995 ((eq state 'while-boundary)
996 (cond ((eq sym 'do)
997 (c-bos-pop-state (setq ret 'beginning)))
998 ((eq sym 'boundary) ; isn't a do-while
999 (c-bos-restore-pos) ; the position of the while
1000 (c-bos-pop-state)))) ; no longer searching for do.
1002 ((eq state 'catch)
1003 (if (eq sym 'boundary)
1004 (setq state 'catch-boundary)
1005 (c-bos-report-error)
1006 (c-bos-pop-state-and-retry)))
1008 ((eq state 'catch-boundary)
1009 (cond
1010 ((eq sym 'try)
1011 (c-bos-pop-state (setq ret 'beginning)))
1012 ((eq sym 'catch)
1013 (setq state 'catch))
1014 ((eq sym 'boundary)
1015 (c-bos-report-error)
1016 (c-bos-pop-state)))))
1018 ;; This is state common. We get here when the previous
1019 ;; cond statement found no particular state handler.
1020 (cond ((eq sym 'boundary)
1021 ;; If we have a boundary at the start
1022 ;; position we push a frame to go to the
1023 ;; previous statement.
1024 (if (>= pos start)
1025 (c-bos-push-state)
1026 (c-bos-pop-state)))
1027 ((eq sym 'else)
1028 (c-bos-push-state)
1029 (c-bos-save-error-info 'if 'else)
1030 (setq state 'else))
1031 ((eq sym 'while)
1032 ;; Is this a real while, or a do-while?
1033 ;; The next `when' triggers unless we are SURE that
1034 ;; the `while' is not the tail end of a `do-while'.
1035 (when (or (not pptok)
1036 (memq (char-after pptok) delims)
1037 ;; The following kludge is to prevent
1038 ;; infinite recursion when called from
1039 ;; c-awk-after-if-for-while-condition-p,
1040 ;; or the like.
1041 (and (eq (point) start)
1042 (c-vsemi-status-unknown-p))
1043 (c-at-vsemi-p pptok))
1044 ;; Since this can cause backtracking we do a
1045 ;; little more careful analysis to avoid it: If
1046 ;; the while isn't followed by a (possibly
1047 ;; virtual) semicolon it can't be a do-while.
1048 (c-bos-push-state)
1049 (setq state 'while)))
1050 ((memq sym '(catch finally))
1051 (c-bos-push-state)
1052 (c-bos-save-error-info 'try sym)
1053 (setq state 'catch))))
1055 (when c-maybe-labelp
1056 ;; We're either past a statement boundary or at the
1057 ;; start of a statement, so throw away any label data
1058 ;; for the previous one.
1059 (setq after-labels-pos nil
1060 last-label-pos nil
1061 c-maybe-labelp nil))))
1063 ;; Step to the previous sexp, but not if we crossed a
1064 ;; boundary, since that doesn't consume an sexp.
1065 (if (eq sym 'boundary)
1066 (setq ret 'previous)
1068 ;; HERE IS THE SINGLE PLACE INSIDE THE PDA LOOP WHERE WE MOVE
1069 ;; BACKWARDS THROUGH THE SOURCE.
1071 (c-backward-syntactic-ws)
1072 (let ((before-sws-pos (point))
1073 ;; The end position of the area to search for statement
1074 ;; barriers in this round.
1075 (maybe-after-boundary-pos pos))
1077 ;; Go back over exactly one logical sexp, taking proper
1078 ;; account of macros and escaped EOLs.
1079 (while
1080 (progn
1081 (unless (c-safe (c-backward-sexp) t)
1082 ;; Give up if we hit an unbalanced block. Since the
1083 ;; stack won't be empty the code below will report a
1084 ;; suitable error.
1085 (throw 'loop nil))
1086 (cond
1087 ;; Have we moved into a macro?
1088 ((and (not macro-start)
1089 (c-beginning-of-macro))
1090 ;; Have we crossed a statement boundary? If not,
1091 ;; keep going back until we find one or a "real" sexp.
1092 (and
1093 (save-excursion
1094 (c-end-of-macro)
1095 (not (c-crosses-statement-barrier-p
1096 (point) maybe-after-boundary-pos)))
1097 (setq maybe-after-boundary-pos (point))))
1098 ;; Have we just gone back over an escaped NL? This
1099 ;; doesn't count as a sexp.
1100 ((looking-at "\\\\$")))))
1102 ;; Have we crossed a statement boundary?
1103 (setq boundary-pos
1104 (cond
1105 ;; Are we at a macro beginning?
1106 ((and (not macro-start)
1107 c-opt-cpp-prefix
1108 (looking-at c-opt-cpp-prefix))
1109 (save-excursion
1110 (c-end-of-macro)
1111 (c-crosses-statement-barrier-p
1112 (point) maybe-after-boundary-pos)))
1113 ;; Just gone back over a brace block?
1114 ((and
1115 (eq (char-after) ?{)
1116 (not (c-looking-at-inexpr-block lim nil t))
1117 (save-excursion
1118 (c-backward-token-2 1 t nil)
1119 (not (looking-at "=\\([^=]\\|$\\)"))))
1120 (save-excursion
1121 (c-forward-sexp) (point)))
1122 ;; Just gone back over some paren block?
1123 ((looking-at "\\s(")
1124 (save-excursion
1125 (goto-char (1+ (c-down-list-backward
1126 before-sws-pos)))
1127 (c-crosses-statement-barrier-p
1128 (point) maybe-after-boundary-pos)))
1129 ;; Just gone back over an ordinary symbol of some sort?
1130 (t (c-crosses-statement-barrier-p
1131 (point) maybe-after-boundary-pos))))
1133 (when boundary-pos
1134 (setq pptok ptok
1135 ptok tok
1136 tok boundary-pos
1137 sym 'boundary)
1138 ;; Like a C "continue". Analyze the next sexp.
1139 (throw 'loop t))))
1141 ;; ObjC method def?
1142 (when (and c-opt-method-key
1143 (setq saved (c-in-method-def-p)))
1144 (setq pos saved
1145 ignore-labels t) ; Avoid the label check on exit.
1146 (throw 'loop nil)) ; 3. ObjC method def.
1148 ;; Might we have a bitfield declaration, "<type> <id> : <size>"?
1149 (if c-has-bitfields
1150 (cond
1151 ;; The : <size> and <id> fields?
1152 ((and (numberp c-maybe-labelp)
1153 (not bitfield-size-pos)
1154 (save-excursion
1155 (goto-char (or tok start))
1156 (not (looking-at c-keywords-regexp)))
1157 (not (looking-at c-keywords-regexp))
1158 (not (c-punctuation-in (point) c-maybe-labelp)))
1159 (setq bitfield-size-pos (or tok start)
1160 bitfield-id-pos (point)))
1161 ;; The <type> field?
1162 ((and bitfield-id-pos
1163 (not bitfield-type-pos))
1164 (if (and (looking-at c-symbol-key) ; Can only be an integer type. :-)
1165 (not (looking-at c-not-primitive-type-keywords-regexp))
1166 (not (c-punctuation-in (point) tok)))
1167 (setq bitfield-type-pos (point))
1168 (setq bitfield-size-pos nil
1169 bitfield-id-pos nil)))))
1171 ;; Handle labels.
1172 (unless (eq ignore-labels t)
1173 (when (numberp c-maybe-labelp)
1174 ;; `c-crosses-statement-barrier-p' has found a colon, so we
1175 ;; might be in a label now. Have we got a real label
1176 ;; (including a case label) or something like C++'s "public:"?
1177 ;; A case label might use an expression rather than a token.
1178 (setq after-case:-pos (or tok start))
1179 (if (or (looking-at c-nonlabel-token-key) ; e.g. "while" or "'a'"
1180 ;; Catch C++'s inheritance construct "class foo : bar".
1181 (save-excursion
1182 (and
1183 (c-safe (c-backward-sexp) t)
1184 (looking-at c-nonlabel-token-2-key))))
1185 (setq c-maybe-labelp nil)
1186 (if after-labels-pos ; Have we already encountered a label?
1187 (if (not last-label-pos)
1188 (setq last-label-pos (or tok start)))
1189 (setq after-labels-pos (or tok start)))
1190 (setq c-maybe-labelp t
1191 label-good-pos nil))) ; bogus "label"
1193 (when (and (not label-good-pos) ; i.e. no invalid "label"'s yet
1194 ; been found.
1195 (looking-at c-nonlabel-token-key)) ; e.g. "while :"
1196 ;; We're in a potential label and it's the first
1197 ;; time we've found something that isn't allowed in
1198 ;; one.
1199 (setq label-good-pos (or tok start))))
1201 ;; We've moved back by a sexp, so update the token positions.
1202 (setq sym nil
1203 pptok ptok
1204 ptok tok
1205 tok (point)
1206 pos tok) ; always non-nil
1207 ) ; end of (catch loop ....)
1208 ) ; end of sexp-at-a-time (while ....)
1210 ;; If the stack isn't empty there might be errors to report.
1211 (while stack
1212 (if (and (vectorp saved-pos) (eq (length saved-pos) 3))
1213 (c-bos-report-error))
1214 (setq saved-pos (cdr (car stack))
1215 stack (cdr stack)))
1217 (when (and (eq ret 'same)
1218 (not (memq sym '(boundary ignore nil))))
1219 ;; Need to investigate closer whether we've crossed
1220 ;; between a substatement and its containing statement.
1221 (if (setq saved
1222 (cond ((and (looking-at c-block-stmt-1-2-key)
1223 (eq (char-after ptok) ?\())
1224 pptok)
1225 ((looking-at c-block-stmt-1-key)
1226 ptok)
1227 (t pptok)))
1228 (cond ((> start saved) (setq pos saved))
1229 ((= start saved) (setq ret 'up)))))
1231 (when (and (not ignore-labels)
1232 (eq c-maybe-labelp t)
1233 (not (eq ret 'beginning))
1234 after-labels-pos
1235 (not bitfield-type-pos) ; Bitfields take precedence over labels.
1236 (or (not label-good-pos)
1237 (<= label-good-pos pos)
1238 (progn
1239 (goto-char (if (and last-label-pos
1240 (< last-label-pos start))
1241 last-label-pos
1242 pos))
1243 (looking-at c-label-kwds-regexp))))
1244 ;; We're in a label. Maybe we should step to the statement
1245 ;; after it.
1246 (if (< after-labels-pos start)
1247 (setq pos after-labels-pos)
1248 (setq ret 'label)
1249 (if (and last-label-pos (< last-label-pos start))
1250 ;; Might have jumped over several labels. Go to the last one.
1251 (setq pos last-label-pos)))))
1253 ;; Have we got "case <expression>:"?
1254 (goto-char pos)
1255 (when (and after-case:-pos
1256 (not (eq ret 'beginning))
1257 (looking-at c-case-kwds-regexp))
1258 (if (< after-case:-pos start)
1259 (setq pos after-case:-pos))
1260 (if (eq ret 'same)
1261 (setq ret 'label)))
1263 ;; Skip over the unary operators that can start the statement.
1264 (while (progn
1265 (c-backward-syntactic-ws)
1266 ;; protect AWK post-inc/decrement operators, etc.
1267 (and (not (c-at-vsemi-p (point)))
1268 (/= (skip-chars-backward "-+!*&~@`#") 0)))
1269 (setq pos (point)))
1270 (goto-char pos)
1271 ret)))
1273 (defun c-punctuation-in (from to)
1274 "Return non-nil if there is a non-comment non-macro punctuation character
1275 between FROM and TO. FROM must not be in a string or comment. The returned
1276 value is the position of the first such character."
1277 (save-excursion
1278 (goto-char from)
1279 (let ((pos (point)))
1280 (while (progn (skip-chars-forward c-symbol-chars to)
1281 (c-forward-syntactic-ws to)
1282 (> (point) pos))
1283 (setq pos (point))))
1284 (and (< (point) to) (point))))
1286 (defun c-crosses-statement-barrier-p (from to)
1287 "Return non-nil if buffer positions FROM to TO cross one or more
1288 statement or declaration boundaries. The returned value is actually
1289 the position of the earliest boundary char. FROM must not be within
1290 a string or comment.
1292 The variable `c-maybe-labelp' is set to the position of the first `:' that
1293 might start a label (i.e. not part of `::' and not preceded by `?'). If a
1294 single `?' is found, then `c-maybe-labelp' is cleared.
1296 For AWK, a statement which is terminated by an EOL (not a ; or a }) is
1297 regarded as having a \"virtual semicolon\" immediately after the last token on
1298 the line. If this virtual semicolon is _at_ from, the function recognizes it.
1300 Note that this function might do hidden buffer changes. See the
1301 comment at the start of cc-engine.el for more info."
1302 (let* ((skip-chars
1303 ;; If the current language has CPP macros, insert # into skip-chars.
1304 (if c-opt-cpp-symbol
1305 (concat (substring c-stmt-delim-chars 0 1) ; "^"
1306 c-opt-cpp-symbol ; usually "#"
1307 (substring c-stmt-delim-chars 1)) ; e.g. ";{}?:"
1308 c-stmt-delim-chars))
1309 (non-skip-list
1310 (append (substring skip-chars 1) nil)) ; e.g. (?# ?\; ?{ ?} ?? ?:)
1311 lit-range lit-start vsemi-pos)
1312 (save-restriction
1313 (widen)
1314 (save-excursion
1315 (catch 'done
1316 (goto-char from)
1317 (while (progn (skip-chars-forward
1318 skip-chars
1319 (min to (c-point 'bonl)))
1320 (< (point) to))
1321 (cond
1322 ;; Virtual semicolon?
1323 ((and (bolp)
1324 (save-excursion
1325 (progn
1326 (if (setq lit-start (c-literal-start from)) ; Have we landed in a string/comment?
1327 (goto-char lit-start))
1328 (c-backward-syntactic-ws) ; ? put a limit here, maybe?
1329 (setq vsemi-pos (point))
1330 (c-at-vsemi-p))))
1331 (throw 'done vsemi-pos))
1332 ;; In a string/comment?
1333 ((setq lit-range (c-literal-limits from))
1334 (goto-char (cdr lit-range)))
1335 ((eq (char-after) ?:)
1336 (forward-char)
1337 (if (and (eq (char-after) ?:)
1338 (< (point) to))
1339 ;; Ignore scope operators.
1340 (forward-char)
1341 (setq c-maybe-labelp (1- (point)))))
1342 ((eq (char-after) ??)
1343 ;; A question mark. Can't be a label, so stop
1344 ;; looking for more : and ?.
1345 (setq c-maybe-labelp nil
1346 skip-chars (substring c-stmt-delim-chars 0 -2)))
1347 ;; At a CPP construct or a "#" or "##" operator?
1348 ((and c-opt-cpp-symbol (looking-at c-opt-cpp-symbol))
1349 (if (save-excursion
1350 (skip-chars-backward " \t")
1351 (and (bolp)
1352 (or (bobp)
1353 (not (eq (char-before (1- (point))) ?\\)))))
1354 (c-end-of-macro)
1355 (skip-chars-forward c-opt-cpp-symbol)))
1356 ((memq (char-after) non-skip-list)
1357 (throw 'done (point)))))
1358 ;; In trailing space after an as yet undetected virtual semicolon?
1359 (c-backward-syntactic-ws from)
1360 (when (and (bolp) (not (bobp))) ; Can happen in AWK Mode with an
1361 ; unterminated string/regexp.
1362 (backward-char))
1363 (if (and (< (point) to)
1364 (c-at-vsemi-p))
1365 (point)
1366 nil))))))
1368 (defun c-at-statement-start-p ()
1369 "Return non-nil if the point is at the first token in a statement
1370 or somewhere in the syntactic whitespace before it.
1372 A \"statement\" here is not restricted to those inside code blocks.
1373 Any kind of declaration-like construct that occur outside function
1374 bodies is also considered a \"statement\".
1376 Note that this function might do hidden buffer changes. See the
1377 comment at the start of cc-engine.el for more info."
1379 (save-excursion
1380 (let ((end (point))
1381 c-maybe-labelp)
1382 (c-syntactic-skip-backward (substring c-stmt-delim-chars 1) nil t)
1383 (or (bobp)
1384 (eq (char-before) ?})
1385 (and (eq (char-before) ?{)
1386 (not (and c-special-brace-lists
1387 (progn (backward-char)
1388 (c-looking-at-special-brace-list)))))
1389 (c-crosses-statement-barrier-p (point) end)))))
1391 (defun c-at-expression-start-p ()
1392 "Return non-nil if the point is at the first token in an expression or
1393 statement, or somewhere in the syntactic whitespace before it.
1395 An \"expression\" here is a bit different from the normal language
1396 grammar sense: It's any sequence of expression tokens except commas,
1397 unless they are enclosed inside parentheses of some kind. Also, an
1398 expression never continues past an enclosing parenthesis, but it might
1399 contain parenthesis pairs of any sort except braces.
1401 Since expressions never cross statement boundaries, this function also
1402 recognizes statement beginnings, just like `c-at-statement-start-p'.
1404 Note that this function might do hidden buffer changes. See the
1405 comment at the start of cc-engine.el for more info."
1407 (save-excursion
1408 (let ((end (point))
1409 (c-stmt-delim-chars c-stmt-delim-chars-with-comma)
1410 c-maybe-labelp)
1411 (c-syntactic-skip-backward (substring c-stmt-delim-chars 1) nil t)
1412 (or (bobp)
1413 (memq (char-before) '(?{ ?}))
1414 (save-excursion (backward-char)
1415 (looking-at "\\s("))
1416 (c-crosses-statement-barrier-p (point) end)))))
1419 ;; A set of functions that covers various idiosyncrasies in
1420 ;; implementations of `forward-comment'.
1422 ;; Note: Some emacsen considers incorrectly that any line comment
1423 ;; ending with a backslash continues to the next line. I can't think
1424 ;; of any way to work around that in a reliable way without changing
1425 ;; the buffer, though. Suggestions welcome. ;) (No, temporarily
1426 ;; changing the syntax for backslash doesn't work since we must treat
1427 ;; escapes in string literals correctly.)
1429 (defun c-forward-single-comment ()
1430 "Move forward past whitespace and the closest following comment, if any.
1431 Return t if a comment was found, nil otherwise. In either case, the
1432 point is moved past the following whitespace. Line continuations,
1433 i.e. a backslashes followed by line breaks, are treated as whitespace.
1434 The line breaks that end line comments are considered to be the
1435 comment enders, so the point will be put on the beginning of the next
1436 line if it moved past a line comment.
1438 This function does not do any hidden buffer changes."
1440 (let ((start (point)))
1441 (when (looking-at "\\([ \t\n\r\f\v]\\|\\\\[\n\r]\\)+")
1442 (goto-char (match-end 0)))
1444 (when (forward-comment 1)
1445 (if (eobp)
1446 ;; Some emacsen (e.g. XEmacs 21) return t when moving
1447 ;; forwards at eob.
1450 ;; Emacs includes the ending newline in a b-style (c++)
1451 ;; comment, but XEmacs doesn't. We depend on the Emacs
1452 ;; behavior (which also is symmetric).
1453 (if (and (eolp) (elt (parse-partial-sexp start (point)) 7))
1454 (condition-case nil (forward-char 1)))
1456 t))))
1458 (defsubst c-forward-comments ()
1459 "Move forward past all following whitespace and comments.
1460 Line continuations, i.e. a backslashes followed by line breaks, are
1461 treated as whitespace.
1463 Note that this function might do hidden buffer changes. See the
1464 comment at the start of cc-engine.el for more info."
1466 (while (or
1467 ;; If forward-comment in at least XEmacs 21 is given a large
1468 ;; positive value, it'll loop all the way through if it hits
1469 ;; eob.
1470 (and (forward-comment 5)
1471 ;; Some emacsen (e.g. XEmacs 21) return t when moving
1472 ;; forwards at eob.
1473 (not (eobp)))
1475 (when (looking-at "\\\\[\n\r]")
1476 (forward-char 2)
1477 t))))
1479 (defun c-backward-single-comment ()
1480 "Move backward past whitespace and the closest preceding comment, if any.
1481 Return t if a comment was found, nil otherwise. In either case, the
1482 point is moved past the preceding whitespace. Line continuations,
1483 i.e. a backslashes followed by line breaks, are treated as whitespace.
1484 The line breaks that end line comments are considered to be the
1485 comment enders, so the point cannot be at the end of the same line to
1486 move over a line comment.
1488 This function does not do any hidden buffer changes."
1490 (let ((start (point)))
1491 ;; When we got newline terminated comments, forward-comment in all
1492 ;; supported emacsen so far will stop at eol of each line not
1493 ;; ending with a comment when moving backwards. This corrects for
1494 ;; that, and at the same time handles line continuations.
1495 (while (progn
1496 (skip-chars-backward " \t\n\r\f\v")
1497 (and (looking-at "[\n\r]")
1498 (eq (char-before) ?\\)))
1499 (backward-char))
1501 (if (bobp)
1502 ;; Some emacsen (e.g. Emacs 19.34) return t when moving
1503 ;; backwards at bob.
1506 ;; Leave point after the closest following newline if we've
1507 ;; backed up over any above, since forward-comment won't move
1508 ;; backward over a line comment if point is at the end of the
1509 ;; same line.
1510 (re-search-forward "\\=\\s *[\n\r]" start t)
1512 (if (if (forward-comment -1)
1513 (if (eolp)
1514 ;; If forward-comment above succeeded and we're at eol
1515 ;; then the newline we moved over above didn't end a
1516 ;; line comment, so we give it another go.
1517 (forward-comment -1)
1520 ;; Emacs <= 20 and XEmacs move back over the closer of a
1521 ;; block comment that lacks an opener.
1522 (if (looking-at "\\*/")
1523 (progn (forward-char 2) nil)
1524 t)))))
1526 (defsubst c-backward-comments ()
1527 "Move backward past all preceding whitespace and comments.
1528 Line continuations, i.e. a backslashes followed by line breaks, are
1529 treated as whitespace. The line breaks that end line comments are
1530 considered to be the comment enders, so the point cannot be at the end
1531 of the same line to move over a line comment. Unlike
1532 c-backward-syntactic-ws, this function doesn't move back over
1533 preprocessor directives.
1535 Note that this function might do hidden buffer changes. See the
1536 comment at the start of cc-engine.el for more info."
1538 (let ((start (point)))
1539 (while (and
1540 ;; `forward-comment' in some emacsen (e.g. XEmacs 21.4)
1541 ;; return t when moving backwards at bob.
1542 (not (bobp))
1544 (if (let (moved-comment)
1545 (while
1546 (and (not (setq moved-comment (forward-comment -1)))
1547 ;; Cope specifically with ^M^J here -
1548 ;; forward-comment sometimes gets stuck after ^Ms,
1549 ;; sometimes after ^M^J.
1551 (when (eq (char-before) ?\r)
1552 (backward-char)
1554 (when (and (eq (char-before) ?\n)
1555 (eq (char-before (1- (point))) ?\r))
1556 (backward-char 2)
1557 t))))
1558 moved-comment)
1559 (if (looking-at "\\*/")
1560 ;; Emacs <= 20 and XEmacs move back over the
1561 ;; closer of a block comment that lacks an opener.
1562 (progn (forward-char 2) nil)
1565 ;; XEmacs treats line continuations as whitespace but
1566 ;; only in the backward direction, which seems a bit
1567 ;; odd. Anyway, this is necessary for Emacs.
1568 (when (and (looking-at "[\n\r]")
1569 (eq (char-before) ?\\)
1570 (< (point) start))
1571 (backward-char)
1572 t))))))
1575 ;; Tools for skipping over syntactic whitespace.
1577 ;; The following functions use text properties to cache searches over
1578 ;; large regions of syntactic whitespace. It works as follows:
1580 ;; o If a syntactic whitespace region contains anything but simple
1581 ;; whitespace (i.e. space, tab and line breaks), the text property
1582 ;; `c-in-sws' is put over it. At places where we have stopped
1583 ;; within that region there's also a `c-is-sws' text property.
1584 ;; That since there typically are nested whitespace inside that
1585 ;; must be handled separately, e.g. whitespace inside a comment or
1586 ;; cpp directive. Thus, from one point with `c-is-sws' it's safe
1587 ;; to jump to another point with that property within the same
1588 ;; `c-in-sws' region. It can be likened to a ladder where
1589 ;; `c-in-sws' marks the bars and `c-is-sws' the rungs.
1591 ;; o The `c-is-sws' property is put on the simple whitespace chars at
1592 ;; a "rung position" and also maybe on the first following char.
1593 ;; As many characters as can be conveniently found in this range
1594 ;; are marked, but no assumption can be made that the whole range
1595 ;; is marked (it could be clobbered by later changes, for
1596 ;; instance).
1598 ;; Note that some part of the beginning of a sequence of simple
1599 ;; whitespace might be part of the end of a preceding line comment
1600 ;; or cpp directive and must not be considered part of the "rung".
1601 ;; Such whitespace is some amount of horizontal whitespace followed
1602 ;; by a newline. In the case of cpp directives it could also be
1603 ;; two newlines with horizontal whitespace between them.
1605 ;; The reason to include the first following char is to cope with
1606 ;; "rung positions" that don't have any ordinary whitespace. If
1607 ;; `c-is-sws' is put on a token character it does not have
1608 ;; `c-in-sws' set simultaneously. That's the only case when that
1609 ;; can occur, and the reason for not extending the `c-in-sws'
1610 ;; region to cover it is that the `c-in-sws' region could then be
1611 ;; accidentally merged with a following one if the token is only
1612 ;; one character long.
1614 ;; o On buffer changes the `c-in-sws' and `c-is-sws' properties are
1615 ;; removed in the changed region. If the change was inside
1616 ;; syntactic whitespace that means that the "ladder" is broken, but
1617 ;; a later call to `c-forward-sws' or `c-backward-sws' will use the
1618 ;; parts on either side and use an ordinary search only to "repair"
1619 ;; the gap.
1621 ;; Special care needs to be taken if a region is removed: If there
1622 ;; are `c-in-sws' on both sides of it which do not connect inside
1623 ;; the region then they can't be joined. If e.g. a marked macro is
1624 ;; broken, syntactic whitespace inside the new text might be
1625 ;; marked. If those marks would become connected with the old
1626 ;; `c-in-sws' range around the macro then we could get a ladder
1627 ;; with one end outside the macro and the other at some whitespace
1628 ;; within it.
1630 ;; The main motivation for this system is to increase the speed in
1631 ;; skipping over the large whitespace regions that can occur at the
1632 ;; top level in e.g. header files that contain a lot of comments and
1633 ;; cpp directives. For small comments inside code it's probably
1634 ;; slower than using `forward-comment' straightforwardly, but speed is
1635 ;; not a significant factor there anyway.
1637 ; (defface c-debug-is-sws-face
1638 ; '((t (:background "GreenYellow")))
1639 ; "Debug face to mark the `c-is-sws' property.")
1640 ; (defface c-debug-in-sws-face
1641 ; '((t (:underline t)))
1642 ; "Debug face to mark the `c-in-sws' property.")
1644 ; (defun c-debug-put-sws-faces ()
1645 ; ;; Put the sws debug faces on all the `c-is-sws' and `c-in-sws'
1646 ; ;; properties in the buffer.
1647 ; (interactive)
1648 ; (save-excursion
1649 ; (c-save-buffer-state (in-face)
1650 ; (goto-char (point-min))
1651 ; (setq in-face (if (get-text-property (point) 'c-is-sws)
1652 ; (point)))
1653 ; (while (progn
1654 ; (goto-char (next-single-property-change
1655 ; (point) 'c-is-sws nil (point-max)))
1656 ; (if in-face
1657 ; (progn
1658 ; (c-debug-add-face in-face (point) 'c-debug-is-sws-face)
1659 ; (setq in-face nil))
1660 ; (setq in-face (point)))
1661 ; (not (eobp))))
1662 ; (goto-char (point-min))
1663 ; (setq in-face (if (get-text-property (point) 'c-in-sws)
1664 ; (point)))
1665 ; (while (progn
1666 ; (goto-char (next-single-property-change
1667 ; (point) 'c-in-sws nil (point-max)))
1668 ; (if in-face
1669 ; (progn
1670 ; (c-debug-add-face in-face (point) 'c-debug-in-sws-face)
1671 ; (setq in-face nil))
1672 ; (setq in-face (point)))
1673 ; (not (eobp)))))))
1675 (defmacro c-debug-sws-msg (&rest args)
1676 ;;`(message ,@args)
1679 (defmacro c-put-is-sws (beg end)
1680 ;; This macro does a hidden buffer change.
1681 `(let ((beg ,beg) (end ,end))
1682 (put-text-property beg end 'c-is-sws t)
1683 ,@(when (facep 'c-debug-is-sws-face)
1684 `((c-debug-add-face beg end 'c-debug-is-sws-face)))))
1686 (defmacro c-put-in-sws (beg end)
1687 ;; This macro does a hidden buffer change.
1688 `(let ((beg ,beg) (end ,end))
1689 (put-text-property beg end 'c-in-sws t)
1690 ,@(when (facep 'c-debug-is-sws-face)
1691 `((c-debug-add-face beg end 'c-debug-in-sws-face)))))
1693 (defmacro c-remove-is-sws (beg end)
1694 ;; This macro does a hidden buffer change.
1695 `(let ((beg ,beg) (end ,end))
1696 (remove-text-properties beg end '(c-is-sws nil))
1697 ,@(when (facep 'c-debug-is-sws-face)
1698 `((c-debug-remove-face beg end 'c-debug-is-sws-face)))))
1700 (defmacro c-remove-in-sws (beg end)
1701 ;; This macro does a hidden buffer change.
1702 `(let ((beg ,beg) (end ,end))
1703 (remove-text-properties beg end '(c-in-sws nil))
1704 ,@(when (facep 'c-debug-is-sws-face)
1705 `((c-debug-remove-face beg end 'c-debug-in-sws-face)))))
1707 (defmacro c-remove-is-and-in-sws (beg end)
1708 ;; This macro does a hidden buffer change.
1709 `(let ((beg ,beg) (end ,end))
1710 (remove-text-properties beg end '(c-is-sws nil c-in-sws nil))
1711 ,@(when (facep 'c-debug-is-sws-face)
1712 `((c-debug-remove-face beg end 'c-debug-is-sws-face)
1713 (c-debug-remove-face beg end 'c-debug-in-sws-face)))))
1715 ;; The type of literal position `end' is in in a `before-change-functions'
1716 ;; function - one of `c', `c++', `pound', or nil (but NOT `string').
1717 (defvar c-sws-lit-type nil)
1718 ;; A cons (START . STOP) of the bounds of the comment or CPP construct
1719 ;; enclosing END, if any, else nil.
1720 (defvar c-sws-lit-limits nil)
1722 (defun c-invalidate-sws-region-before (end)
1723 ;; Called from c-before-change. END is the end of the change region, the
1724 ;; standard parameter given to all before-change-functions.
1726 ;; Note whether END is inside a comment or CPP construct, and if so note its
1727 ;; bounds in `c-sws-lit-limits' and type in `c-sws-lit-type'.
1728 (save-excursion
1729 (goto-char end)
1730 (let* ((limits (c-literal-limits))
1731 (lit-type (c-literal-type limits)))
1732 (cond
1733 ((memq lit-type '(c c++))
1734 (setq c-sws-lit-type lit-type
1735 c-sws-lit-limits limits))
1736 ((c-beginning-of-macro)
1737 (setq c-sws-lit-type 'pound
1738 c-sws-lit-limits (cons (point)
1739 (progn (c-end-of-macro) (point)))))
1740 (t (setq c-sws-lit-type nil
1741 c-sws-lit-limits nil))))))
1743 (defun c-invalidate-sws-region-after-del (beg end old-len)
1744 ;; Text has been deleted, OLD-LEN characters of it starting from position
1745 ;; BEG. END is typically eq to BEG. Should there have been a comment or
1746 ;; CPP construct open at END before the deletion, check whether this
1747 ;; deletion deleted or "damaged" its opening delimiter. If so, return the
1748 ;; current position of where the construct ended, otherwise return nil.
1749 (when c-sws-lit-limits
1750 (setcdr c-sws-lit-limits (- (cdr c-sws-lit-limits) old-len))
1751 (if (and (< beg (+ (car c-sws-lit-limits) 2)) ; A lazy assumption that
1752 ; comment delimiters are 2
1753 ; chars long.
1754 (or (get-text-property end 'c-in-sws)
1755 (next-single-property-change end 'c-in-sws nil
1756 (cdr c-sws-lit-limits))
1757 (get-text-property end 'c-is-sws)
1758 (next-single-property-change end 'c-is-sws nil
1759 (cdr c-sws-lit-limits))))
1760 (cdr c-sws-lit-limits))))
1762 (defun c-invalidate-sws-region-after-ins (end)
1763 ;; Text has been inserted, ending at buffer position END. Should there be a
1764 ;; literal or CPP construct open at END, check whether there are `c-in-sws'
1765 ;; or `c-is-sws' text properties inside this literal. If there are, return
1766 ;; the buffer position of the end of the literal, else return nil.
1767 (save-excursion
1768 (let* ((limits (c-literal-limits))
1769 (lit-type (c-literal-type limits)))
1770 (goto-char end)
1771 (when (and (not (memq lit-type '(c c++)))
1772 (c-beginning-of-macro))
1773 (setq lit-type 'pound
1774 limits (cons (point)
1775 (progn (c-end-of-macro) (point)))))
1776 (when (memq lit-type '(c c++ pound))
1777 (let ((next-in (next-single-property-change (car limits) 'c-in-sws
1778 nil (cdr limits)))
1779 (next-is (next-single-property-change (car limits) 'c-is-sws
1780 nil (cdr limits))))
1781 (and (or next-in next-is)
1782 (cdr limits)))))))
1784 (defun c-invalidate-sws-region-after (beg end old-len)
1785 ;; Called from `after-change-functions'. Remove any stale `c-in-sws' or
1786 ;; `c-is-sws' text properties from the vicinity of the change. BEG, END,
1787 ;; and OLD-LEN are the standard arguments given to after-change functions.
1789 ;; Note that if `c-forward-sws' or `c-backward-sws' are used outside
1790 ;; `c-save-buffer-state' or similar then this will remove the cache
1791 ;; properties right after they're added.
1793 ;; This function does hidden buffer changes.
1794 (let ((del-end
1795 (and (> old-len 0)
1796 (c-invalidate-sws-region-after-del beg end old-len)))
1797 (ins-end
1798 (and (> end beg)
1799 (c-invalidate-sws-region-after-ins end))))
1800 (save-excursion
1801 ;; Adjust the end to remove the properties in any following simple
1802 ;; ws up to and including the next line break, if there is any
1803 ;; after the changed region. This is necessary e.g. when a rung
1804 ;; marked empty line is converted to a line comment by inserting
1805 ;; "//" before the line break. In that case the line break would
1806 ;; keep the rung mark which could make a later `c-backward-sws'
1807 ;; move into the line comment instead of over it.
1808 (goto-char end)
1809 (skip-chars-forward " \t\f\v")
1810 (when (and (eolp) (not (eobp)))
1811 (setq end (1+ (point)))))
1813 (when (and (= beg end)
1814 (get-text-property beg 'c-in-sws)
1815 (> beg (point-min))
1816 (get-text-property (1- beg) 'c-in-sws))
1817 ;; Ensure that an `c-in-sws' range gets broken. Note that it isn't
1818 ;; safe to keep a range that was continuous before the change. E.g:
1820 ;; #define foo
1821 ;; \
1822 ;; bar
1824 ;; There can be a "ladder" between "#" and "b". Now, if the newline
1825 ;; after "foo" is removed then "bar" will become part of the cpp
1826 ;; directive instead of a syntactically relevant token. In that
1827 ;; case there's no longer syntactic ws from "#" to "b".
1828 (setq beg (1- beg)))
1830 (setq end (max (or del-end end)
1831 (or ins-end end)
1832 end))
1834 (c-debug-sws-msg "c-invalidate-sws-region-after [%s..%s]" beg end)
1835 (c-remove-is-and-in-sws beg end)))
1837 (defun c-forward-sws ()
1838 ;; Used by `c-forward-syntactic-ws' to implement the unbounded search.
1840 ;; This function might do hidden buffer changes.
1842 (let (;; `rung-pos' is set to a position as early as possible in the
1843 ;; unmarked part of the simple ws region.
1844 (rung-pos (point)) next-rung-pos rung-end-pos last-put-in-sws-pos
1845 rung-is-marked next-rung-is-marked simple-ws-end
1846 ;; `safe-start' is set when it's safe to cache the start position.
1847 ;; It's not set if we've initially skipped over comments and line
1848 ;; continuations since we might have gone out through the end of a
1849 ;; macro then. This provision makes `c-forward-sws' not populate the
1850 ;; cache in the majority of cases, but otoh is `c-backward-sws' by far
1851 ;; more common.
1852 safe-start)
1854 ;; Skip simple ws and do a quick check on the following character to see
1855 ;; if it's anything that can't start syntactic ws, so we can bail out
1856 ;; early in the majority of cases when there just are a few ws chars.
1857 (skip-chars-forward " \t\n\r\f\v")
1858 (when (or (looking-at c-syntactic-ws-start)
1859 (and c-opt-cpp-prefix
1860 (looking-at c-noise-macro-name-re)))
1862 (setq rung-end-pos (min (1+ (point)) (point-max)))
1863 (if (setq rung-is-marked (text-property-any rung-pos rung-end-pos
1864 'c-is-sws t))
1865 ;; Find the last rung position to avoid setting properties in all
1866 ;; the cases when the marked rung is complete.
1867 ;; (`next-single-property-change' is certain to move at least one
1868 ;; step forward.)
1869 (setq rung-pos (1- (c-next-single-property-change
1870 rung-is-marked 'c-is-sws nil rung-end-pos)))
1871 ;; Got no marked rung here. Since the simple ws might have started
1872 ;; inside a line comment or cpp directive we must set `rung-pos' as
1873 ;; high as possible.
1874 (setq rung-pos (point)))
1876 (with-silent-modifications
1877 (while
1878 (progn
1879 ;; In the following while form, we move over a "ladder" and
1880 ;; following simple WS each time round the loop, appending the WS
1881 ;; onto the ladder, joining adjacent ladders, and terminating when
1882 ;; there is no more WS or we reach EOB.
1883 (while
1884 (when (and rung-is-marked
1885 (get-text-property (point) 'c-in-sws))
1887 ;; The following search is the main reason that `c-in-sws'
1888 ;; and `c-is-sws' aren't combined to one property.
1889 (goto-char (c-next-single-property-change
1890 (point) 'c-in-sws nil (point-max)))
1891 (unless (get-text-property (point) 'c-is-sws)
1892 ;; If the `c-in-sws' region extended past the last
1893 ;; `c-is-sws' char we have to go back a bit.
1894 (or (get-text-property (1- (point)) 'c-is-sws)
1895 (goto-char (previous-single-property-change
1896 (point) 'c-is-sws)))
1897 (backward-char))
1899 (c-debug-sws-msg
1900 "c-forward-sws cached move %s -> %s (max %s)"
1901 rung-pos (point) (point-max))
1903 (setq rung-pos (point))
1904 (and (> (skip-chars-forward " \t\n\r\f\v") 0)
1905 (not (eobp))))
1907 ;; We'll loop here if there is simple ws after the last rung.
1908 ;; That means that there's been some change in it and it's
1909 ;; possible that we've stepped into another ladder, so extend
1910 ;; the previous one to join with it if there is one, and try to
1911 ;; use the cache again.
1912 (c-debug-sws-msg
1913 "c-forward-sws extending rung with [%s..%s] (max %s)"
1914 (1+ rung-pos) (1+ (point)) (point-max))
1915 (unless (get-text-property (point) 'c-is-sws)
1916 ;; Remove any `c-in-sws' property from the last char of
1917 ;; the rung before we mark it with `c-is-sws', so that we
1918 ;; won't connect with the remains of a broken "ladder".
1919 (c-remove-in-sws (point) (1+ (point))))
1920 (c-put-is-sws (1+ rung-pos)
1921 (1+ (point)))
1922 (c-put-in-sws rung-pos
1923 (setq rung-pos (point)
1924 last-put-in-sws-pos rung-pos)))
1926 ;; Now move over any comments (x)or a CPP construct.
1927 (setq simple-ws-end (point))
1928 (c-forward-comments)
1930 (cond
1931 ((/= (point) simple-ws-end)
1932 ;; Skipped over comments. Don't cache at eob in case the buffer
1933 ;; is narrowed.
1934 (not (eobp)))
1936 ((save-excursion
1937 (and c-opt-cpp-prefix
1938 (looking-at c-opt-cpp-start)
1939 (progn (skip-chars-backward " \t")
1940 (bolp))
1941 (or (bobp)
1942 (progn (backward-char)
1943 (not (eq (char-before) ?\\))))))
1944 ;; Skip a preprocessor directive.
1945 (end-of-line)
1946 (while (and (eq (char-before) ?\\)
1947 (= (forward-line 1) 0))
1948 (end-of-line))
1949 (forward-line 1)
1950 (setq safe-start t)
1951 ;; Don't cache at eob in case the buffer is narrowed.
1952 (not (eobp)))
1954 ((and c-opt-cpp-prefix
1955 (looking-at c-noise-macro-name-re))
1956 ;; Skip over a noise macro.
1957 (goto-char (match-end 1))
1958 (setq safe-start t)
1959 (not (eobp)))))
1961 ;; We've searched over a piece of non-white syntactic ws. See if this
1962 ;; can be cached.
1963 (setq next-rung-pos (point))
1964 (skip-chars-forward " \t\n\r\f\v")
1965 (setq rung-end-pos (min (1+ (point)) (point-max)))
1967 (if (or
1968 ;; Cache if we haven't skipped comments only, and if we started
1969 ;; either from a marked rung or from a completely uncached
1970 ;; position.
1971 (and safe-start
1972 (or rung-is-marked
1973 (not (get-text-property simple-ws-end 'c-in-sws))))
1975 ;; See if there's a marked rung in the encountered simple ws. If
1976 ;; so then we can cache, unless `safe-start' is nil. Even then
1977 ;; we need to do this to check if the cache can be used for the
1978 ;; next step.
1979 (and (setq next-rung-is-marked
1980 (text-property-any next-rung-pos rung-end-pos
1981 'c-is-sws t))
1982 safe-start))
1984 (progn
1985 (c-debug-sws-msg
1986 "c-forward-sws caching [%s..%s] - [%s..%s] (max %s)"
1987 rung-pos (1+ simple-ws-end) next-rung-pos rung-end-pos
1988 (point-max))
1990 ;; Remove the properties for any nested ws that might be cached.
1991 ;; Only necessary for `c-is-sws' since `c-in-sws' will be set
1992 ;; anyway.
1993 (c-remove-is-sws (1+ simple-ws-end) next-rung-pos)
1994 (unless (and rung-is-marked (= rung-pos simple-ws-end))
1995 (c-put-is-sws rung-pos
1996 (1+ simple-ws-end))
1997 (setq rung-is-marked t))
1998 (c-put-in-sws rung-pos
1999 (setq rung-pos (point)
2000 last-put-in-sws-pos rung-pos))
2001 (unless (get-text-property (1- rung-end-pos) 'c-is-sws)
2002 ;; Remove any `c-in-sws' property from the last char of
2003 ;; the rung before we mark it with `c-is-sws', so that we
2004 ;; won't connect with the remains of a broken "ladder".
2005 (c-remove-in-sws (1- rung-end-pos) rung-end-pos))
2006 (c-put-is-sws next-rung-pos
2007 rung-end-pos))
2009 (c-debug-sws-msg
2010 "c-forward-sws not caching [%s..%s] - [%s..%s] (max %s)"
2011 rung-pos (1+ simple-ws-end) next-rung-pos rung-end-pos
2012 (point-max))
2014 ;; Set `rung-pos' for the next rung. It's the same thing here as
2015 ;; initially, except that the rung position is set as early as
2016 ;; possible since we can't be in the ending ws of a line comment or
2017 ;; cpp directive now.
2018 (if (setq rung-is-marked next-rung-is-marked)
2019 (setq rung-pos (1- (c-next-single-property-change
2020 rung-is-marked 'c-is-sws nil rung-end-pos)))
2021 (setq rung-pos next-rung-pos))
2022 (setq safe-start t)))
2024 ;; Make sure that the newly marked `c-in-sws' region doesn't connect to
2025 ;; another one after the point (which might occur when editing inside a
2026 ;; comment or macro).
2027 (when (eq last-put-in-sws-pos (point))
2028 (cond ((< last-put-in-sws-pos (point-max))
2029 (c-debug-sws-msg
2030 "c-forward-sws clearing at %s for cache separation"
2031 last-put-in-sws-pos)
2032 (c-remove-in-sws last-put-in-sws-pos
2033 (1+ last-put-in-sws-pos)))
2035 ;; If at eob we have to clear the last character before the end
2036 ;; instead since the buffer might be narrowed and there might
2037 ;; be a `c-in-sws' after (point-max). In this case it's
2038 ;; necessary to clear both properties.
2039 (c-debug-sws-msg
2040 "c-forward-sws clearing thoroughly at %s for cache separation"
2041 (1- last-put-in-sws-pos))
2042 (c-remove-is-and-in-sws (1- last-put-in-sws-pos)
2043 last-put-in-sws-pos))))
2044 ))))
2046 (defun c-backward-sws ()
2047 ;; Used by `c-backward-syntactic-ws' to implement the unbounded search.
2049 ;; This function might do hidden buffer changes.
2051 (let (;; `rung-pos' is set to a position as late as possible in the unmarked
2052 ;; part of the simple ws region.
2053 (rung-pos (point)) next-rung-pos last-put-in-sws-pos
2054 rung-is-marked simple-ws-beg cmt-skip-pos)
2056 ;; Skip simple horizontal ws and do a quick check on the preceding
2057 ;; character to see if it's anything that can't end syntactic ws, so we can
2058 ;; bail out early in the majority of cases when there just are a few ws
2059 ;; chars. Newlines are complicated in the backward direction, so we can't
2060 ;; skip over them.
2061 (skip-chars-backward " \t\f")
2062 (when (and (not (bobp))
2063 (save-excursion
2064 (backward-char)
2065 (or (looking-at c-syntactic-ws-end)
2066 (and c-opt-cpp-prefix
2067 (looking-at c-symbol-char-key)
2068 (progn (c-beginning-of-current-token)
2069 (looking-at c-noise-macro-name-re))))))
2070 ;; Try to find a rung position in the simple ws preceding point, so that
2071 ;; we can get a cache hit even if the last bit of the simple ws has
2072 ;; changed recently.
2073 (setq simple-ws-beg (point))
2074 (skip-chars-backward " \t\n\r\f\v")
2075 (if (setq rung-is-marked (text-property-any
2076 (point) (min (1+ rung-pos) (point-max))
2077 'c-is-sws t))
2078 ;; `rung-pos' will be the earliest marked position, which means that
2079 ;; there might be later unmarked parts in the simple ws region.
2080 ;; It's not worth the effort to fix that; the last part of the
2081 ;; simple ws is also typically edited often, so it could be wasted.
2082 (goto-char (setq rung-pos rung-is-marked))
2083 (goto-char simple-ws-beg))
2085 (with-silent-modifications
2086 (while
2087 (progn
2088 ;; Each time round the next while form, we move back over a ladder
2089 ;; and append any simple WS preceding it, if possible joining with
2090 ;; the previous ladder.
2091 (while
2092 (when (and rung-is-marked
2093 (not (bobp))
2094 (get-text-property (1- (point)) 'c-in-sws))
2096 ;; The following search is the main reason that `c-in-sws'
2097 ;; and `c-is-sws' aren't combined to one property.
2098 (goto-char (previous-single-property-change
2099 (point) 'c-in-sws nil (point-min)))
2100 (unless (get-text-property (point) 'c-is-sws)
2101 ;; If the `c-in-sws' region extended past the first
2102 ;; `c-is-sws' char we have to go forward a bit.
2103 (goto-char (c-next-single-property-change
2104 (point) 'c-is-sws)))
2106 (c-debug-sws-msg
2107 "c-backward-sws cached move %s <- %s (min %s)"
2108 (point) rung-pos (point-min))
2110 (setq rung-pos (point))
2111 (if (and (< (min (skip-chars-backward " \t\f\v")
2112 (progn
2113 (setq simple-ws-beg (point))
2114 (skip-chars-backward " \t\n\r\f\v")))
2116 (setq rung-is-marked
2117 (text-property-any (point) rung-pos
2118 'c-is-sws t)))
2120 (goto-char simple-ws-beg)
2121 nil))
2123 ;; We'll loop here if there is simple ws before the first rung.
2124 ;; That means that there's been some change in it and it's
2125 ;; possible that we've stepped into another ladder, so extend
2126 ;; the previous one to join with it if there is one, and try to
2127 ;; use the cache again.
2128 (c-debug-sws-msg
2129 "c-backward-sws extending rung with [%s..%s] (min %s)"
2130 rung-is-marked rung-pos (point-min))
2131 (unless (get-text-property (1- rung-pos) 'c-is-sws)
2132 ;; Remove any `c-in-sws' property from the last char of
2133 ;; the rung before we mark it with `c-is-sws', so that we
2134 ;; won't connect with the remains of a broken "ladder".
2135 (c-remove-in-sws (1- rung-pos) rung-pos))
2136 (c-put-is-sws rung-is-marked
2137 rung-pos)
2138 (c-put-in-sws rung-is-marked
2139 (1- rung-pos))
2140 (setq rung-pos rung-is-marked
2141 last-put-in-sws-pos rung-pos))
2143 (c-backward-comments)
2144 (setq cmt-skip-pos (point))
2146 (cond
2147 ((and c-opt-cpp-prefix
2148 (/= cmt-skip-pos simple-ws-beg)
2149 (c-beginning-of-macro))
2150 ;; Inside a cpp directive. See if it should be skipped over.
2151 (let ((cpp-beg (point)))
2153 ;; Move back over all line continuations in the region skipped
2154 ;; over by `c-backward-comments'. If we go past it then we
2155 ;; started inside the cpp directive.
2156 (goto-char simple-ws-beg)
2157 (beginning-of-line)
2158 (while (and (> (point) cmt-skip-pos)
2159 (progn (backward-char)
2160 (eq (char-before) ?\\)))
2161 (beginning-of-line))
2163 (if (< (point) cmt-skip-pos)
2164 ;; Don't move past the cpp directive if we began inside
2165 ;; it. Note that the position at the end of the last line
2166 ;; of the macro is also considered to be within it.
2167 (progn (goto-char cmt-skip-pos)
2168 nil)
2170 ;; It's worthwhile to spend a little bit of effort on finding
2171 ;; the end of the macro, to get a good `simple-ws-beg'
2172 ;; position for the cache. Note that `c-backward-comments'
2173 ;; could have stepped over some comments before going into
2174 ;; the macro, and then `simple-ws-beg' must be kept on the
2175 ;; same side of those comments.
2176 (goto-char simple-ws-beg)
2177 (skip-chars-backward " \t\n\r\f\v")
2178 (if (eq (char-before) ?\\)
2179 (forward-char))
2180 (forward-line 1)
2181 (if (< (point) simple-ws-beg)
2182 ;; Might happen if comments after the macro were skipped
2183 ;; over.
2184 (setq simple-ws-beg (point)))
2186 (goto-char cpp-beg)
2187 t)))
2189 ((/= (save-excursion
2190 (skip-chars-forward " \t\n\r\f\v" simple-ws-beg)
2191 (setq next-rung-pos (point)))
2192 simple-ws-beg)
2193 ;; Skipped over comments. Must put point at the end of
2194 ;; the simple ws at point since we might be after a line
2195 ;; comment or cpp directive that's been partially
2196 ;; narrowed out, and we can't risk marking the simple ws
2197 ;; at the end of it.
2198 (goto-char next-rung-pos)
2201 ((and c-opt-cpp-prefix
2202 (save-excursion
2203 (and (< (skip-syntax-backward "w_") 0)
2204 (progn (setq next-rung-pos (point))
2205 (looking-at c-noise-macro-name-re)))))
2206 ;; Skipped over a noise macro
2207 (goto-char next-rung-pos)
2208 t)))
2210 ;; We've searched over a piece of non-white syntactic ws. See if this
2211 ;; can be cached.
2212 (setq next-rung-pos (point))
2213 (skip-chars-backward " \t\f\v")
2215 (if (or
2216 ;; Cache if we started either from a marked rung or from a
2217 ;; completely uncached position.
2218 rung-is-marked
2219 (not (get-text-property (1- simple-ws-beg) 'c-in-sws))
2221 ;; Cache if there's a marked rung in the encountered simple ws.
2222 (save-excursion
2223 (skip-chars-backward " \t\n\r\f\v")
2224 (text-property-any (point) (min (1+ next-rung-pos) (point-max))
2225 'c-is-sws t)))
2227 (progn
2228 (c-debug-sws-msg
2229 "c-backward-sws caching [%s..%s] - [%s..%s] (min %s)"
2230 (point) (1+ next-rung-pos)
2231 simple-ws-beg (min (1+ rung-pos) (point-max))
2232 (point-min))
2234 ;; Remove the properties for any nested ws that might be cached.
2235 ;; Only necessary for `c-is-sws' since `c-in-sws' will be set
2236 ;; anyway.
2237 (c-remove-is-sws (1+ next-rung-pos) simple-ws-beg)
2238 (unless (and rung-is-marked (= simple-ws-beg rung-pos))
2239 (let ((rung-end-pos (min (1+ rung-pos) (point-max))))
2240 (unless (get-text-property (1- rung-end-pos) 'c-is-sws)
2241 ;; Remove any `c-in-sws' property from the last char of
2242 ;; the rung before we mark it with `c-is-sws', so that we
2243 ;; won't connect with the remains of a broken "ladder".
2244 (c-remove-in-sws (1- rung-end-pos) rung-end-pos))
2245 (c-put-is-sws simple-ws-beg
2246 rung-end-pos)
2247 (setq rung-is-marked t)))
2248 (c-put-in-sws (setq simple-ws-beg (point)
2249 last-put-in-sws-pos simple-ws-beg)
2250 rung-pos)
2251 (c-put-is-sws (setq rung-pos simple-ws-beg)
2252 (1+ next-rung-pos)))
2254 (c-debug-sws-msg
2255 "c-backward-sws not caching [%s..%s] - [%s..%s] (min %s)"
2256 (point) (1+ next-rung-pos)
2257 simple-ws-beg (min (1+ rung-pos) (point-max))
2258 (point-min))
2259 (setq rung-pos next-rung-pos
2260 simple-ws-beg (point))
2263 ;; Make sure that the newly marked `c-in-sws' region doesn't connect to
2264 ;; another one before the point (which might occur when editing inside a
2265 ;; comment or macro).
2266 (when (eq last-put-in-sws-pos (point))
2267 (cond ((< (point-min) last-put-in-sws-pos)
2268 (c-debug-sws-msg
2269 "c-backward-sws clearing at %s for cache separation"
2270 (1- last-put-in-sws-pos))
2271 (c-remove-in-sws (1- last-put-in-sws-pos)
2272 last-put-in-sws-pos))
2273 ((> (point-min) 1)
2274 ;; If at bob and the buffer is narrowed, we have to clear the
2275 ;; character we're standing on instead since there might be a
2276 ;; `c-in-sws' before (point-min). In this case it's necessary
2277 ;; to clear both properties.
2278 (c-debug-sws-msg
2279 "c-backward-sws clearing thoroughly at %s for cache separation"
2280 last-put-in-sws-pos)
2281 (c-remove-is-and-in-sws last-put-in-sws-pos
2282 (1+ last-put-in-sws-pos)))))
2283 ))))
2286 ;; Other whitespace tools
2287 (defun c-partial-ws-p (beg end)
2288 ;; Is the region (beg end) WS, and is there WS (or BOB/EOB) next to the
2289 ;; region? This is a "heuristic" function. .....
2291 ;; The motivation for the second bit is to check whether removing this
2292 ;; region would coalesce two symbols.
2294 ;; FIXME!!! This function doesn't check virtual semicolons in any way. Be
2295 ;; careful about using this function for, e.g. AWK. (2007/3/7)
2296 (save-excursion
2297 (let ((end+1 (min (1+ end) (point-max))))
2298 (or (progn (goto-char (max (point-min) (1- beg)))
2299 (c-skip-ws-forward end)
2300 (eq (point) end))
2301 (progn (goto-char beg)
2302 (c-skip-ws-forward end+1)
2303 (eq (point) end+1))))))
2305 ;; A system for finding noteworthy parens before the point.
2307 (defconst c-state-cache-too-far 5000)
2308 ;; A maximum comfortable scanning distance, e.g. between
2309 ;; `c-state-cache-good-pos' and "HERE" (where we call c-parse-state). When
2310 ;; this distance is exceeded, we take "emergency measures", e.g. by clearing
2311 ;; the cache and starting again from point-min or a beginning of defun. This
2312 ;; value can be tuned for efficiency or set to a lower value for testing.
2314 (defvar c-state-cache nil)
2315 (make-variable-buffer-local 'c-state-cache)
2316 ;; The state cache used by `c-parse-state' to cut down the amount of
2317 ;; searching. It's the result from some earlier `c-parse-state' call. See
2318 ;; `c-parse-state''s doc string for details of its structure.
2320 ;; The use of the cached info is more effective if the next
2321 ;; `c-parse-state' call is on a line close by the one the cached state
2322 ;; was made at; the cache can actually slow down a little if the
2323 ;; cached state was made very far back in the buffer. The cache is
2324 ;; most effective if `c-parse-state' is used on each line while moving
2325 ;; forward.
2327 (defvar c-state-cache-good-pos 1)
2328 (make-variable-buffer-local 'c-state-cache-good-pos)
2329 ;; This is a position where `c-state-cache' is known to be correct, or
2330 ;; nil (see below). It's a position inside one of the recorded unclosed
2331 ;; parens or the top level, but not further nested inside any literal or
2332 ;; subparen that is closed before the last recorded position.
2334 ;; The exact position is chosen to try to be close to yet earlier than
2335 ;; the position where `c-state-cache' will be called next. Right now
2336 ;; the heuristic is to set it to the position after the last found
2337 ;; closing paren (of any type) before the line on which
2338 ;; `c-parse-state' was called. That is chosen primarily to work well
2339 ;; with refontification of the current line.
2341 ;; 2009-07-28: When `c-state-point-min' and the last position where
2342 ;; `c-parse-state' or for which `c-invalidate-state-cache' was called, are
2343 ;; both in the same literal, there is no such "good position", and
2344 ;; c-state-cache-good-pos is then nil. This is the ONLY circumstance in which
2345 ;; it can be nil. In this case, `c-state-point-min-literal' will be non-nil.
2347 ;; 2009-06-12: In a brace desert, c-state-cache-good-pos may also be in
2348 ;; the middle of the desert, as long as it is not within a brace pair
2349 ;; recorded in `c-state-cache' or a paren/bracket pair.
2351 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2352 ;; We maintain a simple cache of positions which aren't in a literal, so as to
2353 ;; speed up testing for non-literality.
2354 (defconst c-state-nonlit-pos-interval 3000)
2355 ;; The approximate interval between entries in `c-state-nonlit-pos-cache'.
2357 (defvar c-state-nonlit-pos-cache nil)
2358 (make-variable-buffer-local 'c-state-nonlit-pos-cache)
2359 ;; A list of buffer positions which are known not to be in a literal or a cpp
2360 ;; construct. This is ordered with higher positions at the front of the list.
2361 ;; Only those which are less than `c-state-nonlit-pos-cache-limit' are valid.
2363 (defvar c-state-nonlit-pos-cache-limit 1)
2364 (make-variable-buffer-local 'c-state-nonlit-pos-cache-limit)
2365 ;; An upper limit on valid entries in `c-state-nonlit-pos-cache'. This is
2366 ;; reduced by buffer changes, and increased by invocations of
2367 ;; `c-state-literal-at'.
2369 (defvar c-state-semi-nonlit-pos-cache nil)
2370 (make-variable-buffer-local 'c-state-semi-nonlit-pos-cache)
2371 ;; A list of elements which are either buffer positions (when such positions
2372 ;; are not in literals) or lists of the form (POS TYPE START), where POS is
2373 ;; a buffer position inside a literal, TYPE is the type of the literal
2374 ;; ('string, 'c, or 'c++) and START is the start of the literal.
2376 (defvar c-state-semi-nonlit-pos-cache-limit 1)
2377 (make-variable-buffer-local 'c-state-semi-nonlit-pos-cache-limit)
2378 ;; An upper limit on valid entries in `c-state-semi-nonlit-pos-cache'. This
2379 ;; is reduced by buffer changes, and increased by invocations of
2380 ;; `c-parse-ps-state-below'.
2382 (defsubst c-truncate-semi-nonlit-pos-cache (pos)
2383 ;; Truncate the upper bound of the cache `c-state-semi-nonlit-pos-cache' to
2384 ;; POS, if it is higher than that position.
2385 (setq c-state-semi-nonlit-pos-cache-limit
2386 (min c-state-semi-nonlit-pos-cache-limit pos)))
2388 (defun c-state-semi-pp-to-literal (here &optional not-in-delimiter)
2389 ;; Do a parse-partial-sexp from a position in the buffer before HERE which
2390 ;; isn't in a literal, and return information about HERE, either:
2391 ;; (STATE TYPE BEG) if HERE is in a literal; or
2392 ;; (STATE) otherwise,
2393 ;; where STATE is the parsing state at HERE, TYPE is the type of the literal
2394 ;; enclosing HERE, (one of 'string, 'c, 'c++) and BEG is the starting
2395 ;; position of that literal (including the delimiter).
2397 ;; Unless NOT-IN-DELIMITER is non-nil, when TO is inside a two-character
2398 ;; comment opener, this is recognized as being in a comment literal.
2400 ;; Only elements 3 (in a string), 4 (in a comment), 5 (following a quote), 7
2401 ;; (comment type), and 8 (start of comment/string), and possibly 10 (in
2402 ;; newer Emacsen only, the syntax of a position after a potential first char
2403 ;; of a two char construct) of STATE are valid.
2404 (save-excursion
2405 (save-restriction
2406 (widen)
2407 (save-match-data
2408 (let* ((base-and-state (c-parse-ps-state-below here))
2409 (base (car base-and-state))
2410 (s (cdr base-and-state))
2411 (s (parse-partial-sexp base here nil nil s))
2413 (cond
2414 ((or (nth 3 s)
2415 (and (nth 4 s)
2416 (not (eq (nth 7 s) 'syntax-table)))) ; in a string or comment
2417 (setq ty (cond
2418 ((nth 3 s) 'string)
2419 ((nth 7 s) 'c++)
2420 (t 'c)))
2421 (list s ty (nth 8 s)))
2423 ((and (not not-in-delimiter) ; inside a comment starter
2424 (not (bobp))
2425 (progn (backward-char)
2426 (and (not (and (memq 'category-properties c-emacs-features)
2427 (looking-at "\\s!")))
2428 (looking-at c-comment-start-regexp))))
2429 (setq ty (if (looking-at c-block-comment-start-regexp) 'c 'c++))
2430 (list s ty (point)))
2432 (t (list s))))))))
2434 (defun c-state-full-pp-to-literal (here &optional not-in-delimiter)
2435 ;; This function will supersede c-state-pp-to-literal.
2437 ;; Do a parse-partial-sexp from a position in the buffer before HERE which
2438 ;; isn't in a literal, and return information about HERE, either:
2439 ;; (STATE TYPE (BEG . END)) if HERE is in a literal; or
2440 ;; (STATE) otherwise,
2441 ;; where STATE is the parsing state at HERE, TYPE is the type of the literal
2442 ;; enclosing HERE, (one of 'string, 'c, 'c++) and (BEG . END) is the
2443 ;; boundaries of that literal (including the delimiters).
2445 ;; Unless NOT-IN-DELIMITER is non-nil, when TO is inside a two-character
2446 ;; comment opener, this is recognized as being in a comment literal.
2448 ;; Only elements 3 (in a string), 4 (in a comment), 5 (following a quote), 7
2449 ;; (comment type), and 8 (start of comment/string), and possibly 10 (in
2450 ;; newer Emacsen only, the syntax of a position after a potential first char
2451 ;; of a two char construct) of STATE are valid.
2452 (save-excursion
2453 (save-restriction
2454 (widen)
2455 (save-match-data
2456 (let* ((base-and-state (c-parse-ps-state-below here))
2457 (base (car base-and-state))
2458 (s (cdr base-and-state))
2459 (s (parse-partial-sexp base here nil nil s))
2460 ty start)
2461 (cond
2462 ((or (nth 3 s)
2463 (and (nth 4 s)
2464 (not (eq (nth 7 s) 'syntax-table)))) ; in a string or comment
2465 (setq ty (cond
2466 ((nth 3 s) 'string)
2467 ((nth 7 s) 'c++)
2468 (t 'c)))
2469 (setq start (nth 8 s))
2470 (parse-partial-sexp here (point-max)
2471 nil ; TARGETDEPTH
2472 nil ; STOPBEFORE
2473 s ; OLDSTATE
2474 'syntax-table) ; stop at end of literal
2475 (list s ty (cons start (point))))
2477 ((and (not not-in-delimiter) ; inside a comment starter
2478 (not (bobp))
2479 (progn (backward-char)
2480 (and (not (and (memq 'category-properties c-emacs-features)
2481 (looking-at "\\s!")))
2482 (looking-at c-comment-start-regexp))))
2483 (setq ty (if (looking-at c-block-comment-start-regexp) 'c 'c++)
2484 start (point))
2485 (forward-comment 1)
2486 (list s ty (cons start (point))))
2488 (t (list s))))))))
2490 (defun c-state-pp-to-literal (from to &optional not-in-delimiter)
2491 ;; Do a parse-partial-sexp from FROM to TO, returning either
2492 ;; (STATE TYPE (BEG . END)) if TO is in a literal; or
2493 ;; (STATE) otherwise,
2494 ;; where STATE is the parsing state at TO, TYPE is the type of the literal
2495 ;; (one of 'c, 'c++, 'string) and (BEG . END) is the boundaries of the literal,
2496 ;; including the delimiters.
2498 ;; Unless NOT-IN-DELIMITER is non-nil, when TO is inside a two-character
2499 ;; comment opener, this is recognized as being in a comment literal.
2501 ;; Only elements 3 (in a string), 4 (in a comment), 5 (following a quote),
2502 ;; 7 (comment type) and 8 (start of comment/string) (and possibly 9) of
2503 ;; STATE are valid.
2504 (save-excursion
2505 (save-match-data
2506 (let ((s (parse-partial-sexp from to))
2507 ty co-st)
2508 (cond
2509 ((or (nth 3 s)
2510 (and (nth 4 s)
2511 (not (eq (nth 7 s) 'syntax-table)))) ; in a string or comment
2512 (setq ty (cond
2513 ((nth 3 s) 'string)
2514 ((nth 7 s) 'c++)
2515 (t 'c)))
2516 (parse-partial-sexp (point) (point-max)
2517 nil ; TARGETDEPTH
2518 nil ; STOPBEFORE
2519 s ; OLDSTATE
2520 'syntax-table) ; stop at end of literal
2521 `(,s ,ty (,(nth 8 s) . ,(point))))
2523 ((and (not not-in-delimiter) ; inside a comment starter
2524 (not (bobp))
2525 (progn (backward-char)
2526 (and (not (looking-at "\\s!"))
2527 (looking-at c-comment-start-regexp))))
2528 (setq ty (if (looking-at c-block-comment-start-regexp) 'c 'c++)
2529 co-st (point))
2530 (forward-comment 1)
2531 `(,s ,ty (,co-st . ,(point))))
2533 (t `(,s)))))))
2535 (defun c-cache-to-parse-ps-state (elt)
2536 ;; Create a list suitable to use as the old-state parameter to
2537 ;; `parse-partial-sexp', out of ELT. ELT is either just a number, a buffer
2538 ;; position, or it is a list (POS TYPE STARTING-POS). Here POS is the
2539 ;; buffer position the other elements are pertinent for, TYPE is either 'c
2540 ;; or 'c++ (for a comment) or a character (for a string delimiter) or t
2541 ;; (meaning a string fence opened the string), STARTING-POS is the starting
2542 ;; position of the comment or string.
2543 (if (consp elt)
2544 (let ((depth 0) (containing nil) (last nil)
2545 in-string in-comment (after-quote nil)
2546 (min-depth 0) com-style com-str-start (intermediate nil)
2547 (between-syntax nil)
2548 (type (cadr elt)))
2549 (setq com-str-start (car (cddr elt)))
2550 (cond
2551 ((or (numberp type) (eq type t)) ; A string
2552 (setq in-string type))
2553 ((memq type '(c c++)) ; A comment
2554 (setq in-comment t
2555 com-style (if (eq type 'c++) 1 nil)))
2556 (t (c-benign-error "Invalid type %s in c-cache-to-parse-ps-state"
2557 elt)))
2558 (list depth containing last
2559 in-string in-comment after-quote
2560 min-depth com-style com-str-start
2561 intermediate nil))
2562 (copy-tree '(0 nil nil nil nil nil 0 nil nil nil nil))))
2564 (defun c-parse-ps-state-to-cache (state)
2565 ;; Convert STATE, a `parse-partial-sexp' state valid at POINT, to an element
2566 ;; for the `c-state-semi-nonlit-pos-cache' cache. This is either POINT
2567 ;; (when point is not in a literal) or a list (POINT TYPE STARTING-POS),
2568 ;; where TYPE is the type of the literal, either 'string, 'c, or 'c++, and
2569 ;; STARTING-POS is the starting position of the comment or string.
2570 (cond
2571 ((nth 3 state) ; A string
2572 (list (point) (nth 3 state) (nth 8 state)))
2573 ((and (nth 4 state) ; A comment
2574 (not (eq (nth 7 state) 'syntax-table))) ; but not a psuedo comment.
2575 (list (point)
2576 (if (eq (nth 7 state) 1) 'c++ 'c)
2577 (nth 8 state)))
2578 (t ; Neither string nor comment.
2579 (point))))
2581 (defsubst c-ps-state-cache-pos (elt)
2582 ;; Get the buffer position from ELT, an element from the cache
2583 ;; `c-state-semi-nonlit-pos-cache'.
2584 (if (atom elt)
2586 (car elt)))
2588 (defun c-parse-ps-state-below (here)
2589 ;; Given a buffer position HERE, Return a cons (CACHE-POS . STATE), where
2590 ;; CACHE-POS is a position not very far before HERE for which the
2591 ;; parse-partial-sexp STATE is valid. Note that the only valid elements of
2592 ;; STATE are those concerning comments and strings; STATE is the state of a
2593 ;; null `parse-partial-sexp' scan when CACHE-POS is not in a comment or
2594 ;; string.
2595 (save-excursion
2596 (save-restriction
2597 (widen)
2598 (let ((c c-state-semi-nonlit-pos-cache)
2599 elt state pos npos high-elt)
2600 ;; Trim the cache to take account of buffer changes.
2601 (while (and c (> (c-ps-state-cache-pos (car c))
2602 c-state-semi-nonlit-pos-cache-limit))
2603 (setq c (cdr c)))
2604 (setq c-state-semi-nonlit-pos-cache c)
2606 (while (and c (> (c-ps-state-cache-pos (car c)) here))
2607 (setq high-elt (car c))
2608 (setq c (cdr c)))
2609 (setq pos (or (and c (c-ps-state-cache-pos (car c)))
2610 (point-min)))
2612 (if high-elt
2613 (setq state (c-cache-to-parse-ps-state (car c)))
2614 (setq elt (if c (car c) (point-min)))
2615 (setq state
2616 (if c
2617 (c-cache-to-parse-ps-state (car c))
2618 (copy-tree '(0 nil nil nil nil nil 0 nil nil nil nil))))
2619 (while
2620 ;; Add an element to `c-state-semi-nonlit-pos-cache' each iteration.
2621 (<= (setq npos (+ pos c-state-nonlit-pos-interval)) here)
2622 (setq state (parse-partial-sexp pos npos nil nil state))
2623 (setq elt (c-parse-ps-state-to-cache state))
2624 (setq c-state-semi-nonlit-pos-cache
2625 (cons elt c-state-semi-nonlit-pos-cache))
2626 (setq pos npos)))
2628 (if (> pos c-state-semi-nonlit-pos-cache-limit)
2629 (setq c-state-semi-nonlit-pos-cache-limit pos))
2631 (cons pos state)))))
2633 (defun c-state-safe-place (here)
2634 ;; Return a buffer position before HERE which is "safe", i.e. outside any
2635 ;; string, comment, or macro.
2637 ;; NOTE: This function manipulates `c-state-nonlit-pos-cache'. This cache
2638 ;; MAY NOT contain any positions within macros, since macros are frequently
2639 ;; turned into comments by use of the `c-cpp-delimiter' category properties.
2640 ;; We cannot rely on this mechanism whilst determining a cache pos since
2641 ;; this function is also called from outwith `c-parse-state'.
2642 (save-restriction
2643 (widen)
2644 (save-excursion
2645 (let ((c c-state-nonlit-pos-cache)
2646 pos npos high-pos lit macro-beg macro-end)
2647 ;; Trim the cache to take account of buffer changes.
2648 (while (and c (> (car c) c-state-nonlit-pos-cache-limit))
2649 (setq c (cdr c)))
2650 (setq c-state-nonlit-pos-cache c)
2652 (while (and c (> (car c) here))
2653 (setq high-pos (car c))
2654 (setq c (cdr c)))
2655 (setq pos (or (car c) (point-min)))
2657 (unless high-pos
2658 (while
2659 ;; Add an element to `c-state-nonlit-pos-cache' each iteration.
2660 (and
2661 (setq npos
2662 (when (<= (+ pos c-state-nonlit-pos-interval) here)
2663 (+ pos c-state-nonlit-pos-interval)))
2665 ;; Test for being in a literal. If so, go to after it.
2666 (progn
2667 (setq lit (car (cddr (c-state-pp-to-literal pos npos))))
2668 (or (null lit)
2669 (prog1 (<= (cdr lit) here)
2670 (setq npos (cdr lit)))))
2672 ;; Test for being in a macro. If so, go to after it.
2673 (progn
2674 (goto-char npos)
2675 (setq macro-beg
2676 (and (c-beginning-of-macro) (/= (point) npos) (point)))
2677 (when macro-beg
2678 (c-syntactic-end-of-macro)
2679 (or (eobp) (forward-char))
2680 (setq macro-end (point)))
2681 (or (null macro-beg)
2682 (prog1 (<= macro-end here)
2683 (setq npos macro-end)))))
2685 (setq pos npos)
2686 (setq c-state-nonlit-pos-cache (cons pos c-state-nonlit-pos-cache)))
2687 ;; Add one extra element above HERE so as to to avoid the previous
2688 ;; expensive calculation when the next call is close to the current
2689 ;; one. This is especially useful when inside a large macro.
2690 (when npos
2691 (setq c-state-nonlit-pos-cache
2692 (cons npos c-state-nonlit-pos-cache))))
2694 (if (> pos c-state-nonlit-pos-cache-limit)
2695 (setq c-state-nonlit-pos-cache-limit pos))
2696 pos))))
2698 (defun c-state-literal-at (here)
2699 ;; If position HERE is inside a literal, return (START . END), the
2700 ;; boundaries of the literal (which may be outside the accessible bit of the
2701 ;; buffer). Otherwise, return nil.
2703 ;; This function is almost the same as `c-literal-limits'. Previously, it
2704 ;; differed in that it was a lower level function, and that it rigorously
2705 ;; followed the syntax from BOB. `c-literal-limits' is now (2011-12)
2706 ;; virtually identical to this function.
2707 (save-restriction
2708 (widen)
2709 (save-excursion
2710 (let ((pos (c-state-safe-place here)))
2711 (car (cddr (c-state-pp-to-literal pos here)))))))
2713 (defsubst c-state-lit-beg (pos)
2714 ;; Return the start of the literal containing POS, or POS itself.
2715 (or (car (c-state-literal-at pos))
2716 pos))
2718 (defsubst c-state-cache-non-literal-place (pos state)
2719 ;; Return a position outside of a string/comment/macro at or before POS.
2720 ;; STATE is the parse-partial-sexp state at POS.
2721 (let ((res (if (or (nth 3 state) ; in a string?
2722 (and (nth 4 state)
2723 (not (eq (nth 7 state) 'syntax-table)))) ; in a comment?
2724 (nth 8 state)
2725 pos)))
2726 (save-excursion
2727 (goto-char res)
2728 (if (c-beginning-of-macro)
2729 (point)
2730 res))))
2732 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2733 ;; Stuff to do with point-min, and coping with any literal there.
2734 (defvar c-state-point-min 1)
2735 (make-variable-buffer-local 'c-state-point-min)
2736 ;; This is (point-min) when `c-state-cache' was last calculated. A change of
2737 ;; narrowing is likely to affect the parens that are visible before the point.
2739 (defvar c-state-point-min-lit-type nil)
2740 (make-variable-buffer-local 'c-state-point-min-lit-type)
2741 (defvar c-state-point-min-lit-start nil)
2742 (make-variable-buffer-local 'c-state-point-min-lit-start)
2743 ;; These two variables define the literal, if any, containing point-min.
2744 ;; Their values are, respectively, 'string, c, or c++, and the start of the
2745 ;; literal. If there's no literal there, they're both nil.
2747 (defvar c-state-min-scan-pos 1)
2748 (make-variable-buffer-local 'c-state-min-scan-pos)
2749 ;; This is the earliest buffer-pos from which scanning can be done. It is
2750 ;; either the end of the literal containing point-min, or point-min itself.
2751 ;; It becomes nil if the buffer is changed earlier than this point.
2752 (defun c-state-get-min-scan-pos ()
2753 ;; Return the lowest valid scanning pos. This will be the end of the
2754 ;; literal enclosing point-min, or point-min itself.
2755 (or c-state-min-scan-pos
2756 (save-restriction
2757 (save-excursion
2758 (widen)
2759 (goto-char c-state-point-min-lit-start)
2760 (if (eq c-state-point-min-lit-type 'string)
2761 (forward-sexp)
2762 (forward-comment 1))
2763 (setq c-state-min-scan-pos (point))))))
2765 (defun c-state-mark-point-min-literal ()
2766 ;; Determine the properties of any literal containing POINT-MIN, setting the
2767 ;; variables `c-state-point-min-lit-type', `c-state-point-min-lit-start',
2768 ;; and `c-state-min-scan-pos' accordingly. The return value is meaningless.
2769 (let ((p-min (point-min))
2770 lit)
2771 (save-restriction
2772 (widen)
2773 (setq lit (c-state-literal-at p-min))
2774 (if lit
2775 (setq c-state-point-min-lit-type
2776 (save-excursion
2777 (goto-char (car lit))
2778 (cond
2779 ((looking-at c-block-comment-start-regexp) 'c)
2780 ((looking-at c-line-comment-starter) 'c++)
2781 (t 'string)))
2782 c-state-point-min-lit-start (car lit)
2783 c-state-min-scan-pos (cdr lit))
2784 (setq c-state-point-min-lit-type nil
2785 c-state-point-min-lit-start nil
2786 c-state-min-scan-pos p-min)))))
2789 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2790 ;; A variable which signals a brace dessert - helpful for reducing the number
2791 ;; of fruitless backward scans.
2792 (defvar c-state-brace-pair-desert nil)
2793 (make-variable-buffer-local 'c-state-brace-pair-desert)
2794 ;; Used only in `c-append-lower-brace-pair-to-state-cache'. It is set when
2795 ;; that defun has searched backwards for a brace pair and not found one. Its
2796 ;; value is either nil or a cons (PA . FROM), where PA is the position of the
2797 ;; enclosing opening paren/brace/bracket which bounds the backwards search (or
2798 ;; nil when at top level) and FROM is where the backward search started. It
2799 ;; is reset to nil in `c-invalidate-state-cache'.
2802 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2803 ;; Lowish level functions/macros which work directly on `c-state-cache', or a
2804 ;; list of like structure.
2805 (defmacro c-state-cache-top-lparen (&optional cache)
2806 ;; Return the address of the top left brace/bracket/paren recorded in CACHE
2807 ;; (default `c-state-cache') (or nil).
2808 (let ((cash (or cache 'c-state-cache)))
2809 `(if (consp (car ,cash))
2810 (caar ,cash)
2811 (car ,cash))))
2813 (defmacro c-state-cache-top-paren (&optional cache)
2814 ;; Return the address of the latest brace/bracket/paren (whether left or
2815 ;; right) recorded in CACHE (default `c-state-cache') or nil.
2816 (let ((cash (or cache 'c-state-cache)))
2817 `(if (consp (car ,cash))
2818 (cdar ,cash)
2819 (car ,cash))))
2821 (defmacro c-state-cache-after-top-paren (&optional cache)
2822 ;; Return the position just after the latest brace/bracket/paren (whether
2823 ;; left or right) recorded in CACHE (default `c-state-cache') or nil.
2824 (let ((cash (or cache 'c-state-cache)))
2825 `(if (consp (car ,cash))
2826 (cdar ,cash)
2827 (and (car ,cash)
2828 (1+ (car ,cash))))))
2830 (defun c-get-cache-scan-pos (here)
2831 ;; From the state-cache, determine the buffer position from which we might
2832 ;; scan forward to HERE to update this cache. This position will be just
2833 ;; after a paren/brace/bracket recorded in the cache, if possible, otherwise
2834 ;; return the earliest position in the accessible region which isn't within
2835 ;; a literal. If the visible portion of the buffer is entirely within a
2836 ;; literal, return NIL.
2837 (let ((c c-state-cache) elt)
2838 ;(while (>= (or (c-state-cache-top-lparen c) 1) here)
2839 (while (and c
2840 (>= (c-state-cache-top-lparen c) here))
2841 (setq c (cdr c)))
2843 (setq elt (car c))
2844 (cond
2845 ((consp elt)
2846 (if (> (cdr elt) here)
2847 (1+ (car elt))
2848 (cdr elt)))
2849 (elt (1+ elt))
2850 ((<= (c-state-get-min-scan-pos) here)
2851 (c-state-get-min-scan-pos))
2852 (t nil))))
2854 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2855 ;; Variables which keep track of preprocessor constructs.
2856 (defvar c-state-old-cpp-beg-marker nil)
2857 (make-variable-buffer-local 'c-state-old-cpp-beg-marker)
2858 (defvar c-state-old-cpp-beg nil)
2859 (make-variable-buffer-local 'c-state-old-cpp-beg)
2860 (defvar c-state-old-cpp-end-marker nil)
2861 (make-variable-buffer-local 'c-state-old-cpp-end-marker)
2862 (defvar c-state-old-cpp-end nil)
2863 (make-variable-buffer-local 'c-state-old-cpp-end)
2864 ;; These are the limits of the macro containing point at the previous call of
2865 ;; `c-parse-state', or nil.
2867 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2868 ;; Defuns which analyze the buffer, yet don't change `c-state-cache'.
2869 (defun c-get-fallback-scan-pos (here)
2870 ;; Return a start position for building `c-state-cache' from
2871 ;; scratch. This will be at the top level, 2 defuns back.
2872 (save-excursion
2873 ;; Go back 2 bods, but ignore any bogus positions returned by
2874 ;; beginning-of-defun (i.e. open paren in column zero).
2875 (goto-char here)
2876 (let ((cnt 2))
2877 (while (not (or (bobp) (zerop cnt)))
2878 (c-beginning-of-defun-1) ; Pure elisp BOD.
2879 (if (eq (char-after) ?\{)
2880 (setq cnt (1- cnt)))))
2881 (point)))
2883 (defun c-state-balance-parens-backwards (here- here+ top)
2884 ;; Return the position of the opening paren/brace/bracket before HERE- which
2885 ;; matches the outermost close p/b/b between HERE+ and TOP. Except when
2886 ;; there's a macro, HERE- and HERE+ are the same. Like this:
2888 ;; ............................................
2889 ;; | |
2890 ;; ( [ ( .........#macro.. ) ( ) ] )
2891 ;; ^ ^ ^ ^
2892 ;; | | | |
2893 ;; return HERE- HERE+ TOP
2895 ;; If there aren't enough opening paren/brace/brackets, return the position
2896 ;; of the outermost one found, or HERE- if there are none. If there are no
2897 ;; closing p/b/bs between HERE+ and TOP, return HERE-. HERE-/+ and TOP
2898 ;; must not be inside literals. Only the accessible portion of the buffer
2899 ;; will be scanned.
2901 ;; PART 1: scan from `here+' up to `top', accumulating ")"s which enclose
2902 ;; `here'. Go round the next loop each time we pass over such a ")". These
2903 ;; probably match "("s before `here-'.
2904 (let (pos pa ren+1 lonely-rens)
2905 (save-excursion
2906 (save-restriction
2907 (narrow-to-region (point-min) top) ; This can move point, sometimes.
2908 (setq pos here+)
2909 (c-safe
2910 (while
2911 (setq ren+1 (c-sc-scan-lists pos 1 1)) ; might signal
2912 (setq lonely-rens (cons ren+1 lonely-rens)
2913 pos ren+1)))))
2915 ;; PART 2: Scan back before `here-' searching for the "("s
2916 ;; matching/mismatching the ")"s found above. We only need to direct the
2917 ;; caller to scan when we've encountered unmatched right parens.
2918 (setq pos here-)
2919 (when lonely-rens
2920 (c-safe
2921 (while
2922 (and lonely-rens ; actual values aren't used.
2923 (setq pa (c-sc-scan-lists pos -1 1)))
2924 (setq pos pa)
2925 (setq lonely-rens (cdr lonely-rens)))))
2926 pos))
2928 (defun c-parse-state-get-strategy (here good-pos)
2929 ;; Determine the scanning strategy for adjusting `c-parse-state', attempting
2930 ;; to minimize the amount of scanning. HERE is the pertinent position in
2931 ;; the buffer, GOOD-POS is a position where `c-state-cache' (possibly with
2932 ;; its head trimmed) is known to be good, or nil if there is no such
2933 ;; position.
2935 ;; The return value is a list, one of the following:
2937 ;; o - ('forward START-POINT) - scan forward from START-POINT,
2938 ;; which is not less than the highest position in `c-state-cache' below HERE,
2939 ;; which is after GOOD-POS.
2940 ;; o - ('backward nil) - scan backwards (from HERE).
2941 ;; o - ('back-and-forward START-POINT) - like 'forward, but when HERE is earlier
2942 ;; than GOOD-POS.
2943 ;; o - ('BOD START-POINT) - scan forwards from START-POINT, which is at the
2944 ;; top level.
2945 ;; o - ('IN-LIT nil) - point is inside the literal containing point-min.
2946 (let* ((in-macro-start ; start of macro containing HERE or nil.
2947 (save-excursion
2948 (goto-char here)
2949 (and (c-beginning-of-macro)
2950 (point))))
2951 (changed-macro-start
2952 (and in-macro-start
2953 (not (and c-state-old-cpp-beg
2954 (= in-macro-start c-state-old-cpp-beg)))
2955 in-macro-start))
2956 (cache-pos (c-get-cache-scan-pos (if changed-macro-start
2957 (min changed-macro-start here)
2958 here))) ; highest suitable position in cache (or 1)
2959 BOD-pos ; position of 2nd BOD before HERE.
2960 strategy ; 'forward, 'backward, 'BOD, or 'IN-LIT.
2961 start-point
2962 how-far) ; putative scanning distance.
2963 (setq good-pos (or good-pos (c-state-get-min-scan-pos)))
2964 (cond
2965 ((< here (c-state-get-min-scan-pos))
2966 (setq strategy 'IN-LIT
2967 start-point nil
2968 cache-pos nil
2969 how-far 0))
2970 ((<= good-pos here)
2971 (setq strategy 'forward
2972 start-point (if changed-macro-start
2973 cache-pos
2974 (max good-pos cache-pos))
2975 how-far (- here start-point)))
2976 ((< (- good-pos here) (- here cache-pos)) ; FIXME!!! ; apply some sort of weighting.
2977 (setq strategy 'backward
2978 how-far (- good-pos here)))
2980 (setq strategy 'back-and-forward
2981 start-point cache-pos
2982 how-far (- here start-point))))
2984 ;; Might we be better off starting from the top level, two defuns back,
2985 ;; instead? This heuristic no longer works well in C++, where
2986 ;; declarations inside namespace brace blocks are frequently placed at
2987 ;; column zero. (2015-11-10): Remove the condition on C++ Mode.
2988 (when (and (or (not (memq 'col-0-paren c-emacs-features))
2989 open-paren-in-column-0-is-defun-start)
2990 ;; (not (c-major-mode-is 'c++-mode))
2991 (> how-far c-state-cache-too-far))
2992 (setq BOD-pos (c-get-fallback-scan-pos here)) ; somewhat EXPENSIVE!!!
2993 (if (< (- here BOD-pos) how-far)
2994 (setq strategy 'BOD
2995 start-point BOD-pos)))
2997 (list strategy start-point)))
3000 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
3001 ;; Routines which change `c-state-cache' and associated values.
3002 (defun c-renarrow-state-cache ()
3003 ;; The region (more precisely, point-min) has changed since we
3004 ;; calculated `c-state-cache'. Amend `c-state-cache' accordingly.
3005 (if (< (point-min) c-state-point-min)
3006 ;; If point-min has MOVED BACKWARDS then we drop the state completely.
3007 ;; It would be possible to do a better job here and recalculate the top
3008 ;; only.
3009 (progn
3010 (c-state-mark-point-min-literal)
3011 (setq c-state-cache nil
3012 c-state-cache-good-pos c-state-min-scan-pos
3013 c-state-brace-pair-desert nil))
3015 ;; point-min has MOVED FORWARD.
3017 ;; Is the new point-min inside a (different) literal?
3018 (unless (and c-state-point-min-lit-start ; at prev. point-min
3019 (< (point-min) (c-state-get-min-scan-pos)))
3020 (c-state-mark-point-min-literal))
3022 ;; Cut off a bit of the tail from `c-state-cache'.
3023 (let ((ptr (cons nil c-state-cache))
3025 (while (and (setq pa (c-state-cache-top-lparen (cdr ptr)))
3026 (>= pa (point-min)))
3027 (setq ptr (cdr ptr)))
3029 (when (consp ptr)
3030 (if (or (eq (cdr ptr) c-state-cache)
3031 (and (consp (cadr ptr))
3032 (> (cdr (cadr ptr)) (point-min)))) ; Our new point-min is
3033 ; inside a recorded
3034 ; brace pair.
3035 (setq c-state-cache nil
3036 c-state-cache-good-pos c-state-min-scan-pos)
3037 (setcdr ptr nil)
3038 (setq c-state-cache-good-pos (1+ (c-state-cache-top-lparen))))
3041 (setq c-state-point-min (point-min)))
3043 (defun c-append-lower-brace-pair-to-state-cache (from here &optional upper-lim)
3044 ;; If there is a brace pair preceding FROM in the buffer, at the same level
3045 ;; of nesting (not necessarily immediately preceding), push a cons onto
3046 ;; `c-state-cache' to represent it. FROM must not be inside a literal. If
3047 ;; UPPER-LIM is non-nil, we append the highest brace pair whose "}" is below
3048 ;; UPPER-LIM.
3050 ;; Return non-nil when this has been done.
3052 ;; The situation it copes with is this transformation:
3054 ;; OLD: { (.) {...........}
3055 ;; ^ ^
3056 ;; FROM HERE
3058 ;; NEW: { {....} (.) {.........
3059 ;; ^ ^ ^
3060 ;; LOWER BRACE PAIR HERE or HERE
3062 ;; This routine should be fast. Since it can get called a LOT, we maintain
3063 ;; `c-state-brace-pair-desert', a small cache of "failures", such that we
3064 ;; reduce the time wasted in repeated fruitless searches in brace deserts.
3065 (save-excursion
3066 (save-restriction
3067 (let* (new-cons
3068 (cache-pos (c-state-cache-top-lparen)) ; might be nil.
3069 (macro-start-or-from
3070 (progn (goto-char from)
3071 (c-beginning-of-macro)
3072 (point)))
3073 (bra ; Position of "{".
3074 ;; Don't start scanning in the middle of a CPP construct unless
3075 ;; it contains HERE - these constructs, in Emacs, are "commented
3076 ;; out" with category properties.
3077 (if (eq (c-get-char-property macro-start-or-from 'category)
3078 'c-cpp-delimiter)
3079 macro-start-or-from
3080 from))
3081 ce) ; Position of "}"
3082 (or upper-lim (setq upper-lim from))
3084 ;; If we're essentially repeating a fruitless search, just give up.
3085 (unless (and c-state-brace-pair-desert
3086 (eq cache-pos (car c-state-brace-pair-desert))
3087 (or (null (car c-state-brace-pair-desert))
3088 (> from (car c-state-brace-pair-desert)))
3089 (<= from (cdr c-state-brace-pair-desert)))
3090 ;; DESERT-LIM. Avoid repeated searching through the cached desert.
3091 (let ((desert-lim
3092 (and c-state-brace-pair-desert
3093 (eq cache-pos (car c-state-brace-pair-desert))
3094 (>= from (cdr c-state-brace-pair-desert))
3095 (cdr c-state-brace-pair-desert)))
3096 ;; CACHE-LIM. This limit will be necessary when an opening
3097 ;; paren at `cache-pos' has just had its matching close paren
3098 ;; inserted into the buffer. `cache-pos' continues to be a
3099 ;; search bound, even though the algorithm below would skip
3100 ;; over the new paren pair.
3101 (cache-lim (and cache-pos (< cache-pos from) cache-pos)))
3102 (narrow-to-region
3103 (cond
3104 ((and desert-lim cache-lim)
3105 (max desert-lim cache-lim))
3106 (desert-lim)
3107 (cache-lim)
3108 ((point-min)))
3109 ;; The top limit is EOB to ensure that `bra' is inside the
3110 ;; accessible part of the buffer at the next scan operation.
3111 (1+ (buffer-size))))
3113 ;; In the next pair of nested loops, the inner one moves back past a
3114 ;; pair of (mis-)matching parens or brackets; the outer one moves
3115 ;; back over a sequence of unmatched close brace/paren/bracket each
3116 ;; time round.
3117 (while
3118 (progn
3119 (c-safe
3120 (while
3121 (and (setq ce (c-sc-scan-lists bra -1 -1)) ; back past )/]/}; might signal
3122 (setq bra (c-sc-scan-lists ce -1 1)) ; back past (/[/{; might signal
3123 (or (> bra here) ;(> ce here)
3124 (and
3125 (< ce here)
3126 (or (not (eq (char-after bra) ?\{))
3127 (and (goto-char bra)
3128 (c-beginning-of-macro)
3129 (< (point) macro-start-or-from))))))))
3130 (and ce (< ce bra)))
3131 (setq bra ce)) ; If we just backed over an unbalanced closing
3132 ; brace, ignore it.
3134 (if (and ce (< ce here) (< bra ce) (eq (char-after bra) ?\{))
3135 ;; We've found the desired brace-pair.
3136 (progn
3137 (setq new-cons (cons bra (1+ ce)))
3138 (cond
3139 ((consp (car c-state-cache))
3140 (setcar c-state-cache new-cons))
3141 ((and (numberp (car c-state-cache)) ; probably never happens
3142 (< ce (car c-state-cache)))
3143 (setcdr c-state-cache
3144 (cons new-cons (cdr c-state-cache))))
3145 (t (setq c-state-cache (cons new-cons c-state-cache)))))
3147 ;; We haven't found a brace pair. Record this in the cache.
3148 (setq c-state-brace-pair-desert
3149 (cons (if (and ce (< bra ce) (> ce here)) ; {..} straddling HERE?
3151 (point-min))
3152 (min here from)))))))))
3154 (defsubst c-state-push-any-brace-pair (bra+1 macro-start-or-here)
3155 ;; If BRA+1 is nil, do nothing. Otherwise, BRA+1 is the buffer position
3156 ;; following a {, and that brace has a (mis-)matching } (or ]), and we
3157 ;; "push" "a" brace pair onto `c-state-cache'.
3159 ;; Here "push" means overwrite the top element if it's itself a brace-pair,
3160 ;; otherwise push it normally.
3162 ;; The brace pair we push is normally the one surrounding BRA+1, but if the
3163 ;; latter is inside a macro, not being a macro containing
3164 ;; MACRO-START-OR-HERE, we scan backwards through the buffer for a non-macro
3165 ;; base pair. This latter case is assumed to be rare.
3167 ;; Note: POINT is not preserved in this routine.
3168 (if bra+1
3169 (if (or (> bra+1 macro-start-or-here)
3170 (progn (goto-char bra+1)
3171 (not (c-beginning-of-macro))))
3172 (setq c-state-cache
3173 (cons (cons (1- bra+1)
3174 (c-sc-scan-lists bra+1 1 1))
3175 (if (consp (car c-state-cache))
3176 (cdr c-state-cache)
3177 c-state-cache)))
3178 ;; N.B. This defsubst codes one method for the simple, normal case,
3179 ;; and a more sophisticated, slower way for the general case. Don't
3180 ;; eliminate this defsubst - it's a speed optimization.
3181 (c-append-lower-brace-pair-to-state-cache (1- bra+1) (point-max)))))
3183 (defun c-append-to-state-cache (from here)
3184 ;; Scan the buffer from FROM to HERE, adding elements into `c-state-cache'
3185 ;; for braces etc. Return a candidate for `c-state-cache-good-pos'.
3187 ;; FROM must be after the latest brace/paren/bracket in `c-state-cache', if
3188 ;; any. Typically, it is immediately after it. It must not be inside a
3189 ;; literal.
3190 (let ((here-bol (c-point 'bol here))
3191 (macro-start-or-here
3192 (save-excursion (goto-char here)
3193 (if (c-beginning-of-macro)
3194 (point)
3195 here)))
3196 pa+1 ; pos just after an opening PAren (or brace).
3197 (ren+1 from) ; usually a pos just after an closing paREN etc.
3198 ; Is actually the pos. to scan for a (/{/[ from,
3199 ; which sometimes is after a silly )/}/].
3200 paren+1 ; Pos after some opening or closing paren.
3201 paren+1s ; A list of `paren+1's; used to determine a
3202 ; good-pos.
3203 bra+1 ; just after L bra-ce.
3204 bra+1s ; list of OLD values of bra+1.
3205 mstart) ; start of a macro.
3207 (save-excursion
3208 (save-restriction
3209 (narrow-to-region (point-min) here)
3210 ;; Each time round the following loop, we enter a successively deeper
3211 ;; level of brace/paren nesting. (Except sometimes we "continue at
3212 ;; the existing level".) `pa+1' is a pos inside an opening
3213 ;; brace/paren/bracket, usually just after it.
3214 (while
3215 (progn
3216 ;; Each time round the next loop moves forward over an opening then
3217 ;; a closing brace/bracket/paren. This loop is white hot, so it
3218 ;; plays ugly tricks to go fast. DON'T PUT ANYTHING INTO THIS
3219 ;; LOOP WHICH ISN'T ABSOLUTELY NECESSARY!!! It terminates when a
3220 ;; call of `scan-lists' signals an error, which happens when there
3221 ;; are no more b/b/p's to scan.
3222 (c-safe
3223 (while t
3224 (setq pa+1 (c-sc-scan-lists ren+1 1 -1) ; Into (/{/[; might signal
3225 paren+1s (cons pa+1 paren+1s))
3226 (setq ren+1 (c-sc-scan-lists pa+1 1 1)) ; Out of )/}/]; might signal
3227 (if (and (eq (char-before pa+1) ?{)) ; Check for a macro later.
3228 (setq bra+1 pa+1))
3229 (setcar paren+1s ren+1)))
3231 (if (and pa+1 (> pa+1 ren+1))
3232 ;; We've just entered a deeper nesting level.
3233 (progn
3234 ;; Insert the brace pair (if present) and the single open
3235 ;; paren/brace/bracket into `c-state-cache' It cannot be
3236 ;; inside a macro, except one around point, because of what
3237 ;; `c-neutralize-syntax-in-CPP' has done.
3238 (c-state-push-any-brace-pair bra+1 macro-start-or-here)
3239 ;; Insert the opening brace/bracket/paren position.
3240 (setq c-state-cache (cons (1- pa+1) c-state-cache))
3241 ;; Clear admin stuff for the next more nested part of the scan.
3242 (setq ren+1 pa+1 pa+1 nil bra+1 nil bra+1s nil)
3243 t) ; Carry on the loop
3245 ;; All open p/b/b's at this nesting level, if any, have probably
3246 ;; been closed by matching/mismatching ones. We're probably
3247 ;; finished - we just need to check for having found an
3248 ;; unmatched )/}/], which we ignore. Such a )/}/] can't be in a
3249 ;; macro, due the action of `c-neutralize-syntax-in-CPP'.
3250 (c-safe (setq ren+1 (c-sc-scan-lists ren+1 1 1)))))) ; acts as loop control.
3252 ;; Record the final, innermost, brace-pair if there is one.
3253 (c-state-push-any-brace-pair bra+1 macro-start-or-here)
3255 ;; Determine a good pos
3256 (while (and (setq paren+1 (car paren+1s))
3257 (> (if (> paren+1 macro-start-or-here)
3258 paren+1
3259 (goto-char paren+1)
3260 (setq mstart (and (c-beginning-of-macro)
3261 (point)))
3262 (or mstart paren+1))
3263 here-bol))
3264 (setq paren+1s (cdr paren+1s)))
3265 (cond
3266 ((and paren+1 mstart)
3267 (min paren+1 mstart))
3268 (paren+1)
3269 (t from))))))
3271 (defun c-remove-stale-state-cache (start-point here pps-point)
3272 ;; Remove stale entries from the `c-cache-state', i.e. those which will
3273 ;; not be in it when it is amended for position HERE. This may involve
3274 ;; replacing a CONS element for a brace pair containing HERE with its car.
3275 ;; Additionally, the "outermost" open-brace entry before HERE will be
3276 ;; converted to a cons if the matching close-brace is below HERE.
3278 ;; START-POINT is a "maximal" "safe position" - there must be no open
3279 ;; parens/braces/brackets between START-POINT and HERE.
3281 ;; As a second thing, calculate the result of parse-partial-sexp at
3282 ;; PPS-POINT, w.r.t. START-POINT. The motivation here is that
3283 ;; `c-state-cache-good-pos' may become PPS-POINT, but the caller may need to
3284 ;; adjust it to get outside a string/comment. (Sorry about this! The code
3285 ;; needs to be FAST).
3287 ;; Return a list (GOOD-POS SCAN-BACK-POS CONS-SEPARATED PPS-STATE), where
3288 ;; o - GOOD-POS is a position where the new value `c-state-cache' is known
3289 ;; to be good (we aim for this to be as high as possible);
3290 ;; o - SCAN-BACK-POS, if not nil, indicates there may be a brace pair
3291 ;; preceding POS which needs to be recorded in `c-state-cache'. It is a
3292 ;; position to scan backwards from. It is the position of the "{" of the
3293 ;; last element to be removed from `c-state-cache', when that elt is a
3294 ;; cons, otherwise nil.
3295 ;; o - CONS-SEPARATED is t when a cons element in `c-state-cache' has been
3296 ;; replaced by its car because HERE lies inside the brace pair represented
3297 ;; by the cons.
3298 ;; o - PPS-STATE is the parse-partial-sexp state at PPS-POINT.
3299 (save-excursion
3300 (save-restriction
3301 (narrow-to-region 1 (point-max))
3302 (let* ((in-macro-start ; start of macro containing HERE or nil.
3303 (save-excursion
3304 (goto-char here)
3305 (and (c-beginning-of-macro)
3306 (point))))
3307 (start-point-actual-macro-start ; Start of macro containing
3308 ; start-point or nil
3309 (and (< start-point here)
3310 (save-excursion
3311 (goto-char start-point)
3312 (and (c-beginning-of-macro)
3313 (point)))))
3314 (start-point-actual-macro-end ; End of this macro, (maybe
3315 ; HERE), or nil.
3316 (and start-point-actual-macro-start
3317 (save-excursion
3318 (goto-char start-point-actual-macro-start)
3319 (c-end-of-macro)
3320 (point))))
3321 pps-state ; Will be 9 or 10 elements long.
3323 upper-lim ; ,beyond which `c-state-cache' entries are removed
3324 scan-back-pos
3325 cons-separated
3326 pair-beg pps-point-state target-depth)
3328 ;; Remove entries beyond HERE. Also remove any entries inside
3329 ;; a macro, unless HERE is in the same macro.
3330 (setq upper-lim
3331 (if (or (null c-state-old-cpp-beg)
3332 (and (> here c-state-old-cpp-beg)
3333 (< here c-state-old-cpp-end)))
3334 here
3335 (min here c-state-old-cpp-beg)))
3336 (while (and c-state-cache (>= (c-state-cache-top-lparen) upper-lim))
3337 (setq scan-back-pos (car-safe (car c-state-cache)))
3338 (setq c-state-cache (cdr c-state-cache)))
3340 ;; If `upper-lim' is inside the last recorded brace pair, remove its
3341 ;; RBrace and indicate we'll need to search backwards for a previous
3342 ;; brace pair.
3343 (when (and c-state-cache
3344 (consp (car c-state-cache))
3345 (> (cdar c-state-cache) upper-lim))
3346 (setcar c-state-cache (caar c-state-cache))
3347 (setq scan-back-pos (car c-state-cache)
3348 cons-separated t))
3350 ;; The next loop jumps forward out of a nested level of parens each
3351 ;; time round; the corresponding elements in `c-state-cache' are
3352 ;; removed. `pos' is just after the brace-pair or the open paren at
3353 ;; (car c-state-cache). There can be no open parens/braces/brackets
3354 ;; between `start-point'/`start-point-actual-macro-start' and HERE,
3355 ;; due to the interface spec to this function.
3356 (setq pos (if (and start-point-actual-macro-end
3357 (not (eq start-point-actual-macro-start
3358 in-macro-start)))
3359 (1+ start-point-actual-macro-end) ; get outside the macro as
3360 ; marked by a `category' text property.
3361 start-point))
3362 (goto-char pos)
3363 (while (and c-state-cache
3364 (or (numberp (car c-state-cache)) ; Have we a { at all?
3365 (cdr c-state-cache))
3366 (< (point) here))
3367 (cond
3368 ((null pps-state) ; first time through
3369 (setq target-depth -1))
3370 ((eq (car pps-state) target-depth) ; found closing ),},]
3371 (setq target-depth (1- (car pps-state))))
3372 ;; Do nothing when we've merely reached pps-point.
3375 ;; Scan!
3376 (setq pps-state
3377 (c-sc-parse-partial-sexp
3378 (point) (if (< (point) pps-point) pps-point here)
3379 target-depth
3380 nil pps-state))
3382 (if (= (point) pps-point)
3383 (setq pps-point-state pps-state))
3385 (when (eq (car pps-state) target-depth)
3386 (setq pos (point)) ; POS is now just after an R-paren/brace.
3387 (cond
3388 ((and (consp (car c-state-cache))
3389 (eq (point) (cdar c-state-cache)))
3390 ;; We've just moved out of the paren pair containing the brace-pair
3391 ;; at (car c-state-cache). `pair-beg' is where the open paren is,
3392 ;; and is potentially where the open brace of a cons in
3393 ;; c-state-cache will be.
3394 (setq pair-beg (car-safe (cdr c-state-cache))
3395 c-state-cache (cdr-safe (cdr c-state-cache)))) ; remove {}pair + containing Lparen.
3396 ((numberp (car c-state-cache))
3397 (setq pair-beg (car c-state-cache)
3398 c-state-cache (cdr c-state-cache))) ; remove this
3399 ; containing Lparen
3400 ((numberp (cadr c-state-cache))
3401 (setq pair-beg (cadr c-state-cache)
3402 c-state-cache (cddr c-state-cache))) ; Remove a paren pair
3403 ; together with enclosed brace pair.
3404 ;; (t nil) ; Ignore an unmated Rparen.
3407 (if (< (point) pps-point)
3408 (setq pps-state (c-sc-parse-partial-sexp
3409 (point) pps-point
3410 nil nil ; TARGETDEPTH, STOPBEFORE
3411 pps-state)))
3413 ;; If the last paren pair we moved out of was actually a brace pair,
3414 ;; insert it into `c-state-cache'.
3415 (when (and pair-beg (eq (char-after pair-beg) ?{))
3416 (if (consp (car-safe c-state-cache))
3417 (setq c-state-cache (cdr c-state-cache)))
3418 (setq c-state-cache (cons (cons pair-beg pos)
3419 c-state-cache)))
3421 (list pos scan-back-pos cons-separated pps-state)))))
3423 (defun c-remove-stale-state-cache-backwards (here)
3424 ;; Strip stale elements of `c-state-cache' by moving backwards through the
3425 ;; buffer, and inform the caller of the scenario detected.
3427 ;; HERE is the position we're setting `c-state-cache' for.
3428 ;; CACHE-POS (a locally bound variable) is just after the latest recorded
3429 ;; position in `c-state-cache' before HERE, or a position at or near
3430 ;; point-min which isn't in a literal.
3432 ;; This function must only be called only when (> `c-state-cache-good-pos'
3433 ;; HERE). Usually the gap between CACHE-POS and HERE is large. It is thus
3434 ;; optimized to eliminate (or minimize) scanning between these two
3435 ;; positions.
3437 ;; Return a three element list (GOOD-POS SCAN-BACK-POS FWD-FLAG), where:
3438 ;; o - GOOD-POS is a "good position", where `c-state-cache' is valid, or
3439 ;; could become so after missing elements are inserted into
3440 ;; `c-state-cache'. This is JUST AFTER an opening or closing
3441 ;; brace/paren/bracket which is already in `c-state-cache' or just before
3442 ;; one otherwise. exceptionally (when there's no such b/p/b handy) the BOL
3443 ;; before `here''s line, or the start of the literal containing it.
3444 ;; o - SCAN-BACK-POS, if non-nil, indicates there may be a brace pair
3445 ;; preceding POS which isn't recorded in `c-state-cache'. It is a position
3446 ;; to scan backwards from.
3447 ;; o - FWD-FLAG, if non-nil, indicates there may be parens/braces between
3448 ;; POS and HERE which aren't recorded in `c-state-cache'.
3450 ;; The comments in this defun use "paren" to mean parenthesis or square
3451 ;; bracket (as contrasted with a brace), and "(" and ")" likewise.
3453 ;; . {..} (..) (..) ( .. { } ) (...) ( .... . ..)
3454 ;; | | | | | |
3455 ;; CP E here D C good
3456 (let ((cache-pos (c-get-cache-scan-pos here)) ; highest position below HERE in cache (or 1)
3457 (pos c-state-cache-good-pos)
3458 pa ren ; positions of "(" and ")"
3459 dropped-cons ; whether the last element dropped from `c-state-cache'
3460 ; was a cons (representing a brace-pair)
3461 good-pos ; see above.
3462 lit ; (START . END) of a literal containing some point.
3463 here-lit-start here-lit-end ; bounds of literal containing `here'
3464 ; or `here' itself.
3465 here- here+ ; start/end of macro around HERE, or HERE
3466 (here-bol (c-point 'bol here))
3467 (too-far-back (max (- here c-state-cache-too-far) (point-min))))
3469 ;; Remove completely irrelevant entries from `c-state-cache'.
3470 (while (and c-state-cache
3471 (>= (setq pa (c-state-cache-top-lparen)) here))
3472 (setq dropped-cons (consp (car c-state-cache)))
3473 (setq c-state-cache (cdr c-state-cache))
3474 (setq pos pa))
3475 ;; At this stage, (>= pos here);
3476 ;; (< (c-state-cache-top-lparen) here) (or is nil).
3478 (cond
3479 ((and (consp (car c-state-cache))
3480 (> (cdar c-state-cache) here))
3481 ;; CASE 1: The top of the cache is a brace pair which now encloses
3482 ;; `here'. As good-pos, return the address of the "{". Since we've no
3483 ;; knowledge of what's inside these braces, we have no alternative but
3484 ;; to direct the caller to scan the buffer from the opening brace.
3485 (setq pos (caar c-state-cache))
3486 (setcar c-state-cache pos)
3487 (list (1+ pos) pos t)) ; return value. We've just converted a brace pair
3488 ; entry into a { entry, so the caller needs to
3489 ; search for a brace pair before the {.
3491 ;; `here' might be inside a literal. Check for this.
3492 ((progn
3493 (setq lit (c-state-literal-at here)
3494 here-lit-start (or (car lit) here)
3495 here-lit-end (or (cdr lit) here))
3496 ;; Has `here' just "newly entered" a macro?
3497 (save-excursion
3498 (goto-char here-lit-start)
3499 (if (and (c-beginning-of-macro)
3500 (or (null c-state-old-cpp-beg)
3501 (not (= (point) c-state-old-cpp-beg))))
3502 (progn
3503 (setq here- (point))
3504 (c-end-of-macro)
3505 (setq here+ (point)))
3506 (setq here- here-lit-start
3507 here+ here-lit-end)))
3509 ;; `here' might be nested inside any depth of parens (or brackets but
3510 ;; not braces). Scan backwards to find the outermost such opening
3511 ;; paren, if there is one. This will be the scan position to return.
3512 (save-restriction
3513 (narrow-to-region cache-pos (point-max))
3514 (setq pos (c-state-balance-parens-backwards here- here+ pos)))
3515 nil)) ; for the cond
3517 ((< pos here-lit-start)
3518 ;; CASE 2: Address of outermost ( or [ which now encloses `here', but
3519 ;; didn't enclose the (previous) `c-state-cache-good-pos'. If there is
3520 ;; a brace pair preceding this, it will already be in `c-state-cache',
3521 ;; unless there was a brace pair after it, i.e. there'll only be one to
3522 ;; scan for if we've just deleted one.
3523 (list pos (and dropped-cons pos) t)) ; Return value.
3525 ;; `here' isn't enclosed in a (previously unrecorded) bracket/paren.
3526 ;; Further forward scanning isn't needed, but we still need to find a
3527 ;; GOOD-POS. Step out of all enclosing "("s on HERE's line.
3528 ((progn
3529 (save-restriction
3530 (narrow-to-region here-bol (point-max))
3531 (setq pos here-lit-start)
3532 (c-safe (while (setq pa (c-sc-scan-lists pos -1 1))
3533 (setq pos pa)))) ; might signal
3534 nil)) ; for the cond
3536 ((save-restriction
3537 (narrow-to-region too-far-back (point-max))
3538 (setq ren (c-safe (c-sc-scan-lists pos -1 -1))))
3539 ;; CASE 3: After a }/)/] before `here''s BOL.
3540 (list (1+ ren) (and dropped-cons pos) nil)) ; Return value
3542 ((progn (setq good-pos (c-state-lit-beg (c-point 'bopl here-bol)))
3543 (>= cache-pos good-pos))
3544 ;; CASE 3.5: Just after an existing entry in `c-state-cache' on `here''s
3545 ;; line or the previous line.
3546 (list cache-pos nil nil))
3549 ;; CASE 4; Best of a bad job: BOL before `here-bol', or beginning of
3550 ;; literal containing it.
3551 (list good-pos (and dropped-cons good-pos) nil)))))
3554 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
3555 ;; Externally visible routines.
3557 (defun c-state-cache-init ()
3558 (setq c-state-cache nil
3559 c-state-cache-good-pos 1
3560 c-state-nonlit-pos-cache nil
3561 c-state-nonlit-pos-cache-limit 1
3562 c-state-semi-nonlit-pos-cache nil
3563 c-state-semi-nonlit-pos-cache-limit 1
3564 c-state-brace-pair-desert nil
3565 c-state-point-min 1
3566 c-state-point-min-lit-type nil
3567 c-state-point-min-lit-start nil
3568 c-state-min-scan-pos 1
3569 c-state-old-cpp-beg nil
3570 c-state-old-cpp-end nil)
3571 (c-state-mark-point-min-literal))
3573 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
3574 ;; Debugging routines to dump `c-state-cache' in a "replayable" form.
3575 ;; (defmacro c-sc-de (elt) ; "c-state-cache-dump-element"
3576 ;; `(format ,(concat "(setq " (symbol-name elt) " %s) ") ,elt))
3577 ;; (defmacro c-sc-qde (elt) ; "c-state-cache-quote-dump-element"
3578 ;; `(format ,(concat "(setq " (symbol-name elt) " '%s) ") ,elt))
3579 ;; (defun c-state-dump ()
3580 ;; ;; For debugging.
3581 ;; ;(message
3582 ;; (concat
3583 ;; (c-sc-qde c-state-cache)
3584 ;; (c-sc-de c-state-cache-good-pos)
3585 ;; (c-sc-qde c-state-nonlit-pos-cache)
3586 ;; (c-sc-de c-state-nonlit-pos-cache-limit)
3587 ;; (c-sc-qde c-state-brace-pair-desert)
3588 ;; (c-sc-de c-state-point-min)
3589 ;; (c-sc-de c-state-point-min-lit-type)
3590 ;; (c-sc-de c-state-point-min-lit-start)
3591 ;; (c-sc-de c-state-min-scan-pos)
3592 ;; (c-sc-de c-state-old-cpp-beg)
3593 ;; (c-sc-de c-state-old-cpp-end)))
3594 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
3596 (defun c-invalidate-state-cache-1 (here)
3597 ;; Invalidate all info on `c-state-cache' that applies to the buffer at HERE
3598 ;; or higher and set `c-state-cache-good-pos' accordingly. The cache is
3599 ;; left in a consistent state.
3601 ;; This is much like `c-whack-state-after', but it never changes a paren
3602 ;; pair element into an open paren element. Doing that would mean that the
3603 ;; new open paren wouldn't have the required preceding paren pair element.
3605 ;; This function is called from c-before-change.
3607 ;; The caches of non-literals:
3608 ;; Note that we use "<=" for the possibility of the second char of a two-char
3609 ;; comment opener being typed; this would invalidate any cache position at
3610 ;; HERE.
3611 (if (<= here c-state-nonlit-pos-cache-limit)
3612 (setq c-state-nonlit-pos-cache-limit (1- here)))
3613 (c-truncate-semi-nonlit-pos-cache here)
3615 ;; `c-state-cache':
3616 ;; Case 1: if `here' is in a literal containing point-min, everything
3617 ;; becomes (or is already) nil.
3618 (if (or (null c-state-cache-good-pos)
3619 (< here (c-state-get-min-scan-pos)))
3620 (setq c-state-cache nil
3621 c-state-cache-good-pos nil
3622 c-state-min-scan-pos nil)
3624 ;; Truncate `c-state-cache' and set `c-state-cache-good-pos' to a value
3625 ;; below `here'. To maintain its consistency, we may need to insert a new
3626 ;; brace pair.
3627 (let ((here-bol (c-point 'bol here))
3628 too-high-pa ; recorded {/(/[ next above or just below here, or nil.
3629 dropped-cons ; was the last removed element a brace pair?
3631 ;; The easy bit - knock over-the-top bits off `c-state-cache'.
3632 (while (and c-state-cache
3633 (>= (setq pa (c-state-cache-top-paren)) here))
3634 (setq dropped-cons (consp (car c-state-cache))
3635 too-high-pa (c-state-cache-top-lparen)
3636 c-state-cache (cdr c-state-cache)))
3638 ;; Do we need to add in an earlier brace pair, having lopped one off?
3639 (if (and dropped-cons
3640 (<= too-high-pa here))
3641 (c-append-lower-brace-pair-to-state-cache too-high-pa here here-bol))
3642 (setq c-state-cache-good-pos (or (c-state-cache-after-top-paren)
3643 (c-state-get-min-scan-pos)))))
3645 ;; The brace-pair desert marker:
3646 (when (car c-state-brace-pair-desert)
3647 (if (< here (car c-state-brace-pair-desert))
3648 (setq c-state-brace-pair-desert nil)
3649 (if (< here (cdr c-state-brace-pair-desert))
3650 (setcdr c-state-brace-pair-desert here)))))
3652 (defun c-parse-state-1 ()
3653 ;; Find and record all noteworthy parens between some good point earlier in
3654 ;; the file and point. That good point is at least the beginning of the
3655 ;; top-level construct we are in, or the beginning of the preceding
3656 ;; top-level construct if we aren't in one.
3658 ;; The returned value is a list of the noteworthy parens with the last one
3659 ;; first. If an element in the list is an integer, it's the position of an
3660 ;; open paren (of any type) which has not been closed before the point. If
3661 ;; an element is a cons, it gives the position of a closed BRACE paren
3662 ;; pair[*]; the car is the start brace position and the cdr is the position
3663 ;; following the closing brace. Only the last closed brace paren pair
3664 ;; before each open paren and before the point is recorded, and thus the
3665 ;; state never contains two cons elements in succession. When a close brace
3666 ;; has no matching open brace (e.g., the matching brace is outside the
3667 ;; visible region), it is not represented in the returned value.
3669 ;; [*] N.B. The close "brace" might be a mismatching close bracket or paren.
3670 ;; This defun explicitly treats mismatching parens/braces/brackets as
3671 ;; matching. It is the open brace which makes it a "brace" pair.
3673 ;; If POINT is within a macro, open parens and brace pairs within
3674 ;; THIS macro MIGHT be recorded. This depends on whether their
3675 ;; syntactic properties have been suppressed by
3676 ;; `c-neutralize-syntax-in-CPP'. This might need fixing (2008-12-11).
3678 ;; Currently no characters which are given paren syntax with the
3679 ;; syntax-table property are recorded, i.e. angle bracket arglist
3680 ;; parens are never present here. Note that this might change.
3682 ;; BUG: This function doesn't cope entirely well with unbalanced
3683 ;; parens in macros. (2008-12-11: this has probably been resolved
3684 ;; by the function `c-neutralize-syntax-in-CPP'.) E.g. in the
3685 ;; following case the brace before the macro isn't balanced with the
3686 ;; one after it:
3688 ;; {
3689 ;; #define X {
3690 ;; }
3692 ;; Note to maintainers: this function DOES get called with point
3693 ;; within comments and strings, so don't assume it doesn't!
3695 ;; This function might do hidden buffer changes.
3696 (let* ((here (point))
3697 (here-bopl (c-point 'bopl))
3698 strategy ; 'forward, 'backward etc..
3699 ;; Candidate positions to start scanning from:
3700 cache-pos ; highest position below HERE already existing in
3701 ; cache (or 1).
3702 good-pos
3703 start-point ; (when scanning forward) a place below HERE where there
3704 ; are no open parens/braces between it and HERE.
3705 bopl-state
3707 cons-separated
3708 scan-backward-pos scan-forward-p) ; used for 'backward.
3709 ;; If POINT-MIN has changed, adjust the cache
3710 (unless (= (point-min) c-state-point-min)
3711 (c-renarrow-state-cache))
3713 ;; Strategy?
3714 (setq res (c-parse-state-get-strategy here c-state-cache-good-pos)
3715 strategy (car res)
3716 start-point (cadr res))
3718 (when (eq strategy 'BOD)
3719 (setq c-state-cache nil
3720 c-state-cache-good-pos start-point))
3722 ;; SCAN!
3723 (cond
3724 ((memq strategy '(forward back-and-forward BOD))
3725 (setq res (c-remove-stale-state-cache start-point here here-bopl))
3726 (setq cache-pos (car res)
3727 scan-backward-pos (cadr res)
3728 cons-separated (car (cddr res))
3729 bopl-state (cadr (cddr res))) ; will be nil if (< here-bopl
3730 ; start-point)
3731 (if (and scan-backward-pos
3732 (or cons-separated (eq strategy 'forward))) ;scan-backward-pos
3733 (c-append-lower-brace-pair-to-state-cache scan-backward-pos here))
3734 (setq good-pos
3735 (c-append-to-state-cache cache-pos here))
3736 (setq c-state-cache-good-pos
3737 (if (and bopl-state
3738 (< good-pos (- here c-state-cache-too-far)))
3739 (c-state-cache-non-literal-place here-bopl bopl-state)
3740 good-pos)))
3742 ((eq strategy 'backward)
3743 (setq res (c-remove-stale-state-cache-backwards here)
3744 good-pos (car res)
3745 scan-backward-pos (cadr res)
3746 scan-forward-p (car (cddr res)))
3747 (if scan-backward-pos
3748 (c-append-lower-brace-pair-to-state-cache scan-backward-pos here))
3749 (setq c-state-cache-good-pos
3750 (if scan-forward-p
3751 (c-append-to-state-cache good-pos here)
3752 good-pos)))
3754 (t ; (eq strategy 'IN-LIT)
3755 (setq c-state-cache nil
3756 c-state-cache-good-pos nil))))
3758 c-state-cache)
3760 (defun c-invalidate-state-cache (here)
3761 ;; This is a wrapper over `c-invalidate-state-cache-1'.
3763 ;; It suppresses the syntactic effect of the < and > (template) brackets and
3764 ;; of all parens in preprocessor constructs, except for any such construct
3765 ;; containing point. We can then call `c-invalidate-state-cache-1' without
3766 ;; worrying further about macros and template delimiters.
3767 (if (eval-when-compile (memq 'category-properties c-emacs-features))
3768 ;; Emacs
3769 (c-with-<->-as-parens-suppressed
3770 (if (and c-state-old-cpp-beg
3771 (< c-state-old-cpp-beg here))
3772 (c-with-all-but-one-cpps-commented-out
3773 c-state-old-cpp-beg
3774 c-state-old-cpp-end
3775 (c-invalidate-state-cache-1 here))
3776 (c-with-cpps-commented-out
3777 (c-invalidate-state-cache-1 here))))
3778 ;; XEmacs
3779 (c-invalidate-state-cache-1 here)))
3781 (defmacro c-state-maybe-marker (place marker)
3782 ;; If PLACE is non-nil, return a marker marking it, otherwise nil.
3783 ;; We (re)use MARKER.
3784 `(and ,place
3785 (or ,marker (setq ,marker (make-marker)))
3786 (set-marker ,marker ,place)))
3788 (defun c-parse-state ()
3789 ;; This is a wrapper over `c-parse-state-1'. See that function for a
3790 ;; description of the functionality and return value.
3792 ;; It suppresses the syntactic effect of the < and > (template) brackets and
3793 ;; of all parens in preprocessor constructs, except for any such construct
3794 ;; containing point. We can then call `c-parse-state-1' without worrying
3795 ;; further about macros and template delimiters.
3796 (let (here-cpp-beg here-cpp-end)
3797 (save-excursion
3798 (when (c-beginning-of-macro)
3799 (setq here-cpp-beg (point))
3800 (unless
3801 (> (setq here-cpp-end (c-syntactic-end-of-macro))
3802 here-cpp-beg)
3803 (setq here-cpp-beg nil here-cpp-end nil))))
3804 ;; FIXME!!! Put in a `condition-case' here to protect the integrity of the
3805 ;; subsystem.
3806 (prog1
3807 (if (eval-when-compile (memq 'category-properties c-emacs-features))
3808 ;; Emacs
3809 (c-with-<->-as-parens-suppressed
3810 (if (and here-cpp-beg (> here-cpp-end here-cpp-beg))
3811 (c-with-all-but-one-cpps-commented-out
3812 here-cpp-beg here-cpp-end
3813 (c-parse-state-1))
3814 (c-with-cpps-commented-out
3815 (c-parse-state-1))))
3816 ;; XEmacs
3817 (c-parse-state-1))
3818 (setq c-state-old-cpp-beg
3819 (c-state-maybe-marker here-cpp-beg c-state-old-cpp-beg-marker)
3820 c-state-old-cpp-end
3821 (c-state-maybe-marker here-cpp-end c-state-old-cpp-end-marker)))))
3823 ;; Debug tool to catch cache inconsistencies. This is called from
3824 ;; 000tests.el.
3825 (defvar c-debug-parse-state nil)
3826 (unless (fboundp 'c-real-parse-state)
3827 (fset 'c-real-parse-state (symbol-function 'c-parse-state)))
3828 (cc-bytecomp-defun c-real-parse-state)
3830 (defvar c-parse-state-point nil)
3831 (defvar c-parse-state-state nil)
3832 (make-variable-buffer-local 'c-parse-state-state)
3833 (defun c-record-parse-state-state ()
3834 (setq c-parse-state-point (point))
3835 (when (markerp (cdr (assq 'c-state-old-cpp-beg c-parse-state-state)))
3836 (move-marker (cdr (assq 'c-state-old-cpp-beg c-parse-state-state)) nil)
3837 (move-marker (cdr (assq 'c-state-old-cpp-end c-parse-state-state)) nil))
3838 (setq c-parse-state-state
3839 (mapcar
3840 (lambda (arg)
3841 (let ((val (symbol-value arg)))
3842 (cons arg
3843 (cond ((consp val) (copy-tree val))
3844 ((markerp val) (copy-marker val))
3845 (t val)))))
3846 '(c-state-cache
3847 c-state-cache-good-pos
3848 c-state-nonlit-pos-cache
3849 c-state-nonlit-pos-cache-limit
3850 c-state-semi-nonlit-pos-cache
3851 c-state-semi-nonlit-pos-cache-limit
3852 c-state-brace-pair-desert
3853 c-state-point-min
3854 c-state-point-min-lit-type
3855 c-state-point-min-lit-start
3856 c-state-min-scan-pos
3857 c-state-old-cpp-beg
3858 c-state-old-cpp-end
3859 c-parse-state-point))))
3860 (defun c-replay-parse-state-state ()
3861 (message "%s"
3862 (concat "(setq "
3863 (mapconcat
3864 (lambda (arg)
3865 (format "%s %s%s" (car arg)
3866 (if (atom (cdr arg)) "" "'")
3867 (if (markerp (cdr arg))
3868 (format "(copy-marker %s)" (marker-position (cdr arg)))
3869 (cdr arg))))
3870 c-parse-state-state " ")
3871 ")")))
3873 (defun c-debug-parse-state-double-cons (state)
3874 (let (state-car conses-not-ok)
3875 (while state
3876 (setq state-car (car state)
3877 state (cdr state))
3878 (if (and (consp state-car)
3879 (consp (car state)))
3880 (setq conses-not-ok t)))
3881 conses-not-ok))
3883 (defun c-debug-parse-state ()
3884 (let ((here (point)) (min-point (point-min)) (res1 (c-real-parse-state)) res2)
3885 (let ((c-state-cache nil)
3886 (c-state-cache-good-pos 1)
3887 (c-state-nonlit-pos-cache nil)
3888 (c-state-nonlit-pos-cache-limit 1)
3889 (c-state-brace-pair-desert nil)
3890 (c-state-point-min 1)
3891 (c-state-point-min-lit-type nil)
3892 (c-state-point-min-lit-start nil)
3893 (c-state-min-scan-pos 1)
3894 (c-state-old-cpp-beg nil)
3895 (c-state-old-cpp-end nil))
3896 (setq res2 (c-real-parse-state)))
3897 (unless (equal res1 res2)
3898 ;; The cache can actually go further back due to the ad-hoc way
3899 ;; the first paren is found, so try to whack off a bit of its
3900 ;; start before complaining.
3901 ;; (save-excursion
3902 ;; (goto-char (or (c-least-enclosing-brace res2) (point)))
3903 ;; (c-beginning-of-defun-1)
3904 ;; (while (not (or (bobp) (eq (char-after) ?{)))
3905 ;; (c-beginning-of-defun-1))
3906 ;; (unless (equal (c-whack-state-before (point) res1) res2)
3907 ;; (message (concat "c-parse-state inconsistency at %s: "
3908 ;; "using cache: %s, from scratch: %s")
3909 ;; here res1 res2)))
3910 (message (concat "c-parse-state inconsistency at %s: "
3911 "using cache: %s, from scratch: %s. POINT-MIN: %s")
3912 here res1 res2 min-point)
3913 (message "Old state:")
3914 (c-replay-parse-state-state))
3916 (when (c-debug-parse-state-double-cons res1)
3917 (message "c-parse-state INVALIDITY at %s: %s"
3918 here res1)
3919 (message "Old state:")
3920 (c-replay-parse-state-state))
3922 (c-record-parse-state-state)
3923 res2 ; res1 correct a cascading series of errors ASAP
3926 (defun c-toggle-parse-state-debug (&optional arg)
3927 (interactive "P")
3928 (setq c-debug-parse-state (c-calculate-state arg c-debug-parse-state))
3929 (fset 'c-parse-state (symbol-function (if c-debug-parse-state
3930 'c-debug-parse-state
3931 'c-real-parse-state)))
3932 (c-keep-region-active)
3933 (message "c-debug-parse-state %sabled"
3934 (if c-debug-parse-state "en" "dis")))
3935 (when c-debug-parse-state
3936 (c-toggle-parse-state-debug 1))
3939 (defun c-whack-state-before (bufpos paren-state)
3940 ;; Whack off any state information from PAREN-STATE which lies
3941 ;; before BUFPOS. Not destructive on PAREN-STATE.
3942 (let* ((newstate (list nil))
3943 (ptr newstate)
3944 car)
3945 (while paren-state
3946 (setq car (car paren-state)
3947 paren-state (cdr paren-state))
3948 (if (< (if (consp car) (car car) car) bufpos)
3949 (setq paren-state nil)
3950 (setcdr ptr (list car))
3951 (setq ptr (cdr ptr))))
3952 (cdr newstate)))
3954 (defun c-whack-state-after (bufpos paren-state)
3955 ;; Whack off any state information from PAREN-STATE which lies at or
3956 ;; after BUFPOS. Not destructive on PAREN-STATE.
3957 (catch 'done
3958 (while paren-state
3959 (let ((car (car paren-state)))
3960 (if (consp car)
3961 ;; just check the car, because in a balanced brace
3962 ;; expression, it must be impossible for the corresponding
3963 ;; close brace to be before point, but the open brace to
3964 ;; be after.
3965 (if (<= bufpos (car car))
3966 nil ; whack it off
3967 (if (< bufpos (cdr car))
3968 ;; its possible that the open brace is before
3969 ;; bufpos, but the close brace is after. In that
3970 ;; case, convert this to a non-cons element. The
3971 ;; rest of the state is before bufpos, so we're
3972 ;; done.
3973 (throw 'done (cons (car car) (cdr paren-state)))
3974 ;; we know that both the open and close braces are
3975 ;; before bufpos, so we also know that everything else
3976 ;; on state is before bufpos.
3977 (throw 'done paren-state)))
3978 (if (<= bufpos car)
3979 nil ; whack it off
3980 ;; it's before bufpos, so everything else should too.
3981 (throw 'done paren-state)))
3982 (setq paren-state (cdr paren-state)))
3983 nil)))
3985 (defun c-most-enclosing-brace (paren-state &optional bufpos)
3986 ;; Return the bufpos of the innermost enclosing open paren before
3987 ;; bufpos, or nil if none was found.
3988 (let (enclosingp)
3989 (or bufpos (setq bufpos 134217727))
3990 (while paren-state
3991 (setq enclosingp (car paren-state)
3992 paren-state (cdr paren-state))
3993 (if (or (consp enclosingp)
3994 (>= enclosingp bufpos))
3995 (setq enclosingp nil)
3996 (setq paren-state nil)))
3997 enclosingp))
3999 (defun c-least-enclosing-brace (paren-state)
4000 ;; Return the bufpos of the outermost enclosing open paren, or nil
4001 ;; if none was found.
4002 (let (pos elem)
4003 (while paren-state
4004 (setq elem (car paren-state)
4005 paren-state (cdr paren-state))
4006 (if (integerp elem)
4007 (setq pos elem)))
4008 pos))
4010 (defun c-safe-position (bufpos paren-state)
4011 ;; Return the closest "safe" position recorded on PAREN-STATE that
4012 ;; is higher up than BUFPOS. Return nil if PAREN-STATE doesn't
4013 ;; contain any. Return nil if BUFPOS is nil, which is useful to
4014 ;; find the closest limit before a given limit that might be nil.
4016 ;; A "safe" position is a position at or after a recorded open
4017 ;; paren, or after a recorded close paren. The returned position is
4018 ;; thus either the first position after a close brace, or the first
4019 ;; position after an enclosing paren, or at the enclosing paren in
4020 ;; case BUFPOS is immediately after it.
4021 (when bufpos
4022 (let (elem)
4023 (catch 'done
4024 (while paren-state
4025 (setq elem (car paren-state))
4026 (if (consp elem)
4027 (cond ((< (cdr elem) bufpos)
4028 (throw 'done (cdr elem)))
4029 ((< (car elem) bufpos)
4030 ;; See below.
4031 (throw 'done (min (1+ (car elem)) bufpos))))
4032 (if (< elem bufpos)
4033 ;; elem is the position at and not after the opening paren, so
4034 ;; we can go forward one more step unless it's equal to
4035 ;; bufpos. This is useful in some cases avoid an extra paren
4036 ;; level between the safe position and bufpos.
4037 (throw 'done (min (1+ elem) bufpos))))
4038 (setq paren-state (cdr paren-state)))))))
4040 (defun c-beginning-of-syntax ()
4041 ;; This is used for `font-lock-beginning-of-syntax-function'. It
4042 ;; goes to the closest previous point that is known to be outside
4043 ;; any string literal or comment. `c-state-cache' is used if it has
4044 ;; a position in the vicinity.
4045 (let* ((paren-state c-state-cache)
4046 elem
4048 (pos (catch 'done
4049 ;; Note: Similar code in `c-safe-position'. The
4050 ;; difference is that we accept a safe position at
4051 ;; the point and don't bother to go forward past open
4052 ;; parens.
4053 (while paren-state
4054 (setq elem (car paren-state))
4055 (if (consp elem)
4056 (cond ((<= (cdr elem) (point))
4057 (throw 'done (cdr elem)))
4058 ((<= (car elem) (point))
4059 (throw 'done (car elem))))
4060 (if (<= elem (point))
4061 (throw 'done elem)))
4062 (setq paren-state (cdr paren-state)))
4063 (point-min))))
4065 (if (> pos (- (point) 4000))
4066 (goto-char pos)
4067 ;; The position is far back. Try `c-beginning-of-defun-1'
4068 ;; (although we can't be entirely sure it will go to a position
4069 ;; outside a comment or string in current emacsen). FIXME:
4070 ;; Consult `syntax-ppss' here.
4071 (c-beginning-of-defun-1)
4072 (if (< (point) pos)
4073 (goto-char pos)))))
4076 ;; Tools for scanning identifiers and other tokens.
4078 (defun c-on-identifier ()
4079 "Return non-nil if the point is on or directly after an identifier.
4080 Keywords are recognized and not considered identifiers. If an
4081 identifier is detected, the returned value is its starting position.
4082 If an identifier ends at the point and another begins at it \(can only
4083 happen in Pike) then the point for the preceding one is returned.
4085 Note that this function might do hidden buffer changes. See the
4086 comment at the start of cc-engine.el for more info."
4088 ;; FIXME: Shouldn't this function handle "operator" in C++?
4090 (save-excursion
4091 (skip-syntax-backward "w_")
4095 ;; Check for a normal (non-keyword) identifier.
4096 (and (looking-at c-symbol-start)
4097 (not (looking-at c-keywords-regexp))
4098 (point))
4100 (when (c-major-mode-is 'pike-mode)
4101 ;; Handle the `<operator> syntax in Pike.
4102 (let ((pos (point)))
4103 (skip-chars-backward "-!%&*+/<=>^|~[]()")
4104 (and (if (< (skip-chars-backward "`") 0)
4106 (goto-char pos)
4107 (eq (char-after) ?\`))
4108 (looking-at c-symbol-key)
4109 (>= (match-end 0) pos)
4110 (point))))
4112 ;; Handle the "operator +" syntax in C++.
4113 (when (and c-overloadable-operators-regexp
4114 (= (c-backward-token-2 0) 0))
4116 (cond ((and (looking-at c-overloadable-operators-regexp)
4117 (or (not c-opt-op-identifier-prefix)
4118 (and (= (c-backward-token-2 1) 0)
4119 (looking-at c-opt-op-identifier-prefix))))
4120 (point))
4122 ((save-excursion
4123 (and c-opt-op-identifier-prefix
4124 (looking-at c-opt-op-identifier-prefix)
4125 (= (c-forward-token-2 1) 0)
4126 (looking-at c-overloadable-operators-regexp)))
4127 (point))))
4131 (defsubst c-simple-skip-symbol-backward ()
4132 ;; If the point is at the end of a symbol then skip backward to the
4133 ;; beginning of it. Don't move otherwise. Return non-nil if point
4134 ;; moved.
4136 ;; This function might do hidden buffer changes.
4137 (or (< (skip-syntax-backward "w_") 0)
4138 (and (c-major-mode-is 'pike-mode)
4139 ;; Handle the `<operator> syntax in Pike.
4140 (let ((pos (point)))
4141 (if (and (< (skip-chars-backward "-!%&*+/<=>^|~[]()") 0)
4142 (< (skip-chars-backward "`") 0)
4143 (looking-at c-symbol-key)
4144 (>= (match-end 0) pos))
4146 (goto-char pos)
4147 nil)))))
4149 (defun c-beginning-of-current-token (&optional back-limit)
4150 ;; Move to the beginning of the current token. Do not move if not
4151 ;; in the middle of one. BACK-LIMIT may be used to bound the
4152 ;; backward search; if given it's assumed to be at the boundary
4153 ;; between two tokens. Return non-nil if the point is moved, nil
4154 ;; otherwise.
4156 ;; This function might do hidden buffer changes.
4157 (let ((start (point)))
4158 (if (looking-at "\\w\\|\\s_")
4159 (skip-syntax-backward "w_" back-limit)
4160 (when (< (skip-syntax-backward ".()" back-limit) 0)
4161 (while (let ((pos (or (and (looking-at c-nonsymbol-token-regexp)
4162 (match-end 0))
4163 ;; `c-nonsymbol-token-regexp' should always match
4164 ;; since we've skipped backward over punctuation
4165 ;; or paren syntax, but consume one char in case
4166 ;; it doesn't so that we don't leave point before
4167 ;; some earlier incorrect token.
4168 (1+ (point)))))
4169 (if (<= pos start)
4170 (goto-char pos))))))
4171 (< (point) start)))
4173 (defun c-end-of-current-token (&optional back-limit)
4174 ;; Move to the end of the current token. Do not move if not in the
4175 ;; middle of one. BACK-LIMIT may be used to bound the backward
4176 ;; search; if given it's assumed to be at the boundary between two
4177 ;; tokens. Return non-nil if the point is moved, nil otherwise.
4179 ;; This function might do hidden buffer changes.
4180 (let ((start (point)))
4181 (cond ((< (skip-syntax-backward "w_" (1- start)) 0)
4182 (skip-syntax-forward "w_"))
4183 ((< (skip-syntax-backward ".()" back-limit) 0)
4184 (while (progn
4185 (if (looking-at c-nonsymbol-token-regexp)
4186 (goto-char (match-end 0))
4187 ;; `c-nonsymbol-token-regexp' should always match since
4188 ;; we've skipped backward over punctuation or paren
4189 ;; syntax, but move forward in case it doesn't so that
4190 ;; we don't leave point earlier than we started with.
4191 (forward-char))
4192 (< (point) start)))))
4193 (> (point) start)))
4195 (defconst c-jump-syntax-balanced
4196 (if (memq 'gen-string-delim c-emacs-features)
4197 "\\w\\|\\s_\\|\\s(\\|\\s)\\|\\s\"\\|\\s|"
4198 "\\w\\|\\s_\\|\\s(\\|\\s)\\|\\s\""))
4200 (defconst c-jump-syntax-unbalanced
4201 (if (memq 'gen-string-delim c-emacs-features)
4202 "\\w\\|\\s_\\|\\s\"\\|\\s|"
4203 "\\w\\|\\s_\\|\\s\""))
4205 (defun c-forward-token-2 (&optional count balanced limit)
4206 "Move forward by tokens.
4207 A token is defined as all symbols and identifiers which aren't
4208 syntactic whitespace \(note that multicharacter tokens like \"==\" are
4209 treated properly). Point is always either left at the beginning of a
4210 token or not moved at all. COUNT specifies the number of tokens to
4211 move; a negative COUNT moves in the opposite direction. A COUNT of 0
4212 moves to the next token beginning only if not already at one. If
4213 BALANCED is true, move over balanced parens, otherwise move into them.
4214 Also, if BALANCED is true, never move out of an enclosing paren.
4216 LIMIT sets the limit for the movement and defaults to the point limit.
4217 The case when LIMIT is set in the middle of a token, comment or macro
4218 is handled correctly, i.e. the point won't be left there.
4220 Return the number of tokens left to move \(positive or negative). If
4221 BALANCED is true, a move over a balanced paren counts as one. Note
4222 that if COUNT is 0 and no appropriate token beginning is found, 1 will
4223 be returned. Thus, a return value of 0 guarantees that point is at
4224 the requested position and a return value less \(without signs) than
4225 COUNT guarantees that point is at the beginning of some token.
4227 Note that this function might do hidden buffer changes. See the
4228 comment at the start of cc-engine.el for more info."
4230 (or count (setq count 1))
4231 (if (< count 0)
4232 (- (c-backward-token-2 (- count) balanced limit))
4234 (let ((jump-syntax (if balanced
4235 c-jump-syntax-balanced
4236 c-jump-syntax-unbalanced))
4237 (last (point))
4238 (prev (point)))
4240 (if (zerop count)
4241 ;; If count is zero we should jump if in the middle of a token.
4242 (c-end-of-current-token))
4244 (save-restriction
4245 (if limit (narrow-to-region (point-min) limit))
4246 (if (/= (point)
4247 (progn (c-forward-syntactic-ws) (point)))
4248 ;; Skip whitespace. Count this as a move if we did in
4249 ;; fact move.
4250 (setq count (max (1- count) 0)))
4252 (if (eobp)
4253 ;; Moved out of bounds. Make sure the returned count isn't zero.
4254 (progn
4255 (if (zerop count) (setq count 1))
4256 (goto-char last))
4258 ;; Use `condition-case' to avoid having the limit tests
4259 ;; inside the loop.
4260 (condition-case nil
4261 (while (and
4262 (> count 0)
4263 (progn
4264 (setq last (point))
4265 (cond ((looking-at jump-syntax)
4266 (goto-char (scan-sexps (point) 1))
4268 ((looking-at c-nonsymbol-token-regexp)
4269 (goto-char (match-end 0))
4271 ;; `c-nonsymbol-token-regexp' above should always
4272 ;; match if there are correct tokens. Try to
4273 ;; widen to see if the limit was set in the
4274 ;; middle of one, else fall back to treating
4275 ;; the offending thing as a one character token.
4276 ((and limit
4277 (save-restriction
4278 (widen)
4279 (looking-at c-nonsymbol-token-regexp)))
4280 nil)
4282 (forward-char)
4283 t))))
4284 (c-forward-syntactic-ws)
4285 (setq prev last
4286 count (1- count)))
4287 (error (goto-char last)))
4289 (when (eobp)
4290 (goto-char prev)
4291 (setq count (1+ count)))))
4293 count)))
4295 (defun c-backward-token-2 (&optional count balanced limit)
4296 "Move backward by tokens.
4297 See `c-forward-token-2' for details."
4299 (or count (setq count 1))
4300 (if (< count 0)
4301 (- (c-forward-token-2 (- count) balanced limit))
4303 (or limit (setq limit (point-min)))
4304 (let ((jump-syntax (if balanced
4305 c-jump-syntax-balanced
4306 c-jump-syntax-unbalanced))
4307 (last (point)))
4309 (if (zerop count)
4310 ;; The count is zero so try to skip to the beginning of the
4311 ;; current token.
4312 (if (> (point)
4313 (progn (c-beginning-of-current-token) (point)))
4314 (if (< (point) limit)
4315 ;; The limit is inside the same token, so return 1.
4316 (setq count 1))
4318 ;; We're not in the middle of a token. If there's
4319 ;; whitespace after the point then we must move backward,
4320 ;; so set count to 1 in that case.
4321 (and (looking-at c-syntactic-ws-start)
4322 ;; If we're looking at a '#' that might start a cpp
4323 ;; directive then we have to do a more elaborate check.
4324 (or (/= (char-after) ?#)
4325 (not c-opt-cpp-prefix)
4326 (save-excursion
4327 (and (= (point)
4328 (progn (beginning-of-line)
4329 (looking-at "[ \t]*")
4330 (match-end 0)))
4331 (or (bobp)
4332 (progn (backward-char)
4333 (not (eq (char-before) ?\\)))))))
4334 (setq count 1))))
4336 ;; Use `condition-case' to avoid having to check for buffer
4337 ;; limits in `backward-char', `scan-sexps' and `goto-char' below.
4338 (condition-case nil
4339 (while (and
4340 (> count 0)
4341 (progn
4342 (c-backward-syntactic-ws)
4343 (backward-char)
4344 (if (looking-at jump-syntax)
4345 (goto-char (scan-sexps (1+ (point)) -1))
4346 ;; This can be very inefficient if there's a long
4347 ;; sequence of operator tokens without any separation.
4348 ;; That doesn't happen in practice, anyway.
4349 (c-beginning-of-current-token))
4350 (>= (point) limit)))
4351 (setq last (point)
4352 count (1- count)))
4353 (error (goto-char last)))
4355 (if (< (point) limit)
4356 (goto-char last))
4358 count)))
4360 (defun c-forward-token-1 (&optional count balanced limit)
4361 "Like `c-forward-token-2' but doesn't treat multicharacter operator
4362 tokens like \"==\" as single tokens, i.e. all sequences of symbol
4363 characters are jumped over character by character. This function is
4364 for compatibility only; it's only a wrapper over `c-forward-token-2'."
4365 (let ((c-nonsymbol-token-regexp "\\s."))
4366 (c-forward-token-2 count balanced limit)))
4368 (defun c-backward-token-1 (&optional count balanced limit)
4369 "Like `c-backward-token-2' but doesn't treat multicharacter operator
4370 tokens like \"==\" as single tokens, i.e. all sequences of symbol
4371 characters are jumped over character by character. This function is
4372 for compatibility only; it's only a wrapper over `c-backward-token-2'."
4373 (let ((c-nonsymbol-token-regexp "\\s."))
4374 (c-backward-token-2 count balanced limit)))
4377 ;; Tools for doing searches restricted to syntactically relevant text.
4379 (defun c-syntactic-re-search-forward (regexp &optional bound noerror
4380 paren-level not-inside-token
4381 lookbehind-submatch)
4382 "Like `re-search-forward', but only report matches that are found
4383 in syntactically significant text. I.e. matches in comments, macros
4384 or string literals are ignored. The start point is assumed to be
4385 outside any comment, macro or string literal, or else the content of
4386 that region is taken as syntactically significant text.
4388 NOERROR, in addition to the values nil, t, and <anything else>
4389 used in `re-search-forward' can also take the values
4390 'before-literal and 'after-literal. In these cases, when BOUND
4391 is also given and is inside a literal, and a search fails, point
4392 will be left, respectively before or after the literal. Be aware
4393 that with 'after-literal, if a string or comment is unclosed at
4394 the end of the buffer, point may be left there, even though it is
4395 inside a literal there.
4397 If PAREN-LEVEL is non-nil, an additional restriction is added to
4398 ignore matches in nested paren sexps. The search will also not go
4399 outside the current list sexp, which has the effect that if the point
4400 should be moved to BOUND when no match is found \(i.e. NOERROR is
4401 neither nil nor t), then it will be at the closing paren if the end of
4402 the current list sexp is encountered first.
4404 If NOT-INSIDE-TOKEN is non-nil, matches in the middle of tokens are
4405 ignored. Things like multicharacter operators and special symbols
4406 \(e.g. \"`()\" in Pike) are handled but currently not floating point
4407 constants.
4409 If LOOKBEHIND-SUBMATCH is non-nil, it's taken as a number of a
4410 subexpression in REGEXP. The end of that submatch is used as the
4411 position to check for syntactic significance. If LOOKBEHIND-SUBMATCH
4412 isn't used or if that subexpression didn't match then the start
4413 position of the whole match is used instead. The \"look behind\"
4414 subexpression is never tested before the starting position, so it
4415 might be a good idea to include \\=\\= as a match alternative in it.
4417 Optimization note: Matches might be missed if the \"look behind\"
4418 subexpression can match the end of nonwhite syntactic whitespace,
4419 i.e. the end of comments or cpp directives. This since the function
4420 skips over such things before resuming the search. It's on the other
4421 hand not safe to assume that the \"look behind\" subexpression never
4422 matches syntactic whitespace.
4424 Bug: Unbalanced parens inside cpp directives are currently not handled
4425 correctly \(i.e. they don't get ignored as they should) when
4426 PAREN-LEVEL is set.
4428 Note that this function might do hidden buffer changes. See the
4429 comment at the start of cc-engine.el for more info."
4431 (or bound (setq bound (point-max)))
4432 (if paren-level (setq paren-level -1))
4434 ;;(message "c-syntactic-re-search-forward %s %s %S" (point) bound regexp)
4436 (let ((start (point))
4438 ;; Start position for the last search.
4439 search-pos
4440 ;; The `parse-partial-sexp' state between the start position
4441 ;; and the point.
4442 state
4443 ;; The current position after the last state update. The next
4444 ;; `parse-partial-sexp' continues from here.
4445 (state-pos (point))
4446 ;; The position at which to check the state and the state
4447 ;; there. This is separate from `state-pos' since we might
4448 ;; need to back up before doing the next search round.
4449 check-pos check-state
4450 ;; Last position known to end a token.
4451 (last-token-end-pos (point-min))
4452 ;; Set when a valid match is found.
4453 found)
4455 (condition-case err
4456 (while
4457 (and
4458 (progn
4459 (setq search-pos (point))
4460 (if (re-search-forward regexp bound noerror)
4462 ;; Without the following, when PAREN-LEVEL is non-nil, and
4463 ;; NOERROR is not nil or t, and the very first search above
4464 ;; has just failed, point would end up at BOUND rather than
4465 ;; just before the next close paren.
4466 (when (and (eq search-pos start)
4467 paren-level
4468 (not (memq noerror '(nil t))))
4469 (setq state (parse-partial-sexp start bound -1))
4470 (if (eq (car state) -1)
4471 (setq bound (1- (point)))))
4472 nil))
4474 (progn
4475 (setq state (parse-partial-sexp
4476 state-pos (match-beginning 0) paren-level nil state)
4477 state-pos (point))
4478 (if (setq check-pos (and lookbehind-submatch
4479 (or (not paren-level)
4480 (>= (car state) 0))
4481 (match-end lookbehind-submatch)))
4482 (setq check-state (parse-partial-sexp
4483 state-pos check-pos paren-level nil state))
4484 (setq check-pos state-pos
4485 check-state state))
4487 ;; NOTE: If we got a look behind subexpression and get
4488 ;; an insignificant match in something that isn't
4489 ;; syntactic whitespace (i.e. strings or in nested
4490 ;; parentheses), then we can never skip more than a
4491 ;; single character from the match start position
4492 ;; (i.e. `state-pos' here) before continuing the
4493 ;; search. That since the look behind subexpression
4494 ;; might match the end of the insignificant region in
4495 ;; the next search.
4497 (cond
4498 ((elt check-state 7)
4499 ;; Match inside a line comment. Skip to eol. Use
4500 ;; `re-search-forward' instead of `skip-chars-forward' to get
4501 ;; the right bound behavior.
4502 (re-search-forward "[\n\r]" bound noerror))
4504 ((elt check-state 4)
4505 ;; Match inside a block comment. Skip to the '*/'.
4506 (search-forward "*/" bound noerror))
4508 ((and (not (elt check-state 5))
4509 (eq (char-before check-pos) ?/)
4510 (not (c-get-char-property (1- check-pos) 'syntax-table))
4511 (memq (char-after check-pos) '(?/ ?*)))
4512 ;; Match in the middle of the opener of a block or line
4513 ;; comment.
4514 (if (= (char-after check-pos) ?/)
4515 (re-search-forward "[\n\r]" bound noerror)
4516 (search-forward "*/" bound noerror)))
4518 ;; The last `parse-partial-sexp' above might have
4519 ;; stopped short of the real check position if the end
4520 ;; of the current sexp was encountered in paren-level
4521 ;; mode. The checks above are always false in that
4522 ;; case, and since they can do better skipping in
4523 ;; lookbehind-submatch mode, we do them before
4524 ;; checking the paren level.
4526 ((and paren-level
4527 (/= (setq tmp (car check-state)) 0))
4528 ;; Check the paren level first since we're short of the
4529 ;; syntactic checking position if the end of the
4530 ;; current sexp was encountered by `parse-partial-sexp'.
4531 (if (> tmp 0)
4533 ;; Inside a nested paren sexp.
4534 (if lookbehind-submatch
4535 ;; See the NOTE above.
4536 (progn (goto-char state-pos) t)
4537 ;; Skip out of the paren quickly.
4538 (setq state (parse-partial-sexp state-pos bound 0 nil state)
4539 state-pos (point)))
4541 ;; Have exited the current paren sexp.
4542 (if noerror
4543 (progn
4544 ;; The last `parse-partial-sexp' call above
4545 ;; has left us just after the closing paren
4546 ;; in this case, so we can modify the bound
4547 ;; to leave the point at the right position
4548 ;; upon return.
4549 (setq bound (1- (point)))
4550 nil)
4551 (signal 'search-failed (list regexp)))))
4553 ((setq tmp (elt check-state 3))
4554 ;; Match inside a string.
4555 (if (or lookbehind-submatch
4556 (not (integerp tmp)))
4557 ;; See the NOTE above.
4558 (progn (goto-char state-pos) t)
4559 ;; Skip to the end of the string before continuing.
4560 (let ((ender (make-string 1 tmp)) (continue t))
4561 (while (if (search-forward ender bound noerror)
4562 (progn
4563 (setq state (parse-partial-sexp
4564 state-pos (point) nil nil state)
4565 state-pos (point))
4566 (elt state 3))
4567 (setq continue nil)))
4568 continue)))
4570 ((save-excursion
4571 (save-match-data
4572 (c-beginning-of-macro start)))
4573 ;; Match inside a macro. Skip to the end of it.
4574 (c-end-of-macro)
4575 (cond ((<= (point) bound) t)
4576 (noerror nil)
4577 (t (signal 'search-failed (list regexp)))))
4579 ((and not-inside-token
4580 (or (< check-pos last-token-end-pos)
4581 (< check-pos
4582 (save-excursion
4583 (goto-char check-pos)
4584 (save-match-data
4585 (c-end-of-current-token last-token-end-pos))
4586 (setq last-token-end-pos (point))))))
4587 ;; Inside a token.
4588 (if lookbehind-submatch
4589 ;; See the NOTE above.
4590 (goto-char state-pos)
4591 (goto-char (min last-token-end-pos bound))))
4594 ;; A real match.
4595 (setq found t)
4596 nil)))
4598 ;; Should loop to search again, but take care to avoid
4599 ;; looping on the same spot.
4600 (or (/= search-pos (point))
4601 (if (= (point) bound)
4602 (if noerror
4604 (signal 'search-failed (list regexp)))
4605 (forward-char)
4606 t))))
4608 (error
4609 (goto-char start)
4610 (signal (car err) (cdr err))))
4612 ;;(message "c-syntactic-re-search-forward done %s" (or (match-end 0) (point)))
4614 (if found
4615 (progn
4616 (goto-char (match-end 0))
4617 (match-end 0))
4619 ;; Search failed. Set point as appropriate.
4620 (cond
4621 ((eq noerror t)
4622 (goto-char start))
4623 ((not (memq noerror '(before-literal after-literal)))
4624 (goto-char bound))
4625 (t (setq state (parse-partial-sexp state-pos bound nil nil state))
4626 (if (or (elt state 3) (elt state 4))
4627 (if (eq noerror 'before-literal)
4628 (goto-char (elt state 8))
4629 (parse-partial-sexp bound (point-max) nil nil
4630 state 'syntax-table))
4631 (goto-char bound))))
4633 nil)))
4635 (defvar safe-pos-list) ; bound in c-syntactic-skip-backward
4637 (defsubst c-ssb-lit-begin ()
4638 ;; Return the start of the literal point is in, or nil.
4639 ;; We read and write the variables `safe-pos', `safe-pos-list', `state'
4640 ;; bound in the caller.
4642 ;; Use `parse-partial-sexp' from a safe position down to the point to check
4643 ;; if it's outside comments and strings.
4644 (save-excursion
4645 (let ((pos (point)) safe-pos state)
4646 ;; Pick a safe position as close to the point as possible.
4648 ;; FIXME: Consult `syntax-ppss' here if our cache doesn't give a good
4649 ;; position.
4651 (while (and safe-pos-list
4652 (> (car safe-pos-list) (point)))
4653 (setq safe-pos-list (cdr safe-pos-list)))
4654 (unless (setq safe-pos (car-safe safe-pos-list))
4655 (setq safe-pos (max (or (c-safe-position
4656 (point) (c-parse-state))
4658 (point-min))
4659 safe-pos-list (list safe-pos)))
4661 ;; Cache positions along the way to use if we have to back up more. We
4662 ;; cache every closing paren on the same level. If the paren cache is
4663 ;; relevant in this region then we're typically already on the same
4664 ;; level as the target position. Note that we might cache positions
4665 ;; after opening parens in case safe-pos is in a nested list. That's
4666 ;; both uncommon and harmless.
4667 (while (progn
4668 (setq state (parse-partial-sexp
4669 safe-pos pos 0))
4670 (< (point) pos))
4671 (setq safe-pos (point)
4672 safe-pos-list (cons safe-pos safe-pos-list)))
4674 ;; If the state contains the start of the containing sexp we cache that
4675 ;; position too, so that parse-partial-sexp in the next run has a bigger
4676 ;; chance of starting at the same level as the target position and thus
4677 ;; will get more good safe positions into the list.
4678 (if (elt state 1)
4679 (setq safe-pos (1+ (elt state 1))
4680 safe-pos-list (cons safe-pos safe-pos-list)))
4682 (if (or (elt state 3) (elt state 4))
4683 ;; Inside string or comment. Continue search at the
4684 ;; beginning of it.
4685 (elt state 8)))))
4687 (defun c-syntactic-skip-backward (skip-chars &optional limit paren-level)
4688 "Like `skip-chars-backward' but only look at syntactically relevant chars,
4689 i.e. don't stop at positions inside syntactic whitespace or string
4690 literals. Preprocessor directives are also ignored, with the exception
4691 of the one that the point starts within, if any. If LIMIT is given,
4692 it's assumed to be at a syntactically relevant position.
4694 If PAREN-LEVEL is non-nil, the function won't stop in nested paren
4695 sexps, and the search will also not go outside the current paren sexp.
4696 However, if LIMIT or the buffer limit is reached inside a nested paren
4697 then the point will be left at the limit.
4699 Non-nil is returned if the point moved, nil otherwise.
4701 Note that this function might do hidden buffer changes. See the
4702 comment at the start of cc-engine.el for more info."
4704 (c-self-bind-state-cache
4705 (let ((start (point))
4706 state-2
4707 ;; A list of syntactically relevant positions in descending
4708 ;; order. It's used to avoid scanning repeatedly over
4709 ;; potentially large regions with `parse-partial-sexp' to verify
4710 ;; each position. Used in `c-ssb-lit-begin'
4711 safe-pos-list
4712 ;; The result from `c-beginning-of-macro' at the start position or the
4713 ;; start position itself if it isn't within a macro. Evaluated on
4714 ;; demand.
4715 start-macro-beg
4716 ;; The earliest position after the current one with the same paren
4717 ;; level. Used only when `paren-level' is set.
4718 lit-beg
4719 (paren-level-pos (point)))
4721 (while
4722 (progn
4723 ;; The next loop "tries" to find the end point each time round,
4724 ;; loops when it hasn't succeeded.
4725 (while
4726 (and
4727 (let ((pos (point)))
4728 (while (and
4729 (< (skip-chars-backward skip-chars limit) 0)
4730 ;; Don't stop inside a literal.
4731 (when (setq lit-beg (c-ssb-lit-begin))
4732 (goto-char lit-beg)
4733 t)))
4734 (< (point) pos))
4736 (let ((pos (point)) state-2 pps-end-pos)
4738 (cond
4739 ((and paren-level
4740 (save-excursion
4741 (setq state-2 (parse-partial-sexp
4742 pos paren-level-pos -1)
4743 pps-end-pos (point))
4744 (/= (car state-2) 0)))
4745 ;; Not at the right level.
4747 (if (and (< (car state-2) 0)
4748 ;; We stop above if we go out of a paren.
4749 ;; Now check whether it precedes or is
4750 ;; nested in the starting sexp.
4751 (save-excursion
4752 (setq state-2
4753 (parse-partial-sexp
4754 pps-end-pos paren-level-pos
4755 nil nil state-2))
4756 (< (car state-2) 0)))
4758 ;; We've stopped short of the starting position
4759 ;; so the hit was inside a nested list. Go up
4760 ;; until we are at the right level.
4761 (condition-case nil
4762 (progn
4763 (goto-char (scan-lists pos -1
4764 (- (car state-2))))
4765 (setq paren-level-pos (point))
4766 (if (and limit (>= limit paren-level-pos))
4767 (progn
4768 (goto-char limit)
4769 nil)
4771 (error
4772 (goto-char (or limit (point-min)))
4773 nil))
4775 ;; The hit was outside the list at the start
4776 ;; position. Go to the start of the list and exit.
4777 (goto-char (1+ (elt state-2 1)))
4778 nil))
4780 ((c-beginning-of-macro limit)
4781 ;; Inside a macro.
4782 (if (< (point)
4783 (or start-macro-beg
4784 (setq start-macro-beg
4785 (save-excursion
4786 (goto-char start)
4787 (c-beginning-of-macro limit)
4788 (point)))))
4791 ;; It's inside the same macro we started in so it's
4792 ;; a relevant match.
4793 (goto-char pos)
4794 nil))))))
4796 (> (point)
4797 (progn
4798 ;; Skip syntactic ws afterwards so that we don't stop at the
4799 ;; end of a comment if `skip-chars' is something like "^/".
4800 (c-backward-syntactic-ws)
4801 (point)))))
4803 ;; We might want to extend this with more useful return values in
4804 ;; the future.
4805 (/= (point) start))))
4807 ;; The following is an alternative implementation of
4808 ;; `c-syntactic-skip-backward' that uses backward movement to keep
4809 ;; track of the syntactic context. It turned out to be generally
4810 ;; slower than the one above which uses forward checks from earlier
4811 ;; safe positions.
4813 ;;(defconst c-ssb-stop-re
4814 ;; ;; The regexp matching chars `c-syntactic-skip-backward' needs to
4815 ;; ;; stop at to avoid going into comments and literals.
4816 ;; (concat
4817 ;; ;; Match comment end syntax and string literal syntax. Also match
4818 ;; ;; '/' for block comment endings (not covered by comment end
4819 ;; ;; syntax).
4820 ;; "\\s>\\|/\\|\\s\""
4821 ;; (if (memq 'gen-string-delim c-emacs-features)
4822 ;; "\\|\\s|"
4823 ;; "")
4824 ;; (if (memq 'gen-comment-delim c-emacs-features)
4825 ;; "\\|\\s!"
4826 ;; "")))
4828 ;;(defconst c-ssb-stop-paren-re
4829 ;; ;; Like `c-ssb-stop-re' but also stops at paren chars.
4830 ;; (concat c-ssb-stop-re "\\|\\s(\\|\\s)"))
4832 ;;(defconst c-ssb-sexp-end-re
4833 ;; ;; Regexp matching the ending syntax of a complex sexp.
4834 ;; (concat c-string-limit-regexp "\\|\\s)"))
4836 ;;(defun c-syntactic-skip-backward (skip-chars &optional limit paren-level)
4837 ;; "Like `skip-chars-backward' but only look at syntactically relevant chars,
4838 ;;i.e. don't stop at positions inside syntactic whitespace or string
4839 ;;literals. Preprocessor directives are also ignored. However, if the
4840 ;;point is within a comment, string literal or preprocessor directory to
4841 ;;begin with, its contents is treated as syntactically relevant chars.
4842 ;;If LIMIT is given, it limits the backward search and the point will be
4843 ;;left there if no earlier position is found.
4845 ;;If PAREN-LEVEL is non-nil, the function won't stop in nested paren
4846 ;;sexps, and the search will also not go outside the current paren sexp.
4847 ;;However, if LIMIT or the buffer limit is reached inside a nested paren
4848 ;;then the point will be left at the limit.
4850 ;;Non-nil is returned if the point moved, nil otherwise.
4852 ;;Note that this function might do hidden buffer changes. See the
4853 ;;comment at the start of cc-engine.el for more info."
4855 ;; (save-restriction
4856 ;; (when limit
4857 ;; (narrow-to-region limit (point-max)))
4859 ;; (let ((start (point)))
4860 ;; (catch 'done
4861 ;; (while (let ((last-pos (point))
4862 ;; (stop-pos (progn
4863 ;; (skip-chars-backward skip-chars)
4864 ;; (point))))
4866 ;; ;; Skip back over the same region as
4867 ;; ;; `skip-chars-backward' above, but keep to
4868 ;; ;; syntactically relevant positions.
4869 ;; (goto-char last-pos)
4870 ;; (while (and
4871 ;; ;; `re-search-backward' with a single char regexp
4872 ;; ;; should be fast.
4873 ;; (re-search-backward
4874 ;; (if paren-level c-ssb-stop-paren-re c-ssb-stop-re)
4875 ;; stop-pos 'move)
4877 ;; (progn
4878 ;; (cond
4879 ;; ((looking-at "\\s(")
4880 ;; ;; `paren-level' is set and we've found the
4881 ;; ;; start of the containing paren.
4882 ;; (forward-char)
4883 ;; (throw 'done t))
4885 ;; ((looking-at c-ssb-sexp-end-re)
4886 ;; ;; We're at the end of a string literal or paren
4887 ;; ;; sexp (if `paren-level' is set).
4888 ;; (forward-char)
4889 ;; (condition-case nil
4890 ;; (c-backward-sexp)
4891 ;; (error
4892 ;; (goto-char limit)
4893 ;; (throw 'done t))))
4895 ;; (t
4896 ;; (forward-char)
4897 ;; ;; At the end of some syntactic ws or possibly
4898 ;; ;; after a plain '/' operator.
4899 ;; (let ((pos (point)))
4900 ;; (c-backward-syntactic-ws)
4901 ;; (if (= pos (point))
4902 ;; ;; Was a plain '/' operator. Go past it.
4903 ;; (backward-char)))))
4905 ;; (> (point) stop-pos))))
4907 ;; ;; Now the point is either at `stop-pos' or at some
4908 ;; ;; position further back if `stop-pos' was at a
4909 ;; ;; syntactically irrelevant place.
4911 ;; ;; Skip additional syntactic ws so that we don't stop
4912 ;; ;; at the end of a comment if `skip-chars' is
4913 ;; ;; something like "^/".
4914 ;; (c-backward-syntactic-ws)
4916 ;; (< (point) stop-pos))))
4918 ;; ;; We might want to extend this with more useful return values
4919 ;; ;; in the future.
4920 ;; (/= (point) start))))
4923 ;; Tools for handling comments and string literals.
4925 (defun c-in-literal (&optional lim detect-cpp)
4926 "Return the type of literal point is in, if any.
4927 The return value is `c' if in a C-style comment, `c++' if in a C++
4928 style comment, `string' if in a string literal, `pound' if DETECT-CPP
4929 is non-nil and in a preprocessor line, or nil if somewhere else.
4930 Optional LIM is used as the backward limit of the search. If omitted,
4931 or nil, `c-beginning-of-defun' is used.
4933 The last point calculated is cached if the cache is enabled, i.e. if
4934 `c-in-literal-cache' is bound to a two element vector.
4936 Note that this function might do hidden buffer changes. See the
4937 comment at the start of cc-engine.el for more info."
4938 (save-restriction
4939 (widen)
4940 (let ((lit (c-state-semi-pp-to-literal (point))))
4941 (or (cadr lit)
4942 (and detect-cpp
4943 (save-excursion (c-beginning-of-macro))
4944 'pound)))))
4946 (defun c-literal-limits (&optional lim near not-in-delimiter)
4947 "Return a cons of the beginning and end positions of the comment or
4948 string surrounding point (including both delimiters), or nil if point
4949 isn't in one. If LIM is non-nil, it's used as the \"safe\" position
4950 to start parsing from. If NEAR is non-nil, then the limits of any
4951 literal next to point is returned. \"Next to\" means there's only
4952 spaces and tabs between point and the literal. The search for such a
4953 literal is done first in forward direction. If NOT-IN-DELIMITER is
4954 non-nil, the case when point is inside a starting delimiter won't be
4955 recognized. This only has effect for comments which have starting
4956 delimiters with more than one character.
4958 Note that this function might do hidden buffer changes. See the
4959 comment at the start of cc-engine.el for more info."
4961 (save-excursion
4962 (let*
4963 ((pos (point))
4964 (lit-limits
4965 (if lim
4966 (let ((s (parse-partial-sexp lim (point))))
4967 (when (or (nth 3 s)
4968 (and (nth 4 s) (not (eq (nth 7 s) 'syntax-table))))
4969 (cons (nth 8 s)
4970 (progn (parse-partial-sexp (point) (point-max)
4971 nil nil
4973 'syntax-table)
4974 (point)))))
4975 (let ((pp-to-lit (c-state-full-pp-to-literal pos not-in-delimiter)))
4976 (car (cddr pp-to-lit))))))
4977 (cond
4978 (lit-limits)
4980 (near
4981 (goto-char pos)
4982 ;; Search forward for a literal.
4983 (skip-chars-forward " \t")
4984 (cond
4985 ((looking-at c-string-limit-regexp) ; String.
4986 (cons (point) (or (c-safe (c-forward-sexp 1) (point))
4987 (point-max))))
4989 ((looking-at c-comment-start-regexp) ; Line or block comment.
4990 (cons (point) (progn (c-forward-single-comment) (point))))
4993 ;; Search backward.
4994 (skip-chars-backward " \t")
4996 (let ((end (point)) beg)
4997 (cond
4998 ((save-excursion
4999 (< (skip-syntax-backward c-string-syntax) 0)) ; String.
5000 (setq beg (c-safe (c-backward-sexp 1) (point))))
5002 ((and (c-safe (forward-char -2) t)
5003 (looking-at "*/"))
5004 ;; Block comment. Due to the nature of line
5005 ;; comments, they will always be covered by the
5006 ;; normal case above.
5007 (goto-char end)
5008 (c-backward-single-comment)
5009 ;; If LIM is bogus, beg will be bogus.
5010 (setq beg (point))))
5012 (if beg (cons beg end))))))
5013 ))))
5015 (defun c-literal-start (&optional safe-pos)
5016 "Return the start of the string or comment surrounding point, or nil if
5017 point isn't in one. SAFE-POS, if non-nil, is a position before point which is
5018 a known \"safe position\", i.e. outside of any string or comment."
5019 (if safe-pos
5020 (let ((s (parse-partial-sexp safe-pos (point))))
5021 (and (or (nth 3 s)
5022 (and (nth 4 s) (not (eq (nth 7 s) 'syntax-table))))
5023 (nth 8 s)))
5024 (car (cddr (c-state-semi-pp-to-literal (point))))))
5026 ;; In case external callers use this; it did have a docstring.
5027 (defalias 'c-literal-limits-fast 'c-literal-limits)
5029 (defun c-collect-line-comments (range)
5030 "If the argument is a cons of two buffer positions (such as returned by
5031 `c-literal-limits'), and that range contains a C++ style line comment,
5032 then an extended range is returned that contains all adjacent line
5033 comments (i.e. all comments that starts in the same column with no
5034 empty lines or non-whitespace characters between them). Otherwise the
5035 argument is returned.
5037 Note that this function might do hidden buffer changes. See the
5038 comment at the start of cc-engine.el for more info."
5040 (save-excursion
5041 (condition-case nil
5042 (if (and (consp range) (progn
5043 (goto-char (car range))
5044 (looking-at c-line-comment-starter)))
5045 (let ((col (current-column))
5046 (beg (point))
5047 (bopl (c-point 'bopl))
5048 (end (cdr range)))
5049 ;; Got to take care in the backward direction to handle
5050 ;; comments which are preceded by code.
5051 (while (and (c-backward-single-comment)
5052 (>= (point) bopl)
5053 (looking-at c-line-comment-starter)
5054 (= col (current-column)))
5055 (setq beg (point)
5056 bopl (c-point 'bopl)))
5057 (goto-char end)
5058 (while (and (progn (skip-chars-forward " \t")
5059 (looking-at c-line-comment-starter))
5060 (= col (current-column))
5061 (prog1 (zerop (forward-line 1))
5062 (setq end (point)))))
5063 (cons beg end))
5064 range)
5065 (error range))))
5067 (defun c-literal-type (range)
5068 "Convenience function that given the result of `c-literal-limits',
5069 returns nil or the type of literal that the range surrounds, one
5070 of the symbols `c', `c++' or `string'. It's much faster than using
5071 `c-in-literal' and is intended to be used when you need both the
5072 type of a literal and its limits.
5074 Note that this function might do hidden buffer changes. See the
5075 comment at the start of cc-engine.el for more info."
5077 (if (consp range)
5078 (save-excursion
5079 (goto-char (car range))
5080 (cond ((looking-at c-string-limit-regexp) 'string)
5081 ((or (looking-at "//") ; c++ line comment
5082 (and (looking-at "\\s<") ; comment starter
5083 (looking-at "#"))) ; awk comment.
5084 'c++)
5085 (t 'c))) ; Assuming the range is valid.
5086 range))
5088 (defsubst c-determine-limit-get-base (start try-size)
5089 ;; Get a "safe place" approximately TRY-SIZE characters before START.
5090 ;; This defsubst doesn't preserve point.
5091 (let* ((pos (max (- start try-size) (point-min)))
5092 (s (c-state-semi-pp-to-literal pos)))
5093 (or (car (cddr s)) pos)))
5095 (defun c-determine-limit (how-far-back &optional start try-size)
5096 ;; Return a buffer position HOW-FAR-BACK non-literal characters from START
5097 ;; (default point). This is done by going back further in the buffer then
5098 ;; searching forward for literals. The position found won't be in a
5099 ;; literal. We start searching for the sought position TRY-SIZE (default
5100 ;; twice HOW-FAR-BACK) bytes back from START. This function must be fast.
5101 ;; :-)
5102 (save-excursion
5103 (let* ((start (or start (point)))
5104 (try-size (or try-size (* 2 how-far-back)))
5105 (base (c-determine-limit-get-base start try-size))
5106 (pos base)
5108 (s (parse-partial-sexp pos pos)) ; null state.
5109 stack elt size
5110 (count 0))
5111 (while (< pos start)
5112 ;; Move forward one literal each time round this loop.
5113 ;; Move forward to the start of a comment or string.
5114 (setq s (parse-partial-sexp
5116 start
5117 nil ; target-depth
5118 nil ; stop-before
5119 s ; state
5120 'syntax-table)) ; stop-comment
5122 ;; Gather details of the non-literal-bit - starting pos and size.
5123 (setq size (- (if (or (and (nth 4 s) (not (eq (nth 7 s) 'syntax-table)))
5124 (nth 3 s))
5125 (nth 8 s)
5126 (point))
5127 pos))
5128 (if (> size 0)
5129 (setq stack (cons (cons pos size) stack)))
5131 ;; Move forward to the end of the comment/string.
5132 (if (or (and (nth 4 s) (not (eq (nth 7 s) 'syntax-table)))
5133 (nth 3 s))
5134 (setq s (parse-partial-sexp
5135 (point)
5136 start
5137 nil ; target-depth
5138 nil ; stop-before
5139 s ; state
5140 'syntax-table))) ; stop-comment
5141 (setq pos (point)))
5143 ;; Now try and find enough non-literal characters recorded on the stack.
5144 ;; Go back one recorded literal each time round this loop.
5145 (while (and (< count how-far-back)
5146 stack)
5147 (setq elt (car stack)
5148 stack (cdr stack))
5149 (setq count (+ count (cdr elt))))
5151 ;; Have we found enough yet?
5152 (cond
5153 ((>= count how-far-back)
5154 (+ (car elt) (- count how-far-back)))
5155 ((eq base (point-min))
5156 (point-min))
5158 (c-determine-limit (- how-far-back count) base try-size))))))
5160 (defun c-determine-+ve-limit (how-far &optional start-pos)
5161 ;; Return a buffer position about HOW-FAR non-literal characters forward
5162 ;; from START-POS (default point), which must not be inside a literal.
5163 (save-excursion
5164 (let ((pos (or start-pos (point)))
5165 (count how-far)
5166 (s (parse-partial-sexp (point) (point)))) ; null state
5167 (while (and (not (eobp))
5168 (> count 0))
5169 ;; Scan over counted characters.
5170 (setq s (parse-partial-sexp
5172 (min (+ pos count) (point-max))
5173 nil ; target-depth
5174 nil ; stop-before
5175 s ; state
5176 'syntax-table)) ; stop-comment
5177 (setq count (- count (- (point) pos) 1)
5178 pos (point))
5179 ;; Scan over literal characters.
5180 (if (nth 8 s)
5181 (setq s (parse-partial-sexp
5183 (point-max)
5184 nil ; target-depth
5185 nil ; stop-before
5186 s ; state
5187 'syntax-table) ; stop-comment
5188 pos (point))))
5189 (point))))
5192 ;; `c-find-decl-spots' and accompanying stuff.
5194 ;; Variables used in `c-find-decl-spots' to cache the search done for
5195 ;; the first declaration in the last call. When that function starts,
5196 ;; it needs to back up over syntactic whitespace to look at the last
5197 ;; token before the region being searched. That can sometimes cause
5198 ;; moves back and forth over a quite large region of comments and
5199 ;; macros, which would be repeated for each changed character when
5200 ;; we're called during fontification, since font-lock refontifies the
5201 ;; current line for each change. Thus it's worthwhile to cache the
5202 ;; first match.
5204 ;; `c-find-decl-syntactic-pos' is a syntactically relevant position in
5205 ;; the syntactic whitespace less or equal to some start position.
5206 ;; There's no cached value if it's nil.
5208 ;; `c-find-decl-match-pos' is the match position if
5209 ;; `c-find-decl-prefix-search' matched before the syntactic whitespace
5210 ;; at `c-find-decl-syntactic-pos', or nil if there's no such match.
5211 (defvar c-find-decl-syntactic-pos nil)
5212 (make-variable-buffer-local 'c-find-decl-syntactic-pos)
5213 (defvar c-find-decl-match-pos nil)
5214 (make-variable-buffer-local 'c-find-decl-match-pos)
5216 (defsubst c-invalidate-find-decl-cache (change-min-pos)
5217 (and c-find-decl-syntactic-pos
5218 (< change-min-pos c-find-decl-syntactic-pos)
5219 (setq c-find-decl-syntactic-pos nil)))
5221 ; (defface c-debug-decl-spot-face
5222 ; '((t (:background "Turquoise")))
5223 ; "Debug face to mark the spots where `c-find-decl-spots' stopped.")
5224 ; (defface c-debug-decl-sws-face
5225 ; '((t (:background "Khaki")))
5226 ; "Debug face to mark the syntactic whitespace between the declaration
5227 ; spots and the preceding token end.")
5229 (defmacro c-debug-put-decl-spot-faces (match-pos decl-pos)
5230 (when (facep 'c-debug-decl-spot-face)
5231 `(c-save-buffer-state ((match-pos ,match-pos) (decl-pos ,decl-pos))
5232 (c-debug-add-face (max match-pos (point-min)) decl-pos
5233 'c-debug-decl-sws-face)
5234 (c-debug-add-face decl-pos (min (1+ decl-pos) (point-max))
5235 'c-debug-decl-spot-face))))
5236 (defmacro c-debug-remove-decl-spot-faces (beg end)
5237 (when (facep 'c-debug-decl-spot-face)
5238 `(c-save-buffer-state ()
5239 (c-debug-remove-face ,beg ,end 'c-debug-decl-spot-face)
5240 (c-debug-remove-face ,beg ,end 'c-debug-decl-sws-face))))
5242 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
5243 ;; Machinery for determining when we're at top level (this including being
5244 ;; directly inside a class or namespace, etc.)
5246 ;; We maintain a stack of brace depths in structures like classes and
5247 ;; namespaces. The car of this structure, when non-nil, indicates that the
5248 ;; associated position is within a template (etc.) structure, and the value is
5249 ;; the position where the (outermost) template ends. The other elements in
5250 ;; the structure are stacked elements, one each for each enclosing "top level"
5251 ;; structure.
5253 ;; At the very outermost level, the value of the stack would be (nil 1), the
5254 ;; "1" indicating an enclosure in a notional all-enclosing block. After
5255 ;; passing a keyword such as "namespace", the value would become (nil 0 1).
5256 ;; At this point, passing a semicolon would cause the 0 to be dropped from the
5257 ;; stack (at any other time, a semicolon is ignored). Alternatively, on
5258 ;; passing an opening brace, the stack would become (nil 1 1). Each opening
5259 ;; brace passed causes the cadr to be incremented, and passing closing braces
5260 ;; causes it to be decremented until it reaches 1. On passing a closing brace
5261 ;; when the cadr of the stack is at 1, this causes it to be removed from the
5262 ;; stack, the corresponding namespace (etc.) structure having been closed.
5264 ;; There is a special stack value -1 which means the C++ colon operator
5265 ;; introducing a list of inherited classes has just been parsed. The value
5266 ;; persists on the stack until the next open brace or semicolon.
5268 ;; When the car of the stack is non-nil, i.e. when we're in a template (etc.)
5269 ;; structure, braces are not counted. The counting resumes only after passing
5270 ;; the template's closing position, which is recorded in the car of the stack.
5272 ;; The test for being at top level consists of the cadr being 0 or 1.
5274 ;; The values of this stack throughout a buffer are cached in a simple linear
5275 ;; cache, every 5000 characters.
5277 ;; Note to maintainers: This cache mechanism is MUCH faster than recalculating
5278 ;; the stack at every entry to `c-find-decl-spots' using `c-at-toplevel-p' or
5279 ;; the like.
5280 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
5281 ;; The approximate interval at which we cache the value of the brace stack.
5282 (defconst c-bs-interval 5000)
5283 ;; The list of cached values of the brace stack. Each value in the list is a
5284 ;; cons of the position it is valid for and the value of the stack as
5285 ;; described above.
5286 (defvar c-bs-cache nil)
5287 (make-variable-buffer-local 'c-bs-cache)
5288 ;; The position of the buffer at and below which entries in `c-bs-cache' are
5289 ;; valid.
5290 (defvar c-bs-cache-limit 1)
5291 (make-variable-buffer-local 'c-bs-cache-limit)
5292 ;; The previous buffer position for which the brace stack value was
5293 ;; determined.
5294 (defvar c-bs-prev-pos most-positive-fixnum)
5295 (make-variable-buffer-local 'c-bs-prev-pos)
5296 ;; The value of the brace stack at `c-bs-prev-pos'.
5297 (defvar c-bs-prev-stack nil)
5298 (make-variable-buffer-local 'c-bs-prev-stack)
5300 (defun c-init-bs-cache ()
5301 ;; Initialize the cache in `c-bs-cache' and related variables.
5302 (setq c-bs-cache nil
5303 c-bs-cache-limit 1
5304 c-bs-prev-pos most-positive-fixnum
5305 c-bs-prev-stack nil))
5307 (defun c-truncate-bs-cache (pos &rest _ignore)
5308 ;; Truncate the upper bound of the cache `c-bs-cache' to POS, if it is
5309 ;; higher than that position. This is called as either a before- or
5310 ;; after-change-function.
5311 (setq c-bs-cache-limit
5312 (min c-bs-cache-limit pos)))
5314 (defun c-update-brace-stack (stack from to)
5315 ;; Give a brace-stack which has the value STACK at position FROM, update it
5316 ;; to it's value at position TO, where TO is after (or equal to) FROM.
5317 ;; Return a cons of either TO (if it is outside a literal) and this new
5318 ;; value, or of the next position after TO outside a literal and the new
5319 ;; value.
5320 (let (match kwd-sym (prev-match-pos 1)
5321 (s (cdr stack))
5322 (bound-<> (car stack))
5324 (save-excursion
5325 (cond
5326 ((and bound-<> (<= to bound-<>))
5327 (goto-char to)) ; Nothing to do.
5328 (bound-<>
5329 (goto-char bound-<>)
5330 (setq bound-<> nil))
5331 (t (goto-char from)))
5332 (while (and (< (point) to)
5333 (c-syntactic-re-search-forward
5334 (if (<= (car s) 0)
5335 c-brace-stack-thing-key
5336 c-brace-stack-no-semi-key)
5337 to 'after-literal)
5338 (> (point) prev-match-pos)) ; prevent infinite loop.
5339 (setq prev-match-pos (point))
5340 (setq match (match-string-no-properties 1)
5341 kwd-sym (c-keyword-sym match))
5342 (cond
5343 ((and (equal match "{")
5344 (progn (backward-char)
5345 (prog1 (looking-at "\\s(")
5346 (forward-char))))
5347 (setq s (if s
5348 (cons (if (<= (car s) 0)
5350 (1+ (car s)))
5351 (cdr s))
5352 (list 1))))
5353 ((and (equal match "}")
5354 (progn (backward-char)
5355 (prog1 (looking-at "\\s)")
5356 (forward-char))))
5357 (setq s
5358 (cond
5359 ((and s (> (car s) 1))
5360 (cons (1- (car s)) (cdr s)))
5361 ((and (cdr s) (eq (car s) 1))
5362 (cdr s))
5363 (t s))))
5364 ((and (equal match "<")
5365 (progn (backward-char)
5366 (prog1 (looking-at "\\s(")
5367 (forward-char))))
5368 (backward-char)
5369 (if (c-forward-<>-arglist nil) ; Should always work.
5370 (when (> (point) to)
5371 (setq bound-<> (point)))
5372 (forward-char)))
5373 ((and (equal match ":")
5375 (eq (car s) 0))
5376 (setq s (cons -1 (cdr s))))
5377 ((and (equal match ",")
5378 (eq (car s) -1))) ; at "," in "class foo : bar, ..."
5379 ((member match '(";" "," ")"))
5380 (when (and s (cdr s) (<= (car s) 0))
5381 (setq s (cdr s))))
5382 ((c-keyword-member kwd-sym 'c-flat-decl-block-kwds)
5383 (push 0 s))))
5384 (cons (point)
5385 (cons bound-<> s)))))
5387 (defun c-brace-stack-at (here)
5388 ;; Given a buffer position HERE, Return the value of the brace stack there.
5389 (save-excursion
5390 (save-restriction
5391 (widen)
5392 (let ((c c-bs-cache)
5393 (can-use-prev (<= c-bs-prev-pos c-bs-cache-limit))
5394 elt stack pos npos high-elt)
5395 ;; Trim the cache to take account of buffer changes.
5396 (while (and c
5397 (> (caar c) c-bs-cache-limit))
5398 (setq c (cdr c)))
5399 (setq c-bs-cache c)
5401 (while (and c
5402 (> (caar c) here))
5403 (setq high-elt (car c))
5404 (setq c (cdr c)))
5405 (setq pos (or (and c (caar c))
5406 (point-min)))
5408 (setq elt (if c
5409 (car c)
5410 (cons (point-min)
5411 (cons nil (list 1)))))
5412 (when (not high-elt)
5413 (setq stack (cdr elt))
5414 (while
5415 ;; Add an element to `c-state-semi-nonlit-pos-cache' each iteration.
5416 (<= (setq npos (+ pos c-bs-interval)) here)
5417 (setq elt (c-update-brace-stack stack pos npos))
5418 (setq npos (car elt))
5419 (setq stack (cdr elt))
5420 (unless (eq npos (point-max)) ; NPOS could be in a literal at EOB.
5421 (setq c-bs-cache (cons elt c-bs-cache)))
5422 (setq pos npos)))
5424 (if (> pos c-bs-cache-limit)
5425 (setq c-bs-cache-limit pos))
5427 ;; Can we just use the previous value?
5428 (if (and can-use-prev
5429 (<= c-bs-prev-pos here)
5430 (> c-bs-prev-pos (car elt)))
5431 (setq pos c-bs-prev-pos
5432 stack c-bs-prev-stack)
5433 (setq pos (car elt)
5434 stack (cdr elt)))
5435 (if (> here c-bs-cache-limit)
5436 (setq c-bs-cache-limit here))
5437 (setq elt (c-update-brace-stack stack pos here)
5438 c-bs-prev-pos (car elt)
5439 c-bs-prev-stack (cdr elt))))))
5441 (defun c-bs-at-toplevel-p (here)
5442 ;; Is position HERE at the top level, as indicated by the brace stack?
5443 (let ((stack (c-brace-stack-at here)))
5444 (or (null stack) ; Probably unnecessary.
5445 (<= (cadr stack) 1))))
5447 (defmacro c-find-decl-prefix-search ()
5448 ;; Macro used inside `c-find-decl-spots'. It ought to be a defun,
5449 ;; but it contains lots of free variables that refer to things
5450 ;; inside `c-find-decl-spots'. The point is left at `cfd-match-pos'
5451 ;; if there is a match, otherwise at `cfd-limit'.
5453 ;; The macro moves point forward to the next putative start of a declaration
5454 ;; or cfd-limit. This decl start is the next token after a "declaration
5455 ;; prefix". The declaration prefix is the earlier of `cfd-prop-match' and
5456 ;; `cfd-re-match'. `cfd-match-pos' is set to the decl prefix.
5458 ;; This macro might do hidden buffer changes.
5460 '(progn
5461 ;; Find the next property match position if we haven't got one already.
5462 (unless cfd-prop-match
5463 (save-excursion
5464 (while (progn
5465 (goto-char (c-next-single-property-change
5466 (point) 'c-type nil cfd-limit))
5467 (and (< (point) cfd-limit)
5468 (not (eq (c-get-char-property (1- (point)) 'c-type)
5469 'c-decl-end)))))
5470 (setq cfd-prop-match (point))))
5472 ;; Find the next `c-decl-prefix-or-start-re' match if we haven't
5473 ;; got one already.
5474 (unless cfd-re-match
5476 (if (> cfd-re-match-end (point))
5477 (goto-char cfd-re-match-end))
5479 ;; Each time round, the next `while' moves forward over a pseudo match
5480 ;; of `c-decl-prefix-or-start-re' which is either inside a literal, or
5481 ;; is a ":" not preceded by "public", etc.. `cfd-re-match' and
5482 ;; `cfd-re-match-end' get set.
5483 (while
5484 (progn
5485 (setq cfd-re-match-end (re-search-forward c-decl-prefix-or-start-re
5486 cfd-limit 'move))
5487 (cond
5488 ((null cfd-re-match-end)
5489 ;; No match. Finish up and exit the loop.
5490 (setq cfd-re-match cfd-limit)
5491 nil)
5492 ((c-got-face-at
5493 (if (setq cfd-re-match (match-end 1))
5494 ;; Matched the end of a token preceding a decl spot.
5495 (progn
5496 (goto-char cfd-re-match)
5497 (1- cfd-re-match))
5498 ;; Matched a token that start a decl spot.
5499 (goto-char (match-beginning 0))
5500 (point))
5501 c-literal-faces)
5502 ;; Pseudo match inside a comment or string literal. Skip out
5503 ;; of comments and string literals.
5504 (while (progn
5505 (goto-char (c-next-single-property-change
5506 (point) 'face nil cfd-limit))
5507 (and (< (point) cfd-limit)
5508 (c-got-face-at (point) c-literal-faces))))
5509 t) ; Continue the loop over pseudo matches.
5510 ((and c-opt-identifier-concat-key
5511 (match-string 1)
5512 (save-excursion
5513 (goto-char (match-beginning 1))
5514 (save-match-data
5515 (looking-at c-opt-identifier-concat-key))))
5516 ;; Found, e.g., "::" in C++
5518 ((and (match-string 1)
5519 (string= (match-string 1) ":")
5520 (save-excursion
5521 (or (/= (c-backward-token-2 2) 0) ; no search limit. :-(
5522 (not (looking-at c-decl-start-colon-kwd-re)))))
5523 ;; Found a ":" which isn't part of "public:", etc.
5525 (t nil)))) ;; Found a real match. Exit the pseudo-match loop.
5527 ;; If our match was at the decl start, we have to back up over the
5528 ;; preceding syntactic ws to set `cfd-match-pos' and to catch
5529 ;; any decl spots in the syntactic ws.
5530 (unless cfd-re-match
5531 (c-backward-syntactic-ws)
5532 (setq cfd-re-match (point))))
5534 ;; Choose whichever match is closer to the start.
5535 (if (< cfd-re-match cfd-prop-match)
5536 (setq cfd-match-pos cfd-re-match
5537 cfd-re-match nil)
5538 (setq cfd-match-pos cfd-prop-match
5539 cfd-prop-match nil))
5540 (setq cfd-top-level (c-bs-at-toplevel-p cfd-match-pos))
5542 (goto-char cfd-match-pos)
5544 (when (< cfd-match-pos cfd-limit)
5545 ;; Skip forward past comments only so we don't skip macros.
5546 (c-forward-comments)
5547 ;; Set the position to continue at. We can avoid going over
5548 ;; the comments skipped above a second time, but it's possible
5549 ;; that the comment skipping has taken us past `cfd-prop-match'
5550 ;; since the property might be used inside comments.
5551 (setq cfd-continue-pos (if cfd-prop-match
5552 (min cfd-prop-match (point))
5553 (point))))))
5555 (defun c-find-decl-spots (cfd-limit cfd-decl-re cfd-face-checklist cfd-fun)
5556 ;; Call CFD-FUN for each possible spot for a declaration, cast or
5557 ;; label from the point to CFD-LIMIT.
5559 ;; CFD-FUN is called with point at the start of the spot. It's passed two
5560 ;; arguments: The first is the end position of the token preceding the spot,
5561 ;; or 0 for the implicit match at bob. The second is a flag that is t when
5562 ;; the match is inside a macro. Point should be moved forward by at least
5563 ;; one token.
5565 ;; If CFD-FUN adds `c-decl-end' properties somewhere below the current spot,
5566 ;; it should return non-nil to ensure that the next search will find them.
5568 ;; Such a spot is:
5569 ;; o The first token after bob.
5570 ;; o The first token after the end of submatch 1 in
5571 ;; `c-decl-prefix-or-start-re' when that submatch matches. This
5572 ;; submatch is typically a (L or R) brace or paren, a ;, or a ,.
5573 ;; o The start of each `c-decl-prefix-or-start-re' match when
5574 ;; submatch 1 doesn't match. This is, for example, the keyword
5575 ;; "class" in Pike.
5576 ;; o The start of a previously recognized declaration; "recognized"
5577 ;; means that the last char of the previous token has a `c-type'
5578 ;; text property with the value `c-decl-end'; this only holds
5579 ;; when `c-type-decl-end-used' is set.
5581 ;; Only a spot that match CFD-DECL-RE and whose face is in the
5582 ;; CFD-FACE-CHECKLIST list causes CFD-FUN to be called. The face
5583 ;; check is disabled if CFD-FACE-CHECKLIST is nil.
5585 ;; If the match is inside a macro then the buffer is narrowed to the
5586 ;; end of it, so that CFD-FUN can investigate the following tokens
5587 ;; without matching something that begins inside a macro and ends
5588 ;; outside it. It's to avoid this work that the CFD-DECL-RE and
5589 ;; CFD-FACE-CHECKLIST checks exist.
5591 ;; The spots are visited approximately in order from top to bottom.
5592 ;; It's however the positions where `c-decl-prefix-or-start-re'
5593 ;; matches and where `c-decl-end' properties are found that are in
5594 ;; order. Since the spots often are at the following token, they
5595 ;; might be visited out of order insofar as more spots are reported
5596 ;; later on within the syntactic whitespace between the match
5597 ;; positions and their spots.
5599 ;; It's assumed that comments and strings are fontified in the
5600 ;; searched range.
5602 ;; This is mainly used in fontification, and so has an elaborate
5603 ;; cache to handle repeated calls from the same start position; see
5604 ;; the variables above.
5606 ;; All variables in this function begin with `cfd-' to avoid name
5607 ;; collision with the (dynamically bound) variables used in CFD-FUN.
5609 ;; This function might do hidden buffer changes.
5611 (let ((cfd-start-pos (point)) ; never changed
5612 (cfd-buffer-end (point-max))
5613 ;; The end of the token preceding the decl spot last found
5614 ;; with `c-decl-prefix-or-start-re'. `cfd-limit' if there's
5615 ;; no match.
5616 cfd-re-match
5617 ;; The end position of the last `c-decl-prefix-or-start-re'
5618 ;; match. If this is greater than `cfd-continue-pos', the
5619 ;; next regexp search is started here instead.
5620 (cfd-re-match-end (point-min))
5621 ;; The end of the last `c-decl-end' found by
5622 ;; `c-find-decl-prefix-search'. `cfd-limit' if there's no
5623 ;; match. If searching for the property isn't needed then we
5624 ;; disable it by setting it to `cfd-limit' directly.
5625 (cfd-prop-match (unless c-type-decl-end-used cfd-limit))
5626 ;; The end of the token preceding the decl spot last found by
5627 ;; `c-find-decl-prefix-search'. 0 for the implicit match at
5628 ;; bob. `cfd-limit' if there's no match. In other words,
5629 ;; this is the minimum of `cfd-re-match' and `cfd-prop-match'.
5630 (cfd-match-pos cfd-limit)
5631 ;; The position to continue searching at.
5632 cfd-continue-pos
5633 ;; The position of the last "real" token we've stopped at.
5634 ;; This can be greater than `cfd-continue-pos' when we get
5635 ;; hits inside macros or at `c-decl-end' positions inside
5636 ;; comments.
5637 (cfd-token-pos 0)
5638 ;; The end position of the last entered macro.
5639 (cfd-macro-end 0)
5640 ;; Whether the last position returned from `c-find-decl-prefix-search'
5641 ;; is at the top-level (including directly in a class or namespace,
5642 ;; etc.).
5643 cfd-top-level)
5645 ;; Initialize by finding a syntactically relevant start position
5646 ;; before the point, and do the first `c-decl-prefix-or-start-re'
5647 ;; search unless we're at bob.
5649 (let (start-in-literal start-in-macro syntactic-pos)
5650 ;; Must back up a bit since we look for the end of the previous
5651 ;; statement or declaration, which is earlier than the first
5652 ;; returned match.
5654 ;; This `cond' moves back over any literals or macros. It has special
5655 ;; handling for when the region being searched is entirely within a
5656 ;; macro. It sets `cfd-continue-pos' (unless we've reached
5657 ;; `cfd-limit').
5658 (cond
5659 ;; First we need to move to a syntactically relevant position.
5660 ;; Begin by backing out of comment or string literals.
5662 ;; This arm of the cond actually triggers if we're in a literal,
5663 ;; and cfd-limit is at most at BONL.
5664 ((and
5665 ;; This arm of the `and' moves backwards out of a literal when
5666 ;; the face at point is a literal face. In this case, its value
5667 ;; is always non-nil.
5668 (when (c-got-face-at (point) c-literal-faces)
5669 ;; Try to use the faces to back up to the start of the
5670 ;; literal. FIXME: What if the point is on a declaration
5671 ;; inside a comment?
5672 (while (and (not (bobp))
5673 (c-got-face-at (1- (point)) c-literal-faces))
5674 (goto-char (previous-single-property-change
5675 (point) 'face nil (point-min))))
5677 ;; XEmacs doesn't fontify the quotes surrounding string
5678 ;; literals.
5679 (and (featurep 'xemacs)
5680 (eq (get-text-property (point) 'face)
5681 'font-lock-string-face)
5682 (not (bobp))
5683 (progn (backward-char)
5684 (not (looking-at c-string-limit-regexp)))
5685 (forward-char))
5687 ;; Don't trust the literal to contain only literal faces
5688 ;; (the font lock package might not have fontified the
5689 ;; start of it at all, for instance) so check that we have
5690 ;; arrived at something that looks like a start or else
5691 ;; resort to `c-literal-limits'.
5692 (unless (looking-at c-literal-start-regexp)
5693 (let ((lit-start (c-literal-start)))
5694 (if lit-start (goto-char lit-start)))
5697 (setq start-in-literal (point))) ; end of `and' arm.
5699 ;; The start is in a literal. If the limit is in the same
5700 ;; one we don't have to find a syntactic position etc. We
5701 ;; only check that if the limit is at or before bonl to save
5702 ;; time; it covers the by far most common case when font-lock
5703 ;; refontifies the current line only.
5704 (<= cfd-limit (c-point 'bonl cfd-start-pos))
5705 (save-excursion
5706 (goto-char cfd-start-pos)
5707 (while (progn
5708 (goto-char (c-next-single-property-change
5709 (point) 'face nil cfd-limit))
5710 (and (< (point) cfd-limit)
5711 (c-got-face-at (point) c-literal-faces))))
5712 (= (point) cfd-limit))) ; end of `cond' arm condition
5714 ;; Completely inside a literal. Set up variables to trig the
5715 ;; (< cfd-continue-pos cfd-start-pos) case below and it'll
5716 ;; find a suitable start position.
5717 (setq cfd-continue-pos start-in-literal)) ; end of `cond' arm
5719 ;; Check if the region might be completely inside a macro, to
5720 ;; optimize that like the completely-inside-literal above.
5721 ((save-excursion
5722 (and (= (forward-line 1) 0)
5723 (bolp) ; forward-line has funny behavior at eob.
5724 (>= (point) cfd-limit)
5725 (progn (backward-char)
5726 (eq (char-before) ?\\))))
5727 ;; (Maybe) completely inside a macro. Only need to trig the
5728 ;; (< cfd-continue-pos cfd-start-pos) case below to make it
5729 ;; set things up.
5730 (setq cfd-continue-pos (1- cfd-start-pos)
5731 start-in-macro t))
5733 ;; The default arm of the `cond' moves back over any macro we're in
5734 ;; and over any syntactic WS. It sets `c-find-decl-syntactic-pos'.
5736 ;; Back out of any macro so we don't miss any declaration
5737 ;; that could follow after it.
5738 (when (c-beginning-of-macro)
5739 (setq start-in-macro t))
5741 ;; Now we're at a proper syntactically relevant position so we
5742 ;; can use the cache. But first clear it if it applied
5743 ;; further down.
5744 (c-invalidate-find-decl-cache cfd-start-pos)
5746 (setq syntactic-pos (point))
5747 (unless (eq syntactic-pos c-find-decl-syntactic-pos)
5748 ;; Don't have to do this if the cache is relevant here,
5749 ;; typically if the same line is refontified again. If
5750 ;; we're just some syntactic whitespace further down we can
5751 ;; still use the cache to limit the skipping.
5752 (c-backward-syntactic-ws c-find-decl-syntactic-pos))
5754 ;; If we hit `c-find-decl-syntactic-pos' and
5755 ;; `c-find-decl-match-pos' is set then we install the cached
5756 ;; values. If we hit `c-find-decl-syntactic-pos' and
5757 ;; `c-find-decl-match-pos' is nil then we know there's no decl
5758 ;; prefix in the whitespace before `c-find-decl-syntactic-pos'
5759 ;; and so we can continue the search from this point. If we
5760 ;; didn't hit `c-find-decl-syntactic-pos' then we're now in
5761 ;; the right spot to begin searching anyway.
5762 (if (and (eq (point) c-find-decl-syntactic-pos)
5763 c-find-decl-match-pos)
5764 (setq cfd-match-pos c-find-decl-match-pos
5765 cfd-continue-pos syntactic-pos)
5767 (setq c-find-decl-syntactic-pos syntactic-pos)
5769 (when (if (bobp)
5770 ;; Always consider bob a match to get the first
5771 ;; declaration in the file. Do this separately instead of
5772 ;; letting `c-decl-prefix-or-start-re' match bob, so that
5773 ;; regexp always can consume at least one character to
5774 ;; ensure that we won't get stuck in an infinite loop.
5775 (setq cfd-re-match 0)
5776 (backward-char)
5777 (c-beginning-of-current-token)
5778 (< (point) cfd-limit))
5779 ;; Do an initial search now. In the bob case above it's
5780 ;; only done to search for a `c-decl-end' spot.
5781 (c-find-decl-prefix-search)) ; sets cfd-continue-pos
5783 (setq c-find-decl-match-pos (and (< cfd-match-pos cfd-start-pos)
5784 cfd-match-pos))))) ; end of `cond'
5786 ;; Advance `cfd-continue-pos' if it's before the start position.
5787 ;; The closest continue position that might have effect at or
5788 ;; after the start depends on what we started in. This also
5789 ;; finds a suitable start position in the special cases when the
5790 ;; region is completely within a literal or macro.
5791 (when (and cfd-continue-pos (< cfd-continue-pos cfd-start-pos))
5793 (cond
5794 (start-in-macro
5795 ;; If we're in a macro then it's the closest preceding token
5796 ;; in the macro. Check this before `start-in-literal',
5797 ;; since if we're inside a literal in a macro, the preceding
5798 ;; token is earlier than any `c-decl-end' spot inside the
5799 ;; literal (comment).
5800 (goto-char (or start-in-literal cfd-start-pos))
5801 ;; The only syntactic ws in macros are comments.
5802 (c-backward-comments)
5803 (backward-char)
5804 (c-beginning-of-current-token))
5806 (start-in-literal
5807 ;; If we're in a comment it can only be the closest
5808 ;; preceding `c-decl-end' position within that comment, if
5809 ;; any. Go back to the beginning of such a property so that
5810 ;; `c-find-decl-prefix-search' will find the end of it.
5811 ;; (Can't stop at the end and install it directly on
5812 ;; `cfd-prop-match' since that variable might be cleared
5813 ;; after `cfd-fun' below.)
5815 ;; Note that if the literal is a string then the property
5816 ;; search will simply skip to the beginning of it right
5817 ;; away.
5818 (if (not c-type-decl-end-used)
5819 (goto-char start-in-literal)
5820 (goto-char cfd-start-pos)
5821 (while (progn
5822 (goto-char (previous-single-property-change
5823 (point) 'c-type nil start-in-literal))
5824 (and (> (point) start-in-literal)
5825 (not (eq (c-get-char-property (point) 'c-type)
5826 'c-decl-end))))))
5828 (when (= (point) start-in-literal)
5829 ;; Didn't find any property inside the comment, so we can
5830 ;; skip it entirely. (This won't skip past a string, but
5831 ;; that'll be handled quickly by the next
5832 ;; `c-find-decl-prefix-search' anyway.)
5833 (c-forward-single-comment)
5834 (if (> (point) cfd-limit)
5835 (goto-char cfd-limit))))
5838 ;; If we started in normal code, the only match that might
5839 ;; apply before the start is what we already got in
5840 ;; `cfd-match-pos' so we can continue at the start position.
5841 ;; (Note that we don't get here if the first match is below
5842 ;; it.)
5843 (goto-char cfd-start-pos))) ; end of `cond'
5845 ;; Delete found matches if they are before our new continue
5846 ;; position, so that `c-find-decl-prefix-search' won't back up
5847 ;; to them later on.
5848 (setq cfd-continue-pos (point))
5849 (when (and cfd-re-match (< cfd-re-match cfd-continue-pos))
5850 (setq cfd-re-match nil))
5851 (when (and cfd-prop-match (< cfd-prop-match cfd-continue-pos))
5852 (setq cfd-prop-match nil))) ; end of `when'
5854 (if syntactic-pos
5855 ;; This is the normal case and we got a proper syntactic
5856 ;; position. If there's a match then it's always outside
5857 ;; macros and comments, so advance to the next token and set
5858 ;; `cfd-token-pos'. The loop below will later go back using
5859 ;; `cfd-continue-pos' to fix declarations inside the
5860 ;; syntactic ws.
5861 (when (and cfd-match-pos (< cfd-match-pos syntactic-pos))
5862 (goto-char syntactic-pos)
5863 (c-forward-syntactic-ws)
5864 (and cfd-continue-pos
5865 (< cfd-continue-pos (point))
5866 (setq cfd-token-pos (point))))
5868 ;; Have one of the special cases when the region is completely
5869 ;; within a literal or macro. `cfd-continue-pos' is set to a
5870 ;; good start position for the search, so do it.
5871 (c-find-decl-prefix-search)))
5873 ;; Now loop, one decl spot per iteration. We already have the first
5874 ;; match in `cfd-match-pos'.
5875 (while (progn
5876 ;; Go forward over "false matches", one per iteration.
5877 (while (and
5878 (< cfd-match-pos cfd-limit)
5881 ;; Kludge to filter out matches on the "<" that
5882 ;; aren't open parens, for the sake of languages
5883 ;; that got `c-recognize-<>-arglists' set.
5884 (and (eq (char-before cfd-match-pos) ?<)
5885 (not (c-get-char-property (1- cfd-match-pos)
5886 'syntax-table)))
5888 ;; If `cfd-continue-pos' is less or equal to
5889 ;; `cfd-token-pos', we've got a hit inside a macro
5890 ;; that's in the syntactic whitespace before the last
5891 ;; "real" declaration we've checked. If they're equal
5892 ;; we've arrived at the declaration a second time, so
5893 ;; there's nothing to do.
5894 (= cfd-continue-pos cfd-token-pos)
5896 (progn
5897 ;; If `cfd-continue-pos' is less than `cfd-token-pos'
5898 ;; we're still searching for declarations embedded in
5899 ;; the syntactic whitespace. In that case we need
5900 ;; only to skip comments and not macros, since they
5901 ;; can't be nested, and that's already been done in
5902 ;; `c-find-decl-prefix-search'.
5903 (when (> cfd-continue-pos cfd-token-pos)
5904 (c-forward-syntactic-ws)
5905 (setq cfd-token-pos (point)))
5907 ;; Continue if the following token fails the
5908 ;; CFD-DECL-RE and CFD-FACE-CHECKLIST checks.
5909 (when (or (>= (point) cfd-limit)
5910 (not (looking-at cfd-decl-re))
5911 (and cfd-face-checklist
5912 (not (c-got-face-at
5913 (point) cfd-face-checklist))))
5914 (goto-char cfd-continue-pos)
5915 t)))
5917 (< (point) cfd-limit)) ; end of "false matches" condition
5918 (c-find-decl-prefix-search)) ; end of "false matches" loop
5920 (< (point) cfd-limit)) ; end of condition for "decl-spot" while
5922 (when (and
5923 (>= (point) cfd-start-pos)
5925 (progn
5926 ;; Narrow to the end of the macro if we got a hit inside
5927 ;; one, to avoid recognizing things that start inside the
5928 ;; macro and end outside it.
5929 (when (> cfd-match-pos cfd-macro-end)
5930 ;; Not in the same macro as in the previous round.
5931 (save-excursion
5932 (goto-char cfd-match-pos)
5933 (setq cfd-macro-end
5934 (if (save-excursion (and (c-beginning-of-macro)
5935 (< (point) cfd-match-pos)))
5936 (progn (c-end-of-macro)
5937 (point))
5938 0))))
5940 (if (zerop cfd-macro-end)
5942 (if (> cfd-macro-end (point))
5943 (progn (narrow-to-region (point-min) cfd-macro-end)
5945 ;; The matched token was the last thing in the macro,
5946 ;; so the whole match is bogus.
5947 (setq cfd-macro-end 0)
5948 nil)))) ; end of when condition
5950 (c-debug-put-decl-spot-faces cfd-match-pos (point))
5951 (if (funcall cfd-fun cfd-match-pos (/= cfd-macro-end 0) cfd-top-level)
5952 (setq cfd-prop-match nil))
5954 (when (/= cfd-macro-end 0)
5955 ;; Restore limits if we did macro narrowing above.
5956 (narrow-to-region (point-min) cfd-buffer-end)))
5958 (goto-char cfd-continue-pos)
5959 (if (= cfd-continue-pos cfd-limit)
5960 (setq cfd-match-pos cfd-limit)
5961 (c-find-decl-prefix-search))))) ; Moves point, sets cfd-continue-pos,
5962 ; cfd-match-pos, etc.
5965 ;; A cache for found types.
5967 ;; Buffer local variable that contains an obarray with the types we've
5968 ;; found. If a declaration is recognized somewhere we record the
5969 ;; fully qualified identifier in it to recognize it as a type
5970 ;; elsewhere in the file too. This is not accurate since we do not
5971 ;; bother with the scoping rules of the languages, but in practice the
5972 ;; same name is seldom used as both a type and something else in a
5973 ;; file, and we only use this as a last resort in ambiguous cases (see
5974 ;; `c-forward-decl-or-cast-1').
5976 ;; Not every type need be in this cache. However, things which have
5977 ;; ceased to be types must be removed from it.
5979 ;; Template types in C++ are added here too but with the template
5980 ;; arglist replaced with "<>" in references or "<" for the one in the
5981 ;; primary type. E.g. the type "Foo<A,B>::Bar<C>" is stored as
5982 ;; "Foo<>::Bar<". This avoids storing very long strings (since C++
5983 ;; template specs can be fairly sized programs in themselves) and
5984 ;; improves the hit ratio (it's a type regardless of the template
5985 ;; args; it's just not the same type, but we're only interested in
5986 ;; recognizing types, not telling distinct types apart). Note that
5987 ;; template types in references are added here too; from the example
5988 ;; above there will also be an entry "Foo<".
5989 (defvar c-found-types nil)
5990 (make-variable-buffer-local 'c-found-types)
5992 (defsubst c-clear-found-types ()
5993 ;; Clears `c-found-types'.
5994 (setq c-found-types (make-vector 53 0)))
5996 (defun c-add-type (from to)
5997 ;; Add the given region as a type in `c-found-types'. If the region
5998 ;; doesn't match an existing type but there is a type which is equal
5999 ;; to the given one except that the last character is missing, then
6000 ;; the shorter type is removed. That's done to avoid adding all
6001 ;; prefixes of a type as it's being entered and font locked. This
6002 ;; doesn't cover cases like when characters are removed from a type
6003 ;; or added in the middle. We'd need the position of point when the
6004 ;; font locking is invoked to solve this well.
6006 ;; This function might do hidden buffer changes.
6007 (let ((type (c-syntactic-content from to c-recognize-<>-arglists)))
6008 (unless (intern-soft type c-found-types)
6009 (unintern (substring type 0 -1) c-found-types)
6010 (intern type c-found-types))))
6012 (defun c-unfind-type (name)
6013 ;; Remove the "NAME" from c-found-types, if present.
6014 (unintern name c-found-types))
6016 (defsubst c-check-type (from to)
6017 ;; Return non-nil if the given region contains a type in
6018 ;; `c-found-types'.
6020 ;; This function might do hidden buffer changes.
6021 (intern-soft (c-syntactic-content from to c-recognize-<>-arglists)
6022 c-found-types))
6024 (defun c-list-found-types ()
6025 ;; Return all the types in `c-found-types' as a sorted list of
6026 ;; strings.
6027 (let (type-list)
6028 (mapatoms (lambda (type)
6029 (setq type-list (cons (symbol-name type)
6030 type-list)))
6031 c-found-types)
6032 (sort type-list 'string-lessp)))
6034 ;; Shut up the byte compiler.
6035 (defvar c-maybe-stale-found-type)
6037 (defun c-trim-found-types (beg end old-len)
6038 ;; An after change function which, in conjunction with the info in
6039 ;; c-maybe-stale-found-type (set in c-before-change), removes a type
6040 ;; from `c-found-types', should this type have become stale. For
6041 ;; example, this happens to "foo" when "foo \n bar();" becomes
6042 ;; "foo(); \n bar();". Such stale types, if not removed, foul up
6043 ;; the fontification.
6045 ;; Have we, perhaps, added non-ws characters to the front/back of a found
6046 ;; type?
6047 (when (> end beg)
6048 (save-excursion
6049 (when (< end (point-max))
6050 (goto-char end)
6051 (if (and (c-beginning-of-current-token) ; only moves when we started in the middle
6052 (progn (goto-char end)
6053 (c-end-of-current-token)))
6054 (c-unfind-type (buffer-substring-no-properties
6055 end (point)))))
6056 (when (> beg (point-min))
6057 (goto-char beg)
6058 (if (and (c-end-of-current-token) ; only moves when we started in the middle
6059 (progn (goto-char beg)
6060 (c-beginning-of-current-token)))
6061 (c-unfind-type (buffer-substring-no-properties
6062 (point) beg))))))
6064 (if c-maybe-stale-found-type ; e.g. (c-decl-id-start "foo" 97 107 " (* ooka) " "o")
6065 (cond
6066 ;; Changing the amount of (already existing) whitespace - don't do anything.
6067 ((and (c-partial-ws-p beg end)
6068 (or (= beg end) ; removal of WS
6069 (string-match "^[ \t\n\r\f\v]*$" (nth 5 c-maybe-stale-found-type)))))
6071 ;; The syntactic relationship which defined a "found type" has been
6072 ;; destroyed.
6073 ((eq (car c-maybe-stale-found-type) 'c-decl-id-start)
6074 (c-unfind-type (cadr c-maybe-stale-found-type)))
6075 ;; ((eq (car c-maybe-stale-found-type) 'c-decl-type-start) FIXME!!!
6079 ;; Setting and removing syntax properties on < and > in languages (C++
6080 ;; and Java) where they can be template/generic delimiters as well as
6081 ;; their normal meaning of "less/greater than".
6083 ;; Normally, < and > have syntax 'punctuation'. When they are found to
6084 ;; be delimiters, they are marked as such with the category properties
6085 ;; c-<-as-paren-syntax, c->-as-paren-syntax respectively.
6087 ;; STRATEGY:
6089 ;; It is impossible to determine with certainty whether a <..> pair in
6090 ;; C++ is two comparison operators or is template delimiters, unless
6091 ;; one duplicates a lot of a C++ compiler. For example, the following
6092 ;; code fragment:
6094 ;; foo (a < b, c > d) ;
6096 ;; could be a function call with two integer parameters (each a
6097 ;; relational expression), or it could be a constructor for class foo
6098 ;; taking one parameter d of templated type "a < b, c >". They are
6099 ;; somewhat easier to distinguish in Java.
6101 ;; The strategy now (2010-01) adopted is to mark and unmark < and
6102 ;; > IN MATCHING PAIRS ONLY. [Previously, they were marked
6103 ;; individually when their context so indicated. This gave rise to
6104 ;; intractable problems when one of a matching pair was deleted, or
6105 ;; pulled into a literal.]
6107 ;; At each buffer change, the syntax-table properties are removed in a
6108 ;; before-change function and reapplied, when needed, in an
6109 ;; after-change function. It is far more important that the
6110 ;; properties get removed when they they are spurious than that they
6111 ;; be present when wanted.
6112 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
6113 (defun c-clear-<-pair-props (&optional pos)
6114 ;; POS (default point) is at a < character. If it is marked with
6115 ;; open paren syntax-table text property, remove the property,
6116 ;; together with the close paren property on the matching > (if
6117 ;; any).
6118 (save-excursion
6119 (if pos
6120 (goto-char pos)
6121 (setq pos (point)))
6122 (when (equal (c-get-char-property (point) 'syntax-table)
6123 c-<-as-paren-syntax)
6124 (with-syntax-table c-no-parens-syntax-table ; ignore unbalanced [,{,(,..
6125 (c-go-list-forward))
6126 (when (equal (c-get-char-property (1- (point)) 'syntax-table)
6127 c->-as-paren-syntax) ; should always be true.
6128 (c-unmark-<->-as-paren (1- (point))))
6129 (c-unmark-<->-as-paren pos))))
6131 (defun c-clear->-pair-props (&optional pos)
6132 ;; POS (default point) is at a > character. If it is marked with
6133 ;; close paren syntax-table property, remove the property, together
6134 ;; with the open paren property on the matching < (if any).
6135 (save-excursion
6136 (if pos
6137 (goto-char pos)
6138 (setq pos (point)))
6139 (when (equal (c-get-char-property (point) 'syntax-table)
6140 c->-as-paren-syntax)
6141 (with-syntax-table c-no-parens-syntax-table ; ignore unbalanced [,{,(,..
6142 (c-go-up-list-backward))
6143 (when (equal (c-get-char-property (point) 'syntax-table)
6144 c-<-as-paren-syntax) ; should always be true.
6145 (c-unmark-<->-as-paren (point)))
6146 (c-unmark-<->-as-paren pos))))
6148 (defun c-clear-<>-pair-props (&optional pos)
6149 ;; POS (default point) is at a < or > character. If it has an
6150 ;; open/close paren syntax-table property, remove this property both
6151 ;; from the current character and its partner (which will also be
6152 ;; thusly marked).
6153 (cond
6154 ((eq (char-after) ?\<)
6155 (c-clear-<-pair-props pos))
6156 ((eq (char-after) ?\>)
6157 (c-clear->-pair-props pos))
6158 (t (c-benign-error
6159 "c-clear-<>-pair-props called from wrong position"))))
6161 (defun c-clear-<-pair-props-if-match-after (lim &optional pos)
6162 ;; POS (default point) is at a < character. If it is both marked
6163 ;; with open/close paren syntax-table property, and has a matching >
6164 ;; (also marked) which is after LIM, remove the property both from
6165 ;; the current > and its partner. Return t when this happens, nil
6166 ;; when it doesn't.
6167 (save-excursion
6168 (if pos
6169 (goto-char pos)
6170 (setq pos (point)))
6171 (when (equal (c-get-char-property (point) 'syntax-table)
6172 c-<-as-paren-syntax)
6173 (with-syntax-table c-no-parens-syntax-table ; ignore unbalanced [,{,(,..
6174 (c-go-list-forward))
6175 (when (and (>= (point) lim)
6176 (equal (c-get-char-property (1- (point)) 'syntax-table)
6177 c->-as-paren-syntax)) ; should always be true.
6178 (c-unmark-<->-as-paren (1- (point)))
6179 (c-unmark-<->-as-paren pos))
6180 t)))
6182 (defun c-clear->-pair-props-if-match-before (lim &optional pos)
6183 ;; POS (default point) is at a > character. If it is both marked
6184 ;; with open/close paren syntax-table property, and has a matching <
6185 ;; (also marked) which is before LIM, remove the property both from
6186 ;; the current < and its partner. Return t when this happens, nil
6187 ;; when it doesn't.
6188 (save-excursion
6189 (if pos
6190 (goto-char pos)
6191 (setq pos (point)))
6192 (when (equal (c-get-char-property (point) 'syntax-table)
6193 c->-as-paren-syntax)
6194 (with-syntax-table c-no-parens-syntax-table ; ignore unbalanced [,{,(,..
6195 (c-go-up-list-backward))
6196 (when (and (<= (point) lim)
6197 (equal (c-get-char-property (point) 'syntax-table)
6198 c-<-as-paren-syntax)) ; should always be true.
6199 (c-unmark-<->-as-paren (point))
6200 (c-unmark-<->-as-paren pos))
6201 t)))
6203 ;; Set by c-common-init in cc-mode.el.
6204 (defvar c-new-BEG)
6205 (defvar c-new-END)
6206 ;; Set by c-after-change in cc-mode.el.
6207 (defvar c-old-BEG)
6208 (defvar c-old-END)
6210 (defun c-before-change-check-<>-operators (beg end)
6211 ;; Unmark certain pairs of "< .... >" which are currently marked as
6212 ;; template/generic delimiters. (This marking is via syntax-table text
6213 ;; properties), and expand the (c-new-BEG c-new-END) region to include all
6214 ;; unmarked < and > operators within the certain bounds (see below).
6216 ;; These pairs are those which are in the current "statement" (i.e.,
6217 ;; the region between the {, }, or ; before BEG and the one after
6218 ;; END), and which enclose any part of the interval (BEG END).
6220 ;; Note that in C++ (?and Java), template/generic parens cannot
6221 ;; enclose a brace or semicolon, so we use these as bounds on the
6222 ;; region we must work on.
6224 ;; This function is called from before-change-functions (via
6225 ;; c-get-state-before-change-functions). Thus the buffer is widened,
6226 ;; and point is undefined, both at entry and exit.
6228 ;; FIXME!!! This routine ignores the possibility of macros entirely.
6229 ;; 2010-01-29.
6230 (save-excursion
6231 (c-save-buffer-state
6232 ((beg-lit-start (progn (goto-char beg) (c-literal-start)))
6233 (end-lit-limits (progn (goto-char end) (c-literal-limits)))
6234 new-beg new-end beg-limit end-limit)
6235 ;; Locate the earliest < after the barrier before the changed region,
6236 ;; which isn't already marked as a paren.
6237 (goto-char (or beg-lit-start beg))
6238 (setq beg-limit (c-determine-limit 512))
6240 ;; Remove the syntax-table/category properties from each pertinent <...>
6241 ;; pair. Firstly, the ones with the < before beg and > after beg....
6242 (while (progn (c-syntactic-skip-backward "^;{}<" beg-limit)
6243 (eq (char-before) ?<))
6244 (c-backward-token-2)
6245 (when (eq (char-after) ?<)
6246 (c-clear-<-pair-props-if-match-after beg)))
6247 (c-forward-syntactic-ws)
6248 (setq new-beg (point))
6250 ;; ...Then the ones with < before end and > after end.
6251 (goto-char (if end-lit-limits (cdr end-lit-limits) end))
6252 (setq end-limit (c-determine-+ve-limit 512))
6253 (while (and (c-syntactic-re-search-forward "[;{}>]" end-limit 'end)
6254 (eq (char-before) ?>))
6255 (c-end-of-current-token)
6256 (when (eq (char-before) ?>)
6257 (c-clear->-pair-props-if-match-before end (1- (point)))))
6258 (c-backward-syntactic-ws)
6259 (setq new-end (point))
6261 ;; Extend the fontification region, if needed.
6262 (and new-beg
6263 (< new-beg c-new-BEG)
6264 (setq c-new-BEG new-beg))
6265 (and new-end
6266 (> new-end c-new-END)
6267 (setq c-new-END new-end)))))
6269 (defun c-after-change-check-<>-operators (beg end)
6270 ;; This is called from `after-change-functions' when
6271 ;; c-recognize-<>-arglists' is set. It ensures that no "<" or ">"
6272 ;; chars with paren syntax become part of another operator like "<<"
6273 ;; or ">=".
6275 ;; This function might do hidden buffer changes.
6277 (save-excursion
6278 (goto-char beg)
6279 (when (or (looking-at "[<>]")
6280 (< (skip-chars-backward "<>") 0))
6282 (goto-char beg)
6283 (c-beginning-of-current-token)
6284 (when (and (< (point) beg)
6285 (looking-at c-<>-multichar-token-regexp)
6286 (< beg (setq beg (match-end 0))))
6287 (while (progn (skip-chars-forward "^<>" beg)
6288 (< (point) beg))
6289 (c-clear-<>-pair-props)
6290 (forward-char))))
6292 (when (< beg end)
6293 (goto-char end)
6294 (when (or (looking-at "[<>]")
6295 (< (skip-chars-backward "<>") 0))
6297 (goto-char end)
6298 (c-beginning-of-current-token)
6299 (when (and (< (point) end)
6300 (looking-at c-<>-multichar-token-regexp)
6301 (< end (setq end (match-end 0))))
6302 (while (progn (skip-chars-forward "^<>" end)
6303 (< (point) end))
6304 (c-clear-<>-pair-props)
6305 (forward-char)))))))
6307 (defun c-restore-<>-properties (_beg _end _old-len)
6308 ;; This function is called as an after-change function. It restores the
6309 ;; category/syntax-table properties on template/generic <..> pairs between
6310 ;; c-new-BEG and c-new-END. It may do hidden buffer changes.
6311 (c-save-buffer-state ((c-parse-and-markup-<>-arglists t)
6312 c-restricted-<>-arglists lit-limits)
6313 (goto-char c-new-BEG)
6314 (if (setq lit-limits (c-literal-limits))
6315 (goto-char (cdr lit-limits)))
6316 (while (and (< (point) c-new-END)
6317 (c-syntactic-re-search-forward "<" c-new-END 'bound))
6318 (backward-char)
6319 (save-excursion
6320 (c-backward-token-2)
6321 (setq c-restricted-<>-arglists
6322 (and (not (looking-at c-opt-<>-sexp-key))
6323 (progn (c-backward-syntactic-ws) ; to ( or ,
6324 (and (memq (char-before) '(?\( ?,)) ; what about <?
6325 (not (eq (c-get-char-property (point) 'c-type)
6326 'c-decl-arg-start)))))))
6327 (or (c-forward-<>-arglist nil)
6328 (forward-char)))))
6331 ;; Functions to handle C++ raw strings.
6333 ;; A valid C++ raw string looks like
6334 ;; R"<id>(<contents>)<id>"
6335 ;; , where <id> is an identifier from 0 to 16 characters long, not containing
6336 ;; spaces, control characters, double quote or left/right paren. <contents>
6337 ;; can include anything which isn't the terminating )<id>", including new
6338 ;; lines, "s, parentheses, etc.
6340 ;; CC Mode handles C++ raw strings by the use of `syntax-table' text
6341 ;; properties as follows:
6343 ;; (i) On a validly terminated raw string, no `syntax-table' text properties
6344 ;; are applied to the opening and closing delimiters, but any " in the
6345 ;; contents is given the property value "punctuation" (`(1)') to prevent it
6346 ;; interacting with the "s in the delimiters.
6348 ;; The font locking routine `c-font-lock-c++-raw-strings' (in cc-fonts.el)
6349 ;; recognizes valid raw strings, and fontifies the delimiters (apart from
6350 ;; the parentheses) with the default face and the parentheses and the
6351 ;; <contents> with font-lock-string-face.
6353 ;; (ii) A valid, but unterminated, raw string opening delimiter gets the
6354 ;; "punctuation" value (`(1)') of the `syntax-table' text property, and the
6355 ;; open parenthesis gets the "string fence" value (`(15)').
6357 ;; `c-font-lock-c++-raw-strings' puts c-font-lock-warning-face on the entire
6358 ;; unmatched opening delimiter (from the R up to the open paren), and allows
6359 ;; the rest of the buffer to get font-lock-string-face, caused by the
6360 ;; unmatched "string fence" `syntax-table' text property value.
6362 ;; (iii) Inside a macro, a valid raw string is handled as in (i). An
6363 ;; unmatched opening delimiter is handled slightly differently. In addition
6364 ;; to the "punctuation" and "string fence" properties on the delimiter,
6365 ;; another "string fence" `syntax-table' property is applied to the last
6366 ;; possible character of the macro before the terminating linefeed (if there
6367 ;; is such a character after the "("). This "last possible" character is
6368 ;; never a backslash escaping the end of line. If the character preceding
6369 ;; this "last possible" character is itself a backslash, this preceding
6370 ;; character gets a "punctuation" `syntax-table' value. If the "(" is
6371 ;; already at the end of the macro, it gets the "punctuation" value, and no
6372 ;; "string fence"s are used.
6374 ;; The effect on the fontification of either of these tactics is that rest of
6375 ;; the macro (if any) after the "(" gets font-lock-string-face, but the rest
6376 ;; of the file is fontified normally.
6379 (defun c-raw-string-pos ()
6380 ;; Get POINT's relationship to any containing raw string.
6381 ;; If point isn't in a raw string, return nil.
6382 ;; Otherwise, return the following list:
6384 ;; (POS B\" B\( E\) E\")
6386 ;; , where POS is the symbol `open-delim' if point is in the opening
6387 ;; delimiter, the symbol `close-delim' if it's in the closing delimiter, and
6388 ;; nil if it's in the string body. B\", B\(, E\), E\" are the positions of
6389 ;; the opening and closing quotes and parentheses of a correctly terminated
6390 ;; raw string. (N.B.: E\) and E\" are NOT on the "outside" of these
6391 ;; characters.) If the raw string is not terminated, E\) and E\" are set to
6392 ;; nil.
6394 ;; Note: this routine is dependant upon the correct syntax-table text
6395 ;; properties being set.
6396 (let ((state (c-state-semi-pp-to-literal (point)))
6397 open-quote-pos open-paren-pos close-paren-pos close-quote-pos id)
6398 (save-excursion
6399 (when
6400 (and
6401 (cond
6402 ((null (cadr state))
6403 (or (eq (char-after) ?\")
6404 (search-backward "\"" (max (- (point) 17) (point-min)) t)))
6405 ((and (eq (cadr state) 'string)
6406 (goto-char (nth 2 state))
6407 (or (eq (char-after) ?\")
6408 (search-backward "\"" (max (- (point) 17) (point-min)) t))
6409 (not (bobp)))))
6410 (eq (char-before) ?R)
6411 (looking-at "\"\\([^ ()\\\n\r\t]\\{0,16\\}\\)("))
6412 (setq open-quote-pos (point)
6413 open-paren-pos (match-end 1)
6414 id (match-string-no-properties 1))
6415 (goto-char (1+ open-paren-pos))
6416 (when (and (not (c-get-char-property open-paren-pos 'syntax-table))
6417 (search-forward (concat ")" id "\"") nil t))
6418 (setq close-paren-pos (match-beginning 0)
6419 close-quote-pos (1- (point))))))
6420 (and open-quote-pos
6421 (list
6422 (cond
6423 ((<= (point) open-paren-pos)
6424 'open-delim)
6425 ((and close-paren-pos
6426 (> (point) close-paren-pos))
6427 'close-delim)
6428 (t nil))
6429 open-quote-pos open-paren-pos close-paren-pos close-quote-pos))))
6431 (defun c-depropertize-raw-string (id open-quote open-paren bound)
6432 ;; Point is immediately after a raw string opening delimiter. Remove any
6433 ;; `syntax-table' text properties associated with the delimiter (if it's
6434 ;; unmatched) or the raw string.
6436 ;; ID, a string, is the delimiter's identifier. OPEN-QUOTE and OPEN-PAREN
6437 ;; are the buffer positions of the delimiter's components. BOUND is the
6438 ;; bound for searching for a matching closing delimiter; it is usually nil,
6439 ;; but if we're inside a macro, it's the end of the macro.
6441 ;; Point is moved to after the (terminated) raw string, or left after the
6442 ;; unmatched opening delimiter, as the case may be. The return value is of
6443 ;; no significance.
6444 (let ((open-paren-prop (c-get-char-property open-paren 'syntax-table)))
6445 (cond
6446 ((null open-paren-prop)
6447 ;; A terminated raw string
6448 (when (search-forward (concat ")" id "\"") nil t)
6449 (let* ((closing-paren (match-beginning 0))
6450 (first-punctuation
6451 (save-match-data
6452 (goto-char (1+ open-paren))
6453 (and (c-search-forward-char-property 'syntax-table '(1)
6454 closing-paren)
6455 (1- (point)))))
6457 (when first-punctuation
6458 (c-clear-char-property-with-value
6459 first-punctuation (match-beginning 0) 'syntax-table '(1))
6460 (c-truncate-semi-nonlit-pos-cache first-punctuation)
6461 ))))
6462 ((or (and (equal open-paren-prop '(15)) (null bound))
6463 (equal open-paren-prop '(1)))
6464 ;; An unterminated raw string either not in a macro, or in a macro with
6465 ;; the open parenthesis right up against the end of macro
6466 (c-clear-char-property open-quote 'syntax-table)
6467 (c-truncate-semi-nonlit-pos-cache open-quote)
6468 (c-clear-char-property open-paren 'syntax-table))
6470 ;; An unterminated string in a macro, with at least one char after the
6471 ;; open paren
6472 (c-clear-char-property open-quote 'syntax-table)
6473 (c-truncate-semi-nonlit-pos-cache open-quote)
6474 (c-clear-char-property open-paren 'syntax-table)
6475 (let ((after-string-fence-pos
6476 (save-excursion
6477 (goto-char (1+ open-paren))
6478 (c-search-forward-char-property 'syntax-table '(15) bound))))
6479 (when after-string-fence-pos
6480 (c-clear-char-property (1- after-string-fence-pos) 'syntax-table)))
6481 ))))
6483 (defun c-depropertize-raw-strings-in-region (start finish)
6484 ;; Remove any `syntax-table' text properties associated with C++ raw strings
6485 ;; contained in the region (START FINISH). Point is undefined at entry and
6486 ;; exit, and the return value has no significance.
6487 (goto-char start)
6488 (while (and (< (point) finish)
6489 (re-search-forward
6490 (concat "\\(" ; 1
6491 c-anchored-cpp-prefix ; 2
6492 "\\)\\|\\(" ; 3
6493 "R\"\\([^ ()\\\n\r\t]\\{0,16\\}\\)(" ; 4
6494 "\\)")
6495 finish t))
6496 (when (save-excursion
6497 (goto-char (match-beginning 0)) (not (c-in-literal)))
6498 (if (match-beginning 4) ; the id
6499 ;; We've found a raw string
6500 (c-depropertize-raw-string
6501 (match-string-no-properties 4) ; id
6502 (1+ (match-beginning 3)) ; open quote
6503 (match-end 4) ; open paren
6504 nil) ; bound
6505 ;; We've found a CPP construct. Search for raw strings within it.
6506 (goto-char (match-beginning 2)) ; the "#"
6507 (c-end-of-macro)
6508 (let ((eom (point)))
6509 (goto-char (match-end 2)) ; after the "#".
6510 (while (and (< (point) eom)
6511 (c-syntactic-re-search-forward
6512 "R\"\\([^ ()\\\n\r\t]\\{0,16\\}\\)(" eom t))
6513 (c-depropertize-raw-string
6514 (match-string-no-properties 1) ; id
6515 (1+ (match-beginning 0)) ; open quote
6516 (match-end 1) ; open paren
6517 eom))))))) ; bound.
6519 (defun c-before-change-check-raw-strings (beg end)
6520 ;; This function clears `syntax-table' text properties from C++ raw strings
6521 ;; in the region (c-new-BEG c-new-END). BEG and END are the standard
6522 ;; arguments supplied to any before-change function.
6524 ;; Point is undefined on both entry and exit, and the return value has no
6525 ;; significance.
6527 ;; This function is called as a before-change function solely due to its
6528 ;; membership of the C++ value of `c-get-state-before-change-functions'.
6529 (c-save-buffer-state
6530 ((beg-rs (progn (goto-char beg) (c-raw-string-pos)))
6531 (beg-plus (if (null beg-rs)
6533 (max beg
6534 (1+ (or (nth 4 beg-rs) (nth 2 beg-rs))))))
6535 (end-rs (progn (goto-char end) (c-raw-string-pos))) ; FIXME!!!
6536 ; Optimize this so that we don't call
6537 ; `c-raw-string-pos' twice when once
6538 ; will do. (2016-06-02).
6539 (end-minus (if (null end-rs)
6541 (min end (cadr end-rs))))
6543 (when beg-rs
6544 (setq c-new-BEG (min c-new-BEG (1- (cadr beg-rs)))))
6545 (c-depropertize-raw-strings-in-region c-new-BEG beg-plus)
6547 (when end-rs
6548 (setq c-new-END (max c-new-END
6549 (1+ (or (nth 4 end-rs)
6550 (nth 2 end-rs))))))
6551 (c-depropertize-raw-strings-in-region end-minus c-new-END)))
6553 (defun c-propertize-raw-string-opener (id open-quote open-paren bound)
6554 ;; Point is immediately after a raw string opening delimiter. Apply any
6555 ;; pertinent `syntax-table' text properties to the delimiter and also the
6556 ;; raw string, should there be a valid matching closing delimiter.
6558 ;; ID, a string, is the delimiter's identifier. OPEN-QUOTE and OPEN-PAREN
6559 ;; are the buffer positions of the delimiter's components. BOUND is the
6560 ;; bound for searching for a matching closing delimiter; it is usually nil,
6561 ;; but if we're inside a macro, it's the end of the macro.
6563 ;; Point is moved to after the (terminated) raw string, or left after the
6564 ;; unmatched opening delimiter, as the case may be. The return value is of
6565 ;; no significance.
6566 (if (search-forward (concat ")" id "\"") bound t)
6567 (let ((end-string (match-beginning 0))
6568 (after-quote (match-end 0)))
6569 (goto-char open-paren)
6570 (while (progn (skip-syntax-forward "^\"" end-string)
6571 (< (point) end-string))
6572 (c-put-char-property (point) 'syntax-table '(1)) ; punctuation
6573 (c-truncate-semi-nonlit-pos-cache (point))
6574 (forward-char))
6575 (goto-char after-quote))
6576 (c-put-char-property open-quote 'syntax-table '(1)) ; punctuation
6577 (c-truncate-semi-nonlit-pos-cache open-quote)
6578 (c-put-char-property open-paren 'syntax-table '(15)) ; generic string
6579 (when bound
6580 ;; In a CPP construct, we try to apply a generic-string `syntax-table'
6581 ;; text property to the last possible character in the string, so that
6582 ;; only characters within the macro get "stringed out".
6583 (goto-char bound)
6584 (if (save-restriction
6585 (narrow-to-region (1+ open-paren) (point-max))
6586 (re-search-backward
6587 (eval-when-compile
6588 ;; This regular expression matches either an escape pair (which
6589 ;; isn't an escaped NL) (submatch 5) or a non-escaped character
6590 ;; (which isn't itself a backslash) (submatch 10). The long
6591 ;; preambles to these (respectively submatches 2-4 and 6-9)
6592 ;; ensure that we have the correct parity for sequences of
6593 ;; backslashes, etc..
6594 (concat "\\(" ; 1
6595 "\\(\\`[^\\]?\\|[^\\][^\\]\\)\\(\\\\\\(.\\|\n\\)\\)*" ; 2-4
6596 "\\(\\\\.\\)" ; 5
6597 "\\|"
6598 "\\(\\`\\|[^\\]\\|\\(\\`[^\\]?\\|[^\\][^\\]\\)\\(\\\\\\(.\\|\n\\)\\)+\\)" ; 6-9
6599 "\\([^\\]\\)" ; 10
6600 "\\)"
6601 "\\(\\\\\n\\)*\\=")) ; 11
6602 (1+ open-paren) t))
6603 (if (match-beginning 10)
6604 (progn
6605 (c-put-char-property (match-beginning 10) 'syntax-table '(15))
6606 (c-truncate-semi-nonlit-pos-cache (match-beginning 10)))
6607 (c-put-char-property (match-beginning 5) 'syntax-table '(1))
6608 (c-put-char-property (1+ (match-beginning 5)) 'syntax-table '(15))
6609 (c-truncate-semi-nonlit-pos-cache (1+ (match-beginning 5))))
6610 (c-put-char-property open-paren 'syntax-table '(1)))
6611 (goto-char bound))))
6613 (defun c-after-change-re-mark-raw-strings (beg end old-len)
6614 ;; This function applies `syntax-table' text properties to C++ raw strings
6615 ;; beginning in the region (c-new-BEG c-new-END). BEG, END, and OLD-LEN are
6616 ;; the standard arguments supplied to any after-change function.
6618 ;; Point is undefined on both entry and exit, and the return value has no
6619 ;; significance.
6621 ;; This function is called as an after-change function solely due to its
6622 ;; membership of the C++ value of `c-before-font-lock-functions'.
6623 (c-save-buffer-state ()
6624 ;; If the region (c-new-BEG c-new-END) has expanded, remove
6625 ;; `syntax-table' text-properties from the new piece(s).
6626 (when (< c-new-BEG c-old-BEG)
6627 (let ((beg-rs (progn (goto-char c-old-BEG) (c-raw-string-pos))))
6628 (c-depropertize-raw-strings-in-region
6629 c-new-BEG
6630 (if beg-rs
6631 (1+ (or (nth 4 beg-rs) (nth 2 beg-rs)))
6632 c-old-BEG))))
6633 (when (> c-new-END c-old-END)
6634 (let ((end-rs (progn (goto-char c-old-END) (c-raw-string-pos))))
6635 (c-depropertize-raw-strings-in-region
6636 (if end-rs
6637 (cadr end-rs)
6638 c-old-END)
6639 c-new-END)))
6641 (goto-char c-new-BEG)
6642 (while (and (< (point) c-new-END)
6643 (re-search-forward
6644 (concat "\\(" ; 1
6645 c-anchored-cpp-prefix ; 2
6646 "\\)\\|\\(" ; 3
6647 "R\"\\([^ ()\\\n\r\t]\\{0,16\\}\\)(" ; 4
6648 "\\)")
6649 c-new-END t))
6650 (when (save-excursion
6651 (goto-char (match-beginning 0)) (not (c-in-literal)))
6652 (if (match-beginning 4) ; the id
6653 ;; We've found a raw string.
6654 (c-propertize-raw-string-opener
6655 (match-string-no-properties 4) ; id
6656 (1+ (match-beginning 3)) ; open quote
6657 (match-end 4) ; open paren
6658 nil) ; bound
6659 ;; We've found a CPP construct. Search for raw strings within it.
6660 (goto-char (match-beginning 2)) ; the "#"
6661 (c-end-of-macro)
6662 (let ((eom (point)))
6663 (goto-char (match-end 2)) ; after the "#".
6664 (while (and (< (point) eom)
6665 (c-syntactic-re-search-forward
6666 "R\"\\([^ ()\\\n\r\t]\\{0,16\\}\\)(" eom t))
6667 (c-propertize-raw-string-opener
6668 (match-string-no-properties 1) ; id
6669 (1+ (match-beginning 0)) ; open quote
6670 (match-end 1) ; open paren
6671 eom)))))))) ; bound
6674 ;; Handling of small scale constructs like types and names.
6676 ;; Dynamically bound variable that instructs `c-forward-type' to also
6677 ;; treat possible types (i.e. those that it normally returns 'maybe or
6678 ;; 'found for) as actual types (and always return 'found for them).
6679 ;; This means that it records them in `c-record-type-identifiers' if
6680 ;; that is set, and that it adds them to `c-found-types'.
6681 (defvar c-promote-possible-types nil)
6683 ;; Dynamically bound variable that instructs `c-forward-<>-arglist' to
6684 ;; mark up successfully parsed arglists with paren syntax properties on
6685 ;; the surrounding angle brackets and with `c-<>-arg-sep' in the
6686 ;; `c-type' property of each argument separating comma.
6688 ;; Setting this variable also makes `c-forward-<>-arglist' recurse into
6689 ;; all arglists for side effects (i.e. recording types), otherwise it
6690 ;; exploits any existing paren syntax properties to quickly jump to the
6691 ;; end of already parsed arglists.
6693 ;; Marking up the arglists is not the default since doing that correctly
6694 ;; depends on a proper value for `c-restricted-<>-arglists'.
6695 (defvar c-parse-and-markup-<>-arglists nil)
6697 ;; Dynamically bound variable that instructs `c-forward-<>-arglist' to
6698 ;; not accept arglists that contain binary operators.
6700 ;; This is primarily used to handle C++ template arglists. C++
6701 ;; disambiguates them by checking whether the preceding name is a
6702 ;; template or not. We can't do that, so we assume it is a template
6703 ;; if it can be parsed as one. That usually works well since
6704 ;; comparison expressions on the forms "a < b > c" or "a < b, c > d"
6705 ;; in almost all cases would be pointless.
6707 ;; However, in function arglists, e.g. in "foo (a < b, c > d)", we
6708 ;; should let the comma separate the function arguments instead. And
6709 ;; in a context where the value of the expression is taken, e.g. in
6710 ;; "if (a < b || c > d)", it's probably not a template.
6711 (defvar c-restricted-<>-arglists nil)
6713 ;; Dynamically bound variables that instructs
6714 ;; `c-forward-keyword-clause', `c-forward-<>-arglist',
6715 ;; `c-forward-name', `c-forward-type', `c-forward-decl-or-cast-1', and
6716 ;; `c-forward-label' to record the ranges of all the type and
6717 ;; reference identifiers they encounter. They will build lists on
6718 ;; these variables where each element is a cons of the buffer
6719 ;; positions surrounding each identifier. This recording is only
6720 ;; activated when `c-record-type-identifiers' is non-nil.
6722 ;; All known types that can't be identifiers are recorded, and also
6723 ;; other possible types if `c-promote-possible-types' is set.
6724 ;; Recording is however disabled inside angle bracket arglists that
6725 ;; are encountered inside names and other angle bracket arglists.
6726 ;; Such occurrences are taken care of by `c-font-lock-<>-arglists'
6727 ;; instead.
6729 ;; Only the names in C++ template style references (e.g. "tmpl" in
6730 ;; "tmpl<a,b>::foo") are recorded as references, other references
6731 ;; aren't handled here.
6733 ;; `c-forward-label' records the label identifier(s) on
6734 ;; `c-record-ref-identifiers'.
6735 (defvar c-record-type-identifiers nil)
6736 (defvar c-record-ref-identifiers nil)
6738 ;; This variable will receive a cons cell of the range of the last
6739 ;; single identifier symbol stepped over by `c-forward-name' if it's
6740 ;; successful. This is the range that should be put on one of the
6741 ;; record lists above by the caller. It's assigned nil if there's no
6742 ;; such symbol in the name.
6743 (defvar c-last-identifier-range nil)
6745 (defmacro c-record-type-id (range)
6746 (if (eq (car-safe range) 'cons)
6747 ;; Always true.
6748 `(setq c-record-type-identifiers
6749 (cons ,range c-record-type-identifiers))
6750 `(let ((range ,range))
6751 (if range
6752 (setq c-record-type-identifiers
6753 (cons range c-record-type-identifiers))))))
6755 (defmacro c-record-ref-id (range)
6756 (if (eq (car-safe range) 'cons)
6757 ;; Always true.
6758 `(setq c-record-ref-identifiers
6759 (cons ,range c-record-ref-identifiers))
6760 `(let ((range ,range))
6761 (if range
6762 (setq c-record-ref-identifiers
6763 (cons range c-record-ref-identifiers))))))
6765 ;; Dynamically bound variable that instructs `c-forward-type' to
6766 ;; record the ranges of types that only are found. Behaves otherwise
6767 ;; like `c-record-type-identifiers'.
6768 (defvar c-record-found-types nil)
6770 (defmacro c-forward-keyword-prefixed-id (type)
6771 ;; Used internally in `c-forward-keyword-clause' to move forward
6772 ;; over a type (if TYPE is 'type) or a name (otherwise) which
6773 ;; possibly is prefixed by keywords and their associated clauses.
6774 ;; Try with a type/name first to not trip up on those that begin
6775 ;; with a keyword. Return t if a known or found type is moved
6776 ;; over. The point is clobbered if nil is returned. If range
6777 ;; recording is enabled, the identifier is recorded on as a type
6778 ;; if TYPE is 'type or as a reference if TYPE is 'ref.
6780 ;; This macro might do hidden buffer changes.
6781 `(let (res)
6782 (setq c-last-identifier-range nil)
6783 (while (if (setq res ,(if (eq type 'type)
6784 `(c-forward-type)
6785 `(c-forward-name)))
6787 (cond ((looking-at c-keywords-regexp)
6788 (c-forward-keyword-clause 1))
6789 ((and c-opt-cpp-prefix
6790 (looking-at c-noise-macro-with-parens-name-re))
6791 (c-forward-noise-clause)))))
6792 (when (memq res '(t known found prefix maybe))
6793 (when c-record-type-identifiers
6794 ,(if (eq type 'type)
6795 `(c-record-type-id c-last-identifier-range)
6796 `(c-record-ref-id c-last-identifier-range)))
6797 t)))
6799 (defmacro c-forward-id-comma-list (type update-safe-pos)
6800 ;; Used internally in `c-forward-keyword-clause' to move forward
6801 ;; over a comma separated list of types or names using
6802 ;; `c-forward-keyword-prefixed-id'.
6804 ;; This macro might do hidden buffer changes.
6805 `(while (and (progn
6806 ,(when update-safe-pos
6807 `(setq safe-pos (point)))
6808 (eq (char-after) ?,))
6809 (progn
6810 (forward-char)
6811 (c-forward-syntactic-ws)
6812 (c-forward-keyword-prefixed-id ,type)))))
6814 (defun c-forward-noise-clause ()
6815 ;; Point is at a c-noise-macro-with-parens-names macro identifier. Go
6816 ;; forward over this name, any parenthesis expression which follows it, and
6817 ;; any syntactic WS, ending up at the next token. If there is an unbalanced
6818 ;; paren expression, leave point at it. Always Return t.
6819 (c-forward-token-2)
6820 (if (and (eq (char-after) ?\()
6821 (c-go-list-forward))
6822 (c-forward-syntactic-ws))
6825 (defun c-forward-keyword-clause (match)
6826 ;; Submatch MATCH in the current match data is assumed to surround a
6827 ;; token. If it's a keyword, move over it and any immediately
6828 ;; following clauses associated with it, stopping at the start of
6829 ;; the next token. t is returned in that case, otherwise the point
6830 ;; stays and nil is returned. The kind of clauses that are
6831 ;; recognized are those specified by `c-type-list-kwds',
6832 ;; `c-ref-list-kwds', `c-colon-type-list-kwds',
6833 ;; `c-paren-nontype-kwds', `c-paren-type-kwds', `c-<>-type-kwds',
6834 ;; and `c-<>-arglist-kwds'.
6836 ;; This function records identifier ranges on
6837 ;; `c-record-type-identifiers' and `c-record-ref-identifiers' if
6838 ;; `c-record-type-identifiers' is non-nil.
6840 ;; Note that for `c-colon-type-list-kwds', which doesn't necessary
6841 ;; apply directly after the keyword, the type list is moved over
6842 ;; only when there is no unaccounted token before it (i.e. a token
6843 ;; that isn't moved over due to some other keyword list). The
6844 ;; identifier ranges in the list are still recorded if that should
6845 ;; be done, though.
6847 ;; This function might do hidden buffer changes.
6849 (let ((kwd-sym (c-keyword-sym (match-string match))) safe-pos pos
6850 ;; The call to `c-forward-<>-arglist' below is made after
6851 ;; `c-<>-sexp-kwds' keywords, so we're certain they actually
6852 ;; are angle bracket arglists and `c-restricted-<>-arglists'
6853 ;; should therefore be nil.
6854 (c-parse-and-markup-<>-arglists t)
6855 c-restricted-<>-arglists)
6857 (when kwd-sym
6858 (goto-char (match-end match))
6859 (c-forward-syntactic-ws)
6860 (setq safe-pos (point))
6862 (cond
6863 ((and (c-keyword-member kwd-sym 'c-type-list-kwds)
6864 (c-forward-keyword-prefixed-id type))
6865 ;; There's a type directly after a keyword in `c-type-list-kwds'.
6866 (c-forward-id-comma-list type t))
6868 ((and (c-keyword-member kwd-sym 'c-ref-list-kwds)
6869 (c-forward-keyword-prefixed-id ref))
6870 ;; There's a name directly after a keyword in `c-ref-list-kwds'.
6871 (c-forward-id-comma-list ref t))
6873 ((and (c-keyword-member kwd-sym 'c-paren-any-kwds)
6874 (eq (char-after) ?\())
6875 ;; There's an open paren after a keyword in `c-paren-any-kwds'.
6877 (forward-char)
6878 (when (and (setq pos (c-up-list-forward))
6879 (eq (char-before pos) ?\)))
6880 (when (and c-record-type-identifiers
6881 (c-keyword-member kwd-sym 'c-paren-type-kwds))
6882 ;; Use `c-forward-type' on every identifier we can find
6883 ;; inside the paren, to record the types.
6884 (while (c-syntactic-re-search-forward c-symbol-start pos t)
6885 (goto-char (match-beginning 0))
6886 (unless (c-forward-type)
6887 (looking-at c-symbol-key) ; Always matches.
6888 (goto-char (match-end 0)))))
6890 (goto-char pos)
6891 (c-forward-syntactic-ws)
6892 (setq safe-pos (point))))
6894 ((and (c-keyword-member kwd-sym 'c-<>-sexp-kwds)
6895 (eq (char-after) ?<)
6896 (c-forward-<>-arglist (c-keyword-member kwd-sym 'c-<>-type-kwds)))
6897 (c-forward-syntactic-ws)
6898 (setq safe-pos (point)))
6900 ((and (c-keyword-member kwd-sym 'c-nonsymbol-sexp-kwds)
6901 (not (looking-at c-symbol-start))
6902 (c-safe (c-forward-sexp) t))
6903 (c-forward-syntactic-ws)
6904 (setq safe-pos (point))))
6906 (when (c-keyword-member kwd-sym 'c-colon-type-list-kwds)
6907 (if (eq (char-after) ?:)
6908 ;; If we are at the colon already, we move over the type
6909 ;; list after it.
6910 (progn
6911 (forward-char)
6912 (c-forward-syntactic-ws)
6913 (when (c-forward-keyword-prefixed-id type)
6914 (c-forward-id-comma-list type t)))
6915 ;; Not at the colon, so stop here. But the identifier
6916 ;; ranges in the type list later on should still be
6917 ;; recorded.
6918 (and c-record-type-identifiers
6919 (progn
6920 ;; If a keyword matched both one of the types above and
6921 ;; this one, we match `c-colon-type-list-re' after the
6922 ;; clause matched above.
6923 (goto-char safe-pos)
6924 (looking-at c-colon-type-list-re))
6925 (progn
6926 (goto-char (match-end 0))
6927 (c-forward-syntactic-ws)
6928 (c-forward-keyword-prefixed-id type))
6929 ;; There's a type after the `c-colon-type-list-re' match
6930 ;; after a keyword in `c-colon-type-list-kwds'.
6931 (c-forward-id-comma-list type nil))))
6933 (goto-char safe-pos)
6934 t)))
6936 ;; cc-mode requires cc-fonts.
6937 (declare-function c-fontify-recorded-types-and-refs "cc-fonts" ())
6939 (defun c-forward-<>-arglist (all-types)
6940 ;; The point is assumed to be at a "<". Try to treat it as the open
6941 ;; paren of an angle bracket arglist and move forward to the
6942 ;; corresponding ">". If successful, the point is left after the
6943 ;; ">" and t is returned, otherwise the point isn't moved and nil is
6944 ;; returned. If ALL-TYPES is t then all encountered arguments in
6945 ;; the arglist that might be types are treated as found types.
6947 ;; The variable `c-parse-and-markup-<>-arglists' controls how this
6948 ;; function handles text properties on the angle brackets and argument
6949 ;; separating commas.
6951 ;; `c-restricted-<>-arglists' controls how lenient the template
6952 ;; arglist recognition should be.
6954 ;; This function records identifier ranges on
6955 ;; `c-record-type-identifiers' and `c-record-ref-identifiers' if
6956 ;; `c-record-type-identifiers' is non-nil.
6958 ;; This function might do hidden buffer changes.
6960 (let ((start (point))
6961 ;; If `c-record-type-identifiers' is set then activate
6962 ;; recording of any found types that constitute an argument in
6963 ;; the arglist.
6964 (c-record-found-types (if c-record-type-identifiers t)))
6965 (if (catch 'angle-bracket-arglist-escape
6966 (setq c-record-found-types
6967 (c-forward-<>-arglist-recur all-types)))
6968 (progn
6969 (when (consp c-record-found-types)
6970 (setq c-record-type-identifiers
6971 ;; `nconc' doesn't mind that the tail of
6972 ;; `c-record-found-types' is t.
6973 (nconc c-record-found-types c-record-type-identifiers)))
6976 (goto-char start)
6977 nil)))
6979 (defun c-forward-<>-arglist-recur (all-types)
6980 ;; Recursive part of `c-forward-<>-arglist'.
6982 ;; This function might do hidden buffer changes.
6983 (let ((start (point)) res pos
6984 ;; Cover this so that any recorded found type ranges are
6985 ;; automatically lost if it turns out to not be an angle
6986 ;; bracket arglist. It's propagated through the return value
6987 ;; on successful completion.
6988 (c-record-found-types c-record-found-types)
6989 ;; List that collects the positions after the argument
6990 ;; separating ',' in the arglist.
6991 arg-start-pos)
6992 ;; If the '<' has paren open syntax then we've marked it as an angle
6993 ;; bracket arglist before, so skip to the end.
6994 (if (and (not c-parse-and-markup-<>-arglists)
6995 (c-get-char-property (point) 'syntax-table))
6997 (progn
6998 (forward-char)
6999 (if (and (c-go-up-list-forward)
7000 (eq (char-before) ?>))
7002 ;; Got unmatched paren angle brackets. We don't clear the paren
7003 ;; syntax properties and retry, on the basis that it's very
7004 ;; unlikely that paren angle brackets become operators by code
7005 ;; manipulation. It's far more likely that it doesn't match due
7006 ;; to narrowing or some temporary change.
7007 (goto-char start)
7008 nil))
7010 (forward-char) ; Forward over the opening '<'.
7012 (unless (looking-at c-<-op-cont-regexp)
7013 ;; go forward one non-alphanumeric character (group) per iteration of
7014 ;; this loop.
7015 (while (and
7016 (progn
7017 (c-forward-syntactic-ws)
7018 (when (or (and c-record-type-identifiers all-types)
7019 (not (equal c-inside-<>-type-key "\\(\\<\\>\\)")))
7020 (c-forward-syntactic-ws)
7021 (cond
7022 ((eq (char-after) ??)
7023 (forward-char))
7024 ((and (looking-at c-identifier-start)
7025 (not (looking-at c-keywords-regexp)))
7026 (if (or (and all-types c-record-type-identifiers)
7027 (c-major-mode-is 'java-mode))
7028 ;; All encountered identifiers are types, so set the
7029 ;; promote flag and parse the type.
7030 (let ((c-promote-possible-types t)
7031 (c-record-found-types t))
7032 (c-forward-type))
7033 (c-forward-token-2))))
7035 (c-forward-syntactic-ws)
7037 (when (looking-at c-inside-<>-type-key)
7038 (goto-char (match-end 1))
7039 (c-forward-syntactic-ws)
7040 (let ((c-promote-possible-types t)
7041 (c-record-found-types t))
7042 (c-forward-type))
7043 (c-forward-syntactic-ws)))
7045 (setq pos (point)) ; e.g. first token inside the '<'
7047 ;; Note: These regexps exploit the match order in \| so
7048 ;; that "<>" is matched by "<" rather than "[^>:-]>".
7049 (c-syntactic-re-search-forward
7050 ;; Stop on ',', '|', '&', '+' and '-' to catch
7051 ;; common binary operators that could be between
7052 ;; two comparison expressions "a<b" and "c>d".
7053 ;; 2016-02-11: C++11 templates can now contain arithmetic
7054 ;; expressions, so template detection in C++ is now less
7055 ;; robust than it was.
7056 c-<>-notable-chars-re
7057 nil t t))
7059 (cond
7060 ((eq (char-before) ?>)
7061 ;; Either an operator starting with '>' or the end of
7062 ;; the angle bracket arglist.
7064 (if (save-excursion
7065 (c-backward-token-2)
7066 (looking-at c-multichar->-op-not->>-regexp))
7067 (progn
7068 (goto-char (match-end 0))
7069 t) ; Continue the loop.
7071 ;; The angle bracket arglist is finished.
7072 (when c-parse-and-markup-<>-arglists
7073 (while arg-start-pos
7074 (c-put-c-type-property (1- (car arg-start-pos))
7075 'c-<>-arg-sep)
7076 (setq arg-start-pos (cdr arg-start-pos)))
7077 (c-mark-<-as-paren start)
7078 (c-mark->-as-paren (1- (point))))
7079 (setq res t)
7080 nil)) ; Exit the loop.
7082 ((eq (char-before) ?<)
7083 ;; Either an operator starting with '<' or a nested arglist.
7084 (setq pos (point))
7085 (let (id-start id-end subres keyword-match)
7086 (cond
7087 ;; The '<' begins a multi-char operator.
7088 ((looking-at c-<-op-cont-regexp)
7089 (goto-char (match-end 0)))
7090 ;; We're at a nested <.....>
7091 ((progn
7092 (backward-char) ; to the '<'
7093 (and
7094 (save-excursion
7095 ;; There's always an identifier before an angle
7096 ;; bracket arglist, or a keyword in `c-<>-type-kwds'
7097 ;; or `c-<>-arglist-kwds'.
7098 (c-backward-syntactic-ws)
7099 (setq id-end (point))
7100 (c-simple-skip-symbol-backward)
7101 (when (or (setq keyword-match
7102 (looking-at c-opt-<>-sexp-key))
7103 (not (looking-at c-keywords-regexp)))
7104 (setq id-start (point))))
7105 (setq subres
7106 (let ((c-promote-possible-types t)
7107 (c-record-found-types t))
7108 (c-forward-<>-arglist-recur
7109 (and keyword-match
7110 (c-keyword-member
7111 (c-keyword-sym (match-string 1))
7112 'c-<>-type-kwds))))))
7113 (or subres (goto-char pos))
7114 subres)
7115 ;; It was an angle bracket arglist.
7116 (setq c-record-found-types subres)
7118 ;; Record the identifier before the template as a type
7119 ;; or reference depending on whether the arglist is last
7120 ;; in a qualified identifier.
7121 (when (and c-record-type-identifiers
7122 (not keyword-match))
7123 (if (and c-opt-identifier-concat-key
7124 (progn
7125 (c-forward-syntactic-ws)
7126 (looking-at c-opt-identifier-concat-key)))
7127 (c-record-ref-id (cons id-start id-end))
7128 (c-record-type-id (cons id-start id-end)))))
7130 ;; At a "less than" operator.
7132 ;; (forward-char) ; NO! We've already gone over the <.
7134 t) ; carry on looping.
7136 ((and
7137 (eq (char-before) ?\()
7138 (c-go-up-list-forward)
7139 (eq (char-before) ?\))))
7141 ((and (not c-restricted-<>-arglists)
7142 (or (and (eq (char-before) ?&)
7143 (not (eq (char-after) ?&)))
7144 (eq (char-before) ?,)))
7145 ;; Just another argument. Record the position. The
7146 ;; type check stuff that made us stop at it is at
7147 ;; the top of the loop.
7148 (setq arg-start-pos (cons (point) arg-start-pos)))
7151 ;; Got a character that can't be in an angle bracket
7152 ;; arglist argument. Abort using `throw', since
7153 ;; it's useless to try to find a surrounding arglist
7154 ;; if we're nested.
7155 (throw 'angle-bracket-arglist-escape nil))))))
7156 (if res
7157 (or c-record-found-types t)))))
7159 (defun c-backward-<>-arglist (all-types &optional limit)
7160 ;; The point is assumed to be directly after a ">". Try to treat it
7161 ;; as the close paren of an angle bracket arglist and move back to
7162 ;; the corresponding "<". If successful, the point is left at
7163 ;; the "<" and t is returned, otherwise the point isn't moved and
7164 ;; nil is returned. ALL-TYPES is passed on to
7165 ;; `c-forward-<>-arglist'.
7167 ;; If the optional LIMIT is given, it bounds the backward search.
7168 ;; It's then assumed to be at a syntactically relevant position.
7170 ;; This is a wrapper around `c-forward-<>-arglist'. See that
7171 ;; function for more details.
7173 (let ((start (point)))
7174 (backward-char)
7175 (if (and (not c-parse-and-markup-<>-arglists)
7176 (c-get-char-property (point) 'syntax-table))
7178 (if (and (c-go-up-list-backward)
7179 (eq (char-after) ?<))
7181 ;; See corresponding note in `c-forward-<>-arglist'.
7182 (goto-char start)
7183 nil)
7185 (while (progn
7186 (c-syntactic-skip-backward "^<;{}" limit t)
7188 (and
7189 (if (eq (char-before) ?<)
7191 ;; Stopped at bob or a char that isn't allowed in an
7192 ;; arglist, so we've failed.
7193 (goto-char start)
7194 nil)
7196 (if (> (point)
7197 (progn (c-beginning-of-current-token)
7198 (point)))
7199 ;; If we moved then the "<" was part of some
7200 ;; multicharacter token.
7203 (backward-char)
7204 (let ((beg-pos (point)))
7205 (if (c-forward-<>-arglist all-types)
7206 (cond ((= (point) start)
7207 ;; Matched the arglist. Break the while.
7208 (goto-char beg-pos)
7209 nil)
7210 ((> (point) start)
7211 ;; We started from a non-paren ">" inside an
7212 ;; arglist.
7213 (goto-char start)
7214 nil)
7216 ;; Matched a shorter arglist. Can be a nested
7217 ;; one so continue looking.
7218 (goto-char beg-pos)
7220 t))))))
7222 (/= (point) start))))
7224 (defun c-forward-name ()
7225 ;; Move forward over a complete name if at the beginning of one,
7226 ;; stopping at the next following token. A keyword, as such,
7227 ;; doesn't count as a name. If the point is not at something that
7228 ;; is recognized as a name then it stays put.
7230 ;; A name could be something as simple as "foo" in C or something as
7231 ;; complex as "X<Y<class A<int>::B, BIT_MAX >> b>, ::operator<> ::
7232 ;; Z<(a>b)> :: operator const X<&foo>::T Q::G<unsigned short
7233 ;; int>::*volatile const" in C++ (this function is actually little
7234 ;; more than a `looking-at' call in all modes except those that,
7235 ;; like C++, have `c-recognize-<>-arglists' set).
7237 ;; Return
7238 ;; o - nil if no name is found;
7239 ;; o - 'template if it's an identifier ending with an angle bracket
7240 ;; arglist;
7241 ;; o - 'operator of it's an operator identifier;
7242 ;; o - t if it's some other kind of name.
7244 ;; This function records identifier ranges on
7245 ;; `c-record-type-identifiers' and `c-record-ref-identifiers' if
7246 ;; `c-record-type-identifiers' is non-nil.
7248 ;; This function might do hidden buffer changes.
7250 (let ((pos (point)) (start (point)) res id-start id-end
7251 ;; Turn off `c-promote-possible-types' here since we might
7252 ;; call `c-forward-<>-arglist' and we don't want it to promote
7253 ;; every suspect thing in the arglist to a type. We're
7254 ;; typically called from `c-forward-type' in this case, and
7255 ;; the caller only wants the top level type that it finds to
7256 ;; be promoted.
7257 c-promote-possible-types)
7258 (while
7259 (and
7260 (looking-at c-identifier-key)
7262 (progn
7263 ;; Check for keyword. We go to the last symbol in
7264 ;; `c-identifier-key' first.
7265 (goto-char (setq id-end (match-end 0)))
7266 (c-simple-skip-symbol-backward)
7267 (setq id-start (point))
7269 (if (looking-at c-keywords-regexp)
7270 (when (and (c-major-mode-is 'c++-mode)
7271 (looking-at
7272 (cc-eval-when-compile
7273 (concat "\\(operator\\|\\(template\\)\\)"
7274 "\\(" (c-lang-const c-nonsymbol-key c++)
7275 "\\|$\\)")))
7276 (if (match-beginning 2)
7277 ;; "template" is only valid inside an
7278 ;; identifier if preceded by "::".
7279 (save-excursion
7280 (c-backward-syntactic-ws)
7281 (and (c-safe (backward-char 2) t)
7282 (looking-at "::")))
7285 ;; Handle a C++ operator or template identifier.
7286 (goto-char id-end)
7287 (c-forward-syntactic-ws)
7288 (cond ((eq (char-before id-end) ?e)
7289 ;; Got "... ::template".
7290 (let ((subres (c-forward-name)))
7291 (when subres
7292 (setq pos (point)
7293 res subres))))
7295 ((looking-at c-identifier-start)
7296 ;; Got a cast operator.
7297 (when (c-forward-type)
7298 (setq pos (point)
7299 res 'operator)
7300 ;; Now we should match a sequence of either
7301 ;; '*', '&' or a name followed by ":: *",
7302 ;; where each can be followed by a sequence
7303 ;; of `c-opt-type-modifier-key'.
7304 (while (cond ((looking-at "[*&]")
7305 (goto-char (match-end 0))
7307 ((looking-at c-identifier-start)
7308 (and (c-forward-name)
7309 (looking-at "::")
7310 (progn
7311 (goto-char (match-end 0))
7312 (c-forward-syntactic-ws)
7313 (eq (char-after) ?*))
7314 (progn
7315 (forward-char)
7316 t))))
7317 (while (progn
7318 (c-forward-syntactic-ws)
7319 (setq pos (point))
7320 (looking-at c-opt-type-modifier-key))
7321 (goto-char (match-end 1))))))
7323 ((looking-at c-overloadable-operators-regexp)
7324 ;; Got some other operator.
7325 (setq c-last-identifier-range
7326 (cons (point) (match-end 0)))
7327 (goto-char (match-end 0))
7328 (c-forward-syntactic-ws)
7329 (setq pos (point)
7330 res 'operator)))
7332 nil)
7334 ;; `id-start' is equal to `id-end' if we've jumped over
7335 ;; an identifier that doesn't end with a symbol token.
7336 ;; That can occur e.g. for Java import directives on the
7337 ;; form "foo.bar.*".
7338 (when (and id-start (/= id-start id-end))
7339 (setq c-last-identifier-range
7340 (cons id-start id-end)))
7341 (goto-char id-end)
7342 (c-forward-syntactic-ws)
7343 (setq pos (point)
7344 res t)))
7346 (progn
7347 (goto-char pos)
7348 (when (or c-opt-identifier-concat-key
7349 c-recognize-<>-arglists)
7351 (cond
7352 ((and c-opt-identifier-concat-key
7353 (looking-at c-opt-identifier-concat-key))
7354 ;; Got a concatenated identifier. This handles the
7355 ;; cases with tricky syntactic whitespace that aren't
7356 ;; covered in `c-identifier-key'.
7357 (goto-char (match-end 0))
7358 (c-forward-syntactic-ws)
7361 ((and c-recognize-<>-arglists
7362 (eq (char-after) ?<))
7363 ;; Maybe an angle bracket arglist.
7364 (when (let (c-last-identifier-range)
7365 (c-forward-<>-arglist nil))
7367 (c-forward-syntactic-ws)
7368 (unless (eq (char-after) ?\()
7369 (setq c-last-identifier-range nil)
7370 (c-add-type start (1+ pos)))
7371 (setq pos (point))
7373 (if (and c-opt-identifier-concat-key
7374 (looking-at c-opt-identifier-concat-key))
7376 ;; Continue if there's an identifier concatenation
7377 ;; operator after the template argument.
7378 (progn
7379 (when (and c-record-type-identifiers id-start)
7380 (c-record-ref-id (cons id-start id-end)))
7381 (forward-char 2)
7382 (c-forward-syntactic-ws)
7385 (when (and c-record-type-identifiers id-start
7386 (not (eq (char-after) ?\()))
7387 (c-record-type-id (cons id-start id-end)))
7388 (setq res 'template)
7389 nil)))
7390 )))))
7392 (goto-char pos)
7393 res))
7395 (defun c-forward-type (&optional brace-block-too)
7396 ;; Move forward over a type spec if at the beginning of one,
7397 ;; stopping at the next following token. The keyword "typedef"
7398 ;; isn't part of a type spec here.
7400 ;; BRACE-BLOCK-TOO, when non-nil, means move over the brace block in
7401 ;; constructs like "struct foo {...} bar ;" or "struct {...} bar;".
7402 ;; The current (2009-03-10) intention is to convert all uses of
7403 ;; `c-forward-type' to call with this parameter set, then to
7404 ;; eliminate it.
7406 ;; Return
7407 ;; o - t if it's a known type that can't be a name or other
7408 ;; expression;
7409 ;; o - 'known if it's an otherwise known type (according to
7410 ;; `*-font-lock-extra-types');
7411 ;; o - 'prefix if it's a known prefix of a type;
7412 ;; o - 'found if it's a type that matches one in `c-found-types';
7413 ;; o - 'maybe if it's an identifier that might be a type;
7414 ;; o - 'decltype if it's a decltype(variable) declaration; - or
7415 ;; o - nil if it can't be a type (the point isn't moved then).
7417 ;; The point is assumed to be at the beginning of a token.
7419 ;; Note that this function doesn't skip past the brace definition
7420 ;; that might be considered part of the type, e.g.
7421 ;; "enum {a, b, c} foo".
7423 ;; This function records identifier ranges on
7424 ;; `c-record-type-identifiers' and `c-record-ref-identifiers' if
7425 ;; `c-record-type-identifiers' is non-nil.
7427 ;; This function might do hidden buffer changes.
7428 (when (and c-recognize-<>-arglists
7429 (looking-at "<"))
7430 (c-forward-<>-arglist t)
7431 (c-forward-syntactic-ws))
7433 (let ((start (point)) pos res name-res id-start id-end id-range)
7435 ;; Skip leading type modifiers. If any are found we know it's a
7436 ;; prefix of a type.
7437 (when c-opt-type-modifier-key ; e.g. "const" "volatile", but NOT "typedef"
7438 (while (looking-at c-opt-type-modifier-key)
7439 (goto-char (match-end 1))
7440 (c-forward-syntactic-ws)
7441 (setq res 'prefix)))
7443 (cond
7444 ((looking-at c-typeof-key) ; e.g. C++'s "decltype".
7445 (goto-char (match-end 1))
7446 (c-forward-syntactic-ws)
7447 (setq res (and (eq (char-after) ?\()
7448 (c-safe (c-forward-sexp))
7449 'decltype))
7450 (if res
7451 (c-forward-syntactic-ws)
7452 (goto-char start)))
7454 ((looking-at c-type-prefix-key) ; e.g. "struct", "class", but NOT
7455 ; "typedef".
7456 (goto-char (match-end 1))
7457 (c-forward-syntactic-ws)
7459 (while (cond
7460 ((looking-at c-decl-hangon-key)
7461 (c-forward-keyword-clause 1))
7462 ((looking-at c-pack-key)
7463 (goto-char (match-end 1))
7464 (c-forward-syntactic-ws))
7465 ((and c-opt-cpp-prefix
7466 (looking-at c-noise-macro-with-parens-name-re))
7467 (c-forward-noise-clause))))
7469 (setq pos (point))
7471 (setq name-res (c-forward-name))
7472 (setq res (not (null name-res)))
7473 (when (eq name-res t)
7474 ;; In many languages the name can be used without the
7475 ;; prefix, so we add it to `c-found-types'.
7476 (c-add-type pos (point))
7477 (when (and c-record-type-identifiers
7478 c-last-identifier-range)
7479 (c-record-type-id c-last-identifier-range)))
7480 (when (and brace-block-too
7481 (memq res '(t nil))
7482 (eq (char-after) ?\{)
7483 (save-excursion
7484 (c-safe
7485 (progn (c-forward-sexp)
7486 (c-forward-syntactic-ws)
7487 (setq pos (point))))))
7488 (goto-char pos)
7489 (setq res t))
7490 (unless res (goto-char start))) ; invalid syntax
7492 ((progn
7493 (setq pos nil)
7494 (if (looking-at c-identifier-start)
7495 (save-excursion
7496 (setq id-start (point)
7497 name-res (c-forward-name))
7498 (when name-res
7499 (setq id-end (point)
7500 id-range c-last-identifier-range))))
7501 (and (cond ((looking-at c-primitive-type-key)
7502 (setq res t))
7503 ((c-with-syntax-table c-identifier-syntax-table
7504 (looking-at c-known-type-key))
7505 (setq res 'known)))
7506 (or (not id-end)
7507 (>= (save-excursion
7508 (save-match-data
7509 (goto-char (match-end 1))
7510 (c-forward-syntactic-ws)
7511 (setq pos (point))))
7512 id-end)
7513 (setq res nil))))
7514 ;; Looking at a primitive or known type identifier. We've
7515 ;; checked for a name first so that we don't go here if the
7516 ;; known type match only is a prefix of another name.
7518 (setq id-end (match-end 1))
7520 (when (and c-record-type-identifiers
7521 (or c-promote-possible-types (eq res t)))
7522 (c-record-type-id (cons (match-beginning 1) (match-end 1))))
7524 (if (and c-opt-type-component-key
7525 (save-match-data
7526 (looking-at c-opt-type-component-key)))
7527 ;; There might be more keywords for the type.
7528 (let (safe-pos)
7529 (c-forward-keyword-clause 1)
7530 (while (progn
7531 (setq safe-pos (point))
7532 (looking-at c-opt-type-component-key))
7533 (when (and c-record-type-identifiers
7534 (looking-at c-primitive-type-key))
7535 (c-record-type-id (cons (match-beginning 1)
7536 (match-end 1))))
7537 (c-forward-keyword-clause 1))
7538 (if (looking-at c-primitive-type-key)
7539 (progn
7540 (when c-record-type-identifiers
7541 (c-record-type-id (cons (match-beginning 1)
7542 (match-end 1))))
7543 (c-forward-keyword-clause 1)
7544 (setq res t))
7545 (goto-char safe-pos)
7546 (setq res 'prefix)))
7547 (unless (save-match-data (c-forward-keyword-clause 1))
7548 (if pos
7549 (goto-char pos)
7550 (goto-char (match-end 1))
7551 (c-forward-syntactic-ws)))))
7553 (name-res
7554 (cond ((eq name-res t)
7555 ;; A normal identifier.
7556 (goto-char id-end)
7557 (if (or res c-promote-possible-types)
7558 (progn
7559 (c-add-type id-start id-end)
7560 (when (and c-record-type-identifiers id-range)
7561 (c-record-type-id id-range))
7562 (unless res
7563 (setq res 'found)))
7564 (setq res (if (c-check-type id-start id-end)
7565 ;; It's an identifier that has been used as
7566 ;; a type somewhere else.
7567 'found
7568 ;; It's an identifier that might be a type.
7569 'maybe))))
7570 ((eq name-res 'template)
7571 ;; A template is sometimes a type.
7572 (goto-char id-end)
7573 (c-forward-syntactic-ws)
7574 (setq res
7575 (if (eq (char-after) ?\()
7576 (if (c-check-type id-start id-end)
7577 ;; It's an identifier that has been used as
7578 ;; a type somewhere else.
7579 'found
7580 ;; It's an identifier that might be a type.
7581 'maybe)
7582 t)))
7584 ;; Otherwise it's an operator identifier, which is not a type.
7585 (goto-char start)
7586 (setq res nil)))))
7588 (when res
7589 ;; Skip trailing type modifiers. If any are found we know it's
7590 ;; a type.
7591 (when c-opt-type-modifier-key
7592 (while (looking-at c-opt-type-modifier-key) ; e.g. "const", "volatile"
7593 (goto-char (match-end 1))
7594 (c-forward-syntactic-ws)
7595 (setq res t)))
7597 ;; Step over any type suffix operator. Do not let the existence
7598 ;; of these alter the classification of the found type, since
7599 ;; these operators typically are allowed in normal expressions
7600 ;; too.
7601 (when c-opt-type-suffix-key ; e.g. "..."
7602 (while (looking-at c-opt-type-suffix-key)
7603 (goto-char (match-end 1))
7604 (c-forward-syntactic-ws)))
7606 ;; Skip any "WS" identifiers (e.g. "final" or "override" in C++)
7607 (while (looking-at c-type-decl-suffix-ws-ids-key)
7608 (goto-char (match-end 1))
7609 (c-forward-syntactic-ws)
7610 (setq res t))
7612 (when c-opt-type-concat-key ; Only/mainly for pike.
7613 ;; Look for a trailing operator that concatenates the type
7614 ;; with a following one, and if so step past that one through
7615 ;; a recursive call. Note that we don't record concatenated
7616 ;; types in `c-found-types' - it's the component types that
7617 ;; are recorded when appropriate.
7618 (setq pos (point))
7619 (let* ((c-promote-possible-types (or (memq res '(t known))
7620 c-promote-possible-types))
7621 ;; If we can't promote then set `c-record-found-types' so that
7622 ;; we can merge in the types from the second part afterwards if
7623 ;; it turns out to be a known type there.
7624 (c-record-found-types (and c-record-type-identifiers
7625 (not c-promote-possible-types)))
7626 subres)
7627 (if (and (looking-at c-opt-type-concat-key)
7629 (progn
7630 (goto-char (match-end 1))
7631 (c-forward-syntactic-ws)
7632 (setq subres (c-forward-type))))
7634 (progn
7635 ;; If either operand certainly is a type then both are, but we
7636 ;; don't let the existence of the operator itself promote two
7637 ;; uncertain types to a certain one.
7638 (cond ((eq res t))
7639 ((eq subres t)
7640 (unless (eq name-res 'template)
7641 (c-add-type id-start id-end))
7642 (when (and c-record-type-identifiers id-range)
7643 (c-record-type-id id-range))
7644 (setq res t))
7645 ((eq res 'known))
7646 ((eq subres 'known)
7647 (setq res 'known))
7648 ((eq res 'found))
7649 ((eq subres 'found)
7650 (setq res 'found))
7652 (setq res 'maybe)))
7654 (when (and (eq res t)
7655 (consp c-record-found-types))
7656 ;; Merge in the ranges of any types found by the second
7657 ;; `c-forward-type'.
7658 (setq c-record-type-identifiers
7659 ;; `nconc' doesn't mind that the tail of
7660 ;; `c-record-found-types' is t.
7661 (nconc c-record-found-types
7662 c-record-type-identifiers))))
7664 (goto-char pos))))
7666 (when (and c-record-found-types (memq res '(known found)) id-range)
7667 (setq c-record-found-types
7668 (cons id-range c-record-found-types))))
7670 ;;(message "c-forward-type %s -> %s: %s" start (point) res)
7672 res))
7674 (defun c-forward-annotation ()
7675 ;; Used for Java code only at the moment. Assumes point is on the @, moves
7676 ;; forward an annotation and returns t. Leaves point unmoved and returns
7677 ;; nil if there is no annotation at point.
7678 (let ((pos (point)))
7680 (and (looking-at "@")
7681 (not (looking-at c-keywords-regexp))
7682 (progn (forward-char) t)
7683 (looking-at c-symbol-key)
7684 (progn (goto-char (match-end 0))
7685 (c-forward-syntactic-ws)
7687 (if (looking-at "(")
7688 (c-go-list-forward)
7690 (progn (goto-char pos) nil))))
7692 (defmacro c-pull-open-brace (ps)
7693 ;; Pull the next open brace from PS (which has the form of paren-state),
7694 ;; skipping over any brace pairs. Returns NIL when PS is exhausted.
7695 `(progn
7696 (while (consp (car ,ps))
7697 (setq ,ps (cdr ,ps)))
7698 (prog1 (car ,ps)
7699 (setq ,ps (cdr ,ps)))))
7701 (defun c-back-over-compound-identifier ()
7702 ;; Point is putatively just after a "compound identifier", i.e. something
7703 ;; looking (in C++) like this "FQN::of::base::Class". Move to the start of
7704 ;; this construct and return t. If the parsing fails, return nil, leaving
7705 ;; point unchanged.
7706 (let ((here (point))
7707 end)
7708 (if (not (c-on-identifier))
7710 (c-simple-skip-symbol-backward)
7711 (while
7712 (progn
7713 (setq end (point))
7714 (c-backward-syntactic-ws)
7715 (c-backward-token-2)
7716 (and
7717 c-opt-identifier-concat-key
7718 (looking-at c-opt-identifier-concat-key)
7719 (progn
7720 (c-backward-syntactic-ws)
7721 (c-simple-skip-symbol-backward))))
7722 (setq end (point)))
7723 (goto-char end)
7724 t)))
7726 (defun c-back-over-member-initializer-braces ()
7727 ;; Point is just after a closing brace/parenthesis. Try to parse this as a
7728 ;; C++ member initializer list, going back to just after the introducing ":"
7729 ;; and returning t. Otherwise return nil, leaving point unchanged.
7730 (let ((here (point)) res)
7731 (setq res
7732 (catch 'done
7733 (when (not (c-go-list-backward))
7734 (throw 'done nil))
7735 (c-backward-syntactic-ws)
7736 (when (not (c-back-over-compound-identifier))
7737 (throw 'done nil))
7738 (c-backward-syntactic-ws)
7740 (while (eq (char-before) ?,)
7741 (backward-char)
7742 (c-backward-syntactic-ws)
7743 (when (not (memq (char-before) '(?\) ?})))
7744 (throw 'done nil))
7745 (when (not (c-go-list-backward))
7746 (throw 'done nil))
7747 (c-backward-syntactic-ws)
7748 (when (not (c-back-over-compound-identifier))
7749 (throw 'done nil))
7750 (c-backward-syntactic-ws))
7752 (eq (char-before) ?:)))
7753 (or res (goto-char here))
7754 res))
7756 (defmacro c-back-over-list-of-member-inits ()
7757 ;; Go back over a list of elements, each looking like:
7758 ;; <symbol> (<expression>) ,
7759 ;; or <symbol> {<expression>} , (with possibly a <....> expressions
7760 ;; following the <symbol>).
7761 ;; when we are putatively immediately after a comma. Stop when we don't see
7762 ;; a comma. If either of <symbol> or bracketed <expression> is missing,
7763 ;; throw nil to 'level. If the terminating } or ) is unmatched, throw nil
7764 ;; to 'done. This is not a general purpose macro!
7765 `(while (eq (char-before) ?,)
7766 (backward-char)
7767 (c-backward-syntactic-ws)
7768 (when (not (memq (char-before) '(?\) ?})))
7769 (throw 'level nil))
7770 (when (not (c-go-list-backward))
7771 (throw 'done nil))
7772 (c-backward-syntactic-ws)
7773 (while (eq (char-before) ?>)
7774 (when (not (c-backward-<>-arglist nil))
7775 (throw 'done nil))
7776 (c-backward-syntactic-ws))
7777 (when (not (c-back-over-compound-identifier))
7778 (throw 'level nil))
7779 (c-backward-syntactic-ws)))
7781 (defun c-back-over-member-initializers ()
7782 ;; Test whether we are in a C++ member initializer list, and if so, go back
7783 ;; to the introducing ":", returning the position of the opening paren of
7784 ;; the function's arglist. Otherwise return nil, leaving point unchanged.
7785 (let ((here (point))
7786 (paren-state (c-parse-state))
7787 pos level-plausible at-top-level res)
7788 ;; Assume tentatively that we're at the top level. Try to go back to the
7789 ;; colon we seek.
7790 (setq res
7791 (catch 'done
7792 (setq level-plausible
7793 (catch 'level
7794 (c-backward-syntactic-ws)
7795 (when (memq (char-before) '(?\) ?}))
7796 (when (not (c-go-list-backward))
7797 (throw 'done nil))
7798 (c-backward-syntactic-ws))
7799 (when (c-back-over-compound-identifier)
7800 (c-backward-syntactic-ws))
7801 (c-back-over-list-of-member-inits)
7802 (and (eq (char-before) ?:)
7803 (save-excursion
7804 (c-backward-token-2)
7805 (not (looking-at c-:$-multichar-token-regexp)))
7806 (c-just-after-func-arglist-p))))
7808 (while (and (not (and level-plausible
7809 (setq at-top-level (c-at-toplevel-p))))
7810 (setq pos (c-pull-open-brace paren-state))) ; might be a paren.
7811 (setq level-plausible
7812 (catch 'level
7813 (goto-char pos)
7814 (c-backward-syntactic-ws)
7815 (when (not (c-back-over-compound-identifier))
7816 (throw 'level nil))
7817 (c-backward-syntactic-ws)
7818 (c-back-over-list-of-member-inits)
7819 (and (eq (char-before) ?:)
7820 (save-excursion
7821 (c-backward-token-2)
7822 (not (looking-at c-:$-multichar-token-regexp)))
7823 (c-just-after-func-arglist-p)))))
7825 (and at-top-level level-plausible)))
7826 (or res (goto-char here))
7827 res))
7830 ;; Handling of large scale constructs like statements and declarations.
7832 ;; Macro used inside `c-forward-decl-or-cast-1'. It ought to be a
7833 ;; defsubst or perhaps even a defun, but it contains lots of free
7834 ;; variables that refer to things inside `c-forward-decl-or-cast-1'.
7835 (defmacro c-fdoc-shift-type-backward (&optional short)
7836 ;; `c-forward-decl-or-cast-1' can consume an arbitrary length list
7837 ;; of types when parsing a declaration, which means that it
7838 ;; sometimes consumes the identifier in the declaration as a type.
7839 ;; This is used to "backtrack" and make the last type be treated as
7840 ;; an identifier instead.
7841 `(progn
7842 ,(unless short
7843 ;; These identifiers are bound only in the inner let.
7844 '(setq identifier-type at-type
7845 identifier-start type-start
7846 got-parens nil
7847 got-identifier t
7848 got-suffix t
7849 got-suffix-after-parens id-start
7850 paren-depth 0))
7852 (if (setq at-type (if (eq backup-at-type 'prefix)
7854 backup-at-type))
7855 (setq type-start backup-type-start
7856 id-start backup-id-start)
7857 (setq type-start start-pos
7858 id-start start-pos))
7860 ;; When these flags already are set we've found specifiers that
7861 ;; unconditionally signal these attributes - backtracking doesn't
7862 ;; change that. So keep them set in that case.
7863 (or at-type-decl
7864 (setq at-type-decl backup-at-type-decl))
7865 (or maybe-typeless
7866 (setq maybe-typeless backup-maybe-typeless))
7868 ,(unless short
7869 ;; This identifier is bound only in the inner let.
7870 '(setq start id-start))))
7872 (defun c-forward-declarator (&optional limit accept-anon)
7873 ;; Assuming point is at the start of a declarator, move forward over it,
7874 ;; leaving point at the next token after it (e.g. a ) or a ; or a ,).
7876 ;; Return a list (ID-START ID-END BRACKETS-AFTER-ID GOT-INIT DECORATED),
7877 ;; where ID-START and ID-END are the bounds of the declarator's identifier,
7878 ;; and BRACKETS-AFTER-ID is non-nil if a [...] pair is present after the id.
7879 ;; GOT-INIT is non-nil when the declarator is followed by "=" or "(",
7880 ;; DECORATED is non-nil when the identifier is embellished by an operator,
7881 ;; like "*x", or "(*x)".
7883 ;; If ACCEPT-ANON is non-nil, move forward over any "anonymous declarator",
7884 ;; i.e. something like the (*) in int (*), such as might be found in a
7885 ;; declaration. In such a case ID-START and ID-END in the return value are
7886 ;; both set to nil. A "null" "anonymous declarator" gives a non-nil result.
7888 ;; If no declarator is found, leave point unmoved and return nil. LIMIT is
7889 ;; an optional limit for forward searching.
7891 ;; Note that the global variable `c-last-identifier-range' is written to, so
7892 ;; the caller should bind it if necessary.
7894 ;; Inside the following "condition form", we move forward over the
7895 ;; declarator's identifier up as far as any opening bracket (for array
7896 ;; size) or paren (for parameters of function-type) or brace (for
7897 ;; array/struct initialization) or "=" or terminating delimiter
7898 ;; (e.g. "," or ";" or "}").
7899 (let ((here (point))
7900 id-start id-end brackets-after-id paren-depth decorated)
7901 (or limit (setq limit (point-max)))
7902 (if (and
7903 (< (point) limit)
7905 ;; The following form moves forward over the declarator's
7906 ;; identifier (and what precedes it), returning t. If there
7907 ;; wasn't one, it returns nil.
7908 (let (got-identifier)
7909 (setq paren-depth 0)
7910 ;; Skip over type decl prefix operators, one for each iteration
7911 ;; of the while. These are, e.g. "*" in "int *foo" or "(" and
7912 ;; "*" in "int (*foo) (void)" (Note similar code in
7913 ;; `c-forward-decl-or-cast-1'.)
7914 (while
7915 (cond
7916 ((looking-at c-decl-hangon-key)
7917 (c-forward-keyword-clause 1))
7918 ((and c-opt-cpp-prefix
7919 (looking-at c-noise-macro-with-parens-name-re))
7920 (c-forward-noise-clause))
7921 ((and (looking-at c-type-decl-prefix-key)
7922 (if (and (c-major-mode-is 'c++-mode)
7923 (match-beginning 3))
7924 ;; If the third submatch matches in C++ then
7925 ;; we're looking at an identifier that's a
7926 ;; prefix only if it specifies a member pointer.
7927 (progn
7928 (setq id-start (point))
7929 (c-forward-name)
7930 (if (looking-at "\\(::\\)")
7931 ;; We only check for a trailing "::" and
7932 ;; let the "*" that should follow be
7933 ;; matched in the next round.
7935 ;; It turned out to be the real identifier,
7936 ;; so flag that and stop.
7937 (setq got-identifier t)
7938 nil))
7940 (if (looking-at c-type-decl-operator-prefix-key)
7941 (setq decorated t))
7942 (if (eq (char-after) ?\()
7943 (progn
7944 (setq paren-depth (1+ paren-depth))
7945 (forward-char))
7946 (goto-char (match-end 1)))
7947 (c-forward-syntactic-ws)
7948 t)))
7950 ;; If we haven't passed the identifier already, do it now.
7951 (unless got-identifier
7952 (setq id-start (point)))
7953 (cond
7954 ((or got-identifier
7955 (c-forward-name))
7956 (save-excursion
7957 (c-backward-syntactic-ws)
7958 (setq id-end (point))))
7959 (accept-anon
7960 (setq id-start nil id-end nil)
7962 (t (/= (point) here))))
7964 ;; Skip out of the parens surrounding the identifier. If closing
7965 ;; parens are missing, this form returns nil.
7966 (or (= paren-depth 0)
7967 (c-safe (goto-char (scan-lists (point) 1 paren-depth))))
7969 (<= (point) limit)
7971 ;; Skip over any trailing bit, such as "__attribute__".
7972 (progn
7973 (while (cond
7974 ((looking-at c-decl-hangon-key)
7975 (c-forward-keyword-clause 1))
7976 ((and c-opt-cpp-prefix
7977 (looking-at c-noise-macro-with-parens-name-re))
7978 (c-forward-noise-clause))))
7979 (<= (point) limit))
7981 ;; Search syntactically to the end of the declarator (";",
7982 ;; ",", a closing paren, eob etc) or to the beginning of an
7983 ;; initializer or function prototype ("=" or "\\s\(").
7984 ;; Note that square brackets are now not also treated as
7985 ;; initializers, since this broke when there were also
7986 ;; initializing brace lists.
7987 (let (found)
7988 (while
7989 (and (setq found (c-syntactic-re-search-forward
7990 "[;,]\\|\\s)\\|\\'\\|\\(=\\|\\s(\\)" limit t t))
7991 (eq (char-before) ?\[)
7992 (c-go-up-list-forward))
7993 (setq brackets-after-id t))
7994 (backward-char)
7995 found))
7996 (list id-start id-end brackets-after-id (match-beginning 1) decorated)
7998 (goto-char here)
7999 nil)))
8001 (defun c-forward-decl-or-cast-1 (preceding-token-end context last-cast-end)
8002 ;; Move forward over a declaration or a cast if at the start of one.
8003 ;; The point is assumed to be at the start of some token. Nil is
8004 ;; returned if no declaration or cast is recognized, and the point
8005 ;; is clobbered in that case.
8007 ;; If a declaration is parsed:
8009 ;; The point is left at the first token after the first complete
8010 ;; declarator, if there is one. The return value is a list of 4 elements,
8011 ;; where the first is the position of the first token in the declarator.
8012 ;; (See below for the other three.)
8013 ;; Some examples:
8015 ;; void foo (int a, char *b) stuff ...
8016 ;; car ^ ^ point
8017 ;; float (*a)[], b;
8018 ;; car ^ ^ point
8019 ;; unsigned int a = c_style_initializer, b;
8020 ;; car ^ ^ point
8021 ;; unsigned int a (cplusplus_style_initializer), b;
8022 ;; car ^ ^ point (might change)
8023 ;; class Foo : public Bar {}
8024 ;; car ^ ^ point
8025 ;; class PikeClass (int a, string b) stuff ...
8026 ;; car ^ ^ point
8027 ;; enum bool;
8028 ;; car ^ ^ point
8029 ;; enum bool flag;
8030 ;; car ^ ^ point
8031 ;; void cplusplus_function (int x) throw (Bad);
8032 ;; car ^ ^ point
8033 ;; Foo::Foo (int b) : Base (b) {}
8034 ;; car ^ ^ point
8036 ;; auto foo = 5;
8037 ;; car ^ ^ point
8038 ;; auto cplusplus_11 (int a, char *b) -> decltype (bar):
8039 ;; car ^ ^ point
8043 ;; The second element of the return value is non-nil when a
8044 ;; `c-typedef-decl-kwds' specifier is found in the declaration.
8045 ;; Specifically it is a dotted pair (A . B) where B is t when a
8046 ;; `c-typedef-kwds' ("typedef") is present, and A is t when some
8047 ;; other `c-typedef-decl-kwds' (e.g. class, struct, enum)
8048 ;; specifier is present. I.e., (some of) the declared
8049 ;; identifier(s) are types.
8051 ;; The third element of the return value is non-nil when the declaration
8052 ;; parsed might be an expression. The fourth element is the position of
8053 ;; the start of the type identifier.
8055 ;; If a cast is parsed:
8057 ;; The point is left at the first token after the closing paren of
8058 ;; the cast. The return value is `cast'. Note that the start
8059 ;; position must be at the first token inside the cast parenthesis
8060 ;; to recognize it.
8062 ;; PRECEDING-TOKEN-END is the first position after the preceding
8063 ;; token, i.e. on the other side of the syntactic ws from the point.
8064 ;; Use a value less than or equal to (point-min) if the point is at
8065 ;; the first token in (the visible part of) the buffer.
8067 ;; CONTEXT is a symbol that describes the context at the point:
8068 ;; 'decl In a comma-separated declaration context (typically
8069 ;; inside a function declaration arglist).
8070 ;; '<> In an angle bracket arglist.
8071 ;; 'arglist Some other type of arglist.
8072 ;; 'top Some other context and point is at the top-level (either
8073 ;; outside any braces or directly inside a class or namespace,
8074 ;; etc.)
8075 ;; nil Some other context or unknown context. Includes
8076 ;; within the parens of an if, for, ... construct.
8077 ;; 'not-decl This value is never supplied to this function. It
8078 ;; would mean we're definitely not in a declaration.
8080 ;; LAST-CAST-END is the first token after the closing paren of a
8081 ;; preceding cast, or nil if none is known. If
8082 ;; `c-forward-decl-or-cast-1' is used in succession, it should be
8083 ;; the position after the closest preceding call where a cast was
8084 ;; matched. In that case it's used to discover chains of casts like
8085 ;; "(a) (b) c".
8087 ;; This function records identifier ranges on
8088 ;; `c-record-type-identifiers' and `c-record-ref-identifiers' if
8089 ;; `c-record-type-identifiers' is non-nil.
8091 ;; This function might do hidden buffer changes.
8093 (let (;; `start-pos' is used below to point to the start of the
8094 ;; first type, i.e. after any leading specifiers. It might
8095 ;; also point at the beginning of the preceding syntactic
8096 ;; whitespace.
8097 (start-pos (point))
8098 ;; Set to the result of `c-forward-type'.
8099 at-type
8100 ;; The position of the first token in what we currently
8101 ;; believe is the type in the declaration or cast, after any
8102 ;; specifiers and their associated clauses.
8103 type-start
8104 ;; The position of the first token in what we currently
8105 ;; believe is the declarator for the first identifier. Set
8106 ;; when the type is found, and moved forward over any
8107 ;; `c-decl-hangon-kwds' and their associated clauses that
8108 ;; occurs after the type.
8109 id-start
8110 ;; These store `at-type', `type-start' and `id-start' of the
8111 ;; identifier before the one in those variables. The previous
8112 ;; identifier might turn out to be the real type in a
8113 ;; declaration if the last one has to be the declarator in it.
8114 ;; If `backup-at-type' is nil then the other variables have
8115 ;; undefined values.
8116 backup-at-type backup-type-start backup-id-start
8117 ;; This stores `kwd-sym' of the symbol before the current one.
8118 ;; This is needed to distinguish the C++11 version of "auto" from
8119 ;; the pre C++11 meaning.
8120 backup-kwd-sym
8121 ;; Set if we've found a specifier (apart from "typedef") that makes
8122 ;; the defined identifier(s) types.
8123 at-type-decl
8124 ;; Set if we've a "typedef" keyword.
8125 at-typedef
8126 ;; Set if we've found a specifier that can start a declaration
8127 ;; where there's no type.
8128 maybe-typeless
8129 ;; Save the value of kwd-sym between loops of the "Check for a
8130 ;; type" loop. Needed to distinguish a C++11 "auto" from a pre
8131 ;; C++11 one.
8132 prev-kwd-sym
8133 ;; If a specifier is found that also can be a type prefix,
8134 ;; these flags are set instead of those above. If we need to
8135 ;; back up an identifier, they are copied to the real flag
8136 ;; variables. Thus they only take effect if we fail to
8137 ;; interpret it as a type.
8138 backup-at-type-decl backup-maybe-typeless
8139 ;; Whether we've found a declaration or a cast. We might know
8140 ;; this before we've found the type in it. It's 'ids if we've
8141 ;; found two consecutive identifiers (usually a sure sign, but
8142 ;; we should allow that in labels too), and t if we've found a
8143 ;; specifier keyword (a 100% sure sign).
8144 at-decl-or-cast
8145 ;; Set when we need to back up to parse this as a declaration
8146 ;; but not as a cast.
8147 backup-if-not-cast
8148 ;; For casts, the return position.
8149 cast-end
8150 ;; Have we got a new-style C++11 "auto"?
8151 new-style-auto
8152 ;; Set when the symbol before `preceding-token-end' is known to
8153 ;; terminate the previous construct, or when we're at point-min.
8154 at-decl-start
8155 ;; Save `c-record-type-identifiers' and
8156 ;; `c-record-ref-identifiers' since ranges are recorded
8157 ;; speculatively and should be thrown away if it turns out
8158 ;; that it isn't a declaration or cast.
8159 (save-rec-type-ids c-record-type-identifiers)
8160 (save-rec-ref-ids c-record-ref-identifiers)
8161 ;; Set when we parse a declaration which might also be an expression,
8162 ;; such as "a *b". See CASE 16 and CASE 17.
8163 maybe-expression)
8165 (save-excursion
8166 (goto-char preceding-token-end)
8167 (setq at-decl-start
8168 (or (bobp)
8169 (let ((tok-end (point)))
8170 (c-backward-token-2)
8171 (member (buffer-substring-no-properties (point) tok-end)
8172 c-pre-start-tokens)))))
8174 (while (c-forward-annotation)
8175 (c-forward-syntactic-ws))
8177 ;; Check for a type. Unknown symbols are treated as possible
8178 ;; types, but they could also be specifiers disguised through
8179 ;; macros like __INLINE__, so we recognize both types and known
8180 ;; specifiers after them too.
8181 (while
8182 (let* ((start (point)) kwd-sym kwd-clause-end found-type noise-start)
8184 (cond
8185 ;; Look for a specifier keyword clause.
8186 ((or (looking-at c-prefix-spec-kwds-re)
8187 (and (c-major-mode-is 'java-mode)
8188 (looking-at "@[A-Za-z0-9]+")))
8189 (save-match-data
8190 (if (looking-at c-typedef-key)
8191 (setq at-typedef t)))
8192 (setq kwd-sym (c-keyword-sym (match-string 1)))
8193 (save-excursion
8194 (c-forward-keyword-clause 1)
8195 (setq kwd-clause-end (point))))
8196 ((and c-opt-cpp-prefix
8197 (looking-at c-noise-macro-with-parens-name-re))
8198 (setq noise-start (point))
8199 (c-forward-noise-clause)
8200 (setq kwd-clause-end (point))))
8202 (when (setq found-type (c-forward-type t)) ; brace-block-too
8203 ;; Found a known or possible type or a prefix of a known type.
8204 (when (and (c-major-mode-is 'c++-mode) ; C++11 style "auto"?
8205 (eq prev-kwd-sym (c-keyword-sym "auto"))
8206 (looking-at "[=(]")) ; FIXME!!! proper regexp.
8207 (setq new-style-auto t)
8208 (setq found-type nil)
8209 (goto-char start)) ; position of foo in "auto foo"
8211 (when at-type
8212 ;; Got two identifiers with nothing but whitespace
8213 ;; between them. That can only happen in declarations.
8214 (setq at-decl-or-cast 'ids)
8216 (when (eq at-type 'found)
8217 ;; If the previous identifier is a found type we
8218 ;; record it as a real one; it might be some sort of
8219 ;; alias for a prefix like "unsigned".
8220 (save-excursion
8221 (goto-char type-start)
8222 (let ((c-promote-possible-types t))
8223 (c-forward-type)))))
8225 (setq backup-at-type at-type
8226 backup-type-start type-start
8227 backup-id-start id-start
8228 backup-kwd-sym kwd-sym
8229 at-type found-type
8230 type-start start
8231 id-start (point)
8232 ;; The previous ambiguous specifier/type turned out
8233 ;; to be a type since we've parsed another one after
8234 ;; it, so clear these backup flags.
8235 backup-at-type-decl nil
8236 backup-maybe-typeless nil))
8238 (if (or kwd-sym noise-start)
8239 (progn
8240 ;; Handle known specifier keywords and
8241 ;; `c-decl-hangon-kwds' which can occur after known
8242 ;; types.
8244 (if (or (c-keyword-member kwd-sym 'c-decl-hangon-kwds)
8245 noise-start)
8246 ;; It's a hang-on keyword or noise clause that can occur
8247 ;; anywhere.
8248 (progn
8249 (if at-type
8250 ;; Move the identifier start position if
8251 ;; we've passed a type.
8252 (setq id-start kwd-clause-end)
8253 ;; Otherwise treat this as a specifier and
8254 ;; move the fallback position.
8255 (setq start-pos kwd-clause-end))
8256 (goto-char kwd-clause-end))
8258 ;; It's an ordinary specifier so we know that
8259 ;; anything before this can't be the type.
8260 (setq backup-at-type nil
8261 start-pos kwd-clause-end)
8263 (if found-type
8264 ;; It's ambiguous whether this keyword is a
8265 ;; specifier or a type prefix, so set the backup
8266 ;; flags. (It's assumed that `c-forward-type'
8267 ;; moved further than `c-forward-keyword-clause'.)
8268 (progn
8269 (when (c-keyword-member kwd-sym 'c-typedef-decl-kwds)
8270 (setq backup-at-type-decl t))
8271 (when (c-keyword-member kwd-sym 'c-typeless-decl-kwds)
8272 (setq backup-maybe-typeless t)))
8274 (when (c-keyword-member kwd-sym 'c-typedef-decl-kwds)
8275 ;; This test only happens after we've scanned a type.
8276 ;; So, with valid syntax, kwd-sym can't be 'typedef.
8277 (setq at-type-decl t))
8278 (when (c-keyword-member kwd-sym 'c-typeless-decl-kwds)
8279 (setq maybe-typeless t))
8281 ;; Haven't matched a type so it's an unambiguous
8282 ;; specifier keyword and we know we're in a
8283 ;; declaration.
8284 (setq at-decl-or-cast t)
8285 (setq prev-kwd-sym kwd-sym)
8287 (goto-char kwd-clause-end))))
8289 ;; If the type isn't known we continue so that we'll jump
8290 ;; over all specifiers and type identifiers. The reason
8291 ;; to do this for a known type prefix is to make things
8292 ;; like "unsigned INT16" work.
8293 (and found-type (not (eq found-type t))))))
8295 (cond
8296 ((eq at-type t)
8297 ;; If a known type was found, we still need to skip over any
8298 ;; hangon keyword clauses after it. Otherwise it has already
8299 ;; been done in the loop above.
8300 (while
8301 (cond ((looking-at c-decl-hangon-key)
8302 (c-forward-keyword-clause 1))
8303 ((and c-opt-cpp-prefix
8304 (looking-at c-noise-macro-with-parens-name-re))
8305 (c-forward-noise-clause))))
8306 (setq id-start (point)))
8308 ((eq at-type 'prefix)
8309 ;; A prefix type is itself a primitive type when it's not
8310 ;; followed by another type.
8311 (setq at-type t))
8313 ((not at-type)
8314 ;; Got no type but set things up to continue anyway to handle
8315 ;; the various cases when a declaration doesn't start with a
8316 ;; type.
8317 (setq id-start start-pos))
8319 ((and (eq at-type 'maybe)
8320 (c-major-mode-is 'c++-mode))
8321 ;; If it's C++ then check if the last "type" ends on the form
8322 ;; "foo::foo" or "foo::~foo", i.e. if it's the name of a
8323 ;; (con|de)structor.
8324 (save-excursion
8325 (let (name end-2 end-1)
8326 (goto-char id-start)
8327 (c-backward-syntactic-ws)
8328 (setq end-2 (point))
8329 (when (and
8330 (c-simple-skip-symbol-backward)
8331 (progn
8332 (setq name
8333 (buffer-substring-no-properties (point) end-2))
8334 ;; Cheating in the handling of syntactic ws below.
8335 (< (skip-chars-backward ":~ \t\n\r\v\f") 0))
8336 (progn
8337 (setq end-1 (point))
8338 (c-simple-skip-symbol-backward))
8339 (>= (point) type-start)
8340 (equal (buffer-substring-no-properties (point) end-1)
8341 name))
8342 ;; It is a (con|de)structor name. In that case the
8343 ;; declaration is typeless so zap out any preceding
8344 ;; identifier(s) that we might have taken as types.
8345 (goto-char type-start)
8346 (setq at-type nil
8347 backup-at-type nil
8348 id-start type-start))))))
8350 ;; Check for and step over a type decl expression after the thing
8351 ;; that is or might be a type. This can't be skipped since we
8352 ;; need the correct end position of the declarator for
8353 ;; `max-type-decl-end-*'.
8354 (let ((start (point)) (paren-depth 0) pos
8355 ;; True if there's a non-open-paren match of
8356 ;; `c-type-decl-prefix-key'.
8357 got-prefix
8358 ;; True if the declarator is surrounded by a parenthesis pair.
8359 got-parens
8360 ;; True if there is an identifier in the declarator.
8361 got-identifier
8362 ;; True if there's a non-close-paren match of
8363 ;; `c-type-decl-suffix-key'.
8364 got-suffix
8365 ;; True if there's a prefix match outside the outermost
8366 ;; paren pair that surrounds the declarator.
8367 got-prefix-before-parens
8368 ;; True if there's a suffix match outside the outermost
8369 ;; paren pair that surrounds the declarator. The value is
8370 ;; the position of the first suffix match.
8371 got-suffix-after-parens
8372 ;; True if we've parsed the type decl to a token that is
8373 ;; known to end declarations in this context.
8374 at-decl-end
8375 ;; The earlier values of `at-type' and `type-start' if we've
8376 ;; shifted the type backwards.
8377 identifier-type identifier-start
8378 ;; If `c-parse-and-markup-<>-arglists' is set we need to
8379 ;; turn it off during the name skipping below to avoid
8380 ;; getting `c-type' properties that might be bogus. That
8381 ;; can happen since we don't know if
8382 ;; `c-restricted-<>-arglists' will be correct inside the
8383 ;; arglist paren that gets entered.
8384 c-parse-and-markup-<>-arglists
8385 ;; Start of the identifier for which `got-identifier' was set.
8386 name-start
8387 ;; Position after (innermost) open parenthesis encountered in the
8388 ;; prefix operators.
8389 after-paren-pos)
8391 (goto-char id-start)
8393 ;; Skip over type decl prefix operators. (Note similar code in
8394 ;; `c-forward-declarator'.)
8395 (if (and c-recognize-typeless-decls
8396 (equal c-type-decl-prefix-key "\\<\\>"))
8397 (when (eq (char-after) ?\()
8398 (progn
8399 (setq paren-depth (1+ paren-depth))
8400 (forward-char)
8401 (setq after-paren-pos (point))))
8402 (while (and (looking-at c-type-decl-prefix-key)
8403 (if (and (c-major-mode-is 'c++-mode)
8404 (match-beginning 3))
8405 ;; If the third submatch matches in C++ then
8406 ;; we're looking at an identifier that's a
8407 ;; prefix only if it specifies a member pointer.
8408 (when (progn (setq pos (point))
8409 (setq got-identifier (c-forward-name)))
8410 (setq name-start pos)
8411 (if (looking-at "\\(::\\)")
8412 ;; We only check for a trailing "::" and
8413 ;; let the "*" that should follow be
8414 ;; matched in the next round.
8415 (progn (setq got-identifier nil) t)
8416 ;; It turned out to be the real identifier,
8417 ;; so stop.
8418 nil))
8421 (if (eq (char-after) ?\()
8422 (progn
8423 (setq paren-depth (1+ paren-depth))
8424 (forward-char)
8425 (setq after-paren-pos (point)))
8426 (unless got-prefix-before-parens
8427 (setq got-prefix-before-parens (= paren-depth 0)))
8428 (setq got-prefix t)
8429 (goto-char (match-end 1)))
8430 (c-forward-syntactic-ws)))
8432 (setq got-parens (> paren-depth 0))
8434 ;; Try to skip over an identifier.
8435 (or got-identifier
8436 (and (looking-at c-identifier-start)
8437 (setq pos (point))
8438 (setq got-identifier (c-forward-name))
8439 (setq name-start pos)))
8441 ;; Skip over type decl suffix operators and trailing noise macros.
8442 (while
8443 (cond
8444 ((and c-opt-cpp-prefix
8445 (looking-at c-noise-macro-with-parens-name-re))
8446 (c-forward-noise-clause))
8448 ((looking-at c-type-decl-suffix-key)
8449 (if (eq (char-after) ?\))
8450 (when (> paren-depth 0)
8451 (setq paren-depth (1- paren-depth))
8452 (forward-char)
8454 (when (if (save-match-data (looking-at "\\s("))
8455 (c-safe (c-forward-sexp 1) t)
8456 (goto-char (match-end 1))
8458 (when (and (not got-suffix-after-parens)
8459 (= paren-depth 0))
8460 (setq got-suffix-after-parens (match-beginning 0)))
8461 (setq got-suffix t))))
8464 ;; No suffix matched. We might have matched the
8465 ;; identifier as a type and the open paren of a
8466 ;; function arglist as a type decl prefix. In that
8467 ;; case we should "backtrack": Reinterpret the last
8468 ;; type as the identifier, move out of the arglist and
8469 ;; continue searching for suffix operators.
8471 ;; Do this even if there's no preceding type, to cope
8472 ;; with old style function declarations in K&R C,
8473 ;; (con|de)structors in C++ and `c-typeless-decl-kwds'
8474 ;; style declarations. That isn't applicable in an
8475 ;; arglist context, though.
8476 (when (and (= paren-depth 1)
8477 (not got-prefix-before-parens)
8478 (not (eq at-type t))
8479 (or backup-at-type
8480 maybe-typeless
8481 backup-maybe-typeless
8482 (when c-recognize-typeless-decls
8483 (and (memq context '(nil top))
8484 ;; Deal with C++11's "copy-initialization"
8485 ;; where we have <type>(<constant>), by
8486 ;; contrasting with a typeless
8487 ;; <name>(<type><parameter>, ...).
8488 (save-excursion
8489 (goto-char after-paren-pos)
8490 (c-forward-syntactic-ws)
8491 (c-forward-type)))))
8492 (setq pos (c-up-list-forward (point)))
8493 (eq (char-before pos) ?\)))
8494 (c-fdoc-shift-type-backward)
8495 (goto-char pos)
8496 t)))
8498 (c-forward-syntactic-ws))
8500 (when (or (and new-style-auto
8501 (looking-at c-auto-ops-re))
8502 (and (or maybe-typeless backup-maybe-typeless)
8503 (not got-identifier)
8504 (not got-prefix)
8505 at-type))
8506 ;; Have found no identifier but `c-typeless-decl-kwds' has
8507 ;; matched so we know we're inside a declaration. The
8508 ;; preceding type must be the identifier instead.
8509 (c-fdoc-shift-type-backward))
8511 ;; Prepare the "-> type;" for fontification later on.
8512 (when (and new-style-auto
8513 (looking-at c-haskell-op-re))
8514 (save-excursion
8515 (goto-char (match-end 0))
8516 (c-forward-syntactic-ws)
8517 (setq type-start (point))
8518 (setq at-type (c-forward-type))))
8520 ;; Move forward over any "WS" ids (like "final" or "override" in C++)
8521 (while (looking-at c-type-decl-suffix-ws-ids-key)
8522 (goto-char (match-end 1))
8523 (c-forward-syntactic-ws))
8525 (setq
8526 at-decl-or-cast
8527 (catch 'at-decl-or-cast
8529 ;; CASE 1
8530 (when (> paren-depth 0)
8531 ;; Encountered something inside parens that isn't matched by
8532 ;; the `c-type-decl-*' regexps, so it's not a type decl
8533 ;; expression. Try to skip out to the same paren depth to
8534 ;; not confuse the cast check below. If we don't manage this and
8535 ;; `at-decl-or-cast' is 'ids we might have an expression like
8536 ;; "foo bar ({ ..." which is a valid C++11 initialization.
8537 (if (and (not (c-safe (goto-char (scan-lists (point) 1 paren-depth))))
8538 (eq at-decl-or-cast 'ids))
8539 (c-fdoc-shift-type-backward))
8540 ;; If we've found a specifier keyword then it's a
8541 ;; declaration regardless.
8542 (throw 'at-decl-or-cast (memq at-decl-or-cast '(t ids))))
8544 (setq at-decl-end
8545 (looking-at (cond ((eq context '<>) "[,>]")
8546 ((not (memq context '(nil top))) "[,\)]")
8547 (t "[,;]"))))
8549 ;; Now we've collected info about various characteristics of
8550 ;; the construct we're looking at. Below follows a decision
8551 ;; tree based on that. It's ordered to check more certain
8552 ;; signs before less certain ones.
8554 (if got-identifier
8555 (progn
8557 ;; CASE 2
8558 (when (and (or at-type maybe-typeless)
8559 (not (or got-prefix got-parens)))
8560 ;; Got another identifier directly after the type, so it's a
8561 ;; declaration.
8562 (throw 'at-decl-or-cast t))
8564 (when (and got-parens
8565 (not got-prefix)
8566 ;; (not got-suffix-after-parens)
8567 (or backup-at-type
8568 maybe-typeless
8569 backup-maybe-typeless
8570 (eq at-decl-or-cast t)
8571 ;; Check whether we have "bar (gnu);" where we
8572 ;; are directly inside a class (etc.) called "bar".
8573 (save-excursion
8574 (and
8575 (progn
8576 (goto-char name-start)
8577 (not (memq (c-forward-type) '(nil maybe))))
8578 (progn
8579 (goto-char id-start)
8580 (c-directly-in-class-called-p
8581 (buffer-substring
8582 type-start
8583 (progn
8584 (goto-char type-start)
8585 (c-forward-type)
8586 (c-backward-syntactic-ws)
8587 (point)))))))))
8588 ;; Got a declaration of the form "foo bar (gnu);" or "bar
8589 ;; (gnu);" where we've recognized "bar" as the type and "gnu"
8590 ;; as the declarator, and in the latter case, checked that
8591 ;; "bar (gnu)" appears directly inside the class "bar". In
8592 ;; this case it's however more likely that "bar" is the
8593 ;; declarator and "gnu" a function argument or initializer
8594 ;; (if `c-recognize-paren-inits' is set), since the parens
8595 ;; around "gnu" would be superfluous if it's a declarator.
8596 ;; Shift the type one step backward.
8597 (c-fdoc-shift-type-backward)))
8599 ;; Found no identifier.
8601 (if backup-at-type
8602 (progn
8604 ;; CASE 3
8605 (when (= (point) start)
8606 ;; Got a plain list of identifiers. If a colon follows it's
8607 ;; a valid label, or maybe a bitfield. Otherwise the last
8608 ;; one probably is the declared identifier and we should
8609 ;; back up to the previous type, providing it isn't a cast.
8610 (if (and (eq (char-after) ?:)
8611 (not (c-major-mode-is 'java-mode)))
8612 (cond
8613 ;; If we've found a specifier keyword then it's a
8614 ;; declaration regardless.
8615 ((eq at-decl-or-cast t)
8616 (throw 'at-decl-or-cast t))
8617 ((and c-has-bitfields
8618 (eq at-decl-or-cast 'ids)) ; bitfield.
8619 (setq backup-if-not-cast t)
8620 (throw 'at-decl-or-cast t)))
8622 (setq backup-if-not-cast t)
8623 (throw 'at-decl-or-cast t)))
8625 ;; CASE 4
8626 (when (and got-suffix
8627 (not got-prefix)
8628 (not got-parens))
8629 ;; Got a plain list of identifiers followed by some suffix.
8630 ;; If this isn't a cast then the last identifier probably is
8631 ;; the declared one and we should back up to the previous
8632 ;; type.
8633 (setq backup-if-not-cast t)
8634 (throw 'at-decl-or-cast t)))
8636 ;; CASE 5
8637 (when (eq at-type t)
8638 ;; If the type is known we know that there can't be any
8639 ;; identifier somewhere else, and it's only in declarations in
8640 ;; e.g. function prototypes and in casts that the identifier may
8641 ;; be left out.
8642 (throw 'at-decl-or-cast t))
8644 (when (= (point) start)
8645 ;; Only got a single identifier (parsed as a type so far).
8646 ;; CASE 6
8647 (if (and
8648 ;; Check that the identifier isn't at the start of an
8649 ;; expression.
8650 at-decl-end
8651 (cond
8652 ((eq context 'decl)
8653 ;; Inside an arglist that contains declarations. If K&R
8654 ;; style declarations and parenthesis style initializers
8655 ;; aren't allowed then the single identifier must be a
8656 ;; type, else we require that it's known or found
8657 ;; (primitive types are handled above).
8658 (or (and (not c-recognize-knr-p)
8659 (not c-recognize-paren-inits))
8660 (memq at-type '(known found))))
8661 ((eq context '<>)
8662 ;; Inside a template arglist. Accept known and found
8663 ;; types; other identifiers could just as well be
8664 ;; constants in C++.
8665 (memq at-type '(known found)))))
8666 (throw 'at-decl-or-cast t)
8667 ;; CASE 7
8668 ;; Can't be a valid declaration or cast, but if we've found a
8669 ;; specifier it can't be anything else either, so treat it as
8670 ;; an invalid/unfinished declaration or cast.
8671 (throw 'at-decl-or-cast at-decl-or-cast))))
8673 (if (and got-parens
8674 (not got-prefix)
8675 (memq context '(nil top))
8676 (not (eq at-type t))
8677 (or backup-at-type
8678 maybe-typeless
8679 backup-maybe-typeless
8680 (when c-recognize-typeless-decls
8681 (or (not got-suffix)
8682 (not (looking-at
8683 c-after-suffixed-type-maybe-decl-key))))))
8684 ;; Got an empty paren pair and a preceding type that probably
8685 ;; really is the identifier. Shift the type backwards to make
8686 ;; the last one the identifier. This is analogous to the
8687 ;; "backtracking" done inside the `c-type-decl-suffix-key' loop
8688 ;; above.
8690 ;; Exception: In addition to the conditions in that
8691 ;; "backtracking" code, do not shift backward if we're not
8692 ;; looking at either `c-after-suffixed-type-decl-key' or "[;,]".
8693 ;; Since there's no preceding type, the shift would mean that
8694 ;; the declaration is typeless. But if the regexp doesn't match
8695 ;; then we will simply fall through in the tests below and not
8696 ;; recognize it at all, so it's better to try it as an abstract
8697 ;; declarator instead.
8698 (c-fdoc-shift-type-backward)
8700 ;; Still no identifier.
8701 ;; CASE 8
8702 (when (and got-prefix (or got-parens got-suffix))
8703 ;; Require `got-prefix' together with either `got-parens' or
8704 ;; `got-suffix' to recognize it as an abstract declarator:
8705 ;; `got-parens' only is probably an empty function call.
8706 ;; `got-suffix' only can build an ordinary expression together
8707 ;; with the preceding identifier which we've taken as a type.
8708 ;; We could actually accept on `got-prefix' only, but that can
8709 ;; easily occur temporarily while writing an expression so we
8710 ;; avoid that case anyway. We could do a better job if we knew
8711 ;; the point when the fontification was invoked.
8712 (throw 'at-decl-or-cast t))
8714 ;; CASE 9
8715 (when (and at-type
8716 (not got-prefix)
8717 (not got-parens)
8718 got-suffix-after-parens
8719 (eq (char-after got-suffix-after-parens) ?\())
8720 ;; Got a type, no declarator but a paren suffix. I.e. it's a
8721 ;; normal function call after all (or perhaps a C++ style object
8722 ;; instantiation expression).
8723 (throw 'at-decl-or-cast nil))))
8725 ;; CASE 9.5
8726 (when (and (not context) ; i.e. not at top level.
8727 (c-major-mode-is 'c++-mode)
8728 (eq at-decl-or-cast 'ids)
8729 after-paren-pos)
8730 ;; We've got something like "foo bar (...)" in C++ which isn't at
8731 ;; the top level. This is probably a uniform initialization of bar
8732 ;; to the contents of the parens. In this case the declarator ends
8733 ;; at the open paren.
8734 (goto-char (1- after-paren-pos))
8735 (throw 'at-decl-or-cast t))
8737 ;; CASE 10
8738 (when at-decl-or-cast
8739 ;; By now we've located the type in the declaration that we know
8740 ;; we're in.
8741 (throw 'at-decl-or-cast t))
8743 ;; CASE 11
8744 (when (and got-identifier
8745 (looking-at c-after-suffixed-type-decl-key)
8746 (or (eq context 'top)
8747 (and (eq context nil)
8748 (match-beginning 1)))
8749 (if (and got-parens
8750 (not got-prefix)
8751 (not got-suffix)
8752 (not (eq at-type t)))
8753 ;; Shift the type backward in the case that there's a
8754 ;; single identifier inside parens. That can only
8755 ;; occur in K&R style function declarations so it's
8756 ;; more likely that it really is a function call.
8757 ;; Therefore we only do this after
8758 ;; `c-after-suffixed-type-decl-key' has matched.
8759 (progn (c-fdoc-shift-type-backward) t)
8760 got-suffix-after-parens))
8761 ;; A declaration according to `c-after-suffixed-type-decl-key'.
8762 (throw 'at-decl-or-cast t))
8764 ;; CASE 12
8765 (when (and (or got-prefix (not got-parens))
8766 (memq at-type '(t known)))
8767 ;; It's a declaration if a known type precedes it and it can't be a
8768 ;; function call.
8769 (throw 'at-decl-or-cast t))
8771 ;; If we get here we can't tell if this is a type decl or a normal
8772 ;; expression by looking at it alone. (That's under the assumption
8773 ;; that normal expressions always can look like type decl expressions,
8774 ;; which isn't really true but the cases where it doesn't hold are so
8775 ;; uncommon (e.g. some placements of "const" in C++) it's not worth
8776 ;; the effort to look for them.)
8778 ;;; 2008-04-16: commented out the next form, to allow the function to recognize
8779 ;;; "foo (int bar)" in CC (an implicit type (in class foo) without a semicolon)
8780 ;;; as a(n almost complete) declaration, enabling it to be fontified.
8781 ;; CASE 13
8782 ;; (unless (or at-decl-end (looking-at "=[^=]"))
8783 ;; If this is a declaration it should end here or its initializer(*)
8784 ;; should start here, so check for allowed separation tokens. Note
8785 ;; that this rule doesn't work e.g. with a K&R arglist after a
8786 ;; function header.
8788 ;; *) Don't check for C++ style initializers using parens
8789 ;; since those already have been matched as suffixes.
8791 ;; If `at-decl-or-cast' is then we've found some other sign that
8792 ;; it's a declaration or cast, so then it's probably an
8793 ;; invalid/unfinished one.
8794 ;; (throw 'at-decl-or-cast at-decl-or-cast))
8796 ;; Below are tests that only should be applied when we're certain to
8797 ;; not have parsed halfway through an expression.
8799 ;; CASE 14
8800 (when (memq at-type '(t known))
8801 ;; The expression starts with a known type so treat it as a
8802 ;; declaration.
8803 (throw 'at-decl-or-cast t))
8805 ;; CASE 15
8806 (when (and (c-major-mode-is 'c++-mode)
8807 ;; In C++ we check if the identifier is a known type, since
8808 ;; (con|de)structors use the class name as identifier.
8809 ;; We've always shifted over the identifier as a type and
8810 ;; then backed up again in this case.
8811 identifier-type
8812 (or (memq identifier-type '(found known))
8813 (and (eq (char-after identifier-start) ?~)
8814 ;; `at-type' probably won't be 'found for
8815 ;; destructors since the "~" is then part of the
8816 ;; type name being checked against the list of
8817 ;; known types, so do a check without that
8818 ;; operator.
8819 (or (save-excursion
8820 (goto-char (1+ identifier-start))
8821 (c-forward-syntactic-ws)
8822 (c-with-syntax-table
8823 c-identifier-syntax-table
8824 (looking-at c-known-type-key)))
8825 (save-excursion
8826 (goto-char (1+ identifier-start))
8827 ;; We have already parsed the type earlier,
8828 ;; so it'd be possible to cache the end
8829 ;; position instead of redoing it here, but
8830 ;; then we'd need to keep track of another
8831 ;; position everywhere.
8832 (c-check-type (point)
8833 (progn (c-forward-type)
8834 (point))))))))
8835 (throw 'at-decl-or-cast t))
8837 (if got-identifier
8838 (progn
8839 ;; CASE 16
8840 (when (and got-prefix-before-parens
8841 at-type
8842 (or at-decl-end (looking-at "=[^=]"))
8843 (memq context '(nil top))
8844 (or (not got-suffix)
8845 at-decl-start))
8846 ;; Got something like "foo * bar;". Since we're not inside
8847 ;; an arglist it would be a meaningless expression because
8848 ;; the result isn't used. We therefore choose to recognize
8849 ;; it as a declaration. We only allow a suffix (which makes
8850 ;; the construct look like a function call) when
8851 ;; `at-decl-start' provides additional evidence that we do
8852 ;; have a declaration.
8853 (setq maybe-expression t)
8854 (throw 'at-decl-or-cast t))
8856 ;; CASE 17
8857 (when (and (or got-suffix-after-parens
8858 (looking-at "=[^=]"))
8859 (eq at-type 'found)
8860 (not (eq context 'arglist)))
8861 ;; Got something like "a (*b) (c);" or "a (b) = c;". It could
8862 ;; be an odd expression or it could be a declaration. Treat
8863 ;; it as a declaration if "a" has been used as a type
8864 ;; somewhere else (if it's a known type we won't get here).
8865 (setq maybe-expression t)
8866 (throw 'at-decl-or-cast t)))
8868 ;; CASE 18
8869 (when (and (not (memq context '(nil top)))
8870 (or got-prefix
8871 (and (eq context 'decl)
8872 (not c-recognize-paren-inits)
8873 (or got-parens got-suffix))))
8874 ;; Got a type followed by an abstract declarator. If `got-prefix'
8875 ;; is set it's something like "a *" without anything after it. If
8876 ;; `got-parens' or `got-suffix' is set it's "a()", "a[]", "a()[]",
8877 ;; or similar, which we accept only if the context rules out
8878 ;; expressions.
8879 (throw 'at-decl-or-cast t)))
8881 ;; If we had a complete symbol table here (which rules out
8882 ;; `c-found-types') we should return t due to the disambiguation rule
8883 ;; (in at least C++) that anything that can be parsed as a declaration
8884 ;; is a declaration. Now we're being more defensive and prefer to
8885 ;; highlight things like "foo (bar);" as a declaration only if we're
8886 ;; inside an arglist that contains declarations.
8887 ;; CASE 19
8888 (eq context 'decl))))
8890 ;; The point is now after the type decl expression.
8892 (cond
8893 ;; Check for a cast.
8894 ((save-excursion
8895 (and
8896 c-cast-parens
8898 ;; Should be the first type/identifier in a cast paren.
8899 (> preceding-token-end (point-min))
8900 (memq (char-before preceding-token-end) c-cast-parens)
8902 ;; The closing paren should follow.
8903 (progn
8904 (c-forward-syntactic-ws)
8905 (looking-at "\\s)"))
8907 ;; There should be a primary expression after it.
8908 (let (pos)
8909 (forward-char)
8910 (c-forward-syntactic-ws)
8911 (setq cast-end (point))
8912 (and (looking-at c-primary-expr-regexp)
8913 (progn
8914 (setq pos (match-end 0))
8916 ;; Check if the expression begins with a prefix keyword.
8917 (match-beginning 2)
8918 (if (match-beginning 1)
8919 ;; Expression begins with an ambiguous operator. Treat
8920 ;; it as a cast if it's a type decl or if we've
8921 ;; recognized the type somewhere else.
8922 (or at-decl-or-cast
8923 (memq at-type '(t known found)))
8924 ;; Unless it's a keyword, it's the beginning of a primary
8925 ;; expression.
8926 (not (looking-at c-keywords-regexp)))))
8927 ;; If `c-primary-expr-regexp' matched a nonsymbol token, check
8928 ;; that it matched a whole one so that we don't e.g. confuse
8929 ;; the operator '-' with '->'. It's ok if it matches further,
8930 ;; though, since it e.g. can match the float '.5' while the
8931 ;; operator regexp only matches '.'.
8932 (or (not (looking-at c-nonsymbol-token-regexp))
8933 (<= (match-end 0) pos))))
8935 ;; There should either be a cast before it or something that isn't an
8936 ;; identifier or close paren.
8937 (> preceding-token-end (point-min))
8938 (progn
8939 (goto-char (1- preceding-token-end))
8940 (or (eq (point) last-cast-end)
8941 (progn
8942 (c-backward-syntactic-ws)
8943 (if (< (skip-syntax-backward "w_") 0)
8944 ;; It's a symbol. Accept it only if it's one of the
8945 ;; keywords that can precede an expression (without
8946 ;; surrounding parens).
8947 (looking-at c-simple-stmt-key)
8948 (and
8949 ;; Check that it isn't a close paren (block close is ok,
8950 ;; though).
8951 (not (memq (char-before) '(?\) ?\])))
8952 ;; Check that it isn't a nonsymbol identifier.
8953 (not (c-on-identifier)))))))))
8955 ;; Handle the cast.
8956 (when (and c-record-type-identifiers at-type (not (eq at-type t)))
8957 (let ((c-promote-possible-types t))
8958 (goto-char type-start)
8959 (c-forward-type)))
8961 (goto-char cast-end)
8962 'cast)
8964 (at-decl-or-cast
8965 ;; We're at a declaration. Highlight the type and the following
8966 ;; declarators.
8968 (when backup-if-not-cast
8969 (c-fdoc-shift-type-backward t))
8971 (when (and (eq context 'decl) (looking-at ","))
8972 ;; Make sure to propagate the `c-decl-arg-start' property to
8973 ;; the next argument if it's set in this one, to cope with
8974 ;; interactive refontification.
8975 (c-put-c-type-property (point) 'c-decl-arg-start))
8977 ;; Record the type's coordinates in `c-record-type-identifiers' for
8978 ;; later fontification.
8979 (when (and c-record-type-identifiers at-type ;; (not (eq at-type t))
8980 ;; There seems no reason to exclude a token from
8981 ;; fontification just because it's "a known type that can't
8982 ;; be a name or other expression". 2013-09-18.
8984 (let ((c-promote-possible-types t))
8985 (save-excursion
8986 (goto-char type-start)
8987 (c-forward-type))))
8989 (list id-start
8990 (and (or at-type-decl at-typedef)
8991 (cons at-type-decl at-typedef))
8992 maybe-expression
8993 type-start))
8996 ;; False alarm. Restore the recorded ranges.
8997 (setq c-record-type-identifiers save-rec-type-ids
8998 c-record-ref-identifiers save-rec-ref-ids)
8999 nil))))
9001 (defun c-forward-label (&optional assume-markup preceding-token-end limit)
9002 ;; Assuming that point is at the beginning of a token, check if it starts a
9003 ;; label and if so move over it and return non-nil (t in default situations,
9004 ;; specific symbols (see below) for interesting situations), otherwise don't
9005 ;; move and return nil. "Label" here means "most things with a colon".
9007 ;; More precisely, a "label" is regarded as one of:
9008 ;; (i) a goto target like "foo:" - returns the symbol `goto-target';
9009 ;; (ii) A case label - either the entire construct "case FOO:", or just the
9010 ;; bare "case", should the colon be missing. We return t;
9011 ;; (iii) a keyword which needs a colon, like "default:" or "private:"; We
9012 ;; return t;
9013 ;; (iv) One of QT's "extended" C++ variants of
9014 ;; "private:"/"protected:"/"public:"/"more:" looking like "public slots:".
9015 ;; Returns the symbol `qt-2kwds-colon'.
9016 ;; (v) QT's construct "signals:". Returns the symbol `qt-1kwd-colon'.
9017 ;; (vi) One of the keywords matched by `c-opt-extra-label-key' (without any
9018 ;; colon). Currently (2006-03), this applies only to Objective C's
9019 ;; keywords "@private", "@protected", and "@public". Returns t.
9021 ;; One of the things which will NOT be recognized as a label is a bit-field
9022 ;; element of a struct, something like "int foo:5".
9024 ;; The end of the label is taken to be just after the colon, or the end of
9025 ;; the first submatch in `c-opt-extra-label-key'. The point is directly
9026 ;; after the end on return. The terminating char gets marked with
9027 ;; `c-decl-end' to improve recognition of the following declaration or
9028 ;; statement.
9030 ;; If ASSUME-MARKUP is non-nil, it's assumed that the preceding
9031 ;; label, if any, has already been marked up like that.
9033 ;; If PRECEDING-TOKEN-END is given, it should be the first position
9034 ;; after the preceding token, i.e. on the other side of the
9035 ;; syntactic ws from the point. Use a value less than or equal to
9036 ;; (point-min) if the point is at the first token in (the visible
9037 ;; part of) the buffer.
9039 ;; The optional LIMIT limits the forward scan for the colon.
9041 ;; This function records the ranges of the label symbols on
9042 ;; `c-record-ref-identifiers' if `c-record-type-identifiers' (!) is
9043 ;; non-nil.
9045 ;; This function might do hidden buffer changes.
9047 (let ((start (point))
9048 label-end
9049 qt-symbol-idx
9050 macro-start ; if we're in one.
9051 label-type
9052 kwd)
9053 (cond
9054 ;; "case" or "default" (Doesn't apply to AWK).
9055 ((looking-at c-label-kwds-regexp)
9056 (let ((kwd-end (match-end 1)))
9057 ;; Record only the keyword itself for fontification, since in
9058 ;; case labels the following is a constant expression and not
9059 ;; a label.
9060 (when c-record-type-identifiers
9061 (c-record-ref-id (cons (match-beginning 1) kwd-end)))
9063 ;; Find the label end.
9064 (goto-char kwd-end)
9065 (setq label-type
9066 (if (and (c-syntactic-re-search-forward
9067 ;; Stop on chars that aren't allowed in expressions,
9068 ;; and on operator chars that would be meaningless
9069 ;; there. FIXME: This doesn't cope with ?: operators.
9070 "[;{=,@]\\|\\(\\=\\|[^:]\\):\\([^:]\\|\\'\\)"
9071 limit t t nil 1)
9072 (match-beginning 2))
9074 (progn ; there's a proper :
9075 (goto-char (match-beginning 2)) ; just after the :
9076 (c-put-c-type-property (1- (point)) 'c-decl-end)
9079 ;; It's an unfinished label. We consider the keyword enough
9080 ;; to recognize it as a label, so that it gets fontified.
9081 ;; Leave the point at the end of it, but don't put any
9082 ;; `c-decl-end' marker.
9083 (goto-char kwd-end)
9084 t))))
9086 ;; @private, @protected, @public, in Objective C, or similar.
9087 ((and c-opt-extra-label-key
9088 (looking-at c-opt-extra-label-key))
9089 ;; For a `c-opt-extra-label-key' match, we record the whole
9090 ;; thing for fontification. That's to get the leading '@' in
9091 ;; Objective-C protection labels fontified.
9092 (goto-char (match-end 1))
9093 (when c-record-type-identifiers
9094 (c-record-ref-id (cons (match-beginning 1) (point))))
9095 (c-put-c-type-property (1- (point)) 'c-decl-end)
9096 (setq label-type t))
9098 ;; All other cases of labels.
9099 ((and c-recognize-colon-labels ; nil for AWK and IDL, otherwise t.
9101 ;; A colon label must have something before the colon.
9102 (not (eq (char-after) ?:))
9104 ;; Check that we're not after a token that can't precede a label.
9106 ;; Trivially succeeds when there's no preceding token.
9107 ;; Succeeds when we're at a virtual semicolon.
9108 (if preceding-token-end
9109 (<= preceding-token-end (point-min))
9110 (save-excursion
9111 (c-backward-syntactic-ws)
9112 (setq preceding-token-end (point))
9113 (or (bobp)
9114 (c-at-vsemi-p))))
9116 ;; Check if we're after a label, if we're after a closing
9117 ;; paren that belong to statement, and with
9118 ;; `c-label-prefix-re'. It's done in different order
9119 ;; depending on `assume-markup' since the checks have
9120 ;; different expensiveness.
9121 (if assume-markup
9123 (eq (c-get-char-property (1- preceding-token-end) 'c-type)
9124 'c-decl-end)
9126 (save-excursion
9127 (goto-char (1- preceding-token-end))
9128 (c-beginning-of-current-token)
9129 (or (looking-at c-label-prefix-re)
9130 (looking-at c-block-stmt-1-key)))
9132 (and (eq (char-before preceding-token-end) ?\))
9133 (c-after-conditional)))
9136 (save-excursion
9137 (goto-char (1- preceding-token-end))
9138 (c-beginning-of-current-token)
9139 (or (looking-at c-label-prefix-re)
9140 (looking-at c-block-stmt-1-key)))
9142 (cond
9143 ((eq (char-before preceding-token-end) ?\))
9144 (c-after-conditional))
9146 ((eq (char-before preceding-token-end) ?:)
9147 ;; Might be after another label, so check it recursively.
9148 (save-restriction
9149 (save-excursion
9150 (goto-char (1- preceding-token-end))
9151 ;; Essentially the same as the
9152 ;; `c-syntactic-re-search-forward' regexp below.
9153 (setq macro-start
9154 (save-excursion (and (c-beginning-of-macro)
9155 (point))))
9156 (if macro-start (narrow-to-region macro-start (point-max)))
9157 (c-syntactic-skip-backward "^-]:?;}=*/%&|,<>!@+" nil t)
9158 ;; Note: the following should work instead of the
9159 ;; narrow-to-region above. Investigate why not,
9160 ;; sometime. ACM, 2006-03-31.
9161 ;; (c-syntactic-skip-backward "^-]:?;}=*/%&|,<>!@+"
9162 ;; macro-start t)
9163 (let ((pte (point))
9164 ;; If the caller turned on recording for us,
9165 ;; it shouldn't apply when we check the
9166 ;; preceding label.
9167 c-record-type-identifiers)
9168 ;; A label can't start at a cpp directive. Check for
9169 ;; this, since c-forward-syntactic-ws would foul up on it.
9170 (unless (and c-opt-cpp-prefix (looking-at c-opt-cpp-prefix))
9171 (c-forward-syntactic-ws)
9172 (c-forward-label nil pte start))))))))))
9174 ;; Point is still at the beginning of the possible label construct.
9176 ;; Check that the next nonsymbol token is ":", or that we're in one
9177 ;; of QT's "slots" declarations. Allow '(' for the sake of macro
9178 ;; arguments. FIXME: Should build this regexp from the language
9179 ;; constants.
9180 (cond
9181 ;; public: protected: private:
9182 ((and
9183 (c-major-mode-is 'c++-mode)
9184 (search-forward-regexp
9185 "\\=p\\(r\\(ivate\\|otected\\)\\|ublic\\)\\>[^_]" nil t)
9186 (progn (backward-char)
9187 (c-forward-syntactic-ws limit)
9188 (looking-at ":\\([^:]\\|\\'\\)"))) ; A single colon.
9189 (forward-char)
9190 (setq label-type t))
9191 ;; QT double keyword like "protected slots:" or goto target.
9192 ((progn (goto-char start) nil))
9193 ((when (c-syntactic-re-search-forward
9194 "[ \t\n[:?;{=*/%&|,<>!@+-]" limit t t) ; not at EOB
9195 (backward-char)
9196 (setq label-end (point))
9197 (setq qt-symbol-idx
9198 (and (c-major-mode-is 'c++-mode)
9199 (string-match
9200 "\\(p\\(r\\(ivate\\|otected\\)\\|ublic\\)\\|more\\)\\>"
9201 (buffer-substring start (point)))))
9202 (c-forward-syntactic-ws limit)
9203 (cond
9204 ((looking-at ":\\([^:]\\|\\'\\)") ; A single colon.
9205 (forward-char)
9206 (setq label-type
9207 (if (or (string= "signals" ; Special QT macro
9208 (setq kwd (buffer-substring-no-properties start label-end)))
9209 (string= "Q_SIGNALS" kwd))
9210 'qt-1kwd-colon
9211 'goto-target)))
9212 ((and qt-symbol-idx
9213 (search-forward-regexp "\\=\\(slots\\|Q_SLOTS\\)\\>" limit t)
9214 (progn (c-forward-syntactic-ws limit)
9215 (looking-at ":\\([^:]\\|\\'\\)"))) ; A single colon
9216 (forward-char)
9217 (setq label-type 'qt-2kwds-colon)))))))
9219 (save-restriction
9220 (narrow-to-region start (point))
9222 ;; Check that `c-nonlabel-token-key' doesn't match anywhere.
9223 (catch 'check-label
9224 (goto-char start)
9225 (while (progn
9226 (when (looking-at c-nonlabel-token-key)
9227 (goto-char start)
9228 (setq label-type nil)
9229 (throw 'check-label nil))
9230 (and (c-safe (c-forward-sexp)
9231 (c-forward-syntactic-ws)
9233 (not (eobp)))))
9235 ;; Record the identifiers in the label for fontification, unless
9236 ;; it begins with `c-label-kwds' in which case the following
9237 ;; identifiers are part of a (constant) expression that
9238 ;; shouldn't be fontified.
9239 (when (and c-record-type-identifiers
9240 (progn (goto-char start)
9241 (not (looking-at c-label-kwds-regexp))))
9242 (while (c-syntactic-re-search-forward c-symbol-key nil t)
9243 (c-record-ref-id (cons (match-beginning 0)
9244 (match-end 0)))))
9246 (c-put-c-type-property (1- (point-max)) 'c-decl-end)
9247 (goto-char (point-max)))))
9250 ;; Not a label.
9251 (goto-char start)))
9252 label-type))
9254 (defun c-forward-objc-directive ()
9255 ;; Assuming the point is at the beginning of a token, try to move
9256 ;; forward to the end of the Objective-C directive that starts
9257 ;; there. Return t if a directive was fully recognized, otherwise
9258 ;; the point is moved as far as one could be successfully parsed and
9259 ;; nil is returned.
9261 ;; This function records identifier ranges on
9262 ;; `c-record-type-identifiers' and `c-record-ref-identifiers' if
9263 ;; `c-record-type-identifiers' is non-nil.
9265 ;; This function might do hidden buffer changes.
9267 (let ((start (point))
9268 start-char
9269 (c-promote-possible-types t)
9271 ;; Turn off recognition of angle bracket arglists while parsing
9272 ;; types here since the protocol reference list might then be
9273 ;; considered part of the preceding name or superclass-name.
9274 c-recognize-<>-arglists)
9276 (if (or
9277 (when (looking-at
9278 (eval-when-compile
9279 (c-make-keywords-re t
9280 (append (c-lang-const c-protection-kwds objc)
9281 '("@end"))
9282 'objc-mode)))
9283 (goto-char (match-end 1))
9286 (and
9287 (looking-at
9288 (eval-when-compile
9289 (c-make-keywords-re t
9290 '("@interface" "@implementation" "@protocol")
9291 'objc-mode)))
9293 ;; Handle the name of the class itself.
9294 (progn
9295 ;; (c-forward-token-2) ; 2006/1/13 This doesn't move if the token's
9296 ;; at EOB.
9297 (goto-char (match-end 0))
9298 (setq lim (point))
9299 (c-skip-ws-forward)
9300 (c-forward-type))
9302 (catch 'break
9303 ;; Look for ": superclass-name" or "( category-name )".
9304 (when (looking-at "[:(]")
9305 (setq start-char (char-after))
9306 (forward-char)
9307 (c-forward-syntactic-ws)
9308 (unless (c-forward-type) (throw 'break nil))
9309 (when (eq start-char ?\()
9310 (unless (eq (char-after) ?\)) (throw 'break nil))
9311 (forward-char)
9312 (c-forward-syntactic-ws)))
9314 ;; Look for a protocol reference list.
9315 (if (eq (char-after) ?<)
9316 (let ((c-recognize-<>-arglists t)
9317 (c-parse-and-markup-<>-arglists t)
9318 c-restricted-<>-arglists)
9319 (c-forward-<>-arglist t))
9320 t))))
9322 (progn
9323 (c-backward-syntactic-ws lim)
9324 (c-clear-c-type-property start (1- (point)) 'c-decl-end)
9325 (c-put-c-type-property (1- (point)) 'c-decl-end)
9328 (c-clear-c-type-property start (point) 'c-decl-end)
9329 nil)))
9331 (defun c-beginning-of-inheritance-list (&optional lim)
9332 ;; Go to the first non-whitespace after the colon that starts a
9333 ;; multiple inheritance introduction. Optional LIM is the farthest
9334 ;; back we should search.
9336 ;; This function might do hidden buffer changes.
9337 (c-with-syntax-table c++-template-syntax-table
9338 (c-backward-token-2 0 t lim)
9339 (while (and (or (looking-at c-symbol-start)
9340 (looking-at "[<,]\\|::"))
9341 (zerop (c-backward-token-2 1 t lim))))))
9343 (defun c-in-method-def-p ()
9344 ;; Return nil if we aren't in a method definition, otherwise the
9345 ;; position of the initial [+-].
9347 ;; This function might do hidden buffer changes.
9348 (save-excursion
9349 (beginning-of-line)
9350 (and c-opt-method-key
9351 (looking-at c-opt-method-key)
9352 (point))
9355 ;; Contributed by Kevin Ryde <user42@zip.com.au>.
9356 (defun c-in-gcc-asm-p ()
9357 ;; Return non-nil if point is within a gcc \"asm\" block.
9359 ;; This should be called with point inside an argument list.
9361 ;; Only one level of enclosing parentheses is considered, so for
9362 ;; instance nil is returned when in a function call within an asm
9363 ;; operand.
9365 ;; This function might do hidden buffer changes.
9367 (and c-opt-asm-stmt-key
9368 (save-excursion
9369 (beginning-of-line)
9370 (backward-up-list 1)
9371 (c-beginning-of-statement-1 (point-min) nil t)
9372 (looking-at c-opt-asm-stmt-key))))
9374 (defun c-at-toplevel-p ()
9375 "Return a determination as to whether point is \"at the top level\".
9376 Informally, \"at the top level\" is anywhere where you can write
9377 a function.
9379 More precisely, being at the top-level means that point is either
9380 outside any enclosing block (such as a function definition), or
9381 directly inside a class, namespace or other block that contains
9382 another declaration level.
9384 If point is not at the top-level (e.g. it is inside a method
9385 definition), then nil is returned. Otherwise, if point is at a
9386 top-level not enclosed within a class definition, t is returned.
9387 Otherwise, a 2-vector is returned where the zeroth element is the
9388 buffer position of the start of the class declaration, and the first
9389 element is the buffer position of the enclosing class's opening
9390 brace.
9392 Note that this function might do hidden buffer changes. See the
9393 comment at the start of cc-engine.el for more info."
9394 ;; Note to maintainers: this function consumes a great mass of CPU cycles.
9395 ;; Its use should thus be minimized as far as possible.
9396 (let ((paren-state (c-parse-state)))
9397 (or (not (c-most-enclosing-brace paren-state))
9398 (c-search-uplist-for-classkey paren-state))))
9400 (defun c-just-after-func-arglist-p (&optional lim)
9401 ;; Return non-nil if the point is in the region after the argument
9402 ;; list of a function and its opening brace (or semicolon in case it
9403 ;; got no body). If there are K&R style argument declarations in
9404 ;; that region, the point has to be inside the first one for this
9405 ;; function to recognize it.
9407 ;; If successful, the point is moved to the first token after the
9408 ;; function header (see `c-forward-decl-or-cast-1' for details) and
9409 ;; the position of the opening paren of the function arglist is
9410 ;; returned.
9412 ;; The point is clobbered if not successful.
9414 ;; LIM is used as bound for backward buffer searches.
9416 ;; This function might do hidden buffer changes.
9418 (let ((beg (point)) id-start)
9419 (and
9420 (eq (c-beginning-of-statement-1 lim) 'same)
9422 (not (and (c-major-mode-is 'objc-mode)
9423 (c-forward-objc-directive)))
9425 (setq id-start
9426 (car-safe (c-forward-decl-or-cast-1 (c-point 'bosws) 'top nil)))
9427 (< id-start beg)
9429 ;; There should not be a '=' or ',' between beg and the
9430 ;; start of the declaration since that means we were in the
9431 ;; "expression part" of the declaration.
9432 (or (> (point) beg)
9433 (not (looking-at "[=,]")))
9435 (save-excursion
9436 ;; Check that there's an arglist paren in the
9437 ;; declaration.
9438 (goto-char id-start)
9439 (cond ((eq (char-after) ?\()
9440 ;; The declarator is a paren expression, so skip past it
9441 ;; so that we don't get stuck on that instead of the
9442 ;; function arglist.
9443 (c-forward-sexp))
9444 ((and c-opt-op-identifier-prefix
9445 (looking-at c-opt-op-identifier-prefix))
9446 ;; Don't trip up on "operator ()".
9447 (c-forward-token-2 2 t)))
9448 (and (< (point) beg)
9449 (c-syntactic-re-search-forward "(" beg t t)
9450 (1- (point)))))))
9452 (defun c-in-knr-argdecl (&optional lim)
9453 ;; Return the position of the first argument declaration if point is
9454 ;; inside a K&R style argument declaration list, nil otherwise.
9455 ;; `c-recognize-knr-p' is not checked. If LIM is non-nil, it's a
9456 ;; position that bounds the backward search for the argument list. This
9457 ;; function doesn't move point.
9459 ;; Point must be within a possible K&R region, e.g. just before a top-level
9460 ;; "{". It must be outside of parens and brackets. The test can return
9461 ;; false positives otherwise.
9463 ;; This function might do hidden buffer changes.
9464 (save-excursion
9465 (save-restriction
9466 ;; If we're in a macro, our search range is restricted to it. Narrow to
9467 ;; the searchable range.
9468 (let* ((macro-start (save-excursion (and (c-beginning-of-macro) (point))))
9469 (macro-end (save-excursion (and macro-start (c-end-of-macro) (point))))
9470 (low-lim (max (or lim (point-min)) (or macro-start (point-min))))
9471 before-lparen after-rparen
9472 (here (point))
9473 (pp-count-out 20) ; Max number of paren/brace constructs before
9474 ; we give up.
9475 ids ; List of identifiers in the parenthesized list.
9476 id-start after-prec-token decl-or-cast decl-res
9477 c-last-identifier-range identifier-ok)
9478 (narrow-to-region low-lim (or macro-end (point-max)))
9480 ;; Search backwards for the defun's argument list. We give up if we
9481 ;; encounter a "}" (end of a previous defun) an "=" (which can't be in
9482 ;; a knr region) or BOB.
9484 ;; The criterion for a paren structure being the arg list is:
9485 ;; o - there is non-WS stuff after it but before any "{"; AND
9486 ;; o - the token after it isn't a ";" AND
9487 ;; o - it is preceded by either an identifier (the function name) or
9488 ;; a macro expansion like "DEFUN (...)"; AND
9489 ;; o - its content is a non-empty comma-separated list of identifiers
9490 ;; (an empty arg list won't have a knr region).
9492 ;; The following snippet illustrates these rules:
9493 ;; int foo (bar, baz, yuk)
9494 ;; int bar [] ;
9495 ;; int (*baz) (my_type) ;
9496 ;; int (*(* yuk) (void)) (void) ;
9497 ;; {
9499 ;; Additionally, for a knr list to be recognized:
9500 ;; o - The identifier of each declarator up to and including the
9501 ;; one "near" point must be contained in the arg list.
9503 (catch 'knr
9504 (while (> pp-count-out 0) ; go back one paren/bracket pair each time.
9505 (setq pp-count-out (1- pp-count-out))
9506 (c-syntactic-skip-backward "^)]}=")
9507 (cond ((eq (char-before) ?\))
9508 (setq after-rparen (point)))
9509 ((eq (char-before) ?\])
9510 (setq after-rparen nil))
9511 (t ; either } (hit previous defun) or = or no more
9512 ; parens/brackets.
9513 (throw 'knr nil)))
9515 (if after-rparen
9516 ;; We're inside a paren. Could it be our argument list....?
9518 (and
9519 (progn
9520 (goto-char after-rparen)
9521 (unless (c-go-list-backward) (throw 'knr nil)) ;
9522 ;; FIXME!!! What about macros between the parens? 2007/01/20
9523 (setq before-lparen (point)))
9525 ;; It can't be the arg list if next token is ; or {
9526 (progn (goto-char after-rparen)
9527 (c-forward-syntactic-ws)
9528 (not (memq (char-after) '(?\; ?\{ ?\=))))
9530 ;; Is the thing preceding the list an identifier (the
9531 ;; function name), or a macro expansion?
9532 (progn
9533 (goto-char before-lparen)
9534 (eq (c-backward-token-2) 0)
9535 (or (eq (c-on-identifier) (point))
9536 (and (eq (char-after) ?\))
9537 (c-go-up-list-backward)
9538 (eq (c-backward-token-2) 0)
9539 (eq (c-on-identifier) (point)))))
9541 ;; Have we got a non-empty list of comma-separated
9542 ;; identifiers?
9543 (progn
9544 (goto-char before-lparen)
9545 (c-forward-token-2) ; to first token inside parens
9546 (and
9547 (setq id-start (c-on-identifier)) ; Must be at least one.
9548 (catch 'id-list
9549 (while
9550 (progn
9551 (forward-char)
9552 (c-end-of-current-token)
9553 (push (buffer-substring-no-properties id-start
9554 (point))
9555 ids)
9556 (c-forward-syntactic-ws)
9557 (eq (char-after) ?\,))
9558 (c-forward-token-2)
9559 (unless (setq id-start (c-on-identifier))
9560 (throw 'id-list nil)))
9561 (eq (char-after) ?\)))))
9563 ;; Are all the identifiers in the k&r list up to the
9564 ;; current one also in the argument list?
9565 (progn
9566 (forward-char) ; over the )
9567 (setq after-prec-token after-rparen)
9568 (c-forward-syntactic-ws)
9569 (while (and
9570 (or (consp (setq decl-or-cast
9571 (c-forward-decl-or-cast-1
9572 after-prec-token
9573 nil ; Or 'arglist ???
9574 nil)))
9575 (progn
9576 (goto-char after-prec-token)
9577 (c-forward-syntactic-ws)
9578 (setq identifier-ok (eq (char-after) ?{))
9579 nil))
9580 (eq (char-after) ?\;)
9581 (setq after-prec-token (1+ (point)))
9582 (goto-char (car decl-or-cast))
9583 (setq decl-res (c-forward-declarator))
9584 (setq identifier-ok
9585 (member (buffer-substring-no-properties
9586 (car decl-res) (cadr decl-res))
9587 ids))
9588 (progn
9589 (goto-char after-prec-token)
9590 (prog1 (< (point) here)
9591 (c-forward-syntactic-ws))))
9592 (setq identifier-ok nil))
9593 identifier-ok))
9594 ;; ...Yes. We've identified the function's argument list.
9595 (throw 'knr
9596 (progn (goto-char after-rparen)
9597 (c-forward-syntactic-ws)
9598 (point)))
9599 ;; ...No. The current parens aren't the function's arg list.
9600 (goto-char before-lparen))
9602 (or (c-go-list-backward) ; backwards over [ .... ]
9603 (throw 'knr nil)))))))))
9605 (defun c-skip-conditional ()
9606 ;; skip forward over conditional at point, including any predicate
9607 ;; statements in parentheses. No error checking is performed.
9609 ;; This function might do hidden buffer changes.
9610 (c-forward-sexp (cond
9611 ;; else if()
9612 ((looking-at (concat "\\<else"
9613 "\\([ \t\n]\\|\\\\\n\\)+"
9614 "if\\>\\([^_]\\|$\\)"))
9616 ;; do, else, try, finally
9617 ((looking-at (concat "\\<\\("
9618 "do\\|else\\|try\\|finally"
9619 "\\)\\>\\([^_]\\|$\\)"))
9621 ;; for, if, while, switch, catch, synchronized, foreach
9622 (t 2))))
9624 (defun c-after-conditional (&optional lim)
9625 ;; If looking at the token after a conditional then return the
9626 ;; position of its start, otherwise return nil.
9628 ;; This function might do hidden buffer changes.
9629 (save-excursion
9630 (and (zerop (c-backward-token-2 1 t lim))
9631 (or (looking-at c-block-stmt-1-key)
9632 (and (eq (char-after) ?\()
9633 (zerop (c-backward-token-2 1 t lim))
9634 (or (looking-at c-block-stmt-2-key)
9635 (looking-at c-block-stmt-1-2-key))))
9636 (point))))
9638 (defun c-after-special-operator-id (&optional lim)
9639 ;; If the point is after an operator identifier that isn't handled
9640 ;; like an ordinary symbol (i.e. like "operator =" in C++) then the
9641 ;; position of the start of that identifier is returned. nil is
9642 ;; returned otherwise. The point may be anywhere in the syntactic
9643 ;; whitespace after the last token of the operator identifier.
9645 ;; This function might do hidden buffer changes.
9646 (save-excursion
9647 (and c-overloadable-operators-regexp
9648 (zerop (c-backward-token-2 1 nil lim))
9649 (looking-at c-overloadable-operators-regexp)
9650 (or (not c-opt-op-identifier-prefix)
9651 (and
9652 (zerop (c-backward-token-2 1 nil lim))
9653 (looking-at c-opt-op-identifier-prefix)))
9654 (point))))
9656 (defsubst c-backward-to-block-anchor (&optional lim)
9657 ;; Assuming point is at a brace that opens a statement block of some
9658 ;; kind, move to the proper anchor point for that block. It might
9659 ;; need to be adjusted further by c-add-stmt-syntax, but the
9660 ;; position at return is suitable as start position for that
9661 ;; function.
9663 ;; This function might do hidden buffer changes.
9664 (unless (= (point) (c-point 'boi))
9665 (let ((start (c-after-conditional lim)))
9666 (if start
9667 (goto-char start)))))
9669 (defsubst c-backward-to-decl-anchor (&optional lim)
9670 ;; Assuming point is at a brace that opens the block of a top level
9671 ;; declaration of some kind, move to the proper anchor point for
9672 ;; that block.
9674 ;; This function might do hidden buffer changes.
9675 (unless (= (point) (c-point 'boi))
9676 (c-beginning-of-statement-1 lim)))
9678 (defun c-search-decl-header-end ()
9679 ;; Search forward for the end of the "header" of the current
9680 ;; declaration. That's the position where the definition body
9681 ;; starts, or the first variable initializer, or the ending
9682 ;; semicolon. I.e. search forward for the closest following
9683 ;; (syntactically relevant) '{', '=' or ';' token. Point is left
9684 ;; _after_ the first found token, or at point-max if none is found.
9686 ;; This function might do hidden buffer changes.
9688 (let ((base (point)))
9689 (if (c-major-mode-is 'c++-mode)
9691 ;; In C++ we need to take special care to handle operator
9692 ;; tokens and those pesky template brackets.
9693 (while (and
9694 (c-syntactic-re-search-forward "[;{<=]" nil 'move t t)
9696 (c-end-of-current-token base)
9697 ;; Handle operator identifiers, i.e. ignore any
9698 ;; operator token preceded by "operator".
9699 (save-excursion
9700 (and (c-safe (c-backward-sexp) t)
9701 (looking-at c-opt-op-identifier-prefix)))
9702 (and (eq (char-before) ?<)
9703 (c-with-syntax-table c++-template-syntax-table
9704 (if (c-safe (goto-char (c-up-list-forward (point))))
9706 (goto-char (point-max))
9707 nil)))))
9708 (setq base (point)))
9710 (while (and
9711 (c-syntactic-re-search-forward "[;{=]" nil 'move t t)
9712 (c-end-of-current-token base))
9713 (setq base (point))))))
9715 (defun c-beginning-of-decl-1 (&optional lim)
9716 ;; Go to the beginning of the current declaration, or the beginning
9717 ;; of the previous one if already at the start of it. Point won't
9718 ;; be moved out of any surrounding paren. Return a cons cell of the
9719 ;; form (MOVE . KNR-POS). MOVE is like the return value from
9720 ;; `c-beginning-of-statement-1'. If point skipped over some K&R
9721 ;; style argument declarations (and they are to be recognized) then
9722 ;; KNR-POS is set to the start of the first such argument
9723 ;; declaration, otherwise KNR-POS is nil. If LIM is non-nil, it's a
9724 ;; position that bounds the backward search.
9726 ;; NB: Cases where the declaration continues after the block, as in
9727 ;; "struct foo { ... } bar;", are currently recognized as two
9728 ;; declarations, e.g. "struct foo { ... }" and "bar;" in this case.
9730 ;; This function might do hidden buffer changes.
9731 (catch 'return
9732 (let* ((start (point))
9733 (last-stmt-start (point))
9734 (move (c-beginning-of-statement-1 lim nil t)))
9736 ;; `c-beginning-of-statement-1' stops at a block start, but we
9737 ;; want to continue if the block doesn't begin a top level
9738 ;; construct, i.e. if it isn't preceded by ';', '}', ':', bob,
9739 ;; or an open paren.
9740 (let ((beg (point)) tentative-move)
9741 ;; Go back one "statement" each time round the loop until we're just
9742 ;; after a ;, }, or :, or at BOB or the start of a macro or start of
9743 ;; an ObjC method. This will move over a multiple declaration whose
9744 ;; components are comma separated.
9745 (while (and
9746 ;; Must check with c-opt-method-key in ObjC mode.
9747 (not (and c-opt-method-key
9748 (looking-at c-opt-method-key)))
9749 (/= last-stmt-start (point))
9750 (progn
9751 (c-backward-syntactic-ws lim)
9752 (not (or (memq (char-before) '(?\; ?} ?: nil))
9753 (c-at-vsemi-p))))
9754 (save-excursion
9755 (backward-char)
9756 (not (looking-at "\\s(")))
9757 ;; Check that we don't move from the first thing in a
9758 ;; macro to its header.
9759 (not (eq (setq tentative-move
9760 (c-beginning-of-statement-1 lim nil t))
9761 'macro)))
9762 (setq last-stmt-start beg
9763 beg (point)
9764 move tentative-move))
9765 (goto-char beg))
9767 (when c-recognize-knr-p
9768 (let ((fallback-pos (point)) knr-argdecl-start)
9769 ;; Handle K&R argdecls. Back up after the "statement" jumped
9770 ;; over by `c-beginning-of-statement-1', unless it was the
9771 ;; function body, in which case we're sitting on the opening
9772 ;; brace now. Then test if we're in a K&R argdecl region and
9773 ;; that we started at the other side of the first argdecl in
9774 ;; it.
9775 (unless (eq (char-after) ?{)
9776 (goto-char last-stmt-start))
9777 (if (and (setq knr-argdecl-start (c-in-knr-argdecl lim))
9778 (< knr-argdecl-start start)
9779 (progn
9780 (goto-char knr-argdecl-start)
9781 (not (eq (c-beginning-of-statement-1 lim nil t) 'macro))))
9782 (throw 'return
9783 (cons (if (eq (char-after fallback-pos) ?{)
9784 'previous
9785 'same)
9786 knr-argdecl-start))
9787 (goto-char fallback-pos))))
9789 ;; `c-beginning-of-statement-1' counts each brace block as a separate
9790 ;; statement, so the result will be 'previous if we've moved over any.
9791 ;; So change our result back to 'same if necessary.
9793 ;; If they were brace list initializers we might not have moved over a
9794 ;; declaration boundary though, so change it to 'same if we've moved
9795 ;; past a '=' before '{', but not ';'. (This ought to be integrated
9796 ;; into `c-beginning-of-statement-1', so we avoid this extra pass which
9797 ;; potentially can search over a large amount of text.). Take special
9798 ;; pains not to get mislead by C++'s "operator=", and the like.
9799 (if (and (eq move 'previous)
9800 (c-with-syntax-table (if (c-major-mode-is 'c++-mode)
9801 c++-template-syntax-table
9802 (syntax-table))
9803 (save-excursion
9804 (and
9805 (progn
9806 (while ; keep going back to "[;={"s until we either find
9807 ; no more, or get to one which isn't an "operator ="
9808 (and (c-syntactic-re-search-forward "[;={]" start t t t)
9809 (eq (char-before) ?=)
9810 c-overloadable-operators-regexp
9811 c-opt-op-identifier-prefix
9812 (save-excursion
9813 (eq (c-backward-token-2) 0)
9814 (looking-at c-overloadable-operators-regexp)
9815 (eq (c-backward-token-2) 0)
9816 (looking-at c-opt-op-identifier-prefix))))
9817 (eq (char-before) ?=))
9818 (c-syntactic-re-search-forward "[;{]" start t t)
9819 (eq (char-before) ?{)
9820 (c-safe (goto-char (c-up-list-forward (point))) t)
9821 (not (c-syntactic-re-search-forward ";" start t t))))))
9822 (cons 'same nil)
9823 (cons move nil)))))
9825 (defun c-end-of-decl-1 ()
9826 ;; Assuming point is at the start of a declaration (as detected by
9827 ;; e.g. `c-beginning-of-decl-1'), go to the end of it. Unlike
9828 ;; `c-beginning-of-decl-1', this function handles the case when a
9829 ;; block is followed by identifiers in e.g. struct declarations in C
9830 ;; or C++. If a proper end was found then t is returned, otherwise
9831 ;; point is moved as far as possible within the current sexp and nil
9832 ;; is returned. This function doesn't handle macros; use
9833 ;; `c-end-of-macro' instead in those cases.
9835 ;; This function might do hidden buffer changes.
9836 (let ((start (point))
9837 (decl-syntax-table (if (c-major-mode-is 'c++-mode)
9838 c++-template-syntax-table
9839 (syntax-table))))
9840 (catch 'return
9841 (c-search-decl-header-end)
9843 (when (and c-recognize-knr-p
9844 (eq (char-before) ?\;)
9845 (c-in-knr-argdecl start))
9846 ;; Stopped at the ';' in a K&R argdecl section which is
9847 ;; detected using the same criteria as in
9848 ;; `c-beginning-of-decl-1'. Move to the following block
9849 ;; start.
9850 (c-syntactic-re-search-forward "{" nil 'move t))
9852 (when (eq (char-before) ?{)
9853 ;; Encountered a block in the declaration. Jump over it.
9854 (condition-case nil
9855 (goto-char (c-up-list-forward (point)))
9856 (error (goto-char (point-max))
9857 (throw 'return nil)))
9858 (if (or (not c-opt-block-decls-with-vars-key)
9859 (save-excursion
9860 (c-with-syntax-table decl-syntax-table
9861 (let ((lim (point)))
9862 (goto-char start)
9863 (not (and
9864 ;; Check for `c-opt-block-decls-with-vars-key'
9865 ;; before the first paren.
9866 (c-syntactic-re-search-forward
9867 (concat "[;=([{]\\|\\("
9868 c-opt-block-decls-with-vars-key
9869 "\\)")
9870 lim t t t)
9871 (match-beginning 1)
9872 (not (eq (char-before) ?_))
9873 ;; Check that the first following paren is
9874 ;; the block.
9875 (c-syntactic-re-search-forward "[;=([{]"
9876 lim t t t)
9877 (eq (char-before) ?{)))))))
9878 ;; The declaration doesn't have any of the
9879 ;; `c-opt-block-decls-with-vars' keywords in the
9880 ;; beginning, so it ends here at the end of the block.
9881 (throw 'return t)))
9883 (c-with-syntax-table decl-syntax-table
9884 (while (progn
9885 (if (eq (char-before) ?\;)
9886 (throw 'return t))
9887 (c-syntactic-re-search-forward ";" nil 'move t))))
9888 nil)))
9890 (defun c-looking-at-decl-block (containing-sexp goto-start &optional limit)
9891 ;; Assuming the point is at an open brace, check if it starts a
9892 ;; block that contains another declaration level, i.e. that isn't a
9893 ;; statement block or a brace list, and if so return non-nil.
9895 ;; If the check is successful, the return value is the start of the
9896 ;; keyword that tells what kind of construct it is, i.e. typically
9897 ;; what `c-decl-block-key' matched. Also, if GOTO-START is set then
9898 ;; the point will be at the start of the construct, before any
9899 ;; leading specifiers, otherwise it's at the returned position.
9901 ;; The point is clobbered if the check is unsuccessful.
9903 ;; CONTAINING-SEXP is the position of the open of the surrounding
9904 ;; paren, or nil if none.
9906 ;; The optional LIMIT limits the backward search for the start of
9907 ;; the construct. It's assumed to be at a syntactically relevant
9908 ;; position.
9910 ;; If any template arglists are found in the searched region before
9911 ;; the open brace, they get marked with paren syntax.
9913 ;; This function might do hidden buffer changes.
9915 (let ((open-brace (point)) kwd-start first-specifier-pos)
9916 (c-syntactic-skip-backward c-block-prefix-charset limit t)
9918 (when (and c-recognize-<>-arglists
9919 (eq (char-before) ?>))
9920 ;; Could be at the end of a template arglist.
9921 (let ((c-parse-and-markup-<>-arglists t))
9922 (while (and
9923 (c-backward-<>-arglist nil limit)
9924 (progn
9925 (c-syntactic-skip-backward c-block-prefix-charset limit t)
9926 (eq (char-before) ?>))))))
9928 ;; Skip back over noise clauses.
9929 (while (and
9930 c-opt-cpp-prefix
9931 (eq (char-before) ?\))
9932 (let ((after-paren (point)))
9933 (if (and (c-go-list-backward)
9934 (progn (c-backward-syntactic-ws)
9935 (c-simple-skip-symbol-backward))
9936 (or (looking-at c-paren-nontype-key)
9937 (looking-at c-noise-macro-with-parens-name-re)))
9938 (progn
9939 (c-syntactic-skip-backward c-block-prefix-charset limit t)
9941 (goto-char after-paren)
9942 nil))))
9944 ;; Note: Can't get bogus hits inside template arglists below since they
9945 ;; have gotten paren syntax above.
9946 (when (and
9947 ;; If `goto-start' is set we begin by searching for the
9948 ;; first possible position of a leading specifier list.
9949 ;; The `c-decl-block-key' search continues from there since
9950 ;; we know it can't match earlier.
9951 (if goto-start
9952 (when (c-syntactic-re-search-forward c-symbol-start
9953 open-brace t t)
9954 (goto-char (setq first-specifier-pos (match-beginning 0)))
9958 (cond
9959 ((c-syntactic-re-search-forward c-decl-block-key open-brace t t t)
9960 (goto-char (setq kwd-start (match-beginning 0)))
9961 (and
9962 ;; Exclude cases where we matched what would ordinarily
9963 ;; be a block declaration keyword, except where it's not
9964 ;; legal because it's part of a "compound keyword" like
9965 ;; "enum class". Of course, if c-after-brace-list-key
9966 ;; is nil, we can skip the test.
9967 (or (equal c-after-brace-list-key "\\<\\>")
9968 (save-match-data
9969 (save-excursion
9970 (not
9971 (and
9972 (looking-at c-after-brace-list-key)
9973 (= (c-backward-token-2 1 t) 0)
9974 (looking-at c-brace-list-key))))))
9976 ;; Found a keyword that can't be a type?
9977 (match-beginning 1)
9979 ;; Can be a type too, in which case it's the return type of a
9980 ;; function (under the assumption that no declaration level
9981 ;; block construct starts with a type).
9982 (not (c-forward-type))
9984 ;; Jumped over a type, but it could be a declaration keyword
9985 ;; followed by the declared identifier that we've jumped over
9986 ;; instead (e.g. in "class Foo {"). If it indeed is a type
9987 ;; then we should be at the declarator now, so check for a
9988 ;; valid declarator start.
9990 ;; Note: This doesn't cope with the case when a declared
9991 ;; identifier is followed by e.g. '(' in a language where '('
9992 ;; also might be part of a declarator expression. Currently
9993 ;; there's no such language.
9994 (not (or (looking-at c-symbol-start)
9995 (looking-at c-type-decl-prefix-key))))))
9997 ;; In Pike a list of modifiers may be followed by a brace
9998 ;; to make them apply to many identifiers. Note that the
9999 ;; match data will be empty on return in this case.
10000 ((and (c-major-mode-is 'pike-mode)
10001 (progn
10002 (goto-char open-brace)
10003 (= (c-backward-token-2) 0))
10004 (looking-at c-specifier-key)
10005 ;; Use this variant to avoid yet another special regexp.
10006 (c-keyword-member (c-keyword-sym (match-string 1))
10007 'c-modifier-kwds))
10008 (setq kwd-start (point))
10009 t)))
10011 ;; Got a match.
10013 (if goto-start
10014 ;; Back up over any preceding specifiers and their clauses
10015 ;; by going forward from `first-specifier-pos', which is the
10016 ;; earliest possible position where the specifier list can
10017 ;; start.
10018 (progn
10019 (goto-char first-specifier-pos)
10021 (while (< (point) kwd-start)
10022 (if (looking-at c-symbol-key)
10023 ;; Accept any plain symbol token on the ground that
10024 ;; it's a specifier masked through a macro (just
10025 ;; like `c-forward-decl-or-cast-1' skip forward over
10026 ;; such tokens).
10028 ;; Could be more restrictive wrt invalid keywords,
10029 ;; but that'd only occur in invalid code so there's
10030 ;; no use spending effort on it.
10031 (let ((end (match-end 0)))
10032 (unless (c-forward-keyword-clause 0)
10033 (goto-char end)
10034 (c-forward-syntactic-ws)))
10036 ;; Can't parse a declaration preamble and is still
10037 ;; before `kwd-start'. That means `first-specifier-pos'
10038 ;; was in some earlier construct. Search again.
10039 (if (c-syntactic-re-search-forward c-symbol-start
10040 kwd-start 'move t)
10041 (goto-char (setq first-specifier-pos (match-beginning 0)))
10042 ;; Got no preamble before the block declaration keyword.
10043 (setq first-specifier-pos kwd-start))))
10045 (goto-char first-specifier-pos))
10046 (goto-char kwd-start))
10048 kwd-start)))
10050 (defun c-directly-in-class-called-p (name)
10051 ;; Check whether point is directly inside a brace block which is the brace
10052 ;; block of a class, struct, or union which is called NAME, a string.
10053 (let* ((paren-state (c-parse-state))
10054 (brace-pos (c-pull-open-brace paren-state))
10056 (when (eq (char-after brace-pos) ?{)
10057 (goto-char brace-pos)
10058 (save-excursion
10059 ; *c-looking-at-decl-block
10060 ; containing-sexp goto-start &optional
10061 ; limit)
10062 (when (and (c-looking-at-decl-block
10063 (c-pull-open-brace paren-state)
10064 nil)
10065 (looking-at c-class-key))
10066 (goto-char (match-end 1))
10067 (c-forward-syntactic-ws)
10068 (looking-at name))))))
10070 (defun c-search-uplist-for-classkey (paren-state)
10071 ;; Check if the closest containing paren sexp is a declaration
10072 ;; block, returning a 2 element vector in that case. Aref 0
10073 ;; contains the bufpos at boi of the class key line, and aref 1
10074 ;; contains the bufpos of the open brace. This function is an
10075 ;; obsolete wrapper for `c-looking-at-decl-block'.
10077 ;; This function might do hidden buffer changes.
10078 (let ((open-paren-pos (c-most-enclosing-brace paren-state)))
10079 (when open-paren-pos
10080 (save-excursion
10081 (goto-char open-paren-pos)
10082 (when (and (eq (char-after) ?{)
10083 (c-looking-at-decl-block
10084 (c-safe-position open-paren-pos paren-state)
10085 nil))
10086 (back-to-indentation)
10087 (vector (point) open-paren-pos))))))
10089 (defun c-most-enclosing-decl-block (paren-state)
10090 ;; Return the buffer position of the most enclosing decl-block brace (in the
10091 ;; sense of c-looking-at-decl-block) in the PAREN-STATE structure, or nil if
10092 ;; none was found.
10093 (let* ((open-brace (c-pull-open-brace paren-state))
10094 (next-open-brace (c-pull-open-brace paren-state)))
10095 (while (and open-brace
10096 (save-excursion
10097 (goto-char open-brace)
10098 (not (c-looking-at-decl-block next-open-brace nil))))
10099 (setq open-brace next-open-brace
10100 next-open-brace (c-pull-open-brace paren-state)))
10101 open-brace))
10103 (defun c-cheap-inside-bracelist-p (paren-state)
10104 ;; Return the position of the L-brace if point is inside a brace list
10105 ;; initialization of an array, etc. This is an approximate function,
10106 ;; designed for speed over accuracy. It will not find every bracelist, but
10107 ;; a non-nil result is reliable. We simply search for "= {" (naturally with
10108 ;; syntactic whitespace allowed). PAREN-STATE is the normal thing that it
10109 ;; is everywhere else.
10110 (let (b-pos)
10111 (save-excursion
10112 (while
10113 (and (setq b-pos (c-pull-open-brace paren-state))
10114 (progn (goto-char b-pos)
10115 (c-backward-sws)
10116 (c-backward-token-2)
10117 (not (looking-at "=")))))
10118 b-pos)))
10120 (defun c-backward-typed-enum-colon ()
10121 ;; We're at a "{" which might be the opening brace of a enum which is
10122 ;; strongly typed (by a ":" followed by a type). If this is the case, leave
10123 ;; point before the colon and return t. Otherwise leave point unchanged and return nil.
10124 ;; Match data will be clobbered.
10125 (let ((here (point))
10126 (colon-pos nil))
10127 (save-excursion
10128 (while
10129 (and (eql (c-backward-token-2) 0)
10130 (or (not (looking-at "\\s)"))
10131 (c-go-up-list-backward))
10132 (cond
10133 ((and (eql (char-after) ?:)
10134 (save-excursion
10135 (c-backward-syntactic-ws)
10136 (or (c-on-identifier)
10137 (progn
10138 (c-backward-token-2)
10139 (looking-at c-brace-list-key)))))
10140 (setq colon-pos (point))
10141 (forward-char)
10142 (c-forward-syntactic-ws)
10143 (or (and (c-forward-type)
10144 (progn (c-forward-syntactic-ws)
10145 (eq (point) here)))
10146 (setq colon-pos nil))
10147 nil)
10148 ((eql (char-after) ?\()
10150 ((looking-at c-symbol-key)
10152 (t nil)))))
10153 (when colon-pos
10154 (goto-char colon-pos)
10155 t)))
10157 (defun c-backward-over-enum-header ()
10158 ;; We're at a "{". Move back to the enum-like keyword that starts this
10159 ;; declaration and return t, otherwise don't move and return nil.
10160 (let ((here (point))
10161 up-sexp-pos before-identifier)
10162 (when c-recognize-post-brace-list-type-p
10163 (c-backward-typed-enum-colon))
10164 (while
10165 (and
10166 (eq (c-backward-token-2) 0)
10167 (or (not (looking-at "\\s)"))
10168 (c-go-up-list-backward))
10169 (cond
10170 ((and (looking-at c-symbol-key) (c-on-identifier)
10171 (not before-identifier))
10172 (setq before-identifier t))
10173 ((and before-identifier
10174 (or (eql (char-after) ?,)
10175 (looking-at c-postfix-decl-spec-key)))
10176 (setq before-identifier nil)
10178 ((looking-at c-after-brace-list-key) t)
10179 ((looking-at c-brace-list-key) nil)
10180 ((eq (char-after) ?\()
10181 (and (eq (c-backward-token-2) 0)
10182 (or (looking-at c-decl-hangon-key)
10183 (and c-opt-cpp-prefix
10184 (looking-at c-noise-macro-with-parens-name-re)))))
10186 ((and c-recognize-<>-arglists
10187 (eq (char-after) ?<)
10188 (looking-at "\\s("))
10190 (t nil))))
10191 (or (looking-at c-brace-list-key)
10192 (progn (goto-char here) nil))))
10194 (defun c-looking-at-or-maybe-in-bracelist (&optional containing-sexp lim)
10195 ;; Point is at an open brace. If this starts a brace list, return a list
10196 ;; whose car is the buffer position of the start of the construct which
10197 ;; introduces the list, and whose cdr is t if we have parsed a keyword
10198 ;; matching `c-opt-inexpr-brace-list-key' (e.g. Java's "new"), nil
10199 ;; otherwise. Otherwise, if point might be inside an enclosing brace list,
10200 ;; return t. If point is definitely neither at nor in a brace list, return
10201 ;; nil.
10203 ;; CONTAINING-SEXP is the position of the brace/paren/bracket enclosing
10204 ;; POINT, or nil if there is no such position, or we do not know it. LIM is
10205 ;; a backward search limit.
10207 ;; Here, "brace list" does not include the body of an enum.
10208 (save-excursion
10209 (let ((start (point))
10210 (class-key
10211 ;; Pike can have class definitions anywhere, so we must
10212 ;; check for the class key here.
10213 (and (c-major-mode-is 'pike-mode)
10214 c-decl-block-key))
10215 (braceassignp 'dontknow)
10216 inexpr-brace-list bufpos macro-start res pos after-type-id-pos)
10218 (setq res (c-backward-token-2 1 t lim))
10219 ;; Checks to do only on the first sexp before the brace.
10220 ;; Have we a C++ initialization, without an "="?
10221 (if (and (c-major-mode-is 'c++-mode)
10222 (cond
10223 ((and (not (eq res 0))
10224 (c-go-up-list-backward nil lim) ; FIXME!!! Check ; `lim' 2016-07-12.
10225 (eq (char-after) ?\())
10226 (setq braceassignp 'c++-noassign))
10227 ((looking-at c-pre-id-bracelist-key))
10228 ((looking-at c-return-key))
10229 ((and (looking-at c-symbol-start)
10230 (not (looking-at c-keywords-regexp)))
10231 (setq after-type-id-pos (point)))
10232 (t nil))
10233 (save-excursion
10234 (cond
10235 ((not (eq res 0))
10236 (and (c-go-up-list-backward nil lim) ; FIXME!!! Check `lim' 2016-07-12.
10237 (eq (char-after) ?\()))
10238 ((looking-at c-pre-id-bracelist-key))
10239 ((looking-at c-return-key))
10240 (t (setq after-type-id-pos (point))
10241 nil))))
10242 (setq braceassignp 'c++-noassign))
10244 (when (and c-opt-inexpr-brace-list-key
10245 (eq (char-after) ?\[))
10246 ;; In Java, an initialization brace list may follow
10247 ;; directly after "new Foo[]", so check for a "new"
10248 ;; earlier.
10249 (while (eq braceassignp 'dontknow)
10250 (setq braceassignp
10251 (cond ((/= (c-backward-token-2 1 t lim) 0) nil)
10252 ((looking-at c-opt-inexpr-brace-list-key)
10253 (setq inexpr-brace-list t)
10255 ((looking-at "\\sw\\|\\s_\\|[.[]")
10256 ;; Carry on looking if this is an
10257 ;; identifier (may contain "." in Java)
10258 ;; or another "[]" sexp.
10259 'dontknow)
10260 (t nil)))))
10262 (setq pos (point))
10263 (cond
10264 ((and after-type-id-pos
10265 (goto-char after-type-id-pos)
10266 (setq res (c-back-over-member-initializers))
10267 (goto-char res)
10268 (eq (car (c-beginning-of-decl-1 lim)) 'same))
10269 (cons (point) nil)) ; Return value.
10271 ((and after-type-id-pos
10272 (progn
10273 (c-backward-syntactic-ws)
10274 (eq (char-before) ?\()))
10275 ;; Single identifier between '(' and '{'. We have a bracelist.
10276 (cons after-type-id-pos nil))
10279 (goto-char pos)
10280 ;; Checks to do on all sexps before the brace, up to the
10281 ;; beginning of the statement.
10282 (while (eq braceassignp 'dontknow)
10283 (cond ((eq (char-after) ?\;)
10284 (setq braceassignp nil))
10285 ((and class-key
10286 (looking-at class-key))
10287 (setq braceassignp nil))
10288 ((eq (char-after) ?=)
10289 ;; We've seen a =, but must check earlier tokens so
10290 ;; that it isn't something that should be ignored.
10291 (setq braceassignp 'maybe)
10292 (while (and (eq braceassignp 'maybe)
10293 (zerop (c-backward-token-2 1 t lim)))
10294 (setq braceassignp
10295 (cond
10296 ;; Check for operator =
10297 ((and c-opt-op-identifier-prefix
10298 (looking-at c-opt-op-identifier-prefix))
10299 nil)
10300 ;; Check for `<opchar>= in Pike.
10301 ((and (c-major-mode-is 'pike-mode)
10302 (or (eq (char-after) ?`)
10303 ;; Special case for Pikes
10304 ;; `[]=, since '[' is not in
10305 ;; the punctuation class.
10306 (and (eq (char-after) ?\[)
10307 (eq (char-before) ?`))))
10308 nil)
10309 ((looking-at "\\s.") 'maybe)
10310 ;; make sure we're not in a C++ template
10311 ;; argument assignment
10312 ((and
10313 (c-major-mode-is 'c++-mode)
10314 (save-excursion
10315 (let ((here (point))
10316 (pos< (progn
10317 (skip-chars-backward "^<>")
10318 (point))))
10319 (and (eq (char-before) ?<)
10320 (not (c-crosses-statement-barrier-p
10321 pos< here))
10322 (not (c-in-literal))
10323 ))))
10324 nil)
10325 (t t))))))
10326 (if (and (eq braceassignp 'dontknow)
10327 (/= (c-backward-token-2 1 t lim) 0))
10328 (setq braceassignp nil)))
10330 (cond
10331 (braceassignp
10332 ;; We've hit the beginning of the aggregate list.
10333 (c-beginning-of-statement-1 containing-sexp)
10334 (cons (point) inexpr-brace-list))
10335 ((and after-type-id-pos
10336 (save-excursion
10337 (when (eq (char-after) ?\;)
10338 (c-forward-token-2 1 t))
10339 (setq bufpos (point))
10340 (when (looking-at c-opt-<>-sexp-key)
10341 (c-forward-token-2)
10342 (when (and (eq (char-after) ?<)
10343 (c-get-char-property (point) 'syntax-table))
10344 (c-go-list-forward nil after-type-id-pos)
10345 (c-forward-syntactic-ws)))
10346 (and
10347 (or (not (looking-at c-class-key))
10348 (save-excursion
10349 (goto-char (match-end 1))
10350 (c-forward-syntactic-ws)
10351 (not (eq (point) after-type-id-pos))))
10352 (progn
10353 (setq res
10354 (c-forward-decl-or-cast-1
10355 (save-excursion (c-backward-syntactic-ws) (point))
10356 nil nil))
10357 (and (consp res)
10358 (eq (car res) after-type-id-pos))))))
10359 (cons bufpos inexpr-brace-list))
10360 ((eq (char-after) ?\;)
10361 ;; Brace lists can't contain a semicolon, so we're done.
10362 ;; (setq containing-sexp nil)
10363 nil)
10364 ((and (setq macro-start (point))
10365 (c-forward-to-cpp-define-body)
10366 (eq (point) start))
10367 ;; We've a macro whose expansion starts with the '{'.
10368 ;; Heuristically, if we have a ';' in it we've not got a
10369 ;; brace list, otherwise we have.
10370 (let ((macro-end (progn (c-end-of-macro) (point))))
10371 (goto-char start)
10372 (forward-char)
10373 (if (and (c-syntactic-re-search-forward "[;,]" macro-end t t)
10374 (eq (char-before) ?\;))
10376 (cons macro-start nil)))) ; (2016-08-30): Lazy! We have no
10377 ; languages where
10378 ; `c-opt-inexpr-brace-list-key' is
10379 ; non-nil and we have macros.
10380 (t t)))) ;; The caller can go up one level.
10383 (defun c-inside-bracelist-p (containing-sexp paren-state)
10384 ;; return the buffer position of the beginning of the brace list
10385 ;; statement if we're inside a brace list, otherwise return nil.
10386 ;; CONTAINING-SEXP is the buffer pos of the innermost containing
10387 ;; paren. PAREN-STATE is the remainder of the state of enclosing
10388 ;; braces
10390 ;; N.B.: This algorithm can potentially get confused by cpp macros
10391 ;; placed in inconvenient locations. It's a trade-off we make for
10392 ;; speed.
10394 ;; This function might do hidden buffer changes.
10396 ;; This will pick up brace list declarations.
10397 (save-excursion
10398 (goto-char containing-sexp)
10399 (c-backward-over-enum-header))
10400 ;; this will pick up array/aggregate init lists, even if they are nested.
10401 (save-excursion
10402 (let ((bufpos t)
10403 lim next-containing)
10404 (while (and (eq bufpos t)
10405 containing-sexp)
10406 (when paren-state
10407 (if (consp (car paren-state))
10408 (setq lim (cdr (car paren-state))
10409 paren-state (cdr paren-state))
10410 (setq lim (car paren-state)))
10411 (when paren-state
10412 (setq next-containing (car paren-state)
10413 paren-state (cdr paren-state))))
10415 (goto-char containing-sexp)
10416 (if (c-looking-at-inexpr-block next-containing next-containing)
10417 ;; We're in an in-expression block of some kind. Do not
10418 ;; check nesting. We deliberately set the limit to the
10419 ;; containing sexp, so that c-looking-at-inexpr-block
10420 ;; doesn't check for an identifier before it.
10421 (setq bufpos nil)
10422 (when (or (not (eq (char-after) ?{))
10423 (eq (setq bufpos (c-looking-at-or-maybe-in-bracelist
10424 next-containing lim))
10426 (setq containing-sexp next-containing
10427 lim nil
10428 next-containing nil))))
10429 (and (consp bufpos) (car bufpos))))))
10431 (defun c-looking-at-special-brace-list (&optional lim)
10432 ;; If we're looking at the start of a pike-style list, i.e., `({ })',
10433 ;; `([ ])', `(< >)', etc., a cons of a cons of its starting and ending
10434 ;; positions and its entry in c-special-brace-lists is returned, nil
10435 ;; otherwise. The ending position is nil if the list is still open.
10436 ;; LIM is the limit for forward search. The point may either be at
10437 ;; the `(' or at the following paren character. Tries to check the
10438 ;; matching closer, but assumes it's correct if no balanced paren is
10439 ;; found (i.e. the case `({ ... } ... )' is detected as _not_ being
10440 ;; a special brace list).
10442 ;; This function might do hidden buffer changes.
10443 (if c-special-brace-lists
10444 (condition-case ()
10445 (save-excursion
10446 (let ((beg (point))
10447 inner-beg end type)
10448 (c-forward-syntactic-ws)
10449 (if (eq (char-after) ?\()
10450 (progn
10451 (forward-char 1)
10452 (c-forward-syntactic-ws)
10453 (setq inner-beg (point))
10454 (setq type (assq (char-after) c-special-brace-lists)))
10455 (if (setq type (assq (char-after) c-special-brace-lists))
10456 (progn
10457 (setq inner-beg (point))
10458 (c-backward-syntactic-ws)
10459 (forward-char -1)
10460 (setq beg (if (eq (char-after) ?\()
10461 (point)
10462 nil)))))
10463 (if (and beg type)
10464 (if (and (c-safe
10465 (goto-char beg)
10466 (c-forward-sexp 1)
10467 (setq end (point))
10468 (= (char-before) ?\)))
10469 (c-safe
10470 (goto-char inner-beg)
10471 (if (looking-at "\\s(")
10472 ;; Check balancing of the inner paren
10473 ;; below.
10474 (progn
10475 (c-forward-sexp 1)
10477 ;; If the inner char isn't a paren then
10478 ;; we can't check balancing, so just
10479 ;; check the char before the outer
10480 ;; closing paren.
10481 (goto-char end)
10482 (backward-char)
10483 (c-backward-syntactic-ws)
10484 (= (char-before) (cdr type)))))
10485 (if (or (/= (char-syntax (char-before)) ?\))
10486 (= (progn
10487 (c-forward-syntactic-ws)
10488 (point))
10489 (1- end)))
10490 (cons (cons beg end) type))
10491 (cons (list beg) type)))))
10492 (error nil))))
10494 (defun c-looking-at-bos (&optional lim)
10495 ;; Return non-nil if between two statements or declarations, assuming
10496 ;; point is not inside a literal or comment.
10498 ;; Obsolete - `c-at-statement-start-p' or `c-at-expression-start-p'
10499 ;; are recommended instead.
10501 ;; This function might do hidden buffer changes.
10502 (c-at-statement-start-p))
10503 (make-obsolete 'c-looking-at-bos 'c-at-statement-start-p "22.1")
10505 (defun c-looking-at-statement-block ()
10506 ;; Point is at an opening brace. If this is a statement block (i.e. the
10507 ;; elements in it are terminated by semicolons) return t. Otherwise, return
10508 ;; nil.
10509 (let ((here (point)))
10510 (prog1
10511 (if (c-go-list-forward)
10512 (let ((there (point)))
10513 (backward-char)
10514 (c-syntactic-skip-backward
10515 "^;," here t)
10516 (cond
10517 ((eq (char-before) ?\;) t)
10518 ((eq (char-before) ?,) nil)
10519 (t (goto-char here)
10520 (forward-char)
10521 (and (c-syntactic-re-search-forward "{" there t t)
10522 (progn (backward-char)
10523 (c-looking-at-statement-block))))))
10524 (forward-char)
10525 (and (c-syntactic-re-search-forward "[;,]" nil t t)
10526 (eq (char-before) ?\;)))
10527 (goto-char here))))
10529 (defun c-looking-at-inexpr-block (lim containing-sexp &optional check-at-end)
10530 ;; Return non-nil if we're looking at the beginning of a block
10531 ;; inside an expression. The value returned is actually a cons of
10532 ;; either 'inlambda, 'inexpr-statement or 'inexpr-class and the
10533 ;; position of the beginning of the construct.
10535 ;; LIM limits the backward search. CONTAINING-SEXP is the start
10536 ;; position of the closest containing list. If it's nil, the
10537 ;; containing paren isn't used to decide whether we're inside an
10538 ;; expression or not. If both LIM and CONTAINING-SEXP are used, LIM
10539 ;; needs to be farther back.
10541 ;; If CHECK-AT-END is non-nil then extra checks at the end of the
10542 ;; brace block might be done. It should only be used when the
10543 ;; construct can be assumed to be complete, i.e. when the original
10544 ;; starting position was further down than that.
10546 ;; This function might do hidden buffer changes.
10548 (save-excursion
10549 (let ((res 'maybe) (passed-bracket-pairs 0) bracket-pos passed-paren
10550 haskell-op-pos
10551 (closest-lim (or containing-sexp lim (point-min)))
10552 ;; Look at the character after point only as a last resort
10553 ;; when we can't disambiguate.
10554 (block-follows (and (eq (char-after) ?{) (point))))
10556 ;; Search for a C++11 "->" which suggests a lambda declaration.
10557 (when (and (c-major-mode-is 'c++-mode)
10558 (setq haskell-op-pos
10559 (save-excursion
10560 (while
10561 (progn
10562 (c-syntactic-skip-backward "^;=}>" closest-lim t)
10563 (and (eq (char-before) ?>)
10564 (c-backward-token-2)
10565 (not (looking-at c-haskell-op-re)))))
10566 (and (looking-at c-haskell-op-re)
10567 (point)))))
10568 (goto-char haskell-op-pos))
10570 (while (and (eq res 'maybe)
10571 (progn (c-backward-syntactic-ws)
10572 (> (point) closest-lim))
10573 (not (bobp))
10574 (progn (backward-char)
10575 (looking-at "[]).]\\|\\w\\|\\s_"))
10576 (c-safe (forward-char)
10577 (goto-char (scan-sexps (point) -1))))
10579 (setq res
10580 (if (looking-at c-keywords-regexp)
10581 (let ((kw-sym (c-keyword-sym (match-string 1))))
10582 (cond
10583 ((and block-follows
10584 (c-keyword-member kw-sym 'c-inexpr-class-kwds))
10585 (and (not (eq passed-paren ?\[))
10586 (or (not (looking-at c-class-key))
10587 ;; If the class definition is at the start of
10588 ;; a statement, we don't consider it an
10589 ;; in-expression class.
10590 (let ((prev (point)))
10591 (while (and
10592 (= (c-backward-token-2 1 nil closest-lim) 0)
10593 (eq (char-syntax (char-after)) ?w))
10594 (setq prev (point)))
10595 (goto-char prev)
10596 (not (c-at-statement-start-p)))
10597 ;; Also, in Pike we treat it as an
10598 ;; in-expression class if it's used in an
10599 ;; object clone expression.
10600 (save-excursion
10601 (and check-at-end
10602 (c-major-mode-is 'pike-mode)
10603 (progn (goto-char block-follows)
10604 (zerop (c-forward-token-2 1 t)))
10605 (eq (char-after) ?\())))
10606 (cons 'inexpr-class (point))))
10607 ((c-keyword-member kw-sym 'c-paren-any-kwds) ; e.g. C++11 "throw" or "noexcept"
10608 (setq passed-paren nil)
10609 (setq passed-bracket-pairs 0)
10610 (setq bracket-pos nil)
10611 'maybe)
10612 ((c-keyword-member kw-sym 'c-inexpr-block-kwds)
10613 (when (not passed-paren)
10614 (cons 'inexpr-statement (point))))
10615 ((c-keyword-member kw-sym 'c-lambda-kwds)
10616 (when (or (not passed-paren)
10617 (eq passed-paren ?\())
10618 (cons 'inlambda (point))))
10619 ((c-keyword-member kw-sym 'c-block-stmt-kwds)
10620 nil)
10622 'maybe)))
10624 (if (looking-at "\\s(")
10625 (if passed-paren
10626 (cond
10627 ((and (eq passed-paren ?\[)
10628 (eq (char-after) ?\[)
10629 (not (eq (char-after (1+ (point))) ?\[))) ; C++ attribute.
10630 ;; Accept several square bracket sexps for
10631 ;; Java array initializations.
10632 (setq passed-bracket-pairs (1+ passed-bracket-pairs))
10633 'maybe)
10634 ((and (eq passed-paren ?\()
10635 (eq (char-after) ?\[)
10636 (not (eq (char-after (1+ (point))) ?\[))
10637 (eq passed-bracket-pairs 0))
10638 ;; C++11 lambda function declaration
10639 (setq passed-bracket-pairs 1)
10640 (setq bracket-pos (point))
10641 'maybe)
10642 (t nil))
10643 (when (not (looking-at "\\[\\["))
10644 (setq passed-paren (char-after))
10645 (when (eq passed-paren ?\[)
10646 (setq passed-bracket-pairs 1)
10647 (setq bracket-pos (point))))
10648 'maybe)
10649 'maybe))))
10651 (if (eq res 'maybe)
10652 (cond
10653 ((and (c-major-mode-is 'c++-mode)
10654 block-follows
10655 (eq passed-bracket-pairs 1)
10656 (save-excursion
10657 (goto-char bracket-pos)
10658 (or (<= (point) (or lim (point-min)))
10659 (progn
10660 (c-backward-token-2 1 nil lim)
10661 (and
10662 (not (c-on-identifier))
10663 (not (looking-at c-opt-op-identifier-prefix)))))))
10664 (cons 'inlambda bracket-pos))
10665 ((and c-recognize-paren-inexpr-blocks
10666 block-follows
10667 containing-sexp
10668 (eq (char-after containing-sexp) ?\())
10669 (goto-char containing-sexp)
10670 (if (or (save-excursion
10671 (c-backward-syntactic-ws lim)
10672 (while (and (eq (char-before) ?>)
10673 (c-get-char-property (1- (point))
10674 'syntax-table)
10675 (c-go-list-backward nil lim))
10676 (c-backward-syntactic-ws lim))
10677 (and (> (point) (or lim (point-min)))
10678 (c-on-identifier)))
10679 (and c-special-brace-lists
10680 (c-looking-at-special-brace-list))
10681 (and (c-major-mode-is 'c++-mode)
10682 (save-excursion
10683 (goto-char block-follows)
10684 (not (c-looking-at-statement-block)))))
10686 (cons 'inexpr-statement (point)))))
10688 res))))
10690 (defun c-looking-at-inexpr-block-backward (paren-state)
10691 ;; Returns non-nil if we're looking at the end of an in-expression
10692 ;; block, otherwise the same as `c-looking-at-inexpr-block'.
10693 ;; PAREN-STATE is the paren state relevant at the current position.
10695 ;; This function might do hidden buffer changes.
10696 (save-excursion
10697 ;; We currently only recognize a block.
10698 (let ((here (point))
10699 (elem (car-safe paren-state))
10700 containing-sexp)
10701 (when (and (consp elem)
10702 (progn (goto-char (cdr elem))
10703 (c-forward-syntactic-ws here)
10704 (= (point) here)))
10705 (goto-char (car elem))
10706 (if (setq paren-state (cdr paren-state))
10707 (setq containing-sexp (car-safe paren-state)))
10708 (c-looking-at-inexpr-block (c-safe-position containing-sexp
10709 paren-state)
10710 containing-sexp)))))
10712 (defun c-looking-at-c++-lambda-capture-list ()
10713 ;; Return non-nil if we're at the opening "[" of the capture list of a C++
10714 ;; lambda function, nil otherwise.
10715 (and
10716 (eq (char-after) ?\[)
10717 (not (eq (char-before) ?\[))
10718 (not (eq (char-after (1+ (point))) ?\[))
10719 (save-excursion
10720 (or (eq (c-backward-token-2 1) 1)
10721 (looking-at c-pre-lambda-tokens-re)))
10722 (not (c-in-literal))))
10724 (defun c-at-macro-vsemi-p (&optional pos)
10725 ;; Is there a "virtual semicolon" at POS or point?
10726 ;; (See cc-defs.el for full details of "virtual semicolons".)
10728 ;; This is true when point is at the last non syntactic WS position on the
10729 ;; line, there is a macro call last on the line, and this particular macro's
10730 ;; name is defined by the regexp `c-vs-macro-regexp' as not needing a
10731 ;; semicolon.
10732 (save-excursion
10733 (save-restriction
10734 (widen)
10735 (if pos
10736 (goto-char pos)
10737 (setq pos (point)))
10738 (and
10739 c-macro-with-semi-re
10740 (eq (skip-chars-backward " \t") 0)
10742 ;; Check we've got nothing after this except comments and empty lines
10743 ;; joined by escaped EOLs.
10744 (skip-chars-forward " \t") ; always returns non-nil.
10745 (progn
10746 (while ; go over 1 block comment per iteration.
10747 (and
10748 (looking-at "\\(\\\\[\n\r][ \t]*\\)*")
10749 (goto-char (match-end 0))
10750 (cond
10751 ((looking-at c-block-comment-start-regexp)
10752 (and (forward-comment 1)
10753 (skip-chars-forward " \t"))) ; always returns non-nil
10754 ((looking-at c-line-comment-start-regexp)
10755 (end-of-line)
10756 nil)
10757 (t nil))))
10758 (eolp))
10760 (goto-char pos)
10761 (progn (c-backward-syntactic-ws)
10762 (eq (point) pos))
10764 ;; Check for one of the listed macros being before point.
10765 (or (not (eq (char-before) ?\)))
10766 (when (c-go-list-backward)
10767 (c-backward-syntactic-ws)
10769 (c-simple-skip-symbol-backward)
10770 (looking-at c-macro-with-semi-re)
10771 (goto-char pos)
10772 (not (c-in-literal)))))) ; The most expensive check last.
10774 (defun c-macro-vsemi-status-unknown-p () t) ; See cc-defs.el.
10777 ;; `c-guess-basic-syntax' and the functions that precedes it below
10778 ;; implements the main decision tree for determining the syntactic
10779 ;; analysis of the current line of code.
10781 ;; Dynamically bound to t when `c-guess-basic-syntax' is called during
10782 ;; auto newline analysis.
10783 (defvar c-auto-newline-analysis nil)
10785 (defun c-brace-anchor-point (bracepos)
10786 ;; BRACEPOS is the position of a brace in a construct like "namespace
10787 ;; Bar {". Return the anchor point in this construct; this is the
10788 ;; earliest symbol on the brace's line which isn't earlier than
10789 ;; "namespace".
10791 ;; Currently (2007-08-17), "like namespace" means "matches
10792 ;; c-other-block-decl-kwds". It doesn't work with "class" or "struct"
10793 ;; or anything like that.
10794 (save-excursion
10795 (let ((boi (c-point 'boi bracepos)))
10796 (goto-char bracepos)
10797 (while (and (> (point) boi)
10798 (not (looking-at c-other-decl-block-key)))
10799 (c-backward-token-2))
10800 (if (> (point) boi) (point) boi))))
10802 (defsubst c-add-syntax (symbol &rest args)
10803 ;; A simple function to prepend a new syntax element to
10804 ;; `c-syntactic-context'. Using `setq' on it is unsafe since it
10805 ;; should always be dynamically bound but since we read it first
10806 ;; we'll fail properly anyway if this function is misused.
10807 (setq c-syntactic-context (cons (cons symbol args)
10808 c-syntactic-context)))
10810 (defsubst c-append-syntax (symbol &rest args)
10811 ;; Like `c-add-syntax' but appends to the end of the syntax list.
10812 ;; (Normally not necessary.)
10813 (setq c-syntactic-context (nconc c-syntactic-context
10814 (list (cons symbol args)))))
10816 (defun c-add-stmt-syntax (syntax-symbol
10817 syntax-extra-args
10818 stop-at-boi-only
10819 containing-sexp
10820 paren-state
10821 &optional fixed-anchor)
10822 ;; Add the indicated SYNTAX-SYMBOL to `c-syntactic-context', extending it as
10823 ;; needed with further syntax elements of the types `substatement',
10824 ;; `inexpr-statement', `arglist-cont-nonempty', `statement-block-intro',
10825 ;; `defun-block-intro', and `brace-list-intro'.
10827 ;; Do the generic processing to anchor the given syntax symbol on the
10828 ;; preceding statement: First skip over any labels and containing statements
10829 ;; on the same line. If FIXED-ANCHOR is non-nil, use this as the
10830 ;; anchor-point for the given syntactic symbol, and don't make syntactic
10831 ;; entries for constructs beginning on lines before that containing
10832 ;; ANCHOR-POINT. Otherwise search backward until we find a statement or
10833 ;; block start that begins at boi without a label or comment.
10835 ;; Point is assumed to be at the prospective anchor point for the
10836 ;; given SYNTAX-SYMBOL. More syntax entries are added if we need to
10837 ;; skip past open parens and containing statements. Most of the added
10838 ;; syntax elements will get the same anchor point - the exception is
10839 ;; for an anchor in a construct like "namespace"[*] - this is as early
10840 ;; as possible in the construct but on the same line as the {.
10842 ;; [*] i.e. with a keyword matching c-other-block-decl-kwds.
10844 ;; SYNTAX-EXTRA-ARGS are a list of the extra arguments for the
10845 ;; syntax symbol. They are appended after the anchor point.
10847 ;; If STOP-AT-BOI-ONLY is nil, we can stop in the middle of the line
10848 ;; if the current statement starts there.
10850 ;; Note: It's not a problem if PAREN-STATE "overshoots"
10851 ;; CONTAINING-SEXP, i.e. contains info about parens further down.
10853 ;; This function might do hidden buffer changes.
10855 (if (= (point) (c-point 'boi))
10856 ;; This is by far the most common case, so let's give it special
10857 ;; treatment.
10858 (apply 'c-add-syntax syntax-symbol (point) syntax-extra-args)
10860 (let ((syntax-last c-syntactic-context)
10861 (boi (c-point 'boi))
10862 (anchor-boi (c-point 'boi))
10863 ;; Set when we're on a label, so that we don't stop there.
10864 ;; FIXME: To be complete we should check if we're on a label
10865 ;; now at the start.
10866 on-label)
10868 ;; Use point as the anchor point for "namespace", "extern", etc.
10869 (apply 'c-add-syntax syntax-symbol
10870 (if (rassq syntax-symbol c-other-decl-block-key-in-symbols-alist)
10871 (point) nil)
10872 syntax-extra-args)
10874 ;; Loop while we have to back out of containing blocks.
10875 (while
10876 (and
10877 (catch 'back-up-block
10879 ;; Loop while we have to back up statements.
10880 (while (or (/= (point) boi)
10881 on-label
10882 (looking-at c-comment-start-regexp))
10884 ;; Skip past any comments that stands between the
10885 ;; statement start and boi.
10886 (let ((savepos (point)))
10887 (while (and (/= savepos boi)
10888 (c-backward-single-comment))
10889 (setq savepos (point)
10890 boi (c-point 'boi)))
10891 (goto-char savepos))
10893 ;; Skip to the beginning of this statement or backward
10894 ;; another one.
10895 (let ((old-pos (point))
10896 (old-boi boi)
10897 (step-type (c-beginning-of-statement-1 containing-sexp)))
10898 (setq boi (c-point 'boi)
10899 on-label (eq step-type 'label))
10901 (cond ((= (point) old-pos)
10902 ;; If we didn't move we're at the start of a block and
10903 ;; have to continue outside it.
10904 (throw 'back-up-block t))
10906 ((and (eq step-type 'up)
10907 (>= (point) old-boi)
10908 (looking-at "else\\>[^_]")
10909 (save-excursion
10910 (goto-char old-pos)
10911 (looking-at "if\\>[^_]")))
10912 ;; Special case to avoid deeper and deeper indentation
10913 ;; of "else if" clauses.
10916 ((and (not stop-at-boi-only)
10917 (/= old-pos old-boi)
10918 (memq step-type '(up previous)))
10919 ;; If stop-at-boi-only is nil, we shouldn't back up
10920 ;; over previous or containing statements to try to
10921 ;; reach boi, so go back to the last position and
10922 ;; exit.
10923 (goto-char old-pos)
10924 (throw 'back-up-block nil))
10927 (if (and (not stop-at-boi-only)
10928 (memq step-type '(up previous beginning)))
10929 ;; If we've moved into another statement then we
10930 ;; should no longer try to stop in the middle of a
10931 ;; line.
10932 (setq stop-at-boi-only t))
10934 ;; Record this as a substatement if we skipped up one
10935 ;; level.
10936 (when (eq step-type 'up)
10937 (c-add-syntax 'substatement nil))))
10940 containing-sexp
10941 (or (null fixed-anchor)
10942 (> containing-sexp anchor-boi)))
10944 ;; Now we have to go out of this block.
10945 (goto-char containing-sexp)
10947 ;; Don't stop in the middle of a special brace list opener
10948 ;; like "({".
10949 (when c-special-brace-lists
10950 (let ((special-list (c-looking-at-special-brace-list)))
10951 (when (and special-list
10952 (< (car (car special-list)) (point)))
10953 (setq containing-sexp (car (car special-list)))
10954 (goto-char containing-sexp))))
10956 (setq paren-state (c-whack-state-after containing-sexp paren-state)
10957 containing-sexp (c-most-enclosing-brace paren-state)
10958 boi (c-point 'boi))
10960 ;; Analyze the construct in front of the block we've stepped out
10961 ;; from and add the right syntactic element for it.
10962 (let ((paren-pos (point))
10963 (paren-char (char-after))
10964 step-type)
10966 (if (eq paren-char ?\()
10967 ;; Stepped out of a parenthesis block, so we're in an
10968 ;; expression now.
10969 (progn
10970 (when (/= paren-pos boi)
10971 (if (and c-recognize-paren-inexpr-blocks
10972 (progn
10973 (c-backward-syntactic-ws containing-sexp)
10974 (or (not (looking-at "\\>"))
10975 (not (c-on-identifier))))
10976 (save-excursion
10977 (goto-char (1+ paren-pos))
10978 (c-forward-syntactic-ws)
10979 (eq (char-after) ?{)))
10980 ;; Stepped out of an in-expression statement. This
10981 ;; syntactic element won't get an anchor pos.
10982 (c-add-syntax 'inexpr-statement)
10984 ;; A parenthesis normally belongs to an arglist.
10985 (c-add-syntax 'arglist-cont-nonempty nil paren-pos)))
10987 (goto-char (max boi
10988 (if containing-sexp
10989 (1+ containing-sexp)
10990 (point-min))))
10991 (setq step-type 'same
10992 on-label nil))
10994 ;; Stepped out of a brace block.
10995 (setq step-type (c-beginning-of-statement-1 containing-sexp)
10996 on-label (eq step-type 'label))
10998 (if (and (eq step-type 'same)
10999 (/= paren-pos (point)))
11000 (let (inexpr)
11001 (cond
11002 ((save-excursion
11003 (goto-char paren-pos)
11004 (setq inexpr (c-looking-at-inexpr-block
11005 (c-safe-position containing-sexp paren-state)
11006 containing-sexp)))
11007 (c-add-syntax (if (eq (car inexpr) 'inlambda)
11008 'defun-block-intro
11009 'statement-block-intro)
11010 nil))
11011 ((looking-at c-other-decl-block-key)
11012 (c-add-syntax
11013 (cdr (assoc (match-string 1)
11014 c-other-decl-block-key-in-symbols-alist))
11015 (max (c-point 'boi paren-pos) (point))))
11016 ((save-excursion
11017 (goto-char paren-pos)
11018 (c-looking-at-or-maybe-in-bracelist containing-sexp))
11019 (if (save-excursion
11020 (goto-char paren-pos)
11021 (c-looking-at-statement-block))
11022 (c-add-syntax 'defun-block-intro nil)
11023 (c-add-syntax 'brace-list-intro nil)))
11024 (t (c-add-syntax 'defun-block-intro nil))))
11026 (c-add-syntax 'statement-block-intro nil)))
11028 (if (= paren-pos boi)
11029 ;; Always done if the open brace was at boi. The
11030 ;; c-beginning-of-statement-1 call above is necessary
11031 ;; anyway, to decide the type of block-intro to add.
11032 (goto-char paren-pos)
11033 (setq boi (c-point 'boi)))
11036 ;; Fill in the current point as the anchor for all the symbols
11037 ;; added above.
11038 (let ((p c-syntactic-context) q)
11039 (while (not (eq p syntax-last))
11040 (setq q (cdr (car p))) ; e.g. (nil 28) [from (arglist-cont-nonempty nil 28)]
11041 (while q
11042 (unless (car q)
11043 (setcar q (if (or (cdr p)
11044 (null fixed-anchor))
11045 (point)
11046 fixed-anchor)))
11047 (setq q (cdr q)))
11048 (setq p (cdr p))))
11051 (defun c-add-class-syntax (symbol
11052 containing-decl-open
11053 containing-decl-start
11054 containing-decl-kwd
11055 paren-state)
11056 ;; The inclass and class-close syntactic symbols are added in
11057 ;; several places and some work is needed to fix everything.
11058 ;; Therefore it's collected here.
11060 ;; This function might do hidden buffer changes.
11061 (goto-char containing-decl-open)
11062 (if (and (eq symbol 'inclass) (= (point) (c-point 'boi)))
11063 (progn
11064 (c-add-syntax symbol containing-decl-open)
11065 containing-decl-open)
11066 (goto-char containing-decl-start)
11067 ;; Ought to use `c-add-stmt-syntax' instead of backing up to boi
11068 ;; here, but we have to do like this for compatibility.
11069 (back-to-indentation)
11070 (c-add-syntax symbol (point))
11071 (if (and (c-keyword-member containing-decl-kwd
11072 'c-inexpr-class-kwds)
11073 (/= containing-decl-start (c-point 'boi containing-decl-start)))
11074 (c-add-syntax 'inexpr-class))
11075 (point)))
11077 (defun c-guess-continued-construct (indent-point
11078 char-after-ip
11079 beg-of-same-or-containing-stmt
11080 containing-sexp
11081 paren-state)
11082 ;; This function contains the decision tree reached through both
11083 ;; cases 18 and 10. It's a continued statement or top level
11084 ;; construct of some kind.
11086 ;; This function might do hidden buffer changes.
11088 (let (special-brace-list placeholder)
11089 (goto-char indent-point)
11090 (skip-chars-forward " \t")
11092 (cond
11093 ;; (CASE A removed.)
11094 ;; CASE B: open braces for class or brace-lists
11095 ((setq special-brace-list
11096 (or (and c-special-brace-lists
11097 (c-looking-at-special-brace-list))
11098 (eq char-after-ip ?{)))
11100 (cond
11101 ;; CASE B.1: class-open
11102 ((save-excursion
11103 (and (eq (char-after) ?{)
11104 (c-looking-at-decl-block containing-sexp t)
11105 (setq beg-of-same-or-containing-stmt (point))))
11106 (c-add-syntax 'class-open beg-of-same-or-containing-stmt))
11108 ;; CASE B.2: brace-list-open
11109 ((or (consp special-brace-list)
11110 (consp
11111 (c-looking-at-or-maybe-in-bracelist
11112 containing-sexp beg-of-same-or-containing-stmt))
11114 ;; The most semantically accurate symbol here is
11115 ;; brace-list-open, but we normally report it simply as a
11116 ;; statement-cont. The reason is that one normally adjusts
11117 ;; brace-list-open for brace lists as top-level constructs,
11118 ;; and brace lists inside statements is a completely different
11119 ;; context. C.f. case 5A.3.
11120 (c-beginning-of-statement-1 containing-sexp)
11121 (c-add-stmt-syntax (if c-auto-newline-analysis
11122 ;; Turn off the dwim above when we're
11123 ;; analyzing the nature of the brace
11124 ;; for the auto newline feature.
11125 'brace-list-open
11126 'statement-cont)
11127 nil nil
11128 containing-sexp paren-state))
11130 ;; CASE B.3: The body of a function declared inside a normal
11131 ;; block. Can occur e.g. in Pike and when using gcc
11132 ;; extensions, but watch out for macros followed by blocks.
11133 ;; C.f. cases E, 16F and 17G.
11134 ((and (not (c-at-statement-start-p))
11135 (eq (c-beginning-of-statement-1 containing-sexp nil nil t)
11136 'same)
11137 (save-excursion
11138 (let ((c-recognize-typeless-decls nil))
11139 ;; Turn off recognition of constructs that lacks a
11140 ;; type in this case, since that's more likely to be
11141 ;; a macro followed by a block.
11142 (c-forward-decl-or-cast-1 (c-point 'bosws) nil nil))))
11143 (c-add-stmt-syntax 'defun-open nil t
11144 containing-sexp paren-state))
11146 ;; CASE B.5: We have a C++11 "return \n { ..... }" Note that we're
11147 ;; not at the "{", currently.
11148 ((progn (goto-char indent-point)
11149 (backward-sexp)
11150 (looking-at c-return-key))
11151 (c-add-stmt-syntax 'statement-cont nil t
11152 containing-sexp paren-state))
11154 ;; CASE B.4: Continued statement with block open. The most
11155 ;; accurate analysis is perhaps `statement-cont' together with
11156 ;; `block-open' but we play DWIM and use `substatement-open'
11157 ;; instead. The rationale is that this typically is a macro
11158 ;; followed by a block which makes it very similar to a
11159 ;; statement with a substatement block.
11161 (c-add-stmt-syntax 'substatement-open nil nil
11162 containing-sexp paren-state))
11165 ;; CASE C: iostream insertion or extraction operator
11166 ((and (looking-at "\\(<<\\|>>\\)\\([^=]\\|$\\)")
11167 (save-excursion
11168 (goto-char beg-of-same-or-containing-stmt)
11169 ;; If there is no preceding streamop in the statement
11170 ;; then indent this line as a normal statement-cont.
11171 (when (c-syntactic-re-search-forward
11172 "\\(<<\\|>>\\)\\([^=]\\|$\\)" indent-point 'move t t)
11173 (c-add-syntax 'stream-op (c-point 'boi))
11174 t))))
11176 ;; CASE E: In the "K&R region" of a function declared inside a
11177 ;; normal block. C.f. case B.3.
11178 ((and (save-excursion
11179 ;; Check that the next token is a '{'. This works as
11180 ;; long as no language that allows nested function
11181 ;; definitions allows stuff like member init lists, K&R
11182 ;; declarations or throws clauses there.
11184 ;; Note that we do a forward search for something ahead
11185 ;; of the indentation line here. That's not good since
11186 ;; the user might not have typed it yet. Unfortunately
11187 ;; it's exceedingly tricky to recognize a function
11188 ;; prototype in a code block without resorting to this.
11189 (c-forward-syntactic-ws)
11190 (eq (char-after) ?{))
11191 (not (c-at-statement-start-p))
11192 (eq (c-beginning-of-statement-1 containing-sexp nil nil t)
11193 'same)
11194 (save-excursion
11195 (let ((c-recognize-typeless-decls nil))
11196 ;; Turn off recognition of constructs that lacks a
11197 ;; type in this case, since that's more likely to be
11198 ;; a macro followed by a block.
11199 (c-forward-decl-or-cast-1 (c-point 'bosws) nil nil))))
11200 (c-add-stmt-syntax 'func-decl-cont nil t
11201 containing-sexp paren-state))
11203 ;;CASE F: continued statement and the only preceding items are
11204 ;;annotations.
11205 ((and (c-major-mode-is 'java-mode)
11206 (setq placeholder (point))
11207 (c-beginning-of-statement-1)
11208 (progn
11209 (while (and (c-forward-annotation)
11210 (< (point) placeholder))
11211 (c-forward-syntactic-ws))
11213 (prog1
11214 (>= (point) placeholder)
11215 (goto-char placeholder)))
11216 (c-beginning-of-statement-1 containing-sexp)
11217 (c-add-syntax 'annotation-var-cont (point)))
11219 ;; CASE G: a template list continuation?
11220 ;; Mostly a duplication of case 5D.3 to fix templates-19:
11221 ((and (c-major-mode-is 'c++-mode)
11222 (save-excursion
11223 (goto-char indent-point)
11224 (c-with-syntax-table c++-template-syntax-table
11225 (setq placeholder (c-up-list-backward)))
11226 (and placeholder
11227 (eq (char-after placeholder) ?<)
11228 (/= (char-before placeholder) ?<)
11229 (progn
11230 (goto-char (1+ placeholder))
11231 (not (looking-at c-<-op-cont-regexp))))))
11232 (c-with-syntax-table c++-template-syntax-table
11233 (goto-char placeholder)
11234 (c-beginning-of-statement-1 containing-sexp t))
11235 (if (save-excursion
11236 (c-backward-syntactic-ws containing-sexp)
11237 (eq (char-before) ?<))
11238 ;; In a nested template arglist.
11239 (progn
11240 (goto-char placeholder)
11241 (c-syntactic-skip-backward "^,;" containing-sexp t)
11242 (c-forward-syntactic-ws))
11243 (back-to-indentation))
11244 ;; FIXME: Should use c-add-stmt-syntax, but it's not yet
11245 ;; template aware.
11246 (c-add-syntax 'template-args-cont (point) placeholder))
11248 ;; CASE D: continued statement.
11250 (c-beginning-of-statement-1 containing-sexp)
11251 (c-add-stmt-syntax 'statement-cont nil nil
11252 containing-sexp paren-state))
11255 ;; The next autoload was added by RMS on 2005/8/9 - don't know why (ACM,
11256 ;; 2005/11/29).
11257 ;;;###autoload
11258 (defun c-guess-basic-syntax ()
11259 "Return the syntactic context of the current line."
11260 (save-excursion
11261 (beginning-of-line)
11262 (c-save-buffer-state
11263 ((indent-point (point))
11264 (case-fold-search nil)
11265 ;; A whole ugly bunch of various temporary variables. Have
11266 ;; to declare them here since it's not possible to declare
11267 ;; a variable with only the scope of a cond test and the
11268 ;; following result clauses, and most of this function is a
11269 ;; single gigantic cond. :P
11270 literal char-before-ip before-ws-ip char-after-ip macro-start
11271 in-macro-expr c-syntactic-context placeholder c-in-literal-cache
11272 step-type tmpsymbol keyword injava-inher special-brace-list tmp-pos
11273 containing-<
11274 ;; The following record some positions for the containing
11275 ;; declaration block if we're directly within one:
11276 ;; `containing-decl-open' is the position of the open
11277 ;; brace. `containing-decl-start' is the start of the
11278 ;; declaration. `containing-decl-kwd' is the keyword
11279 ;; symbol of the keyword that tells what kind of block it
11280 ;; is.
11281 containing-decl-open
11282 containing-decl-start
11283 containing-decl-kwd
11284 ;; The open paren of the closest surrounding sexp or nil if
11285 ;; there is none.
11286 containing-sexp
11287 ;; The position after the closest preceding brace sexp
11288 ;; (nested sexps are ignored), or the position after
11289 ;; `containing-sexp' if there is none, or (point-min) if
11290 ;; `containing-sexp' is nil.
11292 ;; The paren state outside `containing-sexp', or at
11293 ;; `indent-point' if `containing-sexp' is nil.
11294 (paren-state (c-parse-state))
11295 ;; There's always at most one syntactic element which got
11296 ;; an anchor pos. It's stored in syntactic-relpos.
11297 syntactic-relpos
11298 (c-stmt-delim-chars c-stmt-delim-chars))
11300 ;; Check if we're directly inside an enclosing declaration
11301 ;; level block.
11302 (when (and (setq containing-sexp
11303 (c-most-enclosing-brace paren-state))
11304 (progn
11305 (goto-char containing-sexp)
11306 (eq (char-after) ?{))
11307 (setq placeholder
11308 (c-looking-at-decl-block
11309 (c-most-enclosing-brace paren-state
11310 containing-sexp)
11311 t)))
11312 (setq containing-decl-open containing-sexp
11313 containing-decl-start (point)
11314 containing-sexp nil)
11315 (goto-char placeholder)
11316 (setq containing-decl-kwd (and (looking-at c-keywords-regexp)
11317 (c-keyword-sym (match-string 1)))))
11319 ;; Init some position variables.
11320 (if paren-state
11321 (progn
11322 (setq containing-sexp (car paren-state)
11323 paren-state (cdr paren-state))
11324 (if (consp containing-sexp)
11325 (save-excursion
11326 (goto-char (cdr containing-sexp))
11327 (if (and (c-major-mode-is 'c++-mode)
11328 (c-back-over-member-initializer-braces))
11329 (c-syntactic-skip-backward "^}" nil t))
11330 (setq lim (point))
11331 (if paren-state
11332 ;; Ignore balanced paren. The next entry
11333 ;; can't be another one.
11334 (setq containing-sexp (car paren-state)
11335 paren-state (cdr paren-state))
11336 ;; If there is no surrounding open paren then
11337 ;; put the last balanced pair back on paren-state.
11338 (setq paren-state (cons containing-sexp paren-state)
11339 containing-sexp nil)))
11340 (setq lim (1+ containing-sexp))))
11341 (setq lim (point-min)))
11343 ;; If we're in a parenthesis list then ',' delimits the
11344 ;; "statements" rather than being an operator (with the
11345 ;; exception of the "for" clause). This difference is
11346 ;; typically only noticeable when statements are used in macro
11347 ;; arglists.
11348 (when (and containing-sexp
11349 (eq (char-after containing-sexp) ?\())
11350 (setq c-stmt-delim-chars c-stmt-delim-chars-with-comma))
11351 ;; cache char before and after indent point, and move point to
11352 ;; the most likely position to perform the majority of tests
11353 (goto-char indent-point)
11354 (c-backward-syntactic-ws lim)
11355 (setq before-ws-ip (point)
11356 char-before-ip (char-before))
11357 (goto-char indent-point)
11358 (skip-chars-forward " \t")
11359 (setq char-after-ip (char-after))
11361 ;; are we in a literal?
11362 (setq literal (c-in-literal lim))
11364 ;; now figure out syntactic qualities of the current line
11365 (cond
11367 ;; CASE 1: in a string.
11368 ((eq literal 'string)
11369 (c-add-syntax 'string (c-point 'bopl)))
11371 ;; CASE 2: in a C or C++ style comment.
11372 ((and (memq literal '(c c++))
11373 ;; This is a kludge for XEmacs where we use
11374 ;; `buffer-syntactic-context', which doesn't correctly
11375 ;; recognize "\*/" to end a block comment.
11376 ;; `parse-partial-sexp' which is used by
11377 ;; `c-literal-limits' will however do that in most
11378 ;; versions, which results in that we get nil from
11379 ;; `c-literal-limits' even when `c-in-literal' claims
11380 ;; we're inside a comment.
11381 (setq placeholder (c-literal-start lim)))
11382 (c-add-syntax literal placeholder))
11384 ;; CASE 3: in a cpp preprocessor macro continuation.
11385 ((and (save-excursion
11386 (when (c-beginning-of-macro)
11387 (setq macro-start (point))))
11388 (/= macro-start (c-point 'boi))
11389 (progn
11390 (setq tmpsymbol 'cpp-macro-cont)
11391 (or (not c-syntactic-indentation-in-macros)
11392 (save-excursion
11393 (goto-char macro-start)
11394 ;; If at the beginning of the body of a #define
11395 ;; directive then analyze as cpp-define-intro
11396 ;; only. Go on with the syntactic analysis
11397 ;; otherwise. in-macro-expr is set if we're in a
11398 ;; cpp expression, i.e. before the #define body
11399 ;; or anywhere in a non-#define directive.
11400 (if (c-forward-to-cpp-define-body)
11401 (let ((indent-boi (c-point 'boi indent-point)))
11402 (setq in-macro-expr (> (point) indent-boi)
11403 tmpsymbol 'cpp-define-intro)
11404 (= (point) indent-boi))
11405 (setq in-macro-expr t)
11406 nil)))))
11407 (c-add-syntax tmpsymbol macro-start)
11408 (setq macro-start nil))
11410 ;; CASE 11: an else clause?
11411 ((looking-at "else\\>[^_]")
11412 (c-beginning-of-statement-1 containing-sexp)
11413 (c-add-stmt-syntax 'else-clause nil t
11414 containing-sexp paren-state))
11416 ;; CASE 12: while closure of a do/while construct?
11417 ((and (looking-at "while\\>[^_]")
11418 (save-excursion
11419 (prog1 (eq (c-beginning-of-statement-1 containing-sexp)
11420 'beginning)
11421 (setq placeholder (point)))))
11422 (goto-char placeholder)
11423 (c-add-stmt-syntax 'do-while-closure nil t
11424 containing-sexp paren-state))
11426 ;; CASE 13: A catch or finally clause? This case is simpler
11427 ;; than if-else and do-while, because a block is required
11428 ;; after every try, catch and finally.
11429 ((save-excursion
11430 (and (cond ((c-major-mode-is 'c++-mode)
11431 (looking-at "catch\\>[^_]"))
11432 ((c-major-mode-is 'java-mode)
11433 (looking-at "\\(catch\\|finally\\)\\>[^_]")))
11434 (and (c-safe (c-backward-syntactic-ws)
11435 (c-backward-sexp)
11437 (eq (char-after) ?{)
11438 (c-safe (c-backward-syntactic-ws)
11439 (c-backward-sexp)
11441 (if (eq (char-after) ?\()
11442 (c-safe (c-backward-sexp) t)
11444 (looking-at "\\(try\\|catch\\)\\>[^_]")
11445 (setq placeholder (point))))
11446 (goto-char placeholder)
11447 (c-add-stmt-syntax 'catch-clause nil t
11448 containing-sexp paren-state))
11450 ;; CASE 18: A substatement we can recognize by keyword.
11451 ((save-excursion
11452 (and c-opt-block-stmt-key
11453 (not (eq char-before-ip ?\;))
11454 (not (c-at-vsemi-p before-ws-ip))
11455 (not (memq char-after-ip '(?\) ?\] ?,)))
11456 (or (not (eq char-before-ip ?}))
11457 (c-looking-at-inexpr-block-backward c-state-cache))
11458 (> (point)
11459 (progn
11460 ;; Ought to cache the result from the
11461 ;; c-beginning-of-statement-1 calls here.
11462 (setq placeholder (point))
11463 (while (eq (setq step-type
11464 (c-beginning-of-statement-1 lim))
11465 'label))
11466 (if (eq step-type 'previous)
11467 (goto-char placeholder)
11468 (setq placeholder (point))
11469 (if (and (eq step-type 'same)
11470 (not (looking-at c-opt-block-stmt-key)))
11471 ;; Step up to the containing statement if we
11472 ;; stayed in the same one.
11473 (let (step)
11474 (while (eq
11475 (setq step
11476 (c-beginning-of-statement-1 lim))
11477 'label))
11478 (if (eq step 'up)
11479 (setq placeholder (point))
11480 ;; There was no containing statement after all.
11481 (goto-char placeholder)))))
11482 placeholder))
11483 (if (looking-at c-block-stmt-2-key)
11484 ;; Require a parenthesis after these keywords.
11485 ;; Necessary to catch e.g. synchronized in Java,
11486 ;; which can be used both as statement and
11487 ;; modifier.
11488 (and (zerop (c-forward-token-2 1 nil))
11489 (eq (char-after) ?\())
11490 (looking-at c-opt-block-stmt-key))))
11492 (if (eq step-type 'up)
11493 ;; CASE 18A: Simple substatement.
11494 (progn
11495 (goto-char placeholder)
11496 (cond
11497 ((eq char-after-ip ?{)
11498 (c-add-stmt-syntax 'substatement-open nil nil
11499 containing-sexp paren-state))
11500 ((save-excursion
11501 (goto-char indent-point)
11502 (back-to-indentation)
11503 (c-forward-label))
11504 (c-add-stmt-syntax 'substatement-label nil nil
11505 containing-sexp paren-state))
11507 (c-add-stmt-syntax 'substatement nil nil
11508 containing-sexp paren-state))))
11510 ;; CASE 18B: Some other substatement. This is shared
11511 ;; with case 10.
11512 (c-guess-continued-construct indent-point
11513 char-after-ip
11514 placeholder
11516 paren-state)))
11518 ;; CASE 14: A case or default label
11519 ((save-excursion
11520 (and (looking-at c-label-kwds-regexp)
11521 (or (c-major-mode-is 'idl-mode)
11522 (and
11523 containing-sexp
11524 (goto-char containing-sexp)
11525 (eq (char-after) ?{)
11526 (progn (c-backward-syntactic-ws) t)
11527 (eq (char-before) ?\))
11528 (c-go-list-backward)
11529 (progn (c-backward-syntactic-ws) t)
11530 (c-simple-skip-symbol-backward)
11531 (looking-at c-block-stmt-2-key)))))
11532 (if containing-sexp
11533 (progn
11534 (goto-char containing-sexp)
11535 (setq lim (c-most-enclosing-brace c-state-cache
11536 containing-sexp))
11537 (c-backward-to-block-anchor lim)
11538 (c-add-stmt-syntax 'case-label nil t lim paren-state))
11539 ;; Got a bogus label at the top level. In lack of better
11540 ;; alternatives, anchor it on (point-min).
11541 (c-add-syntax 'case-label (point-min))))
11543 ;; CASE 15: any other label
11544 ((save-excursion
11545 (back-to-indentation)
11546 (and (not (looking-at c-syntactic-ws-start))
11547 (not (looking-at c-label-kwds-regexp))
11548 (c-forward-label)))
11549 (cond (containing-decl-open
11550 (setq placeholder (c-add-class-syntax 'inclass
11551 containing-decl-open
11552 containing-decl-start
11553 containing-decl-kwd
11554 paren-state))
11555 ;; Append access-label with the same anchor point as
11556 ;; inclass gets.
11557 (c-append-syntax 'access-label placeholder))
11559 (containing-sexp
11560 (goto-char containing-sexp)
11561 (setq lim (c-most-enclosing-brace c-state-cache
11562 containing-sexp))
11563 (save-excursion
11564 (setq tmpsymbol
11565 (if (and (eq (c-beginning-of-statement-1 lim) 'up)
11566 (looking-at "switch\\>[^_]"))
11567 ;; If the surrounding statement is a switch then
11568 ;; let's analyze all labels as switch labels, so
11569 ;; that they get lined up consistently.
11570 'case-label
11571 'label)))
11572 (c-backward-to-block-anchor lim)
11573 (c-add-stmt-syntax tmpsymbol nil t lim paren-state))
11576 ;; A label on the top level. Treat it as a class
11577 ;; context. (point-min) is the closest we get to the
11578 ;; class open brace.
11579 (c-add-syntax 'access-label (point-min)))))
11581 ;; CASE 4: In-expression statement. C.f. cases 7B, 16A and
11582 ;; 17E.
11583 ((setq placeholder (c-looking-at-inexpr-block
11584 (c-safe-position containing-sexp paren-state)
11585 containing-sexp
11586 ;; Have to turn on the heuristics after
11587 ;; the point even though it doesn't work
11588 ;; very well. C.f. test case class-16.pike.
11590 (setq tmpsymbol (assq (car placeholder)
11591 '((inexpr-class . class-open)
11592 (inexpr-statement . block-open))))
11593 (if tmpsymbol
11594 ;; It's a statement block or an anonymous class.
11595 (setq tmpsymbol (cdr tmpsymbol))
11596 ;; It's a Pike lambda. Check whether we are between the
11597 ;; lambda keyword and the argument list or at the defun
11598 ;; opener.
11599 (setq tmpsymbol (if (eq char-after-ip ?{)
11600 'inline-open
11601 'lambda-intro-cont)))
11602 (goto-char (cdr placeholder))
11603 (back-to-indentation)
11604 (c-add-stmt-syntax tmpsymbol nil t
11605 (c-most-enclosing-brace c-state-cache (point))
11606 paren-state)
11607 (unless (eq (point) (cdr placeholder))
11608 (c-add-syntax (car placeholder))))
11610 ;; CASE 5: Line is inside a declaration level block or at top level.
11611 ((or containing-decl-open (null containing-sexp))
11612 (cond
11614 ;; CASE 5A: we are looking at a defun, brace list, class,
11615 ;; or inline-inclass method opening brace
11616 ((setq special-brace-list
11617 (or (and c-special-brace-lists
11618 (c-looking-at-special-brace-list))
11619 (eq char-after-ip ?{)))
11620 (cond
11622 ;; CASE 5A.1: Non-class declaration block open.
11623 ((save-excursion
11624 (let (tmp)
11625 (and (eq char-after-ip ?{)
11626 (setq tmp (c-looking-at-decl-block containing-sexp t))
11627 (progn
11628 (setq placeholder (point))
11629 (goto-char tmp)
11630 (looking-at c-symbol-key))
11631 (c-keyword-member
11632 (c-keyword-sym (setq keyword (match-string 0)))
11633 'c-other-block-decl-kwds))))
11634 (goto-char placeholder)
11635 (c-add-stmt-syntax
11636 (if (string-equal keyword "extern")
11637 ;; Special case for extern-lang-open.
11638 'extern-lang-open
11639 (intern (concat keyword "-open")))
11640 nil t containing-sexp paren-state))
11642 ;; CASE 5A.2: we are looking at a class opening brace
11643 ((save-excursion
11644 (goto-char indent-point)
11645 (skip-chars-forward " \t")
11646 (and (eq (char-after) ?{)
11647 (c-looking-at-decl-block containing-sexp t)
11648 (setq placeholder (point))))
11649 (c-add-syntax 'class-open placeholder))
11651 ;; CASE 5A.3: brace list open
11652 ((save-excursion
11653 (goto-char indent-point)
11654 (skip-chars-forward " \t")
11655 (cond
11656 ((c-backward-over-enum-header)
11657 (setq placeholder (c-point 'boi)))
11658 ((consp (setq placeholder
11659 (c-looking-at-or-maybe-in-bracelist
11660 containing-sexp lim)))
11661 (setq tmpsymbol (and (cdr placeholder) 'topmost-intro-cont))
11662 (setq placeholder (c-point 'boi (car placeholder))))))
11663 (if (and (not c-auto-newline-analysis)
11664 ;(c-major-mode-is 'java-mode) ; Not needed anymore (2016-08-30).
11665 (eq tmpsymbol 'topmost-intro-cont))
11666 ;; We're in Java and have found that the open brace
11667 ;; belongs to a "new Foo[]" initialization list,
11668 ;; which means the brace list is part of an
11669 ;; expression and not a top level definition. We
11670 ;; therefore treat it as any topmost continuation
11671 ;; even though the semantically correct symbol still
11672 ;; is brace-list-open, on the same grounds as in
11673 ;; case B.2.
11674 (progn
11675 (c-beginning-of-statement-1 lim)
11676 (c-add-syntax 'topmost-intro-cont (c-point 'boi)))
11677 (c-add-syntax 'brace-list-open placeholder)))
11679 ;; CASE 5A.4: inline defun open
11680 ((and containing-decl-open
11681 (not (c-keyword-member containing-decl-kwd
11682 'c-other-block-decl-kwds)))
11683 (c-add-syntax 'inline-open)
11684 (c-add-class-syntax 'inclass
11685 containing-decl-open
11686 containing-decl-start
11687 containing-decl-kwd
11688 paren-state))
11690 ;; CASE 5A.5: ordinary defun open
11692 (save-excursion
11693 (c-beginning-of-decl-1 lim)
11694 (while (cond
11695 ((looking-at c-specifier-key)
11696 (c-forward-keyword-clause 1))
11697 ((and c-opt-cpp-prefix
11698 (looking-at c-noise-macro-with-parens-name-re))
11699 (c-forward-noise-clause))))
11700 (c-add-syntax 'defun-open (c-point 'boi))
11701 ;; Bogus to use bol here, but it's the legacy. (Resolved,
11702 ;; 2007-11-09)
11703 ))))
11705 ;; CASE 5R: Member init list. (Used to be part of CASE 5B.1)
11706 ;; Note there is no limit on the backward search here, since member
11707 ;; init lists can, in practice, be very large.
11708 ((save-excursion
11709 (when (and (c-major-mode-is 'c++-mode)
11710 (setq placeholder (c-back-over-member-initializers)))
11711 (setq tmp-pos (point))))
11712 (if (= (c-point 'bosws) (1+ tmp-pos))
11713 (progn
11714 ;; There is no preceding member init clause.
11715 ;; Indent relative to the beginning of indentation
11716 ;; for the topmost-intro line that contains the
11717 ;; prototype's open paren.
11718 (goto-char placeholder)
11719 (c-add-syntax 'member-init-intro (c-point 'boi)))
11720 ;; Indent relative to the first member init clause.
11721 (goto-char (1+ tmp-pos))
11722 (c-forward-syntactic-ws)
11723 (c-add-syntax 'member-init-cont (point))))
11725 ;; CASE 5B: After a function header but before the body (or
11726 ;; the ending semicolon if there's no body).
11727 ((save-excursion
11728 (when (setq placeholder (c-just-after-func-arglist-p
11729 (max lim (c-determine-limit 500))))
11730 (setq tmp-pos (point))))
11731 (cond
11733 ;; CASE 5B.1: Member init list.
11734 ((eq (char-after tmp-pos) ?:)
11735 ;; There is no preceding member init clause.
11736 ;; Indent relative to the beginning of indentation
11737 ;; for the topmost-intro line that contains the
11738 ;; prototype's open paren.
11739 (goto-char placeholder)
11740 (c-add-syntax 'member-init-intro (c-point 'boi)))
11742 ;; CASE 5B.2: K&R arg decl intro
11743 ((and c-recognize-knr-p
11744 (c-in-knr-argdecl lim))
11745 (c-beginning-of-statement-1 lim)
11746 (c-add-syntax 'knr-argdecl-intro (c-point 'boi))
11747 (if containing-decl-open
11748 (c-add-class-syntax 'inclass
11749 containing-decl-open
11750 containing-decl-start
11751 containing-decl-kwd
11752 paren-state)))
11754 ;; CASE 5B.4: Nether region after a C++ or Java func
11755 ;; decl, which could include a `throws' declaration.
11757 (c-beginning-of-statement-1 lim)
11758 (c-add-syntax 'func-decl-cont (c-point 'boi))
11761 ;; CASE 5C: inheritance line. could be first inheritance
11762 ;; line, or continuation of a multiple inheritance
11763 ((or (and (c-major-mode-is 'c++-mode)
11764 (progn
11765 (when (eq char-after-ip ?,)
11766 (skip-chars-forward " \t")
11767 (forward-char))
11768 (looking-at c-opt-postfix-decl-spec-key)))
11769 (and (or (eq char-before-ip ?:)
11770 ;; watch out for scope operator
11771 (save-excursion
11772 (and (eq char-after-ip ?:)
11773 (c-safe (forward-char 1) t)
11774 (not (eq (char-after) ?:))
11776 (save-excursion
11777 (c-beginning-of-statement-1 lim)
11778 (when (looking-at c-opt-<>-sexp-key)
11779 (goto-char (match-end 1))
11780 (c-forward-syntactic-ws)
11781 (c-forward-<>-arglist nil)
11782 (c-forward-syntactic-ws))
11783 (looking-at c-class-key)))
11784 ;; for Java
11785 (and (c-major-mode-is 'java-mode)
11786 (let ((fence (save-excursion
11787 (c-beginning-of-statement-1 lim)
11788 (point)))
11789 cont done)
11790 (save-excursion
11791 (while (not done)
11792 (cond ((looking-at c-opt-postfix-decl-spec-key)
11793 (setq injava-inher (cons cont (point))
11794 done t))
11795 ((or (not (c-safe (c-forward-sexp -1) t))
11796 (<= (point) fence))
11797 (setq done t))
11799 (setq cont t)))
11800 injava-inher)
11801 (not (c-crosses-statement-barrier-p (cdr injava-inher)
11802 (point)))
11804 (cond
11806 ;; CASE 5C.1: non-hanging colon on an inher intro
11807 ((eq char-after-ip ?:)
11808 (c-beginning-of-statement-1 lim)
11809 (c-add-syntax 'inher-intro (c-point 'boi))
11810 ;; don't add inclass symbol since relative point already
11811 ;; contains any class offset
11814 ;; CASE 5C.2: hanging colon on an inher intro
11815 ((eq char-before-ip ?:)
11816 (c-beginning-of-statement-1 lim)
11817 (c-add-syntax 'inher-intro (c-point 'boi))
11818 (if containing-decl-open
11819 (c-add-class-syntax 'inclass
11820 containing-decl-open
11821 containing-decl-start
11822 containing-decl-kwd
11823 paren-state)))
11825 ;; CASE 5C.3: in a Java implements/extends
11826 (injava-inher
11827 (let ((where (cdr injava-inher))
11828 (cont (car injava-inher)))
11829 (goto-char where)
11830 (cond ((looking-at "throws\\>[^_]")
11831 (c-add-syntax 'func-decl-cont
11832 (progn (c-beginning-of-statement-1 lim)
11833 (c-point 'boi))))
11834 (cont (c-add-syntax 'inher-cont where))
11835 (t (c-add-syntax 'inher-intro
11836 (progn (goto-char (cdr injava-inher))
11837 (c-beginning-of-statement-1 lim)
11838 (point))))
11841 ;; CASE 5C.4: a continued inheritance line
11843 (c-beginning-of-inheritance-list lim)
11844 (c-add-syntax 'inher-cont (point))
11845 ;; don't add inclass symbol since relative point already
11846 ;; contains any class offset
11849 ;; CASE 5P: AWK pattern or function or continuation
11850 ;; thereof.
11851 ((c-major-mode-is 'awk-mode)
11852 (setq placeholder (point))
11853 (c-add-stmt-syntax
11854 (if (and (eq (c-beginning-of-statement-1) 'same)
11855 (/= (point) placeholder))
11856 'topmost-intro-cont
11857 'topmost-intro)
11858 nil nil
11859 containing-sexp paren-state))
11861 ;; CASE 5D: this could be a top-level initialization, a
11862 ;; member init list continuation, or a template argument
11863 ;; list continuation.
11864 ((save-excursion
11865 ;; Note: We use the fact that lim is always after any
11866 ;; preceding brace sexp.
11867 (if c-recognize-<>-arglists
11868 (while (and
11869 (progn
11870 (c-syntactic-skip-backward "^;,=<>" lim t)
11871 (> (point) lim))
11873 (when c-overloadable-operators-regexp
11874 (when (setq placeholder (c-after-special-operator-id lim))
11875 (goto-char placeholder)
11877 (cond
11878 ((eq (char-before) ?>)
11879 (or (c-backward-<>-arglist nil lim)
11880 (backward-char))
11882 ((eq (char-before) ?<)
11883 (backward-char)
11884 (if (save-excursion
11885 (c-forward-<>-arglist nil))
11886 (progn (forward-char)
11887 nil)
11889 (t nil)))))
11890 ;; NB: No c-after-special-operator-id stuff in this
11891 ;; clause - we assume only C++ needs it.
11892 (c-syntactic-skip-backward "^;,=" lim t))
11893 (memq (char-before) '(?, ?= ?<)))
11894 (cond
11896 ;; CASE 5D.3: perhaps a template list continuation?
11897 ((and (c-major-mode-is 'c++-mode)
11898 (save-excursion
11899 (save-restriction
11900 (c-with-syntax-table c++-template-syntax-table
11901 (goto-char indent-point)
11902 (setq placeholder (c-up-list-backward))
11903 (and placeholder
11904 (eq (char-after placeholder) ?<))))))
11905 (c-with-syntax-table c++-template-syntax-table
11906 (goto-char placeholder)
11907 (c-beginning-of-statement-1 lim t))
11908 (if (save-excursion
11909 (c-backward-syntactic-ws lim)
11910 (eq (char-before) ?<))
11911 ;; In a nested template arglist.
11912 (progn
11913 (goto-char placeholder)
11914 (c-syntactic-skip-backward "^,;" lim t)
11915 (c-forward-syntactic-ws))
11916 (back-to-indentation))
11917 ;; FIXME: Should use c-add-stmt-syntax, but it's not yet
11918 ;; template aware.
11919 (c-add-syntax 'template-args-cont (point) placeholder))
11921 ;; CASE 5D.4: perhaps a multiple inheritance line?
11922 ((and (c-major-mode-is 'c++-mode)
11923 (save-excursion
11924 (c-beginning-of-statement-1 lim)
11925 (setq placeholder (point))
11926 (if (looking-at "static\\>[^_]")
11927 (c-forward-token-2 1 nil indent-point))
11928 (and (looking-at c-class-key)
11929 (zerop (c-forward-token-2 2 nil indent-point))
11930 (if (eq (char-after) ?<)
11931 (c-with-syntax-table c++-template-syntax-table
11932 (zerop (c-forward-token-2 1 t indent-point)))
11934 (eq (char-after) ?:))))
11935 (goto-char placeholder)
11936 (c-add-syntax 'inher-cont (c-point 'boi)))
11938 ;; CASE 5D.5: Continuation of the "expression part" of a
11939 ;; top level construct. Or, perhaps, an unrecognized construct.
11941 (while (and (setq placeholder (point))
11942 (eq (car (c-beginning-of-decl-1 containing-sexp)) ; Can't use `lim' here.
11943 'same)
11944 (save-excursion
11945 (c-backward-syntactic-ws)
11946 (eq (char-before) ?}))
11947 (< (point) placeholder)))
11948 (c-add-stmt-syntax
11949 (cond
11950 ((eq (point) placeholder) 'statement) ; unrecognized construct
11951 ;; A preceding comma at the top level means that a
11952 ;; new variable declaration starts here. Use
11953 ;; topmost-intro-cont for it, for consistency with
11954 ;; the first variable declaration. C.f. case 5N.
11955 ((eq char-before-ip ?,) 'topmost-intro-cont)
11956 (t 'statement-cont))
11957 nil nil containing-sexp paren-state))
11960 ;; CASE 5F: Close of a non-class declaration level block.
11961 ((and (eq char-after-ip ?})
11962 (c-keyword-member containing-decl-kwd
11963 'c-other-block-decl-kwds))
11964 ;; This is inconsistent: Should use `containing-decl-open'
11965 ;; here if it's at boi, like in case 5J.
11966 (goto-char containing-decl-start)
11967 (c-add-stmt-syntax
11968 (if (string-equal (symbol-name containing-decl-kwd) "extern")
11969 ;; Special case for compatibility with the
11970 ;; extern-lang syntactic symbols.
11971 'extern-lang-close
11972 (intern (concat (symbol-name containing-decl-kwd)
11973 "-close")))
11974 nil t
11975 (c-most-enclosing-brace paren-state (point))
11976 paren-state))
11978 ;; CASE 5G: we are looking at the brace which closes the
11979 ;; enclosing nested class decl
11980 ((and containing-sexp
11981 (eq char-after-ip ?})
11982 (eq containing-decl-open containing-sexp))
11983 (c-add-class-syntax 'class-close
11984 containing-decl-open
11985 containing-decl-start
11986 containing-decl-kwd
11987 paren-state))
11989 ;; CASE 5H: we could be looking at subsequent knr-argdecls
11990 ((and c-recognize-knr-p
11991 (not containing-sexp) ; can't be knr inside braces.
11992 (not (eq char-before-ip ?}))
11993 (save-excursion
11994 (setq placeholder (cdr (c-beginning-of-decl-1 lim)))
11995 (and placeholder
11996 ;; Do an extra check to avoid tripping up on
11997 ;; statements that occur in invalid contexts
11998 ;; (e.g. in macro bodies where we don't really
11999 ;; know the context of what we're looking at).
12000 (not (and c-opt-block-stmt-key
12001 (looking-at c-opt-block-stmt-key)))))
12002 (< placeholder indent-point))
12003 (goto-char placeholder)
12004 (c-add-syntax 'knr-argdecl (point)))
12006 ;; CASE 5I: ObjC method definition.
12007 ((and c-opt-method-key
12008 (looking-at c-opt-method-key))
12009 (c-beginning-of-statement-1 nil t)
12010 (if (= (point) indent-point)
12011 ;; Handle the case when it's the first (non-comment)
12012 ;; thing in the buffer. Can't look for a 'same return
12013 ;; value from cbos1 since ObjC directives currently
12014 ;; aren't recognized fully, so that we get 'same
12015 ;; instead of 'previous if it moved over a preceding
12016 ;; directive.
12017 (goto-char (point-min)))
12018 (c-add-syntax 'objc-method-intro (c-point 'boi)))
12020 ;; CASE 5N: At a variable declaration that follows a class
12021 ;; definition or some other block declaration that doesn't
12022 ;; end at the closing '}'. C.f. case 5D.5.
12023 ((progn
12024 (c-backward-syntactic-ws lim)
12025 (and (eq (char-before) ?})
12026 (save-excursion
12027 (let ((start (point)))
12028 (if (and c-state-cache
12029 (consp (car c-state-cache))
12030 (eq (cdar c-state-cache) (point)))
12031 ;; Speed up the backward search a bit.
12032 (goto-char (caar c-state-cache)))
12033 (c-beginning-of-decl-1 containing-sexp) ; Can't use `lim' here.
12034 (setq placeholder (point))
12035 (if (= start (point))
12036 ;; The '}' is unbalanced.
12038 (c-end-of-decl-1)
12039 (>= (point) indent-point))))))
12040 (goto-char placeholder)
12041 (c-add-stmt-syntax 'topmost-intro-cont nil nil
12042 containing-sexp paren-state))
12044 ;; NOTE: The point is at the end of the previous token here.
12046 ;; CASE 5J: we are at the topmost level, make
12047 ;; sure we skip back past any access specifiers
12048 ((and
12049 ;; A macro continuation line is never at top level.
12050 (not (and macro-start
12051 (> indent-point macro-start)))
12052 (save-excursion
12053 (setq placeholder (point))
12054 (or (memq char-before-ip '(?\; ?{ ?} nil))
12055 (c-at-vsemi-p before-ws-ip)
12056 (when (and (eq char-before-ip ?:)
12057 (eq (c-beginning-of-statement-1 lim)
12058 'label))
12059 (c-backward-syntactic-ws lim)
12060 (setq placeholder (point)))
12061 (and (c-major-mode-is 'objc-mode)
12062 (catch 'not-in-directive
12063 (c-beginning-of-statement-1 lim)
12064 (setq placeholder (point))
12065 (while (and (c-forward-objc-directive)
12066 (< (point) indent-point))
12067 (c-forward-syntactic-ws)
12068 (if (>= (point) indent-point)
12069 (throw 'not-in-directive t))
12070 (setq placeholder (point)))
12071 nil)))))
12072 ;; For historic reasons we anchor at bol of the last
12073 ;; line of the previous declaration. That's clearly
12074 ;; highly bogus and useless, and it makes our lives hard
12075 ;; to remain compatible. :P
12076 (goto-char placeholder)
12077 (c-add-syntax 'topmost-intro (c-point 'bol))
12078 (if containing-decl-open
12079 (if (c-keyword-member containing-decl-kwd
12080 'c-other-block-decl-kwds)
12081 (progn
12082 (goto-char (c-brace-anchor-point containing-decl-open))
12083 (c-add-stmt-syntax
12084 (if (string-equal (symbol-name containing-decl-kwd)
12085 "extern")
12086 ;; Special case for compatibility with the
12087 ;; extern-lang syntactic symbols.
12088 'inextern-lang
12089 (intern (concat "in"
12090 (symbol-name containing-decl-kwd))))
12091 nil t
12092 (c-most-enclosing-brace paren-state (point))
12093 paren-state))
12094 (c-add-class-syntax 'inclass
12095 containing-decl-open
12096 containing-decl-start
12097 containing-decl-kwd
12098 paren-state)))
12099 (when (and c-syntactic-indentation-in-macros
12100 macro-start
12101 (/= macro-start (c-point 'boi indent-point)))
12102 (c-add-syntax 'cpp-define-intro)
12103 (setq macro-start nil)))
12105 ;; CASE 5K: we are at an ObjC method definition
12106 ;; continuation line.
12107 ((and c-opt-method-key
12108 (save-excursion
12109 (c-beginning-of-statement-1 lim)
12110 (beginning-of-line)
12111 (when (looking-at c-opt-method-key)
12112 (setq placeholder (point)))))
12113 (c-add-syntax 'objc-method-args-cont placeholder))
12115 ;; CASE 5L: we are at the first argument of a template
12116 ;; arglist that begins on the previous line.
12117 ((and c-recognize-<>-arglists
12118 (eq (char-before) ?<)
12119 (not (and c-overloadable-operators-regexp
12120 (c-after-special-operator-id lim))))
12121 (c-beginning-of-statement-1 (c-safe-position (point) paren-state))
12122 (c-add-syntax 'template-args-cont (c-point 'boi)))
12124 ;; CASE 5Q: we are at a statement within a macro.
12125 (macro-start
12126 (c-beginning-of-statement-1 containing-sexp)
12127 (c-add-stmt-syntax 'statement nil t containing-sexp paren-state))
12129 ;;CASE 5N: We are at a topmost continuation line and the only
12130 ;;preceding items are annotations.
12131 ((and (c-major-mode-is 'java-mode)
12132 (setq placeholder (point))
12133 (c-beginning-of-statement-1)
12134 (progn
12135 (while (and (c-forward-annotation))
12136 (c-forward-syntactic-ws))
12138 (prog1
12139 (>= (point) placeholder)
12140 (goto-char placeholder)))
12141 (c-add-syntax 'annotation-top-cont (c-point 'boi)))
12143 ;; CASE 5M: we are at a topmost continuation line
12145 (c-beginning-of-statement-1 (c-safe-position (point) paren-state))
12146 (when (c-major-mode-is 'objc-mode)
12147 (setq placeholder (point))
12148 (while (and (c-forward-objc-directive)
12149 (< (point) indent-point))
12150 (c-forward-syntactic-ws)
12151 (setq placeholder (point)))
12152 (goto-char placeholder))
12153 (c-add-syntax 'topmost-intro-cont (c-point 'boi)))
12156 ;; (CASE 6 has been removed.)
12158 ;; CASE 7: line is an expression, not a statement. Most
12159 ;; likely we are either in a function prototype or a function
12160 ;; call argument list
12161 ((not (or (and c-special-brace-lists
12162 (save-excursion
12163 (goto-char containing-sexp)
12164 (c-looking-at-special-brace-list)))
12165 (eq (char-after containing-sexp) ?{)))
12166 (cond
12168 ;; CASE 7A: we are looking at the arglist closing paren.
12169 ;; C.f. case 7F.
12170 ((memq char-after-ip '(?\) ?\]))
12171 (goto-char containing-sexp)
12172 (setq placeholder (c-point 'boi))
12173 (if (and (c-safe (backward-up-list 1) t)
12174 (>= (point) placeholder))
12175 (progn
12176 (forward-char)
12177 (skip-chars-forward " \t"))
12178 (goto-char placeholder))
12179 (c-add-stmt-syntax 'arglist-close (list containing-sexp) t
12180 (c-most-enclosing-brace paren-state (point))
12181 paren-state))
12183 ;; CASE 7B: Looking at the opening brace of an
12184 ;; in-expression block or brace list. C.f. cases 4, 16A
12185 ;; and 17E.
12186 ((and (eq char-after-ip ?{)
12187 (progn
12188 (setq placeholder (c-inside-bracelist-p (point)
12189 paren-state))
12190 (if placeholder
12191 (setq tmpsymbol '(brace-list-open . inexpr-class))
12192 (setq tmpsymbol '(block-open . inexpr-statement)
12193 placeholder
12194 (cdr-safe (c-looking-at-inexpr-block
12195 (c-safe-position containing-sexp
12196 paren-state)
12197 containing-sexp)))
12198 ;; placeholder is nil if it's a block directly in
12199 ;; a function arglist. That makes us skip out of
12200 ;; this case.
12202 (goto-char placeholder)
12203 (back-to-indentation)
12204 (c-add-stmt-syntax (car tmpsymbol) nil t
12205 (c-most-enclosing-brace paren-state (point))
12206 paren-state)
12207 (if (/= (point) placeholder)
12208 (c-add-syntax (cdr tmpsymbol))))
12210 ;; CASE 7C: we are looking at the first argument in an empty
12211 ;; argument list. Use arglist-close if we're actually
12212 ;; looking at a close paren or bracket.
12213 ((memq char-before-ip '(?\( ?\[))
12214 (goto-char containing-sexp)
12215 (setq placeholder (c-point 'boi))
12216 (if (and (c-safe (backward-up-list 1) t)
12217 (>= (point) placeholder))
12218 (progn
12219 (forward-char)
12220 (skip-chars-forward " \t"))
12221 (goto-char placeholder))
12222 (c-add-stmt-syntax 'arglist-intro (list containing-sexp) t
12223 (c-most-enclosing-brace paren-state (point))
12224 paren-state))
12226 ;; CASE 7D: we are inside a conditional test clause. treat
12227 ;; these things as statements
12228 ((progn
12229 (goto-char containing-sexp)
12230 (and (c-safe (c-forward-sexp -1) t)
12231 (looking-at "\\<for\\>[^_]")))
12232 (goto-char (1+ containing-sexp))
12233 (c-forward-syntactic-ws indent-point)
12234 (if (eq char-before-ip ?\;)
12235 (c-add-syntax 'statement (point))
12236 (c-add-syntax 'statement-cont (point))
12239 ;; CASE 7E: maybe a continued ObjC method call. This is the
12240 ;; case when we are inside a [] bracketed exp, and what
12241 ;; precede the opening bracket is not an identifier.
12242 ((and c-opt-method-key
12243 (eq (char-after containing-sexp) ?\[)
12244 (progn
12245 (goto-char (1- containing-sexp))
12246 (c-backward-syntactic-ws (c-point 'bod))
12247 (if (not (looking-at c-symbol-key))
12248 (c-add-syntax 'objc-method-call-cont containing-sexp))
12251 ;; CASE 7F: we are looking at an arglist continuation line,
12252 ;; but the preceding argument is on the same line as the
12253 ;; opening paren. This case includes multi-line
12254 ;; mathematical paren groupings, but we could be on a
12255 ;; for-list continuation line. C.f. case 7A.
12256 ((progn
12257 (goto-char (1+ containing-sexp))
12258 (< (save-excursion
12259 (c-forward-syntactic-ws)
12260 (point))
12261 (c-point 'bonl)))
12262 (goto-char containing-sexp) ; paren opening the arglist
12263 (setq placeholder (c-point 'boi))
12264 (if (and (c-safe (backward-up-list 1) t)
12265 (>= (point) placeholder))
12266 (progn
12267 (forward-char)
12268 (skip-chars-forward " \t"))
12269 (goto-char placeholder))
12270 (c-add-stmt-syntax 'arglist-cont-nonempty (list containing-sexp) t
12271 (c-most-enclosing-brace c-state-cache (point))
12272 paren-state))
12274 ;; CASE 7G: we are looking at just a normal arglist
12275 ;; continuation line
12276 (t (c-forward-syntactic-ws indent-point)
12277 (c-add-syntax 'arglist-cont (c-point 'boi)))
12280 ;; CASE 8: func-local multi-inheritance line
12281 ((and (c-major-mode-is 'c++-mode)
12282 (save-excursion
12283 (goto-char indent-point)
12284 (skip-chars-forward " \t")
12285 (looking-at c-opt-postfix-decl-spec-key)))
12286 (goto-char indent-point)
12287 (skip-chars-forward " \t")
12288 (cond
12290 ;; CASE 8A: non-hanging colon on an inher intro
12291 ((eq char-after-ip ?:)
12292 (c-backward-syntactic-ws lim)
12293 (c-add-syntax 'inher-intro (c-point 'boi)))
12295 ;; CASE 8B: hanging colon on an inher intro
12296 ((eq char-before-ip ?:)
12297 (c-add-syntax 'inher-intro (c-point 'boi)))
12299 ;; CASE 8C: a continued inheritance line
12301 (c-beginning-of-inheritance-list lim)
12302 (c-add-syntax 'inher-cont (point))
12305 ;; CASE 9: we are inside a brace-list
12306 ((and (not (c-major-mode-is 'awk-mode)) ; Maybe this isn't needed (ACM, 2002/3/29)
12307 (setq special-brace-list
12308 (or (and c-special-brace-lists ;;;; ALWAYS NIL FOR AWK!!
12309 (save-excursion
12310 (goto-char containing-sexp)
12311 (c-looking-at-special-brace-list)))
12312 (c-inside-bracelist-p containing-sexp paren-state))))
12313 (cond
12315 ;; CASE 9A: In the middle of a special brace list opener.
12316 ((and (consp special-brace-list)
12317 (save-excursion
12318 (goto-char containing-sexp)
12319 (eq (char-after) ?\())
12320 (eq char-after-ip (car (cdr special-brace-list))))
12321 (goto-char (car (car special-brace-list)))
12322 (skip-chars-backward " \t")
12323 (if (and (bolp)
12324 (assoc 'statement-cont
12325 (setq placeholder (c-guess-basic-syntax))))
12326 (setq c-syntactic-context placeholder)
12327 (c-beginning-of-statement-1
12328 (c-safe-position (1- containing-sexp) paren-state))
12329 (c-forward-token-2 0)
12330 (while (cond
12331 ((looking-at c-specifier-key)
12332 (c-forward-keyword-clause 1))
12333 ((and c-opt-cpp-prefix
12334 (looking-at c-noise-macro-with-parens-name-re))
12335 (c-forward-noise-clause))))
12336 (c-add-syntax 'brace-list-open (c-point 'boi))))
12338 ;; CASE 9B: brace-list-close brace
12339 ((if (consp special-brace-list)
12340 ;; Check special brace list closer.
12341 (progn
12342 (goto-char (car (car special-brace-list)))
12343 (save-excursion
12344 (goto-char indent-point)
12345 (back-to-indentation)
12347 ;; We were between the special close char and the `)'.
12348 (and (eq (char-after) ?\))
12349 (eq (1+ (point)) (cdr (car special-brace-list))))
12350 ;; We were before the special close char.
12351 (and (eq (char-after) (cdr (cdr special-brace-list)))
12352 (zerop (c-forward-token-2))
12353 (eq (1+ (point)) (cdr (car special-brace-list)))))))
12354 ;; Normal brace list check.
12355 (and (eq char-after-ip ?})
12356 (c-safe (goto-char (c-up-list-backward (point))) t)
12357 (= (point) containing-sexp)))
12358 (if (eq (point) (c-point 'boi))
12359 (c-add-syntax 'brace-list-close (point))
12360 (setq lim (c-most-enclosing-brace c-state-cache (point)))
12361 (c-beginning-of-statement-1 lim nil nil t)
12362 (c-add-stmt-syntax 'brace-list-close nil t lim paren-state)))
12365 ;; Prepare for the rest of the cases below by going to the
12366 ;; token following the opening brace
12367 (if (consp special-brace-list)
12368 (progn
12369 (goto-char (car (car special-brace-list)))
12370 (c-forward-token-2 1 nil indent-point))
12371 (goto-char containing-sexp))
12372 (forward-char)
12373 (let ((start (point)))
12374 (c-forward-syntactic-ws indent-point)
12375 (goto-char (max start (c-point 'bol))))
12376 (c-skip-ws-forward indent-point)
12377 (cond
12379 ;; CASE 9C: we're looking at the first line in a brace-list
12380 ((= (point) indent-point)
12381 (if (consp special-brace-list)
12382 (goto-char (car (car special-brace-list)))
12383 (goto-char containing-sexp))
12384 (if (eq (point) (c-point 'boi))
12385 (c-add-syntax 'brace-list-intro (point))
12386 (setq lim (c-most-enclosing-brace c-state-cache (point)))
12387 (c-beginning-of-statement-1 lim)
12388 (c-add-stmt-syntax 'brace-list-intro nil t lim paren-state)))
12390 ;; CASE 9D: this is just a later brace-list-entry or
12391 ;; brace-entry-open
12392 (t (if (or (eq char-after-ip ?{)
12393 (and c-special-brace-lists
12394 (save-excursion
12395 (goto-char indent-point)
12396 (c-forward-syntactic-ws (c-point 'eol))
12397 (c-looking-at-special-brace-list (point)))))
12398 (c-add-syntax 'brace-entry-open (point))
12399 (c-add-stmt-syntax 'brace-list-entry nil t containing-sexp
12400 paren-state (point))
12402 ))))
12404 ;; CASE 10: A continued statement or top level construct.
12405 ((and (not (memq char-before-ip '(?\; ?:)))
12406 (not (c-at-vsemi-p before-ws-ip))
12407 (or (not (eq char-before-ip ?}))
12408 (c-looking-at-inexpr-block-backward c-state-cache))
12409 (> (point)
12410 (save-excursion
12411 (c-beginning-of-statement-1 containing-sexp)
12412 (setq placeholder (point))))
12413 (/= placeholder containing-sexp))
12414 ;; This is shared with case 18.
12415 (c-guess-continued-construct indent-point
12416 char-after-ip
12417 placeholder
12418 containing-sexp
12419 paren-state))
12421 ;; CASE 16: block close brace, possibly closing the defun or
12422 ;; the class
12423 ((eq char-after-ip ?})
12424 ;; From here on we have the next containing sexp in lim.
12425 (setq lim (c-most-enclosing-brace paren-state))
12426 (goto-char containing-sexp)
12427 (cond
12429 ;; CASE 16E: Closing a statement block? This catches
12430 ;; cases where it's preceded by a statement keyword,
12431 ;; which works even when used in an "invalid" context,
12432 ;; e.g. a macro argument.
12433 ((c-after-conditional)
12434 (c-backward-to-block-anchor lim)
12435 (c-add-stmt-syntax 'block-close nil t lim paren-state))
12437 ;; CASE 16A: closing a lambda defun or an in-expression
12438 ;; block? C.f. cases 4, 7B and 17E.
12439 ((setq placeholder (c-looking-at-inexpr-block
12440 (c-safe-position containing-sexp paren-state)
12441 nil))
12442 (setq tmpsymbol (if (eq (car placeholder) 'inlambda)
12443 'inline-close
12444 'block-close))
12445 (goto-char containing-sexp)
12446 (back-to-indentation)
12447 (if (= containing-sexp (point))
12448 (c-add-syntax tmpsymbol (point))
12449 (goto-char (cdr placeholder))
12450 (back-to-indentation)
12451 (c-add-stmt-syntax tmpsymbol nil t
12452 (c-most-enclosing-brace paren-state (point))
12453 paren-state)
12454 (if (/= (point) (cdr placeholder))
12455 (c-add-syntax (car placeholder)))))
12457 ;; CASE 16B: does this close an inline or a function in
12458 ;; a non-class declaration level block?
12459 ((save-excursion
12460 (and lim
12461 (progn
12462 (goto-char lim)
12463 (c-looking-at-decl-block
12464 (c-most-enclosing-brace paren-state lim)
12465 nil))
12466 (setq placeholder (point))))
12467 (c-backward-to-decl-anchor lim)
12468 (back-to-indentation)
12469 (if (save-excursion
12470 (goto-char placeholder)
12471 (looking-at c-other-decl-block-key))
12472 (c-add-syntax 'defun-close (point))
12473 (c-add-syntax 'inline-close (point))))
12475 ;; CASE 16F: Can be a defun-close of a function declared
12476 ;; in a statement block, e.g. in Pike or when using gcc
12477 ;; extensions, but watch out for macros followed by
12478 ;; blocks. Let it through to be handled below.
12479 ;; C.f. cases B.3 and 17G.
12480 ((save-excursion
12481 (and (not (c-at-statement-start-p))
12482 (eq (c-beginning-of-statement-1 lim nil nil t) 'same)
12483 (setq placeholder (point))
12484 (let ((c-recognize-typeless-decls nil))
12485 ;; Turn off recognition of constructs that
12486 ;; lacks a type in this case, since that's more
12487 ;; likely to be a macro followed by a block.
12488 (c-forward-decl-or-cast-1 (c-point 'bosws) nil nil))))
12489 (back-to-indentation)
12490 (if (/= (point) containing-sexp)
12491 (goto-char placeholder))
12492 (c-add-stmt-syntax 'defun-close nil t lim paren-state))
12494 ;; CASE 16C: If there is an enclosing brace then this is
12495 ;; a block close since defun closes inside declaration
12496 ;; level blocks have been handled above.
12497 (lim
12498 ;; If the block is preceded by a case/switch label on
12499 ;; the same line, we anchor at the first preceding label
12500 ;; at boi. The default handling in c-add-stmt-syntax
12501 ;; really fixes it better, but we do like this to keep
12502 ;; the indentation compatible with version 5.28 and
12503 ;; earlier. C.f. case 17H.
12504 (while (and (/= (setq placeholder (point)) (c-point 'boi))
12505 (eq (c-beginning-of-statement-1 lim) 'label)))
12506 (goto-char placeholder)
12507 (if (looking-at c-label-kwds-regexp)
12508 (c-add-syntax 'block-close (point))
12509 (goto-char containing-sexp)
12510 ;; c-backward-to-block-anchor not necessary here; those
12511 ;; situations are handled in case 16E above.
12512 (c-add-stmt-syntax 'block-close nil t lim paren-state)))
12514 ;; CASE 16D: Only top level defun close left.
12516 (goto-char containing-sexp)
12517 (c-backward-to-decl-anchor lim)
12518 (c-add-stmt-syntax 'defun-close nil nil
12519 (c-most-enclosing-brace paren-state)
12520 paren-state))
12523 ;; CASE 19: line is an expression, not a statement, and is directly
12524 ;; contained by a template delimiter. Most likely, we are in a
12525 ;; template arglist within a statement. This case is based on CASE
12526 ;; 7. At some point in the future, we may wish to create more
12527 ;; syntactic symbols such as `template-intro',
12528 ;; `template-cont-nonempty', etc., and distinguish between them as we
12529 ;; do for `arglist-intro' etc. (2009-12-07).
12530 ((and c-recognize-<>-arglists
12531 (setq containing-< (c-up-list-backward indent-point containing-sexp))
12532 (eq (char-after containing-<) ?\<))
12533 (setq placeholder (c-point 'boi containing-<))
12534 (goto-char containing-sexp) ; Most nested Lbrace/Lparen (but not
12535 ; '<') before indent-point.
12536 (if (>= (point) placeholder)
12537 (progn
12538 (forward-char)
12539 (skip-chars-forward " \t"))
12540 (goto-char placeholder))
12541 (c-add-stmt-syntax 'template-args-cont (list containing-<) t
12542 (c-most-enclosing-brace c-state-cache (point))
12543 paren-state))
12545 ;; CASE 17: Statement or defun catchall.
12547 (goto-char indent-point)
12548 ;; Back up statements until we find one that starts at boi.
12549 (while (let* ((prev-point (point))
12550 (last-step-type (c-beginning-of-statement-1
12551 containing-sexp)))
12552 (if (= (point) prev-point)
12553 (progn
12554 (setq step-type (or step-type last-step-type))
12555 nil)
12556 (setq step-type last-step-type)
12557 (/= (point) (c-point 'boi)))))
12558 (cond
12560 ;; CASE 17B: continued statement
12561 ((and (eq step-type 'same)
12562 (/= (point) indent-point))
12563 (c-add-stmt-syntax 'statement-cont nil nil
12564 containing-sexp paren-state))
12566 ;; CASE 17A: After a case/default label?
12567 ((progn
12568 (while (and (eq step-type 'label)
12569 (not (looking-at c-label-kwds-regexp)))
12570 (setq step-type
12571 (c-beginning-of-statement-1 containing-sexp)))
12572 (eq step-type 'label))
12573 (c-add-stmt-syntax (if (eq char-after-ip ?{)
12574 'statement-case-open
12575 'statement-case-intro)
12576 nil t containing-sexp paren-state))
12578 ;; CASE 17D: any old statement
12579 ((progn
12580 (while (eq step-type 'label)
12581 (setq step-type
12582 (c-beginning-of-statement-1 containing-sexp)))
12583 (eq step-type 'previous))
12584 (c-add-stmt-syntax 'statement nil t
12585 containing-sexp paren-state)
12586 (if (eq char-after-ip ?{)
12587 (c-add-syntax 'block-open)))
12589 ;; CASE 17I: Inside a substatement block.
12590 ((progn
12591 ;; The following tests are all based on containing-sexp.
12592 (goto-char containing-sexp)
12593 ;; From here on we have the next containing sexp in lim.
12594 (setq lim (c-most-enclosing-brace paren-state containing-sexp))
12595 (c-after-conditional))
12596 (c-backward-to-block-anchor lim)
12597 (c-add-stmt-syntax 'statement-block-intro nil t
12598 lim paren-state)
12599 (if (eq char-after-ip ?{)
12600 (c-add-syntax 'block-open)))
12602 ;; CASE 17E: first statement in an in-expression block.
12603 ;; C.f. cases 4, 7B and 16A.
12604 ((setq placeholder (c-looking-at-inexpr-block
12605 (c-safe-position containing-sexp paren-state)
12606 nil))
12607 (setq tmpsymbol (if (eq (car placeholder) 'inlambda)
12608 'defun-block-intro
12609 'statement-block-intro))
12610 (back-to-indentation)
12611 (if (= containing-sexp (point))
12612 (c-add-syntax tmpsymbol (point))
12613 (goto-char (cdr placeholder))
12614 (back-to-indentation)
12615 (c-add-stmt-syntax tmpsymbol nil t
12616 (c-most-enclosing-brace c-state-cache (point))
12617 paren-state)
12618 (if (/= (point) (cdr placeholder))
12619 (c-add-syntax (car placeholder))))
12620 (if (eq char-after-ip ?{)
12621 (c-add-syntax 'block-open)))
12623 ;; CASE 17F: first statement in an inline, or first
12624 ;; statement in a top-level defun. we can tell this is it
12625 ;; if there are no enclosing braces that haven't been
12626 ;; narrowed out by a class (i.e. don't use bod here).
12627 ((save-excursion
12628 (or (not (setq placeholder (c-most-enclosing-brace
12629 paren-state)))
12630 (and (progn
12631 (goto-char placeholder)
12632 (eq (char-after) ?{))
12633 (c-looking-at-decl-block (c-most-enclosing-brace
12634 paren-state (point))
12635 nil))))
12636 (c-backward-to-decl-anchor lim)
12637 (back-to-indentation)
12638 (c-add-syntax 'defun-block-intro (point)))
12640 ;; CASE 17G: First statement in a function declared inside
12641 ;; a normal block. This can occur in Pike and with
12642 ;; e.g. the gcc extensions, but watch out for macros
12643 ;; followed by blocks. C.f. cases B.3 and 16F.
12644 ((save-excursion
12645 (and (not (c-at-statement-start-p))
12646 (eq (c-beginning-of-statement-1 lim nil nil t) 'same)
12647 (setq placeholder (point))
12648 (let ((c-recognize-typeless-decls nil))
12649 ;; Turn off recognition of constructs that lacks
12650 ;; a type in this case, since that's more likely
12651 ;; to be a macro followed by a block.
12652 (c-forward-decl-or-cast-1 (c-point 'bosws) nil nil))))
12653 (back-to-indentation)
12654 (if (/= (point) containing-sexp)
12655 (goto-char placeholder))
12656 (c-add-stmt-syntax 'defun-block-intro nil t
12657 lim paren-state))
12659 ;; CASE 17H: First statement in a block.
12661 ;; If the block is preceded by a case/switch label on the
12662 ;; same line, we anchor at the first preceding label at
12663 ;; boi. The default handling in c-add-stmt-syntax is
12664 ;; really fixes it better, but we do like this to keep the
12665 ;; indentation compatible with version 5.28 and earlier.
12666 ;; C.f. case 16C.
12667 (while (and (/= (setq placeholder (point)) (c-point 'boi))
12668 (eq (c-beginning-of-statement-1 lim) 'label)))
12669 (goto-char placeholder)
12670 (if (looking-at c-label-kwds-regexp)
12671 (c-add-syntax 'statement-block-intro (point))
12672 (goto-char containing-sexp)
12673 ;; c-backward-to-block-anchor not necessary here; those
12674 ;; situations are handled in case 17I above.
12675 (c-add-stmt-syntax 'statement-block-intro nil t
12676 lim paren-state))
12677 (if (eq char-after-ip ?{)
12678 (c-add-syntax 'block-open)))
12682 ;; now we need to look at any modifiers
12683 (goto-char indent-point)
12684 (skip-chars-forward " \t")
12686 ;; are we looking at a comment only line?
12687 (when (and (looking-at c-comment-start-regexp)
12688 (/= (c-forward-token-2 0 nil (c-point 'eol)) 0))
12689 (c-append-syntax 'comment-intro))
12691 ;; we might want to give additional offset to friends (in C++).
12692 (when (and c-opt-friend-key
12693 (looking-at c-opt-friend-key))
12694 (c-append-syntax 'friend))
12696 ;; Set syntactic-relpos.
12697 (let ((p c-syntactic-context))
12698 (while (and p
12699 (if (integerp (c-langelem-pos (car p)))
12700 (progn
12701 (setq syntactic-relpos (c-langelem-pos (car p)))
12702 nil)
12704 (setq p (cdr p))))
12706 ;; Start of or a continuation of a preprocessor directive?
12707 (if (and macro-start
12708 (eq macro-start (c-point 'boi))
12709 (not (and (c-major-mode-is 'pike-mode)
12710 (eq (char-after (1+ macro-start)) ?\"))))
12711 (c-append-syntax 'cpp-macro)
12712 (when (and c-syntactic-indentation-in-macros macro-start)
12713 (if in-macro-expr
12714 (when (or
12715 (< syntactic-relpos macro-start)
12716 (not (or
12717 (assq 'arglist-intro c-syntactic-context)
12718 (assq 'arglist-cont c-syntactic-context)
12719 (assq 'arglist-cont-nonempty c-syntactic-context)
12720 (assq 'arglist-close c-syntactic-context))))
12721 ;; If inside a cpp expression, i.e. anywhere in a
12722 ;; cpp directive except a #define body, we only let
12723 ;; through the syntactic analysis that is internal
12724 ;; in the expression. That means the arglist
12725 ;; elements, if they are anchored inside the cpp
12726 ;; expression.
12727 (setq c-syntactic-context nil)
12728 (c-add-syntax 'cpp-macro-cont macro-start))
12729 (when (and (eq macro-start syntactic-relpos)
12730 (not (assq 'cpp-define-intro c-syntactic-context))
12731 (save-excursion
12732 (goto-char macro-start)
12733 (or (not (c-forward-to-cpp-define-body))
12734 (<= (point) (c-point 'boi indent-point)))))
12735 ;; Inside a #define body and the syntactic analysis is
12736 ;; anchored on the start of the #define. In this case
12737 ;; we add cpp-define-intro to get the extra
12738 ;; indentation of the #define body.
12739 (c-add-syntax 'cpp-define-intro)))))
12741 ;; return the syntax
12742 c-syntactic-context)))
12745 ;; Indentation calculation.
12747 (defun c-evaluate-offset (offset langelem symbol)
12748 ;; offset can be a number, a function, a variable, a list, or one of
12749 ;; the symbols + or -
12751 ;; This function might do hidden buffer changes.
12752 (let ((res
12753 (cond
12754 ((numberp offset) offset)
12755 ((vectorp offset) offset)
12756 ((null offset) nil)
12758 ((eq offset '+) c-basic-offset)
12759 ((eq offset '-) (- c-basic-offset))
12760 ((eq offset '++) (* 2 c-basic-offset))
12761 ((eq offset '--) (* 2 (- c-basic-offset)))
12762 ((eq offset '*) (/ c-basic-offset 2))
12763 ((eq offset '/) (/ (- c-basic-offset) 2))
12765 ((functionp offset)
12766 (c-evaluate-offset
12767 (funcall offset
12768 (cons (c-langelem-sym langelem)
12769 (c-langelem-pos langelem)))
12770 langelem symbol))
12772 ((listp offset)
12773 (cond
12774 ((eq (car offset) 'quote)
12775 (c-benign-error "The offset %S for %s was mistakenly quoted"
12776 offset symbol)
12777 nil)
12779 ((memq (car offset) '(min max))
12780 (let (res val (method (car offset)))
12781 (setq offset (cdr offset))
12782 (while offset
12783 (setq val (c-evaluate-offset (car offset) langelem symbol))
12784 (cond
12785 ((not val))
12786 ((not res)
12787 (setq res val))
12788 ((integerp val)
12789 (if (vectorp res)
12790 (c-benign-error "\
12791 Error evaluating offset %S for %s: \
12792 Cannot combine absolute offset %S with relative %S in `%s' method"
12793 (car offset) symbol res val method)
12794 (setq res (funcall method res val))))
12796 (if (integerp res)
12797 (c-benign-error "\
12798 Error evaluating offset %S for %s: \
12799 Cannot combine relative offset %S with absolute %S in `%s' method"
12800 (car offset) symbol res val method)
12801 (setq res (vector (funcall method (aref res 0)
12802 (aref val 0)))))))
12803 (setq offset (cdr offset)))
12804 res))
12806 ((eq (car offset) 'add)
12807 (let (res val)
12808 (setq offset (cdr offset))
12809 (while offset
12810 (setq val (c-evaluate-offset (car offset) langelem symbol))
12811 (cond
12812 ((not val))
12813 ((not res)
12814 (setq res val))
12815 ((integerp val)
12816 (if (vectorp res)
12817 (setq res (vector (+ (aref res 0) val)))
12818 (setq res (+ res val))))
12820 (if (vectorp res)
12821 (c-benign-error "\
12822 Error evaluating offset %S for %s: \
12823 Cannot combine absolute offsets %S and %S in `add' method"
12824 (car offset) symbol res val)
12825 (setq res val)))) ; Override.
12826 (setq offset (cdr offset)))
12827 res))
12830 (let (res)
12831 (when (eq (car offset) 'first)
12832 (setq offset (cdr offset)))
12833 (while (and (not res) offset)
12834 (setq res (c-evaluate-offset (car offset) langelem symbol)
12835 offset (cdr offset)))
12836 res))))
12838 ((and (symbolp offset) (boundp offset))
12839 (symbol-value offset))
12842 (c-benign-error "Unknown offset format %S for %s" offset symbol)
12843 nil))))
12845 (if (or (null res) (integerp res)
12846 (and (vectorp res) (= (length res) 1) (integerp (aref res 0))))
12848 (c-benign-error "Error evaluating offset %S for %s: Got invalid value %S"
12849 offset symbol res)
12850 nil)))
12852 (defun c-calc-offset (langelem)
12853 ;; Get offset from LANGELEM which is a list beginning with the
12854 ;; syntactic symbol and followed by any analysis data it provides.
12855 ;; That data may be zero or more elements, but if at least one is
12856 ;; given then the first is the anchor position (or nil). The symbol
12857 ;; is matched against `c-offsets-alist' and the offset calculated
12858 ;; from that is returned.
12860 ;; This function might do hidden buffer changes.
12861 (let* ((symbol (c-langelem-sym langelem))
12862 (match (assq symbol c-offsets-alist))
12863 (offset (cdr-safe match)))
12864 (if match
12865 (setq offset (c-evaluate-offset offset langelem symbol))
12866 (if c-strict-syntax-p
12867 (c-benign-error "No offset found for syntactic symbol %s" symbol))
12868 (setq offset 0))
12869 (if (vectorp offset)
12870 offset
12871 (or (and (numberp offset) offset)
12872 (and (symbolp offset) (symbol-value offset))
12876 (defun c-get-offset (langelem)
12877 ;; This is a compatibility wrapper for `c-calc-offset' in case
12878 ;; someone is calling it directly. It takes an old style syntactic
12879 ;; element on the form (SYMBOL . ANCHOR-POS) and converts it to the
12880 ;; new list form.
12882 ;; This function might do hidden buffer changes.
12883 (if (c-langelem-pos langelem)
12884 (c-calc-offset (list (c-langelem-sym langelem)
12885 (c-langelem-pos langelem)))
12886 (c-calc-offset langelem)))
12888 (defun c-get-syntactic-indentation (langelems)
12889 ;; Calculate the syntactic indentation from a syntactic description
12890 ;; as returned by `c-guess-syntax'.
12892 ;; Note that topmost-intro always has an anchor position at bol, for
12893 ;; historical reasons. It's often used together with other symbols
12894 ;; that have more sane positions. Since we always use the first
12895 ;; found anchor position, we rely on that these other symbols always
12896 ;; precede topmost-intro in the LANGELEMS list.
12898 ;; This function might do hidden buffer changes.
12899 (let ((indent 0) anchor)
12901 (while langelems
12902 (let* ((c-syntactic-element (car langelems))
12903 (res (c-calc-offset c-syntactic-element)))
12905 (if (vectorp res)
12906 ;; Got an absolute column that overrides any indentation
12907 ;; we've collected so far, but not the relative
12908 ;; indentation we might get for the nested structures
12909 ;; further down the langelems list.
12910 (setq indent (elt res 0)
12911 anchor (point-min)) ; A position at column 0.
12913 ;; Got a relative change of the current calculated
12914 ;; indentation.
12915 (setq indent (+ indent res))
12917 ;; Use the anchor position from the first syntactic
12918 ;; element with one.
12919 (unless anchor
12920 (setq anchor (c-langelem-pos (car langelems)))))
12922 (setq langelems (cdr langelems))))
12924 (if anchor
12925 (+ indent (save-excursion
12926 (goto-char anchor)
12927 (current-column)))
12928 indent)))
12931 (cc-provide 'cc-engine)
12933 ;; Local Variables:
12934 ;; indent-tabs-mode: t
12935 ;; tab-width: 8
12936 ;; End:
12937 ;;; cc-engine.el ends here