Merge from trunk
[bpt/emacs.git] / lisp / progmodes / cc-engine.el
1 ;;; cc-engine.el --- core syntax guessing engine for CC mode
2
3 ;; Copyright (C) 1985, 1987, 1992-2011 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 ;; Silence the compiler.
151 (cc-bytecomp-defun buffer-syntactic-context) ; XEmacs
152
153 \f
154 ;; Make declarations for all the `c-lang-defvar' variables in cc-langs.
155
156 (defmacro c-declare-lang-variables ()
157 `(progn
158 ,@(apply 'nconc
159 (mapcar (lambda (init)
160 `(,(if (elt init 2)
161 `(defvar ,(car init) nil ,(elt init 2))
162 `(defvar ,(car init) nil))
163 (make-variable-buffer-local ',(car init))))
164 (cdr c-lang-variable-inits)))))
165 (c-declare-lang-variables)
166
167 \f
168 ;;; Internal state variables.
169
170 ;; Internal state of hungry delete key feature
171 (defvar c-hungry-delete-key nil)
172 (make-variable-buffer-local 'c-hungry-delete-key)
173
174 ;; The electric flag (toggled by `c-toggle-electric-state').
175 ;; If t, electric actions (like automatic reindentation, and (if
176 ;; c-auto-newline is also set) auto newlining) will happen when an electric
177 ;; key like `{' is pressed (or an electric keyword like `else').
178 (defvar c-electric-flag t)
179 (make-variable-buffer-local 'c-electric-flag)
180
181 ;; Internal state of auto newline feature.
182 (defvar c-auto-newline nil)
183 (make-variable-buffer-local 'c-auto-newline)
184
185 ;; Included in the mode line to indicate the active submodes.
186 ;; (defvar c-submode-indicators nil)
187 ;; (make-variable-buffer-local 'c-submode-indicators)
188
189 (defun c-calculate-state (arg prevstate)
190 ;; Calculate the new state of PREVSTATE, t or nil, based on arg. If
191 ;; arg is nil or zero, toggle the state. If arg is negative, turn
192 ;; the state off, and if arg is positive, turn the state on
193 (if (or (not arg)
194 (zerop (setq arg (prefix-numeric-value arg))))
195 (not prevstate)
196 (> arg 0)))
197
198 ;; Dynamically bound cache for `c-in-literal'.
199 (defvar c-in-literal-cache t)
200
201 \f
202 ;; Basic handling of preprocessor directives.
203
204 ;; This is a dynamically bound cache used together with
205 ;; `c-query-macro-start' and `c-query-and-set-macro-start'. It only
206 ;; works as long as point doesn't cross a macro boundary.
207 (defvar c-macro-start 'unknown)
208
209 (defsubst c-query-and-set-macro-start ()
210 (if (symbolp c-macro-start)
211 (setq c-macro-start (save-excursion
212 (c-save-buffer-state ()
213 (and (c-beginning-of-macro)
214 (point)))))
215 c-macro-start))
216
217 (defsubst c-query-macro-start ()
218 (if (symbolp c-macro-start)
219 (save-excursion
220 (c-save-buffer-state ()
221 (and (c-beginning-of-macro)
222 (point))))
223 c-macro-start))
224
225 (defun c-beginning-of-macro (&optional lim)
226 "Go to the beginning of a preprocessor directive.
227 Leave point at the beginning of the directive and return t if in one,
228 otherwise return nil and leave point unchanged.
229
230 Note that this function might do hidden buffer changes. See the
231 comment at the start of cc-engine.el for more info."
232 (when c-opt-cpp-prefix
233 (let ((here (point)))
234 (save-restriction
235 (if lim (narrow-to-region lim (point-max)))
236 (beginning-of-line)
237 (while (eq (char-before (1- (point))) ?\\)
238 (forward-line -1))
239 (back-to-indentation)
240 (if (and (<= (point) here)
241 (looking-at c-opt-cpp-start))
242 t
243 (goto-char here)
244 nil)))))
245
246 (defun c-end-of-macro ()
247 "Go to the end of a preprocessor directive.
248 More accurately, move the point to the end of the closest following
249 line that doesn't end with a line continuation backslash - no check is
250 done that the point is inside a cpp directive to begin with.
251
252 Note that this function might do hidden buffer changes. See the
253 comment at the start of cc-engine.el for more info."
254 (while (progn
255 (end-of-line)
256 (when (and (eq (char-before) ?\\)
257 (not (eobp)))
258 (forward-char)
259 t))))
260
261 (defun c-syntactic-end-of-macro ()
262 ;; Go to the end of a CPP directive, or a "safe" pos just before.
263 ;;
264 ;; This is normally the end of the next non-escaped line. A "safe"
265 ;; position is one not within a string or comment. (The EOL on a line
266 ;; comment is NOT "safe").
267 ;;
268 ;; This function must only be called from the beginning of a CPP construct.
269 ;;
270 ;; Note that this function might do hidden buffer changes. See the comment
271 ;; at the start of cc-engine.el for more info.
272 (let* ((here (point))
273 (there (progn (c-end-of-macro) (point)))
274 (s (parse-partial-sexp here there)))
275 (while (and (or (nth 3 s) ; in a string
276 (nth 4 s)) ; in a comment (maybe at end of line comment)
277 (> there here)) ; No infinite loops, please.
278 (setq there (1- (nth 8 s)))
279 (setq s (parse-partial-sexp here there)))
280 (point)))
281
282 (defun c-forward-over-cpp-define-id ()
283 ;; Assuming point is at the "#" that introduces a preprocessor
284 ;; directive, it's moved forward to the end of the identifier which is
285 ;; "#define"d (or whatever c-opt-cpp-macro-define specifies). Non-nil
286 ;; is returned in this case, in all other cases nil is returned and
287 ;; point isn't moved.
288 ;;
289 ;; This function might do hidden buffer changes.
290 (when (and c-opt-cpp-macro-define-id
291 (looking-at c-opt-cpp-macro-define-id))
292 (goto-char (match-end 0))))
293
294 (defun c-forward-to-cpp-define-body ()
295 ;; Assuming point is at the "#" that introduces a preprocessor
296 ;; directive, it's moved forward to the start of the definition body
297 ;; if it's a "#define" (or whatever c-opt-cpp-macro-define
298 ;; specifies). Non-nil is returned in this case, in all other cases
299 ;; nil is returned and point isn't moved.
300 ;;
301 ;; This function might do hidden buffer changes.
302 (when (and c-opt-cpp-macro-define-start
303 (looking-at c-opt-cpp-macro-define-start)
304 (not (= (match-end 0) (c-point 'eol))))
305 (goto-char (match-end 0))))
306
307 \f
308 ;;; Basic utility functions.
309
310 (defun c-syntactic-content (from to paren-level)
311 ;; Return the given region as a string where all syntactic
312 ;; whitespace is removed or, where necessary, replaced with a single
313 ;; space. If PAREN-LEVEL is given then all parens in the region are
314 ;; collapsed to "()", "[]" etc.
315 ;;
316 ;; This function might do hidden buffer changes.
317
318 (save-excursion
319 (save-restriction
320 (narrow-to-region from to)
321 (goto-char from)
322 (let* ((parts (list nil)) (tail parts) pos in-paren)
323
324 (while (re-search-forward c-syntactic-ws-start to t)
325 (goto-char (setq pos (match-beginning 0)))
326 (c-forward-syntactic-ws)
327 (if (= (point) pos)
328 (forward-char)
329
330 (when paren-level
331 (save-excursion
332 (setq in-paren (= (car (parse-partial-sexp from pos 1)) 1)
333 pos (point))))
334
335 (if (and (> pos from)
336 (< (point) to)
337 (looking-at "\\w\\|\\s_")
338 (save-excursion
339 (goto-char (1- pos))
340 (looking-at "\\w\\|\\s_")))
341 (progn
342 (setcdr tail (list (buffer-substring-no-properties from pos)
343 " "))
344 (setq tail (cddr tail)))
345 (setcdr tail (list (buffer-substring-no-properties from pos)))
346 (setq tail (cdr tail)))
347
348 (when in-paren
349 (when (= (car (parse-partial-sexp pos to -1)) -1)
350 (setcdr tail (list (buffer-substring-no-properties
351 (1- (point)) (point))))
352 (setq tail (cdr tail))))
353
354 (setq from (point))))
355
356 (setcdr tail (list (buffer-substring-no-properties from to)))
357 (apply 'concat (cdr parts))))))
358
359 (defun c-shift-line-indentation (shift-amt)
360 ;; Shift the indentation of the current line with the specified
361 ;; amount (positive inwards). The buffer is modified only if
362 ;; SHIFT-AMT isn't equal to zero.
363 (let ((pos (- (point-max) (point)))
364 (c-macro-start c-macro-start)
365 tmp-char-inserted)
366 (if (zerop shift-amt)
367 nil
368 ;; If we're on an empty line inside a macro, we take the point
369 ;; to be at the current indentation and shift it to the
370 ;; appropriate column. This way we don't treat the extra
371 ;; whitespace out to the line continuation as indentation.
372 (when (and (c-query-and-set-macro-start)
373 (looking-at "[ \t]*\\\\$")
374 (save-excursion
375 (skip-chars-backward " \t")
376 (bolp)))
377 (insert ?x)
378 (backward-char)
379 (setq tmp-char-inserted t))
380 (unwind-protect
381 (let ((col (current-indentation)))
382 (delete-region (c-point 'bol) (c-point 'boi))
383 (beginning-of-line)
384 (indent-to (+ col shift-amt)))
385 (when tmp-char-inserted
386 (delete-char 1))))
387 ;; If initial point was within line's indentation and we're not on
388 ;; a line with a line continuation in a macro, position after the
389 ;; indentation. Else stay at same point in text.
390 (if (and (< (point) (c-point 'boi))
391 (not tmp-char-inserted))
392 (back-to-indentation)
393 (if (> (- (point-max) pos) (point))
394 (goto-char (- (point-max) pos))))))
395
396 (defsubst c-keyword-sym (keyword)
397 ;; Return non-nil if the string KEYWORD is a known keyword. More
398 ;; precisely, the value is the symbol for the keyword in
399 ;; `c-keywords-obarray'.
400 (intern-soft keyword c-keywords-obarray))
401
402 (defsubst c-keyword-member (keyword-sym lang-constant)
403 ;; Return non-nil if the symbol KEYWORD-SYM, as returned by
404 ;; `c-keyword-sym', is a member of LANG-CONSTANT, which is the name
405 ;; of a language constant that ends with "-kwds". If KEYWORD-SYM is
406 ;; nil then the result is nil.
407 (get keyword-sym lang-constant))
408
409 ;; String syntax chars, suitable for skip-syntax-(forward|backward).
410 (defconst c-string-syntax (if (memq 'gen-string-delim c-emacs-features)
411 "\"|"
412 "\""))
413
414 ;; Regexp matching string limit syntax.
415 (defconst c-string-limit-regexp (if (memq 'gen-string-delim c-emacs-features)
416 "\\s\"\\|\\s|"
417 "\\s\""))
418
419 ;; Regexp matching WS followed by string limit syntax.
420 (defconst c-ws*-string-limit-regexp
421 (concat "[ \t]*\\(" c-string-limit-regexp "\\)"))
422
423 ;; Holds formatted error strings for the few cases where parse errors
424 ;; are reported.
425 (defvar c-parsing-error nil)
426 (make-variable-buffer-local 'c-parsing-error)
427
428 (defun c-echo-parsing-error (&optional quiet)
429 (when (and c-report-syntactic-errors c-parsing-error (not quiet))
430 (c-benign-error "%s" c-parsing-error))
431 c-parsing-error)
432
433 ;; Faces given to comments and string literals. This is used in some
434 ;; situations to speed up recognition; it isn't mandatory that font
435 ;; locking is in use. This variable is extended with the face in
436 ;; `c-doc-face-name' when fontification is activated in cc-fonts.el.
437 (defvar c-literal-faces
438 (append '(font-lock-comment-face font-lock-string-face)
439 (when (facep 'font-lock-comment-delimiter-face)
440 ;; New in Emacs 22.
441 '(font-lock-comment-delimiter-face))))
442
443 (defsubst c-put-c-type-property (pos value)
444 ;; Put a c-type property with the given value at POS.
445 (c-put-char-property pos 'c-type value))
446
447 (defun c-clear-c-type-property (from to value)
448 ;; Remove all occurrences of the c-type property that has the given
449 ;; value in the region between FROM and TO. VALUE is assumed to not
450 ;; be nil.
451 ;;
452 ;; Note: This assumes that c-type is put on single chars only; it's
453 ;; very inefficient if matching properties cover large regions.
454 (save-excursion
455 (goto-char from)
456 (while (progn
457 (when (eq (get-text-property (point) 'c-type) value)
458 (c-clear-char-property (point) 'c-type))
459 (goto-char (next-single-property-change (point) 'c-type nil to))
460 (< (point) to)))))
461
462 \f
463 ;; Some debug tools to visualize various special positions. This
464 ;; debug code isn't as portable as the rest of CC Mode.
465
466 (cc-bytecomp-defun overlays-in)
467 (cc-bytecomp-defun overlay-get)
468 (cc-bytecomp-defun overlay-start)
469 (cc-bytecomp-defun overlay-end)
470 (cc-bytecomp-defun delete-overlay)
471 (cc-bytecomp-defun overlay-put)
472 (cc-bytecomp-defun make-overlay)
473
474 (defun c-debug-add-face (beg end face)
475 (c-save-buffer-state ((overlays (overlays-in beg end)) overlay)
476 (while overlays
477 (setq overlay (car overlays)
478 overlays (cdr overlays))
479 (when (eq (overlay-get overlay 'face) face)
480 (setq beg (min beg (overlay-start overlay))
481 end (max end (overlay-end overlay)))
482 (delete-overlay overlay)))
483 (overlay-put (make-overlay beg end) 'face face)))
484
485 (defun c-debug-remove-face (beg end face)
486 (c-save-buffer-state ((overlays (overlays-in beg end)) overlay
487 (ol-beg beg) (ol-end end))
488 (while overlays
489 (setq overlay (car overlays)
490 overlays (cdr overlays))
491 (when (eq (overlay-get overlay 'face) face)
492 (setq ol-beg (min ol-beg (overlay-start overlay))
493 ol-end (max ol-end (overlay-end overlay)))
494 (delete-overlay overlay)))
495 (when (< ol-beg beg)
496 (overlay-put (make-overlay ol-beg beg) 'face face))
497 (when (> ol-end end)
498 (overlay-put (make-overlay end ol-end) 'face face))))
499
500 \f
501 ;; `c-beginning-of-statement-1' and accompanying stuff.
502
503 ;; KLUDGE ALERT: c-maybe-labelp is used to pass information between
504 ;; c-crosses-statement-barrier-p and c-beginning-of-statement-1. A
505 ;; better way should be implemented, but this will at least shut up
506 ;; the byte compiler.
507 (defvar c-maybe-labelp)
508
509 ;; New awk-compatible version of c-beginning-of-statement-1, ACM 2002/6/22
510
511 ;; Macros used internally in c-beginning-of-statement-1 for the
512 ;; automaton actions.
513 (defmacro c-bos-push-state ()
514 '(setq stack (cons (cons state saved-pos)
515 stack)))
516 (defmacro c-bos-pop-state (&optional do-if-done)
517 `(if (setq state (car (car stack))
518 saved-pos (cdr (car stack))
519 stack (cdr stack))
520 t
521 ,do-if-done
522 (throw 'loop nil)))
523 (defmacro c-bos-pop-state-and-retry ()
524 '(throw 'loop (setq state (car (car stack))
525 saved-pos (cdr (car stack))
526 ;; Throw nil if stack is empty, else throw non-nil.
527 stack (cdr stack))))
528 (defmacro c-bos-save-pos ()
529 '(setq saved-pos (vector pos tok ptok pptok)))
530 (defmacro c-bos-restore-pos ()
531 '(unless (eq (elt saved-pos 0) start)
532 (setq pos (elt saved-pos 0)
533 tok (elt saved-pos 1)
534 ptok (elt saved-pos 2)
535 pptok (elt saved-pos 3))
536 (goto-char pos)
537 (setq sym nil)))
538 (defmacro c-bos-save-error-info (missing got)
539 `(setq saved-pos (vector pos ,missing ,got)))
540 (defmacro c-bos-report-error ()
541 '(unless noerror
542 (setq c-parsing-error
543 (format "No matching `%s' found for `%s' on line %d"
544 (elt saved-pos 1)
545 (elt saved-pos 2)
546 (1+ (count-lines (point-min)
547 (c-point 'bol (elt saved-pos 0))))))))
548
549 (defun c-beginning-of-statement-1 (&optional lim ignore-labels
550 noerror comma-delim)
551 "Move to the start of the current statement or declaration, or to
552 the previous one if already at the beginning of one. Only
553 statements/declarations on the same level are considered, i.e. don't
554 move into or out of sexps (not even normal expression parentheses).
555
556 If point is already at the earliest statement within braces or parens,
557 this function doesn't move back into any whitespace preceding it; it
558 returns 'same in this case.
559
560 Stop at statement continuation tokens like \"else\", \"catch\",
561 \"finally\" and the \"while\" in \"do ... while\" if the start point
562 is within the continuation. If starting at such a token, move to the
563 corresponding statement start. If at the beginning of a statement,
564 move to the closest containing statement if there is any. This might
565 also stop at a continuation clause.
566
567 Labels are treated as part of the following statements if
568 IGNORE-LABELS is non-nil. (FIXME: Doesn't work if we stop at a known
569 statement start keyword.) Otherwise, each label is treated as a
570 separate statement.
571
572 Macros are ignored \(i.e. skipped over) unless point is within one, in
573 which case the content of the macro is treated as normal code. Aside
574 from any normal statement starts found in it, stop at the first token
575 of the content in the macro, i.e. the expression of an \"#if\" or the
576 start of the definition in a \"#define\". Also stop at start of
577 macros before leaving them.
578
579 Return:
580 'label if stopped at a label or \"case...:\" or \"default:\";
581 'same if stopped at the beginning of the current statement;
582 'up if stepped to a containing statement;
583 'previous if stepped to a preceding statement;
584 'beginning if stepped from a statement continuation clause to
585 its start clause; or
586 'macro if stepped to a macro start.
587 Note that 'same and not 'label is returned if stopped at the same
588 label without crossing the colon character.
589
590 LIM may be given to limit the search. If the search hits the limit,
591 point will be left at the closest following token, or at the start
592 position if that is less ('same is returned in this case).
593
594 NOERROR turns off error logging to `c-parsing-error'.
595
596 Normally only ';' and virtual semicolons are considered to delimit
597 statements, but if COMMA-DELIM is non-nil then ',' is treated
598 as a delimiter too.
599
600 Note that this function might do hidden buffer changes. See the
601 comment at the start of cc-engine.el for more info."
602
603 ;; The bulk of this function is a pushdown automaton that looks at statement
604 ;; boundaries and the tokens (such as "while") in c-opt-block-stmt-key. Its
605 ;; purpose is to keep track of nested statements, ensuring that such
606 ;; statements are skipped over in their entirety (somewhat akin to what C-M-p
607 ;; does with nested braces/brackets/parentheses).
608 ;;
609 ;; Note: The position of a boundary is the following token.
610 ;;
611 ;; Beginning with the current token (the one following point), move back one
612 ;; sexp at a time (where a sexp is, more or less, either a token or the
613 ;; entire contents of a brace/bracket/paren pair). Each time a statement
614 ;; boundary is crossed or a "while"-like token is found, update the state of
615 ;; the PDA. Stop at the beginning of a statement when the stack (holding
616 ;; nested statement info) is empty and the position has been moved.
617 ;;
618 ;; The following variables constitute the PDA:
619 ;;
620 ;; sym: This is either the "while"-like token (e.g. 'for) we've just
621 ;; scanned back over, 'boundary if we've just gone back over a
622 ;; statement boundary, or nil otherwise.
623 ;; state: takes one of the values (nil else else-boundary while
624 ;; while-boundary catch catch-boundary).
625 ;; nil means "no "while"-like token yet scanned".
626 ;; 'else, for example, means "just gone back over an else".
627 ;; 'else-boundary means "just gone back over a statement boundary
628 ;; immediately after having gone back over an else".
629 ;; saved-pos: A vector of either saved positions (tok ptok pptok, etc.) or
630 ;; of error reporting information.
631 ;; stack: The stack onto which the PDA pushes its state. Each entry
632 ;; consists of a saved value of state and saved-pos. An entry is
633 ;; pushed when we move back over a "continuation" token (e.g. else)
634 ;; and popped when we encounter the corresponding opening token
635 ;; (e.g. if).
636 ;;
637 ;;
638 ;; The following diagram briefly outlines the PDA.
639 ;;
640 ;; Common state:
641 ;; "else": Push state, goto state `else'.
642 ;; "while": Push state, goto state `while'.
643 ;; "catch" or "finally": Push state, goto state `catch'.
644 ;; boundary: Pop state.
645 ;; other: Do nothing special.
646 ;;
647 ;; State `else':
648 ;; boundary: Goto state `else-boundary'.
649 ;; other: Error, pop state, retry token.
650 ;;
651 ;; State `else-boundary':
652 ;; "if": Pop state.
653 ;; boundary: Error, pop state.
654 ;; other: See common state.
655 ;;
656 ;; State `while':
657 ;; boundary: Save position, goto state `while-boundary'.
658 ;; other: Pop state, retry token.
659 ;;
660 ;; State `while-boundary':
661 ;; "do": Pop state.
662 ;; boundary: Restore position if it's not at start, pop state. [*see below]
663 ;; other: See common state.
664 ;;
665 ;; State `catch':
666 ;; boundary: Goto state `catch-boundary'.
667 ;; other: Error, pop state, retry token.
668 ;;
669 ;; State `catch-boundary':
670 ;; "try": Pop state.
671 ;; "catch": Goto state `catch'.
672 ;; boundary: Error, pop state.
673 ;; other: See common state.
674 ;;
675 ;; [*] In the `while-boundary' state, we had pushed a 'while state, and were
676 ;; searching for a "do" which would have opened a do-while. If we didn't
677 ;; find it, we discard the analysis done since the "while", go back to this
678 ;; token in the buffer and restart the scanning there, this time WITHOUT
679 ;; pushing the 'while state onto the stack.
680 ;;
681 ;; In addition to the above there is some special handling of labels
682 ;; and macros.
683
684 (let ((case-fold-search nil)
685 (start (point))
686 macro-start
687 (delims (if comma-delim '(?\; ?,) '(?\;)))
688 (c-stmt-delim-chars (if comma-delim
689 c-stmt-delim-chars-with-comma
690 c-stmt-delim-chars))
691 c-in-literal-cache c-maybe-labelp after-case:-pos saved
692 ;; Current position.
693 pos
694 ;; Position of last stmt boundary character (e.g. ;).
695 boundary-pos
696 ;; The position of the last sexp or bound that follows the
697 ;; first found colon, i.e. the start of the nonlabel part of
698 ;; the statement. It's `start' if a colon is found just after
699 ;; the start.
700 after-labels-pos
701 ;; Like `after-labels-pos', but the first such position inside
702 ;; a label, i.e. the start of the last label before the start
703 ;; of the nonlabel part of the statement.
704 last-label-pos
705 ;; The last position where a label is possible provided the
706 ;; statement started there. It's nil as long as no invalid
707 ;; label content has been found (according to
708 ;; `c-nonlabel-token-key'. It's `start' if no valid label
709 ;; content was found in the label. Note that we might still
710 ;; regard it a label if it starts with `c-label-kwds'.
711 label-good-pos
712 ;; Symbol just scanned back over (e.g. 'while or 'boundary).
713 ;; See above.
714 sym
715 ;; Current state in the automaton. See above.
716 state
717 ;; Current saved positions. See above.
718 saved-pos
719 ;; Stack of conses (state . saved-pos).
720 stack
721 ;; Regexp which matches "for", "if", etc.
722 (cond-key (or c-opt-block-stmt-key
723 "\\<\\>")) ; Matches nothing.
724 ;; Return value.
725 (ret 'same)
726 ;; Positions of the last three sexps or bounds we've stopped at.
727 tok ptok pptok)
728
729 (save-restriction
730 (if lim (narrow-to-region lim (point-max)))
731
732 (if (save-excursion
733 (and (c-beginning-of-macro)
734 (/= (point) start)))
735 (setq macro-start (point)))
736
737 ;; Try to skip back over unary operator characters, to register
738 ;; that we've moved.
739 (while (progn
740 (setq pos (point))
741 (c-backward-syntactic-ws)
742 ;; Protect post-++/-- operators just before a virtual semicolon.
743 (and (not (c-at-vsemi-p))
744 (/= (skip-chars-backward "-+!*&~@`#") 0))))
745
746 ;; Skip back over any semicolon here. If it was a bare semicolon, we're
747 ;; done. Later on we ignore the boundaries for statements that don't
748 ;; contain any sexp. The only thing that is affected is that the error
749 ;; checking is a little less strict, and we really don't bother.
750 (if (and (memq (char-before) delims)
751 (progn (forward-char -1)
752 (setq saved (point))
753 (c-backward-syntactic-ws)
754 (or (memq (char-before) delims)
755 (memq (char-before) '(?: nil))
756 (eq (char-syntax (char-before)) ?\()
757 (c-at-vsemi-p))))
758 (setq ret 'previous
759 pos saved)
760
761 ;; Begin at start and not pos to detect macros if we stand
762 ;; directly after the #.
763 (goto-char start)
764 (if (looking-at "\\<\\|\\W")
765 ;; Record this as the first token if not starting inside it.
766 (setq tok start))
767
768 ;; The following while loop goes back one sexp (balanced parens,
769 ;; etc. with contents, or symbol or suchlike) each iteration. This
770 ;; movement is accomplished with a call to scan-sexps approx 130 lines
771 ;; below.
772 (while
773 (catch 'loop ;; Throw nil to break, non-nil to continue.
774 (cond
775 ((save-excursion
776 (and macro-start ; Always NIL for AWK.
777 (progn (skip-chars-backward " \t")
778 (eq (char-before) ?#))
779 (progn (setq saved (1- (point)))
780 (beginning-of-line)
781 (not (eq (char-before (1- (point))) ?\\)))
782 (looking-at c-opt-cpp-start)
783 (progn (skip-chars-forward " \t")
784 (eq (point) saved))))
785 (goto-char saved)
786 (if (and (c-forward-to-cpp-define-body)
787 (progn (c-forward-syntactic-ws start)
788 (< (point) start)))
789 ;; Stop at the first token in the content of the macro.
790 (setq pos (point)
791 ignore-labels t) ; Avoid the label check on exit.
792 (setq pos saved
793 ret 'macro
794 ignore-labels t))
795 (throw 'loop nil))
796
797 ;; Do a round through the automaton if we've just passed a
798 ;; statement boundary or passed a "while"-like token.
799 ((or sym
800 (and (looking-at cond-key)
801 (setq sym (intern (match-string 1)))))
802
803 (when (and (< pos start) (null stack))
804 (throw 'loop nil))
805
806 ;; The PDA state handling.
807 ;;
808 ;; Refer to the description of the PDA in the opening
809 ;; comments. In the following OR form, the first leaf
810 ;; attempts to handles one of the specific actions detailed
811 ;; (e.g., finding token "if" whilst in state `else-boundary').
812 ;; We drop through to the second leaf (which handles common
813 ;; state) if no specific handler is found in the first cond.
814 ;; If a parsing error is detected (e.g. an "else" with no
815 ;; preceding "if"), we throw to the enclosing catch.
816 ;;
817 ;; Note that the (eq state 'else) means
818 ;; "we've just passed an else", NOT "we're looking for an
819 ;; else".
820 (or (cond
821 ((eq state 'else)
822 (if (eq sym 'boundary)
823 (setq state 'else-boundary)
824 (c-bos-report-error)
825 (c-bos-pop-state-and-retry)))
826
827 ((eq state 'else-boundary)
828 (cond ((eq sym 'if)
829 (c-bos-pop-state (setq ret 'beginning)))
830 ((eq sym 'boundary)
831 (c-bos-report-error)
832 (c-bos-pop-state))))
833
834 ((eq state 'while)
835 (if (and (eq sym 'boundary)
836 ;; Since this can cause backtracking we do a
837 ;; little more careful analysis to avoid it:
838 ;; If there's a label in front of the while
839 ;; it can't be part of a do-while.
840 (not after-labels-pos))
841 (progn (c-bos-save-pos)
842 (setq state 'while-boundary))
843 (c-bos-pop-state-and-retry))) ; Can't be a do-while
844
845 ((eq state 'while-boundary)
846 (cond ((eq sym 'do)
847 (c-bos-pop-state (setq ret 'beginning)))
848 ((eq sym 'boundary) ; isn't a do-while
849 (c-bos-restore-pos) ; the position of the while
850 (c-bos-pop-state)))) ; no longer searching for do.
851
852 ((eq state 'catch)
853 (if (eq sym 'boundary)
854 (setq state 'catch-boundary)
855 (c-bos-report-error)
856 (c-bos-pop-state-and-retry)))
857
858 ((eq state 'catch-boundary)
859 (cond
860 ((eq sym 'try)
861 (c-bos-pop-state (setq ret 'beginning)))
862 ((eq sym 'catch)
863 (setq state 'catch))
864 ((eq sym 'boundary)
865 (c-bos-report-error)
866 (c-bos-pop-state)))))
867
868 ;; This is state common. We get here when the previous
869 ;; cond statement found no particular state handler.
870 (cond ((eq sym 'boundary)
871 ;; If we have a boundary at the start
872 ;; position we push a frame to go to the
873 ;; previous statement.
874 (if (>= pos start)
875 (c-bos-push-state)
876 (c-bos-pop-state)))
877 ((eq sym 'else)
878 (c-bos-push-state)
879 (c-bos-save-error-info 'if 'else)
880 (setq state 'else))
881 ((eq sym 'while)
882 ;; Is this a real while, or a do-while?
883 ;; The next `when' triggers unless we are SURE that
884 ;; the `while' is not the tailend of a `do-while'.
885 (when (or (not pptok)
886 (memq (char-after pptok) delims)
887 ;; The following kludge is to prevent
888 ;; infinite recursion when called from
889 ;; c-awk-after-if-for-while-condition-p,
890 ;; or the like.
891 (and (eq (point) start)
892 (c-vsemi-status-unknown-p))
893 (c-at-vsemi-p pptok))
894 ;; Since this can cause backtracking we do a
895 ;; little more careful analysis to avoid it: If
896 ;; the while isn't followed by a (possibly
897 ;; virtual) semicolon it can't be a do-while.
898 (c-bos-push-state)
899 (setq state 'while)))
900 ((memq sym '(catch finally))
901 (c-bos-push-state)
902 (c-bos-save-error-info 'try sym)
903 (setq state 'catch))))
904
905 (when c-maybe-labelp
906 ;; We're either past a statement boundary or at the
907 ;; start of a statement, so throw away any label data
908 ;; for the previous one.
909 (setq after-labels-pos nil
910 last-label-pos nil
911 c-maybe-labelp nil))))
912
913 ;; Step to the previous sexp, but not if we crossed a
914 ;; boundary, since that doesn't consume an sexp.
915 (if (eq sym 'boundary)
916 (setq ret 'previous)
917
918 ;; HERE IS THE SINGLE PLACE INSIDE THE PDA LOOP WHERE WE MOVE
919 ;; BACKWARDS THROUGH THE SOURCE.
920
921 ;; This is typically fast with the caching done by
922 ;; c-(backward|forward)-sws.
923 (c-backward-syntactic-ws)
924
925 (let ((before-sws-pos (point))
926 ;; Set as long as we have to continue jumping by sexps.
927 ;; It's the position to use as end in the next round.
928 sexp-loop-continue-pos
929 ;; The end position of the area to search for statement
930 ;; barriers in this round.
931 (sexp-loop-end-pos pos))
932
933 ;; The following while goes back one sexp per iteration.
934 (while
935 (progn
936 (unless (c-safe (c-backward-sexp) t)
937 ;; Give up if we hit an unbalanced block. Since the
938 ;; stack won't be empty the code below will report a
939 ;; suitable error.
940 (throw 'loop nil))
941
942 ;; Check if the sexp movement crossed a statement or
943 ;; declaration boundary. But first modify the point
944 ;; so that `c-crosses-statement-barrier-p' only looks
945 ;; at the non-sexp chars following the sexp.
946 (save-excursion
947 (when (setq
948 boundary-pos
949 (cond
950 ((if macro-start
951 nil
952 (save-excursion
953 (when (c-beginning-of-macro)
954 ;; Set continuation position in case
955 ;; `c-crosses-statement-barrier-p'
956 ;; doesn't detect anything below.
957 (setq sexp-loop-continue-pos (point)))))
958 ;; If the sexp movement took us into a
959 ;; macro then there were only some non-sexp
960 ;; chars after it. Skip out of the macro
961 ;; to analyze them but not the non-sexp
962 ;; chars that might be inside the macro.
963 (c-end-of-macro)
964 (c-crosses-statement-barrier-p
965 (point) sexp-loop-end-pos))
966
967 ((and
968 (eq (char-after) ?{)
969 (not (c-looking-at-inexpr-block lim nil t)))
970 ;; Passed a block sexp. That's a boundary
971 ;; alright.
972 (point))
973
974 ((looking-at "\\s\(")
975 ;; Passed some other paren. Only analyze
976 ;; the non-sexp chars after it.
977 (goto-char (1+ (c-down-list-backward
978 before-sws-pos)))
979 ;; We're at a valid token start position
980 ;; (outside the `save-excursion') if
981 ;; `c-crosses-statement-barrier-p' failed.
982 (c-crosses-statement-barrier-p
983 (point) sexp-loop-end-pos))
984
985 (t
986 ;; Passed a symbol sexp or line
987 ;; continuation. It doesn't matter that
988 ;; it's included in the analyzed region.
989 (if (c-crosses-statement-barrier-p
990 (point) sexp-loop-end-pos)
991 t
992 ;; If it was a line continuation then we
993 ;; have to continue looping.
994 (if (looking-at "\\\\$")
995 (setq sexp-loop-continue-pos (point)))
996 nil))))
997
998 (setq pptok ptok
999 ptok tok
1000 tok boundary-pos
1001 sym 'boundary)
1002 ;; Like a C "continue". Analyze the next sexp.
1003 (throw 'loop t)))
1004
1005 sexp-loop-continue-pos) ; End of "go back a sexp" loop condition.
1006 (goto-char sexp-loop-continue-pos)
1007 (setq sexp-loop-end-pos sexp-loop-continue-pos
1008 sexp-loop-continue-pos nil))))
1009
1010 ;; ObjC method def?
1011 (when (and c-opt-method-key
1012 (setq saved (c-in-method-def-p)))
1013 (setq pos saved
1014 ignore-labels t) ; Avoid the label check on exit.
1015 (throw 'loop nil))
1016
1017 ;; Handle labels.
1018 (unless (eq ignore-labels t)
1019 (when (numberp c-maybe-labelp)
1020 ;; `c-crosses-statement-barrier-p' has found a colon, so we
1021 ;; might be in a label now. Have we got a real label
1022 ;; (including a case label) or something like C++'s "public:"?
1023 ;; A case label might use an expression rather than a token.
1024 (setq after-case:-pos (or tok start))
1025 (if (looking-at c-nonlabel-token-key) ; e.g. "while" or "'a'"
1026 (setq c-maybe-labelp nil)
1027 (if after-labels-pos ; Have we already encountered a label?
1028 (if (not last-label-pos)
1029 (setq last-label-pos (or tok start)))
1030 (setq after-labels-pos (or tok start)))
1031 (setq c-maybe-labelp t
1032 label-good-pos nil))) ; bogus "label"
1033
1034 (when (and (not label-good-pos) ; i.e. no invalid "label"'s yet
1035 ; been found.
1036 (looking-at c-nonlabel-token-key)) ; e.g. "while :"
1037 ;; We're in a potential label and it's the first
1038 ;; time we've found something that isn't allowed in
1039 ;; one.
1040 (setq label-good-pos (or tok start))))
1041
1042 ;; We've moved back by a sexp, so update the token positions.
1043 (setq sym nil
1044 pptok ptok
1045 ptok tok
1046 tok (point)
1047 pos tok))) ; Not nil (for the while loop).
1048
1049 ;; If the stack isn't empty there might be errors to report.
1050 (while stack
1051 (if (and (vectorp saved-pos) (eq (length saved-pos) 3))
1052 (c-bos-report-error))
1053 (setq saved-pos (cdr (car stack))
1054 stack (cdr stack)))
1055
1056 (when (and (eq ret 'same)
1057 (not (memq sym '(boundary ignore nil))))
1058 ;; Need to investigate closer whether we've crossed
1059 ;; between a substatement and its containing statement.
1060 (if (setq saved (if (looking-at c-block-stmt-1-key)
1061 ptok
1062 pptok))
1063 (cond ((> start saved) (setq pos saved))
1064 ((= start saved) (setq ret 'up)))))
1065
1066 (when (and (not ignore-labels)
1067 (eq c-maybe-labelp t)
1068 (not (eq ret 'beginning))
1069 after-labels-pos
1070 (or (not label-good-pos)
1071 (<= label-good-pos pos)
1072 (progn
1073 (goto-char (if (and last-label-pos
1074 (< last-label-pos start))
1075 last-label-pos
1076 pos))
1077 (looking-at c-label-kwds-regexp))))
1078 ;; We're in a label. Maybe we should step to the statement
1079 ;; after it.
1080 (if (< after-labels-pos start)
1081 (setq pos after-labels-pos)
1082 (setq ret 'label)
1083 (if (and last-label-pos (< last-label-pos start))
1084 ;; Might have jumped over several labels. Go to the last one.
1085 (setq pos last-label-pos)))))
1086
1087 ;; Have we got "case <expression>:"?
1088 (goto-char pos)
1089 (when (and after-case:-pos
1090 (not (eq ret 'beginning))
1091 (looking-at c-case-kwds-regexp))
1092 (if (< after-case:-pos start)
1093 (setq pos after-case:-pos))
1094 (if (eq ret 'same)
1095 (setq ret 'label)))
1096
1097 ;; Skip over the unary operators that can start the statement.
1098 (while (progn
1099 (c-backward-syntactic-ws)
1100 ;; protect AWK post-inc/decrement operators, etc.
1101 (and (not (c-at-vsemi-p (point)))
1102 (/= (skip-chars-backward "-+!*&~@`#") 0)))
1103 (setq pos (point)))
1104 (goto-char pos)
1105 ret)))
1106
1107 (defun c-crosses-statement-barrier-p (from to)
1108 "Return non-nil if buffer positions FROM to TO cross one or more
1109 statement or declaration boundaries. The returned value is actually
1110 the position of the earliest boundary char. FROM must not be within
1111 a string or comment.
1112
1113 The variable `c-maybe-labelp' is set to the position of the first `:' that
1114 might start a label (i.e. not part of `::' and not preceded by `?'). If a
1115 single `?' is found, then `c-maybe-labelp' is cleared.
1116
1117 For AWK, a statement which is terminated by an EOL (not a \; or a }) is
1118 regarded as having a \"virtual semicolon\" immediately after the last token on
1119 the line. If this virtual semicolon is _at_ from, the function recognizes it.
1120
1121 Note that this function might do hidden buffer changes. See the
1122 comment at the start of cc-engine.el for more info."
1123 (let ((skip-chars c-stmt-delim-chars)
1124 lit-range)
1125 (save-excursion
1126 (catch 'done
1127 (goto-char from)
1128 (while (progn (skip-chars-forward skip-chars to)
1129 (< (point) to))
1130 (cond
1131 ((setq lit-range (c-literal-limits from)) ; Have we landed in a string/comment?
1132 (goto-char (cdr lit-range)))
1133 ((eq (char-after) ?:)
1134 (forward-char)
1135 (if (and (eq (char-after) ?:)
1136 (< (point) to))
1137 ;; Ignore scope operators.
1138 (forward-char)
1139 (setq c-maybe-labelp (1- (point)))))
1140 ((eq (char-after) ??)
1141 ;; A question mark. Can't be a label, so stop
1142 ;; looking for more : and ?.
1143 (setq c-maybe-labelp nil
1144 skip-chars (substring c-stmt-delim-chars 0 -2)))
1145 ((memq (char-after) '(?# ?\n ?\r)) ; A virtual semicolon?
1146 (if (and (eq (char-before) ?\\) (memq (char-after) '(?\n ?\r)))
1147 (backward-char))
1148 (skip-chars-backward " \t" from)
1149 (if (c-at-vsemi-p)
1150 (throw 'done (point))
1151 (forward-line)))
1152 (t (throw 'done (point)))))
1153 ;; In trailing space after an as yet undetected virtual semicolon?
1154 (c-backward-syntactic-ws from)
1155 (if (and (< (point) to)
1156 (c-at-vsemi-p))
1157 (point)
1158 nil)))))
1159
1160 (defun c-at-statement-start-p ()
1161 "Return non-nil if the point is at the first token in a statement
1162 or somewhere in the syntactic whitespace before it.
1163
1164 A \"statement\" here is not restricted to those inside code blocks.
1165 Any kind of declaration-like construct that occur outside function
1166 bodies is also considered a \"statement\".
1167
1168 Note that this function might do hidden buffer changes. See the
1169 comment at the start of cc-engine.el for more info."
1170
1171 (save-excursion
1172 (let ((end (point))
1173 c-maybe-labelp)
1174 (c-syntactic-skip-backward (substring c-stmt-delim-chars 1) nil t)
1175 (or (bobp)
1176 (eq (char-before) ?})
1177 (and (eq (char-before) ?{)
1178 (not (and c-special-brace-lists
1179 (progn (backward-char)
1180 (c-looking-at-special-brace-list)))))
1181 (c-crosses-statement-barrier-p (point) end)))))
1182
1183 (defun c-at-expression-start-p ()
1184 "Return non-nil if the point is at the first token in an expression or
1185 statement, or somewhere in the syntactic whitespace before it.
1186
1187 An \"expression\" here is a bit different from the normal language
1188 grammar sense: It's any sequence of expression tokens except commas,
1189 unless they are enclosed inside parentheses of some kind. Also, an
1190 expression never continues past an enclosing parenthesis, but it might
1191 contain parenthesis pairs of any sort except braces.
1192
1193 Since expressions never cross statement boundaries, this function also
1194 recognizes statement beginnings, just like `c-at-statement-start-p'.
1195
1196 Note that this function might do hidden buffer changes. See the
1197 comment at the start of cc-engine.el for more info."
1198
1199 (save-excursion
1200 (let ((end (point))
1201 (c-stmt-delim-chars c-stmt-delim-chars-with-comma)
1202 c-maybe-labelp)
1203 (c-syntactic-skip-backward (substring c-stmt-delim-chars 1) nil t)
1204 (or (bobp)
1205 (memq (char-before) '(?{ ?}))
1206 (save-excursion (backward-char)
1207 (looking-at "\\s("))
1208 (c-crosses-statement-barrier-p (point) end)))))
1209
1210 \f
1211 ;; A set of functions that covers various idiosyncrasies in
1212 ;; implementations of `forward-comment'.
1213
1214 ;; Note: Some emacsen considers incorrectly that any line comment
1215 ;; ending with a backslash continues to the next line. I can't think
1216 ;; of any way to work around that in a reliable way without changing
1217 ;; the buffer, though. Suggestions welcome. ;) (No, temporarily
1218 ;; changing the syntax for backslash doesn't work since we must treat
1219 ;; escapes in string literals correctly.)
1220
1221 (defun c-forward-single-comment ()
1222 "Move forward past whitespace and the closest following comment, if any.
1223 Return t if a comment was found, nil otherwise. In either case, the
1224 point is moved past the following whitespace. Line continuations,
1225 i.e. a backslashes followed by line breaks, are treated as whitespace.
1226 The line breaks that end line comments are considered to be the
1227 comment enders, so the point will be put on the beginning of the next
1228 line if it moved past a line comment.
1229
1230 This function does not do any hidden buffer changes."
1231
1232 (let ((start (point)))
1233 (when (looking-at "\\([ \t\n\r\f\v]\\|\\\\[\n\r]\\)+")
1234 (goto-char (match-end 0)))
1235
1236 (when (forward-comment 1)
1237 (if (eobp)
1238 ;; Some emacsen (e.g. XEmacs 21) return t when moving
1239 ;; forwards at eob.
1240 nil
1241
1242 ;; Emacs includes the ending newline in a b-style (c++)
1243 ;; comment, but XEmacs doesn't. We depend on the Emacs
1244 ;; behavior (which also is symmetric).
1245 (if (and (eolp) (elt (parse-partial-sexp start (point)) 7))
1246 (condition-case nil (forward-char 1)))
1247
1248 t))))
1249
1250 (defsubst c-forward-comments ()
1251 "Move forward past all following whitespace and comments.
1252 Line continuations, i.e. a backslashes followed by line breaks, are
1253 treated as whitespace.
1254
1255 Note that this function might do hidden buffer changes. See the
1256 comment at the start of cc-engine.el for more info."
1257
1258 (while (or
1259 ;; If forward-comment in at least XEmacs 21 is given a large
1260 ;; positive value, it'll loop all the way through if it hits
1261 ;; eob.
1262 (and (forward-comment 5)
1263 ;; Some emacsen (e.g. XEmacs 21) return t when moving
1264 ;; forwards at eob.
1265 (not (eobp)))
1266
1267 (when (looking-at "\\\\[\n\r]")
1268 (forward-char 2)
1269 t))))
1270
1271 (defun c-backward-single-comment ()
1272 "Move backward past whitespace and the closest preceding comment, if any.
1273 Return t if a comment was found, nil otherwise. In either case, the
1274 point is moved past the preceding whitespace. Line continuations,
1275 i.e. a backslashes followed by line breaks, are treated as whitespace.
1276 The line breaks that end line comments are considered to be the
1277 comment enders, so the point cannot be at the end of the same line to
1278 move over a line comment.
1279
1280 This function does not do any hidden buffer changes."
1281
1282 (let ((start (point)))
1283 ;; When we got newline terminated comments, forward-comment in all
1284 ;; supported emacsen so far will stop at eol of each line not
1285 ;; ending with a comment when moving backwards. This corrects for
1286 ;; that, and at the same time handles line continuations.
1287 (while (progn
1288 (skip-chars-backward " \t\n\r\f\v")
1289 (and (looking-at "[\n\r]")
1290 (eq (char-before) ?\\)))
1291 (backward-char))
1292
1293 (if (bobp)
1294 ;; Some emacsen (e.g. Emacs 19.34) return t when moving
1295 ;; backwards at bob.
1296 nil
1297
1298 ;; Leave point after the closest following newline if we've
1299 ;; backed up over any above, since forward-comment won't move
1300 ;; backward over a line comment if point is at the end of the
1301 ;; same line.
1302 (re-search-forward "\\=\\s *[\n\r]" start t)
1303
1304 (if (if (forward-comment -1)
1305 (if (eolp)
1306 ;; If forward-comment above succeeded and we're at eol
1307 ;; then the newline we moved over above didn't end a
1308 ;; line comment, so we give it another go.
1309 (forward-comment -1)
1310 t))
1311
1312 ;; Emacs <= 20 and XEmacs move back over the closer of a
1313 ;; block comment that lacks an opener.
1314 (if (looking-at "\\*/")
1315 (progn (forward-char 2) nil)
1316 t)))))
1317
1318 (defsubst c-backward-comments ()
1319 "Move backward past all preceding whitespace and comments.
1320 Line continuations, i.e. a backslashes followed by line breaks, are
1321 treated as whitespace. The line breaks that end line comments are
1322 considered to be the comment enders, so the point cannot be at the end
1323 of the same line to move over a line comment. Unlike
1324 c-backward-syntactic-ws, this function doesn't move back over
1325 preprocessor directives.
1326
1327 Note that this function might do hidden buffer changes. See the
1328 comment at the start of cc-engine.el for more info."
1329
1330 (let ((start (point)))
1331 (while (and
1332 ;; `forward-comment' in some emacsen (e.g. XEmacs 21.4)
1333 ;; return t when moving backwards at bob.
1334 (not (bobp))
1335
1336 (if (forward-comment -1)
1337 (if (looking-at "\\*/")
1338 ;; Emacs <= 20 and XEmacs move back over the
1339 ;; closer of a block comment that lacks an opener.
1340 (progn (forward-char 2) nil)
1341 t)
1342
1343 ;; XEmacs treats line continuations as whitespace but
1344 ;; only in the backward direction, which seems a bit
1345 ;; odd. Anyway, this is necessary for Emacs.
1346 (when (and (looking-at "[\n\r]")
1347 (eq (char-before) ?\\)
1348 (< (point) start))
1349 (backward-char)
1350 t))))))
1351
1352 \f
1353 ;; Tools for skipping over syntactic whitespace.
1354
1355 ;; The following functions use text properties to cache searches over
1356 ;; large regions of syntactic whitespace. It works as follows:
1357 ;;
1358 ;; o If a syntactic whitespace region contains anything but simple
1359 ;; whitespace (i.e. space, tab and line breaks), the text property
1360 ;; `c-in-sws' is put over it. At places where we have stopped
1361 ;; within that region there's also a `c-is-sws' text property.
1362 ;; That since there typically are nested whitespace inside that
1363 ;; must be handled separately, e.g. whitespace inside a comment or
1364 ;; cpp directive. Thus, from one point with `c-is-sws' it's safe
1365 ;; to jump to another point with that property within the same
1366 ;; `c-in-sws' region. It can be likened to a ladder where
1367 ;; `c-in-sws' marks the bars and `c-is-sws' the rungs.
1368 ;;
1369 ;; o The `c-is-sws' property is put on the simple whitespace chars at
1370 ;; a "rung position" and also maybe on the first following char.
1371 ;; As many characters as can be conveniently found in this range
1372 ;; are marked, but no assumption can be made that the whole range
1373 ;; is marked (it could be clobbered by later changes, for
1374 ;; instance).
1375 ;;
1376 ;; Note that some part of the beginning of a sequence of simple
1377 ;; whitespace might be part of the end of a preceding line comment
1378 ;; or cpp directive and must not be considered part of the "rung".
1379 ;; Such whitespace is some amount of horizontal whitespace followed
1380 ;; by a newline. In the case of cpp directives it could also be
1381 ;; two newlines with horizontal whitespace between them.
1382 ;;
1383 ;; The reason to include the first following char is to cope with
1384 ;; "rung positions" that doesn't have any ordinary whitespace. If
1385 ;; `c-is-sws' is put on a token character it does not have
1386 ;; `c-in-sws' set simultaneously. That's the only case when that
1387 ;; can occur, and the reason for not extending the `c-in-sws'
1388 ;; region to cover it is that the `c-in-sws' region could then be
1389 ;; accidentally merged with a following one if the token is only
1390 ;; one character long.
1391 ;;
1392 ;; o On buffer changes the `c-in-sws' and `c-is-sws' properties are
1393 ;; removed in the changed region. If the change was inside
1394 ;; syntactic whitespace that means that the "ladder" is broken, but
1395 ;; a later call to `c-forward-sws' or `c-backward-sws' will use the
1396 ;; parts on either side and use an ordinary search only to "repair"
1397 ;; the gap.
1398 ;;
1399 ;; Special care needs to be taken if a region is removed: If there
1400 ;; are `c-in-sws' on both sides of it which do not connect inside
1401 ;; the region then they can't be joined. If e.g. a marked macro is
1402 ;; broken, syntactic whitespace inside the new text might be
1403 ;; marked. If those marks would become connected with the old
1404 ;; `c-in-sws' range around the macro then we could get a ladder
1405 ;; with one end outside the macro and the other at some whitespace
1406 ;; within it.
1407 ;;
1408 ;; The main motivation for this system is to increase the speed in
1409 ;; skipping over the large whitespace regions that can occur at the
1410 ;; top level in e.g. header files that contain a lot of comments and
1411 ;; cpp directives. For small comments inside code it's probably
1412 ;; slower than using `forward-comment' straightforwardly, but speed is
1413 ;; not a significant factor there anyway.
1414
1415 ; (defface c-debug-is-sws-face
1416 ; '((t (:background "GreenYellow")))
1417 ; "Debug face to mark the `c-is-sws' property.")
1418 ; (defface c-debug-in-sws-face
1419 ; '((t (:underline t)))
1420 ; "Debug face to mark the `c-in-sws' property.")
1421
1422 ; (defun c-debug-put-sws-faces ()
1423 ; ;; Put the sws debug faces on all the `c-is-sws' and `c-in-sws'
1424 ; ;; properties in the buffer.
1425 ; (interactive)
1426 ; (save-excursion
1427 ; (c-save-buffer-state (in-face)
1428 ; (goto-char (point-min))
1429 ; (setq in-face (if (get-text-property (point) 'c-is-sws)
1430 ; (point)))
1431 ; (while (progn
1432 ; (goto-char (next-single-property-change
1433 ; (point) 'c-is-sws nil (point-max)))
1434 ; (if in-face
1435 ; (progn
1436 ; (c-debug-add-face in-face (point) 'c-debug-is-sws-face)
1437 ; (setq in-face nil))
1438 ; (setq in-face (point)))
1439 ; (not (eobp))))
1440 ; (goto-char (point-min))
1441 ; (setq in-face (if (get-text-property (point) 'c-in-sws)
1442 ; (point)))
1443 ; (while (progn
1444 ; (goto-char (next-single-property-change
1445 ; (point) 'c-in-sws nil (point-max)))
1446 ; (if in-face
1447 ; (progn
1448 ; (c-debug-add-face in-face (point) 'c-debug-in-sws-face)
1449 ; (setq in-face nil))
1450 ; (setq in-face (point)))
1451 ; (not (eobp)))))))
1452
1453 (defmacro c-debug-sws-msg (&rest args)
1454 ;;`(message ,@args)
1455 )
1456
1457 (defmacro c-put-is-sws (beg end)
1458 ;; This macro does a hidden buffer change.
1459 `(let ((beg ,beg) (end ,end))
1460 (put-text-property beg end 'c-is-sws t)
1461 ,@(when (facep 'c-debug-is-sws-face)
1462 `((c-debug-add-face beg end 'c-debug-is-sws-face)))))
1463
1464 (defmacro c-put-in-sws (beg end)
1465 ;; This macro does a hidden buffer change.
1466 `(let ((beg ,beg) (end ,end))
1467 (put-text-property beg end 'c-in-sws t)
1468 ,@(when (facep 'c-debug-is-sws-face)
1469 `((c-debug-add-face beg end 'c-debug-in-sws-face)))))
1470
1471 (defmacro c-remove-is-sws (beg end)
1472 ;; This macro does a hidden buffer change.
1473 `(let ((beg ,beg) (end ,end))
1474 (remove-text-properties beg end '(c-is-sws nil))
1475 ,@(when (facep 'c-debug-is-sws-face)
1476 `((c-debug-remove-face beg end 'c-debug-is-sws-face)))))
1477
1478 (defmacro c-remove-in-sws (beg end)
1479 ;; This macro does a hidden buffer change.
1480 `(let ((beg ,beg) (end ,end))
1481 (remove-text-properties beg end '(c-in-sws nil))
1482 ,@(when (facep 'c-debug-is-sws-face)
1483 `((c-debug-remove-face beg end 'c-debug-in-sws-face)))))
1484
1485 (defmacro c-remove-is-and-in-sws (beg end)
1486 ;; This macro does a hidden buffer change.
1487 `(let ((beg ,beg) (end ,end))
1488 (remove-text-properties beg end '(c-is-sws nil c-in-sws nil))
1489 ,@(when (facep 'c-debug-is-sws-face)
1490 `((c-debug-remove-face beg end 'c-debug-is-sws-face)
1491 (c-debug-remove-face beg end 'c-debug-in-sws-face)))))
1492
1493 (defsubst c-invalidate-sws-region-after (beg end)
1494 ;; Called from `after-change-functions'. Note that if
1495 ;; `c-forward-sws' or `c-backward-sws' are used outside
1496 ;; `c-save-buffer-state' or similar then this will remove the cache
1497 ;; properties right after they're added.
1498 ;;
1499 ;; This function does hidden buffer changes.
1500
1501 (save-excursion
1502 ;; Adjust the end to remove the properties in any following simple
1503 ;; ws up to and including the next line break, if there is any
1504 ;; after the changed region. This is necessary e.g. when a rung
1505 ;; marked empty line is converted to a line comment by inserting
1506 ;; "//" before the line break. In that case the line break would
1507 ;; keep the rung mark which could make a later `c-backward-sws'
1508 ;; move into the line comment instead of over it.
1509 (goto-char end)
1510 (skip-chars-forward " \t\f\v")
1511 (when (and (eolp) (not (eobp)))
1512 (setq end (1+ (point)))))
1513
1514 (when (and (= beg end)
1515 (get-text-property beg 'c-in-sws)
1516 (> beg (point-min))
1517 (get-text-property (1- beg) 'c-in-sws))
1518 ;; Ensure that an `c-in-sws' range gets broken. Note that it isn't
1519 ;; safe to keep a range that was continuous before the change. E.g:
1520 ;;
1521 ;; #define foo
1522 ;; \
1523 ;; bar
1524 ;;
1525 ;; There can be a "ladder" between "#" and "b". Now, if the newline
1526 ;; after "foo" is removed then "bar" will become part of the cpp
1527 ;; directive instead of a syntactically relevant token. In that
1528 ;; case there's no longer syntactic ws from "#" to "b".
1529 (setq beg (1- beg)))
1530
1531 (c-debug-sws-msg "c-invalidate-sws-region-after [%s..%s]" beg end)
1532 (c-remove-is-and-in-sws beg end))
1533
1534 (defun c-forward-sws ()
1535 ;; Used by `c-forward-syntactic-ws' to implement the unbounded search.
1536 ;;
1537 ;; This function might do hidden buffer changes.
1538
1539 (let (;; `rung-pos' is set to a position as early as possible in the
1540 ;; unmarked part of the simple ws region.
1541 (rung-pos (point)) next-rung-pos rung-end-pos last-put-in-sws-pos
1542 rung-is-marked next-rung-is-marked simple-ws-end
1543 ;; `safe-start' is set when it's safe to cache the start position.
1544 ;; It's not set if we've initially skipped over comments and line
1545 ;; continuations since we might have gone out through the end of a
1546 ;; macro then. This provision makes `c-forward-sws' not populate the
1547 ;; cache in the majority of cases, but otoh is `c-backward-sws' by far
1548 ;; more common.
1549 safe-start)
1550
1551 ;; Skip simple ws and do a quick check on the following character to see
1552 ;; if it's anything that can't start syntactic ws, so we can bail out
1553 ;; early in the majority of cases when there just are a few ws chars.
1554 (skip-chars-forward " \t\n\r\f\v")
1555 (when (looking-at c-syntactic-ws-start)
1556
1557 (setq rung-end-pos (min (1+ (point)) (point-max)))
1558 (if (setq rung-is-marked (text-property-any rung-pos rung-end-pos
1559 'c-is-sws t))
1560 ;; Find the last rung position to avoid setting properties in all
1561 ;; the cases when the marked rung is complete.
1562 ;; (`next-single-property-change' is certain to move at least one
1563 ;; step forward.)
1564 (setq rung-pos (1- (next-single-property-change
1565 rung-is-marked 'c-is-sws nil rung-end-pos)))
1566 ;; Got no marked rung here. Since the simple ws might have started
1567 ;; inside a line comment or cpp directive we must set `rung-pos' as
1568 ;; high as possible.
1569 (setq rung-pos (point)))
1570
1571 (while
1572 (progn
1573 (while
1574 (when (and rung-is-marked
1575 (get-text-property (point) 'c-in-sws))
1576
1577 ;; The following search is the main reason that `c-in-sws'
1578 ;; and `c-is-sws' aren't combined to one property.
1579 (goto-char (next-single-property-change
1580 (point) 'c-in-sws nil (point-max)))
1581 (unless (get-text-property (point) 'c-is-sws)
1582 ;; If the `c-in-sws' region extended past the last
1583 ;; `c-is-sws' char we have to go back a bit.
1584 (or (get-text-property (1- (point)) 'c-is-sws)
1585 (goto-char (previous-single-property-change
1586 (point) 'c-is-sws)))
1587 (backward-char))
1588
1589 (c-debug-sws-msg
1590 "c-forward-sws cached move %s -> %s (max %s)"
1591 rung-pos (point) (point-max))
1592
1593 (setq rung-pos (point))
1594 (and (> (skip-chars-forward " \t\n\r\f\v") 0)
1595 (not (eobp))))
1596
1597 ;; We'll loop here if there is simple ws after the last rung.
1598 ;; That means that there's been some change in it and it's
1599 ;; possible that we've stepped into another ladder, so extend
1600 ;; the previous one to join with it if there is one, and try to
1601 ;; use the cache again.
1602 (c-debug-sws-msg
1603 "c-forward-sws extending rung with [%s..%s] (max %s)"
1604 (1+ rung-pos) (1+ (point)) (point-max))
1605 (unless (get-text-property (point) 'c-is-sws)
1606 ;; Remove any `c-in-sws' property from the last char of
1607 ;; the rung before we mark it with `c-is-sws', so that we
1608 ;; won't connect with the remains of a broken "ladder".
1609 (c-remove-in-sws (point) (1+ (point))))
1610 (c-put-is-sws (1+ rung-pos)
1611 (1+ (point)))
1612 (c-put-in-sws rung-pos
1613 (setq rung-pos (point)
1614 last-put-in-sws-pos rung-pos)))
1615
1616 (setq simple-ws-end (point))
1617 (c-forward-comments)
1618
1619 (cond
1620 ((/= (point) simple-ws-end)
1621 ;; Skipped over comments. Don't cache at eob in case the buffer
1622 ;; is narrowed.
1623 (not (eobp)))
1624
1625 ((save-excursion
1626 (and c-opt-cpp-prefix
1627 (looking-at c-opt-cpp-start)
1628 (progn (skip-chars-backward " \t")
1629 (bolp))
1630 (or (bobp)
1631 (progn (backward-char)
1632 (not (eq (char-before) ?\\))))))
1633 ;; Skip a preprocessor directive.
1634 (end-of-line)
1635 (while (and (eq (char-before) ?\\)
1636 (= (forward-line 1) 0))
1637 (end-of-line))
1638 (forward-line 1)
1639 (setq safe-start t)
1640 ;; Don't cache at eob in case the buffer is narrowed.
1641 (not (eobp)))))
1642
1643 ;; We've searched over a piece of non-white syntactic ws. See if this
1644 ;; can be cached.
1645 (setq next-rung-pos (point))
1646 (skip-chars-forward " \t\n\r\f\v")
1647 (setq rung-end-pos (min (1+ (point)) (point-max)))
1648
1649 (if (or
1650 ;; Cache if we haven't skipped comments only, and if we started
1651 ;; either from a marked rung or from a completely uncached
1652 ;; position.
1653 (and safe-start
1654 (or rung-is-marked
1655 (not (get-text-property simple-ws-end 'c-in-sws))))
1656
1657 ;; See if there's a marked rung in the encountered simple ws. If
1658 ;; so then we can cache, unless `safe-start' is nil. Even then
1659 ;; we need to do this to check if the cache can be used for the
1660 ;; next step.
1661 (and (setq next-rung-is-marked
1662 (text-property-any next-rung-pos rung-end-pos
1663 'c-is-sws t))
1664 safe-start))
1665
1666 (progn
1667 (c-debug-sws-msg
1668 "c-forward-sws caching [%s..%s] - [%s..%s] (max %s)"
1669 rung-pos (1+ simple-ws-end) next-rung-pos rung-end-pos
1670 (point-max))
1671
1672 ;; Remove the properties for any nested ws that might be cached.
1673 ;; Only necessary for `c-is-sws' since `c-in-sws' will be set
1674 ;; anyway.
1675 (c-remove-is-sws (1+ simple-ws-end) next-rung-pos)
1676 (unless (and rung-is-marked (= rung-pos simple-ws-end))
1677 (c-put-is-sws rung-pos
1678 (1+ simple-ws-end))
1679 (setq rung-is-marked t))
1680 (c-put-in-sws rung-pos
1681 (setq rung-pos (point)
1682 last-put-in-sws-pos rung-pos))
1683 (unless (get-text-property (1- rung-end-pos) 'c-is-sws)
1684 ;; Remove any `c-in-sws' property from the last char of
1685 ;; the rung before we mark it with `c-is-sws', so that we
1686 ;; won't connect with the remains of a broken "ladder".
1687 (c-remove-in-sws (1- rung-end-pos) rung-end-pos))
1688 (c-put-is-sws next-rung-pos
1689 rung-end-pos))
1690
1691 (c-debug-sws-msg
1692 "c-forward-sws not caching [%s..%s] - [%s..%s] (max %s)"
1693 rung-pos (1+ simple-ws-end) next-rung-pos rung-end-pos
1694 (point-max))
1695
1696 ;; Set `rung-pos' for the next rung. It's the same thing here as
1697 ;; initially, except that the rung position is set as early as
1698 ;; possible since we can't be in the ending ws of a line comment or
1699 ;; cpp directive now.
1700 (if (setq rung-is-marked next-rung-is-marked)
1701 (setq rung-pos (1- (next-single-property-change
1702 rung-is-marked 'c-is-sws nil rung-end-pos)))
1703 (setq rung-pos next-rung-pos))
1704 (setq safe-start t)))
1705
1706 ;; Make sure that the newly marked `c-in-sws' region doesn't connect to
1707 ;; another one after the point (which might occur when editing inside a
1708 ;; comment or macro).
1709 (when (eq last-put-in-sws-pos (point))
1710 (cond ((< last-put-in-sws-pos (point-max))
1711 (c-debug-sws-msg
1712 "c-forward-sws clearing at %s for cache separation"
1713 last-put-in-sws-pos)
1714 (c-remove-in-sws last-put-in-sws-pos
1715 (1+ last-put-in-sws-pos)))
1716 (t
1717 ;; If at eob we have to clear the last character before the end
1718 ;; instead since the buffer might be narrowed and there might
1719 ;; be a `c-in-sws' after (point-max). In this case it's
1720 ;; necessary to clear both properties.
1721 (c-debug-sws-msg
1722 "c-forward-sws clearing thoroughly at %s for cache separation"
1723 (1- last-put-in-sws-pos))
1724 (c-remove-is-and-in-sws (1- last-put-in-sws-pos)
1725 last-put-in-sws-pos))))
1726 )))
1727
1728 (defun c-backward-sws ()
1729 ;; Used by `c-backward-syntactic-ws' to implement the unbounded search.
1730 ;;
1731 ;; This function might do hidden buffer changes.
1732
1733 (let (;; `rung-pos' is set to a position as late as possible in the unmarked
1734 ;; part of the simple ws region.
1735 (rung-pos (point)) next-rung-pos last-put-in-sws-pos
1736 rung-is-marked simple-ws-beg cmt-skip-pos)
1737
1738 ;; Skip simple horizontal ws and do a quick check on the preceding
1739 ;; character to see if it's anying that can't end syntactic ws, so we can
1740 ;; bail out early in the majority of cases when there just are a few ws
1741 ;; chars. Newlines are complicated in the backward direction, so we can't
1742 ;; skip over them.
1743 (skip-chars-backward " \t\f")
1744 (when (and (not (bobp))
1745 (save-excursion
1746 (backward-char)
1747 (looking-at c-syntactic-ws-end)))
1748
1749 ;; Try to find a rung position in the simple ws preceding point, so that
1750 ;; we can get a cache hit even if the last bit of the simple ws has
1751 ;; changed recently.
1752 (setq simple-ws-beg (point))
1753 (skip-chars-backward " \t\n\r\f\v")
1754 (if (setq rung-is-marked (text-property-any
1755 (point) (min (1+ rung-pos) (point-max))
1756 'c-is-sws t))
1757 ;; `rung-pos' will be the earliest marked position, which means that
1758 ;; there might be later unmarked parts in the simple ws region.
1759 ;; It's not worth the effort to fix that; the last part of the
1760 ;; simple ws is also typically edited often, so it could be wasted.
1761 (goto-char (setq rung-pos rung-is-marked))
1762 (goto-char simple-ws-beg))
1763
1764 (while
1765 (progn
1766 (while
1767 (when (and rung-is-marked
1768 (not (bobp))
1769 (get-text-property (1- (point)) 'c-in-sws))
1770
1771 ;; The following search is the main reason that `c-in-sws'
1772 ;; and `c-is-sws' aren't combined to one property.
1773 (goto-char (previous-single-property-change
1774 (point) 'c-in-sws nil (point-min)))
1775 (unless (get-text-property (point) 'c-is-sws)
1776 ;; If the `c-in-sws' region extended past the first
1777 ;; `c-is-sws' char we have to go forward a bit.
1778 (goto-char (next-single-property-change
1779 (point) 'c-is-sws)))
1780
1781 (c-debug-sws-msg
1782 "c-backward-sws cached move %s <- %s (min %s)"
1783 (point) rung-pos (point-min))
1784
1785 (setq rung-pos (point))
1786 (if (and (< (min (skip-chars-backward " \t\f\v")
1787 (progn
1788 (setq simple-ws-beg (point))
1789 (skip-chars-backward " \t\n\r\f\v")))
1790 0)
1791 (setq rung-is-marked
1792 (text-property-any (point) rung-pos
1793 'c-is-sws t)))
1794 t
1795 (goto-char simple-ws-beg)
1796 nil))
1797
1798 ;; We'll loop here if there is simple ws before the first rung.
1799 ;; That means that there's been some change in it and it's
1800 ;; possible that we've stepped into another ladder, so extend
1801 ;; the previous one to join with it if there is one, and try to
1802 ;; use the cache again.
1803 (c-debug-sws-msg
1804 "c-backward-sws extending rung with [%s..%s] (min %s)"
1805 rung-is-marked rung-pos (point-min))
1806 (unless (get-text-property (1- rung-pos) 'c-is-sws)
1807 ;; Remove any `c-in-sws' property from the last char of
1808 ;; the rung before we mark it with `c-is-sws', so that we
1809 ;; won't connect with the remains of a broken "ladder".
1810 (c-remove-in-sws (1- rung-pos) rung-pos))
1811 (c-put-is-sws rung-is-marked
1812 rung-pos)
1813 (c-put-in-sws rung-is-marked
1814 (1- rung-pos))
1815 (setq rung-pos rung-is-marked
1816 last-put-in-sws-pos rung-pos))
1817
1818 (c-backward-comments)
1819 (setq cmt-skip-pos (point))
1820
1821 (cond
1822 ((and c-opt-cpp-prefix
1823 (/= cmt-skip-pos simple-ws-beg)
1824 (c-beginning-of-macro))
1825 ;; Inside a cpp directive. See if it should be skipped over.
1826 (let ((cpp-beg (point)))
1827
1828 ;; Move back over all line continuations in the region skipped
1829 ;; over by `c-backward-comments'. If we go past it then we
1830 ;; started inside the cpp directive.
1831 (goto-char simple-ws-beg)
1832 (beginning-of-line)
1833 (while (and (> (point) cmt-skip-pos)
1834 (progn (backward-char)
1835 (eq (char-before) ?\\)))
1836 (beginning-of-line))
1837
1838 (if (< (point) cmt-skip-pos)
1839 ;; Don't move past the cpp directive if we began inside
1840 ;; it. Note that the position at the end of the last line
1841 ;; of the macro is also considered to be within it.
1842 (progn (goto-char cmt-skip-pos)
1843 nil)
1844
1845 ;; It's worthwhile to spend a little bit of effort on finding
1846 ;; the end of the macro, to get a good `simple-ws-beg'
1847 ;; position for the cache. Note that `c-backward-comments'
1848 ;; could have stepped over some comments before going into
1849 ;; the macro, and then `simple-ws-beg' must be kept on the
1850 ;; same side of those comments.
1851 (goto-char simple-ws-beg)
1852 (skip-chars-backward " \t\n\r\f\v")
1853 (if (eq (char-before) ?\\)
1854 (forward-char))
1855 (forward-line 1)
1856 (if (< (point) simple-ws-beg)
1857 ;; Might happen if comments after the macro were skipped
1858 ;; over.
1859 (setq simple-ws-beg (point)))
1860
1861 (goto-char cpp-beg)
1862 t)))
1863
1864 ((/= (save-excursion
1865 (skip-chars-forward " \t\n\r\f\v" simple-ws-beg)
1866 (setq next-rung-pos (point)))
1867 simple-ws-beg)
1868 ;; Skipped over comments. Must put point at the end of
1869 ;; the simple ws at point since we might be after a line
1870 ;; comment or cpp directive that's been partially
1871 ;; narrowed out, and we can't risk marking the simple ws
1872 ;; at the end of it.
1873 (goto-char next-rung-pos)
1874 t)))
1875
1876 ;; We've searched over a piece of non-white syntactic ws. See if this
1877 ;; can be cached.
1878 (setq next-rung-pos (point))
1879 (skip-chars-backward " \t\f\v")
1880
1881 (if (or
1882 ;; Cache if we started either from a marked rung or from a
1883 ;; completely uncached position.
1884 rung-is-marked
1885 (not (get-text-property (1- simple-ws-beg) 'c-in-sws))
1886
1887 ;; Cache if there's a marked rung in the encountered simple ws.
1888 (save-excursion
1889 (skip-chars-backward " \t\n\r\f\v")
1890 (text-property-any (point) (min (1+ next-rung-pos) (point-max))
1891 'c-is-sws t)))
1892
1893 (progn
1894 (c-debug-sws-msg
1895 "c-backward-sws caching [%s..%s] - [%s..%s] (min %s)"
1896 (point) (1+ next-rung-pos)
1897 simple-ws-beg (min (1+ rung-pos) (point-max))
1898 (point-min))
1899
1900 ;; Remove the properties for any nested ws that might be cached.
1901 ;; Only necessary for `c-is-sws' since `c-in-sws' will be set
1902 ;; anyway.
1903 (c-remove-is-sws (1+ next-rung-pos) simple-ws-beg)
1904 (unless (and rung-is-marked (= simple-ws-beg rung-pos))
1905 (let ((rung-end-pos (min (1+ rung-pos) (point-max))))
1906 (unless (get-text-property (1- rung-end-pos) 'c-is-sws)
1907 ;; Remove any `c-in-sws' property from the last char of
1908 ;; the rung before we mark it with `c-is-sws', so that we
1909 ;; won't connect with the remains of a broken "ladder".
1910 (c-remove-in-sws (1- rung-end-pos) rung-end-pos))
1911 (c-put-is-sws simple-ws-beg
1912 rung-end-pos)
1913 (setq rung-is-marked t)))
1914 (c-put-in-sws (setq simple-ws-beg (point)
1915 last-put-in-sws-pos simple-ws-beg)
1916 rung-pos)
1917 (c-put-is-sws (setq rung-pos simple-ws-beg)
1918 (1+ next-rung-pos)))
1919
1920 (c-debug-sws-msg
1921 "c-backward-sws not caching [%s..%s] - [%s..%s] (min %s)"
1922 (point) (1+ next-rung-pos)
1923 simple-ws-beg (min (1+ rung-pos) (point-max))
1924 (point-min))
1925 (setq rung-pos next-rung-pos
1926 simple-ws-beg (point))
1927 ))
1928
1929 ;; Make sure that the newly marked `c-in-sws' region doesn't connect to
1930 ;; another one before the point (which might occur when editing inside a
1931 ;; comment or macro).
1932 (when (eq last-put-in-sws-pos (point))
1933 (cond ((< (point-min) last-put-in-sws-pos)
1934 (c-debug-sws-msg
1935 "c-backward-sws clearing at %s for cache separation"
1936 (1- last-put-in-sws-pos))
1937 (c-remove-in-sws (1- last-put-in-sws-pos)
1938 last-put-in-sws-pos))
1939 ((> (point-min) 1)
1940 ;; If at bob and the buffer is narrowed, we have to clear the
1941 ;; character we're standing on instead since there might be a
1942 ;; `c-in-sws' before (point-min). In this case it's necessary
1943 ;; to clear both properties.
1944 (c-debug-sws-msg
1945 "c-backward-sws clearing thoroughly at %s for cache separation"
1946 last-put-in-sws-pos)
1947 (c-remove-is-and-in-sws last-put-in-sws-pos
1948 (1+ last-put-in-sws-pos)))))
1949 )))
1950
1951 \f
1952 ;; Other whitespace tools
1953 (defun c-partial-ws-p (beg end)
1954 ;; Is the region (beg end) WS, and is there WS (or BOB/EOB) next to the
1955 ;; region? This is a "heuristic" function. .....
1956 ;;
1957 ;; The motivation for the second bit is to check whether removing this
1958 ;; region would coalesce two symbols.
1959 ;;
1960 ;; FIXME!!! This function doesn't check virtual semicolons in any way. Be
1961 ;; careful about using this function for, e.g. AWK. (2007/3/7)
1962 (save-excursion
1963 (let ((end+1 (min (1+ end) (point-max))))
1964 (or (progn (goto-char (max (point-min) (1- beg)))
1965 (c-skip-ws-forward end)
1966 (eq (point) end))
1967 (progn (goto-char beg)
1968 (c-skip-ws-forward end+1)
1969 (eq (point) end+1))))))
1970 \f
1971 ;; A system for finding noteworthy parens before the point.
1972
1973 (defconst c-state-cache-too-far 5000)
1974 ;; A maximum comfortable scanning distance, e.g. between
1975 ;; `c-state-cache-good-pos' and "HERE" (where we call c-parse-state). When
1976 ;; this distance is exceeded, we take "emergency meausures", e.g. by clearing
1977 ;; the cache and starting again from point-min or a beginning of defun. This
1978 ;; value can be tuned for efficiency or set to a lower value for testing.
1979
1980 (defvar c-state-cache nil)
1981 (make-variable-buffer-local 'c-state-cache)
1982 ;; The state cache used by `c-parse-state' to cut down the amount of
1983 ;; searching. It's the result from some earlier `c-parse-state' call. See
1984 ;; `c-parse-state''s doc string for details of its structure.
1985 ;;
1986 ;; The use of the cached info is more effective if the next
1987 ;; `c-parse-state' call is on a line close by the one the cached state
1988 ;; was made at; the cache can actually slow down a little if the
1989 ;; cached state was made very far back in the buffer. The cache is
1990 ;; most effective if `c-parse-state' is used on each line while moving
1991 ;; forward.
1992
1993 (defvar c-state-cache-good-pos 1)
1994 (make-variable-buffer-local 'c-state-cache-good-pos)
1995 ;; This is a position where `c-state-cache' is known to be correct, or
1996 ;; nil (see below). It's a position inside one of the recorded unclosed
1997 ;; parens or the top level, but not further nested inside any literal or
1998 ;; subparen that is closed before the last recorded position.
1999 ;;
2000 ;; The exact position is chosen to try to be close to yet earlier than
2001 ;; the position where `c-state-cache' will be called next. Right now
2002 ;; the heuristic is to set it to the position after the last found
2003 ;; closing paren (of any type) before the line on which
2004 ;; `c-parse-state' was called. That is chosen primarily to work well
2005 ;; with refontification of the current line.
2006 ;;
2007 ;; 2009-07-28: When `c-state-point-min' and the last position where
2008 ;; `c-parse-state' or for which `c-invalidate-state-cache' was called, are
2009 ;; both in the same literal, there is no such "good position", and
2010 ;; c-state-cache-good-pos is then nil. This is the ONLY circumstance in which
2011 ;; it can be nil. In this case, `c-state-point-min-literal' will be non-nil.
2012 ;;
2013 ;; 2009-06-12: In a brace desert, c-state-cache-good-pos may also be in
2014 ;; the middle of the desert, as long as it is not within a brace pair
2015 ;; recorded in `c-state-cache' or a paren/bracket pair.
2016
2017
2018 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2019 ;; We maintain a simple cache of positions which aren't in a literal, so as to
2020 ;; speed up testing for non-literality.
2021 (defconst c-state-nonlit-pos-interval 10000)
2022 ;; The approximate interval between entries in `c-state-nonlit-pos-cache'.
2023
2024 (defvar c-state-nonlit-pos-cache nil)
2025 (make-variable-buffer-local 'c-state-nonlit-pos-cache)
2026 ;; A list of buffer positions which are known not to be in a literal. This is
2027 ;; ordered with higher positions at the front of the list. Only those which
2028 ;; are less than `c-state-nonlit-pos-cache-limit' are valid.
2029
2030 (defvar c-state-nonlit-pos-cache-limit 1)
2031 (make-variable-buffer-local 'c-state-nonlit-pos-cache-limit)
2032 ;; An upper limit on valid entries in `c-state-nonlit-pos-cache'. This is
2033 ;; reduced by buffer changes, and increased by invocations of
2034 ;; `c-state-literal-at'.
2035
2036 (defsubst c-state-pp-to-literal (from to)
2037 ;; Do a parse-partial-sexp from FROM to TO, returning the bounds of any
2038 ;; literal at TO as a cons, otherwise NIL.
2039 ;; FROM must not be in a literal, and the buffer should already be wide
2040 ;; enough.
2041 (save-excursion
2042 (let ((s (parse-partial-sexp from to)))
2043 (when (or (nth 3 s) (nth 4 s)) ; in a string or comment
2044 (parse-partial-sexp (point) (point-max)
2045 nil ; TARGETDEPTH
2046 nil ; STOPBEFORE
2047 s ; OLDSTATE
2048 'syntax-table) ; stop at end of literal
2049 (cons (nth 8 s) (point))))))
2050
2051 (defun c-state-literal-at (here)
2052 ;; If position HERE is inside a literal, return (START . END), the
2053 ;; boundaries of the literal (which may be outside the accessible bit of the
2054 ;; buffer). Otherwise, return nil.
2055 ;;
2056 ;; This function is almost the same as `c-literal-limits'. It differs in
2057 ;; that it is a lower level function, and that it rigourously follows the
2058 ;; syntax from BOB, whereas `c-literal-limits' uses a "local" safe position.
2059 (save-restriction
2060 (widen)
2061 (save-excursion
2062 (let ((c c-state-nonlit-pos-cache)
2063 pos npos lit)
2064 ;; Trim the cache to take account of buffer changes.
2065 (while (and c (> (car c) c-state-nonlit-pos-cache-limit))
2066 (setq c (cdr c)))
2067 (setq c-state-nonlit-pos-cache c)
2068
2069 (while (and c (> (car c) here))
2070 (setq c (cdr c)))
2071 (setq pos (or (car c) (point-min)))
2072
2073 (while (<= (setq npos (+ pos c-state-nonlit-pos-interval))
2074 here)
2075 (setq lit (c-state-pp-to-literal pos npos))
2076 (setq pos (or (cdr lit) npos)) ; end of literal containing npos.
2077 (setq c-state-nonlit-pos-cache (cons pos c-state-nonlit-pos-cache)))
2078
2079 (if (> pos c-state-nonlit-pos-cache-limit)
2080 (setq c-state-nonlit-pos-cache-limit pos))
2081 (if (< pos here)
2082 (setq lit (c-state-pp-to-literal pos here)))
2083 lit))))
2084
2085 (defsubst c-state-lit-beg (pos)
2086 ;; Return the start of the literal containing POS, or POS itself.
2087 (or (car (c-state-literal-at pos))
2088 pos))
2089
2090 (defsubst c-state-cache-non-literal-place (pos state)
2091 ;; Return a position outside of a string/comment at or before POS.
2092 ;; STATE is the parse-partial-sexp state at POS.
2093 (if (or (nth 3 state) ; in a string?
2094 (nth 4 state)) ; in a comment?
2095 (nth 8 state)
2096 pos))
2097
2098
2099 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2100 ;; Stuff to do with point-min, and coping with any literal there.
2101 (defvar c-state-point-min 1)
2102 (make-variable-buffer-local 'c-state-point-min)
2103 ;; This is (point-min) when `c-state-cache' was last calculated. A change of
2104 ;; narrowing is likely to affect the parens that are visible before the point.
2105
2106 (defvar c-state-point-min-lit-type nil)
2107 (make-variable-buffer-local 'c-state-point-min-lit-type)
2108 (defvar c-state-point-min-lit-start nil)
2109 (make-variable-buffer-local 'c-state-point-min-lit-start)
2110 ;; These two variables define the literal, if any, containing point-min.
2111 ;; Their values are, respectively, 'string, c, or c++, and the start of the
2112 ;; literal. If there's no literal there, they're both nil.
2113
2114 (defvar c-state-min-scan-pos 1)
2115 (make-variable-buffer-local 'c-state-min-scan-pos)
2116 ;; This is the earliest buffer-pos from which scanning can be done. It is
2117 ;; either the end of the literal containing point-min, or point-min itself.
2118 ;; It becomes nil if the buffer is changed earlier than this point.
2119 (defun c-state-get-min-scan-pos ()
2120 ;; Return the lowest valid scanning pos. This will be the end of the
2121 ;; literal enclosing point-min, or point-min itself.
2122 (or c-state-min-scan-pos
2123 (save-restriction
2124 (save-excursion
2125 (widen)
2126 (goto-char c-state-point-min-lit-start)
2127 (if (eq c-state-point-min-lit-type 'string)
2128 (forward-sexp)
2129 (forward-comment 1))
2130 (setq c-state-min-scan-pos (point))))))
2131
2132 (defun c-state-mark-point-min-literal ()
2133 ;; Determine the properties of any literal containing POINT-MIN, setting the
2134 ;; variables `c-state-point-min-lit-type', `c-state-point-min-lit-start',
2135 ;; and `c-state-min-scan-pos' accordingly. The return value is meaningless.
2136 (let ((p-min (point-min))
2137 lit)
2138 (save-restriction
2139 (widen)
2140 (setq lit (c-state-literal-at p-min))
2141 (if lit
2142 (setq c-state-point-min-lit-type
2143 (save-excursion
2144 (goto-char (car lit))
2145 (cond
2146 ((looking-at c-block-comment-start-regexp) 'c)
2147 ((looking-at c-line-comment-starter) 'c++)
2148 (t 'string)))
2149 c-state-point-min-lit-start (car lit)
2150 c-state-min-scan-pos (cdr lit))
2151 (setq c-state-point-min-lit-type nil
2152 c-state-point-min-lit-start nil
2153 c-state-min-scan-pos p-min)))))
2154
2155
2156 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2157 ;; A variable which signals a brace dessert - helpful for reducing the number
2158 ;; of fruitless backward scans.
2159 (defvar c-state-brace-pair-desert nil)
2160 (make-variable-buffer-local 'c-state-brace-pair-desert)
2161 ;; Used only in `c-append-lower-brace-pair-to-state-cache'. It is set when an
2162 ;; that defun has searched backwards for a brace pair and not found one. Its
2163 ;; value is either nil or a cons (PA . FROM), where PA is the position of the
2164 ;; enclosing opening paren/brace/bracket which bounds the backwards search (or
2165 ;; nil when at top level) and FROM is where the backward search started. It
2166 ;; is reset to nil in `c-invalidate-state-cache'.
2167
2168
2169 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2170 ;; Lowish level functions/macros which work directly on `c-state-cache', or a
2171 ;; list of like structure.
2172 (defmacro c-state-cache-top-lparen (&optional cache)
2173 ;; Return the address of the top left brace/bracket/paren recorded in CACHE
2174 ;; (default `c-state-cache') (or nil).
2175 (let ((cash (or cache 'c-state-cache)))
2176 `(if (consp (car ,cash))
2177 (caar ,cash)
2178 (car ,cash))))
2179
2180 (defmacro c-state-cache-top-paren (&optional cache)
2181 ;; Return the address of the latest brace/bracket/paren (whether left or
2182 ;; right) recorded in CACHE (default `c-state-cache') or nil.
2183 (let ((cash (or cache 'c-state-cache)))
2184 `(if (consp (car ,cash))
2185 (cdar ,cash)
2186 (car ,cash))))
2187
2188 (defmacro c-state-cache-after-top-paren (&optional cache)
2189 ;; Return the position just after the latest brace/bracket/paren (whether
2190 ;; left or right) recorded in CACHE (default `c-state-cache') or nil.
2191 (let ((cash (or cache 'c-state-cache)))
2192 `(if (consp (car ,cash))
2193 (cdar ,cash)
2194 (and (car ,cash)
2195 (1+ (car ,cash))))))
2196
2197 (defun c-get-cache-scan-pos (here)
2198 ;; From the state-cache, determine the buffer position from which we might
2199 ;; scan forward to HERE to update this cache. This position will be just
2200 ;; after a paren/brace/bracket recorded in the cache, if possible, otherwise
2201 ;; return the earliest position in the accessible region which isn't within
2202 ;; a literal. If the visible portion of the buffer is entirely within a
2203 ;; literal, return NIL.
2204 (let ((c c-state-cache) elt)
2205 ;(while (>= (or (c-state-cache-top-lparen c) 1) here)
2206 (while (and c
2207 (>= (c-state-cache-top-lparen c) here))
2208 (setq c (cdr c)))
2209
2210 (setq elt (car c))
2211 (cond
2212 ((consp elt)
2213 (if (> (cdr elt) here)
2214 (1+ (car elt))
2215 (cdr elt)))
2216 (elt (1+ elt))
2217 ((<= (c-state-get-min-scan-pos) here)
2218 (c-state-get-min-scan-pos))
2219 (t nil))))
2220
2221 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2222 ;; Variables which keep track of preprocessor constructs.
2223 (defvar c-state-old-cpp-beg nil)
2224 (make-variable-buffer-local 'c-state-old-cpp-beg)
2225 (defvar c-state-old-cpp-end nil)
2226 (make-variable-buffer-local 'c-state-old-cpp-end)
2227 ;; These are the limits of the macro containing point at the previous call of
2228 ;; `c-parse-state', or nil.
2229
2230 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2231 ;; Defuns which analyse the buffer, yet don't change `c-state-cache'.
2232 (defun c-get-fallback-scan-pos (here)
2233 ;; Return a start position for building `c-state-cache' from
2234 ;; scratch. This will be at the top level, 2 defuns back.
2235 (save-excursion
2236 ;; Go back 2 bods, but ignore any bogus positions returned by
2237 ;; beginning-of-defun (i.e. open paren in column zero).
2238 (goto-char here)
2239 (let ((cnt 2))
2240 (while (not (or (bobp) (zerop cnt)))
2241 (c-beginning-of-defun-1) ; Pure elisp BOD.
2242 (if (eq (char-after) ?\{)
2243 (setq cnt (1- cnt)))))
2244 (point)))
2245
2246 (defun c-state-balance-parens-backwards (here- here+ top)
2247 ;; Return the position of the opening paren/brace/bracket before HERE- which
2248 ;; matches the outermost close p/b/b between HERE+ and TOP. Except when
2249 ;; there's a macro, HERE- and HERE+ are the same. Like this:
2250 ;;
2251 ;; ............................................
2252 ;; | |
2253 ;; ( [ ( .........#macro.. ) ( ) ] )
2254 ;; ^ ^ ^ ^
2255 ;; | | | |
2256 ;; return HERE- HERE+ TOP
2257 ;;
2258 ;; If there aren't enough opening paren/brace/brackets, return the position
2259 ;; of the outermost one found, or HERE- if there are none. If there are no
2260 ;; closeing p/b/bs between HERE+ and TOP, return HERE-. HERE-/+ and TOP
2261 ;; must not be inside literals. Only the accessible portion of the buffer
2262 ;; will be scanned.
2263
2264 ;; PART 1: scan from `here+' up to `top', accumulating ")"s which enclose
2265 ;; `here'. Go round the next loop each time we pass over such a ")". These
2266 ;; probably match "("s before `here-'.
2267 (let (pos pa ren+1 lonely-rens)
2268 (save-excursion
2269 (save-restriction
2270 (narrow-to-region (point-min) top) ; This can move point, sometimes.
2271 (setq pos here+)
2272 (c-safe
2273 (while
2274 (setq ren+1 (scan-lists pos 1 1)) ; might signal
2275 (setq lonely-rens (cons ren+1 lonely-rens)
2276 pos ren+1)))))
2277
2278 ;; PART 2: Scan back before `here-' searching for the "("s
2279 ;; matching/mismatching the ")"s found above. We only need to direct the
2280 ;; caller to scan when we've encountered unmatched right parens.
2281 (setq pos here-)
2282 (when lonely-rens
2283 (c-safe
2284 (while
2285 (and lonely-rens ; actual values aren't used.
2286 (setq pa (scan-lists pos -1 1)))
2287 (setq pos pa)
2288 (setq lonely-rens (cdr lonely-rens)))))
2289 pos))
2290
2291 (defun c-parse-state-get-strategy (here good-pos)
2292 ;; Determine the scanning strategy for adjusting `c-parse-state', attempting
2293 ;; to minimise the amount of scanning. HERE is the pertinent position in
2294 ;; the buffer, GOOD-POS is a position where `c-state-cache' (possibly with
2295 ;; its head trimmed) is known to be good, or nil if there is no such
2296 ;; position.
2297 ;;
2298 ;; The return value is a list, one of the following:
2299 ;;
2300 ;; o - ('forward CACHE-POS START-POINT) - scan forward from START-POINT,
2301 ;; which is not less than CACHE-POS.
2302 ;; o - ('backward CACHE-POS nil) - scan backwards (from HERE).
2303 ;; o - ('BOD nil START-POINT) - scan forwards from START-POINT, which is at the
2304 ;; top level.
2305 ;; o - ('IN-LIT nil nil) - point is inside the literal containing point-min.
2306 ;; , where CACHE-POS is the highest position recorded in `c-state-cache' at
2307 ;; or below HERE.
2308 (let ((cache-pos (c-get-cache-scan-pos here)) ; highest position below HERE in cache (or 1)
2309 BOD-pos ; position of 2nd BOD before HERE.
2310 strategy ; 'forward, 'backward, 'BOD, or 'IN-LIT.
2311 start-point
2312 how-far) ; putative scanning distance.
2313 (setq good-pos (or good-pos (c-state-get-min-scan-pos)))
2314 (cond
2315 ((< here (c-state-get-min-scan-pos))
2316 (setq strategy 'IN-LIT
2317 start-point nil
2318 cache-pos nil
2319 how-far 0))
2320 ((<= good-pos here)
2321 (setq strategy 'forward
2322 start-point (max good-pos cache-pos)
2323 how-far (- here start-point)))
2324 ((< (- good-pos here) (- here cache-pos)) ; FIXME!!! ; apply some sort of weighting.
2325 (setq strategy 'backward
2326 how-far (- good-pos here)))
2327 (t
2328 (setq strategy 'forward
2329 how-far (- here cache-pos)
2330 start-point cache-pos)))
2331
2332 ;; Might we be better off starting from the top level, two defuns back,
2333 ;; instead?
2334 (when (> how-far c-state-cache-too-far)
2335 (setq BOD-pos (c-get-fallback-scan-pos here)) ; somewhat EXPENSIVE!!!
2336 (if (< (- here BOD-pos) how-far)
2337 (setq strategy 'BOD
2338 start-point BOD-pos)))
2339
2340 (list
2341 strategy
2342 (and (memq strategy '(forward backward)) cache-pos)
2343 (and (memq strategy '(forward BOD)) start-point))))
2344
2345
2346 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2347 ;; Routines which change `c-state-cache' and associated values.
2348 (defun c-renarrow-state-cache ()
2349 ;; The region (more precisely, point-min) has changed since we
2350 ;; calculated `c-state-cache'. Amend `c-state-cache' accordingly.
2351 (if (< (point-min) c-state-point-min)
2352 ;; If point-min has MOVED BACKWARDS then we drop the state completely.
2353 ;; It would be possible to do a better job here and recalculate the top
2354 ;; only.
2355 (progn
2356 (c-state-mark-point-min-literal)
2357 (setq c-state-cache nil
2358 c-state-cache-good-pos c-state-min-scan-pos
2359 c-state-brace-pair-desert nil))
2360
2361 ;; point-min has MOVED FORWARD.
2362
2363 ;; Is the new point-min inside a (different) literal?
2364 (unless (and c-state-point-min-lit-start ; at prev. point-min
2365 (< (point-min) (c-state-get-min-scan-pos)))
2366 (c-state-mark-point-min-literal))
2367
2368 ;; Cut off a bit of the tail from `c-state-cache'.
2369 (let ((ptr (cons nil c-state-cache))
2370 pa)
2371 (while (and (setq pa (c-state-cache-top-lparen (cdr ptr)))
2372 (>= pa (point-min)))
2373 (setq ptr (cdr ptr)))
2374
2375 (when (consp ptr)
2376 (if (eq (cdr ptr) c-state-cache)
2377 (setq c-state-cache nil
2378 c-state-cache-good-pos c-state-min-scan-pos)
2379 (setcdr ptr nil)
2380 (setq c-state-cache-good-pos (1+ (c-state-cache-top-lparen))))
2381 )))
2382
2383 (setq c-state-point-min (point-min)))
2384
2385 (defun c-append-lower-brace-pair-to-state-cache (from &optional upper-lim)
2386 ;; If there is a brace pair preceding FROM in the buffer (not necessarily
2387 ;; immediately preceding), push a cons onto `c-state-cache' to represent it.
2388 ;; FROM must not be inside a literal. If UPPER-LIM is non-nil, we append
2389 ;; the highest brace pair whose "}" is below UPPER-LIM.
2390 ;;
2391 ;; Return non-nil when this has been done.
2392 ;;
2393 ;; This routine should be fast. Since it can get called a LOT, we maintain
2394 ;; `c-state-brace-pair-desert', a small cache of "failures", such that we
2395 ;; reduce the time wasted in repeated fruitless searches in brace deserts.
2396 (save-excursion
2397 (save-restriction
2398 (let ((bra from) ce ; Positions of "{" and "}".
2399 new-cons
2400 (cache-pos (c-state-cache-top-lparen)) ; might be nil.
2401 (macro-start-or-from
2402 (progn (goto-char from)
2403 (c-beginning-of-macro)
2404 (point))))
2405 (or upper-lim (setq upper-lim from))
2406
2407 ;; If we're essentially repeating a fruitless search, just give up.
2408 (unless (and c-state-brace-pair-desert
2409 (eq cache-pos (car c-state-brace-pair-desert))
2410 (<= from (cdr c-state-brace-pair-desert)))
2411 ;; Only search what we absolutely need to:
2412 (if (and c-state-brace-pair-desert
2413 (> from (cdr c-state-brace-pair-desert)))
2414 (narrow-to-region (cdr c-state-brace-pair-desert) (point-max)))
2415
2416 ;; In the next pair of nested loops, the inner one moves back past a
2417 ;; pair of (mis-)matching parens or brackets; the outer one moves
2418 ;; back over a sequence of unmatched close brace/paren/bracket each
2419 ;; time round.
2420 (while
2421 (progn
2422 (c-safe
2423 (while
2424 (and (setq ce (scan-lists bra -1 -1)) ; back past )/]/}; might signal
2425 (setq bra (scan-lists ce -1 1)) ; back past (/[/{; might signal
2426 (or (> ce upper-lim)
2427 (not (eq (char-after bra) ?\{))
2428 (and (goto-char bra)
2429 (c-beginning-of-macro)
2430 (< (point) macro-start-or-from))))))
2431 (and ce (< ce bra)))
2432 (setq bra ce)) ; If we just backed over an unbalanced closing
2433 ; brace, ignore it.
2434
2435 (if (and ce (< bra ce) (eq (char-after bra) ?\{))
2436 ;; We've found the desired brace-pair.
2437 (progn
2438 (setq new-cons (cons bra (1+ ce)))
2439 (cond
2440 ((consp (car c-state-cache))
2441 (setcar c-state-cache new-cons))
2442 ((and (numberp (car c-state-cache)) ; probably never happens
2443 (< ce (car c-state-cache)))
2444 (setcdr c-state-cache
2445 (cons new-cons (cdr c-state-cache))))
2446 (t (setq c-state-cache (cons new-cons c-state-cache)))))
2447
2448 ;; We haven't found a brace pair. Record this.
2449 (setq c-state-brace-pair-desert (cons cache-pos from))))))))
2450
2451 (defsubst c-state-push-any-brace-pair (bra+1 macro-start-or-here)
2452 ;; If BRA+1 is nil, do nothing. Otherwise, BRA+1 is the buffer position
2453 ;; following a {, and that brace has a (mis-)matching } (or ]), and we
2454 ;; "push" "a" brace pair onto `c-state-cache'.
2455 ;;
2456 ;; Here "push" means overwrite the top element if it's itself a brace-pair,
2457 ;; otherwise push it normally.
2458 ;;
2459 ;; The brace pair we push is normally the one surrounding BRA+1, but if the
2460 ;; latter is inside a macro, not being a macro containing
2461 ;; MACRO-START-OR-HERE, we scan backwards through the buffer for a non-macro
2462 ;; base pair. This latter case is assumed to be rare.
2463 ;;
2464 ;; Note: POINT is not preserved in this routine.
2465 (if bra+1
2466 (if (or (> bra+1 macro-start-or-here)
2467 (progn (goto-char bra+1)
2468 (not (c-beginning-of-macro))))
2469 (setq c-state-cache
2470 (cons (cons (1- bra+1)
2471 (scan-lists bra+1 1 1))
2472 (if (consp (car c-state-cache))
2473 (cdr c-state-cache)
2474 c-state-cache)))
2475 ;; N.B. This defsubst codes one method for the simple, normal case,
2476 ;; and a more sophisticated, slower way for the general case. Don't
2477 ;; eliminate this defsubst - it's a speed optimisation.
2478 (c-append-lower-brace-pair-to-state-cache (1- bra+1)))))
2479
2480 (defun c-append-to-state-cache (from)
2481 ;; Scan the buffer from FROM to (point-max), adding elements into
2482 ;; `c-state-cache' for braces etc. Return a candidate for
2483 ;; `c-state-cache-good-pos'.
2484 ;;
2485 ;; FROM must be after the latest brace/paren/bracket in `c-state-cache', if
2486 ;; any. Typically, it is immediately after it. It must not be inside a
2487 ;; literal.
2488 (let ((here-bol (c-point 'bol (point-max)))
2489 (macro-start-or-here
2490 (save-excursion (goto-char (point-max))
2491 (if (c-beginning-of-macro)
2492 (point)
2493 (point-max))))
2494 pa+1 ; pos just after an opening PAren (or brace).
2495 (ren+1 from) ; usually a pos just after an closing paREN etc.
2496 ; Is actually the pos. to scan for a (/{/[ from,
2497 ; which sometimes is after a silly )/}/].
2498 paren+1 ; Pos after some opening or closing paren.
2499 paren+1s ; A list of `paren+1's; used to determine a
2500 ; good-pos.
2501 bra+1 ce+1 ; just after L/R bra-ces.
2502 bra+1s ; list of OLD values of bra+1.
2503 mstart) ; start of a macro.
2504
2505 (save-excursion
2506 ;; Each time round the following loop, we enter a succesively deeper
2507 ;; level of brace/paren nesting. (Except sometimes we "continue at
2508 ;; the existing level".) `pa+1' is a pos inside an opening
2509 ;; brace/paren/bracket, usually just after it.
2510 (while
2511 (progn
2512 ;; Each time round the next loop moves forward over an opening then
2513 ;; a closing brace/bracket/paren. This loop is white hot, so it
2514 ;; plays ugly tricks to go fast. DON'T PUT ANYTHING INTO THIS
2515 ;; LOOP WHICH ISN'T ABSOLUTELY NECESSARY!!! It terminates when a
2516 ;; call of `scan-lists' signals an error, which happens when there
2517 ;; are no more b/b/p's to scan.
2518 (c-safe
2519 (while t
2520 (setq pa+1 (scan-lists ren+1 1 -1) ; Into (/{/[; might signal
2521 paren+1s (cons pa+1 paren+1s))
2522 (setq ren+1 (scan-lists pa+1 1 1)) ; Out of )/}/]; might signal
2523 (if (and (eq (char-before pa+1) ?{)) ; Check for a macro later.
2524 (setq bra+1 pa+1))
2525 (setcar paren+1s ren+1)))
2526
2527 (if (and pa+1 (> pa+1 ren+1))
2528 ;; We've just entered a deeper nesting level.
2529 (progn
2530 ;; Insert the brace pair (if present) and the single open
2531 ;; paren/brace/bracket into `c-state-cache' It cannot be
2532 ;; inside a macro, except one around point, because of what
2533 ;; `c-neutralize-syntax-in-CPP' has done.
2534 (c-state-push-any-brace-pair bra+1 macro-start-or-here)
2535 ;; Insert the opening brace/bracket/paren position.
2536 (setq c-state-cache (cons (1- pa+1) c-state-cache))
2537 ;; Clear admin stuff for the next more nested part of the scan.
2538 (setq ren+1 pa+1 pa+1 nil bra+1 nil bra+1s nil)
2539 t) ; Carry on the loop
2540
2541 ;; All open p/b/b's at this nesting level, if any, have probably
2542 ;; been closed by matching/mismatching ones. We're probably
2543 ;; finished - we just need to check for having found an
2544 ;; unmatched )/}/], which we ignore. Such a )/}/] can't be in a
2545 ;; macro, due the action of `c-neutralize-syntax-in-CPP'.
2546 (c-safe (setq ren+1 (scan-lists ren+1 1 1)))))) ; acts as loop control.
2547
2548 ;; Record the final, innermost, brace-pair if there is one.
2549 (c-state-push-any-brace-pair bra+1 macro-start-or-here)
2550
2551 ;; Determine a good pos
2552 (while (and (setq paren+1 (car paren+1s))
2553 (> (if (> paren+1 macro-start-or-here)
2554 paren+1
2555 (goto-char paren+1)
2556 (setq mstart (and (c-beginning-of-macro)
2557 (point)))
2558 (or mstart paren+1))
2559 here-bol))
2560 (setq paren+1s (cdr paren+1s)))
2561 (cond
2562 ((and paren+1 mstart)
2563 (min paren+1 mstart))
2564 (paren+1)
2565 (t from)))))
2566
2567 (defun c-remove-stale-state-cache (good-pos pps-point)
2568 ;; Remove stale entries from the `c-cache-state', i.e. those which will
2569 ;; not be in it when it is amended for position (point-max).
2570 ;; Additionally, the "outermost" open-brace entry before (point-max)
2571 ;; will be converted to a cons if the matching close-brace is scanned.
2572 ;;
2573 ;; GOOD-POS is a "maximal" "safe position" - there must be no open
2574 ;; parens/braces/brackets between GOOD-POS and (point-max).
2575 ;;
2576 ;; As a second thing, calculate the result of parse-partial-sexp at
2577 ;; PPS-POINT, w.r.t. GOOD-POS. The motivation here is that
2578 ;; `c-state-cache-good-pos' may become PPS-POINT, but the caller may need to
2579 ;; adjust it to get outside a string/comment. (Sorry about this! The code
2580 ;; needs to be FAST).
2581 ;;
2582 ;; Return a list (GOOD-POS SCAN-BACK-POS PPS-STATE), where
2583 ;; o - GOOD-POS is a position where the new value `c-state-cache' is known
2584 ;; to be good (we aim for this to be as high as possible);
2585 ;; o - SCAN-BACK-POS, if not nil, indicates there may be a brace pair
2586 ;; preceding POS which needs to be recorded in `c-state-cache'. It is a
2587 ;; position to scan backwards from.
2588 ;; o - PPS-STATE is the parse-partial-sexp state at PPS-POINT.
2589 (save-restriction
2590 (narrow-to-region 1 (point-max))
2591 (save-excursion
2592 (let* ((in-macro-start ; start of macro containing (point-max) or nil.
2593 (save-excursion
2594 (goto-char (point-max))
2595 (and (c-beginning-of-macro)
2596 (point))))
2597 (good-pos-actual-macro-start ; Start of macro containing good-pos
2598 ; or nil
2599 (and (< good-pos (point-max))
2600 (save-excursion
2601 (goto-char good-pos)
2602 (and (c-beginning-of-macro)
2603 (point)))))
2604 (good-pos-actual-macro-end ; End of this macro, (maybe
2605 ; (point-max)), or nil.
2606 (and good-pos-actual-macro-start
2607 (save-excursion
2608 (goto-char good-pos-actual-macro-start)
2609 (c-end-of-macro)
2610 (point))))
2611 pps-state ; Will be 9 or 10 elements long.
2612 pos
2613 upper-lim ; ,beyond which `c-state-cache' entries are removed
2614 scan-back-pos
2615 pair-beg pps-point-state target-depth)
2616
2617 ;; Remove entries beyond (point-max). Also remove any entries inside
2618 ;; a macro, unless (point-max) is in the same macro.
2619 (setq upper-lim
2620 (if (or (null c-state-old-cpp-beg)
2621 (and (> (point-max) c-state-old-cpp-beg)
2622 (< (point-max) c-state-old-cpp-end)))
2623 (point-max)
2624 (min (point-max) c-state-old-cpp-beg)))
2625 (while (and c-state-cache (>= (c-state-cache-top-lparen) upper-lim))
2626 (setq c-state-cache (cdr c-state-cache)))
2627 ;; If `upper-lim' is inside the last recorded brace pair, remove its
2628 ;; RBrace and indicate we'll need to search backwards for a previous
2629 ;; brace pair.
2630 (when (and c-state-cache
2631 (consp (car c-state-cache))
2632 (> (cdar c-state-cache) upper-lim))
2633 (setcar c-state-cache (caar c-state-cache))
2634 (setq scan-back-pos (car c-state-cache)))
2635
2636 ;; The next loop jumps forward out of a nested level of parens each
2637 ;; time round; the corresponding elements in `c-state-cache' are
2638 ;; removed. `pos' is just after the brace-pair or the open paren at
2639 ;; (car c-state-cache). There can be no open parens/braces/brackets
2640 ;; between `good-pos'/`good-pos-actual-macro-start' and (point-max),
2641 ;; due to the interface spec to this function.
2642 (setq pos (if (and good-pos-actual-macro-end
2643 (not (eq good-pos-actual-macro-start
2644 in-macro-start)))
2645 (1+ good-pos-actual-macro-end) ; get outside the macro as
2646 ; marked by a `category' text property.
2647 good-pos))
2648 (goto-char pos)
2649 (while (and c-state-cache
2650 (< (point) (point-max)))
2651 (cond
2652 ((null pps-state) ; first time through
2653 (setq target-depth -1))
2654 ((eq (car pps-state) target-depth) ; found closing ),},]
2655 (setq target-depth (1- (car pps-state))))
2656 ;; Do nothing when we've merely reached pps-point.
2657 )
2658
2659 ;; Scan!
2660 (setq pps-state
2661 (parse-partial-sexp
2662 (point) (if (< (point) pps-point) pps-point (point-max))
2663 target-depth
2664 nil pps-state))
2665
2666 (if (= (point) pps-point)
2667 (setq pps-point-state pps-state))
2668
2669 (when (eq (car pps-state) target-depth)
2670 (setq pos (point)) ; POS is now just after an R-paren/brace.
2671 (cond
2672 ((and (consp (car c-state-cache))
2673 (eq (point) (cdar c-state-cache)))
2674 ;; We've just moved out of the paren pair containing the brace-pair
2675 ;; at (car c-state-cache). `pair-beg' is where the open paren is,
2676 ;; and is potentially where the open brace of a cons in
2677 ;; c-state-cache will be.
2678 (setq pair-beg (car-safe (cdr c-state-cache))
2679 c-state-cache (cdr-safe (cdr c-state-cache)))) ; remove {}pair + containing Lparen.
2680 ((numberp (car c-state-cache))
2681 (setq pair-beg (car c-state-cache)
2682 c-state-cache (cdr c-state-cache))) ; remove this
2683 ; containing Lparen
2684 ((numberp (cadr c-state-cache))
2685 (setq pair-beg (cadr c-state-cache)
2686 c-state-cache (cddr c-state-cache))) ; Remove a paren pair
2687 ; together with enclosed brace pair.
2688 ;; (t nil) ; Ignore an unmated Rparen.
2689 )))
2690
2691 (if (< (point) pps-point)
2692 (setq pps-state (parse-partial-sexp (point) pps-point
2693 nil nil ; TARGETDEPTH, STOPBEFORE
2694 pps-state)))
2695
2696 ;; If the last paren pair we moved out of was actually a brace pair,
2697 ;; insert it into `c-state-cache'.
2698 (when (and pair-beg (eq (char-after pair-beg) ?{))
2699 (if (consp (car-safe c-state-cache))
2700 (setq c-state-cache (cdr c-state-cache)))
2701 (setq c-state-cache (cons (cons pair-beg pos)
2702 c-state-cache)))
2703
2704 (list pos scan-back-pos pps-state)))))
2705
2706 (defun c-remove-stale-state-cache-backwards (here cache-pos)
2707 ;; Strip stale elements of `c-state-cache' by moving backwards through the
2708 ;; buffer, and inform the caller of the scenario detected.
2709 ;;
2710 ;; HERE is the position we're setting `c-state-cache' for.
2711 ;; CACHE-POS is just after the latest recorded position in `c-state-cache'
2712 ;; before HERE, or a position at or near point-min which isn't in a
2713 ;; literal.
2714 ;;
2715 ;; This function must only be called only when (> `c-state-cache-good-pos'
2716 ;; HERE). Usually the gap between CACHE-POS and HERE is large. It is thus
2717 ;; optimised to eliminate (or minimise) scanning between these two
2718 ;; positions.
2719 ;;
2720 ;; Return a three element list (GOOD-POS SCAN-BACK-POS FWD-FLAG), where:
2721 ;; o - GOOD-POS is a "good position", where `c-state-cache' is valid, or
2722 ;; could become so after missing elements are inserted into
2723 ;; `c-state-cache'. This is JUST AFTER an opening or closing
2724 ;; brace/paren/bracket which is already in `c-state-cache' or just before
2725 ;; one otherwise. exceptionally (when there's no such b/p/b handy) the BOL
2726 ;; before `here''s line, or the start of the literal containing it.
2727 ;; o - SCAN-BACK-POS, if non-nil, indicates there may be a brace pair
2728 ;; preceding POS which isn't recorded in `c-state-cache'. It is a position
2729 ;; to scan backwards from.
2730 ;; o - FWD-FLAG, if non-nil, indicates there may be parens/braces between
2731 ;; POS and HERE which aren't recorded in `c-state-cache'.
2732 ;;
2733 ;; The comments in this defun use "paren" to mean parenthesis or square
2734 ;; bracket (as contrasted with a brace), and "(" and ")" likewise.
2735 ;;
2736 ;; . {..} (..) (..) ( .. { } ) (...) ( .... . ..)
2737 ;; | | | | | |
2738 ;; CP E here D C good
2739 (let ((pos c-state-cache-good-pos)
2740 pa ren ; positions of "(" and ")"
2741 dropped-cons ; whether the last element dropped from `c-state-cache'
2742 ; was a cons (representing a brace-pair)
2743 good-pos ; see above.
2744 lit ; (START . END) of a literal containing some point.
2745 here-lit-start here-lit-end ; bounds of literal containing `here'
2746 ; or `here' itself.
2747 here- here+ ; start/end of macro around HERE, or HERE
2748 (here-bol (c-point 'bol here))
2749 (too-far-back (max (- here c-state-cache-too-far) 1)))
2750
2751 ;; Remove completely irrelevant entries from `c-state-cache'.
2752 (while (and c-state-cache
2753 (>= (setq pa (c-state-cache-top-lparen)) here))
2754 (setq dropped-cons (consp (car c-state-cache)))
2755 (setq c-state-cache (cdr c-state-cache))
2756 (setq pos pa))
2757 ;; At this stage, (> pos here);
2758 ;; (< (c-state-cache-top-lparen) here) (or is nil).
2759
2760 (cond
2761 ((and (consp (car c-state-cache))
2762 (> (cdar c-state-cache) here))
2763 ;; CASE 1: The top of the cache is a brace pair which now encloses
2764 ;; `here'. As good-pos, return the address. of the "{". Since we've no
2765 ;; knowledge of what's inside these braces, we have no alternative but
2766 ;; to direct the caller to scan the buffer from the opening brace.
2767 (setq pos (caar c-state-cache))
2768 (setcar c-state-cache pos)
2769 (list (1+ pos) pos t)) ; return value. We've just converted a brace pair
2770 ; entry into a { entry, so the caller needs to
2771 ; search for a brace pair before the {.
2772
2773 ;; `here' might be inside a literal. Check for this.
2774 ((progn
2775 (setq lit (c-state-literal-at here)
2776 here-lit-start (or (car lit) here)
2777 here-lit-end (or (cdr lit) here))
2778 ;; Has `here' just "newly entered" a macro?
2779 (save-excursion
2780 (goto-char here-lit-start)
2781 (if (and (c-beginning-of-macro)
2782 (or (null c-state-old-cpp-beg)
2783 (not (= (point) c-state-old-cpp-beg))))
2784 (progn
2785 (setq here- (point))
2786 (c-end-of-macro)
2787 (setq here+ (point)))
2788 (setq here- here-lit-start
2789 here+ here-lit-end)))
2790
2791 ;; `here' might be nested inside any depth of parens (or brackets but
2792 ;; not braces). Scan backwards to find the outermost such opening
2793 ;; paren, if there is one. This will be the scan position to return.
2794 (save-restriction
2795 (narrow-to-region cache-pos (point-max))
2796 (setq pos (c-state-balance-parens-backwards here- here+ pos)))
2797 nil)) ; for the cond
2798
2799 ((< pos here-lit-start)
2800 ;; CASE 2: Address of outermost ( or [ which now encloses `here', but
2801 ;; didn't enclose the (previous) `c-state-cache-good-pos'. If there is
2802 ;; a brace pair preceding this, it will already be in `c-state-cache',
2803 ;; unless there was a brace pair after it, i.e. there'll only be one to
2804 ;; scan for if we've just deleted one.
2805 (list pos (and dropped-cons pos) t)) ; Return value.
2806
2807 ;; `here' isn't enclosed in a (previously unrecorded) bracket/paren.
2808 ;; Further forward scanning isn't needed, but we still need to find a
2809 ;; GOOD-POS. Step out of all enclosing "("s on HERE's line.
2810 ((progn
2811 (save-restriction
2812 (narrow-to-region here-bol (point-max))
2813 (setq pos here-lit-start)
2814 (c-safe (while (setq pa (scan-lists pos -1 1))
2815 (setq pos pa)))) ; might signal
2816 nil)) ; for the cond
2817
2818 ((setq ren (c-safe-scan-lists pos -1 -1 too-far-back))
2819 ;; CASE 3: After a }/)/] before `here''s BOL.
2820 (list (1+ ren) (and dropped-cons pos) nil)) ; Return value
2821
2822 (t
2823 ;; CASE 4; Best of a bad job: BOL before `here-bol', or beginning of
2824 ;; literal containing it.
2825 (setq good-pos (c-state-lit-beg (c-point 'bopl here-bol)))
2826 (list good-pos (and dropped-cons good-pos) nil)))))
2827
2828
2829 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2830 ;; Externally visible routines.
2831
2832 (defun c-state-cache-init ()
2833 (setq c-state-cache nil
2834 c-state-cache-good-pos 1
2835 c-state-nonlit-pos-cache nil
2836 c-state-nonlit-pos-cache-limit 1
2837 c-state-brace-pair-desert nil
2838 c-state-point-min 1
2839 c-state-point-min-lit-type nil
2840 c-state-point-min-lit-start nil
2841 c-state-min-scan-pos 1
2842 c-state-old-cpp-beg nil
2843 c-state-old-cpp-end nil)
2844 (c-state-mark-point-min-literal))
2845
2846 (defun c-invalidate-state-cache-1 (here)
2847 ;; Invalidate all info on `c-state-cache' that applies to the buffer at HERE
2848 ;; or higher and set `c-state-cache-good-pos' accordingly. The cache is
2849 ;; left in a consistent state.
2850 ;;
2851 ;; This is much like `c-whack-state-after', but it never changes a paren
2852 ;; pair element into an open paren element. Doing that would mean that the
2853 ;; new open paren wouldn't have the required preceding paren pair element.
2854 ;;
2855 ;; This function is called from c-after-change.
2856
2857 ;; The cache of non-literals:
2858 (if (< here c-state-nonlit-pos-cache-limit)
2859 (setq c-state-nonlit-pos-cache-limit here))
2860
2861 ;; `c-state-cache':
2862 ;; Case 1: if `here' is in a literal containing point-min, everything
2863 ;; becomes (or is already) nil.
2864 (if (or (null c-state-cache-good-pos)
2865 (< here (c-state-get-min-scan-pos)))
2866 (setq c-state-cache nil
2867 c-state-cache-good-pos nil
2868 c-state-min-scan-pos nil)
2869
2870 ;;; Truncate `c-state-cache' and set `c-state-cache-good-pos' to a value below
2871 ;;; `here'. To maintain its consistency, we may need to insert a new brace
2872 ;;; pair.
2873 (let ((here-bol (c-point 'bol here))
2874 too-high-pa ; recorded {/(/[ next above here, or nil.
2875 dropped-cons ; was the last removed element a brace pair?
2876 pa)
2877 ;; The easy bit - knock over-the-top bits off `c-state-cache'.
2878 (while (and c-state-cache
2879 (>= (setq pa (c-state-cache-top-paren)) here))
2880 (setq dropped-cons (consp (car c-state-cache))
2881 too-high-pa (c-state-cache-top-lparen)
2882 c-state-cache (cdr c-state-cache)))
2883
2884 ;; Do we need to add in an earlier brace pair, having lopped one off?
2885 (if (and dropped-cons
2886 (< too-high-pa (+ here c-state-cache-too-far)))
2887 (c-append-lower-brace-pair-to-state-cache too-high-pa here-bol))
2888 (setq c-state-cache-good-pos (or (c-state-cache-after-top-paren)
2889 (c-state-get-min-scan-pos)))))
2890
2891 ;; The brace-pair desert marker:
2892 (when (car c-state-brace-pair-desert)
2893 (if (< here (car c-state-brace-pair-desert))
2894 (setq c-state-brace-pair-desert nil)
2895 (if (< here (cdr c-state-brace-pair-desert))
2896 (setcdr c-state-brace-pair-desert here)))))
2897
2898 (defun c-parse-state-1 ()
2899 ;; Find and record all noteworthy parens between some good point earlier in
2900 ;; the file and point. That good point is at least the beginning of the
2901 ;; top-level construct we are in, or the beginning of the preceding
2902 ;; top-level construct if we aren't in one.
2903 ;;
2904 ;; The returned value is a list of the noteworthy parens with the last one
2905 ;; first. If an element in the list is an integer, it's the position of an
2906 ;; open paren (of any type) which has not been closed before the point. If
2907 ;; an element is a cons, it gives the position of a closed BRACE paren
2908 ;; pair[*]; the car is the start brace position and the cdr is the position
2909 ;; following the closing brace. Only the last closed brace paren pair
2910 ;; before each open paren and before the point is recorded, and thus the
2911 ;; state never contains two cons elements in succession. When a close brace
2912 ;; has no matching open brace (e.g., the matching brace is outside the
2913 ;; visible region), it is not represented in the returned value.
2914 ;;
2915 ;; [*] N.B. The close "brace" might be a mismatching close bracket or paren.
2916 ;; This defun explicitly treats mismatching parens/braces/brackets as
2917 ;; matching. It is the open brace which makes it a "brace" pair.
2918 ;;
2919 ;; If POINT is within a macro, open parens and brace pairs within
2920 ;; THIS macro MIGHT be recorded. This depends on whether their
2921 ;; syntactic properties have been suppressed by
2922 ;; `c-neutralize-syntax-in-CPP'. This might need fixing (2008-12-11).
2923 ;;
2924 ;; Currently no characters which are given paren syntax with the
2925 ;; syntax-table property are recorded, i.e. angle bracket arglist
2926 ;; parens are never present here. Note that this might change.
2927 ;;
2928 ;; BUG: This function doesn't cope entirely well with unbalanced
2929 ;; parens in macros. (2008-12-11: this has probably been resolved
2930 ;; by the function `c-neutralize-syntax-in-CPP'.) E.g. in the
2931 ;; following case the brace before the macro isn't balanced with the
2932 ;; one after it:
2933 ;;
2934 ;; {
2935 ;; #define X {
2936 ;; }
2937 ;;
2938 ;; Note to maintainers: this function DOES get called with point
2939 ;; within comments and strings, so don't assume it doesn't!
2940 ;;
2941 ;; This function might do hidden buffer changes.
2942 (let* ((here (point))
2943 (here-bopl (c-point 'bopl))
2944 strategy ; 'forward, 'backward etc..
2945 ;; Candidate positions to start scanning from:
2946 cache-pos ; highest position below HERE already existing in
2947 ; cache (or 1).
2948 good-pos
2949 start-point
2950 bopl-state
2951 res
2952 scan-backward-pos scan-forward-p) ; used for 'backward.
2953 ;; If POINT-MIN has changed, adjust the cache
2954 (unless (= (point-min) c-state-point-min)
2955 (c-renarrow-state-cache))
2956
2957 ;; Strategy?
2958 (setq res (c-parse-state-get-strategy here c-state-cache-good-pos)
2959 strategy (car res)
2960 cache-pos (cadr res)
2961 start-point (nth 2 res))
2962
2963 (when (eq strategy 'BOD)
2964 (setq c-state-cache nil
2965 c-state-cache-good-pos start-point))
2966
2967 ;; SCAN!
2968 (save-restriction
2969 (cond
2970 ((memq strategy '(forward BOD))
2971 (narrow-to-region (point-min) here)
2972 (setq res (c-remove-stale-state-cache start-point here-bopl))
2973 (setq cache-pos (car res)
2974 scan-backward-pos (cadr res)
2975 bopl-state (car (cddr res))) ; will be nil if (< here-bopl
2976 ; start-point)
2977 (if scan-backward-pos
2978 (c-append-lower-brace-pair-to-state-cache scan-backward-pos))
2979 (setq good-pos
2980 (c-append-to-state-cache cache-pos))
2981 (setq c-state-cache-good-pos
2982 (if (and bopl-state
2983 (< good-pos (- here c-state-cache-too-far)))
2984 (c-state-cache-non-literal-place here-bopl bopl-state)
2985 good-pos)))
2986
2987 ((eq strategy 'backward)
2988 (setq res (c-remove-stale-state-cache-backwards here cache-pos)
2989 good-pos (car res)
2990 scan-backward-pos (cadr res)
2991 scan-forward-p (car (cddr res)))
2992 (if scan-backward-pos
2993 (c-append-lower-brace-pair-to-state-cache
2994 scan-backward-pos))
2995 (setq c-state-cache-good-pos
2996 (if scan-forward-p
2997 (progn (narrow-to-region (point-min) here)
2998 (c-append-to-state-cache good-pos))
2999
3000 (c-get-cache-scan-pos good-pos))))
3001
3002 (t ; (eq strategy 'IN-LIT)
3003 (setq c-state-cache nil
3004 c-state-cache-good-pos nil)))))
3005
3006 c-state-cache)
3007
3008 (defun c-invalidate-state-cache (here)
3009 ;; This is a wrapper over `c-invalidate-state-cache-1'.
3010 ;;
3011 ;; It suppresses the syntactic effect of the < and > (template) brackets and
3012 ;; of all parens in preprocessor constructs, except for any such construct
3013 ;; containing point. We can then call `c-invalidate-state-cache-1' without
3014 ;; worrying further about macros and template delimiters.
3015 (c-with-<->-as-parens-suppressed
3016 (if (and c-state-old-cpp-beg
3017 (< c-state-old-cpp-beg here))
3018 (c-with-all-but-one-cpps-commented-out
3019 c-state-old-cpp-beg
3020 (min c-state-old-cpp-end here)
3021 (c-invalidate-state-cache-1 here))
3022 (c-with-cpps-commented-out
3023 (c-invalidate-state-cache-1 here)))))
3024
3025 (defun c-parse-state ()
3026 ;; This is a wrapper over `c-parse-state-1'. See that function for a
3027 ;; description of the functionality and return value.
3028 ;;
3029 ;; It suppresses the syntactic effect of the < and > (template) brackets and
3030 ;; of all parens in preprocessor constructs, except for any such construct
3031 ;; containing point. We can then call `c-parse-state-1' without worrying
3032 ;; further about macros and template delimiters.
3033 (let (here-cpp-beg here-cpp-end)
3034 (save-excursion
3035 (when (c-beginning-of-macro)
3036 (setq here-cpp-beg (point))
3037 (unless
3038 (> (setq here-cpp-end (c-syntactic-end-of-macro))
3039 here-cpp-beg)
3040 (setq here-cpp-beg nil here-cpp-end nil))))
3041 ;; FIXME!!! Put in a `condition-case' here to protect the integrity of the
3042 ;; subsystem.
3043 (prog1
3044 (c-with-<->-as-parens-suppressed
3045 (if (and here-cpp-beg (> here-cpp-end here-cpp-beg))
3046 (c-with-all-but-one-cpps-commented-out
3047 here-cpp-beg here-cpp-end
3048 (c-parse-state-1))
3049 (c-with-cpps-commented-out
3050 (c-parse-state-1))))
3051 (setq c-state-old-cpp-beg (and here-cpp-beg (copy-marker here-cpp-beg t))
3052 c-state-old-cpp-end (and here-cpp-end (copy-marker here-cpp-end t)))
3053 )))
3054
3055 ;; Debug tool to catch cache inconsistencies. This is called from
3056 ;; 000tests.el.
3057 (defvar c-debug-parse-state nil)
3058 (unless (fboundp 'c-real-parse-state)
3059 (fset 'c-real-parse-state (symbol-function 'c-parse-state)))
3060 (cc-bytecomp-defun c-real-parse-state)
3061 (defun c-debug-parse-state ()
3062 (let ((here (point)) (res1 (c-real-parse-state)) res2)
3063 (let ((c-state-cache nil)
3064 (c-state-cache-good-pos 1)
3065 (c-state-nonlit-pos-cache nil)
3066 (c-state-nonlit-pos-cache-limit 1)
3067 (c-state-brace-pair-desert nil)
3068 (c-state-point-min 1)
3069 (c-state-point-min-lit-type nil)
3070 (c-state-point-min-lit-start nil)
3071 (c-state-min-scan-pos 1)
3072 (c-state-old-cpp-beg nil)
3073 (c-state-old-cpp-end nil))
3074 (setq res2 (c-real-parse-state)))
3075 (unless (equal res1 res2)
3076 ;; The cache can actually go further back due to the ad-hoc way
3077 ;; the first paren is found, so try to whack off a bit of its
3078 ;; start before complaining.
3079 (save-excursion
3080 (goto-char (or (c-least-enclosing-brace res2) (point)))
3081 (c-beginning-of-defun-1)
3082 (while (not (or (bobp) (eq (char-after) ?{)))
3083 (c-beginning-of-defun-1))
3084 (unless (equal (c-whack-state-before (point) res1) res2)
3085 (message (concat "c-parse-state inconsistency at %s: "
3086 "using cache: %s, from scratch: %s")
3087 here res1 res2))))
3088 res1))
3089
3090 (defun c-toggle-parse-state-debug (&optional arg)
3091 (interactive "P")
3092 (setq c-debug-parse-state (c-calculate-state arg c-debug-parse-state))
3093 (fset 'c-parse-state (symbol-function (if c-debug-parse-state
3094 'c-debug-parse-state
3095 'c-real-parse-state)))
3096 (c-keep-region-active))
3097 (when c-debug-parse-state
3098 (c-toggle-parse-state-debug 1))
3099
3100 \f
3101 (defun c-whack-state-before (bufpos paren-state)
3102 ;; Whack off any state information from PAREN-STATE which lies
3103 ;; before BUFPOS. Not destructive on PAREN-STATE.
3104 (let* ((newstate (list nil))
3105 (ptr newstate)
3106 car)
3107 (while paren-state
3108 (setq car (car paren-state)
3109 paren-state (cdr paren-state))
3110 (if (< (if (consp car) (car car) car) bufpos)
3111 (setq paren-state nil)
3112 (setcdr ptr (list car))
3113 (setq ptr (cdr ptr))))
3114 (cdr newstate)))
3115
3116 (defun c-whack-state-after (bufpos paren-state)
3117 ;; Whack off any state information from PAREN-STATE which lies at or
3118 ;; after BUFPOS. Not destructive on PAREN-STATE.
3119 (catch 'done
3120 (while paren-state
3121 (let ((car (car paren-state)))
3122 (if (consp car)
3123 ;; just check the car, because in a balanced brace
3124 ;; expression, it must be impossible for the corresponding
3125 ;; close brace to be before point, but the open brace to
3126 ;; be after.
3127 (if (<= bufpos (car car))
3128 nil ; whack it off
3129 (if (< bufpos (cdr car))
3130 ;; its possible that the open brace is before
3131 ;; bufpos, but the close brace is after. In that
3132 ;; case, convert this to a non-cons element. The
3133 ;; rest of the state is before bufpos, so we're
3134 ;; done.
3135 (throw 'done (cons (car car) (cdr paren-state)))
3136 ;; we know that both the open and close braces are
3137 ;; before bufpos, so we also know that everything else
3138 ;; on state is before bufpos.
3139 (throw 'done paren-state)))
3140 (if (<= bufpos car)
3141 nil ; whack it off
3142 ;; it's before bufpos, so everything else should too.
3143 (throw 'done paren-state)))
3144 (setq paren-state (cdr paren-state)))
3145 nil)))
3146
3147 (defun c-most-enclosing-brace (paren-state &optional bufpos)
3148 ;; Return the bufpos of the innermost enclosing open paren before
3149 ;; bufpos, or nil if none was found.
3150 (let (enclosingp)
3151 (or bufpos (setq bufpos 134217727))
3152 (while paren-state
3153 (setq enclosingp (car paren-state)
3154 paren-state (cdr paren-state))
3155 (if (or (consp enclosingp)
3156 (>= enclosingp bufpos))
3157 (setq enclosingp nil)
3158 (setq paren-state nil)))
3159 enclosingp))
3160
3161 (defun c-least-enclosing-brace (paren-state)
3162 ;; Return the bufpos of the outermost enclosing open paren, or nil
3163 ;; if none was found.
3164 (let (pos elem)
3165 (while paren-state
3166 (setq elem (car paren-state)
3167 paren-state (cdr paren-state))
3168 (if (integerp elem)
3169 (setq pos elem)))
3170 pos))
3171
3172 (defun c-safe-position (bufpos paren-state)
3173 ;; Return the closest "safe" position recorded on PAREN-STATE that
3174 ;; is higher up than BUFPOS. Return nil if PAREN-STATE doesn't
3175 ;; contain any. Return nil if BUFPOS is nil, which is useful to
3176 ;; find the closest limit before a given limit that might be nil.
3177 ;;
3178 ;; A "safe" position is a position at or after a recorded open
3179 ;; paren, or after a recorded close paren. The returned position is
3180 ;; thus either the first position after a close brace, or the first
3181 ;; position after an enclosing paren, or at the enclosing paren in
3182 ;; case BUFPOS is immediately after it.
3183 (when bufpos
3184 (let (elem)
3185 (catch 'done
3186 (while paren-state
3187 (setq elem (car paren-state))
3188 (if (consp elem)
3189 (cond ((< (cdr elem) bufpos)
3190 (throw 'done (cdr elem)))
3191 ((< (car elem) bufpos)
3192 ;; See below.
3193 (throw 'done (min (1+ (car elem)) bufpos))))
3194 (if (< elem bufpos)
3195 ;; elem is the position at and not after the opening paren, so
3196 ;; we can go forward one more step unless it's equal to
3197 ;; bufpos. This is useful in some cases avoid an extra paren
3198 ;; level between the safe position and bufpos.
3199 (throw 'done (min (1+ elem) bufpos))))
3200 (setq paren-state (cdr paren-state)))))))
3201
3202 (defun c-beginning-of-syntax ()
3203 ;; This is used for `font-lock-beginning-of-syntax-function'. It
3204 ;; goes to the closest previous point that is known to be outside
3205 ;; any string literal or comment. `c-state-cache' is used if it has
3206 ;; a position in the vicinity.
3207 (let* ((paren-state c-state-cache)
3208 elem
3209
3210 (pos (catch 'done
3211 ;; Note: Similar code in `c-safe-position'. The
3212 ;; difference is that we accept a safe position at
3213 ;; the point and don't bother to go forward past open
3214 ;; parens.
3215 (while paren-state
3216 (setq elem (car paren-state))
3217 (if (consp elem)
3218 (cond ((<= (cdr elem) (point))
3219 (throw 'done (cdr elem)))
3220 ((<= (car elem) (point))
3221 (throw 'done (car elem))))
3222 (if (<= elem (point))
3223 (throw 'done elem)))
3224 (setq paren-state (cdr paren-state)))
3225 (point-min))))
3226
3227 (if (> pos (- (point) 4000))
3228 (goto-char pos)
3229 ;; The position is far back. Try `c-beginning-of-defun-1'
3230 ;; (although we can't be entirely sure it will go to a position
3231 ;; outside a comment or string in current emacsen). FIXME:
3232 ;; Consult `syntax-ppss' here.
3233 (c-beginning-of-defun-1)
3234 (if (< (point) pos)
3235 (goto-char pos)))))
3236
3237 \f
3238 ;; Tools for scanning identifiers and other tokens.
3239
3240 (defun c-on-identifier ()
3241 "Return non-nil if the point is on or directly after an identifier.
3242 Keywords are recognized and not considered identifiers. If an
3243 identifier is detected, the returned value is its starting position.
3244 If an identifier ends at the point and another begins at it \(can only
3245 happen in Pike) then the point for the preceding one is returned.
3246
3247 Note that this function might do hidden buffer changes. See the
3248 comment at the start of cc-engine.el for more info."
3249
3250 ;; FIXME: Shouldn't this function handle "operator" in C++?
3251
3252 (save-excursion
3253 (skip-syntax-backward "w_")
3254
3255 (or
3256
3257 ;; Check for a normal (non-keyword) identifier.
3258 (and (looking-at c-symbol-start)
3259 (not (looking-at c-keywords-regexp))
3260 (point))
3261
3262 (when (c-major-mode-is 'pike-mode)
3263 ;; Handle the `<operator> syntax in Pike.
3264 (let ((pos (point)))
3265 (skip-chars-backward "-!%&*+/<=>^|~[]()")
3266 (and (if (< (skip-chars-backward "`") 0)
3267 t
3268 (goto-char pos)
3269 (eq (char-after) ?\`))
3270 (looking-at c-symbol-key)
3271 (>= (match-end 0) pos)
3272 (point))))
3273
3274 ;; Handle the "operator +" syntax in C++.
3275 (when (and c-overloadable-operators-regexp
3276 (= (c-backward-token-2 0) 0))
3277
3278 (cond ((and (looking-at c-overloadable-operators-regexp)
3279 (or (not c-opt-op-identifier-prefix)
3280 (and (= (c-backward-token-2 1) 0)
3281 (looking-at c-opt-op-identifier-prefix))))
3282 (point))
3283
3284 ((save-excursion
3285 (and c-opt-op-identifier-prefix
3286 (looking-at c-opt-op-identifier-prefix)
3287 (= (c-forward-token-2 1) 0)
3288 (looking-at c-overloadable-operators-regexp)))
3289 (point))))
3290
3291 )))
3292
3293 (defsubst c-simple-skip-symbol-backward ()
3294 ;; If the point is at the end of a symbol then skip backward to the
3295 ;; beginning of it. Don't move otherwise. Return non-nil if point
3296 ;; moved.
3297 ;;
3298 ;; This function might do hidden buffer changes.
3299 (or (< (skip-syntax-backward "w_") 0)
3300 (and (c-major-mode-is 'pike-mode)
3301 ;; Handle the `<operator> syntax in Pike.
3302 (let ((pos (point)))
3303 (if (and (< (skip-chars-backward "-!%&*+/<=>^|~[]()") 0)
3304 (< (skip-chars-backward "`") 0)
3305 (looking-at c-symbol-key)
3306 (>= (match-end 0) pos))
3307 t
3308 (goto-char pos)
3309 nil)))))
3310
3311 (defun c-beginning-of-current-token (&optional back-limit)
3312 ;; Move to the beginning of the current token. Do not move if not
3313 ;; in the middle of one. BACK-LIMIT may be used to bound the
3314 ;; backward search; if given it's assumed to be at the boundary
3315 ;; between two tokens. Return non-nil if the point is moved, nil
3316 ;; otherwise.
3317 ;;
3318 ;; This function might do hidden buffer changes.
3319 (let ((start (point)))
3320 (if (looking-at "\\w\\|\\s_")
3321 (skip-syntax-backward "w_" back-limit)
3322 (when (< (skip-syntax-backward ".()" back-limit) 0)
3323 (while (let ((pos (or (and (looking-at c-nonsymbol-token-regexp)
3324 (match-end 0))
3325 ;; `c-nonsymbol-token-regexp' should always match
3326 ;; since we've skipped backward over punctuator
3327 ;; or paren syntax, but consume one char in case
3328 ;; it doesn't so that we don't leave point before
3329 ;; some earlier incorrect token.
3330 (1+ (point)))))
3331 (if (<= pos start)
3332 (goto-char pos))))))
3333 (< (point) start)))
3334
3335 (defun c-end-of-current-token (&optional back-limit)
3336 ;; Move to the end of the current token. Do not move if not in the
3337 ;; middle of one. BACK-LIMIT may be used to bound the backward
3338 ;; search; if given it's assumed to be at the boundary between two
3339 ;; tokens. Return non-nil if the point is moved, nil otherwise.
3340 ;;
3341 ;; This function might do hidden buffer changes.
3342 (let ((start (point)))
3343 (cond ((< (skip-syntax-backward "w_" (1- start)) 0)
3344 (skip-syntax-forward "w_"))
3345 ((< (skip-syntax-backward ".()" back-limit) 0)
3346 (while (progn
3347 (if (looking-at c-nonsymbol-token-regexp)
3348 (goto-char (match-end 0))
3349 ;; `c-nonsymbol-token-regexp' should always match since
3350 ;; we've skipped backward over punctuator or paren
3351 ;; syntax, but move forward in case it doesn't so that
3352 ;; we don't leave point earlier than we started with.
3353 (forward-char))
3354 (< (point) start)))))
3355 (> (point) start)))
3356
3357 (defconst c-jump-syntax-balanced
3358 (if (memq 'gen-string-delim c-emacs-features)
3359 "\\w\\|\\s_\\|\\s\(\\|\\s\)\\|\\s\"\\|\\s|"
3360 "\\w\\|\\s_\\|\\s\(\\|\\s\)\\|\\s\""))
3361
3362 (defconst c-jump-syntax-unbalanced
3363 (if (memq 'gen-string-delim c-emacs-features)
3364 "\\w\\|\\s_\\|\\s\"\\|\\s|"
3365 "\\w\\|\\s_\\|\\s\""))
3366
3367 (defun c-forward-token-2 (&optional count balanced limit)
3368 "Move forward by tokens.
3369 A token is defined as all symbols and identifiers which aren't
3370 syntactic whitespace \(note that multicharacter tokens like \"==\" are
3371 treated properly). Point is always either left at the beginning of a
3372 token or not moved at all. COUNT specifies the number of tokens to
3373 move; a negative COUNT moves in the opposite direction. A COUNT of 0
3374 moves to the next token beginning only if not already at one. If
3375 BALANCED is true, move over balanced parens, otherwise move into them.
3376 Also, if BALANCED is true, never move out of an enclosing paren.
3377
3378 LIMIT sets the limit for the movement and defaults to the point limit.
3379 The case when LIMIT is set in the middle of a token, comment or macro
3380 is handled correctly, i.e. the point won't be left there.
3381
3382 Return the number of tokens left to move \(positive or negative). If
3383 BALANCED is true, a move over a balanced paren counts as one. Note
3384 that if COUNT is 0 and no appropriate token beginning is found, 1 will
3385 be returned. Thus, a return value of 0 guarantees that point is at
3386 the requested position and a return value less \(without signs) than
3387 COUNT guarantees that point is at the beginning of some token.
3388
3389 Note that this function might do hidden buffer changes. See the
3390 comment at the start of cc-engine.el for more info."
3391
3392 (or count (setq count 1))
3393 (if (< count 0)
3394 (- (c-backward-token-2 (- count) balanced limit))
3395
3396 (let ((jump-syntax (if balanced
3397 c-jump-syntax-balanced
3398 c-jump-syntax-unbalanced))
3399 (last (point))
3400 (prev (point)))
3401
3402 (if (zerop count)
3403 ;; If count is zero we should jump if in the middle of a token.
3404 (c-end-of-current-token))
3405
3406 (save-restriction
3407 (if limit (narrow-to-region (point-min) limit))
3408 (if (/= (point)
3409 (progn (c-forward-syntactic-ws) (point)))
3410 ;; Skip whitespace. Count this as a move if we did in
3411 ;; fact move.
3412 (setq count (max (1- count) 0)))
3413
3414 (if (eobp)
3415 ;; Moved out of bounds. Make sure the returned count isn't zero.
3416 (progn
3417 (if (zerop count) (setq count 1))
3418 (goto-char last))
3419
3420 ;; Use `condition-case' to avoid having the limit tests
3421 ;; inside the loop.
3422 (condition-case nil
3423 (while (and
3424 (> count 0)
3425 (progn
3426 (setq last (point))
3427 (cond ((looking-at jump-syntax)
3428 (goto-char (scan-sexps (point) 1))
3429 t)
3430 ((looking-at c-nonsymbol-token-regexp)
3431 (goto-char (match-end 0))
3432 t)
3433 ;; `c-nonsymbol-token-regexp' above should always
3434 ;; match if there are correct tokens. Try to
3435 ;; widen to see if the limit was set in the
3436 ;; middle of one, else fall back to treating
3437 ;; the offending thing as a one character token.
3438 ((and limit
3439 (save-restriction
3440 (widen)
3441 (looking-at c-nonsymbol-token-regexp)))
3442 nil)
3443 (t
3444 (forward-char)
3445 t))))
3446 (c-forward-syntactic-ws)
3447 (setq prev last
3448 count (1- count)))
3449 (error (goto-char last)))
3450
3451 (when (eobp)
3452 (goto-char prev)
3453 (setq count (1+ count)))))
3454
3455 count)))
3456
3457 (defun c-backward-token-2 (&optional count balanced limit)
3458 "Move backward by tokens.
3459 See `c-forward-token-2' for details."
3460
3461 (or count (setq count 1))
3462 (if (< count 0)
3463 (- (c-forward-token-2 (- count) balanced limit))
3464
3465 (or limit (setq limit (point-min)))
3466 (let ((jump-syntax (if balanced
3467 c-jump-syntax-balanced
3468 c-jump-syntax-unbalanced))
3469 (last (point)))
3470
3471 (if (zerop count)
3472 ;; The count is zero so try to skip to the beginning of the
3473 ;; current token.
3474 (if (> (point)
3475 (progn (c-beginning-of-current-token) (point)))
3476 (if (< (point) limit)
3477 ;; The limit is inside the same token, so return 1.
3478 (setq count 1))
3479
3480 ;; We're not in the middle of a token. If there's
3481 ;; whitespace after the point then we must move backward,
3482 ;; so set count to 1 in that case.
3483 (and (looking-at c-syntactic-ws-start)
3484 ;; If we're looking at a '#' that might start a cpp
3485 ;; directive then we have to do a more elaborate check.
3486 (or (/= (char-after) ?#)
3487 (not c-opt-cpp-prefix)
3488 (save-excursion
3489 (and (= (point)
3490 (progn (beginning-of-line)
3491 (looking-at "[ \t]*")
3492 (match-end 0)))
3493 (or (bobp)
3494 (progn (backward-char)
3495 (not (eq (char-before) ?\\)))))))
3496 (setq count 1))))
3497
3498 ;; Use `condition-case' to avoid having to check for buffer
3499 ;; limits in `backward-char', `scan-sexps' and `goto-char' below.
3500 (condition-case nil
3501 (while (and
3502 (> count 0)
3503 (progn
3504 (c-backward-syntactic-ws)
3505 (backward-char)
3506 (if (looking-at jump-syntax)
3507 (goto-char (scan-sexps (1+ (point)) -1))
3508 ;; This can be very inefficient if there's a long
3509 ;; sequence of operator tokens without any separation.
3510 ;; That doesn't happen in practice, anyway.
3511 (c-beginning-of-current-token))
3512 (>= (point) limit)))
3513 (setq last (point)
3514 count (1- count)))
3515 (error (goto-char last)))
3516
3517 (if (< (point) limit)
3518 (goto-char last))
3519
3520 count)))
3521
3522 (defun c-forward-token-1 (&optional count balanced limit)
3523 "Like `c-forward-token-2' but doesn't treat multicharacter operator
3524 tokens like \"==\" as single tokens, i.e. all sequences of symbol
3525 characters are jumped over character by character. This function is
3526 for compatibility only; it's only a wrapper over `c-forward-token-2'."
3527 (let ((c-nonsymbol-token-regexp "\\s.\\|\\s\(\\|\\s\)"))
3528 (c-forward-token-2 count balanced limit)))
3529
3530 (defun c-backward-token-1 (&optional count balanced limit)
3531 "Like `c-backward-token-2' but doesn't treat multicharacter operator
3532 tokens like \"==\" as single tokens, i.e. all sequences of symbol
3533 characters are jumped over character by character. This function is
3534 for compatibility only; it's only a wrapper over `c-backward-token-2'."
3535 (let ((c-nonsymbol-token-regexp "\\s.\\|\\s\(\\|\\s\)"))
3536 (c-backward-token-2 count balanced limit)))
3537
3538 \f
3539 ;; Tools for doing searches restricted to syntactically relevant text.
3540
3541 (defun c-syntactic-re-search-forward (regexp &optional bound noerror
3542 paren-level not-inside-token
3543 lookbehind-submatch)
3544 "Like `re-search-forward', but only report matches that are found
3545 in syntactically significant text. I.e. matches in comments, macros
3546 or string literals are ignored. The start point is assumed to be
3547 outside any comment, macro or string literal, or else the content of
3548 that region is taken as syntactically significant text.
3549
3550 If PAREN-LEVEL is non-nil, an additional restriction is added to
3551 ignore matches in nested paren sexps. The search will also not go
3552 outside the current list sexp, which has the effect that if the point
3553 should be moved to BOUND when no match is found \(i.e. NOERROR is
3554 neither nil nor t), then it will be at the closing paren if the end of
3555 the current list sexp is encountered first.
3556
3557 If NOT-INSIDE-TOKEN is non-nil, matches in the middle of tokens are
3558 ignored. Things like multicharacter operators and special symbols
3559 \(e.g. \"`()\" in Pike) are handled but currently not floating point
3560 constants.
3561
3562 If LOOKBEHIND-SUBMATCH is non-nil, it's taken as a number of a
3563 subexpression in REGEXP. The end of that submatch is used as the
3564 position to check for syntactic significance. If LOOKBEHIND-SUBMATCH
3565 isn't used or if that subexpression didn't match then the start
3566 position of the whole match is used instead. The \"look behind\"
3567 subexpression is never tested before the starting position, so it
3568 might be a good idea to include \\=\\= as a match alternative in it.
3569
3570 Optimization note: Matches might be missed if the \"look behind\"
3571 subexpression can match the end of nonwhite syntactic whitespace,
3572 i.e. the end of comments or cpp directives. This since the function
3573 skips over such things before resuming the search. It's on the other
3574 hand not safe to assume that the \"look behind\" subexpression never
3575 matches syntactic whitespace.
3576
3577 Bug: Unbalanced parens inside cpp directives are currently not handled
3578 correctly \(i.e. they don't get ignored as they should) when
3579 PAREN-LEVEL is set.
3580
3581 Note that this function might do hidden buffer changes. See the
3582 comment at the start of cc-engine.el for more info."
3583
3584 (or bound (setq bound (point-max)))
3585 (if paren-level (setq paren-level -1))
3586
3587 ;;(message "c-syntactic-re-search-forward %s %s %S" (point) bound regexp)
3588
3589 (let ((start (point))
3590 tmp
3591 ;; Start position for the last search.
3592 search-pos
3593 ;; The `parse-partial-sexp' state between the start position
3594 ;; and the point.
3595 state
3596 ;; The current position after the last state update. The next
3597 ;; `parse-partial-sexp' continues from here.
3598 (state-pos (point))
3599 ;; The position at which to check the state and the state
3600 ;; there. This is separate from `state-pos' since we might
3601 ;; need to back up before doing the next search round.
3602 check-pos check-state
3603 ;; Last position known to end a token.
3604 (last-token-end-pos (point-min))
3605 ;; Set when a valid match is found.
3606 found)
3607
3608 (condition-case err
3609 (while
3610 (and
3611 (progn
3612 (setq search-pos (point))
3613 (re-search-forward regexp bound noerror))
3614
3615 (progn
3616 (setq state (parse-partial-sexp
3617 state-pos (match-beginning 0) paren-level nil state)
3618 state-pos (point))
3619 (if (setq check-pos (and lookbehind-submatch
3620 (or (not paren-level)
3621 (>= (car state) 0))
3622 (match-end lookbehind-submatch)))
3623 (setq check-state (parse-partial-sexp
3624 state-pos check-pos paren-level nil state))
3625 (setq check-pos state-pos
3626 check-state state))
3627
3628 ;; NOTE: If we got a look behind subexpression and get
3629 ;; an insignificant match in something that isn't
3630 ;; syntactic whitespace (i.e. strings or in nested
3631 ;; parentheses), then we can never skip more than a
3632 ;; single character from the match start position
3633 ;; (i.e. `state-pos' here) before continuing the
3634 ;; search. That since the look behind subexpression
3635 ;; might match the end of the insignificant region in
3636 ;; the next search.
3637
3638 (cond
3639 ((elt check-state 7)
3640 ;; Match inside a line comment. Skip to eol. Use
3641 ;; `re-search-forward' instead of `skip-chars-forward' to get
3642 ;; the right bound behavior.
3643 (re-search-forward "[\n\r]" bound noerror))
3644
3645 ((elt check-state 4)
3646 ;; Match inside a block comment. Skip to the '*/'.
3647 (search-forward "*/" bound noerror))
3648
3649 ((and (not (elt check-state 5))
3650 (eq (char-before check-pos) ?/)
3651 (not (c-get-char-property (1- check-pos) 'syntax-table))
3652 (memq (char-after check-pos) '(?/ ?*)))
3653 ;; Match in the middle of the opener of a block or line
3654 ;; comment.
3655 (if (= (char-after check-pos) ?/)
3656 (re-search-forward "[\n\r]" bound noerror)
3657 (search-forward "*/" bound noerror)))
3658
3659 ;; The last `parse-partial-sexp' above might have
3660 ;; stopped short of the real check position if the end
3661 ;; of the current sexp was encountered in paren-level
3662 ;; mode. The checks above are always false in that
3663 ;; case, and since they can do better skipping in
3664 ;; lookbehind-submatch mode, we do them before
3665 ;; checking the paren level.
3666
3667 ((and paren-level
3668 (/= (setq tmp (car check-state)) 0))
3669 ;; Check the paren level first since we're short of the
3670 ;; syntactic checking position if the end of the
3671 ;; current sexp was encountered by `parse-partial-sexp'.
3672 (if (> tmp 0)
3673
3674 ;; Inside a nested paren sexp.
3675 (if lookbehind-submatch
3676 ;; See the NOTE above.
3677 (progn (goto-char state-pos) t)
3678 ;; Skip out of the paren quickly.
3679 (setq state (parse-partial-sexp state-pos bound 0 nil state)
3680 state-pos (point)))
3681
3682 ;; Have exited the current paren sexp.
3683 (if noerror
3684 (progn
3685 ;; The last `parse-partial-sexp' call above
3686 ;; has left us just after the closing paren
3687 ;; in this case, so we can modify the bound
3688 ;; to leave the point at the right position
3689 ;; upon return.
3690 (setq bound (1- (point)))
3691 nil)
3692 (signal 'search-failed (list regexp)))))
3693
3694 ((setq tmp (elt check-state 3))
3695 ;; Match inside a string.
3696 (if (or lookbehind-submatch
3697 (not (integerp tmp)))
3698 ;; See the NOTE above.
3699 (progn (goto-char state-pos) t)
3700 ;; Skip to the end of the string before continuing.
3701 (let ((ender (make-string 1 tmp)) (continue t))
3702 (while (if (search-forward ender bound noerror)
3703 (progn
3704 (setq state (parse-partial-sexp
3705 state-pos (point) nil nil state)
3706 state-pos (point))
3707 (elt state 3))
3708 (setq continue nil)))
3709 continue)))
3710
3711 ((save-excursion
3712 (save-match-data
3713 (c-beginning-of-macro start)))
3714 ;; Match inside a macro. Skip to the end of it.
3715 (c-end-of-macro)
3716 (cond ((<= (point) bound) t)
3717 (noerror nil)
3718 (t (signal 'search-failed (list regexp)))))
3719
3720 ((and not-inside-token
3721 (or (< check-pos last-token-end-pos)
3722 (< check-pos
3723 (save-excursion
3724 (goto-char check-pos)
3725 (save-match-data
3726 (c-end-of-current-token last-token-end-pos))
3727 (setq last-token-end-pos (point))))))
3728 ;; Inside a token.
3729 (if lookbehind-submatch
3730 ;; See the NOTE above.
3731 (goto-char state-pos)
3732 (goto-char (min last-token-end-pos bound))))
3733
3734 (t
3735 ;; A real match.
3736 (setq found t)
3737 nil)))
3738
3739 ;; Should loop to search again, but take care to avoid
3740 ;; looping on the same spot.
3741 (or (/= search-pos (point))
3742 (if (= (point) bound)
3743 (if noerror
3744 nil
3745 (signal 'search-failed (list regexp)))
3746 (forward-char)
3747 t))))
3748
3749 (error
3750 (goto-char start)
3751 (signal (car err) (cdr err))))
3752
3753 ;;(message "c-syntactic-re-search-forward done %s" (or (match-end 0) (point)))
3754
3755 (if found
3756 (progn
3757 (goto-char (match-end 0))
3758 (match-end 0))
3759
3760 ;; Search failed. Set point as appropriate.
3761 (if (eq noerror t)
3762 (goto-char start)
3763 (goto-char bound))
3764 nil)))
3765
3766 (defvar safe-pos-list) ; bound in c-syntactic-skip-backward
3767
3768 (defsubst c-ssb-lit-begin ()
3769 ;; Return the start of the literal point is in, or nil.
3770 ;; We read and write the variables `safe-pos', `safe-pos-list', `state'
3771 ;; bound in the caller.
3772
3773 ;; Use `parse-partial-sexp' from a safe position down to the point to check
3774 ;; if it's outside comments and strings.
3775 (save-excursion
3776 (let ((pos (point)) safe-pos state pps-end-pos)
3777 ;; Pick a safe position as close to the point as possible.
3778 ;;
3779 ;; FIXME: Consult `syntax-ppss' here if our cache doesn't give a good
3780 ;; position.
3781
3782 (while (and safe-pos-list
3783 (> (car safe-pos-list) (point)))
3784 (setq safe-pos-list (cdr safe-pos-list)))
3785 (unless (setq safe-pos (car-safe safe-pos-list))
3786 (setq safe-pos (max (or (c-safe-position
3787 (point) (or c-state-cache
3788 (c-parse-state)))
3789 0)
3790 (point-min))
3791 safe-pos-list (list safe-pos)))
3792
3793 ;; Cache positions along the way to use if we have to back up more. We
3794 ;; cache every closing paren on the same level. If the paren cache is
3795 ;; relevant in this region then we're typically already on the same
3796 ;; level as the target position. Note that we might cache positions
3797 ;; after opening parens in case safe-pos is in a nested list. That's
3798 ;; both uncommon and harmless.
3799 (while (progn
3800 (setq state (parse-partial-sexp
3801 safe-pos pos 0))
3802 (< (point) pos))
3803 (setq safe-pos (point)
3804 safe-pos-list (cons safe-pos safe-pos-list)))
3805
3806 ;; If the state contains the start of the containing sexp we cache that
3807 ;; position too, so that parse-partial-sexp in the next run has a bigger
3808 ;; chance of starting at the same level as the target position and thus
3809 ;; will get more good safe positions into the list.
3810 (if (elt state 1)
3811 (setq safe-pos (1+ (elt state 1))
3812 safe-pos-list (cons safe-pos safe-pos-list)))
3813
3814 (if (or (elt state 3) (elt state 4))
3815 ;; Inside string or comment. Continue search at the
3816 ;; beginning of it.
3817 (elt state 8)))))
3818
3819 (defun c-syntactic-skip-backward (skip-chars &optional limit paren-level)
3820 "Like `skip-chars-backward' but only look at syntactically relevant chars,
3821 i.e. don't stop at positions inside syntactic whitespace or string
3822 literals. Preprocessor directives are also ignored, with the exception
3823 of the one that the point starts within, if any. If LIMIT is given,
3824 it's assumed to be at a syntactically relevant position.
3825
3826 If PAREN-LEVEL is non-nil, the function won't stop in nested paren
3827 sexps, and the search will also not go outside the current paren sexp.
3828 However, if LIMIT or the buffer limit is reached inside a nested paren
3829 then the point will be left at the limit.
3830
3831 Non-nil is returned if the point moved, nil otherwise.
3832
3833 Note that this function might do hidden buffer changes. See the
3834 comment at the start of cc-engine.el for more info."
3835
3836 (let ((start (point))
3837 state-2
3838 ;; A list of syntactically relevant positions in descending
3839 ;; order. It's used to avoid scanning repeatedly over
3840 ;; potentially large regions with `parse-partial-sexp' to verify
3841 ;; each position. Used in `c-ssb-lit-begin'
3842 safe-pos-list
3843 ;; The result from `c-beginning-of-macro' at the start position or the
3844 ;; start position itself if it isn't within a macro. Evaluated on
3845 ;; demand.
3846 start-macro-beg
3847 ;; The earliest position after the current one with the same paren
3848 ;; level. Used only when `paren-level' is set.
3849 lit-beg
3850 (paren-level-pos (point)))
3851
3852 (while
3853 (progn
3854 ;; The next loop "tries" to find the end point each time round,
3855 ;; loops when it hasn't succeeded.
3856 (while
3857 (and
3858 (< (skip-chars-backward skip-chars limit) 0)
3859
3860 (let ((pos (point)) state-2 pps-end-pos)
3861
3862 (cond
3863 ;; Don't stop inside a literal
3864 ((setq lit-beg (c-ssb-lit-begin))
3865 (goto-char lit-beg)
3866 t)
3867
3868 ((and paren-level
3869 (save-excursion
3870 (setq state-2 (parse-partial-sexp
3871 pos paren-level-pos -1)
3872 pps-end-pos (point))
3873 (/= (car state-2) 0)))
3874 ;; Not at the right level.
3875
3876 (if (and (< (car state-2) 0)
3877 ;; We stop above if we go out of a paren.
3878 ;; Now check whether it precedes or is
3879 ;; nested in the starting sexp.
3880 (save-excursion
3881 (setq state-2
3882 (parse-partial-sexp
3883 pps-end-pos paren-level-pos
3884 nil nil state-2))
3885 (< (car state-2) 0)))
3886
3887 ;; We've stopped short of the starting position
3888 ;; so the hit was inside a nested list. Go up
3889 ;; until we are at the right level.
3890 (condition-case nil
3891 (progn
3892 (goto-char (scan-lists pos -1
3893 (- (car state-2))))
3894 (setq paren-level-pos (point))
3895 (if (and limit (>= limit paren-level-pos))
3896 (progn
3897 (goto-char limit)
3898 nil)
3899 t))
3900 (error
3901 (goto-char (or limit (point-min)))
3902 nil))
3903
3904 ;; The hit was outside the list at the start
3905 ;; position. Go to the start of the list and exit.
3906 (goto-char (1+ (elt state-2 1)))
3907 nil))
3908
3909 ((c-beginning-of-macro limit)
3910 ;; Inside a macro.
3911 (if (< (point)
3912 (or start-macro-beg
3913 (setq start-macro-beg
3914 (save-excursion
3915 (goto-char start)
3916 (c-beginning-of-macro limit)
3917 (point)))))
3918 t
3919
3920 ;; It's inside the same macro we started in so it's
3921 ;; a relevant match.
3922 (goto-char pos)
3923 nil))))))
3924
3925 (> (point)
3926 (progn
3927 ;; Skip syntactic ws afterwards so that we don't stop at the
3928 ;; end of a comment if `skip-chars' is something like "^/".
3929 (c-backward-syntactic-ws)
3930 (point)))))
3931
3932 ;; We might want to extend this with more useful return values in
3933 ;; the future.
3934 (/= (point) start)))
3935
3936 ;; The following is an alternative implementation of
3937 ;; `c-syntactic-skip-backward' that uses backward movement to keep
3938 ;; track of the syntactic context. It turned out to be generally
3939 ;; slower than the one above which uses forward checks from earlier
3940 ;; safe positions.
3941 ;;
3942 ;;(defconst c-ssb-stop-re
3943 ;; ;; The regexp matching chars `c-syntactic-skip-backward' needs to
3944 ;; ;; stop at to avoid going into comments and literals.
3945 ;; (concat
3946 ;; ;; Match comment end syntax and string literal syntax. Also match
3947 ;; ;; '/' for block comment endings (not covered by comment end
3948 ;; ;; syntax).
3949 ;; "\\s>\\|/\\|\\s\""
3950 ;; (if (memq 'gen-string-delim c-emacs-features)
3951 ;; "\\|\\s|"
3952 ;; "")
3953 ;; (if (memq 'gen-comment-delim c-emacs-features)
3954 ;; "\\|\\s!"
3955 ;; "")))
3956 ;;
3957 ;;(defconst c-ssb-stop-paren-re
3958 ;; ;; Like `c-ssb-stop-re' but also stops at paren chars.
3959 ;; (concat c-ssb-stop-re "\\|\\s(\\|\\s)"))
3960 ;;
3961 ;;(defconst c-ssb-sexp-end-re
3962 ;; ;; Regexp matching the ending syntax of a complex sexp.
3963 ;; (concat c-string-limit-regexp "\\|\\s)"))
3964 ;;
3965 ;;(defun c-syntactic-skip-backward (skip-chars &optional limit paren-level)
3966 ;; "Like `skip-chars-backward' but only look at syntactically relevant chars,
3967 ;;i.e. don't stop at positions inside syntactic whitespace or string
3968 ;;literals. Preprocessor directives are also ignored. However, if the
3969 ;;point is within a comment, string literal or preprocessor directory to
3970 ;;begin with, its contents is treated as syntactically relevant chars.
3971 ;;If LIMIT is given, it limits the backward search and the point will be
3972 ;;left there if no earlier position is found.
3973 ;;
3974 ;;If PAREN-LEVEL is non-nil, the function won't stop in nested paren
3975 ;;sexps, and the search will also not go outside the current paren sexp.
3976 ;;However, if LIMIT or the buffer limit is reached inside a nested paren
3977 ;;then the point will be left at the limit.
3978 ;;
3979 ;;Non-nil is returned if the point moved, nil otherwise.
3980 ;;
3981 ;;Note that this function might do hidden buffer changes. See the
3982 ;;comment at the start of cc-engine.el for more info."
3983 ;;
3984 ;; (save-restriction
3985 ;; (when limit
3986 ;; (narrow-to-region limit (point-max)))
3987 ;;
3988 ;; (let ((start (point)))
3989 ;; (catch 'done
3990 ;; (while (let ((last-pos (point))
3991 ;; (stop-pos (progn
3992 ;; (skip-chars-backward skip-chars)
3993 ;; (point))))
3994 ;;
3995 ;; ;; Skip back over the same region as
3996 ;; ;; `skip-chars-backward' above, but keep to
3997 ;; ;; syntactically relevant positions.
3998 ;; (goto-char last-pos)
3999 ;; (while (and
4000 ;; ;; `re-search-backward' with a single char regexp
4001 ;; ;; should be fast.
4002 ;; (re-search-backward
4003 ;; (if paren-level c-ssb-stop-paren-re c-ssb-stop-re)
4004 ;; stop-pos 'move)
4005 ;;
4006 ;; (progn
4007 ;; (cond
4008 ;; ((looking-at "\\s(")
4009 ;; ;; `paren-level' is set and we've found the
4010 ;; ;; start of the containing paren.
4011 ;; (forward-char)
4012 ;; (throw 'done t))
4013 ;;
4014 ;; ((looking-at c-ssb-sexp-end-re)
4015 ;; ;; We're at the end of a string literal or paren
4016 ;; ;; sexp (if `paren-level' is set).
4017 ;; (forward-char)
4018 ;; (condition-case nil
4019 ;; (c-backward-sexp)
4020 ;; (error
4021 ;; (goto-char limit)
4022 ;; (throw 'done t))))
4023 ;;
4024 ;; (t
4025 ;; (forward-char)
4026 ;; ;; At the end of some syntactic ws or possibly
4027 ;; ;; after a plain '/' operator.
4028 ;; (let ((pos (point)))
4029 ;; (c-backward-syntactic-ws)
4030 ;; (if (= pos (point))
4031 ;; ;; Was a plain '/' operator. Go past it.
4032 ;; (backward-char)))))
4033 ;;
4034 ;; (> (point) stop-pos))))
4035 ;;
4036 ;; ;; Now the point is either at `stop-pos' or at some
4037 ;; ;; position further back if `stop-pos' was at a
4038 ;; ;; syntactically irrelevant place.
4039 ;;
4040 ;; ;; Skip additional syntactic ws so that we don't stop
4041 ;; ;; at the end of a comment if `skip-chars' is
4042 ;; ;; something like "^/".
4043 ;; (c-backward-syntactic-ws)
4044 ;;
4045 ;; (< (point) stop-pos))))
4046 ;;
4047 ;; ;; We might want to extend this with more useful return values
4048 ;; ;; in the future.
4049 ;; (/= (point) start))))
4050
4051 \f
4052 ;; Tools for handling comments and string literals.
4053
4054 (defun c-slow-in-literal (&optional lim detect-cpp)
4055 "Return the type of literal point is in, if any.
4056 The return value is `c' if in a C-style comment, `c++' if in a C++
4057 style comment, `string' if in a string literal, `pound' if DETECT-CPP
4058 is non-nil and in a preprocessor line, or nil if somewhere else.
4059 Optional LIM is used as the backward limit of the search. If omitted,
4060 or nil, `c-beginning-of-defun' is used.
4061
4062 The last point calculated is cached if the cache is enabled, i.e. if
4063 `c-in-literal-cache' is bound to a two element vector.
4064
4065 Note that this function might do hidden buffer changes. See the
4066 comment at the start of cc-engine.el for more info."
4067
4068 (if (and (vectorp c-in-literal-cache)
4069 (= (point) (aref c-in-literal-cache 0)))
4070 (aref c-in-literal-cache 1)
4071 (let ((rtn (save-excursion
4072 (let* ((pos (point))
4073 (lim (or lim (progn
4074 (c-beginning-of-syntax)
4075 (point))))
4076 (state (parse-partial-sexp lim pos)))
4077 (cond
4078 ((elt state 3) 'string)
4079 ((elt state 4) (if (elt state 7) 'c++ 'c))
4080 ((and detect-cpp (c-beginning-of-macro lim)) 'pound)
4081 (t nil))))))
4082 ;; cache this result if the cache is enabled
4083 (if (not c-in-literal-cache)
4084 (setq c-in-literal-cache (vector (point) rtn)))
4085 rtn)))
4086
4087 ;; XEmacs has a built-in function that should make this much quicker.
4088 ;; I don't think we even need the cache, which makes our lives more
4089 ;; complicated anyway. In this case, lim is only used to detect
4090 ;; cpp directives.
4091 ;;
4092 ;; Note that there is a bug in Xemacs's buffer-syntactic-context when used in
4093 ;; conjunction with syntax-table-properties. The bug is present in, e.g.,
4094 ;; Xemacs 21.4.4. It manifested itself thus:
4095 ;;
4096 ;; Starting with an empty AWK Mode buffer, type
4097 ;; /regexp/ {<C-j>
4098 ;; Point gets wrongly left at column 0, rather than being indented to tab-width.
4099 ;;
4100 ;; AWK Mode is designed such that when the first / is typed, it gets the
4101 ;; syntax-table property "string fence". When the second / is typed, BOTH /s
4102 ;; are given the s-t property "string". However, buffer-syntactic-context
4103 ;; fails to take account of the change of the s-t property on the opening / to
4104 ;; "string", and reports that the { is within a string started by the second /.
4105 ;;
4106 ;; The workaround for this is for the AWK Mode initialisation to switch the
4107 ;; defalias for c-in-literal to c-slow-in-literal. This will slow down other
4108 ;; cc-modes in Xemacs whenever an awk-buffer has been initialised.
4109 ;;
4110 ;; (Alan Mackenzie, 2003/4/30).
4111
4112 (defun c-fast-in-literal (&optional lim detect-cpp)
4113 ;; This function might do hidden buffer changes.
4114 (let ((context (buffer-syntactic-context)))
4115 (cond
4116 ((eq context 'string) 'string)
4117 ((eq context 'comment) 'c++)
4118 ((eq context 'block-comment) 'c)
4119 ((and detect-cpp (save-excursion (c-beginning-of-macro lim))) 'pound))))
4120
4121 (defalias 'c-in-literal
4122 (if (fboundp 'buffer-syntactic-context)
4123 'c-fast-in-literal ; XEmacs
4124 'c-slow-in-literal)) ; GNU Emacs
4125
4126 ;; The defalias above isn't enough to shut up the byte compiler.
4127 (cc-bytecomp-defun c-in-literal)
4128
4129 (defun c-literal-limits (&optional lim near not-in-delimiter)
4130 "Return a cons of the beginning and end positions of the comment or
4131 string surrounding point (including both delimiters), or nil if point
4132 isn't in one. If LIM is non-nil, it's used as the \"safe\" position
4133 to start parsing from. If NEAR is non-nil, then the limits of any
4134 literal next to point is returned. \"Next to\" means there's only
4135 spaces and tabs between point and the literal. The search for such a
4136 literal is done first in forward direction. If NOT-IN-DELIMITER is
4137 non-nil, the case when point is inside a starting delimiter won't be
4138 recognized. This only has effect for comments which have starting
4139 delimiters with more than one character.
4140
4141 Note that this function might do hidden buffer changes. See the
4142 comment at the start of cc-engine.el for more info."
4143
4144 (save-excursion
4145 (let* ((pos (point))
4146 (lim (or lim (progn
4147 (c-beginning-of-syntax)
4148 (point))))
4149 (state (parse-partial-sexp lim pos)))
4150
4151 (cond ((elt state 3) ; String.
4152 (goto-char (elt state 8))
4153 (cons (point) (or (c-safe (c-forward-sexp 1) (point))
4154 (point-max))))
4155
4156 ((elt state 4) ; Comment.
4157 (goto-char (elt state 8))
4158 (cons (point) (progn (c-forward-single-comment) (point))))
4159
4160 ((and (not not-in-delimiter)
4161 (not (elt state 5))
4162 (eq (char-before) ?/)
4163 (looking-at "[/*]"))
4164 ;; We're standing in a comment starter.
4165 (backward-char 1)
4166 (cons (point) (progn (c-forward-single-comment) (point))))
4167
4168 (near
4169 (goto-char pos)
4170
4171 ;; Search forward for a literal.
4172 (skip-chars-forward " \t")
4173
4174 (cond
4175 ((looking-at c-string-limit-regexp) ; String.
4176 (cons (point) (or (c-safe (c-forward-sexp 1) (point))
4177 (point-max))))
4178
4179 ((looking-at c-comment-start-regexp) ; Line or block comment.
4180 (cons (point) (progn (c-forward-single-comment) (point))))
4181
4182 (t
4183 ;; Search backward.
4184 (skip-chars-backward " \t")
4185
4186 (let ((end (point)) beg)
4187 (cond
4188 ((save-excursion
4189 (< (skip-syntax-backward c-string-syntax) 0)) ; String.
4190 (setq beg (c-safe (c-backward-sexp 1) (point))))
4191
4192 ((and (c-safe (forward-char -2) t)
4193 (looking-at "*/"))
4194 ;; Block comment. Due to the nature of line
4195 ;; comments, they will always be covered by the
4196 ;; normal case above.
4197 (goto-char end)
4198 (c-backward-single-comment)
4199 ;; If LIM is bogus, beg will be bogus.
4200 (setq beg (point))))
4201
4202 (if beg (cons beg end))))))
4203 ))))
4204
4205 ;; In case external callers use this; it did have a docstring.
4206 (defalias 'c-literal-limits-fast 'c-literal-limits)
4207
4208 (defun c-collect-line-comments (range)
4209 "If the argument is a cons of two buffer positions (such as returned by
4210 `c-literal-limits'), and that range contains a C++ style line comment,
4211 then an extended range is returned that contains all adjacent line
4212 comments (i.e. all comments that starts in the same column with no
4213 empty lines or non-whitespace characters between them). Otherwise the
4214 argument is returned.
4215
4216 Note that this function might do hidden buffer changes. See the
4217 comment at the start of cc-engine.el for more info."
4218
4219 (save-excursion
4220 (condition-case nil
4221 (if (and (consp range) (progn
4222 (goto-char (car range))
4223 (looking-at c-line-comment-starter)))
4224 (let ((col (current-column))
4225 (beg (point))
4226 (bopl (c-point 'bopl))
4227 (end (cdr range)))
4228 ;; Got to take care in the backward direction to handle
4229 ;; comments which are preceded by code.
4230 (while (and (c-backward-single-comment)
4231 (>= (point) bopl)
4232 (looking-at c-line-comment-starter)
4233 (= col (current-column)))
4234 (setq beg (point)
4235 bopl (c-point 'bopl)))
4236 (goto-char end)
4237 (while (and (progn (skip-chars-forward " \t")
4238 (looking-at c-line-comment-starter))
4239 (= col (current-column))
4240 (prog1 (zerop (forward-line 1))
4241 (setq end (point)))))
4242 (cons beg end))
4243 range)
4244 (error range))))
4245
4246 (defun c-literal-type (range)
4247 "Convenience function that given the result of `c-literal-limits',
4248 returns nil or the type of literal that the range surrounds, one
4249 of the symbols 'c, 'c++ or 'string. It's much faster than using
4250 `c-in-literal' and is intended to be used when you need both the
4251 type of a literal and its limits.
4252
4253 Note that this function might do hidden buffer changes. See the
4254 comment at the start of cc-engine.el for more info."
4255
4256 (if (consp range)
4257 (save-excursion
4258 (goto-char (car range))
4259 (cond ((looking-at c-string-limit-regexp) 'string)
4260 ((or (looking-at "//") ; c++ line comment
4261 (and (looking-at "\\s<") ; comment starter
4262 (looking-at "#"))) ; awk comment.
4263 'c++)
4264 (t 'c))) ; Assuming the range is valid.
4265 range))
4266
4267 \f
4268 ;; `c-find-decl-spots' and accompanying stuff.
4269
4270 ;; Variables used in `c-find-decl-spots' to cache the search done for
4271 ;; the first declaration in the last call. When that function starts,
4272 ;; it needs to back up over syntactic whitespace to look at the last
4273 ;; token before the region being searched. That can sometimes cause
4274 ;; moves back and forth over a quite large region of comments and
4275 ;; macros, which would be repeated for each changed character when
4276 ;; we're called during fontification, since font-lock refontifies the
4277 ;; current line for each change. Thus it's worthwhile to cache the
4278 ;; first match.
4279 ;;
4280 ;; `c-find-decl-syntactic-pos' is a syntactically relevant position in
4281 ;; the syntactic whitespace less or equal to some start position.
4282 ;; There's no cached value if it's nil.
4283 ;;
4284 ;; `c-find-decl-match-pos' is the match position if
4285 ;; `c-find-decl-prefix-search' matched before the syntactic whitespace
4286 ;; at `c-find-decl-syntactic-pos', or nil if there's no such match.
4287 (defvar c-find-decl-syntactic-pos nil)
4288 (make-variable-buffer-local 'c-find-decl-syntactic-pos)
4289 (defvar c-find-decl-match-pos nil)
4290 (make-variable-buffer-local 'c-find-decl-match-pos)
4291
4292 (defsubst c-invalidate-find-decl-cache (change-min-pos)
4293 (and c-find-decl-syntactic-pos
4294 (< change-min-pos c-find-decl-syntactic-pos)
4295 (setq c-find-decl-syntactic-pos nil)))
4296
4297 ; (defface c-debug-decl-spot-face
4298 ; '((t (:background "Turquoise")))
4299 ; "Debug face to mark the spots where `c-find-decl-spots' stopped.")
4300 ; (defface c-debug-decl-sws-face
4301 ; '((t (:background "Khaki")))
4302 ; "Debug face to mark the syntactic whitespace between the declaration
4303 ; spots and the preceding token end.")
4304
4305 (defmacro c-debug-put-decl-spot-faces (match-pos decl-pos)
4306 (when (facep 'c-debug-decl-spot-face)
4307 `(c-save-buffer-state ((match-pos ,match-pos) (decl-pos ,decl-pos))
4308 (c-debug-add-face (max match-pos (point-min)) decl-pos
4309 'c-debug-decl-sws-face)
4310 (c-debug-add-face decl-pos (min (1+ decl-pos) (point-max))
4311 'c-debug-decl-spot-face))))
4312 (defmacro c-debug-remove-decl-spot-faces (beg end)
4313 (when (facep 'c-debug-decl-spot-face)
4314 `(c-save-buffer-state ()
4315 (c-debug-remove-face ,beg ,end 'c-debug-decl-spot-face)
4316 (c-debug-remove-face ,beg ,end 'c-debug-decl-sws-face))))
4317
4318 (defmacro c-find-decl-prefix-search ()
4319 ;; Macro used inside `c-find-decl-spots'. It ought to be a defun,
4320 ;; but it contains lots of free variables that refer to things
4321 ;; inside `c-find-decl-spots'. The point is left at `cfd-match-pos'
4322 ;; if there is a match, otherwise at `cfd-limit'.
4323 ;;
4324 ;; This macro might do hidden buffer changes.
4325
4326 '(progn
4327 ;; Find the next property match position if we haven't got one already.
4328 (unless cfd-prop-match
4329 (save-excursion
4330 (while (progn
4331 (goto-char (next-single-property-change
4332 (point) 'c-type nil cfd-limit))
4333 (and (< (point) cfd-limit)
4334 (not (eq (c-get-char-property (1- (point)) 'c-type)
4335 'c-decl-end)))))
4336 (setq cfd-prop-match (point))))
4337
4338 ;; Find the next `c-decl-prefix-or-start-re' match if we haven't
4339 ;; got one already.
4340 (unless cfd-re-match
4341
4342 (if (> cfd-re-match-end (point))
4343 (goto-char cfd-re-match-end))
4344
4345 (while (if (setq cfd-re-match-end
4346 (re-search-forward c-decl-prefix-or-start-re
4347 cfd-limit 'move))
4348
4349 ;; Match. Check if it's inside a comment or string literal.
4350 (c-got-face-at
4351 (if (setq cfd-re-match (match-end 1))
4352 ;; Matched the end of a token preceding a decl spot.
4353 (progn
4354 (goto-char cfd-re-match)
4355 (1- cfd-re-match))
4356 ;; Matched a token that start a decl spot.
4357 (goto-char (match-beginning 0))
4358 (point))
4359 c-literal-faces)
4360
4361 ;; No match. Finish up and exit the loop.
4362 (setq cfd-re-match cfd-limit)
4363 nil)
4364
4365 ;; Skip out of comments and string literals.
4366 (while (progn
4367 (goto-char (next-single-property-change
4368 (point) 'face nil cfd-limit))
4369 (and (< (point) cfd-limit)
4370 (c-got-face-at (point) c-literal-faces)))))
4371
4372 ;; If we matched at the decl start, we have to back up over the
4373 ;; preceding syntactic ws to set `cfd-match-pos' and to catch
4374 ;; any decl spots in the syntactic ws.
4375 (unless cfd-re-match
4376 (c-backward-syntactic-ws)
4377 (setq cfd-re-match (point))))
4378
4379 ;; Choose whichever match is closer to the start.
4380 (if (< cfd-re-match cfd-prop-match)
4381 (setq cfd-match-pos cfd-re-match
4382 cfd-re-match nil)
4383 (setq cfd-match-pos cfd-prop-match
4384 cfd-prop-match nil))
4385
4386 (goto-char cfd-match-pos)
4387
4388 (when (< cfd-match-pos cfd-limit)
4389 ;; Skip forward past comments only so we don't skip macros.
4390 (c-forward-comments)
4391 ;; Set the position to continue at. We can avoid going over
4392 ;; the comments skipped above a second time, but it's possible
4393 ;; that the comment skipping has taken us past `cfd-prop-match'
4394 ;; since the property might be used inside comments.
4395 (setq cfd-continue-pos (if cfd-prop-match
4396 (min cfd-prop-match (point))
4397 (point))))))
4398
4399 (defun c-find-decl-spots (cfd-limit cfd-decl-re cfd-face-checklist cfd-fun)
4400 ;; Call CFD-FUN for each possible spot for a declaration, cast or
4401 ;; label from the point to CFD-LIMIT.
4402 ;;
4403 ;; CFD-FUN is called with point at the start of the spot. It's
4404 ;; passed two arguments: The first is the end position of the token
4405 ;; preceding the spot, or 0 for the implicit match at bob. The
4406 ;; second is a flag that is t when the match is inside a macro. If
4407 ;; CFD-FUN adds `c-decl-end' properties somewhere below the current
4408 ;; spot, it should return non-nil to ensure that the next search
4409 ;; will find them.
4410 ;;
4411 ;; Such a spot is:
4412 ;; o The first token after bob.
4413 ;; o The first token after the end of submatch 1 in
4414 ;; `c-decl-prefix-or-start-re' when that submatch matches.
4415 ;; o The start of each `c-decl-prefix-or-start-re' match when
4416 ;; submatch 1 doesn't match.
4417 ;; o The first token after the end of each occurrence of the
4418 ;; `c-type' text property with the value `c-decl-end', provided
4419 ;; `c-type-decl-end-used' is set.
4420 ;;
4421 ;; Only a spot that match CFD-DECL-RE and whose face is in the
4422 ;; CFD-FACE-CHECKLIST list causes CFD-FUN to be called. The face
4423 ;; check is disabled if CFD-FACE-CHECKLIST is nil.
4424 ;;
4425 ;; If the match is inside a macro then the buffer is narrowed to the
4426 ;; end of it, so that CFD-FUN can investigate the following tokens
4427 ;; without matching something that begins inside a macro and ends
4428 ;; outside it. It's to avoid this work that the CFD-DECL-RE and
4429 ;; CFD-FACE-CHECKLIST checks exist.
4430 ;;
4431 ;; The spots are visited approximately in order from top to bottom.
4432 ;; It's however the positions where `c-decl-prefix-or-start-re'
4433 ;; matches and where `c-decl-end' properties are found that are in
4434 ;; order. Since the spots often are at the following token, they
4435 ;; might be visited out of order insofar as more spots are reported
4436 ;; later on within the syntactic whitespace between the match
4437 ;; positions and their spots.
4438 ;;
4439 ;; It's assumed that comments and strings are fontified in the
4440 ;; searched range.
4441 ;;
4442 ;; This is mainly used in fontification, and so has an elaborate
4443 ;; cache to handle repeated calls from the same start position; see
4444 ;; the variables above.
4445 ;;
4446 ;; All variables in this function begin with `cfd-' to avoid name
4447 ;; collision with the (dynamically bound) variables used in CFD-FUN.
4448 ;;
4449 ;; This function might do hidden buffer changes.
4450
4451 (let ((cfd-start-pos (point))
4452 (cfd-buffer-end (point-max))
4453 ;; The end of the token preceding the decl spot last found
4454 ;; with `c-decl-prefix-or-start-re'. `cfd-limit' if there's
4455 ;; no match.
4456 cfd-re-match
4457 ;; The end position of the last `c-decl-prefix-or-start-re'
4458 ;; match. If this is greater than `cfd-continue-pos', the
4459 ;; next regexp search is started here instead.
4460 (cfd-re-match-end (point-min))
4461 ;; The end of the last `c-decl-end' found by
4462 ;; `c-find-decl-prefix-search'. `cfd-limit' if there's no
4463 ;; match. If searching for the property isn't needed then we
4464 ;; disable it by setting it to `cfd-limit' directly.
4465 (cfd-prop-match (unless c-type-decl-end-used cfd-limit))
4466 ;; The end of the token preceding the decl spot last found by
4467 ;; `c-find-decl-prefix-search'. 0 for the implicit match at
4468 ;; bob. `cfd-limit' if there's no match. In other words,
4469 ;; this is the minimum of `cfd-re-match' and `cfd-prop-match'.
4470 (cfd-match-pos cfd-limit)
4471 ;; The position to continue searching at.
4472 cfd-continue-pos
4473 ;; The position of the last "real" token we've stopped at.
4474 ;; This can be greater than `cfd-continue-pos' when we get
4475 ;; hits inside macros or at `c-decl-end' positions inside
4476 ;; comments.
4477 (cfd-token-pos 0)
4478 ;; The end position of the last entered macro.
4479 (cfd-macro-end 0))
4480
4481 ;; Initialize by finding a syntactically relevant start position
4482 ;; before the point, and do the first `c-decl-prefix-or-start-re'
4483 ;; search unless we're at bob.
4484
4485 (let (start-in-literal start-in-macro syntactic-pos)
4486 ;; Must back up a bit since we look for the end of the previous
4487 ;; statement or declaration, which is earlier than the first
4488 ;; returned match.
4489
4490 (cond
4491 ;; First we need to move to a syntactically relevant position.
4492 ;; Begin by backing out of comment or string literals.
4493 ((and
4494 (when (c-got-face-at (point) c-literal-faces)
4495 ;; Try to use the faces to back up to the start of the
4496 ;; literal. FIXME: What if the point is on a declaration
4497 ;; inside a comment?
4498 (while (and (not (bobp))
4499 (c-got-face-at (1- (point)) c-literal-faces))
4500 (goto-char (previous-single-property-change
4501 (point) 'face nil (point-min))))
4502
4503 ;; XEmacs doesn't fontify the quotes surrounding string
4504 ;; literals.
4505 (and (featurep 'xemacs)
4506 (eq (get-text-property (point) 'face)
4507 'font-lock-string-face)
4508 (not (bobp))
4509 (progn (backward-char)
4510 (not (looking-at c-string-limit-regexp)))
4511 (forward-char))
4512
4513 ;; Don't trust the literal to contain only literal faces
4514 ;; (the font lock package might not have fontified the
4515 ;; start of it at all, for instance) so check that we have
4516 ;; arrived at something that looks like a start or else
4517 ;; resort to `c-literal-limits'.
4518 (unless (looking-at c-literal-start-regexp)
4519 (let ((range (c-literal-limits)))
4520 (if range (goto-char (car range)))))
4521
4522 (setq start-in-literal (point)))
4523
4524 ;; The start is in a literal. If the limit is in the same
4525 ;; one we don't have to find a syntactic position etc. We
4526 ;; only check that if the limit is at or before bonl to save
4527 ;; time; it covers the by far most common case when font-lock
4528 ;; refontifies the current line only.
4529 (<= cfd-limit (c-point 'bonl cfd-start-pos))
4530 (save-excursion
4531 (goto-char cfd-start-pos)
4532 (while (progn
4533 (goto-char (next-single-property-change
4534 (point) 'face nil cfd-limit))
4535 (and (< (point) cfd-limit)
4536 (c-got-face-at (point) c-literal-faces))))
4537 (= (point) cfd-limit)))
4538
4539 ;; Completely inside a literal. Set up variables to trig the
4540 ;; (< cfd-continue-pos cfd-start-pos) case below and it'll
4541 ;; find a suitable start position.
4542 (setq cfd-continue-pos start-in-literal))
4543
4544 ;; Check if the region might be completely inside a macro, to
4545 ;; optimize that like the completely-inside-literal above.
4546 ((save-excursion
4547 (and (= (forward-line 1) 0)
4548 (bolp) ; forward-line has funny behavior at eob.
4549 (>= (point) cfd-limit)
4550 (progn (backward-char)
4551 (eq (char-before) ?\\))))
4552 ;; (Maybe) completely inside a macro. Only need to trig the
4553 ;; (< cfd-continue-pos cfd-start-pos) case below to make it
4554 ;; set things up.
4555 (setq cfd-continue-pos (1- cfd-start-pos)
4556 start-in-macro t))
4557
4558 (t
4559 ;; Back out of any macro so we don't miss any declaration
4560 ;; that could follow after it.
4561 (when (c-beginning-of-macro)
4562 (setq start-in-macro t))
4563
4564 ;; Now we're at a proper syntactically relevant position so we
4565 ;; can use the cache. But first clear it if it applied
4566 ;; further down.
4567 (c-invalidate-find-decl-cache cfd-start-pos)
4568
4569 (setq syntactic-pos (point))
4570 (unless (eq syntactic-pos c-find-decl-syntactic-pos)
4571 ;; Don't have to do this if the cache is relevant here,
4572 ;; typically if the same line is refontified again. If
4573 ;; we're just some syntactic whitespace further down we can
4574 ;; still use the cache to limit the skipping.
4575 (c-backward-syntactic-ws c-find-decl-syntactic-pos))
4576
4577 ;; If we hit `c-find-decl-syntactic-pos' and
4578 ;; `c-find-decl-match-pos' is set then we install the cached
4579 ;; values. If we hit `c-find-decl-syntactic-pos' and
4580 ;; `c-find-decl-match-pos' is nil then we know there's no decl
4581 ;; prefix in the whitespace before `c-find-decl-syntactic-pos'
4582 ;; and so we can continue the search from this point. If we
4583 ;; didn't hit `c-find-decl-syntactic-pos' then we're now in
4584 ;; the right spot to begin searching anyway.
4585 (if (and (eq (point) c-find-decl-syntactic-pos)
4586 c-find-decl-match-pos)
4587 (setq cfd-match-pos c-find-decl-match-pos
4588 cfd-continue-pos syntactic-pos)
4589
4590 (setq c-find-decl-syntactic-pos syntactic-pos)
4591
4592 (when (if (bobp)
4593 ;; Always consider bob a match to get the first
4594 ;; declaration in the file. Do this separately instead of
4595 ;; letting `c-decl-prefix-or-start-re' match bob, so that
4596 ;; regexp always can consume at least one character to
4597 ;; ensure that we won't get stuck in an infinite loop.
4598 (setq cfd-re-match 0)
4599 (backward-char)
4600 (c-beginning-of-current-token)
4601 (< (point) cfd-limit))
4602 ;; Do an initial search now. In the bob case above it's
4603 ;; only done to search for a `c-decl-end' spot.
4604 (c-find-decl-prefix-search))
4605
4606 (setq c-find-decl-match-pos (and (< cfd-match-pos cfd-start-pos)
4607 cfd-match-pos)))))
4608
4609 ;; Advance `cfd-continue-pos' if it's before the start position.
4610 ;; The closest continue position that might have effect at or
4611 ;; after the start depends on what we started in. This also
4612 ;; finds a suitable start position in the special cases when the
4613 ;; region is completely within a literal or macro.
4614 (when (and cfd-continue-pos (< cfd-continue-pos cfd-start-pos))
4615
4616 (cond
4617 (start-in-macro
4618 ;; If we're in a macro then it's the closest preceding token
4619 ;; in the macro. Check this before `start-in-literal',
4620 ;; since if we're inside a literal in a macro, the preceding
4621 ;; token is earlier than any `c-decl-end' spot inside the
4622 ;; literal (comment).
4623 (goto-char (or start-in-literal cfd-start-pos))
4624 ;; The only syntactic ws in macros are comments.
4625 (c-backward-comments)
4626 (backward-char)
4627 (c-beginning-of-current-token))
4628
4629 (start-in-literal
4630 ;; If we're in a comment it can only be the closest
4631 ;; preceding `c-decl-end' position within that comment, if
4632 ;; any. Go back to the beginning of such a property so that
4633 ;; `c-find-decl-prefix-search' will find the end of it.
4634 ;; (Can't stop at the end and install it directly on
4635 ;; `cfd-prop-match' since that variable might be cleared
4636 ;; after `cfd-fun' below.)
4637 ;;
4638 ;; Note that if the literal is a string then the property
4639 ;; search will simply skip to the beginning of it right
4640 ;; away.
4641 (if (not c-type-decl-end-used)
4642 (goto-char start-in-literal)
4643 (goto-char cfd-start-pos)
4644 (while (progn
4645 (goto-char (previous-single-property-change
4646 (point) 'c-type nil start-in-literal))
4647 (and (> (point) start-in-literal)
4648 (not (eq (c-get-char-property (point) 'c-type)
4649 'c-decl-end))))))
4650
4651 (when (= (point) start-in-literal)
4652 ;; Didn't find any property inside the comment, so we can
4653 ;; skip it entirely. (This won't skip past a string, but
4654 ;; that'll be handled quickly by the next
4655 ;; `c-find-decl-prefix-search' anyway.)
4656 (c-forward-single-comment)
4657 (if (> (point) cfd-limit)
4658 (goto-char cfd-limit))))
4659
4660 (t
4661 ;; If we started in normal code, the only match that might
4662 ;; apply before the start is what we already got in
4663 ;; `cfd-match-pos' so we can continue at the start position.
4664 ;; (Note that we don't get here if the first match is below
4665 ;; it.)
4666 (goto-char cfd-start-pos)))
4667
4668 ;; Delete found matches if they are before our new continue
4669 ;; position, so that `c-find-decl-prefix-search' won't back up
4670 ;; to them later on.
4671 (setq cfd-continue-pos (point))
4672 (when (and cfd-re-match (< cfd-re-match cfd-continue-pos))
4673 (setq cfd-re-match nil))
4674 (when (and cfd-prop-match (< cfd-prop-match cfd-continue-pos))
4675 (setq cfd-prop-match nil)))
4676
4677 (if syntactic-pos
4678 ;; This is the normal case and we got a proper syntactic
4679 ;; position. If there's a match then it's always outside
4680 ;; macros and comments, so advance to the next token and set
4681 ;; `cfd-token-pos'. The loop below will later go back using
4682 ;; `cfd-continue-pos' to fix declarations inside the
4683 ;; syntactic ws.
4684 (when (and cfd-match-pos (< cfd-match-pos syntactic-pos))
4685 (goto-char syntactic-pos)
4686 (c-forward-syntactic-ws)
4687 (and cfd-continue-pos
4688 (< cfd-continue-pos (point))
4689 (setq cfd-token-pos (point))))
4690
4691 ;; Have one of the special cases when the region is completely
4692 ;; within a literal or macro. `cfd-continue-pos' is set to a
4693 ;; good start position for the search, so do it.
4694 (c-find-decl-prefix-search)))
4695
4696 ;; Now loop. Round what? (ACM, 2006/7/5). We already got the first match.
4697
4698 (while (progn
4699 (while (and
4700 (< cfd-match-pos cfd-limit)
4701
4702 (or
4703 ;; Kludge to filter out matches on the "<" that
4704 ;; aren't open parens, for the sake of languages
4705 ;; that got `c-recognize-<>-arglists' set.
4706 (and (eq (char-before cfd-match-pos) ?<)
4707 (not (c-get-char-property (1- cfd-match-pos)
4708 'syntax-table)))
4709
4710 ;; If `cfd-continue-pos' is less or equal to
4711 ;; `cfd-token-pos', we've got a hit inside a macro
4712 ;; that's in the syntactic whitespace before the last
4713 ;; "real" declaration we've checked. If they're equal
4714 ;; we've arrived at the declaration a second time, so
4715 ;; there's nothing to do.
4716 (= cfd-continue-pos cfd-token-pos)
4717
4718 (progn
4719 ;; If `cfd-continue-pos' is less than `cfd-token-pos'
4720 ;; we're still searching for declarations embedded in
4721 ;; the syntactic whitespace. In that case we need
4722 ;; only to skip comments and not macros, since they
4723 ;; can't be nested, and that's already been done in
4724 ;; `c-find-decl-prefix-search'.
4725 (when (> cfd-continue-pos cfd-token-pos)
4726 (c-forward-syntactic-ws)
4727 (setq cfd-token-pos (point)))
4728
4729 ;; Continue if the following token fails the
4730 ;; CFD-DECL-RE and CFD-FACE-CHECKLIST checks.
4731 (when (or (>= (point) cfd-limit)
4732 (not (looking-at cfd-decl-re))
4733 (and cfd-face-checklist
4734 (not (c-got-face-at
4735 (point) cfd-face-checklist))))
4736 (goto-char cfd-continue-pos)
4737 t)))
4738
4739 (< (point) cfd-limit))
4740 (c-find-decl-prefix-search))
4741
4742 (< (point) cfd-limit))
4743
4744 (when (and
4745 (>= (point) cfd-start-pos)
4746
4747 (progn
4748 ;; Narrow to the end of the macro if we got a hit inside
4749 ;; one, to avoid recognizing things that start inside the
4750 ;; macro and end outside it.
4751 (when (> cfd-match-pos cfd-macro-end)
4752 ;; Not in the same macro as in the previous round.
4753 (save-excursion
4754 (goto-char cfd-match-pos)
4755 (setq cfd-macro-end
4756 (if (save-excursion (and (c-beginning-of-macro)
4757 (< (point) cfd-match-pos)))
4758 (progn (c-end-of-macro)
4759 (point))
4760 0))))
4761
4762 (if (zerop cfd-macro-end)
4763 t
4764 (if (> cfd-macro-end (point))
4765 (progn (narrow-to-region (point-min) cfd-macro-end)
4766 t)
4767 ;; The matched token was the last thing in the macro,
4768 ;; so the whole match is bogus.
4769 (setq cfd-macro-end 0)
4770 nil))))
4771
4772 (c-debug-put-decl-spot-faces cfd-match-pos (point))
4773 (if (funcall cfd-fun cfd-match-pos (/= cfd-macro-end 0))
4774 (setq cfd-prop-match nil))
4775
4776 (when (/= cfd-macro-end 0)
4777 ;; Restore limits if we did macro narrowment above.
4778 (narrow-to-region (point-min) cfd-buffer-end)))
4779
4780 (goto-char cfd-continue-pos)
4781 (if (= cfd-continue-pos cfd-limit)
4782 (setq cfd-match-pos cfd-limit)
4783 (c-find-decl-prefix-search)))))
4784
4785 \f
4786 ;; A cache for found types.
4787
4788 ;; Buffer local variable that contains an obarray with the types we've
4789 ;; found. If a declaration is recognized somewhere we record the
4790 ;; fully qualified identifier in it to recognize it as a type
4791 ;; elsewhere in the file too. This is not accurate since we do not
4792 ;; bother with the scoping rules of the languages, but in practice the
4793 ;; same name is seldom used as both a type and something else in a
4794 ;; file, and we only use this as a last resort in ambiguous cases (see
4795 ;; `c-forward-decl-or-cast-1').
4796 ;;
4797 ;; Not every type need be in this cache. However, things which have
4798 ;; ceased to be types must be removed from it.
4799 ;;
4800 ;; Template types in C++ are added here too but with the template
4801 ;; arglist replaced with "<>" in references or "<" for the one in the
4802 ;; primary type. E.g. the type "Foo<A,B>::Bar<C>" is stored as
4803 ;; "Foo<>::Bar<". This avoids storing very long strings (since C++
4804 ;; template specs can be fairly sized programs in themselves) and
4805 ;; improves the hit ratio (it's a type regardless of the template
4806 ;; args; it's just not the same type, but we're only interested in
4807 ;; recognizing types, not telling distinct types apart). Note that
4808 ;; template types in references are added here too; from the example
4809 ;; above there will also be an entry "Foo<".
4810 (defvar c-found-types nil)
4811 (make-variable-buffer-local 'c-found-types)
4812
4813 (defsubst c-clear-found-types ()
4814 ;; Clears `c-found-types'.
4815 (setq c-found-types (make-vector 53 0)))
4816
4817 (defun c-add-type (from to)
4818 ;; Add the given region as a type in `c-found-types'. If the region
4819 ;; doesn't match an existing type but there is a type which is equal
4820 ;; to the given one except that the last character is missing, then
4821 ;; the shorter type is removed. That's done to avoid adding all
4822 ;; prefixes of a type as it's being entered and font locked. This
4823 ;; doesn't cover cases like when characters are removed from a type
4824 ;; or added in the middle. We'd need the position of point when the
4825 ;; font locking is invoked to solve this well.
4826 ;;
4827 ;; This function might do hidden buffer changes.
4828 (let ((type (c-syntactic-content from to c-recognize-<>-arglists)))
4829 (unless (intern-soft type c-found-types)
4830 (unintern (substring type 0 -1) c-found-types)
4831 (intern type c-found-types))))
4832
4833 (defun c-unfind-type (name)
4834 ;; Remove the "NAME" from c-found-types, if present.
4835 (unintern name c-found-types))
4836
4837 (defsubst c-check-type (from to)
4838 ;; Return non-nil if the given region contains a type in
4839 ;; `c-found-types'.
4840 ;;
4841 ;; This function might do hidden buffer changes.
4842 (intern-soft (c-syntactic-content from to c-recognize-<>-arglists)
4843 c-found-types))
4844
4845 (defun c-list-found-types ()
4846 ;; Return all the types in `c-found-types' as a sorted list of
4847 ;; strings.
4848 (let (type-list)
4849 (mapatoms (lambda (type)
4850 (setq type-list (cons (symbol-name type)
4851 type-list)))
4852 c-found-types)
4853 (sort type-list 'string-lessp)))
4854
4855 ;; Shut up the byte compiler.
4856 (defvar c-maybe-stale-found-type)
4857
4858 (defun c-trim-found-types (beg end old-len)
4859 ;; An after change function which, in conjunction with the info in
4860 ;; c-maybe-stale-found-type (set in c-before-change), removes a type
4861 ;; from `c-found-types', should this type have become stale. For
4862 ;; example, this happens to "foo" when "foo \n bar();" becomes
4863 ;; "foo(); \n bar();". Such stale types, if not removed, foul up
4864 ;; the fontification.
4865 ;;
4866 ;; Have we, perhaps, added non-ws characters to the front/back of a found
4867 ;; type?
4868 (when (> end beg)
4869 (save-excursion
4870 (when (< end (point-max))
4871 (goto-char end)
4872 (if (and (c-beginning-of-current-token) ; only moves when we started in the middle
4873 (progn (goto-char end)
4874 (c-end-of-current-token)))
4875 (c-unfind-type (buffer-substring-no-properties
4876 end (point)))))
4877 (when (> beg (point-min))
4878 (goto-char beg)
4879 (if (and (c-end-of-current-token) ; only moves when we started in the middle
4880 (progn (goto-char beg)
4881 (c-beginning-of-current-token)))
4882 (c-unfind-type (buffer-substring-no-properties
4883 (point) beg))))))
4884
4885 (if c-maybe-stale-found-type ; e.g. (c-decl-id-start "foo" 97 107 " (* ooka) " "o")
4886 (cond
4887 ;; Changing the amount of (already existing) whitespace - don't do anything.
4888 ((and (c-partial-ws-p beg end)
4889 (or (= beg end) ; removal of WS
4890 (string-match "^[ \t\n\r\f\v]*$" (nth 5 c-maybe-stale-found-type)))))
4891
4892 ;; The syntactic relationship which defined a "found type" has been
4893 ;; destroyed.
4894 ((eq (car c-maybe-stale-found-type) 'c-decl-id-start)
4895 (c-unfind-type (cadr c-maybe-stale-found-type)))
4896 ;; ((eq (car c-maybe-stale-found-type) 'c-decl-type-start) FIXME!!!
4897 )))
4898
4899 \f
4900 ;; Setting and removing syntax properties on < and > in languages (C++
4901 ;; and Java) where they can be template/generic delimiters as well as
4902 ;; their normal meaning of "less/greater than".
4903
4904 ;; Normally, < and > have syntax 'punctuation'. When they are found to
4905 ;; be delimiters, they are marked as such with the category properties
4906 ;; c-<-as-paren-syntax, c->-as-paren-syntax respectively.
4907
4908 ;; STRATEGY:
4909 ;;
4910 ;; It is impossible to determine with certainty whether a <..> pair in
4911 ;; C++ is two comparison operators or is template delimiters, unless
4912 ;; one duplicates a lot of a C++ compiler. For example, the following
4913 ;; code fragment:
4914 ;;
4915 ;; foo (a < b, c > d) ;
4916 ;;
4917 ;; could be a function call with two integer parameters (each a
4918 ;; relational expression), or it could be a constructor for class foo
4919 ;; taking one parameter d of templated type "a < b, c >". They are
4920 ;; somewhat easier to distinguish in Java.
4921 ;;
4922 ;; The strategy now (2010-01) adopted is to mark and unmark < and
4923 ;; > IN MATCHING PAIRS ONLY. [Previously, they were marked
4924 ;; individually when their context so indicated. This gave rise to
4925 ;; intractible problems when one of a matching pair was deleted, or
4926 ;; pulled into a literal.]
4927 ;;
4928 ;; At each buffer change, the syntax-table properties are removed in a
4929 ;; before-change function and reapplied, when needed, in an
4930 ;; after-change function. It is far more important that the
4931 ;; properties get removed when they they are spurious than that they
4932 ;; be present when wanted.
4933 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
4934 (defun c-clear-<-pair-props (&optional pos)
4935 ;; POS (default point) is at a < character. If it is marked with
4936 ;; open paren syntax-table text property, remove the property,
4937 ;; together with the close paren property on the matching > (if
4938 ;; any).
4939 (save-excursion
4940 (if pos
4941 (goto-char pos)
4942 (setq pos (point)))
4943 (when (equal (c-get-char-property (point) 'syntax-table)
4944 c-<-as-paren-syntax)
4945 (with-syntax-table c-no-parens-syntax-table ; ignore unbalanced [,{,(,..
4946 (c-go-list-forward))
4947 (when (equal (c-get-char-property (1- (point)) 'syntax-table)
4948 c->-as-paren-syntax) ; should always be true.
4949 (c-clear-char-property (1- (point)) 'category))
4950 (c-clear-char-property pos 'category))))
4951
4952 (defun c-clear->-pair-props (&optional pos)
4953 ;; POS (default point) is at a > character. If it is marked with
4954 ;; close paren syntax-table property, remove the property, together
4955 ;; with the open paren property on the matching < (if any).
4956 (save-excursion
4957 (if pos
4958 (goto-char pos)
4959 (setq pos (point)))
4960 (when (equal (c-get-char-property (point) 'syntax-table)
4961 c->-as-paren-syntax)
4962 (with-syntax-table c-no-parens-syntax-table ; ignore unbalanced [,{,(,..
4963 (c-go-up-list-backward))
4964 (when (equal (c-get-char-property (point) 'syntax-table)
4965 c-<-as-paren-syntax) ; should always be true.
4966 (c-clear-char-property (point) 'category))
4967 (c-clear-char-property pos 'category))))
4968
4969 (defun c-clear-<>-pair-props (&optional pos)
4970 ;; POS (default point) is at a < or > character. If it has an
4971 ;; open/close paren syntax-table property, remove this property both
4972 ;; from the current character and its partner (which will also be
4973 ;; thusly marked).
4974 (cond
4975 ((eq (char-after) ?\<)
4976 (c-clear-<-pair-props pos))
4977 ((eq (char-after) ?\>)
4978 (c-clear->-pair-props pos))
4979 (t (c-benign-error
4980 "c-clear-<>-pair-props called from wrong position"))))
4981
4982 (defun c-clear-<-pair-props-if-match-after (lim &optional pos)
4983 ;; POS (default point) is at a < character. If it is both marked
4984 ;; with open/close paren syntax-table property, and has a matching >
4985 ;; (also marked) which is after LIM, remove the property both from
4986 ;; the current > and its partner. Return t when this happens, nil
4987 ;; when it doesn't.
4988 (save-excursion
4989 (if pos
4990 (goto-char pos)
4991 (setq pos (point)))
4992 (when (equal (c-get-char-property (point) 'syntax-table)
4993 c-<-as-paren-syntax)
4994 (with-syntax-table c-no-parens-syntax-table ; ignore unbalanced [,{,(,..
4995 (c-go-list-forward))
4996 (when (and (>= (point) lim)
4997 (equal (c-get-char-property (1- (point)) 'syntax-table)
4998 c->-as-paren-syntax)) ; should always be true.
4999 (c-unmark-<->-as-paren (1- (point)))
5000 (c-unmark-<->-as-paren pos))
5001 t)))
5002
5003 (defun c-clear->-pair-props-if-match-before (lim &optional pos)
5004 ;; POS (default point) is at a > character. If it is both marked
5005 ;; with open/close paren syntax-table property, and has a matching <
5006 ;; (also marked) which is before LIM, remove the property both from
5007 ;; the current < and its partner. Return t when this happens, nil
5008 ;; when it doesn't.
5009 (save-excursion
5010 (if pos
5011 (goto-char pos)
5012 (setq pos (point)))
5013 (when (equal (c-get-char-property (point) 'syntax-table)
5014 c->-as-paren-syntax)
5015 (with-syntax-table c-no-parens-syntax-table ; ignore unbalanced [,{,(,..
5016 (c-go-up-list-backward))
5017 (when (and (<= (point) lim)
5018 (equal (c-get-char-property (point) 'syntax-table)
5019 c-<-as-paren-syntax)) ; should always be true.
5020 (c-unmark-<->-as-paren (point))
5021 (c-unmark-<->-as-paren pos))
5022 t)))
5023
5024 ;; Set by c-common-init in cc-mode.el.
5025 (defvar c-new-BEG)
5026 (defvar c-new-END)
5027
5028 (defun c-before-change-check-<>-operators (beg end)
5029 ;; Unmark certain pairs of "< .... >" which are currently marked as
5030 ;; template/generic delimiters. (This marking is via syntax-table
5031 ;; text properties).
5032 ;;
5033 ;; These pairs are those which are in the current "statement" (i.e.,
5034 ;; the region between the {, }, or ; before BEG and the one after
5035 ;; END), and which enclose any part of the interval (BEG END).
5036 ;;
5037 ;; Note that in C++ (?and Java), template/generic parens cannot
5038 ;; enclose a brace or semicolon, so we use these as bounds on the
5039 ;; region we must work on.
5040 ;;
5041 ;; This function is called from before-change-functions (via
5042 ;; c-get-state-before-change-functions). Thus the buffer is widened,
5043 ;; and point is undefined, both at entry and exit.
5044 ;;
5045 ;; FIXME!!! This routine ignores the possibility of macros entirely.
5046 ;; 2010-01-29.
5047 (save-excursion
5048 (let ((beg-lit-limits (progn (goto-char beg) (c-literal-limits)))
5049 (end-lit-limits (progn (goto-char end) (c-literal-limits)))
5050 new-beg new-end need-new-beg need-new-end)
5051 ;; Locate the barrier before the changed region
5052 (goto-char (if beg-lit-limits (car beg-lit-limits) beg))
5053 (c-syntactic-skip-backward "^;{}" (max (- beg 2048) (point-min)))
5054 (setq new-beg (point))
5055
5056 ;; Remove the syntax-table properties from each pertinent <...> pair.
5057 ;; Firsly, the ones with the < before beg and > after beg.
5058 (while (c-search-forward-char-property 'category 'c-<-as-paren-syntax beg)
5059 (if (c-clear-<-pair-props-if-match-after beg (1- (point)))
5060 (setq need-new-beg t)))
5061
5062 ;; Locate the barrier after END.
5063 (goto-char (if end-lit-limits (cdr end-lit-limits) end))
5064 (c-syntactic-re-search-forward "[;{}]"
5065 (min (+ end 2048) (point-max)) 'end)
5066 (setq new-end (point))
5067
5068 ;; Remove syntax-table properties from the remaining pertinent <...>
5069 ;; pairs, those with a > after end and < before end.
5070 (while (c-search-backward-char-property 'category 'c->-as-paren-syntax end)
5071 (if (c-clear->-pair-props-if-match-before end)
5072 (setq need-new-end t)))
5073
5074 ;; Extend the fontification region, if needed.
5075 (when need-new-beg
5076 (goto-char new-beg)
5077 (c-forward-syntactic-ws)
5078 (and (< (point) c-new-BEG) (setq c-new-BEG (point))))
5079
5080 (when need-new-end
5081 (and (> new-end c-new-END) (setq c-new-END new-end))))))
5082
5083
5084
5085 (defun c-after-change-check-<>-operators (beg end)
5086 ;; This is called from `after-change-functions' when
5087 ;; c-recognize-<>-arglists' is set. It ensures that no "<" or ">"
5088 ;; chars with paren syntax become part of another operator like "<<"
5089 ;; or ">=".
5090 ;;
5091 ;; This function might do hidden buffer changes.
5092
5093 (save-excursion
5094 (goto-char beg)
5095 (when (or (looking-at "[<>]")
5096 (< (skip-chars-backward "<>") 0))
5097
5098 (goto-char beg)
5099 (c-beginning-of-current-token)
5100 (when (and (< (point) beg)
5101 (looking-at c-<>-multichar-token-regexp)
5102 (< beg (setq beg (match-end 0))))
5103 (while (progn (skip-chars-forward "^<>" beg)
5104 (< (point) beg))
5105 (c-clear-<>-pair-props)
5106 (forward-char))))
5107
5108 (when (< beg end)
5109 (goto-char end)
5110 (when (or (looking-at "[<>]")
5111 (< (skip-chars-backward "<>") 0))
5112
5113 (goto-char end)
5114 (c-beginning-of-current-token)
5115 (when (and (< (point) end)
5116 (looking-at c-<>-multichar-token-regexp)
5117 (< end (setq end (match-end 0))))
5118 (while (progn (skip-chars-forward "^<>" end)
5119 (< (point) end))
5120 (c-clear-<>-pair-props)
5121 (forward-char)))))))
5122
5123
5124 \f
5125 ;; Handling of small scale constructs like types and names.
5126
5127 ;; Dynamically bound variable that instructs `c-forward-type' to also
5128 ;; treat possible types (i.e. those that it normally returns 'maybe or
5129 ;; 'found for) as actual types (and always return 'found for them).
5130 ;; This means that it records them in `c-record-type-identifiers' if
5131 ;; that is set, and that it adds them to `c-found-types'.
5132 (defvar c-promote-possible-types nil)
5133
5134 ;; Dynamically bound variable that instructs `c-forward-<>-arglist' to
5135 ;; mark up successfully parsed arglists with paren syntax properties on
5136 ;; the surrounding angle brackets and with `c-<>-arg-sep' in the
5137 ;; `c-type' property of each argument separating comma.
5138 ;;
5139 ;; Setting this variable also makes `c-forward-<>-arglist' recurse into
5140 ;; all arglists for side effects (i.e. recording types), otherwise it
5141 ;; exploits any existing paren syntax properties to quickly jump to the
5142 ;; end of already parsed arglists.
5143 ;;
5144 ;; Marking up the arglists is not the default since doing that correctly
5145 ;; depends on a proper value for `c-restricted-<>-arglists'.
5146 (defvar c-parse-and-markup-<>-arglists nil)
5147
5148 ;; Dynamically bound variable that instructs `c-forward-<>-arglist' to
5149 ;; not accept arglists that contain binary operators.
5150 ;;
5151 ;; This is primarily used to handle C++ template arglists. C++
5152 ;; disambiguates them by checking whether the preceding name is a
5153 ;; template or not. We can't do that, so we assume it is a template
5154 ;; if it can be parsed as one. That usually works well since
5155 ;; comparison expressions on the forms "a < b > c" or "a < b, c > d"
5156 ;; in almost all cases would be pointless.
5157 ;;
5158 ;; However, in function arglists, e.g. in "foo (a < b, c > d)", we
5159 ;; should let the comma separate the function arguments instead. And
5160 ;; in a context where the value of the expression is taken, e.g. in
5161 ;; "if (a < b || c > d)", it's probably not a template.
5162 (defvar c-restricted-<>-arglists nil)
5163
5164 ;; Dynamically bound variables that instructs
5165 ;; `c-forward-keyword-clause', `c-forward-<>-arglist',
5166 ;; `c-forward-name', `c-forward-type', `c-forward-decl-or-cast-1', and
5167 ;; `c-forward-label' to record the ranges of all the type and
5168 ;; reference identifiers they encounter. They will build lists on
5169 ;; these variables where each element is a cons of the buffer
5170 ;; positions surrounding each identifier. This recording is only
5171 ;; activated when `c-record-type-identifiers' is non-nil.
5172 ;;
5173 ;; All known types that can't be identifiers are recorded, and also
5174 ;; other possible types if `c-promote-possible-types' is set.
5175 ;; Recording is however disabled inside angle bracket arglists that
5176 ;; are encountered inside names and other angle bracket arglists.
5177 ;; Such occurrences are taken care of by `c-font-lock-<>-arglists'
5178 ;; instead.
5179 ;;
5180 ;; Only the names in C++ template style references (e.g. "tmpl" in
5181 ;; "tmpl<a,b>::foo") are recorded as references, other references
5182 ;; aren't handled here.
5183 ;;
5184 ;; `c-forward-label' records the label identifier(s) on
5185 ;; `c-record-ref-identifiers'.
5186 (defvar c-record-type-identifiers nil)
5187 (defvar c-record-ref-identifiers nil)
5188
5189 ;; This variable will receive a cons cell of the range of the last
5190 ;; single identifier symbol stepped over by `c-forward-name' if it's
5191 ;; successful. This is the range that should be put on one of the
5192 ;; record lists above by the caller. It's assigned nil if there's no
5193 ;; such symbol in the name.
5194 (defvar c-last-identifier-range nil)
5195
5196 (defmacro c-record-type-id (range)
5197 (if (eq (car-safe range) 'cons)
5198 ;; Always true.
5199 `(setq c-record-type-identifiers
5200 (cons ,range c-record-type-identifiers))
5201 `(let ((range ,range))
5202 (if range
5203 (setq c-record-type-identifiers
5204 (cons range c-record-type-identifiers))))))
5205
5206 (defmacro c-record-ref-id (range)
5207 (if (eq (car-safe range) 'cons)
5208 ;; Always true.
5209 `(setq c-record-ref-identifiers
5210 (cons ,range c-record-ref-identifiers))
5211 `(let ((range ,range))
5212 (if range
5213 (setq c-record-ref-identifiers
5214 (cons range c-record-ref-identifiers))))))
5215
5216 ;; Dynamically bound variable that instructs `c-forward-type' to
5217 ;; record the ranges of types that only are found. Behaves otherwise
5218 ;; like `c-record-type-identifiers'.
5219 (defvar c-record-found-types nil)
5220
5221 (defmacro c-forward-keyword-prefixed-id (type)
5222 ;; Used internally in `c-forward-keyword-clause' to move forward
5223 ;; over a type (if TYPE is 'type) or a name (otherwise) which
5224 ;; possibly is prefixed by keywords and their associated clauses.
5225 ;; Try with a type/name first to not trip up on those that begin
5226 ;; with a keyword. Return t if a known or found type is moved
5227 ;; over. The point is clobbered if nil is returned. If range
5228 ;; recording is enabled, the identifier is recorded on as a type
5229 ;; if TYPE is 'type or as a reference if TYPE is 'ref.
5230 ;;
5231 ;; This macro might do hidden buffer changes.
5232 `(let (res)
5233 (while (if (setq res ,(if (eq type 'type)
5234 `(c-forward-type)
5235 `(c-forward-name)))
5236 nil
5237 (and (looking-at c-keywords-regexp)
5238 (c-forward-keyword-clause 1))))
5239 (when (memq res '(t known found prefix))
5240 ,(when (eq type 'ref)
5241 `(when c-record-type-identifiers
5242 (c-record-ref-id c-last-identifier-range)))
5243 t)))
5244
5245 (defmacro c-forward-id-comma-list (type update-safe-pos)
5246 ;; Used internally in `c-forward-keyword-clause' to move forward
5247 ;; over a comma separated list of types or names using
5248 ;; `c-forward-keyword-prefixed-id'.
5249 ;;
5250 ;; This macro might do hidden buffer changes.
5251 `(while (and (progn
5252 ,(when update-safe-pos
5253 `(setq safe-pos (point)))
5254 (eq (char-after) ?,))
5255 (progn
5256 (forward-char)
5257 (c-forward-syntactic-ws)
5258 (c-forward-keyword-prefixed-id ,type)))))
5259
5260 (defun c-forward-keyword-clause (match)
5261 ;; Submatch MATCH in the current match data is assumed to surround a
5262 ;; token. If it's a keyword, move over it and any immediately
5263 ;; following clauses associated with it, stopping at the start of
5264 ;; the next token. t is returned in that case, otherwise the point
5265 ;; stays and nil is returned. The kind of clauses that are
5266 ;; recognized are those specified by `c-type-list-kwds',
5267 ;; `c-ref-list-kwds', `c-colon-type-list-kwds',
5268 ;; `c-paren-nontype-kwds', `c-paren-type-kwds', `c-<>-type-kwds',
5269 ;; and `c-<>-arglist-kwds'.
5270 ;;
5271 ;; This function records identifier ranges on
5272 ;; `c-record-type-identifiers' and `c-record-ref-identifiers' if
5273 ;; `c-record-type-identifiers' is non-nil.
5274 ;;
5275 ;; Note that for `c-colon-type-list-kwds', which doesn't necessary
5276 ;; apply directly after the keyword, the type list is moved over
5277 ;; only when there is no unaccounted token before it (i.e. a token
5278 ;; that isn't moved over due to some other keyword list). The
5279 ;; identifier ranges in the list are still recorded if that should
5280 ;; be done, though.
5281 ;;
5282 ;; This function might do hidden buffer changes.
5283
5284 (let ((kwd-sym (c-keyword-sym (match-string match))) safe-pos pos
5285 ;; The call to `c-forward-<>-arglist' below is made after
5286 ;; `c-<>-sexp-kwds' keywords, so we're certain they actually
5287 ;; are angle bracket arglists and `c-restricted-<>-arglists'
5288 ;; should therefore be nil.
5289 (c-parse-and-markup-<>-arglists t)
5290 c-restricted-<>-arglists)
5291
5292 (when kwd-sym
5293 (goto-char (match-end match))
5294 (c-forward-syntactic-ws)
5295 (setq safe-pos (point))
5296
5297 (cond
5298 ((and (c-keyword-member kwd-sym 'c-type-list-kwds)
5299 (c-forward-keyword-prefixed-id type))
5300 ;; There's a type directly after a keyword in `c-type-list-kwds'.
5301 (c-forward-id-comma-list type t))
5302
5303 ((and (c-keyword-member kwd-sym 'c-ref-list-kwds)
5304 (c-forward-keyword-prefixed-id ref))
5305 ;; There's a name directly after a keyword in `c-ref-list-kwds'.
5306 (c-forward-id-comma-list ref t))
5307
5308 ((and (c-keyword-member kwd-sym 'c-paren-any-kwds)
5309 (eq (char-after) ?\())
5310 ;; There's an open paren after a keyword in `c-paren-any-kwds'.
5311
5312 (forward-char)
5313 (when (and (setq pos (c-up-list-forward))
5314 (eq (char-before pos) ?\)))
5315 (when (and c-record-type-identifiers
5316 (c-keyword-member kwd-sym 'c-paren-type-kwds))
5317 ;; Use `c-forward-type' on every identifier we can find
5318 ;; inside the paren, to record the types.
5319 (while (c-syntactic-re-search-forward c-symbol-start pos t)
5320 (goto-char (match-beginning 0))
5321 (unless (c-forward-type)
5322 (looking-at c-symbol-key) ; Always matches.
5323 (goto-char (match-end 0)))))
5324
5325 (goto-char pos)
5326 (c-forward-syntactic-ws)
5327 (setq safe-pos (point))))
5328
5329 ((and (c-keyword-member kwd-sym 'c-<>-sexp-kwds)
5330 (eq (char-after) ?<)
5331 (c-forward-<>-arglist (c-keyword-member kwd-sym 'c-<>-type-kwds)))
5332 (c-forward-syntactic-ws)
5333 (setq safe-pos (point)))
5334
5335 ((and (c-keyword-member kwd-sym 'c-nonsymbol-sexp-kwds)
5336 (not (looking-at c-symbol-start))
5337 (c-safe (c-forward-sexp) t))
5338 (c-forward-syntactic-ws)
5339 (setq safe-pos (point))))
5340
5341 (when (c-keyword-member kwd-sym 'c-colon-type-list-kwds)
5342 (if (eq (char-after) ?:)
5343 ;; If we are at the colon already, we move over the type
5344 ;; list after it.
5345 (progn
5346 (forward-char)
5347 (c-forward-syntactic-ws)
5348 (when (c-forward-keyword-prefixed-id type)
5349 (c-forward-id-comma-list type t)))
5350 ;; Not at the colon, so stop here. But the identifier
5351 ;; ranges in the type list later on should still be
5352 ;; recorded.
5353 (and c-record-type-identifiers
5354 (progn
5355 ;; If a keyword matched both one of the types above and
5356 ;; this one, we match `c-colon-type-list-re' after the
5357 ;; clause matched above.
5358 (goto-char safe-pos)
5359 (looking-at c-colon-type-list-re))
5360 (progn
5361 (goto-char (match-end 0))
5362 (c-forward-syntactic-ws)
5363 (c-forward-keyword-prefixed-id type))
5364 ;; There's a type after the `c-colon-type-list-re' match
5365 ;; after a keyword in `c-colon-type-list-kwds'.
5366 (c-forward-id-comma-list type nil))))
5367
5368 (goto-char safe-pos)
5369 t)))
5370
5371 ;; cc-mode requires cc-fonts.
5372 (declare-function c-fontify-recorded-types-and-refs "cc-fonts" ())
5373
5374 (defvar c-forward-<>-arglist-recur-depth)
5375
5376 (defun c-forward-<>-arglist (all-types)
5377 ;; The point is assumed to be at a "<". Try to treat it as the open
5378 ;; paren of an angle bracket arglist and move forward to the
5379 ;; corresponding ">". If successful, the point is left after the
5380 ;; ">" and t is returned, otherwise the point isn't moved and nil is
5381 ;; returned. If ALL-TYPES is t then all encountered arguments in
5382 ;; the arglist that might be types are treated as found types.
5383 ;;
5384 ;; The variable `c-parse-and-markup-<>-arglists' controls how this
5385 ;; function handles text properties on the angle brackets and argument
5386 ;; separating commas.
5387 ;;
5388 ;; `c-restricted-<>-arglists' controls how lenient the template
5389 ;; arglist recognition should be.
5390 ;;
5391 ;; This function records identifier ranges on
5392 ;; `c-record-type-identifiers' and `c-record-ref-identifiers' if
5393 ;; `c-record-type-identifiers' is non-nil.
5394 ;;
5395 ;; This function might do hidden buffer changes.
5396
5397 (let ((start (point))
5398 ;; If `c-record-type-identifiers' is set then activate
5399 ;; recording of any found types that constitute an argument in
5400 ;; the arglist.
5401 (c-record-found-types (if c-record-type-identifiers t))
5402 (c-forward-<>-arglist-recur--depth 0))
5403 (if (catch 'angle-bracket-arglist-escape
5404 (setq c-record-found-types
5405 (c-forward-<>-arglist-recur all-types)))
5406 (progn
5407 (when (consp c-record-found-types)
5408 (setq c-record-type-identifiers
5409 ;; `nconc' doesn't mind that the tail of
5410 ;; `c-record-found-types' is t.
5411 (nconc c-record-found-types c-record-type-identifiers)))
5412 (if (c-major-mode-is 'java-mode) (c-fontify-recorded-types-and-refs))
5413 t)
5414
5415 (goto-char start)
5416 nil)))
5417
5418 (defun c-forward-<>-arglist-recur (all-types)
5419
5420 ;; Temporary workaround for Bug#7722.
5421 (when (boundp 'c-forward-<>-arglist-recur--depth)
5422 (if (> c-forward-<>-arglist-recur--depth 200)
5423 (error "Max recursion depth reached in <> arglist")
5424 (setq c-forward-<>-arglist-recur--depth
5425 (1+ c-forward-<>-arglist-recur--depth))))
5426
5427 ;; Recursive part of `c-forward-<>-arglist'.
5428 ;;
5429 ;; This function might do hidden buffer changes.
5430
5431 (let ((start (point)) res pos tmp
5432 ;; Cover this so that any recorded found type ranges are
5433 ;; automatically lost if it turns out to not be an angle
5434 ;; bracket arglist. It's propagated through the return value
5435 ;; on successful completion.
5436 (c-record-found-types c-record-found-types)
5437 ;; List that collects the positions after the argument
5438 ;; separating ',' in the arglist.
5439 arg-start-pos)
5440 ;; If the '<' has paren open syntax then we've marked it as an angle
5441 ;; bracket arglist before, so skip to the end.
5442 (if (and (not c-parse-and-markup-<>-arglists)
5443 (c-get-char-property (point) 'syntax-table))
5444
5445 (progn
5446 (forward-char)
5447 (if (and (c-go-up-list-forward)
5448 (eq (char-before) ?>))
5449 t
5450 ;; Got unmatched paren angle brackets. We don't clear the paren
5451 ;; syntax properties and retry, on the basis that it's very
5452 ;; unlikely that paren angle brackets become operators by code
5453 ;; manipulation. It's far more likely that it doesn't match due
5454 ;; to narrowing or some temporary change.
5455 (goto-char start)
5456 nil))
5457
5458 (forward-char)
5459
5460 (unless (looking-at c-<-op-cont-regexp)
5461 (while (and
5462 (progn
5463 (c-forward-syntactic-ws)
5464 (let ((orig-record-found-types c-record-found-types))
5465 (when (or (and c-record-type-identifiers all-types)
5466 (c-major-mode-is 'java-mode))
5467 ;; All encountered identifiers are types, so set the
5468 ;; promote flag and parse the type.
5469 (progn
5470 (c-forward-syntactic-ws)
5471 (if (looking-at "\\?")
5472 (forward-char)
5473 (when (looking-at c-identifier-start)
5474 (let ((c-promote-possible-types t)
5475 (c-record-found-types t))
5476 (c-forward-type))))
5477
5478 (c-forward-syntactic-ws)
5479
5480 (when (or (looking-at "extends")
5481 (looking-at "super"))
5482 (forward-word)
5483 (c-forward-syntactic-ws)
5484 (let ((c-promote-possible-types t)
5485 (c-record-found-types t))
5486 (c-forward-type)
5487 (c-forward-syntactic-ws))))))
5488
5489 (setq pos (point))
5490
5491 ;; Note: These regexps exploit the match order in \| so
5492 ;; that "<>" is matched by "<" rather than "[^>:-]>".
5493 (c-syntactic-re-search-forward
5494 ;; Stop on ',', '|', '&', '+' and '-' to catch
5495 ;; common binary operators that could be between
5496 ;; two comparison expressions "a<b" and "c>d".
5497 "[<;{},|+&-]\\|[>)]"
5498 nil t t))
5499
5500 (cond
5501 ((eq (char-before) ?>)
5502 ;; Either an operator starting with '>' or the end of
5503 ;; the angle bracket arglist.
5504
5505 (if (looking-at c->-op-cont-regexp)
5506 (progn
5507 (goto-char (match-end 0))
5508 t) ; Continue the loop.
5509
5510 ;; The angle bracket arglist is finished.
5511 (when c-parse-and-markup-<>-arglists
5512 (while arg-start-pos
5513 (c-put-c-type-property (1- (car arg-start-pos))
5514 'c-<>-arg-sep)
5515 (setq arg-start-pos (cdr arg-start-pos)))
5516 (c-mark-<-as-paren start)
5517 (c-mark->-as-paren (1- (point))))
5518 (setq res t)
5519 nil)) ; Exit the loop.
5520
5521 ((eq (char-before) ?<)
5522 ;; Either an operator starting with '<' or a nested arglist.
5523 (setq pos (point))
5524 (let (id-start id-end subres keyword-match)
5525 (if (if (looking-at c-<-op-cont-regexp)
5526 (setq tmp (match-end 0))
5527 (setq tmp pos)
5528 (backward-char)
5529 (not
5530 (and
5531
5532 (save-excursion
5533 ;; There's always an identifier before an angle
5534 ;; bracket arglist, or a keyword in
5535 ;; `c-<>-type-kwds' or `c-<>-arglist-kwds'.
5536 (c-backward-syntactic-ws)
5537 (setq id-end (point))
5538 (c-simple-skip-symbol-backward)
5539 (when (or (setq keyword-match
5540 (looking-at c-opt-<>-sexp-key))
5541 (not (looking-at c-keywords-regexp)))
5542 (setq id-start (point))))
5543
5544 (setq subres
5545 (let ((c-promote-possible-types t)
5546 (c-record-found-types t))
5547 (c-forward-<>-arglist-recur
5548 (and keyword-match
5549 (c-keyword-member
5550 (c-keyword-sym (match-string 1))
5551 'c-<>-type-kwds)))))
5552 )))
5553
5554 ;; It was not an angle bracket arglist.
5555 (goto-char tmp)
5556
5557 ;; It was an angle bracket arglist.
5558 (setq c-record-found-types subres)
5559
5560 ;; Record the identifier before the template as a type
5561 ;; or reference depending on whether the arglist is last
5562 ;; in a qualified identifier.
5563 (when (and c-record-type-identifiers
5564 (not keyword-match))
5565 (if (and c-opt-identifier-concat-key
5566 (progn
5567 (c-forward-syntactic-ws)
5568 (looking-at c-opt-identifier-concat-key)))
5569 (c-record-ref-id (cons id-start id-end))
5570 (c-record-type-id (cons id-start id-end))))))
5571 t)
5572
5573 ((and (not c-restricted-<>-arglists)
5574 (or (and (eq (char-before) ?&)
5575 (not (eq (char-after) ?&)))
5576 (eq (char-before) ?,)))
5577 ;; Just another argument. Record the position. The
5578 ;; type check stuff that made us stop at it is at
5579 ;; the top of the loop.
5580 (setq arg-start-pos (cons (point) arg-start-pos)))
5581
5582 (t
5583 ;; Got a character that can't be in an angle bracket
5584 ;; arglist argument. Abort using `throw', since
5585 ;; it's useless to try to find a surrounding arglist
5586 ;; if we're nested.
5587 (throw 'angle-bracket-arglist-escape nil))))))
5588 (if res
5589 (or c-record-found-types t)))))
5590
5591 (defun c-backward-<>-arglist (all-types &optional limit)
5592 ;; The point is assumed to be directly after a ">". Try to treat it
5593 ;; as the close paren of an angle bracket arglist and move back to
5594 ;; the corresponding "<". If successful, the point is left at
5595 ;; the "<" and t is returned, otherwise the point isn't moved and
5596 ;; nil is returned. ALL-TYPES is passed on to
5597 ;; `c-forward-<>-arglist'.
5598 ;;
5599 ;; If the optional LIMIT is given, it bounds the backward search.
5600 ;; It's then assumed to be at a syntactically relevant position.
5601 ;;
5602 ;; This is a wrapper around `c-forward-<>-arglist'. See that
5603 ;; function for more details.
5604
5605 (let ((start (point)))
5606 (backward-char)
5607 (if (and (not c-parse-and-markup-<>-arglists)
5608 (c-get-char-property (point) 'syntax-table))
5609
5610 (if (and (c-go-up-list-backward)
5611 (eq (char-after) ?<))
5612 t
5613 ;; See corresponding note in `c-forward-<>-arglist'.
5614 (goto-char start)
5615 nil)
5616
5617 (while (progn
5618 (c-syntactic-skip-backward "^<;{}" limit t)
5619
5620 (and
5621 (if (eq (char-before) ?<)
5622 t
5623 ;; Stopped at bob or a char that isn't allowed in an
5624 ;; arglist, so we've failed.
5625 (goto-char start)
5626 nil)
5627
5628 (if (> (point)
5629 (progn (c-beginning-of-current-token)
5630 (point)))
5631 ;; If we moved then the "<" was part of some
5632 ;; multicharacter token.
5633 t
5634
5635 (backward-char)
5636 (let ((beg-pos (point)))
5637 (if (c-forward-<>-arglist all-types)
5638 (cond ((= (point) start)
5639 ;; Matched the arglist. Break the while.
5640 (goto-char beg-pos)
5641 nil)
5642 ((> (point) start)
5643 ;; We started from a non-paren ">" inside an
5644 ;; arglist.
5645 (goto-char start)
5646 nil)
5647 (t
5648 ;; Matched a shorter arglist. Can be a nested
5649 ;; one so continue looking.
5650 (goto-char beg-pos)
5651 t))
5652 t))))))
5653
5654 (/= (point) start))))
5655
5656 (defun c-forward-name ()
5657 ;; Move forward over a complete name if at the beginning of one,
5658 ;; stopping at the next following token. A keyword, as such,
5659 ;; doesn't count as a name. If the point is not at something that
5660 ;; is recognized as a name then it stays put.
5661 ;;
5662 ;; A name could be something as simple as "foo" in C or something as
5663 ;; complex as "X<Y<class A<int>::B, BIT_MAX >> b>, ::operator<> ::
5664 ;; Z<(a>b)> :: operator const X<&foo>::T Q::G<unsigned short
5665 ;; int>::*volatile const" in C++ (this function is actually little
5666 ;; more than a `looking-at' call in all modes except those that,
5667 ;; like C++, have `c-recognize-<>-arglists' set).
5668 ;;
5669 ;; Return
5670 ;; o - nil if no name is found;
5671 ;; o - 'template if it's an identifier ending with an angle bracket
5672 ;; arglist;
5673 ;; o - 'operator of it's an operator identifier;
5674 ;; o - t if it's some other kind of name.
5675 ;;
5676 ;; This function records identifier ranges on
5677 ;; `c-record-type-identifiers' and `c-record-ref-identifiers' if
5678 ;; `c-record-type-identifiers' is non-nil.
5679 ;;
5680 ;; This function might do hidden buffer changes.
5681
5682 (let ((pos (point)) (start (point)) res id-start id-end
5683 ;; Turn off `c-promote-possible-types' here since we might
5684 ;; call `c-forward-<>-arglist' and we don't want it to promote
5685 ;; every suspect thing in the arglist to a type. We're
5686 ;; typically called from `c-forward-type' in this case, and
5687 ;; the caller only wants the top level type that it finds to
5688 ;; be promoted.
5689 c-promote-possible-types)
5690 (while
5691 (and
5692 (looking-at c-identifier-key)
5693
5694 (progn
5695 ;; Check for keyword. We go to the last symbol in
5696 ;; `c-identifier-key' first.
5697 (goto-char (setq id-end (match-end 0)))
5698 (c-simple-skip-symbol-backward)
5699 (setq id-start (point))
5700
5701 (if (looking-at c-keywords-regexp)
5702 (when (and (c-major-mode-is 'c++-mode)
5703 (looking-at
5704 (cc-eval-when-compile
5705 (concat "\\(operator\\|\\(template\\)\\)"
5706 "\\(" (c-lang-const c-nonsymbol-key c++)
5707 "\\|$\\)")))
5708 (if (match-beginning 2)
5709 ;; "template" is only valid inside an
5710 ;; identifier if preceded by "::".
5711 (save-excursion
5712 (c-backward-syntactic-ws)
5713 (and (c-safe (backward-char 2) t)
5714 (looking-at "::")))
5715 t))
5716
5717 ;; Handle a C++ operator or template identifier.
5718 (goto-char id-end)
5719 (c-forward-syntactic-ws)
5720 (cond ((eq (char-before id-end) ?e)
5721 ;; Got "... ::template".
5722 (let ((subres (c-forward-name)))
5723 (when subres
5724 (setq pos (point)
5725 res subres))))
5726
5727 ((looking-at c-identifier-start)
5728 ;; Got a cast operator.
5729 (when (c-forward-type)
5730 (setq pos (point)
5731 res 'operator)
5732 ;; Now we should match a sequence of either
5733 ;; '*', '&' or a name followed by ":: *",
5734 ;; where each can be followed by a sequence
5735 ;; of `c-opt-type-modifier-key'.
5736 (while (cond ((looking-at "[*&]")
5737 (goto-char (match-end 0))
5738 t)
5739 ((looking-at c-identifier-start)
5740 (and (c-forward-name)
5741 (looking-at "::")
5742 (progn
5743 (goto-char (match-end 0))
5744 (c-forward-syntactic-ws)
5745 (eq (char-after) ?*))
5746 (progn
5747 (forward-char)
5748 t))))
5749 (while (progn
5750 (c-forward-syntactic-ws)
5751 (setq pos (point))
5752 (looking-at c-opt-type-modifier-key))
5753 (goto-char (match-end 1))))))
5754
5755 ((looking-at c-overloadable-operators-regexp)
5756 ;; Got some other operator.
5757 (setq c-last-identifier-range
5758 (cons (point) (match-end 0)))
5759 (goto-char (match-end 0))
5760 (c-forward-syntactic-ws)
5761 (setq pos (point)
5762 res 'operator)))
5763
5764 nil)
5765
5766 ;; `id-start' is equal to `id-end' if we've jumped over
5767 ;; an identifier that doesn't end with a symbol token.
5768 ;; That can occur e.g. for Java import directives on the
5769 ;; form "foo.bar.*".
5770 (when (and id-start (/= id-start id-end))
5771 (setq c-last-identifier-range
5772 (cons id-start id-end)))
5773 (goto-char id-end)
5774 (c-forward-syntactic-ws)
5775 (setq pos (point)
5776 res t)))
5777
5778 (progn
5779 (goto-char pos)
5780 (when (or c-opt-identifier-concat-key
5781 c-recognize-<>-arglists)
5782
5783 (cond
5784 ((and c-opt-identifier-concat-key
5785 (looking-at c-opt-identifier-concat-key))
5786 ;; Got a concatenated identifier. This handles the
5787 ;; cases with tricky syntactic whitespace that aren't
5788 ;; covered in `c-identifier-key'.
5789 (goto-char (match-end 0))
5790 (c-forward-syntactic-ws)
5791 t)
5792
5793 ((and c-recognize-<>-arglists
5794 (eq (char-after) ?<))
5795 ;; Maybe an angle bracket arglist.
5796 (when (let ((c-record-type-identifiers t)
5797 (c-record-found-types t))
5798 (c-forward-<>-arglist nil))
5799
5800 (c-add-type start (1+ pos))
5801 (c-forward-syntactic-ws)
5802 (setq pos (point)
5803 c-last-identifier-range nil)
5804
5805 (if (and c-opt-identifier-concat-key
5806 (looking-at c-opt-identifier-concat-key))
5807
5808 ;; Continue if there's an identifier concatenation
5809 ;; operator after the template argument.
5810 (progn
5811 (when (and c-record-type-identifiers id-start)
5812 (c-record-ref-id (cons id-start id-end)))
5813 (forward-char 2)
5814 (c-forward-syntactic-ws)
5815 t)
5816
5817 (when (and c-record-type-identifiers id-start)
5818 (c-record-type-id (cons id-start id-end)))
5819 (setq res 'template)
5820 nil)))
5821 )))))
5822
5823 (goto-char pos)
5824 res))
5825
5826 (defun c-forward-type (&optional brace-block-too)
5827 ;; Move forward over a type spec if at the beginning of one,
5828 ;; stopping at the next following token. The keyword "typedef"
5829 ;; isn't part of a type spec here.
5830 ;;
5831 ;; BRACE-BLOCK-TOO, when non-nil, means move over the brace block in
5832 ;; constructs like "struct foo {...} bar ;" or "struct {...} bar;".
5833 ;; The current (2009-03-10) intention is to convert all uses of
5834 ;; `c-forward-type' to call with this parameter set, then to
5835 ;; eliminate it.
5836 ;;
5837 ;; Return
5838 ;; o - t if it's a known type that can't be a name or other
5839 ;; expression;
5840 ;; o - 'known if it's an otherwise known type (according to
5841 ;; `*-font-lock-extra-types');
5842 ;; o - 'prefix if it's a known prefix of a type;
5843 ;; o - 'found if it's a type that matches one in `c-found-types';
5844 ;; o - 'maybe if it's an identfier that might be a type; or
5845 ;; o - nil if it can't be a type (the point isn't moved then).
5846 ;;
5847 ;; The point is assumed to be at the beginning of a token.
5848 ;;
5849 ;; Note that this function doesn't skip past the brace definition
5850 ;; that might be considered part of the type, e.g.
5851 ;; "enum {a, b, c} foo".
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 (when (and c-recognize-<>-arglists
5859 (looking-at "<"))
5860 (c-forward-<>-arglist t)
5861 (c-forward-syntactic-ws))
5862
5863 (let ((start (point)) pos res name-res id-start id-end id-range)
5864
5865 ;; Skip leading type modifiers. If any are found we know it's a
5866 ;; prefix of a type.
5867 (when c-opt-type-modifier-key ; e.g. "const" "volatile", but NOT "typedef"
5868 (while (looking-at c-opt-type-modifier-key)
5869 (goto-char (match-end 1))
5870 (c-forward-syntactic-ws)
5871 (setq res 'prefix)))
5872
5873 (cond
5874 ((looking-at c-type-prefix-key) ; e.g. "struct", "class", but NOT
5875 ; "typedef".
5876 (goto-char (match-end 1))
5877 (c-forward-syntactic-ws)
5878 (setq pos (point))
5879
5880 (setq name-res (c-forward-name))
5881 (setq res (not (null name-res)))
5882 (when (eq name-res t)
5883 ;; In many languages the name can be used without the
5884 ;; prefix, so we add it to `c-found-types'.
5885 (c-add-type pos (point))
5886 (when (and c-record-type-identifiers
5887 c-last-identifier-range)
5888 (c-record-type-id c-last-identifier-range)))
5889 (when (and brace-block-too
5890 (memq res '(t nil))
5891 (eq (char-after) ?\{)
5892 (save-excursion
5893 (c-safe
5894 (progn (c-forward-sexp)
5895 (c-forward-syntactic-ws)
5896 (setq pos (point))))))
5897 (goto-char pos)
5898 (setq res t))
5899 (unless res (goto-char start))) ; invalid syntax
5900
5901 ((progn
5902 (setq pos nil)
5903 (if (looking-at c-identifier-start)
5904 (save-excursion
5905 (setq id-start (point)
5906 name-res (c-forward-name))
5907 (when name-res
5908 (setq id-end (point)
5909 id-range c-last-identifier-range))))
5910 (and (cond ((looking-at c-primitive-type-key)
5911 (setq res t))
5912 ((c-with-syntax-table c-identifier-syntax-table
5913 (looking-at c-known-type-key))
5914 (setq res 'known)))
5915 (or (not id-end)
5916 (>= (save-excursion
5917 (save-match-data
5918 (goto-char (match-end 1))
5919 (c-forward-syntactic-ws)
5920 (setq pos (point))))
5921 id-end)
5922 (setq res nil))))
5923 ;; Looking at a primitive or known type identifier. We've
5924 ;; checked for a name first so that we don't go here if the
5925 ;; known type match only is a prefix of another name.
5926
5927 (setq id-end (match-end 1))
5928
5929 (when (and c-record-type-identifiers
5930 (or c-promote-possible-types (eq res t)))
5931 (c-record-type-id (cons (match-beginning 1) (match-end 1))))
5932
5933 (if (and c-opt-type-component-key
5934 (save-match-data
5935 (looking-at c-opt-type-component-key)))
5936 ;; There might be more keywords for the type.
5937 (let (safe-pos)
5938 (c-forward-keyword-clause 1)
5939 (while (progn
5940 (setq safe-pos (point))
5941 (looking-at c-opt-type-component-key))
5942 (when (and c-record-type-identifiers
5943 (looking-at c-primitive-type-key))
5944 (c-record-type-id (cons (match-beginning 1)
5945 (match-end 1))))
5946 (c-forward-keyword-clause 1))
5947 (if (looking-at c-primitive-type-key)
5948 (progn
5949 (when c-record-type-identifiers
5950 (c-record-type-id (cons (match-beginning 1)
5951 (match-end 1))))
5952 (c-forward-keyword-clause 1)
5953 (setq res t))
5954 (goto-char safe-pos)
5955 (setq res 'prefix)))
5956 (unless (save-match-data (c-forward-keyword-clause 1))
5957 (if pos
5958 (goto-char pos)
5959 (goto-char (match-end 1))
5960 (c-forward-syntactic-ws)))))
5961
5962 (name-res
5963 (cond ((eq name-res t)
5964 ;; A normal identifier.
5965 (goto-char id-end)
5966 (if (or res c-promote-possible-types)
5967 (progn
5968 (c-add-type id-start id-end)
5969 (when (and c-record-type-identifiers id-range)
5970 (c-record-type-id id-range))
5971 (unless res
5972 (setq res 'found)))
5973 (setq res (if (c-check-type id-start id-end)
5974 ;; It's an identifier that has been used as
5975 ;; a type somewhere else.
5976 'found
5977 ;; It's an identifier that might be a type.
5978 'maybe))))
5979 ((eq name-res 'template)
5980 ;; A template is a type.
5981 (goto-char id-end)
5982 (setq res t))
5983 (t
5984 ;; Otherwise it's an operator identifier, which is not a type.
5985 (goto-char start)
5986 (setq res nil)))))
5987
5988 (when res
5989 ;; Skip trailing type modifiers. If any are found we know it's
5990 ;; a type.
5991 (when c-opt-type-modifier-key
5992 (while (looking-at c-opt-type-modifier-key) ; e.g. "const", "volatile"
5993 (goto-char (match-end 1))
5994 (c-forward-syntactic-ws)
5995 (setq res t)))
5996 ;; Step over any type suffix operator. Do not let the existence
5997 ;; of these alter the classification of the found type, since
5998 ;; these operators typically are allowed in normal expressions
5999 ;; too.
6000 (when c-opt-type-suffix-key
6001 (while (looking-at c-opt-type-suffix-key)
6002 (goto-char (match-end 1))
6003 (c-forward-syntactic-ws)))
6004
6005 (when c-opt-type-concat-key ; Only/mainly for pike.
6006 ;; Look for a trailing operator that concatenates the type
6007 ;; with a following one, and if so step past that one through
6008 ;; a recursive call. Note that we don't record concatenated
6009 ;; types in `c-found-types' - it's the component types that
6010 ;; are recorded when appropriate.
6011 (setq pos (point))
6012 (let* ((c-promote-possible-types (or (memq res '(t known))
6013 c-promote-possible-types))
6014 ;; If we can't promote then set `c-record-found-types' so that
6015 ;; we can merge in the types from the second part afterwards if
6016 ;; it turns out to be a known type there.
6017 (c-record-found-types (and c-record-type-identifiers
6018 (not c-promote-possible-types)))
6019 subres)
6020 (if (and (looking-at c-opt-type-concat-key)
6021
6022 (progn
6023 (goto-char (match-end 1))
6024 (c-forward-syntactic-ws)
6025 (setq subres (c-forward-type))))
6026
6027 (progn
6028 ;; If either operand certainly is a type then both are, but we
6029 ;; don't let the existence of the operator itself promote two
6030 ;; uncertain types to a certain one.
6031 (cond ((eq res t))
6032 ((eq subres t)
6033 (unless (eq name-res 'template)
6034 (c-add-type id-start id-end))
6035 (when (and c-record-type-identifiers id-range)
6036 (c-record-type-id id-range))
6037 (setq res t))
6038 ((eq res 'known))
6039 ((eq subres 'known)
6040 (setq res 'known))
6041 ((eq res 'found))
6042 ((eq subres 'found)
6043 (setq res 'found))
6044 (t
6045 (setq res 'maybe)))
6046
6047 (when (and (eq res t)
6048 (consp c-record-found-types))
6049 ;; Merge in the ranges of any types found by the second
6050 ;; `c-forward-type'.
6051 (setq c-record-type-identifiers
6052 ;; `nconc' doesn't mind that the tail of
6053 ;; `c-record-found-types' is t.
6054 (nconc c-record-found-types
6055 c-record-type-identifiers))))
6056
6057 (goto-char pos))))
6058
6059 (when (and c-record-found-types (memq res '(known found)) id-range)
6060 (setq c-record-found-types
6061 (cons id-range c-record-found-types))))
6062
6063 ;;(message "c-forward-type %s -> %s: %s" start (point) res)
6064
6065 res))
6066
6067 (defun c-forward-annotation ()
6068 ;; Used for Java code only at the moment. Assumes point is on the
6069 ;; @, moves forward an annotation. returns nil if there is no
6070 ;; annotation at point.
6071 (and (looking-at "@")
6072 (progn (forward-char) t)
6073 (c-forward-type)
6074 (progn (c-forward-syntactic-ws) t)
6075 (if (looking-at "(")
6076 (c-go-list-forward)
6077 t)))
6078
6079 \f
6080 ;; Handling of large scale constructs like statements and declarations.
6081
6082 ;; Macro used inside `c-forward-decl-or-cast-1'. It ought to be a
6083 ;; defsubst or perhaps even a defun, but it contains lots of free
6084 ;; variables that refer to things inside `c-forward-decl-or-cast-1'.
6085 (defmacro c-fdoc-shift-type-backward (&optional short)
6086 ;; `c-forward-decl-or-cast-1' can consume an arbitrary length list
6087 ;; of types when parsing a declaration, which means that it
6088 ;; sometimes consumes the identifier in the declaration as a type.
6089 ;; This is used to "backtrack" and make the last type be treated as
6090 ;; an identifier instead.
6091 `(progn
6092 ,(unless short
6093 ;; These identifiers are bound only in the inner let.
6094 '(setq identifier-type at-type
6095 identifier-start type-start
6096 got-parens nil
6097 got-identifier t
6098 got-suffix t
6099 got-suffix-after-parens id-start
6100 paren-depth 0))
6101
6102 (if (setq at-type (if (eq backup-at-type 'prefix)
6103 t
6104 backup-at-type))
6105 (setq type-start backup-type-start
6106 id-start backup-id-start)
6107 (setq type-start start-pos
6108 id-start start-pos))
6109
6110 ;; When these flags already are set we've found specifiers that
6111 ;; unconditionally signal these attributes - backtracking doesn't
6112 ;; change that. So keep them set in that case.
6113 (or at-type-decl
6114 (setq at-type-decl backup-at-type-decl))
6115 (or maybe-typeless
6116 (setq maybe-typeless backup-maybe-typeless))
6117
6118 ,(unless short
6119 ;; This identifier is bound only in the inner let.
6120 '(setq start id-start))))
6121
6122 (defun c-forward-decl-or-cast-1 (preceding-token-end context last-cast-end)
6123 ;; Move forward over a declaration or a cast if at the start of one.
6124 ;; The point is assumed to be at the start of some token. Nil is
6125 ;; returned if no declaration or cast is recognized, and the point
6126 ;; is clobbered in that case.
6127 ;;
6128 ;; If a declaration is parsed:
6129 ;;
6130 ;; The point is left at the first token after the first complete
6131 ;; declarator, if there is one. The return value is a cons where
6132 ;; the car is the position of the first token in the declarator. (See
6133 ;; below for the cdr.)
6134 ;; Some examples:
6135 ;;
6136 ;; void foo (int a, char *b) stuff ...
6137 ;; car ^ ^ point
6138 ;; float (*a)[], b;
6139 ;; car ^ ^ point
6140 ;; unsigned int a = c_style_initializer, b;
6141 ;; car ^ ^ point
6142 ;; unsigned int a (cplusplus_style_initializer), b;
6143 ;; car ^ ^ point (might change)
6144 ;; class Foo : public Bar {}
6145 ;; car ^ ^ point
6146 ;; class PikeClass (int a, string b) stuff ...
6147 ;; car ^ ^ point
6148 ;; enum bool;
6149 ;; car ^ ^ point
6150 ;; enum bool flag;
6151 ;; car ^ ^ point
6152 ;; void cplusplus_function (int x) throw (Bad);
6153 ;; car ^ ^ point
6154 ;; Foo::Foo (int b) : Base (b) {}
6155 ;; car ^ ^ point
6156 ;;
6157 ;; The cdr of the return value is non-nil when a
6158 ;; `c-typedef-decl-kwds' specifier is found in the declaration.
6159 ;; Specifically it is a dotted pair (A . B) where B is t when a
6160 ;; `c-typedef-kwds' ("typedef") is present, and A is t when some
6161 ;; other `c-typedef-decl-kwds' (e.g. class, struct, enum)
6162 ;; specifier is present. I.e., (some of) the declared
6163 ;; identifier(s) are types.
6164 ;;
6165 ;; If a cast is parsed:
6166 ;;
6167 ;; The point is left at the first token after the closing paren of
6168 ;; the cast. The return value is `cast'. Note that the start
6169 ;; position must be at the first token inside the cast parenthesis
6170 ;; to recognize it.
6171 ;;
6172 ;; PRECEDING-TOKEN-END is the first position after the preceding
6173 ;; token, i.e. on the other side of the syntactic ws from the point.
6174 ;; Use a value less than or equal to (point-min) if the point is at
6175 ;; the first token in (the visible part of) the buffer.
6176 ;;
6177 ;; CONTEXT is a symbol that describes the context at the point:
6178 ;; 'decl In a comma-separated declaration context (typically
6179 ;; inside a function declaration arglist).
6180 ;; '<> In an angle bracket arglist.
6181 ;; 'arglist Some other type of arglist.
6182 ;; nil Some other context or unknown context. Includes
6183 ;; within the parens of an if, for, ... construct.
6184 ;;
6185 ;; LAST-CAST-END is the first token after the closing paren of a
6186 ;; preceding cast, or nil if none is known. If
6187 ;; `c-forward-decl-or-cast-1' is used in succession, it should be
6188 ;; the position after the closest preceding call where a cast was
6189 ;; matched. In that case it's used to discover chains of casts like
6190 ;; "(a) (b) c".
6191 ;;
6192 ;; This function records identifier ranges on
6193 ;; `c-record-type-identifiers' and `c-record-ref-identifiers' if
6194 ;; `c-record-type-identifiers' is non-nil.
6195 ;;
6196 ;; This function might do hidden buffer changes.
6197
6198 (let (;; `start-pos' is used below to point to the start of the
6199 ;; first type, i.e. after any leading specifiers. It might
6200 ;; also point at the beginning of the preceding syntactic
6201 ;; whitespace.
6202 (start-pos (point))
6203 ;; Set to the result of `c-forward-type'.
6204 at-type
6205 ;; The position of the first token in what we currently
6206 ;; believe is the type in the declaration or cast, after any
6207 ;; specifiers and their associated clauses.
6208 type-start
6209 ;; The position of the first token in what we currently
6210 ;; believe is the declarator for the first identifier. Set
6211 ;; when the type is found, and moved forward over any
6212 ;; `c-decl-hangon-kwds' and their associated clauses that
6213 ;; occurs after the type.
6214 id-start
6215 ;; These store `at-type', `type-start' and `id-start' of the
6216 ;; identifier before the one in those variables. The previous
6217 ;; identifier might turn out to be the real type in a
6218 ;; declaration if the last one has to be the declarator in it.
6219 ;; If `backup-at-type' is nil then the other variables have
6220 ;; undefined values.
6221 backup-at-type backup-type-start backup-id-start
6222 ;; Set if we've found a specifier (apart from "typedef") that makes
6223 ;; the defined identifier(s) types.
6224 at-type-decl
6225 ;; Set if we've a "typedef" keyword.
6226 at-typedef
6227 ;; Set if we've found a specifier that can start a declaration
6228 ;; where there's no type.
6229 maybe-typeless
6230 ;; If a specifier is found that also can be a type prefix,
6231 ;; these flags are set instead of those above. If we need to
6232 ;; back up an identifier, they are copied to the real flag
6233 ;; variables. Thus they only take effect if we fail to
6234 ;; interpret it as a type.
6235 backup-at-type-decl backup-maybe-typeless
6236 ;; Whether we've found a declaration or a cast. We might know
6237 ;; this before we've found the type in it. It's 'ids if we've
6238 ;; found two consecutive identifiers (usually a sure sign, but
6239 ;; we should allow that in labels too), and t if we've found a
6240 ;; specifier keyword (a 100% sure sign).
6241 at-decl-or-cast
6242 ;; Set when we need to back up to parse this as a declaration
6243 ;; but not as a cast.
6244 backup-if-not-cast
6245 ;; For casts, the return position.
6246 cast-end
6247 ;; Save `c-record-type-identifiers' and
6248 ;; `c-record-ref-identifiers' since ranges are recorded
6249 ;; speculatively and should be thrown away if it turns out
6250 ;; that it isn't a declaration or cast.
6251 (save-rec-type-ids c-record-type-identifiers)
6252 (save-rec-ref-ids c-record-ref-identifiers))
6253
6254 (while (c-forward-annotation)
6255 (c-forward-syntactic-ws))
6256
6257 ;; Check for a type. Unknown symbols are treated as possible
6258 ;; types, but they could also be specifiers disguised through
6259 ;; macros like __INLINE__, so we recognize both types and known
6260 ;; specifiers after them too.
6261 (while
6262 (let* ((start (point)) kwd-sym kwd-clause-end found-type)
6263
6264 ;; Look for a specifier keyword clause.
6265 (when (looking-at c-prefix-spec-kwds-re)
6266 (if (looking-at c-typedef-key)
6267 (setq at-typedef t))
6268 (setq kwd-sym (c-keyword-sym (match-string 1)))
6269 (save-excursion
6270 (c-forward-keyword-clause 1)
6271 (setq kwd-clause-end (point))))
6272
6273 (when (setq found-type (c-forward-type t)) ; brace-block-too
6274 ;; Found a known or possible type or a prefix of a known type.
6275
6276 (when at-type
6277 ;; Got two identifiers with nothing but whitespace
6278 ;; between them. That can only happen in declarations.
6279 (setq at-decl-or-cast 'ids)
6280
6281 (when (eq at-type 'found)
6282 ;; If the previous identifier is a found type we
6283 ;; record it as a real one; it might be some sort of
6284 ;; alias for a prefix like "unsigned".
6285 (save-excursion
6286 (goto-char type-start)
6287 (let ((c-promote-possible-types t))
6288 (c-forward-type)))))
6289
6290 (setq backup-at-type at-type
6291 backup-type-start type-start
6292 backup-id-start id-start
6293 at-type found-type
6294 type-start start
6295 id-start (point)
6296 ;; The previous ambiguous specifier/type turned out
6297 ;; to be a type since we've parsed another one after
6298 ;; it, so clear these backup flags.
6299 backup-at-type-decl nil
6300 backup-maybe-typeless nil))
6301
6302 (if kwd-sym
6303 (progn
6304 ;; Handle known specifier keywords and
6305 ;; `c-decl-hangon-kwds' which can occur after known
6306 ;; types.
6307
6308 (if (c-keyword-member kwd-sym 'c-decl-hangon-kwds)
6309 ;; It's a hang-on keyword that can occur anywhere.
6310 (progn
6311 (setq at-decl-or-cast t)
6312 (if at-type
6313 ;; Move the identifier start position if
6314 ;; we've passed a type.
6315 (setq id-start kwd-clause-end)
6316 ;; Otherwise treat this as a specifier and
6317 ;; move the fallback position.
6318 (setq start-pos kwd-clause-end))
6319 (goto-char kwd-clause-end))
6320
6321 ;; It's an ordinary specifier so we know that
6322 ;; anything before this can't be the type.
6323 (setq backup-at-type nil
6324 start-pos kwd-clause-end)
6325
6326 (if found-type
6327 ;; It's ambiguous whether this keyword is a
6328 ;; specifier or a type prefix, so set the backup
6329 ;; flags. (It's assumed that `c-forward-type'
6330 ;; moved further than `c-forward-keyword-clause'.)
6331 (progn
6332 (when (c-keyword-member kwd-sym 'c-typedef-decl-kwds)
6333 (setq backup-at-type-decl t))
6334 (when (c-keyword-member kwd-sym 'c-typeless-decl-kwds)
6335 (setq backup-maybe-typeless t)))
6336
6337 (when (c-keyword-member kwd-sym 'c-typedef-decl-kwds)
6338 ;; This test only happens after we've scanned a type.
6339 ;; So, with valid syntax, kwd-sym can't be 'typedef.
6340 (setq at-type-decl t))
6341 (when (c-keyword-member kwd-sym 'c-typeless-decl-kwds)
6342 (setq maybe-typeless t))
6343
6344 ;; Haven't matched a type so it's an umambiguous
6345 ;; specifier keyword and we know we're in a
6346 ;; declaration.
6347 (setq at-decl-or-cast t)
6348
6349 (goto-char kwd-clause-end))))
6350
6351 ;; If the type isn't known we continue so that we'll jump
6352 ;; over all specifiers and type identifiers. The reason
6353 ;; to do this for a known type prefix is to make things
6354 ;; like "unsigned INT16" work.
6355 (and found-type (not (eq found-type t))))))
6356
6357 (cond
6358 ((eq at-type t)
6359 ;; If a known type was found, we still need to skip over any
6360 ;; hangon keyword clauses after it. Otherwise it has already
6361 ;; been done in the loop above.
6362 (while (looking-at c-decl-hangon-key)
6363 (c-forward-keyword-clause 1))
6364 (setq id-start (point)))
6365
6366 ((eq at-type 'prefix)
6367 ;; A prefix type is itself a primitive type when it's not
6368 ;; followed by another type.
6369 (setq at-type t))
6370
6371 ((not at-type)
6372 ;; Got no type but set things up to continue anyway to handle
6373 ;; the various cases when a declaration doesn't start with a
6374 ;; type.
6375 (setq id-start start-pos))
6376
6377 ((and (eq at-type 'maybe)
6378 (c-major-mode-is 'c++-mode))
6379 ;; If it's C++ then check if the last "type" ends on the form
6380 ;; "foo::foo" or "foo::~foo", i.e. if it's the name of a
6381 ;; (con|de)structor.
6382 (save-excursion
6383 (let (name end-2 end-1)
6384 (goto-char id-start)
6385 (c-backward-syntactic-ws)
6386 (setq end-2 (point))
6387 (when (and
6388 (c-simple-skip-symbol-backward)
6389 (progn
6390 (setq name
6391 (buffer-substring-no-properties (point) end-2))
6392 ;; Cheating in the handling of syntactic ws below.
6393 (< (skip-chars-backward ":~ \t\n\r\v\f") 0))
6394 (progn
6395 (setq end-1 (point))
6396 (c-simple-skip-symbol-backward))
6397 (>= (point) type-start)
6398 (equal (buffer-substring-no-properties (point) end-1)
6399 name))
6400 ;; It is a (con|de)structor name. In that case the
6401 ;; declaration is typeless so zap out any preceding
6402 ;; identifier(s) that we might have taken as types.
6403 (goto-char type-start)
6404 (setq at-type nil
6405 backup-at-type nil
6406 id-start type-start))))))
6407
6408 ;; Check for and step over a type decl expression after the thing
6409 ;; that is or might be a type. This can't be skipped since we
6410 ;; need the correct end position of the declarator for
6411 ;; `max-type-decl-end-*'.
6412 (let ((start (point)) (paren-depth 0) pos
6413 ;; True if there's a non-open-paren match of
6414 ;; `c-type-decl-prefix-key'.
6415 got-prefix
6416 ;; True if the declarator is surrounded by a parenthesis pair.
6417 got-parens
6418 ;; True if there is an identifier in the declarator.
6419 got-identifier
6420 ;; True if there's a non-close-paren match of
6421 ;; `c-type-decl-suffix-key'.
6422 got-suffix
6423 ;; True if there's a prefix match outside the outermost
6424 ;; paren pair that surrounds the declarator.
6425 got-prefix-before-parens
6426 ;; True if there's a suffix match outside the outermost
6427 ;; paren pair that surrounds the declarator. The value is
6428 ;; the position of the first suffix match.
6429 got-suffix-after-parens
6430 ;; True if we've parsed the type decl to a token that is
6431 ;; known to end declarations in this context.
6432 at-decl-end
6433 ;; The earlier values of `at-type' and `type-start' if we've
6434 ;; shifted the type backwards.
6435 identifier-type identifier-start
6436 ;; If `c-parse-and-markup-<>-arglists' is set we need to
6437 ;; turn it off during the name skipping below to avoid
6438 ;; getting `c-type' properties that might be bogus. That
6439 ;; can happen since we don't know if
6440 ;; `c-restricted-<>-arglists' will be correct inside the
6441 ;; arglist paren that gets entered.
6442 c-parse-and-markup-<>-arglists)
6443
6444 (goto-char id-start)
6445
6446 ;; Skip over type decl prefix operators. (Note similar code in
6447 ;; `c-font-lock-declarators'.)
6448 (while (and (looking-at c-type-decl-prefix-key)
6449 (if (and (c-major-mode-is 'c++-mode)
6450 (match-beginning 2))
6451 ;; If the second submatch matches in C++ then
6452 ;; we're looking at an identifier that's a
6453 ;; prefix only if it specifies a member pointer.
6454 (when (setq got-identifier (c-forward-name))
6455 (if (looking-at "\\(::\\)")
6456 ;; We only check for a trailing "::" and
6457 ;; let the "*" that should follow be
6458 ;; matched in the next round.
6459 (progn (setq got-identifier nil) t)
6460 ;; It turned out to be the real identifier,
6461 ;; so stop.
6462 nil))
6463 t))
6464
6465 (if (eq (char-after) ?\()
6466 (progn
6467 (setq paren-depth (1+ paren-depth))
6468 (forward-char))
6469 (unless got-prefix-before-parens
6470 (setq got-prefix-before-parens (= paren-depth 0)))
6471 (setq got-prefix t)
6472 (goto-char (match-end 1)))
6473 (c-forward-syntactic-ws))
6474
6475 (setq got-parens (> paren-depth 0))
6476
6477 ;; Skip over an identifier.
6478 (or got-identifier
6479 (and (looking-at c-identifier-start)
6480 (setq got-identifier (c-forward-name))))
6481
6482 ;; Skip over type decl suffix operators.
6483 (while (if (looking-at c-type-decl-suffix-key)
6484
6485 (if (eq (char-after) ?\))
6486 (when (> paren-depth 0)
6487 (setq paren-depth (1- paren-depth))
6488 (forward-char)
6489 t)
6490 (when (if (save-match-data (looking-at "\\s\("))
6491 (c-safe (c-forward-sexp 1) t)
6492 (goto-char (match-end 1))
6493 t)
6494 (when (and (not got-suffix-after-parens)
6495 (= paren-depth 0))
6496 (setq got-suffix-after-parens (match-beginning 0)))
6497 (setq got-suffix t)))
6498
6499 ;; No suffix matched. We might have matched the
6500 ;; identifier as a type and the open paren of a
6501 ;; function arglist as a type decl prefix. In that
6502 ;; case we should "backtrack": Reinterpret the last
6503 ;; type as the identifier, move out of the arglist and
6504 ;; continue searching for suffix operators.
6505 ;;
6506 ;; Do this even if there's no preceding type, to cope
6507 ;; with old style function declarations in K&R C,
6508 ;; (con|de)structors in C++ and `c-typeless-decl-kwds'
6509 ;; style declarations. That isn't applicable in an
6510 ;; arglist context, though.
6511 (when (and (= paren-depth 1)
6512 (not got-prefix-before-parens)
6513 (not (eq at-type t))
6514 (or backup-at-type
6515 maybe-typeless
6516 backup-maybe-typeless
6517 (when c-recognize-typeless-decls
6518 (not context)))
6519 (setq pos (c-up-list-forward (point)))
6520 (eq (char-before pos) ?\)))
6521 (c-fdoc-shift-type-backward)
6522 (goto-char pos)
6523 t))
6524
6525 (c-forward-syntactic-ws))
6526
6527 (when (and (or maybe-typeless backup-maybe-typeless)
6528 (not got-identifier)
6529 (not got-prefix)
6530 at-type)
6531 ;; Have found no identifier but `c-typeless-decl-kwds' has
6532 ;; matched so we know we're inside a declaration. The
6533 ;; preceding type must be the identifier instead.
6534 (c-fdoc-shift-type-backward))
6535
6536 (setq
6537 at-decl-or-cast
6538 (catch 'at-decl-or-cast
6539
6540 ;; CASE 1
6541 (when (> paren-depth 0)
6542 ;; Encountered something inside parens that isn't matched by
6543 ;; the `c-type-decl-*' regexps, so it's not a type decl
6544 ;; expression. Try to skip out to the same paren depth to
6545 ;; not confuse the cast check below.
6546 (c-safe (goto-char (scan-lists (point) 1 paren-depth)))
6547 ;; If we've found a specifier keyword then it's a
6548 ;; declaration regardless.
6549 (throw 'at-decl-or-cast (eq at-decl-or-cast t)))
6550
6551 (setq at-decl-end
6552 (looking-at (cond ((eq context '<>) "[,>]")
6553 (context "[,\)]")
6554 (t "[,;]"))))
6555
6556 ;; Now we've collected info about various characteristics of
6557 ;; the construct we're looking at. Below follows a decision
6558 ;; tree based on that. It's ordered to check more certain
6559 ;; signs before less certain ones.
6560
6561 (if got-identifier
6562 (progn
6563
6564 ;; CASE 2
6565 (when (and (or at-type maybe-typeless)
6566 (not (or got-prefix got-parens)))
6567 ;; Got another identifier directly after the type, so it's a
6568 ;; declaration.
6569 (throw 'at-decl-or-cast t))
6570
6571 (when (and got-parens
6572 (not got-prefix)
6573 (not got-suffix-after-parens)
6574 (or backup-at-type
6575 maybe-typeless
6576 backup-maybe-typeless))
6577 ;; Got a declaration of the form "foo bar (gnu);" where we've
6578 ;; recognized "bar" as the type and "gnu" as the declarator.
6579 ;; In this case it's however more likely that "bar" is the
6580 ;; declarator and "gnu" a function argument or initializer (if
6581 ;; `c-recognize-paren-inits' is set), since the parens around
6582 ;; "gnu" would be superfluous if it's a declarator. Shift the
6583 ;; type one step backward.
6584 (c-fdoc-shift-type-backward)))
6585
6586 ;; Found no identifier.
6587
6588 (if backup-at-type
6589 (progn
6590
6591 ;; CASE 3
6592 (when (= (point) start)
6593 ;; Got a plain list of identifiers. If a colon follows it's
6594 ;; a valid label. Otherwise the last one probably is the
6595 ;; declared identifier and we should back up to the previous
6596 ;; type, providing it isn't a cast.
6597 (if (and (eq (char-after) ?:)
6598 (not (c-major-mode-is 'java-mode)))
6599 ;; If we've found a specifier keyword then it's a
6600 ;; declaration regardless.
6601 (throw 'at-decl-or-cast (eq at-decl-or-cast t))
6602 (setq backup-if-not-cast t)
6603 (throw 'at-decl-or-cast t)))
6604
6605 ;; CASE 4
6606 (when (and got-suffix
6607 (not got-prefix)
6608 (not got-parens))
6609 ;; Got a plain list of identifiers followed by some suffix.
6610 ;; If this isn't a cast then the last identifier probably is
6611 ;; the declared one and we should back up to the previous
6612 ;; type.
6613 (setq backup-if-not-cast t)
6614 (throw 'at-decl-or-cast t)))
6615
6616 ;; CASE 5
6617 (when (eq at-type t)
6618 ;; If the type is known we know that there can't be any
6619 ;; identifier somewhere else, and it's only in declarations in
6620 ;; e.g. function prototypes and in casts that the identifier may
6621 ;; be left out.
6622 (throw 'at-decl-or-cast t))
6623
6624 (when (= (point) start)
6625 ;; Only got a single identifier (parsed as a type so far).
6626 ;; CASE 6
6627 (if (and
6628 ;; Check that the identifier isn't at the start of an
6629 ;; expression.
6630 at-decl-end
6631 (cond
6632 ((eq context 'decl)
6633 ;; Inside an arglist that contains declarations. If K&R
6634 ;; style declarations and parenthesis style initializers
6635 ;; aren't allowed then the single identifier must be a
6636 ;; type, else we require that it's known or found
6637 ;; (primitive types are handled above).
6638 (or (and (not c-recognize-knr-p)
6639 (not c-recognize-paren-inits))
6640 (memq at-type '(known found))))
6641 ((eq context '<>)
6642 ;; Inside a template arglist. Accept known and found
6643 ;; types; other identifiers could just as well be
6644 ;; constants in C++.
6645 (memq at-type '(known found)))))
6646 (throw 'at-decl-or-cast t)
6647 ;; CASE 7
6648 ;; Can't be a valid declaration or cast, but if we've found a
6649 ;; specifier it can't be anything else either, so treat it as
6650 ;; an invalid/unfinished declaration or cast.
6651 (throw 'at-decl-or-cast at-decl-or-cast))))
6652
6653 (if (and got-parens
6654 (not got-prefix)
6655 (not context)
6656 (not (eq at-type t))
6657 (or backup-at-type
6658 maybe-typeless
6659 backup-maybe-typeless
6660 (when c-recognize-typeless-decls
6661 (or (not got-suffix)
6662 (not (looking-at
6663 c-after-suffixed-type-maybe-decl-key))))))
6664 ;; Got an empty paren pair and a preceding type that probably
6665 ;; really is the identifier. Shift the type backwards to make
6666 ;; the last one the identifier. This is analogous to the
6667 ;; "backtracking" done inside the `c-type-decl-suffix-key' loop
6668 ;; above.
6669 ;;
6670 ;; Exception: In addition to the conditions in that
6671 ;; "backtracking" code, do not shift backward if we're not
6672 ;; looking at either `c-after-suffixed-type-decl-key' or "[;,]".
6673 ;; Since there's no preceding type, the shift would mean that
6674 ;; the declaration is typeless. But if the regexp doesn't match
6675 ;; then we will simply fall through in the tests below and not
6676 ;; recognize it at all, so it's better to try it as an abstract
6677 ;; declarator instead.
6678 (c-fdoc-shift-type-backward)
6679
6680 ;; Still no identifier.
6681 ;; CASE 8
6682 (when (and got-prefix (or got-parens got-suffix))
6683 ;; Require `got-prefix' together with either `got-parens' or
6684 ;; `got-suffix' to recognize it as an abstract declarator:
6685 ;; `got-parens' only is probably an empty function call.
6686 ;; `got-suffix' only can build an ordinary expression together
6687 ;; with the preceding identifier which we've taken as a type.
6688 ;; We could actually accept on `got-prefix' only, but that can
6689 ;; easily occur temporarily while writing an expression so we
6690 ;; avoid that case anyway. We could do a better job if we knew
6691 ;; the point when the fontification was invoked.
6692 (throw 'at-decl-or-cast t))
6693
6694 ;; CASE 9
6695 (when (and at-type
6696 (not got-prefix)
6697 (not got-parens)
6698 got-suffix-after-parens
6699 (eq (char-after got-suffix-after-parens) ?\())
6700 ;; Got a type, no declarator but a paren suffix. I.e. it's a
6701 ;; normal function call afterall (or perhaps a C++ style object
6702 ;; instantiation expression).
6703 (throw 'at-decl-or-cast nil))))
6704
6705 ;; CASE 10
6706 (when at-decl-or-cast
6707 ;; By now we've located the type in the declaration that we know
6708 ;; we're in.
6709 (throw 'at-decl-or-cast t))
6710
6711 ;; CASE 11
6712 (when (and got-identifier
6713 (not context)
6714 (looking-at c-after-suffixed-type-decl-key)
6715 (if (and got-parens
6716 (not got-prefix)
6717 (not got-suffix)
6718 (not (eq at-type t)))
6719 ;; Shift the type backward in the case that there's a
6720 ;; single identifier inside parens. That can only
6721 ;; occur in K&R style function declarations so it's
6722 ;; more likely that it really is a function call.
6723 ;; Therefore we only do this after
6724 ;; `c-after-suffixed-type-decl-key' has matched.
6725 (progn (c-fdoc-shift-type-backward) t)
6726 got-suffix-after-parens))
6727 ;; A declaration according to `c-after-suffixed-type-decl-key'.
6728 (throw 'at-decl-or-cast t))
6729
6730 ;; CASE 12
6731 (when (and (or got-prefix (not got-parens))
6732 (memq at-type '(t known)))
6733 ;; It's a declaration if a known type precedes it and it can't be a
6734 ;; function call.
6735 (throw 'at-decl-or-cast t))
6736
6737 ;; If we get here we can't tell if this is a type decl or a normal
6738 ;; expression by looking at it alone. (That's under the assumption
6739 ;; that normal expressions always can look like type decl expressions,
6740 ;; which isn't really true but the cases where it doesn't hold are so
6741 ;; uncommon (e.g. some placements of "const" in C++) it's not worth
6742 ;; the effort to look for them.)
6743
6744 (unless (or at-decl-end (looking-at "=[^=]"))
6745 ;; If this is a declaration it should end here or its initializer(*)
6746 ;; should start here, so check for allowed separation tokens. Note
6747 ;; that this rule doesn't work e.g. with a K&R arglist after a
6748 ;; function header.
6749 ;;
6750 ;; *) Don't check for C++ style initializers using parens
6751 ;; since those already have been matched as suffixes.
6752 ;;
6753 ;; If `at-decl-or-cast' is then we've found some other sign that
6754 ;; it's a declaration or cast, so then it's probably an
6755 ;; invalid/unfinished one.
6756 (throw 'at-decl-or-cast at-decl-or-cast))
6757
6758 ;; Below are tests that only should be applied when we're certain to
6759 ;; not have parsed halfway through an expression.
6760
6761 ;; CASE 14
6762 (when (memq at-type '(t known))
6763 ;; The expression starts with a known type so treat it as a
6764 ;; declaration.
6765 (throw 'at-decl-or-cast t))
6766
6767 ;; CASE 15
6768 (when (and (c-major-mode-is 'c++-mode)
6769 ;; In C++ we check if the identifier is a known type, since
6770 ;; (con|de)structors use the class name as identifier.
6771 ;; We've always shifted over the identifier as a type and
6772 ;; then backed up again in this case.
6773 identifier-type
6774 (or (memq identifier-type '(found known))
6775 (and (eq (char-after identifier-start) ?~)
6776 ;; `at-type' probably won't be 'found for
6777 ;; destructors since the "~" is then part of the
6778 ;; type name being checked against the list of
6779 ;; known types, so do a check without that
6780 ;; operator.
6781 (or (save-excursion
6782 (goto-char (1+ identifier-start))
6783 (c-forward-syntactic-ws)
6784 (c-with-syntax-table
6785 c-identifier-syntax-table
6786 (looking-at c-known-type-key)))
6787 (save-excursion
6788 (goto-char (1+ identifier-start))
6789 ;; We have already parsed the type earlier,
6790 ;; so it'd be possible to cache the end
6791 ;; position instead of redoing it here, but
6792 ;; then we'd need to keep track of another
6793 ;; position everywhere.
6794 (c-check-type (point)
6795 (progn (c-forward-type)
6796 (point))))))))
6797 (throw 'at-decl-or-cast t))
6798
6799 (if got-identifier
6800 (progn
6801 ;; CASE 16
6802 (when (and got-prefix-before-parens
6803 at-type
6804 (or at-decl-end (looking-at "=[^=]"))
6805 (not context)
6806 (not got-suffix))
6807 ;; Got something like "foo * bar;". Since we're not inside an
6808 ;; arglist it would be a meaningless expression because the
6809 ;; result isn't used. We therefore choose to recognize it as
6810 ;; a declaration. Do not allow a suffix since it could then
6811 ;; be a function call.
6812 (throw 'at-decl-or-cast t))
6813
6814 ;; CASE 17
6815 (when (and (or got-suffix-after-parens
6816 (looking-at "=[^=]"))
6817 (eq at-type 'found)
6818 (not (eq context 'arglist)))
6819 ;; Got something like "a (*b) (c);" or "a (b) = c;". It could
6820 ;; be an odd expression or it could be a declaration. Treat
6821 ;; it as a declaration if "a" has been used as a type
6822 ;; somewhere else (if it's a known type we won't get here).
6823 (throw 'at-decl-or-cast t)))
6824
6825 ;; CASE 18
6826 (when (and context
6827 (or got-prefix
6828 (and (eq context 'decl)
6829 (not c-recognize-paren-inits)
6830 (or got-parens got-suffix))))
6831 ;; Got a type followed by an abstract declarator. If `got-prefix'
6832 ;; is set it's something like "a *" without anything after it. If
6833 ;; `got-parens' or `got-suffix' is set it's "a()", "a[]", "a()[]",
6834 ;; or similar, which we accept only if the context rules out
6835 ;; expressions.
6836 (throw 'at-decl-or-cast t)))
6837
6838 ;; If we had a complete symbol table here (which rules out
6839 ;; `c-found-types') we should return t due to the disambiguation rule
6840 ;; (in at least C++) that anything that can be parsed as a declaration
6841 ;; is a declaration. Now we're being more defensive and prefer to
6842 ;; highlight things like "foo (bar);" as a declaration only if we're
6843 ;; inside an arglist that contains declarations.
6844 (eq context 'decl))))
6845
6846 ;; The point is now after the type decl expression.
6847
6848 (cond
6849 ;; Check for a cast.
6850 ((save-excursion
6851 (and
6852 c-cast-parens
6853
6854 ;; Should be the first type/identifier in a cast paren.
6855 (> preceding-token-end (point-min))
6856 (memq (char-before preceding-token-end) c-cast-parens)
6857
6858 ;; The closing paren should follow.
6859 (progn
6860 (c-forward-syntactic-ws)
6861 (looking-at "\\s\)"))
6862
6863 ;; There should be a primary expression after it.
6864 (let (pos)
6865 (forward-char)
6866 (c-forward-syntactic-ws)
6867 (setq cast-end (point))
6868 (and (looking-at c-primary-expr-regexp)
6869 (progn
6870 (setq pos (match-end 0))
6871 (or
6872 ;; Check if the expression begins with a prefix keyword.
6873 (match-beginning 2)
6874 (if (match-beginning 1)
6875 ;; Expression begins with an ambiguous operator. Treat
6876 ;; it as a cast if it's a type decl or if we've
6877 ;; recognized the type somewhere else.
6878 (or at-decl-or-cast
6879 (memq at-type '(t known found)))
6880 ;; Unless it's a keyword, it's the beginning of a primary
6881 ;; expression.
6882 (not (looking-at c-keywords-regexp)))))
6883 ;; If `c-primary-expr-regexp' matched a nonsymbol token, check
6884 ;; that it matched a whole one so that we don't e.g. confuse
6885 ;; the operator '-' with '->'. It's ok if it matches further,
6886 ;; though, since it e.g. can match the float '.5' while the
6887 ;; operator regexp only matches '.'.
6888 (or (not (looking-at c-nonsymbol-token-regexp))
6889 (<= (match-end 0) pos))))
6890
6891 ;; There should either be a cast before it or something that isn't an
6892 ;; identifier or close paren.
6893 (> preceding-token-end (point-min))
6894 (progn
6895 (goto-char (1- preceding-token-end))
6896 (or (eq (point) last-cast-end)
6897 (progn
6898 (c-backward-syntactic-ws)
6899 (if (< (skip-syntax-backward "w_") 0)
6900 ;; It's a symbol. Accept it only if it's one of the
6901 ;; keywords that can precede an expression (without
6902 ;; surrounding parens).
6903 (looking-at c-simple-stmt-key)
6904 (and
6905 ;; Check that it isn't a close paren (block close is ok,
6906 ;; though).
6907 (not (memq (char-before) '(?\) ?\])))
6908 ;; Check that it isn't a nonsymbol identifier.
6909 (not (c-on-identifier)))))))))
6910
6911 ;; Handle the cast.
6912 (when (and c-record-type-identifiers at-type (not (eq at-type t)))
6913 (let ((c-promote-possible-types t))
6914 (goto-char type-start)
6915 (c-forward-type)))
6916
6917 (goto-char cast-end)
6918 'cast)
6919
6920 (at-decl-or-cast
6921 ;; We're at a declaration. Highlight the type and the following
6922 ;; declarators.
6923
6924 (when backup-if-not-cast
6925 (c-fdoc-shift-type-backward t))
6926
6927 (when (and (eq context 'decl) (looking-at ","))
6928 ;; Make sure to propagate the `c-decl-arg-start' property to
6929 ;; the next argument if it's set in this one, to cope with
6930 ;; interactive refontification.
6931 (c-put-c-type-property (point) 'c-decl-arg-start))
6932
6933 (when (and c-record-type-identifiers at-type (not (eq at-type t)))
6934 (let ((c-promote-possible-types t))
6935 (save-excursion
6936 (goto-char type-start)
6937 (c-forward-type))))
6938
6939 (cons id-start
6940 (and (or at-type-decl at-typedef)
6941 (cons at-type-decl at-typedef))))
6942
6943 (t
6944 ;; False alarm. Restore the recorded ranges.
6945 (setq c-record-type-identifiers save-rec-type-ids
6946 c-record-ref-identifiers save-rec-ref-ids)
6947 nil))))
6948
6949 (defun c-forward-label (&optional assume-markup preceding-token-end limit)
6950 ;; Assuming that point is at the beginning of a token, check if it starts a
6951 ;; label and if so move over it and return non-nil (t in default situations,
6952 ;; specific symbols (see below) for interesting situations), otherwise don't
6953 ;; move and return nil. "Label" here means "most things with a colon".
6954 ;;
6955 ;; More precisely, a "label" is regarded as one of:
6956 ;; (i) a goto target like "foo:" - returns the symbol `goto-target';
6957 ;; (ii) A case label - either the entire construct "case FOO:", or just the
6958 ;; bare "case", should the colon be missing. We return t;
6959 ;; (iii) a keyword which needs a colon, like "default:" or "private:"; We
6960 ;; return t;
6961 ;; (iv) One of QT's "extended" C++ variants of
6962 ;; "private:"/"protected:"/"public:"/"more:" looking like "public slots:".
6963 ;; Returns the symbol `qt-2kwds-colon'.
6964 ;; (v) QT's construct "signals:". Returns the symbol `qt-1kwd-colon'.
6965 ;; (vi) One of the keywords matched by `c-opt-extra-label-key' (without any
6966 ;; colon). Currently (2006-03), this applies only to Objective C's
6967 ;; keywords "@private", "@protected", and "@public". Returns t.
6968 ;;
6969 ;; One of the things which will NOT be recognised as a label is a bit-field
6970 ;; element of a struct, something like "int foo:5".
6971 ;;
6972 ;; The end of the label is taken to be just after the colon, or the end of
6973 ;; the first submatch in `c-opt-extra-label-key'. The point is directly
6974 ;; after the end on return. The terminating char gets marked with
6975 ;; `c-decl-end' to improve recognition of the following declaration or
6976 ;; statement.
6977 ;;
6978 ;; If ASSUME-MARKUP is non-nil, it's assumed that the preceding
6979 ;; label, if any, has already been marked up like that.
6980 ;;
6981 ;; If PRECEDING-TOKEN-END is given, it should be the first position
6982 ;; after the preceding token, i.e. on the other side of the
6983 ;; syntactic ws from the point. Use a value less than or equal to
6984 ;; (point-min) if the point is at the first token in (the visible
6985 ;; part of) the buffer.
6986 ;;
6987 ;; The optional LIMIT limits the forward scan for the colon.
6988 ;;
6989 ;; This function records the ranges of the label symbols on
6990 ;; `c-record-ref-identifiers' if `c-record-type-identifiers' (!) is
6991 ;; non-nil.
6992 ;;
6993 ;; This function might do hidden buffer changes.
6994
6995 (let ((start (point))
6996 label-end
6997 qt-symbol-idx
6998 macro-start ; if we're in one.
6999 label-type
7000 kwd)
7001 (cond
7002 ;; "case" or "default" (Doesn't apply to AWK).
7003 ((looking-at c-label-kwds-regexp)
7004 (let ((kwd-end (match-end 1)))
7005 ;; Record only the keyword itself for fontification, since in
7006 ;; case labels the following is a constant expression and not
7007 ;; a label.
7008 (when c-record-type-identifiers
7009 (c-record-ref-id (cons (match-beginning 1) kwd-end)))
7010
7011 ;; Find the label end.
7012 (goto-char kwd-end)
7013 (setq label-type
7014 (if (and (c-syntactic-re-search-forward
7015 ;; Stop on chars that aren't allowed in expressions,
7016 ;; and on operator chars that would be meaningless
7017 ;; there. FIXME: This doesn't cope with ?: operators.
7018 "[;{=,@]\\|\\(\\=\\|[^:]\\):\\([^:]\\|\\'\\)"
7019 limit t t nil 1)
7020 (match-beginning 2))
7021
7022 (progn ; there's a proper :
7023 (goto-char (match-beginning 2)) ; just after the :
7024 (c-put-c-type-property (1- (point)) 'c-decl-end)
7025 t)
7026
7027 ;; It's an unfinished label. We consider the keyword enough
7028 ;; to recognize it as a label, so that it gets fontified.
7029 ;; Leave the point at the end of it, but don't put any
7030 ;; `c-decl-end' marker.
7031 (goto-char kwd-end)
7032 t))))
7033
7034 ;; @private, @protected, @public, in Objective C, or similar.
7035 ((and c-opt-extra-label-key
7036 (looking-at c-opt-extra-label-key))
7037 ;; For a `c-opt-extra-label-key' match, we record the whole
7038 ;; thing for fontification. That's to get the leading '@' in
7039 ;; Objective-C protection labels fontified.
7040 (goto-char (match-end 1))
7041 (when c-record-type-identifiers
7042 (c-record-ref-id (cons (match-beginning 1) (point))))
7043 (c-put-c-type-property (1- (point)) 'c-decl-end)
7044 (setq label-type t))
7045
7046 ;; All other cases of labels.
7047 ((and c-recognize-colon-labels ; nil for AWK and IDL, otherwise t.
7048
7049 ;; A colon label must have something before the colon.
7050 (not (eq (char-after) ?:))
7051
7052 ;; Check that we're not after a token that can't precede a label.
7053 (or
7054 ;; Trivially succeeds when there's no preceding token.
7055 (if preceding-token-end
7056 (<= preceding-token-end (point-min))
7057 (save-excursion
7058 (c-backward-syntactic-ws)
7059 (setq preceding-token-end (point))
7060 (bobp)))
7061
7062 ;; Check if we're after a label, if we're after a closing
7063 ;; paren that belong to statement, and with
7064 ;; `c-label-prefix-re'. It's done in different order
7065 ;; depending on `assume-markup' since the checks have
7066 ;; different expensiveness.
7067 (if assume-markup
7068 (or
7069 (eq (c-get-char-property (1- preceding-token-end) 'c-type)
7070 'c-decl-end)
7071
7072 (save-excursion
7073 (goto-char (1- preceding-token-end))
7074 (c-beginning-of-current-token)
7075 (or (looking-at c-label-prefix-re)
7076 (looking-at c-block-stmt-1-key)))
7077
7078 (and (eq (char-before preceding-token-end) ?\))
7079 (c-after-conditional)))
7080
7081 (or
7082 (save-excursion
7083 (goto-char (1- preceding-token-end))
7084 (c-beginning-of-current-token)
7085 (or (looking-at c-label-prefix-re)
7086 (looking-at c-block-stmt-1-key)))
7087
7088 (cond
7089 ((eq (char-before preceding-token-end) ?\))
7090 (c-after-conditional))
7091
7092 ((eq (char-before preceding-token-end) ?:)
7093 ;; Might be after another label, so check it recursively.
7094 (save-restriction
7095 (save-excursion
7096 (goto-char (1- preceding-token-end))
7097 ;; Essentially the same as the
7098 ;; `c-syntactic-re-search-forward' regexp below.
7099 (setq macro-start
7100 (save-excursion (and (c-beginning-of-macro)
7101 (point))))
7102 (if macro-start (narrow-to-region macro-start (point-max)))
7103 (c-syntactic-skip-backward "^-]:?;}=*/%&|,<>!@+" nil t)
7104 ;; Note: the following should work instead of the
7105 ;; narrow-to-region above. Investigate why not,
7106 ;; sometime. ACM, 2006-03-31.
7107 ;; (c-syntactic-skip-backward "^-]:?;}=*/%&|,<>!@+"
7108 ;; macro-start t)
7109 (let ((pte (point))
7110 ;; If the caller turned on recording for us,
7111 ;; it shouldn't apply when we check the
7112 ;; preceding label.
7113 c-record-type-identifiers)
7114 ;; A label can't start at a cpp directive. Check for
7115 ;; this, since c-forward-syntactic-ws would foul up on it.
7116 (unless (and c-opt-cpp-prefix (looking-at c-opt-cpp-prefix))
7117 (c-forward-syntactic-ws)
7118 (c-forward-label nil pte start))))))))))
7119
7120 ;; Point is still at the beginning of the possible label construct.
7121 ;;
7122 ;; Check that the next nonsymbol token is ":", or that we're in one
7123 ;; of QT's "slots" declarations. Allow '(' for the sake of macro
7124 ;; arguments. FIXME: Should build this regexp from the language
7125 ;; constants.
7126 (cond
7127 ;; public: protected: private:
7128 ((and
7129 (c-major-mode-is 'c++-mode)
7130 (search-forward-regexp
7131 "\\=p\\(r\\(ivate\\|otected\\)\\|ublic\\)\\>[^_]" nil t)
7132 (progn (backward-char)
7133 (c-forward-syntactic-ws limit)
7134 (looking-at ":\\([^:]\\|\\'\\)"))) ; A single colon.
7135 (forward-char)
7136 (setq label-type t))
7137 ;; QT double keyword like "protected slots:" or goto target.
7138 ((progn (goto-char start) nil))
7139 ((when (c-syntactic-re-search-forward
7140 "[ \t\n[:?;{=*/%&|,<>!@+-]" limit t t) ; not at EOB
7141 (backward-char)
7142 (setq label-end (point))
7143 (setq qt-symbol-idx
7144 (and (c-major-mode-is 'c++-mode)
7145 (string-match
7146 "\\(p\\(r\\(ivate\\|otected\\)\\|ublic\\)\\|more\\)\\>"
7147 (buffer-substring start (point)))))
7148 (c-forward-syntactic-ws limit)
7149 (cond
7150 ((looking-at ":\\([^:]\\|\\'\\)") ; A single colon.
7151 (forward-char)
7152 (setq label-type
7153 (if (or (string= "signals" ; Special QT macro
7154 (setq kwd (buffer-substring-no-properties start label-end)))
7155 (string= "Q_SIGNALS" kwd))
7156 'qt-1kwd-colon
7157 'goto-target)))
7158 ((and qt-symbol-idx
7159 (search-forward-regexp "\\=\\(slots\\|Q_SLOTS\\)\\>" limit t)
7160 (progn (c-forward-syntactic-ws limit)
7161 (looking-at ":\\([^:]\\|\\'\\)"))) ; A single colon
7162 (forward-char)
7163 (setq label-type 'qt-2kwds-colon)))))))
7164
7165 (save-restriction
7166 (narrow-to-region start (point))
7167
7168 ;; Check that `c-nonlabel-token-key' doesn't match anywhere.
7169 (catch 'check-label
7170 (goto-char start)
7171 (while (progn
7172 (when (looking-at c-nonlabel-token-key)
7173 (goto-char start)
7174 (setq label-type nil)
7175 (throw 'check-label nil))
7176 (and (c-safe (c-forward-sexp)
7177 (c-forward-syntactic-ws)
7178 t)
7179 (not (eobp)))))
7180
7181 ;; Record the identifiers in the label for fontification, unless
7182 ;; it begins with `c-label-kwds' in which case the following
7183 ;; identifiers are part of a (constant) expression that
7184 ;; shouldn't be fontified.
7185 (when (and c-record-type-identifiers
7186 (progn (goto-char start)
7187 (not (looking-at c-label-kwds-regexp))))
7188 (while (c-syntactic-re-search-forward c-symbol-key nil t)
7189 (c-record-ref-id (cons (match-beginning 0)
7190 (match-end 0)))))
7191
7192 (c-put-c-type-property (1- (point-max)) 'c-decl-end)
7193 (goto-char (point-max)))))
7194
7195 (t
7196 ;; Not a label.
7197 (goto-char start)))
7198 label-type))
7199
7200 (defun c-forward-objc-directive ()
7201 ;; Assuming the point is at the beginning of a token, try to move
7202 ;; forward to the end of the Objective-C directive that starts
7203 ;; there. Return t if a directive was fully recognized, otherwise
7204 ;; the point is moved as far as one could be successfully parsed and
7205 ;; nil is returned.
7206 ;;
7207 ;; This function records identifier ranges on
7208 ;; `c-record-type-identifiers' and `c-record-ref-identifiers' if
7209 ;; `c-record-type-identifiers' is non-nil.
7210 ;;
7211 ;; This function might do hidden buffer changes.
7212
7213 (let ((start (point))
7214 start-char
7215 (c-promote-possible-types t)
7216 ;; Turn off recognition of angle bracket arglists while parsing
7217 ;; types here since the protocol reference list might then be
7218 ;; considered part of the preceding name or superclass-name.
7219 c-recognize-<>-arglists)
7220
7221 (if (or
7222 (when (looking-at
7223 (eval-when-compile
7224 (c-make-keywords-re t
7225 (append (c-lang-const c-protection-kwds objc)
7226 '("@end"))
7227 'objc-mode)))
7228 (goto-char (match-end 1))
7229 t)
7230
7231 (and
7232 (looking-at
7233 (eval-when-compile
7234 (c-make-keywords-re t
7235 '("@interface" "@implementation" "@protocol")
7236 'objc-mode)))
7237
7238 ;; Handle the name of the class itself.
7239 (progn
7240 ; (c-forward-token-2) ; 2006/1/13 This doesn't move if the token's
7241 ; at EOB.
7242 (goto-char (match-end 0))
7243 (c-skip-ws-forward)
7244 (c-forward-type))
7245
7246 (catch 'break
7247 ;; Look for ": superclass-name" or "( category-name )".
7248 (when (looking-at "[:\(]")
7249 (setq start-char (char-after))
7250 (forward-char)
7251 (c-forward-syntactic-ws)
7252 (unless (c-forward-type) (throw 'break nil))
7253 (when (eq start-char ?\()
7254 (unless (eq (char-after) ?\)) (throw 'break nil))
7255 (forward-char)
7256 (c-forward-syntactic-ws)))
7257
7258 ;; Look for a protocol reference list.
7259 (if (eq (char-after) ?<)
7260 (let ((c-recognize-<>-arglists t)
7261 (c-parse-and-markup-<>-arglists t)
7262 c-restricted-<>-arglists)
7263 (c-forward-<>-arglist t))
7264 t))))
7265
7266 (progn
7267 (c-backward-syntactic-ws)
7268 (c-clear-c-type-property start (1- (point)) 'c-decl-end)
7269 (c-put-c-type-property (1- (point)) 'c-decl-end)
7270 t)
7271
7272 (c-clear-c-type-property start (point) 'c-decl-end)
7273 nil)))
7274
7275 (defun c-beginning-of-inheritance-list (&optional lim)
7276 ;; Go to the first non-whitespace after the colon that starts a
7277 ;; multiple inheritance introduction. Optional LIM is the farthest
7278 ;; back we should search.
7279 ;;
7280 ;; This function might do hidden buffer changes.
7281 (c-with-syntax-table c++-template-syntax-table
7282 (c-backward-token-2 0 t lim)
7283 (while (and (or (looking-at c-symbol-start)
7284 (looking-at "[<,]\\|::"))
7285 (zerop (c-backward-token-2 1 t lim))))))
7286
7287 (defun c-in-method-def-p ()
7288 ;; Return nil if we aren't in a method definition, otherwise the
7289 ;; position of the initial [+-].
7290 ;;
7291 ;; This function might do hidden buffer changes.
7292 (save-excursion
7293 (beginning-of-line)
7294 (and c-opt-method-key
7295 (looking-at c-opt-method-key)
7296 (point))
7297 ))
7298
7299 ;; Contributed by Kevin Ryde <user42@zip.com.au>.
7300 (defun c-in-gcc-asm-p ()
7301 ;; Return non-nil if point is within a gcc \"asm\" block.
7302 ;;
7303 ;; This should be called with point inside an argument list.
7304 ;;
7305 ;; Only one level of enclosing parentheses is considered, so for
7306 ;; instance `nil' is returned when in a function call within an asm
7307 ;; operand.
7308 ;;
7309 ;; This function might do hidden buffer changes.
7310
7311 (and c-opt-asm-stmt-key
7312 (save-excursion
7313 (beginning-of-line)
7314 (backward-up-list 1)
7315 (c-beginning-of-statement-1 (point-min) nil t)
7316 (looking-at c-opt-asm-stmt-key))))
7317
7318 (defun c-at-toplevel-p ()
7319 "Return a determination as to whether point is \"at the top level\".
7320 Informally, \"at the top level\" is anywhere where you can write
7321 a function.
7322
7323 More precisely, being at the top-level means that point is either
7324 outside any enclosing block (such as a function definition), or
7325 directly inside a class, namespace or other block that contains
7326 another declaration level.
7327
7328 If point is not at the top-level (e.g. it is inside a method
7329 definition), then nil is returned. Otherwise, if point is at a
7330 top-level not enclosed within a class definition, t is returned.
7331 Otherwise, a 2-vector is returned where the zeroth element is the
7332 buffer position of the start of the class declaration, and the first
7333 element is the buffer position of the enclosing class's opening
7334 brace.
7335
7336 Note that this function might do hidden buffer changes. See the
7337 comment at the start of cc-engine.el for more info."
7338 (let ((paren-state (c-parse-state)))
7339 (or (not (c-most-enclosing-brace paren-state))
7340 (c-search-uplist-for-classkey paren-state))))
7341
7342 (defun c-just-after-func-arglist-p (&optional lim)
7343 ;; Return non-nil if the point is in the region after the argument
7344 ;; list of a function and its opening brace (or semicolon in case it
7345 ;; got no body). If there are K&R style argument declarations in
7346 ;; that region, the point has to be inside the first one for this
7347 ;; function to recognize it.
7348 ;;
7349 ;; If successful, the point is moved to the first token after the
7350 ;; function header (see `c-forward-decl-or-cast-1' for details) and
7351 ;; the position of the opening paren of the function arglist is
7352 ;; returned.
7353 ;;
7354 ;; The point is clobbered if not successful.
7355 ;;
7356 ;; LIM is used as bound for backward buffer searches.
7357 ;;
7358 ;; This function might do hidden buffer changes.
7359
7360 (let ((beg (point)) end id-start)
7361 (and
7362 (eq (c-beginning-of-statement-1 lim) 'same)
7363
7364 (not (or (c-major-mode-is 'objc-mode)
7365 (c-forward-objc-directive)))
7366
7367 (setq id-start
7368 (car-safe (c-forward-decl-or-cast-1 (c-point 'bosws) nil nil)))
7369 (< id-start beg)
7370
7371 ;; There should not be a '=' or ',' between beg and the
7372 ;; start of the declaration since that means we were in the
7373 ;; "expression part" of the declaration.
7374 (or (> (point) beg)
7375 (not (looking-at "[=,]")))
7376
7377 (save-excursion
7378 ;; Check that there's an arglist paren in the
7379 ;; declaration.
7380 (goto-char id-start)
7381 (cond ((eq (char-after) ?\()
7382 ;; The declarator is a paren expression, so skip past it
7383 ;; so that we don't get stuck on that instead of the
7384 ;; function arglist.
7385 (c-forward-sexp))
7386 ((and c-opt-op-identifier-prefix
7387 (looking-at c-opt-op-identifier-prefix))
7388 ;; Don't trip up on "operator ()".
7389 (c-forward-token-2 2 t)))
7390 (and (< (point) beg)
7391 (c-syntactic-re-search-forward "(" beg t t)
7392 (1- (point)))))))
7393
7394 (defun c-in-knr-argdecl (&optional lim)
7395 ;; Return the position of the first argument declaration if point is
7396 ;; inside a K&R style argument declaration list, nil otherwise.
7397 ;; `c-recognize-knr-p' is not checked. If LIM is non-nil, it's a
7398 ;; position that bounds the backward search for the argument list.
7399 ;;
7400 ;; Point must be within a possible K&R region, e.g. just before a top-level
7401 ;; "{". It must be outside of parens and brackets. The test can return
7402 ;; false positives otherwise.
7403 ;;
7404 ;; This function might do hidden buffer changes.
7405
7406 (save-excursion
7407 (save-restriction
7408 ;; If we're in a macro, our search range is restricted to it. Narrow to
7409 ;; the searchable range.
7410 (let* ((macro-start (c-query-macro-start))
7411 (lim (max (or lim (point-min)) (or macro-start (point-min))))
7412 before-lparen after-rparen
7413 (pp-count-out 20)) ; Max number of paren/brace constructs before we give up
7414 (narrow-to-region lim (c-point 'eol))
7415
7416 ;; Search backwards for the defun's argument list. We give up if we
7417 ;; encounter a "}" (end of a previous defun) or BOB.
7418 ;;
7419 ;; The criterion for a paren structure being the arg list is:
7420 ;; o - there is non-WS stuff after it but before any "{"; AND
7421 ;; o - the token after it isn't a ";" AND
7422 ;; o - it is preceded by either an identifier (the function name) or
7423 ;; a macro expansion like "DEFUN (...)"; AND
7424 ;; o - its content is a non-empty comma-separated list of identifiers
7425 ;; (an empty arg list won't have a knr region).
7426 ;;
7427 ;; The following snippet illustrates these rules:
7428 ;; int foo (bar, baz, yuk)
7429 ;; int bar [] ;
7430 ;; int (*baz) (my_type) ;
7431 ;; int (*) (void) (*yuk) (void) ;
7432 ;; {
7433
7434 (catch 'knr
7435 (while (> pp-count-out 0) ; go back one paren/bracket pair each time.
7436 (setq pp-count-out (1- pp-count-out))
7437 (c-syntactic-skip-backward "^)]}")
7438 (cond ((eq (char-before) ?\))
7439 (setq after-rparen (point)))
7440 ((eq (char-before) ?\])
7441 (setq after-rparen nil))
7442 (t ; either } (hit previous defun) or no more parens/brackets
7443 (throw 'knr nil)))
7444
7445 (if after-rparen
7446 ;; We're inside a paren. Could it be our argument list....?
7447 (if
7448 (and
7449 (progn
7450 (goto-char after-rparen)
7451 (unless (c-go-list-backward) (throw 'knr nil)) ;
7452 ;; FIXME!!! What about macros between the parens? 2007/01/20
7453 (setq before-lparen (point)))
7454
7455 ;; It can't be the arg list if next token is ; or {
7456 (progn (goto-char after-rparen)
7457 (c-forward-syntactic-ws)
7458 (not (memq (char-after) '(?\; ?\{))))
7459
7460 ;; Is the thing preceding the list an identifier (the
7461 ;; function name), or a macro expansion?
7462 (progn
7463 (goto-char before-lparen)
7464 (eq (c-backward-token-2) 0)
7465 (or (c-on-identifier)
7466 (and (eq (char-after) ?\))
7467 (c-go-up-list-backward)
7468 (eq (c-backward-token-2) 0)
7469 (c-on-identifier))))
7470
7471 ;; Have we got a non-empty list of comma-separated
7472 ;; identifiers?
7473 (progn
7474 (goto-char before-lparen)
7475 (c-forward-token-2) ; to first token inside parens
7476 (and
7477 (c-on-identifier)
7478 (c-forward-token-2)
7479 (catch 'id-list
7480 (while (eq (char-after) ?\,)
7481 (c-forward-token-2)
7482 (unless (c-on-identifier) (throw 'id-list nil))
7483 (c-forward-token-2))
7484 (eq (char-after) ?\))))))
7485
7486 ;; ...Yes. We've identified the function's argument list.
7487 (throw 'knr
7488 (progn (goto-char after-rparen)
7489 (c-forward-syntactic-ws)
7490 (point)))
7491
7492 ;; ...No. The current parens aren't the function's arg list.
7493 (goto-char before-lparen))
7494
7495 (or (c-go-list-backward) ; backwards over [ .... ]
7496 (throw 'knr nil)))))))))
7497
7498 (defun c-skip-conditional ()
7499 ;; skip forward over conditional at point, including any predicate
7500 ;; statements in parentheses. No error checking is performed.
7501 ;;
7502 ;; This function might do hidden buffer changes.
7503 (c-forward-sexp (cond
7504 ;; else if()
7505 ((looking-at (concat "\\<else"
7506 "\\([ \t\n]\\|\\\\\n\\)+"
7507 "if\\>\\([^_]\\|$\\)"))
7508 3)
7509 ;; do, else, try, finally
7510 ((looking-at (concat "\\<\\("
7511 "do\\|else\\|try\\|finally"
7512 "\\)\\>\\([^_]\\|$\\)"))
7513 1)
7514 ;; for, if, while, switch, catch, synchronized, foreach
7515 (t 2))))
7516
7517 (defun c-after-conditional (&optional lim)
7518 ;; If looking at the token after a conditional then return the
7519 ;; position of its start, otherwise return nil.
7520 ;;
7521 ;; This function might do hidden buffer changes.
7522 (save-excursion
7523 (and (zerop (c-backward-token-2 1 t lim))
7524 (or (looking-at c-block-stmt-1-key)
7525 (and (eq (char-after) ?\()
7526 (zerop (c-backward-token-2 1 t lim))
7527 (looking-at c-block-stmt-2-key)))
7528 (point))))
7529
7530 (defun c-after-special-operator-id (&optional lim)
7531 ;; If the point is after an operator identifier that isn't handled
7532 ;; like an ordinary symbol (i.e. like "operator =" in C++) then the
7533 ;; position of the start of that identifier is returned. nil is
7534 ;; returned otherwise. The point may be anywhere in the syntactic
7535 ;; whitespace after the last token of the operator identifier.
7536 ;;
7537 ;; This function might do hidden buffer changes.
7538 (save-excursion
7539 (and c-overloadable-operators-regexp
7540 (zerop (c-backward-token-2 1 nil lim))
7541 (looking-at c-overloadable-operators-regexp)
7542 (or (not c-opt-op-identifier-prefix)
7543 (and
7544 (zerop (c-backward-token-2 1 nil lim))
7545 (looking-at c-opt-op-identifier-prefix)))
7546 (point))))
7547
7548 (defsubst c-backward-to-block-anchor (&optional lim)
7549 ;; Assuming point is at a brace that opens a statement block of some
7550 ;; kind, move to the proper anchor point for that block. It might
7551 ;; need to be adjusted further by c-add-stmt-syntax, but the
7552 ;; position at return is suitable as start position for that
7553 ;; function.
7554 ;;
7555 ;; This function might do hidden buffer changes.
7556 (unless (= (point) (c-point 'boi))
7557 (let ((start (c-after-conditional lim)))
7558 (if start
7559 (goto-char start)))))
7560
7561 (defsubst c-backward-to-decl-anchor (&optional lim)
7562 ;; Assuming point is at a brace that opens the block of a top level
7563 ;; declaration of some kind, move to the proper anchor point for
7564 ;; that block.
7565 ;;
7566 ;; This function might do hidden buffer changes.
7567 (unless (= (point) (c-point 'boi))
7568 (c-beginning-of-statement-1 lim)))
7569
7570 (defun c-search-decl-header-end ()
7571 ;; Search forward for the end of the "header" of the current
7572 ;; declaration. That's the position where the definition body
7573 ;; starts, or the first variable initializer, or the ending
7574 ;; semicolon. I.e. search forward for the closest following
7575 ;; (syntactically relevant) '{', '=' or ';' token. Point is left
7576 ;; _after_ the first found token, or at point-max if none is found.
7577 ;;
7578 ;; This function might do hidden buffer changes.
7579
7580 (let ((base (point)))
7581 (if (c-major-mode-is 'c++-mode)
7582
7583 ;; In C++ we need to take special care to handle operator
7584 ;; tokens and those pesky template brackets.
7585 (while (and
7586 (c-syntactic-re-search-forward "[;{<=]" nil 'move t t)
7587 (or
7588 (c-end-of-current-token base)
7589 ;; Handle operator identifiers, i.e. ignore any
7590 ;; operator token preceded by "operator".
7591 (save-excursion
7592 (and (c-safe (c-backward-sexp) t)
7593 (looking-at c-opt-op-identifier-prefix)))
7594 (and (eq (char-before) ?<)
7595 (c-with-syntax-table c++-template-syntax-table
7596 (if (c-safe (goto-char (c-up-list-forward (point))))
7597 t
7598 (goto-char (point-max))
7599 nil)))))
7600 (setq base (point)))
7601
7602 (while (and
7603 (c-syntactic-re-search-forward "[;{=]" nil 'move t t)
7604 (c-end-of-current-token base))
7605 (setq base (point))))))
7606
7607 (defun c-beginning-of-decl-1 (&optional lim)
7608 ;; Go to the beginning of the current declaration, or the beginning
7609 ;; of the previous one if already at the start of it. Point won't
7610 ;; be moved out of any surrounding paren. Return a cons cell of the
7611 ;; form (MOVE . KNR-POS). MOVE is like the return value from
7612 ;; `c-beginning-of-statement-1'. If point skipped over some K&R
7613 ;; style argument declarations (and they are to be recognized) then
7614 ;; KNR-POS is set to the start of the first such argument
7615 ;; declaration, otherwise KNR-POS is nil. If LIM is non-nil, it's a
7616 ;; position that bounds the backward search.
7617 ;;
7618 ;; NB: Cases where the declaration continues after the block, as in
7619 ;; "struct foo { ... } bar;", are currently recognized as two
7620 ;; declarations, e.g. "struct foo { ... }" and "bar;" in this case.
7621 ;;
7622 ;; This function might do hidden buffer changes.
7623 (catch 'return
7624 (let* ((start (point))
7625 (last-stmt-start (point))
7626 (move (c-beginning-of-statement-1 lim nil t)))
7627
7628 ;; `c-beginning-of-statement-1' stops at a block start, but we
7629 ;; want to continue if the block doesn't begin a top level
7630 ;; construct, i.e. if it isn't preceded by ';', '}', ':', bob,
7631 ;; or an open paren.
7632 (let ((beg (point)) tentative-move)
7633 ;; Go back one "statement" each time round the loop until we're just
7634 ;; after a ;, }, or :, or at BOB or the start of a macro or start of
7635 ;; an ObjC method. This will move over a multiple declaration whose
7636 ;; components are comma separated.
7637 (while (and
7638 ;; Must check with c-opt-method-key in ObjC mode.
7639 (not (and c-opt-method-key
7640 (looking-at c-opt-method-key)))
7641 (/= last-stmt-start (point))
7642 (progn
7643 (c-backward-syntactic-ws lim)
7644 (not (memq (char-before) '(?\; ?} ?: nil))))
7645 (save-excursion
7646 (backward-char)
7647 (not (looking-at "\\s(")))
7648 ;; Check that we don't move from the first thing in a
7649 ;; macro to its header.
7650 (not (eq (setq tentative-move
7651 (c-beginning-of-statement-1 lim nil t))
7652 'macro)))
7653 (setq last-stmt-start beg
7654 beg (point)
7655 move tentative-move))
7656 (goto-char beg))
7657
7658 (when c-recognize-knr-p
7659 (let ((fallback-pos (point)) knr-argdecl-start)
7660 ;; Handle K&R argdecls. Back up after the "statement" jumped
7661 ;; over by `c-beginning-of-statement-1', unless it was the
7662 ;; function body, in which case we're sitting on the opening
7663 ;; brace now. Then test if we're in a K&R argdecl region and
7664 ;; that we started at the other side of the first argdecl in
7665 ;; it.
7666 (unless (eq (char-after) ?{)
7667 (goto-char last-stmt-start))
7668 (if (and (setq knr-argdecl-start (c-in-knr-argdecl lim))
7669 (< knr-argdecl-start start)
7670 (progn
7671 (goto-char knr-argdecl-start)
7672 (not (eq (c-beginning-of-statement-1 lim nil t) 'macro))))
7673 (throw 'return
7674 (cons (if (eq (char-after fallback-pos) ?{)
7675 'previous
7676 'same)
7677 knr-argdecl-start))
7678 (goto-char fallback-pos))))
7679
7680 ;; `c-beginning-of-statement-1' counts each brace block as a separate
7681 ;; statement, so the result will be 'previous if we've moved over any.
7682 ;; So change our result back to 'same if necessary.
7683 ;;
7684 ;; If they were brace list initializers we might not have moved over a
7685 ;; declaration boundary though, so change it to 'same if we've moved
7686 ;; past a '=' before '{', but not ';'. (This ought to be integrated
7687 ;; into `c-beginning-of-statement-1', so we avoid this extra pass which
7688 ;; potentially can search over a large amount of text.). Take special
7689 ;; pains not to get mislead by C++'s "operator=", and the like.
7690 (if (and (eq move 'previous)
7691 (c-with-syntax-table (if (c-major-mode-is 'c++-mode)
7692 c++-template-syntax-table
7693 (syntax-table))
7694 (save-excursion
7695 (and
7696 (progn
7697 (while ; keep going back to "[;={"s until we either find
7698 ; no more, or get to one which isn't an "operator ="
7699 (and (c-syntactic-re-search-forward "[;={]" start t t t)
7700 (eq (char-before) ?=)
7701 c-overloadable-operators-regexp
7702 c-opt-op-identifier-prefix
7703 (save-excursion
7704 (eq (c-backward-token-2) 0)
7705 (looking-at c-overloadable-operators-regexp)
7706 (eq (c-backward-token-2) 0)
7707 (looking-at c-opt-op-identifier-prefix))))
7708 (eq (char-before) ?=))
7709 (c-syntactic-re-search-forward "[;{]" start t t)
7710 (eq (char-before) ?{)
7711 (c-safe (goto-char (c-up-list-forward (point))) t)
7712 (not (c-syntactic-re-search-forward ";" start t t))))))
7713 (cons 'same nil)
7714 (cons move nil)))))
7715
7716 (defun c-end-of-decl-1 ()
7717 ;; Assuming point is at the start of a declaration (as detected by
7718 ;; e.g. `c-beginning-of-decl-1'), go to the end of it. Unlike
7719 ;; `c-beginning-of-decl-1', this function handles the case when a
7720 ;; block is followed by identifiers in e.g. struct declarations in C
7721 ;; or C++. If a proper end was found then t is returned, otherwise
7722 ;; point is moved as far as possible within the current sexp and nil
7723 ;; is returned. This function doesn't handle macros; use
7724 ;; `c-end-of-macro' instead in those cases.
7725 ;;
7726 ;; This function might do hidden buffer changes.
7727 (let ((start (point))
7728 (decl-syntax-table (if (c-major-mode-is 'c++-mode)
7729 c++-template-syntax-table
7730 (syntax-table))))
7731 (catch 'return
7732 (c-search-decl-header-end)
7733
7734 (when (and c-recognize-knr-p
7735 (eq (char-before) ?\;)
7736 (c-in-knr-argdecl start))
7737 ;; Stopped at the ';' in a K&R argdecl section which is
7738 ;; detected using the same criteria as in
7739 ;; `c-beginning-of-decl-1'. Move to the following block
7740 ;; start.
7741 (c-syntactic-re-search-forward "{" nil 'move t))
7742
7743 (when (eq (char-before) ?{)
7744 ;; Encountered a block in the declaration. Jump over it.
7745 (condition-case nil
7746 (goto-char (c-up-list-forward (point)))
7747 (error (goto-char (point-max))
7748 (throw 'return nil)))
7749 (if (or (not c-opt-block-decls-with-vars-key)
7750 (save-excursion
7751 (c-with-syntax-table decl-syntax-table
7752 (let ((lim (point)))
7753 (goto-char start)
7754 (not (and
7755 ;; Check for `c-opt-block-decls-with-vars-key'
7756 ;; before the first paren.
7757 (c-syntactic-re-search-forward
7758 (concat "[;=\(\[{]\\|\\("
7759 c-opt-block-decls-with-vars-key
7760 "\\)")
7761 lim t t t)
7762 (match-beginning 1)
7763 (not (eq (char-before) ?_))
7764 ;; Check that the first following paren is
7765 ;; the block.
7766 (c-syntactic-re-search-forward "[;=\(\[{]"
7767 lim t t t)
7768 (eq (char-before) ?{)))))))
7769 ;; The declaration doesn't have any of the
7770 ;; `c-opt-block-decls-with-vars' keywords in the
7771 ;; beginning, so it ends here at the end of the block.
7772 (throw 'return t)))
7773
7774 (c-with-syntax-table decl-syntax-table
7775 (while (progn
7776 (if (eq (char-before) ?\;)
7777 (throw 'return t))
7778 (c-syntactic-re-search-forward ";" nil 'move t))))
7779 nil)))
7780
7781 (defun c-looking-at-decl-block (containing-sexp goto-start &optional limit)
7782 ;; Assuming the point is at an open brace, check if it starts a
7783 ;; block that contains another declaration level, i.e. that isn't a
7784 ;; statement block or a brace list, and if so return non-nil.
7785 ;;
7786 ;; If the check is successful, the return value is the start of the
7787 ;; keyword that tells what kind of construct it is, i.e. typically
7788 ;; what `c-decl-block-key' matched. Also, if GOTO-START is set then
7789 ;; the point will be at the start of the construct, before any
7790 ;; leading specifiers, otherwise it's at the returned position.
7791 ;;
7792 ;; The point is clobbered if the check is unsuccessful.
7793 ;;
7794 ;; CONTAINING-SEXP is the position of the open of the surrounding
7795 ;; paren, or nil if none.
7796 ;;
7797 ;; The optional LIMIT limits the backward search for the start of
7798 ;; the construct. It's assumed to be at a syntactically relevant
7799 ;; position.
7800 ;;
7801 ;; If any template arglists are found in the searched region before
7802 ;; the open brace, they get marked with paren syntax.
7803 ;;
7804 ;; This function might do hidden buffer changes.
7805
7806 (let ((open-brace (point)) kwd-start first-specifier-pos)
7807 (c-syntactic-skip-backward c-block-prefix-charset limit t)
7808
7809 (when (and c-recognize-<>-arglists
7810 (eq (char-before) ?>))
7811 ;; Could be at the end of a template arglist.
7812 (let ((c-parse-and-markup-<>-arglists t)
7813 (c-disallow-comma-in-<>-arglists
7814 (and containing-sexp
7815 (not (eq (char-after containing-sexp) ?{)))))
7816 (while (and
7817 (c-backward-<>-arglist nil limit)
7818 (progn
7819 (c-syntactic-skip-backward c-block-prefix-charset limit t)
7820 (eq (char-before) ?>))))))
7821
7822 ;; Note: Can't get bogus hits inside template arglists below since they
7823 ;; have gotten paren syntax above.
7824 (when (and
7825 ;; If `goto-start' is set we begin by searching for the
7826 ;; first possible position of a leading specifier list.
7827 ;; The `c-decl-block-key' search continues from there since
7828 ;; we know it can't match earlier.
7829 (if goto-start
7830 (when (c-syntactic-re-search-forward c-symbol-start
7831 open-brace t t)
7832 (goto-char (setq first-specifier-pos (match-beginning 0)))
7833 t)
7834 t)
7835
7836 (cond
7837 ((c-syntactic-re-search-forward c-decl-block-key open-brace t t t)
7838 (goto-char (setq kwd-start (match-beginning 0)))
7839 (or
7840
7841 ;; Found a keyword that can't be a type?
7842 (match-beginning 1)
7843
7844 ;; Can be a type too, in which case it's the return type of a
7845 ;; function (under the assumption that no declaration level
7846 ;; block construct starts with a type).
7847 (not (c-forward-type))
7848
7849 ;; Jumped over a type, but it could be a declaration keyword
7850 ;; followed by the declared identifier that we've jumped over
7851 ;; instead (e.g. in "class Foo {"). If it indeed is a type
7852 ;; then we should be at the declarator now, so check for a
7853 ;; valid declarator start.
7854 ;;
7855 ;; Note: This doesn't cope with the case when a declared
7856 ;; identifier is followed by e.g. '(' in a language where '('
7857 ;; also might be part of a declarator expression. Currently
7858 ;; there's no such language.
7859 (not (or (looking-at c-symbol-start)
7860 (looking-at c-type-decl-prefix-key)))))
7861
7862 ;; In Pike a list of modifiers may be followed by a brace
7863 ;; to make them apply to many identifiers. Note that the
7864 ;; match data will be empty on return in this case.
7865 ((and (c-major-mode-is 'pike-mode)
7866 (progn
7867 (goto-char open-brace)
7868 (= (c-backward-token-2) 0))
7869 (looking-at c-specifier-key)
7870 ;; Use this variant to avoid yet another special regexp.
7871 (c-keyword-member (c-keyword-sym (match-string 1))
7872 'c-modifier-kwds))
7873 (setq kwd-start (point))
7874 t)))
7875
7876 ;; Got a match.
7877
7878 (if goto-start
7879 ;; Back up over any preceding specifiers and their clauses
7880 ;; by going forward from `first-specifier-pos', which is the
7881 ;; earliest possible position where the specifier list can
7882 ;; start.
7883 (progn
7884 (goto-char first-specifier-pos)
7885
7886 (while (< (point) kwd-start)
7887 (if (looking-at c-symbol-key)
7888 ;; Accept any plain symbol token on the ground that
7889 ;; it's a specifier masked through a macro (just
7890 ;; like `c-forward-decl-or-cast-1' skip forward over
7891 ;; such tokens).
7892 ;;
7893 ;; Could be more restrictive wrt invalid keywords,
7894 ;; but that'd only occur in invalid code so there's
7895 ;; no use spending effort on it.
7896 (let ((end (match-end 0)))
7897 (unless (c-forward-keyword-clause 0)
7898 (goto-char end)
7899 (c-forward-syntactic-ws)))
7900
7901 ;; Can't parse a declaration preamble and is still
7902 ;; before `kwd-start'. That means `first-specifier-pos'
7903 ;; was in some earlier construct. Search again.
7904 (if (c-syntactic-re-search-forward c-symbol-start
7905 kwd-start 'move t)
7906 (goto-char (setq first-specifier-pos (match-beginning 0)))
7907 ;; Got no preamble before the block declaration keyword.
7908 (setq first-specifier-pos kwd-start))))
7909
7910 (goto-char first-specifier-pos))
7911 (goto-char kwd-start))
7912
7913 kwd-start)))
7914
7915 (defun c-search-uplist-for-classkey (paren-state)
7916 ;; Check if the closest containing paren sexp is a declaration
7917 ;; block, returning a 2 element vector in that case. Aref 0
7918 ;; contains the bufpos at boi of the class key line, and aref 1
7919 ;; contains the bufpos of the open brace. This function is an
7920 ;; obsolete wrapper for `c-looking-at-decl-block'.
7921 ;;
7922 ;; This function might do hidden buffer changes.
7923 (let ((open-paren-pos (c-most-enclosing-brace paren-state)))
7924 (when open-paren-pos
7925 (save-excursion
7926 (goto-char open-paren-pos)
7927 (when (and (eq (char-after) ?{)
7928 (c-looking-at-decl-block
7929 (c-safe-position open-paren-pos paren-state)
7930 nil))
7931 (back-to-indentation)
7932 (vector (point) open-paren-pos))))))
7933
7934 (defun c-inside-bracelist-p (containing-sexp paren-state)
7935 ;; return the buffer position of the beginning of the brace list
7936 ;; statement if we're inside a brace list, otherwise return nil.
7937 ;; CONTAINING-SEXP is the buffer pos of the innermost containing
7938 ;; paren. PAREN-STATE is the remainder of the state of enclosing
7939 ;; braces
7940 ;;
7941 ;; N.B.: This algorithm can potentially get confused by cpp macros
7942 ;; placed in inconvenient locations. It's a trade-off we make for
7943 ;; speed.
7944 ;;
7945 ;; This function might do hidden buffer changes.
7946 (or
7947 ;; This will pick up brace list declarations.
7948 (c-safe
7949 (save-excursion
7950 (goto-char containing-sexp)
7951 (c-forward-sexp -1)
7952 (let (bracepos)
7953 (if (and (or (looking-at c-brace-list-key)
7954 (progn (c-forward-sexp -1)
7955 (looking-at c-brace-list-key)))
7956 (setq bracepos (c-down-list-forward (point)))
7957 (not (c-crosses-statement-barrier-p (point)
7958 (- bracepos 2))))
7959 (point)))))
7960 ;; this will pick up array/aggregate init lists, even if they are nested.
7961 (save-excursion
7962 (let ((class-key
7963 ;; Pike can have class definitions anywhere, so we must
7964 ;; check for the class key here.
7965 (and (c-major-mode-is 'pike-mode)
7966 c-decl-block-key))
7967 bufpos braceassignp lim next-containing)
7968 (while (and (not bufpos)
7969 containing-sexp)
7970 (when paren-state
7971 (if (consp (car paren-state))
7972 (setq lim (cdr (car paren-state))
7973 paren-state (cdr paren-state))
7974 (setq lim (car paren-state)))
7975 (when paren-state
7976 (setq next-containing (car paren-state)
7977 paren-state (cdr paren-state))))
7978 (goto-char containing-sexp)
7979 (if (c-looking-at-inexpr-block next-containing next-containing)
7980 ;; We're in an in-expression block of some kind. Do not
7981 ;; check nesting. We deliberately set the limit to the
7982 ;; containing sexp, so that c-looking-at-inexpr-block
7983 ;; doesn't check for an identifier before it.
7984 (setq containing-sexp nil)
7985 ;; see if the open brace is preceded by = or [...] in
7986 ;; this statement, but watch out for operator=
7987 (setq braceassignp 'dontknow)
7988 (c-backward-token-2 1 t lim)
7989 ;; Checks to do only on the first sexp before the brace.
7990 (when (and c-opt-inexpr-brace-list-key
7991 (eq (char-after) ?\[))
7992 ;; In Java, an initialization brace list may follow
7993 ;; directly after "new Foo[]", so check for a "new"
7994 ;; earlier.
7995 (while (eq braceassignp 'dontknow)
7996 (setq braceassignp
7997 (cond ((/= (c-backward-token-2 1 t lim) 0) nil)
7998 ((looking-at c-opt-inexpr-brace-list-key) t)
7999 ((looking-at "\\sw\\|\\s_\\|[.[]")
8000 ;; Carry on looking if this is an
8001 ;; identifier (may contain "." in Java)
8002 ;; or another "[]" sexp.
8003 'dontknow)
8004 (t nil)))))
8005 ;; Checks to do on all sexps before the brace, up to the
8006 ;; beginning of the statement.
8007 (while (eq braceassignp 'dontknow)
8008 (cond ((eq (char-after) ?\;)
8009 (setq braceassignp nil))
8010 ((and class-key
8011 (looking-at class-key))
8012 (setq braceassignp nil))
8013 ((eq (char-after) ?=)
8014 ;; We've seen a =, but must check earlier tokens so
8015 ;; that it isn't something that should be ignored.
8016 (setq braceassignp 'maybe)
8017 (while (and (eq braceassignp 'maybe)
8018 (zerop (c-backward-token-2 1 t lim)))
8019 (setq braceassignp
8020 (cond
8021 ;; Check for operator =
8022 ((and c-opt-op-identifier-prefix
8023 (looking-at c-opt-op-identifier-prefix))
8024 nil)
8025 ;; Check for `<opchar>= in Pike.
8026 ((and (c-major-mode-is 'pike-mode)
8027 (or (eq (char-after) ?`)
8028 ;; Special case for Pikes
8029 ;; `[]=, since '[' is not in
8030 ;; the punctuation class.
8031 (and (eq (char-after) ?\[)
8032 (eq (char-before) ?`))))
8033 nil)
8034 ((looking-at "\\s.") 'maybe)
8035 ;; make sure we're not in a C++ template
8036 ;; argument assignment
8037 ((and
8038 (c-major-mode-is 'c++-mode)
8039 (save-excursion
8040 (let ((here (point))
8041 (pos< (progn
8042 (skip-chars-backward "^<>")
8043 (point))))
8044 (and (eq (char-before) ?<)
8045 (not (c-crosses-statement-barrier-p
8046 pos< here))
8047 (not (c-in-literal))
8048 ))))
8049 nil)
8050 (t t))))))
8051 (if (and (eq braceassignp 'dontknow)
8052 (/= (c-backward-token-2 1 t lim) 0))
8053 (setq braceassignp nil)))
8054 (if (not braceassignp)
8055 (if (eq (char-after) ?\;)
8056 ;; Brace lists can't contain a semicolon, so we're done.
8057 (setq containing-sexp nil)
8058 ;; Go up one level.
8059 (setq containing-sexp next-containing
8060 lim nil
8061 next-containing nil))
8062 ;; we've hit the beginning of the aggregate list
8063 (c-beginning-of-statement-1
8064 (c-most-enclosing-brace paren-state))
8065 (setq bufpos (point))))
8066 )
8067 bufpos))
8068 ))
8069
8070 (defun c-looking-at-special-brace-list (&optional lim)
8071 ;; If we're looking at the start of a pike-style list, ie `({ })',
8072 ;; `([ ])', `(< >)' etc, a cons of a cons of its starting and ending
8073 ;; positions and its entry in c-special-brace-lists is returned, nil
8074 ;; otherwise. The ending position is nil if the list is still open.
8075 ;; LIM is the limit for forward search. The point may either be at
8076 ;; the `(' or at the following paren character. Tries to check the
8077 ;; matching closer, but assumes it's correct if no balanced paren is
8078 ;; found (i.e. the case `({ ... } ... )' is detected as _not_ being
8079 ;; a special brace list).
8080 ;;
8081 ;; This function might do hidden buffer changes.
8082 (if c-special-brace-lists
8083 (condition-case ()
8084 (save-excursion
8085 (let ((beg (point))
8086 inner-beg end type)
8087 (c-forward-syntactic-ws)
8088 (if (eq (char-after) ?\()
8089 (progn
8090 (forward-char 1)
8091 (c-forward-syntactic-ws)
8092 (setq inner-beg (point))
8093 (setq type (assq (char-after) c-special-brace-lists)))
8094 (if (setq type (assq (char-after) c-special-brace-lists))
8095 (progn
8096 (setq inner-beg (point))
8097 (c-backward-syntactic-ws)
8098 (forward-char -1)
8099 (setq beg (if (eq (char-after) ?\()
8100 (point)
8101 nil)))))
8102 (if (and beg type)
8103 (if (and (c-safe
8104 (goto-char beg)
8105 (c-forward-sexp 1)
8106 (setq end (point))
8107 (= (char-before) ?\)))
8108 (c-safe
8109 (goto-char inner-beg)
8110 (if (looking-at "\\s(")
8111 ;; Check balancing of the inner paren
8112 ;; below.
8113 (progn
8114 (c-forward-sexp 1)
8115 t)
8116 ;; If the inner char isn't a paren then
8117 ;; we can't check balancing, so just
8118 ;; check the char before the outer
8119 ;; closing paren.
8120 (goto-char end)
8121 (backward-char)
8122 (c-backward-syntactic-ws)
8123 (= (char-before) (cdr type)))))
8124 (if (or (/= (char-syntax (char-before)) ?\))
8125 (= (progn
8126 (c-forward-syntactic-ws)
8127 (point))
8128 (1- end)))
8129 (cons (cons beg end) type))
8130 (cons (list beg) type)))))
8131 (error nil))))
8132
8133 (defun c-looking-at-bos (&optional lim)
8134 ;; Return non-nil if between two statements or declarations, assuming
8135 ;; point is not inside a literal or comment.
8136 ;;
8137 ;; Obsolete - `c-at-statement-start-p' or `c-at-expression-start-p'
8138 ;; are recommended instead.
8139 ;;
8140 ;; This function might do hidden buffer changes.
8141 (c-at-statement-start-p))
8142 (make-obsolete 'c-looking-at-bos 'c-at-statement-start-p "22.1")
8143
8144 (defun c-looking-at-inexpr-block (lim containing-sexp &optional check-at-end)
8145 ;; Return non-nil if we're looking at the beginning of a block
8146 ;; inside an expression. The value returned is actually a cons of
8147 ;; either 'inlambda, 'inexpr-statement or 'inexpr-class and the
8148 ;; position of the beginning of the construct.
8149 ;;
8150 ;; LIM limits the backward search. CONTAINING-SEXP is the start
8151 ;; position of the closest containing list. If it's nil, the
8152 ;; containing paren isn't used to decide whether we're inside an
8153 ;; expression or not. If both LIM and CONTAINING-SEXP are used, LIM
8154 ;; needs to be farther back.
8155 ;;
8156 ;; If CHECK-AT-END is non-nil then extra checks at the end of the
8157 ;; brace block might be done. It should only be used when the
8158 ;; construct can be assumed to be complete, i.e. when the original
8159 ;; starting position was further down than that.
8160 ;;
8161 ;; This function might do hidden buffer changes.
8162
8163 (save-excursion
8164 (let ((res 'maybe) passed-paren
8165 (closest-lim (or containing-sexp lim (point-min)))
8166 ;; Look at the character after point only as a last resort
8167 ;; when we can't disambiguate.
8168 (block-follows (and (eq (char-after) ?{) (point))))
8169
8170 (while (and (eq res 'maybe)
8171 (progn (c-backward-syntactic-ws)
8172 (> (point) closest-lim))
8173 (not (bobp))
8174 (progn (backward-char)
8175 (looking-at "[\]\).]\\|\\w\\|\\s_"))
8176 (c-safe (forward-char)
8177 (goto-char (scan-sexps (point) -1))))
8178
8179 (setq res
8180 (if (looking-at c-keywords-regexp)
8181 (let ((kw-sym (c-keyword-sym (match-string 1))))
8182 (cond
8183 ((and block-follows
8184 (c-keyword-member kw-sym 'c-inexpr-class-kwds))
8185 (and (not (eq passed-paren ?\[))
8186 (or (not (looking-at c-class-key))
8187 ;; If the class definition is at the start of
8188 ;; a statement, we don't consider it an
8189 ;; in-expression class.
8190 (let ((prev (point)))
8191 (while (and
8192 (= (c-backward-token-2 1 nil closest-lim) 0)
8193 (eq (char-syntax (char-after)) ?w))
8194 (setq prev (point)))
8195 (goto-char prev)
8196 (not (c-at-statement-start-p)))
8197 ;; Also, in Pike we treat it as an
8198 ;; in-expression class if it's used in an
8199 ;; object clone expression.
8200 (save-excursion
8201 (and check-at-end
8202 (c-major-mode-is 'pike-mode)
8203 (progn (goto-char block-follows)
8204 (zerop (c-forward-token-2 1 t)))
8205 (eq (char-after) ?\())))
8206 (cons 'inexpr-class (point))))
8207 ((c-keyword-member kw-sym 'c-inexpr-block-kwds)
8208 (when (not passed-paren)
8209 (cons 'inexpr-statement (point))))
8210 ((c-keyword-member kw-sym 'c-lambda-kwds)
8211 (when (or (not passed-paren)
8212 (eq passed-paren ?\())
8213 (cons 'inlambda (point))))
8214 ((c-keyword-member kw-sym 'c-block-stmt-kwds)
8215 nil)
8216 (t
8217 'maybe)))
8218
8219 (if (looking-at "\\s(")
8220 (if passed-paren
8221 (if (and (eq passed-paren ?\[)
8222 (eq (char-after) ?\[))
8223 ;; Accept several square bracket sexps for
8224 ;; Java array initializations.
8225 'maybe)
8226 (setq passed-paren (char-after))
8227 'maybe)
8228 'maybe))))
8229
8230 (if (eq res 'maybe)
8231 (when (and c-recognize-paren-inexpr-blocks
8232 block-follows
8233 containing-sexp
8234 (eq (char-after containing-sexp) ?\())
8235 (goto-char containing-sexp)
8236 (if (or (save-excursion
8237 (c-backward-syntactic-ws lim)
8238 (and (> (point) (or lim (point-min)))
8239 (c-on-identifier)))
8240 (and c-special-brace-lists
8241 (c-looking-at-special-brace-list)))
8242 nil
8243 (cons 'inexpr-statement (point))))
8244
8245 res))))
8246
8247 (defun c-looking-at-inexpr-block-backward (paren-state)
8248 ;; Returns non-nil if we're looking at the end of an in-expression
8249 ;; block, otherwise the same as `c-looking-at-inexpr-block'.
8250 ;; PAREN-STATE is the paren state relevant at the current position.
8251 ;;
8252 ;; This function might do hidden buffer changes.
8253 (save-excursion
8254 ;; We currently only recognize a block.
8255 (let ((here (point))
8256 (elem (car-safe paren-state))
8257 containing-sexp)
8258 (when (and (consp elem)
8259 (progn (goto-char (cdr elem))
8260 (c-forward-syntactic-ws here)
8261 (= (point) here)))
8262 (goto-char (car elem))
8263 (if (setq paren-state (cdr paren-state))
8264 (setq containing-sexp (car-safe paren-state)))
8265 (c-looking-at-inexpr-block (c-safe-position containing-sexp
8266 paren-state)
8267 containing-sexp)))))
8268
8269 \f
8270 ;; `c-guess-basic-syntax' and the functions that precedes it below
8271 ;; implements the main decision tree for determining the syntactic
8272 ;; analysis of the current line of code.
8273
8274 ;; Dynamically bound to t when `c-guess-basic-syntax' is called during
8275 ;; auto newline analysis.
8276 (defvar c-auto-newline-analysis nil)
8277
8278 (defun c-brace-anchor-point (bracepos)
8279 ;; BRACEPOS is the position of a brace in a construct like "namespace
8280 ;; Bar {". Return the anchor point in this construct; this is the
8281 ;; earliest symbol on the brace's line which isn't earlier than
8282 ;; "namespace".
8283 ;;
8284 ;; Currently (2007-08-17), "like namespace" means "matches
8285 ;; c-other-block-decl-kwds". It doesn't work with "class" or "struct"
8286 ;; or anything like that.
8287 (save-excursion
8288 (let ((boi (c-point 'boi bracepos)))
8289 (goto-char bracepos)
8290 (while (and (> (point) boi)
8291 (not (looking-at c-other-decl-block-key)))
8292 (c-backward-token-2))
8293 (if (> (point) boi) (point) boi))))
8294
8295 (defsubst c-add-syntax (symbol &rest args)
8296 ;; A simple function to prepend a new syntax element to
8297 ;; `c-syntactic-context'. Using `setq' on it is unsafe since it
8298 ;; should always be dynamically bound but since we read it first
8299 ;; we'll fail properly anyway if this function is misused.
8300 (setq c-syntactic-context (cons (cons symbol args)
8301 c-syntactic-context)))
8302
8303 (defsubst c-append-syntax (symbol &rest args)
8304 ;; Like `c-add-syntax' but appends to the end of the syntax list.
8305 ;; (Normally not necessary.)
8306 (setq c-syntactic-context (nconc c-syntactic-context
8307 (list (cons symbol args)))))
8308
8309 (defun c-add-stmt-syntax (syntax-symbol
8310 syntax-extra-args
8311 stop-at-boi-only
8312 containing-sexp
8313 paren-state)
8314 ;; Add the indicated SYNTAX-SYMBOL to `c-syntactic-context', extending it as
8315 ;; needed with further syntax elements of the types `substatement',
8316 ;; `inexpr-statement', `arglist-cont-nonempty', `statement-block-intro', and
8317 ;; `defun-block-intro'.
8318 ;;
8319 ;; Do the generic processing to anchor the given syntax symbol on
8320 ;; the preceding statement: Skip over any labels and containing
8321 ;; statements on the same line, and then search backward until we
8322 ;; find a statement or block start that begins at boi without a
8323 ;; label or comment.
8324 ;;
8325 ;; Point is assumed to be at the prospective anchor point for the
8326 ;; given SYNTAX-SYMBOL. More syntax entries are added if we need to
8327 ;; skip past open parens and containing statements. Most of the added
8328 ;; syntax elements will get the same anchor point - the exception is
8329 ;; for an anchor in a construct like "namespace"[*] - this is as early
8330 ;; as possible in the construct but on the same line as the {.
8331 ;;
8332 ;; [*] i.e. with a keyword matching c-other-block-decl-kwds.
8333 ;;
8334 ;; SYNTAX-EXTRA-ARGS are a list of the extra arguments for the
8335 ;; syntax symbol. They are appended after the anchor point.
8336 ;;
8337 ;; If STOP-AT-BOI-ONLY is nil, we can stop in the middle of the line
8338 ;; if the current statement starts there.
8339 ;;
8340 ;; Note: It's not a problem if PAREN-STATE "overshoots"
8341 ;; CONTAINING-SEXP, i.e. contains info about parens further down.
8342 ;;
8343 ;; This function might do hidden buffer changes.
8344
8345 (if (= (point) (c-point 'boi))
8346 ;; This is by far the most common case, so let's give it special
8347 ;; treatment.
8348 (apply 'c-add-syntax syntax-symbol (point) syntax-extra-args)
8349
8350 (let ((syntax-last c-syntactic-context)
8351 (boi (c-point 'boi))
8352 ;; Set when we're on a label, so that we don't stop there.
8353 ;; FIXME: To be complete we should check if we're on a label
8354 ;; now at the start.
8355 on-label)
8356
8357 ;; Use point as the anchor point for "namespace", "extern", etc.
8358 (apply 'c-add-syntax syntax-symbol
8359 (if (rassq syntax-symbol c-other-decl-block-key-in-symbols-alist)
8360 (point) nil)
8361 syntax-extra-args)
8362
8363 ;; Loop while we have to back out of containing blocks.
8364 (while
8365 (and
8366 (catch 'back-up-block
8367
8368 ;; Loop while we have to back up statements.
8369 (while (or (/= (point) boi)
8370 on-label
8371 (looking-at c-comment-start-regexp))
8372
8373 ;; Skip past any comments that stands between the
8374 ;; statement start and boi.
8375 (let ((savepos (point)))
8376 (while (and (/= savepos boi)
8377 (c-backward-single-comment))
8378 (setq savepos (point)
8379 boi (c-point 'boi)))
8380 (goto-char savepos))
8381
8382 ;; Skip to the beginning of this statement or backward
8383 ;; another one.
8384 (let ((old-pos (point))
8385 (old-boi boi)
8386 (step-type (c-beginning-of-statement-1 containing-sexp)))
8387 (setq boi (c-point 'boi)
8388 on-label (eq step-type 'label))
8389
8390 (cond ((= (point) old-pos)
8391 ;; If we didn't move we're at the start of a block and
8392 ;; have to continue outside it.
8393 (throw 'back-up-block t))
8394
8395 ((and (eq step-type 'up)
8396 (>= (point) old-boi)
8397 (looking-at "else\\>[^_]")
8398 (save-excursion
8399 (goto-char old-pos)
8400 (looking-at "if\\>[^_]")))
8401 ;; Special case to avoid deeper and deeper indentation
8402 ;; of "else if" clauses.
8403 )
8404
8405 ((and (not stop-at-boi-only)
8406 (/= old-pos old-boi)
8407 (memq step-type '(up previous)))
8408 ;; If stop-at-boi-only is nil, we shouldn't back up
8409 ;; over previous or containing statements to try to
8410 ;; reach boi, so go back to the last position and
8411 ;; exit.
8412 (goto-char old-pos)
8413 (throw 'back-up-block nil))
8414
8415 (t
8416 (if (and (not stop-at-boi-only)
8417 (memq step-type '(up previous beginning)))
8418 ;; If we've moved into another statement then we
8419 ;; should no longer try to stop in the middle of a
8420 ;; line.
8421 (setq stop-at-boi-only t))
8422
8423 ;; Record this as a substatement if we skipped up one
8424 ;; level.
8425 (when (eq step-type 'up)
8426 (c-add-syntax 'substatement nil))))
8427 )))
8428
8429 containing-sexp)
8430
8431 ;; Now we have to go out of this block.
8432 (goto-char containing-sexp)
8433
8434 ;; Don't stop in the middle of a special brace list opener
8435 ;; like "({".
8436 (when c-special-brace-lists
8437 (let ((special-list (c-looking-at-special-brace-list)))
8438 (when (and special-list
8439 (< (car (car special-list)) (point)))
8440 (setq containing-sexp (car (car special-list)))
8441 (goto-char containing-sexp))))
8442
8443 (setq paren-state (c-whack-state-after containing-sexp paren-state)
8444 containing-sexp (c-most-enclosing-brace paren-state)
8445 boi (c-point 'boi))
8446
8447 ;; Analyze the construct in front of the block we've stepped out
8448 ;; from and add the right syntactic element for it.
8449 (let ((paren-pos (point))
8450 (paren-char (char-after))
8451 step-type)
8452
8453 (if (eq paren-char ?\()
8454 ;; Stepped out of a parenthesis block, so we're in an
8455 ;; expression now.
8456 (progn
8457 (when (/= paren-pos boi)
8458 (if (and c-recognize-paren-inexpr-blocks
8459 (progn
8460 (c-backward-syntactic-ws containing-sexp)
8461 (or (not (looking-at "\\>"))
8462 (not (c-on-identifier))))
8463 (save-excursion
8464 (goto-char (1+ paren-pos))
8465 (c-forward-syntactic-ws)
8466 (eq (char-after) ?{)))
8467 ;; Stepped out of an in-expression statement. This
8468 ;; syntactic element won't get an anchor pos.
8469 (c-add-syntax 'inexpr-statement)
8470
8471 ;; A parenthesis normally belongs to an arglist.
8472 (c-add-syntax 'arglist-cont-nonempty nil paren-pos)))
8473
8474 (goto-char (max boi
8475 (if containing-sexp
8476 (1+ containing-sexp)
8477 (point-min))))
8478 (setq step-type 'same
8479 on-label nil))
8480
8481 ;; Stepped out of a brace block.
8482 (setq step-type (c-beginning-of-statement-1 containing-sexp)
8483 on-label (eq step-type 'label))
8484
8485 (if (and (eq step-type 'same)
8486 (/= paren-pos (point)))
8487 (let (inexpr)
8488 (cond
8489 ((save-excursion
8490 (goto-char paren-pos)
8491 (setq inexpr (c-looking-at-inexpr-block
8492 (c-safe-position containing-sexp paren-state)
8493 containing-sexp)))
8494 (c-add-syntax (if (eq (car inexpr) 'inlambda)
8495 'defun-block-intro
8496 'statement-block-intro)
8497 nil))
8498 ((looking-at c-other-decl-block-key)
8499 (c-add-syntax
8500 (cdr (assoc (match-string 1)
8501 c-other-decl-block-key-in-symbols-alist))
8502 (max (c-point 'boi paren-pos) (point))))
8503 (t (c-add-syntax 'defun-block-intro nil))))
8504
8505 (c-add-syntax 'statement-block-intro nil)))
8506
8507 (if (= paren-pos boi)
8508 ;; Always done if the open brace was at boi. The
8509 ;; c-beginning-of-statement-1 call above is necessary
8510 ;; anyway, to decide the type of block-intro to add.
8511 (goto-char paren-pos)
8512 (setq boi (c-point 'boi)))
8513 ))
8514
8515 ;; Fill in the current point as the anchor for all the symbols
8516 ;; added above.
8517 (let ((p c-syntactic-context) q)
8518 (while (not (eq p syntax-last))
8519 (setq q (cdr (car p))) ; e.g. (nil 28) [from (arglist-cont-nonempty nil 28)]
8520 (while q
8521 (unless (car q)
8522 (setcar q (point)))
8523 (setq q (cdr q)))
8524 (setq p (cdr p))))
8525 )))
8526
8527 (defun c-add-class-syntax (symbol
8528 containing-decl-open
8529 containing-decl-start
8530 containing-decl-kwd
8531 paren-state)
8532 ;; The inclass and class-close syntactic symbols are added in
8533 ;; several places and some work is needed to fix everything.
8534 ;; Therefore it's collected here.
8535 ;;
8536 ;; This function might do hidden buffer changes.
8537 (goto-char containing-decl-open)
8538 (if (and (eq symbol 'inclass) (= (point) (c-point 'boi)))
8539 (progn
8540 (c-add-syntax symbol containing-decl-open)
8541 containing-decl-open)
8542 (goto-char containing-decl-start)
8543 ;; Ought to use `c-add-stmt-syntax' instead of backing up to boi
8544 ;; here, but we have to do like this for compatibility.
8545 (back-to-indentation)
8546 (c-add-syntax symbol (point))
8547 (if (and (c-keyword-member containing-decl-kwd
8548 'c-inexpr-class-kwds)
8549 (/= containing-decl-start (c-point 'boi containing-decl-start)))
8550 (c-add-syntax 'inexpr-class))
8551 (point)))
8552
8553 (defun c-guess-continued-construct (indent-point
8554 char-after-ip
8555 beg-of-same-or-containing-stmt
8556 containing-sexp
8557 paren-state)
8558 ;; This function contains the decision tree reached through both
8559 ;; cases 18 and 10. It's a continued statement or top level
8560 ;; construct of some kind.
8561 ;;
8562 ;; This function might do hidden buffer changes.
8563
8564 (let (special-brace-list placeholder)
8565 (goto-char indent-point)
8566 (skip-chars-forward " \t")
8567
8568 (cond
8569 ;; (CASE A removed.)
8570 ;; CASE B: open braces for class or brace-lists
8571 ((setq special-brace-list
8572 (or (and c-special-brace-lists
8573 (c-looking-at-special-brace-list))
8574 (eq char-after-ip ?{)))
8575
8576 (cond
8577 ;; CASE B.1: class-open
8578 ((save-excursion
8579 (and (eq (char-after) ?{)
8580 (c-looking-at-decl-block containing-sexp t)
8581 (setq beg-of-same-or-containing-stmt (point))))
8582 (c-add-syntax 'class-open beg-of-same-or-containing-stmt))
8583
8584 ;; CASE B.2: brace-list-open
8585 ((or (consp special-brace-list)
8586 (save-excursion
8587 (goto-char beg-of-same-or-containing-stmt)
8588 (c-syntactic-re-search-forward "=\\([^=]\\|$\\)"
8589 indent-point t t t)))
8590 ;; The most semantically accurate symbol here is
8591 ;; brace-list-open, but we normally report it simply as a
8592 ;; statement-cont. The reason is that one normally adjusts
8593 ;; brace-list-open for brace lists as top-level constructs,
8594 ;; and brace lists inside statements is a completely different
8595 ;; context. C.f. case 5A.3.
8596 (c-beginning-of-statement-1 containing-sexp)
8597 (c-add-stmt-syntax (if c-auto-newline-analysis
8598 ;; Turn off the dwim above when we're
8599 ;; analyzing the nature of the brace
8600 ;; for the auto newline feature.
8601 'brace-list-open
8602 'statement-cont)
8603 nil nil
8604 containing-sexp paren-state))
8605
8606 ;; CASE B.3: The body of a function declared inside a normal
8607 ;; block. Can occur e.g. in Pike and when using gcc
8608 ;; extensions, but watch out for macros followed by blocks.
8609 ;; C.f. cases E, 16F and 17G.
8610 ((and (not (c-at-statement-start-p))
8611 (eq (c-beginning-of-statement-1 containing-sexp nil nil t)
8612 'same)
8613 (save-excursion
8614 (let ((c-recognize-typeless-decls nil))
8615 ;; Turn off recognition of constructs that lacks a
8616 ;; type in this case, since that's more likely to be
8617 ;; a macro followed by a block.
8618 (c-forward-decl-or-cast-1 (c-point 'bosws) nil nil))))
8619 (c-add-stmt-syntax 'defun-open nil t
8620 containing-sexp paren-state))
8621
8622 ;; CASE B.4: Continued statement with block open. The most
8623 ;; accurate analysis is perhaps `statement-cont' together with
8624 ;; `block-open' but we play DWIM and use `substatement-open'
8625 ;; instead. The rationaly is that this typically is a macro
8626 ;; followed by a block which makes it very similar to a
8627 ;; statement with a substatement block.
8628 (t
8629 (c-add-stmt-syntax 'substatement-open nil nil
8630 containing-sexp paren-state))
8631 ))
8632
8633 ;; CASE C: iostream insertion or extraction operator
8634 ((and (looking-at "\\(<<\\|>>\\)\\([^=]\\|$\\)")
8635 (save-excursion
8636 (goto-char beg-of-same-or-containing-stmt)
8637 ;; If there is no preceding streamop in the statement
8638 ;; then indent this line as a normal statement-cont.
8639 (when (c-syntactic-re-search-forward
8640 "\\(<<\\|>>\\)\\([^=]\\|$\\)" indent-point 'move t t)
8641 (c-add-syntax 'stream-op (c-point 'boi))
8642 t))))
8643
8644 ;; CASE E: In the "K&R region" of a function declared inside a
8645 ;; normal block. C.f. case B.3.
8646 ((and (save-excursion
8647 ;; Check that the next token is a '{'. This works as
8648 ;; long as no language that allows nested function
8649 ;; definitions allows stuff like member init lists, K&R
8650 ;; declarations or throws clauses there.
8651 ;;
8652 ;; Note that we do a forward search for something ahead
8653 ;; of the indentation line here. That's not good since
8654 ;; the user might not have typed it yet. Unfortunately
8655 ;; it's exceedingly tricky to recognize a function
8656 ;; prototype in a code block without resorting to this.
8657 (c-forward-syntactic-ws)
8658 (eq (char-after) ?{))
8659 (not (c-at-statement-start-p))
8660 (eq (c-beginning-of-statement-1 containing-sexp nil nil t)
8661 'same)
8662 (save-excursion
8663 (let ((c-recognize-typeless-decls nil))
8664 ;; Turn off recognition of constructs that lacks a
8665 ;; type in this case, since that's more likely to be
8666 ;; a macro followed by a block.
8667 (c-forward-decl-or-cast-1 (c-point 'bosws) nil nil))))
8668 (c-add-stmt-syntax 'func-decl-cont nil t
8669 containing-sexp paren-state))
8670
8671 ;;CASE F: continued statement and the only preceding items are
8672 ;;annotations.
8673 ((and (c-major-mode-is 'java-mode)
8674 (setq placeholder (point))
8675 (c-beginning-of-statement-1)
8676 (progn
8677 (while (and (c-forward-annotation)
8678 (< (point) placeholder))
8679 (c-forward-syntactic-ws))
8680 t)
8681 (prog1
8682 (>= (point) placeholder)
8683 (goto-char placeholder)))
8684 (c-beginning-of-statement-1 containing-sexp)
8685 (c-add-syntax 'annotation-var-cont (point)))
8686
8687 ;; CASE D: continued statement.
8688 (t
8689 (c-beginning-of-statement-1 containing-sexp)
8690 (c-add-stmt-syntax 'statement-cont nil nil
8691 containing-sexp paren-state))
8692 )))
8693
8694 ;; The next autoload was added by RMS on 2005/8/9 - don't know why (ACM,
8695 ;; 2005/11/29).
8696 ;;;###autoload
8697 (defun c-guess-basic-syntax ()
8698 "Return the syntactic context of the current line."
8699 (save-excursion
8700 (beginning-of-line)
8701 (c-save-buffer-state
8702 ((indent-point (point))
8703 (case-fold-search nil)
8704 ;; A whole ugly bunch of various temporary variables. Have
8705 ;; to declare them here since it's not possible to declare
8706 ;; a variable with only the scope of a cond test and the
8707 ;; following result clauses, and most of this function is a
8708 ;; single gigantic cond. :P
8709 literal char-before-ip before-ws-ip char-after-ip macro-start
8710 in-macro-expr c-syntactic-context placeholder c-in-literal-cache
8711 step-type tmpsymbol keyword injava-inher special-brace-list tmp-pos
8712 containing-<
8713 ;; The following record some positions for the containing
8714 ;; declaration block if we're directly within one:
8715 ;; `containing-decl-open' is the position of the open
8716 ;; brace. `containing-decl-start' is the start of the
8717 ;; declaration. `containing-decl-kwd' is the keyword
8718 ;; symbol of the keyword that tells what kind of block it
8719 ;; is.
8720 containing-decl-open
8721 containing-decl-start
8722 containing-decl-kwd
8723 ;; The open paren of the closest surrounding sexp or nil if
8724 ;; there is none.
8725 containing-sexp
8726 ;; The position after the closest preceding brace sexp
8727 ;; (nested sexps are ignored), or the position after
8728 ;; `containing-sexp' if there is none, or (point-min) if
8729 ;; `containing-sexp' is nil.
8730 lim
8731 ;; The paren state outside `containing-sexp', or at
8732 ;; `indent-point' if `containing-sexp' is nil.
8733 (paren-state (c-parse-state))
8734 ;; There's always at most one syntactic element which got
8735 ;; an anchor pos. It's stored in syntactic-relpos.
8736 syntactic-relpos
8737 (c-stmt-delim-chars c-stmt-delim-chars))
8738
8739 ;; Check if we're directly inside an enclosing declaration
8740 ;; level block.
8741 (when (and (setq containing-sexp
8742 (c-most-enclosing-brace paren-state))
8743 (progn
8744 (goto-char containing-sexp)
8745 (eq (char-after) ?{))
8746 (setq placeholder
8747 (c-looking-at-decl-block
8748 (c-most-enclosing-brace paren-state
8749 containing-sexp)
8750 t)))
8751 (setq containing-decl-open containing-sexp
8752 containing-decl-start (point)
8753 containing-sexp nil)
8754 (goto-char placeholder)
8755 (setq containing-decl-kwd (and (looking-at c-keywords-regexp)
8756 (c-keyword-sym (match-string 1)))))
8757
8758 ;; Init some position variables.
8759 (if c-state-cache
8760 (progn
8761 (setq containing-sexp (car paren-state)
8762 paren-state (cdr paren-state))
8763 (if (consp containing-sexp)
8764 (progn
8765 (setq lim (cdr containing-sexp))
8766 (if (cdr c-state-cache)
8767 ;; Ignore balanced paren. The next entry
8768 ;; can't be another one.
8769 (setq containing-sexp (car (cdr c-state-cache))
8770 paren-state (cdr paren-state))
8771 ;; If there is no surrounding open paren then
8772 ;; put the last balanced pair back on paren-state.
8773 (setq paren-state (cons containing-sexp paren-state)
8774 containing-sexp nil)))
8775 (setq lim (1+ containing-sexp))))
8776 (setq lim (point-min)))
8777
8778 ;; If we're in a parenthesis list then ',' delimits the
8779 ;; "statements" rather than being an operator (with the
8780 ;; exception of the "for" clause). This difference is
8781 ;; typically only noticeable when statements are used in macro
8782 ;; arglists.
8783 (when (and containing-sexp
8784 (eq (char-after containing-sexp) ?\())
8785 (setq c-stmt-delim-chars c-stmt-delim-chars-with-comma))
8786 ;; cache char before and after indent point, and move point to
8787 ;; the most likely position to perform the majority of tests
8788 (goto-char indent-point)
8789 (c-backward-syntactic-ws lim)
8790 (setq before-ws-ip (point)
8791 char-before-ip (char-before))
8792 (goto-char indent-point)
8793 (skip-chars-forward " \t")
8794 (setq char-after-ip (char-after))
8795
8796 ;; are we in a literal?
8797 (setq literal (c-in-literal lim))
8798
8799 ;; now figure out syntactic qualities of the current line
8800 (cond
8801
8802 ;; CASE 1: in a string.
8803 ((eq literal 'string)
8804 (c-add-syntax 'string (c-point 'bopl)))
8805
8806 ;; CASE 2: in a C or C++ style comment.
8807 ((and (memq literal '(c c++))
8808 ;; This is a kludge for XEmacs where we use
8809 ;; `buffer-syntactic-context', which doesn't correctly
8810 ;; recognize "\*/" to end a block comment.
8811 ;; `parse-partial-sexp' which is used by
8812 ;; `c-literal-limits' will however do that in most
8813 ;; versions, which results in that we get nil from
8814 ;; `c-literal-limits' even when `c-in-literal' claims
8815 ;; we're inside a comment.
8816 (setq placeholder (c-literal-limits lim)))
8817 (c-add-syntax literal (car placeholder)))
8818
8819 ;; CASE 3: in a cpp preprocessor macro continuation.
8820 ((and (save-excursion
8821 (when (c-beginning-of-macro)
8822 (setq macro-start (point))))
8823 (/= macro-start (c-point 'boi))
8824 (progn
8825 (setq tmpsymbol 'cpp-macro-cont)
8826 (or (not c-syntactic-indentation-in-macros)
8827 (save-excursion
8828 (goto-char macro-start)
8829 ;; If at the beginning of the body of a #define
8830 ;; directive then analyze as cpp-define-intro
8831 ;; only. Go on with the syntactic analysis
8832 ;; otherwise. in-macro-expr is set if we're in a
8833 ;; cpp expression, i.e. before the #define body
8834 ;; or anywhere in a non-#define directive.
8835 (if (c-forward-to-cpp-define-body)
8836 (let ((indent-boi (c-point 'boi indent-point)))
8837 (setq in-macro-expr (> (point) indent-boi)
8838 tmpsymbol 'cpp-define-intro)
8839 (= (point) indent-boi))
8840 (setq in-macro-expr t)
8841 nil)))))
8842 (c-add-syntax tmpsymbol macro-start)
8843 (setq macro-start nil))
8844
8845 ;; CASE 11: an else clause?
8846 ((looking-at "else\\>[^_]")
8847 (c-beginning-of-statement-1 containing-sexp)
8848 (c-add-stmt-syntax 'else-clause nil t
8849 containing-sexp paren-state))
8850
8851 ;; CASE 12: while closure of a do/while construct?
8852 ((and (looking-at "while\\>[^_]")
8853 (save-excursion
8854 (prog1 (eq (c-beginning-of-statement-1 containing-sexp)
8855 'beginning)
8856 (setq placeholder (point)))))
8857 (goto-char placeholder)
8858 (c-add-stmt-syntax 'do-while-closure nil t
8859 containing-sexp paren-state))
8860
8861 ;; CASE 13: A catch or finally clause? This case is simpler
8862 ;; than if-else and do-while, because a block is required
8863 ;; after every try, catch and finally.
8864 ((save-excursion
8865 (and (cond ((c-major-mode-is 'c++-mode)
8866 (looking-at "catch\\>[^_]"))
8867 ((c-major-mode-is 'java-mode)
8868 (looking-at "\\(catch\\|finally\\)\\>[^_]")))
8869 (and (c-safe (c-backward-syntactic-ws)
8870 (c-backward-sexp)
8871 t)
8872 (eq (char-after) ?{)
8873 (c-safe (c-backward-syntactic-ws)
8874 (c-backward-sexp)
8875 t)
8876 (if (eq (char-after) ?\()
8877 (c-safe (c-backward-sexp) t)
8878 t))
8879 (looking-at "\\(try\\|catch\\)\\>[^_]")
8880 (setq placeholder (point))))
8881 (goto-char placeholder)
8882 (c-add-stmt-syntax 'catch-clause nil t
8883 containing-sexp paren-state))
8884
8885 ;; CASE 18: A substatement we can recognize by keyword.
8886 ((save-excursion
8887 (and c-opt-block-stmt-key
8888 (not (eq char-before-ip ?\;))
8889 (not (c-at-vsemi-p before-ws-ip))
8890 (not (memq char-after-ip '(?\) ?\] ?,)))
8891 (or (not (eq char-before-ip ?}))
8892 (c-looking-at-inexpr-block-backward c-state-cache))
8893 (> (point)
8894 (progn
8895 ;; Ought to cache the result from the
8896 ;; c-beginning-of-statement-1 calls here.
8897 (setq placeholder (point))
8898 (while (eq (setq step-type
8899 (c-beginning-of-statement-1 lim))
8900 'label))
8901 (if (eq step-type 'previous)
8902 (goto-char placeholder)
8903 (setq placeholder (point))
8904 (if (and (eq step-type 'same)
8905 (not (looking-at c-opt-block-stmt-key)))
8906 ;; Step up to the containing statement if we
8907 ;; stayed in the same one.
8908 (let (step)
8909 (while (eq
8910 (setq step
8911 (c-beginning-of-statement-1 lim))
8912 'label))
8913 (if (eq step 'up)
8914 (setq placeholder (point))
8915 ;; There was no containing statement afterall.
8916 (goto-char placeholder)))))
8917 placeholder))
8918 (if (looking-at c-block-stmt-2-key)
8919 ;; Require a parenthesis after these keywords.
8920 ;; Necessary to catch e.g. synchronized in Java,
8921 ;; which can be used both as statement and
8922 ;; modifier.
8923 (and (zerop (c-forward-token-2 1 nil))
8924 (eq (char-after) ?\())
8925 (looking-at c-opt-block-stmt-key))))
8926
8927 (if (eq step-type 'up)
8928 ;; CASE 18A: Simple substatement.
8929 (progn
8930 (goto-char placeholder)
8931 (cond
8932 ((eq char-after-ip ?{)
8933 (c-add-stmt-syntax 'substatement-open nil nil
8934 containing-sexp paren-state))
8935 ((save-excursion
8936 (goto-char indent-point)
8937 (back-to-indentation)
8938 (c-forward-label))
8939 (c-add-stmt-syntax 'substatement-label nil nil
8940 containing-sexp paren-state))
8941 (t
8942 (c-add-stmt-syntax 'substatement nil nil
8943 containing-sexp paren-state))))
8944
8945 ;; CASE 18B: Some other substatement. This is shared
8946 ;; with case 10.
8947 (c-guess-continued-construct indent-point
8948 char-after-ip
8949 placeholder
8950 lim
8951 paren-state)))
8952
8953 ;; CASE 14: A case or default label
8954 ((looking-at c-label-kwds-regexp)
8955 (if containing-sexp
8956 (progn
8957 (goto-char containing-sexp)
8958 (setq lim (c-most-enclosing-brace c-state-cache
8959 containing-sexp))
8960 (c-backward-to-block-anchor lim)
8961 (c-add-stmt-syntax 'case-label nil t lim paren-state))
8962 ;; Got a bogus label at the top level. In lack of better
8963 ;; alternatives, anchor it on (point-min).
8964 (c-add-syntax 'case-label (point-min))))
8965
8966 ;; CASE 15: any other label
8967 ((save-excursion
8968 (back-to-indentation)
8969 (and (not (looking-at c-syntactic-ws-start))
8970 (c-forward-label)))
8971 (cond (containing-decl-open
8972 (setq placeholder (c-add-class-syntax 'inclass
8973 containing-decl-open
8974 containing-decl-start
8975 containing-decl-kwd
8976 paren-state))
8977 ;; Append access-label with the same anchor point as
8978 ;; inclass gets.
8979 (c-append-syntax 'access-label placeholder))
8980
8981 (containing-sexp
8982 (goto-char containing-sexp)
8983 (setq lim (c-most-enclosing-brace c-state-cache
8984 containing-sexp))
8985 (save-excursion
8986 (setq tmpsymbol
8987 (if (and (eq (c-beginning-of-statement-1 lim) 'up)
8988 (looking-at "switch\\>[^_]"))
8989 ;; If the surrounding statement is a switch then
8990 ;; let's analyze all labels as switch labels, so
8991 ;; that they get lined up consistently.
8992 'case-label
8993 'label)))
8994 (c-backward-to-block-anchor lim)
8995 (c-add-stmt-syntax tmpsymbol nil t lim paren-state))
8996
8997 (t
8998 ;; A label on the top level. Treat it as a class
8999 ;; context. (point-min) is the closest we get to the
9000 ;; class open brace.
9001 (c-add-syntax 'access-label (point-min)))))
9002
9003 ;; CASE 4: In-expression statement. C.f. cases 7B, 16A and
9004 ;; 17E.
9005 ((setq placeholder (c-looking-at-inexpr-block
9006 (c-safe-position containing-sexp paren-state)
9007 containing-sexp
9008 ;; Have to turn on the heuristics after
9009 ;; the point even though it doesn't work
9010 ;; very well. C.f. test case class-16.pike.
9011 t))
9012 (setq tmpsymbol (assq (car placeholder)
9013 '((inexpr-class . class-open)
9014 (inexpr-statement . block-open))))
9015 (if tmpsymbol
9016 ;; It's a statement block or an anonymous class.
9017 (setq tmpsymbol (cdr tmpsymbol))
9018 ;; It's a Pike lambda. Check whether we are between the
9019 ;; lambda keyword and the argument list or at the defun
9020 ;; opener.
9021 (setq tmpsymbol (if (eq char-after-ip ?{)
9022 'inline-open
9023 'lambda-intro-cont)))
9024 (goto-char (cdr placeholder))
9025 (back-to-indentation)
9026 (c-add-stmt-syntax tmpsymbol nil t
9027 (c-most-enclosing-brace c-state-cache (point))
9028 paren-state)
9029 (unless (eq (point) (cdr placeholder))
9030 (c-add-syntax (car placeholder))))
9031
9032 ;; CASE 5: Line is inside a declaration level block or at top level.
9033 ((or containing-decl-open (null containing-sexp))
9034 (cond
9035
9036 ;; CASE 5A: we are looking at a defun, brace list, class,
9037 ;; or inline-inclass method opening brace
9038 ((setq special-brace-list
9039 (or (and c-special-brace-lists
9040 (c-looking-at-special-brace-list))
9041 (eq char-after-ip ?{)))
9042 (cond
9043
9044 ;; CASE 5A.1: Non-class declaration block open.
9045 ((save-excursion
9046 (let (tmp)
9047 (and (eq char-after-ip ?{)
9048 (setq tmp (c-looking-at-decl-block containing-sexp t))
9049 (progn
9050 (setq placeholder (point))
9051 (goto-char tmp)
9052 (looking-at c-symbol-key))
9053 (c-keyword-member
9054 (c-keyword-sym (setq keyword (match-string 0)))
9055 'c-other-block-decl-kwds))))
9056 (goto-char placeholder)
9057 (c-add-stmt-syntax
9058 (if (string-equal keyword "extern")
9059 ;; Special case for extern-lang-open.
9060 'extern-lang-open
9061 (intern (concat keyword "-open")))
9062 nil t containing-sexp paren-state))
9063
9064 ;; CASE 5A.2: we are looking at a class opening brace
9065 ((save-excursion
9066 (goto-char indent-point)
9067 (skip-chars-forward " \t")
9068 (and (eq (char-after) ?{)
9069 (c-looking-at-decl-block containing-sexp t)
9070 (setq placeholder (point))))
9071 (c-add-syntax 'class-open placeholder))
9072
9073 ;; CASE 5A.3: brace list open
9074 ((save-excursion
9075 (c-beginning-of-decl-1 lim)
9076 (while (looking-at c-specifier-key)
9077 (goto-char (match-end 1))
9078 (c-forward-syntactic-ws indent-point))
9079 (setq placeholder (c-point 'boi))
9080 (or (consp special-brace-list)
9081 (and (or (save-excursion
9082 (goto-char indent-point)
9083 (setq tmpsymbol nil)
9084 (while (and (> (point) placeholder)
9085 (zerop (c-backward-token-2 1 t))
9086 (/= (char-after) ?=))
9087 (and c-opt-inexpr-brace-list-key
9088 (not tmpsymbol)
9089 (looking-at c-opt-inexpr-brace-list-key)
9090 (setq tmpsymbol 'topmost-intro-cont)))
9091 (eq (char-after) ?=))
9092 (looking-at c-brace-list-key))
9093 (save-excursion
9094 (while (and (< (point) indent-point)
9095 (zerop (c-forward-token-2 1 t))
9096 (not (memq (char-after) '(?\; ?\()))))
9097 (not (memq (char-after) '(?\; ?\()))
9098 ))))
9099 (if (and (not c-auto-newline-analysis)
9100 (c-major-mode-is 'java-mode)
9101 (eq tmpsymbol 'topmost-intro-cont))
9102 ;; We're in Java and have found that the open brace
9103 ;; belongs to a "new Foo[]" initialization list,
9104 ;; which means the brace list is part of an
9105 ;; expression and not a top level definition. We
9106 ;; therefore treat it as any topmost continuation
9107 ;; even though the semantically correct symbol still
9108 ;; is brace-list-open, on the same grounds as in
9109 ;; case B.2.
9110 (progn
9111 (c-beginning-of-statement-1 lim)
9112 (c-add-syntax 'topmost-intro-cont (c-point 'boi)))
9113 (c-add-syntax 'brace-list-open placeholder)))
9114
9115 ;; CASE 5A.4: inline defun open
9116 ((and containing-decl-open
9117 (not (c-keyword-member containing-decl-kwd
9118 'c-other-block-decl-kwds)))
9119 (c-add-syntax 'inline-open)
9120 (c-add-class-syntax 'inclass
9121 containing-decl-open
9122 containing-decl-start
9123 containing-decl-kwd
9124 paren-state))
9125
9126 ;; CASE 5A.5: ordinary defun open
9127 (t
9128 (save-excursion
9129 (c-beginning-of-decl-1 lim)
9130 (while (looking-at c-specifier-key)
9131 (goto-char (match-end 1))
9132 (c-forward-syntactic-ws indent-point))
9133 (c-add-syntax 'defun-open (c-point 'boi))
9134 ;; Bogus to use bol here, but it's the legacy. (Resolved,
9135 ;; 2007-11-09)
9136 ))))
9137
9138 ;; CASE 5B: After a function header but before the body (or
9139 ;; the ending semicolon if there's no body).
9140 ((save-excursion
9141 (when (setq placeholder (c-just-after-func-arglist-p lim))
9142 (setq tmp-pos (point))))
9143 (cond
9144
9145 ;; CASE 5B.1: Member init list.
9146 ((eq (char-after tmp-pos) ?:)
9147 (if (or (> tmp-pos indent-point)
9148 (= (c-point 'bosws) (1+ tmp-pos)))
9149 (progn
9150 ;; There is no preceding member init clause.
9151 ;; Indent relative to the beginning of indentation
9152 ;; for the topmost-intro line that contains the
9153 ;; prototype's open paren.
9154 (goto-char placeholder)
9155 (c-add-syntax 'member-init-intro (c-point 'boi)))
9156 ;; Indent relative to the first member init clause.
9157 (goto-char (1+ tmp-pos))
9158 (c-forward-syntactic-ws)
9159 (c-add-syntax 'member-init-cont (point))))
9160
9161 ;; CASE 5B.2: K&R arg decl intro
9162 ((and c-recognize-knr-p
9163 (c-in-knr-argdecl lim))
9164 (c-beginning-of-statement-1 lim)
9165 (c-add-syntax 'knr-argdecl-intro (c-point 'boi))
9166 (if containing-decl-open
9167 (c-add-class-syntax 'inclass
9168 containing-decl-open
9169 containing-decl-start
9170 containing-decl-kwd
9171 paren-state)))
9172
9173 ;; CASE 5B.4: Nether region after a C++ or Java func
9174 ;; decl, which could include a `throws' declaration.
9175 (t
9176 (c-beginning-of-statement-1 lim)
9177 (c-add-syntax 'func-decl-cont (c-point 'boi))
9178 )))
9179
9180 ;; CASE 5C: inheritance line. could be first inheritance
9181 ;; line, or continuation of a multiple inheritance
9182 ((or (and (c-major-mode-is 'c++-mode)
9183 (progn
9184 (when (eq char-after-ip ?,)
9185 (skip-chars-forward " \t")
9186 (forward-char))
9187 (looking-at c-opt-postfix-decl-spec-key)))
9188 (and (or (eq char-before-ip ?:)
9189 ;; watch out for scope operator
9190 (save-excursion
9191 (and (eq char-after-ip ?:)
9192 (c-safe (forward-char 1) t)
9193 (not (eq (char-after) ?:))
9194 )))
9195 (save-excursion
9196 (c-backward-syntactic-ws lim)
9197 (if (eq char-before-ip ?:)
9198 (progn
9199 (forward-char -1)
9200 (c-backward-syntactic-ws lim)))
9201 (back-to-indentation)
9202 (looking-at c-class-key)))
9203 ;; for Java
9204 (and (c-major-mode-is 'java-mode)
9205 (let ((fence (save-excursion
9206 (c-beginning-of-statement-1 lim)
9207 (point)))
9208 cont done)
9209 (save-excursion
9210 (while (not done)
9211 (cond ((looking-at c-opt-postfix-decl-spec-key)
9212 (setq injava-inher (cons cont (point))
9213 done t))
9214 ((or (not (c-safe (c-forward-sexp -1) t))
9215 (<= (point) fence))
9216 (setq done t))
9217 )
9218 (setq cont t)))
9219 injava-inher)
9220 (not (c-crosses-statement-barrier-p (cdr injava-inher)
9221 (point)))
9222 ))
9223 (cond
9224
9225 ;; CASE 5C.1: non-hanging colon on an inher intro
9226 ((eq char-after-ip ?:)
9227 (c-beginning-of-statement-1 lim)
9228 (c-add-syntax 'inher-intro (c-point 'boi))
9229 ;; don't add inclass symbol since relative point already
9230 ;; contains any class offset
9231 )
9232
9233 ;; CASE 5C.2: hanging colon on an inher intro
9234 ((eq char-before-ip ?:)
9235 (c-beginning-of-statement-1 lim)
9236 (c-add-syntax 'inher-intro (c-point 'boi))
9237 (if containing-decl-open
9238 (c-add-class-syntax 'inclass
9239 containing-decl-open
9240 containing-decl-start
9241 containing-decl-kwd
9242 paren-state)))
9243
9244 ;; CASE 5C.3: in a Java implements/extends
9245 (injava-inher
9246 (let ((where (cdr injava-inher))
9247 (cont (car injava-inher)))
9248 (goto-char where)
9249 (cond ((looking-at "throws\\>[^_]")
9250 (c-add-syntax 'func-decl-cont
9251 (progn (c-beginning-of-statement-1 lim)
9252 (c-point 'boi))))
9253 (cont (c-add-syntax 'inher-cont where))
9254 (t (c-add-syntax 'inher-intro
9255 (progn (goto-char (cdr injava-inher))
9256 (c-beginning-of-statement-1 lim)
9257 (point))))
9258 )))
9259
9260 ;; CASE 5C.4: a continued inheritance line
9261 (t
9262 (c-beginning-of-inheritance-list lim)
9263 (c-add-syntax 'inher-cont (point))
9264 ;; don't add inclass symbol since relative point already
9265 ;; contains any class offset
9266 )))
9267
9268 ;; CASE 5D: this could be a top-level initialization, a
9269 ;; member init list continuation, or a template argument
9270 ;; list continuation.
9271 ((save-excursion
9272 ;; Note: We use the fact that lim is always after any
9273 ;; preceding brace sexp.
9274 (if c-recognize-<>-arglists
9275 (while (and
9276 (progn
9277 (c-syntactic-skip-backward "^;,=<>" lim t)
9278 (> (point) lim))
9279 (or
9280 (when c-overloadable-operators-regexp
9281 (when (setq placeholder (c-after-special-operator-id lim))
9282 (goto-char placeholder)
9283 t))
9284 (cond
9285 ((eq (char-before) ?>)
9286 (or (c-backward-<>-arglist nil lim)
9287 (backward-char))
9288 t)
9289 ((eq (char-before) ?<)
9290 (backward-char)
9291 (if (save-excursion
9292 (c-forward-<>-arglist nil))
9293 (progn (forward-char)
9294 nil)
9295 t))
9296 (t nil)))))
9297 ;; NB: No c-after-special-operator-id stuff in this
9298 ;; clause - we assume only C++ needs it.
9299 (c-syntactic-skip-backward "^;,=" lim t))
9300 (memq (char-before) '(?, ?= ?<)))
9301 (cond
9302
9303 ;; CASE 5D.3: perhaps a template list continuation?
9304 ((and (c-major-mode-is 'c++-mode)
9305 (save-excursion
9306 (save-restriction
9307 (c-with-syntax-table c++-template-syntax-table
9308 (goto-char indent-point)
9309 (setq placeholder (c-up-list-backward))
9310 (and placeholder
9311 (eq (char-after placeholder) ?<))))))
9312 (c-with-syntax-table c++-template-syntax-table
9313 (goto-char placeholder)
9314 (c-beginning-of-statement-1 lim t)
9315 (if (save-excursion
9316 (c-backward-syntactic-ws lim)
9317 (eq (char-before) ?<))
9318 ;; In a nested template arglist.
9319 (progn
9320 (goto-char placeholder)
9321 (c-syntactic-skip-backward "^,;" lim t)
9322 (c-forward-syntactic-ws))
9323 (back-to-indentation)))
9324 ;; FIXME: Should use c-add-stmt-syntax, but it's not yet
9325 ;; template aware.
9326 (c-add-syntax 'template-args-cont (point) placeholder))
9327
9328 ;; CASE 5D.4: perhaps a multiple inheritance line?
9329 ((and (c-major-mode-is 'c++-mode)
9330 (save-excursion
9331 (c-beginning-of-statement-1 lim)
9332 (setq placeholder (point))
9333 (if (looking-at "static\\>[^_]")
9334 (c-forward-token-2 1 nil indent-point))
9335 (and (looking-at c-class-key)
9336 (zerop (c-forward-token-2 2 nil indent-point))
9337 (if (eq (char-after) ?<)
9338 (c-with-syntax-table c++-template-syntax-table
9339 (zerop (c-forward-token-2 1 t indent-point)))
9340 t)
9341 (eq (char-after) ?:))))
9342 (goto-char placeholder)
9343 (c-add-syntax 'inher-cont (c-point 'boi)))
9344
9345 ;; CASE 5D.5: Continuation of the "expression part" of a
9346 ;; top level construct. Or, perhaps, an unrecognised construct.
9347 (t
9348 (while (and (setq placeholder (point))
9349 (eq (car (c-beginning-of-decl-1 containing-sexp))
9350 'same)
9351 (save-excursion
9352 (c-backward-syntactic-ws)
9353 (eq (char-before) ?}))
9354 (< (point) placeholder)))
9355 (c-add-stmt-syntax
9356 (cond
9357 ((eq (point) placeholder) 'statement) ; unrecognised construct
9358 ;; A preceding comma at the top level means that a
9359 ;; new variable declaration starts here. Use
9360 ;; topmost-intro-cont for it, for consistency with
9361 ;; the first variable declaration. C.f. case 5N.
9362 ((eq char-before-ip ?,) 'topmost-intro-cont)
9363 (t 'statement-cont))
9364 nil nil containing-sexp paren-state))
9365 ))
9366
9367 ;; CASE 5F: Close of a non-class declaration level block.
9368 ((and (eq char-after-ip ?})
9369 (c-keyword-member containing-decl-kwd
9370 'c-other-block-decl-kwds))
9371 ;; This is inconsistent: Should use `containing-decl-open'
9372 ;; here if it's at boi, like in case 5J.
9373 (goto-char containing-decl-start)
9374 (c-add-stmt-syntax
9375 (if (string-equal (symbol-name containing-decl-kwd) "extern")
9376 ;; Special case for compatibility with the
9377 ;; extern-lang syntactic symbols.
9378 'extern-lang-close
9379 (intern (concat (symbol-name containing-decl-kwd)
9380 "-close")))
9381 nil t
9382 (c-most-enclosing-brace paren-state (point))
9383 paren-state))
9384
9385 ;; CASE 5G: we are looking at the brace which closes the
9386 ;; enclosing nested class decl
9387 ((and containing-sexp
9388 (eq char-after-ip ?})
9389 (eq containing-decl-open containing-sexp))
9390 (c-add-class-syntax 'class-close
9391 containing-decl-open
9392 containing-decl-start
9393 containing-decl-kwd
9394 paren-state))
9395
9396 ;; CASE 5H: we could be looking at subsequent knr-argdecls
9397 ((and c-recognize-knr-p
9398 (not containing-sexp) ; can't be knr inside braces.
9399 (not (eq char-before-ip ?}))
9400 (save-excursion
9401 (setq placeholder (cdr (c-beginning-of-decl-1 lim)))
9402 (and placeholder
9403 ;; Do an extra check to avoid tripping up on
9404 ;; statements that occur in invalid contexts
9405 ;; (e.g. in macro bodies where we don't really
9406 ;; know the context of what we're looking at).
9407 (not (and c-opt-block-stmt-key
9408 (looking-at c-opt-block-stmt-key)))))
9409 (< placeholder indent-point))
9410 (goto-char placeholder)
9411 (c-add-syntax 'knr-argdecl (point)))
9412
9413 ;; CASE 5I: ObjC method definition.
9414 ((and c-opt-method-key
9415 (looking-at c-opt-method-key))
9416 (c-beginning-of-statement-1 nil t)
9417 (if (= (point) indent-point)
9418 ;; Handle the case when it's the first (non-comment)
9419 ;; thing in the buffer. Can't look for a 'same return
9420 ;; value from cbos1 since ObjC directives currently
9421 ;; aren't recognized fully, so that we get 'same
9422 ;; instead of 'previous if it moved over a preceding
9423 ;; directive.
9424 (goto-char (point-min)))
9425 (c-add-syntax 'objc-method-intro (c-point 'boi)))
9426
9427 ;; CASE 5P: AWK pattern or function or continuation
9428 ;; thereof.
9429 ((c-major-mode-is 'awk-mode)
9430 (setq placeholder (point))
9431 (c-add-stmt-syntax
9432 (if (and (eq (c-beginning-of-statement-1) 'same)
9433 (/= (point) placeholder))
9434 'topmost-intro-cont
9435 'topmost-intro)
9436 nil nil
9437 containing-sexp paren-state))
9438
9439 ;; CASE 5N: At a variable declaration that follows a class
9440 ;; definition or some other block declaration that doesn't
9441 ;; end at the closing '}'. C.f. case 5D.5.
9442 ((progn
9443 (c-backward-syntactic-ws lim)
9444 (and (eq (char-before) ?})
9445 (save-excursion
9446 (let ((start (point)))
9447 (if (and c-state-cache
9448 (consp (car c-state-cache))
9449 (eq (cdar c-state-cache) (point)))
9450 ;; Speed up the backward search a bit.
9451 (goto-char (caar c-state-cache)))
9452 (c-beginning-of-decl-1 containing-sexp)
9453 (setq placeholder (point))
9454 (if (= start (point))
9455 ;; The '}' is unbalanced.
9456 nil
9457 (c-end-of-decl-1)
9458 (>= (point) indent-point))))))
9459 (goto-char placeholder)
9460 (c-add-stmt-syntax 'topmost-intro-cont nil nil
9461 containing-sexp paren-state))
9462
9463 ;; NOTE: The point is at the end of the previous token here.
9464
9465 ;; CASE 5J: we are at the topmost level, make
9466 ;; sure we skip back past any access specifiers
9467 ((and
9468 ;; A macro continuation line is never at top level.
9469 (not (and macro-start
9470 (> indent-point macro-start)))
9471 (save-excursion
9472 (setq placeholder (point))
9473 (or (memq char-before-ip '(?\; ?{ ?} nil))
9474 (c-at-vsemi-p before-ws-ip)
9475 (when (and (eq char-before-ip ?:)
9476 (eq (c-beginning-of-statement-1 lim)
9477 'label))
9478 (c-backward-syntactic-ws lim)
9479 (setq placeholder (point)))
9480 (and (c-major-mode-is 'objc-mode)
9481 (catch 'not-in-directive
9482 (c-beginning-of-statement-1 lim)
9483 (setq placeholder (point))
9484 (while (and (c-forward-objc-directive)
9485 (< (point) indent-point))
9486 (c-forward-syntactic-ws)
9487 (if (>= (point) indent-point)
9488 (throw 'not-in-directive t))
9489 (setq placeholder (point)))
9490 nil)))))
9491 ;; For historic reasons we anchor at bol of the last
9492 ;; line of the previous declaration. That's clearly
9493 ;; highly bogus and useless, and it makes our lives hard
9494 ;; to remain compatible. :P
9495 (goto-char placeholder)
9496 (c-add-syntax 'topmost-intro (c-point 'bol))
9497 (if containing-decl-open
9498 (if (c-keyword-member containing-decl-kwd
9499 'c-other-block-decl-kwds)
9500 (progn
9501 (goto-char (c-brace-anchor-point containing-decl-open))
9502 (c-add-stmt-syntax
9503 (if (string-equal (symbol-name containing-decl-kwd)
9504 "extern")
9505 ;; Special case for compatibility with the
9506 ;; extern-lang syntactic symbols.
9507 'inextern-lang
9508 (intern (concat "in"
9509 (symbol-name containing-decl-kwd))))
9510 nil t
9511 (c-most-enclosing-brace paren-state (point))
9512 paren-state))
9513 (c-add-class-syntax 'inclass
9514 containing-decl-open
9515 containing-decl-start
9516 containing-decl-kwd
9517 paren-state)))
9518 (when (and c-syntactic-indentation-in-macros
9519 macro-start
9520 (/= macro-start (c-point 'boi indent-point)))
9521 (c-add-syntax 'cpp-define-intro)
9522 (setq macro-start nil)))
9523
9524 ;; CASE 5K: we are at an ObjC method definition
9525 ;; continuation line.
9526 ((and c-opt-method-key
9527 (save-excursion
9528 (c-beginning-of-statement-1 lim)
9529 (beginning-of-line)
9530 (when (looking-at c-opt-method-key)
9531 (setq placeholder (point)))))
9532 (c-add-syntax 'objc-method-args-cont placeholder))
9533
9534 ;; CASE 5L: we are at the first argument of a template
9535 ;; arglist that begins on the previous line.
9536 ((and c-recognize-<>-arglists
9537 (eq (char-before) ?<)
9538 (not (and c-overloadable-operators-regexp
9539 (c-after-special-operator-id lim))))
9540 (c-beginning-of-statement-1 (c-safe-position (point) paren-state))
9541 (c-add-syntax 'template-args-cont (c-point 'boi)))
9542
9543 ;; CASE 5Q: we are at a statement within a macro.
9544 (macro-start
9545 (c-beginning-of-statement-1 containing-sexp)
9546 (c-add-stmt-syntax 'statement nil t containing-sexp paren-state))
9547
9548 ;;CASE 5N: We are at a tompmost continuation line and the only
9549 ;;preceding items are annotations.
9550 ((and (c-major-mode-is 'java-mode)
9551 (setq placeholder (point))
9552 (c-beginning-of-statement-1)
9553 (progn
9554 (while (and (c-forward-annotation))
9555 (c-forward-syntactic-ws))
9556 t)
9557 (prog1
9558 (>= (point) placeholder)
9559 (goto-char placeholder)))
9560 (c-add-syntax 'annotation-top-cont (c-point 'boi)))
9561
9562 ;; CASE 5M: we are at a topmost continuation line
9563 (t
9564 (c-beginning-of-statement-1 (c-safe-position (point) paren-state))
9565 (when (c-major-mode-is 'objc-mode)
9566 (setq placeholder (point))
9567 (while (and (c-forward-objc-directive)
9568 (< (point) indent-point))
9569 (c-forward-syntactic-ws)
9570 (setq placeholder (point)))
9571 (goto-char placeholder))
9572 (c-add-syntax 'topmost-intro-cont (c-point 'boi)))
9573 ))
9574
9575
9576 ;; (CASE 6 has been removed.)
9577
9578 ;; CASE 7: line is an expression, not a statement. Most
9579 ;; likely we are either in a function prototype or a function
9580 ;; call argument list
9581 ((not (or (and c-special-brace-lists
9582 (save-excursion
9583 (goto-char containing-sexp)
9584 (c-looking-at-special-brace-list)))
9585 (eq (char-after containing-sexp) ?{)))
9586 (cond
9587
9588 ;; CASE 7A: we are looking at the arglist closing paren.
9589 ;; C.f. case 7F.
9590 ((memq char-after-ip '(?\) ?\]))
9591 (goto-char containing-sexp)
9592 (setq placeholder (c-point 'boi))
9593 (if (and (c-safe (backward-up-list 1) t)
9594 (>= (point) placeholder))
9595 (progn
9596 (forward-char)
9597 (skip-chars-forward " \t"))
9598 (goto-char placeholder))
9599 (c-add-stmt-syntax 'arglist-close (list containing-sexp) t
9600 (c-most-enclosing-brace paren-state (point))
9601 paren-state))
9602
9603 ;; CASE 19: line is an expression, not a statement, and is directly
9604 ;; contained by a template delimiter. Most likely, we are in a
9605 ;; template arglist within a statement. This case is based on CASE
9606 ;; 7. At some point in the future, we may wish to create more
9607 ;; syntactic symbols such as `template-intro',
9608 ;; `template-cont-nonempty', etc., and distinguish between them as we
9609 ;; do for `arglist-intro' etc. (2009-12-07).
9610 ((and c-recognize-<>-arglists
9611 (setq containing-< (c-up-list-backward indent-point containing-sexp))
9612 (eq (char-after containing-<) ?\<))
9613 (setq placeholder (c-point 'boi containing-<))
9614 (goto-char containing-sexp) ; Most nested Lbrace/Lparen (but not
9615 ; '<') before indent-point.
9616 (if (>= (point) placeholder)
9617 (progn
9618 (forward-char)
9619 (skip-chars-forward " \t"))
9620 (goto-char placeholder))
9621 (c-add-stmt-syntax 'template-args-cont (list containing-<) t
9622 (c-most-enclosing-brace c-state-cache (point))
9623 paren-state))
9624
9625 ;; CASE 7B: Looking at the opening brace of an
9626 ;; in-expression block or brace list. C.f. cases 4, 16A
9627 ;; and 17E.
9628 ((and (eq char-after-ip ?{)
9629 (progn
9630 (setq placeholder (c-inside-bracelist-p (point)
9631 paren-state))
9632 (if placeholder
9633 (setq tmpsymbol '(brace-list-open . inexpr-class))
9634 (setq tmpsymbol '(block-open . inexpr-statement)
9635 placeholder
9636 (cdr-safe (c-looking-at-inexpr-block
9637 (c-safe-position containing-sexp
9638 paren-state)
9639 containing-sexp)))
9640 ;; placeholder is nil if it's a block directly in
9641 ;; a function arglist. That makes us skip out of
9642 ;; this case.
9643 )))
9644 (goto-char placeholder)
9645 (back-to-indentation)
9646 (c-add-stmt-syntax (car tmpsymbol) nil t
9647 (c-most-enclosing-brace paren-state (point))
9648 paren-state)
9649 (if (/= (point) placeholder)
9650 (c-add-syntax (cdr tmpsymbol))))
9651
9652 ;; CASE 7C: we are looking at the first argument in an empty
9653 ;; argument list. Use arglist-close if we're actually
9654 ;; looking at a close paren or bracket.
9655 ((memq char-before-ip '(?\( ?\[))
9656 (goto-char containing-sexp)
9657 (setq placeholder (c-point 'boi))
9658 (if (and (c-safe (backward-up-list 1) t)
9659 (>= (point) placeholder))
9660 (progn
9661 (forward-char)
9662 (skip-chars-forward " \t"))
9663 (goto-char placeholder))
9664 (c-add-stmt-syntax 'arglist-intro (list containing-sexp) t
9665 (c-most-enclosing-brace paren-state (point))
9666 paren-state))
9667
9668 ;; CASE 7D: we are inside a conditional test clause. treat
9669 ;; these things as statements
9670 ((progn
9671 (goto-char containing-sexp)
9672 (and (c-safe (c-forward-sexp -1) t)
9673 (looking-at "\\<for\\>[^_]")))
9674 (goto-char (1+ containing-sexp))
9675 (c-forward-syntactic-ws indent-point)
9676 (if (eq char-before-ip ?\;)
9677 (c-add-syntax 'statement (point))
9678 (c-add-syntax 'statement-cont (point))
9679 ))
9680
9681 ;; CASE 7E: maybe a continued ObjC method call. This is the
9682 ;; case when we are inside a [] bracketed exp, and what
9683 ;; precede the opening bracket is not an identifier.
9684 ((and c-opt-method-key
9685 (eq (char-after containing-sexp) ?\[)
9686 (progn
9687 (goto-char (1- containing-sexp))
9688 (c-backward-syntactic-ws (c-point 'bod))
9689 (if (not (looking-at c-symbol-key))
9690 (c-add-syntax 'objc-method-call-cont containing-sexp))
9691 )))
9692
9693 ;; CASE 7F: we are looking at an arglist continuation line,
9694 ;; but the preceding argument is on the same line as the
9695 ;; opening paren. This case includes multi-line
9696 ;; mathematical paren groupings, but we could be on a
9697 ;; for-list continuation line. C.f. case 7A.
9698 ((progn
9699 (goto-char (1+ containing-sexp))
9700 (< (save-excursion
9701 (c-forward-syntactic-ws)
9702 (point))
9703 (c-point 'bonl)))
9704 (goto-char containing-sexp) ; paren opening the arglist
9705 (setq placeholder (c-point 'boi))
9706 (if (and (c-safe (backward-up-list 1) t)
9707 (>= (point) placeholder))
9708 (progn
9709 (forward-char)
9710 (skip-chars-forward " \t"))
9711 (goto-char placeholder))
9712 (c-add-stmt-syntax 'arglist-cont-nonempty (list containing-sexp) t
9713 (c-most-enclosing-brace c-state-cache (point))
9714 paren-state))
9715
9716 ;; CASE 7G: we are looking at just a normal arglist
9717 ;; continuation line
9718 (t (c-forward-syntactic-ws indent-point)
9719 (c-add-syntax 'arglist-cont (c-point 'boi)))
9720 ))
9721
9722 ;; CASE 8: func-local multi-inheritance line
9723 ((and (c-major-mode-is 'c++-mode)
9724 (save-excursion
9725 (goto-char indent-point)
9726 (skip-chars-forward " \t")
9727 (looking-at c-opt-postfix-decl-spec-key)))
9728 (goto-char indent-point)
9729 (skip-chars-forward " \t")
9730 (cond
9731
9732 ;; CASE 8A: non-hanging colon on an inher intro
9733 ((eq char-after-ip ?:)
9734 (c-backward-syntactic-ws lim)
9735 (c-add-syntax 'inher-intro (c-point 'boi)))
9736
9737 ;; CASE 8B: hanging colon on an inher intro
9738 ((eq char-before-ip ?:)
9739 (c-add-syntax 'inher-intro (c-point 'boi)))
9740
9741 ;; CASE 8C: a continued inheritance line
9742 (t
9743 (c-beginning-of-inheritance-list lim)
9744 (c-add-syntax 'inher-cont (point))
9745 )))
9746
9747 ;; CASE 9: we are inside a brace-list
9748 ((and (not (c-major-mode-is 'awk-mode)) ; Maybe this isn't needed (ACM, 2002/3/29)
9749 (setq special-brace-list
9750 (or (and c-special-brace-lists ;;;; ALWAYS NIL FOR AWK!!
9751 (save-excursion
9752 (goto-char containing-sexp)
9753 (c-looking-at-special-brace-list)))
9754 (c-inside-bracelist-p containing-sexp paren-state))))
9755 (cond
9756
9757 ;; CASE 9A: In the middle of a special brace list opener.
9758 ((and (consp special-brace-list)
9759 (save-excursion
9760 (goto-char containing-sexp)
9761 (eq (char-after) ?\())
9762 (eq char-after-ip (car (cdr special-brace-list))))
9763 (goto-char (car (car special-brace-list)))
9764 (skip-chars-backward " \t")
9765 (if (and (bolp)
9766 (assoc 'statement-cont
9767 (setq placeholder (c-guess-basic-syntax))))
9768 (setq c-syntactic-context placeholder)
9769 (c-beginning-of-statement-1
9770 (c-safe-position (1- containing-sexp) paren-state))
9771 (c-forward-token-2 0)
9772 (while (looking-at c-specifier-key)
9773 (goto-char (match-end 1))
9774 (c-forward-syntactic-ws))
9775 (c-add-syntax 'brace-list-open (c-point 'boi))))
9776
9777 ;; CASE 9B: brace-list-close brace
9778 ((if (consp special-brace-list)
9779 ;; Check special brace list closer.
9780 (progn
9781 (goto-char (car (car special-brace-list)))
9782 (save-excursion
9783 (goto-char indent-point)
9784 (back-to-indentation)
9785 (or
9786 ;; We were between the special close char and the `)'.
9787 (and (eq (char-after) ?\))
9788 (eq (1+ (point)) (cdr (car special-brace-list))))
9789 ;; We were before the special close char.
9790 (and (eq (char-after) (cdr (cdr special-brace-list)))
9791 (zerop (c-forward-token-2))
9792 (eq (1+ (point)) (cdr (car special-brace-list)))))))
9793 ;; Normal brace list check.
9794 (and (eq char-after-ip ?})
9795 (c-safe (goto-char (c-up-list-backward (point))) t)
9796 (= (point) containing-sexp)))
9797 (if (eq (point) (c-point 'boi))
9798 (c-add-syntax 'brace-list-close (point))
9799 (setq lim (c-most-enclosing-brace c-state-cache (point)))
9800 (c-beginning-of-statement-1 lim)
9801 (c-add-stmt-syntax 'brace-list-close nil t lim paren-state)))
9802
9803 (t
9804 ;; Prepare for the rest of the cases below by going to the
9805 ;; token following the opening brace
9806 (if (consp special-brace-list)
9807 (progn
9808 (goto-char (car (car special-brace-list)))
9809 (c-forward-token-2 1 nil indent-point))
9810 (goto-char containing-sexp))
9811 (forward-char)
9812 (let ((start (point)))
9813 (c-forward-syntactic-ws indent-point)
9814 (goto-char (max start (c-point 'bol))))
9815 (c-skip-ws-forward indent-point)
9816 (cond
9817
9818 ;; CASE 9C: we're looking at the first line in a brace-list
9819 ((= (point) indent-point)
9820 (if (consp special-brace-list)
9821 (goto-char (car (car special-brace-list)))
9822 (goto-char containing-sexp))
9823 (if (eq (point) (c-point 'boi))
9824 (c-add-syntax 'brace-list-intro (point))
9825 (setq lim (c-most-enclosing-brace c-state-cache (point)))
9826 (c-beginning-of-statement-1 lim)
9827 (c-add-stmt-syntax 'brace-list-intro nil t lim paren-state)))
9828
9829 ;; CASE 9D: this is just a later brace-list-entry or
9830 ;; brace-entry-open
9831 (t (if (or (eq char-after-ip ?{)
9832 (and c-special-brace-lists
9833 (save-excursion
9834 (goto-char indent-point)
9835 (c-forward-syntactic-ws (c-point 'eol))
9836 (c-looking-at-special-brace-list (point)))))
9837 (c-add-syntax 'brace-entry-open (point))
9838 (c-add-syntax 'brace-list-entry (point))
9839 ))
9840 ))))
9841
9842 ;; CASE 10: A continued statement or top level construct.
9843 ((and (not (memq char-before-ip '(?\; ?:)))
9844 (not (c-at-vsemi-p before-ws-ip))
9845 (or (not (eq char-before-ip ?}))
9846 (c-looking-at-inexpr-block-backward c-state-cache))
9847 (> (point)
9848 (save-excursion
9849 (c-beginning-of-statement-1 containing-sexp)
9850 (setq placeholder (point))))
9851 (/= placeholder containing-sexp))
9852 ;; This is shared with case 18.
9853 (c-guess-continued-construct indent-point
9854 char-after-ip
9855 placeholder
9856 containing-sexp
9857 paren-state))
9858
9859 ;; CASE 16: block close brace, possibly closing the defun or
9860 ;; the class
9861 ((eq char-after-ip ?})
9862 ;; From here on we have the next containing sexp in lim.
9863 (setq lim (c-most-enclosing-brace paren-state))
9864 (goto-char containing-sexp)
9865 (cond
9866
9867 ;; CASE 16E: Closing a statement block? This catches
9868 ;; cases where it's preceded by a statement keyword,
9869 ;; which works even when used in an "invalid" context,
9870 ;; e.g. a macro argument.
9871 ((c-after-conditional)
9872 (c-backward-to-block-anchor lim)
9873 (c-add-stmt-syntax 'block-close nil t lim paren-state))
9874
9875 ;; CASE 16A: closing a lambda defun or an in-expression
9876 ;; block? C.f. cases 4, 7B and 17E.
9877 ((setq placeholder (c-looking-at-inexpr-block
9878 (c-safe-position containing-sexp paren-state)
9879 nil))
9880 (setq tmpsymbol (if (eq (car placeholder) 'inlambda)
9881 'inline-close
9882 'block-close))
9883 (goto-char containing-sexp)
9884 (back-to-indentation)
9885 (if (= containing-sexp (point))
9886 (c-add-syntax tmpsymbol (point))
9887 (goto-char (cdr placeholder))
9888 (back-to-indentation)
9889 (c-add-stmt-syntax tmpsymbol nil t
9890 (c-most-enclosing-brace paren-state (point))
9891 paren-state)
9892 (if (/= (point) (cdr placeholder))
9893 (c-add-syntax (car placeholder)))))
9894
9895 ;; CASE 16B: does this close an inline or a function in
9896 ;; a non-class declaration level block?
9897 ((save-excursion
9898 (and lim
9899 (progn
9900 (goto-char lim)
9901 (c-looking-at-decl-block
9902 (c-most-enclosing-brace paren-state lim)
9903 nil))
9904 (setq placeholder (point))))
9905 (c-backward-to-decl-anchor lim)
9906 (back-to-indentation)
9907 (if (save-excursion
9908 (goto-char placeholder)
9909 (looking-at c-other-decl-block-key))
9910 (c-add-syntax 'defun-close (point))
9911 (c-add-syntax 'inline-close (point))))
9912
9913 ;; CASE 16F: Can be a defun-close of a function declared
9914 ;; in a statement block, e.g. in Pike or when using gcc
9915 ;; extensions, but watch out for macros followed by
9916 ;; blocks. Let it through to be handled below.
9917 ;; C.f. cases B.3 and 17G.
9918 ((save-excursion
9919 (and (not (c-at-statement-start-p))
9920 (eq (c-beginning-of-statement-1 lim nil nil t) 'same)
9921 (setq placeholder (point))
9922 (let ((c-recognize-typeless-decls nil))
9923 ;; Turn off recognition of constructs that
9924 ;; lacks a type in this case, since that's more
9925 ;; likely to be a macro followed by a block.
9926 (c-forward-decl-or-cast-1 (c-point 'bosws) nil nil))))
9927 (back-to-indentation)
9928 (if (/= (point) containing-sexp)
9929 (goto-char placeholder))
9930 (c-add-stmt-syntax 'defun-close nil t lim paren-state))
9931
9932 ;; CASE 16C: If there is an enclosing brace then this is
9933 ;; a block close since defun closes inside declaration
9934 ;; level blocks have been handled above.
9935 (lim
9936 ;; If the block is preceded by a case/switch label on
9937 ;; the same line, we anchor at the first preceding label
9938 ;; at boi. The default handling in c-add-stmt-syntax
9939 ;; really fixes it better, but we do like this to keep
9940 ;; the indentation compatible with version 5.28 and
9941 ;; earlier. C.f. case 17H.
9942 (while (and (/= (setq placeholder (point)) (c-point 'boi))
9943 (eq (c-beginning-of-statement-1 lim) 'label)))
9944 (goto-char placeholder)
9945 (if (looking-at c-label-kwds-regexp)
9946 (c-add-syntax 'block-close (point))
9947 (goto-char containing-sexp)
9948 ;; c-backward-to-block-anchor not necessary here; those
9949 ;; situations are handled in case 16E above.
9950 (c-add-stmt-syntax 'block-close nil t lim paren-state)))
9951
9952 ;; CASE 16D: Only top level defun close left.
9953 (t
9954 (goto-char containing-sexp)
9955 (c-backward-to-decl-anchor lim)
9956 (c-add-stmt-syntax 'defun-close nil nil
9957 (c-most-enclosing-brace paren-state)
9958 paren-state))
9959 ))
9960
9961 ;; CASE 17: Statement or defun catchall.
9962 (t
9963 (goto-char indent-point)
9964 ;; Back up statements until we find one that starts at boi.
9965 (while (let* ((prev-point (point))
9966 (last-step-type (c-beginning-of-statement-1
9967 containing-sexp)))
9968 (if (= (point) prev-point)
9969 (progn
9970 (setq step-type (or step-type last-step-type))
9971 nil)
9972 (setq step-type last-step-type)
9973 (/= (point) (c-point 'boi)))))
9974 (cond
9975
9976 ;; CASE 17B: continued statement
9977 ((and (eq step-type 'same)
9978 (/= (point) indent-point))
9979 (c-add-stmt-syntax 'statement-cont nil nil
9980 containing-sexp paren-state))
9981
9982 ;; CASE 17A: After a case/default label?
9983 ((progn
9984 (while (and (eq step-type 'label)
9985 (not (looking-at c-label-kwds-regexp)))
9986 (setq step-type
9987 (c-beginning-of-statement-1 containing-sexp)))
9988 (eq step-type 'label))
9989 (c-add-stmt-syntax (if (eq char-after-ip ?{)
9990 'statement-case-open
9991 'statement-case-intro)
9992 nil t containing-sexp paren-state))
9993
9994 ;; CASE 17D: any old statement
9995 ((progn
9996 (while (eq step-type 'label)
9997 (setq step-type
9998 (c-beginning-of-statement-1 containing-sexp)))
9999 (eq step-type 'previous))
10000 (c-add-stmt-syntax 'statement nil t
10001 containing-sexp paren-state)
10002 (if (eq char-after-ip ?{)
10003 (c-add-syntax 'block-open)))
10004
10005 ;; CASE 17I: Inside a substatement block.
10006 ((progn
10007 ;; The following tests are all based on containing-sexp.
10008 (goto-char containing-sexp)
10009 ;; From here on we have the next containing sexp in lim.
10010 (setq lim (c-most-enclosing-brace paren-state containing-sexp))
10011 (c-after-conditional))
10012 (c-backward-to-block-anchor lim)
10013 (c-add-stmt-syntax 'statement-block-intro nil t
10014 lim paren-state)
10015 (if (eq char-after-ip ?{)
10016 (c-add-syntax 'block-open)))
10017
10018 ;; CASE 17E: first statement in an in-expression block.
10019 ;; C.f. cases 4, 7B and 16A.
10020 ((setq placeholder (c-looking-at-inexpr-block
10021 (c-safe-position containing-sexp paren-state)
10022 nil))
10023 (setq tmpsymbol (if (eq (car placeholder) 'inlambda)
10024 'defun-block-intro
10025 'statement-block-intro))
10026 (back-to-indentation)
10027 (if (= containing-sexp (point))
10028 (c-add-syntax tmpsymbol (point))
10029 (goto-char (cdr placeholder))
10030 (back-to-indentation)
10031 (c-add-stmt-syntax tmpsymbol nil t
10032 (c-most-enclosing-brace c-state-cache (point))
10033 paren-state)
10034 (if (/= (point) (cdr placeholder))
10035 (c-add-syntax (car placeholder))))
10036 (if (eq char-after-ip ?{)
10037 (c-add-syntax 'block-open)))
10038
10039 ;; CASE 17F: first statement in an inline, or first
10040 ;; statement in a top-level defun. we can tell this is it
10041 ;; if there are no enclosing braces that haven't been
10042 ;; narrowed out by a class (i.e. don't use bod here).
10043 ((save-excursion
10044 (or (not (setq placeholder (c-most-enclosing-brace
10045 paren-state)))
10046 (and (progn
10047 (goto-char placeholder)
10048 (eq (char-after) ?{))
10049 (c-looking-at-decl-block (c-most-enclosing-brace
10050 paren-state (point))
10051 nil))))
10052 (c-backward-to-decl-anchor lim)
10053 (back-to-indentation)
10054 (c-add-syntax 'defun-block-intro (point)))
10055
10056 ;; CASE 17G: First statement in a function declared inside
10057 ;; a normal block. This can occur in Pike and with
10058 ;; e.g. the gcc extensions, but watch out for macros
10059 ;; followed by blocks. C.f. cases B.3 and 16F.
10060 ((save-excursion
10061 (and (not (c-at-statement-start-p))
10062 (eq (c-beginning-of-statement-1 lim nil nil t) 'same)
10063 (setq placeholder (point))
10064 (let ((c-recognize-typeless-decls nil))
10065 ;; Turn off recognition of constructs that lacks
10066 ;; a type in this case, since that's more likely
10067 ;; to be a macro followed by a block.
10068 (c-forward-decl-or-cast-1 (c-point 'bosws) nil nil))))
10069 (back-to-indentation)
10070 (if (/= (point) containing-sexp)
10071 (goto-char placeholder))
10072 (c-add-stmt-syntax 'defun-block-intro nil t
10073 lim paren-state))
10074
10075 ;; CASE 17H: First statement in a block.
10076 (t
10077 ;; If the block is preceded by a case/switch label on the
10078 ;; same line, we anchor at the first preceding label at
10079 ;; boi. The default handling in c-add-stmt-syntax is
10080 ;; really fixes it better, but we do like this to keep the
10081 ;; indentation compatible with version 5.28 and earlier.
10082 ;; C.f. case 16C.
10083 (while (and (/= (setq placeholder (point)) (c-point 'boi))
10084 (eq (c-beginning-of-statement-1 lim) 'label)))
10085 (goto-char placeholder)
10086 (if (looking-at c-label-kwds-regexp)
10087 (c-add-syntax 'statement-block-intro (point))
10088 (goto-char containing-sexp)
10089 ;; c-backward-to-block-anchor not necessary here; those
10090 ;; situations are handled in case 17I above.
10091 (c-add-stmt-syntax 'statement-block-intro nil t
10092 lim paren-state))
10093 (if (eq char-after-ip ?{)
10094 (c-add-syntax 'block-open)))
10095 ))
10096 )
10097
10098 ;; now we need to look at any modifiers
10099 (goto-char indent-point)
10100 (skip-chars-forward " \t")
10101
10102 ;; are we looking at a comment only line?
10103 (when (and (looking-at c-comment-start-regexp)
10104 (/= (c-forward-token-2 0 nil (c-point 'eol)) 0))
10105 (c-append-syntax 'comment-intro))
10106
10107 ;; we might want to give additional offset to friends (in C++).
10108 (when (and c-opt-friend-key
10109 (looking-at c-opt-friend-key))
10110 (c-append-syntax 'friend))
10111
10112 ;; Set syntactic-relpos.
10113 (let ((p c-syntactic-context))
10114 (while (and p
10115 (if (integerp (c-langelem-pos (car p)))
10116 (progn
10117 (setq syntactic-relpos (c-langelem-pos (car p)))
10118 nil)
10119 t))
10120 (setq p (cdr p))))
10121
10122 ;; Start of or a continuation of a preprocessor directive?
10123 (if (and macro-start
10124 (eq macro-start (c-point 'boi))
10125 (not (and (c-major-mode-is 'pike-mode)
10126 (eq (char-after (1+ macro-start)) ?\"))))
10127 (c-append-syntax 'cpp-macro)
10128 (when (and c-syntactic-indentation-in-macros macro-start)
10129 (if in-macro-expr
10130 (when (or
10131 (< syntactic-relpos macro-start)
10132 (not (or
10133 (assq 'arglist-intro c-syntactic-context)
10134 (assq 'arglist-cont c-syntactic-context)
10135 (assq 'arglist-cont-nonempty c-syntactic-context)
10136 (assq 'arglist-close c-syntactic-context))))
10137 ;; If inside a cpp expression, i.e. anywhere in a
10138 ;; cpp directive except a #define body, we only let
10139 ;; through the syntactic analysis that is internal
10140 ;; in the expression. That means the arglist
10141 ;; elements, if they are anchored inside the cpp
10142 ;; expression.
10143 (setq c-syntactic-context nil)
10144 (c-add-syntax 'cpp-macro-cont macro-start))
10145 (when (and (eq macro-start syntactic-relpos)
10146 (not (assq 'cpp-define-intro c-syntactic-context))
10147 (save-excursion
10148 (goto-char macro-start)
10149 (or (not (c-forward-to-cpp-define-body))
10150 (<= (point) (c-point 'boi indent-point)))))
10151 ;; Inside a #define body and the syntactic analysis is
10152 ;; anchored on the start of the #define. In this case
10153 ;; we add cpp-define-intro to get the extra
10154 ;; indentation of the #define body.
10155 (c-add-syntax 'cpp-define-intro)))))
10156
10157 ;; return the syntax
10158 c-syntactic-context)))
10159
10160 \f
10161 ;; Indentation calculation.
10162
10163 (defun c-evaluate-offset (offset langelem symbol)
10164 ;; offset can be a number, a function, a variable, a list, or one of
10165 ;; the symbols + or -
10166 ;;
10167 ;; This function might do hidden buffer changes.
10168 (let ((res
10169 (cond
10170 ((numberp offset) offset)
10171 ((vectorp offset) offset)
10172 ((null offset) nil)
10173
10174 ((eq offset '+) c-basic-offset)
10175 ((eq offset '-) (- c-basic-offset))
10176 ((eq offset '++) (* 2 c-basic-offset))
10177 ((eq offset '--) (* 2 (- c-basic-offset)))
10178 ((eq offset '*) (/ c-basic-offset 2))
10179 ((eq offset '/) (/ (- c-basic-offset) 2))
10180
10181 ((functionp offset)
10182 (c-evaluate-offset
10183 (funcall offset
10184 (cons (c-langelem-sym langelem)
10185 (c-langelem-pos langelem)))
10186 langelem symbol))
10187
10188 ((listp offset)
10189 (cond
10190 ((eq (car offset) 'quote)
10191 (c-benign-error "The offset %S for %s was mistakenly quoted"
10192 offset symbol)
10193 nil)
10194
10195 ((memq (car offset) '(min max))
10196 (let (res val (method (car offset)))
10197 (setq offset (cdr offset))
10198 (while offset
10199 (setq val (c-evaluate-offset (car offset) langelem symbol))
10200 (cond
10201 ((not val))
10202 ((not res)
10203 (setq res val))
10204 ((integerp val)
10205 (if (vectorp res)
10206 (c-benign-error "\
10207 Error evaluating offset %S for %s: \
10208 Cannot combine absolute offset %S with relative %S in `%s' method"
10209 (car offset) symbol res val method)
10210 (setq res (funcall method res val))))
10211 (t
10212 (if (integerp res)
10213 (c-benign-error "\
10214 Error evaluating offset %S for %s: \
10215 Cannot combine relative offset %S with absolute %S in `%s' method"
10216 (car offset) symbol res val method)
10217 (setq res (vector (funcall method (aref res 0)
10218 (aref val 0)))))))
10219 (setq offset (cdr offset)))
10220 res))
10221
10222 ((eq (car offset) 'add)
10223 (let (res val)
10224 (setq offset (cdr offset))
10225 (while offset
10226 (setq val (c-evaluate-offset (car offset) langelem symbol))
10227 (cond
10228 ((not val))
10229 ((not res)
10230 (setq res val))
10231 ((integerp val)
10232 (if (vectorp res)
10233 (setq res (vector (+ (aref res 0) val)))
10234 (setq res (+ res val))))
10235 (t
10236 (if (vectorp res)
10237 (c-benign-error "\
10238 Error evaluating offset %S for %s: \
10239 Cannot combine absolute offsets %S and %S in `add' method"
10240 (car offset) symbol res val)
10241 (setq res val)))) ; Override.
10242 (setq offset (cdr offset)))
10243 res))
10244
10245 (t
10246 (let (res)
10247 (when (eq (car offset) 'first)
10248 (setq offset (cdr offset)))
10249 (while (and (not res) offset)
10250 (setq res (c-evaluate-offset (car offset) langelem symbol)
10251 offset (cdr offset)))
10252 res))))
10253
10254 ((and (symbolp offset) (boundp offset))
10255 (symbol-value offset))
10256
10257 (t
10258 (c-benign-error "Unknown offset format %S for %s" offset symbol)
10259 nil))))
10260
10261 (if (or (null res) (integerp res)
10262 (and (vectorp res) (= (length res) 1) (integerp (aref res 0))))
10263 res
10264 (c-benign-error "Error evaluating offset %S for %s: Got invalid value %S"
10265 offset symbol res)
10266 nil)))
10267
10268 (defun c-calc-offset (langelem)
10269 ;; Get offset from LANGELEM which is a list beginning with the
10270 ;; syntactic symbol and followed by any analysis data it provides.
10271 ;; That data may be zero or more elements, but if at least one is
10272 ;; given then the first is the anchor position (or nil). The symbol
10273 ;; is matched against `c-offsets-alist' and the offset calculated
10274 ;; from that is returned.
10275 ;;
10276 ;; This function might do hidden buffer changes.
10277 (let* ((symbol (c-langelem-sym langelem))
10278 (match (assq symbol c-offsets-alist))
10279 (offset (cdr-safe match)))
10280 (if match
10281 (setq offset (c-evaluate-offset offset langelem symbol))
10282 (if c-strict-syntax-p
10283 (c-benign-error "No offset found for syntactic symbol %s" symbol))
10284 (setq offset 0))
10285 (if (vectorp offset)
10286 offset
10287 (or (and (numberp offset) offset)
10288 (and (symbolp offset) (symbol-value offset))
10289 0))
10290 ))
10291
10292 (defun c-get-offset (langelem)
10293 ;; This is a compatibility wrapper for `c-calc-offset' in case
10294 ;; someone is calling it directly. It takes an old style syntactic
10295 ;; element on the form (SYMBOL . ANCHOR-POS) and converts it to the
10296 ;; new list form.
10297 ;;
10298 ;; This function might do hidden buffer changes.
10299 (if (c-langelem-pos langelem)
10300 (c-calc-offset (list (c-langelem-sym langelem)
10301 (c-langelem-pos langelem)))
10302 (c-calc-offset langelem)))
10303
10304 (defun c-get-syntactic-indentation (langelems)
10305 ;; Calculate the syntactic indentation from a syntactic description
10306 ;; as returned by `c-guess-syntax'.
10307 ;;
10308 ;; Note that topmost-intro always has an anchor position at bol, for
10309 ;; historical reasons. It's often used together with other symbols
10310 ;; that has more sane positions. Since we always use the first
10311 ;; found anchor position, we rely on that these other symbols always
10312 ;; precede topmost-intro in the LANGELEMS list.
10313 ;;
10314 ;; This function might do hidden buffer changes.
10315 (let ((indent 0) anchor)
10316
10317 (while langelems
10318 (let* ((c-syntactic-element (car langelems))
10319 (res (c-calc-offset c-syntactic-element)))
10320
10321 (if (vectorp res)
10322 ;; Got an absolute column that overrides any indentation
10323 ;; we've collected so far, but not the relative
10324 ;; indentation we might get for the nested structures
10325 ;; further down the langelems list.
10326 (setq indent (elt res 0)
10327 anchor (point-min)) ; A position at column 0.
10328
10329 ;; Got a relative change of the current calculated
10330 ;; indentation.
10331 (setq indent (+ indent res))
10332
10333 ;; Use the anchor position from the first syntactic
10334 ;; element with one.
10335 (unless anchor
10336 (setq anchor (c-langelem-pos (car langelems)))))
10337
10338 (setq langelems (cdr langelems))))
10339
10340 (if anchor
10341 (+ indent (save-excursion
10342 (goto-char anchor)
10343 (current-column)))
10344 indent)))
10345
10346 \f
10347 (cc-provide 'cc-engine)
10348
10349 ;;; cc-engine.el ends here