Correct c-parse-state-get-strategy for moving HERE backward into a macro.
[emacs.git] / lisp / progmodes / cc-engine.el
blobe84c4cebf6943308a6f1e8668c82696a000e47b5
1 ;;; cc-engine.el --- core syntax guessing engine for CC mode -*- coding: utf-8 -*-
3 ;; Copyright (C) 1985, 1987, 1992-2017 Free Software Foundation, Inc.
5 ;; Authors: 2001- Alan Mackenzie
6 ;; 1998- Martin Stjernholm
7 ;; 1992-1999 Barry A. Warsaw
8 ;; 1987 Dave Detlefs
9 ;; 1987 Stewart Clamen
10 ;; 1985 Richard M. Stallman
11 ;; Maintainer: bug-cc-mode@gnu.org
12 ;; Created: 22-Apr-1997 (split from cc-mode.el)
13 ;; Keywords: c languages
14 ;; Package: cc-mode
16 ;; This file is part of GNU Emacs.
18 ;; GNU Emacs is free software: you can redistribute it and/or modify
19 ;; it under the terms of the GNU General Public License as published by
20 ;; the Free Software Foundation, either version 3 of the License, or
21 ;; (at your option) any later version.
23 ;; GNU Emacs is distributed in the hope that it will be useful,
24 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
25 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26 ;; GNU General Public License for more details.
28 ;; You should have received a copy of the GNU General Public License
29 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
31 ;;; Commentary:
33 ;; The functions which have docstring documentation can be considered
34 ;; part of an API which other packages can use in CC Mode buffers.
35 ;; Otoh, undocumented functions and functions with the documentation
36 ;; in comments are considered purely internal and can change semantics
37 ;; or even disappear in the future.
39 ;; (This policy applies to CC Mode as a whole, not just this file. It
40 ;; probably also applies to many other Emacs packages, but here it's
41 ;; clearly spelled out.)
43 ;; Hidden buffer changes
45 ;; Various functions in CC Mode use text properties for caching and
46 ;; syntactic markup purposes, and those of them that might modify such
47 ;; properties but still don't modify the buffer in a visible way are
48 ;; said to do "hidden buffer changes". They should be used within
49 ;; `c-save-buffer-state' or a similar function that saves and restores
50 ;; buffer modifiedness, disables buffer change hooks, etc.
52 ;; Interactive functions are assumed to not do hidden buffer changes,
53 ;; except in the specific parts of them that do real changes.
55 ;; Lineup functions are assumed to do hidden buffer changes. They
56 ;; must not do real changes, though.
58 ;; All other functions that do hidden buffer changes have that noted
59 ;; in their doc string or comment.
61 ;; The intention with this system is to avoid wrapping every leaf
62 ;; function that do hidden buffer changes inside
63 ;; `c-save-buffer-state'. It should be used as near the top of the
64 ;; interactive functions as possible.
66 ;; Functions called during font locking are allowed to do hidden
67 ;; buffer changes since the font-lock package run them in a context
68 ;; similar to `c-save-buffer-state' (in fact, that function is heavily
69 ;; inspired by `save-buffer-state' in the font-lock package).
71 ;; Use of text properties
73 ;; CC Mode uses several text properties internally to mark up various
74 ;; positions, e.g. to improve speed and to eliminate glitches in
75 ;; interactive refontification.
77 ;; Note: This doc is for internal use only. Other packages should not
78 ;; assume that these text properties are used as described here.
80 ;; 'category
81 ;; Used for "indirection". With its help, some other property can
82 ;; be cheaply and easily switched on or off everywhere it occurs.
84 ;; 'syntax-table
85 ;; Used to modify the syntax of some characters. It is used to
86 ;; mark the "<" and ">" of angle bracket parens with paren syntax, to
87 ;; "hide" obtrusive characters in preprocessor lines, and to mark C++
88 ;; raw strings to enable their fontification.
90 ;; This property is used on single characters and is therefore
91 ;; always treated as front and rear nonsticky (or start and end open
92 ;; in XEmacs vocabulary). It's therefore installed on
93 ;; `text-property-default-nonsticky' if that variable exists (Emacs
94 ;; >= 21).
96 ;; 'c-is-sws and 'c-in-sws
97 ;; Used by `c-forward-syntactic-ws' and `c-backward-syntactic-ws' to
98 ;; speed them up. See the comment blurb before `c-put-is-sws'
99 ;; below for further details.
101 ;; 'c-type
102 ;; This property is used on single characters to mark positions with
103 ;; special syntactic relevance of various sorts. Its primary use is
104 ;; to avoid glitches when multiline constructs are refontified
105 ;; interactively (on font lock decoration level 3). It's cleared in
106 ;; a region before it's fontified and is then put on relevant chars
107 ;; in that region as they are encountered during the fontification.
108 ;; The value specifies the kind of position:
110 ;; 'c-decl-arg-start
111 ;; Put on the last char of the token preceding each declaration
112 ;; inside a declaration style arglist (typically in a function
113 ;; prototype).
115 ;; 'c-decl-end
116 ;; Put on the last char of the token preceding a declaration.
117 ;; This is used in cases where declaration boundaries can't be
118 ;; recognized simply by looking for a token like ";" or "}".
119 ;; `c-type-decl-end-used' must be set if this is used (see also
120 ;; `c-find-decl-spots').
122 ;; 'c-<>-arg-sep
123 ;; Put on the commas that separate arguments in angle bracket
124 ;; arglists like C++ template arglists.
126 ;; 'c-decl-id-start and 'c-decl-type-start
127 ;; Put on the last char of the token preceding each declarator
128 ;; in the declarator list of a declaration. They are also used
129 ;; between the identifiers cases like enum declarations.
130 ;; 'c-decl-type-start is used when the declarators are types,
131 ;; 'c-decl-id-start otherwise.
133 ;; 'c-not-decl
134 ;; Put on the brace which introduces a brace list and on the commas
135 ;; which separate the element within it.
137 ;; 'c-awk-NL-prop
138 ;; Used in AWK mode to mark the various kinds of newlines. See
139 ;; cc-awk.el.
141 ;;; Code:
143 (eval-when-compile
144 (let ((load-path
145 (if (and (boundp 'byte-compile-dest-file)
146 (stringp byte-compile-dest-file))
147 (cons (file-name-directory byte-compile-dest-file) load-path)
148 load-path)))
149 (load "cc-bytecomp" nil t)))
151 (cc-require 'cc-defs)
152 (cc-require-when-compile 'cc-langs)
153 (cc-require 'cc-vars)
155 (eval-when-compile (require 'cl))
158 ;; Make declarations for all the `c-lang-defvar' variables in cc-langs.
160 (defmacro c-declare-lang-variables ()
161 `(progn
162 ,@(c--mapcan (lambda (init)
163 `(,(if (elt init 2)
164 `(defvar ,(car init) nil ,(elt init 2))
165 `(defvar ,(car init) nil))
166 (make-variable-buffer-local ',(car init))))
167 (cdr c-lang-variable-inits))))
168 (c-declare-lang-variables)
171 ;;; Internal state variables.
173 ;; Internal state of hungry delete key feature
174 (defvar c-hungry-delete-key nil)
175 (make-variable-buffer-local 'c-hungry-delete-key)
177 ;; The electric flag (toggled by `c-toggle-electric-state').
178 ;; If t, electric actions (like automatic reindentation, and (if
179 ;; c-auto-newline is also set) auto newlining) will happen when an electric
180 ;; key like `{' is pressed (or an electric keyword like `else').
181 (defvar c-electric-flag t)
182 (make-variable-buffer-local 'c-electric-flag)
184 ;; Internal state of auto newline feature.
185 (defvar c-auto-newline nil)
186 (make-variable-buffer-local 'c-auto-newline)
188 ;; Included in the mode line to indicate the active submodes.
189 ;; (defvar c-submode-indicators nil)
190 ;; (make-variable-buffer-local 'c-submode-indicators)
192 (defun c-calculate-state (arg prevstate)
193 ;; Calculate the new state of PREVSTATE, t or nil, based on arg. If
194 ;; arg is nil or zero, toggle the state. If arg is negative, turn
195 ;; the state off, and if arg is positive, turn the state on
196 (if (or (not arg)
197 (zerop (setq arg (prefix-numeric-value arg))))
198 (not prevstate)
199 (> arg 0)))
202 ;; Basic handling of preprocessor directives.
204 ;; This is a dynamically bound cache used together with
205 ;; `c-query-macro-start' and `c-query-and-set-macro-start'. It only
206 ;; works as long as point doesn't cross a macro boundary.
207 (defvar c-macro-start 'unknown)
209 (defsubst c-query-and-set-macro-start ()
210 (if (symbolp c-macro-start)
211 (setq c-macro-start (save-excursion
212 (c-save-buffer-state ()
213 (and (c-beginning-of-macro)
214 (point)))))
215 c-macro-start))
217 (defsubst c-query-macro-start ()
218 (if (symbolp c-macro-start)
219 (save-excursion
220 (c-save-buffer-state ()
221 (and (c-beginning-of-macro)
222 (point))))
223 c-macro-start))
225 ;; One element macro cache to cope with continual movement within very large
226 ;; CPP macros.
227 (defvar c-macro-cache nil)
228 (make-variable-buffer-local 'c-macro-cache)
229 ;; Nil or cons of the bounds of the most recent CPP form probed by
230 ;; `c-beginning-of-macro', `c-end-of-macro' or `c-syntactic-end-of-macro'.
231 ;; The cdr will be nil if we know only the start of the CPP form.
232 (defvar c-macro-cache-start-pos nil)
233 (make-variable-buffer-local 'c-macro-cache-start-pos)
234 ;; The starting position from where we determined `c-macro-cache'.
235 (defvar c-macro-cache-syntactic nil)
236 (make-variable-buffer-local 'c-macro-cache-syntactic)
237 ;; Either nil, or the syntactic end of the macro currently represented by
238 ;; `c-macro-cache'.
239 (defvar c-macro-cache-no-comment nil)
240 (make-variable-buffer-local 'c-macro-cache-no-comment)
241 ;; Either nil, or the last character of the macro currently represented by
242 ;; `c-macro-cache' which isn't in a comment. */
244 (defun c-invalidate-macro-cache (beg end)
245 ;; Called from a before-change function. If the change region is before or
246 ;; in the macro characterized by `c-macro-cache' etc., nullify it
247 ;; appropriately. BEG and END are the standard before-change-functions
248 ;; parameters. END isn't used.
249 (cond
250 ((null c-macro-cache))
251 ((< beg (car c-macro-cache))
252 (setq c-macro-cache nil
253 c-macro-cache-start-pos nil
254 c-macro-cache-syntactic nil
255 c-macro-cache-no-comment nil))
256 ((and (cdr c-macro-cache)
257 (< beg (cdr c-macro-cache)))
258 (setcdr c-macro-cache nil)
259 (setq c-macro-cache-start-pos beg
260 c-macro-cache-syntactic nil
261 c-macro-cache-no-comment nil))))
263 (defun c-macro-is-genuine-p ()
264 ;; Check that the ostensible CPP construct at point is a real one. In
265 ;; particular, if point is on the first line of a narrowed buffer, make sure
266 ;; that the "#" isn't, say, the second character of a "##" operator. Return
267 ;; t when the macro is real, nil otherwise.
268 (let ((here (point)))
269 (beginning-of-line)
270 (prog1
271 (if (and (eq (point) (point-min))
272 (/= (point) 1))
273 (save-restriction
274 (widen)
275 (beginning-of-line)
276 (and (looking-at c-anchored-cpp-prefix)
277 (eq (match-beginning 1) here)))
279 (goto-char here))))
281 (defun c-beginning-of-macro (&optional lim)
282 "Go to the beginning of a preprocessor directive.
283 Leave point at the beginning of the directive and return t if in one,
284 otherwise return nil and leave point unchanged.
286 Note that this function might do hidden buffer changes. See the
287 comment at the start of cc-engine.el for more info."
288 (let ((here (point)))
289 (when c-opt-cpp-prefix
290 (if (and (car c-macro-cache)
291 (>= (point) (car c-macro-cache))
292 (or (and (cdr c-macro-cache)
293 (<= (point) (cdr c-macro-cache)))
294 (<= (point) c-macro-cache-start-pos)))
295 (unless (< (car c-macro-cache) (or lim (point-min)))
296 (progn (goto-char (max (or lim (point-min)) (car c-macro-cache)))
297 (setq c-macro-cache-start-pos
298 (max c-macro-cache-start-pos here))
300 (setq c-macro-cache nil
301 c-macro-cache-start-pos nil
302 c-macro-cache-syntactic nil
303 c-macro-cache-no-comment nil)
305 (save-restriction
306 (if lim (narrow-to-region lim (point-max)))
307 (beginning-of-line)
308 (while (eq (char-before (1- (point))) ?\\)
309 (forward-line -1))
310 (back-to-indentation)
311 (if (and (<= (point) here)
312 (save-match-data (looking-at c-opt-cpp-start))
313 (c-macro-is-genuine-p))
314 (progn
315 (setq c-macro-cache (cons (point) nil)
316 c-macro-cache-start-pos here)
318 (goto-char here)
319 nil))))))
321 (defun c-end-of-macro ()
322 "Go to the end of a preprocessor directive.
323 More accurately, move the point to the end of the closest following
324 line that doesn't end with a line continuation backslash - no check is
325 done that the point is inside a cpp directive to begin with.
327 Note that this function might do hidden buffer changes. See the
328 comment at the start of cc-engine.el for more info."
329 (if (and (cdr c-macro-cache)
330 (<= (point) (cdr c-macro-cache))
331 (>= (point) (car c-macro-cache)))
332 (goto-char (cdr c-macro-cache))
333 (unless (and (car c-macro-cache)
334 (<= (point) c-macro-cache-start-pos)
335 (>= (point) (car c-macro-cache)))
336 (setq c-macro-cache nil
337 c-macro-cache-start-pos nil
338 c-macro-cache-syntactic nil
339 c-macro-cache-no-comment nil))
340 (while (progn
341 (end-of-line)
342 (when (and (eq (char-before) ?\\)
343 (not (eobp)))
344 (forward-char)
345 t)))
346 (when (car c-macro-cache)
347 (setcdr c-macro-cache (point)))))
349 (defun c-syntactic-end-of-macro ()
350 ;; Go to the end of a CPP directive, or a "safe" pos just before.
352 ;; This is normally the end of the next non-escaped line. A "safe"
353 ;; position is one not within a string or comment. (The EOL on a line
354 ;; comment is NOT "safe").
356 ;; This function must only be called from the beginning of a CPP construct.
358 ;; Note that this function might do hidden buffer changes. See the comment
359 ;; at the start of cc-engine.el for more info.
360 (let* ((here (point))
361 (there (progn (c-end-of-macro) (point)))
363 (if c-macro-cache-syntactic
364 (goto-char c-macro-cache-syntactic)
365 (setq s (parse-partial-sexp here there))
366 (while (and (or (nth 3 s) ; in a string
367 (nth 4 s)) ; in a comment (maybe at end of line comment)
368 (> there here)) ; No infinite loops, please.
369 (setq there (1- (nth 8 s)))
370 (setq s (parse-partial-sexp here there)))
371 (setq c-macro-cache-syntactic (point)))
372 (point)))
374 (defun c-no-comment-end-of-macro ()
375 ;; Go to the end of a CPP directive, or a pos just before which isn't in a
376 ;; comment. For this purpose, open strings are ignored.
378 ;; This function must only be called from the beginning of a CPP construct.
380 ;; Note that this function might do hidden buffer changes. See the comment
381 ;; at the start of cc-engine.el for more info.
382 (let* ((here (point))
383 (there (progn (c-end-of-macro) (point)))
385 (if c-macro-cache-no-comment
386 (goto-char c-macro-cache-no-comment)
387 (setq s (parse-partial-sexp here there))
388 (while (and (nth 3 s) ; in a string
389 (> there here)) ; No infinite loops, please.
390 (setq here (1+ (nth 8 s)))
391 (setq s (parse-partial-sexp here there)))
392 (when (nth 4 s)
393 (goto-char (1- (nth 8 s))))
394 (setq c-macro-cache-no-comment (point)))
395 (point)))
397 (defun c-forward-over-cpp-define-id ()
398 ;; Assuming point is at the "#" that introduces a preprocessor
399 ;; directive, it's moved forward to the end of the identifier which is
400 ;; "#define"d (or whatever c-opt-cpp-macro-define specifies). Non-nil
401 ;; is returned in this case, in all other cases nil is returned and
402 ;; point isn't moved.
404 ;; This function might do hidden buffer changes.
405 (when (and c-opt-cpp-macro-define-id
406 (looking-at c-opt-cpp-macro-define-id))
407 (goto-char (match-end 0))))
409 (defun c-forward-to-cpp-define-body ()
410 ;; Assuming point is at the "#" that introduces a preprocessor
411 ;; directive, it's moved forward to the start of the definition body
412 ;; if it's a "#define" (or whatever c-opt-cpp-macro-define
413 ;; specifies). Non-nil is returned in this case, in all other cases
414 ;; nil is returned and point isn't moved.
416 ;; This function might do hidden buffer changes.
417 (when (and c-opt-cpp-macro-define-start
418 (looking-at c-opt-cpp-macro-define-start)
419 (not (= (match-end 0) (c-point 'eol))))
420 (goto-char (match-end 0))))
423 ;;; Basic utility functions.
425 (defun c-delq-from-dotted-list (elt dlist)
426 ;; If ELT is a member of the (possibly dotted) list DLIST, remove all
427 ;; occurrences of it (except for any in the last cdr of DLIST).
429 ;; Call this as (setq DLIST (c-delq-from-dotted-list ELT DLIST)), as
430 ;; sometimes the original structure is changed, sometimes it's not.
432 ;; This function is needed in Emacs < 24.5, and possibly XEmacs, because
433 ;; `delq' throws an error in these versions when given a dotted list.
434 (let ((tail dlist) prev)
435 (while (consp tail)
436 (if (eq (car tail) elt)
437 (if prev
438 (setcdr prev (cdr tail))
439 (setq dlist (cdr dlist)))
440 (setq prev tail))
441 (setq tail (cdr tail)))
442 dlist))
444 (defun c-syntactic-content (from to paren-level)
445 ;; Return the given region as a string where all syntactic
446 ;; whitespace is removed or, where necessary, replaced with a single
447 ;; space. If PAREN-LEVEL is given then all parens in the region are
448 ;; collapsed to "()", "[]" etc.
450 ;; This function might do hidden buffer changes.
452 (save-excursion
453 (save-restriction
454 (narrow-to-region from to)
455 (goto-char from)
456 (let* ((parts (list nil)) (tail parts) pos in-paren)
458 (while (re-search-forward c-syntactic-ws-start to t)
459 (goto-char (setq pos (match-beginning 0)))
460 (c-forward-syntactic-ws)
461 (if (= (point) pos)
462 (forward-char)
464 (when paren-level
465 (save-excursion
466 (setq in-paren (= (car (parse-partial-sexp from pos 1)) 1)
467 pos (point))))
469 (if (and (> pos from)
470 (< (point) to)
471 (looking-at "\\w\\|\\s_")
472 (save-excursion
473 (goto-char (1- pos))
474 (looking-at "\\w\\|\\s_")))
475 (progn
476 (setcdr tail (list (buffer-substring-no-properties from pos)
477 " "))
478 (setq tail (cddr tail)))
479 (setcdr tail (list (buffer-substring-no-properties from pos)))
480 (setq tail (cdr tail)))
482 (when in-paren
483 (when (= (car (parse-partial-sexp pos to -1)) -1)
484 (setcdr tail (list (buffer-substring-no-properties
485 (1- (point)) (point))))
486 (setq tail (cdr tail))))
488 (setq from (point))))
490 (setcdr tail (list (buffer-substring-no-properties from to)))
491 (apply 'concat (cdr parts))))))
493 (defun c-shift-line-indentation (shift-amt)
494 ;; Shift the indentation of the current line with the specified
495 ;; amount (positive inwards). The buffer is modified only if
496 ;; SHIFT-AMT isn't equal to zero.
497 (let ((pos (- (point-max) (point)))
498 (c-macro-start c-macro-start)
499 tmp-char-inserted)
500 (if (zerop shift-amt)
502 ;; If we're on an empty line inside a macro, we take the point
503 ;; to be at the current indentation and shift it to the
504 ;; appropriate column. This way we don't treat the extra
505 ;; whitespace out to the line continuation as indentation.
506 (when (and (c-query-and-set-macro-start)
507 (looking-at "[ \t]*\\\\$")
508 (save-excursion
509 (skip-chars-backward " \t")
510 (bolp)))
511 (insert ?x)
512 (backward-char)
513 (setq tmp-char-inserted t))
514 (unwind-protect
515 (let ((col (current-indentation)))
516 (delete-region (c-point 'bol) (c-point 'boi))
517 (beginning-of-line)
518 (indent-to (+ col shift-amt)))
519 (when tmp-char-inserted
520 (delete-char 1))))
521 ;; If initial point was within line's indentation and we're not on
522 ;; a line with a line continuation in a macro, position after the
523 ;; indentation. Else stay at same point in text.
524 (if (and (< (point) (c-point 'boi))
525 (not tmp-char-inserted))
526 (back-to-indentation)
527 (if (> (- (point-max) pos) (point))
528 (goto-char (- (point-max) pos))))))
530 (defsubst c-keyword-sym (keyword)
531 ;; Return non-nil if the string KEYWORD is a known keyword. More
532 ;; precisely, the value is the symbol for the keyword in
533 ;; `c-keywords-obarray'.
534 (intern-soft keyword c-keywords-obarray))
536 (defsubst c-keyword-member (keyword-sym lang-constant)
537 ;; Return non-nil if the symbol KEYWORD-SYM, as returned by
538 ;; `c-keyword-sym', is a member of LANG-CONSTANT, which is the name
539 ;; of a language constant that ends with "-kwds". If KEYWORD-SYM is
540 ;; nil then the result is nil.
541 (get keyword-sym lang-constant))
543 ;; String syntax chars, suitable for skip-syntax-(forward|backward).
544 (defconst c-string-syntax (if (memq 'gen-string-delim c-emacs-features)
545 "\"|"
546 "\""))
548 ;; Regexp matching string limit syntax.
549 (defconst c-string-limit-regexp (if (memq 'gen-string-delim c-emacs-features)
550 "\\s\"\\|\\s|"
551 "\\s\""))
553 ;; Regexp matching WS followed by string limit syntax.
554 (defconst c-ws*-string-limit-regexp
555 (concat "[ \t]*\\(" c-string-limit-regexp "\\)"))
557 ;; Holds formatted error strings for the few cases where parse errors
558 ;; are reported.
559 (defvar c-parsing-error nil)
560 (make-variable-buffer-local 'c-parsing-error)
562 (defun c-echo-parsing-error (&optional quiet)
563 (when (and c-report-syntactic-errors c-parsing-error (not quiet))
564 (c-benign-error "%s" c-parsing-error))
565 c-parsing-error)
567 ;; Faces given to comments and string literals. This is used in some
568 ;; situations to speed up recognition; it isn't mandatory that font
569 ;; locking is in use. This variable is extended with the face in
570 ;; `c-doc-face-name' when fontification is activated in cc-fonts.el.
571 (defvar c-literal-faces
572 (append '(font-lock-comment-face font-lock-string-face)
573 (when (facep 'font-lock-comment-delimiter-face)
574 ;; New in Emacs 22.
575 '(font-lock-comment-delimiter-face))))
577 (defsubst c-put-c-type-property (pos value)
578 ;; Put a c-type property with the given value at POS.
579 (c-put-char-property pos 'c-type value))
581 (defun c-clear-c-type-property (from to value)
582 ;; Remove all occurrences of the c-type property that has the given
583 ;; value in the region between FROM and TO. VALUE is assumed to not
584 ;; be nil.
586 ;; Note: This assumes that c-type is put on single chars only; it's
587 ;; very inefficient if matching properties cover large regions.
588 (save-excursion
589 (goto-char from)
590 (while (progn
591 (when (eq (get-text-property (point) 'c-type) value)
592 (c-clear-char-property (point) 'c-type))
593 (goto-char (c-next-single-property-change (point) 'c-type nil to))
594 (< (point) to)))))
597 ;; Some debug tools to visualize various special positions. This
598 ;; debug code isn't as portable as the rest of CC Mode.
600 (cc-bytecomp-defun overlays-in)
601 (cc-bytecomp-defun overlay-get)
602 (cc-bytecomp-defun overlay-start)
603 (cc-bytecomp-defun overlay-end)
604 (cc-bytecomp-defun delete-overlay)
605 (cc-bytecomp-defun overlay-put)
606 (cc-bytecomp-defun make-overlay)
608 (defun c-debug-add-face (beg end face)
609 (c-save-buffer-state ((overlays (overlays-in beg end)) overlay)
610 (while overlays
611 (setq overlay (car overlays)
612 overlays (cdr overlays))
613 (when (eq (overlay-get overlay 'face) face)
614 (setq beg (min beg (overlay-start overlay))
615 end (max end (overlay-end overlay)))
616 (delete-overlay overlay)))
617 (overlay-put (make-overlay beg end) 'face face)))
619 (defun c-debug-remove-face (beg end face)
620 (c-save-buffer-state ((overlays (overlays-in beg end)) overlay
621 (ol-beg beg) (ol-end end))
622 (while overlays
623 (setq overlay (car overlays)
624 overlays (cdr overlays))
625 (when (eq (overlay-get overlay 'face) face)
626 (setq ol-beg (min ol-beg (overlay-start overlay))
627 ol-end (max ol-end (overlay-end overlay)))
628 (delete-overlay overlay)))
629 (when (< ol-beg beg)
630 (overlay-put (make-overlay ol-beg beg) 'face face))
631 (when (> ol-end end)
632 (overlay-put (make-overlay end ol-end) 'face face))))
635 ;; `c-beginning-of-statement-1' and accompanying stuff.
637 ;; KLUDGE ALERT: c-maybe-labelp is used to pass information between
638 ;; c-crosses-statement-barrier-p and c-beginning-of-statement-1. A
639 ;; better way should be implemented, but this will at least shut up
640 ;; the byte compiler.
641 (defvar c-maybe-labelp)
643 ;; New awk-compatible version of c-beginning-of-statement-1, ACM 2002/6/22
645 ;; Macros used internally in c-beginning-of-statement-1 for the
646 ;; automaton actions.
647 (defmacro c-bos-push-state ()
648 '(setq stack (cons (cons state saved-pos)
649 stack)))
650 (defmacro c-bos-pop-state (&optional do-if-done)
651 `(if (setq state (car (car stack))
652 saved-pos (cdr (car stack))
653 stack (cdr stack))
655 ,do-if-done
656 (throw 'loop nil)))
657 (defmacro c-bos-pop-state-and-retry ()
658 '(throw 'loop (setq state (car (car stack))
659 saved-pos (cdr (car stack))
660 ;; Throw nil if stack is empty, else throw non-nil.
661 stack (cdr stack))))
662 (defmacro c-bos-save-pos ()
663 '(setq saved-pos (vector pos tok ptok pptok)))
664 (defmacro c-bos-restore-pos ()
665 '(unless (eq (elt saved-pos 0) start)
666 (setq pos (elt saved-pos 0)
667 tok (elt saved-pos 1)
668 ptok (elt saved-pos 2)
669 pptok (elt saved-pos 3))
670 (goto-char pos)
671 (setq sym nil)))
672 (defmacro c-bos-save-error-info (missing got)
673 `(setq saved-pos (vector pos ,missing ,got)))
674 (defmacro c-bos-report-error ()
675 '(unless noerror
676 (setq c-parsing-error
677 (format-message
678 "No matching `%s' found for `%s' on line %d"
679 (elt saved-pos 1)
680 (elt saved-pos 2)
681 (1+ (count-lines (point-min)
682 (c-point 'bol (elt saved-pos 0))))))))
684 (defun c-beginning-of-statement-1 (&optional lim ignore-labels
685 noerror comma-delim)
686 "Move to the start of the current statement or declaration, or to
687 the previous one if already at the beginning of one. Only
688 statements/declarations on the same level are considered, i.e. don't
689 move into or out of sexps (not even normal expression parentheses).
691 If point is already at the earliest statement within braces or parens,
692 this function doesn't move back into any whitespace preceding it; it
693 returns `same' in this case.
695 Stop at statement continuation tokens like \"else\", \"catch\",
696 \"finally\" and the \"while\" in \"do ... while\" if the start point
697 is within the continuation. If starting at such a token, move to the
698 corresponding statement start. If at the beginning of a statement,
699 move to the closest containing statement if there is any. This might
700 also stop at a continuation clause.
702 Labels are treated as part of the following statements if
703 IGNORE-LABELS is non-nil. (FIXME: Doesn't work if we stop at a known
704 statement start keyword.) Otherwise, each label is treated as a
705 separate statement.
707 Macros are ignored \(i.e. skipped over) unless point is within one, in
708 which case the content of the macro is treated as normal code. Aside
709 from any normal statement starts found in it, stop at the first token
710 of the content in the macro, i.e. the expression of an \"#if\" or the
711 start of the definition in a \"#define\". Also stop at start of
712 macros before leaving them.
714 Return:
715 `label' if stopped at a label or \"case...:\" or \"default:\";
716 `same' if stopped at the beginning of the current statement;
717 `up' if stepped to a containing statement;
718 `previous' if stepped to a preceding statement;
719 `beginning' if stepped from a statement continuation clause to
720 its start clause; or
721 `macro' if stepped to a macro start.
722 Note that `same' and not `label' is returned if stopped at the same
723 label without crossing the colon character.
725 LIM may be given to limit the search. If the search hits the limit,
726 point will be left at the closest following token, or at the start
727 position if that is less (`same' is returned in this case).
729 NOERROR turns off error logging to `c-parsing-error'.
731 Normally only `;' and virtual semicolons are considered to delimit
732 statements, but if COMMA-DELIM is non-nil then `,' is treated
733 as a delimiter too.
735 Note that this function might do hidden buffer changes. See the
736 comment at the start of cc-engine.el for more info."
738 ;; The bulk of this function is a pushdown automaton that looks at statement
739 ;; boundaries and the tokens (such as "while") in c-opt-block-stmt-key. Its
740 ;; purpose is to keep track of nested statements, ensuring that such
741 ;; statements are skipped over in their entirety (somewhat akin to what C-M-p
742 ;; does with nested braces/brackets/parentheses).
744 ;; Note: The position of a boundary is the following token.
746 ;; Beginning with the current token (the one following point), move back one
747 ;; sexp at a time (where a sexp is, more or less, either a token or the
748 ;; entire contents of a brace/bracket/paren pair). Each time a statement
749 ;; boundary is crossed or a "while"-like token is found, update the state of
750 ;; the PDA. Stop at the beginning of a statement when the stack (holding
751 ;; nested statement info) is empty and the position has been moved.
753 ;; The following variables constitute the PDA:
755 ;; sym: This is either the "while"-like token (e.g. 'for) we've just
756 ;; scanned back over, 'boundary if we've just gone back over a
757 ;; statement boundary, or nil otherwise.
758 ;; state: takes one of the values (nil else else-boundary while
759 ;; while-boundary catch catch-boundary).
760 ;; nil means "no "while"-like token yet scanned".
761 ;; 'else, for example, means "just gone back over an else".
762 ;; 'else-boundary means "just gone back over a statement boundary
763 ;; immediately after having gone back over an else".
764 ;; saved-pos: A vector of either saved positions (tok ptok pptok, etc.) or
765 ;; of error reporting information.
766 ;; stack: The stack onto which the PDA pushes its state. Each entry
767 ;; consists of a saved value of state and saved-pos. An entry is
768 ;; pushed when we move back over a "continuation" token (e.g. else)
769 ;; and popped when we encounter the corresponding opening token
770 ;; (e.g. if).
773 ;; The following diagram briefly outlines the PDA.
775 ;; Common state:
776 ;; "else": Push state, goto state `else'.
777 ;; "while": Push state, goto state `while'.
778 ;; "catch" or "finally": Push state, goto state `catch'.
779 ;; boundary: Pop state.
780 ;; other: Do nothing special.
782 ;; State `else':
783 ;; boundary: Goto state `else-boundary'.
784 ;; other: Error, pop state, retry token.
786 ;; State `else-boundary':
787 ;; "if": Pop state.
788 ;; boundary: Error, pop state.
789 ;; other: See common state.
791 ;; State `while':
792 ;; boundary: Save position, goto state `while-boundary'.
793 ;; other: Pop state, retry token.
795 ;; State `while-boundary':
796 ;; "do": Pop state.
797 ;; boundary: Restore position if it's not at start, pop state. [*see below]
798 ;; other: See common state.
800 ;; State `catch':
801 ;; boundary: Goto state `catch-boundary'.
802 ;; other: Error, pop state, retry token.
804 ;; State `catch-boundary':
805 ;; "try": Pop state.
806 ;; "catch": Goto state `catch'.
807 ;; boundary: Error, pop state.
808 ;; other: See common state.
810 ;; [*] In the `while-boundary' state, we had pushed a 'while state, and were
811 ;; searching for a "do" which would have opened a do-while. If we didn't
812 ;; find it, we discard the analysis done since the "while", go back to this
813 ;; token in the buffer and restart the scanning there, this time WITHOUT
814 ;; pushing the 'while state onto the stack.
816 ;; In addition to the above there is some special handling of labels
817 ;; and macros.
819 (let ((case-fold-search nil)
820 (start (point))
821 macro-start
822 (delims (if comma-delim '(?\; ?,) '(?\;)))
823 (c-stmt-delim-chars (if comma-delim
824 c-stmt-delim-chars-with-comma
825 c-stmt-delim-chars))
826 c-in-literal-cache c-maybe-labelp after-case:-pos saved
827 ;; Current position.
829 ;; Position of last stmt boundary character (e.g. ;).
830 boundary-pos
831 ;; The position of the last sexp or bound that follows the
832 ;; first found colon, i.e. the start of the nonlabel part of
833 ;; the statement. It's `start' if a colon is found just after
834 ;; the start.
835 after-labels-pos
836 ;; Like `after-labels-pos', but the first such position inside
837 ;; a label, i.e. the start of the last label before the start
838 ;; of the nonlabel part of the statement.
839 last-label-pos
840 ;; The last position where a label is possible provided the
841 ;; statement started there. It's nil as long as no invalid
842 ;; label content has been found (according to
843 ;; `c-nonlabel-token-key'). It's `start' if no valid label
844 ;; content was found in the label. Note that we might still
845 ;; regard it a label if it starts with `c-label-kwds'.
846 label-good-pos
847 ;; Putative positions of the components of a bitfield declaration,
848 ;; e.g. "int foo : NUM_FOO_BITS ;"
849 bitfield-type-pos bitfield-id-pos bitfield-size-pos
850 ;; Symbol just scanned back over (e.g. 'while or 'boundary).
851 ;; See above.
853 ;; Current state in the automaton. See above.
854 state
855 ;; Current saved positions. See above.
856 saved-pos
857 ;; Stack of conses (state . saved-pos).
858 stack
859 ;; Regexp which matches "for", "if", etc.
860 (cond-key (or c-opt-block-stmt-key
861 "\\<\\>")) ; Matches nothing.
862 ;; Return value.
863 (ret 'same)
864 ;; Positions of the last three sexps or bounds we've stopped at.
865 tok ptok pptok)
867 (save-restriction
868 (if lim (narrow-to-region lim (point-max)))
870 (if (save-excursion
871 (and (c-beginning-of-macro)
872 (/= (point) start)))
873 (setq macro-start (point)))
875 ;; Try to skip back over unary operator characters, to register
876 ;; that we've moved.
877 (while (progn
878 (setq pos (point))
879 (c-backward-syntactic-ws)
880 ;; Protect post-++/-- operators just before a virtual semicolon.
881 (and (not (c-at-vsemi-p))
882 (/= (skip-chars-backward "-+!*&~@`#") 0))))
884 ;; Skip back over any semicolon here. If it was a bare semicolon, we're
885 ;; done. Later on we ignore the boundaries for statements that don't
886 ;; contain any sexp. The only thing that is affected is that the error
887 ;; checking is a little less strict, and we really don't bother.
888 (if (and (memq (char-before) delims)
889 (progn (forward-char -1)
890 (setq saved (point))
891 (c-backward-syntactic-ws)
892 (or (memq (char-before) delims)
893 (memq (char-before) '(?: nil))
894 (eq (char-syntax (char-before)) ?\()
895 (c-at-vsemi-p))))
896 (setq ret 'previous
897 pos saved)
899 ;; Begin at start and not pos to detect macros if we stand
900 ;; directly after the #.
901 (goto-char start)
902 (if (looking-at "\\<\\|\\W")
903 ;; Record this as the first token if not starting inside it.
904 (setq tok start))
906 ;; The following while loop goes back one sexp (balanced parens,
907 ;; etc. with contents, or symbol or suchlike) each iteration. This
908 ;; movement is accomplished with a call to c-backward-sexp approx 170
909 ;; lines below.
911 ;; The loop is exited only by throwing nil to the (catch 'loop ...):
912 ;; 1. On reaching the start of a macro;
913 ;; 2. On having passed a stmt boundary with the PDA stack empty;
914 ;; 3. On reaching the start of an Objective C method def;
915 ;; 4. From macro `c-bos-pop-state'; when the stack is empty;
916 ;; 5. From macro `c-bos-pop-state-and-retry' when the stack is empty.
917 (while
918 (catch 'loop ;; Throw nil to break, non-nil to continue.
919 (cond
920 ;; Are we in a macro, just after the opening #?
921 ((save-excursion
922 (and macro-start ; Always NIL for AWK.
923 (progn (skip-chars-backward " \t")
924 (eq (char-before) ?#))
925 (progn (setq saved (1- (point)))
926 (beginning-of-line)
927 (not (eq (char-before (1- (point))) ?\\)))
928 (looking-at c-opt-cpp-start)
929 (progn (skip-chars-forward " \t")
930 (eq (point) saved))))
931 (goto-char saved)
932 (if (and (c-forward-to-cpp-define-body)
933 (progn (c-forward-syntactic-ws start)
934 (< (point) start)))
935 ;; Stop at the first token in the content of the macro.
936 (setq pos (point)
937 ignore-labels t) ; Avoid the label check on exit.
938 (setq pos saved
939 ret 'macro
940 ignore-labels t))
941 (throw 'loop nil)) ; 1. Start of macro.
943 ;; Do a round through the automaton if we've just passed a
944 ;; statement boundary or passed a "while"-like token.
945 ((or sym
946 (and (looking-at cond-key)
947 (setq sym (intern (match-string 1)))))
949 (when (and (< pos start) (null stack))
950 (throw 'loop nil)) ; 2. Statement boundary.
952 ;; The PDA state handling.
954 ;; Refer to the description of the PDA in the opening
955 ;; comments. In the following OR form, the first leaf
956 ;; attempts to handles one of the specific actions detailed
957 ;; (e.g., finding token "if" whilst in state `else-boundary').
958 ;; We drop through to the second leaf (which handles common
959 ;; state) if no specific handler is found in the first cond.
960 ;; If a parsing error is detected (e.g. an "else" with no
961 ;; preceding "if"), we throw to the enclosing catch.
963 ;; Note that the (eq state 'else) means
964 ;; "we've just passed an else", NOT "we're looking for an
965 ;; else".
966 (or (cond
967 ((eq state 'else)
968 (if (eq sym 'boundary)
969 (setq state 'else-boundary)
970 (c-bos-report-error)
971 (c-bos-pop-state-and-retry)))
973 ((eq state 'else-boundary)
974 (cond ((eq sym 'if)
975 (c-bos-pop-state (setq ret 'beginning)))
976 ((eq sym 'boundary)
977 (c-bos-report-error)
978 (c-bos-pop-state))))
980 ((eq state 'while)
981 (if (and (eq sym 'boundary)
982 ;; Since this can cause backtracking we do a
983 ;; little more careful analysis to avoid it:
984 ;; If there's a label in front of the while
985 ;; it can't be part of a do-while.
986 (not after-labels-pos))
987 (progn (c-bos-save-pos)
988 (setq state 'while-boundary))
989 (c-bos-pop-state-and-retry))) ; Can't be a do-while
991 ((eq state 'while-boundary)
992 (cond ((eq sym 'do)
993 (c-bos-pop-state (setq ret 'beginning)))
994 ((eq sym 'boundary) ; isn't a do-while
995 (c-bos-restore-pos) ; the position of the while
996 (c-bos-pop-state)))) ; no longer searching for do.
998 ((eq state 'catch)
999 (if (eq sym 'boundary)
1000 (setq state 'catch-boundary)
1001 (c-bos-report-error)
1002 (c-bos-pop-state-and-retry)))
1004 ((eq state 'catch-boundary)
1005 (cond
1006 ((eq sym 'try)
1007 (c-bos-pop-state (setq ret 'beginning)))
1008 ((eq sym 'catch)
1009 (setq state 'catch))
1010 ((eq sym 'boundary)
1011 (c-bos-report-error)
1012 (c-bos-pop-state)))))
1014 ;; This is state common. We get here when the previous
1015 ;; cond statement found no particular state handler.
1016 (cond ((eq sym 'boundary)
1017 ;; If we have a boundary at the start
1018 ;; position we push a frame to go to the
1019 ;; previous statement.
1020 (if (>= pos start)
1021 (c-bos-push-state)
1022 (c-bos-pop-state)))
1023 ((eq sym 'else)
1024 (c-bos-push-state)
1025 (c-bos-save-error-info 'if 'else)
1026 (setq state 'else))
1027 ((eq sym 'while)
1028 ;; Is this a real while, or a do-while?
1029 ;; The next `when' triggers unless we are SURE that
1030 ;; the `while' is not the tail end of a `do-while'.
1031 (when (or (not pptok)
1032 (memq (char-after pptok) delims)
1033 ;; The following kludge is to prevent
1034 ;; infinite recursion when called from
1035 ;; c-awk-after-if-for-while-condition-p,
1036 ;; or the like.
1037 (and (eq (point) start)
1038 (c-vsemi-status-unknown-p))
1039 (c-at-vsemi-p pptok))
1040 ;; Since this can cause backtracking we do a
1041 ;; little more careful analysis to avoid it: If
1042 ;; the while isn't followed by a (possibly
1043 ;; virtual) semicolon it can't be a do-while.
1044 (c-bos-push-state)
1045 (setq state 'while)))
1046 ((memq sym '(catch finally))
1047 (c-bos-push-state)
1048 (c-bos-save-error-info 'try sym)
1049 (setq state 'catch))))
1051 (when c-maybe-labelp
1052 ;; We're either past a statement boundary or at the
1053 ;; start of a statement, so throw away any label data
1054 ;; for the previous one.
1055 (setq after-labels-pos nil
1056 last-label-pos nil
1057 c-maybe-labelp nil))))
1059 ;; Step to the previous sexp, but not if we crossed a
1060 ;; boundary, since that doesn't consume an sexp.
1061 (if (eq sym 'boundary)
1062 (setq ret 'previous)
1064 ;; HERE IS THE SINGLE PLACE INSIDE THE PDA LOOP WHERE WE MOVE
1065 ;; BACKWARDS THROUGH THE SOURCE.
1067 (c-backward-syntactic-ws)
1068 (let ((before-sws-pos (point))
1069 ;; The end position of the area to search for statement
1070 ;; barriers in this round.
1071 (maybe-after-boundary-pos pos))
1073 ;; Go back over exactly one logical sexp, taking proper
1074 ;; account of macros and escaped EOLs.
1075 (while
1076 (progn
1077 (unless (c-safe (c-backward-sexp) t)
1078 ;; Give up if we hit an unbalanced block. Since the
1079 ;; stack won't be empty the code below will report a
1080 ;; suitable error.
1081 (throw 'loop nil))
1082 (cond
1083 ;; Have we moved into a macro?
1084 ((and (not macro-start)
1085 (c-beginning-of-macro))
1086 ;; Have we crossed a statement boundary? If not,
1087 ;; keep going back until we find one or a "real" sexp.
1088 (and
1089 (save-excursion
1090 (c-end-of-macro)
1091 (not (c-crosses-statement-barrier-p
1092 (point) maybe-after-boundary-pos)))
1093 (setq maybe-after-boundary-pos (point))))
1094 ;; Have we just gone back over an escaped NL? This
1095 ;; doesn't count as a sexp.
1096 ((looking-at "\\\\$")))))
1098 ;; Have we crossed a statement boundary?
1099 (setq boundary-pos
1100 (cond
1101 ;; Are we at a macro beginning?
1102 ((and (not macro-start)
1103 c-opt-cpp-prefix
1104 (looking-at c-opt-cpp-prefix))
1105 (save-excursion
1106 (c-end-of-macro)
1107 (c-crosses-statement-barrier-p
1108 (point) maybe-after-boundary-pos)))
1109 ;; Just gone back over a brace block?
1110 ((and
1111 (eq (char-after) ?{)
1112 (not (c-looking-at-inexpr-block lim nil t))
1113 (save-excursion
1114 (c-backward-token-2 1 t nil)
1115 (not (looking-at "=\\([^=]\\|$\\)"))))
1116 (save-excursion
1117 (c-forward-sexp) (point)))
1118 ;; Just gone back over some paren block?
1119 ((looking-at "\\s(")
1120 (save-excursion
1121 (goto-char (1+ (c-down-list-backward
1122 before-sws-pos)))
1123 (c-crosses-statement-barrier-p
1124 (point) maybe-after-boundary-pos)))
1125 ;; Just gone back over an ordinary symbol of some sort?
1126 (t (c-crosses-statement-barrier-p
1127 (point) maybe-after-boundary-pos))))
1129 (when boundary-pos
1130 (setq pptok ptok
1131 ptok tok
1132 tok boundary-pos
1133 sym 'boundary)
1134 ;; Like a C "continue". Analyze the next sexp.
1135 (throw 'loop t))))
1137 ;; ObjC method def?
1138 (when (and c-opt-method-key
1139 (setq saved (c-in-method-def-p)))
1140 (setq pos saved
1141 ignore-labels t) ; Avoid the label check on exit.
1142 (throw 'loop nil)) ; 3. ObjC method def.
1144 ;; Might we have a bitfield declaration, "<type> <id> : <size>"?
1145 (if c-has-bitfields
1146 (cond
1147 ;; The : <size> and <id> fields?
1148 ((and (numberp c-maybe-labelp)
1149 (not bitfield-size-pos)
1150 (save-excursion
1151 (goto-char (or tok start))
1152 (not (looking-at c-keywords-regexp)))
1153 (not (looking-at c-keywords-regexp))
1154 (not (c-punctuation-in (point) c-maybe-labelp)))
1155 (setq bitfield-size-pos (or tok start)
1156 bitfield-id-pos (point)))
1157 ;; The <type> field?
1158 ((and bitfield-id-pos
1159 (not bitfield-type-pos))
1160 (if (and (looking-at c-symbol-key) ; Can only be an integer type. :-)
1161 (not (looking-at c-not-primitive-type-keywords-regexp))
1162 (not (c-punctuation-in (point) tok)))
1163 (setq bitfield-type-pos (point))
1164 (setq bitfield-size-pos nil
1165 bitfield-id-pos nil)))))
1167 ;; Handle labels.
1168 (unless (eq ignore-labels t)
1169 (when (numberp c-maybe-labelp)
1170 ;; `c-crosses-statement-barrier-p' has found a colon, so we
1171 ;; might be in a label now. Have we got a real label
1172 ;; (including a case label) or something like C++'s "public:"?
1173 ;; A case label might use an expression rather than a token.
1174 (setq after-case:-pos (or tok start))
1175 (if (or (looking-at c-nonlabel-token-key) ; e.g. "while" or "'a'"
1176 ;; Catch C++'s inheritance construct "class foo : bar".
1177 (save-excursion
1178 (and
1179 (c-safe (c-backward-sexp) t)
1180 (looking-at c-nonlabel-token-2-key))))
1181 (setq c-maybe-labelp nil)
1182 (if after-labels-pos ; Have we already encountered a label?
1183 (if (not last-label-pos)
1184 (setq last-label-pos (or tok start)))
1185 (setq after-labels-pos (or tok start)))
1186 (setq c-maybe-labelp t
1187 label-good-pos nil))) ; bogus "label"
1189 (when (and (not label-good-pos) ; i.e. no invalid "label"'s yet
1190 ; been found.
1191 (looking-at c-nonlabel-token-key)) ; e.g. "while :"
1192 ;; We're in a potential label and it's the first
1193 ;; time we've found something that isn't allowed in
1194 ;; one.
1195 (setq label-good-pos (or tok start))))
1197 ;; We've moved back by a sexp, so update the token positions.
1198 (setq sym nil
1199 pptok ptok
1200 ptok tok
1201 tok (point)
1202 pos tok) ; always non-nil
1203 ) ; end of (catch loop ....)
1204 ) ; end of sexp-at-a-time (while ....)
1206 ;; If the stack isn't empty there might be errors to report.
1207 (while stack
1208 (if (and (vectorp saved-pos) (eq (length saved-pos) 3))
1209 (c-bos-report-error))
1210 (setq saved-pos (cdr (car stack))
1211 stack (cdr stack)))
1213 (when (and (eq ret 'same)
1214 (not (memq sym '(boundary ignore nil))))
1215 ;; Need to investigate closer whether we've crossed
1216 ;; between a substatement and its containing statement.
1217 (if (setq saved
1218 (cond ((and (looking-at c-block-stmt-1-2-key)
1219 (eq (char-after ptok) ?\())
1220 pptok)
1221 ((looking-at c-block-stmt-1-key)
1222 ptok)
1223 (t pptok)))
1224 (cond ((> start saved) (setq pos saved))
1225 ((= start saved) (setq ret 'up)))))
1227 (when (and (not ignore-labels)
1228 (eq c-maybe-labelp t)
1229 (not (eq ret 'beginning))
1230 after-labels-pos
1231 (not bitfield-type-pos) ; Bitfields take precedence over labels.
1232 (or (not label-good-pos)
1233 (<= label-good-pos pos)
1234 (progn
1235 (goto-char (if (and last-label-pos
1236 (< last-label-pos start))
1237 last-label-pos
1238 pos))
1239 (looking-at c-label-kwds-regexp))))
1240 ;; We're in a label. Maybe we should step to the statement
1241 ;; after it.
1242 (if (< after-labels-pos start)
1243 (setq pos after-labels-pos)
1244 (setq ret 'label)
1245 (if (and last-label-pos (< last-label-pos start))
1246 ;; Might have jumped over several labels. Go to the last one.
1247 (setq pos last-label-pos)))))
1249 ;; Have we got "case <expression>:"?
1250 (goto-char pos)
1251 (when (and after-case:-pos
1252 (not (eq ret 'beginning))
1253 (looking-at c-case-kwds-regexp))
1254 (if (< after-case:-pos start)
1255 (setq pos after-case:-pos))
1256 (if (eq ret 'same)
1257 (setq ret 'label)))
1259 ;; Skip over the unary operators that can start the statement.
1260 (while (progn
1261 (c-backward-syntactic-ws)
1262 ;; protect AWK post-inc/decrement operators, etc.
1263 (and (not (c-at-vsemi-p (point)))
1264 (/= (skip-chars-backward "-+!*&~@`#") 0)))
1265 (setq pos (point)))
1266 (goto-char pos)
1267 ret)))
1269 (defun c-punctuation-in (from to)
1270 "Return non-nil if there is a non-comment non-macro punctuation character
1271 between FROM and TO. FROM must not be in a string or comment. The returned
1272 value is the position of the first such character."
1273 (save-excursion
1274 (goto-char from)
1275 (let ((pos (point)))
1276 (while (progn (skip-chars-forward c-symbol-chars to)
1277 (c-forward-syntactic-ws to)
1278 (> (point) pos))
1279 (setq pos (point))))
1280 (and (< (point) to) (point))))
1282 (defun c-crosses-statement-barrier-p (from to)
1283 "Return non-nil if buffer positions FROM to TO cross one or more
1284 statement or declaration boundaries. The returned value is actually
1285 the position of the earliest boundary char. FROM must not be within
1286 a string or comment.
1288 The variable `c-maybe-labelp' is set to the position of the first `:' that
1289 might start a label (i.e. not part of `::' and not preceded by `?'). If a
1290 single `?' is found, then `c-maybe-labelp' is cleared.
1292 For AWK, a statement which is terminated by an EOL (not a ; or a }) is
1293 regarded as having a \"virtual semicolon\" immediately after the last token on
1294 the line. If this virtual semicolon is _at_ from, the function recognizes it.
1296 Note that this function might do hidden buffer changes. See the
1297 comment at the start of cc-engine.el for more info."
1298 (let* ((skip-chars
1299 ;; If the current language has CPP macros, insert # into skip-chars.
1300 (if c-opt-cpp-symbol
1301 (concat (substring c-stmt-delim-chars 0 1) ; "^"
1302 c-opt-cpp-symbol ; usually "#"
1303 (substring c-stmt-delim-chars 1)) ; e.g. ";{}?:"
1304 c-stmt-delim-chars))
1305 (non-skip-list
1306 (append (substring skip-chars 1) nil)) ; e.g. (?# ?\; ?{ ?} ?? ?:)
1307 lit-range lit-start vsemi-pos)
1308 (save-restriction
1309 (widen)
1310 (save-excursion
1311 (catch 'done
1312 (goto-char from)
1313 (while (progn (skip-chars-forward
1314 skip-chars
1315 (min to (c-point 'bonl)))
1316 (< (point) to))
1317 (cond
1318 ;; Virtual semicolon?
1319 ((and (bolp)
1320 (save-excursion
1321 (progn
1322 (if (setq lit-start (c-literal-start from)) ; Have we landed in a string/comment?
1323 (goto-char lit-start))
1324 (c-backward-syntactic-ws) ; ? put a limit here, maybe?
1325 (setq vsemi-pos (point))
1326 (c-at-vsemi-p))))
1327 (throw 'done vsemi-pos))
1328 ;; In a string/comment?
1329 ((setq lit-range (c-literal-limits from))
1330 (goto-char (cdr lit-range)))
1331 ((eq (char-after) ?:)
1332 (forward-char)
1333 (if (and (eq (char-after) ?:)
1334 (< (point) to))
1335 ;; Ignore scope operators.
1336 (forward-char)
1337 (setq c-maybe-labelp (1- (point)))))
1338 ((eq (char-after) ??)
1339 ;; A question mark. Can't be a label, so stop
1340 ;; looking for more : and ?.
1341 (setq c-maybe-labelp nil
1342 skip-chars (substring c-stmt-delim-chars 0 -2)))
1343 ;; At a CPP construct or a "#" or "##" operator?
1344 ((and c-opt-cpp-symbol (looking-at c-opt-cpp-symbol))
1345 (if (save-excursion
1346 (skip-chars-backward " \t")
1347 (and (bolp)
1348 (or (bobp)
1349 (not (eq (char-before (1- (point))) ?\\)))))
1350 (c-end-of-macro)
1351 (skip-chars-forward c-opt-cpp-symbol)))
1352 ((memq (char-after) non-skip-list)
1353 (throw 'done (point)))))
1354 ;; In trailing space after an as yet undetected virtual semicolon?
1355 (c-backward-syntactic-ws from)
1356 (when (and (bolp) (not (bobp))) ; Can happen in AWK Mode with an
1357 ; unterminated string/regexp.
1358 (backward-char))
1359 (if (and (< (point) to)
1360 (c-at-vsemi-p))
1361 (point)
1362 nil))))))
1364 (defun c-at-statement-start-p ()
1365 "Return non-nil if the point is at the first token in a statement
1366 or somewhere in the syntactic whitespace before it.
1368 A \"statement\" here is not restricted to those inside code blocks.
1369 Any kind of declaration-like construct that occur outside function
1370 bodies is also considered a \"statement\".
1372 Note that this function might do hidden buffer changes. See the
1373 comment at the start of cc-engine.el for more info."
1375 (save-excursion
1376 (let ((end (point))
1377 c-maybe-labelp)
1378 (c-syntactic-skip-backward (substring c-stmt-delim-chars 1) nil t)
1379 (or (bobp)
1380 (eq (char-before) ?})
1381 (and (eq (char-before) ?{)
1382 (not (and c-special-brace-lists
1383 (progn (backward-char)
1384 (c-looking-at-special-brace-list)))))
1385 (c-crosses-statement-barrier-p (point) end)))))
1387 (defun c-at-expression-start-p ()
1388 "Return non-nil if the point is at the first token in an expression or
1389 statement, or somewhere in the syntactic whitespace before it.
1391 An \"expression\" here is a bit different from the normal language
1392 grammar sense: It's any sequence of expression tokens except commas,
1393 unless they are enclosed inside parentheses of some kind. Also, an
1394 expression never continues past an enclosing parenthesis, but it might
1395 contain parenthesis pairs of any sort except braces.
1397 Since expressions never cross statement boundaries, this function also
1398 recognizes statement beginnings, just like `c-at-statement-start-p'.
1400 Note that this function might do hidden buffer changes. See the
1401 comment at the start of cc-engine.el for more info."
1403 (save-excursion
1404 (let ((end (point))
1405 (c-stmt-delim-chars c-stmt-delim-chars-with-comma)
1406 c-maybe-labelp)
1407 (c-syntactic-skip-backward (substring c-stmt-delim-chars 1) nil t)
1408 (or (bobp)
1409 (memq (char-before) '(?{ ?}))
1410 (save-excursion (backward-char)
1411 (looking-at "\\s("))
1412 (c-crosses-statement-barrier-p (point) end)))))
1415 ;; A set of functions that covers various idiosyncrasies in
1416 ;; implementations of `forward-comment'.
1418 ;; Note: Some emacsen considers incorrectly that any line comment
1419 ;; ending with a backslash continues to the next line. I can't think
1420 ;; of any way to work around that in a reliable way without changing
1421 ;; the buffer, though. Suggestions welcome. ;) (No, temporarily
1422 ;; changing the syntax for backslash doesn't work since we must treat
1423 ;; escapes in string literals correctly.)
1425 (defun c-forward-single-comment ()
1426 "Move forward past whitespace and the closest following comment, if any.
1427 Return t if a comment was found, nil otherwise. In either case, the
1428 point is moved past the following whitespace. Line continuations,
1429 i.e. a backslashes followed by line breaks, are treated as whitespace.
1430 The line breaks that end line comments are considered to be the
1431 comment enders, so the point will be put on the beginning of the next
1432 line if it moved past a line comment.
1434 This function does not do any hidden buffer changes."
1436 (let ((start (point)))
1437 (when (looking-at "\\([ \t\n\r\f\v]\\|\\\\[\n\r]\\)+")
1438 (goto-char (match-end 0)))
1440 (when (forward-comment 1)
1441 (if (eobp)
1442 ;; Some emacsen (e.g. XEmacs 21) return t when moving
1443 ;; forwards at eob.
1446 ;; Emacs includes the ending newline in a b-style (c++)
1447 ;; comment, but XEmacs doesn't. We depend on the Emacs
1448 ;; behavior (which also is symmetric).
1449 (if (and (eolp) (elt (parse-partial-sexp start (point)) 7))
1450 (condition-case nil (forward-char 1)))
1452 t))))
1454 (defsubst c-forward-comments ()
1455 "Move forward past all following whitespace and comments.
1456 Line continuations, i.e. a backslashes followed by line breaks, are
1457 treated as whitespace.
1459 Note that this function might do hidden buffer changes. See the
1460 comment at the start of cc-engine.el for more info."
1462 (while (or
1463 ;; If forward-comment in at least XEmacs 21 is given a large
1464 ;; positive value, it'll loop all the way through if it hits
1465 ;; eob.
1466 (and (forward-comment 5)
1467 ;; Some emacsen (e.g. XEmacs 21) return t when moving
1468 ;; forwards at eob.
1469 (not (eobp)))
1471 (when (looking-at "\\\\[\n\r]")
1472 (forward-char 2)
1473 t))))
1475 (defun c-backward-single-comment ()
1476 "Move backward past whitespace and the closest preceding comment, if any.
1477 Return t if a comment was found, nil otherwise. In either case, the
1478 point is moved past the preceding whitespace. Line continuations,
1479 i.e. a backslashes followed by line breaks, are treated as whitespace.
1480 The line breaks that end line comments are considered to be the
1481 comment enders, so the point cannot be at the end of the same line to
1482 move over a line comment.
1484 This function does not do any hidden buffer changes."
1486 (let ((start (point)))
1487 ;; When we got newline terminated comments, forward-comment in all
1488 ;; supported emacsen so far will stop at eol of each line not
1489 ;; ending with a comment when moving backwards. This corrects for
1490 ;; that, and at the same time handles line continuations.
1491 (while (progn
1492 (skip-chars-backward " \t\n\r\f\v")
1493 (and (looking-at "[\n\r]")
1494 (eq (char-before) ?\\)))
1495 (backward-char))
1497 (if (bobp)
1498 ;; Some emacsen (e.g. Emacs 19.34) return t when moving
1499 ;; backwards at bob.
1502 ;; Leave point after the closest following newline if we've
1503 ;; backed up over any above, since forward-comment won't move
1504 ;; backward over a line comment if point is at the end of the
1505 ;; same line.
1506 (re-search-forward "\\=\\s *[\n\r]" start t)
1508 (if (if (forward-comment -1)
1509 (if (eolp)
1510 ;; If forward-comment above succeeded and we're at eol
1511 ;; then the newline we moved over above didn't end a
1512 ;; line comment, so we give it another go.
1513 (forward-comment -1)
1516 ;; Emacs <= 20 and XEmacs move back over the closer of a
1517 ;; block comment that lacks an opener.
1518 (if (looking-at "\\*/")
1519 (progn (forward-char 2) nil)
1520 t)))))
1522 (defsubst c-backward-comments ()
1523 "Move backward past all preceding whitespace and comments.
1524 Line continuations, i.e. a backslashes followed by line breaks, are
1525 treated as whitespace. The line breaks that end line comments are
1526 considered to be the comment enders, so the point cannot be at the end
1527 of the same line to move over a line comment. Unlike
1528 c-backward-syntactic-ws, this function doesn't move back over
1529 preprocessor directives.
1531 Note that this function might do hidden buffer changes. See the
1532 comment at the start of cc-engine.el for more info."
1534 (let ((start (point)))
1535 (while (and
1536 ;; `forward-comment' in some emacsen (e.g. XEmacs 21.4)
1537 ;; return t when moving backwards at bob.
1538 (not (bobp))
1540 (if (let (moved-comment)
1541 (while
1542 (and (not (setq moved-comment (forward-comment -1)))
1543 ;; Cope specifically with ^M^J here -
1544 ;; forward-comment sometimes gets stuck after ^Ms,
1545 ;; sometimes after ^M^J.
1547 (when (eq (char-before) ?\r)
1548 (backward-char)
1550 (when (and (eq (char-before) ?\n)
1551 (eq (char-before (1- (point))) ?\r))
1552 (backward-char 2)
1553 t))))
1554 moved-comment)
1555 (if (looking-at "\\*/")
1556 ;; Emacs <= 20 and XEmacs move back over the
1557 ;; closer of a block comment that lacks an opener.
1558 (progn (forward-char 2) nil)
1561 ;; XEmacs treats line continuations as whitespace but
1562 ;; only in the backward direction, which seems a bit
1563 ;; odd. Anyway, this is necessary for Emacs.
1564 (when (and (looking-at "[\n\r]")
1565 (eq (char-before) ?\\)
1566 (< (point) start))
1567 (backward-char)
1568 t))))))
1571 ;; Tools for skipping over syntactic whitespace.
1573 ;; The following functions use text properties to cache searches over
1574 ;; large regions of syntactic whitespace. It works as follows:
1576 ;; o If a syntactic whitespace region contains anything but simple
1577 ;; whitespace (i.e. space, tab and line breaks), the text property
1578 ;; `c-in-sws' is put over it. At places where we have stopped
1579 ;; within that region there's also a `c-is-sws' text property.
1580 ;; That since there typically are nested whitespace inside that
1581 ;; must be handled separately, e.g. whitespace inside a comment or
1582 ;; cpp directive. Thus, from one point with `c-is-sws' it's safe
1583 ;; to jump to another point with that property within the same
1584 ;; `c-in-sws' region. It can be likened to a ladder where
1585 ;; `c-in-sws' marks the bars and `c-is-sws' the rungs.
1587 ;; o The `c-is-sws' property is put on the simple whitespace chars at
1588 ;; a "rung position" and also maybe on the first following char.
1589 ;; As many characters as can be conveniently found in this range
1590 ;; are marked, but no assumption can be made that the whole range
1591 ;; is marked (it could be clobbered by later changes, for
1592 ;; instance).
1594 ;; Note that some part of the beginning of a sequence of simple
1595 ;; whitespace might be part of the end of a preceding line comment
1596 ;; or cpp directive and must not be considered part of the "rung".
1597 ;; Such whitespace is some amount of horizontal whitespace followed
1598 ;; by a newline. In the case of cpp directives it could also be
1599 ;; two newlines with horizontal whitespace between them.
1601 ;; The reason to include the first following char is to cope with
1602 ;; "rung positions" that don't have any ordinary whitespace. If
1603 ;; `c-is-sws' is put on a token character it does not have
1604 ;; `c-in-sws' set simultaneously. That's the only case when that
1605 ;; can occur, and the reason for not extending the `c-in-sws'
1606 ;; region to cover it is that the `c-in-sws' region could then be
1607 ;; accidentally merged with a following one if the token is only
1608 ;; one character long.
1610 ;; o On buffer changes the `c-in-sws' and `c-is-sws' properties are
1611 ;; removed in the changed region. If the change was inside
1612 ;; syntactic whitespace that means that the "ladder" is broken, but
1613 ;; a later call to `c-forward-sws' or `c-backward-sws' will use the
1614 ;; parts on either side and use an ordinary search only to "repair"
1615 ;; the gap.
1617 ;; Special care needs to be taken if a region is removed: If there
1618 ;; are `c-in-sws' on both sides of it which do not connect inside
1619 ;; the region then they can't be joined. If e.g. a marked macro is
1620 ;; broken, syntactic whitespace inside the new text might be
1621 ;; marked. If those marks would become connected with the old
1622 ;; `c-in-sws' range around the macro then we could get a ladder
1623 ;; with one end outside the macro and the other at some whitespace
1624 ;; within it.
1626 ;; The main motivation for this system is to increase the speed in
1627 ;; skipping over the large whitespace regions that can occur at the
1628 ;; top level in e.g. header files that contain a lot of comments and
1629 ;; cpp directives. For small comments inside code it's probably
1630 ;; slower than using `forward-comment' straightforwardly, but speed is
1631 ;; not a significant factor there anyway.
1633 ; (defface c-debug-is-sws-face
1634 ; '((t (:background "GreenYellow")))
1635 ; "Debug face to mark the `c-is-sws' property.")
1636 ; (defface c-debug-in-sws-face
1637 ; '((t (:underline t)))
1638 ; "Debug face to mark the `c-in-sws' property.")
1640 ; (defun c-debug-put-sws-faces ()
1641 ; ;; Put the sws debug faces on all the `c-is-sws' and `c-in-sws'
1642 ; ;; properties in the buffer.
1643 ; (interactive)
1644 ; (save-excursion
1645 ; (c-save-buffer-state (in-face)
1646 ; (goto-char (point-min))
1647 ; (setq in-face (if (get-text-property (point) 'c-is-sws)
1648 ; (point)))
1649 ; (while (progn
1650 ; (goto-char (next-single-property-change
1651 ; (point) 'c-is-sws nil (point-max)))
1652 ; (if in-face
1653 ; (progn
1654 ; (c-debug-add-face in-face (point) 'c-debug-is-sws-face)
1655 ; (setq in-face nil))
1656 ; (setq in-face (point)))
1657 ; (not (eobp))))
1658 ; (goto-char (point-min))
1659 ; (setq in-face (if (get-text-property (point) 'c-in-sws)
1660 ; (point)))
1661 ; (while (progn
1662 ; (goto-char (next-single-property-change
1663 ; (point) 'c-in-sws nil (point-max)))
1664 ; (if in-face
1665 ; (progn
1666 ; (c-debug-add-face in-face (point) 'c-debug-in-sws-face)
1667 ; (setq in-face nil))
1668 ; (setq in-face (point)))
1669 ; (not (eobp)))))))
1671 (defmacro c-debug-sws-msg (&rest args)
1672 ;;`(message ,@args)
1675 (defmacro c-put-is-sws (beg end)
1676 ;; This macro does a hidden buffer change.
1677 `(let ((beg ,beg) (end ,end))
1678 (put-text-property beg end 'c-is-sws t)
1679 ,@(when (facep 'c-debug-is-sws-face)
1680 `((c-debug-add-face beg end 'c-debug-is-sws-face)))))
1682 (defmacro c-put-in-sws (beg end)
1683 ;; This macro does a hidden buffer change.
1684 `(let ((beg ,beg) (end ,end))
1685 (put-text-property beg end 'c-in-sws t)
1686 ,@(when (facep 'c-debug-is-sws-face)
1687 `((c-debug-add-face beg end 'c-debug-in-sws-face)))))
1689 (defmacro c-remove-is-sws (beg end)
1690 ;; This macro does a hidden buffer change.
1691 `(let ((beg ,beg) (end ,end))
1692 (remove-text-properties beg end '(c-is-sws nil))
1693 ,@(when (facep 'c-debug-is-sws-face)
1694 `((c-debug-remove-face beg end 'c-debug-is-sws-face)))))
1696 (defmacro c-remove-in-sws (beg end)
1697 ;; This macro does a hidden buffer change.
1698 `(let ((beg ,beg) (end ,end))
1699 (remove-text-properties beg end '(c-in-sws nil))
1700 ,@(when (facep 'c-debug-is-sws-face)
1701 `((c-debug-remove-face beg end 'c-debug-in-sws-face)))))
1703 (defmacro c-remove-is-and-in-sws (beg end)
1704 ;; This macro does a hidden buffer change.
1705 `(let ((beg ,beg) (end ,end))
1706 (remove-text-properties beg end '(c-is-sws nil c-in-sws nil))
1707 ,@(when (facep 'c-debug-is-sws-face)
1708 `((c-debug-remove-face beg end 'c-debug-is-sws-face)
1709 (c-debug-remove-face beg end 'c-debug-in-sws-face)))))
1711 ;; The type of literal position `end' is in in a `before-change-functions'
1712 ;; function - one of `c', `c++', `pound', or nil (but NOT `string').
1713 (defvar c-sws-lit-type nil)
1714 ;; A cons (START . STOP) of the bounds of the comment or CPP construct
1715 ;; enclosing END, if any, else nil.
1716 (defvar c-sws-lit-limits nil)
1718 (defun c-invalidate-sws-region-before (end)
1719 ;; Called from c-before-change. END is the end of the change region, the
1720 ;; standard parameter given to all before-change-functions.
1722 ;; Note whether END is inside a comment or CPP construct, and if so note its
1723 ;; bounds in `c-sws-lit-limits' and type in `c-sws-lit-type'.
1724 (save-excursion
1725 (goto-char end)
1726 (let* ((limits (c-literal-limits))
1727 (lit-type (c-literal-type limits)))
1728 (cond
1729 ((memq lit-type '(c c++))
1730 (setq c-sws-lit-type lit-type
1731 c-sws-lit-limits limits))
1732 ((c-beginning-of-macro)
1733 (setq c-sws-lit-type 'pound
1734 c-sws-lit-limits (cons (point)
1735 (progn (c-end-of-macro) (point)))))
1736 (t (setq c-sws-lit-type nil
1737 c-sws-lit-limits nil))))))
1739 (defun c-invalidate-sws-region-after-del (beg end old-len)
1740 ;; Text has been deleted, OLD-LEN characters of it starting from position
1741 ;; BEG. END is typically eq to BEG. Should there have been a comment or
1742 ;; CPP construct open at END before the deletion, check whether this
1743 ;; deletion deleted or "damaged" its opening delimiter. If so, return the
1744 ;; current position of where the construct ended, otherwise return nil.
1745 (when c-sws-lit-limits
1746 (setcdr c-sws-lit-limits (- (cdr c-sws-lit-limits) old-len))
1747 (if (and (< beg (+ (car c-sws-lit-limits) 2)) ; A lazy assumption that
1748 ; comment delimiters are 2
1749 ; chars long.
1750 (or (get-text-property end 'c-in-sws)
1751 (next-single-property-change end 'c-in-sws nil
1752 (cdr c-sws-lit-limits))
1753 (get-text-property end 'c-is-sws)
1754 (next-single-property-change end 'c-is-sws nil
1755 (cdr c-sws-lit-limits))))
1756 (cdr c-sws-lit-limits))))
1758 (defun c-invalidate-sws-region-after-ins (end)
1759 ;; Text has been inserted, ending at buffer position END. Should there be a
1760 ;; literal or CPP construct open at END, check whether there are `c-in-sws'
1761 ;; or `c-is-sws' text properties inside this literal. If there are, return
1762 ;; the buffer position of the end of the literal, else return nil.
1763 (save-excursion
1764 (let* ((limits (c-literal-limits))
1765 (lit-type (c-literal-type limits)))
1766 (goto-char end)
1767 (when (and (not (memq lit-type '(c c++)))
1768 (c-beginning-of-macro))
1769 (setq lit-type 'pound
1770 limits (cons (point)
1771 (progn (c-end-of-macro) (point)))))
1772 (when (memq lit-type '(c c++ pound))
1773 (let ((next-in (next-single-property-change (car limits) 'c-in-sws
1774 nil (cdr limits)))
1775 (next-is (next-single-property-change (car limits) 'c-is-sws
1776 nil (cdr limits))))
1777 (and (or next-in next-is)
1778 (cdr limits)))))))
1780 (defun c-invalidate-sws-region-after (beg end old-len)
1781 ;; Called from `after-change-functions'. Remove any stale `c-in-sws' or
1782 ;; `c-is-sws' text properties from the vicinity of the change. BEG, END,
1783 ;; and OLD-LEN are the standard arguments given to after-change functions.
1785 ;; Note that if `c-forward-sws' or `c-backward-sws' are used outside
1786 ;; `c-save-buffer-state' or similar then this will remove the cache
1787 ;; properties right after they're added.
1789 ;; This function does hidden buffer changes.
1790 (let ((del-end
1791 (and (> old-len 0)
1792 (c-invalidate-sws-region-after-del beg end old-len)))
1793 (ins-end
1794 (and (> end beg)
1795 (c-invalidate-sws-region-after-ins end))))
1796 (save-excursion
1797 ;; Adjust the end to remove the properties in any following simple
1798 ;; ws up to and including the next line break, if there is any
1799 ;; after the changed region. This is necessary e.g. when a rung
1800 ;; marked empty line is converted to a line comment by inserting
1801 ;; "//" before the line break. In that case the line break would
1802 ;; keep the rung mark which could make a later `c-backward-sws'
1803 ;; move into the line comment instead of over it.
1804 (goto-char end)
1805 (skip-chars-forward " \t\f\v")
1806 (when (and (eolp) (not (eobp)))
1807 (setq end (1+ (point)))))
1809 (when (and (= beg end)
1810 (get-text-property beg 'c-in-sws)
1811 (> beg (point-min))
1812 (get-text-property (1- beg) 'c-in-sws))
1813 ;; Ensure that an `c-in-sws' range gets broken. Note that it isn't
1814 ;; safe to keep a range that was continuous before the change. E.g:
1816 ;; #define foo
1817 ;; \
1818 ;; bar
1820 ;; There can be a "ladder" between "#" and "b". Now, if the newline
1821 ;; after "foo" is removed then "bar" will become part of the cpp
1822 ;; directive instead of a syntactically relevant token. In that
1823 ;; case there's no longer syntactic ws from "#" to "b".
1824 (setq beg (1- beg)))
1826 (setq end (max (or del-end end)
1827 (or ins-end end)
1828 end))
1830 (c-debug-sws-msg "c-invalidate-sws-region-after [%s..%s]" beg end)
1831 (c-remove-is-and-in-sws beg end)))
1833 (defun c-forward-sws ()
1834 ;; Used by `c-forward-syntactic-ws' to implement the unbounded search.
1836 ;; This function might do hidden buffer changes.
1838 (let (;; `rung-pos' is set to a position as early as possible in the
1839 ;; unmarked part of the simple ws region.
1840 (rung-pos (point)) next-rung-pos rung-end-pos last-put-in-sws-pos
1841 rung-is-marked next-rung-is-marked simple-ws-end
1842 ;; `safe-start' is set when it's safe to cache the start position.
1843 ;; It's not set if we've initially skipped over comments and line
1844 ;; continuations since we might have gone out through the end of a
1845 ;; macro then. This provision makes `c-forward-sws' not populate the
1846 ;; cache in the majority of cases, but otoh is `c-backward-sws' by far
1847 ;; more common.
1848 safe-start)
1850 ;; Skip simple ws and do a quick check on the following character to see
1851 ;; if it's anything that can't start syntactic ws, so we can bail out
1852 ;; early in the majority of cases when there just are a few ws chars.
1853 (skip-chars-forward " \t\n\r\f\v")
1854 (when (or (looking-at c-syntactic-ws-start)
1855 (and c-opt-cpp-prefix
1856 (looking-at c-noise-macro-name-re)))
1858 (setq rung-end-pos (min (1+ (point)) (point-max)))
1859 (if (setq rung-is-marked (text-property-any rung-pos rung-end-pos
1860 'c-is-sws t))
1861 ;; Find the last rung position to avoid setting properties in all
1862 ;; the cases when the marked rung is complete.
1863 ;; (`next-single-property-change' is certain to move at least one
1864 ;; step forward.)
1865 (setq rung-pos (1- (c-next-single-property-change
1866 rung-is-marked 'c-is-sws nil rung-end-pos)))
1867 ;; Got no marked rung here. Since the simple ws might have started
1868 ;; inside a line comment or cpp directive we must set `rung-pos' as
1869 ;; high as possible.
1870 (setq rung-pos (point)))
1872 (with-silent-modifications
1873 (while
1874 (progn
1875 ;; In the following while form, we move over a "ladder" and
1876 ;; following simple WS each time round the loop, appending the WS
1877 ;; onto the ladder, joining adjacent ladders, and terminating when
1878 ;; there is no more WS or we reach EOB.
1879 (while
1880 (when (and rung-is-marked
1881 (get-text-property (point) 'c-in-sws))
1883 ;; The following search is the main reason that `c-in-sws'
1884 ;; and `c-is-sws' aren't combined to one property.
1885 (goto-char (c-next-single-property-change
1886 (point) 'c-in-sws nil (point-max)))
1887 (unless (get-text-property (point) 'c-is-sws)
1888 ;; If the `c-in-sws' region extended past the last
1889 ;; `c-is-sws' char we have to go back a bit.
1890 (or (get-text-property (1- (point)) 'c-is-sws)
1891 (goto-char (previous-single-property-change
1892 (point) 'c-is-sws)))
1893 (backward-char))
1895 (c-debug-sws-msg
1896 "c-forward-sws cached move %s -> %s (max %s)"
1897 rung-pos (point) (point-max))
1899 (setq rung-pos (point))
1900 (and (> (skip-chars-forward " \t\n\r\f\v") 0)
1901 (not (eobp))))
1903 ;; We'll loop here if there is simple ws after the last rung.
1904 ;; That means that there's been some change in it and it's
1905 ;; possible that we've stepped into another ladder, so extend
1906 ;; the previous one to join with it if there is one, and try to
1907 ;; use the cache again.
1908 (c-debug-sws-msg
1909 "c-forward-sws extending rung with [%s..%s] (max %s)"
1910 (1+ rung-pos) (1+ (point)) (point-max))
1911 (unless (get-text-property (point) 'c-is-sws)
1912 ;; Remove any `c-in-sws' property from the last char of
1913 ;; the rung before we mark it with `c-is-sws', so that we
1914 ;; won't connect with the remains of a broken "ladder".
1915 (c-remove-in-sws (point) (1+ (point))))
1916 (c-put-is-sws (1+ rung-pos)
1917 (1+ (point)))
1918 (c-put-in-sws rung-pos
1919 (setq rung-pos (point)
1920 last-put-in-sws-pos rung-pos)))
1922 ;; Now move over any comments (x)or a CPP construct.
1923 (setq simple-ws-end (point))
1924 (c-forward-comments)
1926 (cond
1927 ((/= (point) simple-ws-end)
1928 ;; Skipped over comments. Don't cache at eob in case the buffer
1929 ;; is narrowed.
1930 (not (eobp)))
1932 ((save-excursion
1933 (and c-opt-cpp-prefix
1934 (looking-at c-opt-cpp-start)
1935 (progn (skip-chars-backward " \t")
1936 (bolp))
1937 (or (bobp)
1938 (progn (backward-char)
1939 (not (eq (char-before) ?\\))))))
1940 ;; Skip a preprocessor directive.
1941 (end-of-line)
1942 (while (and (eq (char-before) ?\\)
1943 (= (forward-line 1) 0))
1944 (end-of-line))
1945 (forward-line 1)
1946 (setq safe-start t)
1947 ;; Don't cache at eob in case the buffer is narrowed.
1948 (not (eobp)))
1950 ((and c-opt-cpp-prefix
1951 (looking-at c-noise-macro-name-re))
1952 ;; Skip over a noise macro.
1953 (goto-char (match-end 1))
1954 (setq safe-start t)
1955 (not (eobp)))))
1957 ;; We've searched over a piece of non-white syntactic ws. See if this
1958 ;; can be cached.
1959 (setq next-rung-pos (point))
1960 (skip-chars-forward " \t\n\r\f\v")
1961 (setq rung-end-pos (min (1+ (point)) (point-max)))
1963 (if (or
1964 ;; Cache if we haven't skipped comments only, and if we started
1965 ;; either from a marked rung or from a completely uncached
1966 ;; position.
1967 (and safe-start
1968 (or rung-is-marked
1969 (not (get-text-property simple-ws-end 'c-in-sws))))
1971 ;; See if there's a marked rung in the encountered simple ws. If
1972 ;; so then we can cache, unless `safe-start' is nil. Even then
1973 ;; we need to do this to check if the cache can be used for the
1974 ;; next step.
1975 (and (setq next-rung-is-marked
1976 (text-property-any next-rung-pos rung-end-pos
1977 'c-is-sws t))
1978 safe-start))
1980 (progn
1981 (c-debug-sws-msg
1982 "c-forward-sws caching [%s..%s] - [%s..%s] (max %s)"
1983 rung-pos (1+ simple-ws-end) next-rung-pos rung-end-pos
1984 (point-max))
1986 ;; Remove the properties for any nested ws that might be cached.
1987 ;; Only necessary for `c-is-sws' since `c-in-sws' will be set
1988 ;; anyway.
1989 (c-remove-is-sws (1+ simple-ws-end) next-rung-pos)
1990 (unless (and rung-is-marked (= rung-pos simple-ws-end))
1991 (c-put-is-sws rung-pos
1992 (1+ simple-ws-end))
1993 (setq rung-is-marked t))
1994 (c-put-in-sws rung-pos
1995 (setq rung-pos (point)
1996 last-put-in-sws-pos rung-pos))
1997 (unless (get-text-property (1- rung-end-pos) 'c-is-sws)
1998 ;; Remove any `c-in-sws' property from the last char of
1999 ;; the rung before we mark it with `c-is-sws', so that we
2000 ;; won't connect with the remains of a broken "ladder".
2001 (c-remove-in-sws (1- rung-end-pos) rung-end-pos))
2002 (c-put-is-sws next-rung-pos
2003 rung-end-pos))
2005 (c-debug-sws-msg
2006 "c-forward-sws not caching [%s..%s] - [%s..%s] (max %s)"
2007 rung-pos (1+ simple-ws-end) next-rung-pos rung-end-pos
2008 (point-max))
2010 ;; Set `rung-pos' for the next rung. It's the same thing here as
2011 ;; initially, except that the rung position is set as early as
2012 ;; possible since we can't be in the ending ws of a line comment or
2013 ;; cpp directive now.
2014 (if (setq rung-is-marked next-rung-is-marked)
2015 (setq rung-pos (1- (c-next-single-property-change
2016 rung-is-marked 'c-is-sws nil rung-end-pos)))
2017 (setq rung-pos next-rung-pos))
2018 (setq safe-start t)))
2020 ;; Make sure that the newly marked `c-in-sws' region doesn't connect to
2021 ;; another one after the point (which might occur when editing inside a
2022 ;; comment or macro).
2023 (when (eq last-put-in-sws-pos (point))
2024 (cond ((< last-put-in-sws-pos (point-max))
2025 (c-debug-sws-msg
2026 "c-forward-sws clearing at %s for cache separation"
2027 last-put-in-sws-pos)
2028 (c-remove-in-sws last-put-in-sws-pos
2029 (1+ last-put-in-sws-pos)))
2031 ;; If at eob we have to clear the last character before the end
2032 ;; instead since the buffer might be narrowed and there might
2033 ;; be a `c-in-sws' after (point-max). In this case it's
2034 ;; necessary to clear both properties.
2035 (c-debug-sws-msg
2036 "c-forward-sws clearing thoroughly at %s for cache separation"
2037 (1- last-put-in-sws-pos))
2038 (c-remove-is-and-in-sws (1- last-put-in-sws-pos)
2039 last-put-in-sws-pos))))
2040 ))))
2042 (defun c-backward-sws ()
2043 ;; Used by `c-backward-syntactic-ws' to implement the unbounded search.
2045 ;; This function might do hidden buffer changes.
2047 (let (;; `rung-pos' is set to a position as late as possible in the unmarked
2048 ;; part of the simple ws region.
2049 (rung-pos (point)) next-rung-pos last-put-in-sws-pos
2050 rung-is-marked simple-ws-beg cmt-skip-pos)
2052 ;; Skip simple horizontal ws and do a quick check on the preceding
2053 ;; character to see if it's anything that can't end syntactic ws, so we can
2054 ;; bail out early in the majority of cases when there just are a few ws
2055 ;; chars. Newlines are complicated in the backward direction, so we can't
2056 ;; skip over them.
2057 (skip-chars-backward " \t\f")
2058 (when (and (not (bobp))
2059 (save-excursion
2060 (backward-char)
2061 (or (looking-at c-syntactic-ws-end)
2062 (and c-opt-cpp-prefix
2063 (looking-at c-symbol-char-key)
2064 (progn (c-beginning-of-current-token)
2065 (looking-at c-noise-macro-name-re))))))
2066 ;; Try to find a rung position in the simple ws preceding point, so that
2067 ;; we can get a cache hit even if the last bit of the simple ws has
2068 ;; changed recently.
2069 (setq simple-ws-beg (point))
2070 (skip-chars-backward " \t\n\r\f\v")
2071 (if (setq rung-is-marked (text-property-any
2072 (point) (min (1+ rung-pos) (point-max))
2073 'c-is-sws t))
2074 ;; `rung-pos' will be the earliest marked position, which means that
2075 ;; there might be later unmarked parts in the simple ws region.
2076 ;; It's not worth the effort to fix that; the last part of the
2077 ;; simple ws is also typically edited often, so it could be wasted.
2078 (goto-char (setq rung-pos rung-is-marked))
2079 (goto-char simple-ws-beg))
2081 (with-silent-modifications
2082 (while
2083 (progn
2084 ;; Each time round the next while form, we move back over a ladder
2085 ;; and append any simple WS preceding it, if possible joining with
2086 ;; the previous ladder.
2087 (while
2088 (when (and rung-is-marked
2089 (not (bobp))
2090 (get-text-property (1- (point)) 'c-in-sws))
2092 ;; The following search is the main reason that `c-in-sws'
2093 ;; and `c-is-sws' aren't combined to one property.
2094 (goto-char (previous-single-property-change
2095 (point) 'c-in-sws nil (point-min)))
2096 (unless (get-text-property (point) 'c-is-sws)
2097 ;; If the `c-in-sws' region extended past the first
2098 ;; `c-is-sws' char we have to go forward a bit.
2099 (goto-char (c-next-single-property-change
2100 (point) 'c-is-sws)))
2102 (c-debug-sws-msg
2103 "c-backward-sws cached move %s <- %s (min %s)"
2104 (point) rung-pos (point-min))
2106 (setq rung-pos (point))
2107 (if (and (< (min (skip-chars-backward " \t\f\v")
2108 (progn
2109 (setq simple-ws-beg (point))
2110 (skip-chars-backward " \t\n\r\f\v")))
2112 (setq rung-is-marked
2113 (text-property-any (point) rung-pos
2114 'c-is-sws t)))
2116 (goto-char simple-ws-beg)
2117 nil))
2119 ;; We'll loop here if there is simple ws before the first rung.
2120 ;; That means that there's been some change in it and it's
2121 ;; possible that we've stepped into another ladder, so extend
2122 ;; the previous one to join with it if there is one, and try to
2123 ;; use the cache again.
2124 (c-debug-sws-msg
2125 "c-backward-sws extending rung with [%s..%s] (min %s)"
2126 rung-is-marked rung-pos (point-min))
2127 (unless (get-text-property (1- rung-pos) 'c-is-sws)
2128 ;; Remove any `c-in-sws' property from the last char of
2129 ;; the rung before we mark it with `c-is-sws', so that we
2130 ;; won't connect with the remains of a broken "ladder".
2131 (c-remove-in-sws (1- rung-pos) rung-pos))
2132 (c-put-is-sws rung-is-marked
2133 rung-pos)
2134 (c-put-in-sws rung-is-marked
2135 (1- rung-pos))
2136 (setq rung-pos rung-is-marked
2137 last-put-in-sws-pos rung-pos))
2139 (c-backward-comments)
2140 (setq cmt-skip-pos (point))
2142 (cond
2143 ((and c-opt-cpp-prefix
2144 (/= cmt-skip-pos simple-ws-beg)
2145 (c-beginning-of-macro))
2146 ;; Inside a cpp directive. See if it should be skipped over.
2147 (let ((cpp-beg (point)))
2149 ;; Move back over all line continuations in the region skipped
2150 ;; over by `c-backward-comments'. If we go past it then we
2151 ;; started inside the cpp directive.
2152 (goto-char simple-ws-beg)
2153 (beginning-of-line)
2154 (while (and (> (point) cmt-skip-pos)
2155 (progn (backward-char)
2156 (eq (char-before) ?\\)))
2157 (beginning-of-line))
2159 (if (< (point) cmt-skip-pos)
2160 ;; Don't move past the cpp directive if we began inside
2161 ;; it. Note that the position at the end of the last line
2162 ;; of the macro is also considered to be within it.
2163 (progn (goto-char cmt-skip-pos)
2164 nil)
2166 ;; It's worthwhile to spend a little bit of effort on finding
2167 ;; the end of the macro, to get a good `simple-ws-beg'
2168 ;; position for the cache. Note that `c-backward-comments'
2169 ;; could have stepped over some comments before going into
2170 ;; the macro, and then `simple-ws-beg' must be kept on the
2171 ;; same side of those comments.
2172 (goto-char simple-ws-beg)
2173 (skip-chars-backward " \t\n\r\f\v")
2174 (if (eq (char-before) ?\\)
2175 (forward-char))
2176 (forward-line 1)
2177 (if (< (point) simple-ws-beg)
2178 ;; Might happen if comments after the macro were skipped
2179 ;; over.
2180 (setq simple-ws-beg (point)))
2182 (goto-char cpp-beg)
2183 t)))
2185 ((/= (save-excursion
2186 (skip-chars-forward " \t\n\r\f\v" simple-ws-beg)
2187 (setq next-rung-pos (point)))
2188 simple-ws-beg)
2189 ;; Skipped over comments. Must put point at the end of
2190 ;; the simple ws at point since we might be after a line
2191 ;; comment or cpp directive that's been partially
2192 ;; narrowed out, and we can't risk marking the simple ws
2193 ;; at the end of it.
2194 (goto-char next-rung-pos)
2197 ((and c-opt-cpp-prefix
2198 (save-excursion
2199 (and (< (skip-syntax-backward "w_") 0)
2200 (progn (setq next-rung-pos (point))
2201 (looking-at c-noise-macro-name-re)))))
2202 ;; Skipped over a noise macro
2203 (goto-char next-rung-pos)
2204 t)))
2206 ;; We've searched over a piece of non-white syntactic ws. See if this
2207 ;; can be cached.
2208 (setq next-rung-pos (point))
2209 (skip-chars-backward " \t\f\v")
2211 (if (or
2212 ;; Cache if we started either from a marked rung or from a
2213 ;; completely uncached position.
2214 rung-is-marked
2215 (not (get-text-property (1- simple-ws-beg) 'c-in-sws))
2217 ;; Cache if there's a marked rung in the encountered simple ws.
2218 (save-excursion
2219 (skip-chars-backward " \t\n\r\f\v")
2220 (text-property-any (point) (min (1+ next-rung-pos) (point-max))
2221 'c-is-sws t)))
2223 (progn
2224 (c-debug-sws-msg
2225 "c-backward-sws caching [%s..%s] - [%s..%s] (min %s)"
2226 (point) (1+ next-rung-pos)
2227 simple-ws-beg (min (1+ rung-pos) (point-max))
2228 (point-min))
2230 ;; Remove the properties for any nested ws that might be cached.
2231 ;; Only necessary for `c-is-sws' since `c-in-sws' will be set
2232 ;; anyway.
2233 (c-remove-is-sws (1+ next-rung-pos) simple-ws-beg)
2234 (unless (and rung-is-marked (= simple-ws-beg rung-pos))
2235 (let ((rung-end-pos (min (1+ rung-pos) (point-max))))
2236 (unless (get-text-property (1- rung-end-pos) 'c-is-sws)
2237 ;; Remove any `c-in-sws' property from the last char of
2238 ;; the rung before we mark it with `c-is-sws', so that we
2239 ;; won't connect with the remains of a broken "ladder".
2240 (c-remove-in-sws (1- rung-end-pos) rung-end-pos))
2241 (c-put-is-sws simple-ws-beg
2242 rung-end-pos)
2243 (setq rung-is-marked t)))
2244 (c-put-in-sws (setq simple-ws-beg (point)
2245 last-put-in-sws-pos simple-ws-beg)
2246 rung-pos)
2247 (c-put-is-sws (setq rung-pos simple-ws-beg)
2248 (1+ next-rung-pos)))
2250 (c-debug-sws-msg
2251 "c-backward-sws not caching [%s..%s] - [%s..%s] (min %s)"
2252 (point) (1+ next-rung-pos)
2253 simple-ws-beg (min (1+ rung-pos) (point-max))
2254 (point-min))
2255 (setq rung-pos next-rung-pos
2256 simple-ws-beg (point))
2259 ;; Make sure that the newly marked `c-in-sws' region doesn't connect to
2260 ;; another one before the point (which might occur when editing inside a
2261 ;; comment or macro).
2262 (when (eq last-put-in-sws-pos (point))
2263 (cond ((< (point-min) last-put-in-sws-pos)
2264 (c-debug-sws-msg
2265 "c-backward-sws clearing at %s for cache separation"
2266 (1- last-put-in-sws-pos))
2267 (c-remove-in-sws (1- last-put-in-sws-pos)
2268 last-put-in-sws-pos))
2269 ((> (point-min) 1)
2270 ;; If at bob and the buffer is narrowed, we have to clear the
2271 ;; character we're standing on instead since there might be a
2272 ;; `c-in-sws' before (point-min). In this case it's necessary
2273 ;; to clear both properties.
2274 (c-debug-sws-msg
2275 "c-backward-sws clearing thoroughly at %s for cache separation"
2276 last-put-in-sws-pos)
2277 (c-remove-is-and-in-sws last-put-in-sws-pos
2278 (1+ last-put-in-sws-pos)))))
2279 ))))
2282 ;; Other whitespace tools
2283 (defun c-partial-ws-p (beg end)
2284 ;; Is the region (beg end) WS, and is there WS (or BOB/EOB) next to the
2285 ;; region? This is a "heuristic" function. .....
2287 ;; The motivation for the second bit is to check whether removing this
2288 ;; region would coalesce two symbols.
2290 ;; FIXME!!! This function doesn't check virtual semicolons in any way. Be
2291 ;; careful about using this function for, e.g. AWK. (2007/3/7)
2292 (save-excursion
2293 (let ((end+1 (min (1+ end) (point-max))))
2294 (or (progn (goto-char (max (point-min) (1- beg)))
2295 (c-skip-ws-forward end)
2296 (eq (point) end))
2297 (progn (goto-char beg)
2298 (c-skip-ws-forward end+1)
2299 (eq (point) end+1))))))
2301 ;; A system for finding noteworthy parens before the point.
2303 (defconst c-state-cache-too-far 5000)
2304 ;; A maximum comfortable scanning distance, e.g. between
2305 ;; `c-state-cache-good-pos' and "HERE" (where we call c-parse-state). When
2306 ;; this distance is exceeded, we take "emergency measures", e.g. by clearing
2307 ;; the cache and starting again from point-min or a beginning of defun. This
2308 ;; value can be tuned for efficiency or set to a lower value for testing.
2310 (defvar c-state-cache nil)
2311 (make-variable-buffer-local 'c-state-cache)
2312 ;; The state cache used by `c-parse-state' to cut down the amount of
2313 ;; searching. It's the result from some earlier `c-parse-state' call. See
2314 ;; `c-parse-state''s doc string for details of its structure.
2316 ;; The use of the cached info is more effective if the next
2317 ;; `c-parse-state' call is on a line close by the one the cached state
2318 ;; was made at; the cache can actually slow down a little if the
2319 ;; cached state was made very far back in the buffer. The cache is
2320 ;; most effective if `c-parse-state' is used on each line while moving
2321 ;; forward.
2323 (defvar c-state-cache-good-pos 1)
2324 (make-variable-buffer-local 'c-state-cache-good-pos)
2325 ;; This is a position where `c-state-cache' is known to be correct, or
2326 ;; nil (see below). It's a position inside one of the recorded unclosed
2327 ;; parens or the top level, but not further nested inside any literal or
2328 ;; subparen that is closed before the last recorded position.
2330 ;; The exact position is chosen to try to be close to yet earlier than
2331 ;; the position where `c-state-cache' will be called next. Right now
2332 ;; the heuristic is to set it to the position after the last found
2333 ;; closing paren (of any type) before the line on which
2334 ;; `c-parse-state' was called. That is chosen primarily to work well
2335 ;; with refontification of the current line.
2337 ;; 2009-07-28: When `c-state-point-min' and the last position where
2338 ;; `c-parse-state' or for which `c-invalidate-state-cache' was called, are
2339 ;; both in the same literal, there is no such "good position", and
2340 ;; c-state-cache-good-pos is then nil. This is the ONLY circumstance in which
2341 ;; it can be nil. In this case, `c-state-point-min-literal' will be non-nil.
2343 ;; 2009-06-12: In a brace desert, c-state-cache-good-pos may also be in
2344 ;; the middle of the desert, as long as it is not within a brace pair
2345 ;; recorded in `c-state-cache' or a paren/bracket pair.
2347 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2348 ;; We maintain a simple cache of positions which aren't in a literal, so as to
2349 ;; speed up testing for non-literality.
2350 (defconst c-state-nonlit-pos-interval 3000)
2351 ;; The approximate interval between entries in `c-state-nonlit-pos-cache'.
2353 (defvar c-state-nonlit-pos-cache nil)
2354 (make-variable-buffer-local 'c-state-nonlit-pos-cache)
2355 ;; A list of buffer positions which are known not to be in a literal or a cpp
2356 ;; construct. This is ordered with higher positions at the front of the list.
2357 ;; Only those which are less than `c-state-nonlit-pos-cache-limit' are valid.
2359 (defvar c-state-nonlit-pos-cache-limit 1)
2360 (make-variable-buffer-local 'c-state-nonlit-pos-cache-limit)
2361 ;; An upper limit on valid entries in `c-state-nonlit-pos-cache'. This is
2362 ;; reduced by buffer changes, and increased by invocations of
2363 ;; `c-state-literal-at'.
2365 (defvar c-state-semi-nonlit-pos-cache nil)
2366 (make-variable-buffer-local 'c-state-semi-nonlit-pos-cache)
2367 ;; A list of elements which are either buffer positions (when such positions
2368 ;; are not in literals) or lists of the form (POS TYPE START), where POS is
2369 ;; a buffer position inside a literal, TYPE is the type of the literal
2370 ;; ('string, 'c, or 'c++) and START is the start of the literal.
2372 (defvar c-state-semi-nonlit-pos-cache-limit 1)
2373 (make-variable-buffer-local 'c-state-semi-nonlit-pos-cache-limit)
2374 ;; An upper limit on valid entries in `c-state-semi-nonlit-pos-cache'. This
2375 ;; is reduced by buffer changes, and increased by invocations of
2376 ;; `c-parse-ps-state-below'.
2378 (defsubst c-truncate-semi-nonlit-pos-cache (pos)
2379 ;; Truncate the upper bound of the cache `c-state-semi-nonlit-pos-cache' to
2380 ;; POS, if it is higher than that position.
2381 (setq c-state-semi-nonlit-pos-cache-limit
2382 (min c-state-semi-nonlit-pos-cache-limit pos)))
2384 (defun c-state-semi-pp-to-literal (here &optional not-in-delimiter)
2385 ;; Do a parse-partial-sexp from a position in the buffer before HERE which
2386 ;; isn't in a literal, and return information about HERE, either:
2387 ;; (STATE TYPE BEG) if HERE is in a literal; or
2388 ;; (STATE) otherwise,
2389 ;; where STATE is the parsing state at HERE, TYPE is the type of the literal
2390 ;; enclosing HERE, (one of 'string, 'c, 'c++) and BEG is the starting
2391 ;; position of that literal (including the delimiter).
2393 ;; Unless NOT-IN-DELIMITER is non-nil, when TO is inside a two-character
2394 ;; comment opener, this is recognized as being in a comment literal.
2396 ;; Only elements 3 (in a string), 4 (in a comment), 5 (following a quote), 7
2397 ;; (comment type), and 8 (start of comment/string), and possibly 10 (in
2398 ;; newer Emacsen only, the syntax of a position after a potential first char
2399 ;; of a two char construct) of STATE are valid.
2400 (save-excursion
2401 (save-restriction
2402 (widen)
2403 (save-match-data
2404 (let* ((base-and-state (c-parse-ps-state-below here))
2405 (base (car base-and-state))
2406 (s (cdr base-and-state))
2407 (s (parse-partial-sexp base here nil nil s))
2409 (cond
2410 ((or (nth 3 s) (nth 4 s)) ; in a string or comment
2411 (setq ty (cond
2412 ((nth 3 s) 'string)
2413 ((nth 7 s) 'c++)
2414 (t 'c)))
2415 (list s ty (nth 8 s)))
2417 ((and (not not-in-delimiter) ; inside a comment starter
2418 (not (bobp))
2419 (progn (backward-char)
2420 (and (not (and (memq 'category-properties c-emacs-features)
2421 (looking-at "\\s!")))
2422 (looking-at c-comment-start-regexp))))
2423 (setq ty (if (looking-at c-block-comment-start-regexp) 'c 'c++))
2424 (list s ty (point)))
2426 (t (list s))))))))
2428 (defun c-state-full-pp-to-literal (here &optional not-in-delimiter)
2429 ;; This function will supersede c-state-pp-to-literal.
2431 ;; Do a parse-partial-sexp from a position in the buffer before HERE which
2432 ;; isn't in a literal, and return information about HERE, either:
2433 ;; (STATE TYPE (BEG . END)) if HERE is in a literal; or
2434 ;; (STATE) otherwise,
2435 ;; where STATE is the parsing state at HERE, TYPE is the type of the literal
2436 ;; enclosing HERE, (one of 'string, 'c, 'c++) and (BEG . END) is the
2437 ;; boundaries of that literal (including the delimiters).
2439 ;; Unless NOT-IN-DELIMITER is non-nil, when TO is inside a two-character
2440 ;; comment opener, this is recognized as being in a comment literal.
2442 ;; Only elements 3 (in a string), 4 (in a comment), 5 (following a quote), 7
2443 ;; (comment type), and 8 (start of comment/string), and possibly 10 (in
2444 ;; newer Emacsen only, the syntax of a position after a potential first char
2445 ;; of a two char construct) of STATE are valid.
2446 (save-excursion
2447 (save-restriction
2448 (widen)
2449 (save-match-data
2450 (let* ((base-and-state (c-parse-ps-state-below here))
2451 (base (car base-and-state))
2452 (s (cdr base-and-state))
2453 (s (parse-partial-sexp base here nil nil s))
2454 ty start)
2455 (cond
2456 ((or (nth 3 s) (nth 4 s)) ; in a string or comment
2457 (setq ty (cond
2458 ((nth 3 s) 'string)
2459 ((nth 7 s) 'c++)
2460 (t 'c)))
2461 (setq start (nth 8 s))
2462 (parse-partial-sexp here (point-max)
2463 nil ; TARGETDEPTH
2464 nil ; STOPBEFORE
2465 s ; OLDSTATE
2466 'syntax-table) ; stop at end of literal
2467 (list s ty (cons start (point))))
2469 ((and (not not-in-delimiter) ; inside a comment starter
2470 (not (bobp))
2471 (progn (backward-char)
2472 (and (not (and (memq 'category-properties c-emacs-features)
2473 (looking-at "\\s!")))
2474 (looking-at c-comment-start-regexp))))
2475 (setq ty (if (looking-at c-block-comment-start-regexp) 'c 'c++)
2476 start (point))
2477 (forward-comment 1)
2478 (list s ty (cons start (point))))
2480 (t (list s))))))))
2482 (defsubst c-state-pp-to-literal (from to &optional not-in-delimiter)
2483 ;; Do a parse-partial-sexp from FROM to TO, returning either
2484 ;; (STATE TYPE (BEG . END)) if TO is in a literal; or
2485 ;; (STATE) otherwise,
2486 ;; where STATE is the parsing state at TO, TYPE is the type of the literal
2487 ;; (one of 'c, 'c++, 'string) and (BEG . END) is the boundaries of the literal,
2488 ;; including the delimiters.
2490 ;; Unless NOT-IN-DELIMITER is non-nil, when TO is inside a two-character
2491 ;; comment opener, this is recognized as being in a comment literal.
2493 ;; Only elements 3 (in a string), 4 (in a comment), 5 (following a quote),
2494 ;; 7 (comment type) and 8 (start of comment/string) (and possibly 9) of
2495 ;; STATE are valid.
2496 (save-excursion
2497 (save-match-data
2498 (let ((s (parse-partial-sexp from to))
2499 ty co-st)
2500 (cond
2501 ((or (nth 3 s) (nth 4 s)) ; in a string or comment
2502 (setq ty (cond
2503 ((nth 3 s) 'string)
2504 ((nth 7 s) 'c++)
2505 (t 'c)))
2506 (parse-partial-sexp (point) (point-max)
2507 nil ; TARGETDEPTH
2508 nil ; STOPBEFORE
2509 s ; OLDSTATE
2510 'syntax-table) ; stop at end of literal
2511 `(,s ,ty (,(nth 8 s) . ,(point))))
2513 ((and (not not-in-delimiter) ; inside a comment starter
2514 (not (bobp))
2515 (progn (backward-char)
2516 (and (not (looking-at "\\s!"))
2517 (looking-at c-comment-start-regexp))))
2518 (setq ty (if (looking-at c-block-comment-start-regexp) 'c 'c++)
2519 co-st (point))
2520 (forward-comment 1)
2521 `(,s ,ty (,co-st . ,(point))))
2523 (t `(,s)))))))
2525 (defun c-cache-to-parse-ps-state (elt)
2526 ;; Create a list suitable to use as the old-state parameter to
2527 ;; `parse-partial-sexp', out of ELT. ELT is either just a number, a buffer
2528 ;; position, or it is a list (POS TYPE STARTING-POS). Here POS is the
2529 ;; buffer position the other elements are pertinent for, TYPE is either 'c
2530 ;; or 'c++ (for a comment) or a character (for a string delimiter) or t
2531 ;; (meaning a string fence opened the string), STARTING-POS is the starting
2532 ;; position of the comment or string.
2533 (if (consp elt)
2534 (let ((depth 0) (containing nil) (last nil)
2535 in-string in-comment (after-quote nil)
2536 (min-depth 0) com-style com-str-start (intermediate nil)
2537 (between-syntax nil)
2538 (type (cadr elt)))
2539 (setq com-str-start (car (cddr elt)))
2540 (cond
2541 ((or (numberp type) (eq type t)) ; A string
2542 (setq in-string type))
2543 ((memq type '(c c++)) ; A comment
2544 (setq in-comment t
2545 com-style (if (eq type 'c++) 1 nil)))
2546 (t (c-benign-error "Invalid type %s in c-cache-to-parse-ps-state"
2547 elt)))
2548 (list depth containing last
2549 in-string in-comment after-quote
2550 min-depth com-style com-str-start
2551 intermediate nil))
2552 (copy-tree '(0 nil nil nil nil nil 0 nil nil nil nil))))
2554 (defun c-parse-ps-state-to-cache (state)
2555 ;; Convert STATE, a `parse-partial-sexp' state valid at POINT, to an element
2556 ;; for the `c-state-semi-nonlit-pos-cache' cache. This is either POINT
2557 ;; (when point is not in a literal) or a list (POINT TYPE STARTING-POS),
2558 ;; where TYPE is the type of the literal, either 'string, 'c, or 'c++, and
2559 ;; STARTING-POS is the starting position of the comment or string.
2560 (cond
2561 ((nth 3 state) ; A string
2562 (list (point) (nth 3 state) (nth 8 state)))
2563 ((nth 4 state) ; A comment
2564 (list (point)
2565 (if (eq (nth 7 state) 1) 'c++ 'c)
2566 (nth 8 state)))
2567 (t ; Neither string nor comment.
2568 (point))))
2570 (defsubst c-ps-state-cache-pos (elt)
2571 ;; Get the buffer position from ELT, an element from the cache
2572 ;; `c-state-semi-nonlit-pos-cache'.
2573 (if (atom elt)
2575 (car elt)))
2577 (defun c-parse-ps-state-below (here)
2578 ;; Given a buffer position HERE, Return a cons (CACHE-POS . STATE), where
2579 ;; CACHE-POS is a position not very far before HERE for which the
2580 ;; parse-partial-sexp STATE is valid. Note that the only valid elements of
2581 ;; STATE are those concerning comments and strings; STATE is the state of a
2582 ;; null `parse-partial-sexp' scan when CACHE-POS is not in a comment or
2583 ;; string.
2584 (save-excursion
2585 (save-restriction
2586 (widen)
2587 (let ((c c-state-semi-nonlit-pos-cache)
2588 elt state pos npos high-elt)
2589 ;; Trim the cache to take account of buffer changes.
2590 (while (and c (> (c-ps-state-cache-pos (car c))
2591 c-state-semi-nonlit-pos-cache-limit))
2592 (setq c (cdr c)))
2593 (setq c-state-semi-nonlit-pos-cache c)
2595 (while (and c (> (c-ps-state-cache-pos (car c)) here))
2596 (setq high-elt (car c))
2597 (setq c (cdr c)))
2598 (setq pos (or (and c (c-ps-state-cache-pos (car c)))
2599 (point-min)))
2601 (if high-elt
2602 (setq state (c-cache-to-parse-ps-state (car c)))
2603 (setq elt (if c (car c) (point-min)))
2604 (setq state
2605 (if c
2606 (c-cache-to-parse-ps-state (car c))
2607 (copy-tree '(0 nil nil nil nil nil 0 nil nil nil nil))))
2608 (while
2609 ;; Add an element to `c-state-semi-nonlit-pos-cache' each iteration.
2610 (<= (setq npos (+ pos c-state-nonlit-pos-interval)) here)
2611 (setq state (parse-partial-sexp pos npos nil nil state))
2612 (setq elt (c-parse-ps-state-to-cache state))
2613 (setq c-state-semi-nonlit-pos-cache
2614 (cons elt c-state-semi-nonlit-pos-cache))
2615 (setq pos npos)))
2617 (if (> pos c-state-semi-nonlit-pos-cache-limit)
2618 (setq c-state-semi-nonlit-pos-cache-limit pos))
2620 (cons pos state)))))
2622 (defun c-state-safe-place (here)
2623 ;; Return a buffer position before HERE which is "safe", i.e. outside any
2624 ;; string, comment, or macro.
2626 ;; NOTE: This function manipulates `c-state-nonlit-pos-cache'. This cache
2627 ;; MAY NOT contain any positions within macros, since macros are frequently
2628 ;; turned into comments by use of the `c-cpp-delimiter' category properties.
2629 ;; We cannot rely on this mechanism whilst determining a cache pos since
2630 ;; this function is also called from outwith `c-parse-state'.
2631 (save-restriction
2632 (widen)
2633 (save-excursion
2634 (let ((c c-state-nonlit-pos-cache)
2635 pos npos high-pos lit macro-beg macro-end)
2636 ;; Trim the cache to take account of buffer changes.
2637 (while (and c (> (car c) c-state-nonlit-pos-cache-limit))
2638 (setq c (cdr c)))
2639 (setq c-state-nonlit-pos-cache c)
2641 (while (and c (> (car c) here))
2642 (setq high-pos (car c))
2643 (setq c (cdr c)))
2644 (setq pos (or (car c) (point-min)))
2646 (unless high-pos
2647 (while
2648 ;; Add an element to `c-state-nonlit-pos-cache' each iteration.
2649 (and
2650 (setq npos
2651 (when (<= (+ pos c-state-nonlit-pos-interval) here)
2652 (+ pos c-state-nonlit-pos-interval)))
2654 ;; Test for being in a literal. If so, go to after it.
2655 (progn
2656 (setq lit (car (cddr (c-state-pp-to-literal pos npos))))
2657 (or (null lit)
2658 (prog1 (<= (cdr lit) here)
2659 (setq npos (cdr lit)))))
2661 ;; Test for being in a macro. If so, go to after it.
2662 (progn
2663 (goto-char npos)
2664 (setq macro-beg
2665 (and (c-beginning-of-macro) (/= (point) npos) (point)))
2666 (when macro-beg
2667 (c-syntactic-end-of-macro)
2668 (or (eobp) (forward-char))
2669 (setq macro-end (point)))
2670 (or (null macro-beg)
2671 (prog1 (<= macro-end here)
2672 (setq npos macro-end)))))
2674 (setq pos npos)
2675 (setq c-state-nonlit-pos-cache (cons pos c-state-nonlit-pos-cache)))
2676 ;; Add one extra element above HERE so as to to avoid the previous
2677 ;; expensive calculation when the next call is close to the current
2678 ;; one. This is especially useful when inside a large macro.
2679 (when npos
2680 (setq c-state-nonlit-pos-cache
2681 (cons npos c-state-nonlit-pos-cache))))
2683 (if (> pos c-state-nonlit-pos-cache-limit)
2684 (setq c-state-nonlit-pos-cache-limit pos))
2685 pos))))
2687 (defun c-state-literal-at (here)
2688 ;; If position HERE is inside a literal, return (START . END), the
2689 ;; boundaries of the literal (which may be outside the accessible bit of the
2690 ;; buffer). Otherwise, return nil.
2692 ;; This function is almost the same as `c-literal-limits'. Previously, it
2693 ;; differed in that it was a lower level function, and that it rigorously
2694 ;; followed the syntax from BOB. `c-literal-limits' is now (2011-12)
2695 ;; virtually identical to this function.
2696 (save-restriction
2697 (widen)
2698 (save-excursion
2699 (let ((pos (c-state-safe-place here)))
2700 (car (cddr (c-state-pp-to-literal pos here)))))))
2702 (defsubst c-state-lit-beg (pos)
2703 ;; Return the start of the literal containing POS, or POS itself.
2704 (or (car (c-state-literal-at pos))
2705 pos))
2707 (defsubst c-state-cache-non-literal-place (pos state)
2708 ;; Return a position outside of a string/comment/macro at or before POS.
2709 ;; STATE is the parse-partial-sexp state at POS.
2710 (let ((res (if (or (nth 3 state) ; in a string?
2711 (nth 4 state)) ; in a comment?
2712 (nth 8 state)
2713 pos)))
2714 (save-excursion
2715 (goto-char res)
2716 (if (c-beginning-of-macro)
2717 (point)
2718 res))))
2720 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2721 ;; Stuff to do with point-min, and coping with any literal there.
2722 (defvar c-state-point-min 1)
2723 (make-variable-buffer-local 'c-state-point-min)
2724 ;; This is (point-min) when `c-state-cache' was last calculated. A change of
2725 ;; narrowing is likely to affect the parens that are visible before the point.
2727 (defvar c-state-point-min-lit-type nil)
2728 (make-variable-buffer-local 'c-state-point-min-lit-type)
2729 (defvar c-state-point-min-lit-start nil)
2730 (make-variable-buffer-local 'c-state-point-min-lit-start)
2731 ;; These two variables define the literal, if any, containing point-min.
2732 ;; Their values are, respectively, 'string, c, or c++, and the start of the
2733 ;; literal. If there's no literal there, they're both nil.
2735 (defvar c-state-min-scan-pos 1)
2736 (make-variable-buffer-local 'c-state-min-scan-pos)
2737 ;; This is the earliest buffer-pos from which scanning can be done. It is
2738 ;; either the end of the literal containing point-min, or point-min itself.
2739 ;; It becomes nil if the buffer is changed earlier than this point.
2740 (defun c-state-get-min-scan-pos ()
2741 ;; Return the lowest valid scanning pos. This will be the end of the
2742 ;; literal enclosing point-min, or point-min itself.
2743 (or c-state-min-scan-pos
2744 (save-restriction
2745 (save-excursion
2746 (widen)
2747 (goto-char c-state-point-min-lit-start)
2748 (if (eq c-state-point-min-lit-type 'string)
2749 (forward-sexp)
2750 (forward-comment 1))
2751 (setq c-state-min-scan-pos (point))))))
2753 (defun c-state-mark-point-min-literal ()
2754 ;; Determine the properties of any literal containing POINT-MIN, setting the
2755 ;; variables `c-state-point-min-lit-type', `c-state-point-min-lit-start',
2756 ;; and `c-state-min-scan-pos' accordingly. The return value is meaningless.
2757 (let ((p-min (point-min))
2758 lit)
2759 (save-restriction
2760 (widen)
2761 (setq lit (c-state-literal-at p-min))
2762 (if lit
2763 (setq c-state-point-min-lit-type
2764 (save-excursion
2765 (goto-char (car lit))
2766 (cond
2767 ((looking-at c-block-comment-start-regexp) 'c)
2768 ((looking-at c-line-comment-starter) 'c++)
2769 (t 'string)))
2770 c-state-point-min-lit-start (car lit)
2771 c-state-min-scan-pos (cdr lit))
2772 (setq c-state-point-min-lit-type nil
2773 c-state-point-min-lit-start nil
2774 c-state-min-scan-pos p-min)))))
2777 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2778 ;; A variable which signals a brace dessert - helpful for reducing the number
2779 ;; of fruitless backward scans.
2780 (defvar c-state-brace-pair-desert nil)
2781 (make-variable-buffer-local 'c-state-brace-pair-desert)
2782 ;; Used only in `c-append-lower-brace-pair-to-state-cache'. It is set when
2783 ;; that defun has searched backwards for a brace pair and not found one. Its
2784 ;; value is either nil or a cons (PA . FROM), where PA is the position of the
2785 ;; enclosing opening paren/brace/bracket which bounds the backwards search (or
2786 ;; nil when at top level) and FROM is where the backward search started. It
2787 ;; is reset to nil in `c-invalidate-state-cache'.
2790 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2791 ;; Lowish level functions/macros which work directly on `c-state-cache', or a
2792 ;; list of like structure.
2793 (defmacro c-state-cache-top-lparen (&optional cache)
2794 ;; Return the address of the top left brace/bracket/paren recorded in CACHE
2795 ;; (default `c-state-cache') (or nil).
2796 (let ((cash (or cache 'c-state-cache)))
2797 `(if (consp (car ,cash))
2798 (caar ,cash)
2799 (car ,cash))))
2801 (defmacro c-state-cache-top-paren (&optional cache)
2802 ;; Return the address of the latest brace/bracket/paren (whether left or
2803 ;; right) recorded in CACHE (default `c-state-cache') or nil.
2804 (let ((cash (or cache 'c-state-cache)))
2805 `(if (consp (car ,cash))
2806 (cdar ,cash)
2807 (car ,cash))))
2809 (defmacro c-state-cache-after-top-paren (&optional cache)
2810 ;; Return the position just after the latest brace/bracket/paren (whether
2811 ;; left or right) recorded in CACHE (default `c-state-cache') or nil.
2812 (let ((cash (or cache 'c-state-cache)))
2813 `(if (consp (car ,cash))
2814 (cdar ,cash)
2815 (and (car ,cash)
2816 (1+ (car ,cash))))))
2818 (defun c-get-cache-scan-pos (here)
2819 ;; From the state-cache, determine the buffer position from which we might
2820 ;; scan forward to HERE to update this cache. This position will be just
2821 ;; after a paren/brace/bracket recorded in the cache, if possible, otherwise
2822 ;; return the earliest position in the accessible region which isn't within
2823 ;; a literal. If the visible portion of the buffer is entirely within a
2824 ;; literal, return NIL.
2825 (let ((c c-state-cache) elt)
2826 ;(while (>= (or (c-state-cache-top-lparen c) 1) here)
2827 (while (and c
2828 (>= (c-state-cache-top-lparen c) here))
2829 (setq c (cdr c)))
2831 (setq elt (car c))
2832 (cond
2833 ((consp elt)
2834 (if (> (cdr elt) here)
2835 (1+ (car elt))
2836 (cdr elt)))
2837 (elt (1+ elt))
2838 ((<= (c-state-get-min-scan-pos) here)
2839 (c-state-get-min-scan-pos))
2840 (t nil))))
2842 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2843 ;; Variables which keep track of preprocessor constructs.
2844 (defvar c-state-old-cpp-beg-marker nil)
2845 (make-variable-buffer-local 'c-state-old-cpp-beg-marker)
2846 (defvar c-state-old-cpp-beg nil)
2847 (make-variable-buffer-local 'c-state-old-cpp-beg)
2848 (defvar c-state-old-cpp-end-marker nil)
2849 (make-variable-buffer-local 'c-state-old-cpp-end-marker)
2850 (defvar c-state-old-cpp-end nil)
2851 (make-variable-buffer-local 'c-state-old-cpp-end)
2852 ;; These are the limits of the macro containing point at the previous call of
2853 ;; `c-parse-state', or nil.
2855 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2856 ;; Defuns which analyze the buffer, yet don't change `c-state-cache'.
2857 (defun c-get-fallback-scan-pos (here)
2858 ;; Return a start position for building `c-state-cache' from
2859 ;; scratch. This will be at the top level, 2 defuns back.
2860 (save-excursion
2861 ;; Go back 2 bods, but ignore any bogus positions returned by
2862 ;; beginning-of-defun (i.e. open paren in column zero).
2863 (goto-char here)
2864 (let ((cnt 2))
2865 (while (not (or (bobp) (zerop cnt)))
2866 (c-beginning-of-defun-1) ; Pure elisp BOD.
2867 (if (eq (char-after) ?\{)
2868 (setq cnt (1- cnt)))))
2869 (point)))
2871 (defun c-state-balance-parens-backwards (here- here+ top)
2872 ;; Return the position of the opening paren/brace/bracket before HERE- which
2873 ;; matches the outermost close p/b/b between HERE+ and TOP. Except when
2874 ;; there's a macro, HERE- and HERE+ are the same. Like this:
2876 ;; ............................................
2877 ;; | |
2878 ;; ( [ ( .........#macro.. ) ( ) ] )
2879 ;; ^ ^ ^ ^
2880 ;; | | | |
2881 ;; return HERE- HERE+ TOP
2883 ;; If there aren't enough opening paren/brace/brackets, return the position
2884 ;; of the outermost one found, or HERE- if there are none. If there are no
2885 ;; closing p/b/bs between HERE+ and TOP, return HERE-. HERE-/+ and TOP
2886 ;; must not be inside literals. Only the accessible portion of the buffer
2887 ;; will be scanned.
2889 ;; PART 1: scan from `here+' up to `top', accumulating ")"s which enclose
2890 ;; `here'. Go round the next loop each time we pass over such a ")". These
2891 ;; probably match "("s before `here-'.
2892 (let (pos pa ren+1 lonely-rens)
2893 (save-excursion
2894 (save-restriction
2895 (narrow-to-region (point-min) top) ; This can move point, sometimes.
2896 (setq pos here+)
2897 (c-safe
2898 (while
2899 (setq ren+1 (c-sc-scan-lists pos 1 1)) ; might signal
2900 (setq lonely-rens (cons ren+1 lonely-rens)
2901 pos ren+1)))))
2903 ;; PART 2: Scan back before `here-' searching for the "("s
2904 ;; matching/mismatching the ")"s found above. We only need to direct the
2905 ;; caller to scan when we've encountered unmatched right parens.
2906 (setq pos here-)
2907 (when lonely-rens
2908 (c-safe
2909 (while
2910 (and lonely-rens ; actual values aren't used.
2911 (setq pa (c-sc-scan-lists pos -1 1)))
2912 (setq pos pa)
2913 (setq lonely-rens (cdr lonely-rens)))))
2914 pos))
2916 (defun c-parse-state-get-strategy (here good-pos)
2917 ;; Determine the scanning strategy for adjusting `c-parse-state', attempting
2918 ;; to minimize the amount of scanning. HERE is the pertinent position in
2919 ;; the buffer, GOOD-POS is a position where `c-state-cache' (possibly with
2920 ;; its head trimmed) is known to be good, or nil if there is no such
2921 ;; position.
2923 ;; The return value is a list, one of the following:
2925 ;; o - ('forward START-POINT) - scan forward from START-POINT,
2926 ;; which is not less than the highest position in `c-state-cache' below HERE,
2927 ;; which is after GOOD-POS.
2928 ;; o - ('backward nil) - scan backwards (from HERE).
2929 ;; o - ('back-and-forward START-POINT) - like 'forward, but when HERE is earlier
2930 ;; than GOOD-POS.
2931 ;; o - ('BOD START-POINT) - scan forwards from START-POINT, which is at the
2932 ;; top level.
2933 ;; o - ('IN-LIT nil) - point is inside the literal containing point-min.
2934 (let* ((in-macro-start ; start of macro containing HERE or nil.
2935 (save-excursion
2936 (goto-char here)
2937 (and (c-beginning-of-macro)
2938 (point))))
2939 (changed-macro-start
2940 (and in-macro-start
2941 (not (and c-state-old-cpp-beg
2942 (= in-macro-start c-state-old-cpp-beg)))
2943 in-macro-start))
2944 (cache-pos (c-get-cache-scan-pos (if changed-macro-start
2945 (min changed-macro-start here)
2946 here))) ; highest suitable position in cache (or 1)
2947 BOD-pos ; position of 2nd BOD before HERE.
2948 strategy ; 'forward, 'backward, 'BOD, or 'IN-LIT.
2949 start-point
2950 how-far) ; putative scanning distance.
2951 (setq good-pos (or good-pos (c-state-get-min-scan-pos)))
2952 (cond
2953 ((< here (c-state-get-min-scan-pos))
2954 (setq strategy 'IN-LIT
2955 start-point nil
2956 cache-pos nil
2957 how-far 0))
2958 ((<= good-pos here)
2959 (setq strategy 'forward
2960 start-point (if changed-macro-start
2961 cache-pos
2962 (max good-pos cache-pos))
2963 how-far (- here start-point)))
2964 ((< (- good-pos here) (- here cache-pos)) ; FIXME!!! ; apply some sort of weighting.
2965 (setq strategy 'backward
2966 how-far (- good-pos here)))
2968 (setq strategy 'back-and-forward
2969 start-point cache-pos
2970 how-far (- here start-point))))
2972 ;; Might we be better off starting from the top level, two defuns back,
2973 ;; instead? This heuristic no longer works well in C++, where
2974 ;; declarations inside namespace brace blocks are frequently placed at
2975 ;; column zero. (2015-11-10): Remove the condition on C++ Mode.
2976 (when (and (or (not (memq 'col-0-paren c-emacs-features))
2977 open-paren-in-column-0-is-defun-start)
2978 ;; (not (c-major-mode-is 'c++-mode))
2979 (> how-far c-state-cache-too-far))
2980 (setq BOD-pos (c-get-fallback-scan-pos here)) ; somewhat EXPENSIVE!!!
2981 (if (< (- here BOD-pos) how-far)
2982 (setq strategy 'BOD
2983 start-point BOD-pos)))
2985 (list strategy start-point)))
2988 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2989 ;; Routines which change `c-state-cache' and associated values.
2990 (defun c-renarrow-state-cache ()
2991 ;; The region (more precisely, point-min) has changed since we
2992 ;; calculated `c-state-cache'. Amend `c-state-cache' accordingly.
2993 (if (< (point-min) c-state-point-min)
2994 ;; If point-min has MOVED BACKWARDS then we drop the state completely.
2995 ;; It would be possible to do a better job here and recalculate the top
2996 ;; only.
2997 (progn
2998 (c-state-mark-point-min-literal)
2999 (setq c-state-cache nil
3000 c-state-cache-good-pos c-state-min-scan-pos
3001 c-state-brace-pair-desert nil))
3003 ;; point-min has MOVED FORWARD.
3005 ;; Is the new point-min inside a (different) literal?
3006 (unless (and c-state-point-min-lit-start ; at prev. point-min
3007 (< (point-min) (c-state-get-min-scan-pos)))
3008 (c-state-mark-point-min-literal))
3010 ;; Cut off a bit of the tail from `c-state-cache'.
3011 (let ((ptr (cons nil c-state-cache))
3013 (while (and (setq pa (c-state-cache-top-lparen (cdr ptr)))
3014 (>= pa (point-min)))
3015 (setq ptr (cdr ptr)))
3017 (when (consp ptr)
3018 (if (or (eq (cdr ptr) c-state-cache)
3019 (and (consp (cadr ptr))
3020 (> (cdr (cadr ptr)) (point-min)))) ; Our new point-min is
3021 ; inside a recorded
3022 ; brace pair.
3023 (setq c-state-cache nil
3024 c-state-cache-good-pos c-state-min-scan-pos)
3025 (setcdr ptr nil)
3026 (setq c-state-cache-good-pos (1+ (c-state-cache-top-lparen))))
3029 (setq c-state-point-min (point-min)))
3031 (defun c-append-lower-brace-pair-to-state-cache (from here &optional upper-lim)
3032 ;; If there is a brace pair preceding FROM in the buffer, at the same level
3033 ;; of nesting (not necessarily immediately preceding), push a cons onto
3034 ;; `c-state-cache' to represent it. FROM must not be inside a literal. If
3035 ;; UPPER-LIM is non-nil, we append the highest brace pair whose "}" is below
3036 ;; UPPER-LIM.
3038 ;; Return non-nil when this has been done.
3040 ;; The situation it copes with is this transformation:
3042 ;; OLD: { (.) {...........}
3043 ;; ^ ^
3044 ;; FROM HERE
3046 ;; NEW: { {....} (.) {.........
3047 ;; ^ ^ ^
3048 ;; LOWER BRACE PAIR HERE or HERE
3050 ;; This routine should be fast. Since it can get called a LOT, we maintain
3051 ;; `c-state-brace-pair-desert', a small cache of "failures", such that we
3052 ;; reduce the time wasted in repeated fruitless searches in brace deserts.
3053 (save-excursion
3054 (save-restriction
3055 (let* (new-cons
3056 (cache-pos (c-state-cache-top-lparen)) ; might be nil.
3057 (macro-start-or-from
3058 (progn (goto-char from)
3059 (c-beginning-of-macro)
3060 (point)))
3061 (bra ; Position of "{".
3062 ;; Don't start scanning in the middle of a CPP construct unless
3063 ;; it contains HERE - these constructs, in Emacs, are "commented
3064 ;; out" with category properties.
3065 (if (eq (c-get-char-property macro-start-or-from 'category)
3066 'c-cpp-delimiter)
3067 macro-start-or-from
3068 from))
3069 ce) ; Position of "}"
3070 (or upper-lim (setq upper-lim from))
3072 ;; If we're essentially repeating a fruitless search, just give up.
3073 (unless (and c-state-brace-pair-desert
3074 (eq cache-pos (car c-state-brace-pair-desert))
3075 (or (null (car c-state-brace-pair-desert))
3076 (> from (car c-state-brace-pair-desert)))
3077 (<= from (cdr c-state-brace-pair-desert)))
3078 ;; DESERT-LIM. Avoid repeated searching through the cached desert.
3079 (let ((desert-lim
3080 (and c-state-brace-pair-desert
3081 (eq cache-pos (car c-state-brace-pair-desert))
3082 (>= from (cdr c-state-brace-pair-desert))
3083 (cdr c-state-brace-pair-desert)))
3084 ;; CACHE-LIM. This limit will be necessary when an opening
3085 ;; paren at `cache-pos' has just had its matching close paren
3086 ;; inserted into the buffer. `cache-pos' continues to be a
3087 ;; search bound, even though the algorithm below would skip
3088 ;; over the new paren pair.
3089 (cache-lim (and cache-pos (< cache-pos from) cache-pos)))
3090 (narrow-to-region
3091 (cond
3092 ((and desert-lim cache-lim)
3093 (max desert-lim cache-lim))
3094 (desert-lim)
3095 (cache-lim)
3096 ((point-min)))
3097 ;; The top limit is EOB to ensure that `bra' is inside the
3098 ;; accessible part of the buffer at the next scan operation.
3099 (1+ (buffer-size))))
3101 ;; In the next pair of nested loops, the inner one moves back past a
3102 ;; pair of (mis-)matching parens or brackets; the outer one moves
3103 ;; back over a sequence of unmatched close brace/paren/bracket each
3104 ;; time round.
3105 (while
3106 (progn
3107 (c-safe
3108 (while
3109 (and (setq ce (c-sc-scan-lists bra -1 -1)) ; back past )/]/}; might signal
3110 (setq bra (c-sc-scan-lists ce -1 1)) ; back past (/[/{; might signal
3111 (or (> bra here) ;(> ce here)
3112 (and
3113 (< ce here)
3114 (or (not (eq (char-after bra) ?\{))
3115 (and (goto-char bra)
3116 (c-beginning-of-macro)
3117 (< (point) macro-start-or-from))))))))
3118 (and ce (< ce bra)))
3119 (setq bra ce)) ; If we just backed over an unbalanced closing
3120 ; brace, ignore it.
3122 (if (and ce (< ce here) (< bra ce) (eq (char-after bra) ?\{))
3123 ;; We've found the desired brace-pair.
3124 (progn
3125 (setq new-cons (cons bra (1+ ce)))
3126 (cond
3127 ((consp (car c-state-cache))
3128 (setcar c-state-cache new-cons))
3129 ((and (numberp (car c-state-cache)) ; probably never happens
3130 (< ce (car c-state-cache)))
3131 (setcdr c-state-cache
3132 (cons new-cons (cdr c-state-cache))))
3133 (t (setq c-state-cache (cons new-cons c-state-cache)))))
3135 ;; We haven't found a brace pair. Record this in the cache.
3136 (setq c-state-brace-pair-desert
3137 (cons (if (and ce (< bra ce) (> ce here)) ; {..} straddling HERE?
3139 (point-min))
3140 (min here from)))))))))
3142 (defsubst c-state-push-any-brace-pair (bra+1 macro-start-or-here)
3143 ;; If BRA+1 is nil, do nothing. Otherwise, BRA+1 is the buffer position
3144 ;; following a {, and that brace has a (mis-)matching } (or ]), and we
3145 ;; "push" "a" brace pair onto `c-state-cache'.
3147 ;; Here "push" means overwrite the top element if it's itself a brace-pair,
3148 ;; otherwise push it normally.
3150 ;; The brace pair we push is normally the one surrounding BRA+1, but if the
3151 ;; latter is inside a macro, not being a macro containing
3152 ;; MACRO-START-OR-HERE, we scan backwards through the buffer for a non-macro
3153 ;; base pair. This latter case is assumed to be rare.
3155 ;; Note: POINT is not preserved in this routine.
3156 (if bra+1
3157 (if (or (> bra+1 macro-start-or-here)
3158 (progn (goto-char bra+1)
3159 (not (c-beginning-of-macro))))
3160 (setq c-state-cache
3161 (cons (cons (1- bra+1)
3162 (c-sc-scan-lists bra+1 1 1))
3163 (if (consp (car c-state-cache))
3164 (cdr c-state-cache)
3165 c-state-cache)))
3166 ;; N.B. This defsubst codes one method for the simple, normal case,
3167 ;; and a more sophisticated, slower way for the general case. Don't
3168 ;; eliminate this defsubst - it's a speed optimization.
3169 (c-append-lower-brace-pair-to-state-cache (1- bra+1) (point-max)))))
3171 (defun c-append-to-state-cache (from here)
3172 ;; Scan the buffer from FROM to HERE, adding elements into `c-state-cache'
3173 ;; for braces etc. Return a candidate for `c-state-cache-good-pos'.
3175 ;; FROM must be after the latest brace/paren/bracket in `c-state-cache', if
3176 ;; any. Typically, it is immediately after it. It must not be inside a
3177 ;; literal.
3178 (let ((here-bol (c-point 'bol here))
3179 (macro-start-or-here
3180 (save-excursion (goto-char here)
3181 (if (c-beginning-of-macro)
3182 (point)
3183 here)))
3184 pa+1 ; pos just after an opening PAren (or brace).
3185 (ren+1 from) ; usually a pos just after an closing paREN etc.
3186 ; Is actually the pos. to scan for a (/{/[ from,
3187 ; which sometimes is after a silly )/}/].
3188 paren+1 ; Pos after some opening or closing paren.
3189 paren+1s ; A list of `paren+1's; used to determine a
3190 ; good-pos.
3191 bra+1 ; just after L bra-ce.
3192 bra+1s ; list of OLD values of bra+1.
3193 mstart) ; start of a macro.
3195 (save-excursion
3196 (save-restriction
3197 (narrow-to-region (point-min) here)
3198 ;; Each time round the following loop, we enter a successively deeper
3199 ;; level of brace/paren nesting. (Except sometimes we "continue at
3200 ;; the existing level".) `pa+1' is a pos inside an opening
3201 ;; brace/paren/bracket, usually just after it.
3202 (while
3203 (progn
3204 ;; Each time round the next loop moves forward over an opening then
3205 ;; a closing brace/bracket/paren. This loop is white hot, so it
3206 ;; plays ugly tricks to go fast. DON'T PUT ANYTHING INTO THIS
3207 ;; LOOP WHICH ISN'T ABSOLUTELY NECESSARY!!! It terminates when a
3208 ;; call of `scan-lists' signals an error, which happens when there
3209 ;; are no more b/b/p's to scan.
3210 (c-safe
3211 (while t
3212 (setq pa+1 (c-sc-scan-lists ren+1 1 -1) ; Into (/{/[; might signal
3213 paren+1s (cons pa+1 paren+1s))
3214 (setq ren+1 (c-sc-scan-lists pa+1 1 1)) ; Out of )/}/]; might signal
3215 (if (and (eq (char-before pa+1) ?{)) ; Check for a macro later.
3216 (setq bra+1 pa+1))
3217 (setcar paren+1s ren+1)))
3219 (if (and pa+1 (> pa+1 ren+1))
3220 ;; We've just entered a deeper nesting level.
3221 (progn
3222 ;; Insert the brace pair (if present) and the single open
3223 ;; paren/brace/bracket into `c-state-cache' It cannot be
3224 ;; inside a macro, except one around point, because of what
3225 ;; `c-neutralize-syntax-in-CPP' has done.
3226 (c-state-push-any-brace-pair bra+1 macro-start-or-here)
3227 ;; Insert the opening brace/bracket/paren position.
3228 (setq c-state-cache (cons (1- pa+1) c-state-cache))
3229 ;; Clear admin stuff for the next more nested part of the scan.
3230 (setq ren+1 pa+1 pa+1 nil bra+1 nil bra+1s nil)
3231 t) ; Carry on the loop
3233 ;; All open p/b/b's at this nesting level, if any, have probably
3234 ;; been closed by matching/mismatching ones. We're probably
3235 ;; finished - we just need to check for having found an
3236 ;; unmatched )/}/], which we ignore. Such a )/}/] can't be in a
3237 ;; macro, due the action of `c-neutralize-syntax-in-CPP'.
3238 (c-safe (setq ren+1 (c-sc-scan-lists ren+1 1 1)))))) ; acts as loop control.
3240 ;; Record the final, innermost, brace-pair if there is one.
3241 (c-state-push-any-brace-pair bra+1 macro-start-or-here)
3243 ;; Determine a good pos
3244 (while (and (setq paren+1 (car paren+1s))
3245 (> (if (> paren+1 macro-start-or-here)
3246 paren+1
3247 (goto-char paren+1)
3248 (setq mstart (and (c-beginning-of-macro)
3249 (point)))
3250 (or mstart paren+1))
3251 here-bol))
3252 (setq paren+1s (cdr paren+1s)))
3253 (cond
3254 ((and paren+1 mstart)
3255 (min paren+1 mstart))
3256 (paren+1)
3257 (t from))))))
3259 (defun c-remove-stale-state-cache (start-point here pps-point)
3260 ;; Remove stale entries from the `c-cache-state', i.e. those which will
3261 ;; not be in it when it is amended for position HERE. This may involve
3262 ;; replacing a CONS element for a brace pair containing HERE with its car.
3263 ;; Additionally, the "outermost" open-brace entry before HERE will be
3264 ;; converted to a cons if the matching close-brace is below HERE.
3266 ;; START-POINT is a "maximal" "safe position" - there must be no open
3267 ;; parens/braces/brackets between START-POINT and HERE.
3269 ;; As a second thing, calculate the result of parse-partial-sexp at
3270 ;; PPS-POINT, w.r.t. START-POINT. The motivation here is that
3271 ;; `c-state-cache-good-pos' may become PPS-POINT, but the caller may need to
3272 ;; adjust it to get outside a string/comment. (Sorry about this! The code
3273 ;; needs to be FAST).
3275 ;; Return a list (GOOD-POS SCAN-BACK-POS CONS-SEPARATED PPS-STATE), where
3276 ;; o - GOOD-POS is a position where the new value `c-state-cache' is known
3277 ;; to be good (we aim for this to be as high as possible);
3278 ;; o - SCAN-BACK-POS, if not nil, indicates there may be a brace pair
3279 ;; preceding POS which needs to be recorded in `c-state-cache'. It is a
3280 ;; position to scan backwards from. It is the position of the "{" of the
3281 ;; last element to be removed from `c-state-cache', when that elt is a
3282 ;; cons, otherwise nil.
3283 ;; o - CONS-SEPARATED is t when a cons element in `c-state-cache' has been
3284 ;; replaced by its car because HERE lies inside the brace pair represented
3285 ;; by the cons.
3286 ;; o - PPS-STATE is the parse-partial-sexp state at PPS-POINT.
3287 (save-excursion
3288 (save-restriction
3289 (narrow-to-region 1 (point-max))
3290 (let* ((in-macro-start ; start of macro containing HERE or nil.
3291 (save-excursion
3292 (goto-char here)
3293 (and (c-beginning-of-macro)
3294 (point))))
3295 (start-point-actual-macro-start ; Start of macro containing
3296 ; start-point or nil
3297 (and (< start-point here)
3298 (save-excursion
3299 (goto-char start-point)
3300 (and (c-beginning-of-macro)
3301 (point)))))
3302 (start-point-actual-macro-end ; End of this macro, (maybe
3303 ; HERE), or nil.
3304 (and start-point-actual-macro-start
3305 (save-excursion
3306 (goto-char start-point-actual-macro-start)
3307 (c-end-of-macro)
3308 (point))))
3309 pps-state ; Will be 9 or 10 elements long.
3311 upper-lim ; ,beyond which `c-state-cache' entries are removed
3312 scan-back-pos
3313 cons-separated
3314 pair-beg pps-point-state target-depth)
3316 ;; Remove entries beyond HERE. Also remove any entries inside
3317 ;; a macro, unless HERE is in the same macro.
3318 (setq upper-lim
3319 (if (or (null c-state-old-cpp-beg)
3320 (and (> here c-state-old-cpp-beg)
3321 (< here c-state-old-cpp-end)))
3322 here
3323 (min here c-state-old-cpp-beg)))
3324 (while (and c-state-cache (>= (c-state-cache-top-lparen) upper-lim))
3325 (setq scan-back-pos (car-safe (car c-state-cache)))
3326 (setq c-state-cache (cdr c-state-cache)))
3328 ;; If `upper-lim' is inside the last recorded brace pair, remove its
3329 ;; RBrace and indicate we'll need to search backwards for a previous
3330 ;; brace pair.
3331 (when (and c-state-cache
3332 (consp (car c-state-cache))
3333 (> (cdar c-state-cache) upper-lim))
3334 (setcar c-state-cache (caar c-state-cache))
3335 (setq scan-back-pos (car c-state-cache)
3336 cons-separated t))
3338 ;; The next loop jumps forward out of a nested level of parens each
3339 ;; time round; the corresponding elements in `c-state-cache' are
3340 ;; removed. `pos' is just after the brace-pair or the open paren at
3341 ;; (car c-state-cache). There can be no open parens/braces/brackets
3342 ;; between `start-point'/`start-point-actual-macro-start' and HERE,
3343 ;; due to the interface spec to this function.
3344 (setq pos (if (and start-point-actual-macro-end
3345 (not (eq start-point-actual-macro-start
3346 in-macro-start)))
3347 (1+ start-point-actual-macro-end) ; get outside the macro as
3348 ; marked by a `category' text property.
3349 start-point))
3350 (goto-char pos)
3351 (while (and c-state-cache
3352 (or (numberp (car c-state-cache)) ; Have we a { at all?
3353 (cdr c-state-cache))
3354 (< (point) here))
3355 (cond
3356 ((null pps-state) ; first time through
3357 (setq target-depth -1))
3358 ((eq (car pps-state) target-depth) ; found closing ),},]
3359 (setq target-depth (1- (car pps-state))))
3360 ;; Do nothing when we've merely reached pps-point.
3363 ;; Scan!
3364 (setq pps-state
3365 (c-sc-parse-partial-sexp
3366 (point) (if (< (point) pps-point) pps-point here)
3367 target-depth
3368 nil pps-state))
3370 (if (= (point) pps-point)
3371 (setq pps-point-state pps-state))
3373 (when (eq (car pps-state) target-depth)
3374 (setq pos (point)) ; POS is now just after an R-paren/brace.
3375 (cond
3376 ((and (consp (car c-state-cache))
3377 (eq (point) (cdar c-state-cache)))
3378 ;; We've just moved out of the paren pair containing the brace-pair
3379 ;; at (car c-state-cache). `pair-beg' is where the open paren is,
3380 ;; and is potentially where the open brace of a cons in
3381 ;; c-state-cache will be.
3382 (setq pair-beg (car-safe (cdr c-state-cache))
3383 c-state-cache (cdr-safe (cdr c-state-cache)))) ; remove {}pair + containing Lparen.
3384 ((numberp (car c-state-cache))
3385 (setq pair-beg (car c-state-cache)
3386 c-state-cache (cdr c-state-cache))) ; remove this
3387 ; containing Lparen
3388 ((numberp (cadr c-state-cache))
3389 (setq pair-beg (cadr c-state-cache)
3390 c-state-cache (cddr c-state-cache))) ; Remove a paren pair
3391 ; together with enclosed brace pair.
3392 ;; (t nil) ; Ignore an unmated Rparen.
3395 (if (< (point) pps-point)
3396 (setq pps-state (c-sc-parse-partial-sexp
3397 (point) pps-point
3398 nil nil ; TARGETDEPTH, STOPBEFORE
3399 pps-state)))
3401 ;; If the last paren pair we moved out of was actually a brace pair,
3402 ;; insert it into `c-state-cache'.
3403 (when (and pair-beg (eq (char-after pair-beg) ?{))
3404 (if (consp (car-safe c-state-cache))
3405 (setq c-state-cache (cdr c-state-cache)))
3406 (setq c-state-cache (cons (cons pair-beg pos)
3407 c-state-cache)))
3409 (list pos scan-back-pos cons-separated pps-state)))))
3411 (defun c-remove-stale-state-cache-backwards (here)
3412 ;; Strip stale elements of `c-state-cache' by moving backwards through the
3413 ;; buffer, and inform the caller of the scenario detected.
3415 ;; HERE is the position we're setting `c-state-cache' for.
3416 ;; CACHE-POS (a locally bound variable) is just after the latest recorded
3417 ;; position in `c-state-cache' before HERE, or a position at or near
3418 ;; point-min which isn't in a literal.
3420 ;; This function must only be called only when (> `c-state-cache-good-pos'
3421 ;; HERE). Usually the gap between CACHE-POS and HERE is large. It is thus
3422 ;; optimized to eliminate (or minimize) scanning between these two
3423 ;; positions.
3425 ;; Return a three element list (GOOD-POS SCAN-BACK-POS FWD-FLAG), where:
3426 ;; o - GOOD-POS is a "good position", where `c-state-cache' is valid, or
3427 ;; could become so after missing elements are inserted into
3428 ;; `c-state-cache'. This is JUST AFTER an opening or closing
3429 ;; brace/paren/bracket which is already in `c-state-cache' or just before
3430 ;; one otherwise. exceptionally (when there's no such b/p/b handy) the BOL
3431 ;; before `here''s line, or the start of the literal containing it.
3432 ;; o - SCAN-BACK-POS, if non-nil, indicates there may be a brace pair
3433 ;; preceding POS which isn't recorded in `c-state-cache'. It is a position
3434 ;; to scan backwards from.
3435 ;; o - FWD-FLAG, if non-nil, indicates there may be parens/braces between
3436 ;; POS and HERE which aren't recorded in `c-state-cache'.
3438 ;; The comments in this defun use "paren" to mean parenthesis or square
3439 ;; bracket (as contrasted with a brace), and "(" and ")" likewise.
3441 ;; . {..} (..) (..) ( .. { } ) (...) ( .... . ..)
3442 ;; | | | | | |
3443 ;; CP E here D C good
3444 (let ((cache-pos (c-get-cache-scan-pos here)) ; highest position below HERE in cache (or 1)
3445 (pos c-state-cache-good-pos)
3446 pa ren ; positions of "(" and ")"
3447 dropped-cons ; whether the last element dropped from `c-state-cache'
3448 ; was a cons (representing a brace-pair)
3449 good-pos ; see above.
3450 lit ; (START . END) of a literal containing some point.
3451 here-lit-start here-lit-end ; bounds of literal containing `here'
3452 ; or `here' itself.
3453 here- here+ ; start/end of macro around HERE, or HERE
3454 (here-bol (c-point 'bol here))
3455 (too-far-back (max (- here c-state-cache-too-far) (point-min))))
3457 ;; Remove completely irrelevant entries from `c-state-cache'.
3458 (while (and c-state-cache
3459 (>= (setq pa (c-state-cache-top-lparen)) here))
3460 (setq dropped-cons (consp (car c-state-cache)))
3461 (setq c-state-cache (cdr c-state-cache))
3462 (setq pos pa))
3463 ;; At this stage, (>= pos here);
3464 ;; (< (c-state-cache-top-lparen) here) (or is nil).
3466 (cond
3467 ((and (consp (car c-state-cache))
3468 (> (cdar c-state-cache) here))
3469 ;; CASE 1: The top of the cache is a brace pair which now encloses
3470 ;; `here'. As good-pos, return the address. of the "{". Since we've no
3471 ;; knowledge of what's inside these braces, we have no alternative but
3472 ;; to direct the caller to scan the buffer from the opening brace.
3473 (setq pos (caar c-state-cache))
3474 (setcar c-state-cache pos)
3475 (list (1+ pos) pos t)) ; return value. We've just converted a brace pair
3476 ; entry into a { entry, so the caller needs to
3477 ; search for a brace pair before the {.
3479 ;; `here' might be inside a literal. Check for this.
3480 ((progn
3481 (setq lit (c-state-literal-at here)
3482 here-lit-start (or (car lit) here)
3483 here-lit-end (or (cdr lit) here))
3484 ;; Has `here' just "newly entered" a macro?
3485 (save-excursion
3486 (goto-char here-lit-start)
3487 (if (and (c-beginning-of-macro)
3488 (or (null c-state-old-cpp-beg)
3489 (not (= (point) c-state-old-cpp-beg))))
3490 (progn
3491 (setq here- (point))
3492 (c-end-of-macro)
3493 (setq here+ (point)))
3494 (setq here- here-lit-start
3495 here+ here-lit-end)))
3497 ;; `here' might be nested inside any depth of parens (or brackets but
3498 ;; not braces). Scan backwards to find the outermost such opening
3499 ;; paren, if there is one. This will be the scan position to return.
3500 (save-restriction
3501 (narrow-to-region cache-pos (point-max))
3502 (setq pos (c-state-balance-parens-backwards here- here+ pos)))
3503 nil)) ; for the cond
3505 ((< pos here-lit-start)
3506 ;; CASE 2: Address of outermost ( or [ which now encloses `here', but
3507 ;; didn't enclose the (previous) `c-state-cache-good-pos'. If there is
3508 ;; a brace pair preceding this, it will already be in `c-state-cache',
3509 ;; unless there was a brace pair after it, i.e. there'll only be one to
3510 ;; scan for if we've just deleted one.
3511 (list pos (and dropped-cons pos) t)) ; Return value.
3513 ;; `here' isn't enclosed in a (previously unrecorded) bracket/paren.
3514 ;; Further forward scanning isn't needed, but we still need to find a
3515 ;; GOOD-POS. Step out of all enclosing "("s on HERE's line.
3516 ((progn
3517 (save-restriction
3518 (narrow-to-region here-bol (point-max))
3519 (setq pos here-lit-start)
3520 (c-safe (while (setq pa (c-sc-scan-lists pos -1 1))
3521 (setq pos pa)))) ; might signal
3522 nil)) ; for the cond
3524 ((save-restriction
3525 (narrow-to-region too-far-back (point-max))
3526 (setq ren (c-safe (c-sc-scan-lists pos -1 -1))))
3527 ;; CASE 3: After a }/)/] before `here''s BOL.
3528 (list (1+ ren) (and dropped-cons pos) nil)) ; Return value
3530 ((progn (setq good-pos (c-state-lit-beg (c-point 'bopl here-bol)))
3531 (>= cache-pos good-pos))
3532 ;; CASE 3.5: Just after an existing entry in `c-state-cache' on `here''s
3533 ;; line or the previous line.
3534 (list cache-pos nil nil))
3537 ;; CASE 4; Best of a bad job: BOL before `here-bol', or beginning of
3538 ;; literal containing it.
3539 (list good-pos (and dropped-cons good-pos) nil)))))
3542 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
3543 ;; Externally visible routines.
3545 (defun c-state-cache-init ()
3546 (setq c-state-cache nil
3547 c-state-cache-good-pos 1
3548 c-state-nonlit-pos-cache nil
3549 c-state-nonlit-pos-cache-limit 1
3550 c-state-semi-nonlit-pos-cache nil
3551 c-state-semi-nonlit-pos-cache-limit 1
3552 c-state-brace-pair-desert nil
3553 c-state-point-min 1
3554 c-state-point-min-lit-type nil
3555 c-state-point-min-lit-start nil
3556 c-state-min-scan-pos 1
3557 c-state-old-cpp-beg nil
3558 c-state-old-cpp-end nil)
3559 (c-state-mark-point-min-literal))
3561 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
3562 ;; Debugging routines to dump `c-state-cache' in a "replayable" form.
3563 ;; (defmacro c-sc-de (elt) ; "c-state-cache-dump-element"
3564 ;; `(format ,(concat "(setq " (symbol-name elt) " %s) ") ,elt))
3565 ;; (defmacro c-sc-qde (elt) ; "c-state-cache-quote-dump-element"
3566 ;; `(format ,(concat "(setq " (symbol-name elt) " '%s) ") ,elt))
3567 ;; (defun c-state-dump ()
3568 ;; ;; For debugging.
3569 ;; ;(message
3570 ;; (concat
3571 ;; (c-sc-qde c-state-cache)
3572 ;; (c-sc-de c-state-cache-good-pos)
3573 ;; (c-sc-qde c-state-nonlit-pos-cache)
3574 ;; (c-sc-de c-state-nonlit-pos-cache-limit)
3575 ;; (c-sc-qde c-state-brace-pair-desert)
3576 ;; (c-sc-de c-state-point-min)
3577 ;; (c-sc-de c-state-point-min-lit-type)
3578 ;; (c-sc-de c-state-point-min-lit-start)
3579 ;; (c-sc-de c-state-min-scan-pos)
3580 ;; (c-sc-de c-state-old-cpp-beg)
3581 ;; (c-sc-de c-state-old-cpp-end)))
3582 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
3584 (defun c-invalidate-state-cache-1 (here)
3585 ;; Invalidate all info on `c-state-cache' that applies to the buffer at HERE
3586 ;; or higher and set `c-state-cache-good-pos' accordingly. The cache is
3587 ;; left in a consistent state.
3589 ;; This is much like `c-whack-state-after', but it never changes a paren
3590 ;; pair element into an open paren element. Doing that would mean that the
3591 ;; new open paren wouldn't have the required preceding paren pair element.
3593 ;; This function is called from c-before-change.
3595 ;; The caches of non-literals:
3596 ;; Note that we use "<=" for the possibility of the second char of a two-char
3597 ;; comment opener being typed; this would invalidate any cache position at
3598 ;; HERE.
3599 (if (<= here c-state-nonlit-pos-cache-limit)
3600 (setq c-state-nonlit-pos-cache-limit (1- here)))
3601 (c-truncate-semi-nonlit-pos-cache here)
3603 ;; `c-state-cache':
3604 ;; Case 1: if `here' is in a literal containing point-min, everything
3605 ;; becomes (or is already) nil.
3606 (if (or (null c-state-cache-good-pos)
3607 (< here (c-state-get-min-scan-pos)))
3608 (setq c-state-cache nil
3609 c-state-cache-good-pos nil
3610 c-state-min-scan-pos nil)
3612 ;; Truncate `c-state-cache' and set `c-state-cache-good-pos' to a value
3613 ;; below `here'. To maintain its consistency, we may need to insert a new
3614 ;; brace pair.
3615 (let ((here-bol (c-point 'bol here))
3616 too-high-pa ; recorded {/(/[ next above or just below here, or nil.
3617 dropped-cons ; was the last removed element a brace pair?
3619 ;; The easy bit - knock over-the-top bits off `c-state-cache'.
3620 (while (and c-state-cache
3621 (>= (setq pa (c-state-cache-top-paren)) here))
3622 (setq dropped-cons (consp (car c-state-cache))
3623 too-high-pa (c-state-cache-top-lparen)
3624 c-state-cache (cdr c-state-cache)))
3626 ;; Do we need to add in an earlier brace pair, having lopped one off?
3627 (if (and dropped-cons
3628 (<= too-high-pa here))
3629 (c-append-lower-brace-pair-to-state-cache too-high-pa here here-bol))
3630 (setq c-state-cache-good-pos (or (c-state-cache-after-top-paren)
3631 (c-state-get-min-scan-pos)))))
3633 ;; The brace-pair desert marker:
3634 (when (car c-state-brace-pair-desert)
3635 (if (< here (car c-state-brace-pair-desert))
3636 (setq c-state-brace-pair-desert nil)
3637 (if (< here (cdr c-state-brace-pair-desert))
3638 (setcdr c-state-brace-pair-desert here)))))
3640 (defun c-parse-state-1 ()
3641 ;; Find and record all noteworthy parens between some good point earlier in
3642 ;; the file and point. That good point is at least the beginning of the
3643 ;; top-level construct we are in, or the beginning of the preceding
3644 ;; top-level construct if we aren't in one.
3646 ;; The returned value is a list of the noteworthy parens with the last one
3647 ;; first. If an element in the list is an integer, it's the position of an
3648 ;; open paren (of any type) which has not been closed before the point. If
3649 ;; an element is a cons, it gives the position of a closed BRACE paren
3650 ;; pair[*]; the car is the start brace position and the cdr is the position
3651 ;; following the closing brace. Only the last closed brace paren pair
3652 ;; before each open paren and before the point is recorded, and thus the
3653 ;; state never contains two cons elements in succession. When a close brace
3654 ;; has no matching open brace (e.g., the matching brace is outside the
3655 ;; visible region), it is not represented in the returned value.
3657 ;; [*] N.B. The close "brace" might be a mismatching close bracket or paren.
3658 ;; This defun explicitly treats mismatching parens/braces/brackets as
3659 ;; matching. It is the open brace which makes it a "brace" pair.
3661 ;; If POINT is within a macro, open parens and brace pairs within
3662 ;; THIS macro MIGHT be recorded. This depends on whether their
3663 ;; syntactic properties have been suppressed by
3664 ;; `c-neutralize-syntax-in-CPP'. This might need fixing (2008-12-11).
3666 ;; Currently no characters which are given paren syntax with the
3667 ;; syntax-table property are recorded, i.e. angle bracket arglist
3668 ;; parens are never present here. Note that this might change.
3670 ;; BUG: This function doesn't cope entirely well with unbalanced
3671 ;; parens in macros. (2008-12-11: this has probably been resolved
3672 ;; by the function `c-neutralize-syntax-in-CPP'.) E.g. in the
3673 ;; following case the brace before the macro isn't balanced with the
3674 ;; one after it:
3676 ;; {
3677 ;; #define X {
3678 ;; }
3680 ;; Note to maintainers: this function DOES get called with point
3681 ;; within comments and strings, so don't assume it doesn't!
3683 ;; This function might do hidden buffer changes.
3684 (let* ((here (point))
3685 (here-bopl (c-point 'bopl))
3686 strategy ; 'forward, 'backward etc..
3687 ;; Candidate positions to start scanning from:
3688 cache-pos ; highest position below HERE already existing in
3689 ; cache (or 1).
3690 good-pos
3691 start-point ; (when scanning forward) a place below HERE where there
3692 ; are no open parens/braces between it and HERE.
3693 bopl-state
3695 cons-separated
3696 scan-backward-pos scan-forward-p) ; used for 'backward.
3697 ;; If POINT-MIN has changed, adjust the cache
3698 (unless (= (point-min) c-state-point-min)
3699 (c-renarrow-state-cache))
3701 ;; Strategy?
3702 (setq res (c-parse-state-get-strategy here c-state-cache-good-pos)
3703 strategy (car res)
3704 start-point (cadr res))
3706 (when (eq strategy 'BOD)
3707 (setq c-state-cache nil
3708 c-state-cache-good-pos start-point))
3710 ;; SCAN!
3711 (cond
3712 ((memq strategy '(forward back-and-forward BOD))
3713 (setq res (c-remove-stale-state-cache start-point here here-bopl))
3714 (setq cache-pos (car res)
3715 scan-backward-pos (cadr res)
3716 cons-separated (car (cddr res))
3717 bopl-state (cadr (cddr res))) ; will be nil if (< here-bopl
3718 ; start-point)
3719 (if (and scan-backward-pos
3720 (or cons-separated (eq strategy 'forward))) ;scan-backward-pos
3721 (c-append-lower-brace-pair-to-state-cache scan-backward-pos here))
3722 (setq good-pos
3723 (c-append-to-state-cache cache-pos here))
3724 (setq c-state-cache-good-pos
3725 (if (and bopl-state
3726 (< good-pos (- here c-state-cache-too-far)))
3727 (c-state-cache-non-literal-place here-bopl bopl-state)
3728 good-pos)))
3730 ((eq strategy 'backward)
3731 (setq res (c-remove-stale-state-cache-backwards here)
3732 good-pos (car res)
3733 scan-backward-pos (cadr res)
3734 scan-forward-p (car (cddr res)))
3735 (if scan-backward-pos
3736 (c-append-lower-brace-pair-to-state-cache scan-backward-pos here))
3737 (setq c-state-cache-good-pos
3738 (if scan-forward-p
3739 (c-append-to-state-cache good-pos here)
3740 good-pos)))
3742 (t ; (eq strategy 'IN-LIT)
3743 (setq c-state-cache nil
3744 c-state-cache-good-pos nil))))
3746 c-state-cache)
3748 (defun c-invalidate-state-cache (here)
3749 ;; This is a wrapper over `c-invalidate-state-cache-1'.
3751 ;; It suppresses the syntactic effect of the < and > (template) brackets and
3752 ;; of all parens in preprocessor constructs, except for any such construct
3753 ;; containing point. We can then call `c-invalidate-state-cache-1' without
3754 ;; worrying further about macros and template delimiters.
3755 (if (eval-when-compile (memq 'category-properties c-emacs-features))
3756 ;; Emacs
3757 (c-with-<->-as-parens-suppressed
3758 (if (and c-state-old-cpp-beg
3759 (< c-state-old-cpp-beg here))
3760 (c-with-all-but-one-cpps-commented-out
3761 c-state-old-cpp-beg
3762 c-state-old-cpp-end
3763 (c-invalidate-state-cache-1 here))
3764 (c-with-cpps-commented-out
3765 (c-invalidate-state-cache-1 here))))
3766 ;; XEmacs
3767 (c-invalidate-state-cache-1 here)))
3769 (defmacro c-state-maybe-marker (place marker)
3770 ;; If PLACE is non-nil, return a marker marking it, otherwise nil.
3771 ;; We (re)use MARKER.
3772 `(and ,place
3773 (or ,marker (setq ,marker (make-marker)))
3774 (set-marker ,marker ,place)))
3776 (defun c-parse-state ()
3777 ;; This is a wrapper over `c-parse-state-1'. See that function for a
3778 ;; description of the functionality and return value.
3780 ;; It suppresses the syntactic effect of the < and > (template) brackets and
3781 ;; of all parens in preprocessor constructs, except for any such construct
3782 ;; containing point. We can then call `c-parse-state-1' without worrying
3783 ;; further about macros and template delimiters.
3784 (let (here-cpp-beg here-cpp-end)
3785 (save-excursion
3786 (when (c-beginning-of-macro)
3787 (setq here-cpp-beg (point))
3788 (unless
3789 (> (setq here-cpp-end (c-syntactic-end-of-macro))
3790 here-cpp-beg)
3791 (setq here-cpp-beg nil here-cpp-end nil))))
3792 ;; FIXME!!! Put in a `condition-case' here to protect the integrity of the
3793 ;; subsystem.
3794 (prog1
3795 (if (eval-when-compile (memq 'category-properties c-emacs-features))
3796 ;; Emacs
3797 (c-with-<->-as-parens-suppressed
3798 (if (and here-cpp-beg (> here-cpp-end here-cpp-beg))
3799 (c-with-all-but-one-cpps-commented-out
3800 here-cpp-beg here-cpp-end
3801 (c-parse-state-1))
3802 (c-with-cpps-commented-out
3803 (c-parse-state-1))))
3804 ;; XEmacs
3805 (c-parse-state-1))
3806 (setq c-state-old-cpp-beg
3807 (c-state-maybe-marker here-cpp-beg c-state-old-cpp-beg-marker)
3808 c-state-old-cpp-end
3809 (c-state-maybe-marker here-cpp-end c-state-old-cpp-end-marker)))))
3811 ;; Debug tool to catch cache inconsistencies. This is called from
3812 ;; 000tests.el.
3813 (defvar c-debug-parse-state nil)
3814 (unless (fboundp 'c-real-parse-state)
3815 (fset 'c-real-parse-state (symbol-function 'c-parse-state)))
3816 (cc-bytecomp-defun c-real-parse-state)
3818 (defvar c-parse-state-point nil)
3819 (defvar c-parse-state-state nil)
3820 (make-variable-buffer-local 'c-parse-state-state)
3821 (defun c-record-parse-state-state ()
3822 (setq c-parse-state-point (point))
3823 (when (markerp (cdr (assq 'c-state-old-cpp-beg c-parse-state-state)))
3824 (move-marker (cdr (assq 'c-state-old-cpp-beg c-parse-state-state)) nil)
3825 (move-marker (cdr (assq 'c-state-old-cpp-end c-parse-state-state)) nil))
3826 (setq c-parse-state-state
3827 (mapcar
3828 (lambda (arg)
3829 (let ((val (symbol-value arg)))
3830 (cons arg
3831 (cond ((consp val) (copy-tree val))
3832 ((markerp val) (copy-marker val))
3833 (t val)))))
3834 '(c-state-cache
3835 c-state-cache-good-pos
3836 c-state-nonlit-pos-cache
3837 c-state-nonlit-pos-cache-limit
3838 c-state-semi-nonlit-pos-cache
3839 c-state-semi-nonlit-pos-cache-limit
3840 c-state-brace-pair-desert
3841 c-state-point-min
3842 c-state-point-min-lit-type
3843 c-state-point-min-lit-start
3844 c-state-min-scan-pos
3845 c-state-old-cpp-beg
3846 c-state-old-cpp-end
3847 c-parse-state-point))))
3848 (defun c-replay-parse-state-state ()
3849 (message "%s"
3850 (concat "(setq "
3851 (mapconcat
3852 (lambda (arg)
3853 (format "%s %s%s" (car arg)
3854 (if (atom (cdr arg)) "" "'")
3855 (if (markerp (cdr arg))
3856 (format "(copy-marker %s)" (marker-position (cdr arg)))
3857 (cdr arg))))
3858 c-parse-state-state " ")
3859 ")")))
3861 (defun c-debug-parse-state-double-cons (state)
3862 (let (state-car conses-not-ok)
3863 (while state
3864 (setq state-car (car state)
3865 state (cdr state))
3866 (if (and (consp state-car)
3867 (consp (car state)))
3868 (setq conses-not-ok t)))
3869 conses-not-ok))
3871 (defun c-debug-parse-state ()
3872 (let ((here (point)) (min-point (point-min)) (res1 (c-real-parse-state)) res2)
3873 (let ((c-state-cache nil)
3874 (c-state-cache-good-pos 1)
3875 (c-state-nonlit-pos-cache nil)
3876 (c-state-nonlit-pos-cache-limit 1)
3877 (c-state-brace-pair-desert nil)
3878 (c-state-point-min 1)
3879 (c-state-point-min-lit-type nil)
3880 (c-state-point-min-lit-start nil)
3881 (c-state-min-scan-pos 1)
3882 (c-state-old-cpp-beg nil)
3883 (c-state-old-cpp-end nil))
3884 (setq res2 (c-real-parse-state)))
3885 (unless (equal res1 res2)
3886 ;; The cache can actually go further back due to the ad-hoc way
3887 ;; the first paren is found, so try to whack off a bit of its
3888 ;; start before complaining.
3889 ;; (save-excursion
3890 ;; (goto-char (or (c-least-enclosing-brace res2) (point)))
3891 ;; (c-beginning-of-defun-1)
3892 ;; (while (not (or (bobp) (eq (char-after) ?{)))
3893 ;; (c-beginning-of-defun-1))
3894 ;; (unless (equal (c-whack-state-before (point) res1) res2)
3895 ;; (message (concat "c-parse-state inconsistency at %s: "
3896 ;; "using cache: %s, from scratch: %s")
3897 ;; here res1 res2)))
3898 (message (concat "c-parse-state inconsistency at %s: "
3899 "using cache: %s, from scratch: %s. POINT-MIN: %s")
3900 here res1 res2 min-point)
3901 (message "Old state:")
3902 (c-replay-parse-state-state))
3904 (when (c-debug-parse-state-double-cons res1)
3905 (message "c-parse-state INVALIDITY at %s: %s"
3906 here res1)
3907 (message "Old state:")
3908 (c-replay-parse-state-state))
3910 (c-record-parse-state-state)
3911 res2 ; res1 correct a cascading series of errors ASAP
3914 (defun c-toggle-parse-state-debug (&optional arg)
3915 (interactive "P")
3916 (setq c-debug-parse-state (c-calculate-state arg c-debug-parse-state))
3917 (fset 'c-parse-state (symbol-function (if c-debug-parse-state
3918 'c-debug-parse-state
3919 'c-real-parse-state)))
3920 (c-keep-region-active)
3921 (message "c-debug-parse-state %sabled"
3922 (if c-debug-parse-state "en" "dis")))
3923 (when c-debug-parse-state
3924 (c-toggle-parse-state-debug 1))
3927 (defun c-whack-state-before (bufpos paren-state)
3928 ;; Whack off any state information from PAREN-STATE which lies
3929 ;; before BUFPOS. Not destructive on PAREN-STATE.
3930 (let* ((newstate (list nil))
3931 (ptr newstate)
3932 car)
3933 (while paren-state
3934 (setq car (car paren-state)
3935 paren-state (cdr paren-state))
3936 (if (< (if (consp car) (car car) car) bufpos)
3937 (setq paren-state nil)
3938 (setcdr ptr (list car))
3939 (setq ptr (cdr ptr))))
3940 (cdr newstate)))
3942 (defun c-whack-state-after (bufpos paren-state)
3943 ;; Whack off any state information from PAREN-STATE which lies at or
3944 ;; after BUFPOS. Not destructive on PAREN-STATE.
3945 (catch 'done
3946 (while paren-state
3947 (let ((car (car paren-state)))
3948 (if (consp car)
3949 ;; just check the car, because in a balanced brace
3950 ;; expression, it must be impossible for the corresponding
3951 ;; close brace to be before point, but the open brace to
3952 ;; be after.
3953 (if (<= bufpos (car car))
3954 nil ; whack it off
3955 (if (< bufpos (cdr car))
3956 ;; its possible that the open brace is before
3957 ;; bufpos, but the close brace is after. In that
3958 ;; case, convert this to a non-cons element. The
3959 ;; rest of the state is before bufpos, so we're
3960 ;; done.
3961 (throw 'done (cons (car car) (cdr paren-state)))
3962 ;; we know that both the open and close braces are
3963 ;; before bufpos, so we also know that everything else
3964 ;; on state is before bufpos.
3965 (throw 'done paren-state)))
3966 (if (<= bufpos car)
3967 nil ; whack it off
3968 ;; it's before bufpos, so everything else should too.
3969 (throw 'done paren-state)))
3970 (setq paren-state (cdr paren-state)))
3971 nil)))
3973 (defun c-most-enclosing-brace (paren-state &optional bufpos)
3974 ;; Return the bufpos of the innermost enclosing open paren before
3975 ;; bufpos, or nil if none was found.
3976 (let (enclosingp)
3977 (or bufpos (setq bufpos 134217727))
3978 (while paren-state
3979 (setq enclosingp (car paren-state)
3980 paren-state (cdr paren-state))
3981 (if (or (consp enclosingp)
3982 (>= enclosingp bufpos))
3983 (setq enclosingp nil)
3984 (setq paren-state nil)))
3985 enclosingp))
3987 (defun c-least-enclosing-brace (paren-state)
3988 ;; Return the bufpos of the outermost enclosing open paren, or nil
3989 ;; if none was found.
3990 (let (pos elem)
3991 (while paren-state
3992 (setq elem (car paren-state)
3993 paren-state (cdr paren-state))
3994 (if (integerp elem)
3995 (setq pos elem)))
3996 pos))
3998 (defun c-safe-position (bufpos paren-state)
3999 ;; Return the closest "safe" position recorded on PAREN-STATE that
4000 ;; is higher up than BUFPOS. Return nil if PAREN-STATE doesn't
4001 ;; contain any. Return nil if BUFPOS is nil, which is useful to
4002 ;; find the closest limit before a given limit that might be nil.
4004 ;; A "safe" position is a position at or after a recorded open
4005 ;; paren, or after a recorded close paren. The returned position is
4006 ;; thus either the first position after a close brace, or the first
4007 ;; position after an enclosing paren, or at the enclosing paren in
4008 ;; case BUFPOS is immediately after it.
4009 (when bufpos
4010 (let (elem)
4011 (catch 'done
4012 (while paren-state
4013 (setq elem (car paren-state))
4014 (if (consp elem)
4015 (cond ((< (cdr elem) bufpos)
4016 (throw 'done (cdr elem)))
4017 ((< (car elem) bufpos)
4018 ;; See below.
4019 (throw 'done (min (1+ (car elem)) bufpos))))
4020 (if (< elem bufpos)
4021 ;; elem is the position at and not after the opening paren, so
4022 ;; we can go forward one more step unless it's equal to
4023 ;; bufpos. This is useful in some cases avoid an extra paren
4024 ;; level between the safe position and bufpos.
4025 (throw 'done (min (1+ elem) bufpos))))
4026 (setq paren-state (cdr paren-state)))))))
4028 (defun c-beginning-of-syntax ()
4029 ;; This is used for `font-lock-beginning-of-syntax-function'. It
4030 ;; goes to the closest previous point that is known to be outside
4031 ;; any string literal or comment. `c-state-cache' is used if it has
4032 ;; a position in the vicinity.
4033 (let* ((paren-state c-state-cache)
4034 elem
4036 (pos (catch 'done
4037 ;; Note: Similar code in `c-safe-position'. The
4038 ;; difference is that we accept a safe position at
4039 ;; the point and don't bother to go forward past open
4040 ;; parens.
4041 (while paren-state
4042 (setq elem (car paren-state))
4043 (if (consp elem)
4044 (cond ((<= (cdr elem) (point))
4045 (throw 'done (cdr elem)))
4046 ((<= (car elem) (point))
4047 (throw 'done (car elem))))
4048 (if (<= elem (point))
4049 (throw 'done elem)))
4050 (setq paren-state (cdr paren-state)))
4051 (point-min))))
4053 (if (> pos (- (point) 4000))
4054 (goto-char pos)
4055 ;; The position is far back. Try `c-beginning-of-defun-1'
4056 ;; (although we can't be entirely sure it will go to a position
4057 ;; outside a comment or string in current emacsen). FIXME:
4058 ;; Consult `syntax-ppss' here.
4059 (c-beginning-of-defun-1)
4060 (if (< (point) pos)
4061 (goto-char pos)))))
4064 ;; Tools for scanning identifiers and other tokens.
4066 (defun c-on-identifier ()
4067 "Return non-nil if the point is on or directly after an identifier.
4068 Keywords are recognized and not considered identifiers. If an
4069 identifier is detected, the returned value is its starting position.
4070 If an identifier ends at the point and another begins at it \(can only
4071 happen in Pike) then the point for the preceding one is returned.
4073 Note that this function might do hidden buffer changes. See the
4074 comment at the start of cc-engine.el for more info."
4076 ;; FIXME: Shouldn't this function handle "operator" in C++?
4078 (save-excursion
4079 (skip-syntax-backward "w_")
4083 ;; Check for a normal (non-keyword) identifier.
4084 (and (looking-at c-symbol-start)
4085 (not (looking-at c-keywords-regexp))
4086 (point))
4088 (when (c-major-mode-is 'pike-mode)
4089 ;; Handle the `<operator> syntax in Pike.
4090 (let ((pos (point)))
4091 (skip-chars-backward "-!%&*+/<=>^|~[]()")
4092 (and (if (< (skip-chars-backward "`") 0)
4094 (goto-char pos)
4095 (eq (char-after) ?\`))
4096 (looking-at c-symbol-key)
4097 (>= (match-end 0) pos)
4098 (point))))
4100 ;; Handle the "operator +" syntax in C++.
4101 (when (and c-overloadable-operators-regexp
4102 (= (c-backward-token-2 0) 0))
4104 (cond ((and (looking-at c-overloadable-operators-regexp)
4105 (or (not c-opt-op-identifier-prefix)
4106 (and (= (c-backward-token-2 1) 0)
4107 (looking-at c-opt-op-identifier-prefix))))
4108 (point))
4110 ((save-excursion
4111 (and c-opt-op-identifier-prefix
4112 (looking-at c-opt-op-identifier-prefix)
4113 (= (c-forward-token-2 1) 0)
4114 (looking-at c-overloadable-operators-regexp)))
4115 (point))))
4119 (defsubst c-simple-skip-symbol-backward ()
4120 ;; If the point is at the end of a symbol then skip backward to the
4121 ;; beginning of it. Don't move otherwise. Return non-nil if point
4122 ;; moved.
4124 ;; This function might do hidden buffer changes.
4125 (or (< (skip-syntax-backward "w_") 0)
4126 (and (c-major-mode-is 'pike-mode)
4127 ;; Handle the `<operator> syntax in Pike.
4128 (let ((pos (point)))
4129 (if (and (< (skip-chars-backward "-!%&*+/<=>^|~[]()") 0)
4130 (< (skip-chars-backward "`") 0)
4131 (looking-at c-symbol-key)
4132 (>= (match-end 0) pos))
4134 (goto-char pos)
4135 nil)))))
4137 (defun c-beginning-of-current-token (&optional back-limit)
4138 ;; Move to the beginning of the current token. Do not move if not
4139 ;; in the middle of one. BACK-LIMIT may be used to bound the
4140 ;; backward search; if given it's assumed to be at the boundary
4141 ;; between two tokens. Return non-nil if the point is moved, nil
4142 ;; otherwise.
4144 ;; This function might do hidden buffer changes.
4145 (let ((start (point)))
4146 (if (looking-at "\\w\\|\\s_")
4147 (skip-syntax-backward "w_" back-limit)
4148 (when (< (skip-syntax-backward ".()" back-limit) 0)
4149 (while (let ((pos (or (and (looking-at c-nonsymbol-token-regexp)
4150 (match-end 0))
4151 ;; `c-nonsymbol-token-regexp' should always match
4152 ;; since we've skipped backward over punctuation
4153 ;; or paren syntax, but consume one char in case
4154 ;; it doesn't so that we don't leave point before
4155 ;; some earlier incorrect token.
4156 (1+ (point)))))
4157 (if (<= pos start)
4158 (goto-char pos))))))
4159 (< (point) start)))
4161 (defun c-end-of-current-token (&optional back-limit)
4162 ;; Move to the end of the current token. Do not move if not in the
4163 ;; middle of one. BACK-LIMIT may be used to bound the backward
4164 ;; search; if given it's assumed to be at the boundary between two
4165 ;; tokens. Return non-nil if the point is moved, nil otherwise.
4167 ;; This function might do hidden buffer changes.
4168 (let ((start (point)))
4169 (cond ((< (skip-syntax-backward "w_" (1- start)) 0)
4170 (skip-syntax-forward "w_"))
4171 ((< (skip-syntax-backward ".()" back-limit) 0)
4172 (while (progn
4173 (if (looking-at c-nonsymbol-token-regexp)
4174 (goto-char (match-end 0))
4175 ;; `c-nonsymbol-token-regexp' should always match since
4176 ;; we've skipped backward over punctuation or paren
4177 ;; syntax, but move forward in case it doesn't so that
4178 ;; we don't leave point earlier than we started with.
4179 (forward-char))
4180 (< (point) start)))))
4181 (> (point) start)))
4183 (defconst c-jump-syntax-balanced
4184 (if (memq 'gen-string-delim c-emacs-features)
4185 "\\w\\|\\s_\\|\\s(\\|\\s)\\|\\s\"\\|\\s|"
4186 "\\w\\|\\s_\\|\\s(\\|\\s)\\|\\s\""))
4188 (defconst c-jump-syntax-unbalanced
4189 (if (memq 'gen-string-delim c-emacs-features)
4190 "\\w\\|\\s_\\|\\s\"\\|\\s|"
4191 "\\w\\|\\s_\\|\\s\""))
4193 (defun c-forward-token-2 (&optional count balanced limit)
4194 "Move forward by tokens.
4195 A token is defined as all symbols and identifiers which aren't
4196 syntactic whitespace \(note that multicharacter tokens like \"==\" are
4197 treated properly). Point is always either left at the beginning of a
4198 token or not moved at all. COUNT specifies the number of tokens to
4199 move; a negative COUNT moves in the opposite direction. A COUNT of 0
4200 moves to the next token beginning only if not already at one. If
4201 BALANCED is true, move over balanced parens, otherwise move into them.
4202 Also, if BALANCED is true, never move out of an enclosing paren.
4204 LIMIT sets the limit for the movement and defaults to the point limit.
4205 The case when LIMIT is set in the middle of a token, comment or macro
4206 is handled correctly, i.e. the point won't be left there.
4208 Return the number of tokens left to move \(positive or negative). If
4209 BALANCED is true, a move over a balanced paren counts as one. Note
4210 that if COUNT is 0 and no appropriate token beginning is found, 1 will
4211 be returned. Thus, a return value of 0 guarantees that point is at
4212 the requested position and a return value less \(without signs) than
4213 COUNT guarantees that point is at the beginning of some token.
4215 Note that this function might do hidden buffer changes. See the
4216 comment at the start of cc-engine.el for more info."
4218 (or count (setq count 1))
4219 (if (< count 0)
4220 (- (c-backward-token-2 (- count) balanced limit))
4222 (let ((jump-syntax (if balanced
4223 c-jump-syntax-balanced
4224 c-jump-syntax-unbalanced))
4225 (last (point))
4226 (prev (point)))
4228 (if (zerop count)
4229 ;; If count is zero we should jump if in the middle of a token.
4230 (c-end-of-current-token))
4232 (save-restriction
4233 (if limit (narrow-to-region (point-min) limit))
4234 (if (/= (point)
4235 (progn (c-forward-syntactic-ws) (point)))
4236 ;; Skip whitespace. Count this as a move if we did in
4237 ;; fact move.
4238 (setq count (max (1- count) 0)))
4240 (if (eobp)
4241 ;; Moved out of bounds. Make sure the returned count isn't zero.
4242 (progn
4243 (if (zerop count) (setq count 1))
4244 (goto-char last))
4246 ;; Use `condition-case' to avoid having the limit tests
4247 ;; inside the loop.
4248 (condition-case nil
4249 (while (and
4250 (> count 0)
4251 (progn
4252 (setq last (point))
4253 (cond ((looking-at jump-syntax)
4254 (goto-char (scan-sexps (point) 1))
4256 ((looking-at c-nonsymbol-token-regexp)
4257 (goto-char (match-end 0))
4259 ;; `c-nonsymbol-token-regexp' above should always
4260 ;; match if there are correct tokens. Try to
4261 ;; widen to see if the limit was set in the
4262 ;; middle of one, else fall back to treating
4263 ;; the offending thing as a one character token.
4264 ((and limit
4265 (save-restriction
4266 (widen)
4267 (looking-at c-nonsymbol-token-regexp)))
4268 nil)
4270 (forward-char)
4271 t))))
4272 (c-forward-syntactic-ws)
4273 (setq prev last
4274 count (1- count)))
4275 (error (goto-char last)))
4277 (when (eobp)
4278 (goto-char prev)
4279 (setq count (1+ count)))))
4281 count)))
4283 (defun c-backward-token-2 (&optional count balanced limit)
4284 "Move backward by tokens.
4285 See `c-forward-token-2' for details."
4287 (or count (setq count 1))
4288 (if (< count 0)
4289 (- (c-forward-token-2 (- count) balanced limit))
4291 (or limit (setq limit (point-min)))
4292 (let ((jump-syntax (if balanced
4293 c-jump-syntax-balanced
4294 c-jump-syntax-unbalanced))
4295 (last (point)))
4297 (if (zerop count)
4298 ;; The count is zero so try to skip to the beginning of the
4299 ;; current token.
4300 (if (> (point)
4301 (progn (c-beginning-of-current-token) (point)))
4302 (if (< (point) limit)
4303 ;; The limit is inside the same token, so return 1.
4304 (setq count 1))
4306 ;; We're not in the middle of a token. If there's
4307 ;; whitespace after the point then we must move backward,
4308 ;; so set count to 1 in that case.
4309 (and (looking-at c-syntactic-ws-start)
4310 ;; If we're looking at a '#' that might start a cpp
4311 ;; directive then we have to do a more elaborate check.
4312 (or (/= (char-after) ?#)
4313 (not c-opt-cpp-prefix)
4314 (save-excursion
4315 (and (= (point)
4316 (progn (beginning-of-line)
4317 (looking-at "[ \t]*")
4318 (match-end 0)))
4319 (or (bobp)
4320 (progn (backward-char)
4321 (not (eq (char-before) ?\\)))))))
4322 (setq count 1))))
4324 ;; Use `condition-case' to avoid having to check for buffer
4325 ;; limits in `backward-char', `scan-sexps' and `goto-char' below.
4326 (condition-case nil
4327 (while (and
4328 (> count 0)
4329 (progn
4330 (c-backward-syntactic-ws)
4331 (backward-char)
4332 (if (looking-at jump-syntax)
4333 (goto-char (scan-sexps (1+ (point)) -1))
4334 ;; This can be very inefficient if there's a long
4335 ;; sequence of operator tokens without any separation.
4336 ;; That doesn't happen in practice, anyway.
4337 (c-beginning-of-current-token))
4338 (>= (point) limit)))
4339 (setq last (point)
4340 count (1- count)))
4341 (error (goto-char last)))
4343 (if (< (point) limit)
4344 (goto-char last))
4346 count)))
4348 (defun c-forward-token-1 (&optional count balanced limit)
4349 "Like `c-forward-token-2' but doesn't treat multicharacter operator
4350 tokens like \"==\" as single tokens, i.e. all sequences of symbol
4351 characters are jumped over character by character. This function is
4352 for compatibility only; it's only a wrapper over `c-forward-token-2'."
4353 (let ((c-nonsymbol-token-regexp "\\s."))
4354 (c-forward-token-2 count balanced limit)))
4356 (defun c-backward-token-1 (&optional count balanced limit)
4357 "Like `c-backward-token-2' but doesn't treat multicharacter operator
4358 tokens like \"==\" as single tokens, i.e. all sequences of symbol
4359 characters are jumped over character by character. This function is
4360 for compatibility only; it's only a wrapper over `c-backward-token-2'."
4361 (let ((c-nonsymbol-token-regexp "\\s."))
4362 (c-backward-token-2 count balanced limit)))
4365 ;; Tools for doing searches restricted to syntactically relevant text.
4367 (defun c-syntactic-re-search-forward (regexp &optional bound noerror
4368 paren-level not-inside-token
4369 lookbehind-submatch)
4370 "Like `re-search-forward', but only report matches that are found
4371 in syntactically significant text. I.e. matches in comments, macros
4372 or string literals are ignored. The start point is assumed to be
4373 outside any comment, macro or string literal, or else the content of
4374 that region is taken as syntactically significant text.
4376 NOERROR, in addition to the values nil, t, and <anything else>
4377 used in `re-search-forward' can also take the values
4378 'before-literal and 'after-literal. In these cases, when BOUND
4379 is also given and is inside a literal, and a search fails, point
4380 will be left, respectively before or after the literal. Be aware
4381 that with 'after-literal, if a string or comment is unclosed at
4382 the end of the buffer, point may be left there, even though it is
4383 inside a literal there.
4385 If PAREN-LEVEL is non-nil, an additional restriction is added to
4386 ignore matches in nested paren sexps. The search will also not go
4387 outside the current list sexp, which has the effect that if the point
4388 should be moved to BOUND when no match is found \(i.e. NOERROR is
4389 neither nil nor t), then it will be at the closing paren if the end of
4390 the current list sexp is encountered first.
4392 If NOT-INSIDE-TOKEN is non-nil, matches in the middle of tokens are
4393 ignored. Things like multicharacter operators and special symbols
4394 \(e.g. \"`()\" in Pike) are handled but currently not floating point
4395 constants.
4397 If LOOKBEHIND-SUBMATCH is non-nil, it's taken as a number of a
4398 subexpression in REGEXP. The end of that submatch is used as the
4399 position to check for syntactic significance. If LOOKBEHIND-SUBMATCH
4400 isn't used or if that subexpression didn't match then the start
4401 position of the whole match is used instead. The \"look behind\"
4402 subexpression is never tested before the starting position, so it
4403 might be a good idea to include \\=\\= as a match alternative in it.
4405 Optimization note: Matches might be missed if the \"look behind\"
4406 subexpression can match the end of nonwhite syntactic whitespace,
4407 i.e. the end of comments or cpp directives. This since the function
4408 skips over such things before resuming the search. It's on the other
4409 hand not safe to assume that the \"look behind\" subexpression never
4410 matches syntactic whitespace.
4412 Bug: Unbalanced parens inside cpp directives are currently not handled
4413 correctly \(i.e. they don't get ignored as they should) when
4414 PAREN-LEVEL is set.
4416 Note that this function might do hidden buffer changes. See the
4417 comment at the start of cc-engine.el for more info."
4419 (or bound (setq bound (point-max)))
4420 (if paren-level (setq paren-level -1))
4422 ;;(message "c-syntactic-re-search-forward %s %s %S" (point) bound regexp)
4424 (let ((start (point))
4426 ;; Start position for the last search.
4427 search-pos
4428 ;; The `parse-partial-sexp' state between the start position
4429 ;; and the point.
4430 state
4431 ;; The current position after the last state update. The next
4432 ;; `parse-partial-sexp' continues from here.
4433 (state-pos (point))
4434 ;; The position at which to check the state and the state
4435 ;; there. This is separate from `state-pos' since we might
4436 ;; need to back up before doing the next search round.
4437 check-pos check-state
4438 ;; Last position known to end a token.
4439 (last-token-end-pos (point-min))
4440 ;; Set when a valid match is found.
4441 found)
4443 (condition-case err
4444 (while
4445 (and
4446 (progn
4447 (setq search-pos (point))
4448 (if (re-search-forward regexp bound noerror)
4450 ;; Without the following, when PAREN-LEVEL is non-nil, and
4451 ;; NOERROR is not nil or t, and the very first search above
4452 ;; has just failed, point would end up at BOUND rather than
4453 ;; just before the next close paren.
4454 (when (and (eq search-pos start)
4455 paren-level
4456 (not (memq noerror '(nil t))))
4457 (setq state (parse-partial-sexp start bound -1))
4458 (if (eq (car state) -1)
4459 (setq bound (1- (point)))))
4460 nil))
4462 (progn
4463 (setq state (parse-partial-sexp
4464 state-pos (match-beginning 0) paren-level nil state)
4465 state-pos (point))
4466 (if (setq check-pos (and lookbehind-submatch
4467 (or (not paren-level)
4468 (>= (car state) 0))
4469 (match-end lookbehind-submatch)))
4470 (setq check-state (parse-partial-sexp
4471 state-pos check-pos paren-level nil state))
4472 (setq check-pos state-pos
4473 check-state state))
4475 ;; NOTE: If we got a look behind subexpression and get
4476 ;; an insignificant match in something that isn't
4477 ;; syntactic whitespace (i.e. strings or in nested
4478 ;; parentheses), then we can never skip more than a
4479 ;; single character from the match start position
4480 ;; (i.e. `state-pos' here) before continuing the
4481 ;; search. That since the look behind subexpression
4482 ;; might match the end of the insignificant region in
4483 ;; the next search.
4485 (cond
4486 ((elt check-state 7)
4487 ;; Match inside a line comment. Skip to eol. Use
4488 ;; `re-search-forward' instead of `skip-chars-forward' to get
4489 ;; the right bound behavior.
4490 (re-search-forward "[\n\r]" bound noerror))
4492 ((elt check-state 4)
4493 ;; Match inside a block comment. Skip to the '*/'.
4494 (search-forward "*/" bound noerror))
4496 ((and (not (elt check-state 5))
4497 (eq (char-before check-pos) ?/)
4498 (not (c-get-char-property (1- check-pos) 'syntax-table))
4499 (memq (char-after check-pos) '(?/ ?*)))
4500 ;; Match in the middle of the opener of a block or line
4501 ;; comment.
4502 (if (= (char-after check-pos) ?/)
4503 (re-search-forward "[\n\r]" bound noerror)
4504 (search-forward "*/" bound noerror)))
4506 ;; The last `parse-partial-sexp' above might have
4507 ;; stopped short of the real check position if the end
4508 ;; of the current sexp was encountered in paren-level
4509 ;; mode. The checks above are always false in that
4510 ;; case, and since they can do better skipping in
4511 ;; lookbehind-submatch mode, we do them before
4512 ;; checking the paren level.
4514 ((and paren-level
4515 (/= (setq tmp (car check-state)) 0))
4516 ;; Check the paren level first since we're short of the
4517 ;; syntactic checking position if the end of the
4518 ;; current sexp was encountered by `parse-partial-sexp'.
4519 (if (> tmp 0)
4521 ;; Inside a nested paren sexp.
4522 (if lookbehind-submatch
4523 ;; See the NOTE above.
4524 (progn (goto-char state-pos) t)
4525 ;; Skip out of the paren quickly.
4526 (setq state (parse-partial-sexp state-pos bound 0 nil state)
4527 state-pos (point)))
4529 ;; Have exited the current paren sexp.
4530 (if noerror
4531 (progn
4532 ;; The last `parse-partial-sexp' call above
4533 ;; has left us just after the closing paren
4534 ;; in this case, so we can modify the bound
4535 ;; to leave the point at the right position
4536 ;; upon return.
4537 (setq bound (1- (point)))
4538 nil)
4539 (signal 'search-failed (list regexp)))))
4541 ((setq tmp (elt check-state 3))
4542 ;; Match inside a string.
4543 (if (or lookbehind-submatch
4544 (not (integerp tmp)))
4545 ;; See the NOTE above.
4546 (progn (goto-char state-pos) t)
4547 ;; Skip to the end of the string before continuing.
4548 (let ((ender (make-string 1 tmp)) (continue t))
4549 (while (if (search-forward ender bound noerror)
4550 (progn
4551 (setq state (parse-partial-sexp
4552 state-pos (point) nil nil state)
4553 state-pos (point))
4554 (elt state 3))
4555 (setq continue nil)))
4556 continue)))
4558 ((save-excursion
4559 (save-match-data
4560 (c-beginning-of-macro start)))
4561 ;; Match inside a macro. Skip to the end of it.
4562 (c-end-of-macro)
4563 (cond ((<= (point) bound) t)
4564 (noerror nil)
4565 (t (signal 'search-failed (list regexp)))))
4567 ((and not-inside-token
4568 (or (< check-pos last-token-end-pos)
4569 (< check-pos
4570 (save-excursion
4571 (goto-char check-pos)
4572 (save-match-data
4573 (c-end-of-current-token last-token-end-pos))
4574 (setq last-token-end-pos (point))))))
4575 ;; Inside a token.
4576 (if lookbehind-submatch
4577 ;; See the NOTE above.
4578 (goto-char state-pos)
4579 (goto-char (min last-token-end-pos bound))))
4582 ;; A real match.
4583 (setq found t)
4584 nil)))
4586 ;; Should loop to search again, but take care to avoid
4587 ;; looping on the same spot.
4588 (or (/= search-pos (point))
4589 (if (= (point) bound)
4590 (if noerror
4592 (signal 'search-failed (list regexp)))
4593 (forward-char)
4594 t))))
4596 (error
4597 (goto-char start)
4598 (signal (car err) (cdr err))))
4600 ;;(message "c-syntactic-re-search-forward done %s" (or (match-end 0) (point)))
4602 (if found
4603 (progn
4604 (goto-char (match-end 0))
4605 (match-end 0))
4607 ;; Search failed. Set point as appropriate.
4608 (cond
4609 ((eq noerror t)
4610 (goto-char start))
4611 ((not (memq noerror '(before-literal after-literal)))
4612 (goto-char bound))
4613 (t (setq state (parse-partial-sexp state-pos bound nil nil state))
4614 (if (or (elt state 3) (elt state 4))
4615 (if (eq noerror 'before-literal)
4616 (goto-char (elt state 8))
4617 (parse-partial-sexp bound (point-max) nil nil
4618 state 'syntax-table))
4619 (goto-char bound))))
4621 nil)))
4623 (defvar safe-pos-list) ; bound in c-syntactic-skip-backward
4625 (defsubst c-ssb-lit-begin ()
4626 ;; Return the start of the literal point is in, or nil.
4627 ;; We read and write the variables `safe-pos', `safe-pos-list', `state'
4628 ;; bound in the caller.
4630 ;; Use `parse-partial-sexp' from a safe position down to the point to check
4631 ;; if it's outside comments and strings.
4632 (save-excursion
4633 (let ((pos (point)) safe-pos state)
4634 ;; Pick a safe position as close to the point as possible.
4636 ;; FIXME: Consult `syntax-ppss' here if our cache doesn't give a good
4637 ;; position.
4639 (while (and safe-pos-list
4640 (> (car safe-pos-list) (point)))
4641 (setq safe-pos-list (cdr safe-pos-list)))
4642 (unless (setq safe-pos (car-safe safe-pos-list))
4643 (setq safe-pos (max (or (c-safe-position
4644 (point) (c-parse-state))
4646 (point-min))
4647 safe-pos-list (list safe-pos)))
4649 ;; Cache positions along the way to use if we have to back up more. We
4650 ;; cache every closing paren on the same level. If the paren cache is
4651 ;; relevant in this region then we're typically already on the same
4652 ;; level as the target position. Note that we might cache positions
4653 ;; after opening parens in case safe-pos is in a nested list. That's
4654 ;; both uncommon and harmless.
4655 (while (progn
4656 (setq state (parse-partial-sexp
4657 safe-pos pos 0))
4658 (< (point) pos))
4659 (setq safe-pos (point)
4660 safe-pos-list (cons safe-pos safe-pos-list)))
4662 ;; If the state contains the start of the containing sexp we cache that
4663 ;; position too, so that parse-partial-sexp in the next run has a bigger
4664 ;; chance of starting at the same level as the target position and thus
4665 ;; will get more good safe positions into the list.
4666 (if (elt state 1)
4667 (setq safe-pos (1+ (elt state 1))
4668 safe-pos-list (cons safe-pos safe-pos-list)))
4670 (if (or (elt state 3) (elt state 4))
4671 ;; Inside string or comment. Continue search at the
4672 ;; beginning of it.
4673 (elt state 8)))))
4675 (defun c-syntactic-skip-backward (skip-chars &optional limit paren-level)
4676 "Like `skip-chars-backward' but only look at syntactically relevant chars,
4677 i.e. don't stop at positions inside syntactic whitespace or string
4678 literals. Preprocessor directives are also ignored, with the exception
4679 of the one that the point starts within, if any. If LIMIT is given,
4680 it's assumed to be at a syntactically relevant position.
4682 If PAREN-LEVEL is non-nil, the function won't stop in nested paren
4683 sexps, and the search will also not go outside the current paren sexp.
4684 However, if LIMIT or the buffer limit is reached inside a nested paren
4685 then the point will be left at the limit.
4687 Non-nil is returned if the point moved, nil otherwise.
4689 Note that this function might do hidden buffer changes. See the
4690 comment at the start of cc-engine.el for more info."
4692 (c-self-bind-state-cache
4693 (let ((start (point))
4694 state-2
4695 ;; A list of syntactically relevant positions in descending
4696 ;; order. It's used to avoid scanning repeatedly over
4697 ;; potentially large regions with `parse-partial-sexp' to verify
4698 ;; each position. Used in `c-ssb-lit-begin'
4699 safe-pos-list
4700 ;; The result from `c-beginning-of-macro' at the start position or the
4701 ;; start position itself if it isn't within a macro. Evaluated on
4702 ;; demand.
4703 start-macro-beg
4704 ;; The earliest position after the current one with the same paren
4705 ;; level. Used only when `paren-level' is set.
4706 lit-beg
4707 (paren-level-pos (point)))
4709 (while
4710 (progn
4711 ;; The next loop "tries" to find the end point each time round,
4712 ;; loops when it hasn't succeeded.
4713 (while
4714 (and
4715 (let ((pos (point)))
4716 (while (and
4717 (< (skip-chars-backward skip-chars limit) 0)
4718 ;; Don't stop inside a literal.
4719 (when (setq lit-beg (c-ssb-lit-begin))
4720 (goto-char lit-beg)
4721 t)))
4722 (< (point) pos))
4724 (let ((pos (point)) state-2 pps-end-pos)
4726 (cond
4727 ((and paren-level
4728 (save-excursion
4729 (setq state-2 (parse-partial-sexp
4730 pos paren-level-pos -1)
4731 pps-end-pos (point))
4732 (/= (car state-2) 0)))
4733 ;; Not at the right level.
4735 (if (and (< (car state-2) 0)
4736 ;; We stop above if we go out of a paren.
4737 ;; Now check whether it precedes or is
4738 ;; nested in the starting sexp.
4739 (save-excursion
4740 (setq state-2
4741 (parse-partial-sexp
4742 pps-end-pos paren-level-pos
4743 nil nil state-2))
4744 (< (car state-2) 0)))
4746 ;; We've stopped short of the starting position
4747 ;; so the hit was inside a nested list. Go up
4748 ;; until we are at the right level.
4749 (condition-case nil
4750 (progn
4751 (goto-char (scan-lists pos -1
4752 (- (car state-2))))
4753 (setq paren-level-pos (point))
4754 (if (and limit (>= limit paren-level-pos))
4755 (progn
4756 (goto-char limit)
4757 nil)
4759 (error
4760 (goto-char (or limit (point-min)))
4761 nil))
4763 ;; The hit was outside the list at the start
4764 ;; position. Go to the start of the list and exit.
4765 (goto-char (1+ (elt state-2 1)))
4766 nil))
4768 ((c-beginning-of-macro limit)
4769 ;; Inside a macro.
4770 (if (< (point)
4771 (or start-macro-beg
4772 (setq start-macro-beg
4773 (save-excursion
4774 (goto-char start)
4775 (c-beginning-of-macro limit)
4776 (point)))))
4779 ;; It's inside the same macro we started in so it's
4780 ;; a relevant match.
4781 (goto-char pos)
4782 nil))))))
4784 (> (point)
4785 (progn
4786 ;; Skip syntactic ws afterwards so that we don't stop at the
4787 ;; end of a comment if `skip-chars' is something like "^/".
4788 (c-backward-syntactic-ws)
4789 (point)))))
4791 ;; We might want to extend this with more useful return values in
4792 ;; the future.
4793 (/= (point) start))))
4795 ;; The following is an alternative implementation of
4796 ;; `c-syntactic-skip-backward' that uses backward movement to keep
4797 ;; track of the syntactic context. It turned out to be generally
4798 ;; slower than the one above which uses forward checks from earlier
4799 ;; safe positions.
4801 ;;(defconst c-ssb-stop-re
4802 ;; ;; The regexp matching chars `c-syntactic-skip-backward' needs to
4803 ;; ;; stop at to avoid going into comments and literals.
4804 ;; (concat
4805 ;; ;; Match comment end syntax and string literal syntax. Also match
4806 ;; ;; '/' for block comment endings (not covered by comment end
4807 ;; ;; syntax).
4808 ;; "\\s>\\|/\\|\\s\""
4809 ;; (if (memq 'gen-string-delim c-emacs-features)
4810 ;; "\\|\\s|"
4811 ;; "")
4812 ;; (if (memq 'gen-comment-delim c-emacs-features)
4813 ;; "\\|\\s!"
4814 ;; "")))
4816 ;;(defconst c-ssb-stop-paren-re
4817 ;; ;; Like `c-ssb-stop-re' but also stops at paren chars.
4818 ;; (concat c-ssb-stop-re "\\|\\s(\\|\\s)"))
4820 ;;(defconst c-ssb-sexp-end-re
4821 ;; ;; Regexp matching the ending syntax of a complex sexp.
4822 ;; (concat c-string-limit-regexp "\\|\\s)"))
4824 ;;(defun c-syntactic-skip-backward (skip-chars &optional limit paren-level)
4825 ;; "Like `skip-chars-backward' but only look at syntactically relevant chars,
4826 ;;i.e. don't stop at positions inside syntactic whitespace or string
4827 ;;literals. Preprocessor directives are also ignored. However, if the
4828 ;;point is within a comment, string literal or preprocessor directory to
4829 ;;begin with, its contents is treated as syntactically relevant chars.
4830 ;;If LIMIT is given, it limits the backward search and the point will be
4831 ;;left there if no earlier position is found.
4833 ;;If PAREN-LEVEL is non-nil, the function won't stop in nested paren
4834 ;;sexps, and the search will also not go outside the current paren sexp.
4835 ;;However, if LIMIT or the buffer limit is reached inside a nested paren
4836 ;;then the point will be left at the limit.
4838 ;;Non-nil is returned if the point moved, nil otherwise.
4840 ;;Note that this function might do hidden buffer changes. See the
4841 ;;comment at the start of cc-engine.el for more info."
4843 ;; (save-restriction
4844 ;; (when limit
4845 ;; (narrow-to-region limit (point-max)))
4847 ;; (let ((start (point)))
4848 ;; (catch 'done
4849 ;; (while (let ((last-pos (point))
4850 ;; (stop-pos (progn
4851 ;; (skip-chars-backward skip-chars)
4852 ;; (point))))
4854 ;; ;; Skip back over the same region as
4855 ;; ;; `skip-chars-backward' above, but keep to
4856 ;; ;; syntactically relevant positions.
4857 ;; (goto-char last-pos)
4858 ;; (while (and
4859 ;; ;; `re-search-backward' with a single char regexp
4860 ;; ;; should be fast.
4861 ;; (re-search-backward
4862 ;; (if paren-level c-ssb-stop-paren-re c-ssb-stop-re)
4863 ;; stop-pos 'move)
4865 ;; (progn
4866 ;; (cond
4867 ;; ((looking-at "\\s(")
4868 ;; ;; `paren-level' is set and we've found the
4869 ;; ;; start of the containing paren.
4870 ;; (forward-char)
4871 ;; (throw 'done t))
4873 ;; ((looking-at c-ssb-sexp-end-re)
4874 ;; ;; We're at the end of a string literal or paren
4875 ;; ;; sexp (if `paren-level' is set).
4876 ;; (forward-char)
4877 ;; (condition-case nil
4878 ;; (c-backward-sexp)
4879 ;; (error
4880 ;; (goto-char limit)
4881 ;; (throw 'done t))))
4883 ;; (t
4884 ;; (forward-char)
4885 ;; ;; At the end of some syntactic ws or possibly
4886 ;; ;; after a plain '/' operator.
4887 ;; (let ((pos (point)))
4888 ;; (c-backward-syntactic-ws)
4889 ;; (if (= pos (point))
4890 ;; ;; Was a plain '/' operator. Go past it.
4891 ;; (backward-char)))))
4893 ;; (> (point) stop-pos))))
4895 ;; ;; Now the point is either at `stop-pos' or at some
4896 ;; ;; position further back if `stop-pos' was at a
4897 ;; ;; syntactically irrelevant place.
4899 ;; ;; Skip additional syntactic ws so that we don't stop
4900 ;; ;; at the end of a comment if `skip-chars' is
4901 ;; ;; something like "^/".
4902 ;; (c-backward-syntactic-ws)
4904 ;; (< (point) stop-pos))))
4906 ;; ;; We might want to extend this with more useful return values
4907 ;; ;; in the future.
4908 ;; (/= (point) start))))
4911 ;; Tools for handling comments and string literals.
4913 (defun c-in-literal (&optional lim detect-cpp)
4914 "Return the type of literal point is in, if any.
4915 The return value is `c' if in a C-style comment, `c++' if in a C++
4916 style comment, `string' if in a string literal, `pound' if DETECT-CPP
4917 is non-nil and in a preprocessor line, or nil if somewhere else.
4918 Optional LIM is used as the backward limit of the search. If omitted,
4919 or nil, `c-beginning-of-defun' is used.
4921 The last point calculated is cached if the cache is enabled, i.e. if
4922 `c-in-literal-cache' is bound to a two element vector.
4924 Note that this function might do hidden buffer changes. See the
4925 comment at the start of cc-engine.el for more info."
4926 (save-restriction
4927 (widen)
4928 (let ((lit (c-state-semi-pp-to-literal (point))))
4929 (or (cadr lit)
4930 (and detect-cpp
4931 (save-excursion (c-beginning-of-macro))
4932 'pound)))))
4934 (defun c-literal-limits (&optional lim near not-in-delimiter)
4935 "Return a cons of the beginning and end positions of the comment or
4936 string surrounding point (including both delimiters), or nil if point
4937 isn't in one. If LIM is non-nil, it's used as the \"safe\" position
4938 to start parsing from. If NEAR is non-nil, then the limits of any
4939 literal next to point is returned. \"Next to\" means there's only
4940 spaces and tabs between point and the literal. The search for such a
4941 literal is done first in forward direction. If NOT-IN-DELIMITER is
4942 non-nil, the case when point is inside a starting delimiter won't be
4943 recognized. This only has effect for comments which have starting
4944 delimiters with more than one character.
4946 Note that this function might do hidden buffer changes. See the
4947 comment at the start of cc-engine.el for more info."
4949 (save-excursion
4950 (let*
4951 ((pos (point))
4952 (lit-limits
4953 (if lim
4954 (let ((s (parse-partial-sexp lim (point))))
4955 (when (or (nth 3 s) (nth 4 s))
4956 (cons (nth 8 s)
4957 (progn (parse-partial-sexp (point) (point-max)
4958 nil nil
4960 'syntax-table)
4961 (point)))))
4962 (let ((pp-to-lit (c-state-full-pp-to-literal pos not-in-delimiter)))
4963 (car (cddr pp-to-lit))))))
4964 (cond
4965 (lit-limits)
4967 (near
4968 (goto-char pos)
4969 ;; Search forward for a literal.
4970 (skip-chars-forward " \t")
4971 (cond
4972 ((looking-at c-string-limit-regexp) ; String.
4973 (cons (point) (or (c-safe (c-forward-sexp 1) (point))
4974 (point-max))))
4976 ((looking-at c-comment-start-regexp) ; Line or block comment.
4977 (cons (point) (progn (c-forward-single-comment) (point))))
4980 ;; Search backward.
4981 (skip-chars-backward " \t")
4983 (let ((end (point)) beg)
4984 (cond
4985 ((save-excursion
4986 (< (skip-syntax-backward c-string-syntax) 0)) ; String.
4987 (setq beg (c-safe (c-backward-sexp 1) (point))))
4989 ((and (c-safe (forward-char -2) t)
4990 (looking-at "*/"))
4991 ;; Block comment. Due to the nature of line
4992 ;; comments, they will always be covered by the
4993 ;; normal case above.
4994 (goto-char end)
4995 (c-backward-single-comment)
4996 ;; If LIM is bogus, beg will be bogus.
4997 (setq beg (point))))
4999 (if beg (cons beg end))))))
5000 ))))
5002 (defun c-literal-start (&optional safe-pos)
5003 "Return the start of the string or comment surrounding point, or nil if
5004 point isn't in one. SAFE-POS, if non-nil, is a position before point which is
5005 a known \"safe position\", i.e. outside of any string or comment."
5006 (if safe-pos
5007 (let ((s (parse-partial-sexp safe-pos (point))))
5008 (and (or (nth 3 s) (nth 4 s))
5009 (nth 8 s)))
5010 (car (cddr (c-state-semi-pp-to-literal (point))))))
5012 ;; In case external callers use this; it did have a docstring.
5013 (defalias 'c-literal-limits-fast 'c-literal-limits)
5015 (defun c-collect-line-comments (range)
5016 "If the argument is a cons of two buffer positions (such as returned by
5017 `c-literal-limits'), and that range contains a C++ style line comment,
5018 then an extended range is returned that contains all adjacent line
5019 comments (i.e. all comments that starts in the same column with no
5020 empty lines or non-whitespace characters between them). Otherwise the
5021 argument is returned.
5023 Note that this function might do hidden buffer changes. See the
5024 comment at the start of cc-engine.el for more info."
5026 (save-excursion
5027 (condition-case nil
5028 (if (and (consp range) (progn
5029 (goto-char (car range))
5030 (looking-at c-line-comment-starter)))
5031 (let ((col (current-column))
5032 (beg (point))
5033 (bopl (c-point 'bopl))
5034 (end (cdr range)))
5035 ;; Got to take care in the backward direction to handle
5036 ;; comments which are preceded by code.
5037 (while (and (c-backward-single-comment)
5038 (>= (point) bopl)
5039 (looking-at c-line-comment-starter)
5040 (= col (current-column)))
5041 (setq beg (point)
5042 bopl (c-point 'bopl)))
5043 (goto-char end)
5044 (while (and (progn (skip-chars-forward " \t")
5045 (looking-at c-line-comment-starter))
5046 (= col (current-column))
5047 (prog1 (zerop (forward-line 1))
5048 (setq end (point)))))
5049 (cons beg end))
5050 range)
5051 (error range))))
5053 (defun c-literal-type (range)
5054 "Convenience function that given the result of `c-literal-limits',
5055 returns nil or the type of literal that the range surrounds, one
5056 of the symbols `c', `c++' or `string'. It's much faster than using
5057 `c-in-literal' and is intended to be used when you need both the
5058 type of a literal and its limits.
5060 Note that this function might do hidden buffer changes. See the
5061 comment at the start of cc-engine.el for more info."
5063 (if (consp range)
5064 (save-excursion
5065 (goto-char (car range))
5066 (cond ((looking-at c-string-limit-regexp) 'string)
5067 ((or (looking-at "//") ; c++ line comment
5068 (and (looking-at "\\s<") ; comment starter
5069 (looking-at "#"))) ; awk comment.
5070 'c++)
5071 (t 'c))) ; Assuming the range is valid.
5072 range))
5074 (defsubst c-determine-limit-get-base (start try-size)
5075 ;; Get a "safe place" approximately TRY-SIZE characters before START.
5076 ;; This defsubst doesn't preserve point.
5077 (let* ((pos (max (- start try-size) (point-min)))
5078 (s (c-state-semi-pp-to-literal pos)))
5079 (or (car (cddr s)) pos)))
5081 (defun c-determine-limit (how-far-back &optional start try-size)
5082 ;; Return a buffer position HOW-FAR-BACK non-literal characters from START
5083 ;; (default point). This is done by going back further in the buffer then
5084 ;; searching forward for literals. The position found won't be in a
5085 ;; literal. We start searching for the sought position TRY-SIZE (default
5086 ;; twice HOW-FAR-BACK) bytes back from START. This function must be fast.
5087 ;; :-)
5088 (save-excursion
5089 (let* ((start (or start (point)))
5090 (try-size (or try-size (* 2 how-far-back)))
5091 (base (c-determine-limit-get-base start try-size))
5092 (pos base)
5094 (s (parse-partial-sexp pos pos)) ; null state.
5095 stack elt size
5096 (count 0))
5097 (while (< pos start)
5098 ;; Move forward one literal each time round this loop.
5099 ;; Move forward to the start of a comment or string.
5100 (setq s (parse-partial-sexp
5102 start
5103 nil ; target-depth
5104 nil ; stop-before
5105 s ; state
5106 'syntax-table)) ; stop-comment
5108 ;; Gather details of the non-literal-bit - starting pos and size.
5109 (setq size (- (if (or (nth 4 s) (nth 3 s))
5110 (nth 8 s)
5111 (point))
5112 pos))
5113 (if (> size 0)
5114 (setq stack (cons (cons pos size) stack)))
5116 ;; Move forward to the end of the comment/string.
5117 (if (or (nth 4 s) (nth 3 s))
5118 (setq s (parse-partial-sexp
5119 (point)
5120 start
5121 nil ; target-depth
5122 nil ; stop-before
5123 s ; state
5124 'syntax-table))) ; stop-comment
5125 (setq pos (point)))
5127 ;; Now try and find enough non-literal characters recorded on the stack.
5128 ;; Go back one recorded literal each time round this loop.
5129 (while (and (< count how-far-back)
5130 stack)
5131 (setq elt (car stack)
5132 stack (cdr stack))
5133 (setq count (+ count (cdr elt))))
5135 ;; Have we found enough yet?
5136 (cond
5137 ((>= count how-far-back)
5138 (+ (car elt) (- count how-far-back)))
5139 ((eq base (point-min))
5140 (point-min))
5142 (c-determine-limit (- how-far-back count) base try-size))))))
5144 (defun c-determine-+ve-limit (how-far &optional start-pos)
5145 ;; Return a buffer position about HOW-FAR non-literal characters forward
5146 ;; from START-POS (default point), which must not be inside a literal.
5147 (save-excursion
5148 (let ((pos (or start-pos (point)))
5149 (count how-far)
5150 (s (parse-partial-sexp (point) (point)))) ; null state
5151 (while (and (not (eobp))
5152 (> count 0))
5153 ;; Scan over counted characters.
5154 (setq s (parse-partial-sexp
5156 (min (+ pos count) (point-max))
5157 nil ; target-depth
5158 nil ; stop-before
5159 s ; state
5160 'syntax-table)) ; stop-comment
5161 (setq count (- count (- (point) pos) 1)
5162 pos (point))
5163 ;; Scan over literal characters.
5164 (if (nth 8 s)
5165 (setq s (parse-partial-sexp
5167 (point-max)
5168 nil ; target-depth
5169 nil ; stop-before
5170 s ; state
5171 'syntax-table) ; stop-comment
5172 pos (point))))
5173 (point))))
5176 ;; `c-find-decl-spots' and accompanying stuff.
5178 ;; Variables used in `c-find-decl-spots' to cache the search done for
5179 ;; the first declaration in the last call. When that function starts,
5180 ;; it needs to back up over syntactic whitespace to look at the last
5181 ;; token before the region being searched. That can sometimes cause
5182 ;; moves back and forth over a quite large region of comments and
5183 ;; macros, which would be repeated for each changed character when
5184 ;; we're called during fontification, since font-lock refontifies the
5185 ;; current line for each change. Thus it's worthwhile to cache the
5186 ;; first match.
5188 ;; `c-find-decl-syntactic-pos' is a syntactically relevant position in
5189 ;; the syntactic whitespace less or equal to some start position.
5190 ;; There's no cached value if it's nil.
5192 ;; `c-find-decl-match-pos' is the match position if
5193 ;; `c-find-decl-prefix-search' matched before the syntactic whitespace
5194 ;; at `c-find-decl-syntactic-pos', or nil if there's no such match.
5195 (defvar c-find-decl-syntactic-pos nil)
5196 (make-variable-buffer-local 'c-find-decl-syntactic-pos)
5197 (defvar c-find-decl-match-pos nil)
5198 (make-variable-buffer-local 'c-find-decl-match-pos)
5200 (defsubst c-invalidate-find-decl-cache (change-min-pos)
5201 (and c-find-decl-syntactic-pos
5202 (< change-min-pos c-find-decl-syntactic-pos)
5203 (setq c-find-decl-syntactic-pos nil)))
5205 ; (defface c-debug-decl-spot-face
5206 ; '((t (:background "Turquoise")))
5207 ; "Debug face to mark the spots where `c-find-decl-spots' stopped.")
5208 ; (defface c-debug-decl-sws-face
5209 ; '((t (:background "Khaki")))
5210 ; "Debug face to mark the syntactic whitespace between the declaration
5211 ; spots and the preceding token end.")
5213 (defmacro c-debug-put-decl-spot-faces (match-pos decl-pos)
5214 (when (facep 'c-debug-decl-spot-face)
5215 `(c-save-buffer-state ((match-pos ,match-pos) (decl-pos ,decl-pos))
5216 (c-debug-add-face (max match-pos (point-min)) decl-pos
5217 'c-debug-decl-sws-face)
5218 (c-debug-add-face decl-pos (min (1+ decl-pos) (point-max))
5219 'c-debug-decl-spot-face))))
5220 (defmacro c-debug-remove-decl-spot-faces (beg end)
5221 (when (facep 'c-debug-decl-spot-face)
5222 `(c-save-buffer-state ()
5223 (c-debug-remove-face ,beg ,end 'c-debug-decl-spot-face)
5224 (c-debug-remove-face ,beg ,end 'c-debug-decl-sws-face))))
5226 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
5227 ;; Machinery for determining when we're at top level (this including being
5228 ;; directly inside a class or namespace, etc.)
5230 ;; We maintain a stack of brace depths in structures like classes and
5231 ;; namespaces. The car of this structure, when non-nil, indicates that the
5232 ;; associated position is within a template (etc.) structure, and the value is
5233 ;; the position where the (outermost) template ends. The other elements in
5234 ;; the structure are stacked elements, one each for each enclosing "top level"
5235 ;; structure.
5237 ;; At the very outermost level, the value of the stack would be (nil 1), the
5238 ;; "1" indicating an enclosure in a notional all-enclosing block. After
5239 ;; passing a keyword such as "namespace", the value would become (nil 0 1).
5240 ;; At this point, passing a semicolon would cause the 0 to be dropped from the
5241 ;; stack (at any other time, a semicolon is ignored). Alternatively, on
5242 ;; passing an opening brace, the stack would become (nil 1 1). Each opening
5243 ;; brace passed causes the cadr to be incremented, and passing closing braces
5244 ;; causes it to be decremented until it reaches 1. On passing a closing brace
5245 ;; when the cadr of the stack is at 1, this causes it to be removed from the
5246 ;; stack, the corresponding namespace (etc.) structure having been closed.
5248 ;; There is a special stack value -1 which means the C++ colon operator
5249 ;; introducing a list of inherited classes has just been parsed. The value
5250 ;; persists on the stack until the next open brace or semicolon.
5252 ;; When the car of the stack is non-nil, i.e. when we're in a template (etc.)
5253 ;; structure, braces are not counted. The counting resumes only after passing
5254 ;; the template's closing position, which is recorded in the car of the stack.
5256 ;; The test for being at top level consists of the cadr being 0 or 1.
5258 ;; The values of this stack throughout a buffer are cached in a simple linear
5259 ;; cache, every 5000 characters.
5261 ;; Note to maintainers: This cache mechanism is MUCH faster than recalculating
5262 ;; the stack at every entry to `c-find-decl-spots' using `c-at-toplevel-p' or
5263 ;; the like.
5264 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
5265 ;; The approximate interval at which we cache the value of the brace stack.
5266 (defconst c-bs-interval 5000)
5267 ;; The list of cached values of the brace stack. Each value in the list is a
5268 ;; cons of the position it is valid for and the value of the stack as
5269 ;; described above.
5270 (defvar c-bs-cache nil)
5271 (make-variable-buffer-local 'c-bs-cache)
5272 ;; The position of the buffer at and below which entries in `c-bs-cache' are
5273 ;; valid.
5274 (defvar c-bs-cache-limit 1)
5275 (make-variable-buffer-local 'c-bs-cache-limit)
5276 ;; The previous buffer position for which the brace stack value was
5277 ;; determined.
5278 (defvar c-bs-prev-pos most-positive-fixnum)
5279 (make-variable-buffer-local 'c-bs-prev-pos)
5280 ;; The value of the brace stack at `c-bs-prev-pos'.
5281 (defvar c-bs-prev-stack nil)
5282 (make-variable-buffer-local 'c-bs-prev-stack)
5284 (defun c-init-bs-cache ()
5285 ;; Initialize the cache in `c-bs-cache' and related variables.
5286 (setq c-bs-cache nil
5287 c-bs-cache-limit 1
5288 c-bs-prev-pos most-positive-fixnum
5289 c-bs-prev-stack nil))
5291 (defun c-truncate-bs-cache (pos &rest _ignore)
5292 ;; Truncate the upper bound of the cache `c-bs-cache' to POS, if it is
5293 ;; higher than that position. This is called as either a before- or
5294 ;; after-change-function.
5295 (setq c-bs-cache-limit
5296 (min c-bs-cache-limit pos)))
5298 (defun c-update-brace-stack (stack from to)
5299 ;; Give a brace-stack which has the value STACK at position FROM, update it
5300 ;; to it's value at position TO, where TO is after (or equal to) FROM.
5301 ;; Return a cons of either TO (if it is outside a literal) and this new
5302 ;; value, or of the next position after TO outside a literal and the new
5303 ;; value.
5304 (let (match kwd-sym (prev-match-pos 1)
5305 (s (cdr stack))
5306 (bound-<> (car stack))
5308 (save-excursion
5309 (cond
5310 ((and bound-<> (<= to bound-<>))
5311 (goto-char to)) ; Nothing to do.
5312 (bound-<>
5313 (goto-char bound-<>)
5314 (setq bound-<> nil))
5315 (t (goto-char from)))
5316 (while (and (< (point) to)
5317 (c-syntactic-re-search-forward
5318 (if (<= (car s) 0)
5319 c-brace-stack-thing-key
5320 c-brace-stack-no-semi-key)
5321 to 'after-literal)
5322 (> (point) prev-match-pos)) ; prevent infinite loop.
5323 (setq prev-match-pos (point))
5324 (setq match (match-string-no-properties 1)
5325 kwd-sym (c-keyword-sym match))
5326 (cond
5327 ((and (equal match "{")
5328 (progn (backward-char)
5329 (prog1 (looking-at "\\s(")
5330 (forward-char))))
5331 (setq s (if s
5332 (cons (if (<= (car s) 0)
5334 (1+ (car s)))
5335 (cdr s))
5336 (list 1))))
5337 ((and (equal match "}")
5338 (progn (backward-char)
5339 (prog1 (looking-at "\\s)")
5340 (forward-char))))
5341 (setq s
5342 (cond
5343 ((and s (> (car s) 1))
5344 (cons (1- (car s)) (cdr s)))
5345 ((and (cdr s) (eq (car s) 1))
5346 (cdr s))
5347 (t s))))
5348 ((and (equal match "<")
5349 (progn (backward-char)
5350 (prog1 (looking-at "\\s(")
5351 (forward-char))))
5352 (backward-char)
5353 (if (c-forward-<>-arglist nil) ; Should always work.
5354 (when (> (point) to)
5355 (setq bound-<> (point)))
5356 (forward-char)))
5357 ((and (equal match ":")
5359 (eq (car s) 0))
5360 (setq s (cons -1 (cdr s))))
5361 ((and (equal match ",")
5362 (eq (car s) -1))) ; at "," in "class foo : bar, ..."
5363 ((member match '(";" "," ")"))
5364 (when (and s (cdr s) (<= (car s) 0))
5365 (setq s (cdr s))))
5366 ((c-keyword-member kwd-sym 'c-flat-decl-block-kwds)
5367 (push 0 s))))
5368 (cons (point)
5369 (cons bound-<> s)))))
5371 (defun c-brace-stack-at (here)
5372 ;; Given a buffer position HERE, Return the value of the brace stack there.
5373 (save-excursion
5374 (save-restriction
5375 (widen)
5376 (let ((c c-bs-cache)
5377 (can-use-prev (<= c-bs-prev-pos c-bs-cache-limit))
5378 elt stack pos npos high-elt)
5379 ;; Trim the cache to take account of buffer changes.
5380 (while (and c
5381 (> (caar c) c-bs-cache-limit))
5382 (setq c (cdr c)))
5383 (setq c-bs-cache c)
5385 (while (and c
5386 (> (caar c) here))
5387 (setq high-elt (car c))
5388 (setq c (cdr c)))
5389 (setq pos (or (and c (caar c))
5390 (point-min)))
5392 (setq elt (if c
5393 (car c)
5394 (cons (point-min)
5395 (cons nil (list 1)))))
5396 (when (not high-elt)
5397 (setq stack (cdr elt))
5398 (while
5399 ;; Add an element to `c-state-semi-nonlit-pos-cache' each iteration.
5400 (<= (setq npos (+ pos c-bs-interval)) here)
5401 (setq elt (c-update-brace-stack stack pos npos))
5402 (setq npos (car elt))
5403 (setq stack (cdr elt))
5404 (unless (eq npos (point-max)) ; NPOS could be in a literal at EOB.
5405 (setq c-bs-cache (cons elt c-bs-cache)))
5406 (setq pos npos)))
5408 (if (> pos c-bs-cache-limit)
5409 (setq c-bs-cache-limit pos))
5411 ;; Can we just use the previous value?
5412 (if (and can-use-prev
5413 (<= c-bs-prev-pos here)
5414 (> c-bs-prev-pos (car elt)))
5415 (setq pos c-bs-prev-pos
5416 stack c-bs-prev-stack)
5417 (setq pos (car elt)
5418 stack (cdr elt)))
5419 (if (> here c-bs-cache-limit)
5420 (setq c-bs-cache-limit here))
5421 (setq elt (c-update-brace-stack stack pos here)
5422 c-bs-prev-pos (car elt)
5423 c-bs-prev-stack (cdr elt))))))
5425 (defun c-bs-at-toplevel-p (here)
5426 ;; Is position HERE at the top level, as indicated by the brace stack?
5427 (let ((stack (c-brace-stack-at here)))
5428 (or (null stack) ; Probably unnecessary.
5429 (<= (cadr stack) 1))))
5431 (defmacro c-find-decl-prefix-search ()
5432 ;; Macro used inside `c-find-decl-spots'. It ought to be a defun,
5433 ;; but it contains lots of free variables that refer to things
5434 ;; inside `c-find-decl-spots'. The point is left at `cfd-match-pos'
5435 ;; if there is a match, otherwise at `cfd-limit'.
5437 ;; The macro moves point forward to the next putative start of a declaration
5438 ;; or cfd-limit. This decl start is the next token after a "declaration
5439 ;; prefix". The declaration prefix is the earlier of `cfd-prop-match' and
5440 ;; `cfd-re-match'. `cfd-match-pos' is set to the decl prefix.
5442 ;; This macro might do hidden buffer changes.
5444 '(progn
5445 ;; Find the next property match position if we haven't got one already.
5446 (unless cfd-prop-match
5447 (save-excursion
5448 (while (progn
5449 (goto-char (c-next-single-property-change
5450 (point) 'c-type nil cfd-limit))
5451 (and (< (point) cfd-limit)
5452 (not (eq (c-get-char-property (1- (point)) 'c-type)
5453 'c-decl-end)))))
5454 (setq cfd-prop-match (point))))
5456 ;; Find the next `c-decl-prefix-or-start-re' match if we haven't
5457 ;; got one already.
5458 (unless cfd-re-match
5460 (if (> cfd-re-match-end (point))
5461 (goto-char cfd-re-match-end))
5463 ;; Each time round, the next `while' moves forward over a pseudo match
5464 ;; of `c-decl-prefix-or-start-re' which is either inside a literal, or
5465 ;; is a ":" not preceded by "public", etc.. `cfd-re-match' and
5466 ;; `cfd-re-match-end' get set.
5467 (while
5468 (progn
5469 (setq cfd-re-match-end (re-search-forward c-decl-prefix-or-start-re
5470 cfd-limit 'move))
5471 (cond
5472 ((null cfd-re-match-end)
5473 ;; No match. Finish up and exit the loop.
5474 (setq cfd-re-match cfd-limit)
5475 nil)
5476 ((c-got-face-at
5477 (if (setq cfd-re-match (match-end 1))
5478 ;; Matched the end of a token preceding a decl spot.
5479 (progn
5480 (goto-char cfd-re-match)
5481 (1- cfd-re-match))
5482 ;; Matched a token that start a decl spot.
5483 (goto-char (match-beginning 0))
5484 (point))
5485 c-literal-faces)
5486 ;; Pseudo match inside a comment or string literal. Skip out
5487 ;; of comments and string literals.
5488 (while (progn
5489 (goto-char (c-next-single-property-change
5490 (point) 'face nil cfd-limit))
5491 (and (< (point) cfd-limit)
5492 (c-got-face-at (point) c-literal-faces))))
5493 t) ; Continue the loop over pseudo matches.
5494 ((and c-opt-identifier-concat-key
5495 (match-string 1)
5496 (save-excursion
5497 (goto-char (match-beginning 1))
5498 (save-match-data
5499 (looking-at c-opt-identifier-concat-key))))
5500 ;; Found, e.g., "::" in C++
5502 ((and (match-string 1)
5503 (string= (match-string 1) ":")
5504 (save-excursion
5505 (or (/= (c-backward-token-2 2) 0) ; no search limit. :-(
5506 (not (looking-at c-decl-start-colon-kwd-re)))))
5507 ;; Found a ":" which isn't part of "public:", etc.
5509 (t nil)))) ;; Found a real match. Exit the pseudo-match loop.
5511 ;; If our match was at the decl start, we have to back up over the
5512 ;; preceding syntactic ws to set `cfd-match-pos' and to catch
5513 ;; any decl spots in the syntactic ws.
5514 (unless cfd-re-match
5515 (c-backward-syntactic-ws)
5516 (setq cfd-re-match (point))))
5518 ;; Choose whichever match is closer to the start.
5519 (if (< cfd-re-match cfd-prop-match)
5520 (setq cfd-match-pos cfd-re-match
5521 cfd-re-match nil)
5522 (setq cfd-match-pos cfd-prop-match
5523 cfd-prop-match nil))
5524 (setq cfd-top-level (c-bs-at-toplevel-p cfd-match-pos))
5526 (goto-char cfd-match-pos)
5528 (when (< cfd-match-pos cfd-limit)
5529 ;; Skip forward past comments only so we don't skip macros.
5530 (c-forward-comments)
5531 ;; Set the position to continue at. We can avoid going over
5532 ;; the comments skipped above a second time, but it's possible
5533 ;; that the comment skipping has taken us past `cfd-prop-match'
5534 ;; since the property might be used inside comments.
5535 (setq cfd-continue-pos (if cfd-prop-match
5536 (min cfd-prop-match (point))
5537 (point))))))
5539 (defun c-find-decl-spots (cfd-limit cfd-decl-re cfd-face-checklist cfd-fun)
5540 ;; Call CFD-FUN for each possible spot for a declaration, cast or
5541 ;; label from the point to CFD-LIMIT.
5543 ;; CFD-FUN is called with point at the start of the spot. It's passed two
5544 ;; arguments: The first is the end position of the token preceding the spot,
5545 ;; or 0 for the implicit match at bob. The second is a flag that is t when
5546 ;; the match is inside a macro. Point should be moved forward by at least
5547 ;; one token.
5549 ;; If CFD-FUN adds `c-decl-end' properties somewhere below the current spot,
5550 ;; it should return non-nil to ensure that the next search will find them.
5552 ;; Such a spot is:
5553 ;; o The first token after bob.
5554 ;; o The first token after the end of submatch 1 in
5555 ;; `c-decl-prefix-or-start-re' when that submatch matches. This
5556 ;; submatch is typically a (L or R) brace or paren, a ;, or a ,.
5557 ;; o The start of each `c-decl-prefix-or-start-re' match when
5558 ;; submatch 1 doesn't match. This is, for example, the keyword
5559 ;; "class" in Pike.
5560 ;; o The start of a previously recognized declaration; "recognized"
5561 ;; means that the last char of the previous token has a `c-type'
5562 ;; text property with the value `c-decl-end'; this only holds
5563 ;; when `c-type-decl-end-used' is set.
5565 ;; Only a spot that match CFD-DECL-RE and whose face is in the
5566 ;; CFD-FACE-CHECKLIST list causes CFD-FUN to be called. The face
5567 ;; check is disabled if CFD-FACE-CHECKLIST is nil.
5569 ;; If the match is inside a macro then the buffer is narrowed to the
5570 ;; end of it, so that CFD-FUN can investigate the following tokens
5571 ;; without matching something that begins inside a macro and ends
5572 ;; outside it. It's to avoid this work that the CFD-DECL-RE and
5573 ;; CFD-FACE-CHECKLIST checks exist.
5575 ;; The spots are visited approximately in order from top to bottom.
5576 ;; It's however the positions where `c-decl-prefix-or-start-re'
5577 ;; matches and where `c-decl-end' properties are found that are in
5578 ;; order. Since the spots often are at the following token, they
5579 ;; might be visited out of order insofar as more spots are reported
5580 ;; later on within the syntactic whitespace between the match
5581 ;; positions and their spots.
5583 ;; It's assumed that comments and strings are fontified in the
5584 ;; searched range.
5586 ;; This is mainly used in fontification, and so has an elaborate
5587 ;; cache to handle repeated calls from the same start position; see
5588 ;; the variables above.
5590 ;; All variables in this function begin with `cfd-' to avoid name
5591 ;; collision with the (dynamically bound) variables used in CFD-FUN.
5593 ;; This function might do hidden buffer changes.
5595 (let ((cfd-start-pos (point)) ; never changed
5596 (cfd-buffer-end (point-max))
5597 ;; The end of the token preceding the decl spot last found
5598 ;; with `c-decl-prefix-or-start-re'. `cfd-limit' if there's
5599 ;; no match.
5600 cfd-re-match
5601 ;; The end position of the last `c-decl-prefix-or-start-re'
5602 ;; match. If this is greater than `cfd-continue-pos', the
5603 ;; next regexp search is started here instead.
5604 (cfd-re-match-end (point-min))
5605 ;; The end of the last `c-decl-end' found by
5606 ;; `c-find-decl-prefix-search'. `cfd-limit' if there's no
5607 ;; match. If searching for the property isn't needed then we
5608 ;; disable it by setting it to `cfd-limit' directly.
5609 (cfd-prop-match (unless c-type-decl-end-used cfd-limit))
5610 ;; The end of the token preceding the decl spot last found by
5611 ;; `c-find-decl-prefix-search'. 0 for the implicit match at
5612 ;; bob. `cfd-limit' if there's no match. In other words,
5613 ;; this is the minimum of `cfd-re-match' and `cfd-prop-match'.
5614 (cfd-match-pos cfd-limit)
5615 ;; The position to continue searching at.
5616 cfd-continue-pos
5617 ;; The position of the last "real" token we've stopped at.
5618 ;; This can be greater than `cfd-continue-pos' when we get
5619 ;; hits inside macros or at `c-decl-end' positions inside
5620 ;; comments.
5621 (cfd-token-pos 0)
5622 ;; The end position of the last entered macro.
5623 (cfd-macro-end 0)
5624 ;; Whether the last position returned from `c-find-decl-prefix-search'
5625 ;; is at the top-level (including directly in a class or namespace,
5626 ;; etc.).
5627 cfd-top-level)
5629 ;; Initialize by finding a syntactically relevant start position
5630 ;; before the point, and do the first `c-decl-prefix-or-start-re'
5631 ;; search unless we're at bob.
5633 (let (start-in-literal start-in-macro syntactic-pos)
5634 ;; Must back up a bit since we look for the end of the previous
5635 ;; statement or declaration, which is earlier than the first
5636 ;; returned match.
5638 ;; This `cond' moves back over any literals or macros. It has special
5639 ;; handling for when the region being searched is entirely within a
5640 ;; macro. It sets `cfd-continue-pos' (unless we've reached
5641 ;; `cfd-limit').
5642 (cond
5643 ;; First we need to move to a syntactically relevant position.
5644 ;; Begin by backing out of comment or string literals.
5646 ;; This arm of the cond actually triggers if we're in a literal,
5647 ;; and cfd-limit is at most at BONL.
5648 ((and
5649 ;; This arm of the `and' moves backwards out of a literal when
5650 ;; the face at point is a literal face. In this case, its value
5651 ;; is always non-nil.
5652 (when (c-got-face-at (point) c-literal-faces)
5653 ;; Try to use the faces to back up to the start of the
5654 ;; literal. FIXME: What if the point is on a declaration
5655 ;; inside a comment?
5656 (while (and (not (bobp))
5657 (c-got-face-at (1- (point)) c-literal-faces))
5658 (goto-char (previous-single-property-change
5659 (point) 'face nil (point-min))))
5661 ;; XEmacs doesn't fontify the quotes surrounding string
5662 ;; literals.
5663 (and (featurep 'xemacs)
5664 (eq (get-text-property (point) 'face)
5665 'font-lock-string-face)
5666 (not (bobp))
5667 (progn (backward-char)
5668 (not (looking-at c-string-limit-regexp)))
5669 (forward-char))
5671 ;; Don't trust the literal to contain only literal faces
5672 ;; (the font lock package might not have fontified the
5673 ;; start of it at all, for instance) so check that we have
5674 ;; arrived at something that looks like a start or else
5675 ;; resort to `c-literal-limits'.
5676 (unless (looking-at c-literal-start-regexp)
5677 (let ((lit-start (c-literal-start)))
5678 (if lit-start (goto-char lit-start)))
5681 (setq start-in-literal (point))) ; end of `and' arm.
5683 ;; The start is in a literal. If the limit is in the same
5684 ;; one we don't have to find a syntactic position etc. We
5685 ;; only check that if the limit is at or before bonl to save
5686 ;; time; it covers the by far most common case when font-lock
5687 ;; refontifies the current line only.
5688 (<= cfd-limit (c-point 'bonl cfd-start-pos))
5689 (save-excursion
5690 (goto-char cfd-start-pos)
5691 (while (progn
5692 (goto-char (c-next-single-property-change
5693 (point) 'face nil cfd-limit))
5694 (and (< (point) cfd-limit)
5695 (c-got-face-at (point) c-literal-faces))))
5696 (= (point) cfd-limit))) ; end of `cond' arm condition
5698 ;; Completely inside a literal. Set up variables to trig the
5699 ;; (< cfd-continue-pos cfd-start-pos) case below and it'll
5700 ;; find a suitable start position.
5701 (setq cfd-continue-pos start-in-literal)) ; end of `cond' arm
5703 ;; Check if the region might be completely inside a macro, to
5704 ;; optimize that like the completely-inside-literal above.
5705 ((save-excursion
5706 (and (= (forward-line 1) 0)
5707 (bolp) ; forward-line has funny behavior at eob.
5708 (>= (point) cfd-limit)
5709 (progn (backward-char)
5710 (eq (char-before) ?\\))))
5711 ;; (Maybe) completely inside a macro. Only need to trig the
5712 ;; (< cfd-continue-pos cfd-start-pos) case below to make it
5713 ;; set things up.
5714 (setq cfd-continue-pos (1- cfd-start-pos)
5715 start-in-macro t))
5717 ;; The default arm of the `cond' moves back over any macro we're in
5718 ;; and over any syntactic WS. It sets `c-find-decl-syntactic-pos'.
5720 ;; Back out of any macro so we don't miss any declaration
5721 ;; that could follow after it.
5722 (when (c-beginning-of-macro)
5723 (setq start-in-macro t))
5725 ;; Now we're at a proper syntactically relevant position so we
5726 ;; can use the cache. But first clear it if it applied
5727 ;; further down.
5728 (c-invalidate-find-decl-cache cfd-start-pos)
5730 (setq syntactic-pos (point))
5731 (unless (eq syntactic-pos c-find-decl-syntactic-pos)
5732 ;; Don't have to do this if the cache is relevant here,
5733 ;; typically if the same line is refontified again. If
5734 ;; we're just some syntactic whitespace further down we can
5735 ;; still use the cache to limit the skipping.
5736 (c-backward-syntactic-ws c-find-decl-syntactic-pos))
5738 ;; If we hit `c-find-decl-syntactic-pos' and
5739 ;; `c-find-decl-match-pos' is set then we install the cached
5740 ;; values. If we hit `c-find-decl-syntactic-pos' and
5741 ;; `c-find-decl-match-pos' is nil then we know there's no decl
5742 ;; prefix in the whitespace before `c-find-decl-syntactic-pos'
5743 ;; and so we can continue the search from this point. If we
5744 ;; didn't hit `c-find-decl-syntactic-pos' then we're now in
5745 ;; the right spot to begin searching anyway.
5746 (if (and (eq (point) c-find-decl-syntactic-pos)
5747 c-find-decl-match-pos)
5748 (setq cfd-match-pos c-find-decl-match-pos
5749 cfd-continue-pos syntactic-pos)
5751 (setq c-find-decl-syntactic-pos syntactic-pos)
5753 (when (if (bobp)
5754 ;; Always consider bob a match to get the first
5755 ;; declaration in the file. Do this separately instead of
5756 ;; letting `c-decl-prefix-or-start-re' match bob, so that
5757 ;; regexp always can consume at least one character to
5758 ;; ensure that we won't get stuck in an infinite loop.
5759 (setq cfd-re-match 0)
5760 (backward-char)
5761 (c-beginning-of-current-token)
5762 (< (point) cfd-limit))
5763 ;; Do an initial search now. In the bob case above it's
5764 ;; only done to search for a `c-decl-end' spot.
5765 (c-find-decl-prefix-search)) ; sets cfd-continue-pos
5767 (setq c-find-decl-match-pos (and (< cfd-match-pos cfd-start-pos)
5768 cfd-match-pos))))) ; end of `cond'
5770 ;; Advance `cfd-continue-pos' if it's before the start position.
5771 ;; The closest continue position that might have effect at or
5772 ;; after the start depends on what we started in. This also
5773 ;; finds a suitable start position in the special cases when the
5774 ;; region is completely within a literal or macro.
5775 (when (and cfd-continue-pos (< cfd-continue-pos cfd-start-pos))
5777 (cond
5778 (start-in-macro
5779 ;; If we're in a macro then it's the closest preceding token
5780 ;; in the macro. Check this before `start-in-literal',
5781 ;; since if we're inside a literal in a macro, the preceding
5782 ;; token is earlier than any `c-decl-end' spot inside the
5783 ;; literal (comment).
5784 (goto-char (or start-in-literal cfd-start-pos))
5785 ;; The only syntactic ws in macros are comments.
5786 (c-backward-comments)
5787 (backward-char)
5788 (c-beginning-of-current-token))
5790 (start-in-literal
5791 ;; If we're in a comment it can only be the closest
5792 ;; preceding `c-decl-end' position within that comment, if
5793 ;; any. Go back to the beginning of such a property so that
5794 ;; `c-find-decl-prefix-search' will find the end of it.
5795 ;; (Can't stop at the end and install it directly on
5796 ;; `cfd-prop-match' since that variable might be cleared
5797 ;; after `cfd-fun' below.)
5799 ;; Note that if the literal is a string then the property
5800 ;; search will simply skip to the beginning of it right
5801 ;; away.
5802 (if (not c-type-decl-end-used)
5803 (goto-char start-in-literal)
5804 (goto-char cfd-start-pos)
5805 (while (progn
5806 (goto-char (previous-single-property-change
5807 (point) 'c-type nil start-in-literal))
5808 (and (> (point) start-in-literal)
5809 (not (eq (c-get-char-property (point) 'c-type)
5810 'c-decl-end))))))
5812 (when (= (point) start-in-literal)
5813 ;; Didn't find any property inside the comment, so we can
5814 ;; skip it entirely. (This won't skip past a string, but
5815 ;; that'll be handled quickly by the next
5816 ;; `c-find-decl-prefix-search' anyway.)
5817 (c-forward-single-comment)
5818 (if (> (point) cfd-limit)
5819 (goto-char cfd-limit))))
5822 ;; If we started in normal code, the only match that might
5823 ;; apply before the start is what we already got in
5824 ;; `cfd-match-pos' so we can continue at the start position.
5825 ;; (Note that we don't get here if the first match is below
5826 ;; it.)
5827 (goto-char cfd-start-pos))) ; end of `cond'
5829 ;; Delete found matches if they are before our new continue
5830 ;; position, so that `c-find-decl-prefix-search' won't back up
5831 ;; to them later on.
5832 (setq cfd-continue-pos (point))
5833 (when (and cfd-re-match (< cfd-re-match cfd-continue-pos))
5834 (setq cfd-re-match nil))
5835 (when (and cfd-prop-match (< cfd-prop-match cfd-continue-pos))
5836 (setq cfd-prop-match nil))) ; end of `when'
5838 (if syntactic-pos
5839 ;; This is the normal case and we got a proper syntactic
5840 ;; position. If there's a match then it's always outside
5841 ;; macros and comments, so advance to the next token and set
5842 ;; `cfd-token-pos'. The loop below will later go back using
5843 ;; `cfd-continue-pos' to fix declarations inside the
5844 ;; syntactic ws.
5845 (when (and cfd-match-pos (< cfd-match-pos syntactic-pos))
5846 (goto-char syntactic-pos)
5847 (c-forward-syntactic-ws)
5848 (and cfd-continue-pos
5849 (< cfd-continue-pos (point))
5850 (setq cfd-token-pos (point))))
5852 ;; Have one of the special cases when the region is completely
5853 ;; within a literal or macro. `cfd-continue-pos' is set to a
5854 ;; good start position for the search, so do it.
5855 (c-find-decl-prefix-search)))
5857 ;; Now loop, one decl spot per iteration. We already have the first
5858 ;; match in `cfd-match-pos'.
5859 (while (progn
5860 ;; Go forward over "false matches", one per iteration.
5861 (while (and
5862 (< cfd-match-pos cfd-limit)
5865 ;; Kludge to filter out matches on the "<" that
5866 ;; aren't open parens, for the sake of languages
5867 ;; that got `c-recognize-<>-arglists' set.
5868 (and (eq (char-before cfd-match-pos) ?<)
5869 (not (c-get-char-property (1- cfd-match-pos)
5870 'syntax-table)))
5872 ;; If `cfd-continue-pos' is less or equal to
5873 ;; `cfd-token-pos', we've got a hit inside a macro
5874 ;; that's in the syntactic whitespace before the last
5875 ;; "real" declaration we've checked. If they're equal
5876 ;; we've arrived at the declaration a second time, so
5877 ;; there's nothing to do.
5878 (= cfd-continue-pos cfd-token-pos)
5880 (progn
5881 ;; If `cfd-continue-pos' is less than `cfd-token-pos'
5882 ;; we're still searching for declarations embedded in
5883 ;; the syntactic whitespace. In that case we need
5884 ;; only to skip comments and not macros, since they
5885 ;; can't be nested, and that's already been done in
5886 ;; `c-find-decl-prefix-search'.
5887 (when (> cfd-continue-pos cfd-token-pos)
5888 (c-forward-syntactic-ws)
5889 (setq cfd-token-pos (point)))
5891 ;; Continue if the following token fails the
5892 ;; CFD-DECL-RE and CFD-FACE-CHECKLIST checks.
5893 (when (or (>= (point) cfd-limit)
5894 (not (looking-at cfd-decl-re))
5895 (and cfd-face-checklist
5896 (not (c-got-face-at
5897 (point) cfd-face-checklist))))
5898 (goto-char cfd-continue-pos)
5899 t)))
5901 (< (point) cfd-limit)) ; end of "false matches" condition
5902 (c-find-decl-prefix-search)) ; end of "false matches" loop
5904 (< (point) cfd-limit)) ; end of condition for "decl-spot" while
5906 (when (and
5907 (>= (point) cfd-start-pos)
5909 (progn
5910 ;; Narrow to the end of the macro if we got a hit inside
5911 ;; one, to avoid recognizing things that start inside the
5912 ;; macro and end outside it.
5913 (when (> cfd-match-pos cfd-macro-end)
5914 ;; Not in the same macro as in the previous round.
5915 (save-excursion
5916 (goto-char cfd-match-pos)
5917 (setq cfd-macro-end
5918 (if (save-excursion (and (c-beginning-of-macro)
5919 (< (point) cfd-match-pos)))
5920 (progn (c-end-of-macro)
5921 (point))
5922 0))))
5924 (if (zerop cfd-macro-end)
5926 (if (> cfd-macro-end (point))
5927 (progn (narrow-to-region (point-min) cfd-macro-end)
5929 ;; The matched token was the last thing in the macro,
5930 ;; so the whole match is bogus.
5931 (setq cfd-macro-end 0)
5932 nil)))) ; end of when condition
5934 (c-debug-put-decl-spot-faces cfd-match-pos (point))
5935 (if (funcall cfd-fun cfd-match-pos (/= cfd-macro-end 0) cfd-top-level)
5936 (setq cfd-prop-match nil))
5938 (when (/= cfd-macro-end 0)
5939 ;; Restore limits if we did macro narrowing above.
5940 (narrow-to-region (point-min) cfd-buffer-end)))
5942 (goto-char cfd-continue-pos)
5943 (if (= cfd-continue-pos cfd-limit)
5944 (setq cfd-match-pos cfd-limit)
5945 (c-find-decl-prefix-search))))) ; Moves point, sets cfd-continue-pos,
5946 ; cfd-match-pos, etc.
5949 ;; A cache for found types.
5951 ;; Buffer local variable that contains an obarray with the types we've
5952 ;; found. If a declaration is recognized somewhere we record the
5953 ;; fully qualified identifier in it to recognize it as a type
5954 ;; elsewhere in the file too. This is not accurate since we do not
5955 ;; bother with the scoping rules of the languages, but in practice the
5956 ;; same name is seldom used as both a type and something else in a
5957 ;; file, and we only use this as a last resort in ambiguous cases (see
5958 ;; `c-forward-decl-or-cast-1').
5960 ;; Not every type need be in this cache. However, things which have
5961 ;; ceased to be types must be removed from it.
5963 ;; Template types in C++ are added here too but with the template
5964 ;; arglist replaced with "<>" in references or "<" for the one in the
5965 ;; primary type. E.g. the type "Foo<A,B>::Bar<C>" is stored as
5966 ;; "Foo<>::Bar<". This avoids storing very long strings (since C++
5967 ;; template specs can be fairly sized programs in themselves) and
5968 ;; improves the hit ratio (it's a type regardless of the template
5969 ;; args; it's just not the same type, but we're only interested in
5970 ;; recognizing types, not telling distinct types apart). Note that
5971 ;; template types in references are added here too; from the example
5972 ;; above there will also be an entry "Foo<".
5973 (defvar c-found-types nil)
5974 (make-variable-buffer-local 'c-found-types)
5976 (defsubst c-clear-found-types ()
5977 ;; Clears `c-found-types'.
5978 (setq c-found-types (make-vector 53 0)))
5980 (defun c-add-type (from to)
5981 ;; Add the given region as a type in `c-found-types'. If the region
5982 ;; doesn't match an existing type but there is a type which is equal
5983 ;; to the given one except that the last character is missing, then
5984 ;; the shorter type is removed. That's done to avoid adding all
5985 ;; prefixes of a type as it's being entered and font locked. This
5986 ;; doesn't cover cases like when characters are removed from a type
5987 ;; or added in the middle. We'd need the position of point when the
5988 ;; font locking is invoked to solve this well.
5990 ;; This function might do hidden buffer changes.
5991 (let ((type (c-syntactic-content from to c-recognize-<>-arglists)))
5992 (unless (intern-soft type c-found-types)
5993 (unintern (substring type 0 -1) c-found-types)
5994 (intern type c-found-types))))
5996 (defun c-unfind-type (name)
5997 ;; Remove the "NAME" from c-found-types, if present.
5998 (unintern name c-found-types))
6000 (defsubst c-check-type (from to)
6001 ;; Return non-nil if the given region contains a type in
6002 ;; `c-found-types'.
6004 ;; This function might do hidden buffer changes.
6005 (intern-soft (c-syntactic-content from to c-recognize-<>-arglists)
6006 c-found-types))
6008 (defun c-list-found-types ()
6009 ;; Return all the types in `c-found-types' as a sorted list of
6010 ;; strings.
6011 (let (type-list)
6012 (mapatoms (lambda (type)
6013 (setq type-list (cons (symbol-name type)
6014 type-list)))
6015 c-found-types)
6016 (sort type-list 'string-lessp)))
6018 ;; Shut up the byte compiler.
6019 (defvar c-maybe-stale-found-type)
6021 (defun c-trim-found-types (beg end old-len)
6022 ;; An after change function which, in conjunction with the info in
6023 ;; c-maybe-stale-found-type (set in c-before-change), removes a type
6024 ;; from `c-found-types', should this type have become stale. For
6025 ;; example, this happens to "foo" when "foo \n bar();" becomes
6026 ;; "foo(); \n bar();". Such stale types, if not removed, foul up
6027 ;; the fontification.
6029 ;; Have we, perhaps, added non-ws characters to the front/back of a found
6030 ;; type?
6031 (when (> end beg)
6032 (save-excursion
6033 (when (< end (point-max))
6034 (goto-char end)
6035 (if (and (c-beginning-of-current-token) ; only moves when we started in the middle
6036 (progn (goto-char end)
6037 (c-end-of-current-token)))
6038 (c-unfind-type (buffer-substring-no-properties
6039 end (point)))))
6040 (when (> beg (point-min))
6041 (goto-char beg)
6042 (if (and (c-end-of-current-token) ; only moves when we started in the middle
6043 (progn (goto-char beg)
6044 (c-beginning-of-current-token)))
6045 (c-unfind-type (buffer-substring-no-properties
6046 (point) beg))))))
6048 (if c-maybe-stale-found-type ; e.g. (c-decl-id-start "foo" 97 107 " (* ooka) " "o")
6049 (cond
6050 ;; Changing the amount of (already existing) whitespace - don't do anything.
6051 ((and (c-partial-ws-p beg end)
6052 (or (= beg end) ; removal of WS
6053 (string-match "^[ \t\n\r\f\v]*$" (nth 5 c-maybe-stale-found-type)))))
6055 ;; The syntactic relationship which defined a "found type" has been
6056 ;; destroyed.
6057 ((eq (car c-maybe-stale-found-type) 'c-decl-id-start)
6058 (c-unfind-type (cadr c-maybe-stale-found-type)))
6059 ;; ((eq (car c-maybe-stale-found-type) 'c-decl-type-start) FIXME!!!
6063 ;; Setting and removing syntax properties on < and > in languages (C++
6064 ;; and Java) where they can be template/generic delimiters as well as
6065 ;; their normal meaning of "less/greater than".
6067 ;; Normally, < and > have syntax 'punctuation'. When they are found to
6068 ;; be delimiters, they are marked as such with the category properties
6069 ;; c-<-as-paren-syntax, c->-as-paren-syntax respectively.
6071 ;; STRATEGY:
6073 ;; It is impossible to determine with certainty whether a <..> pair in
6074 ;; C++ is two comparison operators or is template delimiters, unless
6075 ;; one duplicates a lot of a C++ compiler. For example, the following
6076 ;; code fragment:
6078 ;; foo (a < b, c > d) ;
6080 ;; could be a function call with two integer parameters (each a
6081 ;; relational expression), or it could be a constructor for class foo
6082 ;; taking one parameter d of templated type "a < b, c >". They are
6083 ;; somewhat easier to distinguish in Java.
6085 ;; The strategy now (2010-01) adopted is to mark and unmark < and
6086 ;; > IN MATCHING PAIRS ONLY. [Previously, they were marked
6087 ;; individually when their context so indicated. This gave rise to
6088 ;; intractable problems when one of a matching pair was deleted, or
6089 ;; pulled into a literal.]
6091 ;; At each buffer change, the syntax-table properties are removed in a
6092 ;; before-change function and reapplied, when needed, in an
6093 ;; after-change function. It is far more important that the
6094 ;; properties get removed when they they are spurious than that they
6095 ;; be present when wanted.
6096 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
6097 (defun c-clear-<-pair-props (&optional pos)
6098 ;; POS (default point) is at a < character. If it is marked with
6099 ;; open paren syntax-table text property, remove the property,
6100 ;; together with the close paren property on the matching > (if
6101 ;; any).
6102 (save-excursion
6103 (if pos
6104 (goto-char pos)
6105 (setq pos (point)))
6106 (when (equal (c-get-char-property (point) 'syntax-table)
6107 c-<-as-paren-syntax)
6108 (with-syntax-table c-no-parens-syntax-table ; ignore unbalanced [,{,(,..
6109 (c-go-list-forward))
6110 (when (equal (c-get-char-property (1- (point)) 'syntax-table)
6111 c->-as-paren-syntax) ; should always be true.
6112 (c-unmark-<->-as-paren (1- (point))))
6113 (c-unmark-<->-as-paren pos))))
6115 (defun c-clear->-pair-props (&optional pos)
6116 ;; POS (default point) is at a > character. If it is marked with
6117 ;; close paren syntax-table property, remove the property, together
6118 ;; with the open paren property on the matching < (if any).
6119 (save-excursion
6120 (if pos
6121 (goto-char pos)
6122 (setq pos (point)))
6123 (when (equal (c-get-char-property (point) 'syntax-table)
6124 c->-as-paren-syntax)
6125 (with-syntax-table c-no-parens-syntax-table ; ignore unbalanced [,{,(,..
6126 (c-go-up-list-backward))
6127 (when (equal (c-get-char-property (point) 'syntax-table)
6128 c-<-as-paren-syntax) ; should always be true.
6129 (c-unmark-<->-as-paren (point)))
6130 (c-unmark-<->-as-paren pos))))
6132 (defun c-clear-<>-pair-props (&optional pos)
6133 ;; POS (default point) is at a < or > character. If it has an
6134 ;; open/close paren syntax-table property, remove this property both
6135 ;; from the current character and its partner (which will also be
6136 ;; thusly marked).
6137 (cond
6138 ((eq (char-after) ?\<)
6139 (c-clear-<-pair-props pos))
6140 ((eq (char-after) ?\>)
6141 (c-clear->-pair-props pos))
6142 (t (c-benign-error
6143 "c-clear-<>-pair-props called from wrong position"))))
6145 (defun c-clear-<-pair-props-if-match-after (lim &optional pos)
6146 ;; POS (default point) is at a < character. If it is both marked
6147 ;; with open/close paren syntax-table property, and has a matching >
6148 ;; (also marked) which is after LIM, remove the property both from
6149 ;; the current > and its partner. Return t when this happens, nil
6150 ;; when it doesn't.
6151 (save-excursion
6152 (if pos
6153 (goto-char pos)
6154 (setq pos (point)))
6155 (when (equal (c-get-char-property (point) 'syntax-table)
6156 c-<-as-paren-syntax)
6157 (with-syntax-table c-no-parens-syntax-table ; ignore unbalanced [,{,(,..
6158 (c-go-list-forward))
6159 (when (and (>= (point) lim)
6160 (equal (c-get-char-property (1- (point)) 'syntax-table)
6161 c->-as-paren-syntax)) ; should always be true.
6162 (c-unmark-<->-as-paren (1- (point)))
6163 (c-unmark-<->-as-paren pos))
6164 t)))
6166 (defun c-clear->-pair-props-if-match-before (lim &optional pos)
6167 ;; POS (default point) is at a > character. If it is both marked
6168 ;; with open/close paren syntax-table property, and has a matching <
6169 ;; (also marked) which is before LIM, remove the property both from
6170 ;; the current < and its partner. Return t when this happens, nil
6171 ;; when it doesn't.
6172 (save-excursion
6173 (if pos
6174 (goto-char pos)
6175 (setq pos (point)))
6176 (when (equal (c-get-char-property (point) 'syntax-table)
6177 c->-as-paren-syntax)
6178 (with-syntax-table c-no-parens-syntax-table ; ignore unbalanced [,{,(,..
6179 (c-go-up-list-backward))
6180 (when (and (<= (point) lim)
6181 (equal (c-get-char-property (point) 'syntax-table)
6182 c-<-as-paren-syntax)) ; should always be true.
6183 (c-unmark-<->-as-paren (point))
6184 (c-unmark-<->-as-paren pos))
6185 t)))
6187 ;; Set by c-common-init in cc-mode.el.
6188 (defvar c-new-BEG)
6189 (defvar c-new-END)
6190 ;; Set by c-after-change in cc-mode.el.
6191 (defvar c-old-BEG)
6192 (defvar c-old-END)
6194 (defun c-before-change-check-<>-operators (beg end)
6195 ;; Unmark certain pairs of "< .... >" which are currently marked as
6196 ;; template/generic delimiters. (This marking is via syntax-table text
6197 ;; properties), and expand the (c-new-BEG c-new-END) region to include all
6198 ;; unmarked < and > operators within the certain bounds (see below).
6200 ;; These pairs are those which are in the current "statement" (i.e.,
6201 ;; the region between the {, }, or ; before BEG and the one after
6202 ;; END), and which enclose any part of the interval (BEG END).
6204 ;; Note that in C++ (?and Java), template/generic parens cannot
6205 ;; enclose a brace or semicolon, so we use these as bounds on the
6206 ;; region we must work on.
6208 ;; This function is called from before-change-functions (via
6209 ;; c-get-state-before-change-functions). Thus the buffer is widened,
6210 ;; and point is undefined, both at entry and exit.
6212 ;; FIXME!!! This routine ignores the possibility of macros entirely.
6213 ;; 2010-01-29.
6214 (save-excursion
6215 (c-save-buffer-state
6216 ((beg-lit-start (progn (goto-char beg) (c-literal-start)))
6217 (end-lit-limits (progn (goto-char end) (c-literal-limits)))
6218 new-beg new-end beg-limit end-limit)
6219 ;; Locate the earliest < after the barrier before the changed region,
6220 ;; which isn't already marked as a paren.
6221 (goto-char (or beg-lit-start beg))
6222 (setq beg-limit (c-determine-limit 512))
6224 ;; Remove the syntax-table/category properties from each pertinent <...>
6225 ;; pair. Firstly, the ones with the < before beg and > after beg....
6226 (while (progn (c-syntactic-skip-backward "^;{}<" beg-limit)
6227 (eq (char-before) ?<))
6228 (c-backward-token-2)
6229 (when (eq (char-after) ?<)
6230 (c-clear-<-pair-props-if-match-after beg)))
6231 (c-forward-syntactic-ws)
6232 (setq new-beg (point))
6234 ;; ...Then the ones with < before end and > after end.
6235 (goto-char (if end-lit-limits (cdr end-lit-limits) end))
6236 (setq end-limit (c-determine-+ve-limit 512))
6237 (while (and (c-syntactic-re-search-forward "[;{}>]" end-limit 'end)
6238 (eq (char-before) ?>))
6239 (c-end-of-current-token)
6240 (when (eq (char-before) ?>)
6241 (c-clear->-pair-props-if-match-before end (1- (point)))))
6242 (c-backward-syntactic-ws)
6243 (setq new-end (point))
6245 ;; Extend the fontification region, if needed.
6246 (and new-beg
6247 (< new-beg c-new-BEG)
6248 (setq c-new-BEG new-beg))
6249 (and new-end
6250 (> new-end c-new-END)
6251 (setq c-new-END new-end)))))
6253 (defun c-after-change-check-<>-operators (beg end)
6254 ;; This is called from `after-change-functions' when
6255 ;; c-recognize-<>-arglists' is set. It ensures that no "<" or ">"
6256 ;; chars with paren syntax become part of another operator like "<<"
6257 ;; or ">=".
6259 ;; This function might do hidden buffer changes.
6261 (save-excursion
6262 (goto-char beg)
6263 (when (or (looking-at "[<>]")
6264 (< (skip-chars-backward "<>") 0))
6266 (goto-char beg)
6267 (c-beginning-of-current-token)
6268 (when (and (< (point) beg)
6269 (looking-at c-<>-multichar-token-regexp)
6270 (< beg (setq beg (match-end 0))))
6271 (while (progn (skip-chars-forward "^<>" beg)
6272 (< (point) beg))
6273 (c-clear-<>-pair-props)
6274 (forward-char))))
6276 (when (< beg end)
6277 (goto-char end)
6278 (when (or (looking-at "[<>]")
6279 (< (skip-chars-backward "<>") 0))
6281 (goto-char end)
6282 (c-beginning-of-current-token)
6283 (when (and (< (point) end)
6284 (looking-at c-<>-multichar-token-regexp)
6285 (< end (setq end (match-end 0))))
6286 (while (progn (skip-chars-forward "^<>" end)
6287 (< (point) end))
6288 (c-clear-<>-pair-props)
6289 (forward-char)))))))
6291 (defun c-restore-<>-properties (_beg _end _old-len)
6292 ;; This function is called as an after-change function. It restores the
6293 ;; category/syntax-table properties on template/generic <..> pairs between
6294 ;; c-new-BEG and c-new-END. It may do hidden buffer changes.
6295 (c-save-buffer-state ((c-parse-and-markup-<>-arglists t)
6296 c-restricted-<>-arglists lit-limits)
6297 (goto-char c-new-BEG)
6298 (if (setq lit-limits (c-literal-limits))
6299 (goto-char (cdr lit-limits)))
6300 (while (and (< (point) c-new-END)
6301 (c-syntactic-re-search-forward "<" c-new-END 'bound))
6302 (backward-char)
6303 (save-excursion
6304 (c-backward-token-2)
6305 (setq c-restricted-<>-arglists
6306 (and (not (looking-at c-opt-<>-sexp-key))
6307 (progn (c-backward-syntactic-ws) ; to ( or ,
6308 (and (memq (char-before) '(?\( ?,)) ; what about <?
6309 (not (eq (c-get-char-property (point) 'c-type)
6310 'c-decl-arg-start)))))))
6311 (or (c-forward-<>-arglist nil)
6312 (forward-char)))))
6315 ;; Functions to handle C++ raw strings.
6317 ;; A valid C++ raw string looks like
6318 ;; R"<id>(<contents>)<id>"
6319 ;; , where <id> is an identifier from 0 to 16 characters long, not containing
6320 ;; spaces, control characters, double quote or left/right paren. <contents>
6321 ;; can include anything which isn't the terminating )<id>", including new
6322 ;; lines, "s, parentheses, etc.
6324 ;; CC Mode handles C++ raw strings by the use of `syntax-table' text
6325 ;; properties as follows:
6327 ;; (i) On a validly terminated raw string, no `syntax-table' text properties
6328 ;; are applied to the opening and closing delimiters, but any " in the
6329 ;; contents is given the property value "punctuation" (`(1)') to prevent it
6330 ;; interacting with the "s in the delimiters.
6332 ;; The font locking routine `c-font-lock-c++-raw-strings' (in cc-fonts.el)
6333 ;; recognizes valid raw strings, and fontifies the delimiters (apart from
6334 ;; the parentheses) with the default face and the parentheses and the
6335 ;; <contents> with font-lock-string-face.
6337 ;; (ii) A valid, but unterminated, raw string opening delimiter gets the
6338 ;; "punctuation" value (`(1)') of the `syntax-table' text property, and the
6339 ;; open parenthesis gets the "string fence" value (`(15)').
6341 ;; `c-font-lock-c++-raw-strings' puts c-font-lock-warning-face on the entire
6342 ;; unmatched opening delimiter (from the R up to the open paren), and allows
6343 ;; the rest of the buffer to get font-lock-string-face, caused by the
6344 ;; unmatched "string fence" `syntax-table' text property value.
6346 ;; (iii) Inside a macro, a valid raw string is handled as in (i). An
6347 ;; unmatched opening delimiter is handled slightly differently. In addition
6348 ;; to the "punctuation" and "string fence" properties on the delimiter,
6349 ;; another "string fence" `syntax-table' property is applied to the last
6350 ;; possible character of the macro before the terminating linefeed (if there
6351 ;; is such a character after the "("). This "last possible" character is
6352 ;; never a backslash escaping the end of line. If the character preceding
6353 ;; this "last possible" character is itself a backslash, this preceding
6354 ;; character gets a "punctuation" `syntax-table' value. If the "(" is
6355 ;; already at the end of the macro, it gets the "punctuation" value, and no
6356 ;; "string fence"s are used.
6358 ;; The effect on the fontification of either of these tactics is that rest of
6359 ;; the macro (if any) after the "(" gets font-lock-string-face, but the rest
6360 ;; of the file is fontified normally.
6363 (defun c-raw-string-pos ()
6364 ;; Get POINT's relationship to any containing raw string.
6365 ;; If point isn't in a raw string, return nil.
6366 ;; Otherwise, return the following list:
6368 ;; (POS B\" B\( E\) E\")
6370 ;; , where POS is the symbol `open-delim' if point is in the opening
6371 ;; delimiter, the symbol `close-delim' if it's in the closing delimiter, and
6372 ;; nil if it's in the string body. B\", B\(, E\), E\" are the positions of
6373 ;; the opening and closing quotes and parentheses of a correctly terminated
6374 ;; raw string. (N.B.: E\) and E\" are NOT on the "outside" of these
6375 ;; characters.) If the raw string is not terminated, E\) and E\" are set to
6376 ;; nil.
6378 ;; Note: this routine is dependant upon the correct syntax-table text
6379 ;; properties being set.
6380 (let ((state (c-state-semi-pp-to-literal (point)))
6381 open-quote-pos open-paren-pos close-paren-pos close-quote-pos id)
6382 (save-excursion
6383 (when
6384 (and
6385 (cond
6386 ((null (cadr state))
6387 (or (eq (char-after) ?\")
6388 (search-backward "\"" (max (- (point) 17) (point-min)) t)))
6389 ((and (eq (cadr state) 'string)
6390 (goto-char (nth 2 state))
6391 (or (eq (char-after) ?\")
6392 (search-backward "\"" (max (- (point) 17) (point-min)) t))
6393 (not (bobp)))))
6394 (eq (char-before) ?R)
6395 (looking-at "\"\\([^ ()\\\n\r\t]\\{0,16\\}\\)("))
6396 (setq open-quote-pos (point)
6397 open-paren-pos (match-end 1)
6398 id (match-string-no-properties 1))
6399 (goto-char (1+ open-paren-pos))
6400 (when (and (not (c-get-char-property open-paren-pos 'syntax-table))
6401 (search-forward (concat ")" id "\"") nil t))
6402 (setq close-paren-pos (match-beginning 0)
6403 close-quote-pos (1- (point))))))
6404 (and open-quote-pos
6405 (list
6406 (cond
6407 ((<= (point) open-paren-pos)
6408 'open-delim)
6409 ((and close-paren-pos
6410 (> (point) close-paren-pos))
6411 'close-delim)
6412 (t nil))
6413 open-quote-pos open-paren-pos close-paren-pos close-quote-pos))))
6415 (defun c-depropertize-raw-string (id open-quote open-paren bound)
6416 ;; Point is immediately after a raw string opening delimiter. Remove any
6417 ;; `syntax-table' text properties associated with the delimiter (if it's
6418 ;; unmatched) or the raw string.
6420 ;; ID, a string, is the delimiter's identifier. OPEN-QUOTE and OPEN-PAREN
6421 ;; are the buffer positions of the delimiter's components. BOUND is the
6422 ;; bound for searching for a matching closing delimiter; it is usually nil,
6423 ;; but if we're inside a macro, it's the end of the macro.
6425 ;; Point is moved to after the (terminated) raw string, or left after the
6426 ;; unmatched opening delimiter, as the case may be. The return value is of
6427 ;; no significance.
6428 (let ((open-paren-prop (c-get-char-property open-paren 'syntax-table)))
6429 (cond
6430 ((null open-paren-prop)
6431 ;; A terminated raw string
6432 (when (search-forward (concat ")" id "\"") nil t)
6433 (let* ((closing-paren (match-beginning 0))
6434 (first-punctuation
6435 (save-match-data
6436 (goto-char (1+ open-paren))
6437 (and (c-search-forward-char-property 'syntax-table '(1)
6438 closing-paren)
6439 (1- (point)))))
6441 (when first-punctuation
6442 (c-clear-char-property-with-value
6443 first-punctuation (match-beginning 0) 'syntax-table '(1))
6444 (c-truncate-semi-nonlit-pos-cache first-punctuation)
6445 ))))
6446 ((or (and (equal open-paren-prop '(15)) (null bound))
6447 (equal open-paren-prop '(1)))
6448 ;; An unterminated raw string either not in a macro, or in a macro with
6449 ;; the open parenthesis right up against the end of macro
6450 (c-clear-char-property open-quote 'syntax-table)
6451 (c-truncate-semi-nonlit-pos-cache open-quote)
6452 (c-clear-char-property open-paren 'syntax-table))
6454 ;; An unterminated string in a macro, with at least one char after the
6455 ;; open paren
6456 (c-clear-char-property open-quote 'syntax-table)
6457 (c-truncate-semi-nonlit-pos-cache open-quote)
6458 (c-clear-char-property open-paren 'syntax-table)
6459 (let ((after-string-fence-pos
6460 (save-excursion
6461 (goto-char (1+ open-paren))
6462 (c-search-forward-char-property 'syntax-table '(15) bound))))
6463 (when after-string-fence-pos
6464 (c-clear-char-property (1- after-string-fence-pos) 'syntax-table)))
6465 ))))
6467 (defun c-depropertize-raw-strings-in-region (start finish)
6468 ;; Remove any `syntax-table' text properties associated with C++ raw strings
6469 ;; contained in the region (START FINISH). Point is undefined at entry and
6470 ;; exit, and the return value has no significance.
6471 (goto-char start)
6472 (while (and (< (point) finish)
6473 (re-search-forward
6474 (concat "\\(" ; 1
6475 c-anchored-cpp-prefix ; 2
6476 "\\)\\|\\(" ; 3
6477 "R\"\\([^ ()\\\n\r\t]\\{0,16\\}\\)(" ; 4
6478 "\\)")
6479 finish t))
6480 (when (save-excursion
6481 (goto-char (match-beginning 0)) (not (c-in-literal)))
6482 (if (match-beginning 4) ; the id
6483 ;; We've found a raw string
6484 (c-depropertize-raw-string
6485 (match-string-no-properties 4) ; id
6486 (1+ (match-beginning 3)) ; open quote
6487 (match-end 4) ; open paren
6488 nil) ; bound
6489 ;; We've found a CPP construct. Search for raw strings within it.
6490 (goto-char (match-beginning 2)) ; the "#"
6491 (c-end-of-macro)
6492 (let ((eom (point)))
6493 (goto-char (match-end 2)) ; after the "#".
6494 (while (and (< (point) eom)
6495 (c-syntactic-re-search-forward
6496 "R\"\\([^ ()\\\n\r\t]\\{0,16\\}\\)(" eom t))
6497 (c-depropertize-raw-string
6498 (match-string-no-properties 1) ; id
6499 (1+ (match-beginning 0)) ; open quote
6500 (match-end 1) ; open paren
6501 eom))))))) ; bound.
6503 (defun c-before-change-check-raw-strings (beg end)
6504 ;; This function clears `syntax-table' text properties from C++ raw strings
6505 ;; in the region (c-new-BEG c-new-END). BEG and END are the standard
6506 ;; arguments supplied to any before-change function.
6508 ;; Point is undefined on both entry and exit, and the return value has no
6509 ;; significance.
6511 ;; This function is called as a before-change function solely due to its
6512 ;; membership of the C++ value of `c-get-state-before-change-functions'.
6513 (c-save-buffer-state
6514 ((beg-rs (progn (goto-char beg) (c-raw-string-pos)))
6515 (beg-plus (if (null beg-rs)
6517 (max beg
6518 (1+ (or (nth 4 beg-rs) (nth 2 beg-rs))))))
6519 (end-rs (progn (goto-char end) (c-raw-string-pos))) ; FIXME!!!
6520 ; Optimize this so that we don't call
6521 ; `c-raw-string-pos' twice when once
6522 ; will do. (2016-06-02).
6523 (end-minus (if (null end-rs)
6525 (min end (cadr end-rs))))
6527 (when beg-rs
6528 (setq c-new-BEG (min c-new-BEG (1- (cadr beg-rs)))))
6529 (c-depropertize-raw-strings-in-region c-new-BEG beg-plus)
6531 (when end-rs
6532 (setq c-new-END (max c-new-END
6533 (1+ (or (nth 4 end-rs)
6534 (nth 2 end-rs))))))
6535 (c-depropertize-raw-strings-in-region end-minus c-new-END)))
6537 (defun c-propertize-raw-string-opener (id open-quote open-paren bound)
6538 ;; Point is immediately after a raw string opening delimiter. Apply any
6539 ;; pertinent `syntax-table' text properties to the delimiter and also the
6540 ;; raw string, should there be a valid matching closing delimiter.
6542 ;; ID, a string, is the delimiter's identifier. OPEN-QUOTE and OPEN-PAREN
6543 ;; are the buffer positions of the delimiter's components. BOUND is the
6544 ;; bound for searching for a matching closing delimiter; it is usually nil,
6545 ;; but if we're inside a macro, it's the end of the macro.
6547 ;; Point is moved to after the (terminated) raw string, or left after the
6548 ;; unmatched opening delimiter, as the case may be. The return value is of
6549 ;; no significance.
6550 (if (search-forward (concat ")" id "\"") bound t)
6551 (let ((end-string (match-beginning 0))
6552 (after-quote (match-end 0)))
6553 (goto-char open-paren)
6554 (while (progn (skip-syntax-forward "^\"" end-string)
6555 (< (point) end-string))
6556 (c-put-char-property (point) 'syntax-table '(1)) ; punctuation
6557 (c-truncate-semi-nonlit-pos-cache (point))
6558 (forward-char))
6559 (goto-char after-quote))
6560 (c-put-char-property open-quote 'syntax-table '(1)) ; punctuation
6561 (c-truncate-semi-nonlit-pos-cache open-quote)
6562 (c-put-char-property open-paren 'syntax-table '(15)) ; generic string
6563 (when bound
6564 ;; In a CPP construct, we try to apply a generic-string `syntax-table'
6565 ;; text property to the last possible character in the string, so that
6566 ;; only characters within the macro get "stringed out".
6567 (goto-char bound)
6568 (if (save-restriction
6569 (narrow-to-region (1+ open-paren) (point-max))
6570 (re-search-backward
6571 (eval-when-compile
6572 ;; This regular expression matches either an escape pair (which
6573 ;; isn't an escaped NL) (submatch 5) or a non-escaped character
6574 ;; (which isn't itself a backslash) (submatch 10). The long
6575 ;; preambles to these (respectively submatches 2-4 and 6-9)
6576 ;; ensure that we have the correct parity for sequences of
6577 ;; backslashes, etc..
6578 (concat "\\(" ; 1
6579 "\\(\\`[^\\]?\\|[^\\][^\\]\\)\\(\\\\\\(.\\|\n\\)\\)*" ; 2-4
6580 "\\(\\\\.\\)" ; 5
6581 "\\|"
6582 "\\(\\`\\|[^\\]\\|\\(\\`[^\\]?\\|[^\\][^\\]\\)\\(\\\\\\(.\\|\n\\)\\)+\\)" ; 6-9
6583 "\\([^\\]\\)" ; 10
6584 "\\)"
6585 "\\(\\\\\n\\)*\\=")) ; 11
6586 (1+ open-paren) t))
6587 (if (match-beginning 10)
6588 (progn
6589 (c-put-char-property (match-beginning 10) 'syntax-table '(15))
6590 (c-truncate-semi-nonlit-pos-cache (match-beginning 10)))
6591 (c-put-char-property (match-beginning 5) 'syntax-table '(1))
6592 (c-put-char-property (1+ (match-beginning 5)) 'syntax-table '(15))
6593 (c-truncate-semi-nonlit-pos-cache (1+ (match-beginning 5))))
6594 (c-put-char-property open-paren 'syntax-table '(1)))
6595 (goto-char bound))))
6597 (defun c-after-change-re-mark-raw-strings (beg end old-len)
6598 ;; This function applies `syntax-table' text properties to C++ raw strings
6599 ;; beginning in the region (c-new-BEG c-new-END). BEG, END, and OLD-LEN are
6600 ;; the standard arguments supplied to any after-change function.
6602 ;; Point is undefined on both entry and exit, and the return value has no
6603 ;; significance.
6605 ;; This function is called as an after-change function solely due to its
6606 ;; membership of the C++ value of `c-before-font-lock-functions'.
6607 (c-save-buffer-state ()
6608 ;; If the region (c-new-BEG c-new-END) has expanded, remove
6609 ;; `syntax-table' text-properties from the new piece(s).
6610 (when (< c-new-BEG c-old-BEG)
6611 (let ((beg-rs (progn (goto-char c-old-BEG) (c-raw-string-pos))))
6612 (c-depropertize-raw-strings-in-region
6613 c-new-BEG
6614 (if beg-rs
6615 (1+ (or (nth 4 beg-rs) (nth 2 beg-rs)))
6616 c-old-BEG))))
6617 (when (> c-new-END c-old-END)
6618 (let ((end-rs (progn (goto-char c-old-END) (c-raw-string-pos))))
6619 (c-depropertize-raw-strings-in-region
6620 (if end-rs
6621 (cadr end-rs)
6622 c-old-END)
6623 c-new-END)))
6625 (goto-char c-new-BEG)
6626 (while (and (< (point) c-new-END)
6627 (re-search-forward
6628 (concat "\\(" ; 1
6629 c-anchored-cpp-prefix ; 2
6630 "\\)\\|\\(" ; 3
6631 "R\"\\([^ ()\\\n\r\t]\\{0,16\\}\\)(" ; 4
6632 "\\)")
6633 c-new-END t))
6634 (when (save-excursion
6635 (goto-char (match-beginning 0)) (not (c-in-literal)))
6636 (if (match-beginning 4) ; the id
6637 ;; We've found a raw string.
6638 (c-propertize-raw-string-opener
6639 (match-string-no-properties 4) ; id
6640 (1+ (match-beginning 3)) ; open quote
6641 (match-end 4) ; open paren
6642 nil) ; bound
6643 ;; We've found a CPP construct. Search for raw strings within it.
6644 (goto-char (match-beginning 2)) ; the "#"
6645 (c-end-of-macro)
6646 (let ((eom (point)))
6647 (goto-char (match-end 2)) ; after the "#".
6648 (while (and (< (point) eom)
6649 (c-syntactic-re-search-forward
6650 "R\"\\([^ ()\\\n\r\t]\\{0,16\\}\\)(" eom t))
6651 (c-propertize-raw-string-opener
6652 (match-string-no-properties 1) ; id
6653 (1+ (match-beginning 0)) ; open quote
6654 (match-end 1) ; open paren
6655 eom)))))))) ; bound
6658 ;; Handling of small scale constructs like types and names.
6660 ;; Dynamically bound variable that instructs `c-forward-type' to also
6661 ;; treat possible types (i.e. those that it normally returns 'maybe or
6662 ;; 'found for) as actual types (and always return 'found for them).
6663 ;; This means that it records them in `c-record-type-identifiers' if
6664 ;; that is set, and that it adds them to `c-found-types'.
6665 (defvar c-promote-possible-types nil)
6667 ;; Dynamically bound variable that instructs `c-forward-<>-arglist' to
6668 ;; mark up successfully parsed arglists with paren syntax properties on
6669 ;; the surrounding angle brackets and with `c-<>-arg-sep' in the
6670 ;; `c-type' property of each argument separating comma.
6672 ;; Setting this variable also makes `c-forward-<>-arglist' recurse into
6673 ;; all arglists for side effects (i.e. recording types), otherwise it
6674 ;; exploits any existing paren syntax properties to quickly jump to the
6675 ;; end of already parsed arglists.
6677 ;; Marking up the arglists is not the default since doing that correctly
6678 ;; depends on a proper value for `c-restricted-<>-arglists'.
6679 (defvar c-parse-and-markup-<>-arglists nil)
6681 ;; Dynamically bound variable that instructs `c-forward-<>-arglist' to
6682 ;; not accept arglists that contain binary operators.
6684 ;; This is primarily used to handle C++ template arglists. C++
6685 ;; disambiguates them by checking whether the preceding name is a
6686 ;; template or not. We can't do that, so we assume it is a template
6687 ;; if it can be parsed as one. That usually works well since
6688 ;; comparison expressions on the forms "a < b > c" or "a < b, c > d"
6689 ;; in almost all cases would be pointless.
6691 ;; However, in function arglists, e.g. in "foo (a < b, c > d)", we
6692 ;; should let the comma separate the function arguments instead. And
6693 ;; in a context where the value of the expression is taken, e.g. in
6694 ;; "if (a < b || c > d)", it's probably not a template.
6695 (defvar c-restricted-<>-arglists nil)
6697 ;; Dynamically bound variables that instructs
6698 ;; `c-forward-keyword-clause', `c-forward-<>-arglist',
6699 ;; `c-forward-name', `c-forward-type', `c-forward-decl-or-cast-1', and
6700 ;; `c-forward-label' to record the ranges of all the type and
6701 ;; reference identifiers they encounter. They will build lists on
6702 ;; these variables where each element is a cons of the buffer
6703 ;; positions surrounding each identifier. This recording is only
6704 ;; activated when `c-record-type-identifiers' is non-nil.
6706 ;; All known types that can't be identifiers are recorded, and also
6707 ;; other possible types if `c-promote-possible-types' is set.
6708 ;; Recording is however disabled inside angle bracket arglists that
6709 ;; are encountered inside names and other angle bracket arglists.
6710 ;; Such occurrences are taken care of by `c-font-lock-<>-arglists'
6711 ;; instead.
6713 ;; Only the names in C++ template style references (e.g. "tmpl" in
6714 ;; "tmpl<a,b>::foo") are recorded as references, other references
6715 ;; aren't handled here.
6717 ;; `c-forward-label' records the label identifier(s) on
6718 ;; `c-record-ref-identifiers'.
6719 (defvar c-record-type-identifiers nil)
6720 (defvar c-record-ref-identifiers nil)
6722 ;; This variable will receive a cons cell of the range of the last
6723 ;; single identifier symbol stepped over by `c-forward-name' if it's
6724 ;; successful. This is the range that should be put on one of the
6725 ;; record lists above by the caller. It's assigned nil if there's no
6726 ;; such symbol in the name.
6727 (defvar c-last-identifier-range nil)
6729 (defmacro c-record-type-id (range)
6730 (if (eq (car-safe range) 'cons)
6731 ;; Always true.
6732 `(setq c-record-type-identifiers
6733 (cons ,range c-record-type-identifiers))
6734 `(let ((range ,range))
6735 (if range
6736 (setq c-record-type-identifiers
6737 (cons range c-record-type-identifiers))))))
6739 (defmacro c-record-ref-id (range)
6740 (if (eq (car-safe range) 'cons)
6741 ;; Always true.
6742 `(setq c-record-ref-identifiers
6743 (cons ,range c-record-ref-identifiers))
6744 `(let ((range ,range))
6745 (if range
6746 (setq c-record-ref-identifiers
6747 (cons range c-record-ref-identifiers))))))
6749 ;; Dynamically bound variable that instructs `c-forward-type' to
6750 ;; record the ranges of types that only are found. Behaves otherwise
6751 ;; like `c-record-type-identifiers'.
6752 (defvar c-record-found-types nil)
6754 (defmacro c-forward-keyword-prefixed-id (type)
6755 ;; Used internally in `c-forward-keyword-clause' to move forward
6756 ;; over a type (if TYPE is 'type) or a name (otherwise) which
6757 ;; possibly is prefixed by keywords and their associated clauses.
6758 ;; Try with a type/name first to not trip up on those that begin
6759 ;; with a keyword. Return t if a known or found type is moved
6760 ;; over. The point is clobbered if nil is returned. If range
6761 ;; recording is enabled, the identifier is recorded on as a type
6762 ;; if TYPE is 'type or as a reference if TYPE is 'ref.
6764 ;; This macro might do hidden buffer changes.
6765 `(let (res)
6766 (setq c-last-identifier-range nil)
6767 (while (if (setq res ,(if (eq type 'type)
6768 `(c-forward-type)
6769 `(c-forward-name)))
6771 (cond ((looking-at c-keywords-regexp)
6772 (c-forward-keyword-clause 1))
6773 ((and c-opt-cpp-prefix
6774 (looking-at c-noise-macro-with-parens-name-re))
6775 (c-forward-noise-clause)))))
6776 (when (memq res '(t known found prefix maybe))
6777 (when c-record-type-identifiers
6778 ,(if (eq type 'type)
6779 `(c-record-type-id c-last-identifier-range)
6780 `(c-record-ref-id c-last-identifier-range)))
6781 t)))
6783 (defmacro c-forward-id-comma-list (type update-safe-pos)
6784 ;; Used internally in `c-forward-keyword-clause' to move forward
6785 ;; over a comma separated list of types or names using
6786 ;; `c-forward-keyword-prefixed-id'.
6788 ;; This macro might do hidden buffer changes.
6789 `(while (and (progn
6790 ,(when update-safe-pos
6791 `(setq safe-pos (point)))
6792 (eq (char-after) ?,))
6793 (progn
6794 (forward-char)
6795 (c-forward-syntactic-ws)
6796 (c-forward-keyword-prefixed-id ,type)))))
6798 (defun c-forward-noise-clause ()
6799 ;; Point is at a c-noise-macro-with-parens-names macro identifier. Go
6800 ;; forward over this name, any parenthesis expression which follows it, and
6801 ;; any syntactic WS, ending up at the next token. If there is an unbalanced
6802 ;; paren expression, leave point at it. Always Return t.
6803 (c-forward-token-2)
6804 (if (and (eq (char-after) ?\()
6805 (c-go-list-forward))
6806 (c-forward-syntactic-ws))
6809 (defun c-forward-keyword-clause (match)
6810 ;; Submatch MATCH in the current match data is assumed to surround a
6811 ;; token. If it's a keyword, move over it and any immediately
6812 ;; following clauses associated with it, stopping at the start of
6813 ;; the next token. t is returned in that case, otherwise the point
6814 ;; stays and nil is returned. The kind of clauses that are
6815 ;; recognized are those specified by `c-type-list-kwds',
6816 ;; `c-ref-list-kwds', `c-colon-type-list-kwds',
6817 ;; `c-paren-nontype-kwds', `c-paren-type-kwds', `c-<>-type-kwds',
6818 ;; and `c-<>-arglist-kwds'.
6820 ;; This function records identifier ranges on
6821 ;; `c-record-type-identifiers' and `c-record-ref-identifiers' if
6822 ;; `c-record-type-identifiers' is non-nil.
6824 ;; Note that for `c-colon-type-list-kwds', which doesn't necessary
6825 ;; apply directly after the keyword, the type list is moved over
6826 ;; only when there is no unaccounted token before it (i.e. a token
6827 ;; that isn't moved over due to some other keyword list). The
6828 ;; identifier ranges in the list are still recorded if that should
6829 ;; be done, though.
6831 ;; This function might do hidden buffer changes.
6833 (let ((kwd-sym (c-keyword-sym (match-string match))) safe-pos pos
6834 ;; The call to `c-forward-<>-arglist' below is made after
6835 ;; `c-<>-sexp-kwds' keywords, so we're certain they actually
6836 ;; are angle bracket arglists and `c-restricted-<>-arglists'
6837 ;; should therefore be nil.
6838 (c-parse-and-markup-<>-arglists t)
6839 c-restricted-<>-arglists)
6841 (when kwd-sym
6842 (goto-char (match-end match))
6843 (c-forward-syntactic-ws)
6844 (setq safe-pos (point))
6846 (cond
6847 ((and (c-keyword-member kwd-sym 'c-type-list-kwds)
6848 (c-forward-keyword-prefixed-id type))
6849 ;; There's a type directly after a keyword in `c-type-list-kwds'.
6850 (c-forward-id-comma-list type t))
6852 ((and (c-keyword-member kwd-sym 'c-ref-list-kwds)
6853 (c-forward-keyword-prefixed-id ref))
6854 ;; There's a name directly after a keyword in `c-ref-list-kwds'.
6855 (c-forward-id-comma-list ref t))
6857 ((and (c-keyword-member kwd-sym 'c-paren-any-kwds)
6858 (eq (char-after) ?\())
6859 ;; There's an open paren after a keyword in `c-paren-any-kwds'.
6861 (forward-char)
6862 (when (and (setq pos (c-up-list-forward))
6863 (eq (char-before pos) ?\)))
6864 (when (and c-record-type-identifiers
6865 (c-keyword-member kwd-sym 'c-paren-type-kwds))
6866 ;; Use `c-forward-type' on every identifier we can find
6867 ;; inside the paren, to record the types.
6868 (while (c-syntactic-re-search-forward c-symbol-start pos t)
6869 (goto-char (match-beginning 0))
6870 (unless (c-forward-type)
6871 (looking-at c-symbol-key) ; Always matches.
6872 (goto-char (match-end 0)))))
6874 (goto-char pos)
6875 (c-forward-syntactic-ws)
6876 (setq safe-pos (point))))
6878 ((and (c-keyword-member kwd-sym 'c-<>-sexp-kwds)
6879 (eq (char-after) ?<)
6880 (c-forward-<>-arglist (c-keyword-member kwd-sym 'c-<>-type-kwds)))
6881 (c-forward-syntactic-ws)
6882 (setq safe-pos (point)))
6884 ((and (c-keyword-member kwd-sym 'c-nonsymbol-sexp-kwds)
6885 (not (looking-at c-symbol-start))
6886 (c-safe (c-forward-sexp) t))
6887 (c-forward-syntactic-ws)
6888 (setq safe-pos (point))))
6890 (when (c-keyword-member kwd-sym 'c-colon-type-list-kwds)
6891 (if (eq (char-after) ?:)
6892 ;; If we are at the colon already, we move over the type
6893 ;; list after it.
6894 (progn
6895 (forward-char)
6896 (c-forward-syntactic-ws)
6897 (when (c-forward-keyword-prefixed-id type)
6898 (c-forward-id-comma-list type t)))
6899 ;; Not at the colon, so stop here. But the identifier
6900 ;; ranges in the type list later on should still be
6901 ;; recorded.
6902 (and c-record-type-identifiers
6903 (progn
6904 ;; If a keyword matched both one of the types above and
6905 ;; this one, we match `c-colon-type-list-re' after the
6906 ;; clause matched above.
6907 (goto-char safe-pos)
6908 (looking-at c-colon-type-list-re))
6909 (progn
6910 (goto-char (match-end 0))
6911 (c-forward-syntactic-ws)
6912 (c-forward-keyword-prefixed-id type))
6913 ;; There's a type after the `c-colon-type-list-re' match
6914 ;; after a keyword in `c-colon-type-list-kwds'.
6915 (c-forward-id-comma-list type nil))))
6917 (goto-char safe-pos)
6918 t)))
6920 ;; cc-mode requires cc-fonts.
6921 (declare-function c-fontify-recorded-types-and-refs "cc-fonts" ())
6923 (defun c-forward-<>-arglist (all-types)
6924 ;; The point is assumed to be at a "<". Try to treat it as the open
6925 ;; paren of an angle bracket arglist and move forward to the
6926 ;; corresponding ">". If successful, the point is left after the
6927 ;; ">" and t is returned, otherwise the point isn't moved and nil is
6928 ;; returned. If ALL-TYPES is t then all encountered arguments in
6929 ;; the arglist that might be types are treated as found types.
6931 ;; The variable `c-parse-and-markup-<>-arglists' controls how this
6932 ;; function handles text properties on the angle brackets and argument
6933 ;; separating commas.
6935 ;; `c-restricted-<>-arglists' controls how lenient the template
6936 ;; arglist recognition should be.
6938 ;; This function records identifier ranges on
6939 ;; `c-record-type-identifiers' and `c-record-ref-identifiers' if
6940 ;; `c-record-type-identifiers' is non-nil.
6942 ;; This function might do hidden buffer changes.
6944 (let ((start (point))
6945 ;; If `c-record-type-identifiers' is set then activate
6946 ;; recording of any found types that constitute an argument in
6947 ;; the arglist.
6948 (c-record-found-types (if c-record-type-identifiers t)))
6949 (if (catch 'angle-bracket-arglist-escape
6950 (setq c-record-found-types
6951 (c-forward-<>-arglist-recur all-types)))
6952 (progn
6953 (when (consp c-record-found-types)
6954 (setq c-record-type-identifiers
6955 ;; `nconc' doesn't mind that the tail of
6956 ;; `c-record-found-types' is t.
6957 (nconc c-record-found-types c-record-type-identifiers)))
6960 (goto-char start)
6961 nil)))
6963 (defun c-forward-<>-arglist-recur (all-types)
6964 ;; Recursive part of `c-forward-<>-arglist'.
6966 ;; This function might do hidden buffer changes.
6967 (let ((start (point)) res pos
6968 ;; Cover this so that any recorded found type ranges are
6969 ;; automatically lost if it turns out to not be an angle
6970 ;; bracket arglist. It's propagated through the return value
6971 ;; on successful completion.
6972 (c-record-found-types c-record-found-types)
6973 ;; List that collects the positions after the argument
6974 ;; separating ',' in the arglist.
6975 arg-start-pos)
6976 ;; If the '<' has paren open syntax then we've marked it as an angle
6977 ;; bracket arglist before, so skip to the end.
6978 (if (and (not c-parse-and-markup-<>-arglists)
6979 (c-get-char-property (point) 'syntax-table))
6981 (progn
6982 (forward-char)
6983 (if (and (c-go-up-list-forward)
6984 (eq (char-before) ?>))
6986 ;; Got unmatched paren angle brackets. We don't clear the paren
6987 ;; syntax properties and retry, on the basis that it's very
6988 ;; unlikely that paren angle brackets become operators by code
6989 ;; manipulation. It's far more likely that it doesn't match due
6990 ;; to narrowing or some temporary change.
6991 (goto-char start)
6992 nil))
6994 (forward-char) ; Forward over the opening '<'.
6996 (unless (looking-at c-<-op-cont-regexp)
6997 ;; go forward one non-alphanumeric character (group) per iteration of
6998 ;; this loop.
6999 (while (and
7000 (progn
7001 (c-forward-syntactic-ws)
7002 (when (or (and c-record-type-identifiers all-types)
7003 (not (equal c-inside-<>-type-key "\\(\\<\\>\\)")))
7004 (c-forward-syntactic-ws)
7005 (cond
7006 ((eq (char-after) ??)
7007 (forward-char))
7008 ((and (looking-at c-identifier-start)
7009 (not (looking-at c-keywords-regexp)))
7010 (if (or (and all-types c-record-type-identifiers)
7011 (c-major-mode-is 'java-mode))
7012 ;; All encountered identifiers are types, so set the
7013 ;; promote flag and parse the type.
7014 (let ((c-promote-possible-types t)
7015 (c-record-found-types t))
7016 (c-forward-type))
7017 (c-forward-token-2))))
7019 (c-forward-syntactic-ws)
7021 (when (looking-at c-inside-<>-type-key)
7022 (goto-char (match-end 1))
7023 (c-forward-syntactic-ws)
7024 (let ((c-promote-possible-types t)
7025 (c-record-found-types t))
7026 (c-forward-type))
7027 (c-forward-syntactic-ws)))
7029 (setq pos (point)) ; e.g. first token inside the '<'
7031 ;; Note: These regexps exploit the match order in \| so
7032 ;; that "<>" is matched by "<" rather than "[^>:-]>".
7033 (c-syntactic-re-search-forward
7034 ;; Stop on ',', '|', '&', '+' and '-' to catch
7035 ;; common binary operators that could be between
7036 ;; two comparison expressions "a<b" and "c>d".
7037 ;; 2016-02-11: C++11 templates can now contain arithmetic
7038 ;; expressions, so template detection in C++ is now less
7039 ;; robust than it was.
7040 c-<>-notable-chars-re
7041 nil t t))
7043 (cond
7044 ((eq (char-before) ?>)
7045 ;; Either an operator starting with '>' or the end of
7046 ;; the angle bracket arglist.
7048 (if (save-excursion
7049 (c-backward-token-2)
7050 (looking-at c-multichar->-op-not->>-regexp))
7051 (progn
7052 (goto-char (match-end 0))
7053 t) ; Continue the loop.
7055 ;; The angle bracket arglist is finished.
7056 (when c-parse-and-markup-<>-arglists
7057 (while arg-start-pos
7058 (c-put-c-type-property (1- (car arg-start-pos))
7059 'c-<>-arg-sep)
7060 (setq arg-start-pos (cdr arg-start-pos)))
7061 (c-mark-<-as-paren start)
7062 (c-mark->-as-paren (1- (point))))
7063 (setq res t)
7064 nil)) ; Exit the loop.
7066 ((eq (char-before) ?<)
7067 ;; Either an operator starting with '<' or a nested arglist.
7068 (setq pos (point))
7069 (let (id-start id-end subres keyword-match)
7070 (cond
7071 ;; The '<' begins a multi-char operator.
7072 ((looking-at c-<-op-cont-regexp)
7073 (goto-char (match-end 0)))
7074 ;; We're at a nested <.....>
7075 ((progn
7076 (backward-char) ; to the '<'
7077 (and
7078 (save-excursion
7079 ;; There's always an identifier before an angle
7080 ;; bracket arglist, or a keyword in `c-<>-type-kwds'
7081 ;; or `c-<>-arglist-kwds'.
7082 (c-backward-syntactic-ws)
7083 (setq id-end (point))
7084 (c-simple-skip-symbol-backward)
7085 (when (or (setq keyword-match
7086 (looking-at c-opt-<>-sexp-key))
7087 (not (looking-at c-keywords-regexp)))
7088 (setq id-start (point))))
7089 (setq subres
7090 (let ((c-promote-possible-types t)
7091 (c-record-found-types t))
7092 (c-forward-<>-arglist-recur
7093 (and keyword-match
7094 (c-keyword-member
7095 (c-keyword-sym (match-string 1))
7096 'c-<>-type-kwds))))))
7097 (or subres (goto-char pos))
7098 subres)
7099 ;; It was an angle bracket arglist.
7100 (setq c-record-found-types subres)
7102 ;; Record the identifier before the template as a type
7103 ;; or reference depending on whether the arglist is last
7104 ;; in a qualified identifier.
7105 (when (and c-record-type-identifiers
7106 (not keyword-match))
7107 (if (and c-opt-identifier-concat-key
7108 (progn
7109 (c-forward-syntactic-ws)
7110 (looking-at c-opt-identifier-concat-key)))
7111 (c-record-ref-id (cons id-start id-end))
7112 (c-record-type-id (cons id-start id-end)))))
7114 ;; At a "less than" operator.
7116 ;; (forward-char) ; NO! We've already gone over the <.
7118 t) ; carry on looping.
7120 ((and
7121 (eq (char-before) ?\()
7122 (c-go-up-list-forward)
7123 (eq (char-before) ?\))))
7125 ((and (not c-restricted-<>-arglists)
7126 (or (and (eq (char-before) ?&)
7127 (not (eq (char-after) ?&)))
7128 (eq (char-before) ?,)))
7129 ;; Just another argument. Record the position. The
7130 ;; type check stuff that made us stop at it is at
7131 ;; the top of the loop.
7132 (setq arg-start-pos (cons (point) arg-start-pos)))
7135 ;; Got a character that can't be in an angle bracket
7136 ;; arglist argument. Abort using `throw', since
7137 ;; it's useless to try to find a surrounding arglist
7138 ;; if we're nested.
7139 (throw 'angle-bracket-arglist-escape nil))))))
7140 (if res
7141 (or c-record-found-types t)))))
7143 (defun c-backward-<>-arglist (all-types &optional limit)
7144 ;; The point is assumed to be directly after a ">". Try to treat it
7145 ;; as the close paren of an angle bracket arglist and move back to
7146 ;; the corresponding "<". If successful, the point is left at
7147 ;; the "<" and t is returned, otherwise the point isn't moved and
7148 ;; nil is returned. ALL-TYPES is passed on to
7149 ;; `c-forward-<>-arglist'.
7151 ;; If the optional LIMIT is given, it bounds the backward search.
7152 ;; It's then assumed to be at a syntactically relevant position.
7154 ;; This is a wrapper around `c-forward-<>-arglist'. See that
7155 ;; function for more details.
7157 (let ((start (point)))
7158 (backward-char)
7159 (if (and (not c-parse-and-markup-<>-arglists)
7160 (c-get-char-property (point) 'syntax-table))
7162 (if (and (c-go-up-list-backward)
7163 (eq (char-after) ?<))
7165 ;; See corresponding note in `c-forward-<>-arglist'.
7166 (goto-char start)
7167 nil)
7169 (while (progn
7170 (c-syntactic-skip-backward "^<;{}" limit t)
7172 (and
7173 (if (eq (char-before) ?<)
7175 ;; Stopped at bob or a char that isn't allowed in an
7176 ;; arglist, so we've failed.
7177 (goto-char start)
7178 nil)
7180 (if (> (point)
7181 (progn (c-beginning-of-current-token)
7182 (point)))
7183 ;; If we moved then the "<" was part of some
7184 ;; multicharacter token.
7187 (backward-char)
7188 (let ((beg-pos (point)))
7189 (if (c-forward-<>-arglist all-types)
7190 (cond ((= (point) start)
7191 ;; Matched the arglist. Break the while.
7192 (goto-char beg-pos)
7193 nil)
7194 ((> (point) start)
7195 ;; We started from a non-paren ">" inside an
7196 ;; arglist.
7197 (goto-char start)
7198 nil)
7200 ;; Matched a shorter arglist. Can be a nested
7201 ;; one so continue looking.
7202 (goto-char beg-pos)
7204 t))))))
7206 (/= (point) start))))
7208 (defun c-forward-name ()
7209 ;; Move forward over a complete name if at the beginning of one,
7210 ;; stopping at the next following token. A keyword, as such,
7211 ;; doesn't count as a name. If the point is not at something that
7212 ;; is recognized as a name then it stays put.
7214 ;; A name could be something as simple as "foo" in C or something as
7215 ;; complex as "X<Y<class A<int>::B, BIT_MAX >> b>, ::operator<> ::
7216 ;; Z<(a>b)> :: operator const X<&foo>::T Q::G<unsigned short
7217 ;; int>::*volatile const" in C++ (this function is actually little
7218 ;; more than a `looking-at' call in all modes except those that,
7219 ;; like C++, have `c-recognize-<>-arglists' set).
7221 ;; Return
7222 ;; o - nil if no name is found;
7223 ;; o - 'template if it's an identifier ending with an angle bracket
7224 ;; arglist;
7225 ;; o - 'operator of it's an operator identifier;
7226 ;; o - t if it's some other kind of name.
7228 ;; This function records identifier ranges on
7229 ;; `c-record-type-identifiers' and `c-record-ref-identifiers' if
7230 ;; `c-record-type-identifiers' is non-nil.
7232 ;; This function might do hidden buffer changes.
7234 (let ((pos (point)) (start (point)) res id-start id-end
7235 ;; Turn off `c-promote-possible-types' here since we might
7236 ;; call `c-forward-<>-arglist' and we don't want it to promote
7237 ;; every suspect thing in the arglist to a type. We're
7238 ;; typically called from `c-forward-type' in this case, and
7239 ;; the caller only wants the top level type that it finds to
7240 ;; be promoted.
7241 c-promote-possible-types)
7242 (while
7243 (and
7244 (looking-at c-identifier-key)
7246 (progn
7247 ;; Check for keyword. We go to the last symbol in
7248 ;; `c-identifier-key' first.
7249 (goto-char (setq id-end (match-end 0)))
7250 (c-simple-skip-symbol-backward)
7251 (setq id-start (point))
7253 (if (looking-at c-keywords-regexp)
7254 (when (and (c-major-mode-is 'c++-mode)
7255 (looking-at
7256 (cc-eval-when-compile
7257 (concat "\\(operator\\|\\(template\\)\\)"
7258 "\\(" (c-lang-const c-nonsymbol-key c++)
7259 "\\|$\\)")))
7260 (if (match-beginning 2)
7261 ;; "template" is only valid inside an
7262 ;; identifier if preceded by "::".
7263 (save-excursion
7264 (c-backward-syntactic-ws)
7265 (and (c-safe (backward-char 2) t)
7266 (looking-at "::")))
7269 ;; Handle a C++ operator or template identifier.
7270 (goto-char id-end)
7271 (c-forward-syntactic-ws)
7272 (cond ((eq (char-before id-end) ?e)
7273 ;; Got "... ::template".
7274 (let ((subres (c-forward-name)))
7275 (when subres
7276 (setq pos (point)
7277 res subres))))
7279 ((looking-at c-identifier-start)
7280 ;; Got a cast operator.
7281 (when (c-forward-type)
7282 (setq pos (point)
7283 res 'operator)
7284 ;; Now we should match a sequence of either
7285 ;; '*', '&' or a name followed by ":: *",
7286 ;; where each can be followed by a sequence
7287 ;; of `c-opt-type-modifier-key'.
7288 (while (cond ((looking-at "[*&]")
7289 (goto-char (match-end 0))
7291 ((looking-at c-identifier-start)
7292 (and (c-forward-name)
7293 (looking-at "::")
7294 (progn
7295 (goto-char (match-end 0))
7296 (c-forward-syntactic-ws)
7297 (eq (char-after) ?*))
7298 (progn
7299 (forward-char)
7300 t))))
7301 (while (progn
7302 (c-forward-syntactic-ws)
7303 (setq pos (point))
7304 (looking-at c-opt-type-modifier-key))
7305 (goto-char (match-end 1))))))
7307 ((looking-at c-overloadable-operators-regexp)
7308 ;; Got some other operator.
7309 (setq c-last-identifier-range
7310 (cons (point) (match-end 0)))
7311 (goto-char (match-end 0))
7312 (c-forward-syntactic-ws)
7313 (setq pos (point)
7314 res 'operator)))
7316 nil)
7318 ;; `id-start' is equal to `id-end' if we've jumped over
7319 ;; an identifier that doesn't end with a symbol token.
7320 ;; That can occur e.g. for Java import directives on the
7321 ;; form "foo.bar.*".
7322 (when (and id-start (/= id-start id-end))
7323 (setq c-last-identifier-range
7324 (cons id-start id-end)))
7325 (goto-char id-end)
7326 (c-forward-syntactic-ws)
7327 (setq pos (point)
7328 res t)))
7330 (progn
7331 (goto-char pos)
7332 (when (or c-opt-identifier-concat-key
7333 c-recognize-<>-arglists)
7335 (cond
7336 ((and c-opt-identifier-concat-key
7337 (looking-at c-opt-identifier-concat-key))
7338 ;; Got a concatenated identifier. This handles the
7339 ;; cases with tricky syntactic whitespace that aren't
7340 ;; covered in `c-identifier-key'.
7341 (goto-char (match-end 0))
7342 (c-forward-syntactic-ws)
7345 ((and c-recognize-<>-arglists
7346 (eq (char-after) ?<))
7347 ;; Maybe an angle bracket arglist.
7348 (when (let (c-last-identifier-range)
7349 (c-forward-<>-arglist nil))
7351 (c-forward-syntactic-ws)
7352 (unless (eq (char-after) ?\()
7353 (setq c-last-identifier-range nil)
7354 (c-add-type start (1+ pos)))
7355 (setq pos (point))
7357 (if (and c-opt-identifier-concat-key
7358 (looking-at c-opt-identifier-concat-key))
7360 ;; Continue if there's an identifier concatenation
7361 ;; operator after the template argument.
7362 (progn
7363 (when (and c-record-type-identifiers id-start)
7364 (c-record-ref-id (cons id-start id-end)))
7365 (forward-char 2)
7366 (c-forward-syntactic-ws)
7369 (when (and c-record-type-identifiers id-start
7370 (not (eq (char-after) ?\()))
7371 (c-record-type-id (cons id-start id-end)))
7372 (setq res 'template)
7373 nil)))
7374 )))))
7376 (goto-char pos)
7377 res))
7379 (defun c-forward-type (&optional brace-block-too)
7380 ;; Move forward over a type spec if at the beginning of one,
7381 ;; stopping at the next following token. The keyword "typedef"
7382 ;; isn't part of a type spec here.
7384 ;; BRACE-BLOCK-TOO, when non-nil, means move over the brace block in
7385 ;; constructs like "struct foo {...} bar ;" or "struct {...} bar;".
7386 ;; The current (2009-03-10) intention is to convert all uses of
7387 ;; `c-forward-type' to call with this parameter set, then to
7388 ;; eliminate it.
7390 ;; Return
7391 ;; o - t if it's a known type that can't be a name or other
7392 ;; expression;
7393 ;; o - 'known if it's an otherwise known type (according to
7394 ;; `*-font-lock-extra-types');
7395 ;; o - 'prefix if it's a known prefix of a type;
7396 ;; o - 'found if it's a type that matches one in `c-found-types';
7397 ;; o - 'maybe if it's an identifier that might be a type;
7398 ;; o - 'decltype if it's a decltype(variable) declaration; - or
7399 ;; o - nil if it can't be a type (the point isn't moved then).
7401 ;; The point is assumed to be at the beginning of a token.
7403 ;; Note that this function doesn't skip past the brace definition
7404 ;; that might be considered part of the type, e.g.
7405 ;; "enum {a, b, c} foo".
7407 ;; This function records identifier ranges on
7408 ;; `c-record-type-identifiers' and `c-record-ref-identifiers' if
7409 ;; `c-record-type-identifiers' is non-nil.
7411 ;; This function might do hidden buffer changes.
7412 (when (and c-recognize-<>-arglists
7413 (looking-at "<"))
7414 (c-forward-<>-arglist t)
7415 (c-forward-syntactic-ws))
7417 (let ((start (point)) pos res name-res id-start id-end id-range)
7419 ;; Skip leading type modifiers. If any are found we know it's a
7420 ;; prefix of a type.
7421 (when c-opt-type-modifier-key ; e.g. "const" "volatile", but NOT "typedef"
7422 (while (looking-at c-opt-type-modifier-key)
7423 (goto-char (match-end 1))
7424 (c-forward-syntactic-ws)
7425 (setq res 'prefix)))
7427 (cond
7428 ((looking-at c-typeof-key) ; e.g. C++'s "decltype".
7429 (goto-char (match-end 1))
7430 (c-forward-syntactic-ws)
7431 (setq res (and (eq (char-after) ?\()
7432 (c-safe (c-forward-sexp))
7433 'decltype))
7434 (if res
7435 (c-forward-syntactic-ws)
7436 (goto-char start)))
7438 ((looking-at c-type-prefix-key) ; e.g. "struct", "class", but NOT
7439 ; "typedef".
7440 (goto-char (match-end 1))
7441 (c-forward-syntactic-ws)
7443 (while (cond
7444 ((looking-at c-decl-hangon-key)
7445 (c-forward-keyword-clause 1))
7446 ((looking-at c-pack-key)
7447 (goto-char (match-end 1))
7448 (c-forward-syntactic-ws))
7449 ((and c-opt-cpp-prefix
7450 (looking-at c-noise-macro-with-parens-name-re))
7451 (c-forward-noise-clause))))
7453 (setq pos (point))
7455 (setq name-res (c-forward-name))
7456 (setq res (not (null name-res)))
7457 (when (eq name-res t)
7458 ;; In many languages the name can be used without the
7459 ;; prefix, so we add it to `c-found-types'.
7460 (c-add-type pos (point))
7461 (when (and c-record-type-identifiers
7462 c-last-identifier-range)
7463 (c-record-type-id c-last-identifier-range)))
7464 (when (and brace-block-too
7465 (memq res '(t nil))
7466 (eq (char-after) ?\{)
7467 (save-excursion
7468 (c-safe
7469 (progn (c-forward-sexp)
7470 (c-forward-syntactic-ws)
7471 (setq pos (point))))))
7472 (goto-char pos)
7473 (setq res t))
7474 (unless res (goto-char start))) ; invalid syntax
7476 ((progn
7477 (setq pos nil)
7478 (if (looking-at c-identifier-start)
7479 (save-excursion
7480 (setq id-start (point)
7481 name-res (c-forward-name))
7482 (when name-res
7483 (setq id-end (point)
7484 id-range c-last-identifier-range))))
7485 (and (cond ((looking-at c-primitive-type-key)
7486 (setq res t))
7487 ((c-with-syntax-table c-identifier-syntax-table
7488 (looking-at c-known-type-key))
7489 (setq res 'known)))
7490 (or (not id-end)
7491 (>= (save-excursion
7492 (save-match-data
7493 (goto-char (match-end 1))
7494 (c-forward-syntactic-ws)
7495 (setq pos (point))))
7496 id-end)
7497 (setq res nil))))
7498 ;; Looking at a primitive or known type identifier. We've
7499 ;; checked for a name first so that we don't go here if the
7500 ;; known type match only is a prefix of another name.
7502 (setq id-end (match-end 1))
7504 (when (and c-record-type-identifiers
7505 (or c-promote-possible-types (eq res t)))
7506 (c-record-type-id (cons (match-beginning 1) (match-end 1))))
7508 (if (and c-opt-type-component-key
7509 (save-match-data
7510 (looking-at c-opt-type-component-key)))
7511 ;; There might be more keywords for the type.
7512 (let (safe-pos)
7513 (c-forward-keyword-clause 1)
7514 (while (progn
7515 (setq safe-pos (point))
7516 (looking-at c-opt-type-component-key))
7517 (when (and c-record-type-identifiers
7518 (looking-at c-primitive-type-key))
7519 (c-record-type-id (cons (match-beginning 1)
7520 (match-end 1))))
7521 (c-forward-keyword-clause 1))
7522 (if (looking-at c-primitive-type-key)
7523 (progn
7524 (when c-record-type-identifiers
7525 (c-record-type-id (cons (match-beginning 1)
7526 (match-end 1))))
7527 (c-forward-keyword-clause 1)
7528 (setq res t))
7529 (goto-char safe-pos)
7530 (setq res 'prefix)))
7531 (unless (save-match-data (c-forward-keyword-clause 1))
7532 (if pos
7533 (goto-char pos)
7534 (goto-char (match-end 1))
7535 (c-forward-syntactic-ws)))))
7537 (name-res
7538 (cond ((eq name-res t)
7539 ;; A normal identifier.
7540 (goto-char id-end)
7541 (if (or res c-promote-possible-types)
7542 (progn
7543 (c-add-type id-start id-end)
7544 (when (and c-record-type-identifiers id-range)
7545 (c-record-type-id id-range))
7546 (unless res
7547 (setq res 'found)))
7548 (setq res (if (c-check-type id-start id-end)
7549 ;; It's an identifier that has been used as
7550 ;; a type somewhere else.
7551 'found
7552 ;; It's an identifier that might be a type.
7553 'maybe))))
7554 ((eq name-res 'template)
7555 ;; A template is sometimes a type.
7556 (goto-char id-end)
7557 (c-forward-syntactic-ws)
7558 (setq res
7559 (if (eq (char-after) ?\()
7560 (if (c-check-type id-start id-end)
7561 ;; It's an identifier that has been used as
7562 ;; a type somewhere else.
7563 'found
7564 ;; It's an identifier that might be a type.
7565 'maybe)
7566 t)))
7568 ;; Otherwise it's an operator identifier, which is not a type.
7569 (goto-char start)
7570 (setq res nil)))))
7572 (when res
7573 ;; Skip trailing type modifiers. If any are found we know it's
7574 ;; a type.
7575 (when c-opt-type-modifier-key
7576 (while (looking-at c-opt-type-modifier-key) ; e.g. "const", "volatile"
7577 (goto-char (match-end 1))
7578 (c-forward-syntactic-ws)
7579 (setq res t)))
7581 ;; Step over any type suffix operator. Do not let the existence
7582 ;; of these alter the classification of the found type, since
7583 ;; these operators typically are allowed in normal expressions
7584 ;; too.
7585 (when c-opt-type-suffix-key ; e.g. "..."
7586 (while (looking-at c-opt-type-suffix-key)
7587 (goto-char (match-end 1))
7588 (c-forward-syntactic-ws)))
7590 ;; Skip any "WS" identifiers (e.g. "final" or "override" in C++)
7591 (while (looking-at c-type-decl-suffix-ws-ids-key)
7592 (goto-char (match-end 1))
7593 (c-forward-syntactic-ws)
7594 (setq res t))
7596 (when c-opt-type-concat-key ; Only/mainly for pike.
7597 ;; Look for a trailing operator that concatenates the type
7598 ;; with a following one, and if so step past that one through
7599 ;; a recursive call. Note that we don't record concatenated
7600 ;; types in `c-found-types' - it's the component types that
7601 ;; are recorded when appropriate.
7602 (setq pos (point))
7603 (let* ((c-promote-possible-types (or (memq res '(t known))
7604 c-promote-possible-types))
7605 ;; If we can't promote then set `c-record-found-types' so that
7606 ;; we can merge in the types from the second part afterwards if
7607 ;; it turns out to be a known type there.
7608 (c-record-found-types (and c-record-type-identifiers
7609 (not c-promote-possible-types)))
7610 subres)
7611 (if (and (looking-at c-opt-type-concat-key)
7613 (progn
7614 (goto-char (match-end 1))
7615 (c-forward-syntactic-ws)
7616 (setq subres (c-forward-type))))
7618 (progn
7619 ;; If either operand certainly is a type then both are, but we
7620 ;; don't let the existence of the operator itself promote two
7621 ;; uncertain types to a certain one.
7622 (cond ((eq res t))
7623 ((eq subres t)
7624 (unless (eq name-res 'template)
7625 (c-add-type id-start id-end))
7626 (when (and c-record-type-identifiers id-range)
7627 (c-record-type-id id-range))
7628 (setq res t))
7629 ((eq res 'known))
7630 ((eq subres 'known)
7631 (setq res 'known))
7632 ((eq res 'found))
7633 ((eq subres 'found)
7634 (setq res 'found))
7636 (setq res 'maybe)))
7638 (when (and (eq res t)
7639 (consp c-record-found-types))
7640 ;; Merge in the ranges of any types found by the second
7641 ;; `c-forward-type'.
7642 (setq c-record-type-identifiers
7643 ;; `nconc' doesn't mind that the tail of
7644 ;; `c-record-found-types' is t.
7645 (nconc c-record-found-types
7646 c-record-type-identifiers))))
7648 (goto-char pos))))
7650 (when (and c-record-found-types (memq res '(known found)) id-range)
7651 (setq c-record-found-types
7652 (cons id-range c-record-found-types))))
7654 ;;(message "c-forward-type %s -> %s: %s" start (point) res)
7656 res))
7658 (defun c-forward-annotation ()
7659 ;; Used for Java code only at the moment. Assumes point is on the @, moves
7660 ;; forward an annotation and returns t. Leaves point unmoved and returns
7661 ;; nil if there is no annotation at point.
7662 (let ((pos (point)))
7664 (and (looking-at "@")
7665 (not (looking-at c-keywords-regexp))
7666 (progn (forward-char) t)
7667 (looking-at c-symbol-key)
7668 (progn (goto-char (match-end 0))
7669 (c-forward-syntactic-ws)
7671 (if (looking-at "(")
7672 (c-go-list-forward)
7674 (progn (goto-char pos) nil))))
7676 (defmacro c-pull-open-brace (ps)
7677 ;; Pull the next open brace from PS (which has the form of paren-state),
7678 ;; skipping over any brace pairs. Returns NIL when PS is exhausted.
7679 `(progn
7680 (while (consp (car ,ps))
7681 (setq ,ps (cdr ,ps)))
7682 (prog1 (car ,ps)
7683 (setq ,ps (cdr ,ps)))))
7685 (defun c-back-over-compound-identifier ()
7686 ;; Point is putatively just after a "compound identifier", i.e. something
7687 ;; looking (in C++) like this "FQN::of::base::Class". Move to the start of
7688 ;; this construct and return t. If the parsing fails, return nil, leaving
7689 ;; point unchanged.
7690 (let ((here (point))
7691 end)
7692 (if (not (c-on-identifier))
7694 (c-simple-skip-symbol-backward)
7695 (while
7696 (progn
7697 (setq end (point))
7698 (c-backward-syntactic-ws)
7699 (c-backward-token-2)
7700 (and
7701 c-opt-identifier-concat-key
7702 (looking-at c-opt-identifier-concat-key)
7703 (progn
7704 (c-backward-syntactic-ws)
7705 (c-simple-skip-symbol-backward))))
7706 (setq end (point)))
7707 (goto-char end)
7708 t)))
7710 (defun c-back-over-member-initializer-braces ()
7711 ;; Point is just after a closing brace/parenthesis. Try to parse this as a
7712 ;; C++ member initializer list, going back to just after the introducing ":"
7713 ;; and returning t. Otherwise return nil, leaving point unchanged.
7714 (let ((here (point)) res)
7715 (setq res
7716 (catch 'done
7717 (when (not (c-go-list-backward))
7718 (throw 'done nil))
7719 (c-backward-syntactic-ws)
7720 (when (not (c-back-over-compound-identifier))
7721 (throw 'done nil))
7722 (c-backward-syntactic-ws)
7724 (while (eq (char-before) ?,)
7725 (backward-char)
7726 (c-backward-syntactic-ws)
7727 (when (not (memq (char-before) '(?\) ?})))
7728 (throw 'done nil))
7729 (when (not (c-go-list-backward))
7730 (throw 'done nil))
7731 (c-backward-syntactic-ws)
7732 (when (not (c-back-over-compound-identifier))
7733 (throw 'done nil))
7734 (c-backward-syntactic-ws))
7736 (eq (char-before) ?:)))
7737 (or res (goto-char here))
7738 res))
7740 (defmacro c-back-over-list-of-member-inits ()
7741 ;; Go back over a list of elements, each looking like:
7742 ;; <symbol> (<expression>) ,
7743 ;; or <symbol> {<expression>} , (with possibly a <....> expressions
7744 ;; following the <symbol>).
7745 ;; when we are putatively immediately after a comma. Stop when we don't see
7746 ;; a comma. If either of <symbol> or bracketed <expression> is missing,
7747 ;; throw nil to 'level. If the terminating } or ) is unmatched, throw nil
7748 ;; to 'done. This is not a general purpose macro!
7749 `(while (eq (char-before) ?,)
7750 (backward-char)
7751 (c-backward-syntactic-ws)
7752 (when (not (memq (char-before) '(?\) ?})))
7753 (throw 'level nil))
7754 (when (not (c-go-list-backward))
7755 (throw 'done nil))
7756 (c-backward-syntactic-ws)
7757 (while (eq (char-before) ?>)
7758 (when (not (c-backward-<>-arglist nil))
7759 (throw 'done nil))
7760 (c-backward-syntactic-ws))
7761 (when (not (c-back-over-compound-identifier))
7762 (throw 'level nil))
7763 (c-backward-syntactic-ws)))
7765 (defun c-back-over-member-initializers ()
7766 ;; Test whether we are in a C++ member initializer list, and if so, go back
7767 ;; to the introducing ":", returning the position of the opening paren of
7768 ;; the function's arglist. Otherwise return nil, leaving point unchanged.
7769 (let ((here (point))
7770 (paren-state (c-parse-state))
7771 pos level-plausible at-top-level res)
7772 ;; Assume tentatively that we're at the top level. Try to go back to the
7773 ;; colon we seek.
7774 (setq res
7775 (catch 'done
7776 (setq level-plausible
7777 (catch 'level
7778 (c-backward-syntactic-ws)
7779 (when (memq (char-before) '(?\) ?}))
7780 (when (not (c-go-list-backward))
7781 (throw 'done nil))
7782 (c-backward-syntactic-ws))
7783 (when (c-back-over-compound-identifier)
7784 (c-backward-syntactic-ws))
7785 (c-back-over-list-of-member-inits)
7786 (and (eq (char-before) ?:)
7787 (save-excursion
7788 (c-backward-token-2)
7789 (not (looking-at c-:$-multichar-token-regexp)))
7790 (c-just-after-func-arglist-p))))
7792 (while (and (not (and level-plausible
7793 (setq at-top-level (c-at-toplevel-p))))
7794 (setq pos (c-pull-open-brace paren-state))) ; might be a paren.
7795 (setq level-plausible
7796 (catch 'level
7797 (goto-char pos)
7798 (c-backward-syntactic-ws)
7799 (when (not (c-back-over-compound-identifier))
7800 (throw 'level nil))
7801 (c-backward-syntactic-ws)
7802 (c-back-over-list-of-member-inits)
7803 (and (eq (char-before) ?:)
7804 (save-excursion
7805 (c-backward-token-2)
7806 (not (looking-at c-:$-multichar-token-regexp)))
7807 (c-just-after-func-arglist-p)))))
7809 (and at-top-level level-plausible)))
7810 (or res (goto-char here))
7811 res))
7814 ;; Handling of large scale constructs like statements and declarations.
7816 ;; Macro used inside `c-forward-decl-or-cast-1'. It ought to be a
7817 ;; defsubst or perhaps even a defun, but it contains lots of free
7818 ;; variables that refer to things inside `c-forward-decl-or-cast-1'.
7819 (defmacro c-fdoc-shift-type-backward (&optional short)
7820 ;; `c-forward-decl-or-cast-1' can consume an arbitrary length list
7821 ;; of types when parsing a declaration, which means that it
7822 ;; sometimes consumes the identifier in the declaration as a type.
7823 ;; This is used to "backtrack" and make the last type be treated as
7824 ;; an identifier instead.
7825 `(progn
7826 ,(unless short
7827 ;; These identifiers are bound only in the inner let.
7828 '(setq identifier-type at-type
7829 identifier-start type-start
7830 got-parens nil
7831 got-identifier t
7832 got-suffix t
7833 got-suffix-after-parens id-start
7834 paren-depth 0))
7836 (if (setq at-type (if (eq backup-at-type 'prefix)
7838 backup-at-type))
7839 (setq type-start backup-type-start
7840 id-start backup-id-start)
7841 (setq type-start start-pos
7842 id-start start-pos))
7844 ;; When these flags already are set we've found specifiers that
7845 ;; unconditionally signal these attributes - backtracking doesn't
7846 ;; change that. So keep them set in that case.
7847 (or at-type-decl
7848 (setq at-type-decl backup-at-type-decl))
7849 (or maybe-typeless
7850 (setq maybe-typeless backup-maybe-typeless))
7852 ,(unless short
7853 ;; This identifier is bound only in the inner let.
7854 '(setq start id-start))))
7856 (defun c-forward-declarator (&optional limit accept-anon)
7857 ;; Assuming point is at the start of a declarator, move forward over it,
7858 ;; leaving point at the next token after it (e.g. a ) or a ; or a ,).
7860 ;; Return a list (ID-START ID-END BRACKETS-AFTER-ID GOT-INIT DECORATED),
7861 ;; where ID-START and ID-END are the bounds of the declarator's identifier,
7862 ;; and BRACKETS-AFTER-ID is non-nil if a [...] pair is present after the id.
7863 ;; GOT-INIT is non-nil when the declarator is followed by "=" or "(",
7864 ;; DECORATED is non-nil when the identifier is embellished by an operator,
7865 ;; like "*x", or "(*x)".
7867 ;; If ACCEPT-ANON is non-nil, move forward over any "anonymous declarator",
7868 ;; i.e. something like the (*) in int (*), such as might be found in a
7869 ;; declaration. In such a case ID-START and ID-END in the return value are
7870 ;; both set to nil. A "null" "anonymous declarator" gives a non-nil result.
7872 ;; If no declarator is found, leave point unmoved and return nil. LIMIT is
7873 ;; an optional limit for forward searching.
7875 ;; Note that the global variable `c-last-identifier-range' is written to, so
7876 ;; the caller should bind it if necessary.
7878 ;; Inside the following "condition form", we move forward over the
7879 ;; declarator's identifier up as far as any opening bracket (for array
7880 ;; size) or paren (for parameters of function-type) or brace (for
7881 ;; array/struct initialization) or "=" or terminating delimiter
7882 ;; (e.g. "," or ";" or "}").
7883 (let ((here (point))
7884 id-start id-end brackets-after-id paren-depth decorated)
7885 (or limit (setq limit (point-max)))
7886 (if (and
7887 (< (point) limit)
7889 ;; The following form moves forward over the declarator's
7890 ;; identifier (and what precedes it), returning t. If there
7891 ;; wasn't one, it returns nil.
7892 (let (got-identifier)
7893 (setq paren-depth 0)
7894 ;; Skip over type decl prefix operators, one for each iteration
7895 ;; of the while. These are, e.g. "*" in "int *foo" or "(" and
7896 ;; "*" in "int (*foo) (void)" (Note similar code in
7897 ;; `c-forward-decl-or-cast-1'.)
7898 (while
7899 (cond
7900 ((looking-at c-decl-hangon-key)
7901 (c-forward-keyword-clause 1))
7902 ((and c-opt-cpp-prefix
7903 (looking-at c-noise-macro-with-parens-name-re))
7904 (c-forward-noise-clause))
7905 ((and (looking-at c-type-decl-prefix-key)
7906 (if (and (c-major-mode-is 'c++-mode)
7907 (match-beginning 3))
7908 ;; If the third submatch matches in C++ then
7909 ;; we're looking at an identifier that's a
7910 ;; prefix only if it specifies a member pointer.
7911 (progn
7912 (setq id-start (point))
7913 (c-forward-name)
7914 (if (looking-at "\\(::\\)")
7915 ;; We only check for a trailing "::" and
7916 ;; let the "*" that should follow be
7917 ;; matched in the next round.
7919 ;; It turned out to be the real identifier,
7920 ;; so flag that and stop.
7921 (setq got-identifier t)
7922 nil))
7924 (if (looking-at c-type-decl-operator-prefix-key)
7925 (setq decorated t))
7926 (if (eq (char-after) ?\()
7927 (progn
7928 (setq paren-depth (1+ paren-depth))
7929 (forward-char))
7930 (goto-char (match-end 1)))
7931 (c-forward-syntactic-ws)
7932 t)))
7934 ;; If we haven't passed the identifier already, do it now.
7935 (unless got-identifier
7936 (setq id-start (point)))
7937 (cond
7938 ((or got-identifier
7939 (c-forward-name))
7940 (save-excursion
7941 (c-backward-syntactic-ws)
7942 (setq id-end (point))))
7943 (accept-anon
7944 (setq id-start nil id-end nil)
7946 (t (/= (point) here))))
7948 ;; Skip out of the parens surrounding the identifier. If closing
7949 ;; parens are missing, this form returns nil.
7950 (or (= paren-depth 0)
7951 (c-safe (goto-char (scan-lists (point) 1 paren-depth))))
7953 (<= (point) limit)
7955 ;; Skip over any trailing bit, such as "__attribute__".
7956 (progn
7957 (while (cond
7958 ((looking-at c-decl-hangon-key)
7959 (c-forward-keyword-clause 1))
7960 ((and c-opt-cpp-prefix
7961 (looking-at c-noise-macro-with-parens-name-re))
7962 (c-forward-noise-clause))))
7963 (<= (point) limit))
7965 ;; Search syntactically to the end of the declarator (";",
7966 ;; ",", a closing paren, eob etc) or to the beginning of an
7967 ;; initializer or function prototype ("=" or "\\s\(").
7968 ;; Note that square brackets are now not also treated as
7969 ;; initializers, since this broke when there were also
7970 ;; initializing brace lists.
7971 (let (found)
7972 (while
7973 (and (setq found (c-syntactic-re-search-forward
7974 "[;,]\\|\\s)\\|\\'\\|\\(=\\|\\s(\\)" limit t t))
7975 (eq (char-before) ?\[)
7976 (c-go-up-list-forward))
7977 (setq brackets-after-id t))
7978 (backward-char)
7979 found))
7980 (list id-start id-end brackets-after-id (match-beginning 1) decorated)
7982 (goto-char here)
7983 nil)))
7985 (defun c-forward-decl-or-cast-1 (preceding-token-end context last-cast-end)
7986 ;; Move forward over a declaration or a cast if at the start of one.
7987 ;; The point is assumed to be at the start of some token. Nil is
7988 ;; returned if no declaration or cast is recognized, and the point
7989 ;; is clobbered in that case.
7991 ;; If a declaration is parsed:
7993 ;; The point is left at the first token after the first complete
7994 ;; declarator, if there is one. The return value is a list of 4 elements,
7995 ;; where the first is the position of the first token in the declarator.
7996 ;; (See below for the other three.)
7997 ;; Some examples:
7999 ;; void foo (int a, char *b) stuff ...
8000 ;; car ^ ^ point
8001 ;; float (*a)[], b;
8002 ;; car ^ ^ point
8003 ;; unsigned int a = c_style_initializer, b;
8004 ;; car ^ ^ point
8005 ;; unsigned int a (cplusplus_style_initializer), b;
8006 ;; car ^ ^ point (might change)
8007 ;; class Foo : public Bar {}
8008 ;; car ^ ^ point
8009 ;; class PikeClass (int a, string b) stuff ...
8010 ;; car ^ ^ point
8011 ;; enum bool;
8012 ;; car ^ ^ point
8013 ;; enum bool flag;
8014 ;; car ^ ^ point
8015 ;; void cplusplus_function (int x) throw (Bad);
8016 ;; car ^ ^ point
8017 ;; Foo::Foo (int b) : Base (b) {}
8018 ;; car ^ ^ point
8020 ;; auto foo = 5;
8021 ;; car ^ ^ point
8022 ;; auto cplusplus_11 (int a, char *b) -> decltype (bar):
8023 ;; car ^ ^ point
8027 ;; The second element of the return value is non-nil when a
8028 ;; `c-typedef-decl-kwds' specifier is found in the declaration.
8029 ;; Specifically it is a dotted pair (A . B) where B is t when a
8030 ;; `c-typedef-kwds' ("typedef") is present, and A is t when some
8031 ;; other `c-typedef-decl-kwds' (e.g. class, struct, enum)
8032 ;; specifier is present. I.e., (some of) the declared
8033 ;; identifier(s) are types.
8035 ;; The third element of the return value is non-nil when the declaration
8036 ;; parsed might be an expression. The fourth element is the position of
8037 ;; the start of the type identifier.
8039 ;; If a cast is parsed:
8041 ;; The point is left at the first token after the closing paren of
8042 ;; the cast. The return value is `cast'. Note that the start
8043 ;; position must be at the first token inside the cast parenthesis
8044 ;; to recognize it.
8046 ;; PRECEDING-TOKEN-END is the first position after the preceding
8047 ;; token, i.e. on the other side of the syntactic ws from the point.
8048 ;; Use a value less than or equal to (point-min) if the point is at
8049 ;; the first token in (the visible part of) the buffer.
8051 ;; CONTEXT is a symbol that describes the context at the point:
8052 ;; 'decl In a comma-separated declaration context (typically
8053 ;; inside a function declaration arglist).
8054 ;; '<> In an angle bracket arglist.
8055 ;; 'arglist Some other type of arglist.
8056 ;; 'top Some other context and point is at the top-level (either
8057 ;; outside any braces or directly inside a class or namespace,
8058 ;; etc.)
8059 ;; nil Some other context or unknown context. Includes
8060 ;; within the parens of an if, for, ... construct.
8061 ;; 'not-decl This value is never supplied to this function. It
8062 ;; would mean we're definitely not in a declaration.
8064 ;; LAST-CAST-END is the first token after the closing paren of a
8065 ;; preceding cast, or nil if none is known. If
8066 ;; `c-forward-decl-or-cast-1' is used in succession, it should be
8067 ;; the position after the closest preceding call where a cast was
8068 ;; matched. In that case it's used to discover chains of casts like
8069 ;; "(a) (b) c".
8071 ;; This function records identifier ranges on
8072 ;; `c-record-type-identifiers' and `c-record-ref-identifiers' if
8073 ;; `c-record-type-identifiers' is non-nil.
8075 ;; This function might do hidden buffer changes.
8077 (let (;; `start-pos' is used below to point to the start of the
8078 ;; first type, i.e. after any leading specifiers. It might
8079 ;; also point at the beginning of the preceding syntactic
8080 ;; whitespace.
8081 (start-pos (point))
8082 ;; Set to the result of `c-forward-type'.
8083 at-type
8084 ;; The position of the first token in what we currently
8085 ;; believe is the type in the declaration or cast, after any
8086 ;; specifiers and their associated clauses.
8087 type-start
8088 ;; The position of the first token in what we currently
8089 ;; believe is the declarator for the first identifier. Set
8090 ;; when the type is found, and moved forward over any
8091 ;; `c-decl-hangon-kwds' and their associated clauses that
8092 ;; occurs after the type.
8093 id-start
8094 ;; These store `at-type', `type-start' and `id-start' of the
8095 ;; identifier before the one in those variables. The previous
8096 ;; identifier might turn out to be the real type in a
8097 ;; declaration if the last one has to be the declarator in it.
8098 ;; If `backup-at-type' is nil then the other variables have
8099 ;; undefined values.
8100 backup-at-type backup-type-start backup-id-start
8101 ;; This stores `kwd-sym' of the symbol before the current one.
8102 ;; This is needed to distinguish the C++11 version of "auto" from
8103 ;; the pre C++11 meaning.
8104 backup-kwd-sym
8105 ;; Set if we've found a specifier (apart from "typedef") that makes
8106 ;; the defined identifier(s) types.
8107 at-type-decl
8108 ;; Set if we've a "typedef" keyword.
8109 at-typedef
8110 ;; Set if we've found a specifier that can start a declaration
8111 ;; where there's no type.
8112 maybe-typeless
8113 ;; Save the value of kwd-sym between loops of the "Check for a
8114 ;; type" loop. Needed to distinguish a C++11 "auto" from a pre
8115 ;; C++11 one.
8116 prev-kwd-sym
8117 ;; If a specifier is found that also can be a type prefix,
8118 ;; these flags are set instead of those above. If we need to
8119 ;; back up an identifier, they are copied to the real flag
8120 ;; variables. Thus they only take effect if we fail to
8121 ;; interpret it as a type.
8122 backup-at-type-decl backup-maybe-typeless
8123 ;; Whether we've found a declaration or a cast. We might know
8124 ;; this before we've found the type in it. It's 'ids if we've
8125 ;; found two consecutive identifiers (usually a sure sign, but
8126 ;; we should allow that in labels too), and t if we've found a
8127 ;; specifier keyword (a 100% sure sign).
8128 at-decl-or-cast
8129 ;; Set when we need to back up to parse this as a declaration
8130 ;; but not as a cast.
8131 backup-if-not-cast
8132 ;; For casts, the return position.
8133 cast-end
8134 ;; Have we got a new-style C++11 "auto"?
8135 new-style-auto
8136 ;; Set when the symbol before `preceding-token-end' is known to
8137 ;; terminate the previous construct, or when we're at point-min.
8138 at-decl-start
8139 ;; Save `c-record-type-identifiers' and
8140 ;; `c-record-ref-identifiers' since ranges are recorded
8141 ;; speculatively and should be thrown away if it turns out
8142 ;; that it isn't a declaration or cast.
8143 (save-rec-type-ids c-record-type-identifiers)
8144 (save-rec-ref-ids c-record-ref-identifiers)
8145 ;; Set when we parse a declaration which might also be an expression,
8146 ;; such as "a *b". See CASE 16 and CASE 17.
8147 maybe-expression)
8149 (save-excursion
8150 (goto-char preceding-token-end)
8151 (setq at-decl-start
8152 (or (bobp)
8153 (let ((tok-end (point)))
8154 (c-backward-token-2)
8155 (member (buffer-substring-no-properties (point) tok-end)
8156 c-pre-start-tokens)))))
8158 (while (c-forward-annotation)
8159 (c-forward-syntactic-ws))
8161 ;; Check for a type. Unknown symbols are treated as possible
8162 ;; types, but they could also be specifiers disguised through
8163 ;; macros like __INLINE__, so we recognize both types and known
8164 ;; specifiers after them too.
8165 (while
8166 (let* ((start (point)) kwd-sym kwd-clause-end found-type noise-start)
8168 (cond
8169 ;; Look for a specifier keyword clause.
8170 ((or (looking-at c-prefix-spec-kwds-re)
8171 (and (c-major-mode-is 'java-mode)
8172 (looking-at "@[A-Za-z0-9]+")))
8173 (save-match-data
8174 (if (looking-at c-typedef-key)
8175 (setq at-typedef t)))
8176 (setq kwd-sym (c-keyword-sym (match-string 1)))
8177 (save-excursion
8178 (c-forward-keyword-clause 1)
8179 (setq kwd-clause-end (point))))
8180 ((and c-opt-cpp-prefix
8181 (looking-at c-noise-macro-with-parens-name-re))
8182 (setq noise-start (point))
8183 (c-forward-noise-clause)
8184 (setq kwd-clause-end (point))))
8186 (when (setq found-type (c-forward-type t)) ; brace-block-too
8187 ;; Found a known or possible type or a prefix of a known type.
8188 (when (and (c-major-mode-is 'c++-mode) ; C++11 style "auto"?
8189 (eq prev-kwd-sym (c-keyword-sym "auto"))
8190 (looking-at "[=(]")) ; FIXME!!! proper regexp.
8191 (setq new-style-auto t)
8192 (setq found-type nil)
8193 (goto-char start)) ; position of foo in "auto foo"
8195 (when at-type
8196 ;; Got two identifiers with nothing but whitespace
8197 ;; between them. That can only happen in declarations.
8198 (setq at-decl-or-cast 'ids)
8200 (when (eq at-type 'found)
8201 ;; If the previous identifier is a found type we
8202 ;; record it as a real one; it might be some sort of
8203 ;; alias for a prefix like "unsigned".
8204 (save-excursion
8205 (goto-char type-start)
8206 (let ((c-promote-possible-types t))
8207 (c-forward-type)))))
8209 (setq backup-at-type at-type
8210 backup-type-start type-start
8211 backup-id-start id-start
8212 backup-kwd-sym kwd-sym
8213 at-type found-type
8214 type-start start
8215 id-start (point)
8216 ;; The previous ambiguous specifier/type turned out
8217 ;; to be a type since we've parsed another one after
8218 ;; it, so clear these backup flags.
8219 backup-at-type-decl nil
8220 backup-maybe-typeless nil))
8222 (if (or kwd-sym noise-start)
8223 (progn
8224 ;; Handle known specifier keywords and
8225 ;; `c-decl-hangon-kwds' which can occur after known
8226 ;; types.
8228 (if (or (c-keyword-member kwd-sym 'c-decl-hangon-kwds)
8229 noise-start)
8230 ;; It's a hang-on keyword or noise clause that can occur
8231 ;; anywhere.
8232 (progn
8233 (if at-type
8234 ;; Move the identifier start position if
8235 ;; we've passed a type.
8236 (setq id-start kwd-clause-end)
8237 ;; Otherwise treat this as a specifier and
8238 ;; move the fallback position.
8239 (setq start-pos kwd-clause-end))
8240 (goto-char kwd-clause-end))
8242 ;; It's an ordinary specifier so we know that
8243 ;; anything before this can't be the type.
8244 (setq backup-at-type nil
8245 start-pos kwd-clause-end)
8247 (if found-type
8248 ;; It's ambiguous whether this keyword is a
8249 ;; specifier or a type prefix, so set the backup
8250 ;; flags. (It's assumed that `c-forward-type'
8251 ;; moved further than `c-forward-keyword-clause'.)
8252 (progn
8253 (when (c-keyword-member kwd-sym 'c-typedef-decl-kwds)
8254 (setq backup-at-type-decl t))
8255 (when (c-keyword-member kwd-sym 'c-typeless-decl-kwds)
8256 (setq backup-maybe-typeless t)))
8258 (when (c-keyword-member kwd-sym 'c-typedef-decl-kwds)
8259 ;; This test only happens after we've scanned a type.
8260 ;; So, with valid syntax, kwd-sym can't be 'typedef.
8261 (setq at-type-decl t))
8262 (when (c-keyword-member kwd-sym 'c-typeless-decl-kwds)
8263 (setq maybe-typeless t))
8265 ;; Haven't matched a type so it's an unambiguous
8266 ;; specifier keyword and we know we're in a
8267 ;; declaration.
8268 (setq at-decl-or-cast t)
8269 (setq prev-kwd-sym kwd-sym)
8271 (goto-char kwd-clause-end))))
8273 ;; If the type isn't known we continue so that we'll jump
8274 ;; over all specifiers and type identifiers. The reason
8275 ;; to do this for a known type prefix is to make things
8276 ;; like "unsigned INT16" work.
8277 (and found-type (not (eq found-type t))))))
8279 (cond
8280 ((eq at-type t)
8281 ;; If a known type was found, we still need to skip over any
8282 ;; hangon keyword clauses after it. Otherwise it has already
8283 ;; been done in the loop above.
8284 (while
8285 (cond ((looking-at c-decl-hangon-key)
8286 (c-forward-keyword-clause 1))
8287 ((and c-opt-cpp-prefix
8288 (looking-at c-noise-macro-with-parens-name-re))
8289 (c-forward-noise-clause))))
8290 (setq id-start (point)))
8292 ((eq at-type 'prefix)
8293 ;; A prefix type is itself a primitive type when it's not
8294 ;; followed by another type.
8295 (setq at-type t))
8297 ((not at-type)
8298 ;; Got no type but set things up to continue anyway to handle
8299 ;; the various cases when a declaration doesn't start with a
8300 ;; type.
8301 (setq id-start start-pos))
8303 ((and (eq at-type 'maybe)
8304 (c-major-mode-is 'c++-mode))
8305 ;; If it's C++ then check if the last "type" ends on the form
8306 ;; "foo::foo" or "foo::~foo", i.e. if it's the name of a
8307 ;; (con|de)structor.
8308 (save-excursion
8309 (let (name end-2 end-1)
8310 (goto-char id-start)
8311 (c-backward-syntactic-ws)
8312 (setq end-2 (point))
8313 (when (and
8314 (c-simple-skip-symbol-backward)
8315 (progn
8316 (setq name
8317 (buffer-substring-no-properties (point) end-2))
8318 ;; Cheating in the handling of syntactic ws below.
8319 (< (skip-chars-backward ":~ \t\n\r\v\f") 0))
8320 (progn
8321 (setq end-1 (point))
8322 (c-simple-skip-symbol-backward))
8323 (>= (point) type-start)
8324 (equal (buffer-substring-no-properties (point) end-1)
8325 name))
8326 ;; It is a (con|de)structor name. In that case the
8327 ;; declaration is typeless so zap out any preceding
8328 ;; identifier(s) that we might have taken as types.
8329 (goto-char type-start)
8330 (setq at-type nil
8331 backup-at-type nil
8332 id-start type-start))))))
8334 ;; Check for and step over a type decl expression after the thing
8335 ;; that is or might be a type. This can't be skipped since we
8336 ;; need the correct end position of the declarator for
8337 ;; `max-type-decl-end-*'.
8338 (let ((start (point)) (paren-depth 0) pos
8339 ;; True if there's a non-open-paren match of
8340 ;; `c-type-decl-prefix-key'.
8341 got-prefix
8342 ;; True if the declarator is surrounded by a parenthesis pair.
8343 got-parens
8344 ;; True if there is an identifier in the declarator.
8345 got-identifier
8346 ;; True if there's a non-close-paren match of
8347 ;; `c-type-decl-suffix-key'.
8348 got-suffix
8349 ;; True if there's a prefix match outside the outermost
8350 ;; paren pair that surrounds the declarator.
8351 got-prefix-before-parens
8352 ;; True if there's a suffix match outside the outermost
8353 ;; paren pair that surrounds the declarator. The value is
8354 ;; the position of the first suffix match.
8355 got-suffix-after-parens
8356 ;; True if we've parsed the type decl to a token that is
8357 ;; known to end declarations in this context.
8358 at-decl-end
8359 ;; The earlier values of `at-type' and `type-start' if we've
8360 ;; shifted the type backwards.
8361 identifier-type identifier-start
8362 ;; If `c-parse-and-markup-<>-arglists' is set we need to
8363 ;; turn it off during the name skipping below to avoid
8364 ;; getting `c-type' properties that might be bogus. That
8365 ;; can happen since we don't know if
8366 ;; `c-restricted-<>-arglists' will be correct inside the
8367 ;; arglist paren that gets entered.
8368 c-parse-and-markup-<>-arglists
8369 ;; Start of the identifier for which `got-identifier' was set.
8370 name-start
8371 ;; Position after (innermost) open parenthesis encountered in the
8372 ;; prefix operators.
8373 after-paren-pos)
8375 (goto-char id-start)
8377 ;; Skip over type decl prefix operators. (Note similar code in
8378 ;; `c-forward-declarator'.)
8379 (if (and c-recognize-typeless-decls
8380 (equal c-type-decl-prefix-key "\\<\\>"))
8381 (when (eq (char-after) ?\()
8382 (progn
8383 (setq paren-depth (1+ paren-depth))
8384 (forward-char)
8385 (setq after-paren-pos (point))))
8386 (while (and (looking-at c-type-decl-prefix-key)
8387 (if (and (c-major-mode-is 'c++-mode)
8388 (match-beginning 3))
8389 ;; If the third submatch matches in C++ then
8390 ;; we're looking at an identifier that's a
8391 ;; prefix only if it specifies a member pointer.
8392 (when (progn (setq pos (point))
8393 (setq got-identifier (c-forward-name)))
8394 (setq name-start pos)
8395 (if (looking-at "\\(::\\)")
8396 ;; We only check for a trailing "::" and
8397 ;; let the "*" that should follow be
8398 ;; matched in the next round.
8399 (progn (setq got-identifier nil) t)
8400 ;; It turned out to be the real identifier,
8401 ;; so stop.
8402 nil))
8405 (if (eq (char-after) ?\()
8406 (progn
8407 (setq paren-depth (1+ paren-depth))
8408 (forward-char)
8409 (setq after-paren-pos (point)))
8410 (unless got-prefix-before-parens
8411 (setq got-prefix-before-parens (= paren-depth 0)))
8412 (setq got-prefix t)
8413 (goto-char (match-end 1)))
8414 (c-forward-syntactic-ws)))
8416 (setq got-parens (> paren-depth 0))
8418 ;; Try to skip over an identifier.
8419 (or got-identifier
8420 (and (looking-at c-identifier-start)
8421 (setq pos (point))
8422 (setq got-identifier (c-forward-name))
8423 (setq name-start pos)))
8425 ;; Skip over type decl suffix operators and trailing noise macros.
8426 (while
8427 (cond
8428 ((and c-opt-cpp-prefix
8429 (looking-at c-noise-macro-with-parens-name-re))
8430 (c-forward-noise-clause))
8432 ((looking-at c-type-decl-suffix-key)
8433 (if (eq (char-after) ?\))
8434 (when (> paren-depth 0)
8435 (setq paren-depth (1- paren-depth))
8436 (forward-char)
8438 (when (if (save-match-data (looking-at "\\s("))
8439 (c-safe (c-forward-sexp 1) t)
8440 (goto-char (match-end 1))
8442 (when (and (not got-suffix-after-parens)
8443 (= paren-depth 0))
8444 (setq got-suffix-after-parens (match-beginning 0)))
8445 (setq got-suffix t))))
8448 ;; No suffix matched. We might have matched the
8449 ;; identifier as a type and the open paren of a
8450 ;; function arglist as a type decl prefix. In that
8451 ;; case we should "backtrack": Reinterpret the last
8452 ;; type as the identifier, move out of the arglist and
8453 ;; continue searching for suffix operators.
8455 ;; Do this even if there's no preceding type, to cope
8456 ;; with old style function declarations in K&R C,
8457 ;; (con|de)structors in C++ and `c-typeless-decl-kwds'
8458 ;; style declarations. That isn't applicable in an
8459 ;; arglist context, though.
8460 (when (and (= paren-depth 1)
8461 (not got-prefix-before-parens)
8462 (not (eq at-type t))
8463 (or backup-at-type
8464 maybe-typeless
8465 backup-maybe-typeless
8466 (when c-recognize-typeless-decls
8467 (and (memq context '(nil top))
8468 ;; Deal with C++11's "copy-initialization"
8469 ;; where we have <type>(<constant>), by
8470 ;; contrasting with a typeless
8471 ;; <name>(<type><parameter>, ...).
8472 (save-excursion
8473 (goto-char after-paren-pos)
8474 (c-forward-syntactic-ws)
8475 (c-forward-type)))))
8476 (setq pos (c-up-list-forward (point)))
8477 (eq (char-before pos) ?\)))
8478 (c-fdoc-shift-type-backward)
8479 (goto-char pos)
8480 t)))
8482 (c-forward-syntactic-ws))
8484 (when (or (and new-style-auto
8485 (looking-at c-auto-ops-re))
8486 (and (or maybe-typeless backup-maybe-typeless)
8487 (not got-identifier)
8488 (not got-prefix)
8489 at-type))
8490 ;; Have found no identifier but `c-typeless-decl-kwds' has
8491 ;; matched so we know we're inside a declaration. The
8492 ;; preceding type must be the identifier instead.
8493 (c-fdoc-shift-type-backward))
8495 ;; Prepare the "-> type;" for fontification later on.
8496 (when (and new-style-auto
8497 (looking-at c-haskell-op-re))
8498 (save-excursion
8499 (goto-char (match-end 0))
8500 (c-forward-syntactic-ws)
8501 (setq type-start (point))
8502 (setq at-type (c-forward-type))))
8504 ;; Move forward over any "WS" ids (like "final" or "override" in C++)
8505 (while (looking-at c-type-decl-suffix-ws-ids-key)
8506 (goto-char (match-end 1))
8507 (c-forward-syntactic-ws))
8509 (setq
8510 at-decl-or-cast
8511 (catch 'at-decl-or-cast
8513 ;; CASE 1
8514 (when (> paren-depth 0)
8515 ;; Encountered something inside parens that isn't matched by
8516 ;; the `c-type-decl-*' regexps, so it's not a type decl
8517 ;; expression. Try to skip out to the same paren depth to
8518 ;; not confuse the cast check below. If we don't manage this and
8519 ;; `at-decl-or-cast' is 'ids we might have an expression like
8520 ;; "foo bar ({ ..." which is a valid C++11 initialization.
8521 (if (and (not (c-safe (goto-char (scan-lists (point) 1 paren-depth))))
8522 (eq at-decl-or-cast 'ids))
8523 (c-fdoc-shift-type-backward))
8524 ;; If we've found a specifier keyword then it's a
8525 ;; declaration regardless.
8526 (throw 'at-decl-or-cast (memq at-decl-or-cast '(t ids))))
8528 (setq at-decl-end
8529 (looking-at (cond ((eq context '<>) "[,>]")
8530 ((not (memq context '(nil top))) "[,\)]")
8531 (t "[,;]"))))
8533 ;; Now we've collected info about various characteristics of
8534 ;; the construct we're looking at. Below follows a decision
8535 ;; tree based on that. It's ordered to check more certain
8536 ;; signs before less certain ones.
8538 (if got-identifier
8539 (progn
8541 ;; CASE 2
8542 (when (and (or at-type maybe-typeless)
8543 (not (or got-prefix got-parens)))
8544 ;; Got another identifier directly after the type, so it's a
8545 ;; declaration.
8546 (throw 'at-decl-or-cast t))
8548 (when (and got-parens
8549 (not got-prefix)
8550 ;; (not got-suffix-after-parens)
8551 (or backup-at-type
8552 maybe-typeless
8553 backup-maybe-typeless
8554 (eq at-decl-or-cast t)
8555 ;; Check whether we have "bar (gnu);" where we
8556 ;; are directly inside a class (etc.) called "bar".
8557 (save-excursion
8558 (and
8559 (progn
8560 (goto-char name-start)
8561 (not (memq (c-forward-type) '(nil maybe))))
8562 (progn
8563 (goto-char id-start)
8564 (c-directly-in-class-called-p
8565 (buffer-substring
8566 type-start
8567 (progn
8568 (goto-char type-start)
8569 (c-forward-type)
8570 (c-backward-syntactic-ws)
8571 (point)))))))))
8572 ;; Got a declaration of the form "foo bar (gnu);" or "bar
8573 ;; (gnu);" where we've recognized "bar" as the type and "gnu"
8574 ;; as the declarator, and in the latter case, checked that
8575 ;; "bar (gnu)" appears directly inside the class "bar". In
8576 ;; this case it's however more likely that "bar" is the
8577 ;; declarator and "gnu" a function argument or initializer
8578 ;; (if `c-recognize-paren-inits' is set), since the parens
8579 ;; around "gnu" would be superfluous if it's a declarator.
8580 ;; Shift the type one step backward.
8581 (c-fdoc-shift-type-backward)))
8583 ;; Found no identifier.
8585 (if backup-at-type
8586 (progn
8588 ;; CASE 3
8589 (when (= (point) start)
8590 ;; Got a plain list of identifiers. If a colon follows it's
8591 ;; a valid label, or maybe a bitfield. Otherwise the last
8592 ;; one probably is the declared identifier and we should
8593 ;; back up to the previous type, providing it isn't a cast.
8594 (if (and (eq (char-after) ?:)
8595 (not (c-major-mode-is 'java-mode)))
8596 (cond
8597 ;; If we've found a specifier keyword then it's a
8598 ;; declaration regardless.
8599 ((eq at-decl-or-cast t)
8600 (throw 'at-decl-or-cast t))
8601 ((and c-has-bitfields
8602 (eq at-decl-or-cast 'ids)) ; bitfield.
8603 (setq backup-if-not-cast t)
8604 (throw 'at-decl-or-cast t)))
8606 (setq backup-if-not-cast t)
8607 (throw 'at-decl-or-cast t)))
8609 ;; CASE 4
8610 (when (and got-suffix
8611 (not got-prefix)
8612 (not got-parens))
8613 ;; Got a plain list of identifiers followed by some suffix.
8614 ;; If this isn't a cast then the last identifier probably is
8615 ;; the declared one and we should back up to the previous
8616 ;; type.
8617 (setq backup-if-not-cast t)
8618 (throw 'at-decl-or-cast t)))
8620 ;; CASE 5
8621 (when (eq at-type t)
8622 ;; If the type is known we know that there can't be any
8623 ;; identifier somewhere else, and it's only in declarations in
8624 ;; e.g. function prototypes and in casts that the identifier may
8625 ;; be left out.
8626 (throw 'at-decl-or-cast t))
8628 (when (= (point) start)
8629 ;; Only got a single identifier (parsed as a type so far).
8630 ;; CASE 6
8631 (if (and
8632 ;; Check that the identifier isn't at the start of an
8633 ;; expression.
8634 at-decl-end
8635 (cond
8636 ((eq context 'decl)
8637 ;; Inside an arglist that contains declarations. If K&R
8638 ;; style declarations and parenthesis style initializers
8639 ;; aren't allowed then the single identifier must be a
8640 ;; type, else we require that it's known or found
8641 ;; (primitive types are handled above).
8642 (or (and (not c-recognize-knr-p)
8643 (not c-recognize-paren-inits))
8644 (memq at-type '(known found))))
8645 ((eq context '<>)
8646 ;; Inside a template arglist. Accept known and found
8647 ;; types; other identifiers could just as well be
8648 ;; constants in C++.
8649 (memq at-type '(known found)))))
8650 (throw 'at-decl-or-cast t)
8651 ;; CASE 7
8652 ;; Can't be a valid declaration or cast, but if we've found a
8653 ;; specifier it can't be anything else either, so treat it as
8654 ;; an invalid/unfinished declaration or cast.
8655 (throw 'at-decl-or-cast at-decl-or-cast))))
8657 (if (and got-parens
8658 (not got-prefix)
8659 (memq context '(nil top))
8660 (not (eq at-type t))
8661 (or backup-at-type
8662 maybe-typeless
8663 backup-maybe-typeless
8664 (when c-recognize-typeless-decls
8665 (or (not got-suffix)
8666 (not (looking-at
8667 c-after-suffixed-type-maybe-decl-key))))))
8668 ;; Got an empty paren pair and a preceding type that probably
8669 ;; really is the identifier. Shift the type backwards to make
8670 ;; the last one the identifier. This is analogous to the
8671 ;; "backtracking" done inside the `c-type-decl-suffix-key' loop
8672 ;; above.
8674 ;; Exception: In addition to the conditions in that
8675 ;; "backtracking" code, do not shift backward if we're not
8676 ;; looking at either `c-after-suffixed-type-decl-key' or "[;,]".
8677 ;; Since there's no preceding type, the shift would mean that
8678 ;; the declaration is typeless. But if the regexp doesn't match
8679 ;; then we will simply fall through in the tests below and not
8680 ;; recognize it at all, so it's better to try it as an abstract
8681 ;; declarator instead.
8682 (c-fdoc-shift-type-backward)
8684 ;; Still no identifier.
8685 ;; CASE 8
8686 (when (and got-prefix (or got-parens got-suffix))
8687 ;; Require `got-prefix' together with either `got-parens' or
8688 ;; `got-suffix' to recognize it as an abstract declarator:
8689 ;; `got-parens' only is probably an empty function call.
8690 ;; `got-suffix' only can build an ordinary expression together
8691 ;; with the preceding identifier which we've taken as a type.
8692 ;; We could actually accept on `got-prefix' only, but that can
8693 ;; easily occur temporarily while writing an expression so we
8694 ;; avoid that case anyway. We could do a better job if we knew
8695 ;; the point when the fontification was invoked.
8696 (throw 'at-decl-or-cast t))
8698 ;; CASE 9
8699 (when (and at-type
8700 (not got-prefix)
8701 (not got-parens)
8702 got-suffix-after-parens
8703 (eq (char-after got-suffix-after-parens) ?\())
8704 ;; Got a type, no declarator but a paren suffix. I.e. it's a
8705 ;; normal function call after all (or perhaps a C++ style object
8706 ;; instantiation expression).
8707 (throw 'at-decl-or-cast nil))))
8709 ;; CASE 9.5
8710 (when (and (not context) ; i.e. not at top level.
8711 (c-major-mode-is 'c++-mode)
8712 (eq at-decl-or-cast 'ids)
8713 after-paren-pos)
8714 ;; We've got something like "foo bar (...)" in C++ which isn't at
8715 ;; the top level. This is probably a uniform initialization of bar
8716 ;; to the contents of the parens. In this case the declarator ends
8717 ;; at the open paren.
8718 (goto-char (1- after-paren-pos))
8719 (throw 'at-decl-or-cast t))
8721 ;; CASE 10
8722 (when at-decl-or-cast
8723 ;; By now we've located the type in the declaration that we know
8724 ;; we're in.
8725 (throw 'at-decl-or-cast t))
8727 ;; CASE 11
8728 (when (and got-identifier
8729 (looking-at c-after-suffixed-type-decl-key)
8730 (or (eq context 'top)
8731 (and (eq context nil)
8732 (match-beginning 1)))
8733 (if (and got-parens
8734 (not got-prefix)
8735 (not got-suffix)
8736 (not (eq at-type t)))
8737 ;; Shift the type backward in the case that there's a
8738 ;; single identifier inside parens. That can only
8739 ;; occur in K&R style function declarations so it's
8740 ;; more likely that it really is a function call.
8741 ;; Therefore we only do this after
8742 ;; `c-after-suffixed-type-decl-key' has matched.
8743 (progn (c-fdoc-shift-type-backward) t)
8744 got-suffix-after-parens))
8745 ;; A declaration according to `c-after-suffixed-type-decl-key'.
8746 (throw 'at-decl-or-cast t))
8748 ;; CASE 12
8749 (when (and (or got-prefix (not got-parens))
8750 (memq at-type '(t known)))
8751 ;; It's a declaration if a known type precedes it and it can't be a
8752 ;; function call.
8753 (throw 'at-decl-or-cast t))
8755 ;; If we get here we can't tell if this is a type decl or a normal
8756 ;; expression by looking at it alone. (That's under the assumption
8757 ;; that normal expressions always can look like type decl expressions,
8758 ;; which isn't really true but the cases where it doesn't hold are so
8759 ;; uncommon (e.g. some placements of "const" in C++) it's not worth
8760 ;; the effort to look for them.)
8762 ;;; 2008-04-16: commented out the next form, to allow the function to recognize
8763 ;;; "foo (int bar)" in CC (an implicit type (in class foo) without a semicolon)
8764 ;;; as a(n almost complete) declaration, enabling it to be fontified.
8765 ;; CASE 13
8766 ;; (unless (or at-decl-end (looking-at "=[^=]"))
8767 ;; If this is a declaration it should end here or its initializer(*)
8768 ;; should start here, so check for allowed separation tokens. Note
8769 ;; that this rule doesn't work e.g. with a K&R arglist after a
8770 ;; function header.
8772 ;; *) Don't check for C++ style initializers using parens
8773 ;; since those already have been matched as suffixes.
8775 ;; If `at-decl-or-cast' is then we've found some other sign that
8776 ;; it's a declaration or cast, so then it's probably an
8777 ;; invalid/unfinished one.
8778 ;; (throw 'at-decl-or-cast at-decl-or-cast))
8780 ;; Below are tests that only should be applied when we're certain to
8781 ;; not have parsed halfway through an expression.
8783 ;; CASE 14
8784 (when (memq at-type '(t known))
8785 ;; The expression starts with a known type so treat it as a
8786 ;; declaration.
8787 (throw 'at-decl-or-cast t))
8789 ;; CASE 15
8790 (when (and (c-major-mode-is 'c++-mode)
8791 ;; In C++ we check if the identifier is a known type, since
8792 ;; (con|de)structors use the class name as identifier.
8793 ;; We've always shifted over the identifier as a type and
8794 ;; then backed up again in this case.
8795 identifier-type
8796 (or (memq identifier-type '(found known))
8797 (and (eq (char-after identifier-start) ?~)
8798 ;; `at-type' probably won't be 'found for
8799 ;; destructors since the "~" is then part of the
8800 ;; type name being checked against the list of
8801 ;; known types, so do a check without that
8802 ;; operator.
8803 (or (save-excursion
8804 (goto-char (1+ identifier-start))
8805 (c-forward-syntactic-ws)
8806 (c-with-syntax-table
8807 c-identifier-syntax-table
8808 (looking-at c-known-type-key)))
8809 (save-excursion
8810 (goto-char (1+ identifier-start))
8811 ;; We have already parsed the type earlier,
8812 ;; so it'd be possible to cache the end
8813 ;; position instead of redoing it here, but
8814 ;; then we'd need to keep track of another
8815 ;; position everywhere.
8816 (c-check-type (point)
8817 (progn (c-forward-type)
8818 (point))))))))
8819 (throw 'at-decl-or-cast t))
8821 (if got-identifier
8822 (progn
8823 ;; CASE 16
8824 (when (and got-prefix-before-parens
8825 at-type
8826 (or at-decl-end (looking-at "=[^=]"))
8827 (memq context '(nil top))
8828 (or (not got-suffix)
8829 at-decl-start))
8830 ;; Got something like "foo * bar;". Since we're not inside
8831 ;; an arglist it would be a meaningless expression because
8832 ;; the result isn't used. We therefore choose to recognize
8833 ;; it as a declaration. We only allow a suffix (which makes
8834 ;; the construct look like a function call) when
8835 ;; `at-decl-start' provides additional evidence that we do
8836 ;; have a declaration.
8837 (setq maybe-expression t)
8838 (throw 'at-decl-or-cast t))
8840 ;; CASE 17
8841 (when (and (or got-suffix-after-parens
8842 (looking-at "=[^=]"))
8843 (eq at-type 'found)
8844 (not (eq context 'arglist)))
8845 ;; Got something like "a (*b) (c);" or "a (b) = c;". It could
8846 ;; be an odd expression or it could be a declaration. Treat
8847 ;; it as a declaration if "a" has been used as a type
8848 ;; somewhere else (if it's a known type we won't get here).
8849 (setq maybe-expression t)
8850 (throw 'at-decl-or-cast t)))
8852 ;; CASE 18
8853 (when (and (not (memq context '(nil top)))
8854 (or got-prefix
8855 (and (eq context 'decl)
8856 (not c-recognize-paren-inits)
8857 (or got-parens got-suffix))))
8858 ;; Got a type followed by an abstract declarator. If `got-prefix'
8859 ;; is set it's something like "a *" without anything after it. If
8860 ;; `got-parens' or `got-suffix' is set it's "a()", "a[]", "a()[]",
8861 ;; or similar, which we accept only if the context rules out
8862 ;; expressions.
8863 (throw 'at-decl-or-cast t)))
8865 ;; If we had a complete symbol table here (which rules out
8866 ;; `c-found-types') we should return t due to the disambiguation rule
8867 ;; (in at least C++) that anything that can be parsed as a declaration
8868 ;; is a declaration. Now we're being more defensive and prefer to
8869 ;; highlight things like "foo (bar);" as a declaration only if we're
8870 ;; inside an arglist that contains declarations.
8871 ;; CASE 19
8872 (eq context 'decl))))
8874 ;; The point is now after the type decl expression.
8876 (cond
8877 ;; Check for a cast.
8878 ((save-excursion
8879 (and
8880 c-cast-parens
8882 ;; Should be the first type/identifier in a cast paren.
8883 (> preceding-token-end (point-min))
8884 (memq (char-before preceding-token-end) c-cast-parens)
8886 ;; The closing paren should follow.
8887 (progn
8888 (c-forward-syntactic-ws)
8889 (looking-at "\\s)"))
8891 ;; There should be a primary expression after it.
8892 (let (pos)
8893 (forward-char)
8894 (c-forward-syntactic-ws)
8895 (setq cast-end (point))
8896 (and (looking-at c-primary-expr-regexp)
8897 (progn
8898 (setq pos (match-end 0))
8900 ;; Check if the expression begins with a prefix keyword.
8901 (match-beginning 2)
8902 (if (match-beginning 1)
8903 ;; Expression begins with an ambiguous operator. Treat
8904 ;; it as a cast if it's a type decl or if we've
8905 ;; recognized the type somewhere else.
8906 (or at-decl-or-cast
8907 (memq at-type '(t known found)))
8908 ;; Unless it's a keyword, it's the beginning of a primary
8909 ;; expression.
8910 (not (looking-at c-keywords-regexp)))))
8911 ;; If `c-primary-expr-regexp' matched a nonsymbol token, check
8912 ;; that it matched a whole one so that we don't e.g. confuse
8913 ;; the operator '-' with '->'. It's ok if it matches further,
8914 ;; though, since it e.g. can match the float '.5' while the
8915 ;; operator regexp only matches '.'.
8916 (or (not (looking-at c-nonsymbol-token-regexp))
8917 (<= (match-end 0) pos))))
8919 ;; There should either be a cast before it or something that isn't an
8920 ;; identifier or close paren.
8921 (> preceding-token-end (point-min))
8922 (progn
8923 (goto-char (1- preceding-token-end))
8924 (or (eq (point) last-cast-end)
8925 (progn
8926 (c-backward-syntactic-ws)
8927 (if (< (skip-syntax-backward "w_") 0)
8928 ;; It's a symbol. Accept it only if it's one of the
8929 ;; keywords that can precede an expression (without
8930 ;; surrounding parens).
8931 (looking-at c-simple-stmt-key)
8932 (and
8933 ;; Check that it isn't a close paren (block close is ok,
8934 ;; though).
8935 (not (memq (char-before) '(?\) ?\])))
8936 ;; Check that it isn't a nonsymbol identifier.
8937 (not (c-on-identifier)))))))))
8939 ;; Handle the cast.
8940 (when (and c-record-type-identifiers at-type (not (eq at-type t)))
8941 (let ((c-promote-possible-types t))
8942 (goto-char type-start)
8943 (c-forward-type)))
8945 (goto-char cast-end)
8946 'cast)
8948 (at-decl-or-cast
8949 ;; We're at a declaration. Highlight the type and the following
8950 ;; declarators.
8952 (when backup-if-not-cast
8953 (c-fdoc-shift-type-backward t))
8955 (when (and (eq context 'decl) (looking-at ","))
8956 ;; Make sure to propagate the `c-decl-arg-start' property to
8957 ;; the next argument if it's set in this one, to cope with
8958 ;; interactive refontification.
8959 (c-put-c-type-property (point) 'c-decl-arg-start))
8961 ;; Record the type's coordinates in `c-record-type-identifiers' for
8962 ;; later fontification.
8963 (when (and c-record-type-identifiers at-type ;; (not (eq at-type t))
8964 ;; There seems no reason to exclude a token from
8965 ;; fontification just because it's "a known type that can't
8966 ;; be a name or other expression". 2013-09-18.
8968 (let ((c-promote-possible-types t))
8969 (save-excursion
8970 (goto-char type-start)
8971 (c-forward-type))))
8973 (list id-start
8974 (and (or at-type-decl at-typedef)
8975 (cons at-type-decl at-typedef))
8976 maybe-expression
8977 type-start))
8980 ;; False alarm. Restore the recorded ranges.
8981 (setq c-record-type-identifiers save-rec-type-ids
8982 c-record-ref-identifiers save-rec-ref-ids)
8983 nil))))
8985 (defun c-forward-label (&optional assume-markup preceding-token-end limit)
8986 ;; Assuming that point is at the beginning of a token, check if it starts a
8987 ;; label and if so move over it and return non-nil (t in default situations,
8988 ;; specific symbols (see below) for interesting situations), otherwise don't
8989 ;; move and return nil. "Label" here means "most things with a colon".
8991 ;; More precisely, a "label" is regarded as one of:
8992 ;; (i) a goto target like "foo:" - returns the symbol `goto-target';
8993 ;; (ii) A case label - either the entire construct "case FOO:", or just the
8994 ;; bare "case", should the colon be missing. We return t;
8995 ;; (iii) a keyword which needs a colon, like "default:" or "private:"; We
8996 ;; return t;
8997 ;; (iv) One of QT's "extended" C++ variants of
8998 ;; "private:"/"protected:"/"public:"/"more:" looking like "public slots:".
8999 ;; Returns the symbol `qt-2kwds-colon'.
9000 ;; (v) QT's construct "signals:". Returns the symbol `qt-1kwd-colon'.
9001 ;; (vi) One of the keywords matched by `c-opt-extra-label-key' (without any
9002 ;; colon). Currently (2006-03), this applies only to Objective C's
9003 ;; keywords "@private", "@protected", and "@public". Returns t.
9005 ;; One of the things which will NOT be recognized as a label is a bit-field
9006 ;; element of a struct, something like "int foo:5".
9008 ;; The end of the label is taken to be just after the colon, or the end of
9009 ;; the first submatch in `c-opt-extra-label-key'. The point is directly
9010 ;; after the end on return. The terminating char gets marked with
9011 ;; `c-decl-end' to improve recognition of the following declaration or
9012 ;; statement.
9014 ;; If ASSUME-MARKUP is non-nil, it's assumed that the preceding
9015 ;; label, if any, has already been marked up like that.
9017 ;; If PRECEDING-TOKEN-END is given, it should be the first position
9018 ;; after the preceding token, i.e. on the other side of the
9019 ;; syntactic ws from the point. Use a value less than or equal to
9020 ;; (point-min) if the point is at the first token in (the visible
9021 ;; part of) the buffer.
9023 ;; The optional LIMIT limits the forward scan for the colon.
9025 ;; This function records the ranges of the label symbols on
9026 ;; `c-record-ref-identifiers' if `c-record-type-identifiers' (!) is
9027 ;; non-nil.
9029 ;; This function might do hidden buffer changes.
9031 (let ((start (point))
9032 label-end
9033 qt-symbol-idx
9034 macro-start ; if we're in one.
9035 label-type
9036 kwd)
9037 (cond
9038 ;; "case" or "default" (Doesn't apply to AWK).
9039 ((looking-at c-label-kwds-regexp)
9040 (let ((kwd-end (match-end 1)))
9041 ;; Record only the keyword itself for fontification, since in
9042 ;; case labels the following is a constant expression and not
9043 ;; a label.
9044 (when c-record-type-identifiers
9045 (c-record-ref-id (cons (match-beginning 1) kwd-end)))
9047 ;; Find the label end.
9048 (goto-char kwd-end)
9049 (setq label-type
9050 (if (and (c-syntactic-re-search-forward
9051 ;; Stop on chars that aren't allowed in expressions,
9052 ;; and on operator chars that would be meaningless
9053 ;; there. FIXME: This doesn't cope with ?: operators.
9054 "[;{=,@]\\|\\(\\=\\|[^:]\\):\\([^:]\\|\\'\\)"
9055 limit t t nil 1)
9056 (match-beginning 2))
9058 (progn ; there's a proper :
9059 (goto-char (match-beginning 2)) ; just after the :
9060 (c-put-c-type-property (1- (point)) 'c-decl-end)
9063 ;; It's an unfinished label. We consider the keyword enough
9064 ;; to recognize it as a label, so that it gets fontified.
9065 ;; Leave the point at the end of it, but don't put any
9066 ;; `c-decl-end' marker.
9067 (goto-char kwd-end)
9068 t))))
9070 ;; @private, @protected, @public, in Objective C, or similar.
9071 ((and c-opt-extra-label-key
9072 (looking-at c-opt-extra-label-key))
9073 ;; For a `c-opt-extra-label-key' match, we record the whole
9074 ;; thing for fontification. That's to get the leading '@' in
9075 ;; Objective-C protection labels fontified.
9076 (goto-char (match-end 1))
9077 (when c-record-type-identifiers
9078 (c-record-ref-id (cons (match-beginning 1) (point))))
9079 (c-put-c-type-property (1- (point)) 'c-decl-end)
9080 (setq label-type t))
9082 ;; All other cases of labels.
9083 ((and c-recognize-colon-labels ; nil for AWK and IDL, otherwise t.
9085 ;; A colon label must have something before the colon.
9086 (not (eq (char-after) ?:))
9088 ;; Check that we're not after a token that can't precede a label.
9090 ;; Trivially succeeds when there's no preceding token.
9091 ;; Succeeds when we're at a virtual semicolon.
9092 (if preceding-token-end
9093 (<= preceding-token-end (point-min))
9094 (save-excursion
9095 (c-backward-syntactic-ws)
9096 (setq preceding-token-end (point))
9097 (or (bobp)
9098 (c-at-vsemi-p))))
9100 ;; Check if we're after a label, if we're after a closing
9101 ;; paren that belong to statement, and with
9102 ;; `c-label-prefix-re'. It's done in different order
9103 ;; depending on `assume-markup' since the checks have
9104 ;; different expensiveness.
9105 (if assume-markup
9107 (eq (c-get-char-property (1- preceding-token-end) 'c-type)
9108 'c-decl-end)
9110 (save-excursion
9111 (goto-char (1- preceding-token-end))
9112 (c-beginning-of-current-token)
9113 (or (looking-at c-label-prefix-re)
9114 (looking-at c-block-stmt-1-key)))
9116 (and (eq (char-before preceding-token-end) ?\))
9117 (c-after-conditional)))
9120 (save-excursion
9121 (goto-char (1- preceding-token-end))
9122 (c-beginning-of-current-token)
9123 (or (looking-at c-label-prefix-re)
9124 (looking-at c-block-stmt-1-key)))
9126 (cond
9127 ((eq (char-before preceding-token-end) ?\))
9128 (c-after-conditional))
9130 ((eq (char-before preceding-token-end) ?:)
9131 ;; Might be after another label, so check it recursively.
9132 (save-restriction
9133 (save-excursion
9134 (goto-char (1- preceding-token-end))
9135 ;; Essentially the same as the
9136 ;; `c-syntactic-re-search-forward' regexp below.
9137 (setq macro-start
9138 (save-excursion (and (c-beginning-of-macro)
9139 (point))))
9140 (if macro-start (narrow-to-region macro-start (point-max)))
9141 (c-syntactic-skip-backward "^-]:?;}=*/%&|,<>!@+" nil t)
9142 ;; Note: the following should work instead of the
9143 ;; narrow-to-region above. Investigate why not,
9144 ;; sometime. ACM, 2006-03-31.
9145 ;; (c-syntactic-skip-backward "^-]:?;}=*/%&|,<>!@+"
9146 ;; macro-start t)
9147 (let ((pte (point))
9148 ;; If the caller turned on recording for us,
9149 ;; it shouldn't apply when we check the
9150 ;; preceding label.
9151 c-record-type-identifiers)
9152 ;; A label can't start at a cpp directive. Check for
9153 ;; this, since c-forward-syntactic-ws would foul up on it.
9154 (unless (and c-opt-cpp-prefix (looking-at c-opt-cpp-prefix))
9155 (c-forward-syntactic-ws)
9156 (c-forward-label nil pte start))))))))))
9158 ;; Point is still at the beginning of the possible label construct.
9160 ;; Check that the next nonsymbol token is ":", or that we're in one
9161 ;; of QT's "slots" declarations. Allow '(' for the sake of macro
9162 ;; arguments. FIXME: Should build this regexp from the language
9163 ;; constants.
9164 (cond
9165 ;; public: protected: private:
9166 ((and
9167 (c-major-mode-is 'c++-mode)
9168 (search-forward-regexp
9169 "\\=p\\(r\\(ivate\\|otected\\)\\|ublic\\)\\>[^_]" nil t)
9170 (progn (backward-char)
9171 (c-forward-syntactic-ws limit)
9172 (looking-at ":\\([^:]\\|\\'\\)"))) ; A single colon.
9173 (forward-char)
9174 (setq label-type t))
9175 ;; QT double keyword like "protected slots:" or goto target.
9176 ((progn (goto-char start) nil))
9177 ((when (c-syntactic-re-search-forward
9178 "[ \t\n[:?;{=*/%&|,<>!@+-]" limit t t) ; not at EOB
9179 (backward-char)
9180 (setq label-end (point))
9181 (setq qt-symbol-idx
9182 (and (c-major-mode-is 'c++-mode)
9183 (string-match
9184 "\\(p\\(r\\(ivate\\|otected\\)\\|ublic\\)\\|more\\)\\>"
9185 (buffer-substring start (point)))))
9186 (c-forward-syntactic-ws limit)
9187 (cond
9188 ((looking-at ":\\([^:]\\|\\'\\)") ; A single colon.
9189 (forward-char)
9190 (setq label-type
9191 (if (or (string= "signals" ; Special QT macro
9192 (setq kwd (buffer-substring-no-properties start label-end)))
9193 (string= "Q_SIGNALS" kwd))
9194 'qt-1kwd-colon
9195 'goto-target)))
9196 ((and qt-symbol-idx
9197 (search-forward-regexp "\\=\\(slots\\|Q_SLOTS\\)\\>" limit t)
9198 (progn (c-forward-syntactic-ws limit)
9199 (looking-at ":\\([^:]\\|\\'\\)"))) ; A single colon
9200 (forward-char)
9201 (setq label-type 'qt-2kwds-colon)))))))
9203 (save-restriction
9204 (narrow-to-region start (point))
9206 ;; Check that `c-nonlabel-token-key' doesn't match anywhere.
9207 (catch 'check-label
9208 (goto-char start)
9209 (while (progn
9210 (when (looking-at c-nonlabel-token-key)
9211 (goto-char start)
9212 (setq label-type nil)
9213 (throw 'check-label nil))
9214 (and (c-safe (c-forward-sexp)
9215 (c-forward-syntactic-ws)
9217 (not (eobp)))))
9219 ;; Record the identifiers in the label for fontification, unless
9220 ;; it begins with `c-label-kwds' in which case the following
9221 ;; identifiers are part of a (constant) expression that
9222 ;; shouldn't be fontified.
9223 (when (and c-record-type-identifiers
9224 (progn (goto-char start)
9225 (not (looking-at c-label-kwds-regexp))))
9226 (while (c-syntactic-re-search-forward c-symbol-key nil t)
9227 (c-record-ref-id (cons (match-beginning 0)
9228 (match-end 0)))))
9230 (c-put-c-type-property (1- (point-max)) 'c-decl-end)
9231 (goto-char (point-max)))))
9234 ;; Not a label.
9235 (goto-char start)))
9236 label-type))
9238 (defun c-forward-objc-directive ()
9239 ;; Assuming the point is at the beginning of a token, try to move
9240 ;; forward to the end of the Objective-C directive that starts
9241 ;; there. Return t if a directive was fully recognized, otherwise
9242 ;; the point is moved as far as one could be successfully parsed and
9243 ;; nil is returned.
9245 ;; This function records identifier ranges on
9246 ;; `c-record-type-identifiers' and `c-record-ref-identifiers' if
9247 ;; `c-record-type-identifiers' is non-nil.
9249 ;; This function might do hidden buffer changes.
9251 (let ((start (point))
9252 start-char
9253 (c-promote-possible-types t)
9255 ;; Turn off recognition of angle bracket arglists while parsing
9256 ;; types here since the protocol reference list might then be
9257 ;; considered part of the preceding name or superclass-name.
9258 c-recognize-<>-arglists)
9260 (if (or
9261 (when (looking-at
9262 (eval-when-compile
9263 (c-make-keywords-re t
9264 (append (c-lang-const c-protection-kwds objc)
9265 '("@end"))
9266 'objc-mode)))
9267 (goto-char (match-end 1))
9270 (and
9271 (looking-at
9272 (eval-when-compile
9273 (c-make-keywords-re t
9274 '("@interface" "@implementation" "@protocol")
9275 'objc-mode)))
9277 ;; Handle the name of the class itself.
9278 (progn
9279 ;; (c-forward-token-2) ; 2006/1/13 This doesn't move if the token's
9280 ;; at EOB.
9281 (goto-char (match-end 0))
9282 (setq lim (point))
9283 (c-skip-ws-forward)
9284 (c-forward-type))
9286 (catch 'break
9287 ;; Look for ": superclass-name" or "( category-name )".
9288 (when (looking-at "[:(]")
9289 (setq start-char (char-after))
9290 (forward-char)
9291 (c-forward-syntactic-ws)
9292 (unless (c-forward-type) (throw 'break nil))
9293 (when (eq start-char ?\()
9294 (unless (eq (char-after) ?\)) (throw 'break nil))
9295 (forward-char)
9296 (c-forward-syntactic-ws)))
9298 ;; Look for a protocol reference list.
9299 (if (eq (char-after) ?<)
9300 (let ((c-recognize-<>-arglists t)
9301 (c-parse-and-markup-<>-arglists t)
9302 c-restricted-<>-arglists)
9303 (c-forward-<>-arglist t))
9304 t))))
9306 (progn
9307 (c-backward-syntactic-ws lim)
9308 (c-clear-c-type-property start (1- (point)) 'c-decl-end)
9309 (c-put-c-type-property (1- (point)) 'c-decl-end)
9312 (c-clear-c-type-property start (point) 'c-decl-end)
9313 nil)))
9315 (defun c-beginning-of-inheritance-list (&optional lim)
9316 ;; Go to the first non-whitespace after the colon that starts a
9317 ;; multiple inheritance introduction. Optional LIM is the farthest
9318 ;; back we should search.
9320 ;; This function might do hidden buffer changes.
9321 (c-with-syntax-table c++-template-syntax-table
9322 (c-backward-token-2 0 t lim)
9323 (while (and (or (looking-at c-symbol-start)
9324 (looking-at "[<,]\\|::"))
9325 (zerop (c-backward-token-2 1 t lim))))))
9327 (defun c-in-method-def-p ()
9328 ;; Return nil if we aren't in a method definition, otherwise the
9329 ;; position of the initial [+-].
9331 ;; This function might do hidden buffer changes.
9332 (save-excursion
9333 (beginning-of-line)
9334 (and c-opt-method-key
9335 (looking-at c-opt-method-key)
9336 (point))
9339 ;; Contributed by Kevin Ryde <user42@zip.com.au>.
9340 (defun c-in-gcc-asm-p ()
9341 ;; Return non-nil if point is within a gcc \"asm\" block.
9343 ;; This should be called with point inside an argument list.
9345 ;; Only one level of enclosing parentheses is considered, so for
9346 ;; instance nil is returned when in a function call within an asm
9347 ;; operand.
9349 ;; This function might do hidden buffer changes.
9351 (and c-opt-asm-stmt-key
9352 (save-excursion
9353 (beginning-of-line)
9354 (backward-up-list 1)
9355 (c-beginning-of-statement-1 (point-min) nil t)
9356 (looking-at c-opt-asm-stmt-key))))
9358 (defun c-at-toplevel-p ()
9359 "Return a determination as to whether point is \"at the top level\".
9360 Informally, \"at the top level\" is anywhere where you can write
9361 a function.
9363 More precisely, being at the top-level means that point is either
9364 outside any enclosing block (such as a function definition), or
9365 directly inside a class, namespace or other block that contains
9366 another declaration level.
9368 If point is not at the top-level (e.g. it is inside a method
9369 definition), then nil is returned. Otherwise, if point is at a
9370 top-level not enclosed within a class definition, t is returned.
9371 Otherwise, a 2-vector is returned where the zeroth element is the
9372 buffer position of the start of the class declaration, and the first
9373 element is the buffer position of the enclosing class's opening
9374 brace.
9376 Note that this function might do hidden buffer changes. See the
9377 comment at the start of cc-engine.el for more info."
9378 ;; Note to maintainers: this function consumes a great mass of CPU cycles.
9379 ;; Its use should thus be minimized as far as possible.
9380 (let ((paren-state (c-parse-state)))
9381 (or (not (c-most-enclosing-brace paren-state))
9382 (c-search-uplist-for-classkey paren-state))))
9384 (defun c-just-after-func-arglist-p (&optional lim)
9385 ;; Return non-nil if the point is in the region after the argument
9386 ;; list of a function and its opening brace (or semicolon in case it
9387 ;; got no body). If there are K&R style argument declarations in
9388 ;; that region, the point has to be inside the first one for this
9389 ;; function to recognize it.
9391 ;; If successful, the point is moved to the first token after the
9392 ;; function header (see `c-forward-decl-or-cast-1' for details) and
9393 ;; the position of the opening paren of the function arglist is
9394 ;; returned.
9396 ;; The point is clobbered if not successful.
9398 ;; LIM is used as bound for backward buffer searches.
9400 ;; This function might do hidden buffer changes.
9402 (let ((beg (point)) id-start)
9403 (and
9404 (eq (c-beginning-of-statement-1 lim) 'same)
9406 (not (and (c-major-mode-is 'objc-mode)
9407 (c-forward-objc-directive)))
9409 (setq id-start
9410 (car-safe (c-forward-decl-or-cast-1 (c-point 'bosws) 'top nil)))
9411 (< id-start beg)
9413 ;; There should not be a '=' or ',' between beg and the
9414 ;; start of the declaration since that means we were in the
9415 ;; "expression part" of the declaration.
9416 (or (> (point) beg)
9417 (not (looking-at "[=,]")))
9419 (save-excursion
9420 ;; Check that there's an arglist paren in the
9421 ;; declaration.
9422 (goto-char id-start)
9423 (cond ((eq (char-after) ?\()
9424 ;; The declarator is a paren expression, so skip past it
9425 ;; so that we don't get stuck on that instead of the
9426 ;; function arglist.
9427 (c-forward-sexp))
9428 ((and c-opt-op-identifier-prefix
9429 (looking-at c-opt-op-identifier-prefix))
9430 ;; Don't trip up on "operator ()".
9431 (c-forward-token-2 2 t)))
9432 (and (< (point) beg)
9433 (c-syntactic-re-search-forward "(" beg t t)
9434 (1- (point)))))))
9436 (defun c-in-knr-argdecl (&optional lim)
9437 ;; Return the position of the first argument declaration if point is
9438 ;; inside a K&R style argument declaration list, nil otherwise.
9439 ;; `c-recognize-knr-p' is not checked. If LIM is non-nil, it's a
9440 ;; position that bounds the backward search for the argument list. This
9441 ;; function doesn't move point.
9443 ;; Point must be within a possible K&R region, e.g. just before a top-level
9444 ;; "{". It must be outside of parens and brackets. The test can return
9445 ;; false positives otherwise.
9447 ;; This function might do hidden buffer changes.
9448 (save-excursion
9449 (save-restriction
9450 ;; If we're in a macro, our search range is restricted to it. Narrow to
9451 ;; the searchable range.
9452 (let* ((macro-start (save-excursion (and (c-beginning-of-macro) (point))))
9453 (macro-end (save-excursion (and macro-start (c-end-of-macro) (point))))
9454 (low-lim (max (or lim (point-min)) (or macro-start (point-min))))
9455 before-lparen after-rparen
9456 (here (point))
9457 (pp-count-out 20) ; Max number of paren/brace constructs before
9458 ; we give up.
9459 ids ; List of identifiers in the parenthesized list.
9460 id-start after-prec-token decl-or-cast decl-res
9461 c-last-identifier-range identifier-ok)
9462 (narrow-to-region low-lim (or macro-end (point-max)))
9464 ;; Search backwards for the defun's argument list. We give up if we
9465 ;; encounter a "}" (end of a previous defun) an "=" (which can't be in
9466 ;; a knr region) or BOB.
9468 ;; The criterion for a paren structure being the arg list is:
9469 ;; o - there is non-WS stuff after it but before any "{"; AND
9470 ;; o - the token after it isn't a ";" AND
9471 ;; o - it is preceded by either an identifier (the function name) or
9472 ;; a macro expansion like "DEFUN (...)"; AND
9473 ;; o - its content is a non-empty comma-separated list of identifiers
9474 ;; (an empty arg list won't have a knr region).
9476 ;; The following snippet illustrates these rules:
9477 ;; int foo (bar, baz, yuk)
9478 ;; int bar [] ;
9479 ;; int (*baz) (my_type) ;
9480 ;; int (*(* yuk) (void)) (void) ;
9481 ;; {
9483 ;; Additionally, for a knr list to be recognized:
9484 ;; o - The identifier of each declarator up to and including the
9485 ;; one "near" point must be contained in the arg list.
9487 (catch 'knr
9488 (while (> pp-count-out 0) ; go back one paren/bracket pair each time.
9489 (setq pp-count-out (1- pp-count-out))
9490 (c-syntactic-skip-backward "^)]}=")
9491 (cond ((eq (char-before) ?\))
9492 (setq after-rparen (point)))
9493 ((eq (char-before) ?\])
9494 (setq after-rparen nil))
9495 (t ; either } (hit previous defun) or = or no more
9496 ; parens/brackets.
9497 (throw 'knr nil)))
9499 (if after-rparen
9500 ;; We're inside a paren. Could it be our argument list....?
9502 (and
9503 (progn
9504 (goto-char after-rparen)
9505 (unless (c-go-list-backward) (throw 'knr nil)) ;
9506 ;; FIXME!!! What about macros between the parens? 2007/01/20
9507 (setq before-lparen (point)))
9509 ;; It can't be the arg list if next token is ; or {
9510 (progn (goto-char after-rparen)
9511 (c-forward-syntactic-ws)
9512 (not (memq (char-after) '(?\; ?\{ ?\=))))
9514 ;; Is the thing preceding the list an identifier (the
9515 ;; function name), or a macro expansion?
9516 (progn
9517 (goto-char before-lparen)
9518 (eq (c-backward-token-2) 0)
9519 (or (eq (c-on-identifier) (point))
9520 (and (eq (char-after) ?\))
9521 (c-go-up-list-backward)
9522 (eq (c-backward-token-2) 0)
9523 (eq (c-on-identifier) (point)))))
9525 ;; Have we got a non-empty list of comma-separated
9526 ;; identifiers?
9527 (progn
9528 (goto-char before-lparen)
9529 (c-forward-token-2) ; to first token inside parens
9530 (and
9531 (setq id-start (c-on-identifier)) ; Must be at least one.
9532 (catch 'id-list
9533 (while
9534 (progn
9535 (forward-char)
9536 (c-end-of-current-token)
9537 (push (buffer-substring-no-properties id-start
9538 (point))
9539 ids)
9540 (c-forward-syntactic-ws)
9541 (eq (char-after) ?\,))
9542 (c-forward-token-2)
9543 (unless (setq id-start (c-on-identifier))
9544 (throw 'id-list nil)))
9545 (eq (char-after) ?\)))))
9547 ;; Are all the identifiers in the k&r list up to the
9548 ;; current one also in the argument list?
9549 (progn
9550 (forward-char) ; over the )
9551 (setq after-prec-token after-rparen)
9552 (c-forward-syntactic-ws)
9553 (while (and
9554 (or (consp (setq decl-or-cast
9555 (c-forward-decl-or-cast-1
9556 after-prec-token
9557 nil ; Or 'arglist ???
9558 nil)))
9559 (progn
9560 (goto-char after-prec-token)
9561 (c-forward-syntactic-ws)
9562 (setq identifier-ok (eq (char-after) ?{))
9563 nil))
9564 (eq (char-after) ?\;)
9565 (setq after-prec-token (1+ (point)))
9566 (goto-char (car decl-or-cast))
9567 (setq decl-res (c-forward-declarator))
9568 (setq identifier-ok
9569 (member (buffer-substring-no-properties
9570 (car decl-res) (cadr decl-res))
9571 ids))
9572 (progn
9573 (goto-char after-prec-token)
9574 (prog1 (< (point) here)
9575 (c-forward-syntactic-ws))))
9576 (setq identifier-ok nil))
9577 identifier-ok))
9578 ;; ...Yes. We've identified the function's argument list.
9579 (throw 'knr
9580 (progn (goto-char after-rparen)
9581 (c-forward-syntactic-ws)
9582 (point)))
9583 ;; ...No. The current parens aren't the function's arg list.
9584 (goto-char before-lparen))
9586 (or (c-go-list-backward) ; backwards over [ .... ]
9587 (throw 'knr nil)))))))))
9589 (defun c-skip-conditional ()
9590 ;; skip forward over conditional at point, including any predicate
9591 ;; statements in parentheses. No error checking is performed.
9593 ;; This function might do hidden buffer changes.
9594 (c-forward-sexp (cond
9595 ;; else if()
9596 ((looking-at (concat "\\<else"
9597 "\\([ \t\n]\\|\\\\\n\\)+"
9598 "if\\>\\([^_]\\|$\\)"))
9600 ;; do, else, try, finally
9601 ((looking-at (concat "\\<\\("
9602 "do\\|else\\|try\\|finally"
9603 "\\)\\>\\([^_]\\|$\\)"))
9605 ;; for, if, while, switch, catch, synchronized, foreach
9606 (t 2))))
9608 (defun c-after-conditional (&optional lim)
9609 ;; If looking at the token after a conditional then return the
9610 ;; position of its start, otherwise return nil.
9612 ;; This function might do hidden buffer changes.
9613 (save-excursion
9614 (and (zerop (c-backward-token-2 1 t lim))
9615 (or (looking-at c-block-stmt-1-key)
9616 (and (eq (char-after) ?\()
9617 (zerop (c-backward-token-2 1 t lim))
9618 (or (looking-at c-block-stmt-2-key)
9619 (looking-at c-block-stmt-1-2-key))))
9620 (point))))
9622 (defun c-after-special-operator-id (&optional lim)
9623 ;; If the point is after an operator identifier that isn't handled
9624 ;; like an ordinary symbol (i.e. like "operator =" in C++) then the
9625 ;; position of the start of that identifier is returned. nil is
9626 ;; returned otherwise. The point may be anywhere in the syntactic
9627 ;; whitespace after the last token of the operator identifier.
9629 ;; This function might do hidden buffer changes.
9630 (save-excursion
9631 (and c-overloadable-operators-regexp
9632 (zerop (c-backward-token-2 1 nil lim))
9633 (looking-at c-overloadable-operators-regexp)
9634 (or (not c-opt-op-identifier-prefix)
9635 (and
9636 (zerop (c-backward-token-2 1 nil lim))
9637 (looking-at c-opt-op-identifier-prefix)))
9638 (point))))
9640 (defsubst c-backward-to-block-anchor (&optional lim)
9641 ;; Assuming point is at a brace that opens a statement block of some
9642 ;; kind, move to the proper anchor point for that block. It might
9643 ;; need to be adjusted further by c-add-stmt-syntax, but the
9644 ;; position at return is suitable as start position for that
9645 ;; function.
9647 ;; This function might do hidden buffer changes.
9648 (unless (= (point) (c-point 'boi))
9649 (let ((start (c-after-conditional lim)))
9650 (if start
9651 (goto-char start)))))
9653 (defsubst c-backward-to-decl-anchor (&optional lim)
9654 ;; Assuming point is at a brace that opens the block of a top level
9655 ;; declaration of some kind, move to the proper anchor point for
9656 ;; that block.
9658 ;; This function might do hidden buffer changes.
9659 (unless (= (point) (c-point 'boi))
9660 (c-beginning-of-statement-1 lim)))
9662 (defun c-search-decl-header-end ()
9663 ;; Search forward for the end of the "header" of the current
9664 ;; declaration. That's the position where the definition body
9665 ;; starts, or the first variable initializer, or the ending
9666 ;; semicolon. I.e. search forward for the closest following
9667 ;; (syntactically relevant) '{', '=' or ';' token. Point is left
9668 ;; _after_ the first found token, or at point-max if none is found.
9670 ;; This function might do hidden buffer changes.
9672 (let ((base (point)))
9673 (if (c-major-mode-is 'c++-mode)
9675 ;; In C++ we need to take special care to handle operator
9676 ;; tokens and those pesky template brackets.
9677 (while (and
9678 (c-syntactic-re-search-forward "[;{<=]" nil 'move t t)
9680 (c-end-of-current-token base)
9681 ;; Handle operator identifiers, i.e. ignore any
9682 ;; operator token preceded by "operator".
9683 (save-excursion
9684 (and (c-safe (c-backward-sexp) t)
9685 (looking-at c-opt-op-identifier-prefix)))
9686 (and (eq (char-before) ?<)
9687 (c-with-syntax-table c++-template-syntax-table
9688 (if (c-safe (goto-char (c-up-list-forward (point))))
9690 (goto-char (point-max))
9691 nil)))))
9692 (setq base (point)))
9694 (while (and
9695 (c-syntactic-re-search-forward "[;{=]" nil 'move t t)
9696 (c-end-of-current-token base))
9697 (setq base (point))))))
9699 (defun c-beginning-of-decl-1 (&optional lim)
9700 ;; Go to the beginning of the current declaration, or the beginning
9701 ;; of the previous one if already at the start of it. Point won't
9702 ;; be moved out of any surrounding paren. Return a cons cell of the
9703 ;; form (MOVE . KNR-POS). MOVE is like the return value from
9704 ;; `c-beginning-of-statement-1'. If point skipped over some K&R
9705 ;; style argument declarations (and they are to be recognized) then
9706 ;; KNR-POS is set to the start of the first such argument
9707 ;; declaration, otherwise KNR-POS is nil. If LIM is non-nil, it's a
9708 ;; position that bounds the backward search.
9710 ;; NB: Cases where the declaration continues after the block, as in
9711 ;; "struct foo { ... } bar;", are currently recognized as two
9712 ;; declarations, e.g. "struct foo { ... }" and "bar;" in this case.
9714 ;; This function might do hidden buffer changes.
9715 (catch 'return
9716 (let* ((start (point))
9717 (last-stmt-start (point))
9718 (move (c-beginning-of-statement-1 lim nil t)))
9720 ;; `c-beginning-of-statement-1' stops at a block start, but we
9721 ;; want to continue if the block doesn't begin a top level
9722 ;; construct, i.e. if it isn't preceded by ';', '}', ':', bob,
9723 ;; or an open paren.
9724 (let ((beg (point)) tentative-move)
9725 ;; Go back one "statement" each time round the loop until we're just
9726 ;; after a ;, }, or :, or at BOB or the start of a macro or start of
9727 ;; an ObjC method. This will move over a multiple declaration whose
9728 ;; components are comma separated.
9729 (while (and
9730 ;; Must check with c-opt-method-key in ObjC mode.
9731 (not (and c-opt-method-key
9732 (looking-at c-opt-method-key)))
9733 (/= last-stmt-start (point))
9734 (progn
9735 (c-backward-syntactic-ws lim)
9736 (not (or (memq (char-before) '(?\; ?} ?: nil))
9737 (c-at-vsemi-p))))
9738 (save-excursion
9739 (backward-char)
9740 (not (looking-at "\\s(")))
9741 ;; Check that we don't move from the first thing in a
9742 ;; macro to its header.
9743 (not (eq (setq tentative-move
9744 (c-beginning-of-statement-1 lim nil t))
9745 'macro)))
9746 (setq last-stmt-start beg
9747 beg (point)
9748 move tentative-move))
9749 (goto-char beg))
9751 (when c-recognize-knr-p
9752 (let ((fallback-pos (point)) knr-argdecl-start)
9753 ;; Handle K&R argdecls. Back up after the "statement" jumped
9754 ;; over by `c-beginning-of-statement-1', unless it was the
9755 ;; function body, in which case we're sitting on the opening
9756 ;; brace now. Then test if we're in a K&R argdecl region and
9757 ;; that we started at the other side of the first argdecl in
9758 ;; it.
9759 (unless (eq (char-after) ?{)
9760 (goto-char last-stmt-start))
9761 (if (and (setq knr-argdecl-start (c-in-knr-argdecl lim))
9762 (< knr-argdecl-start start)
9763 (progn
9764 (goto-char knr-argdecl-start)
9765 (not (eq (c-beginning-of-statement-1 lim nil t) 'macro))))
9766 (throw 'return
9767 (cons (if (eq (char-after fallback-pos) ?{)
9768 'previous
9769 'same)
9770 knr-argdecl-start))
9771 (goto-char fallback-pos))))
9773 ;; `c-beginning-of-statement-1' counts each brace block as a separate
9774 ;; statement, so the result will be 'previous if we've moved over any.
9775 ;; So change our result back to 'same if necessary.
9777 ;; If they were brace list initializers we might not have moved over a
9778 ;; declaration boundary though, so change it to 'same if we've moved
9779 ;; past a '=' before '{', but not ';'. (This ought to be integrated
9780 ;; into `c-beginning-of-statement-1', so we avoid this extra pass which
9781 ;; potentially can search over a large amount of text.). Take special
9782 ;; pains not to get mislead by C++'s "operator=", and the like.
9783 (if (and (eq move 'previous)
9784 (c-with-syntax-table (if (c-major-mode-is 'c++-mode)
9785 c++-template-syntax-table
9786 (syntax-table))
9787 (save-excursion
9788 (and
9789 (progn
9790 (while ; keep going back to "[;={"s until we either find
9791 ; no more, or get to one which isn't an "operator ="
9792 (and (c-syntactic-re-search-forward "[;={]" start t t t)
9793 (eq (char-before) ?=)
9794 c-overloadable-operators-regexp
9795 c-opt-op-identifier-prefix
9796 (save-excursion
9797 (eq (c-backward-token-2) 0)
9798 (looking-at c-overloadable-operators-regexp)
9799 (eq (c-backward-token-2) 0)
9800 (looking-at c-opt-op-identifier-prefix))))
9801 (eq (char-before) ?=))
9802 (c-syntactic-re-search-forward "[;{]" start t t)
9803 (eq (char-before) ?{)
9804 (c-safe (goto-char (c-up-list-forward (point))) t)
9805 (not (c-syntactic-re-search-forward ";" start t t))))))
9806 (cons 'same nil)
9807 (cons move nil)))))
9809 (defun c-end-of-decl-1 ()
9810 ;; Assuming point is at the start of a declaration (as detected by
9811 ;; e.g. `c-beginning-of-decl-1'), go to the end of it. Unlike
9812 ;; `c-beginning-of-decl-1', this function handles the case when a
9813 ;; block is followed by identifiers in e.g. struct declarations in C
9814 ;; or C++. If a proper end was found then t is returned, otherwise
9815 ;; point is moved as far as possible within the current sexp and nil
9816 ;; is returned. This function doesn't handle macros; use
9817 ;; `c-end-of-macro' instead in those cases.
9819 ;; This function might do hidden buffer changes.
9820 (let ((start (point))
9821 (decl-syntax-table (if (c-major-mode-is 'c++-mode)
9822 c++-template-syntax-table
9823 (syntax-table))))
9824 (catch 'return
9825 (c-search-decl-header-end)
9827 (when (and c-recognize-knr-p
9828 (eq (char-before) ?\;)
9829 (c-in-knr-argdecl start))
9830 ;; Stopped at the ';' in a K&R argdecl section which is
9831 ;; detected using the same criteria as in
9832 ;; `c-beginning-of-decl-1'. Move to the following block
9833 ;; start.
9834 (c-syntactic-re-search-forward "{" nil 'move t))
9836 (when (eq (char-before) ?{)
9837 ;; Encountered a block in the declaration. Jump over it.
9838 (condition-case nil
9839 (goto-char (c-up-list-forward (point)))
9840 (error (goto-char (point-max))
9841 (throw 'return nil)))
9842 (if (or (not c-opt-block-decls-with-vars-key)
9843 (save-excursion
9844 (c-with-syntax-table decl-syntax-table
9845 (let ((lim (point)))
9846 (goto-char start)
9847 (not (and
9848 ;; Check for `c-opt-block-decls-with-vars-key'
9849 ;; before the first paren.
9850 (c-syntactic-re-search-forward
9851 (concat "[;=([{]\\|\\("
9852 c-opt-block-decls-with-vars-key
9853 "\\)")
9854 lim t t t)
9855 (match-beginning 1)
9856 (not (eq (char-before) ?_))
9857 ;; Check that the first following paren is
9858 ;; the block.
9859 (c-syntactic-re-search-forward "[;=([{]"
9860 lim t t t)
9861 (eq (char-before) ?{)))))))
9862 ;; The declaration doesn't have any of the
9863 ;; `c-opt-block-decls-with-vars' keywords in the
9864 ;; beginning, so it ends here at the end of the block.
9865 (throw 'return t)))
9867 (c-with-syntax-table decl-syntax-table
9868 (while (progn
9869 (if (eq (char-before) ?\;)
9870 (throw 'return t))
9871 (c-syntactic-re-search-forward ";" nil 'move t))))
9872 nil)))
9874 (defun c-looking-at-decl-block (containing-sexp goto-start &optional limit)
9875 ;; Assuming the point is at an open brace, check if it starts a
9876 ;; block that contains another declaration level, i.e. that isn't a
9877 ;; statement block or a brace list, and if so return non-nil.
9879 ;; If the check is successful, the return value is the start of the
9880 ;; keyword that tells what kind of construct it is, i.e. typically
9881 ;; what `c-decl-block-key' matched. Also, if GOTO-START is set then
9882 ;; the point will be at the start of the construct, before any
9883 ;; leading specifiers, otherwise it's at the returned position.
9885 ;; The point is clobbered if the check is unsuccessful.
9887 ;; CONTAINING-SEXP is the position of the open of the surrounding
9888 ;; paren, or nil if none.
9890 ;; The optional LIMIT limits the backward search for the start of
9891 ;; the construct. It's assumed to be at a syntactically relevant
9892 ;; position.
9894 ;; If any template arglists are found in the searched region before
9895 ;; the open brace, they get marked with paren syntax.
9897 ;; This function might do hidden buffer changes.
9899 (let ((open-brace (point)) kwd-start first-specifier-pos)
9900 (c-syntactic-skip-backward c-block-prefix-charset limit t)
9902 (when (and c-recognize-<>-arglists
9903 (eq (char-before) ?>))
9904 ;; Could be at the end of a template arglist.
9905 (let ((c-parse-and-markup-<>-arglists t))
9906 (while (and
9907 (c-backward-<>-arglist nil limit)
9908 (progn
9909 (c-syntactic-skip-backward c-block-prefix-charset limit t)
9910 (eq (char-before) ?>))))))
9912 ;; Skip back over noise clauses.
9913 (while (and
9914 c-opt-cpp-prefix
9915 (eq (char-before) ?\))
9916 (let ((after-paren (point)))
9917 (if (and (c-go-list-backward)
9918 (progn (c-backward-syntactic-ws)
9919 (c-simple-skip-symbol-backward))
9920 (or (looking-at c-paren-nontype-key)
9921 (looking-at c-noise-macro-with-parens-name-re)))
9922 (progn
9923 (c-syntactic-skip-backward c-block-prefix-charset limit t)
9925 (goto-char after-paren)
9926 nil))))
9928 ;; Note: Can't get bogus hits inside template arglists below since they
9929 ;; have gotten paren syntax above.
9930 (when (and
9931 ;; If `goto-start' is set we begin by searching for the
9932 ;; first possible position of a leading specifier list.
9933 ;; The `c-decl-block-key' search continues from there since
9934 ;; we know it can't match earlier.
9935 (if goto-start
9936 (when (c-syntactic-re-search-forward c-symbol-start
9937 open-brace t t)
9938 (goto-char (setq first-specifier-pos (match-beginning 0)))
9942 (cond
9943 ((c-syntactic-re-search-forward c-decl-block-key open-brace t t t)
9944 (goto-char (setq kwd-start (match-beginning 0)))
9945 (and
9946 ;; Exclude cases where we matched what would ordinarily
9947 ;; be a block declaration keyword, except where it's not
9948 ;; legal because it's part of a "compound keyword" like
9949 ;; "enum class". Of course, if c-after-brace-list-key
9950 ;; is nil, we can skip the test.
9951 (or (equal c-after-brace-list-key "\\<\\>")
9952 (save-match-data
9953 (save-excursion
9954 (not
9955 (and
9956 (looking-at c-after-brace-list-key)
9957 (= (c-backward-token-2 1 t) 0)
9958 (looking-at c-brace-list-key))))))
9960 ;; Found a keyword that can't be a type?
9961 (match-beginning 1)
9963 ;; Can be a type too, in which case it's the return type of a
9964 ;; function (under the assumption that no declaration level
9965 ;; block construct starts with a type).
9966 (not (c-forward-type))
9968 ;; Jumped over a type, but it could be a declaration keyword
9969 ;; followed by the declared identifier that we've jumped over
9970 ;; instead (e.g. in "class Foo {"). If it indeed is a type
9971 ;; then we should be at the declarator now, so check for a
9972 ;; valid declarator start.
9974 ;; Note: This doesn't cope with the case when a declared
9975 ;; identifier is followed by e.g. '(' in a language where '('
9976 ;; also might be part of a declarator expression. Currently
9977 ;; there's no such language.
9978 (not (or (looking-at c-symbol-start)
9979 (looking-at c-type-decl-prefix-key))))))
9981 ;; In Pike a list of modifiers may be followed by a brace
9982 ;; to make them apply to many identifiers. Note that the
9983 ;; match data will be empty on return in this case.
9984 ((and (c-major-mode-is 'pike-mode)
9985 (progn
9986 (goto-char open-brace)
9987 (= (c-backward-token-2) 0))
9988 (looking-at c-specifier-key)
9989 ;; Use this variant to avoid yet another special regexp.
9990 (c-keyword-member (c-keyword-sym (match-string 1))
9991 'c-modifier-kwds))
9992 (setq kwd-start (point))
9993 t)))
9995 ;; Got a match.
9997 (if goto-start
9998 ;; Back up over any preceding specifiers and their clauses
9999 ;; by going forward from `first-specifier-pos', which is the
10000 ;; earliest possible position where the specifier list can
10001 ;; start.
10002 (progn
10003 (goto-char first-specifier-pos)
10005 (while (< (point) kwd-start)
10006 (if (looking-at c-symbol-key)
10007 ;; Accept any plain symbol token on the ground that
10008 ;; it's a specifier masked through a macro (just
10009 ;; like `c-forward-decl-or-cast-1' skip forward over
10010 ;; such tokens).
10012 ;; Could be more restrictive wrt invalid keywords,
10013 ;; but that'd only occur in invalid code so there's
10014 ;; no use spending effort on it.
10015 (let ((end (match-end 0)))
10016 (unless (c-forward-keyword-clause 0)
10017 (goto-char end)
10018 (c-forward-syntactic-ws)))
10020 ;; Can't parse a declaration preamble and is still
10021 ;; before `kwd-start'. That means `first-specifier-pos'
10022 ;; was in some earlier construct. Search again.
10023 (if (c-syntactic-re-search-forward c-symbol-start
10024 kwd-start 'move t)
10025 (goto-char (setq first-specifier-pos (match-beginning 0)))
10026 ;; Got no preamble before the block declaration keyword.
10027 (setq first-specifier-pos kwd-start))))
10029 (goto-char first-specifier-pos))
10030 (goto-char kwd-start))
10032 kwd-start)))
10034 (defun c-directly-in-class-called-p (name)
10035 ;; Check whether point is directly inside a brace block which is the brace
10036 ;; block of a class, struct, or union which is called NAME, a string.
10037 (let* ((paren-state (c-parse-state))
10038 (brace-pos (c-pull-open-brace paren-state))
10040 (when (eq (char-after brace-pos) ?{)
10041 (goto-char brace-pos)
10042 (save-excursion
10043 ; *c-looking-at-decl-block
10044 ; containing-sexp goto-start &optional
10045 ; limit)
10046 (when (and (c-looking-at-decl-block
10047 (c-pull-open-brace paren-state)
10048 nil)
10049 (looking-at c-class-key))
10050 (goto-char (match-end 1))
10051 (c-forward-syntactic-ws)
10052 (looking-at name))))))
10054 (defun c-search-uplist-for-classkey (paren-state)
10055 ;; Check if the closest containing paren sexp is a declaration
10056 ;; block, returning a 2 element vector in that case. Aref 0
10057 ;; contains the bufpos at boi of the class key line, and aref 1
10058 ;; contains the bufpos of the open brace. This function is an
10059 ;; obsolete wrapper for `c-looking-at-decl-block'.
10061 ;; This function might do hidden buffer changes.
10062 (let ((open-paren-pos (c-most-enclosing-brace paren-state)))
10063 (when open-paren-pos
10064 (save-excursion
10065 (goto-char open-paren-pos)
10066 (when (and (eq (char-after) ?{)
10067 (c-looking-at-decl-block
10068 (c-safe-position open-paren-pos paren-state)
10069 nil))
10070 (back-to-indentation)
10071 (vector (point) open-paren-pos))))))
10073 (defun c-most-enclosing-decl-block (paren-state)
10074 ;; Return the buffer position of the most enclosing decl-block brace (in the
10075 ;; sense of c-looking-at-decl-block) in the PAREN-STATE structure, or nil if
10076 ;; none was found.
10077 (let* ((open-brace (c-pull-open-brace paren-state))
10078 (next-open-brace (c-pull-open-brace paren-state)))
10079 (while (and open-brace
10080 (save-excursion
10081 (goto-char open-brace)
10082 (not (c-looking-at-decl-block next-open-brace nil))))
10083 (setq open-brace next-open-brace
10084 next-open-brace (c-pull-open-brace paren-state)))
10085 open-brace))
10087 (defun c-cheap-inside-bracelist-p (paren-state)
10088 ;; Return the position of the L-brace if point is inside a brace list
10089 ;; initialization of an array, etc. This is an approximate function,
10090 ;; designed for speed over accuracy. It will not find every bracelist, but
10091 ;; a non-nil result is reliable. We simply search for "= {" (naturally with
10092 ;; syntactic whitespace allowed). PAREN-STATE is the normal thing that it
10093 ;; is everywhere else.
10094 (let (b-pos)
10095 (save-excursion
10096 (while
10097 (and (setq b-pos (c-pull-open-brace paren-state))
10098 (progn (goto-char b-pos)
10099 (c-backward-sws)
10100 (c-backward-token-2)
10101 (not (looking-at "=")))))
10102 b-pos)))
10104 (defun c-backward-typed-enum-colon ()
10105 ;; We're at a "{" which might be the opening brace of a enum which is
10106 ;; strongly typed (by a ":" followed by a type). If this is the case, leave
10107 ;; point before the colon and return t. Otherwise leave point unchanged and return nil.
10108 ;; Match data will be clobbered.
10109 (let ((here (point))
10110 (colon-pos nil))
10111 (save-excursion
10112 (while
10113 (and (eql (c-backward-token-2) 0)
10114 (or (not (looking-at "\\s)"))
10115 (c-go-up-list-backward))
10116 (cond
10117 ((and (eql (char-after) ?:)
10118 (save-excursion
10119 (c-backward-syntactic-ws)
10120 (or (c-on-identifier)
10121 (progn
10122 (c-backward-token-2)
10123 (looking-at c-brace-list-key)))))
10124 (setq colon-pos (point))
10125 (forward-char)
10126 (c-forward-syntactic-ws)
10127 (or (and (c-forward-type)
10128 (progn (c-forward-syntactic-ws)
10129 (eq (point) here)))
10130 (setq colon-pos nil))
10131 nil)
10132 ((eql (char-after) ?\()
10134 ((looking-at c-symbol-key)
10136 (t nil)))))
10137 (when colon-pos
10138 (goto-char colon-pos)
10139 t)))
10141 (defun c-backward-over-enum-header ()
10142 ;; We're at a "{". Move back to the enum-like keyword that starts this
10143 ;; declaration and return t, otherwise don't move and return nil.
10144 (let ((here (point))
10145 up-sexp-pos before-identifier)
10146 (when c-recognize-post-brace-list-type-p
10147 (c-backward-typed-enum-colon))
10148 (while
10149 (and
10150 (eq (c-backward-token-2) 0)
10151 (or (not (looking-at "\\s)"))
10152 (c-go-up-list-backward))
10153 (cond
10154 ((and (looking-at c-symbol-key) (c-on-identifier)
10155 (not before-identifier))
10156 (setq before-identifier t))
10157 ((and before-identifier
10158 (or (eql (char-after) ?,)
10159 (looking-at c-postfix-decl-spec-key)))
10160 (setq before-identifier nil)
10162 ((looking-at c-after-brace-list-key) t)
10163 ((looking-at c-brace-list-key) nil)
10164 ((eq (char-after) ?\()
10165 (and (eq (c-backward-token-2) 0)
10166 (or (looking-at c-decl-hangon-key)
10167 (and c-opt-cpp-prefix
10168 (looking-at c-noise-macro-with-parens-name-re)))))
10170 ((and c-recognize-<>-arglists
10171 (eq (char-after) ?<)
10172 (looking-at "\\s("))
10174 (t nil))))
10175 (or (looking-at c-brace-list-key)
10176 (progn (goto-char here) nil))))
10178 (defun c-looking-at-or-maybe-in-bracelist (&optional containing-sexp lim)
10179 ;; Point is at an open brace. If this starts a brace list, return a list
10180 ;; whose car is the buffer position of the start of the construct which
10181 ;; introduces the list, and whose cdr is t if we have parsed a keyword
10182 ;; matching `c-opt-inexpr-brace-list-key' (e.g. Java's "new"), nil
10183 ;; otherwise. Otherwise, if point might be inside an enclosing brace list,
10184 ;; return t. If point is definitely neither at nor in a brace list, return
10185 ;; nil.
10187 ;; CONTAINING-SEXP is the position of the brace/paren/bracket enclosing
10188 ;; POINT, or nil if there is no such position, or we do not know it. LIM is
10189 ;; a backward search limit.
10191 ;; Here, "brace list" does not include the body of an enum.
10192 (save-excursion
10193 (let ((start (point))
10194 (class-key
10195 ;; Pike can have class definitions anywhere, so we must
10196 ;; check for the class key here.
10197 (and (c-major-mode-is 'pike-mode)
10198 c-decl-block-key))
10199 (braceassignp 'dontknow)
10200 inexpr-brace-list bufpos macro-start res pos after-type-id-pos)
10202 (setq res (c-backward-token-2 1 t lim))
10203 ;; Checks to do only on the first sexp before the brace.
10204 ;; Have we a C++ initialization, without an "="?
10205 (if (and (c-major-mode-is 'c++-mode)
10206 (cond
10207 ((and (not (eq res 0))
10208 (c-go-up-list-backward nil lim) ; FIXME!!! Check ; `lim' 2016-07-12.
10209 (eq (char-after) ?\())
10210 (setq braceassignp 'c++-noassign))
10211 ((looking-at c-pre-id-bracelist-key))
10212 ((looking-at c-return-key))
10213 ((and (looking-at c-symbol-start)
10214 (not (looking-at c-keywords-regexp)))
10215 (setq after-type-id-pos (point)))
10216 (t nil))
10217 (save-excursion
10218 (cond
10219 ((not (eq res 0))
10220 (and (c-go-up-list-backward nil lim) ; FIXME!!! Check `lim' 2016-07-12.
10221 (eq (char-after) ?\()))
10222 ((looking-at c-pre-id-bracelist-key))
10223 ((looking-at c-return-key))
10224 (t (setq after-type-id-pos (point))
10225 nil))))
10226 (setq braceassignp 'c++-noassign))
10228 (when (and c-opt-inexpr-brace-list-key
10229 (eq (char-after) ?\[))
10230 ;; In Java, an initialization brace list may follow
10231 ;; directly after "new Foo[]", so check for a "new"
10232 ;; earlier.
10233 (while (eq braceassignp 'dontknow)
10234 (setq braceassignp
10235 (cond ((/= (c-backward-token-2 1 t lim) 0) nil)
10236 ((looking-at c-opt-inexpr-brace-list-key)
10237 (setq inexpr-brace-list t)
10239 ((looking-at "\\sw\\|\\s_\\|[.[]")
10240 ;; Carry on looking if this is an
10241 ;; identifier (may contain "." in Java)
10242 ;; or another "[]" sexp.
10243 'dontknow)
10244 (t nil)))))
10246 (setq pos (point))
10247 (if (and after-type-id-pos
10248 (goto-char after-type-id-pos)
10249 (setq res (c-back-over-member-initializers))
10250 (goto-char res)
10251 (eq (car (c-beginning-of-decl-1 lim)) 'same))
10252 (cons (point) nil) ; Return value.
10254 (goto-char pos)
10255 ;; Checks to do on all sexps before the brace, up to the
10256 ;; beginning of the statement.
10257 (while (eq braceassignp 'dontknow)
10258 (cond ((eq (char-after) ?\;)
10259 (setq braceassignp nil))
10260 ((and class-key
10261 (looking-at class-key))
10262 (setq braceassignp nil))
10263 ((eq (char-after) ?=)
10264 ;; We've seen a =, but must check earlier tokens so
10265 ;; that it isn't something that should be ignored.
10266 (setq braceassignp 'maybe)
10267 (while (and (eq braceassignp 'maybe)
10268 (zerop (c-backward-token-2 1 t lim)))
10269 (setq braceassignp
10270 (cond
10271 ;; Check for operator =
10272 ((and c-opt-op-identifier-prefix
10273 (looking-at c-opt-op-identifier-prefix))
10274 nil)
10275 ;; Check for `<opchar>= in Pike.
10276 ((and (c-major-mode-is 'pike-mode)
10277 (or (eq (char-after) ?`)
10278 ;; Special case for Pikes
10279 ;; `[]=, since '[' is not in
10280 ;; the punctuation class.
10281 (and (eq (char-after) ?\[)
10282 (eq (char-before) ?`))))
10283 nil)
10284 ((looking-at "\\s.") 'maybe)
10285 ;; make sure we're not in a C++ template
10286 ;; argument assignment
10287 ((and
10288 (c-major-mode-is 'c++-mode)
10289 (save-excursion
10290 (let ((here (point))
10291 (pos< (progn
10292 (skip-chars-backward "^<>")
10293 (point))))
10294 (and (eq (char-before) ?<)
10295 (not (c-crosses-statement-barrier-p
10296 pos< here))
10297 (not (c-in-literal))
10298 ))))
10299 nil)
10300 (t t))))))
10301 (if (and (eq braceassignp 'dontknow)
10302 (/= (c-backward-token-2 1 t lim) 0))
10303 (setq braceassignp nil)))
10305 (cond
10306 (braceassignp
10307 ;; We've hit the beginning of the aggregate list.
10308 (c-beginning-of-statement-1 containing-sexp)
10309 (cons (point) inexpr-brace-list))
10310 ((and after-type-id-pos
10311 (save-excursion
10312 (when (eq (char-after) ?\;)
10313 (c-forward-token-2 1 t))
10314 (setq bufpos (point))
10315 (when (looking-at c-opt-<>-sexp-key)
10316 (c-forward-token-2)
10317 (when (and (eq (char-after) ?<)
10318 (c-get-char-property (point) 'syntax-table))
10319 (c-go-list-forward nil after-type-id-pos)
10320 (c-forward-syntactic-ws)))
10321 (and
10322 (or (not (looking-at c-class-key))
10323 (save-excursion
10324 (goto-char (match-end 1))
10325 (c-forward-syntactic-ws)
10326 (not (eq (point) after-type-id-pos))))
10327 (progn
10328 (setq res
10329 (c-forward-decl-or-cast-1
10330 (save-excursion (c-backward-syntactic-ws) (point))
10331 nil nil))
10332 (and (consp res)
10333 (eq (car res) after-type-id-pos))))))
10334 (cons bufpos inexpr-brace-list))
10335 ((eq (char-after) ?\;)
10336 ;; Brace lists can't contain a semicolon, so we're done.
10337 ;; (setq containing-sexp nil)
10338 nil)
10339 ((and (setq macro-start (point))
10340 (c-forward-to-cpp-define-body)
10341 (eq (point) start))
10342 ;; We've a macro whose expansion starts with the '{'.
10343 ;; Heuristically, if we have a ';' in it we've not got a
10344 ;; brace list, otherwise we have.
10345 (let ((macro-end (progn (c-end-of-macro) (point))))
10346 (goto-char start)
10347 (forward-char)
10348 (if (and (c-syntactic-re-search-forward "[;,]" macro-end t t)
10349 (eq (char-before) ?\;))
10351 (cons macro-start nil)))) ; (2016-08-30): Lazy! We have no
10352 ; languages where
10353 ; `c-opt-inexpr-brace-list-key' is
10354 ; non-nil and we have macros.
10355 (t t))) ;; The caller can go up one level.
10358 (defun c-inside-bracelist-p (containing-sexp paren-state)
10359 ;; return the buffer position of the beginning of the brace list
10360 ;; statement if we're inside a brace list, otherwise return nil.
10361 ;; CONTAINING-SEXP is the buffer pos of the innermost containing
10362 ;; paren. PAREN-STATE is the remainder of the state of enclosing
10363 ;; braces
10365 ;; N.B.: This algorithm can potentially get confused by cpp macros
10366 ;; placed in inconvenient locations. It's a trade-off we make for
10367 ;; speed.
10369 ;; This function might do hidden buffer changes.
10371 ;; This will pick up brace list declarations.
10372 (save-excursion
10373 (goto-char containing-sexp)
10374 (c-backward-over-enum-header))
10375 ;; this will pick up array/aggregate init lists, even if they are nested.
10376 (save-excursion
10377 (let ((bufpos t)
10378 lim next-containing)
10379 (while (and (eq bufpos t)
10380 containing-sexp)
10381 (when paren-state
10382 (if (consp (car paren-state))
10383 (setq lim (cdr (car paren-state))
10384 paren-state (cdr paren-state))
10385 (setq lim (car paren-state)))
10386 (when paren-state
10387 (setq next-containing (car paren-state)
10388 paren-state (cdr paren-state))))
10390 (goto-char containing-sexp)
10391 (if (c-looking-at-inexpr-block next-containing next-containing)
10392 ;; We're in an in-expression block of some kind. Do not
10393 ;; check nesting. We deliberately set the limit to the
10394 ;; containing sexp, so that c-looking-at-inexpr-block
10395 ;; doesn't check for an identifier before it.
10396 (setq bufpos nil)
10397 (when (or (not (eq (char-after) ?{))
10398 (eq (setq bufpos (c-looking-at-or-maybe-in-bracelist
10399 next-containing lim))
10401 (setq containing-sexp next-containing
10402 lim nil
10403 next-containing nil))))
10404 (and (consp bufpos) (car bufpos))))))
10406 (defun c-looking-at-special-brace-list (&optional lim)
10407 ;; If we're looking at the start of a pike-style list, i.e., `({ })',
10408 ;; `([ ])', `(< >)', etc., a cons of a cons of its starting and ending
10409 ;; positions and its entry in c-special-brace-lists is returned, nil
10410 ;; otherwise. The ending position is nil if the list is still open.
10411 ;; LIM is the limit for forward search. The point may either be at
10412 ;; the `(' or at the following paren character. Tries to check the
10413 ;; matching closer, but assumes it's correct if no balanced paren is
10414 ;; found (i.e. the case `({ ... } ... )' is detected as _not_ being
10415 ;; a special brace list).
10417 ;; This function might do hidden buffer changes.
10418 (if c-special-brace-lists
10419 (condition-case ()
10420 (save-excursion
10421 (let ((beg (point))
10422 inner-beg end type)
10423 (c-forward-syntactic-ws)
10424 (if (eq (char-after) ?\()
10425 (progn
10426 (forward-char 1)
10427 (c-forward-syntactic-ws)
10428 (setq inner-beg (point))
10429 (setq type (assq (char-after) c-special-brace-lists)))
10430 (if (setq type (assq (char-after) c-special-brace-lists))
10431 (progn
10432 (setq inner-beg (point))
10433 (c-backward-syntactic-ws)
10434 (forward-char -1)
10435 (setq beg (if (eq (char-after) ?\()
10436 (point)
10437 nil)))))
10438 (if (and beg type)
10439 (if (and (c-safe
10440 (goto-char beg)
10441 (c-forward-sexp 1)
10442 (setq end (point))
10443 (= (char-before) ?\)))
10444 (c-safe
10445 (goto-char inner-beg)
10446 (if (looking-at "\\s(")
10447 ;; Check balancing of the inner paren
10448 ;; below.
10449 (progn
10450 (c-forward-sexp 1)
10452 ;; If the inner char isn't a paren then
10453 ;; we can't check balancing, so just
10454 ;; check the char before the outer
10455 ;; closing paren.
10456 (goto-char end)
10457 (backward-char)
10458 (c-backward-syntactic-ws)
10459 (= (char-before) (cdr type)))))
10460 (if (or (/= (char-syntax (char-before)) ?\))
10461 (= (progn
10462 (c-forward-syntactic-ws)
10463 (point))
10464 (1- end)))
10465 (cons (cons beg end) type))
10466 (cons (list beg) type)))))
10467 (error nil))))
10469 (defun c-looking-at-bos (&optional lim)
10470 ;; Return non-nil if between two statements or declarations, assuming
10471 ;; point is not inside a literal or comment.
10473 ;; Obsolete - `c-at-statement-start-p' or `c-at-expression-start-p'
10474 ;; are recommended instead.
10476 ;; This function might do hidden buffer changes.
10477 (c-at-statement-start-p))
10478 (make-obsolete 'c-looking-at-bos 'c-at-statement-start-p "22.1")
10480 (defun c-looking-at-inexpr-block (lim containing-sexp &optional check-at-end)
10481 ;; Return non-nil if we're looking at the beginning of a block
10482 ;; inside an expression. The value returned is actually a cons of
10483 ;; either 'inlambda, 'inexpr-statement or 'inexpr-class and the
10484 ;; position of the beginning of the construct.
10486 ;; LIM limits the backward search. CONTAINING-SEXP is the start
10487 ;; position of the closest containing list. If it's nil, the
10488 ;; containing paren isn't used to decide whether we're inside an
10489 ;; expression or not. If both LIM and CONTAINING-SEXP are used, LIM
10490 ;; needs to be farther back.
10492 ;; If CHECK-AT-END is non-nil then extra checks at the end of the
10493 ;; brace block might be done. It should only be used when the
10494 ;; construct can be assumed to be complete, i.e. when the original
10495 ;; starting position was further down than that.
10497 ;; This function might do hidden buffer changes.
10499 (save-excursion
10500 (let ((res 'maybe) (passed-bracket-pairs 0) bracket-pos passed-paren
10501 haskell-op-pos
10502 (closest-lim (or containing-sexp lim (point-min)))
10503 ;; Look at the character after point only as a last resort
10504 ;; when we can't disambiguate.
10505 (block-follows (and (eq (char-after) ?{) (point))))
10507 ;; Search for a C++11 "->" which suggests a lambda declaration.
10508 (when (and (c-major-mode-is 'c++-mode)
10509 (setq haskell-op-pos
10510 (save-excursion
10511 (while
10512 (progn
10513 (c-syntactic-skip-backward "^;=}>" closest-lim t)
10514 (and (eq (char-before) ?>)
10515 (c-backward-token-2)
10516 (not (looking-at c-haskell-op-re)))))
10517 (and (looking-at c-haskell-op-re)
10518 (point)))))
10519 (goto-char haskell-op-pos))
10521 (while (and (eq res 'maybe)
10522 (progn (c-backward-syntactic-ws)
10523 (> (point) closest-lim))
10524 (not (bobp))
10525 (progn (backward-char)
10526 (looking-at "[]).]\\|\\w\\|\\s_"))
10527 (c-safe (forward-char)
10528 (goto-char (scan-sexps (point) -1))))
10530 (setq res
10531 (if (looking-at c-keywords-regexp)
10532 (let ((kw-sym (c-keyword-sym (match-string 1))))
10533 (cond
10534 ((and block-follows
10535 (c-keyword-member kw-sym 'c-inexpr-class-kwds))
10536 (and (not (eq passed-paren ?\[))
10537 (or (not (looking-at c-class-key))
10538 ;; If the class definition is at the start of
10539 ;; a statement, we don't consider it an
10540 ;; in-expression class.
10541 (let ((prev (point)))
10542 (while (and
10543 (= (c-backward-token-2 1 nil closest-lim) 0)
10544 (eq (char-syntax (char-after)) ?w))
10545 (setq prev (point)))
10546 (goto-char prev)
10547 (not (c-at-statement-start-p)))
10548 ;; Also, in Pike we treat it as an
10549 ;; in-expression class if it's used in an
10550 ;; object clone expression.
10551 (save-excursion
10552 (and check-at-end
10553 (c-major-mode-is 'pike-mode)
10554 (progn (goto-char block-follows)
10555 (zerop (c-forward-token-2 1 t)))
10556 (eq (char-after) ?\())))
10557 (cons 'inexpr-class (point))))
10558 ((c-keyword-member kw-sym 'c-paren-any-kwds) ; e.g. C++11 "throw" or "noexcept"
10559 (setq passed-paren nil)
10560 (setq passed-bracket-pairs 0)
10561 (setq bracket-pos nil)
10562 'maybe)
10563 ((c-keyword-member kw-sym 'c-inexpr-block-kwds)
10564 (when (not passed-paren)
10565 (cons 'inexpr-statement (point))))
10566 ((c-keyword-member kw-sym 'c-lambda-kwds)
10567 (when (or (not passed-paren)
10568 (eq passed-paren ?\())
10569 (cons 'inlambda (point))))
10570 ((c-keyword-member kw-sym 'c-block-stmt-kwds)
10571 nil)
10573 'maybe)))
10575 (if (looking-at "\\s(")
10576 (if passed-paren
10577 (cond
10578 ((and (eq passed-paren ?\[)
10579 (eq (char-after) ?\[)
10580 (not (eq (char-after (1+ (point))) ?\[))) ; C++ attribute.
10581 ;; Accept several square bracket sexps for
10582 ;; Java array initializations.
10583 (setq passed-bracket-pairs (1+ passed-bracket-pairs))
10584 'maybe)
10585 ((and (eq passed-paren ?\()
10586 (eq (char-after) ?\[)
10587 (not (eq (char-after (1+ (point))) ?\[))
10588 (eq passed-bracket-pairs 0))
10589 ;; C++11 lambda function declaration
10590 (setq passed-bracket-pairs 1)
10591 (setq bracket-pos (point))
10592 'maybe)
10593 (t nil))
10594 (when (not (looking-at "\\[\\["))
10595 (setq passed-paren (char-after))
10596 (when (eq passed-paren ?\[)
10597 (setq passed-bracket-pairs 1)
10598 (setq bracket-pos (point))))
10599 'maybe)
10600 'maybe))))
10602 (if (eq res 'maybe)
10603 (cond
10604 ((and (c-major-mode-is 'c++-mode)
10605 block-follows
10606 (eq passed-bracket-pairs 1)
10607 (save-excursion
10608 (goto-char bracket-pos)
10609 (or (<= (point) (or lim (point-min)))
10610 (progn
10611 (c-backward-token-2 1 nil lim)
10612 (and
10613 (not (c-on-identifier))
10614 (not (looking-at c-opt-op-identifier-prefix)))))))
10615 (cons 'inlambda bracket-pos))
10616 ((and c-recognize-paren-inexpr-blocks
10617 block-follows
10618 containing-sexp
10619 (eq (char-after containing-sexp) ?\())
10620 (goto-char containing-sexp)
10621 (if (or (save-excursion
10622 (c-backward-syntactic-ws lim)
10623 (while (and (eq (char-before) ?>)
10624 (c-get-char-property (1- (point))
10625 'syntax-table)
10626 (c-go-list-backward nil lim))
10627 (c-backward-syntactic-ws lim))
10628 (and (> (point) (or lim (point-min)))
10629 (c-on-identifier)))
10630 (and c-special-brace-lists
10631 (c-looking-at-special-brace-list))
10632 (and (c-major-mode-is 'c++-mode)
10633 (save-excursion
10634 (goto-char block-follows)
10635 (if (c-go-list-forward)
10636 (progn
10637 (backward-char)
10638 (c-syntactic-skip-backward
10639 "^;," block-follows t)
10640 (not (eq (char-before) ?\;)))
10641 (or (not (c-syntactic-re-search-forward
10642 "[;,]" nil t t))
10643 (not (eq (char-before) ?\;)))))))
10645 (cons 'inexpr-statement (point)))))
10647 res))))
10649 (defun c-looking-at-inexpr-block-backward (paren-state)
10650 ;; Returns non-nil if we're looking at the end of an in-expression
10651 ;; block, otherwise the same as `c-looking-at-inexpr-block'.
10652 ;; PAREN-STATE is the paren state relevant at the current position.
10654 ;; This function might do hidden buffer changes.
10655 (save-excursion
10656 ;; We currently only recognize a block.
10657 (let ((here (point))
10658 (elem (car-safe paren-state))
10659 containing-sexp)
10660 (when (and (consp elem)
10661 (progn (goto-char (cdr elem))
10662 (c-forward-syntactic-ws here)
10663 (= (point) here)))
10664 (goto-char (car elem))
10665 (if (setq paren-state (cdr paren-state))
10666 (setq containing-sexp (car-safe paren-state)))
10667 (c-looking-at-inexpr-block (c-safe-position containing-sexp
10668 paren-state)
10669 containing-sexp)))))
10671 (defun c-looking-at-c++-lambda-capture-list ()
10672 ;; Return non-nil if we're at the opening "[" of the capture list of a C++
10673 ;; lambda function, nil otherwise.
10674 (and
10675 (eq (char-after) ?\[)
10676 (not (eq (char-before) ?\[))
10677 (not (eq (char-after (1+ (point))) ?\[))
10678 (save-excursion
10679 (or (eq (c-backward-token-2 1) 1)
10680 (looking-at c-pre-lambda-tokens-re)))
10681 (not (c-in-literal))))
10683 (defun c-at-macro-vsemi-p (&optional pos)
10684 ;; Is there a "virtual semicolon" at POS or point?
10685 ;; (See cc-defs.el for full details of "virtual semicolons".)
10687 ;; This is true when point is at the last non syntactic WS position on the
10688 ;; line, there is a macro call last on the line, and this particular macro's
10689 ;; name is defined by the regexp `c-vs-macro-regexp' as not needing a
10690 ;; semicolon.
10691 (save-excursion
10692 (save-restriction
10693 (widen)
10694 (if pos
10695 (goto-char pos)
10696 (setq pos (point)))
10697 (and
10698 c-macro-with-semi-re
10699 (eq (skip-chars-backward " \t") 0)
10701 ;; Check we've got nothing after this except comments and empty lines
10702 ;; joined by escaped EOLs.
10703 (skip-chars-forward " \t") ; always returns non-nil.
10704 (progn
10705 (while ; go over 1 block comment per iteration.
10706 (and
10707 (looking-at "\\(\\\\[\n\r][ \t]*\\)*")
10708 (goto-char (match-end 0))
10709 (cond
10710 ((looking-at c-block-comment-start-regexp)
10711 (and (forward-comment 1)
10712 (skip-chars-forward " \t"))) ; always returns non-nil
10713 ((looking-at c-line-comment-start-regexp)
10714 (end-of-line)
10715 nil)
10716 (t nil))))
10717 (eolp))
10719 (goto-char pos)
10720 (progn (c-backward-syntactic-ws)
10721 (eq (point) pos))
10723 ;; Check for one of the listed macros being before point.
10724 (or (not (eq (char-before) ?\)))
10725 (when (c-go-list-backward)
10726 (c-backward-syntactic-ws)
10728 (c-simple-skip-symbol-backward)
10729 (looking-at c-macro-with-semi-re)
10730 (goto-char pos)
10731 (not (c-in-literal)))))) ; The most expensive check last.
10733 (defun c-macro-vsemi-status-unknown-p () t) ; See cc-defs.el.
10736 ;; `c-guess-basic-syntax' and the functions that precedes it below
10737 ;; implements the main decision tree for determining the syntactic
10738 ;; analysis of the current line of code.
10740 ;; Dynamically bound to t when `c-guess-basic-syntax' is called during
10741 ;; auto newline analysis.
10742 (defvar c-auto-newline-analysis nil)
10744 (defun c-brace-anchor-point (bracepos)
10745 ;; BRACEPOS is the position of a brace in a construct like "namespace
10746 ;; Bar {". Return the anchor point in this construct; this is the
10747 ;; earliest symbol on the brace's line which isn't earlier than
10748 ;; "namespace".
10750 ;; Currently (2007-08-17), "like namespace" means "matches
10751 ;; c-other-block-decl-kwds". It doesn't work with "class" or "struct"
10752 ;; or anything like that.
10753 (save-excursion
10754 (let ((boi (c-point 'boi bracepos)))
10755 (goto-char bracepos)
10756 (while (and (> (point) boi)
10757 (not (looking-at c-other-decl-block-key)))
10758 (c-backward-token-2))
10759 (if (> (point) boi) (point) boi))))
10761 (defsubst c-add-syntax (symbol &rest args)
10762 ;; A simple function to prepend a new syntax element to
10763 ;; `c-syntactic-context'. Using `setq' on it is unsafe since it
10764 ;; should always be dynamically bound but since we read it first
10765 ;; we'll fail properly anyway if this function is misused.
10766 (setq c-syntactic-context (cons (cons symbol args)
10767 c-syntactic-context)))
10769 (defsubst c-append-syntax (symbol &rest args)
10770 ;; Like `c-add-syntax' but appends to the end of the syntax list.
10771 ;; (Normally not necessary.)
10772 (setq c-syntactic-context (nconc c-syntactic-context
10773 (list (cons symbol args)))))
10775 (defun c-add-stmt-syntax (syntax-symbol
10776 syntax-extra-args
10777 stop-at-boi-only
10778 containing-sexp
10779 paren-state)
10780 ;; Add the indicated SYNTAX-SYMBOL to `c-syntactic-context', extending it as
10781 ;; needed with further syntax elements of the types `substatement',
10782 ;; `inexpr-statement', `arglist-cont-nonempty', `statement-block-intro', and
10783 ;; `defun-block-intro'.
10785 ;; Do the generic processing to anchor the given syntax symbol on
10786 ;; the preceding statement: Skip over any labels and containing
10787 ;; statements on the same line, and then search backward until we
10788 ;; find a statement or block start that begins at boi without a
10789 ;; label or comment.
10791 ;; Point is assumed to be at the prospective anchor point for the
10792 ;; given SYNTAX-SYMBOL. More syntax entries are added if we need to
10793 ;; skip past open parens and containing statements. Most of the added
10794 ;; syntax elements will get the same anchor point - the exception is
10795 ;; for an anchor in a construct like "namespace"[*] - this is as early
10796 ;; as possible in the construct but on the same line as the {.
10798 ;; [*] i.e. with a keyword matching c-other-block-decl-kwds.
10800 ;; SYNTAX-EXTRA-ARGS are a list of the extra arguments for the
10801 ;; syntax symbol. They are appended after the anchor point.
10803 ;; If STOP-AT-BOI-ONLY is nil, we can stop in the middle of the line
10804 ;; if the current statement starts there.
10806 ;; Note: It's not a problem if PAREN-STATE "overshoots"
10807 ;; CONTAINING-SEXP, i.e. contains info about parens further down.
10809 ;; This function might do hidden buffer changes.
10811 (if (= (point) (c-point 'boi))
10812 ;; This is by far the most common case, so let's give it special
10813 ;; treatment.
10814 (apply 'c-add-syntax syntax-symbol (point) syntax-extra-args)
10816 (let ((syntax-last c-syntactic-context)
10817 (boi (c-point 'boi))
10818 ;; Set when we're on a label, so that we don't stop there.
10819 ;; FIXME: To be complete we should check if we're on a label
10820 ;; now at the start.
10821 on-label)
10823 ;; Use point as the anchor point for "namespace", "extern", etc.
10824 (apply 'c-add-syntax syntax-symbol
10825 (if (rassq syntax-symbol c-other-decl-block-key-in-symbols-alist)
10826 (point) nil)
10827 syntax-extra-args)
10829 ;; Loop while we have to back out of containing blocks.
10830 (while
10831 (and
10832 (catch 'back-up-block
10834 ;; Loop while we have to back up statements.
10835 (while (or (/= (point) boi)
10836 on-label
10837 (looking-at c-comment-start-regexp))
10839 ;; Skip past any comments that stands between the
10840 ;; statement start and boi.
10841 (let ((savepos (point)))
10842 (while (and (/= savepos boi)
10843 (c-backward-single-comment))
10844 (setq savepos (point)
10845 boi (c-point 'boi)))
10846 (goto-char savepos))
10848 ;; Skip to the beginning of this statement or backward
10849 ;; another one.
10850 (let ((old-pos (point))
10851 (old-boi boi)
10852 (step-type (c-beginning-of-statement-1 containing-sexp)))
10853 (setq boi (c-point 'boi)
10854 on-label (eq step-type 'label))
10856 (cond ((= (point) old-pos)
10857 ;; If we didn't move we're at the start of a block and
10858 ;; have to continue outside it.
10859 (throw 'back-up-block t))
10861 ((and (eq step-type 'up)
10862 (>= (point) old-boi)
10863 (looking-at "else\\>[^_]")
10864 (save-excursion
10865 (goto-char old-pos)
10866 (looking-at "if\\>[^_]")))
10867 ;; Special case to avoid deeper and deeper indentation
10868 ;; of "else if" clauses.
10871 ((and (not stop-at-boi-only)
10872 (/= old-pos old-boi)
10873 (memq step-type '(up previous)))
10874 ;; If stop-at-boi-only is nil, we shouldn't back up
10875 ;; over previous or containing statements to try to
10876 ;; reach boi, so go back to the last position and
10877 ;; exit.
10878 (goto-char old-pos)
10879 (throw 'back-up-block nil))
10882 (if (and (not stop-at-boi-only)
10883 (memq step-type '(up previous beginning)))
10884 ;; If we've moved into another statement then we
10885 ;; should no longer try to stop in the middle of a
10886 ;; line.
10887 (setq stop-at-boi-only t))
10889 ;; Record this as a substatement if we skipped up one
10890 ;; level.
10891 (when (eq step-type 'up)
10892 (c-add-syntax 'substatement nil))))
10895 containing-sexp)
10897 ;; Now we have to go out of this block.
10898 (goto-char containing-sexp)
10900 ;; Don't stop in the middle of a special brace list opener
10901 ;; like "({".
10902 (when c-special-brace-lists
10903 (let ((special-list (c-looking-at-special-brace-list)))
10904 (when (and special-list
10905 (< (car (car special-list)) (point)))
10906 (setq containing-sexp (car (car special-list)))
10907 (goto-char containing-sexp))))
10909 (setq paren-state (c-whack-state-after containing-sexp paren-state)
10910 containing-sexp (c-most-enclosing-brace paren-state)
10911 boi (c-point 'boi))
10913 ;; Analyze the construct in front of the block we've stepped out
10914 ;; from and add the right syntactic element for it.
10915 (let ((paren-pos (point))
10916 (paren-char (char-after))
10917 step-type)
10919 (if (eq paren-char ?\()
10920 ;; Stepped out of a parenthesis block, so we're in an
10921 ;; expression now.
10922 (progn
10923 (when (/= paren-pos boi)
10924 (if (and c-recognize-paren-inexpr-blocks
10925 (progn
10926 (c-backward-syntactic-ws containing-sexp)
10927 (or (not (looking-at "\\>"))
10928 (not (c-on-identifier))))
10929 (save-excursion
10930 (goto-char (1+ paren-pos))
10931 (c-forward-syntactic-ws)
10932 (eq (char-after) ?{)))
10933 ;; Stepped out of an in-expression statement. This
10934 ;; syntactic element won't get an anchor pos.
10935 (c-add-syntax 'inexpr-statement)
10937 ;; A parenthesis normally belongs to an arglist.
10938 (c-add-syntax 'arglist-cont-nonempty nil paren-pos)))
10940 (goto-char (max boi
10941 (if containing-sexp
10942 (1+ containing-sexp)
10943 (point-min))))
10944 (setq step-type 'same
10945 on-label nil))
10947 ;; Stepped out of a brace block.
10948 (setq step-type (c-beginning-of-statement-1 containing-sexp)
10949 on-label (eq step-type 'label))
10951 (if (and (eq step-type 'same)
10952 (/= paren-pos (point)))
10953 (let (inexpr)
10954 (cond
10955 ((save-excursion
10956 (goto-char paren-pos)
10957 (setq inexpr (c-looking-at-inexpr-block
10958 (c-safe-position containing-sexp paren-state)
10959 containing-sexp)))
10960 (c-add-syntax (if (eq (car inexpr) 'inlambda)
10961 'defun-block-intro
10962 'statement-block-intro)
10963 nil))
10964 ((looking-at c-other-decl-block-key)
10965 (c-add-syntax
10966 (cdr (assoc (match-string 1)
10967 c-other-decl-block-key-in-symbols-alist))
10968 (max (c-point 'boi paren-pos) (point))))
10969 (t (c-add-syntax 'defun-block-intro nil))))
10971 (c-add-syntax 'statement-block-intro nil)))
10973 (if (= paren-pos boi)
10974 ;; Always done if the open brace was at boi. The
10975 ;; c-beginning-of-statement-1 call above is necessary
10976 ;; anyway, to decide the type of block-intro to add.
10977 (goto-char paren-pos)
10978 (setq boi (c-point 'boi)))
10981 ;; Fill in the current point as the anchor for all the symbols
10982 ;; added above.
10983 (let ((p c-syntactic-context) q)
10984 (while (not (eq p syntax-last))
10985 (setq q (cdr (car p))) ; e.g. (nil 28) [from (arglist-cont-nonempty nil 28)]
10986 (while q
10987 (unless (car q)
10988 (setcar q (point)))
10989 (setq q (cdr q)))
10990 (setq p (cdr p))))
10993 (defun c-add-class-syntax (symbol
10994 containing-decl-open
10995 containing-decl-start
10996 containing-decl-kwd
10997 paren-state)
10998 ;; The inclass and class-close syntactic symbols are added in
10999 ;; several places and some work is needed to fix everything.
11000 ;; Therefore it's collected here.
11002 ;; This function might do hidden buffer changes.
11003 (goto-char containing-decl-open)
11004 (if (and (eq symbol 'inclass) (= (point) (c-point 'boi)))
11005 (progn
11006 (c-add-syntax symbol containing-decl-open)
11007 containing-decl-open)
11008 (goto-char containing-decl-start)
11009 ;; Ought to use `c-add-stmt-syntax' instead of backing up to boi
11010 ;; here, but we have to do like this for compatibility.
11011 (back-to-indentation)
11012 (c-add-syntax symbol (point))
11013 (if (and (c-keyword-member containing-decl-kwd
11014 'c-inexpr-class-kwds)
11015 (/= containing-decl-start (c-point 'boi containing-decl-start)))
11016 (c-add-syntax 'inexpr-class))
11017 (point)))
11019 (defun c-guess-continued-construct (indent-point
11020 char-after-ip
11021 beg-of-same-or-containing-stmt
11022 containing-sexp
11023 paren-state)
11024 ;; This function contains the decision tree reached through both
11025 ;; cases 18 and 10. It's a continued statement or top level
11026 ;; construct of some kind.
11028 ;; This function might do hidden buffer changes.
11030 (let (special-brace-list placeholder)
11031 (goto-char indent-point)
11032 (skip-chars-forward " \t")
11034 (cond
11035 ;; (CASE A removed.)
11036 ;; CASE B: open braces for class or brace-lists
11037 ((setq special-brace-list
11038 (or (and c-special-brace-lists
11039 (c-looking-at-special-brace-list))
11040 (eq char-after-ip ?{)))
11042 (cond
11043 ;; CASE B.1: class-open
11044 ((save-excursion
11045 (and (eq (char-after) ?{)
11046 (c-looking-at-decl-block containing-sexp t)
11047 (setq beg-of-same-or-containing-stmt (point))))
11048 (c-add-syntax 'class-open beg-of-same-or-containing-stmt))
11050 ;; CASE B.2: brace-list-open
11051 ((or (consp special-brace-list)
11052 (consp
11053 (c-looking-at-or-maybe-in-bracelist
11054 containing-sexp beg-of-same-or-containing-stmt))
11056 ;; The most semantically accurate symbol here is
11057 ;; brace-list-open, but we normally report it simply as a
11058 ;; statement-cont. The reason is that one normally adjusts
11059 ;; brace-list-open for brace lists as top-level constructs,
11060 ;; and brace lists inside statements is a completely different
11061 ;; context. C.f. case 5A.3.
11062 (c-beginning-of-statement-1 containing-sexp)
11063 (c-add-stmt-syntax (if c-auto-newline-analysis
11064 ;; Turn off the dwim above when we're
11065 ;; analyzing the nature of the brace
11066 ;; for the auto newline feature.
11067 'brace-list-open
11068 'statement-cont)
11069 nil nil
11070 containing-sexp paren-state))
11072 ;; CASE B.3: The body of a function declared inside a normal
11073 ;; block. Can occur e.g. in Pike and when using gcc
11074 ;; extensions, but watch out for macros followed by blocks.
11075 ;; C.f. cases E, 16F and 17G.
11076 ((and (not (c-at-statement-start-p))
11077 (eq (c-beginning-of-statement-1 containing-sexp nil nil t)
11078 'same)
11079 (save-excursion
11080 (let ((c-recognize-typeless-decls nil))
11081 ;; Turn off recognition of constructs that lacks a
11082 ;; type in this case, since that's more likely to be
11083 ;; a macro followed by a block.
11084 (c-forward-decl-or-cast-1 (c-point 'bosws) nil nil))))
11085 (c-add-stmt-syntax 'defun-open nil t
11086 containing-sexp paren-state))
11088 ;; CASE B.5: We have a C++11 "return \n { ..... }" Note that we're
11089 ;; not at the "{", currently.
11090 ((progn (goto-char indent-point)
11091 (backward-sexp)
11092 (looking-at c-return-key))
11093 (c-add-stmt-syntax 'statement-cont nil t
11094 containing-sexp paren-state))
11096 ;; CASE B.4: Continued statement with block open. The most
11097 ;; accurate analysis is perhaps `statement-cont' together with
11098 ;; `block-open' but we play DWIM and use `substatement-open'
11099 ;; instead. The rationale is that this typically is a macro
11100 ;; followed by a block which makes it very similar to a
11101 ;; statement with a substatement block.
11103 (c-add-stmt-syntax 'substatement-open nil nil
11104 containing-sexp paren-state))
11107 ;; CASE C: iostream insertion or extraction operator
11108 ((and (looking-at "\\(<<\\|>>\\)\\([^=]\\|$\\)")
11109 (save-excursion
11110 (goto-char beg-of-same-or-containing-stmt)
11111 ;; If there is no preceding streamop in the statement
11112 ;; then indent this line as a normal statement-cont.
11113 (when (c-syntactic-re-search-forward
11114 "\\(<<\\|>>\\)\\([^=]\\|$\\)" indent-point 'move t t)
11115 (c-add-syntax 'stream-op (c-point 'boi))
11116 t))))
11118 ;; CASE E: In the "K&R region" of a function declared inside a
11119 ;; normal block. C.f. case B.3.
11120 ((and (save-excursion
11121 ;; Check that the next token is a '{'. This works as
11122 ;; long as no language that allows nested function
11123 ;; definitions allows stuff like member init lists, K&R
11124 ;; declarations or throws clauses there.
11126 ;; Note that we do a forward search for something ahead
11127 ;; of the indentation line here. That's not good since
11128 ;; the user might not have typed it yet. Unfortunately
11129 ;; it's exceedingly tricky to recognize a function
11130 ;; prototype in a code block without resorting to this.
11131 (c-forward-syntactic-ws)
11132 (eq (char-after) ?{))
11133 (not (c-at-statement-start-p))
11134 (eq (c-beginning-of-statement-1 containing-sexp nil nil t)
11135 'same)
11136 (save-excursion
11137 (let ((c-recognize-typeless-decls nil))
11138 ;; Turn off recognition of constructs that lacks a
11139 ;; type in this case, since that's more likely to be
11140 ;; a macro followed by a block.
11141 (c-forward-decl-or-cast-1 (c-point 'bosws) nil nil))))
11142 (c-add-stmt-syntax 'func-decl-cont nil t
11143 containing-sexp paren-state))
11145 ;;CASE F: continued statement and the only preceding items are
11146 ;;annotations.
11147 ((and (c-major-mode-is 'java-mode)
11148 (setq placeholder (point))
11149 (c-beginning-of-statement-1)
11150 (progn
11151 (while (and (c-forward-annotation)
11152 (< (point) placeholder))
11153 (c-forward-syntactic-ws))
11155 (prog1
11156 (>= (point) placeholder)
11157 (goto-char placeholder)))
11158 (c-beginning-of-statement-1 containing-sexp)
11159 (c-add-syntax 'annotation-var-cont (point)))
11161 ;; CASE G: a template list continuation?
11162 ;; Mostly a duplication of case 5D.3 to fix templates-19:
11163 ((and (c-major-mode-is 'c++-mode)
11164 (save-excursion
11165 (goto-char indent-point)
11166 (c-with-syntax-table c++-template-syntax-table
11167 (setq placeholder (c-up-list-backward)))
11168 (and placeholder
11169 (eq (char-after placeholder) ?<)
11170 (/= (char-before placeholder) ?<)
11171 (progn
11172 (goto-char (1+ placeholder))
11173 (not (looking-at c-<-op-cont-regexp))))))
11174 (c-with-syntax-table c++-template-syntax-table
11175 (goto-char placeholder)
11176 (c-beginning-of-statement-1 containing-sexp t))
11177 (if (save-excursion
11178 (c-backward-syntactic-ws containing-sexp)
11179 (eq (char-before) ?<))
11180 ;; In a nested template arglist.
11181 (progn
11182 (goto-char placeholder)
11183 (c-syntactic-skip-backward "^,;" containing-sexp t)
11184 (c-forward-syntactic-ws))
11185 (back-to-indentation))
11186 ;; FIXME: Should use c-add-stmt-syntax, but it's not yet
11187 ;; template aware.
11188 (c-add-syntax 'template-args-cont (point) placeholder))
11190 ;; CASE D: continued statement.
11192 (c-beginning-of-statement-1 containing-sexp)
11193 (c-add-stmt-syntax 'statement-cont nil nil
11194 containing-sexp paren-state))
11197 ;; The next autoload was added by RMS on 2005/8/9 - don't know why (ACM,
11198 ;; 2005/11/29).
11199 ;;;###autoload
11200 (defun c-guess-basic-syntax ()
11201 "Return the syntactic context of the current line."
11202 (save-excursion
11203 (beginning-of-line)
11204 (c-save-buffer-state
11205 ((indent-point (point))
11206 (case-fold-search nil)
11207 ;; A whole ugly bunch of various temporary variables. Have
11208 ;; to declare them here since it's not possible to declare
11209 ;; a variable with only the scope of a cond test and the
11210 ;; following result clauses, and most of this function is a
11211 ;; single gigantic cond. :P
11212 literal char-before-ip before-ws-ip char-after-ip macro-start
11213 in-macro-expr c-syntactic-context placeholder c-in-literal-cache
11214 step-type tmpsymbol keyword injava-inher special-brace-list tmp-pos
11215 containing-<
11216 ;; The following record some positions for the containing
11217 ;; declaration block if we're directly within one:
11218 ;; `containing-decl-open' is the position of the open
11219 ;; brace. `containing-decl-start' is the start of the
11220 ;; declaration. `containing-decl-kwd' is the keyword
11221 ;; symbol of the keyword that tells what kind of block it
11222 ;; is.
11223 containing-decl-open
11224 containing-decl-start
11225 containing-decl-kwd
11226 ;; The open paren of the closest surrounding sexp or nil if
11227 ;; there is none.
11228 containing-sexp
11229 ;; The position after the closest preceding brace sexp
11230 ;; (nested sexps are ignored), or the position after
11231 ;; `containing-sexp' if there is none, or (point-min) if
11232 ;; `containing-sexp' is nil.
11234 ;; The paren state outside `containing-sexp', or at
11235 ;; `indent-point' if `containing-sexp' is nil.
11236 (paren-state (c-parse-state))
11237 ;; There's always at most one syntactic element which got
11238 ;; an anchor pos. It's stored in syntactic-relpos.
11239 syntactic-relpos
11240 (c-stmt-delim-chars c-stmt-delim-chars))
11242 ;; Check if we're directly inside an enclosing declaration
11243 ;; level block.
11244 (when (and (setq containing-sexp
11245 (c-most-enclosing-brace paren-state))
11246 (progn
11247 (goto-char containing-sexp)
11248 (eq (char-after) ?{))
11249 (setq placeholder
11250 (c-looking-at-decl-block
11251 (c-most-enclosing-brace paren-state
11252 containing-sexp)
11253 t)))
11254 (setq containing-decl-open containing-sexp
11255 containing-decl-start (point)
11256 containing-sexp nil)
11257 (goto-char placeholder)
11258 (setq containing-decl-kwd (and (looking-at c-keywords-regexp)
11259 (c-keyword-sym (match-string 1)))))
11261 ;; Init some position variables.
11262 (if paren-state
11263 (progn
11264 (setq containing-sexp (car paren-state)
11265 paren-state (cdr paren-state))
11266 (if (consp containing-sexp)
11267 (save-excursion
11268 (goto-char (cdr containing-sexp))
11269 (if (and (c-major-mode-is 'c++-mode)
11270 (c-back-over-member-initializer-braces))
11271 (c-syntactic-skip-backward "^}" nil t))
11272 (setq lim (point))
11273 (if paren-state
11274 ;; Ignore balanced paren. The next entry
11275 ;; can't be another one.
11276 (setq containing-sexp (car paren-state)
11277 paren-state (cdr paren-state))
11278 ;; If there is no surrounding open paren then
11279 ;; put the last balanced pair back on paren-state.
11280 (setq paren-state (cons containing-sexp paren-state)
11281 containing-sexp nil)))
11282 (setq lim (1+ containing-sexp))))
11283 (setq lim (point-min)))
11285 ;; If we're in a parenthesis list then ',' delimits the
11286 ;; "statements" rather than being an operator (with the
11287 ;; exception of the "for" clause). This difference is
11288 ;; typically only noticeable when statements are used in macro
11289 ;; arglists.
11290 (when (and containing-sexp
11291 (eq (char-after containing-sexp) ?\())
11292 (setq c-stmt-delim-chars c-stmt-delim-chars-with-comma))
11293 ;; cache char before and after indent point, and move point to
11294 ;; the most likely position to perform the majority of tests
11295 (goto-char indent-point)
11296 (c-backward-syntactic-ws lim)
11297 (setq before-ws-ip (point)
11298 char-before-ip (char-before))
11299 (goto-char indent-point)
11300 (skip-chars-forward " \t")
11301 (setq char-after-ip (char-after))
11303 ;; are we in a literal?
11304 (setq literal (c-in-literal lim))
11306 ;; now figure out syntactic qualities of the current line
11307 (cond
11309 ;; CASE 1: in a string.
11310 ((eq literal 'string)
11311 (c-add-syntax 'string (c-point 'bopl)))
11313 ;; CASE 2: in a C or C++ style comment.
11314 ((and (memq literal '(c c++))
11315 ;; This is a kludge for XEmacs where we use
11316 ;; `buffer-syntactic-context', which doesn't correctly
11317 ;; recognize "\*/" to end a block comment.
11318 ;; `parse-partial-sexp' which is used by
11319 ;; `c-literal-limits' will however do that in most
11320 ;; versions, which results in that we get nil from
11321 ;; `c-literal-limits' even when `c-in-literal' claims
11322 ;; we're inside a comment.
11323 (setq placeholder (c-literal-start lim)))
11324 (c-add-syntax literal placeholder))
11326 ;; CASE 3: in a cpp preprocessor macro continuation.
11327 ((and (save-excursion
11328 (when (c-beginning-of-macro)
11329 (setq macro-start (point))))
11330 (/= macro-start (c-point 'boi))
11331 (progn
11332 (setq tmpsymbol 'cpp-macro-cont)
11333 (or (not c-syntactic-indentation-in-macros)
11334 (save-excursion
11335 (goto-char macro-start)
11336 ;; If at the beginning of the body of a #define
11337 ;; directive then analyze as cpp-define-intro
11338 ;; only. Go on with the syntactic analysis
11339 ;; otherwise. in-macro-expr is set if we're in a
11340 ;; cpp expression, i.e. before the #define body
11341 ;; or anywhere in a non-#define directive.
11342 (if (c-forward-to-cpp-define-body)
11343 (let ((indent-boi (c-point 'boi indent-point)))
11344 (setq in-macro-expr (> (point) indent-boi)
11345 tmpsymbol 'cpp-define-intro)
11346 (= (point) indent-boi))
11347 (setq in-macro-expr t)
11348 nil)))))
11349 (c-add-syntax tmpsymbol macro-start)
11350 (setq macro-start nil))
11352 ;; CASE 11: an else clause?
11353 ((looking-at "else\\>[^_]")
11354 (c-beginning-of-statement-1 containing-sexp)
11355 (c-add-stmt-syntax 'else-clause nil t
11356 containing-sexp paren-state))
11358 ;; CASE 12: while closure of a do/while construct?
11359 ((and (looking-at "while\\>[^_]")
11360 (save-excursion
11361 (prog1 (eq (c-beginning-of-statement-1 containing-sexp)
11362 'beginning)
11363 (setq placeholder (point)))))
11364 (goto-char placeholder)
11365 (c-add-stmt-syntax 'do-while-closure nil t
11366 containing-sexp paren-state))
11368 ;; CASE 13: A catch or finally clause? This case is simpler
11369 ;; than if-else and do-while, because a block is required
11370 ;; after every try, catch and finally.
11371 ((save-excursion
11372 (and (cond ((c-major-mode-is 'c++-mode)
11373 (looking-at "catch\\>[^_]"))
11374 ((c-major-mode-is 'java-mode)
11375 (looking-at "\\(catch\\|finally\\)\\>[^_]")))
11376 (and (c-safe (c-backward-syntactic-ws)
11377 (c-backward-sexp)
11379 (eq (char-after) ?{)
11380 (c-safe (c-backward-syntactic-ws)
11381 (c-backward-sexp)
11383 (if (eq (char-after) ?\()
11384 (c-safe (c-backward-sexp) t)
11386 (looking-at "\\(try\\|catch\\)\\>[^_]")
11387 (setq placeholder (point))))
11388 (goto-char placeholder)
11389 (c-add-stmt-syntax 'catch-clause nil t
11390 containing-sexp paren-state))
11392 ;; CASE 18: A substatement we can recognize by keyword.
11393 ((save-excursion
11394 (and c-opt-block-stmt-key
11395 (not (eq char-before-ip ?\;))
11396 (not (c-at-vsemi-p before-ws-ip))
11397 (not (memq char-after-ip '(?\) ?\] ?,)))
11398 (or (not (eq char-before-ip ?}))
11399 (c-looking-at-inexpr-block-backward c-state-cache))
11400 (> (point)
11401 (progn
11402 ;; Ought to cache the result from the
11403 ;; c-beginning-of-statement-1 calls here.
11404 (setq placeholder (point))
11405 (while (eq (setq step-type
11406 (c-beginning-of-statement-1 lim))
11407 'label))
11408 (if (eq step-type 'previous)
11409 (goto-char placeholder)
11410 (setq placeholder (point))
11411 (if (and (eq step-type 'same)
11412 (not (looking-at c-opt-block-stmt-key)))
11413 ;; Step up to the containing statement if we
11414 ;; stayed in the same one.
11415 (let (step)
11416 (while (eq
11417 (setq step
11418 (c-beginning-of-statement-1 lim))
11419 'label))
11420 (if (eq step 'up)
11421 (setq placeholder (point))
11422 ;; There was no containing statement after all.
11423 (goto-char placeholder)))))
11424 placeholder))
11425 (if (looking-at c-block-stmt-2-key)
11426 ;; Require a parenthesis after these keywords.
11427 ;; Necessary to catch e.g. synchronized in Java,
11428 ;; which can be used both as statement and
11429 ;; modifier.
11430 (and (zerop (c-forward-token-2 1 nil))
11431 (eq (char-after) ?\())
11432 (looking-at c-opt-block-stmt-key))))
11434 (if (eq step-type 'up)
11435 ;; CASE 18A: Simple substatement.
11436 (progn
11437 (goto-char placeholder)
11438 (cond
11439 ((eq char-after-ip ?{)
11440 (c-add-stmt-syntax 'substatement-open nil nil
11441 containing-sexp paren-state))
11442 ((save-excursion
11443 (goto-char indent-point)
11444 (back-to-indentation)
11445 (c-forward-label))
11446 (c-add-stmt-syntax 'substatement-label nil nil
11447 containing-sexp paren-state))
11449 (c-add-stmt-syntax 'substatement nil nil
11450 containing-sexp paren-state))))
11452 ;; CASE 18B: Some other substatement. This is shared
11453 ;; with case 10.
11454 (c-guess-continued-construct indent-point
11455 char-after-ip
11456 placeholder
11458 paren-state)))
11460 ;; CASE 14: A case or default label
11461 ((save-excursion
11462 (and (looking-at c-label-kwds-regexp)
11463 (or (c-major-mode-is 'idl-mode)
11464 (and
11465 containing-sexp
11466 (goto-char containing-sexp)
11467 (eq (char-after) ?{)
11468 (progn (c-backward-syntactic-ws) t)
11469 (eq (char-before) ?\))
11470 (c-go-list-backward)
11471 (progn (c-backward-syntactic-ws) t)
11472 (c-simple-skip-symbol-backward)
11473 (looking-at c-block-stmt-2-key)))))
11474 (if containing-sexp
11475 (progn
11476 (goto-char containing-sexp)
11477 (setq lim (c-most-enclosing-brace c-state-cache
11478 containing-sexp))
11479 (c-backward-to-block-anchor lim)
11480 (c-add-stmt-syntax 'case-label nil t lim paren-state))
11481 ;; Got a bogus label at the top level. In lack of better
11482 ;; alternatives, anchor it on (point-min).
11483 (c-add-syntax 'case-label (point-min))))
11485 ;; CASE 15: any other label
11486 ((save-excursion
11487 (back-to-indentation)
11488 (and (not (looking-at c-syntactic-ws-start))
11489 (not (looking-at c-label-kwds-regexp))
11490 (c-forward-label)))
11491 (cond (containing-decl-open
11492 (setq placeholder (c-add-class-syntax 'inclass
11493 containing-decl-open
11494 containing-decl-start
11495 containing-decl-kwd
11496 paren-state))
11497 ;; Append access-label with the same anchor point as
11498 ;; inclass gets.
11499 (c-append-syntax 'access-label placeholder))
11501 (containing-sexp
11502 (goto-char containing-sexp)
11503 (setq lim (c-most-enclosing-brace c-state-cache
11504 containing-sexp))
11505 (save-excursion
11506 (setq tmpsymbol
11507 (if (and (eq (c-beginning-of-statement-1 lim) 'up)
11508 (looking-at "switch\\>[^_]"))
11509 ;; If the surrounding statement is a switch then
11510 ;; let's analyze all labels as switch labels, so
11511 ;; that they get lined up consistently.
11512 'case-label
11513 'label)))
11514 (c-backward-to-block-anchor lim)
11515 (c-add-stmt-syntax tmpsymbol nil t lim paren-state))
11518 ;; A label on the top level. Treat it as a class
11519 ;; context. (point-min) is the closest we get to the
11520 ;; class open brace.
11521 (c-add-syntax 'access-label (point-min)))))
11523 ;; CASE 4: In-expression statement. C.f. cases 7B, 16A and
11524 ;; 17E.
11525 ((setq placeholder (c-looking-at-inexpr-block
11526 (c-safe-position containing-sexp paren-state)
11527 containing-sexp
11528 ;; Have to turn on the heuristics after
11529 ;; the point even though it doesn't work
11530 ;; very well. C.f. test case class-16.pike.
11532 (setq tmpsymbol (assq (car placeholder)
11533 '((inexpr-class . class-open)
11534 (inexpr-statement . block-open))))
11535 (if tmpsymbol
11536 ;; It's a statement block or an anonymous class.
11537 (setq tmpsymbol (cdr tmpsymbol))
11538 ;; It's a Pike lambda. Check whether we are between the
11539 ;; lambda keyword and the argument list or at the defun
11540 ;; opener.
11541 (setq tmpsymbol (if (eq char-after-ip ?{)
11542 'inline-open
11543 'lambda-intro-cont)))
11544 (goto-char (cdr placeholder))
11545 (back-to-indentation)
11546 (c-add-stmt-syntax tmpsymbol nil t
11547 (c-most-enclosing-brace c-state-cache (point))
11548 paren-state)
11549 (unless (eq (point) (cdr placeholder))
11550 (c-add-syntax (car placeholder))))
11552 ;; CASE 5: Line is inside a declaration level block or at top level.
11553 ((or containing-decl-open (null containing-sexp))
11554 (cond
11556 ;; CASE 5A: we are looking at a defun, brace list, class,
11557 ;; or inline-inclass method opening brace
11558 ((setq special-brace-list
11559 (or (and c-special-brace-lists
11560 (c-looking-at-special-brace-list))
11561 (eq char-after-ip ?{)))
11562 (cond
11564 ;; CASE 5A.1: Non-class declaration block open.
11565 ((save-excursion
11566 (let (tmp)
11567 (and (eq char-after-ip ?{)
11568 (setq tmp (c-looking-at-decl-block containing-sexp t))
11569 (progn
11570 (setq placeholder (point))
11571 (goto-char tmp)
11572 (looking-at c-symbol-key))
11573 (c-keyword-member
11574 (c-keyword-sym (setq keyword (match-string 0)))
11575 'c-other-block-decl-kwds))))
11576 (goto-char placeholder)
11577 (c-add-stmt-syntax
11578 (if (string-equal keyword "extern")
11579 ;; Special case for extern-lang-open.
11580 'extern-lang-open
11581 (intern (concat keyword "-open")))
11582 nil t containing-sexp paren-state))
11584 ;; CASE 5A.2: we are looking at a class opening brace
11585 ((save-excursion
11586 (goto-char indent-point)
11587 (skip-chars-forward " \t")
11588 (and (eq (char-after) ?{)
11589 (c-looking-at-decl-block containing-sexp t)
11590 (setq placeholder (point))))
11591 (c-add-syntax 'class-open placeholder))
11593 ;; CASE 5A.3: brace list open
11594 ((save-excursion
11595 (goto-char indent-point)
11596 (skip-chars-forward " \t")
11597 (cond
11598 ((c-backward-over-enum-header)
11599 (setq placeholder (c-point 'boi)))
11600 ((consp (setq placeholder
11601 (c-looking-at-or-maybe-in-bracelist
11602 containing-sexp lim)))
11603 (setq tmpsymbol (and (cdr placeholder) 'topmost-intro-cont))
11604 (setq placeholder (c-point 'boi (car placeholder))))))
11605 (if (and (not c-auto-newline-analysis)
11606 ;(c-major-mode-is 'java-mode) ; Not needed anymore (2016-08-30).
11607 (eq tmpsymbol 'topmost-intro-cont))
11608 ;; We're in Java and have found that the open brace
11609 ;; belongs to a "new Foo[]" initialization list,
11610 ;; which means the brace list is part of an
11611 ;; expression and not a top level definition. We
11612 ;; therefore treat it as any topmost continuation
11613 ;; even though the semantically correct symbol still
11614 ;; is brace-list-open, on the same grounds as in
11615 ;; case B.2.
11616 (progn
11617 (c-beginning-of-statement-1 lim)
11618 (c-add-syntax 'topmost-intro-cont (c-point 'boi)))
11619 (c-add-syntax 'brace-list-open placeholder)))
11621 ;; CASE 5A.4: inline defun open
11622 ((and containing-decl-open
11623 (not (c-keyword-member containing-decl-kwd
11624 'c-other-block-decl-kwds)))
11625 (c-add-syntax 'inline-open)
11626 (c-add-class-syntax 'inclass
11627 containing-decl-open
11628 containing-decl-start
11629 containing-decl-kwd
11630 paren-state))
11632 ;; CASE 5A.5: ordinary defun open
11634 (save-excursion
11635 (c-beginning-of-decl-1 lim)
11636 (while (cond
11637 ((looking-at c-specifier-key)
11638 (c-forward-keyword-clause 1))
11639 ((and c-opt-cpp-prefix
11640 (looking-at c-noise-macro-with-parens-name-re))
11641 (c-forward-noise-clause))))
11642 (c-add-syntax 'defun-open (c-point 'boi))
11643 ;; Bogus to use bol here, but it's the legacy. (Resolved,
11644 ;; 2007-11-09)
11645 ))))
11647 ;; CASE 5R: Member init list. (Used to be part of CASE 5B.1)
11648 ;; Note there is no limit on the backward search here, since member
11649 ;; init lists can, in practice, be very large.
11650 ((save-excursion
11651 (when (and (c-major-mode-is 'c++-mode)
11652 (setq placeholder (c-back-over-member-initializers)))
11653 (setq tmp-pos (point))))
11654 (if (= (c-point 'bosws) (1+ tmp-pos))
11655 (progn
11656 ;; There is no preceding member init clause.
11657 ;; Indent relative to the beginning of indentation
11658 ;; for the topmost-intro line that contains the
11659 ;; prototype's open paren.
11660 (goto-char placeholder)
11661 (c-add-syntax 'member-init-intro (c-point 'boi)))
11662 ;; Indent relative to the first member init clause.
11663 (goto-char (1+ tmp-pos))
11664 (c-forward-syntactic-ws)
11665 (c-add-syntax 'member-init-cont (point))))
11667 ;; CASE 5B: After a function header but before the body (or
11668 ;; the ending semicolon if there's no body).
11669 ((save-excursion
11670 (when (setq placeholder (c-just-after-func-arglist-p
11671 (max lim (c-determine-limit 500))))
11672 (setq tmp-pos (point))))
11673 (cond
11675 ;; CASE 5B.1: Member init list.
11676 ((eq (char-after tmp-pos) ?:)
11677 ;; There is no preceding member init clause.
11678 ;; Indent relative to the beginning of indentation
11679 ;; for the topmost-intro line that contains the
11680 ;; prototype's open paren.
11681 (goto-char placeholder)
11682 (c-add-syntax 'member-init-intro (c-point 'boi)))
11684 ;; CASE 5B.2: K&R arg decl intro
11685 ((and c-recognize-knr-p
11686 (c-in-knr-argdecl lim))
11687 (c-beginning-of-statement-1 lim)
11688 (c-add-syntax 'knr-argdecl-intro (c-point 'boi))
11689 (if containing-decl-open
11690 (c-add-class-syntax 'inclass
11691 containing-decl-open
11692 containing-decl-start
11693 containing-decl-kwd
11694 paren-state)))
11696 ;; CASE 5B.4: Nether region after a C++ or Java func
11697 ;; decl, which could include a `throws' declaration.
11699 (c-beginning-of-statement-1 lim)
11700 (c-add-syntax 'func-decl-cont (c-point 'boi))
11703 ;; CASE 5C: inheritance line. could be first inheritance
11704 ;; line, or continuation of a multiple inheritance
11705 ((or (and (c-major-mode-is 'c++-mode)
11706 (progn
11707 (when (eq char-after-ip ?,)
11708 (skip-chars-forward " \t")
11709 (forward-char))
11710 (looking-at c-opt-postfix-decl-spec-key)))
11711 (and (or (eq char-before-ip ?:)
11712 ;; watch out for scope operator
11713 (save-excursion
11714 (and (eq char-after-ip ?:)
11715 (c-safe (forward-char 1) t)
11716 (not (eq (char-after) ?:))
11718 (save-excursion
11719 (c-beginning-of-statement-1 lim)
11720 (when (looking-at c-opt-<>-sexp-key)
11721 (goto-char (match-end 1))
11722 (c-forward-syntactic-ws)
11723 (c-forward-<>-arglist nil)
11724 (c-forward-syntactic-ws))
11725 (looking-at c-class-key)))
11726 ;; for Java
11727 (and (c-major-mode-is 'java-mode)
11728 (let ((fence (save-excursion
11729 (c-beginning-of-statement-1 lim)
11730 (point)))
11731 cont done)
11732 (save-excursion
11733 (while (not done)
11734 (cond ((looking-at c-opt-postfix-decl-spec-key)
11735 (setq injava-inher (cons cont (point))
11736 done t))
11737 ((or (not (c-safe (c-forward-sexp -1) t))
11738 (<= (point) fence))
11739 (setq done t))
11741 (setq cont t)))
11742 injava-inher)
11743 (not (c-crosses-statement-barrier-p (cdr injava-inher)
11744 (point)))
11746 (cond
11748 ;; CASE 5C.1: non-hanging colon on an inher intro
11749 ((eq char-after-ip ?:)
11750 (c-beginning-of-statement-1 lim)
11751 (c-add-syntax 'inher-intro (c-point 'boi))
11752 ;; don't add inclass symbol since relative point already
11753 ;; contains any class offset
11756 ;; CASE 5C.2: hanging colon on an inher intro
11757 ((eq char-before-ip ?:)
11758 (c-beginning-of-statement-1 lim)
11759 (c-add-syntax 'inher-intro (c-point 'boi))
11760 (if containing-decl-open
11761 (c-add-class-syntax 'inclass
11762 containing-decl-open
11763 containing-decl-start
11764 containing-decl-kwd
11765 paren-state)))
11767 ;; CASE 5C.3: in a Java implements/extends
11768 (injava-inher
11769 (let ((where (cdr injava-inher))
11770 (cont (car injava-inher)))
11771 (goto-char where)
11772 (cond ((looking-at "throws\\>[^_]")
11773 (c-add-syntax 'func-decl-cont
11774 (progn (c-beginning-of-statement-1 lim)
11775 (c-point 'boi))))
11776 (cont (c-add-syntax 'inher-cont where))
11777 (t (c-add-syntax 'inher-intro
11778 (progn (goto-char (cdr injava-inher))
11779 (c-beginning-of-statement-1 lim)
11780 (point))))
11783 ;; CASE 5C.4: a continued inheritance line
11785 (c-beginning-of-inheritance-list lim)
11786 (c-add-syntax 'inher-cont (point))
11787 ;; don't add inclass symbol since relative point already
11788 ;; contains any class offset
11791 ;; CASE 5P: AWK pattern or function or continuation
11792 ;; thereof.
11793 ((c-major-mode-is 'awk-mode)
11794 (setq placeholder (point))
11795 (c-add-stmt-syntax
11796 (if (and (eq (c-beginning-of-statement-1) 'same)
11797 (/= (point) placeholder))
11798 'topmost-intro-cont
11799 'topmost-intro)
11800 nil nil
11801 containing-sexp paren-state))
11803 ;; CASE 5D: this could be a top-level initialization, a
11804 ;; member init list continuation, or a template argument
11805 ;; list continuation.
11806 ((save-excursion
11807 ;; Note: We use the fact that lim is always after any
11808 ;; preceding brace sexp.
11809 (if c-recognize-<>-arglists
11810 (while (and
11811 (progn
11812 (c-syntactic-skip-backward "^;,=<>" lim t)
11813 (> (point) lim))
11815 (when c-overloadable-operators-regexp
11816 (when (setq placeholder (c-after-special-operator-id lim))
11817 (goto-char placeholder)
11819 (cond
11820 ((eq (char-before) ?>)
11821 (or (c-backward-<>-arglist nil lim)
11822 (backward-char))
11824 ((eq (char-before) ?<)
11825 (backward-char)
11826 (if (save-excursion
11827 (c-forward-<>-arglist nil))
11828 (progn (forward-char)
11829 nil)
11831 (t nil)))))
11832 ;; NB: No c-after-special-operator-id stuff in this
11833 ;; clause - we assume only C++ needs it.
11834 (c-syntactic-skip-backward "^;,=" lim t))
11835 (memq (char-before) '(?, ?= ?<)))
11836 (cond
11838 ;; CASE 5D.3: perhaps a template list continuation?
11839 ((and (c-major-mode-is 'c++-mode)
11840 (save-excursion
11841 (save-restriction
11842 (c-with-syntax-table c++-template-syntax-table
11843 (goto-char indent-point)
11844 (setq placeholder (c-up-list-backward))
11845 (and placeholder
11846 (eq (char-after placeholder) ?<))))))
11847 (c-with-syntax-table c++-template-syntax-table
11848 (goto-char placeholder)
11849 (c-beginning-of-statement-1 lim t))
11850 (if (save-excursion
11851 (c-backward-syntactic-ws lim)
11852 (eq (char-before) ?<))
11853 ;; In a nested template arglist.
11854 (progn
11855 (goto-char placeholder)
11856 (c-syntactic-skip-backward "^,;" lim t)
11857 (c-forward-syntactic-ws))
11858 (back-to-indentation))
11859 ;; FIXME: Should use c-add-stmt-syntax, but it's not yet
11860 ;; template aware.
11861 (c-add-syntax 'template-args-cont (point) placeholder))
11863 ;; CASE 5D.4: perhaps a multiple inheritance line?
11864 ((and (c-major-mode-is 'c++-mode)
11865 (save-excursion
11866 (c-beginning-of-statement-1 lim)
11867 (setq placeholder (point))
11868 (if (looking-at "static\\>[^_]")
11869 (c-forward-token-2 1 nil indent-point))
11870 (and (looking-at c-class-key)
11871 (zerop (c-forward-token-2 2 nil indent-point))
11872 (if (eq (char-after) ?<)
11873 (c-with-syntax-table c++-template-syntax-table
11874 (zerop (c-forward-token-2 1 t indent-point)))
11876 (eq (char-after) ?:))))
11877 (goto-char placeholder)
11878 (c-add-syntax 'inher-cont (c-point 'boi)))
11880 ;; CASE 5D.5: Continuation of the "expression part" of a
11881 ;; top level construct. Or, perhaps, an unrecognized construct.
11883 (while (and (setq placeholder (point))
11884 (eq (car (c-beginning-of-decl-1 containing-sexp)) ; Can't use `lim' here.
11885 'same)
11886 (save-excursion
11887 (c-backward-syntactic-ws)
11888 (eq (char-before) ?}))
11889 (< (point) placeholder)))
11890 (c-add-stmt-syntax
11891 (cond
11892 ((eq (point) placeholder) 'statement) ; unrecognized construct
11893 ;; A preceding comma at the top level means that a
11894 ;; new variable declaration starts here. Use
11895 ;; topmost-intro-cont for it, for consistency with
11896 ;; the first variable declaration. C.f. case 5N.
11897 ((eq char-before-ip ?,) 'topmost-intro-cont)
11898 (t 'statement-cont))
11899 nil nil containing-sexp paren-state))
11902 ;; CASE 5F: Close of a non-class declaration level block.
11903 ((and (eq char-after-ip ?})
11904 (c-keyword-member containing-decl-kwd
11905 'c-other-block-decl-kwds))
11906 ;; This is inconsistent: Should use `containing-decl-open'
11907 ;; here if it's at boi, like in case 5J.
11908 (goto-char containing-decl-start)
11909 (c-add-stmt-syntax
11910 (if (string-equal (symbol-name containing-decl-kwd) "extern")
11911 ;; Special case for compatibility with the
11912 ;; extern-lang syntactic symbols.
11913 'extern-lang-close
11914 (intern (concat (symbol-name containing-decl-kwd)
11915 "-close")))
11916 nil t
11917 (c-most-enclosing-brace paren-state (point))
11918 paren-state))
11920 ;; CASE 5G: we are looking at the brace which closes the
11921 ;; enclosing nested class decl
11922 ((and containing-sexp
11923 (eq char-after-ip ?})
11924 (eq containing-decl-open containing-sexp))
11925 (c-add-class-syntax 'class-close
11926 containing-decl-open
11927 containing-decl-start
11928 containing-decl-kwd
11929 paren-state))
11931 ;; CASE 5H: we could be looking at subsequent knr-argdecls
11932 ((and c-recognize-knr-p
11933 (not containing-sexp) ; can't be knr inside braces.
11934 (not (eq char-before-ip ?}))
11935 (save-excursion
11936 (setq placeholder (cdr (c-beginning-of-decl-1 lim)))
11937 (and placeholder
11938 ;; Do an extra check to avoid tripping up on
11939 ;; statements that occur in invalid contexts
11940 ;; (e.g. in macro bodies where we don't really
11941 ;; know the context of what we're looking at).
11942 (not (and c-opt-block-stmt-key
11943 (looking-at c-opt-block-stmt-key)))))
11944 (< placeholder indent-point))
11945 (goto-char placeholder)
11946 (c-add-syntax 'knr-argdecl (point)))
11948 ;; CASE 5I: ObjC method definition.
11949 ((and c-opt-method-key
11950 (looking-at c-opt-method-key))
11951 (c-beginning-of-statement-1 nil t)
11952 (if (= (point) indent-point)
11953 ;; Handle the case when it's the first (non-comment)
11954 ;; thing in the buffer. Can't look for a 'same return
11955 ;; value from cbos1 since ObjC directives currently
11956 ;; aren't recognized fully, so that we get 'same
11957 ;; instead of 'previous if it moved over a preceding
11958 ;; directive.
11959 (goto-char (point-min)))
11960 (c-add-syntax 'objc-method-intro (c-point 'boi)))
11962 ;; CASE 5N: At a variable declaration that follows a class
11963 ;; definition or some other block declaration that doesn't
11964 ;; end at the closing '}'. C.f. case 5D.5.
11965 ((progn
11966 (c-backward-syntactic-ws lim)
11967 (and (eq (char-before) ?})
11968 (save-excursion
11969 (let ((start (point)))
11970 (if (and c-state-cache
11971 (consp (car c-state-cache))
11972 (eq (cdar c-state-cache) (point)))
11973 ;; Speed up the backward search a bit.
11974 (goto-char (caar c-state-cache)))
11975 (c-beginning-of-decl-1 containing-sexp) ; Can't use `lim' here.
11976 (setq placeholder (point))
11977 (if (= start (point))
11978 ;; The '}' is unbalanced.
11980 (c-end-of-decl-1)
11981 (>= (point) indent-point))))))
11982 (goto-char placeholder)
11983 (c-add-stmt-syntax 'topmost-intro-cont nil nil
11984 containing-sexp paren-state))
11986 ;; NOTE: The point is at the end of the previous token here.
11988 ;; CASE 5J: we are at the topmost level, make
11989 ;; sure we skip back past any access specifiers
11990 ((and
11991 ;; A macro continuation line is never at top level.
11992 (not (and macro-start
11993 (> indent-point macro-start)))
11994 (save-excursion
11995 (setq placeholder (point))
11996 (or (memq char-before-ip '(?\; ?{ ?} nil))
11997 (c-at-vsemi-p before-ws-ip)
11998 (when (and (eq char-before-ip ?:)
11999 (eq (c-beginning-of-statement-1 lim)
12000 'label))
12001 (c-backward-syntactic-ws lim)
12002 (setq placeholder (point)))
12003 (and (c-major-mode-is 'objc-mode)
12004 (catch 'not-in-directive
12005 (c-beginning-of-statement-1 lim)
12006 (setq placeholder (point))
12007 (while (and (c-forward-objc-directive)
12008 (< (point) indent-point))
12009 (c-forward-syntactic-ws)
12010 (if (>= (point) indent-point)
12011 (throw 'not-in-directive t))
12012 (setq placeholder (point)))
12013 nil)))))
12014 ;; For historic reasons we anchor at bol of the last
12015 ;; line of the previous declaration. That's clearly
12016 ;; highly bogus and useless, and it makes our lives hard
12017 ;; to remain compatible. :P
12018 (goto-char placeholder)
12019 (c-add-syntax 'topmost-intro (c-point 'bol))
12020 (if containing-decl-open
12021 (if (c-keyword-member containing-decl-kwd
12022 'c-other-block-decl-kwds)
12023 (progn
12024 (goto-char (c-brace-anchor-point containing-decl-open))
12025 (c-add-stmt-syntax
12026 (if (string-equal (symbol-name containing-decl-kwd)
12027 "extern")
12028 ;; Special case for compatibility with the
12029 ;; extern-lang syntactic symbols.
12030 'inextern-lang
12031 (intern (concat "in"
12032 (symbol-name containing-decl-kwd))))
12033 nil t
12034 (c-most-enclosing-brace paren-state (point))
12035 paren-state))
12036 (c-add-class-syntax 'inclass
12037 containing-decl-open
12038 containing-decl-start
12039 containing-decl-kwd
12040 paren-state)))
12041 (when (and c-syntactic-indentation-in-macros
12042 macro-start
12043 (/= macro-start (c-point 'boi indent-point)))
12044 (c-add-syntax 'cpp-define-intro)
12045 (setq macro-start nil)))
12047 ;; CASE 5K: we are at an ObjC method definition
12048 ;; continuation line.
12049 ((and c-opt-method-key
12050 (save-excursion
12051 (c-beginning-of-statement-1 lim)
12052 (beginning-of-line)
12053 (when (looking-at c-opt-method-key)
12054 (setq placeholder (point)))))
12055 (c-add-syntax 'objc-method-args-cont placeholder))
12057 ;; CASE 5L: we are at the first argument of a template
12058 ;; arglist that begins on the previous line.
12059 ((and c-recognize-<>-arglists
12060 (eq (char-before) ?<)
12061 (not (and c-overloadable-operators-regexp
12062 (c-after-special-operator-id lim))))
12063 (c-beginning-of-statement-1 (c-safe-position (point) paren-state))
12064 (c-add-syntax 'template-args-cont (c-point 'boi)))
12066 ;; CASE 5Q: we are at a statement within a macro.
12067 (macro-start
12068 (c-beginning-of-statement-1 containing-sexp)
12069 (c-add-stmt-syntax 'statement nil t containing-sexp paren-state))
12071 ;;CASE 5N: We are at a topmost continuation line and the only
12072 ;;preceding items are annotations.
12073 ((and (c-major-mode-is 'java-mode)
12074 (setq placeholder (point))
12075 (c-beginning-of-statement-1)
12076 (progn
12077 (while (and (c-forward-annotation))
12078 (c-forward-syntactic-ws))
12080 (prog1
12081 (>= (point) placeholder)
12082 (goto-char placeholder)))
12083 (c-add-syntax 'annotation-top-cont (c-point 'boi)))
12085 ;; CASE 5M: we are at a topmost continuation line
12087 (c-beginning-of-statement-1 (c-safe-position (point) paren-state))
12088 (when (c-major-mode-is 'objc-mode)
12089 (setq placeholder (point))
12090 (while (and (c-forward-objc-directive)
12091 (< (point) indent-point))
12092 (c-forward-syntactic-ws)
12093 (setq placeholder (point)))
12094 (goto-char placeholder))
12095 (c-add-syntax 'topmost-intro-cont (c-point 'boi)))
12098 ;; (CASE 6 has been removed.)
12100 ;; CASE 7: line is an expression, not a statement. Most
12101 ;; likely we are either in a function prototype or a function
12102 ;; call argument list
12103 ((not (or (and c-special-brace-lists
12104 (save-excursion
12105 (goto-char containing-sexp)
12106 (c-looking-at-special-brace-list)))
12107 (eq (char-after containing-sexp) ?{)))
12108 (cond
12110 ;; CASE 7A: we are looking at the arglist closing paren.
12111 ;; C.f. case 7F.
12112 ((memq char-after-ip '(?\) ?\]))
12113 (goto-char containing-sexp)
12114 (setq placeholder (c-point 'boi))
12115 (if (and (c-safe (backward-up-list 1) t)
12116 (>= (point) placeholder))
12117 (progn
12118 (forward-char)
12119 (skip-chars-forward " \t"))
12120 (goto-char placeholder))
12121 (c-add-stmt-syntax 'arglist-close (list containing-sexp) t
12122 (c-most-enclosing-brace paren-state (point))
12123 paren-state))
12125 ;; CASE 7B: Looking at the opening brace of an
12126 ;; in-expression block or brace list. C.f. cases 4, 16A
12127 ;; and 17E.
12128 ((and (eq char-after-ip ?{)
12129 (progn
12130 (setq placeholder (c-inside-bracelist-p (point)
12131 paren-state))
12132 (if placeholder
12133 (setq tmpsymbol '(brace-list-open . inexpr-class))
12134 (setq tmpsymbol '(block-open . inexpr-statement)
12135 placeholder
12136 (cdr-safe (c-looking-at-inexpr-block
12137 (c-safe-position containing-sexp
12138 paren-state)
12139 containing-sexp)))
12140 ;; placeholder is nil if it's a block directly in
12141 ;; a function arglist. That makes us skip out of
12142 ;; this case.
12144 (goto-char placeholder)
12145 (back-to-indentation)
12146 (c-add-stmt-syntax (car tmpsymbol) nil t
12147 (c-most-enclosing-brace paren-state (point))
12148 paren-state)
12149 (if (/= (point) placeholder)
12150 (c-add-syntax (cdr tmpsymbol))))
12152 ;; CASE 7C: we are looking at the first argument in an empty
12153 ;; argument list. Use arglist-close if we're actually
12154 ;; looking at a close paren or bracket.
12155 ((memq char-before-ip '(?\( ?\[))
12156 (goto-char containing-sexp)
12157 (setq placeholder (c-point 'boi))
12158 (if (and (c-safe (backward-up-list 1) t)
12159 (>= (point) placeholder))
12160 (progn
12161 (forward-char)
12162 (skip-chars-forward " \t"))
12163 (goto-char placeholder))
12164 (c-add-stmt-syntax 'arglist-intro (list containing-sexp) t
12165 (c-most-enclosing-brace paren-state (point))
12166 paren-state))
12168 ;; CASE 7D: we are inside a conditional test clause. treat
12169 ;; these things as statements
12170 ((progn
12171 (goto-char containing-sexp)
12172 (and (c-safe (c-forward-sexp -1) t)
12173 (looking-at "\\<for\\>[^_]")))
12174 (goto-char (1+ containing-sexp))
12175 (c-forward-syntactic-ws indent-point)
12176 (if (eq char-before-ip ?\;)
12177 (c-add-syntax 'statement (point))
12178 (c-add-syntax 'statement-cont (point))
12181 ;; CASE 7E: maybe a continued ObjC method call. This is the
12182 ;; case when we are inside a [] bracketed exp, and what
12183 ;; precede the opening bracket is not an identifier.
12184 ((and c-opt-method-key
12185 (eq (char-after containing-sexp) ?\[)
12186 (progn
12187 (goto-char (1- containing-sexp))
12188 (c-backward-syntactic-ws (c-point 'bod))
12189 (if (not (looking-at c-symbol-key))
12190 (c-add-syntax 'objc-method-call-cont containing-sexp))
12193 ;; CASE 7F: we are looking at an arglist continuation line,
12194 ;; but the preceding argument is on the same line as the
12195 ;; opening paren. This case includes multi-line
12196 ;; mathematical paren groupings, but we could be on a
12197 ;; for-list continuation line. C.f. case 7A.
12198 ((progn
12199 (goto-char (1+ containing-sexp))
12200 (< (save-excursion
12201 (c-forward-syntactic-ws)
12202 (point))
12203 (c-point 'bonl)))
12204 (goto-char containing-sexp) ; paren opening the arglist
12205 (setq placeholder (c-point 'boi))
12206 (if (and (c-safe (backward-up-list 1) t)
12207 (>= (point) placeholder))
12208 (progn
12209 (forward-char)
12210 (skip-chars-forward " \t"))
12211 (goto-char placeholder))
12212 (c-add-stmt-syntax 'arglist-cont-nonempty (list containing-sexp) t
12213 (c-most-enclosing-brace c-state-cache (point))
12214 paren-state))
12216 ;; CASE 7G: we are looking at just a normal arglist
12217 ;; continuation line
12218 (t (c-forward-syntactic-ws indent-point)
12219 (c-add-syntax 'arglist-cont (c-point 'boi)))
12222 ;; CASE 8: func-local multi-inheritance line
12223 ((and (c-major-mode-is 'c++-mode)
12224 (save-excursion
12225 (goto-char indent-point)
12226 (skip-chars-forward " \t")
12227 (looking-at c-opt-postfix-decl-spec-key)))
12228 (goto-char indent-point)
12229 (skip-chars-forward " \t")
12230 (cond
12232 ;; CASE 8A: non-hanging colon on an inher intro
12233 ((eq char-after-ip ?:)
12234 (c-backward-syntactic-ws lim)
12235 (c-add-syntax 'inher-intro (c-point 'boi)))
12237 ;; CASE 8B: hanging colon on an inher intro
12238 ((eq char-before-ip ?:)
12239 (c-add-syntax 'inher-intro (c-point 'boi)))
12241 ;; CASE 8C: a continued inheritance line
12243 (c-beginning-of-inheritance-list lim)
12244 (c-add-syntax 'inher-cont (point))
12247 ;; CASE 9: we are inside a brace-list
12248 ((and (not (c-major-mode-is 'awk-mode)) ; Maybe this isn't needed (ACM, 2002/3/29)
12249 (setq special-brace-list
12250 (or (and c-special-brace-lists ;;;; ALWAYS NIL FOR AWK!!
12251 (save-excursion
12252 (goto-char containing-sexp)
12253 (c-looking-at-special-brace-list)))
12254 (c-inside-bracelist-p containing-sexp paren-state))))
12255 (cond
12257 ;; CASE 9A: In the middle of a special brace list opener.
12258 ((and (consp special-brace-list)
12259 (save-excursion
12260 (goto-char containing-sexp)
12261 (eq (char-after) ?\())
12262 (eq char-after-ip (car (cdr special-brace-list))))
12263 (goto-char (car (car special-brace-list)))
12264 (skip-chars-backward " \t")
12265 (if (and (bolp)
12266 (assoc 'statement-cont
12267 (setq placeholder (c-guess-basic-syntax))))
12268 (setq c-syntactic-context placeholder)
12269 (c-beginning-of-statement-1
12270 (c-safe-position (1- containing-sexp) paren-state))
12271 (c-forward-token-2 0)
12272 (while (cond
12273 ((looking-at c-specifier-key)
12274 (c-forward-keyword-clause 1))
12275 ((and c-opt-cpp-prefix
12276 (looking-at c-noise-macro-with-parens-name-re))
12277 (c-forward-noise-clause))))
12278 (c-add-syntax 'brace-list-open (c-point 'boi))))
12280 ;; CASE 9B: brace-list-close brace
12281 ((if (consp special-brace-list)
12282 ;; Check special brace list closer.
12283 (progn
12284 (goto-char (car (car special-brace-list)))
12285 (save-excursion
12286 (goto-char indent-point)
12287 (back-to-indentation)
12289 ;; We were between the special close char and the `)'.
12290 (and (eq (char-after) ?\))
12291 (eq (1+ (point)) (cdr (car special-brace-list))))
12292 ;; We were before the special close char.
12293 (and (eq (char-after) (cdr (cdr special-brace-list)))
12294 (zerop (c-forward-token-2))
12295 (eq (1+ (point)) (cdr (car special-brace-list)))))))
12296 ;; Normal brace list check.
12297 (and (eq char-after-ip ?})
12298 (c-safe (goto-char (c-up-list-backward (point))) t)
12299 (= (point) containing-sexp)))
12300 (if (eq (point) (c-point 'boi))
12301 (c-add-syntax 'brace-list-close (point))
12302 (setq lim (c-most-enclosing-brace c-state-cache (point)))
12303 (c-beginning-of-statement-1 lim nil nil t)
12304 (c-add-stmt-syntax 'brace-list-close nil t lim paren-state)))
12307 ;; Prepare for the rest of the cases below by going to the
12308 ;; token following the opening brace
12309 (if (consp special-brace-list)
12310 (progn
12311 (goto-char (car (car special-brace-list)))
12312 (c-forward-token-2 1 nil indent-point))
12313 (goto-char containing-sexp))
12314 (forward-char)
12315 (let ((start (point)))
12316 (c-forward-syntactic-ws indent-point)
12317 (goto-char (max start (c-point 'bol))))
12318 (c-skip-ws-forward indent-point)
12319 (cond
12321 ;; CASE 9C: we're looking at the first line in a brace-list
12322 ((= (point) indent-point)
12323 (if (consp special-brace-list)
12324 (goto-char (car (car special-brace-list)))
12325 (goto-char containing-sexp))
12326 (if (eq (point) (c-point 'boi))
12327 (c-add-syntax 'brace-list-intro (point))
12328 (setq lim (c-most-enclosing-brace c-state-cache (point)))
12329 (c-beginning-of-statement-1 lim)
12330 (c-add-stmt-syntax 'brace-list-intro nil t lim paren-state)))
12332 ;; CASE 9D: this is just a later brace-list-entry or
12333 ;; brace-entry-open
12334 (t (if (or (eq char-after-ip ?{)
12335 (and c-special-brace-lists
12336 (save-excursion
12337 (goto-char indent-point)
12338 (c-forward-syntactic-ws (c-point 'eol))
12339 (c-looking-at-special-brace-list (point)))))
12340 (c-add-syntax 'brace-entry-open (point))
12341 (c-add-syntax 'brace-list-entry (point))
12343 ))))
12345 ;; CASE 10: A continued statement or top level construct.
12346 ((and (not (memq char-before-ip '(?\; ?:)))
12347 (not (c-at-vsemi-p before-ws-ip))
12348 (or (not (eq char-before-ip ?}))
12349 (c-looking-at-inexpr-block-backward c-state-cache))
12350 (> (point)
12351 (save-excursion
12352 (c-beginning-of-statement-1 containing-sexp)
12353 (setq placeholder (point))))
12354 (/= placeholder containing-sexp))
12355 ;; This is shared with case 18.
12356 (c-guess-continued-construct indent-point
12357 char-after-ip
12358 placeholder
12359 containing-sexp
12360 paren-state))
12362 ;; CASE 16: block close brace, possibly closing the defun or
12363 ;; the class
12364 ((eq char-after-ip ?})
12365 ;; From here on we have the next containing sexp in lim.
12366 (setq lim (c-most-enclosing-brace paren-state))
12367 (goto-char containing-sexp)
12368 (cond
12370 ;; CASE 16E: Closing a statement block? This catches
12371 ;; cases where it's preceded by a statement keyword,
12372 ;; which works even when used in an "invalid" context,
12373 ;; e.g. a macro argument.
12374 ((c-after-conditional)
12375 (c-backward-to-block-anchor lim)
12376 (c-add-stmt-syntax 'block-close nil t lim paren-state))
12378 ;; CASE 16A: closing a lambda defun or an in-expression
12379 ;; block? C.f. cases 4, 7B and 17E.
12380 ((setq placeholder (c-looking-at-inexpr-block
12381 (c-safe-position containing-sexp paren-state)
12382 nil))
12383 (setq tmpsymbol (if (eq (car placeholder) 'inlambda)
12384 'inline-close
12385 'block-close))
12386 (goto-char containing-sexp)
12387 (back-to-indentation)
12388 (if (= containing-sexp (point))
12389 (c-add-syntax tmpsymbol (point))
12390 (goto-char (cdr placeholder))
12391 (back-to-indentation)
12392 (c-add-stmt-syntax tmpsymbol nil t
12393 (c-most-enclosing-brace paren-state (point))
12394 paren-state)
12395 (if (/= (point) (cdr placeholder))
12396 (c-add-syntax (car placeholder)))))
12398 ;; CASE 16B: does this close an inline or a function in
12399 ;; a non-class declaration level block?
12400 ((save-excursion
12401 (and lim
12402 (progn
12403 (goto-char lim)
12404 (c-looking-at-decl-block
12405 (c-most-enclosing-brace paren-state lim)
12406 nil))
12407 (setq placeholder (point))))
12408 (c-backward-to-decl-anchor lim)
12409 (back-to-indentation)
12410 (if (save-excursion
12411 (goto-char placeholder)
12412 (looking-at c-other-decl-block-key))
12413 (c-add-syntax 'defun-close (point))
12414 (c-add-syntax 'inline-close (point))))
12416 ;; CASE 16F: Can be a defun-close of a function declared
12417 ;; in a statement block, e.g. in Pike or when using gcc
12418 ;; extensions, but watch out for macros followed by
12419 ;; blocks. Let it through to be handled below.
12420 ;; C.f. cases B.3 and 17G.
12421 ((save-excursion
12422 (and (not (c-at-statement-start-p))
12423 (eq (c-beginning-of-statement-1 lim nil nil t) 'same)
12424 (setq placeholder (point))
12425 (let ((c-recognize-typeless-decls nil))
12426 ;; Turn off recognition of constructs that
12427 ;; lacks a type in this case, since that's more
12428 ;; likely to be a macro followed by a block.
12429 (c-forward-decl-or-cast-1 (c-point 'bosws) nil nil))))
12430 (back-to-indentation)
12431 (if (/= (point) containing-sexp)
12432 (goto-char placeholder))
12433 (c-add-stmt-syntax 'defun-close nil t lim paren-state))
12435 ;; CASE 16C: If there is an enclosing brace then this is
12436 ;; a block close since defun closes inside declaration
12437 ;; level blocks have been handled above.
12438 (lim
12439 ;; If the block is preceded by a case/switch label on
12440 ;; the same line, we anchor at the first preceding label
12441 ;; at boi. The default handling in c-add-stmt-syntax
12442 ;; really fixes it better, but we do like this to keep
12443 ;; the indentation compatible with version 5.28 and
12444 ;; earlier. C.f. case 17H.
12445 (while (and (/= (setq placeholder (point)) (c-point 'boi))
12446 (eq (c-beginning-of-statement-1 lim) 'label)))
12447 (goto-char placeholder)
12448 (if (looking-at c-label-kwds-regexp)
12449 (c-add-syntax 'block-close (point))
12450 (goto-char containing-sexp)
12451 ;; c-backward-to-block-anchor not necessary here; those
12452 ;; situations are handled in case 16E above.
12453 (c-add-stmt-syntax 'block-close nil t lim paren-state)))
12455 ;; CASE 16D: Only top level defun close left.
12457 (goto-char containing-sexp)
12458 (c-backward-to-decl-anchor lim)
12459 (c-add-stmt-syntax 'defun-close nil nil
12460 (c-most-enclosing-brace paren-state)
12461 paren-state))
12464 ;; CASE 19: line is an expression, not a statement, and is directly
12465 ;; contained by a template delimiter. Most likely, we are in a
12466 ;; template arglist within a statement. This case is based on CASE
12467 ;; 7. At some point in the future, we may wish to create more
12468 ;; syntactic symbols such as `template-intro',
12469 ;; `template-cont-nonempty', etc., and distinguish between them as we
12470 ;; do for `arglist-intro' etc. (2009-12-07).
12471 ((and c-recognize-<>-arglists
12472 (setq containing-< (c-up-list-backward indent-point containing-sexp))
12473 (eq (char-after containing-<) ?\<))
12474 (setq placeholder (c-point 'boi containing-<))
12475 (goto-char containing-sexp) ; Most nested Lbrace/Lparen (but not
12476 ; '<') before indent-point.
12477 (if (>= (point) placeholder)
12478 (progn
12479 (forward-char)
12480 (skip-chars-forward " \t"))
12481 (goto-char placeholder))
12482 (c-add-stmt-syntax 'template-args-cont (list containing-<) t
12483 (c-most-enclosing-brace c-state-cache (point))
12484 paren-state))
12486 ;; CASE 17: Statement or defun catchall.
12488 (goto-char indent-point)
12489 ;; Back up statements until we find one that starts at boi.
12490 (while (let* ((prev-point (point))
12491 (last-step-type (c-beginning-of-statement-1
12492 containing-sexp)))
12493 (if (= (point) prev-point)
12494 (progn
12495 (setq step-type (or step-type last-step-type))
12496 nil)
12497 (setq step-type last-step-type)
12498 (/= (point) (c-point 'boi)))))
12499 (cond
12501 ;; CASE 17B: continued statement
12502 ((and (eq step-type 'same)
12503 (/= (point) indent-point))
12504 (c-add-stmt-syntax 'statement-cont nil nil
12505 containing-sexp paren-state))
12507 ;; CASE 17A: After a case/default label?
12508 ((progn
12509 (while (and (eq step-type 'label)
12510 (not (looking-at c-label-kwds-regexp)))
12511 (setq step-type
12512 (c-beginning-of-statement-1 containing-sexp)))
12513 (eq step-type 'label))
12514 (c-add-stmt-syntax (if (eq char-after-ip ?{)
12515 'statement-case-open
12516 'statement-case-intro)
12517 nil t containing-sexp paren-state))
12519 ;; CASE 17D: any old statement
12520 ((progn
12521 (while (eq step-type 'label)
12522 (setq step-type
12523 (c-beginning-of-statement-1 containing-sexp)))
12524 (eq step-type 'previous))
12525 (c-add-stmt-syntax 'statement nil t
12526 containing-sexp paren-state)
12527 (if (eq char-after-ip ?{)
12528 (c-add-syntax 'block-open)))
12530 ;; CASE 17I: Inside a substatement block.
12531 ((progn
12532 ;; The following tests are all based on containing-sexp.
12533 (goto-char containing-sexp)
12534 ;; From here on we have the next containing sexp in lim.
12535 (setq lim (c-most-enclosing-brace paren-state containing-sexp))
12536 (c-after-conditional))
12537 (c-backward-to-block-anchor lim)
12538 (c-add-stmt-syntax 'statement-block-intro nil t
12539 lim paren-state)
12540 (if (eq char-after-ip ?{)
12541 (c-add-syntax 'block-open)))
12543 ;; CASE 17E: first statement in an in-expression block.
12544 ;; C.f. cases 4, 7B and 16A.
12545 ((setq placeholder (c-looking-at-inexpr-block
12546 (c-safe-position containing-sexp paren-state)
12547 nil))
12548 (setq tmpsymbol (if (eq (car placeholder) 'inlambda)
12549 'defun-block-intro
12550 'statement-block-intro))
12551 (back-to-indentation)
12552 (if (= containing-sexp (point))
12553 (c-add-syntax tmpsymbol (point))
12554 (goto-char (cdr placeholder))
12555 (back-to-indentation)
12556 (c-add-stmt-syntax tmpsymbol nil t
12557 (c-most-enclosing-brace c-state-cache (point))
12558 paren-state)
12559 (if (/= (point) (cdr placeholder))
12560 (c-add-syntax (car placeholder))))
12561 (if (eq char-after-ip ?{)
12562 (c-add-syntax 'block-open)))
12564 ;; CASE 17F: first statement in an inline, or first
12565 ;; statement in a top-level defun. we can tell this is it
12566 ;; if there are no enclosing braces that haven't been
12567 ;; narrowed out by a class (i.e. don't use bod here).
12568 ((save-excursion
12569 (or (not (setq placeholder (c-most-enclosing-brace
12570 paren-state)))
12571 (and (progn
12572 (goto-char placeholder)
12573 (eq (char-after) ?{))
12574 (c-looking-at-decl-block (c-most-enclosing-brace
12575 paren-state (point))
12576 nil))))
12577 (c-backward-to-decl-anchor lim)
12578 (back-to-indentation)
12579 (c-add-syntax 'defun-block-intro (point)))
12581 ;; CASE 17G: First statement in a function declared inside
12582 ;; a normal block. This can occur in Pike and with
12583 ;; e.g. the gcc extensions, but watch out for macros
12584 ;; followed by blocks. C.f. cases B.3 and 16F.
12585 ((save-excursion
12586 (and (not (c-at-statement-start-p))
12587 (eq (c-beginning-of-statement-1 lim nil nil t) 'same)
12588 (setq placeholder (point))
12589 (let ((c-recognize-typeless-decls nil))
12590 ;; Turn off recognition of constructs that lacks
12591 ;; a type in this case, since that's more likely
12592 ;; to be a macro followed by a block.
12593 (c-forward-decl-or-cast-1 (c-point 'bosws) nil nil))))
12594 (back-to-indentation)
12595 (if (/= (point) containing-sexp)
12596 (goto-char placeholder))
12597 (c-add-stmt-syntax 'defun-block-intro nil t
12598 lim paren-state))
12600 ;; CASE 17H: First statement in a block.
12602 ;; If the block is preceded by a case/switch label on the
12603 ;; same line, we anchor at the first preceding label at
12604 ;; boi. The default handling in c-add-stmt-syntax is
12605 ;; really fixes it better, but we do like this to keep the
12606 ;; indentation compatible with version 5.28 and earlier.
12607 ;; C.f. case 16C.
12608 (while (and (/= (setq placeholder (point)) (c-point 'boi))
12609 (eq (c-beginning-of-statement-1 lim) 'label)))
12610 (goto-char placeholder)
12611 (if (looking-at c-label-kwds-regexp)
12612 (c-add-syntax 'statement-block-intro (point))
12613 (goto-char containing-sexp)
12614 ;; c-backward-to-block-anchor not necessary here; those
12615 ;; situations are handled in case 17I above.
12616 (c-add-stmt-syntax 'statement-block-intro nil t
12617 lim paren-state))
12618 (if (eq char-after-ip ?{)
12619 (c-add-syntax 'block-open)))
12623 ;; now we need to look at any modifiers
12624 (goto-char indent-point)
12625 (skip-chars-forward " \t")
12627 ;; are we looking at a comment only line?
12628 (when (and (looking-at c-comment-start-regexp)
12629 (/= (c-forward-token-2 0 nil (c-point 'eol)) 0))
12630 (c-append-syntax 'comment-intro))
12632 ;; we might want to give additional offset to friends (in C++).
12633 (when (and c-opt-friend-key
12634 (looking-at c-opt-friend-key))
12635 (c-append-syntax 'friend))
12637 ;; Set syntactic-relpos.
12638 (let ((p c-syntactic-context))
12639 (while (and p
12640 (if (integerp (c-langelem-pos (car p)))
12641 (progn
12642 (setq syntactic-relpos (c-langelem-pos (car p)))
12643 nil)
12645 (setq p (cdr p))))
12647 ;; Start of or a continuation of a preprocessor directive?
12648 (if (and macro-start
12649 (eq macro-start (c-point 'boi))
12650 (not (and (c-major-mode-is 'pike-mode)
12651 (eq (char-after (1+ macro-start)) ?\"))))
12652 (c-append-syntax 'cpp-macro)
12653 (when (and c-syntactic-indentation-in-macros macro-start)
12654 (if in-macro-expr
12655 (when (or
12656 (< syntactic-relpos macro-start)
12657 (not (or
12658 (assq 'arglist-intro c-syntactic-context)
12659 (assq 'arglist-cont c-syntactic-context)
12660 (assq 'arglist-cont-nonempty c-syntactic-context)
12661 (assq 'arglist-close c-syntactic-context))))
12662 ;; If inside a cpp expression, i.e. anywhere in a
12663 ;; cpp directive except a #define body, we only let
12664 ;; through the syntactic analysis that is internal
12665 ;; in the expression. That means the arglist
12666 ;; elements, if they are anchored inside the cpp
12667 ;; expression.
12668 (setq c-syntactic-context nil)
12669 (c-add-syntax 'cpp-macro-cont macro-start))
12670 (when (and (eq macro-start syntactic-relpos)
12671 (not (assq 'cpp-define-intro c-syntactic-context))
12672 (save-excursion
12673 (goto-char macro-start)
12674 (or (not (c-forward-to-cpp-define-body))
12675 (<= (point) (c-point 'boi indent-point)))))
12676 ;; Inside a #define body and the syntactic analysis is
12677 ;; anchored on the start of the #define. In this case
12678 ;; we add cpp-define-intro to get the extra
12679 ;; indentation of the #define body.
12680 (c-add-syntax 'cpp-define-intro)))))
12682 ;; return the syntax
12683 c-syntactic-context)))
12686 ;; Indentation calculation.
12688 (defun c-evaluate-offset (offset langelem symbol)
12689 ;; offset can be a number, a function, a variable, a list, or one of
12690 ;; the symbols + or -
12692 ;; This function might do hidden buffer changes.
12693 (let ((res
12694 (cond
12695 ((numberp offset) offset)
12696 ((vectorp offset) offset)
12697 ((null offset) nil)
12699 ((eq offset '+) c-basic-offset)
12700 ((eq offset '-) (- c-basic-offset))
12701 ((eq offset '++) (* 2 c-basic-offset))
12702 ((eq offset '--) (* 2 (- c-basic-offset)))
12703 ((eq offset '*) (/ c-basic-offset 2))
12704 ((eq offset '/) (/ (- c-basic-offset) 2))
12706 ((functionp offset)
12707 (c-evaluate-offset
12708 (funcall offset
12709 (cons (c-langelem-sym langelem)
12710 (c-langelem-pos langelem)))
12711 langelem symbol))
12713 ((listp offset)
12714 (cond
12715 ((eq (car offset) 'quote)
12716 (c-benign-error "The offset %S for %s was mistakenly quoted"
12717 offset symbol)
12718 nil)
12720 ((memq (car offset) '(min max))
12721 (let (res val (method (car offset)))
12722 (setq offset (cdr offset))
12723 (while offset
12724 (setq val (c-evaluate-offset (car offset) langelem symbol))
12725 (cond
12726 ((not val))
12727 ((not res)
12728 (setq res val))
12729 ((integerp val)
12730 (if (vectorp res)
12731 (c-benign-error "\
12732 Error evaluating offset %S for %s: \
12733 Cannot combine absolute offset %S with relative %S in `%s' method"
12734 (car offset) symbol res val method)
12735 (setq res (funcall method res val))))
12737 (if (integerp res)
12738 (c-benign-error "\
12739 Error evaluating offset %S for %s: \
12740 Cannot combine relative offset %S with absolute %S in `%s' method"
12741 (car offset) symbol res val method)
12742 (setq res (vector (funcall method (aref res 0)
12743 (aref val 0)))))))
12744 (setq offset (cdr offset)))
12745 res))
12747 ((eq (car offset) 'add)
12748 (let (res val)
12749 (setq offset (cdr offset))
12750 (while offset
12751 (setq val (c-evaluate-offset (car offset) langelem symbol))
12752 (cond
12753 ((not val))
12754 ((not res)
12755 (setq res val))
12756 ((integerp val)
12757 (if (vectorp res)
12758 (setq res (vector (+ (aref res 0) val)))
12759 (setq res (+ res val))))
12761 (if (vectorp res)
12762 (c-benign-error "\
12763 Error evaluating offset %S for %s: \
12764 Cannot combine absolute offsets %S and %S in `add' method"
12765 (car offset) symbol res val)
12766 (setq res val)))) ; Override.
12767 (setq offset (cdr offset)))
12768 res))
12771 (let (res)
12772 (when (eq (car offset) 'first)
12773 (setq offset (cdr offset)))
12774 (while (and (not res) offset)
12775 (setq res (c-evaluate-offset (car offset) langelem symbol)
12776 offset (cdr offset)))
12777 res))))
12779 ((and (symbolp offset) (boundp offset))
12780 (symbol-value offset))
12783 (c-benign-error "Unknown offset format %S for %s" offset symbol)
12784 nil))))
12786 (if (or (null res) (integerp res)
12787 (and (vectorp res) (= (length res) 1) (integerp (aref res 0))))
12789 (c-benign-error "Error evaluating offset %S for %s: Got invalid value %S"
12790 offset symbol res)
12791 nil)))
12793 (defun c-calc-offset (langelem)
12794 ;; Get offset from LANGELEM which is a list beginning with the
12795 ;; syntactic symbol and followed by any analysis data it provides.
12796 ;; That data may be zero or more elements, but if at least one is
12797 ;; given then the first is the anchor position (or nil). The symbol
12798 ;; is matched against `c-offsets-alist' and the offset calculated
12799 ;; from that is returned.
12801 ;; This function might do hidden buffer changes.
12802 (let* ((symbol (c-langelem-sym langelem))
12803 (match (assq symbol c-offsets-alist))
12804 (offset (cdr-safe match)))
12805 (if match
12806 (setq offset (c-evaluate-offset offset langelem symbol))
12807 (if c-strict-syntax-p
12808 (c-benign-error "No offset found for syntactic symbol %s" symbol))
12809 (setq offset 0))
12810 (if (vectorp offset)
12811 offset
12812 (or (and (numberp offset) offset)
12813 (and (symbolp offset) (symbol-value offset))
12817 (defun c-get-offset (langelem)
12818 ;; This is a compatibility wrapper for `c-calc-offset' in case
12819 ;; someone is calling it directly. It takes an old style syntactic
12820 ;; element on the form (SYMBOL . ANCHOR-POS) and converts it to the
12821 ;; new list form.
12823 ;; This function might do hidden buffer changes.
12824 (if (c-langelem-pos langelem)
12825 (c-calc-offset (list (c-langelem-sym langelem)
12826 (c-langelem-pos langelem)))
12827 (c-calc-offset langelem)))
12829 (defun c-get-syntactic-indentation (langelems)
12830 ;; Calculate the syntactic indentation from a syntactic description
12831 ;; as returned by `c-guess-syntax'.
12833 ;; Note that topmost-intro always has an anchor position at bol, for
12834 ;; historical reasons. It's often used together with other symbols
12835 ;; that has more sane positions. Since we always use the first
12836 ;; found anchor position, we rely on that these other symbols always
12837 ;; precede topmost-intro in the LANGELEMS list.
12839 ;; This function might do hidden buffer changes.
12840 (let ((indent 0) anchor)
12842 (while langelems
12843 (let* ((c-syntactic-element (car langelems))
12844 (res (c-calc-offset c-syntactic-element)))
12846 (if (vectorp res)
12847 ;; Got an absolute column that overrides any indentation
12848 ;; we've collected so far, but not the relative
12849 ;; indentation we might get for the nested structures
12850 ;; further down the langelems list.
12851 (setq indent (elt res 0)
12852 anchor (point-min)) ; A position at column 0.
12854 ;; Got a relative change of the current calculated
12855 ;; indentation.
12856 (setq indent (+ indent res))
12858 ;; Use the anchor position from the first syntactic
12859 ;; element with one.
12860 (unless anchor
12861 (setq anchor (c-langelem-pos (car langelems)))))
12863 (setq langelems (cdr langelems))))
12865 (if anchor
12866 (+ indent (save-excursion
12867 (goto-char anchor)
12868 (current-column)))
12869 indent)))
12872 (cc-provide 'cc-engine)
12874 ;; Local Variables:
12875 ;; indent-tabs-mode: t
12876 ;; tab-width: 8
12877 ;; End:
12878 ;;; cc-engine.el ends here