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)
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.
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
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
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.
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
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.
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:
97 ;; Put on the last char of the token preceding each declaration
98 ;; inside a declaration style arglist (typically in a function
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').
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.
120 ;; Used in AWK mode to mark the various kinds of newlines. See
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)
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 ()
158 ,@(mapcan (lambda (init)
160 `(defvar ,(car init) nil ,(elt init 2))
161 `(defvar ,(car init) nil))
162 (make-variable-buffer-local ',(car init))))
163 (cdr c-lang-variable-inits))))
164 (c-declare-lang-variables)
167 ;;; Internal state variables.
169 ;; Internal state of hungry delete key feature
170 (defvar c-hungry-delete-key nil)
171 (make-variable-buffer-local 'c-hungry-delete-key)
173 ;; Internal state of auto newline feature.
174 (defvar c-auto-newline nil)
175 (make-variable-buffer-local 'c-auto-newline)
177 ;; Internal auto-newline/hungry-delete designation string for mode line.
178 (defvar c-auto-hungry-string nil)
179 (make-variable-buffer-local 'c-auto-hungry-string)
181 (defun c-calculate-state (arg prevstate)
182 ;; Calculate the new state of PREVSTATE, t or nil, based on arg. If
183 ;; arg is nil or zero, toggle the state. If arg is negative, turn
184 ;; the state off, and if arg is positive, turn the state on
186 (zerop (setq arg (prefix-numeric-value arg))))
190 ;; Dynamically bound cache for `c-in-literal'.
191 (defvar c-in-literal-cache t)
193 ;; Must be set in buffers where the `c-type' text property might be used
194 ;; with the value `c-decl-end'.
195 (defvar c-type-decl-end-used nil)
196 (make-variable-buffer-local 'c-type-decl-end-used)
199 ;;; Basic utility functions.
201 (defun c-syntactic-content (from to)
202 ;; Return the given region as a string where all syntactic
203 ;; whitespace is removed or, where necessary, replaced with a single
207 (let* ((parts (list nil)) (tail parts) pos)
208 (while (re-search-forward c-syntactic-ws-start to t)
209 (goto-char (setq pos (match-beginning 0)))
210 (c-forward-syntactic-ws to)
213 (if (and (> pos from)
215 (looking-at "\\w\\|\\s_")
218 (looking-at "\\w\\|\\s_")))
220 (setcdr tail (list (buffer-substring-no-properties from pos)
222 (setq tail (cddr tail)))
223 (setcdr tail (list (buffer-substring-no-properties from pos)))
224 (setq tail (cdr tail)))
225 (setq from (point))))
226 (setcdr tail (list (buffer-substring-no-properties from to)))
227 (apply 'concat (cdr parts)))))
229 (defsubst c-keyword-sym (keyword)
230 ;; Return non-nil if the string KEYWORD is a known keyword. More
231 ;; precisely, the value is the symbol for the keyword in
232 ;; `c-keywords-obarray'.
233 (intern-soft keyword c-keywords-obarray))
235 (defsubst c-keyword-member (keyword-sym lang-constant)
236 ;; Return non-nil if the symbol KEYWORD-SYM, as returned by
237 ;; `c-keyword-sym', is a member of LANG-CONSTANT, which is the name
238 ;; of a language constant that ends with "-kwds". If KEYWORD-SYM is
239 ;; nil then the result is nil.
240 (get keyword-sym lang-constant))
242 ;; String syntax chars, suitable for skip-syntax-(forward|backward).
243 (defconst c-string-syntax (if (memq 'gen-string-delim c-emacs-features)
247 ;; Regexp matching string start syntax.
248 (defconst c-string-limit-regexp (if (memq 'gen-string-delim c-emacs-features)
252 ;; Holds formatted error strings for the few cases where parse errors
254 (defvar c-parsing-error nil)
255 (make-variable-buffer-local 'c-parsing-error)
257 (defun c-echo-parsing-error (&optional quiet)
258 ;; This function does not do any hidden buffer changes.
259 (when (and c-report-syntactic-errors c-parsing-error (not quiet))
260 (c-benign-error "%s" c-parsing-error))
263 ;; Faces given to comments and string literals. This is used in some
264 ;; situations to speed up recognition; it isn't mandatory that font
265 ;; locking is in use. This variable is extended with the face in
266 ;; `c-doc-face-name' when fontification is activated in cc-fonts.el.
267 (defconst c-literal-faces
268 '(font-lock-comment-face font-lock-string-face))
271 ;; Some debug tools to visualize various special positions. This
272 ;; debug code isn't as portable as the rest of CC Mode.
274 (cc-bytecomp-defun overlays-in)
275 (cc-bytecomp-defun overlay-get)
276 (cc-bytecomp-defun overlay-start)
277 (cc-bytecomp-defun overlay-end)
278 (cc-bytecomp-defun delete-overlay)
279 (cc-bytecomp-defun overlay-put)
280 (cc-bytecomp-defun make-overlay)
282 (defun c-debug-add-face (beg end face)
283 (c-save-buffer-state ((overlays (overlays-in beg end)) overlay)
285 (setq overlay (car overlays)
286 overlays (cdr overlays))
287 (when (eq (overlay-get overlay 'face) face)
288 (setq beg (min beg (overlay-start overlay))
289 end (max end (overlay-end overlay)))
290 (delete-overlay overlay)))
291 (overlay-put (make-overlay beg end) 'face face)))
293 (defun c-debug-remove-face (beg end face)
294 (c-save-buffer-state ((overlays (overlays-in beg end)) overlay
295 (ol-beg beg) (ol-end end))
297 (setq overlay (car overlays)
298 overlays (cdr overlays))
299 (when (eq (overlay-get overlay 'face) face)
300 (setq ol-beg (min ol-beg (overlay-start overlay))
301 ol-end (max ol-end (overlay-end overlay)))
302 (delete-overlay overlay)))
304 (overlay-put (make-overlay ol-beg beg) 'face face))
306 (overlay-put (make-overlay end ol-end) 'face face))))
309 ;; `c-beginning-of-statement-1' and accompanying stuff.
311 ;; KLUDGE ALERT: c-maybe-labelp is used to pass information between
312 ;; c-crosses-statement-barrier-p and c-beginning-of-statement-1. A
313 ;; better way should be implemented, but this will at least shut up
314 ;; the byte compiler.
315 (defvar c-maybe-labelp nil)
317 ;; New awk-compatible version of c-beginning-of-statement-1, ACM 2002/6/22
319 ;; Macros used internally in c-beginning-of-statement-1 for the
320 ;; automaton actions.
321 (defmacro c-bos-push-state ()
322 '(setq stack (cons (cons state saved-pos)
324 (defmacro c-bos-pop-state (&optional do-if-done)
325 `(if (setq state (car (car stack))
326 saved-pos (cdr (car stack))
331 (defmacro c-bos-pop-state-and-retry ()
332 '(throw 'loop (setq state (car (car stack))
333 saved-pos (cdr (car stack))
334 ;; Throw nil if stack is empty, else throw non-nil.
336 (defmacro c-bos-save-pos ()
337 '(setq saved-pos (vector pos tok ptok pptok)))
338 (defmacro c-bos-restore-pos ()
339 '(unless (eq (elt saved-pos 0) start)
340 (setq pos (elt saved-pos 0)
341 tok (elt saved-pos 1)
342 ptok (elt saved-pos 2)
343 pptok (elt saved-pos 3))
346 (defmacro c-bos-save-error-info (missing got)
347 `(setq saved-pos (vector pos ,missing ,got)))
348 (defmacro c-bos-report-error ()
350 (setq c-parsing-error
351 (format "No matching `%s' found for `%s' on line %d"
354 (1+ (count-lines (point-min)
355 (c-point 'bol (elt saved-pos 0))))))))
357 (defun c-beginning-of-statement-1 (&optional lim ignore-labels
359 "Move to the start of the current statement or declaration, or to
360 the previous one if already at the beginning of one. Only
361 statements/declarations on the same level are considered, i.e. don't
362 move into or out of sexps (not even normal expression parentheses).
364 Stop at statement continuation tokens like \"else\", \"catch\",
365 \"finally\" and the \"while\" in \"do ... while\" if the start point
366 is within the continuation. If starting at such a token, move to the
367 corresponding statement start. If at the beginning of a statement,
368 move to the closest containing statement if there is any. This might
369 also stop at a continuation clause.
371 Labels are treated as separate statements if IGNORE-LABELS is non-nil.
372 The function is not overly intelligent in telling labels from other
373 uses of colons; if used outside a statement context it might trip up
374 on e.g. inherit colons, so IGNORE-LABELS should be used then. There
375 should be no such mistakes in a statement context, however.
377 Macros are ignored unless point is within one, in which case the
378 content of the macro is treated as normal code. Aside from any normal
379 statement starts found in it, stop at the first token of the content
380 in the macro, i.e. the expression of an \"#if\" or the start of the
381 definition in a \"#define\". Also stop at start of macros before
384 Return 'label if stopped at a label, 'same if stopped at the beginning
385 of the current statement, 'up if stepped to a containing statement,
386 'previous if stepped to a preceding statement, 'beginning if stepped
387 from a statement continuation clause to its start clause, or 'macro if
388 stepped to a macro start. Note that 'same and not 'label is returned
389 if stopped at the same label without crossing the colon character.
391 LIM may be given to limit the search. If the search hits the limit,
392 point will be left at the closest following token, or at the start
393 position if that is less ('same is returned in this case).
395 NOERROR turns off error logging to `c-parsing-error'.
397 Normally only ';' is considered to delimit statements, but if
398 COMMA-DELIM is non-nil then ',' is treated likewise."
400 ;; The bulk of this function is a pushdown automaton that looks at statement
401 ;; boundaries and the tokens (such as "while") in c-opt-block-stmt-key. Its
402 ;; purpose is to keep track of nested statements, ensuring that such
403 ;; statments are skipped over in their entirety (somewhat akin to what C-M-p
404 ;; does with nested braces/brackets/parentheses).
406 ;; Note: The position of a boundary is the following token.
408 ;; Beginning with the current token (the one following point), move back one
409 ;; sexp at a time (where a sexp is, more or less, either a token or the
410 ;; entire contents of a brace/bracket/paren pair). Each time a statement
411 ;; boundary is crossed or a "while"-like token is found, update the state of
412 ;; the PDA. Stop at the beginning of a statement when the stack (holding
413 ;; nested statement info) is empty and the position has been moved.
415 ;; The following variables constitute the PDA:
417 ;; sym: This is either the "while"-like token (e.g. 'for) we've just
418 ;; scanned back over, 'boundary if we've just gone back over a
419 ;; statement boundary, or nil otherwise.
420 ;; state: takes one of the values (nil else else-boundary while
421 ;; while-boundary catch catch-boundary).
422 ;; nil means "no "while"-like token yet scanned".
423 ;; 'else, for example, means "just gone back over an else".
424 ;; 'else-boundary means "just gone back over a statement boundary
425 ;; immediately after having gone back over an else".
426 ;; saved-pos: A vector of either saved positions (tok ptok pptok, etc.) or
427 ;; of error reporting information.
428 ;; stack: The stack onto which the PDA pushes its state. Each entry
429 ;; consists of a saved value of state and saved-pos. An entry is
430 ;; pushed when we move back over a "continuation" token (e.g. else)
431 ;; and popped when we encounter the corresponding opening token
435 ;; The following diagram briefly outlines the PDA.
438 ;; "else": Push state, goto state `else'.
439 ;; "while": Push state, goto state `while'.
440 ;; "catch" or "finally": Push state, goto state `catch'.
441 ;; boundary: Pop state.
442 ;; other: Do nothing special.
445 ;; boundary: Goto state `else-boundary'.
446 ;; other: Error, pop state, retry token.
448 ;; State `else-boundary':
450 ;; boundary: Error, pop state.
451 ;; other: See common state.
454 ;; boundary: Save position, goto state `while-boundary'.
455 ;; other: Pop state, retry token.
457 ;; State `while-boundary':
459 ;; boundary: Restore position if it's not at start, pop state. [*see below]
460 ;; other: See common state.
463 ;; boundary: Goto state `catch-boundary'.
464 ;; other: Error, pop state, retry token.
466 ;; State `catch-boundary':
468 ;; "catch": Goto state `catch'.
469 ;; boundary: Error, pop state.
470 ;; other: See common state.
472 ;; [*] In the `while-boundary' state, we had pushed a 'while state, and were
473 ;; searching for a "do" which would have opened a do-while. If we didn't
474 ;; find it, we discard the analysis done since the "while", go back to this
475 ;; token in the buffer and restart the scanning there, this time WITHOUT
476 ;; pushing the 'while state onto the stack.
478 ;; In addition to the above there is some special handling of labels
481 (let ((case-fold-search nil)
484 (delims (if comma-delim '(?\; ?,) '(?\;)))
485 (c-stmt-delim-chars (if comma-delim
486 c-stmt-delim-chars-with-comma
488 pos ; Current position.
489 boundary-pos ; Position of last stmt boundary character (e.g. ;).
490 after-labels-pos ; Value of tok after first found colon.
491 last-label-pos ; Value of tok after last found colon.
492 sym ; Symbol just scanned back over (e.g. 'while or
493 ; 'boundary). See above
494 state ; Current state in the automaton. See above.
495 saved-pos ; Current saved positions. See above
496 stack ; Stack of conses (state . saved-pos).
497 (cond-key (or c-opt-block-stmt-key ; regexp which matches "for", "if", etc.
498 "\\<\\>")) ; Matches nothing.
499 (ret 'same) ; Return value.
500 tok ptok pptok ; Pos of last three sexps or bounds.
501 c-in-literal-cache c-maybe-labelp saved)
504 (if lim (narrow-to-region lim (point-max)))
507 (and (c-beginning-of-macro)
509 (setq macro-start (point)))
511 ;; Try to skip back over unary operator characters, to register
515 (if (c-mode-is-new-awk-p)
516 (c-awk-backward-syntactic-ws)
517 (c-backward-syntactic-ws))
518 (/= (skip-chars-backward "-+!*&~@`#") 0))) ; ACM, 2002/5/31;
522 ;; Skip back over any semicolon here. If it was a bare semicolon, we're
523 ;; done. Later on we ignore the boundaries for statements that doesn't
524 ;; contain any sexp. The only thing that is affected is that the error
525 ;; checking is a little less strict, and we really don't bother.
526 (if (and (memq (char-before) delims)
527 (progn (forward-char -1)
529 (if (c-mode-is-new-awk-p)
530 (c-awk-backward-syntactic-ws)
531 (c-backward-syntactic-ws))
532 (or (memq (char-before) delims)
533 (memq (char-before) '(?: nil))
534 (eq (char-syntax (char-before)) ?\()
535 (and (c-mode-is-new-awk-p)
536 (c-awk-after-logical-semicolon))))) ; ACM 2002/6/22
537 ;; ACM, 2002/7/20: What about giving a limit to the above function?
538 ;; ACM, 2003/6/16: The above two lines (checking for
539 ;; awk-logical-semicolon) are probably redundant after rewriting
540 ;; c-awk-backward-syntactic-ws.
544 ;; Begin at start and not pos to detect macros if we stand
545 ;; directly after the #.
547 (if (looking-at "\\<\\|\\W")
548 ;; Record this as the first token if not starting inside it.
551 ;; The following while loop goes back one sexp (balanced parens,
552 ;; etc. with contents, or symbol or suchlike) each iteration. This
553 ;; movement is accomplished with a call to scan-sexps approx 130 lines
556 (catch 'loop ;; Throw nil to break, non-nil to continue.
558 ;; Check for macro start. Take this out for AWK Mode (ACM, 2002/5/31)
559 ;; NO!! just make sure macro-start is nil in AWK Mode (ACM, 2002/6/22)
560 ;; It always is (ACM, 2002/6/23)
563 (progn (skip-chars-backward " \t")
564 (eq (char-before) ?#))
565 (progn (setq saved (1- (point)))
567 (not (eq (char-before (1- (point))) ?\\)))
568 (looking-at c-opt-cpp-start)
569 (progn (skip-chars-forward " \t")
570 (eq (point) saved))))
572 (if (and (c-forward-to-cpp-define-body)
573 (progn (c-forward-syntactic-ws start)
575 ;; Stop at the first token in the content of the macro.
577 ignore-labels t) ; Avoid the label check on exit.
583 ;; Do a round through the automaton if we've just passed a
584 ;; statement boundary or passed a "while"-like token.
586 (and (looking-at cond-key)
587 (setq sym (intern (match-string 1)))))
589 (when (and (< pos start) (null stack))
592 ;; The PDA state handling.
594 ;; Refer to the description of the PDA in the openining
595 ;; comments. In the following OR form, the first leaf
596 ;; attempts to handles one of the specific actions detailed
597 ;; (e.g., finding token "if" whilst in state `else-boundary').
598 ;; We drop through to the second leaf (which handles common
599 ;; state) if no specific handler is found in the first cond.
600 ;; If a parsing error is detected (e.g. an "else" with no
601 ;; preceding "if"), we throw to the enclosing catch.
603 ;; Note that the (eq state 'else) means
604 ;; "we've just passed an else", NOT "we're looking for an
608 (if (eq sym 'boundary)
609 (setq state 'else-boundary)
611 (c-bos-pop-state-and-retry)))
613 ((eq state 'else-boundary)
615 (c-bos-pop-state (setq ret 'beginning)))
621 (if (and (eq sym 'boundary)
622 ;; Since this can cause backtracking we do a
623 ;; little more careful analysis to avoid it:
624 ;; If there's a label in front of the while
625 ;; it can't be part of a do-while.
626 (not after-labels-pos))
627 (progn (c-bos-save-pos)
628 (setq state 'while-boundary))
629 (c-bos-pop-state-and-retry))) ; Can't be a do-while
631 ((eq state 'while-boundary)
633 (c-bos-pop-state (setq ret 'beginning)))
634 ((eq sym 'boundary) ; isn't a do-while
635 (c-bos-restore-pos) ; the position of the while
636 (c-bos-pop-state)))) ; no longer searching for do.
639 (if (eq sym 'boundary)
640 (setq state 'catch-boundary)
642 (c-bos-pop-state-and-retry)))
644 ((eq state 'catch-boundary)
647 (c-bos-pop-state (setq ret 'beginning)))
652 (c-bos-pop-state)))))
654 ;; This is state common. We get here when the previous
655 ;; cond statement found no particular state handler.
656 (cond ((eq sym 'boundary)
657 ;; If we have a boundary at the start
658 ;; position we push a frame to go to the
659 ;; previous statement.
665 (c-bos-save-error-info 'if 'else)
668 (when (or (not pptok)
669 (memq (char-after pptok) delims)
670 (and (c-mode-is-new-awk-p)
672 ;; might we be calling this from
673 ;; c-awk-after-if-do-for-while-condition-p?
674 ;; If so, avoid infinite recursion.
675 (and (eq (point) start)
676 (c-awk-NL-prop-not-set))
677 ;; The following may recursively
678 ;; call this function.
679 (c-awk-completed-stmt-ws-ends-line-p pptok))))
680 ;; Since this can cause backtracking we do a
681 ;; little more careful analysis to avoid it: If
682 ;; the while isn't followed by a semicolon it
683 ;; can't be a do-while.
684 ;; ACM, 2002/5/31; IT CAN IN AWK Mode. ;-(
686 (setq state 'while)))
687 ((memq sym '(catch finally))
689 (c-bos-save-error-info 'try sym)
690 (setq state 'catch))))
693 ;; We're either past a statement boundary or at the
694 ;; start of a statement, so throw away any label data
695 ;; for the previous one.
696 (setq after-labels-pos nil
698 c-maybe-labelp nil))))
700 ;; Step to the previous sexp, but not if we crossed a
701 ;; boundary, since that doesn't consume an sexp.
702 (if (eq sym 'boundary)
705 ;; HERE IS THE SINGLE PLACE INSIDE THE PDA LOOP WHERE WE MOVE
706 ;; BACKWARDS THROUGH THE SOURCE. The following loop goes back
707 ;; one sexp and then only loops in special circumstances (line
708 ;; continuations and skipping past entire macros).
711 (or (c-safe (goto-char (scan-sexps (point) -1)) t)
712 ;; Give up if we hit an unbalanced block.
713 ;; Since the stack won't be empty the code
714 ;; below will report a suitable error.
716 (cond ((looking-at "\\\\$")
717 ;; Step again if we hit a line continuation.
720 ;; If we started inside a macro then this
721 ;; sexp is always interesting.
723 ((not (c-mode-is-new-awk-p)) ; Changed from t, ACM 2002/6/25
724 ;; Otherwise check that we didn't step
725 ;; into a macro from the end.
728 (and (c-beginning-of-macro)
731 (goto-char macro-start)
734 ;; Did the last movement by a sexp cross a statement boundary?
735 (when (save-excursion
736 (if (if (eq (char-after) ?{)
737 (c-looking-at-inexpr-block lim nil)
738 (looking-at "\\s\("))
740 ;; Should not include the paren sexp we've
741 ;; passed over in the boundary check.
742 (if (> (point) (- pos 100))
745 ;; Find its end position this way instead of
746 ;; moving forward if the sexp is large.
750 (goto-char (1+ (c-down-list-backward)))
752 ;; Check that we didn't step into
753 ;; a macro from the end.
756 (and (c-beginning-of-macro)
759 (goto-char macro-start)
762 (setq boundary-pos (c-crosses-statement-barrier-p
769 (throw 'loop t))) ; like a C "continue". Analyze the next sexp.
771 (when (and (numberp c-maybe-labelp) (not ignore-labels))
772 ;; c-crosses-statement-barrier-p has found a colon, so
773 ;; we might be in a label now.
774 (if (not after-labels-pos)
775 (setq after-labels-pos tok))
776 (setq last-label-pos tok
780 (when (and c-opt-method-key
781 (setq saved (c-in-method-def-p)))
783 ignore-labels t) ; Avoid the label check on exit.
786 ;; We've moved back by a sexp, so update the token positions.
791 pos tok))) ; Not nil (for the while loop).
793 ;; If the stack isn't empty there might be errors to report.
795 (if (and (vectorp saved-pos) (eq (length saved-pos) 3))
796 (c-bos-report-error))
797 (setq saved-pos (cdr (car stack))
800 (when (and (eq ret 'same)
801 (not (memq sym '(boundary ignore nil))))
802 ;; Need to investigate closer whether we've crossed
803 ;; between a substatement and its containing statement.
804 (if (setq saved (if (looking-at c-block-stmt-1-key)
807 (cond ((> start saved) (setq pos saved))
808 ((= start saved) (setq ret 'up)))))
810 (when (and c-maybe-labelp
812 (not (eq ret 'beginning))
814 ;; We're in a label. Maybe we should step to the statement
816 (if (< after-labels-pos start)
817 (setq pos after-labels-pos)
819 (if (< last-label-pos start)
820 (setq pos last-label-pos)))))
822 ;; Skip over the unary operators that can start the statement.
825 (if (c-mode-is-new-awk-p)
826 (c-awk-backward-syntactic-ws)
827 (c-backward-syntactic-ws))
828 (/= (skip-chars-backward "-+!*&~@`#") 0)) ; Hopefully the # won't hurt awk.
833 (defun c-crosses-statement-barrier-p (from to)
834 "Return non-nil if buffer positions FROM to TO cross one or more
835 statement or declaration boundaries. The returned value is actually
836 the position of the earliest boundary char. FROM must not be within
839 The variable `c-maybe-labelp' is set to the position of the first `:' that
840 might start a label (i.e. not part of `::' and not preceded by `?'). If a
841 single `?' is found, then `c-maybe-labelp' is cleared."
842 (let ((skip-chars c-stmt-delim-chars)
847 (while (progn (skip-chars-forward skip-chars to)
849 (if (setq lit-range (c-literal-limits from)) ; Have we landed in a string/comment?
850 (progn (goto-char (setq from (cdr lit-range)))
851 (if (and (c-mode-is-new-awk-p) (bolp)) ; ACM 2002/7/17. Make sure we
852 (backward-char))) ; don't skip over a virtual semi-colon after an awk comment. :-(
853 (cond ((eq (char-after) ?:)
855 (if (and (eq (char-after) ?:)
857 ;; Ignore scope operators.
859 (setq c-maybe-labelp (1- (point)))))
860 ((eq (char-after) ??)
861 ;; A question mark. Can't be a label, so stop
862 ;; looking for more : and ?.
863 (setq c-maybe-labelp nil
864 skip-chars (substring c-stmt-delim-chars 0 -2)))
865 ((and (eolp) ; Can only happen in AWK Mode
866 (not (c-awk-completed-stmt-ws-ends-line-p)))
868 ((and (c-mode-is-new-awk-p)
869 (bolp) lit-range ; awk: comment/string ended prev line.
870 (not (c-awk-completed-stmt-ws-ends-prev-line-p))))
871 (t (throw 'done (point))))))
875 ;; A set of functions that covers various idiosyncrasies in
876 ;; implementations of `forward-comment'.
878 ;; Note: Some emacsen considers incorrectly that any line comment
879 ;; ending with a backslash continues to the next line. I can't think
880 ;; of any way to work around that in a reliable way without changing
881 ;; the buffer, though. Suggestions welcome. ;) (No, temporarily
882 ;; changing the syntax for backslash doesn't work since we must treat
883 ;; escapes in string literals correctly.)
885 (defun c-forward-single-comment ()
886 "Move forward past whitespace and the closest following comment, if any.
887 Return t if a comment was found, nil otherwise. In either case, the
888 point is moved past the following whitespace. Line continuations,
889 i.e. a backslashes followed by line breaks, are treated as whitespace.
890 The line breaks that end line comments are considered to be the
891 comment enders, so the point will be put on the beginning of the next
892 line if it moved past a line comment.
894 This function does not do any hidden buffer changes."
896 (let ((start (point)))
897 (when (looking-at "\\([ \t\n\r\f\v]\\|\\\\[\n\r]\\)+")
898 (goto-char (match-end 0)))
900 (when (forward-comment 1)
902 ;; Some emacsen (e.g. XEmacs 21) return t when moving
906 ;; Emacs includes the ending newline in a b-style (c++)
907 ;; comment, but XEmacs doesn't. We depend on the Emacs
908 ;; behavior (which also is symmetric).
909 (if (and (eolp) (elt (parse-partial-sexp start (point)) 7))
910 (condition-case nil (forward-char 1)))
914 (defsubst c-forward-comments ()
915 "Move forward past all following whitespace and comments.
916 Line continuations, i.e. a backslashes followed by line breaks, are
917 treated as whitespace.
919 This function does not do any hidden buffer changes."
922 ;; If forward-comment in at least XEmacs 21 is given a large
923 ;; positive value, it'll loop all the way through if it hits
925 (and (forward-comment 5)
926 ;; Some emacsen (e.g. XEmacs 21) return t when moving
930 (when (looking-at "\\\\[\n\r]")
934 (defun c-backward-single-comment ()
935 "Move backward past whitespace and the closest preceding comment, if any.
936 Return t if a comment was found, nil otherwise. In either case, the
937 point is moved past the preceding whitespace. Line continuations,
938 i.e. a backslashes followed by line breaks, are treated as whitespace.
939 The line breaks that end line comments are considered to be the
940 comment enders, so the point cannot be at the end of the same line to
941 move over a line comment.
943 This function does not do any hidden buffer changes."
945 (let ((start (point)))
946 ;; When we got newline terminated comments, forward-comment in all
947 ;; supported emacsen so far will stop at eol of each line not
948 ;; ending with a comment when moving backwards. This corrects for
949 ;; that, and at the same time handles line continuations.
951 (skip-chars-backward " \t\n\r\f\v")
952 (and (looking-at "[\n\r]")
953 (eq (char-before) ?\\)
958 ;; Some emacsen (e.g. Emacs 19.34) return t when moving
962 ;; Leave point after the closest following newline if we've
963 ;; backed up over any above, since forward-comment won't move
964 ;; backward over a line comment if point is at the end of the
966 (re-search-forward "\\=\\s *[\n\r]" start t)
968 (if (if (forward-comment -1)
970 ;; If forward-comment above succeeded and we're at eol
971 ;; then the newline we moved over above didn't end a
972 ;; line comment, so we give it another go.
976 ;; Emacs <= 20 and XEmacs move back over the closer of a
977 ;; block comment that lacks an opener.
978 (if (looking-at "\\*/")
979 (progn (forward-char 2) nil)
982 (defsubst c-backward-comments ()
983 "Move backward past all preceding whitespace and comments.
984 Line continuations, i.e. a backslashes followed by line breaks, are
985 treated as whitespace. The line breaks that end line comments are
986 considered to be the comment enders, so the point cannot be at the end
987 of the same line to move over a line comment.
989 This function does not do any hidden buffer changes."
991 (let ((start (point)))
993 ;; `forward-comment' in some emacsen (e.g. Emacs 19.34)
994 ;; return t when moving backwards at bob.
997 (if (forward-comment -1)
998 (if (looking-at "\\*/")
999 ;; Emacs <= 20 and XEmacs move back over the
1000 ;; closer of a block comment that lacks an opener.
1001 (progn (forward-char 2) nil)
1004 ;; XEmacs treats line continuations as whitespace but
1005 ;; only in the backward direction, which seems a bit
1006 ;; odd. Anyway, this is necessary for Emacs.
1007 (when (and (looking-at "[\n\r]")
1008 (eq (char-before) ?\\)
1014 ;; Basic handling of preprocessor directives.
1016 ;; This is a dynamically bound cache used together with
1017 ;; `c-query-macro-start' and `c-query-and-set-macro-start'. It only
1018 ;; works as long as point doesn't cross a macro boundary.
1019 (defvar c-macro-start 'unknown)
1021 (defsubst c-query-and-set-macro-start ()
1022 ;; This function does not do any hidden buffer changes.
1023 (if (symbolp c-macro-start)
1024 (setq c-macro-start (save-excursion
1025 (and (c-beginning-of-macro)
1029 (defsubst c-query-macro-start ()
1030 ;; This function does not do any hidden buffer changes.
1031 (if (symbolp c-macro-start)
1033 (and (c-beginning-of-macro)
1037 (defun c-beginning-of-macro (&optional lim)
1038 "Go to the beginning of a preprocessor directive.
1039 Leave point at the beginning of the directive and return t if in one,
1040 otherwise return nil and leave point unchanged.
1042 This function does not do any hidden buffer changes."
1043 (when c-opt-cpp-prefix
1044 (let ((here (point)))
1046 (if lim (narrow-to-region lim (point-max)))
1048 (while (eq (char-before (1- (point))) ?\\)
1050 (back-to-indentation)
1051 (if (and (<= (point) here)
1052 (looking-at c-opt-cpp-start))
1057 (defun c-end-of-macro ()
1058 "Go to the end of a preprocessor directive.
1059 More accurately, move point to the end of the closest following line
1060 that doesn't end with a line continuation backslash.
1062 This function does not do any hidden buffer changes."
1065 (when (and (eq (char-before) ?\\)
1070 (defun c-forward-to-cpp-define-body ()
1071 ;; Assuming point is at the "#" that introduces a preprocessor
1072 ;; directive, it's moved forward to the start of the definition body
1073 ;; if it's a "#define". Non-nil is returned in this case, in all
1074 ;; other cases nil is returned and point isn't moved.
1075 (when (and (looking-at
1077 "define[ \t]+\\(\\sw\\|_\\)+\\(\([^\)]*\)\\)?"
1078 "\\([ \t]\\|\\\\\n\\)*"))
1079 (not (= (match-end 0) (c-point 'eol))))
1080 (goto-char (match-end 0))))
1083 ;; Tools for skipping over syntactic whitespace.
1085 ;; The following functions use text properties to cache searches over
1086 ;; large regions of syntactic whitespace. It works as follows:
1088 ;; o If a syntactic whitespace region contains anything but simple
1089 ;; whitespace (i.e. space, tab and line breaks), the text property
1090 ;; `c-in-sws' is put over it. At places where we have stopped
1091 ;; within that region there's also a `c-is-sws' text property.
1092 ;; That since there typically are nested whitespace inside that
1093 ;; must be handled separately, e.g. whitespace inside a comment or
1094 ;; cpp directive. Thus, from one point with `c-is-sws' it's safe
1095 ;; to jump to another point with that property within the same
1096 ;; `c-in-sws' region. It can be likened to a ladder where
1097 ;; `c-in-sws' marks the bars and `c-is-sws' the rungs.
1099 ;; o The `c-is-sws' property is put on the simple whitespace chars at
1100 ;; a "rung position" and also maybe on the first following char.
1101 ;; As many characters as can be conveniently found in this range
1102 ;; are marked, but no assumption can be made that the whole range
1103 ;; is marked (it could be clobbered by later changes, for
1106 ;; Note that some part of the beginning of a sequence of simple
1107 ;; whitespace might be part of the end of a preceding line comment
1108 ;; or cpp directive and must not be considered part of the "rung".
1109 ;; Such whitespace is some amount of horizontal whitespace followed
1110 ;; by a newline. In the case of cpp directives it could also be
1111 ;; two newlines with horizontal whitespace between them.
1113 ;; The reason to include the first following char is to cope with
1114 ;; "rung positions" that doesn't have any ordinary whitespace. If
1115 ;; `c-is-sws' is put on a token character it does not have
1116 ;; `c-in-sws' set simultaneously. That's the only case when that
1117 ;; can occur, and the reason for not extending the `c-in-sws'
1118 ;; region to cover it is that the `c-in-sws' region could then be
1119 ;; accidentally merged with a following one if the token is only
1120 ;; one character long.
1122 ;; o On buffer changes the `c-in-sws' and `c-is-sws' properties are
1123 ;; removed in the changed region. If the change was inside
1124 ;; syntactic whitespace that means that the "ladder" is broken, but
1125 ;; a later call to `c-forward-sws' or `c-backward-sws' will use the
1126 ;; parts on either side and use an ordinary search only to "repair"
1129 ;; Special care needs to be taken if a region is removed: If there
1130 ;; are `c-in-sws' on both sides of it which do not connect inside
1131 ;; the region then they can't be joined. If e.g. a marked macro is
1132 ;; broken, syntactic whitespace inside the new text might be
1133 ;; marked. If those marks would become connected with the old
1134 ;; `c-in-sws' range around the macro then we could get a ladder
1135 ;; with one end outside the macro and the other at some whitespace
1138 ;; The main motivation for this system is to increase the speed in
1139 ;; skipping over the large whitespace regions that can occur at the
1140 ;; top level in e.g. header files that contain a lot of comments and
1141 ;; cpp directives. For small comments inside code it's probably
1142 ;; slower than using `forward-comment' straightforwardly, but speed is
1143 ;; not a significant factor there anyway.
1145 ; (defface c-debug-is-sws-face
1146 ; '((t (:background "GreenYellow")))
1147 ; "Debug face to mark the `c-is-sws' property.")
1148 ; (defface c-debug-in-sws-face
1149 ; '((t (:underline t)))
1150 ; "Debug face to mark the `c-in-sws' property.")
1152 ; (defun c-debug-put-sws-faces ()
1153 ; ;; Put the sws debug faces on all the `c-is-sws' and `c-in-sws'
1154 ; ;; properties in the buffer.
1158 ; (goto-char (point-min))
1159 ; (setq in-face (if (get-text-property (point) 'c-is-sws)
1162 ; (goto-char (next-single-property-change
1163 ; (point) 'c-is-sws nil (point-max)))
1166 ; (c-debug-add-face in-face (point) 'c-debug-is-sws-face)
1167 ; (setq in-face nil))
1168 ; (setq in-face (point)))
1170 ; (goto-char (point-min))
1171 ; (setq in-face (if (get-text-property (point) 'c-in-sws)
1174 ; (goto-char (next-single-property-change
1175 ; (point) 'c-in-sws nil (point-max)))
1178 ; (c-debug-add-face in-face (point) 'c-debug-in-sws-face)
1179 ; (setq in-face nil))
1180 ; (setq in-face (point)))
1183 (defmacro c-debug-sws-msg (&rest args)
1187 (defmacro c-put-is-sws (beg end)
1188 `(let ((beg ,beg) (end ,end))
1189 (put-text-property beg end 'c-is-sws t)
1190 ,@(when (facep 'c-debug-is-sws-face)
1191 `((c-debug-add-face beg end 'c-debug-is-sws-face)))))
1193 (defmacro c-put-in-sws (beg end)
1194 `(let ((beg ,beg) (end ,end))
1195 (put-text-property beg end 'c-in-sws t)
1196 ,@(when (facep 'c-debug-is-sws-face)
1197 `((c-debug-add-face beg end 'c-debug-in-sws-face)))))
1199 (defmacro c-remove-is-sws (beg end)
1200 `(let ((beg ,beg) (end ,end))
1201 (remove-text-properties beg end '(c-is-sws nil))
1202 ,@(when (facep 'c-debug-is-sws-face)
1203 `((c-debug-remove-face beg end 'c-debug-is-sws-face)))))
1205 (defmacro c-remove-in-sws (beg end)
1206 `(let ((beg ,beg) (end ,end))
1207 (remove-text-properties beg end '(c-in-sws nil))
1208 ,@(when (facep 'c-debug-is-sws-face)
1209 `((c-debug-remove-face beg end 'c-debug-in-sws-face)))))
1211 (defmacro c-remove-is-and-in-sws (beg end)
1212 `(let ((beg ,beg) (end ,end))
1213 (remove-text-properties beg end '(c-is-sws nil c-in-sws nil))
1214 ,@(when (facep 'c-debug-is-sws-face)
1215 `((c-debug-remove-face beg end 'c-debug-is-sws-face)
1216 (c-debug-remove-face beg end 'c-debug-in-sws-face)))))
1218 (defsubst c-invalidate-sws-region-after (beg end)
1219 ;; Called from `after-change-functions'. Note that if
1220 ;; `c-forward-sws' or `c-backward-sws' are used outside
1221 ;; `c-save-buffer-state' or similar then this will remove the cache
1222 ;; properties right after they're added.
1225 ;; Adjust the end to remove the properties in any following simple
1226 ;; ws up to and including the next line break, if there is any
1227 ;; after the changed region. This is necessary e.g. when a rung
1228 ;; marked empty line is converted to a line comment by inserting
1229 ;; "//" before the line break. In that case the line break would
1230 ;; keep the rung mark which could make a later `c-backward-sws'
1231 ;; move into the line comment instead of over it.
1233 (skip-chars-forward " \t\f\v")
1234 (when (and (eolp) (not (eobp)))
1235 (setq end (1+ (point)))))
1237 (when (and (= beg end)
1238 (get-text-property beg 'c-in-sws)
1240 (get-text-property (1- beg) 'c-in-sws))
1241 ;; Ensure that an `c-in-sws' range gets broken. Note that it isn't
1242 ;; safe to keep a range that was continuous before the change. E.g:
1248 ;; There can be a "ladder" between "#" and "b". Now, if the newline
1249 ;; after "foo" is removed then "bar" will become part of the cpp
1250 ;; directive instead of a syntactically relevant token. In that
1251 ;; case there's no longer syntactic ws from "#" to "b".
1252 (setq beg (1- beg)))
1254 (c-debug-sws-msg "c-invalidate-sws-region-after [%s..%s]" beg end)
1255 (c-remove-is-and-in-sws beg end))
1257 (defun c-forward-sws ()
1258 ;; Used by `c-forward-syntactic-ws' to implement the unbounded search.
1260 (let (;; `rung-pos' is set to a position as early as possible in the
1261 ;; unmarked part of the simple ws region.
1262 (rung-pos (point)) next-rung-pos rung-end-pos last-put-in-sws-pos
1263 rung-is-marked next-rung-is-marked simple-ws-end
1264 ;; `safe-start' is set when it's safe to cache the start position.
1265 ;; It's not set if we've initially skipped over comments and line
1266 ;; continuations since we might have gone out through the end of a
1267 ;; macro then. This provision makes `c-forward-sws' not populate the
1268 ;; cache in the majority of cases, but otoh is `c-backward-sws' by far
1272 ;; Skip simple ws and do a quick check on the following character to see
1273 ;; if it's anything that can't start syntactic ws, so we can bail out
1274 ;; early in the majority of cases when there just are a few ws chars.
1275 (skip-chars-forward " \t\n\r\f\v")
1276 (when (looking-at c-syntactic-ws-start)
1278 (setq rung-end-pos (min (1+ (point)) (point-max)))
1279 (if (setq rung-is-marked (text-property-any rung-pos rung-end-pos
1281 ;; Find the last rung position to avoid setting properties in all
1282 ;; the cases when the marked rung is complete.
1283 ;; (`next-single-property-change' is certain to move at least one
1285 (setq rung-pos (1- (next-single-property-change
1286 rung-is-marked 'c-is-sws nil rung-end-pos)))
1287 ;; Got no marked rung here. Since the simple ws might have started
1288 ;; inside a line comment or cpp directive we must set `rung-pos' as
1289 ;; high as possible.
1290 (setq rung-pos (point)))
1295 (when (and rung-is-marked
1296 (get-text-property (point) 'c-in-sws))
1298 ;; The following search is the main reason that `c-in-sws'
1299 ;; and `c-is-sws' aren't combined to one property.
1300 (goto-char (next-single-property-change
1301 (point) 'c-in-sws nil (point-max)))
1302 (unless (get-text-property (point) 'c-is-sws)
1303 ;; If the `c-in-sws' region extended past the last
1304 ;; `c-is-sws' char we have to go back a bit.
1305 (or (get-text-property (1- (point)) 'c-is-sws)
1306 (goto-char (previous-single-property-change
1307 (point) 'c-is-sws)))
1311 "c-forward-sws cached move %s -> %s (max %s)"
1312 rung-pos (point) (point-max))
1314 (setq rung-pos (point))
1315 (and (> (skip-chars-forward " \t\n\r\f\v") 0)
1318 ;; We'll loop here if there is simple ws after the last rung.
1319 ;; That means that there's been some change in it and it's
1320 ;; possible that we've stepped into another ladder, so extend
1321 ;; the previous one to join with it if there is one, and try to
1322 ;; use the cache again.
1324 "c-forward-sws extending rung with [%s..%s] (max %s)"
1325 (1+ rung-pos) (1+ (point)) (point-max))
1326 (unless (get-text-property (point) 'c-is-sws)
1327 ;; Remove any `c-in-sws' property from the last char of
1328 ;; the rung before we mark it with `c-is-sws', so that we
1329 ;; won't connect with the remains of a broken "ladder".
1330 (c-remove-in-sws (point) (1+ (point))))
1331 (c-put-is-sws (1+ rung-pos)
1333 (c-put-in-sws rung-pos
1334 (setq rung-pos (point)
1335 last-put-in-sws-pos rung-pos)))
1337 (setq simple-ws-end (point))
1338 (c-forward-comments)
1341 ((/= (point) simple-ws-end)
1342 ;; Skipped over comments. Don't cache at eob in case the buffer
1347 (and c-opt-cpp-prefix
1348 (looking-at c-opt-cpp-start)
1349 (progn (skip-chars-backward " \t")
1352 (progn (backward-char)
1353 (not (eq (char-before) ?\\))))))
1354 ;; Skip a preprocessor directive.
1356 (while (and (eq (char-before) ?\\)
1357 (= (forward-line 1) 0))
1361 ;; Don't cache at eob in case the buffer is narrowed.
1364 ;; We've searched over a piece of non-white syntactic ws. See if this
1366 (setq next-rung-pos (point))
1367 (skip-chars-forward " \t\n\r\f\v")
1368 (setq rung-end-pos (min (1+ (point)) (point-max)))
1371 ;; Cache if we haven't skipped comments only, and if we started
1372 ;; either from a marked rung or from a completely uncached
1376 (not (get-text-property simple-ws-end 'c-in-sws))))
1378 ;; See if there's a marked rung in the encountered simple ws. If
1379 ;; so then we can cache, unless `safe-start' is nil. Even then
1380 ;; we need to do this to check if the cache can be used for the
1382 (and (setq next-rung-is-marked
1383 (text-property-any next-rung-pos rung-end-pos
1389 "c-forward-sws caching [%s..%s] - [%s..%s] (max %s)"
1390 rung-pos (1+ simple-ws-end) next-rung-pos rung-end-pos
1393 ;; Remove the properties for any nested ws that might be cached.
1394 ;; Only necessary for `c-is-sws' since `c-in-sws' will be set
1396 (c-remove-is-sws (1+ simple-ws-end) next-rung-pos)
1397 (unless (and rung-is-marked (= rung-pos simple-ws-end))
1398 (c-put-is-sws rung-pos
1400 (setq rung-is-marked t))
1401 (c-put-in-sws rung-pos
1402 (setq rung-pos (point)
1403 last-put-in-sws-pos rung-pos))
1404 (unless (get-text-property (1- rung-end-pos) 'c-is-sws)
1405 ;; Remove any `c-in-sws' property from the last char of
1406 ;; the rung before we mark it with `c-is-sws', so that we
1407 ;; won't connect with the remains of a broken "ladder".
1408 (c-remove-in-sws (1- rung-end-pos) rung-end-pos))
1409 (c-put-is-sws next-rung-pos
1413 "c-forward-sws not caching [%s..%s] - [%s..%s] (max %s)"
1414 rung-pos (1+ simple-ws-end) next-rung-pos rung-end-pos
1417 ;; Set `rung-pos' for the next rung. It's the same thing here as
1418 ;; initially, except that the rung position is set as early as
1419 ;; possible since we can't be in the ending ws of a line comment or
1420 ;; cpp directive now.
1421 (if (setq rung-is-marked next-rung-is-marked)
1422 (setq rung-pos (1- (next-single-property-change
1423 rung-is-marked 'c-is-sws nil rung-end-pos)))
1424 (setq rung-pos next-rung-pos))
1425 (setq safe-start t)))
1427 ;; Make sure that the newly marked `c-in-sws' region doesn't connect to
1428 ;; another one after the point (which might occur when editing inside a
1429 ;; comment or macro).
1430 (when (eq last-put-in-sws-pos (point))
1431 (cond ((< last-put-in-sws-pos (point-max))
1433 "c-forward-sws clearing at %s for cache separation"
1434 last-put-in-sws-pos)
1435 (c-remove-in-sws last-put-in-sws-pos
1436 (1+ last-put-in-sws-pos)))
1438 ;; If at eob we have to clear the last character before the end
1439 ;; instead since the buffer might be narrowed and there might
1440 ;; be a `c-in-sws' after (point-max). In this case it's
1441 ;; necessary to clear both properties.
1443 "c-forward-sws clearing thoroughly at %s for cache separation"
1444 (1- last-put-in-sws-pos))
1445 (c-remove-is-and-in-sws (1- last-put-in-sws-pos)
1446 last-put-in-sws-pos))))
1449 (defun c-backward-sws ()
1450 ;; Used by `c-backward-syntactic-ws' to implement the unbounded search.
1452 (let (;; `rung-pos' is set to a position as late as possible in the unmarked
1453 ;; part of the simple ws region.
1454 (rung-pos (point)) next-rung-pos last-put-in-sws-pos
1455 rung-is-marked simple-ws-beg cmt-skip-pos)
1457 ;; Skip simple horizontal ws and do a quick check on the preceding
1458 ;; character to see if it's anying that can't end syntactic ws, so we can
1459 ;; bail out early in the majority of cases when there just are a few ws
1460 ;; chars. Newlines are complicated in the backward direction, so we can't
1462 (skip-chars-backward " \t\f")
1463 (when (and (not (bobp))
1466 (looking-at c-syntactic-ws-end)))
1468 ;; Try to find a rung position in the simple ws preceding point, so that
1469 ;; we can get a cache hit even if the last bit of the simple ws has
1470 ;; changed recently.
1471 (setq simple-ws-beg (point))
1472 (skip-chars-backward " \t\n\r\f\v")
1473 (if (setq rung-is-marked (text-property-any
1474 (point) (min (1+ rung-pos) (point-max))
1476 ;; `rung-pos' will be the earliest marked position, which means that
1477 ;; there might be later unmarked parts in the simple ws region.
1478 ;; It's not worth the effort to fix that; the last part of the
1479 ;; simple ws is also typically edited often, so it could be wasted.
1480 (goto-char (setq rung-pos rung-is-marked))
1481 (goto-char simple-ws-beg))
1486 (when (and rung-is-marked
1488 (get-text-property (1- (point)) 'c-in-sws))
1490 ;; The following search is the main reason that `c-in-sws'
1491 ;; and `c-is-sws' aren't combined to one property.
1492 (goto-char (previous-single-property-change
1493 (point) 'c-in-sws nil (point-min)))
1494 (unless (get-text-property (point) 'c-is-sws)
1495 ;; If the `c-in-sws' region extended past the first
1496 ;; `c-is-sws' char we have to go forward a bit.
1497 (goto-char (next-single-property-change
1498 (point) 'c-is-sws)))
1501 "c-backward-sws cached move %s <- %s (min %s)"
1502 (point) rung-pos (point-min))
1504 (setq rung-pos (point))
1505 (if (and (< (min (skip-chars-backward " \t\f\v")
1507 (setq simple-ws-beg (point))
1508 (skip-chars-backward " \t\n\r\f\v")))
1510 (setq rung-is-marked
1511 (text-property-any (point) rung-pos
1514 (goto-char simple-ws-beg)
1517 ;; We'll loop here if there is simple ws before the first rung.
1518 ;; That means that there's been some change in it and it's
1519 ;; possible that we've stepped into another ladder, so extend
1520 ;; the previous one to join with it if there is one, and try to
1521 ;; use the cache again.
1523 "c-backward-sws extending rung with [%s..%s] (min %s)"
1524 rung-is-marked rung-pos (point-min))
1525 (unless (get-text-property (1- rung-pos) 'c-is-sws)
1526 ;; Remove any `c-in-sws' property from the last char of
1527 ;; the rung before we mark it with `c-is-sws', so that we
1528 ;; won't connect with the remains of a broken "ladder".
1529 (c-remove-in-sws (1- rung-pos) rung-pos))
1530 (c-put-is-sws rung-is-marked
1532 (c-put-in-sws rung-is-marked
1534 (setq rung-pos rung-is-marked
1535 last-put-in-sws-pos rung-pos))
1537 (c-backward-comments)
1538 (setq cmt-skip-pos (point))
1541 ((and c-opt-cpp-prefix
1542 (/= cmt-skip-pos simple-ws-beg)
1543 (c-beginning-of-macro))
1544 ;; Inside a cpp directive. See if it should be skipped over.
1545 (let ((cpp-beg (point)))
1547 ;; Move back over all line continuations in the region skipped
1548 ;; over by `c-backward-comments'. If we go past it then we
1549 ;; started inside the cpp directive.
1550 (goto-char simple-ws-beg)
1552 (while (and (> (point) cmt-skip-pos)
1553 (progn (backward-char)
1554 (eq (char-before) ?\\)))
1555 (beginning-of-line))
1557 (if (< (point) cmt-skip-pos)
1558 ;; Don't move past the cpp directive if we began inside
1559 ;; it. Note that the position at the end of the last line
1560 ;; of the macro is also considered to be within it.
1561 (progn (goto-char cmt-skip-pos)
1564 ;; It's worthwhile to spend a little bit of effort on finding
1565 ;; the end of the macro, to get a good `simple-ws-beg'
1566 ;; position for the cache. Note that `c-backward-comments'
1567 ;; could have stepped over some comments before going into
1568 ;; the macro, and then `simple-ws-beg' must be kept on the
1569 ;; same side of those comments.
1570 (goto-char simple-ws-beg)
1571 (skip-chars-backward " \t\n\r\f\v")
1572 (if (eq (char-before) ?\\)
1575 (if (< (point) simple-ws-beg)
1576 ;; Might happen if comments after the macro were skipped
1578 (setq simple-ws-beg (point)))
1583 ((/= (save-excursion
1584 (skip-chars-forward " \t\n\r\f\v" simple-ws-beg)
1585 (setq next-rung-pos (point)))
1587 ;; Skipped over comments. Must put point at the end of
1588 ;; the simple ws at point since we might be after a line
1589 ;; comment or cpp directive that's been partially
1590 ;; narrowed out, and we can't risk marking the simple ws
1591 ;; at the end of it.
1592 (goto-char next-rung-pos)
1595 ;; We've searched over a piece of non-white syntactic ws. See if this
1597 (setq next-rung-pos (point))
1598 (skip-chars-backward " \t\f\v")
1601 ;; Cache if we started either from a marked rung or from a
1602 ;; completely uncached position.
1604 (not (get-text-property (1- simple-ws-beg) 'c-in-sws))
1606 ;; Cache if there's a marked rung in the encountered simple ws.
1608 (skip-chars-backward " \t\n\r\f\v")
1609 (text-property-any (point) (min (1+ next-rung-pos) (point-max))
1614 "c-backward-sws caching [%s..%s] - [%s..%s] (min %s)"
1615 (point) (1+ next-rung-pos)
1616 simple-ws-beg (min (1+ rung-pos) (point-max))
1619 ;; Remove the properties for any nested ws that might be cached.
1620 ;; Only necessary for `c-is-sws' since `c-in-sws' will be set
1622 (c-remove-is-sws (1+ next-rung-pos) simple-ws-beg)
1623 (unless (and rung-is-marked (= simple-ws-beg rung-pos))
1624 (let ((rung-end-pos (min (1+ rung-pos) (point-max))))
1625 (unless (get-text-property (1- rung-end-pos) 'c-is-sws)
1626 ;; Remove any `c-in-sws' property from the last char of
1627 ;; the rung before we mark it with `c-is-sws', so that we
1628 ;; won't connect with the remains of a broken "ladder".
1629 (c-remove-in-sws (1- rung-end-pos) rung-end-pos))
1630 (c-put-is-sws simple-ws-beg
1632 (setq rung-is-marked t)))
1633 (c-put-in-sws (setq simple-ws-beg (point)
1634 last-put-in-sws-pos simple-ws-beg)
1636 (c-put-is-sws (setq rung-pos simple-ws-beg)
1637 (1+ next-rung-pos)))
1640 "c-backward-sws not caching [%s..%s] - [%s..%s] (min %s)"
1641 (point) (1+ next-rung-pos)
1642 simple-ws-beg (min (1+ rung-pos) (point-max))
1644 (setq rung-pos next-rung-pos
1645 simple-ws-beg (point))
1648 ;; Make sure that the newly marked `c-in-sws' region doesn't connect to
1649 ;; another one before the point (which might occur when editing inside a
1650 ;; comment or macro).
1651 (when (eq last-put-in-sws-pos (point))
1652 (cond ((< (point-min) last-put-in-sws-pos)
1654 "c-backward-sws clearing at %s for cache separation"
1655 (1- last-put-in-sws-pos))
1656 (c-remove-in-sws (1- last-put-in-sws-pos)
1657 last-put-in-sws-pos))
1659 ;; If at bob and the buffer is narrowed, we have to clear the
1660 ;; character we're standing on instead since there might be a
1661 ;; `c-in-sws' before (point-min). In this case it's necessary
1662 ;; to clear both properties.
1664 "c-backward-sws clearing thoroughly at %s for cache separation"
1665 last-put-in-sws-pos)
1666 (c-remove-is-and-in-sws last-put-in-sws-pos
1667 (1+ last-put-in-sws-pos)))))
1671 ;; A system for handling noteworthy parens before the point.
1673 (defvar c-state-cache nil)
1674 (make-variable-buffer-local 'c-state-cache)
1675 ;; The state cache used by `c-parse-state' to cut down the amount of
1676 ;; searching. It's the result from some earlier `c-parse-state' call.
1677 ;; The use of the cached info is more effective if the next
1678 ;; `c-parse-state' call is on a line close by the one the cached state
1679 ;; was made at; the cache can actually slow down a little if the
1680 ;; cached state was made very far back in the buffer. The cache is
1681 ;; most effective if `c-parse-state' is used on each line while moving
1684 (defvar c-state-cache-start 1)
1685 (make-variable-buffer-local 'c-state-cache-start)
1686 ;; This is (point-min) when `c-state-cache' was calculated, since a
1687 ;; change of narrowing is likely to affect the parens that are visible
1688 ;; before the point.
1690 (defsubst c-invalidate-state-cache (pos)
1691 ;; Invalidate all info on `c-state-cache' that applies to the buffer
1692 ;; at POS or higher. This is much like `c-whack-state-after', but
1693 ;; it never changes a paren pair element into an open paren element.
1694 ;; Doing that would mean that the new open paren wouldn't have the
1695 ;; required preceding paren pair element.
1697 ;; This function does not do any hidden buffer changes.
1698 (while (and c-state-cache
1699 (let ((elem (car c-state-cache)))
1701 (or (<= pos (car elem))
1704 (setq c-state-cache (cdr c-state-cache))))
1706 (defun c-parse-state ()
1707 ;; Finds and records all noteworthy parens between some good point
1708 ;; earlier in the file and point. That good point is at least the
1709 ;; beginning of the top-level construct we are in, or the beginning
1710 ;; of the preceding top-level construct if we aren't in one.
1712 ;; The returned value is a list of the noteworthy parens with the
1713 ;; last one first. If an element in the list is an integer, it's
1714 ;; the position of an open paren which has not been closed before
1715 ;; the point. If an element is a cons, it gives the position of a
1716 ;; closed brace paren pair; the car is the start paren position and
1717 ;; the cdr is the position following the closing paren. Only the
1718 ;; last closed brace paren pair before each open paren is recorded,
1719 ;; and thus the state never contains two cons elements in
1722 ;; Currently no characters which are given paren syntax with the
1723 ;; syntax-table property are recorded, i.e. angle bracket arglist
1724 ;; parens are never present here. Note that this might change.
1726 ;; This function does not do any hidden buffer changes.
1729 (let* ((here (point))
1730 (c-macro-start (c-query-macro-start))
1731 (in-macro-start (or c-macro-start (point)))
1732 old-state last-pos pairs pos save-pos)
1733 (c-invalidate-state-cache (point))
1735 ;; If the minimum position has changed due to narrowing then we
1736 ;; have to fix the tail of `c-state-cache' accordingly.
1737 (unless (= c-state-cache-start (point-min))
1738 (if (> (point-min) c-state-cache-start)
1739 ;; If point-min has moved forward then we just need to cut
1740 ;; off a bit of the tail.
1741 (let ((ptr (cons nil c-state-cache)) elem)
1742 (while (and (setq elem (cdr ptr))
1743 (>= (if (consp elem) (car elem) elem)
1747 (if (eq (cdr ptr) c-state-cache)
1748 (setq c-state-cache nil)
1750 ;; If point-min has moved backward then we drop the state
1751 ;; completely. It's possible to do a better job here and
1752 ;; recalculate the top only.
1753 (setq c-state-cache nil))
1754 (setq c-state-cache-start (point-min)))
1756 ;; Get the latest position we know are directly inside the
1757 ;; closest containing paren of the cached state.
1758 (setq last-pos (and c-state-cache
1759 (if (consp (car c-state-cache))
1760 (cdr (car c-state-cache))
1761 (1+ (car c-state-cache)))))
1763 ;; Check if the found last-pos is in a macro. If it is, and
1764 ;; we're not in the same macro, we must discard everything on
1765 ;; c-state-cache that is inside the macro before using it.
1768 (goto-char last-pos)
1769 (when (and (c-beginning-of-macro)
1770 (/= (point) in-macro-start))
1771 (c-invalidate-state-cache (point))
1772 ;; Set last-pos again, just like above.
1773 (setq last-pos (and c-state-cache
1774 (if (consp (car c-state-cache))
1775 (cdr (car c-state-cache))
1776 (1+ (car c-state-cache))))))))
1779 ;; Find the start position for the forward search. (Can't
1780 ;; search in the backward direction since point might be
1781 ;; in some kind of literal.)
1784 ;; There's a cached state with a containing paren. Pop
1785 ;; off the stale containing sexps from it by going
1786 ;; forward out of parens as far as possible.
1787 (narrow-to-region (point-min) here)
1788 (let (placeholder pair-beg)
1789 (while (and c-state-cache
1791 (c-up-list-forward last-pos)))
1792 (setq last-pos placeholder)
1793 (if (consp (car c-state-cache))
1794 (setq pair-beg (car-safe (cdr c-state-cache))
1795 c-state-cache (cdr-safe (cdr c-state-cache)))
1796 (setq pair-beg (car c-state-cache)
1797 c-state-cache (cdr c-state-cache))))
1799 (when (and pair-beg (eq (char-after pair-beg) ?{))
1800 ;; The last paren pair we moved out from was a brace
1801 ;; pair. Modify the state to record this as a closed
1803 (if (consp (car-safe c-state-cache))
1804 (setq c-state-cache (cdr c-state-cache)))
1805 (setq c-state-cache (cons (cons pair-beg last-pos)
1808 ;; Check if the preceding balanced paren is within a
1809 ;; macro; it should be ignored if we're outside the
1810 ;; macro. There's no need to check any further upwards;
1811 ;; if the macro contains an unbalanced opening paren then
1812 ;; we're smoked anyway.
1813 (when (and (<= (point) in-macro-start)
1814 (consp (car c-state-cache)))
1816 (goto-char (car (car c-state-cache)))
1817 (when (c-beginning-of-macro)
1819 c-state-cache (cdr c-state-cache)))))
1822 (setq old-state c-state-cache)
1826 ;; go back 2 bods, but ignore any bogus positions
1827 ;; returned by beginning-of-defun (i.e. open paren in
1831 (while (not (or (bobp) (zerop cnt)))
1832 (c-beginning-of-defun-1)
1833 (if (eq (char-after) ?\{)
1834 (setq cnt (1- cnt)))))
1837 (narrow-to-region (point-min) here)
1840 ;; Find the balanced brace pairs.
1843 (while (and (setq last-pos (c-down-list-forward pos))
1844 (setq pos (c-up-list-forward last-pos)))
1845 (if (eq (char-before last-pos) ?{)
1846 (setq pairs (cons (cons last-pos pos) pairs))))
1848 ;; Should ignore any pairs that are in a macro, providing
1849 ;; we're not in the same one.
1850 (when (and pairs (< (car (car pairs)) in-macro-start))
1851 (while (and (save-excursion
1852 (goto-char (car (car pairs)))
1853 (c-beginning-of-macro))
1854 (setq pairs (cdr pairs)))))
1856 ;; Record the last brace pair.
1858 (if (and (eq c-state-cache old-state)
1859 (consp (car-safe c-state-cache)))
1860 ;; There's a closed pair on the cached state but we've
1861 ;; found a later one, so remove it.
1862 (setq c-state-cache (cdr c-state-cache)))
1863 (setq pairs (car pairs))
1864 (setcar pairs (1- (car pairs)))
1865 (when (consp (car-safe c-state-cache))
1866 ;; There could already be a cons first in `c-state-cache'
1867 ;; if we've e.g. jumped over an unbalanced open paren in a
1869 (setq c-state-cache (cdr c-state-cache)))
1870 (setq c-state-cache (cons pairs c-state-cache)))
1873 ;; Prepare to loop, but record the open paren only if it's
1874 ;; outside a macro or within the same macro as point, and
1875 ;; if it is a "real" open paren and not some character
1876 ;; that got an open paren syntax-table property.
1879 (if (and (or (>= last-pos in-macro-start)
1881 (goto-char last-pos)
1882 (not (c-beginning-of-macro))))
1883 (= (char-syntax (char-before last-pos)) ?\())
1884 (setq c-state-cache (cons (1- last-pos) c-state-cache))))
1886 (if (setq last-pos (c-up-list-forward pos))
1887 ;; Found a close paren without a corresponding opening
1888 ;; one. Maybe we didn't go back far enough, so try to
1889 ;; scan backward for the start paren and then start over.
1891 (setq pos (c-up-list-backward pos)
1894 ;; Emacs (up to at least 21.2) can get confused by
1895 ;; open parens in column zero inside comments: The
1896 ;; sexp functions can then misbehave and bring us
1897 ;; back to the same point again. Check this so that
1898 ;; we don't get an infinite loop.
1902 (format "Unbalanced close paren at line %d"
1903 (1+ (count-lines (point-min)
1904 (c-point 'bol last-pos)))))))
1909 ;; Debug tool to catch cache inconsistencies.
1910 (defvar c-debug-parse-state nil)
1911 (unless (fboundp 'c-real-parse-state)
1912 (fset 'c-real-parse-state (symbol-function 'c-parse-state)))
1913 (cc-bytecomp-defun c-real-parse-state)
1914 (defun c-debug-parse-state ()
1915 (let ((res1 (c-real-parse-state)) res2)
1916 (let ((c-state-cache nil))
1917 (setq res2 (c-real-parse-state)))
1918 (unless (equal res1 res2)
1919 (error "c-parse-state inconsistency: using cache: %s, from scratch: %s"
1922 (defun c-toggle-parse-state-debug (&optional arg)
1924 (setq c-debug-parse-state (c-calculate-state arg c-debug-parse-state))
1925 (fset 'c-parse-state (symbol-function (if c-debug-parse-state
1926 'c-debug-parse-state
1927 'c-real-parse-state)))
1928 (c-keep-region-active))
1930 (defun c-whack-state-before (bufpos paren-state)
1931 ;; Whack off any state information from PAREN-STATE which lies
1932 ;; before BUFPOS. Not destructive on PAREN-STATE.
1934 ;; This function does not do any hidden buffer changes.
1935 (let* ((newstate (list nil))
1939 (setq car (car paren-state)
1940 paren-state (cdr paren-state))
1941 (if (< (if (consp car) (car car) car) bufpos)
1942 (setq paren-state nil)
1943 (setcdr ptr (list car))
1944 (setq ptr (cdr ptr))))
1947 (defun c-whack-state-after (bufpos paren-state)
1948 ;; Whack off any state information from PAREN-STATE which lies at or
1949 ;; after BUFPOS. Not destructive on PAREN-STATE.
1951 ;; This function does not do any hidden buffer changes.
1954 (let ((car (car paren-state)))
1956 ;; just check the car, because in a balanced brace
1957 ;; expression, it must be impossible for the corresponding
1958 ;; close brace to be before point, but the open brace to
1960 (if (<= bufpos (car car))
1962 (if (< bufpos (cdr car))
1963 ;; its possible that the open brace is before
1964 ;; bufpos, but the close brace is after. In that
1965 ;; case, convert this to a non-cons element. The
1966 ;; rest of the state is before bufpos, so we're
1968 (throw 'done (cons (car car) (cdr paren-state)))
1969 ;; we know that both the open and close braces are
1970 ;; before bufpos, so we also know that everything else
1971 ;; on state is before bufpos.
1972 (throw 'done paren-state)))
1975 ;; it's before bufpos, so everything else should too.
1976 (throw 'done paren-state)))
1977 (setq paren-state (cdr paren-state)))
1980 (defun c-most-enclosing-brace (paren-state &optional bufpos)
1981 ;; Return the bufpos of the innermost enclosing open paren before
1982 ;; bufpos that hasn't been narrowed out, or nil if none was found.
1984 ;; This function does not do any hidden buffer changes.
1986 (or bufpos (setq bufpos 134217727))
1988 (setq enclosingp (car paren-state)
1989 paren-state (cdr paren-state))
1990 (if (or (consp enclosingp)
1991 (>= enclosingp bufpos))
1992 (setq enclosingp nil)
1993 (if (< enclosingp (point-min))
1994 (setq enclosingp nil))
1995 (setq paren-state nil)))
1998 (defun c-least-enclosing-brace (paren-state &optional bufpos)
1999 ;; Return the bufpos of the outermost enclosing open paren before
2000 ;; bufpos that hasn't been narrowed out, or nil if none was found.
2002 ;; This function does not do any hidden buffer changes.
2004 (or bufpos (setq bufpos 134217727))
2006 (setq elem (car paren-state)
2007 paren-state (cdr paren-state))
2008 (unless (or (consp elem)
2010 (if (>= elem (point-min))
2014 (defun c-safe-position (bufpos paren-state)
2015 ;; Return the closest known safe position higher up than BUFPOS, or
2016 ;; nil if PAREN-STATE doesn't contain one. Return nil if BUFPOS is
2017 ;; nil, which is useful to find the closest limit before a given
2018 ;; limit that might be nil.
2020 ;; This function does not do any hidden buffer changes.
2025 (setq elem (car paren-state))
2027 (cond ((< (cdr elem) bufpos)
2028 (throw 'done (cdr elem)))
2029 ((< (car elem) bufpos)
2031 (throw 'done (min (1+ (car elem)) bufpos))))
2033 ;; elem is the position at and not after the opening paren, so
2034 ;; we can go forward one more step unless it's equal to
2035 ;; bufpos. This is useful in some cases avoid an extra paren
2036 ;; level between the safe position and bufpos.
2037 (throw 'done (min (1+ elem) bufpos))))
2038 (setq paren-state (cdr paren-state)))))))
2040 (defun c-beginning-of-syntax ()
2041 ;; This is used for `font-lock-beginning-of-syntax-function'. It
2042 ;; goes to the closest previous point that is known to be outside
2043 ;; any string literal or comment. `c-state-cache' is used if it has
2044 ;; a position in the vicinity.
2045 (let* ((paren-state c-state-cache)
2049 ;; Note: Similar code in `c-safe-position'. The
2050 ;; difference is that we accept a safe position at
2051 ;; the point and don't bother to go forward past open
2054 (setq elem (car paren-state))
2056 (cond ((<= (cdr elem) (point))
2057 (throw 'done (cdr elem)))
2058 ((<= (car elem) (point))
2059 (throw 'done (car elem))))
2060 (if (<= elem (point))
2061 (throw 'done elem)))
2062 (setq paren-state (cdr paren-state)))
2065 (if (> pos (- (point) 4000))
2067 ;; The position is far back. Try `c-beginning-of-defun-1'
2068 ;; (although we can't be entirely sure it will go to a position
2069 ;; outside a comment or string in current emacsen). FIXME:
2070 ;; Consult `syntax-ppss' here.
2071 (c-beginning-of-defun-1)
2076 ;; Tools for scanning identifiers and other tokens.
2078 (defun c-on-identifier ()
2079 "Return non-nil if the point is on or directly after an identifier.
2080 Keywords are recognized and not considered identifiers. If an
2081 identifier is detected, the returned value is its starting position.
2082 If an identifier both starts and stops at the point \(can only happen
2083 in Pike) then the point for the preceding one is returned.
2085 This function does not do any hidden buffer changes."
2088 (if (zerop (skip-syntax-backward "w_"))
2090 (when (c-major-mode-is 'pike-mode)
2091 ;; Handle the `<operator> syntax in Pike.
2092 (let ((pos (point)))
2093 (skip-chars-backward "!%&*+\\-/<=>^|~[]()")
2094 (and (if (< (skip-chars-backward "`") 0)
2097 (eq (char-after) ?\`))
2098 (looking-at c-symbol-key)
2099 (>= (match-end 0) pos)
2102 (and (not (looking-at c-keywords-regexp))
2105 (defsubst c-simple-skip-symbol-backward ()
2106 ;; If the point is at the end of a symbol then skip backward to the
2107 ;; beginning of it. Don't move otherwise. Return non-nil if point
2109 (or (< (skip-syntax-backward "w_") 0)
2110 (and (c-major-mode-is 'pike-mode)
2111 ;; Handle the `<operator> syntax in Pike.
2112 (let ((pos (point)))
2113 (if (and (< (skip-chars-backward "!%&*+\\-/<=>^|~[]()") 0)
2114 (< (skip-chars-backward "`") 0)
2115 (looking-at c-symbol-key)
2116 (>= (match-end 0) pos))
2121 (defsubst c-beginning-of-current-token (&optional back-limit)
2122 ;; Move to the beginning of the current token. Do not move if not
2123 ;; in the middle of one. BACK-LIMIT may be used to bound the
2124 ;; backward search; if given it's assumed to be at the boundary
2125 ;; between two tokens.
2126 (if (looking-at "\\w\\|\\s_")
2127 (skip-syntax-backward "w_" back-limit)
2128 (let ((start (point)))
2129 (when (< (skip-syntax-backward ".()" back-limit) 0)
2130 (while (let ((pos (or (and (looking-at c-nonsymbol-token-regexp)
2132 ;; `c-nonsymbol-token-regexp' should always match
2133 ;; since we've skipped backward over punctuator
2134 ;; or paren syntax, but consume one char in case
2135 ;; it doesn't so that we don't leave point before
2136 ;; some earlier incorrect token.
2142 (defsubst c-end-of-current-token (&optional back-limit)
2143 ;; Move to the end of the current token. Do not move if not in the
2144 ;; middle of one. BACK-LIMIT may be used to bound the backward
2145 ;; search; if given it's assumed to be at the boundary between two
2147 (let ((start (point)))
2148 (cond ((< (skip-syntax-backward "w_" (1- start)) 0)
2149 (skip-syntax-forward "w_"))
2150 ((< (skip-syntax-backward ".()" back-limit) 0)
2152 (if (looking-at c-nonsymbol-token-regexp)
2153 (goto-char (match-end 0))
2154 ;; `c-nonsymbol-token-regexp' should always match since
2155 ;; we've skipped backward over punctuator or paren
2156 ;; syntax, but move forward in case it doesn't so that
2157 ;; we don't leave point earlier than we started with.
2159 (< (point) start)))))))
2161 (defconst c-jump-syntax-balanced
2162 (if (memq 'gen-string-delim c-emacs-features)
2163 "\\w\\|\\s_\\|\\s\(\\|\\s\)\\|\\s\"\\|\\s|"
2164 "\\w\\|\\s_\\|\\s\(\\|\\s\)\\|\\s\""))
2166 (defconst c-jump-syntax-unbalanced
2167 (if (memq 'gen-string-delim c-emacs-features)
2168 "\\w\\|\\s_\\|\\s\"\\|\\s|"
2169 "\\w\\|\\s_\\|\\s\""))
2171 (defun c-forward-token-2 (&optional count balanced limit)
2172 "Move forward by tokens.
2173 A token is defined as all symbols and identifiers which aren't
2174 syntactic whitespace \(note that multicharacter tokens like \"==\" are
2175 treated properly). Point is always either left at the beginning of a
2176 token or not moved at all. COUNT specifies the number of tokens to
2177 move; a negative COUNT moves in the opposite direction. A COUNT of 0
2178 moves to the next token beginning only if not already at one. If
2179 BALANCED is true, move over balanced parens, otherwise move into them.
2180 Also, if BALANCED is true, never move out of an enclosing paren.
2182 LIMIT sets the limit for the movement and defaults to the point limit.
2183 The case when LIMIT is set in the middle of a token, comment or macro
2184 is handled correctly, i.e. the point won't be left there.
2186 Return the number of tokens left to move \(positive or negative). If
2187 BALANCED is true, a move over a balanced paren counts as one. Note
2188 that if COUNT is 0 and no appropriate token beginning is found, 1 will
2189 be returned. Thus, a return value of 0 guarantees that point is at
2190 the requested position and a return value less \(without signs) than
2191 COUNT guarantees that point is at the beginning of some token."
2193 (or count (setq count 1))
2195 (- (c-backward-token-2 (- count) balanced limit))
2197 (let ((jump-syntax (if balanced
2198 c-jump-syntax-balanced
2199 c-jump-syntax-unbalanced))
2204 ;; If count is zero we should jump if in the middle of a token.
2205 (c-end-of-current-token))
2208 (if limit (narrow-to-region (point-min) limit))
2210 (progn (c-forward-syntactic-ws) (point)))
2211 ;; Skip whitespace. Count this as a move if we did in
2213 (setq count (max (1- count) 0)))
2216 ;; Moved out of bounds. Make sure the returned count isn't zero.
2218 (if (zerop count) (setq count 1))
2221 ;; Use `condition-case' to avoid having the limit tests
2228 (cond ((looking-at jump-syntax)
2229 (goto-char (scan-sexps (point) 1))
2231 ((looking-at c-nonsymbol-token-regexp)
2232 (goto-char (match-end 0))
2234 ;; `c-nonsymbol-token-regexp' above should always
2235 ;; match if there are correct tokens. Try to
2236 ;; widen to see if the limit was set in the
2237 ;; middle of one, else fall back to treating
2238 ;; the offending thing as a one character token.
2242 (looking-at c-nonsymbol-token-regexp)))
2247 (c-forward-syntactic-ws)
2250 (error (goto-char last)))
2254 (setq count (1+ count)))))
2258 (defun c-backward-token-2 (&optional count balanced limit)
2259 "Move backward by tokens.
2260 See `c-forward-token-2' for details."
2262 (or count (setq count 1))
2264 (- (c-forward-token-2 (- count) balanced limit))
2266 (or limit (setq limit (point-min)))
2267 (let ((jump-syntax (if balanced
2268 c-jump-syntax-balanced
2269 c-jump-syntax-unbalanced))
2273 ;; The count is zero so try to skip to the beginning of the
2276 (progn (c-beginning-of-current-token) (point)))
2277 (if (< (point) limit)
2278 ;; The limit is inside the same token, so return 1.
2281 ;; We're not in the middle of a token. If there's
2282 ;; whitespace after the point then we must move backward,
2283 ;; so set count to 1 in that case.
2284 (and (looking-at c-syntactic-ws-start)
2285 ;; If we're looking at a '#' that might start a cpp
2286 ;; directive then we have to do a more elaborate check.
2287 (or (/= (char-after) ?#)
2288 (not c-opt-cpp-prefix)
2291 (progn (beginning-of-line)
2292 (looking-at "[ \t]*")
2295 (progn (backward-char)
2296 (not (eq (char-before) ?\\)))))))
2299 ;; Use `condition-case' to avoid having to check for buffer
2300 ;; limits in `backward-char', `scan-sexps' and `goto-char' below.
2305 (c-backward-syntactic-ws)
2307 (if (looking-at jump-syntax)
2308 (goto-char (scan-sexps (1+ (point)) -1))
2309 ;; This can be very inefficient if there's a long
2310 ;; sequence of operator tokens without any separation.
2311 ;; That doesn't happen in practice, anyway.
2312 (c-beginning-of-current-token))
2313 (>= (point) limit)))
2316 (error (goto-char last)))
2318 (if (< (point) limit)
2323 (defun c-forward-token-1 (&optional count balanced limit)
2324 "Like `c-forward-token-2' but doesn't treat multicharacter operator
2325 tokens like \"==\" as single tokens, i.e. all sequences of symbol
2326 characters are jumped over character by character. This function is
2327 for compatibility only; it's only a wrapper over `c-forward-token-2'."
2328 (let ((c-nonsymbol-token-regexp "\\s.\\|\\s\(\\|\\s\)"))
2329 (c-forward-token-2 count balanced limit)))
2331 (defun c-backward-token-1 (&optional count balanced limit)
2332 "Like `c-backward-token-2' but doesn't treat multicharacter operator
2333 tokens like \"==\" as single tokens, i.e. all sequences of symbol
2334 characters are jumped over character by character. This function is
2335 for compatibility only; it's only a wrapper over `c-backward-token-2'."
2336 (let ((c-nonsymbol-token-regexp "\\s.\\|\\s\(\\|\\s\)"))
2337 (c-backward-token-2 count balanced limit)))
2340 ;; Tools for doing searches restricted to syntactically relevant text.
2342 (defun c-syntactic-re-search-forward (regexp &optional bound noerror
2343 paren-level not-inside-token
2344 lookbehind-submatch)
2345 "Like `re-search-forward', but only report matches that are found
2346 in syntactically significant text. I.e. matches in comments, macros
2347 or string literals are ignored. The start point is assumed to be
2348 outside any comment, macro or string literal, or else the content of
2349 that region is taken as syntactically significant text.
2351 If PAREN-LEVEL is non-nil, an additional restriction is added to
2352 ignore matches in nested paren sexps, and the search will also not go
2353 outside the current paren sexp.
2355 If NOT-INSIDE-TOKEN is non-nil, matches in the middle of tokens are
2356 ignored. Things like multicharacter operators and special symbols
2357 \(e.g. \"`()\" in Pike) are handled but currently not floating point
2360 If LOOKBEHIND-SUBMATCH is non-nil, it's taken as a number of a
2361 subexpression in REGEXP. The end of that submatch is used as the
2362 position to check for syntactic significance. If LOOKBEHIND-SUBMATCH
2363 isn't used or if that subexpression didn't match then the start
2364 position of the whole match is used instead. The \"look behind\"
2365 subexpression is never tested before the starting position, so it
2366 might be a good idea to include \\=\\= as a match alternative in it.
2368 Optimization note: Matches might be missed if the \"look behind\"
2369 subexpression should match the end of nonwhite syntactic whitespace,
2370 i.e. the end of comments or cpp directives. This since the function
2371 skips over such things before resuming the search. It's also not safe
2372 to assume that the \"look behind\" subexpression never can match
2373 syntactic whitespace."
2375 (or bound (setq bound (point-max)))
2376 (if paren-level (setq paren-level -1))
2378 ;;(message "c-syntactic-re-search-forward %s %s %S" (point) bound regexp)
2380 (let ((start (point))
2382 (last-token-end-pos (point-min))
2383 match-pos found state check-pos check-state tmp)
2388 (re-search-forward regexp bound noerror)
2391 (setq match-pos (point)
2392 state (parse-partial-sexp
2393 pos (match-beginning 0) paren-level nil state)
2395 (if (setq check-pos (and lookbehind-submatch
2396 (match-end lookbehind-submatch)))
2397 (setq check-state (parse-partial-sexp
2398 pos check-pos paren-level nil state))
2402 ;; If we got a look behind subexpression and get an
2403 ;; insignificant match in something that isn't
2404 ;; syntactic whitespace (i.e. strings or in nested
2405 ;; parentheses), then we can never skip more than a
2406 ;; single character from the match position before
2407 ;; continuing the search. That since the look behind
2408 ;; subexpression might match the end of the
2409 ;; insignificant region.
2412 ((setq tmp (elt check-state 3))
2413 ;; Match inside a string.
2414 (if (or lookbehind-submatch
2415 (not (integerp tmp)))
2416 (goto-char (min (1+ pos) bound))
2417 ;; Skip to the end of the string before continuing.
2418 (let ((ender (make-string 1 tmp)) (continue t))
2419 (while (if (search-forward ender bound noerror)
2421 (setq state (parse-partial-sexp
2422 pos (point) nil nil state)
2425 (setq continue nil)))
2428 ((elt check-state 7)
2429 ;; Match inside a line comment. Skip to eol. Use
2430 ;; `re-search-forward' instead of `skip-chars-forward' to get
2431 ;; the right bound behavior.
2432 (re-search-forward "[\n\r]" bound noerror))
2434 ((elt check-state 4)
2435 ;; Match inside a block comment. Skip to the '*/'.
2436 (search-forward "*/" bound noerror))
2438 ((and (not (elt check-state 5))
2439 (eq (char-before check-pos) ?/)
2440 (memq (char-after check-pos) '(?/ ?*)))
2441 ;; Match in the middle of the opener of a block or line
2443 (if (= (char-after check-pos) ?/)
2444 (re-search-forward "[\n\r]" bound noerror)
2445 (search-forward "*/" bound noerror)))
2447 ((and not-inside-token
2448 (or (< check-pos last-token-end-pos)
2451 (goto-char check-pos)
2452 (c-end-of-current-token last-token-end-pos)
2453 (setq last-token-end-pos (point))))))
2454 ;; Match inside a token.
2455 (cond ((<= (point) bound)
2456 (goto-char (min (1+ pos) bound))
2459 (t (signal 'search-failed "end of token"))))
2463 (c-beginning-of-macro start)))
2464 ;; Match inside a macro. Skip to the end of it.
2466 (cond ((<= (point) bound) t)
2468 (t (signal 'search-failed "end of macro"))))
2471 (/= (setq tmp (car check-state)) 0))
2473 ;; Match inside a nested paren sexp.
2474 (if lookbehind-submatch
2475 (goto-char (min (1+ pos) bound))
2476 ;; Skip out of the paren quickly.
2477 (setq state (parse-partial-sexp pos bound 0 nil state)
2479 ;; Have exited the current paren sexp. The
2480 ;; `parse-partial-sexp' above has left us just after the
2481 ;; closing paren in this case. Just make
2482 ;; `re-search-forward' above fail in the appropriate way;
2483 ;; we'll adjust the leave off point below if necessary.
2484 (setq bound (point))))
2493 (signal (car err) (cdr err))))
2495 ;;(message "c-syntactic-re-search-forward done %s" (or match-pos (point)))
2499 (goto-char match-pos)
2502 ;; Search failed. Set point as appropriate.
2503 (cond ((eq noerror t)
2506 (if (eq (car (parse-partial-sexp pos bound -1 nil state)) -1)
2512 (defun c-syntactic-skip-backward (skip-chars &optional limit)
2513 "Like `skip-chars-backward' but only look at syntactically relevant chars,
2514 i.e. don't stop at positions inside syntactic whitespace or string
2515 literals. Preprocessor directives are also ignored, with the exception
2516 of the one that the point starts within, if any. If LIMIT is given,
2517 it's assumed to be at a syntactically relevant position.
2519 This function does not do any hidden buffer changes."
2521 (let ((start (point))
2522 ;; A list of syntactically relevant positions in descending
2523 ;; order. It's used to avoid scanning repeatedly over
2524 ;; potentially large regions with `parse-partial-sexp' to verify
2527 ;; The result from `c-beginning-of-macro' at the start position or the
2528 ;; start position itself if it isn't within a macro. Evaluated on
2534 (< (skip-chars-backward skip-chars limit) 0)
2536 ;; Use `parse-partial-sexp' from a safe position down to
2537 ;; the point to check if it's outside comments and
2539 (let ((pos (point)) safe-pos state)
2540 ;; Pick a safe position as close to the point as
2543 ;; FIXME: Consult `syntax-ppss' here if our
2544 ;; cache doesn't give a good position.
2545 (while (and safe-pos-list
2546 (> (car safe-pos-list) (point)))
2547 (setq safe-pos-list (cdr safe-pos-list)))
2548 (unless (setq safe-pos (car-safe safe-pos-list))
2549 (setq safe-pos (max (or (c-safe-position
2550 (point) (or c-state-cache
2554 safe-pos-list (list safe-pos)))
2557 (setq state (parse-partial-sexp
2560 ;; Cache positions along the way to use if we have to
2561 ;; back up more. Every closing paren on the same
2562 ;; level seems like fairly well spaced positions.
2563 (setq safe-pos (point)
2564 safe-pos-list (cons safe-pos safe-pos-list)))
2567 ((or (elt state 3) (elt state 4))
2568 ;; Inside string or comment. Continue search at the
2570 (if (setq pos (nth 8 state))
2571 ;; It's an emacs where `parse-partial-sexp'
2572 ;; supplies the starting position.
2574 (goto-char (car (c-literal-limits safe-pos))))
2577 ((c-beginning-of-macro limit)
2581 (setq start-macro-beg
2584 (c-beginning-of-macro limit)
2587 ;; It's inside the same macro we started in so it's
2588 ;; a relevant match.
2594 ;; Skip syntactic ws afterwards so that we don't stop at the
2595 ;; end of a comment if `skip-chars' is something like "^/".
2596 (c-backward-syntactic-ws)
2602 ;; Tools for handling comments and string literals.
2604 (defun c-slow-in-literal (&optional lim detect-cpp)
2605 "Return the type of literal point is in, if any.
2606 The return value is `c' if in a C-style comment, `c++' if in a C++
2607 style comment, `string' if in a string literal, `pound' if DETECT-CPP
2608 is non-nil and in a preprocessor line, or nil if somewhere else.
2609 Optional LIM is used as the backward limit of the search. If omitted,
2610 or nil, `c-beginning-of-defun' is used.
2612 The last point calculated is cached if the cache is enabled, i.e. if
2613 `c-in-literal-cache' is bound to a two element vector.
2615 This function does not do any hidden buffer changes."
2616 (if (and (vectorp c-in-literal-cache)
2617 (= (point) (aref c-in-literal-cache 0)))
2618 (aref c-in-literal-cache 1)
2619 (let ((rtn (save-excursion
2620 (let* ((pos (point))
2622 (c-beginning-of-syntax)
2624 (state (parse-partial-sexp lim pos)))
2626 ((elt state 3) 'string)
2627 ((elt state 4) (if (elt state 7) 'c++ 'c))
2628 ((and detect-cpp (c-beginning-of-macro lim)) 'pound)
2630 ;; cache this result if the cache is enabled
2631 (if (not c-in-literal-cache)
2632 (setq c-in-literal-cache (vector (point) rtn)))
2635 ;; XEmacs has a built-in function that should make this much quicker.
2636 ;; I don't think we even need the cache, which makes our lives more
2637 ;; complicated anyway. In this case, lim is only used to detect
2640 ;; Note that there is a bug in Xemacs's buffer-syntactic-context when used in
2641 ;; conjunction with syntax-table-properties. The bug is present in, e.g.,
2642 ;; Xemacs 21.4.4. It manifested itself thus:
2644 ;; Starting with an empty AWK Mode buffer, type
2646 ;; Point gets wrongly left at column 0, rather than being indented to tab-width.
2648 ;; AWK Mode is designed such that when the first / is typed, it gets the
2649 ;; syntax-table property "string fence". When the second / is typed, BOTH /s
2650 ;; are given the s-t property "string". However, buffer-syntactic-context
2651 ;; fails to take account of the change of the s-t property on the opening / to
2652 ;; "string", and reports that the { is within a string started by the second /.
2654 ;; The workaround for this is for the AWK Mode initialisation to switch the
2655 ;; defalias for c-in-literal to c-slow-in-literal. This will slow down other
2656 ;; cc-modes in Xemacs whenever an awk-buffer has been initialised.
2658 ;; (Alan Mackenzie, 2003/4/30).
2660 (defun c-fast-in-literal (&optional lim detect-cpp)
2661 (let ((context (buffer-syntactic-context)))
2663 ((eq context 'string) 'string)
2664 ((eq context 'comment) 'c++)
2665 ((eq context 'block-comment) 'c)
2666 ((and detect-cpp (save-excursion (c-beginning-of-macro lim))) 'pound))))
2668 (defalias 'c-in-literal
2669 (if (fboundp 'buffer-syntactic-context)
2670 'c-fast-in-literal ; XEmacs
2671 'c-slow-in-literal)) ; GNU Emacs
2673 ;; The defalias above isn't enough to shut up the byte compiler.
2674 (cc-bytecomp-defun c-in-literal)
2676 (defun c-literal-limits (&optional lim near not-in-delimiter)
2677 "Return a cons of the beginning and end positions of the comment or
2678 string surrounding point (including both delimiters), or nil if point
2679 isn't in one. If LIM is non-nil, it's used as the \"safe\" position
2680 to start parsing from. If NEAR is non-nil, then the limits of any
2681 literal next to point is returned. \"Next to\" means there's only
2682 spaces and tabs between point and the literal. The search for such a
2683 literal is done first in forward direction. If NOT-IN-DELIMITER is
2684 non-nil, the case when point is inside a starting delimiter won't be
2685 recognized. This only has effect for comments, which have starting
2686 delimiters with more than one character.
2688 This function does not do any hidden buffer changes."
2691 (let* ((pos (point))
2693 (c-beginning-of-syntax)
2695 (state (parse-partial-sexp lim pos)))
2697 (cond ((elt state 3)
2698 ;; String. Search backward for the start.
2699 (while (elt state 3)
2700 (search-backward (make-string 1 (elt state 3)))
2701 (setq state (parse-partial-sexp lim (point))))
2702 (cons (point) (or (c-safe (c-forward-sexp 1) (point))
2706 ;; Line comment. Search from bol for the comment starter.
2708 (setq state (parse-partial-sexp lim (point))
2710 (while (not (elt state 7))
2711 (search-forward "//") ; Should never fail.
2712 (setq state (parse-partial-sexp
2713 lim (point) nil nil state)
2716 (cons (point) (progn (c-forward-single-comment) (point))))
2719 ;; Block comment. Search backward for the comment starter.
2720 (while (elt state 4)
2721 (search-backward "/*") ; Should never fail.
2722 (setq state (parse-partial-sexp lim (point))))
2723 (cons (point) (progn (c-forward-single-comment) (point))))
2725 ((and (not not-in-delimiter)
2727 (eq (char-before) ?/)
2728 (looking-at "[/*]"))
2729 ;; We're standing in a comment starter.
2731 (cons (point) (progn (c-forward-single-comment) (point))))
2736 ;; Search forward for a literal.
2737 (skip-chars-forward " \t")
2740 ((looking-at c-string-limit-regexp) ; String.
2741 (cons (point) (or (c-safe (c-forward-sexp 1) (point))
2744 ((looking-at c-comment-start-regexp) ; Line or block comment.
2745 (cons (point) (progn (c-forward-single-comment) (point))))
2749 (skip-chars-backward " \t")
2751 (let ((end (point)) beg)
2754 (< (skip-syntax-backward c-string-syntax) 0)) ; String.
2755 (setq beg (c-safe (c-backward-sexp 1) (point))))
2757 ((and (c-safe (forward-char -2) t)
2759 ;; Block comment. Due to the nature of line
2760 ;; comments, they will always be covered by the
2761 ;; normal case above.
2763 (c-backward-single-comment)
2764 ;; If LIM is bogus, beg will be bogus.
2765 (setq beg (point))))
2767 (if beg (cons beg end))))))
2770 (defun c-literal-limits-fast (&optional lim near not-in-delimiter)
2771 ;; Like c-literal-limits, but for emacsen whose `parse-partial-sexp'
2772 ;; returns the pos of the comment start.
2774 "Return a cons of the beginning and end positions of the comment or
2775 string surrounding point (including both delimiters), or nil if point
2776 isn't in one. If LIM is non-nil, it's used as the \"safe\" position
2777 to start parsing from. If NEAR is non-nil, then the limits of any
2778 literal next to point is returned. \"Next to\" means there's only
2779 spaces and tabs between point and the literal. The search for such a
2780 literal is done first in forward direction. If NOT-IN-DELIMITER is
2781 non-nil, the case when point is inside a starting delimiter won't be
2782 recognized. This only has effect for comments, which have starting
2783 delimiters with more than one character.
2785 This function does not do any hidden buffer changes."
2788 (let* ((pos (point))
2790 (c-beginning-of-syntax)
2792 (state (parse-partial-sexp lim pos)))
2794 (cond ((elt state 3) ; String.
2795 (goto-char (elt state 8))
2796 (cons (point) (or (c-safe (c-forward-sexp 1) (point))
2799 ((elt state 4) ; Comment.
2800 (goto-char (elt state 8))
2801 (cons (point) (progn (c-forward-single-comment) (point))))
2803 ((and (not not-in-delimiter)
2805 (eq (char-before) ?/)
2806 (looking-at "[/*]"))
2807 ;; We're standing in a comment starter.
2809 (cons (point) (progn (c-forward-single-comment) (point))))
2814 ;; Search forward for a literal.
2815 (skip-chars-forward " \t")
2818 ((looking-at c-string-limit-regexp) ; String.
2819 (cons (point) (or (c-safe (c-forward-sexp 1) (point))
2822 ((looking-at c-comment-start-regexp) ; Line or block comment.
2823 (cons (point) (progn (c-forward-single-comment) (point))))
2827 (skip-chars-backward " \t")
2829 (let ((end (point)) beg)
2832 (< (skip-syntax-backward c-string-syntax) 0)) ; String.
2833 (setq beg (c-safe (c-backward-sexp 1) (point))))
2835 ((and (c-safe (forward-char -2) t)
2837 ;; Block comment. Due to the nature of line
2838 ;; comments, they will always be covered by the
2839 ;; normal case above.
2841 (c-backward-single-comment)
2842 ;; If LIM is bogus, beg will be bogus.
2843 (setq beg (point))))
2845 (if beg (cons beg end))))))
2848 (if (memq 'pps-extended-state c-emacs-features)
2849 (defalias 'c-literal-limits 'c-literal-limits-fast))
2851 (defun c-collect-line-comments (range)
2852 "If the argument is a cons of two buffer positions (such as returned by
2853 `c-literal-limits'), and that range contains a C++ style line comment,
2854 then an extended range is returned that contains all adjacent line
2855 comments (i.e. all comments that starts in the same column with no
2856 empty lines or non-whitespace characters between them). Otherwise the
2857 argument is returned.
2859 This function does not do any hidden buffer changes."
2862 (if (and (consp range) (progn
2863 (goto-char (car range))
2865 (let ((col (current-column))
2867 (bopl (c-point 'bopl))
2869 ;; Got to take care in the backward direction to handle
2870 ;; comments which are preceded by code.
2871 (while (and (c-backward-single-comment)
2874 (= col (current-column)))
2876 bopl (c-point 'bopl)))
2878 (while (and (progn (skip-chars-forward " \t")
2880 (= col (current-column))
2881 (prog1 (zerop (forward-line 1))
2882 (setq end (point)))))
2887 (defun c-literal-type (range)
2888 "Convenience function that given the result of `c-literal-limits',
2889 returns nil or the type of literal that the range surrounds. It's
2890 much faster than using `c-in-literal' and is intended to be used when
2891 you need both the type of a literal and its limits.
2893 This function does not do any hidden buffer changes."
2896 (goto-char (car range))
2897 (cond ((looking-at c-string-limit-regexp) 'string)
2898 ((or (looking-at "//") ; c++ line comment
2899 (and (looking-at "\\s<") ; comment starter
2900 (looking-at "#"))) ; awk comment.
2902 (t 'c))) ; Assuming the range is valid.
2906 ;; `c-find-decl-spots' and accompanying stuff.
2908 ;; Variables used in `c-find-decl-spots' to cache the search done for
2909 ;; the first declaration in the last call. When that function starts,
2910 ;; it needs to back up over syntactic whitespace to look at the last
2911 ;; token before the region being searched. That can sometimes cause
2912 ;; moves back and forth over a quite large region of comments and
2913 ;; macros, which would be repeated for each changed character when
2914 ;; we're called during fontification, since font-lock refontifies the
2915 ;; current line for each change. Thus it's worthwhile to cache the
2918 ;; `c-find-decl-syntactic-pos' is a syntactically relevant position in
2919 ;; the syntactic whitespace less or equal to some start position.
2920 ;; There's no cached value if it's nil.
2922 ;; `c-find-decl-match-pos' is the match position if
2923 ;; `c-find-decl-prefix-search' matched before the syntactic whitespace
2924 ;; at `c-find-decl-syntactic-pos', or nil if there's no such match.
2925 (defvar c-find-decl-syntactic-pos nil)
2926 (make-variable-buffer-local 'c-find-decl-syntactic-pos)
2927 (defvar c-find-decl-match-pos nil)
2928 (make-variable-buffer-local 'c-find-decl-match-pos)
2930 (defsubst c-invalidate-find-decl-cache (change-min-pos)
2931 (and c-find-decl-syntactic-pos
2932 (< change-min-pos c-find-decl-syntactic-pos)
2933 (setq c-find-decl-syntactic-pos nil)))
2935 ; (defface c-debug-decl-spot-face
2936 ; '((t (:background "Turquoise")))
2937 ; "Debug face to mark the spots where `c-find-decl-spots' stopped.")
2938 ; (defface c-debug-decl-sws-face
2939 ; '((t (:background "Khaki")))
2940 ; "Debug face to mark the syntactic whitespace between the declaration
2941 ; spots and the preceding token end.")
2943 (defmacro c-debug-put-decl-spot-faces (match-pos decl-pos)
2944 (when (facep 'c-debug-decl-spot-face)
2945 `(let ((match-pos ,match-pos) (decl-pos ,decl-pos))
2946 (c-debug-add-face (max match-pos (point-min)) decl-pos
2947 'c-debug-decl-sws-face)
2948 (c-debug-add-face decl-pos (min (1+ decl-pos) (point-max))
2949 'c-debug-decl-spot-face))))
2950 (defmacro c-debug-remove-decl-spot-faces (beg end)
2951 (when (facep 'c-debug-decl-spot-face)
2953 (c-debug-remove-face ,beg ,end 'c-debug-decl-spot-face)
2954 (c-debug-remove-face ,beg ,end 'c-debug-decl-sws-face))))
2956 (defmacro c-find-decl-prefix-search ()
2957 ;; Macro used inside `c-find-decl-spots'. It ought to be a defun,
2958 ;; but it contains lots of free variables that refer to things
2959 ;; inside `c-find-decl-spots'. The point is left at `cfd-match-pos'
2960 ;; if there is a match, otherwise at `cfd-limit'.
2963 ;; Find the next property match position if we haven't got one already.
2964 (unless cfd-prop-match
2967 (goto-char (next-single-property-change
2968 (point) 'c-type nil cfd-limit))
2969 (and (< (point) cfd-limit)
2970 (not (eq (c-get-char-property (1- (point)) 'c-type)
2972 (setq cfd-prop-match (point))))
2974 ;; Find the next `c-decl-prefix-re' match if we haven't got one already.
2975 (unless cfd-re-match
2976 (while (and (setq cfd-re-match
2977 (re-search-forward c-decl-prefix-re cfd-limit 'move))
2978 (c-got-face-at (1- (setq cfd-re-match (match-end 1)))
2980 ;; Search again if the match is within a comment or a string literal.
2982 (goto-char (next-single-property-change
2983 cfd-re-match 'face nil cfd-limit))
2984 (and (< (point) cfd-limit)
2985 (c-got-face-at (point) c-literal-faces)))
2986 (setq cfd-re-match (point))))
2987 (unless cfd-re-match
2988 (setq cfd-re-match cfd-limit)))
2990 ;; Choose whichever match is closer to the start.
2991 (if (< cfd-re-match cfd-prop-match)
2992 (setq cfd-match-pos cfd-re-match
2994 (setq cfd-match-pos cfd-prop-match
2995 cfd-prop-match nil))
2997 (goto-char cfd-match-pos)
2999 (when (< cfd-match-pos cfd-limit)
3000 ;; Skip forward past comments only so we don't skip macros.
3001 (c-forward-comments)
3002 ;; Set the position to continue at. We can avoid going over
3003 ;; the comments skipped above a second time, but it's possible
3004 ;; that the comment skipping has taken us past `cfd-prop-match'
3005 ;; since the property might be used inside comments.
3006 (setq cfd-continue-pos (if cfd-prop-match
3007 (min cfd-prop-match (point))
3010 (defun c-find-decl-spots (cfd-limit cfd-decl-re cfd-face-checklist cfd-fun)
3011 ;; Call CFD-FUN for each possible spot for a declaration from the
3012 ;; point to CFD-LIMIT. A spot for a declaration is the first token
3013 ;; in the buffer and each token after the ones matched by
3014 ;; `c-decl-prefix-re' and after the occurrences of the `c-type'
3015 ;; property with the value `c-decl-end' (if `c-type-decl-end-used'
3016 ;; is set). Only a spot that match CFD-DECL-RE and whose face is in
3017 ;; the CFD-FACE-CHECKLIST list causes CFD-FUN to be called. The
3018 ;; face check is disabled if CFD-FACE-CHECKLIST is nil.
3020 ;; If the match is inside a macro then the buffer is narrowed to the
3021 ;; end of it, so that CFD-FUN can investigate the following tokens
3022 ;; without matching something that begins inside a macro and ends
3023 ;; outside it. It's to avoid this work that the CFD-DECL-RE and
3024 ;; CFD-FACE-CHECKLIST checks exist.
3026 ;; CFD-FUN is called with point at the start of the spot. It's
3027 ;; passed two arguments: The first is the end position of the token
3028 ;; that `c-decl-prefix-re' matched, or 0 for the implicit match at
3029 ;; bob. The second is a flag that is t when the match is inside a
3032 ;; It's assumed that comment and strings are fontified in the
3035 ;; This is mainly used in fontification, and so has an elaborate
3036 ;; cache to handle repeated calls from the same start position; see
3037 ;; the variables above.
3039 ;; All variables in this function begin with `cfd-' to avoid name
3040 ;; collision with the (dynamically bound) variables used in CFD-FUN.
3042 (let ((cfd-buffer-end (point-max))
3043 ;; The last regexp match found by `c-find-decl-prefix-search'.
3045 ;; The last `c-decl-end' found by `c-find-decl-prefix-search'.
3046 ;; If searching for the property isn't needed then we disable
3047 ;; it by faking a first match at the limit.
3048 (cfd-prop-match (unless c-type-decl-end-used cfd-limit))
3049 ;; The position of the last match found by
3050 ;; `c-find-decl-prefix-search'. For regexp matches it's the
3051 ;; end of the matched token, for property matches it's the end
3052 ;; of the property. 0 for the implicit match at bob.
3053 ;; `cfd-limit' if there's no match.
3054 (cfd-match-pos cfd-limit)
3055 ;; The position to continue searching at.
3057 ;; The position of the last "real" token we've stopped at.
3058 ;; This can be greater than `cfd-continue-pos' when we get
3059 ;; hits inside macros or at `c-decl-end' positions inside
3062 ;; The end position of the last entered macro.
3065 ;; Initialize by finding a syntactically relevant start position
3066 ;; before the point, and do the first `c-decl-prefix-re' search
3067 ;; unless we're at bob.
3069 (let ((start-pos (point)) syntactic-pos)
3070 ;; Must back up a bit since we look for the end of the previous
3071 ;; statement or declaration, which is earlier than the first
3074 (when (c-got-face-at (point) c-literal-faces)
3075 ;; But first we need to move to a syntactically relevant
3076 ;; position. Use the faces to back up to the start of the
3077 ;; comment or string literal.
3078 (when (and (not (bobp))
3079 (c-got-face-at (1- (point)) c-literal-faces))
3081 (goto-char (previous-single-property-change
3082 (point) 'face nil (point-min)))
3083 (and (> (point) (point-min))
3084 (c-got-face-at (point) c-literal-faces)))))
3086 ;; XEmacs doesn't fontify the quotes surrounding string
3088 (and (featurep 'xemacs)
3089 (eq (get-text-property (point) 'face)
3090 'font-lock-string-face)
3092 (progn (backward-char)
3093 (not (looking-at c-string-limit-regexp)))
3096 ;; The font lock package might not have fontified the start of
3097 ;; the literal at all so check that we have arrived at
3098 ;; something that looks like a start or else resort to
3099 ;; `c-literal-limits'.
3100 (unless (looking-at c-literal-start-regexp)
3101 (let ((range (c-literal-limits)))
3102 (if range (goto-char (car range))))))
3104 ;; Must back out of any macro so that we don't miss any
3105 ;; declaration that could follow after it, unless the limit is
3106 ;; inside the macro. We only check that for the current line to
3107 ;; save some time; it's enough for the by far most common case
3108 ;; when font-lock refontifies the current line only.
3109 (when (save-excursion
3110 (and (= (forward-line 1) 0)
3111 (or (< (c-point 'eol) cfd-limit)
3112 (progn (backward-char)
3113 (not (eq (char-before) ?\\))))))
3114 (c-beginning-of-macro))
3116 ;; Clear the cache if it applied further down.
3117 (c-invalidate-find-decl-cache start-pos)
3119 (setq syntactic-pos (point))
3120 (c-backward-syntactic-ws c-find-decl-syntactic-pos)
3122 ;; If we hit `c-find-decl-syntactic-pos' and
3123 ;; `c-find-decl-match-pos' is set then we install the cached
3124 ;; values. If we hit `c-find-decl-syntactic-pos' and
3125 ;; `c-find-decl-match-pos' is nil then we know there's no decl
3126 ;; prefix in the whitespace before `c-find-decl-syntactic-pos'
3127 ;; and so we can continue the search from this point. If we
3128 ;; didn't hit `c-find-decl-syntactic-pos' then we're now in the
3129 ;; right spot to begin searching anyway.
3130 (if (and (eq (point) c-find-decl-syntactic-pos)
3131 c-find-decl-match-pos)
3134 ;; The match is always outside macros and comments so we
3135 ;; start at the next token. The loop below will later go
3136 ;; back using `cfd-continue-pos' to fix declarations inside
3137 ;; the syntactic ws.
3138 (goto-char syntactic-pos)
3139 (c-forward-syntactic-ws)
3140 (setq cfd-match-pos c-find-decl-match-pos
3141 cfd-continue-pos syntactic-pos)
3142 (if (< cfd-continue-pos (point))
3143 (setq cfd-token-pos (point))))
3145 (setq c-find-decl-syntactic-pos syntactic-pos)
3148 ;; Always consider bob a match to get the first declaration
3149 ;; in the file. Do this separately instead of letting
3150 ;; `c-decl-prefix-re' match bob, so that it always can
3151 ;; consume at least one character to ensure that we won't
3152 ;; get stuck in an infinite loop.
3153 (setq cfd-re-match 0)
3155 (c-beginning-of-current-token)
3156 (< (point) cfd-limit))
3157 ;; Do an initial search now. In the bob case above it's only done
3158 ;; to search for the `c-type' property.
3159 (c-find-decl-prefix-search))
3161 ;; Advance `cfd-continue-pos' if we got a hit before the start
3162 ;; position. The earliest position that could affect after
3163 ;; the start position is the char before the preceding
3165 (when (and cfd-continue-pos (< cfd-continue-pos start-pos))
3166 (goto-char syntactic-pos)
3167 (c-backward-comments)
3170 (c-beginning-of-current-token))
3171 (setq cfd-continue-pos (max cfd-continue-pos (point))))
3173 ;; If we got a match it's always outside macros and comments so
3174 ;; advance to the next token and set `cfd-token-pos'. The loop
3175 ;; below will later go back using `cfd-continue-pos' to fix
3176 ;; declarations inside the syntactic ws.
3177 (when (and (< cfd-match-pos cfd-limit) (< (point) syntactic-pos))
3178 (goto-char syntactic-pos)
3179 (c-forward-syntactic-ws)
3180 (and cfd-continue-pos
3181 (< cfd-continue-pos (point))
3182 (setq cfd-token-pos (point))))
3184 (setq c-find-decl-match-pos (and (< cfd-match-pos start-pos)
3187 ;; Now loop. We already got the first match.
3191 (< cfd-match-pos cfd-limit)
3194 ;; Kludge to filter out matches on the "<" that
3195 ;; aren't open parens, for the sake of languages
3196 ;; that got `c-recognize-<>-arglists' set.
3197 (and (eq (char-before cfd-match-pos) ?<)
3198 (not (c-get-char-property (1- cfd-match-pos)
3201 ;; If `cfd-continue-pos' is less or equal to
3202 ;; `cfd-token-pos', we've got a hit inside a macro
3203 ;; that's in the syntactic whitespace before the last
3204 ;; "real" declaration we've checked. If they're equal
3205 ;; we've arrived at the declaration a second time, so
3206 ;; there's nothing to do.
3207 (= cfd-continue-pos cfd-token-pos)
3210 ;; If `cfd-continue-pos' is less than `cfd-token-pos'
3211 ;; we're still searching for declarations embedded in
3212 ;; the syntactic whitespace. In that case we need
3213 ;; only to skip comments and not macros, since they
3214 ;; can't be nested, and that's already been done in
3215 ;; `c-find-decl-prefix-search'.
3216 (when (> cfd-continue-pos cfd-token-pos)
3217 (c-forward-syntactic-ws)
3218 (setq cfd-token-pos (point)))
3220 ;; Continue if the following token fails the
3221 ;; CFD-DECL-RE and CFD-FACE-CHECKLIST checks.
3222 (when (or (>= (point) cfd-limit)
3223 (not (looking-at cfd-decl-re))
3224 (and cfd-face-checklist
3226 (point) cfd-face-checklist))))
3227 (goto-char cfd-continue-pos)
3230 (< (point) cfd-limit))
3231 (c-find-decl-prefix-search))
3233 (< (point) cfd-limit))
3236 ;; Narrow to the end of the macro if we got a hit inside
3237 ;; one, to avoid recognizing things that start inside
3238 ;; the macro and end outside it.
3239 (when (> cfd-match-pos cfd-macro-end)
3240 ;; Not in the same macro as in the previous round.
3242 (goto-char cfd-match-pos)
3244 (if (save-excursion (and (c-beginning-of-macro)
3245 (< (point) cfd-match-pos)))
3246 (progn (c-end-of-macro)
3250 (if (zerop cfd-macro-end)
3252 (if (> cfd-macro-end (point))
3253 (progn (narrow-to-region (point-min) cfd-macro-end)
3255 ;; The matched token was the last thing in the
3256 ;; macro, so the whole match is bogus.
3257 (setq cfd-macro-end 0)
3260 (c-debug-put-decl-spot-faces cfd-match-pos (point))
3261 (funcall cfd-fun cfd-match-pos (/= cfd-macro-end 0))
3263 (when (/= cfd-macro-end 0)
3264 ;; Restore limits if we did macro narrowment above.
3265 (narrow-to-region (point-min) cfd-buffer-end)))
3267 (goto-char cfd-continue-pos)
3268 (if (= cfd-continue-pos cfd-limit)
3269 (setq cfd-match-pos cfd-limit)
3270 (c-find-decl-prefix-search)))))
3273 ;; A cache for found types.
3275 ;; Buffer local variable that contains an obarray with the types we've
3276 ;; found. If a declaration is recognized somewhere we record the
3277 ;; fully qualified identifier in it to recognize it as a type
3278 ;; elsewhere in the file too. This is not accurate since we do not
3279 ;; bother with the scoping rules of the languages, but in practice the
3280 ;; same name is seldom used as both a type and something else in a
3281 ;; file, and we only use this as a last resort in ambiguous cases (see
3282 ;; `c-font-lock-declarations').
3283 (defvar c-found-types nil)
3284 (make-variable-buffer-local 'c-found-types)
3286 (defsubst c-clear-found-types ()
3287 ;; Clears `c-found-types'.
3289 ;; This function does not do any hidden buffer changes.
3290 (setq c-found-types (make-vector 53 0)))
3292 (defun c-add-type (from to)
3293 ;; Add the given region as a type in `c-found-types'. If the region
3294 ;; doesn't match an existing type but there is a type which is equal
3295 ;; to the given one except that the last character is missing, then
3296 ;; the shorter type is removed. That's done to avoid adding all
3297 ;; prefixes of a type as it's being entered and font locked. This
3298 ;; doesn't cover cases like when characters are removed from a type
3299 ;; or added in the middle. We'd need the position of point when the
3300 ;; font locking is invoked to solve this well.
3301 (unless (and c-recognize-<>-arglists
3304 (c-syntactic-re-search-forward "<" to t)))
3305 ;; To avoid storing very long strings, do not add a type that
3306 ;; contains '<' in languages with angle bracket arglists, since
3307 ;; the type then probably contains a C++ template spec and those
3308 ;; can be fairly sized programs in themselves.
3309 (let ((type (c-syntactic-content from to)))
3310 (unless (intern-soft type c-found-types)
3311 (unintern (substring type 0 -1) c-found-types)
3312 (intern type c-found-types)))))
3314 (defsubst c-check-type (from to)
3315 ;; Return non-nil if the given region contains a type in
3317 (intern-soft (c-syntactic-content from to) c-found-types))
3319 (defun c-list-found-types ()
3320 ;; Return all the types in `c-found-types' as a sorted list of
3323 (mapatoms (lambda (type)
3324 (setq type-list (cons (symbol-name type)
3327 (sort type-list 'string-lessp)))
3330 ;; Handling of small scale constructs like types and names.
3332 (defun c-remove-<>-arglist-properties (from to)
3333 ;; Remove all the properties put by `c-forward-<>-arglist' in the
3334 ;; specified region. Point is clobbered.
3336 (while (progn (skip-chars-forward "^<>," to)
3338 (if (eq (char-after) ?,)
3339 (when (eq (c-get-char-property (point) 'c-type) 'c-<>-arg-sep)
3340 (c-clear-char-property (point) 'c-type))
3341 (c-clear-char-property (point) 'syntax-table))
3344 ;; Dynamically bound variable that instructs `c-forward-type' to also
3345 ;; treat possible types (i.e. those that it normally returns 'maybe or
3346 ;; 'found for) as actual types (and always return 'found for them).
3347 ;; This means that it records them in `c-record-type-identifiers' if
3348 ;; that is set, and that it adds them to `c-found-types'.
3349 (defvar c-promote-possible-types nil)
3351 ;; Dynamically bound variable that instructs `c-forward-<>-arglist' to
3352 ;; not accept arglists that contain more than one argument. It's used
3353 ;; to handle ambiguous cases like "foo (a < b, c > d)" better.
3354 (defvar c-disallow-comma-in-<>-arglists nil)
3356 ;; Dynamically bound variables that instructs `c-forward-name',
3357 ;; `c-forward-type' and `c-forward-<>-arglist' to record the ranges of
3358 ;; all the type and reference identifiers they encounter. They will
3359 ;; build lists on these variables where each element is a cons of the
3360 ;; buffer positions surrounding each identifier. This recording is
3361 ;; only activated when `c-record-type-identifiers' is non-nil.
3363 ;; All known types that can't be identifiers are recorded, and also
3364 ;; other possible types if `c-promote-possible-types' is set.
3365 ;; Recording is however disabled inside angle bracket arglists that
3366 ;; are encountered inside names and other angle bracket arglists.
3367 ;; Such occurences are taken care of by `c-font-lock-<>-arglists'
3370 ;; Only the names in C++ template style references (e.g. "tmpl" in
3371 ;; "tmpl<a,b>::foo") are recorded as references, other references
3372 ;; aren't handled here.
3373 (defvar c-record-type-identifiers nil)
3374 (defvar c-record-ref-identifiers nil)
3376 ;; If `c-record-type-identifiers' is set, this will receive a cons
3377 ;; cell of the range of the last single identifier symbol stepped over
3378 ;; by `c-forward-name' if it's successful. This is the range that
3379 ;; should be put on one of the record lists by the caller. It's
3380 ;; assigned nil if there's no such symbol in the name.
3381 (defvar c-last-identifier-range nil)
3383 (defmacro c-record-type-id (range)
3384 (if (eq (car-safe range) 'cons)
3386 `(setq c-record-type-identifiers
3387 (cons ,range c-record-type-identifiers))
3388 `(let ((range ,range))
3390 (setq c-record-type-identifiers
3391 (cons range c-record-type-identifiers))))))
3393 (defmacro c-record-ref-id (range)
3394 (if (eq (car-safe range) 'cons)
3396 `(setq c-record-ref-identifiers
3397 (cons ,range c-record-ref-identifiers))
3398 `(let ((range ,range))
3400 (setq c-record-ref-identifiers
3401 (cons range c-record-ref-identifiers))))))
3403 ;; Dynamically bound variable that instructs `c-forward-type' to
3404 ;; record the ranges of types that only are found. Behaves otherwise
3405 ;; like `c-record-type-identifiers'.
3406 (defvar c-record-found-types nil)
3408 (defmacro c-forward-keyword-prefixed-id (type)
3409 ;; Used internally in `c-forward-keyword-clause' to move forward
3410 ;; over a type (if TYPE is 'type) or a name (otherwise) which
3411 ;; possibly is prefixed by keywords and their associated clauses.
3412 ;; Try with a type/name first to not trip up on those that begin
3413 ;; with a keyword. Return t if a known or found type is moved
3414 ;; over. The point is clobbered if nil is returned. If range
3415 ;; recording is enabled, the identifier is recorded on as a type
3416 ;; if TYPE is 'type or as a reference if TYPE is 'ref.
3418 (while (if (setq res ,(if (eq type 'type)
3422 (and (looking-at c-keywords-regexp)
3423 (c-forward-keyword-clause))))
3424 (when (memq res '(t known found prefix))
3425 ,(when (eq type 'ref)
3426 `(when c-record-type-identifiers
3427 (c-record-ref-id c-last-identifier-range)))
3430 (defmacro c-forward-id-comma-list (type)
3431 ;; Used internally in `c-forward-keyword-clause' to move forward
3432 ;; over a comma separated list of types or names using
3433 ;; `c-forward-keyword-prefixed-id'.
3435 (setq safe-pos (point))
3436 (eq (char-after) ?,))
3439 (c-forward-syntactic-ws)
3440 (c-forward-keyword-prefixed-id ,type)))))
3442 (defun c-forward-keyword-clause ()
3443 ;; The first submatch in the current match data is assumed to
3444 ;; surround a token. If it's a keyword, move over it and any
3445 ;; following clauses associated with it, stopping at the next
3446 ;; following token. t is returned in that case, otherwise the point
3447 ;; stays and nil is returned. The kind of clauses that are
3448 ;; recognized are those specified by `c-type-list-kwds',
3449 ;; `c-ref-list-kwds', `c-colon-type-list-kwds',
3450 ;; `c-paren-nontype-kwds', `c-paren-type-kwds', `c-<>-type-kwds',
3451 ;; and `c-<>-arglist-kwds'.
3453 (let ((kwd-sym (c-keyword-sym (match-string 1))) safe-pos pos)
3455 (goto-char (match-end 1))
3456 (c-forward-syntactic-ws)
3457 (setq safe-pos (point))
3460 ((and (c-keyword-member kwd-sym 'c-type-list-kwds)
3461 (c-forward-keyword-prefixed-id type))
3462 ;; There's a type directly after a keyword in `c-type-list-kwds'.
3463 (c-forward-id-comma-list type))
3465 ((and (c-keyword-member kwd-sym 'c-ref-list-kwds)
3466 (c-forward-keyword-prefixed-id ref))
3467 ;; There's a name directly after a keyword in `c-ref-list-kwds'.
3468 (c-forward-id-comma-list ref))
3470 ((and (c-keyword-member kwd-sym 'c-paren-any-kwds)
3471 (eq (char-after) ?\())
3472 ;; There's an open paren after a keyword in `c-paren-any-kwds'.
3475 (when (and (setq pos (c-up-list-forward))
3476 (eq (char-before pos) ?\)))
3477 (when (and c-record-type-identifiers
3478 (c-keyword-member kwd-sym 'c-paren-type-kwds))
3479 ;; Use `c-forward-type' on every identifier we can find
3480 ;; inside the paren, to record the types.
3481 (while (c-syntactic-re-search-forward c-symbol-start pos t)
3482 (goto-char (match-beginning 0))
3483 (unless (c-forward-type)
3484 (looking-at c-symbol-key) ; Always matches.
3485 (goto-char (match-end 0)))))
3488 (c-forward-syntactic-ws)
3489 (setq safe-pos (point))))
3491 ((and (c-keyword-member kwd-sym 'c-<>-sexp-kwds)
3492 (eq (char-after) ?<)
3493 (c-forward-<>-arglist (c-keyword-member kwd-sym 'c-<>-type-kwds)
3494 (or c-record-type-identifiers
3495 c-disallow-comma-in-<>-arglists)))
3496 (c-forward-syntactic-ws)
3497 (setq safe-pos (point)))
3499 ((and (c-keyword-member kwd-sym 'c-nonsymbol-sexp-kwds)
3500 (not (looking-at c-symbol-start)))
3502 (c-forward-syntactic-ws)
3503 (setq safe-pos (point))))
3505 (when (and (c-keyword-member kwd-sym 'c-colon-type-list-kwds)
3507 ;; If a keyword matched both one of the types above and
3508 ;; this one, we match `c-colon-type-list-re' after the
3509 ;; clause matched above.
3510 (goto-char safe-pos)
3511 (looking-at c-colon-type-list-re))
3513 (goto-char (match-end 0))
3514 (c-forward-syntactic-ws)
3515 (c-forward-keyword-prefixed-id type)))
3516 ;; There's a type after the `c-colon-type-list-re'
3517 ;; match after a keyword in `c-colon-type-list-kwds'.
3518 (c-forward-id-comma-list type))
3520 (goto-char safe-pos)
3523 (defun c-forward-<>-arglist (all-types reparse)
3524 ;; The point is assumed to be at a '<'. Try to treat it as the open
3525 ;; paren of an angle bracket arglist and move forward to the the
3526 ;; corresponding '>'. If successful, the point is left after the
3527 ;; '>' and t is returned, otherwise the point isn't moved and nil is
3528 ;; returned. If ALL-TYPES is t then all encountered arguments in
3529 ;; the arglist that might be types are treated as found types.
3531 ;; The surrounding '<' and '>' are given syntax-table properties to
3532 ;; make them behave like parentheses. Each argument separating ','
3533 ;; is also set to `c-<>-arg-sep' in the `c-type' property. These
3534 ;; properties are also cleared in a relevant region forward from the
3535 ;; point if they seems to be set and it turns out to not be an
3538 ;; If the arglist has been successfully parsed before then paren
3539 ;; syntax properties will be exploited to quickly jump to the end,
3540 ;; but that can be disabled by setting REPARSE to t. That is
3541 ;; necessary if the various side effects, e.g. recording of type
3542 ;; ranges, are important. Setting REPARSE to t only applies
3543 ;; recursively to nested angle bracket arglists if
3544 ;; `c-disallow-comma-in-<>-arglists' is set.
3546 ;; This is primarily used in C++ to mark up template arglists. C++
3547 ;; disambiguates them by checking whether the preceding name is a
3548 ;; template or not. We can't do that, so we assume it is a template
3549 ;; if it can be parsed as one. This usually works well since
3550 ;; comparison expressions on the forms "a < b > c" or "a < b, c > d"
3551 ;; in almost all cases would be pointless. Cases like function
3552 ;; calls on the form "foo (a < b, c > d)" needs to be handled
3553 ;; specially through the `c-disallow-comma-in-<>-arglists' variable.
3555 (let ((start (point))
3556 ;; If `c-record-type-identifiers' is set then activate
3557 ;; recording of any found types that constitute an argument in
3559 (c-record-found-types (if c-record-type-identifiers t)))
3560 (if (catch 'angle-bracket-arglist-escape
3561 (setq c-record-found-types
3562 (c-forward-<>-arglist-recur all-types reparse)))
3564 (when (consp c-record-found-types)
3565 (setq c-record-type-identifiers
3566 ;; `nconc' doesn't mind that the tail of
3567 ;; `c-record-found-types' is t.
3568 (nconc c-record-found-types c-record-type-identifiers)))
3574 (defun c-forward-<>-arglist-recur (all-types reparse)
3575 ;; Recursive part of `c-forward-<>-arglist'.
3577 (let ((start (point)) res pos tmp
3578 ;; Cover this so that any recorded found type ranges are
3579 ;; automatically lost if it turns out to not be an angle
3580 ;; bracket arglist. It's propagated through the return value
3581 ;; on successful completion.
3582 (c-record-found-types c-record-found-types)
3583 ;; List that collects the positions after the argument
3584 ;; separating ',' in the arglist.
3587 ;; If the '<' has paren open syntax then we've marked it as an
3588 ;; angle bracket arglist before, so try to skip to the end and see
3589 ;; that the close paren matches.
3590 (if (and (c-get-char-property (point) 'syntax-table)
3593 (if (and (not (looking-at c-<-op-cont-regexp))
3594 (if (c-parse-sexp-lookup-properties)
3595 (c-go-up-list-forward)
3598 (while (c-syntactic-re-search-forward
3600 (when (c-get-char-property (1- (point))
3602 (if (eq (char-before) ?<)
3603 (setq depth (1+ depth))
3604 (setq depth (1- depth))
3605 (when (= depth 0) (throw 'at-end t)))))
3607 (not (looking-at c->-op-cont-regexp))
3611 (progn (c-beginning-of-current-token)
3614 ;; Got an arglist that appears to be valid.
3616 ;; Reparsing is requested, so zap the properties in the
3617 ;; region and go on to redo it. It's done here to
3618 ;; avoid leaving it behind if we exit through
3619 ;; `angle-bracket-arglist-escape' below.
3621 (c-remove-<>-arglist-properties start (point))
3626 ;; Got unmatched paren brackets or either paren was
3627 ;; actually some other token. Recover by clearing the
3628 ;; syntax properties on all the '<' and '>' in the
3629 ;; range where we'll search for the arglist below.
3631 (while (progn (skip-chars-forward "^<>,;{}")
3632 (looking-at "[<>,]"))
3633 (if (eq (char-after) ?,)
3634 (when (eq (c-get-char-property (point) 'c-type)
3636 (c-clear-char-property (point) 'c-type))
3637 (c-clear-char-property (point) 'syntax-table))
3644 (unless (looking-at c-<-op-cont-regexp)
3648 (when c-record-type-identifiers
3651 ;; All encountered identifiers are types, so set the
3652 ;; promote flag and parse the type.
3654 (c-forward-syntactic-ws)
3655 (when (looking-at c-identifier-start)
3656 (let ((c-promote-possible-types t))
3659 ;; Check if this arglist argument is a sole type. If
3660 ;; it's known then it's recorded in
3661 ;; `c-record-type-identifiers'. If it only is found
3662 ;; then it's recorded in `c-record-found-types' which we
3663 ;; might roll back if it turns out that this isn't an
3664 ;; angle bracket arglist afterall.
3665 (when (memq (char-before) '(?, ?<))
3666 (let ((orig-record-found-types c-record-found-types))
3667 (c-forward-syntactic-ws)
3668 (and (memq (c-forward-type) '(known found))
3669 (not (looking-at "[,>]"))
3670 ;; A found type was recorded but it's not the
3671 ;; only thing in the arglist argument, so reset
3672 ;; `c-record-found-types'.
3673 (setq c-record-found-types
3674 orig-record-found-types))))))
3677 (or (when (eq (char-after) ?>)
3678 ;; Must check for '>' at the very start separately,
3679 ;; since the regexp below has to avoid ">>" without
3684 ;; Note: This regexp exploits the match order in
3685 ;; \| so that "<>" is matched by "<" rather than
3687 (c-syntactic-re-search-forward
3688 "[<;{},]\\|\\([^>:-]>\\)" nil 'move t t 1)
3690 ;; If the arglist starter has lost its open paren
3691 ;; syntax but not the closer, we won't find the
3692 ;; closer above since we only search in the
3693 ;; balanced sexp. In that case we stop just short
3694 ;; of it so check if the following char is the closer.
3695 (when (eq (char-after) ?>)
3696 ;; Remove its syntax so that we don't enter the
3697 ;; recovery code below. That's not necessary
3698 ;; since there's no real reason to suspect that
3699 ;; things inside the arglist are unbalanced.
3700 (c-clear-char-property (point) 'syntax-table)
3705 ((eq (char-before) ?>)
3706 ;; Either an operator starting with '>' or the end of
3707 ;; the angle bracket arglist.
3709 (if (and (/= (1- (point)) pos)
3710 (c-get-char-property (1- (point)) 'syntax-table)
3712 (c-clear-char-property (1- (point)) 'syntax-table)
3713 (c-parse-sexp-lookup-properties)))
3715 ;; We've skipped past a list that ended with '>'. It
3716 ;; must be unbalanced since nested arglists are handled
3717 ;; in the case below. Recover by removing all paren
3718 ;; properties on '<' and '>' in the searched region and
3721 (c-remove-<>-arglist-properties pos (point))
3725 (if (looking-at c->-op-cont-regexp)
3727 (when (text-property-not-all
3728 (1- (point)) (match-end 0) 'syntax-table nil)
3729 (c-remove-<>-arglist-properties (1- (point))
3731 (goto-char (match-end 0))
3734 ;; The angle bracket arglist is finished.
3735 (while arg-start-pos
3736 (c-put-char-property (1- (car arg-start-pos))
3737 'c-type 'c-<>-arg-sep)
3738 (setq arg-start-pos (cdr arg-start-pos)))
3739 (c-mark-<-as-paren start)
3740 (c-mark->-as-paren (1- (point)))
3744 ((eq (char-before) ?<)
3745 ;; Either an operator starting with '<' or a nested arglist.
3748 (let (id-start id-end subres keyword-match)
3749 (if (if (looking-at c-<-op-cont-regexp)
3750 (setq tmp (match-end 0))
3757 ;; There's always an identifier before a angle
3758 ;; bracket arglist, or a keyword in
3759 ;; `c-<>-type-kwds' or `c-<>-arglist-kwds'.
3760 (c-backward-syntactic-ws)
3761 (setq id-end (point))
3762 (c-simple-skip-symbol-backward)
3763 (when (or (setq keyword-match
3764 (looking-at c-opt-<>-sexp-key))
3765 (not (looking-at c-keywords-regexp)))
3766 (setq id-start (point))))
3769 (let ((c-record-type-identifiers nil)
3770 (c-record-found-types nil))
3771 (c-forward-<>-arglist-recur
3774 (c-keyword-sym (match-string 1))
3777 c-disallow-comma-in-<>-arglists))))
3780 ;; It was not an angle bracket arglist.
3782 (when (text-property-not-all
3783 (1- pos) tmp 'syntax-table nil)
3784 (if (c-parse-sexp-lookup-properties)
3785 ;; Got an invalid open paren syntax on this
3786 ;; '<'. We'll probably get an unbalanced '>'
3787 ;; further ahead if we just remove the syntax
3788 ;; here, so recover by removing all paren
3789 ;; properties up to and including the
3790 ;; balancing close paren.
3791 (parse-partial-sexp pos (point-max) -1)
3793 (c-remove-<>-arglist-properties pos (point)))
3796 ;; It was an angle bracket arglist.
3797 (setq c-record-found-types subres)
3799 ;; Record the identifier before the template as a type
3800 ;; or reference depending on whether the arglist is last
3801 ;; in a qualified identifier.
3802 (when (and c-record-type-identifiers
3803 (not keyword-match))
3804 (if (and c-opt-identifier-concat-key
3806 (c-forward-syntactic-ws)
3807 (looking-at c-opt-identifier-concat-key)))
3808 (c-record-ref-id (cons id-start id-end))
3809 (c-record-type-id (cons id-start id-end))))))
3812 ((and (eq (char-before) ?,)
3813 (not c-disallow-comma-in-<>-arglists))
3814 ;; Just another argument. Record the position. The
3815 ;; type check stuff that made us stop at it is at
3816 ;; the top of the loop.
3817 (setq arg-start-pos (cons (point) arg-start-pos)))
3820 ;; Got a character that can't be in an angle bracket
3821 ;; arglist argument. Abort using `throw', since
3822 ;; it's useless to try to find a surrounding arglist
3824 (throw 'angle-bracket-arglist-escape nil))))))
3827 (or c-record-found-types t)))))
3829 (defun c-forward-name ()
3830 ;; Move forward over a complete name if at the beginning of one,
3831 ;; stopping at the next following token. If the point is not at
3832 ;; something that are recognized as name then it stays put. A name
3833 ;; could be something as simple as "foo" in C or something as
3834 ;; complex as "X<Y<class A<int>::B, BIT_MAX >> b>, ::operator<> ::
3835 ;; Z<(a>b)> :: operator const X<&foo>::T Q::G<unsigned short
3836 ;; int>::*volatile const" in C++ (this function is actually little
3837 ;; more than a `looking-at' call in all modes except those that,
3838 ;; like C++, have `c-recognize-<>-arglists' set). Return nil if no
3839 ;; name is found, 'template if it's an identifier ending with an
3840 ;; angle bracket arglist, 'operator of it's an operator identifier,
3841 ;; or t if it's some other kind of name.
3843 (let ((pos (point)) res id-start id-end
3844 ;; Turn off `c-promote-possible-types' here since we might
3845 ;; call `c-forward-<>-arglist' and we don't want it to promote
3846 ;; every suspect thing in the arglist to a type. We're
3847 ;; typically called from `c-forward-type' in this case, and
3848 ;; the caller only wants the top level type that it finds to
3850 c-promote-possible-types)
3853 (looking-at c-identifier-key)
3856 ;; Check for keyword. We go to the last symbol in
3857 ;; `c-identifier-key' first.
3858 (if (eq c-identifier-key c-symbol-key)
3859 (setq id-start (point)
3860 id-end (match-end 0))
3861 (goto-char (setq id-end (match-end 0)))
3862 (c-simple-skip-symbol-backward)
3863 (setq id-start (point)))
3865 (if (looking-at c-keywords-regexp)
3866 (when (and (c-major-mode-is 'c++-mode)
3868 (cc-eval-when-compile
3869 (concat "\\(operator\\|\\(template\\)\\)"
3870 "\\(" (c-lang-const c-nonsymbol-key c++)
3872 (if (match-beginning 2)
3873 ;; "template" is only valid inside an
3874 ;; identifier if preceded by "::".
3876 (c-backward-syntactic-ws)
3877 (and (c-safe (backward-char 2) t)
3881 ;; Handle a C++ operator or template identifier.
3883 (c-forward-syntactic-ws)
3884 (cond ((eq (char-before id-end) ?e)
3885 ;; Got "... ::template".
3886 (let ((subres (c-forward-name)))
3891 ((looking-at c-identifier-start)
3892 ;; Got a cast operator.
3893 (when (c-forward-type)
3896 ;; Now we should match a sequence of either
3897 ;; '*', '&' or a name followed by ":: *",
3898 ;; where each can be followed by a sequence
3899 ;; of `c-opt-type-modifier-key'.
3900 (while (cond ((looking-at "[*&]")
3901 (goto-char (match-end 0))
3903 ((looking-at c-identifier-start)
3904 (and (c-forward-name)
3907 (goto-char (match-end 0))
3908 (c-forward-syntactic-ws)
3909 (eq (char-after) ?*))
3914 (c-forward-syntactic-ws)
3916 (looking-at c-opt-type-modifier-key))
3917 (goto-char (match-end 1))))))
3919 ((looking-at c-overloadable-operators-regexp)
3920 ;; Got some other operator.
3921 (when c-record-type-identifiers
3922 (setq c-last-identifier-range
3923 (cons (point) (match-end 0))))
3924 (goto-char (match-end 0))
3925 (c-forward-syntactic-ws)
3931 (when c-record-type-identifiers
3932 (setq c-last-identifier-range
3933 (cons id-start id-end)))
3935 (c-forward-syntactic-ws)
3941 (when (or c-opt-identifier-concat-key
3942 c-recognize-<>-arglists)
3945 ((and c-opt-identifier-concat-key
3946 (looking-at c-opt-identifier-concat-key))
3947 ;; Got a concatenated identifier. This handles the
3948 ;; cases with tricky syntactic whitespace that aren't
3949 ;; covered in `c-identifier-key'.
3950 (goto-char (match-end 0))
3951 (c-forward-syntactic-ws)
3954 ((and c-recognize-<>-arglists
3955 (eq (char-after) ?<))
3956 ;; Maybe an angle bracket arglist.
3957 (when (let ((c-record-type-identifiers nil)
3958 (c-record-found-types nil))
3959 (c-forward-<>-arglist
3960 nil c-disallow-comma-in-<>-arglists))
3961 (c-forward-syntactic-ws)
3963 (if (and c-opt-identifier-concat-key
3964 (looking-at c-opt-identifier-concat-key))
3965 ;; Continue if there's an identifier concatenation
3966 ;; operator after the template argument.
3968 (when c-record-type-identifiers
3969 (c-record-ref-id (cons id-start id-end))
3970 (setq c-last-identifier-range nil))
3972 (c-forward-syntactic-ws)
3974 ;; `c-add-type' isn't called here since we don't
3975 ;; want to add types containing angle bracket
3977 (when c-record-type-identifiers
3978 (c-record-type-id (cons id-start id-end))
3979 (setq c-last-identifier-range nil))
3980 (setq res 'template)
3987 (defun c-forward-type ()
3988 ;; Move forward over a type spec if at the beginning of one,
3989 ;; stopping at the next following token. Return t if it's a known
3990 ;; type that can't be a name, 'known if it's an otherwise known type
3991 ;; (according to `*-font-lock-extra-types'), 'prefix if it's a known
3992 ;; prefix of a type, 'found if it's a type that matches one in
3993 ;; `c-found-types', 'maybe if it's an identfier that might be a
3994 ;; type, or nil if it can't be a type (the point isn't moved then).
3995 ;; The point is assumed to be at the beginning of a token.
3997 ;; Note that this function doesn't skip past the brace definition
3998 ;; that might be considered part of the type, e.g.
3999 ;; "enum {a, b, c} foo".
4000 (let ((start (point)) pos res res2 id-start id-end id-range)
4002 ;; Skip leading type modifiers. If any are found we know it's a
4003 ;; prefix of a type.
4004 (when c-opt-type-modifier-key
4005 (while (looking-at c-opt-type-modifier-key)
4006 (goto-char (match-end 1))
4007 (c-forward-syntactic-ws)
4008 (setq res 'prefix)))
4011 ((looking-at c-type-prefix-key)
4012 ;; Looking at a keyword that prefixes a type identifier,
4014 (goto-char (match-end 1))
4015 (c-forward-syntactic-ws)
4017 (if (memq (setq res2 (c-forward-name)) '(t template))
4020 ;; In many languages the name can be used without the
4021 ;; prefix, so we add it to `c-found-types'.
4022 (c-add-type pos (point))
4023 (when c-record-type-identifiers
4024 (c-record-type-id c-last-identifier-range)))
4032 (if (looking-at c-identifier-start)
4034 (setq id-start (point)
4035 res2 (c-forward-name))
4037 (setq id-end (point)
4038 id-range c-last-identifier-range))))
4039 (and (cond ((looking-at c-primitive-type-key)
4041 ((c-with-syntax-table c-identifier-syntax-table
4042 (looking-at c-known-type-key))
4047 (goto-char (match-end 1))
4048 (c-forward-syntactic-ws)
4049 (setq pos (point))))
4052 ;; Looking at a primitive or known type identifier. We've
4053 ;; checked for a name first so that we don't go here if the
4054 ;; known type match only is a prefix of another name.
4056 (setq id-end (match-end 1))
4058 (when (and c-record-type-identifiers
4059 (or c-promote-possible-types (eq res t)))
4060 (c-record-type-id (cons (match-beginning 1) (match-end 1))))
4062 (if (and c-opt-type-component-key
4064 (looking-at c-opt-type-component-key)))
4065 ;; There might be more keywords for the type.
4067 (c-forward-keyword-clause)
4069 (setq safe-pos (point))
4070 (looking-at c-opt-type-component-key))
4071 (when (and c-record-type-identifiers
4072 (looking-at c-primitive-type-key))
4073 (c-record-type-id (cons (match-beginning 1)
4075 (c-forward-keyword-clause))
4076 (if (looking-at c-primitive-type-key)
4078 (when c-record-type-identifiers
4079 (c-record-type-id (cons (match-beginning 1)
4081 (c-forward-keyword-clause)
4083 (goto-char safe-pos)
4084 (setq res 'prefix)))
4085 (unless (save-match-data (c-forward-keyword-clause))
4088 (goto-char (match-end 1))
4089 (c-forward-syntactic-ws)))))
4093 ;; A normal identifier.
4095 (if (or res c-promote-possible-types)
4097 (c-add-type id-start id-end)
4098 (when c-record-type-identifiers
4099 (c-record-type-id id-range))
4102 (setq res (if (c-check-type id-start id-end)
4103 ;; It's an identifier that has been used as
4104 ;; a type somewhere else.
4106 ;; It's an identifier that might be a type.
4108 ((eq res2 'template)
4109 ;; A template is a type.
4113 ;; Otherwise it's an operator identifier, which is not a type.
4118 ;; Skip trailing type modifiers. If any are found we know it's
4120 (when c-opt-type-modifier-key
4121 (while (looking-at c-opt-type-modifier-key)
4122 (goto-char (match-end 1))
4123 (c-forward-syntactic-ws)
4126 ;; Step over any type suffix operator. Do not let the existence
4127 ;; of these alter the classification of the found type, since
4128 ;; these operators typically are allowed in normal expressions
4130 (when c-opt-type-suffix-key
4131 (while (looking-at c-opt-type-suffix-key)
4132 (goto-char (match-end 1))
4133 (c-forward-syntactic-ws)))
4135 (when c-opt-type-concat-key
4136 ;; Look for a trailing operator that concatenate the type with
4137 ;; a following one, and if so step past that one through a
4140 (let* ((c-promote-possible-types (or (memq res '(t known))
4141 c-promote-possible-types))
4142 ;; If we can't promote then set `c-record-found-types' so that
4143 ;; we can merge in the types from the second part afterwards if
4144 ;; it turns out to be a known type there.
4145 (c-record-found-types (and c-record-type-identifiers
4146 (not c-promote-possible-types))))
4147 (if (and (looking-at c-opt-type-concat-key)
4150 (goto-char (match-end 1))
4151 (c-forward-syntactic-ws)
4152 (setq res2 (c-forward-type))))
4155 ;; If either operand certainly is a type then both are, but we
4156 ;; don't let the existence of the operator itself promote two
4157 ;; uncertain types to a certain one.
4159 ((or (eq res 'known) (memq res2 '(t known)))
4160 (c-add-type id-start id-end)
4161 (when c-record-type-identifiers
4162 (c-record-type-id id-range))
4170 (when (and (eq res t)
4171 (consp c-record-found-types))
4172 ;; Merge in the ranges of any types found by the second
4173 ;; `c-forward-type'.
4174 (setq c-record-type-identifiers
4175 ;; `nconc' doesn't mind that the tail of
4176 ;; `c-record-found-types' is t.
4177 (nconc c-record-found-types
4178 c-record-type-identifiers))))
4182 (when (and c-record-found-types (memq res '(known found)) id-range)
4183 (setq c-record-found-types
4184 (cons id-range c-record-found-types))))
4186 ;;(message "c-forward-type %s -> %s: %s" start (point) res)
4191 ;; Handling of large scale constructs like statements and declarations.
4193 (defun c-beginning-of-inheritance-list (&optional lim)
4194 ;; Go to the first non-whitespace after the colon that starts a
4195 ;; multiple inheritance introduction. Optional LIM is the farthest
4196 ;; back we should search.
4197 (let* ((lim (or lim (save-excursion
4198 (c-beginning-of-syntax)
4200 (c-with-syntax-table c++-template-syntax-table
4201 (c-backward-token-2 0 t lim)
4202 (while (and (or (looking-at c-symbol-start)
4203 (looking-at "[<,]"))
4204 (zerop (c-backward-token-2 1 t lim))))
4205 (skip-chars-forward "^:"))))
4207 (defun c-in-method-def-p ()
4208 ;; Return nil if we aren't in a method definition, otherwise the
4209 ;; position of the initial [+-].
4212 (and c-opt-method-key
4213 (looking-at c-opt-method-key)
4217 ;; Contributed by Kevin Ryde <user42@zip.com.au>.
4218 (defun c-in-gcc-asm-p ()
4219 ;; Return non-nil if point is within a gcc \"asm\" block.
4221 ;; This should be called with point inside an argument list.
4223 ;; Only one level of enclosing parentheses is considered, so for
4224 ;; instance `nil' is returned when in a function call within an asm
4227 (and c-opt-asm-stmt-key
4230 (backward-up-list 1)
4231 (c-beginning-of-statement-1 (point-min) nil t)
4232 (looking-at c-opt-asm-stmt-key))))
4234 (defun c-at-toplevel-p ()
4235 "Return a determination as to whether point is at the `top-level'.
4236 Being at the top-level means that point is either outside any
4237 enclosing block (such function definition), or only inside a class,
4238 namespace or other block that contains another declaration level.
4240 If point is not at the top-level (e.g. it is inside a method
4241 definition), then nil is returned. Otherwise, if point is at a
4242 top-level not enclosed within a class definition, t is returned.
4243 Otherwise, a 2-vector is returned where the zeroth element is the
4244 buffer position of the start of the class declaration, and the first
4245 element is the buffer position of the enclosing class's opening
4247 (let ((paren-state (c-parse-state)))
4248 (or (not (c-most-enclosing-brace paren-state))
4249 (c-search-uplist-for-classkey paren-state))))
4251 (defun c-just-after-func-arglist-p (&optional lim)
4252 ;; Return t if we are between a function's argument list closing
4253 ;; paren and its opening brace. Note that the list close brace
4254 ;; could be followed by a "const" specifier or a member init hanging
4255 ;; colon. LIM is used as bound for some backward buffer searches;
4256 ;; the search might continue past it.
4258 ;; Note: This test is easily fooled. It only works reasonably well
4259 ;; in the situations where `c-guess-basic-syntax' uses it.
4261 (if (c-mode-is-new-awk-p)
4262 (c-awk-backward-syntactic-ws lim)
4263 (c-backward-syntactic-ws lim))
4264 (let ((checkpoint (point)))
4265 ;; could be looking at const specifier
4266 (if (and (eq (char-before) ?t)
4268 (looking-at "\\<const\\>[^_]"))
4269 (c-backward-syntactic-ws lim)
4270 ;; otherwise, we could be looking at a hanging member init
4272 (goto-char checkpoint)
4273 (while (eq (char-before) ?,)
4274 ;; this will catch member inits with multiple
4277 (c-backward-syntactic-ws (c-point 'bol))
4278 (if (eq (char-before) ?\))
4280 (c-backward-sexp 1))
4281 (c-backward-syntactic-ws lim))
4282 (if (and (eq (char-before) ?:)
4285 (c-backward-syntactic-ws lim)
4286 (looking-at "\\([ \t\n]\\|\\\\\n\\)*:\\([^:]+\\|$\\)")))
4288 (goto-char checkpoint))
4290 (setq checkpoint (point))
4291 (and (eq (char-before) ?\))
4292 ;; Check that it isn't a cpp expression, e.g. the
4293 ;; expression of an #if directive or the "function header"
4295 (or (not (c-beginning-of-macro))
4296 (and (c-forward-to-cpp-define-body)
4297 (< (point) checkpoint)))
4298 ;; check if we are looking at an ObjC method def
4299 (or (not c-opt-method-key)
4301 (goto-char checkpoint)
4304 (c-backward-syntactic-ws lim)
4305 (not (or (memq (char-before) '(?- ?+))
4306 ;; or a class category
4309 (looking-at c-class-key))
4313 (defun c-in-knr-argdecl (&optional lim)
4314 ;; Return the position of the first argument declaration if point is
4315 ;; inside a K&R style argument declaration list, nil otherwise.
4316 ;; `c-recognize-knr-p' is not checked. If LIM is non-nil, it's a
4317 ;; position that bounds the backward search for the argument list.
4319 ;; Note: A declaration level context is assumed; the test can return
4320 ;; false positives for statements. This test is even more easily
4321 ;; fooled than `c-just-after-func-arglist-p'.
4326 ;; Go back to the closest preceding normal parenthesis sexp. We
4327 ;; take that as the argument list in the function header. Then
4328 ;; check that it's followed by some symbol before the next ';'
4329 ;; or '{'. If it does, it's the header of the K&R argdecl we're
4331 (if lim (narrow-to-region lim (point)))
4332 (let ((outside-macro (not (c-query-macro-start)))
4336 (while (if (and (c-safe (setq paren-end
4337 (c-down-list-backward (point))))
4338 (eq (char-after paren-end) ?\)))
4340 (goto-char (1+ paren-end))
4342 (c-beginning-of-macro)))
4343 (throw 'done nil))))
4346 (c-forward-syntactic-ws)
4347 (looking-at "\\w\\|\\s_"))
4348 (c-safe (c-up-list-backward paren-end))
4351 ;; If it's a K&R declaration then we're now at the
4352 ;; beginning of the function arglist. Check that there
4353 ;; isn't a '=' before it in this statement since that
4354 ;; means it some kind of initialization instead.
4355 (c-syntactic-skip-backward "^;=}{")
4356 (not (eq (char-before) ?=)))
4360 (defun c-skip-conditional ()
4361 ;; skip forward over conditional at point, including any predicate
4362 ;; statements in parentheses. No error checking is performed.
4363 (c-forward-sexp (cond
4365 ((looking-at (concat "\\<else"
4366 "\\([ \t\n]\\|\\\\\n\\)+"
4367 "if\\>\\([^_]\\|$\\)"))
4369 ;; do, else, try, finally
4370 ((looking-at (concat "\\<\\("
4371 "do\\|else\\|try\\|finally"
4372 "\\)\\>\\([^_]\\|$\\)"))
4374 ;; for, if, while, switch, catch, synchronized, foreach
4377 (defun c-after-conditional (&optional lim)
4378 ;; If looking at the token after a conditional then return the
4379 ;; position of its start, otherwise return nil.
4381 (and (zerop (c-backward-token-2 1 t lim))
4382 (or (looking-at c-block-stmt-1-key)
4383 (and (eq (char-after) ?\()
4384 (zerop (c-backward-token-2 1 t lim))
4385 (looking-at c-block-stmt-2-key)))
4388 (defsubst c-backward-to-block-anchor (&optional lim)
4389 ;; Assuming point is at a brace that opens a statement block of some
4390 ;; kind, move to the proper anchor point for that block. It might
4391 ;; need to be adjusted further by c-add-stmt-syntax, but the
4392 ;; position at return is suitable as start position for that
4394 (unless (= (point) (c-point 'boi))
4395 (let ((start (c-after-conditional lim)))
4397 (goto-char start)))))
4399 (defun c-backward-to-decl-anchor (&optional lim)
4400 ;; Assuming point is at a brace that opens the block of a top level
4401 ;; declaration of some kind, move to the proper anchor point for
4403 (unless (= (point) (c-point 'boi))
4404 ;; What we have below is actually an extremely stripped variant of
4405 ;; c-beginning-of-statement-1.
4406 (let ((pos (point)))
4407 ;; Switch syntax table to avoid stopping at line continuations.
4409 (if lim (narrow-to-region lim (point-max)))
4411 (c-backward-syntactic-ws)
4412 (c-safe (goto-char (scan-sexps (point) -1)) t))
4413 (not (c-crosses-statement-barrier-p (point) pos)))
4417 (defsubst c-search-decl-header-end ()
4418 ;; Search forward for the end of the "header" of the current
4419 ;; declaration. That's the position where the definition body
4420 ;; starts, or the first variable initializer, or the ending
4421 ;; semicolon. I.e. search forward for the closest following
4422 ;; (syntactically relevant) '{', '=' or ';' token. Point is left
4423 ;; _after_ the first found token, or at point-max if none is found.
4424 (if (c-major-mode-is 'c++-mode)
4425 ;; In C++ we need to take special care to handle those pesky
4426 ;; template brackets.
4427 (while (and (c-syntactic-re-search-forward "[;{=<]" nil 'move t)
4428 (when (eq (char-before) ?<)
4429 (c-with-syntax-table c++-template-syntax-table
4430 (if (c-safe (goto-char (c-up-list-forward (point))))
4432 (goto-char (point-max))
4434 (c-syntactic-re-search-forward "[;{=]" nil 'move t t)))
4436 (defun c-beginning-of-decl-1 (&optional lim)
4437 ;; Go to the beginning of the current declaration, or the beginning
4438 ;; of the previous one if already at the start of it. Point won't
4439 ;; be moved out of any surrounding paren. Return a cons cell on the
4440 ;; form (MOVE . KNR-POS). MOVE is like the return value from
4441 ;; `c-beginning-of-statement-1'. If point skipped over some K&R
4442 ;; style argument declarations (and they are to be recognized) then
4443 ;; KNR-POS is set to the start of the first such argument
4444 ;; declaration, otherwise KNR-POS is nil. If LIM is non-nil, it's a
4445 ;; position that bounds the backward search.
4447 ;; NB: Cases where the declaration continues after the block, as in
4448 ;; "struct foo { ... } bar;", are currently recognized as two
4449 ;; declarations, e.g. "struct foo { ... }" and "bar;" in this case.
4451 (let* ((start (point))
4452 (last-stmt-start (point))
4453 (move (c-beginning-of-statement-1 lim t t)))
4455 ;; `c-beginning-of-statement-1' stops at a block start, but we
4456 ;; want to continue if the block doesn't begin a top level
4457 ;; construct, i.e. if it isn't preceded by ';', '}', ':', or bob.
4458 (let ((beg (point)) tentative-move)
4460 ;; Must check with c-opt-method-key in ObjC mode.
4461 (not (and c-opt-method-key
4462 (looking-at c-opt-method-key)))
4463 (/= last-stmt-start (point))
4465 (c-backward-syntactic-ws lim)
4466 (not (memq (char-before) '(?\; ?} ?: nil))))
4467 ;; Check that we don't move from the first thing in a
4468 ;; macro to its header.
4469 (not (eq (setq tentative-move
4470 (c-beginning-of-statement-1 lim t t))
4472 (setq last-stmt-start beg
4474 move tentative-move))
4477 (when c-recognize-knr-p
4478 (let ((fallback-pos (point)) knr-argdecl-start)
4479 ;; Handle K&R argdecls. Back up after the "statement" jumped
4480 ;; over by `c-beginning-of-statement-1', unless it was the
4481 ;; function body, in which case we're sitting on the opening
4482 ;; brace now. Then test if we're in a K&R argdecl region and
4483 ;; that we started at the other side of the first argdecl in
4485 (unless (eq (char-after) ?{)
4486 (goto-char last-stmt-start))
4487 (if (and (setq knr-argdecl-start (c-in-knr-argdecl lim))
4488 (< knr-argdecl-start start)
4490 (goto-char knr-argdecl-start)
4491 (not (eq (c-beginning-of-statement-1 lim t t) 'macro))))
4493 (cons (if (eq (char-after fallback-pos) ?{)
4497 (goto-char fallback-pos))))
4499 (when c-opt-access-key
4500 ;; Might have ended up before a protection label. This should
4501 ;; perhaps be checked before `c-recognize-knr-p' to be really
4502 ;; accurate, but we know that no language has both.
4503 (while (looking-at c-opt-access-key)
4504 (goto-char (match-end 0))
4505 (c-forward-syntactic-ws)
4506 (when (>= (point) start)
4508 (throw 'return (cons 'same nil)))))
4510 ;; `c-beginning-of-statement-1' counts each brace block as a
4511 ;; separate statement, so the result will be 'previous if we've
4512 ;; moved over any. If they were brace list initializers we might
4513 ;; not have moved over a declaration boundary though, so change it
4514 ;; to 'same if we've moved past a '=' before '{', but not ';'.
4515 ;; (This ought to be integrated into `c-beginning-of-statement-1',
4516 ;; so we avoid this extra pass which potentially can search over a
4517 ;; large amount of text.)
4518 (if (and (eq move 'previous)
4519 (c-with-syntax-table (if (c-major-mode-is 'c++-mode)
4520 c++-template-syntax-table
4523 (and (c-syntactic-re-search-forward "[;={]" start t t t)
4524 (eq (char-before) ?=)
4525 (c-syntactic-re-search-forward "[;{]" start t t)
4526 (eq (char-before) ?{)
4527 (c-safe (goto-char (c-up-list-forward (point))) t)
4528 (not (c-syntactic-re-search-forward ";" start t t))))))
4532 (defun c-end-of-decl-1 ()
4533 ;; Assuming point is at the start of a declaration (as detected by
4534 ;; e.g. `c-beginning-of-decl-1'), go to the end of it. Unlike
4535 ;; `c-beginning-of-decl-1', this function handles the case when a
4536 ;; block is followed by identifiers in e.g. struct declarations in C
4537 ;; or C++. If a proper end was found then t is returned, otherwise
4538 ;; point is moved as far as possible within the current sexp and nil
4539 ;; is returned. This function doesn't handle macros; use
4540 ;; `c-end-of-macro' instead in those cases.
4541 (let ((start (point))
4542 (decl-syntax-table (if (c-major-mode-is 'c++-mode)
4543 c++-template-syntax-table
4546 (c-search-decl-header-end)
4548 (when (and c-recognize-knr-p
4549 (eq (char-before) ?\;)
4550 (c-in-knr-argdecl start))
4551 ;; Stopped at the ';' in a K&R argdecl section which is
4552 ;; detected using the same criteria as in
4553 ;; `c-beginning-of-decl-1'. Move to the following block
4555 (c-syntactic-re-search-forward "{" nil 'move t))
4557 (when (eq (char-before) ?{)
4558 ;; Encountered a block in the declaration. Jump over it.
4560 (goto-char (c-up-list-forward (point)))
4561 (error (goto-char (point-max))
4562 (throw 'return nil)))
4563 (if (or (not c-opt-block-decls-with-vars-key)
4565 (c-with-syntax-table decl-syntax-table
4566 (let ((lim (point)))
4569 ;; Check for `c-opt-block-decls-with-vars-key'
4570 ;; before the first paren.
4571 (c-syntactic-re-search-forward
4572 (concat "[;=\(\[{]\\|\\("
4573 c-opt-block-decls-with-vars-key
4577 (not (eq (char-before) ?_))
4578 ;; Check that the first following paren is
4580 (c-syntactic-re-search-forward "[;=\(\[{]"
4582 (eq (char-before) ?{)))))))
4583 ;; The declaration doesn't have any of the
4584 ;; `c-opt-block-decls-with-vars' keywords in the
4585 ;; beginning, so it ends here at the end of the block.
4588 (c-with-syntax-table decl-syntax-table
4590 (if (eq (char-before) ?\;)
4592 (c-syntactic-re-search-forward ";" nil 'move t))))
4595 (defun c-beginning-of-member-init-list (&optional limit)
4596 ;; Goes to the beginning of a member init list (i.e. just after the
4597 ;; ':') if inside one. Returns t in that case, nil otherwise.
4599 (setq limit (point-min)))
4600 (skip-chars-forward " \t")
4602 (if (eq (char-after) ?,)
4604 (c-backward-syntactic-ws limit))
4607 (while (and (< limit (point))
4608 (eq (char-before) ?,))
4610 ;; this will catch member inits with multiple
4613 (c-backward-syntactic-ws limit)
4614 (if (eq (char-before) ?\))
4615 (unless (c-safe (c-backward-sexp 1))
4617 (c-backward-syntactic-ws limit)
4619 ;; Skip over any template arg to the class. This way with a
4620 ;; syntax table is bogus but it'll have to do for now.
4621 (if (and (eq (char-before) ?>)
4622 (c-major-mode-is 'c++-mode))
4623 (c-with-syntax-table c++-template-syntax-table
4624 (unless (c-safe (c-backward-sexp 1))
4625 (throw 'exit nil))))
4626 (c-safe (c-backward-sexp 1))
4627 (c-backward-syntactic-ws limit)
4629 ;; Skip backwards over a fully::qualified::name.
4630 (while (and (eq (char-before) ?:)
4633 (eq (char-before) ?:)))
4635 (c-safe (c-backward-sexp 1)))
4637 ;; If we've stepped over a number then this is a bitfield.
4638 (when (and c-opt-bitfield-key
4639 (looking-at "[0-9]"))
4642 ;; now continue checking
4643 (c-backward-syntactic-ws limit))
4645 (and (< limit (point))
4646 (eq (char-before) ?:))))
4648 (defun c-search-uplist-for-classkey (paren-state)
4649 ;; search for the containing class, returning a 2 element vector if
4650 ;; found. aref 0 contains the bufpos of the boi of the class key
4651 ;; line, and aref 1 contains the bufpos of the open brace.
4652 (if (null paren-state)
4653 ;; no paren-state means we cannot be inside a class
4655 (let ((carcache (car paren-state))
4656 search-start search-end)
4657 (if (consp carcache)
4658 ;; a cons cell in the first element means that there is some
4659 ;; balanced sexp before the current bufpos. this we can
4660 ;; ignore. the nth 1 and nth 2 elements define for us the
4661 ;; search boundaries
4662 (setq search-start (nth 2 paren-state)
4663 search-end (nth 1 paren-state))
4664 ;; if the car was not a cons cell then nth 0 and nth 1 define
4665 ;; for us the search boundaries
4666 (setq search-start (nth 1 paren-state)
4667 search-end (nth 0 paren-state)))
4668 ;; if search-end is nil, or if the search-end character isn't an
4669 ;; open brace, we are definitely not in a class
4670 (if (or (not search-end)
4671 (< search-end (point-min))
4672 (not (eq (char-after search-end) ?{)))
4674 ;; now, we need to look more closely at search-start. if
4675 ;; search-start is nil, then our start boundary is really
4677 (if (not search-start)
4678 (setq search-start (point-min))
4679 ;; if search-start is a cons cell, then we can start
4680 ;; searching from the end of the balanced sexp just ahead of
4682 (if (consp search-start)
4683 (setq search-start (cdr search-start))
4684 ;; Otherwise we start searching within the surrounding paren sexp.
4685 (setq search-start (1+ search-start))))
4686 ;; now we can do a quick regexp search from search-start to
4687 ;; search-end and see if we can find a class key. watch for
4688 ;; class like strings in literals
4691 (goto-char search-start)
4692 (let (foundp class match-end)
4693 (while (and (not foundp)
4695 (c-forward-syntactic-ws search-end)
4696 (> search-end (point)))
4697 ;; Add one to the search limit, to allow
4698 ;; matching of the "{" in the regexp.
4699 (re-search-forward c-decl-block-key
4702 (setq class (match-beginning 0)
4703 match-end (match-end 0))
4705 (if (c-in-literal search-start)
4706 (goto-char match-end) ; its in a comment or string, ignore
4708 (setq foundp (vector (c-point 'boi) search-end))
4710 ;; check for embedded keywords
4711 ((let ((char (char-after (1- class))))
4713 (memq (char-syntax char) '(?w ?_))))
4714 (goto-char match-end)
4716 ;; make sure we're really looking at the start of a
4717 ;; class definition, and not an ObjC method.
4718 ((and c-opt-method-key
4719 (re-search-forward c-opt-method-key search-end t)
4720 (not (c-in-literal class)))
4722 ;; Check if this is an anonymous inner class.
4723 ((and c-opt-inexpr-class-key
4724 (looking-at c-opt-inexpr-class-key))
4725 (while (and (zerop (c-forward-token-2 1 t))
4726 (looking-at "(\\|\\w\\|\\s_\\|\\.")))
4727 (if (eq (point) search-end)
4728 ;; We're done. Just trap this case in the cond.
4730 ;; False alarm; all conditions aren't satisfied.
4732 ;; Its impossible to define a regexp for this, and
4733 ;; nearly so to do it programmatically.
4735 ;; ; picks up forward decls
4736 ;; = picks up init lists
4737 ;; ) picks up return types
4738 ;; > picks up templates, but remember that we can
4739 ;; inherit from templates!
4740 ((let ((skipchars "^;=)"))
4741 ;; try to see if we found the `class' keyword
4742 ;; inside a template arg list
4744 (skip-chars-backward "^<>" search-start)
4745 (if (eq (char-before) ?<)
4746 (setq skipchars (concat skipchars ">"))))
4748 (skip-chars-forward skipchars search-end)
4749 (c-in-literal class))
4751 (/= (point) search-end))
4757 (defun c-inside-bracelist-p (containing-sexp paren-state)
4758 ;; return the buffer position of the beginning of the brace list
4759 ;; statement if we're inside a brace list, otherwise return nil.
4760 ;; CONTAINING-SEXP is the buffer pos of the innermost containing
4761 ;; paren. BRACE-STATE is the remainder of the state of enclosing
4764 ;; N.B.: This algorithm can potentially get confused by cpp macros
4765 ;; places in inconvenient locations. Its a trade-off we make for
4768 ;; This will pick up brace list declarations.
4771 (goto-char containing-sexp)
4774 (if (and (or (looking-at c-brace-list-key)
4775 (progn (c-forward-sexp -1)
4776 (looking-at c-brace-list-key)))
4777 (setq bracepos (c-down-list-forward (point)))
4778 (not (c-crosses-statement-barrier-p (point)
4781 ;; this will pick up array/aggregate init lists, even if they are nested.
4784 ;; Pike can have class definitions anywhere, so we must
4785 ;; check for the class key here.
4786 (and (c-major-mode-is 'pike-mode)
4788 bufpos braceassignp lim next-containing)
4789 (while (and (not bufpos)
4792 (if (consp (car paren-state))
4793 (setq lim (cdr (car paren-state))
4794 paren-state (cdr paren-state))
4795 (setq lim (car paren-state)))
4797 (setq next-containing (car paren-state)
4798 paren-state (cdr paren-state))))
4799 (goto-char containing-sexp)
4800 (if (c-looking-at-inexpr-block next-containing next-containing)
4801 ;; We're in an in-expression block of some kind. Do not
4802 ;; check nesting. We deliberately set the limit to the
4803 ;; containing sexp, so that c-looking-at-inexpr-block
4804 ;; doesn't check for an identifier before it.
4805 (setq containing-sexp nil)
4806 ;; see if the open brace is preceded by = or [...] in
4807 ;; this statement, but watch out for operator=
4808 (setq braceassignp 'dontknow)
4809 (c-backward-token-2 1 t lim)
4810 ;; Checks to do only on the first sexp before the brace.
4811 (when (and c-opt-inexpr-brace-list-key
4812 (eq (char-after) ?\[))
4813 ;; In Java, an initialization brace list may follow
4814 ;; directly after "new Foo[]", so check for a "new"
4816 (while (eq braceassignp 'dontknow)
4818 (cond ((/= (c-backward-token-2 1 t lim) 0) nil)
4819 ((looking-at c-opt-inexpr-brace-list-key) t)
4820 ((looking-at "\\sw\\|\\s_\\|[.[]")
4821 ;; Carry on looking if this is an
4822 ;; identifier (may contain "." in Java)
4823 ;; or another "[]" sexp.
4826 ;; Checks to do on all sexps before the brace, up to the
4827 ;; beginning of the statement.
4828 (while (eq braceassignp 'dontknow)
4829 (cond ((eq (char-after) ?\;)
4830 (setq braceassignp nil))
4832 (looking-at class-key))
4833 (setq braceassignp nil))
4834 ((eq (char-after) ?=)
4835 ;; We've seen a =, but must check earlier tokens so
4836 ;; that it isn't something that should be ignored.
4837 (setq braceassignp 'maybe)
4838 (while (and (eq braceassignp 'maybe)
4839 (zerop (c-backward-token-2 1 t lim)))
4842 ;; Check for operator =
4843 ((looking-at "operator\\>[^_]") nil)
4844 ;; Check for `<opchar>= in Pike.
4845 ((and (c-major-mode-is 'pike-mode)
4846 (or (eq (char-after) ?`)
4847 ;; Special case for Pikes
4848 ;; `[]=, since '[' is not in
4849 ;; the punctuation class.
4850 (and (eq (char-after) ?\[)
4851 (eq (char-before) ?`))))
4853 ((looking-at "\\s.") 'maybe)
4854 ;; make sure we're not in a C++ template
4855 ;; argument assignment
4857 (c-major-mode-is 'c++-mode)
4859 (let ((here (point))
4861 (skip-chars-backward "^<>")
4863 (and (eq (char-before) ?<)
4864 (not (c-crosses-statement-barrier-p
4866 (not (c-in-literal))
4870 (if (and (eq braceassignp 'dontknow)
4871 (/= (c-backward-token-2 1 t lim) 0))
4872 (setq braceassignp nil)))
4873 (if (not braceassignp)
4874 (if (eq (char-after) ?\;)
4875 ;; Brace lists can't contain a semicolon, so we're done.
4876 (setq containing-sexp nil)
4878 (setq containing-sexp next-containing
4880 next-containing nil))
4881 ;; we've hit the beginning of the aggregate list
4882 (c-beginning-of-statement-1
4883 (c-most-enclosing-brace paren-state))
4884 (setq bufpos (point))))
4889 (defun c-looking-at-special-brace-list (&optional lim)
4890 ;; If we're looking at the start of a pike-style list, ie `({Â })',
4891 ;; `([Â ])', `(<Â >)' etc, a cons of a cons of its starting and ending
4892 ;; positions and its entry in c-special-brace-lists is returned, nil
4893 ;; otherwise. The ending position is nil if the list is still open.
4894 ;; LIM is the limit for forward search. The point may either be at
4895 ;; the `(' or at the following paren character. Tries to check the
4896 ;; matching closer, but assumes it's correct if no balanced paren is
4897 ;; found (i.e. the case `({ ... } ... )' is detected as _not_ being
4898 ;; a special brace list).
4899 (if c-special-brace-lists
4904 (c-forward-syntactic-ws)
4905 (if (eq (char-after) ?\()
4908 (c-forward-syntactic-ws)
4909 (setq type (assq (char-after) c-special-brace-lists)))
4910 (if (setq type (assq (char-after) c-special-brace-lists))
4912 (c-backward-syntactic-ws)
4914 (setq beg (if (eq (char-after) ?\()
4918 (if (and (c-safe (goto-char beg)
4921 (= (char-before) ?\)))
4922 (c-safe (goto-char beg)
4925 ;; Kludges needed to handle inner
4926 ;; chars both with and without
4928 (or (/= (char-syntax (char-before)) ?\))
4929 (= (char-before) (cdr type)))))
4930 (if (or (/= (char-syntax (char-before)) ?\))
4932 (c-forward-syntactic-ws)
4935 (cons (cons beg end) type))
4936 (cons (list beg) type)))))
4939 (defun c-looking-at-bos (&optional lim)
4940 ;; Return non-nil if between two statements or declarations, assuming
4941 ;; point is not inside a literal or comment.
4943 (c-backward-syntactic-ws lim)
4945 ;; Return t if at the start inside some parenthesis expression
4946 ;; too, to catch macros that have statements as arguments.
4947 (memq (char-before) '(?\; ?} ?\())
4948 (and (eq (char-before) ?{)
4949 (not (and c-special-brace-lists
4950 (progn (backward-char)
4951 (c-looking-at-special-brace-list))))))))
4953 (defun c-looking-at-inexpr-block (lim containing-sexp)
4954 ;; Returns non-nil if we're looking at the beginning of a block
4955 ;; inside an expression. The value returned is actually a cons of
4956 ;; either 'inlambda, 'inexpr-statement or 'inexpr-class and the
4957 ;; position of the beginning of the construct. LIM limits the
4958 ;; backward search. CONTAINING-SEXP is the start position of the
4959 ;; closest containing list. If it's nil, the containing paren isn't
4960 ;; used to decide whether we're inside an expression or not. If
4961 ;; both LIM and CONTAINING-SEXP is used, LIM needs to be farther
4964 (let ((res 'maybe) passed-bracket
4965 (closest-lim (or containing-sexp lim (point-min)))
4966 ;; Look at the character after point only as a last resort
4967 ;; when we can't disambiguate.
4968 (block-follows (and (eq (char-after) ?{) (point))))
4969 (while (and (eq res 'maybe)
4970 (progn (c-backward-syntactic-ws)
4971 (> (point) closest-lim))
4973 (progn (backward-char)
4974 (looking-at "[\]\).]\\|\\w\\|\\s_"))
4975 (progn (forward-char)
4976 (goto-char (scan-sexps (point) -1))))
4980 c-opt-inexpr-class-key
4981 (looking-at c-opt-inexpr-class-key))
4982 (and (not passed-bracket)
4983 (or (not (looking-at c-class-key))
4984 ;; If the class definition is at the start of
4985 ;; a statement, we don't consider it an
4986 ;; in-expression class.
4987 (let ((prev (point)))
4989 (= (c-backward-token-2 1 nil closest-lim) 0)
4990 (eq (char-syntax (char-after)) ?w))
4991 (setq prev (point)))
4993 (not (c-looking-at-bos)))
4994 ;; Also, in Pike we treat it as an
4995 ;; in-expression class if it's used in an
4996 ;; object clone expression.
4998 (and (c-major-mode-is 'pike-mode)
4999 (progn (goto-char block-follows)
5000 (zerop (c-forward-token-2 1 t)))
5001 (eq (char-after) ?\())))
5002 (cons 'inexpr-class (point))))
5003 ((and c-opt-inexpr-block-key
5004 (looking-at c-opt-inexpr-block-key))
5005 (cons 'inexpr-statement (point)))
5006 ((and c-opt-lambda-key
5007 (looking-at c-opt-lambda-key))
5008 (cons 'inlambda (point)))
5009 ((and c-opt-block-stmt-key
5010 (looking-at c-opt-block-stmt-key))
5013 (if (eq (char-after) ?\[)
5014 (setq passed-bracket t))
5017 (when (and block-follows
5019 (eq (char-after containing-sexp) ?\())
5020 (goto-char containing-sexp)
5021 (if (or (save-excursion
5022 (c-backward-syntactic-ws lim)
5023 (and (> (point) (or lim (point-min)))
5025 (and c-special-brace-lists
5026 (c-looking-at-special-brace-list)))
5028 (cons 'inexpr-statement (point))))
5031 (defun c-looking-at-inexpr-block-backward (paren-state)
5032 ;; Returns non-nil if we're looking at the end of an in-expression
5033 ;; block, otherwise the same as `c-looking-at-inexpr-block'.
5034 ;; PAREN-STATE is the paren state relevant at the current position.
5036 ;; We currently only recognize a block.
5037 (let ((here (point))
5038 (elem (car-safe paren-state))
5040 (when (and (consp elem)
5041 (progn (goto-char (cdr elem))
5042 (c-forward-syntactic-ws here)
5044 (goto-char (car elem))
5045 (if (setq paren-state (cdr paren-state))
5046 (setq containing-sexp (car-safe paren-state)))
5047 (c-looking-at-inexpr-block (c-safe-position containing-sexp
5049 containing-sexp)))))
5051 (defun c-narrow-out-enclosing-class (paren-state lim)
5052 ;; Narrow the buffer so that the enclosing class is hidden. Uses
5053 ;; and returns the value from c-search-uplist-for-classkey.
5054 (setq paren-state (c-whack-state-after (point) paren-state))
5057 (setq inclass-p (c-search-uplist-for-classkey paren-state))
5060 (goto-char (1+ (aref inclass-p 1)))
5061 (c-skip-ws-forward lim)
5062 ;; if point is now left of the class opening brace, we're
5063 ;; hosed, so try a different tact
5064 (if (<= (point) (aref inclass-p 1))
5066 (goto-char (1+ (aref inclass-p 1)))
5067 (c-forward-syntactic-ws lim)))
5069 ;; end point is the end of the current line
5073 ;; return the class vector
5077 ;; `c-guess-basic-syntax' and the functions that precedes it below
5078 ;; implements the main decision tree for determining the syntactic
5079 ;; analysis of the current line of code.
5081 ;; Dynamically bound to t when `c-guess-basic-syntax' is called during
5082 ;; auto newline analysis.
5083 (defvar c-auto-newline-analysis nil)
5085 (defsubst c-add-syntax (symbol &rest args)
5086 ;; A simple function to prepend a new syntax element to
5087 ;; `c-syntactic-context'. Using `setq' on it is unsafe since it
5088 ;; should always be dynamically bound but since we read it first
5089 ;; we'll fail properly anyway if this function is misused.
5090 (setq c-syntactic-context (cons (cons symbol args)
5091 c-syntactic-context)))
5093 (defsubst c-append-syntax (symbol &rest args)
5094 ;; Like `c-add-syntax' but appends to the end of the syntax list.
5095 ;; (Normally not necessary.)
5096 (setq c-syntactic-context (nconc c-syntactic-context
5097 (list (cons symbol args)))))
5099 (defun c-add-stmt-syntax (syntax-symbol
5105 ;; Do the generic processing to anchor the given syntax symbol on
5106 ;; the preceding statement: Skip over any labels and containing
5107 ;; statements on the same line, and then search backward until we
5108 ;; find a statement or block start that begins at boi without a
5109 ;; label or comment.
5111 ;; Point is assumed to be at the prospective anchor point for the
5112 ;; given SYNTAX-SYMBOL. More syntax entries are added if we need to
5113 ;; skip past open parens and containing statements. All the added
5114 ;; syntax elements will get the same anchor point.
5116 ;; SYNTAX-EXTRA-ARGS are a list of the extra arguments for the
5117 ;; syntax symbol. They are appended after the anchor point.
5119 ;; If STOP-AT-BOI-ONLY is nil, we might stop in the middle of the
5120 ;; line if another statement precedes the current one on this line.
5122 ;; If AT-BLOCK-START is non-nil, point is taken to be at the
5123 ;; beginning of a block or brace list, which then might be nested
5124 ;; inside an expression. If AT-BLOCK-START is nil, this is found
5125 ;; out by checking whether the character at point is "{" or not.
5126 (if (= (point) (c-point 'boi))
5127 ;; This is by far the most common case, so let's give it special
5129 (apply 'c-add-syntax syntax-symbol (point) syntax-extra-args)
5131 (let ((savepos (point))
5132 (syntax-last c-syntactic-context)
5133 (boi (c-point 'boi))
5134 (prev-paren (if at-block-start ?{ (char-after)))
5135 step-type step-tmp at-comment special-list)
5136 (apply 'c-add-syntax syntax-symbol nil syntax-extra-args)
5138 ;; Begin by skipping any labels and containing statements that
5139 ;; are on the same line.
5140 (while (and (/= (point) boi)
5141 (if (memq (setq step-tmp
5142 (c-beginning-of-statement-1 boi nil t))
5147 (/= (point) savepos))
5148 (setq savepos (point)
5149 step-type step-tmp))
5152 ;; Loop if we have to back out of the containing block.
5156 ;; Loop if we have to back up another statement.
5160 ;; Always start by skipping over any comments that
5161 ;; stands between the statement and boi.
5162 (while (and (/= (setq savepos (point)) boi)
5163 (c-backward-single-comment))
5165 boi (c-point 'boi)))
5170 (eq step-type 'label)
5174 ;; Current position might not be good enough;
5175 ;; skip backward another statement.
5176 (setq step-type (c-beginning-of-statement-1
5179 (if (and (not stop-at-boi-only)
5181 (memq step-type '(up previous)))
5182 ;; If stop-at-boi-only is nil, we shouldn't
5183 ;; back up over previous or containing
5184 ;; statements to try to reach boi, so go
5185 ;; back to the last position and exit.
5189 (if (and (not stop-at-boi-only)
5190 (memq step-type '(up previous beginning)))
5191 ;; If we've moved into another statement
5192 ;; then we should no longer try to stop
5194 (setq stop-at-boi-only t))
5196 ;; Record this a substatement if we skipped up
5197 ;; one level, but not if we're still on the
5198 ;; same line. This so e.g. a sequence of "else
5199 ;; if" clauses won't indent deeper and deeper.
5200 (when (and (eq step-type 'up)
5202 (c-add-syntax 'substatement nil))
5204 (setq boi (c-point 'boi))
5205 (/= (point) savepos)))))
5207 (setq savepos (point)
5209 (setq at-comment nil)
5211 (when (and (eq step-type 'same)
5213 (goto-char containing-sexp)
5215 ;; Don't stop in the middle of a special brace list opener
5217 (when (and c-special-brace-lists
5219 (c-looking-at-special-brace-list)))
5220 (setq containing-sexp (car (car special-list)))
5221 (goto-char containing-sexp))
5223 (setq paren-state (c-whack-state-after containing-sexp
5225 containing-sexp (c-most-enclosing-brace paren-state)
5229 (if (eq (setq prev-paren (char-after)) ?\()
5231 (c-backward-syntactic-ws containing-sexp)
5232 (when (/= savepos boi)
5233 (if (and (or (not (looking-at "\\>"))
5234 (not (c-on-identifier)))
5237 (c-forward-syntactic-ws)
5239 (c-forward-syntactic-ws)
5240 (eq (char-after) ?{)))
5241 ;; We're in an in-expression statement.
5242 ;; This syntactic element won't get an anchor pos.
5243 (c-add-syntax 'inexpr-statement)
5244 (c-add-syntax 'arglist-cont-nonempty nil savepos)))
5247 (1+ containing-sexp)
5249 (setq step-type 'same))
5251 (c-beginning-of-statement-1 containing-sexp)))
5253 (let ((at-bod (and (eq step-type 'same)
5254 (/= savepos (point))
5255 (eq prev-paren ?{))))
5257 (when (= savepos boi)
5258 ;; If the open brace was at boi, we're always
5259 ;; done. The c-beginning-of-statement-1 call
5260 ;; above is necessary anyway, to decide the type
5261 ;; of block-intro to add.
5265 (when (eq prev-paren ?{)
5266 (c-add-syntax (if at-bod
5268 'statement-block-intro)
5271 (when (and (not at-bod) savepos)
5272 ;; Loop if the brace wasn't at boi, and we didn't
5273 ;; arrive at a defun block.
5274 (if (eq step-type 'same)
5275 ;; Avoid backing up another sexp if the point
5276 ;; we're at now is found to be good enough in
5278 (setq step-type nil))
5279 (if (and (not stop-at-boi-only)
5280 (memq step-type '(up previous beginning)))
5281 (setq stop-at-boi-only t))
5282 (setq boi (c-point 'boi)))))
5285 ;; Fill in the current point as the anchor for all the symbols
5287 (let ((p c-syntactic-context))
5288 (while (not (eq p syntax-last))
5290 (setcar (cdr (car p)) (point)))
5295 (defun c-add-class-syntax (symbol classkey paren-state)
5296 ;; The inclass and class-close syntactic symbols are added in
5297 ;; several places and some work is needed to fix everything.
5298 ;; Therefore it's collected here.
5301 (let (inexpr anchor containing-sexp)
5302 (goto-char (aref classkey 1))
5303 (if (and (eq symbol 'inclass) (= (point) (c-point 'boi)))
5304 (c-add-syntax symbol (setq anchor (point)))
5305 (c-add-syntax symbol (setq anchor (aref classkey 0)))
5306 (if (and c-opt-inexpr-class-key
5307 (setq containing-sexp (c-most-enclosing-brace paren-state
5309 inexpr (cdr (c-looking-at-inexpr-block
5310 (c-safe-position containing-sexp
5313 (/= inexpr (c-point 'boi inexpr)))
5314 (c-add-syntax 'inexpr-class)))
5317 (defun c-guess-continued-construct (indent-point
5319 beg-of-same-or-containing-stmt
5322 ;; This function contains the decision tree reached through both
5323 ;; cases 18 and 10. It's a continued statement or top level
5324 ;; construct of some kind.
5326 (let (special-brace-list)
5327 (goto-char indent-point)
5328 (skip-chars-forward " \t")
5331 ;; (CASE A removed.)
5332 ;; CASE B: open braces for class or brace-lists
5333 ((setq special-brace-list
5334 (or (and c-special-brace-lists
5335 (c-looking-at-special-brace-list))
5336 (eq char-after-ip ?{)))
5339 ;; CASE B.1: class-open
5341 (skip-chars-forward "{")
5342 (let ((decl (c-search-uplist-for-classkey (c-parse-state))))
5344 (setq beg-of-same-or-containing-stmt (aref decl 0)))
5346 (c-add-syntax 'class-open beg-of-same-or-containing-stmt))
5348 ;; CASE B.2: brace-list-open
5349 ((or (consp special-brace-list)
5351 (goto-char beg-of-same-or-containing-stmt)
5352 (c-syntactic-re-search-forward "=\\([^=]\\|$\\)"
5353 indent-point t t t)))
5354 ;; The most semantically accurate symbol here is
5355 ;; brace-list-open, but we normally report it simply as a
5356 ;; statement-cont. The reason is that one normally adjusts
5357 ;; brace-list-open for brace lists as top-level constructs,
5358 ;; and brace lists inside statements is a completely different
5359 ;; context. C.f. case 5A.3.
5360 (c-beginning-of-statement-1 containing-sexp)
5361 (c-add-stmt-syntax (if c-auto-newline-analysis
5362 ;; Turn off the dwim above when we're
5363 ;; analyzing the nature of the brace
5364 ;; for the auto newline feature.
5368 containing-sexp paren-state))
5370 ;; CASE B.3: The body of a function declared inside a normal
5371 ;; block. Can occur e.g. in Pike and when using gcc
5372 ;; extensions. Might also trigger it with some macros followed
5373 ;; by blocks, and this gives sane indentation then too.
5374 ;; C.f. cases E, 16F and 17G.
5375 ((and (not (c-looking-at-bos))
5376 (eq (c-beginning-of-statement-1 containing-sexp nil nil t)
5378 (c-add-stmt-syntax 'defun-open nil t nil
5379 containing-sexp paren-state))
5381 ;; CASE B.4: Continued statement with block open.
5383 (goto-char beg-of-same-or-containing-stmt)
5384 (c-add-stmt-syntax 'statement-cont nil nil nil
5385 containing-sexp paren-state)
5386 (c-add-syntax 'block-open))
5389 ;; CASE C: iostream insertion or extraction operator
5390 ((and (looking-at "\\(<<\\|>>\\)\\([^=]\\|$\\)")
5392 (goto-char beg-of-same-or-containing-stmt)
5393 ;; If there is no preceding streamop in the statement
5394 ;; then indent this line as a normal statement-cont.
5395 (when (c-syntactic-re-search-forward
5396 "\\(<<\\|>>\\)\\([^=]\\|$\\)" indent-point 'move t t)
5397 (c-add-syntax 'stream-op (c-point 'boi))
5400 ;; CASE E: In the "K&R region" of a function declared inside a
5401 ;; normal block. C.f. case B.3.
5402 ((and (save-excursion
5403 ;; Check that the next token is a '{'. This works as
5404 ;; long as no language that allows nested function
5405 ;; definitions doesn't allow stuff like member init
5406 ;; lists, K&R declarations or throws clauses there.
5408 ;; Note that we do a forward search for something ahead
5409 ;; of the indentation line here. That's not good since
5410 ;; the user might not have typed it yet. Unfortunately
5411 ;; it's exceedingly tricky to recognize a function
5412 ;; prototype in a code block without resorting to this.
5413 (c-forward-syntactic-ws)
5414 (eq (char-after) ?{))
5415 (not (c-looking-at-bos))
5416 (eq (c-beginning-of-statement-1 containing-sexp nil nil t)
5418 (c-add-stmt-syntax 'func-decl-cont nil t nil
5419 containing-sexp paren-state))
5421 ;; CASE D: continued statement.
5423 (c-beginning-of-statement-1 containing-sexp)
5424 (c-add-stmt-syntax 'statement-cont nil nil nil
5425 containing-sexp paren-state))
5428 (defun c-guess-basic-syntax ()
5429 "Return the syntactic context of the current line.
5430 This function does not do any hidden buffer changes."
5434 (c-save-buffer-state
5435 ((indent-point (point))
5436 (case-fold-search nil)
5437 (paren-state (c-parse-state))
5438 literal containing-sexp char-before-ip char-after-ip lim
5439 c-syntactic-context placeholder c-in-literal-cache step-type
5440 tmpsymbol keyword injava-inher special-brace-list
5441 ;; narrow out any enclosing class or extern "C" block
5442 (inclass-p (c-narrow-out-enclosing-class paren-state
5444 ;; `c-state-cache' is shadowed here so that we don't
5445 ;; throw it away due to the narrowing that might be done
5446 ;; by the function above. That means we must not do any
5447 ;; changes during the execution of this function, since
5448 ;; `c-invalidate-state-cache' then would change this local
5449 ;; variable and leave a bogus value in the global one.
5450 (c-state-cache (if inclass-p
5451 (c-whack-state-before (point-min) paren-state)
5453 (c-state-cache-start (point-min))
5454 inenclosing-p macro-start in-macro-expr
5455 ;; There's always at most one syntactic element which got
5456 ;; a relpos. It's stored in syntactic-relpos.
5458 (c-stmt-delim-chars c-stmt-delim-chars))
5459 ;; Check for meta top-level enclosing constructs such as
5460 ;; extern language definitions.
5464 (when (and inclass-p
5466 (goto-char (aref inclass-p 0))
5467 (looking-at c-other-decl-block-key)))
5468 (setq inenclosing-p (match-string 1))
5469 (if (string-equal inenclosing-p "extern")
5470 ;; Compatibility with legacy choice of name for the
5471 ;; extern-lang syntactic symbols.
5472 (setq inenclosing-p "extern-lang")))))
5474 ;; Init some position variables:
5476 ;; containing-sexp is the open paren of the closest
5477 ;; surrounding sexp or nil if there is none that hasn't been
5480 ;; lim is the position after the closest preceding brace sexp
5481 ;; (nested sexps are ignored), or the position after
5482 ;; containing-sexp if there is none, or (point-min) if
5483 ;; containing-sexp is nil.
5485 ;; c-state-cache is the state from c-parse-state at
5486 ;; indent-point, without any parens outside the region
5487 ;; narrowed by c-narrow-out-enclosing-class.
5489 ;; paren-state is the state from c-parse-state outside
5490 ;; containing-sexp, or at indent-point if containing-sexp is
5491 ;; nil. paren-state is not limited to the narrowed region, as
5492 ;; opposed to c-state-cache.
5495 (setq containing-sexp (car paren-state)
5496 paren-state (cdr paren-state))
5497 (if (consp containing-sexp)
5499 (setq lim (cdr containing-sexp))
5500 (if (cdr c-state-cache)
5501 ;; Ignore balanced paren. The next entry
5502 ;; can't be another one.
5503 (setq containing-sexp (car (cdr c-state-cache))
5504 paren-state (cdr paren-state))
5505 ;; If there is no surrounding open paren then
5506 ;; put the last balanced pair back on paren-state.
5507 (setq paren-state (cons containing-sexp paren-state)
5508 containing-sexp nil)))
5509 (setq lim (1+ containing-sexp))))
5510 (setq lim (point-min)))
5512 ;; If we're in a parenthesis list then ',' delimits the
5513 ;; "statements" rather than being an operator (with the
5514 ;; exception of the "for" clause). This difference is
5515 ;; typically only noticeable when statements are used in macro
5517 (when (and containing-sexp
5518 (eq (char-after containing-sexp) ?\())
5519 (setq c-stmt-delim-chars c-stmt-delim-chars-with-comma))
5521 ;; cache char before and after indent point, and move point to
5522 ;; the most likely position to perform the majority of tests
5523 (goto-char indent-point)
5524 (c-backward-syntactic-ws lim)
5525 (setq char-before-ip (char-before))
5526 (goto-char indent-point)
5527 (skip-chars-forward " \t")
5528 (setq char-after-ip (char-after))
5530 ;; are we in a literal?
5531 (setq literal (c-in-literal lim))
5533 ;; now figure out syntactic qualities of the current line
5535 ;; CASE 1: in a string.
5536 ((eq literal 'string)
5537 (c-add-syntax 'string (c-point 'bopl)))
5538 ;; CASE 2: in a C or C++ style comment.
5539 ((and (memq literal '(c c++))
5540 ;; This is a kludge for XEmacs where we use
5541 ;; `buffer-syntactic-context', which doesn't correctly
5542 ;; recognize "\*/" to end a block comment.
5543 ;; `parse-partial-sexp' which is used by
5544 ;; `c-literal-limits' will however do that in most
5545 ;; versions, which results in that we get nil from
5546 ;; `c-literal-limits' even when `c-in-literal' claims
5547 ;; we're inside a comment.
5548 (setq placeholder (c-literal-limits lim)))
5549 (c-add-syntax literal (car placeholder)))
5550 ;; CASE 3: in a cpp preprocessor macro continuation.
5551 ((and (save-excursion
5552 (when (c-beginning-of-macro)
5553 (setq macro-start (point))))
5554 (/= macro-start (c-point 'boi))
5556 (setq tmpsymbol 'cpp-macro-cont)
5557 (or (not c-syntactic-indentation-in-macros)
5559 (goto-char macro-start)
5560 ;; If at the beginning of the body of a #define
5561 ;; directive then analyze as cpp-define-intro
5562 ;; only. Go on with the syntactic analysis
5563 ;; otherwise. in-macro-expr is set if we're in a
5564 ;; cpp expression, i.e. before the #define body
5565 ;; or anywhere in a non-#define directive.
5566 (if (c-forward-to-cpp-define-body)
5567 (let ((indent-boi (c-point 'boi indent-point)))
5568 (setq in-macro-expr (> (point) indent-boi)
5569 tmpsymbol 'cpp-define-intro)
5570 (= (point) indent-boi))
5571 (setq in-macro-expr t)
5573 (c-add-syntax tmpsymbol macro-start)
5574 (setq macro-start nil))
5575 ;; CASE 11: an else clause?
5576 ((looking-at "else\\>[^_]")
5577 (c-beginning-of-statement-1 containing-sexp)
5578 (c-add-stmt-syntax 'else-clause nil t nil
5579 containing-sexp paren-state))
5580 ;; CASE 12: while closure of a do/while construct?
5581 ((and (looking-at "while\\>[^_]")
5583 (prog1 (eq (c-beginning-of-statement-1 containing-sexp)
5585 (setq placeholder (point)))))
5586 (goto-char placeholder)
5587 (c-add-stmt-syntax 'do-while-closure nil t nil
5588 containing-sexp paren-state))
5589 ;; CASE 13: A catch or finally clause? This case is simpler
5590 ;; than if-else and do-while, because a block is required
5591 ;; after every try, catch and finally.
5593 (and (cond ((c-major-mode-is 'c++-mode)
5594 (looking-at "catch\\>[^_]"))
5595 ((c-major-mode-is 'java-mode)
5596 (looking-at "\\(catch\\|finally\\)\\>[^_]")))
5597 (and (c-safe (c-backward-syntactic-ws)
5600 (eq (char-after) ?{)
5601 (c-safe (c-backward-syntactic-ws)
5604 (if (eq (char-after) ?\()
5605 (c-safe (c-backward-sexp) t)
5607 (looking-at "\\(try\\|catch\\)\\>[^_]")
5608 (setq placeholder (point))))
5609 (goto-char placeholder)
5610 (c-add-stmt-syntax 'catch-clause nil t nil
5611 containing-sexp paren-state))
5612 ;; CASE 18: A substatement we can recognize by keyword.
5614 (and c-opt-block-stmt-key
5615 (if (c-mode-is-new-awk-p)
5616 (c-awk-prev-line-incomplete-p containing-sexp) ; ACM 2002/3/29
5617 (not (eq char-before-ip ?\;)))
5618 (not (memq char-after-ip '(?\) ?\] ?,)))
5619 (or (not (eq char-before-ip ?}))
5620 (c-looking-at-inexpr-block-backward c-state-cache))
5623 ;; Ought to cache the result from the
5624 ;; c-beginning-of-statement-1 calls here.
5625 (setq placeholder (point))
5626 (while (eq (setq step-type
5627 (c-beginning-of-statement-1 lim))
5629 (if (eq step-type 'previous)
5630 (goto-char placeholder)
5631 (setq placeholder (point))
5632 (if (and (eq step-type 'same)
5633 (not (looking-at c-opt-block-stmt-key)))
5634 ;; Step up to the containing statement if we
5635 ;; stayed in the same one.
5639 (c-beginning-of-statement-1 lim))
5642 (setq placeholder (point))
5643 ;; There was no containing statement afterall.
5644 (goto-char placeholder)))))
5646 (if (looking-at c-block-stmt-2-key)
5647 ;; Require a parenthesis after these keywords.
5648 ;; Necessary to catch e.g. synchronized in Java,
5649 ;; which can be used both as statement and
5651 (and (zerop (c-forward-token-2 1 nil))
5652 (eq (char-after) ?\())
5653 (looking-at c-opt-block-stmt-key))))
5654 (if (eq step-type 'up)
5655 ;; CASE 18A: Simple substatement.
5657 (goto-char placeholder)
5659 ((eq char-after-ip ?{)
5660 (c-add-stmt-syntax 'substatement-open nil nil nil
5661 containing-sexp paren-state))
5663 (goto-char indent-point)
5664 (back-to-indentation)
5665 (looking-at c-label-key))
5666 (c-add-stmt-syntax 'substatement-label nil nil nil
5667 containing-sexp paren-state))
5669 (c-add-stmt-syntax 'substatement nil nil nil
5670 containing-sexp paren-state))))
5671 ;; CASE 18B: Some other substatement. This is shared
5673 (c-guess-continued-construct indent-point
5678 ;; CASE 4: In-expression statement. C.f. cases 7B, 16A and
5680 ((and (or c-opt-inexpr-class-key
5681 c-opt-inexpr-block-key
5683 (setq placeholder (c-looking-at-inexpr-block
5684 (c-safe-position containing-sexp paren-state)
5686 (setq tmpsymbol (assq (car placeholder)
5687 '((inexpr-class . class-open)
5688 (inexpr-statement . block-open))))
5690 ;; It's a statement block or an anonymous class.
5691 (setq tmpsymbol (cdr tmpsymbol))
5692 ;; It's a Pike lambda. Check whether we are between the
5693 ;; lambda keyword and the argument list or at the defun
5695 (setq tmpsymbol (if (eq char-after-ip ?{)
5697 'lambda-intro-cont)))
5698 (goto-char (cdr placeholder))
5699 (back-to-indentation)
5700 (c-add-stmt-syntax tmpsymbol nil t nil
5701 (c-most-enclosing-brace c-state-cache (point))
5702 (c-whack-state-after (point) paren-state))
5703 (unless (eq (point) (cdr placeholder))
5704 (c-add-syntax (car placeholder))))
5705 ;; CASE 5: Line is at top level.
5706 ((null containing-sexp)
5708 ;; CASE 5A: we are looking at a defun, brace list, class,
5709 ;; or inline-inclass method opening brace
5710 ((setq special-brace-list
5711 (or (and c-special-brace-lists
5712 (c-looking-at-special-brace-list))
5713 (eq char-after-ip ?{)))
5715 ;; CASE 5A.1: Non-class declaration block open.
5717 (goto-char indent-point)
5718 (skip-chars-forward " \t")
5719 (and (c-safe (c-backward-sexp 2) t)
5720 (looking-at c-other-decl-block-key)
5721 (setq keyword (match-string 1)
5722 placeholder (point))
5723 (if (string-equal keyword "extern")
5724 ;; Special case for extern-lang-open. The
5725 ;; check for a following string is disabled
5726 ;; since it doesn't disambiguate anything.
5728 ;; (c-forward-sexp 1)
5729 ;; (c-forward-syntactic-ws)
5730 ;; (eq (char-after) ?\"))
5731 (setq tmpsymbol 'extern-lang-open))
5732 (setq tmpsymbol (intern (concat keyword "-open"))))
5734 (goto-char placeholder)
5735 (c-add-syntax tmpsymbol (c-point 'boi)))
5736 ;; CASE 5A.2: we are looking at a class opening brace
5738 (goto-char indent-point)
5739 (skip-chars-forward " \t{")
5740 (let ((decl (c-search-uplist-for-classkey (c-parse-state))))
5742 (setq placeholder (aref decl 0)))
5744 (c-add-syntax 'class-open placeholder))
5745 ;; CASE 5A.3: brace list open
5747 (c-beginning-of-decl-1 lim)
5748 (while (looking-at c-specifier-key)
5749 (goto-char (match-end 1))
5750 (c-forward-syntactic-ws indent-point))
5751 (setq placeholder (c-point 'boi))
5752 (or (consp special-brace-list)
5753 (and (or (save-excursion
5754 (goto-char indent-point)
5755 (setq tmpsymbol nil)
5756 (while (and (> (point) placeholder)
5757 (zerop (c-backward-token-2 1 t))
5758 (/= (char-after) ?=))
5759 (and c-opt-inexpr-brace-list-key
5761 (looking-at c-opt-inexpr-brace-list-key)
5762 (setq tmpsymbol 'topmost-intro-cont)))
5763 (eq (char-after) ?=))
5764 (looking-at c-brace-list-key))
5766 (while (and (< (point) indent-point)
5767 (zerop (c-forward-token-2 1 t))
5768 (not (memq (char-after) '(?\; ?\()))))
5769 (not (memq (char-after) '(?\; ?\()))
5771 (if (and (not c-auto-newline-analysis)
5772 (c-major-mode-is 'java-mode)
5773 (eq tmpsymbol 'topmost-intro-cont))
5774 ;; We're in Java and have found that the open brace
5775 ;; belongs to a "new Foo[]" initialization list,
5776 ;; which means the brace list is part of an
5777 ;; expression and not a top level definition. We
5778 ;; therefore treat it as any topmost continuation
5779 ;; even though the semantically correct symbol still
5780 ;; is brace-list-open, on the same grounds as in
5783 (c-beginning-of-statement-1 lim)
5784 (c-add-syntax 'topmost-intro-cont (c-point 'boi)))
5785 (c-add-syntax 'brace-list-open placeholder)))
5786 ;; CASE 5A.4: inline defun open
5787 ((and inclass-p (not inenclosing-p))
5788 (c-add-syntax 'inline-open)
5789 (c-add-class-syntax 'inclass inclass-p paren-state))
5790 ;; CASE 5A.5: ordinary defun open
5792 (goto-char placeholder)
5793 (if (or inclass-p macro-start)
5794 (c-add-syntax 'defun-open (c-point 'boi))
5795 ;; Bogus to use bol here, but it's the legacy.
5796 (c-add-syntax 'defun-open (c-point 'bol)))
5798 ;; CASE 5B: first K&R arg decl or member init
5799 ((c-just-after-func-arglist-p lim)
5801 ;; CASE 5B.1: a member init
5802 ((or (eq char-before-ip ?:)
5803 (eq char-after-ip ?:))
5804 ;; this line should be indented relative to the beginning
5805 ;; of indentation for the topmost-intro line that contains
5806 ;; the prototype's open paren
5807 ;; TBD: is the following redundant?
5808 (if (eq char-before-ip ?:)
5810 (c-backward-syntactic-ws lim)
5811 ;; TBD: is the preceding redundant?
5812 (if (eq (char-before) ?:)
5813 (progn (forward-char -1)
5814 (c-backward-syntactic-ws lim)))
5815 (if (eq (char-before) ?\))
5816 (c-backward-sexp 1))
5817 (setq placeholder (point))
5819 (and (c-safe (c-backward-sexp 1) t)
5820 (looking-at "throw[^_]")
5821 (c-safe (c-backward-sexp 1) t)
5822 (setq placeholder (point))))
5823 (goto-char placeholder)
5824 (c-add-syntax 'member-init-intro (c-point 'boi))
5825 ;; we don't need to add any class offset since this
5826 ;; should be relative to the ctor's indentation
5828 ;; CASE 5B.2: K&R arg decl intro
5830 (c-beginning-of-statement-1 lim)
5831 (c-add-syntax 'knr-argdecl-intro (c-point 'boi))
5833 (c-add-class-syntax 'inclass inclass-p paren-state)))
5834 ;; CASE 5B.3: Inside a member init list.
5835 ((c-beginning-of-member-init-list lim)
5836 (c-forward-syntactic-ws)
5837 (c-add-syntax 'member-init-cont (point)))
5838 ;; CASE 5B.4: Nether region after a C++ or Java func
5839 ;; decl, which could include a `throws' declaration.
5841 (c-beginning-of-statement-1 lim)
5842 (c-add-syntax 'func-decl-cont (c-point 'boi))
5844 ;; CASE 5C: inheritance line. could be first inheritance
5845 ;; line, or continuation of a multiple inheritance
5846 ((or (and (c-major-mode-is 'c++-mode)
5848 (when (eq char-after-ip ?,)
5849 (skip-chars-forward " \t")
5851 (looking-at c-opt-postfix-decl-spec-key)))
5852 (and (or (eq char-before-ip ?:)
5853 ;; watch out for scope operator
5855 (and (eq char-after-ip ?:)
5856 (c-safe (forward-char 1) t)
5857 (not (eq (char-after) ?:))
5860 (c-backward-syntactic-ws lim)
5861 (if (eq char-before-ip ?:)
5864 (c-backward-syntactic-ws lim)))
5865 (back-to-indentation)
5866 (looking-at c-class-key)))
5868 (and (c-major-mode-is 'java-mode)
5869 (let ((fence (save-excursion
5870 (c-beginning-of-statement-1 lim)
5875 (cond ((looking-at c-opt-postfix-decl-spec-key)
5876 (setq injava-inher (cons cont (point))
5878 ((or (not (c-safe (c-forward-sexp -1) t))
5884 (not (c-crosses-statement-barrier-p (cdr injava-inher)
5888 ;; CASE 5C.1: non-hanging colon on an inher intro
5889 ((eq char-after-ip ?:)
5890 (c-beginning-of-statement-1 lim)
5891 (c-add-syntax 'inher-intro (c-point 'boi))
5892 ;; don't add inclass symbol since relative point already
5893 ;; contains any class offset
5895 ;; CASE 5C.2: hanging colon on an inher intro
5896 ((eq char-before-ip ?:)
5897 (c-beginning-of-statement-1 lim)
5898 (c-add-syntax 'inher-intro (c-point 'boi))
5900 (c-add-class-syntax 'inclass inclass-p paren-state)))
5901 ;; CASE 5C.3: in a Java implements/extends
5903 (let ((where (cdr injava-inher))
5904 (cont (car injava-inher)))
5906 (cond ((looking-at "throws\\>[^_]")
5907 (c-add-syntax 'func-decl-cont
5908 (progn (c-beginning-of-statement-1 lim)
5910 (cont (c-add-syntax 'inher-cont where))
5911 (t (c-add-syntax 'inher-intro
5912 (progn (goto-char (cdr injava-inher))
5913 (c-beginning-of-statement-1 lim)
5916 ;; CASE 5C.4: a continued inheritance line
5918 (c-beginning-of-inheritance-list lim)
5919 (c-add-syntax 'inher-cont (point))
5920 ;; don't add inclass symbol since relative point already
5921 ;; contains any class offset
5923 ;; CASE 5D: this could be a top-level initialization, a
5924 ;; member init list continuation, or a template argument
5925 ;; list continuation.
5926 ((c-with-syntax-table (if (c-major-mode-is 'c++-mode)
5927 c++-template-syntax-table
5930 ;; Note: We use the fact that lim is always after any
5931 ;; preceding brace sexp.
5932 (while (and (zerop (c-backward-token-2 1 t lim))
5933 (not (looking-at "[;<,=]"))))
5934 (or (memq (char-after) '(?, ?=))
5935 (and (c-major-mode-is 'c++-mode)
5936 (zerop (c-backward-token-2 1 nil lim))
5937 (eq (char-after) ?<)))))
5938 (goto-char indent-point)
5940 (c-beginning-of-member-init-list lim))
5942 ;; CASE 5D.1: hanging member init colon, but watch out
5943 ;; for bogus matches on access specifiers inside classes.
5946 (setq placeholder (point))
5947 (c-backward-token-2 1 t lim)
5948 (and (eq (char-after) ?:)
5949 (not (eq (char-before) ?:))))
5951 (goto-char placeholder)
5952 (back-to-indentation)
5954 (/= (car (save-excursion
5955 (parse-partial-sexp (point) placeholder)))
5958 (if c-opt-access-key
5959 (not (looking-at c-opt-access-key)) t)
5960 (not (looking-at c-class-key))
5961 (if c-opt-bitfield-key
5962 (not (looking-at c-opt-bitfield-key)) t))
5964 (goto-char placeholder)
5965 (c-forward-syntactic-ws)
5966 (c-add-syntax 'member-init-cont (point))
5967 ;; we do not need to add class offset since relative
5968 ;; point is the member init above us
5970 ;; CASE 5D.2: non-hanging member init colon
5972 (c-forward-syntactic-ws indent-point)
5973 (eq (char-after) ?:))
5974 (skip-chars-forward " \t:")
5975 (c-add-syntax 'member-init-cont (point)))
5976 ;; CASE 5D.3: perhaps a template list continuation?
5977 ((and (c-major-mode-is 'c++-mode)
5980 (c-with-syntax-table c++-template-syntax-table
5981 (goto-char indent-point)
5982 (setq placeholder (c-up-list-backward (point)))
5984 (eq (char-after placeholder) ?<))))))
5985 ;; we can probably indent it just like an arglist-cont
5986 (goto-char placeholder)
5987 (c-beginning-of-statement-1 lim t)
5988 (c-add-syntax 'template-args-cont (c-point 'boi)))
5989 ;; CASE 5D.4: perhaps a multiple inheritance line?
5990 ((and (c-major-mode-is 'c++-mode)
5992 (c-beginning-of-statement-1 lim)
5993 (setq placeholder (point))
5994 (if (looking-at "static\\>[^_]")
5995 (c-forward-token-2 1 nil indent-point))
5996 (and (looking-at c-class-key)
5997 (zerop (c-forward-token-2 2 nil indent-point))
5998 (if (eq (char-after) ?<)
5999 (c-with-syntax-table c++-template-syntax-table
6000 (zerop (c-forward-token-2 1 t indent-point)))
6002 (eq (char-after) ?:))))
6003 (goto-char placeholder)
6004 (c-add-syntax 'inher-cont (c-point 'boi)))
6005 ;; CASE 5D.5: Continuation of the "expression part" of a
6006 ;; top level construct.
6008 (while (and (eq (car (c-beginning-of-decl-1 containing-sexp))
6011 (c-backward-syntactic-ws)
6012 (eq (char-before) ?}))))
6014 (if (eq char-before-ip ?,)
6015 ;; A preceding comma at the top level means that a
6016 ;; new variable declaration starts here. Use
6017 ;; topmost-intro-cont for it, for consistency with
6018 ;; the first variable declaration. C.f. case 5N.
6021 nil nil nil containing-sexp paren-state))
6023 ;; CASE 5E: we are looking at a access specifier
6026 (looking-at c-opt-access-key))
6027 (setq placeholder (c-add-class-syntax 'inclass inclass-p
6029 ;; Append access-label with the same anchor point as inclass gets.
6030 (c-append-syntax 'access-label placeholder))
6031 ;; CASE 5F: Close of a non-class declaration level block.
6033 (eq char-after-ip ?}))
6034 (c-add-syntax (intern (concat inenclosing-p "-close"))
6035 (aref inclass-p 0)))
6036 ;; CASE 5G: we are looking at the brace which closes the
6037 ;; enclosing nested class decl
6039 (eq char-after-ip ?})
6044 (and (c-safe (c-backward-sexp 1) t)
6045 (= (point) (aref inclass-p 1))
6047 (c-add-class-syntax 'class-close inclass-p paren-state))
6048 ;; CASE 5H: we could be looking at subsequent knr-argdecls
6049 ((and c-recognize-knr-p
6050 (not (eq char-before-ip ?}))
6052 (setq placeholder (cdr (c-beginning-of-decl-1 lim)))
6054 ;; Do an extra check to avoid tripping up on
6055 ;; statements that occur in invalid contexts
6056 ;; (e.g. in macro bodies where we don't really
6057 ;; know the context of what we're looking at).
6058 (not (and c-opt-block-stmt-key
6059 (looking-at c-opt-block-stmt-key)))))
6060 (< placeholder indent-point))
6061 (goto-char placeholder)
6062 (c-add-syntax 'knr-argdecl (point)))
6063 ;; CASE 5I: ObjC method definition.
6064 ((and c-opt-method-key
6065 (looking-at c-opt-method-key))
6066 (c-beginning-of-statement-1 lim)
6067 (c-add-syntax 'objc-method-intro (c-point 'boi)))
6068 ;; CASE 5P: AWK pattern or function or continuation
6070 ((c-mode-is-new-awk-p)
6071 (setq placeholder (point))
6073 (if (and (eq (c-beginning-of-statement-1) 'same)
6074 (/= (point) placeholder))
6078 containing-sexp paren-state))
6079 ;; CASE 5N: At a variable declaration that follows a class
6080 ;; definition or some other block declaration that doesn't
6081 ;; end at the closing '}'. C.f. case 5D.5.
6083 (c-backward-syntactic-ws lim)
6084 (and (eq (char-before) ?})
6086 (let ((start (point)))
6088 ;; Speed up the backward search a bit.
6089 (goto-char (car (car paren-state))))
6090 (c-beginning-of-decl-1 containing-sexp)
6091 (setq placeholder (point))
6092 (if (= start (point))
6093 ;; The '}' is unbalanced.
6096 (> (point) indent-point))))))
6097 (goto-char placeholder)
6098 (c-add-stmt-syntax 'topmost-intro-cont nil nil nil
6099 containing-sexp paren-state))
6100 ;; CASE 5J: we are at the topmost level, make
6101 ;; sure we skip back past any access specifiers
6103 (while (and inclass-p
6107 (c-safe (c-backward-sexp 1) t)
6108 (looking-at c-opt-access-key)))
6110 (c-backward-syntactic-ws lim))
6112 (if (c-mode-is-new-awk-p)
6113 (not (c-awk-prev-line-incomplete-p))
6114 (memq (char-before) '(?\; ?})))
6115 (and (c-major-mode-is 'objc-mode)
6117 (c-beginning-of-statement-1 lim)
6118 (eq (char-after) ?@)))))
6119 ;; real beginning-of-line could be narrowed out due to
6120 ;; enclosure in a class block
6123 (c-add-syntax 'topmost-intro (c-point 'bol))
6124 ;; Using bol instead of boi above is highly bogus, and
6125 ;; it makes our lives hard to remain compatible. :P
6128 (goto-char (aref inclass-p 1))
6129 (or (= (point) (c-point 'boi))
6130 (goto-char (aref inclass-p 0)))
6132 (c-add-syntax (intern (concat "in" inenclosing-p))
6134 (c-add-class-syntax 'inclass inclass-p paren-state))
6136 (when (and c-syntactic-indentation-in-macros
6138 (/= macro-start (c-point 'boi indent-point)))
6139 (c-add-syntax 'cpp-define-intro)
6140 (setq macro-start nil))
6142 ;; CASE 5K: we are at an ObjC method definition
6143 ;; continuation line.
6144 ((and c-opt-method-key
6146 (c-beginning-of-statement-1 lim)
6148 (looking-at c-opt-method-key)))
6149 (c-add-syntax 'objc-method-args-cont (point)))
6150 ;; CASE 5L: we are at the first argument of a template
6151 ;; arglist that begins on the previous line.
6152 ((eq (char-before) ?<)
6153 (c-beginning-of-statement-1 (c-safe-position (point) paren-state))
6154 (c-add-syntax 'template-args-cont (c-point 'boi)))
6155 ;; CASE 5M: we are at a topmost continuation line
6157 (c-beginning-of-statement-1 (c-safe-position (point) paren-state))
6158 (c-add-syntax 'topmost-intro-cont (c-point 'boi)))
6160 ;; (CASE 6 has been removed.)
6161 ;; CASE 7: line is an expression, not a statement. Most
6162 ;; likely we are either in a function prototype or a function
6163 ;; call argument list
6164 ((not (or (and c-special-brace-lists
6166 (goto-char containing-sexp)
6167 (c-looking-at-special-brace-list)))
6168 (eq (char-after containing-sexp) ?{)))
6170 ;; CASE 7A: we are looking at the arglist closing paren.
6172 ((memq char-after-ip '(?\) ?\]))
6173 (goto-char containing-sexp)
6174 (setq placeholder (c-point 'boi))
6175 (if (and (c-safe (backward-up-list 1) t)
6176 (> (point) placeholder))
6179 (skip-chars-forward " \t"))
6180 (goto-char placeholder))
6181 (c-add-stmt-syntax 'arglist-close (list containing-sexp) t nil
6182 (c-most-enclosing-brace paren-state (point))
6183 (c-whack-state-after (point) paren-state)))
6184 ;; CASE 7B: Looking at the opening brace of an
6185 ;; in-expression block or brace list. C.f. cases 4, 16A
6187 ((and (eq char-after-ip ?{)
6189 (setq placeholder (c-inside-bracelist-p (point)
6192 (setq tmpsymbol '(brace-list-open . inexpr-class))
6193 (setq tmpsymbol '(block-open . inexpr-statement)
6195 (cdr-safe (c-looking-at-inexpr-block
6196 (c-safe-position containing-sexp
6199 ;; placeholder is nil if it's a block directly in
6200 ;; a function arglist. That makes us skip out of
6203 (goto-char placeholder)
6204 (back-to-indentation)
6205 (c-add-stmt-syntax (car tmpsymbol) nil t nil
6206 (c-most-enclosing-brace paren-state (point))
6207 (c-whack-state-after (point) paren-state))
6208 (if (/= (point) placeholder)
6209 (c-add-syntax (cdr tmpsymbol))))
6210 ;; CASE 7C: we are looking at the first argument in an empty
6211 ;; argument list. Use arglist-close if we're actually
6212 ;; looking at a close paren or bracket.
6213 ((memq char-before-ip '(?\( ?\[))
6214 (goto-char containing-sexp)
6215 (setq placeholder (c-point 'boi))
6216 (when (and (c-safe (backward-up-list 1) t)
6217 (> (point) placeholder))
6219 (skip-chars-forward " \t")
6220 (setq placeholder (point)))
6221 (c-add-syntax 'arglist-intro placeholder))
6222 ;; CASE 7D: we are inside a conditional test clause. treat
6223 ;; these things as statements
6225 (goto-char containing-sexp)
6226 (and (c-safe (c-forward-sexp -1) t)
6227 (looking-at "\\<for\\>[^_]")))
6228 (goto-char (1+ containing-sexp))
6229 (c-forward-syntactic-ws indent-point)
6230 (if (eq char-before-ip ?\;)
6231 (c-add-syntax 'statement (point))
6232 (c-add-syntax 'statement-cont (point))
6234 ;; CASE 7E: maybe a continued ObjC method call. This is the
6235 ;; case when we are inside a [] bracketed exp, and what
6236 ;; precede the opening bracket is not an identifier.
6237 ((and c-opt-method-key
6238 (eq (char-after containing-sexp) ?\[)
6240 (goto-char (1- containing-sexp))
6241 (c-backward-syntactic-ws (c-point 'bod))
6242 (if (not (looking-at c-symbol-key))
6243 (c-add-syntax 'objc-method-call-cont containing-sexp))
6245 ;; CASE 7F: we are looking at an arglist continuation line,
6246 ;; but the preceding argument is on the same line as the
6247 ;; opening paren. This case includes multi-line
6248 ;; mathematical paren groupings, but we could be on a
6249 ;; for-list continuation line. C.f. case 7A.
6251 (goto-char (1+ containing-sexp))
6252 (skip-chars-forward " \t")
6254 (not (looking-at "\\\\$"))))
6255 (goto-char containing-sexp)
6256 (setq placeholder (c-point 'boi))
6257 (if (and (c-safe (backward-up-list 1) t)
6258 (> (point) placeholder))
6261 (skip-chars-forward " \t"))
6262 (goto-char placeholder))
6263 (c-add-stmt-syntax 'arglist-cont-nonempty (list containing-sexp)
6265 (c-most-enclosing-brace c-state-cache (point))
6266 (c-whack-state-after (point) paren-state)))
6267 ;; CASE 7G: we are looking at just a normal arglist
6268 ;; continuation line
6269 (t (c-forward-syntactic-ws indent-point)
6270 (c-add-syntax 'arglist-cont (c-point 'boi)))
6272 ;; CASE 8: func-local multi-inheritance line
6273 ((and (c-major-mode-is 'c++-mode)
6275 (goto-char indent-point)
6276 (skip-chars-forward " \t")
6277 (looking-at c-opt-postfix-decl-spec-key)))
6278 (goto-char indent-point)
6279 (skip-chars-forward " \t")
6281 ;; CASE 8A: non-hanging colon on an inher intro
6282 ((eq char-after-ip ?:)
6283 (c-backward-syntactic-ws lim)
6284 (c-add-syntax 'inher-intro (c-point 'boi)))
6285 ;; CASE 8B: hanging colon on an inher intro
6286 ((eq char-before-ip ?:)
6287 (c-add-syntax 'inher-intro (c-point 'boi)))
6288 ;; CASE 8C: a continued inheritance line
6290 (c-beginning-of-inheritance-list lim)
6291 (c-add-syntax 'inher-cont (point))
6293 ;; CASE 9: we are inside a brace-list
6294 ((and (not (c-mode-is-new-awk-p)) ; Maybe this isn't needed (ACM, 2002/3/29)
6295 (setq special-brace-list
6296 (or (and c-special-brace-lists
6298 (goto-char containing-sexp)
6299 (c-looking-at-special-brace-list)))
6300 (c-inside-bracelist-p containing-sexp paren-state))))
6302 ;; CASE 9A: In the middle of a special brace list opener.
6303 ((and (consp special-brace-list)
6305 (goto-char containing-sexp)
6306 (eq (char-after) ?\())
6307 (eq char-after-ip (car (cdr special-brace-list))))
6308 (goto-char (car (car special-brace-list)))
6309 (skip-chars-backward " \t")
6311 (assoc 'statement-cont
6312 (setq placeholder (c-guess-basic-syntax))))
6313 (setq c-syntactic-context placeholder)
6314 (c-beginning-of-statement-1
6315 (c-safe-position (1- containing-sexp) paren-state))
6316 (c-forward-token-2 0)
6317 (while (looking-at c-specifier-key)
6318 (goto-char (match-end 1))
6319 (c-forward-syntactic-ws))
6320 (c-add-syntax 'brace-list-open (c-point 'boi))))
6321 ;; CASE 9B: brace-list-close brace
6322 ((if (consp special-brace-list)
6323 ;; Check special brace list closer.
6325 (goto-char (car (car special-brace-list)))
6327 (goto-char indent-point)
6328 (back-to-indentation)
6330 ;; We were between the special close char and the `)'.
6331 (and (eq (char-after) ?\))
6332 (eq (1+ (point)) (cdr (car special-brace-list))))
6333 ;; We were before the special close char.
6334 (and (eq (char-after) (cdr (cdr special-brace-list)))
6335 (zerop (c-forward-token-2))
6336 (eq (1+ (point)) (cdr (car special-brace-list)))))))
6337 ;; Normal brace list check.
6338 (and (eq char-after-ip ?})
6339 (c-safe (goto-char (c-up-list-backward (point))) t)
6340 (= (point) containing-sexp)))
6341 (if (eq (point) (c-point 'boi))
6342 (c-add-syntax 'brace-list-close (point))
6343 (setq lim (c-most-enclosing-brace c-state-cache (point)))
6344 (c-beginning-of-statement-1 lim)
6345 (c-add-stmt-syntax 'brace-list-close nil t t lim
6346 (c-whack-state-after (point) paren-state))))
6348 ;; Prepare for the rest of the cases below by going to the
6349 ;; token following the opening brace
6350 (if (consp special-brace-list)
6352 (goto-char (car (car special-brace-list)))
6353 (c-forward-token-2 1 nil indent-point))
6354 (goto-char containing-sexp))
6356 (let ((start (point)))
6357 (c-forward-syntactic-ws indent-point)
6358 (goto-char (max start (c-point 'bol))))
6359 (c-skip-ws-forward indent-point)
6361 ;; CASE 9C: we're looking at the first line in a brace-list
6362 ((= (point) indent-point)
6363 (if (consp special-brace-list)
6364 (goto-char (car (car special-brace-list)))
6365 (goto-char containing-sexp))
6366 (if (eq (point) (c-point 'boi))
6367 (c-add-syntax 'brace-list-intro (point))
6368 (setq lim (c-most-enclosing-brace c-state-cache (point)))
6369 (c-beginning-of-statement-1 lim)
6370 (c-add-stmt-syntax 'brace-list-intro nil t t lim
6371 (c-whack-state-after (point) paren-state))))
6372 ;; CASE 9D: this is just a later brace-list-entry or
6374 (t (if (or (eq char-after-ip ?{)
6375 (and c-special-brace-lists
6377 (goto-char indent-point)
6378 (c-forward-syntactic-ws (c-point 'eol))
6379 (c-looking-at-special-brace-list (point)))))
6380 (c-add-syntax 'brace-entry-open (point))
6381 (c-add-syntax 'brace-list-entry (point))
6384 ;; CASE 10: A continued statement or top level construct.
6385 ((and (if (c-mode-is-new-awk-p)
6386 (c-awk-prev-line-incomplete-p containing-sexp) ; ACM 2002/3/29
6387 (and (not (memq char-before-ip '(?\; ?:)))
6388 (or (not (eq char-before-ip ?}))
6389 (c-looking-at-inexpr-block-backward c-state-cache))))
6392 (c-beginning-of-statement-1 containing-sexp)
6393 (setq placeholder (point))))
6394 (/= placeholder containing-sexp))
6395 ;; This is shared with case 18.
6396 (c-guess-continued-construct indent-point
6401 ;; CASE 14: A case or default label
6402 ((looking-at c-label-kwds-regexp)
6403 (goto-char containing-sexp)
6404 (setq lim (c-most-enclosing-brace c-state-cache containing-sexp))
6405 (c-backward-to-block-anchor lim)
6406 (c-add-stmt-syntax 'case-label nil t nil
6408 ;; CASE 15: any other label
6409 ((looking-at c-label-key)
6410 (goto-char containing-sexp)
6411 (setq lim (c-most-enclosing-brace c-state-cache containing-sexp))
6414 (if (and (eq (c-beginning-of-statement-1 lim) 'up)
6415 (looking-at "switch\\>[^_]"))
6416 ;; If the surrounding statement is a switch then
6417 ;; let's analyze all labels as switch labels, so
6418 ;; that they get lined up consistently.
6421 (c-backward-to-block-anchor lim)
6422 (c-add-stmt-syntax tmpsymbol nil t nil
6424 ;; CASE 16: block close brace, possibly closing the defun or
6426 ((eq char-after-ip ?})
6427 ;; From here on we have the next containing sexp in lim.
6428 (setq lim (c-most-enclosing-brace paren-state))
6429 (goto-char containing-sexp)
6431 ;; CASE 16E: Closing a statement block? This catches
6432 ;; cases where it's preceded by a statement keyword,
6433 ;; which works even when used in an "invalid" context,
6434 ;; e.g. a macro argument.
6435 ((c-after-conditional)
6436 (c-backward-to-block-anchor lim)
6437 (c-add-stmt-syntax 'block-close nil t nil
6439 ;; CASE 16A: closing a lambda defun or an in-expression
6440 ;; block? C.f. cases 4, 7B and 17E.
6441 ((setq placeholder (c-looking-at-inexpr-block
6442 (c-safe-position containing-sexp paren-state)
6444 (setq tmpsymbol (if (eq (car placeholder) 'inlambda)
6447 (goto-char containing-sexp)
6448 (back-to-indentation)
6449 (if (= containing-sexp (point))
6450 (c-add-syntax tmpsymbol (point))
6451 (goto-char (cdr placeholder))
6452 (back-to-indentation)
6453 (c-add-stmt-syntax tmpsymbol nil t nil
6454 (c-most-enclosing-brace paren-state (point))
6455 (c-whack-state-after (point) paren-state))
6456 (if (/= (point) (cdr placeholder))
6457 (c-add-syntax (car placeholder)))))
6458 ;; CASE 16B: does this close an inline or a function in
6459 ;; a non-class declaration level block?
6460 ((setq placeholder (c-search-uplist-for-classkey paren-state))
6461 (c-backward-to-decl-anchor lim)
6462 (back-to-indentation)
6464 (goto-char (aref placeholder 0))
6465 (looking-at c-other-decl-block-key))
6466 (c-add-syntax 'defun-close (point))
6467 (c-add-syntax 'inline-close (point))))
6468 ;; CASE 16F: Can be a defun-close of a function declared
6469 ;; in a statement block, e.g. in Pike or when using gcc
6470 ;; extensions. Might also trigger it with some macros
6471 ;; followed by blocks, and this gives sane indentation
6472 ;; then too. Let it through to be handled below.
6473 ;; C.f. cases B.3 and 17G.
6474 ((and (not inenclosing-p)
6477 (and (not (c-looking-at-bos))
6478 (eq (c-beginning-of-statement-1 lim nil nil t) 'same)
6479 (setq placeholder (point)))))
6480 (back-to-indentation)
6481 (if (/= (point) containing-sexp)
6482 (goto-char placeholder))
6483 (c-add-stmt-syntax 'defun-close nil t nil
6485 ;; CASE 16C: if there an enclosing brace that hasn't
6486 ;; been narrowed out by a class, then this is a
6487 ;; block-close. C.f. case 17H.
6488 ((and (not inenclosing-p) lim)
6489 ;; If the block is preceded by a case/switch label on
6490 ;; the same line, we anchor at the first preceding label
6491 ;; at boi. The default handling in c-add-stmt-syntax is
6492 ;; really fixes it better, but we do like this to keep
6493 ;; the indentation compatible with version 5.28 and
6495 (while (and (/= (setq placeholder (point)) (c-point 'boi))
6496 (eq (c-beginning-of-statement-1 lim) 'label)))
6497 (goto-char placeholder)
6498 (if (looking-at c-label-kwds-regexp)
6499 (c-add-syntax 'block-close (point))
6500 (goto-char containing-sexp)
6501 ;; c-backward-to-block-anchor not necessary here; those
6502 ;; situations are handled in case 16E above.
6503 (c-add-stmt-syntax 'block-close nil t nil
6505 ;; CASE 16D: find out whether we're closing a top-level
6509 (narrow-to-region (point-min) indent-point)
6510 (let ((decl (c-search-uplist-for-classkey (c-parse-state))))
6512 (c-add-class-syntax 'class-close decl paren-state)
6513 (goto-char containing-sexp)
6514 (c-backward-to-decl-anchor lim)
6515 (back-to-indentation)
6516 (c-add-syntax 'defun-close (point)))))
6518 ;; CASE 17: Statement or defun catchall.
6520 (goto-char indent-point)
6521 ;; Back up statements until we find one that starts at boi.
6522 (while (let* ((prev-point (point))
6523 (last-step-type (c-beginning-of-statement-1
6525 (if (= (point) prev-point)
6527 (setq step-type (or step-type last-step-type))
6529 (setq step-type last-step-type)
6530 (/= (point) (c-point 'boi)))))
6532 ;; CASE 17B: continued statement
6533 ((and (eq step-type 'same)
6534 (/= (point) indent-point))
6535 (c-add-stmt-syntax 'statement-cont nil nil nil
6536 containing-sexp paren-state))
6537 ;; CASE 17A: After a case/default label?
6539 (while (and (eq step-type 'label)
6540 (not (looking-at c-label-kwds-regexp)))
6542 (c-beginning-of-statement-1 containing-sexp)))
6543 (eq step-type 'label))
6544 (c-add-stmt-syntax (if (eq char-after-ip ?{)
6545 'statement-case-open
6546 'statement-case-intro)
6547 nil t nil containing-sexp paren-state))
6548 ;; CASE 17D: any old statement
6550 (while (eq step-type 'label)
6552 (c-beginning-of-statement-1 containing-sexp)))
6553 (eq step-type 'previous))
6554 (c-add-stmt-syntax 'statement nil t nil
6555 containing-sexp paren-state)
6556 (if (eq char-after-ip ?{)
6557 (c-add-syntax 'block-open)))
6558 ;; CASE 17I: Inside a substatement block.
6560 ;; The following tests are all based on containing-sexp.
6561 (goto-char containing-sexp)
6562 ;; From here on we have the next containing sexp in lim.
6563 (setq lim (c-most-enclosing-brace paren-state containing-sexp))
6564 (c-after-conditional))
6565 (c-backward-to-block-anchor lim)
6566 (c-add-stmt-syntax 'statement-block-intro nil t nil
6568 (if (eq char-after-ip ?{)
6569 (c-add-syntax 'block-open)))
6570 ;; CASE 17E: first statement in an in-expression block.
6571 ;; C.f. cases 4, 7B and 16A.
6572 ((setq placeholder (c-looking-at-inexpr-block
6573 (c-safe-position containing-sexp paren-state)
6575 (setq tmpsymbol (if (eq (car placeholder) 'inlambda)
6577 'statement-block-intro))
6578 (back-to-indentation)
6579 (if (= containing-sexp (point))
6580 (c-add-syntax tmpsymbol (point))
6581 (goto-char (cdr placeholder))
6582 (back-to-indentation)
6583 (c-add-stmt-syntax tmpsymbol nil t nil
6584 (c-most-enclosing-brace c-state-cache (point))
6585 (c-whack-state-after (point) paren-state))
6586 (if (/= (point) (cdr placeholder))
6587 (c-add-syntax (car placeholder))))
6588 (if (eq char-after-ip ?{)
6589 (c-add-syntax 'block-open)))
6590 ;; CASE 17F: first statement in an inline, or first
6591 ;; statement in a top-level defun. we can tell this is it
6592 ;; if there are no enclosing braces that haven't been
6593 ;; narrowed out by a class (i.e. don't use bod here).
6597 (c-narrow-out-enclosing-class paren-state containing-sexp)
6598 (not (c-most-enclosing-brace paren-state))))
6599 (c-backward-to-decl-anchor lim)
6600 (back-to-indentation)
6601 (c-add-syntax 'defun-block-intro (point)))
6602 ;; CASE 17G: First statement in a function declared inside
6603 ;; a normal block. This can occur in Pike and with
6604 ;; e.g. the gcc extensions. Might also trigger it with
6605 ;; some macros followed by blocks, and this gives sane
6606 ;; indentation then too. C.f. cases B.3 and 16F.
6608 (and (not (c-looking-at-bos))
6609 (eq (c-beginning-of-statement-1 lim nil nil t) 'same)
6610 (setq placeholder (point))))
6611 (back-to-indentation)
6612 (if (/= (point) containing-sexp)
6613 (goto-char placeholder))
6614 (c-add-stmt-syntax 'defun-block-intro nil t nil
6616 ;; CASE 17H: First statement in a block. C.f. case 16C.
6618 ;; If the block is preceded by a case/switch label on the
6619 ;; same line, we anchor at the first preceding label at
6620 ;; boi. The default handling in c-add-stmt-syntax is
6621 ;; really fixes it better, but we do like this to keep the
6622 ;; indentation compatible with version 5.28 and earlier.
6623 (while (and (/= (setq placeholder (point)) (c-point 'boi))
6624 (eq (c-beginning-of-statement-1 lim) 'label)))
6625 (goto-char placeholder)
6626 (if (looking-at c-label-kwds-regexp)
6627 (c-add-syntax 'statement-block-intro (point))
6628 (goto-char containing-sexp)
6629 ;; c-backward-to-block-anchor not necessary here; those
6630 ;; situations are handled in case 17I above.
6631 (c-add-stmt-syntax 'statement-block-intro nil t nil
6633 (if (eq char-after-ip ?{)
6634 (c-add-syntax 'block-open)))
6637 ;; now we need to look at any modifiers
6638 (goto-char indent-point)
6639 (skip-chars-forward " \t")
6640 ;; are we looking at a comment only line?
6641 (when (and (looking-at c-comment-start-regexp)
6642 (/= (c-forward-token-2 0 nil (c-point 'eol)) 0))
6643 (c-append-syntax 'comment-intro))
6644 ;; we might want to give additional offset to friends (in C++).
6645 (when (and c-opt-friend-key
6646 (looking-at c-opt-friend-key))
6647 (c-append-syntax 'friend))
6649 ;; Set syntactic-relpos.
6650 (let ((p c-syntactic-context))
6652 (if (integerp (car-safe (cdr-safe (car p))))
6654 (setq syntactic-relpos (car (cdr (car p))))
6659 ;; Start of or a continuation of a preprocessor directive?
6660 (if (and macro-start
6661 (eq macro-start (c-point 'boi))
6662 (not (and (c-major-mode-is 'pike-mode)
6663 (eq (char-after (1+ macro-start)) ?\"))))
6664 (c-append-syntax 'cpp-macro)
6665 (when (and c-syntactic-indentation-in-macros macro-start)
6668 (< syntactic-relpos macro-start)
6670 (assq 'arglist-intro c-syntactic-context)
6671 (assq 'arglist-cont c-syntactic-context)
6672 (assq 'arglist-cont-nonempty c-syntactic-context)
6673 (assq 'arglist-close c-syntactic-context))))
6674 ;; If inside a cpp expression, i.e. anywhere in a
6675 ;; cpp directive except a #define body, we only let
6676 ;; through the syntactic analysis that is internal
6677 ;; in the expression. That means the arglist
6678 ;; elements, if they are anchored inside the cpp
6680 (setq c-syntactic-context nil)
6681 (c-add-syntax 'cpp-macro-cont macro-start))
6682 (when (and (eq macro-start syntactic-relpos)
6683 (not (assq 'cpp-define-intro c-syntactic-context))
6685 (goto-char macro-start)
6686 (or (not (c-forward-to-cpp-define-body))
6687 (<= (point) (c-point 'boi indent-point)))))
6688 ;; Inside a #define body and the syntactic analysis is
6689 ;; anchored on the start of the #define. In this case
6690 ;; we add cpp-define-intro to get the extra
6691 ;; indentation of the #define body.
6692 (c-add-syntax 'cpp-define-intro)))))
6693 ;; return the syntax
6694 c-syntactic-context))))
6697 ;; Indentation calculation.
6699 (defun c-evaluate-offset (offset langelem symbol)
6700 ;; offset can be a number, a function, a variable, a list, or one of
6701 ;; the symbols + or -
6703 ((eq offset '+) c-basic-offset)
6704 ((eq offset '-) (- c-basic-offset))
6705 ((eq offset '++) (* 2 c-basic-offset))
6706 ((eq offset '--) (* 2 (- c-basic-offset)))
6707 ((eq offset '*) (/ c-basic-offset 2))
6708 ((eq offset '/) (/ (- c-basic-offset) 2))
6709 ((numberp offset) offset)
6710 ((functionp offset) (c-evaluate-offset
6712 (cons (car langelem)
6713 (car-safe (cdr langelem))))
6715 ((vectorp offset) offset)
6719 (while (and (not done) offset)
6720 (setq done (c-evaluate-offset (car offset) langelem symbol)
6721 offset (cdr offset)))
6722 (if (and c-strict-syntax-p (not done))
6723 (c-benign-error "No offset found for syntactic symbol %s" symbol))
6725 (t (symbol-value offset))
6728 (defun c-calc-offset (langelem)
6729 ;; Get offset from LANGELEM which is a list beginning with the
6730 ;; syntactic symbol and followed by any analysis data it provides.
6731 ;; That data may be zero or more elements, but if at least one is
6732 ;; given then the first is the relpos (or nil). The symbol is
6733 ;; matched against `c-offsets-alist' and the offset calculated from
6734 ;; that is returned.
6735 (let* ((symbol (car langelem))
6736 (match (assq symbol c-offsets-alist))
6737 (offset (cdr-safe match)))
6739 (setq offset (c-evaluate-offset offset langelem symbol))
6740 (if c-strict-syntax-p
6741 (c-benign-error "No offset found for syntactic symbol %s" symbol))
6743 (if (vectorp offset)
6745 (or (and (numberp offset) offset)
6746 (and (symbolp offset) (symbol-value offset))
6750 (defun c-get-offset (langelem)
6751 ;; This is a compatibility wrapper for `c-calc-offset' in case
6752 ;; someone is calling it directly. It takes an old style syntactic
6753 ;; element on the form (SYMBOL . RELPOS) and converts it to the new
6756 (c-calc-offset (list (car langelem) (cdr langelem)))
6757 (c-calc-offset langelem)))
6759 (defun c-get-syntactic-indentation (langelems)
6760 ;; Calculate the syntactic indentation from a syntactic description
6761 ;; as returned by `c-guess-syntax'.
6763 ;; Note that topmost-intro always has a relpos at bol, for
6764 ;; historical reasons. It's often used together with other symbols
6765 ;; that has more sane positions. Since we always use the first
6766 ;; found relpos, we rely on that these other symbols always precede
6767 ;; topmost-intro in the LANGELEMS list.
6768 (let ((indent 0) anchor)
6771 (let* ((c-syntactic-element (car langelems))
6772 (res (c-calc-offset c-syntactic-element)))
6775 ;; Got an absolute column that overrides any indentation
6776 ;; we've collected so far, but not the relative
6777 ;; indentation we might get for the nested structures
6778 ;; further down the langelems list.
6779 (setq indent (elt res 0)
6780 anchor (point-min)) ; A position at column 0.
6782 ;; Got a relative change of the current calculated
6784 (setq indent (+ indent res))
6786 ;; Use the anchor position from the first syntactic
6787 ;; element with one.
6789 (let ((relpos (car-safe (cdr (car langelems)))))
6791 (setq anchor relpos)))))
6793 (setq langelems (cdr langelems))))
6796 (+ indent (save-excursion
6802 (cc-provide 'cc-engine)
6804 ;;; cc-engine.el ends here