* floatfns.c (Ftan): Use tan (x), not (sin (x) / cos (x)).
[bpt/emacs.git] / lisp / progmodes / cc-engine.el
Content-type: text/html HCoop Git - bpt/emacs.git/blame - lisp/progmodes/cc-engine.el


500 - Internal Server Error

Malformed UTF-8 character (fatal) at (eval 8) line 1, <$fd> line 18019.
CommitLineData
785eecbb
RS
1;;; cc-engine.el --- core syntax guessing engine for CC mode
2
acaf905b 3;; Copyright (C) 1985, 1987, 1992-2012 Free Software Foundation, Inc.
785eecbb 4
e309f66c
AM
5;; Authors: 2001- Alan Mackenzie
6;; 1998- Martin Stjernholm
d9e94c22 7;; 1992-1999 Barry A. Warsaw
5858f68c
GM
8;; 1987 Dave Detlefs
9;; 1987 Stewart Clamen
785eecbb 10;; 1985 Richard M. Stallman
0ec8351b 11;; Maintainer: bug-cc-mode@gnu.org
785eecbb 12;; Created: 22-Apr-1997 (split from cc-mode.el)
bd78fa1d
CY
13;; Keywords: c languages
14;; Package: cc-mode
785eecbb
RS
15
16;; This file is part of GNU Emacs.
17
b1fc2b50 18;; GNU Emacs is free software: you can redistribute it and/or modify
785eecbb 19;; it under the terms of the GNU General Public License as published by
b1fc2b50
GM
20;; the Free Software Foundation, either version 3 of the License, or
21;; (at your option) any later version.
785eecbb
RS
22
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.
27
28;; You should have received a copy of the GNU General Public License
b1fc2b50 29;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
785eecbb 30
3afbc435
PJ
31;;; Commentary:
32
a66cd3ee
MS
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.
38;;
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.)
42
d9e94c22
MS
43;; Hidden buffer changes
44;;
45;; Various functions in CC Mode use text properties for caching and
46;; syntactic markup purposes, and those of them that might modify such
0386b551
AM
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.
d9e94c22 51;;
0386b551
AM
52;; Interactive functions are assumed to not do hidden buffer changes,
53;; except in the specific parts of them that do real changes.
d9e94c22 54;;
0386b551
AM
55;; Lineup functions are assumed to do hidden buffer changes. They
56;; must not do real changes, though.
d9e94c22 57;;
0386b551
AM
58;; All other functions that do hidden buffer changes have that noted
59;; in their doc string or comment.
60;;
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.
65;;
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).
d9e94c22
MS
70
71;; Use of text properties
72;;
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.
76;;
7bfc3fdb
MS
77;; Note: This doc is for internal use only. Other packages should not
78;; assume that these text properties are used as described here.
79;;
0ec1d2c5
AM
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.
83;;
d9e94c22 84;; 'syntax-table
c8018ede
AM
85;; Used to modify the syntax of some characters. It is used to
86;; mark the "<" and ">" of angle bracket parens with paren syntax, and
87;; to "hide" obtrusive characters in preprocessor lines.
d9e94c22
MS
88;;
89;; This property is used on single characters and is therefore
90;; always treated as front and rear nonsticky (or start and end open
91;; in XEmacs vocabulary). It's therefore installed on
92;; `text-property-default-nonsticky' if that variable exists (Emacs
93;; >= 21).
94;;
95;; 'c-is-sws and 'c-in-sws
96;; Used by `c-forward-syntactic-ws' and `c-backward-syntactic-ws' to
97;; speed them up. See the comment blurb before `c-put-is-sws'
98;; below for further details.
99;;
100;; 'c-type
101;; This property is used on single characters to mark positions with
0386b551
AM
102;; special syntactic relevance of various sorts. Its primary use is
103;; to avoid glitches when multiline constructs are refontified
d9e94c22
MS
104;; interactively (on font lock decoration level 3). It's cleared in
105;; a region before it's fontified and is then put on relevant chars
106;; in that region as they are encountered during the fontification.
107;; The value specifies the kind of position:
108;;
109;; 'c-decl-arg-start
110;; Put on the last char of the token preceding each declaration
111;; inside a declaration style arglist (typically in a function
112;; prototype).
113;;
114;; 'c-decl-end
115;; Put on the last char of the token preceding a declaration.
116;; This is used in cases where declaration boundaries can't be
117;; recognized simply by looking for a token like ";" or "}".
118;; `c-type-decl-end-used' must be set if this is used (see also
119;; `c-find-decl-spots').
120;;
121;; 'c-<>-arg-sep
122;; Put on the commas that separate arguments in angle bracket
123;; arglists like C++ template arglists.
124;;
125;; 'c-decl-id-start and 'c-decl-type-start
126;; Put on the last char of the token preceding each declarator
127;; in the declarator list of a declaration. They are also used
128;; between the identifiers cases like enum declarations.
129;; 'c-decl-type-start is used when the declarators are types,
130;; 'c-decl-id-start otherwise.
131;;
132;; 'c-awk-NL-prop
133;; Used in AWK mode to mark the various kinds of newlines. See
134;; cc-awk.el.
135
3afbc435
PJ
136;;; Code:
137
0ec8351b 138(eval-when-compile
51f606de 139 (let ((load-path
130c507e
GM
140 (if (and (boundp 'byte-compile-dest-file)
141 (stringp byte-compile-dest-file))
142 (cons (file-name-directory byte-compile-dest-file) load-path)
51f606de 143 load-path)))
d9e94c22 144 (load "cc-bytecomp" nil t)))
130c507e
GM
145
146(cc-require 'cc-defs)
d9e94c22 147(cc-require-when-compile 'cc-langs)
130c507e 148(cc-require 'cc-vars)
d9e94c22 149
130c507e
GM
150;; Silence the compiler.
151(cc-bytecomp-defun buffer-syntactic-context) ; XEmacs
0ec8351b 152
51f606de 153\f
d9e94c22
MS
154;; Make declarations for all the `c-lang-defvar' variables in cc-langs.
155
156(defmacro c-declare-lang-variables ()
157 `(progn
485fe977
RS
158 ,@(apply 'nconc
159 (mapcar (lambda (init)
160 `(,(if (elt init 2)
161 `(defvar ,(car init) nil ,(elt init 2))
162 `(defvar ,(car init) nil))
163 (make-variable-buffer-local ',(car init))))
164 (cdr c-lang-variable-inits)))))
d9e94c22
MS
165(c-declare-lang-variables)
166
167\f
168;;; Internal state variables.
169
170;; Internal state of hungry delete key feature
171(defvar c-hungry-delete-key nil)
172(make-variable-buffer-local 'c-hungry-delete-key)
173
0386b551
AM
174;; The electric flag (toggled by `c-toggle-electric-state').
175;; If t, electric actions (like automatic reindentation, and (if
176;; c-auto-newline is also set) auto newlining) will happen when an electric
177;; key like `{' is pressed (or an electric keyword like `else').
178(defvar c-electric-flag t)
179(make-variable-buffer-local 'c-electric-flag)
180
d9e94c22
MS
181;; Internal state of auto newline feature.
182(defvar c-auto-newline nil)
183(make-variable-buffer-local 'c-auto-newline)
184
0386b551 185;; Included in the mode line to indicate the active submodes.
cb694ab7
AM
186;; (defvar c-submode-indicators nil)
187;; (make-variable-buffer-local 'c-submode-indicators)
d9e94c22 188
a66cd3ee
MS
189(defun c-calculate-state (arg prevstate)
190 ;; Calculate the new state of PREVSTATE, t or nil, based on arg. If
191 ;; arg is nil or zero, toggle the state. If arg is negative, turn
192 ;; the state off, and if arg is positive, turn the state on
193 (if (or (not arg)
194 (zerop (setq arg (prefix-numeric-value arg))))
195 (not prevstate)
196 (> arg 0)))
197
d9e94c22 198\f
037558bf
MS
199;; Basic handling of preprocessor directives.
200
201;; This is a dynamically bound cache used together with
202;; `c-query-macro-start' and `c-query-and-set-macro-start'. It only
203;; works as long as point doesn't cross a macro boundary.
204(defvar c-macro-start 'unknown)
205
206(defsubst c-query-and-set-macro-start ()
037558bf
MS
207 (if (symbolp c-macro-start)
208 (setq c-macro-start (save-excursion
0386b551
AM
209 (c-save-buffer-state ()
210 (and (c-beginning-of-macro)
211 (point)))))
037558bf
MS
212 c-macro-start))
213
214(defsubst c-query-macro-start ()
037558bf
MS
215 (if (symbolp c-macro-start)
216 (save-excursion
0386b551
AM
217 (c-save-buffer-state ()
218 (and (c-beginning-of-macro)
219 (point))))
037558bf
MS
220 c-macro-start))
221
56d093a9
AM
222;; One element macro cache to cope with continual movement within very large
223;; CPP macros.
224(defvar c-macro-cache nil)
225(make-variable-buffer-local 'c-macro-cache)
226;; Nil or cons of the bounds of the most recent CPP form probed by
227;; `c-beginning-of-macro', `c-end-of-macro' or `c-syntactic-end-of-macro'.
228;; The cdr will be nil if we know only the start of the CPP form.
229(defvar c-macro-cache-start-pos nil)
230(make-variable-buffer-local 'c-macro-cache-start-pos)
231;; The starting position from where we determined `c-macro-cache'.
232(defvar c-macro-cache-syntactic nil)
233(make-variable-buffer-local 'c-macro-cache-syntactic)
234;; non-nil iff `c-macro-cache' has both elements set AND the cdr is at a
235;; syntactic end of macro, not merely an apparent one.
236
237(defun c-invalidate-macro-cache (beg end)
238 ;; Called from a before-change function. If the change region is before or
f003f294 239 ;; in the macro characterized by `c-macro-cache' etc., nullify it
56d093a9
AM
240 ;; appropriately. BEG and END are the standard before-change-functions
241 ;; parameters. END isn't used.
242 (cond
243 ((null c-macro-cache))
244 ((< beg (car c-macro-cache))
245 (setq c-macro-cache nil
246 c-macro-cache-start-pos nil
247 c-macro-cache-syntactic nil))
248 ((and (cdr c-macro-cache)
249 (< beg (cdr c-macro-cache)))
250 (setcdr c-macro-cache nil)
251 (setq c-macro-cache-start-pos beg
252 c-macro-cache-syntactic nil))))
253
037558bf
MS
254(defun c-beginning-of-macro (&optional lim)
255 "Go to the beginning of a preprocessor directive.
256Leave point at the beginning of the directive and return t if in one,
257otherwise return nil and leave point unchanged.
258
0386b551
AM
259Note that this function might do hidden buffer changes. See the
260comment at the start of cc-engine.el for more info."
56d093a9
AM
261 (let ((here (point)))
262 (when c-opt-cpp-prefix
263 (if (and (car c-macro-cache)
264 (>= (point) (car c-macro-cache))
265 (or (and (cdr c-macro-cache)
266 (<= (point) (cdr c-macro-cache)))
267 (<= (point) c-macro-cache-start-pos)))
268 (unless (< (car c-macro-cache) (or lim (point-min)))
269 (progn (goto-char (max (or lim (point-min)) (car c-macro-cache)))
270 (setq c-macro-cache-start-pos
271 (max c-macro-cache-start-pos here))
272 t))
273 (setq c-macro-cache nil
274 c-macro-cache-start-pos nil
275 c-macro-cache-syntactic nil)
276
277 (save-restriction
278 (if lim (narrow-to-region lim (point-max)))
279 (beginning-of-line)
280 (while (eq (char-before (1- (point))) ?\\)
281 (forward-line -1))
282 (back-to-indentation)
283 (if (and (<= (point) here)
284 (looking-at c-opt-cpp-start))
285 (progn
286 (setq c-macro-cache (cons (point) nil)
287 c-macro-cache-start-pos here)
288 t)
289 (goto-char here)
290 nil))))))
037558bf
MS
291
292(defun c-end-of-macro ()
293 "Go to the end of a preprocessor directive.
0386b551
AM
294More accurately, move the point to the end of the closest following
295line that doesn't end with a line continuation backslash - no check is
296done that the point is inside a cpp directive to begin with.
037558bf 297
0386b551
AM
298Note that this function might do hidden buffer changes. See the
299comment at the start of cc-engine.el for more info."
56d093a9
AM
300 (if (and (cdr c-macro-cache)
301 (<= (point) (cdr c-macro-cache))
302 (>= (point) (car c-macro-cache)))
303 (goto-char (cdr c-macro-cache))
304 (unless (and (car c-macro-cache)
305 (<= (point) c-macro-cache-start-pos)
306 (>= (point) (car c-macro-cache)))
307 (setq c-macro-cache nil
308 c-macro-cache-start-pos nil
309 c-macro-cache-syntactic nil))
310 (while (progn
311 (end-of-line)
312 (when (and (eq (char-before) ?\\)
313 (not (eobp)))
314 (forward-char)
315 t)))
316 (when (car c-macro-cache)
317 (setcdr c-macro-cache (point)))))
037558bf 318
0ec1d2c5
AM
319(defun c-syntactic-end-of-macro ()
320 ;; Go to the end of a CPP directive, or a "safe" pos just before.
321 ;;
322 ;; This is normally the end of the next non-escaped line. A "safe"
323 ;; position is one not within a string or comment. (The EOL on a line
324 ;; comment is NOT "safe").
325 ;;
326 ;; This function must only be called from the beginning of a CPP construct.
327 ;;
328 ;; Note that this function might do hidden buffer changes. See the comment
329 ;; at the start of cc-engine.el for more info.
330 (let* ((here (point))
331 (there (progn (c-end-of-macro) (point)))
56d093a9
AM
332 s)
333 (unless c-macro-cache-syntactic
334 (setq s (parse-partial-sexp here there))
335 (while (and (or (nth 3 s) ; in a string
336 (nth 4 s)) ; in a comment (maybe at end of line comment)
337 (> there here)) ; No infinite loops, please.
338 (setq there (1- (nth 8 s)))
339 (setq s (parse-partial-sexp here there)))
340 (setq c-macro-cache-syntactic (car c-macro-cache)))
0ec1d2c5
AM
341 (point)))
342
51c9af45
AM
343(defun c-forward-over-cpp-define-id ()
344 ;; Assuming point is at the "#" that introduces a preprocessor
345 ;; directive, it's moved forward to the end of the identifier which is
346 ;; "#define"d (or whatever c-opt-cpp-macro-define specifies). Non-nil
347 ;; is returned in this case, in all other cases nil is returned and
348 ;; point isn't moved.
349 ;;
350 ;; This function might do hidden buffer changes.
351 (when (and c-opt-cpp-macro-define-id
352 (looking-at c-opt-cpp-macro-define-id))
353 (goto-char (match-end 0))))
354
037558bf
MS
355(defun c-forward-to-cpp-define-body ()
356 ;; Assuming point is at the "#" that introduces a preprocessor
357 ;; directive, it's moved forward to the start of the definition body
0386b551
AM
358 ;; if it's a "#define" (or whatever c-opt-cpp-macro-define
359 ;; specifies). Non-nil is returned in this case, in all other cases
360 ;; nil is returned and point isn't moved.
361 ;;
362 ;; This function might do hidden buffer changes.
363 (when (and c-opt-cpp-macro-define-start
364 (looking-at c-opt-cpp-macro-define-start)
037558bf
MS
365 (not (= (match-end 0) (c-point 'eol))))
366 (goto-char (match-end 0))))
367
368\f
d9e94c22
MS
369;;; Basic utility functions.
370
0386b551 371(defun c-syntactic-content (from to paren-level)
d9e94c22
MS
372 ;; Return the given region as a string where all syntactic
373 ;; whitespace is removed or, where necessary, replaced with a single
0386b551
AM
374 ;; space. If PAREN-LEVEL is given then all parens in the region are
375 ;; collapsed to "()", "[]" etc.
376 ;;
377 ;; This function might do hidden buffer changes.
378
d9e94c22 379 (save-excursion
0386b551
AM
380 (save-restriction
381 (narrow-to-region from to)
382 (goto-char from)
383 (let* ((parts (list nil)) (tail parts) pos in-paren)
384
385 (while (re-search-forward c-syntactic-ws-start to t)
386 (goto-char (setq pos (match-beginning 0)))
387 (c-forward-syntactic-ws)
388 (if (= (point) pos)
389 (forward-char)
390
391 (when paren-level
392 (save-excursion
393 (setq in-paren (= (car (parse-partial-sexp from pos 1)) 1)
394 pos (point))))
395
396 (if (and (> pos from)
397 (< (point) to)
398 (looking-at "\\w\\|\\s_")
399 (save-excursion
400 (goto-char (1- pos))
401 (looking-at "\\w\\|\\s_")))
402 (progn
403 (setcdr tail (list (buffer-substring-no-properties from pos)
404 " "))
405 (setq tail (cddr tail)))
406 (setcdr tail (list (buffer-substring-no-properties from pos)))
407 (setq tail (cdr tail)))
408
409 (when in-paren
410 (when (= (car (parse-partial-sexp pos to -1)) -1)
411 (setcdr tail (list (buffer-substring-no-properties
412 (1- (point)) (point))))
413 (setq tail (cdr tail))))
414
415 (setq from (point))))
416
417 (setcdr tail (list (buffer-substring-no-properties from to)))
418 (apply 'concat (cdr parts))))))
419
420(defun c-shift-line-indentation (shift-amt)
421 ;; Shift the indentation of the current line with the specified
422 ;; amount (positive inwards). The buffer is modified only if
423 ;; SHIFT-AMT isn't equal to zero.
424 (let ((pos (- (point-max) (point)))
425 (c-macro-start c-macro-start)
426 tmp-char-inserted)
427 (if (zerop shift-amt)
428 nil
429 ;; If we're on an empty line inside a macro, we take the point
430 ;; to be at the current indentation and shift it to the
431 ;; appropriate column. This way we don't treat the extra
432 ;; whitespace out to the line continuation as indentation.
433 (when (and (c-query-and-set-macro-start)
434 (looking-at "[ \t]*\\\\$")
435 (save-excursion
436 (skip-chars-backward " \t")
437 (bolp)))
438 (insert ?x)
439 (backward-char)
440 (setq tmp-char-inserted t))
441 (unwind-protect
442 (let ((col (current-indentation)))
443 (delete-region (c-point 'bol) (c-point 'boi))
444 (beginning-of-line)
445 (indent-to (+ col shift-amt)))
446 (when tmp-char-inserted
447 (delete-char 1))))
448 ;; If initial point was within line's indentation and we're not on
449 ;; a line with a line continuation in a macro, position after the
450 ;; indentation. Else stay at same point in text.
451 (if (and (< (point) (c-point 'boi))
452 (not tmp-char-inserted))
453 (back-to-indentation)
454 (if (> (- (point-max) pos) (point))
455 (goto-char (- (point-max) pos))))))
d9e94c22
MS
456
457(defsubst c-keyword-sym (keyword)
458 ;; Return non-nil if the string KEYWORD is a known keyword. More
459 ;; precisely, the value is the symbol for the keyword in
460 ;; `c-keywords-obarray'.
461 (intern-soft keyword c-keywords-obarray))
462
463(defsubst c-keyword-member (keyword-sym lang-constant)
464 ;; Return non-nil if the symbol KEYWORD-SYM, as returned by
465 ;; `c-keyword-sym', is a member of LANG-CONSTANT, which is the name
466 ;; of a language constant that ends with "-kwds". If KEYWORD-SYM is
467 ;; nil then the result is nil.
468 (get keyword-sym lang-constant))
469
470;; String syntax chars, suitable for skip-syntax-(forward|backward).
471(defconst c-string-syntax (if (memq 'gen-string-delim c-emacs-features)
472 "\"|"
473 "\""))
474
0386b551 475;; Regexp matching string limit syntax.
d9e94c22
MS
476(defconst c-string-limit-regexp (if (memq 'gen-string-delim c-emacs-features)
477 "\\s\"\\|\\s|"
478 "\\s\""))
479
0386b551
AM
480;; Regexp matching WS followed by string limit syntax.
481(defconst c-ws*-string-limit-regexp
482 (concat "[ \t]*\\(" c-string-limit-regexp "\\)"))
483
d9e94c22
MS
484;; Holds formatted error strings for the few cases where parse errors
485;; are reported.
a66cd3ee 486(defvar c-parsing-error nil)
d9e94c22
MS
487(make-variable-buffer-local 'c-parsing-error)
488
489(defun c-echo-parsing-error (&optional quiet)
d9e94c22
MS
490 (when (and c-report-syntactic-errors c-parsing-error (not quiet))
491 (c-benign-error "%s" c-parsing-error))
492 c-parsing-error)
493
494;; Faces given to comments and string literals. This is used in some
495;; situations to speed up recognition; it isn't mandatory that font
496;; locking is in use. This variable is extended with the face in
497;; `c-doc-face-name' when fontification is activated in cc-fonts.el.
1e330469 498(defvar c-literal-faces
0386b551
AM
499 (append '(font-lock-comment-face font-lock-string-face)
500 (when (facep 'font-lock-comment-delimiter-face)
501 ;; New in Emacs 22.
502 '(font-lock-comment-delimiter-face))))
503
504(defsubst c-put-c-type-property (pos value)
505 ;; Put a c-type property with the given value at POS.
506 (c-put-char-property pos 'c-type value))
507
508(defun c-clear-c-type-property (from to value)
5a89f0a7 509 ;; Remove all occurrences of the c-type property that has the given
0386b551
AM
510 ;; value in the region between FROM and TO. VALUE is assumed to not
511 ;; be nil.
512 ;;
513 ;; Note: This assumes that c-type is put on single chars only; it's
514 ;; very inefficient if matching properties cover large regions.
515 (save-excursion
516 (goto-char from)
517 (while (progn
518 (when (eq (get-text-property (point) 'c-type) value)
519 (c-clear-char-property (point) 'c-type))
520 (goto-char (next-single-property-change (point) 'c-type nil to))
521 (< (point) to)))))
037558bf 522
d9e94c22
MS
523\f
524;; Some debug tools to visualize various special positions. This
525;; debug code isn't as portable as the rest of CC Mode.
526
527(cc-bytecomp-defun overlays-in)
528(cc-bytecomp-defun overlay-get)
529(cc-bytecomp-defun overlay-start)
530(cc-bytecomp-defun overlay-end)
531(cc-bytecomp-defun delete-overlay)
532(cc-bytecomp-defun overlay-put)
533(cc-bytecomp-defun make-overlay)
534
535(defun c-debug-add-face (beg end face)
536 (c-save-buffer-state ((overlays (overlays-in beg end)) overlay)
537 (while overlays
538 (setq overlay (car overlays)
539 overlays (cdr overlays))
540 (when (eq (overlay-get overlay 'face) face)
541 (setq beg (min beg (overlay-start overlay))
542 end (max end (overlay-end overlay)))
543 (delete-overlay overlay)))
544 (overlay-put (make-overlay beg end) 'face face)))
545
546(defun c-debug-remove-face (beg end face)
547 (c-save-buffer-state ((overlays (overlays-in beg end)) overlay
548 (ol-beg beg) (ol-end end))
549 (while overlays
550 (setq overlay (car overlays)
551 overlays (cdr overlays))
552 (when (eq (overlay-get overlay 'face) face)
553 (setq ol-beg (min ol-beg (overlay-start overlay))
554 ol-end (max ol-end (overlay-end overlay)))
555 (delete-overlay overlay)))
556 (when (< ol-beg beg)
557 (overlay-put (make-overlay ol-beg beg) 'face face))
558 (when (> ol-end end)
559 (overlay-put (make-overlay end ol-end) 'face face))))
560
561\f
562;; `c-beginning-of-statement-1' and accompanying stuff.
130c507e 563
64001211
RS
564;; KLUDGE ALERT: c-maybe-labelp is used to pass information between
565;; c-crosses-statement-barrier-p and c-beginning-of-statement-1. A
566;; better way should be implemented, but this will at least shut up
567;; the byte compiler.
0386b551 568(defvar c-maybe-labelp)
64001211 569
d9e94c22
MS
570;; New awk-compatible version of c-beginning-of-statement-1, ACM 2002/6/22
571
a66cd3ee
MS
572;; Macros used internally in c-beginning-of-statement-1 for the
573;; automaton actions.
574(defmacro c-bos-push-state ()
575 '(setq stack (cons (cons state saved-pos)
576 stack)))
577(defmacro c-bos-pop-state (&optional do-if-done)
578 `(if (setq state (car (car stack))
579 saved-pos (cdr (car stack))
580 stack (cdr stack))
581 t
582 ,do-if-done
583 (throw 'loop nil)))
584(defmacro c-bos-pop-state-and-retry ()
585 '(throw 'loop (setq state (car (car stack))
586 saved-pos (cdr (car stack))
587 ;; Throw nil if stack is empty, else throw non-nil.
588 stack (cdr stack))))
589(defmacro c-bos-save-pos ()
590 '(setq saved-pos (vector pos tok ptok pptok)))
591(defmacro c-bos-restore-pos ()
592 '(unless (eq (elt saved-pos 0) start)
593 (setq pos (elt saved-pos 0)
594 tok (elt saved-pos 1)
595 ptok (elt saved-pos 2)
596 pptok (elt saved-pos 3))
597 (goto-char pos)
598 (setq sym nil)))
599(defmacro c-bos-save-error-info (missing got)
600 `(setq saved-pos (vector pos ,missing ,got)))
601(defmacro c-bos-report-error ()
602 '(unless noerror
603 (setq c-parsing-error
604 (format "No matching `%s' found for `%s' on line %d"
605 (elt saved-pos 1)
606 (elt saved-pos 2)
607 (1+ (count-lines (point-min)
608 (c-point 'bol (elt saved-pos 0))))))))
609
610(defun c-beginning-of-statement-1 (&optional lim ignore-labels
611 noerror comma-delim)
612 "Move to the start of the current statement or declaration, or to
613the previous one if already at the beginning of one. Only
614statements/declarations on the same level are considered, i.e. don't
615move into or out of sexps (not even normal expression parentheses).
616
5a89f0a7 617If point is already at the earliest statement within braces or parens,
a85fd6da
AM
618this function doesn't move back into any whitespace preceding it; it
619returns 'same in this case.
620
d9e94c22
MS
621Stop at statement continuation tokens like \"else\", \"catch\",
622\"finally\" and the \"while\" in \"do ... while\" if the start point
623is within the continuation. If starting at such a token, move to the
624corresponding statement start. If at the beginning of a statement,
625move to the closest containing statement if there is any. This might
626also stop at a continuation clause.
a66cd3ee 627
0386b551
AM
628Labels are treated as part of the following statements if
629IGNORE-LABELS is non-nil. (FIXME: Doesn't work if we stop at a known
cb694ab7
AM
630statement start keyword.) Otherwise, each label is treated as a
631separate statement.
a66cd3ee 632
cb694ab7
AM
633Macros are ignored \(i.e. skipped over) unless point is within one, in
634which case the content of the macro is treated as normal code. Aside
635from any normal statement starts found in it, stop at the first token
636of the content in the macro, i.e. the expression of an \"#if\" or the
637start of the definition in a \"#define\". Also stop at start of
638macros before leaving them.
a66cd3ee 639
a85fd6da 640Return:
d28e7f28 641'label if stopped at a label or \"case...:\" or \"default:\";
a85fd6da
AM
642'same if stopped at the beginning of the current statement;
643'up if stepped to a containing statement;
644'previous if stepped to a preceding statement;
645'beginning if stepped from a statement continuation clause to
646 its start clause; or
647'macro if stepped to a macro start.
648Note that 'same and not 'label is returned if stopped at the same
649label without crossing the colon character.
a66cd3ee
MS
650
651LIM may be given to limit the search. If the search hits the limit,
652point will be left at the closest following token, or at the start
653position if that is less ('same is returned in this case).
654
655NOERROR turns off error logging to `c-parsing-error'.
656
cb694ab7
AM
657Normally only ';' and virtual semicolons are considered to delimit
658statements, but if COMMA-DELIM is non-nil then ',' is treated
659as a delimiter too.
0386b551
AM
660
661Note that this function might do hidden buffer changes. See the
662comment at the start of cc-engine.el for more info."
a66cd3ee 663
d9e94c22
MS
664 ;; The bulk of this function is a pushdown automaton that looks at statement
665 ;; boundaries and the tokens (such as "while") in c-opt-block-stmt-key. Its
666 ;; purpose is to keep track of nested statements, ensuring that such
5a89f0a7 667 ;; statements are skipped over in their entirety (somewhat akin to what C-M-p
d9e94c22 668 ;; does with nested braces/brackets/parentheses).
a66cd3ee
MS
669 ;;
670 ;; Note: The position of a boundary is the following token.
671 ;;
d9e94c22
MS
672 ;; Beginning with the current token (the one following point), move back one
673 ;; sexp at a time (where a sexp is, more or less, either a token or the
674 ;; entire contents of a brace/bracket/paren pair). Each time a statement
675 ;; boundary is crossed or a "while"-like token is found, update the state of
676 ;; the PDA. Stop at the beginning of a statement when the stack (holding
677 ;; nested statement info) is empty and the position has been moved.
678 ;;
679 ;; The following variables constitute the PDA:
680 ;;
681 ;; sym: This is either the "while"-like token (e.g. 'for) we've just
682 ;; scanned back over, 'boundary if we've just gone back over a
683 ;; statement boundary, or nil otherwise.
684 ;; state: takes one of the values (nil else else-boundary while
685 ;; while-boundary catch catch-boundary).
686 ;; nil means "no "while"-like token yet scanned".
687 ;; 'else, for example, means "just gone back over an else".
688 ;; 'else-boundary means "just gone back over a statement boundary
689 ;; immediately after having gone back over an else".
690 ;; saved-pos: A vector of either saved positions (tok ptok pptok, etc.) or
691 ;; of error reporting information.
692 ;; stack: The stack onto which the PDA pushes its state. Each entry
693 ;; consists of a saved value of state and saved-pos. An entry is
694 ;; pushed when we move back over a "continuation" token (e.g. else)
695 ;; and popped when we encounter the corresponding opening token
696 ;; (e.g. if).
697 ;;
698 ;;
b414f371 699 ;; The following diagram briefly outlines the PDA.
a66cd3ee
MS
700 ;;
701 ;; Common state:
d9e94c22
MS
702 ;; "else": Push state, goto state `else'.
703 ;; "while": Push state, goto state `while'.
704 ;; "catch" or "finally": Push state, goto state `catch'.
705 ;; boundary: Pop state.
a66cd3ee
MS
706 ;; other: Do nothing special.
707 ;;
d9e94c22
MS
708 ;; State `else':
709 ;; boundary: Goto state `else-boundary'.
710 ;; other: Error, pop state, retry token.
711 ;;
712 ;; State `else-boundary':
713 ;; "if": Pop state.
714 ;; boundary: Error, pop state.
715 ;; other: See common state.
716 ;;
717 ;; State `while':
718 ;; boundary: Save position, goto state `while-boundary'.
719 ;; other: Pop state, retry token.
720 ;;
721 ;; State `while-boundary':
722 ;; "do": Pop state.
723 ;; boundary: Restore position if it's not at start, pop state. [*see below]
724 ;; other: See common state.
725 ;;
726 ;; State `catch':
727 ;; boundary: Goto state `catch-boundary'.
728 ;; other: Error, pop state, retry token.
729 ;;
730 ;; State `catch-boundary':
731 ;; "try": Pop state.
732 ;; "catch": Goto state `catch'.
733 ;; boundary: Error, pop state.
734 ;; other: See common state.
735 ;;
736 ;; [*] In the `while-boundary' state, we had pushed a 'while state, and were
737 ;; searching for a "do" which would have opened a do-while. If we didn't
738 ;; find it, we discard the analysis done since the "while", go back to this
739 ;; token in the buffer and restart the scanning there, this time WITHOUT
740 ;; pushing the 'while state onto the stack.
741 ;;
a66cd3ee
MS
742 ;; In addition to the above there is some special handling of labels
743 ;; and macros.
744
745 (let ((case-fold-search nil)
746 (start (point))
747 macro-start
748 (delims (if comma-delim '(?\; ?,) '(?\;)))
749 (c-stmt-delim-chars (if comma-delim
750 c-stmt-delim-chars-with-comma
751 c-stmt-delim-chars))
d28e7f28 752 c-in-literal-cache c-maybe-labelp after-case:-pos saved
0386b551
AM
753 ;; Current position.
754 pos
755 ;; Position of last stmt boundary character (e.g. ;).
756 boundary-pos
757 ;; The position of the last sexp or bound that follows the
758 ;; first found colon, i.e. the start of the nonlabel part of
759 ;; the statement. It's `start' if a colon is found just after
760 ;; the start.
761 after-labels-pos
762 ;; Like `after-labels-pos', but the first such position inside
763 ;; a label, i.e. the start of the last label before the start
764 ;; of the nonlabel part of the statement.
765 last-label-pos
766 ;; The last position where a label is possible provided the
767 ;; statement started there. It's nil as long as no invalid
768 ;; label content has been found (according to
020716e1 769 ;; `c-nonlabel-token-key'). It's `start' if no valid label
0386b551
AM
770 ;; content was found in the label. Note that we might still
771 ;; regard it a label if it starts with `c-label-kwds'.
772 label-good-pos
b248a85d
AM
773 ;; Putative positions of the components of a bitfield declaration,
774 ;; e.g. "int foo : NUM_FOO_BITS ;"
775 bitfield-type-pos bitfield-id-pos bitfield-size-pos
0386b551
AM
776 ;; Symbol just scanned back over (e.g. 'while or 'boundary).
777 ;; See above.
778 sym
779 ;; Current state in the automaton. See above.
780 state
781 ;; Current saved positions. See above.
782 saved-pos
783 ;; Stack of conses (state . saved-pos).
784 stack
785 ;; Regexp which matches "for", "if", etc.
786 (cond-key (or c-opt-block-stmt-key
a66cd3ee 787 "\\<\\>")) ; Matches nothing.
0386b551
AM
788 ;; Return value.
789 (ret 'same)
790 ;; Positions of the last three sexps or bounds we've stopped at.
791 tok ptok pptok)
a66cd3ee
MS
792
793 (save-restriction
794 (if lim (narrow-to-region lim (point-max)))
795
796 (if (save-excursion
797 (and (c-beginning-of-macro)
798 (/= (point) start)))
799 (setq macro-start (point)))
800
d9e94c22 801 ;; Try to skip back over unary operator characters, to register
a66cd3ee
MS
802 ;; that we've moved.
803 (while (progn
804 (setq pos (point))
0386b551
AM
805 (c-backward-syntactic-ws)
806 ;; Protect post-++/-- operators just before a virtual semicolon.
807 (and (not (c-at-vsemi-p))
808 (/= (skip-chars-backward "-+!*&~@`#") 0))))
d9e94c22
MS
809
810 ;; Skip back over any semicolon here. If it was a bare semicolon, we're
0386b551 811 ;; done. Later on we ignore the boundaries for statements that don't
d9e94c22
MS
812 ;; contain any sexp. The only thing that is affected is that the error
813 ;; checking is a little less strict, and we really don't bother.
a66cd3ee
MS
814 (if (and (memq (char-before) delims)
815 (progn (forward-char -1)
816 (setq saved (point))
0386b551 817 (c-backward-syntactic-ws)
a66cd3ee
MS
818 (or (memq (char-before) delims)
819 (memq (char-before) '(?: nil))
d9e94c22 820 (eq (char-syntax (char-before)) ?\()
0386b551 821 (c-at-vsemi-p))))
a66cd3ee
MS
822 (setq ret 'previous
823 pos saved)
824
825 ;; Begin at start and not pos to detect macros if we stand
826 ;; directly after the #.
827 (goto-char start)
828 (if (looking-at "\\<\\|\\W")
829 ;; Record this as the first token if not starting inside it.
830 (setq tok start))
831
b248a85d
AM
832
833 ;; The following while loop goes back one sexp (balanced parens,
834 ;; etc. with contents, or symbol or suchlike) each iteration. This
835 ;; movement is accomplished with a call to c-backward-sexp approx 170
836 ;; lines below.
837 ;;
838 ;; The loop is exited only by throwing nil to the (catch 'loop ...):
839 ;; 1. On reaching the start of a macro;
840 ;; 2. On having passed a stmt boundary with the PDA stack empty;
841 ;; 3. On reaching the start of an Objective C method def;
842 ;; 4. From macro `c-bos-pop-state'; when the stack is empty;
843 ;; 5. From macro `c-bos-pop-state-and-retry' when the stack is empty.
a66cd3ee
MS
844 (while
845 (catch 'loop ;; Throw nil to break, non-nil to continue.
846 (cond
b248a85d 847 ;; Are we in a macro, just after the opening #?
a66cd3ee 848 ((save-excursion
0386b551 849 (and macro-start ; Always NIL for AWK.
a66cd3ee
MS
850 (progn (skip-chars-backward " \t")
851 (eq (char-before) ?#))
852 (progn (setq saved (1- (point)))
853 (beginning-of-line)
854 (not (eq (char-before (1- (point))) ?\\)))
d9e94c22 855 (looking-at c-opt-cpp-start)
a66cd3ee
MS
856 (progn (skip-chars-forward " \t")
857 (eq (point) saved))))
858 (goto-char saved)
859 (if (and (c-forward-to-cpp-define-body)
860 (progn (c-forward-syntactic-ws start)
861 (< (point) start)))
862 ;; Stop at the first token in the content of the macro.
863 (setq pos (point)
864 ignore-labels t) ; Avoid the label check on exit.
865 (setq pos saved
866 ret 'macro
867 ignore-labels t))
b248a85d 868 (throw 'loop nil)) ; 1. Start of macro.
a66cd3ee 869
d9e94c22
MS
870 ;; Do a round through the automaton if we've just passed a
871 ;; statement boundary or passed a "while"-like token.
a66cd3ee
MS
872 ((or sym
873 (and (looking-at cond-key)
874 (setq sym (intern (match-string 1)))))
875
876 (when (and (< pos start) (null stack))
b248a85d 877 (throw 'loop nil)) ; 2. Statement boundary.
a66cd3ee 878
d9e94c22
MS
879 ;; The PDA state handling.
880 ;;
037558bf 881 ;; Refer to the description of the PDA in the opening
d9e94c22
MS
882 ;; comments. In the following OR form, the first leaf
883 ;; attempts to handles one of the specific actions detailed
884 ;; (e.g., finding token "if" whilst in state `else-boundary').
885 ;; We drop through to the second leaf (which handles common
886 ;; state) if no specific handler is found in the first cond.
887 ;; If a parsing error is detected (e.g. an "else" with no
888 ;; preceding "if"), we throw to the enclosing catch.
889 ;;
890 ;; Note that the (eq state 'else) means
891 ;; "we've just passed an else", NOT "we're looking for an
892 ;; else".
a66cd3ee
MS
893 (or (cond
894 ((eq state 'else)
895 (if (eq sym 'boundary)
896 (setq state 'else-boundary)
897 (c-bos-report-error)
898 (c-bos-pop-state-and-retry)))
899
900 ((eq state 'else-boundary)
901 (cond ((eq sym 'if)
902 (c-bos-pop-state (setq ret 'beginning)))
903 ((eq sym 'boundary)
904 (c-bos-report-error)
905 (c-bos-pop-state))))
906
907 ((eq state 'while)
908 (if (and (eq sym 'boundary)
909 ;; Since this can cause backtracking we do a
910 ;; little more careful analysis to avoid it:
911 ;; If there's a label in front of the while
912 ;; it can't be part of a do-while.
913 (not after-labels-pos))
914 (progn (c-bos-save-pos)
915 (setq state 'while-boundary))
d9e94c22 916 (c-bos-pop-state-and-retry))) ; Can't be a do-while
a66cd3ee
MS
917
918 ((eq state 'while-boundary)
919 (cond ((eq sym 'do)
920 (c-bos-pop-state (setq ret 'beginning)))
d9e94c22
MS
921 ((eq sym 'boundary) ; isn't a do-while
922 (c-bos-restore-pos) ; the position of the while
923 (c-bos-pop-state)))) ; no longer searching for do.
a66cd3ee
MS
924
925 ((eq state 'catch)
926 (if (eq sym 'boundary)
927 (setq state 'catch-boundary)
928 (c-bos-report-error)
929 (c-bos-pop-state-and-retry)))
930
931 ((eq state 'catch-boundary)
932 (cond
933 ((eq sym 'try)
934 (c-bos-pop-state (setq ret 'beginning)))
935 ((eq sym 'catch)
936 (setq state 'catch))
937 ((eq sym 'boundary)
938 (c-bos-report-error)
939 (c-bos-pop-state)))))
940
d9e94c22
MS
941 ;; This is state common. We get here when the previous
942 ;; cond statement found no particular state handler.
a66cd3ee 943 (cond ((eq sym 'boundary)
d9e94c22
MS
944 ;; If we have a boundary at the start
945 ;; position we push a frame to go to the
946 ;; previous statement.
947 (if (>= pos start)
948 (c-bos-push-state)
949 (c-bos-pop-state)))
a66cd3ee
MS
950 ((eq sym 'else)
951 (c-bos-push-state)
952 (c-bos-save-error-info 'if 'else)
953 (setq state 'else))
954 ((eq sym 'while)
0386b551
AM
955 ;; Is this a real while, or a do-while?
956 ;; The next `when' triggers unless we are SURE that
40ba43b4 957 ;; the `while' is not the tail end of a `do-while'.
a66cd3ee 958 (when (or (not pptok)
d9e94c22 959 (memq (char-after pptok) delims)
0386b551
AM
960 ;; The following kludge is to prevent
961 ;; infinite recursion when called from
962 ;; c-awk-after-if-for-while-condition-p,
963 ;; or the like.
964 (and (eq (point) start)
965 (c-vsemi-status-unknown-p))
966 (c-at-vsemi-p pptok))
a66cd3ee
MS
967 ;; Since this can cause backtracking we do a
968 ;; little more careful analysis to avoid it: If
0386b551
AM
969 ;; the while isn't followed by a (possibly
970 ;; virtual) semicolon it can't be a do-while.
a66cd3ee
MS
971 (c-bos-push-state)
972 (setq state 'while)))
973 ((memq sym '(catch finally))
974 (c-bos-push-state)
975 (c-bos-save-error-info 'try sym)
976 (setq state 'catch))))
977
978 (when c-maybe-labelp
979 ;; We're either past a statement boundary or at the
980 ;; start of a statement, so throw away any label data
981 ;; for the previous one.
982 (setq after-labels-pos nil
983 last-label-pos nil
984 c-maybe-labelp nil))))
985
d9e94c22
MS
986 ;; Step to the previous sexp, but not if we crossed a
987 ;; boundary, since that doesn't consume an sexp.
a66cd3ee
MS
988 (if (eq sym 'boundary)
989 (setq ret 'previous)
d9e94c22
MS
990
991 ;; HERE IS THE SINGLE PLACE INSIDE THE PDA LOOP WHERE WE MOVE
0386b551
AM
992 ;; BACKWARDS THROUGH THE SOURCE.
993
0386b551 994 (c-backward-syntactic-ws)
0386b551 995 (let ((before-sws-pos (point))
0386b551
AM
996 ;; The end position of the area to search for statement
997 ;; barriers in this round.
b248a85d 998 (maybe-after-boundary-pos pos))
0386b551 999
b248a85d
AM
1000 ;; Go back over exactly one logical sexp, taking proper
1001 ;; account of macros and escaped EOLs.
0386b551
AM
1002 (while
1003 (progn
1004 (unless (c-safe (c-backward-sexp) t)
1005 ;; Give up if we hit an unbalanced block. Since the
1006 ;; stack won't be empty the code below will report a
1007 ;; suitable error.
a66cd3ee 1008 (throw 'loop nil))
b248a85d
AM
1009 (cond
1010 ;; Have we moved into a macro?
1011 ((and (not macro-start)
1012 (c-beginning-of-macro))
1013 ;; Have we crossed a statement boundary? If not,
1014 ;; keep going back until we find one or a "real" sexp.
1015 (and
1016 (save-excursion
1017 (c-end-of-macro)
1018 (not (c-crosses-statement-barrier-p
1019 (point) maybe-after-boundary-pos)))
1020 (setq maybe-after-boundary-pos (point))))
1021 ;; Have we just gone back over an escaped NL? This
1022 ;; doesn't count as a sexp.
1023 ((looking-at "\\\\$")))))
1024
1025 ;; Have we crossed a statement boundary?
1026 (setq boundary-pos
1027 (cond
1028 ;; Are we at a macro beginning?
1029 ((and (not macro-start)
1030 c-opt-cpp-prefix
1031 (looking-at c-opt-cpp-prefix))
1032 (save-excursion
1033 (c-end-of-macro)
1034 (c-crosses-statement-barrier-p
1035 (point) maybe-after-boundary-pos)))
1036 ;; Just gone back over a brace block?
1037 ((and
1038 (eq (char-after) ?{)
1039 (not (c-looking-at-inexpr-block lim nil t)))
1040 (save-excursion
1041 (c-forward-sexp) (point)))
1042 ;; Just gone back over some paren block?
1043 ((looking-at "\\s\(")
1044 (save-excursion
1045 (goto-char (1+ (c-down-list-backward
1046 before-sws-pos)))
1047 (c-crosses-statement-barrier-p
1048 (point) maybe-after-boundary-pos)))
1049 ;; Just gone back over an ordinary symbol of some sort?
1050 (t (c-crosses-statement-barrier-p
1051 (point) maybe-after-boundary-pos))))
91af3942 1052
b248a85d
AM
1053 (when boundary-pos
1054 (setq pptok ptok
1055 ptok tok
1056 tok boundary-pos
1057 sym 'boundary)
1058 ;; Like a C "continue". Analyze the next sexp.
1059 (throw 'loop t))))
a66cd3ee
MS
1060
1061 ;; ObjC method def?
1062 (when (and c-opt-method-key
1063 (setq saved (c-in-method-def-p)))
1064 (setq pos saved
1065 ignore-labels t) ; Avoid the label check on exit.
b248a85d
AM
1066 (throw 'loop nil)) ; 3. ObjC method def.
1067
1068 ;; Might we have a bitfield declaration, "<type> <id> : <size>"?
1069 (if c-has-bitfields
1070 (cond
1071 ;; The : <size> and <id> fields?
1072 ((and (numberp c-maybe-labelp)
1073 (not bitfield-size-pos)
1074 (save-excursion
1075 (goto-char (or tok start))
1076 (not (looking-at c-keywords-regexp)))
1077 (not (looking-at c-keywords-regexp))
1078 (not (c-punctuation-in (point) c-maybe-labelp)))
1079 (setq bitfield-size-pos (or tok start)
1080 bitfield-id-pos (point)))
1081 ;; The <type> field?
1082 ((and bitfield-id-pos
1083 (not bitfield-type-pos))
1084 (if (and (looking-at c-symbol-key) ; Can only be an integer type. :-)
1085 (not (looking-at c-not-primitive-type-keywords-regexp))
1086 (not (c-punctuation-in (point) tok)))
1087 (setq bitfield-type-pos (point))
1088 (setq bitfield-size-pos nil
1089 bitfield-id-pos nil)))))
a66cd3ee 1090
0386b551
AM
1091 ;; Handle labels.
1092 (unless (eq ignore-labels t)
1093 (when (numberp c-maybe-labelp)
cb694ab7
AM
1094 ;; `c-crosses-statement-barrier-p' has found a colon, so we
1095 ;; might be in a label now. Have we got a real label
1096 ;; (including a case label) or something like C++'s "public:"?
d28e7f28
AM
1097 ;; A case label might use an expression rather than a token.
1098 (setq after-case:-pos (or tok start))
020716e1
AM
1099 (if (or (looking-at c-nonlabel-token-key) ; e.g. "while" or "'a'"
1100 ;; Catch C++'s inheritance construct "class foo : bar".
1101 (save-excursion
1102 (and
1103 (c-safe (c-backward-sexp) t)
1104 (looking-at c-nonlabel-token-2-key))))
d28e7f28
AM
1105 (setq c-maybe-labelp nil)
1106 (if after-labels-pos ; Have we already encountered a label?
1107 (if (not last-label-pos)
1108 (setq last-label-pos (or tok start)))
1109 (setq after-labels-pos (or tok start)))
1110 (setq c-maybe-labelp t
1111 label-good-pos nil))) ; bogus "label"
cb694ab7
AM
1112
1113 (when (and (not label-good-pos) ; i.e. no invalid "label"'s yet
1114 ; been found.
1115 (looking-at c-nonlabel-token-key)) ; e.g. "while :"
0386b551
AM
1116 ;; We're in a potential label and it's the first
1117 ;; time we've found something that isn't allowed in
1118 ;; one.
1119 (setq label-good-pos (or tok start))))
1120
1121 ;; We've moved back by a sexp, so update the token positions.
a66cd3ee
MS
1122 (setq sym nil
1123 pptok ptok
1124 ptok tok
1125 tok (point)
b248a85d
AM
1126 pos tok) ; always non-nil
1127 ) ; end of (catch loop ....)
1128 ) ; end of sexp-at-a-time (while ....)
91af3942 1129
a66cd3ee
MS
1130 ;; If the stack isn't empty there might be errors to report.
1131 (while stack
1132 (if (and (vectorp saved-pos) (eq (length saved-pos) 3))
1133 (c-bos-report-error))
1134 (setq saved-pos (cdr (car stack))
1135 stack (cdr stack)))
1136
1137 (when (and (eq ret 'same)
1138 (not (memq sym '(boundary ignore nil))))
1139 ;; Need to investigate closer whether we've crossed
1140 ;; between a substatement and its containing statement.
1141 (if (setq saved (if (looking-at c-block-stmt-1-key)
1142 ptok
1143 pptok))
1144 (cond ((> start saved) (setq pos saved))
1145 ((= start saved) (setq ret 'up)))))
1146
0386b551
AM
1147 (when (and (not ignore-labels)
1148 (eq c-maybe-labelp t)
d9e94c22 1149 (not (eq ret 'beginning))
0386b551 1150 after-labels-pos
b248a85d 1151 (not bitfield-type-pos) ; Bitfields take precedence over labels.
0386b551
AM
1152 (or (not label-good-pos)
1153 (<= label-good-pos pos)
1154 (progn
1155 (goto-char (if (and last-label-pos
1156 (< last-label-pos start))
1157 last-label-pos
1158 pos))
1159 (looking-at c-label-kwds-regexp))))
a66cd3ee
MS
1160 ;; We're in a label. Maybe we should step to the statement
1161 ;; after it.
1162 (if (< after-labels-pos start)
1163 (setq pos after-labels-pos)
1164 (setq ret 'label)
0386b551
AM
1165 (if (and last-label-pos (< last-label-pos start))
1166 ;; Might have jumped over several labels. Go to the last one.
a66cd3ee
MS
1167 (setq pos last-label-pos)))))
1168
d28e7f28 1169 ;; Have we got "case <expression>:"?
a66cd3ee 1170 (goto-char pos)
d28e7f28
AM
1171 (when (and after-case:-pos
1172 (not (eq ret 'beginning))
1173 (looking-at c-case-kwds-regexp))
1174 (if (< after-case:-pos start)
0000ee90
AM
1175 (setq pos after-case:-pos))
1176 (if (eq ret 'same)
1177 (setq ret 'label)))
d28e7f28
AM
1178
1179 ;; Skip over the unary operators that can start the statement.
a66cd3ee 1180 (while (progn
0386b551
AM
1181 (c-backward-syntactic-ws)
1182 ;; protect AWK post-inc/decrement operators, etc.
1183 (and (not (c-at-vsemi-p (point)))
1184 (/= (skip-chars-backward "-+!*&~@`#") 0)))
a66cd3ee
MS
1185 (setq pos (point)))
1186 (goto-char pos)
1187 ret)))
785eecbb 1188
b248a85d
AM
1189(defun c-punctuation-in (from to)
1190 "Return non-nil if there is a non-comment non-macro punctuation character
1191between FROM and TO. FROM must not be in a string or comment. The returned
1192value is the position of the first such character."
1193 (save-excursion
1194 (goto-char from)
1195 (let ((pos (point)))
1196 (while (progn (skip-chars-forward c-symbol-chars to)
1197 (c-forward-syntactic-ws to)
1198 (> (point) pos))
1199 (setq pos (point))))
1200 (and (< (point) to) (point))))
1201
785eecbb 1202(defun c-crosses-statement-barrier-p (from to)
a66cd3ee
MS
1203 "Return non-nil if buffer positions FROM to TO cross one or more
1204statement or declaration boundaries. The returned value is actually
d9e94c22
MS
1205the position of the earliest boundary char. FROM must not be within
1206a string or comment.
a66cd3ee
MS
1207
1208The variable `c-maybe-labelp' is set to the position of the first `:' that
1209might start a label (i.e. not part of `::' and not preceded by `?'). If a
0386b551
AM
1210single `?' is found, then `c-maybe-labelp' is cleared.
1211
1212For AWK, a statement which is terminated by an EOL (not a \; or a }) is
1213regarded as having a \"virtual semicolon\" immediately after the last token on
b414f371 1214the line. If this virtual semicolon is _at_ from, the function recognizes it.
0386b551
AM
1215
1216Note that this function might do hidden buffer changes. See the
1217comment at the start of cc-engine.el for more info."
536610a4
AM
1218 (let* ((skip-chars
1219 ;; If the current language has CPP macros, insert # into skip-chars.
1220 (if c-opt-cpp-symbol
1221 (concat (substring c-stmt-delim-chars 0 1) ; "^"
1222 c-opt-cpp-symbol ; usually "#"
1223 (substring c-stmt-delim-chars 1)) ; e.g. ";{}?:"
1224 c-stmt-delim-chars))
1225 (non-skip-list
1226 (append (substring skip-chars 1) nil)) ; e.g. (?# ?\; ?{ ?} ?? ?:)
1227 lit-range vsemi-pos)
1228 (save-restriction
1229 (widen)
1230 (save-excursion
1231 (catch 'done
1232 (goto-char from)
1233 (while (progn (skip-chars-forward
1234 skip-chars
1235 (min to (c-point 'bonl)))
1236 (< (point) to))
1237 (cond
1238 ;; Virtual semicolon?
1239 ((and (bolp)
1240 (save-excursion
1241 (progn
1242 (if (setq lit-range (c-literal-limits from)) ; Have we landed in a string/comment?
1243 (goto-char (car lit-range)))
1244 (c-backward-syntactic-ws) ; ? put a limit here, maybe?
1245 (setq vsemi-pos (point))
1246 (c-at-vsemi-p))))
1247 (throw 'done vsemi-pos))
1248 ;; In a string/comment?
2e492df3 1249 ((setq lit-range (c-literal-limits from))
536610a4
AM
1250 (goto-char (cdr lit-range)))
1251 ((eq (char-after) ?:)
1252 (forward-char)
1253 (if (and (eq (char-after) ?:)
1254 (< (point) to))
1255 ;; Ignore scope operators.
1256 (forward-char)
1257 (setq c-maybe-labelp (1- (point)))))
1258 ((eq (char-after) ??)
1259 ;; A question mark. Can't be a label, so stop
1260 ;; looking for more : and ?.
1261 (setq c-maybe-labelp nil
1262 skip-chars (substring c-stmt-delim-chars 0 -2)))
1263 ;; At a CPP construct?
1264 ((and c-opt-cpp-symbol (looking-at c-opt-cpp-symbol)
1265 (save-excursion
1266 (forward-line 0)
1267 (looking-at c-opt-cpp-prefix)))
1268 (c-end-of-macro))
1269 ((memq (char-after) non-skip-list)
1270 (throw 'done (point)))))
1271 ;; In trailing space after an as yet undetected virtual semicolon?
1272 (c-backward-syntactic-ws from)
1273 (if (and (< (point) to)
1274 (c-at-vsemi-p))
1275 (point)
1276 nil))))))
0386b551
AM
1277
1278(defun c-at-statement-start-p ()
1279 "Return non-nil if the point is at the first token in a statement
1280or somewhere in the syntactic whitespace before it.
1281
1282A \"statement\" here is not restricted to those inside code blocks.
1283Any kind of declaration-like construct that occur outside function
1284bodies is also considered a \"statement\".
1285
1286Note that this function might do hidden buffer changes. See the
1287comment at the start of cc-engine.el for more info."
1288
1289 (save-excursion
1290 (let ((end (point))
1291 c-maybe-labelp)
1292 (c-syntactic-skip-backward (substring c-stmt-delim-chars 1) nil t)
1293 (or (bobp)
1294 (eq (char-before) ?})
1295 (and (eq (char-before) ?{)
1296 (not (and c-special-brace-lists
1297 (progn (backward-char)
1298 (c-looking-at-special-brace-list)))))
1299 (c-crosses-statement-barrier-p (point) end)))))
1300
1301(defun c-at-expression-start-p ()
1302 "Return non-nil if the point is at the first token in an expression or
1303statement, or somewhere in the syntactic whitespace before it.
1304
1305An \"expression\" here is a bit different from the normal language
1306grammar sense: It's any sequence of expression tokens except commas,
1307unless they are enclosed inside parentheses of some kind. Also, an
1308expression never continues past an enclosing parenthesis, but it might
1309contain parenthesis pairs of any sort except braces.
1310
1311Since expressions never cross statement boundaries, this function also
1312recognizes statement beginnings, just like `c-at-statement-start-p'.
1313
1314Note that this function might do hidden buffer changes. See the
1315comment at the start of cc-engine.el for more info."
1316
1317 (save-excursion
1318 (let ((end (point))
1319 (c-stmt-delim-chars c-stmt-delim-chars-with-comma)
1320 c-maybe-labelp)
1321 (c-syntactic-skip-backward (substring c-stmt-delim-chars 1) nil t)
1322 (or (bobp)
1323 (memq (char-before) '(?{ ?}))
1324 (save-excursion (backward-char)
1325 (looking-at "\\s("))
1326 (c-crosses-statement-barrier-p (point) end)))))
785eecbb
RS
1327
1328\f
d9e94c22
MS
1329;; A set of functions that covers various idiosyncrasies in
1330;; implementations of `forward-comment'.
1331
1332;; Note: Some emacsen considers incorrectly that any line comment
1333;; ending with a backslash continues to the next line. I can't think
1334;; of any way to work around that in a reliable way without changing
1335;; the buffer, though. Suggestions welcome. ;) (No, temporarily
1336;; changing the syntax for backslash doesn't work since we must treat
1337;; escapes in string literals correctly.)
1338
1339(defun c-forward-single-comment ()
1340 "Move forward past whitespace and the closest following comment, if any.
1341Return t if a comment was found, nil otherwise. In either case, the
1342point is moved past the following whitespace. Line continuations,
1343i.e. a backslashes followed by line breaks, are treated as whitespace.
1344The line breaks that end line comments are considered to be the
1345comment enders, so the point will be put on the beginning of the next
1346line if it moved past a line comment.
1347
1348This function does not do any hidden buffer changes."
1349
1350 (let ((start (point)))
1351 (when (looking-at "\\([ \t\n\r\f\v]\\|\\\\[\n\r]\\)+")
1352 (goto-char (match-end 0)))
1353
1354 (when (forward-comment 1)
1355 (if (eobp)
1356 ;; Some emacsen (e.g. XEmacs 21) return t when moving
1357 ;; forwards at eob.
1358 nil
1359
1360 ;; Emacs includes the ending newline in a b-style (c++)
1361 ;; comment, but XEmacs doesn't. We depend on the Emacs
1362 ;; behavior (which also is symmetric).
1363 (if (and (eolp) (elt (parse-partial-sexp start (point)) 7))
1364 (condition-case nil (forward-char 1)))
1365
1366 t))))
1367
1368(defsubst c-forward-comments ()
1369 "Move forward past all following whitespace and comments.
1370Line continuations, i.e. a backslashes followed by line breaks, are
1371treated as whitespace.
1372
0386b551
AM
1373Note that this function might do hidden buffer changes. See the
1374comment at the start of cc-engine.el for more info."
d9e94c22
MS
1375
1376 (while (or
1377 ;; If forward-comment in at least XEmacs 21 is given a large
1378 ;; positive value, it'll loop all the way through if it hits
1379 ;; eob.
1380 (and (forward-comment 5)
1381 ;; Some emacsen (e.g. XEmacs 21) return t when moving
1382 ;; forwards at eob.
1383 (not (eobp)))
1384
1385 (when (looking-at "\\\\[\n\r]")
1386 (forward-char 2)
1387 t))))
1388
1389(defun c-backward-single-comment ()
1390 "Move backward past whitespace and the closest preceding comment, if any.
1391Return t if a comment was found, nil otherwise. In either case, the
1392point is moved past the preceding whitespace. Line continuations,
1393i.e. a backslashes followed by line breaks, are treated as whitespace.
1394The line breaks that end line comments are considered to be the
1395comment enders, so the point cannot be at the end of the same line to
1396move over a line comment.
1397
1398This function does not do any hidden buffer changes."
1399
1400 (let ((start (point)))
1401 ;; When we got newline terminated comments, forward-comment in all
1402 ;; supported emacsen so far will stop at eol of each line not
1403 ;; ending with a comment when moving backwards. This corrects for
1404 ;; that, and at the same time handles line continuations.
1405 (while (progn
1406 (skip-chars-backward " \t\n\r\f\v")
1407 (and (looking-at "[\n\r]")
0386b551 1408 (eq (char-before) ?\\)))
d9e94c22
MS
1409 (backward-char))
1410
1411 (if (bobp)
1412 ;; Some emacsen (e.g. Emacs 19.34) return t when moving
1413 ;; backwards at bob.
1414 nil
1415
1416 ;; Leave point after the closest following newline if we've
1417 ;; backed up over any above, since forward-comment won't move
1418 ;; backward over a line comment if point is at the end of the
1419 ;; same line.
1420 (re-search-forward "\\=\\s *[\n\r]" start t)
1421
bf2c1571 1422 (if (if (let (open-paren-in-column-0-is-defun-start) (forward-comment -1))
d9e94c22
MS
1423 (if (eolp)
1424 ;; If forward-comment above succeeded and we're at eol
1425 ;; then the newline we moved over above didn't end a
1426 ;; line comment, so we give it another go.
bf2c1571
AM
1427 (let (open-paren-in-column-0-is-defun-start)
1428 (forward-comment -1))
d9e94c22
MS
1429 t))
1430
1431 ;; Emacs <= 20 and XEmacs move back over the closer of a
1432 ;; block comment that lacks an opener.
1433 (if (looking-at "\\*/")
1434 (progn (forward-char 2) nil)
1435 t)))))
1436
1437(defsubst c-backward-comments ()
1438 "Move backward past all preceding whitespace and comments.
1439Line continuations, i.e. a backslashes followed by line breaks, are
1440treated as whitespace. The line breaks that end line comments are
1441considered to be the comment enders, so the point cannot be at the end
0386b551
AM
1442of the same line to move over a line comment. Unlike
1443c-backward-syntactic-ws, this function doesn't move back over
1444preprocessor directives.
d9e94c22 1445
0386b551
AM
1446Note that this function might do hidden buffer changes. See the
1447comment at the start of cc-engine.el for more info."
d9e94c22
MS
1448
1449 (let ((start (point)))
1450 (while (and
0386b551 1451 ;; `forward-comment' in some emacsen (e.g. XEmacs 21.4)
d9e94c22
MS
1452 ;; return t when moving backwards at bob.
1453 (not (bobp))
1454
bf2c1571
AM
1455 (if (let (open-paren-in-column-0-is-defun-start)
1456 (forward-comment -1))
d9e94c22
MS
1457 (if (looking-at "\\*/")
1458 ;; Emacs <= 20 and XEmacs move back over the
1459 ;; closer of a block comment that lacks an opener.
1460 (progn (forward-char 2) nil)
1461 t)
1462
1463 ;; XEmacs treats line continuations as whitespace but
1464 ;; only in the backward direction, which seems a bit
1465 ;; odd. Anyway, this is necessary for Emacs.
1466 (when (and (looking-at "[\n\r]")
1467 (eq (char-before) ?\\)
1468 (< (point) start))
1469 (backward-char)
1470 t))))))
1471
1472\f
d9e94c22 1473;; Tools for skipping over syntactic whitespace.
a66cd3ee 1474
d9e94c22
MS
1475;; The following functions use text properties to cache searches over
1476;; large regions of syntactic whitespace. It works as follows:
1477;;
1478;; o If a syntactic whitespace region contains anything but simple
1479;; whitespace (i.e. space, tab and line breaks), the text property
1480;; `c-in-sws' is put over it. At places where we have stopped
1481;; within that region there's also a `c-is-sws' text property.
1482;; That since there typically are nested whitespace inside that
1483;; must be handled separately, e.g. whitespace inside a comment or
1484;; cpp directive. Thus, from one point with `c-is-sws' it's safe
1485;; to jump to another point with that property within the same
1486;; `c-in-sws' region. It can be likened to a ladder where
1487;; `c-in-sws' marks the bars and `c-is-sws' the rungs.
1488;;
1489;; o The `c-is-sws' property is put on the simple whitespace chars at
1490;; a "rung position" and also maybe on the first following char.
1491;; As many characters as can be conveniently found in this range
1492;; are marked, but no assumption can be made that the whole range
1493;; is marked (it could be clobbered by later changes, for
1494;; instance).
1495;;
1496;; Note that some part of the beginning of a sequence of simple
1497;; whitespace might be part of the end of a preceding line comment
1498;; or cpp directive and must not be considered part of the "rung".
1499;; Such whitespace is some amount of horizontal whitespace followed
1500;; by a newline. In the case of cpp directives it could also be
1501;; two newlines with horizontal whitespace between them.
1502;;
1503;; The reason to include the first following char is to cope with
1504;; "rung positions" that doesn't have any ordinary whitespace. If
1505;; `c-is-sws' is put on a token character it does not have
1506;; `c-in-sws' set simultaneously. That's the only case when that
1507;; can occur, and the reason for not extending the `c-in-sws'
1508;; region to cover it is that the `c-in-sws' region could then be
1509;; accidentally merged with a following one if the token is only
1510;; one character long.
1511;;
1512;; o On buffer changes the `c-in-sws' and `c-is-sws' properties are
1513;; removed in the changed region. If the change was inside
1514;; syntactic whitespace that means that the "ladder" is broken, but
1515;; a later call to `c-forward-sws' or `c-backward-sws' will use the
1516;; parts on either side and use an ordinary search only to "repair"
1517;; the gap.
1518;;
1519;; Special care needs to be taken if a region is removed: If there
1520;; are `c-in-sws' on both sides of it which do not connect inside
1521;; the region then they can't be joined. If e.g. a marked macro is
1522;; broken, syntactic whitespace inside the new text might be
1523;; marked. If those marks would become connected with the old
1524;; `c-in-sws' range around the macro then we could get a ladder
1525;; with one end outside the macro and the other at some whitespace
1526;; within it.
1527;;
1528;; The main motivation for this system is to increase the speed in
1529;; skipping over the large whitespace regions that can occur at the
1530;; top level in e.g. header files that contain a lot of comments and
1531;; cpp directives. For small comments inside code it's probably
1532;; slower than using `forward-comment' straightforwardly, but speed is
1533;; not a significant factor there anyway.
1534
1535; (defface c-debug-is-sws-face
1536; '((t (:background "GreenYellow")))
1537; "Debug face to mark the `c-is-sws' property.")
1538; (defface c-debug-in-sws-face
1539; '((t (:underline t)))
1540; "Debug face to mark the `c-in-sws' property.")
1541
1542; (defun c-debug-put-sws-faces ()
1543; ;; Put the sws debug faces on all the `c-is-sws' and `c-in-sws'
1544; ;; properties in the buffer.
1545; (interactive)
1546; (save-excursion
0386b551 1547; (c-save-buffer-state (in-face)
d9e94c22
MS
1548; (goto-char (point-min))
1549; (setq in-face (if (get-text-property (point) 'c-is-sws)
1550; (point)))
1551; (while (progn
1552; (goto-char (next-single-property-change
1553; (point) 'c-is-sws nil (point-max)))
1554; (if in-face
1555; (progn
1556; (c-debug-add-face in-face (point) 'c-debug-is-sws-face)
1557; (setq in-face nil))
1558; (setq in-face (point)))
1559; (not (eobp))))
1560; (goto-char (point-min))
1561; (setq in-face (if (get-text-property (point) 'c-in-sws)
1562; (point)))
1563; (while (progn
1564; (goto-char (next-single-property-change
1565; (point) 'c-in-sws nil (point-max)))
1566; (if in-face
1567; (progn
1568; (c-debug-add-face in-face (point) 'c-debug-in-sws-face)
1569; (setq in-face nil))
1570; (setq in-face (point)))
1571; (not (eobp)))))))
1572
1573(defmacro c-debug-sws-msg (&rest args)
1574 ;;`(message ,@args)
1575 )
1576
1577(defmacro c-put-is-sws (beg end)
0386b551 1578 ;; This macro does a hidden buffer change.
d9e94c22
MS
1579 `(let ((beg ,beg) (end ,end))
1580 (put-text-property beg end 'c-is-sws t)
1581 ,@(when (facep 'c-debug-is-sws-face)
1582 `((c-debug-add-face beg end 'c-debug-is-sws-face)))))
1583
1584(defmacro c-put-in-sws (beg end)
0386b551 1585 ;; This macro does a hidden buffer change.
d9e94c22
MS
1586 `(let ((beg ,beg) (end ,end))
1587 (put-text-property beg end 'c-in-sws t)
1588 ,@(when (facep 'c-debug-is-sws-face)
1589 `((c-debug-add-face beg end 'c-debug-in-sws-face)))))
1590
1591(defmacro c-remove-is-sws (beg end)
0386b551 1592 ;; This macro does a hidden buffer change.
d9e94c22
MS
1593 `(let ((beg ,beg) (end ,end))
1594 (remove-text-properties beg end '(c-is-sws nil))
1595 ,@(when (facep 'c-debug-is-sws-face)
1596 `((c-debug-remove-face beg end 'c-debug-is-sws-face)))))
1597
1598(defmacro c-remove-in-sws (beg end)
0386b551 1599 ;; This macro does a hidden buffer change.
d9e94c22
MS
1600 `(let ((beg ,beg) (end ,end))
1601 (remove-text-properties beg end '(c-in-sws nil))
1602 ,@(when (facep 'c-debug-is-sws-face)
1603 `((c-debug-remove-face beg end 'c-debug-in-sws-face)))))
1604
1605(defmacro c-remove-is-and-in-sws (beg end)
0386b551 1606 ;; This macro does a hidden buffer change.
d9e94c22
MS
1607 `(let ((beg ,beg) (end ,end))
1608 (remove-text-properties beg end '(c-is-sws nil c-in-sws nil))
1609 ,@(when (facep 'c-debug-is-sws-face)
1610 `((c-debug-remove-face beg end 'c-debug-is-sws-face)
1611 (c-debug-remove-face beg end 'c-debug-in-sws-face)))))
1612
1613(defsubst c-invalidate-sws-region-after (beg end)
1614 ;; Called from `after-change-functions'. Note that if
1615 ;; `c-forward-sws' or `c-backward-sws' are used outside
1616 ;; `c-save-buffer-state' or similar then this will remove the cache
1617 ;; properties right after they're added.
0386b551
AM
1618 ;;
1619 ;; This function does hidden buffer changes.
d9e94c22
MS
1620
1621 (save-excursion
1622 ;; Adjust the end to remove the properties in any following simple
1623 ;; ws up to and including the next line break, if there is any
1624 ;; after the changed region. This is necessary e.g. when a rung
1625 ;; marked empty line is converted to a line comment by inserting
1626 ;; "//" before the line break. In that case the line break would
1627 ;; keep the rung mark which could make a later `c-backward-sws'
1628 ;; move into the line comment instead of over it.
1629 (goto-char end)
1630 (skip-chars-forward " \t\f\v")
1631 (when (and (eolp) (not (eobp)))
1632 (setq end (1+ (point)))))
1633
1634 (when (and (= beg end)
1635 (get-text-property beg 'c-in-sws)
2a15eb73 1636 (> beg (point-min))
d9e94c22
MS
1637 (get-text-property (1- beg) 'c-in-sws))
1638 ;; Ensure that an `c-in-sws' range gets broken. Note that it isn't
1639 ;; safe to keep a range that was continuous before the change. E.g:
1640 ;;
1641 ;; #define foo
1642 ;; \
1643 ;; bar
1644 ;;
1645 ;; There can be a "ladder" between "#" and "b". Now, if the newline
1646 ;; after "foo" is removed then "bar" will become part of the cpp
1647 ;; directive instead of a syntactically relevant token. In that
1648 ;; case there's no longer syntactic ws from "#" to "b".
1649 (setq beg (1- beg)))
1650
1651 (c-debug-sws-msg "c-invalidate-sws-region-after [%s..%s]" beg end)
1652 (c-remove-is-and-in-sws beg end))
1653
1654(defun c-forward-sws ()
1655 ;; Used by `c-forward-syntactic-ws' to implement the unbounded search.
0386b551
AM
1656 ;;
1657 ;; This function might do hidden buffer changes.
d9e94c22
MS
1658
1659 (let (;; `rung-pos' is set to a position as early as possible in the
1660 ;; unmarked part of the simple ws region.
1661 (rung-pos (point)) next-rung-pos rung-end-pos last-put-in-sws-pos
1662 rung-is-marked next-rung-is-marked simple-ws-end
1663 ;; `safe-start' is set when it's safe to cache the start position.
1664 ;; It's not set if we've initially skipped over comments and line
1665 ;; continuations since we might have gone out through the end of a
1666 ;; macro then. This provision makes `c-forward-sws' not populate the
1667 ;; cache in the majority of cases, but otoh is `c-backward-sws' by far
1668 ;; more common.
1669 safe-start)
1670
1671 ;; Skip simple ws and do a quick check on the following character to see
1672 ;; if it's anything that can't start syntactic ws, so we can bail out
1673 ;; early in the majority of cases when there just are a few ws chars.
1674 (skip-chars-forward " \t\n\r\f\v")
1675 (when (looking-at c-syntactic-ws-start)
1676
1677 (setq rung-end-pos (min (1+ (point)) (point-max)))
1678 (if (setq rung-is-marked (text-property-any rung-pos rung-end-pos
1679 'c-is-sws t))
1680 ;; Find the last rung position to avoid setting properties in all
1681 ;; the cases when the marked rung is complete.
1682 ;; (`next-single-property-change' is certain to move at least one
1683 ;; step forward.)
1684 (setq rung-pos (1- (next-single-property-change
1685 rung-is-marked 'c-is-sws nil rung-end-pos)))
1686 ;; Got no marked rung here. Since the simple ws might have started
1687 ;; inside a line comment or cpp directive we must set `rung-pos' as
1688 ;; high as possible.
1689 (setq rung-pos (point)))
1690
2143fa32 1691 (with-silent-modifications
d9e94c22
MS
1692 (while
1693 (progn
1694 (while
1695 (when (and rung-is-marked
1696 (get-text-property (point) 'c-in-sws))
1697
1698 ;; The following search is the main reason that `c-in-sws'
1699 ;; and `c-is-sws' aren't combined to one property.
1700 (goto-char (next-single-property-change
1701 (point) 'c-in-sws nil (point-max)))
1702 (unless (get-text-property (point) 'c-is-sws)
1703 ;; If the `c-in-sws' region extended past the last
1704 ;; `c-is-sws' char we have to go back a bit.
1705 (or (get-text-property (1- (point)) 'c-is-sws)
1706 (goto-char (previous-single-property-change
1707 (point) 'c-is-sws)))
1708 (backward-char))
1709
1710 (c-debug-sws-msg
1711 "c-forward-sws cached move %s -> %s (max %s)"
1712 rung-pos (point) (point-max))
1713
1714 (setq rung-pos (point))
1715 (and (> (skip-chars-forward " \t\n\r\f\v") 0)
1716 (not (eobp))))
1717
1718 ;; We'll loop here if there is simple ws after the last rung.
1719 ;; That means that there's been some change in it and it's
1720 ;; possible that we've stepped into another ladder, so extend
1721 ;; the previous one to join with it if there is one, and try to
1722 ;; use the cache again.
1723 (c-debug-sws-msg
1724 "c-forward-sws extending rung with [%s..%s] (max %s)"
1725 (1+ rung-pos) (1+ (point)) (point-max))
1726 (unless (get-text-property (point) 'c-is-sws)
1727 ;; Remove any `c-in-sws' property from the last char of
1728 ;; the rung before we mark it with `c-is-sws', so that we
1729 ;; won't connect with the remains of a broken "ladder".
1730 (c-remove-in-sws (point) (1+ (point))))
1731 (c-put-is-sws (1+ rung-pos)
1732 (1+ (point)))
1733 (c-put-in-sws rung-pos
1734 (setq rung-pos (point)
1735 last-put-in-sws-pos rung-pos)))
1736
1737 (setq simple-ws-end (point))
1738 (c-forward-comments)
1739
1740 (cond
1741 ((/= (point) simple-ws-end)
1742 ;; Skipped over comments. Don't cache at eob in case the buffer
1743 ;; is narrowed.
1744 (not (eobp)))
1745
1746 ((save-excursion
1747 (and c-opt-cpp-prefix
1748 (looking-at c-opt-cpp-start)
1749 (progn (skip-chars-backward " \t")
1750 (bolp))
1751 (or (bobp)
1752 (progn (backward-char)
1753 (not (eq (char-before) ?\\))))))
1754 ;; Skip a preprocessor directive.
1755 (end-of-line)
1756 (while (and (eq (char-before) ?\\)
1757 (= (forward-line 1) 0))
1758 (end-of-line))
1759 (forward-line 1)
1760 (setq safe-start t)
1761 ;; Don't cache at eob in case the buffer is narrowed.
1762 (not (eobp)))))
1763
1764 ;; We've searched over a piece of non-white syntactic ws. See if this
1765 ;; can be cached.
1766 (setq next-rung-pos (point))
1767 (skip-chars-forward " \t\n\r\f\v")
1768 (setq rung-end-pos (min (1+ (point)) (point-max)))
1769
1770 (if (or
1771 ;; Cache if we haven't skipped comments only, and if we started
1772 ;; either from a marked rung or from a completely uncached
1773 ;; position.
1774 (and safe-start
1775 (or rung-is-marked
1776 (not (get-text-property simple-ws-end 'c-in-sws))))
1777
1778 ;; See if there's a marked rung in the encountered simple ws. If
1779 ;; so then we can cache, unless `safe-start' is nil. Even then
1780 ;; we need to do this to check if the cache can be used for the
1781 ;; next step.
1782 (and (setq next-rung-is-marked
1783 (text-property-any next-rung-pos rung-end-pos
1784 'c-is-sws t))
1785 safe-start))
b2acd789 1786
0ec8351b 1787 (progn
d9e94c22
MS
1788 (c-debug-sws-msg
1789 "c-forward-sws caching [%s..%s] - [%s..%s] (max %s)"
1790 rung-pos (1+ simple-ws-end) next-rung-pos rung-end-pos
1791 (point-max))
1792
1793 ;; Remove the properties for any nested ws that might be cached.
1794 ;; Only necessary for `c-is-sws' since `c-in-sws' will be set
1795 ;; anyway.
1796 (c-remove-is-sws (1+ simple-ws-end) next-rung-pos)
1797 (unless (and rung-is-marked (= rung-pos simple-ws-end))
1798 (c-put-is-sws rung-pos
1799 (1+ simple-ws-end))
1800 (setq rung-is-marked t))
1801 (c-put-in-sws rung-pos
1802 (setq rung-pos (point)
1803 last-put-in-sws-pos rung-pos))
1804 (unless (get-text-property (1- rung-end-pos) 'c-is-sws)
1805 ;; Remove any `c-in-sws' property from the last char of
1806 ;; the rung before we mark it with `c-is-sws', so that we
1807 ;; won't connect with the remains of a broken "ladder".
1808 (c-remove-in-sws (1- rung-end-pos) rung-end-pos))
1809 (c-put-is-sws next-rung-pos
1810 rung-end-pos))
1811
1812 (c-debug-sws-msg
1813 "c-forward-sws not caching [%s..%s] - [%s..%s] (max %s)"
1814 rung-pos (1+ simple-ws-end) next-rung-pos rung-end-pos
1815 (point-max))
1816
1817 ;; Set `rung-pos' for the next rung. It's the same thing here as
1818 ;; initially, except that the rung position is set as early as
1819 ;; possible since we can't be in the ending ws of a line comment or
1820 ;; cpp directive now.
1821 (if (setq rung-is-marked next-rung-is-marked)
1822 (setq rung-pos (1- (next-single-property-change
1823 rung-is-marked 'c-is-sws nil rung-end-pos)))
1824 (setq rung-pos next-rung-pos))
1825 (setq safe-start t)))
1826
1827 ;; Make sure that the newly marked `c-in-sws' region doesn't connect to
1828 ;; another one after the point (which might occur when editing inside a
1829 ;; comment or macro).
1830 (when (eq last-put-in-sws-pos (point))
1831 (cond ((< last-put-in-sws-pos (point-max))
1832 (c-debug-sws-msg
1833 "c-forward-sws clearing at %s for cache separation"
1834 last-put-in-sws-pos)
1835 (c-remove-in-sws last-put-in-sws-pos
1836 (1+ last-put-in-sws-pos)))
1837 (t
1838 ;; If at eob we have to clear the last character before the end
1839 ;; instead since the buffer might be narrowed and there might
1840 ;; be a `c-in-sws' after (point-max). In this case it's
1841 ;; necessary to clear both properties.
1842 (c-debug-sws-msg
1843 "c-forward-sws clearing thoroughly at %s for cache separation"
1844 (1- last-put-in-sws-pos))
1845 (c-remove-is-and-in-sws (1- last-put-in-sws-pos)
1846 last-put-in-sws-pos))))
2143fa32 1847 ))))
b2acd789 1848
d9e94c22
MS
1849(defun c-backward-sws ()
1850 ;; Used by `c-backward-syntactic-ws' to implement the unbounded search.
0386b551
AM
1851 ;;
1852 ;; This function might do hidden buffer changes.
d9e94c22
MS
1853
1854 (let (;; `rung-pos' is set to a position as late as possible in the unmarked
1855 ;; part of the simple ws region.
1856 (rung-pos (point)) next-rung-pos last-put-in-sws-pos
1857 rung-is-marked simple-ws-beg cmt-skip-pos)
1858
1859 ;; Skip simple horizontal ws and do a quick check on the preceding
91af3942 1860 ;; character to see if it's anything that can't end syntactic ws, so we can
d9e94c22
MS
1861 ;; bail out early in the majority of cases when there just are a few ws
1862 ;; chars. Newlines are complicated in the backward direction, so we can't
1863 ;; skip over them.
1864 (skip-chars-backward " \t\f")
1865 (when (and (not (bobp))
1866 (save-excursion
1867 (backward-char)
1868 (looking-at c-syntactic-ws-end)))
1869
1870 ;; Try to find a rung position in the simple ws preceding point, so that
1871 ;; we can get a cache hit even if the last bit of the simple ws has
1872 ;; changed recently.
1873 (setq simple-ws-beg (point))
1874 (skip-chars-backward " \t\n\r\f\v")
1875 (if (setq rung-is-marked (text-property-any
1876 (point) (min (1+ rung-pos) (point-max))
1877 'c-is-sws t))
1878 ;; `rung-pos' will be the earliest marked position, which means that
1879 ;; there might be later unmarked parts in the simple ws region.
1880 ;; It's not worth the effort to fix that; the last part of the
1881 ;; simple ws is also typically edited often, so it could be wasted.
1882 (goto-char (setq rung-pos rung-is-marked))
1883 (goto-char simple-ws-beg))
1884
2143fa32 1885 (with-silent-modifications
d9e94c22
MS
1886 (while
1887 (progn
1888 (while
1889 (when (and rung-is-marked
1890 (not (bobp))
1891 (get-text-property (1- (point)) 'c-in-sws))
1892
1893 ;; The following search is the main reason that `c-in-sws'
1894 ;; and `c-is-sws' aren't combined to one property.
1895 (goto-char (previous-single-property-change
1896 (point) 'c-in-sws nil (point-min)))
1897 (unless (get-text-property (point) 'c-is-sws)
1898 ;; If the `c-in-sws' region extended past the first
1899 ;; `c-is-sws' char we have to go forward a bit.
1900 (goto-char (next-single-property-change
1901 (point) 'c-is-sws)))
1902
1903 (c-debug-sws-msg
1904 "c-backward-sws cached move %s <- %s (min %s)"
1905 (point) rung-pos (point-min))
1906
1907 (setq rung-pos (point))
1908 (if (and (< (min (skip-chars-backward " \t\f\v")
1909 (progn
1910 (setq simple-ws-beg (point))
1911 (skip-chars-backward " \t\n\r\f\v")))
1912 0)
1913 (setq rung-is-marked
1914 (text-property-any (point) rung-pos
1915 'c-is-sws t)))
1916 t
1917 (goto-char simple-ws-beg)
1918 nil))
1919
1920 ;; We'll loop here if there is simple ws before the first rung.
1921 ;; That means that there's been some change in it and it's
1922 ;; possible that we've stepped into another ladder, so extend
1923 ;; the previous one to join with it if there is one, and try to
1924 ;; use the cache again.
1925 (c-debug-sws-msg
1926 "c-backward-sws extending rung with [%s..%s] (min %s)"
1927 rung-is-marked rung-pos (point-min))
1928 (unless (get-text-property (1- rung-pos) 'c-is-sws)
1929 ;; Remove any `c-in-sws' property from the last char of
1930 ;; the rung before we mark it with `c-is-sws', so that we
1931 ;; won't connect with the remains of a broken "ladder".
1932 (c-remove-in-sws (1- rung-pos) rung-pos))
1933 (c-put-is-sws rung-is-marked
1934 rung-pos)
1935 (c-put-in-sws rung-is-marked
1936 (1- rung-pos))
1937 (setq rung-pos rung-is-marked
1938 last-put-in-sws-pos rung-pos))
1939
1940 (c-backward-comments)
1941 (setq cmt-skip-pos (point))
a66cd3ee 1942
d9e94c22
MS
1943 (cond
1944 ((and c-opt-cpp-prefix
1945 (/= cmt-skip-pos simple-ws-beg)
1946 (c-beginning-of-macro))
1947 ;; Inside a cpp directive. See if it should be skipped over.
1948 (let ((cpp-beg (point)))
1949
1950 ;; Move back over all line continuations in the region skipped
1951 ;; over by `c-backward-comments'. If we go past it then we
1952 ;; started inside the cpp directive.
1953 (goto-char simple-ws-beg)
1954 (beginning-of-line)
1955 (while (and (> (point) cmt-skip-pos)
1956 (progn (backward-char)
1957 (eq (char-before) ?\\)))
1958 (beginning-of-line))
1959
1960 (if (< (point) cmt-skip-pos)
1961 ;; Don't move past the cpp directive if we began inside
1962 ;; it. Note that the position at the end of the last line
1963 ;; of the macro is also considered to be within it.
1964 (progn (goto-char cmt-skip-pos)
1965 nil)
1966
1967 ;; It's worthwhile to spend a little bit of effort on finding
1968 ;; the end of the macro, to get a good `simple-ws-beg'
1969 ;; position for the cache. Note that `c-backward-comments'
1970 ;; could have stepped over some comments before going into
1971 ;; the macro, and then `simple-ws-beg' must be kept on the
1972 ;; same side of those comments.
1973 (goto-char simple-ws-beg)
1974 (skip-chars-backward " \t\n\r\f\v")
1975 (if (eq (char-before) ?\\)
1976 (forward-char))
1977 (forward-line 1)
1978 (if (< (point) simple-ws-beg)
1979 ;; Might happen if comments after the macro were skipped
1980 ;; over.
1981 (setq simple-ws-beg (point)))
1982
1983 (goto-char cpp-beg)
1984 t)))
1985
1986 ((/= (save-excursion
1987 (skip-chars-forward " \t\n\r\f\v" simple-ws-beg)
1988 (setq next-rung-pos (point)))
1989 simple-ws-beg)
1990 ;; Skipped over comments. Must put point at the end of
1991 ;; the simple ws at point since we might be after a line
1992 ;; comment or cpp directive that's been partially
1993 ;; narrowed out, and we can't risk marking the simple ws
1994 ;; at the end of it.
1995 (goto-char next-rung-pos)
1996 t)))
1997
1998 ;; We've searched over a piece of non-white syntactic ws. See if this
1999 ;; can be cached.
2000 (setq next-rung-pos (point))
2001 (skip-chars-backward " \t\f\v")
2002
2003 (if (or
2004 ;; Cache if we started either from a marked rung or from a
2005 ;; completely uncached position.
2006 rung-is-marked
2007 (not (get-text-property (1- simple-ws-beg) 'c-in-sws))
2008
2009 ;; Cache if there's a marked rung in the encountered simple ws.
2010 (save-excursion
2011 (skip-chars-backward " \t\n\r\f\v")
2012 (text-property-any (point) (min (1+ next-rung-pos) (point-max))
2013 'c-is-sws t)))
a66cd3ee 2014
d9e94c22
MS
2015 (progn
2016 (c-debug-sws-msg
2017 "c-backward-sws caching [%s..%s] - [%s..%s] (min %s)"
2018 (point) (1+ next-rung-pos)
2019 simple-ws-beg (min (1+ rung-pos) (point-max))
2020 (point-min))
2021
2022 ;; Remove the properties for any nested ws that might be cached.
2023 ;; Only necessary for `c-is-sws' since `c-in-sws' will be set
2024 ;; anyway.
2025 (c-remove-is-sws (1+ next-rung-pos) simple-ws-beg)
2026 (unless (and rung-is-marked (= simple-ws-beg rung-pos))
2027 (let ((rung-end-pos (min (1+ rung-pos) (point-max))))
2028 (unless (get-text-property (1- rung-end-pos) 'c-is-sws)
2029 ;; Remove any `c-in-sws' property from the last char of
2030 ;; the rung before we mark it with `c-is-sws', so that we
2031 ;; won't connect with the remains of a broken "ladder".
2032 (c-remove-in-sws (1- rung-end-pos) rung-end-pos))
2033 (c-put-is-sws simple-ws-beg
2034 rung-end-pos)
2035 (setq rung-is-marked t)))
2036 (c-put-in-sws (setq simple-ws-beg (point)
2037 last-put-in-sws-pos simple-ws-beg)
2038 rung-pos)
2039 (c-put-is-sws (setq rung-pos simple-ws-beg)
2040 (1+ next-rung-pos)))
2041
2042 (c-debug-sws-msg
2043 "c-backward-sws not caching [%s..%s] - [%s..%s] (min %s)"
2044 (point) (1+ next-rung-pos)
2045 simple-ws-beg (min (1+ rung-pos) (point-max))
2046 (point-min))
2047 (setq rung-pos next-rung-pos
2048 simple-ws-beg (point))
2049 ))
2050
2051 ;; Make sure that the newly marked `c-in-sws' region doesn't connect to
2052 ;; another one before the point (which might occur when editing inside a
2053 ;; comment or macro).
2054 (when (eq last-put-in-sws-pos (point))
2055 (cond ((< (point-min) last-put-in-sws-pos)
2056 (c-debug-sws-msg
2057 "c-backward-sws clearing at %s for cache separation"
2058 (1- last-put-in-sws-pos))
2059 (c-remove-in-sws (1- last-put-in-sws-pos)
2060 last-put-in-sws-pos))
2061 ((> (point-min) 1)
2062 ;; If at bob and the buffer is narrowed, we have to clear the
2063 ;; character we're standing on instead since there might be a
2064 ;; `c-in-sws' before (point-min). In this case it's necessary
2065 ;; to clear both properties.
2066 (c-debug-sws-msg
2067 "c-backward-sws clearing thoroughly at %s for cache separation"
2068 last-put-in-sws-pos)
2069 (c-remove-is-and-in-sws last-put-in-sws-pos
2070 (1+ last-put-in-sws-pos)))))
2143fa32 2071 ))))
785eecbb 2072
d9e94c22 2073\f
580fba94
AM
2074;; Other whitespace tools
2075(defun c-partial-ws-p (beg end)
2076 ;; Is the region (beg end) WS, and is there WS (or BOB/EOB) next to the
2077 ;; region? This is a "heuristic" function. .....
b414f371 2078 ;;
c421028a
AM
2079 ;; The motivation for the second bit is to check whether removing this
2080 ;; region would coalesce two symbols.
580fba94
AM
2081 ;;
2082 ;; FIXME!!! This function doesn't check virtual semicolons in any way. Be
2083 ;; careful about using this function for, e.g. AWK. (2007/3/7)
2084 (save-excursion
2085 (let ((end+1 (min (1+ end) (point-max))))
2086 (or (progn (goto-char (max (point-min) (1- beg)))
2087 (c-skip-ws-forward end)
2088 (eq (point) end))
2089 (progn (goto-char beg)
2090 (c-skip-ws-forward end+1)
2091 (eq (point) end+1))))))
2092\f
0386b551 2093;; A system for finding noteworthy parens before the point.
e1c458ae 2094
0ec1d2c5
AM
2095(defconst c-state-cache-too-far 5000)
2096;; A maximum comfortable scanning distance, e.g. between
2097;; `c-state-cache-good-pos' and "HERE" (where we call c-parse-state). When
0d26e0b6 2098;; this distance is exceeded, we take "emergency measures", e.g. by clearing
0ec1d2c5
AM
2099;; the cache and starting again from point-min or a beginning of defun. This
2100;; value can be tuned for efficiency or set to a lower value for testing.
2101
d9e94c22
MS
2102(defvar c-state-cache nil)
2103(make-variable-buffer-local 'c-state-cache)
2104;; The state cache used by `c-parse-state' to cut down the amount of
0ec1d2c5
AM
2105;; searching. It's the result from some earlier `c-parse-state' call. See
2106;; `c-parse-state''s doc string for details of its structure.
0386b551 2107;;
d9e94c22
MS
2108;; The use of the cached info is more effective if the next
2109;; `c-parse-state' call is on a line close by the one the cached state
2110;; was made at; the cache can actually slow down a little if the
2111;; cached state was made very far back in the buffer. The cache is
2112;; most effective if `c-parse-state' is used on each line while moving
2113;; forward.
e1c458ae 2114
0386b551
AM
2115(defvar c-state-cache-good-pos 1)
2116(make-variable-buffer-local 'c-state-cache-good-pos)
0ec1d2c5
AM
2117;; This is a position where `c-state-cache' is known to be correct, or
2118;; nil (see below). It's a position inside one of the recorded unclosed
2119;; parens or the top level, but not further nested inside any literal or
2120;; subparen that is closed before the last recorded position.
0386b551
AM
2121;;
2122;; The exact position is chosen to try to be close to yet earlier than
2123;; the position where `c-state-cache' will be called next. Right now
2124;; the heuristic is to set it to the position after the last found
2125;; closing paren (of any type) before the line on which
2126;; `c-parse-state' was called. That is chosen primarily to work well
2127;; with refontification of the current line.
0ec1d2c5
AM
2128;;
2129;; 2009-07-28: When `c-state-point-min' and the last position where
2130;; `c-parse-state' or for which `c-invalidate-state-cache' was called, are
2131;; both in the same literal, there is no such "good position", and
2132;; c-state-cache-good-pos is then nil. This is the ONLY circumstance in which
2133;; it can be nil. In this case, `c-state-point-min-literal' will be non-nil.
2134;;
2135;; 2009-06-12: In a brace desert, c-state-cache-good-pos may also be in
2136;; the middle of the desert, as long as it is not within a brace pair
2137;; recorded in `c-state-cache' or a paren/bracket pair.
2138
2139
2140;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2141;; We maintain a simple cache of positions which aren't in a literal, so as to
2142;; speed up testing for non-literality.
9657183b 2143(defconst c-state-nonlit-pos-interval 3000)
0ec1d2c5
AM
2144;; The approximate interval between entries in `c-state-nonlit-pos-cache'.
2145
2146(defvar c-state-nonlit-pos-cache nil)
2147(make-variable-buffer-local 'c-state-nonlit-pos-cache)
e84efb70
AM
2148;; A list of buffer positions which are known not to be in a literal or a cpp
2149;; construct. This is ordered with higher positions at the front of the list.
2150;; Only those which are less than `c-state-nonlit-pos-cache-limit' are valid.
0ec1d2c5
AM
2151
2152(defvar c-state-nonlit-pos-cache-limit 1)
2153(make-variable-buffer-local 'c-state-nonlit-pos-cache-limit)
2154;; An upper limit on valid entries in `c-state-nonlit-pos-cache'. This is
2155;; reduced by buffer changes, and increased by invocations of
2156;; `c-state-literal-at'.
2157
56d093a9
AM
2158(defvar c-state-semi-nonlit-pos-cache nil)
2159(make-variable-buffer-local 'c-state-semi-nonlit-pos-cache)
2160;; A list of buffer positions which are known not to be in a literal. This is
2161;; ordered with higher positions at the front of the list. Only those which
2162;; are less than `c-state-semi-nonlit-pos-cache-limit' are valid.
2163
2164(defvar c-state-semi-nonlit-pos-cache-limit 1)
2165(make-variable-buffer-local 'c-state-semi-nonlit-pos-cache-limit)
2166;; An upper limit on valid entries in `c-state-semi-nonlit-pos-cache'. This is
2167;; reduced by buffer changes, and increased by invocations of
f003f294 2168;; `c-state-literal-at'. FIXME!!!
56d093a9 2169
0ec1d2c5 2170(defsubst c-state-pp-to-literal (from to)
13d49cbb
AM
2171 ;; Do a parse-partial-sexp from FROM to TO, returning either
2172 ;; (STATE TYPE (BEG . END)) if TO is in a literal; or
2173 ;; (STATE) otherwise,
2174 ;; where STATE is the parsing state at TO, TYPE is the type of the literal
2175 ;; (one of 'c, 'c++, 'string) and (BEG . END) is the boundaries of the literal.
2176 ;;
2177 ;; Only elements 3 (in a string), 4 (in a comment), 5 (following a quote),
2178 ;; 7 (comment type) and 8 (start of comment/string) (and possibly 9) of
2179 ;; STATE are valid.
0ec1d2c5 2180 (save-excursion
13d49cbb
AM
2181 (let ((s (parse-partial-sexp from to))
2182 ty)
0ec1d2c5 2183 (when (or (nth 3 s) (nth 4 s)) ; in a string or comment
13d49cbb
AM
2184 (setq ty (cond
2185 ((nth 3 s) 'string)
2186 ((eq (nth 7 s) t) 'c++)
2187 (t 'c)))
0ec1d2c5
AM
2188 (parse-partial-sexp (point) (point-max)
2189 nil ; TARGETDEPTH
2190 nil ; STOPBEFORE
2191 s ; OLDSTATE
13d49cbb
AM
2192 'syntax-table)) ; stop at end of literal
2193 (if ty
2194 `(,s ,ty (,(nth 8 s) . ,(point)))
2195 `(,s)))))
2196
2197(defun c-state-safe-place (here)
2198 ;; Return a buffer position before HERE which is "safe", i.e. outside any
2199 ;; string, comment, or macro.
e84efb70
AM
2200 ;;
2201 ;; NOTE: This function manipulates `c-state-nonlit-pos-cache'. This cache
2202 ;; MAY NOT contain any positions within macros, since macros are frequently
2203 ;; turned into comments by use of the `c-cpp-delimiter' category properties.
2204 ;; We cannot rely on this mechanism whilst determining a cache pos since
2205 ;; this function is also called from outwith `c-parse-state'.
0ec1d2c5
AM
2206 (save-restriction
2207 (widen)
2208 (save-excursion
2209 (let ((c c-state-nonlit-pos-cache)
56d093a9 2210 pos npos high-pos lit macro-beg macro-end)
0ec1d2c5
AM
2211 ;; Trim the cache to take account of buffer changes.
2212 (while (and c (> (car c) c-state-nonlit-pos-cache-limit))
2213 (setq c (cdr c)))
2214 (setq c-state-nonlit-pos-cache c)
2215
2216 (while (and c (> (car c) here))
56d093a9 2217 (setq high-pos (car c))
0ec1d2c5
AM
2218 (setq c (cdr c)))
2219 (setq pos (or (car c) (point-min)))
2220
56d093a9
AM
2221 (unless high-pos
2222 (while
2223 ;; Add an element to `c-state-nonlit-pos-cache' each iteration.
2224 (and
2225 (<= (setq npos (+ pos c-state-nonlit-pos-interval)) here)
2226
2227 ;; Test for being in a literal. If so, go to after it.
2228 (progn
2229 (setq lit (car (cddr (c-state-pp-to-literal pos npos))))
2230 (or (null lit)
2231 (prog1 (<= (cdr lit) here)
2232 (setq npos (cdr lit)))))
2233
2234 ;; Test for being in a macro. If so, go to after it.
2235 (progn
2236 (goto-char npos)
2237 (setq macro-beg
2238 (and (c-beginning-of-macro) (/= (point) npos) (point)))
2239 (when macro-beg
2240 (c-syntactic-end-of-macro)
2241 (or (eobp) (forward-char))
2242 (setq macro-end (point)))
2243 (or (null macro-beg)
2244 (prog1 (<= macro-end here)
2245 (setq npos macro-end)))))
2246
2247 (setq pos npos)
2248 (setq c-state-nonlit-pos-cache (cons pos c-state-nonlit-pos-cache)))
2249 ;; Add one extra element above HERE so as to to avoid the previous
2250 ;; expensive calculation when the next call is close to the current
2251 ;; one. This is especially useful when inside a large macro.
2252 (setq c-state-nonlit-pos-cache (cons npos c-state-nonlit-pos-cache)))
0ec1d2c5
AM
2253
2254 (if (> pos c-state-nonlit-pos-cache-limit)
2255 (setq c-state-nonlit-pos-cache-limit pos))
13d49cbb 2256 pos))))
9858f6c3 2257
56d093a9
AM
2258(defun c-state-semi-safe-place (here)
2259 ;; Return a buffer position before HERE which is "safe", i.e. outside any
2260 ;; string or comment. It may be in a macro.
2261 (save-restriction
2262 (widen)
2263 (save-excursion
2264 (let ((c c-state-semi-nonlit-pos-cache)
2265 pos npos high-pos lit macro-beg macro-end)
2266 ;; Trim the cache to take account of buffer changes.
2267 (while (and c (> (car c) c-state-semi-nonlit-pos-cache-limit))
2268 (setq c (cdr c)))
2269 (setq c-state-semi-nonlit-pos-cache c)
2270
2271 (while (and c (> (car c) here))
2272 (setq high-pos (car c))
2273 (setq c (cdr c)))
2274 (setq pos (or (car c) (point-min)))
2275
2276 (unless high-pos
2277 (while
2278 ;; Add an element to `c-state-semi-nonlit-pos-cache' each iteration.
2279 (and
2280 (<= (setq npos (+ pos c-state-nonlit-pos-interval)) here)
2281
2282 ;; Test for being in a literal. If so, go to after it.
2283 (progn
2284 (setq lit (car (cddr (c-state-pp-to-literal pos npos))))
2285 (or (null lit)
2286 (prog1 (<= (cdr lit) here)
2287 (setq npos (cdr lit))))))
2288
2289 (setq pos npos)
2290 (setq c-state-semi-nonlit-pos-cache
2291 (cons pos c-state-semi-nonlit-pos-cache))))
2292
2293 (if (> pos c-state-semi-nonlit-pos-cache-limit)
2294 (setq c-state-semi-nonlit-pos-cache-limit pos))
2295 pos))))
2296
13d49cbb
AM
2297(defun c-state-literal-at (here)
2298 ;; If position HERE is inside a literal, return (START . END), the
2299 ;; boundaries of the literal (which may be outside the accessible bit of the
2300 ;; buffer). Otherwise, return nil.
2301 ;;
2302 ;; This function is almost the same as `c-literal-limits'. Previously, it
9858f6c3 2303 ;; differed in that it was a lower level function, and that it rigorously
13d49cbb
AM
2304 ;; followed the syntax from BOB. `c-literal-limits' is now (2011-12)
2305 ;; virtually identical to this function.
2306 (save-restriction
2307 (widen)
2308 (save-excursion
2309 (let ((pos (c-state-safe-place here)))
2310 (car (cddr (c-state-pp-to-literal pos here)))))))
0ec1d2c5
AM
2311
2312(defsubst c-state-lit-beg (pos)
2313 ;; Return the start of the literal containing POS, or POS itself.
2314 (or (car (c-state-literal-at pos))
2315 pos))
2316
2317(defsubst c-state-cache-non-literal-place (pos state)
9c4aeabf 2318 ;; Return a position outside of a string/comment/macro at or before POS.
0ec1d2c5 2319 ;; STATE is the parse-partial-sexp state at POS.
9c4aeabf
AM
2320 (let ((res (if (or (nth 3 state) ; in a string?
2321 (nth 4 state)) ; in a comment?
2322 (nth 8 state)
2323 pos)))
2324 (save-excursion
2325 (goto-char res)
2326 (if (c-beginning-of-macro)
2327 (point)
2328 res))))
0ec1d2c5
AM
2329
2330;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2331;; Stuff to do with point-min, and coping with any literal there.
2332(defvar c-state-point-min 1)
2333(make-variable-buffer-local 'c-state-point-min)
2334;; This is (point-min) when `c-state-cache' was last calculated. A change of
2335;; narrowing is likely to affect the parens that are visible before the point.
2336
2337(defvar c-state-point-min-lit-type nil)
2338(make-variable-buffer-local 'c-state-point-min-lit-type)
2339(defvar c-state-point-min-lit-start nil)
2340(make-variable-buffer-local 'c-state-point-min-lit-start)
2341;; These two variables define the literal, if any, containing point-min.
2342;; Their values are, respectively, 'string, c, or c++, and the start of the
2343;; literal. If there's no literal there, they're both nil.
2344
2345(defvar c-state-min-scan-pos 1)
2346(make-variable-buffer-local 'c-state-min-scan-pos)
2347;; This is the earliest buffer-pos from which scanning can be done. It is
2348;; either the end of the literal containing point-min, or point-min itself.
2349;; It becomes nil if the buffer is changed earlier than this point.
2350(defun c-state-get-min-scan-pos ()
2351 ;; Return the lowest valid scanning pos. This will be the end of the
2352 ;; literal enclosing point-min, or point-min itself.
2353 (or c-state-min-scan-pos
2354 (save-restriction
2355 (save-excursion
2356 (widen)
2357 (goto-char c-state-point-min-lit-start)
2358 (if (eq c-state-point-min-lit-type 'string)
2359 (forward-sexp)
2360 (forward-comment 1))
2361 (setq c-state-min-scan-pos (point))))))
2362
2363(defun c-state-mark-point-min-literal ()
2364 ;; Determine the properties of any literal containing POINT-MIN, setting the
2365 ;; variables `c-state-point-min-lit-type', `c-state-point-min-lit-start',
2366 ;; and `c-state-min-scan-pos' accordingly. The return value is meaningless.
2367 (let ((p-min (point-min))
2368 lit)
2369 (save-restriction
2370 (widen)
2371 (setq lit (c-state-literal-at p-min))
2372 (if lit
2373 (setq c-state-point-min-lit-type
2374 (save-excursion
2375 (goto-char (car lit))
2376 (cond
2377 ((looking-at c-block-comment-start-regexp) 'c)
2378 ((looking-at c-line-comment-starter) 'c++)
2379 (t 'string)))
2380 c-state-point-min-lit-start (car lit)
2381 c-state-min-scan-pos (cdr lit))
2382 (setq c-state-point-min-lit-type nil
2383 c-state-point-min-lit-start nil
2384 c-state-min-scan-pos p-min)))))
2385
2386
2387;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2388;; A variable which signals a brace dessert - helpful for reducing the number
2389;; of fruitless backward scans.
2390(defvar c-state-brace-pair-desert nil)
2391(make-variable-buffer-local 'c-state-brace-pair-desert)
e84efb70 2392;; Used only in `c-append-lower-brace-pair-to-state-cache'. It is set when
0ec1d2c5
AM
2393;; that defun has searched backwards for a brace pair and not found one. Its
2394;; value is either nil or a cons (PA . FROM), where PA is the position of the
2395;; enclosing opening paren/brace/bracket which bounds the backwards search (or
2396;; nil when at top level) and FROM is where the backward search started. It
2397;; is reset to nil in `c-invalidate-state-cache'.
2398
2399
2400;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2401;; Lowish level functions/macros which work directly on `c-state-cache', or a
2402;; list of like structure.
2403(defmacro c-state-cache-top-lparen (&optional cache)
2404 ;; Return the address of the top left brace/bracket/paren recorded in CACHE
2405 ;; (default `c-state-cache') (or nil).
2406 (let ((cash (or cache 'c-state-cache)))
2407 `(if (consp (car ,cash))
2408 (caar ,cash)
2409 (car ,cash))))
2410
2411(defmacro c-state-cache-top-paren (&optional cache)
2412 ;; Return the address of the latest brace/bracket/paren (whether left or
2413 ;; right) recorded in CACHE (default `c-state-cache') or nil.
2414 (let ((cash (or cache 'c-state-cache)))
2415 `(if (consp (car ,cash))
2416 (cdar ,cash)
2417 (car ,cash))))
2418
2419(defmacro c-state-cache-after-top-paren (&optional cache)
2420 ;; Return the position just after the latest brace/bracket/paren (whether
2421 ;; left or right) recorded in CACHE (default `c-state-cache') or nil.
2422 (let ((cash (or cache 'c-state-cache)))
2423 `(if (consp (car ,cash))
2424 (cdar ,cash)
2425 (and (car ,cash)
2426 (1+ (car ,cash))))))
2427
2428(defun c-get-cache-scan-pos (here)
2429 ;; From the state-cache, determine the buffer position from which we might
2430 ;; scan forward to HERE to update this cache. This position will be just
2431 ;; after a paren/brace/bracket recorded in the cache, if possible, otherwise
2432 ;; return the earliest position in the accessible region which isn't within
2433 ;; a literal. If the visible portion of the buffer is entirely within a
2434 ;; literal, return NIL.
2435 (let ((c c-state-cache) elt)
2436 ;(while (>= (or (c-state-cache-top-lparen c) 1) here)
2437 (while (and c
2438 (>= (c-state-cache-top-lparen c) here))
2439 (setq c (cdr c)))
2440
2441 (setq elt (car c))
2442 (cond
2443 ((consp elt)
2444 (if (> (cdr elt) here)
2445 (1+ (car elt))
2446 (cdr elt)))
2447 (elt (1+ elt))
2448 ((<= (c-state-get-min-scan-pos) here)
2449 (c-state-get-min-scan-pos))
2450 (t nil))))
2451
2452;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2453;; Variables which keep track of preprocessor constructs.
2454(defvar c-state-old-cpp-beg nil)
2455(make-variable-buffer-local 'c-state-old-cpp-beg)
2456(defvar c-state-old-cpp-end nil)
2457(make-variable-buffer-local 'c-state-old-cpp-end)
2458;; These are the limits of the macro containing point at the previous call of
2459;; `c-parse-state', or nil.
2460
2461;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
c7015153 2462;; Defuns which analyze the buffer, yet don't change `c-state-cache'.
0ec1d2c5
AM
2463(defun c-get-fallback-scan-pos (here)
2464 ;; Return a start position for building `c-state-cache' from
2465 ;; scratch. This will be at the top level, 2 defuns back.
0386b551
AM
2466 (save-excursion
2467 ;; Go back 2 bods, but ignore any bogus positions returned by
2468 ;; beginning-of-defun (i.e. open paren in column zero).
2469 (goto-char here)
2470 (let ((cnt 2))
2471 (while (not (or (bobp) (zerop cnt)))
0ec1d2c5 2472 (c-beginning-of-defun-1) ; Pure elisp BOD.
0386b551
AM
2473 (if (eq (char-after) ?\{)
2474 (setq cnt (1- cnt)))))
2475 (point)))
2476
c0209c2c
AM
2477(defun c-state-balance-parens-backwards (here- here+ top)
2478 ;; Return the position of the opening paren/brace/bracket before HERE- which
2479 ;; matches the outermost close p/b/b between HERE+ and TOP. Except when
2480 ;; there's a macro, HERE- and HERE+ are the same. Like this:
0ec1d2c5 2481 ;;
c0209c2c
AM
2482 ;; ............................................
2483 ;; | |
2484 ;; ( [ ( .........#macro.. ) ( ) ] )
2485 ;; ^ ^ ^ ^
2486 ;; | | | |
2487 ;; return HERE- HERE+ TOP
0ec1d2c5
AM
2488 ;;
2489 ;; If there aren't enough opening paren/brace/brackets, return the position
c0209c2c 2490 ;; of the outermost one found, or HERE- if there are none. If there are no
da6062e6 2491 ;; closing p/b/bs between HERE+ and TOP, return HERE-. HERE-/+ and TOP
c0209c2c
AM
2492 ;; must not be inside literals. Only the accessible portion of the buffer
2493 ;; will be scanned.
2494
2495 ;; PART 1: scan from `here+' up to `top', accumulating ")"s which enclose
2496 ;; `here'. Go round the next loop each time we pass over such a ")". These
2497 ;; probably match "("s before `here-'.
0ec1d2c5
AM
2498 (let (pos pa ren+1 lonely-rens)
2499 (save-excursion
2500 (save-restriction
2501 (narrow-to-region (point-min) top) ; This can move point, sometimes.
c0209c2c 2502 (setq pos here+)
0ec1d2c5
AM
2503 (c-safe
2504 (while
2505 (setq ren+1 (scan-lists pos 1 1)) ; might signal
2506 (setq lonely-rens (cons ren+1 lonely-rens)
2507 pos ren+1)))))
2508
c0209c2c 2509 ;; PART 2: Scan back before `here-' searching for the "("s
0ec1d2c5
AM
2510 ;; matching/mismatching the ")"s found above. We only need to direct the
2511 ;; caller to scan when we've encountered unmatched right parens.
c0209c2c
AM
2512 (setq pos here-)
2513 (when lonely-rens
2514 (c-safe
2515 (while
2516 (and lonely-rens ; actual values aren't used.
2517 (setq pa (scan-lists pos -1 1)))
2518 (setq pos pa)
2519 (setq lonely-rens (cdr lonely-rens)))))
2520 pos))
0ec1d2c5
AM
2521
2522(defun c-parse-state-get-strategy (here good-pos)
2523 ;; Determine the scanning strategy for adjusting `c-parse-state', attempting
53964682 2524 ;; to minimize the amount of scanning. HERE is the pertinent position in
0ec1d2c5
AM
2525 ;; the buffer, GOOD-POS is a position where `c-state-cache' (possibly with
2526 ;; its head trimmed) is known to be good, or nil if there is no such
2527 ;; position.
2528 ;;
2529 ;; The return value is a list, one of the following:
2530 ;;
2531 ;; o - ('forward CACHE-POS START-POINT) - scan forward from START-POINT,
2532 ;; which is not less than CACHE-POS.
2533 ;; o - ('backward CACHE-POS nil) - scan backwards (from HERE).
2534 ;; o - ('BOD nil START-POINT) - scan forwards from START-POINT, which is at the
2535 ;; top level.
2536 ;; o - ('IN-LIT nil nil) - point is inside the literal containing point-min.
2537 ;; , where CACHE-POS is the highest position recorded in `c-state-cache' at
2538 ;; or below HERE.
2539 (let ((cache-pos (c-get-cache-scan-pos here)) ; highest position below HERE in cache (or 1)
2540 BOD-pos ; position of 2nd BOD before HERE.
2541 strategy ; 'forward, 'backward, 'BOD, or 'IN-LIT.
2542 start-point
2543 how-far) ; putative scanning distance.
2544 (setq good-pos (or good-pos (c-state-get-min-scan-pos)))
2545 (cond
2546 ((< here (c-state-get-min-scan-pos))
2547 (setq strategy 'IN-LIT
2548 start-point nil
2549 cache-pos nil
2550 how-far 0))
2551 ((<= good-pos here)
2552 (setq strategy 'forward
2553 start-point (max good-pos cache-pos)
2554 how-far (- here start-point)))
2555 ((< (- good-pos here) (- here cache-pos)) ; FIXME!!! ; apply some sort of weighting.
2556 (setq strategy 'backward
2557 how-far (- good-pos here)))
2558 (t
2559 (setq strategy 'forward
2560 how-far (- here cache-pos)
2561 start-point cache-pos)))
2562
2563 ;; Might we be better off starting from the top level, two defuns back,
2564 ;; instead?
2565 (when (> how-far c-state-cache-too-far)
2566 (setq BOD-pos (c-get-fallback-scan-pos here)) ; somewhat EXPENSIVE!!!
2567 (if (< (- here BOD-pos) how-far)
2568 (setq strategy 'BOD
2569 start-point BOD-pos)))
2570
2571 (list
2572 strategy
2573 (and (memq strategy '(forward backward)) cache-pos)
2574 (and (memq strategy '(forward BOD)) start-point))))
2575
2576
2577;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2578;; Routines which change `c-state-cache' and associated values.
2579(defun c-renarrow-state-cache ()
2580 ;; The region (more precisely, point-min) has changed since we
2581 ;; calculated `c-state-cache'. Amend `c-state-cache' accordingly.
2582 (if (< (point-min) c-state-point-min)
2583 ;; If point-min has MOVED BACKWARDS then we drop the state completely.
2584 ;; It would be possible to do a better job here and recalculate the top
2585 ;; only.
2586 (progn
2587 (c-state-mark-point-min-literal)
2588 (setq c-state-cache nil
2589 c-state-cache-good-pos c-state-min-scan-pos
2590 c-state-brace-pair-desert nil))
2591
2592 ;; point-min has MOVED FORWARD.
2593
2594 ;; Is the new point-min inside a (different) literal?
2595 (unless (and c-state-point-min-lit-start ; at prev. point-min
2596 (< (point-min) (c-state-get-min-scan-pos)))
2597 (c-state-mark-point-min-literal))
2598
2599 ;; Cut off a bit of the tail from `c-state-cache'.
2600 (let ((ptr (cons nil c-state-cache))
2601 pa)
2602 (while (and (setq pa (c-state-cache-top-lparen (cdr ptr)))
2603 (>= pa (point-min)))
2604 (setq ptr (cdr ptr)))
2605
2606 (when (consp ptr)
2607 (if (eq (cdr ptr) c-state-cache)
2608 (setq c-state-cache nil
2609 c-state-cache-good-pos c-state-min-scan-pos)
2610 (setcdr ptr nil)
2611 (setq c-state-cache-good-pos (1+ (c-state-cache-top-lparen))))
2612 )))
2613
2614 (setq c-state-point-min (point-min)))
2615
2616(defun c-append-lower-brace-pair-to-state-cache (from &optional upper-lim)
583e23bd
AM
2617 ;; If there is a brace pair preceding FROM in the buffer, at the same level
2618 ;; of nesting (not necessarily immediately preceding), push a cons onto
2619 ;; `c-state-cache' to represent it. FROM must not be inside a literal. If
2620 ;; UPPER-LIM is non-nil, we append the highest brace pair whose "}" is below
2621 ;; UPPER-LIM.
0ec1d2c5
AM
2622 ;;
2623 ;; Return non-nil when this has been done.
2624 ;;
583e23bd
AM
2625 ;; The situation it copes with is this transformation:
2626 ;;
2627 ;; OLD: { (.) {...........}
2628 ;; ^ ^
2629 ;; FROM HERE
2630 ;;
2631 ;; NEW: { {....} (.) {.........
2632 ;; ^ ^ ^
2633 ;; LOWER BRACE PAIR HERE or HERE
2634 ;;
0ec1d2c5
AM
2635 ;; This routine should be fast. Since it can get called a LOT, we maintain
2636 ;; `c-state-brace-pair-desert', a small cache of "failures", such that we
2637 ;; reduce the time wasted in repeated fruitless searches in brace deserts.
2638 (save-excursion
2639 (save-restriction
2640 (let ((bra from) ce ; Positions of "{" and "}".
2641 new-cons
2642 (cache-pos (c-state-cache-top-lparen)) ; might be nil.
2643 (macro-start-or-from
2644 (progn (goto-char from)
2645 (c-beginning-of-macro)
2646 (point))))
2647 (or upper-lim (setq upper-lim from))
2648
2649 ;; If we're essentially repeating a fruitless search, just give up.
2650 (unless (and c-state-brace-pair-desert
2651 (eq cache-pos (car c-state-brace-pair-desert))
2652 (<= from (cdr c-state-brace-pair-desert)))
583e23bd
AM
2653 ;; DESERT-LIM. Only search what we absolutely need to,
2654 (let ((desert-lim
2655 (and c-state-brace-pair-desert
2656 (eq cache-pos (car c-state-brace-pair-desert))
2657 (cdr c-state-brace-pair-desert)))
2658 ;; CACHE-LIM. This limit will be necessary when an opening
2659 ;; paren at `cache-pos' has just had its matching close paren
2660 ;; inserted. `cache-pos' continues to be a search bound, even
2661 ;; though the algorithm below would skip over the new paren
2662 ;; pair.
2663 (cache-lim (and cache-pos (< cache-pos from) cache-pos)))
2664 (narrow-to-region
2665 (cond
2666 ((and desert-lim cache-lim)
2667 (max desert-lim cache-lim))
2668 (desert-lim)
2669 (cache-lim)
2670 ((point-min)))
2671 (point-max)))
0ec1d2c5
AM
2672
2673 ;; In the next pair of nested loops, the inner one moves back past a
2674 ;; pair of (mis-)matching parens or brackets; the outer one moves
2675 ;; back over a sequence of unmatched close brace/paren/bracket each
2676 ;; time round.
2677 (while
2678 (progn
2679 (c-safe
2680 (while
2681 (and (setq ce (scan-lists bra -1 -1)) ; back past )/]/}; might signal
2682 (setq bra (scan-lists ce -1 1)) ; back past (/[/{; might signal
2683 (or (> ce upper-lim)
2684 (not (eq (char-after bra) ?\{))
2685 (and (goto-char bra)
2686 (c-beginning-of-macro)
2687 (< (point) macro-start-or-from))))))
2688 (and ce (< ce bra)))
2689 (setq bra ce)) ; If we just backed over an unbalanced closing
2690 ; brace, ignore it.
2691
2692 (if (and ce (< bra ce) (eq (char-after bra) ?\{))
2693 ;; We've found the desired brace-pair.
2694 (progn
2695 (setq new-cons (cons bra (1+ ce)))
2696 (cond
2697 ((consp (car c-state-cache))
2698 (setcar c-state-cache new-cons))
2699 ((and (numberp (car c-state-cache)) ; probably never happens
2700 (< ce (car c-state-cache)))
2701 (setcdr c-state-cache
2702 (cons new-cons (cdr c-state-cache))))
2703 (t (setq c-state-cache (cons new-cons c-state-cache)))))
2704
583e23bd 2705 ;; We haven't found a brace pair. Record this in the cache.
0ec1d2c5
AM
2706 (setq c-state-brace-pair-desert (cons cache-pos from))))))))
2707
2708(defsubst c-state-push-any-brace-pair (bra+1 macro-start-or-here)
2709 ;; If BRA+1 is nil, do nothing. Otherwise, BRA+1 is the buffer position
2710 ;; following a {, and that brace has a (mis-)matching } (or ]), and we
2711 ;; "push" "a" brace pair onto `c-state-cache'.
2712 ;;
2713 ;; Here "push" means overwrite the top element if it's itself a brace-pair,
2714 ;; otherwise push it normally.
2715 ;;
2716 ;; The brace pair we push is normally the one surrounding BRA+1, but if the
2717 ;; latter is inside a macro, not being a macro containing
2718 ;; MACRO-START-OR-HERE, we scan backwards through the buffer for a non-macro
2719 ;; base pair. This latter case is assumed to be rare.
2720 ;;
2721 ;; Note: POINT is not preserved in this routine.
2722 (if bra+1
2723 (if (or (> bra+1 macro-start-or-here)
2724 (progn (goto-char bra+1)
2725 (not (c-beginning-of-macro))))
2726 (setq c-state-cache
2727 (cons (cons (1- bra+1)
2728 (scan-lists bra+1 1 1))
2729 (if (consp (car c-state-cache))
2730 (cdr c-state-cache)
2731 c-state-cache)))
2732 ;; N.B. This defsubst codes one method for the simple, normal case,
2733 ;; and a more sophisticated, slower way for the general case. Don't
e1dbe924 2734 ;; eliminate this defsubst - it's a speed optimization.
0ec1d2c5
AM
2735 (c-append-lower-brace-pair-to-state-cache (1- bra+1)))))
2736
2737(defun c-append-to-state-cache (from)
2738 ;; Scan the buffer from FROM to (point-max), adding elements into
2739 ;; `c-state-cache' for braces etc. Return a candidate for
2740 ;; `c-state-cache-good-pos'.
2741 ;;
2742 ;; FROM must be after the latest brace/paren/bracket in `c-state-cache', if
2743 ;; any. Typically, it is immediately after it. It must not be inside a
2744 ;; literal.
2745 (let ((here-bol (c-point 'bol (point-max)))
2746 (macro-start-or-here
2747 (save-excursion (goto-char (point-max))
2748 (if (c-beginning-of-macro)
2749 (point)
2750 (point-max))))
2751 pa+1 ; pos just after an opening PAren (or brace).
2752 (ren+1 from) ; usually a pos just after an closing paREN etc.
2753 ; Is actually the pos. to scan for a (/{/[ from,
2754 ; which sometimes is after a silly )/}/].
2755 paren+1 ; Pos after some opening or closing paren.
2756 paren+1s ; A list of `paren+1's; used to determine a
2757 ; good-pos.
2758 bra+1 ce+1 ; just after L/R bra-ces.
2759 bra+1s ; list of OLD values of bra+1.
2760 mstart) ; start of a macro.
2761
2762 (save-excursion
22bcf204 2763 ;; Each time round the following loop, we enter a successively deeper
0ec1d2c5
AM
2764 ;; level of brace/paren nesting. (Except sometimes we "continue at
2765 ;; the existing level".) `pa+1' is a pos inside an opening
2766 ;; brace/paren/bracket, usually just after it.
2767 (while
2768 (progn
2769 ;; Each time round the next loop moves forward over an opening then
2770 ;; a closing brace/bracket/paren. This loop is white hot, so it
2771 ;; plays ugly tricks to go fast. DON'T PUT ANYTHING INTO THIS
2772 ;; LOOP WHICH ISN'T ABSOLUTELY NECESSARY!!! It terminates when a
2773 ;; call of `scan-lists' signals an error, which happens when there
2774 ;; are no more b/b/p's to scan.
2775 (c-safe
2776 (while t
2777 (setq pa+1 (scan-lists ren+1 1 -1) ; Into (/{/[; might signal
2778 paren+1s (cons pa+1 paren+1s))
2779 (setq ren+1 (scan-lists pa+1 1 1)) ; Out of )/}/]; might signal
2780 (if (and (eq (char-before pa+1) ?{)) ; Check for a macro later.
2781 (setq bra+1 pa+1))
2782 (setcar paren+1s ren+1)))
2783
2784 (if (and pa+1 (> pa+1 ren+1))
2785 ;; We've just entered a deeper nesting level.
2786 (progn
2787 ;; Insert the brace pair (if present) and the single open
2788 ;; paren/brace/bracket into `c-state-cache' It cannot be
2789 ;; inside a macro, except one around point, because of what
2790 ;; `c-neutralize-syntax-in-CPP' has done.
2791 (c-state-push-any-brace-pair bra+1 macro-start-or-here)
2792 ;; Insert the opening brace/bracket/paren position.
2793 (setq c-state-cache (cons (1- pa+1) c-state-cache))
2794 ;; Clear admin stuff for the next more nested part of the scan.
2795 (setq ren+1 pa+1 pa+1 nil bra+1 nil bra+1s nil)
2796 t) ; Carry on the loop
2797
2798 ;; All open p/b/b's at this nesting level, if any, have probably
2799 ;; been closed by matching/mismatching ones. We're probably
2800 ;; finished - we just need to check for having found an
2801 ;; unmatched )/}/], which we ignore. Such a )/}/] can't be in a
2802 ;; macro, due the action of `c-neutralize-syntax-in-CPP'.
2803 (c-safe (setq ren+1 (scan-lists ren+1 1 1)))))) ; acts as loop control.
2804
2805 ;; Record the final, innermost, brace-pair if there is one.
2806 (c-state-push-any-brace-pair bra+1 macro-start-or-here)
2807
2808 ;; Determine a good pos
2809 (while (and (setq paren+1 (car paren+1s))
2810 (> (if (> paren+1 macro-start-or-here)
2811 paren+1
2812 (goto-char paren+1)
2813 (setq mstart (and (c-beginning-of-macro)
2814 (point)))
2815 (or mstart paren+1))
2816 here-bol))
2817 (setq paren+1s (cdr paren+1s)))
2818 (cond
2819 ((and paren+1 mstart)
2820 (min paren+1 mstart))
2821 (paren+1)
2822 (t from)))))
2823
2824(defun c-remove-stale-state-cache (good-pos pps-point)
2825 ;; Remove stale entries from the `c-cache-state', i.e. those which will
2826 ;; not be in it when it is amended for position (point-max).
2827 ;; Additionally, the "outermost" open-brace entry before (point-max)
2828 ;; will be converted to a cons if the matching close-brace is scanned.
2829 ;;
2830 ;; GOOD-POS is a "maximal" "safe position" - there must be no open
2831 ;; parens/braces/brackets between GOOD-POS and (point-max).
2832 ;;
2833 ;; As a second thing, calculate the result of parse-partial-sexp at
2834 ;; PPS-POINT, w.r.t. GOOD-POS. The motivation here is that
2835 ;; `c-state-cache-good-pos' may become PPS-POINT, but the caller may need to
2836 ;; adjust it to get outside a string/comment. (Sorry about this! The code
2837 ;; needs to be FAST).
2838 ;;
2839 ;; Return a list (GOOD-POS SCAN-BACK-POS PPS-STATE), where
2840 ;; o - GOOD-POS is a position where the new value `c-state-cache' is known
2841 ;; to be good (we aim for this to be as high as possible);
2842 ;; o - SCAN-BACK-POS, if not nil, indicates there may be a brace pair
2843 ;; preceding POS which needs to be recorded in `c-state-cache'. It is a
2844 ;; position to scan backwards from.
2845 ;; o - PPS-STATE is the parse-partial-sexp state at PPS-POINT.
2846 (save-restriction
2847 (narrow-to-region 1 (point-max))
2848 (save-excursion
eb2f6eeb 2849 (let* ((in-macro-start ; start of macro containing (point-max) or nil.
0ec1d2c5
AM
2850 (save-excursion
2851 (goto-char (point-max))
2852 (and (c-beginning-of-macro)
2853 (point))))
2854 (good-pos-actual-macro-start ; Start of macro containing good-pos
2855 ; or nil
2856 (and (< good-pos (point-max))
2857 (save-excursion
2858 (goto-char good-pos)
2859 (and (c-beginning-of-macro)
2860 (point)))))
2861 (good-pos-actual-macro-end ; End of this macro, (maybe
2862 ; (point-max)), or nil.
2863 (and good-pos-actual-macro-start
2864 (save-excursion
2865 (goto-char good-pos-actual-macro-start)
2866 (c-end-of-macro)
2867 (point))))
2868 pps-state ; Will be 9 or 10 elements long.
2869 pos
2870 upper-lim ; ,beyond which `c-state-cache' entries are removed
2871 scan-back-pos
2872 pair-beg pps-point-state target-depth)
2873
2874 ;; Remove entries beyond (point-max). Also remove any entries inside
2875 ;; a macro, unless (point-max) is in the same macro.
2876 (setq upper-lim
2877 (if (or (null c-state-old-cpp-beg)
2878 (and (> (point-max) c-state-old-cpp-beg)
2879 (< (point-max) c-state-old-cpp-end)))
2880 (point-max)
2881 (min (point-max) c-state-old-cpp-beg)))
657071fc 2882 (while (and c-state-cache (>= (c-state-cache-top-lparen) upper-lim))
0ec1d2c5
AM
2883 (setq c-state-cache (cdr c-state-cache)))
2884 ;; If `upper-lim' is inside the last recorded brace pair, remove its
2885 ;; RBrace and indicate we'll need to search backwards for a previous
2886 ;; brace pair.
2887 (when (and c-state-cache
2888 (consp (car c-state-cache))
2889 (> (cdar c-state-cache) upper-lim))
2890 (setcar c-state-cache (caar c-state-cache))
2891 (setq scan-back-pos (car c-state-cache)))
2892
2893 ;; The next loop jumps forward out of a nested level of parens each
2894 ;; time round; the corresponding elements in `c-state-cache' are
2895 ;; removed. `pos' is just after the brace-pair or the open paren at
2896 ;; (car c-state-cache). There can be no open parens/braces/brackets
2897 ;; between `good-pos'/`good-pos-actual-macro-start' and (point-max),
2898 ;; due to the interface spec to this function.
dd21b621 2899 (setq pos (if (and good-pos-actual-macro-end
eb2f6eeb
AM
2900 (not (eq good-pos-actual-macro-start
2901 in-macro-start)))
0ec1d2c5
AM
2902 (1+ good-pos-actual-macro-end) ; get outside the macro as
2903 ; marked by a `category' text property.
2904 good-pos))
2905 (goto-char pos)
2906 (while (and c-state-cache
2907 (< (point) (point-max)))
2908 (cond
2909 ((null pps-state) ; first time through
2910 (setq target-depth -1))
2911 ((eq (car pps-state) target-depth) ; found closing ),},]
2912 (setq target-depth (1- (car pps-state))))
2913 ;; Do nothing when we've merely reached pps-point.
2914 )
2915
2916 ;; Scan!
2917 (setq pps-state
2918 (parse-partial-sexp
2919 (point) (if (< (point) pps-point) pps-point (point-max))
2920 target-depth
2921 nil pps-state))
2922
2923 (if (= (point) pps-point)
2924 (setq pps-point-state pps-state))
2925
2926 (when (eq (car pps-state) target-depth)
2927 (setq pos (point)) ; POS is now just after an R-paren/brace.
2928 (cond
2929 ((and (consp (car c-state-cache))
2930 (eq (point) (cdar c-state-cache)))
2931 ;; We've just moved out of the paren pair containing the brace-pair
2932 ;; at (car c-state-cache). `pair-beg' is where the open paren is,
2933 ;; and is potentially where the open brace of a cons in
2934 ;; c-state-cache will be.
2935 (setq pair-beg (car-safe (cdr c-state-cache))
2936 c-state-cache (cdr-safe (cdr c-state-cache)))) ; remove {}pair + containing Lparen.
2937 ((numberp (car c-state-cache))
2938 (setq pair-beg (car c-state-cache)
2939 c-state-cache (cdr c-state-cache))) ; remove this
2940 ; containing Lparen
2941 ((numberp (cadr c-state-cache))
2942 (setq pair-beg (cadr c-state-cache)
2943 c-state-cache (cddr c-state-cache))) ; Remove a paren pair
2944 ; together with enclosed brace pair.
2945 ;; (t nil) ; Ignore an unmated Rparen.
2946 )))
2947
2948 (if (< (point) pps-point)
2949 (setq pps-state (parse-partial-sexp (point) pps-point
2950 nil nil ; TARGETDEPTH, STOPBEFORE
2951 pps-state)))
2952
2953 ;; If the last paren pair we moved out of was actually a brace pair,
2954 ;; insert it into `c-state-cache'.
2955 (when (and pair-beg (eq (char-after pair-beg) ?{))
2956 (if (consp (car-safe c-state-cache))
2957 (setq c-state-cache (cdr c-state-cache)))
2958 (setq c-state-cache (cons (cons pair-beg pos)
2959 c-state-cache)))
2960
2961 (list pos scan-back-pos pps-state)))))
2962
2963(defun c-remove-stale-state-cache-backwards (here cache-pos)
2964 ;; Strip stale elements of `c-state-cache' by moving backwards through the
2965 ;; buffer, and inform the caller of the scenario detected.
2966 ;;
2967 ;; HERE is the position we're setting `c-state-cache' for.
2968 ;; CACHE-POS is just after the latest recorded position in `c-state-cache'
2969 ;; before HERE, or a position at or near point-min which isn't in a
2970 ;; literal.
2971 ;;
2972 ;; This function must only be called only when (> `c-state-cache-good-pos'
2973 ;; HERE). Usually the gap between CACHE-POS and HERE is large. It is thus
e1dbe924 2974 ;; optimized to eliminate (or minimize) scanning between these two
0ec1d2c5
AM
2975 ;; positions.
2976 ;;
2977 ;; Return a three element list (GOOD-POS SCAN-BACK-POS FWD-FLAG), where:
2978 ;; o - GOOD-POS is a "good position", where `c-state-cache' is valid, or
2979 ;; could become so after missing elements are inserted into
2980 ;; `c-state-cache'. This is JUST AFTER an opening or closing
2981 ;; brace/paren/bracket which is already in `c-state-cache' or just before
2982 ;; one otherwise. exceptionally (when there's no such b/p/b handy) the BOL
2983 ;; before `here''s line, or the start of the literal containing it.
2984 ;; o - SCAN-BACK-POS, if non-nil, indicates there may be a brace pair
2985 ;; preceding POS which isn't recorded in `c-state-cache'. It is a position
2986 ;; to scan backwards from.
2987 ;; o - FWD-FLAG, if non-nil, indicates there may be parens/braces between
2988 ;; POS and HERE which aren't recorded in `c-state-cache'.
2989 ;;
2990 ;; The comments in this defun use "paren" to mean parenthesis or square
2991 ;; bracket (as contrasted with a brace), and "(" and ")" likewise.
2992 ;;
2993 ;; . {..} (..) (..) ( .. { } ) (...) ( .... . ..)
2994 ;; | | | | | |
2995 ;; CP E here D C good
2996 (let ((pos c-state-cache-good-pos)
2997 pa ren ; positions of "(" and ")"
2998 dropped-cons ; whether the last element dropped from `c-state-cache'
2999 ; was a cons (representing a brace-pair)
3000 good-pos ; see above.
3001 lit ; (START . END) of a literal containing some point.
3002 here-lit-start here-lit-end ; bounds of literal containing `here'
3003 ; or `here' itself.
c0209c2c 3004 here- here+ ; start/end of macro around HERE, or HERE
0ec1d2c5 3005 (here-bol (c-point 'bol here))
c400c4d7 3006 (too-far-back (max (- here c-state-cache-too-far) (point-min))))
0ec1d2c5
AM
3007
3008 ;; Remove completely irrelevant entries from `c-state-cache'.
3009 (while (and c-state-cache
3010 (>= (setq pa (c-state-cache-top-lparen)) here))
3011 (setq dropped-cons (consp (car c-state-cache)))
3012 (setq c-state-cache (cdr c-state-cache))
3013 (setq pos pa))
3014 ;; At this stage, (> pos here);
3015 ;; (< (c-state-cache-top-lparen) here) (or is nil).
3016
c0209c2c
AM
3017 (cond
3018 ((and (consp (car c-state-cache))
3019 (> (cdar c-state-cache) here))
3020 ;; CASE 1: The top of the cache is a brace pair which now encloses
3021 ;; `here'. As good-pos, return the address. of the "{". Since we've no
3022 ;; knowledge of what's inside these braces, we have no alternative but
3023 ;; to direct the caller to scan the buffer from the opening brace.
3024 (setq pos (caar c-state-cache))
3025 (setcar c-state-cache pos)
3026 (list (1+ pos) pos t)) ; return value. We've just converted a brace pair
3027 ; entry into a { entry, so the caller needs to
3028 ; search for a brace pair before the {.
3029
3030 ;; `here' might be inside a literal. Check for this.
3031 ((progn
3032 (setq lit (c-state-literal-at here)
3033 here-lit-start (or (car lit) here)
3034 here-lit-end (or (cdr lit) here))
3035 ;; Has `here' just "newly entered" a macro?
3036 (save-excursion
3037 (goto-char here-lit-start)
3038 (if (and (c-beginning-of-macro)
3039 (or (null c-state-old-cpp-beg)
3040 (not (= (point) c-state-old-cpp-beg))))
3041 (progn
3042 (setq here- (point))
3043 (c-end-of-macro)
3044 (setq here+ (point)))
3045 (setq here- here-lit-start
3046 here+ here-lit-end)))
3047
3048 ;; `here' might be nested inside any depth of parens (or brackets but
3049 ;; not braces). Scan backwards to find the outermost such opening
3050 ;; paren, if there is one. This will be the scan position to return.
3051 (save-restriction
3052 (narrow-to-region cache-pos (point-max))
3053 (setq pos (c-state-balance-parens-backwards here- here+ pos)))
3054 nil)) ; for the cond
3055
3056 ((< pos here-lit-start)
3057 ;; CASE 2: Address of outermost ( or [ which now encloses `here', but
3058 ;; didn't enclose the (previous) `c-state-cache-good-pos'. If there is
3059 ;; a brace pair preceding this, it will already be in `c-state-cache',
3060 ;; unless there was a brace pair after it, i.e. there'll only be one to
3061 ;; scan for if we've just deleted one.
3062 (list pos (and dropped-cons pos) t)) ; Return value.
3063
3064 ;; `here' isn't enclosed in a (previously unrecorded) bracket/paren.
3065 ;; Further forward scanning isn't needed, but we still need to find a
3066 ;; GOOD-POS. Step out of all enclosing "("s on HERE's line.
3067 ((progn
0ec1d2c5
AM
3068 (save-restriction
3069 (narrow-to-region here-bol (point-max))
3070 (setq pos here-lit-start)
3071 (c-safe (while (setq pa (scan-lists pos -1 1))
3072 (setq pos pa)))) ; might signal
c0209c2c
AM
3073 nil)) ; for the cond
3074
3075 ((setq ren (c-safe-scan-lists pos -1 -1 too-far-back))
3076 ;; CASE 3: After a }/)/] before `here''s BOL.
3077 (list (1+ ren) (and dropped-cons pos) nil)) ; Return value
3078
3079 (t
3080 ;; CASE 4; Best of a bad job: BOL before `here-bol', or beginning of
3081 ;; literal containing it.
3082 (setq good-pos (c-state-lit-beg (c-point 'bopl here-bol)))
3083 (list good-pos (and dropped-cons good-pos) nil)))))
0ec1d2c5
AM
3084
3085
3086;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
3087;; Externally visible routines.
3088
3089(defun c-state-cache-init ()
3090 (setq c-state-cache nil
3091 c-state-cache-good-pos 1
3092 c-state-nonlit-pos-cache nil
3093 c-state-nonlit-pos-cache-limit 1
3094 c-state-brace-pair-desert nil
3095 c-state-point-min 1
3096 c-state-point-min-lit-type nil
3097 c-state-point-min-lit-start nil
3098 c-state-min-scan-pos 1
3099 c-state-old-cpp-beg nil
3100 c-state-old-cpp-end nil)
3101 (c-state-mark-point-min-literal))
3102
e84efb70
AM
3103;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
3104;; Debugging routines to dump `c-state-cache' in a "replayable" form.
3105;; (defmacro c-sc-de (elt) ; "c-state-cache-dump-element"
3106;; `(format ,(concat "(setq " (symbol-name elt) " %s) ") ,elt))
3107;; (defmacro c-sc-qde (elt) ; "c-state-cache-quote-dump-element"
3108;; `(format ,(concat "(setq " (symbol-name elt) " '%s) ") ,elt))
3109;; (defun c-state-dump ()
3110;; ;; For debugging.
3111;; ;(message
3112;; (concat
3113;; (c-sc-qde c-state-cache)
3114;; (c-sc-de c-state-cache-good-pos)
3115;; (c-sc-qde c-state-nonlit-pos-cache)
3116;; (c-sc-de c-state-nonlit-pos-cache-limit)
3117;; (c-sc-qde c-state-brace-pair-desert)
3118;; (c-sc-de c-state-point-min)
3119;; (c-sc-de c-state-point-min-lit-type)
3120;; (c-sc-de c-state-point-min-lit-start)
3121;; (c-sc-de c-state-min-scan-pos)
3122;; (c-sc-de c-state-old-cpp-beg)
3123;; (c-sc-de c-state-old-cpp-end)))
3124;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
3125
0ec1d2c5
AM
3126(defun c-invalidate-state-cache-1 (here)
3127 ;; Invalidate all info on `c-state-cache' that applies to the buffer at HERE
3128 ;; or higher and set `c-state-cache-good-pos' accordingly. The cache is
3129 ;; left in a consistent state.
3130 ;;
3131 ;; This is much like `c-whack-state-after', but it never changes a paren
3132 ;; pair element into an open paren element. Doing that would mean that the
3133 ;; new open paren wouldn't have the required preceding paren pair element.
785eecbb 3134 ;;
0ec1d2c5
AM
3135 ;; This function is called from c-after-change.
3136
56d093a9 3137 ;; The caches of non-literals:
0ec1d2c5
AM
3138 (if (< here c-state-nonlit-pos-cache-limit)
3139 (setq c-state-nonlit-pos-cache-limit here))
56d093a9
AM
3140 (if (< here c-state-semi-nonlit-pos-cache-limit)
3141 (setq c-state-semi-nonlit-pos-cache-limit here))
0ec1d2c5
AM
3142
3143 ;; `c-state-cache':
3144 ;; Case 1: if `here' is in a literal containing point-min, everything
3145 ;; becomes (or is already) nil.
3146 (if (or (null c-state-cache-good-pos)
3147 (< here (c-state-get-min-scan-pos)))
3148 (setq c-state-cache nil
3149 c-state-cache-good-pos nil
3150 c-state-min-scan-pos nil)
3151
c400c4d7
SM
3152 ;; Truncate `c-state-cache' and set `c-state-cache-good-pos' to a value
3153 ;; below `here'. To maintain its consistency, we may need to insert a new
3154 ;; brace pair.
0ec1d2c5
AM
3155 (let ((here-bol (c-point 'bol here))
3156 too-high-pa ; recorded {/(/[ next above here, or nil.
3157 dropped-cons ; was the last removed element a brace pair?
3158 pa)
3159 ;; The easy bit - knock over-the-top bits off `c-state-cache'.
3160 (while (and c-state-cache
3161 (>= (setq pa (c-state-cache-top-paren)) here))
3162 (setq dropped-cons (consp (car c-state-cache))
3163 too-high-pa (c-state-cache-top-lparen)
3164 c-state-cache (cdr c-state-cache)))
3165
3166 ;; Do we need to add in an earlier brace pair, having lopped one off?
3167 (if (and dropped-cons
3168 (< too-high-pa (+ here c-state-cache-too-far)))
3169 (c-append-lower-brace-pair-to-state-cache too-high-pa here-bol))
3170 (setq c-state-cache-good-pos (or (c-state-cache-after-top-paren)
3171 (c-state-get-min-scan-pos)))))
3172
3173 ;; The brace-pair desert marker:
3174 (when (car c-state-brace-pair-desert)
3175 (if (< here (car c-state-brace-pair-desert))
3176 (setq c-state-brace-pair-desert nil)
3177 (if (< here (cdr c-state-brace-pair-desert))
3178 (setcdr c-state-brace-pair-desert here)))))
3179
3180(defun c-parse-state-1 ()
3181 ;; Find and record all noteworthy parens between some good point earlier in
3182 ;; the file and point. That good point is at least the beginning of the
3183 ;; top-level construct we are in, or the beginning of the preceding
3184 ;; top-level construct if we aren't in one.
3185 ;;
3186 ;; The returned value is a list of the noteworthy parens with the last one
3187 ;; first. If an element in the list is an integer, it's the position of an
3188 ;; open paren (of any type) which has not been closed before the point. If
3189 ;; an element is a cons, it gives the position of a closed BRACE paren
3190 ;; pair[*]; the car is the start brace position and the cdr is the position
3191 ;; following the closing brace. Only the last closed brace paren pair
3192 ;; before each open paren and before the point is recorded, and thus the
3193 ;; state never contains two cons elements in succession. When a close brace
3194 ;; has no matching open brace (e.g., the matching brace is outside the
3195 ;; visible region), it is not represented in the returned value.
3196 ;;
3197 ;; [*] N.B. The close "brace" might be a mismatching close bracket or paren.
3198 ;; This defun explicitly treats mismatching parens/braces/brackets as
3199 ;; matching. It is the open brace which makes it a "brace" pair.
3200 ;;
3201 ;; If POINT is within a macro, open parens and brace pairs within
3202 ;; THIS macro MIGHT be recorded. This depends on whether their
3203 ;; syntactic properties have been suppressed by
3204 ;; `c-neutralize-syntax-in-CPP'. This might need fixing (2008-12-11).
d9e94c22
MS
3205 ;;
3206 ;; Currently no characters which are given paren syntax with the
3207 ;; syntax-table property are recorded, i.e. angle bracket arglist
3208 ;; parens are never present here. Note that this might change.
3209 ;;
0386b551 3210 ;; BUG: This function doesn't cope entirely well with unbalanced
0ec1d2c5
AM
3211 ;; parens in macros. (2008-12-11: this has probably been resolved
3212 ;; by the function `c-neutralize-syntax-in-CPP'.) E.g. in the
3213 ;; following case the brace before the macro isn't balanced with the
3214 ;; one after it:
0386b551
AM
3215 ;;
3216 ;; {
3217 ;; #define X {
3218 ;; }
3219 ;;
0ec1d2c5
AM
3220 ;; Note to maintainers: this function DOES get called with point
3221 ;; within comments and strings, so don't assume it doesn't!
3222 ;;
0386b551 3223 ;; This function might do hidden buffer changes.
0ec1d2c5
AM
3224 (let* ((here (point))
3225 (here-bopl (c-point 'bopl))
3226 strategy ; 'forward, 'backward etc..
3227 ;; Candidate positions to start scanning from:
3228 cache-pos ; highest position below HERE already existing in
3229 ; cache (or 1).
3230 good-pos
3231 start-point
3232 bopl-state
3233 res
3234 scan-backward-pos scan-forward-p) ; used for 'backward.
3235 ;; If POINT-MIN has changed, adjust the cache
3236 (unless (= (point-min) c-state-point-min)
3237 (c-renarrow-state-cache))
3238
3239 ;; Strategy?
3240 (setq res (c-parse-state-get-strategy here c-state-cache-good-pos)
3241 strategy (car res)
3242 cache-pos (cadr res)
3243 start-point (nth 2 res))
3244
3245 (when (eq strategy 'BOD)
3246 (setq c-state-cache nil
3247 c-state-cache-good-pos start-point))
3248
3249 ;; SCAN!
3250 (save-restriction
3251 (cond
3252 ((memq strategy '(forward BOD))
0386b551 3253 (narrow-to-region (point-min) here)
0ec1d2c5
AM
3254 (setq res (c-remove-stale-state-cache start-point here-bopl))
3255 (setq cache-pos (car res)
3256 scan-backward-pos (cadr res)
3257 bopl-state (car (cddr res))) ; will be nil if (< here-bopl
3258 ; start-point)
3259 (if scan-backward-pos
3260 (c-append-lower-brace-pair-to-state-cache scan-backward-pos))
3261 (setq good-pos
3262 (c-append-to-state-cache cache-pos))
3263 (setq c-state-cache-good-pos
3264 (if (and bopl-state
3265 (< good-pos (- here c-state-cache-too-far)))
3266 (c-state-cache-non-literal-place here-bopl bopl-state)
3267 good-pos)))
3268
3269 ((eq strategy 'backward)
3270 (setq res (c-remove-stale-state-cache-backwards here cache-pos)
3271 good-pos (car res)
3272 scan-backward-pos (cadr res)
3273 scan-forward-p (car (cddr res)))
3274 (if scan-backward-pos
3275 (c-append-lower-brace-pair-to-state-cache
3276 scan-backward-pos))
3277 (setq c-state-cache-good-pos
3278 (if scan-forward-p
3279 (progn (narrow-to-region (point-min) here)
3280 (c-append-to-state-cache good-pos))
2e492df3 3281 good-pos)))
0ec1d2c5
AM
3282
3283 (t ; (eq strategy 'IN-LIT)
3284 (setq c-state-cache nil
3285 c-state-cache-good-pos nil)))))
3286
3287 c-state-cache)
3288
3289(defun c-invalidate-state-cache (here)
3290 ;; This is a wrapper over `c-invalidate-state-cache-1'.
3291 ;;
3292 ;; It suppresses the syntactic effect of the < and > (template) brackets and
3293 ;; of all parens in preprocessor constructs, except for any such construct
3294 ;; containing point. We can then call `c-invalidate-state-cache-1' without
3295 ;; worrying further about macros and template delimiters.
3296 (c-with-<->-as-parens-suppressed
3e8f7d91
AM
3297 (if (and c-state-old-cpp-beg
3298 (< c-state-old-cpp-beg here))
0ec1d2c5 3299 (c-with-all-but-one-cpps-commented-out
3e8f7d91
AM
3300 c-state-old-cpp-beg
3301 (min c-state-old-cpp-end here)
0ec1d2c5
AM
3302 (c-invalidate-state-cache-1 here))
3303 (c-with-cpps-commented-out
3304 (c-invalidate-state-cache-1 here)))))
3305
3306(defun c-parse-state ()
3307 ;; This is a wrapper over `c-parse-state-1'. See that function for a
3308 ;; description of the functionality and return value.
3309 ;;
3310 ;; It suppresses the syntactic effect of the < and > (template) brackets and
3311 ;; of all parens in preprocessor constructs, except for any such construct
3312 ;; containing point. We can then call `c-parse-state-1' without worrying
3313 ;; further about macros and template delimiters.
3314 (let (here-cpp-beg here-cpp-end)
3315 (save-excursion
3316 (when (c-beginning-of-macro)
3317 (setq here-cpp-beg (point))
3318 (unless
3319 (> (setq here-cpp-end (c-syntactic-end-of-macro))
3320 here-cpp-beg)
3321 (setq here-cpp-beg nil here-cpp-end nil))))
3322 ;; FIXME!!! Put in a `condition-case' here to protect the integrity of the
3323 ;; subsystem.
3324 (prog1
3325 (c-with-<->-as-parens-suppressed
3326 (if (and here-cpp-beg (> here-cpp-end here-cpp-beg))
3327 (c-with-all-but-one-cpps-commented-out
3328 here-cpp-beg here-cpp-end
3329 (c-parse-state-1))
3330 (c-with-cpps-commented-out
3331 (c-parse-state-1))))
3e8f7d91
AM
3332 (setq c-state-old-cpp-beg (and here-cpp-beg (copy-marker here-cpp-beg t))
3333 c-state-old-cpp-end (and here-cpp-end (copy-marker here-cpp-end t)))
3334 )))
0ec1d2c5
AM
3335
3336;; Debug tool to catch cache inconsistencies. This is called from
3337;; 000tests.el.
a66cd3ee
MS
3338(defvar c-debug-parse-state nil)
3339(unless (fboundp 'c-real-parse-state)
3340 (fset 'c-real-parse-state (symbol-function 'c-parse-state)))
3341(cc-bytecomp-defun c-real-parse-state)
cd5495ff
AM
3342
3343(defvar c-parse-state-state nil)
3344(defun c-record-parse-state-state ()
3345 (setq c-parse-state-state
3346 (mapcar
3347 (lambda (arg)
3348 (cons arg (symbol-value arg)))
3349 '(c-state-cache
3350 c-state-cache-good-pos
3351 c-state-nonlit-pos-cache
3352 c-state-nonlit-pos-cache-limit
3353 c-state-brace-pair-desert
3354 c-state-point-min
3355 c-state-point-min-lit-type
3356 c-state-point-min-lit-start
3357 c-state-min-scan-pos
3358 c-state-old-cpp-beg
3359 c-state-old-cpp-end))))
3360(defun c-replay-parse-state-state ()
3361 (message
3362 (concat "(setq "
3363 (mapconcat
3364 (lambda (arg)
3365 (format "%s %s%s" (car arg) (if (atom (cdr arg)) "" "'") (cdr arg)))
3366 c-parse-state-state " ")
3367 ")")))
3368
a66cd3ee 3369(defun c-debug-parse-state ()
0ec1d2c5 3370 (let ((here (point)) (res1 (c-real-parse-state)) res2)
0386b551 3371 (let ((c-state-cache nil)
0ec1d2c5
AM
3372 (c-state-cache-good-pos 1)
3373 (c-state-nonlit-pos-cache nil)
3374 (c-state-nonlit-pos-cache-limit 1)
3375 (c-state-brace-pair-desert nil)
3376 (c-state-point-min 1)
3377 (c-state-point-min-lit-type nil)
3378 (c-state-point-min-lit-start nil)
3379 (c-state-min-scan-pos 1)
3380 (c-state-old-cpp-beg nil)
3381 (c-state-old-cpp-end nil))
a66cd3ee
MS
3382 (setq res2 (c-real-parse-state)))
3383 (unless (equal res1 res2)
0386b551
AM
3384 ;; The cache can actually go further back due to the ad-hoc way
3385 ;; the first paren is found, so try to whack off a bit of its
3386 ;; start before complaining.
cd5495ff
AM
3387 ;; (save-excursion
3388 ;; (goto-char (or (c-least-enclosing-brace res2) (point)))
3389 ;; (c-beginning-of-defun-1)
3390 ;; (while (not (or (bobp) (eq (char-after) ?{)))
3391 ;; (c-beginning-of-defun-1))
3392 ;; (unless (equal (c-whack-state-before (point) res1) res2)
3393 ;; (message (concat "c-parse-state inconsistency at %s: "
3394 ;; "using cache: %s, from scratch: %s")
3395 ;; here res1 res2)))
3396 (message (concat "c-parse-state inconsistency at %s: "
3397 "using cache: %s, from scratch: %s")
3398 here res1 res2)
3399 (message "Old state:")
3400 (c-replay-parse-state-state))
3401 (c-record-parse-state-state)
a66cd3ee 3402 res1))
0ec1d2c5 3403
a66cd3ee
MS
3404(defun c-toggle-parse-state-debug (&optional arg)
3405 (interactive "P")
3406 (setq c-debug-parse-state (c-calculate-state arg c-debug-parse-state))
3407 (fset 'c-parse-state (symbol-function (if c-debug-parse-state
3408 'c-debug-parse-state
3409 'c-real-parse-state)))
3410 (c-keep-region-active))
0386b551
AM
3411(when c-debug-parse-state
3412 (c-toggle-parse-state-debug 1))
a66cd3ee 3413
0ec1d2c5 3414\f
d9e94c22
MS
3415(defun c-whack-state-before (bufpos paren-state)
3416 ;; Whack off any state information from PAREN-STATE which lies
3417 ;; before BUFPOS. Not destructive on PAREN-STATE.
d9e94c22
MS
3418 (let* ((newstate (list nil))
3419 (ptr newstate)
3420 car)
3421 (while paren-state
3422 (setq car (car paren-state)
3423 paren-state (cdr paren-state))
3424 (if (< (if (consp car) (car car) car) bufpos)
3425 (setq paren-state nil)
3426 (setcdr ptr (list car))
3427 (setq ptr (cdr ptr))))
3428 (cdr newstate)))
3429
3430(defun c-whack-state-after (bufpos paren-state)
3431 ;; Whack off any state information from PAREN-STATE which lies at or
3432 ;; after BUFPOS. Not destructive on PAREN-STATE.
d9e94c22
MS
3433 (catch 'done
3434 (while paren-state
3435 (let ((car (car paren-state)))
3436 (if (consp car)
3437 ;; just check the car, because in a balanced brace
3438 ;; expression, it must be impossible for the corresponding
3439 ;; close brace to be before point, but the open brace to
3440 ;; be after.
3441 (if (<= bufpos (car car))
3442 nil ; whack it off
3443 (if (< bufpos (cdr car))
3444 ;; its possible that the open brace is before
3445 ;; bufpos, but the close brace is after. In that
3446 ;; case, convert this to a non-cons element. The
3447 ;; rest of the state is before bufpos, so we're
3448 ;; done.
3449 (throw 'done (cons (car car) (cdr paren-state)))
3450 ;; we know that both the open and close braces are
3451 ;; before bufpos, so we also know that everything else
3452 ;; on state is before bufpos.
3453 (throw 'done paren-state)))
3454 (if (<= bufpos car)
3455 nil ; whack it off
3456 ;; it's before bufpos, so everything else should too.
3457 (throw 'done paren-state)))
3458 (setq paren-state (cdr paren-state)))
3459 nil)))
3460
3461(defun c-most-enclosing-brace (paren-state &optional bufpos)
3462 ;; Return the bufpos of the innermost enclosing open paren before
0386b551 3463 ;; bufpos, or nil if none was found.
d9e94c22
MS
3464 (let (enclosingp)
3465 (or bufpos (setq bufpos 134217727))
3466 (while paren-state
3467 (setq enclosingp (car paren-state)
3468 paren-state (cdr paren-state))
3469 (if (or (consp enclosingp)
3470 (>= enclosingp bufpos))
3471 (setq enclosingp nil)
d9e94c22
MS
3472 (setq paren-state nil)))
3473 enclosingp))
3474
0386b551
AM
3475(defun c-least-enclosing-brace (paren-state)
3476 ;; Return the bufpos of the outermost enclosing open paren, or nil
3477 ;; if none was found.
d9e94c22 3478 (let (pos elem)
d9e94c22
MS
3479 (while paren-state
3480 (setq elem (car paren-state)
3481 paren-state (cdr paren-state))
0386b551
AM
3482 (if (integerp elem)
3483 (setq pos elem)))
d9e94c22
MS
3484 pos))
3485
3486(defun c-safe-position (bufpos paren-state)
0386b551
AM
3487 ;; Return the closest "safe" position recorded on PAREN-STATE that
3488 ;; is higher up than BUFPOS. Return nil if PAREN-STATE doesn't
3489 ;; contain any. Return nil if BUFPOS is nil, which is useful to
3490 ;; find the closest limit before a given limit that might be nil.
d9e94c22 3491 ;;
0386b551
AM
3492 ;; A "safe" position is a position at or after a recorded open
3493 ;; paren, or after a recorded close paren. The returned position is
3494 ;; thus either the first position after a close brace, or the first
3495 ;; position after an enclosing paren, or at the enclosing paren in
3496 ;; case BUFPOS is immediately after it.
d9e94c22
MS
3497 (when bufpos
3498 (let (elem)
3499 (catch 'done
3500 (while paren-state
3501 (setq elem (car paren-state))
3502 (if (consp elem)
3503 (cond ((< (cdr elem) bufpos)
3504 (throw 'done (cdr elem)))
3505 ((< (car elem) bufpos)
3506 ;; See below.
3507 (throw 'done (min (1+ (car elem)) bufpos))))
3508 (if (< elem bufpos)
3509 ;; elem is the position at and not after the opening paren, so
3510 ;; we can go forward one more step unless it's equal to
3511 ;; bufpos. This is useful in some cases avoid an extra paren
3512 ;; level between the safe position and bufpos.
3513 (throw 'done (min (1+ elem) bufpos))))
3514 (setq paren-state (cdr paren-state)))))))
3515
3516(defun c-beginning-of-syntax ()
3517 ;; This is used for `font-lock-beginning-of-syntax-function'. It
3518 ;; goes to the closest previous point that is known to be outside
3519 ;; any string literal or comment. `c-state-cache' is used if it has
3520 ;; a position in the vicinity.
3521 (let* ((paren-state c-state-cache)
3522 elem
3523
3524 (pos (catch 'done
3525 ;; Note: Similar code in `c-safe-position'. The
3526 ;; difference is that we accept a safe position at
3527 ;; the point and don't bother to go forward past open
3528 ;; parens.
3529 (while paren-state
3530 (setq elem (car paren-state))
3531 (if (consp elem)
3532 (cond ((<= (cdr elem) (point))
3533 (throw 'done (cdr elem)))
3534 ((<= (car elem) (point))
3535 (throw 'done (car elem))))
3536 (if (<= elem (point))
3537 (throw 'done elem)))
3538 (setq paren-state (cdr paren-state)))
3539 (point-min))))
3540
3541 (if (> pos (- (point) 4000))
3542 (goto-char pos)
3543 ;; The position is far back. Try `c-beginning-of-defun-1'
3544 ;; (although we can't be entirely sure it will go to a position
3545 ;; outside a comment or string in current emacsen). FIXME:
3546 ;; Consult `syntax-ppss' here.
3547 (c-beginning-of-defun-1)
3548 (if (< (point) pos)
3549 (goto-char pos)))))
3550
3551\f
3552;; Tools for scanning identifiers and other tokens.
3553
3554(defun c-on-identifier ()
3555 "Return non-nil if the point is on or directly after an identifier.
3556Keywords are recognized and not considered identifiers. If an
3557identifier is detected, the returned value is its starting position.
0386b551
AM
3558If an identifier ends at the point and another begins at it \(can only
3559happen in Pike) then the point for the preceding one is returned.
d9e94c22 3560
0386b551
AM
3561Note that this function might do hidden buffer changes. See the
3562comment at the start of cc-engine.el for more info."
3563
3564 ;; FIXME: Shouldn't this function handle "operator" in C++?
d9e94c22
MS
3565
3566 (save-excursion
0386b551
AM
3567 (skip-syntax-backward "w_")
3568
3569 (or
3570
3571 ;; Check for a normal (non-keyword) identifier.
3572 (and (looking-at c-symbol-start)
3573 (not (looking-at c-keywords-regexp))
3574 (point))
3575
3576 (when (c-major-mode-is 'pike-mode)
3577 ;; Handle the `<operator> syntax in Pike.
3578 (let ((pos (point)))
3579 (skip-chars-backward "-!%&*+/<=>^|~[]()")
3580 (and (if (< (skip-chars-backward "`") 0)
3581 t
3582 (goto-char pos)
3583 (eq (char-after) ?\`))
3584 (looking-at c-symbol-key)
3585 (>= (match-end 0) pos)
3586 (point))))
3587
3588 ;; Handle the "operator +" syntax in C++.
3589 (when (and c-overloadable-operators-regexp
3590 (= (c-backward-token-2 0) 0))
3591
3592 (cond ((and (looking-at c-overloadable-operators-regexp)
51c9af45 3593 (or (not c-opt-op-identifier-prefix)
0386b551 3594 (and (= (c-backward-token-2 1) 0)
51c9af45 3595 (looking-at c-opt-op-identifier-prefix))))
0386b551
AM
3596 (point))
3597
3598 ((save-excursion
51c9af45
AM
3599 (and c-opt-op-identifier-prefix
3600 (looking-at c-opt-op-identifier-prefix)
0386b551
AM
3601 (= (c-forward-token-2 1) 0)
3602 (looking-at c-overloadable-operators-regexp)))
3603 (point))))
3604
3605 )))
d9e94c22
MS
3606
3607(defsubst c-simple-skip-symbol-backward ()
3608 ;; If the point is at the end of a symbol then skip backward to the
3609 ;; beginning of it. Don't move otherwise. Return non-nil if point
3610 ;; moved.
0386b551
AM
3611 ;;
3612 ;; This function might do hidden buffer changes.
d9e94c22
MS
3613 (or (< (skip-syntax-backward "w_") 0)
3614 (and (c-major-mode-is 'pike-mode)
3615 ;; Handle the `<operator> syntax in Pike.
3616 (let ((pos (point)))
2a15eb73 3617 (if (and (< (skip-chars-backward "-!%&*+/<=>^|~[]()") 0)
d9e94c22
MS
3618 (< (skip-chars-backward "`") 0)
3619 (looking-at c-symbol-key)
3620 (>= (match-end 0) pos))
3621 t
3622 (goto-char pos)
3623 nil)))))
3624
0386b551 3625(defun c-beginning-of-current-token (&optional back-limit)
d9e94c22
MS
3626 ;; Move to the beginning of the current token. Do not move if not
3627 ;; in the middle of one. BACK-LIMIT may be used to bound the
3628 ;; backward search; if given it's assumed to be at the boundary
a85fd6da 3629 ;; between two tokens. Return non-nil if the point is moved, nil
580fba94 3630 ;; otherwise.
0386b551
AM
3631 ;;
3632 ;; This function might do hidden buffer changes.
d9e94c22 3633 (let ((start (point)))
580fba94
AM
3634 (if (looking-at "\\w\\|\\s_")
3635 (skip-syntax-backward "w_" back-limit)
3636 (when (< (skip-syntax-backward ".()" back-limit) 0)
3637 (while (let ((pos (or (and (looking-at c-nonsymbol-token-regexp)
3638 (match-end 0))
3639 ;; `c-nonsymbol-token-regexp' should always match
3640 ;; since we've skipped backward over punctuator
3641 ;; or paren syntax, but consume one char in case
3642 ;; it doesn't so that we don't leave point before
3643 ;; some earlier incorrect token.
3644 (1+ (point)))))
3645 (if (<= pos start)
3646 (goto-char pos))))))
3647 (< (point) start)))
d9e94c22 3648
ff959bab 3649(defun c-end-of-current-token (&optional back-limit)
d9e94c22
MS
3650 ;; Move to the end of the current token. Do not move if not in the
3651 ;; middle of one. BACK-LIMIT may be used to bound the backward
3652 ;; search; if given it's assumed to be at the boundary between two
ff959bab 3653 ;; tokens. Return non-nil if the point is moved, nil otherwise.
0386b551
AM
3654 ;;
3655 ;; This function might do hidden buffer changes.
d9e94c22
MS
3656 (let ((start (point)))
3657 (cond ((< (skip-syntax-backward "w_" (1- start)) 0)
3658 (skip-syntax-forward "w_"))
3659 ((< (skip-syntax-backward ".()" back-limit) 0)
3660 (while (progn
3661 (if (looking-at c-nonsymbol-token-regexp)
3662 (goto-char (match-end 0))
3663 ;; `c-nonsymbol-token-regexp' should always match since
3664 ;; we've skipped backward over punctuator or paren
3665 ;; syntax, but move forward in case it doesn't so that
3666 ;; we don't leave point earlier than we started with.
3667 (forward-char))
ff959bab
MS
3668 (< (point) start)))))
3669 (> (point) start)))
d9e94c22
MS
3670
3671(defconst c-jump-syntax-balanced
3672 (if (memq 'gen-string-delim c-emacs-features)
3673 "\\w\\|\\s_\\|\\s\(\\|\\s\)\\|\\s\"\\|\\s|"
3674 "\\w\\|\\s_\\|\\s\(\\|\\s\)\\|\\s\""))
3675
3676(defconst c-jump-syntax-unbalanced
3677 (if (memq 'gen-string-delim c-emacs-features)
3678 "\\w\\|\\s_\\|\\s\"\\|\\s|"
3679 "\\w\\|\\s_\\|\\s\""))
3680
3681(defun c-forward-token-2 (&optional count balanced limit)
3682 "Move forward by tokens.
3683A token is defined as all symbols and identifiers which aren't
3684syntactic whitespace \(note that multicharacter tokens like \"==\" are
3685treated properly). Point is always either left at the beginning of a
3686token or not moved at all. COUNT specifies the number of tokens to
3687move; a negative COUNT moves in the opposite direction. A COUNT of 0
3688moves to the next token beginning only if not already at one. If
3689BALANCED is true, move over balanced parens, otherwise move into them.
3690Also, if BALANCED is true, never move out of an enclosing paren.
3691
3692LIMIT sets the limit for the movement and defaults to the point limit.
3693The case when LIMIT is set in the middle of a token, comment or macro
3694is handled correctly, i.e. the point won't be left there.
3695
3696Return the number of tokens left to move \(positive or negative). If
3697BALANCED is true, a move over a balanced paren counts as one. Note
3698that if COUNT is 0 and no appropriate token beginning is found, 1 will
3699be returned. Thus, a return value of 0 guarantees that point is at
3700the requested position and a return value less \(without signs) than
0386b551
AM
3701COUNT guarantees that point is at the beginning of some token.
3702
3703Note that this function might do hidden buffer changes. See the
3704comment at the start of cc-engine.el for more info."
d9e94c22
MS
3705
3706 (or count (setq count 1))
3707 (if (< count 0)
3708 (- (c-backward-token-2 (- count) balanced limit))
3709
3710 (let ((jump-syntax (if balanced
3711 c-jump-syntax-balanced
3712 c-jump-syntax-unbalanced))
3713 (last (point))
3714 (prev (point)))
3715
3716 (if (zerop count)
3717 ;; If count is zero we should jump if in the middle of a token.
3718 (c-end-of-current-token))
3719
3720 (save-restriction
3721 (if limit (narrow-to-region (point-min) limit))
3722 (if (/= (point)
3723 (progn (c-forward-syntactic-ws) (point)))
3724 ;; Skip whitespace. Count this as a move if we did in
3725 ;; fact move.
3726 (setq count (max (1- count) 0)))
3727
3728 (if (eobp)
3729 ;; Moved out of bounds. Make sure the returned count isn't zero.
3730 (progn
3731 (if (zerop count) (setq count 1))
3732 (goto-char last))
3733
3734 ;; Use `condition-case' to avoid having the limit tests
3735 ;; inside the loop.
3736 (condition-case nil
3737 (while (and
3738 (> count 0)
3739 (progn
3740 (setq last (point))
3741 (cond ((looking-at jump-syntax)
3742 (goto-char (scan-sexps (point) 1))
3743 t)
3744 ((looking-at c-nonsymbol-token-regexp)
3745 (goto-char (match-end 0))
3746 t)
3747 ;; `c-nonsymbol-token-regexp' above should always
3748 ;; match if there are correct tokens. Try to
3749 ;; widen to see if the limit was set in the
3750 ;; middle of one, else fall back to treating
3751 ;; the offending thing as a one character token.
3752 ((and limit
3753 (save-restriction
3754 (widen)
3755 (looking-at c-nonsymbol-token-regexp)))
3756 nil)
3757 (t
3758 (forward-char)
3759 t))))
3760 (c-forward-syntactic-ws)
3761 (setq prev last
3762 count (1- count)))
3763 (error (goto-char last)))
3764
3765 (when (eobp)
3766 (goto-char prev)
3767 (setq count (1+ count)))))
3768
3769 count)))
3770
3771(defun c-backward-token-2 (&optional count balanced limit)
3772 "Move backward by tokens.
3773See `c-forward-token-2' for details."
3774
3775 (or count (setq count 1))
3776 (if (< count 0)
3777 (- (c-forward-token-2 (- count) balanced limit))
3778
3779 (or limit (setq limit (point-min)))
3780 (let ((jump-syntax (if balanced
3781 c-jump-syntax-balanced
3782 c-jump-syntax-unbalanced))
3783 (last (point)))
3784
3785 (if (zerop count)
3786 ;; The count is zero so try to skip to the beginning of the
3787 ;; current token.
3788 (if (> (point)
3789 (progn (c-beginning-of-current-token) (point)))
3790 (if (< (point) limit)
3791 ;; The limit is inside the same token, so return 1.
3792 (setq count 1))
3793
3794 ;; We're not in the middle of a token. If there's
3795 ;; whitespace after the point then we must move backward,
3796 ;; so set count to 1 in that case.
3797 (and (looking-at c-syntactic-ws-start)
3798 ;; If we're looking at a '#' that might start a cpp
3799 ;; directive then we have to do a more elaborate check.
3800 (or (/= (char-after) ?#)
3801 (not c-opt-cpp-prefix)
3802 (save-excursion
3803 (and (= (point)
3804 (progn (beginning-of-line)
3805 (looking-at "[ \t]*")
3806 (match-end 0)))
3807 (or (bobp)
3808 (progn (backward-char)
3809 (not (eq (char-before) ?\\)))))))
3810 (setq count 1))))
3811
3812 ;; Use `condition-case' to avoid having to check for buffer
3813 ;; limits in `backward-char', `scan-sexps' and `goto-char' below.
3814 (condition-case nil
3815 (while (and
3816 (> count 0)
3817 (progn
3818 (c-backward-syntactic-ws)
3819 (backward-char)
3820 (if (looking-at jump-syntax)
3821 (goto-char (scan-sexps (1+ (point)) -1))
3822 ;; This can be very inefficient if there's a long
3823 ;; sequence of operator tokens without any separation.
3824 ;; That doesn't happen in practice, anyway.
3825 (c-beginning-of-current-token))
3826 (>= (point) limit)))
3827 (setq last (point)
3828 count (1- count)))
3829 (error (goto-char last)))
3830
3831 (if (< (point) limit)
3832 (goto-char last))
3833
3834 count)))
3835
3836(defun c-forward-token-1 (&optional count balanced limit)
3837 "Like `c-forward-token-2' but doesn't treat multicharacter operator
3838tokens like \"==\" as single tokens, i.e. all sequences of symbol
3839characters are jumped over character by character. This function is
3840for compatibility only; it's only a wrapper over `c-forward-token-2'."
3841 (let ((c-nonsymbol-token-regexp "\\s.\\|\\s\(\\|\\s\)"))
3842 (c-forward-token-2 count balanced limit)))
3843
3844(defun c-backward-token-1 (&optional count balanced limit)
3845 "Like `c-backward-token-2' but doesn't treat multicharacter operator
3846tokens like \"==\" as single tokens, i.e. all sequences of symbol
3847characters are jumped over character by character. This function is
3848for compatibility only; it's only a wrapper over `c-backward-token-2'."
3849 (let ((c-nonsymbol-token-regexp "\\s.\\|\\s\(\\|\\s\)"))
3850 (c-backward-token-2 count balanced limit)))
3851
3852\f
3853;; Tools for doing searches restricted to syntactically relevant text.
3854
3855(defun c-syntactic-re-search-forward (regexp &optional bound noerror
3856 paren-level not-inside-token
3857 lookbehind-submatch)
3858 "Like `re-search-forward', but only report matches that are found
3859in syntactically significant text. I.e. matches in comments, macros
3860or string literals are ignored. The start point is assumed to be
3861outside any comment, macro or string literal, or else the content of
3862that region is taken as syntactically significant text.
3863
3864If PAREN-LEVEL is non-nil, an additional restriction is added to
2a15eb73
MS
3865ignore matches in nested paren sexps. The search will also not go
3866outside the current list sexp, which has the effect that if the point
3867should be moved to BOUND when no match is found \(i.e. NOERROR is
3868neither nil nor t), then it will be at the closing paren if the end of
3869the current list sexp is encountered first.
d9e94c22
MS
3870
3871If NOT-INSIDE-TOKEN is non-nil, matches in the middle of tokens are
3872ignored. Things like multicharacter operators and special symbols
3873\(e.g. \"`()\" in Pike) are handled but currently not floating point
3874constants.
3875
3876If LOOKBEHIND-SUBMATCH is non-nil, it's taken as a number of a
3877subexpression in REGEXP. The end of that submatch is used as the
3878position to check for syntactic significance. If LOOKBEHIND-SUBMATCH
3879isn't used or if that subexpression didn't match then the start
3880position of the whole match is used instead. The \"look behind\"
3881subexpression is never tested before the starting position, so it
3882might be a good idea to include \\=\\= as a match alternative in it.
3883
3884Optimization note: Matches might be missed if the \"look behind\"
2a15eb73 3885subexpression can match the end of nonwhite syntactic whitespace,
d9e94c22 3886i.e. the end of comments or cpp directives. This since the function
2a15eb73
MS
3887skips over such things before resuming the search. It's on the other
3888hand not safe to assume that the \"look behind\" subexpression never
3889matches syntactic whitespace.
3890
3891Bug: Unbalanced parens inside cpp directives are currently not handled
3892correctly \(i.e. they don't get ignored as they should) when
0386b551
AM
3893PAREN-LEVEL is set.
3894
3895Note that this function might do hidden buffer changes. See the
3896comment at the start of cc-engine.el for more info."
d9e94c22
MS
3897
3898 (or bound (setq bound (point-max)))
3899 (if paren-level (setq paren-level -1))
3900
3901 ;;(message "c-syntactic-re-search-forward %s %s %S" (point) bound regexp)
3902
3903 (let ((start (point))
2a15eb73
MS
3904 tmp
3905 ;; Start position for the last search.
3906 search-pos
3907 ;; The `parse-partial-sexp' state between the start position
3908 ;; and the point.
3909 state
3910 ;; The current position after the last state update. The next
3911 ;; `parse-partial-sexp' continues from here.
3912 (state-pos (point))
3913 ;; The position at which to check the state and the state
3914 ;; there. This is separate from `state-pos' since we might
3915 ;; need to back up before doing the next search round.
3916 check-pos check-state
3917 ;; Last position known to end a token.
d9e94c22 3918 (last-token-end-pos (point-min))
2a15eb73
MS
3919 ;; Set when a valid match is found.
3920 found)
d9e94c22
MS
3921
3922 (condition-case err
3923 (while
3924 (and
2a15eb73
MS
3925 (progn
3926 (setq search-pos (point))
3927 (re-search-forward regexp bound noerror))
d9e94c22
MS
3928
3929 (progn
2a15eb73
MS
3930 (setq state (parse-partial-sexp
3931 state-pos (match-beginning 0) paren-level nil state)
3932 state-pos (point))
d9e94c22 3933 (if (setq check-pos (and lookbehind-submatch
2a15eb73
MS
3934 (or (not paren-level)
3935 (>= (car state) 0))
d9e94c22
MS
3936 (match-end lookbehind-submatch)))
3937 (setq check-state (parse-partial-sexp
2a15eb73
MS
3938 state-pos check-pos paren-level nil state))
3939 (setq check-pos state-pos
d9e94c22
MS
3940 check-state state))
3941
2a15eb73
MS
3942 ;; NOTE: If we got a look behind subexpression and get
3943 ;; an insignificant match in something that isn't
d9e94c22
MS
3944 ;; syntactic whitespace (i.e. strings or in nested
3945 ;; parentheses), then we can never skip more than a
2a15eb73
MS
3946 ;; single character from the match start position
3947 ;; (i.e. `state-pos' here) before continuing the
3948 ;; search. That since the look behind subexpression
3949 ;; might match the end of the insignificant region in
3950 ;; the next search.
d9e94c22
MS
3951
3952 (cond
d9e94c22
MS
3953 ((elt check-state 7)
3954 ;; Match inside a line comment. Skip to eol. Use
3955 ;; `re-search-forward' instead of `skip-chars-forward' to get
3956 ;; the right bound behavior.
3957 (re-search-forward "[\n\r]" bound noerror))
3958
3959 ((elt check-state 4)
3960 ;; Match inside a block comment. Skip to the '*/'.
3961 (search-forward "*/" bound noerror))
3962
3963 ((and (not (elt check-state 5))
3964 (eq (char-before check-pos) ?/)
2a15eb73 3965 (not (c-get-char-property (1- check-pos) 'syntax-table))
d9e94c22
MS
3966 (memq (char-after check-pos) '(?/ ?*)))
3967 ;; Match in the middle of the opener of a block or line
3968 ;; comment.
3969 (if (= (char-after check-pos) ?/)
3970 (re-search-forward "[\n\r]" bound noerror)
3971 (search-forward "*/" bound noerror)))
3972
2a15eb73
MS
3973 ;; The last `parse-partial-sexp' above might have
3974 ;; stopped short of the real check position if the end
3975 ;; of the current sexp was encountered in paren-level
3976 ;; mode. The checks above are always false in that
3977 ;; case, and since they can do better skipping in
3978 ;; lookbehind-submatch mode, we do them before
3979 ;; checking the paren level.
3980
3981 ((and paren-level
3982 (/= (setq tmp (car check-state)) 0))
3983 ;; Check the paren level first since we're short of the
3984 ;; syntactic checking position if the end of the
3985 ;; current sexp was encountered by `parse-partial-sexp'.
3986 (if (> tmp 0)
3987
3988 ;; Inside a nested paren sexp.
3989 (if lookbehind-submatch
3990 ;; See the NOTE above.
3991 (progn (goto-char state-pos) t)
3992 ;; Skip out of the paren quickly.
3993 (setq state (parse-partial-sexp state-pos bound 0 nil state)
3994 state-pos (point)))
3995
3996 ;; Have exited the current paren sexp.
3997 (if noerror
3998 (progn
3999 ;; The last `parse-partial-sexp' call above
4000 ;; has left us just after the closing paren
4001 ;; in this case, so we can modify the bound
4002 ;; to leave the point at the right position
4003 ;; upon return.
4004 (setq bound (1- (point)))
4005 nil)
4006 (signal 'search-failed (list regexp)))))
4007
4008 ((setq tmp (elt check-state 3))
4009 ;; Match inside a string.
4010 (if (or lookbehind-submatch
4011 (not (integerp tmp)))
4012 ;; See the NOTE above.
4013 (progn (goto-char state-pos) t)
4014 ;; Skip to the end of the string before continuing.
4015 (let ((ender (make-string 1 tmp)) (continue t))
4016 (while (if (search-forward ender bound noerror)
4017 (progn
4018 (setq state (parse-partial-sexp
4019 state-pos (point) nil nil state)
4020 state-pos (point))
4021 (elt state 3))
4022 (setq continue nil)))
4023 continue)))
d9e94c22
MS
4024
4025 ((save-excursion
4026 (save-match-data
4027 (c-beginning-of-macro start)))
4028 ;; Match inside a macro. Skip to the end of it.
4029 (c-end-of-macro)
4030 (cond ((<= (point) bound) t)
4031 (noerror nil)
2a15eb73 4032 (t (signal 'search-failed (list regexp)))))
d9e94c22 4033
2a15eb73
MS
4034 ((and not-inside-token
4035 (or (< check-pos last-token-end-pos)
4036 (< check-pos
4037 (save-excursion
4038 (goto-char check-pos)
4039 (save-match-data
4040 (c-end-of-current-token last-token-end-pos))
4041 (setq last-token-end-pos (point))))))
4042 ;; Inside a token.
4043 (if lookbehind-submatch
4044 ;; See the NOTE above.
4045 (goto-char state-pos)
4046 (goto-char (min last-token-end-pos bound))))
d9e94c22
MS
4047
4048 (t
4049 ;; A real match.
4050 (setq found t)
2a15eb73
MS
4051 nil)))
4052
4053 ;; Should loop to search again, but take care to avoid
4054 ;; looping on the same spot.
4055 (or (/= search-pos (point))
4056 (if (= (point) bound)
4057 (if noerror
4058 nil
4059 (signal 'search-failed (list regexp)))
4060 (forward-char)
4061 t))))
d9e94c22
MS
4062
4063 (error
4064 (goto-char start)
4065 (signal (car err) (cdr err))))
4066
2a15eb73 4067 ;;(message "c-syntactic-re-search-forward done %s" (or (match-end 0) (point)))
d9e94c22
MS
4068
4069 (if found
4070 (progn
2a15eb73
MS
4071 (goto-char (match-end 0))
4072 (match-end 0))
d9e94c22
MS
4073
4074 ;; Search failed. Set point as appropriate.
2a15eb73
MS
4075 (if (eq noerror t)
4076 (goto-char start)
4077 (goto-char bound))
d9e94c22
MS
4078 nil)))
4079
47641aac
GM
4080(defvar safe-pos-list) ; bound in c-syntactic-skip-backward
4081
d0fcee66
AM
4082(defsubst c-ssb-lit-begin ()
4083 ;; Return the start of the literal point is in, or nil.
4084 ;; We read and write the variables `safe-pos', `safe-pos-list', `state'
4085 ;; bound in the caller.
4086
4087 ;; Use `parse-partial-sexp' from a safe position down to the point to check
4088 ;; if it's outside comments and strings.
4089 (save-excursion
4090 (let ((pos (point)) safe-pos state pps-end-pos)
4091 ;; Pick a safe position as close to the point as possible.
4092 ;;
4093 ;; FIXME: Consult `syntax-ppss' here if our cache doesn't give a good
4094 ;; position.
47641aac 4095
d0fcee66
AM
4096 (while (and safe-pos-list
4097 (> (car safe-pos-list) (point)))
4098 (setq safe-pos-list (cdr safe-pos-list)))
4099 (unless (setq safe-pos (car-safe safe-pos-list))
4100 (setq safe-pos (max (or (c-safe-position
4101 (point) (or c-state-cache
4102 (c-parse-state)))
4103 0)
4104 (point-min))
4105 safe-pos-list (list safe-pos)))
4106
4107 ;; Cache positions along the way to use if we have to back up more. We
4108 ;; cache every closing paren on the same level. If the paren cache is
4109 ;; relevant in this region then we're typically already on the same
4110 ;; level as the target position. Note that we might cache positions
4111 ;; after opening parens in case safe-pos is in a nested list. That's
4112 ;; both uncommon and harmless.
4113 (while (progn
4114 (setq state (parse-partial-sexp
4115 safe-pos pos 0))
4116 (< (point) pos))
4117 (setq safe-pos (point)
4118 safe-pos-list (cons safe-pos safe-pos-list)))
4119
4120 ;; If the state contains the start of the containing sexp we cache that
4121 ;; position too, so that parse-partial-sexp in the next run has a bigger
4122 ;; chance of starting at the same level as the target position and thus
4123 ;; will get more good safe positions into the list.
4124 (if (elt state 1)
4125 (setq safe-pos (1+ (elt state 1))
4126 safe-pos-list (cons safe-pos safe-pos-list)))
4127
4128 (if (or (elt state 3) (elt state 4))
4129 ;; Inside string or comment. Continue search at the
4130 ;; beginning of it.
4131 (elt state 8)))))
4132
0386b551 4133(defun c-syntactic-skip-backward (skip-chars &optional limit paren-level)
d9e94c22
MS
4134 "Like `skip-chars-backward' but only look at syntactically relevant chars,
4135i.e. don't stop at positions inside syntactic whitespace or string
4136literals. Preprocessor directives are also ignored, with the exception
4137of the one that the point starts within, if any. If LIMIT is given,
0386b551
AM
4138it's assumed to be at a syntactically relevant position.
4139
4140If PAREN-LEVEL is non-nil, the function won't stop in nested paren
4141sexps, and the search will also not go outside the current paren sexp.
4142However, if LIMIT or the buffer limit is reached inside a nested paren
4143then the point will be left at the limit.
4144
4145Non-nil is returned if the point moved, nil otherwise.
4146
4147Note that this function might do hidden buffer changes. See the
4148comment at the start of cc-engine.el for more info."
d9e94c22
MS
4149
4150 (let ((start (point))
d0fcee66 4151 state-2
d9e94c22
MS
4152 ;; A list of syntactically relevant positions in descending
4153 ;; order. It's used to avoid scanning repeatedly over
4154 ;; potentially large regions with `parse-partial-sexp' to verify
d0fcee66 4155 ;; each position. Used in `c-ssb-lit-begin'
d9e94c22
MS
4156 safe-pos-list
4157 ;; The result from `c-beginning-of-macro' at the start position or the
4158 ;; start position itself if it isn't within a macro. Evaluated on
4159 ;; demand.
0386b551
AM
4160 start-macro-beg
4161 ;; The earliest position after the current one with the same paren
4162 ;; level. Used only when `paren-level' is set.
d0fcee66 4163 lit-beg
0386b551 4164 (paren-level-pos (point)))
d9e94c22 4165
d0fcee66
AM
4166 (while
4167 (progn
4168 ;; The next loop "tries" to find the end point each time round,
4169 ;; loops when it hasn't succeeded.
4170 (while
4171 (and
4172 (< (skip-chars-backward skip-chars limit) 0)
4173
4174 (let ((pos (point)) state-2 pps-end-pos)
4175
4176 (cond
4177 ;; Don't stop inside a literal
4178 ((setq lit-beg (c-ssb-lit-begin))
4179 (goto-char lit-beg)
4180 t)
4181
4182 ((and paren-level
4183 (save-excursion
4184 (setq state-2 (parse-partial-sexp
4185 pos paren-level-pos -1)
4186 pps-end-pos (point))
4187 (/= (car state-2) 0)))
4188 ;; Not at the right level.
4189
4190 (if (and (< (car state-2) 0)
4191 ;; We stop above if we go out of a paren.
4192 ;; Now check whether it precedes or is
4193 ;; nested in the starting sexp.
4194 (save-excursion
4195 (setq state-2
4196 (parse-partial-sexp
4197 pps-end-pos paren-level-pos
4198 nil nil state-2))
4199 (< (car state-2) 0)))
4200
4201 ;; We've stopped short of the starting position
4202 ;; so the hit was inside a nested list. Go up
4203 ;; until we are at the right level.
4204 (condition-case nil
4205 (progn
4206 (goto-char (scan-lists pos -1
4207 (- (car state-2))))
4208 (setq paren-level-pos (point))
4209 (if (and limit (>= limit paren-level-pos))
0386b551 4210 (progn
d0fcee66
AM
4211 (goto-char limit)
4212 nil)
4213 t))
4214 (error
4215 (goto-char (or limit (point-min)))
4216 nil))
4217
4218 ;; The hit was outside the list at the start
4219 ;; position. Go to the start of the list and exit.
4220 (goto-char (1+ (elt state-2 1)))
4221 nil))
4222
4223 ((c-beginning-of-macro limit)
4224 ;; Inside a macro.
4225 (if (< (point)
4226 (or start-macro-beg
4227 (setq start-macro-beg
4228 (save-excursion
4229 (goto-char start)
4230 (c-beginning-of-macro limit)
4231 (point)))))
4232 t
4233
4234 ;; It's inside the same macro we started in so it's
4235 ;; a relevant match.
4236 (goto-char pos)
4237 nil))))))
91af3942 4238
d0fcee66
AM
4239 (> (point)
4240 (progn
4241 ;; Skip syntactic ws afterwards so that we don't stop at the
4242 ;; end of a comment if `skip-chars' is something like "^/".
4243 (c-backward-syntactic-ws)
4244 (point)))))
d9e94c22 4245
0386b551
AM
4246 ;; We might want to extend this with more useful return values in
4247 ;; the future.
4248 (/= (point) start)))
4249
4250;; The following is an alternative implementation of
4251;; `c-syntactic-skip-backward' that uses backward movement to keep
4252;; track of the syntactic context. It turned out to be generally
4253;; slower than the one above which uses forward checks from earlier
4254;; safe positions.
4255;;
4256;;(defconst c-ssb-stop-re
4257;; ;; The regexp matching chars `c-syntactic-skip-backward' needs to
4258;; ;; stop at to avoid going into comments and literals.
4259;; (concat
4260;; ;; Match comment end syntax and string literal syntax. Also match
4261;; ;; '/' for block comment endings (not covered by comment end
4262;; ;; syntax).
4263;; "\\s>\\|/\\|\\s\""
4264;; (if (memq 'gen-string-delim c-emacs-features)
4265;; "\\|\\s|"
4266;; "")
4267;; (if (memq 'gen-comment-delim c-emacs-features)
4268;; "\\|\\s!"
4269;; "")))
4270;;
4271;;(defconst c-ssb-stop-paren-re
4272;; ;; Like `c-ssb-stop-re' but also stops at paren chars.
4273;; (concat c-ssb-stop-re "\\|\\s(\\|\\s)"))
4274;;
4275;;(defconst c-ssb-sexp-end-re
4276;; ;; Regexp matching the ending syntax of a complex sexp.
4277;; (concat c-string-limit-regexp "\\|\\s)"))
4278;;
4279;;(defun c-syntactic-skip-backward (skip-chars &optional limit paren-level)
4280;; "Like `skip-chars-backward' but only look at syntactically relevant chars,
4281;;i.e. don't stop at positions inside syntactic whitespace or string
4282;;literals. Preprocessor directives are also ignored. However, if the
4283;;point is within a comment, string literal or preprocessor directory to
4284;;begin with, its contents is treated as syntactically relevant chars.
4285;;If LIMIT is given, it limits the backward search and the point will be
4286;;left there if no earlier position is found.
4287;;
4288;;If PAREN-LEVEL is non-nil, the function won't stop in nested paren
4289;;sexps, and the search will also not go outside the current paren sexp.
4290;;However, if LIMIT or the buffer limit is reached inside a nested paren
4291;;then the point will be left at the limit.
4292;;
4293;;Non-nil is returned if the point moved, nil otherwise.
4294;;
4295;;Note that this function might do hidden buffer changes. See the
4296;;comment at the start of cc-engine.el for more info."
4297;;
4298;; (save-restriction
4299;; (when limit
4300;; (narrow-to-region limit (point-max)))
4301;;
4302;; (let ((start (point)))
4303;; (catch 'done
4304;; (while (let ((last-pos (point))
4305;; (stop-pos (progn
4306;; (skip-chars-backward skip-chars)
4307;; (point))))
4308;;
4309;; ;; Skip back over the same region as
4310;; ;; `skip-chars-backward' above, but keep to
4311;; ;; syntactically relevant positions.
4312;; (goto-char last-pos)
4313;; (while (and
4314;; ;; `re-search-backward' with a single char regexp
4315;; ;; should be fast.
4316;; (re-search-backward
4317;; (if paren-level c-ssb-stop-paren-re c-ssb-stop-re)
4318;; stop-pos 'move)
4319;;
4320;; (progn
4321;; (cond
4322;; ((looking-at "\\s(")
4323;; ;; `paren-level' is set and we've found the
4324;; ;; start of the containing paren.
4325;; (forward-char)
4326;; (throw 'done t))
4327;;
4328;; ((looking-at c-ssb-sexp-end-re)
4329;; ;; We're at the end of a string literal or paren
4330;; ;; sexp (if `paren-level' is set).
4331;; (forward-char)
4332;; (condition-case nil
4333;; (c-backward-sexp)
4334;; (error
4335;; (goto-char limit)
4336;; (throw 'done t))))
4337;;
4338;; (t
4339;; (forward-char)
4340;; ;; At the end of some syntactic ws or possibly
4341;; ;; after a plain '/' operator.
4342;; (let ((pos (point)))
4343;; (c-backward-syntactic-ws)
4344;; (if (= pos (point))
4345;; ;; Was a plain '/' operator. Go past it.
4346;; (backward-char)))))
4347;;
4348;; (> (point) stop-pos))))
4349;;
4350;; ;; Now the point is either at `stop-pos' or at some
4351;; ;; position further back if `stop-pos' was at a
4352;; ;; syntactically irrelevant place.
4353;;
4354;; ;; Skip additional syntactic ws so that we don't stop
4355;; ;; at the end of a comment if `skip-chars' is
4356;; ;; something like "^/".
4357;; (c-backward-syntactic-ws)
4358;;
4359;; (< (point) stop-pos))))
4360;;
4361;; ;; We might want to extend this with more useful return values
4362;; ;; in the future.
4363;; (/= (point) start))))
d9e94c22
MS
4364
4365\f
4366;; Tools for handling comments and string literals.
4367
13d49cbb 4368(defun c-in-literal (&optional lim detect-cpp)
d9e94c22
MS
4369 "Return the type of literal point is in, if any.
4370The return value is `c' if in a C-style comment, `c++' if in a C++
4371style comment, `string' if in a string literal, `pound' if DETECT-CPP
4372is non-nil and in a preprocessor line, or nil if somewhere else.
4373Optional LIM is used as the backward limit of the search. If omitted,
4374or nil, `c-beginning-of-defun' is used.
4375
4376The last point calculated is cached if the cache is enabled, i.e. if
4377`c-in-literal-cache' is bound to a two element vector.
4378
0386b551
AM
4379Note that this function might do hidden buffer changes. See the
4380comment at the start of cc-engine.el for more info."
2cc769a8
AM
4381 (save-restriction
4382 (widen)
56d093a9 4383 (let* ((safe-place (c-state-semi-safe-place (point)))
2cc769a8
AM
4384 (lit (c-state-pp-to-literal safe-place (point))))
4385 (or (cadr lit)
4386 (and detect-cpp
4387 (save-excursion (c-beginning-of-macro))
4388 'pound)))))
d9e94c22
MS
4389
4390(defun c-literal-limits (&optional lim near not-in-delimiter)
4391 "Return a cons of the beginning and end positions of the comment or
4392string surrounding point (including both delimiters), or nil if point
4393isn't in one. If LIM is non-nil, it's used as the \"safe\" position
4394to start parsing from. If NEAR is non-nil, then the limits of any
4395literal next to point is returned. \"Next to\" means there's only
4396spaces and tabs between point and the literal. The search for such a
4397literal is done first in forward direction. If NOT-IN-DELIMITER is
4398non-nil, the case when point is inside a starting delimiter won't be
a85fd6da 4399recognized. This only has effect for comments which have starting
d9e94c22
MS
4400delimiters with more than one character.
4401
0386b551
AM
4402Note that this function might do hidden buffer changes. See the
4403comment at the start of cc-engine.el for more info."
d9e94c22
MS
4404
4405 (save-excursion
4406 (let* ((pos (point))
56d093a9 4407 (lim (or lim (c-state-semi-safe-place pos)))
2cc769a8
AM
4408 (pp-to-lit (save-restriction
4409 (widen)
4410 (c-state-pp-to-literal lim pos)))
13d49cbb 4411 (state (car pp-to-lit))
13d49cbb 4412 (lit-limits (car (cddr pp-to-lit))))
d9e94c22 4413
13d49cbb
AM
4414 (cond
4415 (lit-limits)
4416 ((and (not not-in-delimiter)
4417 (not (elt state 5))
4418 (eq (char-before) ?/)
4419 (looking-at "[/*]")) ; FIXME!!! use c-line/block-comment-starter. 2008-09-28.
4420 ;; We're standing in a comment starter.
4421 (backward-char 1)
4422 (cons (point) (progn (c-forward-single-comment) (point))))
4423
4424 (near
4425 (goto-char pos)
4426 ;; Search forward for a literal.
4427 (skip-chars-forward " \t")
4428 (cond
4429 ((looking-at c-string-limit-regexp) ; String.
4430 (cons (point) (or (c-safe (c-forward-sexp 1) (point))
4431 (point-max))))
d9e94c22 4432
13d49cbb
AM
4433 ((looking-at c-comment-start-regexp) ; Line or block comment.
4434 (cons (point) (progn (c-forward-single-comment) (point))))
4435
4436 (t
4437 ;; Search backward.
4438 (skip-chars-backward " \t")
4439
4440 (let ((end (point)) beg)
4441 (cond
4442 ((save-excursion
4443 (< (skip-syntax-backward c-string-syntax) 0)) ; String.
4444 (setq beg (c-safe (c-backward-sexp 1) (point))))
4445
4446 ((and (c-safe (forward-char -2) t)
4447 (looking-at "*/"))
4448 ;; Block comment. Due to the nature of line
4449 ;; comments, they will always be covered by the
4450 ;; normal case above.
4451 (goto-char end)
4452 (c-backward-single-comment)
4453 ;; If LIM is bogus, beg will be bogus.
4454 (setq beg (point))))
4455
4456 (if beg (cons beg end))))))
4457 ))))
d9e94c22 4458
0386b551
AM
4459;; In case external callers use this; it did have a docstring.
4460(defalias 'c-literal-limits-fast 'c-literal-limits)
d9e94c22 4461
0386b551
AM
4462(defun c-collect-line-comments (range)
4463 "If the argument is a cons of two buffer positions (such as returned by
4464`c-literal-limits'), and that range contains a C++ style line comment,
4465then an extended range is returned that contains all adjacent line
4466comments (i.e. all comments that starts in the same column with no
4467empty lines or non-whitespace characters between them). Otherwise the
4468argument is returned.
d9e94c22 4469
0386b551
AM
4470Note that this function might do hidden buffer changes. See the
4471comment at the start of cc-engine.el for more info."
d9e94c22
MS
4472
4473 (save-excursion
0386b551
AM
4474 (condition-case nil
4475 (if (and (consp range) (progn
4476 (goto-char (car range))
4477 (looking-at c-line-comment-starter)))
b414f371 4478 (let ((col (current-column))
0386b551
AM
4479 (beg (point))
4480 (bopl (c-point 'bopl))
4481 (end (cdr range)))
4482 ;; Got to take care in the backward direction to handle
4483 ;; comments which are preceded by code.
4484 (while (and (c-backward-single-comment)
4485 (>= (point) bopl)
4486 (looking-at c-line-comment-starter)
4487 (= col (current-column)))
4488 (setq beg (point)
4489 bopl (c-point 'bopl)))
4490 (goto-char end)
4491 (while (and (progn (skip-chars-forward " \t")
4492 (looking-at c-line-comment-starter))
4493 (= col (current-column))
4494 (prog1 (zerop (forward-line 1))
4495 (setq end (point)))))
4496 (cons beg end))
4497 range)
4498 (error range))))
d9e94c22
MS
4499
4500(defun c-literal-type (range)
4501 "Convenience function that given the result of `c-literal-limits',
a85fd6da
AM
4502returns nil or the type of literal that the range surrounds, one
4503of the symbols 'c, 'c++ or 'string. It's much faster than using
4504`c-in-literal' and is intended to be used when you need both the
4505type of a literal and its limits.
d9e94c22 4506
0386b551
AM
4507Note that this function might do hidden buffer changes. See the
4508comment at the start of cc-engine.el for more info."
4509
d9e94c22
MS
4510 (if (consp range)
4511 (save-excursion
4512 (goto-char (car range))
4513 (cond ((looking-at c-string-limit-regexp) 'string)
4514 ((or (looking-at "//") ; c++ line comment
4515 (and (looking-at "\\s<") ; comment starter
4516 (looking-at "#"))) ; awk comment.
4517 'c++)
4518 (t 'c))) ; Assuming the range is valid.
4519 range))
4520
9657183b
AM
4521(defsubst c-determine-limit-get-base (start try-size)
4522 ;; Get a "safe place" approximately TRY-SIZE characters before START.
4523 ;; This doesn't preserve point.
4524 (let* ((pos (max (- start try-size) (point-min)))
56d093a9 4525 (base (c-state-semi-safe-place pos))
9657183b
AM
4526 (s (parse-partial-sexp base pos)))
4527 (if (or (nth 4 s) (nth 3 s)) ; comment or string
4528 (nth 8 s)
4529 (point))))
4530
4531(defun c-determine-limit (how-far-back &optional start try-size)
4532 ;; Return a buffer position HOW-FAR-BACK non-literal characters from START
4533 ;; (default point). This is done by going back further in the buffer then
4534 ;; searching forward for literals. The position found won't be in a
4535 ;; literal. We start searching for the sought position TRY-SIZE (default
4536 ;; twice HOW-FAR-BACK) bytes back from START. This function must be fast.
4537 ;; :-)
4538 (save-excursion
4539 (let* ((start (or start (point)))
4540 (try-size (or try-size (* 2 how-far-back)))
4541 (base (c-determine-limit-get-base start try-size))
4542 (pos base)
4543
4544 (s (parse-partial-sexp pos pos)) ; null state.
4545 stack elt size
4546 (count 0))
4547 (while (< pos start)
4548 ;; Move forward one literal each time round this loop.
4549 ;; Move forward to the start of a comment or string.
4550 (setq s (parse-partial-sexp
4551 pos
4552 start
4553 nil ; target-depth
4554 nil ; stop-before
4555 s ; state
4556 'syntax-table)) ; stop-comment
4557
4558 ;; Gather details of the non-literal-bit - starting pos and size.
4559 (setq size (- (if (or (nth 4 s) (nth 3 s))
4560 (nth 8 s)
4561 (point))
4562 pos))
4563 (if (> size 0)
4564 (setq stack (cons (cons pos size) stack)))
4565
4566 ;; Move forward to the end of the comment/string.
4567 (if (or (nth 4 s) (nth 3 s))
4568 (setq s (parse-partial-sexp
4569 (point)
4570 start
4571 nil ; target-depth
4572 nil ; stop-before
4573 s ; state
4574 'syntax-table))) ; stop-comment
4575 (setq pos (point)))
4576
4577 ;; Now try and find enough non-literal characters recorded on the stack.
4578 ;; Go back one recorded literal each time round this loop.
4579 (while (and (< count how-far-back)
4580 stack)
4581 (setq elt (car stack)
4582 stack (cdr stack))
4583 (setq count (+ count (cdr elt))))
4584
4585 ;; Have we found enough yet?
4586 (cond
4587 ((>= count how-far-back)
4588 (+ (car elt) (- count how-far-back)))
4589 ((eq base (point-min))
4590 (point-min))
4591 (t
4592 (c-determine-limit (- how-far-back count) base try-size))))))
2e492df3
AM
4593
4594(defun c-determine-+ve-limit (how-far &optional start-pos)
4595 ;; Return a buffer position about HOW-FAR non-literal characters forward
4596 ;; from START-POS (default point), which must not be inside a literal.
4597 (save-excursion
4598 (let ((pos (or start-pos (point)))
4599 (count how-far)
4600 (s (parse-partial-sexp (point) (point)))) ; null state
4601 (while (and (not (eobp))
4602 (> count 0))
4603 ;; Scan over counted characters.
4604 (setq s (parse-partial-sexp
4605 pos
4606 (min (+ pos count) (point-max))
4607 nil ; target-depth
4608 nil ; stop-before
4609 s ; state
4610 'syntax-table)) ; stop-comment
4611 (setq count (- count (- (point) pos) 1)
4612 pos (point))
4613 ;; Scan over literal characters.
4614 (if (nth 8 s)
4615 (setq s (parse-partial-sexp
4616 pos
4617 (point-max)
4618 nil ; target-depth
4619 nil ; stop-before
4620 s ; state
4621 'syntax-table) ; stop-comment
4622 pos (point))))
4623 (point))))
4624
d9e94c22
MS
4625\f
4626;; `c-find-decl-spots' and accompanying stuff.
4627
4628;; Variables used in `c-find-decl-spots' to cache the search done for
4629;; the first declaration in the last call. When that function starts,
4630;; it needs to back up over syntactic whitespace to look at the last
4631;; token before the region being searched. That can sometimes cause
4632;; moves back and forth over a quite large region of comments and
4633;; macros, which would be repeated for each changed character when
4634;; we're called during fontification, since font-lock refontifies the
4635;; current line for each change. Thus it's worthwhile to cache the
4636;; first match.
4637;;
4638;; `c-find-decl-syntactic-pos' is a syntactically relevant position in
4639;; the syntactic whitespace less or equal to some start position.
4640;; There's no cached value if it's nil.
4641;;
4642;; `c-find-decl-match-pos' is the match position if
4643;; `c-find-decl-prefix-search' matched before the syntactic whitespace
4644;; at `c-find-decl-syntactic-pos', or nil if there's no such match.
4645(defvar c-find-decl-syntactic-pos nil)
4646(make-variable-buffer-local 'c-find-decl-syntactic-pos)
4647(defvar c-find-decl-match-pos nil)
4648(make-variable-buffer-local 'c-find-decl-match-pos)
4649
4650(defsubst c-invalidate-find-decl-cache (change-min-pos)
4651 (and c-find-decl-syntactic-pos
4652 (< change-min-pos c-find-decl-syntactic-pos)
4653 (setq c-find-decl-syntactic-pos nil)))
4654
4655; (defface c-debug-decl-spot-face
4656; '((t (:background "Turquoise")))
4657; "Debug face to mark the spots where `c-find-decl-spots' stopped.")
4658; (defface c-debug-decl-sws-face
4659; '((t (:background "Khaki")))
4660; "Debug face to mark the syntactic whitespace between the declaration
4661; spots and the preceding token end.")
4662
4663(defmacro c-debug-put-decl-spot-faces (match-pos decl-pos)
4664 (when (facep 'c-debug-decl-spot-face)
0386b551 4665 `(c-save-buffer-state ((match-pos ,match-pos) (decl-pos ,decl-pos))
d9e94c22
MS
4666 (c-debug-add-face (max match-pos (point-min)) decl-pos
4667 'c-debug-decl-sws-face)
4668 (c-debug-add-face decl-pos (min (1+ decl-pos) (point-max))
4669 'c-debug-decl-spot-face))))
4670(defmacro c-debug-remove-decl-spot-faces (beg end)
4671 (when (facep 'c-debug-decl-spot-face)
0386b551 4672 `(c-save-buffer-state ()
d9e94c22
MS
4673 (c-debug-remove-face ,beg ,end 'c-debug-decl-spot-face)
4674 (c-debug-remove-face ,beg ,end 'c-debug-decl-sws-face))))
4675
4676(defmacro c-find-decl-prefix-search ()
4677 ;; Macro used inside `c-find-decl-spots'. It ought to be a defun,
4678 ;; but it contains lots of free variables that refer to things
4679 ;; inside `c-find-decl-spots'. The point is left at `cfd-match-pos'
4680 ;; if there is a match, otherwise at `cfd-limit'.
0386b551
AM
4681 ;;
4682 ;; This macro might do hidden buffer changes.
d9e94c22
MS
4683
4684 '(progn
4685 ;; Find the next property match position if we haven't got one already.
4686 (unless cfd-prop-match
4687 (save-excursion
4688 (while (progn
4689 (goto-char (next-single-property-change
4690 (point) 'c-type nil cfd-limit))
4691 (and (< (point) cfd-limit)
4692 (not (eq (c-get-char-property (1- (point)) 'c-type)
4693 'c-decl-end)))))
4694 (setq cfd-prop-match (point))))
4695
0386b551
AM
4696 ;; Find the next `c-decl-prefix-or-start-re' match if we haven't
4697 ;; got one already.
d9e94c22 4698 (unless cfd-re-match
0386b551
AM
4699
4700 (if (> cfd-re-match-end (point))
4701 (goto-char cfd-re-match-end))
4702
4703 (while (if (setq cfd-re-match-end
4704 (re-search-forward c-decl-prefix-or-start-re
4705 cfd-limit 'move))
4706
4707 ;; Match. Check if it's inside a comment or string literal.
4708 (c-got-face-at
4709 (if (setq cfd-re-match (match-end 1))
4710 ;; Matched the end of a token preceding a decl spot.
4711 (progn
4712 (goto-char cfd-re-match)
4713 (1- cfd-re-match))
4714 ;; Matched a token that start a decl spot.
4715 (goto-char (match-beginning 0))
4716 (point))
4717 c-literal-faces)
4718
4719 ;; No match. Finish up and exit the loop.
4720 (setq cfd-re-match cfd-limit)
4721 nil)
4722
4723 ;; Skip out of comments and string literals.
d9e94c22
MS
4724 (while (progn
4725 (goto-char (next-single-property-change
0386b551 4726 (point) 'face nil cfd-limit))
d9e94c22 4727 (and (< (point) cfd-limit)
0386b551
AM
4728 (c-got-face-at (point) c-literal-faces)))))
4729
4730 ;; If we matched at the decl start, we have to back up over the
4731 ;; preceding syntactic ws to set `cfd-match-pos' and to catch
4732 ;; any decl spots in the syntactic ws.
d9e94c22 4733 (unless cfd-re-match
0386b551
AM
4734 (c-backward-syntactic-ws)
4735 (setq cfd-re-match (point))))
d9e94c22
MS
4736
4737 ;; Choose whichever match is closer to the start.
4738 (if (< cfd-re-match cfd-prop-match)
4739 (setq cfd-match-pos cfd-re-match
4740 cfd-re-match nil)
4741 (setq cfd-match-pos cfd-prop-match
4742 cfd-prop-match nil))
4743
4744 (goto-char cfd-match-pos)
4745
4746 (when (< cfd-match-pos cfd-limit)
4747 ;; Skip forward past comments only so we don't skip macros.
4748 (c-forward-comments)
4749 ;; Set the position to continue at. We can avoid going over
4750 ;; the comments skipped above a second time, but it's possible
4751 ;; that the comment skipping has taken us past `cfd-prop-match'
4752 ;; since the property might be used inside comments.
4753 (setq cfd-continue-pos (if cfd-prop-match
4754 (min cfd-prop-match (point))
4755 (point))))))
4756
4757(defun c-find-decl-spots (cfd-limit cfd-decl-re cfd-face-checklist cfd-fun)
0386b551 4758 ;; Call CFD-FUN for each possible spot for a declaration, cast or
a85fd6da
AM
4759 ;; label from the point to CFD-LIMIT.
4760 ;;
9657183b
AM
4761 ;; CFD-FUN is called with point at the start of the spot. It's passed two
4762 ;; arguments: The first is the end position of the token preceding the spot,
4763 ;; or 0 for the implicit match at bob. The second is a flag that is t when
4764 ;; the match is inside a macro. Point should be moved forward by at least
4765 ;; one token.
4766 ;;
4767 ;; If CFD-FUN adds `c-decl-end' properties somewhere below the current spot,
4768 ;; it should return non-nil to ensure that the next search will find them.
0386b551 4769 ;;
a85fd6da 4770 ;; Such a spot is:
0386b551
AM
4771 ;; o The first token after bob.
4772 ;; o The first token after the end of submatch 1 in
4773 ;; `c-decl-prefix-or-start-re' when that submatch matches.
4774 ;; o The start of each `c-decl-prefix-or-start-re' match when
4775 ;; submatch 1 doesn't match.
5a89f0a7 4776 ;; o The first token after the end of each occurrence of the
0386b551
AM
4777 ;; `c-type' text property with the value `c-decl-end', provided
4778 ;; `c-type-decl-end-used' is set.
4779 ;;
4780 ;; Only a spot that match CFD-DECL-RE and whose face is in the
4781 ;; CFD-FACE-CHECKLIST list causes CFD-FUN to be called. The face
4782 ;; check is disabled if CFD-FACE-CHECKLIST is nil.
d9e94c22
MS
4783 ;;
4784 ;; If the match is inside a macro then the buffer is narrowed to the
4785 ;; end of it, so that CFD-FUN can investigate the following tokens
4786 ;; without matching something that begins inside a macro and ends
4787 ;; outside it. It's to avoid this work that the CFD-DECL-RE and
4788 ;; CFD-FACE-CHECKLIST checks exist.
4789 ;;
0386b551
AM
4790 ;; The spots are visited approximately in order from top to bottom.
4791 ;; It's however the positions where `c-decl-prefix-or-start-re'
4792 ;; matches and where `c-decl-end' properties are found that are in
4793 ;; order. Since the spots often are at the following token, they
4794 ;; might be visited out of order insofar as more spots are reported
4795 ;; later on within the syntactic whitespace between the match
4796 ;; positions and their spots.
4797 ;;
4798 ;; It's assumed that comments and strings are fontified in the
d9e94c22
MS
4799 ;; searched range.
4800 ;;
4801 ;; This is mainly used in fontification, and so has an elaborate
4802 ;; cache to handle repeated calls from the same start position; see
4803 ;; the variables above.
4804 ;;
4805 ;; All variables in this function begin with `cfd-' to avoid name
4806 ;; collision with the (dynamically bound) variables used in CFD-FUN.
0386b551
AM
4807 ;;
4808 ;; This function might do hidden buffer changes.
d9e94c22 4809
0386b551
AM
4810 (let ((cfd-start-pos (point))
4811 (cfd-buffer-end (point-max))
4812 ;; The end of the token preceding the decl spot last found
4813 ;; with `c-decl-prefix-or-start-re'. `cfd-limit' if there's
4814 ;; no match.
d9e94c22 4815 cfd-re-match
0386b551
AM
4816 ;; The end position of the last `c-decl-prefix-or-start-re'
4817 ;; match. If this is greater than `cfd-continue-pos', the
4818 ;; next regexp search is started here instead.
4819 (cfd-re-match-end (point-min))
4820 ;; The end of the last `c-decl-end' found by
4821 ;; `c-find-decl-prefix-search'. `cfd-limit' if there's no
4822 ;; match. If searching for the property isn't needed then we
4823 ;; disable it by setting it to `cfd-limit' directly.
d9e94c22 4824 (cfd-prop-match (unless c-type-decl-end-used cfd-limit))
0386b551
AM
4825 ;; The end of the token preceding the decl spot last found by
4826 ;; `c-find-decl-prefix-search'. 0 for the implicit match at
4827 ;; bob. `cfd-limit' if there's no match. In other words,
4828 ;; this is the minimum of `cfd-re-match' and `cfd-prop-match'.
d9e94c22
MS
4829 (cfd-match-pos cfd-limit)
4830 ;; The position to continue searching at.
4831 cfd-continue-pos
4832 ;; The position of the last "real" token we've stopped at.
4833 ;; This can be greater than `cfd-continue-pos' when we get
4834 ;; hits inside macros or at `c-decl-end' positions inside
4835 ;; comments.
4836 (cfd-token-pos 0)
4837 ;; The end position of the last entered macro.
4838 (cfd-macro-end 0))
4839
4840 ;; Initialize by finding a syntactically relevant start position
0386b551
AM
4841 ;; before the point, and do the first `c-decl-prefix-or-start-re'
4842 ;; search unless we're at bob.
d9e94c22 4843
0386b551 4844 (let (start-in-literal start-in-macro syntactic-pos)
d9e94c22
MS
4845 ;; Must back up a bit since we look for the end of the previous
4846 ;; statement or declaration, which is earlier than the first
4847 ;; returned match.
4848
0386b551
AM
4849 (cond
4850 ;; First we need to move to a syntactically relevant position.
4851 ;; Begin by backing out of comment or string literals.
4852 ((and
4853 (when (c-got-face-at (point) c-literal-faces)
4854 ;; Try to use the faces to back up to the start of the
4855 ;; literal. FIXME: What if the point is on a declaration
4856 ;; inside a comment?
4857 (while (and (not (bobp))
4858 (c-got-face-at (1- (point)) c-literal-faces))
4859 (goto-char (previous-single-property-change
4860 (point) 'face nil (point-min))))
4861
4862 ;; XEmacs doesn't fontify the quotes surrounding string
4863 ;; literals.
4864 (and (featurep 'xemacs)
4865 (eq (get-text-property (point) 'face)
4866 'font-lock-string-face)
4867 (not (bobp))
4868 (progn (backward-char)
4869 (not (looking-at c-string-limit-regexp)))
4870 (forward-char))
4871
4872 ;; Don't trust the literal to contain only literal faces
4873 ;; (the font lock package might not have fontified the
4874 ;; start of it at all, for instance) so check that we have
4875 ;; arrived at something that looks like a start or else
4876 ;; resort to `c-literal-limits'.
4877 (unless (looking-at c-literal-start-regexp)
4878 (let ((range (c-literal-limits)))
4879 (if range (goto-char (car range)))))
4880
4881 (setq start-in-literal (point)))
4882
4883 ;; The start is in a literal. If the limit is in the same
4884 ;; one we don't have to find a syntactic position etc. We
4885 ;; only check that if the limit is at or before bonl to save
4886 ;; time; it covers the by far most common case when font-lock
4887 ;; refontifies the current line only.
4888 (<= cfd-limit (c-point 'bonl cfd-start-pos))
4889 (save-excursion
4890 (goto-char cfd-start-pos)
4891 (while (progn
4892 (goto-char (next-single-property-change
4893 (point) 'face nil cfd-limit))
4894 (and (< (point) cfd-limit)
4895 (c-got-face-at (point) c-literal-faces))))
4896 (= (point) cfd-limit)))
4897
4898 ;; Completely inside a literal. Set up variables to trig the
4899 ;; (< cfd-continue-pos cfd-start-pos) case below and it'll
4900 ;; find a suitable start position.
4901 (setq cfd-continue-pos start-in-literal))
4902
4903 ;; Check if the region might be completely inside a macro, to
4904 ;; optimize that like the completely-inside-literal above.
4905 ((save-excursion
4906 (and (= (forward-line 1) 0)
4907 (bolp) ; forward-line has funny behavior at eob.
4908 (>= (point) cfd-limit)
4909 (progn (backward-char)
4910 (eq (char-before) ?\\))))
4911 ;; (Maybe) completely inside a macro. Only need to trig the
4912 ;; (< cfd-continue-pos cfd-start-pos) case below to make it
4913 ;; set things up.
4914 (setq cfd-continue-pos (1- cfd-start-pos)
4915 start-in-macro t))
d9e94c22 4916
0386b551
AM
4917 (t
4918 ;; Back out of any macro so we don't miss any declaration
4919 ;; that could follow after it.
4920 (when (c-beginning-of-macro)
4921 (setq start-in-macro t))
4922
4923 ;; Now we're at a proper syntactically relevant position so we
4924 ;; can use the cache. But first clear it if it applied
4925 ;; further down.
4926 (c-invalidate-find-decl-cache cfd-start-pos)
4927
4928 (setq syntactic-pos (point))
4929 (unless (eq syntactic-pos c-find-decl-syntactic-pos)
4930 ;; Don't have to do this if the cache is relevant here,
4931 ;; typically if the same line is refontified again. If
4932 ;; we're just some syntactic whitespace further down we can
4933 ;; still use the cache to limit the skipping.
4934 (c-backward-syntactic-ws c-find-decl-syntactic-pos))
4935
4936 ;; If we hit `c-find-decl-syntactic-pos' and
4937 ;; `c-find-decl-match-pos' is set then we install the cached
4938 ;; values. If we hit `c-find-decl-syntactic-pos' and
4939 ;; `c-find-decl-match-pos' is nil then we know there's no decl
4940 ;; prefix in the whitespace before `c-find-decl-syntactic-pos'
4941 ;; and so we can continue the search from this point. If we
4942 ;; didn't hit `c-find-decl-syntactic-pos' then we're now in
4943 ;; the right spot to begin searching anyway.
4944 (if (and (eq (point) c-find-decl-syntactic-pos)
4945 c-find-decl-match-pos)
d9e94c22
MS
4946 (setq cfd-match-pos c-find-decl-match-pos
4947 cfd-continue-pos syntactic-pos)
0386b551
AM
4948
4949 (setq c-find-decl-syntactic-pos syntactic-pos)
4950
4951 (when (if (bobp)
4952 ;; Always consider bob a match to get the first
4953 ;; declaration in the file. Do this separately instead of
4954 ;; letting `c-decl-prefix-or-start-re' match bob, so that
4955 ;; regexp always can consume at least one character to
4956 ;; ensure that we won't get stuck in an infinite loop.
4957 (setq cfd-re-match 0)
4958 (backward-char)
4959 (c-beginning-of-current-token)
4960 (< (point) cfd-limit))
4961 ;; Do an initial search now. In the bob case above it's
4962 ;; only done to search for a `c-decl-end' spot.
4963 (c-find-decl-prefix-search))
4964
4965 (setq c-find-decl-match-pos (and (< cfd-match-pos cfd-start-pos)
4966 cfd-match-pos)))))
4967
4968 ;; Advance `cfd-continue-pos' if it's before the start position.
4969 ;; The closest continue position that might have effect at or
4970 ;; after the start depends on what we started in. This also
4971 ;; finds a suitable start position in the special cases when the
4972 ;; region is completely within a literal or macro.
4973 (when (and cfd-continue-pos (< cfd-continue-pos cfd-start-pos))
4974
4975 (cond
4976 (start-in-macro
4977 ;; If we're in a macro then it's the closest preceding token
4978 ;; in the macro. Check this before `start-in-literal',
4979 ;; since if we're inside a literal in a macro, the preceding
4980 ;; token is earlier than any `c-decl-end' spot inside the
4981 ;; literal (comment).
4982 (goto-char (or start-in-literal cfd-start-pos))
4983 ;; The only syntactic ws in macros are comments.
d9e94c22 4984 (c-backward-comments)
0386b551
AM
4985 (backward-char)
4986 (c-beginning-of-current-token))
4987
4988 (start-in-literal
4989 ;; If we're in a comment it can only be the closest
4990 ;; preceding `c-decl-end' position within that comment, if
4991 ;; any. Go back to the beginning of such a property so that
4992 ;; `c-find-decl-prefix-search' will find the end of it.
4993 ;; (Can't stop at the end and install it directly on
4994 ;; `cfd-prop-match' since that variable might be cleared
4995 ;; after `cfd-fun' below.)
4996 ;;
4997 ;; Note that if the literal is a string then the property
4998 ;; search will simply skip to the beginning of it right
4999 ;; away.
5000 (if (not c-type-decl-end-used)
5001 (goto-char start-in-literal)
5002 (goto-char cfd-start-pos)
5003 (while (progn
5004 (goto-char (previous-single-property-change
5005 (point) 'c-type nil start-in-literal))
5006 (and (> (point) start-in-literal)
5007 (not (eq (c-get-char-property (point) 'c-type)
5008 'c-decl-end))))))
5009
5010 (when (= (point) start-in-literal)
5011 ;; Didn't find any property inside the comment, so we can
5012 ;; skip it entirely. (This won't skip past a string, but
5013 ;; that'll be handled quickly by the next
5014 ;; `c-find-decl-prefix-search' anyway.)
5015 (c-forward-single-comment)
5016 (if (> (point) cfd-limit)
5017 (goto-char cfd-limit))))
d9e94c22 5018
0386b551
AM
5019 (t
5020 ;; If we started in normal code, the only match that might
5021 ;; apply before the start is what we already got in
5022 ;; `cfd-match-pos' so we can continue at the start position.
5023 ;; (Note that we don't get here if the first match is below
5024 ;; it.)
5025 (goto-char cfd-start-pos)))
5026
5027 ;; Delete found matches if they are before our new continue
5028 ;; position, so that `c-find-decl-prefix-search' won't back up
5029 ;; to them later on.
5030 (setq cfd-continue-pos (point))
5031 (when (and cfd-re-match (< cfd-re-match cfd-continue-pos))
5032 (setq cfd-re-match nil))
5033 (when (and cfd-prop-match (< cfd-prop-match cfd-continue-pos))
5034 (setq cfd-prop-match nil)))
5035
5036 (if syntactic-pos
5037 ;; This is the normal case and we got a proper syntactic
5038 ;; position. If there's a match then it's always outside
5039 ;; macros and comments, so advance to the next token and set
5040 ;; `cfd-token-pos'. The loop below will later go back using
5041 ;; `cfd-continue-pos' to fix declarations inside the
5042 ;; syntactic ws.
5043 (when (and cfd-match-pos (< cfd-match-pos syntactic-pos))
5044 (goto-char syntactic-pos)
5045 (c-forward-syntactic-ws)
5046 (and cfd-continue-pos
5047 (< cfd-continue-pos (point))
5048 (setq cfd-token-pos (point))))
5049
5050 ;; Have one of the special cases when the region is completely
5051 ;; within a literal or macro. `cfd-continue-pos' is set to a
5052 ;; good start position for the search, so do it.
5053 (c-find-decl-prefix-search)))
d9e94c22 5054
51c9af45 5055 ;; Now loop. Round what? (ACM, 2006/7/5). We already got the first match.
d9e94c22
MS
5056
5057 (while (progn
5058 (while (and
5059 (< cfd-match-pos cfd-limit)
5060
5061 (or
5062 ;; Kludge to filter out matches on the "<" that
5063 ;; aren't open parens, for the sake of languages
5064 ;; that got `c-recognize-<>-arglists' set.
5065 (and (eq (char-before cfd-match-pos) ?<)
5066 (not (c-get-char-property (1- cfd-match-pos)
5067 'syntax-table)))
5068
5069 ;; If `cfd-continue-pos' is less or equal to
5070 ;; `cfd-token-pos', we've got a hit inside a macro
5071 ;; that's in the syntactic whitespace before the last
5072 ;; "real" declaration we've checked. If they're equal
5073 ;; we've arrived at the declaration a second time, so
5074 ;; there's nothing to do.
5075 (= cfd-continue-pos cfd-token-pos)
5076
5077 (progn
5078 ;; If `cfd-continue-pos' is less than `cfd-token-pos'
5079 ;; we're still searching for declarations embedded in
5080 ;; the syntactic whitespace. In that case we need
5081 ;; only to skip comments and not macros, since they
5082 ;; can't be nested, and that's already been done in
5083 ;; `c-find-decl-prefix-search'.
5084 (when (> cfd-continue-pos cfd-token-pos)
5085 (c-forward-syntactic-ws)
5086 (setq cfd-token-pos (point)))
5087
5088 ;; Continue if the following token fails the
5089 ;; CFD-DECL-RE and CFD-FACE-CHECKLIST checks.
5090 (when (or (>= (point) cfd-limit)
5091 (not (looking-at cfd-decl-re))
5092 (and cfd-face-checklist
5093 (not (c-got-face-at
5094 (point) cfd-face-checklist))))
5095 (goto-char cfd-continue-pos)
5096 t)))
5097
5098 (< (point) cfd-limit))
5099 (c-find-decl-prefix-search))
5100
5101 (< (point) cfd-limit))
5102
0386b551
AM
5103 (when (and
5104 (>= (point) cfd-start-pos)
d9e94c22 5105
0386b551
AM
5106 (progn
5107 ;; Narrow to the end of the macro if we got a hit inside
5108 ;; one, to avoid recognizing things that start inside the
5109 ;; macro and end outside it.
5110 (when (> cfd-match-pos cfd-macro-end)
5111 ;; Not in the same macro as in the previous round.
5112 (save-excursion
5113 (goto-char cfd-match-pos)
5114 (setq cfd-macro-end
5115 (if (save-excursion (and (c-beginning-of-macro)
5116 (< (point) cfd-match-pos)))
5117 (progn (c-end-of-macro)
5118 (point))
5119 0))))
5120
5121 (if (zerop cfd-macro-end)
5122 t
5123 (if (> cfd-macro-end (point))
5124 (progn (narrow-to-region (point-min) cfd-macro-end)
5125 t)
5126 ;; The matched token was the last thing in the macro,
5127 ;; so the whole match is bogus.
5128 (setq cfd-macro-end 0)
5129 nil))))
d9e94c22
MS
5130
5131 (c-debug-put-decl-spot-faces cfd-match-pos (point))
0386b551
AM
5132 (if (funcall cfd-fun cfd-match-pos (/= cfd-macro-end 0))
5133 (setq cfd-prop-match nil))
d9e94c22
MS
5134
5135 (when (/= cfd-macro-end 0)
e1dbe924 5136 ;; Restore limits if we did macro narrowing above.
d9e94c22
MS
5137 (narrow-to-region (point-min) cfd-buffer-end)))
5138
5139 (goto-char cfd-continue-pos)
5140 (if (= cfd-continue-pos cfd-limit)
5141 (setq cfd-match-pos cfd-limit)
9657183b
AM
5142 (c-find-decl-prefix-search))))) ; Moves point, sets cfd-continue-pos,
5143 ; cfd-match-pos, etc.
d9e94c22
MS
5144
5145\f
5146;; A cache for found types.
5147
5148;; Buffer local variable that contains an obarray with the types we've
5149;; found. If a declaration is recognized somewhere we record the
5150;; fully qualified identifier in it to recognize it as a type
5151;; elsewhere in the file too. This is not accurate since we do not
5152;; bother with the scoping rules of the languages, but in practice the
5153;; same name is seldom used as both a type and something else in a
5154;; file, and we only use this as a last resort in ambiguous cases (see
0386b551
AM
5155;; `c-forward-decl-or-cast-1').
5156;;
580fba94
AM
5157;; Not every type need be in this cache. However, things which have
5158;; ceased to be types must be removed from it.
5159;;
0386b551
AM
5160;; Template types in C++ are added here too but with the template
5161;; arglist replaced with "<>" in references or "<" for the one in the
5162;; primary type. E.g. the type "Foo<A,B>::Bar<C>" is stored as
5163;; "Foo<>::Bar<". This avoids storing very long strings (since C++
5164;; template specs can be fairly sized programs in themselves) and
5165;; improves the hit ratio (it's a type regardless of the template
5166;; args; it's just not the same type, but we're only interested in
5167;; recognizing types, not telling distinct types apart). Note that
5168;; template types in references are added here too; from the example
5169;; above there will also be an entry "Foo<".
d9e94c22
MS
5170(defvar c-found-types nil)
5171(make-variable-buffer-local 'c-found-types)
5172
5173(defsubst c-clear-found-types ()
5174 ;; Clears `c-found-types'.
d9e94c22
MS
5175 (setq c-found-types (make-vector 53 0)))
5176
5177(defun c-add-type (from to)
5178 ;; Add the given region as a type in `c-found-types'. If the region
5179 ;; doesn't match an existing type but there is a type which is equal
5180 ;; to the given one except that the last character is missing, then
5181 ;; the shorter type is removed. That's done to avoid adding all
5182 ;; prefixes of a type as it's being entered and font locked. This
5183 ;; doesn't cover cases like when characters are removed from a type
5184 ;; or added in the middle. We'd need the position of point when the
5185 ;; font locking is invoked to solve this well.
0386b551
AM
5186 ;;
5187 ;; This function might do hidden buffer changes.
5188 (let ((type (c-syntactic-content from to c-recognize-<>-arglists)))
5189 (unless (intern-soft type c-found-types)
5190 (unintern (substring type 0 -1) c-found-types)
5191 (intern type c-found-types))))
d9e94c22 5192
580fba94
AM
5193(defun c-unfind-type (name)
5194 ;; Remove the "NAME" from c-found-types, if present.
5195 (unintern name c-found-types))
5196
d9e94c22
MS
5197(defsubst c-check-type (from to)
5198 ;; Return non-nil if the given region contains a type in
5199 ;; `c-found-types'.
0386b551
AM
5200 ;;
5201 ;; This function might do hidden buffer changes.
5202 (intern-soft (c-syntactic-content from to c-recognize-<>-arglists)
5203 c-found-types))
d9e94c22
MS
5204
5205(defun c-list-found-types ()
5206 ;; Return all the types in `c-found-types' as a sorted list of
5207 ;; strings.
5208 (let (type-list)
5209 (mapatoms (lambda (type)
5210 (setq type-list (cons (symbol-name type)
5211 type-list)))
5212 c-found-types)
5213 (sort type-list 'string-lessp)))
a66cd3ee 5214
2f42c75f
DN
5215;; Shut up the byte compiler.
5216(defvar c-maybe-stale-found-type)
5217
580fba94
AM
5218(defun c-trim-found-types (beg end old-len)
5219 ;; An after change function which, in conjunction with the info in
5220 ;; c-maybe-stale-found-type (set in c-before-change), removes a type
5221 ;; from `c-found-types', should this type have become stale. For
5222 ;; example, this happens to "foo" when "foo \n bar();" becomes
5223 ;; "foo(); \n bar();". Such stale types, if not removed, foul up
5224 ;; the fontification.
b414f371 5225 ;;
580fba94
AM
5226 ;; Have we, perhaps, added non-ws characters to the front/back of a found
5227 ;; type?
5228 (when (> end beg)
5229 (save-excursion
5230 (when (< end (point-max))
5231 (goto-char end)
5232 (if (and (c-beginning-of-current-token) ; only moves when we started in the middle
5233 (progn (goto-char end)
5234 (c-end-of-current-token)))
5235 (c-unfind-type (buffer-substring-no-properties
5236 end (point)))))
5237 (when (> beg (point-min))
5238 (goto-char beg)
5239 (if (and (c-end-of-current-token) ; only moves when we started in the middle
5240 (progn (goto-char beg)
5241 (c-beginning-of-current-token)))
5242 (c-unfind-type (buffer-substring-no-properties
5243 (point) beg))))))
b414f371 5244
580fba94
AM
5245 (if c-maybe-stale-found-type ; e.g. (c-decl-id-start "foo" 97 107 " (* ooka) " "o")
5246 (cond
5247 ;; Changing the amount of (already existing) whitespace - don't do anything.
5248 ((and (c-partial-ws-p beg end)
5249 (or (= beg end) ; removal of WS
580fba94
AM
5250 (string-match "^[ \t\n\r\f\v]*$" (nth 5 c-maybe-stale-found-type)))))
5251
5252 ;; The syntactic relationship which defined a "found type" has been
5253 ;; destroyed.
5254 ((eq (car c-maybe-stale-found-type) 'c-decl-id-start)
5255 (c-unfind-type (cadr c-maybe-stale-found-type)))
5256;; ((eq (car c-maybe-stale-found-type) 'c-decl-type-start) FIXME!!!
5257 )))
5258
d9e94c22 5259\f
dd969a56
AM
5260;; Setting and removing syntax properties on < and > in languages (C++
5261;; and Java) where they can be template/generic delimiters as well as
5262;; their normal meaning of "less/greater than".
5263
5264;; Normally, < and > have syntax 'punctuation'. When they are found to
5265;; be delimiters, they are marked as such with the category properties
5266;; c-<-as-paren-syntax, c->-as-paren-syntax respectively.
5267
5268;; STRATEGY:
5269;;
5270;; It is impossible to determine with certainty whether a <..> pair in
5271;; C++ is two comparison operators or is template delimiters, unless
5272;; one duplicates a lot of a C++ compiler. For example, the following
5273;; code fragment:
5274;;
5275;; foo (a < b, c > d) ;
5276;;
5277;; could be a function call with two integer parameters (each a
5278;; relational expression), or it could be a constructor for class foo
5279;; taking one parameter d of templated type "a < b, c >". They are
5280;; somewhat easier to distinguish in Java.
5281;;
5282;; The strategy now (2010-01) adopted is to mark and unmark < and
5283;; > IN MATCHING PAIRS ONLY. [Previously, they were marked
5284;; individually when their context so indicated. This gave rise to
53964682 5285;; intractable problems when one of a matching pair was deleted, or
dd969a56
AM
5286;; pulled into a literal.]
5287;;
5288;; At each buffer change, the syntax-table properties are removed in a
5289;; before-change function and reapplied, when needed, in an
5290;; after-change function. It is far more important that the
5291;; properties get removed when they they are spurious than that they
5292;; be present when wanted.
5293;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
5294(defun c-clear-<-pair-props (&optional pos)
5295 ;; POS (default point) is at a < character. If it is marked with
5296 ;; open paren syntax-table text property, remove the property,
5297 ;; together with the close paren property on the matching > (if
5298 ;; any).
5299 (save-excursion
5300 (if pos
5301 (goto-char pos)
5302 (setq pos (point)))
5303 (when (equal (c-get-char-property (point) 'syntax-table)
5304 c-<-as-paren-syntax)
5305 (with-syntax-table c-no-parens-syntax-table ; ignore unbalanced [,{,(,..
5306 (c-go-list-forward))
5307 (when (equal (c-get-char-property (1- (point)) 'syntax-table)
5308 c->-as-paren-syntax) ; should always be true.
8a249abc
AM
5309 (c-clear-char-property (1- (point)) 'category))
5310 (c-clear-char-property pos 'category))))
dd969a56
AM
5311
5312(defun c-clear->-pair-props (&optional pos)
5313 ;; POS (default point) is at a > character. If it is marked with
5314 ;; close paren syntax-table property, remove the property, together
5315 ;; with the open paren property on the matching < (if any).
5316 (save-excursion
5317 (if pos
5318 (goto-char pos)
5319 (setq pos (point)))
5320 (when (equal (c-get-char-property (point) 'syntax-table)
5321 c->-as-paren-syntax)
5322 (with-syntax-table c-no-parens-syntax-table ; ignore unbalanced [,{,(,..
5323 (c-go-up-list-backward))
5324 (when (equal (c-get-char-property (point) 'syntax-table)
5325 c-<-as-paren-syntax) ; should always be true.
8a249abc
AM
5326 (c-clear-char-property (point) 'category))
5327 (c-clear-char-property pos 'category))))
dd969a56
AM
5328
5329(defun c-clear-<>-pair-props (&optional pos)
5330 ;; POS (default point) is at a < or > character. If it has an
5331 ;; open/close paren syntax-table property, remove this property both
5332 ;; from the current character and its partner (which will also be
5333 ;; thusly marked).
5334 (cond
5335 ((eq (char-after) ?\<)
5336 (c-clear-<-pair-props pos))
5337 ((eq (char-after) ?\>)
5338 (c-clear->-pair-props pos))
5339 (t (c-benign-error
5340 "c-clear-<>-pair-props called from wrong position"))))
5341
5342(defun c-clear-<-pair-props-if-match-after (lim &optional pos)
5343 ;; POS (default point) is at a < character. If it is both marked
5344 ;; with open/close paren syntax-table property, and has a matching >
5345 ;; (also marked) which is after LIM, remove the property both from
43a91810
AM
5346 ;; the current > and its partner. Return t when this happens, nil
5347 ;; when it doesn't.
dd969a56
AM
5348 (save-excursion
5349 (if pos
5350 (goto-char pos)
5351 (setq pos (point)))
5352 (when (equal (c-get-char-property (point) 'syntax-table)
5353 c-<-as-paren-syntax)
5354 (with-syntax-table c-no-parens-syntax-table ; ignore unbalanced [,{,(,..
5355 (c-go-list-forward))
5356 (when (and (>= (point) lim)
5357 (equal (c-get-char-property (1- (point)) 'syntax-table)
5358 c->-as-paren-syntax)) ; should always be true.
5359 (c-unmark-<->-as-paren (1- (point)))
43a91810
AM
5360 (c-unmark-<->-as-paren pos))
5361 t)))
dd969a56
AM
5362
5363(defun c-clear->-pair-props-if-match-before (lim &optional pos)
5364 ;; POS (default point) is at a > character. If it is both marked
5365 ;; with open/close paren syntax-table property, and has a matching <
5366 ;; (also marked) which is before LIM, remove the property both from
43a91810
AM
5367 ;; the current < and its partner. Return t when this happens, nil
5368 ;; when it doesn't.
dd969a56
AM
5369 (save-excursion
5370 (if pos
5371 (goto-char pos)
5372 (setq pos (point)))
5373 (when (equal (c-get-char-property (point) 'syntax-table)
5374 c->-as-paren-syntax)
5375 (with-syntax-table c-no-parens-syntax-table ; ignore unbalanced [,{,(,..
5376 (c-go-up-list-backward))
5377 (when (and (<= (point) lim)
5378 (equal (c-get-char-property (point) 'syntax-table)
5379 c-<-as-paren-syntax)) ; should always be true.
5380 (c-unmark-<->-as-paren (point))
43a91810
AM
5381 (c-unmark-<->-as-paren pos))
5382 t)))
dd969a56 5383
f3b554af
GM
5384;; Set by c-common-init in cc-mode.el.
5385(defvar c-new-BEG)
5386(defvar c-new-END)
5387
dd969a56
AM
5388(defun c-before-change-check-<>-operators (beg end)
5389 ;; Unmark certain pairs of "< .... >" which are currently marked as
5390 ;; template/generic delimiters. (This marking is via syntax-table
5391 ;; text properties).
5392 ;;
5393 ;; These pairs are those which are in the current "statement" (i.e.,
5394 ;; the region between the {, }, or ; before BEG and the one after
5395 ;; END), and which enclose any part of the interval (BEG END).
5396 ;;
5397 ;; Note that in C++ (?and Java), template/generic parens cannot
5398 ;; enclose a brace or semicolon, so we use these as bounds on the
5399 ;; region we must work on.
5400 ;;
5401 ;; This function is called from before-change-functions (via
5402 ;; c-get-state-before-change-functions). Thus the buffer is widened,
5403 ;; and point is undefined, both at entry and exit.
5404 ;;
5405 ;; FIXME!!! This routine ignores the possibility of macros entirely.
5406 ;; 2010-01-29.
5407 (save-excursion
5408 (let ((beg-lit-limits (progn (goto-char beg) (c-literal-limits)))
43a91810
AM
5409 (end-lit-limits (progn (goto-char end) (c-literal-limits)))
5410 new-beg new-end need-new-beg need-new-end)
dd969a56
AM
5411 ;; Locate the barrier before the changed region
5412 (goto-char (if beg-lit-limits (car beg-lit-limits) beg))
0de3da9f 5413 (c-syntactic-skip-backward "^;{}" (c-determine-limit 512))
43a91810 5414 (setq new-beg (point))
dd969a56
AM
5415
5416 ;; Remove the syntax-table properties from each pertinent <...> pair.
5417 ;; Firsly, the ones with the < before beg and > after beg.
5418 (while (c-search-forward-char-property 'category 'c-<-as-paren-syntax beg)
43a91810
AM
5419 (if (c-clear-<-pair-props-if-match-after beg (1- (point)))
5420 (setq need-new-beg t)))
dd969a56
AM
5421
5422 ;; Locate the barrier after END.
5423 (goto-char (if end-lit-limits (cdr end-lit-limits) end))
0de3da9f 5424 (c-syntactic-re-search-forward "[;{}]" (c-determine-+ve-limit 512) 'end)
43a91810 5425 (setq new-end (point))
dd969a56
AM
5426
5427 ;; Remove syntax-table properties from the remaining pertinent <...>
5428 ;; pairs, those with a > after end and < before end.
5429 (while (c-search-backward-char-property 'category 'c->-as-paren-syntax end)
43a91810
AM
5430 (if (c-clear->-pair-props-if-match-before end)
5431 (setq need-new-end t)))
5432
5433 ;; Extend the fontification region, if needed.
5434 (when need-new-beg
5435 (goto-char new-beg)
5436 (c-forward-syntactic-ws)
5437 (and (< (point) c-new-BEG) (setq c-new-BEG (point))))
5438
5439 (when need-new-end
5440 (and (> new-end c-new-END) (setq c-new-END new-end))))))
dd969a56
AM
5441
5442
d9e94c22 5443
0386b551
AM
5444(defun c-after-change-check-<>-operators (beg end)
5445 ;; This is called from `after-change-functions' when
5446 ;; c-recognize-<>-arglists' is set. It ensures that no "<" or ">"
5447 ;; chars with paren syntax become part of another operator like "<<"
5448 ;; or ">=".
5449 ;;
5450 ;; This function might do hidden buffer changes.
5451
cb694ab7
AM
5452 (save-excursion
5453 (goto-char beg)
5454 (when (or (looking-at "[<>]")
5455 (< (skip-chars-backward "<>") 0))
5456
0386b551 5457 (goto-char beg)
cb694ab7
AM
5458 (c-beginning-of-current-token)
5459 (when (and (< (point) beg)
5460 (looking-at c-<>-multichar-token-regexp)
5461 (< beg (setq beg (match-end 0))))
5462 (while (progn (skip-chars-forward "^<>" beg)
5463 (< (point) beg))
dd969a56 5464 (c-clear-<>-pair-props)
cb694ab7
AM
5465 (forward-char))))
5466
5467 (when (< beg end)
5468 (goto-char end)
0386b551
AM
5469 (when (or (looking-at "[<>]")
5470 (< (skip-chars-backward "<>") 0))
5471
cb694ab7 5472 (goto-char end)
0386b551 5473 (c-beginning-of-current-token)
cb694ab7 5474 (when (and (< (point) end)
0386b551 5475 (looking-at c-<>-multichar-token-regexp)
cb694ab7
AM
5476 (< end (setq end (match-end 0))))
5477 (while (progn (skip-chars-forward "^<>" end)
5478 (< (point) end))
dd969a56 5479 (c-clear-<>-pair-props)
cb694ab7 5480 (forward-char)))))))
d9e94c22 5481
dd969a56
AM
5482
5483\f
5484;; Handling of small scale constructs like types and names.
5485
d9e94c22
MS
5486;; Dynamically bound variable that instructs `c-forward-type' to also
5487;; treat possible types (i.e. those that it normally returns 'maybe or
5488;; 'found for) as actual types (and always return 'found for them).
5489;; This means that it records them in `c-record-type-identifiers' if
5490;; that is set, and that it adds them to `c-found-types'.
5491(defvar c-promote-possible-types nil)
5492
0386b551
AM
5493;; Dynamically bound variable that instructs `c-forward-<>-arglist' to
5494;; mark up successfully parsed arglists with paren syntax properties on
5495;; the surrounding angle brackets and with `c-<>-arg-sep' in the
5496;; `c-type' property of each argument separating comma.
5497;;
5498;; Setting this variable also makes `c-forward-<>-arglist' recurse into
5499;; all arglists for side effects (i.e. recording types), otherwise it
5500;; exploits any existing paren syntax properties to quickly jump to the
5501;; end of already parsed arglists.
5502;;
5503;; Marking up the arglists is not the default since doing that correctly
5504;; depends on a proper value for `c-restricted-<>-arglists'.
5505(defvar c-parse-and-markup-<>-arglists nil)
5506
d9e94c22 5507;; Dynamically bound variable that instructs `c-forward-<>-arglist' to
037558bf
MS
5508;; not accept arglists that contain binary operators.
5509;;
5510;; This is primarily used to handle C++ template arglists. C++
5511;; disambiguates them by checking whether the preceding name is a
5512;; template or not. We can't do that, so we assume it is a template
5513;; if it can be parsed as one. That usually works well since
5514;; comparison expressions on the forms "a < b > c" or "a < b, c > d"
5515;; in almost all cases would be pointless.
5516;;
5517;; However, in function arglists, e.g. in "foo (a < b, c > d)", we
5518;; should let the comma separate the function arguments instead. And
5519;; in a context where the value of the expression is taken, e.g. in
5520;; "if (a < b || c > d)", it's probably not a template.
5521(defvar c-restricted-<>-arglists nil)
d9e94c22 5522
0386b551
AM
5523;; Dynamically bound variables that instructs
5524;; `c-forward-keyword-clause', `c-forward-<>-arglist',
5525;; `c-forward-name', `c-forward-type', `c-forward-decl-or-cast-1', and
5526;; `c-forward-label' to record the ranges of all the type and
5527;; reference identifiers they encounter. They will build lists on
5528;; these variables where each element is a cons of the buffer
5529;; positions surrounding each identifier. This recording is only
5530;; activated when `c-record-type-identifiers' is non-nil.
d9e94c22
MS
5531;;
5532;; All known types that can't be identifiers are recorded, and also
5533;; other possible types if `c-promote-possible-types' is set.
5534;; Recording is however disabled inside angle bracket arglists that
5535;; are encountered inside names and other angle bracket arglists.
0386b551 5536;; Such occurrences are taken care of by `c-font-lock-<>-arglists'
d9e94c22
MS
5537;; instead.
5538;;
5539;; Only the names in C++ template style references (e.g. "tmpl" in
5540;; "tmpl<a,b>::foo") are recorded as references, other references
5541;; aren't handled here.
0386b551
AM
5542;;
5543;; `c-forward-label' records the label identifier(s) on
5544;; `c-record-ref-identifiers'.
d9e94c22
MS
5545(defvar c-record-type-identifiers nil)
5546(defvar c-record-ref-identifiers nil)
5547
0386b551
AM
5548;; This variable will receive a cons cell of the range of the last
5549;; single identifier symbol stepped over by `c-forward-name' if it's
5550;; successful. This is the range that should be put on one of the
5551;; record lists above by the caller. It's assigned nil if there's no
5552;; such symbol in the name.
d9e94c22
MS
5553(defvar c-last-identifier-range nil)
5554
5555(defmacro c-record-type-id (range)
5556 (if (eq (car-safe range) 'cons)
5557 ;; Always true.
5558 `(setq c-record-type-identifiers
5559 (cons ,range c-record-type-identifiers))
5560 `(let ((range ,range))
5561 (if range
5562 (setq c-record-type-identifiers
5563 (cons range c-record-type-identifiers))))))
5564
5565(defmacro c-record-ref-id (range)
5566 (if (eq (car-safe range) 'cons)
5567 ;; Always true.
5568 `(setq c-record-ref-identifiers
5569 (cons ,range c-record-ref-identifiers))
5570 `(let ((range ,range))
5571 (if range
5572 (setq c-record-ref-identifiers
5573 (cons range c-record-ref-identifiers))))))
5574
5575;; Dynamically bound variable that instructs `c-forward-type' to
5576;; record the ranges of types that only are found. Behaves otherwise
5577;; like `c-record-type-identifiers'.
5578(defvar c-record-found-types nil)
5579
5580(defmacro c-forward-keyword-prefixed-id (type)
5581 ;; Used internally in `c-forward-keyword-clause' to move forward
5582 ;; over a type (if TYPE is 'type) or a name (otherwise) which
5583 ;; possibly is prefixed by keywords and their associated clauses.
5584 ;; Try with a type/name first to not trip up on those that begin
5585 ;; with a keyword. Return t if a known or found type is moved
5586 ;; over. The point is clobbered if nil is returned. If range
5587 ;; recording is enabled, the identifier is recorded on as a type
5588 ;; if TYPE is 'type or as a reference if TYPE is 'ref.
0386b551
AM
5589 ;;
5590 ;; This macro might do hidden buffer changes.
d9e94c22
MS
5591 `(let (res)
5592 (while (if (setq res ,(if (eq type 'type)
5593 `(c-forward-type)
5594 `(c-forward-name)))
5595 nil
5596 (and (looking-at c-keywords-regexp)
0386b551 5597 (c-forward-keyword-clause 1))))
d9e94c22
MS
5598 (when (memq res '(t known found prefix))
5599 ,(when (eq type 'ref)
5600 `(when c-record-type-identifiers
5601 (c-record-ref-id c-last-identifier-range)))
5602 t)))
5603
0386b551 5604(defmacro c-forward-id-comma-list (type update-safe-pos)
d9e94c22
MS
5605 ;; Used internally in `c-forward-keyword-clause' to move forward
5606 ;; over a comma separated list of types or names using
5607 ;; `c-forward-keyword-prefixed-id'.
0386b551
AM
5608 ;;
5609 ;; This macro might do hidden buffer changes.
d9e94c22 5610 `(while (and (progn
0386b551
AM
5611 ,(when update-safe-pos
5612 `(setq safe-pos (point)))
d9e94c22
MS
5613 (eq (char-after) ?,))
5614 (progn
5615 (forward-char)
5616 (c-forward-syntactic-ws)
5617 (c-forward-keyword-prefixed-id ,type)))))
5618
0386b551
AM
5619(defun c-forward-keyword-clause (match)
5620 ;; Submatch MATCH in the current match data is assumed to surround a
5621 ;; token. If it's a keyword, move over it and any immediately
5622 ;; following clauses associated with it, stopping at the start of
5623 ;; the next token. t is returned in that case, otherwise the point
d9e94c22
MS
5624 ;; stays and nil is returned. The kind of clauses that are
5625 ;; recognized are those specified by `c-type-list-kwds',
5626 ;; `c-ref-list-kwds', `c-colon-type-list-kwds',
5627 ;; `c-paren-nontype-kwds', `c-paren-type-kwds', `c-<>-type-kwds',
5628 ;; and `c-<>-arglist-kwds'.
0386b551
AM
5629 ;;
5630 ;; This function records identifier ranges on
5631 ;; `c-record-type-identifiers' and `c-record-ref-identifiers' if
5632 ;; `c-record-type-identifiers' is non-nil.
5633 ;;
5634 ;; Note that for `c-colon-type-list-kwds', which doesn't necessary
5635 ;; apply directly after the keyword, the type list is moved over
5636 ;; only when there is no unaccounted token before it (i.e. a token
5637 ;; that isn't moved over due to some other keyword list). The
5638 ;; identifier ranges in the list are still recorded if that should
5639 ;; be done, though.
5640 ;;
5641 ;; This function might do hidden buffer changes.
5642
5643 (let ((kwd-sym (c-keyword-sym (match-string match))) safe-pos pos
5644 ;; The call to `c-forward-<>-arglist' below is made after
5645 ;; `c-<>-sexp-kwds' keywords, so we're certain they actually
5646 ;; are angle bracket arglists and `c-restricted-<>-arglists'
5647 ;; should therefore be nil.
5648 (c-parse-and-markup-<>-arglists t)
5649 c-restricted-<>-arglists)
d9e94c22 5650
d9e94c22 5651 (when kwd-sym
0386b551 5652 (goto-char (match-end match))
d9e94c22
MS
5653 (c-forward-syntactic-ws)
5654 (setq safe-pos (point))
a66cd3ee 5655
d9e94c22
MS
5656 (cond
5657 ((and (c-keyword-member kwd-sym 'c-type-list-kwds)
5658 (c-forward-keyword-prefixed-id type))
5659 ;; There's a type directly after a keyword in `c-type-list-kwds'.
0386b551 5660 (c-forward-id-comma-list type t))
d9e94c22
MS
5661
5662 ((and (c-keyword-member kwd-sym 'c-ref-list-kwds)
5663 (c-forward-keyword-prefixed-id ref))
5664 ;; There's a name directly after a keyword in `c-ref-list-kwds'.
0386b551 5665 (c-forward-id-comma-list ref t))
d9e94c22
MS
5666
5667 ((and (c-keyword-member kwd-sym 'c-paren-any-kwds)
5668 (eq (char-after) ?\())
5669 ;; There's an open paren after a keyword in `c-paren-any-kwds'.
5670
5671 (forward-char)
5672 (when (and (setq pos (c-up-list-forward))
5673 (eq (char-before pos) ?\)))
5674 (when (and c-record-type-identifiers
5675 (c-keyword-member kwd-sym 'c-paren-type-kwds))
5676 ;; Use `c-forward-type' on every identifier we can find
5677 ;; inside the paren, to record the types.
5678 (while (c-syntactic-re-search-forward c-symbol-start pos t)
5679 (goto-char (match-beginning 0))
5680 (unless (c-forward-type)
5681 (looking-at c-symbol-key) ; Always matches.
5682 (goto-char (match-end 0)))))
5683
5684 (goto-char pos)
5685 (c-forward-syntactic-ws)
5686 (setq safe-pos (point))))
5687
5688 ((and (c-keyword-member kwd-sym 'c-<>-sexp-kwds)
5689 (eq (char-after) ?<)
0386b551 5690 (c-forward-<>-arglist (c-keyword-member kwd-sym 'c-<>-type-kwds)))
d9e94c22
MS
5691 (c-forward-syntactic-ws)
5692 (setq safe-pos (point)))
5693
5694 ((and (c-keyword-member kwd-sym 'c-nonsymbol-sexp-kwds)
449a2b0d
MS
5695 (not (looking-at c-symbol-start))
5696 (c-safe (c-forward-sexp) t))
d9e94c22
MS
5697 (c-forward-syntactic-ws)
5698 (setq safe-pos (point))))
5699
0386b551
AM
5700 (when (c-keyword-member kwd-sym 'c-colon-type-list-kwds)
5701 (if (eq (char-after) ?:)
5702 ;; If we are at the colon already, we move over the type
5703 ;; list after it.
5704 (progn
5705 (forward-char)
5706 (c-forward-syntactic-ws)
5707 (when (c-forward-keyword-prefixed-id type)
5708 (c-forward-id-comma-list type t)))
5709 ;; Not at the colon, so stop here. But the identifier
5710 ;; ranges in the type list later on should still be
5711 ;; recorded.
5712 (and c-record-type-identifiers
5713 (progn
5714 ;; If a keyword matched both one of the types above and
5715 ;; this one, we match `c-colon-type-list-re' after the
5716 ;; clause matched above.
5717 (goto-char safe-pos)
5718 (looking-at c-colon-type-list-re))
5719 (progn
5720 (goto-char (match-end 0))
5721 (c-forward-syntactic-ws)
5722 (c-forward-keyword-prefixed-id type))
5723 ;; There's a type after the `c-colon-type-list-re' match
5724 ;; after a keyword in `c-colon-type-list-kwds'.
5725 (c-forward-id-comma-list type nil))))
d9e94c22
MS
5726
5727 (goto-char safe-pos)
5728 t)))
5729
f3b554af
GM
5730;; cc-mode requires cc-fonts.
5731(declare-function c-fontify-recorded-types-and-refs "cc-fonts" ())
5732
0386b551
AM
5733(defun c-forward-<>-arglist (all-types)
5734 ;; The point is assumed to be at a "<". Try to treat it as the open
b4dc7d98 5735 ;; paren of an angle bracket arglist and move forward to the
0386b551
AM
5736 ;; corresponding ">". If successful, the point is left after the
5737 ;; ">" and t is returned, otherwise the point isn't moved and nil is
d9e94c22
MS
5738 ;; returned. If ALL-TYPES is t then all encountered arguments in
5739 ;; the arglist that might be types are treated as found types.
5740 ;;
0386b551
AM
5741 ;; The variable `c-parse-and-markup-<>-arglists' controls how this
5742 ;; function handles text properties on the angle brackets and argument
5743 ;; separating commas.
d9e94c22 5744 ;;
0386b551
AM
5745 ;; `c-restricted-<>-arglists' controls how lenient the template
5746 ;; arglist recognition should be.
5747 ;;
5748 ;; This function records identifier ranges on
5749 ;; `c-record-type-identifiers' and `c-record-ref-identifiers' if
5750 ;; `c-record-type-identifiers' is non-nil.
5751 ;;
5752 ;; This function might do hidden buffer changes.
d9e94c22
MS
5753
5754 (let ((start (point))
5755 ;; If `c-record-type-identifiers' is set then activate
5756 ;; recording of any found types that constitute an argument in
5757 ;; the arglist.
abfc152b 5758 (c-record-found-types (if c-record-type-identifiers t)))
d9e94c22
MS
5759 (if (catch 'angle-bracket-arglist-escape
5760 (setq c-record-found-types
0386b551 5761 (c-forward-<>-arglist-recur all-types)))
d9e94c22
MS
5762 (progn
5763 (when (consp c-record-found-types)
5764 (setq c-record-type-identifiers
5765 ;; `nconc' doesn't mind that the tail of
5766 ;; `c-record-found-types' is t.
5767 (nconc c-record-found-types c-record-type-identifiers)))
452ea855 5768 (if (c-major-mode-is 'java-mode) (c-fontify-recorded-types-and-refs))
d9e94c22
MS
5769 t)
5770
5771 (goto-char start)
a66cd3ee 5772 nil)))
785eecbb 5773
0386b551 5774(defun c-forward-<>-arglist-recur (all-types)
d9e94c22 5775 ;; Recursive part of `c-forward-<>-arglist'.
0386b551
AM
5776 ;;
5777 ;; This function might do hidden buffer changes.
d9e94c22
MS
5778
5779 (let ((start (point)) res pos tmp
5780 ;; Cover this so that any recorded found type ranges are
5781 ;; automatically lost if it turns out to not be an angle
5782 ;; bracket arglist. It's propagated through the return value
5783 ;; on successful completion.
5784 (c-record-found-types c-record-found-types)
5785 ;; List that collects the positions after the argument
5786 ;; separating ',' in the arglist.
5787 arg-start-pos)
0386b551
AM
5788 ;; If the '<' has paren open syntax then we've marked it as an angle
5789 ;; bracket arglist before, so skip to the end.
5790 (if (and (not c-parse-and-markup-<>-arglists)
5791 (c-get-char-property (point) 'syntax-table))
5792
5793 (progn
5794 (forward-char)
5795 (if (and (c-go-up-list-forward)
5796 (eq (char-before) ?>))
5797 t
0386b551
AM
5798 ;; Got unmatched paren angle brackets. We don't clear the paren
5799 ;; syntax properties and retry, on the basis that it's very
5800 ;; unlikely that paren angle brackets become operators by code
5801 ;; manipulation. It's far more likely that it doesn't match due
5802 ;; to narrowing or some temporary change.
5803 (goto-char start)
5804 nil))
d9e94c22 5805
abfc152b 5806 (forward-char) ; Forward over the opening '<'.
452ea855 5807
d9e94c22 5808 (unless (looking-at c-<-op-cont-regexp)
abfc152b
AM
5809 ;; go forward one non-alphanumeric character (group) per iteration of
5810 ;; this loop.
22c3ce97 5811 (while (and
d9e94c22 5812 (progn
22c3ce97
AM
5813 (c-forward-syntactic-ws)
5814 (let ((orig-record-found-types c-record-found-types))
5815 (when (or (and c-record-type-identifiers all-types)
5816 (c-major-mode-is 'java-mode))
5817 ;; All encountered identifiers are types, so set the
5818 ;; promote flag and parse the type.
5819 (progn
5820 (c-forward-syntactic-ws)
5821 (if (looking-at "\\?")
5822 (forward-char)
5823 (when (looking-at c-identifier-start)
5824 (let ((c-promote-possible-types t)
5825 (c-record-found-types t))
5826 (c-forward-type))))
5827
5828 (c-forward-syntactic-ws)
5829
5830 (when (or (looking-at "extends")
5831 (looking-at "super"))
5832 (forward-word)
5833 (c-forward-syntactic-ws)
5834 (let ((c-promote-possible-types t)
5835 (c-record-found-types t))
5836 (c-forward-type)
5837 (c-forward-syntactic-ws))))))
5838
abfc152b 5839 (setq pos (point)) ; e.g. first token inside the '<'
22c3ce97 5840
a4ee83cc
AM
5841 ;; Note: These regexps exploit the match order in \| so
5842 ;; that "<>" is matched by "<" rather than "[^>:-]>".
5843 (c-syntactic-re-search-forward
5844 ;; Stop on ',', '|', '&', '+' and '-' to catch
5845 ;; common binary operators that could be between
5846 ;; two comparison expressions "a<b" and "c>d".
5847 "[<;{},|+&-]\\|[>)]"
5848 nil t t))
22c3ce97
AM
5849
5850 (cond
5851 ((eq (char-before) ?>)
d9e94c22
MS
5852 ;; Either an operator starting with '>' or the end of
5853 ;; the angle bracket arglist.
5854
0386b551 5855 (if (looking-at c->-op-cont-regexp)
d9e94c22 5856 (progn
0386b551
AM
5857 (goto-char (match-end 0))
5858 t) ; Continue the loop.
d9e94c22 5859
0386b551
AM
5860 ;; The angle bracket arglist is finished.
5861 (when c-parse-and-markup-<>-arglists
d9e94c22 5862 (while arg-start-pos
0386b551
AM
5863 (c-put-c-type-property (1- (car arg-start-pos))
5864 'c-<>-arg-sep)
d9e94c22
MS
5865 (setq arg-start-pos (cdr arg-start-pos)))
5866 (c-mark-<-as-paren start)
0386b551
AM
5867 (c-mark->-as-paren (1- (point))))
5868 (setq res t)
5869 nil)) ; Exit the loop.
d9e94c22
MS
5870
5871 ((eq (char-before) ?<)
5872 ;; Either an operator starting with '<' or a nested arglist.
d9e94c22
MS
5873 (setq pos (point))
5874 (let (id-start id-end subres keyword-match)
abfc152b
AM
5875 (cond
5876 ;; The '<' begins a multi-char operator.
5877 ((looking-at c-<-op-cont-regexp)
5878 (setq tmp (match-end 0))
5879 (goto-char (match-end 0)))
5880 ;; We're at a nested <.....>
5881 ((progn
5882 (setq tmp pos)
5883 (backward-char) ; to the '<'
5884 (and
5885 (save-excursion
5886 ;; There's always an identifier before an angle
5887 ;; bracket arglist, or a keyword in `c-<>-type-kwds'
5888 ;; or `c-<>-arglist-kwds'.
5889 (c-backward-syntactic-ws)
5890 (setq id-end (point))
5891 (c-simple-skip-symbol-backward)
5892 (when (or (setq keyword-match
5893 (looking-at c-opt-<>-sexp-key))
5894 (not (looking-at c-keywords-regexp)))
5895 (setq id-start (point))))
5896 (setq subres
5897 (let ((c-promote-possible-types t)
5898 (c-record-found-types t))
5899 (c-forward-<>-arglist-recur
5900 (and keyword-match
5901 (c-keyword-member
5902 (c-keyword-sym (match-string 1))
5903 'c-<>-type-kwds)))))))
d9e94c22
MS
5904
5905 ;; It was an angle bracket arglist.
5906 (setq c-record-found-types subres)
5907
5908 ;; Record the identifier before the template as a type
5909 ;; or reference depending on whether the arglist is last
5910 ;; in a qualified identifier.
5911 (when (and c-record-type-identifiers
5912 (not keyword-match))
5913 (if (and c-opt-identifier-concat-key
5914 (progn
5915 (c-forward-syntactic-ws)
5916 (looking-at c-opt-identifier-concat-key)))
5917 (c-record-ref-id (cons id-start id-end))
abfc152b
AM
5918 (c-record-type-id (cons id-start id-end)))))
5919
5920 ;; At a "less than" operator.
5921 (t
5922 (forward-char)
5923 )))
5924 t) ; carry on looping.
22c3ce97
AM
5925
5926 ((and (not c-restricted-<>-arglists)
5927 (or (and (eq (char-before) ?&)
5928 (not (eq (char-after) ?&)))
5929 (eq (char-before) ?,)))
5930 ;; Just another argument. Record the position. The
5931 ;; type check stuff that made us stop at it is at
5932 ;; the top of the loop.
d9e94c22
MS
5933 (setq arg-start-pos (cons (point) arg-start-pos)))
5934
5935 (t
5936 ;; Got a character that can't be in an angle bracket
5937 ;; arglist argument. Abort using `throw', since
5938 ;; it's useless to try to find a surrounding arglist
5939 ;; if we're nested.
5940 (throw 'angle-bracket-arglist-escape nil))))))
d9e94c22
MS
5941 (if res
5942 (or c-record-found-types t)))))
5943
0386b551
AM
5944(defun c-backward-<>-arglist (all-types &optional limit)
5945 ;; The point is assumed to be directly after a ">". Try to treat it
5946 ;; as the close paren of an angle bracket arglist and move back to
5947 ;; the corresponding "<". If successful, the point is left at
5948 ;; the "<" and t is returned, otherwise the point isn't moved and
5949 ;; nil is returned. ALL-TYPES is passed on to
5950 ;; `c-forward-<>-arglist'.
5951 ;;
5952 ;; If the optional LIMIT is given, it bounds the backward search.
5953 ;; It's then assumed to be at a syntactically relevant position.
5954 ;;
5955 ;; This is a wrapper around `c-forward-<>-arglist'. See that
5956 ;; function for more details.
5957
5958 (let ((start (point)))
5959 (backward-char)
5960 (if (and (not c-parse-and-markup-<>-arglists)
5961 (c-get-char-property (point) 'syntax-table))
5962
5963 (if (and (c-go-up-list-backward)
5964 (eq (char-after) ?<))
5965 t
5966 ;; See corresponding note in `c-forward-<>-arglist'.
5967 (goto-char start)
5968 nil)
5969
51c9af45 5970 (while (progn
0386b551
AM
5971 (c-syntactic-skip-backward "^<;{}" limit t)
5972
51c9af45
AM
5973 (and
5974 (if (eq (char-before) ?<)
5975 t
5976 ;; Stopped at bob or a char that isn't allowed in an
5977 ;; arglist, so we've failed.
5978 (goto-char start)
5979 nil)
0386b551 5980
51c9af45
AM
5981 (if (> (point)
5982 (progn (c-beginning-of-current-token)
5983 (point)))
5984 ;; If we moved then the "<" was part of some
5985 ;; multicharacter token.
5986 t
0386b551 5987
51c9af45
AM
5988 (backward-char)
5989 (let ((beg-pos (point)))
5990 (if (c-forward-<>-arglist all-types)
5991 (cond ((= (point) start)
5992 ;; Matched the arglist. Break the while.
5993 (goto-char beg-pos)
5994 nil)
5995 ((> (point) start)
5996 ;; We started from a non-paren ">" inside an
5997 ;; arglist.
5998 (goto-char start)
5999 nil)
6000 (t
6001 ;; Matched a shorter arglist. Can be a nested
6002 ;; one so continue looking.
6003 (goto-char beg-pos)
6004 t))
6005 t))))))
0386b551
AM
6006
6007 (/= (point) start))))
6008
d9e94c22
MS
6009(defun c-forward-name ()
6010 ;; Move forward over a complete name if at the beginning of one,
e15f8aaa
AM
6011 ;; stopping at the next following token. A keyword, as such,
6012 ;; doesn't count as a name. If the point is not at something that
6013 ;; is recognized as a name then it stays put.
6014 ;;
6015 ;; A name could be something as simple as "foo" in C or something as
d9e94c22
MS
6016 ;; complex as "X<Y<class A<int>::B, BIT_MAX >> b>, ::operator<> ::
6017 ;; Z<(a>b)> :: operator const X<&foo>::T Q::G<unsigned short
6018 ;; int>::*volatile const" in C++ (this function is actually little
6019 ;; more than a `looking-at' call in all modes except those that,
e15f8aaa
AM
6020 ;; like C++, have `c-recognize-<>-arglists' set).
6021 ;;
6022 ;; Return
6023 ;; o - nil if no name is found;
6024 ;; o - 'template if it's an identifier ending with an angle bracket
6025 ;; arglist;
6026 ;; o - 'operator of it's an operator identifier;
6027 ;; o - t if it's some other kind of name.
0386b551
AM
6028 ;;
6029 ;; This function records identifier ranges on
6030 ;; `c-record-type-identifiers' and `c-record-ref-identifiers' if
6031 ;; `c-record-type-identifiers' is non-nil.
6032 ;;
6033 ;; This function might do hidden buffer changes.
d9e94c22 6034
0386b551 6035 (let ((pos (point)) (start (point)) res id-start id-end
d9e94c22
MS
6036 ;; Turn off `c-promote-possible-types' here since we might
6037 ;; call `c-forward-<>-arglist' and we don't want it to promote
6038 ;; every suspect thing in the arglist to a type. We're
6039 ;; typically called from `c-forward-type' in this case, and
6040 ;; the caller only wants the top level type that it finds to
6041 ;; be promoted.
6042 c-promote-possible-types)
6043 (while
6044 (and
6045 (looking-at c-identifier-key)
6046
6047 (progn
6048 ;; Check for keyword. We go to the last symbol in
6049 ;; `c-identifier-key' first.
0386b551
AM
6050 (goto-char (setq id-end (match-end 0)))
6051 (c-simple-skip-symbol-backward)
6052 (setq id-start (point))
d9e94c22
MS
6053
6054 (if (looking-at c-keywords-regexp)
6055 (when (and (c-major-mode-is 'c++-mode)
6056 (looking-at
6057 (cc-eval-when-compile
6058 (concat "\\(operator\\|\\(template\\)\\)"
6059 "\\(" (c-lang-const c-nonsymbol-key c++)
6060 "\\|$\\)")))
6061 (if (match-beginning 2)
6062 ;; "template" is only valid inside an
6063 ;; identifier if preceded by "::".
6064 (save-excursion
6065 (c-backward-syntactic-ws)
6066 (and (c-safe (backward-char 2) t)
6067 (looking-at "::")))
6068 t))
6069
6070 ;; Handle a C++ operator or template identifier.
6071 (goto-char id-end)
6072 (c-forward-syntactic-ws)
6073 (cond ((eq (char-before id-end) ?e)
6074 ;; Got "... ::template".
6075 (let ((subres (c-forward-name)))
6076 (when subres
6077 (setq pos (point)
6078 res subres))))
6079
6080 ((looking-at c-identifier-start)
6081 ;; Got a cast operator.
6082 (when (c-forward-type)
6083 (setq pos (point)
6084 res 'operator)
6085 ;; Now we should match a sequence of either
6086 ;; '*', '&' or a name followed by ":: *",
6087 ;; where each can be followed by a sequence
6088 ;; of `c-opt-type-modifier-key'.
6089 (while (cond ((looking-at "[*&]")
6090 (goto-char (match-end 0))
6091 t)
6092 ((looking-at c-identifier-start)
6093 (and (c-forward-name)
6094 (looking-at "::")
6095 (progn
6096 (goto-char (match-end 0))
6097 (c-forward-syntactic-ws)
6098 (eq (char-after) ?*))
6099 (progn
6100 (forward-char)
6101 t))))
6102 (while (progn
6103 (c-forward-syntactic-ws)
6104 (setq pos (point))
6105 (looking-at c-opt-type-modifier-key))
6106 (goto-char (match-end 1))))))
6107
6108 ((looking-at c-overloadable-operators-regexp)
6109 ;; Got some other operator.
0386b551
AM
6110 (setq c-last-identifier-range
6111 (cons (point) (match-end 0)))
d9e94c22
MS
6112 (goto-char (match-end 0))
6113 (c-forward-syntactic-ws)
6114 (setq pos (point)
6115 res 'operator)))
6116
6117 nil)
6118
0386b551
AM
6119 ;; `id-start' is equal to `id-end' if we've jumped over
6120 ;; an identifier that doesn't end with a symbol token.
6121 ;; That can occur e.g. for Java import directives on the
6122 ;; form "foo.bar.*".
6123 (when (and id-start (/= id-start id-end))
d9e94c22
MS
6124 (setq c-last-identifier-range
6125 (cons id-start id-end)))
6126 (goto-char id-end)
6127 (c-forward-syntactic-ws)
6128 (setq pos (point)
6129 res t)))
6130
6131 (progn
6132 (goto-char pos)
6133 (when (or c-opt-identifier-concat-key
6134 c-recognize-<>-arglists)
6135
6136 (cond
6137 ((and c-opt-identifier-concat-key
6138 (looking-at c-opt-identifier-concat-key))
6139 ;; Got a concatenated identifier. This handles the
6140 ;; cases with tricky syntactic whitespace that aren't
6141 ;; covered in `c-identifier-key'.
6142 (goto-char (match-end 0))
6143 (c-forward-syntactic-ws)
6144 t)
6145
6146 ((and c-recognize-<>-arglists
6147 (eq (char-after) ?<))
6148 ;; Maybe an angle bracket arglist.
452ea855
AM
6149 (when (let ((c-record-type-identifiers t)
6150 (c-record-found-types t))
0386b551
AM
6151 (c-forward-<>-arglist nil))
6152
6153 (c-add-type start (1+ pos))
d9e94c22 6154 (c-forward-syntactic-ws)
0386b551
AM
6155 (setq pos (point)
6156 c-last-identifier-range nil)
6157
d9e94c22
MS
6158 (if (and c-opt-identifier-concat-key
6159 (looking-at c-opt-identifier-concat-key))
0386b551 6160
d9e94c22
MS
6161 ;; Continue if there's an identifier concatenation
6162 ;; operator after the template argument.
6163 (progn
0386b551
AM
6164 (when (and c-record-type-identifiers id-start)
6165 (c-record-ref-id (cons id-start id-end)))
d9e94c22
MS
6166 (forward-char 2)
6167 (c-forward-syntactic-ws)
6168 t)
0386b551
AM
6169
6170 (when (and c-record-type-identifiers id-start)
6171 (c-record-type-id (cons id-start id-end)))
d9e94c22
MS
6172 (setq res 'template)
6173 nil)))
6174 )))))
6175
6176 (goto-char pos)
6177 res))
6178
e15f8aaa 6179(defun c-forward-type (&optional brace-block-too)
d9e94c22 6180 ;; Move forward over a type spec if at the beginning of one,
e15f8aaa
AM
6181 ;; stopping at the next following token. The keyword "typedef"
6182 ;; isn't part of a type spec here.
6183 ;;
6184 ;; BRACE-BLOCK-TOO, when non-nil, means move over the brace block in
6185 ;; constructs like "struct foo {...} bar ;" or "struct {...} bar;".
6186 ;; The current (2009-03-10) intention is to convert all uses of
6187 ;; `c-forward-type' to call with this parameter set, then to
6188 ;; eliminate it.
6189 ;;
6190 ;; Return
6191 ;; o - t if it's a known type that can't be a name or other
6192 ;; expression;
6193 ;; o - 'known if it's an otherwise known type (according to
6194 ;; `*-font-lock-extra-types');
6195 ;; o - 'prefix if it's a known prefix of a type;
6196 ;; o - 'found if it's a type that matches one in `c-found-types';
53964682 6197 ;; o - 'maybe if it's an identifier that might be a type; or
e15f8aaa
AM
6198 ;; o - nil if it can't be a type (the point isn't moved then).
6199 ;;
6200 ;; The point is assumed to be at the beginning of a token.
d9e94c22
MS
6201 ;;
6202 ;; Note that this function doesn't skip past the brace definition
6203 ;; that might be considered part of the type, e.g.
6204 ;; "enum {a, b, c} foo".
0386b551
AM
6205 ;;
6206 ;; This function records identifier ranges on
6207 ;; `c-record-type-identifiers' and `c-record-ref-identifiers' if
6208 ;; `c-record-type-identifiers' is non-nil.
6209 ;;
6210 ;; This function might do hidden buffer changes.
0e5cf2b8
AM
6211 (when (and c-recognize-<>-arglists
6212 (looking-at "<"))
452ea855
AM
6213 (c-forward-<>-arglist t)
6214 (c-forward-syntactic-ws))
0386b551
AM
6215
6216 (let ((start (point)) pos res name-res id-start id-end id-range)
d9e94c22
MS
6217
6218 ;; Skip leading type modifiers. If any are found we know it's a
6219 ;; prefix of a type.
e15f8aaa 6220 (when c-opt-type-modifier-key ; e.g. "const" "volatile", but NOT "typedef"
d9e94c22
MS
6221 (while (looking-at c-opt-type-modifier-key)
6222 (goto-char (match-end 1))
6223 (c-forward-syntactic-ws)
6224 (setq res 'prefix)))
6225
6226 (cond
e15f8aaa
AM
6227 ((looking-at c-type-prefix-key) ; e.g. "struct", "class", but NOT
6228 ; "typedef".
d9e94c22
MS
6229 (goto-char (match-end 1))
6230 (c-forward-syntactic-ws)
6231 (setq pos (point))
e15f8aaa
AM
6232
6233 (setq name-res (c-forward-name))
6234 (setq res (not (null name-res)))
6235 (when (eq name-res t)
6236 ;; In many languages the name can be used without the
6237 ;; prefix, so we add it to `c-found-types'.
6238 (c-add-type pos (point))
6239 (when (and c-record-type-identifiers
6240 c-last-identifier-range)
6241 (c-record-type-id c-last-identifier-range)))
6242 (when (and brace-block-too
6243 (memq res '(t nil))
6244 (eq (char-after) ?\{)
6245 (save-excursion
6246 (c-safe
6247 (progn (c-forward-sexp)
6248 (c-forward-syntactic-ws)
6249 (setq pos (point))))))
6250 (goto-char pos)
6251 (setq res t))
6252 (unless res (goto-char start))) ; invalid syntax
d9e94c22
MS
6253
6254 ((progn
6255 (setq pos nil)
6256 (if (looking-at c-identifier-start)
6257 (save-excursion
6258 (setq id-start (point)
0386b551
AM
6259 name-res (c-forward-name))
6260 (when name-res
d9e94c22
MS
6261 (setq id-end (point)
6262 id-range c-last-identifier-range))))
6263 (and (cond ((looking-at c-primitive-type-key)
6264 (setq res t))
6265 ((c-with-syntax-table c-identifier-syntax-table
6266 (looking-at c-known-type-key))
6267 (setq res 'known)))
6268 (or (not id-end)
6269 (>= (save-excursion
6270 (save-match-data
6271 (goto-char (match-end 1))
6272 (c-forward-syntactic-ws)
6273 (setq pos (point))))
6274 id-end)
6275 (setq res nil))))
6276 ;; Looking at a primitive or known type identifier. We've
6277 ;; checked for a name first so that we don't go here if the
6278 ;; known type match only is a prefix of another name.
6279
6280 (setq id-end (match-end 1))
6281
6282 (when (and c-record-type-identifiers
6283 (or c-promote-possible-types (eq res t)))
6284 (c-record-type-id (cons (match-beginning 1) (match-end 1))))
6285
6286 (if (and c-opt-type-component-key
6287 (save-match-data
6288 (looking-at c-opt-type-component-key)))
6289 ;; There might be more keywords for the type.
6290 (let (safe-pos)
0386b551 6291 (c-forward-keyword-clause 1)
d9e94c22
MS
6292 (while (progn
6293 (setq safe-pos (point))
6294 (looking-at c-opt-type-component-key))
6295 (when (and c-record-type-identifiers
6296 (looking-at c-primitive-type-key))
6297 (c-record-type-id (cons (match-beginning 1)
6298 (match-end 1))))
0386b551 6299 (c-forward-keyword-clause 1))
d9e94c22
MS
6300 (if (looking-at c-primitive-type-key)
6301 (progn
6302 (when c-record-type-identifiers
6303 (c-record-type-id (cons (match-beginning 1)
6304 (match-end 1))))
0386b551 6305 (c-forward-keyword-clause 1)
d9e94c22
MS
6306 (setq res t))
6307 (goto-char safe-pos)
6308 (setq res 'prefix)))
0386b551 6309 (unless (save-match-data (c-forward-keyword-clause 1))
d9e94c22
MS
6310 (if pos
6311 (goto-char pos)
6312 (goto-char (match-end 1))
6313 (c-forward-syntactic-ws)))))
6314
0386b551
AM
6315 (name-res
6316 (cond ((eq name-res t)
d9e94c22
MS
6317 ;; A normal identifier.
6318 (goto-char id-end)
6319 (if (or res c-promote-possible-types)
6320 (progn
6321 (c-add-type id-start id-end)
0386b551 6322 (when (and c-record-type-identifiers id-range)
d9e94c22
MS
6323 (c-record-type-id id-range))
6324 (unless res
6325 (setq res 'found)))
6326 (setq res (if (c-check-type id-start id-end)
6327 ;; It's an identifier that has been used as
6328 ;; a type somewhere else.
6329 'found
6330 ;; It's an identifier that might be a type.
6331 'maybe))))
0386b551 6332 ((eq name-res 'template)
d9e94c22
MS
6333 ;; A template is a type.
6334 (goto-char id-end)
6335 (setq res t))
6336 (t
6337 ;; Otherwise it's an operator identifier, which is not a type.
6338 (goto-char start)
6339 (setq res nil)))))
6340
6341 (when res
e15f8aaa 6342 ;; Skip trailing type modifiers. If any are found we know it's
d9e94c22
MS
6343 ;; a type.
6344 (when c-opt-type-modifier-key
e15f8aaa 6345 (while (looking-at c-opt-type-modifier-key) ; e.g. "const", "volatile"
d9e94c22
MS
6346 (goto-char (match-end 1))
6347 (c-forward-syntactic-ws)
6348 (setq res t)))
d9e94c22
MS
6349 ;; Step over any type suffix operator. Do not let the existence
6350 ;; of these alter the classification of the found type, since
6351 ;; these operators typically are allowed in normal expressions
6352 ;; too.
6353 (when c-opt-type-suffix-key
6354 (while (looking-at c-opt-type-suffix-key)
6355 (goto-char (match-end 1))
6356 (c-forward-syntactic-ws)))
6357
e15f8aaa 6358 (when c-opt-type-concat-key ; Only/mainly for pike.
0386b551
AM
6359 ;; Look for a trailing operator that concatenates the type
6360 ;; with a following one, and if so step past that one through
6361 ;; a recursive call. Note that we don't record concatenated
6362 ;; types in `c-found-types' - it's the component types that
6363 ;; are recorded when appropriate.
d9e94c22
MS
6364 (setq pos (point))
6365 (let* ((c-promote-possible-types (or (memq res '(t known))
6366 c-promote-possible-types))
6367 ;; If we can't promote then set `c-record-found-types' so that
6368 ;; we can merge in the types from the second part afterwards if
6369 ;; it turns out to be a known type there.
6370 (c-record-found-types (and c-record-type-identifiers
0386b551
AM
6371 (not c-promote-possible-types)))
6372 subres)
d9e94c22
MS
6373 (if (and (looking-at c-opt-type-concat-key)
6374
6375 (progn
6376 (goto-char (match-end 1))
6377 (c-forward-syntactic-ws)
0386b551 6378 (setq subres (c-forward-type))))
d9e94c22
MS
6379
6380 (progn
6381 ;; If either operand certainly is a type then both are, but we
6382 ;; don't let the existence of the operator itself promote two
6383 ;; uncertain types to a certain one.
6384 (cond ((eq res t))
0386b551
AM
6385 ((eq subres t)
6386 (unless (eq name-res 'template)
6387 (c-add-type id-start id-end))
6388 (when (and c-record-type-identifiers id-range)
d9e94c22
MS
6389 (c-record-type-id id-range))
6390 (setq res t))
2a15eb73 6391 ((eq res 'known))
0386b551 6392 ((eq subres 'known)
2a15eb73 6393 (setq res 'known))
d9e94c22 6394 ((eq res 'found))
0386b551 6395 ((eq subres 'found)
d9e94c22
MS
6396 (setq res 'found))
6397 (t
6398 (setq res 'maybe)))
6399
6400 (when (and (eq res t)
6401 (consp c-record-found-types))
6402 ;; Merge in the ranges of any types found by the second
6403 ;; `c-forward-type'.
6404 (setq c-record-type-identifiers
6405 ;; `nconc' doesn't mind that the tail of
6406 ;; `c-record-found-types' is t.
6407 (nconc c-record-found-types
6408 c-record-type-identifiers))))
6409
6410 (goto-char pos))))
6411
6412 (when (and c-record-found-types (memq res '(known found)) id-range)
6413 (setq c-record-found-types
6414 (cons id-range c-record-found-types))))
6415
6416 ;;(message "c-forward-type %s -> %s: %s" start (point) res)
6417
6418 res))
6419
452ea855
AM
6420(defun c-forward-annotation ()
6421 ;; Used for Java code only at the moment. Assumes point is on the
6422 ;; @, moves forward an annotation. returns nil if there is no
6423 ;; annotation at point.
6424 (and (looking-at "@")
6425 (progn (forward-char) t)
6426 (c-forward-type)
6427 (progn (c-forward-syntactic-ws) t)
6428 (if (looking-at "(")
6429 (c-go-list-forward)
6430 t)))
6431
785eecbb 6432\f
d9e94c22
MS
6433;; Handling of large scale constructs like statements and declarations.
6434
0386b551
AM
6435;; Macro used inside `c-forward-decl-or-cast-1'. It ought to be a
6436;; defsubst or perhaps even a defun, but it contains lots of free
6437;; variables that refer to things inside `c-forward-decl-or-cast-1'.
6438(defmacro c-fdoc-shift-type-backward (&optional short)
6439 ;; `c-forward-decl-or-cast-1' can consume an arbitrary length list
6440 ;; of types when parsing a declaration, which means that it
6441 ;; sometimes consumes the identifier in the declaration as a type.
6442 ;; This is used to "backtrack" and make the last type be treated as
6443 ;; an identifier instead.
6444 `(progn
6445 ,(unless short
6446 ;; These identifiers are bound only in the inner let.
6447 '(setq identifier-type at-type
6448 identifier-start type-start
6449 got-parens nil
6450 got-identifier t
6451 got-suffix t
6452 got-suffix-after-parens id-start
6453 paren-depth 0))
6454
6455 (if (setq at-type (if (eq backup-at-type 'prefix)
6456 t
6457 backup-at-type))
6458 (setq type-start backup-type-start
6459 id-start backup-id-start)
6460 (setq type-start start-pos
6461 id-start start-pos))
6462
6463 ;; When these flags already are set we've found specifiers that
6464 ;; unconditionally signal these attributes - backtracking doesn't
6465 ;; change that. So keep them set in that case.
6466 (or at-type-decl
6467 (setq at-type-decl backup-at-type-decl))
6468 (or maybe-typeless
6469 (setq maybe-typeless backup-maybe-typeless))
6470
6471 ,(unless short
6472 ;; This identifier is bound only in the inner let.
6473 '(setq start id-start))))
6474
6475(defun c-forward-decl-or-cast-1 (preceding-token-end context last-cast-end)
6476 ;; Move forward over a declaration or a cast if at the start of one.
6477 ;; The point is assumed to be at the start of some token. Nil is
6478 ;; returned if no declaration or cast is recognized, and the point
6479 ;; is clobbered in that case.
6480 ;;
6481 ;; If a declaration is parsed:
6482 ;;
6483 ;; The point is left at the first token after the first complete
6484 ;; declarator, if there is one. The return value is a cons where
022d0cf4
AM
6485 ;; the car is the position of the first token in the declarator. (See
6486 ;; below for the cdr.)
0386b551
AM
6487 ;; Some examples:
6488 ;;
6489 ;; void foo (int a, char *b) stuff ...
6490 ;; car ^ ^ point
6491 ;; float (*a)[], b;
6492 ;; car ^ ^ point
6493 ;; unsigned int a = c_style_initializer, b;
6494 ;; car ^ ^ point
6495 ;; unsigned int a (cplusplus_style_initializer), b;
6496 ;; car ^ ^ point (might change)
6497 ;; class Foo : public Bar {}
6498 ;; car ^ ^ point
6499 ;; class PikeClass (int a, string b) stuff ...
6500 ;; car ^ ^ point
6501 ;; enum bool;
6502 ;; car ^ ^ point
6503 ;; enum bool flag;
6504 ;; car ^ ^ point
6505 ;; void cplusplus_function (int x) throw (Bad);
6506 ;; car ^ ^ point
6507 ;; Foo::Foo (int b) : Base (b) {}
6508 ;; car ^ ^ point
91af3942 6509 ;;
e15f8aaa
AM
6510 ;; The cdr of the return value is non-nil when a
6511 ;; `c-typedef-decl-kwds' specifier is found in the declaration.
6512 ;; Specifically it is a dotted pair (A . B) where B is t when a
6513 ;; `c-typedef-kwds' ("typedef") is present, and A is t when some
6514 ;; other `c-typedef-decl-kwds' (e.g. class, struct, enum)
6515 ;; specifier is present. I.e., (some of) the declared
6516 ;; identifier(s) are types.
91af3942 6517 ;;
0386b551
AM
6518 ;; If a cast is parsed:
6519 ;;
6520 ;; The point is left at the first token after the closing paren of
6521 ;; the cast. The return value is `cast'. Note that the start
6522 ;; position must be at the first token inside the cast parenthesis
6523 ;; to recognize it.
6524 ;;
6525 ;; PRECEDING-TOKEN-END is the first position after the preceding
6526 ;; token, i.e. on the other side of the syntactic ws from the point.
6527 ;; Use a value less than or equal to (point-min) if the point is at
6528 ;; the first token in (the visible part of) the buffer.
6529 ;;
6530 ;; CONTEXT is a symbol that describes the context at the point:
022d0cf4 6531 ;; 'decl In a comma-separated declaration context (typically
0386b551
AM
6532 ;; inside a function declaration arglist).
6533 ;; '<> In an angle bracket arglist.
6534 ;; 'arglist Some other type of arglist.
a85fd6da
AM
6535 ;; nil Some other context or unknown context. Includes
6536 ;; within the parens of an if, for, ... construct.
0386b551
AM
6537 ;;
6538 ;; LAST-CAST-END is the first token after the closing paren of a
6539 ;; preceding cast, or nil if none is known. If
6540 ;; `c-forward-decl-or-cast-1' is used in succession, it should be
6541 ;; the position after the closest preceding call where a cast was
6542 ;; matched. In that case it's used to discover chains of casts like
6543 ;; "(a) (b) c".
6544 ;;
6545 ;; This function records identifier ranges on
6546 ;; `c-record-type-identifiers' and `c-record-ref-identifiers' if
6547 ;; `c-record-type-identifiers' is non-nil.
6548 ;;
6549 ;; This function might do hidden buffer changes.
6550
6551 (let (;; `start-pos' is used below to point to the start of the
6552 ;; first type, i.e. after any leading specifiers. It might
6553 ;; also point at the beginning of the preceding syntactic
6554 ;; whitespace.
6555 (start-pos (point))
6556 ;; Set to the result of `c-forward-type'.
6557 at-type
6558 ;; The position of the first token in what we currently
6559 ;; believe is the type in the declaration or cast, after any
6560 ;; specifiers and their associated clauses.
6561 type-start
6562 ;; The position of the first token in what we currently
6563 ;; believe is the declarator for the first identifier. Set
6564 ;; when the type is found, and moved forward over any
6565 ;; `c-decl-hangon-kwds' and their associated clauses that
6566 ;; occurs after the type.
6567 id-start
6568 ;; These store `at-type', `type-start' and `id-start' of the
6569 ;; identifier before the one in those variables. The previous
6570 ;; identifier might turn out to be the real type in a
6571 ;; declaration if the last one has to be the declarator in it.
6572 ;; If `backup-at-type' is nil then the other variables have
6573 ;; undefined values.
6574 backup-at-type backup-type-start backup-id-start
e15f8aaa
AM
6575 ;; Set if we've found a specifier (apart from "typedef") that makes
6576 ;; the defined identifier(s) types.
0386b551 6577 at-type-decl
e15f8aaa
AM
6578 ;; Set if we've a "typedef" keyword.
6579 at-typedef
0386b551
AM
6580 ;; Set if we've found a specifier that can start a declaration
6581 ;; where there's no type.
6582 maybe-typeless
6583 ;; If a specifier is found that also can be a type prefix,
6584 ;; these flags are set instead of those above. If we need to
6585 ;; back up an identifier, they are copied to the real flag
6586 ;; variables. Thus they only take effect if we fail to
6587 ;; interpret it as a type.
6588 backup-at-type-decl backup-maybe-typeless
6589 ;; Whether we've found a declaration or a cast. We might know
6590 ;; this before we've found the type in it. It's 'ids if we've
6591 ;; found two consecutive identifiers (usually a sure sign, but
6592 ;; we should allow that in labels too), and t if we've found a
6593 ;; specifier keyword (a 100% sure sign).
6594 at-decl-or-cast
6595 ;; Set when we need to back up to parse this as a declaration
6596 ;; but not as a cast.
6597 backup-if-not-cast
6598 ;; For casts, the return position.
6599 cast-end
6600 ;; Save `c-record-type-identifiers' and
6601 ;; `c-record-ref-identifiers' since ranges are recorded
6602 ;; speculatively and should be thrown away if it turns out
6603 ;; that it isn't a declaration or cast.
6604 (save-rec-type-ids c-record-type-identifiers)
6605 (save-rec-ref-ids c-record-ref-identifiers))
6606
452ea855
AM
6607 (while (c-forward-annotation)
6608 (c-forward-syntactic-ws))
6609
0386b551
AM
6610 ;; Check for a type. Unknown symbols are treated as possible
6611 ;; types, but they could also be specifiers disguised through
6612 ;; macros like __INLINE__, so we recognize both types and known
6613 ;; specifiers after them too.
6614 (while
6615 (let* ((start (point)) kwd-sym kwd-clause-end found-type)
6616
6617 ;; Look for a specifier keyword clause.
e3715033
AM
6618 (when (or (looking-at c-prefix-spec-kwds-re)
6619 (and (c-major-mode-is 'java-mode)
6620 (looking-at "@[A-Za-z0-9]+")))
e15f8aaa
AM
6621 (if (looking-at c-typedef-key)
6622 (setq at-typedef t))
0386b551
AM
6623 (setq kwd-sym (c-keyword-sym (match-string 1)))
6624 (save-excursion
6625 (c-forward-keyword-clause 1)
6626 (setq kwd-clause-end (point))))
6627
e15f8aaa 6628 (when (setq found-type (c-forward-type t)) ; brace-block-too
0386b551
AM
6629 ;; Found a known or possible type or a prefix of a known type.
6630
6631 (when at-type
6632 ;; Got two identifiers with nothing but whitespace
6633 ;; between them. That can only happen in declarations.
6634 (setq at-decl-or-cast 'ids)
6635
6636 (when (eq at-type 'found)
6637 ;; If the previous identifier is a found type we
6638 ;; record it as a real one; it might be some sort of
6639 ;; alias for a prefix like "unsigned".
6640 (save-excursion
6641 (goto-char type-start)
6642 (let ((c-promote-possible-types t))
6643 (c-forward-type)))))
6644
6645 (setq backup-at-type at-type
6646 backup-type-start type-start
6647 backup-id-start id-start
6648 at-type found-type
6649 type-start start
6650 id-start (point)
6651 ;; The previous ambiguous specifier/type turned out
6652 ;; to be a type since we've parsed another one after
6653 ;; it, so clear these backup flags.
6654 backup-at-type-decl nil
6655 backup-maybe-typeless nil))
6656
6657 (if kwd-sym
6658 (progn
6659 ;; Handle known specifier keywords and
6660 ;; `c-decl-hangon-kwds' which can occur after known
6661 ;; types.
6662
6663 (if (c-keyword-member kwd-sym 'c-decl-hangon-kwds)
6664 ;; It's a hang-on keyword that can occur anywhere.
6665 (progn
6666 (setq at-decl-or-cast t)
6667 (if at-type
6668 ;; Move the identifier start position if
6669 ;; we've passed a type.
6670 (setq id-start kwd-clause-end)
6671 ;; Otherwise treat this as a specifier and
6672 ;; move the fallback position.
6673 (setq start-pos kwd-clause-end))
6674 (goto-char kwd-clause-end))
6675
6676 ;; It's an ordinary specifier so we know that
6677 ;; anything before this can't be the type.
6678 (setq backup-at-type nil
6679 start-pos kwd-clause-end)
6680
6681 (if found-type
6682 ;; It's ambiguous whether this keyword is a
6683 ;; specifier or a type prefix, so set the backup
6684 ;; flags. (It's assumed that `c-forward-type'
6685 ;; moved further than `c-forward-keyword-clause'.)
6686 (progn
6687 (when (c-keyword-member kwd-sym 'c-typedef-decl-kwds)
6688 (setq backup-at-type-decl t))
6689 (when (c-keyword-member kwd-sym 'c-typeless-decl-kwds)
6690 (setq backup-maybe-typeless t)))
6691
6692 (when (c-keyword-member kwd-sym 'c-typedef-decl-kwds)
e15f8aaa
AM
6693 ;; This test only happens after we've scanned a type.
6694 ;; So, with valid syntax, kwd-sym can't be 'typedef.
0386b551
AM
6695 (setq at-type-decl t))
6696 (when (c-keyword-member kwd-sym 'c-typeless-decl-kwds)
6697 (setq maybe-typeless t))
6698
ee7683eb 6699 ;; Haven't matched a type so it's an unambiguous
0386b551
AM
6700 ;; specifier keyword and we know we're in a
6701 ;; declaration.
6702 (setq at-decl-or-cast t)
6703
6704 (goto-char kwd-clause-end))))
6705
6706 ;; If the type isn't known we continue so that we'll jump
6707 ;; over all specifiers and type identifiers. The reason
6708 ;; to do this for a known type prefix is to make things
6709 ;; like "unsigned INT16" work.
6710 (and found-type (not (eq found-type t))))))
6711
6712 (cond
6713 ((eq at-type t)
6714 ;; If a known type was found, we still need to skip over any
6715 ;; hangon keyword clauses after it. Otherwise it has already
6716 ;; been done in the loop above.
6717 (while (looking-at c-decl-hangon-key)
6718 (c-forward-keyword-clause 1))
6719 (setq id-start (point)))
6720
6721 ((eq at-type 'prefix)
6722 ;; A prefix type is itself a primitive type when it's not
6723 ;; followed by another type.
6724 (setq at-type t))
6725
6726 ((not at-type)
6727 ;; Got no type but set things up to continue anyway to handle
6728 ;; the various cases when a declaration doesn't start with a
6729 ;; type.
6730 (setq id-start start-pos))
6731
6732 ((and (eq at-type 'maybe)
6733 (c-major-mode-is 'c++-mode))
6734 ;; If it's C++ then check if the last "type" ends on the form
6735 ;; "foo::foo" or "foo::~foo", i.e. if it's the name of a
6736 ;; (con|de)structor.
6737 (save-excursion
6738 (let (name end-2 end-1)
6739 (goto-char id-start)
6740 (c-backward-syntactic-ws)
6741 (setq end-2 (point))
6742 (when (and
6743 (c-simple-skip-symbol-backward)
6744 (progn
6745 (setq name
6746 (buffer-substring-no-properties (point) end-2))
6747 ;; Cheating in the handling of syntactic ws below.
6748 (< (skip-chars-backward ":~ \t\n\r\v\f") 0))
6749 (progn
6750 (setq end-1 (point))
6751 (c-simple-skip-symbol-backward))
6752 (>= (point) type-start)
6753 (equal (buffer-substring-no-properties (point) end-1)
6754 name))
6755 ;; It is a (con|de)structor name. In that case the
6756 ;; declaration is typeless so zap out any preceding
6757 ;; identifier(s) that we might have taken as types.
6758 (goto-char type-start)
6759 (setq at-type nil
6760 backup-at-type nil
6761 id-start type-start))))))
6762
6763 ;; Check for and step over a type decl expression after the thing
6764 ;; that is or might be a type. This can't be skipped since we
6765 ;; need the correct end position of the declarator for
6766 ;; `max-type-decl-end-*'.
6767 (let ((start (point)) (paren-depth 0) pos
6768 ;; True if there's a non-open-paren match of
6769 ;; `c-type-decl-prefix-key'.
6770 got-prefix
6771 ;; True if the declarator is surrounded by a parenthesis pair.
6772 got-parens
6773 ;; True if there is an identifier in the declarator.
6774 got-identifier
6775 ;; True if there's a non-close-paren match of
6776 ;; `c-type-decl-suffix-key'.
6777 got-suffix
6778 ;; True if there's a prefix match outside the outermost
6779 ;; paren pair that surrounds the declarator.
6780 got-prefix-before-parens
1379f2c5 6781 ;; True if there's a suffix match outside the outermost
0386b551
AM
6782 ;; paren pair that surrounds the declarator. The value is
6783 ;; the position of the first suffix match.
6784 got-suffix-after-parens
6785 ;; True if we've parsed the type decl to a token that is
6786 ;; known to end declarations in this context.
6787 at-decl-end
6788 ;; The earlier values of `at-type' and `type-start' if we've
6789 ;; shifted the type backwards.
6790 identifier-type identifier-start
6791 ;; If `c-parse-and-markup-<>-arglists' is set we need to
6792 ;; turn it off during the name skipping below to avoid
6793 ;; getting `c-type' properties that might be bogus. That
6794 ;; can happen since we don't know if
6795 ;; `c-restricted-<>-arglists' will be correct inside the
6796 ;; arglist paren that gets entered.
6797 c-parse-and-markup-<>-arglists)
6798
6799 (goto-char id-start)
6800
6801 ;; Skip over type decl prefix operators. (Note similar code in
6802 ;; `c-font-lock-declarators'.)
6803 (while (and (looking-at c-type-decl-prefix-key)
6804 (if (and (c-major-mode-is 'c++-mode)
05842630 6805 (match-beginning 3))
0386b551
AM
6806 ;; If the second submatch matches in C++ then
6807 ;; we're looking at an identifier that's a
6808 ;; prefix only if it specifies a member pointer.
6809 (when (setq got-identifier (c-forward-name))
6810 (if (looking-at "\\(::\\)")
6811 ;; We only check for a trailing "::" and
6812 ;; let the "*" that should follow be
6813 ;; matched in the next round.
6814 (progn (setq got-identifier nil) t)
6815 ;; It turned out to be the real identifier,
6816 ;; so stop.
6817 nil))
6818 t))
6819
6820 (if (eq (char-after) ?\()
6821 (progn
6822 (setq paren-depth (1+ paren-depth))
6823 (forward-char))
6824 (unless got-prefix-before-parens
6825 (setq got-prefix-before-parens (= paren-depth 0)))
6826 (setq got-prefix t)
6827 (goto-char (match-end 1)))
6828 (c-forward-syntactic-ws))
6829
6830 (setq got-parens (> paren-depth 0))
6831
6832 ;; Skip over an identifier.
6833 (or got-identifier
6834 (and (looking-at c-identifier-start)
6835 (setq got-identifier (c-forward-name))))
6836
6837 ;; Skip over type decl suffix operators.
6838 (while (if (looking-at c-type-decl-suffix-key)
6839
6840 (if (eq (char-after) ?\))
6841 (when (> paren-depth 0)
6842 (setq paren-depth (1- paren-depth))
6843 (forward-char)
6844 t)
6845 (when (if (save-match-data (looking-at "\\s\("))
6846 (c-safe (c-forward-sexp 1) t)
6847 (goto-char (match-end 1))
6848 t)
6849 (when (and (not got-suffix-after-parens)
6850 (= paren-depth 0))
6851 (setq got-suffix-after-parens (match-beginning 0)))
6852 (setq got-suffix t)))
6853
6854 ;; No suffix matched. We might have matched the
6855 ;; identifier as a type and the open paren of a
6856 ;; function arglist as a type decl prefix. In that
6857 ;; case we should "backtrack": Reinterpret the last
6858 ;; type as the identifier, move out of the arglist and
6859 ;; continue searching for suffix operators.
6860 ;;
6861 ;; Do this even if there's no preceding type, to cope
6862 ;; with old style function declarations in K&R C,
6863 ;; (con|de)structors in C++ and `c-typeless-decl-kwds'
6864 ;; style declarations. That isn't applicable in an
6865 ;; arglist context, though.
6866 (when (and (= paren-depth 1)
6867 (not got-prefix-before-parens)
6868 (not (eq at-type t))
6869 (or backup-at-type
6870 maybe-typeless
6871 backup-maybe-typeless
6872 (when c-recognize-typeless-decls
6873 (not context)))
6874 (setq pos (c-up-list-forward (point)))
6875 (eq (char-before pos) ?\)))
6876 (c-fdoc-shift-type-backward)
6877 (goto-char pos)
6878 t))
6879
6880 (c-forward-syntactic-ws))
6881
6882 (when (and (or maybe-typeless backup-maybe-typeless)
6883 (not got-identifier)
6884 (not got-prefix)
6885 at-type)
6886 ;; Have found no identifier but `c-typeless-decl-kwds' has
6887 ;; matched so we know we're inside a declaration. The
6888 ;; preceding type must be the identifier instead.
6889 (c-fdoc-shift-type-backward))
6890
6891 (setq
6892 at-decl-or-cast
6893 (catch 'at-decl-or-cast
6894
a85fd6da 6895 ;; CASE 1
0386b551
AM
6896 (when (> paren-depth 0)
6897 ;; Encountered something inside parens that isn't matched by
6898 ;; the `c-type-decl-*' regexps, so it's not a type decl
6899 ;; expression. Try to skip out to the same paren depth to
6900 ;; not confuse the cast check below.
6901 (c-safe (goto-char (scan-lists (point) 1 paren-depth)))
6902 ;; If we've found a specifier keyword then it's a
6903 ;; declaration regardless.
6904 (throw 'at-decl-or-cast (eq at-decl-or-cast t)))
6905
6906 (setq at-decl-end
6907 (looking-at (cond ((eq context '<>) "[,>]")
6908 (context "[,\)]")
6909 (t "[,;]"))))
6910
6911 ;; Now we've collected info about various characteristics of
6912 ;; the construct we're looking at. Below follows a decision
6913 ;; tree based on that. It's ordered to check more certain
6914 ;; signs before less certain ones.
6915
6916 (if got-identifier
6917 (progn
6918
a85fd6da 6919 ;; CASE 2
0386b551
AM
6920 (when (and (or at-type maybe-typeless)
6921 (not (or got-prefix got-parens)))
6922 ;; Got another identifier directly after the type, so it's a
6923 ;; declaration.
6924 (throw 'at-decl-or-cast t))
6925
6926 (when (and got-parens
6927 (not got-prefix)
6928 (not got-suffix-after-parens)
6929 (or backup-at-type
6930 maybe-typeless
6931 backup-maybe-typeless))
6932 ;; Got a declaration of the form "foo bar (gnu);" where we've
6933 ;; recognized "bar" as the type and "gnu" as the declarator.
6934 ;; In this case it's however more likely that "bar" is the
6935 ;; declarator and "gnu" a function argument or initializer (if
6936 ;; `c-recognize-paren-inits' is set), since the parens around
6937 ;; "gnu" would be superfluous if it's a declarator. Shift the
6938 ;; type one step backward.
6939 (c-fdoc-shift-type-backward)))
6940
6941 ;; Found no identifier.
6942
6943 (if backup-at-type
6944 (progn
6945
b248a85d
AM
6946
6947 ;; CASE 3
6948 (when (= (point) start)
6949 ;; Got a plain list of identifiers. If a colon follows it's
6950 ;; a valid label, or maybe a bitfield. Otherwise the last
6951 ;; one probably is the declared identifier and we should
6952 ;; back up to the previous type, providing it isn't a cast.
452ea855
AM
6953 (if (and (eq (char-after) ?:)
6954 (not (c-major-mode-is 'java-mode)))
b248a85d
AM
6955 (cond
6956 ;; If we've found a specifier keyword then it's a
6957 ;; declaration regardless.
6958 ((eq at-decl-or-cast t)
6959 (throw 'at-decl-or-cast t))
6960 ((and c-has-bitfields
6961 (eq at-decl-or-cast 'ids)) ; bitfield.
6962 (setq backup-if-not-cast t)
6963 (throw 'at-decl-or-cast t)))
6964
6965 (setq backup-if-not-cast t)
6966 (throw 'at-decl-or-cast t)))
0386b551 6967
a85fd6da 6968 ;; CASE 4
0386b551
AM
6969 (when (and got-suffix
6970 (not got-prefix)
6971 (not got-parens))
6972 ;; Got a plain list of identifiers followed by some suffix.
6973 ;; If this isn't a cast then the last identifier probably is
6974 ;; the declared one and we should back up to the previous
6975 ;; type.
6976 (setq backup-if-not-cast t)
6977 (throw 'at-decl-or-cast t)))
6978
a85fd6da 6979 ;; CASE 5
0386b551
AM
6980 (when (eq at-type t)
6981 ;; If the type is known we know that there can't be any
6982 ;; identifier somewhere else, and it's only in declarations in
6983 ;; e.g. function prototypes and in casts that the identifier may
6984 ;; be left out.
6985 (throw 'at-decl-or-cast t))
6986
6987 (when (= (point) start)
6988 ;; Only got a single identifier (parsed as a type so far).
a85fd6da 6989 ;; CASE 6
0386b551
AM
6990 (if (and
6991 ;; Check that the identifier isn't at the start of an
6992 ;; expression.
6993 at-decl-end
6994 (cond
6995 ((eq context 'decl)
6996 ;; Inside an arglist that contains declarations. If K&R
6997 ;; style declarations and parenthesis style initializers
6998 ;; aren't allowed then the single identifier must be a
6999 ;; type, else we require that it's known or found
7000 ;; (primitive types are handled above).
7001 (or (and (not c-recognize-knr-p)
7002 (not c-recognize-paren-inits))
7003 (memq at-type '(known found))))
7004 ((eq context '<>)
7005 ;; Inside a template arglist. Accept known and found
7006 ;; types; other identifiers could just as well be
7007 ;; constants in C++.
7008 (memq at-type '(known found)))))
7009 (throw 'at-decl-or-cast t)
a85fd6da 7010 ;; CASE 7
0386b551
AM
7011 ;; Can't be a valid declaration or cast, but if we've found a
7012 ;; specifier it can't be anything else either, so treat it as
7013 ;; an invalid/unfinished declaration or cast.
7014 (throw 'at-decl-or-cast at-decl-or-cast))))
7015
7016 (if (and got-parens
7017 (not got-prefix)
7018 (not context)
7019 (not (eq at-type t))
7020 (or backup-at-type
7021 maybe-typeless
7022 backup-maybe-typeless
7023 (when c-recognize-typeless-decls
7024 (or (not got-suffix)
7025 (not (looking-at
7026 c-after-suffixed-type-maybe-decl-key))))))
7027 ;; Got an empty paren pair and a preceding type that probably
7028 ;; really is the identifier. Shift the type backwards to make
7029 ;; the last one the identifier. This is analogous to the
7030 ;; "backtracking" done inside the `c-type-decl-suffix-key' loop
7031 ;; above.
7032 ;;
7033 ;; Exception: In addition to the conditions in that
7034 ;; "backtracking" code, do not shift backward if we're not
7035 ;; looking at either `c-after-suffixed-type-decl-key' or "[;,]".
7036 ;; Since there's no preceding type, the shift would mean that
7037 ;; the declaration is typeless. But if the regexp doesn't match
7038 ;; then we will simply fall through in the tests below and not
7039 ;; recognize it at all, so it's better to try it as an abstract
7040 ;; declarator instead.
7041 (c-fdoc-shift-type-backward)
7042
7043 ;; Still no identifier.
a85fd6da 7044 ;; CASE 8
0386b551
AM
7045 (when (and got-prefix (or got-parens got-suffix))
7046 ;; Require `got-prefix' together with either `got-parens' or
7047 ;; `got-suffix' to recognize it as an abstract declarator:
7048 ;; `got-parens' only is probably an empty function call.
7049 ;; `got-suffix' only can build an ordinary expression together
7050 ;; with the preceding identifier which we've taken as a type.
7051 ;; We could actually accept on `got-prefix' only, but that can
7052 ;; easily occur temporarily while writing an expression so we
7053 ;; avoid that case anyway. We could do a better job if we knew
7054 ;; the point when the fontification was invoked.
7055 (throw 'at-decl-or-cast t))
7056
a85fd6da 7057 ;; CASE 9
0386b551
AM
7058 (when (and at-type
7059 (not got-prefix)
7060 (not got-parens)
7061 got-suffix-after-parens
7062 (eq (char-after got-suffix-after-parens) ?\())
7063 ;; Got a type, no declarator but a paren suffix. I.e. it's a
fa463103 7064 ;; normal function call after all (or perhaps a C++ style object
0386b551
AM
7065 ;; instantiation expression).
7066 (throw 'at-decl-or-cast nil))))
7067
a85fd6da 7068 ;; CASE 10
0386b551
AM
7069 (when at-decl-or-cast
7070 ;; By now we've located the type in the declaration that we know
7071 ;; we're in.
7072 (throw 'at-decl-or-cast t))
7073
a85fd6da 7074 ;; CASE 11
0386b551
AM
7075 (when (and got-identifier
7076 (not context)
7077 (looking-at c-after-suffixed-type-decl-key)
7078 (if (and got-parens
7079 (not got-prefix)
7080 (not got-suffix)
7081 (not (eq at-type t)))
7082 ;; Shift the type backward in the case that there's a
7083 ;; single identifier inside parens. That can only
7084 ;; occur in K&R style function declarations so it's
7085 ;; more likely that it really is a function call.
7086 ;; Therefore we only do this after
7087 ;; `c-after-suffixed-type-decl-key' has matched.
7088 (progn (c-fdoc-shift-type-backward) t)
7089 got-suffix-after-parens))
7090 ;; A declaration according to `c-after-suffixed-type-decl-key'.
7091 (throw 'at-decl-or-cast t))
7092
a85fd6da 7093 ;; CASE 12
0386b551
AM
7094 (when (and (or got-prefix (not got-parens))
7095 (memq at-type '(t known)))
7096 ;; It's a declaration if a known type precedes it and it can't be a
7097 ;; function call.
7098 (throw 'at-decl-or-cast t))
7099
7100 ;; If we get here we can't tell if this is a type decl or a normal
7101 ;; expression by looking at it alone. (That's under the assumption
7102 ;; that normal expressions always can look like type decl expressions,
7103 ;; which isn't really true but the cases where it doesn't hold are so
7104 ;; uncommon (e.g. some placements of "const" in C++) it's not worth
7105 ;; the effort to look for them.)
7106
7107 (unless (or at-decl-end (looking-at "=[^=]"))
7108 ;; If this is a declaration it should end here or its initializer(*)
7109 ;; should start here, so check for allowed separation tokens. Note
7110 ;; that this rule doesn't work e.g. with a K&R arglist after a
7111 ;; function header.
7112 ;;
7113 ;; *) Don't check for C++ style initializers using parens
7114 ;; since those already have been matched as suffixes.
7115 ;;
7116 ;; If `at-decl-or-cast' is then we've found some other sign that
7117 ;; it's a declaration or cast, so then it's probably an
7118 ;; invalid/unfinished one.
7119 (throw 'at-decl-or-cast at-decl-or-cast))
7120
7121 ;; Below are tests that only should be applied when we're certain to
7122 ;; not have parsed halfway through an expression.
7123
a85fd6da 7124 ;; CASE 14
0386b551
AM
7125 (when (memq at-type '(t known))
7126 ;; The expression starts with a known type so treat it as a
7127 ;; declaration.
7128 (throw 'at-decl-or-cast t))
7129
a85fd6da 7130 ;; CASE 15
0386b551
AM
7131 (when (and (c-major-mode-is 'c++-mode)
7132 ;; In C++ we check if the identifier is a known type, since
7133 ;; (con|de)structors use the class name as identifier.
7134 ;; We've always shifted over the identifier as a type and
7135 ;; then backed up again in this case.
7136 identifier-type
7137 (or (memq identifier-type '(found known))
7138 (and (eq (char-after identifier-start) ?~)
7139 ;; `at-type' probably won't be 'found for
7140 ;; destructors since the "~" is then part of the
7141 ;; type name being checked against the list of
7142 ;; known types, so do a check without that
7143 ;; operator.
7144 (or (save-excursion
7145 (goto-char (1+ identifier-start))
7146 (c-forward-syntactic-ws)
7147 (c-with-syntax-table
7148 c-identifier-syntax-table
7149 (looking-at c-known-type-key)))
7150 (save-excursion
7151 (goto-char (1+ identifier-start))
7152 ;; We have already parsed the type earlier,
7153 ;; so it'd be possible to cache the end
7154 ;; position instead of redoing it here, but
7155 ;; then we'd need to keep track of another
7156 ;; position everywhere.
7157 (c-check-type (point)
7158 (progn (c-forward-type)
7159 (point))))))))
7160 (throw 'at-decl-or-cast t))
7161
7162 (if got-identifier
7163 (progn
a85fd6da 7164 ;; CASE 16
0386b551
AM
7165 (when (and got-prefix-before-parens
7166 at-type
7167 (or at-decl-end (looking-at "=[^=]"))
7168 (not context)
7169 (not got-suffix))
7170 ;; Got something like "foo * bar;". Since we're not inside an
7171 ;; arglist it would be a meaningless expression because the
7172 ;; result isn't used. We therefore choose to recognize it as
7173 ;; a declaration. Do not allow a suffix since it could then
7174 ;; be a function call.
7175 (throw 'at-decl-or-cast t))
7176
a85fd6da 7177 ;; CASE 17
0386b551
AM
7178 (when (and (or got-suffix-after-parens
7179 (looking-at "=[^=]"))
7180 (eq at-type 'found)
7181 (not (eq context 'arglist)))
7182 ;; Got something like "a (*b) (c);" or "a (b) = c;". It could
7183 ;; be an odd expression or it could be a declaration. Treat
7184 ;; it as a declaration if "a" has been used as a type
7185 ;; somewhere else (if it's a known type we won't get here).
7186 (throw 'at-decl-or-cast t)))
7187
a85fd6da 7188 ;; CASE 18
0386b551
AM
7189 (when (and context
7190 (or got-prefix
7191 (and (eq context 'decl)
7192 (not c-recognize-paren-inits)
7193 (or got-parens got-suffix))))
7194 ;; Got a type followed by an abstract declarator. If `got-prefix'
7195 ;; is set it's something like "a *" without anything after it. If
7196 ;; `got-parens' or `got-suffix' is set it's "a()", "a[]", "a()[]",
7197 ;; or similar, which we accept only if the context rules out
7198 ;; expressions.
7199 (throw 'at-decl-or-cast t)))
7200
7201 ;; If we had a complete symbol table here (which rules out
7202 ;; `c-found-types') we should return t due to the disambiguation rule
7203 ;; (in at least C++) that anything that can be parsed as a declaration
7204 ;; is a declaration. Now we're being more defensive and prefer to
7205 ;; highlight things like "foo (bar);" as a declaration only if we're
7206 ;; inside an arglist that contains declarations.
7207 (eq context 'decl))))
7208
7209 ;; The point is now after the type decl expression.
7210
7211 (cond
7212 ;; Check for a cast.
7213 ((save-excursion
7214 (and
7215 c-cast-parens
7216
7217 ;; Should be the first type/identifier in a cast paren.
7218 (> preceding-token-end (point-min))
7219 (memq (char-before preceding-token-end) c-cast-parens)
7220
7221 ;; The closing paren should follow.
7222 (progn
7223 (c-forward-syntactic-ws)
7224 (looking-at "\\s\)"))
7225
7226 ;; There should be a primary expression after it.
7227 (let (pos)
7228 (forward-char)
7229 (c-forward-syntactic-ws)
7230 (setq cast-end (point))
7231 (and (looking-at c-primary-expr-regexp)
7232 (progn
7233 (setq pos (match-end 0))
7234 (or
7235 ;; Check if the expression begins with a prefix keyword.
7236 (match-beginning 2)
7237 (if (match-beginning 1)
7238 ;; Expression begins with an ambiguous operator. Treat
7239 ;; it as a cast if it's a type decl or if we've
7240 ;; recognized the type somewhere else.
7241 (or at-decl-or-cast
7242 (memq at-type '(t known found)))
7243 ;; Unless it's a keyword, it's the beginning of a primary
7244 ;; expression.
7245 (not (looking-at c-keywords-regexp)))))
7246 ;; If `c-primary-expr-regexp' matched a nonsymbol token, check
7247 ;; that it matched a whole one so that we don't e.g. confuse
7248 ;; the operator '-' with '->'. It's ok if it matches further,
7249 ;; though, since it e.g. can match the float '.5' while the
7250 ;; operator regexp only matches '.'.
7251 (or (not (looking-at c-nonsymbol-token-regexp))
7252 (<= (match-end 0) pos))))
7253
7254 ;; There should either be a cast before it or something that isn't an
7255 ;; identifier or close paren.
7256 (> preceding-token-end (point-min))
7257 (progn
7258 (goto-char (1- preceding-token-end))
7259 (or (eq (point) last-cast-end)
7260 (progn
7261 (c-backward-syntactic-ws)
7262 (if (< (skip-syntax-backward "w_") 0)
7263 ;; It's a symbol. Accept it only if it's one of the
7264 ;; keywords that can precede an expression (without
7265 ;; surrounding parens).
7266 (looking-at c-simple-stmt-key)
7267 (and
7268 ;; Check that it isn't a close paren (block close is ok,
7269 ;; though).
7270 (not (memq (char-before) '(?\) ?\])))
7271 ;; Check that it isn't a nonsymbol identifier.
7272 (not (c-on-identifier)))))))))
7273
7274 ;; Handle the cast.
7275 (when (and c-record-type-identifiers at-type (not (eq at-type t)))
7276 (let ((c-promote-possible-types t))
7277 (goto-char type-start)
7278 (c-forward-type)))
7279
7280 (goto-char cast-end)
7281 'cast)
7282
7283 (at-decl-or-cast
7284 ;; We're at a declaration. Highlight the type and the following
7285 ;; declarators.
7286
7287 (when backup-if-not-cast
7288 (c-fdoc-shift-type-backward t))
7289
7290 (when (and (eq context 'decl) (looking-at ","))
7291 ;; Make sure to propagate the `c-decl-arg-start' property to
7292 ;; the next argument if it's set in this one, to cope with
7293 ;; interactive refontification.
7294 (c-put-c-type-property (point) 'c-decl-arg-start))
7295
7296 (when (and c-record-type-identifiers at-type (not (eq at-type t)))
7297 (let ((c-promote-possible-types t))
7298 (save-excursion
7299 (goto-char type-start)
7300 (c-forward-type))))
7301
e15f8aaa
AM
7302 (cons id-start
7303 (and (or at-type-decl at-typedef)
7304 (cons at-type-decl at-typedef))))
0386b551
AM
7305
7306 (t
7307 ;; False alarm. Restore the recorded ranges.
7308 (setq c-record-type-identifiers save-rec-type-ids
7309 c-record-ref-identifiers save-rec-ref-ids)
7310 nil))))
7311
7312(defun c-forward-label (&optional assume-markup preceding-token-end limit)
51c9af45 7313 ;; Assuming that point is at the beginning of a token, check if it starts a
1379f2c5
AM
7314 ;; label and if so move over it and return non-nil (t in default situations,
7315 ;; specific symbols (see below) for interesting situations), otherwise don't
7316 ;; move and return nil. "Label" here means "most things with a colon".
51c9af45
AM
7317 ;;
7318 ;; More precisely, a "label" is regarded as one of:
1379f2c5
AM
7319 ;; (i) a goto target like "foo:" - returns the symbol `goto-target';
7320 ;; (ii) A case label - either the entire construct "case FOO:", or just the
7321 ;; bare "case", should the colon be missing. We return t;
7322 ;; (iii) a keyword which needs a colon, like "default:" or "private:"; We
7323 ;; return t;
51c9af45 7324 ;; (iv) One of QT's "extended" C++ variants of
1379f2c5
AM
7325 ;; "private:"/"protected:"/"public:"/"more:" looking like "public slots:".
7326 ;; Returns the symbol `qt-2kwds-colon'.
7327 ;; (v) QT's construct "signals:". Returns the symbol `qt-1kwd-colon'.
82ba65cf 7328 ;; (vi) One of the keywords matched by `c-opt-extra-label-key' (without any
51c9af45 7329 ;; colon). Currently (2006-03), this applies only to Objective C's
1379f2c5 7330 ;; keywords "@private", "@protected", and "@public". Returns t.
51c9af45 7331 ;;
e1dbe924 7332 ;; One of the things which will NOT be recognized as a label is a bit-field
51c9af45
AM
7333 ;; element of a struct, something like "int foo:5".
7334 ;;
7335 ;; The end of the label is taken to be just after the colon, or the end of
7336 ;; the first submatch in `c-opt-extra-label-key'. The point is directly
7337 ;; after the end on return. The terminating char gets marked with
7338 ;; `c-decl-end' to improve recognition of the following declaration or
7339 ;; statement.
0386b551
AM
7340 ;;
7341 ;; If ASSUME-MARKUP is non-nil, it's assumed that the preceding
51c9af45 7342 ;; label, if any, has already been marked up like that.
0386b551
AM
7343 ;;
7344 ;; If PRECEDING-TOKEN-END is given, it should be the first position
7345 ;; after the preceding token, i.e. on the other side of the
7346 ;; syntactic ws from the point. Use a value less than or equal to
7347 ;; (point-min) if the point is at the first token in (the visible
7348 ;; part of) the buffer.
7349 ;;
7350 ;; The optional LIMIT limits the forward scan for the colon.
7351 ;;
7352 ;; This function records the ranges of the label symbols on
7353 ;; `c-record-ref-identifiers' if `c-record-type-identifiers' (!) is
7354 ;; non-nil.
7355 ;;
7356 ;; This function might do hidden buffer changes.
7357
51c9af45 7358 (let ((start (point))
1379f2c5 7359 label-end
51c9af45 7360 qt-symbol-idx
1379f2c5 7361 macro-start ; if we're in one.
f412a567
AM
7362 label-type
7363 kwd)
0386b551 7364 (cond
b414f371 7365 ;; "case" or "default" (Doesn't apply to AWK).
0386b551
AM
7366 ((looking-at c-label-kwds-regexp)
7367 (let ((kwd-end (match-end 1)))
7368 ;; Record only the keyword itself for fontification, since in
7369 ;; case labels the following is a constant expression and not
7370 ;; a label.
7371 (when c-record-type-identifiers
7372 (c-record-ref-id (cons (match-beginning 1) kwd-end)))
7373
7374 ;; Find the label end.
7375 (goto-char kwd-end)
1379f2c5
AM
7376 (setq label-type
7377 (if (and (c-syntactic-re-search-forward
7378 ;; Stop on chars that aren't allowed in expressions,
7379 ;; and on operator chars that would be meaningless
7380 ;; there. FIXME: This doesn't cope with ?: operators.
7381 "[;{=,@]\\|\\(\\=\\|[^:]\\):\\([^:]\\|\\'\\)"
7382 limit t t nil 1)
7383 (match-beginning 2))
7384
7385 (progn ; there's a proper :
7386 (goto-char (match-beginning 2)) ; just after the :
7387 (c-put-c-type-property (1- (point)) 'c-decl-end)
7388 t)
7389
7390 ;; It's an unfinished label. We consider the keyword enough
7391 ;; to recognize it as a label, so that it gets fontified.
7392 ;; Leave the point at the end of it, but don't put any
7393 ;; `c-decl-end' marker.
7394 (goto-char kwd-end)
7395 t))))
0386b551 7396
51c9af45 7397 ;; @private, @protected, @public, in Objective C, or similar.
0386b551
AM
7398 ((and c-opt-extra-label-key
7399 (looking-at c-opt-extra-label-key))
7400 ;; For a `c-opt-extra-label-key' match, we record the whole
7401 ;; thing for fontification. That's to get the leading '@' in
7402 ;; Objective-C protection labels fontified.
7403 (goto-char (match-end 1))
7404 (when c-record-type-identifiers
7405 (c-record-ref-id (cons (match-beginning 1) (point))))
7406 (c-put-c-type-property (1- (point)) 'c-decl-end)
1379f2c5 7407 (setq label-type t))
0386b551 7408
51c9af45
AM
7409 ;; All other cases of labels.
7410 ((and c-recognize-colon-labels ; nil for AWK and IDL, otherwise t.
0386b551
AM
7411
7412 ;; A colon label must have something before the colon.
7413 (not (eq (char-after) ?:))
7414
7415 ;; Check that we're not after a token that can't precede a label.
7416 (or
7417 ;; Trivially succeeds when there's no preceding token.
536610a4 7418 ;; Succeeds when we're at a virtual semicolon.
0386b551
AM
7419 (if preceding-token-end
7420 (<= preceding-token-end (point-min))
7421 (save-excursion
7422 (c-backward-syntactic-ws)
7423 (setq preceding-token-end (point))
536610a4
AM
7424 (or (bobp)
7425 (c-at-vsemi-p))))
0386b551
AM
7426
7427 ;; Check if we're after a label, if we're after a closing
7428 ;; paren that belong to statement, and with
7429 ;; `c-label-prefix-re'. It's done in different order
7430 ;; depending on `assume-markup' since the checks have
7431 ;; different expensiveness.
7432 (if assume-markup
7433 (or
7434 (eq (c-get-char-property (1- preceding-token-end) 'c-type)
7435 'c-decl-end)
7436
7437 (save-excursion
7438 (goto-char (1- preceding-token-end))
7439 (c-beginning-of-current-token)
51c9af45
AM
7440 (or (looking-at c-label-prefix-re)
7441 (looking-at c-block-stmt-1-key)))
0386b551
AM
7442
7443 (and (eq (char-before preceding-token-end) ?\))
7444 (c-after-conditional)))
7445
7446 (or
7447 (save-excursion
7448 (goto-char (1- preceding-token-end))
7449 (c-beginning-of-current-token)
51c9af45
AM
7450 (or (looking-at c-label-prefix-re)
7451 (looking-at c-block-stmt-1-key)))
0386b551
AM
7452
7453 (cond
7454 ((eq (char-before preceding-token-end) ?\))
7455 (c-after-conditional))
7456
7457 ((eq (char-before preceding-token-end) ?:)
7458 ;; Might be after another label, so check it recursively.
51c9af45
AM
7459 (save-restriction
7460 (save-excursion
7461 (goto-char (1- preceding-token-end))
7462 ;; Essentially the same as the
7463 ;; `c-syntactic-re-search-forward' regexp below.
7464 (setq macro-start
7465 (save-excursion (and (c-beginning-of-macro)
7466 (point))))
7467 (if macro-start (narrow-to-region macro-start (point-max)))
7468 (c-syntactic-skip-backward "^-]:?;}=*/%&|,<>!@+" nil t)
7469 ;; Note: the following should work instead of the
7470 ;; narrow-to-region above. Investigate why not,
7471 ;; sometime. ACM, 2006-03-31.
7472 ;; (c-syntactic-skip-backward "^-]:?;}=*/%&|,<>!@+"
7473 ;; macro-start t)
7474 (let ((pte (point))
7475 ;; If the caller turned on recording for us,
7476 ;; it shouldn't apply when we check the
7477 ;; preceding label.
7478 c-record-type-identifiers)
7479 ;; A label can't start at a cpp directive. Check for
7480 ;; this, since c-forward-syntactic-ws would foul up on it.
7481 (unless (and c-opt-cpp-prefix (looking-at c-opt-cpp-prefix))
7482 (c-forward-syntactic-ws)
7483 (c-forward-label nil pte start))))))))))
7484
1379f2c5 7485 ;; Point is still at the beginning of the possible label construct.
b414f371 7486 ;;
51c9af45
AM
7487 ;; Check that the next nonsymbol token is ":", or that we're in one
7488 ;; of QT's "slots" declarations. Allow '(' for the sake of macro
7489 ;; arguments. FIXME: Should build this regexp from the language
7490 ;; constants.
1379f2c5
AM
7491 (cond
7492 ;; public: protected: private:
7493 ((and
7494 (c-major-mode-is 'c++-mode)
7495 (search-forward-regexp
7496 "\\=p\\(r\\(ivate\\|otected\\)\\|ublic\\)\\>[^_]" nil t)
7497 (progn (backward-char)
7498 (c-forward-syntactic-ws limit)
7499 (looking-at ":\\([^:]\\|\\'\\)"))) ; A single colon.
7500 (forward-char)
7501 (setq label-type t))
7502 ;; QT double keyword like "protected slots:" or goto target.
7503 ((progn (goto-char start) nil))
7504 ((when (c-syntactic-re-search-forward
7505 "[ \t\n[:?;{=*/%&|,<>!@+-]" limit t t) ; not at EOB
7506 (backward-char)
7507 (setq label-end (point))
7508 (setq qt-symbol-idx
7509 (and (c-major-mode-is 'c++-mode)
7510 (string-match
7511 "\\(p\\(r\\(ivate\\|otected\\)\\|ublic\\)\\|more\\)\\>"
b414f371 7512 (buffer-substring start (point)))))
1379f2c5
AM
7513 (c-forward-syntactic-ws limit)
7514 (cond
7515 ((looking-at ":\\([^:]\\|\\'\\)") ; A single colon.
7516 (forward-char)
7517 (setq label-type
f412a567
AM
7518 (if (or (string= "signals" ; Special QT macro
7519 (setq kwd (buffer-substring-no-properties start label-end)))
7520 (string= "Q_SIGNALS" kwd))
1379f2c5
AM
7521 'qt-1kwd-colon
7522 'goto-target)))
7523 ((and qt-symbol-idx
f412a567 7524 (search-forward-regexp "\\=\\(slots\\|Q_SLOTS\\)\\>" limit t)
1379f2c5
AM
7525 (progn (c-forward-syntactic-ws limit)
7526 (looking-at ":\\([^:]\\|\\'\\)"))) ; A single colon
7527 (forward-char)
7528 (setq label-type 'qt-2kwds-colon)))))))
0386b551
AM
7529
7530 (save-restriction
7531 (narrow-to-region start (point))
7532
7533 ;; Check that `c-nonlabel-token-key' doesn't match anywhere.
7534 (catch 'check-label
7535 (goto-char start)
7536 (while (progn
7537 (when (looking-at c-nonlabel-token-key)
7538 (goto-char start)
1379f2c5 7539 (setq label-type nil)
0386b551
AM
7540 (throw 'check-label nil))
7541 (and (c-safe (c-forward-sexp)
7542 (c-forward-syntactic-ws)
7543 t)
7544 (not (eobp)))))
7545
7546 ;; Record the identifiers in the label for fontification, unless
7547 ;; it begins with `c-label-kwds' in which case the following
7548 ;; identifiers are part of a (constant) expression that
7549 ;; shouldn't be fontified.
7550 (when (and c-record-type-identifiers
7551 (progn (goto-char start)
7552 (not (looking-at c-label-kwds-regexp))))
7553 (while (c-syntactic-re-search-forward c-symbol-key nil t)
7554 (c-record-ref-id (cons (match-beginning 0)
7555 (match-end 0)))))
7556
7557 (c-put-c-type-property (1- (point-max)) 'c-decl-end)
82ba65cf 7558 (goto-char (point-max)))))
0386b551
AM
7559
7560 (t
7561 ;; Not a label.
1379f2c5
AM
7562 (goto-char start)))
7563 label-type))
0386b551
AM
7564
7565(defun c-forward-objc-directive ()
7566 ;; Assuming the point is at the beginning of a token, try to move
7567 ;; forward to the end of the Objective-C directive that starts
7568 ;; there. Return t if a directive was fully recognized, otherwise
7569 ;; the point is moved as far as one could be successfully parsed and
7570 ;; nil is returned.
7571 ;;
7572 ;; This function records identifier ranges on
7573 ;; `c-record-type-identifiers' and `c-record-ref-identifiers' if
7574 ;; `c-record-type-identifiers' is non-nil.
7575 ;;
7576 ;; This function might do hidden buffer changes.
7577
7578 (let ((start (point))
7579 start-char
7580 (c-promote-possible-types t)
667ced3a 7581 lim
0386b551
AM
7582 ;; Turn off recognition of angle bracket arglists while parsing
7583 ;; types here since the protocol reference list might then be
7584 ;; considered part of the preceding name or superclass-name.
7585 c-recognize-<>-arglists)
7586
7587 (if (or
7588 (when (looking-at
7589 (eval-when-compile
7590 (c-make-keywords-re t
7591 (append (c-lang-const c-protection-kwds objc)
7592 '("@end"))
7593 'objc-mode)))
7594 (goto-char (match-end 1))
7595 t)
7596
7597 (and
7598 (looking-at
7599 (eval-when-compile
7600 (c-make-keywords-re t
7601 '("@interface" "@implementation" "@protocol")
7602 'objc-mode)))
7603
7604 ;; Handle the name of the class itself.
7605 (progn
cb694ab7
AM
7606; (c-forward-token-2) ; 2006/1/13 This doesn't move if the token's
7607; at EOB.
7608 (goto-char (match-end 0))
667ced3a 7609 (setq lim (point))
cb694ab7 7610 (c-skip-ws-forward)
0386b551
AM
7611 (c-forward-type))
7612
7613 (catch 'break
7614 ;; Look for ": superclass-name" or "( category-name )".
7615 (when (looking-at "[:\(]")
7616 (setq start-char (char-after))
7617 (forward-char)
7618 (c-forward-syntactic-ws)
7619 (unless (c-forward-type) (throw 'break nil))
7620 (when (eq start-char ?\()
7621 (unless (eq (char-after) ?\)) (throw 'break nil))
7622 (forward-char)
7623 (c-forward-syntactic-ws)))
7624
7625 ;; Look for a protocol reference list.
7626 (if (eq (char-after) ?<)
7627 (let ((c-recognize-<>-arglists t)
7628 (c-parse-and-markup-<>-arglists t)
7629 c-restricted-<>-arglists)
7630 (c-forward-<>-arglist t))
7631 t))))
7632
7633 (progn
667ced3a 7634 (c-backward-syntactic-ws lim)
0386b551
AM
7635 (c-clear-c-type-property start (1- (point)) 'c-decl-end)
7636 (c-put-c-type-property (1- (point)) 'c-decl-end)
7637 t)
7638
7639 (c-clear-c-type-property start (point) 'c-decl-end)
7640 nil)))
7641
785eecbb
RS
7642(defun c-beginning-of-inheritance-list (&optional lim)
7643 ;; Go to the first non-whitespace after the colon that starts a
7644 ;; multiple inheritance introduction. Optional LIM is the farthest
7645 ;; back we should search.
0386b551
AM
7646 ;;
7647 ;; This function might do hidden buffer changes.
7648 (c-with-syntax-table c++-template-syntax-table
7649 (c-backward-token-2 0 t lim)
7650 (while (and (or (looking-at c-symbol-start)
7651 (looking-at "[<,]\\|::"))
7652 (zerop (c-backward-token-2 1 t lim))))))
785eecbb 7653
785eecbb
RS
7654(defun c-in-method-def-p ()
7655 ;; Return nil if we aren't in a method definition, otherwise the
7656 ;; position of the initial [+-].
0386b551
AM
7657 ;;
7658 ;; This function might do hidden buffer changes.
785eecbb
RS
7659 (save-excursion
7660 (beginning-of-line)
a66cd3ee
MS
7661 (and c-opt-method-key
7662 (looking-at c-opt-method-key)
785eecbb
RS
7663 (point))
7664 ))
7665
a66cd3ee
MS
7666;; Contributed by Kevin Ryde <user42@zip.com.au>.
7667(defun c-in-gcc-asm-p ()
7668 ;; Return non-nil if point is within a gcc \"asm\" block.
7669 ;;
7670 ;; This should be called with point inside an argument list.
7671 ;;
7672 ;; Only one level of enclosing parentheses is considered, so for
7673 ;; instance `nil' is returned when in a function call within an asm
7674 ;; operand.
0386b551
AM
7675 ;;
7676 ;; This function might do hidden buffer changes.
a66cd3ee
MS
7677
7678 (and c-opt-asm-stmt-key
7679 (save-excursion
7680 (beginning-of-line)
7681 (backward-up-list 1)
7682 (c-beginning-of-statement-1 (point-min) nil t)
7683 (looking-at c-opt-asm-stmt-key))))
7684
abb7e5cf 7685(defun c-at-toplevel-p ()
a85fd6da
AM
7686 "Return a determination as to whether point is \"at the top level\".
7687Informally, \"at the top level\" is anywhere where you can write
7688a function.
7689
7690More precisely, being at the top-level means that point is either
7691outside any enclosing block (such as a function definition), or
7692directly inside a class, namespace or other block that contains
7693another declaration level.
abb7e5cf
SM
7694
7695If point is not at the top-level (e.g. it is inside a method
7696definition), then nil is returned. Otherwise, if point is at a
7697top-level not enclosed within a class definition, t is returned.
7698Otherwise, a 2-vector is returned where the zeroth element is the
7699buffer position of the start of the class declaration, and the first
7700element is the buffer position of the enclosing class's opening
0386b551
AM
7701brace.
7702
7703Note that this function might do hidden buffer changes. See the
7704comment at the start of cc-engine.el for more info."
a66cd3ee
MS
7705 (let ((paren-state (c-parse-state)))
7706 (or (not (c-most-enclosing-brace paren-state))
7707 (c-search-uplist-for-classkey paren-state))))
7708
d9e94c22 7709(defun c-just-after-func-arglist-p (&optional lim)
0386b551
AM
7710 ;; Return non-nil if the point is in the region after the argument
7711 ;; list of a function and its opening brace (or semicolon in case it
7712 ;; got no body). If there are K&R style argument declarations in
7713 ;; that region, the point has to be inside the first one for this
7714 ;; function to recognize it.
a66cd3ee 7715 ;;
0386b551
AM
7716 ;; If successful, the point is moved to the first token after the
7717 ;; function header (see `c-forward-decl-or-cast-1' for details) and
7718 ;; the position of the opening paren of the function arglist is
7719 ;; returned.
7720 ;;
7721 ;; The point is clobbered if not successful.
7722 ;;
7723 ;; LIM is used as bound for backward buffer searches.
7724 ;;
7725 ;; This function might do hidden buffer changes.
7726
7727 (let ((beg (point)) end id-start)
7728 (and
7729 (eq (c-beginning-of-statement-1 lim) 'same)
7730
2e492df3
AM
7731 (not (and (c-major-mode-is 'objc-mode)
7732 (c-forward-objc-directive)))
0386b551
AM
7733
7734 (setq id-start
7735 (car-safe (c-forward-decl-or-cast-1 (c-point 'bosws) nil nil)))
7736 (< id-start beg)
7737
7738 ;; There should not be a '=' or ',' between beg and the
7739 ;; start of the declaration since that means we were in the
7740 ;; "expression part" of the declaration.
7741 (or (> (point) beg)
7742 (not (looking-at "[=,]")))
7743
7744 (save-excursion
7745 ;; Check that there's an arglist paren in the
7746 ;; declaration.
7747 (goto-char id-start)
7748 (cond ((eq (char-after) ?\()
7749 ;; The declarator is a paren expression, so skip past it
7750 ;; so that we don't get stuck on that instead of the
7751 ;; function arglist.
7752 (c-forward-sexp))
51c9af45
AM
7753 ((and c-opt-op-identifier-prefix
7754 (looking-at c-opt-op-identifier-prefix))
0386b551
AM
7755 ;; Don't trip up on "operator ()".
7756 (c-forward-token-2 2 t)))
7757 (and (< (point) beg)
7758 (c-syntactic-re-search-forward "(" beg t t)
7759 (1- (point)))))))
785eecbb 7760
a66cd3ee
MS
7761(defun c-in-knr-argdecl (&optional lim)
7762 ;; Return the position of the first argument declaration if point is
7763 ;; inside a K&R style argument declaration list, nil otherwise.
7764 ;; `c-recognize-knr-p' is not checked. If LIM is non-nil, it's a
7765 ;; position that bounds the backward search for the argument list.
7766 ;;
9cf17ef1
AM
7767 ;; Point must be within a possible K&R region, e.g. just before a top-level
7768 ;; "{". It must be outside of parens and brackets. The test can return
7769 ;; false positives otherwise.
0386b551
AM
7770 ;;
7771 ;; This function might do hidden buffer changes.
d9e94c22 7772
a66cd3ee
MS
7773 (save-excursion
7774 (save-restriction
9cf17ef1
AM
7775 ;; If we're in a macro, our search range is restricted to it. Narrow to
7776 ;; the searchable range.
98d7371e
AM
7777 (let* ((macro-start (save-excursion (and (c-beginning-of-macro) (point))))
7778 (macro-end (save-excursion (and macro-start (c-end-of-macro) (point))))
7779 (low-lim (max (or lim (point-min)) (or macro-start (point-min))))
6b6481ed 7780 before-lparen after-rparen
98d7371e
AM
7781 (pp-count-out 20)) ; Max number of paren/brace constructs before
7782 ; we give up
7783 (narrow-to-region low-lim (or macro-end (point-max)))
9cf17ef1
AM
7784
7785 ;; Search backwards for the defun's argument list. We give up if we
98d7371e
AM
7786 ;; encounter a "}" (end of a previous defun) an "=" (which can't be in
7787 ;; a knr region) or BOB.
9cf17ef1
AM
7788 ;;
7789 ;; The criterion for a paren structure being the arg list is:
7790 ;; o - there is non-WS stuff after it but before any "{"; AND
7791 ;; o - the token after it isn't a ";" AND
7792 ;; o - it is preceded by either an identifier (the function name) or
7793 ;; a macro expansion like "DEFUN (...)"; AND
7794 ;; o - its content is a non-empty comma-separated list of identifiers
7795 ;; (an empty arg list won't have a knr region).
7796 ;;
7797 ;; The following snippet illustrates these rules:
7798 ;; int foo (bar, baz, yuk)
7799 ;; int bar [] ;
7800 ;; int (*baz) (my_type) ;
7801 ;; int (*) (void) (*yuk) (void) ;
7802 ;; {
7803
7804 (catch 'knr
6b6481ed
AM
7805 (while (> pp-count-out 0) ; go back one paren/bracket pair each time.
7806 (setq pp-count-out (1- pp-count-out))
98d7371e 7807 (c-syntactic-skip-backward "^)]}=")
9cf17ef1
AM
7808 (cond ((eq (char-before) ?\))
7809 (setq after-rparen (point)))
10489fcb
AM
7810 ((eq (char-before) ?\])
7811 (setq after-rparen nil))
98d7371e
AM
7812 (t ; either } (hit previous defun) or = or no more
7813 ; parens/brackets.
10489fcb 7814 (throw 'knr nil)))
9cf17ef1
AM
7815
7816 (if after-rparen
7817 ;; We're inside a paren. Could it be our argument list....?
7818 (if
7819 (and
7820 (progn
7821 (goto-char after-rparen)
7822 (unless (c-go-list-backward) (throw 'knr nil)) ;
7823 ;; FIXME!!! What about macros between the parens? 2007/01/20
7824 (setq before-lparen (point)))
d9e94c22 7825
9cf17ef1
AM
7826 ;; It can't be the arg list if next token is ; or {
7827 (progn (goto-char after-rparen)
7828 (c-forward-syntactic-ws)
98d7371e 7829 (not (memq (char-after) '(?\; ?\{ ?\=))))
d9e94c22 7830
9cf17ef1
AM
7831 ;; Is the thing preceding the list an identifier (the
7832 ;; function name), or a macro expansion?
7833 (progn
7834 (goto-char before-lparen)
7835 (eq (c-backward-token-2) 0)
98d7371e 7836 (or (eq (c-on-identifier) (point))
9cf17ef1
AM
7837 (and (eq (char-after) ?\))
7838 (c-go-up-list-backward)
7839 (eq (c-backward-token-2) 0)
98d7371e 7840 (eq (c-on-identifier) (point)))))
9cf17ef1
AM
7841
7842 ;; Have we got a non-empty list of comma-separated
7843 ;; identifiers?
7844 (progn
7845 (goto-char before-lparen)
7846 (c-forward-token-2) ; to first token inside parens
7847 (and
7848 (c-on-identifier)
7849 (c-forward-token-2)
7850 (catch 'id-list
7851 (while (eq (char-after) ?\,)
7852 (c-forward-token-2)
7853 (unless (c-on-identifier) (throw 'id-list nil))
7854 (c-forward-token-2))
7855 (eq (char-after) ?\))))))
7856
7857 ;; ...Yes. We've identified the function's argument list.
7858 (throw 'knr
7859 (progn (goto-char after-rparen)
7860 (c-forward-syntactic-ws)
7861 (point)))
7862
7863 ;; ...No. The current parens aren't the function's arg list.
7864 (goto-char before-lparen))
7865
7866 (or (c-go-list-backward) ; backwards over [ .... ]
7867 (throw 'knr nil)))))))))
785eecbb
RS
7868
7869(defun c-skip-conditional ()
7870 ;; skip forward over conditional at point, including any predicate
7871 ;; statements in parentheses. No error checking is performed.
0386b551
AM
7872 ;;
7873 ;; This function might do hidden buffer changes.
0ec8351b
BW
7874 (c-forward-sexp (cond
7875 ;; else if()
a66cd3ee
MS
7876 ((looking-at (concat "\\<else"
7877 "\\([ \t\n]\\|\\\\\n\\)+"
7878 "if\\>\\([^_]\\|$\\)"))
7879 3)
0ec8351b 7880 ;; do, else, try, finally
a66cd3ee
MS
7881 ((looking-at (concat "\\<\\("
7882 "do\\|else\\|try\\|finally"
7883 "\\)\\>\\([^_]\\|$\\)"))
130c507e 7884 1)
ce8c7486 7885 ;; for, if, while, switch, catch, synchronized, foreach
0ec8351b 7886 (t 2))))
785eecbb 7887
a66cd3ee
MS
7888(defun c-after-conditional (&optional lim)
7889 ;; If looking at the token after a conditional then return the
7890 ;; position of its start, otherwise return nil.
0386b551
AM
7891 ;;
7892 ;; This function might do hidden buffer changes.
a66cd3ee 7893 (save-excursion
d9e94c22 7894 (and (zerop (c-backward-token-2 1 t lim))
a66cd3ee
MS
7895 (or (looking-at c-block-stmt-1-key)
7896 (and (eq (char-after) ?\()
d9e94c22 7897 (zerop (c-backward-token-2 1 t lim))
a66cd3ee
MS
7898 (looking-at c-block-stmt-2-key)))
7899 (point))))
7900
0386b551
AM
7901(defun c-after-special-operator-id (&optional lim)
7902 ;; If the point is after an operator identifier that isn't handled
7903 ;; like an ordinary symbol (i.e. like "operator =" in C++) then the
7904 ;; position of the start of that identifier is returned. nil is
7905 ;; returned otherwise. The point may be anywhere in the syntactic
7906 ;; whitespace after the last token of the operator identifier.
7907 ;;
7908 ;; This function might do hidden buffer changes.
7909 (save-excursion
7910 (and c-overloadable-operators-regexp
7911 (zerop (c-backward-token-2 1 nil lim))
7912 (looking-at c-overloadable-operators-regexp)
51c9af45 7913 (or (not c-opt-op-identifier-prefix)
0386b551
AM
7914 (and
7915 (zerop (c-backward-token-2 1 nil lim))
51c9af45 7916 (looking-at c-opt-op-identifier-prefix)))
0386b551
AM
7917 (point))))
7918
a66cd3ee
MS
7919(defsubst c-backward-to-block-anchor (&optional lim)
7920 ;; Assuming point is at a brace that opens a statement block of some
7921 ;; kind, move to the proper anchor point for that block. It might
7922 ;; need to be adjusted further by c-add-stmt-syntax, but the
7923 ;; position at return is suitable as start position for that
7924 ;; function.
0386b551
AM
7925 ;;
7926 ;; This function might do hidden buffer changes.
a66cd3ee
MS
7927 (unless (= (point) (c-point 'boi))
7928 (let ((start (c-after-conditional lim)))
7929 (if start
7930 (goto-char start)))))
7931
037558bf 7932(defsubst c-backward-to-decl-anchor (&optional lim)
a66cd3ee
MS
7933 ;; Assuming point is at a brace that opens the block of a top level
7934 ;; declaration of some kind, move to the proper anchor point for
7935 ;; that block.
0386b551
AM
7936 ;;
7937 ;; This function might do hidden buffer changes.
a66cd3ee 7938 (unless (= (point) (c-point 'boi))
037558bf 7939 (c-beginning-of-statement-1 lim)))
a66cd3ee 7940
ff959bab 7941(defun c-search-decl-header-end ()
a66cd3ee
MS
7942 ;; Search forward for the end of the "header" of the current
7943 ;; declaration. That's the position where the definition body
7944 ;; starts, or the first variable initializer, or the ending
7945 ;; semicolon. I.e. search forward for the closest following
7946 ;; (syntactically relevant) '{', '=' or ';' token. Point is left
7947 ;; _after_ the first found token, or at point-max if none is found.
0386b551
AM
7948 ;;
7949 ;; This function might do hidden buffer changes.
ff959bab
MS
7950
7951 (let ((base (point)))
7952 (if (c-major-mode-is 'c++-mode)
7953
7954 ;; In C++ we need to take special care to handle operator
7955 ;; tokens and those pesky template brackets.
7956 (while (and
7957 (c-syntactic-re-search-forward "[;{<=]" nil 'move t t)
7958 (or
7959 (c-end-of-current-token base)
7960 ;; Handle operator identifiers, i.e. ignore any
7961 ;; operator token preceded by "operator".
7962 (save-excursion
7963 (and (c-safe (c-backward-sexp) t)
51c9af45 7964 (looking-at c-opt-op-identifier-prefix)))
ff959bab
MS
7965 (and (eq (char-before) ?<)
7966 (c-with-syntax-table c++-template-syntax-table
7967 (if (c-safe (goto-char (c-up-list-forward (point))))
7968 t
7969 (goto-char (point-max))
7970 nil)))))
7971 (setq base (point)))
7972
7973 (while (and
7974 (c-syntactic-re-search-forward "[;{=]" nil 'move t t)
7975 (c-end-of-current-token base))
7976 (setq base (point))))))
a66cd3ee
MS
7977
7978(defun c-beginning-of-decl-1 (&optional lim)
7979 ;; Go to the beginning of the current declaration, or the beginning
7980 ;; of the previous one if already at the start of it. Point won't
0386b551 7981 ;; be moved out of any surrounding paren. Return a cons cell of the
a66cd3ee
MS
7982 ;; form (MOVE . KNR-POS). MOVE is like the return value from
7983 ;; `c-beginning-of-statement-1'. If point skipped over some K&R
7984 ;; style argument declarations (and they are to be recognized) then
7985 ;; KNR-POS is set to the start of the first such argument
7986 ;; declaration, otherwise KNR-POS is nil. If LIM is non-nil, it's a
7987 ;; position that bounds the backward search.
7988 ;;
7989 ;; NB: Cases where the declaration continues after the block, as in
7990 ;; "struct foo { ... } bar;", are currently recognized as two
7991 ;; declarations, e.g. "struct foo { ... }" and "bar;" in this case.
0386b551
AM
7992 ;;
7993 ;; This function might do hidden buffer changes.
a66cd3ee
MS
7994 (catch 'return
7995 (let* ((start (point))
d9e94c22 7996 (last-stmt-start (point))
0386b551 7997 (move (c-beginning-of-statement-1 lim nil t)))
a66cd3ee 7998
a66cd3ee
MS
7999 ;; `c-beginning-of-statement-1' stops at a block start, but we
8000 ;; want to continue if the block doesn't begin a top level
2a15eb73
MS
8001 ;; construct, i.e. if it isn't preceded by ';', '}', ':', bob,
8002 ;; or an open paren.
d9e94c22 8003 (let ((beg (point)) tentative-move)
51c9af45
AM
8004 ;; Go back one "statement" each time round the loop until we're just
8005 ;; after a ;, }, or :, or at BOB or the start of a macro or start of
8006 ;; an ObjC method. This will move over a multiple declaration whose
8007 ;; components are comma separated.
d9e94c22
MS
8008 (while (and
8009 ;; Must check with c-opt-method-key in ObjC mode.
8010 (not (and c-opt-method-key
8011 (looking-at c-opt-method-key)))
8012 (/= last-stmt-start (point))
8013 (progn
8014 (c-backward-syntactic-ws lim)
8015 (not (memq (char-before) '(?\; ?} ?: nil))))
2a15eb73
MS
8016 (save-excursion
8017 (backward-char)
8018 (not (looking-at "\\s(")))
d9e94c22
MS
8019 ;; Check that we don't move from the first thing in a
8020 ;; macro to its header.
8021 (not (eq (setq tentative-move
0386b551 8022 (c-beginning-of-statement-1 lim nil t))
d9e94c22
MS
8023 'macro)))
8024 (setq last-stmt-start beg
8025 beg (point)
8026 move tentative-move))
8027 (goto-char beg))
8028
8029 (when c-recognize-knr-p
8030 (let ((fallback-pos (point)) knr-argdecl-start)
8031 ;; Handle K&R argdecls. Back up after the "statement" jumped
8032 ;; over by `c-beginning-of-statement-1', unless it was the
8033 ;; function body, in which case we're sitting on the opening
8034 ;; brace now. Then test if we're in a K&R argdecl region and
8035 ;; that we started at the other side of the first argdecl in
8036 ;; it.
8037 (unless (eq (char-after) ?{)
8038 (goto-char last-stmt-start))
8039 (if (and (setq knr-argdecl-start (c-in-knr-argdecl lim))
8040 (< knr-argdecl-start start)
8041 (progn
8042 (goto-char knr-argdecl-start)
0386b551 8043 (not (eq (c-beginning-of-statement-1 lim nil t) 'macro))))
d9e94c22
MS
8044 (throw 'return
8045 (cons (if (eq (char-after fallback-pos) ?{)
8046 'previous
8047 'same)
8048 knr-argdecl-start))
8049 (goto-char fallback-pos))))
8050
51c9af45
AM
8051 ;; `c-beginning-of-statement-1' counts each brace block as a separate
8052 ;; statement, so the result will be 'previous if we've moved over any.
8053 ;; So change our result back to 'same if necessary.
8054 ;;
8055 ;; If they were brace list initializers we might not have moved over a
8056 ;; declaration boundary though, so change it to 'same if we've moved
8057 ;; past a '=' before '{', but not ';'. (This ought to be integrated
8058 ;; into `c-beginning-of-statement-1', so we avoid this extra pass which
8059 ;; potentially can search over a large amount of text.). Take special
8060 ;; pains not to get mislead by C++'s "operator=", and the like.
d9e94c22
MS
8061 (if (and (eq move 'previous)
8062 (c-with-syntax-table (if (c-major-mode-is 'c++-mode)
8063 c++-template-syntax-table
8064 (syntax-table))
8065 (save-excursion
51c9af45
AM
8066 (and
8067 (progn
8068 (while ; keep going back to "[;={"s until we either find
8069 ; no more, or get to one which isn't an "operator ="
8070 (and (c-syntactic-re-search-forward "[;={]" start t t t)
8071 (eq (char-before) ?=)
8072 c-overloadable-operators-regexp
8073 c-opt-op-identifier-prefix
8074 (save-excursion
8075 (eq (c-backward-token-2) 0)
8076 (looking-at c-overloadable-operators-regexp)
8077 (eq (c-backward-token-2) 0)
8078 (looking-at c-opt-op-identifier-prefix))))
8079 (eq (char-before) ?=))
8080 (c-syntactic-re-search-forward "[;{]" start t t)
8081 (eq (char-before) ?{)
8082 (c-safe (goto-char (c-up-list-forward (point))) t)
8083 (not (c-syntactic-re-search-forward ";" start t t))))))
d9e94c22
MS
8084 (cons 'same nil)
8085 (cons move nil)))))
a66cd3ee
MS
8086
8087(defun c-end-of-decl-1 ()
8088 ;; Assuming point is at the start of a declaration (as detected by
8089 ;; e.g. `c-beginning-of-decl-1'), go to the end of it. Unlike
8090 ;; `c-beginning-of-decl-1', this function handles the case when a
8091 ;; block is followed by identifiers in e.g. struct declarations in C
8092 ;; or C++. If a proper end was found then t is returned, otherwise
8093 ;; point is moved as far as possible within the current sexp and nil
8094 ;; is returned. This function doesn't handle macros; use
8095 ;; `c-end-of-macro' instead in those cases.
0386b551
AM
8096 ;;
8097 ;; This function might do hidden buffer changes.
ce8c7486 8098 (let ((start (point))
a66cd3ee
MS
8099 (decl-syntax-table (if (c-major-mode-is 'c++-mode)
8100 c++-template-syntax-table
8101 (syntax-table))))
8102 (catch 'return
8103 (c-search-decl-header-end)
8104
8105 (when (and c-recognize-knr-p
8106 (eq (char-before) ?\;)
8107 (c-in-knr-argdecl start))
8108 ;; Stopped at the ';' in a K&R argdecl section which is
8109 ;; detected using the same criteria as in
8110 ;; `c-beginning-of-decl-1'. Move to the following block
8111 ;; start.
d9e94c22 8112 (c-syntactic-re-search-forward "{" nil 'move t))
a66cd3ee
MS
8113
8114 (when (eq (char-before) ?{)
8115 ;; Encountered a block in the declaration. Jump over it.
8116 (condition-case nil
8117 (goto-char (c-up-list-forward (point)))
d9e94c22
MS
8118 (error (goto-char (point-max))
8119 (throw 'return nil)))
a66cd3ee
MS
8120 (if (or (not c-opt-block-decls-with-vars-key)
8121 (save-excursion
8122 (c-with-syntax-table decl-syntax-table
8123 (let ((lim (point)))
8124 (goto-char start)
b3cf7e18
MS
8125 (not (and
8126 ;; Check for `c-opt-block-decls-with-vars-key'
8127 ;; before the first paren.
8128 (c-syntactic-re-search-forward
d9e94c22 8129 (concat "[;=\(\[{]\\|\\("
b3cf7e18
MS
8130 c-opt-block-decls-with-vars-key
8131 "\\)")
d9e94c22 8132 lim t t t)
b3cf7e18
MS
8133 (match-beginning 1)
8134 (not (eq (char-before) ?_))
d9e94c22
MS
8135 ;; Check that the first following paren is
8136 ;; the block.
8137 (c-syntactic-re-search-forward "[;=\(\[{]"
8138 lim t t t)
b3cf7e18 8139 (eq (char-before) ?{)))))))
a66cd3ee
MS
8140 ;; The declaration doesn't have any of the
8141 ;; `c-opt-block-decls-with-vars' keywords in the
8142 ;; beginning, so it ends here at the end of the block.
8143 (throw 'return t)))
8144
8145 (c-with-syntax-table decl-syntax-table
8146 (while (progn
8147 (if (eq (char-before) ?\;)
8148 (throw 'return t))
d9e94c22 8149 (c-syntactic-re-search-forward ";" nil 'move t))))
a66cd3ee 8150 nil)))
ce8c7486 8151
0386b551
AM
8152(defun c-looking-at-decl-block (containing-sexp goto-start &optional limit)
8153 ;; Assuming the point is at an open brace, check if it starts a
8154 ;; block that contains another declaration level, i.e. that isn't a
8155 ;; statement block or a brace list, and if so return non-nil.
8156 ;;
8157 ;; If the check is successful, the return value is the start of the
8158 ;; keyword that tells what kind of construct it is, i.e. typically
8159 ;; what `c-decl-block-key' matched. Also, if GOTO-START is set then
8160 ;; the point will be at the start of the construct, before any
8161 ;; leading specifiers, otherwise it's at the returned position.
8162 ;;
8163 ;; The point is clobbered if the check is unsuccessful.
8164 ;;
8165 ;; CONTAINING-SEXP is the position of the open of the surrounding
8166 ;; paren, or nil if none.
8167 ;;
8168 ;; The optional LIMIT limits the backward search for the start of
8169 ;; the construct. It's assumed to be at a syntactically relevant
8170 ;; position.
8171 ;;
8172 ;; If any template arglists are found in the searched region before
8173 ;; the open brace, they get marked with paren syntax.
8174 ;;
8175 ;; This function might do hidden buffer changes.
8176
8177 (let ((open-brace (point)) kwd-start first-specifier-pos)
8178 (c-syntactic-skip-backward c-block-prefix-charset limit t)
8179
8180 (when (and c-recognize-<>-arglists
8181 (eq (char-before) ?>))
8182 ;; Could be at the end of a template arglist.
8183 (let ((c-parse-and-markup-<>-arglists t)
8184 (c-disallow-comma-in-<>-arglists
8185 (and containing-sexp
8186 (not (eq (char-after containing-sexp) ?{)))))
8187 (while (and
8188 (c-backward-<>-arglist nil limit)
8189 (progn
8190 (c-syntactic-skip-backward c-block-prefix-charset limit t)
8191 (eq (char-before) ?>))))))
8192
8193 ;; Note: Can't get bogus hits inside template arglists below since they
8194 ;; have gotten paren syntax above.
8195 (when (and
8196 ;; If `goto-start' is set we begin by searching for the
8197 ;; first possible position of a leading specifier list.
8198 ;; The `c-decl-block-key' search continues from there since
8199 ;; we know it can't match earlier.
8200 (if goto-start
8201 (when (c-syntactic-re-search-forward c-symbol-start
8202 open-brace t t)
8203 (goto-char (setq first-specifier-pos (match-beginning 0)))
8204 t)
8205 t)
8206
8207 (cond
8208 ((c-syntactic-re-search-forward c-decl-block-key open-brace t t t)
8209 (goto-char (setq kwd-start (match-beginning 0)))
8210 (or
8211
8212 ;; Found a keyword that can't be a type?
8213 (match-beginning 1)
8214
8215 ;; Can be a type too, in which case it's the return type of a
8216 ;; function (under the assumption that no declaration level
8217 ;; block construct starts with a type).
8218 (not (c-forward-type))
8219
8220 ;; Jumped over a type, but it could be a declaration keyword
8221 ;; followed by the declared identifier that we've jumped over
8222 ;; instead (e.g. in "class Foo {"). If it indeed is a type
8223 ;; then we should be at the declarator now, so check for a
8224 ;; valid declarator start.
8225 ;;
8226 ;; Note: This doesn't cope with the case when a declared
8227 ;; identifier is followed by e.g. '(' in a language where '('
8228 ;; also might be part of a declarator expression. Currently
8229 ;; there's no such language.
8230 (not (or (looking-at c-symbol-start)
8231 (looking-at c-type-decl-prefix-key)))))
8232
8233 ;; In Pike a list of modifiers may be followed by a brace
8234 ;; to make them apply to many identifiers. Note that the
8235 ;; match data will be empty on return in this case.
8236 ((and (c-major-mode-is 'pike-mode)
8237 (progn
8238 (goto-char open-brace)
8239 (= (c-backward-token-2) 0))
8240 (looking-at c-specifier-key)
8241 ;; Use this variant to avoid yet another special regexp.
8242 (c-keyword-member (c-keyword-sym (match-string 1))
8243 'c-modifier-kwds))
8244 (setq kwd-start (point))
8245 t)))
8246
8247 ;; Got a match.
8248
8249 (if goto-start
8250 ;; Back up over any preceding specifiers and their clauses
8251 ;; by going forward from `first-specifier-pos', which is the
8252 ;; earliest possible position where the specifier list can
8253 ;; start.
8254 (progn
8255 (goto-char first-specifier-pos)
8256
8257 (while (< (point) kwd-start)
8258 (if (looking-at c-symbol-key)
8259 ;; Accept any plain symbol token on the ground that
8260 ;; it's a specifier masked through a macro (just
8261 ;; like `c-forward-decl-or-cast-1' skip forward over
8262 ;; such tokens).
8263 ;;
8264 ;; Could be more restrictive wrt invalid keywords,
8265 ;; but that'd only occur in invalid code so there's
8266 ;; no use spending effort on it.
8267 (let ((end (match-end 0)))
8268 (unless (c-forward-keyword-clause 0)
8269 (goto-char end)
8270 (c-forward-syntactic-ws)))
8271
8272 ;; Can't parse a declaration preamble and is still
8273 ;; before `kwd-start'. That means `first-specifier-pos'
8274 ;; was in some earlier construct. Search again.
8275 (if (c-syntactic-re-search-forward c-symbol-start
8276 kwd-start 'move t)
8277 (goto-char (setq first-specifier-pos (match-beginning 0)))
8278 ;; Got no preamble before the block declaration keyword.
8279 (setq first-specifier-pos kwd-start))))
8280
8281 (goto-char first-specifier-pos))
8282 (goto-char kwd-start))
8283
8284 kwd-start)))
ce8c7486 8285
a66cd3ee 8286(defun c-search-uplist-for-classkey (paren-state)
0386b551
AM
8287 ;; Check if the closest containing paren sexp is a declaration
8288 ;; block, returning a 2 element vector in that case. Aref 0
8289 ;; contains the bufpos at boi of the class key line, and aref 1
8290 ;; contains the bufpos of the open brace. This function is an
8291 ;; obsolete wrapper for `c-looking-at-decl-block'.
8292 ;;
8293 ;; This function might do hidden buffer changes.
8294 (let ((open-paren-pos (c-most-enclosing-brace paren-state)))
8295 (when open-paren-pos
8296 (save-excursion
8297 (goto-char open-paren-pos)
8298 (when (and (eq (char-after) ?{)
8299 (c-looking-at-decl-block
8300 (c-safe-position open-paren-pos paren-state)
8301 nil))
8302 (back-to-indentation)
8303 (vector (point) open-paren-pos))))))
785eecbb 8304
020716e1
AM
8305(defmacro c-pull-open-brace (ps)
8306 ;; Pull the next open brace from PS (which has the form of paren-state),
8307 ;; skipping over any brace pairs. Returns NIL when PS is exhausted.
8308 `(progn
8309 (while (consp (car ,ps))
8310 (setq ,ps (cdr ,ps)))
8311 (prog1 (car ,ps)
8312 (setq ,ps (cdr ,ps)))))
8313
8314(defun c-most-enclosing-decl-block (paren-state)
8315 ;; Return the buffer position of the most enclosing decl-block brace (in the
8316 ;; sense of c-looking-at-decl-block) in the PAREN-STATE structure, or nil if
8317 ;; none was found.
8318 (let* ((open-brace (c-pull-open-brace paren-state))
8319 (next-open-brace (c-pull-open-brace paren-state)))
8320 (while (and open-brace
8321 (save-excursion
8322 (goto-char open-brace)
8323 (not (c-looking-at-decl-block next-open-brace nil))))
8324 (setq open-brace next-open-brace
8325 next-open-brace (c-pull-open-brace paren-state)))
8326 open-brace))
8327
9657183b
AM
8328(defun c-cheap-inside-bracelist-p (paren-state)
8329 ;; Return the position of the L-brace if point is inside a brace list
8330 ;; initialization of an array, etc. This is an approximate function,
8331 ;; designed for speed over accuracy. It will not find every bracelist, but
8332 ;; a non-nil result is reliable. We simply search for "= {" (naturally with
8333 ;; syntactic whitespace allowed). PAREN-STATE is the normal thing that it
8334 ;; is everywhere else.
8335 (let (b-pos)
8336 (save-excursion
8337 (while
8338 (and (setq b-pos (c-pull-open-brace paren-state))
8339 (progn (goto-char b-pos)
8340 (c-backward-sws)
8341 (c-backward-token-2)
8342 (not (looking-at "=")))))
8343 b-pos)))
8344
a66cd3ee 8345(defun c-inside-bracelist-p (containing-sexp paren-state)
785eecbb
RS
8346 ;; return the buffer position of the beginning of the brace list
8347 ;; statement if we're inside a brace list, otherwise return nil.
8348 ;; CONTAINING-SEXP is the buffer pos of the innermost containing
0386b551 8349 ;; paren. PAREN-STATE is the remainder of the state of enclosing
130c507e 8350 ;; braces
785eecbb
RS
8351 ;;
8352 ;; N.B.: This algorithm can potentially get confused by cpp macros
0386b551 8353 ;; placed in inconvenient locations. It's a trade-off we make for
785eecbb 8354 ;; speed.
0386b551
AM
8355 ;;
8356 ;; This function might do hidden buffer changes.
785eecbb 8357 (or
d9e94c22 8358 ;; This will pick up brace list declarations.
b2acd789
RS
8359 (c-safe
8360 (save-excursion
8361 (goto-char containing-sexp)
0ec8351b 8362 (c-forward-sexp -1)
b2acd789 8363 (let (bracepos)
d9e94c22 8364 (if (and (or (looking-at c-brace-list-key)
0ec8351b 8365 (progn (c-forward-sexp -1)
d9e94c22 8366 (looking-at c-brace-list-key)))
a66cd3ee 8367 (setq bracepos (c-down-list-forward (point)))
b2acd789
RS
8368 (not (c-crosses-statement-barrier-p (point)
8369 (- bracepos 2))))
8370 (point)))))
785eecbb
RS
8371 ;; this will pick up array/aggregate init lists, even if they are nested.
8372 (save-excursion
0ec8351b
BW
8373 (let ((class-key
8374 ;; Pike can have class definitions anywhere, so we must
8375 ;; check for the class key here.
8376 (and (c-major-mode-is 'pike-mode)
a66cd3ee
MS
8377 c-decl-block-key))
8378 bufpos braceassignp lim next-containing)
785eecbb
RS
8379 (while (and (not bufpos)
8380 containing-sexp)
a66cd3ee
MS
8381 (when paren-state
8382 (if (consp (car paren-state))
8383 (setq lim (cdr (car paren-state))
8384 paren-state (cdr paren-state))
8385 (setq lim (car paren-state)))
8386 (when paren-state
8387 (setq next-containing (car paren-state)
8388 paren-state (cdr paren-state))))
785eecbb 8389 (goto-char containing-sexp)
a66cd3ee
MS
8390 (if (c-looking-at-inexpr-block next-containing next-containing)
8391 ;; We're in an in-expression block of some kind. Do not
8392 ;; check nesting. We deliberately set the limit to the
8393 ;; containing sexp, so that c-looking-at-inexpr-block
8394 ;; doesn't check for an identifier before it.
0ec8351b
BW
8395 (setq containing-sexp nil)
8396 ;; see if the open brace is preceded by = or [...] in
8397 ;; this statement, but watch out for operator=
a66cd3ee 8398 (setq braceassignp 'dontknow)
d9e94c22 8399 (c-backward-token-2 1 t lim)
6393fef2 8400 ;; Checks to do only on the first sexp before the brace.
d9e94c22 8401 (when (and c-opt-inexpr-brace-list-key
6393fef2
RS
8402 (eq (char-after) ?\[))
8403 ;; In Java, an initialization brace list may follow
8404 ;; directly after "new Foo[]", so check for a "new"
8405 ;; earlier.
8406 (while (eq braceassignp 'dontknow)
8407 (setq braceassignp
d9e94c22
MS
8408 (cond ((/= (c-backward-token-2 1 t lim) 0) nil)
8409 ((looking-at c-opt-inexpr-brace-list-key) t)
6393fef2
RS
8410 ((looking-at "\\sw\\|\\s_\\|[.[]")
8411 ;; Carry on looking if this is an
8412 ;; identifier (may contain "." in Java)
8413 ;; or another "[]" sexp.
8414 'dontknow)
8415 (t nil)))))
8416 ;; Checks to do on all sexps before the brace, up to the
8417 ;; beginning of the statement.
8418 (while (eq braceassignp 'dontknow)
0ec8351b
BW
8419 (cond ((eq (char-after) ?\;)
8420 (setq braceassignp nil))
8421 ((and class-key
8422 (looking-at class-key))
8423 (setq braceassignp nil))
8424 ((eq (char-after) ?=)
8425 ;; We've seen a =, but must check earlier tokens so
8426 ;; that it isn't something that should be ignored.
8427 (setq braceassignp 'maybe)
8428 (while (and (eq braceassignp 'maybe)
d9e94c22 8429 (zerop (c-backward-token-2 1 t lim)))
0ec8351b
BW
8430 (setq braceassignp
8431 (cond
8432 ;; Check for operator =
51c9af45
AM
8433 ((and c-opt-op-identifier-prefix
8434 (looking-at c-opt-op-identifier-prefix))
0386b551 8435 nil)
130c507e
GM
8436 ;; Check for `<opchar>= in Pike.
8437 ((and (c-major-mode-is 'pike-mode)
8438 (or (eq (char-after) ?`)
8439 ;; Special case for Pikes
8440 ;; `[]=, since '[' is not in
8441 ;; the punctuation class.
8442 (and (eq (char-after) ?\[)
8443 (eq (char-before) ?`))))
8444 nil)
0ec8351b
BW
8445 ((looking-at "\\s.") 'maybe)
8446 ;; make sure we're not in a C++ template
8447 ;; argument assignment
a66cd3ee
MS
8448 ((and
8449 (c-major-mode-is 'c++-mode)
8450 (save-excursion
8451 (let ((here (point))
8452 (pos< (progn
8453 (skip-chars-backward "^<>")
8454 (point))))
8455 (and (eq (char-before) ?<)
8456 (not (c-crosses-statement-barrier-p
8457 pos< here))
8458 (not (c-in-literal))
8459 ))))
0ec8351b 8460 nil)
6393fef2
RS
8461 (t t))))))
8462 (if (and (eq braceassignp 'dontknow)
d9e94c22 8463 (/= (c-backward-token-2 1 t lim) 0))
6393fef2
RS
8464 (setq braceassignp nil)))
8465 (if (not braceassignp)
0ec8351b
BW
8466 (if (eq (char-after) ?\;)
8467 ;; Brace lists can't contain a semicolon, so we're done.
8468 (setq containing-sexp nil)
a66cd3ee
MS
8469 ;; Go up one level.
8470 (setq containing-sexp next-containing
8471 lim nil
8472 next-containing nil))
0ec8351b
BW
8473 ;; we've hit the beginning of the aggregate list
8474 (c-beginning-of-statement-1
a66cd3ee 8475 (c-most-enclosing-brace paren-state))
0ec8351b 8476 (setq bufpos (point))))
a66cd3ee 8477 )
785eecbb
RS
8478 bufpos))
8479 ))
8480
0ec8351b
BW
8481(defun c-looking-at-special-brace-list (&optional lim)
8482