Fix a comment whitespace typo.
[emacs.git] / lisp / progmodes / cc-engine.el
blob59dc96af03092c56649b00be2b8556211baa54af
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 (&optional lim)
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 If LIM is provided, it is a limit position at which point is left
329 if the end of the macro doesn't occur earlier.
331 Note that this function might do hidden buffer changes. See the
332 comment at the start of cc-engine.el for more info."
333 (save-restriction
334 (if lim (narrow-to-region (point-min) lim))
335 (if (and (cdr c-macro-cache)
336 (<= (point) (cdr c-macro-cache))
337 (>= (point) (car c-macro-cache)))
338 (goto-char (cdr c-macro-cache))
339 (unless (and (car c-macro-cache)
340 (<= (point) c-macro-cache-start-pos)
341 (>= (point) (car c-macro-cache)))
342 (setq c-macro-cache nil
343 c-macro-cache-start-pos nil
344 c-macro-cache-syntactic nil
345 c-macro-cache-no-comment nil))
346 (while (progn
347 (end-of-line)
348 (when (and (eq (char-before) ?\\)
349 (not (eobp)))
350 (forward-char)
351 t)))
352 (when (and (car c-macro-cache)
353 (bolp)
354 (not (eq (char-before (1- (point))) ?\\)))
355 (setcdr c-macro-cache (point))
356 (setq c-macro-cache-syntactic nil)))))
358 (defun c-syntactic-end-of-macro ()
359 ;; Go to the end of a CPP directive, or a "safe" pos just before.
361 ;; This is normally the end of the next non-escaped line. A "safe"
362 ;; position is one not within a string or comment. (The EOL on a line
363 ;; comment is NOT "safe").
365 ;; This function must only be called from the beginning of a CPP construct.
367 ;; Note that this function might do hidden buffer changes. See the comment
368 ;; at the start of cc-engine.el for more info.
369 (let* ((here (point))
370 (there (progn (c-end-of-macro) (point)))
372 (if c-macro-cache-syntactic
373 (goto-char c-macro-cache-syntactic)
374 (setq s (parse-partial-sexp here there))
375 (while (and (or (nth 3 s) ; in a string
376 (and (nth 4 s) ; in a comment (maybe at end of line comment)
377 (not (eq (nth 7 s) 'syntax-table)))) ; Not a pseudo comment
378 (> there here)) ; No infinite loops, please.
379 (setq there (1- (nth 8 s)))
380 (setq s (parse-partial-sexp here there)))
381 (setq c-macro-cache-syntactic (point)))
382 (point)))
384 (defun c-no-comment-end-of-macro ()
385 ;; Go to the end of a CPP directive, or a pos just before which isn't in a
386 ;; comment. For this purpose, open strings are ignored.
388 ;; This function must only be called from the beginning of a CPP construct.
390 ;; Note that this function might do hidden buffer changes. See the comment
391 ;; at the start of cc-engine.el for more info.
392 (let* ((here (point))
393 (there (progn (c-end-of-macro) (point)))
395 (if c-macro-cache-no-comment
396 (goto-char c-macro-cache-no-comment)
397 (setq s (parse-partial-sexp here there))
398 (while (and (nth 3 s) ; in a string
399 (> there here)) ; No infinite loops, please.
400 (setq here (1+ (nth 8 s)))
401 (setq s (parse-partial-sexp here there)))
402 (when (and (nth 4 s)
403 (not (eq (nth 7 s) 'syntax-table))) ; no pseudo comments.
404 (goto-char (1- (nth 8 s))))
405 (setq c-macro-cache-no-comment (point)))
406 (point)))
408 (defun c-forward-over-cpp-define-id ()
409 ;; Assuming point is at the "#" that introduces a preprocessor
410 ;; directive, it's moved forward to the end of the identifier which is
411 ;; "#define"d (or whatever c-opt-cpp-macro-define specifies). Non-nil
412 ;; is returned in this case, in all other cases nil is returned and
413 ;; point isn't moved.
415 ;; This function might do hidden buffer changes.
416 (when (and c-opt-cpp-macro-define-id
417 (looking-at c-opt-cpp-macro-define-id))
418 (goto-char (match-end 0))))
420 (defun c-forward-to-cpp-define-body ()
421 ;; Assuming point is at the "#" that introduces a preprocessor
422 ;; directive, it's moved forward to the start of the definition body
423 ;; if it's a "#define" (or whatever c-opt-cpp-macro-define
424 ;; specifies). Non-nil is returned in this case, in all other cases
425 ;; nil is returned and point isn't moved.
427 ;; This function might do hidden buffer changes.
428 (when (and c-opt-cpp-macro-define-start
429 (looking-at c-opt-cpp-macro-define-start)
430 (not (= (match-end 0) (c-point 'eol))))
431 (goto-char (match-end 0))))
434 ;;; Basic utility functions.
436 (defun c-delq-from-dotted-list (elt dlist)
437 ;; If ELT is a member of the (possibly dotted) list DLIST, remove all
438 ;; occurrences of it (except for any in the last cdr of DLIST).
440 ;; Call this as (setq DLIST (c-delq-from-dotted-list ELT DLIST)), as
441 ;; sometimes the original structure is changed, sometimes it's not.
443 ;; This function is needed in Emacs < 24.5, and possibly XEmacs, because
444 ;; `delq' throws an error in these versions when given a dotted list.
445 (let ((tail dlist) prev)
446 (while (consp tail)
447 (if (eq (car tail) elt)
448 (if prev
449 (setcdr prev (cdr tail))
450 (setq dlist (cdr dlist)))
451 (setq prev tail))
452 (setq tail (cdr tail)))
453 dlist))
455 (defun c-syntactic-content (from to paren-level)
456 ;; Return the given region as a string where all syntactic
457 ;; whitespace is removed or, where necessary, replaced with a single
458 ;; space. If PAREN-LEVEL is given then all parens in the region are
459 ;; collapsed to "()", "[]" etc.
461 ;; This function might do hidden buffer changes.
463 (save-excursion
464 (save-restriction
465 (narrow-to-region from to)
466 (goto-char from)
467 (let* ((parts (list nil)) (tail parts) pos in-paren)
469 (while (re-search-forward c-syntactic-ws-start to t)
470 (goto-char (setq pos (match-beginning 0)))
471 (c-forward-syntactic-ws)
472 (if (= (point) pos)
473 (forward-char)
475 (when paren-level
476 (save-excursion
477 (setq in-paren (= (car (parse-partial-sexp from pos 1)) 1)
478 pos (point))))
480 (if (and (> pos from)
481 (< (point) to)
482 (looking-at "\\w\\|\\s_")
483 (save-excursion
484 (goto-char (1- pos))
485 (looking-at "\\w\\|\\s_")))
486 (progn
487 (setcdr tail (list (buffer-substring-no-properties from pos)
488 " "))
489 (setq tail (cddr tail)))
490 (setcdr tail (list (buffer-substring-no-properties from pos)))
491 (setq tail (cdr tail)))
493 (when in-paren
494 (when (= (car (parse-partial-sexp pos to -1)) -1)
495 (setcdr tail (list (buffer-substring-no-properties
496 (1- (point)) (point))))
497 (setq tail (cdr tail))))
499 (setq from (point))))
501 (setcdr tail (list (buffer-substring-no-properties from to)))
502 (apply 'concat (cdr parts))))))
504 (defun c-shift-line-indentation (shift-amt)
505 ;; Shift the indentation of the current line with the specified
506 ;; amount (positive inwards). The buffer is modified only if
507 ;; SHIFT-AMT isn't equal to zero.
508 (let ((pos (- (point-max) (point)))
509 (c-macro-start c-macro-start)
510 tmp-char-inserted)
511 (if (zerop shift-amt)
513 ;; If we're on an empty line inside a macro, we take the point
514 ;; to be at the current indentation and shift it to the
515 ;; appropriate column. This way we don't treat the extra
516 ;; whitespace out to the line continuation as indentation.
517 (when (and (c-query-and-set-macro-start)
518 (looking-at "[ \t]*\\\\$")
519 (save-excursion
520 (skip-chars-backward " \t")
521 (bolp)))
522 (insert ?x)
523 (backward-char)
524 (setq tmp-char-inserted t))
525 (unwind-protect
526 (let ((col (current-indentation)))
527 (delete-region (c-point 'bol) (c-point 'boi))
528 (beginning-of-line)
529 (indent-to (+ col shift-amt)))
530 (when tmp-char-inserted
531 (delete-char 1))))
532 ;; If initial point was within line's indentation and we're not on
533 ;; a line with a line continuation in a macro, position after the
534 ;; indentation. Else stay at same point in text.
535 (if (and (< (point) (c-point 'boi))
536 (not tmp-char-inserted))
537 (back-to-indentation)
538 (if (> (- (point-max) pos) (point))
539 (goto-char (- (point-max) pos))))))
541 (defsubst c-keyword-sym (keyword)
542 ;; Return non-nil if the string KEYWORD is a known keyword. More
543 ;; precisely, the value is the symbol for the keyword in
544 ;; `c-keywords-obarray'.
545 (intern-soft keyword c-keywords-obarray))
547 (defsubst c-keyword-member (keyword-sym lang-constant)
548 ;; Return non-nil if the symbol KEYWORD-SYM, as returned by
549 ;; `c-keyword-sym', is a member of LANG-CONSTANT, which is the name
550 ;; of a language constant that ends with "-kwds". If KEYWORD-SYM is
551 ;; nil then the result is nil.
552 (get keyword-sym lang-constant))
554 ;; String syntax chars, suitable for skip-syntax-(forward|backward).
555 (defconst c-string-syntax (if (memq 'gen-string-delim c-emacs-features)
556 "\"|"
557 "\""))
559 ;; Regexp matching string limit syntax.
560 (defconst c-string-limit-regexp (if (memq 'gen-string-delim c-emacs-features)
561 "\\s\"\\|\\s|"
562 "\\s\""))
564 ;; Regexp matching WS followed by string limit syntax.
565 (defconst c-ws*-string-limit-regexp
566 (concat "[ \t]*\\(" c-string-limit-regexp "\\)"))
568 ;; Holds formatted error strings for the few cases where parse errors
569 ;; are reported.
570 (defvar c-parsing-error nil)
571 (make-variable-buffer-local 'c-parsing-error)
573 (defun c-echo-parsing-error (&optional quiet)
574 (when (and c-report-syntactic-errors c-parsing-error (not quiet))
575 (c-benign-error "%s" c-parsing-error))
576 c-parsing-error)
578 ;; Faces given to comments and string literals. This is used in some
579 ;; situations to speed up recognition; it isn't mandatory that font
580 ;; locking is in use. This variable is extended with the face in
581 ;; `c-doc-face-name' when fontification is activated in cc-fonts.el.
582 (defvar c-literal-faces
583 (append '(font-lock-comment-face font-lock-string-face)
584 (when (facep 'font-lock-comment-delimiter-face)
585 ;; New in Emacs 22.
586 '(font-lock-comment-delimiter-face))))
588 (defsubst c-put-c-type-property (pos value)
589 ;; Put a c-type property with the given value at POS.
590 (c-put-char-property pos 'c-type value))
592 (defun c-clear-c-type-property (from to value)
593 ;; Remove all occurrences of the c-type property that has the given
594 ;; value in the region between FROM and TO. VALUE is assumed to not
595 ;; be nil.
597 ;; Note: This assumes that c-type is put on single chars only; it's
598 ;; very inefficient if matching properties cover large regions.
599 (save-excursion
600 (goto-char from)
601 (while (progn
602 (when (eq (get-text-property (point) 'c-type) value)
603 (c-clear-char-property (point) 'c-type))
604 (goto-char (c-next-single-property-change (point) 'c-type nil to))
605 (< (point) to)))))
608 ;; Some debug tools to visualize various special positions. This
609 ;; debug code isn't as portable as the rest of CC Mode.
611 (cc-bytecomp-defun overlays-in)
612 (cc-bytecomp-defun overlay-get)
613 (cc-bytecomp-defun overlay-start)
614 (cc-bytecomp-defun overlay-end)
615 (cc-bytecomp-defun delete-overlay)
616 (cc-bytecomp-defun overlay-put)
617 (cc-bytecomp-defun make-overlay)
619 (defun c-debug-add-face (beg end face)
620 (c-save-buffer-state ((overlays (overlays-in beg end)) overlay)
621 (while overlays
622 (setq overlay (car overlays)
623 overlays (cdr overlays))
624 (when (eq (overlay-get overlay 'face) face)
625 (setq beg (min beg (overlay-start overlay))
626 end (max end (overlay-end overlay)))
627 (delete-overlay overlay)))
628 (overlay-put (make-overlay beg end) 'face face)))
630 (defun c-debug-remove-face (beg end face)
631 (c-save-buffer-state ((overlays (overlays-in beg end)) overlay
632 (ol-beg beg) (ol-end end))
633 (while overlays
634 (setq overlay (car overlays)
635 overlays (cdr overlays))
636 (when (eq (overlay-get overlay 'face) face)
637 (setq ol-beg (min ol-beg (overlay-start overlay))
638 ol-end (max ol-end (overlay-end overlay)))
639 (delete-overlay overlay)))
640 (when (< ol-beg beg)
641 (overlay-put (make-overlay ol-beg beg) 'face face))
642 (when (> ol-end end)
643 (overlay-put (make-overlay end ol-end) 'face face))))
646 ;; `c-beginning-of-statement-1' and accompanying stuff.
648 ;; KLUDGE ALERT: c-maybe-labelp is used to pass information between
649 ;; c-crosses-statement-barrier-p and c-beginning-of-statement-1. A
650 ;; better way should be implemented, but this will at least shut up
651 ;; the byte compiler.
652 (defvar c-maybe-labelp)
654 ;; New awk-compatible version of c-beginning-of-statement-1, ACM 2002/6/22
656 ;; Macros used internally in c-beginning-of-statement-1 for the
657 ;; automaton actions.
658 (defmacro c-bos-push-state ()
659 '(setq stack (cons (cons state saved-pos)
660 stack)))
661 (defmacro c-bos-pop-state (&optional do-if-done)
662 `(if (setq state (car (car stack))
663 saved-pos (cdr (car stack))
664 stack (cdr stack))
666 ,do-if-done
667 (throw 'loop nil)))
668 (defmacro c-bos-pop-state-and-retry ()
669 '(throw 'loop (setq state (car (car stack))
670 saved-pos (cdr (car stack))
671 ;; Throw nil if stack is empty, else throw non-nil.
672 stack (cdr stack))))
673 (defmacro c-bos-save-pos ()
674 '(setq saved-pos (vector pos tok ptok pptok)))
675 (defmacro c-bos-restore-pos ()
676 '(unless (eq (elt saved-pos 0) start)
677 (setq pos (elt saved-pos 0)
678 tok (elt saved-pos 1)
679 ptok (elt saved-pos 2)
680 pptok (elt saved-pos 3))
681 (goto-char pos)
682 (setq sym nil)))
683 (defmacro c-bos-save-error-info (missing got)
684 `(setq saved-pos (vector pos ,missing ,got)))
685 (defmacro c-bos-report-error ()
686 '(unless noerror
687 (setq c-parsing-error
688 (format-message
689 "No matching `%s' found for `%s' on line %d"
690 (elt saved-pos 1)
691 (elt saved-pos 2)
692 (1+ (count-lines (point-min)
693 (c-point 'bol (elt saved-pos 0))))))))
695 (defun c-beginning-of-statement-1 (&optional lim ignore-labels
696 noerror comma-delim)
697 "Move to the start of the current statement or declaration, or to
698 the previous one if already at the beginning of one. Only
699 statements/declarations on the same level are considered, i.e. don't
700 move into or out of sexps (not even normal expression parentheses).
702 If point is already at the earliest statement within braces or parens,
703 this function doesn't move back into any whitespace preceding it; it
704 returns `same' in this case.
706 Stop at statement continuation tokens like \"else\", \"catch\",
707 \"finally\" and the \"while\" in \"do ... while\" if the start point
708 is within the continuation. If starting at such a token, move to the
709 corresponding statement start. If at the beginning of a statement,
710 move to the closest containing statement if there is any. This might
711 also stop at a continuation clause.
713 Labels are treated as part of the following statements if
714 IGNORE-LABELS is non-nil. (FIXME: Doesn't work if we stop at a known
715 statement start keyword.) Otherwise, each label is treated as a
716 separate statement.
718 Macros are ignored \(i.e. skipped over) unless point is within one, in
719 which case the content of the macro is treated as normal code. Aside
720 from any normal statement starts found in it, stop at the first token
721 of the content in the macro, i.e. the expression of an \"#if\" or the
722 start of the definition in a \"#define\". Also stop at start of
723 macros before leaving them.
725 Return:
726 `label' if stopped at a label or \"case...:\" or \"default:\";
727 `same' if stopped at the beginning of the current statement;
728 `up' if stepped to a containing statement;
729 `previous' if stepped to a preceding statement;
730 `beginning' if stepped from a statement continuation clause to
731 its start clause; or
732 `macro' if stepped to a macro start.
733 Note that `same' and not `label' is returned if stopped at the same
734 label without crossing the colon character.
736 LIM may be given to limit the search. If the search hits the limit,
737 point will be left at the closest following token, or at the start
738 position if that is less (`same' is returned in this case).
740 NOERROR turns off error logging to `c-parsing-error'.
742 Normally only `;' and virtual semicolons are considered to delimit
743 statements, but if COMMA-DELIM is non-nil then `,' is treated
744 as a delimiter too.
746 Note that this function might do hidden buffer changes. See the
747 comment at the start of cc-engine.el for more info."
749 ;; The bulk of this function is a pushdown automaton that looks at statement
750 ;; boundaries and the tokens (such as "while") in c-opt-block-stmt-key. Its
751 ;; purpose is to keep track of nested statements, ensuring that such
752 ;; statements are skipped over in their entirety (somewhat akin to what C-M-p
753 ;; does with nested braces/brackets/parentheses).
755 ;; Note: The position of a boundary is the following token.
757 ;; Beginning with the current token (the one following point), move back one
758 ;; sexp at a time (where a sexp is, more or less, either a token or the
759 ;; entire contents of a brace/bracket/paren pair). Each time a statement
760 ;; boundary is crossed or a "while"-like token is found, update the state of
761 ;; the PDA. Stop at the beginning of a statement when the stack (holding
762 ;; nested statement info) is empty and the position has been moved.
764 ;; The following variables constitute the PDA:
766 ;; sym: This is either the "while"-like token (e.g. 'for) we've just
767 ;; scanned back over, 'boundary if we've just gone back over a
768 ;; statement boundary, or nil otherwise.
769 ;; state: takes one of the values (nil else else-boundary while
770 ;; while-boundary catch catch-boundary).
771 ;; nil means "no "while"-like token yet scanned".
772 ;; 'else, for example, means "just gone back over an else".
773 ;; 'else-boundary means "just gone back over a statement boundary
774 ;; immediately after having gone back over an else".
775 ;; saved-pos: A vector of either saved positions (tok ptok pptok, etc.) or
776 ;; of error reporting information.
777 ;; stack: The stack onto which the PDA pushes its state. Each entry
778 ;; consists of a saved value of state and saved-pos. An entry is
779 ;; pushed when we move back over a "continuation" token (e.g. else)
780 ;; and popped when we encounter the corresponding opening token
781 ;; (e.g. if).
784 ;; The following diagram briefly outlines the PDA.
786 ;; Common state:
787 ;; "else": Push state, goto state `else'.
788 ;; "while": Push state, goto state `while'.
789 ;; "catch" or "finally": Push state, goto state `catch'.
790 ;; boundary: Pop state.
791 ;; other: Do nothing special.
793 ;; State `else':
794 ;; boundary: Goto state `else-boundary'.
795 ;; other: Error, pop state, retry token.
797 ;; State `else-boundary':
798 ;; "if": Pop state.
799 ;; boundary: Error, pop state.
800 ;; other: See common state.
802 ;; State `while':
803 ;; boundary: Save position, goto state `while-boundary'.
804 ;; other: Pop state, retry token.
806 ;; State `while-boundary':
807 ;; "do": Pop state.
808 ;; boundary: Restore position if it's not at start, pop state. [*see below]
809 ;; other: See common state.
811 ;; State `catch':
812 ;; boundary: Goto state `catch-boundary'.
813 ;; other: Error, pop state, retry token.
815 ;; State `catch-boundary':
816 ;; "try": Pop state.
817 ;; "catch": Goto state `catch'.
818 ;; boundary: Error, pop state.
819 ;; other: See common state.
821 ;; [*] In the `while-boundary' state, we had pushed a 'while state, and were
822 ;; searching for a "do" which would have opened a do-while. If we didn't
823 ;; find it, we discard the analysis done since the "while", go back to this
824 ;; token in the buffer and restart the scanning there, this time WITHOUT
825 ;; pushing the 'while state onto the stack.
827 ;; In addition to the above there is some special handling of labels
828 ;; and macros.
830 (let ((case-fold-search nil)
831 (start (point))
832 macro-start
833 (delims (if comma-delim '(?\; ?,) '(?\;)))
834 (c-stmt-delim-chars (if comma-delim
835 c-stmt-delim-chars-with-comma
836 c-stmt-delim-chars))
837 c-maybe-labelp after-case:-pos saved
838 ;; Current position.
840 ;; Position of last stmt boundary character (e.g. ;).
841 boundary-pos
842 ;; The position of the last sexp or bound that follows the
843 ;; first found colon, i.e. the start of the nonlabel part of
844 ;; the statement. It's `start' if a colon is found just after
845 ;; the start.
846 after-labels-pos
847 ;; Like `after-labels-pos', but the first such position inside
848 ;; a label, i.e. the start of the last label before the start
849 ;; of the nonlabel part of the statement.
850 last-label-pos
851 ;; The last position where a label is possible provided the
852 ;; statement started there. It's nil as long as no invalid
853 ;; label content has been found (according to
854 ;; `c-nonlabel-token-key'). It's `start' if no valid label
855 ;; content was found in the label. Note that we might still
856 ;; regard it a label if it starts with `c-label-kwds'.
857 label-good-pos
858 ;; Putative positions of the components of a bitfield declaration,
859 ;; e.g. "int foo : NUM_FOO_BITS ;"
860 bitfield-type-pos bitfield-id-pos bitfield-size-pos
861 ;; Symbol just scanned back over (e.g. 'while or 'boundary).
862 ;; See above.
864 ;; Current state in the automaton. See above.
865 state
866 ;; Current saved positions. See above.
867 saved-pos
868 ;; Stack of conses (state . saved-pos).
869 stack
870 ;; Regexp which matches "for", "if", etc.
871 (cond-key (or c-opt-block-stmt-key
872 "\\<\\>")) ; Matches nothing.
873 ;; Return value.
874 (ret 'same)
875 ;; Positions of the last three sexps or bounds we've stopped at.
876 tok ptok pptok)
878 (save-restriction
879 (if lim (narrow-to-region lim (point-max)))
881 (if (save-excursion
882 (and (c-beginning-of-macro)
883 (/= (point) start)))
884 (setq macro-start (point)))
886 ;; Try to skip back over unary operator characters, to register
887 ;; that we've moved.
888 (while (progn
889 (setq pos (point))
890 (c-backward-syntactic-ws)
891 ;; Protect post-++/-- operators just before a virtual semicolon.
892 (and (not (c-at-vsemi-p))
893 (/= (skip-chars-backward "-+!*&~@`#") 0))))
895 ;; Skip back over any semicolon here. If it was a bare semicolon, we're
896 ;; done. Later on we ignore the boundaries for statements that don't
897 ;; contain any sexp. The only thing that is affected is that the error
898 ;; checking is a little less strict, and we really don't bother.
899 (if (and (memq (char-before) delims)
900 (progn (forward-char -1)
901 (setq saved (point))
902 (c-backward-syntactic-ws)
903 (or (memq (char-before) delims)
904 (memq (char-before) '(?: nil))
905 (eq (char-syntax (char-before)) ?\()
906 (c-at-vsemi-p))))
907 (setq ret 'previous
908 pos saved)
910 ;; Begin at start and not pos to detect macros if we stand
911 ;; directly after the #.
912 (goto-char start)
913 (if (looking-at "\\<\\|\\W")
914 ;; Record this as the first token if not starting inside it.
915 (setq tok start))
917 ;; The following while loop goes back one sexp (balanced parens,
918 ;; etc. with contents, or symbol or suchlike) each iteration. This
919 ;; movement is accomplished with a call to c-backward-sexp approx 170
920 ;; lines below.
922 ;; The loop is exited only by throwing nil to the (catch 'loop ...):
923 ;; 1. On reaching the start of a macro;
924 ;; 2. On having passed a stmt boundary with the PDA stack empty;
925 ;; 3. On reaching the start of an Objective C method def;
926 ;; 4. From macro `c-bos-pop-state'; when the stack is empty;
927 ;; 5. From macro `c-bos-pop-state-and-retry' when the stack is empty.
928 (while
929 (catch 'loop ;; Throw nil to break, non-nil to continue.
930 (cond
931 ;; Are we in a macro, just after the opening #?
932 ((save-excursion
933 (and macro-start ; Always NIL for AWK.
934 (progn (skip-chars-backward " \t")
935 (eq (char-before) ?#))
936 (progn (setq saved (1- (point)))
937 (beginning-of-line)
938 (not (eq (char-before (1- (point))) ?\\)))
939 (looking-at c-opt-cpp-start)
940 (progn (skip-chars-forward " \t")
941 (eq (point) saved))))
942 (goto-char saved)
943 (if (and (c-forward-to-cpp-define-body)
944 (progn (c-forward-syntactic-ws start)
945 (< (point) start)))
946 ;; Stop at the first token in the content of the macro.
947 (setq pos (point)
948 ignore-labels t) ; Avoid the label check on exit.
949 (setq pos saved
950 ret 'macro
951 ignore-labels t))
952 (throw 'loop nil)) ; 1. Start of macro.
954 ;; Do a round through the automaton if we've just passed a
955 ;; statement boundary or passed a "while"-like token.
956 ((or sym
957 (and (looking-at cond-key)
958 (setq sym (intern (match-string 1)))))
960 (when (and (< pos start) (null stack))
961 (throw 'loop nil)) ; 2. Statement boundary.
963 ;; The PDA state handling.
965 ;; Refer to the description of the PDA in the opening
966 ;; comments. In the following OR form, the first leaf
967 ;; attempts to handles one of the specific actions detailed
968 ;; (e.g., finding token "if" whilst in state `else-boundary').
969 ;; We drop through to the second leaf (which handles common
970 ;; state) if no specific handler is found in the first cond.
971 ;; If a parsing error is detected (e.g. an "else" with no
972 ;; preceding "if"), we throw to the enclosing catch.
974 ;; Note that the (eq state 'else) means
975 ;; "we've just passed an else", NOT "we're looking for an
976 ;; else".
977 (or (cond
978 ((eq state 'else)
979 (if (eq sym 'boundary)
980 (setq state 'else-boundary)
981 (c-bos-report-error)
982 (c-bos-pop-state-and-retry)))
984 ((eq state 'else-boundary)
985 (cond ((eq sym 'if)
986 (c-bos-pop-state (setq ret 'beginning)))
987 ((eq sym 'boundary)
988 (c-bos-report-error)
989 (c-bos-pop-state))))
991 ((eq state 'while)
992 (if (and (eq sym 'boundary)
993 ;; Since this can cause backtracking we do a
994 ;; little more careful analysis to avoid it:
995 ;; If there's a label in front of the while
996 ;; it can't be part of a do-while.
997 (not after-labels-pos))
998 (progn (c-bos-save-pos)
999 (setq state 'while-boundary))
1000 (c-bos-pop-state-and-retry))) ; Can't be a do-while
1002 ((eq state 'while-boundary)
1003 (cond ((eq sym 'do)
1004 (c-bos-pop-state (setq ret 'beginning)))
1005 ((eq sym 'boundary) ; isn't a do-while
1006 (c-bos-restore-pos) ; the position of the while
1007 (c-bos-pop-state)))) ; no longer searching for do.
1009 ((eq state 'catch)
1010 (if (eq sym 'boundary)
1011 (setq state 'catch-boundary)
1012 (c-bos-report-error)
1013 (c-bos-pop-state-and-retry)))
1015 ((eq state 'catch-boundary)
1016 (cond
1017 ((eq sym 'try)
1018 (c-bos-pop-state (setq ret 'beginning)))
1019 ((eq sym 'catch)
1020 (setq state 'catch))
1021 ((eq sym 'boundary)
1022 (c-bos-report-error)
1023 (c-bos-pop-state)))))
1025 ;; This is state common. We get here when the previous
1026 ;; cond statement found no particular state handler.
1027 (cond ((eq sym 'boundary)
1028 ;; If we have a boundary at the start
1029 ;; position we push a frame to go to the
1030 ;; previous statement.
1031 (if (>= pos start)
1032 (c-bos-push-state)
1033 (c-bos-pop-state)))
1034 ((eq sym 'else)
1035 (c-bos-push-state)
1036 (c-bos-save-error-info 'if 'else)
1037 (setq state 'else))
1038 ((eq sym 'while)
1039 ;; Is this a real while, or a do-while?
1040 ;; The next `when' triggers unless we are SURE that
1041 ;; the `while' is not the tail end of a `do-while'.
1042 (when (or (not pptok)
1043 (memq (char-after pptok) delims)
1044 ;; The following kludge is to prevent
1045 ;; infinite recursion when called from
1046 ;; c-awk-after-if-for-while-condition-p,
1047 ;; or the like.
1048 (and (eq (point) start)
1049 (c-vsemi-status-unknown-p))
1050 (c-at-vsemi-p pptok))
1051 ;; Since this can cause backtracking we do a
1052 ;; little more careful analysis to avoid it: If
1053 ;; the while isn't followed by a (possibly
1054 ;; virtual) semicolon it can't be a do-while.
1055 (c-bos-push-state)
1056 (setq state 'while)))
1057 ((memq sym '(catch finally))
1058 (c-bos-push-state)
1059 (c-bos-save-error-info 'try sym)
1060 (setq state 'catch))))
1062 (when c-maybe-labelp
1063 ;; We're either past a statement boundary or at the
1064 ;; start of a statement, so throw away any label data
1065 ;; for the previous one.
1066 (setq after-labels-pos nil
1067 last-label-pos nil
1068 c-maybe-labelp nil))))
1070 ;; Step to the previous sexp, but not if we crossed a
1071 ;; boundary, since that doesn't consume an sexp.
1072 (if (eq sym 'boundary)
1073 (setq ret 'previous)
1075 ;; HERE IS THE SINGLE PLACE INSIDE THE PDA LOOP WHERE WE MOVE
1076 ;; BACKWARDS THROUGH THE SOURCE.
1078 (c-backward-syntactic-ws)
1079 (let ((before-sws-pos (point))
1080 ;; The end position of the area to search for statement
1081 ;; barriers in this round.
1082 (maybe-after-boundary-pos pos))
1084 ;; Go back over exactly one logical sexp, taking proper
1085 ;; account of macros and escaped EOLs.
1086 (while
1087 (progn
1088 (unless (c-safe (c-backward-sexp) t)
1089 ;; Give up if we hit an unbalanced block. Since the
1090 ;; stack won't be empty the code below will report a
1091 ;; suitable error.
1092 (throw 'loop nil))
1093 (cond
1094 ;; Have we moved into a macro?
1095 ((and (not macro-start)
1096 (c-beginning-of-macro))
1097 ;; Have we crossed a statement boundary? If not,
1098 ;; keep going back until we find one or a "real" sexp.
1099 (and
1100 (save-excursion
1101 (c-end-of-macro)
1102 (not (c-crosses-statement-barrier-p
1103 (point) maybe-after-boundary-pos)))
1104 (setq maybe-after-boundary-pos (point))))
1105 ;; Have we just gone back over an escaped NL? This
1106 ;; doesn't count as a sexp.
1107 ((looking-at "\\\\$")))))
1109 ;; Have we crossed a statement boundary?
1110 (setq boundary-pos
1111 (cond
1112 ;; Are we at a macro beginning?
1113 ((and (not macro-start)
1114 c-opt-cpp-prefix
1115 (looking-at c-opt-cpp-prefix))
1116 (save-excursion
1117 (c-end-of-macro)
1118 (c-crosses-statement-barrier-p
1119 (point) maybe-after-boundary-pos)))
1120 ;; Just gone back over a brace block?
1121 ((and
1122 (eq (char-after) ?{)
1123 (not (c-looking-at-inexpr-block lim nil t))
1124 (save-excursion
1125 (c-backward-token-2 1 t nil)
1126 (not (looking-at "=\\([^=]\\|$\\)"))))
1127 (save-excursion
1128 (c-forward-sexp) (point)))
1129 ;; Just gone back over some paren block?
1130 ((looking-at "\\s(")
1131 (save-excursion
1132 (goto-char (1+ (c-down-list-backward
1133 before-sws-pos)))
1134 (c-crosses-statement-barrier-p
1135 (point) maybe-after-boundary-pos)))
1136 ;; Just gone back over an ordinary symbol of some sort?
1137 (t (c-crosses-statement-barrier-p
1138 (point) maybe-after-boundary-pos))))
1140 (when boundary-pos
1141 (setq pptok ptok
1142 ptok tok
1143 tok boundary-pos
1144 sym 'boundary)
1145 ;; Like a C "continue". Analyze the next sexp.
1146 (throw 'loop t))))
1148 ;; ObjC method def?
1149 (when (and c-opt-method-key
1150 (setq saved (c-in-method-def-p)))
1151 (setq pos saved
1152 ignore-labels t) ; Avoid the label check on exit.
1153 (throw 'loop nil)) ; 3. ObjC method def.
1155 ;; Might we have a bitfield declaration, "<type> <id> : <size>"?
1156 (if c-has-bitfields
1157 (cond
1158 ;; The : <size> and <id> fields?
1159 ((and (numberp c-maybe-labelp)
1160 (not bitfield-size-pos)
1161 (save-excursion
1162 (goto-char (or tok start))
1163 (not (looking-at c-keywords-regexp)))
1164 (not (looking-at c-keywords-regexp))
1165 (not (c-punctuation-in (point) c-maybe-labelp)))
1166 (setq bitfield-size-pos (or tok start)
1167 bitfield-id-pos (point)))
1168 ;; The <type> field?
1169 ((and bitfield-id-pos
1170 (not bitfield-type-pos))
1171 (if (and (looking-at c-symbol-key) ; Can only be an integer type. :-)
1172 (not (looking-at c-not-primitive-type-keywords-regexp))
1173 (not (c-punctuation-in (point) tok)))
1174 (setq bitfield-type-pos (point))
1175 (setq bitfield-size-pos nil
1176 bitfield-id-pos nil)))))
1178 ;; Handle labels.
1179 (unless (eq ignore-labels t)
1180 (when (numberp c-maybe-labelp)
1181 ;; `c-crosses-statement-barrier-p' has found a colon, so we
1182 ;; might be in a label now. Have we got a real label
1183 ;; (including a case label) or something like C++'s "public:"?
1184 ;; A case label might use an expression rather than a token.
1185 (setq after-case:-pos (or tok start))
1186 (if (or (looking-at c-nonlabel-token-key) ; e.g. "while" or "'a'"
1187 ;; Catch C++'s inheritance construct "class foo : bar".
1188 (save-excursion
1189 (and
1190 (c-safe (c-backward-sexp) t)
1191 (looking-at c-nonlabel-token-2-key))))
1192 (setq c-maybe-labelp nil)
1193 (if after-labels-pos ; Have we already encountered a label?
1194 (if (not last-label-pos)
1195 (setq last-label-pos (or tok start)))
1196 (setq after-labels-pos (or tok start)))
1197 (setq c-maybe-labelp t
1198 label-good-pos nil))) ; bogus "label"
1200 (when (and (not label-good-pos) ; i.e. no invalid "label"'s yet
1201 ; been found.
1202 (looking-at c-nonlabel-token-key)) ; e.g. "while :"
1203 ;; We're in a potential label and it's the first
1204 ;; time we've found something that isn't allowed in
1205 ;; one.
1206 (setq label-good-pos (or tok start))))
1208 ;; We've moved back by a sexp, so update the token positions.
1209 (setq sym nil
1210 pptok ptok
1211 ptok tok
1212 tok (point)
1213 pos tok) ; always non-nil
1214 ) ; end of (catch loop ....)
1215 ) ; end of sexp-at-a-time (while ....)
1217 ;; If the stack isn't empty there might be errors to report.
1218 (while stack
1219 (if (and (vectorp saved-pos) (eq (length saved-pos) 3))
1220 (c-bos-report-error))
1221 (setq saved-pos (cdr (car stack))
1222 stack (cdr stack)))
1224 (when (and (eq ret 'same)
1225 (not (memq sym '(boundary ignore nil))))
1226 ;; Need to investigate closer whether we've crossed
1227 ;; between a substatement and its containing statement.
1228 (if (setq saved
1229 (cond ((and (looking-at c-block-stmt-1-2-key)
1230 (eq (char-after ptok) ?\())
1231 pptok)
1232 ((looking-at c-block-stmt-1-key)
1233 ptok)
1234 (t pptok)))
1235 (cond ((> start saved) (setq pos saved))
1236 ((= start saved) (setq ret 'up)))))
1238 (when (and (not ignore-labels)
1239 (eq c-maybe-labelp t)
1240 (not (eq ret 'beginning))
1241 after-labels-pos
1242 (not bitfield-type-pos) ; Bitfields take precedence over labels.
1243 (or (not label-good-pos)
1244 (<= label-good-pos pos)
1245 (progn
1246 (goto-char (if (and last-label-pos
1247 (< last-label-pos start))
1248 last-label-pos
1249 pos))
1250 (looking-at c-label-kwds-regexp))))
1251 ;; We're in a label. Maybe we should step to the statement
1252 ;; after it.
1253 (if (< after-labels-pos start)
1254 (setq pos after-labels-pos)
1255 (setq ret 'label)
1256 (if (and last-label-pos (< last-label-pos start))
1257 ;; Might have jumped over several labels. Go to the last one.
1258 (setq pos last-label-pos)))))
1260 ;; Have we got "case <expression>:"?
1261 (goto-char pos)
1262 (when (and after-case:-pos
1263 (not (eq ret 'beginning))
1264 (looking-at c-case-kwds-regexp))
1265 (if (< after-case:-pos start)
1266 (setq pos after-case:-pos))
1267 (if (eq ret 'same)
1268 (setq ret 'label)))
1270 ;; Skip over the unary operators that can start the statement.
1271 (while (progn
1272 (c-backward-syntactic-ws)
1273 ;; protect AWK post-inc/decrement operators, etc.
1274 (and (not (c-at-vsemi-p (point)))
1275 (/= (skip-chars-backward "-+!*&~@`#") 0)))
1276 (setq pos (point)))
1277 (goto-char pos)
1278 ret)))
1280 (defun c-punctuation-in (from to)
1281 "Return non-nil if there is a non-comment non-macro punctuation character
1282 between FROM and TO. FROM must not be in a string or comment. The returned
1283 value is the position of the first such character."
1284 (save-excursion
1285 (goto-char from)
1286 (let ((pos (point)))
1287 (while (progn (skip-chars-forward c-symbol-chars to)
1288 (c-forward-syntactic-ws to)
1289 (> (point) pos))
1290 (setq pos (point))))
1291 (and (< (point) to) (point))))
1293 (defun c-crosses-statement-barrier-p (from to)
1294 "Return non-nil if buffer positions FROM to TO cross one or more
1295 statement or declaration boundaries. The returned value is actually
1296 the position of the earliest boundary char. FROM must not be within
1297 a string or comment.
1299 The variable `c-maybe-labelp' is set to the position of the first `:' that
1300 might start a label (i.e. not part of `::' and not preceded by `?'). If a
1301 single `?' is found, then `c-maybe-labelp' is cleared.
1303 For AWK, a statement which is terminated by an EOL (not a ; or a }) is
1304 regarded as having a \"virtual semicolon\" immediately after the last token on
1305 the line. If this virtual semicolon is _at_ from, the function recognizes it.
1307 Note that this function might do hidden buffer changes. See the
1308 comment at the start of cc-engine.el for more info."
1309 (let* ((skip-chars
1310 ;; If the current language has CPP macros, insert # into skip-chars.
1311 (if c-opt-cpp-symbol
1312 (concat (substring c-stmt-delim-chars 0 1) ; "^"
1313 c-opt-cpp-symbol ; usually "#"
1314 (substring c-stmt-delim-chars 1)) ; e.g. ";{}?:"
1315 c-stmt-delim-chars))
1316 (non-skip-list
1317 (append (substring skip-chars 1) nil)) ; e.g. (?# ?\; ?{ ?} ?? ?:)
1318 lit-range lit-start vsemi-pos)
1319 (save-restriction
1320 (widen)
1321 (save-excursion
1322 (catch 'done
1323 (goto-char from)
1324 (while (progn (skip-chars-forward
1325 skip-chars
1326 (min to (c-point 'bonl)))
1327 (< (point) to))
1328 (cond
1329 ;; Virtual semicolon?
1330 ((and (bolp)
1331 (save-excursion
1332 (progn
1333 (if (setq lit-start (c-literal-start from)) ; Have we landed in a string/comment?
1334 (goto-char lit-start))
1335 (c-backward-syntactic-ws) ; ? put a limit here, maybe?
1336 (setq vsemi-pos (point))
1337 (c-at-vsemi-p))))
1338 (throw 'done vsemi-pos))
1339 ;; In a string/comment?
1340 ((setq lit-range (c-literal-limits from))
1341 (goto-char (cdr lit-range)))
1342 ((eq (char-after) ?:)
1343 (forward-char)
1344 (if (and (eq (char-after) ?:)
1345 (< (point) to))
1346 ;; Ignore scope operators.
1347 (forward-char)
1348 (setq c-maybe-labelp (1- (point)))))
1349 ((eq (char-after) ??)
1350 ;; A question mark. Can't be a label, so stop
1351 ;; looking for more : and ?.
1352 (setq c-maybe-labelp nil
1353 skip-chars (substring c-stmt-delim-chars 0 -2)))
1354 ;; At a CPP construct or a "#" or "##" operator?
1355 ((and c-opt-cpp-symbol (looking-at c-opt-cpp-symbol))
1356 (if (save-excursion
1357 (skip-chars-backward " \t")
1358 (and (bolp)
1359 (or (bobp)
1360 (not (eq (char-before (1- (point))) ?\\)))))
1361 (c-end-of-macro)
1362 (skip-chars-forward c-opt-cpp-symbol)))
1363 ((memq (char-after) non-skip-list)
1364 (throw 'done (point)))))
1365 ;; In trailing space after an as yet undetected virtual semicolon?
1366 (c-backward-syntactic-ws from)
1367 (when (and (bolp) (not (bobp))) ; Can happen in AWK Mode with an
1368 ; unterminated string/regexp.
1369 (backward-char))
1370 (if (and (< (point) to)
1371 (c-at-vsemi-p))
1372 (point)
1373 nil))))))
1375 (defun c-at-statement-start-p ()
1376 "Return non-nil if the point is at the first token in a statement
1377 or somewhere in the syntactic whitespace before it.
1379 A \"statement\" here is not restricted to those inside code blocks.
1380 Any kind of declaration-like construct that occur outside function
1381 bodies is also considered a \"statement\".
1383 Note that this function might do hidden buffer changes. See the
1384 comment at the start of cc-engine.el for more info."
1386 (save-excursion
1387 (let ((end (point))
1388 c-maybe-labelp)
1389 (c-syntactic-skip-backward (substring c-stmt-delim-chars 1) nil t)
1390 (or (bobp)
1391 (eq (char-before) ?})
1392 (and (eq (char-before) ?{)
1393 (not (and c-special-brace-lists
1394 (progn (backward-char)
1395 (c-looking-at-special-brace-list)))))
1396 (c-crosses-statement-barrier-p (point) end)))))
1398 (defun c-at-expression-start-p ()
1399 "Return non-nil if the point is at the first token in an expression or
1400 statement, or somewhere in the syntactic whitespace before it.
1402 An \"expression\" here is a bit different from the normal language
1403 grammar sense: It's any sequence of expression tokens except commas,
1404 unless they are enclosed inside parentheses of some kind. Also, an
1405 expression never continues past an enclosing parenthesis, but it might
1406 contain parenthesis pairs of any sort except braces.
1408 Since expressions never cross statement boundaries, this function also
1409 recognizes statement beginnings, just like `c-at-statement-start-p'.
1411 Note that this function might do hidden buffer changes. See the
1412 comment at the start of cc-engine.el for more info."
1414 (save-excursion
1415 (let ((end (point))
1416 (c-stmt-delim-chars c-stmt-delim-chars-with-comma)
1417 c-maybe-labelp)
1418 (c-syntactic-skip-backward (substring c-stmt-delim-chars 1) nil t)
1419 (or (bobp)
1420 (memq (char-before) '(?{ ?}))
1421 (save-excursion (backward-char)
1422 (looking-at "\\s("))
1423 (c-crosses-statement-barrier-p (point) end)))))
1426 ;; A set of functions that covers various idiosyncrasies in
1427 ;; implementations of `forward-comment'.
1429 ;; Note: Some emacsen considers incorrectly that any line comment
1430 ;; ending with a backslash continues to the next line. I can't think
1431 ;; of any way to work around that in a reliable way without changing
1432 ;; the buffer, though. Suggestions welcome. ;) (No, temporarily
1433 ;; changing the syntax for backslash doesn't work since we must treat
1434 ;; escapes in string literals correctly.)
1436 (defun c-forward-single-comment ()
1437 "Move forward past whitespace and the closest following comment, if any.
1438 Return t if a comment was found, nil otherwise. In either case, the
1439 point is moved past the following whitespace. Line continuations,
1440 i.e. a backslashes followed by line breaks, are treated as whitespace.
1441 The line breaks that end line comments are considered to be the
1442 comment enders, so the point will be put on the beginning of the next
1443 line if it moved past a line comment.
1445 This function does not do any hidden buffer changes."
1447 (let ((start (point)))
1448 (when (looking-at "\\([ \t\n\r\f\v]\\|\\\\[\n\r]\\)+")
1449 (goto-char (match-end 0)))
1451 (when (forward-comment 1)
1452 (if (eobp)
1453 ;; Some emacsen (e.g. XEmacs 21) return t when moving
1454 ;; forwards at eob.
1457 ;; Emacs includes the ending newline in a b-style (c++)
1458 ;; comment, but XEmacs doesn't. We depend on the Emacs
1459 ;; behavior (which also is symmetric).
1460 (if (and (eolp) (elt (parse-partial-sexp start (point)) 7))
1461 (condition-case nil (forward-char 1)))
1463 t))))
1465 (defsubst c-forward-comments ()
1466 "Move forward past all following whitespace and comments.
1467 Line continuations, i.e. a backslashes followed by line breaks, are
1468 treated as whitespace.
1470 Note that this function might do hidden buffer changes. See the
1471 comment at the start of cc-engine.el for more info."
1473 (while (or
1474 ;; If forward-comment in at least XEmacs 21 is given a large
1475 ;; positive value, it'll loop all the way through if it hits
1476 ;; eob.
1477 (and (forward-comment 5)
1478 ;; Some emacsen (e.g. XEmacs 21) return t when moving
1479 ;; forwards at eob.
1480 (not (eobp)))
1482 (when (looking-at "\\\\[\n\r]")
1483 (forward-char 2)
1484 t))))
1486 (defun c-backward-single-comment ()
1487 "Move backward past whitespace and the closest preceding comment, if any.
1488 Return t if a comment was found, nil otherwise. In either case, the
1489 point is moved past the preceding whitespace. Line continuations,
1490 i.e. a backslashes followed by line breaks, are treated as whitespace.
1491 The line breaks that end line comments are considered to be the
1492 comment enders, so the point cannot be at the end of the same line to
1493 move over a line comment.
1495 This function does not do any hidden buffer changes."
1497 (let ((start (point)))
1498 ;; When we got newline terminated comments, forward-comment in all
1499 ;; supported emacsen so far will stop at eol of each line not
1500 ;; ending with a comment when moving backwards. This corrects for
1501 ;; that, and at the same time handles line continuations.
1502 (while (progn
1503 (skip-chars-backward " \t\n\r\f\v")
1504 (and (looking-at "[\n\r]")
1505 (eq (char-before) ?\\)))
1506 (backward-char))
1508 (if (bobp)
1509 ;; Some emacsen (e.g. Emacs 19.34) return t when moving
1510 ;; backwards at bob.
1513 ;; Leave point after the closest following newline if we've
1514 ;; backed up over any above, since forward-comment won't move
1515 ;; backward over a line comment if point is at the end of the
1516 ;; same line.
1517 (re-search-forward "\\=\\s *[\n\r]" start t)
1519 (if (if (forward-comment -1)
1520 (if (eolp)
1521 ;; If forward-comment above succeeded and we're at eol
1522 ;; then the newline we moved over above didn't end a
1523 ;; line comment, so we give it another go.
1524 (forward-comment -1)
1527 ;; Emacs <= 20 and XEmacs move back over the closer of a
1528 ;; block comment that lacks an opener.
1529 (if (looking-at "\\*/")
1530 (progn (forward-char 2) nil)
1531 t)))))
1533 (defsubst c-backward-comments ()
1534 "Move backward past all preceding whitespace and comments.
1535 Line continuations, i.e. a backslashes followed by line breaks, are
1536 treated as whitespace. The line breaks that end line comments are
1537 considered to be the comment enders, so the point cannot be at the end
1538 of the same line to move over a line comment. Unlike
1539 c-backward-syntactic-ws, this function doesn't move back over
1540 preprocessor directives.
1542 Note that this function might do hidden buffer changes. See the
1543 comment at the start of cc-engine.el for more info."
1545 (let ((start (point)))
1546 (while (and
1547 ;; `forward-comment' in some emacsen (e.g. XEmacs 21.4)
1548 ;; return t when moving backwards at bob.
1549 (not (bobp))
1551 (if (let (moved-comment)
1552 (while
1553 (and (not (setq moved-comment (forward-comment -1)))
1554 ;; Cope specifically with ^M^J here -
1555 ;; forward-comment sometimes gets stuck after ^Ms,
1556 ;; sometimes after ^M^J.
1558 (when (eq (char-before) ?\r)
1559 (backward-char)
1561 (when (and (eq (char-before) ?\n)
1562 (eq (char-before (1- (point))) ?\r))
1563 (backward-char 2)
1564 t))))
1565 moved-comment)
1566 (if (looking-at "\\*/")
1567 ;; Emacs <= 20 and XEmacs move back over the
1568 ;; closer of a block comment that lacks an opener.
1569 (progn (forward-char 2) nil)
1572 ;; XEmacs treats line continuations as whitespace but
1573 ;; only in the backward direction, which seems a bit
1574 ;; odd. Anyway, this is necessary for Emacs.
1575 (when (and (looking-at "[\n\r]")
1576 (eq (char-before) ?\\)
1577 (< (point) start))
1578 (backward-char)
1579 t))))))
1582 ;; Tools for skipping over syntactic whitespace.
1584 ;; The following functions use text properties to cache searches over
1585 ;; large regions of syntactic whitespace. It works as follows:
1587 ;; o If a syntactic whitespace region contains anything but simple
1588 ;; whitespace (i.e. space, tab and line breaks), the text property
1589 ;; `c-in-sws' is put over it. At places where we have stopped
1590 ;; within that region there's also a `c-is-sws' text property.
1591 ;; That since there typically are nested whitespace inside that
1592 ;; must be handled separately, e.g. whitespace inside a comment or
1593 ;; cpp directive. Thus, from one point with `c-is-sws' it's safe
1594 ;; to jump to another point with that property within the same
1595 ;; `c-in-sws' region. It can be likened to a ladder where
1596 ;; `c-in-sws' marks the bars and `c-is-sws' the rungs.
1598 ;; o The `c-is-sws' property is put on the simple whitespace chars at
1599 ;; a "rung position" and also maybe on the first following char.
1600 ;; As many characters as can be conveniently found in this range
1601 ;; are marked, but no assumption can be made that the whole range
1602 ;; is marked (it could be clobbered by later changes, for
1603 ;; instance).
1605 ;; Note that some part of the beginning of a sequence of simple
1606 ;; whitespace might be part of the end of a preceding line comment
1607 ;; or cpp directive and must not be considered part of the "rung".
1608 ;; Such whitespace is some amount of horizontal whitespace followed
1609 ;; by a newline. In the case of cpp directives it could also be
1610 ;; two newlines with horizontal whitespace between them.
1612 ;; The reason to include the first following char is to cope with
1613 ;; "rung positions" that don't have any ordinary whitespace. If
1614 ;; `c-is-sws' is put on a token character it does not have
1615 ;; `c-in-sws' set simultaneously. That's the only case when that
1616 ;; can occur, and the reason for not extending the `c-in-sws'
1617 ;; region to cover it is that the `c-in-sws' region could then be
1618 ;; accidentally merged with a following one if the token is only
1619 ;; one character long.
1621 ;; o On buffer changes the `c-in-sws' and `c-is-sws' properties are
1622 ;; removed in the changed region. If the change was inside
1623 ;; syntactic whitespace that means that the "ladder" is broken, but
1624 ;; a later call to `c-forward-sws' or `c-backward-sws' will use the
1625 ;; parts on either side and use an ordinary search only to "repair"
1626 ;; the gap.
1628 ;; Special care needs to be taken if a region is removed: If there
1629 ;; are `c-in-sws' on both sides of it which do not connect inside
1630 ;; the region then they can't be joined. If e.g. a marked macro is
1631 ;; broken, syntactic whitespace inside the new text might be
1632 ;; marked. If those marks would become connected with the old
1633 ;; `c-in-sws' range around the macro then we could get a ladder
1634 ;; with one end outside the macro and the other at some whitespace
1635 ;; within it.
1637 ;; The main motivation for this system is to increase the speed in
1638 ;; skipping over the large whitespace regions that can occur at the
1639 ;; top level in e.g. header files that contain a lot of comments and
1640 ;; cpp directives. For small comments inside code it's probably
1641 ;; slower than using `forward-comment' straightforwardly, but speed is
1642 ;; not a significant factor there anyway.
1644 ; (defface c-debug-is-sws-face
1645 ; '((t (:background "GreenYellow")))
1646 ; "Debug face to mark the `c-is-sws' property.")
1647 ; (defface c-debug-in-sws-face
1648 ; '((t (:underline t)))
1649 ; "Debug face to mark the `c-in-sws' property.")
1651 ; (defun c-debug-put-sws-faces ()
1652 ; ;; Put the sws debug faces on all the `c-is-sws' and `c-in-sws'
1653 ; ;; properties in the buffer.
1654 ; (interactive)
1655 ; (save-excursion
1656 ; (c-save-buffer-state (in-face)
1657 ; (goto-char (point-min))
1658 ; (setq in-face (if (get-text-property (point) 'c-is-sws)
1659 ; (point)))
1660 ; (while (progn
1661 ; (goto-char (next-single-property-change
1662 ; (point) 'c-is-sws nil (point-max)))
1663 ; (if in-face
1664 ; (progn
1665 ; (c-debug-add-face in-face (point) 'c-debug-is-sws-face)
1666 ; (setq in-face nil))
1667 ; (setq in-face (point)))
1668 ; (not (eobp))))
1669 ; (goto-char (point-min))
1670 ; (setq in-face (if (get-text-property (point) 'c-in-sws)
1671 ; (point)))
1672 ; (while (progn
1673 ; (goto-char (next-single-property-change
1674 ; (point) 'c-in-sws nil (point-max)))
1675 ; (if in-face
1676 ; (progn
1677 ; (c-debug-add-face in-face (point) 'c-debug-in-sws-face)
1678 ; (setq in-face nil))
1679 ; (setq in-face (point)))
1680 ; (not (eobp)))))))
1682 (defmacro c-debug-sws-msg (&rest args)
1683 (ignore args)
1684 ;;`(message ,@args)
1687 (defmacro c-put-is-sws (beg end)
1688 ;; This macro does a hidden buffer change.
1689 `(let ((beg ,beg) (end ,end))
1690 (put-text-property beg end 'c-is-sws t)
1691 ,@(when (facep 'c-debug-is-sws-face)
1692 `((c-debug-add-face beg end 'c-debug-is-sws-face)))))
1694 (defmacro c-put-in-sws (beg end)
1695 ;; This macro does a hidden buffer change.
1696 `(let ((beg ,beg) (end ,end))
1697 (put-text-property beg end 'c-in-sws t)
1698 ,@(when (facep 'c-debug-is-sws-face)
1699 `((c-debug-add-face beg end 'c-debug-in-sws-face)))))
1701 (defmacro c-remove-is-sws (beg end)
1702 ;; This macro does a hidden buffer change.
1703 `(let ((beg ,beg) (end ,end))
1704 (remove-text-properties beg end '(c-is-sws nil))
1705 ,@(when (facep 'c-debug-is-sws-face)
1706 `((c-debug-remove-face beg end 'c-debug-is-sws-face)))))
1708 (defmacro c-remove-in-sws (beg end)
1709 ;; This macro does a hidden buffer change.
1710 `(let ((beg ,beg) (end ,end))
1711 (remove-text-properties beg end '(c-in-sws nil))
1712 ,@(when (facep 'c-debug-is-sws-face)
1713 `((c-debug-remove-face beg end 'c-debug-in-sws-face)))))
1715 (defmacro c-remove-is-and-in-sws (beg end)
1716 ;; This macro does a hidden buffer change.
1717 `(let ((beg ,beg) (end ,end))
1718 (remove-text-properties beg end '(c-is-sws nil c-in-sws nil))
1719 ,@(when (facep 'c-debug-is-sws-face)
1720 `((c-debug-remove-face beg end 'c-debug-is-sws-face)
1721 (c-debug-remove-face beg end 'c-debug-in-sws-face)))))
1723 ;; The type of literal position `end' is in in a `before-change-functions'
1724 ;; function - one of `c', `c++', `pound', or nil (but NOT `string').
1725 (defvar c-sws-lit-type nil)
1726 ;; A cons (START . STOP) of the bounds of the comment or CPP construct
1727 ;; enclosing END, if any, else nil.
1728 (defvar c-sws-lit-limits nil)
1730 (defun c-invalidate-sws-region-before (end)
1731 ;; Called from c-before-change. END is the end of the change region, the
1732 ;; standard parameter given to all before-change-functions.
1734 ;; Note whether END is inside a comment or CPP construct, and if so note its
1735 ;; bounds in `c-sws-lit-limits' and type in `c-sws-lit-type'.
1736 (save-excursion
1737 (goto-char end)
1738 (let* ((limits (c-literal-limits))
1739 (lit-type (c-literal-type limits)))
1740 (cond
1741 ((memq lit-type '(c c++))
1742 (setq c-sws-lit-type lit-type
1743 c-sws-lit-limits limits))
1744 ((c-beginning-of-macro)
1745 (setq c-sws-lit-type 'pound
1746 c-sws-lit-limits (cons (point)
1747 (progn (c-end-of-macro) (point)))))
1748 (t (setq c-sws-lit-type nil
1749 c-sws-lit-limits nil))))))
1751 (defun c-invalidate-sws-region-after-del (beg end old-len)
1752 ;; Text has been deleted, OLD-LEN characters of it starting from position
1753 ;; BEG. END is typically eq to BEG. Should there have been a comment or
1754 ;; CPP construct open at END before the deletion, check whether this
1755 ;; deletion deleted or "damaged" its opening delimiter. If so, return the
1756 ;; current position of where the construct ended, otherwise return nil.
1757 (when c-sws-lit-limits
1758 (setcdr c-sws-lit-limits (- (cdr c-sws-lit-limits) old-len))
1759 (if (and (< beg (+ (car c-sws-lit-limits) 2)) ; A lazy assumption that
1760 ; comment delimiters are 2
1761 ; chars long.
1762 (or (get-text-property end 'c-in-sws)
1763 (next-single-property-change end 'c-in-sws nil
1764 (cdr c-sws-lit-limits))
1765 (get-text-property end 'c-is-sws)
1766 (next-single-property-change end 'c-is-sws nil
1767 (cdr c-sws-lit-limits))))
1768 (cdr c-sws-lit-limits))))
1770 (defun c-invalidate-sws-region-after-ins (end)
1771 ;; Text has been inserted, ending at buffer position END. Should there be a
1772 ;; literal or CPP construct open at END, check whether there are `c-in-sws'
1773 ;; or `c-is-sws' text properties inside this literal. If there are, return
1774 ;; the buffer position of the end of the literal, else return nil.
1775 (save-excursion
1776 (let* ((limits (c-literal-limits))
1777 (lit-type (c-literal-type limits)))
1778 (goto-char end)
1779 (when (and (not (memq lit-type '(c c++)))
1780 (c-beginning-of-macro))
1781 (setq lit-type 'pound
1782 limits (cons (point)
1783 (progn (c-end-of-macro) (point)))))
1784 (when (memq lit-type '(c c++ pound))
1785 (let ((next-in (next-single-property-change (car limits) 'c-in-sws
1786 nil (cdr limits)))
1787 (next-is (next-single-property-change (car limits) 'c-is-sws
1788 nil (cdr limits))))
1789 (and (or next-in next-is)
1790 (cdr limits)))))))
1792 (defun c-invalidate-sws-region-after (beg end old-len)
1793 ;; Called from `after-change-functions'. Remove any stale `c-in-sws' or
1794 ;; `c-is-sws' text properties from the vicinity of the change. BEG, END,
1795 ;; and OLD-LEN are the standard arguments given to after-change functions.
1797 ;; Note that if `c-forward-sws' or `c-backward-sws' are used outside
1798 ;; `c-save-buffer-state' or similar then this will remove the cache
1799 ;; properties right after they're added.
1801 ;; This function does hidden buffer changes.
1802 (let ((del-end
1803 (and (> old-len 0)
1804 (c-invalidate-sws-region-after-del beg end old-len)))
1805 (ins-end
1806 (and (> end beg)
1807 (c-invalidate-sws-region-after-ins end))))
1808 (save-excursion
1809 ;; Adjust the end to remove the properties in any following simple
1810 ;; ws up to and including the next line break, if there is any
1811 ;; after the changed region. This is necessary e.g. when a rung
1812 ;; marked empty line is converted to a line comment by inserting
1813 ;; "//" before the line break. In that case the line break would
1814 ;; keep the rung mark which could make a later `c-backward-sws'
1815 ;; move into the line comment instead of over it.
1816 (goto-char end)
1817 (skip-chars-forward " \t\f\v")
1818 (when (and (eolp) (not (eobp)))
1819 (setq end (1+ (point)))))
1821 (when (and (= beg end)
1822 (get-text-property beg 'c-in-sws)
1823 (> beg (point-min))
1824 (get-text-property (1- beg) 'c-in-sws))
1825 ;; Ensure that an `c-in-sws' range gets broken. Note that it isn't
1826 ;; safe to keep a range that was continuous before the change. E.g:
1828 ;; #define foo
1829 ;; \
1830 ;; bar
1832 ;; There can be a "ladder" between "#" and "b". Now, if the newline
1833 ;; after "foo" is removed then "bar" will become part of the cpp
1834 ;; directive instead of a syntactically relevant token. In that
1835 ;; case there's no longer syntactic ws from "#" to "b".
1836 (setq beg (1- beg)))
1838 (setq end (max (or del-end end)
1839 (or ins-end end)
1840 end))
1842 (c-debug-sws-msg "c-invalidate-sws-region-after [%s..%s]" beg end)
1843 (c-remove-is-and-in-sws beg end)))
1845 (defun c-forward-sws ()
1846 ;; Used by `c-forward-syntactic-ws' to implement the unbounded search.
1848 ;; This function might do hidden buffer changes.
1850 (let (;; `rung-pos' is set to a position as early as possible in the
1851 ;; unmarked part of the simple ws region.
1852 (rung-pos (point)) next-rung-pos rung-end-pos last-put-in-sws-pos
1853 rung-is-marked next-rung-is-marked simple-ws-end macro-start macro-end
1854 ;; `safe-start' is set when it's safe to cache the start position.
1855 ;; This is the case except when we have an unterminated block comment
1856 ;; within a macro.
1857 safe-start)
1859 ;; Skip simple ws and do a quick check on the following character to see
1860 ;; if it's anything that can't start syntactic ws, so we can bail out
1861 ;; early in the majority of cases when there just are a few ws chars.
1862 (skip-chars-forward " \t\n\r\f\v")
1863 (when (or (looking-at c-syntactic-ws-start)
1864 (and c-opt-cpp-prefix
1865 (looking-at c-noise-macro-name-re)))
1867 (setq rung-end-pos (min (1+ (point)) (point-max)))
1868 (if (setq rung-is-marked (text-property-any rung-pos rung-end-pos
1869 'c-is-sws t))
1870 ;; Find the last rung position to avoid setting properties in all
1871 ;; the cases when the marked rung is complete.
1872 ;; (`next-single-property-change' is certain to move at least one
1873 ;; step forward.)
1874 (setq rung-pos (1- (c-next-single-property-change
1875 rung-is-marked 'c-is-sws nil rung-end-pos)))
1876 ;; Got no marked rung here. Since the simple ws might have started
1877 ;; inside a line comment or cpp directive we must set `rung-pos' as
1878 ;; high as possible.
1879 (setq rung-pos (point)))
1881 (with-silent-modifications
1882 (while
1883 (progn
1884 ;; In the following while form, we move over a "ladder" and
1885 ;; following simple WS each time round the loop, appending the WS
1886 ;; onto the ladder, joining adjacent ladders, and terminating when
1887 ;; there is no more WS or we reach EOB.
1888 (while
1889 (when (and rung-is-marked
1890 (get-text-property (point) 'c-in-sws))
1892 ;; The following search is the main reason that `c-in-sws'
1893 ;; and `c-is-sws' aren't combined to one property.
1894 (goto-char (c-next-single-property-change
1895 (point) 'c-in-sws nil (point-max)))
1896 (unless (get-text-property (point) 'c-is-sws)
1897 ;; If the `c-in-sws' region extended past the last
1898 ;; `c-is-sws' char we have to go back a bit.
1899 (or (get-text-property (1- (point)) 'c-is-sws)
1900 (goto-char (previous-single-property-change
1901 (point) 'c-is-sws)))
1902 (backward-char))
1904 (c-debug-sws-msg
1905 "c-forward-sws cached move %s -> %s (max %s)"
1906 rung-pos (point) (point-max))
1908 (setq rung-pos (point))
1909 (and (> (skip-chars-forward " \t\n\r\f\v") 0)
1910 (not (eobp))))
1912 ;; We'll loop here if there is simple ws after the last rung.
1913 ;; That means that there's been some change in it and it's
1914 ;; possible that we've stepped into another ladder, so extend
1915 ;; the previous one to join with it if there is one, and try to
1916 ;; use the cache again.
1917 (c-debug-sws-msg
1918 "c-forward-sws extending rung with [%s..%s] (max %s)"
1919 (1+ rung-pos) (1+ (point)) (point-max))
1920 (unless (get-text-property (point) 'c-is-sws)
1921 ;; Remove any `c-in-sws' property from the last char of
1922 ;; the rung before we mark it with `c-is-sws', so that we
1923 ;; won't connect with the remains of a broken "ladder".
1924 (c-remove-in-sws (point) (1+ (point))))
1925 (c-put-is-sws (1+ rung-pos)
1926 (1+ (point)))
1927 (c-put-in-sws rung-pos
1928 (setq rung-pos (point)
1929 last-put-in-sws-pos rung-pos)))
1931 ;; Now move over any comments (x)or a CPP construct.
1932 (setq simple-ws-end (point))
1933 (setq safe-start t)
1934 ;; Take elaborate precautions to detect an open block comment at
1935 ;; the end of a macro. If we find one, we set `safe-start' to nil
1936 ;; and break off any further scanning of comments.
1937 (let ((com-begin (point)) com-end in-macro)
1938 (when (and (c-forward-single-comment)
1939 (setq com-end (point))
1940 (save-excursion
1941 (goto-char com-begin)
1942 (c-beginning-of-macro)))
1943 (setq in-macro t)
1944 (goto-char com-begin)
1945 (if (progn (c-end-of-macro com-end)
1946 (< (point) com-end))
1947 (setq safe-start nil)))
1948 (if in-macro
1949 (while (and safe-start
1950 com-end (> com-end com-begin)
1951 (setq com-begin (point))
1952 (when (and (c-forward-single-comment)
1953 (setq com-end (point)))
1954 (goto-char com-begin)
1955 (if (progn (c-end-of-macro com-end)
1956 (< (point) com-end))
1957 (setq safe-start nil))
1958 safe-start)))
1959 (c-forward-comments)))
1961 (cond
1962 ((/= (point) simple-ws-end)
1963 ;; Skipped over comments. Don't cache at eob in case the buffer
1964 ;; is narrowed.
1965 (not (eobp)))
1967 ((save-excursion
1968 (and c-opt-cpp-prefix
1969 (looking-at c-opt-cpp-start)
1970 (setq macro-start (point))
1971 (progn (skip-chars-backward " \t")
1972 (bolp))
1973 (or (bobp)
1974 (progn (backward-char)
1975 (not (eq (char-before) ?\\))))))
1976 ;; Skip a preprocessor directive.
1977 (end-of-line)
1978 (while (and (eq (char-before) ?\\)
1979 (= (forward-line 1) 0))
1980 (end-of-line))
1981 (setq macro-end (point))
1982 ;; Check for an open block comment at the end of the macro.
1983 (goto-char macro-start)
1984 (let (s in-block-comment)
1985 (while
1986 (progn
1987 (setq s (parse-partial-sexp (point) macro-end
1988 nil nil s 'syntax-table))
1989 (< (point) macro-end))
1990 (setq in-block-comment
1991 (and (elt s 4) ; in a comment
1992 (null (elt s 7))))) ; a block comment
1993 (if in-block-comment (setq safe-start nil)))
1994 (forward-line 1)
1995 ;; Don't cache at eob in case the buffer is narrowed.
1996 (not (eobp)))
1998 ((and c-opt-cpp-prefix
1999 (looking-at c-noise-macro-name-re))
2000 ;; Skip over a noise macro.
2001 (goto-char (match-end 1))
2002 (not (eobp)))))
2004 ;; We've searched over a piece of non-white syntactic ws. See if this
2005 ;; can be cached.
2006 (setq next-rung-pos (point))
2007 (skip-chars-forward " \t\n\r\f\v")
2008 (setq rung-end-pos (min (1+ (point)) (point-max)))
2010 (if (or
2011 ;; Cache if we haven't skipped comments only, and if we started
2012 ;; either from a marked rung or from a completely uncached
2013 ;; position.
2014 (and safe-start
2015 (or rung-is-marked
2016 (not (get-text-property simple-ws-end 'c-in-sws))))
2018 ;; See if there's a marked rung in the encountered simple ws. If
2019 ;; so then we can cache, unless `safe-start' is nil. Even then
2020 ;; we need to do this to check if the cache can be used for the
2021 ;; next step.
2022 (and (setq next-rung-is-marked
2023 (text-property-any next-rung-pos rung-end-pos
2024 'c-is-sws t))
2025 safe-start))
2027 (progn
2028 (c-debug-sws-msg
2029 "c-forward-sws caching [%s..%s] - [%s..%s] (max %s)"
2030 rung-pos (1+ simple-ws-end) next-rung-pos rung-end-pos
2031 (point-max))
2033 ;; Remove the properties for any nested ws that might be cached.
2034 ;; Only necessary for `c-is-sws' since `c-in-sws' will be set
2035 ;; anyway.
2036 (c-remove-is-sws (1+ simple-ws-end) next-rung-pos)
2037 (unless (and rung-is-marked (= rung-pos simple-ws-end))
2038 (c-put-is-sws rung-pos
2039 (1+ simple-ws-end))
2040 (setq rung-is-marked t))
2041 (c-put-in-sws rung-pos
2042 (setq rung-pos (point)
2043 last-put-in-sws-pos rung-pos))
2044 (unless (get-text-property (1- rung-end-pos) 'c-is-sws)
2045 ;; Remove any `c-in-sws' property from the last char of
2046 ;; the rung before we mark it with `c-is-sws', so that we
2047 ;; won't connect with the remains of a broken "ladder".
2048 (c-remove-in-sws (1- rung-end-pos) rung-end-pos))
2049 (c-put-is-sws next-rung-pos
2050 rung-end-pos))
2052 (c-debug-sws-msg
2053 "c-forward-sws not caching [%s..%s] - [%s..%s] (max %s)"
2054 rung-pos (1+ simple-ws-end) next-rung-pos rung-end-pos
2055 (point-max))
2057 ;; Set `rung-pos' for the next rung. It's the same thing here as
2058 ;; initially, except that the rung position is set as early as
2059 ;; possible since we can't be in the ending ws of a line comment or
2060 ;; cpp directive now.
2061 (if (setq rung-is-marked next-rung-is-marked)
2062 (setq rung-pos (1- (c-next-single-property-change
2063 rung-is-marked 'c-is-sws nil rung-end-pos)))
2064 (setq rung-pos next-rung-pos))))
2066 ;; Make sure that the newly marked `c-in-sws' region doesn't connect to
2067 ;; another one after the point (which might occur when editing inside a
2068 ;; comment or macro).
2069 (when (eq last-put-in-sws-pos (point))
2070 (cond ((< last-put-in-sws-pos (point-max))
2071 (c-debug-sws-msg
2072 "c-forward-sws clearing at %s for cache separation"
2073 last-put-in-sws-pos)
2074 (c-remove-in-sws last-put-in-sws-pos
2075 (1+ last-put-in-sws-pos)))
2077 ;; If at eob we have to clear the last character before the end
2078 ;; instead since the buffer might be narrowed and there might
2079 ;; be a `c-in-sws' after (point-max). In this case it's
2080 ;; necessary to clear both properties.
2081 (c-debug-sws-msg
2082 "c-forward-sws clearing thoroughly at %s for cache separation"
2083 (1- last-put-in-sws-pos))
2084 (c-remove-is-and-in-sws (1- last-put-in-sws-pos)
2085 last-put-in-sws-pos))))
2086 ))))
2088 (defun c-backward-sws ()
2089 ;; Used by `c-backward-syntactic-ws' to implement the unbounded search.
2091 ;; This function might do hidden buffer changes.
2093 (let (;; `rung-pos' is set to a position as late as possible in the unmarked
2094 ;; part of the simple ws region.
2095 (rung-pos (point)) next-rung-pos last-put-in-sws-pos
2096 rung-is-marked simple-ws-beg cmt-skip-pos)
2098 ;; Skip simple horizontal ws and do a quick check on the preceding
2099 ;; character to see if it's anything that can't end syntactic ws, so we can
2100 ;; bail out early in the majority of cases when there just are a few ws
2101 ;; chars. Newlines are complicated in the backward direction, so we can't
2102 ;; skip over them.
2103 (skip-chars-backward " \t\f")
2104 (when (and (not (bobp))
2105 (save-excursion
2106 (backward-char)
2107 (or (looking-at c-syntactic-ws-end)
2108 (and c-opt-cpp-prefix
2109 (looking-at c-symbol-char-key)
2110 (progn (c-beginning-of-current-token)
2111 (looking-at c-noise-macro-name-re))))))
2112 ;; Try to find a rung position in the simple ws preceding point, so that
2113 ;; we can get a cache hit even if the last bit of the simple ws has
2114 ;; changed recently.
2115 (setq simple-ws-beg (point))
2116 (skip-chars-backward " \t\n\r\f\v")
2117 (if (setq rung-is-marked (text-property-any
2118 (point) (min (1+ rung-pos) (point-max))
2119 'c-is-sws t))
2120 ;; `rung-pos' will be the earliest marked position, which means that
2121 ;; there might be later unmarked parts in the simple ws region.
2122 ;; It's not worth the effort to fix that; the last part of the
2123 ;; simple ws is also typically edited often, so it could be wasted.
2124 (goto-char (setq rung-pos rung-is-marked))
2125 (goto-char simple-ws-beg))
2127 (with-silent-modifications
2128 (while
2129 (progn
2130 ;; Each time round the next while form, we move back over a ladder
2131 ;; and append any simple WS preceding it, if possible joining with
2132 ;; the previous ladder.
2133 (while
2134 (when (and rung-is-marked
2135 (not (bobp))
2136 (get-text-property (1- (point)) 'c-in-sws))
2138 ;; The following search is the main reason that `c-in-sws'
2139 ;; and `c-is-sws' aren't combined to one property.
2140 (goto-char (previous-single-property-change
2141 (point) 'c-in-sws nil (point-min)))
2142 (unless (get-text-property (point) 'c-is-sws)
2143 ;; If the `c-in-sws' region extended past the first
2144 ;; `c-is-sws' char we have to go forward a bit.
2145 (goto-char (c-next-single-property-change
2146 (point) 'c-is-sws)))
2148 (c-debug-sws-msg
2149 "c-backward-sws cached move %s <- %s (min %s)"
2150 (point) rung-pos (point-min))
2152 (setq rung-pos (point))
2153 (if (and (< (min (skip-chars-backward " \t\f\v")
2154 (progn
2155 (setq simple-ws-beg (point))
2156 (skip-chars-backward " \t\n\r\f\v")))
2158 (setq rung-is-marked
2159 (text-property-any (point) rung-pos
2160 'c-is-sws t)))
2162 (goto-char simple-ws-beg)
2163 nil))
2165 ;; We'll loop here if there is simple ws before the first rung.
2166 ;; That means that there's been some change in it and it's
2167 ;; possible that we've stepped into another ladder, so extend
2168 ;; the previous one to join with it if there is one, and try to
2169 ;; use the cache again.
2170 (c-debug-sws-msg
2171 "c-backward-sws extending rung with [%s..%s] (min %s)"
2172 rung-is-marked rung-pos (point-min))
2173 (unless (get-text-property (1- rung-pos) 'c-is-sws)
2174 ;; Remove any `c-in-sws' property from the last char of
2175 ;; the rung before we mark it with `c-is-sws', so that we
2176 ;; won't connect with the remains of a broken "ladder".
2177 (c-remove-in-sws (1- rung-pos) rung-pos))
2178 (c-put-is-sws rung-is-marked
2179 rung-pos)
2180 (c-put-in-sws rung-is-marked
2181 (1- rung-pos))
2182 (setq rung-pos rung-is-marked
2183 last-put-in-sws-pos rung-pos))
2185 (c-backward-comments)
2186 (setq cmt-skip-pos (point))
2188 (cond
2189 ((and c-opt-cpp-prefix
2190 (/= cmt-skip-pos simple-ws-beg)
2191 (c-beginning-of-macro))
2192 ;; Inside a cpp directive. See if it should be skipped over.
2193 (let ((cpp-beg (point)))
2195 ;; Move back over all line continuations in the region skipped
2196 ;; over by `c-backward-comments'. If we go past it then we
2197 ;; started inside the cpp directive.
2198 (goto-char simple-ws-beg)
2199 (beginning-of-line)
2200 (while (and (> (point) cmt-skip-pos)
2201 (progn (backward-char)
2202 (eq (char-before) ?\\)))
2203 (beginning-of-line))
2205 (if (< (point) cmt-skip-pos)
2206 ;; Don't move past the cpp directive if we began inside
2207 ;; it. Note that the position at the end of the last line
2208 ;; of the macro is also considered to be within it.
2209 (progn (goto-char cmt-skip-pos)
2210 nil)
2212 ;; It's worthwhile to spend a little bit of effort on finding
2213 ;; the end of the macro, to get a good `simple-ws-beg'
2214 ;; position for the cache. Note that `c-backward-comments'
2215 ;; could have stepped over some comments before going into
2216 ;; the macro, and then `simple-ws-beg' must be kept on the
2217 ;; same side of those comments.
2218 (goto-char simple-ws-beg)
2219 (skip-chars-backward " \t\n\r\f\v")
2220 (if (eq (char-before) ?\\)
2221 (forward-char))
2222 (forward-line 1)
2223 (if (< (point) simple-ws-beg)
2224 ;; Might happen if comments after the macro were skipped
2225 ;; over.
2226 (setq simple-ws-beg (point)))
2228 (goto-char cpp-beg)
2229 t)))
2231 ((/= (save-excursion
2232 (skip-chars-forward " \t\n\r\f\v" simple-ws-beg)
2233 (setq next-rung-pos (point)))
2234 simple-ws-beg)
2235 ;; Skipped over comments. Must put point at the end of
2236 ;; the simple ws at point since we might be after a line
2237 ;; comment or cpp directive that's been partially
2238 ;; narrowed out, and we can't risk marking the simple ws
2239 ;; at the end of it.
2240 (goto-char next-rung-pos)
2243 ((and c-opt-cpp-prefix
2244 (save-excursion
2245 (and (< (skip-syntax-backward "w_") 0)
2246 (progn (setq next-rung-pos (point))
2247 (looking-at c-noise-macro-name-re)))))
2248 ;; Skipped over a noise macro
2249 (goto-char next-rung-pos)
2250 t)))
2252 ;; We've searched over a piece of non-white syntactic ws. See if this
2253 ;; can be cached.
2254 (setq next-rung-pos (point))
2255 (skip-chars-backward " \t\f\v")
2257 (if (or
2258 ;; Cache if we started either from a marked rung or from a
2259 ;; completely uncached position.
2260 rung-is-marked
2261 (not (get-text-property (1- simple-ws-beg) 'c-in-sws))
2263 ;; Cache if there's a marked rung in the encountered simple ws.
2264 (save-excursion
2265 (skip-chars-backward " \t\n\r\f\v")
2266 (text-property-any (point) (min (1+ next-rung-pos) (point-max))
2267 'c-is-sws t)))
2269 (progn
2270 (c-debug-sws-msg
2271 "c-backward-sws caching [%s..%s] - [%s..%s] (min %s)"
2272 (point) (1+ next-rung-pos)
2273 simple-ws-beg (min (1+ rung-pos) (point-max))
2274 (point-min))
2276 ;; Remove the properties for any nested ws that might be cached.
2277 ;; Only necessary for `c-is-sws' since `c-in-sws' will be set
2278 ;; anyway.
2279 (c-remove-is-sws (1+ next-rung-pos) simple-ws-beg)
2280 (unless (and rung-is-marked (= simple-ws-beg rung-pos))
2281 (let ((rung-end-pos (min (1+ rung-pos) (point-max))))
2282 (unless (get-text-property (1- rung-end-pos) 'c-is-sws)
2283 ;; Remove any `c-in-sws' property from the last char of
2284 ;; the rung before we mark it with `c-is-sws', so that we
2285 ;; won't connect with the remains of a broken "ladder".
2286 (c-remove-in-sws (1- rung-end-pos) rung-end-pos))
2287 (c-put-is-sws simple-ws-beg
2288 rung-end-pos)
2289 (setq rung-is-marked t)))
2290 (c-put-in-sws (setq simple-ws-beg (point)
2291 last-put-in-sws-pos simple-ws-beg)
2292 rung-pos)
2293 (c-put-is-sws (setq rung-pos simple-ws-beg)
2294 (1+ next-rung-pos)))
2296 (c-debug-sws-msg
2297 "c-backward-sws not caching [%s..%s] - [%s..%s] (min %s)"
2298 (point) (1+ next-rung-pos)
2299 simple-ws-beg (min (1+ rung-pos) (point-max))
2300 (point-min))
2301 (setq rung-pos next-rung-pos
2302 simple-ws-beg (point))
2305 ;; Make sure that the newly marked `c-in-sws' region doesn't connect to
2306 ;; another one before the point (which might occur when editing inside a
2307 ;; comment or macro).
2308 (when (eq last-put-in-sws-pos (point))
2309 (cond ((< (point-min) last-put-in-sws-pos)
2310 (c-debug-sws-msg
2311 "c-backward-sws clearing at %s for cache separation"
2312 (1- last-put-in-sws-pos))
2313 (c-remove-in-sws (1- last-put-in-sws-pos)
2314 last-put-in-sws-pos))
2315 ((> (point-min) 1)
2316 ;; If at bob and the buffer is narrowed, we have to clear the
2317 ;; character we're standing on instead since there might be a
2318 ;; `c-in-sws' before (point-min). In this case it's necessary
2319 ;; to clear both properties.
2320 (c-debug-sws-msg
2321 "c-backward-sws clearing thoroughly at %s for cache separation"
2322 last-put-in-sws-pos)
2323 (c-remove-is-and-in-sws last-put-in-sws-pos
2324 (1+ last-put-in-sws-pos)))))
2325 ))))
2328 ;; Other whitespace tools
2329 (defun c-partial-ws-p (beg end)
2330 ;; Is the region (beg end) WS, and is there WS (or BOB/EOB) next to the
2331 ;; region? This is a "heuristic" function. .....
2333 ;; The motivation for the second bit is to check whether removing this
2334 ;; region would coalesce two symbols.
2336 ;; FIXME!!! This function doesn't check virtual semicolons in any way. Be
2337 ;; careful about using this function for, e.g. AWK. (2007/3/7)
2338 (save-excursion
2339 (let ((end+1 (min (1+ end) (point-max))))
2340 (or (progn (goto-char (max (point-min) (1- beg)))
2341 (c-skip-ws-forward end)
2342 (eq (point) end))
2343 (progn (goto-char beg)
2344 (c-skip-ws-forward end+1)
2345 (eq (point) end+1))))))
2347 ;; A system for finding noteworthy parens before the point.
2349 (defconst c-state-cache-too-far 5000)
2350 ;; A maximum comfortable scanning distance, e.g. between
2351 ;; `c-state-cache-good-pos' and "HERE" (where we call c-parse-state). When
2352 ;; this distance is exceeded, we take "emergency measures", e.g. by clearing
2353 ;; the cache and starting again from point-min or a beginning of defun. This
2354 ;; value can be tuned for efficiency or set to a lower value for testing.
2356 (defvar c-state-cache nil)
2357 (make-variable-buffer-local 'c-state-cache)
2358 ;; The state cache used by `c-parse-state' to cut down the amount of
2359 ;; searching. It's the result from some earlier `c-parse-state' call. See
2360 ;; `c-parse-state''s doc string for details of its structure.
2362 ;; The use of the cached info is more effective if the next
2363 ;; `c-parse-state' call is on a line close by the one the cached state
2364 ;; was made at; the cache can actually slow down a little if the
2365 ;; cached state was made very far back in the buffer. The cache is
2366 ;; most effective if `c-parse-state' is used on each line while moving
2367 ;; forward.
2369 (defvar c-state-cache-good-pos 1)
2370 (make-variable-buffer-local 'c-state-cache-good-pos)
2371 ;; This is a position where `c-state-cache' is known to be correct, or
2372 ;; nil (see below). It's a position inside one of the recorded unclosed
2373 ;; parens or the top level, but not further nested inside any literal or
2374 ;; subparen that is closed before the last recorded position.
2376 ;; The exact position is chosen to try to be close to yet earlier than
2377 ;; the position where `c-state-cache' will be called next. Right now
2378 ;; the heuristic is to set it to the position after the last found
2379 ;; closing paren (of any type) before the line on which
2380 ;; `c-parse-state' was called. That is chosen primarily to work well
2381 ;; with refontification of the current line.
2383 ;; 2009-07-28: When `c-state-point-min' and the last position where
2384 ;; `c-parse-state' or for which `c-invalidate-state-cache' was called, are
2385 ;; both in the same literal, there is no such "good position", and
2386 ;; c-state-cache-good-pos is then nil. This is the ONLY circumstance in which
2387 ;; it can be nil. In this case, `c-state-point-min-literal' will be non-nil.
2389 ;; 2009-06-12: In a brace desert, c-state-cache-good-pos may also be in
2390 ;; the middle of the desert, as long as it is not within a brace pair
2391 ;; recorded in `c-state-cache' or a paren/bracket pair.
2393 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2394 ;; We maintain a simple cache of positions which aren't in a literal, so as to
2395 ;; speed up testing for non-literality.
2396 (defconst c-state-nonlit-pos-interval 3000)
2397 ;; The approximate interval between entries in `c-state-nonlit-pos-cache'.
2399 (defvar c-state-nonlit-pos-cache nil)
2400 (make-variable-buffer-local 'c-state-nonlit-pos-cache)
2401 ;; A list of buffer positions which are known not to be in a literal or a cpp
2402 ;; construct. This is ordered with higher positions at the front of the list.
2403 ;; Only those which are less than `c-state-nonlit-pos-cache-limit' are valid.
2405 (defvar c-state-nonlit-pos-cache-limit 1)
2406 (make-variable-buffer-local 'c-state-nonlit-pos-cache-limit)
2407 ;; An upper limit on valid entries in `c-state-nonlit-pos-cache'. This is
2408 ;; reduced by buffer changes, and increased by invocations of
2409 ;; `c-state-literal-at'.
2411 (defvar c-state-semi-nonlit-pos-cache nil)
2412 (make-variable-buffer-local 'c-state-semi-nonlit-pos-cache)
2413 ;; A list of elements which are either buffer positions (when such positions
2414 ;; are not in literals) or lists of the form (POS TYPE START), where POS is
2415 ;; a buffer position inside a literal, TYPE is the type of the literal
2416 ;; ('string, 'c, or 'c++) and START is the start of the literal.
2418 (defvar c-state-semi-nonlit-pos-cache-limit 1)
2419 (make-variable-buffer-local 'c-state-semi-nonlit-pos-cache-limit)
2420 ;; An upper limit on valid entries in `c-state-semi-nonlit-pos-cache'. This
2421 ;; is reduced by buffer changes, and increased by invocations of
2422 ;; `c-parse-ps-state-below'.
2424 (defsubst c-truncate-semi-nonlit-pos-cache (pos)
2425 ;; Truncate the upper bound of the cache `c-state-semi-nonlit-pos-cache' to
2426 ;; POS, if it is higher than that position.
2427 (setq c-state-semi-nonlit-pos-cache-limit
2428 (min c-state-semi-nonlit-pos-cache-limit pos)))
2430 (defun c-state-semi-pp-to-literal (here &optional not-in-delimiter)
2431 ;; Do a parse-partial-sexp from a position in the buffer before HERE which
2432 ;; isn't in a literal, and return information about HERE, either:
2433 ;; (STATE TYPE BEG) if HERE is in a literal; or
2434 ;; (STATE) otherwise,
2435 ;; where STATE is the parsing state at HERE, TYPE is the type of the literal
2436 ;; enclosing HERE, (one of 'string, 'c, 'c++) and BEG is the starting
2437 ;; position of that literal (including the delimiter).
2439 ;; Unless NOT-IN-DELIMITER is non-nil, when TO is inside a two-character
2440 ;; comment opener, this is recognized as being in a comment literal.
2442 ;; Only elements 3 (in a string), 4 (in a comment), 5 (following a quote), 7
2443 ;; (comment type), and 8 (start of comment/string), and possibly 10 (in
2444 ;; newer Emacsen only, the syntax of a position after a potential first char
2445 ;; of a two char construct) of STATE are valid.
2446 (save-excursion
2447 (save-restriction
2448 (widen)
2449 (save-match-data
2450 (let* ((base-and-state (c-parse-ps-state-below here))
2451 (base (car base-and-state))
2452 (s (cdr base-and-state))
2453 (s (parse-partial-sexp base here nil nil s))
2455 (cond
2456 ((or (nth 3 s)
2457 (and (nth 4 s)
2458 (not (eq (nth 7 s) 'syntax-table)))) ; in a string or comment
2459 (setq ty (cond
2460 ((nth 3 s) 'string)
2461 ((nth 7 s) 'c++)
2462 (t 'c)))
2463 (list s ty (nth 8 s)))
2465 ((and (not not-in-delimiter) ; inside a comment starter
2466 (not (bobp))
2467 (progn (backward-char)
2468 (and (not (and (memq 'category-properties c-emacs-features)
2469 (looking-at "\\s!")))
2470 (looking-at c-comment-start-regexp))))
2471 (setq ty (if (looking-at c-block-comment-start-regexp) 'c 'c++))
2472 (list s ty (point)))
2474 (t (list s))))))))
2476 (defun c-state-full-pp-to-literal (here &optional not-in-delimiter)
2477 ;; This function will supersede c-state-pp-to-literal.
2479 ;; Do a parse-partial-sexp from a position in the buffer before HERE which
2480 ;; isn't in a literal, and return information about HERE, either:
2481 ;; (STATE TYPE (BEG . END)) if HERE is in a literal; or
2482 ;; (STATE) otherwise,
2483 ;; where STATE is the parsing state at HERE, TYPE is the type of the literal
2484 ;; enclosing HERE, (one of 'string, 'c, 'c++) and (BEG . END) is the
2485 ;; boundaries of that literal (including the delimiters).
2487 ;; Unless NOT-IN-DELIMITER is non-nil, when TO is inside a two-character
2488 ;; comment opener, this is recognized as being in a comment literal.
2490 ;; Only elements 3 (in a string), 4 (in a comment), 5 (following a quote), 7
2491 ;; (comment type), and 8 (start of comment/string), and possibly 10 (in
2492 ;; newer Emacsen only, the syntax of a position after a potential first char
2493 ;; of a two char construct) of STATE are valid.
2494 (save-excursion
2495 (save-restriction
2496 (widen)
2497 (save-match-data
2498 (let* ((base-and-state (c-parse-ps-state-below here))
2499 (base (car base-and-state))
2500 (s (cdr base-and-state))
2501 (s (parse-partial-sexp base here nil nil s))
2502 ty start)
2503 (cond
2504 ((or (nth 3 s)
2505 (and (nth 4 s)
2506 (not (eq (nth 7 s) 'syntax-table)))) ; in a string or comment
2507 (setq ty (cond
2508 ((nth 3 s) 'string)
2509 ((nth 7 s) 'c++)
2510 (t 'c)))
2511 (setq start (nth 8 s))
2512 (parse-partial-sexp here (point-max)
2513 nil ; TARGETDEPTH
2514 nil ; STOPBEFORE
2515 s ; OLDSTATE
2516 'syntax-table) ; stop at end of literal
2517 (list s ty (cons start (point))))
2519 ((and (not not-in-delimiter) ; inside a comment starter
2520 (not (bobp))
2521 (progn (backward-char)
2522 (and (not (and (memq 'category-properties c-emacs-features)
2523 (looking-at "\\s!")))
2524 (looking-at c-comment-start-regexp))))
2525 (setq ty (if (looking-at c-block-comment-start-regexp) 'c 'c++)
2526 start (point))
2527 (forward-comment 1)
2528 (list s ty (cons start (point))))
2530 (t (list s))))))))
2532 (defun c-state-pp-to-literal (from to &optional not-in-delimiter)
2533 ;; Do a parse-partial-sexp from FROM to TO, returning either
2534 ;; (STATE TYPE (BEG . END)) if TO is in a literal; or
2535 ;; (STATE) otherwise,
2536 ;; where STATE is the parsing state at TO, TYPE is the type of the literal
2537 ;; (one of 'c, 'c++, 'string) and (BEG . END) is the boundaries of the literal,
2538 ;; including the delimiters.
2540 ;; Unless NOT-IN-DELIMITER is non-nil, when TO is inside a two-character
2541 ;; comment opener, this is recognized as being in a comment literal.
2543 ;; Only elements 3 (in a string), 4 (in a comment), 5 (following a quote),
2544 ;; 7 (comment type) and 8 (start of comment/string) (and possibly 9) of
2545 ;; STATE are valid.
2546 (save-excursion
2547 (save-match-data
2548 (let ((s (parse-partial-sexp from to))
2549 ty co-st)
2550 (cond
2551 ((or (nth 3 s)
2552 (and (nth 4 s)
2553 (not (eq (nth 7 s) 'syntax-table)))) ; in a string or comment
2554 (setq ty (cond
2555 ((nth 3 s) 'string)
2556 ((nth 7 s) 'c++)
2557 (t 'c)))
2558 (parse-partial-sexp (point) (point-max)
2559 nil ; TARGETDEPTH
2560 nil ; STOPBEFORE
2561 s ; OLDSTATE
2562 'syntax-table) ; stop at end of literal
2563 `(,s ,ty (,(nth 8 s) . ,(point))))
2565 ((and (not not-in-delimiter) ; inside a comment starter
2566 (not (bobp))
2567 (progn (backward-char)
2568 (and (not (looking-at "\\s!"))
2569 (looking-at c-comment-start-regexp))))
2570 (setq ty (if (looking-at c-block-comment-start-regexp) 'c 'c++)
2571 co-st (point))
2572 (forward-comment 1)
2573 `(,s ,ty (,co-st . ,(point))))
2575 (t `(,s)))))))
2577 (defun c-cache-to-parse-ps-state (elt)
2578 ;; Create a list suitable to use as the old-state parameter to
2579 ;; `parse-partial-sexp', out of ELT, a member of
2580 ;; `c-state-semi-nonlit-pos-cache'. ELT is either just a number, or a list
2581 ;; with 2, 3, or 4 members (See `c-parse-ps-state-to-cache'). That number
2582 ;; or the car of the list is the "position element" of ELT, the position
2583 ;; where ELT is valid.
2585 ;; POINT is left at the postition for which the returned state is valid. It
2586 ;; will be either the position element of ELT, or one character before
2587 ;; that. (The latter happens in Emacs <= 25 and XEmacs, when ELT indicates
2588 ;; its position element directly follows a potential first character of a
2589 ;; two char construct (such as a comment opener or an escaped character).)
2590 (if (and (consp elt) (>= (length elt) 3))
2591 ;; Inside a string or comment
2592 (let ((depth 0) (containing nil) (last nil)
2593 in-string in-comment (after-quote nil)
2594 (min-depth 0) com-style com-str-start (intermediate nil)
2595 (char-1 (nth 3 elt)) ; first char of poss. 2-char construct
2596 (pos (car elt))
2597 (type (cadr elt)))
2598 (setq com-str-start (car (cddr elt)))
2599 (cond
2600 ((or (numberp type) (eq type t)) ; A string
2601 (setq in-string type))
2602 ((memq type '(c c++)) ; A comment
2603 (setq in-comment t
2604 com-style (if (eq type 'c++) 1 nil)))
2605 (t (c-benign-error "Invalid type %s in c-cache-to-parse-ps-state"
2606 elt)))
2607 (if (memq 'pps-extended-state c-emacs-features)
2608 (progn
2609 (goto-char pos)
2610 (list depth containing last
2611 in-string in-comment after-quote
2612 min-depth com-style com-str-start
2613 intermediate char-1))
2614 (goto-char (if char-1
2615 (1- pos)
2616 pos))
2617 (list depth containing last
2618 in-string in-comment nil
2619 min-depth com-style com-str-start
2620 intermediate)))
2622 ;; Not in a string or comment.
2623 (if (memq 'pps-extended-state c-emacs-features)
2624 (progn
2625 (goto-char (if (consp elt) (car elt) elt))
2626 (list 0 nil nil nil nil
2627 (and (consp elt) (eq (nth 1 elt) 9)) ; 9 is syntax code for "escape".
2628 0 nil nil nil
2629 (and (consp elt) (nth 1 elt))))
2630 (goto-char (if (consp elt) (car elt) elt))
2631 (if (and (consp elt) (cdr elt)) (backward-char))
2632 (copy-tree '(0 nil nil nil nil
2634 0 nil nil nil)))))
2636 (defun c-parse-ps-state-to-cache (state)
2637 ;; Convert STATE, a `parse-partial-sexp' state valid at POINT, to an element
2638 ;; for the `c-state-semi-nonlit-pos-cache' cache. This is one of
2639 ;; o - POINT (when point is not in a literal);
2640 ;; o - (POINT CHAR-1) (when the last character before point is potentially
2641 ;; the first of a two-character construct
2642 ;; o - (POINT TYPE STARTING-POS) (when in a literal);
2643 ;; o - (POINT TYPE STARTING-POS CHAR-1) (Combination of the previous two),
2644 ;; where TYPE is the type of the literal (either 'c, or 'c++, or the
2645 ;; character which closes the string), STARTING-POS is the starting
2646 ;; position of the comment or string. CHAR-1 is either the character
2647 ;; potentially forming the first half of a two-char construct (in Emacs <=
2648 ;; 25 and XEmacs) or the syntax of the character (in Emacs >= 26).
2649 (if (memq 'pps-extended-state c-emacs-features)
2650 ;; Emacs >= 26.
2651 (let ((basic
2652 (cond
2653 ((nth 3 state) ; A string
2654 (list (point) (nth 3 state) (nth 8 state)))
2655 ((and (nth 4 state) ; A comment
2656 (not (eq (nth 7 state) 'syntax-table))) ; but not a psuedo comment.
2657 (list (point)
2658 (if (eq (nth 7 state) 1) 'c++ 'c)
2659 (nth 8 state)))
2660 (t ; Neither string nor comment.
2661 (point)))))
2662 (if (nth 10 state)
2663 (append (if (consp basic)
2664 basic
2665 (list basic))
2666 (list (nth 10 state)))
2667 basic))
2669 ;; Emacs <= 25, XEmacs.
2670 (cond
2671 ((nth 3 state) ; A string
2672 (if (eq (char-before) ?\\)
2673 (list (point) (nth 3 state) (nth 8 state) ?\\)
2674 (list (point) (nth 3 state) (nth 8 state))))
2675 ((and (nth 4 state) ; comment
2676 (not (eq (nth 7 state) 'syntax-table)))
2677 (if (and (eq (char-before) ?*)
2678 (> (- (point) (nth 8 state)) 2)) ; not "/*/".
2679 (list (point)
2680 (if (eq (nth 7 state) 1) 'c++ 'c)
2681 (nth 8 state)
2683 (list (point)
2684 (if (eq (nth 7 state) 1) 'c++ 'c)
2685 (nth 8 state))))
2686 (t (if (memq (char-before) '(?/ ?\\))
2687 (list (point) (char-before))
2688 (point))))))
2690 (defsubst c-ps-state-cache-pos (elt)
2691 ;; Get the buffer position from ELT, an element from the cache
2692 ;; `c-state-semi-nonlit-pos-cache'.
2693 (if (atom elt)
2695 (car elt)))
2697 (defun c-parse-ps-state-below (here)
2698 ;; Given a buffer position HERE, Return a cons (CACHE-POS . STATE), where
2699 ;; CACHE-POS is a position not very far before HERE for which the
2700 ;; parse-partial-sexp STATE is valid. Note that the only valid elements of
2701 ;; STATE are those concerning comments and strings; STATE is the state of a
2702 ;; null `parse-partial-sexp' scan when CACHE-POS is not in a comment or
2703 ;; string.
2704 (save-excursion
2705 (save-restriction
2706 (widen)
2707 (let ((c c-state-semi-nonlit-pos-cache)
2708 elt state npos high-elt)
2709 ;; Trim the cache to take account of buffer changes.
2710 (while (and c (> (c-ps-state-cache-pos (car c))
2711 c-state-semi-nonlit-pos-cache-limit))
2712 (setq c (cdr c)))
2713 (setq c-state-semi-nonlit-pos-cache c)
2715 (while (and c (> (c-ps-state-cache-pos (car c)) here))
2716 (setq high-elt (car c))
2717 (setq c (cdr c)))
2718 (goto-char (or (and c (c-ps-state-cache-pos (car c)))
2719 (point-min)))
2720 (setq state
2721 (if c
2722 (c-cache-to-parse-ps-state (car c))
2723 (copy-tree '(0 nil nil nil nil nil 0 nil nil nil nil))))
2725 (when (not high-elt)
2726 ;; We need to extend the cache. Add an element to
2727 ;; `c-state-semi-nonlit-pos-cache' each iteration of the following.
2728 (while
2729 (<= (setq npos (+ (point) c-state-nonlit-pos-interval)) here)
2730 (setq state (parse-partial-sexp (point) npos nil nil state))
2731 (setq elt (c-parse-ps-state-to-cache state))
2732 (setq c-state-semi-nonlit-pos-cache
2733 (cons elt c-state-semi-nonlit-pos-cache))))
2735 (if (> (point) c-state-semi-nonlit-pos-cache-limit)
2736 (setq c-state-semi-nonlit-pos-cache-limit (point)))
2738 (cons (point) state)))))
2740 (defun c-state-safe-place (here)
2741 ;; Return a buffer position before HERE which is "safe", i.e. outside any
2742 ;; string, comment, or macro.
2744 ;; NOTE: This function manipulates `c-state-nonlit-pos-cache'. This cache
2745 ;; MAY NOT contain any positions within macros, since macros are frequently
2746 ;; turned into comments by use of the `c-cpp-delimiter' category properties.
2747 ;; We cannot rely on this mechanism whilst determining a cache pos since
2748 ;; this function is also called from outwith `c-parse-state'.
2749 (save-restriction
2750 (widen)
2751 (save-excursion
2752 (let ((c c-state-nonlit-pos-cache)
2753 pos npos high-pos lit macro-beg macro-end)
2754 ;; Trim the cache to take account of buffer changes.
2755 (while (and c (> (car c) c-state-nonlit-pos-cache-limit))
2756 (setq c (cdr c)))
2757 (setq c-state-nonlit-pos-cache c)
2759 (while (and c (> (car c) here))
2760 (setq high-pos (car c))
2761 (setq c (cdr c)))
2762 (setq pos (or (car c) (point-min)))
2764 (unless high-pos
2765 (while
2766 ;; Add an element to `c-state-nonlit-pos-cache' each iteration.
2767 (and
2768 (setq npos
2769 (when (<= (+ pos c-state-nonlit-pos-interval) here)
2770 (+ pos c-state-nonlit-pos-interval)))
2772 ;; Test for being in a literal. If so, go to after it.
2773 (progn
2774 (setq lit (car (cddr (c-state-pp-to-literal pos npos))))
2775 (or (null lit)
2776 (prog1 (<= (cdr lit) here)
2777 (setq npos (cdr lit)))))
2779 ;; Test for being in a macro. If so, go to after it.
2780 (progn
2781 (goto-char npos)
2782 (setq macro-beg
2783 (and (c-beginning-of-macro) (/= (point) npos) (point)))
2784 (when macro-beg
2785 (c-syntactic-end-of-macro)
2786 (or (eobp) (forward-char))
2787 (setq macro-end (point)))
2788 (or (null macro-beg)
2789 (prog1 (<= macro-end here)
2790 (setq npos macro-end)))))
2792 (setq pos npos)
2793 (setq c-state-nonlit-pos-cache (cons pos c-state-nonlit-pos-cache)))
2794 ;; Add one extra element above HERE so as to to avoid the previous
2795 ;; expensive calculation when the next call is close to the current
2796 ;; one. This is especially useful when inside a large macro.
2797 (when npos
2798 (setq c-state-nonlit-pos-cache
2799 (cons npos c-state-nonlit-pos-cache))))
2801 (if (> pos c-state-nonlit-pos-cache-limit)
2802 (setq c-state-nonlit-pos-cache-limit pos))
2803 pos))))
2805 (defun c-state-literal-at (here)
2806 ;; If position HERE is inside a literal, return (START . END), the
2807 ;; boundaries of the literal (which may be outside the accessible bit of the
2808 ;; buffer). Otherwise, return nil.
2810 ;; This function is almost the same as `c-literal-limits'. Previously, it
2811 ;; differed in that it was a lower level function, and that it rigorously
2812 ;; followed the syntax from BOB. `c-literal-limits' is now (2011-12)
2813 ;; virtually identical to this function.
2814 (save-restriction
2815 (widen)
2816 (save-excursion
2817 (let ((pos (c-state-safe-place here)))
2818 (car (cddr (c-state-pp-to-literal pos here)))))))
2820 (defsubst c-state-lit-beg (pos)
2821 ;; Return the start of the literal containing POS, or POS itself.
2822 (or (car (c-state-literal-at pos))
2823 pos))
2825 (defsubst c-state-cache-non-literal-place (pos state)
2826 ;; Return a position outside of a string/comment/macro at or before POS.
2827 ;; STATE is the parse-partial-sexp state at POS.
2828 (let ((res (if (or (nth 3 state) ; in a string?
2829 (and (nth 4 state)
2830 (not (eq (nth 7 state) 'syntax-table)))) ; in a comment?
2831 (nth 8 state)
2832 pos)))
2833 (save-excursion
2834 (goto-char res)
2835 (if (c-beginning-of-macro)
2836 (point)
2837 res))))
2839 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2840 ;; Stuff to do with point-min, and coping with any literal there.
2841 (defvar c-state-point-min 1)
2842 (make-variable-buffer-local 'c-state-point-min)
2843 ;; This is (point-min) when `c-state-cache' was last calculated. A change of
2844 ;; narrowing is likely to affect the parens that are visible before the point.
2846 (defvar c-state-point-min-lit-type nil)
2847 (make-variable-buffer-local 'c-state-point-min-lit-type)
2848 (defvar c-state-point-min-lit-start nil)
2849 (make-variable-buffer-local 'c-state-point-min-lit-start)
2850 ;; These two variables define the literal, if any, containing point-min.
2851 ;; Their values are, respectively, 'string, c, or c++, and the start of the
2852 ;; literal. If there's no literal there, they're both nil.
2854 (defvar c-state-min-scan-pos 1)
2855 (make-variable-buffer-local 'c-state-min-scan-pos)
2856 ;; This is the earliest buffer-pos from which scanning can be done. It is
2857 ;; either the end of the literal containing point-min, or point-min itself.
2858 ;; It becomes nil if the buffer is changed earlier than this point.
2859 (defun c-state-get-min-scan-pos ()
2860 ;; Return the lowest valid scanning pos. This will be the end of the
2861 ;; literal enclosing point-min, or point-min itself.
2862 (or c-state-min-scan-pos
2863 (save-restriction
2864 (save-excursion
2865 (widen)
2866 (goto-char c-state-point-min-lit-start)
2867 (if (eq c-state-point-min-lit-type 'string)
2868 (forward-sexp)
2869 (forward-comment 1))
2870 (setq c-state-min-scan-pos (point))))))
2872 (defun c-state-mark-point-min-literal ()
2873 ;; Determine the properties of any literal containing POINT-MIN, setting the
2874 ;; variables `c-state-point-min-lit-type', `c-state-point-min-lit-start',
2875 ;; and `c-state-min-scan-pos' accordingly. The return value is meaningless.
2876 (let ((p-min (point-min))
2877 lit)
2878 (save-restriction
2879 (widen)
2880 (setq lit (c-state-literal-at p-min))
2881 (if lit
2882 (setq c-state-point-min-lit-type
2883 (save-excursion
2884 (goto-char (car lit))
2885 (cond
2886 ((looking-at c-block-comment-start-regexp) 'c)
2887 ((looking-at c-line-comment-starter) 'c++)
2888 (t 'string)))
2889 c-state-point-min-lit-start (car lit)
2890 c-state-min-scan-pos (cdr lit))
2891 (setq c-state-point-min-lit-type nil
2892 c-state-point-min-lit-start nil
2893 c-state-min-scan-pos p-min)))))
2896 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2897 ;; A variable which signals a brace dessert - helpful for reducing the number
2898 ;; of fruitless backward scans.
2899 (defvar c-state-brace-pair-desert nil)
2900 (make-variable-buffer-local 'c-state-brace-pair-desert)
2901 ;; Used only in `c-append-lower-brace-pair-to-state-cache'. It is set when
2902 ;; that defun has searched backwards for a brace pair and not found one. Its
2903 ;; value is either nil or a cons (PA . FROM), where PA is the position of the
2904 ;; enclosing opening paren/brace/bracket which bounds the backwards search (or
2905 ;; nil when at top level) and FROM is where the backward search started. It
2906 ;; is reset to nil in `c-invalidate-state-cache'.
2909 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2910 ;; Lowish level functions/macros which work directly on `c-state-cache', or a
2911 ;; list of like structure.
2912 (defmacro c-state-cache-top-lparen (&optional cache)
2913 ;; Return the address of the top left brace/bracket/paren recorded in CACHE
2914 ;; (default `c-state-cache') (or nil).
2915 (let ((cash (or cache 'c-state-cache)))
2916 `(if (consp (car ,cash))
2917 (caar ,cash)
2918 (car ,cash))))
2920 (defmacro c-state-cache-top-paren (&optional cache)
2921 ;; Return the address of the latest brace/bracket/paren (whether left or
2922 ;; right) recorded in CACHE (default `c-state-cache') or nil.
2923 (let ((cash (or cache 'c-state-cache)))
2924 `(if (consp (car ,cash))
2925 (cdar ,cash)
2926 (car ,cash))))
2928 (defmacro c-state-cache-after-top-paren (&optional cache)
2929 ;; Return the position just after the latest brace/bracket/paren (whether
2930 ;; left or right) recorded in CACHE (default `c-state-cache') or nil.
2931 (let ((cash (or cache 'c-state-cache)))
2932 `(if (consp (car ,cash))
2933 (cdar ,cash)
2934 (and (car ,cash)
2935 (1+ (car ,cash))))))
2937 (defun c-get-cache-scan-pos (here)
2938 ;; From the state-cache, determine the buffer position from which we might
2939 ;; scan forward to HERE to update this cache. This position will be just
2940 ;; after a paren/brace/bracket recorded in the cache, if possible, otherwise
2941 ;; return the earliest position in the accessible region which isn't within
2942 ;; a literal. If the visible portion of the buffer is entirely within a
2943 ;; literal, return NIL.
2944 (let ((c c-state-cache) elt)
2945 ;(while (>= (or (c-state-cache-top-lparen c) 1) here)
2946 (while (and c
2947 (>= (c-state-cache-top-lparen c) here))
2948 (setq c (cdr c)))
2950 (setq elt (car c))
2951 (cond
2952 ((consp elt)
2953 (if (> (cdr elt) here)
2954 (1+ (car elt))
2955 (cdr elt)))
2956 (elt (1+ elt))
2957 ((<= (c-state-get-min-scan-pos) here)
2958 (c-state-get-min-scan-pos))
2959 (t nil))))
2961 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2962 ;; Variables which keep track of preprocessor constructs.
2963 (defvar c-state-old-cpp-beg-marker nil)
2964 (make-variable-buffer-local 'c-state-old-cpp-beg-marker)
2965 (defvar c-state-old-cpp-beg nil)
2966 (make-variable-buffer-local 'c-state-old-cpp-beg)
2967 (defvar c-state-old-cpp-end-marker nil)
2968 (make-variable-buffer-local 'c-state-old-cpp-end-marker)
2969 (defvar c-state-old-cpp-end nil)
2970 (make-variable-buffer-local 'c-state-old-cpp-end)
2971 ;; These are the limits of the macro containing point at the previous call of
2972 ;; `c-parse-state', or nil.
2974 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2975 ;; Defuns which analyze the buffer, yet don't change `c-state-cache'.
2976 (defun c-get-fallback-scan-pos (here)
2977 ;; Return a start position for building `c-state-cache' from
2978 ;; scratch. This will be at the top level, 2 defuns back.
2979 (save-excursion
2980 ;; Go back 2 bods, but ignore any bogus positions returned by
2981 ;; beginning-of-defun (i.e. open paren in column zero).
2982 (goto-char here)
2983 (let ((cnt 2))
2984 (while (not (or (bobp) (zerop cnt)))
2985 (c-beginning-of-defun-1) ; Pure elisp BOD.
2986 (if (eq (char-after) ?\{)
2987 (setq cnt (1- cnt)))))
2988 (point)))
2990 (defun c-state-balance-parens-backwards (here- here+ top)
2991 ;; Return the position of the opening paren/brace/bracket before HERE- which
2992 ;; matches the outermost close p/b/b between HERE+ and TOP. Except when
2993 ;; there's a macro, HERE- and HERE+ are the same. Like this:
2995 ;; ............................................
2996 ;; | |
2997 ;; ( [ ( .........#macro.. ) ( ) ] )
2998 ;; ^ ^ ^ ^
2999 ;; | | | |
3000 ;; return HERE- HERE+ TOP
3002 ;; If there aren't enough opening paren/brace/brackets, return the position
3003 ;; of the outermost one found, or HERE- if there are none. If there are no
3004 ;; closing p/b/bs between HERE+ and TOP, return HERE-. HERE-/+ and TOP
3005 ;; must not be inside literals. Only the accessible portion of the buffer
3006 ;; will be scanned.
3008 ;; PART 1: scan from `here+' up to `top', accumulating ")"s which enclose
3009 ;; `here'. Go round the next loop each time we pass over such a ")". These
3010 ;; probably match "("s before `here-'.
3011 (let (pos pa ren+1 lonely-rens)
3012 (save-excursion
3013 (save-restriction
3014 (narrow-to-region (point-min) top) ; This can move point, sometimes.
3015 (setq pos here+)
3016 (c-safe
3017 (while
3018 (setq ren+1 (c-sc-scan-lists pos 1 1)) ; might signal
3019 (setq lonely-rens (cons ren+1 lonely-rens)
3020 pos ren+1)))))
3022 ;; PART 2: Scan back before `here-' searching for the "("s
3023 ;; matching/mismatching the ")"s found above. We only need to direct the
3024 ;; caller to scan when we've encountered unmatched right parens.
3025 (setq pos here-)
3026 (when lonely-rens
3027 (c-safe
3028 (while
3029 (and lonely-rens ; actual values aren't used.
3030 (setq pa (c-sc-scan-lists pos -1 1)))
3031 (setq pos pa)
3032 (setq lonely-rens (cdr lonely-rens)))))
3033 pos))
3035 (defun c-parse-state-get-strategy (here good-pos)
3036 ;; Determine the scanning strategy for adjusting `c-parse-state', attempting
3037 ;; to minimize the amount of scanning. HERE is the pertinent position in
3038 ;; the buffer, GOOD-POS is a position where `c-state-cache' (possibly with
3039 ;; its head trimmed) is known to be good, or nil if there is no such
3040 ;; position.
3042 ;; The return value is a list, one of the following:
3044 ;; o - ('forward START-POINT) - scan forward from START-POINT,
3045 ;; which is not less than the highest position in `c-state-cache' below HERE,
3046 ;; which is after GOOD-POS.
3047 ;; o - ('backward nil) - scan backwards (from HERE).
3048 ;; o - ('back-and-forward START-POINT) - like 'forward, but when HERE is earlier
3049 ;; than GOOD-POS.
3050 ;; o - ('BOD START-POINT) - scan forwards from START-POINT, which is at the
3051 ;; top level.
3052 ;; o - ('IN-LIT nil) - point is inside the literal containing point-min.
3053 (let* ((in-macro-start ; start of macro containing HERE or nil.
3054 (save-excursion
3055 (goto-char here)
3056 (and (c-beginning-of-macro)
3057 (point))))
3058 (changed-macro-start
3059 (and in-macro-start
3060 (not (and c-state-old-cpp-beg
3061 (= in-macro-start c-state-old-cpp-beg)))
3062 in-macro-start))
3063 (cache-pos (c-get-cache-scan-pos (if changed-macro-start
3064 (min changed-macro-start here)
3065 here))) ; highest suitable position in cache (or 1)
3066 BOD-pos ; position of 2nd BOD before HERE.
3067 strategy ; 'forward, 'backward, 'BOD, or 'IN-LIT.
3068 start-point
3069 how-far) ; putative scanning distance.
3070 (setq good-pos (or good-pos (c-state-get-min-scan-pos)))
3071 (cond
3072 ((< here (c-state-get-min-scan-pos))
3073 (setq strategy 'IN-LIT
3074 start-point nil
3075 cache-pos nil
3076 how-far 0))
3077 ((<= good-pos here)
3078 (setq strategy 'forward
3079 start-point (if changed-macro-start
3080 cache-pos
3081 (max good-pos cache-pos))
3082 how-far (- here start-point)))
3083 ((< (- good-pos here) (- here cache-pos)) ; FIXME!!! ; apply some sort of weighting.
3084 (setq strategy 'backward
3085 how-far (- good-pos here)))
3087 (setq strategy 'back-and-forward
3088 start-point cache-pos
3089 how-far (- here start-point))))
3091 ;; Might we be better off starting from the top level, two defuns back,
3092 ;; instead? This heuristic no longer works well in C++, where
3093 ;; declarations inside namespace brace blocks are frequently placed at
3094 ;; column zero. (2015-11-10): Remove the condition on C++ Mode.
3095 (when (and (or (not (memq 'col-0-paren c-emacs-features))
3096 open-paren-in-column-0-is-defun-start)
3097 ;; (not (c-major-mode-is 'c++-mode))
3098 (> how-far c-state-cache-too-far))
3099 (setq BOD-pos (c-get-fallback-scan-pos here)) ; somewhat EXPENSIVE!!!
3100 (if (< (- here BOD-pos) how-far)
3101 (setq strategy 'BOD
3102 start-point BOD-pos)))
3104 (list strategy start-point)))
3107 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
3108 ;; Routines which change `c-state-cache' and associated values.
3109 (defun c-renarrow-state-cache ()
3110 ;; The region (more precisely, point-min) has changed since we
3111 ;; calculated `c-state-cache'. Amend `c-state-cache' accordingly.
3112 (if (< (point-min) c-state-point-min)
3113 ;; If point-min has MOVED BACKWARDS then we drop the state completely.
3114 ;; It would be possible to do a better job here and recalculate the top
3115 ;; only.
3116 (progn
3117 (c-state-mark-point-min-literal)
3118 (setq c-state-cache nil
3119 c-state-cache-good-pos c-state-min-scan-pos
3120 c-state-brace-pair-desert nil))
3122 ;; point-min has MOVED FORWARD.
3124 ;; Is the new point-min inside a (different) literal?
3125 (unless (and c-state-point-min-lit-start ; at prev. point-min
3126 (< (point-min) (c-state-get-min-scan-pos)))
3127 (c-state-mark-point-min-literal))
3129 ;; Cut off a bit of the tail from `c-state-cache'.
3130 (let ((ptr (cons nil c-state-cache))
3132 (while (and (setq pa (c-state-cache-top-lparen (cdr ptr)))
3133 (>= pa (point-min)))
3134 (setq ptr (cdr ptr)))
3136 (when (consp ptr)
3137 (if (or (eq (cdr ptr) c-state-cache)
3138 (and (consp (cadr ptr))
3139 (> (cdr (cadr ptr)) (point-min)))) ; Our new point-min is
3140 ; inside a recorded
3141 ; brace pair.
3142 (setq c-state-cache nil
3143 c-state-cache-good-pos c-state-min-scan-pos)
3144 (setcdr ptr nil)
3145 (setq c-state-cache-good-pos (1+ (c-state-cache-top-lparen))))
3148 (setq c-state-point-min (point-min)))
3150 (defun c-append-lower-brace-pair-to-state-cache (from here &optional upper-lim)
3151 ;; If there is a brace pair preceding FROM in the buffer, at the same level
3152 ;; of nesting (not necessarily immediately preceding), push a cons onto
3153 ;; `c-state-cache' to represent it. FROM must not be inside a literal. If
3154 ;; UPPER-LIM is non-nil, we append the highest brace pair whose "}" is below
3155 ;; UPPER-LIM.
3157 ;; Return non-nil when this has been done.
3159 ;; The situation it copes with is this transformation:
3161 ;; OLD: { (.) {...........}
3162 ;; ^ ^
3163 ;; FROM HERE
3165 ;; NEW: { {....} (.) {.........
3166 ;; ^ ^ ^
3167 ;; LOWER BRACE PAIR HERE or HERE
3169 ;; This routine should be fast. Since it can get called a LOT, we maintain
3170 ;; `c-state-brace-pair-desert', a small cache of "failures", such that we
3171 ;; reduce the time wasted in repeated fruitless searches in brace deserts.
3172 (save-excursion
3173 (save-restriction
3174 (let* (new-cons
3175 (cache-pos (c-state-cache-top-lparen)) ; might be nil.
3176 (macro-start-or-from
3177 (progn (goto-char from)
3178 (c-beginning-of-macro)
3179 (point)))
3180 (bra ; Position of "{".
3181 ;; Don't start scanning in the middle of a CPP construct unless
3182 ;; it contains HERE - these constructs, in Emacs, are "commented
3183 ;; out" with category properties.
3184 (if (eq (c-get-char-property macro-start-or-from 'category)
3185 'c-cpp-delimiter)
3186 macro-start-or-from
3187 from))
3188 ce) ; Position of "}"
3189 (or upper-lim (setq upper-lim from))
3191 ;; If we're essentially repeating a fruitless search, just give up.
3192 (unless (and c-state-brace-pair-desert
3193 (eq cache-pos (car c-state-brace-pair-desert))
3194 (or (null (car c-state-brace-pair-desert))
3195 (> from (car c-state-brace-pair-desert)))
3196 (<= from (cdr c-state-brace-pair-desert)))
3197 ;; DESERT-LIM. Avoid repeated searching through the cached desert.
3198 (let ((desert-lim
3199 (and c-state-brace-pair-desert
3200 (eq cache-pos (car c-state-brace-pair-desert))
3201 (>= from (cdr c-state-brace-pair-desert))
3202 (cdr c-state-brace-pair-desert)))
3203 ;; CACHE-LIM. This limit will be necessary when an opening
3204 ;; paren at `cache-pos' has just had its matching close paren
3205 ;; inserted into the buffer. `cache-pos' continues to be a
3206 ;; search bound, even though the algorithm below would skip
3207 ;; over the new paren pair.
3208 (cache-lim (and cache-pos (< cache-pos from) cache-pos)))
3209 (narrow-to-region
3210 (cond
3211 ((and desert-lim cache-lim)
3212 (max desert-lim cache-lim))
3213 (desert-lim)
3214 (cache-lim)
3215 ((point-min)))
3216 ;; The top limit is EOB to ensure that `bra' is inside the
3217 ;; accessible part of the buffer at the next scan operation.
3218 (1+ (buffer-size))))
3220 ;; In the next pair of nested loops, the inner one moves back past a
3221 ;; pair of (mis-)matching parens or brackets; the outer one moves
3222 ;; back over a sequence of unmatched close brace/paren/bracket each
3223 ;; time round.
3224 (while
3225 (progn
3226 (c-safe
3227 (while
3228 (and (setq ce (c-sc-scan-lists bra -1 -1)) ; back past )/]/}; might signal
3229 (setq bra (c-sc-scan-lists ce -1 1)) ; back past (/[/{; might signal
3230 (or (> bra here) ;(> ce here)
3231 (and
3232 (< ce here)
3233 (or (not (eq (char-after bra) ?\{))
3234 (and (goto-char bra)
3235 (c-beginning-of-macro)
3236 (< (point) macro-start-or-from))))))))
3237 (and ce (< ce bra)))
3238 (setq bra ce)) ; If we just backed over an unbalanced closing
3239 ; brace, ignore it.
3241 (if (and ce (< ce here) (< bra ce) (eq (char-after bra) ?\{))
3242 ;; We've found the desired brace-pair.
3243 (progn
3244 (setq new-cons (cons bra (1+ ce)))
3245 (cond
3246 ((consp (car c-state-cache))
3247 (setcar c-state-cache new-cons))
3248 ((and (numberp (car c-state-cache)) ; probably never happens
3249 (< ce (car c-state-cache)))
3250 (setcdr c-state-cache
3251 (cons new-cons (cdr c-state-cache))))
3252 (t (setq c-state-cache (cons new-cons c-state-cache)))))
3254 ;; We haven't found a brace pair. Record this in the cache.
3255 (setq c-state-brace-pair-desert
3256 (cons (if (and ce (< bra ce) (> ce here)) ; {..} straddling HERE?
3258 (point-min))
3259 (min here from)))))))))
3261 (defsubst c-state-push-any-brace-pair (bra+1 macro-start-or-here)
3262 ;; If BRA+1 is nil, do nothing. Otherwise, BRA+1 is the buffer position
3263 ;; following a {, and that brace has a (mis-)matching } (or ]), and we
3264 ;; "push" "a" brace pair onto `c-state-cache'.
3266 ;; Here "push" means overwrite the top element if it's itself a brace-pair,
3267 ;; otherwise push it normally.
3269 ;; The brace pair we push is normally the one surrounding BRA+1, but if the
3270 ;; latter is inside a macro, not being a macro containing
3271 ;; MACRO-START-OR-HERE, we scan backwards through the buffer for a non-macro
3272 ;; base pair. This latter case is assumed to be rare.
3274 ;; Note: POINT is not preserved in this routine.
3275 (if bra+1
3276 (if (or (> bra+1 macro-start-or-here)
3277 (progn (goto-char bra+1)
3278 (not (c-beginning-of-macro))))
3279 (setq c-state-cache
3280 (cons (cons (1- bra+1)
3281 (c-sc-scan-lists bra+1 1 1))
3282 (if (consp (car c-state-cache))
3283 (cdr c-state-cache)
3284 c-state-cache)))
3285 ;; N.B. This defsubst codes one method for the simple, normal case,
3286 ;; and a more sophisticated, slower way for the general case. Don't
3287 ;; eliminate this defsubst - it's a speed optimization.
3288 (c-append-lower-brace-pair-to-state-cache (1- bra+1) (point-max)))))
3290 (defun c-append-to-state-cache (from here)
3291 ;; Scan the buffer from FROM to HERE, adding elements into `c-state-cache'
3292 ;; for braces etc. Return a candidate for `c-state-cache-good-pos'.
3294 ;; FROM must be after the latest brace/paren/bracket in `c-state-cache', if
3295 ;; any. Typically, it is immediately after it. It must not be inside a
3296 ;; literal.
3297 (let ((here-bol (c-point 'bol here))
3298 (macro-start-or-here
3299 (save-excursion (goto-char here)
3300 (if (c-beginning-of-macro)
3301 (point)
3302 here)))
3303 pa+1 ; pos just after an opening PAren (or brace).
3304 (ren+1 from) ; usually a pos just after an closing paREN etc.
3305 ; Is actually the pos. to scan for a (/{/[ from,
3306 ; which sometimes is after a silly )/}/].
3307 paren+1 ; Pos after some opening or closing paren.
3308 paren+1s ; A list of `paren+1's; used to determine a
3309 ; good-pos.
3310 bra+1 ; just after L bra-ce.
3311 mstart) ; start of a macro.
3313 (save-excursion
3314 (save-restriction
3315 (narrow-to-region (point-min) here)
3316 ;; Each time round the following loop, we enter a successively deeper
3317 ;; level of brace/paren nesting. (Except sometimes we "continue at
3318 ;; the existing level".) `pa+1' is a pos inside an opening
3319 ;; brace/paren/bracket, usually just after it.
3320 (while
3321 (progn
3322 ;; Each time round the next loop moves forward over an opening then
3323 ;; a closing brace/bracket/paren. This loop is white hot, so it
3324 ;; plays ugly tricks to go fast. DON'T PUT ANYTHING INTO THIS
3325 ;; LOOP WHICH ISN'T ABSOLUTELY NECESSARY!!! It terminates when a
3326 ;; call of `scan-lists' signals an error, which happens when there
3327 ;; are no more b/b/p's to scan.
3328 (c-safe
3329 (while t
3330 (setq pa+1 (c-sc-scan-lists ren+1 1 -1) ; Into (/{/[; might signal
3331 paren+1s (cons pa+1 paren+1s))
3332 (setq ren+1 (c-sc-scan-lists pa+1 1 1)) ; Out of )/}/]; might signal
3333 (if (and (eq (char-before pa+1) ?{)) ; Check for a macro later.
3334 (setq bra+1 pa+1))
3335 (setcar paren+1s ren+1)))
3337 (if (and pa+1 (> pa+1 ren+1))
3338 ;; We've just entered a deeper nesting level.
3339 (progn
3340 ;; Insert the brace pair (if present) and the single open
3341 ;; paren/brace/bracket into `c-state-cache' It cannot be
3342 ;; inside a macro, except one around point, because of what
3343 ;; `c-neutralize-syntax-in-CPP' has done.
3344 (c-state-push-any-brace-pair bra+1 macro-start-or-here)
3345 ;; Insert the opening brace/bracket/paren position.
3346 (setq c-state-cache (cons (1- pa+1) c-state-cache))
3347 ;; Clear admin stuff for the next more nested part of the scan.
3348 (setq ren+1 pa+1 pa+1 nil bra+1 nil)
3349 t) ; Carry on the loop
3351 ;; All open p/b/b's at this nesting level, if any, have probably
3352 ;; been closed by matching/mismatching ones. We're probably
3353 ;; finished - we just need to check for having found an
3354 ;; unmatched )/}/], which we ignore. Such a )/}/] can't be in a
3355 ;; macro, due the action of `c-neutralize-syntax-in-CPP'.
3356 (c-safe (setq ren+1 (c-sc-scan-lists ren+1 1 1)))))) ; acts as loop control.
3358 ;; Record the final, innermost, brace-pair if there is one.
3359 (c-state-push-any-brace-pair bra+1 macro-start-or-here)
3361 ;; Determine a good pos
3362 (while (and (setq paren+1 (car paren+1s))
3363 (> (if (> paren+1 macro-start-or-here)
3364 paren+1
3365 (goto-char paren+1)
3366 (setq mstart (and (c-beginning-of-macro)
3367 (point)))
3368 (or mstart paren+1))
3369 here-bol))
3370 (setq paren+1s (cdr paren+1s)))
3371 (cond
3372 ((and paren+1 mstart)
3373 (min paren+1 mstart))
3374 (paren+1)
3375 (t from))))))
3377 (defun c-remove-stale-state-cache (start-point here pps-point)
3378 ;; Remove stale entries from the `c-cache-state', i.e. those which will
3379 ;; not be in it when it is amended for position HERE. This may involve
3380 ;; replacing a CONS element for a brace pair containing HERE with its car.
3381 ;; Additionally, the "outermost" open-brace entry before HERE will be
3382 ;; converted to a cons if the matching close-brace is below HERE.
3384 ;; START-POINT is a "maximal" "safe position" - there must be no open
3385 ;; parens/braces/brackets between START-POINT and HERE.
3387 ;; As a second thing, calculate the result of parse-partial-sexp at
3388 ;; PPS-POINT, w.r.t. START-POINT. The motivation here is that
3389 ;; `c-state-cache-good-pos' may become PPS-POINT, but the caller may need to
3390 ;; adjust it to get outside a string/comment. (Sorry about this! The code
3391 ;; needs to be FAST).
3393 ;; Return a list (GOOD-POS SCAN-BACK-POS CONS-SEPARATED PPS-STATE), where
3394 ;; o - GOOD-POS is a position where the new value `c-state-cache' is known
3395 ;; to be good (we aim for this to be as high as possible);
3396 ;; o - SCAN-BACK-POS, if not nil, indicates there may be a brace pair
3397 ;; preceding POS which needs to be recorded in `c-state-cache'. It is a
3398 ;; position to scan backwards from. It is the position of the "{" of the
3399 ;; last element to be removed from `c-state-cache', when that elt is a
3400 ;; cons, otherwise nil.
3401 ;; o - CONS-SEPARATED is t when a cons element in `c-state-cache' has been
3402 ;; replaced by its car because HERE lies inside the brace pair represented
3403 ;; by the cons.
3404 ;; o - PPS-STATE is the parse-partial-sexp state at PPS-POINT.
3405 (save-excursion
3406 (save-restriction
3407 (narrow-to-region 1 (point-max))
3408 (let* ((in-macro-start ; start of macro containing HERE or nil.
3409 (save-excursion
3410 (goto-char here)
3411 (and (c-beginning-of-macro)
3412 (point))))
3413 (start-point-actual-macro-start ; Start of macro containing
3414 ; start-point or nil
3415 (and (< start-point here)
3416 (save-excursion
3417 (goto-char start-point)
3418 (and (c-beginning-of-macro)
3419 (point)))))
3420 (start-point-actual-macro-end ; End of this macro, (maybe
3421 ; HERE), or nil.
3422 (and start-point-actual-macro-start
3423 (save-excursion
3424 (goto-char start-point-actual-macro-start)
3425 (c-end-of-macro)
3426 (point))))
3427 pps-state ; Will be 9 or 10 elements long.
3429 upper-lim ; ,beyond which `c-state-cache' entries are removed
3430 scan-back-pos
3431 cons-separated
3432 pair-beg target-depth)
3434 ;; Remove entries beyond HERE. Also remove any entries inside
3435 ;; a macro, unless HERE is in the same macro.
3436 (setq upper-lim
3437 (if (or (null c-state-old-cpp-beg)
3438 (and (> here c-state-old-cpp-beg)
3439 (< here c-state-old-cpp-end)))
3440 here
3441 (min here c-state-old-cpp-beg)))
3442 (while (and c-state-cache (>= (c-state-cache-top-lparen) upper-lim))
3443 (setq scan-back-pos (car-safe (car c-state-cache)))
3444 (setq c-state-cache (cdr c-state-cache)))
3446 ;; If `upper-lim' is inside the last recorded brace pair, remove its
3447 ;; RBrace and indicate we'll need to search backwards for a previous
3448 ;; brace pair.
3449 (when (and c-state-cache
3450 (consp (car c-state-cache))
3451 (> (cdar c-state-cache) upper-lim))
3452 (setcar c-state-cache (caar c-state-cache))
3453 (setq scan-back-pos (car c-state-cache)
3454 cons-separated t))
3456 ;; The next loop jumps forward out of a nested level of parens each
3457 ;; time round; the corresponding elements in `c-state-cache' are
3458 ;; removed. `pos' is just after the brace-pair or the open paren at
3459 ;; (car c-state-cache). There can be no open parens/braces/brackets
3460 ;; between `start-point'/`start-point-actual-macro-start' and HERE,
3461 ;; due to the interface spec to this function.
3462 (setq pos (if (and start-point-actual-macro-end
3463 (not (eq start-point-actual-macro-start
3464 in-macro-start)))
3465 (1+ start-point-actual-macro-end) ; get outside the macro as
3466 ; marked by a `category' text property.
3467 start-point))
3468 (goto-char pos)
3469 (while (and c-state-cache
3470 (or (numberp (car c-state-cache)) ; Have we a { at all?
3471 (cdr c-state-cache))
3472 (< (point) here))
3473 (cond
3474 ((null pps-state) ; first time through
3475 (setq target-depth -1))
3476 ((eq (car pps-state) target-depth) ; found closing ),},]
3477 (setq target-depth (1- (car pps-state))))
3478 ;; Do nothing when we've merely reached pps-point.
3481 ;; Scan!
3482 (setq pps-state
3483 (c-sc-parse-partial-sexp
3484 (point) (if (< (point) pps-point) pps-point here)
3485 target-depth
3486 nil pps-state))
3488 (when (eq (car pps-state) target-depth)
3489 (setq pos (point)) ; POS is now just after an R-paren/brace.
3490 (cond
3491 ((and (consp (car c-state-cache))
3492 (eq (point) (cdar c-state-cache)))
3493 ;; We've just moved out of the paren pair containing the brace-pair
3494 ;; at (car c-state-cache). `pair-beg' is where the open paren is,
3495 ;; and is potentially where the open brace of a cons in
3496 ;; c-state-cache will be.
3497 (setq pair-beg (car-safe (cdr c-state-cache))
3498 c-state-cache (cdr-safe (cdr c-state-cache)))) ; remove {}pair + containing Lparen.
3499 ((numberp (car c-state-cache))
3500 (setq pair-beg (car c-state-cache)
3501 c-state-cache (cdr c-state-cache))) ; remove this
3502 ; containing Lparen
3503 ((numberp (cadr c-state-cache))
3504 (setq pair-beg (cadr c-state-cache)
3505 c-state-cache (cddr c-state-cache))) ; Remove a paren pair
3506 ; together with enclosed brace pair.
3507 ;; (t nil) ; Ignore an unmated Rparen.
3510 (if (< (point) pps-point)
3511 (setq pps-state (c-sc-parse-partial-sexp
3512 (point) pps-point
3513 nil nil ; TARGETDEPTH, STOPBEFORE
3514 pps-state)))
3516 ;; If the last paren pair we moved out of was actually a brace pair,
3517 ;; insert it into `c-state-cache'.
3518 (when (and pair-beg (eq (char-after pair-beg) ?{))
3519 (if (consp (car-safe c-state-cache))
3520 (setq c-state-cache (cdr c-state-cache)))
3521 (setq c-state-cache (cons (cons pair-beg pos)
3522 c-state-cache)))
3524 (list pos scan-back-pos cons-separated pps-state)))))
3526 (defun c-remove-stale-state-cache-backwards (here)
3527 ;; Strip stale elements of `c-state-cache' by moving backwards through the
3528 ;; buffer, and inform the caller of the scenario detected.
3530 ;; HERE is the position we're setting `c-state-cache' for.
3531 ;; CACHE-POS (a locally bound variable) is just after the latest recorded
3532 ;; position in `c-state-cache' before HERE, or a position at or near
3533 ;; point-min which isn't in a literal.
3535 ;; This function must only be called only when (> `c-state-cache-good-pos'
3536 ;; HERE). Usually the gap between CACHE-POS and HERE is large. It is thus
3537 ;; optimized to eliminate (or minimize) scanning between these two
3538 ;; positions.
3540 ;; Return a three element list (GOOD-POS SCAN-BACK-POS FWD-FLAG), where:
3541 ;; o - GOOD-POS is a "good position", where `c-state-cache' is valid, or
3542 ;; could become so after missing elements are inserted into
3543 ;; `c-state-cache'. This is JUST AFTER an opening or closing
3544 ;; brace/paren/bracket which is already in `c-state-cache' or just before
3545 ;; one otherwise. exceptionally (when there's no such b/p/b handy) the BOL
3546 ;; before `here''s line, or the start of the literal containing it.
3547 ;; o - SCAN-BACK-POS, if non-nil, indicates there may be a brace pair
3548 ;; preceding POS which isn't recorded in `c-state-cache'. It is a position
3549 ;; to scan backwards from.
3550 ;; o - FWD-FLAG, if non-nil, indicates there may be parens/braces between
3551 ;; POS and HERE which aren't recorded in `c-state-cache'.
3553 ;; The comments in this defun use "paren" to mean parenthesis or square
3554 ;; bracket (as contrasted with a brace), and "(" and ")" likewise.
3556 ;; . {..} (..) (..) ( .. { } ) (...) ( .... . ..)
3557 ;; | | | | | |
3558 ;; CP E here D C good
3559 (let ((cache-pos (c-get-cache-scan-pos here)) ; highest position below HERE in cache (or 1)
3560 (pos c-state-cache-good-pos)
3561 pa ren ; positions of "(" and ")"
3562 dropped-cons ; whether the last element dropped from `c-state-cache'
3563 ; was a cons (representing a brace-pair)
3564 good-pos ; see above.
3565 lit ; (START . END) of a literal containing some point.
3566 here-lit-start here-lit-end ; bounds of literal containing `here'
3567 ; or `here' itself.
3568 here- here+ ; start/end of macro around HERE, or HERE
3569 (here-bol (c-point 'bol here))
3570 (too-far-back (max (- here c-state-cache-too-far) (point-min))))
3572 ;; Remove completely irrelevant entries from `c-state-cache'.
3573 (while (and c-state-cache
3574 (>= (setq pa (c-state-cache-top-lparen)) here))
3575 (setq dropped-cons (consp (car c-state-cache)))
3576 (setq c-state-cache (cdr c-state-cache))
3577 (setq pos pa))
3578 ;; At this stage, (>= pos here);
3579 ;; (< (c-state-cache-top-lparen) here) (or is nil).
3581 (cond
3582 ((and (consp (car c-state-cache))
3583 (> (cdar c-state-cache) here))
3584 ;; CASE 1: The top of the cache is a brace pair which now encloses
3585 ;; `here'. As good-pos, return the address of the "{". Since we've no
3586 ;; knowledge of what's inside these braces, we have no alternative but
3587 ;; to direct the caller to scan the buffer from the opening brace.
3588 (setq pos (caar c-state-cache))
3589 (setcar c-state-cache pos)
3590 (list (1+ pos) pos t)) ; return value. We've just converted a brace pair
3591 ; entry into a { entry, so the caller needs to
3592 ; search for a brace pair before the {.
3594 ;; `here' might be inside a literal. Check for this.
3595 ((progn
3596 (setq lit (c-state-literal-at here)
3597 here-lit-start (or (car lit) here)
3598 here-lit-end (or (cdr lit) here))
3599 ;; Has `here' just "newly entered" a macro?
3600 (save-excursion
3601 (goto-char here-lit-start)
3602 (if (and (c-beginning-of-macro)
3603 (or (null c-state-old-cpp-beg)
3604 (not (= (point) c-state-old-cpp-beg))))
3605 (progn
3606 (setq here- (point))
3607 (c-end-of-macro)
3608 (setq here+ (point)))
3609 (setq here- here-lit-start
3610 here+ here-lit-end)))
3612 ;; `here' might be nested inside any depth of parens (or brackets but
3613 ;; not braces). Scan backwards to find the outermost such opening
3614 ;; paren, if there is one. This will be the scan position to return.
3615 (save-restriction
3616 (narrow-to-region cache-pos (point-max))
3617 (setq pos (c-state-balance-parens-backwards here- here+ pos)))
3618 nil)) ; for the cond
3620 ((< pos here-lit-start)
3621 ;; CASE 2: Address of outermost ( or [ which now encloses `here', but
3622 ;; didn't enclose the (previous) `c-state-cache-good-pos'. If there is
3623 ;; a brace pair preceding this, it will already be in `c-state-cache',
3624 ;; unless there was a brace pair after it, i.e. there'll only be one to
3625 ;; scan for if we've just deleted one.
3626 (list pos (and dropped-cons pos) t)) ; Return value.
3628 ;; `here' isn't enclosed in a (previously unrecorded) bracket/paren.
3629 ;; Further forward scanning isn't needed, but we still need to find a
3630 ;; GOOD-POS. Step out of all enclosing "("s on HERE's line.
3631 ((progn
3632 (save-restriction
3633 (narrow-to-region here-bol (point-max))
3634 (setq pos here-lit-start)
3635 (c-safe (while (setq pa (c-sc-scan-lists pos -1 1))
3636 (setq pos pa)))) ; might signal
3637 nil)) ; for the cond
3639 ((save-restriction
3640 (narrow-to-region too-far-back (point-max))
3641 (setq ren (c-safe (c-sc-scan-lists pos -1 -1))))
3642 ;; CASE 3: After a }/)/] before `here''s BOL.
3643 (list (1+ ren) (and dropped-cons pos) nil)) ; Return value
3645 ((progn (setq good-pos (c-state-lit-beg (c-point 'bopl here-bol)))
3646 (>= cache-pos good-pos))
3647 ;; CASE 3.5: Just after an existing entry in `c-state-cache' on `here''s
3648 ;; line or the previous line.
3649 (list cache-pos nil nil))
3652 ;; CASE 4; Best of a bad job: BOL before `here-bol', or beginning of
3653 ;; literal containing it.
3654 (list good-pos (and dropped-cons good-pos) nil)))))
3657 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
3658 ;; Externally visible routines.
3660 (defun c-state-cache-init ()
3661 (setq c-state-cache nil
3662 c-state-cache-good-pos 1
3663 c-state-nonlit-pos-cache nil
3664 c-state-nonlit-pos-cache-limit 1
3665 c-state-semi-nonlit-pos-cache nil
3666 c-state-semi-nonlit-pos-cache-limit 1
3667 c-state-brace-pair-desert nil
3668 c-state-point-min 1
3669 c-state-point-min-lit-type nil
3670 c-state-point-min-lit-start nil
3671 c-state-min-scan-pos 1
3672 c-state-old-cpp-beg nil
3673 c-state-old-cpp-end nil)
3674 (c-state-mark-point-min-literal))
3676 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
3677 ;; Debugging routines to dump `c-state-cache' in a "replayable" form.
3678 ;; (defmacro c-sc-de (elt) ; "c-state-cache-dump-element"
3679 ;; `(format ,(concat "(setq " (symbol-name elt) " %s) ") ,elt))
3680 ;; (defmacro c-sc-qde (elt) ; "c-state-cache-quote-dump-element"
3681 ;; `(format ,(concat "(setq " (symbol-name elt) " '%s) ") ,elt))
3682 ;; (defun c-state-dump ()
3683 ;; ;; For debugging.
3684 ;; ;(message
3685 ;; (concat
3686 ;; (c-sc-qde c-state-cache)
3687 ;; (c-sc-de c-state-cache-good-pos)
3688 ;; (c-sc-qde c-state-nonlit-pos-cache)
3689 ;; (c-sc-de c-state-nonlit-pos-cache-limit)
3690 ;; (c-sc-qde c-state-brace-pair-desert)
3691 ;; (c-sc-de c-state-point-min)
3692 ;; (c-sc-de c-state-point-min-lit-type)
3693 ;; (c-sc-de c-state-point-min-lit-start)
3694 ;; (c-sc-de c-state-min-scan-pos)
3695 ;; (c-sc-de c-state-old-cpp-beg)
3696 ;; (c-sc-de c-state-old-cpp-end)))
3697 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
3699 (defun c-invalidate-state-cache-1 (here)
3700 ;; Invalidate all info on `c-state-cache' that applies to the buffer at HERE
3701 ;; or higher and set `c-state-cache-good-pos' accordingly. The cache is
3702 ;; left in a consistent state.
3704 ;; This is much like `c-whack-state-after', but it never changes a paren
3705 ;; pair element into an open paren element. Doing that would mean that the
3706 ;; new open paren wouldn't have the required preceding paren pair element.
3708 ;; This function is called from c-before-change.
3710 ;; The caches of non-literals:
3711 ;; Note that we use "<=" for the possibility of the second char of a two-char
3712 ;; comment opener being typed; this would invalidate any cache position at
3713 ;; HERE.
3714 (if (<= here c-state-nonlit-pos-cache-limit)
3715 (setq c-state-nonlit-pos-cache-limit (1- here)))
3716 (c-truncate-semi-nonlit-pos-cache here)
3718 ;; `c-state-cache':
3719 ;; Case 1: if `here' is in a literal containing point-min, everything
3720 ;; becomes (or is already) nil.
3721 (if (or (null c-state-cache-good-pos)
3722 (< here (c-state-get-min-scan-pos)))
3723 (setq c-state-cache nil
3724 c-state-cache-good-pos nil
3725 c-state-min-scan-pos nil)
3727 ;; Truncate `c-state-cache' and set `c-state-cache-good-pos' to a value
3728 ;; below `here'. To maintain its consistency, we may need to insert a new
3729 ;; brace pair.
3730 (let ((here-bol (c-point 'bol here))
3731 too-high-pa ; recorded {/(/[ next above or just below here, or nil.
3732 dropped-cons) ; was the last removed element a brace pair?
3733 ;; The easy bit - knock over-the-top bits off `c-state-cache'.
3734 (while (and c-state-cache
3735 (>= (c-state-cache-top-paren) here))
3736 (setq dropped-cons (consp (car c-state-cache))
3737 too-high-pa (c-state-cache-top-lparen)
3738 c-state-cache (cdr c-state-cache)))
3740 ;; Do we need to add in an earlier brace pair, having lopped one off?
3741 (if (and dropped-cons
3742 (<= too-high-pa here))
3743 (c-append-lower-brace-pair-to-state-cache too-high-pa here here-bol))
3744 (setq c-state-cache-good-pos (or (c-state-cache-after-top-paren)
3745 (c-state-get-min-scan-pos)))))
3747 ;; The brace-pair desert marker:
3748 (when (car c-state-brace-pair-desert)
3749 (if (< here (car c-state-brace-pair-desert))
3750 (setq c-state-brace-pair-desert nil)
3751 (if (< here (cdr c-state-brace-pair-desert))
3752 (setcdr c-state-brace-pair-desert here)))))
3754 (defun c-parse-state-1 ()
3755 ;; Find and record all noteworthy parens between some good point earlier in
3756 ;; the file and point. That good point is at least the beginning of the
3757 ;; top-level construct we are in, or the beginning of the preceding
3758 ;; top-level construct if we aren't in one.
3760 ;; The returned value is a list of the noteworthy parens with the last one
3761 ;; first. If an element in the list is an integer, it's the position of an
3762 ;; open paren (of any type) which has not been closed before the point. If
3763 ;; an element is a cons, it gives the position of a closed BRACE paren
3764 ;; pair[*]; the car is the start brace position and the cdr is the position
3765 ;; following the closing brace. Only the last closed brace paren pair
3766 ;; before each open paren and before the point is recorded, and thus the
3767 ;; state never contains two cons elements in succession. When a close brace
3768 ;; has no matching open brace (e.g., the matching brace is outside the
3769 ;; visible region), it is not represented in the returned value.
3771 ;; [*] N.B. The close "brace" might be a mismatching close bracket or paren.
3772 ;; This defun explicitly treats mismatching parens/braces/brackets as
3773 ;; matching. It is the open brace which makes it a "brace" pair.
3775 ;; If POINT is within a macro, open parens and brace pairs within
3776 ;; THIS macro MIGHT be recorded. This depends on whether their
3777 ;; syntactic properties have been suppressed by
3778 ;; `c-neutralize-syntax-in-CPP'. This might need fixing (2008-12-11).
3780 ;; Currently no characters which are given paren syntax with the
3781 ;; syntax-table property are recorded, i.e. angle bracket arglist
3782 ;; parens are never present here. Note that this might change.
3784 ;; BUG: This function doesn't cope entirely well with unbalanced
3785 ;; parens in macros. (2008-12-11: this has probably been resolved
3786 ;; by the function `c-neutralize-syntax-in-CPP'.) E.g. in the
3787 ;; following case the brace before the macro isn't balanced with the
3788 ;; one after it:
3790 ;; {
3791 ;; #define X {
3792 ;; }
3794 ;; Note to maintainers: this function DOES get called with point
3795 ;; within comments and strings, so don't assume it doesn't!
3797 ;; This function might do hidden buffer changes.
3798 (let* ((here (point))
3799 (here-bopl (c-point 'bopl))
3800 strategy ; 'forward, 'backward etc..
3801 ;; Candidate positions to start scanning from:
3802 cache-pos ; highest position below HERE already existing in
3803 ; cache (or 1).
3804 good-pos
3805 start-point ; (when scanning forward) a place below HERE where there
3806 ; are no open parens/braces between it and HERE.
3807 bopl-state
3809 cons-separated
3810 scan-backward-pos scan-forward-p) ; used for 'backward.
3811 ;; If POINT-MIN has changed, adjust the cache
3812 (unless (= (point-min) c-state-point-min)
3813 (c-renarrow-state-cache))
3815 ;; Strategy?
3816 (setq res (c-parse-state-get-strategy here c-state-cache-good-pos)
3817 strategy (car res)
3818 start-point (cadr res))
3820 (when (eq strategy 'BOD)
3821 (setq c-state-cache nil
3822 c-state-cache-good-pos start-point))
3824 ;; SCAN!
3825 (cond
3826 ((memq strategy '(forward back-and-forward BOD))
3827 (setq res (c-remove-stale-state-cache start-point here here-bopl))
3828 (setq cache-pos (car res)
3829 scan-backward-pos (cadr res)
3830 cons-separated (car (cddr res))
3831 bopl-state (cadr (cddr res))) ; will be nil if (< here-bopl
3832 ; start-point)
3833 (if (and scan-backward-pos
3834 (or cons-separated (eq strategy 'forward))) ;scan-backward-pos
3835 (c-append-lower-brace-pair-to-state-cache scan-backward-pos here))
3836 (setq good-pos
3837 (c-append-to-state-cache cache-pos here))
3838 (setq c-state-cache-good-pos
3839 (if (and bopl-state
3840 (< good-pos (- here c-state-cache-too-far)))
3841 (c-state-cache-non-literal-place here-bopl bopl-state)
3842 good-pos)))
3844 ((eq strategy 'backward)
3845 (setq res (c-remove-stale-state-cache-backwards here)
3846 good-pos (car res)
3847 scan-backward-pos (cadr res)
3848 scan-forward-p (car (cddr res)))
3849 (if scan-backward-pos
3850 (c-append-lower-brace-pair-to-state-cache scan-backward-pos here))
3851 (setq c-state-cache-good-pos
3852 (if scan-forward-p
3853 (c-append-to-state-cache good-pos here)
3854 good-pos)))
3856 (t ; (eq strategy 'IN-LIT)
3857 (setq c-state-cache nil
3858 c-state-cache-good-pos nil))))
3860 c-state-cache)
3862 (defun c-invalidate-state-cache (here)
3863 ;; This is a wrapper over `c-invalidate-state-cache-1'.
3865 ;; It suppresses the syntactic effect of the < and > (template) brackets and
3866 ;; of all parens in preprocessor constructs, except for any such construct
3867 ;; containing point. We can then call `c-invalidate-state-cache-1' without
3868 ;; worrying further about macros and template delimiters.
3869 (if (eval-when-compile (memq 'category-properties c-emacs-features))
3870 ;; Emacs
3871 (c-with-<->-as-parens-suppressed
3872 (if (and c-state-old-cpp-beg
3873 (< c-state-old-cpp-beg here))
3874 (c-with-all-but-one-cpps-commented-out
3875 c-state-old-cpp-beg
3876 c-state-old-cpp-end
3877 (c-invalidate-state-cache-1 here))
3878 (c-with-cpps-commented-out
3879 (c-invalidate-state-cache-1 here))))
3880 ;; XEmacs
3881 (c-invalidate-state-cache-1 here)))
3883 (defmacro c-state-maybe-marker (place marker)
3884 ;; If PLACE is non-nil, return a marker marking it, otherwise nil.
3885 ;; We (re)use MARKER.
3886 `(and ,place
3887 (or ,marker (setq ,marker (make-marker)))
3888 (set-marker ,marker ,place)))
3890 (defun c-parse-state ()
3891 ;; This is a wrapper over `c-parse-state-1'. See that function for a
3892 ;; description of the functionality and return value.
3894 ;; It suppresses the syntactic effect of the < and > (template) brackets and
3895 ;; of all parens in preprocessor constructs, except for any such construct
3896 ;; containing point. We can then call `c-parse-state-1' without worrying
3897 ;; further about macros and template delimiters.
3898 (let (here-cpp-beg here-cpp-end)
3899 (save-excursion
3900 (when (c-beginning-of-macro)
3901 (setq here-cpp-beg (point))
3902 (unless
3903 (> (setq here-cpp-end (c-syntactic-end-of-macro))
3904 here-cpp-beg)
3905 (setq here-cpp-beg nil here-cpp-end nil))))
3906 ;; FIXME!!! Put in a `condition-case' here to protect the integrity of the
3907 ;; subsystem.
3908 (prog1
3909 (if (eval-when-compile (memq 'category-properties c-emacs-features))
3910 ;; Emacs
3911 (c-with-<->-as-parens-suppressed
3912 (if (and here-cpp-beg (> here-cpp-end here-cpp-beg))
3913 (c-with-all-but-one-cpps-commented-out
3914 here-cpp-beg here-cpp-end
3915 (c-parse-state-1))
3916 (c-with-cpps-commented-out
3917 (c-parse-state-1))))
3918 ;; XEmacs
3919 (c-parse-state-1))
3920 (setq c-state-old-cpp-beg
3921 (c-state-maybe-marker here-cpp-beg c-state-old-cpp-beg-marker)
3922 c-state-old-cpp-end
3923 (c-state-maybe-marker here-cpp-end c-state-old-cpp-end-marker)))))
3925 ;; Debug tool to catch cache inconsistencies. This is called from
3926 ;; 000tests.el.
3927 (defvar c-debug-parse-state nil)
3928 (unless (fboundp 'c-real-parse-state)
3929 (fset 'c-real-parse-state (symbol-function 'c-parse-state)))
3930 (cc-bytecomp-defun c-real-parse-state)
3932 (defvar c-parse-state-point nil)
3933 (defvar c-parse-state-state nil)
3934 (make-variable-buffer-local 'c-parse-state-state)
3935 (defun c-record-parse-state-state ()
3936 (setq c-parse-state-point (point))
3937 (when (markerp (cdr (assq 'c-state-old-cpp-beg c-parse-state-state)))
3938 (move-marker (cdr (assq 'c-state-old-cpp-beg c-parse-state-state)) nil)
3939 (move-marker (cdr (assq 'c-state-old-cpp-end c-parse-state-state)) nil))
3940 (setq c-parse-state-state
3941 (mapcar
3942 (lambda (arg)
3943 (let ((val (symbol-value arg)))
3944 (cons arg
3945 (cond ((consp val) (copy-tree val))
3946 ((markerp val) (copy-marker val))
3947 (t val)))))
3948 '(c-state-cache
3949 c-state-cache-good-pos
3950 c-state-nonlit-pos-cache
3951 c-state-nonlit-pos-cache-limit
3952 c-state-semi-nonlit-pos-cache
3953 c-state-semi-nonlit-pos-cache-limit
3954 c-state-brace-pair-desert
3955 c-state-point-min
3956 c-state-point-min-lit-type
3957 c-state-point-min-lit-start
3958 c-state-min-scan-pos
3959 c-state-old-cpp-beg
3960 c-state-old-cpp-end
3961 c-parse-state-point))))
3962 (defun c-replay-parse-state-state ()
3963 (message "%s"
3964 (concat "(setq "
3965 (mapconcat
3966 (lambda (arg)
3967 (format "%s %s%s" (car arg)
3968 (if (atom (cdr arg)) "" "'")
3969 (if (markerp (cdr arg))
3970 (format "(copy-marker %s)" (marker-position (cdr arg)))
3971 (cdr arg))))
3972 c-parse-state-state " ")
3973 ")")))
3975 (defun c-debug-parse-state-double-cons (state)
3976 (let (state-car conses-not-ok)
3977 (while state
3978 (setq state-car (car state)
3979 state (cdr state))
3980 (if (and (consp state-car)
3981 (consp (car state)))
3982 (setq conses-not-ok t)))
3983 conses-not-ok))
3985 (defun c-debug-parse-state ()
3986 (let ((here (point)) (min-point (point-min)) (res1 (c-real-parse-state)) res2)
3987 (let ((c-state-cache nil)
3988 (c-state-cache-good-pos 1)
3989 (c-state-nonlit-pos-cache nil)
3990 (c-state-nonlit-pos-cache-limit 1)
3991 (c-state-brace-pair-desert nil)
3992 (c-state-point-min 1)
3993 (c-state-point-min-lit-type nil)
3994 (c-state-point-min-lit-start nil)
3995 (c-state-min-scan-pos 1)
3996 (c-state-old-cpp-beg nil)
3997 (c-state-old-cpp-end nil))
3998 (setq res2 (c-real-parse-state)))
3999 (unless (equal res1 res2)
4000 ;; The cache can actually go further back due to the ad-hoc way
4001 ;; the first paren is found, so try to whack off a bit of its
4002 ;; start before complaining.
4003 ;; (save-excursion
4004 ;; (goto-char (or (c-least-enclosing-brace res2) (point)))
4005 ;; (c-beginning-of-defun-1)
4006 ;; (while (not (or (bobp) (eq (char-after) ?{)))
4007 ;; (c-beginning-of-defun-1))
4008 ;; (unless (equal (c-whack-state-before (point) res1) res2)
4009 ;; (message (concat "c-parse-state inconsistency at %s: "
4010 ;; "using cache: %s, from scratch: %s")
4011 ;; here res1 res2)))
4012 (message (concat "c-parse-state inconsistency at %s: "
4013 "using cache: %s, from scratch: %s. POINT-MIN: %s")
4014 here res1 res2 min-point)
4015 (message "Old state:")
4016 (c-replay-parse-state-state))
4018 (when (c-debug-parse-state-double-cons res1)
4019 (message "c-parse-state INVALIDITY at %s: %s"
4020 here res1)
4021 (message "Old state:")
4022 (c-replay-parse-state-state))
4024 (c-record-parse-state-state)
4025 res2 ; res1 correct a cascading series of errors ASAP
4028 (defun c-toggle-parse-state-debug (&optional arg)
4029 (interactive "P")
4030 (setq c-debug-parse-state (c-calculate-state arg c-debug-parse-state))
4031 (fset 'c-parse-state (symbol-function (if c-debug-parse-state
4032 'c-debug-parse-state
4033 'c-real-parse-state)))
4034 (c-keep-region-active)
4035 (message "c-debug-parse-state %sabled"
4036 (if c-debug-parse-state "en" "dis")))
4037 (when c-debug-parse-state
4038 (c-toggle-parse-state-debug 1))
4041 (defun c-whack-state-before (bufpos paren-state)
4042 ;; Whack off any state information from PAREN-STATE which lies
4043 ;; before BUFPOS. Not destructive on PAREN-STATE.
4044 (let* ((newstate (list nil))
4045 (ptr newstate)
4046 car)
4047 (while paren-state
4048 (setq car (car paren-state)
4049 paren-state (cdr paren-state))
4050 (if (< (if (consp car) (car car) car) bufpos)
4051 (setq paren-state nil)
4052 (setcdr ptr (list car))
4053 (setq ptr (cdr ptr))))
4054 (cdr newstate)))
4056 (defun c-whack-state-after (bufpos paren-state)
4057 ;; Whack off any state information from PAREN-STATE which lies at or
4058 ;; after BUFPOS. Not destructive on PAREN-STATE.
4059 (catch 'done
4060 (while paren-state
4061 (let ((car (car paren-state)))
4062 (if (consp car)
4063 ;; just check the car, because in a balanced brace
4064 ;; expression, it must be impossible for the corresponding
4065 ;; close brace to be before point, but the open brace to
4066 ;; be after.
4067 (if (<= bufpos (car car))
4068 nil ; whack it off
4069 (if (< bufpos (cdr car))
4070 ;; its possible that the open brace is before
4071 ;; bufpos, but the close brace is after. In that
4072 ;; case, convert this to a non-cons element. The
4073 ;; rest of the state is before bufpos, so we're
4074 ;; done.
4075 (throw 'done (cons (car car) (cdr paren-state)))
4076 ;; we know that both the open and close braces are
4077 ;; before bufpos, so we also know that everything else
4078 ;; on state is before bufpos.
4079 (throw 'done paren-state)))
4080 (if (<= bufpos car)
4081 nil ; whack it off
4082 ;; it's before bufpos, so everything else should too.
4083 (throw 'done paren-state)))
4084 (setq paren-state (cdr paren-state)))
4085 nil)))
4087 (defun c-most-enclosing-brace (paren-state &optional bufpos)
4088 ;; Return the bufpos of the innermost enclosing open paren before
4089 ;; bufpos, or nil if none was found.
4090 (let (enclosingp)
4091 (or bufpos (setq bufpos 134217727))
4092 (while paren-state
4093 (setq enclosingp (car paren-state)
4094 paren-state (cdr paren-state))
4095 (if (or (consp enclosingp)
4096 (>= enclosingp bufpos))
4097 (setq enclosingp nil)
4098 (setq paren-state nil)))
4099 enclosingp))
4101 (defun c-least-enclosing-brace (paren-state)
4102 ;; Return the bufpos of the outermost enclosing open paren, or nil
4103 ;; if none was found.
4104 (let (pos elem)
4105 (while paren-state
4106 (setq elem (car paren-state)
4107 paren-state (cdr paren-state))
4108 (if (integerp elem)
4109 (setq pos elem)))
4110 pos))
4112 (defun c-safe-position (bufpos paren-state)
4113 ;; Return the closest "safe" position recorded on PAREN-STATE that
4114 ;; is higher up than BUFPOS. Return nil if PAREN-STATE doesn't
4115 ;; contain any. Return nil if BUFPOS is nil, which is useful to
4116 ;; find the closest limit before a given limit that might be nil.
4118 ;; A "safe" position is a position at or after a recorded open
4119 ;; paren, or after a recorded close paren. The returned position is
4120 ;; thus either the first position after a close brace, or the first
4121 ;; position after an enclosing paren, or at the enclosing paren in
4122 ;; case BUFPOS is immediately after it.
4123 (when bufpos
4124 (let (elem)
4125 (catch 'done
4126 (while paren-state
4127 (setq elem (car paren-state))
4128 (if (consp elem)
4129 (cond ((< (cdr elem) bufpos)
4130 (throw 'done (cdr elem)))
4131 ((< (car elem) bufpos)
4132 ;; See below.
4133 (throw 'done (min (1+ (car elem)) bufpos))))
4134 (if (< elem bufpos)
4135 ;; elem is the position at and not after the opening paren, so
4136 ;; we can go forward one more step unless it's equal to
4137 ;; bufpos. This is useful in some cases avoid an extra paren
4138 ;; level between the safe position and bufpos.
4139 (throw 'done (min (1+ elem) bufpos))))
4140 (setq paren-state (cdr paren-state)))))))
4142 (defun c-beginning-of-syntax ()
4143 ;; This is used for `font-lock-beginning-of-syntax-function'. It
4144 ;; goes to the closest previous point that is known to be outside
4145 ;; any string literal or comment. `c-state-cache' is used if it has
4146 ;; a position in the vicinity.
4147 (let* ((paren-state c-state-cache)
4148 elem
4150 (pos (catch 'done
4151 ;; Note: Similar code in `c-safe-position'. The
4152 ;; difference is that we accept a safe position at
4153 ;; the point and don't bother to go forward past open
4154 ;; parens.
4155 (while paren-state
4156 (setq elem (car paren-state))
4157 (if (consp elem)
4158 (cond ((<= (cdr elem) (point))
4159 (throw 'done (cdr elem)))
4160 ((<= (car elem) (point))
4161 (throw 'done (car elem))))
4162 (if (<= elem (point))
4163 (throw 'done elem)))
4164 (setq paren-state (cdr paren-state)))
4165 (point-min))))
4167 (if (> pos (- (point) 4000))
4168 (goto-char pos)
4169 ;; The position is far back. Try `c-beginning-of-defun-1'
4170 ;; (although we can't be entirely sure it will go to a position
4171 ;; outside a comment or string in current emacsen). FIXME:
4172 ;; Consult `syntax-ppss' here.
4173 (c-beginning-of-defun-1)
4174 (if (< (point) pos)
4175 (goto-char pos)))))
4178 ;; Tools for scanning identifiers and other tokens.
4180 (defun c-on-identifier ()
4181 "Return non-nil if the point is on or directly after an identifier.
4182 Keywords are recognized and not considered identifiers. If an
4183 identifier is detected, the returned value is its starting position.
4184 If an identifier ends at the point and another begins at it \(can only
4185 happen in Pike) then the point for the preceding one is returned.
4187 Note that this function might do hidden buffer changes. See the
4188 comment at the start of cc-engine.el for more info."
4190 ;; FIXME: Shouldn't this function handle "operator" in C++?
4192 (save-excursion
4193 (skip-syntax-backward "w_")
4197 ;; Check for a normal (non-keyword) identifier.
4198 (and (looking-at c-symbol-start)
4199 (not (looking-at c-keywords-regexp))
4200 (point))
4202 (when (c-major-mode-is 'pike-mode)
4203 ;; Handle the `<operator> syntax in Pike.
4204 (let ((pos (point)))
4205 (skip-chars-backward "-!%&*+/<=>^|~[]()")
4206 (and (if (< (skip-chars-backward "`") 0)
4208 (goto-char pos)
4209 (eq (char-after) ?\`))
4210 (looking-at c-symbol-key)
4211 (>= (match-end 0) pos)
4212 (point))))
4214 ;; Handle the "operator +" syntax in C++.
4215 (when (and c-overloadable-operators-regexp
4216 (= (c-backward-token-2 0) 0))
4218 (cond ((and (looking-at c-overloadable-operators-regexp)
4219 (or (not c-opt-op-identifier-prefix)
4220 (and (= (c-backward-token-2 1) 0)
4221 (looking-at c-opt-op-identifier-prefix))))
4222 (point))
4224 ((save-excursion
4225 (and c-opt-op-identifier-prefix
4226 (looking-at c-opt-op-identifier-prefix)
4227 (= (c-forward-token-2 1) 0)
4228 (looking-at c-overloadable-operators-regexp)))
4229 (point))))
4233 (defsubst c-simple-skip-symbol-backward ()
4234 ;; If the point is at the end of a symbol then skip backward to the
4235 ;; beginning of it. Don't move otherwise. Return non-nil if point
4236 ;; moved.
4238 ;; This function might do hidden buffer changes.
4239 (or (< (skip-syntax-backward "w_") 0)
4240 (and (c-major-mode-is 'pike-mode)
4241 ;; Handle the `<operator> syntax in Pike.
4242 (let ((pos (point)))
4243 (if (and (< (skip-chars-backward "-!%&*+/<=>^|~[]()") 0)
4244 (< (skip-chars-backward "`") 0)
4245 (looking-at c-symbol-key)
4246 (>= (match-end 0) pos))
4248 (goto-char pos)
4249 nil)))))
4251 (defun c-beginning-of-current-token (&optional back-limit)
4252 ;; Move to the beginning of the current token. Do not move if not
4253 ;; in the middle of one. BACK-LIMIT may be used to bound the
4254 ;; backward search; if given it's assumed to be at the boundary
4255 ;; between two tokens. Return non-nil if the point is moved, nil
4256 ;; otherwise.
4258 ;; This function might do hidden buffer changes.
4259 (let ((start (point)))
4260 (if (looking-at "\\w\\|\\s_")
4261 (skip-syntax-backward "w_" back-limit)
4262 (when (< (skip-syntax-backward ".()" back-limit) 0)
4263 (while (let ((pos (or (and (looking-at c-nonsymbol-token-regexp)
4264 (match-end 0))
4265 ;; `c-nonsymbol-token-regexp' should always match
4266 ;; since we've skipped backward over punctuation
4267 ;; or paren syntax, but consume one char in case
4268 ;; it doesn't so that we don't leave point before
4269 ;; some earlier incorrect token.
4270 (1+ (point)))))
4271 (if (<= pos start)
4272 (goto-char pos))))))
4273 (< (point) start)))
4275 (defun c-end-of-current-token (&optional back-limit)
4276 ;; Move to the end of the current token. Do not move if not in the
4277 ;; middle of one. BACK-LIMIT may be used to bound the backward
4278 ;; search; if given it's assumed to be at the boundary between two
4279 ;; tokens. Return non-nil if the point is moved, nil otherwise.
4281 ;; This function might do hidden buffer changes.
4282 (let ((start (point)))
4283 (cond ((< (skip-syntax-backward "w_" (1- start)) 0)
4284 (skip-syntax-forward "w_"))
4285 ((< (skip-syntax-backward ".()" back-limit) 0)
4286 (while (progn
4287 (if (looking-at c-nonsymbol-token-regexp)
4288 (goto-char (match-end 0))
4289 ;; `c-nonsymbol-token-regexp' should always match since
4290 ;; we've skipped backward over punctuation or paren
4291 ;; syntax, but move forward in case it doesn't so that
4292 ;; we don't leave point earlier than we started with.
4293 (forward-char))
4294 (< (point) start)))))
4295 (> (point) start)))
4297 (defconst c-jump-syntax-balanced
4298 (if (memq 'gen-string-delim c-emacs-features)
4299 "\\w\\|\\s_\\|\\s(\\|\\s)\\|\\s\"\\|\\s|"
4300 "\\w\\|\\s_\\|\\s(\\|\\s)\\|\\s\""))
4302 (defconst c-jump-syntax-unbalanced
4303 (if (memq 'gen-string-delim c-emacs-features)
4304 "\\w\\|\\s_\\|\\s\"\\|\\s|"
4305 "\\w\\|\\s_\\|\\s\""))
4307 (defun c-forward-token-2 (&optional count balanced limit)
4308 "Move forward by tokens.
4309 A token is defined as all symbols and identifiers which aren't
4310 syntactic whitespace \(note that multicharacter tokens like \"==\" are
4311 treated properly). Point is always either left at the beginning of a
4312 token or not moved at all. COUNT specifies the number of tokens to
4313 move; a negative COUNT moves in the opposite direction. A COUNT of 0
4314 moves to the next token beginning only if not already at one. If
4315 BALANCED is true, move over balanced parens, otherwise move into them.
4316 Also, if BALANCED is true, never move out of an enclosing paren.
4318 LIMIT sets the limit for the movement and defaults to the point limit.
4319 The case when LIMIT is set in the middle of a token, comment or macro
4320 is handled correctly, i.e. the point won't be left there.
4322 Return the number of tokens left to move \(positive or negative). If
4323 BALANCED is true, a move over a balanced paren counts as one. Note
4324 that if COUNT is 0 and no appropriate token beginning is found, 1 will
4325 be returned. Thus, a return value of 0 guarantees that point is at
4326 the requested position and a return value less \(without signs) than
4327 COUNT guarantees that point is at the beginning of some token.
4329 Note that this function might do hidden buffer changes. See the
4330 comment at the start of cc-engine.el for more info."
4332 (or count (setq count 1))
4333 (if (< count 0)
4334 (- (c-backward-token-2 (- count) balanced limit))
4336 (let ((jump-syntax (if balanced
4337 c-jump-syntax-balanced
4338 c-jump-syntax-unbalanced))
4339 (last (point))
4340 (prev (point)))
4342 (if (zerop count)
4343 ;; If count is zero we should jump if in the middle of a token.
4344 (c-end-of-current-token))
4346 (save-restriction
4347 (if limit (narrow-to-region (point-min) limit))
4348 (if (/= (point)
4349 (progn (c-forward-syntactic-ws) (point)))
4350 ;; Skip whitespace. Count this as a move if we did in
4351 ;; fact move.
4352 (setq count (max (1- count) 0)))
4354 (if (eobp)
4355 ;; Moved out of bounds. Make sure the returned count isn't zero.
4356 (progn
4357 (if (zerop count) (setq count 1))
4358 (goto-char last))
4360 ;; Use `condition-case' to avoid having the limit tests
4361 ;; inside the loop.
4362 (condition-case nil
4363 (while (and
4364 (> count 0)
4365 (progn
4366 (setq last (point))
4367 (cond ((looking-at jump-syntax)
4368 (goto-char (scan-sexps (point) 1))
4370 ((looking-at c-nonsymbol-token-regexp)
4371 (goto-char (match-end 0))
4373 ;; `c-nonsymbol-token-regexp' above should always
4374 ;; match if there are correct tokens. Try to
4375 ;; widen to see if the limit was set in the
4376 ;; middle of one, else fall back to treating
4377 ;; the offending thing as a one character token.
4378 ((and limit
4379 (save-restriction
4380 (widen)
4381 (looking-at c-nonsymbol-token-regexp)))
4382 nil)
4384 (forward-char)
4385 t))))
4386 (c-forward-syntactic-ws)
4387 (setq prev last
4388 count (1- count)))
4389 (error (goto-char last)))
4391 (when (eobp)
4392 (goto-char prev)
4393 (setq count (1+ count)))))
4395 count)))
4397 (defun c-backward-token-2 (&optional count balanced limit)
4398 "Move backward by tokens.
4399 See `c-forward-token-2' for details."
4401 (or count (setq count 1))
4402 (if (< count 0)
4403 (- (c-forward-token-2 (- count) balanced limit))
4405 (or limit (setq limit (point-min)))
4406 (let ((jump-syntax (if balanced
4407 c-jump-syntax-balanced
4408 c-jump-syntax-unbalanced))
4409 (last (point)))
4411 (if (zerop count)
4412 ;; The count is zero so try to skip to the beginning of the
4413 ;; current token.
4414 (if (> (point)
4415 (progn (c-beginning-of-current-token) (point)))
4416 (if (< (point) limit)
4417 ;; The limit is inside the same token, so return 1.
4418 (setq count 1))
4420 ;; We're not in the middle of a token. If there's
4421 ;; whitespace after the point then we must move backward,
4422 ;; so set count to 1 in that case.
4423 (and (looking-at c-syntactic-ws-start)
4424 ;; If we're looking at a '#' that might start a cpp
4425 ;; directive then we have to do a more elaborate check.
4426 (or (/= (char-after) ?#)
4427 (not c-opt-cpp-prefix)
4428 (save-excursion
4429 (and (= (point)
4430 (progn (beginning-of-line)
4431 (looking-at "[ \t]*")
4432 (match-end 0)))
4433 (or (bobp)
4434 (progn (backward-char)
4435 (not (eq (char-before) ?\\)))))))
4436 (setq count 1))))
4438 ;; Use `condition-case' to avoid having to check for buffer
4439 ;; limits in `backward-char', `scan-sexps' and `goto-char' below.
4440 (condition-case nil
4441 (while (and
4442 (> count 0)
4443 (progn
4444 (c-backward-syntactic-ws)
4445 (backward-char)
4446 (if (looking-at jump-syntax)
4447 (goto-char (scan-sexps (1+ (point)) -1))
4448 ;; This can be very inefficient if there's a long
4449 ;; sequence of operator tokens without any separation.
4450 ;; That doesn't happen in practice, anyway.
4451 (c-beginning-of-current-token))
4452 (>= (point) limit)))
4453 (setq last (point)
4454 count (1- count)))
4455 (error (goto-char last)))
4457 (if (< (point) limit)
4458 (goto-char last))
4460 count)))
4462 (defun c-forward-token-1 (&optional count balanced limit)
4463 "Like `c-forward-token-2' but doesn't treat multicharacter operator
4464 tokens like \"==\" as single tokens, i.e. all sequences of symbol
4465 characters are jumped over character by character. This function is
4466 for compatibility only; it's only a wrapper over `c-forward-token-2'."
4467 (let ((c-nonsymbol-token-regexp "\\s."))
4468 (c-forward-token-2 count balanced limit)))
4470 (defun c-backward-token-1 (&optional count balanced limit)
4471 "Like `c-backward-token-2' but doesn't treat multicharacter operator
4472 tokens like \"==\" as single tokens, i.e. all sequences of symbol
4473 characters are jumped over character by character. This function is
4474 for compatibility only; it's only a wrapper over `c-backward-token-2'."
4475 (let ((c-nonsymbol-token-regexp "\\s."))
4476 (c-backward-token-2 count balanced limit)))
4479 ;; Tools for doing searches restricted to syntactically relevant text.
4481 (defun c-syntactic-re-search-forward (regexp &optional bound noerror
4482 paren-level not-inside-token
4483 lookbehind-submatch)
4484 "Like `re-search-forward', but only report matches that are found
4485 in syntactically significant text. I.e. matches in comments, macros
4486 or string literals are ignored. The start point is assumed to be
4487 outside any comment, macro or string literal, or else the content of
4488 that region is taken as syntactically significant text.
4490 NOERROR, in addition to the values nil, t, and <anything else>
4491 used in `re-search-forward' can also take the values
4492 'before-literal and 'after-literal. In these cases, when BOUND
4493 is also given and is inside a literal, and a search fails, point
4494 will be left, respectively before or after the literal. Be aware
4495 that with 'after-literal, if a string or comment is unclosed at
4496 the end of the buffer, point may be left there, even though it is
4497 inside a literal there.
4499 If PAREN-LEVEL is non-nil, an additional restriction is added to
4500 ignore matches in nested paren sexps. The search will also not go
4501 outside the current list sexp, which has the effect that if the point
4502 should be moved to BOUND when no match is found \(i.e. NOERROR is
4503 neither nil nor t), then it will be at the closing paren if the end of
4504 the current list sexp is encountered first.
4506 If NOT-INSIDE-TOKEN is non-nil, matches in the middle of tokens are
4507 ignored. Things like multicharacter operators and special symbols
4508 \(e.g. \"`()\" in Pike) are handled but currently not floating point
4509 constants.
4511 If LOOKBEHIND-SUBMATCH is non-nil, it's taken as a number of a
4512 subexpression in REGEXP. The end of that submatch is used as the
4513 position to check for syntactic significance. If LOOKBEHIND-SUBMATCH
4514 isn't used or if that subexpression didn't match then the start
4515 position of the whole match is used instead. The \"look behind\"
4516 subexpression is never tested before the starting position, so it
4517 might be a good idea to include \\=\\= as a match alternative in it.
4519 Optimization note: Matches might be missed if the \"look behind\"
4520 subexpression can match the end of nonwhite syntactic whitespace,
4521 i.e. the end of comments or cpp directives. This since the function
4522 skips over such things before resuming the search. It's on the other
4523 hand not safe to assume that the \"look behind\" subexpression never
4524 matches syntactic whitespace.
4526 Bug: Unbalanced parens inside cpp directives are currently not handled
4527 correctly \(i.e. they don't get ignored as they should) when
4528 PAREN-LEVEL is set.
4530 Note that this function might do hidden buffer changes. See the
4531 comment at the start of cc-engine.el for more info."
4533 (or bound (setq bound (point-max)))
4534 (if paren-level (setq paren-level -1))
4536 ;;(message "c-syntactic-re-search-forward %s %s %S" (point) bound regexp)
4538 (let ((start (point))
4540 ;; Start position for the last search.
4541 search-pos
4542 ;; The `parse-partial-sexp' state between the start position
4543 ;; and the point.
4544 state
4545 ;; The current position after the last state update. The next
4546 ;; `parse-partial-sexp' continues from here.
4547 (state-pos (point))
4548 ;; The position at which to check the state and the state
4549 ;; there. This is separate from `state-pos' since we might
4550 ;; need to back up before doing the next search round.
4551 check-pos check-state
4552 ;; Last position known to end a token.
4553 (last-token-end-pos (point-min))
4554 ;; Set when a valid match is found.
4555 found)
4557 (condition-case err
4558 (while
4559 (and
4560 (progn
4561 (setq search-pos (point))
4562 (if (re-search-forward regexp bound noerror)
4564 ;; Without the following, when PAREN-LEVEL is non-nil, and
4565 ;; NOERROR is not nil or t, and the very first search above
4566 ;; has just failed, point would end up at BOUND rather than
4567 ;; just before the next close paren.
4568 (when (and (eq search-pos start)
4569 paren-level
4570 (not (memq noerror '(nil t))))
4571 (setq state (parse-partial-sexp start bound -1))
4572 (if (eq (car state) -1)
4573 (setq bound (1- (point)))))
4574 nil))
4576 (progn
4577 (setq state (parse-partial-sexp
4578 state-pos (match-beginning 0) paren-level nil state)
4579 state-pos (point))
4580 (if (setq check-pos (and lookbehind-submatch
4581 (or (not paren-level)
4582 (>= (car state) 0))
4583 (match-end lookbehind-submatch)))
4584 (setq check-state (parse-partial-sexp
4585 state-pos check-pos paren-level nil state))
4586 (setq check-pos state-pos
4587 check-state state))
4589 ;; NOTE: If we got a look behind subexpression and get
4590 ;; an insignificant match in something that isn't
4591 ;; syntactic whitespace (i.e. strings or in nested
4592 ;; parentheses), then we can never skip more than a
4593 ;; single character from the match start position
4594 ;; (i.e. `state-pos' here) before continuing the
4595 ;; search. That since the look behind subexpression
4596 ;; might match the end of the insignificant region in
4597 ;; the next search.
4599 (cond
4600 ((elt check-state 7)
4601 ;; Match inside a line comment. Skip to eol. Use
4602 ;; `re-search-forward' instead of `skip-chars-forward' to get
4603 ;; the right bound behavior.
4604 (re-search-forward "[\n\r]" bound noerror))
4606 ((elt check-state 4)
4607 ;; Match inside a block comment. Skip to the '*/'.
4608 (search-forward "*/" bound noerror))
4610 ((and (not (elt check-state 5))
4611 (eq (char-before check-pos) ?/)
4612 (not (c-get-char-property (1- check-pos) 'syntax-table))
4613 (memq (char-after check-pos) '(?/ ?*)))
4614 ;; Match in the middle of the opener of a block or line
4615 ;; comment.
4616 (if (= (char-after check-pos) ?/)
4617 (re-search-forward "[\n\r]" bound noerror)
4618 (search-forward "*/" bound noerror)))
4620 ;; The last `parse-partial-sexp' above might have
4621 ;; stopped short of the real check position if the end
4622 ;; of the current sexp was encountered in paren-level
4623 ;; mode. The checks above are always false in that
4624 ;; case, and since they can do better skipping in
4625 ;; lookbehind-submatch mode, we do them before
4626 ;; checking the paren level.
4628 ((and paren-level
4629 (/= (setq tmp (car check-state)) 0))
4630 ;; Check the paren level first since we're short of the
4631 ;; syntactic checking position if the end of the
4632 ;; current sexp was encountered by `parse-partial-sexp'.
4633 (if (> tmp 0)
4635 ;; Inside a nested paren sexp.
4636 (if lookbehind-submatch
4637 ;; See the NOTE above.
4638 (progn (goto-char state-pos) t)
4639 ;; Skip out of the paren quickly.
4640 (setq state (parse-partial-sexp state-pos bound 0 nil state)
4641 state-pos (point)))
4643 ;; Have exited the current paren sexp.
4644 (if noerror
4645 (progn
4646 ;; The last `parse-partial-sexp' call above
4647 ;; has left us just after the closing paren
4648 ;; in this case, so we can modify the bound
4649 ;; to leave the point at the right position
4650 ;; upon return.
4651 (setq bound (1- (point)))
4652 nil)
4653 (signal 'search-failed (list regexp)))))
4655 ((setq tmp (elt check-state 3))
4656 ;; Match inside a string.
4657 (if (or lookbehind-submatch
4658 (not (integerp tmp)))
4659 ;; See the NOTE above.
4660 (progn (goto-char state-pos) t)
4661 ;; Skip to the end of the string before continuing.
4662 (let ((ender (make-string 1 tmp)) (continue t))
4663 (while (if (search-forward ender bound noerror)
4664 (progn
4665 (setq state (parse-partial-sexp
4666 state-pos (point) nil nil state)
4667 state-pos (point))
4668 (elt state 3))
4669 (setq continue nil)))
4670 continue)))
4672 ((save-excursion
4673 (save-match-data
4674 (c-beginning-of-macro start)))
4675 ;; Match inside a macro. Skip to the end of it.
4676 (c-end-of-macro)
4677 (cond ((<= (point) bound) t)
4678 (noerror nil)
4679 (t (signal 'search-failed (list regexp)))))
4681 ((and not-inside-token
4682 (or (< check-pos last-token-end-pos)
4683 (< check-pos
4684 (save-excursion
4685 (goto-char check-pos)
4686 (save-match-data
4687 (c-end-of-current-token last-token-end-pos))
4688 (setq last-token-end-pos (point))))))
4689 ;; Inside a token.
4690 (if lookbehind-submatch
4691 ;; See the NOTE above.
4692 (goto-char state-pos)
4693 (goto-char (min last-token-end-pos bound))))
4696 ;; A real match.
4697 (setq found t)
4698 nil)))
4700 ;; Should loop to search again, but take care to avoid
4701 ;; looping on the same spot.
4702 (or (/= search-pos (point))
4703 (if (= (point) bound)
4704 (if noerror
4706 (signal 'search-failed (list regexp)))
4707 (forward-char)
4708 t))))
4710 (error
4711 (goto-char start)
4712 (signal (car err) (cdr err))))
4714 ;;(message "c-syntactic-re-search-forward done %s" (or (match-end 0) (point)))
4716 (if found
4717 (progn
4718 (goto-char (match-end 0))
4719 (match-end 0))
4721 ;; Search failed. Set point as appropriate.
4722 (cond
4723 ((eq noerror t)
4724 (goto-char start))
4725 ((not (memq noerror '(before-literal after-literal)))
4726 (goto-char bound))
4727 (t (setq state (parse-partial-sexp state-pos bound nil nil state))
4728 (if (or (elt state 3) (elt state 4))
4729 (if (eq noerror 'before-literal)
4730 (goto-char (elt state 8))
4731 (parse-partial-sexp bound (point-max) nil nil
4732 state 'syntax-table))
4733 (goto-char bound))))
4735 nil)))
4737 (defvar safe-pos-list) ; bound in c-syntactic-skip-backward
4739 (defsubst c-ssb-lit-begin ()
4740 ;; Return the start of the literal point is in, or nil.
4741 ;; We read and write the variables `safe-pos', `safe-pos-list', `state'
4742 ;; bound in the caller.
4744 ;; Use `parse-partial-sexp' from a safe position down to the point to check
4745 ;; if it's outside comments and strings.
4746 (save-excursion
4747 (let ((pos (point)) safe-pos state)
4748 ;; Pick a safe position as close to the point as possible.
4750 ;; FIXME: Consult `syntax-ppss' here if our cache doesn't give a good
4751 ;; position.
4753 (while (and safe-pos-list
4754 (> (car safe-pos-list) (point)))
4755 (setq safe-pos-list (cdr safe-pos-list)))
4756 (unless (setq safe-pos (car-safe safe-pos-list))
4757 (setq safe-pos (max (or (c-safe-position
4758 (point) (c-parse-state))
4760 (point-min))
4761 safe-pos-list (list safe-pos)))
4763 ;; Cache positions along the way to use if we have to back up more. We
4764 ;; cache every closing paren on the same level. If the paren cache is
4765 ;; relevant in this region then we're typically already on the same
4766 ;; level as the target position. Note that we might cache positions
4767 ;; after opening parens in case safe-pos is in a nested list. That's
4768 ;; both uncommon and harmless.
4769 (while (progn
4770 (setq state (parse-partial-sexp
4771 safe-pos pos 0))
4772 (< (point) pos))
4773 (setq safe-pos (point)
4774 safe-pos-list (cons safe-pos safe-pos-list)))
4776 ;; If the state contains the start of the containing sexp we cache that
4777 ;; position too, so that parse-partial-sexp in the next run has a bigger
4778 ;; chance of starting at the same level as the target position and thus
4779 ;; will get more good safe positions into the list.
4780 (if (elt state 1)
4781 (setq safe-pos (1+ (elt state 1))
4782 safe-pos-list (cons safe-pos safe-pos-list)))
4784 (if (or (elt state 3) (elt state 4))
4785 ;; Inside string or comment. Continue search at the
4786 ;; beginning of it.
4787 (elt state 8)))))
4789 (defun c-syntactic-skip-backward (skip-chars &optional limit paren-level)
4790 "Like `skip-chars-backward' but only look at syntactically relevant chars,
4791 i.e. don't stop at positions inside syntactic whitespace or string
4792 literals. Preprocessor directives are also ignored, with the exception
4793 of the one that the point starts within, if any. If LIMIT is given,
4794 it's assumed to be at a syntactically relevant position.
4796 If PAREN-LEVEL is non-nil, the function won't stop in nested paren
4797 sexps, and the search will also not go outside the current paren sexp.
4798 However, if LIMIT or the buffer limit is reached inside a nested paren
4799 then the point will be left at the limit.
4801 Non-nil is returned if the point moved, nil otherwise.
4803 Note that this function might do hidden buffer changes. See the
4804 comment at the start of cc-engine.el for more info."
4806 (c-self-bind-state-cache
4807 (let ((start (point))
4808 ;; A list of syntactically relevant positions in descending
4809 ;; order. It's used to avoid scanning repeatedly over
4810 ;; potentially large regions with `parse-partial-sexp' to verify
4811 ;; each position. Used in `c-ssb-lit-begin'
4812 safe-pos-list
4813 ;; The result from `c-beginning-of-macro' at the start position or the
4814 ;; start position itself if it isn't within a macro. Evaluated on
4815 ;; demand.
4816 start-macro-beg
4817 ;; The earliest position after the current one with the same paren
4818 ;; level. Used only when `paren-level' is set.
4819 lit-beg
4820 (paren-level-pos (point)))
4822 (while
4823 (progn
4824 ;; The next loop "tries" to find the end point each time round,
4825 ;; loops when it hasn't succeeded.
4826 (while
4827 (and
4828 (let ((pos (point)))
4829 (while (and
4830 (< (skip-chars-backward skip-chars limit) 0)
4831 ;; Don't stop inside a literal.
4832 (when (setq lit-beg (c-ssb-lit-begin))
4833 (goto-char lit-beg)
4834 t)))
4835 (< (point) pos))
4837 (let ((pos (point)) state-2 pps-end-pos)
4839 (cond
4840 ((and paren-level
4841 (save-excursion
4842 (setq state-2 (parse-partial-sexp
4843 pos paren-level-pos -1)
4844 pps-end-pos (point))
4845 (/= (car state-2) 0)))
4846 ;; Not at the right level.
4848 (if (and (< (car state-2) 0)
4849 ;; We stop above if we go out of a paren.
4850 ;; Now check whether it precedes or is
4851 ;; nested in the starting sexp.
4852 (save-excursion
4853 (setq state-2
4854 (parse-partial-sexp
4855 pps-end-pos paren-level-pos
4856 nil nil state-2))
4857 (< (car state-2) 0)))
4859 ;; We've stopped short of the starting position
4860 ;; so the hit was inside a nested list. Go up
4861 ;; until we are at the right level.
4862 (condition-case nil
4863 (progn
4864 (goto-char (scan-lists pos -1
4865 (- (car state-2))))
4866 (setq paren-level-pos (point))
4867 (if (and limit (>= limit paren-level-pos))
4868 (progn
4869 (goto-char limit)
4870 nil)
4872 (error
4873 (goto-char (or limit (point-min)))
4874 nil))
4876 ;; The hit was outside the list at the start
4877 ;; position. Go to the start of the list and exit.
4878 (goto-char (1+ (elt state-2 1)))
4879 nil))
4881 ((c-beginning-of-macro limit)
4882 ;; Inside a macro.
4883 (if (< (point)
4884 (or start-macro-beg
4885 (setq start-macro-beg
4886 (save-excursion
4887 (goto-char start)
4888 (c-beginning-of-macro limit)
4889 (point)))))
4892 ;; It's inside the same macro we started in so it's
4893 ;; a relevant match.
4894 (goto-char pos)
4895 nil))))))
4897 (> (point)
4898 (progn
4899 ;; Skip syntactic ws afterwards so that we don't stop at the
4900 ;; end of a comment if `skip-chars' is something like "^/".
4901 (c-backward-syntactic-ws)
4902 (point)))))
4904 ;; We might want to extend this with more useful return values in
4905 ;; the future.
4906 (/= (point) start))))
4908 ;; The following is an alternative implementation of
4909 ;; `c-syntactic-skip-backward' that uses backward movement to keep
4910 ;; track of the syntactic context. It turned out to be generally
4911 ;; slower than the one above which uses forward checks from earlier
4912 ;; safe positions.
4914 ;;(defconst c-ssb-stop-re
4915 ;; ;; The regexp matching chars `c-syntactic-skip-backward' needs to
4916 ;; ;; stop at to avoid going into comments and literals.
4917 ;; (concat
4918 ;; ;; Match comment end syntax and string literal syntax. Also match
4919 ;; ;; '/' for block comment endings (not covered by comment end
4920 ;; ;; syntax).
4921 ;; "\\s>\\|/\\|\\s\""
4922 ;; (if (memq 'gen-string-delim c-emacs-features)
4923 ;; "\\|\\s|"
4924 ;; "")
4925 ;; (if (memq 'gen-comment-delim c-emacs-features)
4926 ;; "\\|\\s!"
4927 ;; "")))
4929 ;;(defconst c-ssb-stop-paren-re
4930 ;; ;; Like `c-ssb-stop-re' but also stops at paren chars.
4931 ;; (concat c-ssb-stop-re "\\|\\s(\\|\\s)"))
4933 ;;(defconst c-ssb-sexp-end-re
4934 ;; ;; Regexp matching the ending syntax of a complex sexp.
4935 ;; (concat c-string-limit-regexp "\\|\\s)"))
4937 ;;(defun c-syntactic-skip-backward (skip-chars &optional limit paren-level)
4938 ;; "Like `skip-chars-backward' but only look at syntactically relevant chars,
4939 ;;i.e. don't stop at positions inside syntactic whitespace or string
4940 ;;literals. Preprocessor directives are also ignored. However, if the
4941 ;;point is within a comment, string literal or preprocessor directory to
4942 ;;begin with, its contents is treated as syntactically relevant chars.
4943 ;;If LIMIT is given, it limits the backward search and the point will be
4944 ;;left there if no earlier position is found.
4946 ;;If PAREN-LEVEL is non-nil, the function won't stop in nested paren
4947 ;;sexps, and the search will also not go outside the current paren sexp.
4948 ;;However, if LIMIT or the buffer limit is reached inside a nested paren
4949 ;;then the point will be left at the limit.
4951 ;;Non-nil is returned if the point moved, nil otherwise.
4953 ;;Note that this function might do hidden buffer changes. See the
4954 ;;comment at the start of cc-engine.el for more info."
4956 ;; (save-restriction
4957 ;; (when limit
4958 ;; (narrow-to-region limit (point-max)))
4960 ;; (let ((start (point)))
4961 ;; (catch 'done
4962 ;; (while (let ((last-pos (point))
4963 ;; (stop-pos (progn
4964 ;; (skip-chars-backward skip-chars)
4965 ;; (point))))
4967 ;; ;; Skip back over the same region as
4968 ;; ;; `skip-chars-backward' above, but keep to
4969 ;; ;; syntactically relevant positions.
4970 ;; (goto-char last-pos)
4971 ;; (while (and
4972 ;; ;; `re-search-backward' with a single char regexp
4973 ;; ;; should be fast.
4974 ;; (re-search-backward
4975 ;; (if paren-level c-ssb-stop-paren-re c-ssb-stop-re)
4976 ;; stop-pos 'move)
4978 ;; (progn
4979 ;; (cond
4980 ;; ((looking-at "\\s(")
4981 ;; ;; `paren-level' is set and we've found the
4982 ;; ;; start of the containing paren.
4983 ;; (forward-char)
4984 ;; (throw 'done t))
4986 ;; ((looking-at c-ssb-sexp-end-re)
4987 ;; ;; We're at the end of a string literal or paren
4988 ;; ;; sexp (if `paren-level' is set).
4989 ;; (forward-char)
4990 ;; (condition-case nil
4991 ;; (c-backward-sexp)
4992 ;; (error
4993 ;; (goto-char limit)
4994 ;; (throw 'done t))))
4996 ;; (t
4997 ;; (forward-char)
4998 ;; ;; At the end of some syntactic ws or possibly
4999 ;; ;; after a plain '/' operator.
5000 ;; (let ((pos (point)))
5001 ;; (c-backward-syntactic-ws)
5002 ;; (if (= pos (point))
5003 ;; ;; Was a plain '/' operator. Go past it.
5004 ;; (backward-char)))))
5006 ;; (> (point) stop-pos))))
5008 ;; ;; Now the point is either at `stop-pos' or at some
5009 ;; ;; position further back if `stop-pos' was at a
5010 ;; ;; syntactically irrelevant place.
5012 ;; ;; Skip additional syntactic ws so that we don't stop
5013 ;; ;; at the end of a comment if `skip-chars' is
5014 ;; ;; something like "^/".
5015 ;; (c-backward-syntactic-ws)
5017 ;; (< (point) stop-pos))))
5019 ;; ;; We might want to extend this with more useful return values
5020 ;; ;; in the future.
5021 ;; (/= (point) start))))
5024 ;; Tools for handling comments and string literals.
5026 (defun c-in-literal (&optional _lim detect-cpp)
5027 "Return the type of literal point is in, if any.
5028 The return value is `c' if in a C-style comment, `c++' if in a C++
5029 style comment, `string' if in a string literal, `pound' if DETECT-CPP
5030 is non-nil and in a preprocessor line, or nil if somewhere else.
5031 Optional LIM is used as the backward limit of the search. If omitted,
5032 or nil, `c-beginning-of-defun' is used.
5034 Note that this function might do hidden buffer changes. See the
5035 comment at the start of cc-engine.el for more info."
5036 (save-restriction
5037 (widen)
5038 (let ((lit (c-state-semi-pp-to-literal (point))))
5039 (or (cadr lit)
5040 (and detect-cpp
5041 (save-excursion (c-beginning-of-macro))
5042 'pound)))))
5044 (defun c-literal-limits (&optional lim near not-in-delimiter)
5045 "Return a cons of the beginning and end positions of the comment or
5046 string surrounding point (including both delimiters), or nil if point
5047 isn't in one. If LIM is non-nil, it's used as the \"safe\" position
5048 to start parsing from. If NEAR is non-nil, then the limits of any
5049 literal next to point is returned. \"Next to\" means there's only
5050 spaces and tabs between point and the literal. The search for such a
5051 literal is done first in forward direction. If NOT-IN-DELIMITER is
5052 non-nil, the case when point is inside a starting delimiter won't be
5053 recognized. This only has effect for comments which have starting
5054 delimiters with more than one character.
5056 Note that this function might do hidden buffer changes. See the
5057 comment at the start of cc-engine.el for more info."
5059 (save-excursion
5060 (let*
5061 ((pos (point))
5062 (lit-limits
5063 (if lim
5064 (let ((s (parse-partial-sexp lim (point))))
5065 (when (or (nth 3 s)
5066 (and (nth 4 s) (not (eq (nth 7 s) 'syntax-table))))
5067 (cons (nth 8 s)
5068 (progn (parse-partial-sexp (point) (point-max)
5069 nil nil
5071 'syntax-table)
5072 (point)))))
5073 (let ((pp-to-lit (c-state-full-pp-to-literal pos not-in-delimiter)))
5074 (car (cddr pp-to-lit))))))
5075 (cond
5076 (lit-limits)
5078 (near
5079 (goto-char pos)
5080 ;; Search forward for a literal.
5081 (skip-chars-forward " \t")
5082 (cond
5083 ((looking-at c-string-limit-regexp) ; String.
5084 (cons (point) (or (c-safe (c-forward-sexp 1) (point))
5085 (point-max))))
5087 ((looking-at c-comment-start-regexp) ; Line or block comment.
5088 (cons (point) (progn (c-forward-single-comment) (point))))
5091 ;; Search backward.
5092 (skip-chars-backward " \t")
5094 (let ((end (point)) beg)
5095 (cond
5096 ((save-excursion
5097 (< (skip-syntax-backward c-string-syntax) 0)) ; String.
5098 (setq beg (c-safe (c-backward-sexp 1) (point))))
5100 ((and (c-safe (forward-char -2) t)
5101 (looking-at "*/"))
5102 ;; Block comment. Due to the nature of line
5103 ;; comments, they will always be covered by the
5104 ;; normal case above.
5105 (goto-char end)
5106 (c-backward-single-comment)
5107 ;; If LIM is bogus, beg will be bogus.
5108 (setq beg (point))))
5110 (if beg (cons beg end))))))
5111 ))))
5113 (defun c-literal-start (&optional safe-pos)
5114 "Return the start of the string or comment surrounding point, or nil if
5115 point isn't in one. SAFE-POS, if non-nil, is a position before point which is
5116 a known \"safe position\", i.e. outside of any string or comment."
5117 (if safe-pos
5118 (let ((s (parse-partial-sexp safe-pos (point))))
5119 (and (or (nth 3 s)
5120 (and (nth 4 s) (not (eq (nth 7 s) 'syntax-table))))
5121 (nth 8 s)))
5122 (car (cddr (c-state-semi-pp-to-literal (point))))))
5124 ;; In case external callers use this; it did have a docstring.
5125 (defalias 'c-literal-limits-fast 'c-literal-limits)
5127 (defun c-collect-line-comments (range)
5128 "If the argument is a cons of two buffer positions (such as returned by
5129 `c-literal-limits'), and that range contains a C++ style line comment,
5130 then an extended range is returned that contains all adjacent line
5131 comments (i.e. all comments that starts in the same column with no
5132 empty lines or non-whitespace characters between them). Otherwise the
5133 argument is returned.
5135 Note that this function might do hidden buffer changes. See the
5136 comment at the start of cc-engine.el for more info."
5138 (save-excursion
5139 (condition-case nil
5140 (if (and (consp range) (progn
5141 (goto-char (car range))
5142 (looking-at c-line-comment-starter)))
5143 (let ((col (current-column))
5144 (beg (point))
5145 (bopl (c-point 'bopl))
5146 (end (cdr range)))
5147 ;; Got to take care in the backward direction to handle
5148 ;; comments which are preceded by code.
5149 (while (and (c-backward-single-comment)
5150 (>= (point) bopl)
5151 (looking-at c-line-comment-starter)
5152 (= col (current-column)))
5153 (setq beg (point)
5154 bopl (c-point 'bopl)))
5155 (goto-char end)
5156 (while (and (progn (skip-chars-forward " \t")
5157 (looking-at c-line-comment-starter))
5158 (= col (current-column))
5159 (prog1 (zerop (forward-line 1))
5160 (setq end (point)))))
5161 (cons beg end))
5162 range)
5163 (error range))))
5165 (defun c-literal-type (range)
5166 "Convenience function that given the result of `c-literal-limits',
5167 returns nil or the type of literal that the range surrounds, one
5168 of the symbols `c', `c++' or `string'. It's much faster than using
5169 `c-in-literal' and is intended to be used when you need both the
5170 type of a literal and its limits.
5172 Note that this function might do hidden buffer changes. See the
5173 comment at the start of cc-engine.el for more info."
5175 (if (consp range)
5176 (save-excursion
5177 (goto-char (car range))
5178 (cond ((looking-at c-string-limit-regexp) 'string)
5179 ((or (looking-at "//") ; c++ line comment
5180 (and (looking-at "\\s<") ; comment starter
5181 (looking-at "#"))) ; awk comment.
5182 'c++)
5183 (t 'c))) ; Assuming the range is valid.
5184 range))
5186 (defsubst c-determine-limit-get-base (start try-size)
5187 ;; Get a "safe place" approximately TRY-SIZE characters before START.
5188 ;; This defsubst doesn't preserve point.
5189 (let* ((pos (max (- start try-size) (point-min)))
5190 (s (c-state-semi-pp-to-literal pos)))
5191 (or (car (cddr s)) pos)))
5193 (defun c-determine-limit (how-far-back &optional start try-size)
5194 ;; Return a buffer position HOW-FAR-BACK non-literal characters from START
5195 ;; (default point). This is done by going back further in the buffer then
5196 ;; searching forward for literals. The position found won't be in a
5197 ;; literal. We start searching for the sought position TRY-SIZE (default
5198 ;; twice HOW-FAR-BACK) bytes back from START. This function must be fast.
5199 ;; :-)
5200 (save-excursion
5201 (let* ((start (or start (point)))
5202 (try-size (or try-size (* 2 how-far-back)))
5203 (base (c-determine-limit-get-base start try-size))
5204 (pos base)
5206 (s (parse-partial-sexp pos pos)) ; null state.
5207 stack elt size
5208 (count 0))
5209 (while (< pos start)
5210 ;; Move forward one literal each time round this loop.
5211 ;; Move forward to the start of a comment or string.
5212 (setq s (parse-partial-sexp
5214 start
5215 nil ; target-depth
5216 nil ; stop-before
5217 s ; state
5218 'syntax-table)) ; stop-comment
5220 ;; Gather details of the non-literal-bit - starting pos and size.
5221 (setq size (- (if (or (and (nth 4 s) (not (eq (nth 7 s) 'syntax-table)))
5222 (nth 3 s))
5223 (nth 8 s)
5224 (point))
5225 pos))
5226 (if (> size 0)
5227 (setq stack (cons (cons pos size) stack)))
5229 ;; Move forward to the end of the comment/string.
5230 (if (or (and (nth 4 s) (not (eq (nth 7 s) 'syntax-table)))
5231 (nth 3 s))
5232 (setq s (parse-partial-sexp
5233 (point)
5234 start
5235 nil ; target-depth
5236 nil ; stop-before
5237 s ; state
5238 'syntax-table))) ; stop-comment
5239 (setq pos (point)))
5241 ;; Now try and find enough non-literal characters recorded on the stack.
5242 ;; Go back one recorded literal each time round this loop.
5243 (while (and (< count how-far-back)
5244 stack)
5245 (setq elt (car stack)
5246 stack (cdr stack))
5247 (setq count (+ count (cdr elt))))
5249 ;; Have we found enough yet?
5250 (cond
5251 ((>= count how-far-back)
5252 (+ (car elt) (- count how-far-back)))
5253 ((eq base (point-min))
5254 (point-min))
5256 (c-determine-limit (- how-far-back count) base try-size))))))
5258 (defun c-determine-+ve-limit (how-far &optional start-pos)
5259 ;; Return a buffer position about HOW-FAR non-literal characters forward
5260 ;; from START-POS (default point), which must not be inside a literal.
5261 (save-excursion
5262 (let ((pos (or start-pos (point)))
5263 (count how-far)
5264 (s (parse-partial-sexp (point) (point)))) ; null state
5265 (while (and (not (eobp))
5266 (> count 0))
5267 ;; Scan over counted characters.
5268 (setq s (parse-partial-sexp
5270 (min (+ pos count) (point-max))
5271 nil ; target-depth
5272 nil ; stop-before
5273 s ; state
5274 'syntax-table)) ; stop-comment
5275 (setq count (- count (- (point) pos) 1)
5276 pos (point))
5277 ;; Scan over literal characters.
5278 (if (nth 8 s)
5279 (setq s (parse-partial-sexp
5281 (point-max)
5282 nil ; target-depth
5283 nil ; stop-before
5284 s ; state
5285 'syntax-table) ; stop-comment
5286 pos (point))))
5287 (point))))
5290 ;; `c-find-decl-spots' and accompanying stuff.
5292 ;; Variables used in `c-find-decl-spots' to cache the search done for
5293 ;; the first declaration in the last call. When that function starts,
5294 ;; it needs to back up over syntactic whitespace to look at the last
5295 ;; token before the region being searched. That can sometimes cause
5296 ;; moves back and forth over a quite large region of comments and
5297 ;; macros, which would be repeated for each changed character when
5298 ;; we're called during fontification, since font-lock refontifies the
5299 ;; current line for each change. Thus it's worthwhile to cache the
5300 ;; first match.
5302 ;; `c-find-decl-syntactic-pos' is a syntactically relevant position in
5303 ;; the syntactic whitespace less or equal to some start position.
5304 ;; There's no cached value if it's nil.
5306 ;; `c-find-decl-match-pos' is the match position if
5307 ;; `c-find-decl-prefix-search' matched before the syntactic whitespace
5308 ;; at `c-find-decl-syntactic-pos', or nil if there's no such match.
5309 (defvar c-find-decl-syntactic-pos nil)
5310 (make-variable-buffer-local 'c-find-decl-syntactic-pos)
5311 (defvar c-find-decl-match-pos nil)
5312 (make-variable-buffer-local 'c-find-decl-match-pos)
5314 (defsubst c-invalidate-find-decl-cache (change-min-pos)
5315 (and c-find-decl-syntactic-pos
5316 (< change-min-pos c-find-decl-syntactic-pos)
5317 (setq c-find-decl-syntactic-pos nil)))
5319 ; (defface c-debug-decl-spot-face
5320 ; '((t (:background "Turquoise")))
5321 ; "Debug face to mark the spots where `c-find-decl-spots' stopped.")
5322 ; (defface c-debug-decl-sws-face
5323 ; '((t (:background "Khaki")))
5324 ; "Debug face to mark the syntactic whitespace between the declaration
5325 ; spots and the preceding token end.")
5327 (defmacro c-debug-put-decl-spot-faces (match-pos decl-pos)
5328 (when (facep 'c-debug-decl-spot-face)
5329 `(c-save-buffer-state ((match-pos ,match-pos) (decl-pos ,decl-pos))
5330 (c-debug-add-face (max match-pos (point-min)) decl-pos
5331 'c-debug-decl-sws-face)
5332 (c-debug-add-face decl-pos (min (1+ decl-pos) (point-max))
5333 'c-debug-decl-spot-face))))
5334 (defmacro c-debug-remove-decl-spot-faces (beg end)
5335 (when (facep 'c-debug-decl-spot-face)
5336 `(c-save-buffer-state ()
5337 (c-debug-remove-face ,beg ,end 'c-debug-decl-spot-face)
5338 (c-debug-remove-face ,beg ,end 'c-debug-decl-sws-face))))
5340 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
5341 ;; Machinery for determining when we're at top level (this including being
5342 ;; directly inside a class or namespace, etc.)
5344 ;; We maintain a stack of brace depths in structures like classes and
5345 ;; namespaces. The car of this structure, when non-nil, indicates that the
5346 ;; associated position is within a template (etc.) structure, and the value is
5347 ;; the position where the (outermost) template ends. The other elements in
5348 ;; the structure are stacked elements, one each for each enclosing "top level"
5349 ;; structure.
5351 ;; At the very outermost level, the value of the stack would be (nil 1), the
5352 ;; "1" indicating an enclosure in a notional all-enclosing block. After
5353 ;; passing a keyword such as "namespace", the value would become (nil 0 1).
5354 ;; At this point, passing a semicolon would cause the 0 to be dropped from the
5355 ;; stack (at any other time, a semicolon is ignored). Alternatively, on
5356 ;; passing an opening brace, the stack would become (nil 1 1). Each opening
5357 ;; brace passed causes the cadr to be incremented, and passing closing braces
5358 ;; causes it to be decremented until it reaches 1. On passing a closing brace
5359 ;; when the cadr of the stack is at 1, this causes it to be removed from the
5360 ;; stack, the corresponding namespace (etc.) structure having been closed.
5362 ;; There is a special stack value -1 which means the C++ colon operator
5363 ;; introducing a list of inherited classes has just been parsed. The value
5364 ;; persists on the stack until the next open brace or semicolon.
5366 ;; When the car of the stack is non-nil, i.e. when we're in a template (etc.)
5367 ;; structure, braces are not counted. The counting resumes only after passing
5368 ;; the template's closing position, which is recorded in the car of the stack.
5370 ;; The test for being at top level consists of the cadr being 0 or 1.
5372 ;; The values of this stack throughout a buffer are cached in a simple linear
5373 ;; cache, every 5000 characters.
5375 ;; Note to maintainers: This cache mechanism is MUCH faster than recalculating
5376 ;; the stack at every entry to `c-find-decl-spots' using `c-at-toplevel-p' or
5377 ;; the like.
5378 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
5379 ;; The approximate interval at which we cache the value of the brace stack.
5380 (defconst c-bs-interval 5000)
5381 ;; The list of cached values of the brace stack. Each value in the list is a
5382 ;; cons of the position it is valid for and the value of the stack as
5383 ;; described above.
5384 (defvar c-bs-cache nil)
5385 (make-variable-buffer-local 'c-bs-cache)
5386 ;; The position of the buffer at and below which entries in `c-bs-cache' are
5387 ;; valid.
5388 (defvar c-bs-cache-limit 1)
5389 (make-variable-buffer-local 'c-bs-cache-limit)
5390 ;; The previous buffer position for which the brace stack value was
5391 ;; determined.
5392 (defvar c-bs-prev-pos most-positive-fixnum)
5393 (make-variable-buffer-local 'c-bs-prev-pos)
5394 ;; The value of the brace stack at `c-bs-prev-pos'.
5395 (defvar c-bs-prev-stack nil)
5396 (make-variable-buffer-local 'c-bs-prev-stack)
5398 (defun c-init-bs-cache ()
5399 ;; Initialize the cache in `c-bs-cache' and related variables.
5400 (setq c-bs-cache nil
5401 c-bs-cache-limit 1
5402 c-bs-prev-pos most-positive-fixnum
5403 c-bs-prev-stack nil))
5405 (defun c-truncate-bs-cache (pos &rest _ignore)
5406 ;; Truncate the upper bound of the cache `c-bs-cache' to POS, if it is
5407 ;; higher than that position. This is called as either a before- or
5408 ;; after-change-function.
5409 (setq c-bs-cache-limit
5410 (min c-bs-cache-limit pos)))
5412 (defun c-update-brace-stack (stack from to)
5413 ;; Give a brace-stack which has the value STACK at position FROM, update it
5414 ;; to it's value at position TO, where TO is after (or equal to) FROM.
5415 ;; Return a cons of either TO (if it is outside a literal) and this new
5416 ;; value, or of the next position after TO outside a literal and the new
5417 ;; value.
5418 (let (match kwd-sym (prev-match-pos 1)
5419 (s (cdr stack))
5420 (bound-<> (car stack))
5422 (save-excursion
5423 (cond
5424 ((and bound-<> (<= to bound-<>))
5425 (goto-char to)) ; Nothing to do.
5426 (bound-<>
5427 (goto-char bound-<>)
5428 (setq bound-<> nil))
5429 (t (goto-char from)))
5430 (while (and (< (point) to)
5431 (c-syntactic-re-search-forward
5432 (if (<= (car s) 0)
5433 c-brace-stack-thing-key
5434 c-brace-stack-no-semi-key)
5435 to 'after-literal)
5436 (> (point) prev-match-pos)) ; prevent infinite loop.
5437 (setq prev-match-pos (point))
5438 (setq match (match-string-no-properties 1)
5439 kwd-sym (c-keyword-sym match))
5440 (cond
5441 ((and (equal match "{")
5442 (progn (backward-char)
5443 (prog1 (looking-at "\\s(")
5444 (forward-char))))
5445 (setq s (if s
5446 (cons (if (<= (car s) 0)
5448 (1+ (car s)))
5449 (cdr s))
5450 (list 1))))
5451 ((and (equal match "}")
5452 (progn (backward-char)
5453 (prog1 (looking-at "\\s)")
5454 (forward-char))))
5455 (setq s
5456 (cond
5457 ((and s (> (car s) 1))
5458 (cons (1- (car s)) (cdr s)))
5459 ((and (cdr s) (eq (car s) 1))
5460 (cdr s))
5461 (t s))))
5462 ((and (equal match "<")
5463 (progn (backward-char)
5464 (prog1 (looking-at "\\s(")
5465 (forward-char))))
5466 (backward-char)
5467 (if (c-forward-<>-arglist nil) ; Should always work.
5468 (when (> (point) to)
5469 (setq bound-<> (point)))
5470 (forward-char)))
5471 ((and (equal match ":")
5473 (eq (car s) 0))
5474 (setq s (cons -1 (cdr s))))
5475 ((and (equal match ",")
5476 (eq (car s) -1))) ; at "," in "class foo : bar, ..."
5477 ((member match '(";" "," ")"))
5478 (when (and s (cdr s) (<= (car s) 0))
5479 (setq s (cdr s))))
5480 ((c-keyword-member kwd-sym 'c-flat-decl-block-kwds)
5481 (push 0 s))))
5482 (cons (point)
5483 (cons bound-<> s)))))
5485 (defun c-brace-stack-at (here)
5486 ;; Given a buffer position HERE, Return the value of the brace stack there.
5487 (save-excursion
5488 (save-restriction
5489 (widen)
5490 (let ((c c-bs-cache)
5491 (can-use-prev (<= c-bs-prev-pos c-bs-cache-limit))
5492 elt stack pos npos high-elt)
5493 ;; Trim the cache to take account of buffer changes.
5494 (while (and c
5495 (> (caar c) c-bs-cache-limit))
5496 (setq c (cdr c)))
5497 (setq c-bs-cache c)
5499 (while (and c
5500 (> (caar c) here))
5501 (setq high-elt (car c))
5502 (setq c (cdr c)))
5503 (setq pos (or (and c (caar c))
5504 (point-min)))
5506 (setq elt (if c
5507 (car c)
5508 (cons (point-min)
5509 (cons nil (list 1)))))
5510 (when (not high-elt)
5511 (setq stack (cdr elt))
5512 (while
5513 ;; Add an element to `c-state-semi-nonlit-pos-cache' each iteration.
5514 (<= (setq npos (+ pos c-bs-interval)) here)
5515 (setq elt (c-update-brace-stack stack pos npos))
5516 (setq npos (car elt))
5517 (setq stack (cdr elt))
5518 (unless (eq npos (point-max)) ; NPOS could be in a literal at EOB.
5519 (setq c-bs-cache (cons elt c-bs-cache)))
5520 (setq pos npos)))
5522 (if (> pos c-bs-cache-limit)
5523 (setq c-bs-cache-limit pos))
5525 ;; Can we just use the previous value?
5526 (if (and can-use-prev
5527 (<= c-bs-prev-pos here)
5528 (> c-bs-prev-pos (car elt)))
5529 (setq pos c-bs-prev-pos
5530 stack c-bs-prev-stack)
5531 (setq pos (car elt)
5532 stack (cdr elt)))
5533 (if (> here c-bs-cache-limit)
5534 (setq c-bs-cache-limit here))
5535 (setq elt (c-update-brace-stack stack pos here)
5536 c-bs-prev-pos (car elt)
5537 c-bs-prev-stack (cdr elt))))))
5539 (defun c-bs-at-toplevel-p (here)
5540 ;; Is position HERE at the top level, as indicated by the brace stack?
5541 (let ((stack (c-brace-stack-at here)))
5542 (or (null stack) ; Probably unnecessary.
5543 (<= (cadr stack) 1))))
5545 (defmacro c-find-decl-prefix-search ()
5546 ;; Macro used inside `c-find-decl-spots'. It ought to be a defun,
5547 ;; but it contains lots of free variables that refer to things
5548 ;; inside `c-find-decl-spots'. The point is left at `cfd-match-pos'
5549 ;; if there is a match, otherwise at `cfd-limit'.
5551 ;; The macro moves point forward to the next putative start of a declaration
5552 ;; or cfd-limit. This decl start is the next token after a "declaration
5553 ;; prefix". The declaration prefix is the earlier of `cfd-prop-match' and
5554 ;; `cfd-re-match'. `cfd-match-pos' is set to the decl prefix.
5556 ;; This macro might do hidden buffer changes.
5558 '(progn
5559 ;; Find the next property match position if we haven't got one already.
5560 (unless cfd-prop-match
5561 (save-excursion
5562 (while (progn
5563 (goto-char (c-next-single-property-change
5564 (point) 'c-type nil cfd-limit))
5565 (and (< (point) cfd-limit)
5566 (not (eq (c-get-char-property (1- (point)) 'c-type)
5567 'c-decl-end)))))
5568 (setq cfd-prop-match (point))))
5570 ;; Find the next `c-decl-prefix-or-start-re' match if we haven't
5571 ;; got one already.
5572 (unless cfd-re-match
5574 (if (> cfd-re-match-end (point))
5575 (goto-char cfd-re-match-end))
5577 ;; Each time round, the next `while' moves forward over a pseudo match
5578 ;; of `c-decl-prefix-or-start-re' which is either inside a literal, or
5579 ;; is a ":" not preceded by "public", etc.. `cfd-re-match' and
5580 ;; `cfd-re-match-end' get set.
5581 (while
5582 (progn
5583 (setq cfd-re-match-end (re-search-forward c-decl-prefix-or-start-re
5584 cfd-limit 'move))
5585 (cond
5586 ((null cfd-re-match-end)
5587 ;; No match. Finish up and exit the loop.
5588 (setq cfd-re-match cfd-limit)
5589 nil)
5590 ((c-got-face-at
5591 (if (setq cfd-re-match (match-end 1))
5592 ;; Matched the end of a token preceding a decl spot.
5593 (progn
5594 (goto-char cfd-re-match)
5595 (1- cfd-re-match))
5596 ;; Matched a token that start a decl spot.
5597 (goto-char (match-beginning 0))
5598 (point))
5599 c-literal-faces)
5600 ;; Pseudo match inside a comment or string literal. Skip out
5601 ;; of comments and string literals.
5602 (while (progn
5603 (goto-char (c-next-single-property-change
5604 (point) 'face nil cfd-limit))
5605 (and (< (point) cfd-limit)
5606 (c-got-face-at (point) c-literal-faces))))
5607 t) ; Continue the loop over pseudo matches.
5608 ((and c-opt-identifier-concat-key
5609 (match-string 1)
5610 (save-excursion
5611 (goto-char (match-beginning 1))
5612 (save-match-data
5613 (looking-at c-opt-identifier-concat-key))))
5614 ;; Found, e.g., "::" in C++
5616 ((and (match-string 1)
5617 (string= (match-string 1) ":")
5618 (save-excursion
5619 (or (/= (c-backward-token-2 2) 0) ; no search limit. :-(
5620 (not (looking-at c-decl-start-colon-kwd-re)))))
5621 ;; Found a ":" which isn't part of "public:", etc.
5623 (t nil)))) ;; Found a real match. Exit the pseudo-match loop.
5625 ;; If our match was at the decl start, we have to back up over the
5626 ;; preceding syntactic ws to set `cfd-match-pos' and to catch
5627 ;; any decl spots in the syntactic ws.
5628 (unless cfd-re-match
5629 (c-backward-syntactic-ws)
5630 (setq cfd-re-match (point))))
5632 ;; Choose whichever match is closer to the start.
5633 (if (< cfd-re-match cfd-prop-match)
5634 (setq cfd-match-pos cfd-re-match
5635 cfd-re-match nil)
5636 (setq cfd-match-pos cfd-prop-match
5637 cfd-prop-match nil))
5638 (setq cfd-top-level (c-bs-at-toplevel-p cfd-match-pos))
5640 (goto-char cfd-match-pos)
5642 (when (< cfd-match-pos cfd-limit)
5643 ;; Skip forward past comments only so we don't skip macros.
5644 (c-forward-comments)
5645 ;; Set the position to continue at. We can avoid going over
5646 ;; the comments skipped above a second time, but it's possible
5647 ;; that the comment skipping has taken us past `cfd-prop-match'
5648 ;; since the property might be used inside comments.
5649 (setq cfd-continue-pos (if cfd-prop-match
5650 (min cfd-prop-match (point))
5651 (point))))))
5653 (defun c-find-decl-spots (cfd-limit cfd-decl-re cfd-face-checklist cfd-fun)
5654 ;; Call CFD-FUN for each possible spot for a declaration, cast or
5655 ;; label from the point to CFD-LIMIT.
5657 ;; CFD-FUN is called with point at the start of the spot. It's passed two
5658 ;; arguments: The first is the end position of the token preceding the spot,
5659 ;; or 0 for the implicit match at bob. The second is a flag that is t when
5660 ;; the match is inside a macro. Point should be moved forward by at least
5661 ;; one token.
5663 ;; If CFD-FUN adds `c-decl-end' properties somewhere below the current spot,
5664 ;; it should return non-nil to ensure that the next search will find them.
5666 ;; Such a spot is:
5667 ;; o The first token after bob.
5668 ;; o The first token after the end of submatch 1 in
5669 ;; `c-decl-prefix-or-start-re' when that submatch matches. This
5670 ;; submatch is typically a (L or R) brace or paren, a ;, or a ,.
5671 ;; o The start of each `c-decl-prefix-or-start-re' match when
5672 ;; submatch 1 doesn't match. This is, for example, the keyword
5673 ;; "class" in Pike.
5674 ;; o The start of a previously recognized declaration; "recognized"
5675 ;; means that the last char of the previous token has a `c-type'
5676 ;; text property with the value `c-decl-end'; this only holds
5677 ;; when `c-type-decl-end-used' is set.
5679 ;; Only a spot that match CFD-DECL-RE and whose face is in the
5680 ;; CFD-FACE-CHECKLIST list causes CFD-FUN to be called. The face
5681 ;; check is disabled if CFD-FACE-CHECKLIST is nil.
5683 ;; If the match is inside a macro then the buffer is narrowed to the
5684 ;; end of it, so that CFD-FUN can investigate the following tokens
5685 ;; without matching something that begins inside a macro and ends
5686 ;; outside it. It's to avoid this work that the CFD-DECL-RE and
5687 ;; CFD-FACE-CHECKLIST checks exist.
5689 ;; The spots are visited approximately in order from top to bottom.
5690 ;; It's however the positions where `c-decl-prefix-or-start-re'
5691 ;; matches and where `c-decl-end' properties are found that are in
5692 ;; order. Since the spots often are at the following token, they
5693 ;; might be visited out of order insofar as more spots are reported
5694 ;; later on within the syntactic whitespace between the match
5695 ;; positions and their spots.
5697 ;; It's assumed that comments and strings are fontified in the
5698 ;; searched range.
5700 ;; This is mainly used in fontification, and so has an elaborate
5701 ;; cache to handle repeated calls from the same start position; see
5702 ;; the variables above.
5704 ;; All variables in this function begin with `cfd-' to avoid name
5705 ;; collision with the (dynamically bound) variables used in CFD-FUN.
5707 ;; This function might do hidden buffer changes.
5709 (let ((cfd-start-pos (point)) ; never changed
5710 (cfd-buffer-end (point-max))
5711 ;; The end of the token preceding the decl spot last found
5712 ;; with `c-decl-prefix-or-start-re'. `cfd-limit' if there's
5713 ;; no match.
5714 cfd-re-match
5715 ;; The end position of the last `c-decl-prefix-or-start-re'
5716 ;; match. If this is greater than `cfd-continue-pos', the
5717 ;; next regexp search is started here instead.
5718 (cfd-re-match-end (point-min))
5719 ;; The end of the last `c-decl-end' found by
5720 ;; `c-find-decl-prefix-search'. `cfd-limit' if there's no
5721 ;; match. If searching for the property isn't needed then we
5722 ;; disable it by setting it to `cfd-limit' directly.
5723 (cfd-prop-match (unless c-type-decl-end-used cfd-limit))
5724 ;; The end of the token preceding the decl spot last found by
5725 ;; `c-find-decl-prefix-search'. 0 for the implicit match at
5726 ;; bob. `cfd-limit' if there's no match. In other words,
5727 ;; this is the minimum of `cfd-re-match' and `cfd-prop-match'.
5728 (cfd-match-pos cfd-limit)
5729 ;; The position to continue searching at.
5730 cfd-continue-pos
5731 ;; The position of the last "real" token we've stopped at.
5732 ;; This can be greater than `cfd-continue-pos' when we get
5733 ;; hits inside macros or at `c-decl-end' positions inside
5734 ;; comments.
5735 (cfd-token-pos 0)
5736 ;; The end position of the last entered macro.
5737 (cfd-macro-end 0)
5738 ;; Whether the last position returned from `c-find-decl-prefix-search'
5739 ;; is at the top-level (including directly in a class or namespace,
5740 ;; etc.).
5741 (cfd-top-level (c-bs-at-toplevel-p (point))))
5743 ;; Initialize by finding a syntactically relevant start position
5744 ;; before the point, and do the first `c-decl-prefix-or-start-re'
5745 ;; search unless we're at bob.
5747 (let (start-in-literal start-in-macro syntactic-pos)
5748 ;; Must back up a bit since we look for the end of the previous
5749 ;; statement or declaration, which is earlier than the first
5750 ;; returned match.
5752 ;; This `cond' moves back over any literals or macros. It has special
5753 ;; handling for when the region being searched is entirely within a
5754 ;; macro. It sets `cfd-continue-pos' (unless we've reached
5755 ;; `cfd-limit').
5756 (cond
5757 ;; First we need to move to a syntactically relevant position.
5758 ;; Begin by backing out of comment or string literals.
5760 ;; This arm of the cond actually triggers if we're in a literal,
5761 ;; and cfd-limit is at most at BONL.
5762 ((and
5763 ;; This arm of the `and' moves backwards out of a literal when
5764 ;; the face at point is a literal face. In this case, its value
5765 ;; is always non-nil.
5766 (when (c-got-face-at (point) c-literal-faces)
5767 ;; Try to use the faces to back up to the start of the
5768 ;; literal. FIXME: What if the point is on a declaration
5769 ;; inside a comment?
5770 (while (and (not (bobp))
5771 (c-got-face-at (1- (point)) c-literal-faces))
5772 (goto-char (previous-single-property-change
5773 (point) 'face nil (point-min))))
5775 ;; XEmacs doesn't fontify the quotes surrounding string
5776 ;; literals.
5777 (and (featurep 'xemacs)
5778 (eq (get-text-property (point) 'face)
5779 'font-lock-string-face)
5780 (not (bobp))
5781 (progn (backward-char)
5782 (not (looking-at c-string-limit-regexp)))
5783 (forward-char))
5785 ;; Don't trust the literal to contain only literal faces
5786 ;; (the font lock package might not have fontified the
5787 ;; start of it at all, for instance) so check that we have
5788 ;; arrived at something that looks like a start or else
5789 ;; resort to `c-literal-limits'.
5790 (unless (looking-at c-literal-start-regexp)
5791 (let ((lit-start (c-literal-start)))
5792 (if lit-start (goto-char lit-start)))
5795 (setq start-in-literal (point))) ; end of `and' arm.
5797 ;; The start is in a literal. If the limit is in the same
5798 ;; one we don't have to find a syntactic position etc. We
5799 ;; only check that if the limit is at or before bonl to save
5800 ;; time; it covers the by far most common case when font-lock
5801 ;; refontifies the current line only.
5802 (<= cfd-limit (c-point 'bonl cfd-start-pos))
5803 (save-excursion
5804 (goto-char cfd-start-pos)
5805 (while (progn
5806 (goto-char (c-next-single-property-change
5807 (point) 'face nil cfd-limit))
5808 (and (< (point) cfd-limit)
5809 (c-got-face-at (point) c-literal-faces))))
5810 (= (point) cfd-limit))) ; end of `cond' arm condition
5812 ;; Completely inside a literal. Set up variables to trig the
5813 ;; (< cfd-continue-pos cfd-start-pos) case below and it'll
5814 ;; find a suitable start position.
5815 (setq cfd-continue-pos start-in-literal)) ; end of `cond' arm
5817 ;; Check if the region might be completely inside a macro, to
5818 ;; optimize that like the completely-inside-literal above.
5819 ((save-excursion
5820 (and (= (forward-line 1) 0)
5821 (bolp) ; forward-line has funny behavior at eob.
5822 (>= (point) cfd-limit)
5823 (progn (backward-char)
5824 (eq (char-before) ?\\))))
5825 ;; (Maybe) completely inside a macro. Only need to trig the
5826 ;; (< cfd-continue-pos cfd-start-pos) case below to make it
5827 ;; set things up.
5828 (setq cfd-continue-pos (1- cfd-start-pos)
5829 start-in-macro t))
5831 ;; The default arm of the `cond' moves back over any macro we're in
5832 ;; and over any syntactic WS. It sets `c-find-decl-syntactic-pos'.
5834 ;; Back out of any macro so we don't miss any declaration
5835 ;; that could follow after it.
5836 (when (c-beginning-of-macro)
5837 (setq start-in-macro t))
5839 ;; Now we're at a proper syntactically relevant position so we
5840 ;; can use the cache. But first clear it if it applied
5841 ;; further down.
5842 (c-invalidate-find-decl-cache cfd-start-pos)
5844 (setq syntactic-pos (point))
5845 (unless (eq syntactic-pos c-find-decl-syntactic-pos)
5846 ;; Don't have to do this if the cache is relevant here,
5847 ;; typically if the same line is refontified again. If
5848 ;; we're just some syntactic whitespace further down we can
5849 ;; still use the cache to limit the skipping.
5850 (c-backward-syntactic-ws c-find-decl-syntactic-pos))
5852 ;; If we hit `c-find-decl-syntactic-pos' and
5853 ;; `c-find-decl-match-pos' is set then we install the cached
5854 ;; values. If we hit `c-find-decl-syntactic-pos' and
5855 ;; `c-find-decl-match-pos' is nil then we know there's no decl
5856 ;; prefix in the whitespace before `c-find-decl-syntactic-pos'
5857 ;; and so we can continue the search from this point. If we
5858 ;; didn't hit `c-find-decl-syntactic-pos' then we're now in
5859 ;; the right spot to begin searching anyway.
5860 (if (and (eq (point) c-find-decl-syntactic-pos)
5861 c-find-decl-match-pos)
5862 (setq cfd-match-pos c-find-decl-match-pos
5863 cfd-continue-pos syntactic-pos)
5865 (setq c-find-decl-syntactic-pos syntactic-pos)
5867 (when (if (bobp)
5868 ;; Always consider bob a match to get the first
5869 ;; declaration in the file. Do this separately instead of
5870 ;; letting `c-decl-prefix-or-start-re' match bob, so that
5871 ;; regexp always can consume at least one character to
5872 ;; ensure that we won't get stuck in an infinite loop.
5873 (setq cfd-re-match 0)
5874 (backward-char)
5875 (c-beginning-of-current-token)
5876 (< (point) cfd-limit))
5877 ;; Do an initial search now. In the bob case above it's
5878 ;; only done to search for a `c-decl-end' spot.
5879 (c-find-decl-prefix-search)) ; sets cfd-continue-pos
5881 (setq c-find-decl-match-pos (and (< cfd-match-pos cfd-start-pos)
5882 cfd-match-pos))))) ; end of `cond'
5884 ;; Advance `cfd-continue-pos' if it's before the start position.
5885 ;; The closest continue position that might have effect at or
5886 ;; after the start depends on what we started in. This also
5887 ;; finds a suitable start position in the special cases when the
5888 ;; region is completely within a literal or macro.
5889 (when (and cfd-continue-pos (< cfd-continue-pos cfd-start-pos))
5891 (cond
5892 (start-in-macro
5893 ;; If we're in a macro then it's the closest preceding token
5894 ;; in the macro. Check this before `start-in-literal',
5895 ;; since if we're inside a literal in a macro, the preceding
5896 ;; token is earlier than any `c-decl-end' spot inside the
5897 ;; literal (comment).
5898 (goto-char (or start-in-literal cfd-start-pos))
5899 ;; The only syntactic ws in macros are comments.
5900 (c-backward-comments)
5901 (backward-char)
5902 (c-beginning-of-current-token))
5904 (start-in-literal
5905 ;; If we're in a comment it can only be the closest
5906 ;; preceding `c-decl-end' position within that comment, if
5907 ;; any. Go back to the beginning of such a property so that
5908 ;; `c-find-decl-prefix-search' will find the end of it.
5909 ;; (Can't stop at the end and install it directly on
5910 ;; `cfd-prop-match' since that variable might be cleared
5911 ;; after `cfd-fun' below.)
5913 ;; Note that if the literal is a string then the property
5914 ;; search will simply skip to the beginning of it right
5915 ;; away.
5916 (if (not c-type-decl-end-used)
5917 (goto-char start-in-literal)
5918 (goto-char cfd-start-pos)
5919 (while (progn
5920 (goto-char (previous-single-property-change
5921 (point) 'c-type nil start-in-literal))
5922 (and (> (point) start-in-literal)
5923 (not (eq (c-get-char-property (point) 'c-type)
5924 'c-decl-end))))))
5926 (when (= (point) start-in-literal)
5927 ;; Didn't find any property inside the comment, so we can
5928 ;; skip it entirely. (This won't skip past a string, but
5929 ;; that'll be handled quickly by the next
5930 ;; `c-find-decl-prefix-search' anyway.)
5931 (c-forward-single-comment)
5932 (if (> (point) cfd-limit)
5933 (goto-char cfd-limit))))
5936 ;; If we started in normal code, the only match that might
5937 ;; apply before the start is what we already got in
5938 ;; `cfd-match-pos' so we can continue at the start position.
5939 ;; (Note that we don't get here if the first match is below
5940 ;; it.)
5941 (goto-char cfd-start-pos))) ; end of `cond'
5943 ;; Delete found matches if they are before our new continue
5944 ;; position, so that `c-find-decl-prefix-search' won't back up
5945 ;; to them later on.
5946 (setq cfd-continue-pos (point))
5947 (when (and cfd-re-match (< cfd-re-match cfd-continue-pos))
5948 (setq cfd-re-match nil))
5949 (when (and cfd-prop-match (< cfd-prop-match cfd-continue-pos))
5950 (setq cfd-prop-match nil))) ; end of `when'
5952 (if syntactic-pos
5953 ;; This is the normal case and we got a proper syntactic
5954 ;; position. If there's a match then it's always outside
5955 ;; macros and comments, so advance to the next token and set
5956 ;; `cfd-token-pos'. The loop below will later go back using
5957 ;; `cfd-continue-pos' to fix declarations inside the
5958 ;; syntactic ws.
5959 (when (and cfd-match-pos (< cfd-match-pos syntactic-pos))
5960 (goto-char syntactic-pos)
5961 (c-forward-syntactic-ws)
5962 (and cfd-continue-pos
5963 (< cfd-continue-pos (point))
5964 (setq cfd-token-pos (point))))
5966 ;; Have one of the special cases when the region is completely
5967 ;; within a literal or macro. `cfd-continue-pos' is set to a
5968 ;; good start position for the search, so do it.
5969 (c-find-decl-prefix-search)))
5971 ;; Now loop, one decl spot per iteration. We already have the first
5972 ;; match in `cfd-match-pos'.
5973 (while (progn
5974 ;; Go forward over "false matches", one per iteration.
5975 (while (and
5976 (< cfd-match-pos cfd-limit)
5979 ;; Kludge to filter out matches on the "<" that
5980 ;; aren't open parens, for the sake of languages
5981 ;; that got `c-recognize-<>-arglists' set.
5982 (and (eq (char-before cfd-match-pos) ?<)
5983 (not (c-get-char-property (1- cfd-match-pos)
5984 'syntax-table)))
5986 ;; If `cfd-continue-pos' is less or equal to
5987 ;; `cfd-token-pos', we've got a hit inside a macro
5988 ;; that's in the syntactic whitespace before the last
5989 ;; "real" declaration we've checked. If they're equal
5990 ;; we've arrived at the declaration a second time, so
5991 ;; there's nothing to do.
5992 (= cfd-continue-pos cfd-token-pos)
5994 (progn
5995 ;; If `cfd-continue-pos' is less than `cfd-token-pos'
5996 ;; we're still searching for declarations embedded in
5997 ;; the syntactic whitespace. In that case we need
5998 ;; only to skip comments and not macros, since they
5999 ;; can't be nested, and that's already been done in
6000 ;; `c-find-decl-prefix-search'.
6001 (when (> cfd-continue-pos cfd-token-pos)
6002 (c-forward-syntactic-ws)
6003 (setq cfd-token-pos (point)))
6005 ;; Continue if the following token fails the
6006 ;; CFD-DECL-RE and CFD-FACE-CHECKLIST checks.
6007 (when (or (>= (point) cfd-limit)
6008 (not (looking-at cfd-decl-re))
6009 (and cfd-face-checklist
6010 (not (c-got-face-at
6011 (point) cfd-face-checklist))))
6012 (goto-char cfd-continue-pos)
6013 t)))
6015 (< (point) cfd-limit)) ; end of "false matches" condition
6016 (c-find-decl-prefix-search)) ; end of "false matches" loop
6018 (< (point) cfd-limit)) ; end of condition for "decl-spot" while
6020 (when (and
6021 (>= (point) cfd-start-pos)
6023 (progn
6024 ;; Narrow to the end of the macro if we got a hit inside
6025 ;; one, to avoid recognizing things that start inside the
6026 ;; macro and end outside it.
6027 (when (> cfd-match-pos cfd-macro-end)
6028 ;; Not in the same macro as in the previous round.
6029 (save-excursion
6030 (goto-char cfd-match-pos)
6031 (setq cfd-macro-end
6032 (if (save-excursion (and (c-beginning-of-macro)
6033 (< (point) cfd-match-pos)))
6034 (progn (c-end-of-macro)
6035 (point))
6036 0))))
6038 (if (zerop cfd-macro-end)
6040 (if (> cfd-macro-end (point))
6041 (progn (narrow-to-region (point-min) cfd-macro-end)
6043 ;; The matched token was the last thing in the macro,
6044 ;; so the whole match is bogus.
6045 (setq cfd-macro-end 0)
6046 nil)))) ; end of when condition
6048 (c-debug-put-decl-spot-faces cfd-match-pos (point))
6049 (if (funcall cfd-fun cfd-match-pos (/= cfd-macro-end 0) cfd-top-level)
6050 (setq cfd-prop-match nil))
6052 (when (/= cfd-macro-end 0)
6053 ;; Restore limits if we did macro narrowing above.
6054 (narrow-to-region (point-min) cfd-buffer-end)))
6056 (goto-char cfd-continue-pos)
6057 (if (= cfd-continue-pos cfd-limit)
6058 (setq cfd-match-pos cfd-limit)
6059 (c-find-decl-prefix-search))))) ; Moves point, sets cfd-continue-pos,
6060 ; cfd-match-pos, etc.
6063 ;; A cache for found types.
6065 ;; Buffer local variable that contains an obarray with the types we've
6066 ;; found. If a declaration is recognized somewhere we record the
6067 ;; fully qualified identifier in it to recognize it as a type
6068 ;; elsewhere in the file too. This is not accurate since we do not
6069 ;; bother with the scoping rules of the languages, but in practice the
6070 ;; same name is seldom used as both a type and something else in a
6071 ;; file, and we only use this as a last resort in ambiguous cases (see
6072 ;; `c-forward-decl-or-cast-1').
6074 ;; Not every type need be in this cache. However, things which have
6075 ;; ceased to be types must be removed from it.
6077 ;; Template types in C++ are added here too but with the template
6078 ;; arglist replaced with "<>" in references or "<" for the one in the
6079 ;; primary type. E.g. the type "Foo<A,B>::Bar<C>" is stored as
6080 ;; "Foo<>::Bar<". This avoids storing very long strings (since C++
6081 ;; template specs can be fairly sized programs in themselves) and
6082 ;; improves the hit ratio (it's a type regardless of the template
6083 ;; args; it's just not the same type, but we're only interested in
6084 ;; recognizing types, not telling distinct types apart). Note that
6085 ;; template types in references are added here too; from the example
6086 ;; above there will also be an entry "Foo<".
6087 (defvar c-found-types nil)
6088 (make-variable-buffer-local 'c-found-types)
6090 (defsubst c-clear-found-types ()
6091 ;; Clears `c-found-types'.
6092 (setq c-found-types
6093 (make-hash-table :test #'equal :weakness nil)))
6095 (defun c-add-type (from to)
6096 ;; Add the given region as a type in `c-found-types'. If the region
6097 ;; doesn't match an existing type but there is a type which is equal
6098 ;; to the given one except that the last character is missing, then
6099 ;; the shorter type is removed. That's done to avoid adding all
6100 ;; prefixes of a type as it's being entered and font locked. This
6101 ;; doesn't cover cases like when characters are removed from a type
6102 ;; or added in the middle. We'd need the position of point when the
6103 ;; font locking is invoked to solve this well.
6105 ;; This function might do hidden buffer changes.
6106 (let ((type (c-syntactic-content from to c-recognize-<>-arglists)))
6107 (unless (gethash type c-found-types)
6108 (remhash (substring type 0 -1) c-found-types)
6109 (puthash type t c-found-types))))
6111 (defun c-unfind-type (name)
6112 ;; Remove the "NAME" from c-found-types, if present.
6113 (remhash name c-found-types))
6115 (defsubst c-check-type (from to)
6116 ;; Return non-nil if the given region contains a type in
6117 ;; `c-found-types'.
6119 ;; This function might do hidden buffer changes.
6120 (gethash (c-syntactic-content from to c-recognize-<>-arglists) c-found-types))
6122 (defun c-list-found-types ()
6123 ;; Return all the types in `c-found-types' as a sorted list of
6124 ;; strings.
6125 (let (type-list)
6126 (maphash (lambda (type _)
6127 (setq type-list (cons type type-list)))
6128 c-found-types)
6129 (sort type-list 'string-lessp)))
6131 ;; Shut up the byte compiler.
6132 (defvar c-maybe-stale-found-type)
6134 (defun c-trim-found-types (beg end _old-len)
6135 ;; An after change function which, in conjunction with the info in
6136 ;; c-maybe-stale-found-type (set in c-before-change), removes a type
6137 ;; from `c-found-types', should this type have become stale. For
6138 ;; example, this happens to "foo" when "foo \n bar();" becomes
6139 ;; "foo(); \n bar();". Such stale types, if not removed, foul up
6140 ;; the fontification.
6142 ;; Have we, perhaps, added non-ws characters to the front/back of a found
6143 ;; type?
6144 (when (> end beg)
6145 (save-excursion
6146 (when (< end (point-max))
6147 (goto-char end)
6148 (if (and (c-beginning-of-current-token) ; only moves when we started in the middle
6149 (progn (goto-char end)
6150 (c-end-of-current-token)))
6151 (c-unfind-type (buffer-substring-no-properties
6152 end (point)))))
6153 (when (> beg (point-min))
6154 (goto-char beg)
6155 (if (and (c-end-of-current-token) ; only moves when we started in the middle
6156 (progn (goto-char beg)
6157 (c-beginning-of-current-token)))
6158 (c-unfind-type (buffer-substring-no-properties
6159 (point) beg))))))
6161 (if c-maybe-stale-found-type ; e.g. (c-decl-id-start "foo" 97 107 " (* ooka) " "o")
6162 (cond
6163 ;; Changing the amount of (already existing) whitespace - don't do anything.
6164 ((and (c-partial-ws-p beg end)
6165 (or (= beg end) ; removal of WS
6166 (string-match "^[ \t\n\r\f\v]*$" (nth 5 c-maybe-stale-found-type)))))
6168 ;; The syntactic relationship which defined a "found type" has been
6169 ;; destroyed.
6170 ((eq (car c-maybe-stale-found-type) 'c-decl-id-start)
6171 (c-unfind-type (cadr c-maybe-stale-found-type)))
6172 ;; ((eq (car c-maybe-stale-found-type) 'c-decl-type-start) FIXME!!!
6176 ;; Setting and removing syntax properties on < and > in languages (C++
6177 ;; and Java) where they can be template/generic delimiters as well as
6178 ;; their normal meaning of "less/greater than".
6180 ;; Normally, < and > have syntax 'punctuation'. When they are found to
6181 ;; be delimiters, they are marked as such with the category properties
6182 ;; c-<-as-paren-syntax, c->-as-paren-syntax respectively.
6184 ;; STRATEGY:
6186 ;; It is impossible to determine with certainty whether a <..> pair in
6187 ;; C++ is two comparison operators or is template delimiters, unless
6188 ;; one duplicates a lot of a C++ compiler. For example, the following
6189 ;; code fragment:
6191 ;; foo (a < b, c > d) ;
6193 ;; could be a function call with two integer parameters (each a
6194 ;; relational expression), or it could be a constructor for class foo
6195 ;; taking one parameter d of templated type "a < b, c >". They are
6196 ;; somewhat easier to distinguish in Java.
6198 ;; The strategy now (2010-01) adopted is to mark and unmark < and
6199 ;; > IN MATCHING PAIRS ONLY. [Previously, they were marked
6200 ;; individually when their context so indicated. This gave rise to
6201 ;; intractable problems when one of a matching pair was deleted, or
6202 ;; pulled into a literal.]
6204 ;; At each buffer change, the syntax-table properties are removed in a
6205 ;; before-change function and reapplied, when needed, in an
6206 ;; after-change function. It is far more important that the
6207 ;; properties get removed when they they are spurious than that they
6208 ;; be present when wanted.
6209 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
6210 (defun c-clear-<-pair-props (&optional pos)
6211 ;; POS (default point) is at a < character. If it is marked with
6212 ;; open paren syntax-table text property, remove the property,
6213 ;; together with the close paren property on the matching > (if
6214 ;; any).
6215 (save-excursion
6216 (if pos
6217 (goto-char pos)
6218 (setq pos (point)))
6219 (when (equal (c-get-char-property (point) 'syntax-table)
6220 c-<-as-paren-syntax)
6221 (with-syntax-table c-no-parens-syntax-table ; ignore unbalanced [,{,(,..
6222 (c-go-list-forward))
6223 (when (equal (c-get-char-property (1- (point)) 'syntax-table)
6224 c->-as-paren-syntax) ; should always be true.
6225 (c-unmark-<->-as-paren (1- (point))))
6226 (c-unmark-<->-as-paren pos))))
6228 (defun c-clear->-pair-props (&optional pos)
6229 ;; POS (default point) is at a > character. If it is marked with
6230 ;; close paren syntax-table property, remove the property, together
6231 ;; with the open paren property on the matching < (if any).
6232 (save-excursion
6233 (if pos
6234 (goto-char pos)
6235 (setq pos (point)))
6236 (when (equal (c-get-char-property (point) 'syntax-table)
6237 c->-as-paren-syntax)
6238 (with-syntax-table c-no-parens-syntax-table ; ignore unbalanced [,{,(,..
6239 (c-go-up-list-backward))
6240 (when (equal (c-get-char-property (point) 'syntax-table)
6241 c-<-as-paren-syntax) ; should always be true.
6242 (c-unmark-<->-as-paren (point)))
6243 (c-unmark-<->-as-paren pos))))
6245 (defun c-clear-<>-pair-props (&optional pos)
6246 ;; POS (default point) is at a < or > character. If it has an
6247 ;; open/close paren syntax-table property, remove this property both
6248 ;; from the current character and its partner (which will also be
6249 ;; thusly marked).
6250 (cond
6251 ((eq (char-after) ?\<)
6252 (c-clear-<-pair-props pos))
6253 ((eq (char-after) ?\>)
6254 (c-clear->-pair-props pos))
6255 (t (c-benign-error
6256 "c-clear-<>-pair-props called from wrong position"))))
6258 (defun c-clear-<-pair-props-if-match-after (lim &optional pos)
6259 ;; POS (default point) is at a < character. If it is both marked
6260 ;; with open/close paren syntax-table property, and has a matching >
6261 ;; (also marked) which is after LIM, remove the property both from
6262 ;; the current > and its partner. Return t when this happens, nil
6263 ;; when it doesn't.
6264 (save-excursion
6265 (if pos
6266 (goto-char pos)
6267 (setq pos (point)))
6268 (when (equal (c-get-char-property (point) 'syntax-table)
6269 c-<-as-paren-syntax)
6270 (with-syntax-table c-no-parens-syntax-table ; ignore unbalanced [,{,(,..
6271 (c-go-list-forward))
6272 (when (and (>= (point) lim)
6273 (equal (c-get-char-property (1- (point)) 'syntax-table)
6274 c->-as-paren-syntax)) ; should always be true.
6275 (c-unmark-<->-as-paren (1- (point)))
6276 (c-unmark-<->-as-paren pos))
6277 t)))
6279 (defun c-clear->-pair-props-if-match-before (lim &optional pos)
6280 ;; POS (default point) is at a > character. If it is both marked
6281 ;; with open/close paren syntax-table property, and has a matching <
6282 ;; (also marked) which is before LIM, remove the property both from
6283 ;; the current < and its partner. Return t when this happens, nil
6284 ;; when it doesn't.
6285 (save-excursion
6286 (if pos
6287 (goto-char pos)
6288 (setq pos (point)))
6289 (when (equal (c-get-char-property (point) 'syntax-table)
6290 c->-as-paren-syntax)
6291 (with-syntax-table c-no-parens-syntax-table ; ignore unbalanced [,{,(,..
6292 (c-go-up-list-backward))
6293 (when (and (<= (point) lim)
6294 (equal (c-get-char-property (point) 'syntax-table)
6295 c-<-as-paren-syntax)) ; should always be true.
6296 (c-unmark-<->-as-paren (point))
6297 (c-unmark-<->-as-paren pos))
6298 t)))
6300 ;; Set by c-common-init in cc-mode.el.
6301 (defvar c-new-BEG)
6302 (defvar c-new-END)
6303 ;; Set by c-after-change in cc-mode.el.
6304 (defvar c-old-BEG)
6305 (defvar c-old-END)
6307 (defun c-before-change-check-<>-operators (beg end)
6308 ;; Unmark certain pairs of "< .... >" which are currently marked as
6309 ;; template/generic delimiters. (This marking is via syntax-table text
6310 ;; properties), and expand the (c-new-BEG c-new-END) region to include all
6311 ;; unmarked < and > operators within the certain bounds (see below).
6313 ;; These pairs are those which are in the current "statement" (i.e.,
6314 ;; the region between the {, }, or ; before BEG and the one after
6315 ;; END), and which enclose any part of the interval (BEG END).
6317 ;; Note that in C++ (?and Java), template/generic parens cannot
6318 ;; enclose a brace or semicolon, so we use these as bounds on the
6319 ;; region we must work on.
6321 ;; This function is called from before-change-functions (via
6322 ;; c-get-state-before-change-functions). Thus the buffer is widened,
6323 ;; and point is undefined, both at entry and exit.
6325 ;; FIXME!!! This routine ignores the possibility of macros entirely.
6326 ;; 2010-01-29.
6327 (save-excursion
6328 (c-save-buffer-state
6329 ((beg-lit-start (progn (goto-char beg) (c-literal-start)))
6330 (end-lit-limits (progn (goto-char end) (c-literal-limits)))
6331 new-beg new-end beg-limit end-limit)
6332 ;; Locate the earliest < after the barrier before the changed region,
6333 ;; which isn't already marked as a paren.
6334 (goto-char (or beg-lit-start beg))
6335 (setq beg-limit (c-determine-limit 512))
6337 ;; Remove the syntax-table/category properties from each pertinent <...>
6338 ;; pair. Firstly, the ones with the < before beg and > after beg....
6339 (while (progn (c-syntactic-skip-backward "^;{}<" beg-limit)
6340 (eq (char-before) ?<))
6341 (c-backward-token-2)
6342 (when (eq (char-after) ?<)
6343 (c-clear-<-pair-props-if-match-after beg)
6344 (setq new-beg (point))))
6345 (c-forward-syntactic-ws)
6347 ;; ...Then the ones with < before end and > after end.
6348 (goto-char (if end-lit-limits (cdr end-lit-limits) end))
6349 (setq end-limit (c-determine-+ve-limit 512))
6350 (while (and (c-syntactic-re-search-forward "[;{}>]" end-limit 'end)
6351 (eq (char-before) ?>))
6352 (c-end-of-current-token)
6353 (when (eq (char-before) ?>)
6354 (c-clear->-pair-props-if-match-before end (1- (point)))
6355 (setq new-end (point))))
6356 (c-backward-syntactic-ws)
6358 ;; Extend the fontification region, if needed.
6359 (and new-beg
6360 (< new-beg c-new-BEG)
6361 (setq c-new-BEG new-beg))
6362 (and new-end
6363 (> new-end c-new-END)
6364 (setq c-new-END new-end)))))
6366 (defun c-after-change-check-<>-operators (beg end)
6367 ;; This is called from `after-change-functions' when
6368 ;; c-recognize-<>-arglists' is set. It ensures that no "<" or ">"
6369 ;; chars with paren syntax become part of another operator like "<<"
6370 ;; or ">=".
6372 ;; This function might do hidden buffer changes.
6374 (save-excursion
6375 (goto-char beg)
6376 (when (or (looking-at "[<>]")
6377 (< (skip-chars-backward "<>") 0))
6379 (goto-char beg)
6380 (c-beginning-of-current-token)
6381 (when (and (< (point) beg)
6382 (looking-at c-<>-multichar-token-regexp)
6383 (< beg (setq beg (match-end 0))))
6384 (while (progn (skip-chars-forward "^<>" beg)
6385 (< (point) beg))
6386 (c-clear-<>-pair-props)
6387 (forward-char))))
6389 (when (< beg end)
6390 (goto-char end)
6391 (when (or (looking-at "[<>]")
6392 (< (skip-chars-backward "<>") 0))
6394 (goto-char end)
6395 (c-beginning-of-current-token)
6396 (when (and (< (point) end)
6397 (looking-at c-<>-multichar-token-regexp)
6398 (< end (setq end (match-end 0))))
6399 (while (progn (skip-chars-forward "^<>" end)
6400 (< (point) end))
6401 (c-clear-<>-pair-props)
6402 (forward-char)))))))
6404 (defvar c-restricted-<>-arglists) ;FIXME: Move definition here?
6405 (defvar c-parse-and-markup-<>-arglists) ;FIXME: Move definition here?
6407 (defun c-restore-<>-properties (_beg _end _old-len)
6408 ;; This function is called as an after-change function. It restores the
6409 ;; category/syntax-table properties on template/generic <..> pairs between
6410 ;; c-new-BEG and c-new-END. It may do hidden buffer changes.
6411 (c-save-buffer-state ((c-parse-and-markup-<>-arglists t)
6412 c-restricted-<>-arglists lit-limits)
6413 (goto-char c-new-BEG)
6414 (if (setq lit-limits (c-literal-limits))
6415 (goto-char (cdr lit-limits)))
6416 (while (and (< (point) c-new-END)
6417 (c-syntactic-re-search-forward "<" c-new-END 'bound))
6418 (backward-char)
6419 (save-excursion
6420 (c-backward-token-2)
6421 (setq c-restricted-<>-arglists
6422 (and (not (looking-at c-opt-<>-sexp-key))
6423 (progn (c-backward-syntactic-ws) ; to ( or ,
6424 (and (memq (char-before) '(?\( ?,)) ; what about <?
6425 (not (eq (c-get-char-property (point) 'c-type)
6426 'c-decl-arg-start)))))))
6427 (or (c-forward-<>-arglist nil)
6428 (forward-char)))))
6431 ;; Functions to handle C++ raw strings.
6433 ;; A valid C++ raw string looks like
6434 ;; R"<id>(<contents>)<id>"
6435 ;; , where <id> is an identifier from 0 to 16 characters long, not containing
6436 ;; spaces, control characters, double quote or left/right paren. <contents>
6437 ;; can include anything which isn't the terminating )<id>", including new
6438 ;; lines, "s, parentheses, etc.
6440 ;; CC Mode handles C++ raw strings by the use of `syntax-table' text
6441 ;; properties as follows:
6443 ;; (i) On a validly terminated raw string, no `syntax-table' text properties
6444 ;; are applied to the opening and closing delimiters, but any " in the
6445 ;; contents is given the property value "punctuation" (`(1)') to prevent it
6446 ;; interacting with the "s in the delimiters.
6448 ;; The font locking routine `c-font-lock-c++-raw-strings' (in cc-fonts.el)
6449 ;; recognizes valid raw strings, and fontifies the delimiters (apart from
6450 ;; the parentheses) with the default face and the parentheses and the
6451 ;; <contents> with font-lock-string-face.
6453 ;; (ii) A valid, but unterminated, raw string opening delimiter gets the
6454 ;; "punctuation" value (`(1)') of the `syntax-table' text property, and the
6455 ;; open parenthesis gets the "string fence" value (`(15)').
6457 ;; `c-font-lock-c++-raw-strings' puts c-font-lock-warning-face on the entire
6458 ;; unmatched opening delimiter (from the R up to the open paren), and allows
6459 ;; the rest of the buffer to get font-lock-string-face, caused by the
6460 ;; unmatched "string fence" `syntax-table' text property value.
6462 ;; (iii) Inside a macro, a valid raw string is handled as in (i). An
6463 ;; unmatched opening delimiter is handled slightly differently. In addition
6464 ;; to the "punctuation" and "string fence" properties on the delimiter,
6465 ;; another "string fence" `syntax-table' property is applied to the last
6466 ;; possible character of the macro before the terminating linefeed (if there
6467 ;; is such a character after the "("). This "last possible" character is
6468 ;; never a backslash escaping the end of line. If the character preceding
6469 ;; this "last possible" character is itself a backslash, this preceding
6470 ;; character gets a "punctuation" `syntax-table' value. If the "(" is
6471 ;; already at the end of the macro, it gets the "punctuation" value, and no
6472 ;; "string fence"s are used.
6474 ;; The effect on the fontification of either of these tactics is that rest of
6475 ;; the macro (if any) after the "(" gets font-lock-string-face, but the rest
6476 ;; of the file is fontified normally.
6479 (defun c-raw-string-pos ()
6480 ;; Get POINT's relationship to any containing raw string.
6481 ;; If point isn't in a raw string, return nil.
6482 ;; Otherwise, return the following list:
6484 ;; (POS B\" B\( E\) E\")
6486 ;; , where POS is the symbol `open-delim' if point is in the opening
6487 ;; delimiter, the symbol `close-delim' if it's in the closing delimiter, and
6488 ;; nil if it's in the string body. B\", B\(, E\), E\" are the positions of
6489 ;; the opening and closing quotes and parentheses of a correctly terminated
6490 ;; raw string. (N.B.: E\) and E\" are NOT on the "outside" of these
6491 ;; characters.) If the raw string is not terminated, E\) and E\" are set to
6492 ;; nil.
6494 ;; Note: this routine is dependant upon the correct syntax-table text
6495 ;; properties being set.
6496 (let ((state (c-state-semi-pp-to-literal (point)))
6497 open-quote-pos open-paren-pos close-paren-pos close-quote-pos id)
6498 (save-excursion
6499 (when
6500 (and
6501 (cond
6502 ((null (cadr state))
6503 (or (eq (char-after) ?\")
6504 (search-backward "\"" (max (- (point) 17) (point-min)) t)))
6505 ((and (eq (cadr state) 'string)
6506 (goto-char (nth 2 state))
6507 (or (eq (char-after) ?\")
6508 (search-backward "\"" (max (- (point) 17) (point-min)) t))
6509 (not (bobp)))))
6510 (eq (char-before) ?R)
6511 (looking-at "\"\\([^ ()\\\n\r\t]\\{0,16\\}\\)("))
6512 (setq open-quote-pos (point)
6513 open-paren-pos (match-end 1)
6514 id (match-string-no-properties 1))
6515 (goto-char (1+ open-paren-pos))
6516 (when (and (not (c-get-char-property open-paren-pos 'syntax-table))
6517 (search-forward (concat ")" id "\"") nil t))
6518 (setq close-paren-pos (match-beginning 0)
6519 close-quote-pos (1- (point))))))
6520 (and open-quote-pos
6521 (list
6522 (cond
6523 ((<= (point) open-paren-pos)
6524 'open-delim)
6525 ((and close-paren-pos
6526 (> (point) close-paren-pos))
6527 'close-delim)
6528 (t nil))
6529 open-quote-pos open-paren-pos close-paren-pos close-quote-pos))))
6531 (defun c-depropertize-raw-string (id open-quote open-paren bound)
6532 ;; Point is immediately after a raw string opening delimiter. Remove any
6533 ;; `syntax-table' text properties associated with the delimiter (if it's
6534 ;; unmatched) or the raw string.
6536 ;; ID, a string, is the delimiter's identifier. OPEN-QUOTE and OPEN-PAREN
6537 ;; are the buffer positions of the delimiter's components. BOUND is the
6538 ;; bound for searching for a matching closing delimiter; it is usually nil,
6539 ;; but if we're inside a macro, it's the end of the macro.
6541 ;; Point is moved to after the (terminated) raw string, or left after the
6542 ;; unmatched opening delimiter, as the case may be. The return value is of
6543 ;; no significance.
6544 (let ((open-paren-prop (c-get-char-property open-paren 'syntax-table)))
6545 (cond
6546 ((null open-paren-prop)
6547 ;; A terminated raw string
6548 (when (search-forward (concat ")" id "\"") nil t)
6549 (let* ((closing-paren (match-beginning 0))
6550 (first-punctuation
6551 (save-match-data
6552 (goto-char (1+ open-paren))
6553 (and (c-search-forward-char-property 'syntax-table '(1)
6554 closing-paren)
6555 (1- (point)))))
6557 (when first-punctuation
6558 (c-clear-char-property-with-value
6559 first-punctuation (match-beginning 0) 'syntax-table '(1))
6560 (c-truncate-semi-nonlit-pos-cache first-punctuation)
6561 ))))
6562 ((or (and (equal open-paren-prop '(15)) (null bound))
6563 (equal open-paren-prop '(1)))
6564 ;; An unterminated raw string either not in a macro, or in a macro with
6565 ;; the open parenthesis right up against the end of macro
6566 (c-clear-char-property open-quote 'syntax-table)
6567 (c-truncate-semi-nonlit-pos-cache open-quote)
6568 (c-clear-char-property open-paren 'syntax-table))
6570 ;; An unterminated string in a macro, with at least one char after the
6571 ;; open paren
6572 (c-clear-char-property open-quote 'syntax-table)
6573 (c-truncate-semi-nonlit-pos-cache open-quote)
6574 (c-clear-char-property open-paren 'syntax-table)
6575 (let ((after-string-fence-pos
6576 (save-excursion
6577 (goto-char (1+ open-paren))
6578 (c-search-forward-char-property 'syntax-table '(15) bound))))
6579 (when after-string-fence-pos
6580 (c-clear-char-property (1- after-string-fence-pos) 'syntax-table)))
6581 ))))
6583 (defun c-depropertize-raw-strings-in-region (start finish)
6584 ;; Remove any `syntax-table' text properties associated with C++ raw strings
6585 ;; contained in the region (START FINISH). Point is undefined at entry and
6586 ;; exit, and the return value has no significance.
6587 (goto-char start)
6588 (while (and (< (point) finish)
6589 (re-search-forward
6590 (concat "\\(" ; 1
6591 c-anchored-cpp-prefix ; 2
6592 "\\)\\|\\(" ; 3
6593 "R\"\\([^ ()\\\n\r\t]\\{0,16\\}\\)(" ; 4
6594 "\\)")
6595 finish t))
6596 (when (save-excursion
6597 (goto-char (match-beginning 0)) (not (c-in-literal)))
6598 (if (match-beginning 4) ; the id
6599 ;; We've found a raw string
6600 (c-depropertize-raw-string
6601 (match-string-no-properties 4) ; id
6602 (1+ (match-beginning 3)) ; open quote
6603 (match-end 4) ; open paren
6604 nil) ; bound
6605 ;; We've found a CPP construct. Search for raw strings within it.
6606 (goto-char (match-beginning 2)) ; the "#"
6607 (c-end-of-macro)
6608 (let ((eom (point)))
6609 (goto-char (match-end 2)) ; after the "#".
6610 (while (and (< (point) eom)
6611 (c-syntactic-re-search-forward
6612 "R\"\\([^ ()\\\n\r\t]\\{0,16\\}\\)(" eom t))
6613 (c-depropertize-raw-string
6614 (match-string-no-properties 1) ; id
6615 (1+ (match-beginning 0)) ; open quote
6616 (match-end 1) ; open paren
6617 eom))))))) ; bound.
6619 (defun c-before-change-check-raw-strings (beg end)
6620 ;; This function clears `syntax-table' text properties from C++ raw strings
6621 ;; in the region (c-new-BEG c-new-END). BEG and END are the standard
6622 ;; arguments supplied to any before-change function.
6624 ;; Point is undefined on both entry and exit, and the return value has no
6625 ;; significance.
6627 ;; This function is called as a before-change function solely due to its
6628 ;; membership of the C++ value of `c-get-state-before-change-functions'.
6629 (c-save-buffer-state
6630 ((beg-rs (progn (goto-char beg) (c-raw-string-pos)))
6631 (beg-plus (if (null beg-rs)
6633 (max beg
6634 (1+ (or (nth 4 beg-rs) (nth 2 beg-rs))))))
6635 (end-rs (progn (goto-char end) (c-raw-string-pos))) ; FIXME!!!
6636 ; Optimize this so that we don't call
6637 ; `c-raw-string-pos' twice when once
6638 ; will do. (2016-06-02).
6639 (end-minus (if (null end-rs)
6641 (min end (cadr end-rs))))
6643 (when beg-rs
6644 (setq c-new-BEG (min c-new-BEG (1- (cadr beg-rs)))))
6645 (c-depropertize-raw-strings-in-region c-new-BEG beg-plus)
6647 (when end-rs
6648 (setq c-new-END (max c-new-END
6649 (1+ (or (nth 4 end-rs)
6650 (nth 2 end-rs))))))
6651 (c-depropertize-raw-strings-in-region end-minus c-new-END)))
6653 (defun c-propertize-raw-string-opener (id open-quote open-paren bound)
6654 ;; Point is immediately after a raw string opening delimiter. Apply any
6655 ;; pertinent `syntax-table' text properties to the delimiter and also the
6656 ;; raw string, should there be a valid matching closing delimiter.
6658 ;; ID, a string, is the delimiter's identifier. OPEN-QUOTE and OPEN-PAREN
6659 ;; are the buffer positions of the delimiter's components. BOUND is the
6660 ;; bound for searching for a matching closing delimiter; it is usually nil,
6661 ;; but if we're inside a macro, it's the end of the macro.
6663 ;; Point is moved to after the (terminated) raw string, or left after the
6664 ;; unmatched opening delimiter, as the case may be. The return value is of
6665 ;; no significance.
6666 (if (search-forward (concat ")" id "\"") bound t)
6667 (let ((end-string (match-beginning 0))
6668 (after-quote (match-end 0)))
6669 (goto-char open-paren)
6670 (while (progn (skip-syntax-forward "^\"" end-string)
6671 (< (point) end-string))
6672 (c-put-char-property (point) 'syntax-table '(1)) ; punctuation
6673 (c-truncate-semi-nonlit-pos-cache (point))
6674 (forward-char))
6675 (goto-char after-quote))
6676 (c-put-char-property open-quote 'syntax-table '(1)) ; punctuation
6677 (c-truncate-semi-nonlit-pos-cache open-quote)
6678 (c-put-char-property open-paren 'syntax-table '(15)) ; generic string
6679 (when bound
6680 ;; In a CPP construct, we try to apply a generic-string `syntax-table'
6681 ;; text property to the last possible character in the string, so that
6682 ;; only characters within the macro get "stringed out".
6683 (goto-char bound)
6684 (if (save-restriction
6685 (narrow-to-region (1+ open-paren) (point-max))
6686 (re-search-backward
6687 (eval-when-compile
6688 ;; This regular expression matches either an escape pair (which
6689 ;; isn't an escaped NL) (submatch 5) or a non-escaped character
6690 ;; (which isn't itself a backslash) (submatch 10). The long
6691 ;; preambles to these (respectively submatches 2-4 and 6-9)
6692 ;; ensure that we have the correct parity for sequences of
6693 ;; backslashes, etc..
6694 (concat "\\(" ; 1
6695 "\\(\\`[^\\]?\\|[^\\][^\\]\\)\\(\\\\\\(.\\|\n\\)\\)*" ; 2-4
6696 "\\(\\\\.\\)" ; 5
6697 "\\|"
6698 "\\(\\`\\|[^\\]\\|\\(\\`[^\\]?\\|[^\\][^\\]\\)\\(\\\\\\(.\\|\n\\)\\)+\\)" ; 6-9
6699 "\\([^\\]\\)" ; 10
6700 "\\)"
6701 "\\(\\\\\n\\)*\\=")) ; 11
6702 (1+ open-paren) t))
6703 (if (match-beginning 10)
6704 (progn
6705 (c-put-char-property (match-beginning 10) 'syntax-table '(15))
6706 (c-truncate-semi-nonlit-pos-cache (match-beginning 10)))
6707 (c-put-char-property (match-beginning 5) 'syntax-table '(1))
6708 (c-put-char-property (1+ (match-beginning 5)) 'syntax-table '(15))
6709 (c-truncate-semi-nonlit-pos-cache (1+ (match-beginning 5))))
6710 (c-put-char-property open-paren 'syntax-table '(1)))
6711 (goto-char bound))))
6713 (defun c-after-change-re-mark-raw-strings (_beg _end _old-len)
6714 ;; This function applies `syntax-table' text properties to C++ raw strings
6715 ;; beginning in the region (c-new-BEG c-new-END). BEG, END, and OLD-LEN are
6716 ;; the standard arguments supplied to any after-change function.
6718 ;; Point is undefined on both entry and exit, and the return value has no
6719 ;; significance.
6721 ;; This function is called as an after-change function solely due to its
6722 ;; membership of the C++ value of `c-before-font-lock-functions'.
6723 (c-save-buffer-state ()
6724 ;; If the region (c-new-BEG c-new-END) has expanded, remove
6725 ;; `syntax-table' text-properties from the new piece(s).
6726 (when (< c-new-BEG c-old-BEG)
6727 (let ((beg-rs (progn (goto-char c-old-BEG) (c-raw-string-pos))))
6728 (c-depropertize-raw-strings-in-region
6729 c-new-BEG
6730 (if beg-rs
6731 (1+ (or (nth 4 beg-rs) (nth 2 beg-rs)))
6732 c-old-BEG))))
6733 (when (> c-new-END c-old-END)
6734 (let ((end-rs (progn (goto-char c-old-END) (c-raw-string-pos))))
6735 (c-depropertize-raw-strings-in-region
6736 (if end-rs
6737 (cadr end-rs)
6738 c-old-END)
6739 c-new-END)))
6741 (goto-char c-new-BEG)
6742 (while (and (< (point) c-new-END)
6743 (re-search-forward
6744 (concat "\\(" ; 1
6745 c-anchored-cpp-prefix ; 2
6746 "\\)\\|\\(" ; 3
6747 "R\"\\([^ ()\\\n\r\t]\\{0,16\\}\\)(" ; 4
6748 "\\)")
6749 c-new-END t))
6750 (when (save-excursion
6751 (goto-char (match-beginning 0)) (not (c-in-literal)))
6752 (if (match-beginning 4) ; the id
6753 ;; We've found a raw string.
6754 (c-propertize-raw-string-opener
6755 (match-string-no-properties 4) ; id
6756 (1+ (match-beginning 3)) ; open quote
6757 (match-end 4) ; open paren
6758 nil) ; bound
6759 ;; We've found a CPP construct. Search for raw strings within it.
6760 (goto-char (match-beginning 2)) ; the "#"
6761 (c-end-of-macro)
6762 (let ((eom (point)))
6763 (goto-char (match-end 2)) ; after the "#".
6764 (while (and (< (point) eom)
6765 (c-syntactic-re-search-forward
6766 "R\"\\([^ ()\\\n\r\t]\\{0,16\\}\\)(" eom t))
6767 (c-propertize-raw-string-opener
6768 (match-string-no-properties 1) ; id
6769 (1+ (match-beginning 0)) ; open quote
6770 (match-end 1) ; open paren
6771 eom)))))))) ; bound
6774 ;; Handling of small scale constructs like types and names.
6776 ;; Dynamically bound variable that instructs `c-forward-type' to also
6777 ;; treat possible types (i.e. those that it normally returns 'maybe or
6778 ;; 'found for) as actual types (and always return 'found for them).
6779 ;; This means that it records them in `c-record-type-identifiers' if
6780 ;; that is set, and that it adds them to `c-found-types'.
6781 (defvar c-promote-possible-types nil)
6783 ;; Dynamically bound variable that instructs `c-forward-<>-arglist' to
6784 ;; mark up successfully parsed arglists with paren syntax properties on
6785 ;; the surrounding angle brackets and with `c-<>-arg-sep' in the
6786 ;; `c-type' property of each argument separating comma.
6788 ;; Setting this variable also makes `c-forward-<>-arglist' recurse into
6789 ;; all arglists for side effects (i.e. recording types), otherwise it
6790 ;; exploits any existing paren syntax properties to quickly jump to the
6791 ;; end of already parsed arglists.
6793 ;; Marking up the arglists is not the default since doing that correctly
6794 ;; depends on a proper value for `c-restricted-<>-arglists'.
6795 (defvar c-parse-and-markup-<>-arglists nil)
6797 ;; Dynamically bound variable that instructs `c-forward-<>-arglist' to
6798 ;; not accept arglists that contain binary operators.
6800 ;; This is primarily used to handle C++ template arglists. C++
6801 ;; disambiguates them by checking whether the preceding name is a
6802 ;; template or not. We can't do that, so we assume it is a template
6803 ;; if it can be parsed as one. That usually works well since
6804 ;; comparison expressions on the forms "a < b > c" or "a < b, c > d"
6805 ;; in almost all cases would be pointless.
6807 ;; However, in function arglists, e.g. in "foo (a < b, c > d)", we
6808 ;; should let the comma separate the function arguments instead. And
6809 ;; in a context where the value of the expression is taken, e.g. in
6810 ;; "if (a < b || c > d)", it's probably not a template.
6811 (defvar c-restricted-<>-arglists nil)
6813 ;; Dynamically bound variables that instructs
6814 ;; `c-forward-keyword-clause', `c-forward-<>-arglist',
6815 ;; `c-forward-name', `c-forward-type', `c-forward-decl-or-cast-1', and
6816 ;; `c-forward-label' to record the ranges of all the type and
6817 ;; reference identifiers they encounter. They will build lists on
6818 ;; these variables where each element is a cons of the buffer
6819 ;; positions surrounding each identifier. This recording is only
6820 ;; activated when `c-record-type-identifiers' is non-nil.
6822 ;; All known types that can't be identifiers are recorded, and also
6823 ;; other possible types if `c-promote-possible-types' is set.
6824 ;; Recording is however disabled inside angle bracket arglists that
6825 ;; are encountered inside names and other angle bracket arglists.
6826 ;; Such occurrences are taken care of by `c-font-lock-<>-arglists'
6827 ;; instead.
6829 ;; Only the names in C++ template style references (e.g. "tmpl" in
6830 ;; "tmpl<a,b>::foo") are recorded as references, other references
6831 ;; aren't handled here.
6833 ;; `c-forward-label' records the label identifier(s) on
6834 ;; `c-record-ref-identifiers'.
6835 (defvar c-record-type-identifiers nil)
6836 (defvar c-record-ref-identifiers nil)
6838 ;; This variable will receive a cons cell of the range of the last
6839 ;; single identifier symbol stepped over by `c-forward-name' if it's
6840 ;; successful. This is the range that should be put on one of the
6841 ;; record lists above by the caller. It's assigned nil if there's no
6842 ;; such symbol in the name.
6843 (defvar c-last-identifier-range nil)
6845 (defmacro c-record-type-id (range)
6846 (if (eq (car-safe range) 'cons)
6847 ;; Always true.
6848 `(setq c-record-type-identifiers
6849 (cons ,range c-record-type-identifiers))
6850 `(let ((range ,range))
6851 (if range
6852 (setq c-record-type-identifiers
6853 (cons range c-record-type-identifiers))))))
6855 (defmacro c-record-ref-id (range)
6856 (if (eq (car-safe range) 'cons)
6857 ;; Always true.
6858 `(setq c-record-ref-identifiers
6859 (cons ,range c-record-ref-identifiers))
6860 `(let ((range ,range))
6861 (if range
6862 (setq c-record-ref-identifiers
6863 (cons range c-record-ref-identifiers))))))
6865 ;; Dynamically bound variable that instructs `c-forward-type' to
6866 ;; record the ranges of types that only are found. Behaves otherwise
6867 ;; like `c-record-type-identifiers'.
6868 (defvar c-record-found-types nil)
6870 (defmacro c-forward-keyword-prefixed-id (type)
6871 ;; Used internally in `c-forward-keyword-clause' to move forward
6872 ;; over a type (if TYPE is 'type) or a name (otherwise) which
6873 ;; possibly is prefixed by keywords and their associated clauses.
6874 ;; Try with a type/name first to not trip up on those that begin
6875 ;; with a keyword. Return t if a known or found type is moved
6876 ;; over. The point is clobbered if nil is returned. If range
6877 ;; recording is enabled, the identifier is recorded on as a type
6878 ;; if TYPE is 'type or as a reference if TYPE is 'ref.
6880 ;; This macro might do hidden buffer changes.
6881 `(let (res)
6882 (setq c-last-identifier-range nil)
6883 (while (if (setq res ,(if (eq type 'type)
6884 `(c-forward-type)
6885 `(c-forward-name)))
6887 (cond ((looking-at c-keywords-regexp)
6888 (c-forward-keyword-clause 1))
6889 ((and c-opt-cpp-prefix
6890 (looking-at c-noise-macro-with-parens-name-re))
6891 (c-forward-noise-clause)))))
6892 (when (memq res '(t known found prefix maybe))
6893 (when c-record-type-identifiers
6894 ,(if (eq type 'type)
6895 `(c-record-type-id c-last-identifier-range)
6896 `(c-record-ref-id c-last-identifier-range)))
6897 t)))
6899 (defmacro c-forward-id-comma-list (type update-safe-pos)
6900 ;; Used internally in `c-forward-keyword-clause' to move forward
6901 ;; over a comma separated list of types or names using
6902 ;; `c-forward-keyword-prefixed-id'.
6904 ;; This macro might do hidden buffer changes.
6905 `(while (and (progn
6906 ,(when update-safe-pos
6907 `(setq safe-pos (point)))
6908 (eq (char-after) ?,))
6909 (progn
6910 (forward-char)
6911 (c-forward-syntactic-ws)
6912 (c-forward-keyword-prefixed-id ,type)))))
6914 (defun c-forward-noise-clause ()
6915 ;; Point is at a c-noise-macro-with-parens-names macro identifier. Go
6916 ;; forward over this name, any parenthesis expression which follows it, and
6917 ;; any syntactic WS, ending up at the next token. If there is an unbalanced
6918 ;; paren expression, leave point at it. Always Return t.
6919 (c-forward-token-2)
6920 (if (and (eq (char-after) ?\()
6921 (c-go-list-forward))
6922 (c-forward-syntactic-ws))
6925 (defun c-forward-keyword-clause (match)
6926 ;; Submatch MATCH in the current match data is assumed to surround a
6927 ;; token. If it's a keyword, move over it and any immediately
6928 ;; following clauses associated with it, stopping at the start of
6929 ;; the next token. t is returned in that case, otherwise the point
6930 ;; stays and nil is returned. The kind of clauses that are
6931 ;; recognized are those specified by `c-type-list-kwds',
6932 ;; `c-ref-list-kwds', `c-colon-type-list-kwds',
6933 ;; `c-paren-nontype-kwds', `c-paren-type-kwds', `c-<>-type-kwds',
6934 ;; and `c-<>-arglist-kwds'.
6936 ;; This function records identifier ranges on
6937 ;; `c-record-type-identifiers' and `c-record-ref-identifiers' if
6938 ;; `c-record-type-identifiers' is non-nil.
6940 ;; Note that for `c-colon-type-list-kwds', which doesn't necessary
6941 ;; apply directly after the keyword, the type list is moved over
6942 ;; only when there is no unaccounted token before it (i.e. a token
6943 ;; that isn't moved over due to some other keyword list). The
6944 ;; identifier ranges in the list are still recorded if that should
6945 ;; be done, though.
6947 ;; This function might do hidden buffer changes.
6949 (let ((kwd-sym (c-keyword-sym (match-string match))) safe-pos pos
6950 ;; The call to `c-forward-<>-arglist' below is made after
6951 ;; `c-<>-sexp-kwds' keywords, so we're certain they actually
6952 ;; are angle bracket arglists and `c-restricted-<>-arglists'
6953 ;; should therefore be nil.
6954 (c-parse-and-markup-<>-arglists t)
6955 c-restricted-<>-arglists)
6957 (when kwd-sym
6958 (goto-char (match-end match))
6959 (c-forward-syntactic-ws)
6960 (setq safe-pos (point))
6962 (cond
6963 ((and (c-keyword-member kwd-sym 'c-type-list-kwds)
6964 (c-forward-keyword-prefixed-id type))
6965 ;; There's a type directly after a keyword in `c-type-list-kwds'.
6966 (c-forward-id-comma-list type t))
6968 ((and (c-keyword-member kwd-sym 'c-ref-list-kwds)
6969 (c-forward-keyword-prefixed-id ref))
6970 ;; There's a name directly after a keyword in `c-ref-list-kwds'.
6971 (c-forward-id-comma-list ref t))
6973 ((and (c-keyword-member kwd-sym 'c-paren-any-kwds)
6974 (eq (char-after) ?\())
6975 ;; There's an open paren after a keyword in `c-paren-any-kwds'.
6977 (forward-char)
6978 (when (and (setq pos (c-up-list-forward))
6979 (eq (char-before pos) ?\)))
6980 (when (and c-record-type-identifiers
6981 (c-keyword-member kwd-sym 'c-paren-type-kwds))
6982 ;; Use `c-forward-type' on every identifier we can find
6983 ;; inside the paren, to record the types.
6984 (while (c-syntactic-re-search-forward c-symbol-start pos t)
6985 (goto-char (match-beginning 0))
6986 (unless (c-forward-type)
6987 (looking-at c-symbol-key) ; Always matches.
6988 (goto-char (match-end 0)))))
6990 (goto-char pos)
6991 (c-forward-syntactic-ws)
6992 (setq safe-pos (point))))
6994 ((and (c-keyword-member kwd-sym 'c-<>-sexp-kwds)
6995 (eq (char-after) ?<)
6996 (c-forward-<>-arglist (c-keyword-member kwd-sym 'c-<>-type-kwds)))
6997 (c-forward-syntactic-ws)
6998 (setq safe-pos (point)))
7000 ((and (c-keyword-member kwd-sym 'c-nonsymbol-sexp-kwds)
7001 (not (looking-at c-symbol-start))
7002 (c-safe (c-forward-sexp) t))
7003 (c-forward-syntactic-ws)
7004 (setq safe-pos (point))))
7006 (when (c-keyword-member kwd-sym 'c-colon-type-list-kwds)
7007 (if (eq (char-after) ?:)
7008 ;; If we are at the colon already, we move over the type
7009 ;; list after it.
7010 (progn
7011 (forward-char)
7012 (c-forward-syntactic-ws)
7013 (when (c-forward-keyword-prefixed-id type)
7014 (c-forward-id-comma-list type t)))
7015 ;; Not at the colon, so stop here. But the identifier
7016 ;; ranges in the type list later on should still be
7017 ;; recorded.
7018 (and c-record-type-identifiers
7019 (progn
7020 ;; If a keyword matched both one of the types above and
7021 ;; this one, we match `c-colon-type-list-re' after the
7022 ;; clause matched above.
7023 (goto-char safe-pos)
7024 (looking-at c-colon-type-list-re))
7025 (progn
7026 (goto-char (match-end 0))
7027 (c-forward-syntactic-ws)
7028 (c-forward-keyword-prefixed-id type))
7029 ;; There's a type after the `c-colon-type-list-re' match
7030 ;; after a keyword in `c-colon-type-list-kwds'.
7031 (c-forward-id-comma-list type nil))))
7033 (goto-char safe-pos)
7034 t)))
7036 ;; cc-mode requires cc-fonts.
7037 (declare-function c-fontify-recorded-types-and-refs "cc-fonts" ())
7039 (defun c-forward-<>-arglist (all-types)
7040 ;; The point is assumed to be at a "<". Try to treat it as the open
7041 ;; paren of an angle bracket arglist and move forward to the
7042 ;; corresponding ">". If successful, the point is left after the
7043 ;; ">" and t is returned, otherwise the point isn't moved and nil is
7044 ;; returned. If ALL-TYPES is t then all encountered arguments in
7045 ;; the arglist that might be types are treated as found types.
7047 ;; The variable `c-parse-and-markup-<>-arglists' controls how this
7048 ;; function handles text properties on the angle brackets and argument
7049 ;; separating commas.
7051 ;; `c-restricted-<>-arglists' controls how lenient the template
7052 ;; arglist recognition should be.
7054 ;; This function records identifier ranges on
7055 ;; `c-record-type-identifiers' and `c-record-ref-identifiers' if
7056 ;; `c-record-type-identifiers' is non-nil.
7058 ;; This function might do hidden buffer changes.
7060 (let ((start (point))
7061 (old-found-types (copy-hash-table c-found-types))
7062 ;; If `c-record-type-identifiers' is set then activate
7063 ;; recording of any found types that constitute an argument in
7064 ;; the arglist.
7065 (c-record-found-types (if c-record-type-identifiers t)))
7066 (if (catch 'angle-bracket-arglist-escape
7067 (setq c-record-found-types
7068 (c-forward-<>-arglist-recur all-types)))
7069 (progn
7070 (when (consp c-record-found-types)
7071 (setq c-record-type-identifiers
7072 ;; `nconc' doesn't mind that the tail of
7073 ;; `c-record-found-types' is t.
7074 (nconc c-record-found-types c-record-type-identifiers)))
7077 (setq c-found-types old-found-types)
7078 (goto-char start)
7079 nil)))
7081 (defun c-forward-<>-arglist-recur (all-types)
7082 ;; Recursive part of `c-forward-<>-arglist'.
7084 ;; This function might do hidden buffer changes.
7085 (let ((start (point)) res pos
7086 ;; Cover this so that any recorded found type ranges are
7087 ;; automatically lost if it turns out to not be an angle
7088 ;; bracket arglist. It's propagated through the return value
7089 ;; on successful completion.
7090 (c-record-found-types c-record-found-types)
7091 ;; List that collects the positions after the argument
7092 ;; separating ',' in the arglist.
7093 arg-start-pos)
7094 ;; If the '<' has paren open syntax then we've marked it as an angle
7095 ;; bracket arglist before, so skip to the end.
7096 (if (and (not c-parse-and-markup-<>-arglists)
7097 (c-get-char-property (point) 'syntax-table))
7099 (progn
7100 (forward-char)
7101 (if (and (c-go-up-list-forward)
7102 (eq (char-before) ?>))
7104 ;; Got unmatched paren angle brackets. We don't clear the paren
7105 ;; syntax properties and retry, on the basis that it's very
7106 ;; unlikely that paren angle brackets become operators by code
7107 ;; manipulation. It's far more likely that it doesn't match due
7108 ;; to narrowing or some temporary change.
7109 (goto-char start)
7110 nil))
7112 (forward-char) ; Forward over the opening '<'.
7114 (unless (looking-at c-<-op-cont-regexp)
7115 ;; go forward one non-alphanumeric character (group) per iteration of
7116 ;; this loop.
7117 (while (and
7118 (progn
7119 (c-forward-syntactic-ws)
7120 (when (or (and c-record-type-identifiers all-types)
7121 (not (equal c-inside-<>-type-key "\\(\\<\\>\\)")))
7122 (c-forward-syntactic-ws)
7123 (cond
7124 ((eq (char-after) ??)
7125 (forward-char))
7126 ((and (looking-at c-identifier-start)
7127 (not (looking-at c-keywords-regexp)))
7128 (if (or (and all-types c-record-type-identifiers)
7129 (c-major-mode-is 'java-mode))
7130 ;; All encountered identifiers are types, so set the
7131 ;; promote flag and parse the type.
7132 (let ((c-promote-possible-types t)
7133 (c-record-found-types t))
7134 (c-forward-type))
7135 (c-forward-token-2))))
7137 (c-forward-syntactic-ws)
7139 (when (looking-at c-inside-<>-type-key)
7140 (goto-char (match-end 1))
7141 (c-forward-syntactic-ws)
7142 (let ((c-promote-possible-types t)
7143 (c-record-found-types t))
7144 (c-forward-type))
7145 (c-forward-syntactic-ws)))
7147 (setq pos (point)) ; e.g. first token inside the '<'
7149 ;; Note: These regexps exploit the match order in \| so
7150 ;; that "<>" is matched by "<" rather than "[^>:-]>".
7151 (c-syntactic-re-search-forward
7152 ;; Stop on ',', '|', '&', '+' and '-' to catch
7153 ;; common binary operators that could be between
7154 ;; two comparison expressions "a<b" and "c>d".
7155 ;; 2016-02-11: C++11 templates can now contain arithmetic
7156 ;; expressions, so template detection in C++ is now less
7157 ;; robust than it was.
7158 c-<>-notable-chars-re
7159 nil t t))
7161 (cond
7162 ((eq (char-before) ?>)
7163 ;; Either an operator starting with '>' or the end of
7164 ;; the angle bracket arglist.
7166 (if (save-excursion
7167 (c-backward-token-2)
7168 (looking-at c-multichar->-op-not->>-regexp))
7169 (progn
7170 (goto-char (match-end 0))
7171 t) ; Continue the loop.
7173 ;; The angle bracket arglist is finished.
7174 (when c-parse-and-markup-<>-arglists
7175 (while arg-start-pos
7176 (c-put-c-type-property (1- (car arg-start-pos))
7177 'c-<>-arg-sep)
7178 (setq arg-start-pos (cdr arg-start-pos)))
7179 (c-mark-<-as-paren start)
7180 (c-mark->-as-paren (1- (point))))
7181 (setq res t)
7182 nil)) ; Exit the loop.
7184 ((eq (char-before) ?<)
7185 ;; Either an operator starting with '<' or a nested arglist.
7186 (setq pos (point))
7187 (let (id-start id-end subres keyword-match)
7188 (cond
7189 ;; The '<' begins a multi-char operator.
7190 ((looking-at c-<-op-cont-regexp)
7191 (goto-char (match-end 0)))
7192 ;; We're at a nested <.....>
7193 ((progn
7194 (backward-char) ; to the '<'
7195 (and
7196 (save-excursion
7197 ;; There's always an identifier before an angle
7198 ;; bracket arglist, or a keyword in `c-<>-type-kwds'
7199 ;; or `c-<>-arglist-kwds'.
7200 (c-backward-syntactic-ws)
7201 (setq id-end (point))
7202 (c-simple-skip-symbol-backward)
7203 (when (or (setq keyword-match
7204 (looking-at c-opt-<>-sexp-key))
7205 (not (looking-at c-keywords-regexp)))
7206 (setq id-start (point))))
7207 (setq subres
7208 (let ((c-promote-possible-types t)
7209 (c-record-found-types t))
7210 (c-forward-<>-arglist-recur
7211 (and keyword-match
7212 (c-keyword-member
7213 (c-keyword-sym (match-string 1))
7214 'c-<>-type-kwds))))))
7215 (or subres (goto-char pos))
7216 subres)
7217 ;; It was an angle bracket arglist.
7218 (setq c-record-found-types subres)
7220 ;; Record the identifier before the template as a type
7221 ;; or reference depending on whether the arglist is last
7222 ;; in a qualified identifier.
7223 (when (and c-record-type-identifiers
7224 (not keyword-match))
7225 (if (and c-opt-identifier-concat-key
7226 (progn
7227 (c-forward-syntactic-ws)
7228 (looking-at c-opt-identifier-concat-key)))
7229 (c-record-ref-id (cons id-start id-end))
7230 (c-record-type-id (cons id-start id-end)))))
7232 ;; At a "less than" operator.
7234 ;; (forward-char) ; NO! We've already gone over the <.
7236 t) ; carry on looping.
7238 ((and
7239 (eq (char-before) ?\()
7240 (c-go-up-list-forward)
7241 (eq (char-before) ?\))))
7243 ((and (not c-restricted-<>-arglists)
7244 (or (and (eq (char-before) ?&)
7245 (not (eq (char-after) ?&)))
7246 (eq (char-before) ?,)))
7247 ;; Just another argument. Record the position. The
7248 ;; type check stuff that made us stop at it is at
7249 ;; the top of the loop.
7250 (setq arg-start-pos (cons (point) arg-start-pos)))
7253 ;; Got a character that can't be in an angle bracket
7254 ;; arglist argument. Abort using `throw', since
7255 ;; it's useless to try to find a surrounding arglist
7256 ;; if we're nested.
7257 (throw 'angle-bracket-arglist-escape nil))))))
7258 (if res
7259 (or c-record-found-types t)))))
7261 (defun c-backward-<>-arglist (all-types &optional limit)
7262 ;; The point is assumed to be directly after a ">". Try to treat it
7263 ;; as the close paren of an angle bracket arglist and move back to
7264 ;; the corresponding "<". If successful, the point is left at
7265 ;; the "<" and t is returned, otherwise the point isn't moved and
7266 ;; nil is returned. ALL-TYPES is passed on to
7267 ;; `c-forward-<>-arglist'.
7269 ;; If the optional LIMIT is given, it bounds the backward search.
7270 ;; It's then assumed to be at a syntactically relevant position.
7272 ;; This is a wrapper around `c-forward-<>-arglist'. See that
7273 ;; function for more details.
7275 (let ((start (point)))
7276 (backward-char)
7277 (if (and (not c-parse-and-markup-<>-arglists)
7278 (c-get-char-property (point) 'syntax-table))
7280 (if (and (c-go-up-list-backward)
7281 (eq (char-after) ?<))
7283 ;; See corresponding note in `c-forward-<>-arglist'.
7284 (goto-char start)
7285 nil)
7287 (while (progn
7288 (c-syntactic-skip-backward "^<;{}" limit t)
7290 (and
7291 (if (eq (char-before) ?<)
7293 ;; Stopped at bob or a char that isn't allowed in an
7294 ;; arglist, so we've failed.
7295 (goto-char start)
7296 nil)
7298 (if (> (point)
7299 (progn (c-beginning-of-current-token)
7300 (point)))
7301 ;; If we moved then the "<" was part of some
7302 ;; multicharacter token.
7305 (backward-char)
7306 (let ((beg-pos (point)))
7307 (if (c-forward-<>-arglist all-types)
7308 (cond ((= (point) start)
7309 ;; Matched the arglist. Break the while.
7310 (goto-char beg-pos)
7311 nil)
7312 ((> (point) start)
7313 ;; We started from a non-paren ">" inside an
7314 ;; arglist.
7315 (goto-char start)
7316 nil)
7318 ;; Matched a shorter arglist. Can be a nested
7319 ;; one so continue looking.
7320 (goto-char beg-pos)
7322 t))))))
7324 (/= (point) start))))
7326 (defun c-forward-name ()
7327 ;; Move forward over a complete name if at the beginning of one,
7328 ;; stopping at the next following token. A keyword, as such,
7329 ;; doesn't count as a name. If the point is not at something that
7330 ;; is recognized as a name then it stays put.
7332 ;; A name could be something as simple as "foo" in C or something as
7333 ;; complex as "X<Y<class A<int>::B, BIT_MAX >> b>, ::operator<> ::
7334 ;; Z<(a>b)> :: operator const X<&foo>::T Q::G<unsigned short
7335 ;; int>::*volatile const" in C++ (this function is actually little
7336 ;; more than a `looking-at' call in all modes except those that,
7337 ;; like C++, have `c-recognize-<>-arglists' set).
7339 ;; Return
7340 ;; o - nil if no name is found;
7341 ;; o - 'template if it's an identifier ending with an angle bracket
7342 ;; arglist;
7343 ;; o - 'operator of it's an operator identifier;
7344 ;; o - t if it's some other kind of name.
7346 ;; This function records identifier ranges on
7347 ;; `c-record-type-identifiers' and `c-record-ref-identifiers' if
7348 ;; `c-record-type-identifiers' is non-nil.
7350 ;; This function might do hidden buffer changes.
7352 (let ((pos (point)) (start (point)) res id-start id-end
7353 ;; Turn off `c-promote-possible-types' here since we might
7354 ;; call `c-forward-<>-arglist' and we don't want it to promote
7355 ;; every suspect thing in the arglist to a type. We're
7356 ;; typically called from `c-forward-type' in this case, and
7357 ;; the caller only wants the top level type that it finds to
7358 ;; be promoted.
7359 c-promote-possible-types)
7360 (while
7361 (and
7362 (looking-at c-identifier-key)
7364 (progn
7365 ;; Check for keyword. We go to the last symbol in
7366 ;; `c-identifier-key' first.
7367 (goto-char (setq id-end (match-end 0)))
7368 (c-simple-skip-symbol-backward)
7369 (setq id-start (point))
7371 (if (looking-at c-keywords-regexp)
7372 (when (and (c-major-mode-is 'c++-mode)
7373 (looking-at
7374 (cc-eval-when-compile
7375 (concat "\\(operator\\|\\(template\\)\\)"
7376 "\\(" (c-lang-const c-nonsymbol-key c++)
7377 "\\|$\\)")))
7378 (if (match-beginning 2)
7379 ;; "template" is only valid inside an
7380 ;; identifier if preceded by "::".
7381 (save-excursion
7382 (c-backward-syntactic-ws)
7383 (and (c-safe (backward-char 2) t)
7384 (looking-at "::")))
7387 ;; Handle a C++ operator or template identifier.
7388 (goto-char id-end)
7389 (c-forward-syntactic-ws)
7390 (cond ((eq (char-before id-end) ?e)
7391 ;; Got "... ::template".
7392 (let ((subres (c-forward-name)))
7393 (when subres
7394 (setq pos (point)
7395 res subres))))
7397 ((looking-at c-identifier-start)
7398 ;; Got a cast operator.
7399 (when (c-forward-type)
7400 (setq pos (point)
7401 res 'operator)
7402 ;; Now we should match a sequence of either
7403 ;; '*', '&' or a name followed by ":: *",
7404 ;; where each can be followed by a sequence
7405 ;; of `c-opt-type-modifier-key'.
7406 (while (cond ((looking-at "[*&]")
7407 (goto-char (match-end 0))
7409 ((looking-at c-identifier-start)
7410 (and (c-forward-name)
7411 (looking-at "::")
7412 (progn
7413 (goto-char (match-end 0))
7414 (c-forward-syntactic-ws)
7415 (eq (char-after) ?*))
7416 (progn
7417 (forward-char)
7418 t))))
7419 (while (progn
7420 (c-forward-syntactic-ws)
7421 (setq pos (point))
7422 (looking-at c-opt-type-modifier-key))
7423 (goto-char (match-end 1))))))
7425 ((looking-at c-overloadable-operators-regexp)
7426 ;; Got some other operator.
7427 (setq c-last-identifier-range
7428 (cons (point) (match-end 0)))
7429 (goto-char (match-end 0))
7430 (c-forward-syntactic-ws)
7431 (setq pos (point)
7432 res 'operator)))
7434 nil)
7436 ;; `id-start' is equal to `id-end' if we've jumped over
7437 ;; an identifier that doesn't end with a symbol token.
7438 ;; That can occur e.g. for Java import directives on the
7439 ;; form "foo.bar.*".
7440 (when (and id-start (/= id-start id-end))
7441 (setq c-last-identifier-range
7442 (cons id-start id-end)))
7443 (goto-char id-end)
7444 (c-forward-syntactic-ws)
7445 (setq pos (point)
7446 res t)))
7448 (progn
7449 (goto-char pos)
7450 (when (or c-opt-identifier-concat-key
7451 c-recognize-<>-arglists)
7453 (cond
7454 ((and c-opt-identifier-concat-key
7455 (looking-at c-opt-identifier-concat-key))
7456 ;; Got a concatenated identifier. This handles the
7457 ;; cases with tricky syntactic whitespace that aren't
7458 ;; covered in `c-identifier-key'.
7459 (goto-char (match-end 0))
7460 (c-forward-syntactic-ws)
7463 ((and c-recognize-<>-arglists
7464 (eq (char-after) ?<))
7465 ;; Maybe an angle bracket arglist.
7466 (when (let (c-last-identifier-range)
7467 (c-forward-<>-arglist nil))
7469 (c-forward-syntactic-ws)
7470 (unless (eq (char-after) ?\()
7471 (setq c-last-identifier-range nil)
7472 (c-add-type start (1+ pos)))
7473 (setq pos (point))
7475 (if (and c-opt-identifier-concat-key
7476 (looking-at c-opt-identifier-concat-key))
7478 ;; Continue if there's an identifier concatenation
7479 ;; operator after the template argument.
7480 (progn
7481 (when (and c-record-type-identifiers id-start)
7482 (c-record-ref-id (cons id-start id-end)))
7483 (forward-char 2)
7484 (c-forward-syntactic-ws)
7487 (when (and c-record-type-identifiers id-start
7488 (not (eq (char-after) ?\()))
7489 (c-record-type-id (cons id-start id-end)))
7490 (setq res 'template)
7491 nil)))
7492 )))))
7494 (goto-char pos)
7495 res))
7497 (defun c-forward-type (&optional brace-block-too)
7498 ;; Move forward over a type spec if at the beginning of one,
7499 ;; stopping at the next following token. The keyword "typedef"
7500 ;; isn't part of a type spec here.
7502 ;; BRACE-BLOCK-TOO, when non-nil, means move over the brace block in
7503 ;; constructs like "struct foo {...} bar ;" or "struct {...} bar;".
7504 ;; The current (2009-03-10) intention is to convert all uses of
7505 ;; `c-forward-type' to call with this parameter set, then to
7506 ;; eliminate it.
7508 ;; Return
7509 ;; o - t if it's a known type that can't be a name or other
7510 ;; expression;
7511 ;; o - 'known if it's an otherwise known type (according to
7512 ;; `*-font-lock-extra-types');
7513 ;; o - 'prefix if it's a known prefix of a type;
7514 ;; o - 'found if it's a type that matches one in `c-found-types';
7515 ;; o - 'maybe if it's an identifier that might be a type;
7516 ;; o - 'decltype if it's a decltype(variable) declaration; - or
7517 ;; o - nil if it can't be a type (the point isn't moved then).
7519 ;; The point is assumed to be at the beginning of a token.
7521 ;; Note that this function doesn't skip past the brace definition
7522 ;; that might be considered part of the type, e.g.
7523 ;; "enum {a, b, c} foo".
7525 ;; This function records identifier ranges on
7526 ;; `c-record-type-identifiers' and `c-record-ref-identifiers' if
7527 ;; `c-record-type-identifiers' is non-nil.
7529 ;; This function might do hidden buffer changes.
7530 (when (and c-recognize-<>-arglists
7531 (looking-at "<"))
7532 (c-forward-<>-arglist t)
7533 (c-forward-syntactic-ws))
7535 (let ((start (point)) pos res name-res id-start id-end id-range)
7537 ;; Skip leading type modifiers. If any are found we know it's a
7538 ;; prefix of a type.
7539 (when c-opt-type-modifier-key ; e.g. "const" "volatile", but NOT "typedef"
7540 (while (looking-at c-opt-type-modifier-key)
7541 (goto-char (match-end 1))
7542 (c-forward-syntactic-ws)
7543 (setq res 'prefix)))
7545 (cond
7546 ((looking-at c-typeof-key) ; e.g. C++'s "decltype".
7547 (goto-char (match-end 1))
7548 (c-forward-syntactic-ws)
7549 (setq res (and (eq (char-after) ?\()
7550 (c-safe (c-forward-sexp))
7551 'decltype))
7552 (if res
7553 (c-forward-syntactic-ws)
7554 (goto-char start)))
7556 ((looking-at c-type-prefix-key) ; e.g. "struct", "class", but NOT
7557 ; "typedef".
7558 (goto-char (match-end 1))
7559 (c-forward-syntactic-ws)
7561 (while (cond
7562 ((looking-at c-decl-hangon-key)
7563 (c-forward-keyword-clause 1))
7564 ((looking-at c-pack-key)
7565 (goto-char (match-end 1))
7566 (c-forward-syntactic-ws))
7567 ((and c-opt-cpp-prefix
7568 (looking-at c-noise-macro-with-parens-name-re))
7569 (c-forward-noise-clause))))
7571 (setq pos (point))
7573 (setq name-res (c-forward-name))
7574 (setq res (not (null name-res)))
7575 (when (eq name-res t)
7576 ;; In many languages the name can be used without the
7577 ;; prefix, so we add it to `c-found-types'.
7578 (c-add-type pos (point))
7579 (when (and c-record-type-identifiers
7580 c-last-identifier-range)
7581 (c-record-type-id c-last-identifier-range)))
7582 (when (and brace-block-too
7583 (memq res '(t nil))
7584 (eq (char-after) ?\{)
7585 (save-excursion
7586 (c-safe
7587 (progn (c-forward-sexp)
7588 (c-forward-syntactic-ws)
7589 (setq pos (point))))))
7590 (goto-char pos)
7591 (setq res t))
7592 (unless res (goto-char start))) ; invalid syntax
7594 ((progn
7595 (setq pos nil)
7596 (if (looking-at c-identifier-start)
7597 (save-excursion
7598 (setq id-start (point)
7599 name-res (c-forward-name))
7600 (when name-res
7601 (setq id-end (point)
7602 id-range c-last-identifier-range))))
7603 (and (cond ((looking-at c-primitive-type-key)
7604 (setq res t))
7605 ((c-with-syntax-table c-identifier-syntax-table
7606 (looking-at c-known-type-key))
7607 (setq res 'known)))
7608 (or (not id-end)
7609 (>= (save-excursion
7610 (save-match-data
7611 (goto-char (match-end 1))
7612 (c-forward-syntactic-ws)
7613 (setq pos (point))))
7614 id-end)
7615 (setq res nil))))
7616 ;; Looking at a primitive or known type identifier. We've
7617 ;; checked for a name first so that we don't go here if the
7618 ;; known type match only is a prefix of another name.
7620 (setq id-end (match-end 1))
7622 (when (and c-record-type-identifiers
7623 (or c-promote-possible-types (eq res t)))
7624 (c-record-type-id (cons (match-beginning 1) (match-end 1))))
7626 (if (and c-opt-type-component-key
7627 (save-match-data
7628 (looking-at c-opt-type-component-key)))
7629 ;; There might be more keywords for the type.
7630 (let (safe-pos)
7631 (c-forward-keyword-clause 1)
7632 (while (progn
7633 (setq safe-pos (point))
7634 (looking-at c-opt-type-component-key))
7635 (when (and c-record-type-identifiers
7636 (looking-at c-primitive-type-key))
7637 (c-record-type-id (cons (match-beginning 1)
7638 (match-end 1))))
7639 (c-forward-keyword-clause 1))
7640 (if (looking-at c-primitive-type-key)
7641 (progn
7642 (when c-record-type-identifiers
7643 (c-record-type-id (cons (match-beginning 1)
7644 (match-end 1))))
7645 (c-forward-keyword-clause 1)
7646 (setq res t))
7647 (goto-char safe-pos)
7648 (setq res 'prefix)))
7649 (unless (save-match-data (c-forward-keyword-clause 1))
7650 (if pos
7651 (goto-char pos)
7652 (goto-char (match-end 1))
7653 (c-forward-syntactic-ws)))))
7655 (name-res
7656 (cond ((eq name-res t)
7657 ;; A normal identifier.
7658 (goto-char id-end)
7659 (if (or res c-promote-possible-types)
7660 (progn
7661 (c-add-type id-start id-end)
7662 (when (and c-record-type-identifiers id-range)
7663 (c-record-type-id id-range))
7664 (unless res
7665 (setq res 'found)))
7666 (setq res (if (c-check-type id-start id-end)
7667 ;; It's an identifier that has been used as
7668 ;; a type somewhere else.
7669 'found
7670 ;; It's an identifier that might be a type.
7671 'maybe))))
7672 ((eq name-res 'template)
7673 ;; A template is sometimes a type.
7674 (goto-char id-end)
7675 (c-forward-syntactic-ws)
7676 (setq res
7677 (if (eq (char-after) ?\()
7678 (if (c-check-type id-start id-end)
7679 ;; It's an identifier that has been used as
7680 ;; a type somewhere else.
7681 'found
7682 ;; It's an identifier that might be a type.
7683 'maybe)
7684 t)))
7686 ;; Otherwise it's an operator identifier, which is not a type.
7687 (goto-char start)
7688 (setq res nil)))))
7690 (when res
7691 ;; Skip trailing type modifiers. If any are found we know it's
7692 ;; a type.
7693 (when c-opt-type-modifier-key
7694 (while (looking-at c-opt-type-modifier-key) ; e.g. "const", "volatile"
7695 (goto-char (match-end 1))
7696 (c-forward-syntactic-ws)
7697 (setq res t)))
7699 ;; Step over any type suffix operator. Do not let the existence
7700 ;; of these alter the classification of the found type, since
7701 ;; these operators typically are allowed in normal expressions
7702 ;; too.
7703 (when c-opt-type-suffix-key ; e.g. "..."
7704 (while (looking-at c-opt-type-suffix-key)
7705 (goto-char (match-end 1))
7706 (c-forward-syntactic-ws)))
7708 ;; Skip any "WS" identifiers (e.g. "final" or "override" in C++)
7709 (while (looking-at c-type-decl-suffix-ws-ids-key)
7710 (goto-char (match-end 1))
7711 (c-forward-syntactic-ws)
7712 (setq res t))
7714 (when c-opt-type-concat-key ; Only/mainly for pike.
7715 ;; Look for a trailing operator that concatenates the type
7716 ;; with a following one, and if so step past that one through
7717 ;; a recursive call. Note that we don't record concatenated
7718 ;; types in `c-found-types' - it's the component types that
7719 ;; are recorded when appropriate.
7720 (setq pos (point))
7721 (let* ((c-promote-possible-types (or (memq res '(t known))
7722 c-promote-possible-types))
7723 ;; If we can't promote then set `c-record-found-types' so that
7724 ;; we can merge in the types from the second part afterwards if
7725 ;; it turns out to be a known type there.
7726 (c-record-found-types (and c-record-type-identifiers
7727 (not c-promote-possible-types)))
7728 subres)
7729 (if (and (looking-at c-opt-type-concat-key)
7731 (progn
7732 (goto-char (match-end 1))
7733 (c-forward-syntactic-ws)
7734 (setq subres (c-forward-type))))
7736 (progn
7737 ;; If either operand certainly is a type then both are, but we
7738 ;; don't let the existence of the operator itself promote two
7739 ;; uncertain types to a certain one.
7740 (cond ((eq res t))
7741 ((eq subres t)
7742 (unless (eq name-res 'template)
7743 (c-add-type id-start id-end))
7744 (when (and c-record-type-identifiers id-range)
7745 (c-record-type-id id-range))
7746 (setq res t))
7747 ((eq res 'known))
7748 ((eq subres 'known)
7749 (setq res 'known))
7750 ((eq res 'found))
7751 ((eq subres 'found)
7752 (setq res 'found))
7754 (setq res 'maybe)))
7756 (when (and (eq res t)
7757 (consp c-record-found-types))
7758 ;; Merge in the ranges of any types found by the second
7759 ;; `c-forward-type'.
7760 (setq c-record-type-identifiers
7761 ;; `nconc' doesn't mind that the tail of
7762 ;; `c-record-found-types' is t.
7763 (nconc c-record-found-types
7764 c-record-type-identifiers))))
7766 (goto-char pos))))
7768 (when (and c-record-found-types (memq res '(known found)) id-range)
7769 (setq c-record-found-types
7770 (cons id-range c-record-found-types))))
7772 ;;(message "c-forward-type %s -> %s: %s" start (point) res)
7774 res))
7776 (defun c-forward-annotation ()
7777 ;; Used for Java code only at the moment. Assumes point is on the @, moves
7778 ;; forward an annotation and returns t. Leaves point unmoved and returns
7779 ;; nil if there is no annotation at point.
7780 (let ((pos (point)))
7782 (and (looking-at "@")
7783 (not (looking-at c-keywords-regexp))
7784 (progn (forward-char) t)
7785 (looking-at c-symbol-key)
7786 (progn (goto-char (match-end 0))
7787 (c-forward-syntactic-ws)
7789 (if (looking-at "(")
7790 (c-go-list-forward)
7792 (progn (goto-char pos) nil))))
7794 (defmacro c-pull-open-brace (ps)
7795 ;; Pull the next open brace from PS (which has the form of paren-state),
7796 ;; skipping over any brace pairs. Returns NIL when PS is exhausted.
7797 `(progn
7798 (while (consp (car ,ps))
7799 (setq ,ps (cdr ,ps)))
7800 (prog1 (car ,ps)
7801 (setq ,ps (cdr ,ps)))))
7803 (defun c-back-over-compound-identifier ()
7804 ;; Point is putatively just after a "compound identifier", i.e. something
7805 ;; looking (in C++) like this "FQN::of::base::Class". Move to the start of
7806 ;; this construct and return t. If the parsing fails, return nil, leaving
7807 ;; point unchanged.
7808 (let (end)
7809 (if (not (c-on-identifier))
7811 (c-simple-skip-symbol-backward)
7812 (while
7813 (progn
7814 (setq end (point))
7815 (c-backward-syntactic-ws)
7816 (c-backward-token-2)
7817 (and
7818 c-opt-identifier-concat-key
7819 (looking-at c-opt-identifier-concat-key)
7820 (progn
7821 (c-backward-syntactic-ws)
7822 (c-simple-skip-symbol-backward))))
7823 (setq end (point)))
7824 (goto-char end)
7825 t)))
7827 (defun c-back-over-member-initializer-braces ()
7828 ;; Point is just after a closing brace/parenthesis. Try to parse this as a
7829 ;; C++ member initializer list, going back to just after the introducing ":"
7830 ;; and returning t. Otherwise return nil, leaving point unchanged.
7831 (let ((here (point)) res)
7832 (setq res
7833 (catch 'done
7834 (when (not (c-go-list-backward))
7835 (throw 'done nil))
7836 (c-backward-syntactic-ws)
7837 (when (not (c-back-over-compound-identifier))
7838 (throw 'done nil))
7839 (c-backward-syntactic-ws)
7841 (while (eq (char-before) ?,)
7842 (backward-char)
7843 (c-backward-syntactic-ws)
7844 (when (not (memq (char-before) '(?\) ?})))
7845 (throw 'done nil))
7846 (when (not (c-go-list-backward))
7847 (throw 'done nil))
7848 (c-backward-syntactic-ws)
7849 (when (not (c-back-over-compound-identifier))
7850 (throw 'done nil))
7851 (c-backward-syntactic-ws))
7853 (eq (char-before) ?:)))
7854 (or res (goto-char here))
7855 res))
7857 (defmacro c-back-over-list-of-member-inits ()
7858 ;; Go back over a list of elements, each looking like:
7859 ;; <symbol> (<expression>) ,
7860 ;; or <symbol> {<expression>} , (with possibly a <....> expressions
7861 ;; following the <symbol>).
7862 ;; when we are putatively immediately after a comma. Stop when we don't see
7863 ;; a comma. If either of <symbol> or bracketed <expression> is missing,
7864 ;; throw nil to 'level. If the terminating } or ) is unmatched, throw nil
7865 ;; to 'done. This is not a general purpose macro!
7866 `(while (eq (char-before) ?,)
7867 (backward-char)
7868 (c-backward-syntactic-ws)
7869 (when (not (memq (char-before) '(?\) ?})))
7870 (throw 'level nil))
7871 (when (not (c-go-list-backward))
7872 (throw 'done nil))
7873 (c-backward-syntactic-ws)
7874 (while (eq (char-before) ?>)
7875 (when (not (c-backward-<>-arglist nil))
7876 (throw 'done nil))
7877 (c-backward-syntactic-ws))
7878 (when (not (c-back-over-compound-identifier))
7879 (throw 'level nil))
7880 (c-backward-syntactic-ws)))
7882 (defun c-back-over-member-initializers ()
7883 ;; Test whether we are in a C++ member initializer list, and if so, go back
7884 ;; to the introducing ":", returning the position of the opening paren of
7885 ;; the function's arglist. Otherwise return nil, leaving point unchanged.
7886 (let ((here (point))
7887 (paren-state (c-parse-state))
7888 pos level-plausible at-top-level res)
7889 ;; Assume tentatively that we're at the top level. Try to go back to the
7890 ;; colon we seek.
7891 (setq res
7892 (catch 'done
7893 (setq level-plausible
7894 (catch 'level
7895 (c-backward-syntactic-ws)
7896 (when (memq (char-before) '(?\) ?}))
7897 (when (not (c-go-list-backward))
7898 (throw 'done nil))
7899 (c-backward-syntactic-ws))
7900 (when (c-back-over-compound-identifier)
7901 (c-backward-syntactic-ws))
7902 (c-back-over-list-of-member-inits)
7903 (and (eq (char-before) ?:)
7904 (save-excursion
7905 (c-backward-token-2)
7906 (not (looking-at c-:$-multichar-token-regexp)))
7907 (c-just-after-func-arglist-p))))
7909 (while (and (not (and level-plausible
7910 (setq at-top-level (c-at-toplevel-p))))
7911 (setq pos (c-pull-open-brace paren-state))) ; might be a paren.
7912 (setq level-plausible
7913 (catch 'level
7914 (goto-char pos)
7915 (c-backward-syntactic-ws)
7916 (when (not (c-back-over-compound-identifier))
7917 (throw 'level nil))
7918 (c-backward-syntactic-ws)
7919 (c-back-over-list-of-member-inits)
7920 (and (eq (char-before) ?:)
7921 (save-excursion
7922 (c-backward-token-2)
7923 (not (looking-at c-:$-multichar-token-regexp)))
7924 (c-just-after-func-arglist-p)))))
7926 (and at-top-level level-plausible)))
7927 (or res (goto-char here))
7928 res))
7931 ;; Handling of large scale constructs like statements and declarations.
7933 ;; Macro used inside `c-forward-decl-or-cast-1'. It ought to be a
7934 ;; defsubst or perhaps even a defun, but it contains lots of free
7935 ;; variables that refer to things inside `c-forward-decl-or-cast-1'.
7936 (defmacro c-fdoc-shift-type-backward (&optional short)
7937 ;; `c-forward-decl-or-cast-1' can consume an arbitrary length list
7938 ;; of types when parsing a declaration, which means that it
7939 ;; sometimes consumes the identifier in the declaration as a type.
7940 ;; This is used to "backtrack" and make the last type be treated as
7941 ;; an identifier instead.
7942 `(progn
7943 ,(unless short
7944 ;; These identifiers are bound only in the inner let.
7945 '(setq identifier-type at-type
7946 identifier-start type-start
7947 got-parens nil
7948 got-identifier t
7949 got-suffix t
7950 got-suffix-after-parens id-start
7951 paren-depth 0))
7953 (if (setq at-type (if (eq backup-at-type 'prefix)
7955 backup-at-type))
7956 (setq type-start backup-type-start
7957 id-start backup-id-start)
7958 (setq type-start start-pos
7959 id-start start-pos))
7961 ;; When these flags already are set we've found specifiers that
7962 ;; unconditionally signal these attributes - backtracking doesn't
7963 ;; change that. So keep them set in that case.
7964 (or at-type-decl
7965 (setq at-type-decl backup-at-type-decl))
7966 (or maybe-typeless
7967 (setq maybe-typeless backup-maybe-typeless))
7969 ,(unless short
7970 ;; This identifier is bound only in the inner let.
7971 '(setq start id-start))))
7973 (defun c-forward-declarator (&optional limit accept-anon)
7974 ;; Assuming point is at the start of a declarator, move forward over it,
7975 ;; leaving point at the next token after it (e.g. a ) or a ; or a ,).
7977 ;; Return a list (ID-START ID-END BRACKETS-AFTER-ID GOT-INIT DECORATED),
7978 ;; where ID-START and ID-END are the bounds of the declarator's identifier,
7979 ;; and BRACKETS-AFTER-ID is non-nil if a [...] pair is present after the id.
7980 ;; GOT-INIT is non-nil when the declarator is followed by "=" or "(",
7981 ;; DECORATED is non-nil when the identifier is embellished by an operator,
7982 ;; like "*x", or "(*x)".
7984 ;; If ACCEPT-ANON is non-nil, move forward over any "anonymous declarator",
7985 ;; i.e. something like the (*) in int (*), such as might be found in a
7986 ;; declaration. In such a case ID-START and ID-END in the return value are
7987 ;; both set to nil. A "null" "anonymous declarator" gives a non-nil result.
7989 ;; If no declarator is found, leave point unmoved and return nil. LIMIT is
7990 ;; an optional limit for forward searching.
7992 ;; Note that the global variable `c-last-identifier-range' is written to, so
7993 ;; the caller should bind it if necessary.
7995 ;; Inside the following "condition form", we move forward over the
7996 ;; declarator's identifier up as far as any opening bracket (for array
7997 ;; size) or paren (for parameters of function-type) or brace (for
7998 ;; array/struct initialization) or "=" or terminating delimiter
7999 ;; (e.g. "," or ";" or "}").
8000 (let ((here (point))
8001 id-start id-end brackets-after-id paren-depth decorated)
8002 (or limit (setq limit (point-max)))
8003 (if (and
8004 (< (point) limit)
8006 ;; The following form moves forward over the declarator's
8007 ;; identifier (and what precedes it), returning t. If there
8008 ;; wasn't one, it returns nil.
8009 (let (got-identifier)
8010 (setq paren-depth 0)
8011 ;; Skip over type decl prefix operators, one for each iteration
8012 ;; of the while. These are, e.g. "*" in "int *foo" or "(" and
8013 ;; "*" in "int (*foo) (void)" (Note similar code in
8014 ;; `c-forward-decl-or-cast-1'.)
8015 (while
8016 (cond
8017 ((looking-at c-decl-hangon-key)
8018 (c-forward-keyword-clause 1))
8019 ((and c-opt-cpp-prefix
8020 (looking-at c-noise-macro-with-parens-name-re))
8021 (c-forward-noise-clause))
8022 ((and (looking-at c-type-decl-prefix-key)
8023 (if (and (c-major-mode-is 'c++-mode)
8024 (match-beginning 3))
8025 ;; If the third submatch matches in C++ then
8026 ;; we're looking at an identifier that's a
8027 ;; prefix only if it specifies a member pointer.
8028 (progn
8029 (setq id-start (point))
8030 (c-forward-name)
8031 (if (looking-at "\\(::\\)")
8032 ;; We only check for a trailing "::" and
8033 ;; let the "*" that should follow be
8034 ;; matched in the next round.
8036 ;; It turned out to be the real identifier,
8037 ;; so flag that and stop.
8038 (setq got-identifier t)
8039 nil))
8041 (if (looking-at c-type-decl-operator-prefix-key)
8042 (setq decorated t))
8043 (if (eq (char-after) ?\()
8044 (progn
8045 (setq paren-depth (1+ paren-depth))
8046 (forward-char))
8047 (goto-char (match-end 1)))
8048 (c-forward-syntactic-ws)
8049 t)))
8051 ;; If we haven't passed the identifier already, do it now.
8052 (unless got-identifier
8053 (setq id-start (point)))
8054 (cond
8055 ((or got-identifier
8056 (c-forward-name))
8057 (save-excursion
8058 (c-backward-syntactic-ws)
8059 (setq id-end (point))))
8060 (accept-anon
8061 (setq id-start nil id-end nil)
8063 (t (/= (point) here))))
8065 ;; Skip out of the parens surrounding the identifier. If closing
8066 ;; parens are missing, this form returns nil.
8067 (or (= paren-depth 0)
8068 (c-safe (goto-char (scan-lists (point) 1 paren-depth))))
8070 (<= (point) limit)
8072 ;; Skip over any trailing bit, such as "__attribute__".
8073 (progn
8074 (while (cond
8075 ((looking-at c-decl-hangon-key)
8076 (c-forward-keyword-clause 1))
8077 ((and c-opt-cpp-prefix
8078 (looking-at c-noise-macro-with-parens-name-re))
8079 (c-forward-noise-clause))))
8080 (<= (point) limit))
8082 ;; Search syntactically to the end of the declarator (";",
8083 ;; ",", a closing paren, eob etc) or to the beginning of an
8084 ;; initializer or function prototype ("=" or "\\s\(").
8085 ;; Note that square brackets are now not also treated as
8086 ;; initializers, since this broke when there were also
8087 ;; initializing brace lists.
8088 (let (found)
8089 (while
8090 (and (progn
8091 ;; In the next loop, we keep searching forward whilst
8092 ;; we find ":"s which aren't single colons inside C++
8093 ;; "for" statements.
8094 (while
8095 (and
8096 (setq found
8097 (c-syntactic-re-search-forward
8098 "[;:,]\\|\\s)\\|\\(=\\|\\s(\\)"
8099 limit t t))
8100 (eq (char-before) ?:)
8101 (if (looking-at c-:-op-cont-regexp)
8102 (progn (goto-char (match-end 0)) t)
8103 (not
8104 (and (c-major-mode-is 'c++-mode)
8105 (save-excursion
8106 (and
8107 (c-go-up-list-backward)
8108 (eq (char-after) ?\()
8109 (progn (c-backward-syntactic-ws)
8110 (c-simple-skip-symbol-backward))
8111 (looking-at c-paren-stmt-key))))))))
8112 found)
8113 (eq (char-before) ?\[)
8114 (c-go-up-list-forward))
8115 (setq brackets-after-id t))
8116 (when found (backward-char))
8118 (list id-start id-end brackets-after-id (match-beginning 1) decorated)
8120 (goto-char here)
8121 nil)))
8123 (defun c-forward-decl-or-cast-1 (preceding-token-end context last-cast-end)
8124 ;; Move forward over a declaration or a cast if at the start of one.
8125 ;; The point is assumed to be at the start of some token. Nil is
8126 ;; returned if no declaration or cast is recognized, and the point
8127 ;; is clobbered in that case.
8129 ;; If a declaration is parsed:
8131 ;; The point is left at the first token after the first complete
8132 ;; declarator, if there is one. The return value is a list of 4 elements,
8133 ;; where the first is the position of the first token in the declarator.
8134 ;; (See below for the other three.)
8135 ;; Some examples:
8137 ;; void foo (int a, char *b) stuff ...
8138 ;; car ^ ^ point
8139 ;; float (*a)[], b;
8140 ;; car ^ ^ point
8141 ;; unsigned int a = c_style_initializer, b;
8142 ;; car ^ ^ point
8143 ;; unsigned int a (cplusplus_style_initializer), b;
8144 ;; car ^ ^ point (might change)
8145 ;; class Foo : public Bar {}
8146 ;; car ^ ^ point
8147 ;; class PikeClass (int a, string b) stuff ...
8148 ;; car ^ ^ point
8149 ;; enum bool;
8150 ;; car ^ ^ point
8151 ;; enum bool flag;
8152 ;; car ^ ^ point
8153 ;; void cplusplus_function (int x) throw (Bad);
8154 ;; car ^ ^ point
8155 ;; Foo::Foo (int b) : Base (b) {}
8156 ;; car ^ ^ point
8158 ;; auto foo = 5;
8159 ;; car ^ ^ point
8160 ;; auto cplusplus_11 (int a, char *b) -> decltype (bar):
8161 ;; car ^ ^ point
8165 ;; The second element of the return value is non-nil when a
8166 ;; `c-typedef-decl-kwds' specifier is found in the declaration.
8167 ;; Specifically it is a dotted pair (A . B) where B is t when a
8168 ;; `c-typedef-kwds' ("typedef") is present, and A is t when some
8169 ;; other `c-typedef-decl-kwds' (e.g. class, struct, enum)
8170 ;; specifier is present. I.e., (some of) the declared
8171 ;; identifier(s) are types.
8173 ;; The third element of the return value is non-nil when the declaration
8174 ;; parsed might be an expression. The fourth element is the position of
8175 ;; the start of the type identifier.
8177 ;; If a cast is parsed:
8179 ;; The point is left at the first token after the closing paren of
8180 ;; the cast. The return value is `cast'. Note that the start
8181 ;; position must be at the first token inside the cast parenthesis
8182 ;; to recognize it.
8184 ;; PRECEDING-TOKEN-END is the first position after the preceding
8185 ;; token, i.e. on the other side of the syntactic ws from the point.
8186 ;; Use a value less than or equal to (point-min) if the point is at
8187 ;; the first token in (the visible part of) the buffer.
8189 ;; CONTEXT is a symbol that describes the context at the point:
8190 ;; 'decl In a comma-separated declaration context (typically
8191 ;; inside a function declaration arglist).
8192 ;; '<> In an angle bracket arglist.
8193 ;; 'arglist Some other type of arglist.
8194 ;; 'top Some other context and point is at the top-level (either
8195 ;; outside any braces or directly inside a class or namespace,
8196 ;; etc.)
8197 ;; nil Some other context or unknown context. Includes
8198 ;; within the parens of an if, for, ... construct.
8199 ;; 'not-decl This value is never supplied to this function. It
8200 ;; would mean we're definitely not in a declaration.
8202 ;; LAST-CAST-END is the first token after the closing paren of a
8203 ;; preceding cast, or nil if none is known. If
8204 ;; `c-forward-decl-or-cast-1' is used in succession, it should be
8205 ;; the position after the closest preceding call where a cast was
8206 ;; matched. In that case it's used to discover chains of casts like
8207 ;; "(a) (b) c".
8209 ;; This function records identifier ranges on
8210 ;; `c-record-type-identifiers' and `c-record-ref-identifiers' if
8211 ;; `c-record-type-identifiers' is non-nil.
8213 ;; This function might do hidden buffer changes.
8215 (let (;; `start-pos' is used below to point to the start of the
8216 ;; first type, i.e. after any leading specifiers. It might
8217 ;; also point at the beginning of the preceding syntactic
8218 ;; whitespace.
8219 (start-pos (point))
8220 ;; Set to the result of `c-forward-type'.
8221 at-type
8222 ;; The position of the first token in what we currently
8223 ;; believe is the type in the declaration or cast, after any
8224 ;; specifiers and their associated clauses.
8225 type-start
8226 ;; The position of the first token in what we currently
8227 ;; believe is the declarator for the first identifier. Set
8228 ;; when the type is found, and moved forward over any
8229 ;; `c-decl-hangon-kwds' and their associated clauses that
8230 ;; occurs after the type.
8231 id-start
8232 ;; These store `at-type', `type-start' and `id-start' of the
8233 ;; identifier before the one in those variables. The previous
8234 ;; identifier might turn out to be the real type in a
8235 ;; declaration if the last one has to be the declarator in it.
8236 ;; If `backup-at-type' is nil then the other variables have
8237 ;; undefined values.
8238 backup-at-type backup-type-start backup-id-start
8239 ;; Set if we've found a specifier (apart from "typedef") that makes
8240 ;; the defined identifier(s) types.
8241 at-type-decl
8242 ;; Set if we've a "typedef" keyword.
8243 at-typedef
8244 ;; Set if we've found a specifier that can start a declaration
8245 ;; where there's no type.
8246 maybe-typeless
8247 ;; Save the value of kwd-sym between loops of the "Check for a
8248 ;; type" loop. Needed to distinguish a C++11 "auto" from a pre
8249 ;; C++11 one.
8250 prev-kwd-sym
8251 ;; If a specifier is found that also can be a type prefix,
8252 ;; these flags are set instead of those above. If we need to
8253 ;; back up an identifier, they are copied to the real flag
8254 ;; variables. Thus they only take effect if we fail to
8255 ;; interpret it as a type.
8256 backup-at-type-decl backup-maybe-typeless
8257 ;; Whether we've found a declaration or a cast. We might know
8258 ;; this before we've found the type in it. It's 'ids if we've
8259 ;; found two consecutive identifiers (usually a sure sign, but
8260 ;; we should allow that in labels too), and t if we've found a
8261 ;; specifier keyword (a 100% sure sign).
8262 at-decl-or-cast
8263 ;; Set when we need to back up to parse this as a declaration
8264 ;; but not as a cast.
8265 backup-if-not-cast
8266 ;; For casts, the return position.
8267 cast-end
8268 ;; Have we got a new-style C++11 "auto"?
8269 new-style-auto
8270 ;; Set when the symbol before `preceding-token-end' is known to
8271 ;; terminate the previous construct, or when we're at point-min.
8272 at-decl-start
8273 ;; Save `c-record-type-identifiers' and
8274 ;; `c-record-ref-identifiers' since ranges are recorded
8275 ;; speculatively and should be thrown away if it turns out
8276 ;; that it isn't a declaration or cast.
8277 (save-rec-type-ids c-record-type-identifiers)
8278 (save-rec-ref-ids c-record-ref-identifiers)
8279 ;; Set when we parse a declaration which might also be an expression,
8280 ;; such as "a *b". See CASE 16 and CASE 17.
8281 maybe-expression)
8283 (save-excursion
8284 (goto-char preceding-token-end)
8285 (setq at-decl-start
8286 (or (bobp)
8287 (let ((tok-end (point)))
8288 (c-backward-token-2)
8289 (member (buffer-substring-no-properties (point) tok-end)
8290 c-pre-start-tokens)))))
8292 (while (c-forward-annotation)
8293 (c-forward-syntactic-ws))
8295 ;; Check for a type. Unknown symbols are treated as possible
8296 ;; types, but they could also be specifiers disguised through
8297 ;; macros like __INLINE__, so we recognize both types and known
8298 ;; specifiers after them too.
8299 (while
8300 (let* ((start (point)) kwd-sym kwd-clause-end found-type noise-start)
8302 (cond
8303 ;; Look for a specifier keyword clause.
8304 ((or (looking-at c-prefix-spec-kwds-re)
8305 (and (c-major-mode-is 'java-mode)
8306 (looking-at "@[A-Za-z0-9]+")))
8307 (save-match-data
8308 (if (looking-at c-typedef-key)
8309 (setq at-typedef t)))
8310 (setq kwd-sym (c-keyword-sym (match-string 1)))
8311 (save-excursion
8312 (c-forward-keyword-clause 1)
8313 (setq kwd-clause-end (point))))
8314 ((and c-opt-cpp-prefix
8315 (looking-at c-noise-macro-with-parens-name-re))
8316 (setq noise-start (point))
8317 (c-forward-noise-clause)
8318 (setq kwd-clause-end (point))))
8320 (when (setq found-type (c-forward-type t)) ; brace-block-too
8321 ;; Found a known or possible type or a prefix of a known type.
8322 (when (and (c-major-mode-is 'c++-mode) ; C++11 style "auto"?
8323 (eq prev-kwd-sym (c-keyword-sym "auto"))
8324 (looking-at "[=(]")) ; FIXME!!! proper regexp.
8325 (setq new-style-auto t)
8326 (setq found-type nil)
8327 (goto-char start)) ; position of foo in "auto foo"
8329 (when at-type
8330 ;; Got two identifiers with nothing but whitespace
8331 ;; between them. That can only happen in declarations.
8332 (setq at-decl-or-cast 'ids)
8334 (when (eq at-type 'found)
8335 ;; If the previous identifier is a found type we
8336 ;; record it as a real one; it might be some sort of
8337 ;; alias for a prefix like "unsigned".
8338 (save-excursion
8339 (goto-char type-start)
8340 (let ((c-promote-possible-types t))
8341 (c-forward-type)))))
8343 (setq backup-at-type at-type
8344 backup-type-start type-start
8345 backup-id-start id-start
8346 at-type found-type
8347 type-start start
8348 id-start (point)
8349 ;; The previous ambiguous specifier/type turned out
8350 ;; to be a type since we've parsed another one after
8351 ;; it, so clear these backup flags.
8352 backup-at-type-decl nil
8353 backup-maybe-typeless nil))
8355 (if (or kwd-sym noise-start)
8356 (progn
8357 ;; Handle known specifier keywords and
8358 ;; `c-decl-hangon-kwds' which can occur after known
8359 ;; types.
8361 (if (or (c-keyword-member kwd-sym 'c-decl-hangon-kwds)
8362 noise-start)
8363 ;; It's a hang-on keyword or noise clause that can occur
8364 ;; anywhere.
8365 (progn
8366 (if at-type
8367 ;; Move the identifier start position if
8368 ;; we've passed a type.
8369 (setq id-start kwd-clause-end)
8370 ;; Otherwise treat this as a specifier and
8371 ;; move the fallback position.
8372 (setq start-pos kwd-clause-end))
8373 (goto-char kwd-clause-end))
8375 ;; It's an ordinary specifier so we know that
8376 ;; anything before this can't be the type.
8377 (setq backup-at-type nil
8378 start-pos kwd-clause-end)
8380 (if found-type
8381 ;; It's ambiguous whether this keyword is a
8382 ;; specifier or a type prefix, so set the backup
8383 ;; flags. (It's assumed that `c-forward-type'
8384 ;; moved further than `c-forward-keyword-clause'.)
8385 (progn
8386 (when (c-keyword-member kwd-sym 'c-typedef-decl-kwds)
8387 (setq backup-at-type-decl t))
8388 (when (c-keyword-member kwd-sym 'c-typeless-decl-kwds)
8389 (setq backup-maybe-typeless t)))
8391 (when (c-keyword-member kwd-sym 'c-typedef-decl-kwds)
8392 ;; This test only happens after we've scanned a type.
8393 ;; So, with valid syntax, kwd-sym can't be 'typedef.
8394 (setq at-type-decl t))
8395 (when (c-keyword-member kwd-sym 'c-typeless-decl-kwds)
8396 (setq maybe-typeless t))
8398 ;; Haven't matched a type so it's an unambiguous
8399 ;; specifier keyword and we know we're in a
8400 ;; declaration.
8401 (setq at-decl-or-cast t)
8402 (setq prev-kwd-sym kwd-sym)
8404 (goto-char kwd-clause-end))))
8406 ;; If the type isn't known we continue so that we'll jump
8407 ;; over all specifiers and type identifiers. The reason
8408 ;; to do this for a known type prefix is to make things
8409 ;; like "unsigned INT16" work.
8410 (and found-type (not (eq found-type t))))))
8412 (cond
8413 ((eq at-type t)
8414 ;; If a known type was found, we still need to skip over any
8415 ;; hangon keyword clauses after it. Otherwise it has already
8416 ;; been done in the loop above.
8417 (while
8418 (cond ((looking-at c-decl-hangon-key)
8419 (c-forward-keyword-clause 1))
8420 ((and c-opt-cpp-prefix
8421 (looking-at c-noise-macro-with-parens-name-re))
8422 (c-forward-noise-clause))))
8423 (setq id-start (point)))
8425 ((eq at-type 'prefix)
8426 ;; A prefix type is itself a primitive type when it's not
8427 ;; followed by another type.
8428 (setq at-type t))
8430 ((not at-type)
8431 ;; Got no type but set things up to continue anyway to handle
8432 ;; the various cases when a declaration doesn't start with a
8433 ;; type.
8434 (setq id-start start-pos))
8436 ((and (eq at-type 'maybe)
8437 (c-major-mode-is 'c++-mode))
8438 ;; If it's C++ then check if the last "type" ends on the form
8439 ;; "foo::foo" or "foo::~foo", i.e. if it's the name of a
8440 ;; (con|de)structor.
8441 (save-excursion
8442 (let (name end-2 end-1)
8443 (goto-char id-start)
8444 (c-backward-syntactic-ws)
8445 (setq end-2 (point))
8446 (when (and
8447 (c-simple-skip-symbol-backward)
8448 (progn
8449 (setq name
8450 (buffer-substring-no-properties (point) end-2))
8451 ;; Cheating in the handling of syntactic ws below.
8452 (< (skip-chars-backward ":~ \t\n\r\v\f") 0))
8453 (progn
8454 (setq end-1 (point))
8455 (c-simple-skip-symbol-backward))
8456 (>= (point) type-start)
8457 (equal (buffer-substring-no-properties (point) end-1)
8458 name)
8459 (goto-char end-2)
8460 (progn
8461 (c-forward-syntactic-ws)
8462 (eq (char-after) ?\()))
8463 ;; It is a (con|de)structor name. In that case the
8464 ;; declaration is typeless so zap out any preceding
8465 ;; identifier(s) that we might have taken as types.
8466 (goto-char type-start)
8467 (setq at-type nil
8468 backup-at-type nil
8469 id-start type-start))))))
8471 ;; Check for and step over a type decl expression after the thing
8472 ;; that is or might be a type. This can't be skipped since we
8473 ;; need the correct end position of the declarator for
8474 ;; `max-type-decl-end-*'.
8475 (let ((start (point)) (paren-depth 0) pos
8476 ;; True if there's a non-open-paren match of
8477 ;; `c-type-decl-prefix-key'.
8478 got-prefix
8479 ;; True if the declarator is surrounded by a parenthesis pair.
8480 got-parens
8481 ;; True if there is an identifier in the declarator.
8482 got-identifier
8483 ;; True if there's a non-close-paren match of
8484 ;; `c-type-decl-suffix-key'.
8485 got-suffix
8486 ;; True if there's a prefix match outside the outermost
8487 ;; paren pair that surrounds the declarator.
8488 got-prefix-before-parens
8489 ;; True if there's a suffix match outside the outermost
8490 ;; paren pair that surrounds the declarator. The value is
8491 ;; the position of the first suffix match.
8492 got-suffix-after-parens
8493 ;; True if we've parsed the type decl to a token that is
8494 ;; known to end declarations in this context.
8495 at-decl-end
8496 ;; The earlier values of `at-type' and `type-start' if we've
8497 ;; shifted the type backwards.
8498 identifier-type identifier-start
8499 ;; If `c-parse-and-markup-<>-arglists' is set we need to
8500 ;; turn it off during the name skipping below to avoid
8501 ;; getting `c-type' properties that might be bogus. That
8502 ;; can happen since we don't know if
8503 ;; `c-restricted-<>-arglists' will be correct inside the
8504 ;; arglist paren that gets entered.
8505 c-parse-and-markup-<>-arglists
8506 ;; Start of the identifier for which `got-identifier' was set.
8507 name-start
8508 ;; Position after (innermost) open parenthesis encountered in the
8509 ;; prefix operators.
8510 after-paren-pos)
8512 (goto-char id-start)
8514 ;; Skip over type decl prefix operators. (Note similar code in
8515 ;; `c-forward-declarator'.)
8516 (if (and c-recognize-typeless-decls
8517 (equal c-type-decl-prefix-key "\\<\\>"))
8518 (when (eq (char-after) ?\()
8519 (progn
8520 (setq paren-depth (1+ paren-depth))
8521 (forward-char)
8522 (setq after-paren-pos (point))))
8523 (while (and (looking-at c-type-decl-prefix-key)
8524 (if (and (c-major-mode-is 'c++-mode)
8525 (match-beginning 3))
8526 ;; If the third submatch matches in C++ then
8527 ;; we're looking at an identifier that's a
8528 ;; prefix only if it specifies a member pointer.
8529 (when (progn (setq pos (point))
8530 (setq got-identifier (c-forward-name)))
8531 (setq name-start pos)
8532 (if (looking-at "\\(::\\)")
8533 ;; We only check for a trailing "::" and
8534 ;; let the "*" that should follow be
8535 ;; matched in the next round.
8536 (progn (setq got-identifier nil) t)
8537 ;; It turned out to be the real identifier,
8538 ;; so stop.
8539 nil))
8542 (if (eq (char-after) ?\()
8543 (progn
8544 (setq paren-depth (1+ paren-depth))
8545 (forward-char)
8546 (setq after-paren-pos (point)))
8547 (unless got-prefix-before-parens
8548 (setq got-prefix-before-parens (= paren-depth 0)))
8549 (setq got-prefix t)
8550 (goto-char (match-end 1)))
8551 (c-forward-syntactic-ws)))
8553 (setq got-parens (> paren-depth 0))
8555 ;; Try to skip over an identifier.
8556 (or got-identifier
8557 (and (looking-at c-identifier-start)
8558 (setq pos (point))
8559 (setq got-identifier (c-forward-name))
8560 (setq name-start pos)))
8562 ;; Skip over type decl suffix operators and trailing noise macros.
8563 (while
8564 (cond
8565 ((and c-opt-cpp-prefix
8566 (looking-at c-noise-macro-with-parens-name-re))
8567 (c-forward-noise-clause))
8569 ((looking-at c-type-decl-suffix-key)
8570 (if (eq (char-after) ?\))
8571 (when (> paren-depth 0)
8572 (setq paren-depth (1- paren-depth))
8573 (forward-char)
8575 (when (if (save-match-data (looking-at "\\s("))
8576 (c-safe (c-forward-sexp 1) t)
8577 (goto-char (match-end 1))
8579 (when (and (not got-suffix-after-parens)
8580 (= paren-depth 0))
8581 (setq got-suffix-after-parens (match-beginning 0)))
8582 (setq got-suffix t))))
8585 ;; No suffix matched. We might have matched the
8586 ;; identifier as a type and the open paren of a
8587 ;; function arglist as a type decl prefix. In that
8588 ;; case we should "backtrack": Reinterpret the last
8589 ;; type as the identifier, move out of the arglist and
8590 ;; continue searching for suffix operators.
8592 ;; Do this even if there's no preceding type, to cope
8593 ;; with old style function declarations in K&R C,
8594 ;; (con|de)structors in C++ and `c-typeless-decl-kwds'
8595 ;; style declarations. That isn't applicable in an
8596 ;; arglist context, though.
8597 (when (and (= paren-depth 1)
8598 (not got-prefix-before-parens)
8599 (not (eq at-type t))
8600 (or backup-at-type
8601 maybe-typeless
8602 backup-maybe-typeless
8603 (when c-recognize-typeless-decls
8604 (and (memq context '(nil top))
8605 ;; Deal with C++11's "copy-initialization"
8606 ;; where we have <type>(<constant>), by
8607 ;; contrasting with a typeless
8608 ;; <name>(<type><parameter>, ...).
8609 (save-excursion
8610 (goto-char after-paren-pos)
8611 (c-forward-syntactic-ws)
8612 (c-forward-type)))))
8613 (setq pos (c-up-list-forward (point)))
8614 (eq (char-before pos) ?\)))
8615 (c-fdoc-shift-type-backward)
8616 (goto-char pos)
8617 t)))
8619 (c-forward-syntactic-ws))
8621 (when (or (and new-style-auto
8622 (looking-at c-auto-ops-re))
8623 (and (or maybe-typeless backup-maybe-typeless)
8624 (not got-identifier)
8625 (not got-prefix)
8626 at-type))
8627 ;; Have found no identifier but `c-typeless-decl-kwds' has
8628 ;; matched so we know we're inside a declaration. The
8629 ;; preceding type must be the identifier instead.
8630 (c-fdoc-shift-type-backward))
8632 ;; Prepare the "-> type;" for fontification later on.
8633 (when (and new-style-auto
8634 (looking-at c-haskell-op-re))
8635 (save-excursion
8636 (goto-char (match-end 0))
8637 (c-forward-syntactic-ws)
8638 (setq type-start (point))
8639 (setq at-type (c-forward-type))))
8641 ;; Move forward over any "WS" ids (like "final" or "override" in C++)
8642 (while (looking-at c-type-decl-suffix-ws-ids-key)
8643 (goto-char (match-end 1))
8644 (c-forward-syntactic-ws))
8646 (setq
8647 at-decl-or-cast
8648 (catch 'at-decl-or-cast
8650 ;; CASE 1
8651 (when (> paren-depth 0)
8652 ;; Encountered something inside parens that isn't matched by
8653 ;; the `c-type-decl-*' regexps, so it's not a type decl
8654 ;; expression. Try to skip out to the same paren depth to
8655 ;; not confuse the cast check below. If we don't manage this and
8656 ;; `at-decl-or-cast' is 'ids we might have an expression like
8657 ;; "foo bar ({ ..." which is a valid C++11 initialization.
8658 (if (and (not (c-safe (goto-char (scan-lists (point) 1 paren-depth))))
8659 (eq at-decl-or-cast 'ids))
8660 (c-fdoc-shift-type-backward))
8661 ;; If we've found a specifier keyword then it's a
8662 ;; declaration regardless.
8663 (throw 'at-decl-or-cast (memq at-decl-or-cast '(t ids))))
8665 (setq at-decl-end
8666 (looking-at (cond ((eq context '<>) "[,>]")
8667 ((not (memq context '(nil top))) "[,\)]")
8668 (t "[,;]"))))
8670 ;; Now we've collected info about various characteristics of
8671 ;; the construct we're looking at. Below follows a decision
8672 ;; tree based on that. It's ordered to check more certain
8673 ;; signs before less certain ones.
8675 (if got-identifier
8676 (progn
8678 ;; CASE 2
8679 (when (and (or at-type maybe-typeless)
8680 (not (or got-prefix got-parens)))
8681 ;; Got another identifier directly after the type, so it's a
8682 ;; declaration.
8683 (throw 'at-decl-or-cast t))
8685 (when (and got-parens
8686 (not got-prefix)
8687 ;; (not got-suffix-after-parens)
8688 (or backup-at-type
8689 maybe-typeless
8690 backup-maybe-typeless
8691 (eq at-decl-or-cast t)
8692 ;; Check whether we have "bar (gnu);" where we
8693 ;; are directly inside a class (etc.) called "bar".
8694 (save-excursion
8695 (and
8696 (progn
8697 (goto-char name-start)
8698 (not (memq (c-forward-type) '(nil maybe))))
8699 (progn
8700 (goto-char id-start)
8701 (c-directly-in-class-called-p
8702 (buffer-substring
8703 type-start
8704 (progn
8705 (goto-char type-start)
8706 (c-forward-type)
8707 (c-backward-syntactic-ws)
8708 (point)))))))))
8709 ;; Got a declaration of the form "foo bar (gnu);" or "bar
8710 ;; (gnu);" where we've recognized "bar" as the type and "gnu"
8711 ;; as the declarator, and in the latter case, checked that
8712 ;; "bar (gnu)" appears directly inside the class "bar". In
8713 ;; this case it's however more likely that "bar" is the
8714 ;; declarator and "gnu" a function argument or initializer
8715 ;; (if `c-recognize-paren-inits' is set), since the parens
8716 ;; around "gnu" would be superfluous if it's a declarator.
8717 ;; Shift the type one step backward.
8718 (c-fdoc-shift-type-backward)))
8720 ;; Found no identifier.
8722 (if backup-at-type
8723 (progn
8725 ;; CASE 3
8726 (when (= (point) start)
8727 ;; Got a plain list of identifiers. If a colon follows it's
8728 ;; a valid label, or maybe a bitfield. Otherwise the last
8729 ;; one probably is the declared identifier and we should
8730 ;; back up to the previous type, providing it isn't a cast.
8731 (if (and (eq (char-after) ?:)
8732 (not (c-major-mode-is 'java-mode)))
8733 (cond
8734 ;; If we've found a specifier keyword then it's a
8735 ;; declaration regardless.
8736 ((eq at-decl-or-cast t)
8737 (throw 'at-decl-or-cast t))
8738 ((and c-has-bitfields
8739 (eq at-decl-or-cast 'ids)) ; bitfield.
8740 (setq backup-if-not-cast t)
8741 (throw 'at-decl-or-cast t)))
8743 (setq backup-if-not-cast t)
8744 (throw 'at-decl-or-cast t)))
8746 ;; CASE 4
8747 (when (and got-suffix
8748 (not got-prefix)
8749 (not got-parens))
8750 ;; Got a plain list of identifiers followed by some suffix.
8751 ;; If this isn't a cast then the last identifier probably is
8752 ;; the declared one and we should back up to the previous
8753 ;; type.
8754 (setq backup-if-not-cast t)
8755 (throw 'at-decl-or-cast t)))
8757 ;; CASE 5
8758 (when (eq at-type t)
8759 ;; If the type is known we know that there can't be any
8760 ;; identifier somewhere else, and it's only in declarations in
8761 ;; e.g. function prototypes and in casts that the identifier may
8762 ;; be left out.
8763 (throw 'at-decl-or-cast t))
8765 (when (= (point) start)
8766 ;; Only got a single identifier (parsed as a type so far).
8767 ;; CASE 6
8768 (if (and
8769 ;; Check that the identifier isn't at the start of an
8770 ;; expression.
8771 at-decl-end
8772 (cond
8773 ((eq context 'decl)
8774 ;; Inside an arglist that contains declarations. If K&R
8775 ;; style declarations and parenthesis style initializers
8776 ;; aren't allowed then the single identifier must be a
8777 ;; type, else we require that it's known or found
8778 ;; (primitive types are handled above).
8779 (or (and (not c-recognize-knr-p)
8780 (not c-recognize-paren-inits))
8781 (memq at-type '(known found))))
8782 ((eq context '<>)
8783 ;; Inside a template arglist. Accept known and found
8784 ;; types; other identifiers could just as well be
8785 ;; constants in C++.
8786 (memq at-type '(known found)))))
8787 (throw 'at-decl-or-cast t)
8788 ;; CASE 7
8789 ;; Can't be a valid declaration or cast, but if we've found a
8790 ;; specifier it can't be anything else either, so treat it as
8791 ;; an invalid/unfinished declaration or cast.
8792 (throw 'at-decl-or-cast at-decl-or-cast))))
8794 (if (and got-parens
8795 (not got-prefix)
8796 (memq context '(nil top))
8797 (not (eq at-type t))
8798 (or backup-at-type
8799 maybe-typeless
8800 backup-maybe-typeless
8801 (when c-recognize-typeless-decls
8802 (or (not got-suffix)
8803 (not (looking-at
8804 c-after-suffixed-type-maybe-decl-key))))))
8805 ;; Got an empty paren pair and a preceding type that probably
8806 ;; really is the identifier. Shift the type backwards to make
8807 ;; the last one the identifier. This is analogous to the
8808 ;; "backtracking" done inside the `c-type-decl-suffix-key' loop
8809 ;; above.
8811 ;; Exception: In addition to the conditions in that
8812 ;; "backtracking" code, do not shift backward if we're not
8813 ;; looking at either `c-after-suffixed-type-decl-key' or "[;,]".
8814 ;; Since there's no preceding type, the shift would mean that
8815 ;; the declaration is typeless. But if the regexp doesn't match
8816 ;; then we will simply fall through in the tests below and not
8817 ;; recognize it at all, so it's better to try it as an abstract
8818 ;; declarator instead.
8819 (c-fdoc-shift-type-backward)
8821 ;; Still no identifier.
8822 ;; CASE 8
8823 (when (and got-prefix (or got-parens got-suffix))
8824 ;; Require `got-prefix' together with either `got-parens' or
8825 ;; `got-suffix' to recognize it as an abstract declarator:
8826 ;; `got-parens' only is probably an empty function call.
8827 ;; `got-suffix' only can build an ordinary expression together
8828 ;; with the preceding identifier which we've taken as a type.
8829 ;; We could actually accept on `got-prefix' only, but that can
8830 ;; easily occur temporarily while writing an expression so we
8831 ;; avoid that case anyway. We could do a better job if we knew
8832 ;; the point when the fontification was invoked.
8833 (throw 'at-decl-or-cast t))
8835 ;; CASE 9
8836 (when (and at-type
8837 (not got-prefix)
8838 (not got-parens)
8839 got-suffix-after-parens
8840 (eq (char-after got-suffix-after-parens) ?\())
8841 ;; Got a type, no declarator but a paren suffix. I.e. it's a
8842 ;; normal function call after all (or perhaps a C++ style object
8843 ;; instantiation expression).
8844 (throw 'at-decl-or-cast nil))))
8846 ;; CASE 9.5
8847 (when (and (not context) ; i.e. not at top level.
8848 (c-major-mode-is 'c++-mode)
8849 (eq at-decl-or-cast 'ids)
8850 after-paren-pos)
8851 ;; We've got something like "foo bar (...)" in C++ which isn't at
8852 ;; the top level. This is probably a uniform initialization of bar
8853 ;; to the contents of the parens. In this case the declarator ends
8854 ;; at the open paren.
8855 (goto-char (1- after-paren-pos))
8856 (throw 'at-decl-or-cast t))
8858 ;; CASE 10
8859 (when at-decl-or-cast
8860 ;; By now we've located the type in the declaration that we know
8861 ;; we're in.
8862 (throw 'at-decl-or-cast t))
8864 ;; CASE 11
8865 (when (and got-identifier
8866 (looking-at c-after-suffixed-type-decl-key)
8867 (or (eq context 'top)
8868 (and (eq context nil)
8869 (match-beginning 1)))
8870 (if (and got-parens
8871 (not got-prefix)
8872 (not got-suffix)
8873 (not (eq at-type t)))
8874 ;; Shift the type backward in the case that there's a
8875 ;; single identifier inside parens. That can only
8876 ;; occur in K&R style function declarations so it's
8877 ;; more likely that it really is a function call.
8878 ;; Therefore we only do this after
8879 ;; `c-after-suffixed-type-decl-key' has matched.
8880 (progn (c-fdoc-shift-type-backward) t)
8881 got-suffix-after-parens))
8882 ;; A declaration according to `c-after-suffixed-type-decl-key'.
8883 (throw 'at-decl-or-cast t))
8885 ;; CASE 12
8886 (when (and (or got-prefix (not got-parens))
8887 (memq at-type '(t known)))
8888 ;; It's a declaration if a known type precedes it and it can't be a
8889 ;; function call.
8890 (throw 'at-decl-or-cast t))
8892 ;; If we get here we can't tell if this is a type decl or a normal
8893 ;; expression by looking at it alone. (That's under the assumption
8894 ;; that normal expressions always can look like type decl expressions,
8895 ;; which isn't really true but the cases where it doesn't hold are so
8896 ;; uncommon (e.g. some placements of "const" in C++) it's not worth
8897 ;; the effort to look for them.)
8899 ;;; 2008-04-16: commented out the next form, to allow the function to recognize
8900 ;;; "foo (int bar)" in CC (an implicit type (in class foo) without a semicolon)
8901 ;;; as a(n almost complete) declaration, enabling it to be fontified.
8902 ;; CASE 13
8903 ;; (unless (or at-decl-end (looking-at "=[^=]"))
8904 ;; If this is a declaration it should end here or its initializer(*)
8905 ;; should start here, so check for allowed separation tokens. Note
8906 ;; that this rule doesn't work e.g. with a K&R arglist after a
8907 ;; function header.
8909 ;; *) Don't check for C++ style initializers using parens
8910 ;; since those already have been matched as suffixes.
8912 ;; If `at-decl-or-cast' is then we've found some other sign that
8913 ;; it's a declaration or cast, so then it's probably an
8914 ;; invalid/unfinished one.
8915 ;; (throw 'at-decl-or-cast at-decl-or-cast))
8917 ;; Below are tests that only should be applied when we're certain to
8918 ;; not have parsed halfway through an expression.
8920 ;; CASE 14
8921 (when (memq at-type '(t known))
8922 ;; The expression starts with a known type so treat it as a
8923 ;; declaration.
8924 (throw 'at-decl-or-cast t))
8926 ;; CASE 15
8927 (when (and (c-major-mode-is 'c++-mode)
8928 ;; In C++ we check if the identifier is a known type, since
8929 ;; (con|de)structors use the class name as identifier.
8930 ;; We've always shifted over the identifier as a type and
8931 ;; then backed up again in this case.
8932 identifier-type
8933 (or (memq identifier-type '(found known))
8934 (and (eq (char-after identifier-start) ?~)
8935 ;; `at-type' probably won't be 'found for
8936 ;; destructors since the "~" is then part of the
8937 ;; type name being checked against the list of
8938 ;; known types, so do a check without that
8939 ;; operator.
8940 (or (save-excursion
8941 (goto-char (1+ identifier-start))
8942 (c-forward-syntactic-ws)
8943 (c-with-syntax-table
8944 c-identifier-syntax-table
8945 (looking-at c-known-type-key)))
8946 (save-excursion
8947 (goto-char (1+ identifier-start))
8948 ;; We have already parsed the type earlier,
8949 ;; so it'd be possible to cache the end
8950 ;; position instead of redoing it here, but
8951 ;; then we'd need to keep track of another
8952 ;; position everywhere.
8953 (c-check-type (point)
8954 (progn (c-forward-type)
8955 (point))))))))
8956 (throw 'at-decl-or-cast t))
8958 (if got-identifier
8959 (progn
8960 ;; CASE 16
8961 (when (and got-prefix-before-parens
8962 at-type
8963 (or at-decl-end (looking-at "=[^=]"))
8964 (memq context '(nil top))
8965 (or (not got-suffix)
8966 at-decl-start))
8967 ;; Got something like "foo * bar;". Since we're not inside
8968 ;; an arglist it would be a meaningless expression because
8969 ;; the result isn't used. We therefore choose to recognize
8970 ;; it as a declaration. We only allow a suffix (which makes
8971 ;; the construct look like a function call) when
8972 ;; `at-decl-start' provides additional evidence that we do
8973 ;; have a declaration.
8974 (setq maybe-expression t)
8975 (throw 'at-decl-or-cast t))
8977 ;; CASE 17
8978 (when (and (or got-suffix-after-parens
8979 (looking-at "=[^=]"))
8980 (eq at-type 'found)
8981 (not (eq context 'arglist)))
8982 ;; Got something like "a (*b) (c);" or "a (b) = c;". It could
8983 ;; be an odd expression or it could be a declaration. Treat
8984 ;; it as a declaration if "a" has been used as a type
8985 ;; somewhere else (if it's a known type we won't get here).
8986 (setq maybe-expression t)
8987 (throw 'at-decl-or-cast t))
8989 ;; CASE 17.5
8990 (when (and c-asymmetry-fontification-flag
8991 got-prefix-before-parens
8992 at-type
8993 (or (not got-suffix)
8994 at-decl-start))
8995 (let ((space-before-id
8996 (save-excursion
8997 (goto-char name-start)
8998 (or (bolp) (memq (char-before) '(?\ ?\t)))))
8999 (space-after-type
9000 (save-excursion
9001 (goto-char type-start)
9002 (and (c-forward-type)
9003 (progn (c-backward-syntactic-ws) t)
9004 (or (eolp)
9005 (memq (char-after) '(?\ ?\t)))))))
9006 (when (not (eq (not space-before-id)
9007 (not space-after-type)))
9008 (setq maybe-expression t)
9009 (throw 'at-decl-or-cast t)))))
9011 ;; CASE 18
9012 (when (and (not (memq context '(nil top)))
9013 (or got-prefix
9014 (and (eq context 'decl)
9015 (not c-recognize-paren-inits)
9016 (or got-parens got-suffix))))
9017 ;; Got a type followed by an abstract declarator. If `got-prefix'
9018 ;; is set it's something like "a *" without anything after it. If
9019 ;; `got-parens' or `got-suffix' is set it's "a()", "a[]", "a()[]",
9020 ;; or similar, which we accept only if the context rules out
9021 ;; expressions.
9022 (throw 'at-decl-or-cast t)))
9024 ;; If we had a complete symbol table here (which rules out
9025 ;; `c-found-types') we should return t due to the disambiguation rule
9026 ;; (in at least C++) that anything that can be parsed as a declaration
9027 ;; is a declaration. Now we're being more defensive and prefer to
9028 ;; highlight things like "foo (bar);" as a declaration only if we're
9029 ;; inside an arglist that contains declarations.
9030 ;; CASE 19
9031 (eq context 'decl))))
9033 ;; The point is now after the type decl expression.
9035 (cond
9036 ;; Check for a cast.
9037 ((save-excursion
9038 (and
9039 c-cast-parens
9041 ;; Should be the first type/identifier in a cast paren.
9042 (> preceding-token-end (point-min))
9043 (memq (char-before preceding-token-end) c-cast-parens)
9045 ;; The closing paren should follow.
9046 (progn
9047 (c-forward-syntactic-ws)
9048 (looking-at "\\s)"))
9050 ;; There should be a primary expression after it.
9051 (let (pos)
9052 (forward-char)
9053 (c-forward-syntactic-ws)
9054 (setq cast-end (point))
9055 (and (looking-at c-primary-expr-regexp)
9056 (progn
9057 (setq pos (match-end 0))
9059 ;; Check if the expression begins with a prefix keyword.
9060 (match-beginning 2)
9061 (if (match-beginning 1)
9062 ;; Expression begins with an ambiguous operator. Treat
9063 ;; it as a cast if it's a type decl or if we've
9064 ;; recognized the type somewhere else.
9065 (or at-decl-or-cast
9066 (memq at-type '(t known found)))
9067 ;; Unless it's a keyword, it's the beginning of a primary
9068 ;; expression.
9069 (not (looking-at c-keywords-regexp)))))
9070 ;; If `c-primary-expr-regexp' matched a nonsymbol token, check
9071 ;; that it matched a whole one so that we don't e.g. confuse
9072 ;; the operator '-' with '->'. It's ok if it matches further,
9073 ;; though, since it e.g. can match the float '.5' while the
9074 ;; operator regexp only matches '.'.
9075 (or (not (looking-at c-nonsymbol-token-regexp))
9076 (<= (match-end 0) pos))))
9078 ;; There should either be a cast before it or something that isn't an
9079 ;; identifier or close paren.
9080 (> preceding-token-end (point-min))
9081 (progn
9082 (goto-char (1- preceding-token-end))
9083 (or (eq (point) last-cast-end)
9084 (progn
9085 (c-backward-syntactic-ws)
9086 (if (< (skip-syntax-backward "w_") 0)
9087 ;; It's a symbol. Accept it only if it's one of the
9088 ;; keywords that can precede an expression (without
9089 ;; surrounding parens).
9090 (looking-at c-simple-stmt-key)
9091 (and
9092 ;; Check that it isn't a close paren (block close is ok,
9093 ;; though).
9094 (not (memq (char-before) '(?\) ?\])))
9095 ;; Check that it isn't a nonsymbol identifier.
9096 (not (c-on-identifier)))))))))
9098 ;; Handle the cast.
9099 (when (and c-record-type-identifiers at-type (not (eq at-type t)))
9100 (let ((c-promote-possible-types t))
9101 (goto-char type-start)
9102 (c-forward-type)))
9104 (goto-char cast-end)
9105 'cast)
9107 (at-decl-or-cast
9108 ;; We're at a declaration. Highlight the type and the following
9109 ;; declarators.
9111 (when backup-if-not-cast
9112 (c-fdoc-shift-type-backward t))
9114 (when (and (eq context 'decl) (looking-at ","))
9115 ;; Make sure to propagate the `c-decl-arg-start' property to
9116 ;; the next argument if it's set in this one, to cope with
9117 ;; interactive refontification.
9118 (c-put-c-type-property (point) 'c-decl-arg-start))
9120 ;; Record the type's coordinates in `c-record-type-identifiers' for
9121 ;; later fontification.
9122 (when (and c-record-type-identifiers at-type ;; (not (eq at-type t))
9123 ;; There seems no reason to exclude a token from
9124 ;; fontification just because it's "a known type that can't
9125 ;; be a name or other expression". 2013-09-18.
9127 (let ((c-promote-possible-types t))
9128 (save-excursion
9129 (goto-char type-start)
9130 (c-forward-type))))
9132 (list id-start
9133 (and (or at-type-decl at-typedef)
9134 (cons at-type-decl at-typedef))
9135 maybe-expression
9136 type-start))
9139 ;; False alarm. Restore the recorded ranges.
9140 (setq c-record-type-identifiers save-rec-type-ids
9141 c-record-ref-identifiers save-rec-ref-ids)
9142 nil))))
9144 (defun c-forward-label (&optional assume-markup preceding-token-end limit)
9145 ;; Assuming that point is at the beginning of a token, check if it starts a
9146 ;; label and if so move over it and return non-nil (t in default situations,
9147 ;; specific symbols (see below) for interesting situations), otherwise don't
9148 ;; move and return nil. "Label" here means "most things with a colon".
9150 ;; More precisely, a "label" is regarded as one of:
9151 ;; (i) a goto target like "foo:" - returns the symbol `goto-target';
9152 ;; (ii) A case label - either the entire construct "case FOO:", or just the
9153 ;; bare "case", should the colon be missing. We return t;
9154 ;; (iii) a keyword which needs a colon, like "default:" or "private:"; We
9155 ;; return t;
9156 ;; (iv) One of QT's "extended" C++ variants of
9157 ;; "private:"/"protected:"/"public:"/"more:" looking like "public slots:".
9158 ;; Returns the symbol `qt-2kwds-colon'.
9159 ;; (v) QT's construct "signals:". Returns the symbol `qt-1kwd-colon'.
9160 ;; (vi) One of the keywords matched by `c-opt-extra-label-key' (without any
9161 ;; colon). Currently (2006-03), this applies only to Objective C's
9162 ;; keywords "@private", "@protected", and "@public". Returns t.
9164 ;; One of the things which will NOT be recognized as a label is a bit-field
9165 ;; element of a struct, something like "int foo:5".
9167 ;; The end of the label is taken to be just after the colon, or the end of
9168 ;; the first submatch in `c-opt-extra-label-key'. The point is directly
9169 ;; after the end on return. The terminating char gets marked with
9170 ;; `c-decl-end' to improve recognition of the following declaration or
9171 ;; statement.
9173 ;; If ASSUME-MARKUP is non-nil, it's assumed that the preceding
9174 ;; label, if any, has already been marked up like that.
9176 ;; If PRECEDING-TOKEN-END is given, it should be the first position
9177 ;; after the preceding token, i.e. on the other side of the
9178 ;; syntactic ws from the point. Use a value less than or equal to
9179 ;; (point-min) if the point is at the first token in (the visible
9180 ;; part of) the buffer.
9182 ;; The optional LIMIT limits the forward scan for the colon.
9184 ;; This function records the ranges of the label symbols on
9185 ;; `c-record-ref-identifiers' if `c-record-type-identifiers' (!) is
9186 ;; non-nil.
9188 ;; This function might do hidden buffer changes.
9190 (let ((start (point))
9191 label-end
9192 qt-symbol-idx
9193 macro-start ; if we're in one.
9194 label-type
9195 kwd)
9196 (cond
9197 ;; "case" or "default" (Doesn't apply to AWK).
9198 ((looking-at c-label-kwds-regexp)
9199 (let ((kwd-end (match-end 1)))
9200 ;; Record only the keyword itself for fontification, since in
9201 ;; case labels the following is a constant expression and not
9202 ;; a label.
9203 (when c-record-type-identifiers
9204 (c-record-ref-id (cons (match-beginning 1) kwd-end)))
9206 ;; Find the label end.
9207 (goto-char kwd-end)
9208 (setq label-type
9209 (if (and (c-syntactic-re-search-forward
9210 ;; Stop on chars that aren't allowed in expressions,
9211 ;; and on operator chars that would be meaningless
9212 ;; there. FIXME: This doesn't cope with ?: operators.
9213 "[;{=,@]\\|\\(\\=\\|[^:]\\):\\([^:]\\|\\'\\)"
9214 limit t t nil 1)
9215 (match-beginning 2))
9217 (progn ; there's a proper :
9218 (goto-char (match-beginning 2)) ; just after the :
9219 (c-put-c-type-property (1- (point)) 'c-decl-end)
9222 ;; It's an unfinished label. We consider the keyword enough
9223 ;; to recognize it as a label, so that it gets fontified.
9224 ;; Leave the point at the end of it, but don't put any
9225 ;; `c-decl-end' marker.
9226 (goto-char kwd-end)
9227 t))))
9229 ;; @private, @protected, @public, in Objective C, or similar.
9230 ((and c-opt-extra-label-key
9231 (looking-at c-opt-extra-label-key))
9232 ;; For a `c-opt-extra-label-key' match, we record the whole
9233 ;; thing for fontification. That's to get the leading '@' in
9234 ;; Objective-C protection labels fontified.
9235 (goto-char (match-end 1))
9236 (when c-record-type-identifiers
9237 (c-record-ref-id (cons (match-beginning 1) (point))))
9238 (c-put-c-type-property (1- (point)) 'c-decl-end)
9239 (setq label-type t))
9241 ;; All other cases of labels.
9242 ((and c-recognize-colon-labels ; nil for AWK and IDL, otherwise t.
9244 ;; A colon label must have something before the colon.
9245 (not (eq (char-after) ?:))
9247 ;; Check that we're not after a token that can't precede a label.
9249 ;; Trivially succeeds when there's no preceding token.
9250 ;; Succeeds when we're at a virtual semicolon.
9251 (if preceding-token-end
9252 (<= preceding-token-end (point-min))
9253 (save-excursion
9254 (c-backward-syntactic-ws)
9255 (setq preceding-token-end (point))
9256 (or (bobp)
9257 (c-at-vsemi-p))))
9259 ;; Check if we're after a label, if we're after a closing
9260 ;; paren that belong to statement, and with
9261 ;; `c-label-prefix-re'. It's done in different order
9262 ;; depending on `assume-markup' since the checks have
9263 ;; different expensiveness.
9264 (if assume-markup
9266 (eq (c-get-char-property (1- preceding-token-end) 'c-type)
9267 'c-decl-end)
9269 (save-excursion
9270 (goto-char (1- preceding-token-end))
9271 (c-beginning-of-current-token)
9272 (or (looking-at c-label-prefix-re)
9273 (looking-at c-block-stmt-1-key)))
9275 (and (eq (char-before preceding-token-end) ?\))
9276 (c-after-conditional)))
9279 (save-excursion
9280 (goto-char (1- preceding-token-end))
9281 (c-beginning-of-current-token)
9282 (or (looking-at c-label-prefix-re)
9283 (looking-at c-block-stmt-1-key)))
9285 (cond
9286 ((eq (char-before preceding-token-end) ?\))
9287 (c-after-conditional))
9289 ((eq (char-before preceding-token-end) ?:)
9290 ;; Might be after another label, so check it recursively.
9291 (save-restriction
9292 (save-excursion
9293 (goto-char (1- preceding-token-end))
9294 ;; Essentially the same as the
9295 ;; `c-syntactic-re-search-forward' regexp below.
9296 (setq macro-start
9297 (save-excursion (and (c-beginning-of-macro)
9298 (point))))
9299 (if macro-start (narrow-to-region macro-start (point-max)))
9300 (c-syntactic-skip-backward "^-]:?;}=*/%&|,<>!@+" nil t)
9301 ;; Note: the following should work instead of the
9302 ;; narrow-to-region above. Investigate why not,
9303 ;; sometime. ACM, 2006-03-31.
9304 ;; (c-syntactic-skip-backward "^-]:?;}=*/%&|,<>!@+"
9305 ;; macro-start t)
9306 (let ((pte (point))
9307 ;; If the caller turned on recording for us,
9308 ;; it shouldn't apply when we check the
9309 ;; preceding label.
9310 c-record-type-identifiers)
9311 ;; A label can't start at a cpp directive. Check for
9312 ;; this, since c-forward-syntactic-ws would foul up on it.
9313 (unless (and c-opt-cpp-prefix (looking-at c-opt-cpp-prefix))
9314 (c-forward-syntactic-ws)
9315 (c-forward-label nil pte start))))))))))
9317 ;; Point is still at the beginning of the possible label construct.
9319 ;; Check that the next nonsymbol token is ":", or that we're in one
9320 ;; of QT's "slots" declarations. Allow '(' for the sake of macro
9321 ;; arguments. FIXME: Should build this regexp from the language
9322 ;; constants.
9323 (cond
9324 ;; public: protected: private:
9325 ((and
9326 (c-major-mode-is 'c++-mode)
9327 (search-forward-regexp
9328 "\\=p\\(r\\(ivate\\|otected\\)\\|ublic\\)\\>[^_]" nil t)
9329 (progn (backward-char)
9330 (c-forward-syntactic-ws limit)
9331 (looking-at ":\\([^:]\\|\\'\\)"))) ; A single colon.
9332 (forward-char)
9333 (setq label-type t))
9334 ;; QT double keyword like "protected slots:" or goto target.
9335 ((progn (goto-char start) nil))
9336 ((when (c-syntactic-re-search-forward
9337 "[ \t\n[:?;{=*/%&|,<>!@+-]" limit t t) ; not at EOB
9338 (backward-char)
9339 (setq label-end (point))
9340 (setq qt-symbol-idx
9341 (and (c-major-mode-is 'c++-mode)
9342 (string-match
9343 "\\(p\\(r\\(ivate\\|otected\\)\\|ublic\\)\\|more\\)\\>"
9344 (buffer-substring start (point)))))
9345 (c-forward-syntactic-ws limit)
9346 (cond
9347 ((looking-at ":\\([^:]\\|\\'\\)") ; A single colon.
9348 (forward-char)
9349 (setq label-type
9350 (if (or (string= "signals" ; Special QT macro
9351 (setq kwd (buffer-substring-no-properties start label-end)))
9352 (string= "Q_SIGNALS" kwd))
9353 'qt-1kwd-colon
9354 'goto-target)))
9355 ((and qt-symbol-idx
9356 (search-forward-regexp "\\=\\(slots\\|Q_SLOTS\\)\\>" limit t)
9357 (progn (c-forward-syntactic-ws limit)
9358 (looking-at ":\\([^:]\\|\\'\\)"))) ; A single colon
9359 (forward-char)
9360 (setq label-type 'qt-2kwds-colon)))))))
9362 (save-restriction
9363 (narrow-to-region start (point))
9365 ;; Check that `c-nonlabel-token-key' doesn't match anywhere.
9366 (catch 'check-label
9367 (goto-char start)
9368 (while (progn
9369 (when (looking-at c-nonlabel-token-key)
9370 (goto-char start)
9371 (setq label-type nil)
9372 (throw 'check-label nil))
9373 (and (c-safe (c-forward-sexp)
9374 (c-forward-syntactic-ws)
9376 (not (eobp)))))
9378 ;; Record the identifiers in the label for fontification, unless
9379 ;; it begins with `c-label-kwds' in which case the following
9380 ;; identifiers are part of a (constant) expression that
9381 ;; shouldn't be fontified.
9382 (when (and c-record-type-identifiers
9383 (progn (goto-char start)
9384 (not (looking-at c-label-kwds-regexp))))
9385 (while (c-syntactic-re-search-forward c-symbol-key nil t)
9386 (c-record-ref-id (cons (match-beginning 0)
9387 (match-end 0)))))
9389 (c-put-c-type-property (1- (point-max)) 'c-decl-end)
9390 (goto-char (point-max)))))
9393 ;; Not a label.
9394 (goto-char start)))
9395 label-type))
9397 (defun c-forward-objc-directive ()
9398 ;; Assuming the point is at the beginning of a token, try to move
9399 ;; forward to the end of the Objective-C directive that starts
9400 ;; there. Return t if a directive was fully recognized, otherwise
9401 ;; the point is moved as far as one could be successfully parsed and
9402 ;; nil is returned.
9404 ;; This function records identifier ranges on
9405 ;; `c-record-type-identifiers' and `c-record-ref-identifiers' if
9406 ;; `c-record-type-identifiers' is non-nil.
9408 ;; This function might do hidden buffer changes.
9410 (let ((start (point))
9411 start-char
9412 (c-promote-possible-types t)
9414 ;; Turn off recognition of angle bracket arglists while parsing
9415 ;; types here since the protocol reference list might then be
9416 ;; considered part of the preceding name or superclass-name.
9417 c-recognize-<>-arglists)
9419 (if (or
9420 (when (looking-at
9421 (eval-when-compile
9422 (c-make-keywords-re t
9423 (append (c-lang-const c-protection-kwds objc)
9424 '("@end"))
9425 'objc-mode)))
9426 (goto-char (match-end 1))
9429 (and
9430 (looking-at
9431 (eval-when-compile
9432 (c-make-keywords-re t
9433 '("@interface" "@implementation" "@protocol")
9434 'objc-mode)))
9436 ;; Handle the name of the class itself.
9437 (progn
9438 ;; (c-forward-token-2) ; 2006/1/13 This doesn't move if the token's
9439 ;; at EOB.
9440 (goto-char (match-end 0))
9441 (setq lim (point))
9442 (c-skip-ws-forward)
9443 (c-forward-type))
9445 (catch 'break
9446 ;; Look for ": superclass-name" or "( category-name )".
9447 (when (looking-at "[:(]")
9448 (setq start-char (char-after))
9449 (forward-char)
9450 (c-forward-syntactic-ws)
9451 (unless (c-forward-type) (throw 'break nil))
9452 (when (eq start-char ?\()
9453 (unless (eq (char-after) ?\)) (throw 'break nil))
9454 (forward-char)
9455 (c-forward-syntactic-ws)))
9457 ;; Look for a protocol reference list.
9458 (if (eq (char-after) ?<)
9459 (let ((c-recognize-<>-arglists t)
9460 (c-parse-and-markup-<>-arglists t)
9461 c-restricted-<>-arglists)
9462 (c-forward-<>-arglist t))
9463 t))))
9465 (progn
9466 (c-backward-syntactic-ws lim)
9467 (c-clear-c-type-property start (1- (point)) 'c-decl-end)
9468 (c-put-c-type-property (1- (point)) 'c-decl-end)
9471 (c-clear-c-type-property start (point) 'c-decl-end)
9472 nil)))
9474 (defun c-beginning-of-inheritance-list (&optional lim)
9475 ;; Go to the first non-whitespace after the colon that starts a
9476 ;; multiple inheritance introduction. Optional LIM is the farthest
9477 ;; back we should search.
9479 ;; This function might do hidden buffer changes.
9480 (c-with-syntax-table c++-template-syntax-table
9481 (c-backward-token-2 0 t lim)
9482 (while (and (or (looking-at c-symbol-start)
9483 (looking-at "[<,]\\|::"))
9484 (zerop (c-backward-token-2 1 t lim))))))
9486 (defun c-in-method-def-p ()
9487 ;; Return nil if we aren't in a method definition, otherwise the
9488 ;; position of the initial [+-].
9490 ;; This function might do hidden buffer changes.
9491 (save-excursion
9492 (beginning-of-line)
9493 (and c-opt-method-key
9494 (looking-at c-opt-method-key)
9495 (point))
9498 ;; Contributed by Kevin Ryde <user42@zip.com.au>.
9499 (defun c-in-gcc-asm-p ()
9500 ;; Return non-nil if point is within a gcc \"asm\" block.
9502 ;; This should be called with point inside an argument list.
9504 ;; Only one level of enclosing parentheses is considered, so for
9505 ;; instance nil is returned when in a function call within an asm
9506 ;; operand.
9508 ;; This function might do hidden buffer changes.
9510 (and c-opt-asm-stmt-key
9511 (save-excursion
9512 (beginning-of-line)
9513 (backward-up-list 1)
9514 (c-beginning-of-statement-1 (point-min) nil t)
9515 (looking-at c-opt-asm-stmt-key))))
9517 (defun c-at-toplevel-p ()
9518 "Return a determination as to whether point is \"at the top level\".
9519 Informally, \"at the top level\" is anywhere where you can write
9520 a function.
9522 More precisely, being at the top-level means that point is either
9523 outside any enclosing block (such as a function definition), or
9524 directly inside a class, namespace or other block that contains
9525 another declaration level.
9527 If point is not at the top-level (e.g. it is inside a method
9528 definition), then nil is returned. Otherwise, if point is at a
9529 top-level not enclosed within a class definition, t is returned.
9530 Otherwise, a 2-vector is returned where the zeroth element is the
9531 buffer position of the start of the class declaration, and the first
9532 element is the buffer position of the enclosing class's opening
9533 brace.
9535 Note that this function might do hidden buffer changes. See the
9536 comment at the start of cc-engine.el for more info."
9537 ;; Note to maintainers: this function consumes a great mass of CPU cycles.
9538 ;; Its use should thus be minimized as far as possible.
9539 (let ((paren-state (c-parse-state)))
9540 (or (not (c-most-enclosing-brace paren-state))
9541 (c-search-uplist-for-classkey paren-state))))
9543 (defun c-just-after-func-arglist-p (&optional lim)
9544 ;; Return non-nil if the point is in the region after the argument
9545 ;; list of a function and its opening brace (or semicolon in case it
9546 ;; got no body). If there are K&R style argument declarations in
9547 ;; that region, the point has to be inside the first one for this
9548 ;; function to recognize it.
9550 ;; If successful, the point is moved to the first token after the
9551 ;; function header (see `c-forward-decl-or-cast-1' for details) and
9552 ;; the position of the opening paren of the function arglist is
9553 ;; returned.
9555 ;; The point is clobbered if not successful.
9557 ;; LIM is used as bound for backward buffer searches.
9559 ;; This function might do hidden buffer changes.
9561 (let ((beg (point)) id-start)
9562 (and
9563 (eq (c-beginning-of-statement-1 lim) 'same)
9565 (not (and (c-major-mode-is 'objc-mode)
9566 (c-forward-objc-directive)))
9568 (setq id-start
9569 (car-safe (c-forward-decl-or-cast-1 (c-point 'bosws) 'top nil)))
9570 (< id-start beg)
9572 ;; There should not be a '=' or ',' between beg and the
9573 ;; start of the declaration since that means we were in the
9574 ;; "expression part" of the declaration.
9575 (or (> (point) beg)
9576 (not (looking-at "[=,]")))
9578 (save-excursion
9579 ;; Check that there's an arglist paren in the
9580 ;; declaration.
9581 (goto-char id-start)
9582 (cond ((eq (char-after) ?\()
9583 ;; The declarator is a paren expression, so skip past it
9584 ;; so that we don't get stuck on that instead of the
9585 ;; function arglist.
9586 (c-forward-sexp))
9587 ((and c-opt-op-identifier-prefix
9588 (looking-at c-opt-op-identifier-prefix))
9589 ;; Don't trip up on "operator ()".
9590 (c-forward-token-2 2 t)))
9591 (and (< (point) beg)
9592 (c-syntactic-re-search-forward "(" beg t t)
9593 (1- (point)))))))
9595 (defun c-in-knr-argdecl (&optional lim)
9596 ;; Return the position of the first argument declaration if point is
9597 ;; inside a K&R style argument declaration list, nil otherwise.
9598 ;; `c-recognize-knr-p' is not checked. If LIM is non-nil, it's a
9599 ;; position that bounds the backward search for the argument list. This
9600 ;; function doesn't move point.
9602 ;; Point must be within a possible K&R region, e.g. just before a top-level
9603 ;; "{". It must be outside of parens and brackets. The test can return
9604 ;; false positives otherwise.
9606 ;; This function might do hidden buffer changes.
9607 (save-excursion
9608 (save-restriction
9609 ;; If we're in a macro, our search range is restricted to it. Narrow to
9610 ;; the searchable range.
9611 (let* ((macro-start (save-excursion (and (c-beginning-of-macro) (point))))
9612 (macro-end (save-excursion (and macro-start (c-end-of-macro) (point))))
9613 (low-lim (max (or lim (point-min)) (or macro-start (point-min))))
9614 before-lparen after-rparen
9615 (here (point))
9616 (pp-count-out 20) ; Max number of paren/brace constructs before
9617 ; we give up.
9618 ids ; List of identifiers in the parenthesized list.
9619 id-start after-prec-token decl-or-cast decl-res
9620 c-last-identifier-range identifier-ok)
9621 (narrow-to-region low-lim (or macro-end (point-max)))
9623 ;; Search backwards for the defun's argument list. We give up if we
9624 ;; encounter a "}" (end of a previous defun) an "=" (which can't be in
9625 ;; a knr region) or BOB.
9627 ;; The criterion for a paren structure being the arg list is:
9628 ;; o - there is non-WS stuff after it but before any "{"; AND
9629 ;; o - the token after it isn't a ";" AND
9630 ;; o - it is preceded by either an identifier (the function name) or
9631 ;; a macro expansion like "DEFUN (...)"; AND
9632 ;; o - its content is a non-empty comma-separated list of identifiers
9633 ;; (an empty arg list won't have a knr region).
9635 ;; The following snippet illustrates these rules:
9636 ;; int foo (bar, baz, yuk)
9637 ;; int bar [] ;
9638 ;; int (*baz) (my_type) ;
9639 ;; int (*(* yuk) (void)) (void) ;
9640 ;; {
9642 ;; Additionally, for a knr list to be recognized:
9643 ;; o - The identifier of each declarator up to and including the
9644 ;; one "near" point must be contained in the arg list.
9646 (catch 'knr
9647 (while (> pp-count-out 0) ; go back one paren/bracket pair each time.
9648 (setq pp-count-out (1- pp-count-out))
9649 (c-syntactic-skip-backward "^)]}=")
9650 (cond ((eq (char-before) ?\))
9651 (setq after-rparen (point)))
9652 ((eq (char-before) ?\])
9653 (setq after-rparen nil))
9654 (t ; either } (hit previous defun) or = or no more
9655 ; parens/brackets.
9656 (throw 'knr nil)))
9658 (if after-rparen
9659 ;; We're inside a paren. Could it be our argument list....?
9661 (and
9662 (progn
9663 (goto-char after-rparen)
9664 (unless (c-go-list-backward) (throw 'knr nil)) ;
9665 ;; FIXME!!! What about macros between the parens? 2007/01/20
9666 (setq before-lparen (point)))
9668 ;; It can't be the arg list if next token is ; or {
9669 (progn (goto-char after-rparen)
9670 (c-forward-syntactic-ws)
9671 (not (memq (char-after) '(?\; ?\{ ?\=))))
9673 ;; Is the thing preceding the list an identifier (the
9674 ;; function name), or a macro expansion?
9675 (progn
9676 (goto-char before-lparen)
9677 (eq (c-backward-token-2) 0)
9678 (or (eq (c-on-identifier) (point))
9679 (and (eq (char-after) ?\))
9680 (c-go-up-list-backward)
9681 (eq (c-backward-token-2) 0)
9682 (eq (c-on-identifier) (point)))))
9684 ;; Have we got a non-empty list of comma-separated
9685 ;; identifiers?
9686 (progn
9687 (goto-char before-lparen)
9688 (c-forward-token-2) ; to first token inside parens
9689 (and
9690 (setq id-start (c-on-identifier)) ; Must be at least one.
9691 (catch 'id-list
9692 (while
9693 (progn
9694 (forward-char)
9695 (c-end-of-current-token)
9696 (push (buffer-substring-no-properties id-start
9697 (point))
9698 ids)
9699 (c-forward-syntactic-ws)
9700 (eq (char-after) ?\,))
9701 (c-forward-token-2)
9702 (unless (setq id-start (c-on-identifier))
9703 (throw 'id-list nil)))
9704 (eq (char-after) ?\)))))
9706 ;; Are all the identifiers in the k&r list up to the
9707 ;; current one also in the argument list?
9708 (progn
9709 (forward-char) ; over the )
9710 (setq after-prec-token after-rparen)
9711 (c-forward-syntactic-ws)
9712 (while (and
9713 (or (consp (setq decl-or-cast
9714 (c-forward-decl-or-cast-1
9715 after-prec-token
9716 nil ; Or 'arglist ???
9717 nil)))
9718 (progn
9719 (goto-char after-prec-token)
9720 (c-forward-syntactic-ws)
9721 (setq identifier-ok (eq (char-after) ?{))
9722 nil))
9723 (eq (char-after) ?\;)
9724 (setq after-prec-token (1+ (point)))
9725 (goto-char (car decl-or-cast))
9726 (setq decl-res (c-forward-declarator))
9727 (setq identifier-ok
9728 (member (buffer-substring-no-properties
9729 (car decl-res) (cadr decl-res))
9730 ids))
9731 (progn
9732 (goto-char after-prec-token)
9733 (prog1 (< (point) here)
9734 (c-forward-syntactic-ws))))
9735 (setq identifier-ok nil))
9736 identifier-ok))
9737 ;; ...Yes. We've identified the function's argument list.
9738 (throw 'knr
9739 (progn (goto-char after-rparen)
9740 (c-forward-syntactic-ws)
9741 (point)))
9742 ;; ...No. The current parens aren't the function's arg list.
9743 (goto-char before-lparen))
9745 (or (c-go-list-backward) ; backwards over [ .... ]
9746 (throw 'knr nil)))))))))
9748 (defun c-skip-conditional ()
9749 ;; skip forward over conditional at point, including any predicate
9750 ;; statements in parentheses. No error checking is performed.
9752 ;; This function might do hidden buffer changes.
9753 (c-forward-sexp (cond
9754 ;; else if()
9755 ((looking-at (concat "\\<else"
9756 "\\([ \t\n]\\|\\\\\n\\)+"
9757 "if\\>\\([^_]\\|$\\)"))
9759 ;; do, else, try, finally
9760 ((looking-at (concat "\\<\\("
9761 "do\\|else\\|try\\|finally"
9762 "\\)\\>\\([^_]\\|$\\)"))
9764 ;; for, if, while, switch, catch, synchronized, foreach
9765 (t 2))))
9767 (defun c-after-conditional (&optional lim)
9768 ;; If looking at the token after a conditional then return the
9769 ;; position of its start, otherwise return nil.
9771 ;; This function might do hidden buffer changes.
9772 (save-excursion
9773 (and (zerop (c-backward-token-2 1 t lim))
9774 (or (looking-at c-block-stmt-1-key)
9775 (and (eq (char-after) ?\()
9776 (zerop (c-backward-token-2 1 t lim))
9777 (or (looking-at c-block-stmt-2-key)
9778 (looking-at c-block-stmt-1-2-key))))
9779 (point))))
9781 (defun c-after-special-operator-id (&optional lim)
9782 ;; If the point is after an operator identifier that isn't handled
9783 ;; like an ordinary symbol (i.e. like "operator =" in C++) then the
9784 ;; position of the start of that identifier is returned. nil is
9785 ;; returned otherwise. The point may be anywhere in the syntactic
9786 ;; whitespace after the last token of the operator identifier.
9788 ;; This function might do hidden buffer changes.
9789 (save-excursion
9790 (and c-overloadable-operators-regexp
9791 (zerop (c-backward-token-2 1 nil lim))
9792 (looking-at c-overloadable-operators-regexp)
9793 (or (not c-opt-op-identifier-prefix)
9794 (and
9795 (zerop (c-backward-token-2 1 nil lim))
9796 (looking-at c-opt-op-identifier-prefix)))
9797 (point))))
9799 (defsubst c-backward-to-block-anchor (&optional lim)
9800 ;; Assuming point is at a brace that opens a statement block of some
9801 ;; kind, move to the proper anchor point for that block. It might
9802 ;; need to be adjusted further by c-add-stmt-syntax, but the
9803 ;; position at return is suitable as start position for that
9804 ;; function.
9806 ;; This function might do hidden buffer changes.
9807 (unless (= (point) (c-point 'boi))
9808 (let ((start (c-after-conditional lim)))
9809 (if start
9810 (goto-char start)))))
9812 (defsubst c-backward-to-decl-anchor (&optional lim)
9813 ;; Assuming point is at a brace that opens the block of a top level
9814 ;; declaration of some kind, move to the proper anchor point for
9815 ;; that block.
9817 ;; This function might do hidden buffer changes.
9818 (unless (= (point) (c-point 'boi))
9819 (c-beginning-of-statement-1 lim)))
9821 (defun c-search-decl-header-end ()
9822 ;; Search forward for the end of the "header" of the current
9823 ;; declaration. That's the position where the definition body
9824 ;; starts, or the first variable initializer, or the ending
9825 ;; semicolon. I.e. search forward for the closest following
9826 ;; (syntactically relevant) '{', '=' or ';' token. Point is left
9827 ;; _after_ the first found token, or at point-max if none is found.
9829 ;; This function might do hidden buffer changes.
9831 (let ((base (point)))
9832 (if (c-major-mode-is 'c++-mode)
9834 ;; In C++ we need to take special care to handle operator
9835 ;; tokens and those pesky template brackets.
9836 (while (and
9837 (c-syntactic-re-search-forward "[;{<=]" nil 'move t t)
9839 (c-end-of-current-token base)
9840 ;; Handle operator identifiers, i.e. ignore any
9841 ;; operator token preceded by "operator".
9842 (save-excursion
9843 (and (c-safe (c-backward-sexp) t)
9844 (looking-at c-opt-op-identifier-prefix)))
9845 (and (eq (char-before) ?<)
9846 (c-with-syntax-table c++-template-syntax-table
9847 (if (c-safe (goto-char (c-up-list-forward (point))))
9849 (goto-char (point-max))
9850 nil)))))
9851 (setq base (point)))
9853 (while (and
9854 (c-syntactic-re-search-forward "[;{=]" nil 'move t t)
9855 (c-end-of-current-token base))
9856 (setq base (point))))))
9858 (defun c-beginning-of-decl-1 (&optional lim)
9859 ;; Go to the beginning of the current declaration, or the beginning
9860 ;; of the previous one if already at the start of it. Point won't
9861 ;; be moved out of any surrounding paren. Return a cons cell of the
9862 ;; form (MOVE . KNR-POS). MOVE is like the return value from
9863 ;; `c-beginning-of-statement-1'. If point skipped over some K&R
9864 ;; style argument declarations (and they are to be recognized) then
9865 ;; KNR-POS is set to the start of the first such argument
9866 ;; declaration, otherwise KNR-POS is nil. If LIM is non-nil, it's a
9867 ;; position that bounds the backward search.
9869 ;; NB: Cases where the declaration continues after the block, as in
9870 ;; "struct foo { ... } bar;", are currently recognized as two
9871 ;; declarations, e.g. "struct foo { ... }" and "bar;" in this case.
9873 ;; This function might do hidden buffer changes.
9874 (catch 'return
9875 (let* ((start (point))
9876 (last-stmt-start (point))
9877 (move (c-beginning-of-statement-1 lim nil t)))
9879 ;; `c-beginning-of-statement-1' stops at a block start, but we
9880 ;; want to continue if the block doesn't begin a top level
9881 ;; construct, i.e. if it isn't preceded by ';', '}', ':', bob,
9882 ;; or an open paren.
9883 (let ((beg (point)) tentative-move)
9884 ;; Go back one "statement" each time round the loop until we're just
9885 ;; after a ;, }, or :, or at BOB or the start of a macro or start of
9886 ;; an ObjC method. This will move over a multiple declaration whose
9887 ;; components are comma separated.
9888 (while (and
9889 ;; Must check with c-opt-method-key in ObjC mode.
9890 (not (and c-opt-method-key
9891 (looking-at c-opt-method-key)))
9892 (/= last-stmt-start (point))
9893 (progn
9894 (c-backward-syntactic-ws lim)
9895 (not (or (memq (char-before) '(?\; ?} ?: nil))
9896 (c-at-vsemi-p))))
9897 (save-excursion
9898 (backward-char)
9899 (not (looking-at "\\s(")))
9900 ;; Check that we don't move from the first thing in a
9901 ;; macro to its header.
9902 (not (eq (setq tentative-move
9903 (c-beginning-of-statement-1 lim nil t))
9904 'macro)))
9905 (setq last-stmt-start beg
9906 beg (point)
9907 move tentative-move))
9908 (goto-char beg))
9910 (when c-recognize-knr-p
9911 (let ((fallback-pos (point)) knr-argdecl-start)
9912 ;; Handle K&R argdecls. Back up after the "statement" jumped
9913 ;; over by `c-beginning-of-statement-1', unless it was the
9914 ;; function body, in which case we're sitting on the opening
9915 ;; brace now. Then test if we're in a K&R argdecl region and
9916 ;; that we started at the other side of the first argdecl in
9917 ;; it.
9918 (unless (eq (char-after) ?{)
9919 (goto-char last-stmt-start))
9920 (if (and (setq knr-argdecl-start (c-in-knr-argdecl lim))
9921 (< knr-argdecl-start start)
9922 (progn
9923 (goto-char knr-argdecl-start)
9924 (not (eq (c-beginning-of-statement-1 lim nil t) 'macro))))
9925 (throw 'return
9926 (cons (if (eq (char-after fallback-pos) ?{)
9927 'previous
9928 'same)
9929 knr-argdecl-start))
9930 (goto-char fallback-pos))))
9932 ;; `c-beginning-of-statement-1' counts each brace block as a separate
9933 ;; statement, so the result will be 'previous if we've moved over any.
9934 ;; So change our result back to 'same if necessary.
9936 ;; If they were brace list initializers we might not have moved over a
9937 ;; declaration boundary though, so change it to 'same if we've moved
9938 ;; past a '=' before '{', but not ';'. (This ought to be integrated
9939 ;; into `c-beginning-of-statement-1', so we avoid this extra pass which
9940 ;; potentially can search over a large amount of text.). Take special
9941 ;; pains not to get mislead by C++'s "operator=", and the like.
9942 (if (and (eq move 'previous)
9943 (c-with-syntax-table (if (c-major-mode-is 'c++-mode)
9944 c++-template-syntax-table
9945 (syntax-table))
9946 (save-excursion
9947 (and
9948 (progn
9949 (while ; keep going back to "[;={"s until we either find
9950 ; no more, or get to one which isn't an "operator ="
9951 (and (c-syntactic-re-search-forward "[;={]" start t t t)
9952 (eq (char-before) ?=)
9953 c-overloadable-operators-regexp
9954 c-opt-op-identifier-prefix
9955 (save-excursion
9956 (eq (c-backward-token-2) 0)
9957 (looking-at c-overloadable-operators-regexp)
9958 (eq (c-backward-token-2) 0)
9959 (looking-at c-opt-op-identifier-prefix))))
9960 (eq (char-before) ?=))
9961 (c-syntactic-re-search-forward "[;{]" start t t)
9962 (eq (char-before) ?{)
9963 (c-safe (goto-char (c-up-list-forward (point))) t)
9964 (not (c-syntactic-re-search-forward ";" start t t))))))
9965 (cons 'same nil)
9966 (cons move nil)))))
9968 (defun c-end-of-decl-1 ()
9969 ;; Assuming point is at the start of a declaration (as detected by
9970 ;; e.g. `c-beginning-of-decl-1'), go to the end of it. Unlike
9971 ;; `c-beginning-of-decl-1', this function handles the case when a
9972 ;; block is followed by identifiers in e.g. struct declarations in C
9973 ;; or C++. If a proper end was found then t is returned, otherwise
9974 ;; point is moved as far as possible within the current sexp and nil
9975 ;; is returned. This function doesn't handle macros; use
9976 ;; `c-end-of-macro' instead in those cases.
9978 ;; This function might do hidden buffer changes.
9979 (let ((start (point))
9980 (decl-syntax-table (if (c-major-mode-is 'c++-mode)
9981 c++-template-syntax-table
9982 (syntax-table))))
9983 (catch 'return
9984 (c-search-decl-header-end)
9986 (when (and c-recognize-knr-p
9987 (eq (char-before) ?\;)
9988 (c-in-knr-argdecl start))
9989 ;; Stopped at the ';' in a K&R argdecl section which is
9990 ;; detected using the same criteria as in
9991 ;; `c-beginning-of-decl-1'. Move to the following block
9992 ;; start.
9993 (c-syntactic-re-search-forward "{" nil 'move t))
9995 (when (eq (char-before) ?{)
9996 ;; Encountered a block in the declaration. Jump over it.
9997 (condition-case nil
9998 (goto-char (c-up-list-forward (point)))
9999 (error (goto-char (point-max))
10000 (throw 'return nil)))
10001 (if (or (not c-opt-block-decls-with-vars-key)
10002 (save-excursion
10003 (c-with-syntax-table decl-syntax-table
10004 (let ((lim (point)))
10005 (goto-char start)
10006 (not (and
10007 ;; Check for `c-opt-block-decls-with-vars-key'
10008 ;; before the first paren.
10009 (c-syntactic-re-search-forward
10010 (concat "[;=([{]\\|\\("
10011 c-opt-block-decls-with-vars-key
10012 "\\)")
10013 lim t t t)
10014 (match-beginning 1)
10015 (not (eq (char-before) ?_))
10016 ;; Check that the first following paren is
10017 ;; the block.
10018 (c-syntactic-re-search-forward "[;=([{]"
10019 lim t t t)
10020 (eq (char-before) ?{)))))))
10021 ;; The declaration doesn't have any of the
10022 ;; `c-opt-block-decls-with-vars' keywords in the
10023 ;; beginning, so it ends here at the end of the block.
10024 (throw 'return t)))
10026 (c-with-syntax-table decl-syntax-table
10027 (while (progn
10028 (if (eq (char-before) ?\;)
10029 (throw 'return t))
10030 (c-syntactic-re-search-forward ";" nil 'move t))))
10031 nil)))
10033 (defun c-looking-at-decl-block (_containing-sexp goto-start &optional limit)
10034 ;; Assuming the point is at an open brace, check if it starts a
10035 ;; block that contains another declaration level, i.e. that isn't a
10036 ;; statement block or a brace list, and if so return non-nil.
10038 ;; If the check is successful, the return value is the start of the
10039 ;; keyword that tells what kind of construct it is, i.e. typically
10040 ;; what `c-decl-block-key' matched. Also, if GOTO-START is set then
10041 ;; the point will be at the start of the construct, before any
10042 ;; leading specifiers, otherwise it's at the returned position.
10044 ;; The point is clobbered if the check is unsuccessful.
10046 ;; CONTAINING-SEXP is the position of the open of the surrounding
10047 ;; paren, or nil if none.
10049 ;; The optional LIMIT limits the backward search for the start of
10050 ;; the construct. It's assumed to be at a syntactically relevant
10051 ;; position.
10053 ;; If any template arglists are found in the searched region before
10054 ;; the open brace, they get marked with paren syntax.
10056 ;; This function might do hidden buffer changes.
10058 (let ((open-brace (point)) kwd-start first-specifier-pos)
10059 (c-syntactic-skip-backward c-block-prefix-charset limit t)
10061 (when (and c-recognize-<>-arglists
10062 (eq (char-before) ?>))
10063 ;; Could be at the end of a template arglist.
10064 (let ((c-parse-and-markup-<>-arglists t))
10065 (while (and
10066 (c-backward-<>-arglist nil limit)
10067 (progn
10068 (c-syntactic-skip-backward c-block-prefix-charset limit t)
10069 (eq (char-before) ?>))))))
10071 ;; Skip back over noise clauses.
10072 (while (and
10073 c-opt-cpp-prefix
10074 (eq (char-before) ?\))
10075 (let ((after-paren (point)))
10076 (if (and (c-go-list-backward)
10077 (progn (c-backward-syntactic-ws)
10078 (c-simple-skip-symbol-backward))
10079 (or (looking-at c-paren-nontype-key)
10080 (looking-at c-noise-macro-with-parens-name-re)))
10081 (progn
10082 (c-syntactic-skip-backward c-block-prefix-charset limit t)
10084 (goto-char after-paren)
10085 nil))))
10087 ;; Note: Can't get bogus hits inside template arglists below since they
10088 ;; have gotten paren syntax above.
10089 (when (and
10090 ;; If `goto-start' is set we begin by searching for the
10091 ;; first possible position of a leading specifier list.
10092 ;; The `c-decl-block-key' search continues from there since
10093 ;; we know it can't match earlier.
10094 (if goto-start
10095 (when (c-syntactic-re-search-forward c-symbol-start
10096 open-brace t t)
10097 (goto-char (setq first-specifier-pos (match-beginning 0)))
10101 (cond
10102 ((c-syntactic-re-search-forward c-decl-block-key open-brace t t t)
10103 (goto-char (setq kwd-start (match-beginning 0)))
10104 (and
10105 ;; Exclude cases where we matched what would ordinarily
10106 ;; be a block declaration keyword, except where it's not
10107 ;; legal because it's part of a "compound keyword" like
10108 ;; "enum class". Of course, if c-after-brace-list-key
10109 ;; is nil, we can skip the test.
10110 (or (equal c-after-brace-list-key "\\<\\>")
10111 (save-match-data
10112 (save-excursion
10113 (not
10114 (and
10115 (looking-at c-after-brace-list-key)
10116 (= (c-backward-token-2 1 t) 0)
10117 (looking-at c-brace-list-key))))))
10119 ;; Found a keyword that can't be a type?
10120 (match-beginning 1)
10122 ;; Can be a type too, in which case it's the return type of a
10123 ;; function (under the assumption that no declaration level
10124 ;; block construct starts with a type).
10125 (not (c-forward-type))
10127 ;; Jumped over a type, but it could be a declaration keyword
10128 ;; followed by the declared identifier that we've jumped over
10129 ;; instead (e.g. in "class Foo {"). If it indeed is a type
10130 ;; then we should be at the declarator now, so check for a
10131 ;; valid declarator start.
10133 ;; Note: This doesn't cope with the case when a declared
10134 ;; identifier is followed by e.g. '(' in a language where '('
10135 ;; also might be part of a declarator expression. Currently
10136 ;; there's no such language.
10137 (not (or (looking-at c-symbol-start)
10138 (looking-at c-type-decl-prefix-key))))))
10140 ;; In Pike a list of modifiers may be followed by a brace
10141 ;; to make them apply to many identifiers. Note that the
10142 ;; match data will be empty on return in this case.
10143 ((and (c-major-mode-is 'pike-mode)
10144 (progn
10145 (goto-char open-brace)
10146 (= (c-backward-token-2) 0))
10147 (looking-at c-specifier-key)
10148 ;; Use this variant to avoid yet another special regexp.
10149 (c-keyword-member (c-keyword-sym (match-string 1))
10150 'c-modifier-kwds))
10151 (setq kwd-start (point))
10152 t)))
10154 ;; Got a match.
10156 (if goto-start
10157 ;; Back up over any preceding specifiers and their clauses
10158 ;; by going forward from `first-specifier-pos', which is the
10159 ;; earliest possible position where the specifier list can
10160 ;; start.
10161 (progn
10162 (goto-char first-specifier-pos)
10164 (while (< (point) kwd-start)
10165 (if (looking-at c-symbol-key)
10166 ;; Accept any plain symbol token on the ground that
10167 ;; it's a specifier masked through a macro (just
10168 ;; like `c-forward-decl-or-cast-1' skip forward over
10169 ;; such tokens).
10171 ;; Could be more restrictive wrt invalid keywords,
10172 ;; but that'd only occur in invalid code so there's
10173 ;; no use spending effort on it.
10174 (let ((end (match-end 0)))
10175 (unless (c-forward-keyword-clause 0)
10176 (goto-char end)
10177 (c-forward-syntactic-ws)))
10179 ;; Can't parse a declaration preamble and is still
10180 ;; before `kwd-start'. That means `first-specifier-pos'
10181 ;; was in some earlier construct. Search again.
10182 (if (c-syntactic-re-search-forward c-symbol-start
10183 kwd-start 'move t)
10184 (goto-char (setq first-specifier-pos (match-beginning 0)))
10185 ;; Got no preamble before the block declaration keyword.
10186 (setq first-specifier-pos kwd-start))))
10188 (goto-char first-specifier-pos))
10189 (goto-char kwd-start))
10191 kwd-start)))
10193 (defun c-directly-in-class-called-p (name)
10194 ;; Check whether point is directly inside a brace block which is the brace
10195 ;; block of a class, struct, or union which is called NAME, a string.
10196 (let* ((paren-state (c-parse-state))
10197 (brace-pos (c-pull-open-brace paren-state))
10199 (when (eq (char-after brace-pos) ?{)
10200 (goto-char brace-pos)
10201 (save-excursion
10202 ; *c-looking-at-decl-block
10203 ; containing-sexp goto-start &optional
10204 ; limit)
10205 (when (and (c-looking-at-decl-block
10206 (c-pull-open-brace paren-state)
10207 nil)
10208 (looking-at c-class-key))
10209 (goto-char (match-end 1))
10210 (c-forward-syntactic-ws)
10211 (looking-at name))))))
10213 (defun c-search-uplist-for-classkey (paren-state)
10214 ;; Check if the closest containing paren sexp is a declaration
10215 ;; block, returning a 2 element vector in that case. Aref 0
10216 ;; contains the bufpos at boi of the class key line, and aref 1
10217 ;; contains the bufpos of the open brace. This function is an
10218 ;; obsolete wrapper for `c-looking-at-decl-block'.
10220 ;; This function might do hidden buffer changes.
10221 (let ((open-paren-pos (c-most-enclosing-brace paren-state)))
10222 (when open-paren-pos
10223 (save-excursion
10224 (goto-char open-paren-pos)
10225 (when (and (eq (char-after) ?{)
10226 (c-looking-at-decl-block
10227 (c-safe-position open-paren-pos paren-state)
10228 nil))
10229 (back-to-indentation)
10230 (vector (point) open-paren-pos))))))
10232 (defun c-most-enclosing-decl-block (paren-state)
10233 ;; Return the buffer position of the most enclosing decl-block brace (in the
10234 ;; sense of c-looking-at-decl-block) in the PAREN-STATE structure, or nil if
10235 ;; none was found.
10236 (let* ((open-brace (c-pull-open-brace paren-state))
10237 (next-open-brace (c-pull-open-brace paren-state)))
10238 (while (and open-brace
10239 (save-excursion
10240 (goto-char open-brace)
10241 (not (c-looking-at-decl-block next-open-brace nil))))
10242 (setq open-brace next-open-brace
10243 next-open-brace (c-pull-open-brace paren-state)))
10244 open-brace))
10246 (defun c-cheap-inside-bracelist-p (paren-state)
10247 ;; Return the position of the L-brace if point is inside a brace list
10248 ;; initialization of an array, etc. This is an approximate function,
10249 ;; designed for speed over accuracy. It will not find every bracelist, but
10250 ;; a non-nil result is reliable. We simply search for "= {" (naturally with
10251 ;; syntactic whitespace allowed). PAREN-STATE is the normal thing that it
10252 ;; is everywhere else.
10253 (let (b-pos)
10254 (save-excursion
10255 (while
10256 (and (setq b-pos (c-pull-open-brace paren-state))
10257 (progn (goto-char b-pos)
10258 (c-backward-sws)
10259 (c-backward-token-2)
10260 (not (looking-at "=")))))
10261 b-pos)))
10263 (defun c-backward-typed-enum-colon ()
10264 ;; We're at a "{" which might be the opening brace of a enum which is
10265 ;; strongly typed (by a ":" followed by a type). If this is the case, leave
10266 ;; point before the colon and return t. Otherwise leave point unchanged and return nil.
10267 ;; Match data will be clobbered.
10268 (let ((here (point))
10269 (colon-pos nil))
10270 (save-excursion
10271 (while
10272 (and (eql (c-backward-token-2) 0)
10273 (or (not (looking-at "\\s)"))
10274 (c-go-up-list-backward))
10275 (cond
10276 ((looking-at "::")
10278 ((and (eql (char-after) ?:)
10279 (save-excursion
10280 (c-backward-syntactic-ws)
10281 (or (c-on-identifier)
10282 (progn
10283 (c-backward-token-2)
10284 (looking-at c-brace-list-key)))))
10285 (setq colon-pos (point))
10286 (forward-char)
10287 (c-forward-syntactic-ws)
10288 (or (and (c-forward-type)
10289 (progn (c-forward-syntactic-ws)
10290 (eq (point) here)))
10291 (setq colon-pos nil))
10292 nil)
10293 ((eql (char-after) ?\()
10295 ((looking-at c-symbol-key)
10297 (t nil)))))
10298 (when colon-pos
10299 (goto-char colon-pos)
10300 t)))
10302 (defun c-backward-over-enum-header ()
10303 ;; We're at a "{". Move back to the enum-like keyword that starts this
10304 ;; declaration and return t, otherwise don't move and return nil.
10305 (let ((here (point))
10306 before-identifier)
10307 (when c-recognize-post-brace-list-type-p
10308 (c-backward-typed-enum-colon))
10309 (while
10310 (and
10311 (eq (c-backward-token-2) 0)
10312 (or (not (looking-at "\\s)"))
10313 (c-go-up-list-backward))
10314 (cond
10315 ((and (looking-at c-symbol-key) (c-on-identifier)
10316 (not before-identifier))
10317 (setq before-identifier t))
10318 ((and before-identifier
10319 (or (eql (char-after) ?,)
10320 (looking-at c-postfix-decl-spec-key)))
10321 (setq before-identifier nil)
10323 ((looking-at c-after-brace-list-key) t)
10324 ((looking-at c-brace-list-key) nil)
10325 ((eq (char-after) ?\()
10326 (and (eq (c-backward-token-2) 0)
10327 (or (looking-at c-decl-hangon-key)
10328 (and c-opt-cpp-prefix
10329 (looking-at c-noise-macro-with-parens-name-re)))))
10331 ((and c-recognize-<>-arglists
10332 (eq (char-after) ?<)
10333 (looking-at "\\s("))
10335 (t nil))))
10336 (or (looking-at c-brace-list-key)
10337 (progn (goto-char here) nil))))
10339 (defun c-looking-at-or-maybe-in-bracelist (&optional containing-sexp lim)
10340 ;; Point is at an open brace. If this starts a brace list, return a list
10341 ;; whose car is the buffer position of the start of the construct which
10342 ;; introduces the list, and whose cdr is t if we have parsed a keyword
10343 ;; matching `c-opt-inexpr-brace-list-key' (e.g. Java's "new"), nil
10344 ;; otherwise. Otherwise, if point might be inside an enclosing brace list,
10345 ;; return t. If point is definitely neither at nor in a brace list, return
10346 ;; nil.
10348 ;; CONTAINING-SEXP is the position of the brace/paren/bracket enclosing
10349 ;; POINT, or nil if there is no such position, or we do not know it. LIM is
10350 ;; a backward search limit.
10352 ;; Here, "brace list" does not include the body of an enum.
10353 (save-excursion
10354 (let ((start (point))
10355 (class-key
10356 ;; Pike can have class definitions anywhere, so we must
10357 ;; check for the class key here.
10358 (and (c-major-mode-is 'pike-mode)
10359 c-decl-block-key))
10360 (braceassignp 'dontknow)
10361 inexpr-brace-list bufpos macro-start res pos after-type-id-pos)
10363 (setq res (c-backward-token-2 1 t lim))
10364 ;; Checks to do only on the first sexp before the brace.
10365 ;; Have we a C++ initialization, without an "="?
10366 (if (and (c-major-mode-is 'c++-mode)
10367 (cond
10368 ((and (not (eq res 0))
10369 (c-go-up-list-backward nil lim) ; FIXME!!! Check ; `lim' 2016-07-12.
10370 (eq (char-after) ?\())
10371 (setq braceassignp 'c++-noassign))
10372 ((looking-at c-pre-id-bracelist-key))
10373 ((looking-at c-return-key))
10374 ((and (looking-at c-symbol-start)
10375 (not (looking-at c-keywords-regexp)))
10376 (setq after-type-id-pos (point)))
10377 (t nil))
10378 (save-excursion
10379 (cond
10380 ((not (eq res 0))
10381 (and (c-go-up-list-backward nil lim) ; FIXME!!! Check `lim' 2016-07-12.
10382 (eq (char-after) ?\()))
10383 ((looking-at c-pre-id-bracelist-key))
10384 ((looking-at c-return-key))
10385 (t (setq after-type-id-pos (point))
10386 nil))))
10387 (setq braceassignp 'c++-noassign))
10389 (when (and c-opt-inexpr-brace-list-key
10390 (eq (char-after) ?\[))
10391 ;; In Java, an initialization brace list may follow
10392 ;; directly after "new Foo[]", so check for a "new"
10393 ;; earlier.
10394 (while (eq braceassignp 'dontknow)
10395 (setq braceassignp
10396 (cond ((/= (c-backward-token-2 1 t lim) 0) nil)
10397 ((looking-at c-opt-inexpr-brace-list-key)
10398 (setq inexpr-brace-list t)
10400 ((looking-at "\\sw\\|\\s_\\|[.[]")
10401 ;; Carry on looking if this is an
10402 ;; identifier (may contain "." in Java)
10403 ;; or another "[]" sexp.
10404 'dontknow)
10405 (t nil)))))
10407 (setq pos (point))
10408 (cond
10409 ((and after-type-id-pos
10410 (goto-char after-type-id-pos)
10411 (setq res (c-back-over-member-initializers))
10412 (goto-char res)
10413 (eq (car (c-beginning-of-decl-1 lim)) 'same))
10414 (cons (point) nil)) ; Return value.
10416 ((and after-type-id-pos
10417 (progn
10418 (c-backward-syntactic-ws)
10419 (eq (char-before) ?\()))
10420 ;; Single identifier between '(' and '{'. We have a bracelist.
10421 (cons after-type-id-pos nil))
10424 (goto-char pos)
10425 ;; Checks to do on all sexps before the brace, up to the
10426 ;; beginning of the statement.
10427 (while (eq braceassignp 'dontknow)
10428 (cond ((eq (char-after) ?\;)
10429 (setq braceassignp nil))
10430 ((and class-key
10431 (looking-at class-key))
10432 (setq braceassignp nil))
10433 ((eq (char-after) ?=)
10434 ;; We've seen a =, but must check earlier tokens so
10435 ;; that it isn't something that should be ignored.
10436 (setq braceassignp 'maybe)
10437 (while (and (eq braceassignp 'maybe)
10438 (zerop (c-backward-token-2 1 t lim)))
10439 (setq braceassignp
10440 (cond
10441 ;; Check for operator =
10442 ((and c-opt-op-identifier-prefix
10443 (looking-at c-opt-op-identifier-prefix))
10444 nil)
10445 ;; Check for `<opchar>= in Pike.
10446 ((and (c-major-mode-is 'pike-mode)
10447 (or (eq (char-after) ?`)
10448 ;; Special case for Pikes
10449 ;; `[]=, since '[' is not in
10450 ;; the punctuation class.
10451 (and (eq (char-after) ?\[)
10452 (eq (char-before) ?`))))
10453 nil)
10454 ((looking-at "\\s.") 'maybe)
10455 ;; make sure we're not in a C++ template
10456 ;; argument assignment
10457 ((and
10458 (c-major-mode-is 'c++-mode)
10459 (save-excursion
10460 (let ((here (point))
10461 (pos< (progn
10462 (skip-chars-backward "^<>")
10463 (point))))
10464 (and (eq (char-before) ?<)
10465 (not (c-crosses-statement-barrier-p
10466 pos< here))
10467 (not (c-in-literal))
10468 ))))
10469 nil)
10470 (t t))))))
10471 (if (and (eq braceassignp 'dontknow)
10472 (/= (c-backward-token-2 1 t lim) 0))
10473 (setq braceassignp nil)))
10475 (cond
10476 (braceassignp
10477 ;; We've hit the beginning of the aggregate list.
10478 (c-beginning-of-statement-1 containing-sexp)
10479 (cons (point) inexpr-brace-list))
10480 ((and after-type-id-pos
10481 (save-excursion
10482 (when (eq (char-after) ?\;)
10483 (c-forward-token-2 1 t))
10484 (setq bufpos (point))
10485 (when (looking-at c-opt-<>-sexp-key)
10486 (c-forward-token-2)
10487 (when (and (eq (char-after) ?<)
10488 (c-get-char-property (point) 'syntax-table))
10489 (c-go-list-forward nil after-type-id-pos)
10490 (c-forward-syntactic-ws)))
10491 (and
10492 (or (not (looking-at c-class-key))
10493 (save-excursion
10494 (goto-char (match-end 1))
10495 (c-forward-syntactic-ws)
10496 (not (eq (point) after-type-id-pos))))
10497 (progn
10498 (setq res
10499 (c-forward-decl-or-cast-1
10500 (save-excursion (c-backward-syntactic-ws) (point))
10501 nil nil))
10502 (and (consp res)
10503 (eq (car res) after-type-id-pos))))))
10504 (cons bufpos inexpr-brace-list))
10505 ((eq (char-after) ?\;)
10506 ;; Brace lists can't contain a semicolon, so we're done.
10507 ;; (setq containing-sexp nil)
10508 nil)
10509 ((and (setq macro-start (point))
10510 (c-forward-to-cpp-define-body)
10511 (eq (point) start))
10512 ;; We've a macro whose expansion starts with the '{'.
10513 ;; Heuristically, if we have a ';' in it we've not got a
10514 ;; brace list, otherwise we have.
10515 (let ((macro-end (progn (c-end-of-macro) (point))))
10516 (goto-char start)
10517 (forward-char)
10518 (if (and (c-syntactic-re-search-forward "[;,]" macro-end t t)
10519 (eq (char-before) ?\;))
10521 (cons macro-start nil)))) ; (2016-08-30): Lazy! We have no
10522 ; languages where
10523 ; `c-opt-inexpr-brace-list-key' is
10524 ; non-nil and we have macros.
10525 (t t)))) ;; The caller can go up one level.
10528 (defun c-inside-bracelist-p (containing-sexp paren-state)
10529 ;; return the buffer position of the beginning of the brace list
10530 ;; statement if we're inside a brace list, otherwise return nil.
10531 ;; CONTAINING-SEXP is the buffer pos of the innermost containing
10532 ;; paren. PAREN-STATE is the remainder of the state of enclosing
10533 ;; braces
10535 ;; N.B.: This algorithm can potentially get confused by cpp macros
10536 ;; placed in inconvenient locations. It's a trade-off we make for
10537 ;; speed.
10539 ;; This function might do hidden buffer changes.
10541 ;; This will pick up brace list declarations.
10542 (save-excursion
10543 (goto-char containing-sexp)
10544 (c-backward-over-enum-header))
10545 ;; this will pick up array/aggregate init lists, even if they are nested.
10546 (save-excursion
10547 (let ((bufpos t)
10548 lim next-containing)
10549 (while (and (eq bufpos t)
10550 containing-sexp)
10551 (when paren-state
10552 (if (consp (car paren-state))
10553 (setq lim (cdr (car paren-state))
10554 paren-state (cdr paren-state))
10555 (setq lim (car paren-state)))
10556 (when paren-state
10557 (setq next-containing (car paren-state)
10558 paren-state (cdr paren-state))))
10560 (goto-char containing-sexp)
10561 (if (c-looking-at-inexpr-block next-containing next-containing)
10562 ;; We're in an in-expression block of some kind. Do not
10563 ;; check nesting. We deliberately set the limit to the
10564 ;; containing sexp, so that c-looking-at-inexpr-block
10565 ;; doesn't check for an identifier before it.
10566 (setq bufpos nil)
10567 (when (or (not (eq (char-after) ?{))
10568 (eq (setq bufpos (c-looking-at-or-maybe-in-bracelist
10569 next-containing lim))
10571 (setq containing-sexp next-containing
10572 lim nil
10573 next-containing nil))))
10574 (and (consp bufpos) (car bufpos))))))
10576 (defun c-looking-at-special-brace-list (&optional _lim)
10577 ;; If we're looking at the start of a pike-style list, i.e., `({ })',
10578 ;; `([ ])', `(< >)', etc., a cons of a cons of its starting and ending
10579 ;; positions and its entry in c-special-brace-lists is returned, nil
10580 ;; otherwise. The ending position is nil if the list is still open.
10581 ;; LIM is the limit for forward search. The point may either be at
10582 ;; the `(' or at the following paren character. Tries to check the
10583 ;; matching closer, but assumes it's correct if no balanced paren is
10584 ;; found (i.e. the case `({ ... } ... )' is detected as _not_ being
10585 ;; a special brace list).
10587 ;; This function might do hidden buffer changes.
10588 (if c-special-brace-lists
10589 (condition-case ()
10590 (save-excursion
10591 (let ((beg (point))
10592 inner-beg end type)
10593 (c-forward-syntactic-ws)
10594 (if (eq (char-after) ?\()
10595 (progn
10596 (forward-char 1)
10597 (c-forward-syntactic-ws)
10598 (setq inner-beg (point))
10599 (setq type (assq (char-after) c-special-brace-lists)))
10600 (if (setq type (assq (char-after) c-special-brace-lists))
10601 (progn
10602 (setq inner-beg (point))
10603 (c-backward-syntactic-ws)
10604 (forward-char -1)
10605 (setq beg (if (eq (char-after) ?\()
10606 (point)
10607 nil)))))
10608 (if (and beg type)
10609 (if (and (c-safe
10610 (goto-char beg)
10611 (c-forward-sexp 1)
10612 (setq end (point))
10613 (= (char-before) ?\)))
10614 (c-safe
10615 (goto-char inner-beg)
10616 (if (looking-at "\\s(")
10617 ;; Check balancing of the inner paren
10618 ;; below.
10619 (progn
10620 (c-forward-sexp 1)
10622 ;; If the inner char isn't a paren then
10623 ;; we can't check balancing, so just
10624 ;; check the char before the outer
10625 ;; closing paren.
10626 (goto-char end)
10627 (backward-char)
10628 (c-backward-syntactic-ws)
10629 (= (char-before) (cdr type)))))
10630 (if (or (/= (char-syntax (char-before)) ?\))
10631 (= (progn
10632 (c-forward-syntactic-ws)
10633 (point))
10634 (1- end)))
10635 (cons (cons beg end) type))
10636 (cons (list beg) type)))))
10637 (error nil))))
10639 (defun c-looking-at-bos (&optional _lim)
10640 ;; Return non-nil if between two statements or declarations, assuming
10641 ;; point is not inside a literal or comment.
10643 ;; Obsolete - `c-at-statement-start-p' or `c-at-expression-start-p'
10644 ;; are recommended instead.
10646 ;; This function might do hidden buffer changes.
10647 (c-at-statement-start-p))
10648 (make-obsolete 'c-looking-at-bos 'c-at-statement-start-p "22.1")
10650 (defun c-looking-at-statement-block ()
10651 ;; Point is at an opening brace. If this is a statement block (i.e. the
10652 ;; elements in it are terminated by semicolons) return t. Otherwise, return
10653 ;; nil.
10654 (let ((here (point)))
10655 (prog1
10656 (if (c-go-list-forward)
10657 (let ((there (point)))
10658 (backward-char)
10659 (c-syntactic-skip-backward
10660 "^;," here t)
10661 (cond
10662 ((eq (char-before) ?\;) t)
10663 ((eq (char-before) ?,) nil)
10664 (t (goto-char here)
10665 (forward-char)
10666 (and (c-syntactic-re-search-forward "{" there t t)
10667 (progn (backward-char)
10668 (c-looking-at-statement-block))))))
10669 (forward-char)
10670 (and (c-syntactic-re-search-forward "[;,]" nil t t)
10671 (eq (char-before) ?\;)))
10672 (goto-char here))))
10674 (defun c-looking-at-inexpr-block (lim containing-sexp &optional check-at-end)
10675 ;; Return non-nil if we're looking at the beginning of a block
10676 ;; inside an expression. The value returned is actually a cons of
10677 ;; either 'inlambda, 'inexpr-statement or 'inexpr-class and the
10678 ;; position of the beginning of the construct.
10680 ;; LIM limits the backward search. CONTAINING-SEXP is the start
10681 ;; position of the closest containing list. If it's nil, the
10682 ;; containing paren isn't used to decide whether we're inside an
10683 ;; expression or not. If both LIM and CONTAINING-SEXP are used, LIM
10684 ;; needs to be farther back.
10686 ;; If CHECK-AT-END is non-nil then extra checks at the end of the
10687 ;; brace block might be done. It should only be used when the
10688 ;; construct can be assumed to be complete, i.e. when the original
10689 ;; starting position was further down than that.
10691 ;; This function might do hidden buffer changes.
10693 (save-excursion
10694 (let ((res 'maybe) (passed-bracket-pairs 0) bracket-pos passed-paren
10695 haskell-op-pos
10696 (closest-lim (or containing-sexp lim (point-min)))
10697 ;; Look at the character after point only as a last resort
10698 ;; when we can't disambiguate.
10699 (block-follows (and (eq (char-after) ?{) (point))))
10701 ;; Search for a C++11 "->" which suggests a lambda declaration.
10702 (when (and (c-major-mode-is 'c++-mode)
10703 (setq haskell-op-pos
10704 (save-excursion
10705 (while
10706 (progn
10707 (c-syntactic-skip-backward "^;=}>" closest-lim t)
10708 (and (eq (char-before) ?>)
10709 (c-backward-token-2)
10710 (not (looking-at c-haskell-op-re)))))
10711 (and (looking-at c-haskell-op-re)
10712 (point)))))
10713 (goto-char haskell-op-pos))
10715 (while (and (eq res 'maybe)
10716 (progn (c-backward-syntactic-ws)
10717 (> (point) closest-lim))
10718 (not (bobp))
10719 (progn (backward-char)
10720 (looking-at "[]).]\\|\\w\\|\\s_"))
10721 (c-safe (forward-char)
10722 (goto-char (scan-sexps (point) -1))))
10724 (setq res
10725 (if (looking-at c-keywords-regexp)
10726 (let ((kw-sym (c-keyword-sym (match-string 1))))
10727 (cond
10728 ((and block-follows
10729 (c-keyword-member kw-sym 'c-inexpr-class-kwds))
10730 (and (not (eq passed-paren ?\[))
10731 (or (not (looking-at c-class-key))
10732 ;; If the class definition is at the start of
10733 ;; a statement, we don't consider it an
10734 ;; in-expression class.
10735 (let ((prev (point)))
10736 (while (and
10737 (= (c-backward-token-2 1 nil closest-lim) 0)
10738 (eq (char-syntax (char-after)) ?w))
10739 (setq prev (point)))
10740 (goto-char prev)
10741 (not (c-at-statement-start-p)))
10742 ;; Also, in Pike we treat it as an
10743 ;; in-expression class if it's used in an
10744 ;; object clone expression.
10745 (save-excursion
10746 (and check-at-end
10747 (c-major-mode-is 'pike-mode)
10748 (progn (goto-char block-follows)
10749 (zerop (c-forward-token-2 1 t)))
10750 (eq (char-after) ?\())))
10751 (cons 'inexpr-class (point))))
10752 ((c-keyword-member kw-sym 'c-paren-any-kwds) ; e.g. C++11 "throw" or "noexcept"
10753 (setq passed-paren nil)
10754 (setq passed-bracket-pairs 0)
10755 (setq bracket-pos nil)
10756 'maybe)
10757 ((c-keyword-member kw-sym 'c-inexpr-block-kwds)
10758 (when (not passed-paren)
10759 (cons 'inexpr-statement (point))))
10760 ((c-keyword-member kw-sym 'c-lambda-kwds)
10761 (when (or (not passed-paren)
10762 (eq passed-paren ?\())
10763 (cons 'inlambda (point))))
10764 ((c-keyword-member kw-sym 'c-block-stmt-kwds)
10765 nil)
10767 'maybe)))
10769 (if (looking-at "\\s(")
10770 (if passed-paren
10771 (cond
10772 ((and (eq passed-paren ?\[)
10773 (eq (char-after) ?\[)
10774 (not (eq (char-after (1+ (point))) ?\[))) ; C++ attribute.
10775 ;; Accept several square bracket sexps for
10776 ;; Java array initializations.
10777 (setq passed-bracket-pairs (1+ passed-bracket-pairs))
10778 'maybe)
10779 ((and (eq passed-paren ?\()
10780 (eq (char-after) ?\[)
10781 (not (eq (char-after (1+ (point))) ?\[))
10782 (eq passed-bracket-pairs 0))
10783 ;; C++11 lambda function declaration
10784 (setq passed-bracket-pairs 1)
10785 (setq bracket-pos (point))
10786 'maybe)
10787 (t nil))
10788 (when (not (looking-at "\\[\\["))
10789 (setq passed-paren (char-after))
10790 (when (eq passed-paren ?\[)
10791 (setq passed-bracket-pairs 1)
10792 (setq bracket-pos (point))))
10793 'maybe)
10794 'maybe))))
10796 (if (eq res 'maybe)
10797 (cond
10798 ((and (c-major-mode-is 'c++-mode)
10799 block-follows
10800 (eq passed-bracket-pairs 1)
10801 (save-excursion
10802 (goto-char bracket-pos)
10803 (or (<= (point) (or lim (point-min)))
10804 (progn
10805 (c-backward-token-2 1 nil lim)
10806 (and
10807 (not (and (c-on-identifier)
10808 (looking-at c-symbol-chars)))
10809 (not (looking-at c-opt-op-identifier-prefix)))))))
10810 (cons 'inlambda bracket-pos))
10811 ((and c-recognize-paren-inexpr-blocks
10812 block-follows
10813 containing-sexp
10814 (eq (char-after containing-sexp) ?\())
10815 (goto-char containing-sexp)
10816 (if (or (save-excursion
10817 (c-backward-syntactic-ws lim)
10818 (while (and (eq (char-before) ?>)
10819 (c-get-char-property (1- (point))
10820 'syntax-table)
10821 (c-go-list-backward nil lim))
10822 (c-backward-syntactic-ws lim))
10823 (and (> (point) (or lim (point-min)))
10824 (c-on-identifier)))
10825 (and c-special-brace-lists
10826 (c-looking-at-special-brace-list))
10827 (and (c-major-mode-is 'c++-mode)
10828 (save-excursion
10829 (goto-char block-follows)
10830 (not (c-looking-at-statement-block)))))
10832 (cons 'inexpr-statement (point)))))
10834 res))))
10836 (defun c-looking-at-inexpr-block-backward (paren-state)
10837 ;; Returns non-nil if we're looking at the end of an in-expression
10838 ;; block, otherwise the same as `c-looking-at-inexpr-block'.
10839 ;; PAREN-STATE is the paren state relevant at the current position.
10841 ;; This function might do hidden buffer changes.
10842 (save-excursion
10843 ;; We currently only recognize a block.
10844 (let ((here (point))
10845 (elem (car-safe paren-state))
10846 containing-sexp)
10847 (when (and (consp elem)
10848 (progn (goto-char (cdr elem))
10849 (c-forward-syntactic-ws here)
10850 (= (point) here)))
10851 (goto-char (car elem))
10852 (if (setq paren-state (cdr paren-state))
10853 (setq containing-sexp (car-safe paren-state)))
10854 (c-looking-at-inexpr-block (c-safe-position containing-sexp
10855 paren-state)
10856 containing-sexp)))))
10858 (defun c-looking-at-c++-lambda-capture-list ()
10859 ;; Return non-nil if we're at the opening "[" of the capture list of a C++
10860 ;; lambda function, nil otherwise.
10861 (and
10862 (eq (char-after) ?\[)
10863 (not (eq (char-before) ?\[))
10864 (not (eq (char-after (1+ (point))) ?\[))
10865 (save-excursion
10866 (or (eq (c-backward-token-2 1) 1)
10867 (looking-at c-pre-lambda-tokens-re)))
10868 (not (c-in-literal))))
10870 (defun c-at-macro-vsemi-p (&optional pos)
10871 ;; Is there a "virtual semicolon" at POS or point?
10872 ;; (See cc-defs.el for full details of "virtual semicolons".)
10874 ;; This is true when point is at the last non syntactic WS position on the
10875 ;; line, there is a macro call last on the line, and this particular macro's
10876 ;; name is defined by the regexp `c-vs-macro-regexp' as not needing a
10877 ;; semicolon.
10878 (save-excursion
10879 (save-restriction
10880 (widen)
10881 (if pos
10882 (goto-char pos)
10883 (setq pos (point)))
10884 (and
10885 c-macro-with-semi-re
10886 (eq (skip-chars-backward " \t") 0)
10888 ;; Check we've got nothing after this except comments and empty lines
10889 ;; joined by escaped EOLs.
10890 (skip-chars-forward " \t") ; always returns non-nil.
10891 (progn
10892 (while ; go over 1 block comment per iteration.
10893 (and
10894 (looking-at "\\(\\\\[\n\r][ \t]*\\)*")
10895 (goto-char (match-end 0))
10896 (cond
10897 ((looking-at c-block-comment-start-regexp)
10898 (and (forward-comment 1)
10899 (skip-chars-forward " \t"))) ; always returns non-nil
10900 ((looking-at c-line-comment-start-regexp)
10901 (end-of-line)
10902 nil)
10903 (t nil))))
10904 (eolp))
10906 (goto-char pos)
10907 (progn (c-backward-syntactic-ws)
10908 (eq (point) pos))
10910 ;; Check for one of the listed macros being before point.
10911 (or (not (eq (char-before) ?\)))
10912 (when (c-go-list-backward)
10913 (c-backward-syntactic-ws)
10915 (c-simple-skip-symbol-backward)
10916 (looking-at c-macro-with-semi-re)
10917 (goto-char pos)
10918 (not (c-in-literal)))))) ; The most expensive check last.
10920 (defun c-macro-vsemi-status-unknown-p () t) ; See cc-defs.el.
10923 ;; `c-guess-basic-syntax' and the functions that precedes it below
10924 ;; implements the main decision tree for determining the syntactic
10925 ;; analysis of the current line of code.
10927 ;; Dynamically bound to t when `c-guess-basic-syntax' is called during
10928 ;; auto newline analysis.
10929 (defvar c-auto-newline-analysis nil)
10931 (defun c-brace-anchor-point (bracepos)
10932 ;; BRACEPOS is the position of a brace in a construct like "namespace
10933 ;; Bar {". Return the anchor point in this construct; this is the
10934 ;; earliest symbol on the brace's line which isn't earlier than
10935 ;; "namespace".
10937 ;; Currently (2007-08-17), "like namespace" means "matches
10938 ;; c-other-block-decl-kwds". It doesn't work with "class" or "struct"
10939 ;; or anything like that.
10940 (save-excursion
10941 (let ((boi (c-point 'boi bracepos)))
10942 (goto-char bracepos)
10943 (while (and (> (point) boi)
10944 (not (looking-at c-other-decl-block-key)))
10945 (c-backward-token-2))
10946 (if (> (point) boi) (point) boi))))
10948 (defsubst c-add-syntax (symbol &rest args)
10949 ;; A simple function to prepend a new syntax element to
10950 ;; `c-syntactic-context'. Using `setq' on it is unsafe since it
10951 ;; should always be dynamically bound but since we read it first
10952 ;; we'll fail properly anyway if this function is misused.
10953 (setq c-syntactic-context (cons (cons symbol args)
10954 c-syntactic-context)))
10956 (defsubst c-append-syntax (symbol &rest args)
10957 ;; Like `c-add-syntax' but appends to the end of the syntax list.
10958 ;; (Normally not necessary.)
10959 (setq c-syntactic-context (nconc c-syntactic-context
10960 (list (cons symbol args)))))
10962 (defun c-add-stmt-syntax (syntax-symbol
10963 syntax-extra-args
10964 stop-at-boi-only
10965 containing-sexp
10966 paren-state
10967 &optional fixed-anchor)
10968 ;; Add the indicated SYNTAX-SYMBOL to `c-syntactic-context', extending it as
10969 ;; needed with further syntax elements of the types `substatement',
10970 ;; `inexpr-statement', `arglist-cont-nonempty', `statement-block-intro',
10971 ;; `defun-block-intro', and `brace-list-intro'.
10973 ;; Do the generic processing to anchor the given syntax symbol on the
10974 ;; preceding statement: First skip over any labels and containing statements
10975 ;; on the same line. If FIXED-ANCHOR is non-nil, use this as the
10976 ;; anchor-point for the given syntactic symbol, and don't make syntactic
10977 ;; entries for constructs beginning on lines before that containing
10978 ;; ANCHOR-POINT. Otherwise search backward until we find a statement or
10979 ;; block start that begins at boi without a label or comment.
10981 ;; Point is assumed to be at the prospective anchor point for the
10982 ;; given SYNTAX-SYMBOL. More syntax entries are added if we need to
10983 ;; skip past open parens and containing statements. Most of the added
10984 ;; syntax elements will get the same anchor point - the exception is
10985 ;; for an anchor in a construct like "namespace"[*] - this is as early
10986 ;; as possible in the construct but on the same line as the {.
10988 ;; [*] i.e. with a keyword matching c-other-block-decl-kwds.
10990 ;; SYNTAX-EXTRA-ARGS are a list of the extra arguments for the
10991 ;; syntax symbol. They are appended after the anchor point.
10993 ;; If STOP-AT-BOI-ONLY is nil, we can stop in the middle of the line
10994 ;; if the current statement starts there.
10996 ;; Note: It's not a problem if PAREN-STATE "overshoots"
10997 ;; CONTAINING-SEXP, i.e. contains info about parens further down.
10999 ;; This function might do hidden buffer changes.
11001 (if (= (point) (c-point 'boi))
11002 ;; This is by far the most common case, so let's give it special
11003 ;; treatment.
11004 (apply 'c-add-syntax syntax-symbol (point) syntax-extra-args)
11006 (let ((syntax-last c-syntactic-context)
11007 (boi (c-point 'boi))
11008 (anchor-boi (c-point 'boi))
11009 ;; Set when we're on a label, so that we don't stop there.
11010 ;; FIXME: To be complete we should check if we're on a label
11011 ;; now at the start.
11012 on-label)
11014 ;; Use point as the anchor point for "namespace", "extern", etc.
11015 (apply 'c-add-syntax syntax-symbol
11016 (if (rassq syntax-symbol c-other-decl-block-key-in-symbols-alist)
11017 (point) nil)
11018 syntax-extra-args)
11020 ;; Loop while we have to back out of containing blocks.
11021 (while
11022 (and
11023 (catch 'back-up-block
11025 ;; Loop while we have to back up statements.
11026 (while (or (/= (point) boi)
11027 on-label
11028 (looking-at c-comment-start-regexp))
11030 ;; Skip past any comments that stands between the
11031 ;; statement start and boi.
11032 (let ((savepos (point)))
11033 (while (and (/= savepos boi)
11034 (c-backward-single-comment))
11035 (setq savepos (point)
11036 boi (c-point 'boi)))
11037 (goto-char savepos))
11039 ;; Skip to the beginning of this statement or backward
11040 ;; another one.
11041 (let ((old-pos (point))
11042 (old-boi boi)
11043 (step-type (c-beginning-of-statement-1 containing-sexp)))
11044 (setq boi (c-point 'boi)
11045 on-label (eq step-type 'label))
11047 (cond ((= (point) old-pos)
11048 ;; If we didn't move we're at the start of a block and
11049 ;; have to continue outside it.
11050 (throw 'back-up-block t))
11052 ((and (eq step-type 'up)
11053 (>= (point) old-boi)
11054 (looking-at "else\\>[^_]")
11055 (save-excursion
11056 (goto-char old-pos)
11057 (looking-at "if\\>[^_]")))
11058 ;; Special case to avoid deeper and deeper indentation
11059 ;; of "else if" clauses.
11062 ((and (not stop-at-boi-only)
11063 (/= old-pos old-boi)
11064 (memq step-type '(up previous)))
11065 ;; If stop-at-boi-only is nil, we shouldn't back up
11066 ;; over previous or containing statements to try to
11067 ;; reach boi, so go back to the last position and
11068 ;; exit.
11069 (goto-char old-pos)
11070 (throw 'back-up-block nil))
11073 (if (and (not stop-at-boi-only)
11074 (memq step-type '(up previous beginning)))
11075 ;; If we've moved into another statement then we
11076 ;; should no longer try to stop in the middle of a
11077 ;; line.
11078 (setq stop-at-boi-only t))
11080 ;; Record this as a substatement if we skipped up one
11081 ;; level.
11082 (when (eq step-type 'up)
11083 (c-add-syntax 'substatement nil))))
11086 containing-sexp
11087 (or (null fixed-anchor)
11088 (> containing-sexp anchor-boi)))
11090 ;; Now we have to go out of this block.
11091 (goto-char containing-sexp)
11093 ;; Don't stop in the middle of a special brace list opener
11094 ;; like "({".
11095 (when c-special-brace-lists
11096 (let ((special-list (c-looking-at-special-brace-list)))
11097 (when (and special-list
11098 (< (car (car special-list)) (point)))
11099 (setq containing-sexp (car (car special-list)))
11100 (goto-char containing-sexp))))
11102 (setq paren-state (c-whack-state-after containing-sexp paren-state)
11103 containing-sexp (c-most-enclosing-brace paren-state)
11104 boi (c-point 'boi))
11106 ;; Analyze the construct in front of the block we've stepped out
11107 ;; from and add the right syntactic element for it.
11108 (let ((paren-pos (point))
11109 (paren-char (char-after))
11110 step-type)
11112 (if (eq paren-char ?\()
11113 ;; Stepped out of a parenthesis block, so we're in an
11114 ;; expression now.
11115 (progn
11116 (when (/= paren-pos boi)
11117 (if (and c-recognize-paren-inexpr-blocks
11118 (progn
11119 (c-backward-syntactic-ws containing-sexp)
11120 (or (not (looking-at "\\>"))
11121 (not (c-on-identifier))))
11122 (save-excursion
11123 (goto-char (1+ paren-pos))
11124 (c-forward-syntactic-ws)
11125 (eq (char-after) ?{)))
11126 ;; Stepped out of an in-expression statement. This
11127 ;; syntactic element won't get an anchor pos.
11128 (c-add-syntax 'inexpr-statement)
11130 ;; A parenthesis normally belongs to an arglist.
11131 (c-add-syntax 'arglist-cont-nonempty nil paren-pos)))
11133 (goto-char (max boi
11134 (if containing-sexp
11135 (1+ containing-sexp)
11136 (point-min))))
11137 (setq step-type 'same
11138 on-label nil))
11140 ;; Stepped out of a brace block.
11141 (setq step-type (c-beginning-of-statement-1 containing-sexp)
11142 on-label (eq step-type 'label))
11144 (if (and (eq step-type 'same)
11145 (/= paren-pos (point)))
11146 (let (inexpr)
11147 (cond
11148 ((save-excursion
11149 (goto-char paren-pos)
11150 (setq inexpr (c-looking-at-inexpr-block
11151 (c-safe-position containing-sexp paren-state)
11152 containing-sexp)))
11153 (c-add-syntax (if (eq (car inexpr) 'inlambda)
11154 'defun-block-intro
11155 'statement-block-intro)
11156 nil))
11157 ((looking-at c-other-decl-block-key)
11158 (c-add-syntax
11159 (cdr (assoc (match-string 1)
11160 c-other-decl-block-key-in-symbols-alist))
11161 (max (c-point 'boi paren-pos) (point))))
11162 ((save-excursion
11163 (goto-char paren-pos)
11164 (c-looking-at-or-maybe-in-bracelist containing-sexp))
11165 (if (save-excursion
11166 (goto-char paren-pos)
11167 (c-looking-at-statement-block))
11168 (c-add-syntax 'defun-block-intro nil)
11169 (c-add-syntax 'brace-list-intro nil)))
11170 (t (c-add-syntax 'defun-block-intro nil))))
11172 (c-add-syntax 'statement-block-intro nil)))
11174 (if (= paren-pos boi)
11175 ;; Always done if the open brace was at boi. The
11176 ;; c-beginning-of-statement-1 call above is necessary
11177 ;; anyway, to decide the type of block-intro to add.
11178 (goto-char paren-pos)
11179 (setq boi (c-point 'boi)))
11182 ;; Fill in the current point as the anchor for all the symbols
11183 ;; added above.
11184 (let ((p c-syntactic-context) q)
11185 (while (not (eq p syntax-last))
11186 (setq q (cdr (car p))) ; e.g. (nil 28) [from (arglist-cont-nonempty nil 28)]
11187 (while q
11188 (unless (car q)
11189 (setcar q (if (or (cdr p)
11190 (null fixed-anchor))
11191 (point)
11192 fixed-anchor)))
11193 (setq q (cdr q)))
11194 (setq p (cdr p))))
11197 (defun c-add-class-syntax (symbol
11198 containing-decl-open
11199 containing-decl-start
11200 containing-decl-kwd
11201 _paren-state)
11202 ;; The inclass and class-close syntactic symbols are added in
11203 ;; several places and some work is needed to fix everything.
11204 ;; Therefore it's collected here.
11206 ;; This function might do hidden buffer changes.
11207 (goto-char containing-decl-open)
11208 (if (and (eq symbol 'inclass) (= (point) (c-point 'boi)))
11209 (progn
11210 (c-add-syntax symbol containing-decl-open)
11211 containing-decl-open)
11212 (goto-char containing-decl-start)
11213 ;; Ought to use `c-add-stmt-syntax' instead of backing up to boi
11214 ;; here, but we have to do like this for compatibility.
11215 (back-to-indentation)
11216 (c-add-syntax symbol (point))
11217 (if (and (c-keyword-member containing-decl-kwd
11218 'c-inexpr-class-kwds)
11219 (/= containing-decl-start (c-point 'boi containing-decl-start)))
11220 (c-add-syntax 'inexpr-class))
11221 (point)))
11223 (defun c-guess-continued-construct (indent-point
11224 char-after-ip
11225 beg-of-same-or-containing-stmt
11226 containing-sexp
11227 paren-state)
11228 ;; This function contains the decision tree reached through both
11229 ;; cases 18 and 10. It's a continued statement or top level
11230 ;; construct of some kind.
11232 ;; This function might do hidden buffer changes.
11234 (let (special-brace-list placeholder)
11235 (goto-char indent-point)
11236 (skip-chars-forward " \t")
11238 (cond
11239 ;; (CASE A removed.)
11240 ;; CASE B: open braces for class or brace-lists
11241 ((setq special-brace-list
11242 (or (and c-special-brace-lists
11243 (c-looking-at-special-brace-list))
11244 (eq char-after-ip ?{)))
11246 (cond
11247 ;; CASE B.1: class-open
11248 ((save-excursion
11249 (and (eq (char-after) ?{)
11250 (c-looking-at-decl-block containing-sexp t)
11251 (setq beg-of-same-or-containing-stmt (point))))
11252 (c-add-syntax 'class-open beg-of-same-or-containing-stmt))
11254 ;; CASE B.2: brace-list-open
11255 ((or (consp special-brace-list)
11256 (consp
11257 (c-looking-at-or-maybe-in-bracelist
11258 containing-sexp beg-of-same-or-containing-stmt))
11260 ;; The most semantically accurate symbol here is
11261 ;; brace-list-open, but we normally report it simply as a
11262 ;; statement-cont. The reason is that one normally adjusts
11263 ;; brace-list-open for brace lists as top-level constructs,
11264 ;; and brace lists inside statements is a completely different
11265 ;; context. C.f. case 5A.3.
11266 (c-beginning-of-statement-1 containing-sexp)
11267 (c-add-stmt-syntax (if c-auto-newline-analysis
11268 ;; Turn off the dwim above when we're
11269 ;; analyzing the nature of the brace
11270 ;; for the auto newline feature.
11271 'brace-list-open
11272 'statement-cont)
11273 nil nil
11274 containing-sexp paren-state))
11276 ;; CASE B.3: The body of a function declared inside a normal
11277 ;; block. Can occur e.g. in Pike and when using gcc
11278 ;; extensions, but watch out for macros followed by blocks.
11279 ;; C.f. cases E, 16F and 17G.
11280 ((and (not (c-at-statement-start-p))
11281 (eq (c-beginning-of-statement-1 containing-sexp nil nil t)
11282 'same)
11283 (save-excursion
11284 (let ((c-recognize-typeless-decls nil))
11285 ;; Turn off recognition of constructs that lacks a
11286 ;; type in this case, since that's more likely to be
11287 ;; a macro followed by a block.
11288 (c-forward-decl-or-cast-1 (c-point 'bosws) nil nil))))
11289 (c-add-stmt-syntax 'defun-open nil t
11290 containing-sexp paren-state))
11292 ;; CASE B.5: We have a C++11 "return \n { ..... }" Note that we're
11293 ;; not at the "{", currently.
11294 ((progn (goto-char indent-point)
11295 (backward-sexp)
11296 (looking-at c-return-key))
11297 (c-add-stmt-syntax 'statement-cont nil t
11298 containing-sexp paren-state))
11300 ;; CASE B.4: Continued statement with block open. The most
11301 ;; accurate analysis is perhaps `statement-cont' together with
11302 ;; `block-open' but we play DWIM and use `substatement-open'
11303 ;; instead. The rationale is that this typically is a macro
11304 ;; followed by a block which makes it very similar to a
11305 ;; statement with a substatement block.
11307 (c-add-stmt-syntax 'substatement-open nil nil
11308 containing-sexp paren-state))
11311 ;; CASE C: iostream insertion or extraction operator
11312 ((and (looking-at "\\(<<\\|>>\\)\\([^=]\\|$\\)")
11313 (save-excursion
11314 (goto-char beg-of-same-or-containing-stmt)
11315 ;; If there is no preceding streamop in the statement
11316 ;; then indent this line as a normal statement-cont.
11317 (when (c-syntactic-re-search-forward
11318 "\\(<<\\|>>\\)\\([^=]\\|$\\)" indent-point 'move t t)
11319 (c-add-syntax 'stream-op (c-point 'boi))
11320 t))))
11322 ;; CASE E: In the "K&R region" of a function declared inside a
11323 ;; normal block. C.f. case B.3.
11324 ((and (save-excursion
11325 ;; Check that the next token is a '{'. This works as
11326 ;; long as no language that allows nested function
11327 ;; definitions allows stuff like member init lists, K&R
11328 ;; declarations or throws clauses there.
11330 ;; Note that we do a forward search for something ahead
11331 ;; of the indentation line here. That's not good since
11332 ;; the user might not have typed it yet. Unfortunately
11333 ;; it's exceedingly tricky to recognize a function
11334 ;; prototype in a code block without resorting to this.
11335 (c-forward-syntactic-ws)
11336 (eq (char-after) ?{))
11337 (not (c-at-statement-start-p))
11338 (eq (c-beginning-of-statement-1 containing-sexp nil nil t)
11339 'same)
11340 (save-excursion
11341 (let ((c-recognize-typeless-decls nil))
11342 ;; Turn off recognition of constructs that lacks a
11343 ;; type in this case, since that's more likely to be
11344 ;; a macro followed by a block.
11345 (c-forward-decl-or-cast-1 (c-point 'bosws) nil nil))))
11346 (c-add-stmt-syntax 'func-decl-cont nil t
11347 containing-sexp paren-state))
11349 ;;CASE F: continued statement and the only preceding items are
11350 ;;annotations.
11351 ((and (c-major-mode-is 'java-mode)
11352 (setq placeholder (point))
11353 (c-beginning-of-statement-1)
11354 (progn
11355 (while (and (c-forward-annotation)
11356 (< (point) placeholder))
11357 (c-forward-syntactic-ws))
11359 (prog1
11360 (>= (point) placeholder)
11361 (goto-char placeholder)))
11362 (c-beginning-of-statement-1 containing-sexp)
11363 (c-add-syntax 'annotation-var-cont (point)))
11365 ;; CASE G: a template list continuation?
11366 ;; Mostly a duplication of case 5D.3 to fix templates-19:
11367 ((and (c-major-mode-is 'c++-mode)
11368 (save-excursion
11369 (goto-char indent-point)
11370 (c-with-syntax-table c++-template-syntax-table
11371 (setq placeholder (c-up-list-backward)))
11372 (and placeholder
11373 (eq (char-after placeholder) ?<)
11374 (/= (char-before placeholder) ?<)
11375 (progn
11376 (goto-char (1+ placeholder))
11377 (not (looking-at c-<-op-cont-regexp))))))
11378 (c-with-syntax-table c++-template-syntax-table
11379 (goto-char placeholder)
11380 (c-beginning-of-statement-1 containing-sexp t))
11381 (if (save-excursion
11382 (c-backward-syntactic-ws containing-sexp)
11383 (eq (char-before) ?<))
11384 ;; In a nested template arglist.
11385 (progn
11386 (goto-char placeholder)
11387 (c-syntactic-skip-backward "^,;" containing-sexp t)
11388 (c-forward-syntactic-ws))
11389 (back-to-indentation))
11390 ;; FIXME: Should use c-add-stmt-syntax, but it's not yet
11391 ;; template aware.
11392 (c-add-syntax 'template-args-cont (point) placeholder))
11394 ;; CASE D: continued statement.
11396 (c-beginning-of-statement-1 containing-sexp)
11397 (c-add-stmt-syntax 'statement-cont nil nil
11398 containing-sexp paren-state))
11401 ;; The next autoload was added by RMS on 2005/8/9 - don't know why (ACM,
11402 ;; 2005/11/29).
11403 ;;;###autoload
11404 (defun c-guess-basic-syntax ()
11405 "Return the syntactic context of the current line."
11406 (save-excursion
11407 (beginning-of-line)
11408 (c-save-buffer-state
11409 ((indent-point (point))
11410 (case-fold-search nil)
11411 ;; A whole ugly bunch of various temporary variables. Have
11412 ;; to declare them here since it's not possible to declare
11413 ;; a variable with only the scope of a cond test and the
11414 ;; following result clauses, and most of this function is a
11415 ;; single gigantic cond. :P
11416 literal char-before-ip before-ws-ip char-after-ip macro-start
11417 in-macro-expr c-syntactic-context placeholder
11418 step-type tmpsymbol keyword injava-inher special-brace-list tmp-pos
11419 containing-<
11420 ;; The following record some positions for the containing
11421 ;; declaration block if we're directly within one:
11422 ;; `containing-decl-open' is the position of the open
11423 ;; brace. `containing-decl-start' is the start of the
11424 ;; declaration. `containing-decl-kwd' is the keyword
11425 ;; symbol of the keyword that tells what kind of block it
11426 ;; is.
11427 containing-decl-open
11428 containing-decl-start
11429 containing-decl-kwd
11430 ;; The open paren of the closest surrounding sexp or nil if
11431 ;; there is none.
11432 containing-sexp
11433 ;; The position after the closest preceding brace sexp
11434 ;; (nested sexps are ignored), or the position after
11435 ;; `containing-sexp' if there is none, or (point-min) if
11436 ;; `containing-sexp' is nil.
11438 ;; The paren state outside `containing-sexp', or at
11439 ;; `indent-point' if `containing-sexp' is nil.
11440 (paren-state (c-parse-state))
11441 ;; There's always at most one syntactic element which got
11442 ;; an anchor pos. It's stored in syntactic-relpos.
11443 syntactic-relpos
11444 (c-stmt-delim-chars c-stmt-delim-chars))
11446 ;; Check if we're directly inside an enclosing declaration
11447 ;; level block.
11448 (when (and (setq containing-sexp
11449 (c-most-enclosing-brace paren-state))
11450 (progn
11451 (goto-char containing-sexp)
11452 (eq (char-after) ?{))
11453 (setq placeholder
11454 (c-looking-at-decl-block
11455 (c-most-enclosing-brace paren-state
11456 containing-sexp)
11457 t)))
11458 (setq containing-decl-open containing-sexp
11459 containing-decl-start (point)
11460 containing-sexp nil)
11461 (goto-char placeholder)
11462 (setq containing-decl-kwd (and (looking-at c-keywords-regexp)
11463 (c-keyword-sym (match-string 1)))))
11465 ;; Init some position variables.
11466 (if paren-state
11467 (progn
11468 (setq containing-sexp (car paren-state)
11469 paren-state (cdr paren-state))
11470 (if (consp containing-sexp)
11471 (save-excursion
11472 (goto-char (cdr containing-sexp))
11473 (if (and (c-major-mode-is 'c++-mode)
11474 (c-back-over-member-initializer-braces))
11475 (c-syntactic-skip-backward "^}" nil t))
11476 (setq lim (point))
11477 (if paren-state
11478 ;; Ignore balanced paren. The next entry
11479 ;; can't be another one.
11480 (setq containing-sexp (car paren-state)
11481 paren-state (cdr paren-state))
11482 ;; If there is no surrounding open paren then
11483 ;; put the last balanced pair back on paren-state.
11484 (setq paren-state (cons containing-sexp paren-state)
11485 containing-sexp nil)))
11486 (setq lim (1+ containing-sexp))))
11487 (setq lim (point-min)))
11489 ;; If we're in a parenthesis list then ',' delimits the
11490 ;; "statements" rather than being an operator (with the
11491 ;; exception of the "for" clause). This difference is
11492 ;; typically only noticeable when statements are used in macro
11493 ;; arglists.
11494 (when (and containing-sexp
11495 (eq (char-after containing-sexp) ?\())
11496 (setq c-stmt-delim-chars c-stmt-delim-chars-with-comma))
11497 ;; cache char before and after indent point, and move point to
11498 ;; the most likely position to perform the majority of tests
11499 (goto-char indent-point)
11500 (c-backward-syntactic-ws lim)
11501 (setq before-ws-ip (point)
11502 char-before-ip (char-before))
11503 (goto-char indent-point)
11504 (skip-chars-forward " \t")
11505 (setq char-after-ip (char-after))
11507 ;; are we in a literal?
11508 (setq literal (c-in-literal lim))
11510 ;; now figure out syntactic qualities of the current line
11511 (cond
11513 ;; CASE 1: in a string.
11514 ((eq literal 'string)
11515 (c-add-syntax 'string (c-point 'bopl)))
11517 ;; CASE 2: in a C or C++ style comment.
11518 ((and (memq literal '(c c++))
11519 ;; This is a kludge for XEmacs where we use
11520 ;; `buffer-syntactic-context', which doesn't correctly
11521 ;; recognize "\*/" to end a block comment.
11522 ;; `parse-partial-sexp' which is used by
11523 ;; `c-literal-limits' will however do that in most
11524 ;; versions, which results in that we get nil from
11525 ;; `c-literal-limits' even when `c-in-literal' claims
11526 ;; we're inside a comment.
11527 (setq placeholder (c-literal-start lim)))
11528 (c-add-syntax literal placeholder))
11530 ;; CASE 3: in a cpp preprocessor macro continuation.
11531 ((and (save-excursion
11532 (when (c-beginning-of-macro)
11533 (setq macro-start (point))))
11534 (/= macro-start (c-point 'boi))
11535 (progn
11536 (setq tmpsymbol 'cpp-macro-cont)
11537 (or (not c-syntactic-indentation-in-macros)
11538 (save-excursion
11539 (goto-char macro-start)
11540 ;; If at the beginning of the body of a #define
11541 ;; directive then analyze as cpp-define-intro
11542 ;; only. Go on with the syntactic analysis
11543 ;; otherwise. in-macro-expr is set if we're in a
11544 ;; cpp expression, i.e. before the #define body
11545 ;; or anywhere in a non-#define directive.
11546 (if (c-forward-to-cpp-define-body)
11547 (let ((indent-boi (c-point 'boi indent-point)))
11548 (setq in-macro-expr (> (point) indent-boi)
11549 tmpsymbol 'cpp-define-intro)
11550 (= (point) indent-boi))
11551 (setq in-macro-expr t)
11552 nil)))))
11553 (c-add-syntax tmpsymbol macro-start)
11554 (setq macro-start nil))
11556 ;; CASE 11: an else clause?
11557 ((looking-at "else\\>[^_]")
11558 (c-beginning-of-statement-1 containing-sexp)
11559 (c-add-stmt-syntax 'else-clause nil t
11560 containing-sexp paren-state))
11562 ;; CASE 12: while closure of a do/while construct?
11563 ((and (looking-at "while\\>[^_]")
11564 (save-excursion
11565 (prog1 (eq (c-beginning-of-statement-1 containing-sexp)
11566 'beginning)
11567 (setq placeholder (point)))))
11568 (goto-char placeholder)
11569 (c-add-stmt-syntax 'do-while-closure nil t
11570 containing-sexp paren-state))
11572 ;; CASE 13: A catch or finally clause? This case is simpler
11573 ;; than if-else and do-while, because a block is required
11574 ;; after every try, catch and finally.
11575 ((save-excursion
11576 (and (cond ((c-major-mode-is 'c++-mode)
11577 (looking-at "catch\\>[^_]"))
11578 ((c-major-mode-is 'java-mode)
11579 (looking-at "\\(catch\\|finally\\)\\>[^_]")))
11580 (and (c-safe (c-backward-syntactic-ws)
11581 (c-backward-sexp)
11583 (eq (char-after) ?{)
11584 (c-safe (c-backward-syntactic-ws)
11585 (c-backward-sexp)
11587 (if (eq (char-after) ?\()
11588 (c-safe (c-backward-sexp) t)
11590 (looking-at "\\(try\\|catch\\)\\>[^_]")
11591 (setq placeholder (point))))
11592 (goto-char placeholder)
11593 (c-add-stmt-syntax 'catch-clause nil t
11594 containing-sexp paren-state))
11596 ;; CASE 18: A substatement we can recognize by keyword.
11597 ((save-excursion
11598 (and c-opt-block-stmt-key
11599 (not (eq char-before-ip ?\;))
11600 (not (c-at-vsemi-p before-ws-ip))
11601 (not (memq char-after-ip '(?\) ?\] ?,)))
11602 (or (not (eq char-before-ip ?}))
11603 (c-looking-at-inexpr-block-backward c-state-cache))
11604 (> (point)
11605 (progn
11606 ;; Ought to cache the result from the
11607 ;; c-beginning-of-statement-1 calls here.
11608 (setq placeholder (point))
11609 (while (eq (setq step-type
11610 (c-beginning-of-statement-1 lim))
11611 'label))
11612 (if (eq step-type 'previous)
11613 (goto-char placeholder)
11614 (setq placeholder (point))
11615 (if (and (eq step-type 'same)
11616 (not (looking-at c-opt-block-stmt-key)))
11617 ;; Step up to the containing statement if we
11618 ;; stayed in the same one.
11619 (let (step)
11620 (while (eq
11621 (setq step
11622 (c-beginning-of-statement-1 lim))
11623 'label))
11624 (if (eq step 'up)
11625 (setq placeholder (point))
11626 ;; There was no containing statement after all.
11627 (goto-char placeholder)))))
11628 placeholder))
11629 (if (looking-at c-block-stmt-2-key)
11630 ;; Require a parenthesis after these keywords.
11631 ;; Necessary to catch e.g. synchronized in Java,
11632 ;; which can be used both as statement and
11633 ;; modifier.
11634 (and (zerop (c-forward-token-2 1 nil))
11635 (eq (char-after) ?\())
11636 (looking-at c-opt-block-stmt-key))))
11638 (if (eq step-type 'up)
11639 ;; CASE 18A: Simple substatement.
11640 (progn
11641 (goto-char placeholder)
11642 (cond
11643 ((eq char-after-ip ?{)
11644 (c-add-stmt-syntax 'substatement-open nil nil
11645 containing-sexp paren-state))
11646 ((save-excursion
11647 (goto-char indent-point)
11648 (back-to-indentation)
11649 (c-forward-label))
11650 (c-add-stmt-syntax 'substatement-label nil nil
11651 containing-sexp paren-state))
11653 (c-add-stmt-syntax 'substatement nil nil
11654 containing-sexp paren-state))))
11656 ;; CASE 18B: Some other substatement. This is shared
11657 ;; with case 10.
11658 (c-guess-continued-construct indent-point
11659 char-after-ip
11660 placeholder
11662 paren-state)))
11664 ;; CASE 14: A case or default label
11665 ((save-excursion
11666 (and (looking-at c-label-kwds-regexp)
11667 (or (c-major-mode-is 'idl-mode)
11668 (and
11669 containing-sexp
11670 (goto-char containing-sexp)
11671 (eq (char-after) ?{)
11672 (progn (c-backward-syntactic-ws) t)
11673 (eq (char-before) ?\))
11674 (c-go-list-backward)
11675 (progn (c-backward-syntactic-ws) t)
11676 (c-simple-skip-symbol-backward)
11677 (looking-at c-block-stmt-2-key)))))
11678 (if containing-sexp
11679 (progn
11680 (goto-char containing-sexp)
11681 (setq lim (c-most-enclosing-brace c-state-cache
11682 containing-sexp))
11683 (c-backward-to-block-anchor lim)
11684 (c-add-stmt-syntax 'case-label nil t lim paren-state))
11685 ;; Got a bogus label at the top level. In lack of better
11686 ;; alternatives, anchor it on (point-min).
11687 (c-add-syntax 'case-label (point-min))))
11689 ;; CASE 15: any other label
11690 ((save-excursion
11691 (back-to-indentation)
11692 (and (not (looking-at c-syntactic-ws-start))
11693 (not (looking-at c-label-kwds-regexp))
11694 (c-forward-label)))
11695 (cond (containing-decl-open
11696 (setq placeholder (c-add-class-syntax 'inclass
11697 containing-decl-open
11698 containing-decl-start
11699 containing-decl-kwd
11700 paren-state))
11701 ;; Append access-label with the same anchor point as
11702 ;; inclass gets.
11703 (c-append-syntax 'access-label placeholder))
11705 (containing-sexp
11706 (goto-char containing-sexp)
11707 (setq lim (c-most-enclosing-brace c-state-cache
11708 containing-sexp))
11709 (save-excursion
11710 (setq tmpsymbol
11711 (if (and (eq (c-beginning-of-statement-1 lim) 'up)
11712 (looking-at "switch\\>[^_]"))
11713 ;; If the surrounding statement is a switch then
11714 ;; let's analyze all labels as switch labels, so
11715 ;; that they get lined up consistently.
11716 'case-label
11717 'label)))
11718 (c-backward-to-block-anchor lim)
11719 (c-add-stmt-syntax tmpsymbol nil t lim paren-state))
11722 ;; A label on the top level. Treat it as a class
11723 ;; context. (point-min) is the closest we get to the
11724 ;; class open brace.
11725 (c-add-syntax 'access-label (point-min)))))
11727 ;; CASE 4: In-expression statement. C.f. cases 7B, 16A and
11728 ;; 17E.
11729 ((setq placeholder (c-looking-at-inexpr-block
11730 (c-safe-position containing-sexp paren-state)
11731 containing-sexp
11732 ;; Have to turn on the heuristics after
11733 ;; the point even though it doesn't work
11734 ;; very well. C.f. test case class-16.pike.
11736 (setq tmpsymbol (assq (car placeholder)
11737 '((inexpr-class . class-open)
11738 (inexpr-statement . block-open))))
11739 (if tmpsymbol
11740 ;; It's a statement block or an anonymous class.
11741 (setq tmpsymbol (cdr tmpsymbol))
11742 ;; It's a Pike lambda. Check whether we are between the
11743 ;; lambda keyword and the argument list or at the defun
11744 ;; opener.
11745 (setq tmpsymbol (if (eq char-after-ip ?{)
11746 'inline-open
11747 'lambda-intro-cont)))
11748 (goto-char (cdr placeholder))
11749 (back-to-indentation)
11750 (c-add-stmt-syntax tmpsymbol nil t
11751 (c-most-enclosing-brace c-state-cache (point))
11752 paren-state)
11753 (unless (eq (point) (cdr placeholder))
11754 (c-add-syntax (car placeholder))))
11756 ;; CASE 5: Line is inside a declaration level block or at top level.
11757 ((or containing-decl-open (null containing-sexp))
11758 (cond
11760 ;; CASE 5A: we are looking at a defun, brace list, class,
11761 ;; or inline-inclass method opening brace
11762 ((setq special-brace-list
11763 (or (and c-special-brace-lists
11764 (c-looking-at-special-brace-list))
11765 (eq char-after-ip ?{)))
11766 (cond
11768 ;; CASE 5A.1: Non-class declaration block open.
11769 ((save-excursion
11770 (let (tmp)
11771 (and (eq char-after-ip ?{)
11772 (setq tmp (c-looking-at-decl-block containing-sexp t))
11773 (progn
11774 (setq placeholder (point))
11775 (goto-char tmp)
11776 (looking-at c-symbol-key))
11777 (c-keyword-member
11778 (c-keyword-sym (setq keyword (match-string 0)))
11779 'c-other-block-decl-kwds))))
11780 (goto-char placeholder)
11781 (c-add-stmt-syntax
11782 (if (string-equal keyword "extern")
11783 ;; Special case for extern-lang-open.
11784 'extern-lang-open
11785 (intern (concat keyword "-open")))
11786 nil t containing-sexp paren-state))
11788 ;; CASE 5A.2: we are looking at a class opening brace
11789 ((save-excursion
11790 (goto-char indent-point)
11791 (skip-chars-forward " \t")
11792 (and (eq (char-after) ?{)
11793 (c-looking-at-decl-block containing-sexp t)
11794 (setq placeholder (point))))
11795 (c-add-syntax 'class-open placeholder))
11797 ;; CASE 5A.3: brace list open
11798 ((save-excursion
11799 (goto-char indent-point)
11800 (skip-chars-forward " \t")
11801 (cond
11802 ((c-backward-over-enum-header)
11803 (setq placeholder (c-point 'boi)))
11804 ((consp (setq placeholder
11805 (c-looking-at-or-maybe-in-bracelist
11806 containing-sexp lim)))
11807 (setq tmpsymbol (and (cdr placeholder) 'topmost-intro-cont))
11808 (setq placeholder (c-point 'boi (car placeholder))))))
11809 (if (and (not c-auto-newline-analysis)
11810 ;(c-major-mode-is 'java-mode) ; Not needed anymore (2016-08-30).
11811 (eq tmpsymbol 'topmost-intro-cont))
11812 ;; We're in Java and have found that the open brace
11813 ;; belongs to a "new Foo[]" initialization list,
11814 ;; which means the brace list is part of an
11815 ;; expression and not a top level definition. We
11816 ;; therefore treat it as any topmost continuation
11817 ;; even though the semantically correct symbol still
11818 ;; is brace-list-open, on the same grounds as in
11819 ;; case B.2.
11820 (progn
11821 (c-beginning-of-statement-1 lim)
11822 (c-add-syntax 'topmost-intro-cont (c-point 'boi)))
11823 (c-add-syntax 'brace-list-open placeholder)))
11825 ;; CASE 5A.4: inline defun open
11826 ((and containing-decl-open
11827 (not (c-keyword-member containing-decl-kwd
11828 'c-other-block-decl-kwds)))
11829 (c-add-syntax 'inline-open)
11830 (c-add-class-syntax 'inclass
11831 containing-decl-open
11832 containing-decl-start
11833 containing-decl-kwd
11834 paren-state))
11836 ;; CASE 5A.5: ordinary defun open
11838 (save-excursion
11839 (c-beginning-of-decl-1 lim)
11840 (while (cond
11841 ((looking-at c-specifier-key)
11842 (c-forward-keyword-clause 1))
11843 ((and c-opt-cpp-prefix
11844 (looking-at c-noise-macro-with-parens-name-re))
11845 (c-forward-noise-clause))))
11846 (c-add-syntax 'defun-open (c-point 'boi))
11847 ;; Bogus to use bol here, but it's the legacy. (Resolved,
11848 ;; 2007-11-09)
11849 ))))
11851 ;; CASE 5R: Member init list. (Used to be part of CASE 5B.1)
11852 ;; Note there is no limit on the backward search here, since member
11853 ;; init lists can, in practice, be very large.
11854 ((save-excursion
11855 (when (and (c-major-mode-is 'c++-mode)
11856 (setq placeholder (c-back-over-member-initializers)))
11857 (setq tmp-pos (point))))
11858 (if (= (c-point 'bosws) (1+ tmp-pos))
11859 (progn
11860 ;; There is no preceding member init clause.
11861 ;; Indent relative to the beginning of indentation
11862 ;; for the topmost-intro line that contains the
11863 ;; prototype's open paren.
11864 (goto-char placeholder)
11865 (c-add-syntax 'member-init-intro (c-point 'boi)))
11866 ;; Indent relative to the first member init clause.
11867 (goto-char (1+ tmp-pos))
11868 (c-forward-syntactic-ws)
11869 (c-add-syntax 'member-init-cont (point))))
11871 ;; CASE 5B: After a function header but before the body (or
11872 ;; the ending semicolon if there's no body).
11873 ((save-excursion
11874 (when (setq placeholder (c-just-after-func-arglist-p
11875 (max lim (c-determine-limit 500))))
11876 (setq tmp-pos (point))))
11877 (cond
11879 ;; CASE 5B.1: Member init list.
11880 ((eq (char-after tmp-pos) ?:)
11881 ;; There is no preceding member init clause.
11882 ;; Indent relative to the beginning of indentation
11883 ;; for the topmost-intro line that contains the
11884 ;; prototype's open paren.
11885 (goto-char placeholder)
11886 (c-add-syntax 'member-init-intro (c-point 'boi)))
11888 ;; CASE 5B.2: K&R arg decl intro
11889 ((and c-recognize-knr-p
11890 (c-in-knr-argdecl lim))
11891 (c-beginning-of-statement-1 lim)
11892 (c-add-syntax 'knr-argdecl-intro (c-point 'boi))
11893 (if containing-decl-open
11894 (c-add-class-syntax 'inclass
11895 containing-decl-open
11896 containing-decl-start
11897 containing-decl-kwd
11898 paren-state)))
11900 ;; CASE 5B.4: Nether region after a C++ or Java func
11901 ;; decl, which could include a `throws' declaration.
11903 (c-beginning-of-statement-1 lim)
11904 (c-add-syntax 'func-decl-cont (c-point 'boi))
11907 ;; CASE 5C: inheritance line. could be first inheritance
11908 ;; line, or continuation of a multiple inheritance
11909 ((or (and (c-major-mode-is 'c++-mode)
11910 (progn
11911 (when (eq char-after-ip ?,)
11912 (skip-chars-forward " \t")
11913 (forward-char))
11914 (looking-at c-opt-postfix-decl-spec-key)))
11915 (and (or (eq char-before-ip ?:)
11916 ;; watch out for scope operator
11917 (save-excursion
11918 (and (eq char-after-ip ?:)
11919 (c-safe (forward-char 1) t)
11920 (not (eq (char-after) ?:))
11922 (save-excursion
11923 (c-beginning-of-statement-1 lim)
11924 (when (looking-at c-opt-<>-sexp-key)
11925 (goto-char (match-end 1))
11926 (c-forward-syntactic-ws)
11927 (c-forward-<>-arglist nil)
11928 (c-forward-syntactic-ws))
11929 (looking-at c-class-key)))
11930 ;; for Java
11931 (and (c-major-mode-is 'java-mode)
11932 (let ((fence (save-excursion
11933 (c-beginning-of-statement-1 lim)
11934 (point)))
11935 cont done)
11936 (save-excursion
11937 (while (not done)
11938 (cond ((looking-at c-opt-postfix-decl-spec-key)
11939 (setq injava-inher (cons cont (point))
11940 done t))
11941 ((or (not (c-safe (c-forward-sexp -1) t))
11942 (<= (point) fence))
11943 (setq done t))
11945 (setq cont t)))
11946 injava-inher)
11947 (not (c-crosses-statement-barrier-p (cdr injava-inher)
11948 (point)))
11950 (cond
11952 ;; CASE 5C.1: non-hanging colon on an inher intro
11953 ((eq char-after-ip ?:)
11954 (c-beginning-of-statement-1 lim)
11955 (c-add-syntax 'inher-intro (c-point 'boi))
11956 ;; don't add inclass symbol since relative point already
11957 ;; contains any class offset
11960 ;; CASE 5C.2: hanging colon on an inher intro
11961 ((eq char-before-ip ?:)
11962 (c-beginning-of-statement-1 lim)
11963 (c-add-syntax 'inher-intro (c-point 'boi))
11964 (if containing-decl-open
11965 (c-add-class-syntax 'inclass
11966 containing-decl-open
11967 containing-decl-start
11968 containing-decl-kwd
11969 paren-state)))
11971 ;; CASE 5C.3: in a Java implements/extends
11972 (injava-inher
11973 (let ((where (cdr injava-inher))
11974 (cont (car injava-inher)))
11975 (goto-char where)
11976 (cond ((looking-at "throws\\>[^_]")
11977 (c-add-syntax 'func-decl-cont
11978 (progn (c-beginning-of-statement-1 lim)
11979 (c-point 'boi))))
11980 (cont (c-add-syntax 'inher-cont where))
11981 (t (c-add-syntax 'inher-intro
11982 (progn (goto-char (cdr injava-inher))
11983 (c-beginning-of-statement-1 lim)
11984 (point))))
11987 ;; CASE 5C.4: a continued inheritance line
11989 (c-beginning-of-inheritance-list lim)
11990 (c-add-syntax 'inher-cont (point))
11991 ;; don't add inclass symbol since relative point already
11992 ;; contains any class offset
11995 ;; CASE 5P: AWK pattern or function or continuation
11996 ;; thereof.
11997 ((c-major-mode-is 'awk-mode)
11998 (setq placeholder (point))
11999 (c-add-stmt-syntax
12000 (if (and (eq (c-beginning-of-statement-1) 'same)
12001 (/= (point) placeholder))
12002 'topmost-intro-cont
12003 'topmost-intro)
12004 nil nil
12005 containing-sexp paren-state))
12007 ;; CASE 5D: this could be a top-level initialization, a
12008 ;; member init list continuation, or a template argument
12009 ;; list continuation.
12010 ((save-excursion
12011 ;; Note: We use the fact that lim is always after any
12012 ;; preceding brace sexp.
12013 (if c-recognize-<>-arglists
12014 (while (and
12015 (progn
12016 (c-syntactic-skip-backward "^;,=<>" lim t)
12017 (> (point) lim))
12019 (when c-overloadable-operators-regexp
12020 (when (setq placeholder (c-after-special-operator-id lim))
12021 (goto-char placeholder)
12023 (cond
12024 ((eq (char-before) ?>)
12025 (or (c-backward-<>-arglist nil lim)
12026 (backward-char))
12028 ((eq (char-before) ?<)
12029 (backward-char)
12030 (if (save-excursion
12031 (c-forward-<>-arglist nil))
12032 (progn (forward-char)
12033 nil)
12035 (t nil)))))
12036 ;; NB: No c-after-special-operator-id stuff in this
12037 ;; clause - we assume only C++ needs it.
12038 (c-syntactic-skip-backward "^;,=" lim t))
12039 (memq (char-before) '(?, ?= ?<)))
12040 (cond
12042 ;; CASE 5D.3: perhaps a template list continuation?
12043 ((and (c-major-mode-is 'c++-mode)
12044 (save-excursion
12045 (save-restriction
12046 (c-with-syntax-table c++-template-syntax-table
12047 (goto-char indent-point)
12048 (setq placeholder (c-up-list-backward))
12049 (and placeholder
12050 (eq (char-after placeholder) ?<))))))
12051 (c-with-syntax-table c++-template-syntax-table
12052 (goto-char placeholder)
12053 (c-beginning-of-statement-1 lim t))
12054 (if (save-excursion
12055 (c-backward-syntactic-ws lim)
12056 (eq (char-before) ?<))
12057 ;; In a nested template arglist.
12058 (progn
12059 (goto-char placeholder)
12060 (c-syntactic-skip-backward "^,;" lim t)
12061 (c-forward-syntactic-ws))
12062 (back-to-indentation))
12063 ;; FIXME: Should use c-add-stmt-syntax, but it's not yet
12064 ;; template aware.
12065 (c-add-syntax 'template-args-cont (point) placeholder))
12067 ;; CASE 5D.4: perhaps a multiple inheritance line?
12068 ((and (c-major-mode-is 'c++-mode)
12069 (save-excursion
12070 (c-beginning-of-statement-1 lim)
12071 (setq placeholder (point))
12072 (if (looking-at "static\\>[^_]")
12073 (c-forward-token-2 1 nil indent-point))
12074 (and (looking-at c-class-key)
12075 (zerop (c-forward-token-2 2 nil indent-point))
12076 (if (eq (char-after) ?<)
12077 (c-with-syntax-table c++-template-syntax-table
12078 (zerop (c-forward-token-2 1 t indent-point)))
12080 (eq (char-after) ?:))))
12081 (goto-char placeholder)
12082 (c-add-syntax 'inher-cont (c-point 'boi)))
12084 ;; CASE 5D.5: Continuation of the "expression part" of a
12085 ;; top level construct. Or, perhaps, an unrecognized construct.
12087 (while (and (setq placeholder (point))
12088 (eq (car (c-beginning-of-decl-1 containing-sexp)) ; Can't use `lim' here.
12089 'same)
12090 (save-excursion
12091 (c-backward-syntactic-ws)
12092 (eq (char-before) ?}))
12093 (< (point) placeholder)))
12094 (c-add-stmt-syntax
12095 (cond
12096 ((eq (point) placeholder) 'statement) ; unrecognized construct
12097 ;; A preceding comma at the top level means that a
12098 ;; new variable declaration starts here. Use
12099 ;; topmost-intro-cont for it, for consistency with
12100 ;; the first variable declaration. C.f. case 5N.
12101 ((eq char-before-ip ?,) 'topmost-intro-cont)
12102 (t 'statement-cont))
12103 nil nil containing-sexp paren-state))
12106 ;; CASE 5F: Close of a non-class declaration level block.
12107 ((and (eq char-after-ip ?})
12108 (c-keyword-member containing-decl-kwd
12109 'c-other-block-decl-kwds))
12110 ;; This is inconsistent: Should use `containing-decl-open'
12111 ;; here if it's at boi, like in case 5J.
12112 (goto-char containing-decl-start)
12113 (c-add-stmt-syntax
12114 (if (string-equal (symbol-name containing-decl-kwd) "extern")
12115 ;; Special case for compatibility with the
12116 ;; extern-lang syntactic symbols.
12117 'extern-lang-close
12118 (intern (concat (symbol-name containing-decl-kwd)
12119 "-close")))
12120 nil t
12121 (c-most-enclosing-brace paren-state (point))
12122 paren-state))
12124 ;; CASE 5G: we are looking at the brace which closes the
12125 ;; enclosing nested class decl
12126 ((and containing-sexp
12127 (eq char-after-ip ?})
12128 (eq containing-decl-open containing-sexp))
12129 (c-add-class-syntax 'class-close
12130 containing-decl-open
12131 containing-decl-start
12132 containing-decl-kwd
12133 paren-state))
12135 ;; CASE 5H: we could be looking at subsequent knr-argdecls
12136 ((and c-recognize-knr-p
12137 (not containing-sexp) ; can't be knr inside braces.
12138 (not (eq char-before-ip ?}))
12139 (save-excursion
12140 (setq placeholder (cdr (c-beginning-of-decl-1 lim)))
12141 (and placeholder
12142 ;; Do an extra check to avoid tripping up on
12143 ;; statements that occur in invalid contexts
12144 ;; (e.g. in macro bodies where we don't really
12145 ;; know the context of what we're looking at).
12146 (not (and c-opt-block-stmt-key
12147 (looking-at c-opt-block-stmt-key)))))
12148 (< placeholder indent-point))
12149 (goto-char placeholder)
12150 (c-add-syntax 'knr-argdecl (point)))
12152 ;; CASE 5I: ObjC method definition.
12153 ((and c-opt-method-key
12154 (looking-at c-opt-method-key))
12155 (c-beginning-of-statement-1 nil t)
12156 (if (= (point) indent-point)
12157 ;; Handle the case when it's the first (non-comment)
12158 ;; thing in the buffer. Can't look for a 'same return
12159 ;; value from cbos1 since ObjC directives currently
12160 ;; aren't recognized fully, so that we get 'same
12161 ;; instead of 'previous if it moved over a preceding
12162 ;; directive.
12163 (goto-char (point-min)))
12164 (c-add-syntax 'objc-method-intro (c-point 'boi)))
12166 ;; CASE 5N: At a variable declaration that follows a class
12167 ;; definition or some other block declaration that doesn't
12168 ;; end at the closing '}'. C.f. case 5D.5.
12169 ((progn
12170 (c-backward-syntactic-ws lim)
12171 (and (eq (char-before) ?})
12172 (save-excursion
12173 (let ((start (point)))
12174 (if (and c-state-cache
12175 (consp (car c-state-cache))
12176 (eq (cdar c-state-cache) (point)))
12177 ;; Speed up the backward search a bit.
12178 (goto-char (caar c-state-cache)))
12179 (c-beginning-of-decl-1 containing-sexp) ; Can't use `lim' here.
12180 (setq placeholder (point))
12181 (if (= start (point))
12182 ;; The '}' is unbalanced.
12184 (c-end-of-decl-1)
12185 (>= (point) indent-point))))))
12186 (goto-char placeholder)
12187 (c-add-stmt-syntax 'topmost-intro-cont nil nil
12188 containing-sexp paren-state))
12190 ;; NOTE: The point is at the end of the previous token here.
12192 ;; CASE 5J: we are at the topmost level, make
12193 ;; sure we skip back past any access specifiers
12194 ((and
12195 ;; A macro continuation line is never at top level.
12196 (not (and macro-start
12197 (> indent-point macro-start)))
12198 (save-excursion
12199 (setq placeholder (point))
12200 (or (memq char-before-ip '(?\; ?{ ?} nil))
12201 (c-at-vsemi-p before-ws-ip)
12202 (when (and (eq char-before-ip ?:)
12203 (eq (c-beginning-of-statement-1 lim)
12204 'label))
12205 (c-backward-syntactic-ws lim)
12206 (setq placeholder (point)))
12207 (and (c-major-mode-is 'objc-mode)
12208 (catch 'not-in-directive
12209 (c-beginning-of-statement-1 lim)
12210 (setq placeholder (point))
12211 (while (and (c-forward-objc-directive)
12212 (< (point) indent-point))
12213 (c-forward-syntactic-ws)
12214 (if (>= (point) indent-point)
12215 (throw 'not-in-directive t))
12216 (setq placeholder (point)))
12217 nil)))))
12218 ;; For historic reasons we anchor at bol of the last
12219 ;; line of the previous declaration. That's clearly
12220 ;; highly bogus and useless, and it makes our lives hard
12221 ;; to remain compatible. :P
12222 (goto-char placeholder)
12223 (c-add-syntax 'topmost-intro (c-point 'bol))
12224 (if containing-decl-open
12225 (if (c-keyword-member containing-decl-kwd
12226 'c-other-block-decl-kwds)
12227 (progn
12228 (goto-char (c-brace-anchor-point containing-decl-open))
12229 (c-add-stmt-syntax
12230 (if (string-equal (symbol-name containing-decl-kwd)
12231 "extern")
12232 ;; Special case for compatibility with the
12233 ;; extern-lang syntactic symbols.
12234 'inextern-lang
12235 (intern (concat "in"
12236 (symbol-name containing-decl-kwd))))
12237 nil t
12238 (c-most-enclosing-brace paren-state (point))
12239 paren-state))
12240 (c-add-class-syntax 'inclass
12241 containing-decl-open
12242 containing-decl-start
12243 containing-decl-kwd
12244 paren-state)))
12245 (when (and c-syntactic-indentation-in-macros
12246 macro-start
12247 (/= macro-start (c-point 'boi indent-point)))
12248 (c-add-syntax 'cpp-define-intro)
12249 (setq macro-start nil)))
12251 ;; CASE 5K: we are at an ObjC method definition
12252 ;; continuation line.
12253 ((and c-opt-method-key
12254 (save-excursion
12255 (c-beginning-of-statement-1 lim)
12256 (beginning-of-line)
12257 (when (looking-at c-opt-method-key)
12258 (setq placeholder (point)))))
12259 (c-add-syntax 'objc-method-args-cont placeholder))
12261 ;; CASE 5L: we are at the first argument of a template
12262 ;; arglist that begins on the previous line.
12263 ((and c-recognize-<>-arglists
12264 (eq (char-before) ?<)
12265 (not (and c-overloadable-operators-regexp
12266 (c-after-special-operator-id lim))))
12267 (c-beginning-of-statement-1 (c-safe-position (point) paren-state))
12268 (c-add-syntax 'template-args-cont (c-point 'boi)))
12270 ;; CASE 5Q: we are at a statement within a macro.
12271 (macro-start
12272 (c-beginning-of-statement-1 containing-sexp)
12273 (c-add-stmt-syntax 'statement nil t containing-sexp paren-state))
12275 ;;CASE 5N: We are at a topmost continuation line and the only
12276 ;;preceding items are annotations.
12277 ((and (c-major-mode-is 'java-mode)
12278 (setq placeholder (point))
12279 (c-beginning-of-statement-1)
12280 (progn
12281 (while (and (c-forward-annotation))
12282 (c-forward-syntactic-ws))
12284 (prog1
12285 (>= (point) placeholder)
12286 (goto-char placeholder)))
12287 (c-add-syntax 'annotation-top-cont (c-point 'boi)))
12289 ;; CASE 5M: we are at a topmost continuation line
12291 (c-beginning-of-statement-1 (c-safe-position (point) paren-state))
12292 (when (c-major-mode-is 'objc-mode)
12293 (setq placeholder (point))
12294 (while (and (c-forward-objc-directive)
12295 (< (point) indent-point))
12296 (c-forward-syntactic-ws)
12297 (setq placeholder (point)))
12298 (goto-char placeholder))
12299 (c-add-syntax 'topmost-intro-cont (c-point 'boi)))
12302 ;; (CASE 6 has been removed.)
12304 ;; CASE 7: line is an expression, not a statement. Most
12305 ;; likely we are either in a function prototype or a function
12306 ;; call argument list
12307 ((not (or (and c-special-brace-lists
12308 (save-excursion
12309 (goto-char containing-sexp)
12310 (c-looking-at-special-brace-list)))
12311 (eq (char-after containing-sexp) ?{)))
12312 (cond
12314 ;; CASE 7A: we are looking at the arglist closing paren.
12315 ;; C.f. case 7F.
12316 ((memq char-after-ip '(?\) ?\]))
12317 (goto-char containing-sexp)
12318 (setq placeholder (c-point 'boi))
12319 (if (and (c-safe (backward-up-list 1) t)
12320 (>= (point) placeholder))
12321 (progn
12322 (forward-char)
12323 (skip-chars-forward " \t"))
12324 (goto-char placeholder))
12325 (c-add-stmt-syntax 'arglist-close (list containing-sexp) t
12326 (c-most-enclosing-brace paren-state (point))
12327 paren-state))
12329 ;; CASE 7B: Looking at the opening brace of an
12330 ;; in-expression block or brace list. C.f. cases 4, 16A
12331 ;; and 17E.
12332 ((and (eq char-after-ip ?{)
12333 (progn
12334 (setq placeholder (c-inside-bracelist-p (point)
12335 paren-state))
12336 (if placeholder
12337 (setq tmpsymbol '(brace-list-open . inexpr-class))
12338 (setq tmpsymbol '(block-open . inexpr-statement)
12339 placeholder
12340 (cdr-safe (c-looking-at-inexpr-block
12341 (c-safe-position containing-sexp
12342 paren-state)
12343 containing-sexp)))
12344 ;; placeholder is nil if it's a block directly in
12345 ;; a function arglist. That makes us skip out of
12346 ;; this case.
12348 (goto-char placeholder)
12349 (back-to-indentation)
12350 (c-add-stmt-syntax (car tmpsymbol) nil t
12351 (c-most-enclosing-brace paren-state (point))
12352 paren-state)
12353 (if (/= (point) placeholder)
12354 (c-add-syntax (cdr tmpsymbol))))
12356 ;; CASE 7C: we are looking at the first argument in an empty
12357 ;; argument list. Use arglist-close if we're actually
12358 ;; looking at a close paren or bracket.
12359 ((memq char-before-ip '(?\( ?\[))
12360 (goto-char containing-sexp)
12361 (setq placeholder (c-point 'boi))
12362 (if (and (c-safe (backward-up-list 1) t)
12363 (>= (point) placeholder))
12364 (progn
12365 (forward-char)
12366 (skip-chars-forward " \t"))
12367 (goto-char placeholder))
12368 (c-add-stmt-syntax 'arglist-intro (list containing-sexp) t
12369 (c-most-enclosing-brace paren-state (point))
12370 paren-state))
12372 ;; CASE 7D: we are inside a conditional test clause. treat
12373 ;; these things as statements
12374 ((progn
12375 (goto-char containing-sexp)
12376 (and (c-safe (c-forward-sexp -1) t)
12377 (looking-at "\\<for\\>[^_]")))
12378 (goto-char (1+ containing-sexp))
12379 (c-forward-syntactic-ws indent-point)
12380 (if (eq char-before-ip ?\;)
12381 (c-add-syntax 'statement (point))
12382 (c-add-syntax 'statement-cont (point))
12385 ;; CASE 7E: maybe a continued ObjC method call. This is the
12386 ;; case when we are inside a [] bracketed exp, and what
12387 ;; precede the opening bracket is not an identifier.
12388 ((and c-opt-method-key
12389 (eq (char-after containing-sexp) ?\[)
12390 (progn
12391 (goto-char (1- containing-sexp))
12392 (c-backward-syntactic-ws (c-point 'bod))
12393 (if (not (looking-at c-symbol-key))
12394 (c-add-syntax 'objc-method-call-cont containing-sexp))
12397 ;; CASE 7F: we are looking at an arglist continuation line,
12398 ;; but the preceding argument is on the same line as the
12399 ;; opening paren. This case includes multi-line
12400 ;; mathematical paren groupings, but we could be on a
12401 ;; for-list continuation line. C.f. case 7A.
12402 ((progn
12403 (goto-char (1+ containing-sexp))
12404 (< (save-excursion
12405 (c-forward-syntactic-ws)
12406 (point))
12407 (c-point 'bonl)))
12408 (goto-char containing-sexp) ; paren opening the arglist
12409 (setq placeholder (c-point 'boi))
12410 (if (and (c-safe (backward-up-list 1) t)
12411 (>= (point) placeholder))
12412 (progn
12413 (forward-char)
12414 (skip-chars-forward " \t"))
12415 (goto-char placeholder))
12416 (c-add-stmt-syntax 'arglist-cont-nonempty (list containing-sexp) t
12417 (c-most-enclosing-brace c-state-cache (point))
12418 paren-state))
12420 ;; CASE 7G: we are looking at just a normal arglist
12421 ;; continuation line
12422 (t (c-forward-syntactic-ws indent-point)
12423 (c-add-syntax 'arglist-cont (c-point 'boi)))
12426 ;; CASE 8: func-local multi-inheritance line
12427 ((and (c-major-mode-is 'c++-mode)
12428 (save-excursion
12429 (goto-char indent-point)
12430 (skip-chars-forward " \t")
12431 (looking-at c-opt-postfix-decl-spec-key)))
12432 (goto-char indent-point)
12433 (skip-chars-forward " \t")
12434 (cond
12436 ;; CASE 8A: non-hanging colon on an inher intro
12437 ((eq char-after-ip ?:)
12438 (c-backward-syntactic-ws lim)
12439 (c-add-syntax 'inher-intro (c-point 'boi)))
12441 ;; CASE 8B: hanging colon on an inher intro
12442 ((eq char-before-ip ?:)
12443 (c-add-syntax 'inher-intro (c-point 'boi)))
12445 ;; CASE 8C: a continued inheritance line
12447 (c-beginning-of-inheritance-list lim)
12448 (c-add-syntax 'inher-cont (point))
12451 ;; CASE 9: we are inside a brace-list
12452 ((and (not (c-major-mode-is 'awk-mode)) ; Maybe this isn't needed (ACM, 2002/3/29)
12453 (setq special-brace-list
12454 (or (and c-special-brace-lists ;;;; ALWAYS NIL FOR AWK!!
12455 (save-excursion
12456 (goto-char containing-sexp)
12457 (c-looking-at-special-brace-list)))
12458 (c-inside-bracelist-p containing-sexp paren-state))))
12459 (cond
12461 ;; CASE 9A: In the middle of a special brace list opener.
12462 ((and (consp special-brace-list)
12463 (save-excursion
12464 (goto-char containing-sexp)
12465 (eq (char-after) ?\())
12466 (eq char-after-ip (car (cdr special-brace-list))))
12467 (goto-char (car (car special-brace-list)))
12468 (skip-chars-backward " \t")
12469 (if (and (bolp)
12470 (assoc 'statement-cont
12471 (setq placeholder (c-guess-basic-syntax))))
12472 (setq c-syntactic-context placeholder)
12473 (c-beginning-of-statement-1
12474 (c-safe-position (1- containing-sexp) paren-state))
12475 (c-forward-token-2 0)
12476 (while (cond
12477 ((looking-at c-specifier-key)
12478 (c-forward-keyword-clause 1))
12479 ((and c-opt-cpp-prefix
12480 (looking-at c-noise-macro-with-parens-name-re))
12481 (c-forward-noise-clause))))
12482 (c-add-syntax 'brace-list-open (c-point 'boi))))
12484 ;; CASE 9B: brace-list-close brace
12485 ((if (consp special-brace-list)
12486 ;; Check special brace list closer.
12487 (progn
12488 (goto-char (car (car special-brace-list)))
12489 (save-excursion
12490 (goto-char indent-point)
12491 (back-to-indentation)
12493 ;; We were between the special close char and the `)'.
12494 (and (eq (char-after) ?\))
12495 (eq (1+ (point)) (cdr (car special-brace-list))))
12496 ;; We were before the special close char.
12497 (and (eq (char-after) (cdr (cdr special-brace-list)))
12498 (zerop (c-forward-token-2))
12499 (eq (1+ (point)) (cdr (car special-brace-list)))))))
12500 ;; Normal brace list check.
12501 (and (eq char-after-ip ?})
12502 (c-safe (goto-char (c-up-list-backward (point))) t)
12503 (= (point) containing-sexp)))
12504 (if (eq (point) (c-point 'boi))
12505 (c-add-syntax 'brace-list-close (point))
12506 (setq lim (c-most-enclosing-brace c-state-cache (point)))
12507 (c-beginning-of-statement-1 lim nil nil t)
12508 (c-add-stmt-syntax 'brace-list-close nil t lim paren-state)))
12511 ;; Prepare for the rest of the cases below by going to the
12512 ;; token following the opening brace
12513 (if (consp special-brace-list)
12514 (progn
12515 (goto-char (car (car special-brace-list)))
12516 (c-forward-token-2 1 nil indent-point))
12517 (goto-char containing-sexp))
12518 (forward-char)
12519 (let ((start (point)))
12520 (c-forward-syntactic-ws indent-point)
12521 (goto-char (max start (c-point 'bol))))
12522 (c-skip-ws-forward indent-point)
12523 (cond
12525 ;; CASE 9C: we're looking at the first line in a brace-list
12526 ((= (point) indent-point)
12527 (if (consp special-brace-list)
12528 (goto-char (car (car special-brace-list)))
12529 (goto-char containing-sexp))
12530 (if (eq (point) (c-point 'boi))
12531 (c-add-syntax 'brace-list-intro (point))
12532 (setq lim (c-most-enclosing-brace c-state-cache (point)))
12533 (c-beginning-of-statement-1 lim)
12534 (c-add-stmt-syntax 'brace-list-intro nil t lim paren-state)))
12536 ;; CASE 9D: this is just a later brace-list-entry or
12537 ;; brace-entry-open
12538 (t (if (or (eq char-after-ip ?{)
12539 (and c-special-brace-lists
12540 (save-excursion
12541 (goto-char indent-point)
12542 (c-forward-syntactic-ws (c-point 'eol))
12543 (c-looking-at-special-brace-list (point)))))
12544 (c-add-syntax 'brace-entry-open (point))
12545 (c-add-stmt-syntax 'brace-list-entry nil t containing-sexp
12546 paren-state (point))
12548 ))))
12550 ;; CASE 10: A continued statement or top level construct.
12551 ((and (not (memq char-before-ip '(?\; ?:)))
12552 (not (c-at-vsemi-p before-ws-ip))
12553 (or (not (eq char-before-ip ?}))
12554 (c-looking-at-inexpr-block-backward c-state-cache))
12555 (> (point)
12556 (save-excursion
12557 (c-beginning-of-statement-1 containing-sexp)
12558 (setq placeholder (point))))
12559 (/= placeholder containing-sexp))
12560 ;; This is shared with case 18.
12561 (c-guess-continued-construct indent-point
12562 char-after-ip
12563 placeholder
12564 containing-sexp
12565 paren-state))
12567 ;; CASE 16: block close brace, possibly closing the defun or
12568 ;; the class
12569 ((eq char-after-ip ?})
12570 ;; From here on we have the next containing sexp in lim.
12571 (setq lim (c-most-enclosing-brace paren-state))
12572 (goto-char containing-sexp)
12573 (cond
12575 ;; CASE 16E: Closing a statement block? This catches
12576 ;; cases where it's preceded by a statement keyword,
12577 ;; which works even when used in an "invalid" context,
12578 ;; e.g. a macro argument.
12579 ((c-after-conditional)
12580 (c-backward-to-block-anchor lim)
12581 (c-add-stmt-syntax 'block-close nil t lim paren-state))
12583 ;; CASE 16A: closing a lambda defun or an in-expression
12584 ;; block? C.f. cases 4, 7B and 17E.
12585 ((setq placeholder (c-looking-at-inexpr-block
12586 (c-safe-position containing-sexp paren-state)
12587 nil))
12588 (setq tmpsymbol (if (eq (car placeholder) 'inlambda)
12589 'inline-close
12590 'block-close))
12591 (goto-char containing-sexp)
12592 (back-to-indentation)
12593 (if (= containing-sexp (point))
12594 (c-add-syntax tmpsymbol (point))
12595 (goto-char (cdr placeholder))
12596 (back-to-indentation)
12597 (c-add-stmt-syntax tmpsymbol nil t
12598 (c-most-enclosing-brace paren-state (point))
12599 paren-state)
12600 (if (/= (point) (cdr placeholder))
12601 (c-add-syntax (car placeholder)))))
12603 ;; CASE 16B: does this close an inline or a function in
12604 ;; a non-class declaration level block?
12605 ((save-excursion
12606 (and lim
12607 (progn
12608 (goto-char lim)
12609 (c-looking-at-decl-block
12610 (c-most-enclosing-brace paren-state lim)
12611 nil))
12612 (setq placeholder (point))))
12613 (c-backward-to-decl-anchor lim)
12614 (back-to-indentation)
12615 (if (save-excursion
12616 (goto-char placeholder)
12617 (looking-at c-other-decl-block-key))
12618 (c-add-syntax 'defun-close (point))
12619 (c-add-syntax 'inline-close (point))))
12621 ;; CASE 16F: Can be a defun-close of a function declared
12622 ;; in a statement block, e.g. in Pike or when using gcc
12623 ;; extensions, but watch out for macros followed by
12624 ;; blocks. Let it through to be handled below.
12625 ;; C.f. cases B.3 and 17G.
12626 ((save-excursion
12627 (and (not (c-at-statement-start-p))
12628 (eq (c-beginning-of-statement-1 lim nil nil t) 'same)
12629 (setq placeholder (point))
12630 (let ((c-recognize-typeless-decls nil))
12631 ;; Turn off recognition of constructs that
12632 ;; lacks a type in this case, since that's more
12633 ;; likely to be a macro followed by a block.
12634 (c-forward-decl-or-cast-1 (c-point 'bosws) nil nil))))
12635 (back-to-indentation)
12636 (if (/= (point) containing-sexp)
12637 (goto-char placeholder))
12638 (c-add-stmt-syntax 'defun-close nil t lim paren-state))
12640 ;; CASE 16C: If there is an enclosing brace then this is
12641 ;; a block close since defun closes inside declaration
12642 ;; level blocks have been handled above.
12643 (lim
12644 ;; If the block is preceded by a case/switch label on
12645 ;; the same line, we anchor at the first preceding label
12646 ;; at boi. The default handling in c-add-stmt-syntax
12647 ;; really fixes it better, but we do like this to keep
12648 ;; the indentation compatible with version 5.28 and
12649 ;; earlier. C.f. case 17H.
12650 (while (and (/= (setq placeholder (point)) (c-point 'boi))
12651 (eq (c-beginning-of-statement-1 lim) 'label)))
12652 (goto-char placeholder)
12653 (if (looking-at c-label-kwds-regexp)
12654 (c-add-syntax 'block-close (point))
12655 (goto-char containing-sexp)
12656 ;; c-backward-to-block-anchor not necessary here; those
12657 ;; situations are handled in case 16E above.
12658 (c-add-stmt-syntax 'block-close nil t lim paren-state)))
12660 ;; CASE 16D: Only top level defun close left.
12662 (goto-char containing-sexp)
12663 (c-backward-to-decl-anchor lim)
12664 (c-add-stmt-syntax 'defun-close nil nil
12665 (c-most-enclosing-brace paren-state)
12666 paren-state))
12669 ;; CASE 19: line is an expression, not a statement, and is directly
12670 ;; contained by a template delimiter. Most likely, we are in a
12671 ;; template arglist within a statement. This case is based on CASE
12672 ;; 7. At some point in the future, we may wish to create more
12673 ;; syntactic symbols such as `template-intro',
12674 ;; `template-cont-nonempty', etc., and distinguish between them as we
12675 ;; do for `arglist-intro' etc. (2009-12-07).
12676 ((and c-recognize-<>-arglists
12677 (setq containing-< (c-up-list-backward indent-point containing-sexp))
12678 (eq (char-after containing-<) ?\<))
12679 (setq placeholder (c-point 'boi containing-<))
12680 (goto-char containing-sexp) ; Most nested Lbrace/Lparen (but not
12681 ; '<') before indent-point.
12682 (if (>= (point) placeholder)
12683 (progn
12684 (forward-char)
12685 (skip-chars-forward " \t"))
12686 (goto-char placeholder))
12687 (c-add-stmt-syntax 'template-args-cont (list containing-<) t
12688 (c-most-enclosing-brace c-state-cache (point))
12689 paren-state))
12691 ;; CASE 17: Statement or defun catchall.
12693 (goto-char indent-point)
12694 ;; Back up statements until we find one that starts at boi.
12695 (while (let* ((prev-point (point))
12696 (last-step-type (c-beginning-of-statement-1
12697 containing-sexp)))
12698 (if (= (point) prev-point)
12699 (progn
12700 (setq step-type (or step-type last-step-type))
12701 nil)
12702 (setq step-type last-step-type)
12703 (/= (point) (c-point 'boi)))))
12704 (cond
12706 ;; CASE 17B: continued statement
12707 ((and (eq step-type 'same)
12708 (/= (point) indent-point))
12709 (c-add-stmt-syntax 'statement-cont nil nil
12710 containing-sexp paren-state))
12712 ;; CASE 17A: After a case/default label?
12713 ((progn
12714 (while (and (eq step-type 'label)
12715 (not (looking-at c-label-kwds-regexp)))
12716 (setq step-type
12717 (c-beginning-of-statement-1 containing-sexp)))
12718 (eq step-type 'label))
12719 (c-add-stmt-syntax (if (eq char-after-ip ?{)
12720 'statement-case-open
12721 'statement-case-intro)
12722 nil t containing-sexp paren-state))
12724 ;; CASE 17D: any old statement
12725 ((progn
12726 (while (eq step-type 'label)
12727 (setq step-type
12728 (c-beginning-of-statement-1 containing-sexp)))
12729 (eq step-type 'previous))
12730 (c-add-stmt-syntax 'statement nil t
12731 containing-sexp paren-state)
12732 (if (eq char-after-ip ?{)
12733 (c-add-syntax 'block-open)))
12735 ;; CASE 17I: Inside a substatement block.
12736 ((progn
12737 ;; The following tests are all based on containing-sexp.
12738 (goto-char containing-sexp)
12739 ;; From here on we have the next containing sexp in lim.
12740 (setq lim (c-most-enclosing-brace paren-state containing-sexp))
12741 (c-after-conditional))
12742 (c-backward-to-block-anchor lim)
12743 (c-add-stmt-syntax 'statement-block-intro nil t
12744 lim paren-state)
12745 (if (eq char-after-ip ?{)
12746 (c-add-syntax 'block-open)))
12748 ;; CASE 17E: first statement in an in-expression block.
12749 ;; C.f. cases 4, 7B and 16A.
12750 ((setq placeholder (c-looking-at-inexpr-block
12751 (c-safe-position containing-sexp paren-state)
12752 nil))
12753 (setq tmpsymbol (if (eq (car placeholder) 'inlambda)
12754 'defun-block-intro
12755 'statement-block-intro))
12756 (back-to-indentation)
12757 (if (= containing-sexp (point))
12758 (c-add-syntax tmpsymbol (point))
12759 (goto-char (cdr placeholder))
12760 (back-to-indentation)
12761 (c-add-stmt-syntax tmpsymbol nil t
12762 (c-most-enclosing-brace c-state-cache (point))
12763 paren-state)
12764 (if (/= (point) (cdr placeholder))
12765 (c-add-syntax (car placeholder))))
12766 (if (eq char-after-ip ?{)
12767 (c-add-syntax 'block-open)))
12769 ;; CASE 17F: first statement in an inline, or first
12770 ;; statement in a top-level defun. we can tell this is it
12771 ;; if there are no enclosing braces that haven't been
12772 ;; narrowed out by a class (i.e. don't use bod here).
12773 ((save-excursion
12774 (or (not (setq placeholder (c-most-enclosing-brace
12775 paren-state)))
12776 (and (progn
12777 (goto-char placeholder)
12778 (eq (char-after) ?{))
12779 (c-looking-at-decl-block (c-most-enclosing-brace
12780 paren-state (point))
12781 nil))))
12782 (c-backward-to-decl-anchor lim)
12783 (back-to-indentation)
12784 (c-add-syntax 'defun-block-intro (point)))
12786 ;; CASE 17G: First statement in a function declared inside
12787 ;; a normal block. This can occur in Pike and with
12788 ;; e.g. the gcc extensions, but watch out for macros
12789 ;; followed by blocks. C.f. cases B.3 and 16F.
12790 ((save-excursion
12791 (and (not (c-at-statement-start-p))
12792 (eq (c-beginning-of-statement-1 lim nil nil t) 'same)
12793 (setq placeholder (point))
12794 (let ((c-recognize-typeless-decls nil))
12795 ;; Turn off recognition of constructs that lacks
12796 ;; a type in this case, since that's more likely
12797 ;; to be a macro followed by a block.
12798 (c-forward-decl-or-cast-1 (c-point 'bosws) nil nil))))
12799 (back-to-indentation)
12800 (if (/= (point) containing-sexp)
12801 (goto-char placeholder))
12802 (c-add-stmt-syntax 'defun-block-intro nil t
12803 lim paren-state))
12805 ;; CASE 17H: First statement in a block.
12807 ;; If the block is preceded by a case/switch label on the
12808 ;; same line, we anchor at the first preceding label at
12809 ;; boi. The default handling in c-add-stmt-syntax is
12810 ;; really fixes it better, but we do like this to keep the
12811 ;; indentation compatible with version 5.28 and earlier.
12812 ;; C.f. case 16C.
12813 (while (and (/= (setq placeholder (point)) (c-point 'boi))
12814 (eq (c-beginning-of-statement-1 lim) 'label)))
12815 (goto-char placeholder)
12816 (if (looking-at c-label-kwds-regexp)
12817 (c-add-syntax 'statement-block-intro (point))
12818 (goto-char containing-sexp)
12819 ;; c-backward-to-block-anchor not necessary here; those
12820 ;; situations are handled in case 17I above.
12821 (c-add-stmt-syntax 'statement-block-intro nil t
12822 lim paren-state))
12823 (if (eq char-after-ip ?{)
12824 (c-add-syntax 'block-open)))
12828 ;; now we need to look at any modifiers
12829 (goto-char indent-point)
12830 (skip-chars-forward " \t")
12832 ;; are we looking at a comment only line?
12833 (when (and (looking-at c-comment-start-regexp)
12834 (/= (c-forward-token-2 0 nil (c-point 'eol)) 0))
12835 (c-append-syntax 'comment-intro))
12837 ;; we might want to give additional offset to friends (in C++).
12838 (when (and c-opt-friend-key
12839 (looking-at c-opt-friend-key))
12840 (c-append-syntax 'friend))
12842 ;; Set syntactic-relpos.
12843 (let ((p c-syntactic-context))
12844 (while (and p
12845 (if (integerp (c-langelem-pos (car p)))
12846 (progn
12847 (setq syntactic-relpos (c-langelem-pos (car p)))
12848 nil)
12850 (setq p (cdr p))))
12852 ;; Start of or a continuation of a preprocessor directive?
12853 (if (and macro-start
12854 (eq macro-start (c-point 'boi))
12855 (not (and (c-major-mode-is 'pike-mode)
12856 (eq (char-after (1+ macro-start)) ?\"))))
12857 (c-append-syntax 'cpp-macro)
12858 (when (and c-syntactic-indentation-in-macros macro-start)
12859 (if in-macro-expr
12860 (when (or
12861 (< syntactic-relpos macro-start)
12862 (not (or
12863 (assq 'arglist-intro c-syntactic-context)
12864 (assq 'arglist-cont c-syntactic-context)
12865 (assq 'arglist-cont-nonempty c-syntactic-context)
12866 (assq 'arglist-close c-syntactic-context))))
12867 ;; If inside a cpp expression, i.e. anywhere in a
12868 ;; cpp directive except a #define body, we only let
12869 ;; through the syntactic analysis that is internal
12870 ;; in the expression. That means the arglist
12871 ;; elements, if they are anchored inside the cpp
12872 ;; expression.
12873 (setq c-syntactic-context nil)
12874 (c-add-syntax 'cpp-macro-cont macro-start))
12875 (when (and (eq macro-start syntactic-relpos)
12876 (not (assq 'cpp-define-intro c-syntactic-context))
12877 (save-excursion
12878 (goto-char macro-start)
12879 (or (not (c-forward-to-cpp-define-body))
12880 (<= (point) (c-point 'boi indent-point)))))
12881 ;; Inside a #define body and the syntactic analysis is
12882 ;; anchored on the start of the #define. In this case
12883 ;; we add cpp-define-intro to get the extra
12884 ;; indentation of the #define body.
12885 (c-add-syntax 'cpp-define-intro)))))
12887 ;; return the syntax
12888 c-syntactic-context)))
12891 ;; Indentation calculation.
12893 (defun c-evaluate-offset (offset langelem symbol)
12894 ;; offset can be a number, a function, a variable, a list, or one of
12895 ;; the symbols + or -
12897 ;; This function might do hidden buffer changes.
12898 (let ((res
12899 (cond
12900 ((numberp offset) offset)
12901 ((vectorp offset) offset)
12902 ((null offset) nil)
12904 ((eq offset '+) c-basic-offset)
12905 ((eq offset '-) (- c-basic-offset))
12906 ((eq offset '++) (* 2 c-basic-offset))
12907 ((eq offset '--) (* 2 (- c-basic-offset)))
12908 ((eq offset '*) (/ c-basic-offset 2))
12909 ((eq offset '/) (/ (- c-basic-offset) 2))
12911 ((functionp offset)
12912 (c-evaluate-offset
12913 (funcall offset
12914 (cons (c-langelem-sym langelem)
12915 (c-langelem-pos langelem)))
12916 langelem symbol))
12918 ((listp offset)
12919 (cond
12920 ((eq (car offset) 'quote)
12921 (c-benign-error "The offset %S for %s was mistakenly quoted"
12922 offset symbol)
12923 nil)
12925 ((memq (car offset) '(min max))
12926 (let (res val (method (car offset)))
12927 (setq offset (cdr offset))
12928 (while offset
12929 (setq val (c-evaluate-offset (car offset) langelem symbol))
12930 (cond
12931 ((not val))
12932 ((not res)
12933 (setq res val))
12934 ((integerp val)
12935 (if (vectorp res)
12936 (c-benign-error "\
12937 Error evaluating offset %S for %s: \
12938 Cannot combine absolute offset %S with relative %S in `%s' method"
12939 (car offset) symbol res val method)
12940 (setq res (funcall method res val))))
12942 (if (integerp res)
12943 (c-benign-error "\
12944 Error evaluating offset %S for %s: \
12945 Cannot combine relative offset %S with absolute %S in `%s' method"
12946 (car offset) symbol res val method)
12947 (setq res (vector (funcall method (aref res 0)
12948 (aref val 0)))))))
12949 (setq offset (cdr offset)))
12950 res))
12952 ((eq (car offset) 'add)
12953 (let (res val)
12954 (setq offset (cdr offset))
12955 (while offset
12956 (setq val (c-evaluate-offset (car offset) langelem symbol))
12957 (cond
12958 ((not val))
12959 ((not res)
12960 (setq res val))
12961 ((integerp val)
12962 (if (vectorp res)
12963 (setq res (vector (+ (aref res 0) val)))
12964 (setq res (+ res val))))
12966 (if (vectorp res)
12967 (c-benign-error "\
12968 Error evaluating offset %S for %s: \
12969 Cannot combine absolute offsets %S and %S in `add' method"
12970 (car offset) symbol res val)
12971 (setq res val)))) ; Override.
12972 (setq offset (cdr offset)))
12973 res))
12976 (let (res)
12977 (when (eq (car offset) 'first)
12978 (setq offset (cdr offset)))
12979 (while (and (not res) offset)
12980 (setq res (c-evaluate-offset (car offset) langelem symbol)
12981 offset (cdr offset)))
12982 res))))
12984 ((and (symbolp offset) (boundp offset))
12985 (symbol-value offset))
12988 (c-benign-error "Unknown offset format %S for %s" offset symbol)
12989 nil))))
12991 (if (or (null res) (integerp res)
12992 (and (vectorp res) (= (length res) 1) (integerp (aref res 0))))
12994 (c-benign-error "Error evaluating offset %S for %s: Got invalid value %S"
12995 offset symbol res)
12996 nil)))
12998 (defun c-calc-offset (langelem)
12999 ;; Get offset from LANGELEM which is a list beginning with the
13000 ;; syntactic symbol and followed by any analysis data it provides.
13001 ;; That data may be zero or more elements, but if at least one is
13002 ;; given then the first is the anchor position (or nil). The symbol
13003 ;; is matched against `c-offsets-alist' and the offset calculated
13004 ;; from that is returned.
13006 ;; This function might do hidden buffer changes.
13007 (let* ((symbol (c-langelem-sym langelem))
13008 (match (assq symbol c-offsets-alist))
13009 (offset (cdr-safe match)))
13010 (if match
13011 (setq offset (c-evaluate-offset offset langelem symbol))
13012 (if c-strict-syntax-p
13013 (c-benign-error "No offset found for syntactic symbol %s" symbol))
13014 (setq offset 0))
13015 (if (vectorp offset)
13016 offset
13017 (or (and (numberp offset) offset)
13018 (and (symbolp offset) (symbol-value offset))
13022 (defun c-get-offset (langelem)
13023 ;; This is a compatibility wrapper for `c-calc-offset' in case
13024 ;; someone is calling it directly. It takes an old style syntactic
13025 ;; element on the form (SYMBOL . ANCHOR-POS) and converts it to the
13026 ;; new list form.
13028 ;; This function might do hidden buffer changes.
13029 (if (c-langelem-pos langelem)
13030 (c-calc-offset (list (c-langelem-sym langelem)
13031 (c-langelem-pos langelem)))
13032 (c-calc-offset langelem)))
13034 (defun c-get-syntactic-indentation (langelems)
13035 ;; Calculate the syntactic indentation from a syntactic description
13036 ;; as returned by `c-guess-syntax'.
13038 ;; Note that topmost-intro always has an anchor position at bol, for
13039 ;; historical reasons. It's often used together with other symbols
13040 ;; that have more sane positions. Since we always use the first
13041 ;; found anchor position, we rely on that these other symbols always
13042 ;; precede topmost-intro in the LANGELEMS list.
13044 ;; This function might do hidden buffer changes.
13045 (let ((indent 0) anchor)
13047 (while langelems
13048 (let* ((c-syntactic-element (car langelems))
13049 (res (c-calc-offset c-syntactic-element)))
13051 (if (vectorp res)
13052 ;; Got an absolute column that overrides any indentation
13053 ;; we've collected so far, but not the relative
13054 ;; indentation we might get for the nested structures
13055 ;; further down the langelems list.
13056 (setq indent (elt res 0)
13057 anchor (point-min)) ; A position at column 0.
13059 ;; Got a relative change of the current calculated
13060 ;; indentation.
13061 (setq indent (+ indent res))
13063 ;; Use the anchor position from the first syntactic
13064 ;; element with one.
13065 (unless anchor
13066 (setq anchor (c-langelem-pos (car langelems)))))
13068 (setq langelems (cdr langelems))))
13070 (if anchor
13071 (+ indent (save-excursion
13072 (goto-char anchor)
13073 (current-column)))
13074 indent)))
13077 (cc-provide 'cc-engine)
13079 ;; Local Variables:
13080 ;; indent-tabs-mode: t
13081 ;; tab-width: 8
13082 ;; End:
13083 ;;; cc-engine.el ends here