Add a NEWS entry.
[emacs.git] / lisp / progmodes / cc-engine.el
blob5a26c0f27708ca43158cbedaeef44e3b22f40f0c
1 ;;; cc-engine.el --- core syntax guessing engine for CC mode -*- coding: utf-8 -*-
3 ;; Copyright (C) 1985, 1987, 1992-2016 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 (defsubst c-invalidate-sws-region-after (beg end)
1712 ;; Called from `after-change-functions'. Note that if
1713 ;; `c-forward-sws' or `c-backward-sws' are used outside
1714 ;; `c-save-buffer-state' or similar then this will remove the cache
1715 ;; properties right after they're added.
1717 ;; This function does hidden buffer changes.
1719 (save-excursion
1720 ;; Adjust the end to remove the properties in any following simple
1721 ;; ws up to and including the next line break, if there is any
1722 ;; after the changed region. This is necessary e.g. when a rung
1723 ;; marked empty line is converted to a line comment by inserting
1724 ;; "//" before the line break. In that case the line break would
1725 ;; keep the rung mark which could make a later `c-backward-sws'
1726 ;; move into the line comment instead of over it.
1727 (goto-char end)
1728 (skip-chars-forward " \t\f\v")
1729 (when (and (eolp) (not (eobp)))
1730 (setq end (1+ (point)))))
1732 (when (and (= beg end)
1733 (get-text-property beg 'c-in-sws)
1734 (> beg (point-min))
1735 (get-text-property (1- beg) 'c-in-sws))
1736 ;; Ensure that an `c-in-sws' range gets broken. Note that it isn't
1737 ;; safe to keep a range that was continuous before the change. E.g:
1739 ;; #define foo
1740 ;; \
1741 ;; bar
1743 ;; There can be a "ladder" between "#" and "b". Now, if the newline
1744 ;; after "foo" is removed then "bar" will become part of the cpp
1745 ;; directive instead of a syntactically relevant token. In that
1746 ;; case there's no longer syntactic ws from "#" to "b".
1747 (setq beg (1- beg)))
1749 (c-debug-sws-msg "c-invalidate-sws-region-after [%s..%s]" beg end)
1750 (c-remove-is-and-in-sws beg end))
1752 (defun c-forward-sws ()
1753 ;; Used by `c-forward-syntactic-ws' to implement the unbounded search.
1755 ;; This function might do hidden buffer changes.
1757 (let (;; `rung-pos' is set to a position as early as possible in the
1758 ;; unmarked part of the simple ws region.
1759 (rung-pos (point)) next-rung-pos rung-end-pos last-put-in-sws-pos
1760 rung-is-marked next-rung-is-marked simple-ws-end
1761 ;; `safe-start' is set when it's safe to cache the start position.
1762 ;; It's not set if we've initially skipped over comments and line
1763 ;; continuations since we might have gone out through the end of a
1764 ;; macro then. This provision makes `c-forward-sws' not populate the
1765 ;; cache in the majority of cases, but otoh is `c-backward-sws' by far
1766 ;; more common.
1767 safe-start)
1769 ;; Skip simple ws and do a quick check on the following character to see
1770 ;; if it's anything that can't start syntactic ws, so we can bail out
1771 ;; early in the majority of cases when there just are a few ws chars.
1772 (skip-chars-forward " \t\n\r\f\v")
1773 (when (or (looking-at c-syntactic-ws-start)
1774 (and c-opt-cpp-prefix
1775 (looking-at c-noise-macro-name-re)))
1777 (setq rung-end-pos (min (1+ (point)) (point-max)))
1778 (if (setq rung-is-marked (text-property-any rung-pos rung-end-pos
1779 'c-is-sws t))
1780 ;; Find the last rung position to avoid setting properties in all
1781 ;; the cases when the marked rung is complete.
1782 ;; (`next-single-property-change' is certain to move at least one
1783 ;; step forward.)
1784 (setq rung-pos (1- (c-next-single-property-change
1785 rung-is-marked 'c-is-sws nil rung-end-pos)))
1786 ;; Got no marked rung here. Since the simple ws might have started
1787 ;; inside a line comment or cpp directive we must set `rung-pos' as
1788 ;; high as possible.
1789 (setq rung-pos (point)))
1791 (with-silent-modifications
1792 (while
1793 (progn
1794 ;; In the following while form, we move over a "ladder" and
1795 ;; following simple WS each time round the loop, appending the WS
1796 ;; onto the ladder, joining adjacent ladders, and terminating when
1797 ;; there is no more WS or we reach EOB.
1798 (while
1799 (when (and rung-is-marked
1800 (get-text-property (point) 'c-in-sws))
1802 ;; The following search is the main reason that `c-in-sws'
1803 ;; and `c-is-sws' aren't combined to one property.
1804 (goto-char (c-next-single-property-change
1805 (point) 'c-in-sws nil (point-max)))
1806 (unless (get-text-property (point) 'c-is-sws)
1807 ;; If the `c-in-sws' region extended past the last
1808 ;; `c-is-sws' char we have to go back a bit.
1809 (or (get-text-property (1- (point)) 'c-is-sws)
1810 (goto-char (previous-single-property-change
1811 (point) 'c-is-sws)))
1812 (backward-char))
1814 (c-debug-sws-msg
1815 "c-forward-sws cached move %s -> %s (max %s)"
1816 rung-pos (point) (point-max))
1818 (setq rung-pos (point))
1819 (and (> (skip-chars-forward " \t\n\r\f\v") 0)
1820 (not (eobp))))
1822 ;; We'll loop here if there is simple ws after the last rung.
1823 ;; That means that there's been some change in it and it's
1824 ;; possible that we've stepped into another ladder, so extend
1825 ;; the previous one to join with it if there is one, and try to
1826 ;; use the cache again.
1827 (c-debug-sws-msg
1828 "c-forward-sws extending rung with [%s..%s] (max %s)"
1829 (1+ rung-pos) (1+ (point)) (point-max))
1830 (unless (get-text-property (point) 'c-is-sws)
1831 ;; Remove any `c-in-sws' property from the last char of
1832 ;; the rung before we mark it with `c-is-sws', so that we
1833 ;; won't connect with the remains of a broken "ladder".
1834 (c-remove-in-sws (point) (1+ (point))))
1835 (c-put-is-sws (1+ rung-pos)
1836 (1+ (point)))
1837 (c-put-in-sws rung-pos
1838 (setq rung-pos (point)
1839 last-put-in-sws-pos rung-pos)))
1841 ;; Now move over any comments (x)or a CPP construct.
1842 (setq simple-ws-end (point))
1843 (c-forward-comments)
1845 (cond
1846 ((/= (point) simple-ws-end)
1847 ;; Skipped over comments. Don't cache at eob in case the buffer
1848 ;; is narrowed.
1849 (not (eobp)))
1851 ((save-excursion
1852 (and c-opt-cpp-prefix
1853 (looking-at c-opt-cpp-start)
1854 (progn (skip-chars-backward " \t")
1855 (bolp))
1856 (or (bobp)
1857 (progn (backward-char)
1858 (not (eq (char-before) ?\\))))))
1859 ;; Skip a preprocessor directive.
1860 (end-of-line)
1861 (while (and (eq (char-before) ?\\)
1862 (= (forward-line 1) 0))
1863 (end-of-line))
1864 (forward-line 1)
1865 (setq safe-start t)
1866 ;; Don't cache at eob in case the buffer is narrowed.
1867 (not (eobp)))
1869 ((and c-opt-cpp-prefix
1870 (looking-at c-noise-macro-name-re))
1871 ;; Skip over a noise macro.
1872 (goto-char (match-end 1))
1873 (setq safe-start t)
1874 (not (eobp)))))
1876 ;; We've searched over a piece of non-white syntactic ws. See if this
1877 ;; can be cached.
1878 (setq next-rung-pos (point))
1879 (skip-chars-forward " \t\n\r\f\v")
1880 (setq rung-end-pos (min (1+ (point)) (point-max)))
1882 (if (or
1883 ;; Cache if we haven't skipped comments only, and if we started
1884 ;; either from a marked rung or from a completely uncached
1885 ;; position.
1886 (and safe-start
1887 (or rung-is-marked
1888 (not (get-text-property simple-ws-end 'c-in-sws))))
1890 ;; See if there's a marked rung in the encountered simple ws. If
1891 ;; so then we can cache, unless `safe-start' is nil. Even then
1892 ;; we need to do this to check if the cache can be used for the
1893 ;; next step.
1894 (and (setq next-rung-is-marked
1895 (text-property-any next-rung-pos rung-end-pos
1896 'c-is-sws t))
1897 safe-start))
1899 (progn
1900 (c-debug-sws-msg
1901 "c-forward-sws caching [%s..%s] - [%s..%s] (max %s)"
1902 rung-pos (1+ simple-ws-end) next-rung-pos rung-end-pos
1903 (point-max))
1905 ;; Remove the properties for any nested ws that might be cached.
1906 ;; Only necessary for `c-is-sws' since `c-in-sws' will be set
1907 ;; anyway.
1908 (c-remove-is-sws (1+ simple-ws-end) next-rung-pos)
1909 (unless (and rung-is-marked (= rung-pos simple-ws-end))
1910 (c-put-is-sws rung-pos
1911 (1+ simple-ws-end))
1912 (setq rung-is-marked t))
1913 (c-put-in-sws rung-pos
1914 (setq rung-pos (point)
1915 last-put-in-sws-pos rung-pos))
1916 (unless (get-text-property (1- rung-end-pos) 'c-is-sws)
1917 ;; Remove any `c-in-sws' property from the last char of
1918 ;; the rung before we mark it with `c-is-sws', so that we
1919 ;; won't connect with the remains of a broken "ladder".
1920 (c-remove-in-sws (1- rung-end-pos) rung-end-pos))
1921 (c-put-is-sws next-rung-pos
1922 rung-end-pos))
1924 (c-debug-sws-msg
1925 "c-forward-sws not caching [%s..%s] - [%s..%s] (max %s)"
1926 rung-pos (1+ simple-ws-end) next-rung-pos rung-end-pos
1927 (point-max))
1929 ;; Set `rung-pos' for the next rung. It's the same thing here as
1930 ;; initially, except that the rung position is set as early as
1931 ;; possible since we can't be in the ending ws of a line comment or
1932 ;; cpp directive now.
1933 (if (setq rung-is-marked next-rung-is-marked)
1934 (setq rung-pos (1- (c-next-single-property-change
1935 rung-is-marked 'c-is-sws nil rung-end-pos)))
1936 (setq rung-pos next-rung-pos))
1937 (setq safe-start t)))
1939 ;; Make sure that the newly marked `c-in-sws' region doesn't connect to
1940 ;; another one after the point (which might occur when editing inside a
1941 ;; comment or macro).
1942 (when (eq last-put-in-sws-pos (point))
1943 (cond ((< last-put-in-sws-pos (point-max))
1944 (c-debug-sws-msg
1945 "c-forward-sws clearing at %s for cache separation"
1946 last-put-in-sws-pos)
1947 (c-remove-in-sws last-put-in-sws-pos
1948 (1+ last-put-in-sws-pos)))
1950 ;; If at eob we have to clear the last character before the end
1951 ;; instead since the buffer might be narrowed and there might
1952 ;; be a `c-in-sws' after (point-max). In this case it's
1953 ;; necessary to clear both properties.
1954 (c-debug-sws-msg
1955 "c-forward-sws clearing thoroughly at %s for cache separation"
1956 (1- last-put-in-sws-pos))
1957 (c-remove-is-and-in-sws (1- last-put-in-sws-pos)
1958 last-put-in-sws-pos))))
1959 ))))
1961 (defun c-backward-sws ()
1962 ;; Used by `c-backward-syntactic-ws' to implement the unbounded search.
1964 ;; This function might do hidden buffer changes.
1966 (let (;; `rung-pos' is set to a position as late as possible in the unmarked
1967 ;; part of the simple ws region.
1968 (rung-pos (point)) next-rung-pos last-put-in-sws-pos
1969 rung-is-marked simple-ws-beg cmt-skip-pos)
1971 ;; Skip simple horizontal ws and do a quick check on the preceding
1972 ;; character to see if it's anything that can't end syntactic ws, so we can
1973 ;; bail out early in the majority of cases when there just are a few ws
1974 ;; chars. Newlines are complicated in the backward direction, so we can't
1975 ;; skip over them.
1976 (skip-chars-backward " \t\f")
1977 (when (and (not (bobp))
1978 (save-excursion
1979 (backward-char)
1980 (or (looking-at c-syntactic-ws-end)
1981 (and c-opt-cpp-prefix
1982 (looking-at c-symbol-char-key)
1983 (progn (c-beginning-of-current-token)
1984 (looking-at c-noise-macro-name-re))))))
1985 ;; Try to find a rung position in the simple ws preceding point, so that
1986 ;; we can get a cache hit even if the last bit of the simple ws has
1987 ;; changed recently.
1988 (setq simple-ws-beg (point))
1989 (skip-chars-backward " \t\n\r\f\v")
1990 (if (setq rung-is-marked (text-property-any
1991 (point) (min (1+ rung-pos) (point-max))
1992 'c-is-sws t))
1993 ;; `rung-pos' will be the earliest marked position, which means that
1994 ;; there might be later unmarked parts in the simple ws region.
1995 ;; It's not worth the effort to fix that; the last part of the
1996 ;; simple ws is also typically edited often, so it could be wasted.
1997 (goto-char (setq rung-pos rung-is-marked))
1998 (goto-char simple-ws-beg))
2000 (with-silent-modifications
2001 (while
2002 (progn
2003 ;; Each time round the next while form, we move back over a ladder
2004 ;; and append any simple WS preceding it, if possible joining with
2005 ;; the previous ladder.
2006 (while
2007 (when (and rung-is-marked
2008 (not (bobp))
2009 (get-text-property (1- (point)) 'c-in-sws))
2011 ;; The following search is the main reason that `c-in-sws'
2012 ;; and `c-is-sws' aren't combined to one property.
2013 (goto-char (previous-single-property-change
2014 (point) 'c-in-sws nil (point-min)))
2015 (unless (get-text-property (point) 'c-is-sws)
2016 ;; If the `c-in-sws' region extended past the first
2017 ;; `c-is-sws' char we have to go forward a bit.
2018 (goto-char (c-next-single-property-change
2019 (point) 'c-is-sws)))
2021 (c-debug-sws-msg
2022 "c-backward-sws cached move %s <- %s (min %s)"
2023 (point) rung-pos (point-min))
2025 (setq rung-pos (point))
2026 (if (and (< (min (skip-chars-backward " \t\f\v")
2027 (progn
2028 (setq simple-ws-beg (point))
2029 (skip-chars-backward " \t\n\r\f\v")))
2031 (setq rung-is-marked
2032 (text-property-any (point) rung-pos
2033 'c-is-sws t)))
2035 (goto-char simple-ws-beg)
2036 nil))
2038 ;; We'll loop here if there is simple ws before the first rung.
2039 ;; That means that there's been some change in it and it's
2040 ;; possible that we've stepped into another ladder, so extend
2041 ;; the previous one to join with it if there is one, and try to
2042 ;; use the cache again.
2043 (c-debug-sws-msg
2044 "c-backward-sws extending rung with [%s..%s] (min %s)"
2045 rung-is-marked rung-pos (point-min))
2046 (unless (get-text-property (1- rung-pos) 'c-is-sws)
2047 ;; Remove any `c-in-sws' property from the last char of
2048 ;; the rung before we mark it with `c-is-sws', so that we
2049 ;; won't connect with the remains of a broken "ladder".
2050 (c-remove-in-sws (1- rung-pos) rung-pos))
2051 (c-put-is-sws rung-is-marked
2052 rung-pos)
2053 (c-put-in-sws rung-is-marked
2054 (1- rung-pos))
2055 (setq rung-pos rung-is-marked
2056 last-put-in-sws-pos rung-pos))
2058 (c-backward-comments)
2059 (setq cmt-skip-pos (point))
2061 (cond
2062 ((and c-opt-cpp-prefix
2063 (/= cmt-skip-pos simple-ws-beg)
2064 (c-beginning-of-macro))
2065 ;; Inside a cpp directive. See if it should be skipped over.
2066 (let ((cpp-beg (point)))
2068 ;; Move back over all line continuations in the region skipped
2069 ;; over by `c-backward-comments'. If we go past it then we
2070 ;; started inside the cpp directive.
2071 (goto-char simple-ws-beg)
2072 (beginning-of-line)
2073 (while (and (> (point) cmt-skip-pos)
2074 (progn (backward-char)
2075 (eq (char-before) ?\\)))
2076 (beginning-of-line))
2078 (if (< (point) cmt-skip-pos)
2079 ;; Don't move past the cpp directive if we began inside
2080 ;; it. Note that the position at the end of the last line
2081 ;; of the macro is also considered to be within it.
2082 (progn (goto-char cmt-skip-pos)
2083 nil)
2085 ;; It's worthwhile to spend a little bit of effort on finding
2086 ;; the end of the macro, to get a good `simple-ws-beg'
2087 ;; position for the cache. Note that `c-backward-comments'
2088 ;; could have stepped over some comments before going into
2089 ;; the macro, and then `simple-ws-beg' must be kept on the
2090 ;; same side of those comments.
2091 (goto-char simple-ws-beg)
2092 (skip-chars-backward " \t\n\r\f\v")
2093 (if (eq (char-before) ?\\)
2094 (forward-char))
2095 (forward-line 1)
2096 (if (< (point) simple-ws-beg)
2097 ;; Might happen if comments after the macro were skipped
2098 ;; over.
2099 (setq simple-ws-beg (point)))
2101 (goto-char cpp-beg)
2102 t)))
2104 ((/= (save-excursion
2105 (skip-chars-forward " \t\n\r\f\v" simple-ws-beg)
2106 (setq next-rung-pos (point)))
2107 simple-ws-beg)
2108 ;; Skipped over comments. Must put point at the end of
2109 ;; the simple ws at point since we might be after a line
2110 ;; comment or cpp directive that's been partially
2111 ;; narrowed out, and we can't risk marking the simple ws
2112 ;; at the end of it.
2113 (goto-char next-rung-pos)
2116 ((and c-opt-cpp-prefix
2117 (save-excursion
2118 (and (< (skip-syntax-backward "w_") 0)
2119 (progn (setq next-rung-pos (point))
2120 (looking-at c-noise-macro-name-re)))))
2121 ;; Skipped over a noise macro
2122 (goto-char next-rung-pos)
2123 t)))
2125 ;; We've searched over a piece of non-white syntactic ws. See if this
2126 ;; can be cached.
2127 (setq next-rung-pos (point))
2128 (skip-chars-backward " \t\f\v")
2130 (if (or
2131 ;; Cache if we started either from a marked rung or from a
2132 ;; completely uncached position.
2133 rung-is-marked
2134 (not (get-text-property (1- simple-ws-beg) 'c-in-sws))
2136 ;; Cache if there's a marked rung in the encountered simple ws.
2137 (save-excursion
2138 (skip-chars-backward " \t\n\r\f\v")
2139 (text-property-any (point) (min (1+ next-rung-pos) (point-max))
2140 'c-is-sws t)))
2142 (progn
2143 (c-debug-sws-msg
2144 "c-backward-sws caching [%s..%s] - [%s..%s] (min %s)"
2145 (point) (1+ next-rung-pos)
2146 simple-ws-beg (min (1+ rung-pos) (point-max))
2147 (point-min))
2149 ;; Remove the properties for any nested ws that might be cached.
2150 ;; Only necessary for `c-is-sws' since `c-in-sws' will be set
2151 ;; anyway.
2152 (c-remove-is-sws (1+ next-rung-pos) simple-ws-beg)
2153 (unless (and rung-is-marked (= simple-ws-beg rung-pos))
2154 (let ((rung-end-pos (min (1+ rung-pos) (point-max))))
2155 (unless (get-text-property (1- rung-end-pos) 'c-is-sws)
2156 ;; Remove any `c-in-sws' property from the last char of
2157 ;; the rung before we mark it with `c-is-sws', so that we
2158 ;; won't connect with the remains of a broken "ladder".
2159 (c-remove-in-sws (1- rung-end-pos) rung-end-pos))
2160 (c-put-is-sws simple-ws-beg
2161 rung-end-pos)
2162 (setq rung-is-marked t)))
2163 (c-put-in-sws (setq simple-ws-beg (point)
2164 last-put-in-sws-pos simple-ws-beg)
2165 rung-pos)
2166 (c-put-is-sws (setq rung-pos simple-ws-beg)
2167 (1+ next-rung-pos)))
2169 (c-debug-sws-msg
2170 "c-backward-sws not caching [%s..%s] - [%s..%s] (min %s)"
2171 (point) (1+ next-rung-pos)
2172 simple-ws-beg (min (1+ rung-pos) (point-max))
2173 (point-min))
2174 (setq rung-pos next-rung-pos
2175 simple-ws-beg (point))
2178 ;; Make sure that the newly marked `c-in-sws' region doesn't connect to
2179 ;; another one before the point (which might occur when editing inside a
2180 ;; comment or macro).
2181 (when (eq last-put-in-sws-pos (point))
2182 (cond ((< (point-min) last-put-in-sws-pos)
2183 (c-debug-sws-msg
2184 "c-backward-sws clearing at %s for cache separation"
2185 (1- last-put-in-sws-pos))
2186 (c-remove-in-sws (1- last-put-in-sws-pos)
2187 last-put-in-sws-pos))
2188 ((> (point-min) 1)
2189 ;; If at bob and the buffer is narrowed, we have to clear the
2190 ;; character we're standing on instead since there might be a
2191 ;; `c-in-sws' before (point-min). In this case it's necessary
2192 ;; to clear both properties.
2193 (c-debug-sws-msg
2194 "c-backward-sws clearing thoroughly at %s for cache separation"
2195 last-put-in-sws-pos)
2196 (c-remove-is-and-in-sws last-put-in-sws-pos
2197 (1+ last-put-in-sws-pos)))))
2198 ))))
2201 ;; Other whitespace tools
2202 (defun c-partial-ws-p (beg end)
2203 ;; Is the region (beg end) WS, and is there WS (or BOB/EOB) next to the
2204 ;; region? This is a "heuristic" function. .....
2206 ;; The motivation for the second bit is to check whether removing this
2207 ;; region would coalesce two symbols.
2209 ;; FIXME!!! This function doesn't check virtual semicolons in any way. Be
2210 ;; careful about using this function for, e.g. AWK. (2007/3/7)
2211 (save-excursion
2212 (let ((end+1 (min (1+ end) (point-max))))
2213 (or (progn (goto-char (max (point-min) (1- beg)))
2214 (c-skip-ws-forward end)
2215 (eq (point) end))
2216 (progn (goto-char beg)
2217 (c-skip-ws-forward end+1)
2218 (eq (point) end+1))))))
2220 ;; A system for finding noteworthy parens before the point.
2222 (defconst c-state-cache-too-far 5000)
2223 ;; A maximum comfortable scanning distance, e.g. between
2224 ;; `c-state-cache-good-pos' and "HERE" (where we call c-parse-state). When
2225 ;; this distance is exceeded, we take "emergency measures", e.g. by clearing
2226 ;; the cache and starting again from point-min or a beginning of defun. This
2227 ;; value can be tuned for efficiency or set to a lower value for testing.
2229 (defvar c-state-cache nil)
2230 (make-variable-buffer-local 'c-state-cache)
2231 ;; The state cache used by `c-parse-state' to cut down the amount of
2232 ;; searching. It's the result from some earlier `c-parse-state' call. See
2233 ;; `c-parse-state''s doc string for details of its structure.
2235 ;; The use of the cached info is more effective if the next
2236 ;; `c-parse-state' call is on a line close by the one the cached state
2237 ;; was made at; the cache can actually slow down a little if the
2238 ;; cached state was made very far back in the buffer. The cache is
2239 ;; most effective if `c-parse-state' is used on each line while moving
2240 ;; forward.
2242 (defvar c-state-cache-good-pos 1)
2243 (make-variable-buffer-local 'c-state-cache-good-pos)
2244 ;; This is a position where `c-state-cache' is known to be correct, or
2245 ;; nil (see below). It's a position inside one of the recorded unclosed
2246 ;; parens or the top level, but not further nested inside any literal or
2247 ;; subparen that is closed before the last recorded position.
2249 ;; The exact position is chosen to try to be close to yet earlier than
2250 ;; the position where `c-state-cache' will be called next. Right now
2251 ;; the heuristic is to set it to the position after the last found
2252 ;; closing paren (of any type) before the line on which
2253 ;; `c-parse-state' was called. That is chosen primarily to work well
2254 ;; with refontification of the current line.
2256 ;; 2009-07-28: When `c-state-point-min' and the last position where
2257 ;; `c-parse-state' or for which `c-invalidate-state-cache' was called, are
2258 ;; both in the same literal, there is no such "good position", and
2259 ;; c-state-cache-good-pos is then nil. This is the ONLY circumstance in which
2260 ;; it can be nil. In this case, `c-state-point-min-literal' will be non-nil.
2262 ;; 2009-06-12: In a brace desert, c-state-cache-good-pos may also be in
2263 ;; the middle of the desert, as long as it is not within a brace pair
2264 ;; recorded in `c-state-cache' or a paren/bracket pair.
2266 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2267 ;; We maintain a simple cache of positions which aren't in a literal, so as to
2268 ;; speed up testing for non-literality.
2269 (defconst c-state-nonlit-pos-interval 3000)
2270 ;; The approximate interval between entries in `c-state-nonlit-pos-cache'.
2272 (defvar c-state-nonlit-pos-cache nil)
2273 (make-variable-buffer-local 'c-state-nonlit-pos-cache)
2274 ;; A list of buffer positions which are known not to be in a literal or a cpp
2275 ;; construct. This is ordered with higher positions at the front of the list.
2276 ;; Only those which are less than `c-state-nonlit-pos-cache-limit' are valid.
2278 (defvar c-state-nonlit-pos-cache-limit 1)
2279 (make-variable-buffer-local 'c-state-nonlit-pos-cache-limit)
2280 ;; An upper limit on valid entries in `c-state-nonlit-pos-cache'. This is
2281 ;; reduced by buffer changes, and increased by invocations of
2282 ;; `c-state-literal-at'.
2284 (defvar c-state-semi-nonlit-pos-cache nil)
2285 (make-variable-buffer-local 'c-state-semi-nonlit-pos-cache)
2286 ;; A list of elements which are either buffer positions (when such positions
2287 ;; are not in literals) or lists of the form (POS TYPE START), where POS is
2288 ;; a buffer position inside a literal, TYPE is the type of the literal
2289 ;; ('string, 'c, or 'c++) and START is the start of the literal.
2291 (defvar c-state-semi-nonlit-pos-cache-limit 1)
2292 (make-variable-buffer-local 'c-state-semi-nonlit-pos-cache-limit)
2293 ;; An upper limit on valid entries in `c-state-semi-nonlit-pos-cache'. This
2294 ;; is reduced by buffer changes, and increased by invocations of
2295 ;; `c-parse-ps-state-below'.
2297 (defsubst c-truncate-semi-nonlit-pos-cache (pos)
2298 ;; Truncate the upper bound of the cache `c-state-semi-nonlit-pos-cache' to
2299 ;; POS, if it is higher than that position.
2300 (setq c-state-semi-nonlit-pos-cache-limit
2301 (min c-state-semi-nonlit-pos-cache-limit pos)))
2303 (defun c-state-semi-pp-to-literal (here &optional not-in-delimiter)
2304 ;; Do a parse-partial-sexp from a position in the buffer before HERE which
2305 ;; isn't in a literal, and return information about HERE, either:
2306 ;; (STATE TYPE BEG) if HERE is in a literal; or
2307 ;; (STATE) otherwise,
2308 ;; where STATE is the parsing state at HERE, TYPE is the type of the literal
2309 ;; enclosing HERE, (one of 'string, 'c, 'c++) and BEG is the starting
2310 ;; position of that literal (including the delimiter).
2312 ;; Unless NOT-IN-DELIMITER is non-nil, when TO is inside a two-character
2313 ;; comment opener, this is recognized as being in a comment literal.
2315 ;; Only elements 3 (in a string), 4 (in a comment), 5 (following a quote), 7
2316 ;; (comment type), and 8 (start of comment/string), and possibly 10 (in
2317 ;; newer Emacsen only, the syntax of a position after a potential first char
2318 ;; of a two char construct) of STATE are valid.
2319 (save-excursion
2320 (save-restriction
2321 (widen)
2322 (save-match-data
2323 (let* ((base-and-state (c-parse-ps-state-below here))
2324 (base (car base-and-state))
2325 (s (cdr base-and-state))
2326 (s (parse-partial-sexp base here nil nil s))
2328 (cond
2329 ((or (nth 3 s) (nth 4 s)) ; in a string or comment
2330 (setq ty (cond
2331 ((nth 3 s) 'string)
2332 ((nth 7 s) 'c++)
2333 (t 'c)))
2334 (list s ty (nth 8 s)))
2336 ((and (not not-in-delimiter) ; inside a comment starter
2337 (not (bobp))
2338 (progn (backward-char)
2339 (and (not (and (memq 'category-properties c-emacs-features)
2340 (looking-at "\\s!")))
2341 (looking-at c-comment-start-regexp))))
2342 (setq ty (if (looking-at c-block-comment-start-regexp) 'c 'c++))
2343 (list s ty (point)))
2345 (t (list s))))))))
2347 (defun c-state-full-pp-to-literal (here &optional not-in-delimiter)
2348 ;; This function will supersede c-state-pp-to-literal.
2350 ;; Do a parse-partial-sexp from a position in the buffer before HERE which
2351 ;; isn't in a literal, and return information about HERE, either:
2352 ;; (STATE TYPE (BEG . END)) if HERE is in a literal; or
2353 ;; (STATE) otherwise,
2354 ;; where STATE is the parsing state at HERE, TYPE is the type of the literal
2355 ;; enclosing HERE, (one of 'string, 'c, 'c++) and (BEG . END) is the
2356 ;; boundaries of that literal (including the delimiters).
2358 ;; Unless NOT-IN-DELIMITER is non-nil, when TO is inside a two-character
2359 ;; comment opener, this is recognized as being in a comment literal.
2361 ;; Only elements 3 (in a string), 4 (in a comment), 5 (following a quote), 7
2362 ;; (comment type), and 8 (start of comment/string), and possibly 10 (in
2363 ;; newer Emacsen only, the syntax of a position after a potential first char
2364 ;; of a two char construct) of STATE are valid.
2365 (save-excursion
2366 (save-restriction
2367 (widen)
2368 (save-match-data
2369 (let* ((base-and-state (c-parse-ps-state-below here))
2370 (base (car base-and-state))
2371 (s (cdr base-and-state))
2372 (s (parse-partial-sexp base here nil nil s))
2373 ty start)
2374 (cond
2375 ((or (nth 3 s) (nth 4 s)) ; in a string or comment
2376 (setq ty (cond
2377 ((nth 3 s) 'string)
2378 ((nth 7 s) 'c++)
2379 (t 'c)))
2380 (setq start (nth 8 s))
2381 (parse-partial-sexp here (point-max)
2382 nil ; TARGETDEPTH
2383 nil ; STOPBEFORE
2384 s ; OLDSTATE
2385 'syntax-table) ; stop at end of literal
2386 (list s ty (cons start (point))))
2388 ((and (not not-in-delimiter) ; inside a comment starter
2389 (not (bobp))
2390 (progn (backward-char)
2391 (and (not (and (memq 'category-properties c-emacs-features)
2392 (looking-at "\\s!")))
2393 (looking-at c-comment-start-regexp))))
2394 (setq ty (if (looking-at c-block-comment-start-regexp) 'c 'c++)
2395 start (point))
2396 (forward-comment 1)
2397 (list s ty (cons start (point))))
2399 (t (list s))))))))
2401 (defsubst c-state-pp-to-literal (from to &optional not-in-delimiter)
2402 ;; Do a parse-partial-sexp from FROM to TO, returning either
2403 ;; (STATE TYPE (BEG . END)) if TO is in a literal; or
2404 ;; (STATE) otherwise,
2405 ;; where STATE is the parsing state at TO, TYPE is the type of the literal
2406 ;; (one of 'c, 'c++, 'string) and (BEG . END) is the boundaries of the literal,
2407 ;; including the delimiters.
2409 ;; Unless NOT-IN-DELIMITER is non-nil, when TO is inside a two-character
2410 ;; comment opener, this is recognized as being in a comment literal.
2412 ;; Only elements 3 (in a string), 4 (in a comment), 5 (following a quote),
2413 ;; 7 (comment type) and 8 (start of comment/string) (and possibly 9) of
2414 ;; STATE are valid.
2415 (save-excursion
2416 (save-match-data
2417 (let ((s (parse-partial-sexp from to))
2418 ty co-st)
2419 (cond
2420 ((or (nth 3 s) (nth 4 s)) ; in a string or comment
2421 (setq ty (cond
2422 ((nth 3 s) 'string)
2423 ((nth 7 s) 'c++)
2424 (t 'c)))
2425 (parse-partial-sexp (point) (point-max)
2426 nil ; TARGETDEPTH
2427 nil ; STOPBEFORE
2428 s ; OLDSTATE
2429 'syntax-table) ; stop at end of literal
2430 `(,s ,ty (,(nth 8 s) . ,(point))))
2432 ((and (not not-in-delimiter) ; inside a comment starter
2433 (not (bobp))
2434 (progn (backward-char)
2435 (and (not (looking-at "\\s!"))
2436 (looking-at c-comment-start-regexp))))
2437 (setq ty (if (looking-at c-block-comment-start-regexp) 'c 'c++)
2438 co-st (point))
2439 (forward-comment 1)
2440 `(,s ,ty (,co-st . ,(point))))
2442 (t `(,s)))))))
2444 (defun c-cache-to-parse-ps-state (elt)
2445 ;; Create a list suitable to use as the old-state parameter to
2446 ;; `parse-partial-sexp', out of ELT. ELT is either just a number, a buffer
2447 ;; position, or it is a list (POS TYPE STARTING-POS). Here POS is the
2448 ;; buffer position the other elements are pertinent for, TYPE is either 'c
2449 ;; or 'c++ (for a comment) or a character (for a string delimiter) or t
2450 ;; (meaning a string fence opened the string), STARTING-POS is the starting
2451 ;; position of the comment or string.
2452 (if (consp elt)
2453 (let ((depth 0) (containing nil) (last nil)
2454 in-string in-comment (after-quote nil)
2455 (min-depth 0) com-style com-str-start (intermediate nil)
2456 (between-syntax nil)
2457 (type (cadr elt)))
2458 (setq com-str-start (car (cddr elt)))
2459 (cond
2460 ((or (numberp type) (eq type t)) ; A string
2461 (setq in-string type))
2462 ((memq type '(c c++)) ; A comment
2463 (setq in-comment t
2464 com-style (if (eq type 'c++) 1 nil)))
2465 (t (c-benign-error "Invalid type %s in c-cache-to-parse-ps-state"
2466 elt)))
2467 (list depth containing last
2468 in-string in-comment after-quote
2469 min-depth com-style com-str-start
2470 intermediate nil))
2471 (copy-tree '(0 nil nil nil nil nil 0 nil nil nil nil))))
2473 (defun c-parse-ps-state-to-cache (state)
2474 ;; Convert STATE, a `parse-partial-sexp' state valid at POINT, to an element
2475 ;; for the `c-state-semi-nonlit-pos-cache' cache. This is either POINT
2476 ;; (when point is not in a literal) or a list (POINT TYPE STARTING-POS),
2477 ;; where TYPE is the type of the literal, either 'string, 'c, or 'c++, and
2478 ;; STARTING-POS is the starting position of the comment or string.
2479 (cond
2480 ((nth 3 state) ; A string
2481 (list (point) (nth 3 state) (nth 8 state)))
2482 ((nth 4 state) ; A comment
2483 (list (point)
2484 (if (eq (nth 7 state) 1) 'c++ 'c)
2485 (nth 8 state)))
2486 (t ; Neither string nor comment.
2487 (point))))
2489 (defsubst c-ps-state-cache-pos (elt)
2490 ;; Get the buffer position from ELT, an element from the cache
2491 ;; `c-state-semi-nonlit-pos-cache'.
2492 (if (atom elt)
2494 (car elt)))
2496 (defun c-parse-ps-state-below (here)
2497 ;; Given a buffer position HERE, Return a cons (CACHE-POS . STATE), where
2498 ;; CACHE-POS is a position not very far before HERE for which the
2499 ;; parse-partial-sexp STATE is valid. Note that the only valid elements of
2500 ;; STATE are those concerning comments and strings; STATE is the state of a
2501 ;; null `parse-partial-sexp' scan when CACHE-POS is not in a comment or
2502 ;; string.
2503 (save-excursion
2504 (save-restriction
2505 (widen)
2506 (let ((c c-state-semi-nonlit-pos-cache)
2507 elt state pos npos high-elt)
2508 ;; Trim the cache to take account of buffer changes.
2509 (while (and c (> (c-ps-state-cache-pos (car c))
2510 c-state-semi-nonlit-pos-cache-limit))
2511 (setq c (cdr c)))
2512 (setq c-state-semi-nonlit-pos-cache c)
2514 (while (and c (> (c-ps-state-cache-pos (car c)) here))
2515 (setq high-elt (car c))
2516 (setq c (cdr c)))
2517 (setq pos (or (and c (c-ps-state-cache-pos (car c)))
2518 (point-min)))
2520 (if high-elt
2521 (setq state (c-cache-to-parse-ps-state (car c)))
2522 (setq elt (if c (car c) (point-min)))
2523 (setq state
2524 (if c
2525 (c-cache-to-parse-ps-state (car c))
2526 (copy-tree '(0 nil nil nil nil nil 0 nil nil nil nil))))
2527 (while
2528 ;; Add an element to `c-state-semi-nonlit-pos-cache' each iteration.
2529 (<= (setq npos (+ pos c-state-nonlit-pos-interval)) here)
2530 (setq state (parse-partial-sexp pos npos nil nil state))
2531 (setq elt (c-parse-ps-state-to-cache state))
2532 (setq c-state-semi-nonlit-pos-cache
2533 (cons elt c-state-semi-nonlit-pos-cache))
2534 (setq pos npos)))
2536 (if (> pos c-state-semi-nonlit-pos-cache-limit)
2537 (setq c-state-semi-nonlit-pos-cache-limit pos))
2539 (cons pos state)))))
2541 (defun c-state-safe-place (here)
2542 ;; Return a buffer position before HERE which is "safe", i.e. outside any
2543 ;; string, comment, or macro.
2545 ;; NOTE: This function manipulates `c-state-nonlit-pos-cache'. This cache
2546 ;; MAY NOT contain any positions within macros, since macros are frequently
2547 ;; turned into comments by use of the `c-cpp-delimiter' category properties.
2548 ;; We cannot rely on this mechanism whilst determining a cache pos since
2549 ;; this function is also called from outwith `c-parse-state'.
2550 (save-restriction
2551 (widen)
2552 (save-excursion
2553 (let ((c c-state-nonlit-pos-cache)
2554 pos npos high-pos lit macro-beg macro-end)
2555 ;; Trim the cache to take account of buffer changes.
2556 (while (and c (> (car c) c-state-nonlit-pos-cache-limit))
2557 (setq c (cdr c)))
2558 (setq c-state-nonlit-pos-cache c)
2560 (while (and c (> (car c) here))
2561 (setq high-pos (car c))
2562 (setq c (cdr c)))
2563 (setq pos (or (car c) (point-min)))
2565 (unless high-pos
2566 (while
2567 ;; Add an element to `c-state-nonlit-pos-cache' each iteration.
2568 (and
2569 (setq npos
2570 (when (<= (+ pos c-state-nonlit-pos-interval) here)
2571 (+ pos c-state-nonlit-pos-interval)))
2573 ;; Test for being in a literal. If so, go to after it.
2574 (progn
2575 (setq lit (car (cddr (c-state-pp-to-literal pos npos))))
2576 (or (null lit)
2577 (prog1 (<= (cdr lit) here)
2578 (setq npos (cdr lit)))))
2580 ;; Test for being in a macro. If so, go to after it.
2581 (progn
2582 (goto-char npos)
2583 (setq macro-beg
2584 (and (c-beginning-of-macro) (/= (point) npos) (point)))
2585 (when macro-beg
2586 (c-syntactic-end-of-macro)
2587 (or (eobp) (forward-char))
2588 (setq macro-end (point)))
2589 (or (null macro-beg)
2590 (prog1 (<= macro-end here)
2591 (setq npos macro-end)))))
2593 (setq pos npos)
2594 (setq c-state-nonlit-pos-cache (cons pos c-state-nonlit-pos-cache)))
2595 ;; Add one extra element above HERE so as to to avoid the previous
2596 ;; expensive calculation when the next call is close to the current
2597 ;; one. This is especially useful when inside a large macro.
2598 (when npos
2599 (setq c-state-nonlit-pos-cache
2600 (cons npos c-state-nonlit-pos-cache))))
2602 (if (> pos c-state-nonlit-pos-cache-limit)
2603 (setq c-state-nonlit-pos-cache-limit pos))
2604 pos))))
2606 (defun c-state-literal-at (here)
2607 ;; If position HERE is inside a literal, return (START . END), the
2608 ;; boundaries of the literal (which may be outside the accessible bit of the
2609 ;; buffer). Otherwise, return nil.
2611 ;; This function is almost the same as `c-literal-limits'. Previously, it
2612 ;; differed in that it was a lower level function, and that it rigorously
2613 ;; followed the syntax from BOB. `c-literal-limits' is now (2011-12)
2614 ;; virtually identical to this function.
2615 (save-restriction
2616 (widen)
2617 (save-excursion
2618 (let ((pos (c-state-safe-place here)))
2619 (car (cddr (c-state-pp-to-literal pos here)))))))
2621 (defsubst c-state-lit-beg (pos)
2622 ;; Return the start of the literal containing POS, or POS itself.
2623 (or (car (c-state-literal-at pos))
2624 pos))
2626 (defsubst c-state-cache-non-literal-place (pos state)
2627 ;; Return a position outside of a string/comment/macro at or before POS.
2628 ;; STATE is the parse-partial-sexp state at POS.
2629 (let ((res (if (or (nth 3 state) ; in a string?
2630 (nth 4 state)) ; in a comment?
2631 (nth 8 state)
2632 pos)))
2633 (save-excursion
2634 (goto-char res)
2635 (if (c-beginning-of-macro)
2636 (point)
2637 res))))
2639 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2640 ;; Stuff to do with point-min, and coping with any literal there.
2641 (defvar c-state-point-min 1)
2642 (make-variable-buffer-local 'c-state-point-min)
2643 ;; This is (point-min) when `c-state-cache' was last calculated. A change of
2644 ;; narrowing is likely to affect the parens that are visible before the point.
2646 (defvar c-state-point-min-lit-type nil)
2647 (make-variable-buffer-local 'c-state-point-min-lit-type)
2648 (defvar c-state-point-min-lit-start nil)
2649 (make-variable-buffer-local 'c-state-point-min-lit-start)
2650 ;; These two variables define the literal, if any, containing point-min.
2651 ;; Their values are, respectively, 'string, c, or c++, and the start of the
2652 ;; literal. If there's no literal there, they're both nil.
2654 (defvar c-state-min-scan-pos 1)
2655 (make-variable-buffer-local 'c-state-min-scan-pos)
2656 ;; This is the earliest buffer-pos from which scanning can be done. It is
2657 ;; either the end of the literal containing point-min, or point-min itself.
2658 ;; It becomes nil if the buffer is changed earlier than this point.
2659 (defun c-state-get-min-scan-pos ()
2660 ;; Return the lowest valid scanning pos. This will be the end of the
2661 ;; literal enclosing point-min, or point-min itself.
2662 (or c-state-min-scan-pos
2663 (save-restriction
2664 (save-excursion
2665 (widen)
2666 (goto-char c-state-point-min-lit-start)
2667 (if (eq c-state-point-min-lit-type 'string)
2668 (forward-sexp)
2669 (forward-comment 1))
2670 (setq c-state-min-scan-pos (point))))))
2672 (defun c-state-mark-point-min-literal ()
2673 ;; Determine the properties of any literal containing POINT-MIN, setting the
2674 ;; variables `c-state-point-min-lit-type', `c-state-point-min-lit-start',
2675 ;; and `c-state-min-scan-pos' accordingly. The return value is meaningless.
2676 (let ((p-min (point-min))
2677 lit)
2678 (save-restriction
2679 (widen)
2680 (setq lit (c-state-literal-at p-min))
2681 (if lit
2682 (setq c-state-point-min-lit-type
2683 (save-excursion
2684 (goto-char (car lit))
2685 (cond
2686 ((looking-at c-block-comment-start-regexp) 'c)
2687 ((looking-at c-line-comment-starter) 'c++)
2688 (t 'string)))
2689 c-state-point-min-lit-start (car lit)
2690 c-state-min-scan-pos (cdr lit))
2691 (setq c-state-point-min-lit-type nil
2692 c-state-point-min-lit-start nil
2693 c-state-min-scan-pos p-min)))))
2696 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2697 ;; A variable which signals a brace dessert - helpful for reducing the number
2698 ;; of fruitless backward scans.
2699 (defvar c-state-brace-pair-desert nil)
2700 (make-variable-buffer-local 'c-state-brace-pair-desert)
2701 ;; Used only in `c-append-lower-brace-pair-to-state-cache'. It is set when
2702 ;; that defun has searched backwards for a brace pair and not found one. Its
2703 ;; value is either nil or a cons (PA . FROM), where PA is the position of the
2704 ;; enclosing opening paren/brace/bracket which bounds the backwards search (or
2705 ;; nil when at top level) and FROM is where the backward search started. It
2706 ;; is reset to nil in `c-invalidate-state-cache'.
2709 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2710 ;; Lowish level functions/macros which work directly on `c-state-cache', or a
2711 ;; list of like structure.
2712 (defmacro c-state-cache-top-lparen (&optional cache)
2713 ;; Return the address of the top left brace/bracket/paren recorded in CACHE
2714 ;; (default `c-state-cache') (or nil).
2715 (let ((cash (or cache 'c-state-cache)))
2716 `(if (consp (car ,cash))
2717 (caar ,cash)
2718 (car ,cash))))
2720 (defmacro c-state-cache-top-paren (&optional cache)
2721 ;; Return the address of the latest brace/bracket/paren (whether left or
2722 ;; right) recorded in CACHE (default `c-state-cache') or nil.
2723 (let ((cash (or cache 'c-state-cache)))
2724 `(if (consp (car ,cash))
2725 (cdar ,cash)
2726 (car ,cash))))
2728 (defmacro c-state-cache-after-top-paren (&optional cache)
2729 ;; Return the position just after the latest brace/bracket/paren (whether
2730 ;; left or right) recorded in CACHE (default `c-state-cache') or nil.
2731 (let ((cash (or cache 'c-state-cache)))
2732 `(if (consp (car ,cash))
2733 (cdar ,cash)
2734 (and (car ,cash)
2735 (1+ (car ,cash))))))
2737 (defun c-get-cache-scan-pos (here)
2738 ;; From the state-cache, determine the buffer position from which we might
2739 ;; scan forward to HERE to update this cache. This position will be just
2740 ;; after a paren/brace/bracket recorded in the cache, if possible, otherwise
2741 ;; return the earliest position in the accessible region which isn't within
2742 ;; a literal. If the visible portion of the buffer is entirely within a
2743 ;; literal, return NIL.
2744 (let ((c c-state-cache) elt)
2745 ;(while (>= (or (c-state-cache-top-lparen c) 1) here)
2746 (while (and c
2747 (>= (c-state-cache-top-lparen c) here))
2748 (setq c (cdr c)))
2750 (setq elt (car c))
2751 (cond
2752 ((consp elt)
2753 (if (> (cdr elt) here)
2754 (1+ (car elt))
2755 (cdr elt)))
2756 (elt (1+ elt))
2757 ((<= (c-state-get-min-scan-pos) here)
2758 (c-state-get-min-scan-pos))
2759 (t nil))))
2761 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2762 ;; Variables which keep track of preprocessor constructs.
2763 (defvar c-state-old-cpp-beg-marker nil)
2764 (make-variable-buffer-local 'c-state-old-cpp-beg-marker)
2765 (defvar c-state-old-cpp-beg nil)
2766 (make-variable-buffer-local 'c-state-old-cpp-beg)
2767 (defvar c-state-old-cpp-end-marker nil)
2768 (make-variable-buffer-local 'c-state-old-cpp-end-marker)
2769 (defvar c-state-old-cpp-end nil)
2770 (make-variable-buffer-local 'c-state-old-cpp-end)
2771 ;; These are the limits of the macro containing point at the previous call of
2772 ;; `c-parse-state', or nil.
2774 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2775 ;; Defuns which analyze the buffer, yet don't change `c-state-cache'.
2776 (defun c-get-fallback-scan-pos (here)
2777 ;; Return a start position for building `c-state-cache' from
2778 ;; scratch. This will be at the top level, 2 defuns back.
2779 (save-excursion
2780 ;; Go back 2 bods, but ignore any bogus positions returned by
2781 ;; beginning-of-defun (i.e. open paren in column zero).
2782 (goto-char here)
2783 (let ((cnt 2))
2784 (while (not (or (bobp) (zerop cnt)))
2785 (c-beginning-of-defun-1) ; Pure elisp BOD.
2786 (if (eq (char-after) ?\{)
2787 (setq cnt (1- cnt)))))
2788 (point)))
2790 (defun c-state-balance-parens-backwards (here- here+ top)
2791 ;; Return the position of the opening paren/brace/bracket before HERE- which
2792 ;; matches the outermost close p/b/b between HERE+ and TOP. Except when
2793 ;; there's a macro, HERE- and HERE+ are the same. Like this:
2795 ;; ............................................
2796 ;; | |
2797 ;; ( [ ( .........#macro.. ) ( ) ] )
2798 ;; ^ ^ ^ ^
2799 ;; | | | |
2800 ;; return HERE- HERE+ TOP
2802 ;; If there aren't enough opening paren/brace/brackets, return the position
2803 ;; of the outermost one found, or HERE- if there are none. If there are no
2804 ;; closing p/b/bs between HERE+ and TOP, return HERE-. HERE-/+ and TOP
2805 ;; must not be inside literals. Only the accessible portion of the buffer
2806 ;; will be scanned.
2808 ;; PART 1: scan from `here+' up to `top', accumulating ")"s which enclose
2809 ;; `here'. Go round the next loop each time we pass over such a ")". These
2810 ;; probably match "("s before `here-'.
2811 (let (pos pa ren+1 lonely-rens)
2812 (save-excursion
2813 (save-restriction
2814 (narrow-to-region (point-min) top) ; This can move point, sometimes.
2815 (setq pos here+)
2816 (c-safe
2817 (while
2818 (setq ren+1 (c-sc-scan-lists pos 1 1)) ; might signal
2819 (setq lonely-rens (cons ren+1 lonely-rens)
2820 pos ren+1)))))
2822 ;; PART 2: Scan back before `here-' searching for the "("s
2823 ;; matching/mismatching the ")"s found above. We only need to direct the
2824 ;; caller to scan when we've encountered unmatched right parens.
2825 (setq pos here-)
2826 (when lonely-rens
2827 (c-safe
2828 (while
2829 (and lonely-rens ; actual values aren't used.
2830 (setq pa (c-sc-scan-lists pos -1 1)))
2831 (setq pos pa)
2832 (setq lonely-rens (cdr lonely-rens)))))
2833 pos))
2835 (defun c-parse-state-get-strategy (here good-pos)
2836 ;; Determine the scanning strategy for adjusting `c-parse-state', attempting
2837 ;; to minimize the amount of scanning. HERE is the pertinent position in
2838 ;; the buffer, GOOD-POS is a position where `c-state-cache' (possibly with
2839 ;; its head trimmed) is known to be good, or nil if there is no such
2840 ;; position.
2842 ;; The return value is a list, one of the following:
2844 ;; o - ('forward START-POINT) - scan forward from START-POINT,
2845 ;; which is not less than the highest position in `c-state-cache' below HERE,
2846 ;; which is after GOOD-POS.
2847 ;; o - ('backward nil) - scan backwards (from HERE).
2848 ;; o - ('back-and-forward START-POINT) - like 'forward, but when HERE is earlier
2849 ;; than GOOD-POS.
2850 ;; o - ('BOD START-POINT) - scan forwards from START-POINT, which is at the
2851 ;; top level.
2852 ;; o - ('IN-LIT nil) - point is inside the literal containing point-min.
2853 (let ((cache-pos (c-get-cache-scan-pos here)) ; highest position below HERE in cache (or 1)
2854 BOD-pos ; position of 2nd BOD before HERE.
2855 strategy ; 'forward, 'backward, 'BOD, or 'IN-LIT.
2856 start-point
2857 how-far) ; putative scanning distance.
2858 (setq good-pos (or good-pos (c-state-get-min-scan-pos)))
2859 (cond
2860 ((< here (c-state-get-min-scan-pos))
2861 (setq strategy 'IN-LIT
2862 start-point nil
2863 cache-pos nil
2864 how-far 0))
2865 ((<= good-pos here)
2866 (setq strategy 'forward
2867 start-point (max good-pos cache-pos)
2868 how-far (- here start-point)))
2869 ((< (- good-pos here) (- here cache-pos)) ; FIXME!!! ; apply some sort of weighting.
2870 (setq strategy 'backward
2871 how-far (- good-pos here)))
2873 (setq strategy 'back-and-forward
2874 start-point cache-pos
2875 how-far (- here start-point))))
2877 ;; Might we be better off starting from the top level, two defuns back,
2878 ;; instead? This heuristic no longer works well in C++, where
2879 ;; declarations inside namespace brace blocks are frequently placed at
2880 ;; column zero. (2015-11-10): Remove the condition on C++ Mode.
2881 (when (and (or (not (memq 'col-0-paren c-emacs-features))
2882 open-paren-in-column-0-is-defun-start)
2883 ;; (not (c-major-mode-is 'c++-mode))
2884 (> how-far c-state-cache-too-far))
2885 (setq BOD-pos (c-get-fallback-scan-pos here)) ; somewhat EXPENSIVE!!!
2886 (if (< (- here BOD-pos) how-far)
2887 (setq strategy 'BOD
2888 start-point BOD-pos)))
2890 (list strategy start-point)))
2893 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2894 ;; Routines which change `c-state-cache' and associated values.
2895 (defun c-renarrow-state-cache ()
2896 ;; The region (more precisely, point-min) has changed since we
2897 ;; calculated `c-state-cache'. Amend `c-state-cache' accordingly.
2898 (if (< (point-min) c-state-point-min)
2899 ;; If point-min has MOVED BACKWARDS then we drop the state completely.
2900 ;; It would be possible to do a better job here and recalculate the top
2901 ;; only.
2902 (progn
2903 (c-state-mark-point-min-literal)
2904 (setq c-state-cache nil
2905 c-state-cache-good-pos c-state-min-scan-pos
2906 c-state-brace-pair-desert nil))
2908 ;; point-min has MOVED FORWARD.
2910 ;; Is the new point-min inside a (different) literal?
2911 (unless (and c-state-point-min-lit-start ; at prev. point-min
2912 (< (point-min) (c-state-get-min-scan-pos)))
2913 (c-state-mark-point-min-literal))
2915 ;; Cut off a bit of the tail from `c-state-cache'.
2916 (let ((ptr (cons nil c-state-cache))
2918 (while (and (setq pa (c-state-cache-top-lparen (cdr ptr)))
2919 (>= pa (point-min)))
2920 (setq ptr (cdr ptr)))
2922 (when (consp ptr)
2923 (if (or (eq (cdr ptr) c-state-cache)
2924 (and (consp (cadr ptr))
2925 (> (cdr (cadr ptr)) (point-min)))) ; Our new point-min is
2926 ; inside a recorded
2927 ; brace pair.
2928 (setq c-state-cache nil
2929 c-state-cache-good-pos c-state-min-scan-pos)
2930 (setcdr ptr nil)
2931 (setq c-state-cache-good-pos (1+ (c-state-cache-top-lparen))))
2934 (setq c-state-point-min (point-min)))
2936 (defun c-append-lower-brace-pair-to-state-cache (from here &optional upper-lim)
2937 ;; If there is a brace pair preceding FROM in the buffer, at the same level
2938 ;; of nesting (not necessarily immediately preceding), push a cons onto
2939 ;; `c-state-cache' to represent it. FROM must not be inside a literal. If
2940 ;; UPPER-LIM is non-nil, we append the highest brace pair whose "}" is below
2941 ;; UPPER-LIM.
2943 ;; Return non-nil when this has been done.
2945 ;; The situation it copes with is this transformation:
2947 ;; OLD: { (.) {...........}
2948 ;; ^ ^
2949 ;; FROM HERE
2951 ;; NEW: { {....} (.) {.........
2952 ;; ^ ^ ^
2953 ;; LOWER BRACE PAIR HERE or HERE
2955 ;; This routine should be fast. Since it can get called a LOT, we maintain
2956 ;; `c-state-brace-pair-desert', a small cache of "failures", such that we
2957 ;; reduce the time wasted in repeated fruitless searches in brace deserts.
2958 (save-excursion
2959 (save-restriction
2960 (let* (new-cons
2961 (cache-pos (c-state-cache-top-lparen)) ; might be nil.
2962 (macro-start-or-from
2963 (progn (goto-char from)
2964 (c-beginning-of-macro)
2965 (point)))
2966 (bra ; Position of "{".
2967 ;; Don't start scanning in the middle of a CPP construct unless
2968 ;; it contains HERE - these constructs, in Emacs, are "commented
2969 ;; out" with category properties.
2970 (if (eq (c-get-char-property macro-start-or-from 'category)
2971 'c-cpp-delimiter)
2972 macro-start-or-from
2973 from))
2974 ce) ; Position of "}"
2975 (or upper-lim (setq upper-lim from))
2977 ;; If we're essentially repeating a fruitless search, just give up.
2978 (unless (and c-state-brace-pair-desert
2979 (eq cache-pos (car c-state-brace-pair-desert))
2980 (or (null (car c-state-brace-pair-desert))
2981 (> from (car c-state-brace-pair-desert)))
2982 (<= from (cdr c-state-brace-pair-desert)))
2983 ;; DESERT-LIM. Avoid repeated searching through the cached desert.
2984 (let ((desert-lim
2985 (and c-state-brace-pair-desert
2986 (eq cache-pos (car c-state-brace-pair-desert))
2987 (>= from (cdr c-state-brace-pair-desert))
2988 (cdr c-state-brace-pair-desert)))
2989 ;; CACHE-LIM. This limit will be necessary when an opening
2990 ;; paren at `cache-pos' has just had its matching close paren
2991 ;; inserted into the buffer. `cache-pos' continues to be a
2992 ;; search bound, even though the algorithm below would skip
2993 ;; over the new paren pair.
2994 (cache-lim (and cache-pos (< cache-pos from) cache-pos)))
2995 (narrow-to-region
2996 (cond
2997 ((and desert-lim cache-lim)
2998 (max desert-lim cache-lim))
2999 (desert-lim)
3000 (cache-lim)
3001 ((point-min)))
3002 ;; The top limit is EOB to ensure that `bra' is inside the
3003 ;; accessible part of the buffer at the next scan operation.
3004 (1+ (buffer-size))))
3006 ;; In the next pair of nested loops, the inner one moves back past a
3007 ;; pair of (mis-)matching parens or brackets; the outer one moves
3008 ;; back over a sequence of unmatched close brace/paren/bracket each
3009 ;; time round.
3010 (while
3011 (progn
3012 (c-safe
3013 (while
3014 (and (setq ce (c-sc-scan-lists bra -1 -1)) ; back past )/]/}; might signal
3015 (setq bra (c-sc-scan-lists ce -1 1)) ; back past (/[/{; might signal
3016 (or (> bra here) ;(> ce here)
3017 (and
3018 (< ce here)
3019 (or (not (eq (char-after bra) ?\{))
3020 (and (goto-char bra)
3021 (c-beginning-of-macro)
3022 (< (point) macro-start-or-from))))))))
3023 (and ce (< ce bra)))
3024 (setq bra ce)) ; If we just backed over an unbalanced closing
3025 ; brace, ignore it.
3027 (if (and ce (< ce here) (< bra ce) (eq (char-after bra) ?\{))
3028 ;; We've found the desired brace-pair.
3029 (progn
3030 (setq new-cons (cons bra (1+ ce)))
3031 (cond
3032 ((consp (car c-state-cache))
3033 (setcar c-state-cache new-cons))
3034 ((and (numberp (car c-state-cache)) ; probably never happens
3035 (< ce (car c-state-cache)))
3036 (setcdr c-state-cache
3037 (cons new-cons (cdr c-state-cache))))
3038 (t (setq c-state-cache (cons new-cons c-state-cache)))))
3040 ;; We haven't found a brace pair. Record this in the cache.
3041 (setq c-state-brace-pair-desert
3042 (cons (if (and ce (< bra ce) (> ce here)) ; {..} straddling HERE?
3044 (point-min))
3045 (min here from)))))))))
3047 (defsubst c-state-push-any-brace-pair (bra+1 macro-start-or-here)
3048 ;; If BRA+1 is nil, do nothing. Otherwise, BRA+1 is the buffer position
3049 ;; following a {, and that brace has a (mis-)matching } (or ]), and we
3050 ;; "push" "a" brace pair onto `c-state-cache'.
3052 ;; Here "push" means overwrite the top element if it's itself a brace-pair,
3053 ;; otherwise push it normally.
3055 ;; The brace pair we push is normally the one surrounding BRA+1, but if the
3056 ;; latter is inside a macro, not being a macro containing
3057 ;; MACRO-START-OR-HERE, we scan backwards through the buffer for a non-macro
3058 ;; base pair. This latter case is assumed to be rare.
3060 ;; Note: POINT is not preserved in this routine.
3061 (if bra+1
3062 (if (or (> bra+1 macro-start-or-here)
3063 (progn (goto-char bra+1)
3064 (not (c-beginning-of-macro))))
3065 (setq c-state-cache
3066 (cons (cons (1- bra+1)
3067 (c-sc-scan-lists bra+1 1 1))
3068 (if (consp (car c-state-cache))
3069 (cdr c-state-cache)
3070 c-state-cache)))
3071 ;; N.B. This defsubst codes one method for the simple, normal case,
3072 ;; and a more sophisticated, slower way for the general case. Don't
3073 ;; eliminate this defsubst - it's a speed optimization.
3074 (c-append-lower-brace-pair-to-state-cache (1- bra+1) (point-max)))))
3076 (defun c-append-to-state-cache (from here)
3077 ;; Scan the buffer from FROM to HERE, adding elements into `c-state-cache'
3078 ;; for braces etc. Return a candidate for `c-state-cache-good-pos'.
3080 ;; FROM must be after the latest brace/paren/bracket in `c-state-cache', if
3081 ;; any. Typically, it is immediately after it. It must not be inside a
3082 ;; literal.
3083 (let ((here-bol (c-point 'bol here))
3084 (macro-start-or-here
3085 (save-excursion (goto-char here)
3086 (if (c-beginning-of-macro)
3087 (point)
3088 here)))
3089 pa+1 ; pos just after an opening PAren (or brace).
3090 (ren+1 from) ; usually a pos just after an closing paREN etc.
3091 ; Is actually the pos. to scan for a (/{/[ from,
3092 ; which sometimes is after a silly )/}/].
3093 paren+1 ; Pos after some opening or closing paren.
3094 paren+1s ; A list of `paren+1's; used to determine a
3095 ; good-pos.
3096 bra+1 ; just after L bra-ce.
3097 bra+1s ; list of OLD values of bra+1.
3098 mstart) ; start of a macro.
3100 (save-excursion
3101 (save-restriction
3102 (narrow-to-region (point-min) here)
3103 ;; Each time round the following loop, we enter a successively deeper
3104 ;; level of brace/paren nesting. (Except sometimes we "continue at
3105 ;; the existing level".) `pa+1' is a pos inside an opening
3106 ;; brace/paren/bracket, usually just after it.
3107 (while
3108 (progn
3109 ;; Each time round the next loop moves forward over an opening then
3110 ;; a closing brace/bracket/paren. This loop is white hot, so it
3111 ;; plays ugly tricks to go fast. DON'T PUT ANYTHING INTO THIS
3112 ;; LOOP WHICH ISN'T ABSOLUTELY NECESSARY!!! It terminates when a
3113 ;; call of `scan-lists' signals an error, which happens when there
3114 ;; are no more b/b/p's to scan.
3115 (c-safe
3116 (while t
3117 (setq pa+1 (c-sc-scan-lists ren+1 1 -1) ; Into (/{/[; might signal
3118 paren+1s (cons pa+1 paren+1s))
3119 (setq ren+1 (c-sc-scan-lists pa+1 1 1)) ; Out of )/}/]; might signal
3120 (if (and (eq (char-before pa+1) ?{)) ; Check for a macro later.
3121 (setq bra+1 pa+1))
3122 (setcar paren+1s ren+1)))
3124 (if (and pa+1 (> pa+1 ren+1))
3125 ;; We've just entered a deeper nesting level.
3126 (progn
3127 ;; Insert the brace pair (if present) and the single open
3128 ;; paren/brace/bracket into `c-state-cache' It cannot be
3129 ;; inside a macro, except one around point, because of what
3130 ;; `c-neutralize-syntax-in-CPP' has done.
3131 (c-state-push-any-brace-pair bra+1 macro-start-or-here)
3132 ;; Insert the opening brace/bracket/paren position.
3133 (setq c-state-cache (cons (1- pa+1) c-state-cache))
3134 ;; Clear admin stuff for the next more nested part of the scan.
3135 (setq ren+1 pa+1 pa+1 nil bra+1 nil bra+1s nil)
3136 t) ; Carry on the loop
3138 ;; All open p/b/b's at this nesting level, if any, have probably
3139 ;; been closed by matching/mismatching ones. We're probably
3140 ;; finished - we just need to check for having found an
3141 ;; unmatched )/}/], which we ignore. Such a )/}/] can't be in a
3142 ;; macro, due the action of `c-neutralize-syntax-in-CPP'.
3143 (c-safe (setq ren+1 (c-sc-scan-lists ren+1 1 1)))))) ; acts as loop control.
3145 ;; Record the final, innermost, brace-pair if there is one.
3146 (c-state-push-any-brace-pair bra+1 macro-start-or-here)
3148 ;; Determine a good pos
3149 (while (and (setq paren+1 (car paren+1s))
3150 (> (if (> paren+1 macro-start-or-here)
3151 paren+1
3152 (goto-char paren+1)
3153 (setq mstart (and (c-beginning-of-macro)
3154 (point)))
3155 (or mstart paren+1))
3156 here-bol))
3157 (setq paren+1s (cdr paren+1s)))
3158 (cond
3159 ((and paren+1 mstart)
3160 (min paren+1 mstart))
3161 (paren+1)
3162 (t from))))))
3164 (defun c-remove-stale-state-cache (start-point here pps-point)
3165 ;; Remove stale entries from the `c-cache-state', i.e. those which will
3166 ;; not be in it when it is amended for position HERE. This may involve
3167 ;; replacing a CONS element for a brace pair containing HERE with its car.
3168 ;; Additionally, the "outermost" open-brace entry before HERE will be
3169 ;; converted to a cons if the matching close-brace is below HERE.
3171 ;; START-POINT is a "maximal" "safe position" - there must be no open
3172 ;; parens/braces/brackets between START-POINT and HERE.
3174 ;; As a second thing, calculate the result of parse-partial-sexp at
3175 ;; PPS-POINT, w.r.t. START-POINT. The motivation here is that
3176 ;; `c-state-cache-good-pos' may become PPS-POINT, but the caller may need to
3177 ;; adjust it to get outside a string/comment. (Sorry about this! The code
3178 ;; needs to be FAST).
3180 ;; Return a list (GOOD-POS SCAN-BACK-POS CONS-SEPARATED PPS-STATE), where
3181 ;; o - GOOD-POS is a position where the new value `c-state-cache' is known
3182 ;; to be good (we aim for this to be as high as possible);
3183 ;; o - SCAN-BACK-POS, if not nil, indicates there may be a brace pair
3184 ;; preceding POS which needs to be recorded in `c-state-cache'. It is a
3185 ;; position to scan backwards from. It is the position of the "{" of the
3186 ;; last element to be removed from `c-state-cache', when that elt is a
3187 ;; cons, otherwise nil.
3188 ;; o - CONS-SEPARATED is t when a cons element in `c-state-cache' has been
3189 ;; replaced by its car because HERE lies inside the brace pair represented
3190 ;; by the cons.
3191 ;; o - PPS-STATE is the parse-partial-sexp state at PPS-POINT.
3192 (save-excursion
3193 (save-restriction
3194 (narrow-to-region 1 (point-max))
3195 (let* ((in-macro-start ; start of macro containing HERE or nil.
3196 (save-excursion
3197 (goto-char here)
3198 (and (c-beginning-of-macro)
3199 (point))))
3200 (start-point-actual-macro-start ; Start of macro containing
3201 ; start-point or nil
3202 (and (< start-point here)
3203 (save-excursion
3204 (goto-char start-point)
3205 (and (c-beginning-of-macro)
3206 (point)))))
3207 (start-point-actual-macro-end ; End of this macro, (maybe
3208 ; HERE), or nil.
3209 (and start-point-actual-macro-start
3210 (save-excursion
3211 (goto-char start-point-actual-macro-start)
3212 (c-end-of-macro)
3213 (point))))
3214 pps-state ; Will be 9 or 10 elements long.
3216 upper-lim ; ,beyond which `c-state-cache' entries are removed
3217 scan-back-pos
3218 cons-separated
3219 pair-beg pps-point-state target-depth)
3221 ;; Remove entries beyond HERE. Also remove any entries inside
3222 ;; a macro, unless HERE is in the same macro.
3223 (setq upper-lim
3224 (if (or (null c-state-old-cpp-beg)
3225 (and (> here c-state-old-cpp-beg)
3226 (< here c-state-old-cpp-end)))
3227 here
3228 (min here c-state-old-cpp-beg)))
3229 (while (and c-state-cache (>= (c-state-cache-top-lparen) upper-lim))
3230 (setq scan-back-pos (car-safe (car c-state-cache)))
3231 (setq c-state-cache (cdr c-state-cache)))
3233 ;; If `upper-lim' is inside the last recorded brace pair, remove its
3234 ;; RBrace and indicate we'll need to search backwards for a previous
3235 ;; brace pair.
3236 (when (and c-state-cache
3237 (consp (car c-state-cache))
3238 (> (cdar c-state-cache) upper-lim))
3239 (setcar c-state-cache (caar c-state-cache))
3240 (setq scan-back-pos (car c-state-cache)
3241 cons-separated t))
3243 ;; The next loop jumps forward out of a nested level of parens each
3244 ;; time round; the corresponding elements in `c-state-cache' are
3245 ;; removed. `pos' is just after the brace-pair or the open paren at
3246 ;; (car c-state-cache). There can be no open parens/braces/brackets
3247 ;; between `start-point'/`start-point-actual-macro-start' and HERE,
3248 ;; due to the interface spec to this function.
3249 (setq pos (if (and start-point-actual-macro-end
3250 (not (eq start-point-actual-macro-start
3251 in-macro-start)))
3252 (1+ start-point-actual-macro-end) ; get outside the macro as
3253 ; marked by a `category' text property.
3254 start-point))
3255 (goto-char pos)
3256 (while (and c-state-cache
3257 (or (numberp (car c-state-cache)) ; Have we a { at all?
3258 (cdr c-state-cache))
3259 (< (point) here))
3260 (cond
3261 ((null pps-state) ; first time through
3262 (setq target-depth -1))
3263 ((eq (car pps-state) target-depth) ; found closing ),},]
3264 (setq target-depth (1- (car pps-state))))
3265 ;; Do nothing when we've merely reached pps-point.
3268 ;; Scan!
3269 (setq pps-state
3270 (c-sc-parse-partial-sexp
3271 (point) (if (< (point) pps-point) pps-point here)
3272 target-depth
3273 nil pps-state))
3275 (if (= (point) pps-point)
3276 (setq pps-point-state pps-state))
3278 (when (eq (car pps-state) target-depth)
3279 (setq pos (point)) ; POS is now just after an R-paren/brace.
3280 (cond
3281 ((and (consp (car c-state-cache))
3282 (eq (point) (cdar c-state-cache)))
3283 ;; We've just moved out of the paren pair containing the brace-pair
3284 ;; at (car c-state-cache). `pair-beg' is where the open paren is,
3285 ;; and is potentially where the open brace of a cons in
3286 ;; c-state-cache will be.
3287 (setq pair-beg (car-safe (cdr c-state-cache))
3288 c-state-cache (cdr-safe (cdr c-state-cache)))) ; remove {}pair + containing Lparen.
3289 ((numberp (car c-state-cache))
3290 (setq pair-beg (car c-state-cache)
3291 c-state-cache (cdr c-state-cache))) ; remove this
3292 ; containing Lparen
3293 ((numberp (cadr c-state-cache))
3294 (setq pair-beg (cadr c-state-cache)
3295 c-state-cache (cddr c-state-cache))) ; Remove a paren pair
3296 ; together with enclosed brace pair.
3297 ;; (t nil) ; Ignore an unmated Rparen.
3300 (if (< (point) pps-point)
3301 (setq pps-state (c-sc-parse-partial-sexp
3302 (point) pps-point
3303 nil nil ; TARGETDEPTH, STOPBEFORE
3304 pps-state)))
3306 ;; If the last paren pair we moved out of was actually a brace pair,
3307 ;; insert it into `c-state-cache'.
3308 (when (and pair-beg (eq (char-after pair-beg) ?{))
3309 (if (consp (car-safe c-state-cache))
3310 (setq c-state-cache (cdr c-state-cache)))
3311 (setq c-state-cache (cons (cons pair-beg pos)
3312 c-state-cache)))
3314 (list pos scan-back-pos cons-separated pps-state)))))
3316 (defun c-remove-stale-state-cache-backwards (here)
3317 ;; Strip stale elements of `c-state-cache' by moving backwards through the
3318 ;; buffer, and inform the caller of the scenario detected.
3320 ;; HERE is the position we're setting `c-state-cache' for.
3321 ;; CACHE-POS (a locally bound variable) is just after the latest recorded
3322 ;; position in `c-state-cache' before HERE, or a position at or near
3323 ;; point-min which isn't in a literal.
3325 ;; This function must only be called only when (> `c-state-cache-good-pos'
3326 ;; HERE). Usually the gap between CACHE-POS and HERE is large. It is thus
3327 ;; optimized to eliminate (or minimize) scanning between these two
3328 ;; positions.
3330 ;; Return a three element list (GOOD-POS SCAN-BACK-POS FWD-FLAG), where:
3331 ;; o - GOOD-POS is a "good position", where `c-state-cache' is valid, or
3332 ;; could become so after missing elements are inserted into
3333 ;; `c-state-cache'. This is JUST AFTER an opening or closing
3334 ;; brace/paren/bracket which is already in `c-state-cache' or just before
3335 ;; one otherwise. exceptionally (when there's no such b/p/b handy) the BOL
3336 ;; before `here''s line, or the start of the literal containing it.
3337 ;; o - SCAN-BACK-POS, if non-nil, indicates there may be a brace pair
3338 ;; preceding POS which isn't recorded in `c-state-cache'. It is a position
3339 ;; to scan backwards from.
3340 ;; o - FWD-FLAG, if non-nil, indicates there may be parens/braces between
3341 ;; POS and HERE which aren't recorded in `c-state-cache'.
3343 ;; The comments in this defun use "paren" to mean parenthesis or square
3344 ;; bracket (as contrasted with a brace), and "(" and ")" likewise.
3346 ;; . {..} (..) (..) ( .. { } ) (...) ( .... . ..)
3347 ;; | | | | | |
3348 ;; CP E here D C good
3349 (let ((cache-pos (c-get-cache-scan-pos here)) ; highest position below HERE in cache (or 1)
3350 (pos c-state-cache-good-pos)
3351 pa ren ; positions of "(" and ")"
3352 dropped-cons ; whether the last element dropped from `c-state-cache'
3353 ; was a cons (representing a brace-pair)
3354 good-pos ; see above.
3355 lit ; (START . END) of a literal containing some point.
3356 here-lit-start here-lit-end ; bounds of literal containing `here'
3357 ; or `here' itself.
3358 here- here+ ; start/end of macro around HERE, or HERE
3359 (here-bol (c-point 'bol here))
3360 (too-far-back (max (- here c-state-cache-too-far) (point-min))))
3362 ;; Remove completely irrelevant entries from `c-state-cache'.
3363 (while (and c-state-cache
3364 (>= (setq pa (c-state-cache-top-lparen)) here))
3365 (setq dropped-cons (consp (car c-state-cache)))
3366 (setq c-state-cache (cdr c-state-cache))
3367 (setq pos pa))
3368 ;; At this stage, (>= pos here);
3369 ;; (< (c-state-cache-top-lparen) here) (or is nil).
3371 (cond
3372 ((and (consp (car c-state-cache))
3373 (> (cdar c-state-cache) here))
3374 ;; CASE 1: The top of the cache is a brace pair which now encloses
3375 ;; `here'. As good-pos, return the address. of the "{". Since we've no
3376 ;; knowledge of what's inside these braces, we have no alternative but
3377 ;; to direct the caller to scan the buffer from the opening brace.
3378 (setq pos (caar c-state-cache))
3379 (setcar c-state-cache pos)
3380 (list (1+ pos) pos t)) ; return value. We've just converted a brace pair
3381 ; entry into a { entry, so the caller needs to
3382 ; search for a brace pair before the {.
3384 ;; `here' might be inside a literal. Check for this.
3385 ((progn
3386 (setq lit (c-state-literal-at here)
3387 here-lit-start (or (car lit) here)
3388 here-lit-end (or (cdr lit) here))
3389 ;; Has `here' just "newly entered" a macro?
3390 (save-excursion
3391 (goto-char here-lit-start)
3392 (if (and (c-beginning-of-macro)
3393 (or (null c-state-old-cpp-beg)
3394 (not (= (point) c-state-old-cpp-beg))))
3395 (progn
3396 (setq here- (point))
3397 (c-end-of-macro)
3398 (setq here+ (point)))
3399 (setq here- here-lit-start
3400 here+ here-lit-end)))
3402 ;; `here' might be nested inside any depth of parens (or brackets but
3403 ;; not braces). Scan backwards to find the outermost such opening
3404 ;; paren, if there is one. This will be the scan position to return.
3405 (save-restriction
3406 (narrow-to-region cache-pos (point-max))
3407 (setq pos (c-state-balance-parens-backwards here- here+ pos)))
3408 nil)) ; for the cond
3410 ((< pos here-lit-start)
3411 ;; CASE 2: Address of outermost ( or [ which now encloses `here', but
3412 ;; didn't enclose the (previous) `c-state-cache-good-pos'. If there is
3413 ;; a brace pair preceding this, it will already be in `c-state-cache',
3414 ;; unless there was a brace pair after it, i.e. there'll only be one to
3415 ;; scan for if we've just deleted one.
3416 (list pos (and dropped-cons pos) t)) ; Return value.
3418 ;; `here' isn't enclosed in a (previously unrecorded) bracket/paren.
3419 ;; Further forward scanning isn't needed, but we still need to find a
3420 ;; GOOD-POS. Step out of all enclosing "("s on HERE's line.
3421 ((progn
3422 (save-restriction
3423 (narrow-to-region here-bol (point-max))
3424 (setq pos here-lit-start)
3425 (c-safe (while (setq pa (c-sc-scan-lists pos -1 1))
3426 (setq pos pa)))) ; might signal
3427 nil)) ; for the cond
3429 ((save-restriction
3430 (narrow-to-region too-far-back (point-max))
3431 (setq ren (c-safe (c-sc-scan-lists pos -1 -1))))
3432 ;; CASE 3: After a }/)/] before `here''s BOL.
3433 (list (1+ ren) (and dropped-cons pos) nil)) ; Return value
3435 ((progn (setq good-pos (c-state-lit-beg (c-point 'bopl here-bol)))
3436 (>= cache-pos good-pos))
3437 ;; CASE 3.5: Just after an existing entry in `c-state-cache' on `here''s
3438 ;; line or the previous line.
3439 (list cache-pos nil nil))
3442 ;; CASE 4; Best of a bad job: BOL before `here-bol', or beginning of
3443 ;; literal containing it.
3444 (list good-pos (and dropped-cons good-pos) nil)))))
3447 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
3448 ;; Externally visible routines.
3450 (defun c-state-cache-init ()
3451 (setq c-state-cache nil
3452 c-state-cache-good-pos 1
3453 c-state-nonlit-pos-cache nil
3454 c-state-nonlit-pos-cache-limit 1
3455 c-state-semi-nonlit-pos-cache nil
3456 c-state-semi-nonlit-pos-cache-limit 1
3457 c-state-brace-pair-desert nil
3458 c-state-point-min 1
3459 c-state-point-min-lit-type nil
3460 c-state-point-min-lit-start nil
3461 c-state-min-scan-pos 1
3462 c-state-old-cpp-beg nil
3463 c-state-old-cpp-end nil)
3464 (c-state-mark-point-min-literal))
3466 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
3467 ;; Debugging routines to dump `c-state-cache' in a "replayable" form.
3468 ;; (defmacro c-sc-de (elt) ; "c-state-cache-dump-element"
3469 ;; `(format ,(concat "(setq " (symbol-name elt) " %s) ") ,elt))
3470 ;; (defmacro c-sc-qde (elt) ; "c-state-cache-quote-dump-element"
3471 ;; `(format ,(concat "(setq " (symbol-name elt) " '%s) ") ,elt))
3472 ;; (defun c-state-dump ()
3473 ;; ;; For debugging.
3474 ;; ;(message
3475 ;; (concat
3476 ;; (c-sc-qde c-state-cache)
3477 ;; (c-sc-de c-state-cache-good-pos)
3478 ;; (c-sc-qde c-state-nonlit-pos-cache)
3479 ;; (c-sc-de c-state-nonlit-pos-cache-limit)
3480 ;; (c-sc-qde c-state-brace-pair-desert)
3481 ;; (c-sc-de c-state-point-min)
3482 ;; (c-sc-de c-state-point-min-lit-type)
3483 ;; (c-sc-de c-state-point-min-lit-start)
3484 ;; (c-sc-de c-state-min-scan-pos)
3485 ;; (c-sc-de c-state-old-cpp-beg)
3486 ;; (c-sc-de c-state-old-cpp-end)))
3487 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
3489 (defun c-invalidate-state-cache-1 (here)
3490 ;; Invalidate all info on `c-state-cache' that applies to the buffer at HERE
3491 ;; or higher and set `c-state-cache-good-pos' accordingly. The cache is
3492 ;; left in a consistent state.
3494 ;; This is much like `c-whack-state-after', but it never changes a paren
3495 ;; pair element into an open paren element. Doing that would mean that the
3496 ;; new open paren wouldn't have the required preceding paren pair element.
3498 ;; This function is called from c-before-change.
3500 ;; The caches of non-literals:
3501 ;; Note that we use "<=" for the possibility of the second char of a two-char
3502 ;; comment opener being typed; this would invalidate any cache position at
3503 ;; HERE.
3504 (if (<= here c-state-nonlit-pos-cache-limit)
3505 (setq c-state-nonlit-pos-cache-limit (1- here)))
3506 (c-truncate-semi-nonlit-pos-cache here)
3508 ;; `c-state-cache':
3509 ;; Case 1: if `here' is in a literal containing point-min, everything
3510 ;; becomes (or is already) nil.
3511 (if (or (null c-state-cache-good-pos)
3512 (< here (c-state-get-min-scan-pos)))
3513 (setq c-state-cache nil
3514 c-state-cache-good-pos nil
3515 c-state-min-scan-pos nil)
3517 ;; Truncate `c-state-cache' and set `c-state-cache-good-pos' to a value
3518 ;; below `here'. To maintain its consistency, we may need to insert a new
3519 ;; brace pair.
3520 (let ((here-bol (c-point 'bol here))
3521 too-high-pa ; recorded {/(/[ next above or just below here, or nil.
3522 dropped-cons ; was the last removed element a brace pair?
3524 ;; The easy bit - knock over-the-top bits off `c-state-cache'.
3525 (while (and c-state-cache
3526 (>= (setq pa (c-state-cache-top-paren)) here))
3527 (setq dropped-cons (consp (car c-state-cache))
3528 too-high-pa (c-state-cache-top-lparen)
3529 c-state-cache (cdr c-state-cache)))
3531 ;; Do we need to add in an earlier brace pair, having lopped one off?
3532 (if (and dropped-cons
3533 (<= too-high-pa here))
3534 (c-append-lower-brace-pair-to-state-cache too-high-pa here here-bol))
3535 (setq c-state-cache-good-pos (or (c-state-cache-after-top-paren)
3536 (c-state-get-min-scan-pos)))))
3538 ;; The brace-pair desert marker:
3539 (when (car c-state-brace-pair-desert)
3540 (if (< here (car c-state-brace-pair-desert))
3541 (setq c-state-brace-pair-desert nil)
3542 (if (< here (cdr c-state-brace-pair-desert))
3543 (setcdr c-state-brace-pair-desert here)))))
3545 (defun c-parse-state-1 ()
3546 ;; Find and record all noteworthy parens between some good point earlier in
3547 ;; the file and point. That good point is at least the beginning of the
3548 ;; top-level construct we are in, or the beginning of the preceding
3549 ;; top-level construct if we aren't in one.
3551 ;; The returned value is a list of the noteworthy parens with the last one
3552 ;; first. If an element in the list is an integer, it's the position of an
3553 ;; open paren (of any type) which has not been closed before the point. If
3554 ;; an element is a cons, it gives the position of a closed BRACE paren
3555 ;; pair[*]; the car is the start brace position and the cdr is the position
3556 ;; following the closing brace. Only the last closed brace paren pair
3557 ;; before each open paren and before the point is recorded, and thus the
3558 ;; state never contains two cons elements in succession. When a close brace
3559 ;; has no matching open brace (e.g., the matching brace is outside the
3560 ;; visible region), it is not represented in the returned value.
3562 ;; [*] N.B. The close "brace" might be a mismatching close bracket or paren.
3563 ;; This defun explicitly treats mismatching parens/braces/brackets as
3564 ;; matching. It is the open brace which makes it a "brace" pair.
3566 ;; If POINT is within a macro, open parens and brace pairs within
3567 ;; THIS macro MIGHT be recorded. This depends on whether their
3568 ;; syntactic properties have been suppressed by
3569 ;; `c-neutralize-syntax-in-CPP'. This might need fixing (2008-12-11).
3571 ;; Currently no characters which are given paren syntax with the
3572 ;; syntax-table property are recorded, i.e. angle bracket arglist
3573 ;; parens are never present here. Note that this might change.
3575 ;; BUG: This function doesn't cope entirely well with unbalanced
3576 ;; parens in macros. (2008-12-11: this has probably been resolved
3577 ;; by the function `c-neutralize-syntax-in-CPP'.) E.g. in the
3578 ;; following case the brace before the macro isn't balanced with the
3579 ;; one after it:
3581 ;; {
3582 ;; #define X {
3583 ;; }
3585 ;; Note to maintainers: this function DOES get called with point
3586 ;; within comments and strings, so don't assume it doesn't!
3588 ;; This function might do hidden buffer changes.
3589 (let* ((here (point))
3590 (here-bopl (c-point 'bopl))
3591 strategy ; 'forward, 'backward etc..
3592 ;; Candidate positions to start scanning from:
3593 cache-pos ; highest position below HERE already existing in
3594 ; cache (or 1).
3595 good-pos
3596 start-point ; (when scanning forward) a place below HERE where there
3597 ; are no open parens/braces between it and HERE.
3598 bopl-state
3600 cons-separated
3601 scan-backward-pos scan-forward-p) ; used for 'backward.
3602 ;; If POINT-MIN has changed, adjust the cache
3603 (unless (= (point-min) c-state-point-min)
3604 (c-renarrow-state-cache))
3606 ;; Strategy?
3607 (setq res (c-parse-state-get-strategy here c-state-cache-good-pos)
3608 strategy (car res)
3609 start-point (cadr res))
3611 (when (eq strategy 'BOD)
3612 (setq c-state-cache nil
3613 c-state-cache-good-pos start-point))
3615 ;; SCAN!
3616 (cond
3617 ((memq strategy '(forward back-and-forward BOD))
3618 (setq res (c-remove-stale-state-cache start-point here here-bopl))
3619 (setq cache-pos (car res)
3620 scan-backward-pos (cadr res)
3621 cons-separated (car (cddr res))
3622 bopl-state (cadr (cddr res))) ; will be nil if (< here-bopl
3623 ; start-point)
3624 (if (and scan-backward-pos
3625 (or cons-separated (eq strategy 'forward))) ;scan-backward-pos
3626 (c-append-lower-brace-pair-to-state-cache scan-backward-pos here))
3627 (setq good-pos
3628 (c-append-to-state-cache cache-pos here))
3629 (setq c-state-cache-good-pos
3630 (if (and bopl-state
3631 (< good-pos (- here c-state-cache-too-far)))
3632 (c-state-cache-non-literal-place here-bopl bopl-state)
3633 good-pos)))
3635 ((eq strategy 'backward)
3636 (setq res (c-remove-stale-state-cache-backwards here)
3637 good-pos (car res)
3638 scan-backward-pos (cadr res)
3639 scan-forward-p (car (cddr res)))
3640 (if scan-backward-pos
3641 (c-append-lower-brace-pair-to-state-cache scan-backward-pos here))
3642 (setq c-state-cache-good-pos
3643 (if scan-forward-p
3644 (c-append-to-state-cache good-pos here)
3645 good-pos)))
3647 (t ; (eq strategy 'IN-LIT)
3648 (setq c-state-cache nil
3649 c-state-cache-good-pos nil))))
3651 c-state-cache)
3653 (defun c-invalidate-state-cache (here)
3654 ;; This is a wrapper over `c-invalidate-state-cache-1'.
3656 ;; It suppresses the syntactic effect of the < and > (template) brackets and
3657 ;; of all parens in preprocessor constructs, except for any such construct
3658 ;; containing point. We can then call `c-invalidate-state-cache-1' without
3659 ;; worrying further about macros and template delimiters.
3660 (if (eval-when-compile (memq 'category-properties c-emacs-features))
3661 ;; Emacs
3662 (c-with-<->-as-parens-suppressed
3663 (if (and c-state-old-cpp-beg
3664 (< c-state-old-cpp-beg here))
3665 (c-with-all-but-one-cpps-commented-out
3666 c-state-old-cpp-beg
3667 c-state-old-cpp-end
3668 (c-invalidate-state-cache-1 here))
3669 (c-with-cpps-commented-out
3670 (c-invalidate-state-cache-1 here))))
3671 ;; XEmacs
3672 (c-invalidate-state-cache-1 here)))
3674 (defmacro c-state-maybe-marker (place marker)
3675 ;; If PLACE is non-nil, return a marker marking it, otherwise nil.
3676 ;; We (re)use MARKER.
3677 `(and ,place
3678 (or ,marker (setq ,marker (make-marker)))
3679 (set-marker ,marker ,place)))
3681 (defun c-parse-state ()
3682 ;; This is a wrapper over `c-parse-state-1'. See that function for a
3683 ;; description of the functionality and return value.
3685 ;; It suppresses the syntactic effect of the < and > (template) brackets and
3686 ;; of all parens in preprocessor constructs, except for any such construct
3687 ;; containing point. We can then call `c-parse-state-1' without worrying
3688 ;; further about macros and template delimiters.
3689 (let (here-cpp-beg here-cpp-end)
3690 (save-excursion
3691 (when (c-beginning-of-macro)
3692 (setq here-cpp-beg (point))
3693 (unless
3694 (> (setq here-cpp-end (c-syntactic-end-of-macro))
3695 here-cpp-beg)
3696 (setq here-cpp-beg nil here-cpp-end nil))))
3697 ;; FIXME!!! Put in a `condition-case' here to protect the integrity of the
3698 ;; subsystem.
3699 (prog1
3700 (if (eval-when-compile (memq 'category-properties c-emacs-features))
3701 ;; Emacs
3702 (c-with-<->-as-parens-suppressed
3703 (if (and here-cpp-beg (> here-cpp-end here-cpp-beg))
3704 (c-with-all-but-one-cpps-commented-out
3705 here-cpp-beg here-cpp-end
3706 (c-parse-state-1))
3707 (c-with-cpps-commented-out
3708 (c-parse-state-1))))
3709 ;; XEmacs
3710 (c-parse-state-1))
3711 (setq c-state-old-cpp-beg
3712 (c-state-maybe-marker here-cpp-beg c-state-old-cpp-beg-marker)
3713 c-state-old-cpp-end
3714 (c-state-maybe-marker here-cpp-end c-state-old-cpp-end-marker)))))
3716 ;; Debug tool to catch cache inconsistencies. This is called from
3717 ;; 000tests.el.
3718 (defvar c-debug-parse-state nil)
3719 (unless (fboundp 'c-real-parse-state)
3720 (fset 'c-real-parse-state (symbol-function 'c-parse-state)))
3721 (cc-bytecomp-defun c-real-parse-state)
3723 (defvar c-parse-state-point nil)
3724 (defvar c-parse-state-state nil)
3725 (make-variable-buffer-local 'c-parse-state-state)
3726 (defun c-record-parse-state-state ()
3727 (setq c-parse-state-point (point))
3728 (when (markerp (cdr (assq 'c-state-old-cpp-beg c-parse-state-state)))
3729 (move-marker (cdr (assq 'c-state-old-cpp-beg c-parse-state-state)) nil)
3730 (move-marker (cdr (assq 'c-state-old-cpp-end c-parse-state-state)) nil))
3731 (setq c-parse-state-state
3732 (mapcar
3733 (lambda (arg)
3734 (let ((val (symbol-value arg)))
3735 (cons arg
3736 (cond ((consp val) (copy-tree val))
3737 ((markerp val) (copy-marker val))
3738 (t val)))))
3739 '(c-state-cache
3740 c-state-cache-good-pos
3741 c-state-nonlit-pos-cache
3742 c-state-nonlit-pos-cache-limit
3743 c-state-semi-nonlit-pos-cache
3744 c-state-semi-nonlit-pos-cache-limit
3745 c-state-brace-pair-desert
3746 c-state-point-min
3747 c-state-point-min-lit-type
3748 c-state-point-min-lit-start
3749 c-state-min-scan-pos
3750 c-state-old-cpp-beg
3751 c-state-old-cpp-end
3752 c-parse-state-point))))
3753 (defun c-replay-parse-state-state ()
3754 (message "%s"
3755 (concat "(setq "
3756 (mapconcat
3757 (lambda (arg)
3758 (format "%s %s%s" (car arg)
3759 (if (atom (cdr arg)) "" "'")
3760 (if (markerp (cdr arg))
3761 (format "(copy-marker %s)" (marker-position (cdr arg)))
3762 (cdr arg))))
3763 c-parse-state-state " ")
3764 ")")))
3766 (defun c-debug-parse-state-double-cons (state)
3767 (let (state-car conses-not-ok)
3768 (while state
3769 (setq state-car (car state)
3770 state (cdr state))
3771 (if (and (consp state-car)
3772 (consp (car state)))
3773 (setq conses-not-ok t)))
3774 conses-not-ok))
3776 (defun c-debug-parse-state ()
3777 (let ((here (point)) (min-point (point-min)) (res1 (c-real-parse-state)) res2)
3778 (let ((c-state-cache nil)
3779 (c-state-cache-good-pos 1)
3780 (c-state-nonlit-pos-cache nil)
3781 (c-state-nonlit-pos-cache-limit 1)
3782 (c-state-brace-pair-desert nil)
3783 (c-state-point-min 1)
3784 (c-state-point-min-lit-type nil)
3785 (c-state-point-min-lit-start nil)
3786 (c-state-min-scan-pos 1)
3787 (c-state-old-cpp-beg nil)
3788 (c-state-old-cpp-end nil))
3789 (setq res2 (c-real-parse-state)))
3790 (unless (equal res1 res2)
3791 ;; The cache can actually go further back due to the ad-hoc way
3792 ;; the first paren is found, so try to whack off a bit of its
3793 ;; start before complaining.
3794 ;; (save-excursion
3795 ;; (goto-char (or (c-least-enclosing-brace res2) (point)))
3796 ;; (c-beginning-of-defun-1)
3797 ;; (while (not (or (bobp) (eq (char-after) ?{)))
3798 ;; (c-beginning-of-defun-1))
3799 ;; (unless (equal (c-whack-state-before (point) res1) res2)
3800 ;; (message (concat "c-parse-state inconsistency at %s: "
3801 ;; "using cache: %s, from scratch: %s")
3802 ;; here res1 res2)))
3803 (message (concat "c-parse-state inconsistency at %s: "
3804 "using cache: %s, from scratch: %s. POINT-MIN: %s")
3805 here res1 res2 min-point)
3806 (message "Old state:")
3807 (c-replay-parse-state-state))
3809 (when (c-debug-parse-state-double-cons res1)
3810 (message "c-parse-state INVALIDITY at %s: %s"
3811 here res1)
3812 (message "Old state:")
3813 (c-replay-parse-state-state))
3815 (c-record-parse-state-state)
3816 res2 ; res1 correct a cascading series of errors ASAP
3819 (defun c-toggle-parse-state-debug (&optional arg)
3820 (interactive "P")
3821 (setq c-debug-parse-state (c-calculate-state arg c-debug-parse-state))
3822 (fset 'c-parse-state (symbol-function (if c-debug-parse-state
3823 'c-debug-parse-state
3824 'c-real-parse-state)))
3825 (c-keep-region-active)
3826 (message "c-debug-parse-state %sabled"
3827 (if c-debug-parse-state "en" "dis")))
3828 (when c-debug-parse-state
3829 (c-toggle-parse-state-debug 1))
3832 (defun c-whack-state-before (bufpos paren-state)
3833 ;; Whack off any state information from PAREN-STATE which lies
3834 ;; before BUFPOS. Not destructive on PAREN-STATE.
3835 (let* ((newstate (list nil))
3836 (ptr newstate)
3837 car)
3838 (while paren-state
3839 (setq car (car paren-state)
3840 paren-state (cdr paren-state))
3841 (if (< (if (consp car) (car car) car) bufpos)
3842 (setq paren-state nil)
3843 (setcdr ptr (list car))
3844 (setq ptr (cdr ptr))))
3845 (cdr newstate)))
3847 (defun c-whack-state-after (bufpos paren-state)
3848 ;; Whack off any state information from PAREN-STATE which lies at or
3849 ;; after BUFPOS. Not destructive on PAREN-STATE.
3850 (catch 'done
3851 (while paren-state
3852 (let ((car (car paren-state)))
3853 (if (consp car)
3854 ;; just check the car, because in a balanced brace
3855 ;; expression, it must be impossible for the corresponding
3856 ;; close brace to be before point, but the open brace to
3857 ;; be after.
3858 (if (<= bufpos (car car))
3859 nil ; whack it off
3860 (if (< bufpos (cdr car))
3861 ;; its possible that the open brace is before
3862 ;; bufpos, but the close brace is after. In that
3863 ;; case, convert this to a non-cons element. The
3864 ;; rest of the state is before bufpos, so we're
3865 ;; done.
3866 (throw 'done (cons (car car) (cdr paren-state)))
3867 ;; we know that both the open and close braces are
3868 ;; before bufpos, so we also know that everything else
3869 ;; on state is before bufpos.
3870 (throw 'done paren-state)))
3871 (if (<= bufpos car)
3872 nil ; whack it off
3873 ;; it's before bufpos, so everything else should too.
3874 (throw 'done paren-state)))
3875 (setq paren-state (cdr paren-state)))
3876 nil)))
3878 (defun c-most-enclosing-brace (paren-state &optional bufpos)
3879 ;; Return the bufpos of the innermost enclosing open paren before
3880 ;; bufpos, or nil if none was found.
3881 (let (enclosingp)
3882 (or bufpos (setq bufpos 134217727))
3883 (while paren-state
3884 (setq enclosingp (car paren-state)
3885 paren-state (cdr paren-state))
3886 (if (or (consp enclosingp)
3887 (>= enclosingp bufpos))
3888 (setq enclosingp nil)
3889 (setq paren-state nil)))
3890 enclosingp))
3892 (defun c-least-enclosing-brace (paren-state)
3893 ;; Return the bufpos of the outermost enclosing open paren, or nil
3894 ;; if none was found.
3895 (let (pos elem)
3896 (while paren-state
3897 (setq elem (car paren-state)
3898 paren-state (cdr paren-state))
3899 (if (integerp elem)
3900 (setq pos elem)))
3901 pos))
3903 (defun c-safe-position (bufpos paren-state)
3904 ;; Return the closest "safe" position recorded on PAREN-STATE that
3905 ;; is higher up than BUFPOS. Return nil if PAREN-STATE doesn't
3906 ;; contain any. Return nil if BUFPOS is nil, which is useful to
3907 ;; find the closest limit before a given limit that might be nil.
3909 ;; A "safe" position is a position at or after a recorded open
3910 ;; paren, or after a recorded close paren. The returned position is
3911 ;; thus either the first position after a close brace, or the first
3912 ;; position after an enclosing paren, or at the enclosing paren in
3913 ;; case BUFPOS is immediately after it.
3914 (when bufpos
3915 (let (elem)
3916 (catch 'done
3917 (while paren-state
3918 (setq elem (car paren-state))
3919 (if (consp elem)
3920 (cond ((< (cdr elem) bufpos)
3921 (throw 'done (cdr elem)))
3922 ((< (car elem) bufpos)
3923 ;; See below.
3924 (throw 'done (min (1+ (car elem)) bufpos))))
3925 (if (< elem bufpos)
3926 ;; elem is the position at and not after the opening paren, so
3927 ;; we can go forward one more step unless it's equal to
3928 ;; bufpos. This is useful in some cases avoid an extra paren
3929 ;; level between the safe position and bufpos.
3930 (throw 'done (min (1+ elem) bufpos))))
3931 (setq paren-state (cdr paren-state)))))))
3933 (defun c-beginning-of-syntax ()
3934 ;; This is used for `font-lock-beginning-of-syntax-function'. It
3935 ;; goes to the closest previous point that is known to be outside
3936 ;; any string literal or comment. `c-state-cache' is used if it has
3937 ;; a position in the vicinity.
3938 (let* ((paren-state c-state-cache)
3939 elem
3941 (pos (catch 'done
3942 ;; Note: Similar code in `c-safe-position'. The
3943 ;; difference is that we accept a safe position at
3944 ;; the point and don't bother to go forward past open
3945 ;; parens.
3946 (while paren-state
3947 (setq elem (car paren-state))
3948 (if (consp elem)
3949 (cond ((<= (cdr elem) (point))
3950 (throw 'done (cdr elem)))
3951 ((<= (car elem) (point))
3952 (throw 'done (car elem))))
3953 (if (<= elem (point))
3954 (throw 'done elem)))
3955 (setq paren-state (cdr paren-state)))
3956 (point-min))))
3958 (if (> pos (- (point) 4000))
3959 (goto-char pos)
3960 ;; The position is far back. Try `c-beginning-of-defun-1'
3961 ;; (although we can't be entirely sure it will go to a position
3962 ;; outside a comment or string in current emacsen). FIXME:
3963 ;; Consult `syntax-ppss' here.
3964 (c-beginning-of-defun-1)
3965 (if (< (point) pos)
3966 (goto-char pos)))))
3969 ;; Tools for scanning identifiers and other tokens.
3971 (defun c-on-identifier ()
3972 "Return non-nil if the point is on or directly after an identifier.
3973 Keywords are recognized and not considered identifiers. If an
3974 identifier is detected, the returned value is its starting position.
3975 If an identifier ends at the point and another begins at it \(can only
3976 happen in Pike) then the point for the preceding one is returned.
3978 Note that this function might do hidden buffer changes. See the
3979 comment at the start of cc-engine.el for more info."
3981 ;; FIXME: Shouldn't this function handle "operator" in C++?
3983 (save-excursion
3984 (skip-syntax-backward "w_")
3988 ;; Check for a normal (non-keyword) identifier.
3989 (and (looking-at c-symbol-start)
3990 (not (looking-at c-keywords-regexp))
3991 (point))
3993 (when (c-major-mode-is 'pike-mode)
3994 ;; Handle the `<operator> syntax in Pike.
3995 (let ((pos (point)))
3996 (skip-chars-backward "-!%&*+/<=>^|~[]()")
3997 (and (if (< (skip-chars-backward "`") 0)
3999 (goto-char pos)
4000 (eq (char-after) ?\`))
4001 (looking-at c-symbol-key)
4002 (>= (match-end 0) pos)
4003 (point))))
4005 ;; Handle the "operator +" syntax in C++.
4006 (when (and c-overloadable-operators-regexp
4007 (= (c-backward-token-2 0) 0))
4009 (cond ((and (looking-at c-overloadable-operators-regexp)
4010 (or (not c-opt-op-identifier-prefix)
4011 (and (= (c-backward-token-2 1) 0)
4012 (looking-at c-opt-op-identifier-prefix))))
4013 (point))
4015 ((save-excursion
4016 (and c-opt-op-identifier-prefix
4017 (looking-at c-opt-op-identifier-prefix)
4018 (= (c-forward-token-2 1) 0)
4019 (looking-at c-overloadable-operators-regexp)))
4020 (point))))
4024 (defsubst c-simple-skip-symbol-backward ()
4025 ;; If the point is at the end of a symbol then skip backward to the
4026 ;; beginning of it. Don't move otherwise. Return non-nil if point
4027 ;; moved.
4029 ;; This function might do hidden buffer changes.
4030 (or (< (skip-syntax-backward "w_") 0)
4031 (and (c-major-mode-is 'pike-mode)
4032 ;; Handle the `<operator> syntax in Pike.
4033 (let ((pos (point)))
4034 (if (and (< (skip-chars-backward "-!%&*+/<=>^|~[]()") 0)
4035 (< (skip-chars-backward "`") 0)
4036 (looking-at c-symbol-key)
4037 (>= (match-end 0) pos))
4039 (goto-char pos)
4040 nil)))))
4042 (defun c-beginning-of-current-token (&optional back-limit)
4043 ;; Move to the beginning of the current token. Do not move if not
4044 ;; in the middle of one. BACK-LIMIT may be used to bound the
4045 ;; backward search; if given it's assumed to be at the boundary
4046 ;; between two tokens. Return non-nil if the point is moved, nil
4047 ;; otherwise.
4049 ;; This function might do hidden buffer changes.
4050 (let ((start (point)))
4051 (if (looking-at "\\w\\|\\s_")
4052 (skip-syntax-backward "w_" back-limit)
4053 (when (< (skip-syntax-backward ".()" back-limit) 0)
4054 (while (let ((pos (or (and (looking-at c-nonsymbol-token-regexp)
4055 (match-end 0))
4056 ;; `c-nonsymbol-token-regexp' should always match
4057 ;; since we've skipped backward over punctuation
4058 ;; or paren syntax, but consume one char in case
4059 ;; it doesn't so that we don't leave point before
4060 ;; some earlier incorrect token.
4061 (1+ (point)))))
4062 (if (<= pos start)
4063 (goto-char pos))))))
4064 (< (point) start)))
4066 (defun c-end-of-current-token (&optional back-limit)
4067 ;; Move to the end of the current token. Do not move if not in the
4068 ;; middle of one. BACK-LIMIT may be used to bound the backward
4069 ;; search; if given it's assumed to be at the boundary between two
4070 ;; tokens. Return non-nil if the point is moved, nil otherwise.
4072 ;; This function might do hidden buffer changes.
4073 (let ((start (point)))
4074 (cond ((< (skip-syntax-backward "w_" (1- start)) 0)
4075 (skip-syntax-forward "w_"))
4076 ((< (skip-syntax-backward ".()" back-limit) 0)
4077 (while (progn
4078 (if (looking-at c-nonsymbol-token-regexp)
4079 (goto-char (match-end 0))
4080 ;; `c-nonsymbol-token-regexp' should always match since
4081 ;; we've skipped backward over punctuation or paren
4082 ;; syntax, but move forward in case it doesn't so that
4083 ;; we don't leave point earlier than we started with.
4084 (forward-char))
4085 (< (point) start)))))
4086 (> (point) start)))
4088 (defconst c-jump-syntax-balanced
4089 (if (memq 'gen-string-delim c-emacs-features)
4090 "\\w\\|\\s_\\|\\s(\\|\\s)\\|\\s\"\\|\\s|"
4091 "\\w\\|\\s_\\|\\s(\\|\\s)\\|\\s\""))
4093 (defconst c-jump-syntax-unbalanced
4094 (if (memq 'gen-string-delim c-emacs-features)
4095 "\\w\\|\\s_\\|\\s\"\\|\\s|"
4096 "\\w\\|\\s_\\|\\s\""))
4098 (defun c-forward-token-2 (&optional count balanced limit)
4099 "Move forward by tokens.
4100 A token is defined as all symbols and identifiers which aren't
4101 syntactic whitespace \(note that multicharacter tokens like \"==\" are
4102 treated properly). Point is always either left at the beginning of a
4103 token or not moved at all. COUNT specifies the number of tokens to
4104 move; a negative COUNT moves in the opposite direction. A COUNT of 0
4105 moves to the next token beginning only if not already at one. If
4106 BALANCED is true, move over balanced parens, otherwise move into them.
4107 Also, if BALANCED is true, never move out of an enclosing paren.
4109 LIMIT sets the limit for the movement and defaults to the point limit.
4110 The case when LIMIT is set in the middle of a token, comment or macro
4111 is handled correctly, i.e. the point won't be left there.
4113 Return the number of tokens left to move \(positive or negative). If
4114 BALANCED is true, a move over a balanced paren counts as one. Note
4115 that if COUNT is 0 and no appropriate token beginning is found, 1 will
4116 be returned. Thus, a return value of 0 guarantees that point is at
4117 the requested position and a return value less \(without signs) than
4118 COUNT guarantees that point is at the beginning of some token.
4120 Note that this function might do hidden buffer changes. See the
4121 comment at the start of cc-engine.el for more info."
4123 (or count (setq count 1))
4124 (if (< count 0)
4125 (- (c-backward-token-2 (- count) balanced limit))
4127 (let ((jump-syntax (if balanced
4128 c-jump-syntax-balanced
4129 c-jump-syntax-unbalanced))
4130 (last (point))
4131 (prev (point)))
4133 (if (zerop count)
4134 ;; If count is zero we should jump if in the middle of a token.
4135 (c-end-of-current-token))
4137 (save-restriction
4138 (if limit (narrow-to-region (point-min) limit))
4139 (if (/= (point)
4140 (progn (c-forward-syntactic-ws) (point)))
4141 ;; Skip whitespace. Count this as a move if we did in
4142 ;; fact move.
4143 (setq count (max (1- count) 0)))
4145 (if (eobp)
4146 ;; Moved out of bounds. Make sure the returned count isn't zero.
4147 (progn
4148 (if (zerop count) (setq count 1))
4149 (goto-char last))
4151 ;; Use `condition-case' to avoid having the limit tests
4152 ;; inside the loop.
4153 (condition-case nil
4154 (while (and
4155 (> count 0)
4156 (progn
4157 (setq last (point))
4158 (cond ((looking-at jump-syntax)
4159 (goto-char (scan-sexps (point) 1))
4161 ((looking-at c-nonsymbol-token-regexp)
4162 (goto-char (match-end 0))
4164 ;; `c-nonsymbol-token-regexp' above should always
4165 ;; match if there are correct tokens. Try to
4166 ;; widen to see if the limit was set in the
4167 ;; middle of one, else fall back to treating
4168 ;; the offending thing as a one character token.
4169 ((and limit
4170 (save-restriction
4171 (widen)
4172 (looking-at c-nonsymbol-token-regexp)))
4173 nil)
4175 (forward-char)
4176 t))))
4177 (c-forward-syntactic-ws)
4178 (setq prev last
4179 count (1- count)))
4180 (error (goto-char last)))
4182 (when (eobp)
4183 (goto-char prev)
4184 (setq count (1+ count)))))
4186 count)))
4188 (defun c-backward-token-2 (&optional count balanced limit)
4189 "Move backward by tokens.
4190 See `c-forward-token-2' for details."
4192 (or count (setq count 1))
4193 (if (< count 0)
4194 (- (c-forward-token-2 (- count) balanced limit))
4196 (or limit (setq limit (point-min)))
4197 (let ((jump-syntax (if balanced
4198 c-jump-syntax-balanced
4199 c-jump-syntax-unbalanced))
4200 (last (point)))
4202 (if (zerop count)
4203 ;; The count is zero so try to skip to the beginning of the
4204 ;; current token.
4205 (if (> (point)
4206 (progn (c-beginning-of-current-token) (point)))
4207 (if (< (point) limit)
4208 ;; The limit is inside the same token, so return 1.
4209 (setq count 1))
4211 ;; We're not in the middle of a token. If there's
4212 ;; whitespace after the point then we must move backward,
4213 ;; so set count to 1 in that case.
4214 (and (looking-at c-syntactic-ws-start)
4215 ;; If we're looking at a '#' that might start a cpp
4216 ;; directive then we have to do a more elaborate check.
4217 (or (/= (char-after) ?#)
4218 (not c-opt-cpp-prefix)
4219 (save-excursion
4220 (and (= (point)
4221 (progn (beginning-of-line)
4222 (looking-at "[ \t]*")
4223 (match-end 0)))
4224 (or (bobp)
4225 (progn (backward-char)
4226 (not (eq (char-before) ?\\)))))))
4227 (setq count 1))))
4229 ;; Use `condition-case' to avoid having to check for buffer
4230 ;; limits in `backward-char', `scan-sexps' and `goto-char' below.
4231 (condition-case nil
4232 (while (and
4233 (> count 0)
4234 (progn
4235 (c-backward-syntactic-ws)
4236 (backward-char)
4237 (if (looking-at jump-syntax)
4238 (goto-char (scan-sexps (1+ (point)) -1))
4239 ;; This can be very inefficient if there's a long
4240 ;; sequence of operator tokens without any separation.
4241 ;; That doesn't happen in practice, anyway.
4242 (c-beginning-of-current-token))
4243 (>= (point) limit)))
4244 (setq last (point)
4245 count (1- count)))
4246 (error (goto-char last)))
4248 (if (< (point) limit)
4249 (goto-char last))
4251 count)))
4253 (defun c-forward-token-1 (&optional count balanced limit)
4254 "Like `c-forward-token-2' but doesn't treat multicharacter operator
4255 tokens like \"==\" as single tokens, i.e. all sequences of symbol
4256 characters are jumped over character by character. This function is
4257 for compatibility only; it's only a wrapper over `c-forward-token-2'."
4258 (let ((c-nonsymbol-token-regexp "\\s."))
4259 (c-forward-token-2 count balanced limit)))
4261 (defun c-backward-token-1 (&optional count balanced limit)
4262 "Like `c-backward-token-2' but doesn't treat multicharacter operator
4263 tokens like \"==\" as single tokens, i.e. all sequences of symbol
4264 characters are jumped over character by character. This function is
4265 for compatibility only; it's only a wrapper over `c-backward-token-2'."
4266 (let ((c-nonsymbol-token-regexp "\\s."))
4267 (c-backward-token-2 count balanced limit)))
4270 ;; Tools for doing searches restricted to syntactically relevant text.
4272 (defun c-syntactic-re-search-forward (regexp &optional bound noerror
4273 paren-level not-inside-token
4274 lookbehind-submatch)
4275 "Like `re-search-forward', but only report matches that are found
4276 in syntactically significant text. I.e. matches in comments, macros
4277 or string literals are ignored. The start point is assumed to be
4278 outside any comment, macro or string literal, or else the content of
4279 that region is taken as syntactically significant text.
4281 NOERROR, in addition to the values nil, t, and <anything else>
4282 used in `re-search-forward' can also take the values
4283 'before-literal and 'after-literal. In these cases, when BOUND
4284 is also given and is inside a literal, and a search fails, point
4285 will be left, respectively before or after the literal. Be aware
4286 that with 'after-literal, if a string or comment is unclosed at
4287 the end of the buffer, point may be left there, even though it is
4288 inside a literal there.
4290 If PAREN-LEVEL is non-nil, an additional restriction is added to
4291 ignore matches in nested paren sexps. The search will also not go
4292 outside the current list sexp, which has the effect that if the point
4293 should be moved to BOUND when no match is found \(i.e. NOERROR is
4294 neither nil nor t), then it will be at the closing paren if the end of
4295 the current list sexp is encountered first.
4297 If NOT-INSIDE-TOKEN is non-nil, matches in the middle of tokens are
4298 ignored. Things like multicharacter operators and special symbols
4299 \(e.g. \"`()\" in Pike) are handled but currently not floating point
4300 constants.
4302 If LOOKBEHIND-SUBMATCH is non-nil, it's taken as a number of a
4303 subexpression in REGEXP. The end of that submatch is used as the
4304 position to check for syntactic significance. If LOOKBEHIND-SUBMATCH
4305 isn't used or if that subexpression didn't match then the start
4306 position of the whole match is used instead. The \"look behind\"
4307 subexpression is never tested before the starting position, so it
4308 might be a good idea to include \\=\\= as a match alternative in it.
4310 Optimization note: Matches might be missed if the \"look behind\"
4311 subexpression can match the end of nonwhite syntactic whitespace,
4312 i.e. the end of comments or cpp directives. This since the function
4313 skips over such things before resuming the search. It's on the other
4314 hand not safe to assume that the \"look behind\" subexpression never
4315 matches syntactic whitespace.
4317 Bug: Unbalanced parens inside cpp directives are currently not handled
4318 correctly \(i.e. they don't get ignored as they should) when
4319 PAREN-LEVEL is set.
4321 Note that this function might do hidden buffer changes. See the
4322 comment at the start of cc-engine.el for more info."
4324 (or bound (setq bound (point-max)))
4325 (if paren-level (setq paren-level -1))
4327 ;;(message "c-syntactic-re-search-forward %s %s %S" (point) bound regexp)
4329 (let ((start (point))
4331 ;; Start position for the last search.
4332 search-pos
4333 ;; The `parse-partial-sexp' state between the start position
4334 ;; and the point.
4335 state
4336 ;; The current position after the last state update. The next
4337 ;; `parse-partial-sexp' continues from here.
4338 (state-pos (point))
4339 ;; The position at which to check the state and the state
4340 ;; there. This is separate from `state-pos' since we might
4341 ;; need to back up before doing the next search round.
4342 check-pos check-state
4343 ;; Last position known to end a token.
4344 (last-token-end-pos (point-min))
4345 ;; Set when a valid match is found.
4346 found)
4348 (condition-case err
4349 (while
4350 (and
4351 (progn
4352 (setq search-pos (point))
4353 (if (re-search-forward regexp bound noerror)
4355 ;; Without the following, when PAREN-LEVEL is non-nil, and
4356 ;; NOERROR is not nil or t, and the very first search above
4357 ;; has just failed, point would end up at BOUND rather than
4358 ;; just before the next close paren.
4359 (when (and (eq search-pos start)
4360 paren-level
4361 (not (memq noerror '(nil t))))
4362 (setq state (parse-partial-sexp start bound -1))
4363 (if (eq (car state) -1)
4364 (setq bound (1- (point)))))
4365 nil))
4367 (progn
4368 (setq state (parse-partial-sexp
4369 state-pos (match-beginning 0) paren-level nil state)
4370 state-pos (point))
4371 (if (setq check-pos (and lookbehind-submatch
4372 (or (not paren-level)
4373 (>= (car state) 0))
4374 (match-end lookbehind-submatch)))
4375 (setq check-state (parse-partial-sexp
4376 state-pos check-pos paren-level nil state))
4377 (setq check-pos state-pos
4378 check-state state))
4380 ;; NOTE: If we got a look behind subexpression and get
4381 ;; an insignificant match in something that isn't
4382 ;; syntactic whitespace (i.e. strings or in nested
4383 ;; parentheses), then we can never skip more than a
4384 ;; single character from the match start position
4385 ;; (i.e. `state-pos' here) before continuing the
4386 ;; search. That since the look behind subexpression
4387 ;; might match the end of the insignificant region in
4388 ;; the next search.
4390 (cond
4391 ((elt check-state 7)
4392 ;; Match inside a line comment. Skip to eol. Use
4393 ;; `re-search-forward' instead of `skip-chars-forward' to get
4394 ;; the right bound behavior.
4395 (re-search-forward "[\n\r]" bound noerror))
4397 ((elt check-state 4)
4398 ;; Match inside a block comment. Skip to the '*/'.
4399 (search-forward "*/" bound noerror))
4401 ((and (not (elt check-state 5))
4402 (eq (char-before check-pos) ?/)
4403 (not (c-get-char-property (1- check-pos) 'syntax-table))
4404 (memq (char-after check-pos) '(?/ ?*)))
4405 ;; Match in the middle of the opener of a block or line
4406 ;; comment.
4407 (if (= (char-after check-pos) ?/)
4408 (re-search-forward "[\n\r]" bound noerror)
4409 (search-forward "*/" bound noerror)))
4411 ;; The last `parse-partial-sexp' above might have
4412 ;; stopped short of the real check position if the end
4413 ;; of the current sexp was encountered in paren-level
4414 ;; mode. The checks above are always false in that
4415 ;; case, and since they can do better skipping in
4416 ;; lookbehind-submatch mode, we do them before
4417 ;; checking the paren level.
4419 ((and paren-level
4420 (/= (setq tmp (car check-state)) 0))
4421 ;; Check the paren level first since we're short of the
4422 ;; syntactic checking position if the end of the
4423 ;; current sexp was encountered by `parse-partial-sexp'.
4424 (if (> tmp 0)
4426 ;; Inside a nested paren sexp.
4427 (if lookbehind-submatch
4428 ;; See the NOTE above.
4429 (progn (goto-char state-pos) t)
4430 ;; Skip out of the paren quickly.
4431 (setq state (parse-partial-sexp state-pos bound 0 nil state)
4432 state-pos (point)))
4434 ;; Have exited the current paren sexp.
4435 (if noerror
4436 (progn
4437 ;; The last `parse-partial-sexp' call above
4438 ;; has left us just after the closing paren
4439 ;; in this case, so we can modify the bound
4440 ;; to leave the point at the right position
4441 ;; upon return.
4442 (setq bound (1- (point)))
4443 nil)
4444 (signal 'search-failed (list regexp)))))
4446 ((setq tmp (elt check-state 3))
4447 ;; Match inside a string.
4448 (if (or lookbehind-submatch
4449 (not (integerp tmp)))
4450 ;; See the NOTE above.
4451 (progn (goto-char state-pos) t)
4452 ;; Skip to the end of the string before continuing.
4453 (let ((ender (make-string 1 tmp)) (continue t))
4454 (while (if (search-forward ender bound noerror)
4455 (progn
4456 (setq state (parse-partial-sexp
4457 state-pos (point) nil nil state)
4458 state-pos (point))
4459 (elt state 3))
4460 (setq continue nil)))
4461 continue)))
4463 ((save-excursion
4464 (save-match-data
4465 (c-beginning-of-macro start)))
4466 ;; Match inside a macro. Skip to the end of it.
4467 (c-end-of-macro)
4468 (cond ((<= (point) bound) t)
4469 (noerror nil)
4470 (t (signal 'search-failed (list regexp)))))
4472 ((and not-inside-token
4473 (or (< check-pos last-token-end-pos)
4474 (< check-pos
4475 (save-excursion
4476 (goto-char check-pos)
4477 (save-match-data
4478 (c-end-of-current-token last-token-end-pos))
4479 (setq last-token-end-pos (point))))))
4480 ;; Inside a token.
4481 (if lookbehind-submatch
4482 ;; See the NOTE above.
4483 (goto-char state-pos)
4484 (goto-char (min last-token-end-pos bound))))
4487 ;; A real match.
4488 (setq found t)
4489 nil)))
4491 ;; Should loop to search again, but take care to avoid
4492 ;; looping on the same spot.
4493 (or (/= search-pos (point))
4494 (if (= (point) bound)
4495 (if noerror
4497 (signal 'search-failed (list regexp)))
4498 (forward-char)
4499 t))))
4501 (error
4502 (goto-char start)
4503 (signal (car err) (cdr err))))
4505 ;;(message "c-syntactic-re-search-forward done %s" (or (match-end 0) (point)))
4507 (if found
4508 (progn
4509 (goto-char (match-end 0))
4510 (match-end 0))
4512 ;; Search failed. Set point as appropriate.
4513 (cond
4514 ((eq noerror t)
4515 (goto-char start))
4516 ((not (memq noerror '(before-literal after-literal)))
4517 (goto-char bound))
4518 (t (setq state (parse-partial-sexp state-pos bound nil nil state))
4519 (if (or (elt state 3) (elt state 4))
4520 (if (eq noerror 'before-literal)
4521 (goto-char (elt state 8))
4522 (parse-partial-sexp bound (point-max) nil nil
4523 state 'syntax-table))
4524 (goto-char bound))))
4526 nil)))
4528 (defvar safe-pos-list) ; bound in c-syntactic-skip-backward
4530 (defsubst c-ssb-lit-begin ()
4531 ;; Return the start of the literal point is in, or nil.
4532 ;; We read and write the variables `safe-pos', `safe-pos-list', `state'
4533 ;; bound in the caller.
4535 ;; Use `parse-partial-sexp' from a safe position down to the point to check
4536 ;; if it's outside comments and strings.
4537 (save-excursion
4538 (let ((pos (point)) safe-pos state)
4539 ;; Pick a safe position as close to the point as possible.
4541 ;; FIXME: Consult `syntax-ppss' here if our cache doesn't give a good
4542 ;; position.
4544 (while (and safe-pos-list
4545 (> (car safe-pos-list) (point)))
4546 (setq safe-pos-list (cdr safe-pos-list)))
4547 (unless (setq safe-pos (car-safe safe-pos-list))
4548 (setq safe-pos (max (or (c-safe-position
4549 (point) (c-parse-state))
4551 (point-min))
4552 safe-pos-list (list safe-pos)))
4554 ;; Cache positions along the way to use if we have to back up more. We
4555 ;; cache every closing paren on the same level. If the paren cache is
4556 ;; relevant in this region then we're typically already on the same
4557 ;; level as the target position. Note that we might cache positions
4558 ;; after opening parens in case safe-pos is in a nested list. That's
4559 ;; both uncommon and harmless.
4560 (while (progn
4561 (setq state (parse-partial-sexp
4562 safe-pos pos 0))
4563 (< (point) pos))
4564 (setq safe-pos (point)
4565 safe-pos-list (cons safe-pos safe-pos-list)))
4567 ;; If the state contains the start of the containing sexp we cache that
4568 ;; position too, so that parse-partial-sexp in the next run has a bigger
4569 ;; chance of starting at the same level as the target position and thus
4570 ;; will get more good safe positions into the list.
4571 (if (elt state 1)
4572 (setq safe-pos (1+ (elt state 1))
4573 safe-pos-list (cons safe-pos safe-pos-list)))
4575 (if (or (elt state 3) (elt state 4))
4576 ;; Inside string or comment. Continue search at the
4577 ;; beginning of it.
4578 (elt state 8)))))
4580 (defun c-syntactic-skip-backward (skip-chars &optional limit paren-level)
4581 "Like `skip-chars-backward' but only look at syntactically relevant chars,
4582 i.e. don't stop at positions inside syntactic whitespace or string
4583 literals. Preprocessor directives are also ignored, with the exception
4584 of the one that the point starts within, if any. If LIMIT is given,
4585 it's assumed to be at a syntactically relevant position.
4587 If PAREN-LEVEL is non-nil, the function won't stop in nested paren
4588 sexps, and the search will also not go outside the current paren sexp.
4589 However, if LIMIT or the buffer limit is reached inside a nested paren
4590 then the point will be left at the limit.
4592 Non-nil is returned if the point moved, nil otherwise.
4594 Note that this function might do hidden buffer changes. See the
4595 comment at the start of cc-engine.el for more info."
4597 (c-self-bind-state-cache
4598 (let ((start (point))
4599 state-2
4600 ;; A list of syntactically relevant positions in descending
4601 ;; order. It's used to avoid scanning repeatedly over
4602 ;; potentially large regions with `parse-partial-sexp' to verify
4603 ;; each position. Used in `c-ssb-lit-begin'
4604 safe-pos-list
4605 ;; The result from `c-beginning-of-macro' at the start position or the
4606 ;; start position itself if it isn't within a macro. Evaluated on
4607 ;; demand.
4608 start-macro-beg
4609 ;; The earliest position after the current one with the same paren
4610 ;; level. Used only when `paren-level' is set.
4611 lit-beg
4612 (paren-level-pos (point)))
4614 (while
4615 (progn
4616 ;; The next loop "tries" to find the end point each time round,
4617 ;; loops when it hasn't succeeded.
4618 (while
4619 (and
4620 (let ((pos (point)))
4621 (while (and
4622 (< (skip-chars-backward skip-chars limit) 0)
4623 ;; Don't stop inside a literal.
4624 (when (setq lit-beg (c-ssb-lit-begin))
4625 (goto-char lit-beg)
4626 t)))
4627 (< (point) pos))
4629 (let ((pos (point)) state-2 pps-end-pos)
4631 (cond
4632 ((and paren-level
4633 (save-excursion
4634 (setq state-2 (parse-partial-sexp
4635 pos paren-level-pos -1)
4636 pps-end-pos (point))
4637 (/= (car state-2) 0)))
4638 ;; Not at the right level.
4640 (if (and (< (car state-2) 0)
4641 ;; We stop above if we go out of a paren.
4642 ;; Now check whether it precedes or is
4643 ;; nested in the starting sexp.
4644 (save-excursion
4645 (setq state-2
4646 (parse-partial-sexp
4647 pps-end-pos paren-level-pos
4648 nil nil state-2))
4649 (< (car state-2) 0)))
4651 ;; We've stopped short of the starting position
4652 ;; so the hit was inside a nested list. Go up
4653 ;; until we are at the right level.
4654 (condition-case nil
4655 (progn
4656 (goto-char (scan-lists pos -1
4657 (- (car state-2))))
4658 (setq paren-level-pos (point))
4659 (if (and limit (>= limit paren-level-pos))
4660 (progn
4661 (goto-char limit)
4662 nil)
4664 (error
4665 (goto-char (or limit (point-min)))
4666 nil))
4668 ;; The hit was outside the list at the start
4669 ;; position. Go to the start of the list and exit.
4670 (goto-char (1+ (elt state-2 1)))
4671 nil))
4673 ((c-beginning-of-macro limit)
4674 ;; Inside a macro.
4675 (if (< (point)
4676 (or start-macro-beg
4677 (setq start-macro-beg
4678 (save-excursion
4679 (goto-char start)
4680 (c-beginning-of-macro limit)
4681 (point)))))
4684 ;; It's inside the same macro we started in so it's
4685 ;; a relevant match.
4686 (goto-char pos)
4687 nil))))))
4689 (> (point)
4690 (progn
4691 ;; Skip syntactic ws afterwards so that we don't stop at the
4692 ;; end of a comment if `skip-chars' is something like "^/".
4693 (c-backward-syntactic-ws)
4694 (point)))))
4696 ;; We might want to extend this with more useful return values in
4697 ;; the future.
4698 (/= (point) start))))
4700 ;; The following is an alternative implementation of
4701 ;; `c-syntactic-skip-backward' that uses backward movement to keep
4702 ;; track of the syntactic context. It turned out to be generally
4703 ;; slower than the one above which uses forward checks from earlier
4704 ;; safe positions.
4706 ;;(defconst c-ssb-stop-re
4707 ;; ;; The regexp matching chars `c-syntactic-skip-backward' needs to
4708 ;; ;; stop at to avoid going into comments and literals.
4709 ;; (concat
4710 ;; ;; Match comment end syntax and string literal syntax. Also match
4711 ;; ;; '/' for block comment endings (not covered by comment end
4712 ;; ;; syntax).
4713 ;; "\\s>\\|/\\|\\s\""
4714 ;; (if (memq 'gen-string-delim c-emacs-features)
4715 ;; "\\|\\s|"
4716 ;; "")
4717 ;; (if (memq 'gen-comment-delim c-emacs-features)
4718 ;; "\\|\\s!"
4719 ;; "")))
4721 ;;(defconst c-ssb-stop-paren-re
4722 ;; ;; Like `c-ssb-stop-re' but also stops at paren chars.
4723 ;; (concat c-ssb-stop-re "\\|\\s(\\|\\s)"))
4725 ;;(defconst c-ssb-sexp-end-re
4726 ;; ;; Regexp matching the ending syntax of a complex sexp.
4727 ;; (concat c-string-limit-regexp "\\|\\s)"))
4729 ;;(defun c-syntactic-skip-backward (skip-chars &optional limit paren-level)
4730 ;; "Like `skip-chars-backward' but only look at syntactically relevant chars,
4731 ;;i.e. don't stop at positions inside syntactic whitespace or string
4732 ;;literals. Preprocessor directives are also ignored. However, if the
4733 ;;point is within a comment, string literal or preprocessor directory to
4734 ;;begin with, its contents is treated as syntactically relevant chars.
4735 ;;If LIMIT is given, it limits the backward search and the point will be
4736 ;;left there if no earlier position is found.
4738 ;;If PAREN-LEVEL is non-nil, the function won't stop in nested paren
4739 ;;sexps, and the search will also not go outside the current paren sexp.
4740 ;;However, if LIMIT or the buffer limit is reached inside a nested paren
4741 ;;then the point will be left at the limit.
4743 ;;Non-nil is returned if the point moved, nil otherwise.
4745 ;;Note that this function might do hidden buffer changes. See the
4746 ;;comment at the start of cc-engine.el for more info."
4748 ;; (save-restriction
4749 ;; (when limit
4750 ;; (narrow-to-region limit (point-max)))
4752 ;; (let ((start (point)))
4753 ;; (catch 'done
4754 ;; (while (let ((last-pos (point))
4755 ;; (stop-pos (progn
4756 ;; (skip-chars-backward skip-chars)
4757 ;; (point))))
4759 ;; ;; Skip back over the same region as
4760 ;; ;; `skip-chars-backward' above, but keep to
4761 ;; ;; syntactically relevant positions.
4762 ;; (goto-char last-pos)
4763 ;; (while (and
4764 ;; ;; `re-search-backward' with a single char regexp
4765 ;; ;; should be fast.
4766 ;; (re-search-backward
4767 ;; (if paren-level c-ssb-stop-paren-re c-ssb-stop-re)
4768 ;; stop-pos 'move)
4770 ;; (progn
4771 ;; (cond
4772 ;; ((looking-at "\\s(")
4773 ;; ;; `paren-level' is set and we've found the
4774 ;; ;; start of the containing paren.
4775 ;; (forward-char)
4776 ;; (throw 'done t))
4778 ;; ((looking-at c-ssb-sexp-end-re)
4779 ;; ;; We're at the end of a string literal or paren
4780 ;; ;; sexp (if `paren-level' is set).
4781 ;; (forward-char)
4782 ;; (condition-case nil
4783 ;; (c-backward-sexp)
4784 ;; (error
4785 ;; (goto-char limit)
4786 ;; (throw 'done t))))
4788 ;; (t
4789 ;; (forward-char)
4790 ;; ;; At the end of some syntactic ws or possibly
4791 ;; ;; after a plain '/' operator.
4792 ;; (let ((pos (point)))
4793 ;; (c-backward-syntactic-ws)
4794 ;; (if (= pos (point))
4795 ;; ;; Was a plain '/' operator. Go past it.
4796 ;; (backward-char)))))
4798 ;; (> (point) stop-pos))))
4800 ;; ;; Now the point is either at `stop-pos' or at some
4801 ;; ;; position further back if `stop-pos' was at a
4802 ;; ;; syntactically irrelevant place.
4804 ;; ;; Skip additional syntactic ws so that we don't stop
4805 ;; ;; at the end of a comment if `skip-chars' is
4806 ;; ;; something like "^/".
4807 ;; (c-backward-syntactic-ws)
4809 ;; (< (point) stop-pos))))
4811 ;; ;; We might want to extend this with more useful return values
4812 ;; ;; in the future.
4813 ;; (/= (point) start))))
4816 ;; Tools for handling comments and string literals.
4818 (defun c-in-literal (&optional lim detect-cpp)
4819 "Return the type of literal point is in, if any.
4820 The return value is `c' if in a C-style comment, `c++' if in a C++
4821 style comment, `string' if in a string literal, `pound' if DETECT-CPP
4822 is non-nil and in a preprocessor line, or nil if somewhere else.
4823 Optional LIM is used as the backward limit of the search. If omitted,
4824 or nil, `c-beginning-of-defun' is used.
4826 The last point calculated is cached if the cache is enabled, i.e. if
4827 `c-in-literal-cache' is bound to a two element vector.
4829 Note that this function might do hidden buffer changes. See the
4830 comment at the start of cc-engine.el for more info."
4831 (save-restriction
4832 (widen)
4833 (let ((lit (c-state-semi-pp-to-literal (point))))
4834 (or (cadr lit)
4835 (and detect-cpp
4836 (save-excursion (c-beginning-of-macro))
4837 'pound)))))
4839 (defun c-literal-limits (&optional lim near not-in-delimiter)
4840 "Return a cons of the beginning and end positions of the comment or
4841 string surrounding point (including both delimiters), or nil if point
4842 isn't in one. If LIM is non-nil, it's used as the \"safe\" position
4843 to start parsing from. If NEAR is non-nil, then the limits of any
4844 literal next to point is returned. \"Next to\" means there's only
4845 spaces and tabs between point and the literal. The search for such a
4846 literal is done first in forward direction. If NOT-IN-DELIMITER is
4847 non-nil, the case when point is inside a starting delimiter won't be
4848 recognized. This only has effect for comments which have starting
4849 delimiters with more than one character.
4851 Note that this function might do hidden buffer changes. See the
4852 comment at the start of cc-engine.el for more info."
4854 (save-excursion
4855 (let*
4856 ((pos (point))
4857 (lit-limits
4858 (if lim
4859 (let ((s (parse-partial-sexp lim (point))))
4860 (when (or (nth 3 s) (nth 4 s))
4861 (cons (nth 8 s)
4862 (progn (parse-partial-sexp (point) (point-max)
4863 nil nil
4865 'syntax-table)
4866 (point)))))
4867 (let ((pp-to-lit (c-state-full-pp-to-literal pos not-in-delimiter)))
4868 (car (cddr pp-to-lit))))))
4869 (cond
4870 (lit-limits)
4872 (near
4873 (goto-char pos)
4874 ;; Search forward for a literal.
4875 (skip-chars-forward " \t")
4876 (cond
4877 ((looking-at c-string-limit-regexp) ; String.
4878 (cons (point) (or (c-safe (c-forward-sexp 1) (point))
4879 (point-max))))
4881 ((looking-at c-comment-start-regexp) ; Line or block comment.
4882 (cons (point) (progn (c-forward-single-comment) (point))))
4885 ;; Search backward.
4886 (skip-chars-backward " \t")
4888 (let ((end (point)) beg)
4889 (cond
4890 ((save-excursion
4891 (< (skip-syntax-backward c-string-syntax) 0)) ; String.
4892 (setq beg (c-safe (c-backward-sexp 1) (point))))
4894 ((and (c-safe (forward-char -2) t)
4895 (looking-at "*/"))
4896 ;; Block comment. Due to the nature of line
4897 ;; comments, they will always be covered by the
4898 ;; normal case above.
4899 (goto-char end)
4900 (c-backward-single-comment)
4901 ;; If LIM is bogus, beg will be bogus.
4902 (setq beg (point))))
4904 (if beg (cons beg end))))))
4905 ))))
4907 (defun c-literal-start (&optional safe-pos)
4908 "Return the start of the string or comment surrounding point, or nil if
4909 point isn't in one. SAFE-POS, if non-nil, is a position before point which is
4910 a known \"safe position\", i.e. outside of any string or comment."
4911 (if safe-pos
4912 (let ((s (parse-partial-sexp safe-pos (point))))
4913 (and (or (nth 3 s) (nth 4 s))
4914 (nth 8 s)))
4915 (car (cddr (c-state-semi-pp-to-literal (point))))))
4917 ;; In case external callers use this; it did have a docstring.
4918 (defalias 'c-literal-limits-fast 'c-literal-limits)
4920 (defun c-collect-line-comments (range)
4921 "If the argument is a cons of two buffer positions (such as returned by
4922 `c-literal-limits'), and that range contains a C++ style line comment,
4923 then an extended range is returned that contains all adjacent line
4924 comments (i.e. all comments that starts in the same column with no
4925 empty lines or non-whitespace characters between them). Otherwise the
4926 argument is returned.
4928 Note that this function might do hidden buffer changes. See the
4929 comment at the start of cc-engine.el for more info."
4931 (save-excursion
4932 (condition-case nil
4933 (if (and (consp range) (progn
4934 (goto-char (car range))
4935 (looking-at c-line-comment-starter)))
4936 (let ((col (current-column))
4937 (beg (point))
4938 (bopl (c-point 'bopl))
4939 (end (cdr range)))
4940 ;; Got to take care in the backward direction to handle
4941 ;; comments which are preceded by code.
4942 (while (and (c-backward-single-comment)
4943 (>= (point) bopl)
4944 (looking-at c-line-comment-starter)
4945 (= col (current-column)))
4946 (setq beg (point)
4947 bopl (c-point 'bopl)))
4948 (goto-char end)
4949 (while (and (progn (skip-chars-forward " \t")
4950 (looking-at c-line-comment-starter))
4951 (= col (current-column))
4952 (prog1 (zerop (forward-line 1))
4953 (setq end (point)))))
4954 (cons beg end))
4955 range)
4956 (error range))))
4958 (defun c-literal-type (range)
4959 "Convenience function that given the result of `c-literal-limits',
4960 returns nil or the type of literal that the range surrounds, one
4961 of the symbols `c', `c++' or `string'. It's much faster than using
4962 `c-in-literal' and is intended to be used when you need both the
4963 type of a literal and its limits.
4965 Note that this function might do hidden buffer changes. See the
4966 comment at the start of cc-engine.el for more info."
4968 (if (consp range)
4969 (save-excursion
4970 (goto-char (car range))
4971 (cond ((looking-at c-string-limit-regexp) 'string)
4972 ((or (looking-at "//") ; c++ line comment
4973 (and (looking-at "\\s<") ; comment starter
4974 (looking-at "#"))) ; awk comment.
4975 'c++)
4976 (t 'c))) ; Assuming the range is valid.
4977 range))
4979 (defsubst c-determine-limit-get-base (start try-size)
4980 ;; Get a "safe place" approximately TRY-SIZE characters before START.
4981 ;; This defsubst doesn't preserve point.
4982 (let* ((pos (max (- start try-size) (point-min)))
4983 (s (c-state-semi-pp-to-literal pos)))
4984 (or (car (cddr s)) pos)))
4986 (defun c-determine-limit (how-far-back &optional start try-size)
4987 ;; Return a buffer position HOW-FAR-BACK non-literal characters from START
4988 ;; (default point). This is done by going back further in the buffer then
4989 ;; searching forward for literals. The position found won't be in a
4990 ;; literal. We start searching for the sought position TRY-SIZE (default
4991 ;; twice HOW-FAR-BACK) bytes back from START. This function must be fast.
4992 ;; :-)
4993 (save-excursion
4994 (let* ((start (or start (point)))
4995 (try-size (or try-size (* 2 how-far-back)))
4996 (base (c-determine-limit-get-base start try-size))
4997 (pos base)
4999 (s (parse-partial-sexp pos pos)) ; null state.
5000 stack elt size
5001 (count 0))
5002 (while (< pos start)
5003 ;; Move forward one literal each time round this loop.
5004 ;; Move forward to the start of a comment or string.
5005 (setq s (parse-partial-sexp
5007 start
5008 nil ; target-depth
5009 nil ; stop-before
5010 s ; state
5011 'syntax-table)) ; stop-comment
5013 ;; Gather details of the non-literal-bit - starting pos and size.
5014 (setq size (- (if (or (nth 4 s) (nth 3 s))
5015 (nth 8 s)
5016 (point))
5017 pos))
5018 (if (> size 0)
5019 (setq stack (cons (cons pos size) stack)))
5021 ;; Move forward to the end of the comment/string.
5022 (if (or (nth 4 s) (nth 3 s))
5023 (setq s (parse-partial-sexp
5024 (point)
5025 start
5026 nil ; target-depth
5027 nil ; stop-before
5028 s ; state
5029 'syntax-table))) ; stop-comment
5030 (setq pos (point)))
5032 ;; Now try and find enough non-literal characters recorded on the stack.
5033 ;; Go back one recorded literal each time round this loop.
5034 (while (and (< count how-far-back)
5035 stack)
5036 (setq elt (car stack)
5037 stack (cdr stack))
5038 (setq count (+ count (cdr elt))))
5040 ;; Have we found enough yet?
5041 (cond
5042 ((>= count how-far-back)
5043 (+ (car elt) (- count how-far-back)))
5044 ((eq base (point-min))
5045 (point-min))
5047 (c-determine-limit (- how-far-back count) base try-size))))))
5049 (defun c-determine-+ve-limit (how-far &optional start-pos)
5050 ;; Return a buffer position about HOW-FAR non-literal characters forward
5051 ;; from START-POS (default point), which must not be inside a literal.
5052 (save-excursion
5053 (let ((pos (or start-pos (point)))
5054 (count how-far)
5055 (s (parse-partial-sexp (point) (point)))) ; null state
5056 (while (and (not (eobp))
5057 (> count 0))
5058 ;; Scan over counted characters.
5059 (setq s (parse-partial-sexp
5061 (min (+ pos count) (point-max))
5062 nil ; target-depth
5063 nil ; stop-before
5064 s ; state
5065 'syntax-table)) ; stop-comment
5066 (setq count (- count (- (point) pos) 1)
5067 pos (point))
5068 ;; Scan over literal characters.
5069 (if (nth 8 s)
5070 (setq s (parse-partial-sexp
5072 (point-max)
5073 nil ; target-depth
5074 nil ; stop-before
5075 s ; state
5076 'syntax-table) ; stop-comment
5077 pos (point))))
5078 (point))))
5081 ;; `c-find-decl-spots' and accompanying stuff.
5083 ;; Variables used in `c-find-decl-spots' to cache the search done for
5084 ;; the first declaration in the last call. When that function starts,
5085 ;; it needs to back up over syntactic whitespace to look at the last
5086 ;; token before the region being searched. That can sometimes cause
5087 ;; moves back and forth over a quite large region of comments and
5088 ;; macros, which would be repeated for each changed character when
5089 ;; we're called during fontification, since font-lock refontifies the
5090 ;; current line for each change. Thus it's worthwhile to cache the
5091 ;; first match.
5093 ;; `c-find-decl-syntactic-pos' is a syntactically relevant position in
5094 ;; the syntactic whitespace less or equal to some start position.
5095 ;; There's no cached value if it's nil.
5097 ;; `c-find-decl-match-pos' is the match position if
5098 ;; `c-find-decl-prefix-search' matched before the syntactic whitespace
5099 ;; at `c-find-decl-syntactic-pos', or nil if there's no such match.
5100 (defvar c-find-decl-syntactic-pos nil)
5101 (make-variable-buffer-local 'c-find-decl-syntactic-pos)
5102 (defvar c-find-decl-match-pos nil)
5103 (make-variable-buffer-local 'c-find-decl-match-pos)
5105 (defsubst c-invalidate-find-decl-cache (change-min-pos)
5106 (and c-find-decl-syntactic-pos
5107 (< change-min-pos c-find-decl-syntactic-pos)
5108 (setq c-find-decl-syntactic-pos nil)))
5110 ; (defface c-debug-decl-spot-face
5111 ; '((t (:background "Turquoise")))
5112 ; "Debug face to mark the spots where `c-find-decl-spots' stopped.")
5113 ; (defface c-debug-decl-sws-face
5114 ; '((t (:background "Khaki")))
5115 ; "Debug face to mark the syntactic whitespace between the declaration
5116 ; spots and the preceding token end.")
5118 (defmacro c-debug-put-decl-spot-faces (match-pos decl-pos)
5119 (when (facep 'c-debug-decl-spot-face)
5120 `(c-save-buffer-state ((match-pos ,match-pos) (decl-pos ,decl-pos))
5121 (c-debug-add-face (max match-pos (point-min)) decl-pos
5122 'c-debug-decl-sws-face)
5123 (c-debug-add-face decl-pos (min (1+ decl-pos) (point-max))
5124 'c-debug-decl-spot-face))))
5125 (defmacro c-debug-remove-decl-spot-faces (beg end)
5126 (when (facep 'c-debug-decl-spot-face)
5127 `(c-save-buffer-state ()
5128 (c-debug-remove-face ,beg ,end 'c-debug-decl-spot-face)
5129 (c-debug-remove-face ,beg ,end 'c-debug-decl-sws-face))))
5131 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
5132 ;; Machinery for determining when we're at top level (this including being
5133 ;; directly inside a class or namespace, etc.)
5135 ;; We maintain a stack of brace depths in structures like classes and
5136 ;; namespaces. The car of this structure, when non-nil, indicates that the
5137 ;; associated position is within a template (etc.) structure, and the value is
5138 ;; the position where the (outermost) template ends. The other elements in
5139 ;; the structure are stacked elements, one each for each enclosing "top level"
5140 ;; structure.
5142 ;; At the very outermost level, the value of the stack would be (nil 1), the
5143 ;; "1" indicating an enclosure in a notional all-enclosing block. After
5144 ;; passing a keyword such as "namespace", the value would become (nil 0 1).
5145 ;; At this point, passing a semicolon would cause the 0 to be dropped from the
5146 ;; stack (at any other time, a semicolon is ignored). Alternatively, on
5147 ;; passing an opening brace, the stack would become (nil 1 1). Each opening
5148 ;; brace passed causes the cadr to be incremented, and passing closing braces
5149 ;; causes it to be decremented until it reaches 1. On passing a closing brace
5150 ;; when the cadr of the stack is at 1, this causes it to be removed from the
5151 ;; stack, the corresponding namespace (etc.) structure having been closed.
5153 ;; There is a special stack value -1 which means the C++ colon operator
5154 ;; introducing a list of inherited classes has just been parsed. The value
5155 ;; persists on the stack until the next open brace or semicolon.
5157 ;; When the car of the stack is non-nil, i.e. when we're in a template (etc.)
5158 ;; structure, braces are not counted. The counting resumes only after passing
5159 ;; the template's closing position, which is recorded in the car of the stack.
5161 ;; The test for being at top level consists of the cadr being 0 or 1.
5163 ;; The values of this stack throughout a buffer are cached in a simple linear
5164 ;; cache, every 5000 characters.
5166 ;; Note to maintainers: This cache mechanism is MUCH faster than recalculating
5167 ;; the stack at every entry to `c-find-decl-spots' using `c-at-toplevel-p' or
5168 ;; the like.
5169 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
5170 ;; The approximate interval at which we cache the value of the brace stack.
5171 (defconst c-bs-interval 5000)
5172 ;; The list of cached values of the brace stack. Each value in the list is a
5173 ;; cons of the position it is valid for and the value of the stack as
5174 ;; described above.
5175 (defvar c-bs-cache nil)
5176 (make-variable-buffer-local 'c-bs-cache)
5177 ;; The position of the buffer at and below which entries in `c-bs-cache' are
5178 ;; valid.
5179 (defvar c-bs-cache-limit 1)
5180 (make-variable-buffer-local 'c-bs-cache-limit)
5181 ;; The previous buffer position for which the brace stack value was
5182 ;; determined.
5183 (defvar c-bs-prev-pos most-positive-fixnum)
5184 (make-variable-buffer-local 'c-bs-prev-pos)
5185 ;; The value of the brace stack at `c-bs-prev-pos'.
5186 (defvar c-bs-prev-stack nil)
5187 (make-variable-buffer-local 'c-bs-prev-stack)
5189 (defun c-init-bs-cache ()
5190 ;; Initialize the cache in `c-bs-cache' and related variables.
5191 (setq c-bs-cache nil
5192 c-bs-cache-limit 1
5193 c-bs-prev-pos most-positive-fixnum
5194 c-bs-prev-stack nil))
5196 (defun c-truncate-bs-cache (pos &rest _ignore)
5197 ;; Truncate the upper bound of the cache `c-bs-cache' to POS, if it is
5198 ;; higher than that position. This is called as either a before- or
5199 ;; after-change-function.
5200 (setq c-bs-cache-limit
5201 (min c-bs-cache-limit pos)))
5203 (defun c-update-brace-stack (stack from to)
5204 ;; Give a brace-stack which has the value STACK at position FROM, update it
5205 ;; to it's value at position TO, where TO is after (or equal to) FROM.
5206 ;; Return a cons of either TO (if it is outside a literal) and this new
5207 ;; value, or of the next position after TO outside a literal and the new
5208 ;; value.
5209 (let (match kwd-sym (prev-match-pos 1)
5210 (s (cdr stack))
5211 (bound-<> (car stack))
5213 (save-excursion
5214 (cond
5215 ((and bound-<> (<= to bound-<>))
5216 (goto-char to)) ; Nothing to do.
5217 (bound-<>
5218 (goto-char bound-<>)
5219 (setq bound-<> nil))
5220 (t (goto-char from)))
5221 (while (and (< (point) to)
5222 (c-syntactic-re-search-forward
5223 (if (<= (car s) 0)
5224 c-brace-stack-thing-key
5225 c-brace-stack-no-semi-key)
5226 to 'after-literal)
5227 (> (point) prev-match-pos)) ; prevent infinite loop.
5228 (setq prev-match-pos (point))
5229 (setq match (match-string-no-properties 1)
5230 kwd-sym (c-keyword-sym match))
5231 (cond
5232 ((and (equal match "{")
5233 (progn (backward-char)
5234 (prog1 (looking-at "\\s(")
5235 (forward-char))))
5236 (setq s (if s
5237 (cons (if (<= (car s) 0)
5239 (1+ (car s)))
5240 (cdr s))
5241 (list 1))))
5242 ((and (equal match "}")
5243 (progn (backward-char)
5244 (prog1 (looking-at "\\s)")
5245 (forward-char))))
5246 (setq s
5247 (cond
5248 ((and s (> (car s) 1))
5249 (cons (1- (car s)) (cdr s)))
5250 ((and (cdr s) (eq (car s) 1))
5251 (cdr s))
5252 (t s))))
5253 ((and (equal match "<")
5254 (progn (backward-char)
5255 (prog1 (looking-at "\\s(")
5256 (forward-char))))
5257 (backward-char)
5258 (if (c-forward-<>-arglist nil) ; Should always work.
5259 (when (> (point) to)
5260 (setq bound-<> (point)))
5261 (forward-char)))
5262 ((and (equal match ":")
5264 (eq (car s) 0))
5265 (setq s (cons -1 (cdr s))))
5266 ((and (equal match ",")
5267 (eq (car s) -1))) ; at "," in "class foo : bar, ..."
5268 ((member match '(";" "," ")"))
5269 (when (and s (cdr s) (<= (car s) 0))
5270 (setq s (cdr s))))
5271 ((c-keyword-member kwd-sym 'c-flat-decl-block-kwds)
5272 (push 0 s))))
5273 (cons (point)
5274 (cons bound-<> s)))))
5276 (defun c-brace-stack-at (here)
5277 ;; Given a buffer position HERE, Return the value of the brace stack there.
5278 (save-excursion
5279 (save-restriction
5280 (widen)
5281 (let ((c c-bs-cache)
5282 (can-use-prev (<= c-bs-prev-pos c-bs-cache-limit))
5283 elt stack pos npos high-elt)
5284 ;; Trim the cache to take account of buffer changes.
5285 (while (and c
5286 (> (caar c) c-bs-cache-limit))
5287 (setq c (cdr c)))
5288 (setq c-bs-cache c)
5290 (while (and c
5291 (> (caar c) here))
5292 (setq high-elt (car c))
5293 (setq c (cdr c)))
5294 (setq pos (or (and c (caar c))
5295 (point-min)))
5297 (setq elt (if c
5298 (car c)
5299 (cons (point-min)
5300 (cons nil (list 1)))))
5301 (when (not high-elt)
5302 (setq stack (cdr elt))
5303 (while
5304 ;; Add an element to `c-state-semi-nonlit-pos-cache' each iteration.
5305 (<= (setq npos (+ pos c-bs-interval)) here)
5306 (setq elt (c-update-brace-stack stack pos npos))
5307 (setq npos (car elt))
5308 (setq stack (cdr elt))
5309 (unless (eq npos (point-max)) ; NPOS could be in a literal at EOB.
5310 (setq c-bs-cache (cons elt c-bs-cache)))
5311 (setq pos npos)))
5313 (if (> pos c-bs-cache-limit)
5314 (setq c-bs-cache-limit pos))
5316 ;; Can we just use the previous value?
5317 (if (and can-use-prev
5318 (<= c-bs-prev-pos here)
5319 (> c-bs-prev-pos (car elt)))
5320 (setq pos c-bs-prev-pos
5321 stack c-bs-prev-stack)
5322 (setq pos (car elt)
5323 stack (cdr elt)))
5324 (if (> here c-bs-cache-limit)
5325 (setq c-bs-cache-limit here))
5326 (setq elt (c-update-brace-stack stack pos here)
5327 c-bs-prev-pos (car elt)
5328 c-bs-prev-stack (cdr elt))))))
5330 (defun c-bs-at-toplevel-p (here)
5331 ;; Is position HERE at the top level, as indicated by the brace stack?
5332 (let ((stack (c-brace-stack-at here)))
5333 (or (null stack) ; Probably unnecessary.
5334 (<= (cadr stack) 1))))
5336 (defmacro c-find-decl-prefix-search ()
5337 ;; Macro used inside `c-find-decl-spots'. It ought to be a defun,
5338 ;; but it contains lots of free variables that refer to things
5339 ;; inside `c-find-decl-spots'. The point is left at `cfd-match-pos'
5340 ;; if there is a match, otherwise at `cfd-limit'.
5342 ;; The macro moves point forward to the next putative start of a declaration
5343 ;; or cfd-limit. This decl start is the next token after a "declaration
5344 ;; prefix". The declaration prefix is the earlier of `cfd-prop-match' and
5345 ;; `cfd-re-match'. `cfd-match-pos' is set to the decl prefix.
5347 ;; This macro might do hidden buffer changes.
5349 '(progn
5350 ;; Find the next property match position if we haven't got one already.
5351 (unless cfd-prop-match
5352 (save-excursion
5353 (while (progn
5354 (goto-char (c-next-single-property-change
5355 (point) 'c-type nil cfd-limit))
5356 (and (< (point) cfd-limit)
5357 (not (eq (c-get-char-property (1- (point)) 'c-type)
5358 'c-decl-end)))))
5359 (setq cfd-prop-match (point))))
5361 ;; Find the next `c-decl-prefix-or-start-re' match if we haven't
5362 ;; got one already.
5363 (unless cfd-re-match
5365 (if (> cfd-re-match-end (point))
5366 (goto-char cfd-re-match-end))
5368 ;; Each time round, the next `while' moves forward over a pseudo match
5369 ;; of `c-decl-prefix-or-start-re' which is either inside a literal, or
5370 ;; is a ":" not preceded by "public", etc.. `cfd-re-match' and
5371 ;; `cfd-re-match-end' get set.
5372 (while
5373 (progn
5374 (setq cfd-re-match-end (re-search-forward c-decl-prefix-or-start-re
5375 cfd-limit 'move))
5376 (cond
5377 ((null cfd-re-match-end)
5378 ;; No match. Finish up and exit the loop.
5379 (setq cfd-re-match cfd-limit)
5380 nil)
5381 ((c-got-face-at
5382 (if (setq cfd-re-match (match-end 1))
5383 ;; Matched the end of a token preceding a decl spot.
5384 (progn
5385 (goto-char cfd-re-match)
5386 (1- cfd-re-match))
5387 ;; Matched a token that start a decl spot.
5388 (goto-char (match-beginning 0))
5389 (point))
5390 c-literal-faces)
5391 ;; Pseudo match inside a comment or string literal. Skip out
5392 ;; of comments and string literals.
5393 (while (progn
5394 (goto-char (c-next-single-property-change
5395 (point) 'face nil cfd-limit))
5396 (and (< (point) cfd-limit)
5397 (c-got-face-at (point) c-literal-faces))))
5398 t) ; Continue the loop over pseudo matches.
5399 ((and c-opt-identifier-concat-key
5400 (match-string 1)
5401 (save-excursion
5402 (goto-char (match-beginning 1))
5403 (save-match-data
5404 (looking-at c-opt-identifier-concat-key))))
5405 ;; Found, e.g., "::" in C++
5407 ((and (match-string 1)
5408 (string= (match-string 1) ":")
5409 (save-excursion
5410 (or (/= (c-backward-token-2 2) 0) ; no search limit. :-(
5411 (not (looking-at c-decl-start-colon-kwd-re)))))
5412 ;; Found a ":" which isn't part of "public:", etc.
5414 (t nil)))) ;; Found a real match. Exit the pseudo-match loop.
5416 ;; If our match was at the decl start, we have to back up over the
5417 ;; preceding syntactic ws to set `cfd-match-pos' and to catch
5418 ;; any decl spots in the syntactic ws.
5419 (unless cfd-re-match
5420 (c-backward-syntactic-ws)
5421 (setq cfd-re-match (point))))
5423 ;; Choose whichever match is closer to the start.
5424 (if (< cfd-re-match cfd-prop-match)
5425 (setq cfd-match-pos cfd-re-match
5426 cfd-re-match nil)
5427 (setq cfd-match-pos cfd-prop-match
5428 cfd-prop-match nil))
5429 (setq cfd-top-level (c-bs-at-toplevel-p cfd-match-pos))
5431 (goto-char cfd-match-pos)
5433 (when (< cfd-match-pos cfd-limit)
5434 ;; Skip forward past comments only so we don't skip macros.
5435 (c-forward-comments)
5436 ;; Set the position to continue at. We can avoid going over
5437 ;; the comments skipped above a second time, but it's possible
5438 ;; that the comment skipping has taken us past `cfd-prop-match'
5439 ;; since the property might be used inside comments.
5440 (setq cfd-continue-pos (if cfd-prop-match
5441 (min cfd-prop-match (point))
5442 (point))))))
5444 (defun c-find-decl-spots (cfd-limit cfd-decl-re cfd-face-checklist cfd-fun)
5445 ;; Call CFD-FUN for each possible spot for a declaration, cast or
5446 ;; label from the point to CFD-LIMIT.
5448 ;; CFD-FUN is called with point at the start of the spot. It's passed two
5449 ;; arguments: The first is the end position of the token preceding the spot,
5450 ;; or 0 for the implicit match at bob. The second is a flag that is t when
5451 ;; the match is inside a macro. Point should be moved forward by at least
5452 ;; one token.
5454 ;; If CFD-FUN adds `c-decl-end' properties somewhere below the current spot,
5455 ;; it should return non-nil to ensure that the next search will find them.
5457 ;; Such a spot is:
5458 ;; o The first token after bob.
5459 ;; o The first token after the end of submatch 1 in
5460 ;; `c-decl-prefix-or-start-re' when that submatch matches. This
5461 ;; submatch is typically a (L or R) brace or paren, a ;, or a ,.
5462 ;; o The start of each `c-decl-prefix-or-start-re' match when
5463 ;; submatch 1 doesn't match. This is, for example, the keyword
5464 ;; "class" in Pike.
5465 ;; o The start of a previously recognized declaration; "recognized"
5466 ;; means that the last char of the previous token has a `c-type'
5467 ;; text property with the value `c-decl-end'; this only holds
5468 ;; when `c-type-decl-end-used' is set.
5470 ;; Only a spot that match CFD-DECL-RE and whose face is in the
5471 ;; CFD-FACE-CHECKLIST list causes CFD-FUN to be called. The face
5472 ;; check is disabled if CFD-FACE-CHECKLIST is nil.
5474 ;; If the match is inside a macro then the buffer is narrowed to the
5475 ;; end of it, so that CFD-FUN can investigate the following tokens
5476 ;; without matching something that begins inside a macro and ends
5477 ;; outside it. It's to avoid this work that the CFD-DECL-RE and
5478 ;; CFD-FACE-CHECKLIST checks exist.
5480 ;; The spots are visited approximately in order from top to bottom.
5481 ;; It's however the positions where `c-decl-prefix-or-start-re'
5482 ;; matches and where `c-decl-end' properties are found that are in
5483 ;; order. Since the spots often are at the following token, they
5484 ;; might be visited out of order insofar as more spots are reported
5485 ;; later on within the syntactic whitespace between the match
5486 ;; positions and their spots.
5488 ;; It's assumed that comments and strings are fontified in the
5489 ;; searched range.
5491 ;; This is mainly used in fontification, and so has an elaborate
5492 ;; cache to handle repeated calls from the same start position; see
5493 ;; the variables above.
5495 ;; All variables in this function begin with `cfd-' to avoid name
5496 ;; collision with the (dynamically bound) variables used in CFD-FUN.
5498 ;; This function might do hidden buffer changes.
5500 (let ((cfd-start-pos (point)) ; never changed
5501 (cfd-buffer-end (point-max))
5502 ;; The end of the token preceding the decl spot last found
5503 ;; with `c-decl-prefix-or-start-re'. `cfd-limit' if there's
5504 ;; no match.
5505 cfd-re-match
5506 ;; The end position of the last `c-decl-prefix-or-start-re'
5507 ;; match. If this is greater than `cfd-continue-pos', the
5508 ;; next regexp search is started here instead.
5509 (cfd-re-match-end (point-min))
5510 ;; The end of the last `c-decl-end' found by
5511 ;; `c-find-decl-prefix-search'. `cfd-limit' if there's no
5512 ;; match. If searching for the property isn't needed then we
5513 ;; disable it by setting it to `cfd-limit' directly.
5514 (cfd-prop-match (unless c-type-decl-end-used cfd-limit))
5515 ;; The end of the token preceding the decl spot last found by
5516 ;; `c-find-decl-prefix-search'. 0 for the implicit match at
5517 ;; bob. `cfd-limit' if there's no match. In other words,
5518 ;; this is the minimum of `cfd-re-match' and `cfd-prop-match'.
5519 (cfd-match-pos cfd-limit)
5520 ;; The position to continue searching at.
5521 cfd-continue-pos
5522 ;; The position of the last "real" token we've stopped at.
5523 ;; This can be greater than `cfd-continue-pos' when we get
5524 ;; hits inside macros or at `c-decl-end' positions inside
5525 ;; comments.
5526 (cfd-token-pos 0)
5527 ;; The end position of the last entered macro.
5528 (cfd-macro-end 0)
5529 ;; Whether the last position returned from `c-find-decl-prefix-search'
5530 ;; is at the top-level (including directly in a class or namespace,
5531 ;; etc.).
5532 cfd-top-level)
5534 ;; Initialize by finding a syntactically relevant start position
5535 ;; before the point, and do the first `c-decl-prefix-or-start-re'
5536 ;; search unless we're at bob.
5538 (let (start-in-literal start-in-macro syntactic-pos)
5539 ;; Must back up a bit since we look for the end of the previous
5540 ;; statement or declaration, which is earlier than the first
5541 ;; returned match.
5543 ;; This `cond' moves back over any literals or macros. It has special
5544 ;; handling for when the region being searched is entirely within a
5545 ;; macro. It sets `cfd-continue-pos' (unless we've reached
5546 ;; `cfd-limit').
5547 (cond
5548 ;; First we need to move to a syntactically relevant position.
5549 ;; Begin by backing out of comment or string literals.
5551 ;; This arm of the cond actually triggers if we're in a literal,
5552 ;; and cfd-limit is at most at BONL.
5553 ((and
5554 ;; This arm of the `and' moves backwards out of a literal when
5555 ;; the face at point is a literal face. In this case, its value
5556 ;; is always non-nil.
5557 (when (c-got-face-at (point) c-literal-faces)
5558 ;; Try to use the faces to back up to the start of the
5559 ;; literal. FIXME: What if the point is on a declaration
5560 ;; inside a comment?
5561 (while (and (not (bobp))
5562 (c-got-face-at (1- (point)) c-literal-faces))
5563 (goto-char (previous-single-property-change
5564 (point) 'face nil (point-min))))
5566 ;; XEmacs doesn't fontify the quotes surrounding string
5567 ;; literals.
5568 (and (featurep 'xemacs)
5569 (eq (get-text-property (point) 'face)
5570 'font-lock-string-face)
5571 (not (bobp))
5572 (progn (backward-char)
5573 (not (looking-at c-string-limit-regexp)))
5574 (forward-char))
5576 ;; Don't trust the literal to contain only literal faces
5577 ;; (the font lock package might not have fontified the
5578 ;; start of it at all, for instance) so check that we have
5579 ;; arrived at something that looks like a start or else
5580 ;; resort to `c-literal-limits'.
5581 (unless (looking-at c-literal-start-regexp)
5582 (let ((lit-start (c-literal-start)))
5583 (if lit-start (goto-char lit-start)))
5586 (setq start-in-literal (point))) ; end of `and' arm.
5588 ;; The start is in a literal. If the limit is in the same
5589 ;; one we don't have to find a syntactic position etc. We
5590 ;; only check that if the limit is at or before bonl to save
5591 ;; time; it covers the by far most common case when font-lock
5592 ;; refontifies the current line only.
5593 (<= cfd-limit (c-point 'bonl cfd-start-pos))
5594 (save-excursion
5595 (goto-char cfd-start-pos)
5596 (while (progn
5597 (goto-char (c-next-single-property-change
5598 (point) 'face nil cfd-limit))
5599 (and (< (point) cfd-limit)
5600 (c-got-face-at (point) c-literal-faces))))
5601 (= (point) cfd-limit))) ; end of `cond' arm condition
5603 ;; Completely inside a literal. Set up variables to trig the
5604 ;; (< cfd-continue-pos cfd-start-pos) case below and it'll
5605 ;; find a suitable start position.
5606 (setq cfd-continue-pos start-in-literal)) ; end of `cond' arm
5608 ;; Check if the region might be completely inside a macro, to
5609 ;; optimize that like the completely-inside-literal above.
5610 ((save-excursion
5611 (and (= (forward-line 1) 0)
5612 (bolp) ; forward-line has funny behavior at eob.
5613 (>= (point) cfd-limit)
5614 (progn (backward-char)
5615 (eq (char-before) ?\\))))
5616 ;; (Maybe) completely inside a macro. Only need to trig the
5617 ;; (< cfd-continue-pos cfd-start-pos) case below to make it
5618 ;; set things up.
5619 (setq cfd-continue-pos (1- cfd-start-pos)
5620 start-in-macro t))
5622 ;; The default arm of the `cond' moves back over any macro we're in
5623 ;; and over any syntactic WS. It sets `c-find-decl-syntactic-pos'.
5625 ;; Back out of any macro so we don't miss any declaration
5626 ;; that could follow after it.
5627 (when (c-beginning-of-macro)
5628 (setq start-in-macro t))
5630 ;; Now we're at a proper syntactically relevant position so we
5631 ;; can use the cache. But first clear it if it applied
5632 ;; further down.
5633 (c-invalidate-find-decl-cache cfd-start-pos)
5635 (setq syntactic-pos (point))
5636 (unless (eq syntactic-pos c-find-decl-syntactic-pos)
5637 ;; Don't have to do this if the cache is relevant here,
5638 ;; typically if the same line is refontified again. If
5639 ;; we're just some syntactic whitespace further down we can
5640 ;; still use the cache to limit the skipping.
5641 (c-backward-syntactic-ws c-find-decl-syntactic-pos))
5643 ;; If we hit `c-find-decl-syntactic-pos' and
5644 ;; `c-find-decl-match-pos' is set then we install the cached
5645 ;; values. If we hit `c-find-decl-syntactic-pos' and
5646 ;; `c-find-decl-match-pos' is nil then we know there's no decl
5647 ;; prefix in the whitespace before `c-find-decl-syntactic-pos'
5648 ;; and so we can continue the search from this point. If we
5649 ;; didn't hit `c-find-decl-syntactic-pos' then we're now in
5650 ;; the right spot to begin searching anyway.
5651 (if (and (eq (point) c-find-decl-syntactic-pos)
5652 c-find-decl-match-pos)
5653 (setq cfd-match-pos c-find-decl-match-pos
5654 cfd-continue-pos syntactic-pos)
5656 (setq c-find-decl-syntactic-pos syntactic-pos)
5658 (when (if (bobp)
5659 ;; Always consider bob a match to get the first
5660 ;; declaration in the file. Do this separately instead of
5661 ;; letting `c-decl-prefix-or-start-re' match bob, so that
5662 ;; regexp always can consume at least one character to
5663 ;; ensure that we won't get stuck in an infinite loop.
5664 (setq cfd-re-match 0)
5665 (backward-char)
5666 (c-beginning-of-current-token)
5667 (< (point) cfd-limit))
5668 ;; Do an initial search now. In the bob case above it's
5669 ;; only done to search for a `c-decl-end' spot.
5670 (c-find-decl-prefix-search)) ; sets cfd-continue-pos
5672 (setq c-find-decl-match-pos (and (< cfd-match-pos cfd-start-pos)
5673 cfd-match-pos))))) ; end of `cond'
5675 ;; Advance `cfd-continue-pos' if it's before the start position.
5676 ;; The closest continue position that might have effect at or
5677 ;; after the start depends on what we started in. This also
5678 ;; finds a suitable start position in the special cases when the
5679 ;; region is completely within a literal or macro.
5680 (when (and cfd-continue-pos (< cfd-continue-pos cfd-start-pos))
5682 (cond
5683 (start-in-macro
5684 ;; If we're in a macro then it's the closest preceding token
5685 ;; in the macro. Check this before `start-in-literal',
5686 ;; since if we're inside a literal in a macro, the preceding
5687 ;; token is earlier than any `c-decl-end' spot inside the
5688 ;; literal (comment).
5689 (goto-char (or start-in-literal cfd-start-pos))
5690 ;; The only syntactic ws in macros are comments.
5691 (c-backward-comments)
5692 (backward-char)
5693 (c-beginning-of-current-token))
5695 (start-in-literal
5696 ;; If we're in a comment it can only be the closest
5697 ;; preceding `c-decl-end' position within that comment, if
5698 ;; any. Go back to the beginning of such a property so that
5699 ;; `c-find-decl-prefix-search' will find the end of it.
5700 ;; (Can't stop at the end and install it directly on
5701 ;; `cfd-prop-match' since that variable might be cleared
5702 ;; after `cfd-fun' below.)
5704 ;; Note that if the literal is a string then the property
5705 ;; search will simply skip to the beginning of it right
5706 ;; away.
5707 (if (not c-type-decl-end-used)
5708 (goto-char start-in-literal)
5709 (goto-char cfd-start-pos)
5710 (while (progn
5711 (goto-char (previous-single-property-change
5712 (point) 'c-type nil start-in-literal))
5713 (and (> (point) start-in-literal)
5714 (not (eq (c-get-char-property (point) 'c-type)
5715 'c-decl-end))))))
5717 (when (= (point) start-in-literal)
5718 ;; Didn't find any property inside the comment, so we can
5719 ;; skip it entirely. (This won't skip past a string, but
5720 ;; that'll be handled quickly by the next
5721 ;; `c-find-decl-prefix-search' anyway.)
5722 (c-forward-single-comment)
5723 (if (> (point) cfd-limit)
5724 (goto-char cfd-limit))))
5727 ;; If we started in normal code, the only match that might
5728 ;; apply before the start is what we already got in
5729 ;; `cfd-match-pos' so we can continue at the start position.
5730 ;; (Note that we don't get here if the first match is below
5731 ;; it.)
5732 (goto-char cfd-start-pos))) ; end of `cond'
5734 ;; Delete found matches if they are before our new continue
5735 ;; position, so that `c-find-decl-prefix-search' won't back up
5736 ;; to them later on.
5737 (setq cfd-continue-pos (point))
5738 (when (and cfd-re-match (< cfd-re-match cfd-continue-pos))
5739 (setq cfd-re-match nil))
5740 (when (and cfd-prop-match (< cfd-prop-match cfd-continue-pos))
5741 (setq cfd-prop-match nil))) ; end of `when'
5743 (if syntactic-pos
5744 ;; This is the normal case and we got a proper syntactic
5745 ;; position. If there's a match then it's always outside
5746 ;; macros and comments, so advance to the next token and set
5747 ;; `cfd-token-pos'. The loop below will later go back using
5748 ;; `cfd-continue-pos' to fix declarations inside the
5749 ;; syntactic ws.
5750 (when (and cfd-match-pos (< cfd-match-pos syntactic-pos))
5751 (goto-char syntactic-pos)
5752 (c-forward-syntactic-ws)
5753 (and cfd-continue-pos
5754 (< cfd-continue-pos (point))
5755 (setq cfd-token-pos (point))))
5757 ;; Have one of the special cases when the region is completely
5758 ;; within a literal or macro. `cfd-continue-pos' is set to a
5759 ;; good start position for the search, so do it.
5760 (c-find-decl-prefix-search)))
5762 ;; Now loop, one decl spot per iteration. We already have the first
5763 ;; match in `cfd-match-pos'.
5764 (while (progn
5765 ;; Go forward over "false matches", one per iteration.
5766 (while (and
5767 (< cfd-match-pos cfd-limit)
5770 ;; Kludge to filter out matches on the "<" that
5771 ;; aren't open parens, for the sake of languages
5772 ;; that got `c-recognize-<>-arglists' set.
5773 (and (eq (char-before cfd-match-pos) ?<)
5774 (not (c-get-char-property (1- cfd-match-pos)
5775 'syntax-table)))
5777 ;; If `cfd-continue-pos' is less or equal to
5778 ;; `cfd-token-pos', we've got a hit inside a macro
5779 ;; that's in the syntactic whitespace before the last
5780 ;; "real" declaration we've checked. If they're equal
5781 ;; we've arrived at the declaration a second time, so
5782 ;; there's nothing to do.
5783 (= cfd-continue-pos cfd-token-pos)
5785 (progn
5786 ;; If `cfd-continue-pos' is less than `cfd-token-pos'
5787 ;; we're still searching for declarations embedded in
5788 ;; the syntactic whitespace. In that case we need
5789 ;; only to skip comments and not macros, since they
5790 ;; can't be nested, and that's already been done in
5791 ;; `c-find-decl-prefix-search'.
5792 (when (> cfd-continue-pos cfd-token-pos)
5793 (c-forward-syntactic-ws)
5794 (setq cfd-token-pos (point)))
5796 ;; Continue if the following token fails the
5797 ;; CFD-DECL-RE and CFD-FACE-CHECKLIST checks.
5798 (when (or (>= (point) cfd-limit)
5799 (not (looking-at cfd-decl-re))
5800 (and cfd-face-checklist
5801 (not (c-got-face-at
5802 (point) cfd-face-checklist))))
5803 (goto-char cfd-continue-pos)
5804 t)))
5806 (< (point) cfd-limit)) ; end of "false matches" condition
5807 (c-find-decl-prefix-search)) ; end of "false matches" loop
5809 (< (point) cfd-limit)) ; end of condition for "decl-spot" while
5811 (when (and
5812 (>= (point) cfd-start-pos)
5814 (progn
5815 ;; Narrow to the end of the macro if we got a hit inside
5816 ;; one, to avoid recognizing things that start inside the
5817 ;; macro and end outside it.
5818 (when (> cfd-match-pos cfd-macro-end)
5819 ;; Not in the same macro as in the previous round.
5820 (save-excursion
5821 (goto-char cfd-match-pos)
5822 (setq cfd-macro-end
5823 (if (save-excursion (and (c-beginning-of-macro)
5824 (< (point) cfd-match-pos)))
5825 (progn (c-end-of-macro)
5826 (point))
5827 0))))
5829 (if (zerop cfd-macro-end)
5831 (if (> cfd-macro-end (point))
5832 (progn (narrow-to-region (point-min) cfd-macro-end)
5834 ;; The matched token was the last thing in the macro,
5835 ;; so the whole match is bogus.
5836 (setq cfd-macro-end 0)
5837 nil)))) ; end of when condition
5839 (c-debug-put-decl-spot-faces cfd-match-pos (point))
5840 (if (funcall cfd-fun cfd-match-pos (/= cfd-macro-end 0) cfd-top-level)
5841 (setq cfd-prop-match nil))
5843 (when (/= cfd-macro-end 0)
5844 ;; Restore limits if we did macro narrowing above.
5845 (narrow-to-region (point-min) cfd-buffer-end)))
5847 (goto-char cfd-continue-pos)
5848 (if (= cfd-continue-pos cfd-limit)
5849 (setq cfd-match-pos cfd-limit)
5850 (c-find-decl-prefix-search))))) ; Moves point, sets cfd-continue-pos,
5851 ; cfd-match-pos, etc.
5854 ;; A cache for found types.
5856 ;; Buffer local variable that contains an obarray with the types we've
5857 ;; found. If a declaration is recognized somewhere we record the
5858 ;; fully qualified identifier in it to recognize it as a type
5859 ;; elsewhere in the file too. This is not accurate since we do not
5860 ;; bother with the scoping rules of the languages, but in practice the
5861 ;; same name is seldom used as both a type and something else in a
5862 ;; file, and we only use this as a last resort in ambiguous cases (see
5863 ;; `c-forward-decl-or-cast-1').
5865 ;; Not every type need be in this cache. However, things which have
5866 ;; ceased to be types must be removed from it.
5868 ;; Template types in C++ are added here too but with the template
5869 ;; arglist replaced with "<>" in references or "<" for the one in the
5870 ;; primary type. E.g. the type "Foo<A,B>::Bar<C>" is stored as
5871 ;; "Foo<>::Bar<". This avoids storing very long strings (since C++
5872 ;; template specs can be fairly sized programs in themselves) and
5873 ;; improves the hit ratio (it's a type regardless of the template
5874 ;; args; it's just not the same type, but we're only interested in
5875 ;; recognizing types, not telling distinct types apart). Note that
5876 ;; template types in references are added here too; from the example
5877 ;; above there will also be an entry "Foo<".
5878 (defvar c-found-types nil)
5879 (make-variable-buffer-local 'c-found-types)
5881 (defsubst c-clear-found-types ()
5882 ;; Clears `c-found-types'.
5883 (setq c-found-types (make-vector 53 0)))
5885 (defun c-add-type (from to)
5886 ;; Add the given region as a type in `c-found-types'. If the region
5887 ;; doesn't match an existing type but there is a type which is equal
5888 ;; to the given one except that the last character is missing, then
5889 ;; the shorter type is removed. That's done to avoid adding all
5890 ;; prefixes of a type as it's being entered and font locked. This
5891 ;; doesn't cover cases like when characters are removed from a type
5892 ;; or added in the middle. We'd need the position of point when the
5893 ;; font locking is invoked to solve this well.
5895 ;; This function might do hidden buffer changes.
5896 (let ((type (c-syntactic-content from to c-recognize-<>-arglists)))
5897 (unless (intern-soft type c-found-types)
5898 (unintern (substring type 0 -1) c-found-types)
5899 (intern type c-found-types))))
5901 (defun c-unfind-type (name)
5902 ;; Remove the "NAME" from c-found-types, if present.
5903 (unintern name c-found-types))
5905 (defsubst c-check-type (from to)
5906 ;; Return non-nil if the given region contains a type in
5907 ;; `c-found-types'.
5909 ;; This function might do hidden buffer changes.
5910 (intern-soft (c-syntactic-content from to c-recognize-<>-arglists)
5911 c-found-types))
5913 (defun c-list-found-types ()
5914 ;; Return all the types in `c-found-types' as a sorted list of
5915 ;; strings.
5916 (let (type-list)
5917 (mapatoms (lambda (type)
5918 (setq type-list (cons (symbol-name type)
5919 type-list)))
5920 c-found-types)
5921 (sort type-list 'string-lessp)))
5923 ;; Shut up the byte compiler.
5924 (defvar c-maybe-stale-found-type)
5926 (defun c-trim-found-types (beg end old-len)
5927 ;; An after change function which, in conjunction with the info in
5928 ;; c-maybe-stale-found-type (set in c-before-change), removes a type
5929 ;; from `c-found-types', should this type have become stale. For
5930 ;; example, this happens to "foo" when "foo \n bar();" becomes
5931 ;; "foo(); \n bar();". Such stale types, if not removed, foul up
5932 ;; the fontification.
5934 ;; Have we, perhaps, added non-ws characters to the front/back of a found
5935 ;; type?
5936 (when (> end beg)
5937 (save-excursion
5938 (when (< end (point-max))
5939 (goto-char end)
5940 (if (and (c-beginning-of-current-token) ; only moves when we started in the middle
5941 (progn (goto-char end)
5942 (c-end-of-current-token)))
5943 (c-unfind-type (buffer-substring-no-properties
5944 end (point)))))
5945 (when (> beg (point-min))
5946 (goto-char beg)
5947 (if (and (c-end-of-current-token) ; only moves when we started in the middle
5948 (progn (goto-char beg)
5949 (c-beginning-of-current-token)))
5950 (c-unfind-type (buffer-substring-no-properties
5951 (point) beg))))))
5953 (if c-maybe-stale-found-type ; e.g. (c-decl-id-start "foo" 97 107 " (* ooka) " "o")
5954 (cond
5955 ;; Changing the amount of (already existing) whitespace - don't do anything.
5956 ((and (c-partial-ws-p beg end)
5957 (or (= beg end) ; removal of WS
5958 (string-match "^[ \t\n\r\f\v]*$" (nth 5 c-maybe-stale-found-type)))))
5960 ;; The syntactic relationship which defined a "found type" has been
5961 ;; destroyed.
5962 ((eq (car c-maybe-stale-found-type) 'c-decl-id-start)
5963 (c-unfind-type (cadr c-maybe-stale-found-type)))
5964 ;; ((eq (car c-maybe-stale-found-type) 'c-decl-type-start) FIXME!!!
5968 ;; Setting and removing syntax properties on < and > in languages (C++
5969 ;; and Java) where they can be template/generic delimiters as well as
5970 ;; their normal meaning of "less/greater than".
5972 ;; Normally, < and > have syntax 'punctuation'. When they are found to
5973 ;; be delimiters, they are marked as such with the category properties
5974 ;; c-<-as-paren-syntax, c->-as-paren-syntax respectively.
5976 ;; STRATEGY:
5978 ;; It is impossible to determine with certainty whether a <..> pair in
5979 ;; C++ is two comparison operators or is template delimiters, unless
5980 ;; one duplicates a lot of a C++ compiler. For example, the following
5981 ;; code fragment:
5983 ;; foo (a < b, c > d) ;
5985 ;; could be a function call with two integer parameters (each a
5986 ;; relational expression), or it could be a constructor for class foo
5987 ;; taking one parameter d of templated type "a < b, c >". They are
5988 ;; somewhat easier to distinguish in Java.
5990 ;; The strategy now (2010-01) adopted is to mark and unmark < and
5991 ;; > IN MATCHING PAIRS ONLY. [Previously, they were marked
5992 ;; individually when their context so indicated. This gave rise to
5993 ;; intractable problems when one of a matching pair was deleted, or
5994 ;; pulled into a literal.]
5996 ;; At each buffer change, the syntax-table properties are removed in a
5997 ;; before-change function and reapplied, when needed, in an
5998 ;; after-change function. It is far more important that the
5999 ;; properties get removed when they they are spurious than that they
6000 ;; be present when wanted.
6001 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
6002 (defun c-clear-<-pair-props (&optional pos)
6003 ;; POS (default point) is at a < character. If it is marked with
6004 ;; open paren syntax-table text property, remove the property,
6005 ;; together with the close paren property on the matching > (if
6006 ;; any).
6007 (save-excursion
6008 (if pos
6009 (goto-char pos)
6010 (setq pos (point)))
6011 (when (equal (c-get-char-property (point) 'syntax-table)
6012 c-<-as-paren-syntax)
6013 (with-syntax-table c-no-parens-syntax-table ; ignore unbalanced [,{,(,..
6014 (c-go-list-forward))
6015 (when (equal (c-get-char-property (1- (point)) 'syntax-table)
6016 c->-as-paren-syntax) ; should always be true.
6017 (c-unmark-<->-as-paren (1- (point))))
6018 (c-unmark-<->-as-paren pos))))
6020 (defun c-clear->-pair-props (&optional pos)
6021 ;; POS (default point) is at a > character. If it is marked with
6022 ;; close paren syntax-table property, remove the property, together
6023 ;; with the open paren property on the matching < (if any).
6024 (save-excursion
6025 (if pos
6026 (goto-char pos)
6027 (setq pos (point)))
6028 (when (equal (c-get-char-property (point) 'syntax-table)
6029 c->-as-paren-syntax)
6030 (with-syntax-table c-no-parens-syntax-table ; ignore unbalanced [,{,(,..
6031 (c-go-up-list-backward))
6032 (when (equal (c-get-char-property (point) 'syntax-table)
6033 c-<-as-paren-syntax) ; should always be true.
6034 (c-unmark-<->-as-paren (point)))
6035 (c-unmark-<->-as-paren pos))))
6037 (defun c-clear-<>-pair-props (&optional pos)
6038 ;; POS (default point) is at a < or > character. If it has an
6039 ;; open/close paren syntax-table property, remove this property both
6040 ;; from the current character and its partner (which will also be
6041 ;; thusly marked).
6042 (cond
6043 ((eq (char-after) ?\<)
6044 (c-clear-<-pair-props pos))
6045 ((eq (char-after) ?\>)
6046 (c-clear->-pair-props pos))
6047 (t (c-benign-error
6048 "c-clear-<>-pair-props called from wrong position"))))
6050 (defun c-clear-<-pair-props-if-match-after (lim &optional pos)
6051 ;; POS (default point) is at a < character. If it is both marked
6052 ;; with open/close paren syntax-table property, and has a matching >
6053 ;; (also marked) which is after LIM, remove the property both from
6054 ;; the current > and its partner. Return t when this happens, nil
6055 ;; when it doesn't.
6056 (save-excursion
6057 (if pos
6058 (goto-char pos)
6059 (setq pos (point)))
6060 (when (equal (c-get-char-property (point) 'syntax-table)
6061 c-<-as-paren-syntax)
6062 (with-syntax-table c-no-parens-syntax-table ; ignore unbalanced [,{,(,..
6063 (c-go-list-forward))
6064 (when (and (>= (point) lim)
6065 (equal (c-get-char-property (1- (point)) 'syntax-table)
6066 c->-as-paren-syntax)) ; should always be true.
6067 (c-unmark-<->-as-paren (1- (point)))
6068 (c-unmark-<->-as-paren pos))
6069 t)))
6071 (defun c-clear->-pair-props-if-match-before (lim &optional pos)
6072 ;; POS (default point) is at a > character. If it is both marked
6073 ;; with open/close paren syntax-table property, and has a matching <
6074 ;; (also marked) which is before LIM, remove the property both from
6075 ;; the current < and its partner. Return t when this happens, nil
6076 ;; when it doesn't.
6077 (save-excursion
6078 (if pos
6079 (goto-char pos)
6080 (setq pos (point)))
6081 (when (equal (c-get-char-property (point) 'syntax-table)
6082 c->-as-paren-syntax)
6083 (with-syntax-table c-no-parens-syntax-table ; ignore unbalanced [,{,(,..
6084 (c-go-up-list-backward))
6085 (when (and (<= (point) lim)
6086 (equal (c-get-char-property (point) 'syntax-table)
6087 c-<-as-paren-syntax)) ; should always be true.
6088 (c-unmark-<->-as-paren (point))
6089 (c-unmark-<->-as-paren pos))
6090 t)))
6092 ;; Set by c-common-init in cc-mode.el.
6093 (defvar c-new-BEG)
6094 (defvar c-new-END)
6095 ;; Set by c-after-change in cc-mode.el.
6096 (defvar c-old-BEG)
6097 (defvar c-old-END)
6099 (defun c-before-change-check-<>-operators (beg end)
6100 ;; Unmark certain pairs of "< .... >" which are currently marked as
6101 ;; template/generic delimiters. (This marking is via syntax-table text
6102 ;; properties), and expand the (c-new-BEG c-new-END) region to include all
6103 ;; unmarked < and > operators within the certain bounds (see below).
6105 ;; These pairs are those which are in the current "statement" (i.e.,
6106 ;; the region between the {, }, or ; before BEG and the one after
6107 ;; END), and which enclose any part of the interval (BEG END).
6109 ;; Note that in C++ (?and Java), template/generic parens cannot
6110 ;; enclose a brace or semicolon, so we use these as bounds on the
6111 ;; region we must work on.
6113 ;; This function is called from before-change-functions (via
6114 ;; c-get-state-before-change-functions). Thus the buffer is widened,
6115 ;; and point is undefined, both at entry and exit.
6117 ;; FIXME!!! This routine ignores the possibility of macros entirely.
6118 ;; 2010-01-29.
6119 (save-excursion
6120 (c-save-buffer-state
6121 ((beg-lit-start (progn (goto-char beg) (c-literal-start)))
6122 (end-lit-limits (progn (goto-char end) (c-literal-limits)))
6123 new-beg new-end beg-limit end-limit)
6124 ;; Locate the earliest < after the barrier before the changed region,
6125 ;; which isn't already marked as a paren.
6126 (goto-char (or beg-lit-start beg))
6127 (setq beg-limit (c-determine-limit 512))
6129 ;; Remove the syntax-table/category properties from each pertinent <...>
6130 ;; pair. Firstly, the ones with the < before beg and > after beg....
6131 (while (progn (c-syntactic-skip-backward "^;{}<" beg-limit)
6132 (eq (char-before) ?<))
6133 (c-backward-token-2)
6134 (when (eq (char-after) ?<)
6135 (c-clear-<-pair-props-if-match-after beg)))
6136 (c-forward-syntactic-ws)
6137 (setq new-beg (point))
6139 ;; ...Then the ones with < before end and > after end.
6140 (goto-char (if end-lit-limits (cdr end-lit-limits) end))
6141 (setq end-limit (c-determine-+ve-limit 512))
6142 (while (and (c-syntactic-re-search-forward "[;{}>]" end-limit 'end)
6143 (eq (char-before) ?>))
6144 (c-end-of-current-token)
6145 (when (eq (char-before) ?>)
6146 (c-clear->-pair-props-if-match-before end (1- (point)))))
6147 (c-backward-syntactic-ws)
6148 (setq new-end (point))
6150 ;; Extend the fontification region, if needed.
6151 (and new-beg
6152 (< new-beg c-new-BEG)
6153 (setq c-new-BEG new-beg))
6154 (and new-end
6155 (> new-end c-new-END)
6156 (setq c-new-END new-end)))))
6158 (defun c-after-change-check-<>-operators (beg end)
6159 ;; This is called from `after-change-functions' when
6160 ;; c-recognize-<>-arglists' is set. It ensures that no "<" or ">"
6161 ;; chars with paren syntax become part of another operator like "<<"
6162 ;; or ">=".
6164 ;; This function might do hidden buffer changes.
6166 (save-excursion
6167 (goto-char beg)
6168 (when (or (looking-at "[<>]")
6169 (< (skip-chars-backward "<>") 0))
6171 (goto-char beg)
6172 (c-beginning-of-current-token)
6173 (when (and (< (point) beg)
6174 (looking-at c-<>-multichar-token-regexp)
6175 (< beg (setq beg (match-end 0))))
6176 (while (progn (skip-chars-forward "^<>" beg)
6177 (< (point) beg))
6178 (c-clear-<>-pair-props)
6179 (forward-char))))
6181 (when (< beg end)
6182 (goto-char end)
6183 (when (or (looking-at "[<>]")
6184 (< (skip-chars-backward "<>") 0))
6186 (goto-char end)
6187 (c-beginning-of-current-token)
6188 (when (and (< (point) end)
6189 (looking-at c-<>-multichar-token-regexp)
6190 (< end (setq end (match-end 0))))
6191 (while (progn (skip-chars-forward "^<>" end)
6192 (< (point) end))
6193 (c-clear-<>-pair-props)
6194 (forward-char)))))))
6196 (defun c-restore-<>-properties (_beg _end _old-len)
6197 ;; This function is called as an after-change function. It restores the
6198 ;; category/syntax-table properties on template/generic <..> pairs between
6199 ;; c-new-BEG and c-new-END. It may do hidden buffer changes.
6200 (c-save-buffer-state ((c-parse-and-markup-<>-arglists t)
6201 c-restricted-<>-arglists lit-limits)
6202 (goto-char c-new-BEG)
6203 (if (setq lit-limits (c-literal-limits))
6204 (goto-char (cdr lit-limits)))
6205 (while (and (< (point) c-new-END)
6206 (c-syntactic-re-search-forward "<" c-new-END 'bound))
6207 (backward-char)
6208 (save-excursion
6209 (c-backward-token-2)
6210 (setq c-restricted-<>-arglists
6211 (and (not (looking-at c-opt-<>-sexp-key))
6212 (progn (c-backward-syntactic-ws) ; to ( or ,
6213 (and (memq (char-before) '(?\( ?,)) ; what about <?
6214 (not (eq (c-get-char-property (point) 'c-type)
6215 'c-decl-arg-start)))))))
6216 (or (c-forward-<>-arglist nil)
6217 (forward-char)))))
6220 ;; Functions to handle C++ raw strings.
6222 ;; A valid C++ raw string looks like
6223 ;; R"<id>(<contents>)<id>"
6224 ;; , where <id> is an identifier from 0 to 16 characters long, not containing
6225 ;; spaces, control characters, double quote or left/right paren. <contents>
6226 ;; can include anything which isn't the terminating )<id>", including new
6227 ;; lines, "s, parentheses, etc.
6229 ;; CC Mode handles C++ raw strings by the use of `syntax-table' text
6230 ;; properties as follows:
6232 ;; (i) On a validly terminated raw string, no `syntax-table' text properties
6233 ;; are applied to the opening and closing delimiters, but any " in the
6234 ;; contents is given the property value "punctuation" (`(1)') to prevent it
6235 ;; interacting with the "s in the delimiters.
6237 ;; The font locking routine `c-font-lock-c++-raw-strings' (in cc-fonts.el)
6238 ;; recognizes valid raw strings, and fontifies the delimiters (apart from
6239 ;; the parentheses) with the default face and the parentheses and the
6240 ;; <contents> with font-lock-string-face.
6242 ;; (ii) A valid, but unterminated, raw string opening delimiter gets the
6243 ;; "punctuation" value (`(1)') of the `syntax-table' text property, and the
6244 ;; open parenthesis gets the "string fence" value (`(15)').
6246 ;; `c-font-lock-c++-raw-strings' puts c-font-lock-warning-face on the entire
6247 ;; unmatched opening delimiter (from the R up to the open paren), and allows
6248 ;; the rest of the buffer to get font-lock-string-face, caused by the
6249 ;; unmatched "string fence" `syntax-table' text property value.
6251 ;; (iii) Inside a macro, a valid raw string is handled as in (i). An
6252 ;; unmatched opening delimiter is handled slightly differently. In addition
6253 ;; to the "punctuation" and "string fence" properties on the delimiter,
6254 ;; another "string fence" `syntax-table' property is applied to the last
6255 ;; possible character of the macro before the terminating linefeed (if there
6256 ;; is such a character after the "("). This "last possible" character is
6257 ;; never a backslash escaping the end of line. If the character preceding
6258 ;; this "last possible" character is itself a backslash, this preceding
6259 ;; character gets a "punctuation" `syntax-table' value. If the "(" is
6260 ;; already at the end of the macro, it gets the "punctuation" value, and no
6261 ;; "string fence"s are used.
6263 ;; The effect on the fontification of either of these tactics is that rest of
6264 ;; the macro (if any) after the "(" gets font-lock-string-face, but the rest
6265 ;; of the file is fontified normally.
6268 (defun c-raw-string-pos ()
6269 ;; Get POINT's relationship to any containing raw string.
6270 ;; If point isn't in a raw string, return nil.
6271 ;; Otherwise, return the following list:
6273 ;; (POS B\" B\( E\) E\")
6275 ;; , where POS is the symbol `open-delim' if point is in the opening
6276 ;; delimiter, the symbol `close-delim' if it's in the closing delimiter, and
6277 ;; nil if it's in the string body. B\", B\(, E\), E\" are the positions of
6278 ;; the opening and closing quotes and parentheses of a correctly terminated
6279 ;; raw string. (N.B.: E\) and E\" are NOT on the "outside" of these
6280 ;; characters.) If the raw string is not terminated, E\) and E\" are set to
6281 ;; nil.
6283 ;; Note: this routine is dependant upon the correct syntax-table text
6284 ;; properties being set.
6285 (let ((state (c-state-semi-pp-to-literal (point)))
6286 open-quote-pos open-paren-pos close-paren-pos close-quote-pos id)
6287 (save-excursion
6288 (when
6289 (and
6290 (cond
6291 ((null (cadr state))
6292 (or (eq (char-after) ?\")
6293 (search-backward "\"" (max (- (point) 17) (point-min)) t)))
6294 ((and (eq (cadr state) 'string)
6295 (goto-char (nth 2 state))
6296 (or (eq (char-after) ?\")
6297 (search-backward "\"" (max (- (point) 17) (point-min)) t))
6298 (not (bobp)))))
6299 (eq (char-before) ?R)
6300 (looking-at "\"\\([^ ()\\\n\r\t]\\{0,16\\}\\)("))
6301 (setq open-quote-pos (point)
6302 open-paren-pos (match-end 1)
6303 id (match-string-no-properties 1))
6304 (goto-char (1+ open-paren-pos))
6305 (when (and (not (c-get-char-property open-paren-pos 'syntax-table))
6306 (search-forward (concat ")" id "\"") nil t))
6307 (setq close-paren-pos (match-beginning 0)
6308 close-quote-pos (1- (point))))))
6309 (and open-quote-pos
6310 (list
6311 (cond
6312 ((<= (point) open-paren-pos)
6313 'open-delim)
6314 ((and close-paren-pos
6315 (> (point) close-paren-pos))
6316 'close-delim)
6317 (t nil))
6318 open-quote-pos open-paren-pos close-paren-pos close-quote-pos))))
6320 (defun c-depropertize-raw-string (id open-quote open-paren bound)
6321 ;; Point is immediately after a raw string opening delimiter. Remove any
6322 ;; `syntax-table' text properties associated with the delimiter (if it's
6323 ;; unmatched) or the raw string.
6325 ;; ID, a string, is the delimiter's identifier. OPEN-QUOTE and OPEN-PAREN
6326 ;; are the buffer positions of the delimiter's components. BOUND is the
6327 ;; bound for searching for a matching closing delimiter; it is usually nil,
6328 ;; but if we're inside a macro, it's the end of the macro.
6330 ;; Point is moved to after the (terminated) raw string, or left after the
6331 ;; unmatched opening delimiter, as the case may be. The return value is of
6332 ;; no significance.
6333 (let ((open-paren-prop (c-get-char-property open-paren 'syntax-table)))
6334 (cond
6335 ((null open-paren-prop)
6336 ;; A terminated raw string
6337 (when (search-forward (concat ")" id "\"") nil t)
6338 (let* ((closing-paren (match-beginning 0))
6339 (first-punctuation
6340 (save-match-data
6341 (goto-char (1+ open-paren))
6342 (and (c-search-forward-char-property 'syntax-table '(1)
6343 closing-paren)
6344 (1- (point)))))
6346 (when first-punctuation
6347 (c-clear-char-property-with-value
6348 first-punctuation (match-beginning 0) 'syntax-table '(1))
6349 (c-truncate-semi-nonlit-pos-cache first-punctuation)
6350 ))))
6351 ((or (and (equal open-paren-prop '(15)) (null bound))
6352 (equal open-paren-prop '(1)))
6353 ;; An unterminated raw string either not in a macro, or in a macro with
6354 ;; the open parenthesis right up against the end of macro
6355 (c-clear-char-property open-quote 'syntax-table)
6356 (c-truncate-semi-nonlit-pos-cache open-quote)
6357 (c-clear-char-property open-paren 'syntax-table))
6359 ;; An unterminated string in a macro, with at least one char after the
6360 ;; open paren
6361 (c-clear-char-property open-quote 'syntax-table)
6362 (c-truncate-semi-nonlit-pos-cache open-quote)
6363 (c-clear-char-property open-paren 'syntax-table)
6364 (let ((after-string-fence-pos
6365 (save-excursion
6366 (goto-char (1+ open-paren))
6367 (c-search-forward-char-property 'syntax-table '(15) bound))))
6368 (when after-string-fence-pos
6369 (c-clear-char-property (1- after-string-fence-pos) 'syntax-table)))
6370 ))))
6372 (defun c-depropertize-raw-strings-in-region (start finish)
6373 ;; Remove any `syntax-table' text properties associated with C++ raw strings
6374 ;; contained in the region (START FINISH). Point is undefined at entry and
6375 ;; exit, and the return value has no significance.
6376 (goto-char start)
6377 (while (and (< (point) finish)
6378 (re-search-forward
6379 (concat "\\(" ; 1
6380 c-anchored-cpp-prefix ; 2
6381 "\\)\\|\\(" ; 3
6382 "R\"\\([^ ()\\\n\r\t]\\{0,16\\}\\)(" ; 4
6383 "\\)")
6384 finish t))
6385 (when (save-excursion
6386 (goto-char (match-beginning 0)) (not (c-in-literal)))
6387 (if (match-beginning 4) ; the id
6388 ;; We've found a raw string
6389 (c-depropertize-raw-string
6390 (match-string-no-properties 4) ; id
6391 (1+ (match-beginning 3)) ; open quote
6392 (match-end 4) ; open paren
6393 nil) ; bound
6394 ;; We've found a CPP construct. Search for raw strings within it.
6395 (goto-char (match-beginning 2)) ; the "#"
6396 (c-end-of-macro)
6397 (let ((eom (point)))
6398 (goto-char (match-end 2)) ; after the "#".
6399 (while (and (< (point) eom)
6400 (c-syntactic-re-search-forward
6401 "R\"\\([^ ()\\\n\r\t]\\{0,16\\}\\)(" eom t))
6402 (c-depropertize-raw-string
6403 (match-string-no-properties 1) ; id
6404 (1+ (match-beginning 0)) ; open quote
6405 (match-end 1) ; open paren
6406 eom))))))) ; bound.
6408 (defun c-before-change-check-raw-strings (beg end)
6409 ;; This function clears `syntax-table' text properties from C++ raw strings
6410 ;; in the region (c-new-BEG c-new-END). BEG and END are the standard
6411 ;; arguments supplied to any before-change function.
6413 ;; Point is undefined on both entry and exit, and the return value has no
6414 ;; significance.
6416 ;; This function is called as a before-change function solely due to its
6417 ;; membership of the C++ value of `c-get-state-before-change-functions'.
6418 (c-save-buffer-state
6419 ((beg-rs (progn (goto-char beg) (c-raw-string-pos)))
6420 (beg-plus (if (null beg-rs)
6422 (max beg
6423 (1+ (or (nth 4 beg-rs) (nth 2 beg-rs))))))
6424 (end-rs (progn (goto-char end) (c-raw-string-pos))) ; FIXME!!!
6425 ; Optimize this so that we don't call
6426 ; `c-raw-string-pos' twice when once
6427 ; will do. (2016-06-02).
6428 (end-minus (if (null end-rs)
6430 (min end (cadr end-rs))))
6432 (when beg-rs
6433 (setq c-new-BEG (min c-new-BEG (1- (cadr beg-rs)))))
6434 (c-depropertize-raw-strings-in-region c-new-BEG beg-plus)
6436 (when end-rs
6437 (setq c-new-END (max c-new-END
6438 (1+ (or (nth 4 end-rs)
6439 (nth 2 end-rs))))))
6440 (c-depropertize-raw-strings-in-region end-minus c-new-END)))
6442 (defun c-propertize-raw-string-opener (id open-quote open-paren bound)
6443 ;; Point is immediately after a raw string opening delimiter. Apply any
6444 ;; pertinent `syntax-table' text properties to the delimiter and also the
6445 ;; raw string, should there be a valid matching closing delimiter.
6447 ;; ID, a string, is the delimiter's identifier. OPEN-QUOTE and OPEN-PAREN
6448 ;; are the buffer positions of the delimiter's components. BOUND is the
6449 ;; bound for searching for a matching closing delimiter; it is usually nil,
6450 ;; but if we're inside a macro, it's the end of the macro.
6452 ;; Point is moved to after the (terminated) raw string, or left after the
6453 ;; unmatched opening delimiter, as the case may be. The return value is of
6454 ;; no significance.
6455 (if (search-forward (concat ")" id "\"") bound t)
6456 (let ((end-string (match-beginning 0))
6457 (after-quote (match-end 0)))
6458 (goto-char open-paren)
6459 (while (progn (skip-syntax-forward "^\"" end-string)
6460 (< (point) end-string))
6461 (c-put-char-property (point) 'syntax-table '(1)) ; punctuation
6462 (c-truncate-semi-nonlit-pos-cache (point))
6463 (forward-char))
6464 (goto-char after-quote))
6465 (c-put-char-property open-quote 'syntax-table '(1)) ; punctuation
6466 (c-truncate-semi-nonlit-pos-cache open-quote)
6467 (c-put-char-property open-paren 'syntax-table '(15)) ; generic string
6468 (when bound
6469 ;; In a CPP construct, we try to apply a generic-string `syntax-table'
6470 ;; text property to the last possible character in the string, so that
6471 ;; only characters within the macro get "stringed out".
6472 (goto-char bound)
6473 (if (save-restriction
6474 (narrow-to-region (1+ open-paren) (point-max))
6475 (re-search-backward
6476 (eval-when-compile
6477 ;; This regular expression matches either an escape pair (which
6478 ;; isn't an escaped NL) (submatch 5) or a non-escaped character
6479 ;; (which isn't itself a backslash) (submatch 10). The long
6480 ;; preambles to these (respectively submatches 2-4 and 6-9)
6481 ;; ensure that we have the correct parity for sequences of
6482 ;; backslashes, etc..
6483 (concat "\\(" ; 1
6484 "\\(\\`[^\\]?\\|[^\\][^\\]\\)\\(\\\\\\(.\\|\n\\)\\)*" ; 2-4
6485 "\\(\\\\.\\)" ; 5
6486 "\\|"
6487 "\\(\\`\\|[^\\]\\|\\(\\`[^\\]?\\|[^\\][^\\]\\)\\(\\\\\\(.\\|\n\\)\\)+\\)" ; 6-9
6488 "\\([^\\]\\)" ; 10
6489 "\\)"
6490 "\\(\\\\\n\\)*\\=")) ; 11
6491 (1+ open-paren) t))
6492 (if (match-beginning 10)
6493 (progn
6494 (c-put-char-property (match-beginning 10) 'syntax-table '(15))
6495 (c-truncate-semi-nonlit-pos-cache (match-beginning 10)))
6496 (c-put-char-property (match-beginning 5) 'syntax-table '(1))
6497 (c-put-char-property (1+ (match-beginning 5)) 'syntax-table '(15))
6498 (c-truncate-semi-nonlit-pos-cache (1+ (match-beginning 5))))
6499 (c-put-char-property open-paren 'syntax-table '(1)))
6500 (goto-char bound))))
6502 (defun c-after-change-re-mark-raw-strings (beg end old-len)
6503 ;; This function applies `syntax-table' text properties to C++ raw strings
6504 ;; beginning in the region (c-new-BEG c-new-END). BEG, END, and OLD-LEN are
6505 ;; the standard arguments supplied to any after-change function.
6507 ;; Point is undefined on both entry and exit, and the return value has no
6508 ;; significance.
6510 ;; This function is called as an after-change function solely due to its
6511 ;; membership of the C++ value of `c-before-font-lock-functions'.
6512 (c-save-buffer-state ()
6513 ;; If the region (c-new-BEG c-new-END) has expanded, remove
6514 ;; `syntax-table' text-properties from the new piece(s).
6515 (when (< c-new-BEG c-old-BEG)
6516 (let ((beg-rs (progn (goto-char c-old-BEG) (c-raw-string-pos))))
6517 (c-depropertize-raw-strings-in-region
6518 c-new-BEG
6519 (if beg-rs
6520 (1+ (or (nth 4 beg-rs) (nth 2 beg-rs)))
6521 c-old-BEG))))
6522 (when (> c-new-END c-old-END)
6523 (let ((end-rs (progn (goto-char c-old-END) (c-raw-string-pos))))
6524 (c-depropertize-raw-strings-in-region
6525 (if end-rs
6526 (cadr end-rs)
6527 c-old-END)
6528 c-new-END)))
6530 (goto-char c-new-BEG)
6531 (while (and (< (point) c-new-END)
6532 (re-search-forward
6533 (concat "\\(" ; 1
6534 c-anchored-cpp-prefix ; 2
6535 "\\)\\|\\(" ; 3
6536 "R\"\\([^ ()\\\n\r\t]\\{0,16\\}\\)(" ; 4
6537 "\\)")
6538 c-new-END t))
6539 (when (save-excursion
6540 (goto-char (match-beginning 0)) (not (c-in-literal)))
6541 (if (match-beginning 4) ; the id
6542 ;; We've found a raw string.
6543 (c-propertize-raw-string-opener
6544 (match-string-no-properties 4) ; id
6545 (1+ (match-beginning 3)) ; open quote
6546 (match-end 4) ; open paren
6547 nil) ; bound
6548 ;; We've found a CPP construct. Search for raw strings within it.
6549 (goto-char (match-beginning 2)) ; the "#"
6550 (c-end-of-macro)
6551 (let ((eom (point)))
6552 (goto-char (match-end 2)) ; after the "#".
6553 (while (and (< (point) eom)
6554 (c-syntactic-re-search-forward
6555 "R\"\\([^ ()\\\n\r\t]\\{0,16\\}\\)(" eom t))
6556 (c-propertize-raw-string-opener
6557 (match-string-no-properties 1) ; id
6558 (1+ (match-beginning 0)) ; open quote
6559 (match-end 1) ; open paren
6560 eom)))))))) ; bound
6563 ;; Handling of small scale constructs like types and names.
6565 ;; Dynamically bound variable that instructs `c-forward-type' to also
6566 ;; treat possible types (i.e. those that it normally returns 'maybe or
6567 ;; 'found for) as actual types (and always return 'found for them).
6568 ;; This means that it records them in `c-record-type-identifiers' if
6569 ;; that is set, and that it adds them to `c-found-types'.
6570 (defvar c-promote-possible-types nil)
6572 ;; Dynamically bound variable that instructs `c-forward-<>-arglist' to
6573 ;; mark up successfully parsed arglists with paren syntax properties on
6574 ;; the surrounding angle brackets and with `c-<>-arg-sep' in the
6575 ;; `c-type' property of each argument separating comma.
6577 ;; Setting this variable also makes `c-forward-<>-arglist' recurse into
6578 ;; all arglists for side effects (i.e. recording types), otherwise it
6579 ;; exploits any existing paren syntax properties to quickly jump to the
6580 ;; end of already parsed arglists.
6582 ;; Marking up the arglists is not the default since doing that correctly
6583 ;; depends on a proper value for `c-restricted-<>-arglists'.
6584 (defvar c-parse-and-markup-<>-arglists nil)
6586 ;; Dynamically bound variable that instructs `c-forward-<>-arglist' to
6587 ;; not accept arglists that contain binary operators.
6589 ;; This is primarily used to handle C++ template arglists. C++
6590 ;; disambiguates them by checking whether the preceding name is a
6591 ;; template or not. We can't do that, so we assume it is a template
6592 ;; if it can be parsed as one. That usually works well since
6593 ;; comparison expressions on the forms "a < b > c" or "a < b, c > d"
6594 ;; in almost all cases would be pointless.
6596 ;; However, in function arglists, e.g. in "foo (a < b, c > d)", we
6597 ;; should let the comma separate the function arguments instead. And
6598 ;; in a context where the value of the expression is taken, e.g. in
6599 ;; "if (a < b || c > d)", it's probably not a template.
6600 (defvar c-restricted-<>-arglists nil)
6602 ;; Dynamically bound variables that instructs
6603 ;; `c-forward-keyword-clause', `c-forward-<>-arglist',
6604 ;; `c-forward-name', `c-forward-type', `c-forward-decl-or-cast-1', and
6605 ;; `c-forward-label' to record the ranges of all the type and
6606 ;; reference identifiers they encounter. They will build lists on
6607 ;; these variables where each element is a cons of the buffer
6608 ;; positions surrounding each identifier. This recording is only
6609 ;; activated when `c-record-type-identifiers' is non-nil.
6611 ;; All known types that can't be identifiers are recorded, and also
6612 ;; other possible types if `c-promote-possible-types' is set.
6613 ;; Recording is however disabled inside angle bracket arglists that
6614 ;; are encountered inside names and other angle bracket arglists.
6615 ;; Such occurrences are taken care of by `c-font-lock-<>-arglists'
6616 ;; instead.
6618 ;; Only the names in C++ template style references (e.g. "tmpl" in
6619 ;; "tmpl<a,b>::foo") are recorded as references, other references
6620 ;; aren't handled here.
6622 ;; `c-forward-label' records the label identifier(s) on
6623 ;; `c-record-ref-identifiers'.
6624 (defvar c-record-type-identifiers nil)
6625 (defvar c-record-ref-identifiers nil)
6627 ;; This variable will receive a cons cell of the range of the last
6628 ;; single identifier symbol stepped over by `c-forward-name' if it's
6629 ;; successful. This is the range that should be put on one of the
6630 ;; record lists above by the caller. It's assigned nil if there's no
6631 ;; such symbol in the name.
6632 (defvar c-last-identifier-range nil)
6634 (defmacro c-record-type-id (range)
6635 (if (eq (car-safe range) 'cons)
6636 ;; Always true.
6637 `(setq c-record-type-identifiers
6638 (cons ,range c-record-type-identifiers))
6639 `(let ((range ,range))
6640 (if range
6641 (setq c-record-type-identifiers
6642 (cons range c-record-type-identifiers))))))
6644 (defmacro c-record-ref-id (range)
6645 (if (eq (car-safe range) 'cons)
6646 ;; Always true.
6647 `(setq c-record-ref-identifiers
6648 (cons ,range c-record-ref-identifiers))
6649 `(let ((range ,range))
6650 (if range
6651 (setq c-record-ref-identifiers
6652 (cons range c-record-ref-identifiers))))))
6654 ;; Dynamically bound variable that instructs `c-forward-type' to
6655 ;; record the ranges of types that only are found. Behaves otherwise
6656 ;; like `c-record-type-identifiers'.
6657 (defvar c-record-found-types nil)
6659 (defmacro c-forward-keyword-prefixed-id (type)
6660 ;; Used internally in `c-forward-keyword-clause' to move forward
6661 ;; over a type (if TYPE is 'type) or a name (otherwise) which
6662 ;; possibly is prefixed by keywords and their associated clauses.
6663 ;; Try with a type/name first to not trip up on those that begin
6664 ;; with a keyword. Return t if a known or found type is moved
6665 ;; over. The point is clobbered if nil is returned. If range
6666 ;; recording is enabled, the identifier is recorded on as a type
6667 ;; if TYPE is 'type or as a reference if TYPE is 'ref.
6669 ;; This macro might do hidden buffer changes.
6670 `(let (res)
6671 (setq c-last-identifier-range nil)
6672 (while (if (setq res ,(if (eq type 'type)
6673 `(c-forward-type)
6674 `(c-forward-name)))
6676 (cond ((looking-at c-keywords-regexp)
6677 (c-forward-keyword-clause 1))
6678 ((and c-opt-cpp-prefix
6679 (looking-at c-noise-macro-with-parens-name-re))
6680 (c-forward-noise-clause)))))
6681 (when (memq res '(t known found prefix maybe))
6682 (when c-record-type-identifiers
6683 ,(if (eq type 'type)
6684 `(c-record-type-id c-last-identifier-range)
6685 `(c-record-ref-id c-last-identifier-range)))
6686 t)))
6688 (defmacro c-forward-id-comma-list (type update-safe-pos)
6689 ;; Used internally in `c-forward-keyword-clause' to move forward
6690 ;; over a comma separated list of types or names using
6691 ;; `c-forward-keyword-prefixed-id'.
6693 ;; This macro might do hidden buffer changes.
6694 `(while (and (progn
6695 ,(when update-safe-pos
6696 `(setq safe-pos (point)))
6697 (eq (char-after) ?,))
6698 (progn
6699 (forward-char)
6700 (c-forward-syntactic-ws)
6701 (c-forward-keyword-prefixed-id ,type)))))
6703 (defun c-forward-noise-clause ()
6704 ;; Point is at a c-noise-macro-with-parens-names macro identifier. Go
6705 ;; forward over this name, any parenthesis expression which follows it, and
6706 ;; any syntactic WS, ending up at the next token. If there is an unbalanced
6707 ;; paren expression, leave point at it. Always Return t.
6708 (c-forward-token-2)
6709 (if (and (eq (char-after) ?\()
6710 (c-go-list-forward))
6711 (c-forward-syntactic-ws))
6714 (defun c-forward-keyword-clause (match)
6715 ;; Submatch MATCH in the current match data is assumed to surround a
6716 ;; token. If it's a keyword, move over it and any immediately
6717 ;; following clauses associated with it, stopping at the start of
6718 ;; the next token. t is returned in that case, otherwise the point
6719 ;; stays and nil is returned. The kind of clauses that are
6720 ;; recognized are those specified by `c-type-list-kwds',
6721 ;; `c-ref-list-kwds', `c-colon-type-list-kwds',
6722 ;; `c-paren-nontype-kwds', `c-paren-type-kwds', `c-<>-type-kwds',
6723 ;; and `c-<>-arglist-kwds'.
6725 ;; This function records identifier ranges on
6726 ;; `c-record-type-identifiers' and `c-record-ref-identifiers' if
6727 ;; `c-record-type-identifiers' is non-nil.
6729 ;; Note that for `c-colon-type-list-kwds', which doesn't necessary
6730 ;; apply directly after the keyword, the type list is moved over
6731 ;; only when there is no unaccounted token before it (i.e. a token
6732 ;; that isn't moved over due to some other keyword list). The
6733 ;; identifier ranges in the list are still recorded if that should
6734 ;; be done, though.
6736 ;; This function might do hidden buffer changes.
6738 (let ((kwd-sym (c-keyword-sym (match-string match))) safe-pos pos
6739 ;; The call to `c-forward-<>-arglist' below is made after
6740 ;; `c-<>-sexp-kwds' keywords, so we're certain they actually
6741 ;; are angle bracket arglists and `c-restricted-<>-arglists'
6742 ;; should therefore be nil.
6743 (c-parse-and-markup-<>-arglists t)
6744 c-restricted-<>-arglists)
6746 (when kwd-sym
6747 (goto-char (match-end match))
6748 (c-forward-syntactic-ws)
6749 (setq safe-pos (point))
6751 (cond
6752 ((and (c-keyword-member kwd-sym 'c-type-list-kwds)
6753 (c-forward-keyword-prefixed-id type))
6754 ;; There's a type directly after a keyword in `c-type-list-kwds'.
6755 (c-forward-id-comma-list type t))
6757 ((and (c-keyword-member kwd-sym 'c-ref-list-kwds)
6758 (c-forward-keyword-prefixed-id ref))
6759 ;; There's a name directly after a keyword in `c-ref-list-kwds'.
6760 (c-forward-id-comma-list ref t))
6762 ((and (c-keyword-member kwd-sym 'c-paren-any-kwds)
6763 (eq (char-after) ?\())
6764 ;; There's an open paren after a keyword in `c-paren-any-kwds'.
6766 (forward-char)
6767 (when (and (setq pos (c-up-list-forward))
6768 (eq (char-before pos) ?\)))
6769 (when (and c-record-type-identifiers
6770 (c-keyword-member kwd-sym 'c-paren-type-kwds))
6771 ;; Use `c-forward-type' on every identifier we can find
6772 ;; inside the paren, to record the types.
6773 (while (c-syntactic-re-search-forward c-symbol-start pos t)
6774 (goto-char (match-beginning 0))
6775 (unless (c-forward-type)
6776 (looking-at c-symbol-key) ; Always matches.
6777 (goto-char (match-end 0)))))
6779 (goto-char pos)
6780 (c-forward-syntactic-ws)
6781 (setq safe-pos (point))))
6783 ((and (c-keyword-member kwd-sym 'c-<>-sexp-kwds)
6784 (eq (char-after) ?<)
6785 (c-forward-<>-arglist (c-keyword-member kwd-sym 'c-<>-type-kwds)))
6786 (c-forward-syntactic-ws)
6787 (setq safe-pos (point)))
6789 ((and (c-keyword-member kwd-sym 'c-nonsymbol-sexp-kwds)
6790 (not (looking-at c-symbol-start))
6791 (c-safe (c-forward-sexp) t))
6792 (c-forward-syntactic-ws)
6793 (setq safe-pos (point))))
6795 (when (c-keyword-member kwd-sym 'c-colon-type-list-kwds)
6796 (if (eq (char-after) ?:)
6797 ;; If we are at the colon already, we move over the type
6798 ;; list after it.
6799 (progn
6800 (forward-char)
6801 (c-forward-syntactic-ws)
6802 (when (c-forward-keyword-prefixed-id type)
6803 (c-forward-id-comma-list type t)))
6804 ;; Not at the colon, so stop here. But the identifier
6805 ;; ranges in the type list later on should still be
6806 ;; recorded.
6807 (and c-record-type-identifiers
6808 (progn
6809 ;; If a keyword matched both one of the types above and
6810 ;; this one, we match `c-colon-type-list-re' after the
6811 ;; clause matched above.
6812 (goto-char safe-pos)
6813 (looking-at c-colon-type-list-re))
6814 (progn
6815 (goto-char (match-end 0))
6816 (c-forward-syntactic-ws)
6817 (c-forward-keyword-prefixed-id type))
6818 ;; There's a type after the `c-colon-type-list-re' match
6819 ;; after a keyword in `c-colon-type-list-kwds'.
6820 (c-forward-id-comma-list type nil))))
6822 (goto-char safe-pos)
6823 t)))
6825 ;; cc-mode requires cc-fonts.
6826 (declare-function c-fontify-recorded-types-and-refs "cc-fonts" ())
6828 (defun c-forward-<>-arglist (all-types)
6829 ;; The point is assumed to be at a "<". Try to treat it as the open
6830 ;; paren of an angle bracket arglist and move forward to the
6831 ;; corresponding ">". If successful, the point is left after the
6832 ;; ">" and t is returned, otherwise the point isn't moved and nil is
6833 ;; returned. If ALL-TYPES is t then all encountered arguments in
6834 ;; the arglist that might be types are treated as found types.
6836 ;; The variable `c-parse-and-markup-<>-arglists' controls how this
6837 ;; function handles text properties on the angle brackets and argument
6838 ;; separating commas.
6840 ;; `c-restricted-<>-arglists' controls how lenient the template
6841 ;; arglist recognition should be.
6843 ;; This function records identifier ranges on
6844 ;; `c-record-type-identifiers' and `c-record-ref-identifiers' if
6845 ;; `c-record-type-identifiers' is non-nil.
6847 ;; This function might do hidden buffer changes.
6849 (let ((start (point))
6850 ;; If `c-record-type-identifiers' is set then activate
6851 ;; recording of any found types that constitute an argument in
6852 ;; the arglist.
6853 (c-record-found-types (if c-record-type-identifiers t)))
6854 (if (catch 'angle-bracket-arglist-escape
6855 (setq c-record-found-types
6856 (c-forward-<>-arglist-recur all-types)))
6857 (progn
6858 (when (consp c-record-found-types)
6859 (setq c-record-type-identifiers
6860 ;; `nconc' doesn't mind that the tail of
6861 ;; `c-record-found-types' is t.
6862 (nconc c-record-found-types c-record-type-identifiers)))
6865 (goto-char start)
6866 nil)))
6868 (defun c-forward-<>-arglist-recur (all-types)
6869 ;; Recursive part of `c-forward-<>-arglist'.
6871 ;; This function might do hidden buffer changes.
6872 (let ((start (point)) res pos
6873 ;; Cover this so that any recorded found type ranges are
6874 ;; automatically lost if it turns out to not be an angle
6875 ;; bracket arglist. It's propagated through the return value
6876 ;; on successful completion.
6877 (c-record-found-types c-record-found-types)
6878 ;; List that collects the positions after the argument
6879 ;; separating ',' in the arglist.
6880 arg-start-pos)
6881 ;; If the '<' has paren open syntax then we've marked it as an angle
6882 ;; bracket arglist before, so skip to the end.
6883 (if (and (not c-parse-and-markup-<>-arglists)
6884 (c-get-char-property (point) 'syntax-table))
6886 (progn
6887 (forward-char)
6888 (if (and (c-go-up-list-forward)
6889 (eq (char-before) ?>))
6891 ;; Got unmatched paren angle brackets. We don't clear the paren
6892 ;; syntax properties and retry, on the basis that it's very
6893 ;; unlikely that paren angle brackets become operators by code
6894 ;; manipulation. It's far more likely that it doesn't match due
6895 ;; to narrowing or some temporary change.
6896 (goto-char start)
6897 nil))
6899 (forward-char) ; Forward over the opening '<'.
6901 (unless (looking-at c-<-op-cont-regexp)
6902 ;; go forward one non-alphanumeric character (group) per iteration of
6903 ;; this loop.
6904 (while (and
6905 (progn
6906 (c-forward-syntactic-ws)
6907 (when (or (and c-record-type-identifiers all-types)
6908 (not (equal c-inside-<>-type-key "\\(\\<\\>\\)")))
6909 (c-forward-syntactic-ws)
6910 (cond
6911 ((eq (char-after) ??)
6912 (forward-char))
6913 ((and (looking-at c-identifier-start)
6914 (not (looking-at c-keywords-regexp)))
6915 (if (or (and all-types c-record-type-identifiers)
6916 (c-major-mode-is 'java-mode))
6917 ;; All encountered identifiers are types, so set the
6918 ;; promote flag and parse the type.
6919 (let ((c-promote-possible-types t)
6920 (c-record-found-types t))
6921 (c-forward-type))
6922 (c-forward-token-2))))
6924 (c-forward-syntactic-ws)
6926 (when (looking-at c-inside-<>-type-key)
6927 (goto-char (match-end 1))
6928 (c-forward-syntactic-ws)
6929 (let ((c-promote-possible-types t)
6930 (c-record-found-types t))
6931 (c-forward-type))
6932 (c-forward-syntactic-ws)))
6934 (setq pos (point)) ; e.g. first token inside the '<'
6936 ;; Note: These regexps exploit the match order in \| so
6937 ;; that "<>" is matched by "<" rather than "[^>:-]>".
6938 (c-syntactic-re-search-forward
6939 ;; Stop on ',', '|', '&', '+' and '-' to catch
6940 ;; common binary operators that could be between
6941 ;; two comparison expressions "a<b" and "c>d".
6942 ;; 2016-02-11: C++11 templates can now contain arithmetic
6943 ;; expressions, so template detection in C++ is now less
6944 ;; robust than it was.
6945 c-<>-notable-chars-re
6946 nil t t))
6948 (cond
6949 ((eq (char-before) ?>)
6950 ;; Either an operator starting with '>' or the end of
6951 ;; the angle bracket arglist.
6953 (if (save-excursion
6954 (c-backward-token-2)
6955 (looking-at c-multichar->-op-not->>-regexp))
6956 (progn
6957 (goto-char (match-end 0))
6958 t) ; Continue the loop.
6960 ;; The angle bracket arglist is finished.
6961 (when c-parse-and-markup-<>-arglists
6962 (while arg-start-pos
6963 (c-put-c-type-property (1- (car arg-start-pos))
6964 'c-<>-arg-sep)
6965 (setq arg-start-pos (cdr arg-start-pos)))
6966 (c-mark-<-as-paren start)
6967 (c-mark->-as-paren (1- (point))))
6968 (setq res t)
6969 nil)) ; Exit the loop.
6971 ((eq (char-before) ?<)
6972 ;; Either an operator starting with '<' or a nested arglist.
6973 (setq pos (point))
6974 (let (id-start id-end subres keyword-match)
6975 (cond
6976 ;; The '<' begins a multi-char operator.
6977 ((looking-at c-<-op-cont-regexp)
6978 (goto-char (match-end 0)))
6979 ;; We're at a nested <.....>
6980 ((progn
6981 (backward-char) ; to the '<'
6982 (and
6983 (save-excursion
6984 ;; There's always an identifier before an angle
6985 ;; bracket arglist, or a keyword in `c-<>-type-kwds'
6986 ;; or `c-<>-arglist-kwds'.
6987 (c-backward-syntactic-ws)
6988 (setq id-end (point))
6989 (c-simple-skip-symbol-backward)
6990 (when (or (setq keyword-match
6991 (looking-at c-opt-<>-sexp-key))
6992 (not (looking-at c-keywords-regexp)))
6993 (setq id-start (point))))
6994 (setq subres
6995 (let ((c-promote-possible-types t)
6996 (c-record-found-types t))
6997 (c-forward-<>-arglist-recur
6998 (and keyword-match
6999 (c-keyword-member
7000 (c-keyword-sym (match-string 1))
7001 'c-<>-type-kwds))))))
7002 (or subres (goto-char pos))
7003 subres)
7004 ;; It was an angle bracket arglist.
7005 (setq c-record-found-types subres)
7007 ;; Record the identifier before the template as a type
7008 ;; or reference depending on whether the arglist is last
7009 ;; in a qualified identifier.
7010 (when (and c-record-type-identifiers
7011 (not keyword-match))
7012 (if (and c-opt-identifier-concat-key
7013 (progn
7014 (c-forward-syntactic-ws)
7015 (looking-at c-opt-identifier-concat-key)))
7016 (c-record-ref-id (cons id-start id-end))
7017 (c-record-type-id (cons id-start id-end)))))
7019 ;; At a "less than" operator.
7021 ;; (forward-char) ; NO! We've already gone over the <.
7023 t) ; carry on looping.
7025 ((and
7026 (eq (char-before) ?\()
7027 (c-go-up-list-forward)
7028 (eq (char-before) ?\))))
7030 ((and (not c-restricted-<>-arglists)
7031 (or (and (eq (char-before) ?&)
7032 (not (eq (char-after) ?&)))
7033 (eq (char-before) ?,)))
7034 ;; Just another argument. Record the position. The
7035 ;; type check stuff that made us stop at it is at
7036 ;; the top of the loop.
7037 (setq arg-start-pos (cons (point) arg-start-pos)))
7040 ;; Got a character that can't be in an angle bracket
7041 ;; arglist argument. Abort using `throw', since
7042 ;; it's useless to try to find a surrounding arglist
7043 ;; if we're nested.
7044 (throw 'angle-bracket-arglist-escape nil))))))
7045 (if res
7046 (or c-record-found-types t)))))
7048 (defun c-backward-<>-arglist (all-types &optional limit)
7049 ;; The point is assumed to be directly after a ">". Try to treat it
7050 ;; as the close paren of an angle bracket arglist and move back to
7051 ;; the corresponding "<". If successful, the point is left at
7052 ;; the "<" and t is returned, otherwise the point isn't moved and
7053 ;; nil is returned. ALL-TYPES is passed on to
7054 ;; `c-forward-<>-arglist'.
7056 ;; If the optional LIMIT is given, it bounds the backward search.
7057 ;; It's then assumed to be at a syntactically relevant position.
7059 ;; This is a wrapper around `c-forward-<>-arglist'. See that
7060 ;; function for more details.
7062 (let ((start (point)))
7063 (backward-char)
7064 (if (and (not c-parse-and-markup-<>-arglists)
7065 (c-get-char-property (point) 'syntax-table))
7067 (if (and (c-go-up-list-backward)
7068 (eq (char-after) ?<))
7070 ;; See corresponding note in `c-forward-<>-arglist'.
7071 (goto-char start)
7072 nil)
7074 (while (progn
7075 (c-syntactic-skip-backward "^<;{}" limit t)
7077 (and
7078 (if (eq (char-before) ?<)
7080 ;; Stopped at bob or a char that isn't allowed in an
7081 ;; arglist, so we've failed.
7082 (goto-char start)
7083 nil)
7085 (if (> (point)
7086 (progn (c-beginning-of-current-token)
7087 (point)))
7088 ;; If we moved then the "<" was part of some
7089 ;; multicharacter token.
7092 (backward-char)
7093 (let ((beg-pos (point)))
7094 (if (c-forward-<>-arglist all-types)
7095 (cond ((= (point) start)
7096 ;; Matched the arglist. Break the while.
7097 (goto-char beg-pos)
7098 nil)
7099 ((> (point) start)
7100 ;; We started from a non-paren ">" inside an
7101 ;; arglist.
7102 (goto-char start)
7103 nil)
7105 ;; Matched a shorter arglist. Can be a nested
7106 ;; one so continue looking.
7107 (goto-char beg-pos)
7109 t))))))
7111 (/= (point) start))))
7113 (defun c-forward-name ()
7114 ;; Move forward over a complete name if at the beginning of one,
7115 ;; stopping at the next following token. A keyword, as such,
7116 ;; doesn't count as a name. If the point is not at something that
7117 ;; is recognized as a name then it stays put.
7119 ;; A name could be something as simple as "foo" in C or something as
7120 ;; complex as "X<Y<class A<int>::B, BIT_MAX >> b>, ::operator<> ::
7121 ;; Z<(a>b)> :: operator const X<&foo>::T Q::G<unsigned short
7122 ;; int>::*volatile const" in C++ (this function is actually little
7123 ;; more than a `looking-at' call in all modes except those that,
7124 ;; like C++, have `c-recognize-<>-arglists' set).
7126 ;; Return
7127 ;; o - nil if no name is found;
7128 ;; o - 'template if it's an identifier ending with an angle bracket
7129 ;; arglist;
7130 ;; o - 'operator of it's an operator identifier;
7131 ;; o - t if it's some other kind of name.
7133 ;; This function records identifier ranges on
7134 ;; `c-record-type-identifiers' and `c-record-ref-identifiers' if
7135 ;; `c-record-type-identifiers' is non-nil.
7137 ;; This function might do hidden buffer changes.
7139 (let ((pos (point)) (start (point)) res id-start id-end
7140 ;; Turn off `c-promote-possible-types' here since we might
7141 ;; call `c-forward-<>-arglist' and we don't want it to promote
7142 ;; every suspect thing in the arglist to a type. We're
7143 ;; typically called from `c-forward-type' in this case, and
7144 ;; the caller only wants the top level type that it finds to
7145 ;; be promoted.
7146 c-promote-possible-types)
7147 (while
7148 (and
7149 (looking-at c-identifier-key)
7151 (progn
7152 ;; Check for keyword. We go to the last symbol in
7153 ;; `c-identifier-key' first.
7154 (goto-char (setq id-end (match-end 0)))
7155 (c-simple-skip-symbol-backward)
7156 (setq id-start (point))
7158 (if (looking-at c-keywords-regexp)
7159 (when (and (c-major-mode-is 'c++-mode)
7160 (looking-at
7161 (cc-eval-when-compile
7162 (concat "\\(operator\\|\\(template\\)\\)"
7163 "\\(" (c-lang-const c-nonsymbol-key c++)
7164 "\\|$\\)")))
7165 (if (match-beginning 2)
7166 ;; "template" is only valid inside an
7167 ;; identifier if preceded by "::".
7168 (save-excursion
7169 (c-backward-syntactic-ws)
7170 (and (c-safe (backward-char 2) t)
7171 (looking-at "::")))
7174 ;; Handle a C++ operator or template identifier.
7175 (goto-char id-end)
7176 (c-forward-syntactic-ws)
7177 (cond ((eq (char-before id-end) ?e)
7178 ;; Got "... ::template".
7179 (let ((subres (c-forward-name)))
7180 (when subres
7181 (setq pos (point)
7182 res subres))))
7184 ((looking-at c-identifier-start)
7185 ;; Got a cast operator.
7186 (when (c-forward-type)
7187 (setq pos (point)
7188 res 'operator)
7189 ;; Now we should match a sequence of either
7190 ;; '*', '&' or a name followed by ":: *",
7191 ;; where each can be followed by a sequence
7192 ;; of `c-opt-type-modifier-key'.
7193 (while (cond ((looking-at "[*&]")
7194 (goto-char (match-end 0))
7196 ((looking-at c-identifier-start)
7197 (and (c-forward-name)
7198 (looking-at "::")
7199 (progn
7200 (goto-char (match-end 0))
7201 (c-forward-syntactic-ws)
7202 (eq (char-after) ?*))
7203 (progn
7204 (forward-char)
7205 t))))
7206 (while (progn
7207 (c-forward-syntactic-ws)
7208 (setq pos (point))
7209 (looking-at c-opt-type-modifier-key))
7210 (goto-char (match-end 1))))))
7212 ((looking-at c-overloadable-operators-regexp)
7213 ;; Got some other operator.
7214 (setq c-last-identifier-range
7215 (cons (point) (match-end 0)))
7216 (goto-char (match-end 0))
7217 (c-forward-syntactic-ws)
7218 (setq pos (point)
7219 res 'operator)))
7221 nil)
7223 ;; `id-start' is equal to `id-end' if we've jumped over
7224 ;; an identifier that doesn't end with a symbol token.
7225 ;; That can occur e.g. for Java import directives on the
7226 ;; form "foo.bar.*".
7227 (when (and id-start (/= id-start id-end))
7228 (setq c-last-identifier-range
7229 (cons id-start id-end)))
7230 (goto-char id-end)
7231 (c-forward-syntactic-ws)
7232 (setq pos (point)
7233 res t)))
7235 (progn
7236 (goto-char pos)
7237 (when (or c-opt-identifier-concat-key
7238 c-recognize-<>-arglists)
7240 (cond
7241 ((and c-opt-identifier-concat-key
7242 (looking-at c-opt-identifier-concat-key))
7243 ;; Got a concatenated identifier. This handles the
7244 ;; cases with tricky syntactic whitespace that aren't
7245 ;; covered in `c-identifier-key'.
7246 (goto-char (match-end 0))
7247 (c-forward-syntactic-ws)
7250 ((and c-recognize-<>-arglists
7251 (eq (char-after) ?<))
7252 ;; Maybe an angle bracket arglist.
7253 (when (let (c-last-identifier-range)
7254 (c-forward-<>-arglist nil))
7256 (c-forward-syntactic-ws)
7257 (unless (eq (char-after) ?\()
7258 (setq c-last-identifier-range nil)
7259 (c-add-type start (1+ pos)))
7260 (setq pos (point))
7262 (if (and c-opt-identifier-concat-key
7263 (looking-at c-opt-identifier-concat-key))
7265 ;; Continue if there's an identifier concatenation
7266 ;; operator after the template argument.
7267 (progn
7268 (when (and c-record-type-identifiers id-start)
7269 (c-record-ref-id (cons id-start id-end)))
7270 (forward-char 2)
7271 (c-forward-syntactic-ws)
7274 (when (and c-record-type-identifiers id-start
7275 (not (eq (char-after) ?\()))
7276 (c-record-type-id (cons id-start id-end)))
7277 (setq res 'template)
7278 nil)))
7279 )))))
7281 (goto-char pos)
7282 res))
7284 (defun c-forward-type (&optional brace-block-too)
7285 ;; Move forward over a type spec if at the beginning of one,
7286 ;; stopping at the next following token. The keyword "typedef"
7287 ;; isn't part of a type spec here.
7289 ;; BRACE-BLOCK-TOO, when non-nil, means move over the brace block in
7290 ;; constructs like "struct foo {...} bar ;" or "struct {...} bar;".
7291 ;; The current (2009-03-10) intention is to convert all uses of
7292 ;; `c-forward-type' to call with this parameter set, then to
7293 ;; eliminate it.
7295 ;; Return
7296 ;; o - t if it's a known type that can't be a name or other
7297 ;; expression;
7298 ;; o - 'known if it's an otherwise known type (according to
7299 ;; `*-font-lock-extra-types');
7300 ;; o - 'prefix if it's a known prefix of a type;
7301 ;; o - 'found if it's a type that matches one in `c-found-types';
7302 ;; o - 'maybe if it's an identifier that might be a type;
7303 ;; o - 'decltype if it's a decltype(variable) declaration; - or
7304 ;; o - nil if it can't be a type (the point isn't moved then).
7306 ;; The point is assumed to be at the beginning of a token.
7308 ;; Note that this function doesn't skip past the brace definition
7309 ;; that might be considered part of the type, e.g.
7310 ;; "enum {a, b, c} foo".
7312 ;; This function records identifier ranges on
7313 ;; `c-record-type-identifiers' and `c-record-ref-identifiers' if
7314 ;; `c-record-type-identifiers' is non-nil.
7316 ;; This function might do hidden buffer changes.
7317 (when (and c-recognize-<>-arglists
7318 (looking-at "<"))
7319 (c-forward-<>-arglist t)
7320 (c-forward-syntactic-ws))
7322 (let ((start (point)) pos res name-res id-start id-end id-range)
7324 ;; Skip leading type modifiers. If any are found we know it's a
7325 ;; prefix of a type.
7326 (when c-opt-type-modifier-key ; e.g. "const" "volatile", but NOT "typedef"
7327 (while (looking-at c-opt-type-modifier-key)
7328 (goto-char (match-end 1))
7329 (c-forward-syntactic-ws)
7330 (setq res 'prefix)))
7332 (cond
7333 ((looking-at c-typeof-key) ; e.g. C++'s "decltype".
7334 (goto-char (match-end 1))
7335 (c-forward-syntactic-ws)
7336 (setq res (and (eq (char-after) ?\()
7337 (c-safe (c-forward-sexp))
7338 'decltype))
7339 (if res
7340 (c-forward-syntactic-ws)
7341 (goto-char start)))
7343 ((looking-at c-type-prefix-key) ; e.g. "struct", "class", but NOT
7344 ; "typedef".
7345 (goto-char (match-end 1))
7346 (c-forward-syntactic-ws)
7348 (while (cond
7349 ((looking-at c-decl-hangon-key)
7350 (c-forward-keyword-clause 1))
7351 ((looking-at c-pack-key)
7352 (goto-char (match-end 1))
7353 (c-forward-syntactic-ws))
7354 ((and c-opt-cpp-prefix
7355 (looking-at c-noise-macro-with-parens-name-re))
7356 (c-forward-noise-clause))))
7358 (setq pos (point))
7360 (setq name-res (c-forward-name))
7361 (setq res (not (null name-res)))
7362 (when (eq name-res t)
7363 ;; In many languages the name can be used without the
7364 ;; prefix, so we add it to `c-found-types'.
7365 (c-add-type pos (point))
7366 (when (and c-record-type-identifiers
7367 c-last-identifier-range)
7368 (c-record-type-id c-last-identifier-range)))
7369 (when (and brace-block-too
7370 (memq res '(t nil))
7371 (eq (char-after) ?\{)
7372 (save-excursion
7373 (c-safe
7374 (progn (c-forward-sexp)
7375 (c-forward-syntactic-ws)
7376 (setq pos (point))))))
7377 (goto-char pos)
7378 (setq res t))
7379 (unless res (goto-char start))) ; invalid syntax
7381 ((progn
7382 (setq pos nil)
7383 (if (looking-at c-identifier-start)
7384 (save-excursion
7385 (setq id-start (point)
7386 name-res (c-forward-name))
7387 (when name-res
7388 (setq id-end (point)
7389 id-range c-last-identifier-range))))
7390 (and (cond ((looking-at c-primitive-type-key)
7391 (setq res t))
7392 ((c-with-syntax-table c-identifier-syntax-table
7393 (looking-at c-known-type-key))
7394 (setq res 'known)))
7395 (or (not id-end)
7396 (>= (save-excursion
7397 (save-match-data
7398 (goto-char (match-end 1))
7399 (c-forward-syntactic-ws)
7400 (setq pos (point))))
7401 id-end)
7402 (setq res nil))))
7403 ;; Looking at a primitive or known type identifier. We've
7404 ;; checked for a name first so that we don't go here if the
7405 ;; known type match only is a prefix of another name.
7407 (setq id-end (match-end 1))
7409 (when (and c-record-type-identifiers
7410 (or c-promote-possible-types (eq res t)))
7411 (c-record-type-id (cons (match-beginning 1) (match-end 1))))
7413 (if (and c-opt-type-component-key
7414 (save-match-data
7415 (looking-at c-opt-type-component-key)))
7416 ;; There might be more keywords for the type.
7417 (let (safe-pos)
7418 (c-forward-keyword-clause 1)
7419 (while (progn
7420 (setq safe-pos (point))
7421 (looking-at c-opt-type-component-key))
7422 (when (and c-record-type-identifiers
7423 (looking-at c-primitive-type-key))
7424 (c-record-type-id (cons (match-beginning 1)
7425 (match-end 1))))
7426 (c-forward-keyword-clause 1))
7427 (if (looking-at c-primitive-type-key)
7428 (progn
7429 (when c-record-type-identifiers
7430 (c-record-type-id (cons (match-beginning 1)
7431 (match-end 1))))
7432 (c-forward-keyword-clause 1)
7433 (setq res t))
7434 (goto-char safe-pos)
7435 (setq res 'prefix)))
7436 (unless (save-match-data (c-forward-keyword-clause 1))
7437 (if pos
7438 (goto-char pos)
7439 (goto-char (match-end 1))
7440 (c-forward-syntactic-ws)))))
7442 (name-res
7443 (cond ((eq name-res t)
7444 ;; A normal identifier.
7445 (goto-char id-end)
7446 (if (or res c-promote-possible-types)
7447 (progn
7448 (c-add-type id-start id-end)
7449 (when (and c-record-type-identifiers id-range)
7450 (c-record-type-id id-range))
7451 (unless res
7452 (setq res 'found)))
7453 (setq res (if (c-check-type id-start id-end)
7454 ;; It's an identifier that has been used as
7455 ;; a type somewhere else.
7456 'found
7457 ;; It's an identifier that might be a type.
7458 'maybe))))
7459 ((eq name-res 'template)
7460 ;; A template is sometimes a type.
7461 (goto-char id-end)
7462 (c-forward-syntactic-ws)
7463 (setq res
7464 (if (eq (char-after) ?\()
7465 (if (c-check-type id-start id-end)
7466 ;; It's an identifier that has been used as
7467 ;; a type somewhere else.
7468 'found
7469 ;; It's an identifier that might be a type.
7470 'maybe)
7471 t)))
7473 ;; Otherwise it's an operator identifier, which is not a type.
7474 (goto-char start)
7475 (setq res nil)))))
7477 (when res
7478 ;; Skip trailing type modifiers. If any are found we know it's
7479 ;; a type.
7480 (when c-opt-type-modifier-key
7481 (while (looking-at c-opt-type-modifier-key) ; e.g. "const", "volatile"
7482 (goto-char (match-end 1))
7483 (c-forward-syntactic-ws)
7484 (setq res t)))
7486 ;; Step over any type suffix operator. Do not let the existence
7487 ;; of these alter the classification of the found type, since
7488 ;; these operators typically are allowed in normal expressions
7489 ;; too.
7490 (when c-opt-type-suffix-key ; e.g. "..."
7491 (while (looking-at c-opt-type-suffix-key)
7492 (goto-char (match-end 1))
7493 (c-forward-syntactic-ws)))
7495 ;; Skip any "WS" identifiers (e.g. "final" or "override" in C++)
7496 (while (looking-at c-type-decl-suffix-ws-ids-key)
7497 (goto-char (match-end 1))
7498 (c-forward-syntactic-ws)
7499 (setq res t))
7501 (when c-opt-type-concat-key ; Only/mainly for pike.
7502 ;; Look for a trailing operator that concatenates the type
7503 ;; with a following one, and if so step past that one through
7504 ;; a recursive call. Note that we don't record concatenated
7505 ;; types in `c-found-types' - it's the component types that
7506 ;; are recorded when appropriate.
7507 (setq pos (point))
7508 (let* ((c-promote-possible-types (or (memq res '(t known))
7509 c-promote-possible-types))
7510 ;; If we can't promote then set `c-record-found-types' so that
7511 ;; we can merge in the types from the second part afterwards if
7512 ;; it turns out to be a known type there.
7513 (c-record-found-types (and c-record-type-identifiers
7514 (not c-promote-possible-types)))
7515 subres)
7516 (if (and (looking-at c-opt-type-concat-key)
7518 (progn
7519 (goto-char (match-end 1))
7520 (c-forward-syntactic-ws)
7521 (setq subres (c-forward-type))))
7523 (progn
7524 ;; If either operand certainly is a type then both are, but we
7525 ;; don't let the existence of the operator itself promote two
7526 ;; uncertain types to a certain one.
7527 (cond ((eq res t))
7528 ((eq subres t)
7529 (unless (eq name-res 'template)
7530 (c-add-type id-start id-end))
7531 (when (and c-record-type-identifiers id-range)
7532 (c-record-type-id id-range))
7533 (setq res t))
7534 ((eq res 'known))
7535 ((eq subres 'known)
7536 (setq res 'known))
7537 ((eq res 'found))
7538 ((eq subres 'found)
7539 (setq res 'found))
7541 (setq res 'maybe)))
7543 (when (and (eq res t)
7544 (consp c-record-found-types))
7545 ;; Merge in the ranges of any types found by the second
7546 ;; `c-forward-type'.
7547 (setq c-record-type-identifiers
7548 ;; `nconc' doesn't mind that the tail of
7549 ;; `c-record-found-types' is t.
7550 (nconc c-record-found-types
7551 c-record-type-identifiers))))
7553 (goto-char pos))))
7555 (when (and c-record-found-types (memq res '(known found)) id-range)
7556 (setq c-record-found-types
7557 (cons id-range c-record-found-types))))
7559 ;;(message "c-forward-type %s -> %s: %s" start (point) res)
7561 res))
7563 (defun c-forward-annotation ()
7564 ;; Used for Java code only at the moment. Assumes point is on the @, moves
7565 ;; forward an annotation and returns t. Leaves point unmoved and returns
7566 ;; nil if there is no annotation at point.
7567 (let ((pos (point)))
7569 (and (looking-at "@")
7570 (not (looking-at c-keywords-regexp))
7571 (progn (forward-char) t)
7572 (looking-at c-symbol-key)
7573 (progn (goto-char (match-end 0))
7574 (c-forward-syntactic-ws)
7576 (if (looking-at "(")
7577 (c-go-list-forward)
7579 (progn (goto-char pos) nil))))
7581 (defmacro c-pull-open-brace (ps)
7582 ;; Pull the next open brace from PS (which has the form of paren-state),
7583 ;; skipping over any brace pairs. Returns NIL when PS is exhausted.
7584 `(progn
7585 (while (consp (car ,ps))
7586 (setq ,ps (cdr ,ps)))
7587 (prog1 (car ,ps)
7588 (setq ,ps (cdr ,ps)))))
7590 (defun c-back-over-compound-identifier ()
7591 ;; Point is putatively just after a "compound identifier", i.e. something
7592 ;; looking (in C++) like this "FQN::of::base::Class". Move to the start of
7593 ;; this construct and return t. If the parsing fails, return nil, leaving
7594 ;; point unchanged.
7595 (let ((here (point))
7596 end)
7597 (if (not (c-on-identifier))
7599 (c-simple-skip-symbol-backward)
7600 (while
7601 (progn
7602 (setq end (point))
7603 (c-backward-syntactic-ws)
7604 (c-backward-token-2)
7605 (and
7606 c-opt-identifier-concat-key
7607 (looking-at c-opt-identifier-concat-key)
7608 (progn
7609 (c-backward-syntactic-ws)
7610 (c-simple-skip-symbol-backward))))
7611 (setq end (point)))
7612 (goto-char end)
7613 t)))
7615 (defun c-back-over-member-initializer-braces ()
7616 ;; Point is just after a closing brace/parenthesis. Try to parse this as a
7617 ;; C++ member initializer list, going back to just after the introducing ":"
7618 ;; and returning t. Otherwise return nil, leaving point unchanged.
7619 (let ((here (point)) res)
7620 (setq res
7621 (catch 'done
7622 (when (not (c-go-list-backward))
7623 (throw 'done nil))
7624 (c-backward-syntactic-ws)
7625 (when (not (c-back-over-compound-identifier))
7626 (throw 'done nil))
7627 (c-backward-syntactic-ws)
7629 (while (eq (char-before) ?,)
7630 (backward-char)
7631 (c-backward-syntactic-ws)
7632 (when (not (memq (char-before) '(?\) ?})))
7633 (throw 'done nil))
7634 (when (not (c-go-list-backward))
7635 (throw 'done nil))
7636 (c-backward-syntactic-ws)
7637 (when (not (c-back-over-compound-identifier))
7638 (throw 'done nil))
7639 (c-backward-syntactic-ws))
7641 (eq (char-before) ?:)))
7642 (or res (goto-char here))
7643 res))
7645 (defmacro c-back-over-list-of-member-inits ()
7646 ;; Go back over a list of elements, each looking like:
7647 ;; <symbol> (<expression>) ,
7648 ;; or <symbol> {<expression>} , (with possibly a <....> expressions
7649 ;; following the <symbol>).
7650 ;; when we are putatively immediately after a comma. Stop when we don't see
7651 ;; a comma. If either of <symbol> or bracketed <expression> is missing,
7652 ;; throw nil to 'level. If the terminating } or ) is unmatched, throw nil
7653 ;; to 'done. This is not a general purpose macro!
7654 `(while (eq (char-before) ?,)
7655 (backward-char)
7656 (c-backward-syntactic-ws)
7657 (when (not (memq (char-before) '(?\) ?})))
7658 (throw 'level nil))
7659 (when (not (c-go-list-backward))
7660 (throw 'done nil))
7661 (c-backward-syntactic-ws)
7662 (while (eq (char-before) ?>)
7663 (when (not (c-backward-<>-arglist nil))
7664 (throw 'done nil))
7665 (c-backward-syntactic-ws))
7666 (when (not (c-back-over-compound-identifier))
7667 (throw 'level nil))
7668 (c-backward-syntactic-ws)))
7670 (defun c-back-over-member-initializers ()
7671 ;; Test whether we are in a C++ member initializer list, and if so, go back
7672 ;; to the introducing ":", returning the position of the opening paren of
7673 ;; the function's arglist. Otherwise return nil, leaving point unchanged.
7674 (let ((here (point))
7675 (paren-state (c-parse-state))
7676 pos level-plausible at-top-level res)
7677 ;; Assume tentatively that we're at the top level. Try to go back to the
7678 ;; colon we seek.
7679 (setq res
7680 (catch 'done
7681 (setq level-plausible
7682 (catch 'level
7683 (c-backward-syntactic-ws)
7684 (when (memq (char-before) '(?\) ?}))
7685 (when (not (c-go-list-backward))
7686 (throw 'done nil))
7687 (c-backward-syntactic-ws))
7688 (when (c-back-over-compound-identifier)
7689 (c-backward-syntactic-ws))
7690 (c-back-over-list-of-member-inits)
7691 (and (eq (char-before) ?:)
7692 (save-excursion
7693 (c-backward-token-2)
7694 (not (looking-at c-:$-multichar-token-regexp)))
7695 (c-just-after-func-arglist-p))))
7697 (while (and (not (and level-plausible
7698 (setq at-top-level (c-at-toplevel-p))))
7699 (setq pos (c-pull-open-brace paren-state))) ; might be a paren.
7700 (setq level-plausible
7701 (catch 'level
7702 (goto-char pos)
7703 (c-backward-syntactic-ws)
7704 (when (not (c-back-over-compound-identifier))
7705 (throw 'level nil))
7706 (c-backward-syntactic-ws)
7707 (c-back-over-list-of-member-inits)
7708 (and (eq (char-before) ?:)
7709 (save-excursion
7710 (c-backward-token-2)
7711 (not (looking-at c-:$-multichar-token-regexp)))
7712 (c-just-after-func-arglist-p)))))
7714 (and at-top-level level-plausible)))
7715 (or res (goto-char here))
7716 res))
7719 ;; Handling of large scale constructs like statements and declarations.
7721 ;; Macro used inside `c-forward-decl-or-cast-1'. It ought to be a
7722 ;; defsubst or perhaps even a defun, but it contains lots of free
7723 ;; variables that refer to things inside `c-forward-decl-or-cast-1'.
7724 (defmacro c-fdoc-shift-type-backward (&optional short)
7725 ;; `c-forward-decl-or-cast-1' can consume an arbitrary length list
7726 ;; of types when parsing a declaration, which means that it
7727 ;; sometimes consumes the identifier in the declaration as a type.
7728 ;; This is used to "backtrack" and make the last type be treated as
7729 ;; an identifier instead.
7730 `(progn
7731 ,(unless short
7732 ;; These identifiers are bound only in the inner let.
7733 '(setq identifier-type at-type
7734 identifier-start type-start
7735 got-parens nil
7736 got-identifier t
7737 got-suffix t
7738 got-suffix-after-parens id-start
7739 paren-depth 0))
7741 (if (setq at-type (if (eq backup-at-type 'prefix)
7743 backup-at-type))
7744 (setq type-start backup-type-start
7745 id-start backup-id-start)
7746 (setq type-start start-pos
7747 id-start start-pos))
7749 ;; When these flags already are set we've found specifiers that
7750 ;; unconditionally signal these attributes - backtracking doesn't
7751 ;; change that. So keep them set in that case.
7752 (or at-type-decl
7753 (setq at-type-decl backup-at-type-decl))
7754 (or maybe-typeless
7755 (setq maybe-typeless backup-maybe-typeless))
7757 ,(unless short
7758 ;; This identifier is bound only in the inner let.
7759 '(setq start id-start))))
7761 (defun c-forward-declarator (&optional limit accept-anon)
7762 ;; Assuming point is at the start of a declarator, move forward over it,
7763 ;; leaving point at the next token after it (e.g. a ) or a ; or a ,).
7765 ;; Return a list (ID-START ID-END BRACKETS-AFTER-ID GOT-INIT DECORATED),
7766 ;; where ID-START and ID-END are the bounds of the declarator's identifier,
7767 ;; and BRACKETS-AFTER-ID is non-nil if a [...] pair is present after the id.
7768 ;; GOT-INIT is non-nil when the declarator is followed by "=" or "(",
7769 ;; DECORATED is non-nil when the identifier is embellished by an operator,
7770 ;; like "*x", or "(*x)".
7772 ;; If ACCEPT-ANON is non-nil, move forward over any "anonymous declarator",
7773 ;; i.e. something like the (*) in int (*), such as might be found in a
7774 ;; declaration. In such a case ID-START and ID-END in the return value are
7775 ;; both set to nil. A "null" "anonymous declarator" gives a non-nil result.
7777 ;; If no declarator is found, leave point unmoved and return nil. LIMIT is
7778 ;; an optional limit for forward searching.
7780 ;; Note that the global variable `c-last-identifier-range' is written to, so
7781 ;; the caller should bind it if necessary.
7783 ;; Inside the following "condition form", we move forward over the
7784 ;; declarator's identifier up as far as any opening bracket (for array
7785 ;; size) or paren (for parameters of function-type) or brace (for
7786 ;; array/struct initialization) or "=" or terminating delimiter
7787 ;; (e.g. "," or ";" or "}").
7788 (let ((here (point))
7789 id-start id-end brackets-after-id paren-depth decorated)
7790 (or limit (setq limit (point-max)))
7791 (if (and
7792 (< (point) limit)
7794 ;; The following form moves forward over the declarator's
7795 ;; identifier (and what precedes it), returning t. If there
7796 ;; wasn't one, it returns nil.
7797 (let (got-identifier)
7798 (setq paren-depth 0)
7799 ;; Skip over type decl prefix operators, one for each iteration
7800 ;; of the while. These are, e.g. "*" in "int *foo" or "(" and
7801 ;; "*" in "int (*foo) (void)" (Note similar code in
7802 ;; `c-forward-decl-or-cast-1'.)
7803 (while
7804 (cond
7805 ((looking-at c-decl-hangon-key)
7806 (c-forward-keyword-clause 1))
7807 ((and c-opt-cpp-prefix
7808 (looking-at c-noise-macro-with-parens-name-re))
7809 (c-forward-noise-clause))
7810 ((and (looking-at c-type-decl-prefix-key)
7811 (if (and (c-major-mode-is 'c++-mode)
7812 (match-beginning 3))
7813 ;; If the third submatch matches in C++ then
7814 ;; we're looking at an identifier that's a
7815 ;; prefix only if it specifies a member pointer.
7816 (progn
7817 (setq id-start (point))
7818 (c-forward-name)
7819 (if (looking-at "\\(::\\)")
7820 ;; We only check for a trailing "::" and
7821 ;; let the "*" that should follow be
7822 ;; matched in the next round.
7824 ;; It turned out to be the real identifier,
7825 ;; so flag that and stop.
7826 (setq got-identifier t)
7827 nil))
7829 (if (looking-at c-type-decl-operator-prefix-key)
7830 (setq decorated t))
7831 (if (eq (char-after) ?\()
7832 (progn
7833 (setq paren-depth (1+ paren-depth))
7834 (forward-char))
7835 (goto-char (match-end 1)))
7836 (c-forward-syntactic-ws)
7837 t)))
7839 ;; If we haven't passed the identifier already, do it now.
7840 (unless got-identifier
7841 (setq id-start (point)))
7842 (cond
7843 ((or got-identifier
7844 (c-forward-name))
7845 (save-excursion
7846 (c-backward-syntactic-ws)
7847 (setq id-end (point))))
7848 (accept-anon
7849 (setq id-start nil id-end nil)
7851 (t (/= (point) here))))
7853 ;; Skip out of the parens surrounding the identifier. If closing
7854 ;; parens are missing, this form returns nil.
7855 (or (= paren-depth 0)
7856 (c-safe (goto-char (scan-lists (point) 1 paren-depth))))
7858 (<= (point) limit)
7860 ;; Skip over any trailing bit, such as "__attribute__".
7861 (progn
7862 (while (cond
7863 ((looking-at c-decl-hangon-key)
7864 (c-forward-keyword-clause 1))
7865 ((and c-opt-cpp-prefix
7866 (looking-at c-noise-macro-with-parens-name-re))
7867 (c-forward-noise-clause))))
7868 (<= (point) limit))
7870 ;; Search syntactically to the end of the declarator (";",
7871 ;; ",", a closing paren, eob etc) or to the beginning of an
7872 ;; initializer or function prototype ("=" or "\\s\(").
7873 ;; Note that square brackets are now not also treated as
7874 ;; initializers, since this broke when there were also
7875 ;; initializing brace lists.
7876 (let (found)
7877 (while
7878 (and (setq found (c-syntactic-re-search-forward
7879 "[;,]\\|\\s)\\|\\'\\|\\(=\\|\\s(\\)" limit t t))
7880 (eq (char-before) ?\[)
7881 (c-go-up-list-forward))
7882 (setq brackets-after-id t))
7883 (backward-char)
7884 found))
7885 (list id-start id-end brackets-after-id (match-beginning 1) decorated)
7887 (goto-char here)
7888 nil)))
7890 (defun c-forward-decl-or-cast-1 (preceding-token-end context last-cast-end)
7891 ;; Move forward over a declaration or a cast if at the start of one.
7892 ;; The point is assumed to be at the start of some token. Nil is
7893 ;; returned if no declaration or cast is recognized, and the point
7894 ;; is clobbered in that case.
7896 ;; If a declaration is parsed:
7898 ;; The point is left at the first token after the first complete
7899 ;; declarator, if there is one. The return value is a list of 4 elements,
7900 ;; where the first is the position of the first token in the declarator.
7901 ;; (See below for the other three.)
7902 ;; Some examples:
7904 ;; void foo (int a, char *b) stuff ...
7905 ;; car ^ ^ point
7906 ;; float (*a)[], b;
7907 ;; car ^ ^ point
7908 ;; unsigned int a = c_style_initializer, b;
7909 ;; car ^ ^ point
7910 ;; unsigned int a (cplusplus_style_initializer), b;
7911 ;; car ^ ^ point (might change)
7912 ;; class Foo : public Bar {}
7913 ;; car ^ ^ point
7914 ;; class PikeClass (int a, string b) stuff ...
7915 ;; car ^ ^ point
7916 ;; enum bool;
7917 ;; car ^ ^ point
7918 ;; enum bool flag;
7919 ;; car ^ ^ point
7920 ;; void cplusplus_function (int x) throw (Bad);
7921 ;; car ^ ^ point
7922 ;; Foo::Foo (int b) : Base (b) {}
7923 ;; car ^ ^ point
7925 ;; auto foo = 5;
7926 ;; car ^ ^ point
7927 ;; auto cplusplus_11 (int a, char *b) -> decltype (bar):
7928 ;; car ^ ^ point
7932 ;; The second element of the return value is non-nil when a
7933 ;; `c-typedef-decl-kwds' specifier is found in the declaration.
7934 ;; Specifically it is a dotted pair (A . B) where B is t when a
7935 ;; `c-typedef-kwds' ("typedef") is present, and A is t when some
7936 ;; other `c-typedef-decl-kwds' (e.g. class, struct, enum)
7937 ;; specifier is present. I.e., (some of) the declared
7938 ;; identifier(s) are types.
7940 ;; The third element of the return value is non-nil when the declaration
7941 ;; parsed might be an expression. The fourth element is the position of
7942 ;; the start of the type identifier.
7944 ;; If a cast is parsed:
7946 ;; The point is left at the first token after the closing paren of
7947 ;; the cast. The return value is `cast'. Note that the start
7948 ;; position must be at the first token inside the cast parenthesis
7949 ;; to recognize it.
7951 ;; PRECEDING-TOKEN-END is the first position after the preceding
7952 ;; token, i.e. on the other side of the syntactic ws from the point.
7953 ;; Use a value less than or equal to (point-min) if the point is at
7954 ;; the first token in (the visible part of) the buffer.
7956 ;; CONTEXT is a symbol that describes the context at the point:
7957 ;; 'decl In a comma-separated declaration context (typically
7958 ;; inside a function declaration arglist).
7959 ;; '<> In an angle bracket arglist.
7960 ;; 'arglist Some other type of arglist.
7961 ;; 'top Some other context and point is at the top-level (either
7962 ;; outside any braces or directly inside a class or namespace,
7963 ;; etc.)
7964 ;; nil Some other context or unknown context. Includes
7965 ;; within the parens of an if, for, ... construct.
7966 ;; 'not-decl This value is never supplied to this function. It
7967 ;; would mean we're definitely not in a declaration.
7969 ;; LAST-CAST-END is the first token after the closing paren of a
7970 ;; preceding cast, or nil if none is known. If
7971 ;; `c-forward-decl-or-cast-1' is used in succession, it should be
7972 ;; the position after the closest preceding call where a cast was
7973 ;; matched. In that case it's used to discover chains of casts like
7974 ;; "(a) (b) c".
7976 ;; This function records identifier ranges on
7977 ;; `c-record-type-identifiers' and `c-record-ref-identifiers' if
7978 ;; `c-record-type-identifiers' is non-nil.
7980 ;; This function might do hidden buffer changes.
7982 (let (;; `start-pos' is used below to point to the start of the
7983 ;; first type, i.e. after any leading specifiers. It might
7984 ;; also point at the beginning of the preceding syntactic
7985 ;; whitespace.
7986 (start-pos (point))
7987 ;; Set to the result of `c-forward-type'.
7988 at-type
7989 ;; The position of the first token in what we currently
7990 ;; believe is the type in the declaration or cast, after any
7991 ;; specifiers and their associated clauses.
7992 type-start
7993 ;; The position of the first token in what we currently
7994 ;; believe is the declarator for the first identifier. Set
7995 ;; when the type is found, and moved forward over any
7996 ;; `c-decl-hangon-kwds' and their associated clauses that
7997 ;; occurs after the type.
7998 id-start
7999 ;; These store `at-type', `type-start' and `id-start' of the
8000 ;; identifier before the one in those variables. The previous
8001 ;; identifier might turn out to be the real type in a
8002 ;; declaration if the last one has to be the declarator in it.
8003 ;; If `backup-at-type' is nil then the other variables have
8004 ;; undefined values.
8005 backup-at-type backup-type-start backup-id-start
8006 ;; This stores `kwd-sym' of the symbol before the current one.
8007 ;; This is needed to distinguish the C++11 version of "auto" from
8008 ;; the pre C++11 meaning.
8009 backup-kwd-sym
8010 ;; Set if we've found a specifier (apart from "typedef") that makes
8011 ;; the defined identifier(s) types.
8012 at-type-decl
8013 ;; Set if we've a "typedef" keyword.
8014 at-typedef
8015 ;; Set if we've found a specifier that can start a declaration
8016 ;; where there's no type.
8017 maybe-typeless
8018 ;; Save the value of kwd-sym between loops of the "Check for a
8019 ;; type" loop. Needed to distinguish a C++11 "auto" from a pre
8020 ;; C++11 one.
8021 prev-kwd-sym
8022 ;; If a specifier is found that also can be a type prefix,
8023 ;; these flags are set instead of those above. If we need to
8024 ;; back up an identifier, they are copied to the real flag
8025 ;; variables. Thus they only take effect if we fail to
8026 ;; interpret it as a type.
8027 backup-at-type-decl backup-maybe-typeless
8028 ;; Whether we've found a declaration or a cast. We might know
8029 ;; this before we've found the type in it. It's 'ids if we've
8030 ;; found two consecutive identifiers (usually a sure sign, but
8031 ;; we should allow that in labels too), and t if we've found a
8032 ;; specifier keyword (a 100% sure sign).
8033 at-decl-or-cast
8034 ;; Set when we need to back up to parse this as a declaration
8035 ;; but not as a cast.
8036 backup-if-not-cast
8037 ;; For casts, the return position.
8038 cast-end
8039 ;; Have we got a new-style C++11 "auto"?
8040 new-style-auto
8041 ;; Set when the symbol before `preceding-token-end' is known to
8042 ;; terminate the previous construct, or when we're at point-min.
8043 at-decl-start
8044 ;; Save `c-record-type-identifiers' and
8045 ;; `c-record-ref-identifiers' since ranges are recorded
8046 ;; speculatively and should be thrown away if it turns out
8047 ;; that it isn't a declaration or cast.
8048 (save-rec-type-ids c-record-type-identifiers)
8049 (save-rec-ref-ids c-record-ref-identifiers)
8050 ;; Set when we parse a declaration which might also be an expression,
8051 ;; such as "a *b". See CASE 16 and CASE 17.
8052 maybe-expression)
8054 (save-excursion
8055 (goto-char preceding-token-end)
8056 (setq at-decl-start
8057 (or (bobp)
8058 (let ((tok-end (point)))
8059 (c-backward-token-2)
8060 (member (buffer-substring-no-properties (point) tok-end)
8061 c-pre-start-tokens)))))
8063 (while (c-forward-annotation)
8064 (c-forward-syntactic-ws))
8066 ;; Check for a type. Unknown symbols are treated as possible
8067 ;; types, but they could also be specifiers disguised through
8068 ;; macros like __INLINE__, so we recognize both types and known
8069 ;; specifiers after them too.
8070 (while
8071 (let* ((start (point)) kwd-sym kwd-clause-end found-type noise-start)
8073 (cond
8074 ;; Look for a specifier keyword clause.
8075 ((or (looking-at c-prefix-spec-kwds-re)
8076 (and (c-major-mode-is 'java-mode)
8077 (looking-at "@[A-Za-z0-9]+")))
8078 (save-match-data
8079 (if (looking-at c-typedef-key)
8080 (setq at-typedef t)))
8081 (setq kwd-sym (c-keyword-sym (match-string 1)))
8082 (save-excursion
8083 (c-forward-keyword-clause 1)
8084 (setq kwd-clause-end (point))))
8085 ((and c-opt-cpp-prefix
8086 (looking-at c-noise-macro-with-parens-name-re))
8087 (setq noise-start (point))
8088 (c-forward-noise-clause)
8089 (setq kwd-clause-end (point))))
8091 (when (setq found-type (c-forward-type t)) ; brace-block-too
8092 ;; Found a known or possible type or a prefix of a known type.
8093 (when (and (c-major-mode-is 'c++-mode) ; C++11 style "auto"?
8094 (eq prev-kwd-sym (c-keyword-sym "auto"))
8095 (looking-at "[=(]")) ; FIXME!!! proper regexp.
8096 (setq new-style-auto t)
8097 (setq found-type nil)
8098 (goto-char start)) ; position of foo in "auto foo"
8100 (when at-type
8101 ;; Got two identifiers with nothing but whitespace
8102 ;; between them. That can only happen in declarations.
8103 (setq at-decl-or-cast 'ids)
8105 (when (eq at-type 'found)
8106 ;; If the previous identifier is a found type we
8107 ;; record it as a real one; it might be some sort of
8108 ;; alias for a prefix like "unsigned".
8109 (save-excursion
8110 (goto-char type-start)
8111 (let ((c-promote-possible-types t))
8112 (c-forward-type)))))
8114 (setq backup-at-type at-type
8115 backup-type-start type-start
8116 backup-id-start id-start
8117 backup-kwd-sym kwd-sym
8118 at-type found-type
8119 type-start start
8120 id-start (point)
8121 ;; The previous ambiguous specifier/type turned out
8122 ;; to be a type since we've parsed another one after
8123 ;; it, so clear these backup flags.
8124 backup-at-type-decl nil
8125 backup-maybe-typeless nil))
8127 (if (or kwd-sym noise-start)
8128 (progn
8129 ;; Handle known specifier keywords and
8130 ;; `c-decl-hangon-kwds' which can occur after known
8131 ;; types.
8133 (if (or (c-keyword-member kwd-sym 'c-decl-hangon-kwds)
8134 noise-start)
8135 ;; It's a hang-on keyword or noise clause that can occur
8136 ;; anywhere.
8137 (progn
8138 (if at-type
8139 ;; Move the identifier start position if
8140 ;; we've passed a type.
8141 (setq id-start kwd-clause-end)
8142 ;; Otherwise treat this as a specifier and
8143 ;; move the fallback position.
8144 (setq start-pos kwd-clause-end))
8145 (goto-char kwd-clause-end))
8147 ;; It's an ordinary specifier so we know that
8148 ;; anything before this can't be the type.
8149 (setq backup-at-type nil
8150 start-pos kwd-clause-end)
8152 (if found-type
8153 ;; It's ambiguous whether this keyword is a
8154 ;; specifier or a type prefix, so set the backup
8155 ;; flags. (It's assumed that `c-forward-type'
8156 ;; moved further than `c-forward-keyword-clause'.)
8157 (progn
8158 (when (c-keyword-member kwd-sym 'c-typedef-decl-kwds)
8159 (setq backup-at-type-decl t))
8160 (when (c-keyword-member kwd-sym 'c-typeless-decl-kwds)
8161 (setq backup-maybe-typeless t)))
8163 (when (c-keyword-member kwd-sym 'c-typedef-decl-kwds)
8164 ;; This test only happens after we've scanned a type.
8165 ;; So, with valid syntax, kwd-sym can't be 'typedef.
8166 (setq at-type-decl t))
8167 (when (c-keyword-member kwd-sym 'c-typeless-decl-kwds)
8168 (setq maybe-typeless t))
8170 ;; Haven't matched a type so it's an unambiguous
8171 ;; specifier keyword and we know we're in a
8172 ;; declaration.
8173 (setq at-decl-or-cast t)
8174 (setq prev-kwd-sym kwd-sym)
8176 (goto-char kwd-clause-end))))
8178 ;; If the type isn't known we continue so that we'll jump
8179 ;; over all specifiers and type identifiers. The reason
8180 ;; to do this for a known type prefix is to make things
8181 ;; like "unsigned INT16" work.
8182 (and found-type (not (eq found-type t))))))
8184 (cond
8185 ((eq at-type t)
8186 ;; If a known type was found, we still need to skip over any
8187 ;; hangon keyword clauses after it. Otherwise it has already
8188 ;; been done in the loop above.
8189 (while
8190 (cond ((looking-at c-decl-hangon-key)
8191 (c-forward-keyword-clause 1))
8192 ((and c-opt-cpp-prefix
8193 (looking-at c-noise-macro-with-parens-name-re))
8194 (c-forward-noise-clause))))
8195 (setq id-start (point)))
8197 ((eq at-type 'prefix)
8198 ;; A prefix type is itself a primitive type when it's not
8199 ;; followed by another type.
8200 (setq at-type t))
8202 ((not at-type)
8203 ;; Got no type but set things up to continue anyway to handle
8204 ;; the various cases when a declaration doesn't start with a
8205 ;; type.
8206 (setq id-start start-pos))
8208 ((and (eq at-type 'maybe)
8209 (c-major-mode-is 'c++-mode))
8210 ;; If it's C++ then check if the last "type" ends on the form
8211 ;; "foo::foo" or "foo::~foo", i.e. if it's the name of a
8212 ;; (con|de)structor.
8213 (save-excursion
8214 (let (name end-2 end-1)
8215 (goto-char id-start)
8216 (c-backward-syntactic-ws)
8217 (setq end-2 (point))
8218 (when (and
8219 (c-simple-skip-symbol-backward)
8220 (progn
8221 (setq name
8222 (buffer-substring-no-properties (point) end-2))
8223 ;; Cheating in the handling of syntactic ws below.
8224 (< (skip-chars-backward ":~ \t\n\r\v\f") 0))
8225 (progn
8226 (setq end-1 (point))
8227 (c-simple-skip-symbol-backward))
8228 (>= (point) type-start)
8229 (equal (buffer-substring-no-properties (point) end-1)
8230 name))
8231 ;; It is a (con|de)structor name. In that case the
8232 ;; declaration is typeless so zap out any preceding
8233 ;; identifier(s) that we might have taken as types.
8234 (goto-char type-start)
8235 (setq at-type nil
8236 backup-at-type nil
8237 id-start type-start))))))
8239 ;; Check for and step over a type decl expression after the thing
8240 ;; that is or might be a type. This can't be skipped since we
8241 ;; need the correct end position of the declarator for
8242 ;; `max-type-decl-end-*'.
8243 (let ((start (point)) (paren-depth 0) pos
8244 ;; True if there's a non-open-paren match of
8245 ;; `c-type-decl-prefix-key'.
8246 got-prefix
8247 ;; True if the declarator is surrounded by a parenthesis pair.
8248 got-parens
8249 ;; True if there is an identifier in the declarator.
8250 got-identifier
8251 ;; True if there's a non-close-paren match of
8252 ;; `c-type-decl-suffix-key'.
8253 got-suffix
8254 ;; True if there's a prefix match outside the outermost
8255 ;; paren pair that surrounds the declarator.
8256 got-prefix-before-parens
8257 ;; True if there's a suffix match outside the outermost
8258 ;; paren pair that surrounds the declarator. The value is
8259 ;; the position of the first suffix match.
8260 got-suffix-after-parens
8261 ;; True if we've parsed the type decl to a token that is
8262 ;; known to end declarations in this context.
8263 at-decl-end
8264 ;; The earlier values of `at-type' and `type-start' if we've
8265 ;; shifted the type backwards.
8266 identifier-type identifier-start
8267 ;; If `c-parse-and-markup-<>-arglists' is set we need to
8268 ;; turn it off during the name skipping below to avoid
8269 ;; getting `c-type' properties that might be bogus. That
8270 ;; can happen since we don't know if
8271 ;; `c-restricted-<>-arglists' will be correct inside the
8272 ;; arglist paren that gets entered.
8273 c-parse-and-markup-<>-arglists
8274 ;; Start of the identifier for which `got-identifier' was set.
8275 name-start
8276 ;; Position after (innermost) open parenthesis encountered in the
8277 ;; prefix operators.
8278 after-paren-pos)
8280 (goto-char id-start)
8282 ;; Skip over type decl prefix operators. (Note similar code in
8283 ;; `c-forward-declarator'.)
8284 (if (and c-recognize-typeless-decls
8285 (equal c-type-decl-prefix-key "\\<\\>"))
8286 (when (eq (char-after) ?\()
8287 (progn
8288 (setq paren-depth (1+ paren-depth))
8289 (forward-char)
8290 (setq after-paren-pos (point))))
8291 (while (and (looking-at c-type-decl-prefix-key)
8292 (if (and (c-major-mode-is 'c++-mode)
8293 (match-beginning 3))
8294 ;; If the third submatch matches in C++ then
8295 ;; we're looking at an identifier that's a
8296 ;; prefix only if it specifies a member pointer.
8297 (when (progn (setq pos (point))
8298 (setq got-identifier (c-forward-name)))
8299 (setq name-start pos)
8300 (if (looking-at "\\(::\\)")
8301 ;; We only check for a trailing "::" and
8302 ;; let the "*" that should follow be
8303 ;; matched in the next round.
8304 (progn (setq got-identifier nil) t)
8305 ;; It turned out to be the real identifier,
8306 ;; so stop.
8307 nil))
8310 (if (eq (char-after) ?\()
8311 (progn
8312 (setq paren-depth (1+ paren-depth))
8313 (forward-char)
8314 (setq after-paren-pos (point)))
8315 (unless got-prefix-before-parens
8316 (setq got-prefix-before-parens (= paren-depth 0)))
8317 (setq got-prefix t)
8318 (goto-char (match-end 1)))
8319 (c-forward-syntactic-ws)))
8321 (setq got-parens (> paren-depth 0))
8323 ;; Try to skip over an identifier.
8324 (or got-identifier
8325 (and (looking-at c-identifier-start)
8326 (setq pos (point))
8327 (setq got-identifier (c-forward-name))
8328 (setq name-start pos)))
8330 ;; Skip over type decl suffix operators and trailing noise macros.
8331 (while
8332 (cond
8333 ((and c-opt-cpp-prefix
8334 (looking-at c-noise-macro-with-parens-name-re))
8335 (c-forward-noise-clause))
8337 ((looking-at c-type-decl-suffix-key)
8338 (if (eq (char-after) ?\))
8339 (when (> paren-depth 0)
8340 (setq paren-depth (1- paren-depth))
8341 (forward-char)
8343 (when (if (save-match-data (looking-at "\\s("))
8344 (c-safe (c-forward-sexp 1) t)
8345 (goto-char (match-end 1))
8347 (when (and (not got-suffix-after-parens)
8348 (= paren-depth 0))
8349 (setq got-suffix-after-parens (match-beginning 0)))
8350 (setq got-suffix t))))
8353 ;; No suffix matched. We might have matched the
8354 ;; identifier as a type and the open paren of a
8355 ;; function arglist as a type decl prefix. In that
8356 ;; case we should "backtrack": Reinterpret the last
8357 ;; type as the identifier, move out of the arglist and
8358 ;; continue searching for suffix operators.
8360 ;; Do this even if there's no preceding type, to cope
8361 ;; with old style function declarations in K&R C,
8362 ;; (con|de)structors in C++ and `c-typeless-decl-kwds'
8363 ;; style declarations. That isn't applicable in an
8364 ;; arglist context, though.
8365 (when (and (= paren-depth 1)
8366 (not got-prefix-before-parens)
8367 (not (eq at-type t))
8368 (or backup-at-type
8369 maybe-typeless
8370 backup-maybe-typeless
8371 (when c-recognize-typeless-decls
8372 (and (memq context '(nil top))
8373 ;; Deal with C++11's "copy-initialization"
8374 ;; where we have <type>(<constant>), by
8375 ;; contrasting with a typeless
8376 ;; <name>(<type><parameter>, ...).
8377 (save-excursion
8378 (goto-char after-paren-pos)
8379 (c-forward-syntactic-ws)
8380 (c-forward-type)))))
8381 (setq pos (c-up-list-forward (point)))
8382 (eq (char-before pos) ?\)))
8383 (c-fdoc-shift-type-backward)
8384 (goto-char pos)
8385 t)))
8387 (c-forward-syntactic-ws))
8389 (when (or (and new-style-auto
8390 (looking-at c-auto-ops-re))
8391 (and (or maybe-typeless backup-maybe-typeless)
8392 (not got-identifier)
8393 (not got-prefix)
8394 at-type))
8395 ;; Have found no identifier but `c-typeless-decl-kwds' has
8396 ;; matched so we know we're inside a declaration. The
8397 ;; preceding type must be the identifier instead.
8398 (c-fdoc-shift-type-backward))
8400 ;; Prepare the "-> type;" for fontification later on.
8401 (when (and new-style-auto
8402 (looking-at c-haskell-op-re))
8403 (save-excursion
8404 (goto-char (match-end 0))
8405 (c-forward-syntactic-ws)
8406 (setq type-start (point))
8407 (setq at-type (c-forward-type))))
8409 ;; Move forward over any "WS" ids (like "final" or "override" in C++)
8410 (while (looking-at c-type-decl-suffix-ws-ids-key)
8411 (goto-char (match-end 1))
8412 (c-forward-syntactic-ws))
8414 (setq
8415 at-decl-or-cast
8416 (catch 'at-decl-or-cast
8418 ;; CASE 1
8419 (when (> paren-depth 0)
8420 ;; Encountered something inside parens that isn't matched by
8421 ;; the `c-type-decl-*' regexps, so it's not a type decl
8422 ;; expression. Try to skip out to the same paren depth to
8423 ;; not confuse the cast check below. If we don't manage this and
8424 ;; `at-decl-or-cast' is 'ids we might have an expression like
8425 ;; "foo bar ({ ..." which is a valid C++11 initialization.
8426 (if (and (not (c-safe (goto-char (scan-lists (point) 1 paren-depth))))
8427 (eq at-decl-or-cast 'ids))
8428 (c-fdoc-shift-type-backward))
8429 ;; If we've found a specifier keyword then it's a
8430 ;; declaration regardless.
8431 (throw 'at-decl-or-cast (memq at-decl-or-cast '(t ids))))
8433 (setq at-decl-end
8434 (looking-at (cond ((eq context '<>) "[,>]")
8435 ((not (memq context '(nil top))) "[,\)]")
8436 (t "[,;]"))))
8438 ;; Now we've collected info about various characteristics of
8439 ;; the construct we're looking at. Below follows a decision
8440 ;; tree based on that. It's ordered to check more certain
8441 ;; signs before less certain ones.
8443 (if got-identifier
8444 (progn
8446 ;; CASE 2
8447 (when (and (or at-type maybe-typeless)
8448 (not (or got-prefix got-parens)))
8449 ;; Got another identifier directly after the type, so it's a
8450 ;; declaration.
8451 (throw 'at-decl-or-cast t))
8453 (when (and got-parens
8454 (not got-prefix)
8455 ;; (not got-suffix-after-parens)
8456 (or backup-at-type
8457 maybe-typeless
8458 backup-maybe-typeless
8459 (eq at-decl-or-cast t)
8460 ;; Check whether we have "bar (gnu);" where we
8461 ;; are directly inside a class (etc.) called "bar".
8462 (save-excursion
8463 (and
8464 (progn
8465 (goto-char name-start)
8466 (not (memq (c-forward-type) '(nil maybe))))
8467 (progn
8468 (goto-char id-start)
8469 (c-directly-in-class-called-p
8470 (buffer-substring
8471 type-start
8472 (progn
8473 (goto-char type-start)
8474 (c-forward-type)
8475 (c-backward-syntactic-ws)
8476 (point)))))))))
8477 ;; Got a declaration of the form "foo bar (gnu);" or "bar
8478 ;; (gnu);" where we've recognized "bar" as the type and "gnu"
8479 ;; as the declarator, and in the latter case, checked that
8480 ;; "bar (gnu)" appears directly inside the class "bar". In
8481 ;; this case it's however more likely that "bar" is the
8482 ;; declarator and "gnu" a function argument or initializer
8483 ;; (if `c-recognize-paren-inits' is set), since the parens
8484 ;; around "gnu" would be superfluous if it's a declarator.
8485 ;; Shift the type one step backward.
8486 (c-fdoc-shift-type-backward)))
8488 ;; Found no identifier.
8490 (if backup-at-type
8491 (progn
8493 ;; CASE 3
8494 (when (= (point) start)
8495 ;; Got a plain list of identifiers. If a colon follows it's
8496 ;; a valid label, or maybe a bitfield. Otherwise the last
8497 ;; one probably is the declared identifier and we should
8498 ;; back up to the previous type, providing it isn't a cast.
8499 (if (and (eq (char-after) ?:)
8500 (not (c-major-mode-is 'java-mode)))
8501 (cond
8502 ;; If we've found a specifier keyword then it's a
8503 ;; declaration regardless.
8504 ((eq at-decl-or-cast t)
8505 (throw 'at-decl-or-cast t))
8506 ((and c-has-bitfields
8507 (eq at-decl-or-cast 'ids)) ; bitfield.
8508 (setq backup-if-not-cast t)
8509 (throw 'at-decl-or-cast t)))
8511 (setq backup-if-not-cast t)
8512 (throw 'at-decl-or-cast t)))
8514 ;; CASE 4
8515 (when (and got-suffix
8516 (not got-prefix)
8517 (not got-parens))
8518 ;; Got a plain list of identifiers followed by some suffix.
8519 ;; If this isn't a cast then the last identifier probably is
8520 ;; the declared one and we should back up to the previous
8521 ;; type.
8522 (setq backup-if-not-cast t)
8523 (throw 'at-decl-or-cast t)))
8525 ;; CASE 5
8526 (when (eq at-type t)
8527 ;; If the type is known we know that there can't be any
8528 ;; identifier somewhere else, and it's only in declarations in
8529 ;; e.g. function prototypes and in casts that the identifier may
8530 ;; be left out.
8531 (throw 'at-decl-or-cast t))
8533 (when (= (point) start)
8534 ;; Only got a single identifier (parsed as a type so far).
8535 ;; CASE 6
8536 (if (and
8537 ;; Check that the identifier isn't at the start of an
8538 ;; expression.
8539 at-decl-end
8540 (cond
8541 ((eq context 'decl)
8542 ;; Inside an arglist that contains declarations. If K&R
8543 ;; style declarations and parenthesis style initializers
8544 ;; aren't allowed then the single identifier must be a
8545 ;; type, else we require that it's known or found
8546 ;; (primitive types are handled above).
8547 (or (and (not c-recognize-knr-p)
8548 (not c-recognize-paren-inits))
8549 (memq at-type '(known found))))
8550 ((eq context '<>)
8551 ;; Inside a template arglist. Accept known and found
8552 ;; types; other identifiers could just as well be
8553 ;; constants in C++.
8554 (memq at-type '(known found)))))
8555 (throw 'at-decl-or-cast t)
8556 ;; CASE 7
8557 ;; Can't be a valid declaration or cast, but if we've found a
8558 ;; specifier it can't be anything else either, so treat it as
8559 ;; an invalid/unfinished declaration or cast.
8560 (throw 'at-decl-or-cast at-decl-or-cast))))
8562 (if (and got-parens
8563 (not got-prefix)
8564 (memq context '(nil top))
8565 (not (eq at-type t))
8566 (or backup-at-type
8567 maybe-typeless
8568 backup-maybe-typeless
8569 (when c-recognize-typeless-decls
8570 (or (not got-suffix)
8571 (not (looking-at
8572 c-after-suffixed-type-maybe-decl-key))))))
8573 ;; Got an empty paren pair and a preceding type that probably
8574 ;; really is the identifier. Shift the type backwards to make
8575 ;; the last one the identifier. This is analogous to the
8576 ;; "backtracking" done inside the `c-type-decl-suffix-key' loop
8577 ;; above.
8579 ;; Exception: In addition to the conditions in that
8580 ;; "backtracking" code, do not shift backward if we're not
8581 ;; looking at either `c-after-suffixed-type-decl-key' or "[;,]".
8582 ;; Since there's no preceding type, the shift would mean that
8583 ;; the declaration is typeless. But if the regexp doesn't match
8584 ;; then we will simply fall through in the tests below and not
8585 ;; recognize it at all, so it's better to try it as an abstract
8586 ;; declarator instead.
8587 (c-fdoc-shift-type-backward)
8589 ;; Still no identifier.
8590 ;; CASE 8
8591 (when (and got-prefix (or got-parens got-suffix))
8592 ;; Require `got-prefix' together with either `got-parens' or
8593 ;; `got-suffix' to recognize it as an abstract declarator:
8594 ;; `got-parens' only is probably an empty function call.
8595 ;; `got-suffix' only can build an ordinary expression together
8596 ;; with the preceding identifier which we've taken as a type.
8597 ;; We could actually accept on `got-prefix' only, but that can
8598 ;; easily occur temporarily while writing an expression so we
8599 ;; avoid that case anyway. We could do a better job if we knew
8600 ;; the point when the fontification was invoked.
8601 (throw 'at-decl-or-cast t))
8603 ;; CASE 9
8604 (when (and at-type
8605 (not got-prefix)
8606 (not got-parens)
8607 got-suffix-after-parens
8608 (eq (char-after got-suffix-after-parens) ?\())
8609 ;; Got a type, no declarator but a paren suffix. I.e. it's a
8610 ;; normal function call after all (or perhaps a C++ style object
8611 ;; instantiation expression).
8612 (throw 'at-decl-or-cast nil))))
8614 ;; CASE 9.5
8615 (when (and (not context) ; i.e. not at top level.
8616 (c-major-mode-is 'c++-mode)
8617 (eq at-decl-or-cast 'ids)
8618 after-paren-pos)
8619 ;; We've got something like "foo bar (...)" in C++ which isn't at
8620 ;; the top level. This is probably a uniform initialization of bar
8621 ;; to the contents of the parens. In this case the declarator ends
8622 ;; at the open paren.
8623 (goto-char (1- after-paren-pos))
8624 (throw 'at-decl-or-cast t))
8626 ;; CASE 10
8627 (when at-decl-or-cast
8628 ;; By now we've located the type in the declaration that we know
8629 ;; we're in.
8630 (throw 'at-decl-or-cast t))
8632 ;; CASE 11
8633 (when (and got-identifier
8634 (looking-at c-after-suffixed-type-decl-key)
8635 (or (eq context 'top)
8636 (and (eq context nil)
8637 (match-beginning 1)))
8638 (if (and got-parens
8639 (not got-prefix)
8640 (not got-suffix)
8641 (not (eq at-type t)))
8642 ;; Shift the type backward in the case that there's a
8643 ;; single identifier inside parens. That can only
8644 ;; occur in K&R style function declarations so it's
8645 ;; more likely that it really is a function call.
8646 ;; Therefore we only do this after
8647 ;; `c-after-suffixed-type-decl-key' has matched.
8648 (progn (c-fdoc-shift-type-backward) t)
8649 got-suffix-after-parens))
8650 ;; A declaration according to `c-after-suffixed-type-decl-key'.
8651 (throw 'at-decl-or-cast t))
8653 ;; CASE 12
8654 (when (and (or got-prefix (not got-parens))
8655 (memq at-type '(t known)))
8656 ;; It's a declaration if a known type precedes it and it can't be a
8657 ;; function call.
8658 (throw 'at-decl-or-cast t))
8660 ;; If we get here we can't tell if this is a type decl or a normal
8661 ;; expression by looking at it alone. (That's under the assumption
8662 ;; that normal expressions always can look like type decl expressions,
8663 ;; which isn't really true but the cases where it doesn't hold are so
8664 ;; uncommon (e.g. some placements of "const" in C++) it's not worth
8665 ;; the effort to look for them.)
8667 ;;; 2008-04-16: commented out the next form, to allow the function to recognize
8668 ;;; "foo (int bar)" in CC (an implicit type (in class foo) without a semicolon)
8669 ;;; as a(n almost complete) declaration, enabling it to be fontified.
8670 ;; CASE 13
8671 ;; (unless (or at-decl-end (looking-at "=[^=]"))
8672 ;; If this is a declaration it should end here or its initializer(*)
8673 ;; should start here, so check for allowed separation tokens. Note
8674 ;; that this rule doesn't work e.g. with a K&R arglist after a
8675 ;; function header.
8677 ;; *) Don't check for C++ style initializers using parens
8678 ;; since those already have been matched as suffixes.
8680 ;; If `at-decl-or-cast' is then we've found some other sign that
8681 ;; it's a declaration or cast, so then it's probably an
8682 ;; invalid/unfinished one.
8683 ;; (throw 'at-decl-or-cast at-decl-or-cast))
8685 ;; Below are tests that only should be applied when we're certain to
8686 ;; not have parsed halfway through an expression.
8688 ;; CASE 14
8689 (when (memq at-type '(t known))
8690 ;; The expression starts with a known type so treat it as a
8691 ;; declaration.
8692 (throw 'at-decl-or-cast t))
8694 ;; CASE 15
8695 (when (and (c-major-mode-is 'c++-mode)
8696 ;; In C++ we check if the identifier is a known type, since
8697 ;; (con|de)structors use the class name as identifier.
8698 ;; We've always shifted over the identifier as a type and
8699 ;; then backed up again in this case.
8700 identifier-type
8701 (or (memq identifier-type '(found known))
8702 (and (eq (char-after identifier-start) ?~)
8703 ;; `at-type' probably won't be 'found for
8704 ;; destructors since the "~" is then part of the
8705 ;; type name being checked against the list of
8706 ;; known types, so do a check without that
8707 ;; operator.
8708 (or (save-excursion
8709 (goto-char (1+ identifier-start))
8710 (c-forward-syntactic-ws)
8711 (c-with-syntax-table
8712 c-identifier-syntax-table
8713 (looking-at c-known-type-key)))
8714 (save-excursion
8715 (goto-char (1+ identifier-start))
8716 ;; We have already parsed the type earlier,
8717 ;; so it'd be possible to cache the end
8718 ;; position instead of redoing it here, but
8719 ;; then we'd need to keep track of another
8720 ;; position everywhere.
8721 (c-check-type (point)
8722 (progn (c-forward-type)
8723 (point))))))))
8724 (throw 'at-decl-or-cast t))
8726 (if got-identifier
8727 (progn
8728 ;; CASE 16
8729 (when (and got-prefix-before-parens
8730 at-type
8731 (or at-decl-end (looking-at "=[^=]"))
8732 (memq context '(nil top))
8733 (or (not got-suffix)
8734 at-decl-start))
8735 ;; Got something like "foo * bar;". Since we're not inside
8736 ;; an arglist it would be a meaningless expression because
8737 ;; the result isn't used. We therefore choose to recognize
8738 ;; it as a declaration. We only allow a suffix (which makes
8739 ;; the construct look like a function call) when
8740 ;; `at-decl-start' provides additional evidence that we do
8741 ;; have a declaration.
8742 (setq maybe-expression t)
8743 (throw 'at-decl-or-cast t))
8745 ;; CASE 17
8746 (when (and (or got-suffix-after-parens
8747 (looking-at "=[^=]"))
8748 (eq at-type 'found)
8749 (not (eq context 'arglist)))
8750 ;; Got something like "a (*b) (c);" or "a (b) = c;". It could
8751 ;; be an odd expression or it could be a declaration. Treat
8752 ;; it as a declaration if "a" has been used as a type
8753 ;; somewhere else (if it's a known type we won't get here).
8754 (setq maybe-expression t)
8755 (throw 'at-decl-or-cast t)))
8757 ;; CASE 18
8758 (when (and (not (memq context '(nil top)))
8759 (or got-prefix
8760 (and (eq context 'decl)
8761 (not c-recognize-paren-inits)
8762 (or got-parens got-suffix))))
8763 ;; Got a type followed by an abstract declarator. If `got-prefix'
8764 ;; is set it's something like "a *" without anything after it. If
8765 ;; `got-parens' or `got-suffix' is set it's "a()", "a[]", "a()[]",
8766 ;; or similar, which we accept only if the context rules out
8767 ;; expressions.
8768 (throw 'at-decl-or-cast t)))
8770 ;; If we had a complete symbol table here (which rules out
8771 ;; `c-found-types') we should return t due to the disambiguation rule
8772 ;; (in at least C++) that anything that can be parsed as a declaration
8773 ;; is a declaration. Now we're being more defensive and prefer to
8774 ;; highlight things like "foo (bar);" as a declaration only if we're
8775 ;; inside an arglist that contains declarations.
8776 ;; CASE 19
8777 (eq context 'decl))))
8779 ;; The point is now after the type decl expression.
8781 (cond
8782 ;; Check for a cast.
8783 ((save-excursion
8784 (and
8785 c-cast-parens
8787 ;; Should be the first type/identifier in a cast paren.
8788 (> preceding-token-end (point-min))
8789 (memq (char-before preceding-token-end) c-cast-parens)
8791 ;; The closing paren should follow.
8792 (progn
8793 (c-forward-syntactic-ws)
8794 (looking-at "\\s)"))
8796 ;; There should be a primary expression after it.
8797 (let (pos)
8798 (forward-char)
8799 (c-forward-syntactic-ws)
8800 (setq cast-end (point))
8801 (and (looking-at c-primary-expr-regexp)
8802 (progn
8803 (setq pos (match-end 0))
8805 ;; Check if the expression begins with a prefix keyword.
8806 (match-beginning 2)
8807 (if (match-beginning 1)
8808 ;; Expression begins with an ambiguous operator. Treat
8809 ;; it as a cast if it's a type decl or if we've
8810 ;; recognized the type somewhere else.
8811 (or at-decl-or-cast
8812 (memq at-type '(t known found)))
8813 ;; Unless it's a keyword, it's the beginning of a primary
8814 ;; expression.
8815 (not (looking-at c-keywords-regexp)))))
8816 ;; If `c-primary-expr-regexp' matched a nonsymbol token, check
8817 ;; that it matched a whole one so that we don't e.g. confuse
8818 ;; the operator '-' with '->'. It's ok if it matches further,
8819 ;; though, since it e.g. can match the float '.5' while the
8820 ;; operator regexp only matches '.'.
8821 (or (not (looking-at c-nonsymbol-token-regexp))
8822 (<= (match-end 0) pos))))
8824 ;; There should either be a cast before it or something that isn't an
8825 ;; identifier or close paren.
8826 (> preceding-token-end (point-min))
8827 (progn
8828 (goto-char (1- preceding-token-end))
8829 (or (eq (point) last-cast-end)
8830 (progn
8831 (c-backward-syntactic-ws)
8832 (if (< (skip-syntax-backward "w_") 0)
8833 ;; It's a symbol. Accept it only if it's one of the
8834 ;; keywords that can precede an expression (without
8835 ;; surrounding parens).
8836 (looking-at c-simple-stmt-key)
8837 (and
8838 ;; Check that it isn't a close paren (block close is ok,
8839 ;; though).
8840 (not (memq (char-before) '(?\) ?\])))
8841 ;; Check that it isn't a nonsymbol identifier.
8842 (not (c-on-identifier)))))))))
8844 ;; Handle the cast.
8845 (when (and c-record-type-identifiers at-type (not (eq at-type t)))
8846 (let ((c-promote-possible-types t))
8847 (goto-char type-start)
8848 (c-forward-type)))
8850 (goto-char cast-end)
8851 'cast)
8853 (at-decl-or-cast
8854 ;; We're at a declaration. Highlight the type and the following
8855 ;; declarators.
8857 (when backup-if-not-cast
8858 (c-fdoc-shift-type-backward t))
8860 (when (and (eq context 'decl) (looking-at ","))
8861 ;; Make sure to propagate the `c-decl-arg-start' property to
8862 ;; the next argument if it's set in this one, to cope with
8863 ;; interactive refontification.
8864 (c-put-c-type-property (point) 'c-decl-arg-start))
8866 ;; Record the type's coordinates in `c-record-type-identifiers' for
8867 ;; later fontification.
8868 (when (and c-record-type-identifiers at-type ;; (not (eq at-type t))
8869 ;; There seems no reason to exclude a token from
8870 ;; fontification just because it's "a known type that can't
8871 ;; be a name or other expression". 2013-09-18.
8873 (let ((c-promote-possible-types t))
8874 (save-excursion
8875 (goto-char type-start)
8876 (c-forward-type))))
8878 (list id-start
8879 (and (or at-type-decl at-typedef)
8880 (cons at-type-decl at-typedef))
8881 maybe-expression
8882 type-start))
8885 ;; False alarm. Restore the recorded ranges.
8886 (setq c-record-type-identifiers save-rec-type-ids
8887 c-record-ref-identifiers save-rec-ref-ids)
8888 nil))))
8890 (defun c-forward-label (&optional assume-markup preceding-token-end limit)
8891 ;; Assuming that point is at the beginning of a token, check if it starts a
8892 ;; label and if so move over it and return non-nil (t in default situations,
8893 ;; specific symbols (see below) for interesting situations), otherwise don't
8894 ;; move and return nil. "Label" here means "most things with a colon".
8896 ;; More precisely, a "label" is regarded as one of:
8897 ;; (i) a goto target like "foo:" - returns the symbol `goto-target';
8898 ;; (ii) A case label - either the entire construct "case FOO:", or just the
8899 ;; bare "case", should the colon be missing. We return t;
8900 ;; (iii) a keyword which needs a colon, like "default:" or "private:"; We
8901 ;; return t;
8902 ;; (iv) One of QT's "extended" C++ variants of
8903 ;; "private:"/"protected:"/"public:"/"more:" looking like "public slots:".
8904 ;; Returns the symbol `qt-2kwds-colon'.
8905 ;; (v) QT's construct "signals:". Returns the symbol `qt-1kwd-colon'.
8906 ;; (vi) One of the keywords matched by `c-opt-extra-label-key' (without any
8907 ;; colon). Currently (2006-03), this applies only to Objective C's
8908 ;; keywords "@private", "@protected", and "@public". Returns t.
8910 ;; One of the things which will NOT be recognized as a label is a bit-field
8911 ;; element of a struct, something like "int foo:5".
8913 ;; The end of the label is taken to be just after the colon, or the end of
8914 ;; the first submatch in `c-opt-extra-label-key'. The point is directly
8915 ;; after the end on return. The terminating char gets marked with
8916 ;; `c-decl-end' to improve recognition of the following declaration or
8917 ;; statement.
8919 ;; If ASSUME-MARKUP is non-nil, it's assumed that the preceding
8920 ;; label, if any, has already been marked up like that.
8922 ;; If PRECEDING-TOKEN-END is given, it should be the first position
8923 ;; after the preceding token, i.e. on the other side of the
8924 ;; syntactic ws from the point. Use a value less than or equal to
8925 ;; (point-min) if the point is at the first token in (the visible
8926 ;; part of) the buffer.
8928 ;; The optional LIMIT limits the forward scan for the colon.
8930 ;; This function records the ranges of the label symbols on
8931 ;; `c-record-ref-identifiers' if `c-record-type-identifiers' (!) is
8932 ;; non-nil.
8934 ;; This function might do hidden buffer changes.
8936 (let ((start (point))
8937 label-end
8938 qt-symbol-idx
8939 macro-start ; if we're in one.
8940 label-type
8941 kwd)
8942 (cond
8943 ;; "case" or "default" (Doesn't apply to AWK).
8944 ((looking-at c-label-kwds-regexp)
8945 (let ((kwd-end (match-end 1)))
8946 ;; Record only the keyword itself for fontification, since in
8947 ;; case labels the following is a constant expression and not
8948 ;; a label.
8949 (when c-record-type-identifiers
8950 (c-record-ref-id (cons (match-beginning 1) kwd-end)))
8952 ;; Find the label end.
8953 (goto-char kwd-end)
8954 (setq label-type
8955 (if (and (c-syntactic-re-search-forward
8956 ;; Stop on chars that aren't allowed in expressions,
8957 ;; and on operator chars that would be meaningless
8958 ;; there. FIXME: This doesn't cope with ?: operators.
8959 "[;{=,@]\\|\\(\\=\\|[^:]\\):\\([^:]\\|\\'\\)"
8960 limit t t nil 1)
8961 (match-beginning 2))
8963 (progn ; there's a proper :
8964 (goto-char (match-beginning 2)) ; just after the :
8965 (c-put-c-type-property (1- (point)) 'c-decl-end)
8968 ;; It's an unfinished label. We consider the keyword enough
8969 ;; to recognize it as a label, so that it gets fontified.
8970 ;; Leave the point at the end of it, but don't put any
8971 ;; `c-decl-end' marker.
8972 (goto-char kwd-end)
8973 t))))
8975 ;; @private, @protected, @public, in Objective C, or similar.
8976 ((and c-opt-extra-label-key
8977 (looking-at c-opt-extra-label-key))
8978 ;; For a `c-opt-extra-label-key' match, we record the whole
8979 ;; thing for fontification. That's to get the leading '@' in
8980 ;; Objective-C protection labels fontified.
8981 (goto-char (match-end 1))
8982 (when c-record-type-identifiers
8983 (c-record-ref-id (cons (match-beginning 1) (point))))
8984 (c-put-c-type-property (1- (point)) 'c-decl-end)
8985 (setq label-type t))
8987 ;; All other cases of labels.
8988 ((and c-recognize-colon-labels ; nil for AWK and IDL, otherwise t.
8990 ;; A colon label must have something before the colon.
8991 (not (eq (char-after) ?:))
8993 ;; Check that we're not after a token that can't precede a label.
8995 ;; Trivially succeeds when there's no preceding token.
8996 ;; Succeeds when we're at a virtual semicolon.
8997 (if preceding-token-end
8998 (<= preceding-token-end (point-min))
8999 (save-excursion
9000 (c-backward-syntactic-ws)
9001 (setq preceding-token-end (point))
9002 (or (bobp)
9003 (c-at-vsemi-p))))
9005 ;; Check if we're after a label, if we're after a closing
9006 ;; paren that belong to statement, and with
9007 ;; `c-label-prefix-re'. It's done in different order
9008 ;; depending on `assume-markup' since the checks have
9009 ;; different expensiveness.
9010 (if assume-markup
9012 (eq (c-get-char-property (1- preceding-token-end) 'c-type)
9013 'c-decl-end)
9015 (save-excursion
9016 (goto-char (1- preceding-token-end))
9017 (c-beginning-of-current-token)
9018 (or (looking-at c-label-prefix-re)
9019 (looking-at c-block-stmt-1-key)))
9021 (and (eq (char-before preceding-token-end) ?\))
9022 (c-after-conditional)))
9025 (save-excursion
9026 (goto-char (1- preceding-token-end))
9027 (c-beginning-of-current-token)
9028 (or (looking-at c-label-prefix-re)
9029 (looking-at c-block-stmt-1-key)))
9031 (cond
9032 ((eq (char-before preceding-token-end) ?\))
9033 (c-after-conditional))
9035 ((eq (char-before preceding-token-end) ?:)
9036 ;; Might be after another label, so check it recursively.
9037 (save-restriction
9038 (save-excursion
9039 (goto-char (1- preceding-token-end))
9040 ;; Essentially the same as the
9041 ;; `c-syntactic-re-search-forward' regexp below.
9042 (setq macro-start
9043 (save-excursion (and (c-beginning-of-macro)
9044 (point))))
9045 (if macro-start (narrow-to-region macro-start (point-max)))
9046 (c-syntactic-skip-backward "^-]:?;}=*/%&|,<>!@+" nil t)
9047 ;; Note: the following should work instead of the
9048 ;; narrow-to-region above. Investigate why not,
9049 ;; sometime. ACM, 2006-03-31.
9050 ;; (c-syntactic-skip-backward "^-]:?;}=*/%&|,<>!@+"
9051 ;; macro-start t)
9052 (let ((pte (point))
9053 ;; If the caller turned on recording for us,
9054 ;; it shouldn't apply when we check the
9055 ;; preceding label.
9056 c-record-type-identifiers)
9057 ;; A label can't start at a cpp directive. Check for
9058 ;; this, since c-forward-syntactic-ws would foul up on it.
9059 (unless (and c-opt-cpp-prefix (looking-at c-opt-cpp-prefix))
9060 (c-forward-syntactic-ws)
9061 (c-forward-label nil pte start))))))))))
9063 ;; Point is still at the beginning of the possible label construct.
9065 ;; Check that the next nonsymbol token is ":", or that we're in one
9066 ;; of QT's "slots" declarations. Allow '(' for the sake of macro
9067 ;; arguments. FIXME: Should build this regexp from the language
9068 ;; constants.
9069 (cond
9070 ;; public: protected: private:
9071 ((and
9072 (c-major-mode-is 'c++-mode)
9073 (search-forward-regexp
9074 "\\=p\\(r\\(ivate\\|otected\\)\\|ublic\\)\\>[^_]" nil t)
9075 (progn (backward-char)
9076 (c-forward-syntactic-ws limit)
9077 (looking-at ":\\([^:]\\|\\'\\)"))) ; A single colon.
9078 (forward-char)
9079 (setq label-type t))
9080 ;; QT double keyword like "protected slots:" or goto target.
9081 ((progn (goto-char start) nil))
9082 ((when (c-syntactic-re-search-forward
9083 "[ \t\n[:?;{=*/%&|,<>!@+-]" limit t t) ; not at EOB
9084 (backward-char)
9085 (setq label-end (point))
9086 (setq qt-symbol-idx
9087 (and (c-major-mode-is 'c++-mode)
9088 (string-match
9089 "\\(p\\(r\\(ivate\\|otected\\)\\|ublic\\)\\|more\\)\\>"
9090 (buffer-substring start (point)))))
9091 (c-forward-syntactic-ws limit)
9092 (cond
9093 ((looking-at ":\\([^:]\\|\\'\\)") ; A single colon.
9094 (forward-char)
9095 (setq label-type
9096 (if (or (string= "signals" ; Special QT macro
9097 (setq kwd (buffer-substring-no-properties start label-end)))
9098 (string= "Q_SIGNALS" kwd))
9099 'qt-1kwd-colon
9100 'goto-target)))
9101 ((and qt-symbol-idx
9102 (search-forward-regexp "\\=\\(slots\\|Q_SLOTS\\)\\>" limit t)
9103 (progn (c-forward-syntactic-ws limit)
9104 (looking-at ":\\([^:]\\|\\'\\)"))) ; A single colon
9105 (forward-char)
9106 (setq label-type 'qt-2kwds-colon)))))))
9108 (save-restriction
9109 (narrow-to-region start (point))
9111 ;; Check that `c-nonlabel-token-key' doesn't match anywhere.
9112 (catch 'check-label
9113 (goto-char start)
9114 (while (progn
9115 (when (looking-at c-nonlabel-token-key)
9116 (goto-char start)
9117 (setq label-type nil)
9118 (throw 'check-label nil))
9119 (and (c-safe (c-forward-sexp)
9120 (c-forward-syntactic-ws)
9122 (not (eobp)))))
9124 ;; Record the identifiers in the label for fontification, unless
9125 ;; it begins with `c-label-kwds' in which case the following
9126 ;; identifiers are part of a (constant) expression that
9127 ;; shouldn't be fontified.
9128 (when (and c-record-type-identifiers
9129 (progn (goto-char start)
9130 (not (looking-at c-label-kwds-regexp))))
9131 (while (c-syntactic-re-search-forward c-symbol-key nil t)
9132 (c-record-ref-id (cons (match-beginning 0)
9133 (match-end 0)))))
9135 (c-put-c-type-property (1- (point-max)) 'c-decl-end)
9136 (goto-char (point-max)))))
9139 ;; Not a label.
9140 (goto-char start)))
9141 label-type))
9143 (defun c-forward-objc-directive ()
9144 ;; Assuming the point is at the beginning of a token, try to move
9145 ;; forward to the end of the Objective-C directive that starts
9146 ;; there. Return t if a directive was fully recognized, otherwise
9147 ;; the point is moved as far as one could be successfully parsed and
9148 ;; nil is returned.
9150 ;; This function records identifier ranges on
9151 ;; `c-record-type-identifiers' and `c-record-ref-identifiers' if
9152 ;; `c-record-type-identifiers' is non-nil.
9154 ;; This function might do hidden buffer changes.
9156 (let ((start (point))
9157 start-char
9158 (c-promote-possible-types t)
9160 ;; Turn off recognition of angle bracket arglists while parsing
9161 ;; types here since the protocol reference list might then be
9162 ;; considered part of the preceding name or superclass-name.
9163 c-recognize-<>-arglists)
9165 (if (or
9166 (when (looking-at
9167 (eval-when-compile
9168 (c-make-keywords-re t
9169 (append (c-lang-const c-protection-kwds objc)
9170 '("@end"))
9171 'objc-mode)))
9172 (goto-char (match-end 1))
9175 (and
9176 (looking-at
9177 (eval-when-compile
9178 (c-make-keywords-re t
9179 '("@interface" "@implementation" "@protocol")
9180 'objc-mode)))
9182 ;; Handle the name of the class itself.
9183 (progn
9184 ;; (c-forward-token-2) ; 2006/1/13 This doesn't move if the token's
9185 ;; at EOB.
9186 (goto-char (match-end 0))
9187 (setq lim (point))
9188 (c-skip-ws-forward)
9189 (c-forward-type))
9191 (catch 'break
9192 ;; Look for ": superclass-name" or "( category-name )".
9193 (when (looking-at "[:(]")
9194 (setq start-char (char-after))
9195 (forward-char)
9196 (c-forward-syntactic-ws)
9197 (unless (c-forward-type) (throw 'break nil))
9198 (when (eq start-char ?\()
9199 (unless (eq (char-after) ?\)) (throw 'break nil))
9200 (forward-char)
9201 (c-forward-syntactic-ws)))
9203 ;; Look for a protocol reference list.
9204 (if (eq (char-after) ?<)
9205 (let ((c-recognize-<>-arglists t)
9206 (c-parse-and-markup-<>-arglists t)
9207 c-restricted-<>-arglists)
9208 (c-forward-<>-arglist t))
9209 t))))
9211 (progn
9212 (c-backward-syntactic-ws lim)
9213 (c-clear-c-type-property start (1- (point)) 'c-decl-end)
9214 (c-put-c-type-property (1- (point)) 'c-decl-end)
9217 (c-clear-c-type-property start (point) 'c-decl-end)
9218 nil)))
9220 (defun c-beginning-of-inheritance-list (&optional lim)
9221 ;; Go to the first non-whitespace after the colon that starts a
9222 ;; multiple inheritance introduction. Optional LIM is the farthest
9223 ;; back we should search.
9225 ;; This function might do hidden buffer changes.
9226 (c-with-syntax-table c++-template-syntax-table
9227 (c-backward-token-2 0 t lim)
9228 (while (and (or (looking-at c-symbol-start)
9229 (looking-at "[<,]\\|::"))
9230 (zerop (c-backward-token-2 1 t lim))))))
9232 (defun c-in-method-def-p ()
9233 ;; Return nil if we aren't in a method definition, otherwise the
9234 ;; position of the initial [+-].
9236 ;; This function might do hidden buffer changes.
9237 (save-excursion
9238 (beginning-of-line)
9239 (and c-opt-method-key
9240 (looking-at c-opt-method-key)
9241 (point))
9244 ;; Contributed by Kevin Ryde <user42@zip.com.au>.
9245 (defun c-in-gcc-asm-p ()
9246 ;; Return non-nil if point is within a gcc \"asm\" block.
9248 ;; This should be called with point inside an argument list.
9250 ;; Only one level of enclosing parentheses is considered, so for
9251 ;; instance nil is returned when in a function call within an asm
9252 ;; operand.
9254 ;; This function might do hidden buffer changes.
9256 (and c-opt-asm-stmt-key
9257 (save-excursion
9258 (beginning-of-line)
9259 (backward-up-list 1)
9260 (c-beginning-of-statement-1 (point-min) nil t)
9261 (looking-at c-opt-asm-stmt-key))))
9263 (defun c-at-toplevel-p ()
9264 "Return a determination as to whether point is \"at the top level\".
9265 Informally, \"at the top level\" is anywhere where you can write
9266 a function.
9268 More precisely, being at the top-level means that point is either
9269 outside any enclosing block (such as a function definition), or
9270 directly inside a class, namespace or other block that contains
9271 another declaration level.
9273 If point is not at the top-level (e.g. it is inside a method
9274 definition), then nil is returned. Otherwise, if point is at a
9275 top-level not enclosed within a class definition, t is returned.
9276 Otherwise, a 2-vector is returned where the zeroth element is the
9277 buffer position of the start of the class declaration, and the first
9278 element is the buffer position of the enclosing class's opening
9279 brace.
9281 Note that this function might do hidden buffer changes. See the
9282 comment at the start of cc-engine.el for more info."
9283 ;; Note to maintainers: this function consumes a great mass of CPU cycles.
9284 ;; Its use should thus be minimized as far as possible.
9285 (let ((paren-state (c-parse-state)))
9286 (or (not (c-most-enclosing-brace paren-state))
9287 (c-search-uplist-for-classkey paren-state))))
9289 (defun c-just-after-func-arglist-p (&optional lim)
9290 ;; Return non-nil if the point is in the region after the argument
9291 ;; list of a function and its opening brace (or semicolon in case it
9292 ;; got no body). If there are K&R style argument declarations in
9293 ;; that region, the point has to be inside the first one for this
9294 ;; function to recognize it.
9296 ;; If successful, the point is moved to the first token after the
9297 ;; function header (see `c-forward-decl-or-cast-1' for details) and
9298 ;; the position of the opening paren of the function arglist is
9299 ;; returned.
9301 ;; The point is clobbered if not successful.
9303 ;; LIM is used as bound for backward buffer searches.
9305 ;; This function might do hidden buffer changes.
9307 (let ((beg (point)) id-start)
9308 (and
9309 (eq (c-beginning-of-statement-1 lim) 'same)
9311 (not (and (c-major-mode-is 'objc-mode)
9312 (c-forward-objc-directive)))
9314 (setq id-start
9315 (car-safe (c-forward-decl-or-cast-1 (c-point 'bosws) 'top nil)))
9316 (< id-start beg)
9318 ;; There should not be a '=' or ',' between beg and the
9319 ;; start of the declaration since that means we were in the
9320 ;; "expression part" of the declaration.
9321 (or (> (point) beg)
9322 (not (looking-at "[=,]")))
9324 (save-excursion
9325 ;; Check that there's an arglist paren in the
9326 ;; declaration.
9327 (goto-char id-start)
9328 (cond ((eq (char-after) ?\()
9329 ;; The declarator is a paren expression, so skip past it
9330 ;; so that we don't get stuck on that instead of the
9331 ;; function arglist.
9332 (c-forward-sexp))
9333 ((and c-opt-op-identifier-prefix
9334 (looking-at c-opt-op-identifier-prefix))
9335 ;; Don't trip up on "operator ()".
9336 (c-forward-token-2 2 t)))
9337 (and (< (point) beg)
9338 (c-syntactic-re-search-forward "(" beg t t)
9339 (1- (point)))))))
9341 (defun c-in-knr-argdecl (&optional lim)
9342 ;; Return the position of the first argument declaration if point is
9343 ;; inside a K&R style argument declaration list, nil otherwise.
9344 ;; `c-recognize-knr-p' is not checked. If LIM is non-nil, it's a
9345 ;; position that bounds the backward search for the argument list. This
9346 ;; function doesn't move point.
9348 ;; Point must be within a possible K&R region, e.g. just before a top-level
9349 ;; "{". It must be outside of parens and brackets. The test can return
9350 ;; false positives otherwise.
9352 ;; This function might do hidden buffer changes.
9353 (save-excursion
9354 (save-restriction
9355 ;; If we're in a macro, our search range is restricted to it. Narrow to
9356 ;; the searchable range.
9357 (let* ((macro-start (save-excursion (and (c-beginning-of-macro) (point))))
9358 (macro-end (save-excursion (and macro-start (c-end-of-macro) (point))))
9359 (low-lim (max (or lim (point-min)) (or macro-start (point-min))))
9360 before-lparen after-rparen
9361 (here (point))
9362 (pp-count-out 20) ; Max number of paren/brace constructs before
9363 ; we give up.
9364 ids ; List of identifiers in the parenthesized list.
9365 id-start after-prec-token decl-or-cast decl-res
9366 c-last-identifier-range identifier-ok)
9367 (narrow-to-region low-lim (or macro-end (point-max)))
9369 ;; Search backwards for the defun's argument list. We give up if we
9370 ;; encounter a "}" (end of a previous defun) an "=" (which can't be in
9371 ;; a knr region) or BOB.
9373 ;; The criterion for a paren structure being the arg list is:
9374 ;; o - there is non-WS stuff after it but before any "{"; AND
9375 ;; o - the token after it isn't a ";" AND
9376 ;; o - it is preceded by either an identifier (the function name) or
9377 ;; a macro expansion like "DEFUN (...)"; AND
9378 ;; o - its content is a non-empty comma-separated list of identifiers
9379 ;; (an empty arg list won't have a knr region).
9381 ;; The following snippet illustrates these rules:
9382 ;; int foo (bar, baz, yuk)
9383 ;; int bar [] ;
9384 ;; int (*baz) (my_type) ;
9385 ;; int (*(* yuk) (void)) (void) ;
9386 ;; {
9388 ;; Additionally, for a knr list to be recognized:
9389 ;; o - The identifier of each declarator up to and including the
9390 ;; one "near" point must be contained in the arg list.
9392 (catch 'knr
9393 (while (> pp-count-out 0) ; go back one paren/bracket pair each time.
9394 (setq pp-count-out (1- pp-count-out))
9395 (c-syntactic-skip-backward "^)]}=")
9396 (cond ((eq (char-before) ?\))
9397 (setq after-rparen (point)))
9398 ((eq (char-before) ?\])
9399 (setq after-rparen nil))
9400 (t ; either } (hit previous defun) or = or no more
9401 ; parens/brackets.
9402 (throw 'knr nil)))
9404 (if after-rparen
9405 ;; We're inside a paren. Could it be our argument list....?
9407 (and
9408 (progn
9409 (goto-char after-rparen)
9410 (unless (c-go-list-backward) (throw 'knr nil)) ;
9411 ;; FIXME!!! What about macros between the parens? 2007/01/20
9412 (setq before-lparen (point)))
9414 ;; It can't be the arg list if next token is ; or {
9415 (progn (goto-char after-rparen)
9416 (c-forward-syntactic-ws)
9417 (not (memq (char-after) '(?\; ?\{ ?\=))))
9419 ;; Is the thing preceding the list an identifier (the
9420 ;; function name), or a macro expansion?
9421 (progn
9422 (goto-char before-lparen)
9423 (eq (c-backward-token-2) 0)
9424 (or (eq (c-on-identifier) (point))
9425 (and (eq (char-after) ?\))
9426 (c-go-up-list-backward)
9427 (eq (c-backward-token-2) 0)
9428 (eq (c-on-identifier) (point)))))
9430 ;; Have we got a non-empty list of comma-separated
9431 ;; identifiers?
9432 (progn
9433 (goto-char before-lparen)
9434 (c-forward-token-2) ; to first token inside parens
9435 (and
9436 (setq id-start (c-on-identifier)) ; Must be at least one.
9437 (catch 'id-list
9438 (while
9439 (progn
9440 (forward-char)
9441 (c-end-of-current-token)
9442 (push (buffer-substring-no-properties id-start
9443 (point))
9444 ids)
9445 (c-forward-syntactic-ws)
9446 (eq (char-after) ?\,))
9447 (c-forward-token-2)
9448 (unless (setq id-start (c-on-identifier))
9449 (throw 'id-list nil)))
9450 (eq (char-after) ?\)))))
9452 ;; Are all the identifiers in the k&r list up to the
9453 ;; current one also in the argument list?
9454 (progn
9455 (forward-char) ; over the )
9456 (setq after-prec-token after-rparen)
9457 (c-forward-syntactic-ws)
9458 (while (and
9459 (or (consp (setq decl-or-cast
9460 (c-forward-decl-or-cast-1
9461 after-prec-token
9462 nil ; Or 'arglist ???
9463 nil)))
9464 (progn
9465 (goto-char after-prec-token)
9466 (c-forward-syntactic-ws)
9467 (setq identifier-ok (eq (char-after) ?{))
9468 nil))
9469 (eq (char-after) ?\;)
9470 (setq after-prec-token (1+ (point)))
9471 (goto-char (car decl-or-cast))
9472 (setq decl-res (c-forward-declarator))
9473 (setq identifier-ok
9474 (member (buffer-substring-no-properties
9475 (car decl-res) (cadr decl-res))
9476 ids))
9477 (progn
9478 (goto-char after-prec-token)
9479 (prog1 (< (point) here)
9480 (c-forward-syntactic-ws))))
9481 (setq identifier-ok nil))
9482 identifier-ok))
9483 ;; ...Yes. We've identified the function's argument list.
9484 (throw 'knr
9485 (progn (goto-char after-rparen)
9486 (c-forward-syntactic-ws)
9487 (point)))
9488 ;; ...No. The current parens aren't the function's arg list.
9489 (goto-char before-lparen))
9491 (or (c-go-list-backward) ; backwards over [ .... ]
9492 (throw 'knr nil)))))))))
9494 (defun c-skip-conditional ()
9495 ;; skip forward over conditional at point, including any predicate
9496 ;; statements in parentheses. No error checking is performed.
9498 ;; This function might do hidden buffer changes.
9499 (c-forward-sexp (cond
9500 ;; else if()
9501 ((looking-at (concat "\\<else"
9502 "\\([ \t\n]\\|\\\\\n\\)+"
9503 "if\\>\\([^_]\\|$\\)"))
9505 ;; do, else, try, finally
9506 ((looking-at (concat "\\<\\("
9507 "do\\|else\\|try\\|finally"
9508 "\\)\\>\\([^_]\\|$\\)"))
9510 ;; for, if, while, switch, catch, synchronized, foreach
9511 (t 2))))
9513 (defun c-after-conditional (&optional lim)
9514 ;; If looking at the token after a conditional then return the
9515 ;; position of its start, otherwise return nil.
9517 ;; This function might do hidden buffer changes.
9518 (save-excursion
9519 (and (zerop (c-backward-token-2 1 t lim))
9520 (or (looking-at c-block-stmt-1-key)
9521 (and (eq (char-after) ?\()
9522 (zerop (c-backward-token-2 1 t lim))
9523 (or (looking-at c-block-stmt-2-key)
9524 (looking-at c-block-stmt-1-2-key))))
9525 (point))))
9527 (defun c-after-special-operator-id (&optional lim)
9528 ;; If the point is after an operator identifier that isn't handled
9529 ;; like an ordinary symbol (i.e. like "operator =" in C++) then the
9530 ;; position of the start of that identifier is returned. nil is
9531 ;; returned otherwise. The point may be anywhere in the syntactic
9532 ;; whitespace after the last token of the operator identifier.
9534 ;; This function might do hidden buffer changes.
9535 (save-excursion
9536 (and c-overloadable-operators-regexp
9537 (zerop (c-backward-token-2 1 nil lim))
9538 (looking-at c-overloadable-operators-regexp)
9539 (or (not c-opt-op-identifier-prefix)
9540 (and
9541 (zerop (c-backward-token-2 1 nil lim))
9542 (looking-at c-opt-op-identifier-prefix)))
9543 (point))))
9545 (defsubst c-backward-to-block-anchor (&optional lim)
9546 ;; Assuming point is at a brace that opens a statement block of some
9547 ;; kind, move to the proper anchor point for that block. It might
9548 ;; need to be adjusted further by c-add-stmt-syntax, but the
9549 ;; position at return is suitable as start position for that
9550 ;; function.
9552 ;; This function might do hidden buffer changes.
9553 (unless (= (point) (c-point 'boi))
9554 (let ((start (c-after-conditional lim)))
9555 (if start
9556 (goto-char start)))))
9558 (defsubst c-backward-to-decl-anchor (&optional lim)
9559 ;; Assuming point is at a brace that opens the block of a top level
9560 ;; declaration of some kind, move to the proper anchor point for
9561 ;; that block.
9563 ;; This function might do hidden buffer changes.
9564 (unless (= (point) (c-point 'boi))
9565 (c-beginning-of-statement-1 lim)))
9567 (defun c-search-decl-header-end ()
9568 ;; Search forward for the end of the "header" of the current
9569 ;; declaration. That's the position where the definition body
9570 ;; starts, or the first variable initializer, or the ending
9571 ;; semicolon. I.e. search forward for the closest following
9572 ;; (syntactically relevant) '{', '=' or ';' token. Point is left
9573 ;; _after_ the first found token, or at point-max if none is found.
9575 ;; This function might do hidden buffer changes.
9577 (let ((base (point)))
9578 (if (c-major-mode-is 'c++-mode)
9580 ;; In C++ we need to take special care to handle operator
9581 ;; tokens and those pesky template brackets.
9582 (while (and
9583 (c-syntactic-re-search-forward "[;{<=]" nil 'move t t)
9585 (c-end-of-current-token base)
9586 ;; Handle operator identifiers, i.e. ignore any
9587 ;; operator token preceded by "operator".
9588 (save-excursion
9589 (and (c-safe (c-backward-sexp) t)
9590 (looking-at c-opt-op-identifier-prefix)))
9591 (and (eq (char-before) ?<)
9592 (c-with-syntax-table c++-template-syntax-table
9593 (if (c-safe (goto-char (c-up-list-forward (point))))
9595 (goto-char (point-max))
9596 nil)))))
9597 (setq base (point)))
9599 (while (and
9600 (c-syntactic-re-search-forward "[;{=]" nil 'move t t)
9601 (c-end-of-current-token base))
9602 (setq base (point))))))
9604 (defun c-beginning-of-decl-1 (&optional lim)
9605 ;; Go to the beginning of the current declaration, or the beginning
9606 ;; of the previous one if already at the start of it. Point won't
9607 ;; be moved out of any surrounding paren. Return a cons cell of the
9608 ;; form (MOVE . KNR-POS). MOVE is like the return value from
9609 ;; `c-beginning-of-statement-1'. If point skipped over some K&R
9610 ;; style argument declarations (and they are to be recognized) then
9611 ;; KNR-POS is set to the start of the first such argument
9612 ;; declaration, otherwise KNR-POS is nil. If LIM is non-nil, it's a
9613 ;; position that bounds the backward search.
9615 ;; NB: Cases where the declaration continues after the block, as in
9616 ;; "struct foo { ... } bar;", are currently recognized as two
9617 ;; declarations, e.g. "struct foo { ... }" and "bar;" in this case.
9619 ;; This function might do hidden buffer changes.
9620 (catch 'return
9621 (let* ((start (point))
9622 (last-stmt-start (point))
9623 (move (c-beginning-of-statement-1 lim nil t)))
9625 ;; `c-beginning-of-statement-1' stops at a block start, but we
9626 ;; want to continue if the block doesn't begin a top level
9627 ;; construct, i.e. if it isn't preceded by ';', '}', ':', bob,
9628 ;; or an open paren.
9629 (let ((beg (point)) tentative-move)
9630 ;; Go back one "statement" each time round the loop until we're just
9631 ;; after a ;, }, or :, or at BOB or the start of a macro or start of
9632 ;; an ObjC method. This will move over a multiple declaration whose
9633 ;; components are comma separated.
9634 (while (and
9635 ;; Must check with c-opt-method-key in ObjC mode.
9636 (not (and c-opt-method-key
9637 (looking-at c-opt-method-key)))
9638 (/= last-stmt-start (point))
9639 (progn
9640 (c-backward-syntactic-ws lim)
9641 (not (or (memq (char-before) '(?\; ?} ?: nil))
9642 (c-at-vsemi-p))))
9643 (save-excursion
9644 (backward-char)
9645 (not (looking-at "\\s(")))
9646 ;; Check that we don't move from the first thing in a
9647 ;; macro to its header.
9648 (not (eq (setq tentative-move
9649 (c-beginning-of-statement-1 lim nil t))
9650 'macro)))
9651 (setq last-stmt-start beg
9652 beg (point)
9653 move tentative-move))
9654 (goto-char beg))
9656 (when c-recognize-knr-p
9657 (let ((fallback-pos (point)) knr-argdecl-start)
9658 ;; Handle K&R argdecls. Back up after the "statement" jumped
9659 ;; over by `c-beginning-of-statement-1', unless it was the
9660 ;; function body, in which case we're sitting on the opening
9661 ;; brace now. Then test if we're in a K&R argdecl region and
9662 ;; that we started at the other side of the first argdecl in
9663 ;; it.
9664 (unless (eq (char-after) ?{)
9665 (goto-char last-stmt-start))
9666 (if (and (setq knr-argdecl-start (c-in-knr-argdecl lim))
9667 (< knr-argdecl-start start)
9668 (progn
9669 (goto-char knr-argdecl-start)
9670 (not (eq (c-beginning-of-statement-1 lim nil t) 'macro))))
9671 (throw 'return
9672 (cons (if (eq (char-after fallback-pos) ?{)
9673 'previous
9674 'same)
9675 knr-argdecl-start))
9676 (goto-char fallback-pos))))
9678 ;; `c-beginning-of-statement-1' counts each brace block as a separate
9679 ;; statement, so the result will be 'previous if we've moved over any.
9680 ;; So change our result back to 'same if necessary.
9682 ;; If they were brace list initializers we might not have moved over a
9683 ;; declaration boundary though, so change it to 'same if we've moved
9684 ;; past a '=' before '{', but not ';'. (This ought to be integrated
9685 ;; into `c-beginning-of-statement-1', so we avoid this extra pass which
9686 ;; potentially can search over a large amount of text.). Take special
9687 ;; pains not to get mislead by C++'s "operator=", and the like.
9688 (if (and (eq move 'previous)
9689 (c-with-syntax-table (if (c-major-mode-is 'c++-mode)
9690 c++-template-syntax-table
9691 (syntax-table))
9692 (save-excursion
9693 (and
9694 (progn
9695 (while ; keep going back to "[;={"s until we either find
9696 ; no more, or get to one which isn't an "operator ="
9697 (and (c-syntactic-re-search-forward "[;={]" start t t t)
9698 (eq (char-before) ?=)
9699 c-overloadable-operators-regexp
9700 c-opt-op-identifier-prefix
9701 (save-excursion
9702 (eq (c-backward-token-2) 0)
9703 (looking-at c-overloadable-operators-regexp)
9704 (eq (c-backward-token-2) 0)
9705 (looking-at c-opt-op-identifier-prefix))))
9706 (eq (char-before) ?=))
9707 (c-syntactic-re-search-forward "[;{]" start t t)
9708 (eq (char-before) ?{)
9709 (c-safe (goto-char (c-up-list-forward (point))) t)
9710 (not (c-syntactic-re-search-forward ";" start t t))))))
9711 (cons 'same nil)
9712 (cons move nil)))))
9714 (defun c-end-of-decl-1 ()
9715 ;; Assuming point is at the start of a declaration (as detected by
9716 ;; e.g. `c-beginning-of-decl-1'), go to the end of it. Unlike
9717 ;; `c-beginning-of-decl-1', this function handles the case when a
9718 ;; block is followed by identifiers in e.g. struct declarations in C
9719 ;; or C++. If a proper end was found then t is returned, otherwise
9720 ;; point is moved as far as possible within the current sexp and nil
9721 ;; is returned. This function doesn't handle macros; use
9722 ;; `c-end-of-macro' instead in those cases.
9724 ;; This function might do hidden buffer changes.
9725 (let ((start (point))
9726 (decl-syntax-table (if (c-major-mode-is 'c++-mode)
9727 c++-template-syntax-table
9728 (syntax-table))))
9729 (catch 'return
9730 (c-search-decl-header-end)
9732 (when (and c-recognize-knr-p
9733 (eq (char-before) ?\;)
9734 (c-in-knr-argdecl start))
9735 ;; Stopped at the ';' in a K&R argdecl section which is
9736 ;; detected using the same criteria as in
9737 ;; `c-beginning-of-decl-1'. Move to the following block
9738 ;; start.
9739 (c-syntactic-re-search-forward "{" nil 'move t))
9741 (when (eq (char-before) ?{)
9742 ;; Encountered a block in the declaration. Jump over it.
9743 (condition-case nil
9744 (goto-char (c-up-list-forward (point)))
9745 (error (goto-char (point-max))
9746 (throw 'return nil)))
9747 (if (or (not c-opt-block-decls-with-vars-key)
9748 (save-excursion
9749 (c-with-syntax-table decl-syntax-table
9750 (let ((lim (point)))
9751 (goto-char start)
9752 (not (and
9753 ;; Check for `c-opt-block-decls-with-vars-key'
9754 ;; before the first paren.
9755 (c-syntactic-re-search-forward
9756 (concat "[;=([{]\\|\\("
9757 c-opt-block-decls-with-vars-key
9758 "\\)")
9759 lim t t t)
9760 (match-beginning 1)
9761 (not (eq (char-before) ?_))
9762 ;; Check that the first following paren is
9763 ;; the block.
9764 (c-syntactic-re-search-forward "[;=([{]"
9765 lim t t t)
9766 (eq (char-before) ?{)))))))
9767 ;; The declaration doesn't have any of the
9768 ;; `c-opt-block-decls-with-vars' keywords in the
9769 ;; beginning, so it ends here at the end of the block.
9770 (throw 'return t)))
9772 (c-with-syntax-table decl-syntax-table
9773 (while (progn
9774 (if (eq (char-before) ?\;)
9775 (throw 'return t))
9776 (c-syntactic-re-search-forward ";" nil 'move t))))
9777 nil)))
9779 (defun c-looking-at-decl-block (containing-sexp goto-start &optional limit)
9780 ;; Assuming the point is at an open brace, check if it starts a
9781 ;; block that contains another declaration level, i.e. that isn't a
9782 ;; statement block or a brace list, and if so return non-nil.
9784 ;; If the check is successful, the return value is the start of the
9785 ;; keyword that tells what kind of construct it is, i.e. typically
9786 ;; what `c-decl-block-key' matched. Also, if GOTO-START is set then
9787 ;; the point will be at the start of the construct, before any
9788 ;; leading specifiers, otherwise it's at the returned position.
9790 ;; The point is clobbered if the check is unsuccessful.
9792 ;; CONTAINING-SEXP is the position of the open of the surrounding
9793 ;; paren, or nil if none.
9795 ;; The optional LIMIT limits the backward search for the start of
9796 ;; the construct. It's assumed to be at a syntactically relevant
9797 ;; position.
9799 ;; If any template arglists are found in the searched region before
9800 ;; the open brace, they get marked with paren syntax.
9802 ;; This function might do hidden buffer changes.
9804 (let ((open-brace (point)) kwd-start first-specifier-pos)
9805 (c-syntactic-skip-backward c-block-prefix-charset limit t)
9807 (when (and c-recognize-<>-arglists
9808 (eq (char-before) ?>))
9809 ;; Could be at the end of a template arglist.
9810 (let ((c-parse-and-markup-<>-arglists t))
9811 (while (and
9812 (c-backward-<>-arglist nil limit)
9813 (progn
9814 (c-syntactic-skip-backward c-block-prefix-charset limit t)
9815 (eq (char-before) ?>))))))
9817 ;; Skip back over noise clauses.
9818 (while (and
9819 c-opt-cpp-prefix
9820 (eq (char-before) ?\))
9821 (let ((after-paren (point)))
9822 (if (and (c-go-list-backward)
9823 (progn (c-backward-syntactic-ws)
9824 (c-simple-skip-symbol-backward))
9825 (or (looking-at c-paren-nontype-key)
9826 (looking-at c-noise-macro-with-parens-name-re)))
9827 (progn
9828 (c-syntactic-skip-backward c-block-prefix-charset limit t)
9830 (goto-char after-paren)
9831 nil))))
9833 ;; Note: Can't get bogus hits inside template arglists below since they
9834 ;; have gotten paren syntax above.
9835 (when (and
9836 ;; If `goto-start' is set we begin by searching for the
9837 ;; first possible position of a leading specifier list.
9838 ;; The `c-decl-block-key' search continues from there since
9839 ;; we know it can't match earlier.
9840 (if goto-start
9841 (when (c-syntactic-re-search-forward c-symbol-start
9842 open-brace t t)
9843 (goto-char (setq first-specifier-pos (match-beginning 0)))
9847 (cond
9848 ((c-syntactic-re-search-forward c-decl-block-key open-brace t t t)
9849 (goto-char (setq kwd-start (match-beginning 0)))
9850 (and
9851 ;; Exclude cases where we matched what would ordinarily
9852 ;; be a block declaration keyword, except where it's not
9853 ;; legal because it's part of a "compound keyword" like
9854 ;; "enum class". Of course, if c-after-brace-list-key
9855 ;; is nil, we can skip the test.
9856 (or (equal c-after-brace-list-key "\\<\\>")
9857 (save-match-data
9858 (save-excursion
9859 (not
9860 (and
9861 (looking-at c-after-brace-list-key)
9862 (= (c-backward-token-2 1 t) 0)
9863 (looking-at c-brace-list-key))))))
9865 ;; Found a keyword that can't be a type?
9866 (match-beginning 1)
9868 ;; Can be a type too, in which case it's the return type of a
9869 ;; function (under the assumption that no declaration level
9870 ;; block construct starts with a type).
9871 (not (c-forward-type))
9873 ;; Jumped over a type, but it could be a declaration keyword
9874 ;; followed by the declared identifier that we've jumped over
9875 ;; instead (e.g. in "class Foo {"). If it indeed is a type
9876 ;; then we should be at the declarator now, so check for a
9877 ;; valid declarator start.
9879 ;; Note: This doesn't cope with the case when a declared
9880 ;; identifier is followed by e.g. '(' in a language where '('
9881 ;; also might be part of a declarator expression. Currently
9882 ;; there's no such language.
9883 (not (or (looking-at c-symbol-start)
9884 (looking-at c-type-decl-prefix-key))))))
9886 ;; In Pike a list of modifiers may be followed by a brace
9887 ;; to make them apply to many identifiers. Note that the
9888 ;; match data will be empty on return in this case.
9889 ((and (c-major-mode-is 'pike-mode)
9890 (progn
9891 (goto-char open-brace)
9892 (= (c-backward-token-2) 0))
9893 (looking-at c-specifier-key)
9894 ;; Use this variant to avoid yet another special regexp.
9895 (c-keyword-member (c-keyword-sym (match-string 1))
9896 'c-modifier-kwds))
9897 (setq kwd-start (point))
9898 t)))
9900 ;; Got a match.
9902 (if goto-start
9903 ;; Back up over any preceding specifiers and their clauses
9904 ;; by going forward from `first-specifier-pos', which is the
9905 ;; earliest possible position where the specifier list can
9906 ;; start.
9907 (progn
9908 (goto-char first-specifier-pos)
9910 (while (< (point) kwd-start)
9911 (if (looking-at c-symbol-key)
9912 ;; Accept any plain symbol token on the ground that
9913 ;; it's a specifier masked through a macro (just
9914 ;; like `c-forward-decl-or-cast-1' skip forward over
9915 ;; such tokens).
9917 ;; Could be more restrictive wrt invalid keywords,
9918 ;; but that'd only occur in invalid code so there's
9919 ;; no use spending effort on it.
9920 (let ((end (match-end 0)))
9921 (unless (c-forward-keyword-clause 0)
9922 (goto-char end)
9923 (c-forward-syntactic-ws)))
9925 ;; Can't parse a declaration preamble and is still
9926 ;; before `kwd-start'. That means `first-specifier-pos'
9927 ;; was in some earlier construct. Search again.
9928 (if (c-syntactic-re-search-forward c-symbol-start
9929 kwd-start 'move t)
9930 (goto-char (setq first-specifier-pos (match-beginning 0)))
9931 ;; Got no preamble before the block declaration keyword.
9932 (setq first-specifier-pos kwd-start))))
9934 (goto-char first-specifier-pos))
9935 (goto-char kwd-start))
9937 kwd-start)))
9939 (defun c-directly-in-class-called-p (name)
9940 ;; Check whether point is directly inside a brace block which is the brace
9941 ;; block of a class, struct, or union which is called NAME, a string.
9942 (let* ((paren-state (c-parse-state))
9943 (brace-pos (c-pull-open-brace paren-state))
9945 (when (eq (char-after brace-pos) ?{)
9946 (goto-char brace-pos)
9947 (save-excursion
9948 ; *c-looking-at-decl-block
9949 ; containing-sexp goto-start &optional
9950 ; limit)
9951 (when (and (c-looking-at-decl-block
9952 (c-pull-open-brace paren-state)
9953 nil)
9954 (looking-at c-class-key))
9955 (goto-char (match-end 1))
9956 (c-forward-syntactic-ws)
9957 (looking-at name))))))
9959 (defun c-search-uplist-for-classkey (paren-state)
9960 ;; Check if the closest containing paren sexp is a declaration
9961 ;; block, returning a 2 element vector in that case. Aref 0
9962 ;; contains the bufpos at boi of the class key line, and aref 1
9963 ;; contains the bufpos of the open brace. This function is an
9964 ;; obsolete wrapper for `c-looking-at-decl-block'.
9966 ;; This function might do hidden buffer changes.
9967 (let ((open-paren-pos (c-most-enclosing-brace paren-state)))
9968 (when open-paren-pos
9969 (save-excursion
9970 (goto-char open-paren-pos)
9971 (when (and (eq (char-after) ?{)
9972 (c-looking-at-decl-block
9973 (c-safe-position open-paren-pos paren-state)
9974 nil))
9975 (back-to-indentation)
9976 (vector (point) open-paren-pos))))))
9978 (defun c-most-enclosing-decl-block (paren-state)
9979 ;; Return the buffer position of the most enclosing decl-block brace (in the
9980 ;; sense of c-looking-at-decl-block) in the PAREN-STATE structure, or nil if
9981 ;; none was found.
9982 (let* ((open-brace (c-pull-open-brace paren-state))
9983 (next-open-brace (c-pull-open-brace paren-state)))
9984 (while (and open-brace
9985 (save-excursion
9986 (goto-char open-brace)
9987 (not (c-looking-at-decl-block next-open-brace nil))))
9988 (setq open-brace next-open-brace
9989 next-open-brace (c-pull-open-brace paren-state)))
9990 open-brace))
9992 (defun c-cheap-inside-bracelist-p (paren-state)
9993 ;; Return the position of the L-brace if point is inside a brace list
9994 ;; initialization of an array, etc. This is an approximate function,
9995 ;; designed for speed over accuracy. It will not find every bracelist, but
9996 ;; a non-nil result is reliable. We simply search for "= {" (naturally with
9997 ;; syntactic whitespace allowed). PAREN-STATE is the normal thing that it
9998 ;; is everywhere else.
9999 (let (b-pos)
10000 (save-excursion
10001 (while
10002 (and (setq b-pos (c-pull-open-brace paren-state))
10003 (progn (goto-char b-pos)
10004 (c-backward-sws)
10005 (c-backward-token-2)
10006 (not (looking-at "=")))))
10007 b-pos)))
10009 (defun c-backward-typed-enum-colon ()
10010 ;; We're at a "{" which might be the opening brace of a enum which is
10011 ;; strongly typed (by a ":" followed by a type). If this is the case, leave
10012 ;; point before the colon and return t. Otherwise leave point unchanged and return nil.
10013 ;; Match data will be clobbered.
10014 (let ((here (point))
10015 (colon-pos nil))
10016 (save-excursion
10017 (while
10018 (and (eql (c-backward-token-2) 0)
10019 (or (not (looking-at "\\s)"))
10020 (c-go-up-list-backward))
10021 (cond
10022 ((and (eql (char-after) ?:)
10023 (save-excursion
10024 (c-backward-syntactic-ws)
10025 (or (c-on-identifier)
10026 (progn
10027 (c-backward-token-2)
10028 (looking-at c-brace-list-key)))))
10029 (setq colon-pos (point))
10030 (forward-char)
10031 (c-forward-syntactic-ws)
10032 (or (and (c-forward-type)
10033 (progn (c-forward-syntactic-ws)
10034 (eq (point) here)))
10035 (setq colon-pos nil))
10036 nil)
10037 ((eql (char-after) ?\()
10039 ((looking-at c-symbol-key)
10041 (t nil)))))
10042 (when colon-pos
10043 (goto-char colon-pos)
10044 t)))
10046 (defun c-backward-over-enum-header ()
10047 ;; We're at a "{". Move back to the enum-like keyword that starts this
10048 ;; declaration and return t, otherwise don't move and return nil.
10049 (let ((here (point))
10050 up-sexp-pos before-identifier)
10051 (when c-recognize-post-brace-list-type-p
10052 (c-backward-typed-enum-colon))
10053 (while
10054 (and
10055 (eq (c-backward-token-2) 0)
10056 (or (not (looking-at "\\s)"))
10057 (c-go-up-list-backward))
10058 (cond
10059 ((and (looking-at c-symbol-key) (c-on-identifier)
10060 (not before-identifier))
10061 (setq before-identifier t))
10062 ((and before-identifier
10063 (or (eql (char-after) ?,)
10064 (looking-at c-postfix-decl-spec-key)))
10065 (setq before-identifier nil)
10067 ((looking-at c-after-brace-list-key) t)
10068 ((looking-at c-brace-list-key) nil)
10069 ((eq (char-after) ?\()
10070 (and (eq (c-backward-token-2) 0)
10071 (or (looking-at c-decl-hangon-key)
10072 (and c-opt-cpp-prefix
10073 (looking-at c-noise-macro-with-parens-name-re)))))
10075 ((and c-recognize-<>-arglists
10076 (eq (char-after) ?<)
10077 (looking-at "\\s("))
10079 (t nil))))
10080 (or (looking-at c-brace-list-key)
10081 (progn (goto-char here) nil))))
10083 (defun c-looking-at-or-maybe-in-bracelist (&optional containing-sexp lim)
10084 ;; Point is at an open brace. If this starts a brace list, return a list
10085 ;; whose car is the buffer position of the start of the construct which
10086 ;; introduces the list, and whose cdr is t if we have parsed a keyword
10087 ;; matching `c-opt-inexpr-brace-list-key' (e.g. Java's "new"), nil
10088 ;; otherwise. Otherwise, if point might be inside an enclosing brace list,
10089 ;; return t. If point is definitely neither at nor in a brace list, return
10090 ;; nil.
10092 ;; CONTAINING-SEXP is the position of the brace/paren/bracket enclosing
10093 ;; POINT, or nil if there is no such position, or we do not know it. LIM is
10094 ;; a backward search limit.
10096 ;; Here, "brace list" does not include the body of an enum.
10097 (save-excursion
10098 (let ((start (point))
10099 (class-key
10100 ;; Pike can have class definitions anywhere, so we must
10101 ;; check for the class key here.
10102 (and (c-major-mode-is 'pike-mode)
10103 c-decl-block-key))
10104 (braceassignp 'dontknow)
10105 inexpr-brace-list bufpos macro-start res pos after-type-id-pos)
10107 (setq res (c-backward-token-2 1 t lim))
10108 ;; Checks to do only on the first sexp before the brace.
10109 ;; Have we a C++ initialization, without an "="?
10110 (if (and (c-major-mode-is 'c++-mode)
10111 (cond
10112 ((and (not (eq res 0))
10113 (c-go-up-list-backward nil lim) ; FIXME!!! Check ; `lim' 2016-07-12.
10114 (eq (char-after) ?\())
10115 (setq braceassignp 'c++-noassign))
10116 ((looking-at c-pre-id-bracelist-key))
10117 ((looking-at c-return-key))
10118 ((and (looking-at c-symbol-start)
10119 (not (looking-at c-keywords-regexp)))
10120 (setq after-type-id-pos (point)))
10121 (t nil))
10122 (save-excursion
10123 (cond
10124 ((not (eq res 0))
10125 (and (c-go-up-list-backward nil lim) ; FIXME!!! Check `lim' 2016-07-12.
10126 (eq (char-after) ?\()))
10127 ((looking-at c-pre-id-bracelist-key))
10128 ((looking-at c-return-key))
10129 (t (setq after-type-id-pos (point))
10130 nil))))
10131 (setq braceassignp 'c++-noassign))
10133 (when (and c-opt-inexpr-brace-list-key
10134 (eq (char-after) ?\[))
10135 ;; In Java, an initialization brace list may follow
10136 ;; directly after "new Foo[]", so check for a "new"
10137 ;; earlier.
10138 (while (eq braceassignp 'dontknow)
10139 (setq braceassignp
10140 (cond ((/= (c-backward-token-2 1 t lim) 0) nil)
10141 ((looking-at c-opt-inexpr-brace-list-key)
10142 (setq inexpr-brace-list t)
10144 ((looking-at "\\sw\\|\\s_\\|[.[]")
10145 ;; Carry on looking if this is an
10146 ;; identifier (may contain "." in Java)
10147 ;; or another "[]" sexp.
10148 'dontknow)
10149 (t nil)))))
10151 (setq pos (point))
10152 (if (and after-type-id-pos
10153 (goto-char after-type-id-pos)
10154 (setq res (c-back-over-member-initializers))
10155 (goto-char res)
10156 (eq (car (c-beginning-of-decl-1 lim)) 'same))
10157 (cons (point) nil) ; Return value.
10159 (goto-char pos)
10160 ;; Checks to do on all sexps before the brace, up to the
10161 ;; beginning of the statement.
10162 (while (eq braceassignp 'dontknow)
10163 (cond ((eq (char-after) ?\;)
10164 (setq braceassignp nil))
10165 ((and class-key
10166 (looking-at class-key))
10167 (setq braceassignp nil))
10168 ((eq (char-after) ?=)
10169 ;; We've seen a =, but must check earlier tokens so
10170 ;; that it isn't something that should be ignored.
10171 (setq braceassignp 'maybe)
10172 (while (and (eq braceassignp 'maybe)
10173 (zerop (c-backward-token-2 1 t lim)))
10174 (setq braceassignp
10175 (cond
10176 ;; Check for operator =
10177 ((and c-opt-op-identifier-prefix
10178 (looking-at c-opt-op-identifier-prefix))
10179 nil)
10180 ;; Check for `<opchar>= in Pike.
10181 ((and (c-major-mode-is 'pike-mode)
10182 (or (eq (char-after) ?`)
10183 ;; Special case for Pikes
10184 ;; `[]=, since '[' is not in
10185 ;; the punctuation class.
10186 (and (eq (char-after) ?\[)
10187 (eq (char-before) ?`))))
10188 nil)
10189 ((looking-at "\\s.") 'maybe)
10190 ;; make sure we're not in a C++ template
10191 ;; argument assignment
10192 ((and
10193 (c-major-mode-is 'c++-mode)
10194 (save-excursion
10195 (let ((here (point))
10196 (pos< (progn
10197 (skip-chars-backward "^<>")
10198 (point))))
10199 (and (eq (char-before) ?<)
10200 (not (c-crosses-statement-barrier-p
10201 pos< here))
10202 (not (c-in-literal))
10203 ))))
10204 nil)
10205 (t t))))))
10206 (if (and (eq braceassignp 'dontknow)
10207 (/= (c-backward-token-2 1 t lim) 0))
10208 (setq braceassignp nil)))
10210 (cond
10211 (braceassignp
10212 ;; We've hit the beginning of the aggregate list.
10213 (c-beginning-of-statement-1 containing-sexp)
10214 (cons (point) inexpr-brace-list))
10215 ((and after-type-id-pos
10216 (save-excursion
10217 (when (eq (char-after) ?\;)
10218 (c-forward-token-2 1 t))
10219 (setq bufpos (point))
10220 (when (looking-at c-opt-<>-sexp-key)
10221 (c-forward-token-2)
10222 (when (and (eq (char-after) ?<)
10223 (c-get-char-property (point) 'syntax-table))
10224 (c-go-list-forward nil after-type-id-pos)
10225 (c-forward-syntactic-ws)))
10226 (and
10227 (or (not (looking-at c-class-key))
10228 (save-excursion
10229 (goto-char (match-end 1))
10230 (c-forward-syntactic-ws)
10231 (not (eq (point) after-type-id-pos))))
10232 (progn
10233 (setq res
10234 (c-forward-decl-or-cast-1
10235 (save-excursion (c-backward-syntactic-ws) (point))
10236 nil nil))
10237 (and (consp res)
10238 (eq (car res) after-type-id-pos))))))
10239 (cons bufpos inexpr-brace-list))
10240 ((eq (char-after) ?\;)
10241 ;; Brace lists can't contain a semicolon, so we're done.
10242 ;; (setq containing-sexp nil)
10243 nil)
10244 ((and (setq macro-start (point))
10245 (c-forward-to-cpp-define-body)
10246 (eq (point) start))
10247 ;; We've a macro whose expansion starts with the '{'.
10248 ;; Heuristically, if we have a ';' in it we've not got a
10249 ;; brace list, otherwise we have.
10250 (let ((macro-end (progn (c-end-of-macro) (point))))
10251 (goto-char start)
10252 (forward-char)
10253 (if (and (c-syntactic-re-search-forward "[;,]" macro-end t t)
10254 (eq (char-before) ?\;))
10256 (cons macro-start nil)))) ; (2016-08-30): Lazy! We have no
10257 ; languages where
10258 ; `c-opt-inexpr-brace-list-key' is
10259 ; non-nil and we have macros.
10260 (t t))) ;; The caller can go up one level.
10263 (defun c-inside-bracelist-p (containing-sexp paren-state)
10264 ;; return the buffer position of the beginning of the brace list
10265 ;; statement if we're inside a brace list, otherwise return nil.
10266 ;; CONTAINING-SEXP is the buffer pos of the innermost containing
10267 ;; paren. PAREN-STATE is the remainder of the state of enclosing
10268 ;; braces
10270 ;; N.B.: This algorithm can potentially get confused by cpp macros
10271 ;; placed in inconvenient locations. It's a trade-off we make for
10272 ;; speed.
10274 ;; This function might do hidden buffer changes.
10276 ;; This will pick up brace list declarations.
10277 (save-excursion
10278 (goto-char containing-sexp)
10279 (c-backward-over-enum-header))
10280 ;; this will pick up array/aggregate init lists, even if they are nested.
10281 (save-excursion
10282 (let ((bufpos t)
10283 lim next-containing)
10284 (while (and (eq bufpos t)
10285 containing-sexp)
10286 (when paren-state
10287 (if (consp (car paren-state))
10288 (setq lim (cdr (car paren-state))
10289 paren-state (cdr paren-state))
10290 (setq lim (car paren-state)))
10291 (when paren-state
10292 (setq next-containing (car paren-state)
10293 paren-state (cdr paren-state))))
10295 (goto-char containing-sexp)
10296 (if (c-looking-at-inexpr-block next-containing next-containing)
10297 ;; We're in an in-expression block of some kind. Do not
10298 ;; check nesting. We deliberately set the limit to the
10299 ;; containing sexp, so that c-looking-at-inexpr-block
10300 ;; doesn't check for an identifier before it.
10301 (setq bufpos nil)
10302 (when (or (not (eq (char-after) ?{))
10303 (eq (setq bufpos (c-looking-at-or-maybe-in-bracelist
10304 next-containing lim))
10306 (setq containing-sexp next-containing
10307 lim nil
10308 next-containing nil))))
10309 (and (consp bufpos) (car bufpos))))))
10311 (defun c-looking-at-special-brace-list (&optional lim)
10312 ;; If we're looking at the start of a pike-style list, i.e., `({ })',
10313 ;; `([ ])', `(< >)', etc., a cons of a cons of its starting and ending
10314 ;; positions and its entry in c-special-brace-lists is returned, nil
10315 ;; otherwise. The ending position is nil if the list is still open.
10316 ;; LIM is the limit for forward search. The point may either be at
10317 ;; the `(' or at the following paren character. Tries to check the
10318 ;; matching closer, but assumes it's correct if no balanced paren is
10319 ;; found (i.e. the case `({ ... } ... )' is detected as _not_ being
10320 ;; a special brace list).
10322 ;; This function might do hidden buffer changes.
10323 (if c-special-brace-lists
10324 (condition-case ()
10325 (save-excursion
10326 (let ((beg (point))
10327 inner-beg end type)
10328 (c-forward-syntactic-ws)
10329 (if (eq (char-after) ?\()
10330 (progn
10331 (forward-char 1)
10332 (c-forward-syntactic-ws)
10333 (setq inner-beg (point))
10334 (setq type (assq (char-after) c-special-brace-lists)))
10335 (if (setq type (assq (char-after) c-special-brace-lists))
10336 (progn
10337 (setq inner-beg (point))
10338 (c-backward-syntactic-ws)
10339 (forward-char -1)
10340 (setq beg (if (eq (char-after) ?\()
10341 (point)
10342 nil)))))
10343 (if (and beg type)
10344 (if (and (c-safe
10345 (goto-char beg)
10346 (c-forward-sexp 1)
10347 (setq end (point))
10348 (= (char-before) ?\)))
10349 (c-safe
10350 (goto-char inner-beg)
10351 (if (looking-at "\\s(")
10352 ;; Check balancing of the inner paren
10353 ;; below.
10354 (progn
10355 (c-forward-sexp 1)
10357 ;; If the inner char isn't a paren then
10358 ;; we can't check balancing, so just
10359 ;; check the char before the outer
10360 ;; closing paren.
10361 (goto-char end)
10362 (backward-char)
10363 (c-backward-syntactic-ws)
10364 (= (char-before) (cdr type)))))
10365 (if (or (/= (char-syntax (char-before)) ?\))
10366 (= (progn
10367 (c-forward-syntactic-ws)
10368 (point))
10369 (1- end)))
10370 (cons (cons beg end) type))
10371 (cons (list beg) type)))))
10372 (error nil))))
10374 (defun c-looking-at-bos (&optional lim)
10375 ;; Return non-nil if between two statements or declarations, assuming
10376 ;; point is not inside a literal or comment.
10378 ;; Obsolete - `c-at-statement-start-p' or `c-at-expression-start-p'
10379 ;; are recommended instead.
10381 ;; This function might do hidden buffer changes.
10382 (c-at-statement-start-p))
10383 (make-obsolete 'c-looking-at-bos 'c-at-statement-start-p "22.1")
10385 (defun c-looking-at-inexpr-block (lim containing-sexp &optional check-at-end)
10386 ;; Return non-nil if we're looking at the beginning of a block
10387 ;; inside an expression. The value returned is actually a cons of
10388 ;; either 'inlambda, 'inexpr-statement or 'inexpr-class and the
10389 ;; position of the beginning of the construct.
10391 ;; LIM limits the backward search. CONTAINING-SEXP is the start
10392 ;; position of the closest containing list. If it's nil, the
10393 ;; containing paren isn't used to decide whether we're inside an
10394 ;; expression or not. If both LIM and CONTAINING-SEXP are used, LIM
10395 ;; needs to be farther back.
10397 ;; If CHECK-AT-END is non-nil then extra checks at the end of the
10398 ;; brace block might be done. It should only be used when the
10399 ;; construct can be assumed to be complete, i.e. when the original
10400 ;; starting position was further down than that.
10402 ;; This function might do hidden buffer changes.
10404 (save-excursion
10405 (let ((res 'maybe) (passed-bracket-pairs 0) bracket-pos passed-paren
10406 haskell-op-pos
10407 (closest-lim (or containing-sexp lim (point-min)))
10408 ;; Look at the character after point only as a last resort
10409 ;; when we can't disambiguate.
10410 (block-follows (and (eq (char-after) ?{) (point))))
10412 ;; Search for a C++11 "->" which suggests a lambda declaration.
10413 (when (and (c-major-mode-is 'c++-mode)
10414 (setq haskell-op-pos
10415 (save-excursion
10416 (while
10417 (progn
10418 (c-syntactic-skip-backward "^;=}>" closest-lim t)
10419 (and (eq (char-before) ?>)
10420 (c-backward-token-2)
10421 (not (looking-at c-haskell-op-re)))))
10422 (and (looking-at c-haskell-op-re)
10423 (point)))))
10424 (goto-char haskell-op-pos))
10426 (while (and (eq res 'maybe)
10427 (progn (c-backward-syntactic-ws)
10428 (> (point) closest-lim))
10429 (not (bobp))
10430 (progn (backward-char)
10431 (looking-at "[]).]\\|\\w\\|\\s_"))
10432 (c-safe (forward-char)
10433 (goto-char (scan-sexps (point) -1))))
10435 (setq res
10436 (if (looking-at c-keywords-regexp)
10437 (let ((kw-sym (c-keyword-sym (match-string 1))))
10438 (cond
10439 ((and block-follows
10440 (c-keyword-member kw-sym 'c-inexpr-class-kwds))
10441 (and (not (eq passed-paren ?\[))
10442 (or (not (looking-at c-class-key))
10443 ;; If the class definition is at the start of
10444 ;; a statement, we don't consider it an
10445 ;; in-expression class.
10446 (let ((prev (point)))
10447 (while (and
10448 (= (c-backward-token-2 1 nil closest-lim) 0)
10449 (eq (char-syntax (char-after)) ?w))
10450 (setq prev (point)))
10451 (goto-char prev)
10452 (not (c-at-statement-start-p)))
10453 ;; Also, in Pike we treat it as an
10454 ;; in-expression class if it's used in an
10455 ;; object clone expression.
10456 (save-excursion
10457 (and check-at-end
10458 (c-major-mode-is 'pike-mode)
10459 (progn (goto-char block-follows)
10460 (zerop (c-forward-token-2 1 t)))
10461 (eq (char-after) ?\())))
10462 (cons 'inexpr-class (point))))
10463 ((c-keyword-member kw-sym 'c-paren-any-kwds) ; e.g. C++11 "throw" or "noexcept"
10464 (setq passed-paren nil)
10465 (setq passed-bracket-pairs 0)
10466 (setq bracket-pos nil)
10467 'maybe)
10468 ((c-keyword-member kw-sym 'c-inexpr-block-kwds)
10469 (when (not passed-paren)
10470 (cons 'inexpr-statement (point))))
10471 ((c-keyword-member kw-sym 'c-lambda-kwds)
10472 (when (or (not passed-paren)
10473 (eq passed-paren ?\())
10474 (cons 'inlambda (point))))
10475 ((c-keyword-member kw-sym 'c-block-stmt-kwds)
10476 nil)
10478 'maybe)))
10480 (if (looking-at "\\s(")
10481 (if passed-paren
10482 (cond
10483 ((and (eq passed-paren ?\[)
10484 (eq (char-after) ?\[)
10485 (not (eq (char-after (1+ (point))) ?\[))) ; C++ attribute.
10486 ;; Accept several square bracket sexps for
10487 ;; Java array initializations.
10488 (setq passed-bracket-pairs (1+ passed-bracket-pairs))
10489 'maybe)
10490 ((and (eq passed-paren ?\()
10491 (eq (char-after) ?\[)
10492 (not (eq (char-after (1+ (point))) ?\[))
10493 (eq passed-bracket-pairs 0))
10494 ;; C++11 lambda function declaration
10495 (setq passed-bracket-pairs 1)
10496 (setq bracket-pos (point))
10497 'maybe)
10498 (t nil))
10499 (when (not (looking-at "\\[\\["))
10500 (setq passed-paren (char-after))
10501 (when (eq passed-paren ?\[)
10502 (setq passed-bracket-pairs 1)
10503 (setq bracket-pos (point))))
10504 'maybe)
10505 'maybe))))
10507 (if (eq res 'maybe)
10508 (cond
10509 ((and (c-major-mode-is 'c++-mode)
10510 block-follows
10511 (eq passed-bracket-pairs 1)
10512 (save-excursion
10513 (goto-char bracket-pos)
10514 (or (<= (point) (or lim (point-min)))
10515 (progn
10516 (c-backward-token-2 1 nil lim)
10517 (and
10518 (not (c-on-identifier))
10519 (not (looking-at c-opt-op-identifier-prefix)))))))
10520 (cons 'inlambda bracket-pos))
10521 ((and c-recognize-paren-inexpr-blocks
10522 block-follows
10523 containing-sexp
10524 (eq (char-after containing-sexp) ?\())
10525 (goto-char containing-sexp)
10526 (if (or (save-excursion
10527 (c-backward-syntactic-ws lim)
10528 (while (and (eq (char-before) ?>)
10529 (c-get-char-property (1- (point))
10530 'syntax-table)
10531 (c-go-list-backward nil lim))
10532 (c-backward-syntactic-ws lim))
10533 (and (> (point) (or lim (point-min)))
10534 (c-on-identifier)))
10535 (and c-special-brace-lists
10536 (c-looking-at-special-brace-list))
10537 (and (c-major-mode-is 'c++-mode)
10538 (save-excursion
10539 (goto-char block-follows)
10540 (if (c-go-list-forward)
10541 (progn
10542 (backward-char)
10543 (c-syntactic-skip-backward
10544 "^;," block-follows t)
10545 (not (eq (char-before) ?\;)))
10546 (or (not (c-syntactic-re-search-forward
10547 "[;,]" nil t t))
10548 (not (eq (char-before) ?\;)))))))
10550 (cons 'inexpr-statement (point)))))
10552 res))))
10554 (defun c-looking-at-inexpr-block-backward (paren-state)
10555 ;; Returns non-nil if we're looking at the end of an in-expression
10556 ;; block, otherwise the same as `c-looking-at-inexpr-block'.
10557 ;; PAREN-STATE is the paren state relevant at the current position.
10559 ;; This function might do hidden buffer changes.
10560 (save-excursion
10561 ;; We currently only recognize a block.
10562 (let ((here (point))
10563 (elem (car-safe paren-state))
10564 containing-sexp)
10565 (when (and (consp elem)
10566 (progn (goto-char (cdr elem))
10567 (c-forward-syntactic-ws here)
10568 (= (point) here)))
10569 (goto-char (car elem))
10570 (if (setq paren-state (cdr paren-state))
10571 (setq containing-sexp (car-safe paren-state)))
10572 (c-looking-at-inexpr-block (c-safe-position containing-sexp
10573 paren-state)
10574 containing-sexp)))))
10576 (defun c-looking-at-c++-lambda-capture-list ()
10577 ;; Return non-nil if we're at the opening "[" of the capture list of a C++
10578 ;; lambda function, nil otherwise.
10579 (and
10580 (eq (char-after) ?\[)
10581 (not (eq (char-before) ?\[))
10582 (not (eq (char-after (1+ (point))) ?\[))
10583 (save-excursion
10584 (or (eq (c-backward-token-2 1) 1)
10585 (looking-at c-pre-lambda-tokens-re)))
10586 (not (c-in-literal))))
10588 (defun c-at-macro-vsemi-p (&optional pos)
10589 ;; Is there a "virtual semicolon" at POS or point?
10590 ;; (See cc-defs.el for full details of "virtual semicolons".)
10592 ;; This is true when point is at the last non syntactic WS position on the
10593 ;; line, there is a macro call last on the line, and this particular macro's
10594 ;; name is defined by the regexp `c-vs-macro-regexp' as not needing a
10595 ;; semicolon.
10596 (save-excursion
10597 (save-restriction
10598 (widen)
10599 (if pos
10600 (goto-char pos)
10601 (setq pos (point)))
10602 (and
10603 c-macro-with-semi-re
10604 (eq (skip-chars-backward " \t") 0)
10606 ;; Check we've got nothing after this except comments and empty lines
10607 ;; joined by escaped EOLs.
10608 (skip-chars-forward " \t") ; always returns non-nil.
10609 (progn
10610 (while ; go over 1 block comment per iteration.
10611 (and
10612 (looking-at "\\(\\\\[\n\r][ \t]*\\)*")
10613 (goto-char (match-end 0))
10614 (cond
10615 ((looking-at c-block-comment-start-regexp)
10616 (and (forward-comment 1)
10617 (skip-chars-forward " \t"))) ; always returns non-nil
10618 ((looking-at c-line-comment-start-regexp)
10619 (end-of-line)
10620 nil)
10621 (t nil))))
10622 (eolp))
10624 (goto-char pos)
10625 (progn (c-backward-syntactic-ws)
10626 (eq (point) pos))
10628 ;; Check for one of the listed macros being before point.
10629 (or (not (eq (char-before) ?\)))
10630 (when (c-go-list-backward)
10631 (c-backward-syntactic-ws)
10633 (c-simple-skip-symbol-backward)
10634 (looking-at c-macro-with-semi-re)
10635 (goto-char pos)
10636 (not (c-in-literal)))))) ; The most expensive check last.
10638 (defun c-macro-vsemi-status-unknown-p () t) ; See cc-defs.el.
10641 ;; `c-guess-basic-syntax' and the functions that precedes it below
10642 ;; implements the main decision tree for determining the syntactic
10643 ;; analysis of the current line of code.
10645 ;; Dynamically bound to t when `c-guess-basic-syntax' is called during
10646 ;; auto newline analysis.
10647 (defvar c-auto-newline-analysis nil)
10649 (defun c-brace-anchor-point (bracepos)
10650 ;; BRACEPOS is the position of a brace in a construct like "namespace
10651 ;; Bar {". Return the anchor point in this construct; this is the
10652 ;; earliest symbol on the brace's line which isn't earlier than
10653 ;; "namespace".
10655 ;; Currently (2007-08-17), "like namespace" means "matches
10656 ;; c-other-block-decl-kwds". It doesn't work with "class" or "struct"
10657 ;; or anything like that.
10658 (save-excursion
10659 (let ((boi (c-point 'boi bracepos)))
10660 (goto-char bracepos)
10661 (while (and (> (point) boi)
10662 (not (looking-at c-other-decl-block-key)))
10663 (c-backward-token-2))
10664 (if (> (point) boi) (point) boi))))
10666 (defsubst c-add-syntax (symbol &rest args)
10667 ;; A simple function to prepend a new syntax element to
10668 ;; `c-syntactic-context'. Using `setq' on it is unsafe since it
10669 ;; should always be dynamically bound but since we read it first
10670 ;; we'll fail properly anyway if this function is misused.
10671 (setq c-syntactic-context (cons (cons symbol args)
10672 c-syntactic-context)))
10674 (defsubst c-append-syntax (symbol &rest args)
10675 ;; Like `c-add-syntax' but appends to the end of the syntax list.
10676 ;; (Normally not necessary.)
10677 (setq c-syntactic-context (nconc c-syntactic-context
10678 (list (cons symbol args)))))
10680 (defun c-add-stmt-syntax (syntax-symbol
10681 syntax-extra-args
10682 stop-at-boi-only
10683 containing-sexp
10684 paren-state)
10685 ;; Add the indicated SYNTAX-SYMBOL to `c-syntactic-context', extending it as
10686 ;; needed with further syntax elements of the types `substatement',
10687 ;; `inexpr-statement', `arglist-cont-nonempty', `statement-block-intro', and
10688 ;; `defun-block-intro'.
10690 ;; Do the generic processing to anchor the given syntax symbol on
10691 ;; the preceding statement: Skip over any labels and containing
10692 ;; statements on the same line, and then search backward until we
10693 ;; find a statement or block start that begins at boi without a
10694 ;; label or comment.
10696 ;; Point is assumed to be at the prospective anchor point for the
10697 ;; given SYNTAX-SYMBOL. More syntax entries are added if we need to
10698 ;; skip past open parens and containing statements. Most of the added
10699 ;; syntax elements will get the same anchor point - the exception is
10700 ;; for an anchor in a construct like "namespace"[*] - this is as early
10701 ;; as possible in the construct but on the same line as the {.
10703 ;; [*] i.e. with a keyword matching c-other-block-decl-kwds.
10705 ;; SYNTAX-EXTRA-ARGS are a list of the extra arguments for the
10706 ;; syntax symbol. They are appended after the anchor point.
10708 ;; If STOP-AT-BOI-ONLY is nil, we can stop in the middle of the line
10709 ;; if the current statement starts there.
10711 ;; Note: It's not a problem if PAREN-STATE "overshoots"
10712 ;; CONTAINING-SEXP, i.e. contains info about parens further down.
10714 ;; This function might do hidden buffer changes.
10716 (if (= (point) (c-point 'boi))
10717 ;; This is by far the most common case, so let's give it special
10718 ;; treatment.
10719 (apply 'c-add-syntax syntax-symbol (point) syntax-extra-args)
10721 (let ((syntax-last c-syntactic-context)
10722 (boi (c-point 'boi))
10723 ;; Set when we're on a label, so that we don't stop there.
10724 ;; FIXME: To be complete we should check if we're on a label
10725 ;; now at the start.
10726 on-label)
10728 ;; Use point as the anchor point for "namespace", "extern", etc.
10729 (apply 'c-add-syntax syntax-symbol
10730 (if (rassq syntax-symbol c-other-decl-block-key-in-symbols-alist)
10731 (point) nil)
10732 syntax-extra-args)
10734 ;; Loop while we have to back out of containing blocks.
10735 (while
10736 (and
10737 (catch 'back-up-block
10739 ;; Loop while we have to back up statements.
10740 (while (or (/= (point) boi)
10741 on-label
10742 (looking-at c-comment-start-regexp))
10744 ;; Skip past any comments that stands between the
10745 ;; statement start and boi.
10746 (let ((savepos (point)))
10747 (while (and (/= savepos boi)
10748 (c-backward-single-comment))
10749 (setq savepos (point)
10750 boi (c-point 'boi)))
10751 (goto-char savepos))
10753 ;; Skip to the beginning of this statement or backward
10754 ;; another one.
10755 (let ((old-pos (point))
10756 (old-boi boi)
10757 (step-type (c-beginning-of-statement-1 containing-sexp)))
10758 (setq boi (c-point 'boi)
10759 on-label (eq step-type 'label))
10761 (cond ((= (point) old-pos)
10762 ;; If we didn't move we're at the start of a block and
10763 ;; have to continue outside it.
10764 (throw 'back-up-block t))
10766 ((and (eq step-type 'up)
10767 (>= (point) old-boi)
10768 (looking-at "else\\>[^_]")
10769 (save-excursion
10770 (goto-char old-pos)
10771 (looking-at "if\\>[^_]")))
10772 ;; Special case to avoid deeper and deeper indentation
10773 ;; of "else if" clauses.
10776 ((and (not stop-at-boi-only)
10777 (/= old-pos old-boi)
10778 (memq step-type '(up previous)))
10779 ;; If stop-at-boi-only is nil, we shouldn't back up
10780 ;; over previous or containing statements to try to
10781 ;; reach boi, so go back to the last position and
10782 ;; exit.
10783 (goto-char old-pos)
10784 (throw 'back-up-block nil))
10787 (if (and (not stop-at-boi-only)
10788 (memq step-type '(up previous beginning)))
10789 ;; If we've moved into another statement then we
10790 ;; should no longer try to stop in the middle of a
10791 ;; line.
10792 (setq stop-at-boi-only t))
10794 ;; Record this as a substatement if we skipped up one
10795 ;; level.
10796 (when (eq step-type 'up)
10797 (c-add-syntax 'substatement nil))))
10800 containing-sexp)
10802 ;; Now we have to go out of this block.
10803 (goto-char containing-sexp)
10805 ;; Don't stop in the middle of a special brace list opener
10806 ;; like "({".
10807 (when c-special-brace-lists
10808 (let ((special-list (c-looking-at-special-brace-list)))
10809 (when (and special-list
10810 (< (car (car special-list)) (point)))
10811 (setq containing-sexp (car (car special-list)))
10812 (goto-char containing-sexp))))
10814 (setq paren-state (c-whack-state-after containing-sexp paren-state)
10815 containing-sexp (c-most-enclosing-brace paren-state)
10816 boi (c-point 'boi))
10818 ;; Analyze the construct in front of the block we've stepped out
10819 ;; from and add the right syntactic element for it.
10820 (let ((paren-pos (point))
10821 (paren-char (char-after))
10822 step-type)
10824 (if (eq paren-char ?\()
10825 ;; Stepped out of a parenthesis block, so we're in an
10826 ;; expression now.
10827 (progn
10828 (when (/= paren-pos boi)
10829 (if (and c-recognize-paren-inexpr-blocks
10830 (progn
10831 (c-backward-syntactic-ws containing-sexp)
10832 (or (not (looking-at "\\>"))
10833 (not (c-on-identifier))))
10834 (save-excursion
10835 (goto-char (1+ paren-pos))
10836 (c-forward-syntactic-ws)
10837 (eq (char-after) ?{)))
10838 ;; Stepped out of an in-expression statement. This
10839 ;; syntactic element won't get an anchor pos.
10840 (c-add-syntax 'inexpr-statement)
10842 ;; A parenthesis normally belongs to an arglist.
10843 (c-add-syntax 'arglist-cont-nonempty nil paren-pos)))
10845 (goto-char (max boi
10846 (if containing-sexp
10847 (1+ containing-sexp)
10848 (point-min))))
10849 (setq step-type 'same
10850 on-label nil))
10852 ;; Stepped out of a brace block.
10853 (setq step-type (c-beginning-of-statement-1 containing-sexp)
10854 on-label (eq step-type 'label))
10856 (if (and (eq step-type 'same)
10857 (/= paren-pos (point)))
10858 (let (inexpr)
10859 (cond
10860 ((save-excursion
10861 (goto-char paren-pos)
10862 (setq inexpr (c-looking-at-inexpr-block
10863 (c-safe-position containing-sexp paren-state)
10864 containing-sexp)))
10865 (c-add-syntax (if (eq (car inexpr) 'inlambda)
10866 'defun-block-intro
10867 'statement-block-intro)
10868 nil))
10869 ((looking-at c-other-decl-block-key)
10870 (c-add-syntax
10871 (cdr (assoc (match-string 1)
10872 c-other-decl-block-key-in-symbols-alist))
10873 (max (c-point 'boi paren-pos) (point))))
10874 (t (c-add-syntax 'defun-block-intro nil))))
10876 (c-add-syntax 'statement-block-intro nil)))
10878 (if (= paren-pos boi)
10879 ;; Always done if the open brace was at boi. The
10880 ;; c-beginning-of-statement-1 call above is necessary
10881 ;; anyway, to decide the type of block-intro to add.
10882 (goto-char paren-pos)
10883 (setq boi (c-point 'boi)))
10886 ;; Fill in the current point as the anchor for all the symbols
10887 ;; added above.
10888 (let ((p c-syntactic-context) q)
10889 (while (not (eq p syntax-last))
10890 (setq q (cdr (car p))) ; e.g. (nil 28) [from (arglist-cont-nonempty nil 28)]
10891 (while q
10892 (unless (car q)
10893 (setcar q (point)))
10894 (setq q (cdr q)))
10895 (setq p (cdr p))))
10898 (defun c-add-class-syntax (symbol
10899 containing-decl-open
10900 containing-decl-start
10901 containing-decl-kwd
10902 paren-state)
10903 ;; The inclass and class-close syntactic symbols are added in
10904 ;; several places and some work is needed to fix everything.
10905 ;; Therefore it's collected here.
10907 ;; This function might do hidden buffer changes.
10908 (goto-char containing-decl-open)
10909 (if (and (eq symbol 'inclass) (= (point) (c-point 'boi)))
10910 (progn
10911 (c-add-syntax symbol containing-decl-open)
10912 containing-decl-open)
10913 (goto-char containing-decl-start)
10914 ;; Ought to use `c-add-stmt-syntax' instead of backing up to boi
10915 ;; here, but we have to do like this for compatibility.
10916 (back-to-indentation)
10917 (c-add-syntax symbol (point))
10918 (if (and (c-keyword-member containing-decl-kwd
10919 'c-inexpr-class-kwds)
10920 (/= containing-decl-start (c-point 'boi containing-decl-start)))
10921 (c-add-syntax 'inexpr-class))
10922 (point)))
10924 (defun c-guess-continued-construct (indent-point
10925 char-after-ip
10926 beg-of-same-or-containing-stmt
10927 containing-sexp
10928 paren-state)
10929 ;; This function contains the decision tree reached through both
10930 ;; cases 18 and 10. It's a continued statement or top level
10931 ;; construct of some kind.
10933 ;; This function might do hidden buffer changes.
10935 (let (special-brace-list placeholder)
10936 (goto-char indent-point)
10937 (skip-chars-forward " \t")
10939 (cond
10940 ;; (CASE A removed.)
10941 ;; CASE B: open braces for class or brace-lists
10942 ((setq special-brace-list
10943 (or (and c-special-brace-lists
10944 (c-looking-at-special-brace-list))
10945 (eq char-after-ip ?{)))
10947 (cond
10948 ;; CASE B.1: class-open
10949 ((save-excursion
10950 (and (eq (char-after) ?{)
10951 (c-looking-at-decl-block containing-sexp t)
10952 (setq beg-of-same-or-containing-stmt (point))))
10953 (c-add-syntax 'class-open beg-of-same-or-containing-stmt))
10955 ;; CASE B.2: brace-list-open
10956 ((or (consp special-brace-list)
10957 (consp
10958 (c-looking-at-or-maybe-in-bracelist
10959 containing-sexp beg-of-same-or-containing-stmt))
10961 ;; The most semantically accurate symbol here is
10962 ;; brace-list-open, but we normally report it simply as a
10963 ;; statement-cont. The reason is that one normally adjusts
10964 ;; brace-list-open for brace lists as top-level constructs,
10965 ;; and brace lists inside statements is a completely different
10966 ;; context. C.f. case 5A.3.
10967 (c-beginning-of-statement-1 containing-sexp)
10968 (c-add-stmt-syntax (if c-auto-newline-analysis
10969 ;; Turn off the dwim above when we're
10970 ;; analyzing the nature of the brace
10971 ;; for the auto newline feature.
10972 'brace-list-open
10973 'statement-cont)
10974 nil nil
10975 containing-sexp paren-state))
10977 ;; CASE B.3: The body of a function declared inside a normal
10978 ;; block. Can occur e.g. in Pike and when using gcc
10979 ;; extensions, but watch out for macros followed by blocks.
10980 ;; C.f. cases E, 16F and 17G.
10981 ((and (not (c-at-statement-start-p))
10982 (eq (c-beginning-of-statement-1 containing-sexp nil nil t)
10983 'same)
10984 (save-excursion
10985 (let ((c-recognize-typeless-decls nil))
10986 ;; Turn off recognition of constructs that lacks a
10987 ;; type in this case, since that's more likely to be
10988 ;; a macro followed by a block.
10989 (c-forward-decl-or-cast-1 (c-point 'bosws) nil nil))))
10990 (c-add-stmt-syntax 'defun-open nil t
10991 containing-sexp paren-state))
10993 ;; CASE B.5: We have a C++11 "return \n { ..... }" Note that we're
10994 ;; not at the "{", currently.
10995 ((progn (goto-char indent-point)
10996 (backward-sexp)
10997 (looking-at c-return-key))
10998 (c-add-stmt-syntax 'statement-cont nil t
10999 containing-sexp paren-state))
11001 ;; CASE B.4: Continued statement with block open. The most
11002 ;; accurate analysis is perhaps `statement-cont' together with
11003 ;; `block-open' but we play DWIM and use `substatement-open'
11004 ;; instead. The rationale is that this typically is a macro
11005 ;; followed by a block which makes it very similar to a
11006 ;; statement with a substatement block.
11008 (c-add-stmt-syntax 'substatement-open nil nil
11009 containing-sexp paren-state))
11012 ;; CASE C: iostream insertion or extraction operator
11013 ((and (looking-at "\\(<<\\|>>\\)\\([^=]\\|$\\)")
11014 (save-excursion
11015 (goto-char beg-of-same-or-containing-stmt)
11016 ;; If there is no preceding streamop in the statement
11017 ;; then indent this line as a normal statement-cont.
11018 (when (c-syntactic-re-search-forward
11019 "\\(<<\\|>>\\)\\([^=]\\|$\\)" indent-point 'move t t)
11020 (c-add-syntax 'stream-op (c-point 'boi))
11021 t))))
11023 ;; CASE E: In the "K&R region" of a function declared inside a
11024 ;; normal block. C.f. case B.3.
11025 ((and (save-excursion
11026 ;; Check that the next token is a '{'. This works as
11027 ;; long as no language that allows nested function
11028 ;; definitions allows stuff like member init lists, K&R
11029 ;; declarations or throws clauses there.
11031 ;; Note that we do a forward search for something ahead
11032 ;; of the indentation line here. That's not good since
11033 ;; the user might not have typed it yet. Unfortunately
11034 ;; it's exceedingly tricky to recognize a function
11035 ;; prototype in a code block without resorting to this.
11036 (c-forward-syntactic-ws)
11037 (eq (char-after) ?{))
11038 (not (c-at-statement-start-p))
11039 (eq (c-beginning-of-statement-1 containing-sexp nil nil t)
11040 'same)
11041 (save-excursion
11042 (let ((c-recognize-typeless-decls nil))
11043 ;; Turn off recognition of constructs that lacks a
11044 ;; type in this case, since that's more likely to be
11045 ;; a macro followed by a block.
11046 (c-forward-decl-or-cast-1 (c-point 'bosws) nil nil))))
11047 (c-add-stmt-syntax 'func-decl-cont nil t
11048 containing-sexp paren-state))
11050 ;;CASE F: continued statement and the only preceding items are
11051 ;;annotations.
11052 ((and (c-major-mode-is 'java-mode)
11053 (setq placeholder (point))
11054 (c-beginning-of-statement-1)
11055 (progn
11056 (while (and (c-forward-annotation)
11057 (< (point) placeholder))
11058 (c-forward-syntactic-ws))
11060 (prog1
11061 (>= (point) placeholder)
11062 (goto-char placeholder)))
11063 (c-beginning-of-statement-1 containing-sexp)
11064 (c-add-syntax 'annotation-var-cont (point)))
11066 ;; CASE G: a template list continuation?
11067 ;; Mostly a duplication of case 5D.3 to fix templates-19:
11068 ((and (c-major-mode-is 'c++-mode)
11069 (save-excursion
11070 (goto-char indent-point)
11071 (c-with-syntax-table c++-template-syntax-table
11072 (setq placeholder (c-up-list-backward)))
11073 (and placeholder
11074 (eq (char-after placeholder) ?<)
11075 (/= (char-before placeholder) ?<)
11076 (progn
11077 (goto-char (1+ placeholder))
11078 (not (looking-at c-<-op-cont-regexp))))))
11079 (c-with-syntax-table c++-template-syntax-table
11080 (goto-char placeholder)
11081 (c-beginning-of-statement-1 containing-sexp t))
11082 (if (save-excursion
11083 (c-backward-syntactic-ws containing-sexp)
11084 (eq (char-before) ?<))
11085 ;; In a nested template arglist.
11086 (progn
11087 (goto-char placeholder)
11088 (c-syntactic-skip-backward "^,;" containing-sexp t)
11089 (c-forward-syntactic-ws))
11090 (back-to-indentation))
11091 ;; FIXME: Should use c-add-stmt-syntax, but it's not yet
11092 ;; template aware.
11093 (c-add-syntax 'template-args-cont (point) placeholder))
11095 ;; CASE D: continued statement.
11097 (c-beginning-of-statement-1 containing-sexp)
11098 (c-add-stmt-syntax 'statement-cont nil nil
11099 containing-sexp paren-state))
11102 ;; The next autoload was added by RMS on 2005/8/9 - don't know why (ACM,
11103 ;; 2005/11/29).
11104 ;;;###autoload
11105 (defun c-guess-basic-syntax ()
11106 "Return the syntactic context of the current line."
11107 (save-excursion
11108 (beginning-of-line)
11109 (c-save-buffer-state
11110 ((indent-point (point))
11111 (case-fold-search nil)
11112 ;; A whole ugly bunch of various temporary variables. Have
11113 ;; to declare them here since it's not possible to declare
11114 ;; a variable with only the scope of a cond test and the
11115 ;; following result clauses, and most of this function is a
11116 ;; single gigantic cond. :P
11117 literal char-before-ip before-ws-ip char-after-ip macro-start
11118 in-macro-expr c-syntactic-context placeholder c-in-literal-cache
11119 step-type tmpsymbol keyword injava-inher special-brace-list tmp-pos
11120 containing-<
11121 ;; The following record some positions for the containing
11122 ;; declaration block if we're directly within one:
11123 ;; `containing-decl-open' is the position of the open
11124 ;; brace. `containing-decl-start' is the start of the
11125 ;; declaration. `containing-decl-kwd' is the keyword
11126 ;; symbol of the keyword that tells what kind of block it
11127 ;; is.
11128 containing-decl-open
11129 containing-decl-start
11130 containing-decl-kwd
11131 ;; The open paren of the closest surrounding sexp or nil if
11132 ;; there is none.
11133 containing-sexp
11134 ;; The position after the closest preceding brace sexp
11135 ;; (nested sexps are ignored), or the position after
11136 ;; `containing-sexp' if there is none, or (point-min) if
11137 ;; `containing-sexp' is nil.
11139 ;; The paren state outside `containing-sexp', or at
11140 ;; `indent-point' if `containing-sexp' is nil.
11141 (paren-state (c-parse-state))
11142 ;; There's always at most one syntactic element which got
11143 ;; an anchor pos. It's stored in syntactic-relpos.
11144 syntactic-relpos
11145 (c-stmt-delim-chars c-stmt-delim-chars))
11147 ;; Check if we're directly inside an enclosing declaration
11148 ;; level block.
11149 (when (and (setq containing-sexp
11150 (c-most-enclosing-brace paren-state))
11151 (progn
11152 (goto-char containing-sexp)
11153 (eq (char-after) ?{))
11154 (setq placeholder
11155 (c-looking-at-decl-block
11156 (c-most-enclosing-brace paren-state
11157 containing-sexp)
11158 t)))
11159 (setq containing-decl-open containing-sexp
11160 containing-decl-start (point)
11161 containing-sexp nil)
11162 (goto-char placeholder)
11163 (setq containing-decl-kwd (and (looking-at c-keywords-regexp)
11164 (c-keyword-sym (match-string 1)))))
11166 ;; Init some position variables.
11167 (if paren-state
11168 (progn
11169 (setq containing-sexp (car paren-state)
11170 paren-state (cdr paren-state))
11171 (if (consp containing-sexp)
11172 (save-excursion
11173 (goto-char (cdr containing-sexp))
11174 (if (and (c-major-mode-is 'c++-mode)
11175 (c-back-over-member-initializer-braces))
11176 (c-syntactic-skip-backward "^}" nil t))
11177 (setq lim (point))
11178 (if paren-state
11179 ;; Ignore balanced paren. The next entry
11180 ;; can't be another one.
11181 (setq containing-sexp (car paren-state)
11182 paren-state (cdr paren-state))
11183 ;; If there is no surrounding open paren then
11184 ;; put the last balanced pair back on paren-state.
11185 (setq paren-state (cons containing-sexp paren-state)
11186 containing-sexp nil)))
11187 (setq lim (1+ containing-sexp))))
11188 (setq lim (point-min)))
11190 ;; If we're in a parenthesis list then ',' delimits the
11191 ;; "statements" rather than being an operator (with the
11192 ;; exception of the "for" clause). This difference is
11193 ;; typically only noticeable when statements are used in macro
11194 ;; arglists.
11195 (when (and containing-sexp
11196 (eq (char-after containing-sexp) ?\())
11197 (setq c-stmt-delim-chars c-stmt-delim-chars-with-comma))
11198 ;; cache char before and after indent point, and move point to
11199 ;; the most likely position to perform the majority of tests
11200 (goto-char indent-point)
11201 (c-backward-syntactic-ws lim)
11202 (setq before-ws-ip (point)
11203 char-before-ip (char-before))
11204 (goto-char indent-point)
11205 (skip-chars-forward " \t")
11206 (setq char-after-ip (char-after))
11208 ;; are we in a literal?
11209 (setq literal (c-in-literal lim))
11211 ;; now figure out syntactic qualities of the current line
11212 (cond
11214 ;; CASE 1: in a string.
11215 ((eq literal 'string)
11216 (c-add-syntax 'string (c-point 'bopl)))
11218 ;; CASE 2: in a C or C++ style comment.
11219 ((and (memq literal '(c c++))
11220 ;; This is a kludge for XEmacs where we use
11221 ;; `buffer-syntactic-context', which doesn't correctly
11222 ;; recognize "\*/" to end a block comment.
11223 ;; `parse-partial-sexp' which is used by
11224 ;; `c-literal-limits' will however do that in most
11225 ;; versions, which results in that we get nil from
11226 ;; `c-literal-limits' even when `c-in-literal' claims
11227 ;; we're inside a comment.
11228 (setq placeholder (c-literal-start lim)))
11229 (c-add-syntax literal placeholder))
11231 ;; CASE 3: in a cpp preprocessor macro continuation.
11232 ((and (save-excursion
11233 (when (c-beginning-of-macro)
11234 (setq macro-start (point))))
11235 (/= macro-start (c-point 'boi))
11236 (progn
11237 (setq tmpsymbol 'cpp-macro-cont)
11238 (or (not c-syntactic-indentation-in-macros)
11239 (save-excursion
11240 (goto-char macro-start)
11241 ;; If at the beginning of the body of a #define
11242 ;; directive then analyze as cpp-define-intro
11243 ;; only. Go on with the syntactic analysis
11244 ;; otherwise. in-macro-expr is set if we're in a
11245 ;; cpp expression, i.e. before the #define body
11246 ;; or anywhere in a non-#define directive.
11247 (if (c-forward-to-cpp-define-body)
11248 (let ((indent-boi (c-point 'boi indent-point)))
11249 (setq in-macro-expr (> (point) indent-boi)
11250 tmpsymbol 'cpp-define-intro)
11251 (= (point) indent-boi))
11252 (setq in-macro-expr t)
11253 nil)))))
11254 (c-add-syntax tmpsymbol macro-start)
11255 (setq macro-start nil))
11257 ;; CASE 11: an else clause?
11258 ((looking-at "else\\>[^_]")
11259 (c-beginning-of-statement-1 containing-sexp)
11260 (c-add-stmt-syntax 'else-clause nil t
11261 containing-sexp paren-state))
11263 ;; CASE 12: while closure of a do/while construct?
11264 ((and (looking-at "while\\>[^_]")
11265 (save-excursion
11266 (prog1 (eq (c-beginning-of-statement-1 containing-sexp)
11267 'beginning)
11268 (setq placeholder (point)))))
11269 (goto-char placeholder)
11270 (c-add-stmt-syntax 'do-while-closure nil t
11271 containing-sexp paren-state))
11273 ;; CASE 13: A catch or finally clause? This case is simpler
11274 ;; than if-else and do-while, because a block is required
11275 ;; after every try, catch and finally.
11276 ((save-excursion
11277 (and (cond ((c-major-mode-is 'c++-mode)
11278 (looking-at "catch\\>[^_]"))
11279 ((c-major-mode-is 'java-mode)
11280 (looking-at "\\(catch\\|finally\\)\\>[^_]")))
11281 (and (c-safe (c-backward-syntactic-ws)
11282 (c-backward-sexp)
11284 (eq (char-after) ?{)
11285 (c-safe (c-backward-syntactic-ws)
11286 (c-backward-sexp)
11288 (if (eq (char-after) ?\()
11289 (c-safe (c-backward-sexp) t)
11291 (looking-at "\\(try\\|catch\\)\\>[^_]")
11292 (setq placeholder (point))))
11293 (goto-char placeholder)
11294 (c-add-stmt-syntax 'catch-clause nil t
11295 containing-sexp paren-state))
11297 ;; CASE 18: A substatement we can recognize by keyword.
11298 ((save-excursion
11299 (and c-opt-block-stmt-key
11300 (not (eq char-before-ip ?\;))
11301 (not (c-at-vsemi-p before-ws-ip))
11302 (not (memq char-after-ip '(?\) ?\] ?,)))
11303 (or (not (eq char-before-ip ?}))
11304 (c-looking-at-inexpr-block-backward c-state-cache))
11305 (> (point)
11306 (progn
11307 ;; Ought to cache the result from the
11308 ;; c-beginning-of-statement-1 calls here.
11309 (setq placeholder (point))
11310 (while (eq (setq step-type
11311 (c-beginning-of-statement-1 lim))
11312 'label))
11313 (if (eq step-type 'previous)
11314 (goto-char placeholder)
11315 (setq placeholder (point))
11316 (if (and (eq step-type 'same)
11317 (not (looking-at c-opt-block-stmt-key)))
11318 ;; Step up to the containing statement if we
11319 ;; stayed in the same one.
11320 (let (step)
11321 (while (eq
11322 (setq step
11323 (c-beginning-of-statement-1 lim))
11324 'label))
11325 (if (eq step 'up)
11326 (setq placeholder (point))
11327 ;; There was no containing statement after all.
11328 (goto-char placeholder)))))
11329 placeholder))
11330 (if (looking-at c-block-stmt-2-key)
11331 ;; Require a parenthesis after these keywords.
11332 ;; Necessary to catch e.g. synchronized in Java,
11333 ;; which can be used both as statement and
11334 ;; modifier.
11335 (and (zerop (c-forward-token-2 1 nil))
11336 (eq (char-after) ?\())
11337 (looking-at c-opt-block-stmt-key))))
11339 (if (eq step-type 'up)
11340 ;; CASE 18A: Simple substatement.
11341 (progn
11342 (goto-char placeholder)
11343 (cond
11344 ((eq char-after-ip ?{)
11345 (c-add-stmt-syntax 'substatement-open nil nil
11346 containing-sexp paren-state))
11347 ((save-excursion
11348 (goto-char indent-point)
11349 (back-to-indentation)
11350 (c-forward-label))
11351 (c-add-stmt-syntax 'substatement-label nil nil
11352 containing-sexp paren-state))
11354 (c-add-stmt-syntax 'substatement nil nil
11355 containing-sexp paren-state))))
11357 ;; CASE 18B: Some other substatement. This is shared
11358 ;; with case 10.
11359 (c-guess-continued-construct indent-point
11360 char-after-ip
11361 placeholder
11363 paren-state)))
11365 ;; CASE 14: A case or default label
11366 ((save-excursion
11367 (and (looking-at c-label-kwds-regexp)
11368 (or (c-major-mode-is 'idl-mode)
11369 (and
11370 containing-sexp
11371 (goto-char containing-sexp)
11372 (eq (char-after) ?{)
11373 (progn (c-backward-syntactic-ws) t)
11374 (eq (char-before) ?\))
11375 (c-go-list-backward)
11376 (progn (c-backward-syntactic-ws) t)
11377 (c-simple-skip-symbol-backward)
11378 (looking-at c-block-stmt-2-key)))))
11379 (if containing-sexp
11380 (progn
11381 (goto-char containing-sexp)
11382 (setq lim (c-most-enclosing-brace c-state-cache
11383 containing-sexp))
11384 (c-backward-to-block-anchor lim)
11385 (c-add-stmt-syntax 'case-label nil t lim paren-state))
11386 ;; Got a bogus label at the top level. In lack of better
11387 ;; alternatives, anchor it on (point-min).
11388 (c-add-syntax 'case-label (point-min))))
11390 ;; CASE 15: any other label
11391 ((save-excursion
11392 (back-to-indentation)
11393 (and (not (looking-at c-syntactic-ws-start))
11394 (not (looking-at c-label-kwds-regexp))
11395 (c-forward-label)))
11396 (cond (containing-decl-open
11397 (setq placeholder (c-add-class-syntax 'inclass
11398 containing-decl-open
11399 containing-decl-start
11400 containing-decl-kwd
11401 paren-state))
11402 ;; Append access-label with the same anchor point as
11403 ;; inclass gets.
11404 (c-append-syntax 'access-label placeholder))
11406 (containing-sexp
11407 (goto-char containing-sexp)
11408 (setq lim (c-most-enclosing-brace c-state-cache
11409 containing-sexp))
11410 (save-excursion
11411 (setq tmpsymbol
11412 (if (and (eq (c-beginning-of-statement-1 lim) 'up)
11413 (looking-at "switch\\>[^_]"))
11414 ;; If the surrounding statement is a switch then
11415 ;; let's analyze all labels as switch labels, so
11416 ;; that they get lined up consistently.
11417 'case-label
11418 'label)))
11419 (c-backward-to-block-anchor lim)
11420 (c-add-stmt-syntax tmpsymbol nil t lim paren-state))
11423 ;; A label on the top level. Treat it as a class
11424 ;; context. (point-min) is the closest we get to the
11425 ;; class open brace.
11426 (c-add-syntax 'access-label (point-min)))))
11428 ;; CASE 4: In-expression statement. C.f. cases 7B, 16A and
11429 ;; 17E.
11430 ((setq placeholder (c-looking-at-inexpr-block
11431 (c-safe-position containing-sexp paren-state)
11432 containing-sexp
11433 ;; Have to turn on the heuristics after
11434 ;; the point even though it doesn't work
11435 ;; very well. C.f. test case class-16.pike.
11437 (setq tmpsymbol (assq (car placeholder)
11438 '((inexpr-class . class-open)
11439 (inexpr-statement . block-open))))
11440 (if tmpsymbol
11441 ;; It's a statement block or an anonymous class.
11442 (setq tmpsymbol (cdr tmpsymbol))
11443 ;; It's a Pike lambda. Check whether we are between the
11444 ;; lambda keyword and the argument list or at the defun
11445 ;; opener.
11446 (setq tmpsymbol (if (eq char-after-ip ?{)
11447 'inline-open
11448 'lambda-intro-cont)))
11449 (goto-char (cdr placeholder))
11450 (back-to-indentation)
11451 (c-add-stmt-syntax tmpsymbol nil t
11452 (c-most-enclosing-brace c-state-cache (point))
11453 paren-state)
11454 (unless (eq (point) (cdr placeholder))
11455 (c-add-syntax (car placeholder))))
11457 ;; CASE 5: Line is inside a declaration level block or at top level.
11458 ((or containing-decl-open (null containing-sexp))
11459 (cond
11461 ;; CASE 5A: we are looking at a defun, brace list, class,
11462 ;; or inline-inclass method opening brace
11463 ((setq special-brace-list
11464 (or (and c-special-brace-lists
11465 (c-looking-at-special-brace-list))
11466 (eq char-after-ip ?{)))
11467 (cond
11469 ;; CASE 5A.1: Non-class declaration block open.
11470 ((save-excursion
11471 (let (tmp)
11472 (and (eq char-after-ip ?{)
11473 (setq tmp (c-looking-at-decl-block containing-sexp t))
11474 (progn
11475 (setq placeholder (point))
11476 (goto-char tmp)
11477 (looking-at c-symbol-key))
11478 (c-keyword-member
11479 (c-keyword-sym (setq keyword (match-string 0)))
11480 'c-other-block-decl-kwds))))
11481 (goto-char placeholder)
11482 (c-add-stmt-syntax
11483 (if (string-equal keyword "extern")
11484 ;; Special case for extern-lang-open.
11485 'extern-lang-open
11486 (intern (concat keyword "-open")))
11487 nil t containing-sexp paren-state))
11489 ;; CASE 5A.2: we are looking at a class opening brace
11490 ((save-excursion
11491 (goto-char indent-point)
11492 (skip-chars-forward " \t")
11493 (and (eq (char-after) ?{)
11494 (c-looking-at-decl-block containing-sexp t)
11495 (setq placeholder (point))))
11496 (c-add-syntax 'class-open placeholder))
11498 ;; CASE 5A.3: brace list open
11499 ((save-excursion
11500 (goto-char indent-point)
11501 (skip-chars-forward " \t")
11502 (cond
11503 ((c-backward-over-enum-header)
11504 (setq placeholder (c-point 'boi)))
11505 ((consp (setq placeholder
11506 (c-looking-at-or-maybe-in-bracelist
11507 containing-sexp lim)))
11508 (setq tmpsymbol (and (cdr placeholder) 'topmost-intro-cont))
11509 (setq placeholder (c-point 'boi (car placeholder))))))
11510 (if (and (not c-auto-newline-analysis)
11511 ;(c-major-mode-is 'java-mode) ; Not needed anymore (2016-08-30).
11512 (eq tmpsymbol 'topmost-intro-cont))
11513 ;; We're in Java and have found that the open brace
11514 ;; belongs to a "new Foo[]" initialization list,
11515 ;; which means the brace list is part of an
11516 ;; expression and not a top level definition. We
11517 ;; therefore treat it as any topmost continuation
11518 ;; even though the semantically correct symbol still
11519 ;; is brace-list-open, on the same grounds as in
11520 ;; case B.2.
11521 (progn
11522 (c-beginning-of-statement-1 lim)
11523 (c-add-syntax 'topmost-intro-cont (c-point 'boi)))
11524 (c-add-syntax 'brace-list-open placeholder)))
11526 ;; CASE 5A.4: inline defun open
11527 ((and containing-decl-open
11528 (not (c-keyword-member containing-decl-kwd
11529 'c-other-block-decl-kwds)))
11530 (c-add-syntax 'inline-open)
11531 (c-add-class-syntax 'inclass
11532 containing-decl-open
11533 containing-decl-start
11534 containing-decl-kwd
11535 paren-state))
11537 ;; CASE 5A.5: ordinary defun open
11539 (save-excursion
11540 (c-beginning-of-decl-1 lim)
11541 (while (cond
11542 ((looking-at c-specifier-key)
11543 (c-forward-keyword-clause 1))
11544 ((and c-opt-cpp-prefix
11545 (looking-at c-noise-macro-with-parens-name-re))
11546 (c-forward-noise-clause))))
11547 (c-add-syntax 'defun-open (c-point 'boi))
11548 ;; Bogus to use bol here, but it's the legacy. (Resolved,
11549 ;; 2007-11-09)
11550 ))))
11552 ;; CASE 5R: Member init list. (Used to be part of CASE 5B.1)
11553 ;; Note there is no limit on the backward search here, since member
11554 ;; init lists can, in practice, be very large.
11555 ((save-excursion
11556 (when (and (c-major-mode-is 'c++-mode)
11557 (setq placeholder (c-back-over-member-initializers)))
11558 (setq tmp-pos (point))))
11559 (if (= (c-point 'bosws) (1+ tmp-pos))
11560 (progn
11561 ;; There is no preceding member init clause.
11562 ;; Indent relative to the beginning of indentation
11563 ;; for the topmost-intro line that contains the
11564 ;; prototype's open paren.
11565 (goto-char placeholder)
11566 (c-add-syntax 'member-init-intro (c-point 'boi)))
11567 ;; Indent relative to the first member init clause.
11568 (goto-char (1+ tmp-pos))
11569 (c-forward-syntactic-ws)
11570 (c-add-syntax 'member-init-cont (point))))
11572 ;; CASE 5B: After a function header but before the body (or
11573 ;; the ending semicolon if there's no body).
11574 ((save-excursion
11575 (when (setq placeholder (c-just-after-func-arglist-p
11576 (max lim (c-determine-limit 500))))
11577 (setq tmp-pos (point))))
11578 (cond
11580 ;; CASE 5B.1: Member init list.
11581 ((eq (char-after tmp-pos) ?:)
11582 ;; There is no preceding member init clause.
11583 ;; Indent relative to the beginning of indentation
11584 ;; for the topmost-intro line that contains the
11585 ;; prototype's open paren.
11586 (goto-char placeholder)
11587 (c-add-syntax 'member-init-intro (c-point 'boi)))
11589 ;; CASE 5B.2: K&R arg decl intro
11590 ((and c-recognize-knr-p
11591 (c-in-knr-argdecl lim))
11592 (c-beginning-of-statement-1 lim)
11593 (c-add-syntax 'knr-argdecl-intro (c-point 'boi))
11594 (if containing-decl-open
11595 (c-add-class-syntax 'inclass
11596 containing-decl-open
11597 containing-decl-start
11598 containing-decl-kwd
11599 paren-state)))
11601 ;; CASE 5B.4: Nether region after a C++ or Java func
11602 ;; decl, which could include a `throws' declaration.
11604 (c-beginning-of-statement-1 lim)
11605 (c-add-syntax 'func-decl-cont (c-point 'boi))
11608 ;; CASE 5C: inheritance line. could be first inheritance
11609 ;; line, or continuation of a multiple inheritance
11610 ((or (and (c-major-mode-is 'c++-mode)
11611 (progn
11612 (when (eq char-after-ip ?,)
11613 (skip-chars-forward " \t")
11614 (forward-char))
11615 (looking-at c-opt-postfix-decl-spec-key)))
11616 (and (or (eq char-before-ip ?:)
11617 ;; watch out for scope operator
11618 (save-excursion
11619 (and (eq char-after-ip ?:)
11620 (c-safe (forward-char 1) t)
11621 (not (eq (char-after) ?:))
11623 (save-excursion
11624 (c-beginning-of-statement-1 lim)
11625 (when (looking-at c-opt-<>-sexp-key)
11626 (goto-char (match-end 1))
11627 (c-forward-syntactic-ws)
11628 (c-forward-<>-arglist nil)
11629 (c-forward-syntactic-ws))
11630 (looking-at c-class-key)))
11631 ;; for Java
11632 (and (c-major-mode-is 'java-mode)
11633 (let ((fence (save-excursion
11634 (c-beginning-of-statement-1 lim)
11635 (point)))
11636 cont done)
11637 (save-excursion
11638 (while (not done)
11639 (cond ((looking-at c-opt-postfix-decl-spec-key)
11640 (setq injava-inher (cons cont (point))
11641 done t))
11642 ((or (not (c-safe (c-forward-sexp -1) t))
11643 (<= (point) fence))
11644 (setq done t))
11646 (setq cont t)))
11647 injava-inher)
11648 (not (c-crosses-statement-barrier-p (cdr injava-inher)
11649 (point)))
11651 (cond
11653 ;; CASE 5C.1: non-hanging colon on an inher intro
11654 ((eq char-after-ip ?:)
11655 (c-beginning-of-statement-1 lim)
11656 (c-add-syntax 'inher-intro (c-point 'boi))
11657 ;; don't add inclass symbol since relative point already
11658 ;; contains any class offset
11661 ;; CASE 5C.2: hanging colon on an inher intro
11662 ((eq char-before-ip ?:)
11663 (c-beginning-of-statement-1 lim)
11664 (c-add-syntax 'inher-intro (c-point 'boi))
11665 (if containing-decl-open
11666 (c-add-class-syntax 'inclass
11667 containing-decl-open
11668 containing-decl-start
11669 containing-decl-kwd
11670 paren-state)))
11672 ;; CASE 5C.3: in a Java implements/extends
11673 (injava-inher
11674 (let ((where (cdr injava-inher))
11675 (cont (car injava-inher)))
11676 (goto-char where)
11677 (cond ((looking-at "throws\\>[^_]")
11678 (c-add-syntax 'func-decl-cont
11679 (progn (c-beginning-of-statement-1 lim)
11680 (c-point 'boi))))
11681 (cont (c-add-syntax 'inher-cont where))
11682 (t (c-add-syntax 'inher-intro
11683 (progn (goto-char (cdr injava-inher))
11684 (c-beginning-of-statement-1 lim)
11685 (point))))
11688 ;; CASE 5C.4: a continued inheritance line
11690 (c-beginning-of-inheritance-list lim)
11691 (c-add-syntax 'inher-cont (point))
11692 ;; don't add inclass symbol since relative point already
11693 ;; contains any class offset
11696 ;; CASE 5P: AWK pattern or function or continuation
11697 ;; thereof.
11698 ((c-major-mode-is 'awk-mode)
11699 (setq placeholder (point))
11700 (c-add-stmt-syntax
11701 (if (and (eq (c-beginning-of-statement-1) 'same)
11702 (/= (point) placeholder))
11703 'topmost-intro-cont
11704 'topmost-intro)
11705 nil nil
11706 containing-sexp paren-state))
11708 ;; CASE 5D: this could be a top-level initialization, a
11709 ;; member init list continuation, or a template argument
11710 ;; list continuation.
11711 ((save-excursion
11712 ;; Note: We use the fact that lim is always after any
11713 ;; preceding brace sexp.
11714 (if c-recognize-<>-arglists
11715 (while (and
11716 (progn
11717 (c-syntactic-skip-backward "^;,=<>" lim t)
11718 (> (point) lim))
11720 (when c-overloadable-operators-regexp
11721 (when (setq placeholder (c-after-special-operator-id lim))
11722 (goto-char placeholder)
11724 (cond
11725 ((eq (char-before) ?>)
11726 (or (c-backward-<>-arglist nil lim)
11727 (backward-char))
11729 ((eq (char-before) ?<)
11730 (backward-char)
11731 (if (save-excursion
11732 (c-forward-<>-arglist nil))
11733 (progn (forward-char)
11734 nil)
11736 (t nil)))))
11737 ;; NB: No c-after-special-operator-id stuff in this
11738 ;; clause - we assume only C++ needs it.
11739 (c-syntactic-skip-backward "^;,=" lim t))
11740 (memq (char-before) '(?, ?= ?<)))
11741 (cond
11743 ;; CASE 5D.3: perhaps a template list continuation?
11744 ((and (c-major-mode-is 'c++-mode)
11745 (save-excursion
11746 (save-restriction
11747 (c-with-syntax-table c++-template-syntax-table
11748 (goto-char indent-point)
11749 (setq placeholder (c-up-list-backward))
11750 (and placeholder
11751 (eq (char-after placeholder) ?<))))))
11752 (c-with-syntax-table c++-template-syntax-table
11753 (goto-char placeholder)
11754 (c-beginning-of-statement-1 lim t))
11755 (if (save-excursion
11756 (c-backward-syntactic-ws lim)
11757 (eq (char-before) ?<))
11758 ;; In a nested template arglist.
11759 (progn
11760 (goto-char placeholder)
11761 (c-syntactic-skip-backward "^,;" lim t)
11762 (c-forward-syntactic-ws))
11763 (back-to-indentation))
11764 ;; FIXME: Should use c-add-stmt-syntax, but it's not yet
11765 ;; template aware.
11766 (c-add-syntax 'template-args-cont (point) placeholder))
11768 ;; CASE 5D.4: perhaps a multiple inheritance line?
11769 ((and (c-major-mode-is 'c++-mode)
11770 (save-excursion
11771 (c-beginning-of-statement-1 lim)
11772 (setq placeholder (point))
11773 (if (looking-at "static\\>[^_]")
11774 (c-forward-token-2 1 nil indent-point))
11775 (and (looking-at c-class-key)
11776 (zerop (c-forward-token-2 2 nil indent-point))
11777 (if (eq (char-after) ?<)
11778 (c-with-syntax-table c++-template-syntax-table
11779 (zerop (c-forward-token-2 1 t indent-point)))
11781 (eq (char-after) ?:))))
11782 (goto-char placeholder)
11783 (c-add-syntax 'inher-cont (c-point 'boi)))
11785 ;; CASE 5D.5: Continuation of the "expression part" of a
11786 ;; top level construct. Or, perhaps, an unrecognized construct.
11788 (while (and (setq placeholder (point))
11789 (eq (car (c-beginning-of-decl-1 containing-sexp)) ; Can't use `lim' here.
11790 'same)
11791 (save-excursion
11792 (c-backward-syntactic-ws)
11793 (eq (char-before) ?}))
11794 (< (point) placeholder)))
11795 (c-add-stmt-syntax
11796 (cond
11797 ((eq (point) placeholder) 'statement) ; unrecognized construct
11798 ;; A preceding comma at the top level means that a
11799 ;; new variable declaration starts here. Use
11800 ;; topmost-intro-cont for it, for consistency with
11801 ;; the first variable declaration. C.f. case 5N.
11802 ((eq char-before-ip ?,) 'topmost-intro-cont)
11803 (t 'statement-cont))
11804 nil nil containing-sexp paren-state))
11807 ;; CASE 5F: Close of a non-class declaration level block.
11808 ((and (eq char-after-ip ?})
11809 (c-keyword-member containing-decl-kwd
11810 'c-other-block-decl-kwds))
11811 ;; This is inconsistent: Should use `containing-decl-open'
11812 ;; here if it's at boi, like in case 5J.
11813 (goto-char containing-decl-start)
11814 (c-add-stmt-syntax
11815 (if (string-equal (symbol-name containing-decl-kwd) "extern")
11816 ;; Special case for compatibility with the
11817 ;; extern-lang syntactic symbols.
11818 'extern-lang-close
11819 (intern (concat (symbol-name containing-decl-kwd)
11820 "-close")))
11821 nil t
11822 (c-most-enclosing-brace paren-state (point))
11823 paren-state))
11825 ;; CASE 5G: we are looking at the brace which closes the
11826 ;; enclosing nested class decl
11827 ((and containing-sexp
11828 (eq char-after-ip ?})
11829 (eq containing-decl-open containing-sexp))
11830 (c-add-class-syntax 'class-close
11831 containing-decl-open
11832 containing-decl-start
11833 containing-decl-kwd
11834 paren-state))
11836 ;; CASE 5H: we could be looking at subsequent knr-argdecls
11837 ((and c-recognize-knr-p
11838 (not containing-sexp) ; can't be knr inside braces.
11839 (not (eq char-before-ip ?}))
11840 (save-excursion
11841 (setq placeholder (cdr (c-beginning-of-decl-1 lim)))
11842 (and placeholder
11843 ;; Do an extra check to avoid tripping up on
11844 ;; statements that occur in invalid contexts
11845 ;; (e.g. in macro bodies where we don't really
11846 ;; know the context of what we're looking at).
11847 (not (and c-opt-block-stmt-key
11848 (looking-at c-opt-block-stmt-key)))))
11849 (< placeholder indent-point))
11850 (goto-char placeholder)
11851 (c-add-syntax 'knr-argdecl (point)))
11853 ;; CASE 5I: ObjC method definition.
11854 ((and c-opt-method-key
11855 (looking-at c-opt-method-key))
11856 (c-beginning-of-statement-1 nil t)
11857 (if (= (point) indent-point)
11858 ;; Handle the case when it's the first (non-comment)
11859 ;; thing in the buffer. Can't look for a 'same return
11860 ;; value from cbos1 since ObjC directives currently
11861 ;; aren't recognized fully, so that we get 'same
11862 ;; instead of 'previous if it moved over a preceding
11863 ;; directive.
11864 (goto-char (point-min)))
11865 (c-add-syntax 'objc-method-intro (c-point 'boi)))
11867 ;; CASE 5N: At a variable declaration that follows a class
11868 ;; definition or some other block declaration that doesn't
11869 ;; end at the closing '}'. C.f. case 5D.5.
11870 ((progn
11871 (c-backward-syntactic-ws lim)
11872 (and (eq (char-before) ?})
11873 (save-excursion
11874 (let ((start (point)))
11875 (if (and c-state-cache
11876 (consp (car c-state-cache))
11877 (eq (cdar c-state-cache) (point)))
11878 ;; Speed up the backward search a bit.
11879 (goto-char (caar c-state-cache)))
11880 (c-beginning-of-decl-1 containing-sexp) ; Can't use `lim' here.
11881 (setq placeholder (point))
11882 (if (= start (point))
11883 ;; The '}' is unbalanced.
11885 (c-end-of-decl-1)
11886 (>= (point) indent-point))))))
11887 (goto-char placeholder)
11888 (c-add-stmt-syntax 'topmost-intro-cont nil nil
11889 containing-sexp paren-state))
11891 ;; NOTE: The point is at the end of the previous token here.
11893 ;; CASE 5J: we are at the topmost level, make
11894 ;; sure we skip back past any access specifiers
11895 ((and
11896 ;; A macro continuation line is never at top level.
11897 (not (and macro-start
11898 (> indent-point macro-start)))
11899 (save-excursion
11900 (setq placeholder (point))
11901 (or (memq char-before-ip '(?\; ?{ ?} nil))
11902 (c-at-vsemi-p before-ws-ip)
11903 (when (and (eq char-before-ip ?:)
11904 (eq (c-beginning-of-statement-1 lim)
11905 'label))
11906 (c-backward-syntactic-ws lim)
11907 (setq placeholder (point)))
11908 (and (c-major-mode-is 'objc-mode)
11909 (catch 'not-in-directive
11910 (c-beginning-of-statement-1 lim)
11911 (setq placeholder (point))
11912 (while (and (c-forward-objc-directive)
11913 (< (point) indent-point))
11914 (c-forward-syntactic-ws)
11915 (if (>= (point) indent-point)
11916 (throw 'not-in-directive t))
11917 (setq placeholder (point)))
11918 nil)))))
11919 ;; For historic reasons we anchor at bol of the last
11920 ;; line of the previous declaration. That's clearly
11921 ;; highly bogus and useless, and it makes our lives hard
11922 ;; to remain compatible. :P
11923 (goto-char placeholder)
11924 (c-add-syntax 'topmost-intro (c-point 'bol))
11925 (if containing-decl-open
11926 (if (c-keyword-member containing-decl-kwd
11927 'c-other-block-decl-kwds)
11928 (progn
11929 (goto-char (c-brace-anchor-point containing-decl-open))
11930 (c-add-stmt-syntax
11931 (if (string-equal (symbol-name containing-decl-kwd)
11932 "extern")
11933 ;; Special case for compatibility with the
11934 ;; extern-lang syntactic symbols.
11935 'inextern-lang
11936 (intern (concat "in"
11937 (symbol-name containing-decl-kwd))))
11938 nil t
11939 (c-most-enclosing-brace paren-state (point))
11940 paren-state))
11941 (c-add-class-syntax 'inclass
11942 containing-decl-open
11943 containing-decl-start
11944 containing-decl-kwd
11945 paren-state)))
11946 (when (and c-syntactic-indentation-in-macros
11947 macro-start
11948 (/= macro-start (c-point 'boi indent-point)))
11949 (c-add-syntax 'cpp-define-intro)
11950 (setq macro-start nil)))
11952 ;; CASE 5K: we are at an ObjC method definition
11953 ;; continuation line.
11954 ((and c-opt-method-key
11955 (save-excursion
11956 (c-beginning-of-statement-1 lim)
11957 (beginning-of-line)
11958 (when (looking-at c-opt-method-key)
11959 (setq placeholder (point)))))
11960 (c-add-syntax 'objc-method-args-cont placeholder))
11962 ;; CASE 5L: we are at the first argument of a template
11963 ;; arglist that begins on the previous line.
11964 ((and c-recognize-<>-arglists
11965 (eq (char-before) ?<)
11966 (not (and c-overloadable-operators-regexp
11967 (c-after-special-operator-id lim))))
11968 (c-beginning-of-statement-1 (c-safe-position (point) paren-state))
11969 (c-add-syntax 'template-args-cont (c-point 'boi)))
11971 ;; CASE 5Q: we are at a statement within a macro.
11972 (macro-start
11973 (c-beginning-of-statement-1 containing-sexp)
11974 (c-add-stmt-syntax 'statement nil t containing-sexp paren-state))
11976 ;;CASE 5N: We are at a topmost continuation line and the only
11977 ;;preceding items are annotations.
11978 ((and (c-major-mode-is 'java-mode)
11979 (setq placeholder (point))
11980 (c-beginning-of-statement-1)
11981 (progn
11982 (while (and (c-forward-annotation))
11983 (c-forward-syntactic-ws))
11985 (prog1
11986 (>= (point) placeholder)
11987 (goto-char placeholder)))
11988 (c-add-syntax 'annotation-top-cont (c-point 'boi)))
11990 ;; CASE 5M: we are at a topmost continuation line
11992 (c-beginning-of-statement-1 (c-safe-position (point) paren-state))
11993 (when (c-major-mode-is 'objc-mode)
11994 (setq placeholder (point))
11995 (while (and (c-forward-objc-directive)
11996 (< (point) indent-point))
11997 (c-forward-syntactic-ws)
11998 (setq placeholder (point)))
11999 (goto-char placeholder))
12000 (c-add-syntax 'topmost-intro-cont (c-point 'boi)))
12003 ;; (CASE 6 has been removed.)
12005 ;; CASE 7: line is an expression, not a statement. Most
12006 ;; likely we are either in a function prototype or a function
12007 ;; call argument list
12008 ((not (or (and c-special-brace-lists
12009 (save-excursion
12010 (goto-char containing-sexp)
12011 (c-looking-at-special-brace-list)))
12012 (eq (char-after containing-sexp) ?{)))
12013 (cond
12015 ;; CASE 7A: we are looking at the arglist closing paren.
12016 ;; C.f. case 7F.
12017 ((memq char-after-ip '(?\) ?\]))
12018 (goto-char containing-sexp)
12019 (setq placeholder (c-point 'boi))
12020 (if (and (c-safe (backward-up-list 1) t)
12021 (>= (point) placeholder))
12022 (progn
12023 (forward-char)
12024 (skip-chars-forward " \t"))
12025 (goto-char placeholder))
12026 (c-add-stmt-syntax 'arglist-close (list containing-sexp) t
12027 (c-most-enclosing-brace paren-state (point))
12028 paren-state))
12030 ;; CASE 7B: Looking at the opening brace of an
12031 ;; in-expression block or brace list. C.f. cases 4, 16A
12032 ;; and 17E.
12033 ((and (eq char-after-ip ?{)
12034 (progn
12035 (setq placeholder (c-inside-bracelist-p (point)
12036 paren-state))
12037 (if placeholder
12038 (setq tmpsymbol '(brace-list-open . inexpr-class))
12039 (setq tmpsymbol '(block-open . inexpr-statement)
12040 placeholder
12041 (cdr-safe (c-looking-at-inexpr-block
12042 (c-safe-position containing-sexp
12043 paren-state)
12044 containing-sexp)))
12045 ;; placeholder is nil if it's a block directly in
12046 ;; a function arglist. That makes us skip out of
12047 ;; this case.
12049 (goto-char placeholder)
12050 (back-to-indentation)
12051 (c-add-stmt-syntax (car tmpsymbol) nil t
12052 (c-most-enclosing-brace paren-state (point))
12053 paren-state)
12054 (if (/= (point) placeholder)
12055 (c-add-syntax (cdr tmpsymbol))))
12057 ;; CASE 7C: we are looking at the first argument in an empty
12058 ;; argument list. Use arglist-close if we're actually
12059 ;; looking at a close paren or bracket.
12060 ((memq char-before-ip '(?\( ?\[))
12061 (goto-char containing-sexp)
12062 (setq placeholder (c-point 'boi))
12063 (if (and (c-safe (backward-up-list 1) t)
12064 (>= (point) placeholder))
12065 (progn
12066 (forward-char)
12067 (skip-chars-forward " \t"))
12068 (goto-char placeholder))
12069 (c-add-stmt-syntax 'arglist-intro (list containing-sexp) t
12070 (c-most-enclosing-brace paren-state (point))
12071 paren-state))
12073 ;; CASE 7D: we are inside a conditional test clause. treat
12074 ;; these things as statements
12075 ((progn
12076 (goto-char containing-sexp)
12077 (and (c-safe (c-forward-sexp -1) t)
12078 (looking-at "\\<for\\>[^_]")))
12079 (goto-char (1+ containing-sexp))
12080 (c-forward-syntactic-ws indent-point)
12081 (if (eq char-before-ip ?\;)
12082 (c-add-syntax 'statement (point))
12083 (c-add-syntax 'statement-cont (point))
12086 ;; CASE 7E: maybe a continued ObjC method call. This is the
12087 ;; case when we are inside a [] bracketed exp, and what
12088 ;; precede the opening bracket is not an identifier.
12089 ((and c-opt-method-key
12090 (eq (char-after containing-sexp) ?\[)
12091 (progn
12092 (goto-char (1- containing-sexp))
12093 (c-backward-syntactic-ws (c-point 'bod))
12094 (if (not (looking-at c-symbol-key))
12095 (c-add-syntax 'objc-method-call-cont containing-sexp))
12098 ;; CASE 7F: we are looking at an arglist continuation line,
12099 ;; but the preceding argument is on the same line as the
12100 ;; opening paren. This case includes multi-line
12101 ;; mathematical paren groupings, but we could be on a
12102 ;; for-list continuation line. C.f. case 7A.
12103 ((progn
12104 (goto-char (1+ containing-sexp))
12105 (< (save-excursion
12106 (c-forward-syntactic-ws)
12107 (point))
12108 (c-point 'bonl)))
12109 (goto-char containing-sexp) ; paren opening the arglist
12110 (setq placeholder (c-point 'boi))
12111 (if (and (c-safe (backward-up-list 1) t)
12112 (>= (point) placeholder))
12113 (progn
12114 (forward-char)
12115 (skip-chars-forward " \t"))
12116 (goto-char placeholder))
12117 (c-add-stmt-syntax 'arglist-cont-nonempty (list containing-sexp) t
12118 (c-most-enclosing-brace c-state-cache (point))
12119 paren-state))
12121 ;; CASE 7G: we are looking at just a normal arglist
12122 ;; continuation line
12123 (t (c-forward-syntactic-ws indent-point)
12124 (c-add-syntax 'arglist-cont (c-point 'boi)))
12127 ;; CASE 8: func-local multi-inheritance line
12128 ((and (c-major-mode-is 'c++-mode)
12129 (save-excursion
12130 (goto-char indent-point)
12131 (skip-chars-forward " \t")
12132 (looking-at c-opt-postfix-decl-spec-key)))
12133 (goto-char indent-point)
12134 (skip-chars-forward " \t")
12135 (cond
12137 ;; CASE 8A: non-hanging colon on an inher intro
12138 ((eq char-after-ip ?:)
12139 (c-backward-syntactic-ws lim)
12140 (c-add-syntax 'inher-intro (c-point 'boi)))
12142 ;; CASE 8B: hanging colon on an inher intro
12143 ((eq char-before-ip ?:)
12144 (c-add-syntax 'inher-intro (c-point 'boi)))
12146 ;; CASE 8C: a continued inheritance line
12148 (c-beginning-of-inheritance-list lim)
12149 (c-add-syntax 'inher-cont (point))
12152 ;; CASE 9: we are inside a brace-list
12153 ((and (not (c-major-mode-is 'awk-mode)) ; Maybe this isn't needed (ACM, 2002/3/29)
12154 (setq special-brace-list
12155 (or (and c-special-brace-lists ;;;; ALWAYS NIL FOR AWK!!
12156 (save-excursion
12157 (goto-char containing-sexp)
12158 (c-looking-at-special-brace-list)))
12159 (c-inside-bracelist-p containing-sexp paren-state))))
12160 (cond
12162 ;; CASE 9A: In the middle of a special brace list opener.
12163 ((and (consp special-brace-list)
12164 (save-excursion
12165 (goto-char containing-sexp)
12166 (eq (char-after) ?\())
12167 (eq char-after-ip (car (cdr special-brace-list))))
12168 (goto-char (car (car special-brace-list)))
12169 (skip-chars-backward " \t")
12170 (if (and (bolp)
12171 (assoc 'statement-cont
12172 (setq placeholder (c-guess-basic-syntax))))
12173 (setq c-syntactic-context placeholder)
12174 (c-beginning-of-statement-1
12175 (c-safe-position (1- containing-sexp) paren-state))
12176 (c-forward-token-2 0)
12177 (while (cond
12178 ((looking-at c-specifier-key)
12179 (c-forward-keyword-clause 1))
12180 ((and c-opt-cpp-prefix
12181 (looking-at c-noise-macro-with-parens-name-re))
12182 (c-forward-noise-clause))))
12183 (c-add-syntax 'brace-list-open (c-point 'boi))))
12185 ;; CASE 9B: brace-list-close brace
12186 ((if (consp special-brace-list)
12187 ;; Check special brace list closer.
12188 (progn
12189 (goto-char (car (car special-brace-list)))
12190 (save-excursion
12191 (goto-char indent-point)
12192 (back-to-indentation)
12194 ;; We were between the special close char and the `)'.
12195 (and (eq (char-after) ?\))
12196 (eq (1+ (point)) (cdr (car special-brace-list))))
12197 ;; We were before the special close char.
12198 (and (eq (char-after) (cdr (cdr special-brace-list)))
12199 (zerop (c-forward-token-2))
12200 (eq (1+ (point)) (cdr (car special-brace-list)))))))
12201 ;; Normal brace list check.
12202 (and (eq char-after-ip ?})
12203 (c-safe (goto-char (c-up-list-backward (point))) t)
12204 (= (point) containing-sexp)))
12205 (if (eq (point) (c-point 'boi))
12206 (c-add-syntax 'brace-list-close (point))
12207 (setq lim (c-most-enclosing-brace c-state-cache (point)))
12208 (c-beginning-of-statement-1 lim nil nil t)
12209 (c-add-stmt-syntax 'brace-list-close nil t lim paren-state)))
12212 ;; Prepare for the rest of the cases below by going to the
12213 ;; token following the opening brace
12214 (if (consp special-brace-list)
12215 (progn
12216 (goto-char (car (car special-brace-list)))
12217 (c-forward-token-2 1 nil indent-point))
12218 (goto-char containing-sexp))
12219 (forward-char)
12220 (let ((start (point)))
12221 (c-forward-syntactic-ws indent-point)
12222 (goto-char (max start (c-point 'bol))))
12223 (c-skip-ws-forward indent-point)
12224 (cond
12226 ;; CASE 9C: we're looking at the first line in a brace-list
12227 ((= (point) indent-point)
12228 (if (consp special-brace-list)
12229 (goto-char (car (car special-brace-list)))
12230 (goto-char containing-sexp))
12231 (if (eq (point) (c-point 'boi))
12232 (c-add-syntax 'brace-list-intro (point))
12233 (setq lim (c-most-enclosing-brace c-state-cache (point)))
12234 (c-beginning-of-statement-1 lim)
12235 (c-add-stmt-syntax 'brace-list-intro nil t lim paren-state)))
12237 ;; CASE 9D: this is just a later brace-list-entry or
12238 ;; brace-entry-open
12239 (t (if (or (eq char-after-ip ?{)
12240 (and c-special-brace-lists
12241 (save-excursion
12242 (goto-char indent-point)
12243 (c-forward-syntactic-ws (c-point 'eol))
12244 (c-looking-at-special-brace-list (point)))))
12245 (c-add-syntax 'brace-entry-open (point))
12246 (c-add-syntax 'brace-list-entry (point))
12248 ))))
12250 ;; CASE 10: A continued statement or top level construct.
12251 ((and (not (memq char-before-ip '(?\; ?:)))
12252 (not (c-at-vsemi-p before-ws-ip))
12253 (or (not (eq char-before-ip ?}))
12254 (c-looking-at-inexpr-block-backward c-state-cache))
12255 (> (point)
12256 (save-excursion
12257 (c-beginning-of-statement-1 containing-sexp)
12258 (setq placeholder (point))))
12259 (/= placeholder containing-sexp))
12260 ;; This is shared with case 18.
12261 (c-guess-continued-construct indent-point
12262 char-after-ip
12263 placeholder
12264 containing-sexp
12265 paren-state))
12267 ;; CASE 16: block close brace, possibly closing the defun or
12268 ;; the class
12269 ((eq char-after-ip ?})
12270 ;; From here on we have the next containing sexp in lim.
12271 (setq lim (c-most-enclosing-brace paren-state))
12272 (goto-char containing-sexp)
12273 (cond
12275 ;; CASE 16E: Closing a statement block? This catches
12276 ;; cases where it's preceded by a statement keyword,
12277 ;; which works even when used in an "invalid" context,
12278 ;; e.g. a macro argument.
12279 ((c-after-conditional)
12280 (c-backward-to-block-anchor lim)
12281 (c-add-stmt-syntax 'block-close nil t lim paren-state))
12283 ;; CASE 16A: closing a lambda defun or an in-expression
12284 ;; block? C.f. cases 4, 7B and 17E.
12285 ((setq placeholder (c-looking-at-inexpr-block
12286 (c-safe-position containing-sexp paren-state)
12287 nil))
12288 (setq tmpsymbol (if (eq (car placeholder) 'inlambda)
12289 'inline-close
12290 'block-close))
12291 (goto-char containing-sexp)
12292 (back-to-indentation)
12293 (if (= containing-sexp (point))
12294 (c-add-syntax tmpsymbol (point))
12295 (goto-char (cdr placeholder))
12296 (back-to-indentation)
12297 (c-add-stmt-syntax tmpsymbol nil t
12298 (c-most-enclosing-brace paren-state (point))
12299 paren-state)
12300 (if (/= (point) (cdr placeholder))
12301 (c-add-syntax (car placeholder)))))
12303 ;; CASE 16B: does this close an inline or a function in
12304 ;; a non-class declaration level block?
12305 ((save-excursion
12306 (and lim
12307 (progn
12308 (goto-char lim)
12309 (c-looking-at-decl-block
12310 (c-most-enclosing-brace paren-state lim)
12311 nil))
12312 (setq placeholder (point))))
12313 (c-backward-to-decl-anchor lim)
12314 (back-to-indentation)
12315 (if (save-excursion
12316 (goto-char placeholder)
12317 (looking-at c-other-decl-block-key))
12318 (c-add-syntax 'defun-close (point))
12319 (c-add-syntax 'inline-close (point))))
12321 ;; CASE 16F: Can be a defun-close of a function declared
12322 ;; in a statement block, e.g. in Pike or when using gcc
12323 ;; extensions, but watch out for macros followed by
12324 ;; blocks. Let it through to be handled below.
12325 ;; C.f. cases B.3 and 17G.
12326 ((save-excursion
12327 (and (not (c-at-statement-start-p))
12328 (eq (c-beginning-of-statement-1 lim nil nil t) 'same)
12329 (setq placeholder (point))
12330 (let ((c-recognize-typeless-decls nil))
12331 ;; Turn off recognition of constructs that
12332 ;; lacks a type in this case, since that's more
12333 ;; likely to be a macro followed by a block.
12334 (c-forward-decl-or-cast-1 (c-point 'bosws) nil nil))))
12335 (back-to-indentation)
12336 (if (/= (point) containing-sexp)
12337 (goto-char placeholder))
12338 (c-add-stmt-syntax 'defun-close nil t lim paren-state))
12340 ;; CASE 16C: If there is an enclosing brace then this is
12341 ;; a block close since defun closes inside declaration
12342 ;; level blocks have been handled above.
12343 (lim
12344 ;; If the block is preceded by a case/switch label on
12345 ;; the same line, we anchor at the first preceding label
12346 ;; at boi. The default handling in c-add-stmt-syntax
12347 ;; really fixes it better, but we do like this to keep
12348 ;; the indentation compatible with version 5.28 and
12349 ;; earlier. C.f. case 17H.
12350 (while (and (/= (setq placeholder (point)) (c-point 'boi))
12351 (eq (c-beginning-of-statement-1 lim) 'label)))
12352 (goto-char placeholder)
12353 (if (looking-at c-label-kwds-regexp)
12354 (c-add-syntax 'block-close (point))
12355 (goto-char containing-sexp)
12356 ;; c-backward-to-block-anchor not necessary here; those
12357 ;; situations are handled in case 16E above.
12358 (c-add-stmt-syntax 'block-close nil t lim paren-state)))
12360 ;; CASE 16D: Only top level defun close left.
12362 (goto-char containing-sexp)
12363 (c-backward-to-decl-anchor lim)
12364 (c-add-stmt-syntax 'defun-close nil nil
12365 (c-most-enclosing-brace paren-state)
12366 paren-state))
12369 ;; CASE 19: line is an expression, not a statement, and is directly
12370 ;; contained by a template delimiter. Most likely, we are in a
12371 ;; template arglist within a statement. This case is based on CASE
12372 ;; 7. At some point in the future, we may wish to create more
12373 ;; syntactic symbols such as `template-intro',
12374 ;; `template-cont-nonempty', etc., and distinguish between them as we
12375 ;; do for `arglist-intro' etc. (2009-12-07).
12376 ((and c-recognize-<>-arglists
12377 (setq containing-< (c-up-list-backward indent-point containing-sexp))
12378 (eq (char-after containing-<) ?\<))
12379 (setq placeholder (c-point 'boi containing-<))
12380 (goto-char containing-sexp) ; Most nested Lbrace/Lparen (but not
12381 ; '<') before indent-point.
12382 (if (>= (point) placeholder)
12383 (progn
12384 (forward-char)
12385 (skip-chars-forward " \t"))
12386 (goto-char placeholder))
12387 (c-add-stmt-syntax 'template-args-cont (list containing-<) t
12388 (c-most-enclosing-brace c-state-cache (point))
12389 paren-state))
12391 ;; CASE 17: Statement or defun catchall.
12393 (goto-char indent-point)
12394 ;; Back up statements until we find one that starts at boi.
12395 (while (let* ((prev-point (point))
12396 (last-step-type (c-beginning-of-statement-1
12397 containing-sexp)))
12398 (if (= (point) prev-point)
12399 (progn
12400 (setq step-type (or step-type last-step-type))
12401 nil)
12402 (setq step-type last-step-type)
12403 (/= (point) (c-point 'boi)))))
12404 (cond
12406 ;; CASE 17B: continued statement
12407 ((and (eq step-type 'same)
12408 (/= (point) indent-point))
12409 (c-add-stmt-syntax 'statement-cont nil nil
12410 containing-sexp paren-state))
12412 ;; CASE 17A: After a case/default label?
12413 ((progn
12414 (while (and (eq step-type 'label)
12415 (not (looking-at c-label-kwds-regexp)))
12416 (setq step-type
12417 (c-beginning-of-statement-1 containing-sexp)))
12418 (eq step-type 'label))
12419 (c-add-stmt-syntax (if (eq char-after-ip ?{)
12420 'statement-case-open
12421 'statement-case-intro)
12422 nil t containing-sexp paren-state))
12424 ;; CASE 17D: any old statement
12425 ((progn
12426 (while (eq step-type 'label)
12427 (setq step-type
12428 (c-beginning-of-statement-1 containing-sexp)))
12429 (eq step-type 'previous))
12430 (c-add-stmt-syntax 'statement nil t
12431 containing-sexp paren-state)
12432 (if (eq char-after-ip ?{)
12433 (c-add-syntax 'block-open)))
12435 ;; CASE 17I: Inside a substatement block.
12436 ((progn
12437 ;; The following tests are all based on containing-sexp.
12438 (goto-char containing-sexp)
12439 ;; From here on we have the next containing sexp in lim.
12440 (setq lim (c-most-enclosing-brace paren-state containing-sexp))
12441 (c-after-conditional))
12442 (c-backward-to-block-anchor lim)
12443 (c-add-stmt-syntax 'statement-block-intro nil t
12444 lim paren-state)
12445 (if (eq char-after-ip ?{)
12446 (c-add-syntax 'block-open)))
12448 ;; CASE 17E: first statement in an in-expression block.
12449 ;; C.f. cases 4, 7B and 16A.
12450 ((setq placeholder (c-looking-at-inexpr-block
12451 (c-safe-position containing-sexp paren-state)
12452 nil))
12453 (setq tmpsymbol (if (eq (car placeholder) 'inlambda)
12454 'defun-block-intro
12455 'statement-block-intro))
12456 (back-to-indentation)
12457 (if (= containing-sexp (point))
12458 (c-add-syntax tmpsymbol (point))
12459 (goto-char (cdr placeholder))
12460 (back-to-indentation)
12461 (c-add-stmt-syntax tmpsymbol nil t
12462 (c-most-enclosing-brace c-state-cache (point))
12463 paren-state)
12464 (if (/= (point) (cdr placeholder))
12465 (c-add-syntax (car placeholder))))
12466 (if (eq char-after-ip ?{)
12467 (c-add-syntax 'block-open)))
12469 ;; CASE 17F: first statement in an inline, or first
12470 ;; statement in a top-level defun. we can tell this is it
12471 ;; if there are no enclosing braces that haven't been
12472 ;; narrowed out by a class (i.e. don't use bod here).
12473 ((save-excursion
12474 (or (not (setq placeholder (c-most-enclosing-brace
12475 paren-state)))
12476 (and (progn
12477 (goto-char placeholder)
12478 (eq (char-after) ?{))
12479 (c-looking-at-decl-block (c-most-enclosing-brace
12480 paren-state (point))
12481 nil))))
12482 (c-backward-to-decl-anchor lim)
12483 (back-to-indentation)
12484 (c-add-syntax 'defun-block-intro (point)))
12486 ;; CASE 17G: First statement in a function declared inside
12487 ;; a normal block. This can occur in Pike and with
12488 ;; e.g. the gcc extensions, but watch out for macros
12489 ;; followed by blocks. C.f. cases B.3 and 16F.
12490 ((save-excursion
12491 (and (not (c-at-statement-start-p))
12492 (eq (c-beginning-of-statement-1 lim nil nil t) 'same)
12493 (setq placeholder (point))
12494 (let ((c-recognize-typeless-decls nil))
12495 ;; Turn off recognition of constructs that lacks
12496 ;; a type in this case, since that's more likely
12497 ;; to be a macro followed by a block.
12498 (c-forward-decl-or-cast-1 (c-point 'bosws) nil nil))))
12499 (back-to-indentation)
12500 (if (/= (point) containing-sexp)
12501 (goto-char placeholder))
12502 (c-add-stmt-syntax 'defun-block-intro nil t
12503 lim paren-state))
12505 ;; CASE 17H: First statement in a block.
12507 ;; If the block is preceded by a case/switch label on the
12508 ;; same line, we anchor at the first preceding label at
12509 ;; boi. The default handling in c-add-stmt-syntax is
12510 ;; really fixes it better, but we do like this to keep the
12511 ;; indentation compatible with version 5.28 and earlier.
12512 ;; C.f. case 16C.
12513 (while (and (/= (setq placeholder (point)) (c-point 'boi))
12514 (eq (c-beginning-of-statement-1 lim) 'label)))
12515 (goto-char placeholder)
12516 (if (looking-at c-label-kwds-regexp)
12517 (c-add-syntax 'statement-block-intro (point))
12518 (goto-char containing-sexp)
12519 ;; c-backward-to-block-anchor not necessary here; those
12520 ;; situations are handled in case 17I above.
12521 (c-add-stmt-syntax 'statement-block-intro nil t
12522 lim paren-state))
12523 (if (eq char-after-ip ?{)
12524 (c-add-syntax 'block-open)))
12528 ;; now we need to look at any modifiers
12529 (goto-char indent-point)
12530 (skip-chars-forward " \t")
12532 ;; are we looking at a comment only line?
12533 (when (and (looking-at c-comment-start-regexp)
12534 (/= (c-forward-token-2 0 nil (c-point 'eol)) 0))
12535 (c-append-syntax 'comment-intro))
12537 ;; we might want to give additional offset to friends (in C++).
12538 (when (and c-opt-friend-key
12539 (looking-at c-opt-friend-key))
12540 (c-append-syntax 'friend))
12542 ;; Set syntactic-relpos.
12543 (let ((p c-syntactic-context))
12544 (while (and p
12545 (if (integerp (c-langelem-pos (car p)))
12546 (progn
12547 (setq syntactic-relpos (c-langelem-pos (car p)))
12548 nil)
12550 (setq p (cdr p))))
12552 ;; Start of or a continuation of a preprocessor directive?
12553 (if (and macro-start
12554 (eq macro-start (c-point 'boi))
12555 (not (and (c-major-mode-is 'pike-mode)
12556 (eq (char-after (1+ macro-start)) ?\"))))
12557 (c-append-syntax 'cpp-macro)
12558 (when (and c-syntactic-indentation-in-macros macro-start)
12559 (if in-macro-expr
12560 (when (or
12561 (< syntactic-relpos macro-start)
12562 (not (or
12563 (assq 'arglist-intro c-syntactic-context)
12564 (assq 'arglist-cont c-syntactic-context)
12565 (assq 'arglist-cont-nonempty c-syntactic-context)
12566 (assq 'arglist-close c-syntactic-context))))
12567 ;; If inside a cpp expression, i.e. anywhere in a
12568 ;; cpp directive except a #define body, we only let
12569 ;; through the syntactic analysis that is internal
12570 ;; in the expression. That means the arglist
12571 ;; elements, if they are anchored inside the cpp
12572 ;; expression.
12573 (setq c-syntactic-context nil)
12574 (c-add-syntax 'cpp-macro-cont macro-start))
12575 (when (and (eq macro-start syntactic-relpos)
12576 (not (assq 'cpp-define-intro c-syntactic-context))
12577 (save-excursion
12578 (goto-char macro-start)
12579 (or (not (c-forward-to-cpp-define-body))
12580 (<= (point) (c-point 'boi indent-point)))))
12581 ;; Inside a #define body and the syntactic analysis is
12582 ;; anchored on the start of the #define. In this case
12583 ;; we add cpp-define-intro to get the extra
12584 ;; indentation of the #define body.
12585 (c-add-syntax 'cpp-define-intro)))))
12587 ;; return the syntax
12588 c-syntactic-context)))
12591 ;; Indentation calculation.
12593 (defun c-evaluate-offset (offset langelem symbol)
12594 ;; offset can be a number, a function, a variable, a list, or one of
12595 ;; the symbols + or -
12597 ;; This function might do hidden buffer changes.
12598 (let ((res
12599 (cond
12600 ((numberp offset) offset)
12601 ((vectorp offset) offset)
12602 ((null offset) nil)
12604 ((eq offset '+) c-basic-offset)
12605 ((eq offset '-) (- c-basic-offset))
12606 ((eq offset '++) (* 2 c-basic-offset))
12607 ((eq offset '--) (* 2 (- c-basic-offset)))
12608 ((eq offset '*) (/ c-basic-offset 2))
12609 ((eq offset '/) (/ (- c-basic-offset) 2))
12611 ((functionp offset)
12612 (c-evaluate-offset
12613 (funcall offset
12614 (cons (c-langelem-sym langelem)
12615 (c-langelem-pos langelem)))
12616 langelem symbol))
12618 ((listp offset)
12619 (cond
12620 ((eq (car offset) 'quote)
12621 (c-benign-error "The offset %S for %s was mistakenly quoted"
12622 offset symbol)
12623 nil)
12625 ((memq (car offset) '(min max))
12626 (let (res val (method (car offset)))
12627 (setq offset (cdr offset))
12628 (while offset
12629 (setq val (c-evaluate-offset (car offset) langelem symbol))
12630 (cond
12631 ((not val))
12632 ((not res)
12633 (setq res val))
12634 ((integerp val)
12635 (if (vectorp res)
12636 (c-benign-error "\
12637 Error evaluating offset %S for %s: \
12638 Cannot combine absolute offset %S with relative %S in `%s' method"
12639 (car offset) symbol res val method)
12640 (setq res (funcall method res val))))
12642 (if (integerp res)
12643 (c-benign-error "\
12644 Error evaluating offset %S for %s: \
12645 Cannot combine relative offset %S with absolute %S in `%s' method"
12646 (car offset) symbol res val method)
12647 (setq res (vector (funcall method (aref res 0)
12648 (aref val 0)))))))
12649 (setq offset (cdr offset)))
12650 res))
12652 ((eq (car offset) 'add)
12653 (let (res val)
12654 (setq offset (cdr offset))
12655 (while offset
12656 (setq val (c-evaluate-offset (car offset) langelem symbol))
12657 (cond
12658 ((not val))
12659 ((not res)
12660 (setq res val))
12661 ((integerp val)
12662 (if (vectorp res)
12663 (setq res (vector (+ (aref res 0) val)))
12664 (setq res (+ res val))))
12666 (if (vectorp res)
12667 (c-benign-error "\
12668 Error evaluating offset %S for %s: \
12669 Cannot combine absolute offsets %S and %S in `add' method"
12670 (car offset) symbol res val)
12671 (setq res val)))) ; Override.
12672 (setq offset (cdr offset)))
12673 res))
12676 (let (res)
12677 (when (eq (car offset) 'first)
12678 (setq offset (cdr offset)))
12679 (while (and (not res) offset)
12680 (setq res (c-evaluate-offset (car offset) langelem symbol)
12681 offset (cdr offset)))
12682 res))))
12684 ((and (symbolp offset) (boundp offset))
12685 (symbol-value offset))
12688 (c-benign-error "Unknown offset format %S for %s" offset symbol)
12689 nil))))
12691 (if (or (null res) (integerp res)
12692 (and (vectorp res) (= (length res) 1) (integerp (aref res 0))))
12694 (c-benign-error "Error evaluating offset %S for %s: Got invalid value %S"
12695 offset symbol res)
12696 nil)))
12698 (defun c-calc-offset (langelem)
12699 ;; Get offset from LANGELEM which is a list beginning with the
12700 ;; syntactic symbol and followed by any analysis data it provides.
12701 ;; That data may be zero or more elements, but if at least one is
12702 ;; given then the first is the anchor position (or nil). The symbol
12703 ;; is matched against `c-offsets-alist' and the offset calculated
12704 ;; from that is returned.
12706 ;; This function might do hidden buffer changes.
12707 (let* ((symbol (c-langelem-sym langelem))
12708 (match (assq symbol c-offsets-alist))
12709 (offset (cdr-safe match)))
12710 (if match
12711 (setq offset (c-evaluate-offset offset langelem symbol))
12712 (if c-strict-syntax-p
12713 (c-benign-error "No offset found for syntactic symbol %s" symbol))
12714 (setq offset 0))
12715 (if (vectorp offset)
12716 offset
12717 (or (and (numberp offset) offset)
12718 (and (symbolp offset) (symbol-value offset))
12722 (defun c-get-offset (langelem)
12723 ;; This is a compatibility wrapper for `c-calc-offset' in case
12724 ;; someone is calling it directly. It takes an old style syntactic
12725 ;; element on the form (SYMBOL . ANCHOR-POS) and converts it to the
12726 ;; new list form.
12728 ;; This function might do hidden buffer changes.
12729 (if (c-langelem-pos langelem)
12730 (c-calc-offset (list (c-langelem-sym langelem)
12731 (c-langelem-pos langelem)))
12732 (c-calc-offset langelem)))
12734 (defun c-get-syntactic-indentation (langelems)
12735 ;; Calculate the syntactic indentation from a syntactic description
12736 ;; as returned by `c-guess-syntax'.
12738 ;; Note that topmost-intro always has an anchor position at bol, for
12739 ;; historical reasons. It's often used together with other symbols
12740 ;; that has more sane positions. Since we always use the first
12741 ;; found anchor position, we rely on that these other symbols always
12742 ;; precede topmost-intro in the LANGELEMS list.
12744 ;; This function might do hidden buffer changes.
12745 (let ((indent 0) anchor)
12747 (while langelems
12748 (let* ((c-syntactic-element (car langelems))
12749 (res (c-calc-offset c-syntactic-element)))
12751 (if (vectorp res)
12752 ;; Got an absolute column that overrides any indentation
12753 ;; we've collected so far, but not the relative
12754 ;; indentation we might get for the nested structures
12755 ;; further down the langelems list.
12756 (setq indent (elt res 0)
12757 anchor (point-min)) ; A position at column 0.
12759 ;; Got a relative change of the current calculated
12760 ;; indentation.
12761 (setq indent (+ indent res))
12763 ;; Use the anchor position from the first syntactic
12764 ;; element with one.
12765 (unless anchor
12766 (setq anchor (c-langelem-pos (car langelems)))))
12768 (setq langelems (cdr langelems))))
12770 (if anchor
12771 (+ indent (save-excursion
12772 (goto-char anchor)
12773 (current-column)))
12774 indent)))
12777 (cc-provide 'cc-engine)
12779 ;; Local Variables:
12780 ;; indent-tabs-mode: t
12781 ;; tab-width: 8
12782 ;; End:
12783 ;;; cc-engine.el ends here