; * etc/NEWS: Fix last change.
[emacs.git] / lisp / progmodes / cc-engine.el
blob12ec8f74fea4e82d7a16bf4773237474bbe1e9e0
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 <https://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 elements 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 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 (let ((s (parse-partial-sexp macro-start macro-end)))
1984 (if (and (elt s 4) ; in a comment
1985 (null (elt s 7))) ; a block comment
1986 (setq safe-start nil)))
1987 (forward-line 1)
1988 ;; Don't cache at eob in case the buffer is narrowed.
1989 (not (eobp)))
1991 ((and c-opt-cpp-prefix
1992 (looking-at c-noise-macro-name-re))
1993 ;; Skip over a noise macro.
1994 (goto-char (match-end 1))
1995 (not (eobp)))))
1997 ;; We've searched over a piece of non-white syntactic ws. See if this
1998 ;; can be cached.
1999 (setq next-rung-pos (point))
2000 (skip-chars-forward " \t\n\r\f\v")
2001 (setq rung-end-pos (min (1+ (point)) (point-max)))
2003 (if (or
2004 ;; Cache if we haven't skipped comments only, and if we started
2005 ;; either from a marked rung or from a completely uncached
2006 ;; position.
2007 (and safe-start
2008 (or rung-is-marked
2009 (not (get-text-property simple-ws-end 'c-in-sws))))
2011 ;; See if there's a marked rung in the encountered simple ws. If
2012 ;; so then we can cache, unless `safe-start' is nil. Even then
2013 ;; we need to do this to check if the cache can be used for the
2014 ;; next step.
2015 (and (setq next-rung-is-marked
2016 (text-property-any next-rung-pos rung-end-pos
2017 'c-is-sws t))
2018 safe-start))
2020 (progn
2021 (c-debug-sws-msg
2022 "c-forward-sws caching [%s..%s] - [%s..%s] (max %s)"
2023 rung-pos (1+ simple-ws-end) next-rung-pos rung-end-pos
2024 (point-max))
2026 ;; Remove the properties for any nested ws that might be cached.
2027 ;; Only necessary for `c-is-sws' since `c-in-sws' will be set
2028 ;; anyway.
2029 (c-remove-is-sws (1+ simple-ws-end) next-rung-pos)
2030 (unless (and rung-is-marked (= rung-pos simple-ws-end))
2031 (c-put-is-sws rung-pos
2032 (1+ simple-ws-end))
2033 (setq rung-is-marked t))
2034 (c-put-in-sws rung-pos
2035 (setq rung-pos (point)
2036 last-put-in-sws-pos rung-pos))
2037 (unless (get-text-property (1- rung-end-pos) 'c-is-sws)
2038 ;; Remove any `c-in-sws' property from the last char of
2039 ;; the rung before we mark it with `c-is-sws', so that we
2040 ;; won't connect with the remains of a broken "ladder".
2041 (c-remove-in-sws (1- rung-end-pos) rung-end-pos))
2042 (c-put-is-sws next-rung-pos
2043 rung-end-pos))
2045 (c-debug-sws-msg
2046 "c-forward-sws not caching [%s..%s] - [%s..%s] (max %s)"
2047 rung-pos (1+ simple-ws-end) next-rung-pos rung-end-pos
2048 (point-max))
2050 ;; Set `rung-pos' for the next rung. It's the same thing here as
2051 ;; initially, except that the rung position is set as early as
2052 ;; possible since we can't be in the ending ws of a line comment or
2053 ;; cpp directive now.
2054 (if (setq rung-is-marked next-rung-is-marked)
2055 (setq rung-pos (1- (c-next-single-property-change
2056 rung-is-marked 'c-is-sws nil rung-end-pos)))
2057 (setq rung-pos next-rung-pos))))
2059 ;; Make sure that the newly marked `c-in-sws' region doesn't connect to
2060 ;; another one after the point (which might occur when editing inside a
2061 ;; comment or macro).
2062 (when (eq last-put-in-sws-pos (point))
2063 (cond ((< last-put-in-sws-pos (point-max))
2064 (c-debug-sws-msg
2065 "c-forward-sws clearing at %s for cache separation"
2066 last-put-in-sws-pos)
2067 (c-remove-in-sws last-put-in-sws-pos
2068 (1+ last-put-in-sws-pos)))
2070 ;; If at eob we have to clear the last character before the end
2071 ;; instead since the buffer might be narrowed and there might
2072 ;; be a `c-in-sws' after (point-max). In this case it's
2073 ;; necessary to clear both properties.
2074 (c-debug-sws-msg
2075 "c-forward-sws clearing thoroughly at %s for cache separation"
2076 (1- last-put-in-sws-pos))
2077 (c-remove-is-and-in-sws (1- last-put-in-sws-pos)
2078 last-put-in-sws-pos))))
2079 ))))
2081 (defun c-backward-sws ()
2082 ;; Used by `c-backward-syntactic-ws' to implement the unbounded search.
2084 ;; This function might do hidden buffer changes.
2086 (let (;; `rung-pos' is set to a position as late as possible in the unmarked
2087 ;; part of the simple ws region.
2088 (rung-pos (point)) next-rung-pos last-put-in-sws-pos
2089 rung-is-marked simple-ws-beg cmt-skip-pos)
2091 ;; Skip simple horizontal ws and do a quick check on the preceding
2092 ;; character to see if it's anything that can't end syntactic ws, so we can
2093 ;; bail out early in the majority of cases when there just are a few ws
2094 ;; chars. Newlines are complicated in the backward direction, so we can't
2095 ;; skip over them.
2096 (skip-chars-backward " \t\f")
2097 (when (and (not (bobp))
2098 (save-excursion
2099 (backward-char)
2100 (or (looking-at c-syntactic-ws-end)
2101 (and c-opt-cpp-prefix
2102 (looking-at c-symbol-char-key)
2103 (progn (c-beginning-of-current-token)
2104 (looking-at c-noise-macro-name-re))))))
2105 ;; Try to find a rung position in the simple ws preceding point, so that
2106 ;; we can get a cache hit even if the last bit of the simple ws has
2107 ;; changed recently.
2108 (setq simple-ws-beg (point))
2109 (skip-chars-backward " \t\n\r\f\v")
2110 (if (setq rung-is-marked (text-property-any
2111 (point) (min (1+ rung-pos) (point-max))
2112 'c-is-sws t))
2113 ;; `rung-pos' will be the earliest marked position, which means that
2114 ;; there might be later unmarked parts in the simple ws region.
2115 ;; It's not worth the effort to fix that; the last part of the
2116 ;; simple ws is also typically edited often, so it could be wasted.
2117 (goto-char (setq rung-pos rung-is-marked))
2118 (goto-char simple-ws-beg))
2120 (with-silent-modifications
2121 (while
2122 (progn
2123 ;; Each time round the next while form, we move back over a ladder
2124 ;; and append any simple WS preceding it, if possible joining with
2125 ;; the previous ladder.
2126 (while
2127 (when (and rung-is-marked
2128 (not (bobp))
2129 (get-text-property (1- (point)) 'c-in-sws))
2131 ;; The following search is the main reason that `c-in-sws'
2132 ;; and `c-is-sws' aren't combined to one property.
2133 (goto-char (previous-single-property-change
2134 (point) 'c-in-sws nil (point-min)))
2135 (unless (get-text-property (point) 'c-is-sws)
2136 ;; If the `c-in-sws' region extended past the first
2137 ;; `c-is-sws' char we have to go forward a bit.
2138 (goto-char (c-next-single-property-change
2139 (point) 'c-is-sws)))
2141 (c-debug-sws-msg
2142 "c-backward-sws cached move %s <- %s (min %s)"
2143 (point) rung-pos (point-min))
2145 (setq rung-pos (point))
2146 (if (and (< (min (skip-chars-backward " \t\f\v")
2147 (progn
2148 (setq simple-ws-beg (point))
2149 (skip-chars-backward " \t\n\r\f\v")))
2151 (setq rung-is-marked
2152 (text-property-any (point) rung-pos
2153 'c-is-sws t)))
2155 (goto-char simple-ws-beg)
2156 nil))
2158 ;; We'll loop here if there is simple ws before the first rung.
2159 ;; That means that there's been some change in it and it's
2160 ;; possible that we've stepped into another ladder, so extend
2161 ;; the previous one to join with it if there is one, and try to
2162 ;; use the cache again.
2163 (c-debug-sws-msg
2164 "c-backward-sws extending rung with [%s..%s] (min %s)"
2165 rung-is-marked rung-pos (point-min))
2166 (unless (get-text-property (1- rung-pos) 'c-is-sws)
2167 ;; Remove any `c-in-sws' property from the last char of
2168 ;; the rung before we mark it with `c-is-sws', so that we
2169 ;; won't connect with the remains of a broken "ladder".
2170 (c-remove-in-sws (1- rung-pos) rung-pos))
2171 (c-put-is-sws rung-is-marked
2172 rung-pos)
2173 (c-put-in-sws rung-is-marked
2174 (1- rung-pos))
2175 (setq rung-pos rung-is-marked
2176 last-put-in-sws-pos rung-pos))
2178 (c-backward-comments)
2179 (setq cmt-skip-pos (point))
2181 (cond
2182 ((and c-opt-cpp-prefix
2183 (/= cmt-skip-pos simple-ws-beg)
2184 (c-beginning-of-macro))
2185 ;; Inside a cpp directive. See if it should be skipped over.
2186 (let ((cpp-beg (point)))
2188 ;; Move back over all line continuations in the region skipped
2189 ;; over by `c-backward-comments'. If we go past it then we
2190 ;; started inside the cpp directive.
2191 (goto-char simple-ws-beg)
2192 (beginning-of-line)
2193 (while (and (> (point) cmt-skip-pos)
2194 (progn (backward-char)
2195 (eq (char-before) ?\\)))
2196 (beginning-of-line))
2198 (if (< (point) cmt-skip-pos)
2199 ;; Don't move past the cpp directive if we began inside
2200 ;; it. Note that the position at the end of the last line
2201 ;; of the macro is also considered to be within it.
2202 (progn (goto-char cmt-skip-pos)
2203 nil)
2205 ;; It's worthwhile to spend a little bit of effort on finding
2206 ;; the end of the macro, to get a good `simple-ws-beg'
2207 ;; position for the cache. Note that `c-backward-comments'
2208 ;; could have stepped over some comments before going into
2209 ;; the macro, and then `simple-ws-beg' must be kept on the
2210 ;; same side of those comments.
2211 (goto-char simple-ws-beg)
2212 (skip-chars-backward " \t\n\r\f\v")
2213 (if (eq (char-before) ?\\)
2214 (forward-char))
2215 (forward-line 1)
2216 (if (< (point) simple-ws-beg)
2217 ;; Might happen if comments after the macro were skipped
2218 ;; over.
2219 (setq simple-ws-beg (point)))
2221 (goto-char cpp-beg)
2222 t)))
2224 ((/= (save-excursion
2225 (skip-chars-forward " \t\n\r\f\v" simple-ws-beg)
2226 (setq next-rung-pos (point)))
2227 simple-ws-beg)
2228 ;; Skipped over comments. Must put point at the end of
2229 ;; the simple ws at point since we might be after a line
2230 ;; comment or cpp directive that's been partially
2231 ;; narrowed out, and we can't risk marking the simple ws
2232 ;; at the end of it.
2233 (goto-char next-rung-pos)
2236 ((and c-opt-cpp-prefix
2237 (save-excursion
2238 (and (< (skip-syntax-backward "w_") 0)
2239 (progn (setq next-rung-pos (point))
2240 (looking-at c-noise-macro-name-re)))))
2241 ;; Skipped over a noise macro
2242 (goto-char next-rung-pos)
2243 t)))
2245 ;; We've searched over a piece of non-white syntactic ws. See if this
2246 ;; can be cached.
2247 (setq next-rung-pos (point))
2248 (skip-chars-backward " \t\f\v")
2250 (if (or
2251 ;; Cache if we started either from a marked rung or from a
2252 ;; completely uncached position.
2253 rung-is-marked
2254 (not (get-text-property (1- simple-ws-beg) 'c-in-sws))
2256 ;; Cache if there's a marked rung in the encountered simple ws.
2257 (save-excursion
2258 (skip-chars-backward " \t\n\r\f\v")
2259 (text-property-any (point) (min (1+ next-rung-pos) (point-max))
2260 'c-is-sws t)))
2262 (progn
2263 (c-debug-sws-msg
2264 "c-backward-sws caching [%s..%s] - [%s..%s] (min %s)"
2265 (point) (1+ next-rung-pos)
2266 simple-ws-beg (min (1+ rung-pos) (point-max))
2267 (point-min))
2269 ;; Remove the properties for any nested ws that might be cached.
2270 ;; Only necessary for `c-is-sws' since `c-in-sws' will be set
2271 ;; anyway.
2272 (c-remove-is-sws (1+ next-rung-pos) simple-ws-beg)
2273 (unless (and rung-is-marked (= simple-ws-beg rung-pos))
2274 (let ((rung-end-pos (min (1+ rung-pos) (point-max))))
2275 (unless (get-text-property (1- rung-end-pos) 'c-is-sws)
2276 ;; Remove any `c-in-sws' property from the last char of
2277 ;; the rung before we mark it with `c-is-sws', so that we
2278 ;; won't connect with the remains of a broken "ladder".
2279 (c-remove-in-sws (1- rung-end-pos) rung-end-pos))
2280 (c-put-is-sws simple-ws-beg
2281 rung-end-pos)
2282 (setq rung-is-marked t)))
2283 (c-put-in-sws (setq simple-ws-beg (point)
2284 last-put-in-sws-pos simple-ws-beg)
2285 rung-pos)
2286 (c-put-is-sws (setq rung-pos simple-ws-beg)
2287 (1+ next-rung-pos)))
2289 (c-debug-sws-msg
2290 "c-backward-sws not caching [%s..%s] - [%s..%s] (min %s)"
2291 (point) (1+ next-rung-pos)
2292 simple-ws-beg (min (1+ rung-pos) (point-max))
2293 (point-min))
2294 (setq rung-pos next-rung-pos
2295 simple-ws-beg (point))
2298 ;; Make sure that the newly marked `c-in-sws' region doesn't connect to
2299 ;; another one before the point (which might occur when editing inside a
2300 ;; comment or macro).
2301 (when (eq last-put-in-sws-pos (point))
2302 (cond ((< (point-min) last-put-in-sws-pos)
2303 (c-debug-sws-msg
2304 "c-backward-sws clearing at %s for cache separation"
2305 (1- last-put-in-sws-pos))
2306 (c-remove-in-sws (1- last-put-in-sws-pos)
2307 last-put-in-sws-pos))
2308 ((> (point-min) 1)
2309 ;; If at bob and the buffer is narrowed, we have to clear the
2310 ;; character we're standing on instead since there might be a
2311 ;; `c-in-sws' before (point-min). In this case it's necessary
2312 ;; to clear both properties.
2313 (c-debug-sws-msg
2314 "c-backward-sws clearing thoroughly at %s for cache separation"
2315 last-put-in-sws-pos)
2316 (c-remove-is-and-in-sws last-put-in-sws-pos
2317 (1+ last-put-in-sws-pos)))))
2318 ))))
2321 ;; Other whitespace tools
2322 (defun c-partial-ws-p (beg end)
2323 ;; Is the region (beg end) WS, and is there WS (or BOB/EOB) next to the
2324 ;; region? This is a "heuristic" function. .....
2326 ;; The motivation for the second bit is to check whether removing this
2327 ;; region would coalesce two symbols.
2329 ;; FIXME!!! This function doesn't check virtual semicolons in any way. Be
2330 ;; careful about using this function for, e.g. AWK. (2007/3/7)
2331 (save-excursion
2332 (let ((end+1 (min (1+ end) (point-max))))
2333 (or (progn (goto-char (max (point-min) (1- beg)))
2334 (c-skip-ws-forward end)
2335 (eq (point) end))
2336 (progn (goto-char beg)
2337 (c-skip-ws-forward end+1)
2338 (eq (point) end+1))))))
2340 ;; A system for finding noteworthy parens before the point.
2342 (defconst c-state-cache-too-far 5000)
2343 ;; A maximum comfortable scanning distance, e.g. between
2344 ;; `c-state-cache-good-pos' and "HERE" (where we call c-parse-state). When
2345 ;; this distance is exceeded, we take "emergency measures", e.g. by clearing
2346 ;; the cache and starting again from point-min or a beginning of defun. This
2347 ;; value can be tuned for efficiency or set to a lower value for testing.
2349 (defvar c-state-cache nil)
2350 (make-variable-buffer-local 'c-state-cache)
2351 ;; The state cache used by `c-parse-state' to cut down the amount of
2352 ;; searching. It's the result from some earlier `c-parse-state' call. See
2353 ;; `c-parse-state''s doc string for details of its structure.
2355 ;; The use of the cached info is more effective if the next
2356 ;; `c-parse-state' call is on a line close by the one the cached state
2357 ;; was made at; the cache can actually slow down a little if the
2358 ;; cached state was made very far back in the buffer. The cache is
2359 ;; most effective if `c-parse-state' is used on each line while moving
2360 ;; forward.
2362 (defvar c-state-cache-good-pos 1)
2363 (make-variable-buffer-local 'c-state-cache-good-pos)
2364 ;; This is a position where `c-state-cache' is known to be correct, or
2365 ;; nil (see below). It's a position inside one of the recorded unclosed
2366 ;; parens or the top level, but not further nested inside any literal or
2367 ;; subparen that is closed before the last recorded position.
2369 ;; The exact position is chosen to try to be close to yet earlier than
2370 ;; the position where `c-state-cache' will be called next. Right now
2371 ;; the heuristic is to set it to the position after the last found
2372 ;; closing paren (of any type) before the line on which
2373 ;; `c-parse-state' was called. That is chosen primarily to work well
2374 ;; with refontification of the current line.
2376 ;; 2009-07-28: When `c-state-point-min' and the last position where
2377 ;; `c-parse-state' or for which `c-invalidate-state-cache' was called, are
2378 ;; both in the same literal, there is no such "good position", and
2379 ;; c-state-cache-good-pos is then nil. This is the ONLY circumstance in which
2380 ;; it can be nil. In this case, `c-state-point-min-literal' will be non-nil.
2382 ;; 2009-06-12: In a brace desert, c-state-cache-good-pos may also be in
2383 ;; the middle of the desert, as long as it is not within a brace pair
2384 ;; recorded in `c-state-cache' or a paren/bracket pair.
2386 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2387 ;; We maintain a simple cache of positions which aren't in a literal, so as to
2388 ;; speed up testing for non-literality.
2389 (defconst c-state-nonlit-pos-interval 3000)
2390 ;; The approximate interval between entries in `c-state-nonlit-pos-cache'.
2392 (defvar c-state-nonlit-pos-cache nil)
2393 (make-variable-buffer-local 'c-state-nonlit-pos-cache)
2394 ;; A list of buffer positions which are known not to be in a literal or a cpp
2395 ;; construct. This is ordered with higher positions at the front of the list.
2396 ;; Only those which are less than `c-state-nonlit-pos-cache-limit' are valid.
2398 (defvar c-state-nonlit-pos-cache-limit 1)
2399 (make-variable-buffer-local 'c-state-nonlit-pos-cache-limit)
2400 ;; An upper limit on valid entries in `c-state-nonlit-pos-cache'. This is
2401 ;; reduced by buffer changes, and increased by invocations of
2402 ;; `c-state-literal-at'.
2404 (defvar c-state-semi-nonlit-pos-cache nil)
2405 (make-variable-buffer-local 'c-state-semi-nonlit-pos-cache)
2406 ;; A list of elements which are either buffer positions (when such positions
2407 ;; are not in literals) or lists of the form (POS TYPE START), where POS is
2408 ;; a buffer position inside a literal, TYPE is the type of the literal
2409 ;; ('string, 'c, or 'c++) and START is the start of the literal.
2411 (defvar c-state-semi-nonlit-pos-cache-limit 1)
2412 (make-variable-buffer-local 'c-state-semi-nonlit-pos-cache-limit)
2413 ;; An upper limit on valid entries in `c-state-semi-nonlit-pos-cache'. This
2414 ;; is reduced by buffer changes, and increased by invocations of
2415 ;; `c-parse-ps-state-below'.
2417 (defsubst c-truncate-semi-nonlit-pos-cache (pos)
2418 ;; Truncate the upper bound of the cache `c-state-semi-nonlit-pos-cache' to
2419 ;; POS, if it is higher than that position.
2420 (setq c-state-semi-nonlit-pos-cache-limit
2421 (min c-state-semi-nonlit-pos-cache-limit pos)))
2423 (defun c-state-semi-pp-to-literal (here &optional not-in-delimiter)
2424 ;; Do a parse-partial-sexp from a position in the buffer before HERE which
2425 ;; isn't in a literal, and return information about HERE, either:
2426 ;; (STATE TYPE BEG) if HERE is in a literal; or
2427 ;; (STATE) otherwise,
2428 ;; where STATE is the parsing state at HERE, TYPE is the type of the literal
2429 ;; enclosing HERE, (one of 'string, 'c, 'c++) and BEG is the starting
2430 ;; position of that literal (including the delimiter).
2432 ;; Unless NOT-IN-DELIMITER is non-nil, when TO is inside a two-character
2433 ;; comment opener, this is recognized as being in a comment literal.
2435 ;; Only elements 3 (in a string), 4 (in a comment), 5 (following a quote), 7
2436 ;; (comment type), and 8 (start of comment/string), and possibly 10 (in
2437 ;; newer Emacsen only, the syntax of a position after a potential first char
2438 ;; of a two char construct) of STATE are valid.
2439 (save-excursion
2440 (save-restriction
2441 (widen)
2442 (save-match-data
2443 (let* ((base-and-state (c-parse-ps-state-below here))
2444 (base (car base-and-state))
2445 (s (cdr base-and-state))
2446 (s (parse-partial-sexp base here nil nil s))
2448 (cond
2449 ((or (nth 3 s)
2450 (and (nth 4 s)
2451 (not (eq (nth 7 s) 'syntax-table)))) ; in a string or comment
2452 (setq ty (cond
2453 ((nth 3 s) 'string)
2454 ((nth 7 s) 'c++)
2455 (t 'c)))
2456 (list s ty (nth 8 s)))
2458 ((and (not not-in-delimiter) ; inside a comment starter
2459 (not (bobp))
2460 (progn (backward-char)
2461 (and (not (and (memq 'category-properties c-emacs-features)
2462 (looking-at "\\s!")))
2463 (looking-at c-comment-start-regexp))))
2464 (setq ty (if (looking-at c-block-comment-start-regexp) 'c 'c++))
2465 (list s ty (point)))
2467 (t (list s))))))))
2469 (defun c-state-full-pp-to-literal (here &optional not-in-delimiter)
2470 ;; This function will supersede c-state-pp-to-literal.
2472 ;; Do a parse-partial-sexp from a position in the buffer before HERE which
2473 ;; isn't in a literal, and return information about HERE, either:
2474 ;; (STATE TYPE (BEG . END)) if HERE is in a literal; or
2475 ;; (STATE) otherwise,
2476 ;; where STATE is the parsing state at HERE, TYPE is the type of the literal
2477 ;; enclosing HERE, (one of 'string, 'c, 'c++) and (BEG . END) is the
2478 ;; boundaries of that literal (including the delimiters).
2480 ;; Unless NOT-IN-DELIMITER is non-nil, when TO is inside a two-character
2481 ;; comment opener, this is recognized as being in a comment literal.
2483 ;; Only elements 3 (in a string), 4 (in a comment), 5 (following a quote), 7
2484 ;; (comment type), and 8 (start of comment/string), and possibly 10 (in
2485 ;; newer Emacsen only, the syntax of a position after a potential first char
2486 ;; of a two char construct) of STATE are valid.
2487 (save-excursion
2488 (save-restriction
2489 (widen)
2490 (save-match-data
2491 (let* ((base-and-state (c-parse-ps-state-below here))
2492 (base (car base-and-state))
2493 (s (cdr base-and-state))
2494 (s (parse-partial-sexp base here nil nil s))
2495 ty start)
2496 (cond
2497 ((or (nth 3 s)
2498 (and (nth 4 s)
2499 (not (eq (nth 7 s) 'syntax-table)))) ; in a string or comment
2500 (setq ty (cond
2501 ((nth 3 s) 'string)
2502 ((nth 7 s) 'c++)
2503 (t 'c)))
2504 (setq start (nth 8 s))
2505 (parse-partial-sexp here (point-max)
2506 nil ; TARGETDEPTH
2507 nil ; STOPBEFORE
2508 s ; OLDSTATE
2509 'syntax-table) ; stop at end of literal
2510 (list s ty (cons start (point))))
2512 ((and (not not-in-delimiter) ; inside a comment starter
2513 (not (bobp))
2514 (progn (backward-char)
2515 (and (not (and (memq 'category-properties c-emacs-features)
2516 (looking-at "\\s!")))
2517 (looking-at c-comment-start-regexp))))
2518 (setq ty (if (looking-at c-block-comment-start-regexp) 'c 'c++)
2519 start (point))
2520 (forward-comment 1)
2521 (list s ty (cons start (point))))
2523 (t (list s))))))))
2525 (defun c-state-pp-to-literal (from to &optional not-in-delimiter)
2526 ;; Do a parse-partial-sexp from FROM to TO, returning either
2527 ;; (STATE TYPE (BEG . END)) if TO is in a literal; or
2528 ;; (STATE) otherwise,
2529 ;; where STATE is the parsing state at TO, TYPE is the type of the literal
2530 ;; (one of 'c, 'c++, 'string) and (BEG . END) is the boundaries of the literal,
2531 ;; including the delimiters.
2533 ;; Unless NOT-IN-DELIMITER is non-nil, when TO is inside a two-character
2534 ;; comment opener, this is recognized as being in a comment literal.
2536 ;; Only elements 3 (in a string), 4 (in a comment), 5 (following a quote),
2537 ;; 7 (comment type) and 8 (start of comment/string) (and possibly 9) of
2538 ;; STATE are valid.
2539 (save-excursion
2540 (save-match-data
2541 (let ((s (parse-partial-sexp from to))
2542 ty co-st)
2543 (cond
2544 ((or (nth 3 s)
2545 (and (nth 4 s)
2546 (not (eq (nth 7 s) 'syntax-table)))) ; in a string or comment
2547 (setq ty (cond
2548 ((nth 3 s) 'string)
2549 ((nth 7 s) 'c++)
2550 (t 'c)))
2551 (parse-partial-sexp (point) (point-max)
2552 nil ; TARGETDEPTH
2553 nil ; STOPBEFORE
2554 s ; OLDSTATE
2555 'syntax-table) ; stop at end of literal
2556 `(,s ,ty (,(nth 8 s) . ,(point))))
2558 ((and (not not-in-delimiter) ; inside a comment starter
2559 (not (bobp))
2560 (progn (backward-char)
2561 (and (not (looking-at "\\s!"))
2562 (looking-at c-comment-start-regexp))))
2563 (setq ty (if (looking-at c-block-comment-start-regexp) 'c 'c++)
2564 co-st (point))
2565 (forward-comment 1)
2566 `(,s ,ty (,co-st . ,(point))))
2568 (t `(,s)))))))
2570 (defun c-cache-to-parse-ps-state (elt)
2571 ;; Create a list suitable to use as the old-state parameter to
2572 ;; `parse-partial-sexp', out of ELT, a member of
2573 ;; `c-state-semi-nonlit-pos-cache'. ELT is either just a number, or a list
2574 ;; with 2, 3, or 4 members (See `c-parse-ps-state-to-cache'). That number
2575 ;; or the car of the list is the "position element" of ELT, the position
2576 ;; where ELT is valid.
2578 ;; POINT is left at the postition for which the returned state is valid. It
2579 ;; will be either the position element of ELT, or one character before
2580 ;; that. (The latter happens in Emacs <= 25 and XEmacs, when ELT indicates
2581 ;; its position element directly follows a potential first character of a
2582 ;; two char construct (such as a comment opener or an escaped character).)
2583 (if (and (consp elt) (>= (length elt) 3))
2584 ;; Inside a string or comment
2585 (let ((depth 0) (containing nil) (last nil)
2586 in-string in-comment (after-quote nil)
2587 (min-depth 0) com-style com-str-start (intermediate nil)
2588 (char-1 (nth 3 elt)) ; first char of poss. 2-char construct
2589 (pos (car elt))
2590 (type (cadr elt)))
2591 (setq com-str-start (car (cddr elt)))
2592 (cond
2593 ((or (numberp type) (eq type t)) ; A string
2594 (setq in-string type))
2595 ((memq type '(c c++)) ; A comment
2596 (setq in-comment t
2597 com-style (if (eq type 'c++) 1 nil)))
2598 (t (c-benign-error "Invalid type %s in c-cache-to-parse-ps-state"
2599 elt)))
2600 (if (memq 'pps-extended-state c-emacs-features)
2601 (progn
2602 (goto-char pos)
2603 (list depth containing last
2604 in-string in-comment after-quote
2605 min-depth com-style com-str-start
2606 intermediate char-1))
2607 (goto-char (if char-1
2608 (1- pos)
2609 pos))
2610 (list depth containing last
2611 in-string in-comment nil
2612 min-depth com-style com-str-start
2613 intermediate)))
2615 ;; Not in a string or comment.
2616 (if (memq 'pps-extended-state c-emacs-features)
2617 (progn
2618 (goto-char (if (consp elt) (car elt) elt))
2619 (list 0 nil nil nil nil
2620 (and (consp elt) (eq (nth 1 elt) 9)) ; 9 is syntax code for "escape".
2621 0 nil nil nil
2622 (and (consp elt) (nth 1 elt))))
2623 (goto-char (if (consp elt) (car elt) elt))
2624 (if (and (consp elt) (cdr elt)) (backward-char))
2625 (copy-tree '(0 nil nil nil nil
2627 0 nil nil nil)))))
2629 (defun c-parse-ps-state-to-cache (state)
2630 ;; Convert STATE, a `parse-partial-sexp' state valid at POINT, to an element
2631 ;; for the `c-state-semi-nonlit-pos-cache' cache. This is one of
2632 ;; o - POINT (when point is not in a literal);
2633 ;; o - (POINT CHAR-1) (when the last character before point is potentially
2634 ;; the first of a two-character construct
2635 ;; o - (POINT TYPE STARTING-POS) (when in a literal);
2636 ;; o - (POINT TYPE STARTING-POS CHAR-1) (Combination of the previous two),
2637 ;; where TYPE is the type of the literal (either 'c, or 'c++, or the
2638 ;; character which closes the string), STARTING-POS is the starting
2639 ;; position of the comment or string. CHAR-1 is either the character
2640 ;; potentially forming the first half of a two-char construct (in Emacs <=
2641 ;; 25 and XEmacs) or the syntax of the character (in Emacs >= 26).
2642 (if (memq 'pps-extended-state c-emacs-features)
2643 ;; Emacs >= 26.
2644 (let ((basic
2645 (cond
2646 ((nth 3 state) ; A string
2647 (list (point) (nth 3 state) (nth 8 state)))
2648 ((and (nth 4 state) ; A comment
2649 (not (eq (nth 7 state) 'syntax-table))) ; but not a psuedo comment.
2650 (list (point)
2651 (if (eq (nth 7 state) 1) 'c++ 'c)
2652 (nth 8 state)))
2653 (t ; Neither string nor comment.
2654 (point)))))
2655 (if (nth 10 state)
2656 (append (if (consp basic)
2657 basic
2658 (list basic))
2659 (list (nth 10 state)))
2660 basic))
2662 ;; Emacs <= 25, XEmacs.
2663 (cond
2664 ((nth 3 state) ; A string
2665 (if (eq (char-before) ?\\)
2666 (list (point) (nth 3 state) (nth 8 state) ?\\)
2667 (list (point) (nth 3 state) (nth 8 state))))
2668 ((and (nth 4 state) ; comment
2669 (not (eq (nth 7 state) 'syntax-table)))
2670 (if (and (eq (char-before) ?*)
2671 (> (- (point) (nth 8 state)) 2)) ; not "/*/".
2672 (list (point)
2673 (if (eq (nth 7 state) 1) 'c++ 'c)
2674 (nth 8 state)
2676 (list (point)
2677 (if (eq (nth 7 state) 1) 'c++ 'c)
2678 (nth 8 state))))
2679 (t (if (memq (char-before) '(?/ ?\\))
2680 (list (point) (char-before))
2681 (point))))))
2683 (defsubst c-ps-state-cache-pos (elt)
2684 ;; Get the buffer position from ELT, an element from the cache
2685 ;; `c-state-semi-nonlit-pos-cache'.
2686 (if (atom elt)
2688 (car elt)))
2690 (defun c-parse-ps-state-below (here)
2691 ;; Given a buffer position HERE, Return a cons (CACHE-POS . STATE), where
2692 ;; CACHE-POS is a position not very far before HERE for which the
2693 ;; parse-partial-sexp STATE is valid. Note that the only valid elements of
2694 ;; STATE are those concerning comments and strings; STATE is the state of a
2695 ;; null `parse-partial-sexp' scan when CACHE-POS is not in a comment or
2696 ;; string.
2697 (save-excursion
2698 (save-restriction
2699 (widen)
2700 (let ((c c-state-semi-nonlit-pos-cache)
2701 elt state npos high-elt)
2702 ;; Trim the cache to take account of buffer changes.
2703 (while (and c (> (c-ps-state-cache-pos (car c))
2704 c-state-semi-nonlit-pos-cache-limit))
2705 (setq c (cdr c)))
2706 (setq c-state-semi-nonlit-pos-cache c)
2708 (while (and c (> (c-ps-state-cache-pos (car c)) here))
2709 (setq high-elt (car c))
2710 (setq c (cdr c)))
2711 (goto-char (or (and c (c-ps-state-cache-pos (car c)))
2712 (point-min)))
2713 (setq state
2714 (if c
2715 (c-cache-to-parse-ps-state (car c))
2716 (copy-tree '(0 nil nil nil nil nil 0 nil nil nil nil))))
2718 (when (not high-elt)
2719 ;; We need to extend the cache. Add an element to
2720 ;; `c-state-semi-nonlit-pos-cache' each iteration of the following.
2721 (while
2722 (<= (setq npos (+ (point) c-state-nonlit-pos-interval)) here)
2723 (setq state (parse-partial-sexp (point) npos nil nil state))
2724 (setq elt (c-parse-ps-state-to-cache state))
2725 (setq c-state-semi-nonlit-pos-cache
2726 (cons elt c-state-semi-nonlit-pos-cache))))
2728 (if (> (point) c-state-semi-nonlit-pos-cache-limit)
2729 (setq c-state-semi-nonlit-pos-cache-limit (point)))
2731 (cons (point) state)))))
2733 (defun c-state-safe-place (here)
2734 ;; Return a buffer position before HERE which is "safe", i.e. outside any
2735 ;; string, comment, or macro.
2737 ;; NOTE: This function manipulates `c-state-nonlit-pos-cache'. This cache
2738 ;; MAY NOT contain any positions within macros, since macros are frequently
2739 ;; turned into comments by use of the `c-cpp-delimiter' category properties.
2740 ;; We cannot rely on this mechanism whilst determining a cache pos since
2741 ;; this function is also called from outwith `c-parse-state'.
2742 (save-restriction
2743 (widen)
2744 (save-excursion
2745 (let ((c c-state-nonlit-pos-cache)
2746 pos npos high-pos lit macro-beg macro-end)
2747 ;; Trim the cache to take account of buffer changes.
2748 (while (and c (> (car c) c-state-nonlit-pos-cache-limit))
2749 (setq c (cdr c)))
2750 (setq c-state-nonlit-pos-cache c)
2752 (while (and c (> (car c) here))
2753 (setq high-pos (car c))
2754 (setq c (cdr c)))
2755 (setq pos (or (car c) (point-min)))
2757 (unless high-pos
2758 (while
2759 ;; Add an element to `c-state-nonlit-pos-cache' each iteration.
2760 (and
2761 (setq npos
2762 (when (<= (+ pos c-state-nonlit-pos-interval) here)
2763 (+ pos c-state-nonlit-pos-interval)))
2765 ;; Test for being in a literal. If so, go to after it.
2766 (progn
2767 (setq lit (car (cddr (c-state-pp-to-literal pos npos))))
2768 (or (null lit)
2769 (prog1 (<= (cdr lit) here)
2770 (setq npos (cdr lit)))))
2772 ;; Test for being in a macro. If so, go to after it.
2773 (progn
2774 (goto-char npos)
2775 (setq macro-beg
2776 (and (c-beginning-of-macro) (/= (point) npos) (point)))
2777 (when macro-beg
2778 (c-syntactic-end-of-macro)
2779 (or (eobp) (forward-char))
2780 (setq macro-end (point)))
2781 (or (null macro-beg)
2782 (prog1 (<= macro-end here)
2783 (setq npos macro-end)))))
2785 (setq pos npos)
2786 (setq c-state-nonlit-pos-cache (cons pos c-state-nonlit-pos-cache)))
2787 ;; Add one extra element above HERE so as to avoid the previous
2788 ;; expensive calculation when the next call is close to the current
2789 ;; one. This is especially useful when inside a large macro.
2790 (when npos
2791 (setq c-state-nonlit-pos-cache
2792 (cons npos c-state-nonlit-pos-cache))))
2794 (if (> pos c-state-nonlit-pos-cache-limit)
2795 (setq c-state-nonlit-pos-cache-limit pos))
2796 pos))))
2798 (defun c-state-literal-at (here)
2799 ;; If position HERE is inside a literal, return (START . END), the
2800 ;; boundaries of the literal (which may be outside the accessible bit of the
2801 ;; buffer). Otherwise, return nil.
2803 ;; This function is almost the same as `c-literal-limits'. Previously, it
2804 ;; differed in that it was a lower level function, and that it rigorously
2805 ;; followed the syntax from BOB. `c-literal-limits' is now (2011-12)
2806 ;; virtually identical to this function.
2807 (save-restriction
2808 (widen)
2809 (save-excursion
2810 (let ((pos (c-state-safe-place here)))
2811 (car (cddr (c-state-pp-to-literal pos here)))))))
2813 (defsubst c-state-lit-beg (pos)
2814 ;; Return the start of the literal containing POS, or POS itself.
2815 (or (car (c-state-literal-at pos))
2816 pos))
2818 (defsubst c-state-cache-non-literal-place (pos state)
2819 ;; Return a position outside of a string/comment/macro at or before POS.
2820 ;; STATE is the parse-partial-sexp state at POS.
2821 (let ((res (if (or (nth 3 state) ; in a string?
2822 (and (nth 4 state)
2823 (not (eq (nth 7 state) 'syntax-table)))) ; in a comment?
2824 (nth 8 state)
2825 pos)))
2826 (save-excursion
2827 (goto-char res)
2828 (if (c-beginning-of-macro)
2829 (point)
2830 res))))
2832 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2833 ;; Stuff to do with point-min, and coping with any literal there.
2834 (defvar c-state-point-min 1)
2835 (make-variable-buffer-local 'c-state-point-min)
2836 ;; This is (point-min) when `c-state-cache' was last calculated. A change of
2837 ;; narrowing is likely to affect the parens that are visible before the point.
2839 (defvar c-state-point-min-lit-type nil)
2840 (make-variable-buffer-local 'c-state-point-min-lit-type)
2841 (defvar c-state-point-min-lit-start nil)
2842 (make-variable-buffer-local 'c-state-point-min-lit-start)
2843 ;; These two variables define the literal, if any, containing point-min.
2844 ;; Their values are, respectively, 'string, c, or c++, and the start of the
2845 ;; literal. If there's no literal there, they're both nil.
2847 (defvar c-state-min-scan-pos 1)
2848 (make-variable-buffer-local 'c-state-min-scan-pos)
2849 ;; This is the earliest buffer-pos from which scanning can be done. It is
2850 ;; either the end of the literal containing point-min, or point-min itself.
2851 ;; It becomes nil if the buffer is changed earlier than this point.
2852 (defun c-state-get-min-scan-pos ()
2853 ;; Return the lowest valid scanning pos. This will be the end of the
2854 ;; literal enclosing point-min, or point-min itself.
2855 (or c-state-min-scan-pos
2856 (save-restriction
2857 (save-excursion
2858 (widen)
2859 (goto-char c-state-point-min-lit-start)
2860 (if (eq c-state-point-min-lit-type 'string)
2861 (forward-sexp)
2862 (forward-comment 1))
2863 (setq c-state-min-scan-pos (point))))))
2865 (defun c-state-mark-point-min-literal ()
2866 ;; Determine the properties of any literal containing POINT-MIN, setting the
2867 ;; variables `c-state-point-min-lit-type', `c-state-point-min-lit-start',
2868 ;; and `c-state-min-scan-pos' accordingly. The return value is meaningless.
2869 (let ((p-min (point-min))
2870 lit)
2871 (save-restriction
2872 (widen)
2873 (setq lit (c-state-literal-at p-min))
2874 (if lit
2875 (setq c-state-point-min-lit-type
2876 (save-excursion
2877 (goto-char (car lit))
2878 (cond
2879 ((looking-at c-block-comment-start-regexp) 'c)
2880 ((looking-at c-line-comment-starter) 'c++)
2881 (t 'string)))
2882 c-state-point-min-lit-start (car lit)
2883 c-state-min-scan-pos (cdr lit))
2884 (setq c-state-point-min-lit-type nil
2885 c-state-point-min-lit-start nil
2886 c-state-min-scan-pos p-min)))))
2889 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2890 ;; A variable which signals a brace dessert - helpful for reducing the number
2891 ;; of fruitless backward scans.
2892 (defvar c-state-brace-pair-desert nil)
2893 (make-variable-buffer-local 'c-state-brace-pair-desert)
2894 ;; Used only in `c-append-lower-brace-pair-to-state-cache'. It is set when
2895 ;; that defun has searched backwards for a brace pair and not found one. Its
2896 ;; value is either nil or a cons (PA . FROM), where PA is the position of the
2897 ;; enclosing opening paren/brace/bracket which bounds the backwards search (or
2898 ;; nil when at top level) and FROM is where the backward search started. It
2899 ;; is reset to nil in `c-invalidate-state-cache'.
2902 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2903 ;; Lowish level functions/macros which work directly on `c-state-cache', or a
2904 ;; list of like structure.
2905 (defmacro c-state-cache-top-lparen (&optional cache)
2906 ;; Return the address of the top left brace/bracket/paren recorded in CACHE
2907 ;; (default `c-state-cache') (or nil).
2908 (let ((cash (or cache 'c-state-cache)))
2909 `(if (consp (car ,cash))
2910 (caar ,cash)
2911 (car ,cash))))
2913 (defmacro c-state-cache-top-paren (&optional cache)
2914 ;; Return the address of the latest brace/bracket/paren (whether left or
2915 ;; right) recorded in CACHE (default `c-state-cache') or nil.
2916 (let ((cash (or cache 'c-state-cache)))
2917 `(if (consp (car ,cash))
2918 (cdar ,cash)
2919 (car ,cash))))
2921 (defmacro c-state-cache-after-top-paren (&optional cache)
2922 ;; Return the position just after the latest brace/bracket/paren (whether
2923 ;; left or right) recorded in CACHE (default `c-state-cache') or nil.
2924 (let ((cash (or cache 'c-state-cache)))
2925 `(if (consp (car ,cash))
2926 (cdar ,cash)
2927 (and (car ,cash)
2928 (1+ (car ,cash))))))
2930 (defun c-get-cache-scan-pos (here)
2931 ;; From the state-cache, determine the buffer position from which we might
2932 ;; scan forward to HERE to update this cache. This position will be just
2933 ;; after a paren/brace/bracket recorded in the cache, if possible, otherwise
2934 ;; return the earliest position in the accessible region which isn't within
2935 ;; a literal. If the visible portion of the buffer is entirely within a
2936 ;; literal, return NIL.
2937 (let ((c c-state-cache) elt)
2938 ;(while (>= (or (c-state-cache-top-lparen c) 1) here)
2939 (while (and c
2940 (>= (c-state-cache-top-lparen c) here))
2941 (setq c (cdr c)))
2943 (setq elt (car c))
2944 (cond
2945 ((consp elt)
2946 (if (> (cdr elt) here)
2947 (1+ (car elt))
2948 (cdr elt)))
2949 (elt (1+ elt))
2950 ((<= (c-state-get-min-scan-pos) here)
2951 (c-state-get-min-scan-pos))
2952 (t nil))))
2954 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2955 ;; Variables which keep track of preprocessor constructs.
2956 (defvar c-state-old-cpp-beg-marker nil)
2957 (make-variable-buffer-local 'c-state-old-cpp-beg-marker)
2958 (defvar c-state-old-cpp-beg nil)
2959 (make-variable-buffer-local 'c-state-old-cpp-beg)
2960 (defvar c-state-old-cpp-end-marker nil)
2961 (make-variable-buffer-local 'c-state-old-cpp-end-marker)
2962 (defvar c-state-old-cpp-end nil)
2963 (make-variable-buffer-local 'c-state-old-cpp-end)
2964 ;; These are the limits of the macro containing point at the previous call of
2965 ;; `c-parse-state', or nil.
2967 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2968 ;; Defuns which analyze the buffer, yet don't change `c-state-cache'.
2969 (defun c-get-fallback-scan-pos (here)
2970 ;; Return a start position for building `c-state-cache' from
2971 ;; scratch. This will be at the top level, 2 defuns back.
2972 (save-excursion
2973 ;; Go back 2 bods, but ignore any bogus positions returned by
2974 ;; beginning-of-defun (i.e. open paren in column zero).
2975 (goto-char here)
2976 (let ((cnt 2))
2977 (while (not (or (bobp) (zerop cnt)))
2978 (c-beginning-of-defun-1) ; Pure elisp BOD.
2979 (if (eq (char-after) ?\{)
2980 (setq cnt (1- cnt)))))
2981 (point)))
2983 (defun c-state-balance-parens-backwards (here- here+ top)
2984 ;; Return the position of the opening paren/brace/bracket before HERE- which
2985 ;; matches the outermost close p/b/b between HERE+ and TOP. Except when
2986 ;; there's a macro, HERE- and HERE+ are the same. Like this:
2988 ;; ............................................
2989 ;; | |
2990 ;; ( [ ( .........#macro.. ) ( ) ] )
2991 ;; ^ ^ ^ ^
2992 ;; | | | |
2993 ;; return HERE- HERE+ TOP
2995 ;; If there aren't enough opening paren/brace/brackets, return the position
2996 ;; of the outermost one found, or HERE- if there are none. If there are no
2997 ;; closing p/b/bs between HERE+ and TOP, return HERE-. HERE-/+ and TOP
2998 ;; must not be inside literals. Only the accessible portion of the buffer
2999 ;; will be scanned.
3001 ;; PART 1: scan from `here+' up to `top', accumulating ")"s which enclose
3002 ;; `here'. Go round the next loop each time we pass over such a ")". These
3003 ;; probably match "("s before `here-'.
3004 (let (pos pa ren+1 lonely-rens)
3005 (save-excursion
3006 (save-restriction
3007 (narrow-to-region (point-min) top) ; This can move point, sometimes.
3008 (setq pos here+)
3009 (c-safe
3010 (while
3011 (setq ren+1 (c-sc-scan-lists pos 1 1)) ; might signal
3012 (setq lonely-rens (cons ren+1 lonely-rens)
3013 pos ren+1)))))
3015 ;; PART 2: Scan back before `here-' searching for the "("s
3016 ;; matching/mismatching the ")"s found above. We only need to direct the
3017 ;; caller to scan when we've encountered unmatched right parens.
3018 (setq pos here-)
3019 (when lonely-rens
3020 (c-safe
3021 (while
3022 (and lonely-rens ; actual values aren't used.
3023 (setq pa (c-sc-scan-lists pos -1 1)))
3024 (setq pos pa)
3025 (setq lonely-rens (cdr lonely-rens)))))
3026 pos))
3028 (defun c-parse-state-get-strategy (here good-pos)
3029 ;; Determine the scanning strategy for adjusting `c-parse-state', attempting
3030 ;; to minimize the amount of scanning. HERE is the pertinent position in
3031 ;; the buffer, GOOD-POS is a position where `c-state-cache' (possibly with
3032 ;; its head trimmed) is known to be good, or nil if there is no such
3033 ;; position.
3035 ;; The return value is a list, one of the following:
3037 ;; o - ('forward START-POINT) - scan forward from START-POINT,
3038 ;; which is not less than the highest position in `c-state-cache' below HERE,
3039 ;; which is after GOOD-POS.
3040 ;; o - ('backward nil) - scan backwards (from HERE).
3041 ;; o - ('back-and-forward START-POINT) - like 'forward, but when HERE is earlier
3042 ;; than GOOD-POS.
3043 ;; o - ('BOD START-POINT) - scan forwards from START-POINT, which is at the
3044 ;; top level.
3045 ;; o - ('IN-LIT nil) - point is inside the literal containing point-min.
3046 (let* ((in-macro-start ; start of macro containing HERE or nil.
3047 (save-excursion
3048 (goto-char here)
3049 (and (c-beginning-of-macro)
3050 (point))))
3051 (changed-macro-start
3052 (and in-macro-start
3053 (not (and c-state-old-cpp-beg
3054 (= in-macro-start c-state-old-cpp-beg)))
3055 in-macro-start))
3056 (cache-pos (c-get-cache-scan-pos (if changed-macro-start
3057 (min changed-macro-start here)
3058 here))) ; highest suitable position in cache (or 1)
3059 BOD-pos ; position of 2nd BOD before HERE.
3060 strategy ; 'forward, 'backward, 'BOD, or 'IN-LIT.
3061 start-point
3062 how-far) ; putative scanning distance.
3063 (setq good-pos (or good-pos (c-state-get-min-scan-pos)))
3064 (cond
3065 ((< here (c-state-get-min-scan-pos))
3066 (setq strategy 'IN-LIT
3067 start-point nil
3068 cache-pos nil
3069 how-far 0))
3070 ((<= good-pos here)
3071 (setq strategy 'forward
3072 start-point (if changed-macro-start
3073 cache-pos
3074 (max good-pos cache-pos))
3075 how-far (- here start-point)))
3076 ((< (- good-pos here) (- here cache-pos)) ; FIXME!!! ; apply some sort of weighting.
3077 (setq strategy 'backward
3078 how-far (- good-pos here)))
3080 (setq strategy 'back-and-forward
3081 start-point cache-pos
3082 how-far (- here start-point))))
3084 ;; Might we be better off starting from the top level, two defuns back,
3085 ;; instead? This heuristic no longer works well in C++, where
3086 ;; declarations inside namespace brace blocks are frequently placed at
3087 ;; column zero. (2015-11-10): Remove the condition on C++ Mode.
3088 (when (and (or (not (memq 'col-0-paren c-emacs-features))
3089 open-paren-in-column-0-is-defun-start)
3090 ;; (not (c-major-mode-is 'c++-mode))
3091 (> how-far c-state-cache-too-far))
3092 (setq BOD-pos (c-get-fallback-scan-pos here)) ; somewhat EXPENSIVE!!!
3093 (if (< (- here BOD-pos) how-far)
3094 (setq strategy 'BOD
3095 start-point BOD-pos)))
3097 (list strategy start-point)))
3100 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
3101 ;; Routines which change `c-state-cache' and associated values.
3102 (defun c-renarrow-state-cache ()
3103 ;; The region (more precisely, point-min) has changed since we
3104 ;; calculated `c-state-cache'. Amend `c-state-cache' accordingly.
3105 (if (< (point-min) c-state-point-min)
3106 ;; If point-min has MOVED BACKWARDS then we drop the state completely.
3107 ;; It would be possible to do a better job here and recalculate the top
3108 ;; only.
3109 (progn
3110 (c-state-mark-point-min-literal)
3111 (setq c-state-cache nil
3112 c-state-cache-good-pos c-state-min-scan-pos
3113 c-state-brace-pair-desert nil))
3115 ;; point-min has MOVED FORWARD.
3117 ;; Is the new point-min inside a (different) literal?
3118 (unless (and c-state-point-min-lit-start ; at prev. point-min
3119 (< (point-min) (c-state-get-min-scan-pos)))
3120 (c-state-mark-point-min-literal))
3122 ;; Cut off a bit of the tail from `c-state-cache'.
3123 (let ((ptr (cons nil c-state-cache))
3125 (while (and (setq pa (c-state-cache-top-lparen (cdr ptr)))
3126 (>= pa (point-min)))
3127 (setq ptr (cdr ptr)))
3129 (when (consp ptr)
3130 (if (or (eq (cdr ptr) c-state-cache)
3131 (and (consp (cadr ptr))
3132 (> (cdr (cadr ptr)) (point-min)))) ; Our new point-min is
3133 ; inside a recorded
3134 ; brace pair.
3135 (setq c-state-cache nil
3136 c-state-cache-good-pos c-state-min-scan-pos)
3137 (setcdr ptr nil)
3138 (setq c-state-cache-good-pos (1+ (c-state-cache-top-lparen))))
3141 (setq c-state-point-min (point-min)))
3143 (defun c-append-lower-brace-pair-to-state-cache (from here &optional upper-lim)
3144 ;; If there is a brace pair preceding FROM in the buffer, at the same level
3145 ;; of nesting (not necessarily immediately preceding), push a cons onto
3146 ;; `c-state-cache' to represent it. FROM must not be inside a literal. If
3147 ;; UPPER-LIM is non-nil, we append the highest brace pair whose "}" is below
3148 ;; UPPER-LIM.
3150 ;; Return non-nil when this has been done.
3152 ;; The situation it copes with is this transformation:
3154 ;; OLD: { (.) {...........}
3155 ;; ^ ^
3156 ;; FROM HERE
3158 ;; NEW: { {....} (.) {.........
3159 ;; ^ ^ ^
3160 ;; LOWER BRACE PAIR HERE or HERE
3162 ;; This routine should be fast. Since it can get called a LOT, we maintain
3163 ;; `c-state-brace-pair-desert', a small cache of "failures", such that we
3164 ;; reduce the time wasted in repeated fruitless searches in brace deserts.
3165 (save-excursion
3166 (save-restriction
3167 (let* (new-cons
3168 (cache-pos (c-state-cache-top-lparen)) ; might be nil.
3169 (macro-start-or-from
3170 (progn (goto-char from)
3171 (c-beginning-of-macro)
3172 (point)))
3173 (bra ; Position of "{".
3174 ;; Don't start scanning in the middle of a CPP construct unless
3175 ;; it contains HERE - these constructs, in Emacs, are "commented
3176 ;; out" with category properties.
3177 (if (eq (c-get-char-property macro-start-or-from 'category)
3178 'c-cpp-delimiter)
3179 macro-start-or-from
3180 from))
3181 ce) ; Position of "}"
3182 (or upper-lim (setq upper-lim from))
3184 ;; If we're essentially repeating a fruitless search, just give up.
3185 (unless (and c-state-brace-pair-desert
3186 (eq cache-pos (car c-state-brace-pair-desert))
3187 (or (null (car c-state-brace-pair-desert))
3188 (> from (car c-state-brace-pair-desert)))
3189 (<= from (cdr c-state-brace-pair-desert)))
3190 ;; DESERT-LIM. Avoid repeated searching through the cached desert.
3191 (let ((desert-lim
3192 (and c-state-brace-pair-desert
3193 (eq cache-pos (car c-state-brace-pair-desert))
3194 (>= from (cdr c-state-brace-pair-desert))
3195 (cdr c-state-brace-pair-desert)))
3196 ;; CACHE-LIM. This limit will be necessary when an opening
3197 ;; paren at `cache-pos' has just had its matching close paren
3198 ;; inserted into the buffer. `cache-pos' continues to be a
3199 ;; search bound, even though the algorithm below would skip
3200 ;; over the new paren pair.
3201 (cache-lim (and cache-pos (< cache-pos from) cache-pos)))
3202 (narrow-to-region
3203 (cond
3204 ((and desert-lim cache-lim)
3205 (max desert-lim cache-lim))
3206 (desert-lim)
3207 (cache-lim)
3208 ((point-min)))
3209 ;; The top limit is EOB to ensure that `bra' is inside the
3210 ;; accessible part of the buffer at the next scan operation.
3211 (1+ (buffer-size))))
3213 ;; In the next pair of nested loops, the inner one moves back past a
3214 ;; pair of (mis-)matching parens or brackets; the outer one moves
3215 ;; back over a sequence of unmatched close brace/paren/bracket each
3216 ;; time round.
3217 (while
3218 (progn
3219 (c-safe
3220 (while
3221 (and (setq ce (c-sc-scan-lists bra -1 -1)) ; back past )/]/}; might signal
3222 (setq bra (c-sc-scan-lists ce -1 1)) ; back past (/[/{; might signal
3223 (or (> bra here) ;(> ce here)
3224 (and
3225 (< ce here)
3226 (or (not (eq (char-after bra) ?\{))
3227 (and (goto-char bra)
3228 (c-beginning-of-macro)
3229 (< (point) macro-start-or-from))))))))
3230 (and ce (< ce bra)))
3231 (setq bra ce)) ; If we just backed over an unbalanced closing
3232 ; brace, ignore it.
3234 (if (and ce (< ce here) (< bra ce) (eq (char-after bra) ?\{))
3235 ;; We've found the desired brace-pair.
3236 (progn
3237 (setq new-cons (cons bra (1+ ce)))
3238 (cond
3239 ((consp (car c-state-cache))
3240 (setcar c-state-cache new-cons))
3241 ((and (numberp (car c-state-cache)) ; probably never happens
3242 (< ce (car c-state-cache)))
3243 (setcdr c-state-cache
3244 (cons new-cons (cdr c-state-cache))))
3245 (t (setq c-state-cache (cons new-cons c-state-cache)))))
3247 ;; We haven't found a brace pair. Record this in the cache.
3248 (setq c-state-brace-pair-desert
3249 (cons (if (and ce (< bra ce) (> ce here)) ; {..} straddling HERE?
3251 (point-min))
3252 (min here from)))))))))
3254 (defsubst c-state-push-any-brace-pair (bra+1 macro-start-or-here)
3255 ;; If BRA+1 is nil, do nothing. Otherwise, BRA+1 is the buffer position
3256 ;; following a {, and that brace has a (mis-)matching } (or ]), and we
3257 ;; "push" "a" brace pair onto `c-state-cache'.
3259 ;; Here "push" means overwrite the top element if it's itself a brace-pair,
3260 ;; otherwise push it normally.
3262 ;; The brace pair we push is normally the one surrounding BRA+1, but if the
3263 ;; latter is inside a macro, not being a macro containing
3264 ;; MACRO-START-OR-HERE, we scan backwards through the buffer for a non-macro
3265 ;; base pair. This latter case is assumed to be rare.
3267 ;; Note: POINT is not preserved in this routine.
3268 (if bra+1
3269 (if (or (> bra+1 macro-start-or-here)
3270 (progn (goto-char bra+1)
3271 (not (c-beginning-of-macro))))
3272 (setq c-state-cache
3273 (cons (cons (1- bra+1)
3274 (c-sc-scan-lists bra+1 1 1))
3275 (if (consp (car c-state-cache))
3276 (cdr c-state-cache)
3277 c-state-cache)))
3278 ;; N.B. This defsubst codes one method for the simple, normal case,
3279 ;; and a more sophisticated, slower way for the general case. Don't
3280 ;; eliminate this defsubst - it's a speed optimization.
3281 (c-append-lower-brace-pair-to-state-cache (1- bra+1) (point-max)))))
3283 (defun c-append-to-state-cache (from here)
3284 ;; Scan the buffer from FROM to HERE, adding elements into `c-state-cache'
3285 ;; for braces etc. Return a candidate for `c-state-cache-good-pos'.
3287 ;; FROM must be after the latest brace/paren/bracket in `c-state-cache', if
3288 ;; any. Typically, it is immediately after it. It must not be inside a
3289 ;; literal.
3290 (let ((here-bol (c-point 'bol here))
3291 (macro-start-or-here
3292 (save-excursion (goto-char here)
3293 (if (c-beginning-of-macro)
3294 (point)
3295 here)))
3296 pa+1 ; pos just after an opening PAren (or brace).
3297 (ren+1 from) ; usually a pos just after an closing paREN etc.
3298 ; Is actually the pos. to scan for a (/{/[ from,
3299 ; which sometimes is after a silly )/}/].
3300 paren+1 ; Pos after some opening or closing paren.
3301 paren+1s ; A list of `paren+1's; used to determine a
3302 ; good-pos.
3303 bra+1 ; just after L bra-ce.
3304 mstart) ; start of a macro.
3306 (save-excursion
3307 (save-restriction
3308 (narrow-to-region (point-min) here)
3309 ;; Each time round the following loop, we enter a successively deeper
3310 ;; level of brace/paren nesting. (Except sometimes we "continue at
3311 ;; the existing level".) `pa+1' is a pos inside an opening
3312 ;; brace/paren/bracket, usually just after it.
3313 (while
3314 (progn
3315 ;; Each time round the next loop moves forward over an opening then
3316 ;; a closing brace/bracket/paren. This loop is white hot, so it
3317 ;; plays ugly tricks to go fast. DON'T PUT ANYTHING INTO THIS
3318 ;; LOOP WHICH ISN'T ABSOLUTELY NECESSARY!!! It terminates when a
3319 ;; call of `scan-lists' signals an error, which happens when there
3320 ;; are no more b/b/p's to scan.
3321 (c-safe
3322 (while t
3323 (setq pa+1 (c-sc-scan-lists ren+1 1 -1) ; Into (/{/[; might signal
3324 paren+1s (cons pa+1 paren+1s))
3325 (setq ren+1 (c-sc-scan-lists pa+1 1 1)) ; Out of )/}/]; might signal
3326 (if (and (eq (char-before pa+1) ?{)) ; Check for a macro later.
3327 (setq bra+1 pa+1))
3328 (setcar paren+1s ren+1)))
3330 (if (and pa+1 (> pa+1 ren+1))
3331 ;; We've just entered a deeper nesting level.
3332 (progn
3333 ;; Insert the brace pair (if present) and the single open
3334 ;; paren/brace/bracket into `c-state-cache' It cannot be
3335 ;; inside a macro, except one around point, because of what
3336 ;; `c-neutralize-syntax-in-CPP' has done.
3337 (c-state-push-any-brace-pair bra+1 macro-start-or-here)
3338 ;; Insert the opening brace/bracket/paren position.
3339 (setq c-state-cache (cons (1- pa+1) c-state-cache))
3340 ;; Clear admin stuff for the next more nested part of the scan.
3341 (setq ren+1 pa+1 pa+1 nil bra+1 nil)
3342 t) ; Carry on the loop
3344 ;; All open p/b/b's at this nesting level, if any, have probably
3345 ;; been closed by matching/mismatching ones. We're probably
3346 ;; finished - we just need to check for having found an
3347 ;; unmatched )/}/], which we ignore. Such a )/}/] can't be in a
3348 ;; macro, due the action of `c-neutralize-syntax-in-CPP'.
3349 (c-safe (setq ren+1 (c-sc-scan-lists ren+1 1 1)))))) ; acts as loop control.
3351 ;; Record the final, innermost, brace-pair if there is one.
3352 (c-state-push-any-brace-pair bra+1 macro-start-or-here)
3354 ;; Determine a good pos
3355 (while (and (setq paren+1 (car paren+1s))
3356 (> (if (> paren+1 macro-start-or-here)
3357 paren+1
3358 (goto-char paren+1)
3359 (setq mstart (and (c-beginning-of-macro)
3360 (point)))
3361 (or mstart paren+1))
3362 here-bol))
3363 (setq paren+1s (cdr paren+1s)))
3364 (cond
3365 ((and paren+1 mstart)
3366 (min paren+1 mstart))
3367 (paren+1)
3368 (t from))))))
3370 (defun c-remove-stale-state-cache (start-point here pps-point)
3371 ;; Remove stale entries from the `c-cache-state', i.e. those which will
3372 ;; not be in it when it is amended for position HERE. This may involve
3373 ;; replacing a CONS element for a brace pair containing HERE with its car.
3374 ;; Additionally, the "outermost" open-brace entry before HERE will be
3375 ;; converted to a cons if the matching close-brace is below HERE.
3377 ;; START-POINT is a "maximal" "safe position" - there must be no open
3378 ;; parens/braces/brackets between START-POINT and HERE.
3380 ;; As a second thing, calculate the result of parse-partial-sexp at
3381 ;; PPS-POINT, w.r.t. START-POINT. The motivation here is that
3382 ;; `c-state-cache-good-pos' may become PPS-POINT, but the caller may need to
3383 ;; adjust it to get outside a string/comment. (Sorry about this! The code
3384 ;; needs to be FAST).
3386 ;; Return a list (GOOD-POS SCAN-BACK-POS CONS-SEPARATED PPS-STATE), where
3387 ;; o - GOOD-POS is a position where the new value `c-state-cache' is known
3388 ;; to be good (we aim for this to be as high as possible);
3389 ;; o - SCAN-BACK-POS, if not nil, indicates there may be a brace pair
3390 ;; preceding POS which needs to be recorded in `c-state-cache'. It is a
3391 ;; position to scan backwards from. It is the position of the "{" of the
3392 ;; last element to be removed from `c-state-cache', when that elt is a
3393 ;; cons, otherwise nil.
3394 ;; o - CONS-SEPARATED is t when a cons element in `c-state-cache' has been
3395 ;; replaced by its car because HERE lies inside the brace pair represented
3396 ;; by the cons.
3397 ;; o - PPS-STATE is the parse-partial-sexp state at PPS-POINT.
3398 (save-excursion
3399 (save-restriction
3400 (narrow-to-region 1 (point-max))
3401 (let* ((in-macro-start ; start of macro containing HERE or nil.
3402 (save-excursion
3403 (goto-char here)
3404 (and (c-beginning-of-macro)
3405 (point))))
3406 (start-point-actual-macro-start ; Start of macro containing
3407 ; start-point or nil
3408 (and (< start-point here)
3409 (save-excursion
3410 (goto-char start-point)
3411 (and (c-beginning-of-macro)
3412 (point)))))
3413 (start-point-actual-macro-end ; End of this macro, (maybe
3414 ; HERE), or nil.
3415 (and start-point-actual-macro-start
3416 (save-excursion
3417 (goto-char start-point-actual-macro-start)
3418 (c-end-of-macro)
3419 (point))))
3420 pps-state ; Will be 9 or 10 elements long.
3422 upper-lim ; ,beyond which `c-state-cache' entries are removed
3423 scan-back-pos
3424 cons-separated
3425 pair-beg target-depth)
3427 ;; Remove entries beyond HERE. Also remove any entries inside
3428 ;; a macro, unless HERE is in the same macro.
3429 (setq upper-lim
3430 (if (or (null c-state-old-cpp-beg)
3431 (and (> here c-state-old-cpp-beg)
3432 (< here c-state-old-cpp-end)))
3433 here
3434 (min here c-state-old-cpp-beg)))
3435 (while (and c-state-cache (>= (c-state-cache-top-lparen) upper-lim))
3436 (setq scan-back-pos (car-safe (car c-state-cache)))
3437 (setq c-state-cache (cdr c-state-cache)))
3439 ;; If `upper-lim' is inside the last recorded brace pair, remove its
3440 ;; RBrace and indicate we'll need to search backwards for a previous
3441 ;; brace pair.
3442 (when (and c-state-cache
3443 (consp (car c-state-cache))
3444 (> (cdar c-state-cache) upper-lim))
3445 (setcar c-state-cache (caar c-state-cache))
3446 (setq scan-back-pos (car c-state-cache)
3447 cons-separated t))
3449 ;; The next loop jumps forward out of a nested level of parens each
3450 ;; time round; the corresponding elements in `c-state-cache' are
3451 ;; removed. `pos' is just after the brace-pair or the open paren at
3452 ;; (car c-state-cache). There can be no open parens/braces/brackets
3453 ;; between `start-point'/`start-point-actual-macro-start' and HERE,
3454 ;; due to the interface spec to this function.
3455 (setq pos (if (and start-point-actual-macro-end
3456 (not (eq start-point-actual-macro-start
3457 in-macro-start)))
3458 (1+ start-point-actual-macro-end) ; get outside the macro as
3459 ; marked by a `category' text property.
3460 start-point))
3461 (goto-char pos)
3462 (while (and c-state-cache
3463 (or (numberp (car c-state-cache)) ; Have we a { at all?
3464 (cdr c-state-cache))
3465 (< (point) here))
3466 (cond
3467 ((null pps-state) ; first time through
3468 (setq target-depth -1))
3469 ((eq (car pps-state) target-depth) ; found closing ),},]
3470 (setq target-depth (1- (car pps-state))))
3471 ;; Do nothing when we've merely reached pps-point.
3474 ;; Scan!
3475 (setq pps-state
3476 (c-sc-parse-partial-sexp
3477 (point) (if (< (point) pps-point) pps-point here)
3478 target-depth
3479 nil pps-state))
3481 (when (eq (car pps-state) target-depth)
3482 (setq pos (point)) ; POS is now just after an R-paren/brace.
3483 (cond
3484 ((and (consp (car c-state-cache))
3485 (eq (point) (cdar c-state-cache)))
3486 ;; We've just moved out of the paren pair containing the brace-pair
3487 ;; at (car c-state-cache). `pair-beg' is where the open paren is,
3488 ;; and is potentially where the open brace of a cons in
3489 ;; c-state-cache will be.
3490 (setq pair-beg (car-safe (cdr c-state-cache))
3491 c-state-cache (cdr-safe (cdr c-state-cache)))) ; remove {}pair + containing Lparen.
3492 ((numberp (car c-state-cache))
3493 (setq pair-beg (car c-state-cache)
3494 c-state-cache (cdr c-state-cache))) ; remove this
3495 ; containing Lparen
3496 ((numberp (cadr c-state-cache))
3497 (setq pair-beg (cadr c-state-cache)
3498 c-state-cache (cddr c-state-cache))) ; Remove a paren pair
3499 ; together with enclosed brace pair.
3500 ;; (t nil) ; Ignore an unmated Rparen.
3503 (if (< (point) pps-point)
3504 (setq pps-state (c-sc-parse-partial-sexp
3505 (point) pps-point
3506 nil nil ; TARGETDEPTH, STOPBEFORE
3507 pps-state)))
3509 ;; If the last paren pair we moved out of was actually a brace pair,
3510 ;; insert it into `c-state-cache'.
3511 (when (and pair-beg (eq (char-after pair-beg) ?{))
3512 (if (consp (car-safe c-state-cache))
3513 (setq c-state-cache (cdr c-state-cache)))
3514 (setq c-state-cache (cons (cons pair-beg pos)
3515 c-state-cache)))
3517 (list pos scan-back-pos cons-separated pps-state)))))
3519 (defun c-remove-stale-state-cache-backwards (here)
3520 ;; Strip stale elements of `c-state-cache' by moving backwards through the
3521 ;; buffer, and inform the caller of the scenario detected.
3523 ;; HERE is the position we're setting `c-state-cache' for.
3524 ;; CACHE-POS (a locally bound variable) is just after the latest recorded
3525 ;; position in `c-state-cache' before HERE, or a position at or near
3526 ;; point-min which isn't in a literal.
3528 ;; This function must only be called only when (> `c-state-cache-good-pos'
3529 ;; HERE). Usually the gap between CACHE-POS and HERE is large. It is thus
3530 ;; optimized to eliminate (or minimize) scanning between these two
3531 ;; positions.
3533 ;; Return a three element list (GOOD-POS SCAN-BACK-POS FWD-FLAG), where:
3534 ;; o - GOOD-POS is a "good position", where `c-state-cache' is valid, or
3535 ;; could become so after missing elements are inserted into
3536 ;; `c-state-cache'. This is JUST AFTER an opening or closing
3537 ;; brace/paren/bracket which is already in `c-state-cache' or just before
3538 ;; one otherwise. exceptionally (when there's no such b/p/b handy) the BOL
3539 ;; before `here''s line, or the start of the literal containing it.
3540 ;; o - SCAN-BACK-POS, if non-nil, indicates there may be a brace pair
3541 ;; preceding POS which isn't recorded in `c-state-cache'. It is a position
3542 ;; to scan backwards from.
3543 ;; o - FWD-FLAG, if non-nil, indicates there may be parens/braces between
3544 ;; POS and HERE which aren't recorded in `c-state-cache'.
3546 ;; The comments in this defun use "paren" to mean parenthesis or square
3547 ;; bracket (as contrasted with a brace), and "(" and ")" likewise.
3549 ;; . {..} (..) (..) ( .. { } ) (...) ( .... . ..)
3550 ;; | | | | | |
3551 ;; CP E here D C good
3552 (let ((cache-pos (c-get-cache-scan-pos here)) ; highest position below HERE in cache (or 1)
3553 (pos c-state-cache-good-pos)
3554 pa ren ; positions of "(" and ")"
3555 dropped-cons ; whether the last element dropped from `c-state-cache'
3556 ; was a cons (representing a brace-pair)
3557 good-pos ; see above.
3558 lit ; (START . END) of a literal containing some point.
3559 here-lit-start here-lit-end ; bounds of literal containing `here'
3560 ; or `here' itself.
3561 here- here+ ; start/end of macro around HERE, or HERE
3562 (here-bol (c-point 'bol here))
3563 (too-far-back (max (- here c-state-cache-too-far) (point-min))))
3565 ;; Remove completely irrelevant entries from `c-state-cache'.
3566 (while (and c-state-cache
3567 (>= (setq pa (c-state-cache-top-lparen)) here))
3568 (setq dropped-cons (consp (car c-state-cache)))
3569 (setq c-state-cache (cdr c-state-cache))
3570 (setq pos pa))
3571 ;; At this stage, (>= pos here);
3572 ;; (< (c-state-cache-top-lparen) here) (or is nil).
3574 (cond
3575 ((and (consp (car c-state-cache))
3576 (> (cdar c-state-cache) here))
3577 ;; CASE 1: The top of the cache is a brace pair which now encloses
3578 ;; `here'. As good-pos, return the address of the "{". Since we've no
3579 ;; knowledge of what's inside these braces, we have no alternative but
3580 ;; to direct the caller to scan the buffer from the opening brace.
3581 (setq pos (caar c-state-cache))
3582 (setcar c-state-cache pos)
3583 (list (1+ pos) pos t)) ; return value. We've just converted a brace pair
3584 ; entry into a { entry, so the caller needs to
3585 ; search for a brace pair before the {.
3587 ;; `here' might be inside a literal. Check for this.
3588 ((progn
3589 (setq lit (c-state-literal-at here)
3590 here-lit-start (or (car lit) here)
3591 here-lit-end (or (cdr lit) here))
3592 ;; Has `here' just "newly entered" a macro?
3593 (save-excursion
3594 (goto-char here-lit-start)
3595 (if (and (c-beginning-of-macro)
3596 (or (null c-state-old-cpp-beg)
3597 (not (= (point) c-state-old-cpp-beg))))
3598 (progn
3599 (setq here- (point))
3600 (c-end-of-macro)
3601 (setq here+ (point)))
3602 (setq here- here-lit-start
3603 here+ here-lit-end)))
3605 ;; `here' might be nested inside any depth of parens (or brackets but
3606 ;; not braces). Scan backwards to find the outermost such opening
3607 ;; paren, if there is one. This will be the scan position to return.
3608 (save-restriction
3609 (narrow-to-region cache-pos (point-max))
3610 (setq pos (c-state-balance-parens-backwards here- here+ pos)))
3611 nil)) ; for the cond
3613 ((< pos here-lit-start)
3614 ;; CASE 2: Address of outermost ( or [ which now encloses `here', but
3615 ;; didn't enclose the (previous) `c-state-cache-good-pos'. If there is
3616 ;; a brace pair preceding this, it will already be in `c-state-cache',
3617 ;; unless there was a brace pair after it, i.e. there'll only be one to
3618 ;; scan for if we've just deleted one.
3619 (list pos (and dropped-cons pos) t)) ; Return value.
3621 ;; `here' isn't enclosed in a (previously unrecorded) bracket/paren.
3622 ;; Further forward scanning isn't needed, but we still need to find a
3623 ;; GOOD-POS. Step out of all enclosing "("s on HERE's line.
3624 ((progn
3625 (save-restriction
3626 (narrow-to-region here-bol (point-max))
3627 (setq pos here-lit-start)
3628 (c-safe (while (setq pa (c-sc-scan-lists pos -1 1))
3629 (setq pos pa)))) ; might signal
3630 nil)) ; for the cond
3632 ((save-restriction
3633 (narrow-to-region too-far-back (point-max))
3634 (setq ren (c-safe (c-sc-scan-lists pos -1 -1))))
3635 ;; CASE 3: After a }/)/] before `here''s BOL.
3636 (list (1+ ren) (and dropped-cons pos) nil)) ; Return value
3638 ((progn (setq good-pos (c-state-lit-beg (c-point 'bopl here-bol)))
3639 (>= cache-pos good-pos))
3640 ;; CASE 3.5: Just after an existing entry in `c-state-cache' on `here''s
3641 ;; line or the previous line.
3642 (list cache-pos nil nil))
3645 ;; CASE 4; Best of a bad job: BOL before `here-bol', or beginning of
3646 ;; literal containing it.
3647 (list good-pos (and dropped-cons good-pos) nil)))))
3650 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
3651 ;; Externally visible routines.
3653 (defun c-state-cache-init ()
3654 (setq c-state-cache nil
3655 c-state-cache-good-pos 1
3656 c-state-nonlit-pos-cache nil
3657 c-state-nonlit-pos-cache-limit 1
3658 c-state-semi-nonlit-pos-cache nil
3659 c-state-semi-nonlit-pos-cache-limit 1
3660 c-state-brace-pair-desert nil
3661 c-state-point-min 1
3662 c-state-point-min-lit-type nil
3663 c-state-point-min-lit-start nil
3664 c-state-min-scan-pos 1
3665 c-state-old-cpp-beg nil
3666 c-state-old-cpp-end nil)
3667 (c-state-mark-point-min-literal))
3669 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
3670 ;; Debugging routines to dump `c-state-cache' in a "replayable" form.
3671 ;; (defmacro c-sc-de (elt) ; "c-state-cache-dump-element"
3672 ;; `(format ,(concat "(setq " (symbol-name elt) " %s) ") ,elt))
3673 ;; (defmacro c-sc-qde (elt) ; "c-state-cache-quote-dump-element"
3674 ;; `(format ,(concat "(setq " (symbol-name elt) " '%s) ") ,elt))
3675 ;; (defun c-state-dump ()
3676 ;; ;; For debugging.
3677 ;; ;(message
3678 ;; (concat
3679 ;; (c-sc-qde c-state-cache)
3680 ;; (c-sc-de c-state-cache-good-pos)
3681 ;; (c-sc-qde c-state-nonlit-pos-cache)
3682 ;; (c-sc-de c-state-nonlit-pos-cache-limit)
3683 ;; (c-sc-qde c-state-brace-pair-desert)
3684 ;; (c-sc-de c-state-point-min)
3685 ;; (c-sc-de c-state-point-min-lit-type)
3686 ;; (c-sc-de c-state-point-min-lit-start)
3687 ;; (c-sc-de c-state-min-scan-pos)
3688 ;; (c-sc-de c-state-old-cpp-beg)
3689 ;; (c-sc-de c-state-old-cpp-end)))
3690 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
3692 (defun c-invalidate-state-cache-1 (here)
3693 ;; Invalidate all info on `c-state-cache' that applies to the buffer at HERE
3694 ;; or higher and set `c-state-cache-good-pos' accordingly. The cache is
3695 ;; left in a consistent state.
3697 ;; This is much like `c-whack-state-after', but it never changes a paren
3698 ;; pair element into an open paren element. Doing that would mean that the
3699 ;; new open paren wouldn't have the required preceding paren pair element.
3701 ;; This function is called from c-before-change.
3703 ;; The caches of non-literals:
3704 ;; Note that we use "<=" for the possibility of the second char of a two-char
3705 ;; comment opener being typed; this would invalidate any cache position at
3706 ;; HERE.
3707 (if (<= here c-state-nonlit-pos-cache-limit)
3708 (setq c-state-nonlit-pos-cache-limit (1- here)))
3709 (c-truncate-semi-nonlit-pos-cache here)
3711 ;; `c-state-cache':
3712 ;; Case 1: if `here' is in a literal containing point-min, everything
3713 ;; becomes (or is already) nil.
3714 (if (or (null c-state-cache-good-pos)
3715 (< here (c-state-get-min-scan-pos)))
3716 (setq c-state-cache nil
3717 c-state-cache-good-pos nil
3718 c-state-min-scan-pos nil)
3720 ;; Truncate `c-state-cache' and set `c-state-cache-good-pos' to a value
3721 ;; below `here'. To maintain its consistency, we may need to insert a new
3722 ;; brace pair.
3723 (let ((here-bol (c-point 'bol here))
3724 too-high-pa ; recorded {/(/[ next above or just below here, or nil.
3725 dropped-cons) ; was the last removed element a brace pair?
3726 ;; The easy bit - knock over-the-top bits off `c-state-cache'.
3727 (while (and c-state-cache
3728 (>= (c-state-cache-top-paren) here))
3729 (setq dropped-cons (consp (car c-state-cache))
3730 too-high-pa (c-state-cache-top-lparen)
3731 c-state-cache (cdr c-state-cache)))
3733 ;; Do we need to add in an earlier brace pair, having lopped one off?
3734 (if (and dropped-cons
3735 (<= too-high-pa here))
3736 (c-append-lower-brace-pair-to-state-cache too-high-pa here here-bol))
3737 (setq c-state-cache-good-pos (or (c-state-cache-after-top-paren)
3738 (c-state-get-min-scan-pos)))))
3740 ;; The brace-pair desert marker:
3741 (when (car c-state-brace-pair-desert)
3742 (if (< here (car c-state-brace-pair-desert))
3743 (setq c-state-brace-pair-desert nil)
3744 (if (< here (cdr c-state-brace-pair-desert))
3745 (setcdr c-state-brace-pair-desert here)))))
3747 (defun c-parse-state-1 ()
3748 ;; Find and record all noteworthy parens between some good point earlier in
3749 ;; the file and point. That good point is at least the beginning of the
3750 ;; top-level construct we are in, or the beginning of the preceding
3751 ;; top-level construct if we aren't in one.
3753 ;; The returned value is a list of the noteworthy parens with the last one
3754 ;; first. If an element in the list is an integer, it's the position of an
3755 ;; open paren (of any type) which has not been closed before the point. If
3756 ;; an element is a cons, it gives the position of a closed BRACE paren
3757 ;; pair[*]; the car is the start brace position and the cdr is the position
3758 ;; following the closing brace. Only the last closed brace paren pair
3759 ;; before each open paren and before the point is recorded, and thus the
3760 ;; state never contains two cons elements in succession. When a close brace
3761 ;; has no matching open brace (e.g., the matching brace is outside the
3762 ;; visible region), it is not represented in the returned value.
3764 ;; [*] N.B. The close "brace" might be a mismatching close bracket or paren.
3765 ;; This defun explicitly treats mismatching parens/braces/brackets as
3766 ;; matching. It is the open brace which makes it a "brace" pair.
3768 ;; If POINT is within a macro, open parens and brace pairs within
3769 ;; THIS macro MIGHT be recorded. This depends on whether their
3770 ;; syntactic properties have been suppressed by
3771 ;; `c-neutralize-syntax-in-CPP'. This might need fixing (2008-12-11).
3773 ;; Currently no characters which are given paren syntax with the
3774 ;; syntax-table property are recorded, i.e. angle bracket arglist
3775 ;; parens are never present here. Note that this might change.
3777 ;; BUG: This function doesn't cope entirely well with unbalanced
3778 ;; parens in macros. (2008-12-11: this has probably been resolved
3779 ;; by the function `c-neutralize-syntax-in-CPP'.) E.g. in the
3780 ;; following case the brace before the macro isn't balanced with the
3781 ;; one after it:
3783 ;; {
3784 ;; #define X {
3785 ;; }
3787 ;; Note to maintainers: this function DOES get called with point
3788 ;; within comments and strings, so don't assume it doesn't!
3790 ;; This function might do hidden buffer changes.
3791 (let* ((here (point))
3792 (here-bopl (c-point 'bopl))
3793 strategy ; 'forward, 'backward etc..
3794 ;; Candidate positions to start scanning from:
3795 cache-pos ; highest position below HERE already existing in
3796 ; cache (or 1).
3797 good-pos
3798 start-point ; (when scanning forward) a place below HERE where there
3799 ; are no open parens/braces between it and HERE.
3800 bopl-state
3802 cons-separated
3803 scan-backward-pos scan-forward-p) ; used for 'backward.
3804 ;; If POINT-MIN has changed, adjust the cache
3805 (unless (= (point-min) c-state-point-min)
3806 (c-renarrow-state-cache))
3808 ;; Strategy?
3809 (setq res (c-parse-state-get-strategy here c-state-cache-good-pos)
3810 strategy (car res)
3811 start-point (cadr res))
3813 (when (eq strategy 'BOD)
3814 (setq c-state-cache nil
3815 c-state-cache-good-pos start-point))
3817 ;; SCAN!
3818 (cond
3819 ((memq strategy '(forward back-and-forward BOD))
3820 (setq res (c-remove-stale-state-cache start-point here here-bopl))
3821 (setq cache-pos (car res)
3822 scan-backward-pos (cadr res)
3823 cons-separated (car (cddr res))
3824 bopl-state (cadr (cddr res))) ; will be nil if (< here-bopl
3825 ; start-point)
3826 (if (and scan-backward-pos
3827 (or cons-separated (eq strategy 'forward))) ;scan-backward-pos
3828 (c-append-lower-brace-pair-to-state-cache scan-backward-pos here))
3829 (setq good-pos
3830 (c-append-to-state-cache cache-pos here))
3831 (setq c-state-cache-good-pos
3832 (if (and bopl-state
3833 (< good-pos (- here c-state-cache-too-far)))
3834 (c-state-cache-non-literal-place here-bopl bopl-state)
3835 good-pos)))
3837 ((eq strategy 'backward)
3838 (setq res (c-remove-stale-state-cache-backwards here)
3839 good-pos (car res)
3840 scan-backward-pos (cadr res)
3841 scan-forward-p (car (cddr res)))
3842 (if scan-backward-pos
3843 (c-append-lower-brace-pair-to-state-cache scan-backward-pos here))
3844 (setq c-state-cache-good-pos
3845 (if scan-forward-p
3846 (c-append-to-state-cache good-pos here)
3847 good-pos)))
3849 (t ; (eq strategy 'IN-LIT)
3850 (setq c-state-cache nil
3851 c-state-cache-good-pos nil))))
3853 c-state-cache)
3855 (defun c-invalidate-state-cache (here)
3856 ;; This is a wrapper over `c-invalidate-state-cache-1'.
3858 ;; It suppresses the syntactic effect of the < and > (template) brackets and
3859 ;; of all parens in preprocessor constructs, except for any such construct
3860 ;; containing point. We can then call `c-invalidate-state-cache-1' without
3861 ;; worrying further about macros and template delimiters.
3862 (if (eval-when-compile (memq 'category-properties c-emacs-features))
3863 ;; Emacs
3864 (c-with-<->-as-parens-suppressed
3865 (if (and c-state-old-cpp-beg
3866 (< c-state-old-cpp-beg here))
3867 (c-with-all-but-one-cpps-commented-out
3868 c-state-old-cpp-beg
3869 c-state-old-cpp-end
3870 (c-invalidate-state-cache-1 here))
3871 (c-with-cpps-commented-out
3872 (c-invalidate-state-cache-1 here))))
3873 ;; XEmacs
3874 (c-invalidate-state-cache-1 here)))
3876 (defmacro c-state-maybe-marker (place marker)
3877 ;; If PLACE is non-nil, return a marker marking it, otherwise nil.
3878 ;; We (re)use MARKER.
3879 `(and ,place
3880 (or ,marker (setq ,marker (make-marker)))
3881 (set-marker ,marker ,place)))
3883 (defun c-parse-state ()
3884 ;; This is a wrapper over `c-parse-state-1'. See that function for a
3885 ;; description of the functionality and return value.
3887 ;; It suppresses the syntactic effect of the < and > (template) brackets and
3888 ;; of all parens in preprocessor constructs, except for any such construct
3889 ;; containing point. We can then call `c-parse-state-1' without worrying
3890 ;; further about macros and template delimiters.
3891 (let (here-cpp-beg here-cpp-end)
3892 (save-excursion
3893 (when (c-beginning-of-macro)
3894 (setq here-cpp-beg (point))
3895 (unless
3896 (> (setq here-cpp-end (c-syntactic-end-of-macro))
3897 here-cpp-beg)
3898 (setq here-cpp-beg nil here-cpp-end nil))))
3899 ;; FIXME!!! Put in a `condition-case' here to protect the integrity of the
3900 ;; subsystem.
3901 (prog1
3902 (if (eval-when-compile (memq 'category-properties c-emacs-features))
3903 ;; Emacs
3904 (c-with-<->-as-parens-suppressed
3905 (if (and here-cpp-beg (> here-cpp-end here-cpp-beg))
3906 (c-with-all-but-one-cpps-commented-out
3907 here-cpp-beg here-cpp-end
3908 (c-parse-state-1))
3909 (c-with-cpps-commented-out
3910 (c-parse-state-1))))
3911 ;; XEmacs
3912 (c-parse-state-1))
3913 (setq c-state-old-cpp-beg
3914 (c-state-maybe-marker here-cpp-beg c-state-old-cpp-beg-marker)
3915 c-state-old-cpp-end
3916 (c-state-maybe-marker here-cpp-end c-state-old-cpp-end-marker)))))
3918 ;; Debug tool to catch cache inconsistencies. This is called from
3919 ;; 000tests.el.
3920 (defvar c-debug-parse-state nil)
3921 (unless (fboundp 'c-real-parse-state)
3922 (fset 'c-real-parse-state (symbol-function 'c-parse-state)))
3923 (cc-bytecomp-defun c-real-parse-state)
3925 (defvar c-parse-state-point nil)
3926 (defvar c-parse-state-state nil)
3927 (make-variable-buffer-local 'c-parse-state-state)
3928 (defun c-record-parse-state-state ()
3929 (setq c-parse-state-point (point))
3930 (when (markerp (cdr (assq 'c-state-old-cpp-beg c-parse-state-state)))
3931 (move-marker (cdr (assq 'c-state-old-cpp-beg c-parse-state-state)) nil)
3932 (move-marker (cdr (assq 'c-state-old-cpp-end c-parse-state-state)) nil))
3933 (setq c-parse-state-state
3934 (mapcar
3935 (lambda (arg)
3936 (let ((val (symbol-value arg)))
3937 (cons arg
3938 (cond ((consp val) (copy-tree val))
3939 ((markerp val) (copy-marker val))
3940 (t val)))))
3941 '(c-state-cache
3942 c-state-cache-good-pos
3943 c-state-nonlit-pos-cache
3944 c-state-nonlit-pos-cache-limit
3945 c-state-semi-nonlit-pos-cache
3946 c-state-semi-nonlit-pos-cache-limit
3947 c-state-brace-pair-desert
3948 c-state-point-min
3949 c-state-point-min-lit-type
3950 c-state-point-min-lit-start
3951 c-state-min-scan-pos
3952 c-state-old-cpp-beg
3953 c-state-old-cpp-end
3954 c-parse-state-point))))
3955 (defun c-replay-parse-state-state ()
3956 (message "%s"
3957 (concat "(setq "
3958 (mapconcat
3959 (lambda (arg)
3960 (format "%s %s%s" (car arg)
3961 (if (atom (cdr arg)) "" "'")
3962 (if (markerp (cdr arg))
3963 (format "(copy-marker %s)" (marker-position (cdr arg)))
3964 (cdr arg))))
3965 c-parse-state-state " ")
3966 ")")))
3968 (defun c-debug-parse-state-double-cons (state)
3969 (let (state-car conses-not-ok)
3970 (while state
3971 (setq state-car (car state)
3972 state (cdr state))
3973 (if (and (consp state-car)
3974 (consp (car state)))
3975 (setq conses-not-ok t)))
3976 conses-not-ok))
3978 (defun c-debug-parse-state ()
3979 (let ((here (point)) (min-point (point-min)) (res1 (c-real-parse-state)) res2)
3980 (let ((c-state-cache nil)
3981 (c-state-cache-good-pos 1)
3982 (c-state-nonlit-pos-cache nil)
3983 (c-state-nonlit-pos-cache-limit 1)
3984 (c-state-brace-pair-desert nil)
3985 (c-state-point-min 1)
3986 (c-state-point-min-lit-type nil)
3987 (c-state-point-min-lit-start nil)
3988 (c-state-min-scan-pos 1)
3989 (c-state-old-cpp-beg nil)
3990 (c-state-old-cpp-end nil))
3991 (setq res2 (c-real-parse-state)))
3992 (unless (equal res1 res2)
3993 ;; The cache can actually go further back due to the ad-hoc way
3994 ;; the first paren is found, so try to whack off a bit of its
3995 ;; start before complaining.
3996 ;; (save-excursion
3997 ;; (goto-char (or (c-least-enclosing-brace res2) (point)))
3998 ;; (c-beginning-of-defun-1)
3999 ;; (while (not (or (bobp) (eq (char-after) ?{)))
4000 ;; (c-beginning-of-defun-1))
4001 ;; (unless (equal (c-whack-state-before (point) res1) res2)
4002 ;; (message (concat "c-parse-state inconsistency at %s: "
4003 ;; "using cache: %s, from scratch: %s")
4004 ;; here res1 res2)))
4005 (message (concat "c-parse-state inconsistency at %s: "
4006 "using cache: %s, from scratch: %s. POINT-MIN: %s")
4007 here res1 res2 min-point)
4008 (message "Old state:")
4009 (c-replay-parse-state-state))
4011 (when (c-debug-parse-state-double-cons res1)
4012 (message "c-parse-state INVALIDITY at %s: %s"
4013 here res1)
4014 (message "Old state:")
4015 (c-replay-parse-state-state))
4017 (c-record-parse-state-state)
4018 res2 ; res1 correct a cascading series of errors ASAP
4021 (defun c-toggle-parse-state-debug (&optional arg)
4022 (interactive "P")
4023 (setq c-debug-parse-state (c-calculate-state arg c-debug-parse-state))
4024 (fset 'c-parse-state (symbol-function (if c-debug-parse-state
4025 'c-debug-parse-state
4026 'c-real-parse-state)))
4027 (c-keep-region-active)
4028 (message "c-debug-parse-state %sabled"
4029 (if c-debug-parse-state "en" "dis")))
4030 (when c-debug-parse-state
4031 (c-toggle-parse-state-debug 1))
4034 (defun c-whack-state-before (bufpos paren-state)
4035 ;; Whack off any state information from PAREN-STATE which lies
4036 ;; before BUFPOS. Not destructive on PAREN-STATE.
4037 (let* ((newstate (list nil))
4038 (ptr newstate)
4039 car)
4040 (while paren-state
4041 (setq car (car paren-state)
4042 paren-state (cdr paren-state))
4043 (if (< (if (consp car) (car car) car) bufpos)
4044 (setq paren-state nil)
4045 (setcdr ptr (list car))
4046 (setq ptr (cdr ptr))))
4047 (cdr newstate)))
4049 (defun c-whack-state-after (bufpos paren-state)
4050 ;; Whack off any state information from PAREN-STATE which lies at or
4051 ;; after BUFPOS. Not destructive on PAREN-STATE.
4052 (catch 'done
4053 (while paren-state
4054 (let ((car (car paren-state)))
4055 (if (consp car)
4056 ;; just check the car, because in a balanced brace
4057 ;; expression, it must be impossible for the corresponding
4058 ;; close brace to be before point, but the open brace to
4059 ;; be after.
4060 (if (<= bufpos (car car))
4061 nil ; whack it off
4062 (if (< bufpos (cdr car))
4063 ;; its possible that the open brace is before
4064 ;; bufpos, but the close brace is after. In that
4065 ;; case, convert this to a non-cons element. The
4066 ;; rest of the state is before bufpos, so we're
4067 ;; done.
4068 (throw 'done (cons (car car) (cdr paren-state)))
4069 ;; we know that both the open and close braces are
4070 ;; before bufpos, so we also know that everything else
4071 ;; on state is before bufpos.
4072 (throw 'done paren-state)))
4073 (if (<= bufpos car)
4074 nil ; whack it off
4075 ;; it's before bufpos, so everything else should too.
4076 (throw 'done paren-state)))
4077 (setq paren-state (cdr paren-state)))
4078 nil)))
4080 (defun c-most-enclosing-brace (paren-state &optional bufpos)
4081 ;; Return the bufpos of the innermost enclosing open paren before
4082 ;; bufpos, or nil if none was found.
4083 (let (enclosingp)
4084 (or bufpos (setq bufpos 134217727))
4085 (while paren-state
4086 (setq enclosingp (car paren-state)
4087 paren-state (cdr paren-state))
4088 (if (or (consp enclosingp)
4089 (>= enclosingp bufpos))
4090 (setq enclosingp nil)
4091 (setq paren-state nil)))
4092 enclosingp))
4094 (defun c-least-enclosing-brace (paren-state)
4095 ;; Return the bufpos of the outermost enclosing open paren, or nil
4096 ;; if none was found.
4097 (let (pos elem)
4098 (while paren-state
4099 (setq elem (car paren-state)
4100 paren-state (cdr paren-state))
4101 (if (integerp elem)
4102 (setq pos elem)))
4103 pos))
4105 (defun c-safe-position (bufpos paren-state)
4106 ;; Return the closest "safe" position recorded on PAREN-STATE that
4107 ;; is higher up than BUFPOS. Return nil if PAREN-STATE doesn't
4108 ;; contain any. Return nil if BUFPOS is nil, which is useful to
4109 ;; find the closest limit before a given limit that might be nil.
4111 ;; A "safe" position is a position at or after a recorded open
4112 ;; paren, or after a recorded close paren. The returned position is
4113 ;; thus either the first position after a close brace, or the first
4114 ;; position after an enclosing paren, or at the enclosing paren in
4115 ;; case BUFPOS is immediately after it.
4116 (when bufpos
4117 (let (elem)
4118 (catch 'done
4119 (while paren-state
4120 (setq elem (car paren-state))
4121 (if (consp elem)
4122 (cond ((< (cdr elem) bufpos)
4123 (throw 'done (cdr elem)))
4124 ((< (car elem) bufpos)
4125 ;; See below.
4126 (throw 'done (min (1+ (car elem)) bufpos))))
4127 (if (< elem bufpos)
4128 ;; elem is the position at and not after the opening paren, so
4129 ;; we can go forward one more step unless it's equal to
4130 ;; bufpos. This is useful in some cases avoid an extra paren
4131 ;; level between the safe position and bufpos.
4132 (throw 'done (min (1+ elem) bufpos))))
4133 (setq paren-state (cdr paren-state)))))))
4135 (defun c-beginning-of-syntax ()
4136 ;; This is used for `font-lock-beginning-of-syntax-function'. It
4137 ;; goes to the closest previous point that is known to be outside
4138 ;; any string literal or comment. `c-state-cache' is used if it has
4139 ;; a position in the vicinity.
4140 (let* ((paren-state c-state-cache)
4141 elem
4143 (pos (catch 'done
4144 ;; Note: Similar code in `c-safe-position'. The
4145 ;; difference is that we accept a safe position at
4146 ;; the point and don't bother to go forward past open
4147 ;; parens.
4148 (while paren-state
4149 (setq elem (car paren-state))
4150 (if (consp elem)
4151 (cond ((<= (cdr elem) (point))
4152 (throw 'done (cdr elem)))
4153 ((<= (car elem) (point))
4154 (throw 'done (car elem))))
4155 (if (<= elem (point))
4156 (throw 'done elem)))
4157 (setq paren-state (cdr paren-state)))
4158 (point-min))))
4160 (if (> pos (- (point) 4000))
4161 (goto-char pos)
4162 ;; The position is far back. Try `c-beginning-of-defun-1'
4163 ;; (although we can't be entirely sure it will go to a position
4164 ;; outside a comment or string in current emacsen). FIXME:
4165 ;; Consult `syntax-ppss' here.
4166 (c-beginning-of-defun-1)
4167 (if (< (point) pos)
4168 (goto-char pos)))))
4171 ;; Tools for scanning identifiers and other tokens.
4173 (defun c-on-identifier ()
4174 "Return non-nil if the point is on or directly after an identifier.
4175 Keywords are recognized and not considered identifiers. If an
4176 identifier is detected, the returned value is its starting position.
4177 If an identifier ends at the point and another begins at it \(can only
4178 happen in Pike) then the point for the preceding one is returned.
4180 Note that this function might do hidden buffer changes. See the
4181 comment at the start of cc-engine.el for more info."
4183 ;; FIXME: Shouldn't this function handle "operator" in C++?
4185 (save-excursion
4186 (skip-syntax-backward "w_")
4190 ;; Check for a normal (non-keyword) identifier.
4191 (and (looking-at c-symbol-start)
4192 (not (looking-at c-keywords-regexp))
4193 (point))
4195 (when (c-major-mode-is 'pike-mode)
4196 ;; Handle the `<operator> syntax in Pike.
4197 (let ((pos (point)))
4198 (skip-chars-backward "-!%&*+/<=>^|~[]()")
4199 (and (if (< (skip-chars-backward "`") 0)
4201 (goto-char pos)
4202 (eq (char-after) ?\`))
4203 (looking-at c-symbol-key)
4204 (>= (match-end 0) pos)
4205 (point))))
4207 ;; Handle the "operator +" syntax in C++.
4208 (when (and c-overloadable-operators-regexp
4209 (= (c-backward-token-2 0) 0))
4211 (cond ((and (looking-at c-overloadable-operators-regexp)
4212 (or (not c-opt-op-identifier-prefix)
4213 (and (= (c-backward-token-2 1) 0)
4214 (looking-at c-opt-op-identifier-prefix))))
4215 (point))
4217 ((save-excursion
4218 (and c-opt-op-identifier-prefix
4219 (looking-at c-opt-op-identifier-prefix)
4220 (= (c-forward-token-2 1) 0)
4221 (looking-at c-overloadable-operators-regexp)))
4222 (point))))
4226 (defsubst c-simple-skip-symbol-backward ()
4227 ;; If the point is at the end of a symbol then skip backward to the
4228 ;; beginning of it. Don't move otherwise. Return non-nil if point
4229 ;; moved.
4231 ;; This function might do hidden buffer changes.
4232 (or (< (skip-syntax-backward "w_") 0)
4233 (and (c-major-mode-is 'pike-mode)
4234 ;; Handle the `<operator> syntax in Pike.
4235 (let ((pos (point)))
4236 (if (and (< (skip-chars-backward "-!%&*+/<=>^|~[]()") 0)
4237 (< (skip-chars-backward "`") 0)
4238 (looking-at c-symbol-key)
4239 (>= (match-end 0) pos))
4241 (goto-char pos)
4242 nil)))))
4244 (defun c-beginning-of-current-token (&optional back-limit)
4245 ;; Move to the beginning of the current token. Do not move if not
4246 ;; in the middle of one. BACK-LIMIT may be used to bound the
4247 ;; backward search; if given it's assumed to be at the boundary
4248 ;; between two tokens. Return non-nil if the point is moved, nil
4249 ;; otherwise.
4251 ;; This function might do hidden buffer changes.
4252 (let ((start (point)))
4253 (if (looking-at "\\w\\|\\s_")
4254 (skip-syntax-backward "w_" back-limit)
4255 (when (< (skip-syntax-backward ".()" back-limit) 0)
4256 (while (let ((pos (or (and (looking-at c-nonsymbol-token-regexp)
4257 (match-end 0))
4258 ;; `c-nonsymbol-token-regexp' should always match
4259 ;; since we've skipped backward over punctuation
4260 ;; or paren syntax, but consume one char in case
4261 ;; it doesn't so that we don't leave point before
4262 ;; some earlier incorrect token.
4263 (1+ (point)))))
4264 (if (<= pos start)
4265 (goto-char pos))))))
4266 (< (point) start)))
4268 (defun c-end-of-current-token (&optional back-limit)
4269 ;; Move to the end of the current token. Do not move if not in the
4270 ;; middle of one. BACK-LIMIT may be used to bound the backward
4271 ;; search; if given it's assumed to be at the boundary between two
4272 ;; tokens. Return non-nil if the point is moved, nil otherwise.
4274 ;; This function might do hidden buffer changes.
4275 (let ((start (point)))
4276 (cond ((< (skip-syntax-backward "w_" (1- start)) 0)
4277 (skip-syntax-forward "w_"))
4278 ((< (skip-syntax-backward ".()" back-limit) 0)
4279 (while (progn
4280 (if (looking-at c-nonsymbol-token-regexp)
4281 (goto-char (match-end 0))
4282 ;; `c-nonsymbol-token-regexp' should always match since
4283 ;; we've skipped backward over punctuation or paren
4284 ;; syntax, but move forward in case it doesn't so that
4285 ;; we don't leave point earlier than we started with.
4286 (forward-char))
4287 (< (point) start)))))
4288 (> (point) start)))
4290 (defconst c-jump-syntax-balanced
4291 (if (memq 'gen-string-delim c-emacs-features)
4292 "\\w\\|\\s_\\|\\s(\\|\\s)\\|\\s\"\\|\\s|"
4293 "\\w\\|\\s_\\|\\s(\\|\\s)\\|\\s\""))
4295 (defconst c-jump-syntax-unbalanced
4296 (if (memq 'gen-string-delim c-emacs-features)
4297 "\\w\\|\\s_\\|\\s\"\\|\\s|"
4298 "\\w\\|\\s_\\|\\s\""))
4300 (defun c-forward-over-token-and-ws (&optional balanced)
4301 "Move forward over a token and any following whitespace
4302 Return t if we moved, nil otherwise (i.e. we were at EOB, or a
4303 non-token or BALANCED is non-nil and we can't move). If we
4304 are at syntactic whitespace, move over this in place of a token.
4306 If BALANCED is non-nil move over any balanced parens we are at, and never move
4307 out of an enclosing paren.
4309 This function differs from `c-forward-token-2' in that it will move forward
4310 over the final token in a buffer, up to EOB."
4311 (let ((jump-syntax (if balanced
4312 c-jump-syntax-balanced
4313 c-jump-syntax-unbalanced))
4314 (here (point)))
4315 (when
4316 (condition-case nil
4317 (cond
4318 ((/= (point)
4319 (progn (c-forward-syntactic-ws) (point)))
4320 ;; If we're at whitespace, count this as the token.
4322 ((eobp) nil)
4323 ((looking-at jump-syntax)
4324 (goto-char (scan-sexps (point) 1))
4326 ((looking-at c-nonsymbol-token-regexp)
4327 (goto-char (match-end 0))
4329 ((save-restriction
4330 (widen)
4331 (looking-at c-nonsymbol-token-regexp))
4332 nil)
4334 (forward-char)
4336 (error (goto-char here)
4337 nil))
4338 (c-forward-syntactic-ws)
4339 t)))
4341 (defun c-forward-token-2 (&optional count balanced limit)
4342 "Move forward by tokens.
4343 A token is defined as all symbols and identifiers which aren't
4344 syntactic whitespace \(note that multicharacter tokens like \"==\" are
4345 treated properly). Point is always either left at the beginning of a
4346 token or not moved at all. COUNT specifies the number of tokens to
4347 move; a negative COUNT moves in the opposite direction. A COUNT of 0
4348 moves to the next token beginning only if not already at one. If
4349 BALANCED is true, move over balanced parens, otherwise move into them.
4350 Also, if BALANCED is true, never move out of an enclosing paren.
4352 LIMIT sets the limit for the movement and defaults to the point limit.
4353 The case when LIMIT is set in the middle of a token, comment or macro
4354 is handled correctly, i.e. the point won't be left there.
4356 Return the number of tokens left to move \(positive or negative). If
4357 BALANCED is true, a move over a balanced paren counts as one. Note
4358 that if COUNT is 0 and no appropriate token beginning is found, 1 will
4359 be returned. Thus, a return value of 0 guarantees that point is at
4360 the requested position and a return value less \(without signs) than
4361 COUNT guarantees that point is at the beginning of some token.
4363 Note that this function might do hidden buffer changes. See the
4364 comment at the start of cc-engine.el for more info."
4366 (or count (setq count 1))
4367 (if (< count 0)
4368 (- (c-backward-token-2 (- count) balanced limit))
4370 (let ((here (point))
4371 (last (point)))
4372 (when (zerop count)
4373 ;; If count is zero we should jump if in the middle of a token.
4374 (c-end-of-current-token))
4376 (save-restriction
4377 (if limit (narrow-to-region (point-min) limit))
4378 (if (/= (point)
4379 (progn (c-forward-syntactic-ws) (point)))
4380 ;; Skip whitespace. Count this as a move if we did in
4381 ;; fact move.
4382 (setq count (max (1- count) 0)))
4384 (if (eobp)
4385 ;; Moved out of bounds. Make sure the returned count isn't zero.
4386 (progn
4387 (if (zerop count) (setq count 1))
4388 (goto-char here))
4389 (while (and
4390 (> count 0)
4391 (c-forward-over-token-and-ws balanced)
4392 (not (eobp)))
4393 (setq last (point)
4394 count (1- count)))
4395 (if (eobp)
4396 (goto-char last))))
4397 count)))
4399 (defun c-backward-token-2 (&optional count balanced limit)
4400 "Move backward by tokens.
4401 See `c-forward-token-2' for details."
4403 (or count (setq count 1))
4404 (if (< count 0)
4405 (- (c-forward-token-2 (- count) balanced limit))
4407 (or limit (setq limit (point-min)))
4408 (let ((jump-syntax (if balanced
4409 c-jump-syntax-balanced
4410 c-jump-syntax-unbalanced))
4411 (last (point)))
4413 (if (zerop count)
4414 ;; The count is zero so try to skip to the beginning of the
4415 ;; current token.
4416 (if (> (point)
4417 (progn (c-beginning-of-current-token) (point)))
4418 (if (< (point) limit)
4419 ;; The limit is inside the same token, so return 1.
4420 (setq count 1))
4422 ;; We're not in the middle of a token. If there's
4423 ;; whitespace after the point then we must move backward,
4424 ;; so set count to 1 in that case.
4425 (and (looking-at c-syntactic-ws-start)
4426 ;; If we're looking at a '#' that might start a cpp
4427 ;; directive then we have to do a more elaborate check.
4428 (or (/= (char-after) ?#)
4429 (not c-opt-cpp-prefix)
4430 (save-excursion
4431 (and (= (point)
4432 (progn (beginning-of-line)
4433 (looking-at "[ \t]*")
4434 (match-end 0)))
4435 (or (bobp)
4436 (progn (backward-char)
4437 (not (eq (char-before) ?\\)))))))
4438 (setq count 1))))
4440 ;; Use `condition-case' to avoid having to check for buffer
4441 ;; limits in `backward-char', `scan-sexps' and `goto-char' below.
4442 (condition-case nil
4443 (while (and
4444 (> count 0)
4445 (progn
4446 (c-backward-syntactic-ws)
4447 (backward-char)
4448 (if (looking-at jump-syntax)
4449 (goto-char (scan-sexps (1+ (point)) -1))
4450 ;; This can be very inefficient if there's a long
4451 ;; sequence of operator tokens without any separation.
4452 ;; That doesn't happen in practice, anyway.
4453 (c-beginning-of-current-token))
4454 (>= (point) limit)))
4455 (setq last (point)
4456 count (1- count)))
4457 (error (goto-char last)))
4459 (if (< (point) limit)
4460 (goto-char last))
4462 count)))
4464 (defun c-forward-token-1 (&optional count balanced limit)
4465 "Like `c-forward-token-2' but doesn't treat multicharacter operator
4466 tokens like \"==\" as single tokens, i.e. all sequences of symbol
4467 characters are jumped over character by character. This function is
4468 for compatibility only; it's only a wrapper over `c-forward-token-2'."
4469 (let ((c-nonsymbol-token-regexp "\\s."))
4470 (c-forward-token-2 count balanced limit)))
4472 (defun c-backward-token-1 (&optional count balanced limit)
4473 "Like `c-backward-token-2' but doesn't treat multicharacter operator
4474 tokens like \"==\" as single tokens, i.e. all sequences of symbol
4475 characters are jumped over character by character. This function is
4476 for compatibility only; it's only a wrapper over `c-backward-token-2'."
4477 (let ((c-nonsymbol-token-regexp "\\s."))
4478 (c-backward-token-2 count balanced limit)))
4481 ;; Tools for doing searches restricted to syntactically relevant text.
4483 (defun c-syntactic-re-search-forward (regexp &optional bound noerror
4484 paren-level not-inside-token
4485 lookbehind-submatch)
4486 "Like `re-search-forward', but only report matches that are found
4487 in syntactically significant text. I.e. matches in comments, macros
4488 or string literals are ignored. The start point is assumed to be
4489 outside any comment, macro or string literal, or else the content of
4490 that region is taken as syntactically significant text.
4492 NOERROR, in addition to the values nil, t, and <anything else>
4493 used in `re-search-forward' can also take the values
4494 'before-literal and 'after-literal. In these cases, when BOUND
4495 is also given and is inside a literal, and a search fails, point
4496 will be left, respectively before or after the literal. Be aware
4497 that with 'after-literal, if a string or comment is unclosed at
4498 the end of the buffer, point may be left there, even though it is
4499 inside a literal there.
4501 If PAREN-LEVEL is non-nil, an additional restriction is added to
4502 ignore matches in nested paren sexps. The search will also not go
4503 outside the current list sexp, which has the effect that if the point
4504 should be moved to BOUND when no match is found \(i.e. NOERROR is
4505 neither nil nor t), then it will be at the closing paren if the end of
4506 the current list sexp is encountered first.
4508 If NOT-INSIDE-TOKEN is non-nil, matches in the middle of tokens are
4509 ignored. Things like multicharacter operators and special symbols
4510 \(e.g. \"`()\" in Pike) are handled but currently not floating point
4511 constants.
4513 If LOOKBEHIND-SUBMATCH is non-nil, it's taken as a number of a
4514 subexpression in REGEXP. The end of that submatch is used as the
4515 position to check for syntactic significance. If LOOKBEHIND-SUBMATCH
4516 isn't used or if that subexpression didn't match then the start
4517 position of the whole match is used instead. The \"look behind\"
4518 subexpression is never tested before the starting position, so it
4519 might be a good idea to include \\=\\= as a match alternative in it.
4521 Optimization note: Matches might be missed if the \"look behind\"
4522 subexpression can match the end of nonwhite syntactic whitespace,
4523 i.e. the end of comments or cpp directives. This since the function
4524 skips over such things before resuming the search. It's on the other
4525 hand not safe to assume that the \"look behind\" subexpression never
4526 matches syntactic whitespace.
4528 Bug: Unbalanced parens inside cpp directives are currently not handled
4529 correctly \(i.e. they don't get ignored as they should) when
4530 PAREN-LEVEL is set.
4532 Note that this function might do hidden buffer changes. See the
4533 comment at the start of cc-engine.el for more info."
4535 (or bound (setq bound (point-max)))
4536 (if paren-level (setq paren-level -1))
4538 ;;(message "c-syntactic-re-search-forward %s %s %S" (point) bound regexp)
4540 (let ((start (point))
4542 ;; Start position for the last search.
4543 search-pos
4544 ;; The `parse-partial-sexp' state between the start position
4545 ;; and the point.
4546 state
4547 ;; The current position after the last state update. The next
4548 ;; `parse-partial-sexp' continues from here.
4549 (state-pos (point))
4550 ;; The position at which to check the state and the state
4551 ;; there. This is separate from `state-pos' since we might
4552 ;; need to back up before doing the next search round.
4553 check-pos check-state
4554 ;; Last position known to end a token.
4555 (last-token-end-pos (point-min))
4556 ;; Set when a valid match is found.
4557 found)
4559 (condition-case err
4560 (while
4561 (and
4562 (progn
4563 (setq search-pos (point))
4564 (if (re-search-forward regexp bound noerror)
4566 ;; Without the following, when PAREN-LEVEL is non-nil, and
4567 ;; NOERROR is not nil or t, and the very first search above
4568 ;; has just failed, point would end up at BOUND rather than
4569 ;; just before the next close paren.
4570 (when (and (eq search-pos start)
4571 paren-level
4572 (not (memq noerror '(nil t))))
4573 (setq state (parse-partial-sexp start bound -1))
4574 (if (eq (car state) -1)
4575 (setq bound (1- (point)))))
4576 nil))
4578 (progn
4579 (setq state (parse-partial-sexp
4580 state-pos (match-beginning 0) paren-level nil state)
4581 state-pos (point))
4582 (if (setq check-pos (and lookbehind-submatch
4583 (or (not paren-level)
4584 (>= (car state) 0))
4585 (match-end lookbehind-submatch)))
4586 (setq check-state (parse-partial-sexp
4587 state-pos check-pos paren-level nil state))
4588 (setq check-pos state-pos
4589 check-state state))
4591 ;; NOTE: If we got a look behind subexpression and get
4592 ;; an insignificant match in something that isn't
4593 ;; syntactic whitespace (i.e. strings or in nested
4594 ;; parentheses), then we can never skip more than a
4595 ;; single character from the match start position
4596 ;; (i.e. `state-pos' here) before continuing the
4597 ;; search. That since the look behind subexpression
4598 ;; might match the end of the insignificant region in
4599 ;; the next search.
4601 (cond
4602 ((elt check-state 7)
4603 ;; Match inside a line comment. Skip to eol. Use
4604 ;; `re-search-forward' instead of `skip-chars-forward' to get
4605 ;; the right bound behavior.
4606 (re-search-forward "[\n\r]" bound noerror))
4608 ((elt check-state 4)
4609 ;; Match inside a block comment. Skip to the '*/'.
4610 (search-forward "*/" bound noerror))
4612 ((and (not (elt check-state 5))
4613 (eq (char-before check-pos) ?/)
4614 (not (c-get-char-property (1- check-pos) 'syntax-table))
4615 (memq (char-after check-pos) '(?/ ?*)))
4616 ;; Match in the middle of the opener of a block or line
4617 ;; comment.
4618 (if (= (char-after check-pos) ?/)
4619 (re-search-forward "[\n\r]" bound noerror)
4620 (search-forward "*/" bound noerror)))
4622 ;; The last `parse-partial-sexp' above might have
4623 ;; stopped short of the real check position if the end
4624 ;; of the current sexp was encountered in paren-level
4625 ;; mode. The checks above are always false in that
4626 ;; case, and since they can do better skipping in
4627 ;; lookbehind-submatch mode, we do them before
4628 ;; checking the paren level.
4630 ((and paren-level
4631 (/= (setq tmp (car check-state)) 0))
4632 ;; Check the paren level first since we're short of the
4633 ;; syntactic checking position if the end of the
4634 ;; current sexp was encountered by `parse-partial-sexp'.
4635 (if (> tmp 0)
4637 ;; Inside a nested paren sexp.
4638 (if lookbehind-submatch
4639 ;; See the NOTE above.
4640 (progn (goto-char state-pos) t)
4641 ;; Skip out of the paren quickly.
4642 (setq state (parse-partial-sexp state-pos bound 0 nil state)
4643 state-pos (point)))
4645 ;; Have exited the current paren sexp.
4646 (if noerror
4647 (progn
4648 ;; The last `parse-partial-sexp' call above
4649 ;; has left us just after the closing paren
4650 ;; in this case, so we can modify the bound
4651 ;; to leave the point at the right position
4652 ;; upon return.
4653 (setq bound (1- (point)))
4654 nil)
4655 (signal 'search-failed (list regexp)))))
4657 ((setq tmp (elt check-state 3))
4658 ;; Match inside a string.
4659 (if (or lookbehind-submatch
4660 (not (integerp tmp)))
4661 ;; See the NOTE above.
4662 (progn (goto-char state-pos) t)
4663 ;; Skip to the end of the string before continuing.
4664 (let ((ender (make-string 1 tmp)) (continue t))
4665 (while (if (search-forward ender bound noerror)
4666 (progn
4667 (setq state (parse-partial-sexp
4668 state-pos (point) nil nil state)
4669 state-pos (point))
4670 (elt state 3))
4671 (setq continue nil)))
4672 continue)))
4674 ((save-excursion
4675 (save-match-data
4676 (c-beginning-of-macro start)))
4677 ;; Match inside a macro. Skip to the end of it.
4678 (c-end-of-macro)
4679 (cond ((<= (point) bound) t)
4680 (noerror nil)
4681 (t (signal 'search-failed (list regexp)))))
4683 ((and not-inside-token
4684 (or (< check-pos last-token-end-pos)
4685 (< check-pos
4686 (save-excursion
4687 (goto-char check-pos)
4688 (save-match-data
4689 (c-end-of-current-token last-token-end-pos))
4690 (setq last-token-end-pos (point))))))
4691 ;; Inside a token.
4692 (if lookbehind-submatch
4693 ;; See the NOTE above.
4694 (goto-char state-pos)
4695 (goto-char (min last-token-end-pos bound))))
4698 ;; A real match.
4699 (setq found t)
4700 nil)))
4702 ;; Should loop to search again, but take care to avoid
4703 ;; looping on the same spot.
4704 (or (/= search-pos (point))
4705 (if (= (point) bound)
4706 (if noerror
4708 (signal 'search-failed (list regexp)))
4709 (forward-char)
4710 t))))
4712 (error
4713 (goto-char start)
4714 (signal (car err) (cdr err))))
4716 ;;(message "c-syntactic-re-search-forward done %s" (or (match-end 0) (point)))
4718 (if found
4719 (progn
4720 (goto-char (match-end 0))
4721 (match-end 0))
4723 ;; Search failed. Set point as appropriate.
4724 (cond
4725 ((eq noerror t)
4726 (goto-char start))
4727 ((not (memq noerror '(before-literal after-literal)))
4728 (goto-char bound))
4729 (t (setq state (parse-partial-sexp state-pos bound nil nil state))
4730 (if (or (elt state 3) (elt state 4))
4731 (if (eq noerror 'before-literal)
4732 (goto-char (elt state 8))
4733 (parse-partial-sexp bound (point-max) nil nil
4734 state 'syntax-table))
4735 (goto-char bound))))
4737 nil)))
4739 (defvar safe-pos-list) ; bound in c-syntactic-skip-backward
4741 (defsubst c-ssb-lit-begin ()
4742 ;; Return the start of the literal point is in, or nil.
4743 ;; We read and write the variables `safe-pos', `safe-pos-list', `state'
4744 ;; bound in the caller.
4746 ;; Use `parse-partial-sexp' from a safe position down to the point to check
4747 ;; if it's outside comments and strings.
4748 (save-excursion
4749 (let ((pos (point)) safe-pos state)
4750 ;; Pick a safe position as close to the point as possible.
4752 ;; FIXME: Consult `syntax-ppss' here if our cache doesn't give a good
4753 ;; position.
4755 (while (and safe-pos-list
4756 (> (car safe-pos-list) (point)))
4757 (setq safe-pos-list (cdr safe-pos-list)))
4758 (unless (setq safe-pos (car-safe safe-pos-list))
4759 (setq safe-pos (max (or (c-safe-position
4760 (point) (c-parse-state))
4762 (point-min))
4763 safe-pos-list (list safe-pos)))
4765 ;; Cache positions along the way to use if we have to back up more. We
4766 ;; cache every closing paren on the same level. If the paren cache is
4767 ;; relevant in this region then we're typically already on the same
4768 ;; level as the target position. Note that we might cache positions
4769 ;; after opening parens in case safe-pos is in a nested list. That's
4770 ;; both uncommon and harmless.
4771 (while (progn
4772 (setq state (parse-partial-sexp
4773 safe-pos pos 0))
4774 (< (point) pos))
4775 (setq safe-pos (point)
4776 safe-pos-list (cons safe-pos safe-pos-list)))
4778 ;; If the state contains the start of the containing sexp we cache that
4779 ;; position too, so that parse-partial-sexp in the next run has a bigger
4780 ;; chance of starting at the same level as the target position and thus
4781 ;; will get more good safe positions into the list.
4782 (if (elt state 1)
4783 (setq safe-pos (1+ (elt state 1))
4784 safe-pos-list (cons safe-pos safe-pos-list)))
4786 (if (or (elt state 3) (elt state 4))
4787 ;; Inside string or comment. Continue search at the
4788 ;; beginning of it.
4789 (elt state 8)))))
4791 (defun c-syntactic-skip-backward (skip-chars &optional limit paren-level)
4792 "Like `skip-chars-backward' but only look at syntactically relevant chars,
4793 i.e. don't stop at positions inside syntactic whitespace or string
4794 literals. Preprocessor directives are also ignored, with the exception
4795 of the one that the point starts within, if any. If LIMIT is given,
4796 it's assumed to be at a syntactically relevant position.
4798 If PAREN-LEVEL is non-nil, the function won't stop in nested paren
4799 sexps, and the search will also not go outside the current paren sexp.
4800 However, if LIMIT or the buffer limit is reached inside a nested paren
4801 then the point will be left at the limit.
4803 Non-nil is returned if the point moved, nil otherwise.
4805 Note that this function might do hidden buffer changes. See the
4806 comment at the start of cc-engine.el for more info."
4808 (c-self-bind-state-cache
4809 (let ((start (point))
4810 ;; A list of syntactically relevant positions in descending
4811 ;; order. It's used to avoid scanning repeatedly over
4812 ;; potentially large regions with `parse-partial-sexp' to verify
4813 ;; each position. Used in `c-ssb-lit-begin'
4814 safe-pos-list
4815 ;; The result from `c-beginning-of-macro' at the start position or the
4816 ;; start position itself if it isn't within a macro. Evaluated on
4817 ;; demand.
4818 start-macro-beg
4819 ;; The earliest position after the current one with the same paren
4820 ;; level. Used only when `paren-level' is set.
4821 lit-beg
4822 (paren-level-pos (point)))
4824 (while
4825 (progn
4826 ;; The next loop "tries" to find the end point each time round,
4827 ;; loops when it hasn't succeeded.
4828 (while
4829 (and
4830 (let ((pos (point)))
4831 (while (and
4832 (< (skip-chars-backward skip-chars limit) 0)
4833 ;; Don't stop inside a literal.
4834 (when (setq lit-beg (c-ssb-lit-begin))
4835 (goto-char lit-beg)
4836 t)))
4837 (< (point) pos))
4839 (let ((pos (point)) state-2 pps-end-pos)
4841 (cond
4842 ((and paren-level
4843 (save-excursion
4844 (setq state-2 (parse-partial-sexp
4845 pos paren-level-pos -1)
4846 pps-end-pos (point))
4847 (/= (car state-2) 0)))
4848 ;; Not at the right level.
4850 (if (and (< (car state-2) 0)
4851 ;; We stop above if we go out of a paren.
4852 ;; Now check whether it precedes or is
4853 ;; nested in the starting sexp.
4854 (save-excursion
4855 (setq state-2
4856 (parse-partial-sexp
4857 pps-end-pos paren-level-pos
4858 nil nil state-2))
4859 (< (car state-2) 0)))
4861 ;; We've stopped short of the starting position
4862 ;; so the hit was inside a nested list. Go up
4863 ;; until we are at the right level.
4864 (condition-case nil
4865 (progn
4866 (goto-char (scan-lists pos -1
4867 (- (car state-2))))
4868 (setq paren-level-pos (point))
4869 (if (and limit (>= limit paren-level-pos))
4870 (progn
4871 (goto-char limit)
4872 nil)
4874 (error
4875 (goto-char (or limit (point-min)))
4876 nil))
4878 ;; The hit was outside the list at the start
4879 ;; position. Go to the start of the list and exit.
4880 (goto-char (1+ (elt state-2 1)))
4881 nil))
4883 ((c-beginning-of-macro limit)
4884 ;; Inside a macro.
4885 (if (< (point)
4886 (or start-macro-beg
4887 (setq start-macro-beg
4888 (save-excursion
4889 (goto-char start)
4890 (c-beginning-of-macro limit)
4891 (point)))))
4894 ;; It's inside the same macro we started in so it's
4895 ;; a relevant match.
4896 (goto-char pos)
4897 nil))))))
4899 (> (point)
4900 (progn
4901 ;; Skip syntactic ws afterwards so that we don't stop at the
4902 ;; end of a comment if `skip-chars' is something like "^/".
4903 (c-backward-syntactic-ws)
4904 (point)))))
4906 ;; We might want to extend this with more useful return values in
4907 ;; the future.
4908 (/= (point) start))))
4910 ;; The following is an alternative implementation of
4911 ;; `c-syntactic-skip-backward' that uses backward movement to keep
4912 ;; track of the syntactic context. It turned out to be generally
4913 ;; slower than the one above which uses forward checks from earlier
4914 ;; safe positions.
4916 ;;(defconst c-ssb-stop-re
4917 ;; ;; The regexp matching chars `c-syntactic-skip-backward' needs to
4918 ;; ;; stop at to avoid going into comments and literals.
4919 ;; (concat
4920 ;; ;; Match comment end syntax and string literal syntax. Also match
4921 ;; ;; '/' for block comment endings (not covered by comment end
4922 ;; ;; syntax).
4923 ;; "\\s>\\|/\\|\\s\""
4924 ;; (if (memq 'gen-string-delim c-emacs-features)
4925 ;; "\\|\\s|"
4926 ;; "")
4927 ;; (if (memq 'gen-comment-delim c-emacs-features)
4928 ;; "\\|\\s!"
4929 ;; "")))
4931 ;;(defconst c-ssb-stop-paren-re
4932 ;; ;; Like `c-ssb-stop-re' but also stops at paren chars.
4933 ;; (concat c-ssb-stop-re "\\|\\s(\\|\\s)"))
4935 ;;(defconst c-ssb-sexp-end-re
4936 ;; ;; Regexp matching the ending syntax of a complex sexp.
4937 ;; (concat c-string-limit-regexp "\\|\\s)"))
4939 ;;(defun c-syntactic-skip-backward (skip-chars &optional limit paren-level)
4940 ;; "Like `skip-chars-backward' but only look at syntactically relevant chars,
4941 ;;i.e. don't stop at positions inside syntactic whitespace or string
4942 ;;literals. Preprocessor directives are also ignored. However, if the
4943 ;;point is within a comment, string literal or preprocessor directory to
4944 ;;begin with, its contents is treated as syntactically relevant chars.
4945 ;;If LIMIT is given, it limits the backward search and the point will be
4946 ;;left there if no earlier position is found.
4948 ;;If PAREN-LEVEL is non-nil, the function won't stop in nested paren
4949 ;;sexps, and the search will also not go outside the current paren sexp.
4950 ;;However, if LIMIT or the buffer limit is reached inside a nested paren
4951 ;;then the point will be left at the limit.
4953 ;;Non-nil is returned if the point moved, nil otherwise.
4955 ;;Note that this function might do hidden buffer changes. See the
4956 ;;comment at the start of cc-engine.el for more info."
4958 ;; (save-restriction
4959 ;; (when limit
4960 ;; (narrow-to-region limit (point-max)))
4962 ;; (let ((start (point)))
4963 ;; (catch 'done
4964 ;; (while (let ((last-pos (point))
4965 ;; (stop-pos (progn
4966 ;; (skip-chars-backward skip-chars)
4967 ;; (point))))
4969 ;; ;; Skip back over the same region as
4970 ;; ;; `skip-chars-backward' above, but keep to
4971 ;; ;; syntactically relevant positions.
4972 ;; (goto-char last-pos)
4973 ;; (while (and
4974 ;; ;; `re-search-backward' with a single char regexp
4975 ;; ;; should be fast.
4976 ;; (re-search-backward
4977 ;; (if paren-level c-ssb-stop-paren-re c-ssb-stop-re)
4978 ;; stop-pos 'move)
4980 ;; (progn
4981 ;; (cond
4982 ;; ((looking-at "\\s(")
4983 ;; ;; `paren-level' is set and we've found the
4984 ;; ;; start of the containing paren.
4985 ;; (forward-char)
4986 ;; (throw 'done t))
4988 ;; ((looking-at c-ssb-sexp-end-re)
4989 ;; ;; We're at the end of a string literal or paren
4990 ;; ;; sexp (if `paren-level' is set).
4991 ;; (forward-char)
4992 ;; (condition-case nil
4993 ;; (c-backward-sexp)
4994 ;; (error
4995 ;; (goto-char limit)
4996 ;; (throw 'done t))))
4998 ;; (t
4999 ;; (forward-char)
5000 ;; ;; At the end of some syntactic ws or possibly
5001 ;; ;; after a plain '/' operator.
5002 ;; (let ((pos (point)))
5003 ;; (c-backward-syntactic-ws)
5004 ;; (if (= pos (point))
5005 ;; ;; Was a plain '/' operator. Go past it.
5006 ;; (backward-char)))))
5008 ;; (> (point) stop-pos))))
5010 ;; ;; Now the point is either at `stop-pos' or at some
5011 ;; ;; position further back if `stop-pos' was at a
5012 ;; ;; syntactically irrelevant place.
5014 ;; ;; Skip additional syntactic ws so that we don't stop
5015 ;; ;; at the end of a comment if `skip-chars' is
5016 ;; ;; something like "^/".
5017 ;; (c-backward-syntactic-ws)
5019 ;; (< (point) stop-pos))))
5021 ;; ;; We might want to extend this with more useful return values
5022 ;; ;; in the future.
5023 ;; (/= (point) start))))
5026 ;; Tools for handling comments and string literals.
5028 (defun c-in-literal (&optional _lim detect-cpp)
5029 "Return the type of literal point is in, if any.
5030 The return value is `c' if in a C-style comment, `c++' if in a C++
5031 style comment, `string' if in a string literal, `pound' if DETECT-CPP
5032 is non-nil and in a preprocessor line, or nil if somewhere else.
5033 Optional LIM is used as the backward limit of the search. If omitted,
5034 or nil, `c-beginning-of-defun' is used.
5036 Note that this function might do hidden buffer changes. See the
5037 comment at the start of cc-engine.el for more info."
5038 (save-restriction
5039 (widen)
5040 (let ((lit (c-state-semi-pp-to-literal (point))))
5041 (or (cadr lit)
5042 (and detect-cpp
5043 (save-excursion (c-beginning-of-macro))
5044 'pound)))))
5046 (defun c-literal-limits (&optional lim near not-in-delimiter)
5047 "Return a cons of the beginning and end positions of the comment or
5048 string surrounding point (including both delimiters), or nil if point
5049 isn't in one. If LIM is non-nil, it's used as the \"safe\" position
5050 to start parsing from. If NEAR is non-nil, then the limits of any
5051 literal next to point is returned. \"Next to\" means there's only
5052 spaces and tabs between point and the literal. The search for such a
5053 literal is done first in forward direction. If NOT-IN-DELIMITER is
5054 non-nil, the case when point is inside a starting delimiter won't be
5055 recognized. This only has effect for comments which have starting
5056 delimiters with more than one character.
5058 Note that this function might do hidden buffer changes. See the
5059 comment at the start of cc-engine.el for more info."
5061 (save-excursion
5062 (let*
5063 ((pos (point))
5064 (lit-limits
5065 (if lim
5066 (let ((s (parse-partial-sexp lim (point))))
5067 (when (or (nth 3 s)
5068 (and (nth 4 s) (not (eq (nth 7 s) 'syntax-table))))
5069 (cons (nth 8 s)
5070 (progn (parse-partial-sexp (point) (point-max)
5071 nil nil
5073 'syntax-table)
5074 (point)))))
5075 (let ((pp-to-lit (c-state-full-pp-to-literal pos not-in-delimiter)))
5076 (car (cddr pp-to-lit))))))
5077 (cond
5078 (lit-limits)
5080 (near
5081 (goto-char pos)
5082 ;; Search forward for a literal.
5083 (skip-chars-forward " \t")
5084 (cond
5085 ((looking-at c-string-limit-regexp) ; String.
5086 (cons (point) (or (c-safe (c-forward-sexp 1) (point))
5087 (point-max))))
5089 ((looking-at c-comment-start-regexp) ; Line or block comment.
5090 (cons (point) (progn (c-forward-single-comment) (point))))
5093 ;; Search backward.
5094 (skip-chars-backward " \t")
5096 (let ((end (point)) beg)
5097 (cond
5098 ((save-excursion
5099 (< (skip-syntax-backward c-string-syntax) 0)) ; String.
5100 (setq beg (c-safe (c-backward-sexp 1) (point))))
5102 ((and (c-safe (forward-char -2) t)
5103 (looking-at "*/"))
5104 ;; Block comment. Due to the nature of line
5105 ;; comments, they will always be covered by the
5106 ;; normal case above.
5107 (goto-char end)
5108 (c-backward-single-comment)
5109 ;; If LIM is bogus, beg will be bogus.
5110 (setq beg (point))))
5112 (if beg (cons beg end))))))
5113 ))))
5115 (defun c-literal-start (&optional safe-pos)
5116 "Return the start of the string or comment surrounding point, or nil if
5117 point isn't in one. SAFE-POS, if non-nil, is a position before point which is
5118 a known \"safe position\", i.e. outside of any string or comment."
5119 (if safe-pos
5120 (let ((s (parse-partial-sexp safe-pos (point))))
5121 (and (or (nth 3 s)
5122 (and (nth 4 s) (not (eq (nth 7 s) 'syntax-table))))
5123 (nth 8 s)))
5124 (car (cddr (c-state-semi-pp-to-literal (point))))))
5126 ;; In case external callers use this; it did have a docstring.
5127 (defalias 'c-literal-limits-fast 'c-literal-limits)
5129 (defun c-collect-line-comments (range)
5130 "If the argument is a cons of two buffer positions (such as returned by
5131 `c-literal-limits'), and that range contains a C++ style line comment,
5132 then an extended range is returned that contains all adjacent line
5133 comments (i.e. all comments that starts in the same column with no
5134 empty lines or non-whitespace characters between them). Otherwise the
5135 argument is returned.
5137 Note that this function might do hidden buffer changes. See the
5138 comment at the start of cc-engine.el for more info."
5140 (save-excursion
5141 (condition-case nil
5142 (if (and (consp range) (progn
5143 (goto-char (car range))
5144 (looking-at c-line-comment-starter)))
5145 (let ((col (current-column))
5146 (beg (point))
5147 (bopl (c-point 'bopl))
5148 (end (cdr range)))
5149 ;; Got to take care in the backward direction to handle
5150 ;; comments which are preceded by code.
5151 (while (and (c-backward-single-comment)
5152 (>= (point) bopl)
5153 (looking-at c-line-comment-starter)
5154 (= col (current-column)))
5155 (setq beg (point)
5156 bopl (c-point 'bopl)))
5157 (goto-char end)
5158 (while (and (progn (skip-chars-forward " \t")
5159 (looking-at c-line-comment-starter))
5160 (= col (current-column))
5161 (prog1 (zerop (forward-line 1))
5162 (setq end (point)))))
5163 (cons beg end))
5164 range)
5165 (error range))))
5167 (defun c-literal-type (range)
5168 "Convenience function that given the result of `c-literal-limits',
5169 returns nil or the type of literal that the range surrounds, one
5170 of the symbols `c', `c++' or `string'. It's much faster than using
5171 `c-in-literal' and is intended to be used when you need both the
5172 type of a literal and its limits.
5174 Note that this function might do hidden buffer changes. See the
5175 comment at the start of cc-engine.el for more info."
5177 (if (consp range)
5178 (save-excursion
5179 (goto-char (car range))
5180 (cond ((looking-at c-string-limit-regexp) 'string)
5181 ((or (looking-at "//") ; c++ line comment
5182 (and (looking-at "\\s<") ; comment starter
5183 (looking-at "#"))) ; awk comment.
5184 'c++)
5185 (t 'c))) ; Assuming the range is valid.
5186 range))
5188 (defsubst c-determine-limit-get-base (start try-size)
5189 ;; Get a "safe place" approximately TRY-SIZE characters before START.
5190 ;; This defsubst doesn't preserve point.
5191 (let* ((pos (max (- start try-size) (point-min)))
5192 (s (c-state-semi-pp-to-literal pos))
5193 (cand (or (car (cddr s)) pos)))
5194 (if (>= cand (point-min))
5195 cand
5196 (parse-partial-sexp pos start nil nil (car s) 'syntax-table)
5197 (point))))
5199 (defun c-determine-limit (how-far-back &optional start try-size)
5200 ;; Return a buffer position HOW-FAR-BACK non-literal characters from
5201 ;; START (default point). The starting position, either point or
5202 ;; START may not be in a comment or string.
5204 ;; The position found will not be before POINT-MIN and won't be in a
5205 ;; literal.
5207 ;; We start searching for the sought position TRY-SIZE (default
5208 ;; twice HOW-FAR-BACK) bytes back from START.
5210 ;; This function must be fast. :-)
5211 (save-excursion
5212 (let* ((start (or start (point)))
5213 (try-size (or try-size (* 2 how-far-back)))
5214 (base (c-determine-limit-get-base start try-size))
5215 (pos base)
5217 (s (parse-partial-sexp pos pos)) ; null state.
5218 stack elt size
5219 (count 0))
5220 (while (< pos start)
5221 ;; Move forward one literal each time round this loop.
5222 ;; Move forward to the start of a comment or string.
5223 (setq s (parse-partial-sexp
5225 start
5226 nil ; target-depth
5227 nil ; stop-before
5228 s ; state
5229 'syntax-table)) ; stop-comment
5231 ;; Gather details of the non-literal-bit - starting pos and size.
5232 (setq size (- (if (or (and (nth 4 s) (not (eq (nth 7 s) 'syntax-table)))
5233 (nth 3 s))
5234 (nth 8 s)
5235 (point))
5236 pos))
5237 (if (> size 0)
5238 (setq stack (cons (cons pos size) stack)))
5240 ;; Move forward to the end of the comment/string.
5241 (if (or (and (nth 4 s) (not (eq (nth 7 s) 'syntax-table)))
5242 (nth 3 s))
5243 (setq s (parse-partial-sexp
5244 (point)
5245 start
5246 nil ; target-depth
5247 nil ; stop-before
5248 s ; state
5249 'syntax-table))) ; stop-comment
5250 (setq pos (point)))
5252 ;; Now try and find enough non-literal characters recorded on the stack.
5253 ;; Go back one recorded literal each time round this loop.
5254 (while (and (< count how-far-back)
5255 stack)
5256 (setq elt (car stack)
5257 stack (cdr stack))
5258 (setq count (+ count (cdr elt))))
5260 ;; Have we found enough yet?
5261 (cond
5262 ((>= count how-far-back)
5263 (+ (car elt) (- count how-far-back)))
5264 ((eq base (point-min))
5265 (point-min))
5266 ((> base (- start try-size)) ; Can only happen if we hit point-min.
5267 (car elt))
5269 (c-determine-limit (- how-far-back count) base try-size))))))
5271 (defun c-determine-+ve-limit (how-far &optional start-pos)
5272 ;; Return a buffer position about HOW-FAR non-literal characters forward
5273 ;; from START-POS (default point), which must not be inside a literal.
5274 (save-excursion
5275 (let ((pos (or start-pos (point)))
5276 (count how-far)
5277 (s (parse-partial-sexp (point) (point)))) ; null state
5278 (while (and (not (eobp))
5279 (> count 0))
5280 ;; Scan over counted characters.
5281 (setq s (parse-partial-sexp
5283 (min (+ pos count) (point-max))
5284 nil ; target-depth
5285 nil ; stop-before
5286 s ; state
5287 'syntax-table)) ; stop-comment
5288 (setq count (- count (- (point) pos) 1)
5289 pos (point))
5290 ;; Scan over literal characters.
5291 (if (nth 8 s)
5292 (setq s (parse-partial-sexp
5294 (point-max)
5295 nil ; target-depth
5296 nil ; stop-before
5297 s ; state
5298 'syntax-table) ; stop-comment
5299 pos (point))))
5300 (point))))
5303 ;; `c-find-decl-spots' and accompanying stuff.
5305 ;; Variables used in `c-find-decl-spots' to cache the search done for
5306 ;; the first declaration in the last call. When that function starts,
5307 ;; it needs to back up over syntactic whitespace to look at the last
5308 ;; token before the region being searched. That can sometimes cause
5309 ;; moves back and forth over a quite large region of comments and
5310 ;; macros, which would be repeated for each changed character when
5311 ;; we're called during fontification, since font-lock refontifies the
5312 ;; current line for each change. Thus it's worthwhile to cache the
5313 ;; first match.
5315 ;; `c-find-decl-syntactic-pos' is a syntactically relevant position in
5316 ;; the syntactic whitespace less or equal to some start position.
5317 ;; There's no cached value if it's nil.
5319 ;; `c-find-decl-match-pos' is the match position if
5320 ;; `c-find-decl-prefix-search' matched before the syntactic whitespace
5321 ;; at `c-find-decl-syntactic-pos', or nil if there's no such match.
5322 (defvar c-find-decl-syntactic-pos nil)
5323 (make-variable-buffer-local 'c-find-decl-syntactic-pos)
5324 (defvar c-find-decl-match-pos nil)
5325 (make-variable-buffer-local 'c-find-decl-match-pos)
5327 (defsubst c-invalidate-find-decl-cache (change-min-pos)
5328 (and c-find-decl-syntactic-pos
5329 (< change-min-pos c-find-decl-syntactic-pos)
5330 (setq c-find-decl-syntactic-pos nil)))
5332 ; (defface c-debug-decl-spot-face
5333 ; '((t (:background "Turquoise")))
5334 ; "Debug face to mark the spots where `c-find-decl-spots' stopped.")
5335 ; (defface c-debug-decl-sws-face
5336 ; '((t (:background "Khaki")))
5337 ; "Debug face to mark the syntactic whitespace between the declaration
5338 ; spots and the preceding token end.")
5340 (defmacro c-debug-put-decl-spot-faces (match-pos decl-pos)
5341 (when (facep 'c-debug-decl-spot-face)
5342 `(c-save-buffer-state ((match-pos ,match-pos) (decl-pos ,decl-pos))
5343 (c-debug-add-face (max match-pos (point-min)) decl-pos
5344 'c-debug-decl-sws-face)
5345 (c-debug-add-face decl-pos (min (1+ decl-pos) (point-max))
5346 'c-debug-decl-spot-face))))
5347 (defmacro c-debug-remove-decl-spot-faces (beg end)
5348 (when (facep 'c-debug-decl-spot-face)
5349 `(c-save-buffer-state ()
5350 (c-debug-remove-face ,beg ,end 'c-debug-decl-spot-face)
5351 (c-debug-remove-face ,beg ,end 'c-debug-decl-sws-face))))
5353 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
5354 ;; Machinery for determining when we're at top level (this including being
5355 ;; directly inside a class or namespace, etc.)
5357 ;; We maintain a stack of brace depths in structures like classes and
5358 ;; namespaces. The car of this structure, when non-nil, indicates that the
5359 ;; associated position is within a template (etc.) structure, and the value is
5360 ;; the position where the (outermost) template ends. The other elements in
5361 ;; the structure are stacked elements, one each for each enclosing "top level"
5362 ;; structure.
5364 ;; At the very outermost level, the value of the stack would be (nil 1), the
5365 ;; "1" indicating an enclosure in a notional all-enclosing block. After
5366 ;; passing a keyword such as "namespace", the value would become (nil 0 1).
5367 ;; At this point, passing a semicolon would cause the 0 to be dropped from the
5368 ;; stack (at any other time, a semicolon is ignored). Alternatively, on
5369 ;; passing an opening brace, the stack would become (nil 1 1). Each opening
5370 ;; brace passed causes the cadr to be incremented, and passing closing braces
5371 ;; causes it to be decremented until it reaches 1. On passing a closing brace
5372 ;; when the cadr of the stack is at 1, this causes it to be removed from the
5373 ;; stack, the corresponding namespace (etc.) structure having been closed.
5375 ;; There is a special stack value -1 which means the C++ colon operator
5376 ;; introducing a list of inherited classes has just been parsed. The value
5377 ;; persists on the stack until the next open brace or semicolon.
5379 ;; When the car of the stack is non-nil, i.e. when we're in a template (etc.)
5380 ;; structure, braces are not counted. The counting resumes only after passing
5381 ;; the template's closing position, which is recorded in the car of the stack.
5383 ;; The test for being at top level consists of the cadr being 0 or 1.
5385 ;; The values of this stack throughout a buffer are cached in a simple linear
5386 ;; cache, every 5000 characters.
5388 ;; Note to maintainers: This cache mechanism is MUCH faster than recalculating
5389 ;; the stack at every entry to `c-find-decl-spots' using `c-at-toplevel-p' or
5390 ;; the like.
5391 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
5392 ;; The approximate interval at which we cache the value of the brace stack.
5393 (defconst c-bs-interval 5000)
5394 ;; The list of cached values of the brace stack. Each value in the list is a
5395 ;; cons of the position it is valid for and the value of the stack as
5396 ;; described above.
5397 (defvar c-bs-cache nil)
5398 (make-variable-buffer-local 'c-bs-cache)
5399 ;; The position of the buffer at and below which entries in `c-bs-cache' are
5400 ;; valid.
5401 (defvar c-bs-cache-limit 1)
5402 (make-variable-buffer-local 'c-bs-cache-limit)
5403 ;; The previous buffer position for which the brace stack value was
5404 ;; determined.
5405 (defvar c-bs-prev-pos most-positive-fixnum)
5406 (make-variable-buffer-local 'c-bs-prev-pos)
5407 ;; The value of the brace stack at `c-bs-prev-pos'.
5408 (defvar c-bs-prev-stack nil)
5409 (make-variable-buffer-local 'c-bs-prev-stack)
5411 (defun c-init-bs-cache ()
5412 ;; Initialize the cache in `c-bs-cache' and related variables.
5413 (setq c-bs-cache nil
5414 c-bs-cache-limit 1
5415 c-bs-prev-pos most-positive-fixnum
5416 c-bs-prev-stack nil))
5418 (defun c-truncate-bs-cache (pos &rest _ignore)
5419 ;; Truncate the upper bound of the cache `c-bs-cache' to POS, if it is
5420 ;; higher than that position. This is called as either a before- or
5421 ;; after-change-function.
5422 (setq c-bs-cache-limit
5423 (min c-bs-cache-limit pos)))
5425 (defun c-update-brace-stack (stack from to)
5426 ;; Given a brace-stack which has the value STACK at position FROM, update it
5427 ;; to its value at position TO, where TO is after (or equal to) FROM.
5428 ;; Return a cons of either TO (if it is outside a literal) and this new
5429 ;; value, or of the next position after TO outside a literal and the new
5430 ;; value.
5431 (let (match kwd-sym (prev-match-pos 1)
5432 (s (cdr stack))
5433 (bound-<> (car stack)))
5434 (save-excursion
5435 (cond
5436 ((and bound-<> (<= to bound-<>))
5437 (goto-char to)) ; Nothing to do.
5438 (bound-<>
5439 (goto-char bound-<>)
5440 (setq bound-<> nil))
5441 (t (goto-char from)))
5442 (while (and (< (point) to)
5443 (c-syntactic-re-search-forward
5444 (if (<= (car s) 0)
5445 c-brace-stack-thing-key
5446 c-brace-stack-no-semi-key)
5447 to 'after-literal)
5448 (> (point) prev-match-pos)) ; prevent infinite loop.
5449 (setq prev-match-pos (point))
5450 (setq match (match-string-no-properties 1)
5451 kwd-sym (c-keyword-sym match))
5452 (cond
5453 ((and (equal match "{")
5454 (progn (backward-char)
5455 (prog1 (looking-at "\\s(")
5456 (forward-char))))
5457 (setq s (if s
5458 (cons (if (<= (car s) 0)
5460 (1+ (car s)))
5461 (cdr s))
5462 (list 1))))
5463 ((and (equal match "}")
5464 (progn (backward-char)
5465 (prog1 (looking-at "\\s)")
5466 (forward-char))))
5467 (setq s
5468 (cond
5469 ((and s (> (car s) 1))
5470 (cons (1- (car s)) (cdr s)))
5471 ((and (cdr s) (eq (car s) 1))
5472 (cdr s))
5473 (t s))))
5474 ((and (equal match "<")
5475 (progn (backward-char)
5476 (prog1 (looking-at "\\s(")
5477 (forward-char))))
5478 (backward-char)
5479 (if (c-forward-<>-arglist nil) ; Should always work.
5480 (when (> (point) to)
5481 (setq bound-<> (point)))
5482 (forward-char)))
5483 ((and (equal match ":")
5485 (eq (car s) 0))
5486 (setq s (cons -1 (cdr s))))
5487 ((and (equal match ",")
5488 (eq (car s) -1))) ; at "," in "class foo : bar, ..."
5489 ((member match '(";" "," ")"))
5490 (when (and s (cdr s) (<= (car s) 0))
5491 (setq s (cdr s))))
5492 ((c-keyword-member kwd-sym 'c-flat-decl-block-kwds)
5493 (push 0 s))))
5494 ;; The failing `c-syntactic-re-search-forward' may have left us in the
5495 ;; middle of a token, which might be a significant token. Fix this!
5496 (c-beginning-of-current-token)
5497 (cons (point)
5498 (cons bound-<> s)))))
5500 (defun c-brace-stack-at (here)
5501 ;; Given a buffer position HERE, Return the value of the brace stack there.
5502 (save-excursion
5503 (save-restriction
5504 (widen)
5505 (let ((c c-bs-cache)
5506 (can-use-prev (<= c-bs-prev-pos c-bs-cache-limit))
5507 elt stack pos npos high-elt)
5508 ;; Trim the cache to take account of buffer changes.
5509 (while (and c
5510 (> (caar c) c-bs-cache-limit))
5511 (setq c (cdr c)))
5512 (setq c-bs-cache c)
5514 (while (and c
5515 (> (caar c) here))
5516 (setq high-elt (car c))
5517 (setq c (cdr c)))
5518 (setq pos (or (and c (caar c))
5519 (point-min)))
5521 (setq elt (if c
5522 (car c)
5523 (cons (point-min)
5524 (cons nil (list 1)))))
5525 (when (not high-elt)
5526 (setq stack (cdr elt))
5527 (while
5528 ;; Add an element to `c-state-semi-nonlit-pos-cache' each iteration.
5529 (<= (setq npos (+ pos c-bs-interval)) here)
5530 (setq elt (c-update-brace-stack stack pos npos))
5531 (setq npos (car elt))
5532 (setq stack (cdr elt))
5533 (unless (eq npos (point-max)) ; NPOS could be in a literal at EOB.
5534 (setq c-bs-cache (cons elt c-bs-cache)))
5535 (setq pos npos)))
5537 (if (> pos c-bs-cache-limit)
5538 (setq c-bs-cache-limit pos))
5540 ;; Can we just use the previous value?
5541 (if (and can-use-prev
5542 (<= c-bs-prev-pos here)
5543 (> c-bs-prev-pos (car elt)))
5544 (setq pos c-bs-prev-pos
5545 stack c-bs-prev-stack)
5546 (setq pos (car elt)
5547 stack (cdr elt)))
5548 (if (> here c-bs-cache-limit)
5549 (setq c-bs-cache-limit here))
5550 (setq elt (c-update-brace-stack stack pos here)
5551 c-bs-prev-pos (car elt)
5552 c-bs-prev-stack (cdr elt))))))
5554 (defun c-bs-at-toplevel-p (here)
5555 ;; Is position HERE at the top level, as indicated by the brace stack?
5556 (let ((stack (c-brace-stack-at here)))
5557 (or (null stack) ; Probably unnecessary.
5558 (<= (cadr stack) 1))))
5560 (defmacro c-find-decl-prefix-search ()
5561 ;; Macro used inside `c-find-decl-spots'. It ought to be a defun,
5562 ;; but it contains lots of free variables that refer to things
5563 ;; inside `c-find-decl-spots'. The point is left at `cfd-match-pos'
5564 ;; if there is a match, otherwise at `cfd-limit'.
5566 ;; The macro moves point forward to the next putative start of a declaration
5567 ;; or cfd-limit. This decl start is the next token after a "declaration
5568 ;; prefix". The declaration prefix is the earlier of `cfd-prop-match' and
5569 ;; `cfd-re-match'. `cfd-match-pos' is set to the decl prefix.
5571 ;; This macro might do hidden buffer changes.
5573 '(progn
5574 ;; Find the next property match position if we haven't got one already.
5575 (unless cfd-prop-match
5576 (save-excursion
5577 (while (progn
5578 (goto-char (c-next-single-property-change
5579 (point) 'c-type nil cfd-limit))
5580 (and (< (point) cfd-limit)
5581 (not (eq (c-get-char-property (1- (point)) 'c-type)
5582 'c-decl-end)))))
5583 (setq cfd-prop-match (point))))
5585 ;; Find the next `c-decl-prefix-or-start-re' match if we haven't
5586 ;; got one already.
5587 (unless cfd-re-match
5589 (if (> cfd-re-match-end (point))
5590 (goto-char cfd-re-match-end))
5592 ;; Each time round, the next `while' moves forward over a pseudo match
5593 ;; of `c-decl-prefix-or-start-re' which is either inside a literal, or
5594 ;; is a ":" not preceded by "public", etc.. `cfd-re-match' and
5595 ;; `cfd-re-match-end' get set.
5596 (while
5597 (progn
5598 (setq cfd-re-match-end (re-search-forward c-decl-prefix-or-start-re
5599 cfd-limit 'move))
5600 (cond
5601 ((null cfd-re-match-end)
5602 ;; No match. Finish up and exit the loop.
5603 (setq cfd-re-match cfd-limit)
5604 nil)
5605 ((c-got-face-at
5606 (if (setq cfd-re-match (match-end 1))
5607 ;; Matched the end of a token preceding a decl spot.
5608 (progn
5609 (goto-char cfd-re-match)
5610 (1- cfd-re-match))
5611 ;; Matched a token that start a decl spot.
5612 (goto-char (match-beginning 0))
5613 (point))
5614 c-literal-faces)
5615 ;; Pseudo match inside a comment or string literal. Skip out
5616 ;; of comments and string literals.
5617 (while (progn
5618 (goto-char (c-next-single-property-change
5619 (point) 'face nil cfd-limit))
5620 (and (< (point) cfd-limit)
5621 (c-got-face-at (point) c-literal-faces))))
5622 t) ; Continue the loop over pseudo matches.
5623 ((and c-opt-identifier-concat-key
5624 (match-string 1)
5625 (save-excursion
5626 (goto-char (match-beginning 1))
5627 (save-match-data
5628 (looking-at c-opt-identifier-concat-key))))
5629 ;; Found, e.g., "::" in C++
5631 ((and (match-string 1)
5632 (string= (match-string 1) ":")
5633 (save-excursion
5634 (or (/= (c-backward-token-2 2) 0) ; no search limit. :-(
5635 (not (looking-at c-decl-start-colon-kwd-re)))))
5636 ;; Found a ":" which isn't part of "public:", etc.
5638 (t nil)))) ;; Found a real match. Exit the pseudo-match loop.
5640 ;; If our match was at the decl start, we have to back up over the
5641 ;; preceding syntactic ws to set `cfd-match-pos' and to catch
5642 ;; any decl spots in the syntactic ws.
5643 (unless cfd-re-match
5644 (c-backward-syntactic-ws)
5645 (setq cfd-re-match (point))))
5647 ;; Choose whichever match is closer to the start.
5648 (if (< cfd-re-match cfd-prop-match)
5649 (setq cfd-match-pos cfd-re-match
5650 cfd-re-match nil)
5651 (setq cfd-match-pos cfd-prop-match
5652 cfd-prop-match nil))
5653 (setq cfd-top-level (c-bs-at-toplevel-p cfd-match-pos))
5655 (goto-char cfd-match-pos)
5657 (when (< cfd-match-pos cfd-limit)
5658 ;; Skip forward past comments only so we don't skip macros.
5659 (c-forward-comments)
5660 ;; Set the position to continue at. We can avoid going over
5661 ;; the comments skipped above a second time, but it's possible
5662 ;; that the comment skipping has taken us past `cfd-prop-match'
5663 ;; since the property might be used inside comments.
5664 (setq cfd-continue-pos (if cfd-prop-match
5665 (min cfd-prop-match (point))
5666 (point))))))
5668 (defun c-find-decl-spots (cfd-limit cfd-decl-re cfd-face-checklist cfd-fun)
5669 ;; Call CFD-FUN for each possible spot for a declaration, cast or
5670 ;; label from the point to CFD-LIMIT.
5672 ;; CFD-FUN is called with point at the start of the spot. It's passed three
5673 ;; arguments: The first is the end position of the token preceding the spot,
5674 ;; or 0 for the implicit match at bob. The second is a flag that is t when
5675 ;; the match is inside a macro. The third is a flag that is t when the
5676 ;; match is at "top level", i.e. outside any brace block, or directly inside
5677 ;; a class or namespace, etc. Point should be moved forward by at least one
5678 ;; token.
5680 ;; If CFD-FUN adds `c-decl-end' properties somewhere below the current spot,
5681 ;; it should return non-nil to ensure that the next search will find them.
5683 ;; Such a spot is:
5684 ;; o The first token after bob.
5685 ;; o The first token after the end of submatch 1 in
5686 ;; `c-decl-prefix-or-start-re' when that submatch matches. This
5687 ;; submatch is typically a (L or R) brace or paren, a ;, or a ,.
5688 ;; o The start of each `c-decl-prefix-or-start-re' match when
5689 ;; submatch 1 doesn't match. This is, for example, the keyword
5690 ;; "class" in Pike.
5691 ;; o The start of a previously recognized declaration; "recognized"
5692 ;; means that the last char of the previous token has a `c-type'
5693 ;; text property with the value `c-decl-end'; this only holds
5694 ;; when `c-type-decl-end-used' is set.
5696 ;; Only a spot that match CFD-DECL-RE and whose face is in the
5697 ;; CFD-FACE-CHECKLIST list causes CFD-FUN to be called. The face
5698 ;; check is disabled if CFD-FACE-CHECKLIST is nil.
5700 ;; If the match is inside a macro then the buffer is narrowed to the
5701 ;; end of it, so that CFD-FUN can investigate the following tokens
5702 ;; without matching something that begins inside a macro and ends
5703 ;; outside it. It's to avoid this work that the CFD-DECL-RE and
5704 ;; CFD-FACE-CHECKLIST checks exist.
5706 ;; The spots are visited approximately in order from top to bottom.
5707 ;; It's however the positions where `c-decl-prefix-or-start-re'
5708 ;; matches and where `c-decl-end' properties are found that are in
5709 ;; order. Since the spots often are at the following token, they
5710 ;; might be visited out of order insofar as more spots are reported
5711 ;; later on within the syntactic whitespace between the match
5712 ;; positions and their spots.
5714 ;; It's assumed that comments and strings are fontified in the
5715 ;; searched range.
5717 ;; This is mainly used in fontification, and so has an elaborate
5718 ;; cache to handle repeated calls from the same start position; see
5719 ;; the variables above.
5721 ;; All variables in this function begin with `cfd-' to avoid name
5722 ;; collision with the (dynamically bound) variables used in CFD-FUN.
5724 ;; This function might do hidden buffer changes.
5726 (let ((cfd-start-pos (point)) ; never changed
5727 (cfd-buffer-end (point-max))
5728 ;; The end of the token preceding the decl spot last found
5729 ;; with `c-decl-prefix-or-start-re'. `cfd-limit' if there's
5730 ;; no match.
5731 cfd-re-match
5732 ;; The end position of the last `c-decl-prefix-or-start-re'
5733 ;; match. If this is greater than `cfd-continue-pos', the
5734 ;; next regexp search is started here instead.
5735 (cfd-re-match-end (point-min))
5736 ;; The end of the last `c-decl-end' found by
5737 ;; `c-find-decl-prefix-search'. `cfd-limit' if there's no
5738 ;; match. If searching for the property isn't needed then we
5739 ;; disable it by setting it to `cfd-limit' directly.
5740 (cfd-prop-match (unless c-type-decl-end-used cfd-limit))
5741 ;; The end of the token preceding the decl spot last found by
5742 ;; `c-find-decl-prefix-search'. 0 for the implicit match at
5743 ;; bob. `cfd-limit' if there's no match. In other words,
5744 ;; this is the minimum of `cfd-re-match' and `cfd-prop-match'.
5745 (cfd-match-pos cfd-limit)
5746 ;; The position to continue searching at.
5747 cfd-continue-pos
5748 ;; The position of the last "real" token we've stopped at.
5749 ;; This can be greater than `cfd-continue-pos' when we get
5750 ;; hits inside macros or at `c-decl-end' positions inside
5751 ;; comments.
5752 (cfd-token-pos 0)
5753 ;; The end position of the last entered macro.
5754 (cfd-macro-end 0)
5755 ;; Whether the last position returned from `c-find-decl-prefix-search'
5756 ;; is at the top-level (including directly in a class or namespace,
5757 ;; etc.).
5758 (cfd-top-level (c-bs-at-toplevel-p (point))))
5760 ;; Initialize by finding a syntactically relevant start position
5761 ;; before the point, and do the first `c-decl-prefix-or-start-re'
5762 ;; search unless we're at bob.
5764 (let (start-in-literal start-in-macro syntactic-pos)
5765 ;; Must back up a bit since we look for the end of the previous
5766 ;; statement or declaration, which is earlier than the first
5767 ;; returned match.
5769 ;; This `cond' moves back over any literals or macros. It has special
5770 ;; handling for when the region being searched is entirely within a
5771 ;; macro. It sets `cfd-continue-pos' (unless we've reached
5772 ;; `cfd-limit').
5773 (cond
5774 ;; First we need to move to a syntactically relevant position.
5775 ;; Begin by backing out of comment or string literals.
5777 ;; This arm of the cond actually triggers if we're in a literal,
5778 ;; and cfd-limit is at most at BONL.
5779 ((and
5780 ;; This arm of the `and' moves backwards out of a literal when
5781 ;; the face at point is a literal face. In this case, its value
5782 ;; is always non-nil.
5783 (when (c-got-face-at (point) c-literal-faces)
5784 ;; Try to use the faces to back up to the start of the
5785 ;; literal. FIXME: What if the point is on a declaration
5786 ;; inside a comment?
5787 (while (and (not (bobp))
5788 (c-got-face-at (1- (point)) c-literal-faces))
5789 (goto-char (previous-single-property-change
5790 (point) 'face nil (point-min))))
5792 ;; XEmacs doesn't fontify the quotes surrounding string
5793 ;; literals.
5794 (and (featurep 'xemacs)
5795 (eq (get-text-property (point) 'face)
5796 'font-lock-string-face)
5797 (not (bobp))
5798 (progn (backward-char)
5799 (not (looking-at c-string-limit-regexp)))
5800 (forward-char))
5802 ;; Don't trust the literal to contain only literal faces
5803 ;; (the font lock package might not have fontified the
5804 ;; start of it at all, for instance) so check that we have
5805 ;; arrived at something that looks like a start or else
5806 ;; resort to `c-literal-limits'.
5807 (unless (looking-at c-literal-start-regexp)
5808 (let ((lit-start (c-literal-start)))
5809 (if lit-start (goto-char lit-start)))
5812 (setq start-in-literal (point))) ; end of `and' arm.
5814 ;; The start is in a literal. If the limit is in the same
5815 ;; one we don't have to find a syntactic position etc. We
5816 ;; only check that if the limit is at or before bonl to save
5817 ;; time; it covers the by far most common case when font-lock
5818 ;; refontifies the current line only.
5819 (<= cfd-limit (c-point 'bonl cfd-start-pos))
5820 (save-excursion
5821 (goto-char cfd-start-pos)
5822 (while (progn
5823 (goto-char (c-next-single-property-change
5824 (point) 'face nil cfd-limit))
5825 (and (< (point) cfd-limit)
5826 (c-got-face-at (point) c-literal-faces))))
5827 (= (point) cfd-limit))) ; end of `cond' arm condition
5829 ;; Completely inside a literal. Set up variables to trig the
5830 ;; (< cfd-continue-pos cfd-start-pos) case below and it'll
5831 ;; find a suitable start position.
5832 (setq cfd-continue-pos start-in-literal)) ; end of `cond' arm
5834 ;; Check if the region might be completely inside a macro, to
5835 ;; optimize that like the completely-inside-literal above.
5836 ((save-excursion
5837 (and (= (forward-line 1) 0)
5838 (bolp) ; forward-line has funny behavior at eob.
5839 (>= (point) cfd-limit)
5840 (progn (backward-char)
5841 (eq (char-before) ?\\))))
5842 ;; (Maybe) completely inside a macro. Only need to trig the
5843 ;; (< cfd-continue-pos cfd-start-pos) case below to make it
5844 ;; set things up.
5845 (setq cfd-continue-pos (1- cfd-start-pos)
5846 start-in-macro t))
5848 ;; The default arm of the `cond' moves back over any macro we're in
5849 ;; and over any syntactic WS. It sets `c-find-decl-syntactic-pos'.
5851 ;; Back out of any macro so we don't miss any declaration
5852 ;; that could follow after it.
5853 (when (c-beginning-of-macro)
5854 (setq start-in-macro t))
5856 ;; Now we're at a proper syntactically relevant position so we
5857 ;; can use the cache. But first clear it if it applied
5858 ;; further down.
5859 (c-invalidate-find-decl-cache cfd-start-pos)
5861 (setq syntactic-pos (point))
5862 (unless (eq syntactic-pos c-find-decl-syntactic-pos)
5863 ;; Don't have to do this if the cache is relevant here,
5864 ;; typically if the same line is refontified again. If
5865 ;; we're just some syntactic whitespace further down we can
5866 ;; still use the cache to limit the skipping.
5867 (c-backward-syntactic-ws c-find-decl-syntactic-pos))
5869 ;; If we hit `c-find-decl-syntactic-pos' and
5870 ;; `c-find-decl-match-pos' is set then we install the cached
5871 ;; values. If we hit `c-find-decl-syntactic-pos' and
5872 ;; `c-find-decl-match-pos' is nil then we know there's no decl
5873 ;; prefix in the whitespace before `c-find-decl-syntactic-pos'
5874 ;; and so we can continue the search from this point. If we
5875 ;; didn't hit `c-find-decl-syntactic-pos' then we're now in
5876 ;; the right spot to begin searching anyway.
5877 (if (and (eq (point) c-find-decl-syntactic-pos)
5878 c-find-decl-match-pos)
5879 (setq cfd-match-pos c-find-decl-match-pos
5880 cfd-continue-pos syntactic-pos)
5882 (setq c-find-decl-syntactic-pos syntactic-pos)
5884 (when (if (bobp)
5885 ;; Always consider bob a match to get the first
5886 ;; declaration in the file. Do this separately instead of
5887 ;; letting `c-decl-prefix-or-start-re' match bob, so that
5888 ;; regexp always can consume at least one character to
5889 ;; ensure that we won't get stuck in an infinite loop.
5890 (setq cfd-re-match 0)
5891 (backward-char)
5892 (c-beginning-of-current-token)
5893 (< (point) cfd-limit))
5894 ;; Do an initial search now. In the bob case above it's
5895 ;; only done to search for a `c-decl-end' spot.
5896 (c-find-decl-prefix-search)) ; sets cfd-continue-pos
5898 (setq c-find-decl-match-pos (and (< cfd-match-pos cfd-start-pos)
5899 cfd-match-pos))))) ; end of `cond'
5901 ;; Advance `cfd-continue-pos' if it's before the start position.
5902 ;; The closest continue position that might have effect at or
5903 ;; after the start depends on what we started in. This also
5904 ;; finds a suitable start position in the special cases when the
5905 ;; region is completely within a literal or macro.
5906 (when (and cfd-continue-pos (< cfd-continue-pos cfd-start-pos))
5908 (cond
5909 (start-in-macro
5910 ;; If we're in a macro then it's the closest preceding token
5911 ;; in the macro. Check this before `start-in-literal',
5912 ;; since if we're inside a literal in a macro, the preceding
5913 ;; token is earlier than any `c-decl-end' spot inside the
5914 ;; literal (comment).
5915 (goto-char (or start-in-literal cfd-start-pos))
5916 ;; The only syntactic ws in macros are comments.
5917 (c-backward-comments)
5918 (backward-char)
5919 (c-beginning-of-current-token))
5921 (start-in-literal
5922 ;; If we're in a comment it can only be the closest
5923 ;; preceding `c-decl-end' position within that comment, if
5924 ;; any. Go back to the beginning of such a property so that
5925 ;; `c-find-decl-prefix-search' will find the end of it.
5926 ;; (Can't stop at the end and install it directly on
5927 ;; `cfd-prop-match' since that variable might be cleared
5928 ;; after `cfd-fun' below.)
5930 ;; Note that if the literal is a string then the property
5931 ;; search will simply skip to the beginning of it right
5932 ;; away.
5933 (if (not c-type-decl-end-used)
5934 (goto-char start-in-literal)
5935 (goto-char cfd-start-pos)
5936 (while (progn
5937 (goto-char (previous-single-property-change
5938 (point) 'c-type nil start-in-literal))
5939 (and (> (point) start-in-literal)
5940 (not (eq (c-get-char-property (point) 'c-type)
5941 'c-decl-end))))))
5943 (when (= (point) start-in-literal)
5944 ;; Didn't find any property inside the comment, so we can
5945 ;; skip it entirely. (This won't skip past a string, but
5946 ;; that'll be handled quickly by the next
5947 ;; `c-find-decl-prefix-search' anyway.)
5948 (c-forward-single-comment)
5949 (if (> (point) cfd-limit)
5950 (goto-char cfd-limit))))
5953 ;; If we started in normal code, the only match that might
5954 ;; apply before the start is what we already got in
5955 ;; `cfd-match-pos' so we can continue at the start position.
5956 ;; (Note that we don't get here if the first match is below
5957 ;; it.)
5958 (goto-char cfd-start-pos))) ; end of `cond'
5960 ;; Delete found matches if they are before our new continue
5961 ;; position, so that `c-find-decl-prefix-search' won't back up
5962 ;; to them later on.
5963 (setq cfd-continue-pos (point))
5964 (when (and cfd-re-match (< cfd-re-match cfd-continue-pos))
5965 (setq cfd-re-match nil))
5966 (when (and cfd-prop-match (< cfd-prop-match cfd-continue-pos))
5967 (setq cfd-prop-match nil))) ; end of `when'
5969 (if syntactic-pos
5970 ;; This is the normal case and we got a proper syntactic
5971 ;; position. If there's a match then it's always outside
5972 ;; macros and comments, so advance to the next token and set
5973 ;; `cfd-token-pos'. The loop below will later go back using
5974 ;; `cfd-continue-pos' to fix declarations inside the
5975 ;; syntactic ws.
5976 (when (and cfd-match-pos (< cfd-match-pos syntactic-pos))
5977 (goto-char syntactic-pos)
5978 (c-forward-syntactic-ws)
5979 (and cfd-continue-pos
5980 (< cfd-continue-pos (point))
5981 (setq cfd-token-pos (point))))
5983 ;; Have one of the special cases when the region is completely
5984 ;; within a literal or macro. `cfd-continue-pos' is set to a
5985 ;; good start position for the search, so do it.
5986 (c-find-decl-prefix-search)))
5988 ;; Now loop, one decl spot per iteration. We already have the first
5989 ;; match in `cfd-match-pos'.
5990 (while (progn
5991 ;; Go forward over "false matches", one per iteration.
5992 (while (and
5993 (< cfd-match-pos cfd-limit)
5996 ;; Kludge to filter out matches on the "<" that
5997 ;; aren't open parens, for the sake of languages
5998 ;; that got `c-recognize-<>-arglists' set.
5999 (and (eq (char-before cfd-match-pos) ?<)
6000 (not (c-get-char-property (1- cfd-match-pos)
6001 'syntax-table)))
6003 ;; If `cfd-continue-pos' is less or equal to
6004 ;; `cfd-token-pos', we've got a hit inside a macro
6005 ;; that's in the syntactic whitespace before the last
6006 ;; "real" declaration we've checked. If they're equal
6007 ;; we've arrived at the declaration a second time, so
6008 ;; there's nothing to do.
6009 (= cfd-continue-pos cfd-token-pos)
6011 (progn
6012 ;; If `cfd-continue-pos' is less than `cfd-token-pos'
6013 ;; we're still searching for declarations embedded in
6014 ;; the syntactic whitespace. In that case we need
6015 ;; only to skip comments and not macros, since they
6016 ;; can't be nested, and that's already been done in
6017 ;; `c-find-decl-prefix-search'.
6018 (when (> cfd-continue-pos cfd-token-pos)
6019 (c-forward-syntactic-ws)
6020 (setq cfd-token-pos (point)))
6022 ;; Continue if the following token fails the
6023 ;; CFD-DECL-RE and CFD-FACE-CHECKLIST checks.
6024 (when (or (>= (point) cfd-limit)
6025 (not (looking-at cfd-decl-re))
6026 (and cfd-face-checklist
6027 (not (c-got-face-at
6028 (point) cfd-face-checklist))))
6029 (goto-char cfd-continue-pos)
6030 t)))
6032 (< (point) cfd-limit)) ; end of "false matches" condition
6033 (c-find-decl-prefix-search)) ; end of "false matches" loop
6035 (< (point) cfd-limit)) ; end of condition for "decl-spot" while
6037 (when (and
6038 (>= (point) cfd-start-pos)
6040 (progn
6041 ;; Narrow to the end of the macro if we got a hit inside
6042 ;; one, to avoid recognizing things that start inside the
6043 ;; macro and end outside it.
6044 (when (> cfd-match-pos cfd-macro-end)
6045 ;; Not in the same macro as in the previous round.
6046 (save-excursion
6047 (goto-char cfd-match-pos)
6048 (setq cfd-macro-end
6049 (if (save-excursion (and (c-beginning-of-macro)
6050 (< (point) cfd-match-pos)))
6051 (progn (c-end-of-macro)
6052 (point))
6053 0))))
6055 (if (zerop cfd-macro-end)
6057 (if (> cfd-macro-end (point))
6058 (progn (narrow-to-region (point-min) cfd-macro-end)
6060 ;; The matched token was the last thing in the macro,
6061 ;; so the whole match is bogus.
6062 (setq cfd-macro-end 0)
6063 nil)))) ; end of when condition
6065 (when (> cfd-macro-end 0)
6066 (setq cfd-top-level nil)) ; In a macro is "never" at top level.
6067 (c-debug-put-decl-spot-faces cfd-match-pos (point))
6068 (if (funcall cfd-fun cfd-match-pos (/= cfd-macro-end 0) cfd-top-level)
6069 (setq cfd-prop-match nil))
6071 (when (/= cfd-macro-end 0)
6072 ;; Restore limits if we did macro narrowing above.
6073 (narrow-to-region (point-min) cfd-buffer-end)))
6075 (goto-char cfd-continue-pos)
6076 (if (= cfd-continue-pos cfd-limit)
6077 (setq cfd-match-pos cfd-limit)
6078 (c-find-decl-prefix-search))))) ; Moves point, sets cfd-continue-pos,
6079 ; cfd-match-pos, etc.
6082 ;; A cache for found types.
6084 ;; Buffer local variable that contains an obarray with the types we've
6085 ;; found. If a declaration is recognized somewhere we record the
6086 ;; fully qualified identifier in it to recognize it as a type
6087 ;; elsewhere in the file too. This is not accurate since we do not
6088 ;; bother with the scoping rules of the languages, but in practice the
6089 ;; same name is seldom used as both a type and something else in a
6090 ;; file, and we only use this as a last resort in ambiguous cases (see
6091 ;; `c-forward-decl-or-cast-1').
6093 ;; Not every type need be in this cache. However, things which have
6094 ;; ceased to be types must be removed from it.
6096 ;; Template types in C++ are added here too but with the template
6097 ;; arglist replaced with "<>" in references or "<" for the one in the
6098 ;; primary type. E.g. the type "Foo<A,B>::Bar<C>" is stored as
6099 ;; "Foo<>::Bar<". This avoids storing very long strings (since C++
6100 ;; template specs can be fairly sized programs in themselves) and
6101 ;; improves the hit ratio (it's a type regardless of the template
6102 ;; args; it's just not the same type, but we're only interested in
6103 ;; recognizing types, not telling distinct types apart). Note that
6104 ;; template types in references are added here too; from the example
6105 ;; above there will also be an entry "Foo<".
6106 (defvar c-found-types nil)
6107 (make-variable-buffer-local 'c-found-types)
6109 (defsubst c-clear-found-types ()
6110 ;; Clears `c-found-types'.
6111 (setq c-found-types
6112 (make-hash-table :test #'equal :weakness nil)))
6114 (defun c-add-type (from to)
6115 ;; Add the given region as a type in `c-found-types'. If the region
6116 ;; doesn't match an existing type but there is a type which is equal
6117 ;; to the given one except that the last character is missing, then
6118 ;; the shorter type is removed. That's done to avoid adding all
6119 ;; prefixes of a type as it's being entered and font locked. This
6120 ;; doesn't cover cases like when characters are removed from a type
6121 ;; or added in the middle. We'd need the position of point when the
6122 ;; font locking is invoked to solve this well.
6124 ;; This function might do hidden buffer changes.
6125 (let ((type (c-syntactic-content from to c-recognize-<>-arglists)))
6126 (unless (gethash type c-found-types)
6127 (remhash (substring type 0 -1) c-found-types)
6128 (puthash type t c-found-types))))
6130 (defun c-unfind-type (name)
6131 ;; Remove the "NAME" from c-found-types, if present.
6132 (remhash name c-found-types))
6134 (defsubst c-check-type (from to)
6135 ;; Return non-nil if the given region contains a type in
6136 ;; `c-found-types'.
6138 ;; This function might do hidden buffer changes.
6139 (gethash (c-syntactic-content from to c-recognize-<>-arglists) c-found-types))
6141 (defun c-list-found-types ()
6142 ;; Return all the types in `c-found-types' as a sorted list of
6143 ;; strings.
6144 (let (type-list)
6145 (maphash (lambda (type _)
6146 (setq type-list (cons type type-list)))
6147 c-found-types)
6148 (sort type-list 'string-lessp)))
6150 ;; Shut up the byte compiler.
6151 (defvar c-maybe-stale-found-type)
6153 (defun c-trim-found-types (beg end _old-len)
6154 ;; An after change function which, in conjunction with the info in
6155 ;; c-maybe-stale-found-type (set in c-before-change), removes a type
6156 ;; from `c-found-types', should this type have become stale. For
6157 ;; example, this happens to "foo" when "foo \n bar();" becomes
6158 ;; "foo(); \n bar();". Such stale types, if not removed, foul up
6159 ;; the fontification.
6161 ;; Have we, perhaps, added non-ws characters to the front/back of a found
6162 ;; type?
6163 (when (> end beg)
6164 (save-excursion
6165 (when (< end (point-max))
6166 (goto-char end)
6167 (if (and (c-beginning-of-current-token) ; only moves when we started in the middle
6168 (progn (goto-char end)
6169 (c-end-of-current-token)))
6170 (c-unfind-type (buffer-substring-no-properties
6171 end (point)))))
6172 (when (> beg (point-min))
6173 (goto-char beg)
6174 (if (and (c-end-of-current-token) ; only moves when we started in the middle
6175 (progn (goto-char beg)
6176 (c-beginning-of-current-token)))
6177 (c-unfind-type (buffer-substring-no-properties
6178 (point) beg))))))
6180 (if c-maybe-stale-found-type ; e.g. (c-decl-id-start "foo" 97 107 " (* ooka) " "o")
6181 (cond
6182 ;; Changing the amount of (already existing) whitespace - don't do anything.
6183 ((and (c-partial-ws-p beg end)
6184 (or (= beg end) ; removal of WS
6185 (string-match "^[ \t\n\r\f\v]*$" (nth 5 c-maybe-stale-found-type)))))
6187 ;; The syntactic relationship which defined a "found type" has been
6188 ;; destroyed.
6189 ((eq (car c-maybe-stale-found-type) 'c-decl-id-start)
6190 (c-unfind-type (cadr c-maybe-stale-found-type)))
6191 ;; ((eq (car c-maybe-stale-found-type) 'c-decl-type-start) FIXME!!!
6195 ;; Setting and removing syntax properties on < and > in languages (C++
6196 ;; and Java) where they can be template/generic delimiters as well as
6197 ;; their normal meaning of "less/greater than".
6199 ;; Normally, < and > have syntax 'punctuation'. When they are found to
6200 ;; be delimiters, they are marked as such with the category properties
6201 ;; c-<-as-paren-syntax, c->-as-paren-syntax respectively.
6203 ;; STRATEGY:
6205 ;; It is impossible to determine with certainty whether a <..> pair in
6206 ;; C++ is two comparison operators or is template delimiters, unless
6207 ;; one duplicates a lot of a C++ compiler. For example, the following
6208 ;; code fragment:
6210 ;; foo (a < b, c > d) ;
6212 ;; could be a function call with two integer parameters (each a
6213 ;; relational expression), or it could be a constructor for class foo
6214 ;; taking one parameter d of templated type "a < b, c >". They are
6215 ;; somewhat easier to distinguish in Java.
6217 ;; The strategy now (2010-01) adopted is to mark and unmark < and
6218 ;; > IN MATCHING PAIRS ONLY. [Previously, they were marked
6219 ;; individually when their context so indicated. This gave rise to
6220 ;; intractable problems when one of a matching pair was deleted, or
6221 ;; pulled into a literal.]
6223 ;; At each buffer change, the syntax-table properties are removed in a
6224 ;; before-change function and reapplied, when needed, in an
6225 ;; after-change function. It is far more important that the
6226 ;; properties get removed when they they are spurious than that they
6227 ;; be present when wanted.
6228 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
6229 (defun c-clear-<-pair-props (&optional pos)
6230 ;; POS (default point) is at a < character. If it is marked with
6231 ;; open paren syntax-table text property, remove the property,
6232 ;; together with the close paren property on the matching > (if
6233 ;; any).
6234 (save-excursion
6235 (if pos
6236 (goto-char pos)
6237 (setq pos (point)))
6238 (when (equal (c-get-char-property (point) 'syntax-table)
6239 c-<-as-paren-syntax)
6240 (with-syntax-table c-no-parens-syntax-table ; ignore unbalanced [,{,(,..
6241 (c-go-list-forward))
6242 (when (equal (c-get-char-property (1- (point)) 'syntax-table)
6243 c->-as-paren-syntax) ; should always be true.
6244 (c-unmark-<->-as-paren (1- (point))))
6245 (c-unmark-<->-as-paren pos))))
6247 (defun c-clear->-pair-props (&optional pos)
6248 ;; POS (default point) is at a > character. If it is marked with
6249 ;; close paren syntax-table property, remove the property, together
6250 ;; with the open paren property on the matching < (if any).
6251 (save-excursion
6252 (if pos
6253 (goto-char pos)
6254 (setq pos (point)))
6255 (when (equal (c-get-char-property (point) 'syntax-table)
6256 c->-as-paren-syntax)
6257 (with-syntax-table c-no-parens-syntax-table ; ignore unbalanced [,{,(,..
6258 (c-go-up-list-backward))
6259 (when (equal (c-get-char-property (point) 'syntax-table)
6260 c-<-as-paren-syntax) ; should always be true.
6261 (c-unmark-<->-as-paren (point)))
6262 (c-unmark-<->-as-paren pos))))
6264 (defun c-clear-<>-pair-props (&optional pos)
6265 ;; POS (default point) is at a < or > character. If it has an
6266 ;; open/close paren syntax-table property, remove this property both
6267 ;; from the current character and its partner (which will also be
6268 ;; thusly marked).
6269 (cond
6270 ((eq (char-after) ?\<)
6271 (c-clear-<-pair-props pos))
6272 ((eq (char-after) ?\>)
6273 (c-clear->-pair-props pos))
6274 (t (c-benign-error
6275 "c-clear-<>-pair-props called from wrong position"))))
6277 (defun c-clear-<-pair-props-if-match-after (lim &optional pos)
6278 ;; POS (default point) is at a < character. If it is both marked
6279 ;; with open/close paren syntax-table property, and has a matching >
6280 ;; (also marked) which is after LIM, remove the property both from
6281 ;; the current > and its partner. Return t when this happens, nil
6282 ;; when it doesn't.
6283 (save-excursion
6284 (if pos
6285 (goto-char pos)
6286 (setq pos (point)))
6287 (when (equal (c-get-char-property (point) 'syntax-table)
6288 c-<-as-paren-syntax)
6289 (with-syntax-table c-no-parens-syntax-table ; ignore unbalanced [,{,(,..
6290 (c-go-list-forward))
6291 (when (and (>= (point) lim)
6292 (equal (c-get-char-property (1- (point)) 'syntax-table)
6293 c->-as-paren-syntax)) ; should always be true.
6294 (c-unmark-<->-as-paren (1- (point)))
6295 (c-unmark-<->-as-paren pos))
6296 t)))
6298 (defun c-clear->-pair-props-if-match-before (lim &optional pos)
6299 ;; POS (default point) is at a > character. If it is both marked
6300 ;; with open/close paren syntax-table property, and has a matching <
6301 ;; (also marked) which is before LIM, remove the property both from
6302 ;; the current < and its partner. Return t when this happens, nil
6303 ;; when it doesn't.
6304 (save-excursion
6305 (if pos
6306 (goto-char pos)
6307 (setq pos (point)))
6308 (when (equal (c-get-char-property (point) 'syntax-table)
6309 c->-as-paren-syntax)
6310 (with-syntax-table c-no-parens-syntax-table ; ignore unbalanced [,{,(,..
6311 (c-go-up-list-backward))
6312 (when (and (<= (point) lim)
6313 (equal (c-get-char-property (point) 'syntax-table)
6314 c-<-as-paren-syntax)) ; should always be true.
6315 (c-unmark-<->-as-paren (point))
6316 (c-unmark-<->-as-paren pos))
6317 t)))
6319 ;; Set by c-common-init in cc-mode.el.
6320 (defvar c-new-BEG)
6321 (defvar c-new-END)
6322 ;; Set by c-after-change in cc-mode.el.
6323 (defvar c-old-BEG)
6324 (defvar c-old-END)
6326 (defun c-before-change-check-<>-operators (beg end)
6327 ;; Unmark certain pairs of "< .... >" which are currently marked as
6328 ;; template/generic delimiters. (This marking is via syntax-table text
6329 ;; properties), and expand the (c-new-BEG c-new-END) region to include all
6330 ;; unmarked < and > operators within the certain bounds (see below).
6332 ;; These pairs are those which are in the current "statement" (i.e.,
6333 ;; the region between the {, }, or ; before BEG and the one after
6334 ;; END), and which enclose any part of the interval (BEG END).
6336 ;; Note that in C++ (?and Java), template/generic parens cannot
6337 ;; enclose a brace or semicolon, so we use these as bounds on the
6338 ;; region we must work on.
6340 ;; This function is called from before-change-functions (via
6341 ;; c-get-state-before-change-functions). Thus the buffer is widened,
6342 ;; and point is undefined, both at entry and exit.
6344 ;; FIXME!!! This routine ignores the possibility of macros entirely.
6345 ;; 2010-01-29.
6346 (save-excursion
6347 (c-save-buffer-state
6348 ((beg-lit-start (progn (goto-char beg) (c-literal-start)))
6349 (end-lit-limits (progn (goto-char end) (c-literal-limits)))
6350 new-beg new-end beg-limit end-limit)
6351 ;; Locate the earliest < after the barrier before the changed region,
6352 ;; which isn't already marked as a paren.
6353 (goto-char (or beg-lit-start beg))
6354 (setq beg-limit (c-determine-limit 512))
6356 ;; Remove the syntax-table/category properties from each pertinent <...>
6357 ;; pair. Firstly, the ones with the < before beg and > after beg....
6358 (while (progn (c-syntactic-skip-backward "^;{}<" beg-limit)
6359 (eq (char-before) ?<))
6360 (c-backward-token-2)
6361 (when (eq (char-after) ?<)
6362 (c-clear-<-pair-props-if-match-after beg)
6363 (setq new-beg (point))))
6364 (c-forward-syntactic-ws)
6366 ;; ...Then the ones with < before end and > after end.
6367 (goto-char (if end-lit-limits (cdr end-lit-limits) end))
6368 (setq end-limit (c-determine-+ve-limit 512))
6369 (while (and (c-syntactic-re-search-forward "[;{}>]" end-limit 'end)
6370 (eq (char-before) ?>))
6371 (c-end-of-current-token)
6372 (when (eq (char-before) ?>)
6373 (c-clear->-pair-props-if-match-before end (1- (point)))
6374 (setq new-end (point))))
6375 (c-backward-syntactic-ws)
6377 ;; Extend the fontification region, if needed.
6378 (and new-beg
6379 (< new-beg c-new-BEG)
6380 (setq c-new-BEG new-beg))
6381 (and new-end
6382 (> new-end c-new-END)
6383 (setq c-new-END new-end)))))
6385 (defun c-after-change-check-<>-operators (beg end)
6386 ;; This is called from `after-change-functions' when
6387 ;; c-recognize-<>-arglists' is set. It ensures that no "<" or ">"
6388 ;; chars with paren syntax become part of another operator like "<<"
6389 ;; or ">=".
6391 ;; This function might do hidden buffer changes.
6393 (save-excursion
6394 (goto-char beg)
6395 (when (or (looking-at "[<>]")
6396 (< (skip-chars-backward "<>") 0))
6398 (goto-char beg)
6399 (c-beginning-of-current-token)
6400 (when (and (< (point) beg)
6401 (looking-at c-<>-multichar-token-regexp)
6402 (< beg (setq beg (match-end 0))))
6403 (while (progn (skip-chars-forward "^<>" beg)
6404 (< (point) beg))
6405 (c-clear-<>-pair-props)
6406 (forward-char))))
6408 (when (< beg end)
6409 (goto-char end)
6410 (when (or (looking-at "[<>]")
6411 (< (skip-chars-backward "<>") 0))
6413 (goto-char end)
6414 (c-beginning-of-current-token)
6415 (when (and (< (point) end)
6416 (looking-at c-<>-multichar-token-regexp)
6417 (< end (setq end (match-end 0))))
6418 (while (progn (skip-chars-forward "^<>" end)
6419 (< (point) end))
6420 (c-clear-<>-pair-props)
6421 (forward-char)))))))
6423 (defvar c-restricted-<>-arglists) ;FIXME: Move definition here?
6424 (defvar c-parse-and-markup-<>-arglists) ;FIXME: Move definition here?
6426 (defun c-restore-<>-properties (_beg _end _old-len)
6427 ;; This function is called as an after-change function. It restores the
6428 ;; category/syntax-table properties on template/generic <..> pairs between
6429 ;; c-new-BEG and c-new-END. It may do hidden buffer changes.
6430 (c-save-buffer-state ((c-parse-and-markup-<>-arglists t)
6431 c-restricted-<>-arglists lit-limits)
6432 (goto-char c-new-BEG)
6433 (if (setq lit-limits (c-literal-limits))
6434 (goto-char (cdr lit-limits)))
6435 (while (and (< (point) c-new-END)
6436 (c-syntactic-re-search-forward "<" c-new-END 'bound))
6437 (backward-char)
6438 (save-excursion
6439 (c-backward-token-2)
6440 (setq c-restricted-<>-arglists
6441 (and (not (looking-at c-opt-<>-sexp-key))
6442 (progn (c-backward-syntactic-ws) ; to ( or ,
6443 (and (memq (char-before) '(?\( ?,)) ; what about <?
6444 (not (eq (c-get-char-property (point) 'c-type)
6445 'c-decl-arg-start)))))))
6446 (or (c-forward-<>-arglist nil)
6447 (c-forward-over-token-and-ws)
6448 (goto-char c-new-END)))))
6451 ;; Functions to handle C++ raw strings.
6453 ;; A valid C++ raw string looks like
6454 ;; R"<id>(<contents>)<id>"
6455 ;; , where <id> is an identifier from 0 to 16 characters long, not containing
6456 ;; spaces, control characters, double quote or left/right paren. <contents>
6457 ;; can include anything which isn't the terminating )<id>", including new
6458 ;; lines, "s, parentheses, etc.
6460 ;; CC Mode handles C++ raw strings by the use of `syntax-table' text
6461 ;; properties as follows:
6463 ;; (i) On a validly terminated raw string, no `syntax-table' text properties
6464 ;; are applied to the opening and closing delimiters, but any " in the
6465 ;; contents is given the property value "punctuation" (`(1)') to prevent it
6466 ;; interacting with the "s in the delimiters.
6468 ;; The font locking routine `c-font-lock-c++-raw-strings' (in cc-fonts.el)
6469 ;; recognizes valid raw strings, and fontifies the delimiters (apart from
6470 ;; the parentheses) with the default face and the parentheses and the
6471 ;; <contents> with font-lock-string-face.
6473 ;; (ii) A valid, but unterminated, raw string opening delimiter gets the
6474 ;; "punctuation" value (`(1)') of the `syntax-table' text property, and the
6475 ;; open parenthesis gets the "string fence" value (`(15)').
6477 ;; `c-font-lock-c++-raw-strings' puts c-font-lock-warning-face on the entire
6478 ;; unmatched opening delimiter (from the R up to the open paren), and allows
6479 ;; the rest of the buffer to get font-lock-string-face, caused by the
6480 ;; unmatched "string fence" `syntax-table' text property value.
6482 ;; (iii) Inside a macro, a valid raw string is handled as in (i). An
6483 ;; unmatched opening delimiter is handled slightly differently. In addition
6484 ;; to the "punctuation" and "string fence" properties on the delimiter,
6485 ;; another "string fence" `syntax-table' property is applied to the last
6486 ;; possible character of the macro before the terminating linefeed (if there
6487 ;; is such a character after the "("). This "last possible" character is
6488 ;; never a backslash escaping the end of line. If the character preceding
6489 ;; this "last possible" character is itself a backslash, this preceding
6490 ;; character gets a "punctuation" `syntax-table' value. If the "(" is
6491 ;; already at the end of the macro, it gets the "punctuation" value, and no
6492 ;; "string fence"s are used.
6494 ;; The effect on the fontification of either of these tactics is that rest of
6495 ;; the macro (if any) after the "(" gets font-lock-string-face, but the rest
6496 ;; of the file is fontified normally.
6499 (defun c-raw-string-pos ()
6500 ;; Get POINT's relationship to any containing raw string.
6501 ;; If point isn't in a raw string, return nil.
6502 ;; Otherwise, return the following list:
6504 ;; (POS B\" B\( E\) E\")
6506 ;; , where POS is the symbol `open-delim' if point is in the opening
6507 ;; delimiter, the symbol `close-delim' if it's in the closing delimiter, and
6508 ;; nil if it's in the string body. B\", B\(, E\), E\" are the positions of
6509 ;; the opening and closing quotes and parentheses of a correctly terminated
6510 ;; raw string. (N.B.: E\) and E\" are NOT on the "outside" of these
6511 ;; characters.) If the raw string is not terminated, E\) and E\" are set to
6512 ;; nil.
6514 ;; Note: this routine is dependant upon the correct syntax-table text
6515 ;; properties being set.
6516 (let ((state (c-state-semi-pp-to-literal (point)))
6517 open-quote-pos open-paren-pos close-paren-pos close-quote-pos id)
6518 (save-excursion
6519 (when
6520 (and
6521 (cond
6522 ((null (cadr state))
6523 (or (eq (char-after) ?\")
6524 (search-backward "\"" (max (- (point) 17) (point-min)) t)))
6525 ((and (eq (cadr state) 'string)
6526 (goto-char (nth 2 state))
6527 (or (eq (char-after) ?\")
6528 (search-backward "\"" (max (- (point) 17) (point-min)) t))
6529 (not (bobp)))))
6530 (eq (char-before) ?R)
6531 (looking-at "\"\\([^ ()\\\n\r\t]\\{0,16\\}\\)("))
6532 (setq open-quote-pos (point)
6533 open-paren-pos (match-end 1)
6534 id (match-string-no-properties 1))
6535 (goto-char (1+ open-paren-pos))
6536 (when (and (not (c-get-char-property open-paren-pos 'syntax-table))
6537 (search-forward (concat ")" id "\"") nil t))
6538 (setq close-paren-pos (match-beginning 0)
6539 close-quote-pos (1- (point))))))
6540 (and open-quote-pos
6541 (list
6542 (cond
6543 ((<= (point) open-paren-pos)
6544 'open-delim)
6545 ((and close-paren-pos
6546 (> (point) close-paren-pos))
6547 'close-delim)
6548 (t nil))
6549 open-quote-pos open-paren-pos close-paren-pos close-quote-pos))))
6551 (defun c-depropertize-raw-string (id open-quote open-paren bound)
6552 ;; Point is immediately after a raw string opening delimiter. Remove any
6553 ;; `syntax-table' text properties associated with the delimiter (if it's
6554 ;; unmatched) or the raw string.
6556 ;; ID, a string, is the delimiter's identifier. OPEN-QUOTE and OPEN-PAREN
6557 ;; are the buffer positions of the delimiter's components. BOUND is the
6558 ;; bound for searching for a matching closing delimiter; it is usually nil,
6559 ;; but if we're inside a macro, it's the end of the macro.
6561 ;; Point is moved to after the (terminated) raw string, or left after the
6562 ;; unmatched opening delimiter, as the case may be. The return value is of
6563 ;; no significance.
6564 (let ((open-paren-prop (c-get-char-property open-paren 'syntax-table)))
6565 (cond
6566 ((null open-paren-prop)
6567 ;; A terminated raw string
6568 (when (search-forward (concat ")" id "\"") nil t)
6569 (let* ((closing-paren (match-beginning 0))
6570 (first-punctuation
6571 (save-match-data
6572 (goto-char (1+ open-paren))
6573 (and (c-search-forward-char-property 'syntax-table '(1)
6574 closing-paren)
6575 (1- (point)))))
6577 (when first-punctuation
6578 (c-clear-char-property-with-value
6579 first-punctuation (match-beginning 0) 'syntax-table '(1))
6580 (c-truncate-semi-nonlit-pos-cache first-punctuation)
6581 ))))
6582 ((or (and (equal open-paren-prop '(15)) (null bound))
6583 (equal open-paren-prop '(1)))
6584 ;; An unterminated raw string either not in a macro, or in a macro with
6585 ;; the open parenthesis right up against the end of macro
6586 (c-clear-char-property open-quote 'syntax-table)
6587 (c-truncate-semi-nonlit-pos-cache open-quote)
6588 (c-clear-char-property open-paren 'syntax-table))
6590 ;; An unterminated string in a macro, with at least one char after the
6591 ;; open paren
6592 (c-clear-char-property open-quote 'syntax-table)
6593 (c-truncate-semi-nonlit-pos-cache open-quote)
6594 (c-clear-char-property open-paren 'syntax-table)
6595 (let ((after-string-fence-pos
6596 (save-excursion
6597 (goto-char (1+ open-paren))
6598 (c-search-forward-char-property 'syntax-table '(15) bound))))
6599 (when after-string-fence-pos
6600 (c-clear-char-property (1- after-string-fence-pos) 'syntax-table)))
6601 ))))
6603 (defun c-depropertize-raw-strings-in-region (start finish)
6604 ;; Remove any `syntax-table' text properties associated with C++ raw strings
6605 ;; contained in the region (START FINISH). Point is undefined at entry and
6606 ;; exit, and the return value has no significance.
6607 (goto-char start)
6608 (while (and (< (point) finish)
6609 (re-search-forward
6610 (concat "\\(" ; 1
6611 c-anchored-cpp-prefix ; 2
6612 "\\)\\|\\(" ; 3
6613 "R\"\\([^ ()\\\n\r\t]\\{0,16\\}\\)(" ; 4
6614 "\\)")
6615 finish t))
6616 (when (save-excursion
6617 (goto-char (match-beginning 0)) (not (c-in-literal)))
6618 (if (match-beginning 4) ; the id
6619 ;; We've found a raw string
6620 (c-depropertize-raw-string
6621 (match-string-no-properties 4) ; id
6622 (1+ (match-beginning 3)) ; open quote
6623 (match-end 4) ; open paren
6624 nil) ; bound
6625 ;; We've found a CPP construct. Search for raw strings within it.
6626 (goto-char (match-beginning 2)) ; the "#"
6627 (c-end-of-macro)
6628 (let ((eom (point)))
6629 (goto-char (match-end 2)) ; after the "#".
6630 (while (and (< (point) eom)
6631 (c-syntactic-re-search-forward
6632 "R\"\\([^ ()\\\n\r\t]\\{0,16\\}\\)(" eom t))
6633 (c-depropertize-raw-string
6634 (match-string-no-properties 1) ; id
6635 (1+ (match-beginning 0)) ; open quote
6636 (match-end 1) ; open paren
6637 eom))))))) ; bound.
6639 (defun c-before-change-check-raw-strings (beg end)
6640 ;; This function clears `syntax-table' text properties from C++ raw strings
6641 ;; in the region (c-new-BEG c-new-END). BEG and END are the standard
6642 ;; arguments supplied to any before-change function.
6644 ;; Point is undefined on both entry and exit, and the return value has no
6645 ;; significance.
6647 ;; This function is called as a before-change function solely due to its
6648 ;; membership of the C++ value of `c-get-state-before-change-functions'.
6649 (c-save-buffer-state
6650 ((beg-rs (progn (goto-char beg) (c-raw-string-pos)))
6651 (beg-plus (if (null beg-rs)
6653 (max beg
6654 (1+ (or (nth 4 beg-rs) (nth 2 beg-rs))))))
6655 (end-rs (progn (goto-char end) (c-raw-string-pos))) ; FIXME!!!
6656 ; Optimize this so that we don't call
6657 ; `c-raw-string-pos' twice when once
6658 ; will do. (2016-06-02).
6659 (end-minus (if (null end-rs)
6661 (min end (cadr end-rs))))
6663 (when beg-rs
6664 (setq c-new-BEG (min c-new-BEG (1- (cadr beg-rs)))))
6665 (c-depropertize-raw-strings-in-region c-new-BEG beg-plus)
6667 (when end-rs
6668 (setq c-new-END (max c-new-END
6669 (1+ (or (nth 4 end-rs)
6670 (nth 2 end-rs))))))
6671 (c-depropertize-raw-strings-in-region end-minus c-new-END)))
6673 (defun c-propertize-raw-string-opener (id open-quote open-paren bound)
6674 ;; Point is immediately after a raw string opening delimiter. Apply any
6675 ;; pertinent `syntax-table' text properties to the delimiter and also the
6676 ;; raw string, should there be a valid matching closing delimiter.
6678 ;; ID, a string, is the delimiter's identifier. OPEN-QUOTE and OPEN-PAREN
6679 ;; are the buffer positions of the delimiter's components. BOUND is the
6680 ;; bound for searching for a matching closing delimiter; it is usually nil,
6681 ;; but if we're inside a macro, it's the end of the macro.
6683 ;; Point is moved to after the (terminated) raw string, or left after the
6684 ;; unmatched opening delimiter, as the case may be. The return value is of
6685 ;; no significance.
6686 (if (search-forward (concat ")" id "\"") bound t)
6687 (let ((end-string (match-beginning 0))
6688 (after-quote (match-end 0)))
6689 (goto-char open-paren)
6690 (while (progn (skip-syntax-forward "^\"" end-string)
6691 (< (point) end-string))
6692 (c-put-char-property (point) 'syntax-table '(1)) ; punctuation
6693 (c-truncate-semi-nonlit-pos-cache (point))
6694 (forward-char))
6695 (goto-char after-quote))
6696 (c-put-char-property open-quote 'syntax-table '(1)) ; punctuation
6697 (c-truncate-semi-nonlit-pos-cache open-quote)
6698 (c-put-char-property open-paren 'syntax-table '(15)) ; generic string
6699 (when bound
6700 ;; In a CPP construct, we try to apply a generic-string `syntax-table'
6701 ;; text property to the last possible character in the string, so that
6702 ;; only characters within the macro get "stringed out".
6703 (goto-char bound)
6704 (if (save-restriction
6705 (narrow-to-region (1+ open-paren) (point-max))
6706 (re-search-backward
6707 (eval-when-compile
6708 ;; This regular expression matches either an escape pair (which
6709 ;; isn't an escaped NL) (submatch 5) or a non-escaped character
6710 ;; (which isn't itself a backslash) (submatch 10). The long
6711 ;; preambles to these (respectively submatches 2-4 and 6-9)
6712 ;; ensure that we have the correct parity for sequences of
6713 ;; backslashes, etc..
6714 (concat "\\(" ; 1
6715 "\\(\\`[^\\]?\\|[^\\][^\\]\\)\\(\\\\\\(.\\|\n\\)\\)*" ; 2-4
6716 "\\(\\\\.\\)" ; 5
6717 "\\|"
6718 "\\(\\`\\|[^\\]\\|\\(\\`[^\\]?\\|[^\\][^\\]\\)\\(\\\\\\(.\\|\n\\)\\)+\\)" ; 6-9
6719 "\\([^\\]\\)" ; 10
6720 "\\)"
6721 "\\(\\\\\n\\)*\\=")) ; 11
6722 (1+ open-paren) t))
6723 (if (match-beginning 10)
6724 (progn
6725 (c-put-char-property (match-beginning 10) 'syntax-table '(15))
6726 (c-truncate-semi-nonlit-pos-cache (match-beginning 10)))
6727 (c-put-char-property (match-beginning 5) 'syntax-table '(1))
6728 (c-put-char-property (1+ (match-beginning 5)) 'syntax-table '(15))
6729 (c-truncate-semi-nonlit-pos-cache (1+ (match-beginning 5))))
6730 (c-put-char-property open-paren 'syntax-table '(1)))
6731 (goto-char bound))))
6733 (defun c-after-change-re-mark-raw-strings (_beg _end _old-len)
6734 ;; This function applies `syntax-table' text properties to C++ raw strings
6735 ;; beginning in the region (c-new-BEG c-new-END). BEG, END, and OLD-LEN are
6736 ;; the standard arguments supplied to any after-change function.
6738 ;; Point is undefined on both entry and exit, and the return value has no
6739 ;; significance.
6741 ;; This function is called as an after-change function solely due to its
6742 ;; membership of the C++ value of `c-before-font-lock-functions'.
6743 (c-save-buffer-state ()
6744 ;; If the region (c-new-BEG c-new-END) has expanded, remove
6745 ;; `syntax-table' text-properties from the new piece(s).
6746 (when (< c-new-BEG c-old-BEG)
6747 (let ((beg-rs (progn (goto-char c-old-BEG) (c-raw-string-pos))))
6748 (c-depropertize-raw-strings-in-region
6749 c-new-BEG
6750 (if beg-rs
6751 (1+ (or (nth 4 beg-rs) (nth 2 beg-rs)))
6752 c-old-BEG))))
6753 (when (> c-new-END c-old-END)
6754 (let ((end-rs (progn (goto-char c-old-END) (c-raw-string-pos))))
6755 (c-depropertize-raw-strings-in-region
6756 (if end-rs
6757 (cadr end-rs)
6758 c-old-END)
6759 c-new-END)))
6761 (goto-char c-new-BEG)
6762 (while (and (< (point) c-new-END)
6763 (re-search-forward
6764 (concat "\\(" ; 1
6765 c-anchored-cpp-prefix ; 2
6766 "\\)\\|\\(" ; 3
6767 "R\"\\([^ ()\\\n\r\t]\\{0,16\\}\\)(" ; 4
6768 "\\)")
6769 c-new-END t))
6770 (when (save-excursion
6771 (goto-char (match-beginning 0)) (not (c-in-literal)))
6772 (if (match-beginning 4) ; the id
6773 ;; We've found a raw string.
6774 (c-propertize-raw-string-opener
6775 (match-string-no-properties 4) ; id
6776 (1+ (match-beginning 3)) ; open quote
6777 (match-end 4) ; open paren
6778 nil) ; bound
6779 ;; We've found a CPP construct. Search for raw strings within it.
6780 (goto-char (match-beginning 2)) ; the "#"
6781 (c-end-of-macro)
6782 (let ((eom (point)))
6783 (goto-char (match-end 2)) ; after the "#".
6784 (while (and (< (point) eom)
6785 (c-syntactic-re-search-forward
6786 "R\"\\([^ ()\\\n\r\t]\\{0,16\\}\\)(" eom t))
6787 (c-propertize-raw-string-opener
6788 (match-string-no-properties 1) ; id
6789 (1+ (match-beginning 0)) ; open quote
6790 (match-end 1) ; open paren
6791 eom)))))))) ; bound
6794 ;; Handling of small scale constructs like types and names.
6796 ;; Dynamically bound variable that instructs `c-forward-type' to also
6797 ;; treat possible types (i.e. those that it normally returns 'maybe or
6798 ;; 'found for) as actual types (and always return 'found for them).
6799 ;; This means that it records them in `c-record-type-identifiers' if
6800 ;; that is set, and that it adds them to `c-found-types'.
6801 (defvar c-promote-possible-types nil)
6803 ;; Dynamically bound variable that instructs `c-forward-<>-arglist' to
6804 ;; mark up successfully parsed arglists with paren syntax properties on
6805 ;; the surrounding angle brackets and with `c-<>-arg-sep' in the
6806 ;; `c-type' property of each argument separating comma.
6808 ;; Setting this variable also makes `c-forward-<>-arglist' recurse into
6809 ;; all arglists for side effects (i.e. recording types), otherwise it
6810 ;; exploits any existing paren syntax properties to quickly jump to the
6811 ;; end of already parsed arglists.
6813 ;; Marking up the arglists is not the default since doing that correctly
6814 ;; depends on a proper value for `c-restricted-<>-arglists'.
6815 (defvar c-parse-and-markup-<>-arglists nil)
6817 ;; Dynamically bound variable that instructs `c-forward-<>-arglist' to
6818 ;; not accept arglists that contain binary operators.
6820 ;; This is primarily used to handle C++ template arglists. C++
6821 ;; disambiguates them by checking whether the preceding name is a
6822 ;; template or not. We can't do that, so we assume it is a template
6823 ;; if it can be parsed as one. That usually works well since
6824 ;; comparison expressions on the forms "a < b > c" or "a < b, c > d"
6825 ;; in almost all cases would be pointless.
6827 ;; However, in function arglists, e.g. in "foo (a < b, c > d)", we
6828 ;; should let the comma separate the function arguments instead. And
6829 ;; in a context where the value of the expression is taken, e.g. in
6830 ;; "if (a < b || c > d)", it's probably not a template.
6831 (defvar c-restricted-<>-arglists nil)
6833 ;; Dynamically bound variables that instructs
6834 ;; `c-forward-keyword-clause', `c-forward-<>-arglist',
6835 ;; `c-forward-name', `c-forward-type', `c-forward-decl-or-cast-1', and
6836 ;; `c-forward-label' to record the ranges of all the type and
6837 ;; reference identifiers they encounter. They will build lists on
6838 ;; these variables where each element is a cons of the buffer
6839 ;; positions surrounding each identifier. This recording is only
6840 ;; activated when `c-record-type-identifiers' is non-nil.
6842 ;; All known types that can't be identifiers are recorded, and also
6843 ;; other possible types if `c-promote-possible-types' is set.
6844 ;; Recording is however disabled inside angle bracket arglists that
6845 ;; are encountered inside names and other angle bracket arglists.
6846 ;; Such occurrences are taken care of by `c-font-lock-<>-arglists'
6847 ;; instead.
6849 ;; Only the names in C++ template style references (e.g. "tmpl" in
6850 ;; "tmpl<a,b>::foo") are recorded as references, other references
6851 ;; aren't handled here.
6853 ;; `c-forward-label' records the label identifier(s) on
6854 ;; `c-record-ref-identifiers'.
6855 (defvar c-record-type-identifiers nil)
6856 (defvar c-record-ref-identifiers nil)
6858 ;; This variable will receive a cons cell of the range of the last
6859 ;; single identifier symbol stepped over by `c-forward-name' if it's
6860 ;; successful. This is the range that should be put on one of the
6861 ;; record lists above by the caller. It's assigned nil if there's no
6862 ;; such symbol in the name.
6863 (defvar c-last-identifier-range nil)
6865 (defmacro c-record-type-id (range)
6866 (if (eq (car-safe range) 'cons)
6867 ;; Always true.
6868 `(setq c-record-type-identifiers
6869 (cons ,range c-record-type-identifiers))
6870 `(let ((range ,range))
6871 (if range
6872 (setq c-record-type-identifiers
6873 (cons range c-record-type-identifiers))))))
6875 (defmacro c-record-ref-id (range)
6876 (if (eq (car-safe range) 'cons)
6877 ;; Always true.
6878 `(setq c-record-ref-identifiers
6879 (cons ,range c-record-ref-identifiers))
6880 `(let ((range ,range))
6881 (if range
6882 (setq c-record-ref-identifiers
6883 (cons range c-record-ref-identifiers))))))
6885 ;; Dynamically bound variable that instructs `c-forward-type' to
6886 ;; record the ranges of types that only are found. Behaves otherwise
6887 ;; like `c-record-type-identifiers'.
6888 (defvar c-record-found-types nil)
6890 (defmacro c-forward-keyword-prefixed-id (type)
6891 ;; Used internally in `c-forward-keyword-clause' to move forward
6892 ;; over a type (if TYPE is 'type) or a name (otherwise) which
6893 ;; possibly is prefixed by keywords and their associated clauses.
6894 ;; Try with a type/name first to not trip up on those that begin
6895 ;; with a keyword. Return t if a known or found type is moved
6896 ;; over. The point is clobbered if nil is returned. If range
6897 ;; recording is enabled, the identifier is recorded on as a type
6898 ;; if TYPE is 'type or as a reference if TYPE is 'ref.
6900 ;; This macro might do hidden buffer changes.
6901 `(let (res)
6902 (setq c-last-identifier-range nil)
6903 (while (if (setq res ,(if (eq type 'type)
6904 `(c-forward-type)
6905 `(c-forward-name)))
6907 (cond ((looking-at c-keywords-regexp)
6908 (c-forward-keyword-clause 1))
6909 ((and c-opt-cpp-prefix
6910 (looking-at c-noise-macro-with-parens-name-re))
6911 (c-forward-noise-clause)))))
6912 (when (memq res '(t known found prefix maybe))
6913 (when c-record-type-identifiers
6914 ,(if (eq type 'type)
6915 `(c-record-type-id c-last-identifier-range)
6916 `(c-record-ref-id c-last-identifier-range)))
6917 t)))
6919 (defmacro c-forward-id-comma-list (type update-safe-pos)
6920 ;; Used internally in `c-forward-keyword-clause' to move forward
6921 ;; over a comma separated list of types or names using
6922 ;; `c-forward-keyword-prefixed-id'.
6924 ;; This macro might do hidden buffer changes.
6925 `(while (and (progn
6926 ,(when update-safe-pos
6927 `(setq safe-pos (point)))
6928 (eq (char-after) ?,))
6929 (progn
6930 (forward-char)
6931 (c-forward-syntactic-ws)
6932 (c-forward-keyword-prefixed-id ,type)))))
6934 (defun c-forward-noise-clause ()
6935 ;; Point is at a c-noise-macro-with-parens-names macro identifier. Go
6936 ;; forward over this name, any parenthesis expression which follows it, and
6937 ;; any syntactic WS, ending up at the next token. If there is an unbalanced
6938 ;; paren expression, leave point at it. Always Return t.
6939 (c-forward-token-2)
6940 (if (and (eq (char-after) ?\()
6941 (c-go-list-forward))
6942 (c-forward-syntactic-ws))
6945 (defun c-forward-keyword-clause (match)
6946 ;; Submatch MATCH in the current match data is assumed to surround a
6947 ;; token. If it's a keyword, move over it and any immediately
6948 ;; following clauses associated with it, stopping at the start of
6949 ;; the next token. t is returned in that case, otherwise the point
6950 ;; stays and nil is returned. The kind of clauses that are
6951 ;; recognized are those specified by `c-type-list-kwds',
6952 ;; `c-ref-list-kwds', `c-colon-type-list-kwds',
6953 ;; `c-paren-nontype-kwds', `c-paren-type-kwds', `c-<>-type-kwds',
6954 ;; `c-<>-arglist-kwds', and `c-protection-kwds'.
6956 ;; This function records identifier ranges on
6957 ;; `c-record-type-identifiers' and `c-record-ref-identifiers' if
6958 ;; `c-record-type-identifiers' is non-nil.
6960 ;; Note that for `c-colon-type-list-kwds', which doesn't necessary
6961 ;; apply directly after the keyword, the type list is moved over
6962 ;; only when there is no unaccounted token before it (i.e. a token
6963 ;; that isn't moved over due to some other keyword list). The
6964 ;; identifier ranges in the list are still recorded if that should
6965 ;; be done, though.
6967 ;; This function might do hidden buffer changes.
6969 (let ((kwd-sym (c-keyword-sym (match-string match))) safe-pos pos
6970 ;; The call to `c-forward-<>-arglist' below is made after
6971 ;; `c-<>-sexp-kwds' keywords, so we're certain they actually
6972 ;; are angle bracket arglists and `c-restricted-<>-arglists'
6973 ;; should therefore be nil.
6974 (c-parse-and-markup-<>-arglists t)
6975 c-restricted-<>-arglists)
6977 (when kwd-sym
6978 (goto-char (match-end match))
6979 (c-forward-syntactic-ws)
6980 (setq safe-pos (point))
6982 (cond
6983 ((and (c-keyword-member kwd-sym 'c-type-list-kwds)
6984 (c-forward-keyword-prefixed-id type))
6985 ;; There's a type directly after a keyword in `c-type-list-kwds'.
6986 (c-forward-id-comma-list type t))
6988 ((and (c-keyword-member kwd-sym 'c-ref-list-kwds)
6989 (c-forward-keyword-prefixed-id ref))
6990 ;; There's a name directly after a keyword in `c-ref-list-kwds'.
6991 (c-forward-id-comma-list ref t))
6993 ((and (c-keyword-member kwd-sym 'c-paren-any-kwds)
6994 (eq (char-after) ?\())
6995 ;; There's an open paren after a keyword in `c-paren-any-kwds'.
6997 (forward-char)
6998 (when (and (setq pos (c-up-list-forward))
6999 (eq (char-before pos) ?\)))
7000 (when (and c-record-type-identifiers
7001 (c-keyword-member kwd-sym 'c-paren-type-kwds))
7002 ;; Use `c-forward-type' on every identifier we can find
7003 ;; inside the paren, to record the types.
7004 (while (c-syntactic-re-search-forward c-symbol-start pos t)
7005 (goto-char (match-beginning 0))
7006 (unless (c-forward-type)
7007 (looking-at c-symbol-key) ; Always matches.
7008 (goto-char (match-end 0)))))
7010 (goto-char pos)
7011 (c-forward-syntactic-ws)
7012 (setq safe-pos (point))))
7014 ((and (c-keyword-member kwd-sym 'c-<>-sexp-kwds)
7015 (eq (char-after) ?<)
7016 (c-forward-<>-arglist (c-keyword-member kwd-sym 'c-<>-type-kwds)))
7017 (c-forward-syntactic-ws)
7018 (setq safe-pos (point)))
7020 ((and (c-keyword-member kwd-sym 'c-nonsymbol-sexp-kwds)
7021 (not (looking-at c-symbol-start))
7022 (c-safe (c-forward-sexp) t))
7023 (c-forward-syntactic-ws)
7024 (setq safe-pos (point)))
7026 ((and (c-keyword-member kwd-sym 'c-protection-kwds)
7027 (or (null c-post-protection-token)
7028 (and (looking-at c-post-protection-token)
7029 (save-excursion
7030 (goto-char (match-end 0))
7031 (not (c-end-of-current-token))))))
7032 (if c-post-protection-token
7033 (goto-char (match-end 0)))
7034 (c-forward-syntactic-ws)
7035 (setq safe-pos (point))))
7037 (when (c-keyword-member kwd-sym 'c-colon-type-list-kwds)
7038 (if (eq (char-after) ?:)
7039 ;; If we are at the colon already, we move over the type
7040 ;; list after it.
7041 (progn
7042 (forward-char)
7043 (c-forward-syntactic-ws)
7044 (when (c-forward-keyword-prefixed-id type)
7045 (c-forward-id-comma-list type t)))
7046 ;; Not at the colon, so stop here. But the identifier
7047 ;; ranges in the type list later on should still be
7048 ;; recorded.
7049 (and c-record-type-identifiers
7050 (progn
7051 ;; If a keyword matched both one of the types above and
7052 ;; this one, we match `c-colon-type-list-re' after the
7053 ;; clause matched above.
7054 (goto-char safe-pos)
7055 (looking-at c-colon-type-list-re))
7056 (progn
7057 (goto-char (match-end 0))
7058 (c-forward-syntactic-ws)
7059 (c-forward-keyword-prefixed-id type))
7060 ;; There's a type after the `c-colon-type-list-re' match
7061 ;; after a keyword in `c-colon-type-list-kwds'.
7062 (c-forward-id-comma-list type nil))))
7064 (goto-char safe-pos)
7065 t)))
7067 ;; cc-mode requires cc-fonts.
7068 (declare-function c-fontify-recorded-types-and-refs "cc-fonts" ())
7070 (defun c-forward-<>-arglist (all-types)
7071 ;; The point is assumed to be at a "<". Try to treat it as the open
7072 ;; paren of an angle bracket arglist and move forward to the
7073 ;; corresponding ">". If successful, the point is left after the
7074 ;; ">" and t is returned, otherwise the point isn't moved and nil is
7075 ;; returned. If ALL-TYPES is t then all encountered arguments in
7076 ;; the arglist that might be types are treated as found types.
7078 ;; The variable `c-parse-and-markup-<>-arglists' controls how this
7079 ;; function handles text properties on the angle brackets and argument
7080 ;; separating commas.
7082 ;; `c-restricted-<>-arglists' controls how lenient the template
7083 ;; arglist recognition should be.
7085 ;; This function records identifier ranges on
7086 ;; `c-record-type-identifiers' and `c-record-ref-identifiers' if
7087 ;; `c-record-type-identifiers' is non-nil.
7089 ;; This function might do hidden buffer changes.
7091 (let ((start (point))
7092 (old-found-types (copy-hash-table c-found-types))
7093 ;; If `c-record-type-identifiers' is set then activate
7094 ;; recording of any found types that constitute an argument in
7095 ;; the arglist.
7096 (c-record-found-types (if c-record-type-identifiers t)))
7097 (if (catch 'angle-bracket-arglist-escape
7098 (setq c-record-found-types
7099 (c-forward-<>-arglist-recur all-types)))
7100 (progn
7101 (when (consp c-record-found-types)
7102 (setq c-record-type-identifiers
7103 ;; `nconc' doesn't mind that the tail of
7104 ;; `c-record-found-types' is t.
7105 (nconc c-record-found-types c-record-type-identifiers)))
7108 (setq c-found-types old-found-types)
7109 (goto-char start)
7110 nil)))
7112 (defun c-forward-<>-arglist-recur (all-types)
7113 ;; Recursive part of `c-forward-<>-arglist'.
7115 ;; This function might do hidden buffer changes.
7116 (let ((start (point)) res pos
7117 ;; Cover this so that any recorded found type ranges are
7118 ;; automatically lost if it turns out to not be an angle
7119 ;; bracket arglist. It's propagated through the return value
7120 ;; on successful completion.
7121 (c-record-found-types c-record-found-types)
7122 ;; List that collects the positions after the argument
7123 ;; separating ',' in the arglist.
7124 arg-start-pos)
7125 ;; If the '<' has paren open syntax then we've marked it as an angle
7126 ;; bracket arglist before, so skip to the end.
7127 (if (and (not c-parse-and-markup-<>-arglists)
7128 (c-get-char-property (point) 'syntax-table))
7130 (progn
7131 (forward-char)
7132 (if (and (c-go-up-list-forward)
7133 (eq (char-before) ?>))
7135 ;; Got unmatched paren angle brackets. We don't clear the paren
7136 ;; syntax properties and retry, on the basis that it's very
7137 ;; unlikely that paren angle brackets become operators by code
7138 ;; manipulation. It's far more likely that it doesn't match due
7139 ;; to narrowing or some temporary change.
7140 (goto-char start)
7141 nil))
7143 (forward-char) ; Forward over the opening '<'.
7145 (unless (looking-at c-<-op-cont-regexp)
7146 ;; go forward one non-alphanumeric character (group) per iteration of
7147 ;; this loop.
7148 (while (and
7149 (progn
7150 (c-forward-syntactic-ws)
7151 (when (or (and c-record-type-identifiers all-types)
7152 (not (equal c-inside-<>-type-key "\\(\\<\\>\\)")))
7153 (c-forward-syntactic-ws)
7154 (cond
7155 ((eq (char-after) ??)
7156 (forward-char))
7157 ((and (looking-at c-identifier-start)
7158 (not (looking-at c-keywords-regexp)))
7159 (if (or (and all-types c-record-type-identifiers)
7160 (c-major-mode-is 'java-mode))
7161 ;; All encountered identifiers are types, so set the
7162 ;; promote flag and parse the type.
7163 (let ((c-promote-possible-types t)
7164 (c-record-found-types t))
7165 (c-forward-type))
7166 (c-forward-over-token-and-ws))))
7168 (c-forward-syntactic-ws)
7170 (when (looking-at c-inside-<>-type-key)
7171 (goto-char (match-end 1))
7172 (c-forward-syntactic-ws)
7173 (let ((c-promote-possible-types t)
7174 (c-record-found-types t))
7175 (c-forward-type))
7176 (c-forward-syntactic-ws)))
7178 (setq pos (point)) ; e.g. first token inside the '<'
7180 ;; Note: These regexps exploit the match order in \| so
7181 ;; that "<>" is matched by "<" rather than "[^>:-]>".
7182 (c-syntactic-re-search-forward
7183 ;; Stop on ',', '|', '&', '+' and '-' to catch
7184 ;; common binary operators that could be between
7185 ;; two comparison expressions "a<b" and "c>d".
7186 ;; 2016-02-11: C++11 templates can now contain arithmetic
7187 ;; expressions, so template detection in C++ is now less
7188 ;; robust than it was.
7189 c-<>-notable-chars-re
7190 nil t t))
7192 (cond
7193 ((eq (char-before) ?>)
7194 ;; Either an operator starting with '>' or the end of
7195 ;; the angle bracket arglist.
7197 (if (save-excursion
7198 (c-backward-token-2)
7199 (looking-at c-multichar->-op-not->>-regexp))
7200 (progn
7201 (goto-char (match-end 0))
7202 t) ; Continue the loop.
7204 ;; The angle bracket arglist is finished.
7205 (when c-parse-and-markup-<>-arglists
7206 (while arg-start-pos
7207 (c-put-c-type-property (1- (car arg-start-pos))
7208 'c-<>-arg-sep)
7209 (setq arg-start-pos (cdr arg-start-pos)))
7210 (c-mark-<-as-paren start)
7211 (c-mark->-as-paren (1- (point))))
7212 (setq res t)
7213 nil)) ; Exit the loop.
7215 ((eq (char-before) ?<)
7216 ;; Either an operator starting with '<' or a nested arglist.
7217 (setq pos (point))
7218 (let (id-start id-end subres keyword-match)
7219 (cond
7220 ;; The '<' begins a multi-char operator.
7221 ((looking-at c-<-op-cont-regexp)
7222 (goto-char (match-end 0)))
7223 ;; We're at a nested <.....>
7224 ((progn
7225 (backward-char) ; to the '<'
7226 (and
7227 (save-excursion
7228 ;; There's always an identifier before an angle
7229 ;; bracket arglist, or a keyword in `c-<>-type-kwds'
7230 ;; or `c-<>-arglist-kwds'.
7231 (c-backward-syntactic-ws)
7232 (setq id-end (point))
7233 (c-simple-skip-symbol-backward)
7234 (when (or (setq keyword-match
7235 (looking-at c-opt-<>-sexp-key))
7236 (not (looking-at c-keywords-regexp)))
7237 (setq id-start (point))))
7238 (setq subres
7239 (let ((c-promote-possible-types t)
7240 (c-record-found-types t))
7241 (c-forward-<>-arglist-recur
7242 (and keyword-match
7243 (c-keyword-member
7244 (c-keyword-sym (match-string 1))
7245 'c-<>-type-kwds))))))
7246 (or subres (goto-char pos))
7247 subres)
7248 ;; It was an angle bracket arglist.
7249 (setq c-record-found-types subres)
7251 ;; Record the identifier before the template as a type
7252 ;; or reference depending on whether the arglist is last
7253 ;; in a qualified identifier.
7254 (when (and c-record-type-identifiers
7255 (not keyword-match))
7256 (if (and c-opt-identifier-concat-key
7257 (progn
7258 (c-forward-syntactic-ws)
7259 (looking-at c-opt-identifier-concat-key)))
7260 (c-record-ref-id (cons id-start id-end))
7261 (c-record-type-id (cons id-start id-end)))))
7263 ;; At a "less than" operator.
7265 ;; (forward-char) ; NO! We've already gone over the <.
7267 t) ; carry on looping.
7269 ((and
7270 (eq (char-before) ?\()
7271 (c-go-up-list-forward)
7272 (eq (char-before) ?\))))
7274 ((and (not c-restricted-<>-arglists)
7275 (or (and (eq (char-before) ?&)
7276 (not (eq (char-after) ?&)))
7277 (eq (char-before) ?,)))
7278 ;; Just another argument. Record the position. The
7279 ;; type check stuff that made us stop at it is at
7280 ;; the top of the loop.
7281 (setq arg-start-pos (cons (point) arg-start-pos)))
7284 ;; Got a character that can't be in an angle bracket
7285 ;; arglist argument. Abort using `throw', since
7286 ;; it's useless to try to find a surrounding arglist
7287 ;; if we're nested.
7288 (throw 'angle-bracket-arglist-escape nil))))))
7289 (if res
7290 (or c-record-found-types t)))))
7292 (defun c-backward-<>-arglist (all-types &optional limit)
7293 ;; The point is assumed to be directly after a ">". Try to treat it
7294 ;; as the close paren of an angle bracket arglist and move back to
7295 ;; the corresponding "<". If successful, the point is left at
7296 ;; the "<" and t is returned, otherwise the point isn't moved and
7297 ;; nil is returned. ALL-TYPES is passed on to
7298 ;; `c-forward-<>-arglist'.
7300 ;; If the optional LIMIT is given, it bounds the backward search.
7301 ;; It's then assumed to be at a syntactically relevant position.
7303 ;; This is a wrapper around `c-forward-<>-arglist'. See that
7304 ;; function for more details.
7306 (let ((start (point)))
7307 (backward-char)
7308 (if (and (not c-parse-and-markup-<>-arglists)
7309 (c-get-char-property (point) 'syntax-table))
7311 (if (and (c-go-up-list-backward)
7312 (eq (char-after) ?<))
7314 ;; See corresponding note in `c-forward-<>-arglist'.
7315 (goto-char start)
7316 nil)
7318 (while (progn
7319 (c-syntactic-skip-backward "^<;{}" limit t)
7321 (and
7322 (if (eq (char-before) ?<)
7324 ;; Stopped at bob or a char that isn't allowed in an
7325 ;; arglist, so we've failed.
7326 (goto-char start)
7327 nil)
7329 (if (> (point)
7330 (progn (c-beginning-of-current-token)
7331 (point)))
7332 ;; If we moved then the "<" was part of some
7333 ;; multicharacter token.
7336 (backward-char)
7337 (let ((beg-pos (point)))
7338 (if (c-forward-<>-arglist all-types)
7339 (cond ((= (point) start)
7340 ;; Matched the arglist. Break the while.
7341 (goto-char beg-pos)
7342 nil)
7343 ((> (point) start)
7344 ;; We started from a non-paren ">" inside an
7345 ;; arglist.
7346 (goto-char start)
7347 nil)
7349 ;; Matched a shorter arglist. Can be a nested
7350 ;; one so continue looking.
7351 (goto-char beg-pos)
7353 t))))))
7355 (/= (point) start))))
7357 (defun c-forward-name ()
7358 ;; Move forward over a complete name if at the beginning of one,
7359 ;; stopping at the next following token. A keyword, as such,
7360 ;; doesn't count as a name. If the point is not at something that
7361 ;; is recognized as a name then it stays put.
7363 ;; A name could be something as simple as "foo" in C or something as
7364 ;; complex as "X<Y<class A<int>::B, BIT_MAX >> b>, ::operator<> ::
7365 ;; Z<(a>b)> :: operator const X<&foo>::T Q::G<unsigned short
7366 ;; int>::*volatile const" in C++ (this function is actually little
7367 ;; more than a `looking-at' call in all modes except those that,
7368 ;; like C++, have `c-recognize-<>-arglists' set).
7370 ;; Return
7371 ;; o - nil if no name is found;
7372 ;; o - 'template if it's an identifier ending with an angle bracket
7373 ;; arglist;
7374 ;; o - 'operator of it's an operator identifier;
7375 ;; o - t if it's some other kind of name.
7377 ;; This function records identifier ranges on
7378 ;; `c-record-type-identifiers' and `c-record-ref-identifiers' if
7379 ;; `c-record-type-identifiers' is non-nil.
7381 ;; This function might do hidden buffer changes.
7383 (let ((pos (point)) (start (point)) res id-start id-end
7384 ;; Turn off `c-promote-possible-types' here since we might
7385 ;; call `c-forward-<>-arglist' and we don't want it to promote
7386 ;; every suspect thing in the arglist to a type. We're
7387 ;; typically called from `c-forward-type' in this case, and
7388 ;; the caller only wants the top level type that it finds to
7389 ;; be promoted.
7390 c-promote-possible-types)
7391 (while
7392 (and
7393 (looking-at c-identifier-key)
7395 (progn
7396 ;; Check for keyword. We go to the last symbol in
7397 ;; `c-identifier-key' first.
7398 (goto-char (setq id-end (match-end 0)))
7399 (c-simple-skip-symbol-backward)
7400 (setq id-start (point))
7402 (if (looking-at c-keywords-regexp)
7403 (when (and (c-major-mode-is 'c++-mode)
7404 (looking-at
7405 (cc-eval-when-compile
7406 (concat "\\(operator\\|\\(template\\)\\)"
7407 "\\(" (c-lang-const c-nonsymbol-key c++)
7408 "\\|$\\)")))
7409 (if (match-beginning 2)
7410 ;; "template" is only valid inside an
7411 ;; identifier if preceded by "::".
7412 (save-excursion
7413 (c-backward-syntactic-ws)
7414 (and (c-safe (backward-char 2) t)
7415 (looking-at "::")))
7418 ;; Handle a C++ operator or template identifier.
7419 (goto-char id-end)
7420 (c-forward-syntactic-ws)
7421 (cond ((eq (char-before id-end) ?e)
7422 ;; Got "... ::template".
7423 (let ((subres (c-forward-name)))
7424 (when subres
7425 (setq pos (point)
7426 res subres))))
7428 ((and (looking-at c-identifier-start)
7429 (or (not (looking-at
7430 c-ambiguous-overloadable-or-identifier-prefix-re))
7431 (save-excursion
7432 (and (eq (c-forward-token-2) 0)
7433 (not (eq (char-after) ?\())))))
7434 ;; Got a cast operator.
7435 (when (c-forward-type)
7436 (setq pos (point)
7437 res 'operator)
7438 ;; Now we should match a sequence of either
7439 ;; '*', '&' or a name followed by ":: *",
7440 ;; where each can be followed by a sequence
7441 ;; of `c-opt-type-modifier-key'.
7442 (while (cond ((looking-at "[*&]")
7443 (goto-char (match-end 0))
7445 ((looking-at c-identifier-start)
7446 (and (c-forward-name)
7447 (looking-at "::")
7448 (progn
7449 (goto-char (match-end 0))
7450 (c-forward-syntactic-ws)
7451 (eq (char-after) ?*))
7452 (progn
7453 (forward-char)
7454 t))))
7455 (while (progn
7456 (c-forward-syntactic-ws)
7457 (setq pos (point))
7458 (looking-at c-opt-type-modifier-key))
7459 (goto-char (match-end 1))))))
7461 ((looking-at c-overloadable-operators-regexp)
7462 ;; Got some other operator.
7463 (setq c-last-identifier-range
7464 (cons (point) (match-end 0)))
7465 (goto-char (match-end 0))
7466 (c-forward-syntactic-ws)
7467 (setq pos (point)
7468 res 'operator)))
7470 nil)
7472 ;; `id-start' is equal to `id-end' if we've jumped over
7473 ;; an identifier that doesn't end with a symbol token.
7474 ;; That can occur e.g. for Java import directives on the
7475 ;; form "foo.bar.*".
7476 (when (and id-start (/= id-start id-end))
7477 (setq c-last-identifier-range
7478 (cons id-start id-end)))
7479 (goto-char id-end)
7480 (c-forward-syntactic-ws)
7481 (setq pos (point)
7482 res t)))
7484 (progn
7485 (goto-char pos)
7486 (when (or c-opt-identifier-concat-key
7487 c-recognize-<>-arglists)
7489 (cond
7490 ((and c-opt-identifier-concat-key
7491 (looking-at c-opt-identifier-concat-key))
7492 ;; Got a concatenated identifier. This handles the
7493 ;; cases with tricky syntactic whitespace that aren't
7494 ;; covered in `c-identifier-key'.
7495 (goto-char (match-end 0))
7496 (c-forward-syntactic-ws)
7499 ((and c-recognize-<>-arglists
7500 (eq (char-after) ?<))
7501 ;; Maybe an angle bracket arglist.
7502 (when (let (c-last-identifier-range)
7503 (c-forward-<>-arglist nil))
7505 (c-forward-syntactic-ws)
7506 (unless (eq (char-after) ?\()
7507 (setq c-last-identifier-range nil)
7508 (c-add-type start (1+ pos)))
7509 (setq pos (point))
7511 (if (and c-opt-identifier-concat-key
7512 (looking-at c-opt-identifier-concat-key))
7514 ;; Continue if there's an identifier concatenation
7515 ;; operator after the template argument.
7516 (progn
7517 (when (and c-record-type-identifiers id-start)
7518 (c-record-ref-id (cons id-start id-end)))
7519 (forward-char 2)
7520 (c-forward-syntactic-ws)
7523 (when (and c-record-type-identifiers id-start
7524 (not (eq (char-after) ?\()))
7525 (c-record-type-id (cons id-start id-end)))
7526 (setq res 'template)
7527 nil)))
7528 )))))
7530 (goto-char pos)
7531 res))
7533 (defun c-forward-type (&optional brace-block-too)
7534 ;; Move forward over a type spec if at the beginning of one,
7535 ;; stopping at the next following token. The keyword "typedef"
7536 ;; isn't part of a type spec here.
7538 ;; BRACE-BLOCK-TOO, when non-nil, means move over the brace block in
7539 ;; constructs like "struct foo {...} bar ;" or "struct {...} bar;".
7540 ;; The current (2009-03-10) intention is to convert all uses of
7541 ;; `c-forward-type' to call with this parameter set, then to
7542 ;; eliminate it.
7544 ;; Return
7545 ;; o - t if it's a known type that can't be a name or other
7546 ;; expression;
7547 ;; o - 'known if it's an otherwise known type (according to
7548 ;; `*-font-lock-extra-types');
7549 ;; o - 'prefix if it's a known prefix of a type;
7550 ;; o - 'found if it's a type that matches one in `c-found-types';
7551 ;; o - 'maybe if it's an identifier that might be a type;
7552 ;; o - 'decltype if it's a decltype(variable) declaration; - or
7553 ;; o - nil if it can't be a type (the point isn't moved then).
7555 ;; The point is assumed to be at the beginning of a token.
7557 ;; Note that this function doesn't skip past the brace definition
7558 ;; that might be considered part of the type, e.g.
7559 ;; "enum {a, b, c} foo".
7561 ;; This function records identifier ranges on
7562 ;; `c-record-type-identifiers' and `c-record-ref-identifiers' if
7563 ;; `c-record-type-identifiers' is non-nil.
7565 ;; This function might do hidden buffer changes.
7566 (when (and c-recognize-<>-arglists
7567 (looking-at "<"))
7568 (c-forward-<>-arglist t)
7569 (c-forward-syntactic-ws))
7571 (let ((start (point)) pos res name-res id-start id-end id-range)
7573 ;; Skip leading type modifiers. If any are found we know it's a
7574 ;; prefix of a type.
7575 (when c-opt-type-modifier-prefix-key ; e.g. "const" "volatile", but NOT "typedef"
7576 (while (looking-at c-opt-type-modifier-prefix-key)
7577 (goto-char (match-end 1))
7578 (c-forward-syntactic-ws)
7579 (setq res 'prefix)))
7581 (cond
7582 ((looking-at c-typeof-key) ; e.g. C++'s "decltype".
7583 (goto-char (match-end 1))
7584 (c-forward-syntactic-ws)
7585 (setq res (and (eq (char-after) ?\()
7586 (c-safe (c-forward-sexp))
7587 'decltype))
7588 (if res
7589 (c-forward-syntactic-ws)
7590 (goto-char start)))
7592 ((looking-at c-type-prefix-key) ; e.g. "struct", "class", but NOT
7593 ; "typedef".
7594 (goto-char (match-end 1))
7595 (c-forward-syntactic-ws)
7597 (while (cond
7598 ((looking-at c-decl-hangon-key)
7599 (c-forward-keyword-clause 1))
7600 ((looking-at c-pack-key)
7601 (goto-char (match-end 1))
7602 (c-forward-syntactic-ws))
7603 ((and c-opt-cpp-prefix
7604 (looking-at c-noise-macro-with-parens-name-re))
7605 (c-forward-noise-clause))))
7607 (setq pos (point))
7609 (setq name-res (c-forward-name))
7610 (setq res (not (null name-res)))
7611 (when (eq name-res t)
7612 ;; In many languages the name can be used without the
7613 ;; prefix, so we add it to `c-found-types'.
7614 (c-add-type pos (point))
7615 (when (and c-record-type-identifiers
7616 c-last-identifier-range)
7617 (c-record-type-id c-last-identifier-range)))
7618 (when (and brace-block-too
7619 (memq res '(t nil))
7620 (eq (char-after) ?\{)
7621 (save-excursion
7622 (c-safe
7623 (progn (c-forward-sexp)
7624 (c-forward-syntactic-ws)
7625 (setq pos (point))))))
7626 (goto-char pos)
7627 (setq res t))
7628 (unless res (goto-char start))) ; invalid syntax
7630 ((progn
7631 (setq pos nil)
7632 (if (looking-at c-identifier-start)
7633 (save-excursion
7634 (setq id-start (point)
7635 name-res (c-forward-name))
7636 (when name-res
7637 (setq id-end (point)
7638 id-range c-last-identifier-range))))
7639 (and (cond ((looking-at c-primitive-type-key)
7640 (setq res t))
7641 ((c-with-syntax-table c-identifier-syntax-table
7642 (looking-at c-known-type-key))
7643 (setq res 'known)))
7644 (or (not id-end)
7645 (>= (save-excursion
7646 (save-match-data
7647 (goto-char (match-end 1))
7648 (c-forward-syntactic-ws)
7649 (setq pos (point))))
7650 id-end)
7651 (setq res nil))))
7652 ;; Looking at a primitive or known type identifier. We've
7653 ;; checked for a name first so that we don't go here if the
7654 ;; known type match only is a prefix of another name.
7656 (setq id-end (match-end 1))
7658 (when (and c-record-type-identifiers
7659 (or c-promote-possible-types (eq res t)))
7660 (c-record-type-id (cons (match-beginning 1) (match-end 1))))
7662 (if (and c-opt-type-component-key
7663 (save-match-data
7664 (looking-at c-opt-type-component-key)))
7665 ;; There might be more keywords for the type.
7666 (let (safe-pos)
7667 (c-forward-keyword-clause 1)
7668 (while (progn
7669 (setq safe-pos (point))
7670 (looking-at c-opt-type-component-key))
7671 (when (and c-record-type-identifiers
7672 (looking-at c-primitive-type-key))
7673 (c-record-type-id (cons (match-beginning 1)
7674 (match-end 1))))
7675 (c-forward-keyword-clause 1))
7676 (if (looking-at c-primitive-type-key)
7677 (progn
7678 (when c-record-type-identifiers
7679 (c-record-type-id (cons (match-beginning 1)
7680 (match-end 1))))
7681 (c-forward-keyword-clause 1)
7682 (setq res t))
7683 (goto-char safe-pos)
7684 (setq res 'prefix)))
7685 (unless (save-match-data (c-forward-keyword-clause 1))
7686 (if pos
7687 (goto-char pos)
7688 (goto-char (match-end 1))
7689 (c-forward-syntactic-ws)))))
7691 (name-res
7692 (cond ((eq name-res t)
7693 ;; A normal identifier.
7694 (goto-char id-end)
7695 (if (or res c-promote-possible-types)
7696 (progn
7697 (c-add-type id-start id-end)
7698 (when (and c-record-type-identifiers id-range)
7699 (c-record-type-id id-range))
7700 (unless res
7701 (setq res 'found)))
7702 (setq res (if (c-check-type id-start id-end)
7703 ;; It's an identifier that has been used as
7704 ;; a type somewhere else.
7705 'found
7706 ;; It's an identifier that might be a type.
7707 'maybe))))
7708 ((eq name-res 'template)
7709 ;; A template is sometimes a type.
7710 (goto-char id-end)
7711 (c-forward-syntactic-ws)
7712 (setq res
7713 (if (eq (char-after) ?\()
7714 (if (c-check-type id-start id-end)
7715 ;; It's an identifier that has been used as
7716 ;; a type somewhere else.
7717 'found
7718 ;; It's an identifier that might be a type.
7719 'maybe)
7720 t)))
7722 ;; Otherwise it's an operator identifier, which is not a type.
7723 (goto-char start)
7724 (setq res nil)))))
7726 (when res
7727 ;; Skip trailing type modifiers. If any are found we know it's
7728 ;; a type.
7729 (when c-opt-type-modifier-key
7730 (while (looking-at c-opt-type-modifier-key) ; e.g. "const", "volatile"
7731 (goto-char (match-end 1))
7732 (c-forward-syntactic-ws)
7733 (setq res t)))
7735 ;; Step over any type suffix operator. Do not let the existence
7736 ;; of these alter the classification of the found type, since
7737 ;; these operators typically are allowed in normal expressions
7738 ;; too.
7739 (when c-opt-type-suffix-key ; e.g. "..."
7740 (while (looking-at c-opt-type-suffix-key)
7741 (goto-char (match-end 1))
7742 (c-forward-syntactic-ws)))
7744 ;; Skip any "WS" identifiers (e.g. "final" or "override" in C++)
7745 (while (looking-at c-type-decl-suffix-ws-ids-key)
7746 (goto-char (match-end 1))
7747 (c-forward-syntactic-ws)
7748 (setq res t))
7750 (when c-opt-type-concat-key ; Only/mainly for pike.
7751 ;; Look for a trailing operator that concatenates the type
7752 ;; with a following one, and if so step past that one through
7753 ;; a recursive call. Note that we don't record concatenated
7754 ;; types in `c-found-types' - it's the component types that
7755 ;; are recorded when appropriate.
7756 (setq pos (point))
7757 (let* ((c-promote-possible-types (or (memq res '(t known))
7758 c-promote-possible-types))
7759 ;; If we can't promote then set `c-record-found-types' so that
7760 ;; we can merge in the types from the second part afterwards if
7761 ;; it turns out to be a known type there.
7762 (c-record-found-types (and c-record-type-identifiers
7763 (not c-promote-possible-types)))
7764 subres)
7765 (if (and (looking-at c-opt-type-concat-key)
7767 (progn
7768 (goto-char (match-end 1))
7769 (c-forward-syntactic-ws)
7770 (setq subres (c-forward-type))))
7772 (progn
7773 ;; If either operand certainly is a type then both are, but we
7774 ;; don't let the existence of the operator itself promote two
7775 ;; uncertain types to a certain one.
7776 (cond ((eq res t))
7777 ((eq subres t)
7778 (unless (eq name-res 'template)
7779 (c-add-type id-start id-end))
7780 (when (and c-record-type-identifiers id-range)
7781 (c-record-type-id id-range))
7782 (setq res t))
7783 ((eq res 'known))
7784 ((eq subres 'known)
7785 (setq res 'known))
7786 ((eq res 'found))
7787 ((eq subres 'found)
7788 (setq res 'found))
7790 (setq res 'maybe)))
7792 (when (and (eq res t)
7793 (consp c-record-found-types))
7794 ;; Merge in the ranges of any types found by the second
7795 ;; `c-forward-type'.
7796 (setq c-record-type-identifiers
7797 ;; `nconc' doesn't mind that the tail of
7798 ;; `c-record-found-types' is t.
7799 (nconc c-record-found-types
7800 c-record-type-identifiers))))
7802 (goto-char pos))))
7804 (when (and c-record-found-types (memq res '(known found)) id-range)
7805 (setq c-record-found-types
7806 (cons id-range c-record-found-types))))
7808 ;;(message "c-forward-type %s -> %s: %s" start (point) res)
7810 res))
7812 (defun c-forward-annotation ()
7813 ;; Used for Java code only at the moment. Assumes point is on the @, moves
7814 ;; forward an annotation and returns t. Leaves point unmoved and returns
7815 ;; nil if there is no annotation at point.
7816 (let ((pos (point)))
7818 (and (looking-at "@")
7819 (not (looking-at c-keywords-regexp))
7820 (progn (forward-char) t)
7821 (looking-at c-symbol-key)
7822 (progn (goto-char (match-end 0))
7823 (c-forward-syntactic-ws)
7825 (if (looking-at "(")
7826 (c-go-list-forward)
7828 (progn (goto-char pos) nil))))
7830 (defmacro c-pull-open-brace (ps)
7831 ;; Pull the next open brace from PS (which has the form of paren-state),
7832 ;; skipping over any brace pairs. Returns NIL when PS is exhausted.
7833 `(progn
7834 (while (consp (car ,ps))
7835 (setq ,ps (cdr ,ps)))
7836 (prog1 (car ,ps)
7837 (setq ,ps (cdr ,ps)))))
7839 (defun c-back-over-compound-identifier ()
7840 ;; Point is putatively just after a "compound identifier", i.e. something
7841 ;; looking (in C++) like this "FQN::of::base::Class". Move to the start of
7842 ;; this construct and return t. If the parsing fails, return nil, leaving
7843 ;; point unchanged.
7844 (let (end)
7845 (if (not (c-on-identifier))
7847 (c-simple-skip-symbol-backward)
7848 (while
7849 (progn
7850 (setq end (point))
7851 (c-backward-syntactic-ws)
7852 (c-backward-token-2)
7853 (and
7854 c-opt-identifier-concat-key
7855 (looking-at c-opt-identifier-concat-key)
7856 (progn
7857 (c-backward-syntactic-ws)
7858 (c-simple-skip-symbol-backward))))
7859 (setq end (point)))
7860 (goto-char end)
7861 t)))
7863 (defun c-back-over-member-initializer-braces ()
7864 ;; Point is just after a closing brace/parenthesis. Try to parse this as a
7865 ;; C++ member initializer list, going back to just after the introducing ":"
7866 ;; and returning t. Otherwise return nil, leaving point unchanged.
7867 (let ((here (point)) res)
7868 (setq res
7869 (catch 'done
7870 (when (not (c-go-list-backward))
7871 (throw 'done nil))
7872 (c-backward-syntactic-ws)
7873 (when (not (c-back-over-compound-identifier))
7874 (throw 'done nil))
7875 (c-backward-syntactic-ws)
7877 (while (eq (char-before) ?,)
7878 (backward-char)
7879 (c-backward-syntactic-ws)
7880 (when (not (memq (char-before) '(?\) ?})))
7881 (throw 'done nil))
7882 (when (not (c-go-list-backward))
7883 (throw 'done nil))
7884 (c-backward-syntactic-ws)
7885 (when (not (c-back-over-compound-identifier))
7886 (throw 'done nil))
7887 (c-backward-syntactic-ws))
7889 (eq (char-before) ?:)))
7890 (or res (goto-char here))
7891 res))
7893 (defmacro c-back-over-list-of-member-inits ()
7894 ;; Go back over a list of elements, each looking like:
7895 ;; <symbol> (<expression>) ,
7896 ;; or <symbol> {<expression>} , (with possibly a <....> expressions
7897 ;; following the <symbol>).
7898 ;; when we are putatively immediately after a comma. Stop when we don't see
7899 ;; a comma. If either of <symbol> or bracketed <expression> is missing,
7900 ;; throw nil to 'level. If the terminating } or ) is unmatched, throw nil
7901 ;; to 'done. This is not a general purpose macro!
7902 `(while (eq (char-before) ?,)
7903 (backward-char)
7904 (c-backward-syntactic-ws)
7905 (when (not (memq (char-before) '(?\) ?})))
7906 (throw 'level nil))
7907 (when (not (c-go-list-backward))
7908 (throw 'done nil))
7909 (c-backward-syntactic-ws)
7910 (while (eq (char-before) ?>)
7911 (when (not (c-backward-<>-arglist nil))
7912 (throw 'done nil))
7913 (c-backward-syntactic-ws))
7914 (when (not (c-back-over-compound-identifier))
7915 (throw 'level nil))
7916 (c-backward-syntactic-ws)))
7918 (defun c-back-over-member-initializers ()
7919 ;; Test whether we are in a C++ member initializer list, and if so, go back
7920 ;; to the introducing ":", returning the position of the opening paren of
7921 ;; the function's arglist. Otherwise return nil, leaving point unchanged.
7922 (let ((here (point))
7923 (paren-state (c-parse-state))
7924 pos level-plausible at-top-level res)
7925 ;; Assume tentatively that we're at the top level. Try to go back to the
7926 ;; colon we seek.
7927 (setq res
7928 (catch 'done
7929 (setq level-plausible
7930 (catch 'level
7931 (c-backward-syntactic-ws)
7932 (when (memq (char-before) '(?\) ?}))
7933 (when (not (c-go-list-backward))
7934 (throw 'done nil))
7935 (c-backward-syntactic-ws))
7936 (when (c-back-over-compound-identifier)
7937 (c-backward-syntactic-ws))
7938 (c-back-over-list-of-member-inits)
7939 (and (eq (char-before) ?:)
7940 (save-excursion
7941 (c-backward-token-2)
7942 (not (looking-at c-:$-multichar-token-regexp)))
7943 (c-just-after-func-arglist-p))))
7945 (while (and (not (and level-plausible
7946 (setq at-top-level (c-at-toplevel-p))))
7947 (setq pos (c-pull-open-brace paren-state))) ; might be a paren.
7948 (setq level-plausible
7949 (catch 'level
7950 (goto-char pos)
7951 (c-backward-syntactic-ws)
7952 (when (not (c-back-over-compound-identifier))
7953 (throw 'level nil))
7954 (c-backward-syntactic-ws)
7955 (c-back-over-list-of-member-inits)
7956 (and (eq (char-before) ?:)
7957 (save-excursion
7958 (c-backward-token-2)
7959 (not (looking-at c-:$-multichar-token-regexp)))
7960 (c-just-after-func-arglist-p)))))
7962 (and at-top-level level-plausible)))
7963 (or res (goto-char here))
7964 res))
7967 ;; Handling of large scale constructs like statements and declarations.
7969 ;; Macro used inside `c-forward-decl-or-cast-1'. It ought to be a
7970 ;; defsubst or perhaps even a defun, but it contains lots of free
7971 ;; variables that refer to things inside `c-forward-decl-or-cast-1'.
7972 (defmacro c-fdoc-shift-type-backward (&optional short)
7973 ;; `c-forward-decl-or-cast-1' can consume an arbitrary length list
7974 ;; of types when parsing a declaration, which means that it
7975 ;; sometimes consumes the identifier in the declaration as a type.
7976 ;; This is used to "backtrack" and make the last type be treated as
7977 ;; an identifier instead.
7978 `(progn
7979 ,(unless short
7980 ;; These identifiers are bound only in the inner let.
7981 '(setq identifier-type at-type
7982 identifier-start type-start
7983 got-parens nil
7984 got-identifier t
7985 got-suffix t
7986 got-suffix-after-parens id-start
7987 paren-depth 0))
7989 (if (setq at-type (if (eq backup-at-type 'prefix)
7991 backup-at-type))
7992 (setq type-start backup-type-start
7993 id-start backup-id-start)
7994 (setq type-start start-pos
7995 id-start start-pos))
7997 ;; When these flags already are set we've found specifiers that
7998 ;; unconditionally signal these attributes - backtracking doesn't
7999 ;; change that. So keep them set in that case.
8000 (or at-type-decl
8001 (setq at-type-decl backup-at-type-decl))
8002 (or maybe-typeless
8003 (setq maybe-typeless backup-maybe-typeless))
8005 ,(unless short
8006 ;; This identifier is bound only in the inner let.
8007 '(setq start id-start))))
8009 (defun c-forward-declarator (&optional limit accept-anon)
8010 ;; Assuming point is at the start of a declarator, move forward over it,
8011 ;; leaving point at the next token after it (e.g. a ) or a ; or a ,).
8013 ;; Return a list (ID-START ID-END BRACKETS-AFTER-ID GOT-INIT DECORATED),
8014 ;; where ID-START and ID-END are the bounds of the declarator's identifier,
8015 ;; and BRACKETS-AFTER-ID is non-nil if a [...] pair is present after the id.
8016 ;; GOT-INIT is non-nil when the declarator is followed by "=" or "(",
8017 ;; DECORATED is non-nil when the identifier is embellished by an operator,
8018 ;; like "*x", or "(*x)".
8020 ;; If ACCEPT-ANON is non-nil, move forward over any "anonymous declarator",
8021 ;; i.e. something like the (*) in int (*), such as might be found in a
8022 ;; declaration. In such a case ID-START and ID-END in the return value are
8023 ;; both set to nil. A "null" "anonymous declarator" gives a non-nil result.
8025 ;; If no declarator is found, leave point unmoved and return nil. LIMIT is
8026 ;; an optional limit for forward searching.
8028 ;; Note that the global variable `c-last-identifier-range' is written to, so
8029 ;; the caller should bind it if necessary.
8031 ;; Inside the following "condition form", we move forward over the
8032 ;; declarator's identifier up as far as any opening bracket (for array
8033 ;; size) or paren (for parameters of function-type) or brace (for
8034 ;; array/struct initialization) or "=" or terminating delimiter
8035 ;; (e.g. "," or ";" or "}").
8036 (let ((here (point))
8037 id-start id-end brackets-after-id paren-depth decorated)
8038 (or limit (setq limit (point-max)))
8039 (if (and
8040 (< (point) limit)
8042 ;; The following form moves forward over the declarator's
8043 ;; identifier (and what precedes it), returning t. If there
8044 ;; wasn't one, it returns nil.
8045 (let (got-identifier)
8046 (setq paren-depth 0)
8047 ;; Skip over type decl prefix operators, one for each iteration
8048 ;; of the while. These are, e.g. "*" in "int *foo" or "(" and
8049 ;; "*" in "int (*foo) (void)" (Note similar code in
8050 ;; `c-forward-decl-or-cast-1'.)
8051 (while
8052 (cond
8053 ((looking-at c-decl-hangon-key)
8054 (c-forward-keyword-clause 1))
8055 ((and c-opt-cpp-prefix
8056 (looking-at c-noise-macro-with-parens-name-re))
8057 (c-forward-noise-clause))
8058 ((and (looking-at c-type-decl-prefix-key)
8059 (if (and (c-major-mode-is 'c++-mode)
8060 (match-beginning 3))
8061 ;; If the third submatch matches in C++ then
8062 ;; we're looking at an identifier that's a
8063 ;; prefix only if it specifies a member pointer.
8064 (progn
8065 (setq id-start (point))
8066 (c-forward-name)
8067 (if (looking-at "\\(::\\)")
8068 ;; We only check for a trailing "::" and
8069 ;; let the "*" that should follow be
8070 ;; matched in the next round.
8072 ;; It turned out to be the real identifier,
8073 ;; so flag that and stop.
8074 (setq got-identifier t)
8075 nil))
8077 (if (looking-at c-type-decl-operator-prefix-key)
8078 (setq decorated t))
8079 (if (eq (char-after) ?\()
8080 (progn
8081 (setq paren-depth (1+ paren-depth))
8082 (forward-char))
8083 (goto-char (match-end 1)))
8084 (c-forward-syntactic-ws)
8085 t)))
8087 ;; If we haven't passed the identifier already, do it now.
8088 (unless got-identifier
8089 (setq id-start (point)))
8090 (cond
8091 ((or got-identifier
8092 (c-forward-name))
8093 (save-excursion
8094 (c-backward-syntactic-ws)
8095 (setq id-end (point))))
8096 (accept-anon
8097 (setq id-start nil id-end nil)
8099 (t (/= (point) here))))
8101 ;; Skip out of the parens surrounding the identifier. If closing
8102 ;; parens are missing, this form returns nil.
8103 (or (= paren-depth 0)
8104 (c-safe (goto-char (scan-lists (point) 1 paren-depth))))
8106 (<= (point) limit)
8108 ;; Skip over any trailing bit, such as "__attribute__".
8109 (progn
8110 (while (cond
8111 ((looking-at c-decl-hangon-key)
8112 (c-forward-keyword-clause 1))
8113 ((and c-opt-cpp-prefix
8114 (looking-at c-noise-macro-with-parens-name-re))
8115 (c-forward-noise-clause))))
8116 (<= (point) limit))
8118 ;; Search syntactically to the end of the declarator (";",
8119 ;; ",", a closing paren, eob etc) or to the beginning of an
8120 ;; initializer or function prototype ("=" or "\\s\(").
8121 ;; Note that square brackets are now not also treated as
8122 ;; initializers, since this broke when there were also
8123 ;; initializing brace lists.
8124 (let (found)
8125 (while
8126 (and (< (point) limit)
8127 (progn
8128 ;; In the next loop, we keep searching forward whilst
8129 ;; we find ":"s which aren't single colons inside C++
8130 ;; "for" statements.
8131 (while
8132 (and
8133 (< (point) limit)
8134 (setq found
8135 (c-syntactic-re-search-forward
8136 "[;:,]\\|\\s)\\|\\(=\\|\\s(\\)"
8137 limit t t))
8138 (eq (char-before) ?:)
8139 (if (looking-at c-:-op-cont-regexp)
8140 (progn (goto-char (match-end 0)) t)
8141 (not
8142 (and (c-major-mode-is 'c++-mode)
8143 (save-excursion
8144 (and
8145 (c-go-up-list-backward)
8146 (eq (char-after) ?\()
8147 (progn (c-backward-syntactic-ws)
8148 (c-simple-skip-symbol-backward))
8149 (looking-at c-paren-stmt-key))))))))
8150 found)
8151 (eq (char-before) ?\[)
8152 (c-go-up-list-forward))
8153 (setq brackets-after-id t))
8154 (when found (backward-char))
8155 (<= (point) limit)))
8156 (list id-start id-end brackets-after-id (match-beginning 1) decorated)
8158 (goto-char here)
8159 nil)))
8161 (defun c-forward-decl-or-cast-1 (preceding-token-end context last-cast-end)
8162 ;; Move forward over a declaration or a cast if at the start of one.
8163 ;; The point is assumed to be at the start of some token. Nil is
8164 ;; returned if no declaration or cast is recognized, and the point
8165 ;; is clobbered in that case.
8167 ;; If a declaration is parsed:
8169 ;; The point is left at the first token after the first complete
8170 ;; declarator, if there is one. The return value is a list of 5 elements,
8171 ;; where the first is the position of the first token in the declarator.
8172 ;; (See below for the other four.)
8173 ;; Some examples:
8175 ;; void foo (int a, char *b) stuff ...
8176 ;; car ^ ^ point
8177 ;; float (*a)[], b;
8178 ;; car ^ ^ point
8179 ;; unsigned int a = c_style_initializer, b;
8180 ;; car ^ ^ point
8181 ;; unsigned int a (cplusplus_style_initializer), b;
8182 ;; car ^ ^ point (might change)
8183 ;; class Foo : public Bar {}
8184 ;; car ^ ^ point
8185 ;; class PikeClass (int a, string b) stuff ...
8186 ;; car ^ ^ point
8187 ;; enum bool;
8188 ;; car ^ ^ point
8189 ;; enum bool flag;
8190 ;; car ^ ^ point
8191 ;; void cplusplus_function (int x) throw (Bad);
8192 ;; car ^ ^ point
8193 ;; Foo::Foo (int b) : Base (b) {}
8194 ;; car ^ ^ point
8196 ;; auto foo = 5;
8197 ;; car ^ ^ point
8198 ;; auto cplusplus_11 (int a, char *b) -> decltype (bar):
8199 ;; car ^ ^ point
8203 ;; The second element of the return value is non-nil when a
8204 ;; `c-typedef-decl-kwds' specifier is found in the declaration.
8205 ;; Specifically it is a dotted pair (A . B) where B is t when a
8206 ;; `c-typedef-kwds' ("typedef") is present, and A is t when some
8207 ;; other `c-typedef-decl-kwds' (e.g. class, struct, enum)
8208 ;; specifier is present. I.e., (some of) the declared
8209 ;; identifier(s) are types.
8211 ;; The third element of the return value is non-nil when the declaration
8212 ;; parsed might be an expression. The fourth element is the position of
8213 ;; the start of the type identifier. The fifth element is t if either
8214 ;; CONTEXT was 'top, or the declaration is detected to be treated as top
8215 ;; level (e.g. with the keyword "extern").
8217 ;; If a cast is parsed:
8219 ;; The point is left at the first token after the closing paren of
8220 ;; the cast. The return value is `cast'. Note that the start
8221 ;; position must be at the first token inside the cast parenthesis
8222 ;; to recognize it.
8224 ;; PRECEDING-TOKEN-END is the first position after the preceding
8225 ;; token, i.e. on the other side of the syntactic ws from the point.
8226 ;; Use a value less than or equal to (point-min) if the point is at
8227 ;; the first token in (the visible part of) the buffer.
8229 ;; CONTEXT is a symbol that describes the context at the point:
8230 ;; 'decl In a comma-separated declaration context (typically
8231 ;; inside a function declaration arglist).
8232 ;; '<> In an angle bracket arglist.
8233 ;; 'arglist Some other type of arglist.
8234 ;; 'top Some other context and point is at the top-level (either
8235 ;; outside any braces or directly inside a class or namespace,
8236 ;; etc.)
8237 ;; nil Some other context or unknown context. Includes
8238 ;; within the parens of an if, for, ... construct.
8239 ;; 'not-decl This value is never supplied to this function. It
8240 ;; would mean we're definitely not in a declaration.
8242 ;; LAST-CAST-END is the first token after the closing paren of a
8243 ;; preceding cast, or nil if none is known. If
8244 ;; `c-forward-decl-or-cast-1' is used in succession, it should be
8245 ;; the position after the closest preceding call where a cast was
8246 ;; matched. In that case it's used to discover chains of casts like
8247 ;; "(a) (b) c".
8249 ;; This function records identifier ranges on
8250 ;; `c-record-type-identifiers' and `c-record-ref-identifiers' if
8251 ;; `c-record-type-identifiers' is non-nil.
8253 ;; This function might do hidden buffer changes.
8255 (let (;; `start-pos' is used below to point to the start of the
8256 ;; first type, i.e. after any leading specifiers. It might
8257 ;; also point at the beginning of the preceding syntactic
8258 ;; whitespace.
8259 (start-pos (point))
8260 ;; Set to the result of `c-forward-type'.
8261 at-type
8262 ;; The position of the first token in what we currently
8263 ;; believe is the type in the declaration or cast, after any
8264 ;; specifiers and their associated clauses.
8265 type-start
8266 ;; The position of the first token in what we currently
8267 ;; believe is the declarator for the first identifier. Set
8268 ;; when the type is found, and moved forward over any
8269 ;; `c-decl-hangon-kwds' and their associated clauses that
8270 ;; occurs after the type.
8271 id-start
8272 ;; These store `at-type', `type-start' and `id-start' of the
8273 ;; identifier before the one in those variables. The previous
8274 ;; identifier might turn out to be the real type in a
8275 ;; declaration if the last one has to be the declarator in it.
8276 ;; If `backup-at-type' is nil then the other variables have
8277 ;; undefined values.
8278 backup-at-type backup-type-start backup-id-start
8279 ;; Set if we've found a specifier (apart from "typedef") that makes
8280 ;; the defined identifier(s) types.
8281 at-type-decl
8282 ;; Set if we've a "typedef" keyword.
8283 at-typedef
8284 ;; Set if we've found a specifier that can start a declaration
8285 ;; where there's no type.
8286 maybe-typeless
8287 ;; Save the value of kwd-sym between loops of the "Check for a
8288 ;; type" loop. Needed to distinguish a C++11 "auto" from a pre
8289 ;; C++11 one.
8290 prev-kwd-sym
8291 ;; If a specifier is found that also can be a type prefix,
8292 ;; these flags are set instead of those above. If we need to
8293 ;; back up an identifier, they are copied to the real flag
8294 ;; variables. Thus they only take effect if we fail to
8295 ;; interpret it as a type.
8296 backup-at-type-decl backup-maybe-typeless
8297 ;; Whether we've found a declaration or a cast. We might know
8298 ;; this before we've found the type in it. It's 'ids if we've
8299 ;; found two consecutive identifiers (usually a sure sign, but
8300 ;; we should allow that in labels too), and t if we've found a
8301 ;; specifier keyword (a 100% sure sign).
8302 at-decl-or-cast
8303 ;; Set when we need to back up to parse this as a declaration
8304 ;; but not as a cast.
8305 backup-if-not-cast
8306 ;; For casts, the return position.
8307 cast-end
8308 ;; Have we got a new-style C++11 "auto"?
8309 new-style-auto
8310 ;; Set when the symbol before `preceding-token-end' is known to
8311 ;; terminate the previous construct, or when we're at point-min.
8312 at-decl-start
8313 ;; Set when we have encountered a keyword (e.g. "extern") which
8314 ;; causes the following declaration to be treated as though top-level.
8315 make-top
8316 ;; Save `c-record-type-identifiers' and
8317 ;; `c-record-ref-identifiers' since ranges are recorded
8318 ;; speculatively and should be thrown away if it turns out
8319 ;; that it isn't a declaration or cast.
8320 (save-rec-type-ids c-record-type-identifiers)
8321 (save-rec-ref-ids c-record-ref-identifiers)
8322 ;; Set when we parse a declaration which might also be an expression,
8323 ;; such as "a *b". See CASE 16 and CASE 17.
8324 maybe-expression)
8326 (save-excursion
8327 (goto-char preceding-token-end)
8328 (setq at-decl-start
8329 (or (bobp)
8330 (let ((tok-end (point)))
8331 (c-backward-token-2)
8332 (member (buffer-substring-no-properties (point) tok-end)
8333 c-pre-start-tokens)))))
8335 (while (c-forward-annotation)
8336 (c-forward-syntactic-ws))
8338 ;; Check for a type. Unknown symbols are treated as possible
8339 ;; types, but they could also be specifiers disguised through
8340 ;; macros like __INLINE__, so we recognize both types and known
8341 ;; specifiers after them too.
8342 (while
8343 (let* ((start (point)) kwd-sym kwd-clause-end found-type noise-start)
8345 (cond
8346 ;; Look for a specifier keyword clause.
8347 ((or (and (looking-at c-make-top-level-key)
8348 (setq make-top t))
8349 (looking-at c-prefix-spec-kwds-re)
8350 (and (c-major-mode-is 'java-mode)
8351 (looking-at "@[A-Za-z0-9]+")))
8352 (save-match-data
8353 (if (looking-at c-typedef-key)
8354 (setq at-typedef t)))
8355 (setq kwd-sym (c-keyword-sym (match-string 1)))
8356 (save-excursion
8357 (c-forward-keyword-clause 1)
8358 (setq kwd-clause-end (point))))
8359 ((and c-opt-cpp-prefix
8360 (looking-at c-noise-macro-with-parens-name-re))
8361 (setq noise-start (point))
8362 (c-forward-noise-clause)
8363 (setq kwd-clause-end (point))))
8365 (when (setq found-type (c-forward-type t)) ; brace-block-too
8366 ;; Found a known or possible type or a prefix of a known type.
8367 (when (and (c-major-mode-is 'c++-mode) ; C++11 style "auto"?
8368 (eq prev-kwd-sym (c-keyword-sym "auto"))
8369 (looking-at "[=(]")) ; FIXME!!! proper regexp.
8370 (setq new-style-auto t)
8371 (setq found-type nil)
8372 (goto-char start)) ; position of foo in "auto foo"
8374 (when at-type
8375 ;; Got two identifiers with nothing but whitespace
8376 ;; between them. That can only happen in declarations.
8377 (setq at-decl-or-cast 'ids)
8379 (when (eq at-type 'found)
8380 ;; If the previous identifier is a found type we
8381 ;; record it as a real one; it might be some sort of
8382 ;; alias for a prefix like "unsigned".
8383 (save-excursion
8384 (goto-char type-start)
8385 (let ((c-promote-possible-types t))
8386 (c-forward-type)))))
8388 (setq backup-at-type at-type
8389 backup-type-start type-start
8390 backup-id-start id-start
8391 at-type found-type
8392 type-start start
8393 id-start (point)
8394 ;; The previous ambiguous specifier/type turned out
8395 ;; to be a type since we've parsed another one after
8396 ;; it, so clear these backup flags.
8397 backup-at-type-decl nil
8398 backup-maybe-typeless nil))
8400 (if (or kwd-sym noise-start)
8401 (progn
8402 ;; Handle known specifier keywords and
8403 ;; `c-decl-hangon-kwds' which can occur after known
8404 ;; types.
8406 (if (or (c-keyword-member kwd-sym 'c-decl-hangon-kwds)
8407 noise-start)
8408 ;; It's a hang-on keyword or noise clause that can occur
8409 ;; anywhere.
8410 (progn
8411 (if at-type
8412 ;; Move the identifier start position if
8413 ;; we've passed a type.
8414 (setq id-start kwd-clause-end)
8415 ;; Otherwise treat this as a specifier and
8416 ;; move the fallback position.
8417 (setq start-pos kwd-clause-end))
8418 (goto-char kwd-clause-end))
8420 ;; It's an ordinary specifier so we know that
8421 ;; anything before this can't be the type.
8422 (setq backup-at-type nil
8423 start-pos kwd-clause-end)
8425 (if found-type
8426 ;; It's ambiguous whether this keyword is a
8427 ;; specifier or a type prefix, so set the backup
8428 ;; flags. (It's assumed that `c-forward-type'
8429 ;; moved further than `c-forward-keyword-clause'.)
8430 (progn
8431 (when (c-keyword-member kwd-sym 'c-typedef-decl-kwds)
8432 (setq backup-at-type-decl t))
8433 (when (c-keyword-member kwd-sym 'c-typeless-decl-kwds)
8434 (setq backup-maybe-typeless t)))
8436 (when (c-keyword-member kwd-sym 'c-typedef-decl-kwds)
8437 ;; This test only happens after we've scanned a type.
8438 ;; So, with valid syntax, kwd-sym can't be 'typedef.
8439 (setq at-type-decl t))
8440 (when (c-keyword-member kwd-sym 'c-typeless-decl-kwds)
8441 (setq maybe-typeless t))
8443 ;; Haven't matched a type so it's an unambiguous
8444 ;; specifier keyword and we know we're in a
8445 ;; declaration.
8446 (setq at-decl-or-cast t)
8447 (setq prev-kwd-sym kwd-sym)
8449 (goto-char kwd-clause-end))))
8451 ;; If the type isn't known we continue so that we'll jump
8452 ;; over all specifiers and type identifiers. The reason
8453 ;; to do this for a known type prefix is to make things
8454 ;; like "unsigned INT16" work.
8455 (and found-type (not (eq found-type t))))))
8457 (cond
8458 ((eq at-type t)
8459 ;; If a known type was found, we still need to skip over any
8460 ;; hangon keyword clauses after it. Otherwise it has already
8461 ;; been done in the loop above.
8462 (while
8463 (cond ((looking-at c-decl-hangon-key)
8464 (c-forward-keyword-clause 1))
8465 ((and c-opt-cpp-prefix
8466 (looking-at c-noise-macro-with-parens-name-re))
8467 (c-forward-noise-clause))))
8468 (setq id-start (point)))
8470 ((eq at-type 'prefix)
8471 ;; A prefix type is itself a primitive type when it's not
8472 ;; followed by another type.
8473 (setq at-type t))
8475 ((not at-type)
8476 ;; Got no type but set things up to continue anyway to handle
8477 ;; the various cases when a declaration doesn't start with a
8478 ;; type.
8479 (setq id-start start-pos))
8481 ((and (eq at-type 'maybe)
8482 (c-major-mode-is 'c++-mode))
8483 ;; If it's C++ then check if the last "type" ends on the form
8484 ;; "foo::foo" or "foo::~foo", i.e. if it's the name of a
8485 ;; (con|de)structor.
8486 (save-excursion
8487 (let (name end-2 end-1)
8488 (goto-char id-start)
8489 (c-backward-syntactic-ws)
8490 (setq end-2 (point))
8491 (when (and
8492 (c-simple-skip-symbol-backward)
8493 (progn
8494 (setq name
8495 (buffer-substring-no-properties (point) end-2))
8496 ;; Cheating in the handling of syntactic ws below.
8497 (< (skip-chars-backward ":~ \t\n\r\v\f") 0))
8498 (progn
8499 (setq end-1 (point))
8500 (c-simple-skip-symbol-backward))
8501 (>= (point) type-start)
8502 (equal (buffer-substring-no-properties (point) end-1)
8503 name)
8504 (goto-char end-2)
8505 (progn
8506 (c-forward-syntactic-ws)
8507 (eq (char-after) ?\()))
8508 ;; It is a (con|de)structor name. In that case the
8509 ;; declaration is typeless so zap out any preceding
8510 ;; identifier(s) that we might have taken as types.
8511 (goto-char type-start)
8512 (setq at-type nil
8513 backup-at-type nil
8514 id-start type-start))))))
8516 ;; Check for and step over a type decl expression after the thing
8517 ;; that is or might be a type. This can't be skipped since we
8518 ;; need the correct end position of the declarator for
8519 ;; `max-type-decl-end-*'.
8520 (let ((start (point)) (paren-depth 0) pos
8521 ;; True if there's a non-open-paren match of
8522 ;; `c-type-decl-prefix-key'.
8523 got-prefix
8524 ;; True if the declarator is surrounded by a parenthesis pair.
8525 got-parens
8526 ;; True if there is an identifier in the declarator.
8527 got-identifier
8528 ;; True if there's a non-close-paren match of
8529 ;; `c-type-decl-suffix-key'.
8530 got-suffix
8531 ;; True if there's a prefix match outside the outermost
8532 ;; paren pair that surrounds the declarator.
8533 got-prefix-before-parens
8534 ;; True if there's a suffix match outside the outermost
8535 ;; paren pair that surrounds the declarator. The value is
8536 ;; the position of the first suffix match.
8537 got-suffix-after-parens
8538 ;; True if we've parsed the type decl to a token that is
8539 ;; known to end declarations in this context.
8540 at-decl-end
8541 ;; The earlier values of `at-type' and `type-start' if we've
8542 ;; shifted the type backwards.
8543 identifier-type identifier-start
8544 ;; If `c-parse-and-markup-<>-arglists' is set we need to
8545 ;; turn it off during the name skipping below to avoid
8546 ;; getting `c-type' properties that might be bogus. That
8547 ;; can happen since we don't know if
8548 ;; `c-restricted-<>-arglists' will be correct inside the
8549 ;; arglist paren that gets entered.
8550 c-parse-and-markup-<>-arglists
8551 ;; Start of the identifier for which `got-identifier' was set.
8552 name-start
8553 ;; Position after (innermost) open parenthesis encountered in the
8554 ;; prefix operators.
8555 after-paren-pos)
8557 (goto-char id-start)
8559 ;; Skip over type decl prefix operators. (Note similar code in
8560 ;; `c-forward-declarator'.)
8561 (if (and c-recognize-typeless-decls
8562 (equal c-type-decl-prefix-key "\\<\\>"))
8563 (when (eq (char-after) ?\()
8564 (progn
8565 (setq paren-depth (1+ paren-depth))
8566 (forward-char)
8567 (setq after-paren-pos (point))))
8568 (while (and (looking-at c-type-decl-prefix-key)
8569 (if (and (c-major-mode-is 'c++-mode)
8570 (match-beginning 3))
8571 ;; If the third submatch matches in C++ then
8572 ;; we're looking at an identifier that's a
8573 ;; prefix only if it specifies a member pointer.
8574 (when (progn (setq pos (point))
8575 (setq got-identifier (c-forward-name)))
8576 (setq name-start pos)
8577 (if (looking-at "\\(::\\)")
8578 ;; We only check for a trailing "::" and
8579 ;; let the "*" that should follow be
8580 ;; matched in the next round.
8581 (progn (setq got-identifier nil) t)
8582 ;; It turned out to be the real identifier,
8583 ;; so stop.
8584 nil))
8587 (if (eq (char-after) ?\()
8588 (progn
8589 (setq paren-depth (1+ paren-depth))
8590 (forward-char)
8591 (setq after-paren-pos (point)))
8592 (unless got-prefix-before-parens
8593 (setq got-prefix-before-parens (= paren-depth 0)))
8594 (setq got-prefix t)
8595 (goto-char (match-end 1)))
8596 (c-forward-syntactic-ws)))
8598 (setq got-parens (> paren-depth 0))
8600 ;; Try to skip over an identifier.
8601 (or got-identifier
8602 (and (looking-at c-identifier-start)
8603 (setq pos (point))
8604 (setq got-identifier (c-forward-name))
8605 (setq name-start pos)))
8607 ;; Skip over type decl suffix operators and trailing noise macros.
8608 (while
8609 (cond
8610 ((and c-opt-cpp-prefix
8611 (looking-at c-noise-macro-with-parens-name-re))
8612 (c-forward-noise-clause))
8614 ((and (looking-at c-type-decl-suffix-key)
8615 ;; We avoid recognizing foo(bar) or foo() at top level as a
8616 ;; construct here in C, since we want to recognize this as a
8617 ;; typeless function declaration.
8618 (not (and (c-major-mode-is 'c-mode)
8619 (or (eq context 'top) make-top)
8620 (eq (char-after) ?\)))))
8621 (if (eq (char-after) ?\))
8622 (when (> paren-depth 0)
8623 (setq paren-depth (1- paren-depth))
8624 (forward-char)
8626 (when (if (save-match-data (looking-at "\\s("))
8627 (c-safe (c-forward-sexp 1) t)
8628 (goto-char (match-end 1))
8630 (when (and (not got-suffix-after-parens)
8631 (= paren-depth 0))
8632 (setq got-suffix-after-parens (match-beginning 0)))
8633 (setq got-suffix t))))
8636 ;; No suffix matched. We might have matched the
8637 ;; identifier as a type and the open paren of a
8638 ;; function arglist as a type decl prefix. In that
8639 ;; case we should "backtrack": Reinterpret the last
8640 ;; type as the identifier, move out of the arglist and
8641 ;; continue searching for suffix operators.
8643 ;; Do this even if there's no preceding type, to cope
8644 ;; with old style function declarations in K&R C,
8645 ;; (con|de)structors in C++ and `c-typeless-decl-kwds'
8646 ;; style declarations. That isn't applicable in an
8647 ;; arglist context, though.
8648 (when (and (= paren-depth 1)
8649 (not got-prefix-before-parens)
8650 (not (eq at-type t))
8651 (or backup-at-type
8652 maybe-typeless
8653 backup-maybe-typeless
8654 (when c-recognize-typeless-decls
8655 (and (memq context '(nil top))
8656 ;; Deal with C++11's "copy-initialization"
8657 ;; where we have <type>(<constant>), by
8658 ;; contrasting with a typeless
8659 ;; <name>(<type><parameter>, ...).
8660 (save-excursion
8661 (goto-char after-paren-pos)
8662 (c-forward-syntactic-ws)
8663 (or (c-forward-type)
8664 ;; Recognize a top-level typeless
8665 ;; function declaration in C.
8666 (and (c-major-mode-is 'c-mode)
8667 (or (eq context 'top) make-top)
8668 (eq (char-after) ?\))))))))
8669 (setq pos (c-up-list-forward (point)))
8670 (eq (char-before pos) ?\)))
8671 (c-fdoc-shift-type-backward)
8672 (goto-char pos)
8673 t)))
8675 (c-forward-syntactic-ws))
8677 (when (or (and new-style-auto
8678 (looking-at c-auto-ops-re))
8679 (and (or maybe-typeless backup-maybe-typeless)
8680 (not got-identifier)
8681 (not got-prefix)
8682 at-type))
8683 ;; Have found no identifier but `c-typeless-decl-kwds' has
8684 ;; matched so we know we're inside a declaration. The
8685 ;; preceding type must be the identifier instead.
8686 (c-fdoc-shift-type-backward))
8688 ;; Prepare the "-> type;" for fontification later on.
8689 (when (and new-style-auto
8690 (looking-at c-haskell-op-re))
8691 (save-excursion
8692 (goto-char (match-end 0))
8693 (c-forward-syntactic-ws)
8694 (setq type-start (point))
8695 (setq at-type (c-forward-type))))
8697 ;; Move forward over any "WS" ids (like "final" or "override" in C++)
8698 (while (looking-at c-type-decl-suffix-ws-ids-key)
8699 (goto-char (match-end 1))
8700 (c-forward-syntactic-ws))
8702 (setq
8703 at-decl-or-cast
8704 (catch 'at-decl-or-cast
8706 ;; CASE 1
8707 (when (> paren-depth 0)
8708 ;; Encountered something inside parens that isn't matched by
8709 ;; the `c-type-decl-*' regexps, so it's not a type decl
8710 ;; expression. Try to skip out to the same paren depth to
8711 ;; not confuse the cast check below. If we don't manage this and
8712 ;; `at-decl-or-cast' is 'ids we might have an expression like
8713 ;; "foo bar ({ ..." which is a valid C++11 initialization.
8714 (if (and (not (c-safe (goto-char (scan-lists (point) 1 paren-depth))))
8715 (eq at-decl-or-cast 'ids))
8716 (c-fdoc-shift-type-backward))
8717 ;; If we've found a specifier keyword then it's a
8718 ;; declaration regardless.
8719 (throw 'at-decl-or-cast (memq at-decl-or-cast '(t ids))))
8721 (setq at-decl-end
8722 (looking-at (cond ((eq context '<>) "[,>]")
8723 ((not (memq context '(nil top))) "[,\)]")
8724 (t "[,;]"))))
8726 ;; Now we've collected info about various characteristics of
8727 ;; the construct we're looking at. Below follows a decision
8728 ;; tree based on that. It's ordered to check more certain
8729 ;; signs before less certain ones.
8731 (if got-identifier
8732 (progn
8734 ;; CASE 2
8735 (when (and (or at-type maybe-typeless)
8736 (not (or got-prefix got-parens)))
8737 ;; Got another identifier directly after the type, so it's a
8738 ;; declaration.
8739 (throw 'at-decl-or-cast t))
8741 (when (and got-parens
8742 (not got-prefix)
8743 ;; (not got-suffix-after-parens)
8744 (or backup-at-type
8745 maybe-typeless
8746 backup-maybe-typeless
8747 (eq at-decl-or-cast t)
8748 ;; Check whether we have "bar (gnu);" where we
8749 ;; are directly inside a class (etc.) called "bar".
8750 (save-excursion
8751 (and
8752 (progn
8753 (goto-char name-start)
8754 (not (memq (c-forward-type) '(nil maybe))))
8755 (progn
8756 (goto-char id-start)
8757 (c-directly-in-class-called-p
8758 (buffer-substring
8759 type-start
8760 (progn
8761 (goto-char type-start)
8762 (c-forward-type)
8763 (c-backward-syntactic-ws)
8764 (point)))))))))
8765 ;; Got a declaration of the form "foo bar (gnu);" or "bar
8766 ;; (gnu);" where we've recognized "bar" as the type and "gnu"
8767 ;; as the declarator, and in the latter case, checked that
8768 ;; "bar (gnu)" appears directly inside the class "bar". In
8769 ;; this case it's however more likely that "bar" is the
8770 ;; declarator and "gnu" a function argument or initializer
8771 ;; (if `c-recognize-paren-inits' is set), since the parens
8772 ;; around "gnu" would be superfluous if it's a declarator.
8773 ;; Shift the type one step backward.
8774 (c-fdoc-shift-type-backward)))
8776 ;; Found no identifier.
8778 (if backup-at-type
8779 (progn
8781 ;; CASE 3
8782 (when (= (point) start)
8783 ;; Got a plain list of identifiers. If a colon follows it's
8784 ;; a valid label, or maybe a bitfield. Otherwise the last
8785 ;; one probably is the declared identifier and we should
8786 ;; back up to the previous type, providing it isn't a cast.
8787 (if (and (eq (char-after) ?:)
8788 (not (c-major-mode-is 'java-mode)))
8789 (cond
8790 ;; If we've found a specifier keyword then it's a
8791 ;; declaration regardless.
8792 ((eq at-decl-or-cast t)
8793 (throw 'at-decl-or-cast t))
8794 ((and c-has-bitfields
8795 (eq at-decl-or-cast 'ids)) ; bitfield.
8796 (setq backup-if-not-cast t)
8797 (throw 'at-decl-or-cast t)))
8799 (setq backup-if-not-cast t)
8800 (throw 'at-decl-or-cast t)))
8802 ;; CASE 4
8803 (when (and got-suffix
8804 (not got-prefix)
8805 (not got-parens))
8806 ;; Got a plain list of identifiers followed by some suffix.
8807 ;; If this isn't a cast then the last identifier probably is
8808 ;; the declared one and we should back up to the previous
8809 ;; type.
8810 (setq backup-if-not-cast t)
8811 (throw 'at-decl-or-cast t)))
8813 ;; CASE 5
8814 (when (eq at-type t)
8815 ;; If the type is known we know that there can't be any
8816 ;; identifier somewhere else, and it's only in declarations in
8817 ;; e.g. function prototypes and in casts that the identifier may
8818 ;; be left out.
8819 (throw 'at-decl-or-cast t))
8821 (when (= (point) start)
8822 ;; Only got a single identifier (parsed as a type so far).
8823 ;; CASE 6
8824 (if (and
8825 ;; Check that the identifier isn't at the start of an
8826 ;; expression.
8827 at-decl-end
8828 (cond
8829 ((eq context 'decl)
8830 ;; Inside an arglist that contains declarations. If K&R
8831 ;; style declarations and parenthesis style initializers
8832 ;; aren't allowed then the single identifier must be a
8833 ;; type, else we require that it's known or found
8834 ;; (primitive types are handled above).
8835 (or (and (not c-recognize-knr-p)
8836 (not c-recognize-paren-inits))
8837 (memq at-type '(known found))))
8838 ((eq context '<>)
8839 ;; Inside a template arglist. Accept known and found
8840 ;; types; other identifiers could just as well be
8841 ;; constants in C++.
8842 (memq at-type '(known found)))))
8843 (throw 'at-decl-or-cast t)
8844 ;; CASE 7
8845 ;; Can't be a valid declaration or cast, but if we've found a
8846 ;; specifier it can't be anything else either, so treat it as
8847 ;; an invalid/unfinished declaration or cast.
8848 (throw 'at-decl-or-cast at-decl-or-cast))))
8850 (if (and got-parens
8851 (not got-prefix)
8852 (memq context '(nil top))
8853 (not (eq at-type t))
8854 (or backup-at-type
8855 maybe-typeless
8856 backup-maybe-typeless
8857 (when c-recognize-typeless-decls
8858 (or (not got-suffix)
8859 (not (looking-at
8860 c-after-suffixed-type-maybe-decl-key))))))
8861 ;; Got an empty paren pair and a preceding type that probably
8862 ;; really is the identifier. Shift the type backwards to make
8863 ;; the last one the identifier. This is analogous to the
8864 ;; "backtracking" done inside the `c-type-decl-suffix-key' loop
8865 ;; above.
8867 ;; Exception: In addition to the conditions in that
8868 ;; "backtracking" code, do not shift backward if we're not
8869 ;; looking at either `c-after-suffixed-type-decl-key' or "[;,]".
8870 ;; Since there's no preceding type, the shift would mean that
8871 ;; the declaration is typeless. But if the regexp doesn't match
8872 ;; then we will simply fall through in the tests below and not
8873 ;; recognize it at all, so it's better to try it as an abstract
8874 ;; declarator instead.
8875 (c-fdoc-shift-type-backward)
8877 ;; Still no identifier.
8878 ;; CASE 8
8879 (when (and got-prefix (or got-parens got-suffix))
8880 ;; Require `got-prefix' together with either `got-parens' or
8881 ;; `got-suffix' to recognize it as an abstract declarator:
8882 ;; `got-parens' only is probably an empty function call.
8883 ;; `got-suffix' only can build an ordinary expression together
8884 ;; with the preceding identifier which we've taken as a type.
8885 ;; We could actually accept on `got-prefix' only, but that can
8886 ;; easily occur temporarily while writing an expression so we
8887 ;; avoid that case anyway. We could do a better job if we knew
8888 ;; the point when the fontification was invoked.
8889 (throw 'at-decl-or-cast t))
8891 ;; CASE 9
8892 (when (and at-type
8893 (not got-prefix)
8894 (not got-parens)
8895 got-suffix-after-parens
8896 (eq (char-after got-suffix-after-parens) ?\())
8897 ;; Got a type, no declarator but a paren suffix. I.e. it's a
8898 ;; normal function call after all (or perhaps a C++ style object
8899 ;; instantiation expression).
8900 (throw 'at-decl-or-cast nil))))
8902 ;; CASE 9.5
8903 (when (and (not context) ; i.e. not at top level.
8904 (c-major-mode-is 'c++-mode)
8905 (eq at-decl-or-cast 'ids)
8906 after-paren-pos)
8907 ;; We've got something like "foo bar (...)" in C++ which isn't at
8908 ;; the top level. This is probably a uniform initialization of bar
8909 ;; to the contents of the parens. In this case the declarator ends
8910 ;; at the open paren.
8911 (goto-char (1- after-paren-pos))
8912 (throw 'at-decl-or-cast t))
8914 ;; CASE 10
8915 (when at-decl-or-cast
8916 ;; By now we've located the type in the declaration that we know
8917 ;; we're in.
8918 (throw 'at-decl-or-cast t))
8920 ;; CASE 11
8921 (when (and got-identifier
8922 (looking-at c-after-suffixed-type-decl-key)
8923 (or (eq context 'top)
8924 make-top
8925 (and (eq context nil)
8926 (match-beginning 1)))
8927 (if (and got-parens
8928 (not got-prefix)
8929 (not got-suffix)
8930 (not (eq at-type t)))
8931 ;; Shift the type backward in the case that there's a
8932 ;; single identifier inside parens. That can only
8933 ;; occur in K&R style function declarations so it's
8934 ;; more likely that it really is a function call.
8935 ;; Therefore we only do this after
8936 ;; `c-after-suffixed-type-decl-key' has matched.
8937 (progn (c-fdoc-shift-type-backward) t)
8938 got-suffix-after-parens))
8939 ;; A declaration according to `c-after-suffixed-type-decl-key'.
8940 (throw 'at-decl-or-cast t))
8942 ;; CASE 12
8943 (when (and (or got-prefix (not got-parens))
8944 (memq at-type '(t known)))
8945 ;; It's a declaration if a known type precedes it and it can't be a
8946 ;; function call.
8947 (throw 'at-decl-or-cast t))
8949 ;; If we get here we can't tell if this is a type decl or a normal
8950 ;; expression by looking at it alone. (That's under the assumption
8951 ;; that normal expressions always can look like type decl expressions,
8952 ;; which isn't really true but the cases where it doesn't hold are so
8953 ;; uncommon (e.g. some placements of "const" in C++) it's not worth
8954 ;; the effort to look for them.)
8956 ;;; 2008-04-16: commented out the next form, to allow the function to recognize
8957 ;;; "foo (int bar)" in CC (an implicit type (in class foo) without a semicolon)
8958 ;;; as a(n almost complete) declaration, enabling it to be fontified.
8959 ;; CASE 13
8960 ;; (unless (or at-decl-end (looking-at "=[^=]"))
8961 ;; If this is a declaration it should end here or its initializer(*)
8962 ;; should start here, so check for allowed separation tokens. Note
8963 ;; that this rule doesn't work e.g. with a K&R arglist after a
8964 ;; function header.
8966 ;; *) Don't check for C++ style initializers using parens
8967 ;; since those already have been matched as suffixes.
8969 ;; If `at-decl-or-cast' is then we've found some other sign that
8970 ;; it's a declaration or cast, so then it's probably an
8971 ;; invalid/unfinished one.
8972 ;; (throw 'at-decl-or-cast at-decl-or-cast))
8974 ;; Below are tests that only should be applied when we're certain to
8975 ;; not have parsed halfway through an expression.
8977 ;; CASE 14
8978 (when (memq at-type '(t known))
8979 ;; The expression starts with a known type so treat it as a
8980 ;; declaration.
8981 (throw 'at-decl-or-cast t))
8983 ;; CASE 15
8984 (when (and (c-major-mode-is 'c++-mode)
8985 ;; In C++ we check if the identifier is a known type, since
8986 ;; (con|de)structors use the class name as identifier.
8987 ;; We've always shifted over the identifier as a type and
8988 ;; then backed up again in this case.
8989 identifier-type
8990 (or (memq identifier-type '(found known))
8991 (and (eq (char-after identifier-start) ?~)
8992 ;; `at-type' probably won't be 'found for
8993 ;; destructors since the "~" is then part of the
8994 ;; type name being checked against the list of
8995 ;; known types, so do a check without that
8996 ;; operator.
8997 (or (save-excursion
8998 (goto-char (1+ identifier-start))
8999 (c-forward-syntactic-ws)
9000 (c-with-syntax-table
9001 c-identifier-syntax-table
9002 (looking-at c-known-type-key)))
9003 (save-excursion
9004 (goto-char (1+ identifier-start))
9005 ;; We have already parsed the type earlier,
9006 ;; so it'd be possible to cache the end
9007 ;; position instead of redoing it here, but
9008 ;; then we'd need to keep track of another
9009 ;; position everywhere.
9010 (c-check-type (point)
9011 (progn (c-forward-type)
9012 (point))))))))
9013 (throw 'at-decl-or-cast t))
9015 (if got-identifier
9016 (progn
9017 ;; CASE 16
9018 (when (and got-prefix-before-parens
9019 at-type
9020 (or at-decl-end (looking-at "=[^=]"))
9021 (memq context '(nil top))
9022 (or (not got-suffix)
9023 at-decl-start))
9024 ;; Got something like "foo * bar;". Since we're not inside
9025 ;; an arglist it would be a meaningless expression because
9026 ;; the result isn't used. We therefore choose to recognize
9027 ;; it as a declaration. We only allow a suffix (which makes
9028 ;; the construct look like a function call) when
9029 ;; `at-decl-start' provides additional evidence that we do
9030 ;; have a declaration.
9031 (setq maybe-expression t)
9032 (throw 'at-decl-or-cast t))
9034 ;; CASE 17
9035 (when (and (or got-suffix-after-parens
9036 (looking-at "=[^=]"))
9037 (eq at-type 'found)
9038 (not (eq context 'arglist)))
9039 ;; Got something like "a (*b) (c);" or "a (b) = c;". It could
9040 ;; be an odd expression or it could be a declaration. Treat
9041 ;; it as a declaration if "a" has been used as a type
9042 ;; somewhere else (if it's a known type we won't get here).
9043 (setq maybe-expression t)
9044 (throw 'at-decl-or-cast t))
9046 ;; CASE 17.5
9047 (when (and c-asymmetry-fontification-flag
9048 got-prefix-before-parens
9049 at-type
9050 (or (not got-suffix)
9051 at-decl-start))
9052 (let ((space-before-id
9053 (save-excursion
9054 (goto-char name-start)
9055 (or (bolp) (memq (char-before) '(?\ ?\t)))))
9056 (space-after-type
9057 (save-excursion
9058 (goto-char type-start)
9059 (and (c-forward-type)
9060 (progn (c-backward-syntactic-ws) t)
9061 (or (eolp)
9062 (memq (char-after) '(?\ ?\t)))))))
9063 (when (not (eq (not space-before-id)
9064 (not space-after-type)))
9065 (setq maybe-expression t)
9066 (throw 'at-decl-or-cast t)))))
9068 ;; CASE 18
9069 (when (and (not (memq context '(nil top)))
9070 (or got-prefix
9071 (and (eq context 'decl)
9072 (not c-recognize-paren-inits)
9073 (or got-parens got-suffix))))
9074 ;; Got a type followed by an abstract declarator. If `got-prefix'
9075 ;; is set it's something like "a *" without anything after it. If
9076 ;; `got-parens' or `got-suffix' is set it's "a()", "a[]", "a()[]",
9077 ;; or similar, which we accept only if the context rules out
9078 ;; expressions.
9079 (throw 'at-decl-or-cast t)))
9081 ;; If we had a complete symbol table here (which rules out
9082 ;; `c-found-types') we should return t due to the disambiguation rule
9083 ;; (in at least C++) that anything that can be parsed as a declaration
9084 ;; is a declaration. Now we're being more defensive and prefer to
9085 ;; highlight things like "foo (bar);" as a declaration only if we're
9086 ;; inside an arglist that contains declarations. Update (2017-09): We
9087 ;; now recognize a top-level "foo(bar);" as a declaration in C.
9088 ;; CASE 19
9089 (or (eq context 'decl)
9090 (and (c-major-mode-is 'c-mode)
9091 (or (eq context 'top) make-top))))))
9093 ;; The point is now after the type decl expression.
9095 (cond
9096 ;; Check for a cast.
9097 ((save-excursion
9098 (and
9099 c-cast-parens
9101 ;; Should be the first type/identifier in a cast paren.
9102 (> preceding-token-end (point-min))
9103 (memq (char-before preceding-token-end) c-cast-parens)
9105 ;; The closing paren should follow.
9106 (progn
9107 (c-forward-syntactic-ws)
9108 (looking-at "\\s)"))
9110 ;; There should be a primary expression after it.
9111 (let (pos)
9112 (forward-char)
9113 (c-forward-syntactic-ws)
9114 (setq cast-end (point))
9115 (and (looking-at c-primary-expr-regexp)
9116 (progn
9117 (setq pos (match-end 0))
9119 ;; Check if the expression begins with a prefix keyword.
9120 (match-beginning 2)
9121 (if (match-beginning 1)
9122 ;; Expression begins with an ambiguous operator. Treat
9123 ;; it as a cast if it's a type decl or if we've
9124 ;; recognized the type somewhere else.
9125 (or at-decl-or-cast
9126 (memq at-type '(t known found)))
9127 ;; Unless it's a keyword, it's the beginning of a primary
9128 ;; expression.
9129 (not (looking-at c-keywords-regexp)))))
9130 ;; If `c-primary-expr-regexp' matched a nonsymbol token, check
9131 ;; that it matched a whole one so that we don't e.g. confuse
9132 ;; the operator '-' with '->'. It's ok if it matches further,
9133 ;; though, since it e.g. can match the float '.5' while the
9134 ;; operator regexp only matches '.'.
9135 (or (not (looking-at c-nonsymbol-token-regexp))
9136 (<= (match-end 0) pos))))
9138 ;; There should either be a cast before it or something that isn't an
9139 ;; identifier or close paren.
9140 (> preceding-token-end (point-min))
9141 (progn
9142 (goto-char (1- preceding-token-end))
9143 (or (eq (point) last-cast-end)
9144 (progn
9145 (c-backward-syntactic-ws)
9146 (if (< (skip-syntax-backward "w_") 0)
9147 ;; It's a symbol. Accept it only if it's one of the
9148 ;; keywords that can precede an expression (without
9149 ;; surrounding parens).
9150 (looking-at c-simple-stmt-key)
9151 (and
9152 ;; Check that it isn't a close paren (block close is ok,
9153 ;; though).
9154 (not (memq (char-before) '(?\) ?\])))
9155 ;; Check that it isn't a nonsymbol identifier.
9156 (not (c-on-identifier)))))))))
9158 ;; Handle the cast.
9159 (when (and c-record-type-identifiers at-type (not (eq at-type t)))
9160 (let ((c-promote-possible-types t))
9161 (goto-char type-start)
9162 (c-forward-type)))
9164 (goto-char cast-end)
9165 'cast)
9167 (at-decl-or-cast
9168 ;; We're at a declaration. Highlight the type and the following
9169 ;; declarators.
9171 (when backup-if-not-cast
9172 (c-fdoc-shift-type-backward t))
9174 (when (and (eq context 'decl) (looking-at ","))
9175 ;; Make sure to propagate the `c-decl-arg-start' property to
9176 ;; the next argument if it's set in this one, to cope with
9177 ;; interactive refontification.
9178 (c-put-c-type-property (point) 'c-decl-arg-start))
9180 ;; Record the type's coordinates in `c-record-type-identifiers' for
9181 ;; later fontification.
9182 (when (and c-record-type-identifiers at-type ;; (not (eq at-type t))
9183 ;; There seems no reason to exclude a token from
9184 ;; fontification just because it's "a known type that can't
9185 ;; be a name or other expression". 2013-09-18.
9187 (let ((c-promote-possible-types t))
9188 (save-excursion
9189 (goto-char type-start)
9190 (c-forward-type))))
9192 (list id-start
9193 (and (or at-type-decl at-typedef)
9194 (cons at-type-decl at-typedef))
9195 maybe-expression
9196 type-start
9197 (or (eq context 'top) make-top)))
9200 ;; False alarm. Restore the recorded ranges.
9201 (setq c-record-type-identifiers save-rec-type-ids
9202 c-record-ref-identifiers save-rec-ref-ids)
9203 nil))))
9205 (defun c-forward-label (&optional assume-markup preceding-token-end limit)
9206 ;; Assuming that point is at the beginning of a token, check if it starts a
9207 ;; label and if so move over it and return non-nil (t in default situations,
9208 ;; specific symbols (see below) for interesting situations), otherwise don't
9209 ;; move and return nil. "Label" here means "most things with a colon".
9211 ;; More precisely, a "label" is regarded as one of:
9212 ;; (i) a goto target like "foo:" - returns the symbol `goto-target';
9213 ;; (ii) A case label - either the entire construct "case FOO:", or just the
9214 ;; bare "case", should the colon be missing. We return t;
9215 ;; (iii) a keyword which needs a colon, like "default:" or "private:"; We
9216 ;; return t;
9217 ;; (iv) One of QT's "extended" C++ variants of
9218 ;; "private:"/"protected:"/"public:"/"more:" looking like "public slots:".
9219 ;; Returns the symbol `qt-2kwds-colon'.
9220 ;; (v) QT's construct "signals:". Returns the symbol `qt-1kwd-colon'.
9221 ;; (vi) One of the keywords matched by `c-opt-extra-label-key' (without any
9222 ;; colon). Currently (2006-03), this applies only to Objective C's
9223 ;; keywords "@private", "@protected", and "@public". Returns t.
9225 ;; One of the things which will NOT be recognized as a label is a bit-field
9226 ;; element of a struct, something like "int foo:5".
9228 ;; The end of the label is taken to be just after the colon, or the end of
9229 ;; the first submatch in `c-opt-extra-label-key'. The point is directly
9230 ;; after the end on return. The terminating char gets marked with
9231 ;; `c-decl-end' to improve recognition of the following declaration or
9232 ;; statement.
9234 ;; If ASSUME-MARKUP is non-nil, it's assumed that the preceding
9235 ;; label, if any, has already been marked up like that.
9237 ;; If PRECEDING-TOKEN-END is given, it should be the first position
9238 ;; after the preceding token, i.e. on the other side of the
9239 ;; syntactic ws from the point. Use a value less than or equal to
9240 ;; (point-min) if the point is at the first token in (the visible
9241 ;; part of) the buffer.
9243 ;; The optional LIMIT limits the forward scan for the colon.
9245 ;; This function records the ranges of the label symbols on
9246 ;; `c-record-ref-identifiers' if `c-record-type-identifiers' (!) is
9247 ;; non-nil.
9249 ;; This function might do hidden buffer changes.
9251 (let ((start (point))
9252 label-end
9253 qt-symbol-idx
9254 macro-start ; if we're in one.
9255 label-type
9256 kwd)
9257 (cond
9258 ;; "case" or "default" (Doesn't apply to AWK).
9259 ((looking-at c-label-kwds-regexp)
9260 (let ((kwd-end (match-end 1)))
9261 ;; Record only the keyword itself for fontification, since in
9262 ;; case labels the following is a constant expression and not
9263 ;; a label.
9264 (when c-record-type-identifiers
9265 (c-record-ref-id (cons (match-beginning 1) kwd-end)))
9267 ;; Find the label end.
9268 (goto-char kwd-end)
9269 (setq label-type
9270 (if (and (c-syntactic-re-search-forward
9271 ;; Stop on chars that aren't allowed in expressions,
9272 ;; and on operator chars that would be meaningless
9273 ;; there. FIXME: This doesn't cope with ?: operators.
9274 "[;{=,@]\\|\\(\\=\\|[^:]\\):\\([^:]\\|\\'\\)"
9275 limit t t nil 1)
9276 (match-beginning 2))
9278 (progn ; there's a proper :
9279 (goto-char (match-beginning 2)) ; just after the :
9280 (c-put-c-type-property (1- (point)) 'c-decl-end)
9283 ;; It's an unfinished label. We consider the keyword enough
9284 ;; to recognize it as a label, so that it gets fontified.
9285 ;; Leave the point at the end of it, but don't put any
9286 ;; `c-decl-end' marker.
9287 (goto-char kwd-end)
9288 t))))
9290 ;; @private, @protected, @public, in Objective C, or similar.
9291 ((and c-opt-extra-label-key
9292 (looking-at c-opt-extra-label-key))
9293 ;; For a `c-opt-extra-label-key' match, we record the whole
9294 ;; thing for fontification. That's to get the leading '@' in
9295 ;; Objective-C protection labels fontified.
9296 (goto-char (match-end 1))
9297 (when c-record-type-identifiers
9298 (c-record-ref-id (cons (match-beginning 1) (point))))
9299 (c-put-c-type-property (1- (point)) 'c-decl-end)
9300 (setq label-type t))
9302 ;; All other cases of labels.
9303 ((and c-recognize-colon-labels ; nil for AWK and IDL, otherwise t.
9305 ;; A colon label must have something before the colon.
9306 (not (eq (char-after) ?:))
9308 ;; Check that we're not after a token that can't precede a label.
9310 ;; Trivially succeeds when there's no preceding token.
9311 ;; Succeeds when we're at a virtual semicolon.
9312 (if preceding-token-end
9313 (<= preceding-token-end (point-min))
9314 (save-excursion
9315 (c-backward-syntactic-ws)
9316 (setq preceding-token-end (point))
9317 (or (bobp)
9318 (c-at-vsemi-p))))
9320 ;; Check if we're after a label, if we're after a closing
9321 ;; paren that belong to statement, and with
9322 ;; `c-label-prefix-re'. It's done in different order
9323 ;; depending on `assume-markup' since the checks have
9324 ;; different expensiveness.
9325 (if assume-markup
9327 (eq (c-get-char-property (1- preceding-token-end) 'c-type)
9328 'c-decl-end)
9330 (save-excursion
9331 (goto-char (1- preceding-token-end))
9332 (c-beginning-of-current-token)
9333 (or (looking-at c-label-prefix-re)
9334 (looking-at c-block-stmt-1-key)))
9336 (and (eq (char-before preceding-token-end) ?\))
9337 (c-after-conditional)))
9340 (save-excursion
9341 (goto-char (1- preceding-token-end))
9342 (c-beginning-of-current-token)
9343 (or (looking-at c-label-prefix-re)
9344 (looking-at c-block-stmt-1-key)))
9346 (cond
9347 ((eq (char-before preceding-token-end) ?\))
9348 (c-after-conditional))
9350 ((eq (char-before preceding-token-end) ?:)
9351 ;; Might be after another label, so check it recursively.
9352 (save-restriction
9353 (save-excursion
9354 (goto-char (1- preceding-token-end))
9355 ;; Essentially the same as the
9356 ;; `c-syntactic-re-search-forward' regexp below.
9357 (setq macro-start
9358 (save-excursion (and (c-beginning-of-macro)
9359 (point))))
9360 (if macro-start (narrow-to-region macro-start (point-max)))
9361 (c-syntactic-skip-backward "^-]:?;}=*/%&|,<>!@+" nil t)
9362 ;; Note: the following should work instead of the
9363 ;; narrow-to-region above. Investigate why not,
9364 ;; sometime. ACM, 2006-03-31.
9365 ;; (c-syntactic-skip-backward "^-]:?;}=*/%&|,<>!@+"
9366 ;; macro-start t)
9367 (let ((pte (point))
9368 ;; If the caller turned on recording for us,
9369 ;; it shouldn't apply when we check the
9370 ;; preceding label.
9371 c-record-type-identifiers)
9372 ;; A label can't start at a cpp directive. Check for
9373 ;; this, since c-forward-syntactic-ws would foul up on it.
9374 (unless (and c-opt-cpp-prefix (looking-at c-opt-cpp-prefix))
9375 (c-forward-syntactic-ws)
9376 (c-forward-label nil pte start))))))))))
9378 ;; Point is still at the beginning of the possible label construct.
9380 ;; Check that the next nonsymbol token is ":", or that we're in one
9381 ;; of QT's "slots" declarations. Allow '(' for the sake of macro
9382 ;; arguments. FIXME: Should build this regexp from the language
9383 ;; constants.
9384 (cond
9385 ;; public: protected: private:
9386 ((and
9387 (c-major-mode-is 'c++-mode)
9388 (search-forward-regexp
9389 "\\=p\\(r\\(ivate\\|otected\\)\\|ublic\\)\\>[^_]" nil t)
9390 (progn (backward-char)
9391 (c-forward-syntactic-ws limit)
9392 (looking-at ":\\([^:]\\|\\'\\)"))) ; A single colon.
9393 (forward-char)
9394 (setq label-type t))
9395 ;; QT double keyword like "protected slots:" or goto target.
9396 ((progn (goto-char start) nil))
9397 ((when (c-syntactic-re-search-forward
9398 "[ \t\n[:?;{=*/%&|,<>!@+-]" limit t t) ; not at EOB
9399 (backward-char)
9400 (setq label-end (point))
9401 (setq qt-symbol-idx
9402 (and (c-major-mode-is 'c++-mode)
9403 (string-match
9404 "\\(p\\(r\\(ivate\\|otected\\)\\|ublic\\)\\|more\\)\\>"
9405 (buffer-substring start (point)))))
9406 (c-forward-syntactic-ws limit)
9407 (cond
9408 ((looking-at ":\\([^:]\\|\\'\\)") ; A single colon.
9409 (forward-char)
9410 (setq label-type
9411 (if (or (string= "signals" ; Special QT macro
9412 (setq kwd (buffer-substring-no-properties start label-end)))
9413 (string= "Q_SIGNALS" kwd))
9414 'qt-1kwd-colon
9415 'goto-target)))
9416 ((and qt-symbol-idx
9417 (search-forward-regexp "\\=\\(slots\\|Q_SLOTS\\)\\>" limit t)
9418 (progn (c-forward-syntactic-ws limit)
9419 (looking-at ":\\([^:]\\|\\'\\)"))) ; A single colon
9420 (forward-char)
9421 (setq label-type 'qt-2kwds-colon)))))))
9423 (save-restriction
9424 (narrow-to-region start (point))
9426 ;; Check that `c-nonlabel-token-key' doesn't match anywhere.
9427 (catch 'check-label
9428 (goto-char start)
9429 (while (progn
9430 (when (looking-at c-nonlabel-token-key)
9431 (goto-char start)
9432 (setq label-type nil)
9433 (throw 'check-label nil))
9434 (and (c-safe (c-forward-sexp)
9435 (c-forward-syntactic-ws)
9437 (not (eobp)))))
9439 ;; Record the identifiers in the label for fontification, unless
9440 ;; it begins with `c-label-kwds' in which case the following
9441 ;; identifiers are part of a (constant) expression that
9442 ;; shouldn't be fontified.
9443 (when (and c-record-type-identifiers
9444 (progn (goto-char start)
9445 (not (looking-at c-label-kwds-regexp))))
9446 (while (c-syntactic-re-search-forward c-symbol-key nil t)
9447 (c-record-ref-id (cons (match-beginning 0)
9448 (match-end 0)))))
9450 (c-put-c-type-property (1- (point-max)) 'c-decl-end)
9451 (goto-char (point-max)))))
9454 ;; Not a label.
9455 (goto-char start)))
9456 label-type))
9458 (defun c-forward-objc-directive ()
9459 ;; Assuming the point is at the beginning of a token, try to move
9460 ;; forward to the end of the Objective-C directive that starts
9461 ;; there. Return t if a directive was fully recognized, otherwise
9462 ;; the point is moved as far as one could be successfully parsed and
9463 ;; nil is returned.
9465 ;; This function records identifier ranges on
9466 ;; `c-record-type-identifiers' and `c-record-ref-identifiers' if
9467 ;; `c-record-type-identifiers' is non-nil.
9469 ;; This function might do hidden buffer changes.
9471 (let ((start (point))
9472 start-char
9473 (c-promote-possible-types t)
9475 ;; Turn off recognition of angle bracket arglists while parsing
9476 ;; types here since the protocol reference list might then be
9477 ;; considered part of the preceding name or superclass-name.
9478 c-recognize-<>-arglists)
9480 (if (or
9481 (when (looking-at
9482 (eval-when-compile
9483 (c-make-keywords-re t
9484 (append (c-lang-const c-protection-kwds objc)
9485 '("@end"))
9486 'objc-mode)))
9487 (goto-char (match-end 1))
9490 (and
9491 (looking-at
9492 (eval-when-compile
9493 (c-make-keywords-re t
9494 '("@interface" "@implementation" "@protocol")
9495 'objc-mode)))
9497 ;; Handle the name of the class itself.
9498 (progn
9499 ;; (c-forward-token-2) ; 2006/1/13 This doesn't move if the token's
9500 ;; at EOB.
9501 (goto-char (match-end 0))
9502 (setq lim (point))
9503 (c-skip-ws-forward)
9504 (c-forward-type))
9506 (catch 'break
9507 ;; Look for ": superclass-name" or "( category-name )".
9508 (when (looking-at "[:(]")
9509 (setq start-char (char-after))
9510 (forward-char)
9511 (c-forward-syntactic-ws)
9512 (unless (c-forward-type) (throw 'break nil))
9513 (when (eq start-char ?\()
9514 (unless (eq (char-after) ?\)) (throw 'break nil))
9515 (forward-char)
9516 (c-forward-syntactic-ws)))
9518 ;; Look for a protocol reference list.
9519 (if (eq (char-after) ?<)
9520 (let ((c-recognize-<>-arglists t)
9521 (c-parse-and-markup-<>-arglists t)
9522 c-restricted-<>-arglists)
9523 (c-forward-<>-arglist t))
9524 t))))
9526 (progn
9527 (c-backward-syntactic-ws lim)
9528 (c-clear-c-type-property start (1- (point)) 'c-decl-end)
9529 (c-put-c-type-property (1- (point)) 'c-decl-end)
9532 (c-clear-c-type-property start (point) 'c-decl-end)
9533 nil)))
9535 (defun c-beginning-of-inheritance-list (&optional lim)
9536 ;; Go to the first non-whitespace after the colon that starts a
9537 ;; multiple inheritance introduction. Optional LIM is the farthest
9538 ;; back we should search.
9540 ;; This function might do hidden buffer changes.
9541 (c-with-syntax-table c++-template-syntax-table
9542 (c-backward-token-2 0 t lim)
9543 (while (and (or (looking-at c-symbol-start)
9544 (looking-at "[<,]\\|::"))
9545 (zerop (c-backward-token-2 1 t lim))))))
9547 (defun c-in-method-def-p ()
9548 ;; Return nil if we aren't in a method definition, otherwise the
9549 ;; position of the initial [+-].
9551 ;; This function might do hidden buffer changes.
9552 (save-excursion
9553 (beginning-of-line)
9554 (and c-opt-method-key
9555 (looking-at c-opt-method-key)
9556 (point))
9559 ;; Contributed by Kevin Ryde <user42@zip.com.au>.
9560 (defun c-in-gcc-asm-p ()
9561 ;; Return non-nil if point is within a gcc \"asm\" block.
9563 ;; This should be called with point inside an argument list.
9565 ;; Only one level of enclosing parentheses is considered, so for
9566 ;; instance nil is returned when in a function call within an asm
9567 ;; operand.
9569 ;; This function might do hidden buffer changes.
9571 (and c-opt-asm-stmt-key
9572 (save-excursion
9573 (beginning-of-line)
9574 (backward-up-list 1)
9575 (c-beginning-of-statement-1 (point-min) nil t)
9576 (looking-at c-opt-asm-stmt-key))))
9578 (defun c-at-toplevel-p ()
9579 "Return a determination as to whether point is \"at the top level\".
9580 Informally, \"at the top level\" is anywhere where you can write
9581 a function.
9583 More precisely, being at the top-level means that point is either
9584 outside any enclosing block (such as a function definition), or
9585 directly inside a class, namespace or other block that contains
9586 another declaration level.
9588 If point is not at the top-level (e.g. it is inside a method
9589 definition), then nil is returned. Otherwise, if point is at a
9590 top-level not enclosed within a class definition, t is returned.
9591 Otherwise, a 2-vector is returned where the zeroth element is the
9592 buffer position of the start of the class declaration, and the first
9593 element is the buffer position of the enclosing class's opening
9594 brace.
9596 Note that this function might do hidden buffer changes. See the
9597 comment at the start of cc-engine.el for more info."
9598 ;; Note to maintainers: this function consumes a great mass of CPU cycles.
9599 ;; Its use should thus be minimized as far as possible.
9600 ;; Consider instead using `c-bs-at-toplevel-p'.
9601 (let ((paren-state (c-parse-state)))
9602 (or (not (c-most-enclosing-brace paren-state))
9603 (c-search-uplist-for-classkey paren-state))))
9605 (defun c-just-after-func-arglist-p (&optional lim)
9606 ;; Return non-nil if the point is in the region after the argument
9607 ;; list of a function and its opening brace (or semicolon in case it
9608 ;; got no body). If there are K&R style argument declarations in
9609 ;; that region, the point has to be inside the first one for this
9610 ;; function to recognize it.
9612 ;; If successful, the point is moved to the first token after the
9613 ;; function header (see `c-forward-decl-or-cast-1' for details) and
9614 ;; the position of the opening paren of the function arglist is
9615 ;; returned.
9617 ;; The point is clobbered if not successful.
9619 ;; LIM is used as bound for backward buffer searches.
9621 ;; This function might do hidden buffer changes.
9623 (let ((beg (point)) id-start)
9624 (and
9625 (eq (c-beginning-of-statement-1 lim) 'same)
9627 (not (and (c-major-mode-is 'objc-mode)
9628 (c-forward-objc-directive)))
9630 ;; Don't confuse #if .... defined(foo) for a function arglist.
9631 (not (and (looking-at c-cpp-expr-functions-key)
9632 (save-excursion
9633 (save-restriction
9634 (widen)
9635 (c-beginning-of-macro lim)))))
9636 (setq id-start
9637 (car-safe (c-forward-decl-or-cast-1 (c-point 'bosws) 'top nil)))
9638 (numberp id-start)
9639 (< id-start beg)
9641 ;; There should not be a '=' or ',' between beg and the
9642 ;; start of the declaration since that means we were in the
9643 ;; "expression part" of the declaration.
9644 (or (> (point) beg)
9645 (not (looking-at "[=,]")))
9647 (save-excursion
9648 ;; Check that there's an arglist paren in the
9649 ;; declaration.
9650 (goto-char id-start)
9651 (cond ((eq (char-after) ?\()
9652 ;; The declarator is a paren expression, so skip past it
9653 ;; so that we don't get stuck on that instead of the
9654 ;; function arglist.
9655 (c-forward-sexp))
9656 ((and c-opt-op-identifier-prefix
9657 (looking-at c-opt-op-identifier-prefix))
9658 ;; Don't trip up on "operator ()".
9659 (c-forward-token-2 2 t)))
9660 (and (< (point) beg)
9661 (c-syntactic-re-search-forward "(" beg t t)
9662 (1- (point)))))))
9664 (defun c-in-knr-argdecl (&optional lim)
9665 ;; Return the position of the first argument declaration if point is
9666 ;; inside a K&R style argument declaration list, nil otherwise.
9667 ;; `c-recognize-knr-p' is not checked. If LIM is non-nil, it's a
9668 ;; position that bounds the backward search for the argument list. This
9669 ;; function doesn't move point.
9671 ;; Point must be within a possible K&R region, e.g. just before a top-level
9672 ;; "{". It must be outside of parens and brackets. The test can return
9673 ;; false positives otherwise.
9675 ;; This function might do hidden buffer changes.
9676 (save-excursion
9677 (save-restriction
9678 ;; If we're in a macro, our search range is restricted to it. Narrow to
9679 ;; the searchable range.
9680 (let* ((macro-start (save-excursion (and (c-beginning-of-macro) (point))))
9681 (macro-end (save-excursion (and macro-start (c-end-of-macro) (point))))
9682 (low-lim (max (or lim (point-min)) (or macro-start (point-min))))
9683 before-lparen after-rparen
9684 (here (point))
9685 (pp-count-out 20) ; Max number of paren/brace constructs before
9686 ; we give up.
9687 ids ; List of identifiers in the parenthesized list.
9688 id-start after-prec-token decl-or-cast decl-res
9689 c-last-identifier-range identifier-ok)
9690 (narrow-to-region low-lim (or macro-end (point-max)))
9692 ;; Search backwards for the defun's argument list. We give up if we
9693 ;; encounter a "}" (end of a previous defun) an "=" (which can't be in
9694 ;; a knr region) or BOB.
9696 ;; The criterion for a paren structure being the arg list is:
9697 ;; o - there is non-WS stuff after it but before any "{"; AND
9698 ;; o - the token after it isn't a ";" AND
9699 ;; o - it is preceded by either an identifier (the function name) or
9700 ;; a macro expansion like "DEFUN (...)"; AND
9701 ;; o - its content is a non-empty comma-separated list of identifiers
9702 ;; (an empty arg list won't have a knr region).
9704 ;; The following snippet illustrates these rules:
9705 ;; int foo (bar, baz, yuk)
9706 ;; int bar [] ;
9707 ;; int (*baz) (my_type) ;
9708 ;; int (*(* yuk) (void)) (void) ;
9709 ;; {
9711 ;; Additionally, for a knr list to be recognized:
9712 ;; o - The identifier of each declarator up to and including the
9713 ;; one "near" point must be contained in the arg list.
9715 (catch 'knr
9716 (while (> pp-count-out 0) ; go back one paren/bracket pair each time.
9717 (setq pp-count-out (1- pp-count-out))
9718 (c-syntactic-skip-backward "^)]}=")
9719 (cond ((eq (char-before) ?\))
9720 (setq after-rparen (point)))
9721 ((eq (char-before) ?\])
9722 (setq after-rparen nil))
9723 (t ; either } (hit previous defun) or = or no more
9724 ; parens/brackets.
9725 (throw 'knr nil)))
9727 (if after-rparen
9728 ;; We're inside a paren. Could it be our argument list....?
9730 (and
9731 (progn
9732 (goto-char after-rparen)
9733 (unless (c-go-list-backward) (throw 'knr nil)) ;
9734 ;; FIXME!!! What about macros between the parens? 2007/01/20
9735 (setq before-lparen (point)))
9737 ;; It can't be the arg list if next token is ; or {
9738 (progn (goto-char after-rparen)
9739 (c-forward-syntactic-ws)
9740 (not (memq (char-after) '(?\; ?\{ ?\=))))
9742 ;; Is the thing preceding the list an identifier (the
9743 ;; function name), or a macro expansion?
9744 (progn
9745 (goto-char before-lparen)
9746 (eq (c-backward-token-2) 0)
9747 (or (eq (c-on-identifier) (point))
9748 (and (eq (char-after) ?\))
9749 (c-go-up-list-backward)
9750 (eq (c-backward-token-2) 0)
9751 (eq (c-on-identifier) (point)))))
9753 ;; Have we got a non-empty list of comma-separated
9754 ;; identifiers?
9755 (progn
9756 (goto-char before-lparen)
9757 (and
9758 (c-forward-over-token-and-ws) ; to first token inside parens
9759 (setq id-start (c-on-identifier)) ; Must be at least one.
9760 (catch 'id-list
9761 (while
9762 (progn
9763 (forward-char)
9764 (c-end-of-current-token)
9765 (push (buffer-substring-no-properties id-start
9766 (point))
9767 ids)
9768 (c-forward-syntactic-ws)
9769 (eq (char-after) ?\,))
9770 (c-forward-over-token-and-ws)
9771 (unless (setq id-start (c-on-identifier))
9772 (throw 'id-list nil)))
9773 (eq (char-after) ?\)))))
9775 ;; Are all the identifiers in the k&r list up to the
9776 ;; current one also in the argument list?
9777 (progn
9778 (forward-char) ; over the )
9779 (setq after-prec-token after-rparen)
9780 (c-forward-syntactic-ws)
9781 (while (and
9782 (or (consp (setq decl-or-cast
9783 (c-forward-decl-or-cast-1
9784 after-prec-token
9785 nil ; Or 'arglist ???
9786 nil)))
9787 (progn
9788 (goto-char after-prec-token)
9789 (c-forward-syntactic-ws)
9790 (setq identifier-ok (eq (char-after) ?{))
9791 nil))
9792 (eq (char-after) ?\;)
9793 (setq after-prec-token (1+ (point)))
9794 (goto-char (car decl-or-cast))
9795 (setq decl-res (c-forward-declarator))
9796 (setq identifier-ok
9797 (member (buffer-substring-no-properties
9798 (car decl-res) (cadr decl-res))
9799 ids))
9800 (progn
9801 (goto-char after-prec-token)
9802 (prog1 (< (point) here)
9803 (c-forward-syntactic-ws))))
9804 (setq identifier-ok nil))
9805 identifier-ok))
9806 ;; ...Yes. We've identified the function's argument list.
9807 (throw 'knr
9808 (progn (goto-char after-rparen)
9809 (c-forward-syntactic-ws)
9810 (point)))
9811 ;; ...No. The current parens aren't the function's arg list.
9812 (goto-char before-lparen))
9814 (or (c-go-list-backward) ; backwards over [ .... ]
9815 (throw 'knr nil)))))))))
9817 (defun c-skip-conditional ()
9818 ;; skip forward over conditional at point, including any predicate
9819 ;; statements in parentheses. No error checking is performed.
9821 ;; This function might do hidden buffer changes.
9822 (c-forward-sexp (cond
9823 ;; else if()
9824 ((looking-at (concat "\\<else"
9825 "\\([ \t\n]\\|\\\\\n\\)+"
9826 "if\\>\\([^_]\\|$\\)"))
9828 ;; do, else, try, finally
9829 ((looking-at (concat "\\<\\("
9830 "do\\|else\\|try\\|finally"
9831 "\\)\\>\\([^_]\\|$\\)"))
9833 ;; for, if, while, switch, catch, synchronized, foreach
9834 (t 2))))
9836 (defun c-after-conditional (&optional lim)
9837 ;; If looking at the token after a conditional then return the
9838 ;; position of its start, otherwise return nil.
9840 ;; This function might do hidden buffer changes.
9841 (save-excursion
9842 (and (zerop (c-backward-token-2 1 t lim))
9843 (or (looking-at c-block-stmt-1-key)
9844 (and (eq (char-after) ?\()
9845 (zerop (c-backward-token-2 1 t lim))
9846 (or (looking-at c-block-stmt-2-key)
9847 (looking-at c-block-stmt-1-2-key))))
9848 (point))))
9850 (defun c-after-special-operator-id (&optional lim)
9851 ;; If the point is after an operator identifier that isn't handled
9852 ;; like an ordinary symbol (i.e. like "operator =" in C++) then the
9853 ;; position of the start of that identifier is returned. nil is
9854 ;; returned otherwise. The point may be anywhere in the syntactic
9855 ;; whitespace after the last token of the operator identifier.
9857 ;; This function might do hidden buffer changes.
9858 (save-excursion
9859 (and c-overloadable-operators-regexp
9860 (zerop (c-backward-token-2 1 nil lim))
9861 (looking-at c-overloadable-operators-regexp)
9862 (or (not c-opt-op-identifier-prefix)
9863 (and
9864 (zerop (c-backward-token-2 1 nil lim))
9865 (looking-at c-opt-op-identifier-prefix)))
9866 (point))))
9868 (defsubst c-backward-to-block-anchor (&optional lim)
9869 ;; Assuming point is at a brace that opens a statement block of some
9870 ;; kind, move to the proper anchor point for that block. It might
9871 ;; need to be adjusted further by c-add-stmt-syntax, but the
9872 ;; position at return is suitable as start position for that
9873 ;; function.
9875 ;; This function might do hidden buffer changes.
9876 (unless (= (point) (c-point 'boi))
9877 (let ((start (c-after-conditional lim)))
9878 (if start
9879 (goto-char start)))))
9881 (defsubst c-backward-to-decl-anchor (&optional lim)
9882 ;; Assuming point is at a brace that opens the block of a top level
9883 ;; declaration of some kind, move to the proper anchor point for
9884 ;; that block.
9886 ;; This function might do hidden buffer changes.
9887 (unless (= (point) (c-point 'boi))
9888 (c-beginning-of-statement-1 lim)))
9890 (defun c-search-decl-header-end ()
9891 ;; Search forward for the end of the "header" of the current
9892 ;; declaration. That's the position where the definition body
9893 ;; starts, or the first variable initializer, or the ending
9894 ;; semicolon. I.e. search forward for the closest following
9895 ;; (syntactically relevant) '{', '=' or ';' token. Point is left
9896 ;; _after_ the first found token, or at point-max if none is found.
9898 ;; This function might do hidden buffer changes.
9900 (let ((base (point)))
9901 (if (c-major-mode-is 'c++-mode)
9903 ;; In C++ we need to take special care to handle operator
9904 ;; tokens and those pesky template brackets.
9905 (while (and
9906 (c-syntactic-re-search-forward "[;{<=]" nil 'move t t)
9908 (c-end-of-current-token base)
9909 ;; Handle operator identifiers, i.e. ignore any
9910 ;; operator token preceded by "operator".
9911 (save-excursion
9912 (and (c-safe (c-backward-sexp) t)
9913 (looking-at c-opt-op-identifier-prefix)))
9914 (and (eq (char-before) ?<)
9915 (c-with-syntax-table c++-template-syntax-table
9916 (if (c-safe (goto-char (c-up-list-forward (point))))
9918 (goto-char (point-max))
9919 nil)))))
9920 (setq base (point)))
9922 (while (and
9923 (c-syntactic-re-search-forward "[;{=]" nil 'move t t)
9924 (c-end-of-current-token base))
9925 (setq base (point))))))
9927 (defun c-beginning-of-decl-1 (&optional lim)
9928 ;; Go to the beginning of the current declaration, or the beginning
9929 ;; of the previous one if already at the start of it. Point won't
9930 ;; be moved out of any surrounding paren. Return a cons cell of the
9931 ;; form (MOVE . KNR-POS). MOVE is like the return value from
9932 ;; `c-beginning-of-statement-1'. If point skipped over some K&R
9933 ;; style argument declarations (and they are to be recognized) then
9934 ;; KNR-POS is set to the start of the first such argument
9935 ;; declaration, otherwise KNR-POS is nil. If LIM is non-nil, it's a
9936 ;; position that bounds the backward search.
9938 ;; NB: Cases where the declaration continues after the block, as in
9939 ;; "struct foo { ... } bar;", are currently recognized as two
9940 ;; declarations, e.g. "struct foo { ... }" and "bar;" in this case.
9942 ;; This function might do hidden buffer changes.
9943 (catch 'return
9944 (let* ((start (point))
9945 (last-stmt-start (point))
9946 (move (c-beginning-of-statement-1 lim nil t)))
9948 ;; `c-beginning-of-statement-1' stops at a block start, but we
9949 ;; want to continue if the block doesn't begin a top level
9950 ;; construct, i.e. if it isn't preceded by ';', '}', ':', bob,
9951 ;; or an open paren.
9952 (let ((beg (point)) tentative-move)
9953 ;; Go back one "statement" each time round the loop until we're just
9954 ;; after a ;, }, or :, or at BOB or the start of a macro or start of
9955 ;; an ObjC method. This will move over a multiple declaration whose
9956 ;; components are comma separated.
9957 (while (and
9958 ;; Must check with c-opt-method-key in ObjC mode.
9959 (not (and c-opt-method-key
9960 (looking-at c-opt-method-key)))
9961 (/= last-stmt-start (point))
9962 (progn
9963 (c-backward-syntactic-ws lim)
9964 (not (or (memq (char-before) '(?\; ?} ?: nil))
9965 (c-at-vsemi-p))))
9966 (save-excursion
9967 (backward-char)
9968 (not (looking-at "\\s(")))
9969 ;; Check that we don't move from the first thing in a
9970 ;; macro to its header.
9971 (not (eq (setq tentative-move
9972 (c-beginning-of-statement-1 lim nil t))
9973 'macro)))
9974 (setq last-stmt-start beg
9975 beg (point)
9976 move tentative-move))
9977 (goto-char beg))
9979 (when c-recognize-knr-p
9980 (let ((fallback-pos (point)) knr-argdecl-start)
9981 ;; Handle K&R argdecls. Back up after the "statement" jumped
9982 ;; over by `c-beginning-of-statement-1', unless it was the
9983 ;; function body, in which case we're sitting on the opening
9984 ;; brace now. Then test if we're in a K&R argdecl region and
9985 ;; that we started at the other side of the first argdecl in
9986 ;; it.
9987 (unless (eq (char-after) ?{)
9988 (goto-char last-stmt-start))
9989 (if (and (setq knr-argdecl-start (c-in-knr-argdecl lim))
9990 (< knr-argdecl-start start)
9991 (progn
9992 (goto-char knr-argdecl-start)
9993 (not (eq (c-beginning-of-statement-1 lim nil t) 'macro))))
9994 (throw 'return
9995 (cons (if (eq (char-after fallback-pos) ?{)
9996 'previous
9997 'same)
9998 knr-argdecl-start))
9999 (goto-char fallback-pos))))
10001 ;; `c-beginning-of-statement-1' counts each brace block as a separate
10002 ;; statement, so the result will be 'previous if we've moved over any.
10003 ;; So change our result back to 'same if necessary.
10005 ;; If they were brace list initializers we might not have moved over a
10006 ;; declaration boundary though, so change it to 'same if we've moved
10007 ;; past a '=' before '{', but not ';'. (This ought to be integrated
10008 ;; into `c-beginning-of-statement-1', so we avoid this extra pass which
10009 ;; potentially can search over a large amount of text.). Take special
10010 ;; pains not to get mislead by C++'s "operator=", and the like.
10011 (if (and (eq move 'previous)
10012 (c-with-syntax-table (if (c-major-mode-is 'c++-mode)
10013 c++-template-syntax-table
10014 (syntax-table))
10015 (save-excursion
10016 (and
10017 (progn
10018 (while ; keep going back to "[;={"s until we either find
10019 ; no more, or get to one which isn't an "operator ="
10020 (and (c-syntactic-re-search-forward "[;={]" start t t t)
10021 (eq (char-before) ?=)
10022 c-overloadable-operators-regexp
10023 c-opt-op-identifier-prefix
10024 (save-excursion
10025 (eq (c-backward-token-2) 0)
10026 (looking-at c-overloadable-operators-regexp)
10027 (eq (c-backward-token-2) 0)
10028 (looking-at c-opt-op-identifier-prefix))))
10029 (eq (char-before) ?=))
10030 (c-syntactic-re-search-forward "[;{]" start t t)
10031 (eq (char-before) ?{)
10032 (c-safe (goto-char (c-up-list-forward (point))) t)
10033 (not (c-syntactic-re-search-forward ";" start t t))))))
10034 (cons 'same nil)
10035 (cons move nil)))))
10037 (defun c-end-of-decl-1 ()
10038 ;; Assuming point is at the start of a declaration (as detected by
10039 ;; e.g. `c-beginning-of-decl-1'), go to the end of it. Unlike
10040 ;; `c-beginning-of-decl-1', this function handles the case when a
10041 ;; block is followed by identifiers in e.g. struct declarations in C
10042 ;; or C++. If a proper end was found then t is returned, otherwise
10043 ;; point is moved as far as possible within the current sexp and nil
10044 ;; is returned. This function doesn't handle macros; use
10045 ;; `c-end-of-macro' instead in those cases.
10047 ;; This function might do hidden buffer changes.
10048 (let ((start (point))
10049 (decl-syntax-table (if (c-major-mode-is 'c++-mode)
10050 c++-template-syntax-table
10051 (syntax-table))))
10052 (catch 'return
10053 (c-search-decl-header-end)
10055 (when (and c-recognize-knr-p
10056 (eq (char-before) ?\;)
10057 (c-in-knr-argdecl start))
10058 ;; Stopped at the ';' in a K&R argdecl section which is
10059 ;; detected using the same criteria as in
10060 ;; `c-beginning-of-decl-1'. Move to the following block
10061 ;; start.
10062 (c-syntactic-re-search-forward "{" nil 'move t))
10064 (when (eq (char-before) ?{)
10065 ;; Encountered a block in the declaration. Jump over it.
10066 (condition-case nil
10067 (goto-char (c-up-list-forward (point)))
10068 (error (goto-char (point-max))
10069 (throw 'return nil)))
10070 (if (or (not c-opt-block-decls-with-vars-key)
10071 (save-excursion
10072 (c-with-syntax-table decl-syntax-table
10073 (let ((lim (point)))
10074 (goto-char start)
10075 (not (and
10076 ;; Check for `c-opt-block-decls-with-vars-key'
10077 ;; before the first paren.
10078 (c-syntactic-re-search-forward
10079 (concat "[;=([{]\\|\\("
10080 c-opt-block-decls-with-vars-key
10081 "\\)")
10082 lim t t t)
10083 (match-beginning 1)
10084 (not (eq (char-before) ?_))
10085 ;; Check that the first following paren is
10086 ;; the block.
10087 (c-syntactic-re-search-forward "[;=([{]"
10088 lim t t t)
10089 (eq (char-before) ?{)))))))
10090 ;; The declaration doesn't have any of the
10091 ;; `c-opt-block-decls-with-vars' keywords in the
10092 ;; beginning, so it ends here at the end of the block.
10093 (throw 'return t)))
10095 (c-with-syntax-table decl-syntax-table
10096 (while (progn
10097 (if (eq (char-before) ?\;)
10098 (throw 'return t))
10099 (c-syntactic-re-search-forward ";" nil 'move t))))
10100 nil)))
10102 (defun c-looking-at-decl-block (_containing-sexp goto-start &optional limit)
10103 ;; Assuming the point is at an open brace, check if it starts a
10104 ;; block that contains another declaration level, i.e. that isn't a
10105 ;; statement block or a brace list, and if so return non-nil.
10107 ;; If the check is successful, the return value is the start of the
10108 ;; keyword that tells what kind of construct it is, i.e. typically
10109 ;; what `c-decl-block-key' matched. Also, if GOTO-START is set then
10110 ;; the point will be at the start of the construct, before any
10111 ;; leading specifiers, otherwise it's at the returned position.
10113 ;; The point is clobbered if the check is unsuccessful.
10115 ;; CONTAINING-SEXP is the position of the open of the surrounding
10116 ;; paren, or nil if none.
10118 ;; The optional LIMIT limits the backward search for the start of
10119 ;; the construct. It's assumed to be at a syntactically relevant
10120 ;; position.
10122 ;; If any template arglists are found in the searched region before
10123 ;; the open brace, they get marked with paren syntax.
10125 ;; This function might do hidden buffer changes.
10127 (let ((open-brace (point)) kwd-start first-specifier-pos)
10128 (c-syntactic-skip-backward c-block-prefix-charset limit t)
10130 (when (and c-recognize-<>-arglists
10131 (eq (char-before) ?>))
10132 ;; Could be at the end of a template arglist.
10133 (let ((c-parse-and-markup-<>-arglists t))
10134 (while (and
10135 (c-backward-<>-arglist nil limit)
10136 (progn
10137 (c-syntactic-skip-backward c-block-prefix-charset limit t)
10138 (eq (char-before) ?>))))))
10140 ;; Skip back over noise clauses.
10141 (while (and
10142 c-opt-cpp-prefix
10143 (eq (char-before) ?\))
10144 (let ((after-paren (point)))
10145 (if (and (c-go-list-backward)
10146 (progn (c-backward-syntactic-ws)
10147 (c-simple-skip-symbol-backward))
10148 (or (looking-at c-paren-nontype-key)
10149 (looking-at c-noise-macro-with-parens-name-re)))
10150 (progn
10151 (c-syntactic-skip-backward c-block-prefix-charset limit t)
10153 (goto-char after-paren)
10154 nil))))
10156 ;; Note: Can't get bogus hits inside template arglists below since they
10157 ;; have gotten paren syntax above.
10158 (when (and
10159 ;; If `goto-start' is set we begin by searching for the
10160 ;; first possible position of a leading specifier list.
10161 ;; The `c-decl-block-key' search continues from there since
10162 ;; we know it can't match earlier.
10163 (if goto-start
10164 (when (c-syntactic-re-search-forward c-symbol-start
10165 open-brace t t)
10166 (goto-char (setq first-specifier-pos (match-beginning 0)))
10170 (cond
10171 ((c-syntactic-re-search-forward c-decl-block-key open-brace t t t)
10172 (goto-char (setq kwd-start (match-beginning 0)))
10173 (and
10174 ;; Exclude cases where we matched what would ordinarily
10175 ;; be a block declaration keyword, except where it's not
10176 ;; legal because it's part of a "compound keyword" like
10177 ;; "enum class". Of course, if c-after-brace-list-key
10178 ;; is nil, we can skip the test.
10179 (or (equal c-after-brace-list-key "\\<\\>")
10180 (save-match-data
10181 (save-excursion
10182 (not
10183 (and
10184 (looking-at c-after-brace-list-key)
10185 (= (c-backward-token-2 1 t) 0)
10186 (looking-at c-brace-list-key))))))
10188 ;; Found a keyword that can't be a type?
10189 (match-beginning 1)
10191 ;; Can be a type too, in which case it's the return type of a
10192 ;; function (under the assumption that no declaration level
10193 ;; block construct starts with a type).
10194 (not (c-forward-type))
10196 ;; Jumped over a type, but it could be a declaration keyword
10197 ;; followed by the declared identifier that we've jumped over
10198 ;; instead (e.g. in "class Foo {"). If it indeed is a type
10199 ;; then we should be at the declarator now, so check for a
10200 ;; valid declarator start.
10202 ;; Note: This doesn't cope with the case when a declared
10203 ;; identifier is followed by e.g. '(' in a language where '('
10204 ;; also might be part of a declarator expression. Currently
10205 ;; there's no such language.
10206 (not (or (looking-at c-symbol-start)
10207 (looking-at c-type-decl-prefix-key))))))
10209 ;; In Pike a list of modifiers may be followed by a brace
10210 ;; to make them apply to many identifiers. Note that the
10211 ;; match data will be empty on return in this case.
10212 ((and (c-major-mode-is 'pike-mode)
10213 (progn
10214 (goto-char open-brace)
10215 (= (c-backward-token-2) 0))
10216 (looking-at c-specifier-key)
10217 ;; Use this variant to avoid yet another special regexp.
10218 (c-keyword-member (c-keyword-sym (match-string 1))
10219 'c-modifier-kwds))
10220 (setq kwd-start (point))
10221 t)))
10223 ;; Got a match.
10225 (if goto-start
10226 ;; Back up over any preceding specifiers and their clauses
10227 ;; by going forward from `first-specifier-pos', which is the
10228 ;; earliest possible position where the specifier list can
10229 ;; start.
10230 (progn
10231 (goto-char first-specifier-pos)
10233 (while (< (point) kwd-start)
10234 (if (looking-at c-symbol-key)
10235 ;; Accept any plain symbol token on the ground that
10236 ;; it's a specifier masked through a macro (just
10237 ;; like `c-forward-decl-or-cast-1' skip forward over
10238 ;; such tokens).
10240 ;; Could be more restrictive wrt invalid keywords,
10241 ;; but that'd only occur in invalid code so there's
10242 ;; no use spending effort on it.
10243 (let ((end (match-end 0))
10244 (kwd-sym (c-keyword-sym (match-string 0))))
10245 (unless
10246 (and kwd-sym
10247 ;; Moving over a protection kwd and the following
10248 ;; ":" (in C++ Mode) to the next token could take
10249 ;; us all the way up to `kwd-start', leaving us
10250 ;; no chance to update `first-specifier-pos'.
10251 (not (c-keyword-member kwd-sym 'c-protection-kwds))
10252 (c-forward-keyword-clause 0))
10253 (goto-char end)
10254 (c-forward-syntactic-ws)))
10256 ;; Can't parse a declaration preamble and is still
10257 ;; before `kwd-start'. That means `first-specifier-pos'
10258 ;; was in some earlier construct. Search again.
10259 (if (c-syntactic-re-search-forward c-symbol-start
10260 kwd-start 'move t)
10261 (goto-char (setq first-specifier-pos (match-beginning 0)))
10262 ;; Got no preamble before the block declaration keyword.
10263 (setq first-specifier-pos kwd-start))))
10265 (goto-char first-specifier-pos))
10266 (goto-char kwd-start))
10268 kwd-start)))
10270 (defun c-directly-in-class-called-p (name)
10271 ;; Check whether point is directly inside a brace block which is the brace
10272 ;; block of a class, struct, or union which is called NAME, a string.
10273 (let* ((paren-state (c-parse-state))
10274 (brace-pos (c-pull-open-brace paren-state))
10276 (when (eq (char-after brace-pos) ?{)
10277 (goto-char brace-pos)
10278 (save-excursion
10279 ; *c-looking-at-decl-block
10280 ; containing-sexp goto-start &optional
10281 ; limit)
10282 (when (and (c-looking-at-decl-block
10283 (c-pull-open-brace paren-state)
10284 nil)
10285 (looking-at c-class-key))
10286 (goto-char (match-end 1))
10287 (c-forward-syntactic-ws)
10288 (looking-at name))))))
10290 (defun c-search-uplist-for-classkey (paren-state)
10291 ;; Check if the closest containing paren sexp is a declaration
10292 ;; block, returning a 2 element vector in that case. Aref 0
10293 ;; contains the bufpos at boi of the class key line, and aref 1
10294 ;; contains the bufpos of the open brace. This function is an
10295 ;; obsolete wrapper for `c-looking-at-decl-block'.
10297 ;; This function might do hidden buffer changes.
10298 (let ((open-paren-pos (c-most-enclosing-brace paren-state)))
10299 (when open-paren-pos
10300 (save-excursion
10301 (goto-char open-paren-pos)
10302 (when (and (eq (char-after) ?{)
10303 (c-looking-at-decl-block
10304 (c-safe-position open-paren-pos paren-state)
10305 nil))
10306 (back-to-indentation)
10307 (vector (point) open-paren-pos))))))
10309 (defun c-most-enclosing-decl-block (paren-state)
10310 ;; Return the buffer position of the most enclosing decl-block brace (in the
10311 ;; sense of c-looking-at-decl-block) in the PAREN-STATE structure, or nil if
10312 ;; none was found.
10313 (let* ((open-brace (c-pull-open-brace paren-state))
10314 (next-open-brace (c-pull-open-brace paren-state)))
10315 (while (and open-brace
10316 (save-excursion
10317 (goto-char open-brace)
10318 (not (c-looking-at-decl-block next-open-brace nil))))
10319 (setq open-brace next-open-brace
10320 next-open-brace (c-pull-open-brace paren-state)))
10321 open-brace))
10323 (defun c-cheap-inside-bracelist-p (paren-state)
10324 ;; Return the position of the L-brace if point is inside a brace list
10325 ;; initialization of an array, etc. This is an approximate function,
10326 ;; designed for speed over accuracy. It will not find every bracelist, but
10327 ;; a non-nil result is reliable. We simply search for "= {" (naturally with
10328 ;; syntactic whitespace allowed). PAREN-STATE is the normal thing that it
10329 ;; is everywhere else.
10330 (let (b-pos)
10331 (save-excursion
10332 (while
10333 (and (setq b-pos (c-pull-open-brace paren-state))
10334 (progn (goto-char b-pos)
10335 (c-backward-sws)
10336 (c-backward-token-2)
10337 (not (looking-at "=")))))
10338 b-pos)))
10340 (defun c-backward-typed-enum-colon ()
10341 ;; We're at a "{" which might be the opening brace of a enum which is
10342 ;; strongly typed (by a ":" followed by a type). If this is the case, leave
10343 ;; point before the colon and return t. Otherwise leave point unchanged and return nil.
10344 ;; Match data will be clobbered.
10345 (let ((here (point))
10346 (colon-pos nil))
10347 (save-excursion
10348 (while
10349 (and (eql (c-backward-token-2) 0)
10350 (or (not (looking-at "\\s)"))
10351 (c-go-up-list-backward))
10352 (cond
10353 ((looking-at "::")
10355 ((and (eql (char-after) ?:)
10356 (save-excursion
10357 (c-backward-syntactic-ws)
10358 (or (c-on-identifier)
10359 (progn
10360 (c-backward-token-2)
10361 (looking-at c-brace-list-key)))))
10362 (setq colon-pos (point))
10363 (forward-char)
10364 (c-forward-syntactic-ws)
10365 (or (and (c-forward-type)
10366 (progn (c-forward-syntactic-ws)
10367 (eq (point) here)))
10368 (setq colon-pos nil))
10369 nil)
10370 ((eql (char-after) ?\()
10372 ((looking-at c-symbol-key)
10374 (t nil)))))
10375 (when colon-pos
10376 (goto-char colon-pos)
10377 t)))
10379 (defun c-backward-over-enum-header ()
10380 ;; We're at a "{". Move back to the enum-like keyword that starts this
10381 ;; declaration and return t, otherwise don't move and return nil.
10382 (let ((here (point))
10383 before-identifier)
10384 (when c-recognize-post-brace-list-type-p
10385 (c-backward-typed-enum-colon))
10386 (while
10387 (and
10388 (eq (c-backward-token-2) 0)
10389 (or (not (looking-at "\\s)"))
10390 (c-go-up-list-backward))
10391 (cond
10392 ((and (looking-at c-symbol-key) (c-on-identifier)
10393 (not before-identifier))
10394 (setq before-identifier t))
10395 ((and before-identifier
10396 (or (eql (char-after) ?,)
10397 (looking-at c-postfix-decl-spec-key)))
10398 (setq before-identifier nil)
10400 ((looking-at c-after-brace-list-key) t)
10401 ((looking-at c-brace-list-key) nil)
10402 ((eq (char-after) ?\()
10403 (and (eq (c-backward-token-2) 0)
10404 (or (looking-at c-decl-hangon-key)
10405 (and c-opt-cpp-prefix
10406 (looking-at c-noise-macro-with-parens-name-re)))))
10408 ((and c-recognize-<>-arglists
10409 (eq (char-after) ?<)
10410 (looking-at "\\s("))
10412 (t nil))))
10413 (or (looking-at c-brace-list-key)
10414 (progn (goto-char here) nil))))
10416 (defun c-looking-at-or-maybe-in-bracelist (&optional containing-sexp lim)
10417 ;; Point is at an open brace. If this starts a brace list, return a list
10418 ;; whose car is the buffer position of the start of the construct which
10419 ;; introduces the list, and whose cdr is the symbol `in-paren' if the brace
10420 ;; is directly enclosed in a parenthesis form (i.e. an arglist), t if we
10421 ;; have parsed a keyword matching `c-opt-inexpr-brace-list-key' (e.g. Java's
10422 ;; "new"), nil otherwise. Otherwise, if point might be inside an enclosing
10423 ;; brace list, return t. If point is definitely neither at nor in a brace
10424 ;; list, return nil.
10426 ;; CONTAINING-SEXP is the position of the brace/paren/bracket enclosing
10427 ;; POINT, or nil if there is no such position, or we do not know it. LIM is
10428 ;; a backward search limit.
10430 ;; The determination of whether the brace starts a brace list is solely by
10431 ;; the context of the brace, not by its contents.
10433 ;; Here, "brace list" does not include the body of an enum.
10434 (save-excursion
10435 (let ((start (point))
10436 (class-key
10437 ;; Pike can have class definitions anywhere, so we must
10438 ;; check for the class key here.
10439 (and (c-major-mode-is 'pike-mode)
10440 c-decl-block-key))
10441 (braceassignp 'dontknow)
10442 inexpr-brace-list bufpos macro-start res pos after-type-id-pos
10443 in-paren parens-before-brace)
10445 (setq res (c-backward-token-2 1 t lim))
10446 ;; Checks to do only on the first sexp before the brace.
10447 ;; Have we a C++ initialization, without an "="?
10448 (if (and (c-major-mode-is 'c++-mode)
10449 (cond
10450 ((and (or (not (eq res 0))
10451 (eq (char-after) ?,))
10452 (c-go-up-list-backward nil lim) ; FIXME!!! Check ; `lim' 2016-07-12.
10453 (eq (char-after) ?\())
10454 (setq braceassignp 'c++-noassign
10455 in-paren 'in-paren))
10456 ((looking-at c-pre-id-bracelist-key))
10457 ((looking-at c-return-key))
10458 ((and (looking-at c-symbol-start)
10459 (not (looking-at c-keywords-regexp)))
10460 (setq after-type-id-pos (point)))
10461 ((eq (char-after) ?\()
10462 (setq parens-before-brace t)
10463 nil)
10464 (t nil))
10465 (save-excursion
10466 (cond
10467 ((or (not (eq res 0))
10468 (eq (char-after) ?,))
10469 (and (c-go-up-list-backward nil lim) ; FIXME!!! Check `lim' 2016-07-12.
10470 (eq (char-after) ?\()
10471 (setq in-paren 'in-paren)))
10472 ((looking-at c-pre-id-bracelist-key))
10473 ((looking-at c-return-key))
10474 (t (setq after-type-id-pos (point))
10475 nil))))
10476 (setq braceassignp 'c++-noassign))
10478 (when (and c-opt-inexpr-brace-list-key
10479 (eq (char-after) ?\[))
10480 ;; In Java, an initialization brace list may follow
10481 ;; directly after "new Foo[]", so check for a "new"
10482 ;; earlier.
10483 (while (eq braceassignp 'dontknow)
10484 (setq braceassignp
10485 (cond ((/= (c-backward-token-2 1 t lim) 0) nil)
10486 ((looking-at c-opt-inexpr-brace-list-key)
10487 (setq inexpr-brace-list t)
10489 ((looking-at "\\sw\\|\\s_\\|[.[]")
10490 ;; Carry on looking if this is an
10491 ;; identifier (may contain "." in Java)
10492 ;; or another "[]" sexp.
10493 'dontknow)
10494 (t nil)))))
10496 (setq pos (point))
10497 (cond
10498 ((and after-type-id-pos
10499 (goto-char after-type-id-pos)
10500 (setq res (c-back-over-member-initializers))
10501 (goto-char res)
10502 (eq (car (c-beginning-of-decl-1 lim)) 'same))
10503 (cons (point) nil)) ; Return value.
10505 ((and after-type-id-pos
10506 (progn
10507 (c-backward-syntactic-ws)
10508 (eq (char-before) ?\()))
10509 ;; Single identifier between '(' and '{'. We have a bracelist.
10510 (cons after-type-id-pos 'in-paren))
10512 ;; Are we at the parens of a C++ lambda expression?
10513 ((and parens-before-brace
10514 (save-excursion
10515 (and
10516 (zerop (c-backward-token-2 1 t lim))
10517 (c-looking-at-c++-lambda-capture-list))))
10518 nil) ; a lambda expression isn't a brace list.
10521 (goto-char pos)
10522 ;; Checks to do on all sexps before the brace, up to the
10523 ;; beginning of the statement.
10524 (while (eq braceassignp 'dontknow)
10525 (cond ((eq (char-after) ?\;)
10526 (setq braceassignp nil))
10527 ((and class-key
10528 (looking-at class-key))
10529 (setq braceassignp nil))
10530 ((eq (char-after) ?=)
10531 ;; We've seen a =, but must check earlier tokens so
10532 ;; that it isn't something that should be ignored.
10533 (setq braceassignp 'maybe)
10534 (while (and (eq braceassignp 'maybe)
10535 (zerop (c-backward-token-2 1 t lim)))
10536 (setq braceassignp
10537 (cond
10538 ;; Check for operator =
10539 ((and c-opt-op-identifier-prefix
10540 (looking-at c-opt-op-identifier-prefix))
10541 nil)
10542 ;; Check for `<opchar>= in Pike.
10543 ((and (c-major-mode-is 'pike-mode)
10544 (or (eq (char-after) ?`)
10545 ;; Special case for Pikes
10546 ;; `[]=, since '[' is not in
10547 ;; the punctuation class.
10548 (and (eq (char-after) ?\[)
10549 (eq (char-before) ?`))))
10550 nil)
10551 ((looking-at "\\s.") 'maybe)
10552 ;; make sure we're not in a C++ template
10553 ;; argument assignment
10554 ((and
10555 (c-major-mode-is 'c++-mode)
10556 (save-excursion
10557 (let ((here (point))
10558 (pos< (progn
10559 (skip-chars-backward "^<>")
10560 (point))))
10561 (and (eq (char-before) ?<)
10562 (not (c-crosses-statement-barrier-p
10563 pos< here))
10564 (not (c-in-literal))
10565 ))))
10566 nil)
10567 (t t))))))
10568 (if (and (eq braceassignp 'dontknow)
10569 (/= (c-backward-token-2 1 t lim) 0))
10570 (setq braceassignp nil)))
10572 (cond
10573 (braceassignp
10574 ;; We've hit the beginning of the aggregate list.
10575 (c-beginning-of-statement-1 containing-sexp)
10576 (cons (point) (or in-paren inexpr-brace-list)))
10577 ((and after-type-id-pos
10578 (save-excursion
10579 (when (eq (char-after) ?\;)
10580 (c-forward-over-token-and-ws t))
10581 (setq bufpos (point))
10582 (when (looking-at c-opt-<>-sexp-key)
10583 (c-forward-over-token-and-ws)
10584 (when (and (eq (char-after) ?<)
10585 (c-get-char-property (point) 'syntax-table))
10586 (c-go-list-forward nil after-type-id-pos)
10587 (c-forward-syntactic-ws)))
10588 (and
10589 (or (not (looking-at c-class-key))
10590 (save-excursion
10591 (goto-char (match-end 1))
10592 (c-forward-syntactic-ws)
10593 (not (eq (point) after-type-id-pos))))
10594 (progn
10595 (setq res
10596 (c-forward-decl-or-cast-1
10597 (save-excursion (c-backward-syntactic-ws) (point))
10598 nil nil))
10599 (and (consp res)
10600 (eq (car res) after-type-id-pos))))))
10601 (cons bufpos (or in-paren inexpr-brace-list)))
10602 ((eq (char-after) ?\;)
10603 ;; Brace lists can't contain a semicolon, so we're done.
10604 ;; (setq containing-sexp nil)
10605 nil)
10606 ((and (setq macro-start (point))
10607 (c-forward-to-cpp-define-body)
10608 (eq (point) start))
10609 ;; We've a macro whose expansion starts with the '{'.
10610 ;; Heuristically, if we have a ';' in it we've not got a
10611 ;; brace list, otherwise we have.
10612 (let ((macro-end (progn (c-end-of-macro) (point))))
10613 (goto-char start)
10614 (forward-char)
10615 (if (and (c-syntactic-re-search-forward "[;,]" macro-end t t)
10616 (eq (char-before) ?\;))
10618 (cons macro-start nil)))) ; (2016-08-30): Lazy! We have no
10619 ; languages where
10620 ; `c-opt-inexpr-brace-list-key' is
10621 ; non-nil and we have macros.
10622 (t t)))) ;; The caller can go up one level.
10625 (defun c-inside-bracelist-p (containing-sexp paren-state accept-in-paren)
10626 ;; return the buffer position of the beginning of the brace list
10627 ;; statement if we're inside a brace list, otherwise return nil.
10628 ;; CONTAINING-SEXP is the buffer pos of the innermost containing
10629 ;; paren. PAREN-STATE is the remainder of the state of enclosing
10630 ;; braces. ACCEPT-IN-PAREN is non-nil iff we will accept as a brace
10631 ;; list a brace directly enclosed in a parenthesis.
10633 ;; The "brace list" here is recognized solely by its context, not by
10634 ;; its contents.
10636 ;; N.B.: This algorithm can potentially get confused by cpp macros
10637 ;; placed in inconvenient locations. It's a trade-off we make for
10638 ;; speed.
10640 ;; This function might do hidden buffer changes.
10642 ;; This will pick up brace list declarations.
10643 (save-excursion
10644 (goto-char containing-sexp)
10645 (c-backward-over-enum-header))
10646 ;; this will pick up array/aggregate init lists, even if they are nested.
10647 (save-excursion
10648 (let ((bufpos t)
10649 next-containing)
10650 (while (and (eq bufpos t)
10651 containing-sexp)
10652 (when paren-state
10653 (setq next-containing (c-pull-open-brace paren-state)))
10655 (goto-char containing-sexp)
10656 (if (c-looking-at-inexpr-block next-containing next-containing)
10657 ;; We're in an in-expression block of some kind. Do not
10658 ;; check nesting. We deliberately set the limit to the
10659 ;; containing sexp, so that c-looking-at-inexpr-block
10660 ;; doesn't check for an identifier before it.
10661 (setq bufpos nil)
10662 (if (not (eq (char-after) ?{))
10663 (setq bufpos nil)
10664 (when (eq (setq bufpos (c-looking-at-or-maybe-in-bracelist
10665 next-containing next-containing))
10667 (setq containing-sexp next-containing
10668 next-containing nil)))))
10669 (and (consp bufpos)
10670 (or accept-in-paren (not (eq (cdr bufpos) 'in-paren)))
10671 (car bufpos))))))
10673 (defun c-looking-at-special-brace-list (&optional _lim)
10674 ;; If we're looking at the start of a pike-style list, i.e., `({ })',
10675 ;; `([ ])', `(< >)', etc., a cons of a cons of its starting and ending
10676 ;; positions and its entry in c-special-brace-lists is returned, nil
10677 ;; otherwise. The ending position is nil if the list is still open.
10678 ;; LIM is the limit for forward search. The point may either be at
10679 ;; the `(' or at the following paren character. Tries to check the
10680 ;; matching closer, but assumes it's correct if no balanced paren is
10681 ;; found (i.e. the case `({ ... } ... )' is detected as _not_ being
10682 ;; a special brace list).
10684 ;; This function might do hidden buffer changes.
10685 (if c-special-brace-lists
10686 (condition-case ()
10687 (save-excursion
10688 (let ((beg (point))
10689 inner-beg end type)
10690 (c-forward-syntactic-ws)
10691 (if (eq (char-after) ?\()
10692 (progn
10693 (forward-char 1)
10694 (c-forward-syntactic-ws)
10695 (setq inner-beg (point))
10696 (setq type (assq (char-after) c-special-brace-lists)))
10697 (if (setq type (assq (char-after) c-special-brace-lists))
10698 (progn
10699 (setq inner-beg (point))
10700 (c-backward-syntactic-ws)
10701 (forward-char -1)
10702 (setq beg (if (eq (char-after) ?\()
10703 (point)
10704 nil)))))
10705 (if (and beg type)
10706 (if (and (c-safe
10707 (goto-char beg)
10708 (c-forward-sexp 1)
10709 (setq end (point))
10710 (= (char-before) ?\)))
10711 (c-safe
10712 (goto-char inner-beg)
10713 (if (looking-at "\\s(")
10714 ;; Check balancing of the inner paren
10715 ;; below.
10716 (progn
10717 (c-forward-sexp 1)
10719 ;; If the inner char isn't a paren then
10720 ;; we can't check balancing, so just
10721 ;; check the char before the outer
10722 ;; closing paren.
10723 (goto-char end)
10724 (backward-char)
10725 (c-backward-syntactic-ws)
10726 (= (char-before) (cdr type)))))
10727 (if (or (/= (char-syntax (char-before)) ?\))
10728 (= (progn
10729 (c-forward-syntactic-ws)
10730 (point))
10731 (1- end)))
10732 (cons (cons beg end) type))
10733 (cons (list beg) type)))))
10734 (error nil))))
10736 (defun c-looking-at-bos (&optional _lim)
10737 ;; Return non-nil if between two statements or declarations, assuming
10738 ;; point is not inside a literal or comment.
10740 ;; Obsolete - `c-at-statement-start-p' or `c-at-expression-start-p'
10741 ;; are recommended instead.
10743 ;; This function might do hidden buffer changes.
10744 (c-at-statement-start-p))
10745 (make-obsolete 'c-looking-at-bos 'c-at-statement-start-p "22.1")
10747 (defun c-looking-at-statement-block ()
10748 ;; Point is at an opening brace. If this is a statement block (i.e. the
10749 ;; elements in the block are terminated by semicolons, or the block is
10750 ;; empty, or the block contains a keyword) return t. Otherwise, return nil.
10751 (let ((here (point)))
10752 (prog1
10753 (if (c-go-list-forward)
10754 (let ((there (point)))
10755 (backward-char)
10756 (c-syntactic-skip-backward "^;," here t)
10757 (cond
10758 ((eq (char-before) ?\;) t)
10759 ((eq (char-before) ?,) nil)
10760 (t ; We're at (1+ here).
10761 (cond
10762 ((progn (c-forward-syntactic-ws)
10763 (eq (point) (1- there))))
10764 ((c-syntactic-re-search-forward c-keywords-regexp there t))
10765 ((c-syntactic-re-search-forward "{" there t t)
10766 (backward-char)
10767 (c-looking-at-statement-block))
10768 (t nil)))))
10769 (forward-char)
10770 (cond
10771 ((c-syntactic-re-search-forward "[;,]" nil t t)
10772 (eq (char-before) ?\;))
10773 ((progn (c-forward-syntactic-ws)
10774 (eobp)))
10775 ((c-syntactic-re-search-forward c-keywords-regexp nil t t))
10776 ((c-syntactic-re-search-forward "{" nil t t)
10777 (backward-char)
10778 (c-looking-at-statement-block))
10779 (t nil)))
10780 (goto-char here))))
10782 (defun c-looking-at-inexpr-block (lim containing-sexp &optional check-at-end)
10783 ;; Return non-nil if we're looking at the beginning of a block
10784 ;; inside an expression. The value returned is actually a cons of
10785 ;; either 'inlambda, 'inexpr-statement or 'inexpr-class and the
10786 ;; position of the beginning of the construct.
10788 ;; LIM limits the backward search. CONTAINING-SEXP is the start
10789 ;; position of the closest containing list. If it's nil, the
10790 ;; containing paren isn't used to decide whether we're inside an
10791 ;; expression or not. If both LIM and CONTAINING-SEXP are used, LIM
10792 ;; needs to be farther back.
10794 ;; If CHECK-AT-END is non-nil then extra checks at the end of the
10795 ;; brace block might be done. It should only be used when the
10796 ;; construct can be assumed to be complete, i.e. when the original
10797 ;; starting position was further down than that.
10799 ;; This function might do hidden buffer changes.
10801 (save-excursion
10802 (let ((res 'maybe) (passed-bracket-pairs 0) bracket-pos passed-paren
10803 haskell-op-pos
10804 (closest-lim (or containing-sexp lim (point-min)))
10805 ;; Look at the character after point only as a last resort
10806 ;; when we can't disambiguate.
10807 (block-follows (and (eq (char-after) ?{) (point))))
10809 ;; Search for a C++11 "->" which suggests a lambda declaration.
10810 (when (and (c-major-mode-is 'c++-mode)
10811 (setq haskell-op-pos
10812 (save-excursion
10813 (while
10814 (progn
10815 (c-syntactic-skip-backward "^;=}>" closest-lim t)
10816 (and (eq (char-before) ?>)
10817 (c-backward-token-2)
10818 (not (looking-at c-haskell-op-re)))))
10819 (and (looking-at c-haskell-op-re)
10820 (point)))))
10821 (goto-char haskell-op-pos))
10823 (while (and (eq res 'maybe)
10824 (progn (c-backward-syntactic-ws)
10825 (> (point) closest-lim))
10826 (not (bobp))
10827 (progn (backward-char)
10828 (looking-at "[]).]\\|\\w\\|\\s_"))
10829 (c-safe (forward-char)
10830 (goto-char (scan-sexps (point) -1))))
10832 (setq res
10833 (if (looking-at c-keywords-regexp)
10834 (let ((kw-sym (c-keyword-sym (match-string 1))))
10835 (cond
10836 ((and block-follows
10837 (c-keyword-member kw-sym 'c-inexpr-class-kwds))
10838 (and (not (eq passed-paren ?\[))
10839 (or (not (looking-at c-class-key))
10840 ;; If the class definition is at the start of
10841 ;; a statement, we don't consider it an
10842 ;; in-expression class.
10843 (let ((prev (point)))
10844 (while (and
10845 (= (c-backward-token-2 1 nil closest-lim) 0)
10846 (eq (char-syntax (char-after)) ?w))
10847 (setq prev (point)))
10848 (goto-char prev)
10849 (not (c-at-statement-start-p)))
10850 ;; Also, in Pike we treat it as an
10851 ;; in-expression class if it's used in an
10852 ;; object clone expression.
10853 (save-excursion
10854 (and check-at-end
10855 (c-major-mode-is 'pike-mode)
10856 (progn (goto-char block-follows)
10857 (zerop (c-forward-token-2 1 t)))
10858 (eq (char-after) ?\())))
10859 (cons 'inexpr-class (point))))
10860 ((c-keyword-member kw-sym 'c-paren-any-kwds) ; e.g. C++11 "throw" or "noexcept"
10861 (setq passed-paren nil)
10862 (setq passed-bracket-pairs 0)
10863 (setq bracket-pos nil)
10864 'maybe)
10865 ((c-keyword-member kw-sym 'c-inexpr-block-kwds)
10866 (when (not passed-paren)
10867 (cons 'inexpr-statement (point))))
10868 ((c-keyword-member kw-sym 'c-lambda-kwds)
10869 (when (or (not passed-paren)
10870 (eq passed-paren ?\())
10871 (cons 'inlambda (point))))
10872 ((c-keyword-member kw-sym 'c-block-stmt-kwds)
10873 nil)
10875 'maybe)))
10877 (if (looking-at "\\s(")
10878 (if passed-paren
10879 (cond
10880 ((and (eq passed-paren ?\[)
10881 (eq (char-after) ?\[)
10882 (not (eq (char-after (1+ (point))) ?\[))) ; C++ attribute.
10883 ;; Accept several square bracket sexps for
10884 ;; Java array initializations.
10885 (setq passed-bracket-pairs (1+ passed-bracket-pairs))
10886 'maybe)
10887 ((and (eq passed-paren ?\()
10888 (eq (char-after) ?\[)
10889 (not (eq (char-after (1+ (point))) ?\[))
10890 (eq passed-bracket-pairs 0))
10891 ;; C++11 lambda function declaration
10892 (setq passed-bracket-pairs 1)
10893 (setq bracket-pos (point))
10894 'maybe)
10895 (t nil))
10896 (when (not (looking-at "\\[\\["))
10897 (setq passed-paren (char-after))
10898 (when (eq passed-paren ?\[)
10899 (setq passed-bracket-pairs 1)
10900 (setq bracket-pos (point))))
10901 'maybe)
10902 'maybe))))
10904 (if (eq res 'maybe)
10905 (cond
10906 ((and (c-major-mode-is 'c++-mode)
10907 block-follows
10908 (eq passed-bracket-pairs 1)
10909 (save-excursion
10910 (goto-char bracket-pos)
10911 (or (<= (point) (or lim (point-min)))
10912 (progn
10913 (c-backward-token-2 1 nil lim)
10914 (and
10915 (not (and (c-on-identifier)
10916 (looking-at c-symbol-chars)))
10917 (not (looking-at c-opt-op-identifier-prefix)))))))
10918 (cons 'inlambda bracket-pos))
10919 ((and c-recognize-paren-inexpr-blocks
10920 block-follows
10921 containing-sexp
10922 (eq (char-after containing-sexp) ?\())
10923 (goto-char containing-sexp)
10924 (if (or (save-excursion
10925 (c-backward-syntactic-ws lim)
10926 (while (and (eq (char-before) ?>)
10927 (c-get-char-property (1- (point))
10928 'syntax-table)
10929 (c-go-list-backward nil lim))
10930 (c-backward-syntactic-ws lim))
10931 (and (> (point) (or lim (point-min)))
10932 (c-on-identifier)))
10933 (and c-special-brace-lists
10934 (c-looking-at-special-brace-list))
10935 (and (c-major-mode-is 'c++-mode)
10936 (save-excursion
10937 (goto-char block-follows)
10938 (not (c-looking-at-statement-block)))))
10940 (cons 'inexpr-statement (point)))))
10942 res))))
10944 (defun c-looking-at-inexpr-block-backward (paren-state)
10945 ;; Returns non-nil if we're looking at the end of an in-expression
10946 ;; block, otherwise the same as `c-looking-at-inexpr-block'.
10947 ;; PAREN-STATE is the paren state relevant at the current position.
10949 ;; This function might do hidden buffer changes.
10950 (save-excursion
10951 ;; We currently only recognize a block.
10952 (let ((here (point))
10953 (elem (car-safe paren-state))
10954 containing-sexp)
10955 (when (and (consp elem)
10956 (progn (goto-char (cdr elem))
10957 (c-forward-syntactic-ws here)
10958 (= (point) here)))
10959 (goto-char (car elem))
10960 (if (setq paren-state (cdr paren-state))
10961 (setq containing-sexp (car-safe paren-state)))
10962 (c-looking-at-inexpr-block (c-safe-position containing-sexp
10963 paren-state)
10964 containing-sexp)))))
10966 (defun c-looking-at-c++-lambda-capture-list ()
10967 ;; Return non-nil if we're at the opening "[" of the capture list of a C++
10968 ;; lambda function, nil otherwise.
10969 (and
10970 (eq (char-after) ?\[)
10971 (not (eq (char-before) ?\[))
10972 (not (eq (char-after (1+ (point))) ?\[))
10973 (save-excursion
10974 (or (eq (c-backward-token-2 1) 1)
10975 (looking-at c-pre-lambda-tokens-re)))
10976 (not (c-in-literal))))
10978 (defun c-at-macro-vsemi-p (&optional pos)
10979 ;; Is there a "virtual semicolon" at POS or point?
10980 ;; (See cc-defs.el for full details of "virtual semicolons".)
10982 ;; This is true when point is at the last non syntactic WS position on the
10983 ;; line, there is a macro call last on the line, and this particular macro's
10984 ;; name is defined by the regexp `c-vs-macro-regexp' as not needing a
10985 ;; semicolon.
10986 (save-excursion
10987 (save-restriction
10988 (widen)
10989 (if pos
10990 (goto-char pos)
10991 (setq pos (point)))
10992 (and
10993 c-macro-with-semi-re
10994 (eq (skip-chars-backward " \t") 0)
10996 ;; Check we've got nothing after this except comments and empty lines
10997 ;; joined by escaped EOLs.
10998 (skip-chars-forward " \t") ; always returns non-nil.
10999 (progn
11000 (while ; go over 1 block comment per iteration.
11001 (and
11002 (looking-at "\\(\\\\[\n\r][ \t]*\\)*")
11003 (goto-char (match-end 0))
11004 (cond
11005 ((looking-at c-block-comment-start-regexp)
11006 (and (forward-comment 1)
11007 (skip-chars-forward " \t"))) ; always returns non-nil
11008 ((looking-at c-line-comment-start-regexp)
11009 (end-of-line)
11010 nil)
11011 (t nil))))
11012 (eolp))
11014 (goto-char pos)
11015 (progn (c-backward-syntactic-ws)
11016 (eq (point) pos))
11018 ;; Check for one of the listed macros being before point.
11019 (or (not (eq (char-before) ?\)))
11020 (when (c-go-list-backward)
11021 (c-backward-syntactic-ws)
11023 (c-simple-skip-symbol-backward)
11024 (looking-at c-macro-with-semi-re)
11025 (goto-char pos)
11026 (not (c-in-literal)))))) ; The most expensive check last.
11028 (defun c-macro-vsemi-status-unknown-p () t) ; See cc-defs.el.
11031 ;; `c-guess-basic-syntax' and the functions that precedes it below
11032 ;; implements the main decision tree for determining the syntactic
11033 ;; analysis of the current line of code.
11035 ;; Dynamically bound to t when `c-guess-basic-syntax' is called during
11036 ;; auto newline analysis.
11037 (defvar c-auto-newline-analysis nil)
11039 (defun c-brace-anchor-point (bracepos)
11040 ;; BRACEPOS is the position of a brace in a construct like "namespace
11041 ;; Bar {". Return the anchor point in this construct; this is the
11042 ;; earliest symbol on the brace's line which isn't earlier than
11043 ;; "namespace".
11045 ;; Currently (2007-08-17), "like namespace" means "matches
11046 ;; c-other-block-decl-kwds". It doesn't work with "class" or "struct"
11047 ;; or anything like that.
11048 (save-excursion
11049 (let ((boi (c-point 'boi bracepos)))
11050 (goto-char bracepos)
11051 (while (and (> (point) boi)
11052 (not (looking-at c-other-decl-block-key)))
11053 (c-backward-token-2))
11054 (if (> (point) boi) (point) boi))))
11056 (defsubst c-add-syntax (symbol &rest args)
11057 ;; A simple function to prepend a new syntax element to
11058 ;; `c-syntactic-context'. Using `setq' on it is unsafe since it
11059 ;; should always be dynamically bound but since we read it first
11060 ;; we'll fail properly anyway if this function is misused.
11061 (setq c-syntactic-context (cons (cons symbol args)
11062 c-syntactic-context)))
11064 (defsubst c-append-syntax (symbol &rest args)
11065 ;; Like `c-add-syntax' but appends to the end of the syntax list.
11066 ;; (Normally not necessary.)
11067 (setq c-syntactic-context (nconc c-syntactic-context
11068 (list (cons symbol args)))))
11070 (defun c-add-stmt-syntax (syntax-symbol
11071 syntax-extra-args
11072 stop-at-boi-only
11073 containing-sexp
11074 paren-state
11075 &optional fixed-anchor)
11076 ;; Add the indicated SYNTAX-SYMBOL to `c-syntactic-context', extending it as
11077 ;; needed with further syntax elements of the types `substatement',
11078 ;; `inexpr-statement', `arglist-cont-nonempty', `statement-block-intro',
11079 ;; `defun-block-intro', and `brace-list-intro'.
11081 ;; Do the generic processing to anchor the given syntax symbol on the
11082 ;; preceding statement: First skip over any labels and containing statements
11083 ;; on the same line. If FIXED-ANCHOR is non-nil, use this as the
11084 ;; anchor-point for the given syntactic symbol, and don't make syntactic
11085 ;; entries for constructs beginning on lines before that containing
11086 ;; ANCHOR-POINT. Otherwise search backward until we find a statement or
11087 ;; block start that begins at boi without a label or comment.
11089 ;; Point is assumed to be at the prospective anchor point for the
11090 ;; given SYNTAX-SYMBOL. More syntax entries are added if we need to
11091 ;; skip past open parens and containing statements. Most of the added
11092 ;; syntax elements will get the same anchor point - the exception is
11093 ;; for an anchor in a construct like "namespace"[*] - this is as early
11094 ;; as possible in the construct but on the same line as the {.
11096 ;; [*] i.e. with a keyword matching c-other-block-decl-kwds.
11098 ;; SYNTAX-EXTRA-ARGS are a list of the extra arguments for the
11099 ;; syntax symbol. They are appended after the anchor point.
11101 ;; If STOP-AT-BOI-ONLY is nil, we can stop in the middle of the line
11102 ;; if the current statement starts there.
11104 ;; Note: It's not a problem if PAREN-STATE "overshoots"
11105 ;; CONTAINING-SEXP, i.e. contains info about parens further down.
11107 ;; This function might do hidden buffer changes.
11109 (if (= (point) (c-point 'boi))
11110 ;; This is by far the most common case, so let's give it special
11111 ;; treatment.
11112 (apply 'c-add-syntax syntax-symbol (point) syntax-extra-args)
11114 (let ((syntax-last c-syntactic-context)
11115 (boi (c-point 'boi))
11116 (anchor-boi (c-point 'boi))
11117 ;; Set when we're on a label, so that we don't stop there.
11118 ;; FIXME: To be complete we should check if we're on a label
11119 ;; now at the start.
11120 on-label)
11122 ;; Use point as the anchor point for "namespace", "extern", etc.
11123 (apply 'c-add-syntax syntax-symbol
11124 (if (rassq syntax-symbol c-other-decl-block-key-in-symbols-alist)
11125 (point) nil)
11126 syntax-extra-args)
11128 ;; Loop while we have to back out of containing blocks.
11129 (while
11130 (and
11131 (catch 'back-up-block
11133 ;; Loop while we have to back up statements.
11134 (while (or (/= (point) boi)
11135 on-label
11136 (looking-at c-comment-start-regexp))
11138 ;; Skip past any comments that stands between the
11139 ;; statement start and boi.
11140 (let ((savepos (point)))
11141 (while (and (/= savepos boi)
11142 (c-backward-single-comment))
11143 (setq savepos (point)
11144 boi (c-point 'boi)))
11145 (goto-char savepos))
11147 ;; Skip to the beginning of this statement or backward
11148 ;; another one.
11149 (let ((old-pos (point))
11150 (old-boi boi)
11151 (step-type (c-beginning-of-statement-1 containing-sexp)))
11152 (setq boi (c-point 'boi)
11153 on-label (eq step-type 'label))
11155 (cond ((= (point) old-pos)
11156 ;; If we didn't move we're at the start of a block and
11157 ;; have to continue outside it.
11158 (throw 'back-up-block t))
11160 ((and (eq step-type 'up)
11161 (>= (point) old-boi)
11162 (looking-at "else\\>[^_]")
11163 (save-excursion
11164 (goto-char old-pos)
11165 (looking-at "if\\>[^_]")))
11166 ;; Special case to avoid deeper and deeper indentation
11167 ;; of "else if" clauses.
11170 ((and (not stop-at-boi-only)
11171 (/= old-pos old-boi)
11172 (memq step-type '(up previous)))
11173 ;; If stop-at-boi-only is nil, we shouldn't back up
11174 ;; over previous or containing statements to try to
11175 ;; reach boi, so go back to the last position and
11176 ;; exit.
11177 (goto-char old-pos)
11178 (throw 'back-up-block nil))
11181 (if (and (not stop-at-boi-only)
11182 (memq step-type '(up previous beginning)))
11183 ;; If we've moved into another statement then we
11184 ;; should no longer try to stop in the middle of a
11185 ;; line.
11186 (setq stop-at-boi-only t))
11188 ;; Record this as a substatement if we skipped up one
11189 ;; level.
11190 (when (eq step-type 'up)
11191 (c-add-syntax 'substatement nil))))
11194 containing-sexp
11195 (or (null fixed-anchor)
11196 (> containing-sexp anchor-boi)))
11198 ;; Now we have to go out of this block.
11199 (goto-char containing-sexp)
11201 ;; Don't stop in the middle of a special brace list opener
11202 ;; like "({".
11203 (when c-special-brace-lists
11204 (let ((special-list (c-looking-at-special-brace-list)))
11205 (when (and special-list
11206 (< (car (car special-list)) (point)))
11207 (setq containing-sexp (car (car special-list)))
11208 (goto-char containing-sexp))))
11210 (setq paren-state (c-whack-state-after containing-sexp paren-state)
11211 containing-sexp (c-most-enclosing-brace paren-state)
11212 boi (c-point 'boi))
11214 ;; Analyze the construct in front of the block we've stepped out
11215 ;; from and add the right syntactic element for it.
11216 (let ((paren-pos (point))
11217 (paren-char (char-after))
11218 step-type)
11220 (if (eq paren-char ?\()
11221 ;; Stepped out of a parenthesis block, so we're in an
11222 ;; expression now.
11223 (progn
11224 (when (/= paren-pos boi)
11225 (if (and c-recognize-paren-inexpr-blocks
11226 (progn
11227 (c-backward-syntactic-ws containing-sexp)
11228 (or (not (looking-at "\\>"))
11229 (not (c-on-identifier))))
11230 (save-excursion
11231 (goto-char (1+ paren-pos))
11232 (c-forward-syntactic-ws)
11233 (eq (char-after) ?{)))
11234 ;; Stepped out of an in-expression statement. This
11235 ;; syntactic element won't get an anchor pos.
11236 (c-add-syntax 'inexpr-statement)
11238 ;; A parenthesis normally belongs to an arglist.
11239 (c-add-syntax 'arglist-cont-nonempty nil paren-pos)))
11241 (goto-char (max boi
11242 (if containing-sexp
11243 (1+ containing-sexp)
11244 (point-min))))
11245 (setq step-type 'same
11246 on-label nil))
11248 ;; Stepped out of a brace block.
11249 (setq step-type (c-beginning-of-statement-1 containing-sexp)
11250 on-label (eq step-type 'label))
11252 (if (and (eq step-type 'same)
11253 (/= paren-pos (point)))
11254 (let (inexpr)
11255 (cond
11256 ((save-excursion
11257 (goto-char paren-pos)
11258 (setq inexpr (c-looking-at-inexpr-block
11259 (c-safe-position containing-sexp paren-state)
11260 containing-sexp)))
11261 (c-add-syntax (if (eq (car inexpr) 'inlambda)
11262 'defun-block-intro
11263 'statement-block-intro)
11264 nil))
11265 ((looking-at c-other-decl-block-key)
11266 (c-add-syntax
11267 (cdr (assoc (match-string 1)
11268 c-other-decl-block-key-in-symbols-alist))
11269 (max (c-point 'boi paren-pos) (point))))
11270 ((save-excursion
11271 (goto-char paren-pos)
11272 (c-looking-at-or-maybe-in-bracelist containing-sexp))
11273 (if (save-excursion
11274 (goto-char paren-pos)
11275 (c-looking-at-statement-block))
11276 (c-add-syntax 'defun-block-intro nil)
11277 (c-add-syntax 'brace-list-intro nil)))
11278 (t (c-add-syntax 'defun-block-intro nil))))
11280 (c-add-syntax 'statement-block-intro nil)))
11282 (if (= paren-pos boi)
11283 ;; Always done if the open brace was at boi. The
11284 ;; c-beginning-of-statement-1 call above is necessary
11285 ;; anyway, to decide the type of block-intro to add.
11286 (goto-char paren-pos)
11287 (setq boi (c-point 'boi)))
11290 ;; Fill in the current point as the anchor for all the symbols
11291 ;; added above.
11292 (let ((p c-syntactic-context) q)
11293 (while (not (eq p syntax-last))
11294 (setq q (cdr (car p))) ; e.g. (nil 28) [from (arglist-cont-nonempty nil 28)]
11295 (while q
11296 (unless (car q)
11297 (setcar q (if (or (cdr p)
11298 (null fixed-anchor))
11299 (point)
11300 fixed-anchor)))
11301 (setq q (cdr q)))
11302 (setq p (cdr p))))
11305 (defun c-add-class-syntax (symbol
11306 containing-decl-open
11307 containing-decl-start
11308 containing-decl-kwd
11309 _paren-state)
11310 ;; The inclass and class-close syntactic symbols are added in
11311 ;; several places and some work is needed to fix everything.
11312 ;; Therefore it's collected here.
11314 ;; This function might do hidden buffer changes.
11315 (goto-char containing-decl-open)
11316 (if (and (eq symbol 'inclass) (= (point) (c-point 'boi)))
11317 (progn
11318 (c-add-syntax symbol containing-decl-open)
11319 containing-decl-open)
11320 (goto-char containing-decl-start)
11321 ;; Ought to use `c-add-stmt-syntax' instead of backing up to boi
11322 ;; here, but we have to do like this for compatibility.
11323 (back-to-indentation)
11324 (c-add-syntax symbol (point))
11325 (if (and (c-keyword-member containing-decl-kwd
11326 'c-inexpr-class-kwds)
11327 (/= containing-decl-start (c-point 'boi containing-decl-start)))
11328 (c-add-syntax 'inexpr-class))
11329 (point)))
11331 (defun c-guess-continued-construct (indent-point
11332 char-after-ip
11333 beg-of-same-or-containing-stmt
11334 containing-sexp
11335 paren-state)
11336 ;; This function contains the decision tree reached through both
11337 ;; cases 18 and 10. It's a continued statement or top level
11338 ;; construct of some kind.
11340 ;; This function might do hidden buffer changes.
11342 (let (special-brace-list placeholder)
11343 (goto-char indent-point)
11344 (skip-chars-forward " \t")
11346 (cond
11347 ;; (CASE A removed.)
11348 ;; CASE B: open braces for class or brace-lists
11349 ((setq special-brace-list
11350 (or (and c-special-brace-lists
11351 (c-looking-at-special-brace-list))
11352 (eq char-after-ip ?{)))
11354 (cond
11355 ;; CASE B.1: class-open
11356 ((save-excursion
11357 (and (eq (char-after) ?{)
11358 (c-looking-at-decl-block containing-sexp t)
11359 (setq beg-of-same-or-containing-stmt (point))))
11360 (c-add-syntax 'class-open beg-of-same-or-containing-stmt))
11362 ;; CASE B.2: brace-list-open
11363 ((or (consp special-brace-list)
11364 (consp
11365 (c-looking-at-or-maybe-in-bracelist
11366 containing-sexp beg-of-same-or-containing-stmt))
11368 ;; The most semantically accurate symbol here is
11369 ;; brace-list-open, but we normally report it simply as a
11370 ;; statement-cont. The reason is that one normally adjusts
11371 ;; brace-list-open for brace lists as top-level constructs,
11372 ;; and brace lists inside statements is a completely different
11373 ;; context. C.f. case 5A.3.
11374 (c-beginning-of-statement-1 containing-sexp)
11375 (c-add-stmt-syntax (if c-auto-newline-analysis
11376 ;; Turn off the dwim above when we're
11377 ;; analyzing the nature of the brace
11378 ;; for the auto newline feature.
11379 'brace-list-open
11380 'statement-cont)
11381 nil nil
11382 containing-sexp paren-state))
11384 ;; CASE B.3: The body of a function declared inside a normal
11385 ;; block. Can occur e.g. in Pike and when using gcc
11386 ;; extensions, but watch out for macros followed by blocks.
11387 ;; C.f. cases E, 16F and 17G.
11388 ((and (not (c-at-statement-start-p))
11389 (eq (c-beginning-of-statement-1 containing-sexp nil nil t)
11390 'same)
11391 (save-excursion
11392 (let ((c-recognize-typeless-decls nil))
11393 ;; Turn off recognition of constructs that lacks a
11394 ;; type in this case, since that's more likely to be
11395 ;; a macro followed by a block.
11396 (c-forward-decl-or-cast-1 (c-point 'bosws) nil nil))))
11397 (c-add-stmt-syntax 'defun-open nil t
11398 containing-sexp paren-state))
11400 ;; CASE B.5: We have a C++11 "return \n { ..... }" Note that we're
11401 ;; not at the "{", currently.
11402 ((progn (goto-char indent-point)
11403 (backward-sexp)
11404 (looking-at c-return-key))
11405 (c-add-stmt-syntax 'statement-cont nil t
11406 containing-sexp paren-state))
11408 ;; CASE B.4: Continued statement with block open. The most
11409 ;; accurate analysis is perhaps `statement-cont' together with
11410 ;; `block-open' but we play DWIM and use `substatement-open'
11411 ;; instead. The rationale is that this typically is a macro
11412 ;; followed by a block which makes it very similar to a
11413 ;; statement with a substatement block.
11415 (c-add-stmt-syntax 'substatement-open nil nil
11416 containing-sexp paren-state))
11419 ;; CASE C: iostream insertion or extraction operator
11420 ((and (looking-at "\\(<<\\|>>\\)\\([^=]\\|$\\)")
11421 (save-excursion
11422 (goto-char beg-of-same-or-containing-stmt)
11423 ;; If there is no preceding streamop in the statement
11424 ;; then indent this line as a normal statement-cont.
11425 (when (c-syntactic-re-search-forward
11426 "\\(<<\\|>>\\)\\([^=]\\|$\\)" indent-point 'move t t)
11427 (c-add-syntax 'stream-op (c-point 'boi))
11428 t))))
11430 ;; CASE E: In the "K&R region" of a function declared inside a
11431 ;; normal block. C.f. case B.3.
11432 ((and (save-excursion
11433 ;; Check that the next token is a '{'. This works as
11434 ;; long as no language that allows nested function
11435 ;; definitions allows stuff like member init lists, K&R
11436 ;; declarations or throws clauses there.
11438 ;; Note that we do a forward search for something ahead
11439 ;; of the indentation line here. That's not good since
11440 ;; the user might not have typed it yet. Unfortunately
11441 ;; it's exceedingly tricky to recognize a function
11442 ;; prototype in a code block without resorting to this.
11443 (c-forward-syntactic-ws)
11444 (eq (char-after) ?{))
11445 (not (c-at-statement-start-p))
11446 (eq (c-beginning-of-statement-1 containing-sexp nil nil t)
11447 'same)
11448 (save-excursion
11449 (let ((c-recognize-typeless-decls nil))
11450 ;; Turn off recognition of constructs that lacks a
11451 ;; type in this case, since that's more likely to be
11452 ;; a macro followed by a block.
11453 (c-forward-decl-or-cast-1 (c-point 'bosws) nil nil))))
11454 (c-add-stmt-syntax 'func-decl-cont nil t
11455 containing-sexp paren-state))
11457 ;;CASE F: continued statement and the only preceding items are
11458 ;;annotations.
11459 ((and (c-major-mode-is 'java-mode)
11460 (setq placeholder (point))
11461 (c-beginning-of-statement-1)
11462 (progn
11463 (while (and (c-forward-annotation)
11464 (< (point) placeholder))
11465 (c-forward-syntactic-ws))
11467 (prog1
11468 (>= (point) placeholder)
11469 (goto-char placeholder)))
11470 (c-beginning-of-statement-1 containing-sexp)
11471 (c-add-syntax 'annotation-var-cont (point)))
11473 ;; CASE G: a template list continuation?
11474 ;; Mostly a duplication of case 5D.3 to fix templates-19:
11475 ((and (c-major-mode-is 'c++-mode)
11476 (save-excursion
11477 (goto-char indent-point)
11478 (c-with-syntax-table c++-template-syntax-table
11479 (setq placeholder (c-up-list-backward)))
11480 (and placeholder
11481 (eq (char-after placeholder) ?<)
11482 (/= (char-before placeholder) ?<)
11483 (progn
11484 (goto-char (1+ placeholder))
11485 (not (looking-at c-<-op-cont-regexp))))))
11486 (c-with-syntax-table c++-template-syntax-table
11487 (goto-char placeholder)
11488 (c-beginning-of-statement-1 containing-sexp t))
11489 (if (save-excursion
11490 (c-backward-syntactic-ws containing-sexp)
11491 (eq (char-before) ?<))
11492 ;; In a nested template arglist.
11493 (progn
11494 (goto-char placeholder)
11495 (c-syntactic-skip-backward "^,;" containing-sexp t)
11496 (c-forward-syntactic-ws))
11497 (back-to-indentation))
11498 ;; FIXME: Should use c-add-stmt-syntax, but it's not yet
11499 ;; template aware.
11500 (c-add-syntax 'template-args-cont (point) placeholder))
11502 ;; CASE D: continued statement.
11504 (c-beginning-of-statement-1 containing-sexp)
11505 (c-add-stmt-syntax 'statement-cont nil nil
11506 containing-sexp paren-state))
11509 ;; The next autoload was added by RMS on 2005/8/9 - don't know why (ACM,
11510 ;; 2005/11/29).
11511 ;;;###autoload
11512 (defun c-guess-basic-syntax ()
11513 "Return the syntactic context of the current line."
11514 (save-excursion
11515 (beginning-of-line)
11516 (c-save-buffer-state
11517 ((indent-point (point))
11518 (case-fold-search nil)
11519 ;; A whole ugly bunch of various temporary variables. Have
11520 ;; to declare them here since it's not possible to declare
11521 ;; a variable with only the scope of a cond test and the
11522 ;; following result clauses, and most of this function is a
11523 ;; single gigantic cond. :P
11524 literal char-before-ip before-ws-ip char-after-ip macro-start
11525 in-macro-expr c-syntactic-context placeholder
11526 step-type tmpsymbol keyword injava-inher special-brace-list tmp-pos
11527 containing-<
11528 ;; The following record some positions for the containing
11529 ;; declaration block if we're directly within one:
11530 ;; `containing-decl-open' is the position of the open
11531 ;; brace. `containing-decl-start' is the start of the
11532 ;; declaration. `containing-decl-kwd' is the keyword
11533 ;; symbol of the keyword that tells what kind of block it
11534 ;; is.
11535 containing-decl-open
11536 containing-decl-start
11537 containing-decl-kwd
11538 ;; The open paren of the closest surrounding sexp or nil if
11539 ;; there is none.
11540 containing-sexp
11541 ;; The position after the closest preceding brace sexp
11542 ;; (nested sexps are ignored), or the position after
11543 ;; `containing-sexp' if there is none, or (point-min) if
11544 ;; `containing-sexp' is nil.
11546 ;; The paren state outside `containing-sexp', or at
11547 ;; `indent-point' if `containing-sexp' is nil.
11548 (paren-state (c-parse-state))
11549 (state-cache (copy-tree paren-state))
11550 ;; There's always at most one syntactic element which got
11551 ;; an anchor pos. It's stored in syntactic-relpos.
11552 syntactic-relpos
11553 (c-stmt-delim-chars c-stmt-delim-chars))
11555 ;; Check if we're directly inside an enclosing declaration
11556 ;; level block.
11557 (when (and (setq containing-sexp
11558 (c-most-enclosing-brace paren-state))
11559 (progn
11560 (goto-char containing-sexp)
11561 (eq (char-after) ?{))
11562 (setq placeholder
11563 (c-looking-at-decl-block
11564 (c-most-enclosing-brace paren-state
11565 containing-sexp)
11566 t)))
11567 (setq containing-decl-open containing-sexp
11568 containing-decl-start (point)
11569 containing-sexp nil)
11570 (goto-char placeholder)
11571 (setq containing-decl-kwd (and (looking-at c-keywords-regexp)
11572 (c-keyword-sym (match-string 1)))))
11574 ;; Init some position variables.
11575 (if paren-state
11576 (progn
11577 (setq containing-sexp (car paren-state)
11578 paren-state (cdr paren-state))
11579 (if (consp containing-sexp)
11580 (save-excursion
11581 (goto-char (cdr containing-sexp))
11582 (if (and (c-major-mode-is 'c++-mode)
11583 (c-back-over-member-initializer-braces))
11584 (c-syntactic-skip-backward "^}" nil t))
11585 (setq lim (point))
11586 (if paren-state
11587 ;; Ignore balanced paren. The next entry
11588 ;; can't be another one.
11589 (setq containing-sexp (car paren-state)
11590 paren-state (cdr paren-state))
11591 ;; If there is no surrounding open paren then
11592 ;; put the last balanced pair back on paren-state.
11593 (setq paren-state (cons containing-sexp paren-state)
11594 containing-sexp nil)))
11595 (setq lim (1+ containing-sexp))))
11596 (setq lim (point-min)))
11598 ;; If we're in a parenthesis list then ',' delimits the
11599 ;; "statements" rather than being an operator (with the
11600 ;; exception of the "for" clause). This difference is
11601 ;; typically only noticeable when statements are used in macro
11602 ;; arglists.
11603 (when (and containing-sexp
11604 (eq (char-after containing-sexp) ?\())
11605 (setq c-stmt-delim-chars c-stmt-delim-chars-with-comma))
11606 ;; cache char before and after indent point, and move point to
11607 ;; the most likely position to perform the majority of tests
11608 (goto-char indent-point)
11609 (c-backward-syntactic-ws lim)
11610 (setq before-ws-ip (point)
11611 char-before-ip (char-before))
11612 (goto-char indent-point)
11613 (skip-chars-forward " \t")
11614 (setq char-after-ip (char-after))
11616 ;; are we in a literal?
11617 (setq literal (c-in-literal lim))
11619 ;; now figure out syntactic qualities of the current line
11620 (cond
11622 ;; CASE 1: in a string.
11623 ((eq literal 'string)
11624 (c-add-syntax 'string (c-point 'bopl)))
11626 ;; CASE 2: in a C or C++ style comment.
11627 ((and (memq literal '(c c++))
11628 ;; This is a kludge for XEmacs where we use
11629 ;; `buffer-syntactic-context', which doesn't correctly
11630 ;; recognize "\*/" to end a block comment.
11631 ;; `parse-partial-sexp' which is used by
11632 ;; `c-literal-limits' will however do that in most
11633 ;; versions, which results in that we get nil from
11634 ;; `c-literal-limits' even when `c-in-literal' claims
11635 ;; we're inside a comment.
11636 (setq placeholder (c-literal-start lim)))
11637 (c-add-syntax literal placeholder))
11639 ;; CASE 3: in a cpp preprocessor macro continuation.
11640 ((and (save-excursion
11641 (when (c-beginning-of-macro)
11642 (setq macro-start (point))))
11643 (/= macro-start (c-point 'boi))
11644 (progn
11645 (setq tmpsymbol 'cpp-macro-cont)
11646 (or (not c-syntactic-indentation-in-macros)
11647 (save-excursion
11648 (goto-char macro-start)
11649 ;; If at the beginning of the body of a #define
11650 ;; directive then analyze as cpp-define-intro
11651 ;; only. Go on with the syntactic analysis
11652 ;; otherwise. in-macro-expr is set if we're in a
11653 ;; cpp expression, i.e. before the #define body
11654 ;; or anywhere in a non-#define directive.
11655 (if (c-forward-to-cpp-define-body)
11656 (let ((indent-boi (c-point 'boi indent-point)))
11657 (setq in-macro-expr (> (point) indent-boi)
11658 tmpsymbol 'cpp-define-intro)
11659 (= (point) indent-boi))
11660 (setq in-macro-expr t)
11661 nil)))))
11662 (c-add-syntax tmpsymbol macro-start)
11663 (setq macro-start nil))
11665 ;; CASE 11: an else clause?
11666 ((looking-at "else\\>[^_]")
11667 (c-beginning-of-statement-1 containing-sexp)
11668 (c-add-stmt-syntax 'else-clause nil t
11669 containing-sexp paren-state))
11671 ;; CASE 12: while closure of a do/while construct?
11672 ((and (looking-at "while\\>[^_]")
11673 (save-excursion
11674 (prog1 (eq (c-beginning-of-statement-1 containing-sexp)
11675 'beginning)
11676 (setq placeholder (point)))))
11677 (goto-char placeholder)
11678 (c-add-stmt-syntax 'do-while-closure nil t
11679 containing-sexp paren-state))
11681 ;; CASE 13: A catch or finally clause? This case is simpler
11682 ;; than if-else and do-while, because a block is required
11683 ;; after every try, catch and finally.
11684 ((save-excursion
11685 (and (cond ((c-major-mode-is 'c++-mode)
11686 (looking-at "catch\\>[^_]"))
11687 ((c-major-mode-is 'java-mode)
11688 (looking-at "\\(catch\\|finally\\)\\>[^_]")))
11689 (and (c-safe (c-backward-syntactic-ws)
11690 (c-backward-sexp)
11692 (eq (char-after) ?{)
11693 (c-safe (c-backward-syntactic-ws)
11694 (c-backward-sexp)
11696 (if (eq (char-after) ?\()
11697 (c-safe (c-backward-sexp) t)
11699 (looking-at "\\(try\\|catch\\)\\>[^_]")
11700 (setq placeholder (point))))
11701 (goto-char placeholder)
11702 (c-add-stmt-syntax 'catch-clause nil t
11703 containing-sexp paren-state))
11705 ;; CASE 18: A substatement we can recognize by keyword.
11706 ((save-excursion
11707 (and c-opt-block-stmt-key
11708 (not (eq char-before-ip ?\;))
11709 (not (c-at-vsemi-p before-ws-ip))
11710 (not (memq char-after-ip '(?\) ?\] ?,)))
11711 (or (not (eq char-before-ip ?}))
11712 (c-looking-at-inexpr-block-backward state-cache))
11713 (> (point)
11714 (progn
11715 ;; Ought to cache the result from the
11716 ;; c-beginning-of-statement-1 calls here.
11717 (setq placeholder (point))
11718 (while (eq (setq step-type
11719 (c-beginning-of-statement-1 lim))
11720 'label))
11721 (if (eq step-type 'previous)
11722 (goto-char placeholder)
11723 (setq placeholder (point))
11724 (if (and (eq step-type 'same)
11725 (not (looking-at c-opt-block-stmt-key)))
11726 ;; Step up to the containing statement if we
11727 ;; stayed in the same one.
11728 (let (step)
11729 (while (eq
11730 (setq step
11731 (c-beginning-of-statement-1 lim))
11732 'label))
11733 (if (eq step 'up)
11734 (setq placeholder (point))
11735 ;; There was no containing statement after all.
11736 (goto-char placeholder)))))
11737 placeholder))
11738 (if (looking-at c-block-stmt-2-key)
11739 ;; Require a parenthesis after these keywords.
11740 ;; Necessary to catch e.g. synchronized in Java,
11741 ;; which can be used both as statement and
11742 ;; modifier.
11743 (and (zerop (c-forward-token-2 1 nil))
11744 (eq (char-after) ?\())
11745 (looking-at c-opt-block-stmt-key))))
11747 (if (eq step-type 'up)
11748 ;; CASE 18A: Simple substatement.
11749 (progn
11750 (goto-char placeholder)
11751 (cond
11752 ((eq char-after-ip ?{)
11753 (c-add-stmt-syntax 'substatement-open nil nil
11754 containing-sexp paren-state))
11755 ((save-excursion
11756 (goto-char indent-point)
11757 (back-to-indentation)
11758 (c-forward-label))
11759 (c-add-stmt-syntax 'substatement-label nil nil
11760 containing-sexp paren-state))
11762 (c-add-stmt-syntax 'substatement nil nil
11763 containing-sexp paren-state))))
11765 ;; CASE 18B: Some other substatement. This is shared
11766 ;; with case 10.
11767 (c-guess-continued-construct indent-point
11768 char-after-ip
11769 placeholder
11771 paren-state)))
11773 ;; CASE 14: A case or default label
11774 ((save-excursion
11775 (and (looking-at c-label-kwds-regexp)
11776 (or (c-major-mode-is 'idl-mode)
11777 (and
11778 containing-sexp
11779 (goto-char containing-sexp)
11780 (eq (char-after) ?{)
11781 (progn (c-backward-syntactic-ws) t)
11782 (eq (char-before) ?\))
11783 (c-go-list-backward)
11784 (progn (c-backward-syntactic-ws) t)
11785 (c-simple-skip-symbol-backward)
11786 (looking-at c-block-stmt-2-key)))))
11787 (if containing-sexp
11788 (progn
11789 (goto-char containing-sexp)
11790 (setq lim (c-most-enclosing-brace state-cache
11791 containing-sexp))
11792 (c-backward-to-block-anchor lim)
11793 (c-add-stmt-syntax 'case-label nil t lim paren-state))
11794 ;; Got a bogus label at the top level. In lack of better
11795 ;; alternatives, anchor it on (point-min).
11796 (c-add-syntax 'case-label (point-min))))
11798 ;; CASE 15: any other label
11799 ((save-excursion
11800 (back-to-indentation)
11801 (and (not (looking-at c-syntactic-ws-start))
11802 (not (looking-at c-label-kwds-regexp))
11803 (c-forward-label)))
11804 (cond (containing-decl-open
11805 (setq placeholder (c-add-class-syntax 'inclass
11806 containing-decl-open
11807 containing-decl-start
11808 containing-decl-kwd
11809 paren-state))
11810 ;; Append access-label with the same anchor point as
11811 ;; inclass gets.
11812 (c-append-syntax 'access-label placeholder))
11814 (containing-sexp
11815 (goto-char containing-sexp)
11816 (setq lim (c-most-enclosing-brace state-cache
11817 containing-sexp))
11818 (save-excursion
11819 (setq tmpsymbol
11820 (if (and (eq (c-beginning-of-statement-1 lim) 'up)
11821 (looking-at "switch\\>[^_]"))
11822 ;; If the surrounding statement is a switch then
11823 ;; let's analyze all labels as switch labels, so
11824 ;; that they get lined up consistently.
11825 'case-label
11826 'label)))
11827 (c-backward-to-block-anchor lim)
11828 (c-add-stmt-syntax tmpsymbol nil t lim paren-state))
11831 ;; A label on the top level. Treat it as a class
11832 ;; context. (point-min) is the closest we get to the
11833 ;; class open brace.
11834 (c-add-syntax 'access-label (point-min)))))
11836 ;; CASE 4: In-expression statement. C.f. cases 7B, 16A and
11837 ;; 17E.
11838 ((setq placeholder (c-looking-at-inexpr-block
11839 (c-safe-position containing-sexp paren-state)
11840 containing-sexp
11841 ;; Have to turn on the heuristics after
11842 ;; the point even though it doesn't work
11843 ;; very well. C.f. test case class-16.pike.
11845 (setq tmpsymbol (assq (car placeholder)
11846 '((inexpr-class . class-open)
11847 (inexpr-statement . block-open))))
11848 (if tmpsymbol
11849 ;; It's a statement block or an anonymous class.
11850 (setq tmpsymbol (cdr tmpsymbol))
11851 ;; It's a Pike lambda. Check whether we are between the
11852 ;; lambda keyword and the argument list or at the defun
11853 ;; opener.
11854 (setq tmpsymbol (if (eq char-after-ip ?{)
11855 'inline-open
11856 'lambda-intro-cont)))
11857 (goto-char (cdr placeholder))
11858 (back-to-indentation)
11859 (c-add-stmt-syntax tmpsymbol nil t
11860 (c-most-enclosing-brace state-cache (point))
11861 paren-state)
11862 (unless (eq (point) (cdr placeholder))
11863 (c-add-syntax (car placeholder))))
11865 ;; CASE 5: Line is inside a declaration level block or at top level.
11866 ((or containing-decl-open (null containing-sexp))
11867 (cond
11869 ;; CASE 5A: we are looking at a defun, brace list, class,
11870 ;; or inline-inclass method opening brace
11871 ((setq special-brace-list
11872 (or (and c-special-brace-lists
11873 (c-looking-at-special-brace-list))
11874 (eq char-after-ip ?{)))
11875 (cond
11877 ;; CASE 5A.1: Non-class declaration block open.
11878 ((save-excursion
11879 (let (tmp)
11880 (and (eq char-after-ip ?{)
11881 (setq tmp (c-looking-at-decl-block containing-sexp t))
11882 (progn
11883 (setq placeholder (point))
11884 (goto-char tmp)
11885 (looking-at c-symbol-key))
11886 (c-keyword-member
11887 (c-keyword-sym (setq keyword (match-string 0)))
11888 'c-other-block-decl-kwds))))
11889 (goto-char placeholder)
11890 (c-add-stmt-syntax
11891 (if (string-equal keyword "extern")
11892 ;; Special case for extern-lang-open.
11893 'extern-lang-open
11894 (intern (concat keyword "-open")))
11895 nil t containing-sexp paren-state))
11897 ;; CASE 5A.2: we are looking at a class opening brace
11898 ((save-excursion
11899 (goto-char indent-point)
11900 (skip-chars-forward " \t")
11901 (and (eq (char-after) ?{)
11902 (c-looking-at-decl-block containing-sexp t)
11903 (setq placeholder (point))))
11904 (c-add-syntax 'class-open placeholder))
11906 ;; CASE 5A.3: brace list open
11907 ((save-excursion
11908 (goto-char indent-point)
11909 (skip-chars-forward " \t")
11910 (cond
11911 ((c-backward-over-enum-header)
11912 (setq placeholder (c-point 'boi)))
11913 ((consp (setq placeholder
11914 (c-looking-at-or-maybe-in-bracelist
11915 containing-sexp lim)))
11916 (setq tmpsymbol (and (cdr placeholder) 'topmost-intro-cont))
11917 (setq placeholder (c-point 'boi (car placeholder))))))
11918 (if (and (not c-auto-newline-analysis)
11919 ;(c-major-mode-is 'java-mode) ; Not needed anymore (2016-08-30).
11920 (eq tmpsymbol 'topmost-intro-cont))
11921 ;; We're in Java and have found that the open brace
11922 ;; belongs to a "new Foo[]" initialization list,
11923 ;; which means the brace list is part of an
11924 ;; expression and not a top level definition. We
11925 ;; therefore treat it as any topmost continuation
11926 ;; even though the semantically correct symbol still
11927 ;; is brace-list-open, on the same grounds as in
11928 ;; case B.2.
11929 (progn
11930 (c-beginning-of-statement-1 lim)
11931 (c-add-syntax 'topmost-intro-cont (c-point 'boi)))
11932 (c-add-syntax 'brace-list-open placeholder)))
11934 ;; CASE 5A.4: inline defun open
11935 ((and containing-decl-open
11936 (not (c-keyword-member containing-decl-kwd
11937 'c-other-block-decl-kwds)))
11938 (c-add-syntax 'inline-open)
11939 (c-add-class-syntax 'inclass
11940 containing-decl-open
11941 containing-decl-start
11942 containing-decl-kwd
11943 paren-state))
11945 ;; CASE 5A.5: ordinary defun open
11947 (save-excursion
11948 (c-beginning-of-decl-1 lim)
11949 (while (cond
11950 ((looking-at c-specifier-key)
11951 (c-forward-keyword-clause 1))
11952 ((and c-opt-cpp-prefix
11953 (looking-at c-noise-macro-with-parens-name-re))
11954 (c-forward-noise-clause))))
11955 (c-add-syntax 'defun-open (c-point 'boi))
11956 ;; Bogus to use bol here, but it's the legacy. (Resolved,
11957 ;; 2007-11-09)
11958 ))))
11960 ;; CASE 5R: Member init list. (Used to be part of CASE 5B.1)
11961 ;; Note there is no limit on the backward search here, since member
11962 ;; init lists can, in practice, be very large.
11963 ((save-excursion
11964 (when (and (c-major-mode-is 'c++-mode)
11965 (setq placeholder (c-back-over-member-initializers)))
11966 (setq tmp-pos (point))))
11967 (if (= (c-point 'bosws) (1+ tmp-pos))
11968 (progn
11969 ;; There is no preceding member init clause.
11970 ;; Indent relative to the beginning of indentation
11971 ;; for the topmost-intro line that contains the
11972 ;; prototype's open paren.
11973 (goto-char placeholder)
11974 (c-add-syntax 'member-init-intro (c-point 'boi)))
11975 ;; Indent relative to the first member init clause.
11976 (goto-char (1+ tmp-pos))
11977 (c-forward-syntactic-ws)
11978 (c-add-syntax 'member-init-cont (point))))
11980 ;; CASE 5B: After a function header but before the body (or
11981 ;; the ending semicolon if there's no body).
11982 ((save-excursion
11983 (when (setq placeholder (c-just-after-func-arglist-p
11984 (max lim (c-determine-limit 500))))
11985 (setq tmp-pos (point))))
11986 (cond
11988 ;; CASE 5B.1: Member init list.
11989 ((eq (char-after tmp-pos) ?:)
11990 ;; There is no preceding member init clause.
11991 ;; Indent relative to the beginning of indentation
11992 ;; for the topmost-intro line that contains the
11993 ;; prototype's open paren.
11994 (goto-char placeholder)
11995 (c-add-syntax 'member-init-intro (c-point 'boi)))
11997 ;; CASE 5B.2: K&R arg decl intro
11998 ((and c-recognize-knr-p
11999 (c-in-knr-argdecl lim))
12000 (c-beginning-of-statement-1 lim)
12001 (c-add-syntax 'knr-argdecl-intro (c-point 'boi))
12002 (if containing-decl-open
12003 (c-add-class-syntax 'inclass
12004 containing-decl-open
12005 containing-decl-start
12006 containing-decl-kwd
12007 paren-state)))
12009 ;; CASE 5B.4: Nether region after a C++ or Java func
12010 ;; decl, which could include a `throws' declaration.
12012 (c-beginning-of-statement-1 lim)
12013 (c-add-syntax 'func-decl-cont (c-point 'boi))
12016 ;; CASE 5C: inheritance line. could be first inheritance
12017 ;; line, or continuation of a multiple inheritance
12018 ((or (and (c-major-mode-is 'c++-mode)
12019 (progn
12020 (when (eq char-after-ip ?,)
12021 (skip-chars-forward " \t")
12022 (forward-char))
12023 (looking-at c-opt-postfix-decl-spec-key)))
12024 (and (or (eq char-before-ip ?:)
12025 ;; watch out for scope operator
12026 (save-excursion
12027 (and (eq char-after-ip ?:)
12028 (c-safe (forward-char 1) t)
12029 (not (eq (char-after) ?:))
12031 (save-excursion
12032 (c-beginning-of-statement-1 lim)
12033 (when (looking-at c-opt-<>-sexp-key)
12034 (goto-char (match-end 1))
12035 (c-forward-syntactic-ws)
12036 (c-forward-<>-arglist nil)
12037 (c-forward-syntactic-ws))
12038 (looking-at c-class-key)))
12039 ;; for Java
12040 (and (c-major-mode-is 'java-mode)
12041 (let ((fence (save-excursion
12042 (c-beginning-of-statement-1 lim)
12043 (point)))
12044 cont done)
12045 (save-excursion
12046 (while (not done)
12047 (cond ((looking-at c-opt-postfix-decl-spec-key)
12048 (setq injava-inher (cons cont (point))
12049 done t))
12050 ((or (not (c-safe (c-forward-sexp -1) t))
12051 (<= (point) fence))
12052 (setq done t))
12054 (setq cont t)))
12055 injava-inher)
12056 (not (c-crosses-statement-barrier-p (cdr injava-inher)
12057 (point)))
12059 (cond
12061 ;; CASE 5C.1: non-hanging colon on an inher intro
12062 ((eq char-after-ip ?:)
12063 (c-beginning-of-statement-1 lim)
12064 (c-add-syntax 'inher-intro (c-point 'boi))
12065 ;; don't add inclass symbol since relative point already
12066 ;; contains any class offset
12069 ;; CASE 5C.2: hanging colon on an inher intro
12070 ((eq char-before-ip ?:)
12071 (c-beginning-of-statement-1 lim)
12072 (c-add-syntax 'inher-intro (c-point 'boi))
12073 (if containing-decl-open
12074 (c-add-class-syntax 'inclass
12075 containing-decl-open
12076 containing-decl-start
12077 containing-decl-kwd
12078 paren-state)))
12080 ;; CASE 5C.3: in a Java implements/extends
12081 (injava-inher
12082 (let ((where (cdr injava-inher))
12083 (cont (car injava-inher)))
12084 (goto-char where)
12085 (cond ((looking-at "throws\\>[^_]")
12086 (c-add-syntax 'func-decl-cont
12087 (progn (c-beginning-of-statement-1 lim)
12088 (c-point 'boi))))
12089 (cont (c-add-syntax 'inher-cont where))
12090 (t (c-add-syntax 'inher-intro
12091 (progn (goto-char (cdr injava-inher))
12092 (c-beginning-of-statement-1 lim)
12093 (point))))
12096 ;; CASE 5C.4: a continued inheritance line
12098 (c-beginning-of-inheritance-list lim)
12099 (c-add-syntax 'inher-cont (point))
12100 ;; don't add inclass symbol since relative point already
12101 ;; contains any class offset
12104 ;; CASE 5P: AWK pattern or function or continuation
12105 ;; thereof.
12106 ((c-major-mode-is 'awk-mode)
12107 (setq placeholder (point))
12108 (c-add-stmt-syntax
12109 (if (and (eq (c-beginning-of-statement-1) 'same)
12110 (/= (point) placeholder))
12111 'topmost-intro-cont
12112 'topmost-intro)
12113 nil nil
12114 containing-sexp paren-state))
12116 ;; CASE 5D: this could be a top-level initialization, a
12117 ;; member init list continuation, or a template argument
12118 ;; list continuation.
12119 ((save-excursion
12120 ;; Note: We use the fact that lim is always after any
12121 ;; preceding brace sexp.
12122 (if c-recognize-<>-arglists
12123 (while (and
12124 (progn
12125 (c-syntactic-skip-backward "^;,=<>" lim t)
12126 (> (point) lim))
12128 (when c-overloadable-operators-regexp
12129 (when (setq placeholder (c-after-special-operator-id lim))
12130 (goto-char placeholder)
12132 (cond
12133 ((eq (char-before) ?>)
12134 (or (c-backward-<>-arglist nil lim)
12135 (backward-char))
12137 ((eq (char-before) ?<)
12138 (backward-char)
12139 (if (save-excursion
12140 (c-forward-<>-arglist nil))
12141 (progn (forward-char)
12142 nil)
12144 (t nil)))))
12145 ;; NB: No c-after-special-operator-id stuff in this
12146 ;; clause - we assume only C++ needs it.
12147 (c-syntactic-skip-backward "^;,=" lim t))
12148 (memq (char-before) '(?, ?= ?<)))
12149 (cond
12151 ;; CASE 5D.3: perhaps a template list continuation?
12152 ((and (c-major-mode-is 'c++-mode)
12153 (save-excursion
12154 (save-restriction
12155 (c-with-syntax-table c++-template-syntax-table
12156 (goto-char indent-point)
12157 (setq placeholder (c-up-list-backward))
12158 (and placeholder
12159 (eq (char-after placeholder) ?<))))))
12160 (c-with-syntax-table c++-template-syntax-table
12161 (goto-char placeholder)
12162 (c-beginning-of-statement-1 lim t))
12163 (if (save-excursion
12164 (c-backward-syntactic-ws lim)
12165 (eq (char-before) ?<))
12166 ;; In a nested template arglist.
12167 (progn
12168 (goto-char placeholder)
12169 (c-syntactic-skip-backward "^,;" lim t)
12170 (c-forward-syntactic-ws))
12171 (back-to-indentation))
12172 ;; FIXME: Should use c-add-stmt-syntax, but it's not yet
12173 ;; template aware.
12174 (c-add-syntax 'template-args-cont (point) placeholder))
12176 ;; CASE 5D.4: perhaps a multiple inheritance line?
12177 ((and (c-major-mode-is 'c++-mode)
12178 (save-excursion
12179 (c-beginning-of-statement-1 lim)
12180 (setq placeholder (point))
12181 (if (looking-at "static\\>[^_]")
12182 (c-forward-token-2 1 nil indent-point))
12183 (and (looking-at c-class-key)
12184 (zerop (c-forward-token-2 2 nil indent-point))
12185 (if (eq (char-after) ?<)
12186 (c-with-syntax-table c++-template-syntax-table
12187 (zerop (c-forward-token-2 1 t indent-point)))
12189 (eq (char-after) ?:))))
12190 (goto-char placeholder)
12191 (c-add-syntax 'inher-cont (c-point 'boi)))
12193 ;; CASE 5D.5: Continuation of the "expression part" of a
12194 ;; top level construct. Or, perhaps, an unrecognized construct.
12196 (while (and (setq placeholder (point))
12197 (eq (car (c-beginning-of-decl-1 containing-sexp)) ; Can't use `lim' here.
12198 'same)
12199 (save-excursion
12200 (c-backward-syntactic-ws)
12201 (eq (char-before) ?}))
12202 (< (point) placeholder)))
12203 (c-add-stmt-syntax
12204 (cond
12205 ((eq (point) placeholder) 'statement) ; unrecognized construct
12206 ;; A preceding comma at the top level means that a
12207 ;; new variable declaration starts here. Use
12208 ;; topmost-intro-cont for it, for consistency with
12209 ;; the first variable declaration. C.f. case 5N.
12210 ((eq char-before-ip ?,) 'topmost-intro-cont)
12211 (t 'statement-cont))
12212 nil nil containing-sexp paren-state))
12215 ;; CASE 5F: Close of a non-class declaration level block.
12216 ((and (eq char-after-ip ?})
12217 (c-keyword-member containing-decl-kwd
12218 'c-other-block-decl-kwds))
12219 ;; This is inconsistent: Should use `containing-decl-open'
12220 ;; here if it's at boi, like in case 5J.
12221 (goto-char containing-decl-start)
12222 (c-add-stmt-syntax
12223 (if (string-equal (symbol-name containing-decl-kwd) "extern")
12224 ;; Special case for compatibility with the
12225 ;; extern-lang syntactic symbols.
12226 'extern-lang-close
12227 (intern (concat (symbol-name containing-decl-kwd)
12228 "-close")))
12229 nil t
12230 (c-most-enclosing-brace paren-state (point))
12231 paren-state))
12233 ;; CASE 5G: we are looking at the brace which closes the
12234 ;; enclosing nested class decl
12235 ((and containing-sexp
12236 (eq char-after-ip ?})
12237 (eq containing-decl-open containing-sexp))
12238 (c-add-class-syntax 'class-close
12239 containing-decl-open
12240 containing-decl-start
12241 containing-decl-kwd
12242 paren-state))
12244 ;; CASE 5H: we could be looking at subsequent knr-argdecls
12245 ((and c-recognize-knr-p
12246 (not containing-sexp) ; can't be knr inside braces.
12247 (not (eq char-before-ip ?}))
12248 (save-excursion
12249 (setq placeholder (cdr (c-beginning-of-decl-1 lim)))
12250 (and placeholder
12251 ;; Do an extra check to avoid tripping up on
12252 ;; statements that occur in invalid contexts
12253 ;; (e.g. in macro bodies where we don't really
12254 ;; know the context of what we're looking at).
12255 (not (and c-opt-block-stmt-key
12256 (looking-at c-opt-block-stmt-key)))))
12257 (< placeholder indent-point))
12258 (goto-char placeholder)
12259 (c-add-syntax 'knr-argdecl (point)))
12261 ;; CASE 5I: ObjC method definition.
12262 ((and c-opt-method-key
12263 (looking-at c-opt-method-key))
12264 (c-beginning-of-statement-1 nil t)
12265 (if (= (point) indent-point)
12266 ;; Handle the case when it's the first (non-comment)
12267 ;; thing in the buffer. Can't look for a 'same return
12268 ;; value from cbos1 since ObjC directives currently
12269 ;; aren't recognized fully, so that we get 'same
12270 ;; instead of 'previous if it moved over a preceding
12271 ;; directive.
12272 (goto-char (point-min)))
12273 (c-add-syntax 'objc-method-intro (c-point 'boi)))
12275 ;; CASE 5N: At a variable declaration that follows a class
12276 ;; definition or some other block declaration that doesn't
12277 ;; end at the closing '}'. C.f. case 5D.5.
12278 ((progn
12279 (c-backward-syntactic-ws lim)
12280 (and (eq (char-before) ?})
12281 (save-excursion
12282 (let ((start (point)))
12283 (if (and state-cache
12284 (consp (car state-cache))
12285 (eq (cdar state-cache) (point)))
12286 ;; Speed up the backward search a bit.
12287 (goto-char (caar state-cache)))
12288 (c-beginning-of-decl-1 containing-sexp) ; Can't use `lim' here.
12289 (setq placeholder (point))
12290 (if (= start (point))
12291 ;; The '}' is unbalanced.
12293 (c-end-of-decl-1)
12294 (>= (point) indent-point))))))
12295 (goto-char placeholder)
12296 (c-add-stmt-syntax 'topmost-intro-cont nil nil
12297 containing-sexp paren-state))
12299 ;; NOTE: The point is at the end of the previous token here.
12301 ;; CASE 5J: we are at the topmost level, make
12302 ;; sure we skip back past any access specifiers
12303 ((and
12304 ;; A macro continuation line is never at top level.
12305 (not (and macro-start
12306 (> indent-point macro-start)))
12307 (save-excursion
12308 (setq placeholder (point))
12309 (or (memq char-before-ip '(?\; ?{ ?} nil))
12310 (c-at-vsemi-p before-ws-ip)
12311 (when (and (eq char-before-ip ?:)
12312 (eq (c-beginning-of-statement-1 lim)
12313 'label))
12314 (c-backward-syntactic-ws lim)
12315 (setq placeholder (point)))
12316 (and (c-major-mode-is 'objc-mode)
12317 (catch 'not-in-directive
12318 (c-beginning-of-statement-1 lim)
12319 (setq placeholder (point))
12320 (while (and (c-forward-objc-directive)
12321 (< (point) indent-point))
12322 (c-forward-syntactic-ws)
12323 (if (>= (point) indent-point)
12324 (throw 'not-in-directive t))
12325 (setq placeholder (point)))
12326 nil)))))
12327 ;; For historic reasons we anchor at bol of the last
12328 ;; line of the previous declaration. That's clearly
12329 ;; highly bogus and useless, and it makes our lives hard
12330 ;; to remain compatible. :P
12331 (goto-char placeholder)
12332 (c-add-syntax 'topmost-intro (c-point 'bol))
12333 (if containing-decl-open
12334 (if (c-keyword-member containing-decl-kwd
12335 'c-other-block-decl-kwds)
12336 (progn
12337 (goto-char (c-brace-anchor-point containing-decl-open))
12338 (c-add-stmt-syntax
12339 (if (string-equal (symbol-name containing-decl-kwd)
12340 "extern")
12341 ;; Special case for compatibility with the
12342 ;; extern-lang syntactic symbols.
12343 'inextern-lang
12344 (intern (concat "in"
12345 (symbol-name containing-decl-kwd))))
12346 nil t
12347 (c-most-enclosing-brace paren-state (point))
12348 paren-state))
12349 (c-add-class-syntax 'inclass
12350 containing-decl-open
12351 containing-decl-start
12352 containing-decl-kwd
12353 paren-state)))
12354 (when (and c-syntactic-indentation-in-macros
12355 macro-start
12356 (/= macro-start (c-point 'boi indent-point)))
12357 (c-add-syntax 'cpp-define-intro)
12358 (setq macro-start nil)))
12360 ;; CASE 5K: we are at an ObjC method definition
12361 ;; continuation line.
12362 ((and c-opt-method-key
12363 (save-excursion
12364 (c-beginning-of-statement-1 lim)
12365 (beginning-of-line)
12366 (when (looking-at c-opt-method-key)
12367 (setq placeholder (point)))))
12368 (c-add-syntax 'objc-method-args-cont placeholder))
12370 ;; CASE 5L: we are at the first argument of a template
12371 ;; arglist that begins on the previous line.
12372 ((and c-recognize-<>-arglists
12373 (eq (char-before) ?<)
12374 (not (and c-overloadable-operators-regexp
12375 (c-after-special-operator-id lim))))
12376 (c-beginning-of-statement-1 (c-safe-position (point) paren-state))
12377 (c-add-syntax 'template-args-cont (c-point 'boi)))
12379 ;; CASE 5Q: we are at a statement within a macro.
12380 (macro-start
12381 (c-beginning-of-statement-1 containing-sexp)
12382 (c-add-stmt-syntax 'statement nil t containing-sexp paren-state))
12384 ;;CASE 5N: We are at a topmost continuation line and the only
12385 ;;preceding items are annotations.
12386 ((and (c-major-mode-is 'java-mode)
12387 (setq placeholder (point))
12388 (c-beginning-of-statement-1)
12389 (progn
12390 (while (and (c-forward-annotation))
12391 (c-forward-syntactic-ws))
12393 (prog1
12394 (>= (point) placeholder)
12395 (goto-char placeholder)))
12396 (c-add-syntax 'annotation-top-cont (c-point 'boi)))
12398 ;; CASE 5M: we are at a topmost continuation line
12400 (c-beginning-of-statement-1 (c-safe-position (point) paren-state))
12401 (when (c-major-mode-is 'objc-mode)
12402 (setq placeholder (point))
12403 (while (and (c-forward-objc-directive)
12404 (< (point) indent-point))
12405 (c-forward-syntactic-ws)
12406 (setq placeholder (point)))
12407 (goto-char placeholder))
12408 (c-add-syntax 'topmost-intro-cont (c-point 'boi)))
12411 ;; (CASE 6 has been removed.)
12413 ;; CASE 7: line is an expression, not a statement. Most
12414 ;; likely we are either in a function prototype or a function
12415 ;; call argument list
12416 ((not (or (and c-special-brace-lists
12417 (save-excursion
12418 (goto-char containing-sexp)
12419 (c-looking-at-special-brace-list)))
12420 (eq (char-after containing-sexp) ?{)))
12421 (cond
12423 ;; CASE 7A: we are looking at the arglist closing paren.
12424 ;; C.f. case 7F.
12425 ((memq char-after-ip '(?\) ?\]))
12426 (goto-char containing-sexp)
12427 (setq placeholder (c-point 'boi))
12428 (if (and (c-safe (backward-up-list 1) t)
12429 (>= (point) placeholder))
12430 (progn
12431 (forward-char)
12432 (skip-chars-forward " \t"))
12433 (goto-char placeholder))
12434 (c-add-stmt-syntax 'arglist-close (list containing-sexp) t
12435 (c-most-enclosing-brace paren-state (point))
12436 paren-state))
12438 ;; CASE 7B: Looking at the opening brace of an
12439 ;; in-expression block or brace list. C.f. cases 4, 16A
12440 ;; and 17E.
12441 ((and (eq char-after-ip ?{)
12442 (progn
12443 (setq placeholder (c-inside-bracelist-p (point)
12444 paren-state
12445 nil))
12446 (if placeholder
12447 (setq tmpsymbol '(brace-list-open . inexpr-class))
12448 (setq tmpsymbol '(block-open . inexpr-statement)
12449 placeholder
12450 (cdr-safe (c-looking-at-inexpr-block
12451 (c-safe-position containing-sexp
12452 paren-state)
12453 containing-sexp)))
12454 ;; placeholder is nil if it's a block directly in
12455 ;; a function arglist. That makes us skip out of
12456 ;; this case.
12458 (goto-char placeholder)
12459 (back-to-indentation)
12460 (c-add-stmt-syntax (car tmpsymbol) nil t
12461 (c-most-enclosing-brace paren-state (point))
12462 paren-state)
12463 (if (/= (point) placeholder)
12464 (c-add-syntax (cdr tmpsymbol))))
12466 ;; CASE 7C: we are looking at the first argument in an empty
12467 ;; argument list. Use arglist-close if we're actually
12468 ;; looking at a close paren or bracket.
12469 ((memq char-before-ip '(?\( ?\[))
12470 (goto-char containing-sexp)
12471 (setq placeholder (c-point 'boi))
12472 (if (and (c-safe (backward-up-list 1) t)
12473 (>= (point) placeholder))
12474 (progn
12475 (forward-char)
12476 (skip-chars-forward " \t"))
12477 (goto-char placeholder))
12478 (c-add-stmt-syntax 'arglist-intro (list containing-sexp) t
12479 (c-most-enclosing-brace paren-state (point))
12480 paren-state))
12482 ;; CASE 7D: we are inside a conditional test clause. treat
12483 ;; these things as statements
12484 ((progn
12485 (goto-char containing-sexp)
12486 (and (c-safe (c-forward-sexp -1) t)
12487 (looking-at "\\<for\\>[^_]")))
12488 (goto-char (1+ containing-sexp))
12489 (c-forward-syntactic-ws indent-point)
12490 (if (eq char-before-ip ?\;)
12491 (c-add-syntax 'statement (point))
12492 (c-add-syntax 'statement-cont (point))
12495 ;; CASE 7E: maybe a continued ObjC method call. This is the
12496 ;; case when we are inside a [] bracketed exp, and what
12497 ;; precede the opening bracket is not an identifier.
12498 ((and c-opt-method-key
12499 (eq (char-after containing-sexp) ?\[)
12500 (progn
12501 (goto-char (1- containing-sexp))
12502 (c-backward-syntactic-ws (c-point 'bod))
12503 (if (not (looking-at c-symbol-key))
12504 (c-add-syntax 'objc-method-call-cont containing-sexp))
12507 ;; CASE 7F: we are looking at an arglist continuation line,
12508 ;; but the preceding argument is on the same line as the
12509 ;; opening paren. This case includes multi-line
12510 ;; mathematical paren groupings, but we could be on a
12511 ;; for-list continuation line. C.f. case 7A.
12512 ((progn
12513 (goto-char (1+ containing-sexp))
12514 (< (save-excursion
12515 (c-forward-syntactic-ws)
12516 (point))
12517 (c-point 'bonl)))
12518 (goto-char containing-sexp) ; paren opening the arglist
12519 (setq placeholder (c-point 'boi))
12520 (if (and (c-safe (backward-up-list 1) t)
12521 (>= (point) placeholder))
12522 (progn
12523 (forward-char)
12524 (skip-chars-forward " \t"))
12525 (goto-char placeholder))
12526 (c-add-stmt-syntax 'arglist-cont-nonempty (list containing-sexp) t
12527 (c-most-enclosing-brace state-cache (point))
12528 paren-state))
12530 ;; CASE 7G: we are looking at just a normal arglist
12531 ;; continuation line
12532 (t (c-forward-syntactic-ws indent-point)
12533 (c-add-syntax 'arglist-cont (c-point 'boi)))
12536 ;; CASE 8: func-local multi-inheritance line
12537 ((and (c-major-mode-is 'c++-mode)
12538 (save-excursion
12539 (goto-char indent-point)
12540 (skip-chars-forward " \t")
12541 (looking-at c-opt-postfix-decl-spec-key)))
12542 (goto-char indent-point)
12543 (skip-chars-forward " \t")
12544 (cond
12546 ;; CASE 8A: non-hanging colon on an inher intro
12547 ((eq char-after-ip ?:)
12548 (c-backward-syntactic-ws lim)
12549 (c-add-syntax 'inher-intro (c-point 'boi)))
12551 ;; CASE 8B: hanging colon on an inher intro
12552 ((eq char-before-ip ?:)
12553 (c-add-syntax 'inher-intro (c-point 'boi)))
12555 ;; CASE 8C: a continued inheritance line
12557 (c-beginning-of-inheritance-list lim)
12558 (c-add-syntax 'inher-cont (point))
12561 ;; CASE 9: we are inside a brace-list
12562 ((and (not (c-major-mode-is 'awk-mode)) ; Maybe this isn't needed (ACM, 2002/3/29)
12563 (setq special-brace-list
12564 (or (and c-special-brace-lists ;;;; ALWAYS NIL FOR AWK!!
12565 (save-excursion
12566 (goto-char containing-sexp)
12567 (c-looking-at-special-brace-list)))
12568 (c-inside-bracelist-p containing-sexp paren-state t)
12569 (save-excursion
12570 (goto-char containing-sexp)
12571 (and (eq (char-after) ?{)
12572 (not (c-looking-at-statement-block)))))))
12573 (cond
12575 ;; CASE 9A: In the middle of a special brace list opener.
12576 ((and (consp special-brace-list)
12577 (save-excursion
12578 (goto-char containing-sexp)
12579 (eq (char-after) ?\())
12580 (eq char-after-ip (car (cdr special-brace-list))))
12581 (goto-char (car (car special-brace-list)))
12582 (skip-chars-backward " \t")
12583 (if (and (bolp)
12584 (assoc 'statement-cont
12585 (setq placeholder (c-guess-basic-syntax))))
12586 (setq c-syntactic-context placeholder)
12587 (c-beginning-of-statement-1
12588 (c-safe-position (1- containing-sexp) paren-state))
12589 (c-forward-token-2 0)
12590 (while (cond
12591 ((looking-at c-specifier-key)
12592 (c-forward-keyword-clause 1))
12593 ((and c-opt-cpp-prefix
12594 (looking-at c-noise-macro-with-parens-name-re))
12595 (c-forward-noise-clause))))
12596 (c-add-syntax 'brace-list-open (c-point 'boi))))
12598 ;; CASE 9B: brace-list-close brace
12599 ((if (consp special-brace-list)
12600 ;; Check special brace list closer.
12601 (progn
12602 (goto-char (car (car special-brace-list)))
12603 (save-excursion
12604 (goto-char indent-point)
12605 (back-to-indentation)
12607 ;; We were between the special close char and the `)'.
12608 (and (eq (char-after) ?\))
12609 (eq (1+ (point)) (cdr (car special-brace-list))))
12610 ;; We were before the special close char.
12611 (and (eq (char-after) (cdr (cdr special-brace-list)))
12612 (zerop (c-forward-token-2))
12613 (eq (1+ (point)) (cdr (car special-brace-list)))))))
12614 ;; Normal brace list check.
12615 (and (eq char-after-ip ?})
12616 (c-safe (goto-char (c-up-list-backward (point))) t)
12617 (= (point) containing-sexp)))
12618 (if (eq (point) (c-point 'boi))
12619 (c-add-syntax 'brace-list-close (point))
12620 (setq lim (c-most-enclosing-brace state-cache (point)))
12621 (c-beginning-of-statement-1 lim nil nil t)
12622 (c-add-stmt-syntax 'brace-list-close nil t lim paren-state)))
12625 ;; Prepare for the rest of the cases below by going to the
12626 ;; token following the opening brace
12627 (if (consp special-brace-list)
12628 (progn
12629 (goto-char (car (car special-brace-list)))
12630 (c-forward-token-2 1 nil indent-point))
12631 (goto-char containing-sexp))
12632 (forward-char)
12633 (let ((start (point)))
12634 (c-forward-syntactic-ws indent-point)
12635 (goto-char (max start (c-point 'bol))))
12636 (c-skip-ws-forward indent-point)
12637 (cond
12639 ;; CASE 9C: we're looking at the first line in a brace-list
12640 ((= (point) indent-point)
12641 (if (consp special-brace-list)
12642 (goto-char (car (car special-brace-list)))
12643 (goto-char containing-sexp))
12644 (if (eq (point) (c-point 'boi))
12645 (c-add-syntax 'brace-list-intro (point))
12646 (setq lim (c-most-enclosing-brace state-cache (point)))
12647 (c-beginning-of-statement-1 lim)
12648 (c-add-stmt-syntax 'brace-list-intro nil t lim paren-state)))
12650 ;; CASE 9D: this is just a later brace-list-entry or
12651 ;; brace-entry-open
12652 (t (if (or (eq char-after-ip ?{)
12653 (and c-special-brace-lists
12654 (save-excursion
12655 (goto-char indent-point)
12656 (c-forward-syntactic-ws (c-point 'eol))
12657 (c-looking-at-special-brace-list (point)))))
12658 (c-add-syntax 'brace-entry-open (point))
12659 (c-add-stmt-syntax 'brace-list-entry nil t containing-sexp
12660 paren-state (point))
12662 ))))
12664 ;; CASE 10: A continued statement or top level construct.
12665 ((and (not (memq char-before-ip '(?\; ?:)))
12666 (not (c-at-vsemi-p before-ws-ip))
12667 (or (not (eq char-before-ip ?}))
12668 (c-looking-at-inexpr-block-backward state-cache))
12669 (> (point)
12670 (save-excursion
12671 (c-beginning-of-statement-1 containing-sexp)
12672 (setq placeholder (point))))
12673 (/= placeholder containing-sexp))
12674 ;; This is shared with case 18.
12675 (c-guess-continued-construct indent-point
12676 char-after-ip
12677 placeholder
12678 containing-sexp
12679 paren-state))
12681 ;; CASE 16: block close brace, possibly closing the defun or
12682 ;; the class
12683 ((eq char-after-ip ?})
12684 ;; From here on we have the next containing sexp in lim.
12685 (setq lim (c-most-enclosing-brace paren-state))
12686 (goto-char containing-sexp)
12687 (cond
12689 ;; CASE 16E: Closing a statement block? This catches
12690 ;; cases where it's preceded by a statement keyword,
12691 ;; which works even when used in an "invalid" context,
12692 ;; e.g. a macro argument.
12693 ((c-after-conditional)
12694 (c-backward-to-block-anchor lim)
12695 (c-add-stmt-syntax 'block-close nil t lim paren-state))
12697 ;; CASE 16A: closing a lambda defun or an in-expression
12698 ;; block? C.f. cases 4, 7B and 17E.
12699 ((setq placeholder (c-looking-at-inexpr-block
12700 (c-safe-position containing-sexp paren-state)
12701 nil))
12702 (setq tmpsymbol (if (eq (car placeholder) 'inlambda)
12703 'inline-close
12704 'block-close))
12705 (goto-char containing-sexp)
12706 (back-to-indentation)
12707 (if (= containing-sexp (point))
12708 (c-add-syntax tmpsymbol (point))
12709 (goto-char (cdr placeholder))
12710 (back-to-indentation)
12711 (c-add-stmt-syntax tmpsymbol nil t
12712 (c-most-enclosing-brace paren-state (point))
12713 paren-state)
12714 (if (/= (point) (cdr placeholder))
12715 (c-add-syntax (car placeholder)))))
12717 ;; CASE 16B: does this close an inline or a function in
12718 ;; a non-class declaration level block?
12719 ((save-excursion
12720 (and lim
12721 (progn
12722 (goto-char lim)
12723 (c-looking-at-decl-block
12724 (c-most-enclosing-brace paren-state lim)
12725 nil))
12726 (setq placeholder (point))))
12727 (c-backward-to-decl-anchor lim)
12728 (back-to-indentation)
12729 (if (save-excursion
12730 (goto-char placeholder)
12731 (looking-at c-other-decl-block-key))
12732 (c-add-syntax 'defun-close (point))
12733 (c-add-syntax 'inline-close (point))))
12735 ;; CASE 16F: Can be a defun-close of a function declared
12736 ;; in a statement block, e.g. in Pike or when using gcc
12737 ;; extensions, but watch out for macros followed by
12738 ;; blocks. Let it through to be handled below.
12739 ;; C.f. cases B.3 and 17G.
12740 ((save-excursion
12741 (and (not (c-at-statement-start-p))
12742 (eq (c-beginning-of-statement-1 lim nil nil t) 'same)
12743 (setq placeholder (point))
12744 (let ((c-recognize-typeless-decls nil))
12745 ;; Turn off recognition of constructs that
12746 ;; lacks a type in this case, since that's more
12747 ;; likely to be a macro followed by a block.
12748 (c-forward-decl-or-cast-1 (c-point 'bosws) nil nil))))
12749 (back-to-indentation)
12750 (if (/= (point) containing-sexp)
12751 (goto-char placeholder))
12752 (c-add-stmt-syntax 'defun-close nil t lim paren-state))
12754 ;; CASE 16C: If there is an enclosing brace then this is
12755 ;; a block close since defun closes inside declaration
12756 ;; level blocks have been handled above.
12757 (lim
12758 ;; If the block is preceded by a case/switch label on
12759 ;; the same line, we anchor at the first preceding label
12760 ;; at boi. The default handling in c-add-stmt-syntax
12761 ;; really fixes it better, but we do like this to keep
12762 ;; the indentation compatible with version 5.28 and
12763 ;; earlier. C.f. case 17H.
12764 (while (and (/= (setq placeholder (point)) (c-point 'boi))
12765 (eq (c-beginning-of-statement-1 lim) 'label)))
12766 (goto-char placeholder)
12767 (if (looking-at c-label-kwds-regexp)
12768 (c-add-syntax 'block-close (point))
12769 (goto-char containing-sexp)
12770 ;; c-backward-to-block-anchor not necessary here; those
12771 ;; situations are handled in case 16E above.
12772 (c-add-stmt-syntax 'block-close nil t lim paren-state)))
12774 ;; CASE 16D: Only top level defun close left.
12776 (goto-char containing-sexp)
12777 (c-backward-to-decl-anchor lim)
12778 (c-add-stmt-syntax 'defun-close nil nil
12779 (c-most-enclosing-brace paren-state)
12780 paren-state))
12783 ;; CASE 19: line is an expression, not a statement, and is directly
12784 ;; contained by a template delimiter. Most likely, we are in a
12785 ;; template arglist within a statement. This case is based on CASE
12786 ;; 7. At some point in the future, we may wish to create more
12787 ;; syntactic symbols such as `template-intro',
12788 ;; `template-cont-nonempty', etc., and distinguish between them as we
12789 ;; do for `arglist-intro' etc. (2009-12-07).
12790 ((and c-recognize-<>-arglists
12791 (setq containing-< (c-up-list-backward indent-point containing-sexp))
12792 (eq (char-after containing-<) ?\<))
12793 (setq placeholder (c-point 'boi containing-<))
12794 (goto-char containing-sexp) ; Most nested Lbrace/Lparen (but not
12795 ; '<') before indent-point.
12796 (if (>= (point) placeholder)
12797 (progn
12798 (forward-char)
12799 (skip-chars-forward " \t"))
12800 (goto-char placeholder))
12801 (c-add-stmt-syntax 'template-args-cont (list containing-<) t
12802 (c-most-enclosing-brace state-cache (point))
12803 paren-state))
12805 ;; CASE 17: Statement or defun catchall.
12807 (goto-char indent-point)
12808 ;; Back up statements until we find one that starts at boi.
12809 (while (let* ((prev-point (point))
12810 (last-step-type (c-beginning-of-statement-1
12811 containing-sexp)))
12812 (if (= (point) prev-point)
12813 (progn
12814 (setq step-type (or step-type last-step-type))
12815 nil)
12816 (setq step-type last-step-type)
12817 (/= (point) (c-point 'boi)))))
12818 (cond
12820 ;; CASE 17B: continued statement
12821 ((and (eq step-type 'same)
12822 (/= (point) indent-point))
12823 (c-add-stmt-syntax 'statement-cont nil nil
12824 containing-sexp paren-state))
12826 ;; CASE 17A: After a case/default label?
12827 ((progn
12828 (while (and (eq step-type 'label)
12829 (not (looking-at c-label-kwds-regexp)))
12830 (setq step-type
12831 (c-beginning-of-statement-1 containing-sexp)))
12832 (eq step-type 'label))
12833 (c-add-stmt-syntax (if (eq char-after-ip ?{)
12834 'statement-case-open
12835 'statement-case-intro)
12836 nil t containing-sexp paren-state))
12838 ;; CASE 17D: any old statement
12839 ((progn
12840 (while (eq step-type 'label)
12841 (setq step-type
12842 (c-beginning-of-statement-1 containing-sexp)))
12843 (eq step-type 'previous))
12844 (c-add-stmt-syntax 'statement nil t
12845 containing-sexp paren-state)
12846 (if (eq char-after-ip ?{)
12847 (c-add-syntax 'block-open)))
12849 ;; CASE 17I: Inside a substatement block.
12850 ((progn
12851 ;; The following tests are all based on containing-sexp.
12852 (goto-char containing-sexp)
12853 ;; From here on we have the next containing sexp in lim.
12854 (setq lim (c-most-enclosing-brace paren-state containing-sexp))
12855 (c-after-conditional))
12856 (c-backward-to-block-anchor lim)
12857 (c-add-stmt-syntax 'statement-block-intro nil t
12858 lim paren-state)
12859 (if (eq char-after-ip ?{)
12860 (c-add-syntax 'block-open)))
12862 ;; CASE 17E: first statement in an in-expression block.
12863 ;; C.f. cases 4, 7B and 16A.
12864 ((setq placeholder (c-looking-at-inexpr-block
12865 (c-safe-position containing-sexp paren-state)
12866 nil))
12867 (setq tmpsymbol (if (eq (car placeholder) 'inlambda)
12868 'defun-block-intro
12869 'statement-block-intro))
12870 (back-to-indentation)
12871 (if (= containing-sexp (point))
12872 (c-add-syntax tmpsymbol (point))
12873 (goto-char (cdr placeholder))
12874 (back-to-indentation)
12875 (c-add-stmt-syntax tmpsymbol nil t
12876 (c-most-enclosing-brace state-cache (point))
12877 paren-state)
12878 (if (/= (point) (cdr placeholder))
12879 (c-add-syntax (car placeholder))))
12880 (if (eq char-after-ip ?{)
12881 (c-add-syntax 'block-open)))
12883 ;; CASE 17F: first statement in an inline, or first
12884 ;; statement in a top-level defun. we can tell this is it
12885 ;; if there are no enclosing braces that haven't been
12886 ;; narrowed out by a class (i.e. don't use bod here).
12887 ((save-excursion
12888 (or (not (setq placeholder (c-most-enclosing-brace
12889 paren-state)))
12890 (and (progn
12891 (goto-char placeholder)
12892 (eq (char-after) ?{))
12893 (c-looking-at-decl-block (c-most-enclosing-brace
12894 paren-state (point))
12895 nil))))
12896 (c-backward-to-decl-anchor lim)
12897 (back-to-indentation)
12898 (c-add-syntax 'defun-block-intro (point)))
12900 ;; CASE 17G: First statement in a function declared inside
12901 ;; a normal block. This can occur in Pike and with
12902 ;; e.g. the gcc extensions, but watch out for macros
12903 ;; followed by blocks. C.f. cases B.3 and 16F.
12904 ((save-excursion
12905 (and (not (c-at-statement-start-p))
12906 (eq (c-beginning-of-statement-1 lim nil nil t) 'same)
12907 (setq placeholder (point))
12908 (let ((c-recognize-typeless-decls nil))
12909 ;; Turn off recognition of constructs that lacks
12910 ;; a type in this case, since that's more likely
12911 ;; to be a macro followed by a block.
12912 (c-forward-decl-or-cast-1 (c-point 'bosws) nil nil))))
12913 (back-to-indentation)
12914 (if (/= (point) containing-sexp)
12915 (goto-char placeholder))
12916 (c-add-stmt-syntax 'defun-block-intro nil t
12917 lim paren-state))
12919 ;; CASE 17H: First statement in a block.
12921 ;; If the block is preceded by a case/switch label on the
12922 ;; same line, we anchor at the first preceding label at
12923 ;; boi. The default handling in c-add-stmt-syntax is
12924 ;; really fixes it better, but we do like this to keep the
12925 ;; indentation compatible with version 5.28 and earlier.
12926 ;; C.f. case 16C.
12927 (while (and (/= (setq placeholder (point)) (c-point 'boi))
12928 (eq (c-beginning-of-statement-1 lim) 'label)))
12929 (goto-char placeholder)
12930 (if (looking-at c-label-kwds-regexp)
12931 (c-add-syntax 'statement-block-intro (point))
12932 (goto-char containing-sexp)
12933 ;; c-backward-to-block-anchor not necessary here; those
12934 ;; situations are handled in case 17I above.
12935 (c-add-stmt-syntax 'statement-block-intro nil t
12936 lim paren-state))
12937 (if (eq char-after-ip ?{)
12938 (c-add-syntax 'block-open)))
12942 ;; now we need to look at any modifiers
12943 (goto-char indent-point)
12944 (skip-chars-forward " \t")
12946 ;; are we looking at a comment only line?
12947 (when (and (looking-at c-comment-start-regexp)
12948 (/= (c-forward-token-2 0 nil (c-point 'eol)) 0))
12949 (c-append-syntax 'comment-intro))
12951 ;; we might want to give additional offset to friends (in C++).
12952 (when (and c-opt-friend-key
12953 (looking-at c-opt-friend-key))
12954 (c-append-syntax 'friend))
12956 ;; Set syntactic-relpos.
12957 (let ((p c-syntactic-context))
12958 (while (and p
12959 (if (integerp (c-langelem-pos (car p)))
12960 (progn
12961 (setq syntactic-relpos (c-langelem-pos (car p)))
12962 nil)
12964 (setq p (cdr p))))
12966 ;; Start of or a continuation of a preprocessor directive?
12967 (if (and macro-start
12968 (eq macro-start (c-point 'boi))
12969 (not (and (c-major-mode-is 'pike-mode)
12970 (eq (char-after (1+ macro-start)) ?\"))))
12971 (c-append-syntax 'cpp-macro)
12972 (when (and c-syntactic-indentation-in-macros macro-start)
12973 (if in-macro-expr
12974 (when (or
12975 (< syntactic-relpos macro-start)
12976 (not (or
12977 (assq 'arglist-intro c-syntactic-context)
12978 (assq 'arglist-cont c-syntactic-context)
12979 (assq 'arglist-cont-nonempty c-syntactic-context)
12980 (assq 'arglist-close c-syntactic-context))))
12981 ;; If inside a cpp expression, i.e. anywhere in a
12982 ;; cpp directive except a #define body, we only let
12983 ;; through the syntactic analysis that is internal
12984 ;; in the expression. That means the arglist
12985 ;; elements, if they are anchored inside the cpp
12986 ;; expression.
12987 (setq c-syntactic-context nil)
12988 (c-add-syntax 'cpp-macro-cont macro-start))
12989 (when (and (eq macro-start syntactic-relpos)
12990 (not (assq 'cpp-define-intro c-syntactic-context))
12991 (save-excursion
12992 (goto-char macro-start)
12993 (or (not (c-forward-to-cpp-define-body))
12994 (<= (point) (c-point 'boi indent-point)))))
12995 ;; Inside a #define body and the syntactic analysis is
12996 ;; anchored on the start of the #define. In this case
12997 ;; we add cpp-define-intro to get the extra
12998 ;; indentation of the #define body.
12999 (c-add-syntax 'cpp-define-intro)))))
13001 ;; return the syntax
13002 c-syntactic-context)))
13005 ;; Indentation calculation.
13007 (defun c-evaluate-offset (offset langelem symbol)
13008 ;; offset can be a number, a function, a variable, a list, or one of
13009 ;; the symbols + or -
13011 ;; This function might do hidden buffer changes.
13012 (let ((res
13013 (cond
13014 ((numberp offset) offset)
13015 ((vectorp offset) offset)
13016 ((null offset) nil)
13018 ((eq offset '+) c-basic-offset)
13019 ((eq offset '-) (- c-basic-offset))
13020 ((eq offset '++) (* 2 c-basic-offset))
13021 ((eq offset '--) (* 2 (- c-basic-offset)))
13022 ((eq offset '*) (/ c-basic-offset 2))
13023 ((eq offset '/) (/ (- c-basic-offset) 2))
13025 ((functionp offset)
13026 (c-evaluate-offset
13027 (funcall offset
13028 (cons (c-langelem-sym langelem)
13029 (c-langelem-pos langelem)))
13030 langelem symbol))
13032 ((listp offset)
13033 (cond
13034 ((eq (car offset) 'quote)
13035 (c-benign-error "The offset %S for %s was mistakenly quoted"
13036 offset symbol)
13037 nil)
13039 ((memq (car offset) '(min max))
13040 (let (res val (method (car offset)))
13041 (setq offset (cdr offset))
13042 (while offset
13043 (setq val (c-evaluate-offset (car offset) langelem symbol))
13044 (cond
13045 ((not val))
13046 ((not res)
13047 (setq res val))
13048 ((integerp val)
13049 (if (vectorp res)
13050 (c-benign-error "\
13051 Error evaluating offset %S for %s: \
13052 Cannot combine absolute offset %S with relative %S in `%s' method"
13053 (car offset) symbol res val method)
13054 (setq res (funcall method res val))))
13056 (if (integerp res)
13057 (c-benign-error "\
13058 Error evaluating offset %S for %s: \
13059 Cannot combine relative offset %S with absolute %S in `%s' method"
13060 (car offset) symbol res val method)
13061 (setq res (vector (funcall method (aref res 0)
13062 (aref val 0)))))))
13063 (setq offset (cdr offset)))
13064 res))
13066 ((eq (car offset) 'add)
13067 (let (res val)
13068 (setq offset (cdr offset))
13069 (while offset
13070 (setq val (c-evaluate-offset (car offset) langelem symbol))
13071 (cond
13072 ((not val))
13073 ((not res)
13074 (setq res val))
13075 ((integerp val)
13076 (if (vectorp res)
13077 (setq res (vector (+ (aref res 0) val)))
13078 (setq res (+ res val))))
13080 (if (vectorp res)
13081 (c-benign-error "\
13082 Error evaluating offset %S for %s: \
13083 Cannot combine absolute offsets %S and %S in `add' method"
13084 (car offset) symbol res val)
13085 (setq res val)))) ; Override.
13086 (setq offset (cdr offset)))
13087 res))
13090 (let (res)
13091 (when (eq (car offset) 'first)
13092 (setq offset (cdr offset)))
13093 (while (and (not res) offset)
13094 (setq res (c-evaluate-offset (car offset) langelem symbol)
13095 offset (cdr offset)))
13096 res))))
13098 ((and (symbolp offset) (boundp offset))
13099 (symbol-value offset))
13102 (c-benign-error "Unknown offset format %S for %s" offset symbol)
13103 nil))))
13105 (if (or (null res) (integerp res)
13106 (and (vectorp res) (= (length res) 1) (integerp (aref res 0))))
13108 (c-benign-error "Error evaluating offset %S for %s: Got invalid value %S"
13109 offset symbol res)
13110 nil)))
13112 (defun c-calc-offset (langelem)
13113 ;; Get offset from LANGELEM which is a list beginning with the
13114 ;; syntactic symbol and followed by any analysis data it provides.
13115 ;; That data may be zero or more elements, but if at least one is
13116 ;; given then the first is the anchor position (or nil). The symbol
13117 ;; is matched against `c-offsets-alist' and the offset calculated
13118 ;; from that is returned.
13120 ;; This function might do hidden buffer changes.
13121 (let* ((symbol (c-langelem-sym langelem))
13122 (match (assq symbol c-offsets-alist))
13123 (offset (cdr-safe match)))
13124 (if match
13125 (setq offset (c-evaluate-offset offset langelem symbol))
13126 (if c-strict-syntax-p
13127 (c-benign-error "No offset found for syntactic symbol %s" symbol))
13128 (setq offset 0))
13129 (if (vectorp offset)
13130 offset
13131 (or (and (numberp offset) offset)
13132 (and (symbolp offset) (symbol-value offset))
13136 (defun c-get-offset (langelem)
13137 ;; This is a compatibility wrapper for `c-calc-offset' in case
13138 ;; someone is calling it directly. It takes an old style syntactic
13139 ;; element on the form (SYMBOL . ANCHOR-POS) and converts it to the
13140 ;; new list form.
13142 ;; This function might do hidden buffer changes.
13143 (if (c-langelem-pos langelem)
13144 (c-calc-offset (list (c-langelem-sym langelem)
13145 (c-langelem-pos langelem)))
13146 (c-calc-offset langelem)))
13148 (defun c-get-syntactic-indentation (langelems)
13149 ;; Calculate the syntactic indentation from a syntactic description
13150 ;; as returned by `c-guess-syntax'.
13152 ;; Note that topmost-intro always has an anchor position at bol, for
13153 ;; historical reasons. It's often used together with other symbols
13154 ;; that have more sane positions. Since we always use the first
13155 ;; found anchor position, we rely on that these other symbols always
13156 ;; precede topmost-intro in the LANGELEMS list.
13158 ;; This function might do hidden buffer changes.
13159 (let ((indent 0) anchor)
13161 (while langelems
13162 (let* ((c-syntactic-element (car langelems))
13163 (res (c-calc-offset c-syntactic-element)))
13165 (if (vectorp res)
13166 ;; Got an absolute column that overrides any indentation
13167 ;; we've collected so far, but not the relative
13168 ;; indentation we might get for the nested structures
13169 ;; further down the langelems list.
13170 (setq indent (elt res 0)
13171 anchor (point-min)) ; A position at column 0.
13173 ;; Got a relative change of the current calculated
13174 ;; indentation.
13175 (setq indent (+ indent res))
13177 ;; Use the anchor position from the first syntactic
13178 ;; element with one.
13179 (unless anchor
13180 (setq anchor (c-langelem-pos (car langelems)))))
13182 (setq langelems (cdr langelems))))
13184 (if anchor
13185 (+ indent (save-excursion
13186 (goto-char anchor)
13187 (current-column)))
13188 indent)))
13191 (cc-provide 'cc-engine)
13193 ;; Local Variables:
13194 ;; indent-tabs-mode: t
13195 ;; tab-width: 8
13196 ;; End:
13197 ;;; cc-engine.el ends here