(latexenc-find-file-coding-system): Don't inherit the EOL part of the
[emacs.git] / lisp / progmodes / cc-engine.el
blobe9ed474e792807657c6ef06adaa87fabc9028c8a
1 ;;; cc-engine.el --- core syntax guessing engine for CC mode
3 ;; Copyright (C) 1985,1987,1992-2003 Free Software Foundation, Inc.
5 ;; Authors: 1998- Martin Stjernholm
6 ;; 1992-1999 Barry A. Warsaw
7 ;; 1987 Dave Detlefs and Stewart Clamen
8 ;; 1985 Richard M. Stallman
9 ;; Maintainer: bug-cc-mode@gnu.org
10 ;; Created: 22-Apr-1997 (split from cc-mode.el)
11 ;; Version: See cc-mode.el
12 ;; Keywords: c languages oop
14 ;; This file is part of GNU Emacs.
16 ;; GNU Emacs is free software; you can redistribute it and/or modify
17 ;; it under the terms of the GNU General Public License as published by
18 ;; the Free Software Foundation; either version 2, or (at your option)
19 ;; any later version.
21 ;; GNU Emacs is distributed in the hope that it will be useful,
22 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
23 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 ;; GNU General Public License for more details.
26 ;; You should have received a copy of the GNU General Public License
27 ;; along with this program; see the file COPYING. If not, write to
28 ;; the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
29 ;; Boston, MA 02110-1301, USA.
31 ;;; Commentary:
33 ;; The functions which have docstring documentation can be considered
34 ;; part of an API which other packages can use in CC Mode buffers.
35 ;; Otoh, undocumented functions and functions with the documentation
36 ;; in comments are considered purely internal and can change semantics
37 ;; or even disappear in the future.
39 ;; (This policy applies to CC Mode as a whole, not just this file. It
40 ;; probably also applies to many other Emacs packages, but here it's
41 ;; clearly spelled out.)
43 ;; Hidden buffer changes
45 ;; Various functions in CC Mode use text properties for caching and
46 ;; syntactic markup purposes, and those of them that might modify such
47 ;; properties are said to do "hidden buffer changes". They should be
48 ;; used within `c-save-buffer-state' or a similar function that saves
49 ;; and restores buffer modifiedness etc.
51 ;; Interactive functions are assumed to not do hidden buffer changes
52 ;; (this isn't applicable in the specific parts of them that do real
53 ;; changes, though).
55 ;; All other functions are assumed to do hidden buffer changes and
56 ;; must thus be wrapped inside `c-save-buffer-state' if they're used
57 ;; from any function that does not do hidden buffer changes.
59 ;; Every function, except the interactive ones, that doesn't do hidden
60 ;; buffer changes have that explicitly stated in their docstring or
61 ;; comment.
63 ;; Use of text properties
65 ;; CC Mode uses several text properties internally to mark up various
66 ;; positions, e.g. to improve speed and to eliminate glitches in
67 ;; interactive refontification.
69 ;; Note: This doc is for internal use only. Other packages should not
70 ;; assume that these text properties are used as described here.
72 ;; 'syntax-table
73 ;; Used to modify the syntax of some characters. Currently used to
74 ;; mark the "<" and ">" of angle bracket parens with paren syntax.
76 ;; This property is used on single characters and is therefore
77 ;; always treated as front and rear nonsticky (or start and end open
78 ;; in XEmacs vocabulary). It's therefore installed on
79 ;; `text-property-default-nonsticky' if that variable exists (Emacs
80 ;; >= 21).
82 ;; 'c-is-sws and 'c-in-sws
83 ;; Used by `c-forward-syntactic-ws' and `c-backward-syntactic-ws' to
84 ;; speed them up. See the comment blurb before `c-put-is-sws'
85 ;; below for further details.
87 ;; 'c-type
88 ;; This property is used on single characters to mark positions with
89 ;; special syntactic relevance of various sorts. It's primary use
90 ;; is to avoid glitches when multiline constructs are refontified
91 ;; interactively (on font lock decoration level 3). It's cleared in
92 ;; a region before it's fontified and is then put on relevant chars
93 ;; in that region as they are encountered during the fontification.
94 ;; The value specifies the kind of position:
96 ;; 'c-decl-arg-start
97 ;; Put on the last char of the token preceding each declaration
98 ;; inside a declaration style arglist (typically in a function
99 ;; prototype).
101 ;; 'c-decl-end
102 ;; Put on the last char of the token preceding a declaration.
103 ;; This is used in cases where declaration boundaries can't be
104 ;; recognized simply by looking for a token like ";" or "}".
105 ;; `c-type-decl-end-used' must be set if this is used (see also
106 ;; `c-find-decl-spots').
108 ;; 'c-<>-arg-sep
109 ;; Put on the commas that separate arguments in angle bracket
110 ;; arglists like C++ template arglists.
112 ;; 'c-decl-id-start and 'c-decl-type-start
113 ;; Put on the last char of the token preceding each declarator
114 ;; in the declarator list of a declaration. They are also used
115 ;; between the identifiers cases like enum declarations.
116 ;; 'c-decl-type-start is used when the declarators are types,
117 ;; 'c-decl-id-start otherwise.
119 ;; 'c-awk-NL-prop
120 ;; Used in AWK mode to mark the various kinds of newlines. See
121 ;; cc-awk.el.
123 ;;; Code:
125 (eval-when-compile
126 (let ((load-path
127 (if (and (boundp 'byte-compile-dest-file)
128 (stringp byte-compile-dest-file))
129 (cons (file-name-directory byte-compile-dest-file) load-path)
130 load-path)))
131 (load "cc-bytecomp" nil t)))
133 (cc-require 'cc-defs)
134 (cc-require-when-compile 'cc-langs)
135 (cc-require 'cc-vars)
137 ;; Some functions/constants in cc-awk.el that are called/referenced here.
138 ;; (Can't use cc-require due to cyclicity.)
139 (cc-bytecomp-defun c-awk-unstick-NL-prop)
140 (cc-bytecomp-defun c-awk-clear-NL-props)
141 (cc-bytecomp-defvar awk-mode-syntax-table)
142 (cc-bytecomp-defun c-awk-backward-syntactic-ws)
143 (cc-bytecomp-defun c-awk-after-logical-semicolon)
144 (cc-bytecomp-defun c-awk-NL-prop-not-set)
145 (cc-bytecomp-defun c-awk-completed-stmt-ws-ends-line-p)
146 (cc-bytecomp-defun c-awk-completed-stmt-ws-ends-prev-line-p)
147 (cc-bytecomp-defun c-awk-prev-line-incomplete-p)
148 (cc-bytecomp-defun c-awk-after-change)
150 ;; Silence the compiler.
151 (cc-bytecomp-defun buffer-syntactic-context) ; XEmacs
154 ;; Make declarations for all the `c-lang-defvar' variables in cc-langs.
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)
168 ;;; Internal state variables.
170 ;; Internal state of hungry delete key feature
171 (defvar c-hungry-delete-key nil)
172 (make-variable-buffer-local 'c-hungry-delete-key)
174 ;; Internal state of auto newline feature.
175 (defvar c-auto-newline nil)
176 (make-variable-buffer-local 'c-auto-newline)
178 ;; Internal auto-newline/hungry-delete designation string for mode line.
179 (defvar c-auto-hungry-string nil)
180 (make-variable-buffer-local 'c-auto-hungry-string)
182 (defun c-calculate-state (arg prevstate)
183 ;; Calculate the new state of PREVSTATE, t or nil, based on arg. If
184 ;; arg is nil or zero, toggle the state. If arg is negative, turn
185 ;; the state off, and if arg is positive, turn the state on
186 (if (or (not arg)
187 (zerop (setq arg (prefix-numeric-value arg))))
188 (not prevstate)
189 (> arg 0)))
191 ;; Dynamically bound cache for `c-in-literal'.
192 (defvar c-in-literal-cache t)
194 ;; Must be set in buffers where the `c-type' text property might be used
195 ;; with the value `c-decl-end'.
196 (defvar c-type-decl-end-used nil)
197 (make-variable-buffer-local 'c-type-decl-end-used)
200 ;; Basic handling of preprocessor directives.
202 ;; This is a dynamically bound cache used together with
203 ;; `c-query-macro-start' and `c-query-and-set-macro-start'. It only
204 ;; works as long as point doesn't cross a macro boundary.
205 (defvar c-macro-start 'unknown)
207 (defsubst c-query-and-set-macro-start ()
208 ;; This function does not do any hidden buffer changes.
209 (if (symbolp c-macro-start)
210 (setq c-macro-start (save-excursion
211 (and (c-beginning-of-macro)
212 (point))))
213 c-macro-start))
215 (defsubst c-query-macro-start ()
216 ;; This function does not do any hidden buffer changes.
217 (if (symbolp c-macro-start)
218 (save-excursion
219 (and (c-beginning-of-macro)
220 (point)))
221 c-macro-start))
223 (defun c-beginning-of-macro (&optional lim)
224 "Go to the beginning of a preprocessor directive.
225 Leave point at the beginning of the directive and return t if in one,
226 otherwise return nil and leave point unchanged.
228 This function does not do any hidden buffer changes."
229 (when c-opt-cpp-prefix
230 (let ((here (point)))
231 (save-restriction
232 (if lim (narrow-to-region lim (point-max)))
233 (beginning-of-line)
234 (while (eq (char-before (1- (point))) ?\\)
235 (forward-line -1))
236 (back-to-indentation)
237 (if (and (<= (point) here)
238 (looking-at c-opt-cpp-start))
240 (goto-char here)
241 nil)))))
243 (defun c-end-of-macro ()
244 "Go to the end of a preprocessor directive.
245 More accurately, move point to the end of the closest following line
246 that doesn't end with a line continuation backslash.
248 This function does not do any hidden buffer changes."
249 (while (progn
250 (end-of-line)
251 (when (and (eq (char-before) ?\\)
252 (not (eobp)))
253 (forward-char)
254 t))))
256 (defun c-forward-to-cpp-define-body ()
257 ;; Assuming point is at the "#" that introduces a preprocessor
258 ;; directive, it's moved forward to the start of the definition body
259 ;; if it's a "#define". Non-nil is returned in this case, in all
260 ;; other cases nil is returned and point isn't moved.
261 (when (and (looking-at
262 (concat "#[ \t]*"
263 "define[ \t]+\\(\\sw\\|_\\)+\\(\([^\)]*\)\\)?"
264 "\\([ \t]\\|\\\\\n\\)*"))
265 (not (= (match-end 0) (c-point 'eol))))
266 (goto-char (match-end 0))))
269 ;;; Basic utility functions.
271 (defun c-syntactic-content (from to)
272 ;; Return the given region as a string where all syntactic
273 ;; whitespace is removed or, where necessary, replaced with a single
274 ;; space.
275 (save-excursion
276 (goto-char from)
277 (let* ((parts (list nil)) (tail parts) pos)
278 (while (re-search-forward c-syntactic-ws-start to t)
279 (goto-char (setq pos (match-beginning 0)))
280 (c-forward-syntactic-ws to)
281 (if (= (point) pos)
282 (forward-char)
283 (if (and (> pos from)
284 (< (point) to)
285 (looking-at "\\w\\|\\s_")
286 (save-excursion
287 (goto-char (1- pos))
288 (looking-at "\\w\\|\\s_")))
289 (progn
290 (setcdr tail (list (buffer-substring-no-properties from pos)
291 " "))
292 (setq tail (cddr tail)))
293 (setcdr tail (list (buffer-substring-no-properties from pos)))
294 (setq tail (cdr tail)))
295 (setq from (point))))
296 (setcdr tail (list (buffer-substring-no-properties from to)))
297 (apply 'concat (cdr parts)))))
299 (defsubst c-keyword-sym (keyword)
300 ;; Return non-nil if the string KEYWORD is a known keyword. More
301 ;; precisely, the value is the symbol for the keyword in
302 ;; `c-keywords-obarray'.
303 (intern-soft keyword c-keywords-obarray))
305 (defsubst c-keyword-member (keyword-sym lang-constant)
306 ;; Return non-nil if the symbol KEYWORD-SYM, as returned by
307 ;; `c-keyword-sym', is a member of LANG-CONSTANT, which is the name
308 ;; of a language constant that ends with "-kwds". If KEYWORD-SYM is
309 ;; nil then the result is nil.
310 (get keyword-sym lang-constant))
312 ;; String syntax chars, suitable for skip-syntax-(forward|backward).
313 (defconst c-string-syntax (if (memq 'gen-string-delim c-emacs-features)
314 "\"|"
315 "\""))
317 ;; Regexp matching string start syntax.
318 (defconst c-string-limit-regexp (if (memq 'gen-string-delim c-emacs-features)
319 "\\s\"\\|\\s|"
320 "\\s\""))
322 ;; Holds formatted error strings for the few cases where parse errors
323 ;; are reported.
324 (defvar c-parsing-error nil)
325 (make-variable-buffer-local 'c-parsing-error)
327 (defun c-echo-parsing-error (&optional quiet)
328 ;; This function does not do any hidden buffer changes.
329 (when (and c-report-syntactic-errors c-parsing-error (not quiet))
330 (c-benign-error "%s" c-parsing-error))
331 c-parsing-error)
333 ;; Faces given to comments and string literals. This is used in some
334 ;; situations to speed up recognition; it isn't mandatory that font
335 ;; locking is in use. This variable is extended with the face in
336 ;; `c-doc-face-name' when fontification is activated in cc-fonts.el.
337 (defvar c-literal-faces
338 '(font-lock-comment-face font-lock-string-face
339 font-lock-comment-delimiter-face))
341 (defun c-shift-line-indentation (shift-amt)
342 ;; This function does not do any hidden buffer changes.
343 (let ((pos (- (point-max) (point)))
344 (c-macro-start c-macro-start)
345 tmp-char-inserted)
346 (if (zerop shift-amt)
348 (when (and (c-query-and-set-macro-start)
349 (looking-at "[ \t]*\\\\$")
350 (save-excursion
351 (skip-chars-backward " \t")
352 (bolp)))
353 (insert ?x)
354 (backward-char)
355 (setq tmp-char-inserted t))
356 (unwind-protect
357 (let ((col (current-indentation)))
358 (delete-region (c-point 'bol) (c-point 'boi))
359 (beginning-of-line)
360 (indent-to (+ col shift-amt)))
361 (when tmp-char-inserted
362 (delete-char 1))))
363 ;; If initial point was within line's indentation and we're not on
364 ;; a line with a line continuation in a macro, position after the
365 ;; indentation. Else stay at same point in text.
366 (if (and (< (point) (c-point 'boi))
367 (not tmp-char-inserted))
368 (back-to-indentation)
369 (if (> (- (point-max) pos) (point))
370 (goto-char (- (point-max) pos))))))
373 ;; Some debug tools to visualize various special positions. This
374 ;; debug code isn't as portable as the rest of CC Mode.
376 (cc-bytecomp-defun overlays-in)
377 (cc-bytecomp-defun overlay-get)
378 (cc-bytecomp-defun overlay-start)
379 (cc-bytecomp-defun overlay-end)
380 (cc-bytecomp-defun delete-overlay)
381 (cc-bytecomp-defun overlay-put)
382 (cc-bytecomp-defun make-overlay)
384 (defun c-debug-add-face (beg end face)
385 (c-save-buffer-state ((overlays (overlays-in beg end)) overlay)
386 (while overlays
387 (setq overlay (car overlays)
388 overlays (cdr overlays))
389 (when (eq (overlay-get overlay 'face) face)
390 (setq beg (min beg (overlay-start overlay))
391 end (max end (overlay-end overlay)))
392 (delete-overlay overlay)))
393 (overlay-put (make-overlay beg end) 'face face)))
395 (defun c-debug-remove-face (beg end face)
396 (c-save-buffer-state ((overlays (overlays-in beg end)) overlay
397 (ol-beg beg) (ol-end end))
398 (while overlays
399 (setq overlay (car overlays)
400 overlays (cdr overlays))
401 (when (eq (overlay-get overlay 'face) face)
402 (setq ol-beg (min ol-beg (overlay-start overlay))
403 ol-end (max ol-end (overlay-end overlay)))
404 (delete-overlay overlay)))
405 (when (< ol-beg beg)
406 (overlay-put (make-overlay ol-beg beg) 'face face))
407 (when (> ol-end end)
408 (overlay-put (make-overlay end ol-end) 'face face))))
411 ;; `c-beginning-of-statement-1' and accompanying stuff.
413 ;; KLUDGE ALERT: c-maybe-labelp is used to pass information between
414 ;; c-crosses-statement-barrier-p and c-beginning-of-statement-1. A
415 ;; better way should be implemented, but this will at least shut up
416 ;; the byte compiler.
417 (defvar c-maybe-labelp nil)
419 ;; New awk-compatible version of c-beginning-of-statement-1, ACM 2002/6/22
421 ;; Macros used internally in c-beginning-of-statement-1 for the
422 ;; automaton actions.
423 (defmacro c-bos-push-state ()
424 '(setq stack (cons (cons state saved-pos)
425 stack)))
426 (defmacro c-bos-pop-state (&optional do-if-done)
427 `(if (setq state (car (car stack))
428 saved-pos (cdr (car stack))
429 stack (cdr stack))
431 ,do-if-done
432 (throw 'loop nil)))
433 (defmacro c-bos-pop-state-and-retry ()
434 '(throw 'loop (setq state (car (car stack))
435 saved-pos (cdr (car stack))
436 ;; Throw nil if stack is empty, else throw non-nil.
437 stack (cdr stack))))
438 (defmacro c-bos-save-pos ()
439 '(setq saved-pos (vector pos tok ptok pptok)))
440 (defmacro c-bos-restore-pos ()
441 '(unless (eq (elt saved-pos 0) start)
442 (setq pos (elt saved-pos 0)
443 tok (elt saved-pos 1)
444 ptok (elt saved-pos 2)
445 pptok (elt saved-pos 3))
446 (goto-char pos)
447 (setq sym nil)))
448 (defmacro c-bos-save-error-info (missing got)
449 `(setq saved-pos (vector pos ,missing ,got)))
450 (defmacro c-bos-report-error ()
451 '(unless noerror
452 (setq c-parsing-error
453 (format "No matching `%s' found for `%s' on line %d"
454 (elt saved-pos 1)
455 (elt saved-pos 2)
456 (1+ (count-lines (point-min)
457 (c-point 'bol (elt saved-pos 0))))))))
459 (defun c-beginning-of-statement-1 (&optional lim ignore-labels
460 noerror comma-delim)
461 "Move to the start of the current statement or declaration, or to
462 the previous one if already at the beginning of one. Only
463 statements/declarations on the same level are considered, i.e. don't
464 move into or out of sexps (not even normal expression parentheses).
466 Stop at statement continuation tokens like \"else\", \"catch\",
467 \"finally\" and the \"while\" in \"do ... while\" if the start point
468 is within the continuation. If starting at such a token, move to the
469 corresponding statement start. If at the beginning of a statement,
470 move to the closest containing statement if there is any. This might
471 also stop at a continuation clause.
473 Labels are treated as separate statements if IGNORE-LABELS is non-nil.
474 The function is not overly intelligent in telling labels from other
475 uses of colons; if used outside a statement context it might trip up
476 on e.g. inherit colons, so IGNORE-LABELS should be used then. There
477 should be no such mistakes in a statement context, however.
479 Macros are ignored unless point is within one, in which case the
480 content of the macro is treated as normal code. Aside from any normal
481 statement starts found in it, stop at the first token of the content
482 in the macro, i.e. the expression of an \"#if\" or the start of the
483 definition in a \"#define\". Also stop at start of macros before
484 leaving them.
486 Return 'label if stopped at a label, 'same if stopped at the beginning
487 of the current statement, 'up if stepped to a containing statement,
488 'previous if stepped to a preceding statement, 'beginning if stepped
489 from a statement continuation clause to its start clause, or 'macro if
490 stepped to a macro start. Note that 'same and not 'label is returned
491 if stopped at the same label without crossing the colon character.
493 LIM may be given to limit the search. If the search hits the limit,
494 point will be left at the closest following token, or at the start
495 position if that is less ('same is returned in this case).
497 NOERROR turns off error logging to `c-parsing-error'.
499 Normally only ';' is considered to delimit statements, but if
500 COMMA-DELIM is non-nil then ',' is treated likewise."
502 ;; The bulk of this function is a pushdown automaton that looks at statement
503 ;; boundaries and the tokens (such as "while") in c-opt-block-stmt-key. Its
504 ;; purpose is to keep track of nested statements, ensuring that such
505 ;; statments are skipped over in their entirety (somewhat akin to what C-M-p
506 ;; does with nested braces/brackets/parentheses).
508 ;; Note: The position of a boundary is the following token.
510 ;; Beginning with the current token (the one following point), move back one
511 ;; sexp at a time (where a sexp is, more or less, either a token or the
512 ;; entire contents of a brace/bracket/paren pair). Each time a statement
513 ;; boundary is crossed or a "while"-like token is found, update the state of
514 ;; the PDA. Stop at the beginning of a statement when the stack (holding
515 ;; nested statement info) is empty and the position has been moved.
517 ;; The following variables constitute the PDA:
519 ;; sym: This is either the "while"-like token (e.g. 'for) we've just
520 ;; scanned back over, 'boundary if we've just gone back over a
521 ;; statement boundary, or nil otherwise.
522 ;; state: takes one of the values (nil else else-boundary while
523 ;; while-boundary catch catch-boundary).
524 ;; nil means "no "while"-like token yet scanned".
525 ;; 'else, for example, means "just gone back over an else".
526 ;; 'else-boundary means "just gone back over a statement boundary
527 ;; immediately after having gone back over an else".
528 ;; saved-pos: A vector of either saved positions (tok ptok pptok, etc.) or
529 ;; of error reporting information.
530 ;; stack: The stack onto which the PDA pushes its state. Each entry
531 ;; consists of a saved value of state and saved-pos. An entry is
532 ;; pushed when we move back over a "continuation" token (e.g. else)
533 ;; and popped when we encounter the corresponding opening token
534 ;; (e.g. if).
537 ;; The following diagram briefly outlines the PDA.
539 ;; Common state:
540 ;; "else": Push state, goto state `else'.
541 ;; "while": Push state, goto state `while'.
542 ;; "catch" or "finally": Push state, goto state `catch'.
543 ;; boundary: Pop state.
544 ;; other: Do nothing special.
546 ;; State `else':
547 ;; boundary: Goto state `else-boundary'.
548 ;; other: Error, pop state, retry token.
550 ;; State `else-boundary':
551 ;; "if": Pop state.
552 ;; boundary: Error, pop state.
553 ;; other: See common state.
555 ;; State `while':
556 ;; boundary: Save position, goto state `while-boundary'.
557 ;; other: Pop state, retry token.
559 ;; State `while-boundary':
560 ;; "do": Pop state.
561 ;; boundary: Restore position if it's not at start, pop state. [*see below]
562 ;; other: See common state.
564 ;; State `catch':
565 ;; boundary: Goto state `catch-boundary'.
566 ;; other: Error, pop state, retry token.
568 ;; State `catch-boundary':
569 ;; "try": Pop state.
570 ;; "catch": Goto state `catch'.
571 ;; boundary: Error, pop state.
572 ;; other: See common state.
574 ;; [*] In the `while-boundary' state, we had pushed a 'while state, and were
575 ;; searching for a "do" which would have opened a do-while. If we didn't
576 ;; find it, we discard the analysis done since the "while", go back to this
577 ;; token in the buffer and restart the scanning there, this time WITHOUT
578 ;; pushing the 'while state onto the stack.
580 ;; In addition to the above there is some special handling of labels
581 ;; and macros.
583 (let ((case-fold-search nil)
584 (start (point))
585 macro-start
586 (delims (if comma-delim '(?\; ?,) '(?\;)))
587 (c-stmt-delim-chars (if comma-delim
588 c-stmt-delim-chars-with-comma
589 c-stmt-delim-chars))
590 pos ; Current position.
591 boundary-pos ; Position of last stmt boundary character (e.g. ;).
592 after-labels-pos ; Value of tok after first found colon.
593 last-label-pos ; Value of tok after last found colon.
594 sym ; Symbol just scanned back over (e.g. 'while or
595 ; 'boundary). See above
596 state ; Current state in the automaton. See above.
597 saved-pos ; Current saved positions. See above
598 stack ; Stack of conses (state . saved-pos).
599 (cond-key (or c-opt-block-stmt-key ; regexp which matches "for", "if", etc.
600 "\\<\\>")) ; Matches nothing.
601 (ret 'same) ; Return value.
602 tok ptok pptok ; Pos of last three sexps or bounds.
603 c-in-literal-cache c-maybe-labelp saved)
605 (save-restriction
606 (if lim (narrow-to-region lim (point-max)))
608 (if (save-excursion
609 (and (c-beginning-of-macro)
610 (/= (point) start)))
611 (setq macro-start (point)))
613 ;; Try to skip back over unary operator characters, to register
614 ;; that we've moved.
615 (while (progn
616 (setq pos (point))
617 (if (c-mode-is-new-awk-p)
618 (c-awk-backward-syntactic-ws)
619 (c-backward-syntactic-ws))
620 (/= (skip-chars-backward "-+!*&~@`#") 0))) ; ACM, 2002/5/31;
621 ; Make a variable in
622 ; cc-langs.el, maybe
624 ;; Skip back over any semicolon here. If it was a bare semicolon, we're
625 ;; done. Later on we ignore the boundaries for statements that doesn't
626 ;; contain any sexp. The only thing that is affected is that the error
627 ;; checking is a little less strict, and we really don't bother.
628 (if (and (memq (char-before) delims)
629 (progn (forward-char -1)
630 (setq saved (point))
631 (if (c-mode-is-new-awk-p)
632 (c-awk-backward-syntactic-ws)
633 (c-backward-syntactic-ws))
634 (or (memq (char-before) delims)
635 (memq (char-before) '(?: nil))
636 (eq (char-syntax (char-before)) ?\()
637 (and (c-mode-is-new-awk-p)
638 (c-awk-after-logical-semicolon))))) ; ACM 2002/6/22
639 ;; ACM, 2002/7/20: What about giving a limit to the above function?
640 ;; ACM, 2003/6/16: The above two lines (checking for
641 ;; awk-logical-semicolon) are probably redundant after rewriting
642 ;; c-awk-backward-syntactic-ws.
643 (setq ret 'previous
644 pos saved)
646 ;; Begin at start and not pos to detect macros if we stand
647 ;; directly after the #.
648 (goto-char start)
649 (if (looking-at "\\<\\|\\W")
650 ;; Record this as the first token if not starting inside it.
651 (setq tok start))
653 ;; The following while loop goes back one sexp (balanced parens,
654 ;; etc. with contents, or symbol or suchlike) each iteration. This
655 ;; movement is accomplished with a call to scan-sexps approx 130 lines
656 ;; below.
657 (while
658 (catch 'loop ;; Throw nil to break, non-nil to continue.
659 (cond
660 ;; Check for macro start. Take this out for AWK Mode (ACM, 2002/5/31)
661 ;; NO!! just make sure macro-start is nil in AWK Mode (ACM, 2002/6/22)
662 ;; It always is (ACM, 2002/6/23)
663 ((save-excursion
664 (and macro-start
665 (progn (skip-chars-backward " \t")
666 (eq (char-before) ?#))
667 (progn (setq saved (1- (point)))
668 (beginning-of-line)
669 (not (eq (char-before (1- (point))) ?\\)))
670 (looking-at c-opt-cpp-start)
671 (progn (skip-chars-forward " \t")
672 (eq (point) saved))))
673 (goto-char saved)
674 (if (and (c-forward-to-cpp-define-body)
675 (progn (c-forward-syntactic-ws start)
676 (< (point) start)))
677 ;; Stop at the first token in the content of the macro.
678 (setq pos (point)
679 ignore-labels t) ; Avoid the label check on exit.
680 (setq pos saved
681 ret 'macro
682 ignore-labels t))
683 (throw 'loop nil))
685 ;; Do a round through the automaton if we've just passed a
686 ;; statement boundary or passed a "while"-like token.
687 ((or sym
688 (and (looking-at cond-key)
689 (setq sym (intern (match-string 1)))))
691 (when (and (< pos start) (null stack))
692 (throw 'loop nil))
694 ;; The PDA state handling.
696 ;; Refer to the description of the PDA in the opening
697 ;; comments. In the following OR form, the first leaf
698 ;; attempts to handles one of the specific actions detailed
699 ;; (e.g., finding token "if" whilst in state `else-boundary').
700 ;; We drop through to the second leaf (which handles common
701 ;; state) if no specific handler is found in the first cond.
702 ;; If a parsing error is detected (e.g. an "else" with no
703 ;; preceding "if"), we throw to the enclosing catch.
705 ;; Note that the (eq state 'else) means
706 ;; "we've just passed an else", NOT "we're looking for an
707 ;; else".
708 (or (cond
709 ((eq state 'else)
710 (if (eq sym 'boundary)
711 (setq state 'else-boundary)
712 (c-bos-report-error)
713 (c-bos-pop-state-and-retry)))
715 ((eq state 'else-boundary)
716 (cond ((eq sym 'if)
717 (c-bos-pop-state (setq ret 'beginning)))
718 ((eq sym 'boundary)
719 (c-bos-report-error)
720 (c-bos-pop-state))))
722 ((eq state 'while)
723 (if (and (eq sym 'boundary)
724 ;; Since this can cause backtracking we do a
725 ;; little more careful analysis to avoid it:
726 ;; If there's a label in front of the while
727 ;; it can't be part of a do-while.
728 (not after-labels-pos))
729 (progn (c-bos-save-pos)
730 (setq state 'while-boundary))
731 (c-bos-pop-state-and-retry))) ; Can't be a do-while
733 ((eq state 'while-boundary)
734 (cond ((eq sym 'do)
735 (c-bos-pop-state (setq ret 'beginning)))
736 ((eq sym 'boundary) ; isn't a do-while
737 (c-bos-restore-pos) ; the position of the while
738 (c-bos-pop-state)))) ; no longer searching for do.
740 ((eq state 'catch)
741 (if (eq sym 'boundary)
742 (setq state 'catch-boundary)
743 (c-bos-report-error)
744 (c-bos-pop-state-and-retry)))
746 ((eq state 'catch-boundary)
747 (cond
748 ((eq sym 'try)
749 (c-bos-pop-state (setq ret 'beginning)))
750 ((eq sym 'catch)
751 (setq state 'catch))
752 ((eq sym 'boundary)
753 (c-bos-report-error)
754 (c-bos-pop-state)))))
756 ;; This is state common. We get here when the previous
757 ;; cond statement found no particular state handler.
758 (cond ((eq sym 'boundary)
759 ;; If we have a boundary at the start
760 ;; position we push a frame to go to the
761 ;; previous statement.
762 (if (>= pos start)
763 (c-bos-push-state)
764 (c-bos-pop-state)))
765 ((eq sym 'else)
766 (c-bos-push-state)
767 (c-bos-save-error-info 'if 'else)
768 (setq state 'else))
769 ((eq sym 'while)
770 (when (or (not pptok)
771 (memq (char-after pptok) delims)
772 (and (c-mode-is-new-awk-p)
774 ;; might we be calling this from
775 ;; c-awk-after-if-do-for-while-condition-p?
776 ;; If so, avoid infinite recursion.
777 (and (eq (point) start)
778 (c-awk-NL-prop-not-set))
779 ;; The following may recursively
780 ;; call this function.
781 (c-awk-completed-stmt-ws-ends-line-p pptok))))
782 ;; Since this can cause backtracking we do a
783 ;; little more careful analysis to avoid it: If
784 ;; the while isn't followed by a semicolon it
785 ;; can't be a do-while.
786 ;; ACM, 2002/5/31; IT CAN IN AWK Mode. ;-(
787 (c-bos-push-state)
788 (setq state 'while)))
789 ((memq sym '(catch finally))
790 (c-bos-push-state)
791 (c-bos-save-error-info 'try sym)
792 (setq state 'catch))))
794 (when c-maybe-labelp
795 ;; We're either past a statement boundary or at the
796 ;; start of a statement, so throw away any label data
797 ;; for the previous one.
798 (setq after-labels-pos nil
799 last-label-pos nil
800 c-maybe-labelp nil))))
802 ;; Step to the previous sexp, but not if we crossed a
803 ;; boundary, since that doesn't consume an sexp.
804 (if (eq sym 'boundary)
805 (setq ret 'previous)
807 ;; HERE IS THE SINGLE PLACE INSIDE THE PDA LOOP WHERE WE MOVE
808 ;; BACKWARDS THROUGH THE SOURCE. The following loop goes back
809 ;; one sexp and then only loops in special circumstances (line
810 ;; continuations and skipping past entire macros).
811 (while
812 (progn
813 (or (c-safe (goto-char (scan-sexps (point) -1)) t)
814 ;; Give up if we hit an unbalanced block.
815 ;; Since the stack won't be empty the code
816 ;; below will report a suitable error.
817 (throw 'loop nil))
818 (cond ((looking-at "\\\\$")
819 ;; Step again if we hit a line continuation.
821 (macro-start
822 ;; If we started inside a macro then this
823 ;; sexp is always interesting.
824 nil)
825 ((not (c-mode-is-new-awk-p)) ; Changed from t, ACM 2002/6/25
826 ;; Otherwise check that we didn't step
827 ;; into a macro from the end.
828 (let ((macro-start
829 (save-excursion
830 (and (c-beginning-of-macro)
831 (point)))))
832 (when macro-start
833 (goto-char macro-start)
834 t))))))
836 ;; Did the last movement by a sexp cross a statement boundary?
837 (when (save-excursion
838 (if (if (eq (char-after) ?{)
839 (c-looking-at-inexpr-block lim nil)
840 (looking-at "\\s\("))
842 ;; Should not include the paren sexp we've
843 ;; passed over in the boundary check.
844 (if (> (point) (- pos 100))
845 (c-forward-sexp 1)
847 ;; Find its end position this way instead of
848 ;; moving forward if the sexp is large.
849 (goto-char pos)
850 (while
851 (progn
852 (goto-char (1+ (c-down-list-backward)))
853 (unless macro-start
854 ;; Check that we didn't step into
855 ;; a macro from the end.
856 (let ((macro-start
857 (save-excursion
858 (and (c-beginning-of-macro)
859 (point)))))
860 (when macro-start
861 (goto-char macro-start)
862 t)))))))
864 (setq boundary-pos (c-crosses-statement-barrier-p
865 (point) pos)))
867 (setq pptok ptok
868 ptok tok
869 tok boundary-pos
870 sym 'boundary)
871 (throw 'loop t))) ; like a C "continue". Analyze the next sexp.
873 (when (and (numberp c-maybe-labelp)
874 (not ignore-labels)
875 (not (looking-at "\\s\(")))
876 ;; c-crosses-statement-barrier-p has found a colon, so
877 ;; we might be in a label now.
878 (if (not after-labels-pos)
879 (setq after-labels-pos tok))
880 (setq last-label-pos tok
881 c-maybe-labelp t))
883 ;; ObjC method def?
884 (when (and c-opt-method-key
885 (setq saved (c-in-method-def-p)))
886 (setq pos saved
887 ignore-labels t) ; Avoid the label check on exit.
888 (throw 'loop nil))
890 ;; We've moved back by a sexp, so update the token positions.
891 (setq sym nil
892 pptok ptok
893 ptok tok
894 tok (point)
895 pos tok))) ; Not nil (for the while loop).
897 ;; If the stack isn't empty there might be errors to report.
898 (while stack
899 (if (and (vectorp saved-pos) (eq (length saved-pos) 3))
900 (c-bos-report-error))
901 (setq saved-pos (cdr (car stack))
902 stack (cdr stack)))
904 (when (and (eq ret 'same)
905 (not (memq sym '(boundary ignore nil))))
906 ;; Need to investigate closer whether we've crossed
907 ;; between a substatement and its containing statement.
908 (if (setq saved (if (looking-at c-block-stmt-1-key)
909 ptok
910 pptok))
911 (cond ((> start saved) (setq pos saved))
912 ((= start saved) (setq ret 'up)))))
914 (when (and c-maybe-labelp
915 (not ignore-labels)
916 (not (eq ret 'beginning))
917 after-labels-pos)
918 ;; We're in a label. Maybe we should step to the statement
919 ;; after it.
920 (if (< after-labels-pos start)
921 (setq pos after-labels-pos)
922 (setq ret 'label)
923 (if (< last-label-pos start)
924 (setq pos last-label-pos)))))
926 ;; Skip over the unary operators that can start the statement.
927 (goto-char pos)
928 (while (progn
929 (if (c-mode-is-new-awk-p)
930 (c-awk-backward-syntactic-ws)
931 (c-backward-syntactic-ws))
932 (/= (skip-chars-backward "-+!*&~@`#") 0)) ; Hopefully the # won't hurt awk.
933 (setq pos (point)))
934 (goto-char pos)
935 ret)))
937 (defun c-crosses-statement-barrier-p (from to)
938 "Return non-nil if buffer positions FROM to TO cross one or more
939 statement or declaration boundaries. The returned value is actually
940 the position of the earliest boundary char. FROM must not be within
941 a string or comment.
943 The variable `c-maybe-labelp' is set to the position of the first `:' that
944 might start a label (i.e. not part of `::' and not preceded by `?'). If a
945 single `?' is found, then `c-maybe-labelp' is cleared."
946 (let ((skip-chars c-stmt-delim-chars)
947 lit-range)
948 (save-excursion
949 (catch 'done
950 (goto-char from)
951 (while (progn (skip-chars-forward skip-chars to)
952 (< (point) to))
953 (if (setq lit-range (c-literal-limits from)) ; Have we landed in a string/comment?
954 (progn (goto-char (setq from (cdr lit-range)))
955 (if (and (c-mode-is-new-awk-p) (bolp)) ; ACM 2002/7/17. Make sure we
956 (backward-char))) ; don't skip over a virtual semi-colon after an awk comment. :-(
957 (cond ((eq (char-after) ?:)
958 (forward-char)
959 (if (and (eq (char-after) ?:)
960 (< (point) to))
961 ;; Ignore scope operators.
962 (forward-char)
963 (setq c-maybe-labelp (1- (point)))))
964 ((eq (char-after) ??)
965 ;; A question mark. Can't be a label, so stop
966 ;; looking for more : and ?.
967 (setq c-maybe-labelp nil
968 skip-chars (substring c-stmt-delim-chars 0 -2)))
969 ((and (eolp) ; Can only happen in AWK Mode
970 (not (c-awk-completed-stmt-ws-ends-line-p)))
971 (forward-char))
972 ((and (c-mode-is-new-awk-p)
973 (bolp) lit-range ; awk: comment/string ended prev line.
974 (not (c-awk-completed-stmt-ws-ends-prev-line-p))))
975 (t (throw 'done (point))))))
976 nil))))
979 ;; A set of functions that covers various idiosyncrasies in
980 ;; implementations of `forward-comment'.
982 ;; Note: Some emacsen considers incorrectly that any line comment
983 ;; ending with a backslash continues to the next line. I can't think
984 ;; of any way to work around that in a reliable way without changing
985 ;; the buffer, though. Suggestions welcome. ;) (No, temporarily
986 ;; changing the syntax for backslash doesn't work since we must treat
987 ;; escapes in string literals correctly.)
989 (defun c-forward-single-comment ()
990 "Move forward past whitespace and the closest following comment, if any.
991 Return t if a comment was found, nil otherwise. In either case, the
992 point is moved past the following whitespace. Line continuations,
993 i.e. a backslashes followed by line breaks, are treated as whitespace.
994 The line breaks that end line comments are considered to be the
995 comment enders, so the point will be put on the beginning of the next
996 line if it moved past a line comment.
998 This function does not do any hidden buffer changes."
1000 (let ((start (point)))
1001 (when (looking-at "\\([ \t\n\r\f\v]\\|\\\\[\n\r]\\)+")
1002 (goto-char (match-end 0)))
1004 (when (forward-comment 1)
1005 (if (eobp)
1006 ;; Some emacsen (e.g. XEmacs 21) return t when moving
1007 ;; forwards at eob.
1010 ;; Emacs includes the ending newline in a b-style (c++)
1011 ;; comment, but XEmacs doesn't. We depend on the Emacs
1012 ;; behavior (which also is symmetric).
1013 (if (and (eolp) (elt (parse-partial-sexp start (point)) 7))
1014 (condition-case nil (forward-char 1)))
1016 t))))
1018 (defsubst c-forward-comments ()
1019 "Move forward past all following whitespace and comments.
1020 Line continuations, i.e. a backslashes followed by line breaks, are
1021 treated as whitespace.
1023 This function does not do any hidden buffer changes."
1025 (while (or
1026 ;; If forward-comment in at least XEmacs 21 is given a large
1027 ;; positive value, it'll loop all the way through if it hits
1028 ;; eob.
1029 (and (forward-comment 5)
1030 ;; Some emacsen (e.g. XEmacs 21) return t when moving
1031 ;; forwards at eob.
1032 (not (eobp)))
1034 (when (looking-at "\\\\[\n\r]")
1035 (forward-char 2)
1036 t))))
1038 (defun c-backward-single-comment ()
1039 "Move backward past whitespace and the closest preceding comment, if any.
1040 Return t if a comment was found, nil otherwise. In either case, the
1041 point is moved past the preceding whitespace. Line continuations,
1042 i.e. a backslashes followed by line breaks, are treated as whitespace.
1043 The line breaks that end line comments are considered to be the
1044 comment enders, so the point cannot be at the end of the same line to
1045 move over a line comment.
1047 This function does not do any hidden buffer changes."
1049 (let ((start (point)))
1050 ;; When we got newline terminated comments, forward-comment in all
1051 ;; supported emacsen so far will stop at eol of each line not
1052 ;; ending with a comment when moving backwards. This corrects for
1053 ;; that, and at the same time handles line continuations.
1054 (while (progn
1055 (skip-chars-backward " \t\n\r\f\v")
1056 (and (looking-at "[\n\r]")
1057 (eq (char-before) ?\\)
1058 (< (point) start)))
1059 (backward-char))
1061 (if (bobp)
1062 ;; Some emacsen (e.g. Emacs 19.34) return t when moving
1063 ;; backwards at bob.
1066 ;; Leave point after the closest following newline if we've
1067 ;; backed up over any above, since forward-comment won't move
1068 ;; backward over a line comment if point is at the end of the
1069 ;; same line.
1070 (re-search-forward "\\=\\s *[\n\r]" start t)
1072 (if (if (forward-comment -1)
1073 (if (eolp)
1074 ;; If forward-comment above succeeded and we're at eol
1075 ;; then the newline we moved over above didn't end a
1076 ;; line comment, so we give it another go.
1077 (forward-comment -1)
1080 ;; Emacs <= 20 and XEmacs move back over the closer of a
1081 ;; block comment that lacks an opener.
1082 (if (looking-at "\\*/")
1083 (progn (forward-char 2) nil)
1084 t)))))
1086 (defsubst c-backward-comments ()
1087 "Move backward past all preceding whitespace and comments.
1088 Line continuations, i.e. a backslashes followed by line breaks, are
1089 treated as whitespace. The line breaks that end line comments are
1090 considered to be the comment enders, so the point cannot be at the end
1091 of the same line to move over a line comment.
1093 This function does not do any hidden buffer changes."
1095 (let ((start (point)))
1096 (while (and
1097 ;; `forward-comment' in some emacsen (e.g. Emacs 19.34)
1098 ;; return t when moving backwards at bob.
1099 (not (bobp))
1101 (if (forward-comment -1)
1102 (if (looking-at "\\*/")
1103 ;; Emacs <= 20 and XEmacs move back over the
1104 ;; closer of a block comment that lacks an opener.
1105 (progn (forward-char 2) nil)
1108 ;; XEmacs treats line continuations as whitespace but
1109 ;; only in the backward direction, which seems a bit
1110 ;; odd. Anyway, this is necessary for Emacs.
1111 (when (and (looking-at "[\n\r]")
1112 (eq (char-before) ?\\)
1113 (< (point) start))
1114 (backward-char)
1115 t))))))
1118 ;; Tools for skipping over syntactic whitespace.
1120 ;; The following functions use text properties to cache searches over
1121 ;; large regions of syntactic whitespace. It works as follows:
1123 ;; o If a syntactic whitespace region contains anything but simple
1124 ;; whitespace (i.e. space, tab and line breaks), the text property
1125 ;; `c-in-sws' is put over it. At places where we have stopped
1126 ;; within that region there's also a `c-is-sws' text property.
1127 ;; That since there typically are nested whitespace inside that
1128 ;; must be handled separately, e.g. whitespace inside a comment or
1129 ;; cpp directive. Thus, from one point with `c-is-sws' it's safe
1130 ;; to jump to another point with that property within the same
1131 ;; `c-in-sws' region. It can be likened to a ladder where
1132 ;; `c-in-sws' marks the bars and `c-is-sws' the rungs.
1134 ;; o The `c-is-sws' property is put on the simple whitespace chars at
1135 ;; a "rung position" and also maybe on the first following char.
1136 ;; As many characters as can be conveniently found in this range
1137 ;; are marked, but no assumption can be made that the whole range
1138 ;; is marked (it could be clobbered by later changes, for
1139 ;; instance).
1141 ;; Note that some part of the beginning of a sequence of simple
1142 ;; whitespace might be part of the end of a preceding line comment
1143 ;; or cpp directive and must not be considered part of the "rung".
1144 ;; Such whitespace is some amount of horizontal whitespace followed
1145 ;; by a newline. In the case of cpp directives it could also be
1146 ;; two newlines with horizontal whitespace between them.
1148 ;; The reason to include the first following char is to cope with
1149 ;; "rung positions" that doesn't have any ordinary whitespace. If
1150 ;; `c-is-sws' is put on a token character it does not have
1151 ;; `c-in-sws' set simultaneously. That's the only case when that
1152 ;; can occur, and the reason for not extending the `c-in-sws'
1153 ;; region to cover it is that the `c-in-sws' region could then be
1154 ;; accidentally merged with a following one if the token is only
1155 ;; one character long.
1157 ;; o On buffer changes the `c-in-sws' and `c-is-sws' properties are
1158 ;; removed in the changed region. If the change was inside
1159 ;; syntactic whitespace that means that the "ladder" is broken, but
1160 ;; a later call to `c-forward-sws' or `c-backward-sws' will use the
1161 ;; parts on either side and use an ordinary search only to "repair"
1162 ;; the gap.
1164 ;; Special care needs to be taken if a region is removed: If there
1165 ;; are `c-in-sws' on both sides of it which do not connect inside
1166 ;; the region then they can't be joined. If e.g. a marked macro is
1167 ;; broken, syntactic whitespace inside the new text might be
1168 ;; marked. If those marks would become connected with the old
1169 ;; `c-in-sws' range around the macro then we could get a ladder
1170 ;; with one end outside the macro and the other at some whitespace
1171 ;; within it.
1173 ;; The main motivation for this system is to increase the speed in
1174 ;; skipping over the large whitespace regions that can occur at the
1175 ;; top level in e.g. header files that contain a lot of comments and
1176 ;; cpp directives. For small comments inside code it's probably
1177 ;; slower than using `forward-comment' straightforwardly, but speed is
1178 ;; not a significant factor there anyway.
1180 ; (defface c-debug-is-sws-face
1181 ; '((t (:background "GreenYellow")))
1182 ; "Debug face to mark the `c-is-sws' property.")
1183 ; (defface c-debug-in-sws-face
1184 ; '((t (:underline t)))
1185 ; "Debug face to mark the `c-in-sws' property.")
1187 ; (defun c-debug-put-sws-faces ()
1188 ; ;; Put the sws debug faces on all the `c-is-sws' and `c-in-sws'
1189 ; ;; properties in the buffer.
1190 ; (interactive)
1191 ; (save-excursion
1192 ; (let (in-face)
1193 ; (goto-char (point-min))
1194 ; (setq in-face (if (get-text-property (point) 'c-is-sws)
1195 ; (point)))
1196 ; (while (progn
1197 ; (goto-char (next-single-property-change
1198 ; (point) 'c-is-sws nil (point-max)))
1199 ; (if in-face
1200 ; (progn
1201 ; (c-debug-add-face in-face (point) 'c-debug-is-sws-face)
1202 ; (setq in-face nil))
1203 ; (setq in-face (point)))
1204 ; (not (eobp))))
1205 ; (goto-char (point-min))
1206 ; (setq in-face (if (get-text-property (point) 'c-in-sws)
1207 ; (point)))
1208 ; (while (progn
1209 ; (goto-char (next-single-property-change
1210 ; (point) 'c-in-sws nil (point-max)))
1211 ; (if in-face
1212 ; (progn
1213 ; (c-debug-add-face in-face (point) 'c-debug-in-sws-face)
1214 ; (setq in-face nil))
1215 ; (setq in-face (point)))
1216 ; (not (eobp)))))))
1218 (defmacro c-debug-sws-msg (&rest args)
1219 ;;`(message ,@args)
1222 (defmacro c-put-is-sws (beg end)
1223 `(let ((beg ,beg) (end ,end))
1224 (put-text-property beg end 'c-is-sws t)
1225 ,@(when (facep 'c-debug-is-sws-face)
1226 `((c-debug-add-face beg end 'c-debug-is-sws-face)))))
1228 (defmacro c-put-in-sws (beg end)
1229 `(let ((beg ,beg) (end ,end))
1230 (put-text-property beg end 'c-in-sws t)
1231 ,@(when (facep 'c-debug-is-sws-face)
1232 `((c-debug-add-face beg end 'c-debug-in-sws-face)))))
1234 (defmacro c-remove-is-sws (beg end)
1235 `(let ((beg ,beg) (end ,end))
1236 (remove-text-properties beg end '(c-is-sws nil))
1237 ,@(when (facep 'c-debug-is-sws-face)
1238 `((c-debug-remove-face beg end 'c-debug-is-sws-face)))))
1240 (defmacro c-remove-in-sws (beg end)
1241 `(let ((beg ,beg) (end ,end))
1242 (remove-text-properties beg end '(c-in-sws nil))
1243 ,@(when (facep 'c-debug-is-sws-face)
1244 `((c-debug-remove-face beg end 'c-debug-in-sws-face)))))
1246 (defmacro c-remove-is-and-in-sws (beg end)
1247 `(let ((beg ,beg) (end ,end))
1248 (remove-text-properties beg end '(c-is-sws nil c-in-sws nil))
1249 ,@(when (facep 'c-debug-is-sws-face)
1250 `((c-debug-remove-face beg end 'c-debug-is-sws-face)
1251 (c-debug-remove-face beg end 'c-debug-in-sws-face)))))
1253 (defsubst c-invalidate-sws-region-after (beg end)
1254 ;; Called from `after-change-functions'. Note that if
1255 ;; `c-forward-sws' or `c-backward-sws' are used outside
1256 ;; `c-save-buffer-state' or similar then this will remove the cache
1257 ;; properties right after they're added.
1259 (save-excursion
1260 ;; Adjust the end to remove the properties in any following simple
1261 ;; ws up to and including the next line break, if there is any
1262 ;; after the changed region. This is necessary e.g. when a rung
1263 ;; marked empty line is converted to a line comment by inserting
1264 ;; "//" before the line break. In that case the line break would
1265 ;; keep the rung mark which could make a later `c-backward-sws'
1266 ;; move into the line comment instead of over it.
1267 (goto-char end)
1268 (skip-chars-forward " \t\f\v")
1269 (when (and (eolp) (not (eobp)))
1270 (setq end (1+ (point)))))
1272 (when (and (= beg end)
1273 (get-text-property beg 'c-in-sws)
1274 (> beg (point-min))
1275 (get-text-property (1- beg) 'c-in-sws))
1276 ;; Ensure that an `c-in-sws' range gets broken. Note that it isn't
1277 ;; safe to keep a range that was continuous before the change. E.g:
1279 ;; #define foo
1280 ;; \
1281 ;; bar
1283 ;; There can be a "ladder" between "#" and "b". Now, if the newline
1284 ;; after "foo" is removed then "bar" will become part of the cpp
1285 ;; directive instead of a syntactically relevant token. In that
1286 ;; case there's no longer syntactic ws from "#" to "b".
1287 (setq beg (1- beg)))
1289 (c-debug-sws-msg "c-invalidate-sws-region-after [%s..%s]" beg end)
1290 (c-remove-is-and-in-sws beg end))
1292 (defun c-forward-sws ()
1293 ;; Used by `c-forward-syntactic-ws' to implement the unbounded search.
1295 (let (;; `rung-pos' is set to a position as early as possible in the
1296 ;; unmarked part of the simple ws region.
1297 (rung-pos (point)) next-rung-pos rung-end-pos last-put-in-sws-pos
1298 rung-is-marked next-rung-is-marked simple-ws-end
1299 ;; `safe-start' is set when it's safe to cache the start position.
1300 ;; It's not set if we've initially skipped over comments and line
1301 ;; continuations since we might have gone out through the end of a
1302 ;; macro then. This provision makes `c-forward-sws' not populate the
1303 ;; cache in the majority of cases, but otoh is `c-backward-sws' by far
1304 ;; more common.
1305 safe-start)
1307 ;; Skip simple ws and do a quick check on the following character to see
1308 ;; if it's anything that can't start syntactic ws, so we can bail out
1309 ;; early in the majority of cases when there just are a few ws chars.
1310 (skip-chars-forward " \t\n\r\f\v")
1311 (when (looking-at c-syntactic-ws-start)
1313 (setq rung-end-pos (min (1+ (point)) (point-max)))
1314 (if (setq rung-is-marked (text-property-any rung-pos rung-end-pos
1315 'c-is-sws t))
1316 ;; Find the last rung position to avoid setting properties in all
1317 ;; the cases when the marked rung is complete.
1318 ;; (`next-single-property-change' is certain to move at least one
1319 ;; step forward.)
1320 (setq rung-pos (1- (next-single-property-change
1321 rung-is-marked 'c-is-sws nil rung-end-pos)))
1322 ;; Got no marked rung here. Since the simple ws might have started
1323 ;; inside a line comment or cpp directive we must set `rung-pos' as
1324 ;; high as possible.
1325 (setq rung-pos (point)))
1327 (while
1328 (progn
1329 (while
1330 (when (and rung-is-marked
1331 (get-text-property (point) 'c-in-sws))
1333 ;; The following search is the main reason that `c-in-sws'
1334 ;; and `c-is-sws' aren't combined to one property.
1335 (goto-char (next-single-property-change
1336 (point) 'c-in-sws nil (point-max)))
1337 (unless (get-text-property (point) 'c-is-sws)
1338 ;; If the `c-in-sws' region extended past the last
1339 ;; `c-is-sws' char we have to go back a bit.
1340 (or (get-text-property (1- (point)) 'c-is-sws)
1341 (goto-char (previous-single-property-change
1342 (point) 'c-is-sws)))
1343 (backward-char))
1345 (c-debug-sws-msg
1346 "c-forward-sws cached move %s -> %s (max %s)"
1347 rung-pos (point) (point-max))
1349 (setq rung-pos (point))
1350 (and (> (skip-chars-forward " \t\n\r\f\v") 0)
1351 (not (eobp))))
1353 ;; We'll loop here if there is simple ws after the last rung.
1354 ;; That means that there's been some change in it and it's
1355 ;; possible that we've stepped into another ladder, so extend
1356 ;; the previous one to join with it if there is one, and try to
1357 ;; use the cache again.
1358 (c-debug-sws-msg
1359 "c-forward-sws extending rung with [%s..%s] (max %s)"
1360 (1+ rung-pos) (1+ (point)) (point-max))
1361 (unless (get-text-property (point) 'c-is-sws)
1362 ;; Remove any `c-in-sws' property from the last char of
1363 ;; the rung before we mark it with `c-is-sws', so that we
1364 ;; won't connect with the remains of a broken "ladder".
1365 (c-remove-in-sws (point) (1+ (point))))
1366 (c-put-is-sws (1+ rung-pos)
1367 (1+ (point)))
1368 (c-put-in-sws rung-pos
1369 (setq rung-pos (point)
1370 last-put-in-sws-pos rung-pos)))
1372 (setq simple-ws-end (point))
1373 (c-forward-comments)
1375 (cond
1376 ((/= (point) simple-ws-end)
1377 ;; Skipped over comments. Don't cache at eob in case the buffer
1378 ;; is narrowed.
1379 (not (eobp)))
1381 ((save-excursion
1382 (and c-opt-cpp-prefix
1383 (looking-at c-opt-cpp-start)
1384 (progn (skip-chars-backward " \t")
1385 (bolp))
1386 (or (bobp)
1387 (progn (backward-char)
1388 (not (eq (char-before) ?\\))))))
1389 ;; Skip a preprocessor directive.
1390 (end-of-line)
1391 (while (and (eq (char-before) ?\\)
1392 (= (forward-line 1) 0))
1393 (end-of-line))
1394 (forward-line 1)
1395 (setq safe-start t)
1396 ;; Don't cache at eob in case the buffer is narrowed.
1397 (not (eobp)))))
1399 ;; We've searched over a piece of non-white syntactic ws. See if this
1400 ;; can be cached.
1401 (setq next-rung-pos (point))
1402 (skip-chars-forward " \t\n\r\f\v")
1403 (setq rung-end-pos (min (1+ (point)) (point-max)))
1405 (if (or
1406 ;; Cache if we haven't skipped comments only, and if we started
1407 ;; either from a marked rung or from a completely uncached
1408 ;; position.
1409 (and safe-start
1410 (or rung-is-marked
1411 (not (get-text-property simple-ws-end 'c-in-sws))))
1413 ;; See if there's a marked rung in the encountered simple ws. If
1414 ;; so then we can cache, unless `safe-start' is nil. Even then
1415 ;; we need to do this to check if the cache can be used for the
1416 ;; next step.
1417 (and (setq next-rung-is-marked
1418 (text-property-any next-rung-pos rung-end-pos
1419 'c-is-sws t))
1420 safe-start))
1422 (progn
1423 (c-debug-sws-msg
1424 "c-forward-sws caching [%s..%s] - [%s..%s] (max %s)"
1425 rung-pos (1+ simple-ws-end) next-rung-pos rung-end-pos
1426 (point-max))
1428 ;; Remove the properties for any nested ws that might be cached.
1429 ;; Only necessary for `c-is-sws' since `c-in-sws' will be set
1430 ;; anyway.
1431 (c-remove-is-sws (1+ simple-ws-end) next-rung-pos)
1432 (unless (and rung-is-marked (= rung-pos simple-ws-end))
1433 (c-put-is-sws rung-pos
1434 (1+ simple-ws-end))
1435 (setq rung-is-marked t))
1436 (c-put-in-sws rung-pos
1437 (setq rung-pos (point)
1438 last-put-in-sws-pos rung-pos))
1439 (unless (get-text-property (1- rung-end-pos) 'c-is-sws)
1440 ;; Remove any `c-in-sws' property from the last char of
1441 ;; the rung before we mark it with `c-is-sws', so that we
1442 ;; won't connect with the remains of a broken "ladder".
1443 (c-remove-in-sws (1- rung-end-pos) rung-end-pos))
1444 (c-put-is-sws next-rung-pos
1445 rung-end-pos))
1447 (c-debug-sws-msg
1448 "c-forward-sws not caching [%s..%s] - [%s..%s] (max %s)"
1449 rung-pos (1+ simple-ws-end) next-rung-pos rung-end-pos
1450 (point-max))
1452 ;; Set `rung-pos' for the next rung. It's the same thing here as
1453 ;; initially, except that the rung position is set as early as
1454 ;; possible since we can't be in the ending ws of a line comment or
1455 ;; cpp directive now.
1456 (if (setq rung-is-marked next-rung-is-marked)
1457 (setq rung-pos (1- (next-single-property-change
1458 rung-is-marked 'c-is-sws nil rung-end-pos)))
1459 (setq rung-pos next-rung-pos))
1460 (setq safe-start t)))
1462 ;; Make sure that the newly marked `c-in-sws' region doesn't connect to
1463 ;; another one after the point (which might occur when editing inside a
1464 ;; comment or macro).
1465 (when (eq last-put-in-sws-pos (point))
1466 (cond ((< last-put-in-sws-pos (point-max))
1467 (c-debug-sws-msg
1468 "c-forward-sws clearing at %s for cache separation"
1469 last-put-in-sws-pos)
1470 (c-remove-in-sws last-put-in-sws-pos
1471 (1+ last-put-in-sws-pos)))
1473 ;; If at eob we have to clear the last character before the end
1474 ;; instead since the buffer might be narrowed and there might
1475 ;; be a `c-in-sws' after (point-max). In this case it's
1476 ;; necessary to clear both properties.
1477 (c-debug-sws-msg
1478 "c-forward-sws clearing thoroughly at %s for cache separation"
1479 (1- last-put-in-sws-pos))
1480 (c-remove-is-and-in-sws (1- last-put-in-sws-pos)
1481 last-put-in-sws-pos))))
1484 (defun c-backward-sws ()
1485 ;; Used by `c-backward-syntactic-ws' to implement the unbounded search.
1487 (let (;; `rung-pos' is set to a position as late as possible in the unmarked
1488 ;; part of the simple ws region.
1489 (rung-pos (point)) next-rung-pos last-put-in-sws-pos
1490 rung-is-marked simple-ws-beg cmt-skip-pos)
1492 ;; Skip simple horizontal ws and do a quick check on the preceding
1493 ;; character to see if it's anying that can't end syntactic ws, so we can
1494 ;; bail out early in the majority of cases when there just are a few ws
1495 ;; chars. Newlines are complicated in the backward direction, so we can't
1496 ;; skip over them.
1497 (skip-chars-backward " \t\f")
1498 (when (and (not (bobp))
1499 (save-excursion
1500 (backward-char)
1501 (looking-at c-syntactic-ws-end)))
1503 ;; Try to find a rung position in the simple ws preceding point, so that
1504 ;; we can get a cache hit even if the last bit of the simple ws has
1505 ;; changed recently.
1506 (setq simple-ws-beg (point))
1507 (skip-chars-backward " \t\n\r\f\v")
1508 (if (setq rung-is-marked (text-property-any
1509 (point) (min (1+ rung-pos) (point-max))
1510 'c-is-sws t))
1511 ;; `rung-pos' will be the earliest marked position, which means that
1512 ;; there might be later unmarked parts in the simple ws region.
1513 ;; It's not worth the effort to fix that; the last part of the
1514 ;; simple ws is also typically edited often, so it could be wasted.
1515 (goto-char (setq rung-pos rung-is-marked))
1516 (goto-char simple-ws-beg))
1518 (while
1519 (progn
1520 (while
1521 (when (and rung-is-marked
1522 (not (bobp))
1523 (get-text-property (1- (point)) 'c-in-sws))
1525 ;; The following search is the main reason that `c-in-sws'
1526 ;; and `c-is-sws' aren't combined to one property.
1527 (goto-char (previous-single-property-change
1528 (point) 'c-in-sws nil (point-min)))
1529 (unless (get-text-property (point) 'c-is-sws)
1530 ;; If the `c-in-sws' region extended past the first
1531 ;; `c-is-sws' char we have to go forward a bit.
1532 (goto-char (next-single-property-change
1533 (point) 'c-is-sws)))
1535 (c-debug-sws-msg
1536 "c-backward-sws cached move %s <- %s (min %s)"
1537 (point) rung-pos (point-min))
1539 (setq rung-pos (point))
1540 (if (and (< (min (skip-chars-backward " \t\f\v")
1541 (progn
1542 (setq simple-ws-beg (point))
1543 (skip-chars-backward " \t\n\r\f\v")))
1545 (setq rung-is-marked
1546 (text-property-any (point) rung-pos
1547 'c-is-sws t)))
1549 (goto-char simple-ws-beg)
1550 nil))
1552 ;; We'll loop here if there is simple ws before the first rung.
1553 ;; That means that there's been some change in it and it's
1554 ;; possible that we've stepped into another ladder, so extend
1555 ;; the previous one to join with it if there is one, and try to
1556 ;; use the cache again.
1557 (c-debug-sws-msg
1558 "c-backward-sws extending rung with [%s..%s] (min %s)"
1559 rung-is-marked rung-pos (point-min))
1560 (unless (get-text-property (1- rung-pos) 'c-is-sws)
1561 ;; Remove any `c-in-sws' property from the last char of
1562 ;; the rung before we mark it with `c-is-sws', so that we
1563 ;; won't connect with the remains of a broken "ladder".
1564 (c-remove-in-sws (1- rung-pos) rung-pos))
1565 (c-put-is-sws rung-is-marked
1566 rung-pos)
1567 (c-put-in-sws rung-is-marked
1568 (1- rung-pos))
1569 (setq rung-pos rung-is-marked
1570 last-put-in-sws-pos rung-pos))
1572 (c-backward-comments)
1573 (setq cmt-skip-pos (point))
1575 (cond
1576 ((and c-opt-cpp-prefix
1577 (/= cmt-skip-pos simple-ws-beg)
1578 (c-beginning-of-macro))
1579 ;; Inside a cpp directive. See if it should be skipped over.
1580 (let ((cpp-beg (point)))
1582 ;; Move back over all line continuations in the region skipped
1583 ;; over by `c-backward-comments'. If we go past it then we
1584 ;; started inside the cpp directive.
1585 (goto-char simple-ws-beg)
1586 (beginning-of-line)
1587 (while (and (> (point) cmt-skip-pos)
1588 (progn (backward-char)
1589 (eq (char-before) ?\\)))
1590 (beginning-of-line))
1592 (if (< (point) cmt-skip-pos)
1593 ;; Don't move past the cpp directive if we began inside
1594 ;; it. Note that the position at the end of the last line
1595 ;; of the macro is also considered to be within it.
1596 (progn (goto-char cmt-skip-pos)
1597 nil)
1599 ;; It's worthwhile to spend a little bit of effort on finding
1600 ;; the end of the macro, to get a good `simple-ws-beg'
1601 ;; position for the cache. Note that `c-backward-comments'
1602 ;; could have stepped over some comments before going into
1603 ;; the macro, and then `simple-ws-beg' must be kept on the
1604 ;; same side of those comments.
1605 (goto-char simple-ws-beg)
1606 (skip-chars-backward " \t\n\r\f\v")
1607 (if (eq (char-before) ?\\)
1608 (forward-char))
1609 (forward-line 1)
1610 (if (< (point) simple-ws-beg)
1611 ;; Might happen if comments after the macro were skipped
1612 ;; over.
1613 (setq simple-ws-beg (point)))
1615 (goto-char cpp-beg)
1616 t)))
1618 ((/= (save-excursion
1619 (skip-chars-forward " \t\n\r\f\v" simple-ws-beg)
1620 (setq next-rung-pos (point)))
1621 simple-ws-beg)
1622 ;; Skipped over comments. Must put point at the end of
1623 ;; the simple ws at point since we might be after a line
1624 ;; comment or cpp directive that's been partially
1625 ;; narrowed out, and we can't risk marking the simple ws
1626 ;; at the end of it.
1627 (goto-char next-rung-pos)
1628 t)))
1630 ;; We've searched over a piece of non-white syntactic ws. See if this
1631 ;; can be cached.
1632 (setq next-rung-pos (point))
1633 (skip-chars-backward " \t\f\v")
1635 (if (or
1636 ;; Cache if we started either from a marked rung or from a
1637 ;; completely uncached position.
1638 rung-is-marked
1639 (not (get-text-property (1- simple-ws-beg) 'c-in-sws))
1641 ;; Cache if there's a marked rung in the encountered simple ws.
1642 (save-excursion
1643 (skip-chars-backward " \t\n\r\f\v")
1644 (text-property-any (point) (min (1+ next-rung-pos) (point-max))
1645 'c-is-sws t)))
1647 (progn
1648 (c-debug-sws-msg
1649 "c-backward-sws caching [%s..%s] - [%s..%s] (min %s)"
1650 (point) (1+ next-rung-pos)
1651 simple-ws-beg (min (1+ rung-pos) (point-max))
1652 (point-min))
1654 ;; Remove the properties for any nested ws that might be cached.
1655 ;; Only necessary for `c-is-sws' since `c-in-sws' will be set
1656 ;; anyway.
1657 (c-remove-is-sws (1+ next-rung-pos) simple-ws-beg)
1658 (unless (and rung-is-marked (= simple-ws-beg rung-pos))
1659 (let ((rung-end-pos (min (1+ rung-pos) (point-max))))
1660 (unless (get-text-property (1- rung-end-pos) 'c-is-sws)
1661 ;; Remove any `c-in-sws' property from the last char of
1662 ;; the rung before we mark it with `c-is-sws', so that we
1663 ;; won't connect with the remains of a broken "ladder".
1664 (c-remove-in-sws (1- rung-end-pos) rung-end-pos))
1665 (c-put-is-sws simple-ws-beg
1666 rung-end-pos)
1667 (setq rung-is-marked t)))
1668 (c-put-in-sws (setq simple-ws-beg (point)
1669 last-put-in-sws-pos simple-ws-beg)
1670 rung-pos)
1671 (c-put-is-sws (setq rung-pos simple-ws-beg)
1672 (1+ next-rung-pos)))
1674 (c-debug-sws-msg
1675 "c-backward-sws not caching [%s..%s] - [%s..%s] (min %s)"
1676 (point) (1+ next-rung-pos)
1677 simple-ws-beg (min (1+ rung-pos) (point-max))
1678 (point-min))
1679 (setq rung-pos next-rung-pos
1680 simple-ws-beg (point))
1683 ;; Make sure that the newly marked `c-in-sws' region doesn't connect to
1684 ;; another one before the point (which might occur when editing inside a
1685 ;; comment or macro).
1686 (when (eq last-put-in-sws-pos (point))
1687 (cond ((< (point-min) last-put-in-sws-pos)
1688 (c-debug-sws-msg
1689 "c-backward-sws clearing at %s for cache separation"
1690 (1- last-put-in-sws-pos))
1691 (c-remove-in-sws (1- last-put-in-sws-pos)
1692 last-put-in-sws-pos))
1693 ((> (point-min) 1)
1694 ;; If at bob and the buffer is narrowed, we have to clear the
1695 ;; character we're standing on instead since there might be a
1696 ;; `c-in-sws' before (point-min). In this case it's necessary
1697 ;; to clear both properties.
1698 (c-debug-sws-msg
1699 "c-backward-sws clearing thoroughly at %s for cache separation"
1700 last-put-in-sws-pos)
1701 (c-remove-is-and-in-sws last-put-in-sws-pos
1702 (1+ last-put-in-sws-pos)))))
1706 ;; A system for handling noteworthy parens before the point.
1708 (defvar c-state-cache nil)
1709 (make-variable-buffer-local 'c-state-cache)
1710 ;; The state cache used by `c-parse-state' to cut down the amount of
1711 ;; searching. It's the result from some earlier `c-parse-state' call.
1712 ;; The use of the cached info is more effective if the next
1713 ;; `c-parse-state' call is on a line close by the one the cached state
1714 ;; was made at; the cache can actually slow down a little if the
1715 ;; cached state was made very far back in the buffer. The cache is
1716 ;; most effective if `c-parse-state' is used on each line while moving
1717 ;; forward.
1719 (defvar c-state-cache-start 1)
1720 (make-variable-buffer-local 'c-state-cache-start)
1721 ;; This is (point-min) when `c-state-cache' was calculated, since a
1722 ;; change of narrowing is likely to affect the parens that are visible
1723 ;; before the point.
1725 (defsubst c-invalidate-state-cache (pos)
1726 ;; Invalidate all info on `c-state-cache' that applies to the buffer
1727 ;; at POS or higher. This is much like `c-whack-state-after', but
1728 ;; it never changes a paren pair element into an open paren element.
1729 ;; Doing that would mean that the new open paren wouldn't have the
1730 ;; required preceding paren pair element.
1732 ;; This function does not do any hidden buffer changes.
1733 (while (and c-state-cache
1734 (let ((elem (car c-state-cache)))
1735 (if (consp elem)
1736 (or (<= pos (car elem))
1737 (< pos (cdr elem)))
1738 (<= pos elem))))
1739 (setq c-state-cache (cdr c-state-cache))))
1741 (defun c-parse-state ()
1742 ;; Finds and records all noteworthy parens between some good point
1743 ;; earlier in the file and point. That good point is at least the
1744 ;; beginning of the top-level construct we are in, or the beginning
1745 ;; of the preceding top-level construct if we aren't in one.
1747 ;; The returned value is a list of the noteworthy parens with the
1748 ;; last one first. If an element in the list is an integer, it's
1749 ;; the position of an open paren which has not been closed before
1750 ;; the point. If an element is a cons, it gives the position of a
1751 ;; closed brace paren pair; the car is the start paren position and
1752 ;; the cdr is the position following the closing paren. Only the
1753 ;; last closed brace paren pair before each open paren is recorded,
1754 ;; and thus the state never contains two cons elements in
1755 ;; succession.
1757 ;; Currently no characters which are given paren syntax with the
1758 ;; syntax-table property are recorded, i.e. angle bracket arglist
1759 ;; parens are never present here. Note that this might change.
1761 ;; This function does not do any hidden buffer changes.
1763 (save-restriction
1764 (let* ((here (point))
1765 (c-macro-start (c-query-macro-start))
1766 (in-macro-start (or c-macro-start (point)))
1767 old-state last-pos pairs pos save-pos)
1768 (c-invalidate-state-cache (point))
1770 ;; If the minimum position has changed due to narrowing then we
1771 ;; have to fix the tail of `c-state-cache' accordingly.
1772 (unless (= c-state-cache-start (point-min))
1773 (if (> (point-min) c-state-cache-start)
1774 ;; If point-min has moved forward then we just need to cut
1775 ;; off a bit of the tail.
1776 (let ((ptr (cons nil c-state-cache)) elem)
1777 (while (and (setq elem (car-safe (cdr ptr)))
1778 (>= (if (consp elem) (car elem) elem)
1779 (point-min)))
1780 (setq ptr (cdr ptr)))
1781 (when (consp ptr)
1782 (if (eq (cdr ptr) c-state-cache)
1783 (setq c-state-cache nil)
1784 (setcdr ptr nil))))
1785 ;; If point-min has moved backward then we drop the state
1786 ;; completely. It's possible to do a better job here and
1787 ;; recalculate the top only.
1788 (setq c-state-cache nil))
1789 (setq c-state-cache-start (point-min)))
1791 ;; Get the latest position we know are directly inside the
1792 ;; closest containing paren of the cached state.
1793 (setq last-pos (and c-state-cache
1794 (if (consp (car c-state-cache))
1795 (cdr (car c-state-cache))
1796 (1+ (car c-state-cache)))))
1798 ;; Check if the found last-pos is in a macro. If it is, and
1799 ;; we're not in the same macro, we must discard everything on
1800 ;; c-state-cache that is inside the macro before using it.
1801 (when last-pos
1802 (save-excursion
1803 (goto-char last-pos)
1804 (when (and (c-beginning-of-macro)
1805 (/= (point) in-macro-start))
1806 (c-invalidate-state-cache (point))
1807 ;; Set last-pos again, just like above.
1808 (setq last-pos (and c-state-cache
1809 (if (consp (car c-state-cache))
1810 (cdr (car c-state-cache))
1811 (1+ (car c-state-cache))))))))
1813 (setq pos
1814 ;; Find the start position for the forward search. (Can't
1815 ;; search in the backward direction since point might be
1816 ;; in some kind of literal.)
1817 (or (when last-pos
1819 ;; There's a cached state with a containing paren. Pop
1820 ;; off the stale containing sexps from it by going
1821 ;; forward out of parens as far as possible.
1822 (narrow-to-region (point-min) here)
1823 (let (placeholder pair-beg)
1824 (while (and c-state-cache
1825 (setq placeholder
1826 (c-up-list-forward last-pos)))
1827 (setq last-pos placeholder)
1828 (if (consp (car c-state-cache))
1829 (setq pair-beg (car-safe (cdr c-state-cache))
1830 c-state-cache (cdr-safe (cdr c-state-cache)))
1831 (setq pair-beg (car c-state-cache)
1832 c-state-cache (cdr c-state-cache))))
1834 (when (and pair-beg (eq (char-after pair-beg) ?{))
1835 ;; The last paren pair we moved out from was a brace
1836 ;; pair. Modify the state to record this as a closed
1837 ;; pair now.
1838 (if (consp (car-safe c-state-cache))
1839 (setq c-state-cache (cdr c-state-cache)))
1840 (setq c-state-cache (cons (cons pair-beg last-pos)
1841 c-state-cache))))
1843 ;; Check if the preceding balanced paren is within a
1844 ;; macro; it should be ignored if we're outside the
1845 ;; macro. There's no need to check any further upwards;
1846 ;; if the macro contains an unbalanced opening paren then
1847 ;; we're smoked anyway.
1848 (when (and (<= (point) in-macro-start)
1849 (consp (car c-state-cache)))
1850 (save-excursion
1851 (goto-char (car (car c-state-cache)))
1852 (when (c-beginning-of-macro)
1853 (setq here (point)
1854 c-state-cache (cdr c-state-cache)))))
1856 (when c-state-cache
1857 (setq old-state c-state-cache)
1858 last-pos))
1860 (save-excursion
1861 ;; go back 2 bods, but ignore any bogus positions
1862 ;; returned by beginning-of-defun (i.e. open paren in
1863 ;; column zero)
1864 (goto-char here)
1865 (let ((cnt 2))
1866 (while (not (or (bobp) (zerop cnt)))
1867 (c-beginning-of-defun-1)
1868 (if (eq (char-after) ?\{)
1869 (setq cnt (1- cnt)))))
1870 (point))))
1872 (narrow-to-region (point-min) here)
1874 (while pos
1875 ;; Find the balanced brace pairs.
1876 (setq save-pos pos
1877 pairs nil)
1878 (while (and (setq last-pos (c-down-list-forward pos))
1879 (setq pos (c-up-list-forward last-pos)))
1880 (if (eq (char-before last-pos) ?{)
1881 (setq pairs (cons (cons last-pos pos) pairs))))
1883 ;; Should ignore any pairs that are in a macro, providing
1884 ;; we're not in the same one.
1885 (when (and pairs (< (car (car pairs)) in-macro-start))
1886 (while (and (save-excursion
1887 (goto-char (car (car pairs)))
1888 (c-beginning-of-macro))
1889 (setq pairs (cdr pairs)))))
1891 ;; Record the last brace pair.
1892 (when pairs
1893 (if (and (eq c-state-cache old-state)
1894 (consp (car-safe c-state-cache)))
1895 ;; There's a closed pair on the cached state but we've
1896 ;; found a later one, so remove it.
1897 (setq c-state-cache (cdr c-state-cache)))
1898 (setq pairs (car pairs))
1899 (setcar pairs (1- (car pairs)))
1900 (when (consp (car-safe c-state-cache))
1901 ;; There could already be a cons first in `c-state-cache'
1902 ;; if we've e.g. jumped over an unbalanced open paren in a
1903 ;; macro below.
1904 (setq c-state-cache (cdr c-state-cache)))
1905 (setq c-state-cache (cons pairs c-state-cache)))
1907 (if last-pos
1908 ;; Prepare to loop, but record the open paren only if it's
1909 ;; outside a macro or within the same macro as point, and
1910 ;; if it is a legitimate open paren and not some character
1911 ;; that got an open paren syntax-table property.
1912 (progn
1913 (setq pos last-pos)
1914 (if (and (or (>= last-pos in-macro-start)
1915 (save-excursion
1916 (goto-char last-pos)
1917 (not (c-beginning-of-macro))))
1918 ;; Check for known types of parens that we want
1919 ;; to record. The syntax table is not to be
1920 ;; trusted here since the caller might be using
1921 ;; e.g. `c++-template-syntax-table'.
1922 (memq (char-before last-pos) '(?{ ?\( ?\[)))
1923 (setq c-state-cache (cons (1- last-pos) c-state-cache))))
1925 (if (setq last-pos (c-up-list-forward pos))
1926 ;; Found a close paren without a corresponding opening
1927 ;; one. Maybe we didn't go back far enough, so try to
1928 ;; scan backward for the start paren and then start over.
1929 (progn
1930 (setq pos (c-up-list-backward pos)
1931 c-state-cache nil)
1932 (when (or (not pos)
1933 ;; Emacs (up to at least 21.2) can get confused by
1934 ;; open parens in column zero inside comments: The
1935 ;; sexp functions can then misbehave and bring us
1936 ;; back to the same point again. Check this so that
1937 ;; we don't get an infinite loop.
1938 (>= pos save-pos))
1939 (setq pos last-pos
1940 c-parsing-error
1941 (format "Unbalanced close paren at line %d"
1942 (1+ (count-lines (point-min)
1943 (c-point 'bol last-pos)))))))
1944 (setq pos nil))))
1946 c-state-cache)))
1948 ;; Debug tool to catch cache inconsistencies.
1949 (defvar c-debug-parse-state nil)
1950 (unless (fboundp 'c-real-parse-state)
1951 (fset 'c-real-parse-state (symbol-function 'c-parse-state)))
1952 (cc-bytecomp-defun c-real-parse-state)
1953 (defun c-debug-parse-state ()
1954 (let ((res1 (c-real-parse-state)) res2)
1955 (let ((c-state-cache nil))
1956 (setq res2 (c-real-parse-state)))
1957 (unless (equal res1 res2)
1958 (error "c-parse-state inconsistency: using cache: %s, from scratch: %s"
1959 res1 res2))
1960 res1))
1961 (defun c-toggle-parse-state-debug (&optional arg)
1962 (interactive "P")
1963 (setq c-debug-parse-state (c-calculate-state arg c-debug-parse-state))
1964 (fset 'c-parse-state (symbol-function (if c-debug-parse-state
1965 'c-debug-parse-state
1966 'c-real-parse-state)))
1967 (c-keep-region-active))
1969 (defun c-whack-state-before (bufpos paren-state)
1970 ;; Whack off any state information from PAREN-STATE which lies
1971 ;; before BUFPOS. Not destructive on PAREN-STATE.
1973 ;; This function does not do any hidden buffer changes.
1974 (let* ((newstate (list nil))
1975 (ptr newstate)
1976 car)
1977 (while paren-state
1978 (setq car (car paren-state)
1979 paren-state (cdr paren-state))
1980 (if (< (if (consp car) (car car) car) bufpos)
1981 (setq paren-state nil)
1982 (setcdr ptr (list car))
1983 (setq ptr (cdr ptr))))
1984 (cdr newstate)))
1986 (defun c-whack-state-after (bufpos paren-state)
1987 ;; Whack off any state information from PAREN-STATE which lies at or
1988 ;; after BUFPOS. Not destructive on PAREN-STATE.
1990 ;; This function does not do any hidden buffer changes.
1991 (catch 'done
1992 (while paren-state
1993 (let ((car (car paren-state)))
1994 (if (consp car)
1995 ;; just check the car, because in a balanced brace
1996 ;; expression, it must be impossible for the corresponding
1997 ;; close brace to be before point, but the open brace to
1998 ;; be after.
1999 (if (<= bufpos (car car))
2000 nil ; whack it off
2001 (if (< bufpos (cdr car))
2002 ;; its possible that the open brace is before
2003 ;; bufpos, but the close brace is after. In that
2004 ;; case, convert this to a non-cons element. The
2005 ;; rest of the state is before bufpos, so we're
2006 ;; done.
2007 (throw 'done (cons (car car) (cdr paren-state)))
2008 ;; we know that both the open and close braces are
2009 ;; before bufpos, so we also know that everything else
2010 ;; on state is before bufpos.
2011 (throw 'done paren-state)))
2012 (if (<= bufpos car)
2013 nil ; whack it off
2014 ;; it's before bufpos, so everything else should too.
2015 (throw 'done paren-state)))
2016 (setq paren-state (cdr paren-state)))
2017 nil)))
2019 (defun c-most-enclosing-brace (paren-state &optional bufpos)
2020 ;; Return the bufpos of the innermost enclosing open paren before
2021 ;; bufpos that hasn't been narrowed out, or nil if none was found.
2023 ;; This function does not do any hidden buffer changes.
2024 (let (enclosingp)
2025 (or bufpos (setq bufpos 134217727))
2026 (while paren-state
2027 (setq enclosingp (car paren-state)
2028 paren-state (cdr paren-state))
2029 (if (or (consp enclosingp)
2030 (>= enclosingp bufpos))
2031 (setq enclosingp nil)
2032 (if (< enclosingp (point-min))
2033 (setq enclosingp nil))
2034 (setq paren-state nil)))
2035 enclosingp))
2037 (defun c-least-enclosing-brace (paren-state &optional bufpos)
2038 ;; Return the bufpos of the outermost enclosing open paren before
2039 ;; bufpos that hasn't been narrowed out, or nil if none was found.
2041 ;; This function does not do any hidden buffer changes.
2042 (let (pos elem)
2043 (or bufpos (setq bufpos 134217727))
2044 (while paren-state
2045 (setq elem (car paren-state)
2046 paren-state (cdr paren-state))
2047 (unless (or (consp elem)
2048 (>= elem bufpos))
2049 (if (>= elem (point-min))
2050 (setq pos elem))))
2051 pos))
2053 (defun c-safe-position (bufpos paren-state)
2054 ;; Return the closest known safe position higher up than BUFPOS, or
2055 ;; nil if PAREN-STATE doesn't contain one. Return nil if BUFPOS is
2056 ;; nil, which is useful to find the closest limit before a given
2057 ;; limit that might be nil.
2059 ;; This function does not do any hidden buffer changes.
2060 (when bufpos
2061 (let (elem)
2062 (catch 'done
2063 (while paren-state
2064 (setq elem (car paren-state))
2065 (if (consp elem)
2066 (cond ((< (cdr elem) bufpos)
2067 (throw 'done (cdr elem)))
2068 ((< (car elem) bufpos)
2069 ;; See below.
2070 (throw 'done (min (1+ (car elem)) bufpos))))
2071 (if (< elem bufpos)
2072 ;; elem is the position at and not after the opening paren, so
2073 ;; we can go forward one more step unless it's equal to
2074 ;; bufpos. This is useful in some cases avoid an extra paren
2075 ;; level between the safe position and bufpos.
2076 (throw 'done (min (1+ elem) bufpos))))
2077 (setq paren-state (cdr paren-state)))))))
2079 (defun c-beginning-of-syntax ()
2080 ;; This is used for `font-lock-beginning-of-syntax-function'. It
2081 ;; goes to the closest previous point that is known to be outside
2082 ;; any string literal or comment. `c-state-cache' is used if it has
2083 ;; a position in the vicinity.
2084 (let* ((paren-state c-state-cache)
2085 elem
2087 (pos (catch 'done
2088 ;; Note: Similar code in `c-safe-position'. The
2089 ;; difference is that we accept a safe position at
2090 ;; the point and don't bother to go forward past open
2091 ;; parens.
2092 (while paren-state
2093 (setq elem (car paren-state))
2094 (if (consp elem)
2095 (cond ((<= (cdr elem) (point))
2096 (throw 'done (cdr elem)))
2097 ((<= (car elem) (point))
2098 (throw 'done (car elem))))
2099 (if (<= elem (point))
2100 (throw 'done elem)))
2101 (setq paren-state (cdr paren-state)))
2102 (point-min))))
2104 (if (> pos (- (point) 4000))
2105 (goto-char pos)
2106 ;; The position is far back. Try `c-beginning-of-defun-1'
2107 ;; (although we can't be entirely sure it will go to a position
2108 ;; outside a comment or string in current emacsen). FIXME:
2109 ;; Consult `syntax-ppss' here.
2110 (c-beginning-of-defun-1)
2111 (if (< (point) pos)
2112 (goto-char pos)))))
2115 ;; Tools for scanning identifiers and other tokens.
2117 (defun c-on-identifier ()
2118 "Return non-nil if the point is on or directly after an identifier.
2119 Keywords are recognized and not considered identifiers. If an
2120 identifier is detected, the returned value is its starting position.
2121 If an identifier both starts and stops at the point \(can only happen
2122 in Pike) then the point for the preceding one is returned.
2124 This function does not do any hidden buffer changes."
2126 (save-excursion
2127 (if (zerop (skip-syntax-backward "w_"))
2129 (when (c-major-mode-is 'pike-mode)
2130 ;; Handle the `<operator> syntax in Pike.
2131 (let ((pos (point)))
2132 (skip-chars-backward "-!%&*+/<=>^|~[]()")
2133 (and (if (< (skip-chars-backward "`") 0)
2135 (goto-char pos)
2136 (eq (char-after) ?\`))
2137 (looking-at c-symbol-key)
2138 (>= (match-end 0) pos)
2139 (point))))
2141 (and (not (looking-at c-keywords-regexp))
2142 (point)))))
2144 (defsubst c-simple-skip-symbol-backward ()
2145 ;; If the point is at the end of a symbol then skip backward to the
2146 ;; beginning of it. Don't move otherwise. Return non-nil if point
2147 ;; moved.
2148 (or (< (skip-syntax-backward "w_") 0)
2149 (and (c-major-mode-is 'pike-mode)
2150 ;; Handle the `<operator> syntax in Pike.
2151 (let ((pos (point)))
2152 (if (and (< (skip-chars-backward "-!%&*+/<=>^|~[]()") 0)
2153 (< (skip-chars-backward "`") 0)
2154 (looking-at c-symbol-key)
2155 (>= (match-end 0) pos))
2157 (goto-char pos)
2158 nil)))))
2160 (defsubst c-beginning-of-current-token (&optional back-limit)
2161 ;; Move to the beginning of the current token. Do not move if not
2162 ;; in the middle of one. BACK-LIMIT may be used to bound the
2163 ;; backward search; if given it's assumed to be at the boundary
2164 ;; between two tokens.
2165 (if (looking-at "\\w\\|\\s_")
2166 (skip-syntax-backward "w_" back-limit)
2167 (let ((start (point)))
2168 (when (< (skip-syntax-backward ".()" back-limit) 0)
2169 (while (let ((pos (or (and (looking-at c-nonsymbol-token-regexp)
2170 (match-end 0))
2171 ;; `c-nonsymbol-token-regexp' should always match
2172 ;; since we've skipped backward over punctuator
2173 ;; or paren syntax, but consume one char in case
2174 ;; it doesn't so that we don't leave point before
2175 ;; some earlier incorrect token.
2176 (1+ (point)))))
2177 (if (<= pos start)
2178 (goto-char pos))
2179 (< pos start)))))))
2181 (defun c-end-of-current-token (&optional back-limit)
2182 ;; Move to the end of the current token. Do not move if not in the
2183 ;; middle of one. BACK-LIMIT may be used to bound the backward
2184 ;; search; if given it's assumed to be at the boundary between two
2185 ;; tokens. Return non-nil if the point is moved, nil otherwise.
2186 (let ((start (point)))
2187 (cond ((< (skip-syntax-backward "w_" (1- start)) 0)
2188 (skip-syntax-forward "w_"))
2189 ((< (skip-syntax-backward ".()" back-limit) 0)
2190 (while (progn
2191 (if (looking-at c-nonsymbol-token-regexp)
2192 (goto-char (match-end 0))
2193 ;; `c-nonsymbol-token-regexp' should always match since
2194 ;; we've skipped backward over punctuator or paren
2195 ;; syntax, but move forward in case it doesn't so that
2196 ;; we don't leave point earlier than we started with.
2197 (forward-char))
2198 (< (point) start)))))
2199 (> (point) start)))
2201 (defconst c-jump-syntax-balanced
2202 (if (memq 'gen-string-delim c-emacs-features)
2203 "\\w\\|\\s_\\|\\s\(\\|\\s\)\\|\\s\"\\|\\s|"
2204 "\\w\\|\\s_\\|\\s\(\\|\\s\)\\|\\s\""))
2206 (defconst c-jump-syntax-unbalanced
2207 (if (memq 'gen-string-delim c-emacs-features)
2208 "\\w\\|\\s_\\|\\s\"\\|\\s|"
2209 "\\w\\|\\s_\\|\\s\""))
2211 (defun c-forward-token-2 (&optional count balanced limit)
2212 "Move forward by tokens.
2213 A token is defined as all symbols and identifiers which aren't
2214 syntactic whitespace \(note that multicharacter tokens like \"==\" are
2215 treated properly). Point is always either left at the beginning of a
2216 token or not moved at all. COUNT specifies the number of tokens to
2217 move; a negative COUNT moves in the opposite direction. A COUNT of 0
2218 moves to the next token beginning only if not already at one. If
2219 BALANCED is true, move over balanced parens, otherwise move into them.
2220 Also, if BALANCED is true, never move out of an enclosing paren.
2222 LIMIT sets the limit for the movement and defaults to the point limit.
2223 The case when LIMIT is set in the middle of a token, comment or macro
2224 is handled correctly, i.e. the point won't be left there.
2226 Return the number of tokens left to move \(positive or negative). If
2227 BALANCED is true, a move over a balanced paren counts as one. Note
2228 that if COUNT is 0 and no appropriate token beginning is found, 1 will
2229 be returned. Thus, a return value of 0 guarantees that point is at
2230 the requested position and a return value less \(without signs) than
2231 COUNT guarantees that point is at the beginning of some token."
2233 (or count (setq count 1))
2234 (if (< count 0)
2235 (- (c-backward-token-2 (- count) balanced limit))
2237 (let ((jump-syntax (if balanced
2238 c-jump-syntax-balanced
2239 c-jump-syntax-unbalanced))
2240 (last (point))
2241 (prev (point)))
2243 (if (zerop count)
2244 ;; If count is zero we should jump if in the middle of a token.
2245 (c-end-of-current-token))
2247 (save-restriction
2248 (if limit (narrow-to-region (point-min) limit))
2249 (if (/= (point)
2250 (progn (c-forward-syntactic-ws) (point)))
2251 ;; Skip whitespace. Count this as a move if we did in
2252 ;; fact move.
2253 (setq count (max (1- count) 0)))
2255 (if (eobp)
2256 ;; Moved out of bounds. Make sure the returned count isn't zero.
2257 (progn
2258 (if (zerop count) (setq count 1))
2259 (goto-char last))
2261 ;; Use `condition-case' to avoid having the limit tests
2262 ;; inside the loop.
2263 (condition-case nil
2264 (while (and
2265 (> count 0)
2266 (progn
2267 (setq last (point))
2268 (cond ((looking-at jump-syntax)
2269 (goto-char (scan-sexps (point) 1))
2271 ((looking-at c-nonsymbol-token-regexp)
2272 (goto-char (match-end 0))
2274 ;; `c-nonsymbol-token-regexp' above should always
2275 ;; match if there are correct tokens. Try to
2276 ;; widen to see if the limit was set in the
2277 ;; middle of one, else fall back to treating
2278 ;; the offending thing as a one character token.
2279 ((and limit
2280 (save-restriction
2281 (widen)
2282 (looking-at c-nonsymbol-token-regexp)))
2283 nil)
2285 (forward-char)
2286 t))))
2287 (c-forward-syntactic-ws)
2288 (setq prev last
2289 count (1- count)))
2290 (error (goto-char last)))
2292 (when (eobp)
2293 (goto-char prev)
2294 (setq count (1+ count)))))
2296 count)))
2298 (defun c-backward-token-2 (&optional count balanced limit)
2299 "Move backward by tokens.
2300 See `c-forward-token-2' for details."
2302 (or count (setq count 1))
2303 (if (< count 0)
2304 (- (c-forward-token-2 (- count) balanced limit))
2306 (or limit (setq limit (point-min)))
2307 (let ((jump-syntax (if balanced
2308 c-jump-syntax-balanced
2309 c-jump-syntax-unbalanced))
2310 (last (point)))
2312 (if (zerop count)
2313 ;; The count is zero so try to skip to the beginning of the
2314 ;; current token.
2315 (if (> (point)
2316 (progn (c-beginning-of-current-token) (point)))
2317 (if (< (point) limit)
2318 ;; The limit is inside the same token, so return 1.
2319 (setq count 1))
2321 ;; We're not in the middle of a token. If there's
2322 ;; whitespace after the point then we must move backward,
2323 ;; so set count to 1 in that case.
2324 (and (looking-at c-syntactic-ws-start)
2325 ;; If we're looking at a '#' that might start a cpp
2326 ;; directive then we have to do a more elaborate check.
2327 (or (/= (char-after) ?#)
2328 (not c-opt-cpp-prefix)
2329 (save-excursion
2330 (and (= (point)
2331 (progn (beginning-of-line)
2332 (looking-at "[ \t]*")
2333 (match-end 0)))
2334 (or (bobp)
2335 (progn (backward-char)
2336 (not (eq (char-before) ?\\)))))))
2337 (setq count 1))))
2339 ;; Use `condition-case' to avoid having to check for buffer
2340 ;; limits in `backward-char', `scan-sexps' and `goto-char' below.
2341 (condition-case nil
2342 (while (and
2343 (> count 0)
2344 (progn
2345 (c-backward-syntactic-ws)
2346 (backward-char)
2347 (if (looking-at jump-syntax)
2348 (goto-char (scan-sexps (1+ (point)) -1))
2349 ;; This can be very inefficient if there's a long
2350 ;; sequence of operator tokens without any separation.
2351 ;; That doesn't happen in practice, anyway.
2352 (c-beginning-of-current-token))
2353 (>= (point) limit)))
2354 (setq last (point)
2355 count (1- count)))
2356 (error (goto-char last)))
2358 (if (< (point) limit)
2359 (goto-char last))
2361 count)))
2363 (defun c-forward-token-1 (&optional count balanced limit)
2364 "Like `c-forward-token-2' but doesn't treat multicharacter operator
2365 tokens like \"==\" as single tokens, i.e. all sequences of symbol
2366 characters are jumped over character by character. This function is
2367 for compatibility only; it's only a wrapper over `c-forward-token-2'."
2368 (let ((c-nonsymbol-token-regexp "\\s.\\|\\s\(\\|\\s\)"))
2369 (c-forward-token-2 count balanced limit)))
2371 (defun c-backward-token-1 (&optional count balanced limit)
2372 "Like `c-backward-token-2' but doesn't treat multicharacter operator
2373 tokens like \"==\" as single tokens, i.e. all sequences of symbol
2374 characters are jumped over character by character. This function is
2375 for compatibility only; it's only a wrapper over `c-backward-token-2'."
2376 (let ((c-nonsymbol-token-regexp "\\s.\\|\\s\(\\|\\s\)"))
2377 (c-backward-token-2 count balanced limit)))
2380 ;; Tools for doing searches restricted to syntactically relevant text.
2382 (defun c-syntactic-re-search-forward (regexp &optional bound noerror
2383 paren-level not-inside-token
2384 lookbehind-submatch)
2385 "Like `re-search-forward', but only report matches that are found
2386 in syntactically significant text. I.e. matches in comments, macros
2387 or string literals are ignored. The start point is assumed to be
2388 outside any comment, macro or string literal, or else the content of
2389 that region is taken as syntactically significant text.
2391 If PAREN-LEVEL is non-nil, an additional restriction is added to
2392 ignore matches in nested paren sexps. The search will also not go
2393 outside the current list sexp, which has the effect that if the point
2394 should be moved to BOUND when no match is found \(i.e. NOERROR is
2395 neither nil nor t), then it will be at the closing paren if the end of
2396 the current list sexp is encountered first.
2398 If NOT-INSIDE-TOKEN is non-nil, matches in the middle of tokens are
2399 ignored. Things like multicharacter operators and special symbols
2400 \(e.g. \"`()\" in Pike) are handled but currently not floating point
2401 constants.
2403 If LOOKBEHIND-SUBMATCH is non-nil, it's taken as a number of a
2404 subexpression in REGEXP. The end of that submatch is used as the
2405 position to check for syntactic significance. If LOOKBEHIND-SUBMATCH
2406 isn't used or if that subexpression didn't match then the start
2407 position of the whole match is used instead. The \"look behind\"
2408 subexpression is never tested before the starting position, so it
2409 might be a good idea to include \\=\\= as a match alternative in it.
2411 Optimization note: Matches might be missed if the \"look behind\"
2412 subexpression can match the end of nonwhite syntactic whitespace,
2413 i.e. the end of comments or cpp directives. This since the function
2414 skips over such things before resuming the search. It's on the other
2415 hand not safe to assume that the \"look behind\" subexpression never
2416 matches syntactic whitespace.
2418 Bug: Unbalanced parens inside cpp directives are currently not handled
2419 correctly \(i.e. they don't get ignored as they should) when
2420 PAREN-LEVEL is set."
2422 (or bound (setq bound (point-max)))
2423 (if paren-level (setq paren-level -1))
2425 ;;(message "c-syntactic-re-search-forward %s %s %S" (point) bound regexp)
2427 (let ((start (point))
2429 ;; Start position for the last search.
2430 search-pos
2431 ;; The `parse-partial-sexp' state between the start position
2432 ;; and the point.
2433 state
2434 ;; The current position after the last state update. The next
2435 ;; `parse-partial-sexp' continues from here.
2436 (state-pos (point))
2437 ;; The position at which to check the state and the state
2438 ;; there. This is separate from `state-pos' since we might
2439 ;; need to back up before doing the next search round.
2440 check-pos check-state
2441 ;; Last position known to end a token.
2442 (last-token-end-pos (point-min))
2443 ;; Set when a valid match is found.
2444 found)
2446 (condition-case err
2447 (while
2448 (and
2449 (progn
2450 (setq search-pos (point))
2451 (re-search-forward regexp bound noerror))
2453 (progn
2454 (setq state (parse-partial-sexp
2455 state-pos (match-beginning 0) paren-level nil state)
2456 state-pos (point))
2457 (if (setq check-pos (and lookbehind-submatch
2458 (or (not paren-level)
2459 (>= (car state) 0))
2460 (match-end lookbehind-submatch)))
2461 (setq check-state (parse-partial-sexp
2462 state-pos check-pos paren-level nil state))
2463 (setq check-pos state-pos
2464 check-state state))
2466 ;; NOTE: If we got a look behind subexpression and get
2467 ;; an insignificant match in something that isn't
2468 ;; syntactic whitespace (i.e. strings or in nested
2469 ;; parentheses), then we can never skip more than a
2470 ;; single character from the match start position
2471 ;; (i.e. `state-pos' here) before continuing the
2472 ;; search. That since the look behind subexpression
2473 ;; might match the end of the insignificant region in
2474 ;; the next search.
2476 (cond
2477 ((elt check-state 7)
2478 ;; Match inside a line comment. Skip to eol. Use
2479 ;; `re-search-forward' instead of `skip-chars-forward' to get
2480 ;; the right bound behavior.
2481 (re-search-forward "[\n\r]" bound noerror))
2483 ((elt check-state 4)
2484 ;; Match inside a block comment. Skip to the '*/'.
2485 (search-forward "*/" bound noerror))
2487 ((and (not (elt check-state 5))
2488 (eq (char-before check-pos) ?/)
2489 (not (c-get-char-property (1- check-pos) 'syntax-table))
2490 (memq (char-after check-pos) '(?/ ?*)))
2491 ;; Match in the middle of the opener of a block or line
2492 ;; comment.
2493 (if (= (char-after check-pos) ?/)
2494 (re-search-forward "[\n\r]" bound noerror)
2495 (search-forward "*/" bound noerror)))
2497 ;; The last `parse-partial-sexp' above might have
2498 ;; stopped short of the real check position if the end
2499 ;; of the current sexp was encountered in paren-level
2500 ;; mode. The checks above are always false in that
2501 ;; case, and since they can do better skipping in
2502 ;; lookbehind-submatch mode, we do them before
2503 ;; checking the paren level.
2505 ((and paren-level
2506 (/= (setq tmp (car check-state)) 0))
2507 ;; Check the paren level first since we're short of the
2508 ;; syntactic checking position if the end of the
2509 ;; current sexp was encountered by `parse-partial-sexp'.
2510 (if (> tmp 0)
2512 ;; Inside a nested paren sexp.
2513 (if lookbehind-submatch
2514 ;; See the NOTE above.
2515 (progn (goto-char state-pos) t)
2516 ;; Skip out of the paren quickly.
2517 (setq state (parse-partial-sexp state-pos bound 0 nil state)
2518 state-pos (point)))
2520 ;; Have exited the current paren sexp.
2521 (if noerror
2522 (progn
2523 ;; The last `parse-partial-sexp' call above
2524 ;; has left us just after the closing paren
2525 ;; in this case, so we can modify the bound
2526 ;; to leave the point at the right position
2527 ;; upon return.
2528 (setq bound (1- (point)))
2529 nil)
2530 (signal 'search-failed (list regexp)))))
2532 ((setq tmp (elt check-state 3))
2533 ;; Match inside a string.
2534 (if (or lookbehind-submatch
2535 (not (integerp tmp)))
2536 ;; See the NOTE above.
2537 (progn (goto-char state-pos) t)
2538 ;; Skip to the end of the string before continuing.
2539 (let ((ender (make-string 1 tmp)) (continue t))
2540 (while (if (search-forward ender bound noerror)
2541 (progn
2542 (setq state (parse-partial-sexp
2543 state-pos (point) nil nil state)
2544 state-pos (point))
2545 (elt state 3))
2546 (setq continue nil)))
2547 continue)))
2549 ((save-excursion
2550 (save-match-data
2551 (c-beginning-of-macro start)))
2552 ;; Match inside a macro. Skip to the end of it.
2553 (c-end-of-macro)
2554 (cond ((<= (point) bound) t)
2555 (noerror nil)
2556 (t (signal 'search-failed (list regexp)))))
2558 ((and not-inside-token
2559 (or (< check-pos last-token-end-pos)
2560 (< check-pos
2561 (save-excursion
2562 (goto-char check-pos)
2563 (save-match-data
2564 (c-end-of-current-token last-token-end-pos))
2565 (setq last-token-end-pos (point))))))
2566 ;; Inside a token.
2567 (if lookbehind-submatch
2568 ;; See the NOTE above.
2569 (goto-char state-pos)
2570 (goto-char (min last-token-end-pos bound))))
2573 ;; A real match.
2574 (setq found t)
2575 nil)))
2577 ;; Should loop to search again, but take care to avoid
2578 ;; looping on the same spot.
2579 (or (/= search-pos (point))
2580 (if (= (point) bound)
2581 (if noerror
2583 (signal 'search-failed (list regexp)))
2584 (forward-char)
2585 t))))
2587 (error
2588 (goto-char start)
2589 (signal (car err) (cdr err))))
2591 ;;(message "c-syntactic-re-search-forward done %s" (or (match-end 0) (point)))
2593 (if found
2594 (progn
2595 (goto-char (match-end 0))
2596 (match-end 0))
2598 ;; Search failed. Set point as appropriate.
2599 (if (eq noerror t)
2600 (goto-char start)
2601 (goto-char bound))
2602 nil)))
2604 (defun c-syntactic-skip-backward (skip-chars &optional limit)
2605 "Like `skip-chars-backward' but only look at syntactically relevant chars,
2606 i.e. don't stop at positions inside syntactic whitespace or string
2607 literals. Preprocessor directives are also ignored, with the exception
2608 of the one that the point starts within, if any. If LIMIT is given,
2609 it's assumed to be at a syntactically relevant position."
2611 (let ((start (point))
2612 ;; A list of syntactically relevant positions in descending
2613 ;; order. It's used to avoid scanning repeatedly over
2614 ;; potentially large regions with `parse-partial-sexp' to verify
2615 ;; each position.
2616 safe-pos-list
2617 ;; The result from `c-beginning-of-macro' at the start position or the
2618 ;; start position itself if it isn't within a macro. Evaluated on
2619 ;; demand.
2620 start-macro-beg)
2622 (while (progn
2623 (while (and
2624 (< (skip-chars-backward skip-chars limit) 0)
2626 ;; Use `parse-partial-sexp' from a safe position down to
2627 ;; the point to check if it's outside comments and
2628 ;; strings.
2629 (let ((pos (point)) safe-pos state)
2630 ;; Pick a safe position as close to the point as
2631 ;; possible.
2633 ;; FIXME: Consult `syntax-ppss' here if our
2634 ;; cache doesn't give a good position.
2635 (while (and safe-pos-list
2636 (> (car safe-pos-list) (point)))
2637 (setq safe-pos-list (cdr safe-pos-list)))
2638 (unless (setq safe-pos (car-safe safe-pos-list))
2639 (setq safe-pos (max (or (c-safe-position
2640 (point) (or c-state-cache
2641 (c-parse-state)))
2643 (point-min))
2644 safe-pos-list (list safe-pos)))
2646 (while (progn
2647 (setq state (parse-partial-sexp
2648 safe-pos pos 0))
2649 (< (point) pos))
2650 ;; Cache positions along the way to use if we have to
2651 ;; back up more. Every closing paren on the same
2652 ;; level seems like fairly well spaced positions.
2653 (setq safe-pos (point)
2654 safe-pos-list (cons safe-pos safe-pos-list)))
2656 (cond
2657 ((or (elt state 3) (elt state 4))
2658 ;; Inside string or comment. Continue search at the
2659 ;; beginning of it.
2660 (if (setq pos (nth 8 state))
2661 ;; It's an emacs where `parse-partial-sexp'
2662 ;; supplies the starting position.
2663 (goto-char pos)
2664 (goto-char (car (c-literal-limits safe-pos))))
2667 ((c-beginning-of-macro limit)
2668 ;; Inside a macro.
2669 (if (< (point)
2670 (or start-macro-beg
2671 (setq start-macro-beg
2672 (save-excursion
2673 (goto-char start)
2674 (c-beginning-of-macro limit)
2675 (point)))))
2677 ;; It's inside the same macro we started in so it's
2678 ;; a relevant match.
2679 (goto-char pos)
2680 nil))))))
2682 (> (point)
2683 (progn
2684 ;; Skip syntactic ws afterwards so that we don't stop at the
2685 ;; end of a comment if `skip-chars' is something like "^/".
2686 (c-backward-syntactic-ws)
2687 (point)))))
2689 (- (point) start)))
2692 ;; Tools for handling comments and string literals.
2694 (defun c-slow-in-literal (&optional lim detect-cpp)
2695 "Return the type of literal point is in, if any.
2696 The return value is `c' if in a C-style comment, `c++' if in a C++
2697 style comment, `string' if in a string literal, `pound' if DETECT-CPP
2698 is non-nil and in a preprocessor line, or nil if somewhere else.
2699 Optional LIM is used as the backward limit of the search. If omitted,
2700 or nil, `c-beginning-of-defun' is used.
2702 The last point calculated is cached if the cache is enabled, i.e. if
2703 `c-in-literal-cache' is bound to a two element vector.
2705 This function does not do any hidden buffer changes."
2706 (if (and (vectorp c-in-literal-cache)
2707 (= (point) (aref c-in-literal-cache 0)))
2708 (aref c-in-literal-cache 1)
2709 (let ((rtn (save-excursion
2710 (let* ((pos (point))
2711 (lim (or lim (progn
2712 (c-beginning-of-syntax)
2713 (point))))
2714 (state (parse-partial-sexp lim pos)))
2715 (cond
2716 ((elt state 3) 'string)
2717 ((elt state 4) (if (elt state 7) 'c++ 'c))
2718 ((and detect-cpp (c-beginning-of-macro lim)) 'pound)
2719 (t nil))))))
2720 ;; cache this result if the cache is enabled
2721 (if (not c-in-literal-cache)
2722 (setq c-in-literal-cache (vector (point) rtn)))
2723 rtn)))
2725 ;; XEmacs has a built-in function that should make this much quicker.
2726 ;; I don't think we even need the cache, which makes our lives more
2727 ;; complicated anyway. In this case, lim is only used to detect
2728 ;; cpp directives.
2730 ;; Note that there is a bug in Xemacs's buffer-syntactic-context when used in
2731 ;; conjunction with syntax-table-properties. The bug is present in, e.g.,
2732 ;; Xemacs 21.4.4. It manifested itself thus:
2734 ;; Starting with an empty AWK Mode buffer, type
2735 ;; /regexp/ {<C-j>
2736 ;; Point gets wrongly left at column 0, rather than being indented to tab-width.
2738 ;; AWK Mode is designed such that when the first / is typed, it gets the
2739 ;; syntax-table property "string fence". When the second / is typed, BOTH /s
2740 ;; are given the s-t property "string". However, buffer-syntactic-context
2741 ;; fails to take account of the change of the s-t property on the opening / to
2742 ;; "string", and reports that the { is within a string started by the second /.
2744 ;; The workaround for this is for the AWK Mode initialisation to switch the
2745 ;; defalias for c-in-literal to c-slow-in-literal. This will slow down other
2746 ;; cc-modes in Xemacs whenever an awk-buffer has been initialised.
2748 ;; (Alan Mackenzie, 2003/4/30).
2750 (defun c-fast-in-literal (&optional lim detect-cpp)
2751 (let ((context (buffer-syntactic-context)))
2752 (cond
2753 ((eq context 'string) 'string)
2754 ((eq context 'comment) 'c++)
2755 ((eq context 'block-comment) 'c)
2756 ((and detect-cpp (save-excursion (c-beginning-of-macro lim))) 'pound))))
2758 (defalias 'c-in-literal
2759 (if (fboundp 'buffer-syntactic-context)
2760 'c-fast-in-literal ; XEmacs
2761 'c-slow-in-literal)) ; GNU Emacs
2763 ;; The defalias above isn't enough to shut up the byte compiler.
2764 (cc-bytecomp-defun c-in-literal)
2766 (defun c-literal-limits (&optional lim near not-in-delimiter)
2767 "Return a cons of the beginning and end positions of the comment or
2768 string surrounding point (including both delimiters), or nil if point
2769 isn't in one. If LIM is non-nil, it's used as the \"safe\" position
2770 to start parsing from. If NEAR is non-nil, then the limits of any
2771 literal next to point is returned. \"Next to\" means there's only
2772 spaces and tabs between point and the literal. The search for such a
2773 literal is done first in forward direction. If NOT-IN-DELIMITER is
2774 non-nil, the case when point is inside a starting delimiter won't be
2775 recognized. This only has effect for comments, which have starting
2776 delimiters with more than one character.
2778 This function does not do any hidden buffer changes."
2780 (save-excursion
2781 (let* ((pos (point))
2782 (lim (or lim (progn
2783 (c-beginning-of-syntax)
2784 (point))))
2785 (state (parse-partial-sexp lim pos)))
2787 (cond ((elt state 3)
2788 ;; String. Search backward for the start.
2789 (while (elt state 3)
2790 (search-backward (make-string 1 (elt state 3)))
2791 (setq state (parse-partial-sexp lim (point))))
2792 (cons (point) (or (c-safe (c-forward-sexp 1) (point))
2793 (point-max))))
2795 ((elt state 7)
2796 ;; Line comment. Search from bol for the comment starter.
2797 (beginning-of-line)
2798 (setq state (parse-partial-sexp lim (point))
2799 lim (point))
2800 (while (not (elt state 7))
2801 (search-forward "//") ; Should never fail.
2802 (setq state (parse-partial-sexp
2803 lim (point) nil nil state)
2804 lim (point)))
2805 (backward-char 2)
2806 (cons (point) (progn (c-forward-single-comment) (point))))
2808 ((elt state 4)
2809 ;; Block comment. Search backward for the comment starter.
2810 (while (elt state 4)
2811 (search-backward "/*") ; Should never fail.
2812 (setq state (parse-partial-sexp lim (point))))
2813 (cons (point) (progn (c-forward-single-comment) (point))))
2815 ((and (not not-in-delimiter)
2816 (not (elt state 5))
2817 (eq (char-before) ?/)
2818 (looking-at "[/*]"))
2819 ;; We're standing in a comment starter.
2820 (backward-char 1)
2821 (cons (point) (progn (c-forward-single-comment) (point))))
2823 (near
2824 (goto-char pos)
2826 ;; Search forward for a literal.
2827 (skip-chars-forward " \t")
2829 (cond
2830 ((looking-at c-string-limit-regexp) ; String.
2831 (cons (point) (or (c-safe (c-forward-sexp 1) (point))
2832 (point-max))))
2834 ((looking-at c-comment-start-regexp) ; Line or block comment.
2835 (cons (point) (progn (c-forward-single-comment) (point))))
2838 ;; Search backward.
2839 (skip-chars-backward " \t")
2841 (let ((end (point)) beg)
2842 (cond
2843 ((save-excursion
2844 (< (skip-syntax-backward c-string-syntax) 0)) ; String.
2845 (setq beg (c-safe (c-backward-sexp 1) (point))))
2847 ((and (c-safe (forward-char -2) t)
2848 (looking-at "*/"))
2849 ;; Block comment. Due to the nature of line
2850 ;; comments, they will always be covered by the
2851 ;; normal case above.
2852 (goto-char end)
2853 (c-backward-single-comment)
2854 ;; If LIM is bogus, beg will be bogus.
2855 (setq beg (point))))
2857 (if beg (cons beg end))))))
2858 ))))
2860 (defun c-literal-limits-fast (&optional lim near not-in-delimiter)
2861 ;; Like c-literal-limits, but for emacsen whose `parse-partial-sexp'
2862 ;; returns the pos of the comment start.
2864 "Return a cons of the beginning and end positions of the comment or
2865 string surrounding point (including both delimiters), or nil if point
2866 isn't in one. If LIM is non-nil, it's used as the \"safe\" position
2867 to start parsing from. If NEAR is non-nil, then the limits of any
2868 literal next to point is returned. \"Next to\" means there's only
2869 spaces and tabs between point and the literal. The search for such a
2870 literal is done first in forward direction. If NOT-IN-DELIMITER is
2871 non-nil, the case when point is inside a starting delimiter won't be
2872 recognized. This only has effect for comments, which have starting
2873 delimiters with more than one character.
2875 This function does not do any hidden buffer changes."
2877 (save-excursion
2878 (let* ((pos (point))
2879 (lim (or lim (progn
2880 (c-beginning-of-syntax)
2881 (point))))
2882 (state (parse-partial-sexp lim pos)))
2884 (cond ((elt state 3) ; String.
2885 (goto-char (elt state 8))
2886 (cons (point) (or (c-safe (c-forward-sexp 1) (point))
2887 (point-max))))
2889 ((elt state 4) ; Comment.
2890 (goto-char (elt state 8))
2891 (cons (point) (progn (c-forward-single-comment) (point))))
2893 ((and (not not-in-delimiter)
2894 (not (elt state 5))
2895 (eq (char-before) ?/)
2896 (looking-at "[/*]"))
2897 ;; We're standing in a comment starter.
2898 (backward-char 1)
2899 (cons (point) (progn (c-forward-single-comment) (point))))
2901 (near
2902 (goto-char pos)
2904 ;; Search forward for a literal.
2905 (skip-chars-forward " \t")
2907 (cond
2908 ((looking-at c-string-limit-regexp) ; String.
2909 (cons (point) (or (c-safe (c-forward-sexp 1) (point))
2910 (point-max))))
2912 ((looking-at c-comment-start-regexp) ; Line or block comment.
2913 (cons (point) (progn (c-forward-single-comment) (point))))
2916 ;; Search backward.
2917 (skip-chars-backward " \t")
2919 (let ((end (point)) beg)
2920 (cond
2921 ((save-excursion
2922 (< (skip-syntax-backward c-string-syntax) 0)) ; String.
2923 (setq beg (c-safe (c-backward-sexp 1) (point))))
2925 ((and (c-safe (forward-char -2) t)
2926 (looking-at "*/"))
2927 ;; Block comment. Due to the nature of line
2928 ;; comments, they will always be covered by the
2929 ;; normal case above.
2930 (goto-char end)
2931 (c-backward-single-comment)
2932 ;; If LIM is bogus, beg will be bogus.
2933 (setq beg (point))))
2935 (if beg (cons beg end))))))
2936 ))))
2938 (if (memq 'pps-extended-state c-emacs-features)
2939 (defalias 'c-literal-limits 'c-literal-limits-fast))
2941 (defun c-collect-line-comments (range)
2942 "If the argument is a cons of two buffer positions (such as returned by
2943 `c-literal-limits'), and that range contains a C++ style line comment,
2944 then an extended range is returned that contains all adjacent line
2945 comments (i.e. all comments that starts in the same column with no
2946 empty lines or non-whitespace characters between them). Otherwise the
2947 argument is returned.
2949 This function does not do any hidden buffer changes."
2950 (save-excursion
2951 (condition-case nil
2952 (if (and (consp range) (progn
2953 (goto-char (car range))
2954 (looking-at "//")))
2955 (let ((col (current-column))
2956 (beg (point))
2957 (bopl (c-point 'bopl))
2958 (end (cdr range)))
2959 ;; Got to take care in the backward direction to handle
2960 ;; comments which are preceded by code.
2961 (while (and (c-backward-single-comment)
2962 (>= (point) bopl)
2963 (looking-at "//")
2964 (= col (current-column)))
2965 (setq beg (point)
2966 bopl (c-point 'bopl)))
2967 (goto-char end)
2968 (while (and (progn (skip-chars-forward " \t")
2969 (looking-at "//"))
2970 (= col (current-column))
2971 (prog1 (zerop (forward-line 1))
2972 (setq end (point)))))
2973 (cons beg end))
2974 range)
2975 (error range))))
2977 (defun c-literal-type (range)
2978 "Convenience function that given the result of `c-literal-limits',
2979 returns nil or the type of literal that the range surrounds. It's
2980 much faster than using `c-in-literal' and is intended to be used when
2981 you need both the type of a literal and its limits.
2983 This function does not do any hidden buffer changes."
2984 (if (consp range)
2985 (save-excursion
2986 (goto-char (car range))
2987 (cond ((looking-at c-string-limit-regexp) 'string)
2988 ((or (looking-at "//") ; c++ line comment
2989 (and (looking-at "\\s<") ; comment starter
2990 (looking-at "#"))) ; awk comment.
2991 'c++)
2992 (t 'c))) ; Assuming the range is valid.
2993 range))
2996 ;; `c-find-decl-spots' and accompanying stuff.
2998 ;; Variables used in `c-find-decl-spots' to cache the search done for
2999 ;; the first declaration in the last call. When that function starts,
3000 ;; it needs to back up over syntactic whitespace to look at the last
3001 ;; token before the region being searched. That can sometimes cause
3002 ;; moves back and forth over a quite large region of comments and
3003 ;; macros, which would be repeated for each changed character when
3004 ;; we're called during fontification, since font-lock refontifies the
3005 ;; current line for each change. Thus it's worthwhile to cache the
3006 ;; first match.
3008 ;; `c-find-decl-syntactic-pos' is a syntactically relevant position in
3009 ;; the syntactic whitespace less or equal to some start position.
3010 ;; There's no cached value if it's nil.
3012 ;; `c-find-decl-match-pos' is the match position if
3013 ;; `c-find-decl-prefix-search' matched before the syntactic whitespace
3014 ;; at `c-find-decl-syntactic-pos', or nil if there's no such match.
3015 (defvar c-find-decl-syntactic-pos nil)
3016 (make-variable-buffer-local 'c-find-decl-syntactic-pos)
3017 (defvar c-find-decl-match-pos nil)
3018 (make-variable-buffer-local 'c-find-decl-match-pos)
3020 (defsubst c-invalidate-find-decl-cache (change-min-pos)
3021 (and c-find-decl-syntactic-pos
3022 (< change-min-pos c-find-decl-syntactic-pos)
3023 (setq c-find-decl-syntactic-pos nil)))
3025 ; (defface c-debug-decl-spot-face
3026 ; '((t (:background "Turquoise")))
3027 ; "Debug face to mark the spots where `c-find-decl-spots' stopped.")
3028 ; (defface c-debug-decl-sws-face
3029 ; '((t (:background "Khaki")))
3030 ; "Debug face to mark the syntactic whitespace between the declaration
3031 ; spots and the preceding token end.")
3033 (defmacro c-debug-put-decl-spot-faces (match-pos decl-pos)
3034 (when (facep 'c-debug-decl-spot-face)
3035 `(let ((match-pos ,match-pos) (decl-pos ,decl-pos))
3036 (c-debug-add-face (max match-pos (point-min)) decl-pos
3037 'c-debug-decl-sws-face)
3038 (c-debug-add-face decl-pos (min (1+ decl-pos) (point-max))
3039 'c-debug-decl-spot-face))))
3040 (defmacro c-debug-remove-decl-spot-faces (beg end)
3041 (when (facep 'c-debug-decl-spot-face)
3042 `(progn
3043 (c-debug-remove-face ,beg ,end 'c-debug-decl-spot-face)
3044 (c-debug-remove-face ,beg ,end 'c-debug-decl-sws-face))))
3046 (defmacro c-find-decl-prefix-search ()
3047 ;; Macro used inside `c-find-decl-spots'. It ought to be a defun,
3048 ;; but it contains lots of free variables that refer to things
3049 ;; inside `c-find-decl-spots'. The point is left at `cfd-match-pos'
3050 ;; if there is a match, otherwise at `cfd-limit'.
3052 '(progn
3053 ;; Find the next property match position if we haven't got one already.
3054 (unless cfd-prop-match
3055 (save-excursion
3056 (while (progn
3057 (goto-char (next-single-property-change
3058 (point) 'c-type nil cfd-limit))
3059 (and (< (point) cfd-limit)
3060 (not (eq (c-get-char-property (1- (point)) 'c-type)
3061 'c-decl-end)))))
3062 (setq cfd-prop-match (point))))
3064 ;; Find the next `c-decl-prefix-re' match if we haven't got one already.
3065 (unless cfd-re-match
3066 (while (and (setq cfd-re-match
3067 (re-search-forward c-decl-prefix-re cfd-limit 'move))
3068 (c-got-face-at (1- (setq cfd-re-match (match-end 1)))
3069 c-literal-faces))
3070 ;; Search again if the match is within a comment or a string literal.
3071 (while (progn
3072 (goto-char (next-single-property-change
3073 cfd-re-match 'face nil cfd-limit))
3074 (and (< (point) cfd-limit)
3075 (c-got-face-at (point) c-literal-faces)))
3076 (setq cfd-re-match (point))))
3077 (unless cfd-re-match
3078 (setq cfd-re-match cfd-limit)))
3080 ;; Choose whichever match is closer to the start.
3081 (if (< cfd-re-match cfd-prop-match)
3082 (setq cfd-match-pos cfd-re-match
3083 cfd-re-match nil)
3084 (setq cfd-match-pos cfd-prop-match
3085 cfd-prop-match nil))
3087 (goto-char cfd-match-pos)
3089 (when (< cfd-match-pos cfd-limit)
3090 ;; Skip forward past comments only so we don't skip macros.
3091 (c-forward-comments)
3092 ;; Set the position to continue at. We can avoid going over
3093 ;; the comments skipped above a second time, but it's possible
3094 ;; that the comment skipping has taken us past `cfd-prop-match'
3095 ;; since the property might be used inside comments.
3096 (setq cfd-continue-pos (if cfd-prop-match
3097 (min cfd-prop-match (point))
3098 (point))))))
3100 (defun c-find-decl-spots (cfd-limit cfd-decl-re cfd-face-checklist cfd-fun)
3101 ;; Call CFD-FUN for each possible spot for a declaration from the
3102 ;; point to CFD-LIMIT. A spot for a declaration is the first token
3103 ;; in the buffer and each token after the ones matched by
3104 ;; `c-decl-prefix-re' and after the occurrences of the `c-type'
3105 ;; property with the value `c-decl-end' (if `c-type-decl-end-used'
3106 ;; is set). Only a spot that match CFD-DECL-RE and whose face is in
3107 ;; the CFD-FACE-CHECKLIST list causes CFD-FUN to be called. The
3108 ;; face check is disabled if CFD-FACE-CHECKLIST is nil.
3110 ;; If the match is inside a macro then the buffer is narrowed to the
3111 ;; end of it, so that CFD-FUN can investigate the following tokens
3112 ;; without matching something that begins inside a macro and ends
3113 ;; outside it. It's to avoid this work that the CFD-DECL-RE and
3114 ;; CFD-FACE-CHECKLIST checks exist.
3116 ;; CFD-FUN is called with point at the start of the spot. It's
3117 ;; passed two arguments: The first is the end position of the token
3118 ;; that `c-decl-prefix-re' matched, or 0 for the implicit match at
3119 ;; bob. The second is a flag that is t when the match is inside a
3120 ;; macro.
3122 ;; It's assumed that comment and strings are fontified in the
3123 ;; searched range.
3125 ;; This is mainly used in fontification, and so has an elaborate
3126 ;; cache to handle repeated calls from the same start position; see
3127 ;; the variables above.
3129 ;; All variables in this function begin with `cfd-' to avoid name
3130 ;; collision with the (dynamically bound) variables used in CFD-FUN.
3132 (let ((cfd-buffer-end (point-max))
3133 ;; The last regexp match found by `c-find-decl-prefix-search'.
3134 cfd-re-match
3135 ;; The last `c-decl-end' found by `c-find-decl-prefix-search'.
3136 ;; If searching for the property isn't needed then we disable
3137 ;; it by faking a first match at the limit.
3138 (cfd-prop-match (unless c-type-decl-end-used cfd-limit))
3139 ;; The position of the last match found by
3140 ;; `c-find-decl-prefix-search'. For regexp matches it's the
3141 ;; end of the matched token, for property matches it's the end
3142 ;; of the property. 0 for the implicit match at bob.
3143 ;; `cfd-limit' if there's no match.
3144 (cfd-match-pos cfd-limit)
3145 ;; The position to continue searching at.
3146 cfd-continue-pos
3147 ;; The position of the last "real" token we've stopped at.
3148 ;; This can be greater than `cfd-continue-pos' when we get
3149 ;; hits inside macros or at `c-decl-end' positions inside
3150 ;; comments.
3151 (cfd-token-pos 0)
3152 ;; The end position of the last entered macro.
3153 (cfd-macro-end 0))
3155 ;; Initialize by finding a syntactically relevant start position
3156 ;; before the point, and do the first `c-decl-prefix-re' search
3157 ;; unless we're at bob.
3159 (let ((start-pos (point)) syntactic-pos)
3160 ;; Must back up a bit since we look for the end of the previous
3161 ;; statement or declaration, which is earlier than the first
3162 ;; returned match.
3164 (when (c-got-face-at (point) c-literal-faces)
3165 ;; But first we need to move to a syntactically relevant
3166 ;; position. Use the faces to back up to the start of the
3167 ;; comment or string literal.
3168 (when (and (not (bobp))
3169 (c-got-face-at (1- (point)) c-literal-faces))
3170 (while (progn
3171 (goto-char (previous-single-property-change
3172 (point) 'face nil (point-min)))
3173 (and (> (point) (point-min))
3174 (c-got-face-at (point) c-literal-faces)))))
3176 ;; XEmacs doesn't fontify the quotes surrounding string
3177 ;; literals.
3178 (and (featurep 'xemacs)
3179 (eq (get-text-property (point) 'face)
3180 'font-lock-string-face)
3181 (not (bobp))
3182 (progn (backward-char)
3183 (not (looking-at c-string-limit-regexp)))
3184 (forward-char))
3186 ;; The font lock package might not have fontified the start of
3187 ;; the literal at all so check that we have arrived at
3188 ;; something that looks like a start or else resort to
3189 ;; `c-literal-limits'.
3190 (unless (looking-at c-literal-start-regexp)
3191 (let ((range (c-literal-limits)))
3192 (if range (goto-char (car range))))))
3194 ;; Must back out of any macro so that we don't miss any
3195 ;; declaration that could follow after it, unless the limit is
3196 ;; inside the macro. We only check that for the current line to
3197 ;; save some time; it's enough for the by far most common case
3198 ;; when font-lock refontifies the current line only.
3199 (when (save-excursion
3200 (and (= (forward-line 1) 0)
3201 (bolp) ; forward-line has funny behavior at eob.
3202 (or (< (c-point 'eol) cfd-limit)
3203 (progn (backward-char)
3204 (not (eq (char-before) ?\\))))))
3205 (c-beginning-of-macro))
3207 ;; Clear the cache if it applied further down.
3208 (c-invalidate-find-decl-cache start-pos)
3210 (setq syntactic-pos (point))
3211 (c-backward-syntactic-ws c-find-decl-syntactic-pos)
3213 ;; If we hit `c-find-decl-syntactic-pos' and
3214 ;; `c-find-decl-match-pos' is set then we install the cached
3215 ;; values. If we hit `c-find-decl-syntactic-pos' and
3216 ;; `c-find-decl-match-pos' is nil then we know there's no decl
3217 ;; prefix in the whitespace before `c-find-decl-syntactic-pos'
3218 ;; and so we can continue the search from this point. If we
3219 ;; didn't hit `c-find-decl-syntactic-pos' then we're now in the
3220 ;; right spot to begin searching anyway.
3221 (if (and (eq (point) c-find-decl-syntactic-pos)
3222 c-find-decl-match-pos)
3224 (progn
3225 ;; The match is always outside macros and comments so we
3226 ;; start at the next token. The loop below will later go
3227 ;; back using `cfd-continue-pos' to fix declarations inside
3228 ;; the syntactic ws.
3229 (goto-char syntactic-pos)
3230 (c-forward-syntactic-ws)
3231 (setq cfd-match-pos c-find-decl-match-pos
3232 cfd-continue-pos syntactic-pos)
3233 (if (< cfd-continue-pos (point))
3234 (setq cfd-token-pos (point))))
3236 (setq c-find-decl-syntactic-pos syntactic-pos)
3238 (when (if (bobp)
3239 ;; Always consider bob a match to get the first declaration
3240 ;; in the file. Do this separately instead of letting
3241 ;; `c-decl-prefix-re' match bob, so that it always can
3242 ;; consume at least one character to ensure that we won't
3243 ;; get stuck in an infinite loop.
3244 (setq cfd-re-match 0)
3245 (backward-char)
3246 (c-beginning-of-current-token)
3247 (< (point) cfd-limit))
3248 ;; Do an initial search now. In the bob case above it's only done
3249 ;; to search for the `c-type' property.
3250 (c-find-decl-prefix-search))
3252 ;; Advance `cfd-continue-pos' if we got a hit before the start
3253 ;; position. The earliest position that could affect after
3254 ;; the start position is the char before the preceding
3255 ;; comments.
3256 (when (and cfd-continue-pos (< cfd-continue-pos start-pos))
3257 (goto-char syntactic-pos)
3258 (c-backward-comments)
3259 (unless (bobp)
3260 (backward-char)
3261 (c-beginning-of-current-token))
3262 (setq cfd-continue-pos (max cfd-continue-pos (point))))
3264 ;; If we got a match it's always outside macros and comments so
3265 ;; advance to the next token and set `cfd-token-pos'. The loop
3266 ;; below will later go back using `cfd-continue-pos' to fix
3267 ;; declarations inside the syntactic ws.
3268 (when (and (< cfd-match-pos cfd-limit) (< (point) syntactic-pos))
3269 (goto-char syntactic-pos)
3270 (c-forward-syntactic-ws)
3271 (and cfd-continue-pos
3272 (< cfd-continue-pos (point))
3273 (setq cfd-token-pos (point))))
3275 (setq c-find-decl-match-pos (and (< cfd-match-pos start-pos)
3276 cfd-match-pos))))
3278 ;; Now loop. We already got the first match.
3280 (while (progn
3281 (while (and
3282 (< cfd-match-pos cfd-limit)
3285 ;; Kludge to filter out matches on the "<" that
3286 ;; aren't open parens, for the sake of languages
3287 ;; that got `c-recognize-<>-arglists' set.
3288 (and (eq (char-before cfd-match-pos) ?<)
3289 (not (c-get-char-property (1- cfd-match-pos)
3290 'syntax-table)))
3292 ;; If `cfd-continue-pos' is less or equal to
3293 ;; `cfd-token-pos', we've got a hit inside a macro
3294 ;; that's in the syntactic whitespace before the last
3295 ;; "real" declaration we've checked. If they're equal
3296 ;; we've arrived at the declaration a second time, so
3297 ;; there's nothing to do.
3298 (= cfd-continue-pos cfd-token-pos)
3300 (progn
3301 ;; If `cfd-continue-pos' is less than `cfd-token-pos'
3302 ;; we're still searching for declarations embedded in
3303 ;; the syntactic whitespace. In that case we need
3304 ;; only to skip comments and not macros, since they
3305 ;; can't be nested, and that's already been done in
3306 ;; `c-find-decl-prefix-search'.
3307 (when (> cfd-continue-pos cfd-token-pos)
3308 (c-forward-syntactic-ws)
3309 (setq cfd-token-pos (point)))
3311 ;; Continue if the following token fails the
3312 ;; CFD-DECL-RE and CFD-FACE-CHECKLIST checks.
3313 (when (or (>= (point) cfd-limit)
3314 (not (looking-at cfd-decl-re))
3315 (and cfd-face-checklist
3316 (not (c-got-face-at
3317 (point) cfd-face-checklist))))
3318 (goto-char cfd-continue-pos)
3319 t)))
3321 (< (point) cfd-limit))
3322 (c-find-decl-prefix-search))
3324 (< (point) cfd-limit))
3326 (when (progn
3327 ;; Narrow to the end of the macro if we got a hit inside
3328 ;; one, to avoid recognizing things that start inside
3329 ;; the macro and end outside it.
3330 (when (> cfd-match-pos cfd-macro-end)
3331 ;; Not in the same macro as in the previous round.
3332 (save-excursion
3333 (goto-char cfd-match-pos)
3334 (setq cfd-macro-end
3335 (if (save-excursion (and (c-beginning-of-macro)
3336 (< (point) cfd-match-pos)))
3337 (progn (c-end-of-macro)
3338 (point))
3339 0))))
3341 (if (zerop cfd-macro-end)
3343 (if (> cfd-macro-end (point))
3344 (progn (narrow-to-region (point-min) cfd-macro-end)
3346 ;; The matched token was the last thing in the
3347 ;; macro, so the whole match is bogus.
3348 (setq cfd-macro-end 0)
3349 nil)))
3351 (c-debug-put-decl-spot-faces cfd-match-pos (point))
3352 (funcall cfd-fun cfd-match-pos (/= cfd-macro-end 0))
3354 (when (/= cfd-macro-end 0)
3355 ;; Restore limits if we did macro narrowment above.
3356 (narrow-to-region (point-min) cfd-buffer-end)))
3358 (goto-char cfd-continue-pos)
3359 (if (= cfd-continue-pos cfd-limit)
3360 (setq cfd-match-pos cfd-limit)
3361 (c-find-decl-prefix-search)))))
3364 ;; A cache for found types.
3366 ;; Buffer local variable that contains an obarray with the types we've
3367 ;; found. If a declaration is recognized somewhere we record the
3368 ;; fully qualified identifier in it to recognize it as a type
3369 ;; elsewhere in the file too. This is not accurate since we do not
3370 ;; bother with the scoping rules of the languages, but in practice the
3371 ;; same name is seldom used as both a type and something else in a
3372 ;; file, and we only use this as a last resort in ambiguous cases (see
3373 ;; `c-font-lock-declarations').
3374 (defvar c-found-types nil)
3375 (make-variable-buffer-local 'c-found-types)
3377 (defsubst c-clear-found-types ()
3378 ;; Clears `c-found-types'.
3380 ;; This function does not do any hidden buffer changes.
3381 (setq c-found-types (make-vector 53 0)))
3383 (defun c-add-type (from to)
3384 ;; Add the given region as a type in `c-found-types'. If the region
3385 ;; doesn't match an existing type but there is a type which is equal
3386 ;; to the given one except that the last character is missing, then
3387 ;; the shorter type is removed. That's done to avoid adding all
3388 ;; prefixes of a type as it's being entered and font locked. This
3389 ;; doesn't cover cases like when characters are removed from a type
3390 ;; or added in the middle. We'd need the position of point when the
3391 ;; font locking is invoked to solve this well.
3392 (unless (and c-recognize-<>-arglists
3393 (save-excursion
3394 (goto-char from)
3395 (c-syntactic-re-search-forward "<" to t)))
3396 ;; To avoid storing very long strings, do not add a type that
3397 ;; contains '<' in languages with angle bracket arglists, since
3398 ;; the type then probably contains a C++ template spec and those
3399 ;; can be fairly sized programs in themselves.
3400 (let ((type (c-syntactic-content from to)))
3401 (unless (intern-soft type c-found-types)
3402 (unintern (substring type 0 -1) c-found-types)
3403 (intern type c-found-types)))))
3405 (defsubst c-check-type (from to)
3406 ;; Return non-nil if the given region contains a type in
3407 ;; `c-found-types'.
3408 (intern-soft (c-syntactic-content from to) c-found-types))
3410 (defun c-list-found-types ()
3411 ;; Return all the types in `c-found-types' as a sorted list of
3412 ;; strings.
3413 (let (type-list)
3414 (mapatoms (lambda (type)
3415 (setq type-list (cons (symbol-name type)
3416 type-list)))
3417 c-found-types)
3418 (sort type-list 'string-lessp)))
3421 ;; Handling of small scale constructs like types and names.
3423 (defun c-remove-<>-arglist-properties (from to)
3424 ;; Remove all the properties put by `c-forward-<>-arglist' in the
3425 ;; specified region. Point is clobbered.
3426 (goto-char from)
3427 (while (progn (skip-chars-forward "^<>," to)
3428 (< (point) to))
3429 (if (eq (char-after) ?,)
3430 (when (eq (c-get-char-property (point) 'c-type) 'c-<>-arg-sep)
3431 (c-clear-char-property (point) 'c-type))
3432 (c-clear-char-property (point) 'syntax-table))
3433 (forward-char)))
3435 ;; Dynamically bound variable that instructs `c-forward-type' to also
3436 ;; treat possible types (i.e. those that it normally returns 'maybe or
3437 ;; 'found for) as actual types (and always return 'found for them).
3438 ;; This means that it records them in `c-record-type-identifiers' if
3439 ;; that is set, and that it adds them to `c-found-types'.
3440 (defvar c-promote-possible-types nil)
3442 ;; Dynamically bound variable that instructs `c-forward-<>-arglist' to
3443 ;; not accept arglists that contain binary operators.
3445 ;; This is primarily used to handle C++ template arglists. C++
3446 ;; disambiguates them by checking whether the preceding name is a
3447 ;; template or not. We can't do that, so we assume it is a template
3448 ;; if it can be parsed as one. That usually works well since
3449 ;; comparison expressions on the forms "a < b > c" or "a < b, c > d"
3450 ;; in almost all cases would be pointless.
3452 ;; However, in function arglists, e.g. in "foo (a < b, c > d)", we
3453 ;; should let the comma separate the function arguments instead. And
3454 ;; in a context where the value of the expression is taken, e.g. in
3455 ;; "if (a < b || c > d)", it's probably not a template.
3456 (defvar c-restricted-<>-arglists nil)
3458 ;; Dynamically bound variables that instructs `c-forward-name',
3459 ;; `c-forward-type' and `c-forward-<>-arglist' to record the ranges of
3460 ;; all the type and reference identifiers they encounter. They will
3461 ;; build lists on these variables where each element is a cons of the
3462 ;; buffer positions surrounding each identifier. This recording is
3463 ;; only activated when `c-record-type-identifiers' is non-nil.
3465 ;; All known types that can't be identifiers are recorded, and also
3466 ;; other possible types if `c-promote-possible-types' is set.
3467 ;; Recording is however disabled inside angle bracket arglists that
3468 ;; are encountered inside names and other angle bracket arglists.
3469 ;; Such occurences are taken care of by `c-font-lock-<>-arglists'
3470 ;; instead.
3472 ;; Only the names in C++ template style references (e.g. "tmpl" in
3473 ;; "tmpl<a,b>::foo") are recorded as references, other references
3474 ;; aren't handled here.
3475 (defvar c-record-type-identifiers nil)
3476 (defvar c-record-ref-identifiers nil)
3478 ;; If `c-record-type-identifiers' is set, this will receive a cons
3479 ;; cell of the range of the last single identifier symbol stepped over
3480 ;; by `c-forward-name' if it's successful. This is the range that
3481 ;; should be put on one of the record lists by the caller. It's
3482 ;; assigned nil if there's no such symbol in the name.
3483 (defvar c-last-identifier-range nil)
3485 (defmacro c-record-type-id (range)
3486 (if (eq (car-safe range) 'cons)
3487 ;; Always true.
3488 `(setq c-record-type-identifiers
3489 (cons ,range c-record-type-identifiers))
3490 `(let ((range ,range))
3491 (if range
3492 (setq c-record-type-identifiers
3493 (cons range c-record-type-identifiers))))))
3495 (defmacro c-record-ref-id (range)
3496 (if (eq (car-safe range) 'cons)
3497 ;; Always true.
3498 `(setq c-record-ref-identifiers
3499 (cons ,range c-record-ref-identifiers))
3500 `(let ((range ,range))
3501 (if range
3502 (setq c-record-ref-identifiers
3503 (cons range c-record-ref-identifiers))))))
3505 ;; Dynamically bound variable that instructs `c-forward-type' to
3506 ;; record the ranges of types that only are found. Behaves otherwise
3507 ;; like `c-record-type-identifiers'.
3508 (defvar c-record-found-types nil)
3510 (defmacro c-forward-keyword-prefixed-id (type)
3511 ;; Used internally in `c-forward-keyword-clause' to move forward
3512 ;; over a type (if TYPE is 'type) or a name (otherwise) which
3513 ;; possibly is prefixed by keywords and their associated clauses.
3514 ;; Try with a type/name first to not trip up on those that begin
3515 ;; with a keyword. Return t if a known or found type is moved
3516 ;; over. The point is clobbered if nil is returned. If range
3517 ;; recording is enabled, the identifier is recorded on as a type
3518 ;; if TYPE is 'type or as a reference if TYPE is 'ref.
3519 `(let (res)
3520 (while (if (setq res ,(if (eq type 'type)
3521 `(c-forward-type)
3522 `(c-forward-name)))
3524 (and (looking-at c-keywords-regexp)
3525 (c-forward-keyword-clause))))
3526 (when (memq res '(t known found prefix))
3527 ,(when (eq type 'ref)
3528 `(when c-record-type-identifiers
3529 (c-record-ref-id c-last-identifier-range)))
3530 t)))
3532 (defmacro c-forward-id-comma-list (type)
3533 ;; Used internally in `c-forward-keyword-clause' to move forward
3534 ;; over a comma separated list of types or names using
3535 ;; `c-forward-keyword-prefixed-id'.
3536 `(while (and (progn
3537 (setq safe-pos (point))
3538 (eq (char-after) ?,))
3539 (progn
3540 (forward-char)
3541 (c-forward-syntactic-ws)
3542 (c-forward-keyword-prefixed-id ,type)))))
3544 (defun c-forward-keyword-clause ()
3545 ;; The first submatch in the current match data is assumed to
3546 ;; surround a token. If it's a keyword, move over it and any
3547 ;; following clauses associated with it, stopping at the next
3548 ;; following token. t is returned in that case, otherwise the point
3549 ;; stays and nil is returned. The kind of clauses that are
3550 ;; recognized are those specified by `c-type-list-kwds',
3551 ;; `c-ref-list-kwds', `c-colon-type-list-kwds',
3552 ;; `c-paren-nontype-kwds', `c-paren-type-kwds', `c-<>-type-kwds',
3553 ;; and `c-<>-arglist-kwds'.
3555 (let ((kwd-sym (c-keyword-sym (match-string 1))) safe-pos pos)
3556 (when kwd-sym
3557 (goto-char (match-end 1))
3558 (c-forward-syntactic-ws)
3559 (setq safe-pos (point))
3561 (cond
3562 ((and (c-keyword-member kwd-sym 'c-type-list-kwds)
3563 (c-forward-keyword-prefixed-id type))
3564 ;; There's a type directly after a keyword in `c-type-list-kwds'.
3565 (c-forward-id-comma-list type))
3567 ((and (c-keyword-member kwd-sym 'c-ref-list-kwds)
3568 (c-forward-keyword-prefixed-id ref))
3569 ;; There's a name directly after a keyword in `c-ref-list-kwds'.
3570 (c-forward-id-comma-list ref))
3572 ((and (c-keyword-member kwd-sym 'c-paren-any-kwds)
3573 (eq (char-after) ?\())
3574 ;; There's an open paren after a keyword in `c-paren-any-kwds'.
3576 (forward-char)
3577 (when (and (setq pos (c-up-list-forward))
3578 (eq (char-before pos) ?\)))
3579 (when (and c-record-type-identifiers
3580 (c-keyword-member kwd-sym 'c-paren-type-kwds))
3581 ;; Use `c-forward-type' on every identifier we can find
3582 ;; inside the paren, to record the types.
3583 (while (c-syntactic-re-search-forward c-symbol-start pos t)
3584 (goto-char (match-beginning 0))
3585 (unless (c-forward-type)
3586 (looking-at c-symbol-key) ; Always matches.
3587 (goto-char (match-end 0)))))
3589 (goto-char pos)
3590 (c-forward-syntactic-ws)
3591 (setq safe-pos (point))))
3593 ((and (c-keyword-member kwd-sym 'c-<>-sexp-kwds)
3594 (eq (char-after) ?<)
3595 (c-forward-<>-arglist (c-keyword-member kwd-sym 'c-<>-type-kwds)
3596 (or c-record-type-identifiers
3597 c-restricted-<>-arglists)))
3598 (c-forward-syntactic-ws)
3599 (setq safe-pos (point)))
3601 ((and (c-keyword-member kwd-sym 'c-nonsymbol-sexp-kwds)
3602 (not (looking-at c-symbol-start))
3603 (c-safe (c-forward-sexp) t))
3604 (c-forward-syntactic-ws)
3605 (setq safe-pos (point))))
3607 (when (and (c-keyword-member kwd-sym 'c-colon-type-list-kwds)
3608 (progn
3609 ;; If a keyword matched both one of the types above and
3610 ;; this one, we match `c-colon-type-list-re' after the
3611 ;; clause matched above.
3612 (goto-char safe-pos)
3613 (looking-at c-colon-type-list-re))
3614 (progn
3615 (goto-char (match-end 0))
3616 (c-forward-syntactic-ws)
3617 (c-forward-keyword-prefixed-id type)))
3618 ;; There's a type after the `c-colon-type-list-re'
3619 ;; match after a keyword in `c-colon-type-list-kwds'.
3620 (c-forward-id-comma-list type))
3622 (goto-char safe-pos)
3623 t)))
3625 (defun c-forward-<>-arglist (all-types reparse)
3626 ;; The point is assumed to be at a '<'. Try to treat it as the open
3627 ;; paren of an angle bracket arglist and move forward to the the
3628 ;; corresponding '>'. If successful, the point is left after the
3629 ;; '>' and t is returned, otherwise the point isn't moved and nil is
3630 ;; returned. If ALL-TYPES is t then all encountered arguments in
3631 ;; the arglist that might be types are treated as found types.
3633 ;; The surrounding '<' and '>' are given syntax-table properties to
3634 ;; make them behave like parentheses. Each argument separating ','
3635 ;; is also set to `c-<>-arg-sep' in the `c-type' property. These
3636 ;; properties are also cleared in a relevant region forward from the
3637 ;; point if they seems to be set and it turns out to not be an
3638 ;; arglist.
3640 ;; If the arglist has been successfully parsed before then paren
3641 ;; syntax properties will be exploited to quickly jump to the end,
3642 ;; but that can be disabled by setting REPARSE to t. That is
3643 ;; necessary if the various side effects, e.g. recording of type
3644 ;; ranges, are important. Setting REPARSE to t only applies
3645 ;; recursively to nested angle bracket arglists if
3646 ;; `c-restricted-<>-arglists' is set.
3648 (let ((start (point))
3649 ;; If `c-record-type-identifiers' is set then activate
3650 ;; recording of any found types that constitute an argument in
3651 ;; the arglist.
3652 (c-record-found-types (if c-record-type-identifiers t)))
3653 (if (catch 'angle-bracket-arglist-escape
3654 (setq c-record-found-types
3655 (c-forward-<>-arglist-recur all-types reparse)))
3656 (progn
3657 (when (consp c-record-found-types)
3658 (setq c-record-type-identifiers
3659 ;; `nconc' doesn't mind that the tail of
3660 ;; `c-record-found-types' is t.
3661 (nconc c-record-found-types c-record-type-identifiers)))
3664 (goto-char start)
3665 nil)))
3667 (defun c-forward-<>-arglist-recur (all-types reparse)
3668 ;; Recursive part of `c-forward-<>-arglist'.
3670 (let ((start (point)) res pos tmp
3671 ;; Cover this so that any recorded found type ranges are
3672 ;; automatically lost if it turns out to not be an angle
3673 ;; bracket arglist. It's propagated through the return value
3674 ;; on successful completion.
3675 (c-record-found-types c-record-found-types)
3676 ;; List that collects the positions after the argument
3677 ;; separating ',' in the arglist.
3678 arg-start-pos)
3680 ;; If the '<' has paren open syntax then we've marked it as an
3681 ;; angle bracket arglist before, so try to skip to the end and see
3682 ;; that the close paren matches.
3683 (if (and (c-get-char-property (point) 'syntax-table)
3684 (progn
3685 (forward-char)
3686 (if (and (not (looking-at c-<-op-cont-regexp))
3687 (if (c-parse-sexp-lookup-properties)
3688 (c-go-up-list-forward)
3689 (catch 'at-end
3690 (let ((depth 1))
3691 (while (c-syntactic-re-search-forward
3692 "[<>]" nil t t)
3693 (when (c-get-char-property (1- (point))
3694 'syntax-table)
3695 (if (eq (char-before) ?<)
3696 (setq depth (1+ depth))
3697 (setq depth (1- depth))
3698 (when (= depth 0) (throw 'at-end t)))))
3699 nil)))
3700 (not (looking-at c->-op-cont-regexp))
3701 (save-excursion
3702 (backward-char)
3703 (= (point)
3704 (progn (c-beginning-of-current-token)
3705 (point)))))
3707 ;; Got an arglist that appears to be valid.
3708 (if reparse
3709 ;; Reparsing is requested, so zap the properties in the
3710 ;; region and go on to redo it. It's done here to
3711 ;; avoid leaving it behind if we exit through
3712 ;; `angle-bracket-arglist-escape' below.
3713 (progn
3714 (c-remove-<>-arglist-properties start (point))
3715 (goto-char start)
3716 nil)
3719 ;; Got unmatched paren brackets or either paren was
3720 ;; actually some other token. Recover by clearing the
3721 ;; syntax properties on all the '<' and '>' in the
3722 ;; range where we'll search for the arglist below.
3723 (goto-char start)
3724 (while (progn (skip-chars-forward "^<>,;{}")
3725 (looking-at "[<>,]"))
3726 (if (eq (char-after) ?,)
3727 (when (eq (c-get-char-property (point) 'c-type)
3728 'c-<>-arg-sep)
3729 (c-clear-char-property (point) 'c-type))
3730 (c-clear-char-property (point) 'syntax-table))
3731 (forward-char))
3732 (goto-char start)
3733 nil)))
3736 (forward-char)
3737 (unless (looking-at c-<-op-cont-regexp)
3738 (while (and
3739 (progn
3741 (when c-record-type-identifiers
3742 (if all-types
3744 ;; All encountered identifiers are types, so set the
3745 ;; promote flag and parse the type.
3746 (progn
3747 (c-forward-syntactic-ws)
3748 (when (looking-at c-identifier-start)
3749 (let ((c-promote-possible-types t))
3750 (c-forward-type))))
3752 ;; Check if this arglist argument is a sole type. If
3753 ;; it's known then it's recorded in
3754 ;; `c-record-type-identifiers'. If it only is found
3755 ;; then it's recorded in `c-record-found-types' which we
3756 ;; might roll back if it turns out that this isn't an
3757 ;; angle bracket arglist afterall.
3758 (when (memq (char-before) '(?, ?<))
3759 (let ((orig-record-found-types c-record-found-types))
3760 (c-forward-syntactic-ws)
3761 (and (memq (c-forward-type) '(known found))
3762 (not (looking-at "[,>]"))
3763 ;; A found type was recorded but it's not the
3764 ;; only thing in the arglist argument, so reset
3765 ;; `c-record-found-types'.
3766 (setq c-record-found-types
3767 orig-record-found-types))))))
3769 (setq pos (point))
3770 (or (when (eq (char-after) ?>)
3771 ;; Must check for '>' at the very start separately,
3772 ;; since the regexp below has to avoid ">>" without
3773 ;; using \\=.
3774 (forward-char)
3777 ;; Note: These regexps exploit the match order in \| so
3778 ;; that "<>" is matched by "<" rather than "[^>:-]>".
3779 (c-syntactic-re-search-forward
3780 (if c-restricted-<>-arglists
3781 ;; Stop on ',', '|', '&', '+' and '-' to catch
3782 ;; common binary operators that could be between
3783 ;; two comparison expressions "a<b" and "c>d".
3784 "[<;{},|&+-]\\|\\([^>:-]>\\)"
3785 ;; Otherwise we still stop on ',' to find the
3786 ;; argument start positions.
3787 "[<;{},]\\|\\([^>:-]>\\)")
3788 nil 'move t t 1)
3790 ;; If the arglist starter has lost its open paren
3791 ;; syntax but not the closer, we won't find the
3792 ;; closer above since we only search in the
3793 ;; balanced sexp. In that case we stop just short
3794 ;; of it so check if the following char is the closer.
3795 (when (eq (char-after) ?>)
3796 ;; Remove its syntax so that we don't enter the
3797 ;; recovery code below. That's not necessary
3798 ;; since there's no real reason to suspect that
3799 ;; things inside the arglist are unbalanced.
3800 (c-clear-char-property (point) 'syntax-table)
3801 (forward-char)
3802 t)))
3804 (cond
3805 ((eq (char-before) ?>)
3806 ;; Either an operator starting with '>' or the end of
3807 ;; the angle bracket arglist.
3809 (if (and (/= (1- (point)) pos)
3810 (c-get-char-property (1- (point)) 'syntax-table)
3811 (progn
3812 (c-clear-char-property (1- (point)) 'syntax-table)
3813 (c-parse-sexp-lookup-properties)))
3815 ;; We've skipped past a list that ended with '>'. It
3816 ;; must be unbalanced since nested arglists are handled
3817 ;; in the case below. Recover by removing all paren
3818 ;; properties on '<' and '>' in the searched region and
3819 ;; redo the search.
3820 (progn
3821 (c-remove-<>-arglist-properties pos (point))
3822 (goto-char pos)
3825 (if (looking-at c->-op-cont-regexp)
3826 (progn
3827 (when (text-property-not-all
3828 (1- (point)) (match-end 0) 'syntax-table nil)
3829 (c-remove-<>-arglist-properties (1- (point))
3830 (match-end 0)))
3831 (goto-char (match-end 0))
3834 ;; The angle bracket arglist is finished.
3835 (while arg-start-pos
3836 (c-put-char-property (1- (car arg-start-pos))
3837 'c-type 'c-<>-arg-sep)
3838 (setq arg-start-pos (cdr arg-start-pos)))
3839 (c-mark-<-as-paren start)
3840 (c-mark->-as-paren (1- (point)))
3841 (setq res t)
3842 nil)))
3844 ((eq (char-before) ?<)
3845 ;; Either an operator starting with '<' or a nested arglist.
3847 (setq pos (point))
3848 (let (id-start id-end subres keyword-match)
3849 (if (if (looking-at c-<-op-cont-regexp)
3850 (setq tmp (match-end 0))
3851 (setq tmp pos)
3852 (backward-char)
3853 (not
3854 (and
3856 (save-excursion
3857 ;; There's always an identifier before a angle
3858 ;; bracket arglist, or a keyword in
3859 ;; `c-<>-type-kwds' or `c-<>-arglist-kwds'.
3860 (c-backward-syntactic-ws)
3861 (setq id-end (point))
3862 (c-simple-skip-symbol-backward)
3863 (when (or (setq keyword-match
3864 (looking-at c-opt-<>-sexp-key))
3865 (not (looking-at c-keywords-regexp)))
3866 (setq id-start (point))))
3868 (setq subres
3869 (let ((c-record-type-identifiers nil)
3870 (c-record-found-types nil))
3871 (c-forward-<>-arglist-recur
3872 (and keyword-match
3873 (c-keyword-member
3874 (c-keyword-sym (match-string 1))
3875 'c-<>-type-kwds))
3876 (and reparse
3877 c-restricted-<>-arglists))))
3880 ;; It was not an angle bracket arglist.
3881 (progn
3882 (when (text-property-not-all
3883 (1- pos) tmp 'syntax-table nil)
3884 (if (c-parse-sexp-lookup-properties)
3885 ;; Got an invalid open paren syntax on this
3886 ;; '<'. We'll probably get an unbalanced '>'
3887 ;; further ahead if we just remove the syntax
3888 ;; here, so recover by removing all paren
3889 ;; properties up to and including the
3890 ;; balancing close paren.
3891 (parse-partial-sexp pos (point-max) -1)
3892 (goto-char tmp))
3893 (c-remove-<>-arglist-properties pos (point)))
3894 (goto-char tmp))
3896 ;; It was an angle bracket arglist.
3897 (setq c-record-found-types subres)
3899 ;; Record the identifier before the template as a type
3900 ;; or reference depending on whether the arglist is last
3901 ;; in a qualified identifier.
3902 (when (and c-record-type-identifiers
3903 (not keyword-match))
3904 (if (and c-opt-identifier-concat-key
3905 (progn
3906 (c-forward-syntactic-ws)
3907 (looking-at c-opt-identifier-concat-key)))
3908 (c-record-ref-id (cons id-start id-end))
3909 (c-record-type-id (cons id-start id-end))))))
3912 ((and (eq (char-before) ?,)
3913 (not c-restricted-<>-arglists))
3914 ;; Just another argument. Record the position. The
3915 ;; type check stuff that made us stop at it is at
3916 ;; the top of the loop.
3917 (setq arg-start-pos (cons (point) arg-start-pos)))
3920 ;; Got a character that can't be in an angle bracket
3921 ;; arglist argument. Abort using `throw', since
3922 ;; it's useless to try to find a surrounding arglist
3923 ;; if we're nested.
3924 (throw 'angle-bracket-arglist-escape nil))))))
3926 (if res
3927 (or c-record-found-types t)))))
3929 (defun c-forward-name ()
3930 ;; Move forward over a complete name if at the beginning of one,
3931 ;; stopping at the next following token. If the point is not at
3932 ;; something that are recognized as name then it stays put. A name
3933 ;; could be something as simple as "foo" in C or something as
3934 ;; complex as "X<Y<class A<int>::B, BIT_MAX >> b>, ::operator<> ::
3935 ;; Z<(a>b)> :: operator const X<&foo>::T Q::G<unsigned short
3936 ;; int>::*volatile const" in C++ (this function is actually little
3937 ;; more than a `looking-at' call in all modes except those that,
3938 ;; like C++, have `c-recognize-<>-arglists' set). Return nil if no
3939 ;; name is found, 'template if it's an identifier ending with an
3940 ;; angle bracket arglist, 'operator of it's an operator identifier,
3941 ;; or t if it's some other kind of name.
3943 (let ((pos (point)) res id-start id-end
3944 ;; Turn off `c-promote-possible-types' here since we might
3945 ;; call `c-forward-<>-arglist' and we don't want it to promote
3946 ;; every suspect thing in the arglist to a type. We're
3947 ;; typically called from `c-forward-type' in this case, and
3948 ;; the caller only wants the top level type that it finds to
3949 ;; be promoted.
3950 c-promote-possible-types)
3951 (while
3952 (and
3953 (looking-at c-identifier-key)
3955 (progn
3956 ;; Check for keyword. We go to the last symbol in
3957 ;; `c-identifier-key' first.
3958 (if (eq c-identifier-key c-symbol-key)
3959 (setq id-start (point)
3960 id-end (match-end 0))
3961 (goto-char (setq id-end (match-end 0)))
3962 (c-simple-skip-symbol-backward)
3963 (setq id-start (point)))
3965 (if (looking-at c-keywords-regexp)
3966 (when (and (c-major-mode-is 'c++-mode)
3967 (looking-at
3968 (cc-eval-when-compile
3969 (concat "\\(operator\\|\\(template\\)\\)"
3970 "\\(" (c-lang-const c-nonsymbol-key c++)
3971 "\\|$\\)")))
3972 (if (match-beginning 2)
3973 ;; "template" is only valid inside an
3974 ;; identifier if preceded by "::".
3975 (save-excursion
3976 (c-backward-syntactic-ws)
3977 (and (c-safe (backward-char 2) t)
3978 (looking-at "::")))
3981 ;; Handle a C++ operator or template identifier.
3982 (goto-char id-end)
3983 (c-forward-syntactic-ws)
3984 (cond ((eq (char-before id-end) ?e)
3985 ;; Got "... ::template".
3986 (let ((subres (c-forward-name)))
3987 (when subres
3988 (setq pos (point)
3989 res subres))))
3991 ((looking-at c-identifier-start)
3992 ;; Got a cast operator.
3993 (when (c-forward-type)
3994 (setq pos (point)
3995 res 'operator)
3996 ;; Now we should match a sequence of either
3997 ;; '*', '&' or a name followed by ":: *",
3998 ;; where each can be followed by a sequence
3999 ;; of `c-opt-type-modifier-key'.
4000 (while (cond ((looking-at "[*&]")
4001 (goto-char (match-end 0))
4003 ((looking-at c-identifier-start)
4004 (and (c-forward-name)
4005 (looking-at "::")
4006 (progn
4007 (goto-char (match-end 0))
4008 (c-forward-syntactic-ws)
4009 (eq (char-after) ?*))
4010 (progn
4011 (forward-char)
4012 t))))
4013 (while (progn
4014 (c-forward-syntactic-ws)
4015 (setq pos (point))
4016 (looking-at c-opt-type-modifier-key))
4017 (goto-char (match-end 1))))))
4019 ((looking-at c-overloadable-operators-regexp)
4020 ;; Got some other operator.
4021 (when c-record-type-identifiers
4022 (setq c-last-identifier-range
4023 (cons (point) (match-end 0))))
4024 (goto-char (match-end 0))
4025 (c-forward-syntactic-ws)
4026 (setq pos (point)
4027 res 'operator)))
4029 nil)
4031 (when c-record-type-identifiers
4032 (setq c-last-identifier-range
4033 (cons id-start id-end)))
4034 (goto-char id-end)
4035 (c-forward-syntactic-ws)
4036 (setq pos (point)
4037 res t)))
4039 (progn
4040 (goto-char pos)
4041 (when (or c-opt-identifier-concat-key
4042 c-recognize-<>-arglists)
4044 (cond
4045 ((and c-opt-identifier-concat-key
4046 (looking-at c-opt-identifier-concat-key))
4047 ;; Got a concatenated identifier. This handles the
4048 ;; cases with tricky syntactic whitespace that aren't
4049 ;; covered in `c-identifier-key'.
4050 (goto-char (match-end 0))
4051 (c-forward-syntactic-ws)
4054 ((and c-recognize-<>-arglists
4055 (eq (char-after) ?<))
4056 ;; Maybe an angle bracket arglist.
4057 (when (let ((c-record-type-identifiers nil)
4058 (c-record-found-types nil))
4059 (c-forward-<>-arglist
4060 nil c-restricted-<>-arglists))
4061 (c-forward-syntactic-ws)
4062 (setq pos (point))
4063 (if (and c-opt-identifier-concat-key
4064 (looking-at c-opt-identifier-concat-key))
4065 ;; Continue if there's an identifier concatenation
4066 ;; operator after the template argument.
4067 (progn
4068 (when c-record-type-identifiers
4069 (c-record-ref-id (cons id-start id-end))
4070 (setq c-last-identifier-range nil))
4071 (forward-char 2)
4072 (c-forward-syntactic-ws)
4074 ;; `c-add-type' isn't called here since we don't
4075 ;; want to add types containing angle bracket
4076 ;; arglists.
4077 (when c-record-type-identifiers
4078 (c-record-type-id (cons id-start id-end))
4079 (setq c-last-identifier-range nil))
4080 (setq res 'template)
4081 nil)))
4082 )))))
4084 (goto-char pos)
4085 res))
4087 (defun c-forward-type ()
4088 ;; Move forward over a type spec if at the beginning of one,
4089 ;; stopping at the next following token. Return t if it's a known
4090 ;; type that can't be a name or other expression, 'known if it's an
4091 ;; otherwise known type (according to `*-font-lock-extra-types'),
4092 ;; 'prefix if it's a known prefix of a type, 'found if it's a type
4093 ;; that matches one in `c-found-types', 'maybe if it's an identfier
4094 ;; that might be a type, or nil if it can't be a type (the point
4095 ;; isn't moved then). The point is assumed to be at the beginning
4096 ;; of a token.
4098 ;; Note that this function doesn't skip past the brace definition
4099 ;; that might be considered part of the type, e.g.
4100 ;; "enum {a, b, c} foo".
4101 (let ((start (point)) pos res res2 id-start id-end id-range)
4103 ;; Skip leading type modifiers. If any are found we know it's a
4104 ;; prefix of a type.
4105 (when c-opt-type-modifier-key
4106 (while (looking-at c-opt-type-modifier-key)
4107 (goto-char (match-end 1))
4108 (c-forward-syntactic-ws)
4109 (setq res 'prefix)))
4111 (cond
4112 ((looking-at c-type-prefix-key)
4113 ;; Looking at a keyword that prefixes a type identifier,
4114 ;; e.g. "class".
4115 (goto-char (match-end 1))
4116 (c-forward-syntactic-ws)
4117 (setq pos (point))
4118 (if (memq (setq res2 (c-forward-name)) '(t template))
4119 (progn
4120 (when (eq res2 t)
4121 ;; In many languages the name can be used without the
4122 ;; prefix, so we add it to `c-found-types'.
4123 (c-add-type pos (point))
4124 (when c-record-type-identifiers
4125 (c-record-type-id c-last-identifier-range)))
4126 (setq res t))
4127 ;; Invalid syntax.
4128 (goto-char start)
4129 (setq res nil)))
4131 ((progn
4132 (setq pos nil)
4133 (if (looking-at c-identifier-start)
4134 (save-excursion
4135 (setq id-start (point)
4136 res2 (c-forward-name))
4137 (when res2
4138 (setq id-end (point)
4139 id-range c-last-identifier-range))))
4140 (and (cond ((looking-at c-primitive-type-key)
4141 (setq res t))
4142 ((c-with-syntax-table c-identifier-syntax-table
4143 (looking-at c-known-type-key))
4144 (setq res 'known)))
4145 (or (not id-end)
4146 (>= (save-excursion
4147 (save-match-data
4148 (goto-char (match-end 1))
4149 (c-forward-syntactic-ws)
4150 (setq pos (point))))
4151 id-end)
4152 (setq res nil))))
4153 ;; Looking at a primitive or known type identifier. We've
4154 ;; checked for a name first so that we don't go here if the
4155 ;; known type match only is a prefix of another name.
4157 (setq id-end (match-end 1))
4159 (when (and c-record-type-identifiers
4160 (or c-promote-possible-types (eq res t)))
4161 (c-record-type-id (cons (match-beginning 1) (match-end 1))))
4163 (if (and c-opt-type-component-key
4164 (save-match-data
4165 (looking-at c-opt-type-component-key)))
4166 ;; There might be more keywords for the type.
4167 (let (safe-pos)
4168 (c-forward-keyword-clause)
4169 (while (progn
4170 (setq safe-pos (point))
4171 (looking-at c-opt-type-component-key))
4172 (when (and c-record-type-identifiers
4173 (looking-at c-primitive-type-key))
4174 (c-record-type-id (cons (match-beginning 1)
4175 (match-end 1))))
4176 (c-forward-keyword-clause))
4177 (if (looking-at c-primitive-type-key)
4178 (progn
4179 (when c-record-type-identifiers
4180 (c-record-type-id (cons (match-beginning 1)
4181 (match-end 1))))
4182 (c-forward-keyword-clause)
4183 (setq res t))
4184 (goto-char safe-pos)
4185 (setq res 'prefix)))
4186 (unless (save-match-data (c-forward-keyword-clause))
4187 (if pos
4188 (goto-char pos)
4189 (goto-char (match-end 1))
4190 (c-forward-syntactic-ws)))))
4192 (res2
4193 (cond ((eq res2 t)
4194 ;; A normal identifier.
4195 (goto-char id-end)
4196 (if (or res c-promote-possible-types)
4197 (progn
4198 (c-add-type id-start id-end)
4199 (when c-record-type-identifiers
4200 (c-record-type-id id-range))
4201 (unless res
4202 (setq res 'found)))
4203 (setq res (if (c-check-type id-start id-end)
4204 ;; It's an identifier that has been used as
4205 ;; a type somewhere else.
4206 'found
4207 ;; It's an identifier that might be a type.
4208 'maybe))))
4209 ((eq res2 'template)
4210 ;; A template is a type.
4211 (goto-char id-end)
4212 (setq res t))
4214 ;; Otherwise it's an operator identifier, which is not a type.
4215 (goto-char start)
4216 (setq res nil)))))
4218 (when res
4219 ;; Skip trailing type modifiers. If any are found we know it's
4220 ;; a type.
4221 (when c-opt-type-modifier-key
4222 (while (looking-at c-opt-type-modifier-key)
4223 (goto-char (match-end 1))
4224 (c-forward-syntactic-ws)
4225 (setq res t)))
4227 ;; Step over any type suffix operator. Do not let the existence
4228 ;; of these alter the classification of the found type, since
4229 ;; these operators typically are allowed in normal expressions
4230 ;; too.
4231 (when c-opt-type-suffix-key
4232 (while (looking-at c-opt-type-suffix-key)
4233 (goto-char (match-end 1))
4234 (c-forward-syntactic-ws)))
4236 (when c-opt-type-concat-key
4237 ;; Look for a trailing operator that concatenate the type with
4238 ;; a following one, and if so step past that one through a
4239 ;; recursive call.
4240 (setq pos (point))
4241 (let* ((c-promote-possible-types (or (memq res '(t known))
4242 c-promote-possible-types))
4243 ;; If we can't promote then set `c-record-found-types' so that
4244 ;; we can merge in the types from the second part afterwards if
4245 ;; it turns out to be a known type there.
4246 (c-record-found-types (and c-record-type-identifiers
4247 (not c-promote-possible-types))))
4248 (if (and (looking-at c-opt-type-concat-key)
4250 (progn
4251 (goto-char (match-end 1))
4252 (c-forward-syntactic-ws)
4253 (setq res2 (c-forward-type))))
4255 (progn
4256 ;; If either operand certainly is a type then both are, but we
4257 ;; don't let the existence of the operator itself promote two
4258 ;; uncertain types to a certain one.
4259 (cond ((eq res t))
4260 ((eq res2 t)
4261 (c-add-type id-start id-end)
4262 (when c-record-type-identifiers
4263 (c-record-type-id id-range))
4264 (setq res t))
4265 ((eq res 'known))
4266 ((eq res2 'known)
4267 (setq res 'known))
4268 ((eq res 'found))
4269 ((eq res2 'found)
4270 (setq res 'found))
4272 (setq res 'maybe)))
4274 (when (and (eq res t)
4275 (consp c-record-found-types))
4276 ;; Merge in the ranges of any types found by the second
4277 ;; `c-forward-type'.
4278 (setq c-record-type-identifiers
4279 ;; `nconc' doesn't mind that the tail of
4280 ;; `c-record-found-types' is t.
4281 (nconc c-record-found-types
4282 c-record-type-identifiers))))
4284 (goto-char pos))))
4286 (when (and c-record-found-types (memq res '(known found)) id-range)
4287 (setq c-record-found-types
4288 (cons id-range c-record-found-types))))
4290 ;;(message "c-forward-type %s -> %s: %s" start (point) res)
4292 res))
4295 ;; Handling of large scale constructs like statements and declarations.
4297 (defun c-beginning-of-inheritance-list (&optional lim)
4298 ;; Go to the first non-whitespace after the colon that starts a
4299 ;; multiple inheritance introduction. Optional LIM is the farthest
4300 ;; back we should search.
4301 (let* ((lim (or lim (save-excursion
4302 (c-beginning-of-syntax)
4303 (point)))))
4304 (c-with-syntax-table c++-template-syntax-table
4305 (c-backward-token-2 0 t lim)
4306 (while (and (or (looking-at c-symbol-start)
4307 (looking-at "[<,]\\|::"))
4308 (zerop (c-backward-token-2 1 t lim))))
4309 (skip-chars-forward "^:"))))
4311 (defun c-in-method-def-p ()
4312 ;; Return nil if we aren't in a method definition, otherwise the
4313 ;; position of the initial [+-].
4314 (save-excursion
4315 (beginning-of-line)
4316 (and c-opt-method-key
4317 (looking-at c-opt-method-key)
4318 (point))
4321 ;; Contributed by Kevin Ryde <user42@zip.com.au>.
4322 (defun c-in-gcc-asm-p ()
4323 ;; Return non-nil if point is within a gcc \"asm\" block.
4325 ;; This should be called with point inside an argument list.
4327 ;; Only one level of enclosing parentheses is considered, so for
4328 ;; instance `nil' is returned when in a function call within an asm
4329 ;; operand.
4331 (and c-opt-asm-stmt-key
4332 (save-excursion
4333 (beginning-of-line)
4334 (backward-up-list 1)
4335 (c-beginning-of-statement-1 (point-min) nil t)
4336 (looking-at c-opt-asm-stmt-key))))
4338 (defun c-at-toplevel-p ()
4339 "Return a determination as to whether point is at the `top-level'.
4340 Being at the top-level means that point is either outside any
4341 enclosing block (such function definition), or only inside a class,
4342 namespace or other block that contains another declaration level.
4344 If point is not at the top-level (e.g. it is inside a method
4345 definition), then nil is returned. Otherwise, if point is at a
4346 top-level not enclosed within a class definition, t is returned.
4347 Otherwise, a 2-vector is returned where the zeroth element is the
4348 buffer position of the start of the class declaration, and the first
4349 element is the buffer position of the enclosing class's opening
4350 brace."
4351 (let ((paren-state (c-parse-state)))
4352 (or (not (c-most-enclosing-brace paren-state))
4353 (c-search-uplist-for-classkey paren-state))))
4355 (defun c-just-after-func-arglist-p (&optional lim)
4356 ;; Return non-nil if we are between a function's argument list closing
4357 ;; paren and its opening brace. Note that the list close brace
4358 ;; could be followed by a "const" specifier or a member init hanging
4359 ;; colon. LIM is used as bound for some backward buffer searches;
4360 ;; the search might continue past it.
4362 ;; Note: This test is easily fooled. It only works reasonably well
4363 ;; in the situations where `c-guess-basic-syntax' uses it.
4364 (save-excursion
4365 (if (c-mode-is-new-awk-p)
4366 (c-awk-backward-syntactic-ws lim)
4367 (c-backward-syntactic-ws lim))
4368 (let ((checkpoint (point)))
4369 ;; could be looking at const specifier
4370 (if (and (eq (char-before) ?t)
4371 (forward-word -1)
4372 (looking-at "\\<const\\>[^_]"))
4373 (c-backward-syntactic-ws lim)
4374 ;; otherwise, we could be looking at a hanging member init
4375 ;; colon
4376 (goto-char checkpoint)
4377 (while (and
4378 (eq (char-before) ?,)
4379 ;; this will catch member inits with multiple
4380 ;; line arglists
4381 (progn
4382 (forward-char -1)
4383 (c-backward-syntactic-ws (c-point 'bol))
4384 (c-safe (c-backward-sexp 1) t))
4385 (or (not (looking-at "\\s\("))
4386 (c-safe (c-backward-sexp 1) t)))
4387 (c-backward-syntactic-ws lim))
4388 (if (and (eq (char-before) ?:)
4389 (progn
4390 (forward-char -1)
4391 (c-backward-syntactic-ws lim)
4392 (looking-at "\\([ \t\n]\\|\\\\\n\\)*:\\([^:]+\\|$\\)")))
4394 (goto-char checkpoint))
4396 (setq checkpoint (point))
4397 (and (eq (char-before) ?\))
4398 ;; Check that it isn't a cpp expression, e.g. the
4399 ;; expression of an #if directive or the "function header"
4400 ;; of a #define.
4401 (or (not (c-beginning-of-macro))
4402 (and (c-forward-to-cpp-define-body)
4403 (< (point) checkpoint)))
4404 ;; Check if we are looking at an ObjC method def or a class
4405 ;; category.
4406 (not (and c-opt-method-key
4407 (progn
4408 (goto-char checkpoint)
4409 (c-safe (c-backward-sexp) t))
4410 (progn
4411 (c-backward-syntactic-ws lim)
4412 (or (memq (char-before) '(?- ?+))
4413 (and (c-safe (c-forward-sexp -2) t)
4414 (looking-at c-class-key))))))
4415 ;; Pike has compound types that include parens,
4416 ;; e.g. "array(string)". Check that we aren't after one.
4417 (not (and (c-major-mode-is 'pike-mode)
4418 (progn
4419 (goto-char checkpoint)
4420 (c-safe (c-backward-sexp 2) t))
4421 (looking-at c-primitive-type-key)))
4422 ))))
4424 (defun c-in-knr-argdecl (&optional lim)
4425 ;; Return the position of the first argument declaration if point is
4426 ;; inside a K&R style argument declaration list, nil otherwise.
4427 ;; `c-recognize-knr-p' is not checked. If LIM is non-nil, it's a
4428 ;; position that bounds the backward search for the argument list.
4430 ;; Note: A declaration level context is assumed; the test can return
4431 ;; false positives for statements.
4433 (save-excursion
4434 (save-restriction
4436 ;; Go back to the closest preceding normal parenthesis sexp. We
4437 ;; take that as the argument list in the function header. Then
4438 ;; check that it's followed by some symbol before the next ';'
4439 ;; or '{'. If it does, it's the header of the K&R argdecl we're
4440 ;; in.
4441 (if lim (narrow-to-region lim (c-point 'eol)))
4442 (let ((outside-macro (not (c-query-macro-start)))
4443 paren-end)
4445 (catch 'done
4446 (while (if (and (setq paren-end (c-down-list-backward (point)))
4447 (eq (char-after paren-end) ?\)))
4448 (progn
4449 (goto-char (1+ paren-end))
4450 (if outside-macro
4451 (c-beginning-of-macro)))
4452 (throw 'done nil))))
4454 (and (progn
4455 (c-forward-syntactic-ws)
4456 (looking-at "\\w\\|\\s_"))
4458 (save-excursion
4459 ;; The function header in a K&R declaration should only
4460 ;; contain identifiers separated by comma. It should
4461 ;; also contain at least one identifier since there
4462 ;; wouldn't be anything to declare in the K&R region
4463 ;; otherwise.
4464 (when (c-go-up-list-backward paren-end)
4465 (forward-char)
4466 (catch 'knr-ok
4467 (while t
4468 (c-forward-syntactic-ws)
4469 (if (or (looking-at c-known-type-key)
4470 (looking-at c-keywords-regexp))
4471 (throw 'knr-ok nil))
4472 (c-forward-token-2)
4473 (if (eq (char-after) ?,)
4474 (forward-char)
4475 (throw 'knr-ok (and (eq (char-after) ?\))
4476 (= (point) paren-end))))))))
4478 (save-excursion
4479 ;; If it's a K&R declaration then we're now at the
4480 ;; beginning of the function arglist. Check that there
4481 ;; isn't a '=' before it in this statement since that
4482 ;; means it some kind of initialization instead.
4483 (c-syntactic-skip-backward "^;=}{")
4484 (not (eq (char-before) ?=)))
4486 (point))))))
4488 (defun c-skip-conditional ()
4489 ;; skip forward over conditional at point, including any predicate
4490 ;; statements in parentheses. No error checking is performed.
4491 (c-forward-sexp (cond
4492 ;; else if()
4493 ((looking-at (concat "\\<else"
4494 "\\([ \t\n]\\|\\\\\n\\)+"
4495 "if\\>\\([^_]\\|$\\)"))
4497 ;; do, else, try, finally
4498 ((looking-at (concat "\\<\\("
4499 "do\\|else\\|try\\|finally"
4500 "\\)\\>\\([^_]\\|$\\)"))
4502 ;; for, if, while, switch, catch, synchronized, foreach
4503 (t 2))))
4505 (defun c-after-conditional (&optional lim)
4506 ;; If looking at the token after a conditional then return the
4507 ;; position of its start, otherwise return nil.
4508 (save-excursion
4509 (and (zerop (c-backward-token-2 1 t lim))
4510 (or (looking-at c-block-stmt-1-key)
4511 (and (eq (char-after) ?\()
4512 (zerop (c-backward-token-2 1 t lim))
4513 (looking-at c-block-stmt-2-key)))
4514 (point))))
4516 (defsubst c-backward-to-block-anchor (&optional lim)
4517 ;; Assuming point is at a brace that opens a statement block of some
4518 ;; kind, move to the proper anchor point for that block. It might
4519 ;; need to be adjusted further by c-add-stmt-syntax, but the
4520 ;; position at return is suitable as start position for that
4521 ;; function.
4522 (unless (= (point) (c-point 'boi))
4523 (let ((start (c-after-conditional lim)))
4524 (if start
4525 (goto-char start)))))
4527 (defsubst c-backward-to-decl-anchor (&optional lim)
4528 ;; Assuming point is at a brace that opens the block of a top level
4529 ;; declaration of some kind, move to the proper anchor point for
4530 ;; that block.
4531 (unless (= (point) (c-point 'boi))
4532 (c-beginning-of-statement-1 lim)))
4534 (defun c-search-decl-header-end ()
4535 ;; Search forward for the end of the "header" of the current
4536 ;; declaration. That's the position where the definition body
4537 ;; starts, or the first variable initializer, or the ending
4538 ;; semicolon. I.e. search forward for the closest following
4539 ;; (syntactically relevant) '{', '=' or ';' token. Point is left
4540 ;; _after_ the first found token, or at point-max if none is found.
4542 (let ((base (point)))
4543 (if (c-major-mode-is 'c++-mode)
4545 ;; In C++ we need to take special care to handle operator
4546 ;; tokens and those pesky template brackets.
4547 (while (and
4548 (c-syntactic-re-search-forward "[;{<=]" nil 'move t t)
4550 (c-end-of-current-token base)
4551 ;; Handle operator identifiers, i.e. ignore any
4552 ;; operator token preceded by "operator".
4553 (save-excursion
4554 (and (c-safe (c-backward-sexp) t)
4555 (looking-at "operator\\>\\([^_]\\|$\\)")))
4556 (and (eq (char-before) ?<)
4557 (c-with-syntax-table c++-template-syntax-table
4558 (if (c-safe (goto-char (c-up-list-forward (point))))
4560 (goto-char (point-max))
4561 nil)))))
4562 (setq base (point)))
4564 (while (and
4565 (c-syntactic-re-search-forward "[;{=]" nil 'move t t)
4566 (c-end-of-current-token base))
4567 (setq base (point))))))
4569 (defun c-beginning-of-decl-1 (&optional lim)
4570 ;; Go to the beginning of the current declaration, or the beginning
4571 ;; of the previous one if already at the start of it. Point won't
4572 ;; be moved out of any surrounding paren. Return a cons cell on the
4573 ;; form (MOVE . KNR-POS). MOVE is like the return value from
4574 ;; `c-beginning-of-statement-1'. If point skipped over some K&R
4575 ;; style argument declarations (and they are to be recognized) then
4576 ;; KNR-POS is set to the start of the first such argument
4577 ;; declaration, otherwise KNR-POS is nil. If LIM is non-nil, it's a
4578 ;; position that bounds the backward search.
4580 ;; NB: Cases where the declaration continues after the block, as in
4581 ;; "struct foo { ... } bar;", are currently recognized as two
4582 ;; declarations, e.g. "struct foo { ... }" and "bar;" in this case.
4583 (catch 'return
4584 (let* ((start (point))
4585 (last-stmt-start (point))
4586 (move (c-beginning-of-statement-1 lim t t)))
4588 ;; `c-beginning-of-statement-1' stops at a block start, but we
4589 ;; want to continue if the block doesn't begin a top level
4590 ;; construct, i.e. if it isn't preceded by ';', '}', ':', bob,
4591 ;; or an open paren.
4592 (let ((beg (point)) tentative-move)
4593 (while (and
4594 ;; Must check with c-opt-method-key in ObjC mode.
4595 (not (and c-opt-method-key
4596 (looking-at c-opt-method-key)))
4597 (/= last-stmt-start (point))
4598 (progn
4599 (c-backward-syntactic-ws lim)
4600 (not (memq (char-before) '(?\; ?} ?: nil))))
4601 (save-excursion
4602 (backward-char)
4603 (not (looking-at "\\s(")))
4604 ;; Check that we don't move from the first thing in a
4605 ;; macro to its header.
4606 (not (eq (setq tentative-move
4607 (c-beginning-of-statement-1 lim t t))
4608 'macro)))
4609 (setq last-stmt-start beg
4610 beg (point)
4611 move tentative-move))
4612 (goto-char beg))
4614 (when c-recognize-knr-p
4615 (let ((fallback-pos (point)) knr-argdecl-start)
4616 ;; Handle K&R argdecls. Back up after the "statement" jumped
4617 ;; over by `c-beginning-of-statement-1', unless it was the
4618 ;; function body, in which case we're sitting on the opening
4619 ;; brace now. Then test if we're in a K&R argdecl region and
4620 ;; that we started at the other side of the first argdecl in
4621 ;; it.
4622 (unless (eq (char-after) ?{)
4623 (goto-char last-stmt-start))
4624 (if (and (setq knr-argdecl-start (c-in-knr-argdecl lim))
4625 (< knr-argdecl-start start)
4626 (progn
4627 (goto-char knr-argdecl-start)
4628 (not (eq (c-beginning-of-statement-1 lim t t) 'macro))))
4629 (throw 'return
4630 (cons (if (eq (char-after fallback-pos) ?{)
4631 'previous
4632 'same)
4633 knr-argdecl-start))
4634 (goto-char fallback-pos))))
4636 (when c-opt-access-key
4637 ;; Might have ended up before a protection label. This should
4638 ;; perhaps be checked before `c-recognize-knr-p' to be really
4639 ;; accurate, but we know that no language has both.
4640 (while (looking-at c-opt-access-key)
4641 (goto-char (match-end 0))
4642 (c-forward-syntactic-ws)
4643 (when (>= (point) start)
4644 (goto-char start)
4645 (throw 'return (cons 'same nil)))))
4647 ;; `c-beginning-of-statement-1' counts each brace block as a
4648 ;; separate statement, so the result will be 'previous if we've
4649 ;; moved over any. If they were brace list initializers we might
4650 ;; not have moved over a declaration boundary though, so change it
4651 ;; to 'same if we've moved past a '=' before '{', but not ';'.
4652 ;; (This ought to be integrated into `c-beginning-of-statement-1',
4653 ;; so we avoid this extra pass which potentially can search over a
4654 ;; large amount of text.)
4655 (if (and (eq move 'previous)
4656 (c-with-syntax-table (if (c-major-mode-is 'c++-mode)
4657 c++-template-syntax-table
4658 (syntax-table))
4659 (save-excursion
4660 (and (c-syntactic-re-search-forward "[;={]" start t t t)
4661 (eq (char-before) ?=)
4662 (c-syntactic-re-search-forward "[;{]" start t t)
4663 (eq (char-before) ?{)
4664 (c-safe (goto-char (c-up-list-forward (point))) t)
4665 (not (c-syntactic-re-search-forward ";" start t t))))))
4666 (cons 'same nil)
4667 (cons move nil)))))
4669 (defun c-end-of-decl-1 ()
4670 ;; Assuming point is at the start of a declaration (as detected by
4671 ;; e.g. `c-beginning-of-decl-1'), go to the end of it. Unlike
4672 ;; `c-beginning-of-decl-1', this function handles the case when a
4673 ;; block is followed by identifiers in e.g. struct declarations in C
4674 ;; or C++. If a proper end was found then t is returned, otherwise
4675 ;; point is moved as far as possible within the current sexp and nil
4676 ;; is returned. This function doesn't handle macros; use
4677 ;; `c-end-of-macro' instead in those cases.
4678 (let ((start (point))
4679 (decl-syntax-table (if (c-major-mode-is 'c++-mode)
4680 c++-template-syntax-table
4681 (syntax-table))))
4682 (catch 'return
4683 (c-search-decl-header-end)
4685 (when (and c-recognize-knr-p
4686 (eq (char-before) ?\;)
4687 (c-in-knr-argdecl start))
4688 ;; Stopped at the ';' in a K&R argdecl section which is
4689 ;; detected using the same criteria as in
4690 ;; `c-beginning-of-decl-1'. Move to the following block
4691 ;; start.
4692 (c-syntactic-re-search-forward "{" nil 'move t))
4694 (when (eq (char-before) ?{)
4695 ;; Encountered a block in the declaration. Jump over it.
4696 (condition-case nil
4697 (goto-char (c-up-list-forward (point)))
4698 (error (goto-char (point-max))
4699 (throw 'return nil)))
4700 (if (or (not c-opt-block-decls-with-vars-key)
4701 (save-excursion
4702 (c-with-syntax-table decl-syntax-table
4703 (let ((lim (point)))
4704 (goto-char start)
4705 (not (and
4706 ;; Check for `c-opt-block-decls-with-vars-key'
4707 ;; before the first paren.
4708 (c-syntactic-re-search-forward
4709 (concat "[;=\(\[{]\\|\\("
4710 c-opt-block-decls-with-vars-key
4711 "\\)")
4712 lim t t t)
4713 (match-beginning 1)
4714 (not (eq (char-before) ?_))
4715 ;; Check that the first following paren is
4716 ;; the block.
4717 (c-syntactic-re-search-forward "[;=\(\[{]"
4718 lim t t t)
4719 (eq (char-before) ?{)))))))
4720 ;; The declaration doesn't have any of the
4721 ;; `c-opt-block-decls-with-vars' keywords in the
4722 ;; beginning, so it ends here at the end of the block.
4723 (throw 'return t)))
4725 (c-with-syntax-table decl-syntax-table
4726 (while (progn
4727 (if (eq (char-before) ?\;)
4728 (throw 'return t))
4729 (c-syntactic-re-search-forward ";" nil 'move t))))
4730 nil)))
4732 (defun c-beginning-of-member-init-list (&optional limit)
4733 ;; Go to the beginning of a member init list (i.e. just after the
4734 ;; ':') if inside one. Returns t in that case, nil otherwise.
4735 (or limit
4736 (setq limit (point-min)))
4737 (skip-chars-forward " \t")
4739 (if (eq (char-after) ?,)
4740 (forward-char 1)
4741 (c-backward-syntactic-ws limit))
4743 (catch 'exit
4744 (while (and (< limit (point))
4745 (eq (char-before) ?,))
4747 ;; this will catch member inits with multiple
4748 ;; line arglists
4749 (forward-char -1)
4750 (c-backward-syntactic-ws limit)
4751 (if (eq (char-before) ?\))
4752 (unless (c-safe (c-backward-sexp 1))
4753 (throw 'exit nil)))
4754 (c-backward-syntactic-ws limit)
4756 ;; Skip over any template arg to the class. This way with a
4757 ;; syntax table is bogus but it'll have to do for now.
4758 (if (and (eq (char-before) ?>)
4759 (c-major-mode-is 'c++-mode))
4760 (c-with-syntax-table c++-template-syntax-table
4761 (unless (c-safe (c-backward-sexp 1))
4762 (throw 'exit nil))))
4763 (c-safe (c-backward-sexp 1))
4764 (c-backward-syntactic-ws limit)
4766 ;; Skip backwards over a fully::qualified::name.
4767 (while (and (eq (char-before) ?:)
4768 (save-excursion
4769 (forward-char -1)
4770 (eq (char-before) ?:)))
4771 (backward-char 2)
4772 (c-safe (c-backward-sexp 1)))
4774 ;; If we've stepped over a number then this is a bitfield.
4775 (when (and c-opt-bitfield-key
4776 (looking-at "[0-9]"))
4777 (throw 'exit nil))
4779 ;; now continue checking
4780 (c-backward-syntactic-ws limit))
4782 (and (< limit (point))
4783 (eq (char-before) ?:))))
4785 (defun c-search-uplist-for-classkey (paren-state)
4786 ;; search for the containing class, returning a 2 element vector if
4787 ;; found. aref 0 contains the bufpos of the boi of the class key
4788 ;; line, and aref 1 contains the bufpos of the open brace.
4789 (if (null paren-state)
4790 ;; no paren-state means we cannot be inside a class
4792 (let ((carcache (car paren-state))
4793 search-start search-end)
4794 (if (consp carcache)
4795 ;; a cons cell in the first element means that there is some
4796 ;; balanced sexp before the current bufpos. this we can
4797 ;; ignore. the nth 1 and nth 2 elements define for us the
4798 ;; search boundaries
4799 (setq search-start (nth 2 paren-state)
4800 search-end (nth 1 paren-state))
4801 ;; if the car was not a cons cell then nth 0 and nth 1 define
4802 ;; for us the search boundaries
4803 (setq search-start (nth 1 paren-state)
4804 search-end (nth 0 paren-state)))
4805 ;; if search-end is nil, or if the search-end character isn't an
4806 ;; open brace, we are definitely not in a class
4807 (if (or (not search-end)
4808 (< search-end (point-min))
4809 (not (eq (char-after search-end) ?{)))
4811 ;; now, we need to look more closely at search-start. if
4812 ;; search-start is nil, then our start boundary is really
4813 ;; point-min.
4814 (if (not search-start)
4815 (setq search-start (point-min))
4816 ;; if search-start is a cons cell, then we can start
4817 ;; searching from the end of the balanced sexp just ahead of
4818 ;; us
4819 (if (consp search-start)
4820 (setq search-start (cdr search-start))
4821 ;; Otherwise we start searching within the surrounding paren sexp.
4822 (setq search-start (1+ search-start))))
4823 ;; now we can do a quick regexp search from search-start to
4824 ;; search-end and see if we can find a class key. watch for
4825 ;; class like strings in literals
4826 (save-excursion
4827 (save-restriction
4828 (goto-char search-start)
4829 (let (foundp class match-end)
4830 (while (and (not foundp)
4831 (progn
4832 (c-forward-syntactic-ws search-end)
4833 (> search-end (point)))
4834 ;; Add one to the search limit, to allow
4835 ;; matching of the "{" in the regexp.
4836 (re-search-forward c-decl-block-key
4837 (1+ search-end)
4839 (setq class (match-beginning 0)
4840 match-end (match-end 0))
4841 (goto-char class)
4842 (if (c-in-literal search-start)
4843 (goto-char match-end) ; its in a comment or string, ignore
4844 (c-skip-ws-forward)
4845 (setq foundp (vector (c-point 'boi) search-end))
4846 (cond
4847 ;; check for embedded keywords
4848 ((let ((char (char-after (1- class))))
4849 (and char
4850 (memq (char-syntax char) '(?w ?_))))
4851 (goto-char match-end)
4852 (setq foundp nil))
4853 ;; make sure we're really looking at the start of a
4854 ;; class definition, and not an ObjC method.
4855 ((and c-opt-method-key
4856 (re-search-forward c-opt-method-key search-end t)
4857 (not (c-in-literal class)))
4858 (setq foundp nil))
4859 ;; Check if this is an anonymous inner class.
4860 ((and c-opt-inexpr-class-key
4861 (looking-at c-opt-inexpr-class-key))
4862 (while (and (zerop (c-forward-token-2 1 t))
4863 (looking-at "(\\|\\w\\|\\s_\\|\\.")))
4864 (if (eq (point) search-end)
4865 ;; We're done. Just trap this case in the cond.
4867 ;; False alarm; all conditions aren't satisfied.
4868 (setq foundp nil)))
4869 ;; Its impossible to define a regexp for this, and
4870 ;; nearly so to do it programmatically.
4872 ;; ; picks up forward decls
4873 ;; = picks up init lists
4874 ;; ) picks up return types
4875 ;; > picks up templates, but remember that we can
4876 ;; inherit from templates!
4877 ((let ((skipchars "^;=)"))
4878 ;; try to see if we found the `class' keyword
4879 ;; inside a template arg list
4880 (save-excursion
4881 (skip-chars-backward "^<>" search-start)
4882 (if (eq (char-before) ?<)
4883 (setq skipchars (concat skipchars ">"))))
4884 (while (progn
4885 (skip-chars-forward skipchars search-end)
4886 (c-in-literal class))
4887 (forward-char))
4888 (/= (point) search-end))
4889 (setq foundp nil))
4891 foundp))
4892 )))))
4894 (defun c-inside-bracelist-p (containing-sexp paren-state)
4895 ;; return the buffer position of the beginning of the brace list
4896 ;; statement if we're inside a brace list, otherwise return nil.
4897 ;; CONTAINING-SEXP is the buffer pos of the innermost containing
4898 ;; paren. BRACE-STATE is the remainder of the state of enclosing
4899 ;; braces
4901 ;; N.B.: This algorithm can potentially get confused by cpp macros
4902 ;; places in inconvenient locations. Its a trade-off we make for
4903 ;; speed.
4905 ;; This will pick up brace list declarations.
4906 (c-safe
4907 (save-excursion
4908 (goto-char containing-sexp)
4909 (c-forward-sexp -1)
4910 (let (bracepos)
4911 (if (and (or (looking-at c-brace-list-key)
4912 (progn (c-forward-sexp -1)
4913 (looking-at c-brace-list-key)))
4914 (setq bracepos (c-down-list-forward (point)))
4915 (not (c-crosses-statement-barrier-p (point)
4916 (- bracepos 2))))
4917 (point)))))
4918 ;; this will pick up array/aggregate init lists, even if they are nested.
4919 (save-excursion
4920 (let ((class-key
4921 ;; Pike can have class definitions anywhere, so we must
4922 ;; check for the class key here.
4923 (and (c-major-mode-is 'pike-mode)
4924 c-decl-block-key))
4925 bufpos braceassignp lim next-containing)
4926 (while (and (not bufpos)
4927 containing-sexp)
4928 (when paren-state
4929 (if (consp (car paren-state))
4930 (setq lim (cdr (car paren-state))
4931 paren-state (cdr paren-state))
4932 (setq lim (car paren-state)))
4933 (when paren-state
4934 (setq next-containing (car paren-state)
4935 paren-state (cdr paren-state))))
4936 (goto-char containing-sexp)
4937 (if (c-looking-at-inexpr-block next-containing next-containing)
4938 ;; We're in an in-expression block of some kind. Do not
4939 ;; check nesting. We deliberately set the limit to the
4940 ;; containing sexp, so that c-looking-at-inexpr-block
4941 ;; doesn't check for an identifier before it.
4942 (setq containing-sexp nil)
4943 ;; see if the open brace is preceded by = or [...] in
4944 ;; this statement, but watch out for operator=
4945 (setq braceassignp 'dontknow)
4946 (c-backward-token-2 1 t lim)
4947 ;; Checks to do only on the first sexp before the brace.
4948 (when (and c-opt-inexpr-brace-list-key
4949 (eq (char-after) ?\[))
4950 ;; In Java, an initialization brace list may follow
4951 ;; directly after "new Foo[]", so check for a "new"
4952 ;; earlier.
4953 (while (eq braceassignp 'dontknow)
4954 (setq braceassignp
4955 (cond ((/= (c-backward-token-2 1 t lim) 0) nil)
4956 ((looking-at c-opt-inexpr-brace-list-key) t)
4957 ((looking-at "\\sw\\|\\s_\\|[.[]")
4958 ;; Carry on looking if this is an
4959 ;; identifier (may contain "." in Java)
4960 ;; or another "[]" sexp.
4961 'dontknow)
4962 (t nil)))))
4963 ;; Checks to do on all sexps before the brace, up to the
4964 ;; beginning of the statement.
4965 (while (eq braceassignp 'dontknow)
4966 (cond ((eq (char-after) ?\;)
4967 (setq braceassignp nil))
4968 ((and class-key
4969 (looking-at class-key))
4970 (setq braceassignp nil))
4971 ((eq (char-after) ?=)
4972 ;; We've seen a =, but must check earlier tokens so
4973 ;; that it isn't something that should be ignored.
4974 (setq braceassignp 'maybe)
4975 (while (and (eq braceassignp 'maybe)
4976 (zerop (c-backward-token-2 1 t lim)))
4977 (setq braceassignp
4978 (cond
4979 ;; Check for operator =
4980 ((looking-at "operator\\>[^_]") nil)
4981 ;; Check for `<opchar>= in Pike.
4982 ((and (c-major-mode-is 'pike-mode)
4983 (or (eq (char-after) ?`)
4984 ;; Special case for Pikes
4985 ;; `[]=, since '[' is not in
4986 ;; the punctuation class.
4987 (and (eq (char-after) ?\[)
4988 (eq (char-before) ?`))))
4989 nil)
4990 ((looking-at "\\s.") 'maybe)
4991 ;; make sure we're not in a C++ template
4992 ;; argument assignment
4993 ((and
4994 (c-major-mode-is 'c++-mode)
4995 (save-excursion
4996 (let ((here (point))
4997 (pos< (progn
4998 (skip-chars-backward "^<>")
4999 (point))))
5000 (and (eq (char-before) ?<)
5001 (not (c-crosses-statement-barrier-p
5002 pos< here))
5003 (not (c-in-literal))
5004 ))))
5005 nil)
5006 (t t))))))
5007 (if (and (eq braceassignp 'dontknow)
5008 (/= (c-backward-token-2 1 t lim) 0))
5009 (setq braceassignp nil)))
5010 (if (not braceassignp)
5011 (if (eq (char-after) ?\;)
5012 ;; Brace lists can't contain a semicolon, so we're done.
5013 (setq containing-sexp nil)
5014 ;; Go up one level.
5015 (setq containing-sexp next-containing
5016 lim nil
5017 next-containing nil))
5018 ;; we've hit the beginning of the aggregate list
5019 (c-beginning-of-statement-1
5020 (c-most-enclosing-brace paren-state))
5021 (setq bufpos (point))))
5023 bufpos))
5026 (defun c-looking-at-special-brace-list (&optional lim)
5027 ;; If we're looking at the start of a pike-style list, ie `({ })',
5028 ;; `([ ])', `(< >)' etc, a cons of a cons of its starting and ending
5029 ;; positions and its entry in c-special-brace-lists is returned, nil
5030 ;; otherwise. The ending position is nil if the list is still open.
5031 ;; LIM is the limit for forward search. The point may either be at
5032 ;; the `(' or at the following paren character. Tries to check the
5033 ;; matching closer, but assumes it's correct if no balanced paren is
5034 ;; found (i.e. the case `({ ... } ... )' is detected as _not_ being
5035 ;; a special brace list).
5036 (if c-special-brace-lists
5037 (condition-case ()
5038 (save-excursion
5039 (let ((beg (point))
5040 inner-beg end type)
5041 (c-forward-syntactic-ws)
5042 (if (eq (char-after) ?\()
5043 (progn
5044 (forward-char 1)
5045 (c-forward-syntactic-ws)
5046 (setq inner-beg (point))
5047 (setq type (assq (char-after) c-special-brace-lists)))
5048 (if (setq type (assq (char-after) c-special-brace-lists))
5049 (progn
5050 (setq inner-beg (point))
5051 (c-backward-syntactic-ws)
5052 (forward-char -1)
5053 (setq beg (if (eq (char-after) ?\()
5054 (point)
5055 nil)))))
5056 (if (and beg type)
5057 (if (and (c-safe
5058 (goto-char beg)
5059 (c-forward-sexp 1)
5060 (setq end (point))
5061 (= (char-before) ?\)))
5062 (c-safe
5063 (goto-char inner-beg)
5064 (if (looking-at "\\s(")
5065 ;; Check balancing of the inner paren
5066 ;; below.
5067 (progn
5068 (c-forward-sexp 1)
5070 ;; If the inner char isn't a paren then
5071 ;; we can't check balancing, so just
5072 ;; check the char before the outer
5073 ;; closing paren.
5074 (goto-char end)
5075 (backward-char)
5076 (c-backward-syntactic-ws)
5077 (= (char-before) (cdr type)))))
5078 (if (or (/= (char-syntax (char-before)) ?\))
5079 (= (progn
5080 (c-forward-syntactic-ws)
5081 (point))
5082 (1- end)))
5083 (cons (cons beg end) type))
5084 (cons (list beg) type)))))
5085 (error nil))))
5087 (defun c-looking-at-bos (&optional lim)
5088 ;; Return non-nil if between two statements or declarations, assuming
5089 ;; point is not inside a literal or comment.
5090 (save-excursion
5091 (c-backward-syntactic-ws lim)
5092 (or (bobp)
5093 ;; Return t if at the start inside some parenthesis expression
5094 ;; too, to catch macros that have statements as arguments.
5095 (memq (char-before) '(?\; ?} ?\())
5096 (and (eq (char-before) ?{)
5097 (not (and c-special-brace-lists
5098 (progn (backward-char)
5099 (c-looking-at-special-brace-list))))))))
5101 (defun c-looking-at-inexpr-block (lim containing-sexp)
5102 ;; Returns non-nil if we're looking at the beginning of a block
5103 ;; inside an expression. The value returned is actually a cons of
5104 ;; either 'inlambda, 'inexpr-statement or 'inexpr-class and the
5105 ;; position of the beginning of the construct. LIM limits the
5106 ;; backward search. CONTAINING-SEXP is the start position of the
5107 ;; closest containing list. If it's nil, the containing paren isn't
5108 ;; used to decide whether we're inside an expression or not. If
5109 ;; both LIM and CONTAINING-SEXP is used, LIM needs to be farther
5110 ;; back.
5111 (save-excursion
5112 (let ((res 'maybe) passed-bracket
5113 (closest-lim (or containing-sexp lim (point-min)))
5114 ;; Look at the character after point only as a last resort
5115 ;; when we can't disambiguate.
5116 (block-follows (and (eq (char-after) ?{) (point))))
5117 (while (and (eq res 'maybe)
5118 (progn (c-backward-syntactic-ws)
5119 (> (point) closest-lim))
5120 (not (bobp))
5121 (progn (backward-char)
5122 (looking-at "[\]\).]\\|\\w\\|\\s_"))
5123 (progn (forward-char)
5124 (goto-char (scan-sexps (point) -1))))
5125 (setq res
5126 (cond
5127 ((and block-follows
5128 c-opt-inexpr-class-key
5129 (looking-at c-opt-inexpr-class-key))
5130 (and (not passed-bracket)
5131 (or (not (looking-at c-class-key))
5132 ;; If the class definition is at the start of
5133 ;; a statement, we don't consider it an
5134 ;; in-expression class.
5135 (let ((prev (point)))
5136 (while (and
5137 (= (c-backward-token-2 1 nil closest-lim) 0)
5138 (eq (char-syntax (char-after)) ?w))
5139 (setq prev (point)))
5140 (goto-char prev)
5141 (not (c-looking-at-bos)))
5142 ;; Also, in Pike we treat it as an
5143 ;; in-expression class if it's used in an
5144 ;; object clone expression.
5145 (save-excursion
5146 (and (c-major-mode-is 'pike-mode)
5147 (progn (goto-char block-follows)
5148 (zerop (c-forward-token-2 1 t)))
5149 (eq (char-after) ?\())))
5150 (cons 'inexpr-class (point))))
5151 ((and c-opt-inexpr-block-key
5152 (looking-at c-opt-inexpr-block-key))
5153 (cons 'inexpr-statement (point)))
5154 ((and c-opt-lambda-key
5155 (looking-at c-opt-lambda-key))
5156 (cons 'inlambda (point)))
5157 ((and c-opt-block-stmt-key
5158 (looking-at c-opt-block-stmt-key))
5159 nil)
5161 (if (eq (char-after) ?\[)
5162 (setq passed-bracket t))
5163 'maybe))))
5164 (if (eq res 'maybe)
5165 (when (and block-follows
5166 containing-sexp
5167 (eq (char-after containing-sexp) ?\())
5168 (goto-char containing-sexp)
5169 (if (or (save-excursion
5170 (c-backward-syntactic-ws lim)
5171 (and (> (point) (or lim (point-min)))
5172 (c-on-identifier)))
5173 (and c-special-brace-lists
5174 (c-looking-at-special-brace-list)))
5176 (cons 'inexpr-statement (point))))
5177 res))))
5179 (defun c-looking-at-inexpr-block-backward (paren-state)
5180 ;; Returns non-nil if we're looking at the end of an in-expression
5181 ;; block, otherwise the same as `c-looking-at-inexpr-block'.
5182 ;; PAREN-STATE is the paren state relevant at the current position.
5183 (save-excursion
5184 ;; We currently only recognize a block.
5185 (let ((here (point))
5186 (elem (car-safe paren-state))
5187 containing-sexp)
5188 (when (and (consp elem)
5189 (progn (goto-char (cdr elem))
5190 (c-forward-syntactic-ws here)
5191 (= (point) here)))
5192 (goto-char (car elem))
5193 (if (setq paren-state (cdr paren-state))
5194 (setq containing-sexp (car-safe paren-state)))
5195 (c-looking-at-inexpr-block (c-safe-position containing-sexp
5196 paren-state)
5197 containing-sexp)))))
5199 (defun c-narrow-out-enclosing-class (paren-state lim)
5200 ;; Narrow the buffer so that the enclosing class is hidden. Uses
5201 ;; and returns the value from c-search-uplist-for-classkey.
5202 (setq paren-state (c-whack-state-after (point) paren-state))
5203 (let (inclass-p)
5204 (and paren-state
5205 (setq inclass-p (c-search-uplist-for-classkey paren-state))
5206 (narrow-to-region
5207 (progn
5208 (goto-char (1+ (aref inclass-p 1)))
5209 (c-skip-ws-forward lim)
5210 ;; if point is now left of the class opening brace, we're
5211 ;; hosed, so try a different tact
5212 (if (<= (point) (aref inclass-p 1))
5213 (progn
5214 (goto-char (1+ (aref inclass-p 1)))
5215 (c-forward-syntactic-ws lim)))
5216 (point))
5217 ;; end point is the end of the current line
5218 (progn
5219 (goto-char lim)
5220 (c-point 'eol))))
5221 ;; return the class vector
5222 inclass-p))
5225 ;; `c-guess-basic-syntax' and the functions that precedes it below
5226 ;; implements the main decision tree for determining the syntactic
5227 ;; analysis of the current line of code.
5229 ;; Dynamically bound to t when `c-guess-basic-syntax' is called during
5230 ;; auto newline analysis.
5231 (defvar c-auto-newline-analysis nil)
5233 (defsubst c-add-syntax (symbol &rest args)
5234 ;; A simple function to prepend a new syntax element to
5235 ;; `c-syntactic-context'. Using `setq' on it is unsafe since it
5236 ;; should always be dynamically bound but since we read it first
5237 ;; we'll fail properly anyway if this function is misused.
5238 (setq c-syntactic-context (cons (cons symbol args)
5239 c-syntactic-context)))
5241 (defsubst c-append-syntax (symbol &rest args)
5242 ;; Like `c-add-syntax' but appends to the end of the syntax list.
5243 ;; (Normally not necessary.)
5244 (setq c-syntactic-context (nconc c-syntactic-context
5245 (list (cons symbol args)))))
5247 (defun c-add-stmt-syntax (syntax-symbol
5248 syntax-extra-args
5249 stop-at-boi-only
5250 at-block-start
5251 containing-sexp
5252 paren-state)
5253 ;; Do the generic processing to anchor the given syntax symbol on
5254 ;; the preceding statement: Skip over any labels and containing
5255 ;; statements on the same line, and then search backward until we
5256 ;; find a statement or block start that begins at boi without a
5257 ;; label or comment.
5259 ;; Point is assumed to be at the prospective anchor point for the
5260 ;; given SYNTAX-SYMBOL. More syntax entries are added if we need to
5261 ;; skip past open parens and containing statements. All the added
5262 ;; syntax elements will get the same anchor point.
5264 ;; SYNTAX-EXTRA-ARGS are a list of the extra arguments for the
5265 ;; syntax symbol. They are appended after the anchor point.
5267 ;; If STOP-AT-BOI-ONLY is nil, we might stop in the middle of the
5268 ;; line if another statement precedes the current one on this line.
5270 ;; If AT-BLOCK-START is non-nil, point is taken to be at the
5271 ;; beginning of a block or brace list, which then might be nested
5272 ;; inside an expression. If AT-BLOCK-START is nil, this is found
5273 ;; out by checking whether the character at point is "{" or not.
5274 (if (= (point) (c-point 'boi))
5275 ;; This is by far the most common case, so let's give it special
5276 ;; treatment.
5277 (apply 'c-add-syntax syntax-symbol (point) syntax-extra-args)
5279 (let ((savepos (point))
5280 (syntax-last c-syntactic-context)
5281 (boi (c-point 'boi))
5282 (prev-paren (if at-block-start ?{ (char-after)))
5283 step-type step-tmp at-comment special-list)
5284 (apply 'c-add-syntax syntax-symbol nil syntax-extra-args)
5286 ;; Begin by skipping any labels and containing statements that
5287 ;; are on the same line.
5288 (while (and (/= (point) boi)
5289 (if (memq (setq step-tmp
5290 (c-beginning-of-statement-1 boi nil t))
5291 '(up label))
5293 (goto-char savepos)
5294 nil)
5295 (/= (point) savepos))
5296 (setq savepos (point)
5297 step-type step-tmp))
5299 (catch 'done
5300 ;; Loop if we have to back out of the containing block.
5301 (while
5302 (progn
5304 ;; Loop if we have to back up another statement.
5305 (while
5306 (progn
5308 ;; Always start by skipping over any comments that
5309 ;; stands between the statement and boi.
5310 (while (and (/= (setq savepos (point)) boi)
5311 (c-backward-single-comment))
5312 (setq at-comment t
5313 boi (c-point 'boi)))
5314 (goto-char savepos)
5316 (and
5317 (or at-comment
5318 (eq step-type 'label)
5319 (/= savepos boi))
5321 (let ((save-step-type step-type))
5322 ;; Current position might not be good enough;
5323 ;; skip backward another statement.
5324 (setq step-type (c-beginning-of-statement-1
5325 containing-sexp))
5327 (if (and (not stop-at-boi-only)
5328 (/= savepos boi)
5329 (memq step-type '(up previous)))
5330 ;; If stop-at-boi-only is nil, we shouldn't
5331 ;; back up over previous or containing
5332 ;; statements to try to reach boi, so go
5333 ;; back to the last position and exit.
5334 (progn
5335 (goto-char savepos)
5336 nil)
5337 (if (and (not stop-at-boi-only)
5338 (memq step-type '(up previous beginning)))
5339 ;; If we've moved into another statement
5340 ;; then we should no longer try to stop
5341 ;; after boi.
5342 (setq stop-at-boi-only t))
5344 ;; Record this a substatement if we skipped up
5345 ;; one level, but not if we're still on the
5346 ;; same line. This so e.g. a sequence of "else
5347 ;; if" clauses won't indent deeper and deeper.
5348 (when (and (eq step-type 'up)
5349 (< (point) boi))
5350 (c-add-syntax 'substatement nil))
5352 (setq boi (c-point 'boi))
5353 (if (= (point) savepos)
5354 (progn
5355 (setq step-type save-step-type)
5356 nil)
5357 t)))))
5359 (setq savepos (point)
5360 at-comment nil))
5361 (setq at-comment nil)
5363 (when (and containing-sexp
5364 (if (memq step-type '(nil same))
5365 (/= (point) boi)
5366 (eq step-type 'label)))
5367 (goto-char containing-sexp)
5369 ;; Don't stop in the middle of a special brace list opener
5370 ;; like "({".
5371 (when (and c-special-brace-lists
5372 (setq special-list
5373 (c-looking-at-special-brace-list)))
5374 (setq containing-sexp (car (car special-list)))
5375 (goto-char containing-sexp))
5377 (setq paren-state (c-whack-state-after containing-sexp
5378 paren-state)
5379 containing-sexp (c-most-enclosing-brace paren-state)
5380 savepos (point)
5381 boi (c-point 'boi))
5383 (if (eq (setq prev-paren (char-after)) ?\()
5384 (progn
5385 (c-backward-syntactic-ws containing-sexp)
5386 (when (/= savepos boi)
5387 (if (and (or (not (looking-at "\\>"))
5388 (not (c-on-identifier)))
5389 (not special-list)
5390 (save-excursion
5391 (c-forward-syntactic-ws)
5392 (forward-char)
5393 (c-forward-syntactic-ws)
5394 (eq (char-after) ?{)))
5395 ;; We're in an in-expression statement.
5396 ;; This syntactic element won't get an anchor pos.
5397 (c-add-syntax 'inexpr-statement)
5398 (c-add-syntax 'arglist-cont-nonempty nil savepos)))
5399 (goto-char (max boi
5400 (if containing-sexp
5401 (1+ containing-sexp)
5402 (point-min))))
5403 (setq step-type 'same))
5404 (setq step-type
5405 (c-beginning-of-statement-1 containing-sexp)))
5407 (let ((at-bod (and (eq step-type 'same)
5408 (/= savepos (point))
5409 (eq prev-paren ?{))))
5411 (when (= savepos boi)
5412 ;; If the open brace was at boi, we're always
5413 ;; done. The c-beginning-of-statement-1 call
5414 ;; above is necessary anyway, to decide the type
5415 ;; of block-intro to add.
5416 (goto-char savepos)
5417 (setq savepos nil))
5419 (when (eq prev-paren ?{)
5420 (c-add-syntax (if at-bod
5421 'defun-block-intro
5422 'statement-block-intro)
5423 nil))
5425 (when (and (not at-bod) savepos)
5426 ;; Loop if the brace wasn't at boi, and we didn't
5427 ;; arrive at a defun block.
5428 (if (eq step-type 'same)
5429 ;; Avoid backing up another sexp if the point
5430 ;; we're at now is found to be good enough in
5431 ;; the loop above.
5432 (setq step-type nil))
5433 (if (and (not stop-at-boi-only)
5434 (memq step-type '(up previous beginning)))
5435 (setq stop-at-boi-only t))
5436 (setq boi (c-point 'boi)))))
5439 ;; Fill in the current point as the anchor for all the symbols
5440 ;; added above.
5441 (let ((p c-syntactic-context))
5442 (while (not (eq p syntax-last))
5443 (if (cdr (car p))
5444 (setcar (cdr (car p)) (point)))
5445 (setq p (cdr p))))
5449 (defun c-add-class-syntax (symbol classkey paren-state)
5450 ;; The inclass and class-close syntactic symbols are added in
5451 ;; several places and some work is needed to fix everything.
5452 ;; Therefore it's collected here.
5453 (save-restriction
5454 (widen)
5455 (let (inexpr anchor containing-sexp)
5456 (goto-char (aref classkey 1))
5457 (if (and (eq symbol 'inclass) (= (point) (c-point 'boi)))
5458 (c-add-syntax symbol (setq anchor (point)))
5459 (c-add-syntax symbol (setq anchor (aref classkey 0)))
5460 (if (and c-opt-inexpr-class-key
5461 (setq containing-sexp (c-most-enclosing-brace paren-state
5462 (point))
5463 inexpr (cdr (c-looking-at-inexpr-block
5464 (c-safe-position containing-sexp
5465 paren-state)
5466 containing-sexp)))
5467 (/= inexpr (c-point 'boi inexpr)))
5468 (c-add-syntax 'inexpr-class)))
5469 anchor)))
5471 (defun c-guess-continued-construct (indent-point
5472 char-after-ip
5473 beg-of-same-or-containing-stmt
5474 containing-sexp
5475 paren-state)
5476 ;; This function contains the decision tree reached through both
5477 ;; cases 18 and 10. It's a continued statement or top level
5478 ;; construct of some kind.
5480 (let (special-brace-list)
5481 (goto-char indent-point)
5482 (skip-chars-forward " \t")
5484 (cond
5485 ;; (CASE A removed.)
5486 ;; CASE B: open braces for class or brace-lists
5487 ((setq special-brace-list
5488 (or (and c-special-brace-lists
5489 (c-looking-at-special-brace-list))
5490 (eq char-after-ip ?{)))
5492 (cond
5493 ;; CASE B.1: class-open
5494 ((save-excursion
5495 (skip-chars-forward "{")
5496 (let ((decl (c-search-uplist-for-classkey (c-parse-state))))
5497 (and decl
5498 (setq beg-of-same-or-containing-stmt (aref decl 0)))
5500 (c-add-syntax 'class-open beg-of-same-or-containing-stmt))
5502 ;; CASE B.2: brace-list-open
5503 ((or (consp special-brace-list)
5504 (save-excursion
5505 (goto-char beg-of-same-or-containing-stmt)
5506 (c-syntactic-re-search-forward "=\\([^=]\\|$\\)"
5507 indent-point t t t)))
5508 ;; The most semantically accurate symbol here is
5509 ;; brace-list-open, but we normally report it simply as a
5510 ;; statement-cont. The reason is that one normally adjusts
5511 ;; brace-list-open for brace lists as top-level constructs,
5512 ;; and brace lists inside statements is a completely different
5513 ;; context. C.f. case 5A.3.
5514 (c-beginning-of-statement-1 containing-sexp)
5515 (c-add-stmt-syntax (if c-auto-newline-analysis
5516 ;; Turn off the dwim above when we're
5517 ;; analyzing the nature of the brace
5518 ;; for the auto newline feature.
5519 'brace-list-open
5520 'statement-cont)
5521 nil nil nil
5522 containing-sexp paren-state))
5524 ;; CASE B.3: The body of a function declared inside a normal
5525 ;; block. Can occur e.g. in Pike and when using gcc
5526 ;; extensions, but watch out for macros followed by blocks.
5527 ;; C.f. cases E, 16F and 17G.
5528 ((and (not (c-looking-at-bos))
5529 (eq (c-beginning-of-statement-1 containing-sexp nil nil t)
5530 'same)
5531 (save-excursion
5532 ;; Look for a type followed by a symbol, i.e. the start of a
5533 ;; function declaration. Doesn't work for declarations like
5534 ;; "int *foo() ..."; we'd need to refactor the more competent
5535 ;; analysis in `c-font-lock-declarations' for that.
5536 (and (c-forward-type)
5537 (progn
5538 (c-forward-syntactic-ws)
5539 (looking-at c-symbol-start)))))
5540 (c-add-stmt-syntax 'defun-open nil t nil
5541 containing-sexp paren-state))
5543 ;; CASE B.4: Continued statement with block open. The most
5544 ;; accurate analysis is perhaps `statement-cont' together with
5545 ;; `block-open' but we play DWIM and use `substatement-open'
5546 ;; instead. The rationaly is that this typically is a macro
5547 ;; followed by a block which makes it very similar to a
5548 ;; statement with a substatement block.
5550 (c-add-stmt-syntax 'substatement-open nil nil nil
5551 containing-sexp paren-state))
5554 ;; CASE C: iostream insertion or extraction operator
5555 ((and (looking-at "\\(<<\\|>>\\)\\([^=]\\|$\\)")
5556 (save-excursion
5557 (goto-char beg-of-same-or-containing-stmt)
5558 ;; If there is no preceding streamop in the statement
5559 ;; then indent this line as a normal statement-cont.
5560 (when (c-syntactic-re-search-forward
5561 "\\(<<\\|>>\\)\\([^=]\\|$\\)" indent-point 'move t t)
5562 (c-add-syntax 'stream-op (c-point 'boi))
5563 t))))
5565 ;; CASE E: In the "K&R region" of a function declared inside a
5566 ;; normal block. C.f. case B.3.
5567 ((and (save-excursion
5568 ;; Check that the next token is a '{'. This works as
5569 ;; long as no language that allows nested function
5570 ;; definitions allows stuff like member init lists, K&R
5571 ;; declarations or throws clauses there.
5573 ;; Note that we do a forward search for something ahead
5574 ;; of the indentation line here. That's not good since
5575 ;; the user might not have typed it yet. Unfortunately
5576 ;; it's exceedingly tricky to recognize a function
5577 ;; prototype in a code block without resorting to this.
5578 (c-forward-syntactic-ws)
5579 (eq (char-after) ?{))
5580 (not (c-looking-at-bos))
5581 (eq (c-beginning-of-statement-1 containing-sexp nil nil t)
5582 'same)
5583 (save-excursion
5584 ;; Look for a type followed by a symbol, i.e. the start of a
5585 ;; function declaration. Doesn't work for declarations like "int
5586 ;; *foo() ..."; we'd need to refactor the more competent analysis
5587 ;; in `c-font-lock-declarations' for that.
5588 (and (c-forward-type)
5589 (progn
5590 (c-forward-syntactic-ws)
5591 (looking-at c-symbol-start)))))
5592 (c-add-stmt-syntax 'func-decl-cont nil t nil
5593 containing-sexp paren-state))
5595 ;; CASE D: continued statement.
5597 (c-beginning-of-statement-1 containing-sexp)
5598 (c-add-stmt-syntax 'statement-cont nil nil nil
5599 containing-sexp paren-state))
5602 (defun c-guess-basic-syntax ()
5603 "Return the syntactic context of the current line.
5604 This function does not do any hidden buffer changes."
5605 (save-excursion
5606 (save-restriction
5607 (beginning-of-line)
5608 (c-save-buffer-state
5609 ((indent-point (point))
5610 (case-fold-search nil)
5611 (paren-state (c-parse-state))
5612 literal containing-sexp char-before-ip char-after-ip lim
5613 c-syntactic-context placeholder c-in-literal-cache step-type
5614 tmpsymbol keyword injava-inher special-brace-list
5615 ;; narrow out any enclosing class or extern "C" block
5616 (inclass-p (c-narrow-out-enclosing-class paren-state
5617 indent-point))
5618 ;; `c-state-cache' is shadowed here so that we don't
5619 ;; throw it away due to the narrowing that might be done
5620 ;; by the function above. That means we must not do any
5621 ;; changes during the execution of this function, since
5622 ;; `c-invalidate-state-cache' then would change this local
5623 ;; variable and leave a bogus value in the global one.
5624 (c-state-cache (if inclass-p
5625 (c-whack-state-before (point-min) paren-state)
5626 paren-state))
5627 (c-state-cache-start (point-min))
5628 inenclosing-p macro-start in-macro-expr
5629 ;; There's always at most one syntactic element which got
5630 ;; a relpos. It's stored in syntactic-relpos.
5631 syntactic-relpos
5632 (c-stmt-delim-chars c-stmt-delim-chars))
5633 ;; Check for meta top-level enclosing constructs such as
5634 ;; extern language definitions.
5635 (save-excursion
5636 (save-restriction
5637 (widen)
5638 (when (and inclass-p
5639 (progn
5640 (goto-char (aref inclass-p 0))
5641 (looking-at c-other-decl-block-key)))
5642 (setq inenclosing-p (match-string 1))
5643 (if (string-equal inenclosing-p "extern")
5644 ;; Compatibility with legacy choice of name for the
5645 ;; extern-lang syntactic symbols.
5646 (setq inenclosing-p "extern-lang")))))
5648 ;; Init some position variables:
5650 ;; containing-sexp is the open paren of the closest
5651 ;; surrounding sexp or nil if there is none that hasn't been
5652 ;; narrowed out.
5654 ;; lim is the position after the closest preceding brace sexp
5655 ;; (nested sexps are ignored), or the position after
5656 ;; containing-sexp if there is none, or (point-min) if
5657 ;; containing-sexp is nil.
5659 ;; c-state-cache is the state from c-parse-state at
5660 ;; indent-point, without any parens outside the region
5661 ;; narrowed by c-narrow-out-enclosing-class.
5663 ;; paren-state is the state from c-parse-state outside
5664 ;; containing-sexp, or at indent-point if containing-sexp is
5665 ;; nil. paren-state is not limited to the narrowed region, as
5666 ;; opposed to c-state-cache.
5667 (if c-state-cache
5668 (progn
5669 (setq containing-sexp (car paren-state)
5670 paren-state (cdr paren-state))
5671 (if (consp containing-sexp)
5672 (progn
5673 (setq lim (cdr containing-sexp))
5674 (if (cdr c-state-cache)
5675 ;; Ignore balanced paren. The next entry
5676 ;; can't be another one.
5677 (setq containing-sexp (car (cdr c-state-cache))
5678 paren-state (cdr paren-state))
5679 ;; If there is no surrounding open paren then
5680 ;; put the last balanced pair back on paren-state.
5681 (setq paren-state (cons containing-sexp paren-state)
5682 containing-sexp nil)))
5683 (setq lim (1+ containing-sexp))))
5684 (setq lim (point-min)))
5686 ;; If we're in a parenthesis list then ',' delimits the
5687 ;; "statements" rather than being an operator (with the
5688 ;; exception of the "for" clause). This difference is
5689 ;; typically only noticeable when statements are used in macro
5690 ;; arglists.
5691 (when (and containing-sexp
5692 (eq (char-after containing-sexp) ?\())
5693 (setq c-stmt-delim-chars c-stmt-delim-chars-with-comma))
5695 ;; cache char before and after indent point, and move point to
5696 ;; the most likely position to perform the majority of tests
5697 (goto-char indent-point)
5698 (c-backward-syntactic-ws lim)
5699 (setq char-before-ip (char-before))
5700 (goto-char indent-point)
5701 (skip-chars-forward " \t")
5702 (setq char-after-ip (char-after))
5704 ;; are we in a literal?
5705 (setq literal (c-in-literal lim))
5707 ;; now figure out syntactic qualities of the current line
5708 (cond
5709 ;; CASE 1: in a string.
5710 ((eq literal 'string)
5711 (c-add-syntax 'string (c-point 'bopl)))
5712 ;; CASE 2: in a C or C++ style comment.
5713 ((and (memq literal '(c c++))
5714 ;; This is a kludge for XEmacs where we use
5715 ;; `buffer-syntactic-context', which doesn't correctly
5716 ;; recognize "\*/" to end a block comment.
5717 ;; `parse-partial-sexp' which is used by
5718 ;; `c-literal-limits' will however do that in most
5719 ;; versions, which results in that we get nil from
5720 ;; `c-literal-limits' even when `c-in-literal' claims
5721 ;; we're inside a comment.
5722 (setq placeholder (c-literal-limits lim)))
5723 (c-add-syntax literal (car placeholder)))
5724 ;; CASE 3: in a cpp preprocessor macro continuation.
5725 ((and (save-excursion
5726 (when (c-beginning-of-macro)
5727 (setq macro-start (point))))
5728 (/= macro-start (c-point 'boi))
5729 (progn
5730 (setq tmpsymbol 'cpp-macro-cont)
5731 (or (not c-syntactic-indentation-in-macros)
5732 (save-excursion
5733 (goto-char macro-start)
5734 ;; If at the beginning of the body of a #define
5735 ;; directive then analyze as cpp-define-intro
5736 ;; only. Go on with the syntactic analysis
5737 ;; otherwise. in-macro-expr is set if we're in a
5738 ;; cpp expression, i.e. before the #define body
5739 ;; or anywhere in a non-#define directive.
5740 (if (c-forward-to-cpp-define-body)
5741 (let ((indent-boi (c-point 'boi indent-point)))
5742 (setq in-macro-expr (> (point) indent-boi)
5743 tmpsymbol 'cpp-define-intro)
5744 (= (point) indent-boi))
5745 (setq in-macro-expr t)
5746 nil)))))
5747 (c-add-syntax tmpsymbol macro-start)
5748 (setq macro-start nil))
5749 ;; CASE 11: an else clause?
5750 ((looking-at "else\\>[^_]")
5751 (c-beginning-of-statement-1 containing-sexp)
5752 (c-add-stmt-syntax 'else-clause nil t nil
5753 containing-sexp paren-state))
5754 ;; CASE 12: while closure of a do/while construct?
5755 ((and (looking-at "while\\>[^_]")
5756 (save-excursion
5757 (prog1 (eq (c-beginning-of-statement-1 containing-sexp)
5758 'beginning)
5759 (setq placeholder (point)))))
5760 (goto-char placeholder)
5761 (c-add-stmt-syntax 'do-while-closure nil t nil
5762 containing-sexp paren-state))
5763 ;; CASE 13: A catch or finally clause? This case is simpler
5764 ;; than if-else and do-while, because a block is required
5765 ;; after every try, catch and finally.
5766 ((save-excursion
5767 (and (cond ((c-major-mode-is 'c++-mode)
5768 (looking-at "catch\\>[^_]"))
5769 ((c-major-mode-is 'java-mode)
5770 (looking-at "\\(catch\\|finally\\)\\>[^_]")))
5771 (and (c-safe (c-backward-syntactic-ws)
5772 (c-backward-sexp)
5774 (eq (char-after) ?{)
5775 (c-safe (c-backward-syntactic-ws)
5776 (c-backward-sexp)
5778 (if (eq (char-after) ?\()
5779 (c-safe (c-backward-sexp) t)
5781 (looking-at "\\(try\\|catch\\)\\>[^_]")
5782 (setq placeholder (point))))
5783 (goto-char placeholder)
5784 (c-add-stmt-syntax 'catch-clause nil t nil
5785 containing-sexp paren-state))
5786 ;; CASE 18: A substatement we can recognize by keyword.
5787 ((save-excursion
5788 (and c-opt-block-stmt-key
5789 (if (c-mode-is-new-awk-p)
5790 (c-awk-prev-line-incomplete-p containing-sexp) ; ACM 2002/3/29
5791 (not (eq char-before-ip ?\;)))
5792 (not (memq char-after-ip '(?\) ?\] ?,)))
5793 (or (not (eq char-before-ip ?}))
5794 (c-looking-at-inexpr-block-backward c-state-cache))
5795 (> (point)
5796 (progn
5797 ;; Ought to cache the result from the
5798 ;; c-beginning-of-statement-1 calls here.
5799 (setq placeholder (point))
5800 (while (eq (setq step-type
5801 (c-beginning-of-statement-1 lim))
5802 'label))
5803 (if (eq step-type 'previous)
5804 (goto-char placeholder)
5805 (setq placeholder (point))
5806 (if (and (eq step-type 'same)
5807 (not (looking-at c-opt-block-stmt-key)))
5808 ;; Step up to the containing statement if we
5809 ;; stayed in the same one.
5810 (let (step)
5811 (while (eq
5812 (setq step
5813 (c-beginning-of-statement-1 lim))
5814 'label))
5815 (if (eq step 'up)
5816 (setq placeholder (point))
5817 ;; There was no containing statement afterall.
5818 (goto-char placeholder)))))
5819 placeholder))
5820 (if (looking-at c-block-stmt-2-key)
5821 ;; Require a parenthesis after these keywords.
5822 ;; Necessary to catch e.g. synchronized in Java,
5823 ;; which can be used both as statement and
5824 ;; modifier.
5825 (and (zerop (c-forward-token-2 1 nil))
5826 (eq (char-after) ?\())
5827 (looking-at c-opt-block-stmt-key))))
5828 (if (eq step-type 'up)
5829 ;; CASE 18A: Simple substatement.
5830 (progn
5831 (goto-char placeholder)
5832 (cond
5833 ((eq char-after-ip ?{)
5834 (c-add-stmt-syntax 'substatement-open nil nil nil
5835 containing-sexp paren-state))
5836 ((save-excursion
5837 (goto-char indent-point)
5838 (back-to-indentation)
5839 (looking-at c-label-key))
5840 (c-add-stmt-syntax 'substatement-label nil nil nil
5841 containing-sexp paren-state))
5843 (c-add-stmt-syntax 'substatement nil nil nil
5844 containing-sexp paren-state))))
5845 ;; CASE 18B: Some other substatement. This is shared
5846 ;; with case 10.
5847 (c-guess-continued-construct indent-point
5848 char-after-ip
5849 placeholder
5851 paren-state)))
5852 ;; CASE 4: In-expression statement. C.f. cases 7B, 16A and
5853 ;; 17E.
5854 ((and (or c-opt-inexpr-class-key
5855 c-opt-inexpr-block-key
5856 c-opt-lambda-key)
5857 (setq placeholder (c-looking-at-inexpr-block
5858 (c-safe-position containing-sexp paren-state)
5859 containing-sexp)))
5860 (setq tmpsymbol (assq (car placeholder)
5861 '((inexpr-class . class-open)
5862 (inexpr-statement . block-open))))
5863 (if tmpsymbol
5864 ;; It's a statement block or an anonymous class.
5865 (setq tmpsymbol (cdr tmpsymbol))
5866 ;; It's a Pike lambda. Check whether we are between the
5867 ;; lambda keyword and the argument list or at the defun
5868 ;; opener.
5869 (setq tmpsymbol (if (eq char-after-ip ?{)
5870 'inline-open
5871 'lambda-intro-cont)))
5872 (goto-char (cdr placeholder))
5873 (back-to-indentation)
5874 (c-add-stmt-syntax tmpsymbol nil t nil
5875 (c-most-enclosing-brace c-state-cache (point))
5876 (c-whack-state-after (point) paren-state))
5877 (unless (eq (point) (cdr placeholder))
5878 (c-add-syntax (car placeholder))))
5879 ;; CASE 5: Line is at top level.
5880 ((null containing-sexp)
5881 (cond
5882 ;; CASE 5A: we are looking at a defun, brace list, class,
5883 ;; or inline-inclass method opening brace
5884 ((setq special-brace-list
5885 (or (and c-special-brace-lists
5886 (c-looking-at-special-brace-list))
5887 (eq char-after-ip ?{)))
5888 (cond
5889 ;; CASE 5A.1: Non-class declaration block open.
5890 ((save-excursion
5891 (goto-char indent-point)
5892 (skip-chars-forward " \t")
5893 (and (c-safe (c-backward-sexp 2) t)
5894 (looking-at c-other-decl-block-key)
5895 (setq keyword (match-string 1)
5896 placeholder (point))
5897 (if (string-equal keyword "extern")
5898 ;; Special case for extern-lang-open. The
5899 ;; check for a following string is disabled
5900 ;; since it doesn't disambiguate anything.
5901 (and ;;(progn
5902 ;; (c-forward-sexp 1)
5903 ;; (c-forward-syntactic-ws)
5904 ;; (eq (char-after) ?\"))
5905 (setq tmpsymbol 'extern-lang-open))
5906 (setq tmpsymbol (intern (concat keyword "-open"))))
5908 (goto-char placeholder)
5909 (c-add-syntax tmpsymbol (c-point 'boi)))
5910 ;; CASE 5A.2: we are looking at a class opening brace
5911 ((save-excursion
5912 (goto-char indent-point)
5913 (skip-chars-forward " \t{")
5914 (let ((decl (c-search-uplist-for-classkey (c-parse-state))))
5915 (and decl
5916 (setq placeholder (aref decl 0)))
5918 (c-add-syntax 'class-open placeholder))
5919 ;; CASE 5A.3: brace list open
5920 ((save-excursion
5921 (c-beginning-of-decl-1 lim)
5922 (while (looking-at c-specifier-key)
5923 (goto-char (match-end 1))
5924 (c-forward-syntactic-ws indent-point))
5925 (setq placeholder (c-point 'boi))
5926 (or (consp special-brace-list)
5927 (and (or (save-excursion
5928 (goto-char indent-point)
5929 (setq tmpsymbol nil)
5930 (while (and (> (point) placeholder)
5931 (zerop (c-backward-token-2 1 t))
5932 (/= (char-after) ?=))
5933 (and c-opt-inexpr-brace-list-key
5934 (not tmpsymbol)
5935 (looking-at c-opt-inexpr-brace-list-key)
5936 (setq tmpsymbol 'topmost-intro-cont)))
5937 (eq (char-after) ?=))
5938 (looking-at c-brace-list-key))
5939 (save-excursion
5940 (while (and (< (point) indent-point)
5941 (zerop (c-forward-token-2 1 t))
5942 (not (memq (char-after) '(?\; ?\()))))
5943 (not (memq (char-after) '(?\; ?\()))
5944 ))))
5945 (if (and (not c-auto-newline-analysis)
5946 (c-major-mode-is 'java-mode)
5947 (eq tmpsymbol 'topmost-intro-cont))
5948 ;; We're in Java and have found that the open brace
5949 ;; belongs to a "new Foo[]" initialization list,
5950 ;; which means the brace list is part of an
5951 ;; expression and not a top level definition. We
5952 ;; therefore treat it as any topmost continuation
5953 ;; even though the semantically correct symbol still
5954 ;; is brace-list-open, on the same grounds as in
5955 ;; case B.2.
5956 (progn
5957 (c-beginning-of-statement-1 lim)
5958 (c-add-syntax 'topmost-intro-cont (c-point 'boi)))
5959 (c-add-syntax 'brace-list-open placeholder)))
5960 ;; CASE 5A.4: inline defun open
5961 ((and inclass-p (not inenclosing-p))
5962 (c-add-syntax 'inline-open)
5963 (c-add-class-syntax 'inclass inclass-p paren-state))
5964 ;; CASE 5A.5: ordinary defun open
5966 (goto-char placeholder)
5967 (if (or inclass-p macro-start)
5968 (c-add-syntax 'defun-open (c-point 'boi))
5969 ;; Bogus to use bol here, but it's the legacy.
5970 (c-add-syntax 'defun-open (c-point 'bol)))
5972 ;; CASE 5B: first K&R arg decl or member init
5973 ((c-just-after-func-arglist-p lim)
5974 (cond
5975 ;; CASE 5B.1: a member init
5976 ((or (eq char-before-ip ?:)
5977 (eq char-after-ip ?:))
5978 ;; this line should be indented relative to the beginning
5979 ;; of indentation for the topmost-intro line that contains
5980 ;; the prototype's open paren
5981 ;; TBD: is the following redundant?
5982 (if (eq char-before-ip ?:)
5983 (forward-char -1))
5984 (c-backward-syntactic-ws lim)
5985 ;; TBD: is the preceding redundant?
5986 (if (eq (char-before) ?:)
5987 (progn (forward-char -1)
5988 (c-backward-syntactic-ws lim)))
5989 (if (eq (char-before) ?\))
5990 (c-backward-sexp 1))
5991 (setq placeholder (point))
5992 (save-excursion
5993 (and (c-safe (c-backward-sexp 1) t)
5994 (looking-at "throw[^_]")
5995 (c-safe (c-backward-sexp 1) t)
5996 (setq placeholder (point))))
5997 (goto-char placeholder)
5998 (c-add-syntax 'member-init-intro (c-point 'boi))
5999 ;; we don't need to add any class offset since this
6000 ;; should be relative to the ctor's indentation
6002 ;; CASE 5B.2: K&R arg decl intro
6003 ((and c-recognize-knr-p
6004 (c-in-knr-argdecl lim))
6005 (c-beginning-of-statement-1 lim)
6006 (c-add-syntax 'knr-argdecl-intro (c-point 'boi))
6007 (if inclass-p
6008 (c-add-class-syntax 'inclass inclass-p paren-state)))
6009 ;; CASE 5B.3: Inside a member init list.
6010 ((c-beginning-of-member-init-list lim)
6011 (c-forward-syntactic-ws)
6012 (c-add-syntax 'member-init-cont (point)))
6013 ;; CASE 5B.4: Nether region after a C++ or Java func
6014 ;; decl, which could include a `throws' declaration.
6016 (c-beginning-of-statement-1 lim)
6017 (c-add-syntax 'func-decl-cont (c-point 'boi))
6019 ;; CASE 5C: inheritance line. could be first inheritance
6020 ;; line, or continuation of a multiple inheritance
6021 ((or (and (c-major-mode-is 'c++-mode)
6022 (progn
6023 (when (eq char-after-ip ?,)
6024 (skip-chars-forward " \t")
6025 (forward-char))
6026 (looking-at c-opt-postfix-decl-spec-key)))
6027 (and (or (eq char-before-ip ?:)
6028 ;; watch out for scope operator
6029 (save-excursion
6030 (and (eq char-after-ip ?:)
6031 (c-safe (forward-char 1) t)
6032 (not (eq (char-after) ?:))
6034 (save-excursion
6035 (c-backward-syntactic-ws lim)
6036 (if (eq char-before-ip ?:)
6037 (progn
6038 (forward-char -1)
6039 (c-backward-syntactic-ws lim)))
6040 (back-to-indentation)
6041 (looking-at c-class-key)))
6042 ;; for Java
6043 (and (c-major-mode-is 'java-mode)
6044 (let ((fence (save-excursion
6045 (c-beginning-of-statement-1 lim)
6046 (point)))
6047 cont done)
6048 (save-excursion
6049 (while (not done)
6050 (cond ((looking-at c-opt-postfix-decl-spec-key)
6051 (setq injava-inher (cons cont (point))
6052 done t))
6053 ((or (not (c-safe (c-forward-sexp -1) t))
6054 (<= (point) fence))
6055 (setq done t))
6057 (setq cont t)))
6058 injava-inher)
6059 (not (c-crosses-statement-barrier-p (cdr injava-inher)
6060 (point)))
6062 (cond
6063 ;; CASE 5C.1: non-hanging colon on an inher intro
6064 ((eq char-after-ip ?:)
6065 (c-beginning-of-statement-1 lim)
6066 (c-add-syntax 'inher-intro (c-point 'boi))
6067 ;; don't add inclass symbol since relative point already
6068 ;; contains any class offset
6070 ;; CASE 5C.2: hanging colon on an inher intro
6071 ((eq char-before-ip ?:)
6072 (c-beginning-of-statement-1 lim)
6073 (c-add-syntax 'inher-intro (c-point 'boi))
6074 (if inclass-p
6075 (c-add-class-syntax 'inclass inclass-p paren-state)))
6076 ;; CASE 5C.3: in a Java implements/extends
6077 (injava-inher
6078 (let ((where (cdr injava-inher))
6079 (cont (car injava-inher)))
6080 (goto-char where)
6081 (cond ((looking-at "throws\\>[^_]")
6082 (c-add-syntax 'func-decl-cont
6083 (progn (c-beginning-of-statement-1 lim)
6084 (c-point 'boi))))
6085 (cont (c-add-syntax 'inher-cont where))
6086 (t (c-add-syntax 'inher-intro
6087 (progn (goto-char (cdr injava-inher))
6088 (c-beginning-of-statement-1 lim)
6089 (point))))
6091 ;; CASE 5C.4: a continued inheritance line
6093 (c-beginning-of-inheritance-list lim)
6094 (c-add-syntax 'inher-cont (point))
6095 ;; don't add inclass symbol since relative point already
6096 ;; contains any class offset
6098 ;; CASE 5D: this could be a top-level initialization, a
6099 ;; member init list continuation, or a template argument
6100 ;; list continuation.
6101 ((c-with-syntax-table (if (c-major-mode-is 'c++-mode)
6102 c++-template-syntax-table
6103 (syntax-table))
6104 (save-excursion
6105 ;; Note: We use the fact that lim is always after any
6106 ;; preceding brace sexp.
6107 (while (and (zerop (c-backward-token-2 1 t lim))
6108 (or (not (looking-at "[;<,=]"))
6109 (and c-overloadable-operators-regexp
6110 (looking-at c-overloadable-operators-regexp)
6111 (save-excursion
6112 (c-backward-token-2 1 nil lim)
6113 (looking-at "operator\\>[^_]"))))))
6114 (or (memq (char-after) '(?, ?=))
6115 (and (c-major-mode-is 'c++-mode)
6116 (zerop (c-backward-token-2 1 nil lim))
6117 (eq (char-after) ?<)))))
6118 (goto-char indent-point)
6119 (setq placeholder
6120 (c-beginning-of-member-init-list lim))
6121 (cond
6122 ;; CASE 5D.1: hanging member init colon, but watch out
6123 ;; for bogus matches on access specifiers inside classes.
6124 ((and placeholder
6125 (save-excursion
6126 (setq placeholder (point))
6127 (c-backward-token-2 1 t lim)
6128 (and (eq (char-after) ?:)
6129 (not (eq (char-before) ?:))))
6130 (save-excursion
6131 (goto-char placeholder)
6132 (back-to-indentation)
6134 (/= (car (save-excursion
6135 (parse-partial-sexp (point) placeholder)))
6137 (and
6138 (if c-opt-access-key
6139 (not (looking-at c-opt-access-key)) t)
6140 (not (looking-at c-class-key))
6141 (if c-opt-bitfield-key
6142 (not (looking-at c-opt-bitfield-key)) t))
6144 (goto-char placeholder)
6145 (c-forward-syntactic-ws)
6146 (c-add-syntax 'member-init-cont (point))
6147 ;; we do not need to add class offset since relative
6148 ;; point is the member init above us
6150 ;; CASE 5D.2: non-hanging member init colon
6151 ((progn
6152 (c-forward-syntactic-ws indent-point)
6153 (eq (char-after) ?:))
6154 (skip-chars-forward " \t:")
6155 (c-add-syntax 'member-init-cont (point)))
6156 ;; CASE 5D.3: perhaps a template list continuation?
6157 ((and (c-major-mode-is 'c++-mode)
6158 (save-excursion
6159 (save-restriction
6160 (c-with-syntax-table c++-template-syntax-table
6161 (goto-char indent-point)
6162 (setq placeholder (c-up-list-backward (point)))
6163 (and placeholder
6164 (eq (char-after placeholder) ?<))))))
6165 ;; we can probably indent it just like an arglist-cont
6166 (goto-char placeholder)
6167 (c-beginning-of-statement-1 lim t)
6168 (c-add-syntax 'template-args-cont (c-point 'boi)))
6169 ;; CASE 5D.4: perhaps a multiple inheritance line?
6170 ((and (c-major-mode-is 'c++-mode)
6171 (save-excursion
6172 (c-beginning-of-statement-1 lim)
6173 (setq placeholder (point))
6174 (if (looking-at "static\\>[^_]")
6175 (c-forward-token-2 1 nil indent-point))
6176 (and (looking-at c-class-key)
6177 (zerop (c-forward-token-2 2 nil indent-point))
6178 (if (eq (char-after) ?<)
6179 (c-with-syntax-table c++-template-syntax-table
6180 (zerop (c-forward-token-2 1 t indent-point)))
6182 (eq (char-after) ?:))))
6183 (goto-char placeholder)
6184 (c-add-syntax 'inher-cont (c-point 'boi)))
6185 ;; CASE 5D.5: Continuation of the "expression part" of a
6186 ;; top level construct.
6188 (while (and (eq (car (c-beginning-of-decl-1 containing-sexp))
6189 'same)
6190 (save-excursion
6191 (c-backward-syntactic-ws)
6192 (eq (char-before) ?}))))
6193 (c-add-stmt-syntax
6194 (if (eq char-before-ip ?,)
6195 ;; A preceding comma at the top level means that a
6196 ;; new variable declaration starts here. Use
6197 ;; topmost-intro-cont for it, for consistency with
6198 ;; the first variable declaration. C.f. case 5N.
6199 'topmost-intro-cont
6200 'statement-cont)
6201 nil nil nil containing-sexp paren-state))
6203 ;; CASE 5E: we are looking at a access specifier
6204 ((and inclass-p
6205 c-opt-access-key
6206 (looking-at c-opt-access-key))
6207 (setq placeholder (c-add-class-syntax 'inclass inclass-p
6208 paren-state))
6209 ;; Append access-label with the same anchor point as inclass gets.
6210 (c-append-syntax 'access-label placeholder))
6211 ;; CASE 5F: Close of a non-class declaration level block.
6212 ((and inenclosing-p
6213 (eq char-after-ip ?}))
6214 (c-add-syntax (intern (concat inenclosing-p "-close"))
6215 (aref inclass-p 0)))
6216 ;; CASE 5G: we are looking at the brace which closes the
6217 ;; enclosing nested class decl
6218 ((and inclass-p
6219 (eq char-after-ip ?})
6220 (save-excursion
6221 (save-restriction
6222 (widen)
6223 (forward-char 1)
6224 (and (c-safe (c-backward-sexp 1) t)
6225 (= (point) (aref inclass-p 1))
6226 ))))
6227 (c-add-class-syntax 'class-close inclass-p paren-state))
6228 ;; CASE 5H: we could be looking at subsequent knr-argdecls
6229 ((and c-recognize-knr-p
6230 (not (eq char-before-ip ?}))
6231 (save-excursion
6232 (setq placeholder (cdr (c-beginning-of-decl-1 lim)))
6233 (and placeholder
6234 ;; Do an extra check to avoid tripping up on
6235 ;; statements that occur in invalid contexts
6236 ;; (e.g. in macro bodies where we don't really
6237 ;; know the context of what we're looking at).
6238 (not (and c-opt-block-stmt-key
6239 (looking-at c-opt-block-stmt-key)))))
6240 (< placeholder indent-point))
6241 (goto-char placeholder)
6242 (c-add-syntax 'knr-argdecl (point)))
6243 ;; CASE 5I: ObjC method definition.
6244 ((and c-opt-method-key
6245 (looking-at c-opt-method-key))
6246 (c-beginning-of-statement-1 nil t)
6247 (if (= (point) indent-point)
6248 ;; Handle the case when it's the first (non-comment)
6249 ;; thing in the buffer. Can't look for a 'same return
6250 ;; value from cbos1 since ObjC directives currently
6251 ;; aren't recognized fully, so that we get 'same
6252 ;; instead of 'previous if it moved over a preceding
6253 ;; directive.
6254 (goto-char (point-min)))
6255 (c-add-syntax 'objc-method-intro (c-point 'boi)))
6256 ;; CASE 5P: AWK pattern or function or continuation
6257 ;; thereof.
6258 ((c-mode-is-new-awk-p)
6259 (setq placeholder (point))
6260 (c-add-stmt-syntax
6261 (if (and (eq (c-beginning-of-statement-1) 'same)
6262 (/= (point) placeholder))
6263 'topmost-intro-cont
6264 'topmost-intro)
6265 nil nil nil
6266 containing-sexp paren-state))
6267 ;; CASE 5N: At a variable declaration that follows a class
6268 ;; definition or some other block declaration that doesn't
6269 ;; end at the closing '}'. C.f. case 5D.5.
6270 ((progn
6271 (c-backward-syntactic-ws lim)
6272 (and (eq (char-before) ?})
6273 (save-excursion
6274 (let ((start (point)))
6275 (if paren-state
6276 ;; Speed up the backward search a bit.
6277 (goto-char (car (car paren-state))))
6278 (c-beginning-of-decl-1 containing-sexp)
6279 (setq placeholder (point))
6280 (if (= start (point))
6281 ;; The '}' is unbalanced.
6283 (c-end-of-decl-1)
6284 (>= (point) indent-point))))))
6285 (goto-char placeholder)
6286 (c-add-stmt-syntax 'topmost-intro-cont nil nil nil
6287 containing-sexp paren-state))
6288 ;; CASE 5J: we are at the topmost level, make
6289 ;; sure we skip back past any access specifiers
6290 ((progn
6291 (while (and inclass-p
6292 c-opt-access-key
6293 (not (bobp))
6294 (save-excursion
6295 (c-safe (c-backward-sexp 1) t)
6296 (looking-at c-opt-access-key)))
6297 (c-backward-sexp 1)
6298 (c-backward-syntactic-ws lim))
6299 (or (bobp)
6300 (if (c-mode-is-new-awk-p)
6301 (not (c-awk-prev-line-incomplete-p))
6302 (memq (char-before) '(?\; ?})))
6303 (and (c-major-mode-is 'objc-mode)
6304 (progn
6305 (c-beginning-of-statement-1 lim)
6306 (eq (char-after) ?@)))))
6307 ;; real beginning-of-line could be narrowed out due to
6308 ;; enclosure in a class block
6309 (save-restriction
6310 (widen)
6311 (c-add-syntax 'topmost-intro (c-point 'bol))
6312 ;; Using bol instead of boi above is highly bogus, and
6313 ;; it makes our lives hard to remain compatible. :P
6314 (if inclass-p
6315 (progn
6316 (goto-char (aref inclass-p 1))
6317 (or (= (point) (c-point 'boi))
6318 (goto-char (aref inclass-p 0)))
6319 (if inenclosing-p
6320 (c-add-syntax (intern (concat "in" inenclosing-p))
6321 (c-point 'boi))
6322 (c-add-class-syntax 'inclass inclass-p paren-state))
6324 (when (and c-syntactic-indentation-in-macros
6325 macro-start
6326 (/= macro-start (c-point 'boi indent-point)))
6327 (c-add-syntax 'cpp-define-intro)
6328 (setq macro-start nil))
6330 ;; CASE 5K: we are at an ObjC method definition
6331 ;; continuation line.
6332 ((and c-opt-method-key
6333 (save-excursion
6334 (goto-char indent-point)
6335 (c-beginning-of-statement-1 lim)
6336 (beginning-of-line)
6337 (when (looking-at c-opt-method-key)
6338 (setq placeholder (point)))))
6339 (c-add-syntax 'objc-method-args-cont placeholder))
6340 ;; CASE 5L: we are at the first argument of a template
6341 ;; arglist that begins on the previous line.
6342 ((eq (char-before) ?<)
6343 (c-beginning-of-statement-1 (c-safe-position (point) paren-state))
6344 (c-add-syntax 'template-args-cont (c-point 'boi)))
6345 ;; CASE 5M: we are at a topmost continuation line
6347 (c-beginning-of-statement-1 (c-safe-position (point) paren-state))
6348 (c-add-syntax 'topmost-intro-cont (c-point 'boi)))
6350 ;; (CASE 6 has been removed.)
6351 ;; CASE 7: line is an expression, not a statement. Most
6352 ;; likely we are either in a function prototype or a function
6353 ;; call argument list
6354 ((not (or (and c-special-brace-lists
6355 (save-excursion
6356 (goto-char containing-sexp)
6357 (c-looking-at-special-brace-list)))
6358 (eq (char-after containing-sexp) ?{)))
6359 (cond
6360 ;; CASE 7A: we are looking at the arglist closing paren.
6361 ;; C.f. case 7F.
6362 ((memq char-after-ip '(?\) ?\]))
6363 (goto-char containing-sexp)
6364 (setq placeholder (c-point 'boi))
6365 (if (and (c-safe (backward-up-list 1) t)
6366 (>= (point) placeholder))
6367 (progn
6368 (forward-char)
6369 (skip-chars-forward " \t"))
6370 (goto-char placeholder))
6371 (c-add-stmt-syntax 'arglist-close (list containing-sexp) t nil
6372 (c-most-enclosing-brace paren-state (point))
6373 (c-whack-state-after (point) paren-state)))
6374 ;; CASE 7B: Looking at the opening brace of an
6375 ;; in-expression block or brace list. C.f. cases 4, 16A
6376 ;; and 17E.
6377 ((and (eq char-after-ip ?{)
6378 (progn
6379 (setq placeholder (c-inside-bracelist-p (point)
6380 c-state-cache))
6381 (if placeholder
6382 (setq tmpsymbol '(brace-list-open . inexpr-class))
6383 (setq tmpsymbol '(block-open . inexpr-statement)
6384 placeholder
6385 (cdr-safe (c-looking-at-inexpr-block
6386 (c-safe-position containing-sexp
6387 paren-state)
6388 containing-sexp)))
6389 ;; placeholder is nil if it's a block directly in
6390 ;; a function arglist. That makes us skip out of
6391 ;; this case.
6393 (goto-char placeholder)
6394 (back-to-indentation)
6395 (c-add-stmt-syntax (car tmpsymbol) nil t nil
6396 (c-most-enclosing-brace paren-state (point))
6397 (c-whack-state-after (point) paren-state))
6398 (if (/= (point) placeholder)
6399 (c-add-syntax (cdr tmpsymbol))))
6400 ;; CASE 7C: we are looking at the first argument in an empty
6401 ;; argument list. Use arglist-close if we're actually
6402 ;; looking at a close paren or bracket.
6403 ((memq char-before-ip '(?\( ?\[))
6404 (goto-char containing-sexp)
6405 (setq placeholder (c-point 'boi))
6406 (when (and (c-safe (backward-up-list 1) t)
6407 (>= (point) placeholder))
6408 (forward-char)
6409 (skip-chars-forward " \t")
6410 (setq placeholder (point)))
6411 (c-add-syntax 'arglist-intro placeholder))
6412 ;; CASE 7D: we are inside a conditional test clause. treat
6413 ;; these things as statements
6414 ((progn
6415 (goto-char containing-sexp)
6416 (and (c-safe (c-forward-sexp -1) t)
6417 (looking-at "\\<for\\>[^_]")))
6418 (goto-char (1+ containing-sexp))
6419 (c-forward-syntactic-ws indent-point)
6420 (if (eq char-before-ip ?\;)
6421 (c-add-syntax 'statement (point))
6422 (c-add-syntax 'statement-cont (point))
6424 ;; CASE 7E: maybe a continued ObjC method call. This is the
6425 ;; case when we are inside a [] bracketed exp, and what
6426 ;; precede the opening bracket is not an identifier.
6427 ((and c-opt-method-key
6428 (eq (char-after containing-sexp) ?\[)
6429 (progn
6430 (goto-char (1- containing-sexp))
6431 (c-backward-syntactic-ws (c-point 'bod))
6432 (if (not (looking-at c-symbol-key))
6433 (c-add-syntax 'objc-method-call-cont containing-sexp))
6435 ;; CASE 7F: we are looking at an arglist continuation line,
6436 ;; but the preceding argument is on the same line as the
6437 ;; opening paren. This case includes multi-line
6438 ;; mathematical paren groupings, but we could be on a
6439 ;; for-list continuation line. C.f. case 7A.
6440 ((progn
6441 (goto-char (1+ containing-sexp))
6442 (skip-chars-forward " \t")
6443 (and (not (eolp))
6444 (not (looking-at "\\\\$"))))
6445 (goto-char containing-sexp)
6446 (setq placeholder (c-point 'boi))
6447 (if (and (c-safe (backward-up-list 1) t)
6448 (>= (point) placeholder))
6449 (progn
6450 (forward-char)
6451 (skip-chars-forward " \t"))
6452 (goto-char placeholder))
6453 (c-add-stmt-syntax 'arglist-cont-nonempty (list containing-sexp)
6454 t nil
6455 (c-most-enclosing-brace c-state-cache (point))
6456 (c-whack-state-after (point) paren-state)))
6457 ;; CASE 7G: we are looking at just a normal arglist
6458 ;; continuation line
6459 (t (c-forward-syntactic-ws indent-point)
6460 (c-add-syntax 'arglist-cont (c-point 'boi)))
6462 ;; CASE 8: func-local multi-inheritance line
6463 ((and (c-major-mode-is 'c++-mode)
6464 (save-excursion
6465 (goto-char indent-point)
6466 (skip-chars-forward " \t")
6467 (looking-at c-opt-postfix-decl-spec-key)))
6468 (goto-char indent-point)
6469 (skip-chars-forward " \t")
6470 (cond
6471 ;; CASE 8A: non-hanging colon on an inher intro
6472 ((eq char-after-ip ?:)
6473 (c-backward-syntactic-ws lim)
6474 (c-add-syntax 'inher-intro (c-point 'boi)))
6475 ;; CASE 8B: hanging colon on an inher intro
6476 ((eq char-before-ip ?:)
6477 (c-add-syntax 'inher-intro (c-point 'boi)))
6478 ;; CASE 8C: a continued inheritance line
6480 (c-beginning-of-inheritance-list lim)
6481 (c-add-syntax 'inher-cont (point))
6483 ;; CASE 9: we are inside a brace-list
6484 ((and (not (c-mode-is-new-awk-p)) ; Maybe this isn't needed (ACM, 2002/3/29)
6485 (setq special-brace-list
6486 (or (and c-special-brace-lists
6487 (save-excursion
6488 (goto-char containing-sexp)
6489 (c-looking-at-special-brace-list)))
6490 (c-inside-bracelist-p containing-sexp paren-state))))
6491 (cond
6492 ;; CASE 9A: In the middle of a special brace list opener.
6493 ((and (consp special-brace-list)
6494 (save-excursion
6495 (goto-char containing-sexp)
6496 (eq (char-after) ?\())
6497 (eq char-after-ip (car (cdr special-brace-list))))
6498 (goto-char (car (car special-brace-list)))
6499 (skip-chars-backward " \t")
6500 (if (and (bolp)
6501 (assoc 'statement-cont
6502 (setq placeholder (c-guess-basic-syntax))))
6503 (setq c-syntactic-context placeholder)
6504 (c-beginning-of-statement-1
6505 (c-safe-position (1- containing-sexp) paren-state))
6506 (c-forward-token-2 0)
6507 (while (looking-at c-specifier-key)
6508 (goto-char (match-end 1))
6509 (c-forward-syntactic-ws))
6510 (c-add-syntax 'brace-list-open (c-point 'boi))))
6511 ;; CASE 9B: brace-list-close brace
6512 ((if (consp special-brace-list)
6513 ;; Check special brace list closer.
6514 (progn
6515 (goto-char (car (car special-brace-list)))
6516 (save-excursion
6517 (goto-char indent-point)
6518 (back-to-indentation)
6520 ;; We were between the special close char and the `)'.
6521 (and (eq (char-after) ?\))
6522 (eq (1+ (point)) (cdr (car special-brace-list))))
6523 ;; We were before the special close char.
6524 (and (eq (char-after) (cdr (cdr special-brace-list)))
6525 (zerop (c-forward-token-2))
6526 (eq (1+ (point)) (cdr (car special-brace-list)))))))
6527 ;; Normal brace list check.
6528 (and (eq char-after-ip ?})
6529 (c-safe (goto-char (c-up-list-backward (point))) t)
6530 (= (point) containing-sexp)))
6531 (if (eq (point) (c-point 'boi))
6532 (c-add-syntax 'brace-list-close (point))
6533 (setq lim (c-most-enclosing-brace c-state-cache (point)))
6534 (c-beginning-of-statement-1 lim)
6535 (c-add-stmt-syntax 'brace-list-close nil t t lim
6536 (c-whack-state-after (point) paren-state))))
6538 ;; Prepare for the rest of the cases below by going to the
6539 ;; token following the opening brace
6540 (if (consp special-brace-list)
6541 (progn
6542 (goto-char (car (car special-brace-list)))
6543 (c-forward-token-2 1 nil indent-point))
6544 (goto-char containing-sexp))
6545 (forward-char)
6546 (let ((start (point)))
6547 (c-forward-syntactic-ws indent-point)
6548 (goto-char (max start (c-point 'bol))))
6549 (c-skip-ws-forward indent-point)
6550 (cond
6551 ;; CASE 9C: we're looking at the first line in a brace-list
6552 ((= (point) indent-point)
6553 (if (consp special-brace-list)
6554 (goto-char (car (car special-brace-list)))
6555 (goto-char containing-sexp))
6556 (if (eq (point) (c-point 'boi))
6557 (c-add-syntax 'brace-list-intro (point))
6558 (setq lim (c-most-enclosing-brace c-state-cache (point)))
6559 (c-beginning-of-statement-1 lim)
6560 (c-add-stmt-syntax 'brace-list-intro nil t t lim
6561 (c-whack-state-after (point) paren-state))))
6562 ;; CASE 9D: this is just a later brace-list-entry or
6563 ;; brace-entry-open
6564 (t (if (or (eq char-after-ip ?{)
6565 (and c-special-brace-lists
6566 (save-excursion
6567 (goto-char indent-point)
6568 (c-forward-syntactic-ws (c-point 'eol))
6569 (c-looking-at-special-brace-list (point)))))
6570 (c-add-syntax 'brace-entry-open (point))
6571 (c-add-syntax 'brace-list-entry (point))
6573 ))))
6574 ;; CASE 10: A continued statement or top level construct.
6575 ((and (if (c-mode-is-new-awk-p)
6576 (c-awk-prev-line-incomplete-p containing-sexp) ; ACM 2002/3/29
6577 (and (not (memq char-before-ip '(?\; ?:)))
6578 (or (not (eq char-before-ip ?}))
6579 (c-looking-at-inexpr-block-backward c-state-cache))))
6580 (> (point)
6581 (save-excursion
6582 (c-beginning-of-statement-1 containing-sexp)
6583 (setq placeholder (point))))
6584 (/= placeholder containing-sexp))
6585 ;; This is shared with case 18.
6586 (c-guess-continued-construct indent-point
6587 char-after-ip
6588 placeholder
6589 containing-sexp
6590 paren-state))
6591 ;; CASE 14: A case or default label
6592 ((looking-at c-label-kwds-regexp)
6593 (goto-char containing-sexp)
6594 (setq lim (c-most-enclosing-brace c-state-cache containing-sexp))
6595 (c-backward-to-block-anchor lim)
6596 (c-add-stmt-syntax 'case-label nil t nil
6597 lim paren-state))
6598 ;; CASE 15: any other label
6599 ((looking-at c-label-key)
6600 (goto-char containing-sexp)
6601 (setq lim (c-most-enclosing-brace c-state-cache containing-sexp))
6602 (save-excursion
6603 (setq tmpsymbol
6604 (if (and (eq (c-beginning-of-statement-1 lim) 'up)
6605 (looking-at "switch\\>[^_]"))
6606 ;; If the surrounding statement is a switch then
6607 ;; let's analyze all labels as switch labels, so
6608 ;; that they get lined up consistently.
6609 'case-label
6610 'label)))
6611 (c-backward-to-block-anchor lim)
6612 (c-add-stmt-syntax tmpsymbol nil t nil
6613 lim paren-state))
6614 ;; CASE 16: block close brace, possibly closing the defun or
6615 ;; the class
6616 ((eq char-after-ip ?})
6617 ;; From here on we have the next containing sexp in lim.
6618 (setq lim (c-most-enclosing-brace paren-state))
6619 (goto-char containing-sexp)
6620 (cond
6621 ;; CASE 16E: Closing a statement block? This catches
6622 ;; cases where it's preceded by a statement keyword,
6623 ;; which works even when used in an "invalid" context,
6624 ;; e.g. a macro argument.
6625 ((c-after-conditional)
6626 (c-backward-to-block-anchor lim)
6627 (c-add-stmt-syntax 'block-close nil t nil
6628 lim paren-state))
6629 ;; CASE 16A: closing a lambda defun or an in-expression
6630 ;; block? C.f. cases 4, 7B and 17E.
6631 ((setq placeholder (c-looking-at-inexpr-block
6632 (c-safe-position containing-sexp paren-state)
6633 nil))
6634 (setq tmpsymbol (if (eq (car placeholder) 'inlambda)
6635 'inline-close
6636 'block-close))
6637 (goto-char containing-sexp)
6638 (back-to-indentation)
6639 (if (= containing-sexp (point))
6640 (c-add-syntax tmpsymbol (point))
6641 (goto-char (cdr placeholder))
6642 (back-to-indentation)
6643 (c-add-stmt-syntax tmpsymbol nil t nil
6644 (c-most-enclosing-brace paren-state (point))
6645 (c-whack-state-after (point) paren-state))
6646 (if (/= (point) (cdr placeholder))
6647 (c-add-syntax (car placeholder)))))
6648 ;; CASE 16B: does this close an inline or a function in
6649 ;; a non-class declaration level block?
6650 ((setq placeholder (c-search-uplist-for-classkey paren-state))
6651 (c-backward-to-decl-anchor lim)
6652 (back-to-indentation)
6653 (if (save-excursion
6654 (goto-char (aref placeholder 0))
6655 (looking-at c-other-decl-block-key))
6656 (c-add-syntax 'defun-close (point))
6657 (c-add-syntax 'inline-close (point))))
6658 ;; CASE 16F: Can be a defun-close of a function declared
6659 ;; in a statement block, e.g. in Pike or when using gcc
6660 ;; extensions, but watch out for macros followed by
6661 ;; blocks. Let it through to be handled below.
6662 ;; C.f. cases B.3 and 17G.
6663 ((and (not inenclosing-p)
6665 (save-excursion
6666 (and (not (c-looking-at-bos))
6667 (eq (c-beginning-of-statement-1 lim nil nil t) 'same)
6668 (setq placeholder (point))
6669 ;; Look for a type or identifier followed by a
6670 ;; symbol, i.e. the start of a function declaration.
6671 ;; Doesn't work for declarations like "int *foo()
6672 ;; ..."; we'd need to refactor the more competent
6673 ;; analysis in `c-font-lock-declarations' for that.
6674 (c-forward-type)
6675 (progn
6676 (c-forward-syntactic-ws)
6677 (looking-at c-symbol-start)))))
6678 (back-to-indentation)
6679 (if (/= (point) containing-sexp)
6680 (goto-char placeholder))
6681 (c-add-stmt-syntax 'defun-close nil t nil
6682 lim paren-state))
6683 ;; CASE 16C: if there an enclosing brace that hasn't
6684 ;; been narrowed out by a class, then this is a
6685 ;; block-close. C.f. case 17H.
6686 ((and (not inenclosing-p) lim)
6687 ;; If the block is preceded by a case/switch label on
6688 ;; the same line, we anchor at the first preceding label
6689 ;; at boi. The default handling in c-add-stmt-syntax is
6690 ;; really fixes it better, but we do like this to keep
6691 ;; the indentation compatible with version 5.28 and
6692 ;; earlier.
6693 (while (and (/= (setq placeholder (point)) (c-point 'boi))
6694 (eq (c-beginning-of-statement-1 lim) 'label)))
6695 (goto-char placeholder)
6696 (if (looking-at c-label-kwds-regexp)
6697 (c-add-syntax 'block-close (point))
6698 (goto-char containing-sexp)
6699 ;; c-backward-to-block-anchor not necessary here; those
6700 ;; situations are handled in case 16E above.
6701 (c-add-stmt-syntax 'block-close nil t nil
6702 lim paren-state)))
6703 ;; CASE 16D: find out whether we're closing a top-level
6704 ;; class or a defun
6706 (save-restriction
6707 (narrow-to-region (point-min) indent-point)
6708 (let ((decl (c-search-uplist-for-classkey (c-parse-state))))
6709 (if decl
6710 (c-add-class-syntax 'class-close decl paren-state)
6711 (goto-char containing-sexp)
6712 (c-backward-to-decl-anchor lim)
6713 (back-to-indentation)
6714 (c-add-syntax 'defun-close (point)))))
6716 ;; CASE 17: Statement or defun catchall.
6718 (goto-char indent-point)
6719 ;; Back up statements until we find one that starts at boi.
6720 (while (let* ((prev-point (point))
6721 (last-step-type (c-beginning-of-statement-1
6722 containing-sexp)))
6723 (if (= (point) prev-point)
6724 (progn
6725 (setq step-type (or step-type last-step-type))
6726 nil)
6727 (setq step-type last-step-type)
6728 (/= (point) (c-point 'boi)))))
6729 (cond
6730 ;; CASE 17B: continued statement
6731 ((and (eq step-type 'same)
6732 (/= (point) indent-point))
6733 (c-add-stmt-syntax 'statement-cont nil nil nil
6734 containing-sexp paren-state))
6735 ;; CASE 17A: After a case/default label?
6736 ((progn
6737 (while (and (eq step-type 'label)
6738 (not (looking-at c-label-kwds-regexp)))
6739 (setq step-type
6740 (c-beginning-of-statement-1 containing-sexp)))
6741 (eq step-type 'label))
6742 (c-add-stmt-syntax (if (eq char-after-ip ?{)
6743 'statement-case-open
6744 'statement-case-intro)
6745 nil t nil containing-sexp paren-state))
6746 ;; CASE 17D: any old statement
6747 ((progn
6748 (while (eq step-type 'label)
6749 (setq step-type
6750 (c-beginning-of-statement-1 containing-sexp)))
6751 (eq step-type 'previous))
6752 (c-add-stmt-syntax 'statement nil t nil
6753 containing-sexp paren-state)
6754 (if (eq char-after-ip ?{)
6755 (c-add-syntax 'block-open)))
6756 ;; CASE 17I: Inside a substatement block.
6757 ((progn
6758 ;; The following tests are all based on containing-sexp.
6759 (goto-char containing-sexp)
6760 ;; From here on we have the next containing sexp in lim.
6761 (setq lim (c-most-enclosing-brace paren-state containing-sexp))
6762 (c-after-conditional))
6763 (c-backward-to-block-anchor lim)
6764 (c-add-stmt-syntax 'statement-block-intro nil t nil
6765 lim paren-state)
6766 (if (eq char-after-ip ?{)
6767 (c-add-syntax 'block-open)))
6768 ;; CASE 17E: first statement in an in-expression block.
6769 ;; C.f. cases 4, 7B and 16A.
6770 ((setq placeholder (c-looking-at-inexpr-block
6771 (c-safe-position containing-sexp paren-state)
6772 nil))
6773 (setq tmpsymbol (if (eq (car placeholder) 'inlambda)
6774 'defun-block-intro
6775 'statement-block-intro))
6776 (back-to-indentation)
6777 (if (= containing-sexp (point))
6778 (c-add-syntax tmpsymbol (point))
6779 (goto-char (cdr placeholder))
6780 (back-to-indentation)
6781 (c-add-stmt-syntax tmpsymbol nil t nil
6782 (c-most-enclosing-brace c-state-cache (point))
6783 (c-whack-state-after (point) paren-state))
6784 (if (/= (point) (cdr placeholder))
6785 (c-add-syntax (car placeholder))))
6786 (if (eq char-after-ip ?{)
6787 (c-add-syntax 'block-open)))
6788 ;; CASE 17F: first statement in an inline, or first
6789 ;; statement in a top-level defun. we can tell this is it
6790 ;; if there are no enclosing braces that haven't been
6791 ;; narrowed out by a class (i.e. don't use bod here).
6792 ((save-excursion
6793 (save-restriction
6794 (widen)
6795 (c-narrow-out-enclosing-class paren-state containing-sexp)
6796 (not (c-most-enclosing-brace paren-state))))
6797 (c-backward-to-decl-anchor lim)
6798 (back-to-indentation)
6799 (c-add-syntax 'defun-block-intro (point)))
6800 ;; CASE 17G: First statement in a function declared inside
6801 ;; a normal block. This can occur in Pike and with
6802 ;; e.g. the gcc extensions, but watch out for macros
6803 ;; followed by blocks. C.f. cases B.3 and 16F.
6804 ((save-excursion
6805 (and (not (c-looking-at-bos))
6806 (eq (c-beginning-of-statement-1 lim nil nil t) 'same)
6807 (setq placeholder (point))
6808 ;; Look for a type or identifier followed by a
6809 ;; symbol, i.e. the start of a function declaration.
6810 ;; Doesn't work for declarations like "int *foo()
6811 ;; ..."; we'd need to refactor the more competent
6812 ;; analysis in `c-font-lock-declarations' for that.
6813 (c-forward-type)
6814 (progn
6815 (c-forward-syntactic-ws)
6816 (looking-at c-symbol-start))))
6817 (back-to-indentation)
6818 (if (/= (point) containing-sexp)
6819 (goto-char placeholder))
6820 (c-add-stmt-syntax 'defun-block-intro nil t nil
6821 lim paren-state))
6822 ;; CASE 17H: First statement in a block. C.f. case 16C.
6824 ;; If the block is preceded by a case/switch label on the
6825 ;; same line, we anchor at the first preceding label at
6826 ;; boi. The default handling in c-add-stmt-syntax is
6827 ;; really fixes it better, but we do like this to keep the
6828 ;; indentation compatible with version 5.28 and earlier.
6829 (while (and (/= (setq placeholder (point)) (c-point 'boi))
6830 (eq (c-beginning-of-statement-1 lim) 'label)))
6831 (goto-char placeholder)
6832 (if (looking-at c-label-kwds-regexp)
6833 (c-add-syntax 'statement-block-intro (point))
6834 (goto-char containing-sexp)
6835 ;; c-backward-to-block-anchor not necessary here; those
6836 ;; situations are handled in case 17I above.
6837 (c-add-stmt-syntax 'statement-block-intro nil t nil
6838 lim paren-state))
6839 (if (eq char-after-ip ?{)
6840 (c-add-syntax 'block-open)))
6843 ;; now we need to look at any modifiers
6844 (goto-char indent-point)
6845 (skip-chars-forward " \t")
6846 ;; are we looking at a comment only line?
6847 (when (and (looking-at c-comment-start-regexp)
6848 (/= (c-forward-token-2 0 nil (c-point 'eol)) 0))
6849 (c-append-syntax 'comment-intro))
6850 ;; we might want to give additional offset to friends (in C++).
6851 (when (and c-opt-friend-key
6852 (looking-at c-opt-friend-key))
6853 (c-append-syntax 'friend))
6855 ;; Set syntactic-relpos.
6856 (let ((p c-syntactic-context))
6857 (while (and p
6858 (if (integerp (car-safe (cdr-safe (car p))))
6859 (progn
6860 (setq syntactic-relpos (car (cdr (car p))))
6861 nil)
6863 (setq p (cdr p))))
6865 ;; Start of or a continuation of a preprocessor directive?
6866 (if (and macro-start
6867 (eq macro-start (c-point 'boi))
6868 (not (and (c-major-mode-is 'pike-mode)
6869 (eq (char-after (1+ macro-start)) ?\"))))
6870 (c-append-syntax 'cpp-macro)
6871 (when (and c-syntactic-indentation-in-macros macro-start)
6872 (if in-macro-expr
6873 (when (or
6874 (< syntactic-relpos macro-start)
6875 (not (or
6876 (assq 'arglist-intro c-syntactic-context)
6877 (assq 'arglist-cont c-syntactic-context)
6878 (assq 'arglist-cont-nonempty c-syntactic-context)
6879 (assq 'arglist-close c-syntactic-context))))
6880 ;; If inside a cpp expression, i.e. anywhere in a
6881 ;; cpp directive except a #define body, we only let
6882 ;; through the syntactic analysis that is internal
6883 ;; in the expression. That means the arglist
6884 ;; elements, if they are anchored inside the cpp
6885 ;; expression.
6886 (setq c-syntactic-context nil)
6887 (c-add-syntax 'cpp-macro-cont macro-start))
6888 (when (and (eq macro-start syntactic-relpos)
6889 (not (assq 'cpp-define-intro c-syntactic-context))
6890 (save-excursion
6891 (goto-char macro-start)
6892 (or (not (c-forward-to-cpp-define-body))
6893 (<= (point) (c-point 'boi indent-point)))))
6894 ;; Inside a #define body and the syntactic analysis is
6895 ;; anchored on the start of the #define. In this case
6896 ;; we add cpp-define-intro to get the extra
6897 ;; indentation of the #define body.
6898 (c-add-syntax 'cpp-define-intro)))))
6899 ;; return the syntax
6900 c-syntactic-context))))
6903 ;; Indentation calculation.
6905 (defun c-evaluate-offset (offset langelem symbol)
6906 ;; offset can be a number, a function, a variable, a list, or one of
6907 ;; the symbols + or -
6908 (cond
6909 ((eq offset '+) c-basic-offset)
6910 ((eq offset '-) (- c-basic-offset))
6911 ((eq offset '++) (* 2 c-basic-offset))
6912 ((eq offset '--) (* 2 (- c-basic-offset)))
6913 ((eq offset '*) (/ c-basic-offset 2))
6914 ((eq offset '/) (/ (- c-basic-offset) 2))
6915 ((numberp offset) offset)
6916 ((functionp offset) (c-evaluate-offset
6917 (funcall offset
6918 (cons (car langelem)
6919 (car-safe (cdr langelem))))
6920 langelem symbol))
6921 ((vectorp offset) offset)
6922 ((null offset) nil)
6923 ((listp offset)
6924 (if (eq (car offset) 'quote)
6925 (error
6926 "Setting in c-offsets-alist element \"(%s . '%s)\" was mistakenly quoted"
6927 symbol (cadr offset)))
6928 (let (done)
6929 (while (and (not done) offset)
6930 (setq done (c-evaluate-offset (car offset) langelem symbol)
6931 offset (cdr offset)))
6932 (if (and c-strict-syntax-p (not done))
6933 (c-benign-error "No offset found for syntactic symbol %s" symbol))
6934 done))
6935 (t (symbol-value offset))
6938 (defun c-calc-offset (langelem)
6939 ;; Get offset from LANGELEM which is a list beginning with the
6940 ;; syntactic symbol and followed by any analysis data it provides.
6941 ;; That data may be zero or more elements, but if at least one is
6942 ;; given then the first is the relpos (or nil). The symbol is
6943 ;; matched against `c-offsets-alist' and the offset calculated from
6944 ;; that is returned.
6945 (let* ((symbol (car langelem))
6946 (match (assq symbol c-offsets-alist))
6947 (offset (cdr-safe match)))
6948 (if match
6949 (setq offset (c-evaluate-offset offset langelem symbol))
6950 (if c-strict-syntax-p
6951 (c-benign-error "No offset found for syntactic symbol %s" symbol))
6952 (setq offset 0))
6953 (if (vectorp offset)
6954 offset
6955 (or (and (numberp offset) offset)
6956 (and (symbolp offset) (symbol-value offset))
6960 (defun c-get-offset (langelem)
6961 ;; This is a compatibility wrapper for `c-calc-offset' in case
6962 ;; someone is calling it directly. It takes an old style syntactic
6963 ;; element on the form (SYMBOL . RELPOS) and converts it to the new
6964 ;; list form.
6965 (if (cdr langelem)
6966 (c-calc-offset (list (car langelem) (cdr langelem)))
6967 (c-calc-offset langelem)))
6969 (defun c-get-syntactic-indentation (langelems)
6970 ;; Calculate the syntactic indentation from a syntactic description
6971 ;; as returned by `c-guess-syntax'.
6973 ;; Note that topmost-intro always has a relpos at bol, for
6974 ;; historical reasons. It's often used together with other symbols
6975 ;; that has more sane positions. Since we always use the first
6976 ;; found relpos, we rely on that these other symbols always precede
6977 ;; topmost-intro in the LANGELEMS list.
6978 (let ((indent 0) anchor)
6980 (while langelems
6981 (let* ((c-syntactic-element (car langelems))
6982 (res (c-calc-offset c-syntactic-element)))
6984 (if (vectorp res)
6985 ;; Got an absolute column that overrides any indentation
6986 ;; we've collected so far, but not the relative
6987 ;; indentation we might get for the nested structures
6988 ;; further down the langelems list.
6989 (setq indent (elt res 0)
6990 anchor (point-min)) ; A position at column 0.
6992 ;; Got a relative change of the current calculated
6993 ;; indentation.
6994 (setq indent (+ indent res))
6996 ;; Use the anchor position from the first syntactic
6997 ;; element with one.
6998 (unless anchor
6999 (let ((relpos (car-safe (cdr (car langelems)))))
7000 (if relpos
7001 (setq anchor relpos)))))
7003 (setq langelems (cdr langelems))))
7005 (if anchor
7006 (+ indent (save-excursion
7007 (goto-char anchor)
7008 (current-column)))
7009 indent)))
7012 (cc-provide 'cc-engine)
7014 ;;; arch-tag: 149add18-4673-4da5-ac47-6805e4eae089
7015 ;;; cc-engine.el ends here