]> code.delx.au - gnu-emacs/blob - lisp/progmodes/cc-engine.el
Update copyright year to 2015
[gnu-emacs] / lisp / progmodes / cc-engine.el
1 ;;; cc-engine.el --- core syntax guessing engine for CC mode -*- coding: utf-8 -*-
2
3 ;; Copyright (C) 1985, 1987, 1992-2015 Free Software Foundation, Inc.
4
5 ;; Authors: 2001- Alan Mackenzie
6 ;; 1998- Martin Stjernholm
7 ;; 1992-1999 Barry A. Warsaw
8 ;; 1987 Dave Detlefs
9 ;; 1987 Stewart Clamen
10 ;; 1985 Richard M. Stallman
11 ;; Maintainer: bug-cc-mode@gnu.org
12 ;; Created: 22-Apr-1997 (split from cc-mode.el)
13 ;; Keywords: c languages
14 ;; Package: cc-mode
15
16 ;; This file is part of GNU Emacs.
17
18 ;; GNU Emacs is free software: you can redistribute it and/or modify
19 ;; it under the terms of the GNU General Public License as published by
20 ;; the Free Software Foundation, either version 3 of the License, or
21 ;; (at your option) any later version.
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
29 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
30
31 ;;; Commentary:
32
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
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
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.
51 ;;
52 ;; Interactive functions are assumed to not do hidden buffer changes,
53 ;; except in the specific parts of them that do real changes.
54 ;;
55 ;; Lineup functions are assumed to do hidden buffer changes. They
56 ;; must not do real changes, though.
57 ;;
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).
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 ;;
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 ;;
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 ;;
84 ;; 'syntax-table
85 ;; Used to modify the syntax of some characters. It is used to
86 ;; mark the "<" and ">" of angle bracket parens with paren syntax, and
87 ;; to "hide" obtrusive characters in preprocessor lines.
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
102 ;; special syntactic relevance of various sorts. Its primary use is
103 ;; to avoid glitches when multiline constructs are refontified
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
136 ;;; Code:
137
138 (eval-when-compile
139 (let ((load-path
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)
143 load-path)))
144 (load "cc-bytecomp" nil t)))
145
146 (cc-require 'cc-defs)
147 (cc-require-when-compile 'cc-langs)
148 (cc-require 'cc-vars)
149
150 \f
151 ;; Make declarations for all the `c-lang-defvar' variables in cc-langs.
152
153 (defmacro c-declare-lang-variables ()
154 `(progn
155 ,@(apply 'nconc
156 (mapcar (lambda (init)
157 `(,(if (elt init 2)
158 `(defvar ,(car init) nil ,(elt init 2))
159 `(defvar ,(car init) nil))
160 (make-variable-buffer-local ',(car init))))
161 (cdr c-lang-variable-inits)))))
162 (c-declare-lang-variables)
163
164 \f
165 ;;; Internal state variables.
166
167 ;; Internal state of hungry delete key feature
168 (defvar c-hungry-delete-key nil)
169 (make-variable-buffer-local 'c-hungry-delete-key)
170
171 ;; The electric flag (toggled by `c-toggle-electric-state').
172 ;; If t, electric actions (like automatic reindentation, and (if
173 ;; c-auto-newline is also set) auto newlining) will happen when an electric
174 ;; key like `{' is pressed (or an electric keyword like `else').
175 (defvar c-electric-flag t)
176 (make-variable-buffer-local 'c-electric-flag)
177
178 ;; Internal state of auto newline feature.
179 (defvar c-auto-newline nil)
180 (make-variable-buffer-local 'c-auto-newline)
181
182 ;; Included in the mode line to indicate the active submodes.
183 ;; (defvar c-submode-indicators nil)
184 ;; (make-variable-buffer-local 'c-submode-indicators)
185
186 (defun c-calculate-state (arg prevstate)
187 ;; Calculate the new state of PREVSTATE, t or nil, based on arg. If
188 ;; arg is nil or zero, toggle the state. If arg is negative, turn
189 ;; the state off, and if arg is positive, turn the state on
190 (if (or (not arg)
191 (zerop (setq arg (prefix-numeric-value arg))))
192 (not prevstate)
193 (> arg 0)))
194
195 \f
196 ;; Basic handling of preprocessor directives.
197
198 ;; This is a dynamically bound cache used together with
199 ;; `c-query-macro-start' and `c-query-and-set-macro-start'. It only
200 ;; works as long as point doesn't cross a macro boundary.
201 (defvar c-macro-start 'unknown)
202
203 (defsubst c-query-and-set-macro-start ()
204 (if (symbolp c-macro-start)
205 (setq c-macro-start (save-excursion
206 (c-save-buffer-state ()
207 (and (c-beginning-of-macro)
208 (point)))))
209 c-macro-start))
210
211 (defsubst c-query-macro-start ()
212 (if (symbolp c-macro-start)
213 (save-excursion
214 (c-save-buffer-state ()
215 (and (c-beginning-of-macro)
216 (point))))
217 c-macro-start))
218
219 ;; One element macro cache to cope with continual movement within very large
220 ;; CPP macros.
221 (defvar c-macro-cache nil)
222 (make-variable-buffer-local 'c-macro-cache)
223 ;; Nil or cons of the bounds of the most recent CPP form probed by
224 ;; `c-beginning-of-macro', `c-end-of-macro' or `c-syntactic-end-of-macro'.
225 ;; The cdr will be nil if we know only the start of the CPP form.
226 (defvar c-macro-cache-start-pos nil)
227 (make-variable-buffer-local 'c-macro-cache-start-pos)
228 ;; The starting position from where we determined `c-macro-cache'.
229 (defvar c-macro-cache-syntactic nil)
230 (make-variable-buffer-local 'c-macro-cache-syntactic)
231 ;; non-nil iff `c-macro-cache' has both elements set AND the cdr is at a
232 ;; syntactic end of macro, not merely an apparent one.
233
234 (defun c-invalidate-macro-cache (beg end)
235 ;; Called from a before-change function. If the change region is before or
236 ;; in the macro characterized by `c-macro-cache' etc., nullify it
237 ;; appropriately. BEG and END are the standard before-change-functions
238 ;; parameters. END isn't used.
239 (cond
240 ((null c-macro-cache))
241 ((< beg (car c-macro-cache))
242 (setq c-macro-cache nil
243 c-macro-cache-start-pos nil
244 c-macro-cache-syntactic nil))
245 ((and (cdr c-macro-cache)
246 (< beg (cdr c-macro-cache)))
247 (setcdr c-macro-cache nil)
248 (setq c-macro-cache-start-pos beg
249 c-macro-cache-syntactic nil))))
250
251 (defun c-macro-is-genuine-p ()
252 ;; Check that the ostensible CPP construct at point is a real one. In
253 ;; particular, if point is on the first line of a narrowed buffer, make sure
254 ;; that the "#" isn't, say, the second character of a "##" operator. Return
255 ;; t when the macro is real, nil otherwise.
256 (let ((here (point)))
257 (beginning-of-line)
258 (prog1
259 (if (and (eq (point) (point-min))
260 (/= (point) 1))
261 (save-restriction
262 (widen)
263 (beginning-of-line)
264 (and (looking-at c-anchored-cpp-prefix)
265 (eq (match-beginning 1) here)))
266 t)
267 (goto-char here))))
268
269 (defun c-beginning-of-macro (&optional lim)
270 "Go to the beginning of a preprocessor directive.
271 Leave point at the beginning of the directive and return t if in one,
272 otherwise return nil and leave point unchanged.
273
274 Note that this function might do hidden buffer changes. See the
275 comment at the start of cc-engine.el for more info."
276 (let ((here (point)))
277 (when c-opt-cpp-prefix
278 (if (and (car c-macro-cache)
279 (>= (point) (car c-macro-cache))
280 (or (and (cdr c-macro-cache)
281 (<= (point) (cdr c-macro-cache)))
282 (<= (point) c-macro-cache-start-pos)))
283 (unless (< (car c-macro-cache) (or lim (point-min)))
284 (progn (goto-char (max (or lim (point-min)) (car c-macro-cache)))
285 (setq c-macro-cache-start-pos
286 (max c-macro-cache-start-pos here))
287 t))
288 (setq c-macro-cache nil
289 c-macro-cache-start-pos nil
290 c-macro-cache-syntactic nil)
291
292 (save-restriction
293 (if lim (narrow-to-region lim (point-max)))
294 (beginning-of-line)
295 (while (eq (char-before (1- (point))) ?\\)
296 (forward-line -1))
297 (back-to-indentation)
298 (if (and (<= (point) here)
299 (looking-at c-opt-cpp-start)
300 (c-macro-is-genuine-p))
301 (progn
302 (setq c-macro-cache (cons (point) nil)
303 c-macro-cache-start-pos here)
304 t)
305 (goto-char here)
306 nil))))))
307
308 (defun c-end-of-macro ()
309 "Go to the end of a preprocessor directive.
310 More accurately, move the point to the end of the closest following
311 line that doesn't end with a line continuation backslash - no check is
312 done that the point is inside a cpp directive to begin with.
313
314 Note that this function might do hidden buffer changes. See the
315 comment at the start of cc-engine.el for more info."
316 (if (and (cdr c-macro-cache)
317 (<= (point) (cdr c-macro-cache))
318 (>= (point) (car c-macro-cache)))
319 (goto-char (cdr c-macro-cache))
320 (unless (and (car c-macro-cache)
321 (<= (point) c-macro-cache-start-pos)
322 (>= (point) (car c-macro-cache)))
323 (setq c-macro-cache nil
324 c-macro-cache-start-pos nil
325 c-macro-cache-syntactic nil))
326 (while (progn
327 (end-of-line)
328 (when (and (eq (char-before) ?\\)
329 (not (eobp)))
330 (forward-char)
331 t)))
332 (when (car c-macro-cache)
333 (setcdr c-macro-cache (point)))))
334
335 (defun c-syntactic-end-of-macro ()
336 ;; Go to the end of a CPP directive, or a "safe" pos just before.
337 ;;
338 ;; This is normally the end of the next non-escaped line. A "safe"
339 ;; position is one not within a string or comment. (The EOL on a line
340 ;; comment is NOT "safe").
341 ;;
342 ;; This function must only be called from the beginning of a CPP construct.
343 ;;
344 ;; Note that this function might do hidden buffer changes. See the comment
345 ;; at the start of cc-engine.el for more info.
346 (let* ((here (point))
347 (there (progn (c-end-of-macro) (point)))
348 s)
349 (unless c-macro-cache-syntactic
350 (setq s (parse-partial-sexp here there))
351 (while (and (or (nth 3 s) ; in a string
352 (nth 4 s)) ; in a comment (maybe at end of line comment)
353 (> there here)) ; No infinite loops, please.
354 (setq there (1- (nth 8 s)))
355 (setq s (parse-partial-sexp here there)))
356 (setq c-macro-cache-syntactic (car c-macro-cache)))
357 (point)))
358
359 (defun c-forward-over-cpp-define-id ()
360 ;; Assuming point is at the "#" that introduces a preprocessor
361 ;; directive, it's moved forward to the end of the identifier which is
362 ;; "#define"d (or whatever c-opt-cpp-macro-define specifies). Non-nil
363 ;; is returned in this case, in all other cases nil is returned and
364 ;; point isn't moved.
365 ;;
366 ;; This function might do hidden buffer changes.
367 (when (and c-opt-cpp-macro-define-id
368 (looking-at c-opt-cpp-macro-define-id))
369 (goto-char (match-end 0))))
370
371 (defun c-forward-to-cpp-define-body ()
372 ;; Assuming point is at the "#" that introduces a preprocessor
373 ;; directive, it's moved forward to the start of the definition body
374 ;; if it's a "#define" (or whatever c-opt-cpp-macro-define
375 ;; specifies). Non-nil is returned in this case, in all other cases
376 ;; nil is returned and point isn't moved.
377 ;;
378 ;; This function might do hidden buffer changes.
379 (when (and c-opt-cpp-macro-define-start
380 (looking-at c-opt-cpp-macro-define-start)
381 (not (= (match-end 0) (c-point 'eol))))
382 (goto-char (match-end 0))))
383
384 \f
385 ;;; Basic utility functions.
386
387 (defun c-syntactic-content (from to paren-level)
388 ;; Return the given region as a string where all syntactic
389 ;; whitespace is removed or, where necessary, replaced with a single
390 ;; space. If PAREN-LEVEL is given then all parens in the region are
391 ;; collapsed to "()", "[]" etc.
392 ;;
393 ;; This function might do hidden buffer changes.
394
395 (save-excursion
396 (save-restriction
397 (narrow-to-region from to)
398 (goto-char from)
399 (let* ((parts (list nil)) (tail parts) pos in-paren)
400
401 (while (re-search-forward c-syntactic-ws-start to t)
402 (goto-char (setq pos (match-beginning 0)))
403 (c-forward-syntactic-ws)
404 (if (= (point) pos)
405 (forward-char)
406
407 (when paren-level
408 (save-excursion
409 (setq in-paren (= (car (parse-partial-sexp from pos 1)) 1)
410 pos (point))))
411
412 (if (and (> pos from)
413 (< (point) to)
414 (looking-at "\\w\\|\\s_")
415 (save-excursion
416 (goto-char (1- pos))
417 (looking-at "\\w\\|\\s_")))
418 (progn
419 (setcdr tail (list (buffer-substring-no-properties from pos)
420 " "))
421 (setq tail (cddr tail)))
422 (setcdr tail (list (buffer-substring-no-properties from pos)))
423 (setq tail (cdr tail)))
424
425 (when in-paren
426 (when (= (car (parse-partial-sexp pos to -1)) -1)
427 (setcdr tail (list (buffer-substring-no-properties
428 (1- (point)) (point))))
429 (setq tail (cdr tail))))
430
431 (setq from (point))))
432
433 (setcdr tail (list (buffer-substring-no-properties from to)))
434 (apply 'concat (cdr parts))))))
435
436 (defun c-shift-line-indentation (shift-amt)
437 ;; Shift the indentation of the current line with the specified
438 ;; amount (positive inwards). The buffer is modified only if
439 ;; SHIFT-AMT isn't equal to zero.
440 (let ((pos (- (point-max) (point)))
441 (c-macro-start c-macro-start)
442 tmp-char-inserted)
443 (if (zerop shift-amt)
444 nil
445 ;; If we're on an empty line inside a macro, we take the point
446 ;; to be at the current indentation and shift it to the
447 ;; appropriate column. This way we don't treat the extra
448 ;; whitespace out to the line continuation as indentation.
449 (when (and (c-query-and-set-macro-start)
450 (looking-at "[ \t]*\\\\$")
451 (save-excursion
452 (skip-chars-backward " \t")
453 (bolp)))
454 (insert ?x)
455 (backward-char)
456 (setq tmp-char-inserted t))
457 (unwind-protect
458 (let ((col (current-indentation)))
459 (delete-region (c-point 'bol) (c-point 'boi))
460 (beginning-of-line)
461 (indent-to (+ col shift-amt)))
462 (when tmp-char-inserted
463 (delete-char 1))))
464 ;; If initial point was within line's indentation and we're not on
465 ;; a line with a line continuation in a macro, position after the
466 ;; indentation. Else stay at same point in text.
467 (if (and (< (point) (c-point 'boi))
468 (not tmp-char-inserted))
469 (back-to-indentation)
470 (if (> (- (point-max) pos) (point))
471 (goto-char (- (point-max) pos))))))
472
473 (defsubst c-keyword-sym (keyword)
474 ;; Return non-nil if the string KEYWORD is a known keyword. More
475 ;; precisely, the value is the symbol for the keyword in
476 ;; `c-keywords-obarray'.
477 (intern-soft keyword c-keywords-obarray))
478
479 (defsubst c-keyword-member (keyword-sym lang-constant)
480 ;; Return non-nil if the symbol KEYWORD-SYM, as returned by
481 ;; `c-keyword-sym', is a member of LANG-CONSTANT, which is the name
482 ;; of a language constant that ends with "-kwds". If KEYWORD-SYM is
483 ;; nil then the result is nil.
484 (get keyword-sym lang-constant))
485
486 ;; String syntax chars, suitable for skip-syntax-(forward|backward).
487 (defconst c-string-syntax (if (memq 'gen-string-delim c-emacs-features)
488 "\"|"
489 "\""))
490
491 ;; Regexp matching string limit syntax.
492 (defconst c-string-limit-regexp (if (memq 'gen-string-delim c-emacs-features)
493 "\\s\"\\|\\s|"
494 "\\s\""))
495
496 ;; Regexp matching WS followed by string limit syntax.
497 (defconst c-ws*-string-limit-regexp
498 (concat "[ \t]*\\(" c-string-limit-regexp "\\)"))
499
500 ;; Holds formatted error strings for the few cases where parse errors
501 ;; are reported.
502 (defvar c-parsing-error nil)
503 (make-variable-buffer-local 'c-parsing-error)
504
505 (defun c-echo-parsing-error (&optional quiet)
506 (when (and c-report-syntactic-errors c-parsing-error (not quiet))
507 (c-benign-error "%s" c-parsing-error))
508 c-parsing-error)
509
510 ;; Faces given to comments and string literals. This is used in some
511 ;; situations to speed up recognition; it isn't mandatory that font
512 ;; locking is in use. This variable is extended with the face in
513 ;; `c-doc-face-name' when fontification is activated in cc-fonts.el.
514 (defvar c-literal-faces
515 (append '(font-lock-comment-face font-lock-string-face)
516 (when (facep 'font-lock-comment-delimiter-face)
517 ;; New in Emacs 22.
518 '(font-lock-comment-delimiter-face))))
519
520 (defsubst c-put-c-type-property (pos value)
521 ;; Put a c-type property with the given value at POS.
522 (c-put-char-property pos 'c-type value))
523
524 (defun c-clear-c-type-property (from to value)
525 ;; Remove all occurrences of the c-type property that has the given
526 ;; value in the region between FROM and TO. VALUE is assumed to not
527 ;; be nil.
528 ;;
529 ;; Note: This assumes that c-type is put on single chars only; it's
530 ;; very inefficient if matching properties cover large regions.
531 (save-excursion
532 (goto-char from)
533 (while (progn
534 (when (eq (get-text-property (point) 'c-type) value)
535 (c-clear-char-property (point) 'c-type))
536 (goto-char (next-single-property-change (point) 'c-type nil to))
537 (< (point) to)))))
538
539 \f
540 ;; Some debug tools to visualize various special positions. This
541 ;; debug code isn't as portable as the rest of CC Mode.
542
543 (cc-bytecomp-defun overlays-in)
544 (cc-bytecomp-defun overlay-get)
545 (cc-bytecomp-defun overlay-start)
546 (cc-bytecomp-defun overlay-end)
547 (cc-bytecomp-defun delete-overlay)
548 (cc-bytecomp-defun overlay-put)
549 (cc-bytecomp-defun make-overlay)
550
551 (defun c-debug-add-face (beg end face)
552 (c-save-buffer-state ((overlays (overlays-in beg end)) overlay)
553 (while overlays
554 (setq overlay (car overlays)
555 overlays (cdr overlays))
556 (when (eq (overlay-get overlay 'face) face)
557 (setq beg (min beg (overlay-start overlay))
558 end (max end (overlay-end overlay)))
559 (delete-overlay overlay)))
560 (overlay-put (make-overlay beg end) 'face face)))
561
562 (defun c-debug-remove-face (beg end face)
563 (c-save-buffer-state ((overlays (overlays-in beg end)) overlay
564 (ol-beg beg) (ol-end end))
565 (while overlays
566 (setq overlay (car overlays)
567 overlays (cdr overlays))
568 (when (eq (overlay-get overlay 'face) face)
569 (setq ol-beg (min ol-beg (overlay-start overlay))
570 ol-end (max ol-end (overlay-end overlay)))
571 (delete-overlay overlay)))
572 (when (< ol-beg beg)
573 (overlay-put (make-overlay ol-beg beg) 'face face))
574 (when (> ol-end end)
575 (overlay-put (make-overlay end ol-end) 'face face))))
576
577 \f
578 ;; `c-beginning-of-statement-1' and accompanying stuff.
579
580 ;; KLUDGE ALERT: c-maybe-labelp is used to pass information between
581 ;; c-crosses-statement-barrier-p and c-beginning-of-statement-1. A
582 ;; better way should be implemented, but this will at least shut up
583 ;; the byte compiler.
584 (defvar c-maybe-labelp)
585
586 ;; New awk-compatible version of c-beginning-of-statement-1, ACM 2002/6/22
587
588 ;; Macros used internally in c-beginning-of-statement-1 for the
589 ;; automaton actions.
590 (defmacro c-bos-push-state ()
591 '(setq stack (cons (cons state saved-pos)
592 stack)))
593 (defmacro c-bos-pop-state (&optional do-if-done)
594 `(if (setq state (car (car stack))
595 saved-pos (cdr (car stack))
596 stack (cdr stack))
597 t
598 ,do-if-done
599 (throw 'loop nil)))
600 (defmacro c-bos-pop-state-and-retry ()
601 '(throw 'loop (setq state (car (car stack))
602 saved-pos (cdr (car stack))
603 ;; Throw nil if stack is empty, else throw non-nil.
604 stack (cdr stack))))
605 (defmacro c-bos-save-pos ()
606 '(setq saved-pos (vector pos tok ptok pptok)))
607 (defmacro c-bos-restore-pos ()
608 '(unless (eq (elt saved-pos 0) start)
609 (setq pos (elt saved-pos 0)
610 tok (elt saved-pos 1)
611 ptok (elt saved-pos 2)
612 pptok (elt saved-pos 3))
613 (goto-char pos)
614 (setq sym nil)))
615 (defmacro c-bos-save-error-info (missing got)
616 `(setq saved-pos (vector pos ,missing ,got)))
617 (defmacro c-bos-report-error ()
618 '(unless noerror
619 (setq c-parsing-error
620 (format "No matching `%s' found for `%s' on line %d"
621 (elt saved-pos 1)
622 (elt saved-pos 2)
623 (1+ (count-lines (point-min)
624 (c-point 'bol (elt saved-pos 0))))))))
625
626 (defun c-beginning-of-statement-1 (&optional lim ignore-labels
627 noerror comma-delim)
628 "Move to the start of the current statement or declaration, or to
629 the previous one if already at the beginning of one. Only
630 statements/declarations on the same level are considered, i.e. don't
631 move into or out of sexps (not even normal expression parentheses).
632
633 If point is already at the earliest statement within braces or parens,
634 this function doesn't move back into any whitespace preceding it; it
635 returns 'same in this case.
636
637 Stop at statement continuation tokens like \"else\", \"catch\",
638 \"finally\" and the \"while\" in \"do ... while\" if the start point
639 is within the continuation. If starting at such a token, move to the
640 corresponding statement start. If at the beginning of a statement,
641 move to the closest containing statement if there is any. This might
642 also stop at a continuation clause.
643
644 Labels are treated as part of the following statements if
645 IGNORE-LABELS is non-nil. (FIXME: Doesn't work if we stop at a known
646 statement start keyword.) Otherwise, each label is treated as a
647 separate statement.
648
649 Macros are ignored \(i.e. skipped over) unless point is within one, in
650 which case the content of the macro is treated as normal code. Aside
651 from any normal statement starts found in it, stop at the first token
652 of the content in the macro, i.e. the expression of an \"#if\" or the
653 start of the definition in a \"#define\". Also stop at start of
654 macros before leaving them.
655
656 Return:
657 'label if stopped at a label or \"case...:\" or \"default:\";
658 'same if stopped at the beginning of the current statement;
659 'up if stepped to a containing statement;
660 'previous if stepped to a preceding statement;
661 'beginning if stepped from a statement continuation clause to
662 its start clause; or
663 'macro if stepped to a macro start.
664 Note that 'same and not 'label is returned if stopped at the same
665 label without crossing the colon character.
666
667 LIM may be given to limit the search. If the search hits the limit,
668 point will be left at the closest following token, or at the start
669 position if that is less ('same is returned in this case).
670
671 NOERROR turns off error logging to `c-parsing-error'.
672
673 Normally only ';' and virtual semicolons are considered to delimit
674 statements, but if COMMA-DELIM is non-nil then ',' is treated
675 as a delimiter too.
676
677 Note that this function might do hidden buffer changes. See the
678 comment at the start of cc-engine.el for more info."
679
680 ;; The bulk of this function is a pushdown automaton that looks at statement
681 ;; boundaries and the tokens (such as "while") in c-opt-block-stmt-key. Its
682 ;; purpose is to keep track of nested statements, ensuring that such
683 ;; statements are skipped over in their entirety (somewhat akin to what C-M-p
684 ;; does with nested braces/brackets/parentheses).
685 ;;
686 ;; Note: The position of a boundary is the following token.
687 ;;
688 ;; Beginning with the current token (the one following point), move back one
689 ;; sexp at a time (where a sexp is, more or less, either a token or the
690 ;; entire contents of a brace/bracket/paren pair). Each time a statement
691 ;; boundary is crossed or a "while"-like token is found, update the state of
692 ;; the PDA. Stop at the beginning of a statement when the stack (holding
693 ;; nested statement info) is empty and the position has been moved.
694 ;;
695 ;; The following variables constitute the PDA:
696 ;;
697 ;; sym: This is either the "while"-like token (e.g. 'for) we've just
698 ;; scanned back over, 'boundary if we've just gone back over a
699 ;; statement boundary, or nil otherwise.
700 ;; state: takes one of the values (nil else else-boundary while
701 ;; while-boundary catch catch-boundary).
702 ;; nil means "no "while"-like token yet scanned".
703 ;; 'else, for example, means "just gone back over an else".
704 ;; 'else-boundary means "just gone back over a statement boundary
705 ;; immediately after having gone back over an else".
706 ;; saved-pos: A vector of either saved positions (tok ptok pptok, etc.) or
707 ;; of error reporting information.
708 ;; stack: The stack onto which the PDA pushes its state. Each entry
709 ;; consists of a saved value of state and saved-pos. An entry is
710 ;; pushed when we move back over a "continuation" token (e.g. else)
711 ;; and popped when we encounter the corresponding opening token
712 ;; (e.g. if).
713 ;;
714 ;;
715 ;; The following diagram briefly outlines the PDA.
716 ;;
717 ;; Common state:
718 ;; "else": Push state, goto state `else'.
719 ;; "while": Push state, goto state `while'.
720 ;; "catch" or "finally": Push state, goto state `catch'.
721 ;; boundary: Pop state.
722 ;; other: Do nothing special.
723 ;;
724 ;; State `else':
725 ;; boundary: Goto state `else-boundary'.
726 ;; other: Error, pop state, retry token.
727 ;;
728 ;; State `else-boundary':
729 ;; "if": Pop state.
730 ;; boundary: Error, pop state.
731 ;; other: See common state.
732 ;;
733 ;; State `while':
734 ;; boundary: Save position, goto state `while-boundary'.
735 ;; other: Pop state, retry token.
736 ;;
737 ;; State `while-boundary':
738 ;; "do": Pop state.
739 ;; boundary: Restore position if it's not at start, pop state. [*see below]
740 ;; other: See common state.
741 ;;
742 ;; State `catch':
743 ;; boundary: Goto state `catch-boundary'.
744 ;; other: Error, pop state, retry token.
745 ;;
746 ;; State `catch-boundary':
747 ;; "try": Pop state.
748 ;; "catch": Goto state `catch'.
749 ;; boundary: Error, pop state.
750 ;; other: See common state.
751 ;;
752 ;; [*] In the `while-boundary' state, we had pushed a 'while state, and were
753 ;; searching for a "do" which would have opened a do-while. If we didn't
754 ;; find it, we discard the analysis done since the "while", go back to this
755 ;; token in the buffer and restart the scanning there, this time WITHOUT
756 ;; pushing the 'while state onto the stack.
757 ;;
758 ;; In addition to the above there is some special handling of labels
759 ;; and macros.
760
761 (let ((case-fold-search nil)
762 (start (point))
763 macro-start
764 (delims (if comma-delim '(?\; ?,) '(?\;)))
765 (c-stmt-delim-chars (if comma-delim
766 c-stmt-delim-chars-with-comma
767 c-stmt-delim-chars))
768 c-in-literal-cache c-maybe-labelp after-case:-pos saved
769 ;; Current position.
770 pos
771 ;; Position of last stmt boundary character (e.g. ;).
772 boundary-pos
773 ;; The position of the last sexp or bound that follows the
774 ;; first found colon, i.e. the start of the nonlabel part of
775 ;; the statement. It's `start' if a colon is found just after
776 ;; the start.
777 after-labels-pos
778 ;; Like `after-labels-pos', but the first such position inside
779 ;; a label, i.e. the start of the last label before the start
780 ;; of the nonlabel part of the statement.
781 last-label-pos
782 ;; The last position where a label is possible provided the
783 ;; statement started there. It's nil as long as no invalid
784 ;; label content has been found (according to
785 ;; `c-nonlabel-token-key'). It's `start' if no valid label
786 ;; content was found in the label. Note that we might still
787 ;; regard it a label if it starts with `c-label-kwds'.
788 label-good-pos
789 ;; Putative positions of the components of a bitfield declaration,
790 ;; e.g. "int foo : NUM_FOO_BITS ;"
791 bitfield-type-pos bitfield-id-pos bitfield-size-pos
792 ;; Symbol just scanned back over (e.g. 'while or 'boundary).
793 ;; See above.
794 sym
795 ;; Current state in the automaton. See above.
796 state
797 ;; Current saved positions. See above.
798 saved-pos
799 ;; Stack of conses (state . saved-pos).
800 stack
801 ;; Regexp which matches "for", "if", etc.
802 (cond-key (or c-opt-block-stmt-key
803 "\\<\\>")) ; Matches nothing.
804 ;; Return value.
805 (ret 'same)
806 ;; Positions of the last three sexps or bounds we've stopped at.
807 tok ptok pptok)
808
809 (save-restriction
810 (if lim (narrow-to-region lim (point-max)))
811
812 (if (save-excursion
813 (and (c-beginning-of-macro)
814 (/= (point) start)))
815 (setq macro-start (point)))
816
817 ;; Try to skip back over unary operator characters, to register
818 ;; that we've moved.
819 (while (progn
820 (setq pos (point))
821 (c-backward-syntactic-ws)
822 ;; Protect post-++/-- operators just before a virtual semicolon.
823 (and (not (c-at-vsemi-p))
824 (/= (skip-chars-backward "-+!*&~@`#") 0))))
825
826 ;; Skip back over any semicolon here. If it was a bare semicolon, we're
827 ;; done. Later on we ignore the boundaries for statements that don't
828 ;; contain any sexp. The only thing that is affected is that the error
829 ;; checking is a little less strict, and we really don't bother.
830 (if (and (memq (char-before) delims)
831 (progn (forward-char -1)
832 (setq saved (point))
833 (c-backward-syntactic-ws)
834 (or (memq (char-before) delims)
835 (memq (char-before) '(?: nil))
836 (eq (char-syntax (char-before)) ?\()
837 (c-at-vsemi-p))))
838 (setq ret 'previous
839 pos saved)
840
841 ;; Begin at start and not pos to detect macros if we stand
842 ;; directly after the #.
843 (goto-char start)
844 (if (looking-at "\\<\\|\\W")
845 ;; Record this as the first token if not starting inside it.
846 (setq tok start))
847
848
849 ;; The following while loop goes back one sexp (balanced parens,
850 ;; etc. with contents, or symbol or suchlike) each iteration. This
851 ;; movement is accomplished with a call to c-backward-sexp approx 170
852 ;; lines below.
853 ;;
854 ;; The loop is exited only by throwing nil to the (catch 'loop ...):
855 ;; 1. On reaching the start of a macro;
856 ;; 2. On having passed a stmt boundary with the PDA stack empty;
857 ;; 3. On reaching the start of an Objective C method def;
858 ;; 4. From macro `c-bos-pop-state'; when the stack is empty;
859 ;; 5. From macro `c-bos-pop-state-and-retry' when the stack is empty.
860 (while
861 (catch 'loop ;; Throw nil to break, non-nil to continue.
862 (cond
863 ;; Are we in a macro, just after the opening #?
864 ((save-excursion
865 (and macro-start ; Always NIL for AWK.
866 (progn (skip-chars-backward " \t")
867 (eq (char-before) ?#))
868 (progn (setq saved (1- (point)))
869 (beginning-of-line)
870 (not (eq (char-before (1- (point))) ?\\)))
871 (looking-at c-opt-cpp-start)
872 (progn (skip-chars-forward " \t")
873 (eq (point) saved))))
874 (goto-char saved)
875 (if (and (c-forward-to-cpp-define-body)
876 (progn (c-forward-syntactic-ws start)
877 (< (point) start)))
878 ;; Stop at the first token in the content of the macro.
879 (setq pos (point)
880 ignore-labels t) ; Avoid the label check on exit.
881 (setq pos saved
882 ret 'macro
883 ignore-labels t))
884 (throw 'loop nil)) ; 1. Start of macro.
885
886 ;; Do a round through the automaton if we've just passed a
887 ;; statement boundary or passed a "while"-like token.
888 ((or sym
889 (and (looking-at cond-key)
890 (setq sym (intern (match-string 1)))))
891
892 (when (and (< pos start) (null stack))
893 (throw 'loop nil)) ; 2. Statement boundary.
894
895 ;; The PDA state handling.
896 ;;
897 ;; Refer to the description of the PDA in the opening
898 ;; comments. In the following OR form, the first leaf
899 ;; attempts to handles one of the specific actions detailed
900 ;; (e.g., finding token "if" whilst in state `else-boundary').
901 ;; We drop through to the second leaf (which handles common
902 ;; state) if no specific handler is found in the first cond.
903 ;; If a parsing error is detected (e.g. an "else" with no
904 ;; preceding "if"), we throw to the enclosing catch.
905 ;;
906 ;; Note that the (eq state 'else) means
907 ;; "we've just passed an else", NOT "we're looking for an
908 ;; else".
909 (or (cond
910 ((eq state 'else)
911 (if (eq sym 'boundary)
912 (setq state 'else-boundary)
913 (c-bos-report-error)
914 (c-bos-pop-state-and-retry)))
915
916 ((eq state 'else-boundary)
917 (cond ((eq sym 'if)
918 (c-bos-pop-state (setq ret 'beginning)))
919 ((eq sym 'boundary)
920 (c-bos-report-error)
921 (c-bos-pop-state))))
922
923 ((eq state 'while)
924 (if (and (eq sym 'boundary)
925 ;; Since this can cause backtracking we do a
926 ;; little more careful analysis to avoid it:
927 ;; If there's a label in front of the while
928 ;; it can't be part of a do-while.
929 (not after-labels-pos))
930 (progn (c-bos-save-pos)
931 (setq state 'while-boundary))
932 (c-bos-pop-state-and-retry))) ; Can't be a do-while
933
934 ((eq state 'while-boundary)
935 (cond ((eq sym 'do)
936 (c-bos-pop-state (setq ret 'beginning)))
937 ((eq sym 'boundary) ; isn't a do-while
938 (c-bos-restore-pos) ; the position of the while
939 (c-bos-pop-state)))) ; no longer searching for do.
940
941 ((eq state 'catch)
942 (if (eq sym 'boundary)
943 (setq state 'catch-boundary)
944 (c-bos-report-error)
945 (c-bos-pop-state-and-retry)))
946
947 ((eq state 'catch-boundary)
948 (cond
949 ((eq sym 'try)
950 (c-bos-pop-state (setq ret 'beginning)))
951 ((eq sym 'catch)
952 (setq state 'catch))
953 ((eq sym 'boundary)
954 (c-bos-report-error)
955 (c-bos-pop-state)))))
956
957 ;; This is state common. We get here when the previous
958 ;; cond statement found no particular state handler.
959 (cond ((eq sym 'boundary)
960 ;; If we have a boundary at the start
961 ;; position we push a frame to go to the
962 ;; previous statement.
963 (if (>= pos start)
964 (c-bos-push-state)
965 (c-bos-pop-state)))
966 ((eq sym 'else)
967 (c-bos-push-state)
968 (c-bos-save-error-info 'if 'else)
969 (setq state 'else))
970 ((eq sym 'while)
971 ;; Is this a real while, or a do-while?
972 ;; The next `when' triggers unless we are SURE that
973 ;; the `while' is not the tail end of a `do-while'.
974 (when (or (not pptok)
975 (memq (char-after pptok) delims)
976 ;; The following kludge is to prevent
977 ;; infinite recursion when called from
978 ;; c-awk-after-if-for-while-condition-p,
979 ;; or the like.
980 (and (eq (point) start)
981 (c-vsemi-status-unknown-p))
982 (c-at-vsemi-p pptok))
983 ;; Since this can cause backtracking we do a
984 ;; little more careful analysis to avoid it: If
985 ;; the while isn't followed by a (possibly
986 ;; virtual) semicolon it can't be a do-while.
987 (c-bos-push-state)
988 (setq state 'while)))
989 ((memq sym '(catch finally))
990 (c-bos-push-state)
991 (c-bos-save-error-info 'try sym)
992 (setq state 'catch))))
993
994 (when c-maybe-labelp
995 ;; We're either past a statement boundary or at the
996 ;; start of a statement, so throw away any label data
997 ;; for the previous one.
998 (setq after-labels-pos nil
999 last-label-pos nil
1000 c-maybe-labelp nil))))
1001
1002 ;; Step to the previous sexp, but not if we crossed a
1003 ;; boundary, since that doesn't consume an sexp.
1004 (if (eq sym 'boundary)
1005 (setq ret 'previous)
1006
1007 ;; HERE IS THE SINGLE PLACE INSIDE THE PDA LOOP WHERE WE MOVE
1008 ;; BACKWARDS THROUGH THE SOURCE.
1009
1010 (c-backward-syntactic-ws)
1011 (let ((before-sws-pos (point))
1012 ;; The end position of the area to search for statement
1013 ;; barriers in this round.
1014 (maybe-after-boundary-pos pos))
1015
1016 ;; Go back over exactly one logical sexp, taking proper
1017 ;; account of macros and escaped EOLs.
1018 (while
1019 (progn
1020 (unless (c-safe (c-backward-sexp) t)
1021 ;; Give up if we hit an unbalanced block. Since the
1022 ;; stack won't be empty the code below will report a
1023 ;; suitable error.
1024 (throw 'loop nil))
1025 (cond
1026 ;; Have we moved into a macro?
1027 ((and (not macro-start)
1028 (c-beginning-of-macro))
1029 ;; Have we crossed a statement boundary? If not,
1030 ;; keep going back until we find one or a "real" sexp.
1031 (and
1032 (save-excursion
1033 (c-end-of-macro)
1034 (not (c-crosses-statement-barrier-p
1035 (point) maybe-after-boundary-pos)))
1036 (setq maybe-after-boundary-pos (point))))
1037 ;; Have we just gone back over an escaped NL? This
1038 ;; doesn't count as a sexp.
1039 ((looking-at "\\\\$")))))
1040
1041 ;; Have we crossed a statement boundary?
1042 (setq boundary-pos
1043 (cond
1044 ;; Are we at a macro beginning?
1045 ((and (not macro-start)
1046 c-opt-cpp-prefix
1047 (looking-at c-opt-cpp-prefix))
1048 (save-excursion
1049 (c-end-of-macro)
1050 (c-crosses-statement-barrier-p
1051 (point) maybe-after-boundary-pos)))
1052 ;; Just gone back over a brace block?
1053 ((and
1054 (eq (char-after) ?{)
1055 (not (c-looking-at-inexpr-block lim nil t)))
1056 (save-excursion
1057 (c-forward-sexp) (point)))
1058 ;; Just gone back over some paren block?
1059 ((looking-at "\\s\(")
1060 (save-excursion
1061 (goto-char (1+ (c-down-list-backward
1062 before-sws-pos)))
1063 (c-crosses-statement-barrier-p
1064 (point) maybe-after-boundary-pos)))
1065 ;; Just gone back over an ordinary symbol of some sort?
1066 (t (c-crosses-statement-barrier-p
1067 (point) maybe-after-boundary-pos))))
1068
1069 (when boundary-pos
1070 (setq pptok ptok
1071 ptok tok
1072 tok boundary-pos
1073 sym 'boundary)
1074 ;; Like a C "continue". Analyze the next sexp.
1075 (throw 'loop t))))
1076
1077 ;; ObjC method def?
1078 (when (and c-opt-method-key
1079 (setq saved (c-in-method-def-p)))
1080 (setq pos saved
1081 ignore-labels t) ; Avoid the label check on exit.
1082 (throw 'loop nil)) ; 3. ObjC method def.
1083
1084 ;; Might we have a bitfield declaration, "<type> <id> : <size>"?
1085 (if c-has-bitfields
1086 (cond
1087 ;; The : <size> and <id> fields?
1088 ((and (numberp c-maybe-labelp)
1089 (not bitfield-size-pos)
1090 (save-excursion
1091 (goto-char (or tok start))
1092 (not (looking-at c-keywords-regexp)))
1093 (not (looking-at c-keywords-regexp))
1094 (not (c-punctuation-in (point) c-maybe-labelp)))
1095 (setq bitfield-size-pos (or tok start)
1096 bitfield-id-pos (point)))
1097 ;; The <type> field?
1098 ((and bitfield-id-pos
1099 (not bitfield-type-pos))
1100 (if (and (looking-at c-symbol-key) ; Can only be an integer type. :-)
1101 (not (looking-at c-not-primitive-type-keywords-regexp))
1102 (not (c-punctuation-in (point) tok)))
1103 (setq bitfield-type-pos (point))
1104 (setq bitfield-size-pos nil
1105 bitfield-id-pos nil)))))
1106
1107 ;; Handle labels.
1108 (unless (eq ignore-labels t)
1109 (when (numberp c-maybe-labelp)
1110 ;; `c-crosses-statement-barrier-p' has found a colon, so we
1111 ;; might be in a label now. Have we got a real label
1112 ;; (including a case label) or something like C++'s "public:"?
1113 ;; A case label might use an expression rather than a token.
1114 (setq after-case:-pos (or tok start))
1115 (if (or (looking-at c-nonlabel-token-key) ; e.g. "while" or "'a'"
1116 ;; Catch C++'s inheritance construct "class foo : bar".
1117 (save-excursion
1118 (and
1119 (c-safe (c-backward-sexp) t)
1120 (looking-at c-nonlabel-token-2-key))))
1121 (setq c-maybe-labelp nil)
1122 (if after-labels-pos ; Have we already encountered a label?
1123 (if (not last-label-pos)
1124 (setq last-label-pos (or tok start)))
1125 (setq after-labels-pos (or tok start)))
1126 (setq c-maybe-labelp t
1127 label-good-pos nil))) ; bogus "label"
1128
1129 (when (and (not label-good-pos) ; i.e. no invalid "label"'s yet
1130 ; been found.
1131 (looking-at c-nonlabel-token-key)) ; e.g. "while :"
1132 ;; We're in a potential label and it's the first
1133 ;; time we've found something that isn't allowed in
1134 ;; one.
1135 (setq label-good-pos (or tok start))))
1136
1137 ;; We've moved back by a sexp, so update the token positions.
1138 (setq sym nil
1139 pptok ptok
1140 ptok tok
1141 tok (point)
1142 pos tok) ; always non-nil
1143 ) ; end of (catch loop ....)
1144 ) ; end of sexp-at-a-time (while ....)
1145
1146 ;; If the stack isn't empty there might be errors to report.
1147 (while stack
1148 (if (and (vectorp saved-pos) (eq (length saved-pos) 3))
1149 (c-bos-report-error))
1150 (setq saved-pos (cdr (car stack))
1151 stack (cdr stack)))
1152
1153 (when (and (eq ret 'same)
1154 (not (memq sym '(boundary ignore nil))))
1155 ;; Need to investigate closer whether we've crossed
1156 ;; between a substatement and its containing statement.
1157 (if (setq saved
1158 (cond ((and (looking-at c-block-stmt-1-2-key)
1159 (eq (char-after ptok) ?\())
1160 pptok)
1161 ((looking-at c-block-stmt-1-key)
1162 ptok)
1163 (t pptok)))
1164 (cond ((> start saved) (setq pos saved))
1165 ((= start saved) (setq ret 'up)))))
1166
1167 (when (and (not ignore-labels)
1168 (eq c-maybe-labelp t)
1169 (not (eq ret 'beginning))
1170 after-labels-pos
1171 (not bitfield-type-pos) ; Bitfields take precedence over labels.
1172 (or (not label-good-pos)
1173 (<= label-good-pos pos)
1174 (progn
1175 (goto-char (if (and last-label-pos
1176 (< last-label-pos start))
1177 last-label-pos
1178 pos))
1179 (looking-at c-label-kwds-regexp))))
1180 ;; We're in a label. Maybe we should step to the statement
1181 ;; after it.
1182 (if (< after-labels-pos start)
1183 (setq pos after-labels-pos)
1184 (setq ret 'label)
1185 (if (and last-label-pos (< last-label-pos start))
1186 ;; Might have jumped over several labels. Go to the last one.
1187 (setq pos last-label-pos)))))
1188
1189 ;; Have we got "case <expression>:"?
1190 (goto-char pos)
1191 (when (and after-case:-pos
1192 (not (eq ret 'beginning))
1193 (looking-at c-case-kwds-regexp))
1194 (if (< after-case:-pos start)
1195 (setq pos after-case:-pos))
1196 (if (eq ret 'same)
1197 (setq ret 'label)))
1198
1199 ;; Skip over the unary operators that can start the statement.
1200 (while (progn
1201 (c-backward-syntactic-ws)
1202 ;; protect AWK post-inc/decrement operators, etc.
1203 (and (not (c-at-vsemi-p (point)))
1204 (/= (skip-chars-backward "-+!*&~@`#") 0)))
1205 (setq pos (point)))
1206 (goto-char pos)
1207 ret)))
1208
1209 (defun c-punctuation-in (from to)
1210 "Return non-nil if there is a non-comment non-macro punctuation character
1211 between FROM and TO. FROM must not be in a string or comment. The returned
1212 value is the position of the first such character."
1213 (save-excursion
1214 (goto-char from)
1215 (let ((pos (point)))
1216 (while (progn (skip-chars-forward c-symbol-chars to)
1217 (c-forward-syntactic-ws to)
1218 (> (point) pos))
1219 (setq pos (point))))
1220 (and (< (point) to) (point))))
1221
1222 (defun c-crosses-statement-barrier-p (from to)
1223 "Return non-nil if buffer positions FROM to TO cross one or more
1224 statement or declaration boundaries. The returned value is actually
1225 the position of the earliest boundary char. FROM must not be within
1226 a string or comment.
1227
1228 The variable `c-maybe-labelp' is set to the position of the first `:' that
1229 might start a label (i.e. not part of `::' and not preceded by `?'). If a
1230 single `?' is found, then `c-maybe-labelp' is cleared.
1231
1232 For AWK, a statement which is terminated by an EOL (not a \; or a }) is
1233 regarded as having a \"virtual semicolon\" immediately after the last token on
1234 the line. If this virtual semicolon is _at_ from, the function recognizes it.
1235
1236 Note that this function might do hidden buffer changes. See the
1237 comment at the start of cc-engine.el for more info."
1238 (let* ((skip-chars
1239 ;; If the current language has CPP macros, insert # into skip-chars.
1240 (if c-opt-cpp-symbol
1241 (concat (substring c-stmt-delim-chars 0 1) ; "^"
1242 c-opt-cpp-symbol ; usually "#"
1243 (substring c-stmt-delim-chars 1)) ; e.g. ";{}?:"
1244 c-stmt-delim-chars))
1245 (non-skip-list
1246 (append (substring skip-chars 1) nil)) ; e.g. (?# ?\; ?{ ?} ?? ?:)
1247 lit-range vsemi-pos)
1248 (save-restriction
1249 (widen)
1250 (save-excursion
1251 (catch 'done
1252 (goto-char from)
1253 (while (progn (skip-chars-forward
1254 skip-chars
1255 (min to (c-point 'bonl)))
1256 (< (point) to))
1257 (cond
1258 ;; Virtual semicolon?
1259 ((and (bolp)
1260 (save-excursion
1261 (progn
1262 (if (setq lit-range (c-literal-limits from)) ; Have we landed in a string/comment?
1263 (goto-char (car lit-range)))
1264 (c-backward-syntactic-ws) ; ? put a limit here, maybe?
1265 (setq vsemi-pos (point))
1266 (c-at-vsemi-p))))
1267 (throw 'done vsemi-pos))
1268 ;; In a string/comment?
1269 ((setq lit-range (c-literal-limits from))
1270 (goto-char (cdr lit-range)))
1271 ((eq (char-after) ?:)
1272 (forward-char)
1273 (if (and (eq (char-after) ?:)
1274 (< (point) to))
1275 ;; Ignore scope operators.
1276 (forward-char)
1277 (setq c-maybe-labelp (1- (point)))))
1278 ((eq (char-after) ??)
1279 ;; A question mark. Can't be a label, so stop
1280 ;; looking for more : and ?.
1281 (setq c-maybe-labelp nil
1282 skip-chars (substring c-stmt-delim-chars 0 -2)))
1283 ;; At a CPP construct or a "#" or "##" operator?
1284 ((and c-opt-cpp-symbol (looking-at c-opt-cpp-symbol))
1285 (if (save-excursion
1286 (skip-chars-backward " \t")
1287 (and (bolp)
1288 (or (bobp)
1289 (not (eq (char-before (1- (point))) ?\\)))))
1290 (c-end-of-macro)
1291 (skip-chars-forward c-opt-cpp-symbol)))
1292 ((memq (char-after) non-skip-list)
1293 (throw 'done (point)))))
1294 ;; In trailing space after an as yet undetected virtual semicolon?
1295 (c-backward-syntactic-ws from)
1296 (when (and (bolp) (not (bobp))) ; Can happen in AWK Mode with an
1297 ; unterminated string/regexp.
1298 (backward-char))
1299 (if (and (< (point) to)
1300 (c-at-vsemi-p))
1301 (point)
1302 nil))))))
1303
1304 (defun c-at-statement-start-p ()
1305 "Return non-nil if the point is at the first token in a statement
1306 or somewhere in the syntactic whitespace before it.
1307
1308 A \"statement\" here is not restricted to those inside code blocks.
1309 Any kind of declaration-like construct that occur outside function
1310 bodies is also considered a \"statement\".
1311
1312 Note that this function might do hidden buffer changes. See the
1313 comment at the start of cc-engine.el for more info."
1314
1315 (save-excursion
1316 (let ((end (point))
1317 c-maybe-labelp)
1318 (c-syntactic-skip-backward (substring c-stmt-delim-chars 1) nil t)
1319 (or (bobp)
1320 (eq (char-before) ?})
1321 (and (eq (char-before) ?{)
1322 (not (and c-special-brace-lists
1323 (progn (backward-char)
1324 (c-looking-at-special-brace-list)))))
1325 (c-crosses-statement-barrier-p (point) end)))))
1326
1327 (defun c-at-expression-start-p ()
1328 "Return non-nil if the point is at the first token in an expression or
1329 statement, or somewhere in the syntactic whitespace before it.
1330
1331 An \"expression\" here is a bit different from the normal language
1332 grammar sense: It's any sequence of expression tokens except commas,
1333 unless they are enclosed inside parentheses of some kind. Also, an
1334 expression never continues past an enclosing parenthesis, but it might
1335 contain parenthesis pairs of any sort except braces.
1336
1337 Since expressions never cross statement boundaries, this function also
1338 recognizes statement beginnings, just like `c-at-statement-start-p'.
1339
1340 Note that this function might do hidden buffer changes. See the
1341 comment at the start of cc-engine.el for more info."
1342
1343 (save-excursion
1344 (let ((end (point))
1345 (c-stmt-delim-chars c-stmt-delim-chars-with-comma)
1346 c-maybe-labelp)
1347 (c-syntactic-skip-backward (substring c-stmt-delim-chars 1) nil t)
1348 (or (bobp)
1349 (memq (char-before) '(?{ ?}))
1350 (save-excursion (backward-char)
1351 (looking-at "\\s("))
1352 (c-crosses-statement-barrier-p (point) end)))))
1353
1354 \f
1355 ;; A set of functions that covers various idiosyncrasies in
1356 ;; implementations of `forward-comment'.
1357
1358 ;; Note: Some emacsen considers incorrectly that any line comment
1359 ;; ending with a backslash continues to the next line. I can't think
1360 ;; of any way to work around that in a reliable way without changing
1361 ;; the buffer, though. Suggestions welcome. ;) (No, temporarily
1362 ;; changing the syntax for backslash doesn't work since we must treat
1363 ;; escapes in string literals correctly.)
1364
1365 (defun c-forward-single-comment ()
1366 "Move forward past whitespace and the closest following comment, if any.
1367 Return t if a comment was found, nil otherwise. In either case, the
1368 point is moved past the following whitespace. Line continuations,
1369 i.e. a backslashes followed by line breaks, are treated as whitespace.
1370 The line breaks that end line comments are considered to be the
1371 comment enders, so the point will be put on the beginning of the next
1372 line if it moved past a line comment.
1373
1374 This function does not do any hidden buffer changes."
1375
1376 (let ((start (point)))
1377 (when (looking-at "\\([ \t\n\r\f\v]\\|\\\\[\n\r]\\)+")
1378 (goto-char (match-end 0)))
1379
1380 (when (forward-comment 1)
1381 (if (eobp)
1382 ;; Some emacsen (e.g. XEmacs 21) return t when moving
1383 ;; forwards at eob.
1384 nil
1385
1386 ;; Emacs includes the ending newline in a b-style (c++)
1387 ;; comment, but XEmacs doesn't. We depend on the Emacs
1388 ;; behavior (which also is symmetric).
1389 (if (and (eolp) (elt (parse-partial-sexp start (point)) 7))
1390 (condition-case nil (forward-char 1)))
1391
1392 t))))
1393
1394 (defsubst c-forward-comments ()
1395 "Move forward past all following whitespace and comments.
1396 Line continuations, i.e. a backslashes followed by line breaks, are
1397 treated as whitespace.
1398
1399 Note that this function might do hidden buffer changes. See the
1400 comment at the start of cc-engine.el for more info."
1401
1402 (while (or
1403 ;; If forward-comment in at least XEmacs 21 is given a large
1404 ;; positive value, it'll loop all the way through if it hits
1405 ;; eob.
1406 (and (forward-comment 5)
1407 ;; Some emacsen (e.g. XEmacs 21) return t when moving
1408 ;; forwards at eob.
1409 (not (eobp)))
1410
1411 (when (looking-at "\\\\[\n\r]")
1412 (forward-char 2)
1413 t))))
1414
1415 (defun c-backward-single-comment ()
1416 "Move backward past whitespace and the closest preceding comment, if any.
1417 Return t if a comment was found, nil otherwise. In either case, the
1418 point is moved past the preceding whitespace. Line continuations,
1419 i.e. a backslashes followed by line breaks, are treated as whitespace.
1420 The line breaks that end line comments are considered to be the
1421 comment enders, so the point cannot be at the end of the same line to
1422 move over a line comment.
1423
1424 This function does not do any hidden buffer changes."
1425
1426 (let ((start (point)))
1427 ;; When we got newline terminated comments, forward-comment in all
1428 ;; supported emacsen so far will stop at eol of each line not
1429 ;; ending with a comment when moving backwards. This corrects for
1430 ;; that, and at the same time handles line continuations.
1431 (while (progn
1432 (skip-chars-backward " \t\n\r\f\v")
1433 (and (looking-at "[\n\r]")
1434 (eq (char-before) ?\\)))
1435 (backward-char))
1436
1437 (if (bobp)
1438 ;; Some emacsen (e.g. Emacs 19.34) return t when moving
1439 ;; backwards at bob.
1440 nil
1441
1442 ;; Leave point after the closest following newline if we've
1443 ;; backed up over any above, since forward-comment won't move
1444 ;; backward over a line comment if point is at the end of the
1445 ;; same line.
1446 (re-search-forward "\\=\\s *[\n\r]" start t)
1447
1448 (if (if (let (open-paren-in-column-0-is-defun-start) (forward-comment -1))
1449 (if (eolp)
1450 ;; If forward-comment above succeeded and we're at eol
1451 ;; then the newline we moved over above didn't end a
1452 ;; line comment, so we give it another go.
1453 (let (open-paren-in-column-0-is-defun-start)
1454 (forward-comment -1))
1455 t))
1456
1457 ;; Emacs <= 20 and XEmacs move back over the closer of a
1458 ;; block comment that lacks an opener.
1459 (if (looking-at "\\*/")
1460 (progn (forward-char 2) nil)
1461 t)))))
1462
1463 (defsubst c-backward-comments ()
1464 "Move backward past all preceding whitespace and comments.
1465 Line continuations, i.e. a backslashes followed by line breaks, are
1466 treated as whitespace. The line breaks that end line comments are
1467 considered to be the comment enders, so the point cannot be at the end
1468 of the same line to move over a line comment. Unlike
1469 c-backward-syntactic-ws, this function doesn't move back over
1470 preprocessor directives.
1471
1472 Note that this function might do hidden buffer changes. See the
1473 comment at the start of cc-engine.el for more info."
1474
1475 (let ((start (point)))
1476 (while (and
1477 ;; `forward-comment' in some emacsen (e.g. XEmacs 21.4)
1478 ;; return t when moving backwards at bob.
1479 (not (bobp))
1480
1481 (if (let (open-paren-in-column-0-is-defun-start moved-comment)
1482 (while
1483 (and (not (setq moved-comment (forward-comment -1)))
1484 ;; Cope specifically with ^M^J here -
1485 ;; forward-comment sometimes gets stuck after ^Ms,
1486 ;; sometimes after ^M^J.
1487 (or
1488 (when (eq (char-before) ?\r)
1489 (backward-char)
1490 t)
1491 (when (and (eq (char-before) ?\n)
1492 (eq (char-before (1- (point))) ?\r))
1493 (backward-char 2)
1494 t))))
1495 moved-comment)
1496 (if (looking-at "\\*/")
1497 ;; Emacs <= 20 and XEmacs move back over the
1498 ;; closer of a block comment that lacks an opener.
1499 (progn (forward-char 2) nil)
1500 t)
1501
1502 ;; XEmacs treats line continuations as whitespace but
1503 ;; only in the backward direction, which seems a bit
1504 ;; odd. Anyway, this is necessary for Emacs.
1505 (when (and (looking-at "[\n\r]")
1506 (eq (char-before) ?\\)
1507 (< (point) start))
1508 (backward-char)
1509 t))))))
1510
1511 \f
1512 ;; Tools for skipping over syntactic whitespace.
1513
1514 ;; The following functions use text properties to cache searches over
1515 ;; large regions of syntactic whitespace. It works as follows:
1516 ;;
1517 ;; o If a syntactic whitespace region contains anything but simple
1518 ;; whitespace (i.e. space, tab and line breaks), the text property
1519 ;; `c-in-sws' is put over it. At places where we have stopped
1520 ;; within that region there's also a `c-is-sws' text property.
1521 ;; That since there typically are nested whitespace inside that
1522 ;; must be handled separately, e.g. whitespace inside a comment or
1523 ;; cpp directive. Thus, from one point with `c-is-sws' it's safe
1524 ;; to jump to another point with that property within the same
1525 ;; `c-in-sws' region. It can be likened to a ladder where
1526 ;; `c-in-sws' marks the bars and `c-is-sws' the rungs.
1527 ;;
1528 ;; o The `c-is-sws' property is put on the simple whitespace chars at
1529 ;; a "rung position" and also maybe on the first following char.
1530 ;; As many characters as can be conveniently found in this range
1531 ;; are marked, but no assumption can be made that the whole range
1532 ;; is marked (it could be clobbered by later changes, for
1533 ;; instance).
1534 ;;
1535 ;; Note that some part of the beginning of a sequence of simple
1536 ;; whitespace might be part of the end of a preceding line comment
1537 ;; or cpp directive and must not be considered part of the "rung".
1538 ;; Such whitespace is some amount of horizontal whitespace followed
1539 ;; by a newline. In the case of cpp directives it could also be
1540 ;; two newlines with horizontal whitespace between them.
1541 ;;
1542 ;; The reason to include the first following char is to cope with
1543 ;; "rung positions" that doesn't have any ordinary whitespace. If
1544 ;; `c-is-sws' is put on a token character it does not have
1545 ;; `c-in-sws' set simultaneously. That's the only case when that
1546 ;; can occur, and the reason for not extending the `c-in-sws'
1547 ;; region to cover it is that the `c-in-sws' region could then be
1548 ;; accidentally merged with a following one if the token is only
1549 ;; one character long.
1550 ;;
1551 ;; o On buffer changes the `c-in-sws' and `c-is-sws' properties are
1552 ;; removed in the changed region. If the change was inside
1553 ;; syntactic whitespace that means that the "ladder" is broken, but
1554 ;; a later call to `c-forward-sws' or `c-backward-sws' will use the
1555 ;; parts on either side and use an ordinary search only to "repair"
1556 ;; the gap.
1557 ;;
1558 ;; Special care needs to be taken if a region is removed: If there
1559 ;; are `c-in-sws' on both sides of it which do not connect inside
1560 ;; the region then they can't be joined. If e.g. a marked macro is
1561 ;; broken, syntactic whitespace inside the new text might be
1562 ;; marked. If those marks would become connected with the old
1563 ;; `c-in-sws' range around the macro then we could get a ladder
1564 ;; with one end outside the macro and the other at some whitespace
1565 ;; within it.
1566 ;;
1567 ;; The main motivation for this system is to increase the speed in
1568 ;; skipping over the large whitespace regions that can occur at the
1569 ;; top level in e.g. header files that contain a lot of comments and
1570 ;; cpp directives. For small comments inside code it's probably
1571 ;; slower than using `forward-comment' straightforwardly, but speed is
1572 ;; not a significant factor there anyway.
1573
1574 ; (defface c-debug-is-sws-face
1575 ; '((t (:background "GreenYellow")))
1576 ; "Debug face to mark the `c-is-sws' property.")
1577 ; (defface c-debug-in-sws-face
1578 ; '((t (:underline t)))
1579 ; "Debug face to mark the `c-in-sws' property.")
1580
1581 ; (defun c-debug-put-sws-faces ()
1582 ; ;; Put the sws debug faces on all the `c-is-sws' and `c-in-sws'
1583 ; ;; properties in the buffer.
1584 ; (interactive)
1585 ; (save-excursion
1586 ; (c-save-buffer-state (in-face)
1587 ; (goto-char (point-min))
1588 ; (setq in-face (if (get-text-property (point) 'c-is-sws)
1589 ; (point)))
1590 ; (while (progn
1591 ; (goto-char (next-single-property-change
1592 ; (point) 'c-is-sws nil (point-max)))
1593 ; (if in-face
1594 ; (progn
1595 ; (c-debug-add-face in-face (point) 'c-debug-is-sws-face)
1596 ; (setq in-face nil))
1597 ; (setq in-face (point)))
1598 ; (not (eobp))))
1599 ; (goto-char (point-min))
1600 ; (setq in-face (if (get-text-property (point) 'c-in-sws)
1601 ; (point)))
1602 ; (while (progn
1603 ; (goto-char (next-single-property-change
1604 ; (point) 'c-in-sws nil (point-max)))
1605 ; (if in-face
1606 ; (progn
1607 ; (c-debug-add-face in-face (point) 'c-debug-in-sws-face)
1608 ; (setq in-face nil))
1609 ; (setq in-face (point)))
1610 ; (not (eobp)))))))
1611
1612 (defmacro c-debug-sws-msg (&rest args)
1613 ;;`(message ,@args)
1614 )
1615
1616 (defmacro c-put-is-sws (beg end)
1617 ;; This macro does a hidden buffer change.
1618 `(let ((beg ,beg) (end ,end))
1619 (put-text-property beg end 'c-is-sws t)
1620 ,@(when (facep 'c-debug-is-sws-face)
1621 `((c-debug-add-face beg end 'c-debug-is-sws-face)))))
1622
1623 (defmacro c-put-in-sws (beg end)
1624 ;; This macro does a hidden buffer change.
1625 `(let ((beg ,beg) (end ,end))
1626 (put-text-property beg end 'c-in-sws t)
1627 ,@(when (facep 'c-debug-is-sws-face)
1628 `((c-debug-add-face beg end 'c-debug-in-sws-face)))))
1629
1630 (defmacro c-remove-is-sws (beg end)
1631 ;; This macro does a hidden buffer change.
1632 `(let ((beg ,beg) (end ,end))
1633 (remove-text-properties beg end '(c-is-sws nil))
1634 ,@(when (facep 'c-debug-is-sws-face)
1635 `((c-debug-remove-face beg end 'c-debug-is-sws-face)))))
1636
1637 (defmacro c-remove-in-sws (beg end)
1638 ;; This macro does a hidden buffer change.
1639 `(let ((beg ,beg) (end ,end))
1640 (remove-text-properties beg end '(c-in-sws nil))
1641 ,@(when (facep 'c-debug-is-sws-face)
1642 `((c-debug-remove-face beg end 'c-debug-in-sws-face)))))
1643
1644 (defmacro c-remove-is-and-in-sws (beg end)
1645 ;; This macro does a hidden buffer change.
1646 `(let ((beg ,beg) (end ,end))
1647 (remove-text-properties beg end '(c-is-sws nil c-in-sws nil))
1648 ,@(when (facep 'c-debug-is-sws-face)
1649 `((c-debug-remove-face beg end 'c-debug-is-sws-face)
1650 (c-debug-remove-face beg end 'c-debug-in-sws-face)))))
1651
1652 (defsubst c-invalidate-sws-region-after (beg end)
1653 ;; Called from `after-change-functions'. Note that if
1654 ;; `c-forward-sws' or `c-backward-sws' are used outside
1655 ;; `c-save-buffer-state' or similar then this will remove the cache
1656 ;; properties right after they're added.
1657 ;;
1658 ;; This function does hidden buffer changes.
1659
1660 (save-excursion
1661 ;; Adjust the end to remove the properties in any following simple
1662 ;; ws up to and including the next line break, if there is any
1663 ;; after the changed region. This is necessary e.g. when a rung
1664 ;; marked empty line is converted to a line comment by inserting
1665 ;; "//" before the line break. In that case the line break would
1666 ;; keep the rung mark which could make a later `c-backward-sws'
1667 ;; move into the line comment instead of over it.
1668 (goto-char end)
1669 (skip-chars-forward " \t\f\v")
1670 (when (and (eolp) (not (eobp)))
1671 (setq end (1+ (point)))))
1672
1673 (when (and (= beg end)
1674 (get-text-property beg 'c-in-sws)
1675 (> beg (point-min))
1676 (get-text-property (1- beg) 'c-in-sws))
1677 ;; Ensure that an `c-in-sws' range gets broken. Note that it isn't
1678 ;; safe to keep a range that was continuous before the change. E.g:
1679 ;;
1680 ;; #define foo
1681 ;; \
1682 ;; bar
1683 ;;
1684 ;; There can be a "ladder" between "#" and "b". Now, if the newline
1685 ;; after "foo" is removed then "bar" will become part of the cpp
1686 ;; directive instead of a syntactically relevant token. In that
1687 ;; case there's no longer syntactic ws from "#" to "b".
1688 (setq beg (1- beg)))
1689
1690 (c-debug-sws-msg "c-invalidate-sws-region-after [%s..%s]" beg end)
1691 (c-remove-is-and-in-sws beg end))
1692
1693 (defun c-forward-sws ()
1694 ;; Used by `c-forward-syntactic-ws' to implement the unbounded search.
1695 ;;
1696 ;; This function might do hidden buffer changes.
1697
1698 (let (;; `rung-pos' is set to a position as early as possible in the
1699 ;; unmarked part of the simple ws region.
1700 (rung-pos (point)) next-rung-pos rung-end-pos last-put-in-sws-pos
1701 rung-is-marked next-rung-is-marked simple-ws-end
1702 ;; `safe-start' is set when it's safe to cache the start position.
1703 ;; It's not set if we've initially skipped over comments and line
1704 ;; continuations since we might have gone out through the end of a
1705 ;; macro then. This provision makes `c-forward-sws' not populate the
1706 ;; cache in the majority of cases, but otoh is `c-backward-sws' by far
1707 ;; more common.
1708 safe-start)
1709
1710 ;; Skip simple ws and do a quick check on the following character to see
1711 ;; if it's anything that can't start syntactic ws, so we can bail out
1712 ;; early in the majority of cases when there just are a few ws chars.
1713 (skip-chars-forward " \t\n\r\f\v")
1714 (when (looking-at c-syntactic-ws-start)
1715
1716 (setq rung-end-pos (min (1+ (point)) (point-max)))
1717 (if (setq rung-is-marked (text-property-any rung-pos rung-end-pos
1718 'c-is-sws t))
1719 ;; Find the last rung position to avoid setting properties in all
1720 ;; the cases when the marked rung is complete.
1721 ;; (`next-single-property-change' is certain to move at least one
1722 ;; step forward.)
1723 (setq rung-pos (1- (next-single-property-change
1724 rung-is-marked 'c-is-sws nil rung-end-pos)))
1725 ;; Got no marked rung here. Since the simple ws might have started
1726 ;; inside a line comment or cpp directive we must set `rung-pos' as
1727 ;; high as possible.
1728 (setq rung-pos (point)))
1729
1730 (with-silent-modifications
1731 (while
1732 (progn
1733 (while
1734 (when (and rung-is-marked
1735 (get-text-property (point) 'c-in-sws))
1736
1737 ;; The following search is the main reason that `c-in-sws'
1738 ;; and `c-is-sws' aren't combined to one property.
1739 (goto-char (next-single-property-change
1740 (point) 'c-in-sws nil (point-max)))
1741 (unless (get-text-property (point) 'c-is-sws)
1742 ;; If the `c-in-sws' region extended past the last
1743 ;; `c-is-sws' char we have to go back a bit.
1744 (or (get-text-property (1- (point)) 'c-is-sws)
1745 (goto-char (previous-single-property-change
1746 (point) 'c-is-sws)))
1747 (backward-char))
1748
1749 (c-debug-sws-msg
1750 "c-forward-sws cached move %s -> %s (max %s)"
1751 rung-pos (point) (point-max))
1752
1753 (setq rung-pos (point))
1754 (and (> (skip-chars-forward " \t\n\r\f\v") 0)
1755 (not (eobp))))
1756
1757 ;; We'll loop here if there is simple ws after the last rung.
1758 ;; That means that there's been some change in it and it's
1759 ;; possible that we've stepped into another ladder, so extend
1760 ;; the previous one to join with it if there is one, and try to
1761 ;; use the cache again.
1762 (c-debug-sws-msg
1763 "c-forward-sws extending rung with [%s..%s] (max %s)"
1764 (1+ rung-pos) (1+ (point)) (point-max))
1765 (unless (get-text-property (point) 'c-is-sws)
1766 ;; Remove any `c-in-sws' property from the last char of
1767 ;; the rung before we mark it with `c-is-sws', so that we
1768 ;; won't connect with the remains of a broken "ladder".
1769 (c-remove-in-sws (point) (1+ (point))))
1770 (c-put-is-sws (1+ rung-pos)
1771 (1+ (point)))
1772 (c-put-in-sws rung-pos
1773 (setq rung-pos (point)
1774 last-put-in-sws-pos rung-pos)))
1775
1776 (setq simple-ws-end (point))
1777 (c-forward-comments)
1778
1779 (cond
1780 ((/= (point) simple-ws-end)
1781 ;; Skipped over comments. Don't cache at eob in case the buffer
1782 ;; is narrowed.
1783 (not (eobp)))
1784
1785 ((save-excursion
1786 (and c-opt-cpp-prefix
1787 (looking-at c-opt-cpp-start)
1788 (progn (skip-chars-backward " \t")
1789 (bolp))
1790 (or (bobp)
1791 (progn (backward-char)
1792 (not (eq (char-before) ?\\))))))
1793 ;; Skip a preprocessor directive.
1794 (end-of-line)
1795 (while (and (eq (char-before) ?\\)
1796 (= (forward-line 1) 0))
1797 (end-of-line))
1798 (forward-line 1)
1799 (setq safe-start t)
1800 ;; Don't cache at eob in case the buffer is narrowed.
1801 (not (eobp)))))
1802
1803 ;; We've searched over a piece of non-white syntactic ws. See if this
1804 ;; can be cached.
1805 (setq next-rung-pos (point))
1806 (skip-chars-forward " \t\n\r\f\v")
1807 (setq rung-end-pos (min (1+ (point)) (point-max)))
1808
1809 (if (or
1810 ;; Cache if we haven't skipped comments only, and if we started
1811 ;; either from a marked rung or from a completely uncached
1812 ;; position.
1813 (and safe-start
1814 (or rung-is-marked
1815 (not (get-text-property simple-ws-end 'c-in-sws))))
1816
1817 ;; See if there's a marked rung in the encountered simple ws. If
1818 ;; so then we can cache, unless `safe-start' is nil. Even then
1819 ;; we need to do this to check if the cache can be used for the
1820 ;; next step.
1821 (and (setq next-rung-is-marked
1822 (text-property-any next-rung-pos rung-end-pos
1823 'c-is-sws t))
1824 safe-start))
1825
1826 (progn
1827 (c-debug-sws-msg
1828 "c-forward-sws caching [%s..%s] - [%s..%s] (max %s)"
1829 rung-pos (1+ simple-ws-end) next-rung-pos rung-end-pos
1830 (point-max))
1831
1832 ;; Remove the properties for any nested ws that might be cached.
1833 ;; Only necessary for `c-is-sws' since `c-in-sws' will be set
1834 ;; anyway.
1835 (c-remove-is-sws (1+ simple-ws-end) next-rung-pos)
1836 (unless (and rung-is-marked (= rung-pos simple-ws-end))
1837 (c-put-is-sws rung-pos
1838 (1+ simple-ws-end))
1839 (setq rung-is-marked t))
1840 (c-put-in-sws rung-pos
1841 (setq rung-pos (point)
1842 last-put-in-sws-pos rung-pos))
1843 (unless (get-text-property (1- rung-end-pos) 'c-is-sws)
1844 ;; Remove any `c-in-sws' property from the last char of
1845 ;; the rung before we mark it with `c-is-sws', so that we
1846 ;; won't connect with the remains of a broken "ladder".
1847 (c-remove-in-sws (1- rung-end-pos) rung-end-pos))
1848 (c-put-is-sws next-rung-pos
1849 rung-end-pos))
1850
1851 (c-debug-sws-msg
1852 "c-forward-sws not caching [%s..%s] - [%s..%s] (max %s)"
1853 rung-pos (1+ simple-ws-end) next-rung-pos rung-end-pos
1854 (point-max))
1855
1856 ;; Set `rung-pos' for the next rung. It's the same thing here as
1857 ;; initially, except that the rung position is set as early as
1858 ;; possible since we can't be in the ending ws of a line comment or
1859 ;; cpp directive now.
1860 (if (setq rung-is-marked next-rung-is-marked)
1861 (setq rung-pos (1- (next-single-property-change
1862 rung-is-marked 'c-is-sws nil rung-end-pos)))
1863 (setq rung-pos next-rung-pos))
1864 (setq safe-start t)))
1865
1866 ;; Make sure that the newly marked `c-in-sws' region doesn't connect to
1867 ;; another one after the point (which might occur when editing inside a
1868 ;; comment or macro).
1869 (when (eq last-put-in-sws-pos (point))
1870 (cond ((< last-put-in-sws-pos (point-max))
1871 (c-debug-sws-msg
1872 "c-forward-sws clearing at %s for cache separation"
1873 last-put-in-sws-pos)
1874 (c-remove-in-sws last-put-in-sws-pos
1875 (1+ last-put-in-sws-pos)))
1876 (t
1877 ;; If at eob we have to clear the last character before the end
1878 ;; instead since the buffer might be narrowed and there might
1879 ;; be a `c-in-sws' after (point-max). In this case it's
1880 ;; necessary to clear both properties.
1881 (c-debug-sws-msg
1882 "c-forward-sws clearing thoroughly at %s for cache separation"
1883 (1- last-put-in-sws-pos))
1884 (c-remove-is-and-in-sws (1- last-put-in-sws-pos)
1885 last-put-in-sws-pos))))
1886 ))))
1887
1888 (defun c-backward-sws ()
1889 ;; Used by `c-backward-syntactic-ws' to implement the unbounded search.
1890 ;;
1891 ;; This function might do hidden buffer changes.
1892
1893 (let (;; `rung-pos' is set to a position as late as possible in the unmarked
1894 ;; part of the simple ws region.
1895 (rung-pos (point)) next-rung-pos last-put-in-sws-pos
1896 rung-is-marked simple-ws-beg cmt-skip-pos)
1897
1898 ;; Skip simple horizontal ws and do a quick check on the preceding
1899 ;; character to see if it's anything that can't end syntactic ws, so we can
1900 ;; bail out early in the majority of cases when there just are a few ws
1901 ;; chars. Newlines are complicated in the backward direction, so we can't
1902 ;; skip over them.
1903 (skip-chars-backward " \t\f")
1904 (when (and (not (bobp))
1905 (save-excursion
1906 (backward-char)
1907 (looking-at c-syntactic-ws-end)))
1908
1909 ;; Try to find a rung position in the simple ws preceding point, so that
1910 ;; we can get a cache hit even if the last bit of the simple ws has
1911 ;; changed recently.
1912 (setq simple-ws-beg (point))
1913 (skip-chars-backward " \t\n\r\f\v")
1914 (if (setq rung-is-marked (text-property-any
1915 (point) (min (1+ rung-pos) (point-max))
1916 'c-is-sws t))
1917 ;; `rung-pos' will be the earliest marked position, which means that
1918 ;; there might be later unmarked parts in the simple ws region.
1919 ;; It's not worth the effort to fix that; the last part of the
1920 ;; simple ws is also typically edited often, so it could be wasted.
1921 (goto-char (setq rung-pos rung-is-marked))
1922 (goto-char simple-ws-beg))
1923
1924 (with-silent-modifications
1925 (while
1926 (progn
1927 (while
1928 (when (and rung-is-marked
1929 (not (bobp))
1930 (get-text-property (1- (point)) 'c-in-sws))
1931
1932 ;; The following search is the main reason that `c-in-sws'
1933 ;; and `c-is-sws' aren't combined to one property.
1934 (goto-char (previous-single-property-change
1935 (point) 'c-in-sws nil (point-min)))
1936 (unless (get-text-property (point) 'c-is-sws)
1937 ;; If the `c-in-sws' region extended past the first
1938 ;; `c-is-sws' char we have to go forward a bit.
1939 (goto-char (next-single-property-change
1940 (point) 'c-is-sws)))
1941
1942 (c-debug-sws-msg
1943 "c-backward-sws cached move %s <- %s (min %s)"
1944 (point) rung-pos (point-min))
1945
1946 (setq rung-pos (point))
1947 (if (and (< (min (skip-chars-backward " \t\f\v")
1948 (progn
1949 (setq simple-ws-beg (point))
1950 (skip-chars-backward " \t\n\r\f\v")))
1951 0)
1952 (setq rung-is-marked
1953 (text-property-any (point) rung-pos
1954 'c-is-sws t)))
1955 t
1956 (goto-char simple-ws-beg)
1957 nil))
1958
1959 ;; We'll loop here if there is simple ws before the first rung.
1960 ;; That means that there's been some change in it and it's
1961 ;; possible that we've stepped into another ladder, so extend
1962 ;; the previous one to join with it if there is one, and try to
1963 ;; use the cache again.
1964 (c-debug-sws-msg
1965 "c-backward-sws extending rung with [%s..%s] (min %s)"
1966 rung-is-marked rung-pos (point-min))
1967 (unless (get-text-property (1- rung-pos) 'c-is-sws)
1968 ;; Remove any `c-in-sws' property from the last char of
1969 ;; the rung before we mark it with `c-is-sws', so that we
1970 ;; won't connect with the remains of a broken "ladder".
1971 (c-remove-in-sws (1- rung-pos) rung-pos))
1972 (c-put-is-sws rung-is-marked
1973 rung-pos)
1974 (c-put-in-sws rung-is-marked
1975 (1- rung-pos))
1976 (setq rung-pos rung-is-marked
1977 last-put-in-sws-pos rung-pos))
1978
1979 (c-backward-comments)
1980 (setq cmt-skip-pos (point))
1981
1982 (cond
1983 ((and c-opt-cpp-prefix
1984 (/= cmt-skip-pos simple-ws-beg)
1985 (c-beginning-of-macro))
1986 ;; Inside a cpp directive. See if it should be skipped over.
1987 (let ((cpp-beg (point)))
1988
1989 ;; Move back over all line continuations in the region skipped
1990 ;; over by `c-backward-comments'. If we go past it then we
1991 ;; started inside the cpp directive.
1992 (goto-char simple-ws-beg)
1993 (beginning-of-line)
1994 (while (and (> (point) cmt-skip-pos)
1995 (progn (backward-char)
1996 (eq (char-before) ?\\)))
1997 (beginning-of-line))
1998
1999 (if (< (point) cmt-skip-pos)
2000 ;; Don't move past the cpp directive if we began inside
2001 ;; it. Note that the position at the end of the last line
2002 ;; of the macro is also considered to be within it.
2003 (progn (goto-char cmt-skip-pos)
2004 nil)
2005
2006 ;; It's worthwhile to spend a little bit of effort on finding
2007 ;; the end of the macro, to get a good `simple-ws-beg'
2008 ;; position for the cache. Note that `c-backward-comments'
2009 ;; could have stepped over some comments before going into
2010 ;; the macro, and then `simple-ws-beg' must be kept on the
2011 ;; same side of those comments.
2012 (goto-char simple-ws-beg)
2013 (skip-chars-backward " \t\n\r\f\v")
2014 (if (eq (char-before) ?\\)
2015 (forward-char))
2016 (forward-line 1)
2017 (if (< (point) simple-ws-beg)
2018 ;; Might happen if comments after the macro were skipped
2019 ;; over.
2020 (setq simple-ws-beg (point)))
2021
2022 (goto-char cpp-beg)
2023 t)))
2024
2025 ((/= (save-excursion
2026 (skip-chars-forward " \t\n\r\f\v" simple-ws-beg)
2027 (setq next-rung-pos (point)))
2028 simple-ws-beg)
2029 ;; Skipped over comments. Must put point at the end of
2030 ;; the simple ws at point since we might be after a line
2031 ;; comment or cpp directive that's been partially
2032 ;; narrowed out, and we can't risk marking the simple ws
2033 ;; at the end of it.
2034 (goto-char next-rung-pos)
2035 t)))
2036
2037 ;; We've searched over a piece of non-white syntactic ws. See if this
2038 ;; can be cached.
2039 (setq next-rung-pos (point))
2040 (skip-chars-backward " \t\f\v")
2041
2042 (if (or
2043 ;; Cache if we started either from a marked rung or from a
2044 ;; completely uncached position.
2045 rung-is-marked
2046 (not (get-text-property (1- simple-ws-beg) 'c-in-sws))
2047
2048 ;; Cache if there's a marked rung in the encountered simple ws.
2049 (save-excursion
2050 (skip-chars-backward " \t\n\r\f\v")
2051 (text-property-any (point) (min (1+ next-rung-pos) (point-max))
2052 'c-is-sws t)))
2053
2054 (progn
2055 (c-debug-sws-msg
2056 "c-backward-sws caching [%s..%s] - [%s..%s] (min %s)"
2057 (point) (1+ next-rung-pos)
2058 simple-ws-beg (min (1+ rung-pos) (point-max))
2059 (point-min))
2060
2061 ;; Remove the properties for any nested ws that might be cached.
2062 ;; Only necessary for `c-is-sws' since `c-in-sws' will be set
2063 ;; anyway.
2064 (c-remove-is-sws (1+ next-rung-pos) simple-ws-beg)
2065 (unless (and rung-is-marked (= simple-ws-beg rung-pos))
2066 (let ((rung-end-pos (min (1+ rung-pos) (point-max))))
2067 (unless (get-text-property (1- rung-end-pos) 'c-is-sws)
2068 ;; Remove any `c-in-sws' property from the last char of
2069 ;; the rung before we mark it with `c-is-sws', so that we
2070 ;; won't connect with the remains of a broken "ladder".
2071 (c-remove-in-sws (1- rung-end-pos) rung-end-pos))
2072 (c-put-is-sws simple-ws-beg
2073 rung-end-pos)
2074 (setq rung-is-marked t)))
2075 (c-put-in-sws (setq simple-ws-beg (point)
2076 last-put-in-sws-pos simple-ws-beg)
2077 rung-pos)
2078 (c-put-is-sws (setq rung-pos simple-ws-beg)
2079 (1+ next-rung-pos)))
2080
2081 (c-debug-sws-msg
2082 "c-backward-sws not caching [%s..%s] - [%s..%s] (min %s)"
2083 (point) (1+ next-rung-pos)
2084 simple-ws-beg (min (1+ rung-pos) (point-max))
2085 (point-min))
2086 (setq rung-pos next-rung-pos
2087 simple-ws-beg (point))
2088 ))
2089
2090 ;; Make sure that the newly marked `c-in-sws' region doesn't connect to
2091 ;; another one before the point (which might occur when editing inside a
2092 ;; comment or macro).
2093 (when (eq last-put-in-sws-pos (point))
2094 (cond ((< (point-min) last-put-in-sws-pos)
2095 (c-debug-sws-msg
2096 "c-backward-sws clearing at %s for cache separation"
2097 (1- last-put-in-sws-pos))
2098 (c-remove-in-sws (1- last-put-in-sws-pos)
2099 last-put-in-sws-pos))
2100 ((> (point-min) 1)
2101 ;; If at bob and the buffer is narrowed, we have to clear the
2102 ;; character we're standing on instead since there might be a
2103 ;; `c-in-sws' before (point-min). In this case it's necessary
2104 ;; to clear both properties.
2105 (c-debug-sws-msg
2106 "c-backward-sws clearing thoroughly at %s for cache separation"
2107 last-put-in-sws-pos)
2108 (c-remove-is-and-in-sws last-put-in-sws-pos
2109 (1+ last-put-in-sws-pos)))))
2110 ))))
2111
2112 \f
2113 ;; Other whitespace tools
2114 (defun c-partial-ws-p (beg end)
2115 ;; Is the region (beg end) WS, and is there WS (or BOB/EOB) next to the
2116 ;; region? This is a "heuristic" function. .....
2117 ;;
2118 ;; The motivation for the second bit is to check whether removing this
2119 ;; region would coalesce two symbols.
2120 ;;
2121 ;; FIXME!!! This function doesn't check virtual semicolons in any way. Be
2122 ;; careful about using this function for, e.g. AWK. (2007/3/7)
2123 (save-excursion
2124 (let ((end+1 (min (1+ end) (point-max))))
2125 (or (progn (goto-char (max (point-min) (1- beg)))
2126 (c-skip-ws-forward end)
2127 (eq (point) end))
2128 (progn (goto-char beg)
2129 (c-skip-ws-forward end+1)
2130 (eq (point) end+1))))))
2131 \f
2132 ;; A system for finding noteworthy parens before the point.
2133
2134 (defconst c-state-cache-too-far 5000)
2135 ;; A maximum comfortable scanning distance, e.g. between
2136 ;; `c-state-cache-good-pos' and "HERE" (where we call c-parse-state). When
2137 ;; this distance is exceeded, we take "emergency measures", e.g. by clearing
2138 ;; the cache and starting again from point-min or a beginning of defun. This
2139 ;; value can be tuned for efficiency or set to a lower value for testing.
2140
2141 (defvar c-state-cache nil)
2142 (make-variable-buffer-local 'c-state-cache)
2143 ;; The state cache used by `c-parse-state' to cut down the amount of
2144 ;; searching. It's the result from some earlier `c-parse-state' call. See
2145 ;; `c-parse-state''s doc string for details of its structure.
2146 ;;
2147 ;; The use of the cached info is more effective if the next
2148 ;; `c-parse-state' call is on a line close by the one the cached state
2149 ;; was made at; the cache can actually slow down a little if the
2150 ;; cached state was made very far back in the buffer. The cache is
2151 ;; most effective if `c-parse-state' is used on each line while moving
2152 ;; forward.
2153
2154 (defvar c-state-cache-good-pos 1)
2155 (make-variable-buffer-local 'c-state-cache-good-pos)
2156 ;; This is a position where `c-state-cache' is known to be correct, or
2157 ;; nil (see below). It's a position inside one of the recorded unclosed
2158 ;; parens or the top level, but not further nested inside any literal or
2159 ;; subparen that is closed before the last recorded position.
2160 ;;
2161 ;; The exact position is chosen to try to be close to yet earlier than
2162 ;; the position where `c-state-cache' will be called next. Right now
2163 ;; the heuristic is to set it to the position after the last found
2164 ;; closing paren (of any type) before the line on which
2165 ;; `c-parse-state' was called. That is chosen primarily to work well
2166 ;; with refontification of the current line.
2167 ;;
2168 ;; 2009-07-28: When `c-state-point-min' and the last position where
2169 ;; `c-parse-state' or for which `c-invalidate-state-cache' was called, are
2170 ;; both in the same literal, there is no such "good position", and
2171 ;; c-state-cache-good-pos is then nil. This is the ONLY circumstance in which
2172 ;; it can be nil. In this case, `c-state-point-min-literal' will be non-nil.
2173 ;;
2174 ;; 2009-06-12: In a brace desert, c-state-cache-good-pos may also be in
2175 ;; the middle of the desert, as long as it is not within a brace pair
2176 ;; recorded in `c-state-cache' or a paren/bracket pair.
2177
2178
2179 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2180 ;; We maintain a simple cache of positions which aren't in a literal, so as to
2181 ;; speed up testing for non-literality.
2182 (defconst c-state-nonlit-pos-interval 3000)
2183 ;; The approximate interval between entries in `c-state-nonlit-pos-cache'.
2184
2185 (defvar c-state-nonlit-pos-cache nil)
2186 (make-variable-buffer-local 'c-state-nonlit-pos-cache)
2187 ;; A list of buffer positions which are known not to be in a literal or a cpp
2188 ;; construct. This is ordered with higher positions at the front of the list.
2189 ;; Only those which are less than `c-state-nonlit-pos-cache-limit' are valid.
2190
2191 (defvar c-state-nonlit-pos-cache-limit 1)
2192 (make-variable-buffer-local 'c-state-nonlit-pos-cache-limit)
2193 ;; An upper limit on valid entries in `c-state-nonlit-pos-cache'. This is
2194 ;; reduced by buffer changes, and increased by invocations of
2195 ;; `c-state-literal-at'.
2196
2197 (defvar c-state-semi-nonlit-pos-cache nil)
2198 (make-variable-buffer-local 'c-state-semi-nonlit-pos-cache)
2199 ;; A list of buffer positions which are known not to be in a literal. This is
2200 ;; ordered with higher positions at the front of the list. Only those which
2201 ;; are less than `c-state-semi-nonlit-pos-cache-limit' are valid.
2202
2203 (defvar c-state-semi-nonlit-pos-cache-limit 1)
2204 (make-variable-buffer-local 'c-state-semi-nonlit-pos-cache-limit)
2205 ;; An upper limit on valid entries in `c-state-semi-nonlit-pos-cache'. This is
2206 ;; reduced by buffer changes, and increased by invocations of
2207 ;; `c-state-literal-at'. FIXME!!!
2208
2209 (defsubst c-state-pp-to-literal (from to &optional not-in-delimiter)
2210 ;; Do a parse-partial-sexp from FROM to TO, returning either
2211 ;; (STATE TYPE (BEG . END)) if TO is in a literal; or
2212 ;; (STATE) otherwise,
2213 ;; where STATE is the parsing state at TO, TYPE is the type of the literal
2214 ;; (one of 'c, 'c++, 'string) and (BEG . END) is the boundaries of the literal.
2215 ;;
2216 ;; Unless NOT-IN-DELIMITER is non-nil, when TO is inside a two-character
2217 ;; comment opener, this is recognized as being in a comment literal.
2218 ;;
2219 ;; Only elements 3 (in a string), 4 (in a comment), 5 (following a quote),
2220 ;; 7 (comment type) and 8 (start of comment/string) (and possibly 9) of
2221 ;; STATE are valid.
2222 (save-excursion
2223 (let ((s (parse-partial-sexp from to))
2224 ty co-st)
2225 (cond
2226 ((or (nth 3 s) (nth 4 s)) ; in a string or comment
2227 (setq ty (cond
2228 ((nth 3 s) 'string)
2229 ((nth 7 s) 'c++)
2230 (t 'c)))
2231 (parse-partial-sexp (point) (point-max)
2232 nil ; TARGETDEPTH
2233 nil ; STOPBEFORE
2234 s ; OLDSTATE
2235 'syntax-table) ; stop at end of literal
2236 `(,s ,ty (,(nth 8 s) . ,(point))))
2237
2238 ((and (not not-in-delimiter) ; inside a comment starter
2239 (not (bobp))
2240 (progn (backward-char)
2241 (and (not (looking-at "\\s!"))
2242 (looking-at c-comment-start-regexp))))
2243 (setq ty (if (looking-at c-block-comment-start-regexp) 'c 'c++)
2244 co-st (point))
2245 (forward-comment 1)
2246 `(,s ,ty (,co-st . ,(point))))
2247
2248 (t `(,s))))))
2249
2250 (defun c-state-safe-place (here)
2251 ;; Return a buffer position before HERE which is "safe", i.e. outside any
2252 ;; string, comment, or macro.
2253 ;;
2254 ;; NOTE: This function manipulates `c-state-nonlit-pos-cache'. This cache
2255 ;; MAY NOT contain any positions within macros, since macros are frequently
2256 ;; turned into comments by use of the `c-cpp-delimiter' category properties.
2257 ;; We cannot rely on this mechanism whilst determining a cache pos since
2258 ;; this function is also called from outwith `c-parse-state'.
2259 (save-restriction
2260 (widen)
2261 (save-excursion
2262 (let ((c c-state-nonlit-pos-cache)
2263 pos npos high-pos lit macro-beg macro-end)
2264 ;; Trim the cache to take account of buffer changes.
2265 (while (and c (> (car c) c-state-nonlit-pos-cache-limit))
2266 (setq c (cdr c)))
2267 (setq c-state-nonlit-pos-cache c)
2268
2269 (while (and c (> (car c) here))
2270 (setq high-pos (car c))
2271 (setq c (cdr c)))
2272 (setq pos (or (car c) (point-min)))
2273
2274 (unless high-pos
2275 (while
2276 ;; Add an element to `c-state-nonlit-pos-cache' each iteration.
2277 (and
2278 (<= (setq npos (+ pos c-state-nonlit-pos-interval)) here)
2279
2280 ;; Test for being in a literal. If so, go to after it.
2281 (progn
2282 (setq lit (car (cddr (c-state-pp-to-literal pos npos))))
2283 (or (null lit)
2284 (prog1 (<= (cdr lit) here)
2285 (setq npos (cdr lit)))))
2286
2287 ;; Test for being in a macro. If so, go to after it.
2288 (progn
2289 (goto-char npos)
2290 (setq macro-beg
2291 (and (c-beginning-of-macro) (/= (point) npos) (point)))
2292 (when macro-beg
2293 (c-syntactic-end-of-macro)
2294 (or (eobp) (forward-char))
2295 (setq macro-end (point)))
2296 (or (null macro-beg)
2297 (prog1 (<= macro-end here)
2298 (setq npos macro-end)))))
2299
2300 (setq pos npos)
2301 (setq c-state-nonlit-pos-cache (cons pos c-state-nonlit-pos-cache)))
2302 ;; Add one extra element above HERE so as to to avoid the previous
2303 ;; expensive calculation when the next call is close to the current
2304 ;; one. This is especially useful when inside a large macro.
2305 (setq c-state-nonlit-pos-cache (cons npos c-state-nonlit-pos-cache)))
2306
2307 (if (> pos c-state-nonlit-pos-cache-limit)
2308 (setq c-state-nonlit-pos-cache-limit pos))
2309 pos))))
2310
2311 (defun c-state-semi-safe-place (here)
2312 ;; Return a buffer position before HERE which is "safe", i.e. outside any
2313 ;; string or comment. It may be in a macro.
2314 (save-restriction
2315 (widen)
2316 (save-excursion
2317 (let ((c c-state-semi-nonlit-pos-cache)
2318 pos npos high-pos lit macro-beg macro-end)
2319 ;; Trim the cache to take account of buffer changes.
2320 (while (and c (> (car c) c-state-semi-nonlit-pos-cache-limit))
2321 (setq c (cdr c)))
2322 (setq c-state-semi-nonlit-pos-cache c)
2323
2324 (while (and c (> (car c) here))
2325 (setq high-pos (car c))
2326 (setq c (cdr c)))
2327 (setq pos (or (car c) (point-min)))
2328
2329 (unless high-pos
2330 (while
2331 ;; Add an element to `c-state-semi-nonlit-pos-cache' each iteration.
2332 (and
2333 (<= (setq npos (+ pos c-state-nonlit-pos-interval)) here)
2334
2335 ;; Test for being in a literal. If so, go to after it.
2336 (progn
2337 (setq lit (car (cddr (c-state-pp-to-literal pos npos))))
2338 (or (null lit)
2339 (prog1 (<= (cdr lit) here)
2340 (setq npos (cdr lit))))))
2341
2342 (setq pos npos)
2343 (setq c-state-semi-nonlit-pos-cache
2344 (cons pos c-state-semi-nonlit-pos-cache))))
2345
2346 (if (> pos c-state-semi-nonlit-pos-cache-limit)
2347 (setq c-state-semi-nonlit-pos-cache-limit pos))
2348 pos))))
2349
2350 (defun c-state-literal-at (here)
2351 ;; If position HERE is inside a literal, return (START . END), the
2352 ;; boundaries of the literal (which may be outside the accessible bit of the
2353 ;; buffer). Otherwise, return nil.
2354 ;;
2355 ;; This function is almost the same as `c-literal-limits'. Previously, it
2356 ;; differed in that it was a lower level function, and that it rigorously
2357 ;; followed the syntax from BOB. `c-literal-limits' is now (2011-12)
2358 ;; virtually identical to this function.
2359 (save-restriction
2360 (widen)
2361 (save-excursion
2362 (let ((pos (c-state-safe-place here)))
2363 (car (cddr (c-state-pp-to-literal pos here)))))))
2364
2365 (defsubst c-state-lit-beg (pos)
2366 ;; Return the start of the literal containing POS, or POS itself.
2367 (or (car (c-state-literal-at pos))
2368 pos))
2369
2370 (defsubst c-state-cache-non-literal-place (pos state)
2371 ;; Return a position outside of a string/comment/macro at or before POS.
2372 ;; STATE is the parse-partial-sexp state at POS.
2373 (let ((res (if (or (nth 3 state) ; in a string?
2374 (nth 4 state)) ; in a comment?
2375 (nth 8 state)
2376 pos)))
2377 (save-excursion
2378 (goto-char res)
2379 (if (c-beginning-of-macro)
2380 (point)
2381 res))))
2382
2383 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2384 ;; Stuff to do with point-min, and coping with any literal there.
2385 (defvar c-state-point-min 1)
2386 (make-variable-buffer-local 'c-state-point-min)
2387 ;; This is (point-min) when `c-state-cache' was last calculated. A change of
2388 ;; narrowing is likely to affect the parens that are visible before the point.
2389
2390 (defvar c-state-point-min-lit-type nil)
2391 (make-variable-buffer-local 'c-state-point-min-lit-type)
2392 (defvar c-state-point-min-lit-start nil)
2393 (make-variable-buffer-local 'c-state-point-min-lit-start)
2394 ;; These two variables define the literal, if any, containing point-min.
2395 ;; Their values are, respectively, 'string, c, or c++, and the start of the
2396 ;; literal. If there's no literal there, they're both nil.
2397
2398 (defvar c-state-min-scan-pos 1)
2399 (make-variable-buffer-local 'c-state-min-scan-pos)
2400 ;; This is the earliest buffer-pos from which scanning can be done. It is
2401 ;; either the end of the literal containing point-min, or point-min itself.
2402 ;; It becomes nil if the buffer is changed earlier than this point.
2403 (defun c-state-get-min-scan-pos ()
2404 ;; Return the lowest valid scanning pos. This will be the end of the
2405 ;; literal enclosing point-min, or point-min itself.
2406 (or c-state-min-scan-pos
2407 (save-restriction
2408 (save-excursion
2409 (widen)
2410 (goto-char c-state-point-min-lit-start)
2411 (if (eq c-state-point-min-lit-type 'string)
2412 (forward-sexp)
2413 (forward-comment 1))
2414 (setq c-state-min-scan-pos (point))))))
2415
2416 (defun c-state-mark-point-min-literal ()
2417 ;; Determine the properties of any literal containing POINT-MIN, setting the
2418 ;; variables `c-state-point-min-lit-type', `c-state-point-min-lit-start',
2419 ;; and `c-state-min-scan-pos' accordingly. The return value is meaningless.
2420 (let ((p-min (point-min))
2421 lit)
2422 (save-restriction
2423 (widen)
2424 (setq lit (c-state-literal-at p-min))
2425 (if lit
2426 (setq c-state-point-min-lit-type
2427 (save-excursion
2428 (goto-char (car lit))
2429 (cond
2430 ((looking-at c-block-comment-start-regexp) 'c)
2431 ((looking-at c-line-comment-starter) 'c++)
2432 (t 'string)))
2433 c-state-point-min-lit-start (car lit)
2434 c-state-min-scan-pos (cdr lit))
2435 (setq c-state-point-min-lit-type nil
2436 c-state-point-min-lit-start nil
2437 c-state-min-scan-pos p-min)))))
2438
2439
2440 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2441 ;; A variable which signals a brace dessert - helpful for reducing the number
2442 ;; of fruitless backward scans.
2443 (defvar c-state-brace-pair-desert nil)
2444 (make-variable-buffer-local 'c-state-brace-pair-desert)
2445 ;; Used only in `c-append-lower-brace-pair-to-state-cache'. It is set when
2446 ;; that defun has searched backwards for a brace pair and not found one. Its
2447 ;; value is either nil or a cons (PA . FROM), where PA is the position of the
2448 ;; enclosing opening paren/brace/bracket which bounds the backwards search (or
2449 ;; nil when at top level) and FROM is where the backward search started. It
2450 ;; is reset to nil in `c-invalidate-state-cache'.
2451
2452
2453 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2454 ;; Lowish level functions/macros which work directly on `c-state-cache', or a
2455 ;; list of like structure.
2456 (defmacro c-state-cache-top-lparen (&optional cache)
2457 ;; Return the address of the top left brace/bracket/paren recorded in CACHE
2458 ;; (default `c-state-cache') (or nil).
2459 (let ((cash (or cache 'c-state-cache)))
2460 `(if (consp (car ,cash))
2461 (caar ,cash)
2462 (car ,cash))))
2463
2464 (defmacro c-state-cache-top-paren (&optional cache)
2465 ;; Return the address of the latest brace/bracket/paren (whether left or
2466 ;; right) recorded in CACHE (default `c-state-cache') or nil.
2467 (let ((cash (or cache 'c-state-cache)))
2468 `(if (consp (car ,cash))
2469 (cdar ,cash)
2470 (car ,cash))))
2471
2472 (defmacro c-state-cache-after-top-paren (&optional cache)
2473 ;; Return the position just after the latest brace/bracket/paren (whether
2474 ;; left or right) recorded in CACHE (default `c-state-cache') or nil.
2475 (let ((cash (or cache 'c-state-cache)))
2476 `(if (consp (car ,cash))
2477 (cdar ,cash)
2478 (and (car ,cash)
2479 (1+ (car ,cash))))))
2480
2481 (defun c-get-cache-scan-pos (here)
2482 ;; From the state-cache, determine the buffer position from which we might
2483 ;; scan forward to HERE to update this cache. This position will be just
2484 ;; after a paren/brace/bracket recorded in the cache, if possible, otherwise
2485 ;; return the earliest position in the accessible region which isn't within
2486 ;; a literal. If the visible portion of the buffer is entirely within a
2487 ;; literal, return NIL.
2488 (let ((c c-state-cache) elt)
2489 ;(while (>= (or (c-state-cache-top-lparen c) 1) here)
2490 (while (and c
2491 (>= (c-state-cache-top-lparen c) here))
2492 (setq c (cdr c)))
2493
2494 (setq elt (car c))
2495 (cond
2496 ((consp elt)
2497 (if (> (cdr elt) here)
2498 (1+ (car elt))
2499 (cdr elt)))
2500 (elt (1+ elt))
2501 ((<= (c-state-get-min-scan-pos) here)
2502 (c-state-get-min-scan-pos))
2503 (t nil))))
2504
2505 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2506 ;; Variables which keep track of preprocessor constructs.
2507 (defvar c-state-old-cpp-beg-marker nil)
2508 (make-variable-buffer-local 'c-state-old-cpp-beg-marker)
2509 (defvar c-state-old-cpp-beg nil)
2510 (make-variable-buffer-local 'c-state-old-cpp-beg)
2511 (defvar c-state-old-cpp-end-marker nil)
2512 (make-variable-buffer-local 'c-state-old-cpp-end-marker)
2513 (defvar c-state-old-cpp-end nil)
2514 (make-variable-buffer-local 'c-state-old-cpp-end)
2515 ;; These are the limits of the macro containing point at the previous call of
2516 ;; `c-parse-state', or nil.
2517
2518 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2519 ;; Defuns which analyze the buffer, yet don't change `c-state-cache'.
2520 (defun c-state-balance-parens-backwards (here- here+ top)
2521 ;; Return the position of the opening paren/brace/bracket before HERE- which
2522 ;; matches the outermost close p/b/b between HERE+ and TOP. Except when
2523 ;; there's a macro, HERE- and HERE+ are the same. Like this:
2524 ;;
2525 ;; ............................................
2526 ;; | |
2527 ;; ( [ ( .........#macro.. ) ( ) ] )
2528 ;; ^ ^ ^ ^
2529 ;; | | | |
2530 ;; return HERE- HERE+ TOP
2531 ;;
2532 ;; If there aren't enough opening paren/brace/brackets, return the position
2533 ;; of the outermost one found, or HERE- if there are none. If there are no
2534 ;; closing p/b/bs between HERE+ and TOP, return HERE-. HERE-/+ and TOP
2535 ;; must not be inside literals. Only the accessible portion of the buffer
2536 ;; will be scanned.
2537
2538 ;; PART 1: scan from `here+' up to `top', accumulating ")"s which enclose
2539 ;; `here'. Go round the next loop each time we pass over such a ")". These
2540 ;; probably match "("s before `here-'.
2541 (let (pos pa ren+1 lonely-rens)
2542 (save-excursion
2543 (save-restriction
2544 (narrow-to-region (point-min) top) ; This can move point, sometimes.
2545 (setq pos here+)
2546 (c-safe
2547 (while
2548 (setq ren+1 (scan-lists pos 1 1)) ; might signal
2549 (setq lonely-rens (cons ren+1 lonely-rens)
2550 pos ren+1)))))
2551
2552 ;; PART 2: Scan back before `here-' searching for the "("s
2553 ;; matching/mismatching the ")"s found above. We only need to direct the
2554 ;; caller to scan when we've encountered unmatched right parens.
2555 (setq pos here-)
2556 (when lonely-rens
2557 (c-safe
2558 (while
2559 (and lonely-rens ; actual values aren't used.
2560 (setq pa (scan-lists pos -1 1)))
2561 (setq pos pa)
2562 (setq lonely-rens (cdr lonely-rens)))))
2563 pos))
2564
2565 (defun c-parse-state-get-strategy (here good-pos)
2566 ;; Determine the scanning strategy for adjusting `c-parse-state', attempting
2567 ;; to minimize the amount of scanning. HERE is the pertinent position in
2568 ;; the buffer, GOOD-POS is a position where `c-state-cache' (possibly with
2569 ;; its head trimmed) is known to be good, or nil if there is no such
2570 ;; position.
2571 ;;
2572 ;; The return value is a list, one of the following:
2573 ;;
2574 ;; o - ('forward START-POINT) - scan forward from START-POINT,
2575 ;; which is not less than the highest position in `c-state-cache' below HERE,
2576 ;; which is after GOOD-POS.
2577 ;; o - ('backward nil) - scan backwards (from HERE).
2578 ;; o - ('back-and-forward START-POINT) - like 'forward, but when HERE is earlier
2579 ;; than GOOD-POS.
2580 ;; o - ('IN-LIT nil) - point is inside the literal containing point-min.
2581 (let ((cache-pos (c-get-cache-scan-pos here)) ; highest position below HERE in cache (or 1)
2582 strategy ; 'forward, 'backward, or 'IN-LIT.
2583 start-point)
2584 (setq good-pos (or good-pos (c-state-get-min-scan-pos)))
2585 (cond
2586 ((< here (c-state-get-min-scan-pos))
2587 (setq strategy 'IN-LIT))
2588 ((<= good-pos here)
2589 (setq strategy 'forward
2590 start-point (max good-pos cache-pos)))
2591 ((< (- good-pos here) (- here cache-pos)) ; FIXME!!! ; apply some sort of weighting.
2592 (setq strategy 'backward))
2593 (t
2594 (setq strategy 'back-and-forward
2595 start-point cache-pos)))
2596 (list strategy start-point)))
2597
2598
2599 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2600 ;; Routines which change `c-state-cache' and associated values.
2601 (defun c-renarrow-state-cache ()
2602 ;; The region (more precisely, point-min) has changed since we
2603 ;; calculated `c-state-cache'. Amend `c-state-cache' accordingly.
2604 (if (< (point-min) c-state-point-min)
2605 ;; If point-min has MOVED BACKWARDS then we drop the state completely.
2606 ;; It would be possible to do a better job here and recalculate the top
2607 ;; only.
2608 (progn
2609 (c-state-mark-point-min-literal)
2610 (setq c-state-cache nil
2611 c-state-cache-good-pos c-state-min-scan-pos
2612 c-state-brace-pair-desert nil))
2613
2614 ;; point-min has MOVED FORWARD.
2615
2616 ;; Is the new point-min inside a (different) literal?
2617 (unless (and c-state-point-min-lit-start ; at prev. point-min
2618 (< (point-min) (c-state-get-min-scan-pos)))
2619 (c-state-mark-point-min-literal))
2620
2621 ;; Cut off a bit of the tail from `c-state-cache'.
2622 (let ((ptr (cons nil c-state-cache))
2623 pa)
2624 (while (and (setq pa (c-state-cache-top-lparen (cdr ptr)))
2625 (>= pa (point-min)))
2626 (setq ptr (cdr ptr)))
2627
2628 (when (consp ptr)
2629 (if (eq (cdr ptr) c-state-cache)
2630 (setq c-state-cache nil
2631 c-state-cache-good-pos c-state-min-scan-pos)
2632 (setcdr ptr nil)
2633 (setq c-state-cache-good-pos (1+ (c-state-cache-top-lparen))))
2634 )))
2635
2636 (setq c-state-point-min (point-min)))
2637
2638 (defun c-append-lower-brace-pair-to-state-cache (from here &optional upper-lim)
2639 ;; If there is a brace pair preceding FROM in the buffer, at the same level
2640 ;; of nesting (not necessarily immediately preceding), push a cons onto
2641 ;; `c-state-cache' to represent it. FROM must not be inside a literal. If
2642 ;; UPPER-LIM is non-nil, we append the highest brace pair whose "}" is below
2643 ;; UPPER-LIM.
2644 ;;
2645 ;; Return non-nil when this has been done.
2646 ;;
2647 ;; The situation it copes with is this transformation:
2648 ;;
2649 ;; OLD: { (.) {...........}
2650 ;; ^ ^
2651 ;; FROM HERE
2652 ;;
2653 ;; NEW: { {....} (.) {.........
2654 ;; ^ ^ ^
2655 ;; LOWER BRACE PAIR HERE or HERE
2656 ;;
2657 ;; This routine should be fast. Since it can get called a LOT, we maintain
2658 ;; `c-state-brace-pair-desert', a small cache of "failures", such that we
2659 ;; reduce the time wasted in repeated fruitless searches in brace deserts.
2660 (save-excursion
2661 (save-restriction
2662 (let* (new-cons
2663 (cache-pos (c-state-cache-top-lparen)) ; might be nil.
2664 (macro-start-or-from
2665 (progn (goto-char from)
2666 (c-beginning-of-macro)
2667 (point)))
2668 (bra ; Position of "{".
2669 ;; Don't start scanning in the middle of a CPP construct unless
2670 ;; it contains HERE - these constructs, in Emacs, are "commented
2671 ;; out" with category properties.
2672 (if (eq (c-get-char-property macro-start-or-from 'category)
2673 'c-cpp-delimiter)
2674 macro-start-or-from
2675 from))
2676 ce) ; Position of "}"
2677 (or upper-lim (setq upper-lim from))
2678
2679 ;; If we're essentially repeating a fruitless search, just give up.
2680 (unless (and c-state-brace-pair-desert
2681 (eq cache-pos (car c-state-brace-pair-desert))
2682 (or (null (car c-state-brace-pair-desert))
2683 (> from (car c-state-brace-pair-desert)))
2684 (<= from (cdr c-state-brace-pair-desert)))
2685 ;; DESERT-LIM. Avoid repeated searching through the cached desert.
2686 (let ((desert-lim
2687 (and c-state-brace-pair-desert
2688 (eq cache-pos (car c-state-brace-pair-desert))
2689 (>= from (cdr c-state-brace-pair-desert))
2690 (cdr c-state-brace-pair-desert)))
2691 ;; CACHE-LIM. This limit will be necessary when an opening
2692 ;; paren at `cache-pos' has just had its matching close paren
2693 ;; inserted into the buffer. `cache-pos' continues to be a
2694 ;; search bound, even though the algorithm below would skip
2695 ;; over the new paren pair.
2696 (cache-lim (and cache-pos (< cache-pos from) cache-pos)))
2697 (narrow-to-region
2698 (cond
2699 ((and desert-lim cache-lim)
2700 (max desert-lim cache-lim))
2701 (desert-lim)
2702 (cache-lim)
2703 ((point-min)))
2704 ;; The top limit is EOB to ensure that `bra' is inside the
2705 ;; accessible part of the buffer at the next scan operation.
2706 (1+ (buffer-size))))
2707
2708 ;; In the next pair of nested loops, the inner one moves back past a
2709 ;; pair of (mis-)matching parens or brackets; the outer one moves
2710 ;; back over a sequence of unmatched close brace/paren/bracket each
2711 ;; time round.
2712 (while
2713 (progn
2714 (c-safe
2715 (while
2716 (and (setq ce (scan-lists bra -1 -1)) ; back past )/]/}; might signal
2717 (setq bra (scan-lists ce -1 1)) ; back past (/[/{; might signal
2718 (or (> bra here) ;(> ce here)
2719 (and
2720 (< ce here)
2721 (or (not (eq (char-after bra) ?\{))
2722 (and (goto-char bra)
2723 (c-beginning-of-macro)
2724 (< (point) macro-start-or-from))))))))
2725 (and ce (< ce bra)))
2726 (setq bra ce)) ; If we just backed over an unbalanced closing
2727 ; brace, ignore it.
2728
2729 (if (and ce (< ce here) (< bra ce) (eq (char-after bra) ?\{))
2730 ;; We've found the desired brace-pair.
2731 (progn
2732 (setq new-cons (cons bra (1+ ce)))
2733 (cond
2734 ((consp (car c-state-cache))
2735 (setcar c-state-cache new-cons))
2736 ((and (numberp (car c-state-cache)) ; probably never happens
2737 (< ce (car c-state-cache)))
2738 (setcdr c-state-cache
2739 (cons new-cons (cdr c-state-cache))))
2740 (t (setq c-state-cache (cons new-cons c-state-cache)))))
2741
2742 ;; We haven't found a brace pair. Record this in the cache.
2743 (setq c-state-brace-pair-desert
2744 (cons (if (and ce (< bra ce) (> ce here)) ; {..} straddling HERE?
2745 bra
2746 (point-min))
2747 (min here from)))))))))
2748
2749 (defsubst c-state-push-any-brace-pair (bra+1 macro-start-or-here)
2750 ;; If BRA+1 is nil, do nothing. Otherwise, BRA+1 is the buffer position
2751 ;; following a {, and that brace has a (mis-)matching } (or ]), and we
2752 ;; "push" "a" brace pair onto `c-state-cache'.
2753 ;;
2754 ;; Here "push" means overwrite the top element if it's itself a brace-pair,
2755 ;; otherwise push it normally.
2756 ;;
2757 ;; The brace pair we push is normally the one surrounding BRA+1, but if the
2758 ;; latter is inside a macro, not being a macro containing
2759 ;; MACRO-START-OR-HERE, we scan backwards through the buffer for a non-macro
2760 ;; base pair. This latter case is assumed to be rare.
2761 ;;
2762 ;; Note: POINT is not preserved in this routine.
2763 (if bra+1
2764 (if (or (> bra+1 macro-start-or-here)
2765 (progn (goto-char bra+1)
2766 (not (c-beginning-of-macro))))
2767 (setq c-state-cache
2768 (cons (cons (1- bra+1)
2769 (scan-lists bra+1 1 1))
2770 (if (consp (car c-state-cache))
2771 (cdr c-state-cache)
2772 c-state-cache)))
2773 ;; N.B. This defsubst codes one method for the simple, normal case,
2774 ;; and a more sophisticated, slower way for the general case. Don't
2775 ;; eliminate this defsubst - it's a speed optimization.
2776 (c-append-lower-brace-pair-to-state-cache (1- bra+1) (point-max)))))
2777
2778 (defun c-append-to-state-cache (from here)
2779 ;; Scan the buffer from FROM to HERE, adding elements into `c-state-cache'
2780 ;; for braces etc. Return a candidate for `c-state-cache-good-pos'.
2781 ;;
2782 ;; FROM must be after the latest brace/paren/bracket in `c-state-cache', if
2783 ;; any. Typically, it is immediately after it. It must not be inside a
2784 ;; literal.
2785 (let ((here-bol (c-point 'bol here))
2786 (macro-start-or-here
2787 (save-excursion (goto-char here)
2788 (if (c-beginning-of-macro)
2789 (point)
2790 here)))
2791 pa+1 ; pos just after an opening PAren (or brace).
2792 (ren+1 from) ; usually a pos just after an closing paREN etc.
2793 ; Is actually the pos. to scan for a (/{/[ from,
2794 ; which sometimes is after a silly )/}/].
2795 paren+1 ; Pos after some opening or closing paren.
2796 paren+1s ; A list of `paren+1's; used to determine a
2797 ; good-pos.
2798 bra+1 ce+1 ; just after L/R bra-ces.
2799 bra+1s ; list of OLD values of bra+1.
2800 mstart) ; start of a macro.
2801
2802 (save-excursion
2803 (save-restriction
2804 (narrow-to-region (point-min) here)
2805 ;; Each time round the following loop, we enter a successively deeper
2806 ;; level of brace/paren nesting. (Except sometimes we "continue at
2807 ;; the existing level".) `pa+1' is a pos inside an opening
2808 ;; brace/paren/bracket, usually just after it.
2809 (while
2810 (progn
2811 ;; Each time round the next loop moves forward over an opening then
2812 ;; a closing brace/bracket/paren. This loop is white hot, so it
2813 ;; plays ugly tricks to go fast. DON'T PUT ANYTHING INTO THIS
2814 ;; LOOP WHICH ISN'T ABSOLUTELY NECESSARY!!! It terminates when a
2815 ;; call of `scan-lists' signals an error, which happens when there
2816 ;; are no more b/b/p's to scan.
2817 (c-safe
2818 (while t
2819 (setq pa+1 (scan-lists ren+1 1 -1) ; Into (/{/[; might signal
2820 paren+1s (cons pa+1 paren+1s))
2821 (setq ren+1 (scan-lists pa+1 1 1)) ; Out of )/}/]; might signal
2822 (if (and (eq (char-before pa+1) ?{)) ; Check for a macro later.
2823 (setq bra+1 pa+1))
2824 (setcar paren+1s ren+1)))
2825
2826 (if (and pa+1 (> pa+1 ren+1))
2827 ;; We've just entered a deeper nesting level.
2828 (progn
2829 ;; Insert the brace pair (if present) and the single open
2830 ;; paren/brace/bracket into `c-state-cache' It cannot be
2831 ;; inside a macro, except one around point, because of what
2832 ;; `c-neutralize-syntax-in-CPP' has done.
2833 (c-state-push-any-brace-pair bra+1 macro-start-or-here)
2834 ;; Insert the opening brace/bracket/paren position.
2835 (setq c-state-cache (cons (1- pa+1) c-state-cache))
2836 ;; Clear admin stuff for the next more nested part of the scan.
2837 (setq ren+1 pa+1 pa+1 nil bra+1 nil bra+1s nil)
2838 t) ; Carry on the loop
2839
2840 ;; All open p/b/b's at this nesting level, if any, have probably
2841 ;; been closed by matching/mismatching ones. We're probably
2842 ;; finished - we just need to check for having found an
2843 ;; unmatched )/}/], which we ignore. Such a )/}/] can't be in a
2844 ;; macro, due the action of `c-neutralize-syntax-in-CPP'.
2845 (c-safe (setq ren+1 (scan-lists ren+1 1 1)))))) ; acts as loop control.
2846
2847 ;; Record the final, innermost, brace-pair if there is one.
2848 (c-state-push-any-brace-pair bra+1 macro-start-or-here)
2849
2850 ;; Determine a good pos
2851 (while (and (setq paren+1 (car paren+1s))
2852 (> (if (> paren+1 macro-start-or-here)
2853 paren+1
2854 (goto-char paren+1)
2855 (setq mstart (and (c-beginning-of-macro)
2856 (point)))
2857 (or mstart paren+1))
2858 here-bol))
2859 (setq paren+1s (cdr paren+1s)))
2860 (cond
2861 ((and paren+1 mstart)
2862 (min paren+1 mstart))
2863 (paren+1)
2864 (t from))))))
2865
2866 (defun c-remove-stale-state-cache (start-point here pps-point)
2867 ;; Remove stale entries from the `c-cache-state', i.e. those which will
2868 ;; not be in it when it is amended for position HERE. This may involve
2869 ;; replacing a CONS element for a brace pair containing HERE with its car.
2870 ;; Additionally, the "outermost" open-brace entry before HERE will be
2871 ;; converted to a cons if the matching close-brace is below HERE.
2872 ;;
2873 ;; START-POINT is a "maximal" "safe position" - there must be no open
2874 ;; parens/braces/brackets between START-POINT and HERE.
2875 ;;
2876 ;; As a second thing, calculate the result of parse-partial-sexp at
2877 ;; PPS-POINT, w.r.t. START-POINT. The motivation here is that
2878 ;; `c-state-cache-good-pos' may become PPS-POINT, but the caller may need to
2879 ;; adjust it to get outside a string/comment. (Sorry about this! The code
2880 ;; needs to be FAST).
2881 ;;
2882 ;; Return a list (GOOD-POS SCAN-BACK-POS CONS-SEPARATED PPS-STATE), where
2883 ;; o - GOOD-POS is a position where the new value `c-state-cache' is known
2884 ;; to be good (we aim for this to be as high as possible);
2885 ;; o - SCAN-BACK-POS, if not nil, indicates there may be a brace pair
2886 ;; preceding POS which needs to be recorded in `c-state-cache'. It is a
2887 ;; position to scan backwards from. It is the position of the "{" of the
2888 ;; last element to be removed from `c-state-cache', when that elt is a
2889 ;; cons, otherwise nil.
2890 ;; o - CONS-SEPARATED is t when a cons element in `c-state-cache' has been
2891 ;; replaced by its car because HERE lies inside the brace pair represented
2892 ;; by the cons.
2893 ;; o - PPS-STATE is the parse-partial-sexp state at PPS-POINT.
2894 (save-excursion
2895 (save-restriction
2896 (narrow-to-region 1 (point-max))
2897 (let* ((in-macro-start ; start of macro containing HERE or nil.
2898 (save-excursion
2899 (goto-char here)
2900 (and (c-beginning-of-macro)
2901 (point))))
2902 (start-point-actual-macro-start ; Start of macro containing
2903 ; start-point or nil
2904 (and (< start-point here)
2905 (save-excursion
2906 (goto-char start-point)
2907 (and (c-beginning-of-macro)
2908 (point)))))
2909 (start-point-actual-macro-end ; End of this macro, (maybe
2910 ; HERE), or nil.
2911 (and start-point-actual-macro-start
2912 (save-excursion
2913 (goto-char start-point-actual-macro-start)
2914 (c-end-of-macro)
2915 (point))))
2916 pps-state ; Will be 9 or 10 elements long.
2917 pos
2918 upper-lim ; ,beyond which `c-state-cache' entries are removed
2919 scan-back-pos
2920 cons-separated
2921 pair-beg pps-point-state target-depth)
2922
2923 ;; Remove entries beyond HERE. Also remove any entries inside
2924 ;; a macro, unless HERE is in the same macro.
2925 (setq upper-lim
2926 (if (or (null c-state-old-cpp-beg)
2927 (and (> here c-state-old-cpp-beg)
2928 (< here c-state-old-cpp-end)))
2929 here
2930 (min here c-state-old-cpp-beg)))
2931 (while (and c-state-cache (>= (c-state-cache-top-lparen) upper-lim))
2932 (setq scan-back-pos (car-safe (car c-state-cache)))
2933 (setq c-state-cache (cdr c-state-cache)))
2934
2935 ;; If `upper-lim' is inside the last recorded brace pair, remove its
2936 ;; RBrace and indicate we'll need to search backwards for a previous
2937 ;; brace pair.
2938 (when (and c-state-cache
2939 (consp (car c-state-cache))
2940 (> (cdar c-state-cache) upper-lim))
2941 (setcar c-state-cache (caar c-state-cache))
2942 (setq scan-back-pos (car c-state-cache)
2943 cons-separated t))
2944
2945 ;; The next loop jumps forward out of a nested level of parens each
2946 ;; time round; the corresponding elements in `c-state-cache' are
2947 ;; removed. `pos' is just after the brace-pair or the open paren at
2948 ;; (car c-state-cache). There can be no open parens/braces/brackets
2949 ;; between `start-point'/`start-point-actual-macro-start' and HERE,
2950 ;; due to the interface spec to this function.
2951 (setq pos (if (and start-point-actual-macro-end
2952 (not (eq start-point-actual-macro-start
2953 in-macro-start)))
2954 (1+ start-point-actual-macro-end) ; get outside the macro as
2955 ; marked by a `category' text property.
2956 start-point))
2957 (goto-char pos)
2958 (while (and c-state-cache
2959 (or (numberp (car c-state-cache)) ; Have we a { at all?
2960 (cdr c-state-cache))
2961 (< (point) here))
2962 (cond
2963 ((null pps-state) ; first time through
2964 (setq target-depth -1))
2965 ((eq (car pps-state) target-depth) ; found closing ),},]
2966 (setq target-depth (1- (car pps-state))))
2967 ;; Do nothing when we've merely reached pps-point.
2968 )
2969
2970 ;; Scan!
2971 (setq pps-state
2972 (parse-partial-sexp
2973 (point) (if (< (point) pps-point) pps-point here)
2974 target-depth
2975 nil pps-state))
2976
2977 (if (= (point) pps-point)
2978 (setq pps-point-state pps-state))
2979
2980 (when (eq (car pps-state) target-depth)
2981 (setq pos (point)) ; POS is now just after an R-paren/brace.
2982 (cond
2983 ((and (consp (car c-state-cache))
2984 (eq (point) (cdar c-state-cache)))
2985 ;; We've just moved out of the paren pair containing the brace-pair
2986 ;; at (car c-state-cache). `pair-beg' is where the open paren is,
2987 ;; and is potentially where the open brace of a cons in
2988 ;; c-state-cache will be.
2989 (setq pair-beg (car-safe (cdr c-state-cache))
2990 c-state-cache (cdr-safe (cdr c-state-cache)))) ; remove {}pair + containing Lparen.
2991 ((numberp (car c-state-cache))
2992 (setq pair-beg (car c-state-cache)
2993 c-state-cache (cdr c-state-cache))) ; remove this
2994 ; containing Lparen
2995 ((numberp (cadr c-state-cache))
2996 (setq pair-beg (cadr c-state-cache)
2997 c-state-cache (cddr c-state-cache))) ; Remove a paren pair
2998 ; together with enclosed brace pair.
2999 ;; (t nil) ; Ignore an unmated Rparen.
3000 )))
3001
3002 (if (< (point) pps-point)
3003 (setq pps-state (parse-partial-sexp (point) pps-point
3004 nil nil ; TARGETDEPTH, STOPBEFORE
3005 pps-state)))
3006
3007 ;; If the last paren pair we moved out of was actually a brace pair,
3008 ;; insert it into `c-state-cache'.
3009 (when (and pair-beg (eq (char-after pair-beg) ?{))
3010 (if (consp (car-safe c-state-cache))
3011 (setq c-state-cache (cdr c-state-cache)))
3012 (setq c-state-cache (cons (cons pair-beg pos)
3013 c-state-cache)))
3014
3015 (list pos scan-back-pos cons-separated pps-state)))))
3016
3017 (defun c-remove-stale-state-cache-backwards (here)
3018 ;; Strip stale elements of `c-state-cache' by moving backwards through the
3019 ;; buffer, and inform the caller of the scenario detected.
3020 ;;
3021 ;; HERE is the position we're setting `c-state-cache' for.
3022 ;; CACHE-POS (a locally bound variable) is just after the latest recorded
3023 ;; position in `c-state-cache' before HERE, or a position at or near
3024 ;; point-min which isn't in a literal.
3025 ;;
3026 ;; This function must only be called only when (> `c-state-cache-good-pos'
3027 ;; HERE). Usually the gap between CACHE-POS and HERE is large. It is thus
3028 ;; optimized to eliminate (or minimize) scanning between these two
3029 ;; positions.
3030 ;;
3031 ;; Return a three element list (GOOD-POS SCAN-BACK-POS FWD-FLAG), where:
3032 ;; o - GOOD-POS is a "good position", where `c-state-cache' is valid, or
3033 ;; could become so after missing elements are inserted into
3034 ;; `c-state-cache'. This is JUST AFTER an opening or closing
3035 ;; brace/paren/bracket which is already in `c-state-cache' or just before
3036 ;; one otherwise. exceptionally (when there's no such b/p/b handy) the BOL
3037 ;; before `here''s line, or the start of the literal containing it.
3038 ;; o - SCAN-BACK-POS, if non-nil, indicates there may be a brace pair
3039 ;; preceding POS which isn't recorded in `c-state-cache'. It is a position
3040 ;; to scan backwards from.
3041 ;; o - FWD-FLAG, if non-nil, indicates there may be parens/braces between
3042 ;; POS and HERE which aren't recorded in `c-state-cache'.
3043 ;;
3044 ;; The comments in this defun use "paren" to mean parenthesis or square
3045 ;; bracket (as contrasted with a brace), and "(" and ")" likewise.
3046 ;;
3047 ;; . {..} (..) (..) ( .. { } ) (...) ( .... . ..)
3048 ;; | | | | | |
3049 ;; CP E here D C good
3050 (let ((cache-pos (c-get-cache-scan-pos here)) ; highest position below HERE in cache (or 1)
3051 (pos c-state-cache-good-pos)
3052 pa ren ; positions of "(" and ")"
3053 dropped-cons ; whether the last element dropped from `c-state-cache'
3054 ; was a cons (representing a brace-pair)
3055 good-pos ; see above.
3056 lit ; (START . END) of a literal containing some point.
3057 here-lit-start here-lit-end ; bounds of literal containing `here'
3058 ; or `here' itself.
3059 here- here+ ; start/end of macro around HERE, or HERE
3060 (here-bol (c-point 'bol here))
3061 (too-far-back (max (- here c-state-cache-too-far) (point-min))))
3062
3063 ;; Remove completely irrelevant entries from `c-state-cache'.
3064 (while (and c-state-cache
3065 (>= (setq pa (c-state-cache-top-lparen)) here))
3066 (setq dropped-cons (consp (car c-state-cache)))
3067 (setq c-state-cache (cdr c-state-cache))
3068 (setq pos pa))
3069 ;; At this stage, (> pos here);
3070 ;; (< (c-state-cache-top-lparen) here) (or is nil).
3071
3072 (cond
3073 ((and (consp (car c-state-cache))
3074 (> (cdar c-state-cache) here))
3075 ;; CASE 1: The top of the cache is a brace pair which now encloses
3076 ;; `here'. As good-pos, return the address. of the "{". Since we've no
3077 ;; knowledge of what's inside these braces, we have no alternative but
3078 ;; to direct the caller to scan the buffer from the opening brace.
3079 (setq pos (caar c-state-cache))
3080 (setcar c-state-cache pos)
3081 (list (1+ pos) pos t)) ; return value. We've just converted a brace pair
3082 ; entry into a { entry, so the caller needs to
3083 ; search for a brace pair before the {.
3084
3085 ;; `here' might be inside a literal. Check for this.
3086 ((progn
3087 (setq lit (c-state-literal-at here)
3088 here-lit-start (or (car lit) here)
3089 here-lit-end (or (cdr lit) here))
3090 ;; Has `here' just "newly entered" a macro?
3091 (save-excursion
3092 (goto-char here-lit-start)
3093 (if (and (c-beginning-of-macro)
3094 (or (null c-state-old-cpp-beg)
3095 (not (= (point) c-state-old-cpp-beg))))
3096 (progn
3097 (setq here- (point))
3098 (c-end-of-macro)
3099 (setq here+ (point)))
3100 (setq here- here-lit-start
3101 here+ here-lit-end)))
3102
3103 ;; `here' might be nested inside any depth of parens (or brackets but
3104 ;; not braces). Scan backwards to find the outermost such opening
3105 ;; paren, if there is one. This will be the scan position to return.
3106 (save-restriction
3107 (narrow-to-region cache-pos (point-max))
3108 (setq pos (c-state-balance-parens-backwards here- here+ pos)))
3109 nil)) ; for the cond
3110
3111 ((< pos here-lit-start)
3112 ;; CASE 2: Address of outermost ( or [ which now encloses `here', but
3113 ;; didn't enclose the (previous) `c-state-cache-good-pos'. If there is
3114 ;; a brace pair preceding this, it will already be in `c-state-cache',
3115 ;; unless there was a brace pair after it, i.e. there'll only be one to
3116 ;; scan for if we've just deleted one.
3117 (list pos (and dropped-cons pos) t)) ; Return value.
3118
3119 ;; `here' isn't enclosed in a (previously unrecorded) bracket/paren.
3120 ;; Further forward scanning isn't needed, but we still need to find a
3121 ;; GOOD-POS. Step out of all enclosing "("s on HERE's line.
3122 ((progn
3123 (save-restriction
3124 (narrow-to-region here-bol (point-max))
3125 (setq pos here-lit-start)
3126 (c-safe (while (setq pa (scan-lists pos -1 1))
3127 (setq pos pa)))) ; might signal
3128 nil)) ; for the cond
3129
3130 ((setq ren (c-safe-scan-lists pos -1 -1 too-far-back))
3131 ;; CASE 3: After a }/)/] before `here''s BOL.
3132 (list (1+ ren) (and dropped-cons pos) nil)) ; Return value
3133
3134 (t
3135 ;; CASE 4; Best of a bad job: BOL before `here-bol', or beginning of
3136 ;; literal containing it.
3137 (setq good-pos (c-state-lit-beg (c-point 'bopl here-bol)))
3138 (list good-pos (and dropped-cons good-pos) nil)))))
3139
3140
3141 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
3142 ;; Externally visible routines.
3143
3144 (defun c-state-cache-init ()
3145 (setq c-state-cache nil
3146 c-state-cache-good-pos 1
3147 c-state-nonlit-pos-cache nil
3148 c-state-nonlit-pos-cache-limit 1
3149 c-state-semi-nonlit-pos-cache nil
3150 c-state-semi-nonlit-pos-cache-limit 1
3151 c-state-brace-pair-desert nil
3152 c-state-point-min 1
3153 c-state-point-min-lit-type nil
3154 c-state-point-min-lit-start nil
3155 c-state-min-scan-pos 1
3156 c-state-old-cpp-beg nil
3157 c-state-old-cpp-end nil)
3158 (c-state-mark-point-min-literal))
3159
3160 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
3161 ;; Debugging routines to dump `c-state-cache' in a "replayable" form.
3162 ;; (defmacro c-sc-de (elt) ; "c-state-cache-dump-element"
3163 ;; `(format ,(concat "(setq " (symbol-name elt) " %s) ") ,elt))
3164 ;; (defmacro c-sc-qde (elt) ; "c-state-cache-quote-dump-element"
3165 ;; `(format ,(concat "(setq " (symbol-name elt) " '%s) ") ,elt))
3166 ;; (defun c-state-dump ()
3167 ;; ;; For debugging.
3168 ;; ;(message
3169 ;; (concat
3170 ;; (c-sc-qde c-state-cache)
3171 ;; (c-sc-de c-state-cache-good-pos)
3172 ;; (c-sc-qde c-state-nonlit-pos-cache)
3173 ;; (c-sc-de c-state-nonlit-pos-cache-limit)
3174 ;; (c-sc-qde c-state-brace-pair-desert)
3175 ;; (c-sc-de c-state-point-min)
3176 ;; (c-sc-de c-state-point-min-lit-type)
3177 ;; (c-sc-de c-state-point-min-lit-start)
3178 ;; (c-sc-de c-state-min-scan-pos)
3179 ;; (c-sc-de c-state-old-cpp-beg)
3180 ;; (c-sc-de c-state-old-cpp-end)))
3181 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
3182
3183 (defun c-invalidate-state-cache-1 (here)
3184 ;; Invalidate all info on `c-state-cache' that applies to the buffer at HERE
3185 ;; or higher and set `c-state-cache-good-pos' accordingly. The cache is
3186 ;; left in a consistent state.
3187 ;;
3188 ;; This is much like `c-whack-state-after', but it never changes a paren
3189 ;; pair element into an open paren element. Doing that would mean that the
3190 ;; new open paren wouldn't have the required preceding paren pair element.
3191 ;;
3192 ;; This function is called from c-after-change.
3193
3194 ;; The caches of non-literals:
3195 ;; Note that we use "<=" for the possibility of the second char of a two-char
3196 ;; comment opener being typed; this would invalidate any cache position at
3197 ;; HERE.
3198 (if (<= here c-state-nonlit-pos-cache-limit)
3199 (setq c-state-nonlit-pos-cache-limit (1- here)))
3200 (if (<= here c-state-semi-nonlit-pos-cache-limit)
3201 (setq c-state-semi-nonlit-pos-cache-limit (1- here)))
3202
3203 ;; `c-state-cache':
3204 ;; Case 1: if `here' is in a literal containing point-min, everything
3205 ;; becomes (or is already) nil.
3206 (if (or (null c-state-cache-good-pos)
3207 (< here (c-state-get-min-scan-pos)))
3208 (setq c-state-cache nil
3209 c-state-cache-good-pos nil
3210 c-state-min-scan-pos nil)
3211
3212 ;; Truncate `c-state-cache' and set `c-state-cache-good-pos' to a value
3213 ;; below `here'. To maintain its consistency, we may need to insert a new
3214 ;; brace pair.
3215 (let (open-paren-in-column-0-is-defun-start
3216 (here-bol (c-point 'bol here))
3217 too-high-pa ; recorded {/(/[ next above here, or nil.
3218 dropped-cons ; was the last removed element a brace pair?
3219 pa)
3220 ;; The easy bit - knock over-the-top bits off `c-state-cache'.
3221 (while (and c-state-cache
3222 (>= (setq pa (c-state-cache-top-paren)) here))
3223 (setq dropped-cons (consp (car c-state-cache))
3224 too-high-pa (c-state-cache-top-lparen)
3225 c-state-cache (cdr c-state-cache)))
3226
3227 ;; Do we need to add in an earlier brace pair, having lopped one off?
3228 (if (and dropped-cons
3229 (< too-high-pa (+ here c-state-cache-too-far)))
3230 (c-append-lower-brace-pair-to-state-cache too-high-pa here here-bol))
3231 (setq c-state-cache-good-pos (or (c-state-cache-after-top-paren)
3232 (c-state-get-min-scan-pos)))))
3233
3234 ;; The brace-pair desert marker:
3235 (when (car c-state-brace-pair-desert)
3236 (if (< here (car c-state-brace-pair-desert))
3237 (setq c-state-brace-pair-desert nil)
3238 (if (< here (cdr c-state-brace-pair-desert))
3239 (setcdr c-state-brace-pair-desert here)))))
3240
3241 (defun c-parse-state-1 ()
3242 ;; Find and record all noteworthy parens between some good point earlier in
3243 ;; the file and point. That good point is at least the beginning of the
3244 ;; top-level construct we are in, or the beginning of the preceding
3245 ;; top-level construct if we aren't in one.
3246 ;;
3247 ;; The returned value is a list of the noteworthy parens with the last one
3248 ;; first. If an element in the list is an integer, it's the position of an
3249 ;; open paren (of any type) which has not been closed before the point. If
3250 ;; an element is a cons, it gives the position of a closed BRACE paren
3251 ;; pair[*]; the car is the start brace position and the cdr is the position
3252 ;; following the closing brace. Only the last closed brace paren pair
3253 ;; before each open paren and before the point is recorded, and thus the
3254 ;; state never contains two cons elements in succession. When a close brace
3255 ;; has no matching open brace (e.g., the matching brace is outside the
3256 ;; visible region), it is not represented in the returned value.
3257 ;;
3258 ;; [*] N.B. The close "brace" might be a mismatching close bracket or paren.
3259 ;; This defun explicitly treats mismatching parens/braces/brackets as
3260 ;; matching. It is the open brace which makes it a "brace" pair.
3261 ;;
3262 ;; If POINT is within a macro, open parens and brace pairs within
3263 ;; THIS macro MIGHT be recorded. This depends on whether their
3264 ;; syntactic properties have been suppressed by
3265 ;; `c-neutralize-syntax-in-CPP'. This might need fixing (2008-12-11).
3266 ;;
3267 ;; Currently no characters which are given paren syntax with the
3268 ;; syntax-table property are recorded, i.e. angle bracket arglist
3269 ;; parens are never present here. Note that this might change.
3270 ;;
3271 ;; BUG: This function doesn't cope entirely well with unbalanced
3272 ;; parens in macros. (2008-12-11: this has probably been resolved
3273 ;; by the function `c-neutralize-syntax-in-CPP'.) E.g. in the
3274 ;; following case the brace before the macro isn't balanced with the
3275 ;; one after it:
3276 ;;
3277 ;; {
3278 ;; #define X {
3279 ;; }
3280 ;;
3281 ;; Note to maintainers: this function DOES get called with point
3282 ;; within comments and strings, so don't assume it doesn't!
3283 ;;
3284 ;; This function might do hidden buffer changes.
3285 (let* ((here (point))
3286 (here-bopl (c-point 'bopl))
3287 open-paren-in-column-0-is-defun-start
3288 strategy ; 'forward, 'backward etc..
3289 ;; Candidate positions to start scanning from:
3290 cache-pos ; highest position below HERE already existing in
3291 ; cache (or 1).
3292 good-pos
3293 start-point ; (when scanning forward) a place below HERE where there
3294 ; are no open parens/braces between it and HERE.
3295 bopl-state
3296 res
3297 cons-separated
3298 scan-backward-pos scan-forward-p) ; used for 'backward.
3299 ;; If POINT-MIN has changed, adjust the cache
3300 (unless (= (point-min) c-state-point-min)
3301 (c-renarrow-state-cache))
3302
3303 ;; Strategy?
3304 (setq res (c-parse-state-get-strategy here c-state-cache-good-pos)
3305 strategy (car res)
3306 start-point (cadr res))
3307
3308 ;; SCAN!
3309 (cond
3310 ((memq strategy '(forward back-and-forward))
3311 (setq res (c-remove-stale-state-cache start-point here here-bopl))
3312 (setq cache-pos (car res)
3313 scan-backward-pos (cadr res)
3314 cons-separated (car (cddr res))
3315 bopl-state (cadr (cddr res))) ; will be nil if (< here-bopl
3316 ; start-point)
3317 (if (and scan-backward-pos
3318 (or cons-separated (eq strategy 'forward))) ;scan-backward-pos
3319 (c-append-lower-brace-pair-to-state-cache scan-backward-pos here))
3320 (setq good-pos
3321 (c-append-to-state-cache cache-pos here))
3322 (setq c-state-cache-good-pos
3323 (if (and bopl-state
3324 (< good-pos (- here c-state-cache-too-far)))
3325 (c-state-cache-non-literal-place here-bopl bopl-state)
3326 good-pos)))
3327
3328 ((eq strategy 'backward)
3329 (setq res (c-remove-stale-state-cache-backwards here)
3330 good-pos (car res)
3331 scan-backward-pos (cadr res)
3332 scan-forward-p (car (cddr res)))
3333 (if scan-backward-pos
3334 (c-append-lower-brace-pair-to-state-cache scan-backward-pos here))
3335 (setq c-state-cache-good-pos
3336 (if scan-forward-p
3337 (c-append-to-state-cache good-pos here)
3338 good-pos)))
3339
3340 (t ; (eq strategy 'IN-LIT)
3341 (setq c-state-cache nil
3342 c-state-cache-good-pos nil))))
3343
3344 c-state-cache)
3345
3346 (defun c-invalidate-state-cache (here)
3347 ;; This is a wrapper over `c-invalidate-state-cache-1'.
3348 ;;
3349 ;; It suppresses the syntactic effect of the < and > (template) brackets and
3350 ;; of all parens in preprocessor constructs, except for any such construct
3351 ;; containing point. We can then call `c-invalidate-state-cache-1' without
3352 ;; worrying further about macros and template delimiters.
3353 (c-with-<->-as-parens-suppressed
3354 (if (and c-state-old-cpp-beg
3355 (< c-state-old-cpp-beg here))
3356 (c-with-all-but-one-cpps-commented-out
3357 c-state-old-cpp-beg
3358 (min c-state-old-cpp-end here)
3359 (c-invalidate-state-cache-1 here))
3360 (c-with-cpps-commented-out
3361 (c-invalidate-state-cache-1 here)))))
3362
3363 (defmacro c-state-maybe-marker (place marker)
3364 ;; If PLACE is non-nil, return a marker marking it, otherwise nil.
3365 ;; We (re)use MARKER.
3366 `(and ,place
3367 (or ,marker (setq ,marker (make-marker)))
3368 (set-marker ,marker ,place)))
3369
3370 (defun c-parse-state ()
3371 ;; This is a wrapper over `c-parse-state-1'. See that function for a
3372 ;; description of the functionality and return value.
3373 ;;
3374 ;; It suppresses the syntactic effect of the < and > (template) brackets and
3375 ;; of all parens in preprocessor constructs, except for any such construct
3376 ;; containing point. We can then call `c-parse-state-1' without worrying
3377 ;; further about macros and template delimiters.
3378 (let (here-cpp-beg here-cpp-end)
3379 (save-excursion
3380 (when (c-beginning-of-macro)
3381 (setq here-cpp-beg (point))
3382 (unless
3383 (> (setq here-cpp-end (c-syntactic-end-of-macro))
3384 here-cpp-beg)
3385 (setq here-cpp-beg nil here-cpp-end nil))))
3386 ;; FIXME!!! Put in a `condition-case' here to protect the integrity of the
3387 ;; subsystem.
3388 (prog1
3389 (c-with-<->-as-parens-suppressed
3390 (if (and here-cpp-beg (> here-cpp-end here-cpp-beg))
3391 (c-with-all-but-one-cpps-commented-out
3392 here-cpp-beg here-cpp-end
3393 (c-parse-state-1))
3394 (c-with-cpps-commented-out
3395 (c-parse-state-1))))
3396 (setq c-state-old-cpp-beg
3397 (c-state-maybe-marker here-cpp-beg c-state-old-cpp-beg-marker)
3398 c-state-old-cpp-end
3399 (c-state-maybe-marker here-cpp-end c-state-old-cpp-end-marker)))))
3400
3401 ;; Debug tool to catch cache inconsistencies. This is called from
3402 ;; 000tests.el.
3403 (defvar c-debug-parse-state nil)
3404 (unless (fboundp 'c-real-parse-state)
3405 (fset 'c-real-parse-state (symbol-function 'c-parse-state)))
3406 (cc-bytecomp-defun c-real-parse-state)
3407
3408 (defvar c-parse-state-point nil)
3409 (defvar c-parse-state-state nil)
3410 (defun c-record-parse-state-state ()
3411 (setq c-parse-state-point (point))
3412 (setq c-parse-state-state
3413 (mapcar
3414 (lambda (arg)
3415 (let ((val (symbol-value arg)))
3416 (cons arg
3417 (if (consp val)
3418 (copy-tree val)
3419 val))))
3420 '(c-state-cache
3421 c-state-cache-good-pos
3422 c-state-nonlit-pos-cache
3423 c-state-nonlit-pos-cache-limit
3424 c-state-semi-nonlit-pos-cache
3425 c-state-semi-nonlit-pos-cache-limit
3426 c-state-brace-pair-desert
3427 c-state-point-min
3428 c-state-point-min-lit-type
3429 c-state-point-min-lit-start
3430 c-state-min-scan-pos
3431 c-state-old-cpp-beg
3432 c-state-old-cpp-end
3433 c-parse-state-point))))
3434 (defun c-replay-parse-state-state ()
3435 (message
3436 (concat "(setq "
3437 (mapconcat
3438 (lambda (arg)
3439 (format "%s %s%s" (car arg) (if (atom (cdr arg)) "" "'") (cdr arg)))
3440 c-parse-state-state " ")
3441 ")")))
3442
3443 (defun c-debug-parse-state-double-cons (state)
3444 (let (state-car conses-not-ok)
3445 (while state
3446 (setq state-car (car state)
3447 state (cdr state))
3448 (if (and (consp state-car)
3449 (consp (car state)))
3450 (setq conses-not-ok t)))
3451 conses-not-ok))
3452
3453 (defun c-debug-parse-state ()
3454 (let ((here (point)) (res1 (c-real-parse-state)) res2)
3455 (let ((c-state-cache nil)
3456 (c-state-cache-good-pos 1)
3457 (c-state-nonlit-pos-cache nil)
3458 (c-state-nonlit-pos-cache-limit 1)
3459 (c-state-brace-pair-desert nil)
3460 (c-state-point-min 1)
3461 (c-state-point-min-lit-type nil)
3462 (c-state-point-min-lit-start nil)
3463 (c-state-min-scan-pos 1)
3464 (c-state-old-cpp-beg nil)
3465 (c-state-old-cpp-end nil))
3466 (setq res2 (c-real-parse-state)))
3467 (unless (equal res1 res2)
3468 ;; The cache can actually go further back due to the ad-hoc way
3469 ;; the first paren is found, so try to whack off a bit of its
3470 ;; start before complaining.
3471 ;; (save-excursion
3472 ;; (goto-char (or (c-least-enclosing-brace res2) (point)))
3473 ;; (c-beginning-of-defun-1)
3474 ;; (while (not (or (bobp) (eq (char-after) ?{)))
3475 ;; (c-beginning-of-defun-1))
3476 ;; (unless (equal (c-whack-state-before (point) res1) res2)
3477 ;; (message (concat "c-parse-state inconsistency at %s: "
3478 ;; "using cache: %s, from scratch: %s")
3479 ;; here res1 res2)))
3480 (message (concat "c-parse-state inconsistency at %s: "
3481 "using cache: %s, from scratch: %s")
3482 here res1 res2)
3483 (message "Old state:")
3484 (c-replay-parse-state-state))
3485
3486 (when (c-debug-parse-state-double-cons res1)
3487 (message "c-parse-state INVALIDITY at %s: %s"
3488 here res1)
3489 (message "Old state:")
3490 (c-replay-parse-state-state))
3491
3492 (c-record-parse-state-state)
3493 res2 ; res1 correct a cascading series of errors ASAP
3494 ))
3495
3496 (defun c-toggle-parse-state-debug (&optional arg)
3497 (interactive "P")
3498 (setq c-debug-parse-state (c-calculate-state arg c-debug-parse-state))
3499 (fset 'c-parse-state (symbol-function (if c-debug-parse-state
3500 'c-debug-parse-state
3501 'c-real-parse-state)))
3502 (c-keep-region-active)
3503 (message "c-debug-parse-state %sabled"
3504 (if c-debug-parse-state "en" "dis")))
3505 (when c-debug-parse-state
3506 (c-toggle-parse-state-debug 1))
3507
3508 \f
3509 (defun c-whack-state-before (bufpos paren-state)
3510 ;; Whack off any state information from PAREN-STATE which lies
3511 ;; before BUFPOS. Not destructive on PAREN-STATE.
3512 (let* ((newstate (list nil))
3513 (ptr newstate)
3514 car)
3515 (while paren-state
3516 (setq car (car paren-state)
3517 paren-state (cdr paren-state))
3518 (if (< (if (consp car) (car car) car) bufpos)
3519 (setq paren-state nil)
3520 (setcdr ptr (list car))
3521 (setq ptr (cdr ptr))))
3522 (cdr newstate)))
3523
3524 (defun c-whack-state-after (bufpos paren-state)
3525 ;; Whack off any state information from PAREN-STATE which lies at or
3526 ;; after BUFPOS. Not destructive on PAREN-STATE.
3527 (catch 'done
3528 (while paren-state
3529 (let ((car (car paren-state)))
3530 (if (consp car)
3531 ;; just check the car, because in a balanced brace
3532 ;; expression, it must be impossible for the corresponding
3533 ;; close brace to be before point, but the open brace to
3534 ;; be after.
3535 (if (<= bufpos (car car))
3536 nil ; whack it off
3537 (if (< bufpos (cdr car))
3538 ;; its possible that the open brace is before
3539 ;; bufpos, but the close brace is after. In that
3540 ;; case, convert this to a non-cons element. The
3541 ;; rest of the state is before bufpos, so we're
3542 ;; done.
3543 (throw 'done (cons (car car) (cdr paren-state)))
3544 ;; we know that both the open and close braces are
3545 ;; before bufpos, so we also know that everything else
3546 ;; on state is before bufpos.
3547 (throw 'done paren-state)))
3548 (if (<= bufpos car)
3549 nil ; whack it off
3550 ;; it's before bufpos, so everything else should too.
3551 (throw 'done paren-state)))
3552 (setq paren-state (cdr paren-state)))
3553 nil)))
3554
3555 (defun c-most-enclosing-brace (paren-state &optional bufpos)
3556 ;; Return the bufpos of the innermost enclosing open paren before
3557 ;; bufpos, or nil if none was found.
3558 (let (enclosingp)
3559 (or bufpos (setq bufpos 134217727))
3560 (while paren-state
3561 (setq enclosingp (car paren-state)
3562 paren-state (cdr paren-state))
3563 (if (or (consp enclosingp)
3564 (>= enclosingp bufpos))
3565 (setq enclosingp nil)
3566 (setq paren-state nil)))
3567 enclosingp))
3568
3569 (defun c-least-enclosing-brace (paren-state)
3570 ;; Return the bufpos of the outermost enclosing open paren, or nil
3571 ;; if none was found.
3572 (let (pos elem)
3573 (while paren-state
3574 (setq elem (car paren-state)
3575 paren-state (cdr paren-state))
3576 (if (integerp elem)
3577 (setq pos elem)))
3578 pos))
3579
3580 (defun c-safe-position (bufpos paren-state)
3581 ;; Return the closest "safe" position recorded on PAREN-STATE that
3582 ;; is higher up than BUFPOS. Return nil if PAREN-STATE doesn't
3583 ;; contain any. Return nil if BUFPOS is nil, which is useful to
3584 ;; find the closest limit before a given limit that might be nil.
3585 ;;
3586 ;; A "safe" position is a position at or after a recorded open
3587 ;; paren, or after a recorded close paren. The returned position is
3588 ;; thus either the first position after a close brace, or the first
3589 ;; position after an enclosing paren, or at the enclosing paren in
3590 ;; case BUFPOS is immediately after it.
3591 (when bufpos
3592 (let (elem)
3593 (catch 'done
3594 (while paren-state
3595 (setq elem (car paren-state))
3596 (if (consp elem)
3597 (cond ((< (cdr elem) bufpos)
3598 (throw 'done (cdr elem)))
3599 ((< (car elem) bufpos)
3600 ;; See below.
3601 (throw 'done (min (1+ (car elem)) bufpos))))
3602 (if (< elem bufpos)
3603 ;; elem is the position at and not after the opening paren, so
3604 ;; we can go forward one more step unless it's equal to
3605 ;; bufpos. This is useful in some cases avoid an extra paren
3606 ;; level between the safe position and bufpos.
3607 (throw 'done (min (1+ elem) bufpos))))
3608 (setq paren-state (cdr paren-state)))))))
3609
3610 (defun c-beginning-of-syntax ()
3611 ;; This is used for `font-lock-beginning-of-syntax-function'. It
3612 ;; goes to the closest previous point that is known to be outside
3613 ;; any string literal or comment. `c-state-cache' is used if it has
3614 ;; a position in the vicinity.
3615 (let* ((paren-state c-state-cache)
3616 elem
3617
3618 (pos (catch 'done
3619 ;; Note: Similar code in `c-safe-position'. The
3620 ;; difference is that we accept a safe position at
3621 ;; the point and don't bother to go forward past open
3622 ;; parens.
3623 (while paren-state
3624 (setq elem (car paren-state))
3625 (if (consp elem)
3626 (cond ((<= (cdr elem) (point))
3627 (throw 'done (cdr elem)))
3628 ((<= (car elem) (point))
3629 (throw 'done (car elem))))
3630 (if (<= elem (point))
3631 (throw 'done elem)))
3632 (setq paren-state (cdr paren-state)))
3633 (point-min))))
3634
3635 (if (> pos (- (point) 4000))
3636 (goto-char pos)
3637 ;; The position is far back. Try `c-beginning-of-defun-1'
3638 ;; (although we can't be entirely sure it will go to a position
3639 ;; outside a comment or string in current emacsen). FIXME:
3640 ;; Consult `syntax-ppss' here.
3641 (c-beginning-of-defun-1)
3642 (if (< (point) pos)
3643 (goto-char pos)))))
3644
3645 \f
3646 ;; Tools for scanning identifiers and other tokens.
3647
3648 (defun c-on-identifier ()
3649 "Return non-nil if the point is on or directly after an identifier.
3650 Keywords are recognized and not considered identifiers. If an
3651 identifier is detected, the returned value is its starting position.
3652 If an identifier ends at the point and another begins at it \(can only
3653 happen in Pike) then the point for the preceding one is returned.
3654
3655 Note that this function might do hidden buffer changes. See the
3656 comment at the start of cc-engine.el for more info."
3657
3658 ;; FIXME: Shouldn't this function handle "operator" in C++?
3659
3660 (save-excursion
3661 (skip-syntax-backward "w_")
3662
3663 (or
3664
3665 ;; Check for a normal (non-keyword) identifier.
3666 (and (looking-at c-symbol-start)
3667 (not (looking-at c-keywords-regexp))
3668 (point))
3669
3670 (when (c-major-mode-is 'pike-mode)
3671 ;; Handle the `<operator> syntax in Pike.
3672 (let ((pos (point)))
3673 (skip-chars-backward "-!%&*+/<=>^|~[]()")
3674 (and (if (< (skip-chars-backward "`") 0)
3675 t
3676 (goto-char pos)
3677 (eq (char-after) ?\`))
3678 (looking-at c-symbol-key)
3679 (>= (match-end 0) pos)
3680 (point))))
3681
3682 ;; Handle the "operator +" syntax in C++.
3683 (when (and c-overloadable-operators-regexp
3684 (= (c-backward-token-2 0) 0))
3685
3686 (cond ((and (looking-at c-overloadable-operators-regexp)
3687 (or (not c-opt-op-identifier-prefix)
3688 (and (= (c-backward-token-2 1) 0)
3689 (looking-at c-opt-op-identifier-prefix))))
3690 (point))
3691
3692 ((save-excursion
3693 (and c-opt-op-identifier-prefix
3694 (looking-at c-opt-op-identifier-prefix)
3695 (= (c-forward-token-2 1) 0)
3696 (looking-at c-overloadable-operators-regexp)))
3697 (point))))
3698
3699 )))
3700
3701 (defsubst c-simple-skip-symbol-backward ()
3702 ;; If the point is at the end of a symbol then skip backward to the
3703 ;; beginning of it. Don't move otherwise. Return non-nil if point
3704 ;; moved.
3705 ;;
3706 ;; This function might do hidden buffer changes.
3707 (or (< (skip-syntax-backward "w_") 0)
3708 (and (c-major-mode-is 'pike-mode)
3709 ;; Handle the `<operator> syntax in Pike.
3710 (let ((pos (point)))
3711 (if (and (< (skip-chars-backward "-!%&*+/<=>^|~[]()") 0)
3712 (< (skip-chars-backward "`") 0)
3713 (looking-at c-symbol-key)
3714 (>= (match-end 0) pos))
3715 t
3716 (goto-char pos)
3717 nil)))))
3718
3719 (defun c-beginning-of-current-token (&optional back-limit)
3720 ;; Move to the beginning of the current token. Do not move if not
3721 ;; in the middle of one. BACK-LIMIT may be used to bound the
3722 ;; backward search; if given it's assumed to be at the boundary
3723 ;; between two tokens. Return non-nil if the point is moved, nil
3724 ;; otherwise.
3725 ;;
3726 ;; This function might do hidden buffer changes.
3727 (let ((start (point)))
3728 (if (looking-at "\\w\\|\\s_")
3729 (skip-syntax-backward "w_" back-limit)
3730 (when (< (skip-syntax-backward ".()" back-limit) 0)
3731 (while (let ((pos (or (and (looking-at c-nonsymbol-token-regexp)
3732 (match-end 0))
3733 ;; `c-nonsymbol-token-regexp' should always match
3734 ;; since we've skipped backward over punctuation
3735 ;; or paren syntax, but consume one char in case
3736 ;; it doesn't so that we don't leave point before
3737 ;; some earlier incorrect token.
3738 (1+ (point)))))
3739 (if (<= pos start)
3740 (goto-char pos))))))
3741 (< (point) start)))
3742
3743 (defun c-end-of-current-token (&optional back-limit)
3744 ;; Move to the end of the current token. Do not move if not in the
3745 ;; middle of one. BACK-LIMIT may be used to bound the backward
3746 ;; search; if given it's assumed to be at the boundary between two
3747 ;; tokens. Return non-nil if the point is moved, nil otherwise.
3748 ;;
3749 ;; This function might do hidden buffer changes.
3750 (let ((start (point)))
3751 (cond ((< (skip-syntax-backward "w_" (1- start)) 0)
3752 (skip-syntax-forward "w_"))
3753 ((< (skip-syntax-backward ".()" back-limit) 0)
3754 (while (progn
3755 (if (looking-at c-nonsymbol-token-regexp)
3756 (goto-char (match-end 0))
3757 ;; `c-nonsymbol-token-regexp' should always match since
3758 ;; we've skipped backward over punctuation or paren
3759 ;; syntax, but move forward in case it doesn't so that
3760 ;; we don't leave point earlier than we started with.
3761 (forward-char))
3762 (< (point) start)))))
3763 (> (point) start)))
3764
3765 (defconst c-jump-syntax-balanced
3766 (if (memq 'gen-string-delim c-emacs-features)
3767 "\\w\\|\\s_\\|\\s\(\\|\\s\)\\|\\s\"\\|\\s|"
3768 "\\w\\|\\s_\\|\\s\(\\|\\s\)\\|\\s\""))
3769
3770 (defconst c-jump-syntax-unbalanced
3771 (if (memq 'gen-string-delim c-emacs-features)
3772 "\\w\\|\\s_\\|\\s\"\\|\\s|"
3773 "\\w\\|\\s_\\|\\s\""))
3774
3775 (defun c-forward-token-2 (&optional count balanced limit)
3776 "Move forward by tokens.
3777 A token is defined as all symbols and identifiers which aren't
3778 syntactic whitespace \(note that multicharacter tokens like \"==\" are
3779 treated properly). Point is always either left at the beginning of a
3780 token or not moved at all. COUNT specifies the number of tokens to
3781 move; a negative COUNT moves in the opposite direction. A COUNT of 0
3782 moves to the next token beginning only if not already at one. If
3783 BALANCED is true, move over balanced parens, otherwise move into them.
3784 Also, if BALANCED is true, never move out of an enclosing paren.
3785
3786 LIMIT sets the limit for the movement and defaults to the point limit.
3787 The case when LIMIT is set in the middle of a token, comment or macro
3788 is handled correctly, i.e. the point won't be left there.
3789
3790 Return the number of tokens left to move \(positive or negative). If
3791 BALANCED is true, a move over a balanced paren counts as one. Note
3792 that if COUNT is 0 and no appropriate token beginning is found, 1 will
3793 be returned. Thus, a return value of 0 guarantees that point is at
3794 the requested position and a return value less \(without signs) than
3795 COUNT guarantees that point is at the beginning of some token.
3796
3797 Note that this function might do hidden buffer changes. See the
3798 comment at the start of cc-engine.el for more info."
3799
3800 (or count (setq count 1))
3801 (if (< count 0)
3802 (- (c-backward-token-2 (- count) balanced limit))
3803
3804 (let ((jump-syntax (if balanced
3805 c-jump-syntax-balanced
3806 c-jump-syntax-unbalanced))
3807 (last (point))
3808 (prev (point)))
3809
3810 (if (zerop count)
3811 ;; If count is zero we should jump if in the middle of a token.
3812 (c-end-of-current-token))
3813
3814 (save-restriction
3815 (if limit (narrow-to-region (point-min) limit))
3816 (if (/= (point)
3817 (progn (c-forward-syntactic-ws) (point)))
3818 ;; Skip whitespace. Count this as a move if we did in
3819 ;; fact move.
3820 (setq count (max (1- count) 0)))
3821
3822 (if (eobp)
3823 ;; Moved out of bounds. Make sure the returned count isn't zero.
3824 (progn
3825 (if (zerop count) (setq count 1))
3826 (goto-char last))
3827
3828 ;; Use `condition-case' to avoid having the limit tests
3829 ;; inside the loop.
3830 (condition-case nil
3831 (while (and
3832 (> count 0)
3833 (progn
3834 (setq last (point))
3835 (cond ((looking-at jump-syntax)
3836 (goto-char (scan-sexps (point) 1))
3837 t)
3838 ((looking-at c-nonsymbol-token-regexp)
3839 (goto-char (match-end 0))
3840 t)
3841 ;; `c-nonsymbol-token-regexp' above should always
3842 ;; match if there are correct tokens. Try to
3843 ;; widen to see if the limit was set in the
3844 ;; middle of one, else fall back to treating
3845 ;; the offending thing as a one character token.
3846 ((and limit
3847 (save-restriction
3848 (widen)
3849 (looking-at c-nonsymbol-token-regexp)))
3850 nil)
3851 (t
3852 (forward-char)
3853 t))))
3854 (c-forward-syntactic-ws)
3855 (setq prev last
3856 count (1- count)))
3857 (error (goto-char last)))
3858
3859 (when (eobp)
3860 (goto-char prev)
3861 (setq count (1+ count)))))
3862
3863 count)))
3864
3865 (defun c-backward-token-2 (&optional count balanced limit)
3866 "Move backward by tokens.
3867 See `c-forward-token-2' for details."
3868
3869 (or count (setq count 1))
3870 (if (< count 0)
3871 (- (c-forward-token-2 (- count) balanced limit))
3872
3873 (or limit (setq limit (point-min)))
3874 (let ((jump-syntax (if balanced
3875 c-jump-syntax-balanced
3876 c-jump-syntax-unbalanced))
3877 (last (point)))
3878
3879 (if (zerop count)
3880 ;; The count is zero so try to skip to the beginning of the
3881 ;; current token.
3882 (if (> (point)
3883 (progn (c-beginning-of-current-token) (point)))
3884 (if (< (point) limit)
3885 ;; The limit is inside the same token, so return 1.
3886 (setq count 1))
3887
3888 ;; We're not in the middle of a token. If there's
3889 ;; whitespace after the point then we must move backward,
3890 ;; so set count to 1 in that case.
3891 (and (looking-at c-syntactic-ws-start)
3892 ;; If we're looking at a '#' that might start a cpp
3893 ;; directive then we have to do a more elaborate check.
3894 (or (/= (char-after) ?#)
3895 (not c-opt-cpp-prefix)
3896 (save-excursion
3897 (and (= (point)
3898 (progn (beginning-of-line)
3899 (looking-at "[ \t]*")
3900 (match-end 0)))
3901 (or (bobp)
3902 (progn (backward-char)
3903 (not (eq (char-before) ?\\)))))))
3904 (setq count 1))))
3905
3906 ;; Use `condition-case' to avoid having to check for buffer
3907 ;; limits in `backward-char', `scan-sexps' and `goto-char' below.
3908 (condition-case nil
3909 (while (and
3910 (> count 0)
3911 (progn
3912 (c-backward-syntactic-ws)
3913 (backward-char)
3914 (if (looking-at jump-syntax)
3915 (goto-char (scan-sexps (1+ (point)) -1))
3916 ;; This can be very inefficient if there's a long
3917 ;; sequence of operator tokens without any separation.
3918 ;; That doesn't happen in practice, anyway.
3919 (c-beginning-of-current-token))
3920 (>= (point) limit)))
3921 (setq last (point)
3922 count (1- count)))
3923 (error (goto-char last)))
3924
3925 (if (< (point) limit)
3926 (goto-char last))
3927
3928 count)))
3929
3930 (defun c-forward-token-1 (&optional count balanced limit)
3931 "Like `c-forward-token-2' but doesn't treat multicharacter operator
3932 tokens like \"==\" as single tokens, i.e. all sequences of symbol
3933 characters are jumped over character by character. This function is
3934 for compatibility only; it's only a wrapper over `c-forward-token-2'."
3935 (let ((c-nonsymbol-token-regexp "\\s.\\|\\s\(\\|\\s\)"))
3936 (c-forward-token-2 count balanced limit)))
3937
3938 (defun c-backward-token-1 (&optional count balanced limit)
3939 "Like `c-backward-token-2' but doesn't treat multicharacter operator
3940 tokens like \"==\" as single tokens, i.e. all sequences of symbol
3941 characters are jumped over character by character. This function is
3942 for compatibility only; it's only a wrapper over `c-backward-token-2'."
3943 (let ((c-nonsymbol-token-regexp "\\s.\\|\\s\(\\|\\s\)"))
3944 (c-backward-token-2 count balanced limit)))
3945
3946 \f
3947 ;; Tools for doing searches restricted to syntactically relevant text.
3948
3949 (defun c-syntactic-re-search-forward (regexp &optional bound noerror
3950 paren-level not-inside-token
3951 lookbehind-submatch)
3952 "Like `re-search-forward', but only report matches that are found
3953 in syntactically significant text. I.e. matches in comments, macros
3954 or string literals are ignored. The start point is assumed to be
3955 outside any comment, macro or string literal, or else the content of
3956 that region is taken as syntactically significant text.
3957
3958 If PAREN-LEVEL is non-nil, an additional restriction is added to
3959 ignore matches in nested paren sexps. The search will also not go
3960 outside the current list sexp, which has the effect that if the point
3961 should be moved to BOUND when no match is found \(i.e. NOERROR is
3962 neither nil nor t), then it will be at the closing paren if the end of
3963 the current list sexp is encountered first.
3964
3965 If NOT-INSIDE-TOKEN is non-nil, matches in the middle of tokens are
3966 ignored. Things like multicharacter operators and special symbols
3967 \(e.g. \"`()\" in Pike) are handled but currently not floating point
3968 constants.
3969
3970 If LOOKBEHIND-SUBMATCH is non-nil, it's taken as a number of a
3971 subexpression in REGEXP. The end of that submatch is used as the
3972 position to check for syntactic significance. If LOOKBEHIND-SUBMATCH
3973 isn't used or if that subexpression didn't match then the start
3974 position of the whole match is used instead. The \"look behind\"
3975 subexpression is never tested before the starting position, so it
3976 might be a good idea to include \\=\\= as a match alternative in it.
3977
3978 Optimization note: Matches might be missed if the \"look behind\"
3979 subexpression can match the end of nonwhite syntactic whitespace,
3980 i.e. the end of comments or cpp directives. This since the function
3981 skips over such things before resuming the search. It's on the other
3982 hand not safe to assume that the \"look behind\" subexpression never
3983 matches syntactic whitespace.
3984
3985 Bug: Unbalanced parens inside cpp directives are currently not handled
3986 correctly \(i.e. they don't get ignored as they should) when
3987 PAREN-LEVEL is set.
3988
3989 Note that this function might do hidden buffer changes. See the
3990 comment at the start of cc-engine.el for more info."
3991
3992 (or bound (setq bound (point-max)))
3993 (if paren-level (setq paren-level -1))
3994
3995 ;;(message "c-syntactic-re-search-forward %s %s %S" (point) bound regexp)
3996
3997 (let ((start (point))
3998 tmp
3999 ;; Start position for the last search.
4000 search-pos
4001 ;; The `parse-partial-sexp' state between the start position
4002 ;; and the point.
4003 state
4004 ;; The current position after the last state update. The next
4005 ;; `parse-partial-sexp' continues from here.
4006 (state-pos (point))
4007 ;; The position at which to check the state and the state
4008 ;; there. This is separate from `state-pos' since we might
4009 ;; need to back up before doing the next search round.
4010 check-pos check-state
4011 ;; Last position known to end a token.
4012 (last-token-end-pos (point-min))
4013 ;; Set when a valid match is found.
4014 found)
4015
4016 (condition-case err
4017 (while
4018 (and
4019 (progn
4020 (setq search-pos (point))
4021 (re-search-forward regexp bound noerror))
4022
4023 (progn
4024 (setq state (parse-partial-sexp
4025 state-pos (match-beginning 0) paren-level nil state)
4026 state-pos (point))
4027 (if (setq check-pos (and lookbehind-submatch
4028 (or (not paren-level)
4029 (>= (car state) 0))
4030 (match-end lookbehind-submatch)))
4031 (setq check-state (parse-partial-sexp
4032 state-pos check-pos paren-level nil state))
4033 (setq check-pos state-pos
4034 check-state state))
4035
4036 ;; NOTE: If we got a look behind subexpression and get
4037 ;; an insignificant match in something that isn't
4038 ;; syntactic whitespace (i.e. strings or in nested
4039 ;; parentheses), then we can never skip more than a
4040 ;; single character from the match start position
4041 ;; (i.e. `state-pos' here) before continuing the
4042 ;; search. That since the look behind subexpression
4043 ;; might match the end of the insignificant region in
4044 ;; the next search.
4045
4046 (cond
4047 ((elt check-state 7)
4048 ;; Match inside a line comment. Skip to eol. Use
4049 ;; `re-search-forward' instead of `skip-chars-forward' to get
4050 ;; the right bound behavior.
4051 (re-search-forward "[\n\r]" bound noerror))
4052
4053 ((elt check-state 4)
4054 ;; Match inside a block comment. Skip to the '*/'.
4055 (search-forward "*/" bound noerror))
4056
4057 ((and (not (elt check-state 5))
4058 (eq (char-before check-pos) ?/)
4059 (not (c-get-char-property (1- check-pos) 'syntax-table))
4060 (memq (char-after check-pos) '(?/ ?*)))
4061 ;; Match in the middle of the opener of a block or line
4062 ;; comment.
4063 (if (= (char-after check-pos) ?/)
4064 (re-search-forward "[\n\r]" bound noerror)
4065 (search-forward "*/" bound noerror)))
4066
4067 ;; The last `parse-partial-sexp' above might have
4068 ;; stopped short of the real check position if the end
4069 ;; of the current sexp was encountered in paren-level
4070 ;; mode. The checks above are always false in that
4071 ;; case, and since they can do better skipping in
4072 ;; lookbehind-submatch mode, we do them before
4073 ;; checking the paren level.
4074
4075 ((and paren-level
4076 (/= (setq tmp (car check-state)) 0))
4077 ;; Check the paren level first since we're short of the
4078 ;; syntactic checking position if the end of the
4079 ;; current sexp was encountered by `parse-partial-sexp'.
4080 (if (> tmp 0)
4081
4082 ;; Inside a nested paren sexp.
4083 (if lookbehind-submatch
4084 ;; See the NOTE above.
4085 (progn (goto-char state-pos) t)
4086 ;; Skip out of the paren quickly.
4087 (setq state (parse-partial-sexp state-pos bound 0 nil state)
4088 state-pos (point)))
4089
4090 ;; Have exited the current paren sexp.
4091 (if noerror
4092 (progn
4093 ;; The last `parse-partial-sexp' call above
4094 ;; has left us just after the closing paren
4095 ;; in this case, so we can modify the bound
4096 ;; to leave the point at the right position
4097 ;; upon return.
4098 (setq bound (1- (point)))
4099 nil)
4100 (signal 'search-failed (list regexp)))))
4101
4102 ((setq tmp (elt check-state 3))
4103 ;; Match inside a string.
4104 (if (or lookbehind-submatch
4105 (not (integerp tmp)))
4106 ;; See the NOTE above.
4107 (progn (goto-char state-pos) t)
4108 ;; Skip to the end of the string before continuing.
4109 (let ((ender (make-string 1 tmp)) (continue t))
4110 (while (if (search-forward ender bound noerror)
4111 (progn
4112 (setq state (parse-partial-sexp
4113 state-pos (point) nil nil state)
4114 state-pos (point))
4115 (elt state 3))
4116 (setq continue nil)))
4117 continue)))
4118
4119 ((save-excursion
4120 (save-match-data
4121 (c-beginning-of-macro start)))
4122 ;; Match inside a macro. Skip to the end of it.
4123 (c-end-of-macro)
4124 (cond ((<= (point) bound) t)
4125 (noerror nil)
4126 (t (signal 'search-failed (list regexp)))))
4127
4128 ((and not-inside-token
4129 (or (< check-pos last-token-end-pos)
4130 (< check-pos
4131 (save-excursion
4132 (goto-char check-pos)
4133 (save-match-data
4134 (c-end-of-current-token last-token-end-pos))
4135 (setq last-token-end-pos (point))))))
4136 ;; Inside a token.
4137 (if lookbehind-submatch
4138 ;; See the NOTE above.
4139 (goto-char state-pos)
4140 (goto-char (min last-token-end-pos bound))))
4141
4142 (t
4143 ;; A real match.
4144 (setq found t)
4145 nil)))
4146
4147 ;; Should loop to search again, but take care to avoid
4148 ;; looping on the same spot.
4149 (or (/= search-pos (point))
4150 (if (= (point) bound)
4151 (if noerror
4152 nil
4153 (signal 'search-failed (list regexp)))
4154 (forward-char)
4155 t))))
4156
4157 (error
4158 (goto-char start)
4159 (signal (car err) (cdr err))))
4160
4161 ;;(message "c-syntactic-re-search-forward done %s" (or (match-end 0) (point)))
4162
4163 (if found
4164 (progn
4165 (goto-char (match-end 0))
4166 (match-end 0))
4167
4168 ;; Search failed. Set point as appropriate.
4169 (if (eq noerror t)
4170 (goto-char start)
4171 (goto-char bound))
4172 nil)))
4173
4174 (defvar safe-pos-list) ; bound in c-syntactic-skip-backward
4175
4176 (defsubst c-ssb-lit-begin ()
4177 ;; Return the start of the literal point is in, or nil.
4178 ;; We read and write the variables `safe-pos', `safe-pos-list', `state'
4179 ;; bound in the caller.
4180
4181 ;; Use `parse-partial-sexp' from a safe position down to the point to check
4182 ;; if it's outside comments and strings.
4183 (save-excursion
4184 (let ((pos (point)) safe-pos state pps-end-pos)
4185 ;; Pick a safe position as close to the point as possible.
4186 ;;
4187 ;; FIXME: Consult `syntax-ppss' here if our cache doesn't give a good
4188 ;; position.
4189
4190 (while (and safe-pos-list
4191 (> (car safe-pos-list) (point)))
4192 (setq safe-pos-list (cdr safe-pos-list)))
4193 (unless (setq safe-pos (car-safe safe-pos-list))
4194 (setq safe-pos (max (or (c-safe-position
4195 (point) (or c-state-cache
4196 (c-parse-state)))
4197 0)
4198 (point-min))
4199 safe-pos-list (list safe-pos)))
4200
4201 ;; Cache positions along the way to use if we have to back up more. We
4202 ;; cache every closing paren on the same level. If the paren cache is
4203 ;; relevant in this region then we're typically already on the same
4204 ;; level as the target position. Note that we might cache positions
4205 ;; after opening parens in case safe-pos is in a nested list. That's
4206 ;; both uncommon and harmless.
4207 (while (progn
4208 (setq state (parse-partial-sexp
4209 safe-pos pos 0))
4210 (< (point) pos))
4211 (setq safe-pos (point)
4212 safe-pos-list (cons safe-pos safe-pos-list)))
4213
4214 ;; If the state contains the start of the containing sexp we cache that
4215 ;; position too, so that parse-partial-sexp in the next run has a bigger
4216 ;; chance of starting at the same level as the target position and thus
4217 ;; will get more good safe positions into the list.
4218 (if (elt state 1)
4219 (setq safe-pos (1+ (elt state 1))
4220 safe-pos-list (cons safe-pos safe-pos-list)))
4221
4222 (if (or (elt state 3) (elt state 4))
4223 ;; Inside string or comment. Continue search at the
4224 ;; beginning of it.
4225 (elt state 8)))))
4226
4227 (defun c-syntactic-skip-backward (skip-chars &optional limit paren-level)
4228 "Like `skip-chars-backward' but only look at syntactically relevant chars,
4229 i.e. don't stop at positions inside syntactic whitespace or string
4230 literals. Preprocessor directives are also ignored, with the exception
4231 of the one that the point starts within, if any. If LIMIT is given,
4232 it's assumed to be at a syntactically relevant position.
4233
4234 If PAREN-LEVEL is non-nil, the function won't stop in nested paren
4235 sexps, and the search will also not go outside the current paren sexp.
4236 However, if LIMIT or the buffer limit is reached inside a nested paren
4237 then the point will be left at the limit.
4238
4239 Non-nil is returned if the point moved, nil otherwise.
4240
4241 Note that this function might do hidden buffer changes. See the
4242 comment at the start of cc-engine.el for more info."
4243
4244 (let ((start (point))
4245 state-2
4246 ;; A list of syntactically relevant positions in descending
4247 ;; order. It's used to avoid scanning repeatedly over
4248 ;; potentially large regions with `parse-partial-sexp' to verify
4249 ;; each position. Used in `c-ssb-lit-begin'
4250 safe-pos-list
4251 ;; The result from `c-beginning-of-macro' at the start position or the
4252 ;; start position itself if it isn't within a macro. Evaluated on
4253 ;; demand.
4254 start-macro-beg
4255 ;; The earliest position after the current one with the same paren
4256 ;; level. Used only when `paren-level' is set.
4257 lit-beg
4258 (paren-level-pos (point)))
4259
4260 (while
4261 (progn
4262 ;; The next loop "tries" to find the end point each time round,
4263 ;; loops when it hasn't succeeded.
4264 (while
4265 (and
4266 (< (skip-chars-backward skip-chars limit) 0)
4267
4268 (let ((pos (point)) state-2 pps-end-pos)
4269
4270 (cond
4271 ;; Don't stop inside a literal
4272 ((setq lit-beg (c-ssb-lit-begin))
4273 (goto-char lit-beg)
4274 t)
4275
4276 ((and paren-level
4277 (save-excursion
4278 (setq state-2 (parse-partial-sexp
4279 pos paren-level-pos -1)
4280 pps-end-pos (point))
4281 (/= (car state-2) 0)))
4282 ;; Not at the right level.
4283
4284 (if (and (< (car state-2) 0)
4285 ;; We stop above if we go out of a paren.
4286 ;; Now check whether it precedes or is
4287 ;; nested in the starting sexp.
4288 (save-excursion
4289 (setq state-2
4290 (parse-partial-sexp
4291 pps-end-pos paren-level-pos
4292 nil nil state-2))
4293 (< (car state-2) 0)))
4294
4295 ;; We've stopped short of the starting position
4296 ;; so the hit was inside a nested list. Go up
4297 ;; until we are at the right level.
4298 (condition-case nil
4299 (progn
4300 (goto-char (scan-lists pos -1
4301 (- (car state-2))))
4302 (setq paren-level-pos (point))
4303 (if (and limit (>= limit paren-level-pos))
4304 (progn
4305 (goto-char limit)
4306 nil)
4307 t))
4308 (error
4309 (goto-char (or limit (point-min)))
4310 nil))
4311
4312 ;; The hit was outside the list at the start
4313 ;; position. Go to the start of the list and exit.
4314 (goto-char (1+ (elt state-2 1)))
4315 nil))
4316
4317 ((c-beginning-of-macro limit)
4318 ;; Inside a macro.
4319 (if (< (point)
4320 (or start-macro-beg
4321 (setq start-macro-beg
4322 (save-excursion
4323 (goto-char start)
4324 (c-beginning-of-macro limit)
4325 (point)))))
4326 t
4327
4328 ;; It's inside the same macro we started in so it's
4329 ;; a relevant match.
4330 (goto-char pos)
4331 nil))))))
4332
4333 (> (point)
4334 (progn
4335 ;; Skip syntactic ws afterwards so that we don't stop at the
4336 ;; end of a comment if `skip-chars' is something like "^/".
4337 (c-backward-syntactic-ws)
4338 (point)))))
4339
4340 ;; We might want to extend this with more useful return values in
4341 ;; the future.
4342 (/= (point) start)))
4343
4344 ;; The following is an alternative implementation of
4345 ;; `c-syntactic-skip-backward' that uses backward movement to keep
4346 ;; track of the syntactic context. It turned out to be generally
4347 ;; slower than the one above which uses forward checks from earlier
4348 ;; safe positions.
4349 ;;
4350 ;;(defconst c-ssb-stop-re
4351 ;; ;; The regexp matching chars `c-syntactic-skip-backward' needs to
4352 ;; ;; stop at to avoid going into comments and literals.
4353 ;; (concat
4354 ;; ;; Match comment end syntax and string literal syntax. Also match
4355 ;; ;; '/' for block comment endings (not covered by comment end
4356 ;; ;; syntax).
4357 ;; "\\s>\\|/\\|\\s\""
4358 ;; (if (memq 'gen-string-delim c-emacs-features)
4359 ;; "\\|\\s|"
4360 ;; "")
4361 ;; (if (memq 'gen-comment-delim c-emacs-features)
4362 ;; "\\|\\s!"
4363 ;; "")))
4364 ;;
4365 ;;(defconst c-ssb-stop-paren-re
4366 ;; ;; Like `c-ssb-stop-re' but also stops at paren chars.
4367 ;; (concat c-ssb-stop-re "\\|\\s(\\|\\s)"))
4368 ;;
4369 ;;(defconst c-ssb-sexp-end-re
4370 ;; ;; Regexp matching the ending syntax of a complex sexp.
4371 ;; (concat c-string-limit-regexp "\\|\\s)"))
4372 ;;
4373 ;;(defun c-syntactic-skip-backward (skip-chars &optional limit paren-level)
4374 ;; "Like `skip-chars-backward' but only look at syntactically relevant chars,
4375 ;;i.e. don't stop at positions inside syntactic whitespace or string
4376 ;;literals. Preprocessor directives are also ignored. However, if the
4377 ;;point is within a comment, string literal or preprocessor directory to
4378 ;;begin with, its contents is treated as syntactically relevant chars.
4379 ;;If LIMIT is given, it limits the backward search and the point will be
4380 ;;left there if no earlier position is found.
4381 ;;
4382 ;;If PAREN-LEVEL is non-nil, the function won't stop in nested paren
4383 ;;sexps, and the search will also not go outside the current paren sexp.
4384 ;;However, if LIMIT or the buffer limit is reached inside a nested paren
4385 ;;then the point will be left at the limit.
4386 ;;
4387 ;;Non-nil is returned if the point moved, nil otherwise.
4388 ;;
4389 ;;Note that this function might do hidden buffer changes. See the
4390 ;;comment at the start of cc-engine.el for more info."
4391 ;;
4392 ;; (save-restriction
4393 ;; (when limit
4394 ;; (narrow-to-region limit (point-max)))
4395 ;;
4396 ;; (let ((start (point)))
4397 ;; (catch 'done
4398 ;; (while (let ((last-pos (point))
4399 ;; (stop-pos (progn
4400 ;; (skip-chars-backward skip-chars)
4401 ;; (point))))
4402 ;;
4403 ;; ;; Skip back over the same region as
4404 ;; ;; `skip-chars-backward' above, but keep to
4405 ;; ;; syntactically relevant positions.
4406 ;; (goto-char last-pos)
4407 ;; (while (and
4408 ;; ;; `re-search-backward' with a single char regexp
4409 ;; ;; should be fast.
4410 ;; (re-search-backward
4411 ;; (if paren-level c-ssb-stop-paren-re c-ssb-stop-re)
4412 ;; stop-pos 'move)
4413 ;;
4414 ;; (progn
4415 ;; (cond
4416 ;; ((looking-at "\\s(")
4417 ;; ;; `paren-level' is set and we've found the
4418 ;; ;; start of the containing paren.
4419 ;; (forward-char)
4420 ;; (throw 'done t))
4421 ;;
4422 ;; ((looking-at c-ssb-sexp-end-re)
4423 ;; ;; We're at the end of a string literal or paren
4424 ;; ;; sexp (if `paren-level' is set).
4425 ;; (forward-char)
4426 ;; (condition-case nil
4427 ;; (c-backward-sexp)
4428 ;; (error
4429 ;; (goto-char limit)
4430 ;; (throw 'done t))))
4431 ;;
4432 ;; (t
4433 ;; (forward-char)
4434 ;; ;; At the end of some syntactic ws or possibly
4435 ;; ;; after a plain '/' operator.
4436 ;; (let ((pos (point)))
4437 ;; (c-backward-syntactic-ws)
4438 ;; (if (= pos (point))
4439 ;; ;; Was a plain '/' operator. Go past it.
4440 ;; (backward-char)))))
4441 ;;
4442 ;; (> (point) stop-pos))))
4443 ;;
4444 ;; ;; Now the point is either at `stop-pos' or at some
4445 ;; ;; position further back if `stop-pos' was at a
4446 ;; ;; syntactically irrelevant place.
4447 ;;
4448 ;; ;; Skip additional syntactic ws so that we don't stop
4449 ;; ;; at the end of a comment if `skip-chars' is
4450 ;; ;; something like "^/".
4451 ;; (c-backward-syntactic-ws)
4452 ;;
4453 ;; (< (point) stop-pos))))
4454 ;;
4455 ;; ;; We might want to extend this with more useful return values
4456 ;; ;; in the future.
4457 ;; (/= (point) start))))
4458
4459 \f
4460 ;; Tools for handling comments and string literals.
4461
4462 (defun c-in-literal (&optional lim detect-cpp)
4463 "Return the type of literal point is in, if any.
4464 The return value is `c' if in a C-style comment, `c++' if in a C++
4465 style comment, `string' if in a string literal, `pound' if DETECT-CPP
4466 is non-nil and in a preprocessor line, or nil if somewhere else.
4467 Optional LIM is used as the backward limit of the search. If omitted,
4468 or nil, `c-beginning-of-defun' is used.
4469
4470 The last point calculated is cached if the cache is enabled, i.e. if
4471 `c-in-literal-cache' is bound to a two element vector.
4472
4473 Note that this function might do hidden buffer changes. See the
4474 comment at the start of cc-engine.el for more info."
4475 (save-restriction
4476 (widen)
4477 (let* ((safe-place (c-state-semi-safe-place (point)))
4478 (lit (c-state-pp-to-literal safe-place (point))))
4479 (or (cadr lit)
4480 (and detect-cpp
4481 (save-excursion (c-beginning-of-macro))
4482 'pound)))))
4483
4484 (defun c-literal-limits (&optional lim near not-in-delimiter)
4485 "Return a cons of the beginning and end positions of the comment or
4486 string surrounding point (including both delimiters), or nil if point
4487 isn't in one. If LIM is non-nil, it's used as the \"safe\" position
4488 to start parsing from. If NEAR is non-nil, then the limits of any
4489 literal next to point is returned. \"Next to\" means there's only
4490 spaces and tabs between point and the literal. The search for such a
4491 literal is done first in forward direction. If NOT-IN-DELIMITER is
4492 non-nil, the case when point is inside a starting delimiter won't be
4493 recognized. This only has effect for comments which have starting
4494 delimiters with more than one character.
4495
4496 Note that this function might do hidden buffer changes. See the
4497 comment at the start of cc-engine.el for more info."
4498
4499 (save-excursion
4500 (let* ((pos (point))
4501 (lim (or lim (c-state-semi-safe-place pos)))
4502 (pp-to-lit (save-restriction
4503 (widen)
4504 (c-state-pp-to-literal lim pos not-in-delimiter)))
4505 (state (car pp-to-lit))
4506 (lit-limits (car (cddr pp-to-lit))))
4507
4508 (cond
4509 (lit-limits)
4510
4511 (near
4512 (goto-char pos)
4513 ;; Search forward for a literal.
4514 (skip-chars-forward " \t")
4515 (cond
4516 ((looking-at c-string-limit-regexp) ; String.
4517 (cons (point) (or (c-safe (c-forward-sexp 1) (point))
4518 (point-max))))
4519
4520 ((looking-at c-comment-start-regexp) ; Line or block comment.
4521 (cons (point) (progn (c-forward-single-comment) (point))))
4522
4523 (t
4524 ;; Search backward.
4525 (skip-chars-backward " \t")
4526
4527 (let ((end (point)) beg)
4528 (cond
4529 ((save-excursion
4530 (< (skip-syntax-backward c-string-syntax) 0)) ; String.
4531 (setq beg (c-safe (c-backward-sexp 1) (point))))
4532
4533 ((and (c-safe (forward-char -2) t)
4534 (looking-at "*/"))
4535 ;; Block comment. Due to the nature of line
4536 ;; comments, they will always be covered by the
4537 ;; normal case above.
4538 (goto-char end)
4539 (c-backward-single-comment)
4540 ;; If LIM is bogus, beg will be bogus.
4541 (setq beg (point))))
4542
4543 (if beg (cons beg end))))))
4544 ))))
4545
4546 ;; In case external callers use this; it did have a docstring.
4547 (defalias 'c-literal-limits-fast 'c-literal-limits)
4548
4549 (defun c-collect-line-comments (range)
4550 "If the argument is a cons of two buffer positions (such as returned by
4551 `c-literal-limits'), and that range contains a C++ style line comment,
4552 then an extended range is returned that contains all adjacent line
4553 comments (i.e. all comments that starts in the same column with no
4554 empty lines or non-whitespace characters between them). Otherwise the
4555 argument is returned.
4556
4557 Note that this function might do hidden buffer changes. See the
4558 comment at the start of cc-engine.el for more info."
4559
4560 (save-excursion
4561 (condition-case nil
4562 (if (and (consp range) (progn
4563 (goto-char (car range))
4564 (looking-at c-line-comment-starter)))
4565 (let ((col (current-column))
4566 (beg (point))
4567 (bopl (c-point 'bopl))
4568 (end (cdr range)))
4569 ;; Got to take care in the backward direction to handle
4570 ;; comments which are preceded by code.
4571 (while (and (c-backward-single-comment)
4572 (>= (point) bopl)
4573 (looking-at c-line-comment-starter)
4574 (= col (current-column)))
4575 (setq beg (point)
4576 bopl (c-point 'bopl)))
4577 (goto-char end)
4578 (while (and (progn (skip-chars-forward " \t")
4579 (looking-at c-line-comment-starter))
4580 (= col (current-column))
4581 (prog1 (zerop (forward-line 1))
4582 (setq end (point)))))
4583 (cons beg end))
4584 range)
4585 (error range))))
4586
4587 (defun c-literal-type (range)
4588 "Convenience function that given the result of `c-literal-limits',
4589 returns nil or the type of literal that the range surrounds, one
4590 of the symbols 'c, 'c++ or 'string. It's much faster than using
4591 `c-in-literal' and is intended to be used when you need both the
4592 type of a literal and its limits.
4593
4594 Note that this function might do hidden buffer changes. See the
4595 comment at the start of cc-engine.el for more info."
4596
4597 (if (consp range)
4598 (save-excursion
4599 (goto-char (car range))
4600 (cond ((looking-at c-string-limit-regexp) 'string)
4601 ((or (looking-at "//") ; c++ line comment
4602 (and (looking-at "\\s<") ; comment starter
4603 (looking-at "#"))) ; awk comment.
4604 'c++)
4605 (t 'c))) ; Assuming the range is valid.
4606 range))
4607
4608 (defsubst c-determine-limit-get-base (start try-size)
4609 ;; Get a "safe place" approximately TRY-SIZE characters before START.
4610 ;; This doesn't preserve point.
4611 (let* ((pos (max (- start try-size) (point-min)))
4612 (base (c-state-semi-safe-place pos))
4613 (s (parse-partial-sexp base pos)))
4614 (if (or (nth 4 s) (nth 3 s)) ; comment or string
4615 (nth 8 s)
4616 (point))))
4617
4618 (defun c-determine-limit (how-far-back &optional start try-size)
4619 ;; Return a buffer position HOW-FAR-BACK non-literal characters from START
4620 ;; (default point). This is done by going back further in the buffer then
4621 ;; searching forward for literals. The position found won't be in a
4622 ;; literal. We start searching for the sought position TRY-SIZE (default
4623 ;; twice HOW-FAR-BACK) bytes back from START. This function must be fast.
4624 ;; :-)
4625 (save-excursion
4626 (let* ((start (or start (point)))
4627 (try-size (or try-size (* 2 how-far-back)))
4628 (base (c-determine-limit-get-base start try-size))
4629 (pos base)
4630
4631 (s (parse-partial-sexp pos pos)) ; null state.
4632 stack elt size
4633 (count 0))
4634 (while (< pos start)
4635 ;; Move forward one literal each time round this loop.
4636 ;; Move forward to the start of a comment or string.
4637 (setq s (parse-partial-sexp
4638 pos
4639 start
4640 nil ; target-depth
4641 nil ; stop-before
4642 s ; state
4643 'syntax-table)) ; stop-comment
4644
4645 ;; Gather details of the non-literal-bit - starting pos and size.
4646 (setq size (- (if (or (nth 4 s) (nth 3 s))
4647 (nth 8 s)
4648 (point))
4649 pos))
4650 (if (> size 0)
4651 (setq stack (cons (cons pos size) stack)))
4652
4653 ;; Move forward to the end of the comment/string.
4654 (if (or (nth 4 s) (nth 3 s))
4655 (setq s (parse-partial-sexp
4656 (point)
4657 start
4658 nil ; target-depth
4659 nil ; stop-before
4660 s ; state
4661 'syntax-table))) ; stop-comment
4662 (setq pos (point)))
4663
4664 ;; Now try and find enough non-literal characters recorded on the stack.
4665 ;; Go back one recorded literal each time round this loop.
4666 (while (and (< count how-far-back)
4667 stack)
4668 (setq elt (car stack)
4669 stack (cdr stack))
4670 (setq count (+ count (cdr elt))))
4671
4672 ;; Have we found enough yet?
4673 (cond
4674 ((>= count how-far-back)
4675 (+ (car elt) (- count how-far-back)))
4676 ((eq base (point-min))
4677 (point-min))
4678 (t
4679 (c-determine-limit (- how-far-back count) base try-size))))))
4680
4681 (defun c-determine-+ve-limit (how-far &optional start-pos)
4682 ;; Return a buffer position about HOW-FAR non-literal characters forward
4683 ;; from START-POS (default point), which must not be inside a literal.
4684 (save-excursion
4685 (let ((pos (or start-pos (point)))
4686 (count how-far)
4687 (s (parse-partial-sexp (point) (point)))) ; null state
4688 (while (and (not (eobp))
4689 (> count 0))
4690 ;; Scan over counted characters.
4691 (setq s (parse-partial-sexp
4692 pos
4693 (min (+ pos count) (point-max))
4694 nil ; target-depth
4695 nil ; stop-before
4696 s ; state
4697 'syntax-table)) ; stop-comment
4698 (setq count (- count (- (point) pos) 1)
4699 pos (point))
4700 ;; Scan over literal characters.
4701 (if (nth 8 s)
4702 (setq s (parse-partial-sexp
4703 pos
4704 (point-max)
4705 nil ; target-depth
4706 nil ; stop-before
4707 s ; state
4708 'syntax-table) ; stop-comment
4709 pos (point))))
4710 (point))))
4711
4712 \f
4713 ;; `c-find-decl-spots' and accompanying stuff.
4714
4715 ;; Variables used in `c-find-decl-spots' to cache the search done for
4716 ;; the first declaration in the last call. When that function starts,
4717 ;; it needs to back up over syntactic whitespace to look at the last
4718 ;; token before the region being searched. That can sometimes cause
4719 ;; moves back and forth over a quite large region of comments and
4720 ;; macros, which would be repeated for each changed character when
4721 ;; we're called during fontification, since font-lock refontifies the
4722 ;; current line for each change. Thus it's worthwhile to cache the
4723 ;; first match.
4724 ;;
4725 ;; `c-find-decl-syntactic-pos' is a syntactically relevant position in
4726 ;; the syntactic whitespace less or equal to some start position.
4727 ;; There's no cached value if it's nil.
4728 ;;
4729 ;; `c-find-decl-match-pos' is the match position if
4730 ;; `c-find-decl-prefix-search' matched before the syntactic whitespace
4731 ;; at `c-find-decl-syntactic-pos', or nil if there's no such match.
4732 (defvar c-find-decl-syntactic-pos nil)
4733 (make-variable-buffer-local 'c-find-decl-syntactic-pos)
4734 (defvar c-find-decl-match-pos nil)
4735 (make-variable-buffer-local 'c-find-decl-match-pos)
4736
4737 (defsubst c-invalidate-find-decl-cache (change-min-pos)
4738 (and c-find-decl-syntactic-pos
4739 (< change-min-pos c-find-decl-syntactic-pos)
4740 (setq c-find-decl-syntactic-pos nil)))
4741
4742 ; (defface c-debug-decl-spot-face
4743 ; '((t (:background "Turquoise")))
4744 ; "Debug face to mark the spots where `c-find-decl-spots' stopped.")
4745 ; (defface c-debug-decl-sws-face
4746 ; '((t (:background "Khaki")))
4747 ; "Debug face to mark the syntactic whitespace between the declaration
4748 ; spots and the preceding token end.")
4749
4750 (defmacro c-debug-put-decl-spot-faces (match-pos decl-pos)
4751 (when (facep 'c-debug-decl-spot-face)
4752 `(c-save-buffer-state ((match-pos ,match-pos) (decl-pos ,decl-pos))
4753 (c-debug-add-face (max match-pos (point-min)) decl-pos
4754 'c-debug-decl-sws-face)
4755 (c-debug-add-face decl-pos (min (1+ decl-pos) (point-max))
4756 'c-debug-decl-spot-face))))
4757 (defmacro c-debug-remove-decl-spot-faces (beg end)
4758 (when (facep 'c-debug-decl-spot-face)
4759 `(c-save-buffer-state ()
4760 (c-debug-remove-face ,beg ,end 'c-debug-decl-spot-face)
4761 (c-debug-remove-face ,beg ,end 'c-debug-decl-sws-face))))
4762
4763 (defmacro c-find-decl-prefix-search ()
4764 ;; Macro used inside `c-find-decl-spots'. It ought to be a defun,
4765 ;; but it contains lots of free variables that refer to things
4766 ;; inside `c-find-decl-spots'. The point is left at `cfd-match-pos'
4767 ;; if there is a match, otherwise at `cfd-limit'.
4768 ;;
4769 ;; The macro moves point forward to the next putative start of a declaration
4770 ;; or cfd-limit. This decl start is the next token after a "declaration
4771 ;; prefix". The declaration prefix is the earlier of `cfd-prop-match' and
4772 ;; `cfd-re-match'. `cfd-match-pos' is set to the decl prefix.
4773 ;;
4774 ;; This macro might do hidden buffer changes.
4775
4776 '(progn
4777 ;; Find the next property match position if we haven't got one already.
4778 (unless cfd-prop-match
4779 (save-excursion
4780 (while (progn
4781 (goto-char (next-single-property-change
4782 (point) 'c-type nil cfd-limit))
4783 (and (< (point) cfd-limit)
4784 (not (eq (c-get-char-property (1- (point)) 'c-type)
4785 'c-decl-end)))))
4786 (setq cfd-prop-match (point))))
4787
4788 ;; Find the next `c-decl-prefix-or-start-re' match if we haven't
4789 ;; got one already.
4790 (unless cfd-re-match
4791
4792 (if (> cfd-re-match-end (point))
4793 (goto-char cfd-re-match-end))
4794
4795 ;; Each time round, the next `while' moves forward over a pseudo match
4796 ;; of `c-decl-prefix-or-start-re' which is either inside a literal, or
4797 ;; is a ":" not preceded by "public", etc.. `cfd-re-match' and
4798 ;; `cfd-re-match-end' get set.
4799 (while
4800 (progn
4801 (setq cfd-re-match-end (re-search-forward c-decl-prefix-or-start-re
4802 cfd-limit 'move))
4803 (cond
4804 ((null cfd-re-match-end)
4805 ;; No match. Finish up and exit the loop.
4806 (setq cfd-re-match cfd-limit)
4807 nil)
4808 ((c-got-face-at
4809 (if (setq cfd-re-match (match-end 1))
4810 ;; Matched the end of a token preceding a decl spot.
4811 (progn
4812 (goto-char cfd-re-match)
4813 (1- cfd-re-match))
4814 ;; Matched a token that start a decl spot.
4815 (goto-char (match-beginning 0))
4816 (point))
4817 c-literal-faces)
4818 ;; Pseudo match inside a comment or string literal. Skip out
4819 ;; of comments and string literals.
4820 (while (progn
4821 (goto-char (next-single-property-change
4822 (point) 'face nil cfd-limit))
4823 (and (< (point) cfd-limit)
4824 (c-got-face-at (point) c-literal-faces))))
4825 t) ; Continue the loop over pseudo matches.
4826 ((and (match-string 1)
4827 (string= (match-string 1) ":")
4828 (save-excursion
4829 (or (/= (c-backward-token-2 2) 0) ; no search limit. :-(
4830 (not (looking-at c-decl-start-colon-kwd-re)))))
4831 ;; Found a ":" which isn't part of "public:", etc.
4832 t)
4833 (t nil)))) ;; Found a real match. Exit the pseudo-match loop.
4834
4835 ;; If our match was at the decl start, we have to back up over the
4836 ;; preceding syntactic ws to set `cfd-match-pos' and to catch
4837 ;; any decl spots in the syntactic ws.
4838 (unless cfd-re-match
4839 (c-backward-syntactic-ws)
4840 (setq cfd-re-match (point))))
4841
4842 ;; Choose whichever match is closer to the start.
4843 (if (< cfd-re-match cfd-prop-match)
4844 (setq cfd-match-pos cfd-re-match
4845 cfd-re-match nil)
4846 (setq cfd-match-pos cfd-prop-match
4847 cfd-prop-match nil))
4848
4849 (goto-char cfd-match-pos)
4850
4851 (when (< cfd-match-pos cfd-limit)
4852 ;; Skip forward past comments only so we don't skip macros.
4853 (c-forward-comments)
4854 ;; Set the position to continue at. We can avoid going over
4855 ;; the comments skipped above a second time, but it's possible
4856 ;; that the comment skipping has taken us past `cfd-prop-match'
4857 ;; since the property might be used inside comments.
4858 (setq cfd-continue-pos (if cfd-prop-match
4859 (min cfd-prop-match (point))
4860 (point))))))
4861
4862 (defun c-find-decl-spots (cfd-limit cfd-decl-re cfd-face-checklist cfd-fun)
4863 ;; Call CFD-FUN for each possible spot for a declaration, cast or
4864 ;; label from the point to CFD-LIMIT.
4865 ;;
4866 ;; CFD-FUN is called with point at the start of the spot. It's passed two
4867 ;; arguments: The first is the end position of the token preceding the spot,
4868 ;; or 0 for the implicit match at bob. The second is a flag that is t when
4869 ;; the match is inside a macro. Point should be moved forward by at least
4870 ;; one token.
4871 ;;
4872 ;; If CFD-FUN adds `c-decl-end' properties somewhere below the current spot,
4873 ;; it should return non-nil to ensure that the next search will find them.
4874 ;;
4875 ;; Such a spot is:
4876 ;; o The first token after bob.
4877 ;; o The first token after the end of submatch 1 in
4878 ;; `c-decl-prefix-or-start-re' when that submatch matches.
4879 ;; o The start of each `c-decl-prefix-or-start-re' match when
4880 ;; submatch 1 doesn't match.
4881 ;; o The first token after the end of each occurrence of the
4882 ;; `c-type' text property with the value `c-decl-end', provided
4883 ;; `c-type-decl-end-used' is set.
4884 ;;
4885 ;; Only a spot that match CFD-DECL-RE and whose face is in the
4886 ;; CFD-FACE-CHECKLIST list causes CFD-FUN to be called. The face
4887 ;; check is disabled if CFD-FACE-CHECKLIST is nil.
4888 ;;
4889 ;; If the match is inside a macro then the buffer is narrowed to the
4890 ;; end of it, so that CFD-FUN can investigate the following tokens
4891 ;; without matching something that begins inside a macro and ends
4892 ;; outside it. It's to avoid this work that the CFD-DECL-RE and
4893 ;; CFD-FACE-CHECKLIST checks exist.
4894 ;;
4895 ;; The spots are visited approximately in order from top to bottom.
4896 ;; It's however the positions where `c-decl-prefix-or-start-re'
4897 ;; matches and where `c-decl-end' properties are found that are in
4898 ;; order. Since the spots often are at the following token, they
4899 ;; might be visited out of order insofar as more spots are reported
4900 ;; later on within the syntactic whitespace between the match
4901 ;; positions and their spots.
4902 ;;
4903 ;; It's assumed that comments and strings are fontified in the
4904 ;; searched range.
4905 ;;
4906 ;; This is mainly used in fontification, and so has an elaborate
4907 ;; cache to handle repeated calls from the same start position; see
4908 ;; the variables above.
4909 ;;
4910 ;; All variables in this function begin with `cfd-' to avoid name
4911 ;; collision with the (dynamically bound) variables used in CFD-FUN.
4912 ;;
4913 ;; This function might do hidden buffer changes.
4914
4915 (let ((cfd-start-pos (point))
4916 (cfd-buffer-end (point-max))
4917 ;; The end of the token preceding the decl spot last found
4918 ;; with `c-decl-prefix-or-start-re'. `cfd-limit' if there's
4919 ;; no match.
4920 cfd-re-match
4921 ;; The end position of the last `c-decl-prefix-or-start-re'
4922 ;; match. If this is greater than `cfd-continue-pos', the
4923 ;; next regexp search is started here instead.
4924 (cfd-re-match-end (point-min))
4925 ;; The end of the last `c-decl-end' found by
4926 ;; `c-find-decl-prefix-search'. `cfd-limit' if there's no
4927 ;; match. If searching for the property isn't needed then we
4928 ;; disable it by setting it to `cfd-limit' directly.
4929 (cfd-prop-match (unless c-type-decl-end-used cfd-limit))
4930 ;; The end of the token preceding the decl spot last found by
4931 ;; `c-find-decl-prefix-search'. 0 for the implicit match at
4932 ;; bob. `cfd-limit' if there's no match. In other words,
4933 ;; this is the minimum of `cfd-re-match' and `cfd-prop-match'.
4934 (cfd-match-pos cfd-limit)
4935 ;; The position to continue searching at.
4936 cfd-continue-pos
4937 ;; The position of the last "real" token we've stopped at.
4938 ;; This can be greater than `cfd-continue-pos' when we get
4939 ;; hits inside macros or at `c-decl-end' positions inside
4940 ;; comments.
4941 (cfd-token-pos 0)
4942 ;; The end position of the last entered macro.
4943 (cfd-macro-end 0))
4944
4945 ;; Initialize by finding a syntactically relevant start position
4946 ;; before the point, and do the first `c-decl-prefix-or-start-re'
4947 ;; search unless we're at bob.
4948
4949 (let (start-in-literal start-in-macro syntactic-pos)
4950 ;; Must back up a bit since we look for the end of the previous
4951 ;; statement or declaration, which is earlier than the first
4952 ;; returned match.
4953
4954 (cond
4955 ;; First we need to move to a syntactically relevant position.
4956 ;; Begin by backing out of comment or string literals.
4957 ((and
4958 (when (c-got-face-at (point) c-literal-faces)
4959 ;; Try to use the faces to back up to the start of the
4960 ;; literal. FIXME: What if the point is on a declaration
4961 ;; inside a comment?
4962 (while (and (not (bobp))
4963 (c-got-face-at (1- (point)) c-literal-faces))
4964 (goto-char (previous-single-property-change
4965 (point) 'face nil (point-min))))
4966
4967 ;; XEmacs doesn't fontify the quotes surrounding string
4968 ;; literals.
4969 (and (featurep 'xemacs)
4970 (eq (get-text-property (point) 'face)
4971 'font-lock-string-face)
4972 (not (bobp))
4973 (progn (backward-char)
4974 (not (looking-at c-string-limit-regexp)))
4975 (forward-char))
4976
4977 ;; Don't trust the literal to contain only literal faces
4978 ;; (the font lock package might not have fontified the
4979 ;; start of it at all, for instance) so check that we have
4980 ;; arrived at something that looks like a start or else
4981 ;; resort to `c-literal-limits'.
4982 (unless (looking-at c-literal-start-regexp)
4983 (let ((range (c-literal-limits)))
4984 (if range (goto-char (car range)))))
4985
4986 (setq start-in-literal (point)))
4987
4988 ;; The start is in a literal. If the limit is in the same
4989 ;; one we don't have to find a syntactic position etc. We
4990 ;; only check that if the limit is at or before bonl to save
4991 ;; time; it covers the by far most common case when font-lock
4992 ;; refontifies the current line only.
4993 (<= cfd-limit (c-point 'bonl cfd-start-pos))
4994 (save-excursion
4995 (goto-char cfd-start-pos)
4996 (while (progn
4997 (goto-char (next-single-property-change
4998 (point) 'face nil cfd-limit))
4999 (and (< (point) cfd-limit)
5000 (c-got-face-at (point) c-literal-faces))))
5001 (= (point) cfd-limit)))
5002
5003 ;; Completely inside a literal. Set up variables to trig the
5004 ;; (< cfd-continue-pos cfd-start-pos) case below and it'll
5005 ;; find a suitable start position.
5006 (setq cfd-continue-pos start-in-literal))
5007
5008 ;; Check if the region might be completely inside a macro, to
5009 ;; optimize that like the completely-inside-literal above.
5010 ((save-excursion
5011 (and (= (forward-line 1) 0)
5012 (bolp) ; forward-line has funny behavior at eob.
5013 (>= (point) cfd-limit)
5014 (progn (backward-char)
5015 (eq (char-before) ?\\))))
5016 ;; (Maybe) completely inside a macro. Only need to trig the
5017 ;; (< cfd-continue-pos cfd-start-pos) case below to make it
5018 ;; set things up.
5019 (setq cfd-continue-pos (1- cfd-start-pos)
5020 start-in-macro t))
5021
5022 (t
5023 ;; Back out of any macro so we don't miss any declaration
5024 ;; that could follow after it.
5025 (when (c-beginning-of-macro)
5026 (setq start-in-macro t))
5027
5028 ;; Now we're at a proper syntactically relevant position so we
5029 ;; can use the cache. But first clear it if it applied
5030 ;; further down.
5031 (c-invalidate-find-decl-cache cfd-start-pos)
5032
5033 (setq syntactic-pos (point))
5034 (unless (eq syntactic-pos c-find-decl-syntactic-pos)
5035 ;; Don't have to do this if the cache is relevant here,
5036 ;; typically if the same line is refontified again. If
5037 ;; we're just some syntactic whitespace further down we can
5038 ;; still use the cache to limit the skipping.
5039 (c-backward-syntactic-ws c-find-decl-syntactic-pos))
5040
5041 ;; If we hit `c-find-decl-syntactic-pos' and
5042 ;; `c-find-decl-match-pos' is set then we install the cached
5043 ;; values. If we hit `c-find-decl-syntactic-pos' and
5044 ;; `c-find-decl-match-pos' is nil then we know there's no decl
5045 ;; prefix in the whitespace before `c-find-decl-syntactic-pos'
5046 ;; and so we can continue the search from this point. If we
5047 ;; didn't hit `c-find-decl-syntactic-pos' then we're now in
5048 ;; the right spot to begin searching anyway.
5049 (if (and (eq (point) c-find-decl-syntactic-pos)
5050 c-find-decl-match-pos)
5051 (setq cfd-match-pos c-find-decl-match-pos
5052 cfd-continue-pos syntactic-pos)
5053
5054 (setq c-find-decl-syntactic-pos syntactic-pos)
5055
5056 (when (if (bobp)
5057 ;; Always consider bob a match to get the first
5058 ;; declaration in the file. Do this separately instead of
5059 ;; letting `c-decl-prefix-or-start-re' match bob, so that
5060 ;; regexp always can consume at least one character to
5061 ;; ensure that we won't get stuck in an infinite loop.
5062 (setq cfd-re-match 0)
5063 (backward-char)
5064 (c-beginning-of-current-token)
5065 (< (point) cfd-limit))
5066 ;; Do an initial search now. In the bob case above it's
5067 ;; only done to search for a `c-decl-end' spot.
5068 (c-find-decl-prefix-search))
5069
5070 (setq c-find-decl-match-pos (and (< cfd-match-pos cfd-start-pos)
5071 cfd-match-pos)))))
5072
5073 ;; Advance `cfd-continue-pos' if it's before the start position.
5074 ;; The closest continue position that might have effect at or
5075 ;; after the start depends on what we started in. This also
5076 ;; finds a suitable start position in the special cases when the
5077 ;; region is completely within a literal or macro.
5078 (when (and cfd-continue-pos (< cfd-continue-pos cfd-start-pos))
5079
5080 (cond
5081 (start-in-macro
5082 ;; If we're in a macro then it's the closest preceding token
5083 ;; in the macro. Check this before `start-in-literal',
5084 ;; since if we're inside a literal in a macro, the preceding
5085 ;; token is earlier than any `c-decl-end' spot inside the
5086 ;; literal (comment).
5087 (goto-char (or start-in-literal cfd-start-pos))
5088 ;; The only syntactic ws in macros are comments.
5089 (c-backward-comments)
5090 (backward-char)
5091 (c-beginning-of-current-token))
5092
5093 (start-in-literal
5094 ;; If we're in a comment it can only be the closest
5095 ;; preceding `c-decl-end' position within that comment, if
5096 ;; any. Go back to the beginning of such a property so that
5097 ;; `c-find-decl-prefix-search' will find the end of it.
5098 ;; (Can't stop at the end and install it directly on
5099 ;; `cfd-prop-match' since that variable might be cleared
5100 ;; after `cfd-fun' below.)
5101 ;;
5102 ;; Note that if the literal is a string then the property
5103 ;; search will simply skip to the beginning of it right
5104 ;; away.
5105 (if (not c-type-decl-end-used)
5106 (goto-char start-in-literal)
5107 (goto-char cfd-start-pos)
5108 (while (progn
5109 (goto-char (previous-single-property-change
5110 (point) 'c-type nil start-in-literal))
5111 (and (> (point) start-in-literal)
5112 (not (eq (c-get-char-property (point) 'c-type)
5113 'c-decl-end))))))
5114
5115 (when (= (point) start-in-literal)
5116 ;; Didn't find any property inside the comment, so we can
5117 ;; skip it entirely. (This won't skip past a string, but
5118 ;; that'll be handled quickly by the next
5119 ;; `c-find-decl-prefix-search' anyway.)
5120 (c-forward-single-comment)
5121 (if (> (point) cfd-limit)
5122 (goto-char cfd-limit))))
5123
5124 (t
5125 ;; If we started in normal code, the only match that might
5126 ;; apply before the start is what we already got in
5127 ;; `cfd-match-pos' so we can continue at the start position.
5128 ;; (Note that we don't get here if the first match is below
5129 ;; it.)
5130 (goto-char cfd-start-pos)))
5131
5132 ;; Delete found matches if they are before our new continue
5133 ;; position, so that `c-find-decl-prefix-search' won't back up
5134 ;; to them later on.
5135 (setq cfd-continue-pos (point))
5136 (when (and cfd-re-match (< cfd-re-match cfd-continue-pos))
5137 (setq cfd-re-match nil))
5138 (when (and cfd-prop-match (< cfd-prop-match cfd-continue-pos))
5139 (setq cfd-prop-match nil)))
5140
5141 (if syntactic-pos
5142 ;; This is the normal case and we got a proper syntactic
5143 ;; position. If there's a match then it's always outside
5144 ;; macros and comments, so advance to the next token and set
5145 ;; `cfd-token-pos'. The loop below will later go back using
5146 ;; `cfd-continue-pos' to fix declarations inside the
5147 ;; syntactic ws.
5148 (when (and cfd-match-pos (< cfd-match-pos syntactic-pos))
5149 (goto-char syntactic-pos)
5150 (c-forward-syntactic-ws)
5151 (and cfd-continue-pos
5152 (< cfd-continue-pos (point))
5153 (setq cfd-token-pos (point))))
5154
5155 ;; Have one of the special cases when the region is completely
5156 ;; within a literal or macro. `cfd-continue-pos' is set to a
5157 ;; good start position for the search, so do it.
5158 (c-find-decl-prefix-search)))
5159
5160 ;; Now loop. Round what? (ACM, 2006/7/5). We already got the first match.
5161
5162 (while (progn
5163 (while (and
5164 (< cfd-match-pos cfd-limit)
5165
5166 (or
5167 ;; Kludge to filter out matches on the "<" that
5168 ;; aren't open parens, for the sake of languages
5169 ;; that got `c-recognize-<>-arglists' set.
5170 (and (eq (char-before cfd-match-pos) ?<)
5171 (not (c-get-char-property (1- cfd-match-pos)
5172 'syntax-table)))
5173
5174 ;; If `cfd-continue-pos' is less or equal to
5175 ;; `cfd-token-pos', we've got a hit inside a macro
5176 ;; that's in the syntactic whitespace before the last
5177 ;; "real" declaration we've checked. If they're equal
5178 ;; we've arrived at the declaration a second time, so
5179 ;; there's nothing to do.
5180 (= cfd-continue-pos cfd-token-pos)
5181
5182 (progn
5183 ;; If `cfd-continue-pos' is less than `cfd-token-pos'
5184 ;; we're still searching for declarations embedded in
5185 ;; the syntactic whitespace. In that case we need
5186 ;; only to skip comments and not macros, since they
5187 ;; can't be nested, and that's already been done in
5188 ;; `c-find-decl-prefix-search'.
5189 (when (> cfd-continue-pos cfd-token-pos)
5190 (c-forward-syntactic-ws)
5191 (setq cfd-token-pos (point)))
5192
5193 ;; Continue if the following token fails the
5194 ;; CFD-DECL-RE and CFD-FACE-CHECKLIST checks.
5195 (when (or (>= (point) cfd-limit)
5196 (not (looking-at cfd-decl-re))
5197 (and cfd-face-checklist
5198 (not (c-got-face-at
5199 (point) cfd-face-checklist))))
5200 (goto-char cfd-continue-pos)
5201 t)))
5202
5203 (< (point) cfd-limit))
5204 (c-find-decl-prefix-search))
5205
5206 (< (point) cfd-limit))
5207
5208 (when (and
5209 (>= (point) cfd-start-pos)
5210
5211 (progn
5212 ;; Narrow to the end of the macro if we got a hit inside
5213 ;; one, to avoid recognizing things that start inside the
5214 ;; macro and end outside it.
5215 (when (> cfd-match-pos cfd-macro-end)
5216 ;; Not in the same macro as in the previous round.
5217 (save-excursion
5218 (goto-char cfd-match-pos)
5219 (setq cfd-macro-end
5220 (if (save-excursion (and (c-beginning-of-macro)
5221 (< (point) cfd-match-pos)))
5222 (progn (c-end-of-macro)
5223 (point))
5224 0))))
5225
5226 (if (zerop cfd-macro-end)
5227 t
5228 (if (> cfd-macro-end (point))
5229 (progn (narrow-to-region (point-min) cfd-macro-end)
5230 t)
5231 ;; The matched token was the last thing in the macro,
5232 ;; so the whole match is bogus.
5233 (setq cfd-macro-end 0)
5234 nil))))
5235
5236 (c-debug-put-decl-spot-faces cfd-match-pos (point))
5237 (if (funcall cfd-fun cfd-match-pos (/= cfd-macro-end 0))
5238 (setq cfd-prop-match nil))
5239
5240 (when (/= cfd-macro-end 0)
5241 ;; Restore limits if we did macro narrowing above.
5242 (narrow-to-region (point-min) cfd-buffer-end)))
5243
5244 (goto-char cfd-continue-pos)
5245 (if (= cfd-continue-pos cfd-limit)
5246 (setq cfd-match-pos cfd-limit)
5247 (c-find-decl-prefix-search))))) ; Moves point, sets cfd-continue-pos,
5248 ; cfd-match-pos, etc.
5249
5250 \f
5251 ;; A cache for found types.
5252
5253 ;; Buffer local variable that contains an obarray with the types we've
5254 ;; found. If a declaration is recognized somewhere we record the
5255 ;; fully qualified identifier in it to recognize it as a type
5256 ;; elsewhere in the file too. This is not accurate since we do not
5257 ;; bother with the scoping rules of the languages, but in practice the
5258 ;; same name is seldom used as both a type and something else in a
5259 ;; file, and we only use this as a last resort in ambiguous cases (see
5260 ;; `c-forward-decl-or-cast-1').
5261 ;;
5262 ;; Not every type need be in this cache. However, things which have
5263 ;; ceased to be types must be removed from it.
5264 ;;
5265 ;; Template types in C++ are added here too but with the template
5266 ;; arglist replaced with "<>" in references or "<" for the one in the
5267 ;; primary type. E.g. the type "Foo<A,B>::Bar<C>" is stored as
5268 ;; "Foo<>::Bar<". This avoids storing very long strings (since C++
5269 ;; template specs can be fairly sized programs in themselves) and
5270 ;; improves the hit ratio (it's a type regardless of the template
5271 ;; args; it's just not the same type, but we're only interested in
5272 ;; recognizing types, not telling distinct types apart). Note that
5273 ;; template types in references are added here too; from the example
5274 ;; above there will also be an entry "Foo<".
5275 (defvar c-found-types nil)
5276 (make-variable-buffer-local 'c-found-types)
5277
5278 (defsubst c-clear-found-types ()
5279 ;; Clears `c-found-types'.
5280 (setq c-found-types (make-vector 53 0)))
5281
5282 (defun c-add-type (from to)
5283 ;; Add the given region as a type in `c-found-types'. If the region
5284 ;; doesn't match an existing type but there is a type which is equal
5285 ;; to the given one except that the last character is missing, then
5286 ;; the shorter type is removed. That's done to avoid adding all
5287 ;; prefixes of a type as it's being entered and font locked. This
5288 ;; doesn't cover cases like when characters are removed from a type
5289 ;; or added in the middle. We'd need the position of point when the
5290 ;; font locking is invoked to solve this well.
5291 ;;
5292 ;; This function might do hidden buffer changes.
5293 (let ((type (c-syntactic-content from to c-recognize-<>-arglists)))
5294 (unless (intern-soft type c-found-types)
5295 (unintern (substring type 0 -1) c-found-types)
5296 (intern type c-found-types))))
5297
5298 (defun c-unfind-type (name)
5299 ;; Remove the "NAME" from c-found-types, if present.
5300 (unintern name c-found-types))
5301
5302 (defsubst c-check-type (from to)
5303 ;; Return non-nil if the given region contains a type in
5304 ;; `c-found-types'.
5305 ;;
5306 ;; This function might do hidden buffer changes.
5307 (intern-soft (c-syntactic-content from to c-recognize-<>-arglists)
5308 c-found-types))
5309
5310 (defun c-list-found-types ()
5311 ;; Return all the types in `c-found-types' as a sorted list of
5312 ;; strings.
5313 (let (type-list)
5314 (mapatoms (lambda (type)
5315 (setq type-list (cons (symbol-name type)
5316 type-list)))
5317 c-found-types)
5318 (sort type-list 'string-lessp)))
5319
5320 ;; Shut up the byte compiler.
5321 (defvar c-maybe-stale-found-type)
5322
5323 (defun c-trim-found-types (beg end old-len)
5324 ;; An after change function which, in conjunction with the info in
5325 ;; c-maybe-stale-found-type (set in c-before-change), removes a type
5326 ;; from `c-found-types', should this type have become stale. For
5327 ;; example, this happens to "foo" when "foo \n bar();" becomes
5328 ;; "foo(); \n bar();". Such stale types, if not removed, foul up
5329 ;; the fontification.
5330 ;;
5331 ;; Have we, perhaps, added non-ws characters to the front/back of a found
5332 ;; type?
5333 (when (> end beg)
5334 (save-excursion
5335 (when (< end (point-max))
5336 (goto-char end)
5337 (if (and (c-beginning-of-current-token) ; only moves when we started in the middle
5338 (progn (goto-char end)
5339 (c-end-of-current-token)))
5340 (c-unfind-type (buffer-substring-no-properties
5341 end (point)))))
5342 (when (> beg (point-min))
5343 (goto-char beg)
5344 (if (and (c-end-of-current-token) ; only moves when we started in the middle
5345 (progn (goto-char beg)
5346 (c-beginning-of-current-token)))
5347 (c-unfind-type (buffer-substring-no-properties
5348 (point) beg))))))
5349
5350 (if c-maybe-stale-found-type ; e.g. (c-decl-id-start "foo" 97 107 " (* ooka) " "o")
5351 (cond
5352 ;; Changing the amount of (already existing) whitespace - don't do anything.
5353 ((and (c-partial-ws-p beg end)
5354 (or (= beg end) ; removal of WS
5355 (string-match "^[ \t\n\r\f\v]*$" (nth 5 c-maybe-stale-found-type)))))
5356
5357 ;; The syntactic relationship which defined a "found type" has been
5358 ;; destroyed.
5359 ((eq (car c-maybe-stale-found-type) 'c-decl-id-start)
5360 (c-unfind-type (cadr c-maybe-stale-found-type)))
5361 ;; ((eq (car c-maybe-stale-found-type) 'c-decl-type-start) FIXME!!!
5362 )))
5363
5364 \f
5365 ;; Setting and removing syntax properties on < and > in languages (C++
5366 ;; and Java) where they can be template/generic delimiters as well as
5367 ;; their normal meaning of "less/greater than".
5368
5369 ;; Normally, < and > have syntax 'punctuation'. When they are found to
5370 ;; be delimiters, they are marked as such with the category properties
5371 ;; c-<-as-paren-syntax, c->-as-paren-syntax respectively.
5372
5373 ;; STRATEGY:
5374 ;;
5375 ;; It is impossible to determine with certainty whether a <..> pair in
5376 ;; C++ is two comparison operators or is template delimiters, unless
5377 ;; one duplicates a lot of a C++ compiler. For example, the following
5378 ;; code fragment:
5379 ;;
5380 ;; foo (a < b, c > d) ;
5381 ;;
5382 ;; could be a function call with two integer parameters (each a
5383 ;; relational expression), or it could be a constructor for class foo
5384 ;; taking one parameter d of templated type "a < b, c >". They are
5385 ;; somewhat easier to distinguish in Java.
5386 ;;
5387 ;; The strategy now (2010-01) adopted is to mark and unmark < and
5388 ;; > IN MATCHING PAIRS ONLY. [Previously, they were marked
5389 ;; individually when their context so indicated. This gave rise to
5390 ;; intractable problems when one of a matching pair was deleted, or
5391 ;; pulled into a literal.]
5392 ;;
5393 ;; At each buffer change, the syntax-table properties are removed in a
5394 ;; before-change function and reapplied, when needed, in an
5395 ;; after-change function. It is far more important that the
5396 ;; properties get removed when they they are spurious than that they
5397 ;; be present when wanted.
5398 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
5399 (defun c-clear-<-pair-props (&optional pos)
5400 ;; POS (default point) is at a < character. If it is marked with
5401 ;; open paren syntax-table text property, remove the property,
5402 ;; together with the close paren property on the matching > (if
5403 ;; any).
5404 (save-excursion
5405 (if pos
5406 (goto-char pos)
5407 (setq pos (point)))
5408 (when (equal (c-get-char-property (point) 'syntax-table)
5409 c-<-as-paren-syntax)
5410 (with-syntax-table c-no-parens-syntax-table ; ignore unbalanced [,{,(,..
5411 (c-go-list-forward))
5412 (when (equal (c-get-char-property (1- (point)) 'syntax-table)
5413 c->-as-paren-syntax) ; should always be true.
5414 (c-clear-char-property (1- (point)) 'category))
5415 (c-clear-char-property pos 'category))))
5416
5417 (defun c-clear->-pair-props (&optional pos)
5418 ;; POS (default point) is at a > character. If it is marked with
5419 ;; close paren syntax-table property, remove the property, together
5420 ;; with the open paren property on the matching < (if any).
5421 (save-excursion
5422 (if pos
5423 (goto-char pos)
5424 (setq pos (point)))
5425 (when (equal (c-get-char-property (point) 'syntax-table)
5426 c->-as-paren-syntax)
5427 (with-syntax-table c-no-parens-syntax-table ; ignore unbalanced [,{,(,..
5428 (c-go-up-list-backward))
5429 (when (equal (c-get-char-property (point) 'syntax-table)
5430 c-<-as-paren-syntax) ; should always be true.
5431 (c-clear-char-property (point) 'category))
5432 (c-clear-char-property pos 'category))))
5433
5434 (defun c-clear-<>-pair-props (&optional pos)
5435 ;; POS (default point) is at a < or > character. If it has an
5436 ;; open/close paren syntax-table property, remove this property both
5437 ;; from the current character and its partner (which will also be
5438 ;; thusly marked).
5439 (cond
5440 ((eq (char-after) ?\<)
5441 (c-clear-<-pair-props pos))
5442 ((eq (char-after) ?\>)
5443 (c-clear->-pair-props pos))
5444 (t (c-benign-error
5445 "c-clear-<>-pair-props called from wrong position"))))
5446
5447 (defun c-clear-<-pair-props-if-match-after (lim &optional pos)
5448 ;; POS (default point) is at a < character. If it is both marked
5449 ;; with open/close paren syntax-table property, and has a matching >
5450 ;; (also marked) which is after LIM, remove the property both from
5451 ;; the current > and its partner. Return t when this happens, nil
5452 ;; when it doesn't.
5453 (save-excursion
5454 (if pos
5455 (goto-char pos)
5456 (setq pos (point)))
5457 (when (equal (c-get-char-property (point) 'syntax-table)
5458 c-<-as-paren-syntax)
5459 (with-syntax-table c-no-parens-syntax-table ; ignore unbalanced [,{,(,..
5460 (c-go-list-forward))
5461 (when (and (>= (point) lim)
5462 (equal (c-get-char-property (1- (point)) 'syntax-table)
5463 c->-as-paren-syntax)) ; should always be true.
5464 (c-unmark-<->-as-paren (1- (point)))
5465 (c-unmark-<->-as-paren pos))
5466 t)))
5467
5468 (defun c-clear->-pair-props-if-match-before (lim &optional pos)
5469 ;; POS (default point) is at a > character. If it is both marked
5470 ;; with open/close paren syntax-table property, and has a matching <
5471 ;; (also marked) which is before LIM, remove the property both from
5472 ;; the current < and its partner. Return t when this happens, nil
5473 ;; when it doesn't.
5474 (save-excursion
5475 (if pos
5476 (goto-char pos)
5477 (setq pos (point)))
5478 (when (equal (c-get-char-property (point) 'syntax-table)
5479 c->-as-paren-syntax)
5480 (with-syntax-table c-no-parens-syntax-table ; ignore unbalanced [,{,(,..
5481 (c-go-up-list-backward))
5482 (when (and (<= (point) lim)
5483 (equal (c-get-char-property (point) 'syntax-table)
5484 c-<-as-paren-syntax)) ; should always be true.
5485 (c-unmark-<->-as-paren (point))
5486 (c-unmark-<->-as-paren pos))
5487 t)))
5488
5489 ;; Set by c-common-init in cc-mode.el.
5490 (defvar c-new-BEG)
5491 (defvar c-new-END)
5492
5493 (defun c-before-change-check-<>-operators (beg end)
5494 ;; Unmark certain pairs of "< .... >" which are currently marked as
5495 ;; template/generic delimiters. (This marking is via syntax-table
5496 ;; text properties).
5497 ;;
5498 ;; These pairs are those which are in the current "statement" (i.e.,
5499 ;; the region between the {, }, or ; before BEG and the one after
5500 ;; END), and which enclose any part of the interval (BEG END).
5501 ;;
5502 ;; Note that in C++ (?and Java), template/generic parens cannot
5503 ;; enclose a brace or semicolon, so we use these as bounds on the
5504 ;; region we must work on.
5505 ;;
5506 ;; This function is called from before-change-functions (via
5507 ;; c-get-state-before-change-functions). Thus the buffer is widened,
5508 ;; and point is undefined, both at entry and exit.
5509 ;;
5510 ;; FIXME!!! This routine ignores the possibility of macros entirely.
5511 ;; 2010-01-29.
5512 (save-excursion
5513 (let ((beg-lit-limits (progn (goto-char beg) (c-literal-limits)))
5514 (end-lit-limits (progn (goto-char end) (c-literal-limits)))
5515 new-beg new-end need-new-beg need-new-end)
5516 ;; Locate the barrier before the changed region
5517 (goto-char (if beg-lit-limits (car beg-lit-limits) beg))
5518 (c-syntactic-skip-backward "^;{}" (c-determine-limit 512))
5519 (setq new-beg (point))
5520
5521 ;; Remove the syntax-table properties from each pertinent <...> pair.
5522 ;; Firsly, the ones with the < before beg and > after beg.
5523 (while (c-search-forward-char-property 'category 'c-<-as-paren-syntax beg)
5524 (if (c-clear-<-pair-props-if-match-after beg (1- (point)))
5525 (setq need-new-beg t)))
5526
5527 ;; Locate the barrier after END.
5528 (goto-char (if end-lit-limits (cdr end-lit-limits) end))
5529 (c-syntactic-re-search-forward "[;{}]" (c-determine-+ve-limit 512) 'end)
5530 (setq new-end (point))
5531
5532 ;; Remove syntax-table properties from the remaining pertinent <...>
5533 ;; pairs, those with a > after end and < before end.
5534 (while (c-search-backward-char-property 'category 'c->-as-paren-syntax end)
5535 (if (c-clear->-pair-props-if-match-before end)
5536 (setq need-new-end t)))
5537
5538 ;; Extend the fontification region, if needed.
5539 (when need-new-beg
5540 (goto-char new-beg)
5541 (c-forward-syntactic-ws)
5542 (and (< (point) c-new-BEG) (setq c-new-BEG (point))))
5543
5544 (when need-new-end
5545 (and (> new-end c-new-END) (setq c-new-END new-end))))))
5546
5547
5548
5549 (defun c-after-change-check-<>-operators (beg end)
5550 ;; This is called from `after-change-functions' when
5551 ;; c-recognize-<>-arglists' is set. It ensures that no "<" or ">"
5552 ;; chars with paren syntax become part of another operator like "<<"
5553 ;; or ">=".
5554 ;;
5555 ;; This function might do hidden buffer changes.
5556
5557 (save-excursion
5558 (goto-char beg)
5559 (when (or (looking-at "[<>]")
5560 (< (skip-chars-backward "<>") 0))
5561
5562 (goto-char beg)
5563 (c-beginning-of-current-token)
5564 (when (and (< (point) beg)
5565 (looking-at c-<>-multichar-token-regexp)
5566 (< beg (setq beg (match-end 0))))
5567 (while (progn (skip-chars-forward "^<>" beg)
5568 (< (point) beg))
5569 (c-clear-<>-pair-props)
5570 (forward-char))))
5571
5572 (when (< beg end)
5573 (goto-char end)
5574 (when (or (looking-at "[<>]")
5575 (< (skip-chars-backward "<>") 0))
5576
5577 (goto-char end)
5578 (c-beginning-of-current-token)
5579 (when (and (< (point) end)
5580 (looking-at c-<>-multichar-token-regexp)
5581 (< end (setq end (match-end 0))))
5582 (while (progn (skip-chars-forward "^<>" end)
5583 (< (point) end))
5584 (c-clear-<>-pair-props)
5585 (forward-char)))))))
5586
5587
5588 \f
5589 ;; Handling of small scale constructs like types and names.
5590
5591 ;; Dynamically bound variable that instructs `c-forward-type' to also
5592 ;; treat possible types (i.e. those that it normally returns 'maybe or
5593 ;; 'found for) as actual types (and always return 'found for them).
5594 ;; This means that it records them in `c-record-type-identifiers' if
5595 ;; that is set, and that it adds them to `c-found-types'.
5596 (defvar c-promote-possible-types nil)
5597
5598 ;; Dynamically bound variable that instructs `c-forward-<>-arglist' to
5599 ;; mark up successfully parsed arglists with paren syntax properties on
5600 ;; the surrounding angle brackets and with `c-<>-arg-sep' in the
5601 ;; `c-type' property of each argument separating comma.
5602 ;;
5603 ;; Setting this variable also makes `c-forward-<>-arglist' recurse into
5604 ;; all arglists for side effects (i.e. recording types), otherwise it
5605 ;; exploits any existing paren syntax properties to quickly jump to the
5606 ;; end of already parsed arglists.
5607 ;;
5608 ;; Marking up the arglists is not the default since doing that correctly
5609 ;; depends on a proper value for `c-restricted-<>-arglists'.
5610 (defvar c-parse-and-markup-<>-arglists nil)
5611
5612 ;; Dynamically bound variable that instructs `c-forward-<>-arglist' to
5613 ;; not accept arglists that contain binary operators.
5614 ;;
5615 ;; This is primarily used to handle C++ template arglists. C++
5616 ;; disambiguates them by checking whether the preceding name is a
5617 ;; template or not. We can't do that, so we assume it is a template
5618 ;; if it can be parsed as one. That usually works well since
5619 ;; comparison expressions on the forms "a < b > c" or "a < b, c > d"
5620 ;; in almost all cases would be pointless.
5621 ;;
5622 ;; However, in function arglists, e.g. in "foo (a < b, c > d)", we
5623 ;; should let the comma separate the function arguments instead. And
5624 ;; in a context where the value of the expression is taken, e.g. in
5625 ;; "if (a < b || c > d)", it's probably not a template.
5626 (defvar c-restricted-<>-arglists nil)
5627
5628 ;; Dynamically bound variables that instructs
5629 ;; `c-forward-keyword-clause', `c-forward-<>-arglist',
5630 ;; `c-forward-name', `c-forward-type', `c-forward-decl-or-cast-1', and
5631 ;; `c-forward-label' to record the ranges of all the type and
5632 ;; reference identifiers they encounter. They will build lists on
5633 ;; these variables where each element is a cons of the buffer
5634 ;; positions surrounding each identifier. This recording is only
5635 ;; activated when `c-record-type-identifiers' is non-nil.
5636 ;;
5637 ;; All known types that can't be identifiers are recorded, and also
5638 ;; other possible types if `c-promote-possible-types' is set.
5639 ;; Recording is however disabled inside angle bracket arglists that
5640 ;; are encountered inside names and other angle bracket arglists.
5641 ;; Such occurrences are taken care of by `c-font-lock-<>-arglists'
5642 ;; instead.
5643 ;;
5644 ;; Only the names in C++ template style references (e.g. "tmpl" in
5645 ;; "tmpl<a,b>::foo") are recorded as references, other references
5646 ;; aren't handled here.
5647 ;;
5648 ;; `c-forward-label' records the label identifier(s) on
5649 ;; `c-record-ref-identifiers'.
5650 (defvar c-record-type-identifiers nil)
5651 (defvar c-record-ref-identifiers nil)
5652
5653 ;; This variable will receive a cons cell of the range of the last
5654 ;; single identifier symbol stepped over by `c-forward-name' if it's
5655 ;; successful. This is the range that should be put on one of the
5656 ;; record lists above by the caller. It's assigned nil if there's no
5657 ;; such symbol in the name.
5658 (defvar c-last-identifier-range nil)
5659
5660 (defmacro c-record-type-id (range)
5661 (if (eq (car-safe range) 'cons)
5662 ;; Always true.
5663 `(setq c-record-type-identifiers
5664 (cons ,range c-record-type-identifiers))
5665 `(let ((range ,range))
5666 (if range
5667 (setq c-record-type-identifiers
5668 (cons range c-record-type-identifiers))))))
5669
5670 (defmacro c-record-ref-id (range)
5671 (if (eq (car-safe range) 'cons)
5672 ;; Always true.
5673 `(setq c-record-ref-identifiers
5674 (cons ,range c-record-ref-identifiers))
5675 `(let ((range ,range))
5676 (if range
5677 (setq c-record-ref-identifiers
5678 (cons range c-record-ref-identifiers))))))
5679
5680 ;; Dynamically bound variable that instructs `c-forward-type' to
5681 ;; record the ranges of types that only are found. Behaves otherwise
5682 ;; like `c-record-type-identifiers'.
5683 (defvar c-record-found-types nil)
5684
5685 (defmacro c-forward-keyword-prefixed-id (type)
5686 ;; Used internally in `c-forward-keyword-clause' to move forward
5687 ;; over a type (if TYPE is 'type) or a name (otherwise) which
5688 ;; possibly is prefixed by keywords and their associated clauses.
5689 ;; Try with a type/name first to not trip up on those that begin
5690 ;; with a keyword. Return t if a known or found type is moved
5691 ;; over. The point is clobbered if nil is returned. If range
5692 ;; recording is enabled, the identifier is recorded on as a type
5693 ;; if TYPE is 'type or as a reference if TYPE is 'ref.
5694 ;;
5695 ;; This macro might do hidden buffer changes.
5696 `(let (res)
5697 (while (if (setq res ,(if (eq type 'type)
5698 `(c-forward-type)
5699 `(c-forward-name)))
5700 nil
5701 (and (looking-at c-keywords-regexp)
5702 (c-forward-keyword-clause 1))))
5703 (when (memq res '(t known found prefix))
5704 ,(when (eq type 'ref)
5705 `(when c-record-type-identifiers
5706 (c-record-ref-id c-last-identifier-range)))
5707 t)))
5708
5709 (defmacro c-forward-id-comma-list (type update-safe-pos)
5710 ;; Used internally in `c-forward-keyword-clause' to move forward
5711 ;; over a comma separated list of types or names using
5712 ;; `c-forward-keyword-prefixed-id'.
5713 ;;
5714 ;; This macro might do hidden buffer changes.
5715 `(while (and (progn
5716 ,(when update-safe-pos
5717 `(setq safe-pos (point)))
5718 (eq (char-after) ?,))
5719 (progn
5720 (forward-char)
5721 (c-forward-syntactic-ws)
5722 (c-forward-keyword-prefixed-id ,type)))))
5723
5724 (defun c-forward-keyword-clause (match)
5725 ;; Submatch MATCH in the current match data is assumed to surround a
5726 ;; token. If it's a keyword, move over it and any immediately
5727 ;; following clauses associated with it, stopping at the start of
5728 ;; the next token. t is returned in that case, otherwise the point
5729 ;; stays and nil is returned. The kind of clauses that are
5730 ;; recognized are those specified by `c-type-list-kwds',
5731 ;; `c-ref-list-kwds', `c-colon-type-list-kwds',
5732 ;; `c-paren-nontype-kwds', `c-paren-type-kwds', `c-<>-type-kwds',
5733 ;; and `c-<>-arglist-kwds'.
5734 ;;
5735 ;; This function records identifier ranges on
5736 ;; `c-record-type-identifiers' and `c-record-ref-identifiers' if
5737 ;; `c-record-type-identifiers' is non-nil.
5738 ;;
5739 ;; Note that for `c-colon-type-list-kwds', which doesn't necessary
5740 ;; apply directly after the keyword, the type list is moved over
5741 ;; only when there is no unaccounted token before it (i.e. a token
5742 ;; that isn't moved over due to some other keyword list). The
5743 ;; identifier ranges in the list are still recorded if that should
5744 ;; be done, though.
5745 ;;
5746 ;; This function might do hidden buffer changes.
5747
5748 (let ((kwd-sym (c-keyword-sym (match-string match))) safe-pos pos
5749 ;; The call to `c-forward-<>-arglist' below is made after
5750 ;; `c-<>-sexp-kwds' keywords, so we're certain they actually
5751 ;; are angle bracket arglists and `c-restricted-<>-arglists'
5752 ;; should therefore be nil.
5753 (c-parse-and-markup-<>-arglists t)
5754 c-restricted-<>-arglists)
5755
5756 (when kwd-sym
5757 (goto-char (match-end match))
5758 (c-forward-syntactic-ws)
5759 (setq safe-pos (point))
5760
5761 (cond
5762 ((and (c-keyword-member kwd-sym 'c-type-list-kwds)
5763 (c-forward-keyword-prefixed-id type))
5764 ;; There's a type directly after a keyword in `c-type-list-kwds'.
5765 (c-forward-id-comma-list type t))
5766
5767 ((and (c-keyword-member kwd-sym 'c-ref-list-kwds)
5768 (c-forward-keyword-prefixed-id ref))
5769 ;; There's a name directly after a keyword in `c-ref-list-kwds'.
5770 (c-forward-id-comma-list ref t))
5771
5772 ((and (c-keyword-member kwd-sym 'c-paren-any-kwds)
5773 (eq (char-after) ?\())
5774 ;; There's an open paren after a keyword in `c-paren-any-kwds'.
5775
5776 (forward-char)
5777 (when (and (setq pos (c-up-list-forward))
5778 (eq (char-before pos) ?\)))
5779 (when (and c-record-type-identifiers
5780 (c-keyword-member kwd-sym 'c-paren-type-kwds))
5781 ;; Use `c-forward-type' on every identifier we can find
5782 ;; inside the paren, to record the types.
5783 (while (c-syntactic-re-search-forward c-symbol-start pos t)
5784 (goto-char (match-beginning 0))
5785 (unless (c-forward-type)
5786 (looking-at c-symbol-key) ; Always matches.
5787 (goto-char (match-end 0)))))
5788
5789 (goto-char pos)
5790 (c-forward-syntactic-ws)
5791 (setq safe-pos (point))))
5792
5793 ((and (c-keyword-member kwd-sym 'c-<>-sexp-kwds)
5794 (eq (char-after) ?<)
5795 (c-forward-<>-arglist (c-keyword-member kwd-sym 'c-<>-type-kwds)))
5796 (c-forward-syntactic-ws)
5797 (setq safe-pos (point)))
5798
5799 ((and (c-keyword-member kwd-sym 'c-nonsymbol-sexp-kwds)
5800 (not (looking-at c-symbol-start))
5801 (c-safe (c-forward-sexp) t))
5802 (c-forward-syntactic-ws)
5803 (setq safe-pos (point))))
5804
5805 (when (c-keyword-member kwd-sym 'c-colon-type-list-kwds)
5806 (if (eq (char-after) ?:)
5807 ;; If we are at the colon already, we move over the type
5808 ;; list after it.
5809 (progn
5810 (forward-char)
5811 (c-forward-syntactic-ws)
5812 (when (c-forward-keyword-prefixed-id type)
5813 (c-forward-id-comma-list type t)))
5814 ;; Not at the colon, so stop here. But the identifier
5815 ;; ranges in the type list later on should still be
5816 ;; recorded.
5817 (and c-record-type-identifiers
5818 (progn
5819 ;; If a keyword matched both one of the types above and
5820 ;; this one, we match `c-colon-type-list-re' after the
5821 ;; clause matched above.
5822 (goto-char safe-pos)
5823 (looking-at c-colon-type-list-re))
5824 (progn
5825 (goto-char (match-end 0))
5826 (c-forward-syntactic-ws)
5827 (c-forward-keyword-prefixed-id type))
5828 ;; There's a type after the `c-colon-type-list-re' match
5829 ;; after a keyword in `c-colon-type-list-kwds'.
5830 (c-forward-id-comma-list type nil))))
5831
5832 (goto-char safe-pos)
5833 t)))
5834
5835 ;; cc-mode requires cc-fonts.
5836 (declare-function c-fontify-recorded-types-and-refs "cc-fonts" ())
5837
5838 (defun c-forward-<>-arglist (all-types)
5839 ;; The point is assumed to be at a "<". Try to treat it as the open
5840 ;; paren of an angle bracket arglist and move forward to the
5841 ;; corresponding ">". If successful, the point is left after the
5842 ;; ">" and t is returned, otherwise the point isn't moved and nil is
5843 ;; returned. If ALL-TYPES is t then all encountered arguments in
5844 ;; the arglist that might be types are treated as found types.
5845 ;;
5846 ;; The variable `c-parse-and-markup-<>-arglists' controls how this
5847 ;; function handles text properties on the angle brackets and argument
5848 ;; separating commas.
5849 ;;
5850 ;; `c-restricted-<>-arglists' controls how lenient the template
5851 ;; arglist recognition should be.
5852 ;;
5853 ;; This function records identifier ranges on
5854 ;; `c-record-type-identifiers' and `c-record-ref-identifiers' if
5855 ;; `c-record-type-identifiers' is non-nil.
5856 ;;
5857 ;; This function might do hidden buffer changes.
5858
5859 (let ((start (point))
5860 ;; If `c-record-type-identifiers' is set then activate
5861 ;; recording of any found types that constitute an argument in
5862 ;; the arglist.
5863 (c-record-found-types (if c-record-type-identifiers t)))
5864 (if (catch 'angle-bracket-arglist-escape
5865 (setq c-record-found-types
5866 (c-forward-<>-arglist-recur all-types)))
5867 (progn
5868 (when (consp c-record-found-types)
5869 (setq c-record-type-identifiers
5870 ;; `nconc' doesn't mind that the tail of
5871 ;; `c-record-found-types' is t.
5872 (nconc c-record-found-types c-record-type-identifiers)))
5873 (if (c-major-mode-is 'java-mode) (c-fontify-recorded-types-and-refs))
5874 t)
5875
5876 (goto-char start)
5877 nil)))
5878
5879 (defun c-forward-<>-arglist-recur (all-types)
5880 ;; Recursive part of `c-forward-<>-arglist'.
5881 ;;
5882 ;; This function might do hidden buffer changes.
5883
5884 (let ((start (point)) res pos tmp
5885 ;; Cover this so that any recorded found type ranges are
5886 ;; automatically lost if it turns out to not be an angle
5887 ;; bracket arglist. It's propagated through the return value
5888 ;; on successful completion.
5889 (c-record-found-types c-record-found-types)
5890 ;; List that collects the positions after the argument
5891 ;; separating ',' in the arglist.
5892 arg-start-pos)
5893 ;; If the '<' has paren open syntax then we've marked it as an angle
5894 ;; bracket arglist before, so skip to the end.
5895 (if (and (not c-parse-and-markup-<>-arglists)
5896 (c-get-char-property (point) 'syntax-table))
5897
5898 (progn
5899 (forward-char)
5900 (if (and (c-go-up-list-forward)
5901 (eq (char-before) ?>))
5902 t
5903 ;; Got unmatched paren angle brackets. We don't clear the paren
5904 ;; syntax properties and retry, on the basis that it's very
5905 ;; unlikely that paren angle brackets become operators by code
5906 ;; manipulation. It's far more likely that it doesn't match due
5907 ;; to narrowing or some temporary change.
5908 (goto-char start)
5909 nil))
5910
5911 (forward-char) ; Forward over the opening '<'.
5912
5913 (unless (looking-at c-<-op-cont-regexp)
5914 ;; go forward one non-alphanumeric character (group) per iteration of
5915 ;; this loop.
5916 (while (and
5917 (progn
5918 (c-forward-syntactic-ws)
5919 (let ((orig-record-found-types c-record-found-types))
5920 (when (or (and c-record-type-identifiers all-types)
5921 (c-major-mode-is 'java-mode))
5922 ;; All encountered identifiers are types, so set the
5923 ;; promote flag and parse the type.
5924 (progn
5925 (c-forward-syntactic-ws)
5926 (if (looking-at "\\?")
5927 (forward-char)
5928 (when (looking-at c-identifier-start)
5929 (let ((c-promote-possible-types t)
5930 (c-record-found-types t))
5931 (c-forward-type))))
5932
5933 (c-forward-syntactic-ws)
5934
5935 (when (or (looking-at "extends")
5936 (looking-at "super"))
5937 (forward-word)
5938 (c-forward-syntactic-ws)
5939 (let ((c-promote-possible-types t)
5940 (c-record-found-types t))
5941 (c-forward-type)
5942 (c-forward-syntactic-ws))))))
5943
5944 (setq pos (point)) ; e.g. first token inside the '<'
5945
5946 ;; Note: These regexps exploit the match order in \| so
5947 ;; that "<>" is matched by "<" rather than "[^>:-]>".
5948 (c-syntactic-re-search-forward
5949 ;; Stop on ',', '|', '&', '+' and '-' to catch
5950 ;; common binary operators that could be between
5951 ;; two comparison expressions "a<b" and "c>d".
5952 "[<;{},|+&-]\\|[>)]"
5953 nil t t))
5954
5955 (cond
5956 ((eq (char-before) ?>)
5957 ;; Either an operator starting with '>' or the end of
5958 ;; the angle bracket arglist.
5959
5960 (if (looking-at c->-op-cont-regexp)
5961 (progn
5962 (goto-char (match-end 0))
5963 t) ; Continue the loop.
5964
5965 ;; The angle bracket arglist is finished.
5966 (when c-parse-and-markup-<>-arglists
5967 (while arg-start-pos
5968 (c-put-c-type-property (1- (car arg-start-pos))
5969 'c-<>-arg-sep)
5970 (setq arg-start-pos (cdr arg-start-pos)))
5971 (c-mark-<-as-paren start)
5972 (c-mark->-as-paren (1- (point))))
5973 (setq res t)
5974 nil)) ; Exit the loop.
5975
5976 ((eq (char-before) ?<)
5977 ;; Either an operator starting with '<' or a nested arglist.
5978 (setq pos (point))
5979 (let (id-start id-end subres keyword-match)
5980 (cond
5981 ;; The '<' begins a multi-char operator.
5982 ((looking-at c-<-op-cont-regexp)
5983 (setq tmp (match-end 0))
5984 (goto-char (match-end 0)))
5985 ;; We're at a nested <.....>
5986 ((progn
5987 (setq tmp pos)
5988 (backward-char) ; to the '<'
5989 (and
5990 (save-excursion
5991 ;; There's always an identifier before an angle
5992 ;; bracket arglist, or a keyword in `c-<>-type-kwds'
5993 ;; or `c-<>-arglist-kwds'.
5994 (c-backward-syntactic-ws)
5995 (setq id-end (point))
5996 (c-simple-skip-symbol-backward)
5997 (when (or (setq keyword-match
5998 (looking-at c-opt-<>-sexp-key))
5999 (not (looking-at c-keywords-regexp)))
6000 (setq id-start (point))))
6001 (setq subres
6002 (let ((c-promote-possible-types t)
6003 (c-record-found-types t))
6004 (c-forward-<>-arglist-recur
6005 (and keyword-match
6006 (c-keyword-member
6007 (c-keyword-sym (match-string 1))
6008 'c-<>-type-kwds)))))))
6009
6010 ;; It was an angle bracket arglist.
6011 (setq c-record-found-types subres)
6012
6013 ;; Record the identifier before the template as a type
6014 ;; or reference depending on whether the arglist is last
6015 ;; in a qualified identifier.
6016 (when (and c-record-type-identifiers
6017 (not keyword-match))
6018 (if (and c-opt-identifier-concat-key
6019 (progn
6020 (c-forward-syntactic-ws)
6021 (looking-at c-opt-identifier-concat-key)))
6022 (c-record-ref-id (cons id-start id-end))
6023 (c-record-type-id (cons id-start id-end)))))
6024
6025 ;; At a "less than" operator.
6026 (t
6027 (forward-char)
6028 )))
6029 t) ; carry on looping.
6030
6031 ((and (not c-restricted-<>-arglists)
6032 (or (and (eq (char-before) ?&)
6033 (not (eq (char-after) ?&)))
6034 (eq (char-before) ?,)))
6035 ;; Just another argument. Record the position. The
6036 ;; type check stuff that made us stop at it is at
6037 ;; the top of the loop.
6038 (setq arg-start-pos (cons (point) arg-start-pos)))
6039
6040 (t
6041 ;; Got a character that can't be in an angle bracket
6042 ;; arglist argument. Abort using `throw', since
6043 ;; it's useless to try to find a surrounding arglist
6044 ;; if we're nested.
6045 (throw 'angle-bracket-arglist-escape nil))))))
6046 (if res
6047 (or c-record-found-types t)))))
6048
6049 (defun c-backward-<>-arglist (all-types &optional limit)
6050 ;; The point is assumed to be directly after a ">". Try to treat it
6051 ;; as the close paren of an angle bracket arglist and move back to
6052 ;; the corresponding "<". If successful, the point is left at
6053 ;; the "<" and t is returned, otherwise the point isn't moved and
6054 ;; nil is returned. ALL-TYPES is passed on to
6055 ;; `c-forward-<>-arglist'.
6056 ;;
6057 ;; If the optional LIMIT is given, it bounds the backward search.
6058 ;; It's then assumed to be at a syntactically relevant position.
6059 ;;
6060 ;; This is a wrapper around `c-forward-<>-arglist'. See that
6061 ;; function for more details.
6062
6063 (let ((start (point)))
6064 (backward-char)
6065 (if (and (not c-parse-and-markup-<>-arglists)
6066 (c-get-char-property (point) 'syntax-table))
6067
6068 (if (and (c-go-up-list-backward)
6069 (eq (char-after) ?<))
6070 t
6071 ;; See corresponding note in `c-forward-<>-arglist'.
6072 (goto-char start)
6073 nil)
6074
6075 (while (progn
6076 (c-syntactic-skip-backward "^<;{}" limit t)
6077
6078 (and
6079 (if (eq (char-before) ?<)
6080 t
6081 ;; Stopped at bob or a char that isn't allowed in an
6082 ;; arglist, so we've failed.
6083 (goto-char start)
6084 nil)
6085
6086 (if (> (point)
6087 (progn (c-beginning-of-current-token)
6088 (point)))
6089 ;; If we moved then the "<" was part of some
6090 ;; multicharacter token.
6091 t
6092
6093 (backward-char)
6094 (let ((beg-pos (point)))
6095 (if (c-forward-<>-arglist all-types)
6096 (cond ((= (point) start)
6097 ;; Matched the arglist. Break the while.
6098 (goto-char beg-pos)
6099 nil)
6100 ((> (point) start)
6101 ;; We started from a non-paren ">" inside an
6102 ;; arglist.
6103 (goto-char start)
6104 nil)
6105 (t
6106 ;; Matched a shorter arglist. Can be a nested
6107 ;; one so continue looking.
6108 (goto-char beg-pos)
6109 t))
6110 t))))))
6111
6112 (/= (point) start))))
6113
6114 (defun c-forward-name ()
6115 ;; Move forward over a complete name if at the beginning of one,
6116 ;; stopping at the next following token. A keyword, as such,
6117 ;; doesn't count as a name. If the point is not at something that
6118 ;; is recognized as a name then it stays put.
6119 ;;
6120 ;; A name could be something as simple as "foo" in C or something as
6121 ;; complex as "X<Y<class A<int>::B, BIT_MAX >> b>, ::operator<> ::
6122 ;; Z<(a>b)> :: operator const X<&foo>::T Q::G<unsigned short
6123 ;; int>::*volatile const" in C++ (this function is actually little
6124 ;; more than a `looking-at' call in all modes except those that,
6125 ;; like C++, have `c-recognize-<>-arglists' set).
6126 ;;
6127 ;; Return
6128 ;; o - nil if no name is found;
6129 ;; o - 'template if it's an identifier ending with an angle bracket
6130 ;; arglist;
6131 ;; o - 'operator of it's an operator identifier;
6132 ;; o - t if it's some other kind of name.
6133 ;;
6134 ;; This function records identifier ranges on
6135 ;; `c-record-type-identifiers' and `c-record-ref-identifiers' if
6136 ;; `c-record-type-identifiers' is non-nil.
6137 ;;
6138 ;; This function might do hidden buffer changes.
6139
6140 (let ((pos (point)) (start (point)) res id-start id-end
6141 ;; Turn off `c-promote-possible-types' here since we might
6142 ;; call `c-forward-<>-arglist' and we don't want it to promote
6143 ;; every suspect thing in the arglist to a type. We're
6144 ;; typically called from `c-forward-type' in this case, and
6145 ;; the caller only wants the top level type that it finds to
6146 ;; be promoted.
6147 c-promote-possible-types)
6148 (while
6149 (and
6150 (looking-at c-identifier-key)
6151
6152 (progn
6153 ;; Check for keyword. We go to the last symbol in
6154 ;; `c-identifier-key' first.
6155 (goto-char (setq id-end (match-end 0)))
6156 (c-simple-skip-symbol-backward)
6157 (setq id-start (point))
6158
6159 (if (looking-at c-keywords-regexp)
6160 (when (and (c-major-mode-is 'c++-mode)
6161 (looking-at
6162 (cc-eval-when-compile
6163 (concat "\\(operator\\|\\(template\\)\\)"
6164 "\\(" (c-lang-const c-nonsymbol-key c++)
6165 "\\|$\\)")))
6166 (if (match-beginning 2)
6167 ;; "template" is only valid inside an
6168 ;; identifier if preceded by "::".
6169 (save-excursion
6170 (c-backward-syntactic-ws)
6171 (and (c-safe (backward-char 2) t)
6172 (looking-at "::")))
6173 t))
6174
6175 ;; Handle a C++ operator or template identifier.
6176 (goto-char id-end)
6177 (c-forward-syntactic-ws)
6178 (cond ((eq (char-before id-end) ?e)
6179 ;; Got "... ::template".
6180 (let ((subres (c-forward-name)))
6181 (when subres
6182 (setq pos (point)
6183 res subres))))
6184
6185 ((looking-at c-identifier-start)
6186 ;; Got a cast operator.
6187 (when (c-forward-type)
6188 (setq pos (point)
6189 res 'operator)
6190 ;; Now we should match a sequence of either
6191 ;; '*', '&' or a name followed by ":: *",
6192 ;; where each can be followed by a sequence
6193 ;; of `c-opt-type-modifier-key'.
6194 (while (cond ((looking-at "[*&]")
6195 (goto-char (match-end 0))
6196 t)
6197 ((looking-at c-identifier-start)
6198 (and (c-forward-name)
6199 (looking-at "::")
6200 (progn
6201 (goto-char (match-end 0))
6202 (c-forward-syntactic-ws)
6203 (eq (char-after) ?*))
6204 (progn
6205 (forward-char)
6206 t))))
6207 (while (progn
6208 (c-forward-syntactic-ws)
6209 (setq pos (point))
6210 (looking-at c-opt-type-modifier-key))
6211 (goto-char (match-end 1))))))
6212
6213 ((looking-at c-overloadable-operators-regexp)
6214 ;; Got some other operator.
6215 (setq c-last-identifier-range
6216 (cons (point) (match-end 0)))
6217 (goto-char (match-end 0))
6218 (c-forward-syntactic-ws)
6219 (setq pos (point)
6220 res 'operator)))
6221
6222 nil)
6223
6224 ;; `id-start' is equal to `id-end' if we've jumped over
6225 ;; an identifier that doesn't end with a symbol token.
6226 ;; That can occur e.g. for Java import directives on the
6227 ;; form "foo.bar.*".
6228 (when (and id-start (/= id-start id-end))
6229 (setq c-last-identifier-range
6230 (cons id-start id-end)))
6231 (goto-char id-end)
6232 (c-forward-syntactic-ws)
6233 (setq pos (point)
6234 res t)))
6235
6236 (progn
6237 (goto-char pos)
6238 (when (or c-opt-identifier-concat-key
6239 c-recognize-<>-arglists)
6240
6241 (cond
6242 ((and c-opt-identifier-concat-key
6243 (looking-at c-opt-identifier-concat-key))
6244 ;; Got a concatenated identifier. This handles the
6245 ;; cases with tricky syntactic whitespace that aren't
6246 ;; covered in `c-identifier-key'.
6247 (goto-char (match-end 0))
6248 (c-forward-syntactic-ws)
6249 t)
6250
6251 ((and c-recognize-<>-arglists
6252 (eq (char-after) ?<))
6253 ;; Maybe an angle bracket arglist.
6254 (when (let ((c-record-type-identifiers t)
6255 (c-record-found-types t))
6256 (c-forward-<>-arglist nil))
6257
6258 (c-add-type start (1+ pos))
6259 (c-forward-syntactic-ws)
6260 (setq pos (point)
6261 c-last-identifier-range nil)
6262
6263 (if (and c-opt-identifier-concat-key
6264 (looking-at c-opt-identifier-concat-key))
6265
6266 ;; Continue if there's an identifier concatenation
6267 ;; operator after the template argument.
6268 (progn
6269 (when (and c-record-type-identifiers id-start)
6270 (c-record-ref-id (cons id-start id-end)))
6271 (forward-char 2)
6272 (c-forward-syntactic-ws)
6273 t)
6274
6275 (when (and c-record-type-identifiers id-start)
6276 (c-record-type-id (cons id-start id-end)))
6277 (setq res 'template)
6278 nil)))
6279 )))))
6280
6281 (goto-char pos)
6282 res))
6283
6284 (defun c-forward-type (&optional brace-block-too)
6285 ;; Move forward over a type spec if at the beginning of one,
6286 ;; stopping at the next following token. The keyword "typedef"
6287 ;; isn't part of a type spec here.
6288 ;;
6289 ;; BRACE-BLOCK-TOO, when non-nil, means move over the brace block in
6290 ;; constructs like "struct foo {...} bar ;" or "struct {...} bar;".
6291 ;; The current (2009-03-10) intention is to convert all uses of
6292 ;; `c-forward-type' to call with this parameter set, then to
6293 ;; eliminate it.
6294 ;;
6295 ;; Return
6296 ;; o - t if it's a known type that can't be a name or other
6297 ;; expression;
6298 ;; o - 'known if it's an otherwise known type (according to
6299 ;; `*-font-lock-extra-types');
6300 ;; o - 'prefix if it's a known prefix of a type;
6301 ;; o - 'found if it's a type that matches one in `c-found-types';
6302 ;; o - 'maybe if it's an identifier that might be a type; or
6303 ;; o - nil if it can't be a type (the point isn't moved then).
6304 ;;
6305 ;; The point is assumed to be at the beginning of a token.
6306 ;;
6307 ;; Note that this function doesn't skip past the brace definition
6308 ;; that might be considered part of the type, e.g.
6309 ;; "enum {a, b, c} foo".
6310 ;;
6311 ;; This function records identifier ranges on
6312 ;; `c-record-type-identifiers' and `c-record-ref-identifiers' if
6313 ;; `c-record-type-identifiers' is non-nil.
6314 ;;
6315 ;; This function might do hidden buffer changes.
6316 (when (and c-recognize-<>-arglists
6317 (looking-at "<"))
6318 (c-forward-<>-arglist t)
6319 (c-forward-syntactic-ws))
6320
6321 (let ((start (point)) pos res name-res id-start id-end id-range)
6322
6323 ;; Skip leading type modifiers. If any are found we know it's a
6324 ;; prefix of a type.
6325 (when c-opt-type-modifier-key ; e.g. "const" "volatile", but NOT "typedef"
6326 (while (looking-at c-opt-type-modifier-key)
6327 (goto-char (match-end 1))
6328 (c-forward-syntactic-ws)
6329 (setq res 'prefix)))
6330
6331 (cond
6332 ((looking-at c-type-prefix-key) ; e.g. "struct", "class", but NOT
6333 ; "typedef".
6334 (goto-char (match-end 1))
6335 (c-forward-syntactic-ws)
6336 (setq pos (point))
6337
6338 (setq name-res (c-forward-name))
6339 (setq res (not (null name-res)))
6340 (when (eq name-res t)
6341 ;; In many languages the name can be used without the
6342 ;; prefix, so we add it to `c-found-types'.
6343 (c-add-type pos (point))
6344 (when (and c-record-type-identifiers
6345 c-last-identifier-range)
6346 (c-record-type-id c-last-identifier-range)))
6347 (when (and brace-block-too
6348 (memq res '(t nil))
6349 (eq (char-after) ?\{)
6350 (save-excursion
6351 (c-safe
6352 (progn (c-forward-sexp)
6353 (c-forward-syntactic-ws)
6354 (setq pos (point))))))
6355 (goto-char pos)
6356 (setq res t))
6357 (unless res (goto-char start))) ; invalid syntax
6358
6359 ((progn
6360 (setq pos nil)
6361 (if (looking-at c-identifier-start)
6362 (save-excursion
6363 (setq id-start (point)
6364 name-res (c-forward-name))
6365 (when name-res
6366 (setq id-end (point)
6367 id-range c-last-identifier-range))))
6368 (and (cond ((looking-at c-primitive-type-key)
6369 (setq res t))
6370 ((c-with-syntax-table c-identifier-syntax-table
6371 (looking-at c-known-type-key))
6372 (setq res 'known)))
6373 (or (not id-end)
6374 (>= (save-excursion
6375 (save-match-data
6376 (goto-char (match-end 1))
6377 (c-forward-syntactic-ws)
6378 (setq pos (point))))
6379 id-end)
6380 (setq res nil))))
6381 ;; Looking at a primitive or known type identifier. We've
6382 ;; checked for a name first so that we don't go here if the
6383 ;; known type match only is a prefix of another name.
6384
6385 (setq id-end (match-end 1))
6386
6387 (when (and c-record-type-identifiers
6388 (or c-promote-possible-types (eq res t)))
6389 (c-record-type-id (cons (match-beginning 1) (match-end 1))))
6390
6391 (if (and c-opt-type-component-key
6392 (save-match-data
6393 (looking-at c-opt-type-component-key)))
6394 ;; There might be more keywords for the type.
6395 (let (safe-pos)
6396 (c-forward-keyword-clause 1)
6397 (while (progn
6398 (setq safe-pos (point))
6399 (looking-at c-opt-type-component-key))
6400 (when (and c-record-type-identifiers
6401 (looking-at c-primitive-type-key))
6402 (c-record-type-id (cons (match-beginning 1)
6403 (match-end 1))))
6404 (c-forward-keyword-clause 1))
6405 (if (looking-at c-primitive-type-key)
6406 (progn
6407 (when c-record-type-identifiers
6408 (c-record-type-id (cons (match-beginning 1)
6409 (match-end 1))))
6410 (c-forward-keyword-clause 1)
6411 (setq res t))
6412 (goto-char safe-pos)
6413 (setq res 'prefix)))
6414 (unless (save-match-data (c-forward-keyword-clause 1))
6415 (if pos
6416 (goto-char pos)
6417 (goto-char (match-end 1))
6418 (c-forward-syntactic-ws)))))
6419
6420 (name-res
6421 (cond ((eq name-res t)
6422 ;; A normal identifier.
6423 (goto-char id-end)
6424 (if (or res c-promote-possible-types)
6425 (progn
6426 (c-add-type id-start id-end)
6427 (when (and c-record-type-identifiers id-range)
6428 (c-record-type-id id-range))
6429 (unless res
6430 (setq res 'found)))
6431 (setq res (if (c-check-type id-start id-end)
6432 ;; It's an identifier that has been used as
6433 ;; a type somewhere else.
6434 'found
6435 ;; It's an identifier that might be a type.
6436 'maybe))))
6437 ((eq name-res 'template)
6438 ;; A template is a type.
6439 (goto-char id-end)
6440 (setq res t))
6441 (t
6442 ;; Otherwise it's an operator identifier, which is not a type.
6443 (goto-char start)
6444 (setq res nil)))))
6445
6446 (when res
6447 ;; Skip trailing type modifiers. If any are found we know it's
6448 ;; a type.
6449 (when c-opt-type-modifier-key
6450 (while (looking-at c-opt-type-modifier-key) ; e.g. "const", "volatile"
6451 (goto-char (match-end 1))
6452 (c-forward-syntactic-ws)
6453 (setq res t)))
6454 ;; Step over any type suffix operator. Do not let the existence
6455 ;; of these alter the classification of the found type, since
6456 ;; these operators typically are allowed in normal expressions
6457 ;; too.
6458 (when c-opt-type-suffix-key
6459 (while (looking-at c-opt-type-suffix-key)
6460 (goto-char (match-end 1))
6461 (c-forward-syntactic-ws)))
6462
6463 (when c-opt-type-concat-key ; Only/mainly for pike.
6464 ;; Look for a trailing operator that concatenates the type
6465 ;; with a following one, and if so step past that one through
6466 ;; a recursive call. Note that we don't record concatenated
6467 ;; types in `c-found-types' - it's the component types that
6468 ;; are recorded when appropriate.
6469 (setq pos (point))
6470 (let* ((c-promote-possible-types (or (memq res '(t known))
6471 c-promote-possible-types))
6472 ;; If we can't promote then set `c-record-found-types' so that
6473 ;; we can merge in the types from the second part afterwards if
6474 ;; it turns out to be a known type there.
6475 (c-record-found-types (and c-record-type-identifiers
6476 (not c-promote-possible-types)))
6477 subres)
6478 (if (and (looking-at c-opt-type-concat-key)
6479
6480 (progn
6481 (goto-char (match-end 1))
6482 (c-forward-syntactic-ws)
6483 (setq subres (c-forward-type))))
6484
6485 (progn
6486 ;; If either operand certainly is a type then both are, but we
6487 ;; don't let the existence of the operator itself promote two
6488 ;; uncertain types to a certain one.
6489 (cond ((eq res t))
6490 ((eq subres t)
6491 (unless (eq name-res 'template)
6492 (c-add-type id-start id-end))
6493 (when (and c-record-type-identifiers id-range)
6494 (c-record-type-id id-range))
6495 (setq res t))
6496 ((eq res 'known))
6497 ((eq subres 'known)
6498 (setq res 'known))
6499 ((eq res 'found))
6500 ((eq subres 'found)
6501 (setq res 'found))
6502 (t
6503 (setq res 'maybe)))
6504
6505 (when (and (eq res t)
6506 (consp c-record-found-types))
6507 ;; Merge in the ranges of any types found by the second
6508 ;; `c-forward-type'.
6509 (setq c-record-type-identifiers
6510 ;; `nconc' doesn't mind that the tail of
6511 ;; `c-record-found-types' is t.
6512 (nconc c-record-found-types
6513 c-record-type-identifiers))))
6514
6515 (goto-char pos))))
6516
6517 (when (and c-record-found-types (memq res '(known found)) id-range)
6518 (setq c-record-found-types
6519 (cons id-range c-record-found-types))))
6520
6521 ;;(message "c-forward-type %s -> %s: %s" start (point) res)
6522
6523 res))
6524
6525 (defun c-forward-annotation ()
6526 ;; Used for Java code only at the moment. Assumes point is on the
6527 ;; @, moves forward an annotation. returns nil if there is no
6528 ;; annotation at point.
6529 (and (looking-at "@")
6530 (progn (forward-char) t)
6531 (c-forward-type)
6532 (progn (c-forward-syntactic-ws) t)
6533 (if (looking-at "(")
6534 (c-go-list-forward)
6535 t)))
6536
6537 (defmacro c-pull-open-brace (ps)
6538 ;; Pull the next open brace from PS (which has the form of paren-state),
6539 ;; skipping over any brace pairs. Returns NIL when PS is exhausted.
6540 `(progn
6541 (while (consp (car ,ps))
6542 (setq ,ps (cdr ,ps)))
6543 (prog1 (car ,ps)
6544 (setq ,ps (cdr ,ps)))))
6545
6546 (defun c-back-over-member-initializers ()
6547 ;; Test whether we are in a C++ member initializer list, and if so, go back
6548 ;; to the introducing ":", returning the position of the opening paren of
6549 ;; the function's arglist. Otherwise return nil, leaving point unchanged.
6550 (let ((here (point))
6551 (paren-state (c-parse-state))
6552 res)
6553
6554 (setq res
6555 (catch 'done
6556 (if (not (c-at-toplevel-p))
6557 (progn
6558 (while (not (c-at-toplevel-p))
6559 (goto-char (c-pull-open-brace paren-state)))
6560 (c-backward-syntactic-ws)
6561 (when (not (c-simple-skip-symbol-backward))
6562 (throw 'done nil))
6563 (c-backward-syntactic-ws))
6564 (c-backward-syntactic-ws)
6565 (when (memq (char-before) '(?\) ?}))
6566 (when (not (c-go-list-backward))
6567 (throw 'done nil))
6568 (c-backward-syntactic-ws))
6569 (when (c-simple-skip-symbol-backward)
6570 (c-backward-syntactic-ws)))
6571
6572 (while (eq (char-before) ?,)
6573 (backward-char)
6574 (c-backward-syntactic-ws)
6575
6576 (when (not (memq (char-before) '(?\) ?})))
6577 (throw 'done nil))
6578 (when (not (c-go-list-backward))
6579 (throw 'done nil))
6580 (c-backward-syntactic-ws)
6581 (when (not (c-simple-skip-symbol-backward))
6582 (throw 'done nil))
6583 (c-backward-syntactic-ws))
6584
6585 (and
6586 (eq (char-before) ?:)
6587 (c-just-after-func-arglist-p))))
6588
6589 (or res (goto-char here))
6590 res))
6591
6592 \f
6593 ;; Handling of large scale constructs like statements and declarations.
6594
6595 ;; Macro used inside `c-forward-decl-or-cast-1'. It ought to be a
6596 ;; defsubst or perhaps even a defun, but it contains lots of free
6597 ;; variables that refer to things inside `c-forward-decl-or-cast-1'.
6598 (defmacro c-fdoc-shift-type-backward (&optional short)
6599 ;; `c-forward-decl-or-cast-1' can consume an arbitrary length list
6600 ;; of types when parsing a declaration, which means that it
6601 ;; sometimes consumes the identifier in the declaration as a type.
6602 ;; This is used to "backtrack" and make the last type be treated as
6603 ;; an identifier instead.
6604 `(progn
6605 ,(unless short
6606 ;; These identifiers are bound only in the inner let.
6607 '(setq identifier-type at-type
6608 identifier-start type-start
6609 got-parens nil
6610 got-identifier t
6611 got-suffix t
6612 got-suffix-after-parens id-start
6613 paren-depth 0))
6614
6615 (if (setq at-type (if (eq backup-at-type 'prefix)
6616 t
6617 backup-at-type))
6618 (setq type-start backup-type-start
6619 id-start backup-id-start)
6620 (setq type-start start-pos
6621 id-start start-pos))
6622
6623 ;; When these flags already are set we've found specifiers that
6624 ;; unconditionally signal these attributes - backtracking doesn't
6625 ;; change that. So keep them set in that case.
6626 (or at-type-decl
6627 (setq at-type-decl backup-at-type-decl))
6628 (or maybe-typeless
6629 (setq maybe-typeless backup-maybe-typeless))
6630
6631 ,(unless short
6632 ;; This identifier is bound only in the inner let.
6633 '(setq start id-start))))
6634
6635 (defun c-forward-decl-or-cast-1 (preceding-token-end context last-cast-end)
6636 ;; Move forward over a declaration or a cast if at the start of one.
6637 ;; The point is assumed to be at the start of some token. Nil is
6638 ;; returned if no declaration or cast is recognized, and the point
6639 ;; is clobbered in that case.
6640 ;;
6641 ;; If a declaration is parsed:
6642 ;;
6643 ;; The point is left at the first token after the first complete
6644 ;; declarator, if there is one. The return value is a cons where
6645 ;; the car is the position of the first token in the declarator. (See
6646 ;; below for the cdr.)
6647 ;; Some examples:
6648 ;;
6649 ;; void foo (int a, char *b) stuff ...
6650 ;; car ^ ^ point
6651 ;; float (*a)[], b;
6652 ;; car ^ ^ point
6653 ;; unsigned int a = c_style_initializer, b;
6654 ;; car ^ ^ point
6655 ;; unsigned int a (cplusplus_style_initializer), b;
6656 ;; car ^ ^ point (might change)
6657 ;; class Foo : public Bar {}
6658 ;; car ^ ^ point
6659 ;; class PikeClass (int a, string b) stuff ...
6660 ;; car ^ ^ point
6661 ;; enum bool;
6662 ;; car ^ ^ point
6663 ;; enum bool flag;
6664 ;; car ^ ^ point
6665 ;; void cplusplus_function (int x) throw (Bad);
6666 ;; car ^ ^ point
6667 ;; Foo::Foo (int b) : Base (b) {}
6668 ;; car ^ ^ point
6669 ;;
6670 ;; The cdr of the return value is non-nil when a
6671 ;; `c-typedef-decl-kwds' specifier is found in the declaration.
6672 ;; Specifically it is a dotted pair (A . B) where B is t when a
6673 ;; `c-typedef-kwds' ("typedef") is present, and A is t when some
6674 ;; other `c-typedef-decl-kwds' (e.g. class, struct, enum)
6675 ;; specifier is present. I.e., (some of) the declared
6676 ;; identifier(s) are types.
6677 ;;
6678 ;; If a cast is parsed:
6679 ;;
6680 ;; The point is left at the first token after the closing paren of
6681 ;; the cast. The return value is `cast'. Note that the start
6682 ;; position must be at the first token inside the cast parenthesis
6683 ;; to recognize it.
6684 ;;
6685 ;; PRECEDING-TOKEN-END is the first position after the preceding
6686 ;; token, i.e. on the other side of the syntactic ws from the point.
6687 ;; Use a value less than or equal to (point-min) if the point is at
6688 ;; the first token in (the visible part of) the buffer.
6689 ;;
6690 ;; CONTEXT is a symbol that describes the context at the point:
6691 ;; 'decl In a comma-separated declaration context (typically
6692 ;; inside a function declaration arglist).
6693 ;; '<> In an angle bracket arglist.
6694 ;; 'arglist Some other type of arglist.
6695 ;; nil Some other context or unknown context. Includes
6696 ;; within the parens of an if, for, ... construct.
6697 ;;
6698 ;; LAST-CAST-END is the first token after the closing paren of a
6699 ;; preceding cast, or nil if none is known. If
6700 ;; `c-forward-decl-or-cast-1' is used in succession, it should be
6701 ;; the position after the closest preceding call where a cast was
6702 ;; matched. In that case it's used to discover chains of casts like
6703 ;; "(a) (b) c".
6704 ;;
6705 ;; This function records identifier ranges on
6706 ;; `c-record-type-identifiers' and `c-record-ref-identifiers' if
6707 ;; `c-record-type-identifiers' is non-nil.
6708 ;;
6709 ;; This function might do hidden buffer changes.
6710
6711 (let (;; `start-pos' is used below to point to the start of the
6712 ;; first type, i.e. after any leading specifiers. It might
6713 ;; also point at the beginning of the preceding syntactic
6714 ;; whitespace.
6715 (start-pos (point))
6716 ;; Set to the result of `c-forward-type'.
6717 at-type
6718 ;; The position of the first token in what we currently
6719 ;; believe is the type in the declaration or cast, after any
6720 ;; specifiers and their associated clauses.
6721 type-start
6722 ;; The position of the first token in what we currently
6723 ;; believe is the declarator for the first identifier. Set
6724 ;; when the type is found, and moved forward over any
6725 ;; `c-decl-hangon-kwds' and their associated clauses that
6726 ;; occurs after the type.
6727 id-start
6728 ;; These store `at-type', `type-start' and `id-start' of the
6729 ;; identifier before the one in those variables. The previous
6730 ;; identifier might turn out to be the real type in a
6731 ;; declaration if the last one has to be the declarator in it.
6732 ;; If `backup-at-type' is nil then the other variables have
6733 ;; undefined values.
6734 backup-at-type backup-type-start backup-id-start
6735 ;; Set if we've found a specifier (apart from "typedef") that makes
6736 ;; the defined identifier(s) types.
6737 at-type-decl
6738 ;; Set if we've a "typedef" keyword.
6739 at-typedef
6740 ;; Set if we've found a specifier that can start a declaration
6741 ;; where there's no type.
6742 maybe-typeless
6743 ;; If a specifier is found that also can be a type prefix,
6744 ;; these flags are set instead of those above. If we need to
6745 ;; back up an identifier, they are copied to the real flag
6746 ;; variables. Thus they only take effect if we fail to
6747 ;; interpret it as a type.
6748 backup-at-type-decl backup-maybe-typeless
6749 ;; Whether we've found a declaration or a cast. We might know
6750 ;; this before we've found the type in it. It's 'ids if we've
6751 ;; found two consecutive identifiers (usually a sure sign, but
6752 ;; we should allow that in labels too), and t if we've found a
6753 ;; specifier keyword (a 100% sure sign).
6754 at-decl-or-cast
6755 ;; Set when we need to back up to parse this as a declaration
6756 ;; but not as a cast.
6757 backup-if-not-cast
6758 ;; For casts, the return position.
6759 cast-end
6760 ;; Save `c-record-type-identifiers' and
6761 ;; `c-record-ref-identifiers' since ranges are recorded
6762 ;; speculatively and should be thrown away if it turns out
6763 ;; that it isn't a declaration or cast.
6764 (save-rec-type-ids c-record-type-identifiers)
6765 (save-rec-ref-ids c-record-ref-identifiers))
6766
6767 (while (c-forward-annotation)
6768 (c-forward-syntactic-ws))
6769
6770 ;; Check for a type. Unknown symbols are treated as possible
6771 ;; types, but they could also be specifiers disguised through
6772 ;; macros like __INLINE__, so we recognize both types and known
6773 ;; specifiers after them too.
6774 (while
6775 (let* ((start (point)) kwd-sym kwd-clause-end found-type)
6776
6777 ;; Look for a specifier keyword clause.
6778 (when (or (looking-at c-prefix-spec-kwds-re)
6779 (and (c-major-mode-is 'java-mode)
6780 (looking-at "@[A-Za-z0-9]+")))
6781 (if (looking-at c-typedef-key)
6782 (setq at-typedef t))
6783 (setq kwd-sym (c-keyword-sym (match-string 1)))
6784 (save-excursion
6785 (c-forward-keyword-clause 1)
6786 (setq kwd-clause-end (point))))
6787
6788 (when (setq found-type (c-forward-type t)) ; brace-block-too
6789 ;; Found a known or possible type or a prefix of a known type.
6790
6791 (when at-type
6792 ;; Got two identifiers with nothing but whitespace
6793 ;; between them. That can only happen in declarations.
6794 (setq at-decl-or-cast 'ids)
6795
6796 (when (eq at-type 'found)
6797 ;; If the previous identifier is a found type we
6798 ;; record it as a real one; it might be some sort of
6799 ;; alias for a prefix like "unsigned".
6800 (save-excursion
6801 (goto-char type-start)
6802 (let ((c-promote-possible-types t))
6803 (c-forward-type)))))
6804
6805 (setq backup-at-type at-type
6806 backup-type-start type-start
6807 backup-id-start id-start
6808 at-type found-type
6809 type-start start
6810 id-start (point)
6811 ;; The previous ambiguous specifier/type turned out
6812 ;; to be a type since we've parsed another one after
6813 ;; it, so clear these backup flags.
6814 backup-at-type-decl nil
6815 backup-maybe-typeless nil))
6816
6817 (if kwd-sym
6818 (progn
6819 ;; Handle known specifier keywords and
6820 ;; `c-decl-hangon-kwds' which can occur after known
6821 ;; types.
6822
6823 (if (c-keyword-member kwd-sym 'c-decl-hangon-kwds)
6824 ;; It's a hang-on keyword that can occur anywhere.
6825 (progn
6826 (setq at-decl-or-cast t)
6827 (if at-type
6828 ;; Move the identifier start position if
6829 ;; we've passed a type.
6830 (setq id-start kwd-clause-end)
6831 ;; Otherwise treat this as a specifier and
6832 ;; move the fallback position.
6833 (setq start-pos kwd-clause-end))
6834 (goto-char kwd-clause-end))
6835
6836 ;; It's an ordinary specifier so we know that
6837 ;; anything before this can't be the type.
6838 (setq backup-at-type nil
6839 start-pos kwd-clause-end)
6840
6841 (if found-type
6842 ;; It's ambiguous whether this keyword is a
6843 ;; specifier or a type prefix, so set the backup
6844 ;; flags. (It's assumed that `c-forward-type'
6845 ;; moved further than `c-forward-keyword-clause'.)
6846 (progn
6847 (when (c-keyword-member kwd-sym 'c-typedef-decl-kwds)
6848 (setq backup-at-type-decl t))
6849 (when (c-keyword-member kwd-sym 'c-typeless-decl-kwds)
6850 (setq backup-maybe-typeless t)))
6851
6852 (when (c-keyword-member kwd-sym 'c-typedef-decl-kwds)
6853 ;; This test only happens after we've scanned a type.
6854 ;; So, with valid syntax, kwd-sym can't be 'typedef.
6855 (setq at-type-decl t))
6856 (when (c-keyword-member kwd-sym 'c-typeless-decl-kwds)
6857 (setq maybe-typeless t))
6858
6859 ;; Haven't matched a type so it's an unambiguous
6860 ;; specifier keyword and we know we're in a
6861 ;; declaration.
6862 (setq at-decl-or-cast t)
6863
6864 (goto-char kwd-clause-end))))
6865
6866 ;; If the type isn't known we continue so that we'll jump
6867 ;; over all specifiers and type identifiers. The reason
6868 ;; to do this for a known type prefix is to make things
6869 ;; like "unsigned INT16" work.
6870 (and found-type (not (eq found-type t))))))
6871
6872 (cond
6873 ((eq at-type t)
6874 ;; If a known type was found, we still need to skip over any
6875 ;; hangon keyword clauses after it. Otherwise it has already
6876 ;; been done in the loop above.
6877 (while (looking-at c-decl-hangon-key)
6878 (c-forward-keyword-clause 1))
6879 (setq id-start (point)))
6880
6881 ((eq at-type 'prefix)
6882 ;; A prefix type is itself a primitive type when it's not
6883 ;; followed by another type.
6884 (setq at-type t))
6885
6886 ((not at-type)
6887 ;; Got no type but set things up to continue anyway to handle
6888 ;; the various cases when a declaration doesn't start with a
6889 ;; type.
6890 (setq id-start start-pos))
6891
6892 ((and (eq at-type 'maybe)
6893 (c-major-mode-is 'c++-mode))
6894 ;; If it's C++ then check if the last "type" ends on the form
6895 ;; "foo::foo" or "foo::~foo", i.e. if it's the name of a
6896 ;; (con|de)structor.
6897 (save-excursion
6898 (let (name end-2 end-1)
6899 (goto-char id-start)
6900 (c-backward-syntactic-ws)
6901 (setq end-2 (point))
6902 (when (and
6903 (c-simple-skip-symbol-backward)
6904 (progn
6905 (setq name
6906 (buffer-substring-no-properties (point) end-2))
6907 ;; Cheating in the handling of syntactic ws below.
6908 (< (skip-chars-backward ":~ \t\n\r\v\f") 0))
6909 (progn
6910 (setq end-1 (point))
6911 (c-simple-skip-symbol-backward))
6912 (>= (point) type-start)
6913 (equal (buffer-substring-no-properties (point) end-1)
6914 name))
6915 ;; It is a (con|de)structor name. In that case the
6916 ;; declaration is typeless so zap out any preceding
6917 ;; identifier(s) that we might have taken as types.
6918 (goto-char type-start)
6919 (setq at-type nil
6920 backup-at-type nil
6921 id-start type-start))))))
6922
6923 ;; Check for and step over a type decl expression after the thing
6924 ;; that is or might be a type. This can't be skipped since we
6925 ;; need the correct end position of the declarator for
6926 ;; `max-type-decl-end-*'.
6927 (let ((start (point)) (paren-depth 0) pos
6928 ;; True if there's a non-open-paren match of
6929 ;; `c-type-decl-prefix-key'.
6930 got-prefix
6931 ;; True if the declarator is surrounded by a parenthesis pair.
6932 got-parens
6933 ;; True if there is an identifier in the declarator.
6934 got-identifier
6935 ;; True if there's a non-close-paren match of
6936 ;; `c-type-decl-suffix-key'.
6937 got-suffix
6938 ;; True if there's a prefix match outside the outermost
6939 ;; paren pair that surrounds the declarator.
6940 got-prefix-before-parens
6941 ;; True if there's a suffix match outside the outermost
6942 ;; paren pair that surrounds the declarator. The value is
6943 ;; the position of the first suffix match.
6944 got-suffix-after-parens
6945 ;; True if we've parsed the type decl to a token that is
6946 ;; known to end declarations in this context.
6947 at-decl-end
6948 ;; The earlier values of `at-type' and `type-start' if we've
6949 ;; shifted the type backwards.
6950 identifier-type identifier-start
6951 ;; If `c-parse-and-markup-<>-arglists' is set we need to
6952 ;; turn it off during the name skipping below to avoid
6953 ;; getting `c-type' properties that might be bogus. That
6954 ;; can happen since we don't know if
6955 ;; `c-restricted-<>-arglists' will be correct inside the
6956 ;; arglist paren that gets entered.
6957 c-parse-and-markup-<>-arglists
6958 ;; Start of the identifier for which `got-identifier' was set.
6959 name-start)
6960
6961 (goto-char id-start)
6962
6963 ;; Skip over type decl prefix operators. (Note similar code in
6964 ;; `c-font-lock-declarators'.)
6965 (if (and c-recognize-typeless-decls
6966 (equal c-type-decl-prefix-key "\\<\\>"))
6967 (when (eq (char-after) ?\()
6968 (progn
6969 (setq paren-depth (1+ paren-depth))
6970 (forward-char)))
6971 (while (and (looking-at c-type-decl-prefix-key)
6972 (if (and (c-major-mode-is 'c++-mode)
6973 (match-beginning 3))
6974 ;; If the third submatch matches in C++ then
6975 ;; we're looking at an identifier that's a
6976 ;; prefix only if it specifies a member pointer.
6977 (when (progn (setq pos (point))
6978 (setq got-identifier (c-forward-name)))
6979 (setq name-start pos)
6980 (if (looking-at "\\(::\\)")
6981 ;; We only check for a trailing "::" and
6982 ;; let the "*" that should follow be
6983 ;; matched in the next round.
6984 (progn (setq got-identifier nil) t)
6985 ;; It turned out to be the real identifier,
6986 ;; so stop.
6987 nil))
6988 t))
6989
6990 (if (eq (char-after) ?\()
6991 (progn
6992 (setq paren-depth (1+ paren-depth))
6993 (forward-char))
6994 (unless got-prefix-before-parens
6995 (setq got-prefix-before-parens (= paren-depth 0)))
6996 (setq got-prefix t)
6997 (goto-char (match-end 1)))
6998 (c-forward-syntactic-ws)))
6999
7000 (setq got-parens (> paren-depth 0))
7001
7002 ;; Skip over an identifier.
7003 (or got-identifier
7004 (and (looking-at c-identifier-start)
7005 (setq pos (point))
7006 (setq got-identifier (c-forward-name))
7007 (setq name-start pos)))
7008
7009 ;; Skip over type decl suffix operators.
7010 (while (if (looking-at c-type-decl-suffix-key)
7011
7012 (if (eq (char-after) ?\))
7013 (when (> paren-depth 0)
7014 (setq paren-depth (1- paren-depth))
7015 (forward-char)
7016 t)
7017 (when (if (save-match-data (looking-at "\\s\("))
7018 (c-safe (c-forward-sexp 1) t)
7019 (goto-char (match-end 1))
7020 t)
7021 (when (and (not got-suffix-after-parens)
7022 (= paren-depth 0))
7023 (setq got-suffix-after-parens (match-beginning 0)))
7024 (setq got-suffix t)))
7025
7026 ;; No suffix matched. We might have matched the
7027 ;; identifier as a type and the open paren of a
7028 ;; function arglist as a type decl prefix. In that
7029 ;; case we should "backtrack": Reinterpret the last
7030 ;; type as the identifier, move out of the arglist and
7031 ;; continue searching for suffix operators.
7032 ;;
7033 ;; Do this even if there's no preceding type, to cope
7034 ;; with old style function declarations in K&R C,
7035 ;; (con|de)structors in C++ and `c-typeless-decl-kwds'
7036 ;; style declarations. That isn't applicable in an
7037 ;; arglist context, though.
7038 (when (and (= paren-depth 1)
7039 (not got-prefix-before-parens)
7040 (not (eq at-type t))
7041 (or backup-at-type
7042 maybe-typeless
7043 backup-maybe-typeless
7044 (when c-recognize-typeless-decls
7045 (not context)))
7046 (setq pos (c-up-list-forward (point)))
7047 (eq (char-before pos) ?\)))
7048 (c-fdoc-shift-type-backward)
7049 (goto-char pos)
7050 t))
7051
7052 (c-forward-syntactic-ws))
7053
7054 (when (and (or maybe-typeless backup-maybe-typeless)
7055 (not got-identifier)
7056 (not got-prefix)
7057 at-type)
7058 ;; Have found no identifier but `c-typeless-decl-kwds' has
7059 ;; matched so we know we're inside a declaration. The
7060 ;; preceding type must be the identifier instead.
7061 (c-fdoc-shift-type-backward))
7062
7063 (setq
7064 at-decl-or-cast
7065 (catch 'at-decl-or-cast
7066
7067 ;; CASE 1
7068 (when (> paren-depth 0)
7069 ;; Encountered something inside parens that isn't matched by
7070 ;; the `c-type-decl-*' regexps, so it's not a type decl
7071 ;; expression. Try to skip out to the same paren depth to
7072 ;; not confuse the cast check below.
7073 (c-safe (goto-char (scan-lists (point) 1 paren-depth)))
7074 ;; If we've found a specifier keyword then it's a
7075 ;; declaration regardless.
7076 (throw 'at-decl-or-cast (eq at-decl-or-cast t)))
7077
7078 (setq at-decl-end
7079 (looking-at (cond ((eq context '<>) "[,>]")
7080 (context "[,\)]")
7081 (t "[,;]"))))
7082
7083 ;; Now we've collected info about various characteristics of
7084 ;; the construct we're looking at. Below follows a decision
7085 ;; tree based on that. It's ordered to check more certain
7086 ;; signs before less certain ones.
7087
7088 (if got-identifier
7089 (progn
7090
7091 ;; CASE 2
7092 (when (and (or at-type maybe-typeless)
7093 (not (or got-prefix got-parens)))
7094 ;; Got another identifier directly after the type, so it's a
7095 ;; declaration.
7096 (throw 'at-decl-or-cast t))
7097
7098
7099 (when (and got-parens
7100 (not got-prefix)
7101 ;; (not got-suffix-after-parens)
7102 (or backup-at-type
7103 maybe-typeless
7104 backup-maybe-typeless
7105 (eq at-decl-or-cast t)
7106 (save-excursion
7107 (goto-char name-start)
7108 (not (memq (c-forward-type) '(nil maybe))))))
7109 ;; Got a declaration of the form "foo bar (gnu);" or "bar
7110 ;; (gnu);" where we've recognized "bar" as the type and "gnu"
7111 ;; as the declarator. In this case it's however more likely
7112 ;; that "bar" is the declarator and "gnu" a function argument
7113 ;; or initializer (if `c-recognize-paren-inits' is set),
7114 ;; since the parens around "gnu" would be superfluous if it's
7115 ;; a declarator. Shift the type one step backward.
7116 (c-fdoc-shift-type-backward)))
7117
7118 ;; Found no identifier.
7119 (if backup-at-type
7120 (progn
7121
7122
7123 ;; CASE 3
7124 (when (= (point) start)
7125 ;; Got a plain list of identifiers. If a colon follows it's
7126 ;; a valid label, or maybe a bitfield. Otherwise the last
7127 ;; one probably is the declared identifier and we should
7128 ;; back up to the previous type, providing it isn't a cast.
7129 (if (and (eq (char-after) ?:)
7130 (not (c-major-mode-is 'java-mode)))
7131 (cond
7132 ;; If we've found a specifier keyword then it's a
7133 ;; declaration regardless.
7134 ((eq at-decl-or-cast t)
7135 (throw 'at-decl-or-cast t))
7136 ((and c-has-bitfields
7137 (eq at-decl-or-cast 'ids)) ; bitfield.
7138 (setq backup-if-not-cast t)
7139 (throw 'at-decl-or-cast t)))
7140
7141 (setq backup-if-not-cast t)
7142 (throw 'at-decl-or-cast t)))
7143
7144 ;; CASE 4
7145 (when (and got-suffix
7146 (not got-prefix)
7147 (not got-parens))
7148 ;; Got a plain list of identifiers followed by some suffix.
7149 ;; If this isn't a cast then the last identifier probably is
7150 ;; the declared one and we should back up to the previous
7151 ;; type.
7152 (setq backup-if-not-cast t)
7153 (throw 'at-decl-or-cast t)))
7154
7155 ;; CASE 5
7156 (when (eq at-type t)
7157 ;; If the type is known we know that there can't be any
7158 ;; identifier somewhere else, and it's only in declarations in
7159 ;; e.g. function prototypes and in casts that the identifier may
7160 ;; be left out.
7161 (throw 'at-decl-or-cast t))
7162
7163 (when (= (point) start)
7164 ;; Only got a single identifier (parsed as a type so far).
7165 ;; CASE 6
7166 (if (and
7167 ;; Check that the identifier isn't at the start of an
7168 ;; expression.
7169 at-decl-end
7170 (cond
7171 ((eq context 'decl)
7172 ;; Inside an arglist that contains declarations. If K&R
7173 ;; style declarations and parenthesis style initializers
7174 ;; aren't allowed then the single identifier must be a
7175 ;; type, else we require that it's known or found
7176 ;; (primitive types are handled above).
7177 (or (and (not c-recognize-knr-p)
7178 (not c-recognize-paren-inits))
7179 (memq at-type '(known found))))
7180 ((eq context '<>)
7181 ;; Inside a template arglist. Accept known and found
7182 ;; types; other identifiers could just as well be
7183 ;; constants in C++.
7184 (memq at-type '(known found)))))
7185 (throw 'at-decl-or-cast t)
7186 ;; CASE 7
7187 ;; Can't be a valid declaration or cast, but if we've found a
7188 ;; specifier it can't be anything else either, so treat it as
7189 ;; an invalid/unfinished declaration or cast.
7190 (throw 'at-decl-or-cast at-decl-or-cast))))
7191
7192 (if (and got-parens
7193 (not got-prefix)
7194 (not context)
7195 (not (eq at-type t))
7196 (or backup-at-type
7197 maybe-typeless
7198 backup-maybe-typeless
7199 (when c-recognize-typeless-decls
7200 (or (not got-suffix)
7201 (not (looking-at
7202 c-after-suffixed-type-maybe-decl-key))))))
7203 ;; Got an empty paren pair and a preceding type that probably
7204 ;; really is the identifier. Shift the type backwards to make
7205 ;; the last one the identifier. This is analogous to the
7206 ;; "backtracking" done inside the `c-type-decl-suffix-key' loop
7207 ;; above.
7208 ;;
7209 ;; Exception: In addition to the conditions in that
7210 ;; "backtracking" code, do not shift backward if we're not
7211 ;; looking at either `c-after-suffixed-type-decl-key' or "[;,]".
7212 ;; Since there's no preceding type, the shift would mean that
7213 ;; the declaration is typeless. But if the regexp doesn't match
7214 ;; then we will simply fall through in the tests below and not
7215 ;; recognize it at all, so it's better to try it as an abstract
7216 ;; declarator instead.
7217 (c-fdoc-shift-type-backward)
7218
7219 ;; Still no identifier.
7220 ;; CASE 8
7221 (when (and got-prefix (or got-parens got-suffix))
7222 ;; Require `got-prefix' together with either `got-parens' or
7223 ;; `got-suffix' to recognize it as an abstract declarator:
7224 ;; `got-parens' only is probably an empty function call.
7225 ;; `got-suffix' only can build an ordinary expression together
7226 ;; with the preceding identifier which we've taken as a type.
7227 ;; We could actually accept on `got-prefix' only, but that can
7228 ;; easily occur temporarily while writing an expression so we
7229 ;; avoid that case anyway. We could do a better job if we knew
7230 ;; the point when the fontification was invoked.
7231 (throw 'at-decl-or-cast t))
7232
7233 ;; CASE 9
7234 (when (and at-type
7235 (not got-prefix)
7236 (not got-parens)
7237 got-suffix-after-parens
7238 (eq (char-after got-suffix-after-parens) ?\())
7239 ;; Got a type, no declarator but a paren suffix. I.e. it's a
7240 ;; normal function call after all (or perhaps a C++ style object
7241 ;; instantiation expression).
7242 (throw 'at-decl-or-cast nil))))
7243
7244 ;; CASE 10
7245 (when at-decl-or-cast
7246 ;; By now we've located the type in the declaration that we know
7247 ;; we're in.
7248 (throw 'at-decl-or-cast t))
7249
7250 ;; CASE 11
7251 (when (and got-identifier
7252 (not context)
7253 (looking-at c-after-suffixed-type-decl-key)
7254 (if (and got-parens
7255 (not got-prefix)
7256 (not got-suffix)
7257 (not (eq at-type t)))
7258 ;; Shift the type backward in the case that there's a
7259 ;; single identifier inside parens. That can only
7260 ;; occur in K&R style function declarations so it's
7261 ;; more likely that it really is a function call.
7262 ;; Therefore we only do this after
7263 ;; `c-after-suffixed-type-decl-key' has matched.
7264 (progn (c-fdoc-shift-type-backward) t)
7265 got-suffix-after-parens))
7266 ;; A declaration according to `c-after-suffixed-type-decl-key'.
7267 (throw 'at-decl-or-cast t))
7268
7269 ;; CASE 12
7270 (when (and (or got-prefix (not got-parens))
7271 (memq at-type '(t known)))
7272 ;; It's a declaration if a known type precedes it and it can't be a
7273 ;; function call.
7274 (throw 'at-decl-or-cast t))
7275
7276 ;; If we get here we can't tell if this is a type decl or a normal
7277 ;; expression by looking at it alone. (That's under the assumption
7278 ;; that normal expressions always can look like type decl expressions,
7279 ;; which isn't really true but the cases where it doesn't hold are so
7280 ;; uncommon (e.g. some placements of "const" in C++) it's not worth
7281 ;; the effort to look for them.)
7282
7283 ;;; 2008-04-16: commented out the next form, to allow the function to recognize
7284 ;;; "foo (int bar)" in CC (an implicit type (in class foo) without a semicolon)
7285 ;;; as a(n almost complete) declaration, enabling it to be fontified.
7286 ;; CASE 13
7287 ;; (unless (or at-decl-end (looking-at "=[^=]"))
7288 ;; If this is a declaration it should end here or its initializer(*)
7289 ;; should start here, so check for allowed separation tokens. Note
7290 ;; that this rule doesn't work e.g. with a K&R arglist after a
7291 ;; function header.
7292 ;;
7293 ;; *) Don't check for C++ style initializers using parens
7294 ;; since those already have been matched as suffixes.
7295 ;;
7296 ;; If `at-decl-or-cast' is then we've found some other sign that
7297 ;; it's a declaration or cast, so then it's probably an
7298 ;; invalid/unfinished one.
7299 ;; (throw 'at-decl-or-cast at-decl-or-cast))
7300
7301 ;; Below are tests that only should be applied when we're certain to
7302 ;; not have parsed halfway through an expression.
7303
7304 ;; CASE 14
7305 (when (memq at-type '(t known))
7306 ;; The expression starts with a known type so treat it as a
7307 ;; declaration.
7308 (throw 'at-decl-or-cast t))
7309
7310 ;; CASE 15
7311 (when (and (c-major-mode-is 'c++-mode)
7312 ;; In C++ we check if the identifier is a known type, since
7313 ;; (con|de)structors use the class name as identifier.
7314 ;; We've always shifted over the identifier as a type and
7315 ;; then backed up again in this case.
7316 identifier-type
7317 (or (memq identifier-type '(found known))
7318 (and (eq (char-after identifier-start) ?~)
7319 ;; `at-type' probably won't be 'found for
7320 ;; destructors since the "~" is then part of the
7321 ;; type name being checked against the list of
7322 ;; known types, so do a check without that
7323 ;; operator.
7324 (or (save-excursion
7325 (goto-char (1+ identifier-start))
7326 (c-forward-syntactic-ws)
7327 (c-with-syntax-table
7328 c-identifier-syntax-table
7329 (looking-at c-known-type-key)))
7330 (save-excursion
7331 (goto-char (1+ identifier-start))
7332 ;; We have already parsed the type earlier,
7333 ;; so it'd be possible to cache the end
7334 ;; position instead of redoing it here, but
7335 ;; then we'd need to keep track of another
7336 ;; position everywhere.
7337 (c-check-type (point)
7338 (progn (c-forward-type)
7339 (point))))))))
7340 (throw 'at-decl-or-cast t))
7341
7342 (if got-identifier
7343 (progn
7344 ;; CASE 16
7345 (when (and got-prefix-before-parens
7346 at-type
7347 (or at-decl-end (looking-at "=[^=]"))
7348 (not context)
7349 (not got-suffix))
7350 ;; Got something like "foo * bar;". Since we're not inside an
7351 ;; arglist it would be a meaningless expression because the
7352 ;; result isn't used. We therefore choose to recognize it as
7353 ;; a declaration. Do not allow a suffix since it could then
7354 ;; be a function call.
7355 (throw 'at-decl-or-cast t))
7356
7357 ;; CASE 17
7358 (when (and (or got-suffix-after-parens
7359 (looking-at "=[^=]"))
7360 (eq at-type 'found)
7361 (not (eq context 'arglist)))
7362 ;; Got something like "a (*b) (c);" or "a (b) = c;". It could
7363 ;; be an odd expression or it could be a declaration. Treat
7364 ;; it as a declaration if "a" has been used as a type
7365 ;; somewhere else (if it's a known type we won't get here).
7366 (throw 'at-decl-or-cast t)))
7367
7368 ;; CASE 18
7369 (when (and context
7370 (or got-prefix
7371 (and (eq context 'decl)
7372 (not c-recognize-paren-inits)
7373 (or got-parens got-suffix))))
7374 ;; Got a type followed by an abstract declarator. If `got-prefix'
7375 ;; is set it's something like "a *" without anything after it. If
7376 ;; `got-parens' or `got-suffix' is set it's "a()", "a[]", "a()[]",
7377 ;; or similar, which we accept only if the context rules out
7378 ;; expressions.
7379 (throw 'at-decl-or-cast t)))
7380
7381 ;; If we had a complete symbol table here (which rules out
7382 ;; `c-found-types') we should return t due to the disambiguation rule
7383 ;; (in at least C++) that anything that can be parsed as a declaration
7384 ;; is a declaration. Now we're being more defensive and prefer to
7385 ;; highlight things like "foo (bar);" as a declaration only if we're
7386 ;; inside an arglist that contains declarations.
7387 (eq context 'decl))))
7388
7389 ;; The point is now after the type decl expression.
7390
7391 (cond
7392 ;; Check for a cast.
7393 ((save-excursion
7394 (and
7395 c-cast-parens
7396
7397 ;; Should be the first type/identifier in a cast paren.
7398 (> preceding-token-end (point-min))
7399 (memq (char-before preceding-token-end) c-cast-parens)
7400
7401 ;; The closing paren should follow.
7402 (progn
7403 (c-forward-syntactic-ws)
7404 (looking-at "\\s\)"))
7405
7406 ;; There should be a primary expression after it.
7407 (let (pos)
7408 (forward-char)
7409 (c-forward-syntactic-ws)
7410 (setq cast-end (point))
7411 (and (looking-at c-primary-expr-regexp)
7412 (progn
7413 (setq pos (match-end 0))
7414 (or
7415 ;; Check if the expression begins with a prefix keyword.
7416 (match-beginning 2)
7417 (if (match-beginning 1)
7418 ;; Expression begins with an ambiguous operator. Treat
7419 ;; it as a cast if it's a type decl or if we've
7420 ;; recognized the type somewhere else.
7421 (or at-decl-or-cast
7422 (memq at-type '(t known found)))
7423 ;; Unless it's a keyword, it's the beginning of a primary
7424 ;; expression.
7425 (not (looking-at c-keywords-regexp)))))
7426 ;; If `c-primary-expr-regexp' matched a nonsymbol token, check
7427 ;; that it matched a whole one so that we don't e.g. confuse
7428 ;; the operator '-' with '->'. It's ok if it matches further,
7429 ;; though, since it e.g. can match the float '.5' while the
7430 ;; operator regexp only matches '.'.
7431 (or (not (looking-at c-nonsymbol-token-regexp))
7432 (<= (match-end 0) pos))))
7433
7434 ;; There should either be a cast before it or something that isn't an
7435 ;; identifier or close paren.
7436 (> preceding-token-end (point-min))
7437 (progn
7438 (goto-char (1- preceding-token-end))
7439 (or (eq (point) last-cast-end)
7440 (progn
7441 (c-backward-syntactic-ws)
7442 (if (< (skip-syntax-backward "w_") 0)
7443 ;; It's a symbol. Accept it only if it's one of the
7444 ;; keywords that can precede an expression (without
7445 ;; surrounding parens).
7446 (looking-at c-simple-stmt-key)
7447 (and
7448 ;; Check that it isn't a close paren (block close is ok,
7449 ;; though).
7450 (not (memq (char-before) '(?\) ?\])))
7451 ;; Check that it isn't a nonsymbol identifier.
7452 (not (c-on-identifier)))))))))
7453
7454 ;; Handle the cast.
7455 (when (and c-record-type-identifiers at-type (not (eq at-type t)))
7456 (let ((c-promote-possible-types t))
7457 (goto-char type-start)
7458 (c-forward-type)))
7459
7460 (goto-char cast-end)
7461 'cast)
7462
7463 (at-decl-or-cast
7464 ;; We're at a declaration. Highlight the type and the following
7465 ;; declarators.
7466
7467 (when backup-if-not-cast
7468 (c-fdoc-shift-type-backward t))
7469
7470 (when (and (eq context 'decl) (looking-at ","))
7471 ;; Make sure to propagate the `c-decl-arg-start' property to
7472 ;; the next argument if it's set in this one, to cope with
7473 ;; interactive refontification.
7474 (c-put-c-type-property (point) 'c-decl-arg-start))
7475
7476 (when (and c-record-type-identifiers at-type ;; (not (eq at-type t))
7477 ;; There seems no reason to exclude a token from
7478 ;; fontification just because it's "a known type that can't
7479 ;; be a name or other expression". 2013-09-18.
7480 )
7481 (let ((c-promote-possible-types t))
7482 (save-excursion
7483 (goto-char type-start)
7484 (c-forward-type))))
7485
7486 (cons id-start
7487 (and (or at-type-decl at-typedef)
7488 (cons at-type-decl at-typedef))))
7489
7490 (t
7491 ;; False alarm. Restore the recorded ranges.
7492 (setq c-record-type-identifiers save-rec-type-ids
7493 c-record-ref-identifiers save-rec-ref-ids)
7494 nil))))
7495
7496 (defun c-forward-label (&optional assume-markup preceding-token-end limit)
7497 ;; Assuming that point is at the beginning of a token, check if it starts a
7498 ;; label and if so move over it and return non-nil (t in default situations,
7499 ;; specific symbols (see below) for interesting situations), otherwise don't
7500 ;; move and return nil. "Label" here means "most things with a colon".
7501 ;;
7502 ;; More precisely, a "label" is regarded as one of:
7503 ;; (i) a goto target like "foo:" - returns the symbol `goto-target';
7504 ;; (ii) A case label - either the entire construct "case FOO:", or just the
7505 ;; bare "case", should the colon be missing. We return t;
7506 ;; (iii) a keyword which needs a colon, like "default:" or "private:"; We
7507 ;; return t;
7508 ;; (iv) One of QT's "extended" C++ variants of
7509 ;; "private:"/"protected:"/"public:"/"more:" looking like "public slots:".
7510 ;; Returns the symbol `qt-2kwds-colon'.
7511 ;; (v) QT's construct "signals:". Returns the symbol `qt-1kwd-colon'.
7512 ;; (vi) One of the keywords matched by `c-opt-extra-label-key' (without any
7513 ;; colon). Currently (2006-03), this applies only to Objective C's
7514 ;; keywords "@private", "@protected", and "@public". Returns t.
7515 ;;
7516 ;; One of the things which will NOT be recognized as a label is a bit-field
7517 ;; element of a struct, something like "int foo:5".
7518 ;;
7519 ;; The end of the label is taken to be just after the colon, or the end of
7520 ;; the first submatch in `c-opt-extra-label-key'. The point is directly
7521 ;; after the end on return. The terminating char gets marked with
7522 ;; `c-decl-end' to improve recognition of the following declaration or
7523 ;; statement.
7524 ;;
7525 ;; If ASSUME-MARKUP is non-nil, it's assumed that the preceding
7526 ;; label, if any, has already been marked up like that.
7527 ;;
7528 ;; If PRECEDING-TOKEN-END is given, it should be the first position
7529 ;; after the preceding token, i.e. on the other side of the
7530 ;; syntactic ws from the point. Use a value less than or equal to
7531 ;; (point-min) if the point is at the first token in (the visible
7532 ;; part of) the buffer.
7533 ;;
7534 ;; The optional LIMIT limits the forward scan for the colon.
7535 ;;
7536 ;; This function records the ranges of the label symbols on
7537 ;; `c-record-ref-identifiers' if `c-record-type-identifiers' (!) is
7538 ;; non-nil.
7539 ;;
7540 ;; This function might do hidden buffer changes.
7541
7542 (let ((start (point))
7543 label-end
7544 qt-symbol-idx
7545 macro-start ; if we're in one.
7546 label-type
7547 kwd)
7548 (cond
7549 ;; "case" or "default" (Doesn't apply to AWK).
7550 ((looking-at c-label-kwds-regexp)
7551 (let ((kwd-end (match-end 1)))
7552 ;; Record only the keyword itself for fontification, since in
7553 ;; case labels the following is a constant expression and not
7554 ;; a label.
7555 (when c-record-type-identifiers
7556 (c-record-ref-id (cons (match-beginning 1) kwd-end)))
7557
7558 ;; Find the label end.
7559 (goto-char kwd-end)
7560 (setq label-type
7561 (if (and (c-syntactic-re-search-forward
7562 ;; Stop on chars that aren't allowed in expressions,
7563 ;; and on operator chars that would be meaningless
7564 ;; there. FIXME: This doesn't cope with ?: operators.
7565 "[;{=,@]\\|\\(\\=\\|[^:]\\):\\([^:]\\|\\'\\)"
7566 limit t t nil 1)
7567 (match-beginning 2))
7568
7569 (progn ; there's a proper :
7570 (goto-char (match-beginning 2)) ; just after the :
7571 (c-put-c-type-property (1- (point)) 'c-decl-end)
7572 t)
7573
7574 ;; It's an unfinished label. We consider the keyword enough
7575 ;; to recognize it as a label, so that it gets fontified.
7576 ;; Leave the point at the end of it, but don't put any
7577 ;; `c-decl-end' marker.
7578 (goto-char kwd-end)
7579 t))))
7580
7581 ;; @private, @protected, @public, in Objective C, or similar.
7582 ((and c-opt-extra-label-key
7583 (looking-at c-opt-extra-label-key))
7584 ;; For a `c-opt-extra-label-key' match, we record the whole
7585 ;; thing for fontification. That's to get the leading '@' in
7586 ;; Objective-C protection labels fontified.
7587 (goto-char (match-end 1))
7588 (when c-record-type-identifiers
7589 (c-record-ref-id (cons (match-beginning 1) (point))))
7590 (c-put-c-type-property (1- (point)) 'c-decl-end)
7591 (setq label-type t))
7592
7593 ;; All other cases of labels.
7594 ((and c-recognize-colon-labels ; nil for AWK and IDL, otherwise t.
7595
7596 ;; A colon label must have something before the colon.
7597 (not (eq (char-after) ?:))
7598
7599 ;; Check that we're not after a token that can't precede a label.
7600 (or
7601 ;; Trivially succeeds when there's no preceding token.
7602 ;; Succeeds when we're at a virtual semicolon.
7603 (if preceding-token-end
7604 (<= preceding-token-end (point-min))
7605 (save-excursion
7606 (c-backward-syntactic-ws)
7607 (setq preceding-token-end (point))
7608 (or (bobp)
7609 (c-at-vsemi-p))))
7610
7611 ;; Check if we're after a label, if we're after a closing
7612 ;; paren that belong to statement, and with
7613 ;; `c-label-prefix-re'. It's done in different order
7614 ;; depending on `assume-markup' since the checks have
7615 ;; different expensiveness.
7616 (if assume-markup
7617 (or
7618 (eq (c-get-char-property (1- preceding-token-end) 'c-type)
7619 'c-decl-end)
7620
7621 (save-excursion
7622 (goto-char (1- preceding-token-end))
7623 (c-beginning-of-current-token)
7624 (or (looking-at c-label-prefix-re)
7625 (looking-at c-block-stmt-1-key)))
7626
7627 (and (eq (char-before preceding-token-end) ?\))
7628 (c-after-conditional)))
7629
7630 (or
7631 (save-excursion
7632 (goto-char (1- preceding-token-end))
7633 (c-beginning-of-current-token)
7634 (or (looking-at c-label-prefix-re)
7635 (looking-at c-block-stmt-1-key)))
7636
7637 (cond
7638 ((eq (char-before preceding-token-end) ?\))
7639 (c-after-conditional))
7640
7641 ((eq (char-before preceding-token-end) ?:)
7642 ;; Might be after another label, so check it recursively.
7643 (save-restriction
7644 (save-excursion
7645 (goto-char (1- preceding-token-end))
7646 ;; Essentially the same as the
7647 ;; `c-syntactic-re-search-forward' regexp below.
7648 (setq macro-start
7649 (save-excursion (and (c-beginning-of-macro)
7650 (point))))
7651 (if macro-start (narrow-to-region macro-start (point-max)))
7652 (c-syntactic-skip-backward "^-]:?;}=*/%&|,<>!@+" nil t)
7653 ;; Note: the following should work instead of the
7654 ;; narrow-to-region above. Investigate why not,
7655 ;; sometime. ACM, 2006-03-31.
7656 ;; (c-syntactic-skip-backward "^-]:?;}=*/%&|,<>!@+"
7657 ;; macro-start t)
7658 (let ((pte (point))
7659 ;; If the caller turned on recording for us,
7660 ;; it shouldn't apply when we check the
7661 ;; preceding label.
7662 c-record-type-identifiers)
7663 ;; A label can't start at a cpp directive. Check for
7664 ;; this, since c-forward-syntactic-ws would foul up on it.
7665 (unless (and c-opt-cpp-prefix (looking-at c-opt-cpp-prefix))
7666 (c-forward-syntactic-ws)
7667 (c-forward-label nil pte start))))))))))
7668
7669 ;; Point is still at the beginning of the possible label construct.
7670 ;;
7671 ;; Check that the next nonsymbol token is ":", or that we're in one
7672 ;; of QT's "slots" declarations. Allow '(' for the sake of macro
7673 ;; arguments. FIXME: Should build this regexp from the language
7674 ;; constants.
7675 (cond
7676 ;; public: protected: private:
7677 ((and
7678 (c-major-mode-is 'c++-mode)
7679 (search-forward-regexp
7680 "\\=p\\(r\\(ivate\\|otected\\)\\|ublic\\)\\>[^_]" nil t)
7681 (progn (backward-char)
7682 (c-forward-syntactic-ws limit)
7683 (looking-at ":\\([^:]\\|\\'\\)"))) ; A single colon.
7684 (forward-char)
7685 (setq label-type t))
7686 ;; QT double keyword like "protected slots:" or goto target.
7687 ((progn (goto-char start) nil))
7688 ((when (c-syntactic-re-search-forward
7689 "[ \t\n[:?;{=*/%&|,<>!@+-]" limit t t) ; not at EOB
7690 (backward-char)
7691 (setq label-end (point))
7692 (setq qt-symbol-idx
7693 (and (c-major-mode-is 'c++-mode)
7694 (string-match
7695 "\\(p\\(r\\(ivate\\|otected\\)\\|ublic\\)\\|more\\)\\>"
7696 (buffer-substring start (point)))))
7697 (c-forward-syntactic-ws limit)
7698 (cond
7699 ((looking-at ":\\([^:]\\|\\'\\)") ; A single colon.
7700 (forward-char)
7701 (setq label-type
7702 (if (or (string= "signals" ; Special QT macro
7703 (setq kwd (buffer-substring-no-properties start label-end)))
7704 (string= "Q_SIGNALS" kwd))
7705 'qt-1kwd-colon
7706 'goto-target)))
7707 ((and qt-symbol-idx
7708 (search-forward-regexp "\\=\\(slots\\|Q_SLOTS\\)\\>" limit t)
7709 (progn (c-forward-syntactic-ws limit)
7710 (looking-at ":\\([^:]\\|\\'\\)"))) ; A single colon
7711 (forward-char)
7712 (setq label-type 'qt-2kwds-colon)))))))
7713
7714 (save-restriction
7715 (narrow-to-region start (point))
7716
7717 ;; Check that `c-nonlabel-token-key' doesn't match anywhere.
7718 (catch 'check-label
7719 (goto-char start)
7720 (while (progn
7721 (when (looking-at c-nonlabel-token-key)
7722 (goto-char start)
7723 (setq label-type nil)
7724 (throw 'check-label nil))
7725 (and (c-safe (c-forward-sexp)
7726 (c-forward-syntactic-ws)
7727 t)
7728 (not (eobp)))))
7729
7730 ;; Record the identifiers in the label for fontification, unless
7731 ;; it begins with `c-label-kwds' in which case the following
7732 ;; identifiers are part of a (constant) expression that
7733 ;; shouldn't be fontified.
7734 (when (and c-record-type-identifiers
7735 (progn (goto-char start)
7736 (not (looking-at c-label-kwds-regexp))))
7737 (while (c-syntactic-re-search-forward c-symbol-key nil t)
7738 (c-record-ref-id (cons (match-beginning 0)
7739 (match-end 0)))))
7740
7741 (c-put-c-type-property (1- (point-max)) 'c-decl-end)
7742 (goto-char (point-max)))))
7743
7744 (t
7745 ;; Not a label.
7746 (goto-char start)))
7747 label-type))
7748
7749 (defun c-forward-objc-directive ()
7750 ;; Assuming the point is at the beginning of a token, try to move
7751 ;; forward to the end of the Objective-C directive that starts
7752 ;; there. Return t if a directive was fully recognized, otherwise
7753 ;; the point is moved as far as one could be successfully parsed and
7754 ;; nil is returned.
7755 ;;
7756 ;; This function records identifier ranges on
7757 ;; `c-record-type-identifiers' and `c-record-ref-identifiers' if
7758 ;; `c-record-type-identifiers' is non-nil.
7759 ;;
7760 ;; This function might do hidden buffer changes.
7761
7762 (let ((start (point))
7763 start-char
7764 (c-promote-possible-types t)
7765 lim
7766 ;; Turn off recognition of angle bracket arglists while parsing
7767 ;; types here since the protocol reference list might then be
7768 ;; considered part of the preceding name or superclass-name.
7769 c-recognize-<>-arglists)
7770
7771 (if (or
7772 (when (looking-at
7773 (eval-when-compile
7774 (c-make-keywords-re t
7775 (append (c-lang-const c-protection-kwds objc)
7776 '("@end"))
7777 'objc-mode)))
7778 (goto-char (match-end 1))
7779 t)
7780
7781 (and
7782 (looking-at
7783 (eval-when-compile
7784 (c-make-keywords-re t
7785 '("@interface" "@implementation" "@protocol")
7786 'objc-mode)))
7787
7788 ;; Handle the name of the class itself.
7789 (progn
7790 ; (c-forward-token-2) ; 2006/1/13 This doesn't move if the token's
7791 ; at EOB.
7792 (goto-char (match-end 0))
7793 (setq lim (point))
7794 (c-skip-ws-forward)
7795 (c-forward-type))
7796
7797 (catch 'break
7798 ;; Look for ": superclass-name" or "( category-name )".
7799 (when (looking-at "[:\(]")
7800 (setq start-char (char-after))
7801 (forward-char)
7802 (c-forward-syntactic-ws)
7803 (unless (c-forward-type) (throw 'break nil))
7804 (when (eq start-char ?\()
7805 (unless (eq (char-after) ?\)) (throw 'break nil))
7806 (forward-char)
7807 (c-forward-syntactic-ws)))
7808
7809 ;; Look for a protocol reference list.
7810 (if (eq (char-after) ?<)
7811 (let ((c-recognize-<>-arglists t)
7812 (c-parse-and-markup-<>-arglists t)
7813 c-restricted-<>-arglists)
7814 (c-forward-<>-arglist t))
7815 t))))
7816
7817 (progn
7818 (c-backward-syntactic-ws lim)
7819 (c-clear-c-type-property start (1- (point)) 'c-decl-end)
7820 (c-put-c-type-property (1- (point)) 'c-decl-end)
7821 t)
7822
7823 (c-clear-c-type-property start (point) 'c-decl-end)
7824 nil)))
7825
7826 (defun c-beginning-of-inheritance-list (&optional lim)
7827 ;; Go to the first non-whitespace after the colon that starts a
7828 ;; multiple inheritance introduction. Optional LIM is the farthest
7829 ;; back we should search.
7830 ;;
7831 ;; This function might do hidden buffer changes.
7832 (c-with-syntax-table c++-template-syntax-table
7833 (c-backward-token-2 0 t lim)
7834 (while (and (or (looking-at c-symbol-start)
7835 (looking-at "[<,]\\|::"))
7836 (zerop (c-backward-token-2 1 t lim))))))
7837
7838 (defun c-in-method-def-p ()
7839 ;; Return nil if we aren't in a method definition, otherwise the
7840 ;; position of the initial [+-].
7841 ;;
7842 ;; This function might do hidden buffer changes.
7843 (save-excursion
7844 (beginning-of-line)
7845 (and c-opt-method-key
7846 (looking-at c-opt-method-key)
7847 (point))
7848 ))
7849
7850 ;; Contributed by Kevin Ryde <user42@zip.com.au>.
7851 (defun c-in-gcc-asm-p ()
7852 ;; Return non-nil if point is within a gcc \"asm\" block.
7853 ;;
7854 ;; This should be called with point inside an argument list.
7855 ;;
7856 ;; Only one level of enclosing parentheses is considered, so for
7857 ;; instance `nil' is returned when in a function call within an asm
7858 ;; operand.
7859 ;;
7860 ;; This function might do hidden buffer changes.
7861
7862 (and c-opt-asm-stmt-key
7863 (save-excursion
7864 (beginning-of-line)
7865 (backward-up-list 1)
7866 (c-beginning-of-statement-1 (point-min) nil t)
7867 (looking-at c-opt-asm-stmt-key))))
7868
7869 (defun c-at-toplevel-p ()
7870 "Return a determination as to whether point is \"at the top level\".
7871 Informally, \"at the top level\" is anywhere where you can write
7872 a function.
7873
7874 More precisely, being at the top-level means that point is either
7875 outside any enclosing block (such as a function definition), or
7876 directly inside a class, namespace or other block that contains
7877 another declaration level.
7878
7879 If point is not at the top-level (e.g. it is inside a method
7880 definition), then nil is returned. Otherwise, if point is at a
7881 top-level not enclosed within a class definition, t is returned.
7882 Otherwise, a 2-vector is returned where the zeroth element is the
7883 buffer position of the start of the class declaration, and the first
7884 element is the buffer position of the enclosing class's opening
7885 brace.
7886
7887 Note that this function might do hidden buffer changes. See the
7888 comment at the start of cc-engine.el for more info."
7889 (let ((paren-state (c-parse-state)))
7890 (or (not (c-most-enclosing-brace paren-state))
7891 (c-search-uplist-for-classkey paren-state))))
7892
7893 (defun c-just-after-func-arglist-p (&optional lim)
7894 ;; Return non-nil if the point is in the region after the argument
7895 ;; list of a function and its opening brace (or semicolon in case it
7896 ;; got no body). If there are K&R style argument declarations in
7897 ;; that region, the point has to be inside the first one for this
7898 ;; function to recognize it.
7899 ;;
7900 ;; If successful, the point is moved to the first token after the
7901 ;; function header (see `c-forward-decl-or-cast-1' for details) and
7902 ;; the position of the opening paren of the function arglist is
7903 ;; returned.
7904 ;;
7905 ;; The point is clobbered if not successful.
7906 ;;
7907 ;; LIM is used as bound for backward buffer searches.
7908 ;;
7909 ;; This function might do hidden buffer changes.
7910
7911 (let ((beg (point)) end id-start)
7912 (and
7913 (eq (c-beginning-of-statement-1 lim) 'same)
7914
7915 (not (and (c-major-mode-is 'objc-mode)
7916 (c-forward-objc-directive)))
7917
7918 (setq id-start
7919 (car-safe (c-forward-decl-or-cast-1 (c-point 'bosws) nil nil)))
7920 (< id-start beg)
7921
7922 ;; There should not be a '=' or ',' between beg and the
7923 ;; start of the declaration since that means we were in the
7924 ;; "expression part" of the declaration.
7925 (or (> (point) beg)
7926 (not (looking-at "[=,]")))
7927
7928 (save-excursion
7929 ;; Check that there's an arglist paren in the
7930 ;; declaration.
7931 (goto-char id-start)
7932 (cond ((eq (char-after) ?\()
7933 ;; The declarator is a paren expression, so skip past it
7934 ;; so that we don't get stuck on that instead of the
7935 ;; function arglist.
7936 (c-forward-sexp))
7937 ((and c-opt-op-identifier-prefix
7938 (looking-at c-opt-op-identifier-prefix))
7939 ;; Don't trip up on "operator ()".
7940 (c-forward-token-2 2 t)))
7941 (and (< (point) beg)
7942 (c-syntactic-re-search-forward "(" beg t t)
7943 (1- (point)))))))
7944
7945 (defun c-in-knr-argdecl (&optional lim)
7946 ;; Return the position of the first argument declaration if point is
7947 ;; inside a K&R style argument declaration list, nil otherwise.
7948 ;; `c-recognize-knr-p' is not checked. If LIM is non-nil, it's a
7949 ;; position that bounds the backward search for the argument list.
7950 ;;
7951 ;; Point must be within a possible K&R region, e.g. just before a top-level
7952 ;; "{". It must be outside of parens and brackets. The test can return
7953 ;; false positives otherwise.
7954 ;;
7955 ;; This function might do hidden buffer changes.
7956
7957 (save-excursion
7958 (save-restriction
7959 ;; If we're in a macro, our search range is restricted to it. Narrow to
7960 ;; the searchable range.
7961 (let* ((macro-start (save-excursion (and (c-beginning-of-macro) (point))))
7962 (macro-end (save-excursion (and macro-start (c-end-of-macro) (point))))
7963 (low-lim (max (or lim (point-min)) (or macro-start (point-min))))
7964 before-lparen after-rparen
7965 (pp-count-out 20)) ; Max number of paren/brace constructs before
7966 ; we give up
7967 (narrow-to-region low-lim (or macro-end (point-max)))
7968
7969 ;; Search backwards for the defun's argument list. We give up if we
7970 ;; encounter a "}" (end of a previous defun) an "=" (which can't be in
7971 ;; a knr region) or BOB.
7972 ;;
7973 ;; The criterion for a paren structure being the arg list is:
7974 ;; o - there is non-WS stuff after it but before any "{"; AND
7975 ;; o - the token after it isn't a ";" AND
7976 ;; o - it is preceded by either an identifier (the function name) or
7977 ;; a macro expansion like "DEFUN (...)"; AND
7978 ;; o - its content is a non-empty comma-separated list of identifiers
7979 ;; (an empty arg list won't have a knr region).
7980 ;;
7981 ;; The following snippet illustrates these rules:
7982 ;; int foo (bar, baz, yuk)
7983 ;; int bar [] ;
7984 ;; int (*baz) (my_type) ;
7985 ;; int (*) (void) (*yuk) (void) ;
7986 ;; {
7987
7988 (catch 'knr
7989 (while (> pp-count-out 0) ; go back one paren/bracket pair each time.
7990 (setq pp-count-out (1- pp-count-out))
7991 (c-syntactic-skip-backward "^)]}=")
7992 (cond ((eq (char-before) ?\))
7993 (setq after-rparen (point)))
7994 ((eq (char-before) ?\])
7995 (setq after-rparen nil))
7996 (t ; either } (hit previous defun) or = or no more
7997 ; parens/brackets.
7998 (throw 'knr nil)))
7999
8000 (if after-rparen
8001 ;; We're inside a paren. Could it be our argument list....?
8002 (if
8003 (and
8004 (progn
8005 (goto-char after-rparen)
8006 (unless (c-go-list-backward) (throw 'knr nil)) ;
8007 ;; FIXME!!! What about macros between the parens? 2007/01/20
8008 (setq before-lparen (point)))
8009
8010 ;; It can't be the arg list if next token is ; or {
8011 (progn (goto-char after-rparen)
8012 (c-forward-syntactic-ws)
8013 (not (memq (char-after) '(?\; ?\{ ?\=))))
8014
8015 ;; Is the thing preceding the list an identifier (the
8016 ;; function name), or a macro expansion?
8017 (progn
8018 (goto-char before-lparen)
8019 (eq (c-backward-token-2) 0)
8020 (or (eq (c-on-identifier) (point))
8021 (and (eq (char-after) ?\))
8022 (c-go-up-list-backward)
8023 (eq (c-backward-token-2) 0)
8024 (eq (c-on-identifier) (point)))))
8025
8026 ;; Have we got a non-empty list of comma-separated
8027 ;; identifiers?
8028 (progn
8029 (goto-char before-lparen)
8030 (c-forward-token-2) ; to first token inside parens
8031 (and
8032 (c-on-identifier)
8033 (c-forward-token-2)
8034 (catch 'id-list
8035 (while (eq (char-after) ?\,)
8036 (c-forward-token-2)
8037 (unless (c-on-identifier) (throw 'id-list nil))
8038 (c-forward-token-2))
8039 (eq (char-after) ?\))))))
8040
8041 ;; ...Yes. We've identified the function's argument list.
8042 (throw 'knr
8043 (progn (goto-char after-rparen)
8044 (c-forward-syntactic-ws)
8045 (point)))
8046
8047 ;; ...No. The current parens aren't the function's arg list.
8048 (goto-char before-lparen))
8049
8050 (or (c-go-list-backward) ; backwards over [ .... ]
8051 (throw 'knr nil)))))))))
8052
8053 (defun c-skip-conditional ()
8054 ;; skip forward over conditional at point, including any predicate
8055 ;; statements in parentheses. No error checking is performed.
8056 ;;
8057 ;; This function might do hidden buffer changes.
8058 (c-forward-sexp (cond
8059 ;; else if()
8060 ((looking-at (concat "\\<else"
8061 "\\([ \t\n]\\|\\\\\n\\)+"
8062 "if\\>\\([^_]\\|$\\)"))
8063 3)
8064 ;; do, else, try, finally
8065 ((looking-at (concat "\\<\\("
8066 "do\\|else\\|try\\|finally"
8067 "\\)\\>\\([^_]\\|$\\)"))
8068 1)
8069 ;; for, if, while, switch, catch, synchronized, foreach
8070 (t 2))))
8071
8072 (defun c-after-conditional (&optional lim)
8073 ;; If looking at the token after a conditional then return the
8074 ;; position of its start, otherwise return nil.
8075 ;;
8076 ;; This function might do hidden buffer changes.
8077 (save-excursion
8078 (and (zerop (c-backward-token-2 1 t lim))
8079 (or (looking-at c-block-stmt-1-key)
8080 (and (eq (char-after) ?\()
8081 (zerop (c-backward-token-2 1 t lim))
8082 (or (looking-at c-block-stmt-2-key)
8083 (looking-at c-block-stmt-1-2-key))))
8084 (point))))
8085
8086 (defun c-after-special-operator-id (&optional lim)
8087 ;; If the point is after an operator identifier that isn't handled
8088 ;; like an ordinary symbol (i.e. like "operator =" in C++) then the
8089 ;; position of the start of that identifier is returned. nil is
8090 ;; returned otherwise. The point may be anywhere in the syntactic
8091 ;; whitespace after the last token of the operator identifier.
8092 ;;
8093 ;; This function might do hidden buffer changes.
8094 (save-excursion
8095 (and c-overloadable-operators-regexp
8096 (zerop (c-backward-token-2 1 nil lim))
8097 (looking-at c-overloadable-operators-regexp)
8098 (or (not c-opt-op-identifier-prefix)
8099 (and
8100 (zerop (c-backward-token-2 1 nil lim))
8101 (looking-at c-opt-op-identifier-prefix)))
8102 (point))))
8103
8104 (defsubst c-backward-to-block-anchor (&optional lim)
8105 ;; Assuming point is at a brace that opens a statement block of some
8106 ;; kind, move to the proper anchor point for that block. It might
8107 ;; need to be adjusted further by c-add-stmt-syntax, but the
8108 ;; position at return is suitable as start position for that
8109 ;; function.
8110 ;;
8111 ;; This function might do hidden buffer changes.
8112 (unless (= (point) (c-point 'boi))
8113 (let ((start (c-after-conditional lim)))
8114 (if start
8115 (goto-char start)))))
8116
8117 (defsubst c-backward-to-decl-anchor (&optional lim)
8118 ;; Assuming point is at a brace that opens the block of a top level
8119 ;; declaration of some kind, move to the proper anchor point for
8120 ;; that block.
8121 ;;
8122 ;; This function might do hidden buffer changes.
8123 (unless (= (point) (c-point 'boi))
8124 (c-beginning-of-statement-1 lim)))
8125
8126 (defun c-search-decl-header-end ()
8127 ;; Search forward for the end of the "header" of the current
8128 ;; declaration. That's the position where the definition body
8129 ;; starts, or the first variable initializer, or the ending
8130 ;; semicolon. I.e. search forward for the closest following
8131 ;; (syntactically relevant) '{', '=' or ';' token. Point is left
8132 ;; _after_ the first found token, or at point-max if none is found.
8133 ;;
8134 ;; This function might do hidden buffer changes.
8135
8136 (let ((base (point)))
8137 (if (c-major-mode-is 'c++-mode)
8138
8139 ;; In C++ we need to take special care to handle operator
8140 ;; tokens and those pesky template brackets.
8141 (while (and
8142 (c-syntactic-re-search-forward "[;{<=]" nil 'move t t)
8143 (or
8144 (c-end-of-current-token base)
8145 ;; Handle operator identifiers, i.e. ignore any
8146 ;; operator token preceded by "operator".
8147 (save-excursion
8148 (and (c-safe (c-backward-sexp) t)
8149 (looking-at c-opt-op-identifier-prefix)))
8150 (and (eq (char-before) ?<)
8151 (c-with-syntax-table c++-template-syntax-table
8152 (if (c-safe (goto-char (c-up-list-forward (point))))
8153 t
8154 (goto-char (point-max))
8155 nil)))))
8156 (setq base (point)))
8157
8158 (while (and
8159 (c-syntactic-re-search-forward "[;{=]" nil 'move t t)
8160 (c-end-of-current-token base))
8161 (setq base (point))))))
8162
8163 (defun c-beginning-of-decl-1 (&optional lim)
8164 ;; Go to the beginning of the current declaration, or the beginning
8165 ;; of the previous one if already at the start of it. Point won't
8166 ;; be moved out of any surrounding paren. Return a cons cell of the
8167 ;; form (MOVE . KNR-POS). MOVE is like the return value from
8168 ;; `c-beginning-of-statement-1'. If point skipped over some K&R
8169 ;; style argument declarations (and they are to be recognized) then
8170 ;; KNR-POS is set to the start of the first such argument
8171 ;; declaration, otherwise KNR-POS is nil. If LIM is non-nil, it's a
8172 ;; position that bounds the backward search.
8173 ;;
8174 ;; NB: Cases where the declaration continues after the block, as in
8175 ;; "struct foo { ... } bar;", are currently recognized as two
8176 ;; declarations, e.g. "struct foo { ... }" and "bar;" in this case.
8177 ;;
8178 ;; This function might do hidden buffer changes.
8179 (catch 'return
8180 (let* ((start (point))
8181 (last-stmt-start (point))
8182 (move (c-beginning-of-statement-1 lim nil t)))
8183
8184 ;; `c-beginning-of-statement-1' stops at a block start, but we
8185 ;; want to continue if the block doesn't begin a top level
8186 ;; construct, i.e. if it isn't preceded by ';', '}', ':', bob,
8187 ;; or an open paren.
8188 (let ((beg (point)) tentative-move)
8189 ;; Go back one "statement" each time round the loop until we're just
8190 ;; after a ;, }, or :, or at BOB or the start of a macro or start of
8191 ;; an ObjC method. This will move over a multiple declaration whose
8192 ;; components are comma separated.
8193 (while (and
8194 ;; Must check with c-opt-method-key in ObjC mode.
8195 (not (and c-opt-method-key
8196 (looking-at c-opt-method-key)))
8197 (/= last-stmt-start (point))
8198 (progn
8199 (c-backward-syntactic-ws lim)
8200 (not (memq (char-before) '(?\; ?} ?: nil))))
8201 (save-excursion
8202 (backward-char)
8203 (not (looking-at "\\s(")))
8204 ;; Check that we don't move from the first thing in a
8205 ;; macro to its header.
8206 (not (eq (setq tentative-move
8207 (c-beginning-of-statement-1 lim nil t))
8208 'macro)))
8209 (setq last-stmt-start beg
8210 beg (point)
8211 move tentative-move))
8212 (goto-char beg))
8213
8214 (when c-recognize-knr-p
8215 (let ((fallback-pos (point)) knr-argdecl-start)
8216 ;; Handle K&R argdecls. Back up after the "statement" jumped
8217 ;; over by `c-beginning-of-statement-1', unless it was the
8218 ;; function body, in which case we're sitting on the opening
8219 ;; brace now. Then test if we're in a K&R argdecl region and
8220 ;; that we started at the other side of the first argdecl in
8221 ;; it.
8222 (unless (eq (char-after) ?{)
8223 (goto-char last-stmt-start))
8224 (if (and (setq knr-argdecl-start (c-in-knr-argdecl lim))
8225 (< knr-argdecl-start start)
8226 (progn
8227 (goto-char knr-argdecl-start)
8228 (not (eq (c-beginning-of-statement-1 lim nil t) 'macro))))
8229 (throw 'return
8230 (cons (if (eq (char-after fallback-pos) ?{)
8231 'previous
8232 'same)
8233 knr-argdecl-start))
8234 (goto-char fallback-pos))))
8235
8236 ;; `c-beginning-of-statement-1' counts each brace block as a separate
8237 ;; statement, so the result will be 'previous if we've moved over any.
8238 ;; So change our result back to 'same if necessary.
8239 ;;
8240 ;; If they were brace list initializers we might not have moved over a
8241 ;; declaration boundary though, so change it to 'same if we've moved
8242 ;; past a '=' before '{', but not ';'. (This ought to be integrated
8243 ;; into `c-beginning-of-statement-1', so we avoid this extra pass which
8244 ;; potentially can search over a large amount of text.). Take special
8245 ;; pains not to get mislead by C++'s "operator=", and the like.
8246 (if (and (eq move 'previous)
8247 (c-with-syntax-table (if (c-major-mode-is 'c++-mode)
8248 c++-template-syntax-table
8249 (syntax-table))
8250 (save-excursion
8251 (and
8252 (progn
8253 (while ; keep going back to "[;={"s until we either find
8254 ; no more, or get to one which isn't an "operator ="
8255 (and (c-syntactic-re-search-forward "[;={]" start t t t)
8256 (eq (char-before) ?=)
8257 c-overloadable-operators-regexp
8258 c-opt-op-identifier-prefix
8259 (save-excursion
8260 (eq (c-backward-token-2) 0)
8261 (looking-at c-overloadable-operators-regexp)
8262 (eq (c-backward-token-2) 0)
8263 (looking-at c-opt-op-identifier-prefix))))
8264 (eq (char-before) ?=))
8265 (c-syntactic-re-search-forward "[;{]" start t t)
8266 (eq (char-before) ?{)
8267 (c-safe (goto-char (c-up-list-forward (point))) t)
8268 (not (c-syntactic-re-search-forward ";" start t t))))))
8269 (cons 'same nil)
8270 (cons move nil)))))
8271
8272 (defun c-end-of-decl-1 ()
8273 ;; Assuming point is at the start of a declaration (as detected by
8274 ;; e.g. `c-beginning-of-decl-1'), go to the end of it. Unlike
8275 ;; `c-beginning-of-decl-1', this function handles the case when a
8276 ;; block is followed by identifiers in e.g. struct declarations in C
8277 ;; or C++. If a proper end was found then t is returned, otherwise
8278 ;; point is moved as far as possible within the current sexp and nil
8279 ;; is returned. This function doesn't handle macros; use
8280 ;; `c-end-of-macro' instead in those cases.
8281 ;;
8282 ;; This function might do hidden buffer changes.
8283 (let ((start (point))
8284 (decl-syntax-table (if (c-major-mode-is 'c++-mode)
8285 c++-template-syntax-table
8286 (syntax-table))))
8287 (catch 'return
8288 (c-search-decl-header-end)
8289
8290 (when (and c-recognize-knr-p
8291 (eq (char-before) ?\;)
8292 (c-in-knr-argdecl start))
8293 ;; Stopped at the ';' in a K&R argdecl section which is
8294 ;; detected using the same criteria as in
8295 ;; `c-beginning-of-decl-1'. Move to the following block
8296 ;; start.
8297 (c-syntactic-re-search-forward "{" nil 'move t))
8298
8299 (when (eq (char-before) ?{)
8300 ;; Encountered a block in the declaration. Jump over it.
8301 (condition-case nil
8302 (goto-char (c-up-list-forward (point)))
8303 (error (goto-char (point-max))
8304 (throw 'return nil)))
8305 (if (or (not c-opt-block-decls-with-vars-key)
8306 (save-excursion
8307 (c-with-syntax-table decl-syntax-table
8308 (let ((lim (point)))
8309 (goto-char start)
8310 (not (and
8311 ;; Check for `c-opt-block-decls-with-vars-key'
8312 ;; before the first paren.
8313 (c-syntactic-re-search-forward
8314 (concat "[;=\(\[{]\\|\\("
8315 c-opt-block-decls-with-vars-key
8316 "\\)")
8317 lim t t t)
8318 (match-beginning 1)
8319 (not (eq (char-before) ?_))
8320 ;; Check that the first following paren is
8321 ;; the block.
8322 (c-syntactic-re-search-forward "[;=\(\[{]"
8323 lim t t t)
8324 (eq (char-before) ?{)))))))
8325 ;; The declaration doesn't have any of the
8326 ;; `c-opt-block-decls-with-vars' keywords in the
8327 ;; beginning, so it ends here at the end of the block.
8328 (throw 'return t)))
8329
8330 (c-with-syntax-table decl-syntax-table
8331 (while (progn
8332 (if (eq (char-before) ?\;)
8333 (throw 'return t))
8334 (c-syntactic-re-search-forward ";" nil 'move t))))
8335 nil)))
8336
8337 (defun c-looking-at-decl-block (containing-sexp goto-start &optional limit)
8338 ;; Assuming the point is at an open brace, check if it starts a
8339 ;; block that contains another declaration level, i.e. that isn't a
8340 ;; statement block or a brace list, and if so return non-nil.
8341 ;;
8342 ;; If the check is successful, the return value is the start of the
8343 ;; keyword that tells what kind of construct it is, i.e. typically
8344 ;; what `c-decl-block-key' matched. Also, if GOTO-START is set then
8345 ;; the point will be at the start of the construct, before any
8346 ;; leading specifiers, otherwise it's at the returned position.
8347 ;;
8348 ;; The point is clobbered if the check is unsuccessful.
8349 ;;
8350 ;; CONTAINING-SEXP is the position of the open of the surrounding
8351 ;; paren, or nil if none.
8352 ;;
8353 ;; The optional LIMIT limits the backward search for the start of
8354 ;; the construct. It's assumed to be at a syntactically relevant
8355 ;; position.
8356 ;;
8357 ;; If any template arglists are found in the searched region before
8358 ;; the open brace, they get marked with paren syntax.
8359 ;;
8360 ;; This function might do hidden buffer changes.
8361
8362 (let ((open-brace (point)) kwd-start first-specifier-pos)
8363 (c-syntactic-skip-backward c-block-prefix-charset limit t)
8364
8365 (when (and c-recognize-<>-arglists
8366 (eq (char-before) ?>))
8367 ;; Could be at the end of a template arglist.
8368 (let ((c-parse-and-markup-<>-arglists t)
8369 (c-disallow-comma-in-<>-arglists
8370 (and containing-sexp
8371 (not (eq (char-after containing-sexp) ?{)))))
8372 (while (and
8373 (c-backward-<>-arglist nil limit)
8374 (progn
8375 (c-syntactic-skip-backward c-block-prefix-charset limit t)
8376 (eq (char-before) ?>))))))
8377
8378 ;; Note: Can't get bogus hits inside template arglists below since they
8379 ;; have gotten paren syntax above.
8380 (when (and
8381 ;; If `goto-start' is set we begin by searching for the
8382 ;; first possible position of a leading specifier list.
8383 ;; The `c-decl-block-key' search continues from there since
8384 ;; we know it can't match earlier.
8385 (if goto-start
8386 (when (c-syntactic-re-search-forward c-symbol-start
8387 open-brace t t)
8388 (goto-char (setq first-specifier-pos (match-beginning 0)))
8389 t)
8390 t)
8391
8392 (cond
8393 ((c-syntactic-re-search-forward c-decl-block-key open-brace t t t)
8394 (goto-char (setq kwd-start (match-beginning 0)))
8395 (or
8396
8397 ;; Found a keyword that can't be a type?
8398 (match-beginning 1)
8399
8400 ;; Can be a type too, in which case it's the return type of a
8401 ;; function (under the assumption that no declaration level
8402 ;; block construct starts with a type).
8403 (not (c-forward-type))
8404
8405 ;; Jumped over a type, but it could be a declaration keyword
8406 ;; followed by the declared identifier that we've jumped over
8407 ;; instead (e.g. in "class Foo {"). If it indeed is a type
8408 ;; then we should be at the declarator now, so check for a
8409 ;; valid declarator start.
8410 ;;
8411 ;; Note: This doesn't cope with the case when a declared
8412 ;; identifier is followed by e.g. '(' in a language where '('
8413 ;; also might be part of a declarator expression. Currently
8414 ;; there's no such language.
8415 (not (or (looking-at c-symbol-start)
8416 (looking-at c-type-decl-prefix-key)))))
8417
8418 ;; In Pike a list of modifiers may be followed by a brace
8419 ;; to make them apply to many identifiers. Note that the
8420 ;; match data will be empty on return in this case.
8421 ((and (c-major-mode-is 'pike-mode)
8422 (progn
8423 (goto-char open-brace)
8424 (= (c-backward-token-2) 0))
8425 (looking-at c-specifier-key)
8426 ;; Use this variant to avoid yet another special regexp.
8427 (c-keyword-member (c-keyword-sym (match-string 1))
8428 'c-modifier-kwds))
8429 (setq kwd-start (point))
8430 t)))
8431
8432 ;; Got a match.
8433
8434 (if goto-start
8435 ;; Back up over any preceding specifiers and their clauses
8436 ;; by going forward from `first-specifier-pos', which is the
8437 ;; earliest possible position where the specifier list can
8438 ;; start.
8439 (progn
8440 (goto-char first-specifier-pos)
8441
8442 (while (< (point) kwd-start)
8443 (if (looking-at c-symbol-key)
8444 ;; Accept any plain symbol token on the ground that
8445 ;; it's a specifier masked through a macro (just
8446 ;; like `c-forward-decl-or-cast-1' skip forward over
8447 ;; such tokens).
8448 ;;
8449 ;; Could be more restrictive wrt invalid keywords,
8450 ;; but that'd only occur in invalid code so there's
8451 ;; no use spending effort on it.
8452 (let ((end (match-end 0)))
8453 (unless (c-forward-keyword-clause 0)
8454 (goto-char end)
8455 (c-forward-syntactic-ws)))
8456
8457 ;; Can't parse a declaration preamble and is still
8458 ;; before `kwd-start'. That means `first-specifier-pos'
8459 ;; was in some earlier construct. Search again.
8460 (if (c-syntactic-re-search-forward c-symbol-start
8461 kwd-start 'move t)
8462 (goto-char (setq first-specifier-pos (match-beginning 0)))
8463 ;; Got no preamble before the block declaration keyword.
8464 (setq first-specifier-pos kwd-start))))
8465
8466 (goto-char first-specifier-pos))
8467 (goto-char kwd-start))
8468
8469 kwd-start)))
8470
8471 (defun c-search-uplist-for-classkey (paren-state)
8472 ;; Check if the closest containing paren sexp is a declaration
8473 ;; block, returning a 2 element vector in that case. Aref 0
8474 ;; contains the bufpos at boi of the class key line, and aref 1
8475 ;; contains the bufpos of the open brace. This function is an
8476 ;; obsolete wrapper for `c-looking-at-decl-block'.
8477 ;;
8478 ;; This function might do hidden buffer changes.
8479 (let ((open-paren-pos (c-most-enclosing-brace paren-state)))
8480 (when open-paren-pos
8481 (save-excursion
8482 (goto-char open-paren-pos)
8483 (when (and (eq (char-after) ?{)
8484 (c-looking-at-decl-block
8485 (c-safe-position open-paren-pos paren-state)
8486 nil))
8487 (back-to-indentation)
8488 (vector (point) open-paren-pos))))))
8489
8490 (defun c-most-enclosing-decl-block (paren-state)
8491 ;; Return the buffer position of the most enclosing decl-block brace (in the
8492 ;; sense of c-looking-at-decl-block) in the PAREN-STATE structure, or nil if
8493 ;; none was found.
8494 (let* ((open-brace (c-pull-open-brace paren-state))
8495 (next-open-brace (c-pull-open-brace paren-state)))
8496 (while (and open-brace
8497 (save-excursion
8498 (goto-char open-brace)
8499 (not (c-looking-at-decl-block next-open-brace nil))))
8500 (setq open-brace next-open-brace
8501 next-open-brace (c-pull-open-brace paren-state)))
8502 open-brace))
8503
8504 (defun c-cheap-inside-bracelist-p (paren-state)
8505 ;; Return the position of the L-brace if point is inside a brace list
8506 ;; initialization of an array, etc. This is an approximate function,
8507 ;; designed for speed over accuracy. It will not find every bracelist, but
8508 ;; a non-nil result is reliable. We simply search for "= {" (naturally with
8509 ;; syntactic whitespace allowed). PAREN-STATE is the normal thing that it
8510 ;; is everywhere else.
8511 (let (b-pos)
8512 (save-excursion
8513 (while
8514 (and (setq b-pos (c-pull-open-brace paren-state))
8515 (progn (goto-char b-pos)
8516 (c-backward-sws)
8517 (c-backward-token-2)
8518 (not (looking-at "=")))))
8519 b-pos)))
8520
8521 (defun c-backward-over-enum-header ()
8522 ;; We're at a "{". Move back to the enum-like keyword that starts this
8523 ;; declaration and return t, otherwise don't move and return nil.
8524 (let ((here (point))
8525 up-sexp-pos before-identifier)
8526 (while
8527 (and
8528 (eq (c-backward-token-2) 0)
8529 (or (not (looking-at "\\s)"))
8530 (c-go-up-list-backward))
8531 (cond
8532 ((and (looking-at c-symbol-key) (c-on-identifier)
8533 (not before-identifier))
8534 (setq before-identifier t))
8535 ((and before-identifier
8536 (or (eq (char-after) ?,)
8537 (looking-at c-postfix-decl-spec-key)))
8538 (setq before-identifier nil)
8539 t)
8540 ((looking-at c-brace-list-key) nil)
8541 ((and c-recognize-<>-arglists
8542 (eq (char-after) ?<)
8543 (looking-at "\\s("))
8544 t)
8545 (t nil))))
8546 (or (looking-at c-brace-list-key)
8547 (progn (goto-char here) nil))))
8548
8549 (defun c-inside-bracelist-p (containing-sexp paren-state)
8550 ;; return the buffer position of the beginning of the brace list
8551 ;; statement if we're inside a brace list, otherwise return nil.
8552 ;; CONTAINING-SEXP is the buffer pos of the innermost containing
8553 ;; paren. PAREN-STATE is the remainder of the state of enclosing
8554 ;; braces
8555 ;;
8556 ;; N.B.: This algorithm can potentially get confused by cpp macros
8557 ;; placed in inconvenient locations. It's a trade-off we make for
8558 ;; speed.
8559 ;;
8560 ;; This function might do hidden buffer changes.
8561 (or
8562 ;; This will pick up brace list declarations.
8563 (save-excursion
8564 (goto-char containing-sexp)
8565 (c-backward-over-enum-header))
8566 ;; this will pick up array/aggregate init lists, even if they are nested.
8567 (save-excursion
8568 (let ((class-key
8569 ;; Pike can have class definitions anywhere, so we must
8570 ;; check for the class key here.
8571 (and (c-major-mode-is 'pike-mode)
8572 c-decl-block-key))
8573 bufpos braceassignp lim next-containing macro-start)
8574 (while (and (not bufpos)
8575 containing-sexp)
8576 (when paren-state
8577 (if (consp (car paren-state))
8578 (setq lim (cdr (car paren-state))
8579 paren-state (cdr paren-state))
8580 (setq lim (car paren-state)))
8581 (when paren-state
8582 (setq next-containing (car paren-state)
8583 paren-state (cdr paren-state))))
8584 (goto-char containing-sexp)
8585 (if (c-looking-at-inexpr-block next-containing next-containing)
8586 ;; We're in an in-expression block of some kind. Do not
8587 ;; check nesting. We deliberately set the limit to the
8588 ;; containing sexp, so that c-looking-at-inexpr-block
8589 ;; doesn't check for an identifier before it.
8590 (setq containing-sexp nil)
8591 ;; see if the open brace is preceded by = or [...] in
8592 ;; this statement, but watch out for operator=
8593 (setq braceassignp 'dontknow)
8594 (c-backward-token-2 1 t lim)
8595 ;; Checks to do only on the first sexp before the brace.
8596 (when (and c-opt-inexpr-brace-list-key
8597 (eq (char-after) ?\[))
8598 ;; In Java, an initialization brace list may follow
8599 ;; directly after "new Foo[]", so check for a "new"
8600 ;; earlier.
8601 (while (eq braceassignp 'dontknow)
8602 (setq braceassignp
8603 (cond ((/= (c-backward-token-2 1 t lim) 0) nil)
8604 ((looking-at c-opt-inexpr-brace-list-key) t)
8605 ((looking-at "\\sw\\|\\s_\\|[.[]")
8606 ;; Carry on looking if this is an
8607 ;; identifier (may contain "." in Java)
8608 ;; or another "[]" sexp.
8609 'dontknow)
8610 (t nil)))))
8611 ;; Checks to do on all sexps before the brace, up to the
8612 ;; beginning of the statement.
8613 (while (eq braceassignp 'dontknow)
8614 (cond ((eq (char-after) ?\;)
8615 (setq braceassignp nil))
8616 ((and class-key
8617 (looking-at class-key))
8618 (setq braceassignp nil))
8619 ((eq (char-after) ?=)
8620 ;; We've seen a =, but must check earlier tokens so
8621 ;; that it isn't something that should be ignored.
8622 (setq braceassignp 'maybe)
8623 (while (and (eq braceassignp 'maybe)
8624 (zerop (c-backward-token-2 1 t lim)))
8625 (setq braceassignp
8626 (cond
8627 ;; Check for operator =
8628 ((and c-opt-op-identifier-prefix
8629 (looking-at c-opt-op-identifier-prefix))
8630 nil)
8631 ;; Check for `<opchar>= in Pike.
8632 ((and (c-major-mode-is 'pike-mode)
8633 (or (eq (char-after) ?`)
8634 ;; Special case for Pikes
8635 ;; `[]=, since '[' is not in
8636 ;; the punctuation class.
8637 (and (eq (char-after) ?\[)
8638 (eq (char-before) ?`))))
8639 nil)
8640 ((looking-at "\\s.") 'maybe)
8641 ;; make sure we're not in a C++ template
8642 ;; argument assignment
8643 ((and
8644 (c-major-mode-is 'c++-mode)
8645 (save-excursion
8646 (let ((here (point))
8647 (pos< (progn
8648 (skip-chars-backward "^<>")
8649 (point))))
8650 (and (eq (char-before) ?<)
8651 (not (c-crosses-statement-barrier-p
8652 pos< here))
8653 (not (c-in-literal))
8654 ))))
8655 nil)
8656 (t t))))))
8657 (if (and (eq braceassignp 'dontknow)
8658 (/= (c-backward-token-2 1 t lim) 0))
8659 (setq braceassignp nil)))
8660 (cond
8661 (braceassignp
8662 ;; We've hit the beginning of the aggregate list.
8663 (c-beginning-of-statement-1
8664 (c-most-enclosing-brace paren-state))
8665 (setq bufpos (point)))
8666 ((eq (char-after) ?\;)
8667 ;; Brace lists can't contain a semicolon, so we're done.
8668 (setq containing-sexp nil))
8669 ((and (setq macro-start (point))
8670 (c-forward-to-cpp-define-body)
8671 (eq (point) containing-sexp))
8672 ;; We've a macro whose expansion starts with the '{'.
8673 ;; Heuristically, if we have a ';' in it we've not got a
8674 ;; brace list, otherwise we have.
8675 (let ((macro-end (progn (c-end-of-macro) (point))))
8676 (goto-char containing-sexp)
8677 (forward-char)
8678 (if (and (c-syntactic-re-search-forward "[;,]" macro-end t t)
8679 (eq (char-before) ?\;))
8680 (setq bufpos nil
8681 containing-sexp nil)
8682 (setq bufpos macro-start))))
8683 (t
8684 ;; Go up one level
8685 (setq containing-sexp next-containing
8686 lim nil
8687 next-containing nil)))))
8688
8689 bufpos))
8690 ))
8691
8692 (defun c-looking-at-special-brace-list (&optional lim)
8693 ;; If we're looking at the start of a pike-style list, i.e., `({ })',
8694 ;; `([ ])', `(< >)', etc., a cons of a cons of its starting and ending
8695 ;; positions and its entry in c-special-brace-lists is returned, nil
8696 ;; otherwise. The ending position is nil if the list is still open.
8697 ;; LIM is the limit for forward search. The point may either be at
8698 ;; the `(' or at the following paren character. Tries to check the
8699 ;; matching closer, but assumes it's correct if no balanced paren is
8700 ;; found (i.e. the case `({ ... } ... )' is detected as _not_ being
8701 ;; a special brace list).
8702 ;;
8703 ;; This function might do hidden buffer changes.
8704 (if c-special-brace-lists
8705 (condition-case ()
8706 (save-excursion
8707 (let ((beg (point))
8708 inner-beg end type)
8709 (c-forward-syntactic-ws)
8710 (if (eq (char-after) ?\()
8711 (progn
8712 (forward-char 1)
8713 (c-forward-syntactic-ws)
8714 (setq inner-beg (point))
8715 (setq type (assq (char-after) c-special-brace-lists)))
8716 (if (setq type (assq (char-after) c-special-brace-lists))
8717 (progn
8718 (setq inner-beg (point))
8719 (c-backward-syntactic-ws)
8720 (forward-char -1)
8721 (setq beg (if (eq (char-after) ?\()
8722 (point)
8723 nil)))))
8724 (if (and beg type)
8725 (if (and (c-safe
8726 (goto-char beg)
8727 (c-forward-sexp 1)
8728 (setq end (point))
8729 (= (char-before) ?\)))
8730 (c-safe
8731 (goto-char inner-beg)
8732 (if (looking-at "\\s(")
8733 ;; Check balancing of the inner paren
8734 ;; below.
8735 (progn
8736 (c-forward-sexp 1)
8737 t)
8738 ;; If the inner char isn't a paren then
8739 ;; we can't check balancing, so just
8740 ;; check the char before the outer
8741 ;; closing paren.
8742 (goto-char end)
8743 (backward-char)
8744 (c-backward-syntactic-ws)
8745 (= (char-before) (cdr type)))))
8746 (if (or (/= (char-syntax (char-before)) ?\))
8747 (= (progn
8748 (c-forward-syntactic-ws)
8749 (point))
8750 (1- end)))
8751 (cons (cons beg end) type))
8752 (cons (list beg) type)))))
8753 (error nil))))
8754
8755 (defun c-looking-at-bos (&optional lim)
8756 ;; Return non-nil if between two statements or declarations, assuming
8757 ;; point is not inside a literal or comment.
8758 ;;
8759 ;; Obsolete - `c-at-statement-start-p' or `c-at-expression-start-p'
8760 ;; are recommended instead.
8761 ;;
8762 ;; This function might do hidden buffer changes.
8763 (c-at-statement-start-p))
8764 (make-obsolete 'c-looking-at-bos 'c-at-statement-start-p "22.1")
8765
8766 (defun c-looking-at-inexpr-block (lim containing-sexp &optional check-at-end)
8767 ;; Return non-nil if we're looking at the beginning of a block
8768 ;; inside an expression. The value returned is actually a cons of
8769 ;; either 'inlambda, 'inexpr-statement or 'inexpr-class and the
8770 ;; position of the beginning of the construct.
8771 ;;
8772 ;; LIM limits the backward search. CONTAINING-SEXP is the start
8773 ;; position of the closest containing list. If it's nil, the
8774 ;; containing paren isn't used to decide whether we're inside an
8775 ;; expression or not. If both LIM and CONTAINING-SEXP are used, LIM
8776 ;; needs to be farther back.
8777 ;;
8778 ;; If CHECK-AT-END is non-nil then extra checks at the end of the
8779 ;; brace block might be done. It should only be used when the
8780 ;; construct can be assumed to be complete, i.e. when the original
8781 ;; starting position was further down than that.
8782 ;;
8783 ;; This function might do hidden buffer changes.
8784
8785 (save-excursion
8786 (let ((res 'maybe) passed-paren
8787 (closest-lim (or containing-sexp lim (point-min)))
8788 ;; Look at the character after point only as a last resort
8789 ;; when we can't disambiguate.
8790 (block-follows (and (eq (char-after) ?{) (point))))
8791
8792 (while (and (eq res 'maybe)
8793 (progn (c-backward-syntactic-ws)
8794 (> (point) closest-lim))
8795 (not (bobp))
8796 (progn (backward-char)
8797 (looking-at "[\]\).]\\|\\w\\|\\s_"))
8798 (c-safe (forward-char)
8799 (goto-char (scan-sexps (point) -1))))
8800
8801 (setq res
8802 (if (looking-at c-keywords-regexp)
8803 (let ((kw-sym (c-keyword-sym (match-string 1))))
8804 (cond
8805 ((and block-follows
8806 (c-keyword-member kw-sym 'c-inexpr-class-kwds))
8807 (and (not (eq passed-paren ?\[))
8808 (or (not (looking-at c-class-key))
8809 ;; If the class definition is at the start of
8810 ;; a statement, we don't consider it an
8811 ;; in-expression class.
8812 (let ((prev (point)))
8813 (while (and
8814 (= (c-backward-token-2 1 nil closest-lim) 0)
8815 (eq (char-syntax (char-after)) ?w))
8816 (setq prev (point)))
8817 (goto-char prev)
8818 (not (c-at-statement-start-p)))
8819 ;; Also, in Pike we treat it as an
8820 ;; in-expression class if it's used in an
8821 ;; object clone expression.
8822 (save-excursion
8823 (and check-at-end
8824 (c-major-mode-is 'pike-mode)
8825 (progn (goto-char block-follows)
8826 (zerop (c-forward-token-2 1 t)))
8827 (eq (char-after) ?\())))
8828 (cons 'inexpr-class (point))))
8829 ((c-keyword-member kw-sym 'c-inexpr-block-kwds)
8830 (when (not passed-paren)
8831 (cons 'inexpr-statement (point))))
8832 ((c-keyword-member kw-sym 'c-lambda-kwds)
8833 (when (or (not passed-paren)
8834 (eq passed-paren ?\())
8835 (cons 'inlambda (point))))
8836 ((c-keyword-member kw-sym 'c-block-stmt-kwds)
8837 nil)
8838 (t
8839 'maybe)))
8840
8841 (if (looking-at "\\s(")
8842 (if passed-paren
8843 (if (and (eq passed-paren ?\[)
8844 (eq (char-after) ?\[))
8845 ;; Accept several square bracket sexps for
8846 ;; Java array initializations.
8847 'maybe)
8848 (setq passed-paren (char-after))
8849 'maybe)
8850 'maybe))))
8851
8852 (if (eq res 'maybe)
8853 (when (and c-recognize-paren-inexpr-blocks
8854 block-follows
8855 containing-sexp
8856 (eq (char-after containing-sexp) ?\())
8857 (goto-char containing-sexp)
8858 (if (or (save-excursion
8859 (c-backward-syntactic-ws lim)
8860 (and (> (point) (or lim (point-min)))
8861 (c-on-identifier)))
8862 (and c-special-brace-lists
8863 (c-looking-at-special-brace-list)))
8864 nil
8865 (cons 'inexpr-statement (point))))
8866
8867 res))))
8868
8869 (defun c-looking-at-inexpr-block-backward (paren-state)
8870 ;; Returns non-nil if we're looking at the end of an in-expression
8871 ;; block, otherwise the same as `c-looking-at-inexpr-block'.
8872 ;; PAREN-STATE is the paren state relevant at the current position.
8873 ;;
8874 ;; This function might do hidden buffer changes.
8875 (save-excursion
8876 ;; We currently only recognize a block.
8877 (let ((here (point))
8878 (elem (car-safe paren-state))
8879 containing-sexp)
8880 (when (and (consp elem)
8881 (progn (goto-char (cdr elem))
8882 (c-forward-syntactic-ws here)
8883 (= (point) here)))
8884 (goto-char (car elem))
8885 (if (setq paren-state (cdr paren-state))
8886 (setq containing-sexp (car-safe paren-state)))
8887 (c-looking-at-inexpr-block (c-safe-position containing-sexp
8888 paren-state)
8889 containing-sexp)))))
8890
8891 (defun c-at-macro-vsemi-p (&optional pos)
8892 ;; Is there a "virtual semicolon" at POS or point?
8893 ;; (See cc-defs.el for full details of "virtual semicolons".)
8894 ;;
8895 ;; This is true when point is at the last non syntactic WS position on the
8896 ;; line, there is a macro call last on the line, and this particular macro's
8897 ;; name is defined by the regexp `c-vs-macro-regexp' as not needing a
8898 ;; semicolon.
8899 (save-excursion
8900 (save-restriction
8901 (widen)
8902 (if pos
8903 (goto-char pos)
8904 (setq pos (point)))
8905 (and
8906 c-macro-with-semi-re
8907 (eq (skip-chars-backward " \t") 0)
8908
8909 ;; Check we've got nothing after this except comments and empty lines
8910 ;; joined by escaped EOLs.
8911 (skip-chars-forward " \t") ; always returns non-nil.
8912 (progn
8913 (while ; go over 1 block comment per iteration.
8914 (and
8915 (looking-at "\\(\\\\[\n\r][ \t]*\\)*")
8916 (goto-char (match-end 0))
8917 (cond
8918 ((looking-at c-block-comment-start-regexp)
8919 (and (forward-comment 1)
8920 (skip-chars-forward " \t"))) ; always returns non-nil
8921 ((looking-at c-line-comment-start-regexp)
8922 (end-of-line)
8923 nil)
8924 (t nil))))
8925 (eolp))
8926
8927 (goto-char pos)
8928 (progn (c-backward-syntactic-ws)
8929 (eq (point) pos))
8930
8931 ;; Check for one of the listed macros being before point.
8932 (or (not (eq (char-before) ?\)))
8933 (when (c-go-list-backward)
8934 (c-backward-syntactic-ws)
8935 t))
8936 (c-simple-skip-symbol-backward)
8937 (looking-at c-macro-with-semi-re)
8938 (goto-char pos)
8939 (not (c-in-literal)))))) ; The most expensive check last.
8940
8941 (defun c-macro-vsemi-status-unknown-p () t) ; See cc-defs.el.
8942
8943 \f
8944 ;; `c-guess-basic-syntax' and the functions that precedes it below
8945 ;; implements the main decision tree for determining the syntactic
8946 ;; analysis of the current line of code.
8947
8948 ;; Dynamically bound to t when `c-guess-basic-syntax' is called during
8949 ;; auto newline analysis.
8950 (defvar c-auto-newline-analysis nil)
8951
8952 (defun c-brace-anchor-point (bracepos)
8953 ;; BRACEPOS is the position of a brace in a construct like "namespace
8954 ;; Bar {". Return the anchor point in this construct; this is the
8955 ;; earliest symbol on the brace's line which isn't earlier than
8956 ;; "namespace".
8957 ;;
8958 ;; Currently (2007-08-17), "like namespace" means "matches
8959 ;; c-other-block-decl-kwds". It doesn't work with "class" or "struct"
8960 ;; or anything like that.
8961 (save-excursion
8962 (let ((boi (c-point 'boi bracepos)))
8963 (goto-char bracepos)
8964 (while (and (> (point) boi)
8965 (not (looking-at c-other-decl-block-key)))
8966 (c-backward-token-2))
8967 (if (> (point) boi) (point) boi))))
8968
8969 (defsubst c-add-syntax (symbol &rest args)
8970 ;; A simple function to prepend a new syntax element to
8971 ;; `c-syntactic-context'. Using `setq' on it is unsafe since it
8972 ;; should always be dynamically bound but since we read it first
8973 ;; we'll fail properly anyway if this function is misused.
8974 (setq c-syntactic-context (cons (cons symbol args)
8975 c-syntactic-context)))
8976
8977 (defsubst c-append-syntax (symbol &rest args)
8978 ;; Like `c-add-syntax' but appends to the end of the syntax list.
8979 ;; (Normally not necessary.)
8980 (setq c-syntactic-context (nconc c-syntactic-context
8981 (list (cons symbol args)))))
8982
8983 (defun c-add-stmt-syntax (syntax-symbol
8984 syntax-extra-args
8985 stop-at-boi-only
8986 containing-sexp
8987 paren-state)
8988 ;; Add the indicated SYNTAX-SYMBOL to `c-syntactic-context', extending it as
8989 ;; needed with further syntax elements of the types `substatement',
8990 ;; `inexpr-statement', `arglist-cont-nonempty', `statement-block-intro', and
8991 ;; `defun-block-intro'.
8992 ;;
8993 ;; Do the generic processing to anchor the given syntax symbol on
8994 ;; the preceding statement: Skip over any labels and containing
8995 ;; statements on the same line, and then search backward until we
8996 ;; find a statement or block start that begins at boi without a
8997 ;; label or comment.
8998 ;;
8999 ;; Point is assumed to be at the prospective anchor point for the
9000 ;; given SYNTAX-SYMBOL. More syntax entries are added if we need to
9001 ;; skip past open parens and containing statements. Most of the added
9002 ;; syntax elements will get the same anchor point - the exception is
9003 ;; for an anchor in a construct like "namespace"[*] - this is as early
9004 ;; as possible in the construct but on the same line as the {.
9005 ;;
9006 ;; [*] i.e. with a keyword matching c-other-block-decl-kwds.
9007 ;;
9008 ;; SYNTAX-EXTRA-ARGS are a list of the extra arguments for the
9009 ;; syntax symbol. They are appended after the anchor point.
9010 ;;
9011 ;; If STOP-AT-BOI-ONLY is nil, we can stop in the middle of the line
9012 ;; if the current statement starts there.
9013 ;;
9014 ;; Note: It's not a problem if PAREN-STATE "overshoots"
9015 ;; CONTAINING-SEXP, i.e. contains info about parens further down.
9016 ;;
9017 ;; This function might do hidden buffer changes.
9018
9019 (if (= (point) (c-point 'boi))
9020 ;; This is by far the most common case, so let's give it special
9021 ;; treatment.
9022 (apply 'c-add-syntax syntax-symbol (point) syntax-extra-args)
9023
9024 (let ((syntax-last c-syntactic-context)
9025 (boi (c-point 'boi))
9026 ;; Set when we're on a label, so that we don't stop there.
9027 ;; FIXME: To be complete we should check if we're on a label
9028 ;; now at the start.
9029 on-label)
9030
9031 ;; Use point as the anchor point for "namespace", "extern", etc.
9032 (apply 'c-add-syntax syntax-symbol
9033 (if (rassq syntax-symbol c-other-decl-block-key-in-symbols-alist)
9034 (point) nil)
9035 syntax-extra-args)
9036
9037 ;; Loop while we have to back out of containing blocks.
9038 (while
9039 (and
9040 (catch 'back-up-block
9041
9042 ;; Loop while we have to back up statements.
9043 (while (or (/= (point) boi)
9044 on-label
9045 (looking-at c-comment-start-regexp))
9046
9047 ;; Skip past any comments that stands between the
9048 ;; statement start and boi.
9049 (let ((savepos (point)))
9050 (while (and (/= savepos boi)
9051 (c-backward-single-comment))
9052 (setq savepos (point)
9053 boi (c-point 'boi)))
9054 (goto-char savepos))
9055
9056 ;; Skip to the beginning of this statement or backward
9057 ;; another one.
9058 (let ((old-pos (point))
9059 (old-boi boi)
9060 (step-type (c-beginning-of-statement-1 containing-sexp)))
9061 (setq boi (c-point 'boi)
9062 on-label (eq step-type 'label))
9063
9064 (cond ((= (point) old-pos)
9065 ;; If we didn't move we're at the start of a block and
9066 ;; have to continue outside it.
9067 (throw 'back-up-block t))
9068
9069 ((and (eq step-type 'up)
9070 (>= (point) old-boi)
9071 (looking-at "else\\>[^_]")
9072 (save-excursion
9073 (goto-char old-pos)
9074 (looking-at "if\\>[^_]")))
9075 ;; Special case to avoid deeper and deeper indentation
9076 ;; of "else if" clauses.
9077 )
9078
9079 ((and (not stop-at-boi-only)
9080 (/= old-pos old-boi)
9081 (memq step-type '(up previous)))
9082 ;; If stop-at-boi-only is nil, we shouldn't back up
9083 ;; over previous or containing statements to try to
9084 ;; reach boi, so go back to the last position and
9085 ;; exit.
9086 (goto-char old-pos)
9087 (throw 'back-up-block nil))
9088
9089 (t
9090 (if (and (not stop-at-boi-only)
9091 (memq step-type '(up previous beginning)))
9092 ;; If we've moved into another statement then we
9093 ;; should no longer try to stop in the middle of a
9094 ;; line.
9095 (setq stop-at-boi-only t))
9096
9097 ;; Record this as a substatement if we skipped up one
9098 ;; level.
9099 (when (eq step-type 'up)
9100 (c-add-syntax 'substatement nil))))
9101 )))
9102
9103 containing-sexp)
9104
9105 ;; Now we have to go out of this block.
9106 (goto-char containing-sexp)
9107
9108 ;; Don't stop in the middle of a special brace list opener
9109 ;; like "({".
9110 (when c-special-brace-lists
9111 (let ((special-list (c-looking-at-special-brace-list)))
9112 (when (and special-list
9113 (< (car (car special-list)) (point)))
9114 (setq containing-sexp (car (car special-list)))
9115 (goto-char containing-sexp))))
9116
9117 (setq paren-state (c-whack-state-after containing-sexp paren-state)
9118 containing-sexp (c-most-enclosing-brace paren-state)
9119 boi (c-point 'boi))
9120
9121 ;; Analyze the construct in front of the block we've stepped out
9122 ;; from and add the right syntactic element for it.
9123 (let ((paren-pos (point))
9124 (paren-char (char-after))
9125 step-type)
9126
9127 (if (eq paren-char ?\()
9128 ;; Stepped out of a parenthesis block, so we're in an
9129 ;; expression now.
9130 (progn
9131 (when (/= paren-pos boi)
9132 (if (and c-recognize-paren-inexpr-blocks
9133 (progn
9134 (c-backward-syntactic-ws containing-sexp)
9135 (or (not (looking-at "\\>"))
9136 (not (c-on-identifier))))
9137 (save-excursion
9138 (goto-char (1+ paren-pos))
9139 (c-forward-syntactic-ws)
9140 (eq (char-after) ?{)))
9141 ;; Stepped out of an in-expression statement. This
9142 ;; syntactic element won't get an anchor pos.
9143 (c-add-syntax 'inexpr-statement)
9144
9145 ;; A parenthesis normally belongs to an arglist.
9146 (c-add-syntax 'arglist-cont-nonempty nil paren-pos)))
9147
9148 (goto-char (max boi
9149 (if containing-sexp
9150 (1+ containing-sexp)
9151 (point-min))))
9152 (setq step-type 'same
9153 on-label nil))
9154
9155 ;; Stepped out of a brace block.
9156 (setq step-type (c-beginning-of-statement-1 containing-sexp)
9157 on-label (eq step-type 'label))
9158
9159 (if (and (eq step-type 'same)
9160 (/= paren-pos (point)))
9161 (let (inexpr)
9162 (cond
9163 ((save-excursion
9164 (goto-char paren-pos)
9165 (setq inexpr (c-looking-at-inexpr-block
9166 (c-safe-position containing-sexp paren-state)
9167 containing-sexp)))
9168 (c-add-syntax (if (eq (car inexpr) 'inlambda)
9169 'defun-block-intro
9170 'statement-block-intro)
9171 nil))
9172 ((looking-at c-other-decl-block-key)
9173 (c-add-syntax
9174 (cdr (assoc (match-string 1)
9175 c-other-decl-block-key-in-symbols-alist))
9176 (max (c-point 'boi paren-pos) (point))))
9177 (t (c-add-syntax 'defun-block-intro nil))))
9178
9179 (c-add-syntax 'statement-block-intro nil)))
9180
9181 (if (= paren-pos boi)
9182 ;; Always done if the open brace was at boi. The
9183 ;; c-beginning-of-statement-1 call above is necessary
9184 ;; anyway, to decide the type of block-intro to add.
9185 (goto-char paren-pos)
9186 (setq boi (c-point 'boi)))
9187 ))
9188
9189 ;; Fill in the current point as the anchor for all the symbols
9190 ;; added above.
9191 (let ((p c-syntactic-context) q)
9192 (while (not (eq p syntax-last))
9193 (setq q (cdr (car p))) ; e.g. (nil 28) [from (arglist-cont-nonempty nil 28)]
9194 (while q
9195 (unless (car q)
9196 (setcar q (point)))
9197 (setq q (cdr q)))
9198 (setq p (cdr p))))
9199 )))
9200
9201 (defun c-add-class-syntax (symbol
9202 containing-decl-open
9203 containing-decl-start
9204 containing-decl-kwd
9205 paren-state)
9206 ;; The inclass and class-close syntactic symbols are added in
9207 ;; several places and some work is needed to fix everything.
9208 ;; Therefore it's collected here.
9209 ;;
9210 ;; This function might do hidden buffer changes.
9211 (goto-char containing-decl-open)
9212 (if (and (eq symbol 'inclass) (= (point) (c-point 'boi)))
9213 (progn
9214 (c-add-syntax symbol containing-decl-open)
9215 containing-decl-open)
9216 (goto-char containing-decl-start)
9217 ;; Ought to use `c-add-stmt-syntax' instead of backing up to boi
9218 ;; here, but we have to do like this for compatibility.
9219 (back-to-indentation)
9220 (c-add-syntax symbol (point))
9221 (if (and (c-keyword-member containing-decl-kwd
9222 'c-inexpr-class-kwds)
9223 (/= containing-decl-start (c-point 'boi containing-decl-start)))
9224 (c-add-syntax 'inexpr-class))
9225 (point)))
9226
9227 (defun c-guess-continued-construct (indent-point
9228 char-after-ip
9229 beg-of-same-or-containing-stmt
9230 containing-sexp
9231 paren-state)
9232 ;; This function contains the decision tree reached through both
9233 ;; cases 18 and 10. It's a continued statement or top level
9234 ;; construct of some kind.
9235 ;;
9236 ;; This function might do hidden buffer changes.
9237
9238 (let (special-brace-list placeholder)
9239 (goto-char indent-point)
9240 (skip-chars-forward " \t")
9241
9242 (cond
9243 ;; (CASE A removed.)
9244 ;; CASE B: open braces for class or brace-lists
9245 ((setq special-brace-list
9246 (or (and c-special-brace-lists
9247 (c-looking-at-special-brace-list))
9248 (eq char-after-ip ?{)))
9249
9250 (cond
9251 ;; CASE B.1: class-open
9252 ((save-excursion
9253 (and (eq (char-after) ?{)
9254 (c-looking-at-decl-block containing-sexp t)
9255 (setq beg-of-same-or-containing-stmt (point))))
9256 (c-add-syntax 'class-open beg-of-same-or-containing-stmt))
9257
9258 ;; CASE B.2: brace-list-open
9259 ((or (consp special-brace-list)
9260 (save-excursion
9261 (goto-char beg-of-same-or-containing-stmt)
9262 (c-syntactic-re-search-forward "=\\([^=]\\|$\\)"
9263 indent-point t t t)))
9264 ;; The most semantically accurate symbol here is
9265 ;; brace-list-open, but we normally report it simply as a
9266 ;; statement-cont. The reason is that one normally adjusts
9267 ;; brace-list-open for brace lists as top-level constructs,
9268 ;; and brace lists inside statements is a completely different
9269 ;; context. C.f. case 5A.3.
9270 (c-beginning-of-statement-1 containing-sexp)
9271 (c-add-stmt-syntax (if c-auto-newline-analysis
9272 ;; Turn off the dwim above when we're
9273 ;; analyzing the nature of the brace
9274 ;; for the auto newline feature.
9275 'brace-list-open
9276 'statement-cont)
9277 nil nil
9278 containing-sexp paren-state))
9279
9280 ;; CASE B.3: The body of a function declared inside a normal
9281 ;; block. Can occur e.g. in Pike and when using gcc
9282 ;; extensions, but watch out for macros followed by blocks.
9283 ;; C.f. cases E, 16F and 17G.
9284 ((and (not (c-at-statement-start-p))
9285 (eq (c-beginning-of-statement-1 containing-sexp nil nil t)
9286 'same)
9287 (save-excursion
9288 (let ((c-recognize-typeless-decls nil))
9289 ;; Turn off recognition of constructs that lacks a
9290 ;; type in this case, since that's more likely to be
9291 ;; a macro followed by a block.
9292 (c-forward-decl-or-cast-1 (c-point 'bosws) nil nil))))
9293 (c-add-stmt-syntax 'defun-open nil t
9294 containing-sexp paren-state))
9295
9296 ;; CASE B.4: Continued statement with block open. The most
9297 ;; accurate analysis is perhaps `statement-cont' together with
9298 ;; `block-open' but we play DWIM and use `substatement-open'
9299 ;; instead. The rationale is that this typically is a macro
9300 ;; followed by a block which makes it very similar to a
9301 ;; statement with a substatement block.
9302 (t
9303 (c-add-stmt-syntax 'substatement-open nil nil
9304 containing-sexp paren-state))
9305 ))
9306
9307 ;; CASE C: iostream insertion or extraction operator
9308 ((and (looking-at "\\(<<\\|>>\\)\\([^=]\\|$\\)")
9309 (save-excursion
9310 (goto-char beg-of-same-or-containing-stmt)
9311 ;; If there is no preceding streamop in the statement
9312 ;; then indent this line as a normal statement-cont.
9313 (when (c-syntactic-re-search-forward
9314 "\\(<<\\|>>\\)\\([^=]\\|$\\)" indent-point 'move t t)
9315 (c-add-syntax 'stream-op (c-point 'boi))
9316 t))))
9317
9318 ;; CASE E: In the "K&R region" of a function declared inside a
9319 ;; normal block. C.f. case B.3.
9320 ((and (save-excursion
9321 ;; Check that the next token is a '{'. This works as
9322 ;; long as no language that allows nested function
9323 ;; definitions allows stuff like member init lists, K&R
9324 ;; declarations or throws clauses there.
9325 ;;
9326 ;; Note that we do a forward search for something ahead
9327 ;; of the indentation line here. That's not good since
9328 ;; the user might not have typed it yet. Unfortunately
9329 ;; it's exceedingly tricky to recognize a function
9330 ;; prototype in a code block without resorting to this.
9331 (c-forward-syntactic-ws)
9332 (eq (char-after) ?{))
9333 (not (c-at-statement-start-p))
9334 (eq (c-beginning-of-statement-1 containing-sexp nil nil t)
9335 'same)
9336 (save-excursion
9337 (let ((c-recognize-typeless-decls nil))
9338 ;; Turn off recognition of constructs that lacks a
9339 ;; type in this case, since that's more likely to be
9340 ;; a macro followed by a block.
9341 (c-forward-decl-or-cast-1 (c-point 'bosws) nil nil))))
9342 (c-add-stmt-syntax 'func-decl-cont nil t
9343 containing-sexp paren-state))
9344
9345 ;;CASE F: continued statement and the only preceding items are
9346 ;;annotations.
9347 ((and (c-major-mode-is 'java-mode)
9348 (setq placeholder (point))
9349 (c-beginning-of-statement-1)
9350 (progn
9351 (while (and (c-forward-annotation)
9352 (< (point) placeholder))
9353 (c-forward-syntactic-ws))
9354 t)
9355 (prog1
9356 (>= (point) placeholder)
9357 (goto-char placeholder)))
9358 (c-beginning-of-statement-1 containing-sexp)
9359 (c-add-syntax 'annotation-var-cont (point)))
9360
9361 ;; CASE G: a template list continuation?
9362 ;; Mostly a duplication of case 5D.3 to fix templates-19:
9363 ((and (c-major-mode-is 'c++-mode)
9364 (save-excursion
9365 (goto-char indent-point)
9366 (c-with-syntax-table c++-template-syntax-table
9367 (setq placeholder (c-up-list-backward)))
9368 (and placeholder
9369 (eq (char-after placeholder) ?<)
9370 (/= (char-before placeholder) ?<)
9371 (progn
9372 (goto-char (1+ placeholder))
9373 (not (looking-at c-<-op-cont-regexp))))))
9374 (c-with-syntax-table c++-template-syntax-table
9375 (goto-char placeholder)
9376 (c-beginning-of-statement-1 containing-sexp t)
9377 (if (save-excursion
9378 (c-backward-syntactic-ws containing-sexp)
9379 (eq (char-before) ?<))
9380 ;; In a nested template arglist.
9381 (progn
9382 (goto-char placeholder)
9383 (c-syntactic-skip-backward "^,;" containing-sexp t)
9384 (c-forward-syntactic-ws))
9385 (back-to-indentation)))
9386 ;; FIXME: Should use c-add-stmt-syntax, but it's not yet
9387 ;; template aware.
9388 (c-add-syntax 'template-args-cont (point) placeholder))
9389
9390 ;; CASE D: continued statement.
9391 (t
9392 (c-beginning-of-statement-1 containing-sexp)
9393 (c-add-stmt-syntax 'statement-cont nil nil
9394 containing-sexp paren-state))
9395 )))
9396
9397 ;; The next autoload was added by RMS on 2005/8/9 - don't know why (ACM,
9398 ;; 2005/11/29).
9399 ;;;###autoload
9400 (defun c-guess-basic-syntax ()
9401 "Return the syntactic context of the current line."
9402 (save-excursion
9403 (beginning-of-line)
9404 (c-save-buffer-state
9405 ((indent-point (point))
9406 (case-fold-search nil)
9407 open-paren-in-column-0-is-defun-start
9408 ;; A whole ugly bunch of various temporary variables. Have
9409 ;; to declare them here since it's not possible to declare
9410 ;; a variable with only the scope of a cond test and the
9411 ;; following result clauses, and most of this function is a
9412 ;; single gigantic cond. :P
9413 literal char-before-ip before-ws-ip char-after-ip macro-start
9414 in-macro-expr c-syntactic-context placeholder c-in-literal-cache
9415 step-type tmpsymbol keyword injava-inher special-brace-list tmp-pos
9416 containing-<
9417 ;; The following record some positions for the containing
9418 ;; declaration block if we're directly within one:
9419 ;; `containing-decl-open' is the position of the open
9420 ;; brace. `containing-decl-start' is the start of the
9421 ;; declaration. `containing-decl-kwd' is the keyword
9422 ;; symbol of the keyword that tells what kind of block it
9423 ;; is.
9424 containing-decl-open
9425 containing-decl-start
9426 containing-decl-kwd
9427 ;; The open paren of the closest surrounding sexp or nil if
9428 ;; there is none.
9429 containing-sexp
9430 ;; The position after the closest preceding brace sexp
9431 ;; (nested sexps are ignored), or the position after
9432 ;; `containing-sexp' if there is none, or (point-min) if
9433 ;; `containing-sexp' is nil.
9434 lim
9435 ;; The paren state outside `containing-sexp', or at
9436 ;; `indent-point' if `containing-sexp' is nil.
9437 (paren-state (c-parse-state))
9438 ;; There's always at most one syntactic element which got
9439 ;; an anchor pos. It's stored in syntactic-relpos.
9440 syntactic-relpos
9441 (c-stmt-delim-chars c-stmt-delim-chars))
9442
9443 ;; Check if we're directly inside an enclosing declaration
9444 ;; level block.
9445 (when (and (setq containing-sexp
9446 (c-most-enclosing-brace paren-state))
9447 (progn
9448 (goto-char containing-sexp)
9449 (eq (char-after) ?{))
9450 (setq placeholder
9451 (c-looking-at-decl-block
9452 (c-most-enclosing-brace paren-state
9453 containing-sexp)
9454 t)))
9455 (setq containing-decl-open containing-sexp
9456 containing-decl-start (point)
9457 containing-sexp nil)
9458 (goto-char placeholder)
9459 (setq containing-decl-kwd (and (looking-at c-keywords-regexp)
9460 (c-keyword-sym (match-string 1)))))
9461
9462 ;; Init some position variables.
9463 (if c-state-cache
9464 (progn
9465 (setq containing-sexp (car paren-state)
9466 paren-state (cdr paren-state))
9467 (if (consp containing-sexp)
9468 (progn
9469 (setq lim (cdr containing-sexp))
9470 (if (cdr c-state-cache)
9471 ;; Ignore balanced paren. The next entry
9472 ;; can't be another one.
9473 (setq containing-sexp (car (cdr c-state-cache))
9474 paren-state (cdr paren-state))
9475 ;; If there is no surrounding open paren then
9476 ;; put the last balanced pair back on paren-state.
9477 (setq paren-state (cons containing-sexp paren-state)
9478 containing-sexp nil)))
9479 (setq lim (1+ containing-sexp))))
9480 (setq lim (point-min)))
9481
9482 ;; If we're in a parenthesis list then ',' delimits the
9483 ;; "statements" rather than being an operator (with the
9484 ;; exception of the "for" clause). This difference is
9485 ;; typically only noticeable when statements are used in macro
9486 ;; arglists.
9487 (when (and containing-sexp
9488 (eq (char-after containing-sexp) ?\())
9489 (setq c-stmt-delim-chars c-stmt-delim-chars-with-comma))
9490 ;; cache char before and after indent point, and move point to
9491 ;; the most likely position to perform the majority of tests
9492 (goto-char indent-point)
9493 (c-backward-syntactic-ws lim)
9494 (setq before-ws-ip (point)
9495 char-before-ip (char-before))
9496 (goto-char indent-point)
9497 (skip-chars-forward " \t")
9498 (setq char-after-ip (char-after))
9499
9500 ;; are we in a literal?
9501 (setq literal (c-in-literal lim))
9502
9503 ;; now figure out syntactic qualities of the current line
9504 (cond
9505
9506 ;; CASE 1: in a string.
9507 ((eq literal 'string)
9508 (c-add-syntax 'string (c-point 'bopl)))
9509
9510 ;; CASE 2: in a C or C++ style comment.
9511 ((and (memq literal '(c c++))
9512 ;; This is a kludge for XEmacs where we use
9513 ;; `buffer-syntactic-context', which doesn't correctly
9514 ;; recognize "\*/" to end a block comment.
9515 ;; `parse-partial-sexp' which is used by
9516 ;; `c-literal-limits' will however do that in most
9517 ;; versions, which results in that we get nil from
9518 ;; `c-literal-limits' even when `c-in-literal' claims
9519 ;; we're inside a comment.
9520 (setq placeholder (c-literal-limits lim)))
9521 (c-add-syntax literal (car placeholder)))
9522
9523 ;; CASE 3: in a cpp preprocessor macro continuation.
9524 ((and (save-excursion
9525 (when (c-beginning-of-macro)
9526 (setq macro-start (point))))
9527 (/= macro-start (c-point 'boi))
9528 (progn
9529 (setq tmpsymbol 'cpp-macro-cont)
9530 (or (not c-syntactic-indentation-in-macros)
9531 (save-excursion
9532 (goto-char macro-start)
9533 ;; If at the beginning of the body of a #define
9534 ;; directive then analyze as cpp-define-intro
9535 ;; only. Go on with the syntactic analysis
9536 ;; otherwise. in-macro-expr is set if we're in a
9537 ;; cpp expression, i.e. before the #define body
9538 ;; or anywhere in a non-#define directive.
9539 (if (c-forward-to-cpp-define-body)
9540 (let ((indent-boi (c-point 'boi indent-point)))
9541 (setq in-macro-expr (> (point) indent-boi)
9542 tmpsymbol 'cpp-define-intro)
9543 (= (point) indent-boi))
9544 (setq in-macro-expr t)
9545 nil)))))
9546 (c-add-syntax tmpsymbol macro-start)
9547 (setq macro-start nil))
9548
9549 ;; CASE 11: an else clause?
9550 ((looking-at "else\\>[^_]")
9551 (c-beginning-of-statement-1 containing-sexp)
9552 (c-add-stmt-syntax 'else-clause nil t
9553 containing-sexp paren-state))
9554
9555 ;; CASE 12: while closure of a do/while construct?
9556 ((and (looking-at "while\\>[^_]")
9557 (save-excursion
9558 (prog1 (eq (c-beginning-of-statement-1 containing-sexp)
9559 'beginning)
9560 (setq placeholder (point)))))
9561 (goto-char placeholder)
9562 (c-add-stmt-syntax 'do-while-closure nil t
9563 containing-sexp paren-state))
9564
9565 ;; CASE 13: A catch or finally clause? This case is simpler
9566 ;; than if-else and do-while, because a block is required
9567 ;; after every try, catch and finally.
9568 ((save-excursion
9569 (and (cond ((c-major-mode-is 'c++-mode)
9570 (looking-at "catch\\>[^_]"))
9571 ((c-major-mode-is 'java-mode)
9572 (looking-at "\\(catch\\|finally\\)\\>[^_]")))
9573 (and (c-safe (c-backward-syntactic-ws)
9574 (c-backward-sexp)
9575 t)
9576 (eq (char-after) ?{)
9577 (c-safe (c-backward-syntactic-ws)
9578 (c-backward-sexp)
9579 t)
9580 (if (eq (char-after) ?\()
9581 (c-safe (c-backward-sexp) t)
9582 t))
9583 (looking-at "\\(try\\|catch\\)\\>[^_]")
9584 (setq placeholder (point))))
9585 (goto-char placeholder)
9586 (c-add-stmt-syntax 'catch-clause nil t
9587 containing-sexp paren-state))
9588
9589 ;; CASE 18: A substatement we can recognize by keyword.
9590 ((save-excursion
9591 (and c-opt-block-stmt-key
9592 (not (eq char-before-ip ?\;))
9593 (not (c-at-vsemi-p before-ws-ip))
9594 (not (memq char-after-ip '(?\) ?\] ?,)))
9595 (or (not (eq char-before-ip ?}))
9596 (c-looking-at-inexpr-block-backward c-state-cache))
9597 (> (point)
9598 (progn
9599 ;; Ought to cache the result from the
9600 ;; c-beginning-of-statement-1 calls here.
9601 (setq placeholder (point))
9602 (while (eq (setq step-type
9603 (c-beginning-of-statement-1 lim))
9604 'label))
9605 (if (eq step-type 'previous)
9606 (goto-char placeholder)
9607 (setq placeholder (point))
9608 (if (and (eq step-type 'same)
9609 (not (looking-at c-opt-block-stmt-key)))
9610 ;; Step up to the containing statement if we
9611 ;; stayed in the same one.
9612 (let (step)
9613 (while (eq
9614 (setq step
9615 (c-beginning-of-statement-1 lim))
9616 'label))
9617 (if (eq step 'up)
9618 (setq placeholder (point))
9619 ;; There was no containing statement after all.
9620 (goto-char placeholder)))))
9621 placeholder))
9622 (if (looking-at c-block-stmt-2-key)
9623 ;; Require a parenthesis after these keywords.
9624 ;; Necessary to catch e.g. synchronized in Java,
9625 ;; which can be used both as statement and
9626 ;; modifier.
9627 (and (zerop (c-forward-token-2 1 nil))
9628 (eq (char-after) ?\())
9629 (looking-at c-opt-block-stmt-key))))
9630
9631 (if (eq step-type 'up)
9632 ;; CASE 18A: Simple substatement.
9633 (progn
9634 (goto-char placeholder)
9635 (cond
9636 ((eq char-after-ip ?{)
9637 (c-add-stmt-syntax 'substatement-open nil nil
9638 containing-sexp paren-state))
9639 ((save-excursion
9640 (goto-char indent-point)
9641 (back-to-indentation)
9642 (c-forward-label))
9643 (c-add-stmt-syntax 'substatement-label nil nil
9644 containing-sexp paren-state))
9645 (t
9646 (c-add-stmt-syntax 'substatement nil nil
9647 containing-sexp paren-state))))
9648
9649 ;; CASE 18B: Some other substatement. This is shared
9650 ;; with case 10.
9651 (c-guess-continued-construct indent-point
9652 char-after-ip
9653 placeholder
9654 lim
9655 paren-state)))
9656
9657 ;; CASE 14: A case or default label
9658 ((looking-at c-label-kwds-regexp)
9659 (if containing-sexp
9660 (progn
9661 (goto-char containing-sexp)
9662 (setq lim (c-most-enclosing-brace c-state-cache
9663 containing-sexp))
9664 (c-backward-to-block-anchor lim)
9665 (c-add-stmt-syntax 'case-label nil t lim paren-state))
9666 ;; Got a bogus label at the top level. In lack of better
9667 ;; alternatives, anchor it on (point-min).
9668 (c-add-syntax 'case-label (point-min))))
9669
9670 ;; CASE 15: any other label
9671 ((save-excursion
9672 (back-to-indentation)
9673 (and (not (looking-at c-syntactic-ws-start))
9674 (c-forward-label)))
9675 (cond (containing-decl-open
9676 (setq placeholder (c-add-class-syntax 'inclass
9677 containing-decl-open
9678 containing-decl-start
9679 containing-decl-kwd
9680 paren-state))
9681 ;; Append access-label with the same anchor point as
9682 ;; inclass gets.
9683 (c-append-syntax 'access-label placeholder))
9684
9685 (containing-sexp
9686 (goto-char containing-sexp)
9687 (setq lim (c-most-enclosing-brace c-state-cache
9688 containing-sexp))
9689 (save-excursion
9690 (setq tmpsymbol
9691 (if (and (eq (c-beginning-of-statement-1 lim) 'up)
9692 (looking-at "switch\\>[^_]"))
9693 ;; If the surrounding statement is a switch then
9694 ;; let's analyze all labels as switch labels, so
9695 ;; that they get lined up consistently.
9696 'case-label
9697 'label)))
9698 (c-backward-to-block-anchor lim)
9699 (c-add-stmt-syntax tmpsymbol nil t lim paren-state))
9700
9701 (t
9702 ;; A label on the top level. Treat it as a class
9703 ;; context. (point-min) is the closest we get to the
9704 ;; class open brace.
9705 (c-add-syntax 'access-label (point-min)))))
9706
9707 ;; CASE 4: In-expression statement. C.f. cases 7B, 16A and
9708 ;; 17E.
9709 ((setq placeholder (c-looking-at-inexpr-block
9710 (c-safe-position containing-sexp paren-state)
9711 containing-sexp
9712 ;; Have to turn on the heuristics after
9713 ;; the point even though it doesn't work
9714 ;; very well. C.f. test case class-16.pike.
9715 t))
9716 (setq tmpsymbol (assq (car placeholder)
9717 '((inexpr-class . class-open)
9718 (inexpr-statement . block-open))))
9719 (if tmpsymbol
9720 ;; It's a statement block or an anonymous class.
9721 (setq tmpsymbol (cdr tmpsymbol))
9722 ;; It's a Pike lambda. Check whether we are between the
9723 ;; lambda keyword and the argument list or at the defun
9724 ;; opener.
9725 (setq tmpsymbol (if (eq char-after-ip ?{)
9726 'inline-open
9727 'lambda-intro-cont)))
9728 (goto-char (cdr placeholder))
9729 (back-to-indentation)
9730 (c-add-stmt-syntax tmpsymbol nil t
9731 (c-most-enclosing-brace c-state-cache (point))
9732 paren-state)
9733 (unless (eq (point) (cdr placeholder))
9734 (c-add-syntax (car placeholder))))
9735
9736 ;; CASE 5: Line is inside a declaration level block or at top level.
9737 ((or containing-decl-open (null containing-sexp))
9738 (cond
9739
9740 ;; CASE 5A: we are looking at a defun, brace list, class,
9741 ;; or inline-inclass method opening brace
9742 ((setq special-brace-list
9743 (or (and c-special-brace-lists
9744 (c-looking-at-special-brace-list))
9745 (eq char-after-ip ?{)))
9746 (cond
9747
9748 ;; CASE 5A.1: Non-class declaration block open.
9749 ((save-excursion
9750 (let (tmp)
9751 (and (eq char-after-ip ?{)
9752 (setq tmp (c-looking-at-decl-block containing-sexp t))
9753 (progn
9754 (setq placeholder (point))
9755 (goto-char tmp)
9756 (looking-at c-symbol-key))
9757 (c-keyword-member
9758 (c-keyword-sym (setq keyword (match-string 0)))
9759 'c-other-block-decl-kwds))))
9760 (goto-char placeholder)
9761 (c-add-stmt-syntax
9762 (if (string-equal keyword "extern")
9763 ;; Special case for extern-lang-open.
9764 'extern-lang-open
9765 (intern (concat keyword "-open")))
9766 nil t containing-sexp paren-state))
9767
9768 ;; CASE 5A.2: we are looking at a class opening brace
9769 ((save-excursion
9770 (goto-char indent-point)
9771 (skip-chars-forward " \t")
9772 (and (eq (char-after) ?{)
9773 (c-looking-at-decl-block containing-sexp t)
9774 (setq placeholder (point))))
9775 (c-add-syntax 'class-open placeholder))
9776
9777 ;; CASE 5A.3: brace list open
9778 ((save-excursion
9779 (c-beginning-of-decl-1 lim)
9780 (while (looking-at c-specifier-key)
9781 (goto-char (match-end 1))
9782 (c-forward-syntactic-ws indent-point))
9783 (setq placeholder (c-point 'boi))
9784 (or (consp special-brace-list)
9785 (and (or (save-excursion
9786 (goto-char indent-point)
9787 (setq tmpsymbol nil)
9788 (while (and (> (point) placeholder)
9789 (zerop (c-backward-token-2 1 t))
9790 (not (looking-at "=\\([^=]\\|$\\)")))
9791 (and c-opt-inexpr-brace-list-key
9792 (not tmpsymbol)
9793 (looking-at c-opt-inexpr-brace-list-key)
9794 (setq tmpsymbol 'topmost-intro-cont)))
9795 (looking-at "=\\([^=]\\|$\\)"))
9796 (looking-at c-brace-list-key))
9797 (save-excursion
9798 (while (and (< (point) indent-point)
9799 (zerop (c-forward-token-2 1 t))
9800 (not (memq (char-after) '(?\; ?\()))))
9801 (not (memq (char-after) '(?\; ?\()))
9802 ))))
9803 (if (and (not c-auto-newline-analysis)
9804 (c-major-mode-is 'java-mode)
9805 (eq tmpsymbol 'topmost-intro-cont))
9806 ;; We're in Java and have found that the open brace
9807 ;; belongs to a "new Foo[]" initialization list,
9808 ;; which means the brace list is part of an
9809 ;; expression and not a top level definition. We
9810 ;; therefore treat it as any topmost continuation
9811 ;; even though the semantically correct symbol still
9812 ;; is brace-list-open, on the same grounds as in
9813 ;; case B.2.
9814 (progn
9815 (c-beginning-of-statement-1 lim)
9816 (c-add-syntax 'topmost-intro-cont (c-point 'boi)))
9817 (c-add-syntax 'brace-list-open placeholder)))
9818
9819 ;; CASE 5A.4: inline defun open
9820 ((and containing-decl-open
9821 (not (c-keyword-member containing-decl-kwd
9822 'c-other-block-decl-kwds)))
9823 (c-add-syntax 'inline-open)
9824 (c-add-class-syntax 'inclass
9825 containing-decl-open
9826 containing-decl-start
9827 containing-decl-kwd
9828 paren-state))
9829
9830 ;; CASE 5A.5: ordinary defun open
9831 (t
9832 (save-excursion
9833 (c-beginning-of-decl-1 lim)
9834 (while (looking-at c-specifier-key)
9835 (goto-char (match-end 1))
9836 (c-forward-syntactic-ws indent-point))
9837 (c-add-syntax 'defun-open (c-point 'boi))
9838 ;; Bogus to use bol here, but it's the legacy. (Resolved,
9839 ;; 2007-11-09)
9840 ))))
9841
9842 ;; CASE 5R: Member init list. (Used to be part of CASE 5B.1)
9843 ;; Note there is no limit on the backward search here, since member
9844 ;; init lists can, in practice, be very large.
9845 ((save-excursion
9846 (when (setq placeholder (c-back-over-member-initializers))
9847 (setq tmp-pos (point))))
9848 (if (= (c-point 'bosws) (1+ tmp-pos))
9849 (progn
9850 ;; There is no preceding member init clause.
9851 ;; Indent relative to the beginning of indentation
9852 ;; for the topmost-intro line that contains the
9853 ;; prototype's open paren.
9854 (goto-char placeholder)
9855 (c-add-syntax 'member-init-intro (c-point 'boi)))
9856 ;; Indent relative to the first member init clause.
9857 (goto-char (1+ tmp-pos))
9858 (c-forward-syntactic-ws)
9859 (c-add-syntax 'member-init-cont (point))))
9860
9861 ;; CASE 5B: After a function header but before the body (or
9862 ;; the ending semicolon if there's no body).
9863 ((save-excursion
9864 (when (setq placeholder (c-just-after-func-arglist-p
9865 (max lim (c-determine-limit 500))))
9866 (setq tmp-pos (point))))
9867 (cond
9868
9869 ;; CASE 5B.1: Member init list.
9870 ((eq (char-after tmp-pos) ?:)
9871 ;; There is no preceding member init clause.
9872 ;; Indent relative to the beginning of indentation
9873 ;; for the topmost-intro line that contains the
9874 ;; prototype's open paren.
9875 (goto-char placeholder)
9876 (c-add-syntax 'member-init-intro (c-point 'boi)))
9877
9878 ;; CASE 5B.2: K&R arg decl intro
9879 ((and c-recognize-knr-p
9880 (c-in-knr-argdecl lim))
9881 (c-beginning-of-statement-1 lim)
9882 (c-add-syntax 'knr-argdecl-intro (c-point 'boi))
9883 (if containing-decl-open
9884 (c-add-class-syntax 'inclass
9885 containing-decl-open
9886 containing-decl-start
9887 containing-decl-kwd
9888 paren-state)))
9889
9890 ;; CASE 5B.4: Nether region after a C++ or Java func
9891 ;; decl, which could include a `throws' declaration.
9892 (t
9893 (c-beginning-of-statement-1 lim)
9894 (c-add-syntax 'func-decl-cont (c-point 'boi))
9895 )))
9896
9897 ;; CASE 5C: inheritance line. could be first inheritance
9898 ;; line, or continuation of a multiple inheritance
9899 ((or (and (c-major-mode-is 'c++-mode)
9900 (progn
9901 (when (eq char-after-ip ?,)
9902 (skip-chars-forward " \t")
9903 (forward-char))
9904 (looking-at c-opt-postfix-decl-spec-key)))
9905 (and (or (eq char-before-ip ?:)
9906 ;; watch out for scope operator
9907 (save-excursion
9908 (and (eq char-after-ip ?:)
9909 (c-safe (forward-char 1) t)
9910 (not (eq (char-after) ?:))
9911 )))
9912 (save-excursion
9913 (c-beginning-of-statement-1 lim)
9914 (when (looking-at c-opt-<>-sexp-key)
9915 (goto-char (match-end 1))
9916 (c-forward-syntactic-ws)
9917 (c-forward-<>-arglist nil)
9918 (c-forward-syntactic-ws))
9919 (looking-at c-class-key)))
9920 ;; for Java
9921 (and (c-major-mode-is 'java-mode)
9922 (let ((fence (save-excursion
9923 (c-beginning-of-statement-1 lim)
9924 (point)))
9925 cont done)
9926 (save-excursion
9927 (while (not done)
9928 (cond ((looking-at c-opt-postfix-decl-spec-key)
9929 (setq injava-inher (cons cont (point))
9930 done t))
9931 ((or (not (c-safe (c-forward-sexp -1) t))
9932 (<= (point) fence))
9933 (setq done t))
9934 )
9935 (setq cont t)))
9936 injava-inher)
9937 (not (c-crosses-statement-barrier-p (cdr injava-inher)
9938 (point)))
9939 ))
9940 (cond
9941
9942 ;; CASE 5C.1: non-hanging colon on an inher intro
9943 ((eq char-after-ip ?:)
9944 (c-beginning-of-statement-1 lim)
9945 (c-add-syntax 'inher-intro (c-point 'boi))
9946 ;; don't add inclass symbol since relative point already
9947 ;; contains any class offset
9948 )
9949
9950 ;; CASE 5C.2: hanging colon on an inher intro
9951 ((eq char-before-ip ?:)
9952 (c-beginning-of-statement-1 lim)
9953 (c-add-syntax 'inher-intro (c-point 'boi))
9954 (if containing-decl-open
9955 (c-add-class-syntax 'inclass
9956 containing-decl-open
9957 containing-decl-start
9958 containing-decl-kwd
9959 paren-state)))
9960
9961 ;; CASE 5C.3: in a Java implements/extends
9962 (injava-inher
9963 (let ((where (cdr injava-inher))
9964 (cont (car injava-inher)))
9965 (goto-char where)
9966 (cond ((looking-at "throws\\>[^_]")
9967 (c-add-syntax 'func-decl-cont
9968 (progn (c-beginning-of-statement-1 lim)
9969 (c-point 'boi))))
9970 (cont (c-add-syntax 'inher-cont where))
9971 (t (c-add-syntax 'inher-intro
9972 (progn (goto-char (cdr injava-inher))
9973 (c-beginning-of-statement-1 lim)
9974 (point))))
9975 )))
9976
9977 ;; CASE 5C.4: a continued inheritance line
9978 (t
9979 (c-beginning-of-inheritance-list lim)
9980 (c-add-syntax 'inher-cont (point))
9981 ;; don't add inclass symbol since relative point already
9982 ;; contains any class offset
9983 )))
9984
9985 ;; CASE 5P: AWK pattern or function or continuation
9986 ;; thereof.
9987 ((c-major-mode-is 'awk-mode)
9988 (setq placeholder (point))
9989 (c-add-stmt-syntax
9990 (if (and (eq (c-beginning-of-statement-1) 'same)
9991 (/= (point) placeholder))
9992 'topmost-intro-cont
9993 'topmost-intro)
9994 nil nil
9995 containing-sexp paren-state))
9996
9997 ;; CASE 5D: this could be a top-level initialization, a
9998 ;; member init list continuation, or a template argument
9999 ;; list continuation.
10000 ((save-excursion
10001 ;; Note: We use the fact that lim is always after any
10002 ;; preceding brace sexp.
10003 (if c-recognize-<>-arglists
10004 (while (and
10005 (progn
10006 (c-syntactic-skip-backward "^;,=<>" lim t)
10007 (> (point) lim))
10008 (or
10009 (when c-overloadable-operators-regexp
10010 (when (setq placeholder (c-after-special-operator-id lim))
10011 (goto-char placeholder)
10012 t))
10013 (cond
10014 ((eq (char-before) ?>)
10015 (or (c-backward-<>-arglist nil lim)
10016 (backward-char))
10017 t)
10018 ((eq (char-before) ?<)
10019 (backward-char)
10020 (if (save-excursion
10021 (c-forward-<>-arglist nil))
10022 (progn (forward-char)
10023 nil)
10024 t))
10025 (t nil)))))
10026 ;; NB: No c-after-special-operator-id stuff in this
10027 ;; clause - we assume only C++ needs it.
10028 (c-syntactic-skip-backward "^;,=" lim t))
10029 (memq (char-before) '(?, ?= ?<)))
10030 (cond
10031
10032 ;; CASE 5D.3: perhaps a template list continuation?
10033 ((and (c-major-mode-is 'c++-mode)
10034 (save-excursion
10035 (save-restriction
10036 (c-with-syntax-table c++-template-syntax-table
10037 (goto-char indent-point)
10038 (setq placeholder (c-up-list-backward))
10039 (and placeholder
10040 (eq (char-after placeholder) ?<))))))
10041 (c-with-syntax-table c++-template-syntax-table
10042 (goto-char placeholder)
10043 (c-beginning-of-statement-1 lim t)
10044 (if (save-excursion
10045 (c-backward-syntactic-ws lim)
10046 (eq (char-before) ?<))
10047 ;; In a nested template arglist.
10048 (progn
10049 (goto-char placeholder)
10050 (c-syntactic-skip-backward "^,;" lim t)
10051 (c-forward-syntactic-ws))
10052 (back-to-indentation)))
10053 ;; FIXME: Should use c-add-stmt-syntax, but it's not yet
10054 ;; template aware.
10055 (c-add-syntax 'template-args-cont (point) placeholder))
10056
10057 ;; CASE 5D.4: perhaps a multiple inheritance line?
10058 ((and (c-major-mode-is 'c++-mode)
10059 (save-excursion
10060 (c-beginning-of-statement-1 lim)
10061 (setq placeholder (point))
10062 (if (looking-at "static\\>[^_]")
10063 (c-forward-token-2 1 nil indent-point))
10064 (and (looking-at c-class-key)
10065 (zerop (c-forward-token-2 2 nil indent-point))
10066 (if (eq (char-after) ?<)
10067 (c-with-syntax-table c++-template-syntax-table
10068 (zerop (c-forward-token-2 1 t indent-point)))
10069 t)
10070 (eq (char-after) ?:))))
10071 (goto-char placeholder)
10072 (c-add-syntax 'inher-cont (c-point 'boi)))
10073
10074 ;; CASE 5D.5: Continuation of the "expression part" of a
10075 ;; top level construct. Or, perhaps, an unrecognized construct.
10076 (t
10077 (while (and (setq placeholder (point))
10078 (eq (car (c-beginning-of-decl-1 containing-sexp)) ; Can't use `lim' here.
10079 'same)
10080 (save-excursion
10081 (c-backward-syntactic-ws)
10082 (eq (char-before) ?}))
10083 (< (point) placeholder)))
10084 (c-add-stmt-syntax
10085 (cond
10086 ((eq (point) placeholder) 'statement) ; unrecognized construct
10087 ;; A preceding comma at the top level means that a
10088 ;; new variable declaration starts here. Use
10089 ;; topmost-intro-cont for it, for consistency with
10090 ;; the first variable declaration. C.f. case 5N.
10091 ((eq char-before-ip ?,) 'topmost-intro-cont)
10092 (t 'statement-cont))
10093 nil nil containing-sexp paren-state))
10094 ))
10095
10096 ;; CASE 5F: Close of a non-class declaration level block.
10097 ((and (eq char-after-ip ?})
10098 (c-keyword-member containing-decl-kwd
10099 'c-other-block-decl-kwds))
10100 ;; This is inconsistent: Should use `containing-decl-open'
10101 ;; here if it's at boi, like in case 5J.
10102 (goto-char containing-decl-start)
10103 (c-add-stmt-syntax
10104 (if (string-equal (symbol-name containing-decl-kwd) "extern")
10105 ;; Special case for compatibility with the
10106 ;; extern-lang syntactic symbols.
10107 'extern-lang-close
10108 (intern (concat (symbol-name containing-decl-kwd)
10109 "-close")))
10110 nil t
10111 (c-most-enclosing-brace paren-state (point))
10112 paren-state))
10113
10114 ;; CASE 5G: we are looking at the brace which closes the
10115 ;; enclosing nested class decl
10116 ((and containing-sexp
10117 (eq char-after-ip ?})
10118 (eq containing-decl-open containing-sexp))
10119 (c-add-class-syntax 'class-close
10120 containing-decl-open
10121 containing-decl-start
10122 containing-decl-kwd
10123 paren-state))
10124
10125 ;; CASE 5H: we could be looking at subsequent knr-argdecls
10126 ((and c-recognize-knr-p
10127 (not containing-sexp) ; can't be knr inside braces.
10128 (not (eq char-before-ip ?}))
10129 (save-excursion
10130 (setq placeholder (cdr (c-beginning-of-decl-1 lim)))
10131 (and placeholder
10132 ;; Do an extra check to avoid tripping up on
10133 ;; statements that occur in invalid contexts
10134 ;; (e.g. in macro bodies where we don't really
10135 ;; know the context of what we're looking at).
10136 (not (and c-opt-block-stmt-key
10137 (looking-at c-opt-block-stmt-key)))))
10138 (< placeholder indent-point))
10139 (goto-char placeholder)
10140 (c-add-syntax 'knr-argdecl (point)))
10141
10142 ;; CASE 5I: ObjC method definition.
10143 ((and c-opt-method-key
10144 (looking-at c-opt-method-key))
10145 (c-beginning-of-statement-1 nil t)
10146 (if (= (point) indent-point)
10147 ;; Handle the case when it's the first (non-comment)
10148 ;; thing in the buffer. Can't look for a 'same return
10149 ;; value from cbos1 since ObjC directives currently
10150 ;; aren't recognized fully, so that we get 'same
10151 ;; instead of 'previous if it moved over a preceding
10152 ;; directive.
10153 (goto-char (point-min)))
10154 (c-add-syntax 'objc-method-intro (c-point 'boi)))
10155
10156 ;; CASE 5N: At a variable declaration that follows a class
10157 ;; definition or some other block declaration that doesn't
10158 ;; end at the closing '}'. C.f. case 5D.5.
10159 ((progn
10160 (c-backward-syntactic-ws lim)
10161 (and (eq (char-before) ?})
10162 (save-excursion
10163 (let ((start (point)))
10164 (if (and c-state-cache
10165 (consp (car c-state-cache))
10166 (eq (cdar c-state-cache) (point)))
10167 ;; Speed up the backward search a bit.
10168 (goto-char (caar c-state-cache)))
10169 (c-beginning-of-decl-1 containing-sexp) ; Can't use `lim' here.
10170 (setq placeholder (point))
10171 (if (= start (point))
10172 ;; The '}' is unbalanced.
10173 nil
10174 (c-end-of-decl-1)
10175 (>= (point) indent-point))))))
10176 (goto-char placeholder)
10177 (c-add-stmt-syntax 'topmost-intro-cont nil nil
10178 containing-sexp paren-state))
10179
10180 ;; NOTE: The point is at the end of the previous token here.
10181
10182 ;; CASE 5J: we are at the topmost level, make
10183 ;; sure we skip back past any access specifiers
10184 ((and
10185 ;; A macro continuation line is never at top level.
10186 (not (and macro-start
10187 (> indent-point macro-start)))
10188 (save-excursion
10189 (setq placeholder (point))
10190 (or (memq char-before-ip '(?\; ?{ ?} nil))
10191 (c-at-vsemi-p before-ws-ip)
10192 (when (and (eq char-before-ip ?:)
10193 (eq (c-beginning-of-statement-1 lim)
10194 'label))
10195 (c-backward-syntactic-ws lim)
10196 (setq placeholder (point)))
10197 (and (c-major-mode-is 'objc-mode)
10198 (catch 'not-in-directive
10199 (c-beginning-of-statement-1 lim)
10200 (setq placeholder (point))
10201 (while (and (c-forward-objc-directive)
10202 (< (point) indent-point))
10203 (c-forward-syntactic-ws)
10204 (if (>= (point) indent-point)
10205 (throw 'not-in-directive t))
10206 (setq placeholder (point)))
10207 nil)))))
10208 ;; For historic reasons we anchor at bol of the last
10209 ;; line of the previous declaration. That's clearly
10210 ;; highly bogus and useless, and it makes our lives hard
10211 ;; to remain compatible. :P
10212 (goto-char placeholder)
10213 (c-add-syntax 'topmost-intro (c-point 'bol))
10214 (if containing-decl-open
10215 (if (c-keyword-member containing-decl-kwd
10216 'c-other-block-decl-kwds)
10217 (progn
10218 (goto-char (c-brace-anchor-point containing-decl-open))
10219 (c-add-stmt-syntax
10220 (if (string-equal (symbol-name containing-decl-kwd)
10221 "extern")
10222 ;; Special case for compatibility with the
10223 ;; extern-lang syntactic symbols.
10224 'inextern-lang
10225 (intern (concat "in"
10226 (symbol-name containing-decl-kwd))))
10227 nil t
10228 (c-most-enclosing-brace paren-state (point))
10229 paren-state))
10230 (c-add-class-syntax 'inclass
10231 containing-decl-open
10232 containing-decl-start
10233 containing-decl-kwd
10234 paren-state)))
10235 (when (and c-syntactic-indentation-in-macros
10236 macro-start
10237 (/= macro-start (c-point 'boi indent-point)))
10238 (c-add-syntax 'cpp-define-intro)
10239 (setq macro-start nil)))
10240
10241 ;; CASE 5K: we are at an ObjC method definition
10242 ;; continuation line.
10243 ((and c-opt-method-key
10244 (save-excursion
10245 (c-beginning-of-statement-1 lim)
10246 (beginning-of-line)
10247 (when (looking-at c-opt-method-key)
10248 (setq placeholder (point)))))
10249 (c-add-syntax 'objc-method-args-cont placeholder))
10250
10251 ;; CASE 5L: we are at the first argument of a template
10252 ;; arglist that begins on the previous line.
10253 ((and c-recognize-<>-arglists
10254 (eq (char-before) ?<)
10255 (not (and c-overloadable-operators-regexp
10256 (c-after-special-operator-id lim))))
10257 (c-beginning-of-statement-1 (c-safe-position (point) paren-state))
10258 (c-add-syntax 'template-args-cont (c-point 'boi)))
10259
10260 ;; CASE 5Q: we are at a statement within a macro.
10261 (macro-start
10262 (c-beginning-of-statement-1 containing-sexp)
10263 (c-add-stmt-syntax 'statement nil t containing-sexp paren-state))
10264
10265 ;;CASE 5N: We are at a topmost continuation line and the only
10266 ;;preceding items are annotations.
10267 ((and (c-major-mode-is 'java-mode)
10268 (setq placeholder (point))
10269 (c-beginning-of-statement-1)
10270 (progn
10271 (while (and (c-forward-annotation))
10272 (c-forward-syntactic-ws))
10273 t)
10274 (prog1
10275 (>= (point) placeholder)
10276 (goto-char placeholder)))
10277 (c-add-syntax 'annotation-top-cont (c-point 'boi)))
10278
10279 ;; CASE 5M: we are at a topmost continuation line
10280 (t
10281 (c-beginning-of-statement-1 (c-safe-position (point) paren-state))
10282 (when (c-major-mode-is 'objc-mode)
10283 (setq placeholder (point))
10284 (while (and (c-forward-objc-directive)
10285 (< (point) indent-point))
10286 (c-forward-syntactic-ws)
10287 (setq placeholder (point)))
10288 (goto-char placeholder))
10289 (c-add-syntax 'topmost-intro-cont (c-point 'boi)))
10290 ))
10291
10292
10293 ;; (CASE 6 has been removed.)
10294
10295 ;; CASE 7: line is an expression, not a statement. Most
10296 ;; likely we are either in a function prototype or a function
10297 ;; call argument list
10298 ((not (or (and c-special-brace-lists
10299 (save-excursion
10300 (goto-char containing-sexp)
10301 (c-looking-at-special-brace-list)))
10302 (eq (char-after containing-sexp) ?{)))
10303 (cond
10304
10305 ;; CASE 7A: we are looking at the arglist closing paren.
10306 ;; C.f. case 7F.
10307 ((memq char-after-ip '(?\) ?\]))
10308 (goto-char containing-sexp)
10309 (setq placeholder (c-point 'boi))
10310 (if (and (c-safe (backward-up-list 1) t)
10311 (>= (point) placeholder))
10312 (progn
10313 (forward-char)
10314 (skip-chars-forward " \t"))
10315 (goto-char placeholder))
10316 (c-add-stmt-syntax 'arglist-close (list containing-sexp) t
10317 (c-most-enclosing-brace paren-state (point))
10318 paren-state))
10319
10320 ;; CASE 7B: Looking at the opening brace of an
10321 ;; in-expression block or brace list. C.f. cases 4, 16A
10322 ;; and 17E.
10323 ((and (eq char-after-ip ?{)
10324 (progn
10325 (setq placeholder (c-inside-bracelist-p (point)
10326 paren-state))
10327 (if placeholder
10328 (setq tmpsymbol '(brace-list-open . inexpr-class))
10329 (setq tmpsymbol '(block-open . inexpr-statement)
10330 placeholder
10331 (cdr-safe (c-looking-at-inexpr-block
10332 (c-safe-position containing-sexp
10333 paren-state)
10334 containing-sexp)))
10335 ;; placeholder is nil if it's a block directly in
10336 ;; a function arglist. That makes us skip out of
10337 ;; this case.
10338 )))
10339 (goto-char placeholder)
10340 (back-to-indentation)
10341 (c-add-stmt-syntax (car tmpsymbol) nil t
10342 (c-most-enclosing-brace paren-state (point))
10343 paren-state)
10344 (if (/= (point) placeholder)
10345 (c-add-syntax (cdr tmpsymbol))))
10346
10347 ;; CASE 7C: we are looking at the first argument in an empty
10348 ;; argument list. Use arglist-close if we're actually
10349 ;; looking at a close paren or bracket.
10350 ((memq char-before-ip '(?\( ?\[))
10351 (goto-char containing-sexp)
10352 (setq placeholder (c-point 'boi))
10353 (if (and (c-safe (backward-up-list 1) t)
10354 (>= (point) placeholder))
10355 (progn
10356 (forward-char)
10357 (skip-chars-forward " \t"))
10358 (goto-char placeholder))
10359 (c-add-stmt-syntax 'arglist-intro (list containing-sexp) t
10360 (c-most-enclosing-brace paren-state (point))
10361 paren-state))
10362
10363 ;; CASE 7D: we are inside a conditional test clause. treat
10364 ;; these things as statements
10365 ((progn
10366 (goto-char containing-sexp)
10367 (and (c-safe (c-forward-sexp -1) t)
10368 (looking-at "\\<for\\>[^_]")))
10369 (goto-char (1+ containing-sexp))
10370 (c-forward-syntactic-ws indent-point)
10371 (if (eq char-before-ip ?\;)
10372 (c-add-syntax 'statement (point))
10373 (c-add-syntax 'statement-cont (point))
10374 ))
10375
10376 ;; CASE 7E: maybe a continued ObjC method call. This is the
10377 ;; case when we are inside a [] bracketed exp, and what
10378 ;; precede the opening bracket is not an identifier.
10379 ((and c-opt-method-key
10380 (eq (char-after containing-sexp) ?\[)
10381 (progn
10382 (goto-char (1- containing-sexp))
10383 (c-backward-syntactic-ws (c-point 'bod))
10384 (if (not (looking-at c-symbol-key))
10385 (c-add-syntax 'objc-method-call-cont containing-sexp))
10386 )))
10387
10388 ;; CASE 7F: we are looking at an arglist continuation line,
10389 ;; but the preceding argument is on the same line as the
10390 ;; opening paren. This case includes multi-line
10391 ;; mathematical paren groupings, but we could be on a
10392 ;; for-list continuation line. C.f. case 7A.
10393 ((progn
10394 (goto-char (1+ containing-sexp))
10395 (< (save-excursion
10396 (c-forward-syntactic-ws)
10397 (point))
10398 (c-point 'bonl)))
10399 (goto-char containing-sexp) ; paren opening the arglist
10400 (setq placeholder (c-point 'boi))
10401 (if (and (c-safe (backward-up-list 1) t)
10402 (>= (point) placeholder))
10403 (progn
10404 (forward-char)
10405 (skip-chars-forward " \t"))
10406 (goto-char placeholder))
10407 (c-add-stmt-syntax 'arglist-cont-nonempty (list containing-sexp) t
10408 (c-most-enclosing-brace c-state-cache (point))
10409 paren-state))
10410
10411 ;; CASE 7G: we are looking at just a normal arglist
10412 ;; continuation line
10413 (t (c-forward-syntactic-ws indent-point)
10414 (c-add-syntax 'arglist-cont (c-point 'boi)))
10415 ))
10416
10417 ;; CASE 8: func-local multi-inheritance line
10418 ((and (c-major-mode-is 'c++-mode)
10419 (save-excursion
10420 (goto-char indent-point)
10421 (skip-chars-forward " \t")
10422 (looking-at c-opt-postfix-decl-spec-key)))
10423 (goto-char indent-point)
10424 (skip-chars-forward " \t")
10425 (cond
10426
10427 ;; CASE 8A: non-hanging colon on an inher intro
10428 ((eq char-after-ip ?:)
10429 (c-backward-syntactic-ws lim)
10430 (c-add-syntax 'inher-intro (c-point 'boi)))
10431
10432 ;; CASE 8B: hanging colon on an inher intro
10433 ((eq char-before-ip ?:)
10434 (c-add-syntax 'inher-intro (c-point 'boi)))
10435
10436 ;; CASE 8C: a continued inheritance line
10437 (t
10438 (c-beginning-of-inheritance-list lim)
10439 (c-add-syntax 'inher-cont (point))
10440 )))
10441
10442 ;; CASE 9: we are inside a brace-list
10443 ((and (not (c-major-mode-is 'awk-mode)) ; Maybe this isn't needed (ACM, 2002/3/29)
10444 (setq special-brace-list
10445 (or (and c-special-brace-lists ;;;; ALWAYS NIL FOR AWK!!
10446 (save-excursion
10447 (goto-char containing-sexp)
10448 (c-looking-at-special-brace-list)))
10449 (c-inside-bracelist-p containing-sexp paren-state))))
10450 (cond
10451
10452 ;; CASE 9A: In the middle of a special brace list opener.
10453 ((and (consp special-brace-list)
10454 (save-excursion
10455 (goto-char containing-sexp)
10456 (eq (char-after) ?\())
10457 (eq char-after-ip (car (cdr special-brace-list))))
10458 (goto-char (car (car special-brace-list)))
10459 (skip-chars-backward " \t")
10460 (if (and (bolp)
10461 (assoc 'statement-cont
10462 (setq placeholder (c-guess-basic-syntax))))
10463 (setq c-syntactic-context placeholder)
10464 (c-beginning-of-statement-1
10465 (c-safe-position (1- containing-sexp) paren-state))
10466 (c-forward-token-2 0)
10467 (while (looking-at c-specifier-key)
10468 (goto-char (match-end 1))
10469 (c-forward-syntactic-ws))
10470 (c-add-syntax 'brace-list-open (c-point 'boi))))
10471
10472 ;; CASE 9B: brace-list-close brace
10473 ((if (consp special-brace-list)
10474 ;; Check special brace list closer.
10475 (progn
10476 (goto-char (car (car special-brace-list)))
10477 (save-excursion
10478 (goto-char indent-point)
10479 (back-to-indentation)
10480 (or
10481 ;; We were between the special close char and the `)'.
10482 (and (eq (char-after) ?\))
10483 (eq (1+ (point)) (cdr (car special-brace-list))))
10484 ;; We were before the special close char.
10485 (and (eq (char-after) (cdr (cdr special-brace-list)))
10486 (zerop (c-forward-token-2))
10487 (eq (1+ (point)) (cdr (car special-brace-list)))))))
10488 ;; Normal brace list check.
10489 (and (eq char-after-ip ?})
10490 (c-safe (goto-char (c-up-list-backward (point))) t)
10491 (= (point) containing-sexp)))
10492 (if (eq (point) (c-point 'boi))
10493 (c-add-syntax 'brace-list-close (point))
10494 (setq lim (c-most-enclosing-brace c-state-cache (point)))
10495 (c-beginning-of-statement-1 lim)
10496 (c-add-stmt-syntax 'brace-list-close nil t lim paren-state)))
10497
10498 (t
10499 ;; Prepare for the rest of the cases below by going to the
10500 ;; token following the opening brace
10501 (if (consp special-brace-list)
10502 (progn
10503 (goto-char (car (car special-brace-list)))
10504 (c-forward-token-2 1 nil indent-point))
10505 (goto-char containing-sexp))
10506 (forward-char)
10507 (let ((start (point)))
10508 (c-forward-syntactic-ws indent-point)
10509 (goto-char (max start (c-point 'bol))))
10510 (c-skip-ws-forward indent-point)
10511 (cond
10512
10513 ;; CASE 9C: we're looking at the first line in a brace-list
10514 ((= (point) indent-point)
10515 (if (consp special-brace-list)
10516 (goto-char (car (car special-brace-list)))
10517 (goto-char containing-sexp))
10518 (if (eq (point) (c-point 'boi))
10519 (c-add-syntax 'brace-list-intro (point))
10520 (setq lim (c-most-enclosing-brace c-state-cache (point)))
10521 (c-beginning-of-statement-1 lim)
10522 (c-add-stmt-syntax 'brace-list-intro nil t lim paren-state)))
10523
10524 ;; CASE 9D: this is just a later brace-list-entry or
10525 ;; brace-entry-open
10526 (t (if (or (eq char-after-ip ?{)
10527 (and c-special-brace-lists
10528 (save-excursion
10529 (goto-char indent-point)
10530 (c-forward-syntactic-ws (c-point 'eol))
10531 (c-looking-at-special-brace-list (point)))))
10532 (c-add-syntax 'brace-entry-open (point))
10533 (c-add-syntax 'brace-list-entry (point))
10534 ))
10535 ))))
10536
10537 ;; CASE 10: A continued statement or top level construct.
10538 ((and (not (memq char-before-ip '(?\; ?:)))
10539 (not (c-at-vsemi-p before-ws-ip))
10540 (or (not (eq char-before-ip ?}))
10541 (c-looking-at-inexpr-block-backward c-state-cache))
10542 (> (point)
10543 (save-excursion
10544 (c-beginning-of-statement-1 containing-sexp)
10545 (setq placeholder (point))))
10546 (/= placeholder containing-sexp))
10547 ;; This is shared with case 18.
10548 (c-guess-continued-construct indent-point
10549 char-after-ip
10550 placeholder
10551 containing-sexp
10552 paren-state))
10553
10554 ;; CASE 16: block close brace, possibly closing the defun or
10555 ;; the class
10556 ((eq char-after-ip ?})
10557 ;; From here on we have the next containing sexp in lim.
10558 (setq lim (c-most-enclosing-brace paren-state))
10559 (goto-char containing-sexp)
10560 (cond
10561
10562 ;; CASE 16E: Closing a statement block? This catches
10563 ;; cases where it's preceded by a statement keyword,
10564 ;; which works even when used in an "invalid" context,
10565 ;; e.g. a macro argument.
10566 ((c-after-conditional)
10567 (c-backward-to-block-anchor lim)
10568 (c-add-stmt-syntax 'block-close nil t lim paren-state))
10569
10570 ;; CASE 16A: closing a lambda defun or an in-expression
10571 ;; block? C.f. cases 4, 7B and 17E.
10572 ((setq placeholder (c-looking-at-inexpr-block
10573 (c-safe-position containing-sexp paren-state)
10574 nil))
10575 (setq tmpsymbol (if (eq (car placeholder) 'inlambda)
10576 'inline-close
10577 'block-close))
10578 (goto-char containing-sexp)
10579 (back-to-indentation)
10580 (if (= containing-sexp (point))
10581 (c-add-syntax tmpsymbol (point))
10582 (goto-char (cdr placeholder))
10583 (back-to-indentation)
10584 (c-add-stmt-syntax tmpsymbol nil t
10585 (c-most-enclosing-brace paren-state (point))
10586 paren-state)
10587 (if (/= (point) (cdr placeholder))
10588 (c-add-syntax (car placeholder)))))
10589
10590 ;; CASE 16B: does this close an inline or a function in
10591 ;; a non-class declaration level block?
10592 ((save-excursion
10593 (and lim
10594 (progn
10595 (goto-char lim)
10596 (c-looking-at-decl-block
10597 (c-most-enclosing-brace paren-state lim)
10598 nil))
10599 (setq placeholder (point))))
10600 (c-backward-to-decl-anchor lim)
10601 (back-to-indentation)
10602 (if (save-excursion
10603 (goto-char placeholder)
10604 (looking-at c-other-decl-block-key))
10605 (c-add-syntax 'defun-close (point))
10606 (c-add-syntax 'inline-close (point))))
10607
10608 ;; CASE 16F: Can be a defun-close of a function declared
10609 ;; in a statement block, e.g. in Pike or when using gcc
10610 ;; extensions, but watch out for macros followed by
10611 ;; blocks. Let it through to be handled below.
10612 ;; C.f. cases B.3 and 17G.
10613 ((save-excursion
10614 (and (not (c-at-statement-start-p))
10615 (eq (c-beginning-of-statement-1 lim nil nil t) 'same)
10616 (setq placeholder (point))
10617 (let ((c-recognize-typeless-decls nil))
10618 ;; Turn off recognition of constructs that
10619 ;; lacks a type in this case, since that's more
10620 ;; likely to be a macro followed by a block.
10621 (c-forward-decl-or-cast-1 (c-point 'bosws) nil nil))))
10622 (back-to-indentation)
10623 (if (/= (point) containing-sexp)
10624 (goto-char placeholder))
10625 (c-add-stmt-syntax 'defun-close nil t lim paren-state))
10626
10627 ;; CASE 16C: If there is an enclosing brace then this is
10628 ;; a block close since defun closes inside declaration
10629 ;; level blocks have been handled above.
10630 (lim
10631 ;; If the block is preceded by a case/switch label on
10632 ;; the same line, we anchor at the first preceding label
10633 ;; at boi. The default handling in c-add-stmt-syntax
10634 ;; really fixes it better, but we do like this to keep
10635 ;; the indentation compatible with version 5.28 and
10636 ;; earlier. C.f. case 17H.
10637 (while (and (/= (setq placeholder (point)) (c-point 'boi))
10638 (eq (c-beginning-of-statement-1 lim) 'label)))
10639 (goto-char placeholder)
10640 (if (looking-at c-label-kwds-regexp)
10641 (c-add-syntax 'block-close (point))
10642 (goto-char containing-sexp)
10643 ;; c-backward-to-block-anchor not necessary here; those
10644 ;; situations are handled in case 16E above.
10645 (c-add-stmt-syntax 'block-close nil t lim paren-state)))
10646
10647 ;; CASE 16D: Only top level defun close left.
10648 (t
10649 (goto-char containing-sexp)
10650 (c-backward-to-decl-anchor lim)
10651 (c-add-stmt-syntax 'defun-close nil nil
10652 (c-most-enclosing-brace paren-state)
10653 paren-state))
10654 ))
10655
10656 ;; CASE 19: line is an expression, not a statement, and is directly
10657 ;; contained by a template delimiter. Most likely, we are in a
10658 ;; template arglist within a statement. This case is based on CASE
10659 ;; 7. At some point in the future, we may wish to create more
10660 ;; syntactic symbols such as `template-intro',
10661 ;; `template-cont-nonempty', etc., and distinguish between them as we
10662 ;; do for `arglist-intro' etc. (2009-12-07).
10663 ((and c-recognize-<>-arglists
10664 (setq containing-< (c-up-list-backward indent-point containing-sexp))
10665 (eq (char-after containing-<) ?\<))
10666 (setq placeholder (c-point 'boi containing-<))
10667 (goto-char containing-sexp) ; Most nested Lbrace/Lparen (but not
10668 ; '<') before indent-point.
10669 (if (>= (point) placeholder)
10670 (progn
10671 (forward-char)
10672 (skip-chars-forward " \t"))
10673 (goto-char placeholder))
10674 (c-add-stmt-syntax 'template-args-cont (list containing-<) t
10675 (c-most-enclosing-brace c-state-cache (point))
10676 paren-state))
10677
10678 ;; CASE 17: Statement or defun catchall.
10679 (t
10680 (goto-char indent-point)
10681 ;; Back up statements until we find one that starts at boi.
10682 (while (let* ((prev-point (point))
10683 (last-step-type (c-beginning-of-statement-1
10684 containing-sexp)))
10685 (if (= (point) prev-point)
10686 (progn
10687 (setq step-type (or step-type last-step-type))
10688 nil)
10689 (setq step-type last-step-type)
10690 (/= (point) (c-point 'boi)))))
10691 (cond
10692
10693 ;; CASE 17B: continued statement
10694 ((and (eq step-type 'same)
10695 (/= (point) indent-point))
10696 (c-add-stmt-syntax 'statement-cont nil nil
10697 containing-sexp paren-state))
10698
10699 ;; CASE 17A: After a case/default label?
10700 ((progn
10701 (while (and (eq step-type 'label)
10702 (not (looking-at c-label-kwds-regexp)))
10703 (setq step-type
10704 (c-beginning-of-statement-1 containing-sexp)))
10705 (eq step-type 'label))
10706 (c-add-stmt-syntax (if (eq char-after-ip ?{)
10707 'statement-case-open
10708 'statement-case-intro)
10709 nil t containing-sexp paren-state))
10710
10711 ;; CASE 17D: any old statement
10712 ((progn
10713 (while (eq step-type 'label)
10714 (setq step-type
10715 (c-beginning-of-statement-1 containing-sexp)))
10716 (eq step-type 'previous))
10717 (c-add-stmt-syntax 'statement nil t
10718 containing-sexp paren-state)
10719 (if (eq char-after-ip ?{)
10720 (c-add-syntax 'block-open)))
10721
10722 ;; CASE 17I: Inside a substatement block.
10723 ((progn
10724 ;; The following tests are all based on containing-sexp.
10725 (goto-char containing-sexp)
10726 ;; From here on we have the next containing sexp in lim.
10727 (setq lim (c-most-enclosing-brace paren-state containing-sexp))
10728 (c-after-conditional))
10729 (c-backward-to-block-anchor lim)
10730 (c-add-stmt-syntax 'statement-block-intro nil t
10731 lim paren-state)
10732 (if (eq char-after-ip ?{)
10733 (c-add-syntax 'block-open)))
10734
10735 ;; CASE 17E: first statement in an in-expression block.
10736 ;; C.f. cases 4, 7B and 16A.
10737 ((setq placeholder (c-looking-at-inexpr-block
10738 (c-safe-position containing-sexp paren-state)
10739 nil))
10740 (setq tmpsymbol (if (eq (car placeholder) 'inlambda)
10741 'defun-block-intro
10742 'statement-block-intro))
10743 (back-to-indentation)
10744 (if (= containing-sexp (point))
10745 (c-add-syntax tmpsymbol (point))
10746 (goto-char (cdr placeholder))
10747 (back-to-indentation)
10748 (c-add-stmt-syntax tmpsymbol nil t
10749 (c-most-enclosing-brace c-state-cache (point))
10750 paren-state)
10751 (if (/= (point) (cdr placeholder))
10752 (c-add-syntax (car placeholder))))
10753 (if (eq char-after-ip ?{)
10754 (c-add-syntax 'block-open)))
10755
10756 ;; CASE 17F: first statement in an inline, or first
10757 ;; statement in a top-level defun. we can tell this is it
10758 ;; if there are no enclosing braces that haven't been
10759 ;; narrowed out by a class (i.e. don't use bod here).
10760 ((save-excursion
10761 (or (not (setq placeholder (c-most-enclosing-brace
10762 paren-state)))
10763 (and (progn
10764 (goto-char placeholder)
10765 (eq (char-after) ?{))
10766 (c-looking-at-decl-block (c-most-enclosing-brace
10767 paren-state (point))
10768 nil))))
10769 (c-backward-to-decl-anchor lim)
10770 (back-to-indentation)
10771 (c-add-syntax 'defun-block-intro (point)))
10772
10773 ;; CASE 17G: First statement in a function declared inside
10774 ;; a normal block. This can occur in Pike and with
10775 ;; e.g. the gcc extensions, but watch out for macros
10776 ;; followed by blocks. C.f. cases B.3 and 16F.
10777 ((save-excursion
10778 (and (not (c-at-statement-start-p))
10779 (eq (c-beginning-of-statement-1 lim nil nil t) 'same)
10780 (setq placeholder (point))
10781 (let ((c-recognize-typeless-decls nil))
10782 ;; Turn off recognition of constructs that lacks
10783 ;; a type in this case, since that's more likely
10784 ;; to be a macro followed by a block.
10785 (c-forward-decl-or-cast-1 (c-point 'bosws) nil nil))))
10786 (back-to-indentation)
10787 (if (/= (point) containing-sexp)
10788 (goto-char placeholder))
10789 (c-add-stmt-syntax 'defun-block-intro nil t
10790 lim paren-state))
10791
10792 ;; CASE 17H: First statement in a block.
10793 (t
10794 ;; If the block is preceded by a case/switch label on the
10795 ;; same line, we anchor at the first preceding label at
10796 ;; boi. The default handling in c-add-stmt-syntax is
10797 ;; really fixes it better, but we do like this to keep the
10798 ;; indentation compatible with version 5.28 and earlier.
10799 ;; C.f. case 16C.
10800 (while (and (/= (setq placeholder (point)) (c-point 'boi))
10801 (eq (c-beginning-of-statement-1 lim) 'label)))
10802 (goto-char placeholder)
10803 (if (looking-at c-label-kwds-regexp)
10804 (c-add-syntax 'statement-block-intro (point))
10805 (goto-char containing-sexp)
10806 ;; c-backward-to-block-anchor not necessary here; those
10807 ;; situations are handled in case 17I above.
10808 (c-add-stmt-syntax 'statement-block-intro nil t
10809 lim paren-state))
10810 (if (eq char-after-ip ?{)
10811 (c-add-syntax 'block-open)))
10812 ))
10813 )
10814
10815 ;; now we need to look at any modifiers
10816 (goto-char indent-point)
10817 (skip-chars-forward " \t")
10818
10819 ;; are we looking at a comment only line?
10820 (when (and (looking-at c-comment-start-regexp)
10821 (/= (c-forward-token-2 0 nil (c-point 'eol)) 0))
10822 (c-append-syntax 'comment-intro))
10823
10824 ;; we might want to give additional offset to friends (in C++).
10825 (when (and c-opt-friend-key
10826 (looking-at c-opt-friend-key))
10827 (c-append-syntax 'friend))
10828
10829 ;; Set syntactic-relpos.
10830 (let ((p c-syntactic-context))
10831 (while (and p
10832 (if (integerp (c-langelem-pos (car p)))
10833 (progn
10834 (setq syntactic-relpos (c-langelem-pos (car p)))
10835 nil)
10836 t))
10837 (setq p (cdr p))))
10838
10839 ;; Start of or a continuation of a preprocessor directive?
10840 (if (and macro-start
10841 (eq macro-start (c-point 'boi))
10842 (not (and (c-major-mode-is 'pike-mode)
10843 (eq (char-after (1+ macro-start)) ?\"))))
10844 (c-append-syntax 'cpp-macro)
10845 (when (and c-syntactic-indentation-in-macros macro-start)
10846 (if in-macro-expr
10847 (when (or
10848 (< syntactic-relpos macro-start)
10849 (not (or
10850 (assq 'arglist-intro c-syntactic-context)
10851 (assq 'arglist-cont c-syntactic-context)
10852 (assq 'arglist-cont-nonempty c-syntactic-context)
10853 (assq 'arglist-close c-syntactic-context))))
10854 ;; If inside a cpp expression, i.e. anywhere in a
10855 ;; cpp directive except a #define body, we only let
10856 ;; through the syntactic analysis that is internal
10857 ;; in the expression. That means the arglist
10858 ;; elements, if they are anchored inside the cpp
10859 ;; expression.
10860 (setq c-syntactic-context nil)
10861 (c-add-syntax 'cpp-macro-cont macro-start))
10862 (when (and (eq macro-start syntactic-relpos)
10863 (not (assq 'cpp-define-intro c-syntactic-context))
10864 (save-excursion
10865 (goto-char macro-start)
10866 (or (not (c-forward-to-cpp-define-body))
10867 (<= (point) (c-point 'boi indent-point)))))
10868 ;; Inside a #define body and the syntactic analysis is
10869 ;; anchored on the start of the #define. In this case
10870 ;; we add cpp-define-intro to get the extra
10871 ;; indentation of the #define body.
10872 (c-add-syntax 'cpp-define-intro)))))
10873
10874 ;; return the syntax
10875 c-syntactic-context)))
10876
10877 \f
10878 ;; Indentation calculation.
10879
10880 (defun c-evaluate-offset (offset langelem symbol)
10881 ;; offset can be a number, a function, a variable, a list, or one of
10882 ;; the symbols + or -
10883 ;;
10884 ;; This function might do hidden buffer changes.
10885 (let ((res
10886 (cond
10887 ((numberp offset) offset)
10888 ((vectorp offset) offset)
10889 ((null offset) nil)
10890
10891 ((eq offset '+) c-basic-offset)
10892 ((eq offset '-) (- c-basic-offset))
10893 ((eq offset '++) (* 2 c-basic-offset))
10894 ((eq offset '--) (* 2 (- c-basic-offset)))
10895 ((eq offset '*) (/ c-basic-offset 2))
10896 ((eq offset '/) (/ (- c-basic-offset) 2))
10897
10898 ((functionp offset)
10899 (c-evaluate-offset
10900 (funcall offset
10901 (cons (c-langelem-sym langelem)
10902 (c-langelem-pos langelem)))
10903 langelem symbol))
10904
10905 ((listp offset)
10906 (cond
10907 ((eq (car offset) 'quote)
10908 (c-benign-error "The offset %S for %s was mistakenly quoted"
10909 offset symbol)
10910 nil)
10911
10912 ((memq (car offset) '(min max))
10913 (let (res val (method (car offset)))
10914 (setq offset (cdr offset))
10915 (while offset
10916 (setq val (c-evaluate-offset (car offset) langelem symbol))
10917 (cond
10918 ((not val))
10919 ((not res)
10920 (setq res val))
10921 ((integerp val)
10922 (if (vectorp res)
10923 (c-benign-error "\
10924 Error evaluating offset %S for %s: \
10925 Cannot combine absolute offset %S with relative %S in `%s' method"
10926 (car offset) symbol res val method)
10927 (setq res (funcall method res val))))
10928 (t
10929 (if (integerp res)
10930 (c-benign-error "\
10931 Error evaluating offset %S for %s: \
10932 Cannot combine relative offset %S with absolute %S in `%s' method"
10933 (car offset) symbol res val method)
10934 (setq res (vector (funcall method (aref res 0)
10935 (aref val 0)))))))
10936 (setq offset (cdr offset)))
10937 res))
10938
10939 ((eq (car offset) 'add)
10940 (let (res val)
10941 (setq offset (cdr offset))
10942 (while offset
10943 (setq val (c-evaluate-offset (car offset) langelem symbol))
10944 (cond
10945 ((not val))
10946 ((not res)
10947 (setq res val))
10948 ((integerp val)
10949 (if (vectorp res)
10950 (setq res (vector (+ (aref res 0) val)))
10951 (setq res (+ res val))))
10952 (t
10953 (if (vectorp res)
10954 (c-benign-error "\
10955 Error evaluating offset %S for %s: \
10956 Cannot combine absolute offsets %S and %S in `add' method"
10957 (car offset) symbol res val)
10958 (setq res val)))) ; Override.
10959 (setq offset (cdr offset)))
10960 res))
10961
10962 (t
10963 (let (res)
10964 (when (eq (car offset) 'first)
10965 (setq offset (cdr offset)))
10966 (while (and (not res) offset)
10967 (setq res (c-evaluate-offset (car offset) langelem symbol)
10968 offset (cdr offset)))
10969 res))))
10970
10971 ((and (symbolp offset) (boundp offset))
10972 (symbol-value offset))
10973
10974 (t
10975 (c-benign-error "Unknown offset format %S for %s" offset symbol)
10976 nil))))
10977
10978 (if (or (null res) (integerp res)
10979 (and (vectorp res) (= (length res) 1) (integerp (aref res 0))))
10980 res
10981 (c-benign-error "Error evaluating offset %S for %s: Got invalid value %S"
10982 offset symbol res)
10983 nil)))
10984
10985 (defun c-calc-offset (langelem)
10986 ;; Get offset from LANGELEM which is a list beginning with the
10987 ;; syntactic symbol and followed by any analysis data it provides.
10988 ;; That data may be zero or more elements, but if at least one is
10989 ;; given then the first is the anchor position (or nil). The symbol
10990 ;; is matched against `c-offsets-alist' and the offset calculated
10991 ;; from that is returned.
10992 ;;
10993 ;; This function might do hidden buffer changes.
10994 (let* ((symbol (c-langelem-sym langelem))
10995 (match (assq symbol c-offsets-alist))
10996 (offset (cdr-safe match)))
10997 (if match
10998 (setq offset (c-evaluate-offset offset langelem symbol))
10999 (if c-strict-syntax-p
11000 (c-benign-error "No offset found for syntactic symbol %s" symbol))
11001 (setq offset 0))
11002 (if (vectorp offset)
11003 offset
11004 (or (and (numberp offset) offset)
11005 (and (symbolp offset) (symbol-value offset))
11006 0))
11007 ))
11008
11009 (defun c-get-offset (langelem)
11010 ;; This is a compatibility wrapper for `c-calc-offset' in case
11011 ;; someone is calling it directly. It takes an old style syntactic
11012 ;; element on the form (SYMBOL . ANCHOR-POS) and converts it to the
11013 ;; new list form.
11014 ;;
11015 ;; This function might do hidden buffer changes.
11016 (if (c-langelem-pos langelem)
11017 (c-calc-offset (list (c-langelem-sym langelem)
11018 (c-langelem-pos langelem)))
11019 (c-calc-offset langelem)))
11020
11021 (defun c-get-syntactic-indentation (langelems)
11022 ;; Calculate the syntactic indentation from a syntactic description
11023 ;; as returned by `c-guess-syntax'.
11024 ;;
11025 ;; Note that topmost-intro always has an anchor position at bol, for
11026 ;; historical reasons. It's often used together with other symbols
11027 ;; that has more sane positions. Since we always use the first
11028 ;; found anchor position, we rely on that these other symbols always
11029 ;; precede topmost-intro in the LANGELEMS list.
11030 ;;
11031 ;; This function might do hidden buffer changes.
11032 (let ((indent 0) anchor)
11033
11034 (while langelems
11035 (let* ((c-syntactic-element (car langelems))
11036 (res (c-calc-offset c-syntactic-element)))
11037
11038 (if (vectorp res)
11039 ;; Got an absolute column that overrides any indentation
11040 ;; we've collected so far, but not the relative
11041 ;; indentation we might get for the nested structures
11042 ;; further down the langelems list.
11043 (setq indent (elt res 0)
11044 anchor (point-min)) ; A position at column 0.
11045
11046 ;; Got a relative change of the current calculated
11047 ;; indentation.
11048 (setq indent (+ indent res))
11049
11050 ;; Use the anchor position from the first syntactic
11051 ;; element with one.
11052 (unless anchor
11053 (setq anchor (c-langelem-pos (car langelems)))))
11054
11055 (setq langelems (cdr langelems))))
11056
11057 (if anchor
11058 (+ indent (save-excursion
11059 (goto-char anchor)
11060 (current-column)))
11061 indent)))
11062
11063 \f
11064 (cc-provide 'cc-engine)
11065
11066 ;;; cc-engine.el ends here