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