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.
70 ;; Used to modify the syntax of some characters. Currently used to
71 ;; mark the "<" and ">" of angle bracket parens with paren syntax.
73 ;; This property is used on single characters and is therefore
74 ;; always treated as front and rear nonsticky (or start and end open
75 ;; in XEmacs vocabulary). It's therefore installed on
76 ;; `text-property-default-nonsticky' if that variable exists (Emacs
79 ;; 'c-is-sws and 'c-in-sws
80 ;; Used by `c-forward-syntactic-ws' and `c-backward-syntactic-ws' to
81 ;; speed them up. See the comment blurb before `c-put-is-sws'
82 ;; below for further details.
85 ;; This property is used on single characters to mark positions with
86 ;; special syntactic relevance of various sorts. It's primary use
87 ;; is to avoid glitches when multiline constructs are refontified
88 ;; interactively (on font lock decoration level 3). It's cleared in
89 ;; a region before it's fontified and is then put on relevant chars
90 ;; in that region as they are encountered during the fontification.
91 ;; The value specifies the kind of position:
94 ;; Put on the last char of the token preceding each declaration
95 ;; inside a declaration style arglist (typically in a function
99 ;; Put on the last char of the token preceding a declaration.
100 ;; This is used in cases where declaration boundaries can't be
101 ;; recognized simply by looking for a token like ";" or "}".
102 ;; `c-type-decl-end-used' must be set if this is used (see also
103 ;; `c-find-decl-spots').
106 ;; Put on the commas that separate arguments in angle bracket
107 ;; arglists like C++ template arglists.
109 ;; 'c-decl-id-start and 'c-decl-type-start
110 ;; Put on the last char of the token preceding each declarator
111 ;; in the declarator list of a declaration. They are also used
112 ;; between the identifiers cases like enum declarations.
113 ;; 'c-decl-type-start is used when the declarators are types,
114 ;; 'c-decl-id-start otherwise.
117 ;; Used in AWK mode to mark the various kinds of newlines. See
124 (if (and (boundp 'byte-compile-dest-file
)
125 (stringp byte-compile-dest-file
))
126 (cons (file-name-directory byte-compile-dest-file
) load-path
)
128 (load "cc-bytecomp" nil t
)))
130 (cc-require 'cc-defs
)
131 (cc-require-when-compile 'cc-langs
)
132 (cc-require 'cc-vars
)
134 ;; Some functions/constants in cc-awk.el that are called/referenced here.
135 ;; (Can't use cc-require due to cyclicity.)
136 (cc-bytecomp-defun c-awk-unstick-NL-prop)
137 (cc-bytecomp-defun c-awk-clear-NL-props)
138 (cc-bytecomp-defvar awk-mode-syntax-table
)
139 (cc-bytecomp-defun c-awk-backward-syntactic-ws)
140 (cc-bytecomp-defun c-awk-after-logical-semicolon)
141 (cc-bytecomp-defun c-awk-NL-prop-not-set)
142 (cc-bytecomp-defun c-awk-completed-stmt-ws-ends-line-p)
143 (cc-bytecomp-defun c-awk-completed-stmt-ws-ends-prev-line-p)
144 (cc-bytecomp-defun c-awk-prev-line-incomplete-p)
145 (cc-bytecomp-defun c-awk-after-change)
147 ;; Silence the compiler.
148 (cc-bytecomp-defun buffer-syntactic-context) ; XEmacs
151 ;; Make declarations for all the `c-lang-defvar' variables in cc-langs.
153 (defmacro c-declare-lang-variables
()
155 ,@(mapcan (lambda (init)
157 `(defvar ,(car init
) nil
,(elt init
2))
158 `(defvar ,(car init
) nil
))
159 (make-variable-buffer-local ',(car init
))))
160 (cdr c-lang-variable-inits
))))
161 (c-declare-lang-variables)
164 ;;; Internal state variables.
166 ;; Internal state of hungry delete key feature
167 (defvar c-hungry-delete-key nil
)
168 (make-variable-buffer-local 'c-hungry-delete-key
)
170 ;; Internal state of auto newline feature.
171 (defvar c-auto-newline nil
)
172 (make-variable-buffer-local 'c-auto-newline
)
174 ;; Internal auto-newline/hungry-delete designation string for mode line.
175 (defvar c-auto-hungry-string nil
)
176 (make-variable-buffer-local 'c-auto-hungry-string
)
178 (defun c-calculate-state (arg prevstate
)
179 ;; Calculate the new state of PREVSTATE, t or nil, based on arg. If
180 ;; arg is nil or zero, toggle the state. If arg is negative, turn
181 ;; the state off, and if arg is positive, turn the state on
183 (zerop (setq arg
(prefix-numeric-value arg
))))
187 ;; Dynamically bound cache for `c-in-literal'.
188 (defvar c-in-literal-cache t
)
190 ;; Must be set in buffers where the `c-type' text property might be used
191 ;; with the value `c-decl-end'.
192 (defvar c-type-decl-end-used nil
)
193 (make-variable-buffer-local 'c-type-decl-end-used
)
196 ;;; Basic utility functions.
198 (defun c-syntactic-content (from to
)
199 ;; Return the given region as a string where all syntactic
200 ;; whitespace is removed or, where necessary, replaced with a single
204 (let* ((parts (list nil
)) (tail parts
) pos
)
205 (while (re-search-forward c-syntactic-ws-start to t
)
206 (goto-char (setq pos
(match-beginning 0)))
207 (c-forward-syntactic-ws to
)
210 (if (and (> pos from
)
212 (looking-at "\\w\\|\\s_")
215 (looking-at "\\w\\|\\s_")))
217 (setcdr tail
(list (buffer-substring-no-properties from pos
)
219 (setq tail
(cddr tail
)))
220 (setcdr tail
(list (buffer-substring-no-properties from pos
)))
221 (setq tail
(cdr tail
)))
222 (setq from
(point))))
223 (setcdr tail
(list (buffer-substring-no-properties from to
)))
224 (apply 'concat
(cdr parts
)))))
226 (defsubst c-keyword-sym
(keyword)
227 ;; Return non-nil if the string KEYWORD is a known keyword. More
228 ;; precisely, the value is the symbol for the keyword in
229 ;; `c-keywords-obarray'.
230 (intern-soft keyword c-keywords-obarray
))
232 (defsubst c-keyword-member
(keyword-sym lang-constant
)
233 ;; Return non-nil if the symbol KEYWORD-SYM, as returned by
234 ;; `c-keyword-sym', is a member of LANG-CONSTANT, which is the name
235 ;; of a language constant that ends with "-kwds". If KEYWORD-SYM is
236 ;; nil then the result is nil.
237 (get keyword-sym lang-constant
))
239 ;; String syntax chars, suitable for skip-syntax-(forward|backward).
240 (defconst c-string-syntax
(if (memq 'gen-string-delim c-emacs-features
)
244 ;; Regexp matching string start syntax.
245 (defconst c-string-limit-regexp
(if (memq 'gen-string-delim c-emacs-features
)
249 ;; Holds formatted error strings for the few cases where parse errors
251 (defvar c-parsing-error nil
)
252 (make-variable-buffer-local 'c-parsing-error
)
254 (defun c-echo-parsing-error (&optional quiet
)
255 ;; This function does not do any hidden buffer changes.
256 (when (and c-report-syntactic-errors c-parsing-error
(not quiet
))
257 (c-benign-error "%s" c-parsing-error
))
260 ;; Faces given to comments and string literals. This is used in some
261 ;; situations to speed up recognition; it isn't mandatory that font
262 ;; locking is in use. This variable is extended with the face in
263 ;; `c-doc-face-name' when fontification is activated in cc-fonts.el.
264 (defconst c-literal-faces
265 '(font-lock-comment-face font-lock-string-face
))
268 ;; Some debug tools to visualize various special positions. This
269 ;; debug code isn't as portable as the rest of CC Mode.
271 (cc-bytecomp-defun overlays-in)
272 (cc-bytecomp-defun overlay-get)
273 (cc-bytecomp-defun overlay-start)
274 (cc-bytecomp-defun overlay-end)
275 (cc-bytecomp-defun delete-overlay)
276 (cc-bytecomp-defun overlay-put)
277 (cc-bytecomp-defun make-overlay)
279 (defun c-debug-add-face (beg end face
)
280 (c-save-buffer-state ((overlays (overlays-in beg end
)) overlay
)
282 (setq overlay
(car overlays
)
283 overlays
(cdr overlays
))
284 (when (eq (overlay-get overlay
'face
) face
)
285 (setq beg
(min beg
(overlay-start overlay
))
286 end
(max end
(overlay-end overlay
)))
287 (delete-overlay overlay
)))
288 (overlay-put (make-overlay beg end
) 'face face
)))
290 (defun c-debug-remove-face (beg end face
)
291 (c-save-buffer-state ((overlays (overlays-in beg end
)) overlay
292 (ol-beg beg
) (ol-end end
))
294 (setq overlay
(car overlays
)
295 overlays
(cdr overlays
))
296 (when (eq (overlay-get overlay
'face
) face
)
297 (setq ol-beg
(min ol-beg
(overlay-start overlay
))
298 ol-end
(max ol-end
(overlay-end overlay
)))
299 (delete-overlay overlay
)))
301 (overlay-put (make-overlay ol-beg beg
) 'face face
))
303 (overlay-put (make-overlay end ol-end
) 'face face
))))
306 ;; `c-beginning-of-statement-1' and accompanying stuff.
308 ;; KLUDGE ALERT: c-maybe-labelp is used to pass information between
309 ;; c-crosses-statement-barrier-p and c-beginning-of-statement-1. A
310 ;; better way should be implemented, but this will at least shut up
311 ;; the byte compiler.
312 (defvar c-maybe-labelp nil
)
314 ;; New awk-compatible version of c-beginning-of-statement-1, ACM 2002/6/22
316 ;; Macros used internally in c-beginning-of-statement-1 for the
317 ;; automaton actions.
318 (defmacro c-bos-push-state
()
319 '(setq stack
(cons (cons state saved-pos
)
321 (defmacro c-bos-pop-state
(&optional do-if-done
)
322 `(if (setq state
(car (car stack
))
323 saved-pos
(cdr (car stack
))
328 (defmacro c-bos-pop-state-and-retry
()
329 '(throw 'loop
(setq state
(car (car stack
))
330 saved-pos
(cdr (car stack
))
331 ;; Throw nil if stack is empty, else throw non-nil.
333 (defmacro c-bos-save-pos
()
334 '(setq saved-pos
(vector pos tok ptok pptok
)))
335 (defmacro c-bos-restore-pos
()
336 '(unless (eq (elt saved-pos
0) start
)
337 (setq pos
(elt saved-pos
0)
338 tok
(elt saved-pos
1)
339 ptok
(elt saved-pos
2)
340 pptok
(elt saved-pos
3))
343 (defmacro c-bos-save-error-info
(missing got
)
344 `(setq saved-pos
(vector pos
,missing
,got
)))
345 (defmacro c-bos-report-error
()
347 (setq c-parsing-error
348 (format "No matching `%s' found for `%s' on line %d"
351 (1+ (count-lines (point-min)
352 (c-point 'bol
(elt saved-pos
0))))))))
354 (defun c-beginning-of-statement-1 (&optional lim ignore-labels
356 "Move to the start of the current statement or declaration, or to
357 the previous one if already at the beginning of one. Only
358 statements/declarations on the same level are considered, i.e. don't
359 move into or out of sexps (not even normal expression parentheses).
361 Stop at statement continuation tokens like \"else\", \"catch\",
362 \"finally\" and the \"while\" in \"do ... while\" if the start point
363 is within the continuation. If starting at such a token, move to the
364 corresponding statement start. If at the beginning of a statement,
365 move to the closest containing statement if there is any. This might
366 also stop at a continuation clause.
368 Labels are treated as separate statements if IGNORE-LABELS is non-nil.
369 The function is not overly intelligent in telling labels from other
370 uses of colons; if used outside a statement context it might trip up
371 on e.g. inherit colons, so IGNORE-LABELS should be used then. There
372 should be no such mistakes in a statement context, however.
374 Macros are ignored unless point is within one, in which case the
375 content of the macro is treated as normal code. Aside from any normal
376 statement starts found in it, stop at the first token of the content
377 in the macro, i.e. the expression of an \"#if\" or the start of the
378 definition in a \"#define\". Also stop at start of macros before
381 Return 'label if stopped at a label, 'same if stopped at the beginning
382 of the current statement, 'up if stepped to a containing statement,
383 'previous if stepped to a preceding statement, 'beginning if stepped
384 from a statement continuation clause to its start clause, or 'macro if
385 stepped to a macro start. Note that 'same and not 'label is returned
386 if stopped at the same label without crossing the colon character.
388 LIM may be given to limit the search. If the search hits the limit,
389 point will be left at the closest following token, or at the start
390 position if that is less ('same is returned in this case).
392 NOERROR turns off error logging to `c-parsing-error'.
394 Normally only ';' is considered to delimit statements, but if
395 COMMA-DELIM is non-nil then ',' is treated likewise."
397 ;; The bulk of this function is a pushdown automaton that looks at statement
398 ;; boundaries and the tokens (such as "while") in c-opt-block-stmt-key. Its
399 ;; purpose is to keep track of nested statements, ensuring that such
400 ;; statments are skipped over in their entirety (somewhat akin to what C-M-p
401 ;; does with nested braces/brackets/parentheses).
403 ;; Note: The position of a boundary is the following token.
405 ;; Beginning with the current token (the one following point), move back one
406 ;; sexp at a time (where a sexp is, more or less, either a token or the
407 ;; entire contents of a brace/bracket/paren pair). Each time a statement
408 ;; boundary is crossed or a "while"-like token is found, update the state of
409 ;; the PDA. Stop at the beginning of a statement when the stack (holding
410 ;; nested statement info) is empty and the position has been moved.
412 ;; The following variables constitute the PDA:
414 ;; sym: This is either the "while"-like token (e.g. 'for) we've just
415 ;; scanned back over, 'boundary if we've just gone back over a
416 ;; statement boundary, or nil otherwise.
417 ;; state: takes one of the values (nil else else-boundary while
418 ;; while-boundary catch catch-boundary).
419 ;; nil means "no "while"-like token yet scanned".
420 ;; 'else, for example, means "just gone back over an else".
421 ;; 'else-boundary means "just gone back over a statement boundary
422 ;; immediately after having gone back over an else".
423 ;; saved-pos: A vector of either saved positions (tok ptok pptok, etc.) or
424 ;; of error reporting information.
425 ;; stack: The stack onto which the PDA pushes its state. Each entry
426 ;; consists of a saved value of state and saved-pos. An entry is
427 ;; pushed when we move back over a "continuation" token (e.g. else)
428 ;; and popped when we encounter the corresponding opening token
432 ;; The following diagram briefly outlines the PDA.
435 ;; "else": Push state, goto state `else'.
436 ;; "while": Push state, goto state `while'.
437 ;; "catch" or "finally": Push state, goto state `catch'.
438 ;; boundary: Pop state.
439 ;; other: Do nothing special.
442 ;; boundary: Goto state `else-boundary'.
443 ;; other: Error, pop state, retry token.
445 ;; State `else-boundary':
447 ;; boundary: Error, pop state.
448 ;; other: See common state.
451 ;; boundary: Save position, goto state `while-boundary'.
452 ;; other: Pop state, retry token.
454 ;; State `while-boundary':
456 ;; boundary: Restore position if it's not at start, pop state. [*see below]
457 ;; other: See common state.
460 ;; boundary: Goto state `catch-boundary'.
461 ;; other: Error, pop state, retry token.
463 ;; State `catch-boundary':
465 ;; "catch": Goto state `catch'.
466 ;; boundary: Error, pop state.
467 ;; other: See common state.
469 ;; [*] In the `while-boundary' state, we had pushed a 'while state, and were
470 ;; searching for a "do" which would have opened a do-while. If we didn't
471 ;; find it, we discard the analysis done since the "while", go back to this
472 ;; token in the buffer and restart the scanning there, this time WITHOUT
473 ;; pushing the 'while state onto the stack.
475 ;; In addition to the above there is some special handling of labels
478 (let ((case-fold-search nil
)
481 (delims (if comma-delim
'(?\
; ?,) '(?\;)))
482 (c-stmt-delim-chars (if comma-delim
483 c-stmt-delim-chars-with-comma
485 pos
; Current position.
486 boundary-pos
; Position of last stmt boundary character (e.g. ;).
487 after-labels-pos
; Value of tok after first found colon.
488 last-label-pos
; Value of tok after last found colon.
489 sym
; Symbol just scanned back over (e.g. 'while or
490 ; 'boundary). See above
491 state
; Current state in the automaton. See above.
492 saved-pos
; Current saved positions. See above
493 stack
; Stack of conses (state . saved-pos).
494 (cond-key (or c-opt-block-stmt-key
; regexp which matches "for", "if", etc.
495 "\\<\\>")) ; Matches nothing.
496 (ret 'same
) ; Return value.
497 tok ptok pptok
; Pos of last three sexps or bounds.
498 c-in-literal-cache c-maybe-labelp saved
)
501 (if lim
(narrow-to-region lim
(point-max)))
504 (and (c-beginning-of-macro)
506 (setq macro-start
(point)))
508 ;; Try to skip back over unary operator characters, to register
512 (c-backward-syntactic-ws) ; might go back an awk-mode virtual semicolon, here.
513 ; How about using c-awk-NL-prop for AWK Mode, here.
514 ; Something like c-awk-backward-syntactic-ws.
515 ; 2002/6/22. Doesn't matter! Leave it as it is.
516 (/= (skip-chars-backward "-+!*&~@`#") 0))) ; ACM, 2002/5/31;
520 ;; Skip back over any semicolon here. If it was a bare semicolon, we're
521 ;; done. Later on we ignore the boundaries for statements that doesn't
522 ;; contain any sexp. The only thing that is affected is that the error
523 ;; checking is a little less strict, and we really don't bother.
524 (if (and (memq (char-before) delims
)
525 (progn (forward-char -
1)
527 (if (c-mode-is-new-awk-p)
528 (c-awk-backward-syntactic-ws)
529 (c-backward-syntactic-ws))
530 (or (memq (char-before) delims
)
531 (memq (char-before) '(?
: nil
))
532 (eq (char-syntax (char-before)) ?\
()
533 (and (c-mode-is-new-awk-p)
534 (c-awk-after-logical-semicolon))))) ; ACM 2002/6/22
535 ;; ACM, 2002/7/20: What about giving a limit to the above function?
536 ;; ACM, 2003/6/16: The above two lines (checking for
537 ;; awk-logical-semicolon) are probably redundant after rewriting
538 ;; c-awk-backward-syntactic-ws.
542 ;; Begin at start and not pos to detect macros if we stand
543 ;; directly after the #.
545 (if (looking-at "\\<\\|\\W")
546 ;; Record this as the first token if not starting inside it.
549 ;; The following while loop goes back one sexp (balanced parens,
550 ;; etc. with contents, or symbol or suchlike) each iteration. This
551 ;; movement is accomplished with a call to scan-sexps approx 130 lines
554 (catch 'loop
;; Throw nil to break, non-nil to continue.
556 ;; Check for macro start. Take this out for AWK Mode (ACM, 2002/5/31)
557 ;; NO!! just make sure macro-start is nil in AWK Mode (ACM, 2002/6/22)
558 ;; It always is (ACM, 2002/6/23)
561 (progn (skip-chars-backward " \t")
562 (eq (char-before) ?
#))
563 (progn (setq saved
(1- (point)))
565 (not (eq (char-before (1- (point))) ?
\\)))
566 (looking-at c-opt-cpp-start
)
567 (progn (skip-chars-forward " \t")
568 (eq (point) saved
))))
570 (if (and (c-forward-to-cpp-define-body)
571 (progn (c-forward-syntactic-ws start
)
573 ;; Stop at the first token in the content of the macro.
575 ignore-labels t
) ; Avoid the label check on exit.
581 ;; Do a round through the automaton if we've just passed a
582 ;; statement boundary or passed a "while"-like token.
584 (and (looking-at cond-key
)
585 (setq sym
(intern (match-string 1)))))
587 (when (and (< pos start
) (null stack
))
590 ;; The PDA state handling.
592 ;; Refer to the description of the PDA in the openining
593 ;; comments. In the following OR form, the first leaf
594 ;; attempts to handles one of the specific actions detailed
595 ;; (e.g., finding token "if" whilst in state `else-boundary').
596 ;; We drop through to the second leaf (which handles common
597 ;; state) if no specific handler is found in the first cond.
598 ;; If a parsing error is detected (e.g. an "else" with no
599 ;; preceding "if"), we throw to the enclosing catch.
601 ;; Note that the (eq state 'else) means
602 ;; "we've just passed an else", NOT "we're looking for an
606 (if (eq sym
'boundary
)
607 (setq state
'else-boundary
)
609 (c-bos-pop-state-and-retry)))
611 ((eq state
'else-boundary
)
613 (c-bos-pop-state (setq ret
'beginning
)))
619 (if (and (eq sym
'boundary
)
620 ;; Since this can cause backtracking we do a
621 ;; little more careful analysis to avoid it:
622 ;; If there's a label in front of the while
623 ;; it can't be part of a do-while.
624 (not after-labels-pos
))
625 (progn (c-bos-save-pos)
626 (setq state
'while-boundary
))
627 (c-bos-pop-state-and-retry))) ; Can't be a do-while
629 ((eq state
'while-boundary
)
631 (c-bos-pop-state (setq ret
'beginning
)))
632 ((eq sym
'boundary
) ; isn't a do-while
633 (c-bos-restore-pos) ; the position of the while
634 (c-bos-pop-state)))) ; no longer searching for do.
637 (if (eq sym
'boundary
)
638 (setq state
'catch-boundary
)
640 (c-bos-pop-state-and-retry)))
642 ((eq state
'catch-boundary
)
645 (c-bos-pop-state (setq ret
'beginning
)))
650 (c-bos-pop-state)))))
652 ;; This is state common. We get here when the previous
653 ;; cond statement found no particular state handler.
654 (cond ((eq sym
'boundary
)
655 ;; If we have a boundary at the start
656 ;; position we push a frame to go to the
657 ;; previous statement.
663 (c-bos-save-error-info 'if
'else
)
666 (when (or (not pptok
)
667 (memq (char-after pptok
) delims
)
668 (and (c-mode-is-new-awk-p)
670 ;; might we be calling this from
671 ;; c-awk-after-if-do-for-while-condition-p?
672 ;; If so, avoid infinite recursion.
673 (and (eq (point) start
)
674 (c-awk-NL-prop-not-set))
675 ;; The following may recursively
676 ;; call this function.
677 (c-awk-completed-stmt-ws-ends-line-p pptok
))))
678 ;; Since this can cause backtracking we do a
679 ;; little more careful analysis to avoid it: If
680 ;; the while isn't followed by a semicolon it
681 ;; can't be a do-while.
682 ;; ACM, 2002/5/31; IT CAN IN AWK Mode. ;-(
684 (setq state
'while
)))
685 ((memq sym
'(catch finally
))
687 (c-bos-save-error-info 'try sym
)
688 (setq state
'catch
))))
691 ;; We're either past a statement boundary or at the
692 ;; start of a statement, so throw away any label data
693 ;; for the previous one.
694 (setq after-labels-pos nil
696 c-maybe-labelp nil
))))
698 ;; Step to the previous sexp, but not if we crossed a
699 ;; boundary, since that doesn't consume an sexp.
700 (if (eq sym
'boundary
)
703 ;; HERE IS THE SINGLE PLACE INSIDE THE PDA LOOP WHERE WE MOVE
704 ;; BACKWARDS THROUGH THE SOURCE. The following loop goes back
705 ;; one sexp and then only loops in special circumstances (line
706 ;; continuations and skipping past entire macros).
709 (or (c-safe (goto-char (scan-sexps (point) -
1)) t
)
710 ;; Give up if we hit an unbalanced block.
711 ;; Since the stack won't be empty the code
712 ;; below will report a suitable error.
714 (cond ((looking-at "\\\\$")
715 ;; Step again if we hit a line continuation.
718 ;; If we started inside a macro then this
719 ;; sexp is always interesting.
721 ((not (c-mode-is-new-awk-p)) ; Changed from t, ACM 2002/6/25
722 ;; Otherwise check that we didn't step
723 ;; into a macro from the end.
726 (and (c-beginning-of-macro)
729 (goto-char macro-start
)
732 ;; Did the last movement by a sexp cross a statement boundary?
733 (when (save-excursion
734 (if (if (eq (char-after) ?
{)
735 (c-looking-at-inexpr-block lim nil
)
736 (looking-at "\\s\("))
738 ;; Should not include the paren sexp we've
739 ;; passed over in the boundary check.
740 (if (> (point) (- pos
100))
743 ;; Find its end position this way instead of
744 ;; moving forward if the sexp is large.
748 (goto-char (1+ (c-down-list-backward)))
750 ;; Check that we didn't step into
751 ;; a macro from the end.
754 (and (c-beginning-of-macro)
757 (goto-char macro-start
)
760 (setq boundary-pos
(c-crosses-statement-barrier-p
767 (throw 'loop t
))) ; like a C "continue". Analyze the next sexp.
769 (when (and (numberp c-maybe-labelp
) (not ignore-labels
))
770 ;; c-crosses-statement-barrier-p has found a colon, so
771 ;; we might be in a label now.
772 (if (not after-labels-pos
)
773 (setq after-labels-pos tok
))
774 (setq last-label-pos tok
778 (when (and c-opt-method-key
779 (setq saved
(c-in-method-def-p)))
781 ignore-labels t
) ; Avoid the label check on exit.
784 ;; We've moved back by a sexp, so update the token positions.
789 pos tok
))) ; Not nil (for the while loop).
791 ;; If the stack isn't empty there might be errors to report.
793 (if (and (vectorp saved-pos
) (eq (length saved-pos
) 3))
794 (c-bos-report-error))
795 (setq saved-pos
(cdr (car stack
))
798 (when (and (eq ret
'same
)
799 (not (memq sym
'(boundary ignore nil
))))
800 ;; Need to investigate closer whether we've crossed
801 ;; between a substatement and its containing statement.
802 (if (setq saved
(if (looking-at c-block-stmt-1-key
)
805 (cond ((> start saved
) (setq pos saved
))
806 ((= start saved
) (setq ret
'up
)))))
808 (when (and c-maybe-labelp
810 (not (eq ret
'beginning
))
812 ;; We're in a label. Maybe we should step to the statement
814 (if (< after-labels-pos start
)
815 (setq pos after-labels-pos
)
817 (if (< last-label-pos start
)
818 (setq pos last-label-pos
)))))
820 ;; Skip over the unary operators that can start the statement.
823 (c-backward-syntactic-ws)
824 (/= (skip-chars-backward "-+!*&~@`#") 0)) ; Hopefully the # won't hurt awk.
829 (defun c-crosses-statement-barrier-p (from to
)
830 "Return non-nil if buffer positions FROM to TO cross one or more
831 statement or declaration boundaries. The returned value is actually
832 the position of the earliest boundary char. FROM must not be within
835 The variable `c-maybe-labelp' is set to the position of the first `:' that
836 might start a label (i.e. not part of `::' and not preceded by `?'). If a
837 single `?' is found, then `c-maybe-labelp' is cleared."
838 (let ((skip-chars c-stmt-delim-chars
)
843 (while (progn (skip-chars-forward skip-chars to
)
845 (if (setq lit-range
(c-literal-limits from
)) ; Have we landed in a string/comment?
846 (progn (goto-char (setq from
(cdr lit-range
)))
847 (if (and (c-mode-is-new-awk-p) (bolp)) ; ACM 2002/7/17. Make sure we
848 (backward-char))) ; don't skip over a virtual semi-colon after an awk comment. :-(
849 (cond ((eq (char-after) ?
:)
851 (if (and (eq (char-after) ?
:)
853 ;; Ignore scope operators.
855 (setq c-maybe-labelp
(1- (point)))))
856 ((eq (char-after) ??
)
857 ;; A question mark. Can't be a label, so stop
858 ;; looking for more : and ?.
859 (setq c-maybe-labelp nil
860 skip-chars
(substring c-stmt-delim-chars
0 -
2)))
861 ((and (eolp) ; Can only happen in AWK Mode
862 (not (c-awk-completed-stmt-ws-ends-line-p)))
864 ((and (c-mode-is-new-awk-p)
865 (bolp) lit-range
; awk: comment/string ended prev line.
866 (not (c-awk-completed-stmt-ws-ends-prev-line-p))))
867 (t (throw 'done
(point))))))
871 ;; A set of functions that covers various idiosyncrasies in
872 ;; implementations of `forward-comment'.
874 ;; Note: Some emacsen considers incorrectly that any line comment
875 ;; ending with a backslash continues to the next line. I can't think
876 ;; of any way to work around that in a reliable way without changing
877 ;; the buffer, though. Suggestions welcome. ;) (No, temporarily
878 ;; changing the syntax for backslash doesn't work since we must treat
879 ;; escapes in string literals correctly.)
881 (defun c-forward-single-comment ()
882 "Move forward past whitespace and the closest following comment, if any.
883 Return t if a comment was found, nil otherwise. In either case, the
884 point is moved past the following whitespace. Line continuations,
885 i.e. a backslashes followed by line breaks, are treated as whitespace.
886 The line breaks that end line comments are considered to be the
887 comment enders, so the point will be put on the beginning of the next
888 line if it moved past a line comment.
890 This function does not do any hidden buffer changes."
892 (let ((start (point)))
893 (when (looking-at "\\([ \t\n\r\f\v]\\|\\\\[\n\r]\\)+")
894 (goto-char (match-end 0)))
896 (when (forward-comment 1)
898 ;; Some emacsen (e.g. XEmacs 21) return t when moving
902 ;; Emacs includes the ending newline in a b-style (c++)
903 ;; comment, but XEmacs doesn't. We depend on the Emacs
904 ;; behavior (which also is symmetric).
905 (if (and (eolp) (elt (parse-partial-sexp start
(point)) 7))
906 (condition-case nil
(forward-char 1)))
910 (defsubst c-forward-comments
()
911 "Move forward past all following whitespace and comments.
912 Line continuations, i.e. a backslashes followed by line breaks, are
913 treated as whitespace.
915 This function does not do any hidden buffer changes."
918 ;; If forward-comment in at least XEmacs 21 is given a large
919 ;; positive value, it'll loop all the way through if it hits
921 (and (forward-comment 5)
922 ;; Some emacsen (e.g. XEmacs 21) return t when moving
926 (when (looking-at "\\\\[\n\r]")
930 (defun c-backward-single-comment ()
931 "Move backward past whitespace and the closest preceding comment, if any.
932 Return t if a comment was found, nil otherwise. In either case, the
933 point is moved past the preceding whitespace. Line continuations,
934 i.e. a backslashes followed by line breaks, are treated as whitespace.
935 The line breaks that end line comments are considered to be the
936 comment enders, so the point cannot be at the end of the same line to
937 move over a line comment.
939 This function does not do any hidden buffer changes."
941 (let ((start (point)))
942 ;; When we got newline terminated comments, forward-comment in all
943 ;; supported emacsen so far will stop at eol of each line not
944 ;; ending with a comment when moving backwards. This corrects for
945 ;; that, and at the same time handles line continuations.
947 (skip-chars-backward " \t\n\r\f\v")
948 (and (looking-at "[\n\r]")
949 (eq (char-before) ?
\\)
954 ;; Some emacsen (e.g. Emacs 19.34) return t when moving
958 ;; Leave point after the closest following newline if we've
959 ;; backed up over any above, since forward-comment won't move
960 ;; backward over a line comment if point is at the end of the
962 (re-search-forward "\\=\\s *[\n\r]" start t
)
964 (if (if (forward-comment -
1)
966 ;; If forward-comment above succeeded and we're at eol
967 ;; then the newline we moved over above didn't end a
968 ;; line comment, so we give it another go.
972 ;; Emacs <= 20 and XEmacs move back over the closer of a
973 ;; block comment that lacks an opener.
974 (if (looking-at "\\*/")
975 (progn (forward-char 2) nil
)
978 (defsubst c-backward-comments
()
979 "Move backward past all preceding whitespace and comments.
980 Line continuations, i.e. a backslashes followed by line breaks, are
981 treated as whitespace. The line breaks that end line comments are
982 considered to be the comment enders, so the point cannot be at the end
983 of the same line to move over a line comment.
985 This function does not do any hidden buffer changes."
987 (let ((start (point)))
989 ;; `forward-comment' in some emacsen (e.g. Emacs 19.34)
990 ;; return t when moving backwards at bob.
993 (if (forward-comment -
1)
994 (if (looking-at "\\*/")
995 ;; Emacs <= 20 and XEmacs move back over the
996 ;; closer of a block comment that lacks an opener.
997 (progn (forward-char 2) nil
)
1000 ;; XEmacs treats line continuations as whitespace but
1001 ;; only in the backward direction, which seems a bit
1002 ;; odd. Anyway, this is necessary for Emacs.
1003 (when (and (looking-at "[\n\r]")
1004 (eq (char-before) ?
\\)
1010 ;; Basic handling of preprocessor directives.
1012 ;; This is a dynamically bound cache used together with
1013 ;; `c-query-macro-start' and `c-query-and-set-macro-start'. It only
1014 ;; works as long as point doesn't cross a macro boundary.
1015 (defvar c-macro-start
'unknown
)
1017 (defsubst c-query-and-set-macro-start
()
1018 ;; This function does not do any hidden buffer changes.
1019 (if (symbolp c-macro-start
)
1020 (setq c-macro-start
(save-excursion
1021 (and (c-beginning-of-macro)
1025 (defsubst c-query-macro-start
()
1026 ;; This function does not do any hidden buffer changes.
1027 (if (symbolp c-macro-start
)
1029 (and (c-beginning-of-macro)
1033 (defun c-beginning-of-macro (&optional lim
)
1034 "Go to the beginning of a preprocessor directive.
1035 Leave point at the beginning of the directive and return t if in one,
1036 otherwise return nil and leave point unchanged.
1038 This function does not do any hidden buffer changes."
1039 (when c-opt-cpp-prefix
1040 (let ((here (point)))
1042 (if lim
(narrow-to-region lim
(point-max)))
1044 (while (eq (char-before (1- (point))) ?
\\)
1046 (back-to-indentation)
1047 (if (and (<= (point) here
)
1048 (looking-at c-opt-cpp-start
))
1053 (defun c-end-of-macro ()
1054 "Go to the end of a preprocessor directive.
1055 More accurately, move point to the end of the closest following line
1056 that doesn't end with a line continuation backslash.
1058 This function does not do any hidden buffer changes."
1061 (when (and (eq (char-before) ?
\\)
1066 (defun c-forward-to-cpp-define-body ()
1067 ;; Assuming point is at the "#" that introduces a preprocessor
1068 ;; directive, it's moved forward to the start of the definition body
1069 ;; if it's a "#define". Non-nil is returned in this case, in all
1070 ;; other cases nil is returned and point isn't moved.
1071 (when (and (looking-at
1073 "define[ \t]+\\(\\sw\\|_\\)+\\(\([^\)]*\)\\)?"
1074 "\\([ \t]\\|\\\\\n\\)*"))
1075 (not (= (match-end 0) (c-point 'eol
))))
1076 (goto-char (match-end 0))))
1079 ;; Tools for skipping over syntactic whitespace.
1081 ;; The following functions use text properties to cache searches over
1082 ;; large regions of syntactic whitespace. It works as follows:
1084 ;; o If a syntactic whitespace region contains anything but simple
1085 ;; whitespace (i.e. space, tab and line breaks), the text property
1086 ;; `c-in-sws' is put over it. At places where we have stopped
1087 ;; within that region there's also a `c-is-sws' text property.
1088 ;; That since there typically are nested whitespace inside that
1089 ;; must be handled separately, e.g. whitespace inside a comment or
1090 ;; cpp directive. Thus, from one point with `c-is-sws' it's safe
1091 ;; to jump to another point with that property within the same
1092 ;; `c-in-sws' region. It can be likened to a ladder where
1093 ;; `c-in-sws' marks the bars and `c-is-sws' the rungs.
1095 ;; o The `c-is-sws' property is put on the simple whitespace chars at
1096 ;; a "rung position" and also maybe on the first following char.
1097 ;; As many characters as can be conveniently found in this range
1098 ;; are marked, but no assumption can be made that the whole range
1099 ;; is marked (it could be clobbered by later changes, for
1102 ;; Note that some part of the beginning of a sequence of simple
1103 ;; whitespace might be part of the end of a preceding line comment
1104 ;; or cpp directive and must not be considered part of the "rung".
1105 ;; Such whitespace is some amount of horizontal whitespace followed
1106 ;; by a newline. In the case of cpp directives it could also be
1107 ;; two newlines with horizontal whitespace between them.
1109 ;; The reason to include the first following char is to cope with
1110 ;; "rung positions" that doesn't have any ordinary whitespace. If
1111 ;; `c-is-sws' is put on a token character it does not have
1112 ;; `c-in-sws' set simultaneously. That's the only case when that
1113 ;; can occur, and the reason for not extending the `c-in-sws'
1114 ;; region to cover it is that the `c-in-sws' region could then be
1115 ;; accidentally merged with a following one if the token is only
1116 ;; one character long.
1118 ;; o On buffer changes the `c-in-sws' and `c-is-sws' properties are
1119 ;; removed in the changed region. If the change was inside
1120 ;; syntactic whitespace that means that the "ladder" is broken, but
1121 ;; a later call to `c-forward-sws' or `c-backward-sws' will use the
1122 ;; parts on either side and use an ordinary search only to "repair"
1125 ;; Special care needs to be taken if a region is removed: If there
1126 ;; are `c-in-sws' on both sides of it which do not connect inside
1127 ;; the region then they can't be joined. If e.g. a marked macro is
1128 ;; broken, syntactic whitespace inside the new text might be
1129 ;; marked. If those marks would become connected with the old
1130 ;; `c-in-sws' range around the macro then we could get a ladder
1131 ;; with one end outside the macro and the other at some whitespace
1134 ;; The main motivation for this system is to increase the speed in
1135 ;; skipping over the large whitespace regions that can occur at the
1136 ;; top level in e.g. header files that contain a lot of comments and
1137 ;; cpp directives. For small comments inside code it's probably
1138 ;; slower than using `forward-comment' straightforwardly, but speed is
1139 ;; not a significant factor there anyway.
1141 ; (defface c-debug-is-sws-face
1142 ; '((t (:background "GreenYellow")))
1143 ; "Debug face to mark the `c-is-sws' property.")
1144 ; (defface c-debug-in-sws-face
1145 ; '((t (:underline t)))
1146 ; "Debug face to mark the `c-in-sws' property.")
1148 ; (defun c-debug-put-sws-faces ()
1149 ; ;; Put the sws debug faces on all the `c-is-sws' and `c-in-sws'
1150 ; ;; properties in the buffer.
1154 ; (goto-char (point-min))
1155 ; (setq in-face (if (get-text-property (point) 'c-is-sws)
1158 ; (goto-char (next-single-property-change
1159 ; (point) 'c-is-sws nil (point-max)))
1162 ; (c-debug-add-face in-face (point) 'c-debug-is-sws-face)
1163 ; (setq in-face nil))
1164 ; (setq in-face (point)))
1166 ; (goto-char (point-min))
1167 ; (setq in-face (if (get-text-property (point) 'c-in-sws)
1170 ; (goto-char (next-single-property-change
1171 ; (point) 'c-in-sws nil (point-max)))
1174 ; (c-debug-add-face in-face (point) 'c-debug-in-sws-face)
1175 ; (setq in-face nil))
1176 ; (setq in-face (point)))
1179 (defmacro c-debug-sws-msg
(&rest args
)
1183 (defmacro c-put-is-sws
(beg end
)
1184 `(let ((beg ,beg
) (end ,end
))
1185 (put-text-property beg end
'c-is-sws t
)
1186 ,@(when (facep 'c-debug-is-sws-face
)
1187 `((c-debug-add-face beg end
'c-debug-is-sws-face
)))))
1189 (defmacro c-put-in-sws
(beg end
)
1190 `(let ((beg ,beg
) (end ,end
))
1191 (put-text-property beg end
'c-in-sws t
)
1192 ,@(when (facep 'c-debug-is-sws-face
)
1193 `((c-debug-add-face beg end
'c-debug-in-sws-face
)))))
1195 (defmacro c-remove-is-sws
(beg end
)
1196 `(let ((beg ,beg
) (end ,end
))
1197 (remove-text-properties beg end
'(c-is-sws nil
))
1198 ,@(when (facep 'c-debug-is-sws-face
)
1199 `((c-debug-remove-face beg end
'c-debug-is-sws-face
)))))
1201 (defmacro c-remove-in-sws
(beg end
)
1202 `(let ((beg ,beg
) (end ,end
))
1203 (remove-text-properties beg end
'(c-in-sws nil
))
1204 ,@(when (facep 'c-debug-is-sws-face
)
1205 `((c-debug-remove-face beg end
'c-debug-in-sws-face
)))))
1207 (defmacro c-remove-is-and-in-sws
(beg end
)
1208 `(let ((beg ,beg
) (end ,end
))
1209 (remove-text-properties beg end
'(c-is-sws nil c-in-sws nil
))
1210 ,@(when (facep 'c-debug-is-sws-face
)
1211 `((c-debug-remove-face beg end
'c-debug-is-sws-face
)
1212 (c-debug-remove-face beg end
'c-debug-in-sws-face
)))))
1214 (defsubst c-invalidate-sws-region-after
(beg end
)
1215 ;; Called from `after-change-functions'. Note that if
1216 ;; `c-forward-sws' or `c-backward-sws' are used outside
1217 ;; `c-save-buffer-state' or similar then this will remove the cache
1218 ;; properties right after they're added.
1221 ;; Adjust the end to remove the properties in any following simple
1222 ;; ws up to and including the next line break, if there is any
1223 ;; after the changed region. This is necessary e.g. when a rung
1224 ;; marked empty line is converted to a line comment by inserting
1225 ;; "//" before the line break. In that case the line break would
1226 ;; keep the rung mark which could make a later `c-backward-sws'
1227 ;; move into the line comment instead of over it.
1229 (skip-chars-forward " \t\f\v")
1230 (when (and (eolp) (not (eobp)))
1231 (setq end
(1+ (point)))))
1233 (when (and (= beg end
)
1234 (get-text-property beg
'c-in-sws
)
1236 (get-text-property (1- beg
) 'c-in-sws
))
1237 ;; Ensure that an `c-in-sws' range gets broken. Note that it isn't
1238 ;; safe to keep a range that was continuous before the change. E.g:
1244 ;; There can be a "ladder" between "#" and "b". Now, if the newline
1245 ;; after "foo" is removed then "bar" will become part of the cpp
1246 ;; directive instead of a syntactically relevant token. In that
1247 ;; case there's no longer syntactic ws from "#" to "b".
1248 (setq beg
(1- beg
)))
1250 (c-debug-sws-msg "c-invalidate-sws-region-after [%s..%s]" beg end
)
1251 (c-remove-is-and-in-sws beg end
))
1253 (defun c-forward-sws ()
1254 ;; Used by `c-forward-syntactic-ws' to implement the unbounded search.
1256 (let (;; `rung-pos' is set to a position as early as possible in the
1257 ;; unmarked part of the simple ws region.
1258 (rung-pos (point)) next-rung-pos rung-end-pos last-put-in-sws-pos
1259 rung-is-marked next-rung-is-marked simple-ws-end
1260 ;; `safe-start' is set when it's safe to cache the start position.
1261 ;; It's not set if we've initially skipped over comments and line
1262 ;; continuations since we might have gone out through the end of a
1263 ;; macro then. This provision makes `c-forward-sws' not populate the
1264 ;; cache in the majority of cases, but otoh is `c-backward-sws' by far
1268 ;; Skip simple ws and do a quick check on the following character to see
1269 ;; if it's anything that can't start syntactic ws, so we can bail out
1270 ;; early in the majority of cases when there just are a few ws chars.
1271 (skip-chars-forward " \t\n\r\f\v")
1272 (when (looking-at c-syntactic-ws-start
)
1274 (setq rung-end-pos
(min (1+ (point)) (point-max)))
1275 (if (setq rung-is-marked
(text-property-any rung-pos rung-end-pos
1277 ;; Find the last rung position to avoid setting properties in all
1278 ;; the cases when the marked rung is complete.
1279 ;; (`next-single-property-change' is certain to move at least one
1281 (setq rung-pos
(1- (next-single-property-change
1282 rung-is-marked
'c-is-sws nil rung-end-pos
)))
1283 ;; Got no marked rung here. Since the simple ws might have started
1284 ;; inside a line comment or cpp directive we must set `rung-pos' as
1285 ;; high as possible.
1286 (setq rung-pos
(point)))
1291 (when (and rung-is-marked
1292 (get-text-property (point) 'c-in-sws
))
1294 ;; The following search is the main reason that `c-in-sws'
1295 ;; and `c-is-sws' aren't combined to one property.
1296 (goto-char (next-single-property-change
1297 (point) 'c-in-sws nil
(point-max)))
1298 (unless (get-text-property (point) 'c-is-sws
)
1299 ;; If the `c-in-sws' region extended past the last
1300 ;; `c-is-sws' char we have to go back a bit.
1301 (or (get-text-property (1- (point)) 'c-is-sws
)
1302 (goto-char (previous-single-property-change
1303 (point) 'c-is-sws
)))
1307 "c-forward-sws cached move %s -> %s (max %s)"
1308 rung-pos
(point) (point-max))
1310 (setq rung-pos
(point))
1311 (and (> (skip-chars-forward " \t\n\r\f\v") 0)
1314 ;; We'll loop here if there is simple ws after the last rung.
1315 ;; That means that there's been some change in it and it's
1316 ;; possible that we've stepped into another ladder, so extend
1317 ;; the previous one to join with it if there is one, and try to
1318 ;; use the cache again.
1320 "c-forward-sws extending rung with [%s..%s] (max %s)"
1321 (1+ rung-pos
) (1+ (point)) (point-max))
1322 (unless (get-text-property (point) 'c-is-sws
)
1323 ;; Remove any `c-in-sws' property from the last char of
1324 ;; the rung before we mark it with `c-is-sws', so that we
1325 ;; won't connect with the remains of a broken "ladder".
1326 (c-remove-in-sws (point) (1+ (point))))
1327 (c-put-is-sws (1+ rung-pos
)
1329 (c-put-in-sws rung-pos
1330 (setq rung-pos
(point)
1331 last-put-in-sws-pos rung-pos
)))
1333 (setq simple-ws-end
(point))
1334 (c-forward-comments)
1337 ((/= (point) simple-ws-end
)
1338 ;; Skipped over comments. Don't cache at eob in case the buffer
1343 (and c-opt-cpp-prefix
1344 (looking-at c-opt-cpp-start
)
1345 (progn (skip-chars-backward " \t")
1348 (progn (backward-char)
1349 (not (eq (char-before) ?
\\))))))
1350 ;; Skip a preprocessor directive.
1352 (while (and (eq (char-before) ?
\\)
1353 (= (forward-line 1) 0))
1357 ;; Don't cache at eob in case the buffer is narrowed.
1360 ;; We've searched over a piece of non-white syntactic ws. See if this
1362 (setq next-rung-pos
(point))
1363 (skip-chars-forward " \t\n\r\f\v")
1364 (setq rung-end-pos
(min (1+ (point)) (point-max)))
1367 ;; Cache if we haven't skipped comments only, and if we started
1368 ;; either from a marked rung or from a completely uncached
1372 (not (get-text-property simple-ws-end
'c-in-sws
))))
1374 ;; See if there's a marked rung in the encountered simple ws. If
1375 ;; so then we can cache, unless `safe-start' is nil. Even then
1376 ;; we need to do this to check if the cache can be used for the
1378 (and (setq next-rung-is-marked
1379 (text-property-any next-rung-pos rung-end-pos
1385 "c-forward-sws caching [%s..%s] - [%s..%s] (max %s)"
1386 rung-pos
(1+ simple-ws-end
) next-rung-pos rung-end-pos
1389 ;; Remove the properties for any nested ws that might be cached.
1390 ;; Only necessary for `c-is-sws' since `c-in-sws' will be set
1392 (c-remove-is-sws (1+ simple-ws-end
) next-rung-pos
)
1393 (unless (and rung-is-marked
(= rung-pos simple-ws-end
))
1394 (c-put-is-sws rung-pos
1396 (setq rung-is-marked t
))
1397 (c-put-in-sws rung-pos
1398 (setq rung-pos
(point)
1399 last-put-in-sws-pos rung-pos
))
1400 (unless (get-text-property (1- rung-end-pos
) 'c-is-sws
)
1401 ;; Remove any `c-in-sws' property from the last char of
1402 ;; the rung before we mark it with `c-is-sws', so that we
1403 ;; won't connect with the remains of a broken "ladder".
1404 (c-remove-in-sws (1- rung-end-pos
) rung-end-pos
))
1405 (c-put-is-sws next-rung-pos
1409 "c-forward-sws not caching [%s..%s] - [%s..%s] (max %s)"
1410 rung-pos
(1+ simple-ws-end
) next-rung-pos rung-end-pos
1413 ;; Set `rung-pos' for the next rung. It's the same thing here as
1414 ;; initially, except that the rung position is set as early as
1415 ;; possible since we can't be in the ending ws of a line comment or
1416 ;; cpp directive now.
1417 (if (setq rung-is-marked next-rung-is-marked
)
1418 (setq rung-pos
(1- (next-single-property-change
1419 rung-is-marked
'c-is-sws nil rung-end-pos
)))
1420 (setq rung-pos next-rung-pos
))
1421 (setq safe-start t
)))
1423 ;; Make sure that the newly marked `c-in-sws' region doesn't connect to
1424 ;; another one after the point (which might occur when editing inside a
1425 ;; comment or macro).
1426 (when (eq last-put-in-sws-pos
(point))
1427 (cond ((< last-put-in-sws-pos
(point-max))
1429 "c-forward-sws clearing at %s for cache separation"
1430 last-put-in-sws-pos
)
1431 (c-remove-in-sws last-put-in-sws-pos
1432 (1+ last-put-in-sws-pos
)))
1434 ;; If at eob we have to clear the last character before the end
1435 ;; instead since the buffer might be narrowed and there might
1436 ;; be a `c-in-sws' after (point-max). In this case it's
1437 ;; necessary to clear both properties.
1439 "c-forward-sws clearing thoroughly at %s for cache separation"
1440 (1- last-put-in-sws-pos
))
1441 (c-remove-is-and-in-sws (1- last-put-in-sws-pos
)
1442 last-put-in-sws-pos
))))
1445 (defun c-backward-sws ()
1446 ;; Used by `c-backward-syntactic-ws' to implement the unbounded search.
1448 (let (;; `rung-pos' is set to a position as late as possible in the unmarked
1449 ;; part of the simple ws region.
1450 (rung-pos (point)) next-rung-pos last-put-in-sws-pos
1451 rung-is-marked simple-ws-beg cmt-skip-pos
)
1453 ;; Skip simple horizontal ws and do a quick check on the preceding
1454 ;; character to see if it's anying that can't end syntactic ws, so we can
1455 ;; bail out early in the majority of cases when there just are a few ws
1456 ;; chars. Newlines are complicated in the backward direction, so we can't
1458 (skip-chars-backward " \t\f")
1459 (when (and (not (bobp))
1462 (looking-at c-syntactic-ws-end
)))
1464 ;; Try to find a rung position in the simple ws preceding point, so that
1465 ;; we can get a cache hit even if the last bit of the simple ws has
1466 ;; changed recently.
1467 (setq simple-ws-beg
(point))
1468 (skip-chars-backward " \t\n\r\f\v")
1469 (if (setq rung-is-marked
(text-property-any
1470 (point) (min (1+ rung-pos
) (point-max))
1472 ;; `rung-pos' will be the earliest marked position, which means that
1473 ;; there might be later unmarked parts in the simple ws region.
1474 ;; It's not worth the effort to fix that; the last part of the
1475 ;; simple ws is also typically edited often, so it could be wasted.
1476 (goto-char (setq rung-pos rung-is-marked
))
1477 (goto-char simple-ws-beg
))
1482 (when (and rung-is-marked
1484 (get-text-property (1- (point)) 'c-in-sws
))
1486 ;; The following search is the main reason that `c-in-sws'
1487 ;; and `c-is-sws' aren't combined to one property.
1488 (goto-char (previous-single-property-change
1489 (point) 'c-in-sws nil
(point-min)))
1490 (unless (get-text-property (point) 'c-is-sws
)
1491 ;; If the `c-in-sws' region extended past the first
1492 ;; `c-is-sws' char we have to go forward a bit.
1493 (goto-char (next-single-property-change
1494 (point) 'c-is-sws
)))
1497 "c-backward-sws cached move %s <- %s (min %s)"
1498 (point) rung-pos
(point-min))
1500 (setq rung-pos
(point))
1501 (if (and (< (min (skip-chars-backward " \t\f\v")
1503 (setq simple-ws-beg
(point))
1504 (skip-chars-backward " \t\n\r\f\v")))
1506 (setq rung-is-marked
1507 (text-property-any (point) rung-pos
1510 (goto-char simple-ws-beg
)
1513 ;; We'll loop here if there is simple ws before the first rung.
1514 ;; That means that there's been some change in it and it's
1515 ;; possible that we've stepped into another ladder, so extend
1516 ;; the previous one to join with it if there is one, and try to
1517 ;; use the cache again.
1519 "c-backward-sws extending rung with [%s..%s] (min %s)"
1520 rung-is-marked rung-pos
(point-min))
1521 (unless (get-text-property (1- rung-pos
) 'c-is-sws
)
1522 ;; Remove any `c-in-sws' property from the last char of
1523 ;; the rung before we mark it with `c-is-sws', so that we
1524 ;; won't connect with the remains of a broken "ladder".
1525 (c-remove-in-sws (1- rung-pos
) rung-pos
))
1526 (c-put-is-sws rung-is-marked
1528 (c-put-in-sws rung-is-marked
1530 (setq rung-pos rung-is-marked
1531 last-put-in-sws-pos rung-pos
))
1533 (c-backward-comments)
1534 (setq cmt-skip-pos
(point))
1537 ((and c-opt-cpp-prefix
1538 (/= cmt-skip-pos simple-ws-beg
)
1539 (c-beginning-of-macro))
1540 ;; Inside a cpp directive. See if it should be skipped over.
1541 (let ((cpp-beg (point)))
1543 ;; Move back over all line continuations in the region skipped
1544 ;; over by `c-backward-comments'. If we go past it then we
1545 ;; started inside the cpp directive.
1546 (goto-char simple-ws-beg
)
1548 (while (and (> (point) cmt-skip-pos
)
1549 (progn (backward-char)
1550 (eq (char-before) ?
\\)))
1551 (beginning-of-line))
1553 (if (< (point) cmt-skip-pos
)
1554 ;; Don't move past the cpp directive if we began inside
1555 ;; it. Note that the position at the end of the last line
1556 ;; of the macro is also considered to be within it.
1557 (progn (goto-char cmt-skip-pos
)
1560 ;; It's worthwhile to spend a little bit of effort on finding
1561 ;; the end of the macro, to get a good `simple-ws-beg'
1562 ;; position for the cache. Note that `c-backward-comments'
1563 ;; could have stepped over some comments before going into
1564 ;; the macro, and then `simple-ws-beg' must be kept on the
1565 ;; same side of those comments.
1566 (goto-char simple-ws-beg
)
1567 (skip-chars-backward " \t\n\r\f\v")
1568 (if (eq (char-before) ?
\\)
1571 (if (< (point) simple-ws-beg
)
1572 ;; Might happen if comments after the macro were skipped
1574 (setq simple-ws-beg
(point)))
1579 ((/= (save-excursion
1580 (skip-chars-forward " \t\n\r\f\v" simple-ws-beg
)
1581 (setq next-rung-pos
(point)))
1583 ;; Skipped over comments. Must put point at the end of
1584 ;; the simple ws at point since we might be after a line
1585 ;; comment or cpp directive that's been partially
1586 ;; narrowed out, and we can't risk marking the simple ws
1587 ;; at the end of it.
1588 (goto-char next-rung-pos
)
1591 ;; We've searched over a piece of non-white syntactic ws. See if this
1593 (setq next-rung-pos
(point))
1594 (skip-chars-backward " \t\f\v")
1597 ;; Cache if we started either from a marked rung or from a
1598 ;; completely uncached position.
1600 (not (get-text-property (1- simple-ws-beg
) 'c-in-sws
))
1602 ;; Cache if there's a marked rung in the encountered simple ws.
1604 (skip-chars-backward " \t\n\r\f\v")
1605 (text-property-any (point) (min (1+ next-rung-pos
) (point-max))
1610 "c-backward-sws caching [%s..%s] - [%s..%s] (min %s)"
1611 (point) (1+ next-rung-pos
)
1612 simple-ws-beg
(min (1+ rung-pos
) (point-max))
1615 ;; Remove the properties for any nested ws that might be cached.
1616 ;; Only necessary for `c-is-sws' since `c-in-sws' will be set
1618 (c-remove-is-sws (1+ next-rung-pos
) simple-ws-beg
)
1619 (unless (and rung-is-marked
(= simple-ws-beg rung-pos
))
1620 (let ((rung-end-pos (min (1+ rung-pos
) (point-max))))
1621 (unless (get-text-property (1- rung-end-pos
) 'c-is-sws
)
1622 ;; Remove any `c-in-sws' property from the last char of
1623 ;; the rung before we mark it with `c-is-sws', so that we
1624 ;; won't connect with the remains of a broken "ladder".
1625 (c-remove-in-sws (1- rung-end-pos
) rung-end-pos
))
1626 (c-put-is-sws simple-ws-beg
1628 (setq rung-is-marked t
)))
1629 (c-put-in-sws (setq simple-ws-beg
(point)
1630 last-put-in-sws-pos simple-ws-beg
)
1632 (c-put-is-sws (setq rung-pos simple-ws-beg
)
1633 (1+ next-rung-pos
)))
1636 "c-backward-sws not caching [%s..%s] - [%s..%s] (min %s)"
1637 (point) (1+ next-rung-pos
)
1638 simple-ws-beg
(min (1+ rung-pos
) (point-max))
1640 (setq rung-pos next-rung-pos
1641 simple-ws-beg
(point))
1644 ;; Make sure that the newly marked `c-in-sws' region doesn't connect to
1645 ;; another one before the point (which might occur when editing inside a
1646 ;; comment or macro).
1647 (when (eq last-put-in-sws-pos
(point))
1648 (cond ((< (point-min) last-put-in-sws-pos
)
1650 "c-backward-sws clearing at %s for cache separation"
1651 (1- last-put-in-sws-pos
))
1652 (c-remove-in-sws (1- last-put-in-sws-pos
)
1653 last-put-in-sws-pos
))
1655 ;; If at bob and the buffer is narrowed, we have to clear the
1656 ;; character we're standing on instead since there might be a
1657 ;; `c-in-sws' before (point-min). In this case it's necessary
1658 ;; to clear both properties.
1660 "c-backward-sws clearing thoroughly at %s for cache separation"
1661 last-put-in-sws-pos
)
1662 (c-remove-is-and-in-sws last-put-in-sws-pos
1663 (1+ last-put-in-sws-pos
)))))
1667 ;; A system for handling noteworthy parens before the point.
1669 (defvar c-state-cache nil
)
1670 (make-variable-buffer-local 'c-state-cache
)
1671 ;; The state cache used by `c-parse-state' to cut down the amount of
1672 ;; searching. It's the result from some earlier `c-parse-state' call.
1673 ;; The use of the cached info is more effective if the next
1674 ;; `c-parse-state' call is on a line close by the one the cached state
1675 ;; was made at; the cache can actually slow down a little if the
1676 ;; cached state was made very far back in the buffer. The cache is
1677 ;; most effective if `c-parse-state' is used on each line while moving
1680 (defvar c-state-cache-start
1)
1681 (make-variable-buffer-local 'c-state-cache-start
)
1682 ;; This is (point-min) when `c-state-cache' was calculated, since a
1683 ;; change of narrowing is likely to affect the parens that are visible
1684 ;; before the point.
1686 (defsubst c-invalidate-state-cache
(pos)
1687 ;; Invalidate all info on `c-state-cache' that applies to the buffer
1688 ;; at POS or higher. This is much like `c-whack-state-after', but
1689 ;; it never changes a paren pair element into an open paren element.
1690 ;; Doing that would mean that the new open paren wouldn't have the
1691 ;; required preceding paren pair element.
1693 ;; This function does not do any hidden buffer changes.
1694 (while (and c-state-cache
1695 (let ((elem (car c-state-cache
)))
1697 (or (<= pos
(car elem
))
1700 (setq c-state-cache
(cdr c-state-cache
))))
1702 (defun c-parse-state ()
1703 ;; Finds and records all noteworthy parens between some good point
1704 ;; earlier in the file and point. That good point is at least the
1705 ;; beginning of the top-level construct we are in, or the beginning
1706 ;; of the preceding top-level construct if we aren't in one.
1708 ;; The returned value is a list of the noteworthy parens with the
1709 ;; last one first. If an element in the list is an integer, it's
1710 ;; the position of an open paren which has not been closed before
1711 ;; the point. If an element is a cons, it gives the position of a
1712 ;; closed brace paren pair; the car is the start paren position and
1713 ;; the cdr is the position following the closing paren. Only the
1714 ;; last closed brace paren pair before each open paren is recorded,
1715 ;; and thus the state never contains two cons elements in
1718 ;; Currently no characters which are given paren syntax with the
1719 ;; syntax-table property are recorded, i.e. angle bracket arglist
1720 ;; parens are never present here. Note that this might change.
1722 ;; This function does not do any hidden buffer changes.
1725 (let* ((here (point))
1726 (c-macro-start (c-query-macro-start))
1727 (in-macro-start (or c-macro-start
(point)))
1728 old-state last-pos pairs pos save-pos
)
1729 (c-invalidate-state-cache (point))
1731 ;; If the minimum position has changed due to narrowing then we
1732 ;; have to fix the tail of `c-state-cache' accordingly.
1733 (unless (= c-state-cache-start
(point-min))
1734 (if (> (point-min) c-state-cache-start
)
1735 ;; If point-min has moved forward then we just need to cut
1736 ;; off a bit of the tail.
1737 (let ((ptr (cons nil c-state-cache
)) elem
)
1738 (while (and (setq elem
(cdr ptr
))
1739 (>= (if (consp elem
) (car elem
) elem
)
1743 (if (eq (cdr ptr
) c-state-cache
)
1744 (setq c-state-cache nil
)
1746 ;; If point-min has moved backward then we drop the state
1747 ;; completely. It's possible to do a better job here and
1748 ;; recalculate the top only.
1749 (setq c-state-cache nil
))
1750 (setq c-state-cache-start
(point-min)))
1752 ;; Get the latest position we know are directly inside the
1753 ;; closest containing paren of the cached state.
1754 (setq last-pos
(and c-state-cache
1755 (if (consp (car c-state-cache
))
1756 (cdr (car c-state-cache
))
1757 (1+ (car c-state-cache
)))))
1759 ;; Check if the found last-pos is in a macro. If it is, and
1760 ;; we're not in the same macro, we must discard everything on
1761 ;; c-state-cache that is inside the macro before using it.
1764 (goto-char last-pos
)
1765 (when (and (c-beginning-of-macro)
1766 (/= (point) in-macro-start
))
1767 (c-invalidate-state-cache (point))
1768 ;; Set last-pos again, just like above.
1769 (setq last-pos
(and c-state-cache
1770 (if (consp (car c-state-cache
))
1771 (cdr (car c-state-cache
))
1772 (1+ (car c-state-cache
))))))))
1775 ;; Find the start position for the forward search. (Can't
1776 ;; search in the backward direction since point might be
1777 ;; in some kind of literal.)
1780 ;; There's a cached state with a containing paren. Pop
1781 ;; off the stale containing sexps from it by going
1782 ;; forward out of parens as far as possible.
1783 (narrow-to-region (point-min) here
)
1784 (let (placeholder pair-beg
)
1785 (while (and c-state-cache
1787 (c-up-list-forward last-pos
)))
1788 (setq last-pos placeholder
)
1789 (if (consp (car c-state-cache
))
1790 (setq pair-beg
(car-safe (cdr c-state-cache
))
1791 c-state-cache
(cdr-safe (cdr c-state-cache
)))
1792 (setq pair-beg
(car c-state-cache
)
1793 c-state-cache
(cdr c-state-cache
))))
1795 (when (and pair-beg
(eq (char-after pair-beg
) ?
{))
1796 ;; The last paren pair we moved out from was a brace
1797 ;; pair. Modify the state to record this as a closed
1799 (if (consp (car-safe c-state-cache
))
1800 (setq c-state-cache
(cdr c-state-cache
)))
1801 (setq c-state-cache
(cons (cons pair-beg last-pos
)
1804 ;; Check if the preceding balanced paren is within a
1805 ;; macro; it should be ignored if we're outside the
1806 ;; macro. There's no need to check any further upwards;
1807 ;; if the macro contains an unbalanced opening paren then
1808 ;; we're smoked anyway.
1809 (when (and (<= (point) in-macro-start
)
1810 (consp (car c-state-cache
)))
1812 (goto-char (car (car c-state-cache
)))
1813 (when (c-beginning-of-macro)
1815 c-state-cache
(cdr c-state-cache
)))))
1818 (setq old-state c-state-cache
)
1822 ;; go back 2 bods, but ignore any bogus positions
1823 ;; returned by beginning-of-defun (i.e. open paren in
1827 (while (not (or (bobp) (zerop cnt
)))
1828 (c-beginning-of-defun-1)
1829 (if (eq (char-after) ?\
{)
1830 (setq cnt
(1- cnt
)))))
1833 (narrow-to-region (point-min) here
)
1836 ;; Find the balanced brace pairs.
1839 (while (and (setq last-pos
(c-down-list-forward pos
))
1840 (setq pos
(c-up-list-forward last-pos
)))
1841 (if (eq (char-before last-pos
) ?
{)
1842 (setq pairs
(cons (cons last-pos pos
) pairs
))))
1844 ;; Should ignore any pairs that are in a macro, providing
1845 ;; we're not in the same one.
1846 (when (and pairs
(< (car (car pairs
)) in-macro-start
))
1847 (while (and (save-excursion
1848 (goto-char (car (car pairs
)))
1849 (c-beginning-of-macro))
1850 (setq pairs
(cdr pairs
)))))
1852 ;; Record the last brace pair.
1854 (if (and (eq c-state-cache old-state
)
1855 (consp (car-safe c-state-cache
)))
1856 ;; There's a closed pair on the cached state but we've
1857 ;; found a later one, so remove it.
1858 (setq c-state-cache
(cdr c-state-cache
)))
1859 (setq pairs
(car pairs
))
1860 (setcar pairs
(1- (car pairs
)))
1861 (when (consp (car-safe c-state-cache
))
1862 ;; There could already be a cons first in `c-state-cache'
1863 ;; if we've e.g. jumped over an unbalanced open paren in a
1865 (setq c-state-cache
(cdr c-state-cache
)))
1866 (setq c-state-cache
(cons pairs c-state-cache
)))
1869 ;; Prepare to loop, but record the open paren only if it's
1870 ;; outside a macro or within the same macro as point, and
1871 ;; if it is a "real" open paren and not some character
1872 ;; that got an open paren syntax-table property.
1875 (if (and (or (>= last-pos in-macro-start
)
1877 (goto-char last-pos
)
1878 (not (c-beginning-of-macro))))
1879 (= (char-syntax (char-before last-pos
)) ?\
())
1880 (setq c-state-cache
(cons (1- last-pos
) c-state-cache
))))
1882 (if (setq last-pos
(c-up-list-forward pos
))
1883 ;; Found a close paren without a corresponding opening
1884 ;; one. Maybe we didn't go back far enough, so try to
1885 ;; scan backward for the start paren and then start over.
1887 (setq pos
(c-up-list-backward pos
)
1890 ;; Emacs (up to at least 21.2) can get confused by
1891 ;; open parens in column zero inside comments: The
1892 ;; sexp functions can then misbehave and bring us
1893 ;; back to the same point again. Check this so that
1894 ;; we don't get an infinite loop.
1898 (format "Unbalanced close paren at line %d"
1899 (1+ (count-lines (point-min)
1900 (c-point 'bol last-pos
)))))))
1905 ;; Debug tool to catch cache inconsistencies.
1906 (defvar c-debug-parse-state nil
)
1907 (unless (fboundp 'c-real-parse-state
)
1908 (fset 'c-real-parse-state
(symbol-function 'c-parse-state
)))
1909 (cc-bytecomp-defun c-real-parse-state)
1910 (defun c-debug-parse-state ()
1911 (let ((res1 (c-real-parse-state)) res2
)
1912 (let ((c-state-cache nil
))
1913 (setq res2
(c-real-parse-state)))
1914 (unless (equal res1 res2
)
1915 (error "c-parse-state inconsistency: using cache: %s, from scratch: %s"
1918 (defun c-toggle-parse-state-debug (&optional arg
)
1920 (setq c-debug-parse-state
(c-calculate-state arg c-debug-parse-state
))
1921 (fset 'c-parse-state
(symbol-function (if c-debug-parse-state
1922 'c-debug-parse-state
1923 'c-real-parse-state
)))
1924 (c-keep-region-active))
1926 (defun c-whack-state-before (bufpos paren-state
)
1927 ;; Whack off any state information from PAREN-STATE which lies
1928 ;; before BUFPOS. Not destructive on PAREN-STATE.
1930 ;; This function does not do any hidden buffer changes.
1931 (let* ((newstate (list nil
))
1935 (setq car
(car paren-state
)
1936 paren-state
(cdr paren-state
))
1937 (if (< (if (consp car
) (car car
) car
) bufpos
)
1938 (setq paren-state nil
)
1939 (setcdr ptr
(list car
))
1940 (setq ptr
(cdr ptr
))))
1943 (defun c-whack-state-after (bufpos paren-state
)
1944 ;; Whack off any state information from PAREN-STATE which lies at or
1945 ;; after BUFPOS. Not destructive on PAREN-STATE.
1947 ;; This function does not do any hidden buffer changes.
1950 (let ((car (car paren-state
)))
1952 ;; just check the car, because in a balanced brace
1953 ;; expression, it must be impossible for the corresponding
1954 ;; close brace to be before point, but the open brace to
1956 (if (<= bufpos
(car car
))
1958 (if (< bufpos
(cdr car
))
1959 ;; its possible that the open brace is before
1960 ;; bufpos, but the close brace is after. In that
1961 ;; case, convert this to a non-cons element. The
1962 ;; rest of the state is before bufpos, so we're
1964 (throw 'done
(cons (car car
) (cdr paren-state
)))
1965 ;; we know that both the open and close braces are
1966 ;; before bufpos, so we also know that everything else
1967 ;; on state is before bufpos.
1968 (throw 'done paren-state
)))
1971 ;; it's before bufpos, so everything else should too.
1972 (throw 'done paren-state
)))
1973 (setq paren-state
(cdr paren-state
)))
1976 (defun c-most-enclosing-brace (paren-state &optional bufpos
)
1977 ;; Return the bufpos of the innermost enclosing open paren before
1978 ;; bufpos that hasn't been narrowed out, or nil if none was found.
1980 ;; This function does not do any hidden buffer changes.
1982 (or bufpos
(setq bufpos
134217727))
1984 (setq enclosingp
(car paren-state
)
1985 paren-state
(cdr paren-state
))
1986 (if (or (consp enclosingp
)
1987 (>= enclosingp bufpos
))
1988 (setq enclosingp nil
)
1989 (if (< enclosingp
(point-min))
1990 (setq enclosingp nil
))
1991 (setq paren-state nil
)))
1994 (defun c-least-enclosing-brace (paren-state &optional bufpos
)
1995 ;; Return the bufpos of the outermost enclosing open paren before
1996 ;; bufpos that hasn't been narrowed out, or nil if none was found.
1998 ;; This function does not do any hidden buffer changes.
2000 (or bufpos
(setq bufpos
134217727))
2002 (setq elem
(car paren-state
)
2003 paren-state
(cdr paren-state
))
2004 (unless (or (consp elem
)
2006 (if (>= elem
(point-min))
2010 (defun c-safe-position (bufpos paren-state
)
2011 ;; Return the closest known safe position higher up than BUFPOS, or
2012 ;; nil if PAREN-STATE doesn't contain one. Return nil if BUFPOS is
2013 ;; nil, which is useful to find the closest limit before a given
2014 ;; limit that might be nil.
2016 ;; This function does not do any hidden buffer changes.
2021 (setq elem
(car paren-state
))
2023 (cond ((< (cdr elem
) bufpos
)
2024 (throw 'done
(cdr elem
)))
2025 ((< (car elem
) bufpos
)
2027 (throw 'done
(min (1+ (car elem
)) bufpos
))))
2029 ;; elem is the position at and not after the opening paren, so
2030 ;; we can go forward one more step unless it's equal to
2031 ;; bufpos. This is useful in some cases avoid an extra paren
2032 ;; level between the safe position and bufpos.
2033 (throw 'done
(min (1+ elem
) bufpos
))))
2034 (setq paren-state
(cdr paren-state
)))))))
2036 (defun c-beginning-of-syntax ()
2037 ;; This is used for `font-lock-beginning-of-syntax-function'. It
2038 ;; goes to the closest previous point that is known to be outside
2039 ;; any string literal or comment. `c-state-cache' is used if it has
2040 ;; a position in the vicinity.
2041 (let* ((paren-state c-state-cache
)
2045 ;; Note: Similar code in `c-safe-position'. The
2046 ;; difference is that we accept a safe position at
2047 ;; the point and don't bother to go forward past open
2050 (setq elem
(car paren-state
))
2052 (cond ((<= (cdr elem
) (point))
2053 (throw 'done
(cdr elem
)))
2054 ((<= (car elem
) (point))
2055 (throw 'done
(car elem
))))
2056 (if (<= elem
(point))
2057 (throw 'done elem
)))
2058 (setq paren-state
(cdr paren-state
)))
2061 (if (> pos
(- (point) 4000))
2063 ;; The position is far back. Try `c-beginning-of-defun-1'
2064 ;; (although we can't be entirely sure it will go to a position
2065 ;; outside a comment or string in current emacsen). FIXME:
2066 ;; Consult `syntax-ppss' here.
2067 (c-beginning-of-defun-1)
2072 ;; Tools for scanning identifiers and other tokens.
2074 (defun c-on-identifier ()
2075 "Return non-nil if the point is on or directly after an identifier.
2076 Keywords are recognized and not considered identifiers. If an
2077 identifier is detected, the returned value is its starting position.
2078 If an identifier both starts and stops at the point \(can only happen
2079 in Pike) then the point for the preceding one is returned.
2081 This function does not do any hidden buffer changes."
2084 (if (zerop (skip-syntax-backward "w_"))
2086 (when (c-major-mode-is 'pike-mode
)
2087 ;; Handle the `<operator> syntax in Pike.
2088 (let ((pos (point)))
2089 (skip-chars-backward "!%&*+\\-/<=>^|~[]()")
2090 (and (if (< (skip-chars-backward "`") 0)
2093 (eq (char-after) ?\
`))
2094 (looking-at c-symbol-key
)
2095 (>= (match-end 0) pos
)
2098 (and (not (looking-at c-keywords-regexp
))
2101 (defsubst c-simple-skip-symbol-backward
()
2102 ;; If the point is at the end of a symbol then skip backward to the
2103 ;; beginning of it. Don't move otherwise. Return non-nil if point
2105 (or (< (skip-syntax-backward "w_") 0)
2106 (and (c-major-mode-is 'pike-mode
)
2107 ;; Handle the `<operator> syntax in Pike.
2108 (let ((pos (point)))
2109 (if (and (< (skip-chars-backward "!%&*+\\-/<=>^|~[]()") 0)
2110 (< (skip-chars-backward "`") 0)
2111 (looking-at c-symbol-key
)
2112 (>= (match-end 0) pos
))
2117 (defsubst c-beginning-of-current-token
(&optional back-limit
)
2118 ;; Move to the beginning of the current token. Do not move if not
2119 ;; in the middle of one. BACK-LIMIT may be used to bound the
2120 ;; backward search; if given it's assumed to be at the boundary
2121 ;; between two tokens.
2122 (if (looking-at "\\w\\|\\s_")
2123 (skip-syntax-backward "w_" back-limit
)
2124 (let ((start (point)))
2125 (when (< (skip-syntax-backward ".()" back-limit
) 0)
2126 (while (let ((pos (or (and (looking-at c-nonsymbol-token-regexp
)
2128 ;; `c-nonsymbol-token-regexp' should always match
2129 ;; since we've skipped backward over punctuator
2130 ;; or paren syntax, but consume one char in case
2131 ;; it doesn't so that we don't leave point before
2132 ;; some earlier incorrect token.
2138 (defsubst c-end-of-current-token
(&optional back-limit
)
2139 ;; Move to the end of the current token. Do not move if not in the
2140 ;; middle of one. BACK-LIMIT may be used to bound the backward
2141 ;; search; if given it's assumed to be at the boundary between two
2143 (let ((start (point)))
2144 (cond ((< (skip-syntax-backward "w_" (1- start
)) 0)
2145 (skip-syntax-forward "w_"))
2146 ((< (skip-syntax-backward ".()" back-limit
) 0)
2148 (if (looking-at c-nonsymbol-token-regexp
)
2149 (goto-char (match-end 0))
2150 ;; `c-nonsymbol-token-regexp' should always match since
2151 ;; we've skipped backward over punctuator or paren
2152 ;; syntax, but move forward in case it doesn't so that
2153 ;; we don't leave point earlier than we started with.
2155 (< (point) start
)))))))
2157 (defconst c-jump-syntax-balanced
2158 (if (memq 'gen-string-delim c-emacs-features
)
2159 "\\w\\|\\s_\\|\\s\(\\|\\s\)\\|\\s\"\\|\\s|"
2160 "\\w\\|\\s_\\|\\s\(\\|\\s\)\\|\\s\""))
2162 (defconst c-jump-syntax-unbalanced
2163 (if (memq 'gen-string-delim c-emacs-features
)
2164 "\\w\\|\\s_\\|\\s\"\\|\\s|"
2165 "\\w\\|\\s_\\|\\s\""))
2167 (defun c-forward-token-2 (&optional count balanced limit
)
2168 "Move forward by tokens.
2169 A token is defined as all symbols and identifiers which aren't
2170 syntactic whitespace \(note that multicharacter tokens like \"==\" are
2171 treated properly). Point is always either left at the beginning of a
2172 token or not moved at all. COUNT specifies the number of tokens to
2173 move; a negative COUNT moves in the opposite direction. A COUNT of 0
2174 moves to the next token beginning only if not already at one. If
2175 BALANCED is true, move over balanced parens, otherwise move into them.
2176 Also, if BALANCED is true, never move out of an enclosing paren.
2178 LIMIT sets the limit for the movement and defaults to the point limit.
2179 The case when LIMIT is set in the middle of a token, comment or macro
2180 is handled correctly, i.e. the point won't be left there.
2182 Return the number of tokens left to move \(positive or negative). If
2183 BALANCED is true, a move over a balanced paren counts as one. Note
2184 that if COUNT is 0 and no appropriate token beginning is found, 1 will
2185 be returned. Thus, a return value of 0 guarantees that point is at
2186 the requested position and a return value less \(without signs) than
2187 COUNT guarantees that point is at the beginning of some token."
2189 (or count
(setq count
1))
2191 (- (c-backward-token-2 (- count
) balanced limit
))
2193 (let ((jump-syntax (if balanced
2194 c-jump-syntax-balanced
2195 c-jump-syntax-unbalanced
))
2200 ;; If count is zero we should jump if in the middle of a token.
2201 (c-end-of-current-token))
2204 (if limit
(narrow-to-region (point-min) limit
))
2206 (progn (c-forward-syntactic-ws) (point)))
2207 ;; Skip whitespace. Count this as a move if we did in
2209 (setq count
(max (1- count
) 0)))
2212 ;; Moved out of bounds. Make sure the returned count isn't zero.
2214 (if (zerop count
) (setq count
1))
2217 ;; Use `condition-case' to avoid having the limit tests
2224 (cond ((looking-at jump-syntax
)
2225 (goto-char (scan-sexps (point) 1))
2227 ((looking-at c-nonsymbol-token-regexp
)
2228 (goto-char (match-end 0))
2230 ;; `c-nonsymbol-token-regexp' above should always
2231 ;; match if there are correct tokens. Try to
2232 ;; widen to see if the limit was set in the
2233 ;; middle of one, else fall back to treating
2234 ;; the offending thing as a one character token.
2238 (looking-at c-nonsymbol-token-regexp
)))
2243 (c-forward-syntactic-ws)
2246 (error (goto-char last
)))
2250 (setq count
(1+ count
)))))
2254 (defun c-backward-token-2 (&optional count balanced limit
)
2255 "Move backward by tokens.
2256 See `c-forward-token-2' for details."
2258 (or count
(setq count
1))
2260 (- (c-forward-token-2 (- count
) balanced limit
))
2262 (or limit
(setq limit
(point-min)))
2263 (let ((jump-syntax (if balanced
2264 c-jump-syntax-balanced
2265 c-jump-syntax-unbalanced
))
2269 ;; The count is zero so try to skip to the beginning of the
2272 (progn (c-beginning-of-current-token) (point)))
2273 (if (< (point) limit
)
2274 ;; The limit is inside the same token, so return 1.
2277 ;; We're not in the middle of a token. If there's
2278 ;; whitespace after the point then we must move backward,
2279 ;; so set count to 1 in that case.
2280 (and (looking-at c-syntactic-ws-start
)
2281 ;; If we're looking at a '#' that might start a cpp
2282 ;; directive then we have to do a more elaborate check.
2283 (or (/= (char-after) ?
#)
2284 (not c-opt-cpp-prefix
)
2287 (progn (beginning-of-line)
2288 (looking-at "[ \t]*")
2291 (progn (backward-char)
2292 (not (eq (char-before) ?
\\)))))))
2295 ;; Use `condition-case' to avoid having to check for buffer
2296 ;; limits in `backward-char', `scan-sexps' and `goto-char' below.
2301 (c-backward-syntactic-ws)
2303 (if (looking-at jump-syntax
)
2304 (goto-char (scan-sexps (1+ (point)) -
1))
2305 ;; This can be very inefficient if there's a long
2306 ;; sequence of operator tokens without any separation.
2307 ;; That doesn't happen in practice, anyway.
2308 (c-beginning-of-current-token))
2309 (>= (point) limit
)))
2312 (error (goto-char last
)))
2314 (if (< (point) limit
)
2319 (defun c-forward-token-1 (&optional count balanced limit
)
2320 "Like `c-forward-token-2' but doesn't treat multicharacter operator
2321 tokens like \"==\" as single tokens, i.e. all sequences of symbol
2322 characters are jumped over character by character. This function is
2323 for compatibility only; it's only a wrapper over `c-forward-token-2'."
2324 (let ((c-nonsymbol-token-regexp "\\s.\\|\\s\(\\|\\s\)"))
2325 (c-forward-token-2 count balanced limit
)))
2327 (defun c-backward-token-1 (&optional count balanced limit
)
2328 "Like `c-backward-token-2' but doesn't treat multicharacter operator
2329 tokens like \"==\" as single tokens, i.e. all sequences of symbol
2330 characters are jumped over character by character. This function is
2331 for compatibility only; it's only a wrapper over `c-backward-token-2'."
2332 (let ((c-nonsymbol-token-regexp "\\s.\\|\\s\(\\|\\s\)"))
2333 (c-backward-token-2 count balanced limit
)))
2336 ;; Tools for doing searches restricted to syntactically relevant text.
2338 (defun c-syntactic-re-search-forward (regexp &optional bound noerror
2339 paren-level not-inside-token
2340 lookbehind-submatch
)
2341 "Like `re-search-forward', but only report matches that are found
2342 in syntactically significant text. I.e. matches in comments, macros
2343 or string literals are ignored. The start point is assumed to be
2344 outside any comment, macro or string literal, or else the content of
2345 that region is taken as syntactically significant text.
2347 If PAREN-LEVEL is non-nil, an additional restriction is added to
2348 ignore matches in nested paren sexps, and the search will also not go
2349 outside the current paren sexp.
2351 If NOT-INSIDE-TOKEN is non-nil, matches in the middle of tokens are
2352 ignored. Things like multicharacter operators and special symbols
2353 \(e.g. \"`()\" in Pike) are handled but currently not floating point
2356 If LOOKBEHIND-SUBMATCH is non-nil, it's taken as a number of a
2357 subexpression in REGEXP. The end of that submatch is used as the
2358 position to check for syntactic significance. If LOOKBEHIND-SUBMATCH
2359 isn't used or if that subexpression didn't match then the start
2360 position of the whole match is used instead. The \"look behind\"
2361 subexpression is never tested before the starting position, so it
2362 might be a good idea to include \\=\\= as a match alternative in it.
2364 Optimization note: Matches might be missed if the \"look behind\"
2365 subexpression should match the end of nonwhite syntactic whitespace,
2366 i.e. the end of comments or cpp directives. This since the function
2367 skips over such things before resuming the search. It's also not safe
2368 to assume that the \"look behind\" subexpression never can match
2369 syntactic whitespace."
2371 (or bound
(setq bound
(point-max)))
2372 (if paren-level
(setq paren-level -
1))
2374 ;;(message "c-syntactic-re-search-forward %s %s %S" (point) bound regexp)
2376 (let ((start (point))
2378 (last-token-end-pos (point-min))
2379 match-pos found state check-pos check-state tmp
)
2384 (re-search-forward regexp bound noerror
)
2387 (setq match-pos
(point)
2388 state
(parse-partial-sexp
2389 pos
(match-beginning 0) paren-level nil state
)
2391 (if (setq check-pos
(and lookbehind-submatch
2392 (match-end lookbehind-submatch
)))
2393 (setq check-state
(parse-partial-sexp
2394 pos check-pos paren-level nil state
))
2398 ;; If we got a look behind subexpression and get an
2399 ;; insignificant match in something that isn't
2400 ;; syntactic whitespace (i.e. strings or in nested
2401 ;; parentheses), then we can never skip more than a
2402 ;; single character from the match position before
2403 ;; continuing the search. That since the look behind
2404 ;; subexpression might match the end of the
2405 ;; insignificant region.
2408 ((setq tmp
(elt check-state
3))
2409 ;; Match inside a string.
2410 (if (or lookbehind-submatch
2411 (not (integerp tmp
)))
2412 (goto-char (min (1+ pos
) bound
))
2413 ;; Skip to the end of the string before continuing.
2414 (let ((ender (make-string 1 tmp
)) (continue t
))
2415 (while (if (search-forward ender bound noerror
)
2417 (setq state
(parse-partial-sexp
2418 pos
(point) nil nil state
)
2421 (setq continue nil
)))
2424 ((elt check-state
7)
2425 ;; Match inside a line comment. Skip to eol. Use
2426 ;; `re-search-forward' instead of `skip-chars-forward' to get
2427 ;; the right bound behavior.
2428 (re-search-forward "[\n\r]" bound noerror
))
2430 ((elt check-state
4)
2431 ;; Match inside a block comment. Skip to the '*/'.
2432 (search-forward "*/" bound noerror
))
2434 ((and (not (elt check-state
5))
2435 (eq (char-before check-pos
) ?
/)
2436 (memq (char-after check-pos
) '(?
/ ?
*)))
2437 ;; Match in the middle of the opener of a block or line
2439 (if (= (char-after check-pos
) ?
/)
2440 (re-search-forward "[\n\r]" bound noerror
)
2441 (search-forward "*/" bound noerror
)))
2443 ((and not-inside-token
2444 (or (< check-pos last-token-end-pos
)
2447 (goto-char check-pos
)
2448 (c-end-of-current-token last-token-end-pos
)
2449 (setq last-token-end-pos
(point))))))
2450 ;; Match inside a token.
2451 (cond ((<= (point) bound
)
2452 (goto-char (min (1+ pos
) bound
))
2455 (t (signal 'search-failed
"end of token"))))
2459 (c-beginning-of-macro start
)))
2460 ;; Match inside a macro. Skip to the end of it.
2462 (cond ((<= (point) bound
) t
)
2464 (t (signal 'search-failed
"end of macro"))))
2467 (/= (setq tmp
(car check-state
)) 0))
2469 ;; Match inside a nested paren sexp.
2470 (if lookbehind-submatch
2471 (goto-char (min (1+ pos
) bound
))
2472 ;; Skip out of the paren quickly.
2473 (setq state
(parse-partial-sexp pos bound
0 nil state
)
2475 ;; Have exited the current paren sexp. The
2476 ;; `parse-partial-sexp' above has left us just after the
2477 ;; closing paren in this case. Just make
2478 ;; `re-search-forward' above fail in the appropriate way;
2479 ;; we'll adjust the leave off point below if necessary.
2480 (setq bound
(point))))
2489 (signal (car err
) (cdr err
))))
2491 ;;(message "c-syntactic-re-search-forward done %s" (or match-pos (point)))
2495 (goto-char match-pos
)
2498 ;; Search failed. Set point as appropriate.
2499 (cond ((eq noerror t
)
2502 (if (eq (car (parse-partial-sexp pos bound -
1 nil state
)) -
1)
2508 (defun c-syntactic-skip-backward (skip-chars &optional limit
)
2509 "Like `skip-chars-backward' but only look at syntactically relevant chars,
2510 i.e. don't stop at positions inside syntactic whitespace or string
2511 literals. Preprocessor directives are also ignored, with the exception
2512 of the one that the point starts within, if any. If LIMIT is given,
2513 it's assumed to be at a syntactically relevant position.
2515 This function does not do any hidden buffer changes."
2517 (let ((start (point))
2518 ;; A list of syntactically relevant positions in descending
2519 ;; order. It's used to avoid scanning repeatedly over
2520 ;; potentially large regions with `parse-partial-sexp' to verify
2523 ;; The result from `c-beginning-of-macro' at the start position or the
2524 ;; start position itself if it isn't within a macro. Evaluated on
2530 (< (skip-chars-backward skip-chars limit
) 0)
2532 ;; Use `parse-partial-sexp' from a safe position down to
2533 ;; the point to check if it's outside comments and
2535 (let ((pos (point)) safe-pos state
)
2536 ;; Pick a safe position as close to the point as
2539 ;; FIXME: Consult `syntax-ppss' here if our
2540 ;; cache doesn't give a good position.
2541 (while (and safe-pos-list
2542 (> (car safe-pos-list
) (point)))
2543 (setq safe-pos-list
(cdr safe-pos-list
)))
2544 (unless (setq safe-pos
(car-safe safe-pos-list
))
2545 (setq safe-pos
(max (or (c-safe-position
2546 (point) (or c-state-cache
2550 safe-pos-list
(list safe-pos
)))
2553 (setq state
(parse-partial-sexp
2556 ;; Cache positions along the way to use if we have to
2557 ;; back up more. Every closing paren on the same
2558 ;; level seems like fairly well spaced positions.
2559 (setq safe-pos
(point)
2560 safe-pos-list
(cons safe-pos safe-pos-list
)))
2563 ((or (elt state
3) (elt state
4))
2564 ;; Inside string or comment. Continue search at the
2566 (if (setq pos
(nth 8 state
))
2567 ;; It's an emacs where `parse-partial-sexp'
2568 ;; supplies the starting position.
2570 (goto-char (car (c-literal-limits safe-pos
))))
2573 ((c-beginning-of-macro limit
)
2577 (setq start-macro-beg
2580 (c-beginning-of-macro limit
)
2583 ;; It's inside the same macro we started in so it's
2584 ;; a relevant match.
2590 ;; Skip syntactic ws afterwards so that we don't stop at the
2591 ;; end of a comment if `skip-chars' is something like "^/".
2592 (c-backward-syntactic-ws)
2598 ;; Tools for handling comments and string literals.
2600 (defun c-slow-in-literal (&optional lim detect-cpp
)
2601 "Return the type of literal point is in, if any.
2602 The return value is `c' if in a C-style comment, `c++' if in a C++
2603 style comment, `string' if in a string literal, `pound' if DETECT-CPP
2604 is non-nil and in a preprocessor line, or nil if somewhere else.
2605 Optional LIM is used as the backward limit of the search. If omitted,
2606 or nil, `c-beginning-of-defun' is used.
2608 The last point calculated is cached if the cache is enabled, i.e. if
2609 `c-in-literal-cache' is bound to a two element vector.
2611 This function does not do any hidden buffer changes."
2612 (if (and (vectorp c-in-literal-cache
)
2613 (= (point) (aref c-in-literal-cache
0)))
2614 (aref c-in-literal-cache
1)
2615 (let ((rtn (save-excursion
2616 (let* ((pos (point))
2618 (c-beginning-of-syntax)
2620 (state (parse-partial-sexp lim pos
)))
2622 ((elt state
3) 'string
)
2623 ((elt state
4) (if (elt state
7) 'c
++ 'c
))
2624 ((and detect-cpp
(c-beginning-of-macro lim
)) 'pound
)
2626 ;; cache this result if the cache is enabled
2627 (if (not c-in-literal-cache
)
2628 (setq c-in-literal-cache
(vector (point) rtn
)))
2631 ;; XEmacs has a built-in function that should make this much quicker.
2632 ;; I don't think we even need the cache, which makes our lives more
2633 ;; complicated anyway. In this case, lim is only used to detect
2636 ;; Note that there is a bug in Xemacs's buffer-syntactic-context when used in
2637 ;; conjunction with syntax-table-properties. The bug is present in, e.g.,
2638 ;; Xemacs 21.4.4. It manifested itself thus:
2640 ;; Starting with an empty AWK Mode buffer, type
2642 ;; Point gets wrongly left at column 0, rather than being indented to tab-width.
2644 ;; AWK Mode is designed such that when the first / is typed, it gets the
2645 ;; syntax-table property "string fence". When the second / is typed, BOTH /s
2646 ;; are given the s-t property "string". However, buffer-syntactic-context
2647 ;; fails to take account of the change of the s-t property on the opening / to
2648 ;; "string", and reports that the { is within a string started by the second /.
2650 ;; The workaround for this is for the AWK Mode initialisation to switch the
2651 ;; defalias for c-in-literal to c-slow-in-literal. This will slow down other
2652 ;; cc-modes in Xemacs whenever an awk-buffer has been initialised.
2654 ;; (Alan Mackenzie, 2003/4/30).
2656 (defun c-fast-in-literal (&optional lim detect-cpp
)
2657 (let ((context (buffer-syntactic-context)))
2659 ((eq context
'string
) 'string
)
2660 ((eq context
'comment
) 'c
++)
2661 ((eq context
'block-comment
) 'c
)
2662 ((and detect-cpp
(save-excursion (c-beginning-of-macro lim
))) 'pound
))))
2664 (defalias 'c-in-literal
2665 (if (fboundp 'buffer-syntactic-context
)
2666 'c-fast-in-literal
; Xemacs
2667 'c-slow-in-literal
)) ; GNU Emacs
2669 ;; The defalias above isn't enough to shut up the byte compiler.
2670 (cc-bytecomp-defun c-in-literal)
2672 (defun c-literal-limits (&optional lim near not-in-delimiter
)
2673 "Return a cons of the beginning and end positions of the comment or
2674 string surrounding point (including both delimiters), or nil if point
2675 isn't in one. If LIM is non-nil, it's used as the \"safe\" position
2676 to start parsing from. If NEAR is non-nil, then the limits of any
2677 literal next to point is returned. \"Next to\" means there's only
2678 spaces and tabs between point and the literal. The search for such a
2679 literal is done first in forward direction. If NOT-IN-DELIMITER is
2680 non-nil, the case when point is inside a starting delimiter won't be
2681 recognized. This only has effect for comments, which have starting
2682 delimiters with more than one character.
2684 This function does not do any hidden buffer changes."
2687 (let* ((pos (point))
2689 (c-beginning-of-syntax)
2691 (state (parse-partial-sexp lim pos
)))
2693 (cond ((elt state
3)
2694 ;; String. Search backward for the start.
2695 (while (elt state
3)
2696 (search-backward (make-string 1 (elt state
3)))
2697 (setq state
(parse-partial-sexp lim
(point))))
2698 (cons (point) (or (c-safe (c-forward-sexp 1) (point))
2702 ;; Line comment. Search from bol for the comment starter.
2704 (setq state
(parse-partial-sexp lim
(point))
2706 (while (not (elt state
7))
2707 (search-forward "//") ; Should never fail.
2708 (setq state
(parse-partial-sexp
2709 lim
(point) nil nil state
)
2712 (cons (point) (progn (c-forward-single-comment) (point))))
2715 ;; Block comment. Search backward for the comment starter.
2716 (while (elt state
4)
2717 (search-backward "/*") ; Should never fail.
2718 (setq state
(parse-partial-sexp lim
(point))))
2719 (cons (point) (progn (c-forward-single-comment) (point))))
2721 ((and (not not-in-delimiter
)
2723 (eq (char-before) ?
/)
2724 (looking-at "[/*]"))
2725 ;; We're standing in a comment starter.
2727 (cons (point) (progn (c-forward-single-comment) (point))))
2732 ;; Search forward for a literal.
2733 (skip-chars-forward " \t")
2736 ((looking-at c-string-limit-regexp
) ; String.
2737 (cons (point) (or (c-safe (c-forward-sexp 1) (point))
2740 ((looking-at c-comment-start-regexp
) ; Line or block comment.
2741 (cons (point) (progn (c-forward-single-comment) (point))))
2745 (skip-chars-backward " \t")
2747 (let ((end (point)) beg
)
2750 (< (skip-syntax-backward c-string-syntax
) 0)) ; String.
2751 (setq beg
(c-safe (c-backward-sexp 1) (point))))
2753 ((and (c-safe (forward-char -
2) t
)
2755 ;; Block comment. Due to the nature of line
2756 ;; comments, they will always be covered by the
2757 ;; normal case above.
2759 (c-backward-single-comment)
2760 ;; If LIM is bogus, beg will be bogus.
2761 (setq beg
(point))))
2763 (if beg
(cons beg end
))))))
2766 (defun c-literal-limits-fast (&optional lim near not-in-delimiter
)
2767 ;; Like c-literal-limits, but for emacsen whose `parse-partial-sexp'
2768 ;; returns the pos of the comment start.
2770 "Return a cons of the beginning and end positions of the comment or
2771 string surrounding point (including both delimiters), or nil if point
2772 isn't in one. If LIM is non-nil, it's used as the \"safe\" position
2773 to start parsing from. If NEAR is non-nil, then the limits of any
2774 literal next to point is returned. \"Next to\" means there's only
2775 spaces and tabs between point and the literal. The search for such a
2776 literal is done first in forward direction. If NOT-IN-DELIMITER is
2777 non-nil, the case when point is inside a starting delimiter won't be
2778 recognized. This only has effect for comments, which have starting
2779 delimiters with more than one character.
2781 This function does not do any hidden buffer changes."
2784 (let* ((pos (point))
2786 (c-beginning-of-syntax)
2788 (state (parse-partial-sexp lim pos
)))
2790 (cond ((elt state
3) ; String.
2791 (goto-char (elt state
8))
2792 (cons (point) (or (c-safe (c-forward-sexp 1) (point))
2795 ((elt state
4) ; Comment.
2796 (goto-char (elt state
8))
2797 (cons (point) (progn (c-forward-single-comment) (point))))
2799 ((and (not not-in-delimiter
)
2801 (eq (char-before) ?
/)
2802 (looking-at "[/*]"))
2803 ;; We're standing in a comment starter.
2805 (cons (point) (progn (c-forward-single-comment) (point))))
2810 ;; Search forward for a literal.
2811 (skip-chars-forward " \t")
2814 ((looking-at c-string-limit-regexp
) ; String.
2815 (cons (point) (or (c-safe (c-forward-sexp 1) (point))
2818 ((looking-at c-comment-start-regexp
) ; Line or block comment.
2819 (cons (point) (progn (c-forward-single-comment) (point))))
2823 (skip-chars-backward " \t")
2825 (let ((end (point)) beg
)
2828 (< (skip-syntax-backward c-string-syntax
) 0)) ; String.
2829 (setq beg
(c-safe (c-backward-sexp 1) (point))))
2831 ((and (c-safe (forward-char -
2) t
)
2833 ;; Block comment. Due to the nature of line
2834 ;; comments, they will always be covered by the
2835 ;; normal case above.
2837 (c-backward-single-comment)
2838 ;; If LIM is bogus, beg will be bogus.
2839 (setq beg
(point))))
2841 (if beg
(cons beg end
))))))
2844 (if (memq 'pps-extended-state c-emacs-features
)
2845 (defalias 'c-literal-limits
'c-literal-limits-fast
))
2847 (defun c-collect-line-comments (range)
2848 "If the argument is a cons of two buffer positions (such as returned by
2849 `c-literal-limits'), and that range contains a C++ style line comment,
2850 then an extended range is returned that contains all adjacent line
2851 comments (i.e. all comments that starts in the same column with no
2852 empty lines or non-whitespace characters between them). Otherwise the
2853 argument is returned.
2855 This function does not do any hidden buffer changes."
2858 (if (and (consp range
) (progn
2859 (goto-char (car range
))
2861 (let ((col (current-column))
2863 (bopl (c-point 'bopl
))
2865 ;; Got to take care in the backward direction to handle
2866 ;; comments which are preceded by code.
2867 (while (and (c-backward-single-comment)
2870 (= col
(current-column)))
2872 bopl
(c-point 'bopl
)))
2874 (while (and (progn (skip-chars-forward " \t")
2876 (= col
(current-column))
2877 (prog1 (zerop (forward-line 1))
2878 (setq end
(point)))))
2883 (defun c-literal-type (range)
2884 "Convenience function that given the result of `c-literal-limits',
2885 returns nil or the type of literal that the range surrounds. It's
2886 much faster than using `c-in-literal' and is intended to be used when
2887 you need both the type of a literal and its limits.
2889 This function does not do any hidden buffer changes."
2892 (goto-char (car range
))
2893 (cond ((looking-at c-string-limit-regexp
) 'string
)
2894 ((or (looking-at "//") ; c++ line comment
2895 (and (looking-at "\\s<") ; comment starter
2896 (looking-at "#"))) ; awk comment.
2898 (t 'c
))) ; Assuming the range is valid.
2902 ;; `c-find-decl-spots' and accompanying stuff.
2904 ;; Variables used in `c-find-decl-spots' to cache the search done for
2905 ;; the first declaration in the last call. When that function starts,
2906 ;; it needs to back up over syntactic whitespace to look at the last
2907 ;; token before the region being searched. That can sometimes cause
2908 ;; moves back and forth over a quite large region of comments and
2909 ;; macros, which would be repeated for each changed character when
2910 ;; we're called during fontification, since font-lock refontifies the
2911 ;; current line for each change. Thus it's worthwhile to cache the
2914 ;; `c-find-decl-syntactic-pos' is a syntactically relevant position in
2915 ;; the syntactic whitespace less or equal to some start position.
2916 ;; There's no cached value if it's nil.
2918 ;; `c-find-decl-match-pos' is the match position if
2919 ;; `c-find-decl-prefix-search' matched before the syntactic whitespace
2920 ;; at `c-find-decl-syntactic-pos', or nil if there's no such match.
2921 (defvar c-find-decl-syntactic-pos nil
)
2922 (make-variable-buffer-local 'c-find-decl-syntactic-pos
)
2923 (defvar c-find-decl-match-pos nil
)
2924 (make-variable-buffer-local 'c-find-decl-match-pos
)
2926 (defsubst c-invalidate-find-decl-cache
(change-min-pos)
2927 (and c-find-decl-syntactic-pos
2928 (< change-min-pos c-find-decl-syntactic-pos
)
2929 (setq c-find-decl-syntactic-pos nil
)))
2931 ; (defface c-debug-decl-spot-face
2932 ; '((t (:background "Turquoise")))
2933 ; "Debug face to mark the spots where `c-find-decl-spots' stopped.")
2934 ; (defface c-debug-decl-sws-face
2935 ; '((t (:background "Khaki")))
2936 ; "Debug face to mark the syntactic whitespace between the declaration
2937 ; spots and the preceding token end.")
2939 (defmacro c-debug-put-decl-spot-faces
(match-pos decl-pos
)
2940 (when (facep 'c-debug-decl-spot-face
)
2941 `(let ((match-pos ,match-pos
) (decl-pos ,decl-pos
))
2942 (c-debug-add-face (max match-pos
(point-min)) decl-pos
2943 'c-debug-decl-sws-face
)
2944 (c-debug-add-face decl-pos
(min (1+ decl-pos
) (point-max))
2945 'c-debug-decl-spot-face
))))
2946 (defmacro c-debug-remove-decl-spot-faces
(beg end
)
2947 (when (facep 'c-debug-decl-spot-face
)
2949 (c-debug-remove-face ,beg
,end
'c-debug-decl-spot-face
)
2950 (c-debug-remove-face ,beg
,end
'c-debug-decl-sws-face
))))
2952 (defmacro c-find-decl-prefix-search
()
2953 ;; Macro used inside `c-find-decl-spots'. It ought to be a defun,
2954 ;; but it contains lots of free variables that refer to things
2955 ;; inside `c-find-decl-spots'. The point is left at `cfd-match-pos'
2956 ;; if there is a match, otherwise at `cfd-limit'.
2959 ;; Find the next property match position if we haven't got one already.
2960 (unless cfd-prop-match
2963 (goto-char (next-single-property-change
2964 (point) 'c-type nil cfd-limit
))
2965 (and (< (point) cfd-limit
)
2966 (not (eq (c-get-char-property (1- (point)) 'c-type
)
2968 (setq cfd-prop-match
(point))))
2970 ;; Find the next `c-decl-prefix-re' match if we haven't got one already.
2971 (unless cfd-re-match
2972 (while (and (setq cfd-re-match
2973 (re-search-forward c-decl-prefix-re cfd-limit
'move
))
2974 (c-got-face-at (1- (setq cfd-re-match
(match-end 1)))
2976 ;; Search again if the match is within a comment or a string literal.
2978 (goto-char (next-single-property-change
2979 cfd-re-match
'face nil cfd-limit
))
2980 (and (< (point) cfd-limit
)
2981 (c-got-face-at (point) c-literal-faces
)))
2982 (setq cfd-re-match
(point))))
2983 (unless cfd-re-match
2984 (setq cfd-re-match cfd-limit
)))
2986 ;; Choose whichever match is closer to the start.
2987 (if (< cfd-re-match cfd-prop-match
)
2988 (setq cfd-match-pos cfd-re-match
2990 (setq cfd-match-pos cfd-prop-match
2991 cfd-prop-match nil
))
2993 (goto-char cfd-match-pos
)
2995 (when (< cfd-match-pos cfd-limit
)
2996 ;; Skip forward past comments only so we don't skip macros.
2997 (c-forward-comments)
2998 ;; Set the position to continue at. We can avoid going over
2999 ;; the comments skipped above a second time, but it's possible
3000 ;; that the comment skipping has taken us past `cfd-prop-match'
3001 ;; since the property might be used inside comments.
3002 (setq cfd-continue-pos
(if cfd-prop-match
3003 (min cfd-prop-match
(point))
3006 (defun c-find-decl-spots (cfd-limit cfd-decl-re cfd-face-checklist cfd-fun
)
3007 ;; Call CFD-FUN for each possible spot for a declaration from the
3008 ;; point to CFD-LIMIT. A spot for a declaration is the first token
3009 ;; in the buffer and each token after the ones matched by
3010 ;; `c-decl-prefix-re' and after the occurrences of the `c-type'
3011 ;; property with the value `c-decl-end' (if `c-type-decl-end-used'
3012 ;; is set). Only a spot that match CFD-DECL-RE and whose face is in
3013 ;; the CFD-FACE-CHECKLIST list causes CFD-FUN to be called. The
3014 ;; face check is disabled if CFD-FACE-CHECKLIST is nil.
3016 ;; If the match is inside a macro then the buffer is narrowed to the
3017 ;; end of it, so that CFD-FUN can investigate the following tokens
3018 ;; without matching something that begins inside a macro and ends
3019 ;; outside it. It's to avoid this work that the CFD-DECL-RE and
3020 ;; CFD-FACE-CHECKLIST checks exist.
3022 ;; CFD-FUN is called with point at the start of the spot. It's
3023 ;; passed two arguments: The first is the end position of the token
3024 ;; that `c-decl-prefix-re' matched, or 0 for the implicit match at
3025 ;; bob. The second is a flag that is t when the match is inside a
3028 ;; It's assumed that comment and strings are fontified in the
3031 ;; This is mainly used in fontification, and so has an elaborate
3032 ;; cache to handle repeated calls from the same start position; see
3033 ;; the variables above.
3035 ;; All variables in this function begin with `cfd-' to avoid name
3036 ;; collision with the (dynamically bound) variables used in CFD-FUN.
3038 (let ((cfd-buffer-end (point-max))
3039 ;; The last regexp match found by `c-find-decl-prefix-search'.
3041 ;; The last `c-decl-end' found by `c-find-decl-prefix-search'.
3042 ;; If searching for the property isn't needed then we disable
3043 ;; it by faking a first match at the limit.
3044 (cfd-prop-match (unless c-type-decl-end-used cfd-limit
))
3045 ;; The position of the last match found by
3046 ;; `c-find-decl-prefix-search'. For regexp matches it's the
3047 ;; end of the matched token, for property matches it's the end
3048 ;; of the property. 0 for the implicit match at bob.
3049 ;; `cfd-limit' if there's no match.
3050 (cfd-match-pos cfd-limit
)
3051 ;; The position to continue searching at.
3053 ;; The position of the last "real" token we've stopped at.
3054 ;; This can be greater than `cfd-continue-pos' when we get
3055 ;; hits inside macros or at `c-decl-end' positions inside
3058 ;; The end position of the last entered macro.
3061 ;; Initialize by finding a syntactically relevant start position
3062 ;; before the point, and do the first `c-decl-prefix-re' search
3063 ;; unless we're at bob.
3065 (let ((start-pos (point)) syntactic-pos
)
3066 ;; Must back up a bit since we look for the end of the previous
3067 ;; statement or declaration, which is earlier than the first
3070 (when (c-got-face-at (point) c-literal-faces
)
3071 ;; But first we need to move to a syntactically relevant
3072 ;; position. Use the faces to back up to the start of the
3073 ;; comment or string literal.
3074 (when (and (not (bobp))
3075 (c-got-face-at (1- (point)) c-literal-faces
))
3077 (goto-char (previous-single-property-change
3078 (point) 'face nil
(point-min)))
3079 (and (> (point) (point-min))
3080 (c-got-face-at (point) c-literal-faces
)))))
3082 ;; XEmacs doesn't fontify the quotes surrounding string
3084 (and (featurep 'xemacs
)
3085 (eq (get-text-property (point) 'face
)
3086 'font-lock-string-face
)
3088 (progn (backward-char)
3089 (not (looking-at c-string-limit-regexp
)))
3092 ;; The font lock package might not have fontified the start of
3093 ;; the literal at all so check that we have arrived at
3094 ;; something that looks like a start or else resort to
3095 ;; `c-literal-limits'.
3096 (unless (looking-at c-literal-start-regexp
)
3097 (let ((range (c-literal-limits)))
3098 (if range
(goto-char (car range
))))))
3100 ;; Must back out of any macro so that we don't miss any
3101 ;; declaration that could follow after it, unless the limit is
3102 ;; inside the macro. We only check that for the current line to
3103 ;; save some time; it's enough for the by far most common case
3104 ;; when font-lock refontifies the current line only.
3105 (when (save-excursion
3106 (and (= (forward-line 1) 0)
3107 (or (< (c-point 'eol
) cfd-limit
)
3108 (progn (backward-char)
3109 (not (eq (char-before) ?
\\))))))
3110 (c-beginning-of-macro))
3112 ;; Clear the cache if it applied further down.
3113 (c-invalidate-find-decl-cache start-pos
)
3115 (setq syntactic-pos
(point))
3116 (c-backward-syntactic-ws c-find-decl-syntactic-pos
)
3118 ;; If we hit `c-find-decl-syntactic-pos' and
3119 ;; `c-find-decl-match-pos' is set then we install the cached
3120 ;; values. If we hit `c-find-decl-syntactic-pos' and
3121 ;; `c-find-decl-match-pos' is nil then we know there's no decl
3122 ;; prefix in the whitespace before `c-find-decl-syntactic-pos'
3123 ;; and so we can continue the search from this point. If we
3124 ;; didn't hit `c-find-decl-syntactic-pos' then we're now in the
3125 ;; right spot to begin searching anyway.
3126 (if (and (eq (point) c-find-decl-syntactic-pos
)
3127 c-find-decl-match-pos
)
3130 ;; The match is always outside macros and comments so we
3131 ;; start at the next token. The loop below will later go
3132 ;; back using `cfd-continue-pos' to fix declarations inside
3133 ;; the syntactic ws.
3134 (goto-char syntactic-pos
)
3135 (c-forward-syntactic-ws)
3136 (setq cfd-match-pos c-find-decl-match-pos
3137 cfd-continue-pos syntactic-pos
)
3138 (if (< cfd-continue-pos
(point))
3139 (setq cfd-token-pos
(point))))
3141 (setq c-find-decl-syntactic-pos syntactic-pos
)
3144 ;; Always consider bob a match to get the first declaration
3145 ;; in the file. Do this separately instead of letting
3146 ;; `c-decl-prefix-re' match bob, so that it always can
3147 ;; consume at least one character to ensure that we won't
3148 ;; get stuck in an infinite loop.
3149 (setq cfd-re-match
0)
3151 (c-beginning-of-current-token)
3152 (< (point) cfd-limit
))
3153 ;; Do an initial search now. In the bob case above it's only done
3154 ;; to search for the `c-type' property.
3155 (c-find-decl-prefix-search))
3157 ;; Advance `cfd-continue-pos' if we got a hit before the start
3158 ;; position. The earliest position that could affect after
3159 ;; the start position is the char before the preceding
3161 (when (and cfd-continue-pos
(< cfd-continue-pos start-pos
))
3162 (goto-char syntactic-pos
)
3163 (c-backward-comments)
3166 (c-beginning-of-current-token))
3167 (setq cfd-continue-pos
(max cfd-continue-pos
(point))))
3169 ;; If we got a match it's always outside macros and comments so
3170 ;; advance to the next token and set `cfd-token-pos'. The loop
3171 ;; below will later go back using `cfd-continue-pos' to fix
3172 ;; declarations inside the syntactic ws.
3173 (when (and (< cfd-match-pos cfd-limit
) (< (point) syntactic-pos
))
3174 (goto-char syntactic-pos
)
3175 (c-forward-syntactic-ws)
3176 (and cfd-continue-pos
3177 (< cfd-continue-pos
(point))
3178 (setq cfd-token-pos
(point))))
3180 (setq c-find-decl-match-pos
(and (< cfd-match-pos start-pos
)
3183 ;; Now loop. We already got the first match.
3187 (< cfd-match-pos cfd-limit
)
3190 ;; Kludge to filter out matches on the "<" that
3191 ;; aren't open parens, for the sake of languages
3192 ;; that got `c-recognize-<>-arglists' set.
3193 (and (eq (char-before cfd-match-pos
) ?
<)
3194 (not (c-get-char-property (1- cfd-match-pos
)
3197 ;; If `cfd-continue-pos' is less or equal to
3198 ;; `cfd-token-pos', we've got a hit inside a macro
3199 ;; that's in the syntactic whitespace before the last
3200 ;; "real" declaration we've checked. If they're equal
3201 ;; we've arrived at the declaration a second time, so
3202 ;; there's nothing to do.
3203 (= cfd-continue-pos cfd-token-pos
)
3206 ;; If `cfd-continue-pos' is less than `cfd-token-pos'
3207 ;; we're still searching for declarations embedded in
3208 ;; the syntactic whitespace. In that case we need
3209 ;; only to skip comments and not macros, since they
3210 ;; can't be nested, and that's already been done in
3211 ;; `c-find-decl-prefix-search'.
3212 (when (> cfd-continue-pos cfd-token-pos
)
3213 (c-forward-syntactic-ws)
3214 (setq cfd-token-pos
(point)))
3216 ;; Continue if the following token fails the
3217 ;; CFD-DECL-RE and CFD-FACE-CHECKLIST checks.
3218 (when (or (>= (point) cfd-limit
)
3219 (not (looking-at cfd-decl-re
))
3220 (and cfd-face-checklist
3222 (point) cfd-face-checklist
))))
3223 (goto-char cfd-continue-pos
)
3226 (< (point) cfd-limit
))
3227 (c-find-decl-prefix-search))
3229 (< (point) cfd-limit
))
3232 ;; Narrow to the end of the macro if we got a hit inside
3233 ;; one, to avoid recognizing things that start inside
3234 ;; the macro and end outside it.
3235 (when (> cfd-match-pos cfd-macro-end
)
3236 ;; Not in the same macro as in the previous round.
3238 (goto-char cfd-match-pos
)
3240 (if (save-excursion (and (c-beginning-of-macro)
3241 (< (point) cfd-match-pos
)))
3242 (progn (c-end-of-macro)
3246 (if (zerop cfd-macro-end
)
3248 (if (> cfd-macro-end
(point))
3249 (progn (narrow-to-region (point-min) cfd-macro-end
)
3251 ;; The matched token was the last thing in the
3252 ;; macro, so the whole match is bogus.
3253 (setq cfd-macro-end
0)
3256 (c-debug-put-decl-spot-faces cfd-match-pos
(point))
3257 (funcall cfd-fun cfd-match-pos
(/= cfd-macro-end
0))
3259 (when (/= cfd-macro-end
0)
3260 ;; Restore limits if we did macro narrowment above.
3261 (narrow-to-region (point-min) cfd-buffer-end
)))
3263 (goto-char cfd-continue-pos
)
3264 (if (= cfd-continue-pos cfd-limit
)
3265 (setq cfd-match-pos cfd-limit
)
3266 (c-find-decl-prefix-search)))))
3269 ;; A cache for found types.
3271 ;; Buffer local variable that contains an obarray with the types we've
3272 ;; found. If a declaration is recognized somewhere we record the
3273 ;; fully qualified identifier in it to recognize it as a type
3274 ;; elsewhere in the file too. This is not accurate since we do not
3275 ;; bother with the scoping rules of the languages, but in practice the
3276 ;; same name is seldom used as both a type and something else in a
3277 ;; file, and we only use this as a last resort in ambiguous cases (see
3278 ;; `c-font-lock-declarations').
3279 (defvar c-found-types nil
)
3280 (make-variable-buffer-local 'c-found-types
)
3282 (defsubst c-clear-found-types
()
3283 ;; Clears `c-found-types'.
3285 ;; This function does not do any hidden buffer changes.
3286 (setq c-found-types
(make-vector 53 0)))
3288 (defun c-add-type (from to
)
3289 ;; Add the given region as a type in `c-found-types'. If the region
3290 ;; doesn't match an existing type but there is a type which is equal
3291 ;; to the given one except that the last character is missing, then
3292 ;; the shorter type is removed. That's done to avoid adding all
3293 ;; prefixes of a type as it's being entered and font locked. This
3294 ;; doesn't cover cases like when characters are removed from a type
3295 ;; or added in the middle. We'd need the position of point when the
3296 ;; font locking is invoked to solve this well.
3297 (unless (and c-recognize-
<>-arglists
3300 (c-syntactic-re-search-forward "<" to t
)))
3301 ;; To avoid storing very long strings, do not add a type that
3302 ;; contains '<' in languages with angle bracket arglists, since
3303 ;; the type then probably contains a C++ template spec and those
3304 ;; can be fairly sized programs in themselves.
3305 (let ((type (c-syntactic-content from to
)))
3306 (unless (intern-soft type c-found-types
)
3307 (unintern (substring type
0 -
1) c-found-types
)
3308 (intern type c-found-types
)))))
3310 (defsubst c-check-type
(from to
)
3311 ;; Return non-nil if the given region contains a type in
3313 (intern-soft (c-syntactic-content from to
) c-found-types
))
3315 (defun c-list-found-types ()
3316 ;; Return all the types in `c-found-types' as a sorted list of
3319 (mapatoms (lambda (type)
3320 (setq type-list
(cons (symbol-name type
)
3323 (sort type-list
'string-lessp
)))
3326 ;; Handling of small scale constructs like types and names.
3328 (defun c-remove-<>-arglist-properties
(from to
)
3329 ;; Remove all the properties put by `c-forward-<>-arglist' in the
3330 ;; specified region. Point is clobbered.
3332 (while (progn (skip-chars-forward "^<>," to
)
3334 (if (eq (char-after) ?
,)
3335 (when (eq (c-get-char-property (point) 'c-type
) 'c-
<>-arg-sep
)
3336 (c-clear-char-property (point) 'c-type
))
3337 (c-clear-char-property (point) 'syntax-table
))
3340 ;; Dynamically bound variable that instructs `c-forward-type' to also
3341 ;; treat possible types (i.e. those that it normally returns 'maybe or
3342 ;; 'found for) as actual types (and always return 'found for them).
3343 ;; This means that it records them in `c-record-type-identifiers' if
3344 ;; that is set, and that it adds them to `c-found-types'.
3345 (defvar c-promote-possible-types nil
)
3347 ;; Dynamically bound variable that instructs `c-forward-<>-arglist' to
3348 ;; not accept arglists that contain more than one argument. It's used
3349 ;; to handle ambiguous cases like "foo (a < b, c > d)" better.
3350 (defvar c-disallow-comma-in-
<>-arglists nil
)
3352 ;; Dynamically bound variables that instructs `c-forward-name',
3353 ;; `c-forward-type' and `c-forward-<>-arglist' to record the ranges of
3354 ;; all the type and reference identifiers they encounter. They will
3355 ;; build lists on these variables where each element is a cons of the
3356 ;; buffer positions surrounding each identifier. This recording is
3357 ;; only activated when `c-record-type-identifiers' is non-nil.
3359 ;; All known types that can't be identifiers are recorded, and also
3360 ;; other possible types if `c-promote-possible-types' is set.
3361 ;; Recording is however disabled inside angle bracket arglists that
3362 ;; are encountered inside names and other angle bracket arglists.
3363 ;; Such occurences are taken care of by `c-font-lock-<>-arglists'
3366 ;; Only the names in C++ template style references (e.g. "tmpl" in
3367 ;; "tmpl<a,b>::foo") are recorded as references, other references
3368 ;; aren't handled here.
3369 (defvar c-record-type-identifiers nil
)
3370 (defvar c-record-ref-identifiers nil
)
3372 ;; If `c-record-type-identifiers' is set, this will receive a cons
3373 ;; cell of the range of the last single identifier symbol stepped over
3374 ;; by `c-forward-name' if it's successful. This is the range that
3375 ;; should be put on one of the record lists by the caller. It's
3376 ;; assigned nil if there's no such symbol in the name.
3377 (defvar c-last-identifier-range nil
)
3379 (defmacro c-record-type-id
(range)
3380 (if (eq (car-safe range
) 'cons
)
3382 `(setq c-record-type-identifiers
3383 (cons ,range c-record-type-identifiers
))
3384 `(let ((range ,range
))
3386 (setq c-record-type-identifiers
3387 (cons range c-record-type-identifiers
))))))
3389 (defmacro c-record-ref-id
(range)
3390 (if (eq (car-safe range
) 'cons
)
3392 `(setq c-record-ref-identifiers
3393 (cons ,range c-record-ref-identifiers
))
3394 `(let ((range ,range
))
3396 (setq c-record-ref-identifiers
3397 (cons range c-record-ref-identifiers
))))))
3399 ;; Dynamically bound variable that instructs `c-forward-type' to
3400 ;; record the ranges of types that only are found. Behaves otherwise
3401 ;; like `c-record-type-identifiers'.
3402 (defvar c-record-found-types nil
)
3404 (defmacro c-forward-keyword-prefixed-id
(type)
3405 ;; Used internally in `c-forward-keyword-clause' to move forward
3406 ;; over a type (if TYPE is 'type) or a name (otherwise) which
3407 ;; possibly is prefixed by keywords and their associated clauses.
3408 ;; Try with a type/name first to not trip up on those that begin
3409 ;; with a keyword. Return t if a known or found type is moved
3410 ;; over. The point is clobbered if nil is returned. If range
3411 ;; recording is enabled, the identifier is recorded on as a type
3412 ;; if TYPE is 'type or as a reference if TYPE is 'ref.
3414 (while (if (setq res
,(if (eq type
'type
)
3418 (and (looking-at c-keywords-regexp
)
3419 (c-forward-keyword-clause))))
3420 (when (memq res
'(t known found prefix
))
3421 ,(when (eq type
'ref
)
3422 `(when c-record-type-identifiers
3423 (c-record-ref-id c-last-identifier-range
)))
3426 (defmacro c-forward-id-comma-list
(type)
3427 ;; Used internally in `c-forward-keyword-clause' to move forward
3428 ;; over a comma separated list of types or names using
3429 ;; `c-forward-keyword-prefixed-id'.
3431 (setq safe-pos
(point))
3432 (eq (char-after) ?
,))
3435 (c-forward-syntactic-ws)
3436 (c-forward-keyword-prefixed-id ,type
)))))
3438 (defun c-forward-keyword-clause ()
3439 ;; The first submatch in the current match data is assumed to
3440 ;; surround a token. If it's a keyword, move over it and any
3441 ;; following clauses associated with it, stopping at the next
3442 ;; following token. t is returned in that case, otherwise the point
3443 ;; stays and nil is returned. The kind of clauses that are
3444 ;; recognized are those specified by `c-type-list-kwds',
3445 ;; `c-ref-list-kwds', `c-colon-type-list-kwds',
3446 ;; `c-paren-nontype-kwds', `c-paren-type-kwds', `c-<>-type-kwds',
3447 ;; and `c-<>-arglist-kwds'.
3449 (let ((kwd-sym (c-keyword-sym (match-string 1))) safe-pos pos
)
3451 (goto-char (match-end 1))
3452 (c-forward-syntactic-ws)
3453 (setq safe-pos
(point))
3456 ((and (c-keyword-member kwd-sym
'c-type-list-kwds
)
3457 (c-forward-keyword-prefixed-id type
))
3458 ;; There's a type directly after a keyword in `c-type-list-kwds'.
3459 (c-forward-id-comma-list type
))
3461 ((and (c-keyword-member kwd-sym
'c-ref-list-kwds
)
3462 (c-forward-keyword-prefixed-id ref
))
3463 ;; There's a name directly after a keyword in `c-ref-list-kwds'.
3464 (c-forward-id-comma-list ref
))
3466 ((and (c-keyword-member kwd-sym
'c-paren-any-kwds
)
3467 (eq (char-after) ?\
())
3468 ;; There's an open paren after a keyword in `c-paren-any-kwds'.
3471 (when (and (setq pos
(c-up-list-forward))
3472 (eq (char-before pos
) ?\
)))
3473 (when (and c-record-type-identifiers
3474 (c-keyword-member kwd-sym
'c-paren-type-kwds
))
3475 ;; Use `c-forward-type' on every identifier we can find
3476 ;; inside the paren, to record the types.
3477 (while (c-syntactic-re-search-forward c-symbol-start pos t
)
3478 (goto-char (match-beginning 0))
3479 (unless (c-forward-type)
3480 (looking-at c-symbol-key
) ; Always matches.
3481 (goto-char (match-end 0)))))
3484 (c-forward-syntactic-ws)
3485 (setq safe-pos
(point))))
3487 ((and (c-keyword-member kwd-sym
'c-
<>-sexp-kwds
)
3488 (eq (char-after) ?
<)
3489 (c-forward-<>-arglist
(c-keyword-member kwd-sym
'c-
<>-type-kwds
)
3490 (or c-record-type-identifiers
3491 c-disallow-comma-in-
<>-arglists
)))
3492 (c-forward-syntactic-ws)
3493 (setq safe-pos
(point)))
3495 ((and (c-keyword-member kwd-sym
'c-nonsymbol-sexp-kwds
)
3496 (not (looking-at c-symbol-start
)))
3498 (c-forward-syntactic-ws)
3499 (setq safe-pos
(point))))
3501 (when (and (c-keyword-member kwd-sym
'c-colon-type-list-kwds
)
3503 ;; If a keyword matched both one of the types above and
3504 ;; this one, we match `c-colon-type-list-re' after the
3505 ;; clause matched above.
3506 (goto-char safe-pos
)
3507 (looking-at c-colon-type-list-re
))
3509 (goto-char (match-end 0))
3510 (c-forward-syntactic-ws)
3511 (c-forward-keyword-prefixed-id type
)))
3512 ;; There's a type after the `c-colon-type-list-re'
3513 ;; match after a keyword in `c-colon-type-list-kwds'.
3514 (c-forward-id-comma-list type
))
3516 (goto-char safe-pos
)
3519 (defun c-forward-<>-arglist
(all-types reparse
)
3520 ;; The point is assumed to be at a '<'. Try to treat it as the open
3521 ;; paren of an angle bracket arglist and move forward to the the
3522 ;; corresponding '>'. If successful, the point is left after the
3523 ;; '>' and t is returned, otherwise the point isn't moved and nil is
3524 ;; returned. If ALL-TYPES is t then all encountered arguments in
3525 ;; the arglist that might be types are treated as found types.
3527 ;; The surrounding '<' and '>' are given syntax-table properties to
3528 ;; make them behave like parentheses. Each argument separating ','
3529 ;; is also set to `c-<>-arg-sep' in the `c-type' property. These
3530 ;; properties are also cleared in a relevant region forward from the
3531 ;; point if they seems to be set and it turns out to not be an
3534 ;; If the arglist has been successfully parsed before then paren
3535 ;; syntax properties will be exploited to quickly jump to the end,
3536 ;; but that can be disabled by setting REPARSE to t. That is
3537 ;; necessary if the various side effects, e.g. recording of type
3538 ;; ranges, are important. Setting REPARSE to t only applies
3539 ;; recursively to nested angle bracket arglists if
3540 ;; `c-disallow-comma-in-<>-arglists' is set.
3542 ;; This is primarily used in C++ to mark up template arglists. C++
3543 ;; disambiguates them by checking whether the preceding name is a
3544 ;; template or not. We can't do that, so we assume it is a template
3545 ;; if it can be parsed as one. This usually works well since
3546 ;; comparison expressions on the forms "a < b > c" or "a < b, c > d"
3547 ;; in almost all cases would be pointless. Cases like function
3548 ;; calls on the form "foo (a < b, c > d)" needs to be handled
3549 ;; specially through the `c-disallow-comma-in-<>-arglists' variable.
3551 (let ((start (point))
3552 ;; If `c-record-type-identifiers' is set then activate
3553 ;; recording of any found types that constitute an argument in
3555 (c-record-found-types (if c-record-type-identifiers t
)))
3556 (if (catch 'angle-bracket-arglist-escape
3557 (setq c-record-found-types
3558 (c-forward-<>-arglist-recur all-types reparse
)))
3560 (when (consp c-record-found-types
)
3561 (setq c-record-type-identifiers
3562 ;; `nconc' doesn't mind that the tail of
3563 ;; `c-record-found-types' is t.
3564 (nconc c-record-found-types c-record-type-identifiers
)))
3570 (defun c-forward-<>-arglist-recur
(all-types reparse
)
3571 ;; Recursive part of `c-forward-<>-arglist'.
3573 (let ((start (point)) res pos tmp
3574 ;; Cover this so that any recorded found type ranges are
3575 ;; automatically lost if it turns out to not be an angle
3576 ;; bracket arglist. It's propagated through the return value
3577 ;; on successful completion.
3578 (c-record-found-types c-record-found-types
)
3579 ;; List that collects the positions after the argument
3580 ;; separating ',' in the arglist.
3583 ;; If the '<' has paren open syntax then we've marked it as an
3584 ;; angle bracket arglist before, so try to skip to the end and see
3585 ;; that the close paren matches.
3586 (if (and (c-get-char-property (point) 'syntax-table
)
3589 (if (and (not (looking-at c-
<-op-cont-regexp
))
3590 (if (c-parse-sexp-lookup-properties)
3591 (c-go-up-list-forward)
3594 (while (c-syntactic-re-search-forward
3596 (when (c-get-char-property (1- (point))
3598 (if (eq (char-before) ?
<)
3599 (setq depth
(1+ depth
))
3600 (setq depth
(1- depth
))
3601 (when (= depth
0) (throw 'at-end t
)))))
3603 (not (looking-at c-
>-op-cont-regexp
))
3607 (progn (c-beginning-of-current-token)
3610 ;; Got an arglist that appears to be valid.
3612 ;; Reparsing is requested, so zap the properties in the
3613 ;; region and go on to redo it. It's done here to
3614 ;; avoid leaving it behind if we exit through
3615 ;; `angle-bracket-arglist-escape' below.
3617 (c-remove-<>-arglist-properties start
(point))
3622 ;; Got unmatched paren brackets or either paren was
3623 ;; actually some other token. Recover by clearing the
3624 ;; syntax properties on all the '<' and '>' in the
3625 ;; range where we'll search for the arglist below.
3627 (while (progn (skip-chars-forward "^<>,;{}")
3628 (looking-at "[<>,]"))
3629 (if (eq (char-after) ?
,)
3630 (when (eq (c-get-char-property (point) 'c-type
)
3632 (c-clear-char-property (point) 'c-type
))
3633 (c-clear-char-property (point) 'syntax-table
))
3640 (unless (looking-at c-
<-op-cont-regexp
)
3644 (when c-record-type-identifiers
3647 ;; All encountered identifiers are types, so set the
3648 ;; promote flag and parse the type.
3650 (c-forward-syntactic-ws)
3651 (when (looking-at c-identifier-start
)
3652 (let ((c-promote-possible-types t
))
3655 ;; Check if this arglist argument is a sole type. If
3656 ;; it's known then it's recorded in
3657 ;; `c-record-type-identifiers'. If it only is found
3658 ;; then it's recorded in `c-record-found-types' which we
3659 ;; might roll back if it turns out that this isn't an
3660 ;; angle bracket arglist afterall.
3661 (when (memq (char-before) '(?
, ?
<))
3662 (let ((orig-record-found-types c-record-found-types
))
3663 (c-forward-syntactic-ws)
3664 (and (memq (c-forward-type) '(known found
))
3665 (not (looking-at "[,>]"))
3666 ;; A found type was recorded but it's not the
3667 ;; only thing in the arglist argument, so reset
3668 ;; `c-record-found-types'.
3669 (setq c-record-found-types
3670 orig-record-found-types
))))))
3673 (or (when (eq (char-after) ?
>)
3674 ;; Must check for '>' at the very start separately,
3675 ;; since the regexp below has to avoid ">>" without
3680 ;; Note: This regexp exploits the match order in
3681 ;; \| so that "<>" is matched by "<" rather than
3683 (c-syntactic-re-search-forward
3684 "[<;{},]\\|\\([^>:-]>\\)" nil
'move t t
1)
3686 ;; If the arglist starter has lost its open paren
3687 ;; syntax but not the closer, we won't find the
3688 ;; closer above since we only search in the
3689 ;; balanced sexp. In that case we stop just short
3690 ;; of it so check if the following char is the closer.
3691 (when (eq (char-after) ?
>)
3692 ;; Remove its syntax so that we don't enter the
3693 ;; recovery code below. That's not necessary
3694 ;; since there's no real reason to suspect that
3695 ;; things inside the arglist are unbalanced.
3696 (c-clear-char-property (point) 'syntax-table
)
3701 ((eq (char-before) ?
>)
3702 ;; Either an operator starting with '>' or the end of
3703 ;; the angle bracket arglist.
3705 (if (and (/= (1- (point)) pos
)
3706 (c-get-char-property (1- (point)) 'syntax-table
)
3708 (c-clear-char-property (1- (point)) 'syntax-table
)
3709 (c-parse-sexp-lookup-properties)))
3711 ;; We've skipped past a list that ended with '>'. It
3712 ;; must be unbalanced since nested arglists are handled
3713 ;; in the case below. Recover by removing all paren
3714 ;; properties on '<' and '>' in the searched region and
3717 (c-remove-<>-arglist-properties pos
(point))
3721 (if (looking-at c-
>-op-cont-regexp
)
3723 (when (text-property-not-all
3724 (1- (point)) (match-end 0) 'syntax-table nil
)
3725 (c-remove-<>-arglist-properties
(1- (point))
3727 (goto-char (match-end 0))
3730 ;; The angle bracket arglist is finished.
3731 (while arg-start-pos
3732 (c-put-char-property (1- (car arg-start-pos
))
3733 'c-type
'c-
<>-arg-sep
)
3734 (setq arg-start-pos
(cdr arg-start-pos
)))
3735 (c-mark-<-as-paren start
)
3736 (c-mark->-as-paren
(1- (point)))
3740 ((eq (char-before) ?
<)
3741 ;; Either an operator starting with '<' or a nested arglist.
3744 (let (id-start id-end subres keyword-match
)
3745 (if (if (looking-at c-
<-op-cont-regexp
)
3746 (setq tmp
(match-end 0))
3753 ;; There's always an identifier before a angle
3754 ;; bracket arglist, or a keyword in
3755 ;; `c-<>-type-kwds' or `c-<>-arglist-kwds'.
3756 (c-backward-syntactic-ws)
3757 (setq id-end
(point))
3758 (c-simple-skip-symbol-backward)
3759 (when (or (setq keyword-match
3760 (looking-at c-opt-
<>-sexp-key
))
3761 (not (looking-at c-keywords-regexp
)))
3762 (setq id-start
(point))))
3765 (let ((c-record-type-identifiers nil
)
3766 (c-record-found-types nil
))
3767 (c-forward-<>-arglist-recur
3770 (c-keyword-sym (match-string 1))
3773 c-disallow-comma-in-
<>-arglists
))))
3776 ;; It was not an angle bracket arglist.
3778 (when (text-property-not-all
3779 (1- pos
) tmp
'syntax-table nil
)
3780 (if (c-parse-sexp-lookup-properties)
3781 ;; Got an invalid open paren syntax on this
3782 ;; '<'. We'll probably get an unbalanced '>'
3783 ;; further ahead if we just remove the syntax
3784 ;; here, so recover by removing all paren
3785 ;; properties up to and including the
3786 ;; balancing close paren.
3787 (parse-partial-sexp pos
(point-max) -
1)
3789 (c-remove-<>-arglist-properties pos
(point)))
3792 ;; It was an angle bracket arglist.
3793 (setq c-record-found-types subres
)
3795 ;; Record the identifier before the template as a type
3796 ;; or reference depending on whether the arglist is last
3797 ;; in a qualified identifier.
3798 (when (and c-record-type-identifiers
3799 (not keyword-match
))
3800 (if (and c-opt-identifier-concat-key
3802 (c-forward-syntactic-ws)
3803 (looking-at c-opt-identifier-concat-key
)))
3804 (c-record-ref-id (cons id-start id-end
))
3805 (c-record-type-id (cons id-start id-end
))))))
3808 ((and (eq (char-before) ?
,)
3809 (not c-disallow-comma-in-
<>-arglists
))
3810 ;; Just another argument. Record the position. The
3811 ;; type check stuff that made us stop at it is at
3812 ;; the top of the loop.
3813 (setq arg-start-pos
(cons (point) arg-start-pos
)))
3816 ;; Got a character that can't be in an angle bracket
3817 ;; arglist argument. Abort using `throw', since
3818 ;; it's useless to try to find a surrounding arglist
3820 (throw 'angle-bracket-arglist-escape nil
))))))
3823 (or c-record-found-types t
)))))
3825 (defun c-forward-name ()
3826 ;; Move forward over a complete name if at the beginning of one,
3827 ;; stopping at the next following token. If the point is not at
3828 ;; something that are recognized as name then it stays put. A name
3829 ;; could be something as simple as "foo" in C or something as
3830 ;; complex as "X<Y<class A<int>::B, BIT_MAX >> b>, ::operator<> ::
3831 ;; Z<(a>b)> :: operator const X<&foo>::T Q::G<unsigned short
3832 ;; int>::*volatile const" in C++ (this function is actually little
3833 ;; more than a `looking-at' call in all modes except those that,
3834 ;; like C++, have `c-recognize-<>-arglists' set). Return nil if no
3835 ;; name is found, 'template if it's an identifier ending with an
3836 ;; angle bracket arglist, 'operator of it's an operator identifier,
3837 ;; or t if it's some other kind of name.
3839 (let ((pos (point)) res id-start id-end
3840 ;; Turn off `c-promote-possible-types' here since we might
3841 ;; call `c-forward-<>-arglist' and we don't want it to promote
3842 ;; every suspect thing in the arglist to a type. We're
3843 ;; typically called from `c-forward-type' in this case, and
3844 ;; the caller only wants the top level type that it finds to
3846 c-promote-possible-types
)
3849 (looking-at c-identifier-key
)
3852 ;; Check for keyword. We go to the last symbol in
3853 ;; `c-identifier-key' first.
3854 (if (eq c-identifier-key c-symbol-key
)
3855 (setq id-start
(point)
3856 id-end
(match-end 0))
3857 (goto-char (setq id-end
(match-end 0)))
3858 (c-simple-skip-symbol-backward)
3859 (setq id-start
(point)))
3861 (if (looking-at c-keywords-regexp
)
3862 (when (and (c-major-mode-is 'c
++-mode
)
3864 (cc-eval-when-compile
3865 (concat "\\(operator\\|\\(template\\)\\)"
3866 "\\(" (c-lang-const c-nonsymbol-key c
++)
3868 (if (match-beginning 2)
3869 ;; "template" is only valid inside an
3870 ;; identifier if preceded by "::".
3872 (c-backward-syntactic-ws)
3873 (and (c-safe (backward-char 2) t
)
3877 ;; Handle a C++ operator or template identifier.
3879 (c-forward-syntactic-ws)
3880 (cond ((eq (char-before id-end
) ?e
)
3881 ;; Got "... ::template".
3882 (let ((subres (c-forward-name)))
3887 ((looking-at c-identifier-start
)
3888 ;; Got a cast operator.
3889 (when (c-forward-type)
3892 ;; Now we should match a sequence of either
3893 ;; '*', '&' or a name followed by ":: *",
3894 ;; where each can be followed by a sequence
3895 ;; of `c-opt-type-modifier-key'.
3896 (while (cond ((looking-at "[*&]")
3897 (goto-char (match-end 0))
3899 ((looking-at c-identifier-start
)
3900 (and (c-forward-name)
3903 (goto-char (match-end 0))
3904 (c-forward-syntactic-ws)
3905 (eq (char-after) ?
*))
3910 (c-forward-syntactic-ws)
3912 (looking-at c-opt-type-modifier-key
))
3913 (goto-char (match-end 1))))))
3915 ((looking-at c-overloadable-operators-regexp
)
3916 ;; Got some other operator.
3917 (when c-record-type-identifiers
3918 (setq c-last-identifier-range
3919 (cons (point) (match-end 0))))
3920 (goto-char (match-end 0))
3921 (c-forward-syntactic-ws)
3927 (when c-record-type-identifiers
3928 (setq c-last-identifier-range
3929 (cons id-start id-end
)))
3931 (c-forward-syntactic-ws)
3937 (when (or c-opt-identifier-concat-key
3938 c-recognize-
<>-arglists
)
3941 ((and c-opt-identifier-concat-key
3942 (looking-at c-opt-identifier-concat-key
))
3943 ;; Got a concatenated identifier. This handles the
3944 ;; cases with tricky syntactic whitespace that aren't
3945 ;; covered in `c-identifier-key'.
3946 (goto-char (match-end 0))
3947 (c-forward-syntactic-ws)
3950 ((and c-recognize-
<>-arglists
3951 (eq (char-after) ?
<))
3952 ;; Maybe an angle bracket arglist.
3953 (when (let ((c-record-type-identifiers nil
)
3954 (c-record-found-types nil
))
3955 (c-forward-<>-arglist
3956 nil c-disallow-comma-in-
<>-arglists
))
3957 (c-forward-syntactic-ws)
3959 (if (and c-opt-identifier-concat-key
3960 (looking-at c-opt-identifier-concat-key
))
3961 ;; Continue if there's an identifier concatenation
3962 ;; operator after the template argument.
3964 (when c-record-type-identifiers
3965 (c-record-ref-id (cons id-start id-end
))
3966 (setq c-last-identifier-range nil
))
3968 (c-forward-syntactic-ws)
3970 ;; `c-add-type' isn't called here since we don't
3971 ;; want to add types containing angle bracket
3973 (when c-record-type-identifiers
3974 (c-record-type-id (cons id-start id-end
))
3975 (setq c-last-identifier-range nil
))
3976 (setq res
'template
)
3983 (defun c-forward-type ()
3984 ;; Move forward over a type spec if at the beginning of one,
3985 ;; stopping at the next following token. Return t if it's a known
3986 ;; type that can't be a name, 'known if it's an otherwise known type
3987 ;; (according to `*-font-lock-extra-types'), 'prefix if it's a known
3988 ;; prefix of a type, 'found if it's a type that matches one in
3989 ;; `c-found-types', 'maybe if it's an identfier that might be a
3990 ;; type, or nil if it can't be a type (the point isn't moved then).
3991 ;; The point is assumed to be at the beginning of a token.
3993 ;; Note that this function doesn't skip past the brace definition
3994 ;; that might be considered part of the type, e.g.
3995 ;; "enum {a, b, c} foo".
3996 (let ((start (point)) pos res res2 id-start id-end id-range
)
3998 ;; Skip leading type modifiers. If any are found we know it's a
3999 ;; prefix of a type.
4000 (when c-opt-type-modifier-key
4001 (while (looking-at c-opt-type-modifier-key
)
4002 (goto-char (match-end 1))
4003 (c-forward-syntactic-ws)
4004 (setq res
'prefix
)))
4007 ((looking-at c-type-prefix-key
)
4008 ;; Looking at a keyword that prefixes a type identifier,
4010 (goto-char (match-end 1))
4011 (c-forward-syntactic-ws)
4013 (if (memq (setq res2
(c-forward-name)) '(t template
))
4016 ;; In many languages the name can be used without the
4017 ;; prefix, so we add it to `c-found-types'.
4018 (c-add-type pos
(point))
4019 (when c-record-type-identifiers
4020 (c-record-type-id c-last-identifier-range
)))
4028 (if (looking-at c-identifier-start
)
4030 (setq id-start
(point)
4031 res2
(c-forward-name))
4033 (setq id-end
(point)
4034 id-range c-last-identifier-range
))))
4035 (and (cond ((looking-at c-primitive-type-key
)
4037 ((c-with-syntax-table c-identifier-syntax-table
4038 (looking-at c-known-type-key
))
4043 (goto-char (match-end 1))
4044 (c-forward-syntactic-ws)
4045 (setq pos
(point))))
4048 ;; Looking at a primitive or known type identifier. We've
4049 ;; checked for a name first so that we don't go here if the
4050 ;; known type match only is a prefix of another name.
4052 (setq id-end
(match-end 1))
4054 (when (and c-record-type-identifiers
4055 (or c-promote-possible-types
(eq res t
)))
4056 (c-record-type-id (cons (match-beginning 1) (match-end 1))))
4058 (if (and c-opt-type-component-key
4060 (looking-at c-opt-type-component-key
)))
4061 ;; There might be more keywords for the type.
4063 (c-forward-keyword-clause)
4065 (setq safe-pos
(point))
4066 (looking-at c-opt-type-component-key
))
4067 (when (and c-record-type-identifiers
4068 (looking-at c-primitive-type-key
))
4069 (c-record-type-id (cons (match-beginning 1)
4071 (c-forward-keyword-clause))
4072 (if (looking-at c-primitive-type-key
)
4074 (when c-record-type-identifiers
4075 (c-record-type-id (cons (match-beginning 1)
4077 (c-forward-keyword-clause)
4079 (goto-char safe-pos
)
4080 (setq res
'prefix
)))
4081 (unless (save-match-data (c-forward-keyword-clause))
4084 (goto-char (match-end 1))
4085 (c-forward-syntactic-ws)))))
4089 ;; A normal identifier.
4091 (if (or res c-promote-possible-types
)
4093 (c-add-type id-start id-end
)
4094 (when c-record-type-identifiers
4095 (c-record-type-id id-range
))
4098 (setq res
(if (c-check-type id-start id-end
)
4099 ;; It's an identifier that has been used as
4100 ;; a type somewhere else.
4102 ;; It's an identifier that might be a type.
4104 ((eq res2
'template
)
4105 ;; A template is a type.
4109 ;; Otherwise it's an operator identifier, which is not a type.
4114 ;; Skip trailing type modifiers. If any are found we know it's
4116 (when c-opt-type-modifier-key
4117 (while (looking-at c-opt-type-modifier-key
)
4118 (goto-char (match-end 1))
4119 (c-forward-syntactic-ws)
4122 ;; Step over any type suffix operator. Do not let the existence
4123 ;; of these alter the classification of the found type, since
4124 ;; these operators typically are allowed in normal expressions
4126 (when c-opt-type-suffix-key
4127 (while (looking-at c-opt-type-suffix-key
)
4128 (goto-char (match-end 1))
4129 (c-forward-syntactic-ws)))
4131 (when c-opt-type-concat-key
4132 ;; Look for a trailing operator that concatenate the type with
4133 ;; a following one, and if so step past that one through a
4136 (let* ((c-promote-possible-types (or (memq res
'(t known
))
4137 c-promote-possible-types
))
4138 ;; If we can't promote then set `c-record-found-types' so that
4139 ;; we can merge in the types from the second part afterwards if
4140 ;; it turns out to be a known type there.
4141 (c-record-found-types (and c-record-type-identifiers
4142 (not c-promote-possible-types
))))
4143 (if (and (looking-at c-opt-type-concat-key
)
4146 (goto-char (match-end 1))
4147 (c-forward-syntactic-ws)
4148 (setq res2
(c-forward-type))))
4151 ;; If either operand certainly is a type then both are, but we
4152 ;; don't let the existence of the operator itself promote two
4153 ;; uncertain types to a certain one.
4155 ((or (eq res
'known
) (memq res2
'(t known
)))
4156 (c-add-type id-start id-end
)
4157 (when c-record-type-identifiers
4158 (c-record-type-id id-range
))
4166 (when (and (eq res t
)
4167 (consp c-record-found-types
))
4168 ;; Merge in the ranges of any types found by the second
4169 ;; `c-forward-type'.
4170 (setq c-record-type-identifiers
4171 ;; `nconc' doesn't mind that the tail of
4172 ;; `c-record-found-types' is t.
4173 (nconc c-record-found-types
4174 c-record-type-identifiers
))))
4178 (when (and c-record-found-types
(memq res
'(known found
)) id-range
)
4179 (setq c-record-found-types
4180 (cons id-range c-record-found-types
))))
4182 ;;(message "c-forward-type %s -> %s: %s" start (point) res)
4187 ;; Handling of large scale constructs like statements and declarations.
4189 (defun c-beginning-of-inheritance-list (&optional lim
)
4190 ;; Go to the first non-whitespace after the colon that starts a
4191 ;; multiple inheritance introduction. Optional LIM is the farthest
4192 ;; back we should search.
4193 (let* ((lim (or lim
(save-excursion
4194 (c-beginning-of-syntax)
4196 (c-with-syntax-table c
++-template-syntax-table
4197 (c-backward-token-2 0 t lim
)
4198 (while (and (or (looking-at c-symbol-start
)
4199 (looking-at "[<,]"))
4200 (zerop (c-backward-token-2 1 t lim
))))
4201 (skip-chars-forward "^:"))))
4203 (defun c-in-method-def-p ()
4204 ;; Return nil if we aren't in a method definition, otherwise the
4205 ;; position of the initial [+-].
4208 (and c-opt-method-key
4209 (looking-at c-opt-method-key
)
4213 ;; Contributed by Kevin Ryde <user42@zip.com.au>.
4214 (defun c-in-gcc-asm-p ()
4215 ;; Return non-nil if point is within a gcc \"asm\" block.
4217 ;; This should be called with point inside an argument list.
4219 ;; Only one level of enclosing parentheses is considered, so for
4220 ;; instance `nil' is returned when in a function call within an asm
4223 (and c-opt-asm-stmt-key
4226 (backward-up-list 1)
4227 (c-beginning-of-statement-1 (point-min) nil t
)
4228 (looking-at c-opt-asm-stmt-key
))))
4230 (defun c-at-toplevel-p ()
4231 "Return a determination as to whether point is at the `top-level'.
4232 Being at the top-level means that point is either outside any
4233 enclosing block (such function definition), or only inside a class,
4234 namespace or other block that contains another declaration level.
4236 If point is not at the top-level (e.g. it is inside a method
4237 definition), then nil is returned. Otherwise, if point is at a
4238 top-level not enclosed within a class definition, t is returned.
4239 Otherwise, a 2-vector is returned where the zeroth element is the
4240 buffer position of the start of the class declaration, and the first
4241 element is the buffer position of the enclosing class's opening
4243 (let ((paren-state (c-parse-state)))
4244 (or (not (c-most-enclosing-brace paren-state
))
4245 (c-search-uplist-for-classkey paren-state
))))
4247 (defun c-just-after-func-arglist-p (&optional lim
)
4248 ;; Return t if we are between a function's argument list closing
4249 ;; paren and its opening brace. Note that the list close brace
4250 ;; could be followed by a "const" specifier or a member init hanging
4251 ;; colon. LIM is used as bound for some backward buffer searches;
4252 ;; the search might continue past it.
4254 ;; Note: This test is easily fooled. It only works reasonably well
4255 ;; in the situations where `c-guess-basic-syntax' uses it.
4257 (if (c-mode-is-new-awk-p)
4258 (c-awk-backward-syntactic-ws lim
)
4259 (c-backward-syntactic-ws lim
))
4260 (let ((checkpoint (point)))
4261 ;; could be looking at const specifier
4262 (if (and (eq (char-before) ?t
)
4264 (looking-at "\\<const\\>[^_]"))
4265 (c-backward-syntactic-ws lim
)
4266 ;; otherwise, we could be looking at a hanging member init
4268 (goto-char checkpoint
)
4269 (while (eq (char-before) ?
,)
4270 ;; this will catch member inits with multiple
4273 (c-backward-syntactic-ws (c-point 'bol
))
4274 (if (eq (char-before) ?\
))
4276 (c-backward-sexp 1))
4277 (c-backward-syntactic-ws lim
))
4278 (if (and (eq (char-before) ?
:)
4281 (c-backward-syntactic-ws lim
)
4282 (looking-at "\\([ \t\n]\\|\\\\\n\\)*:\\([^:]+\\|$\\)")))
4284 (goto-char checkpoint
))
4286 (setq checkpoint
(point))
4287 (and (eq (char-before) ?\
))
4288 ;; Check that it isn't a cpp expression, e.g. the
4289 ;; expression of an #if directive or the "function header"
4291 (or (not (c-beginning-of-macro))
4292 (and (c-forward-to-cpp-define-body)
4293 (< (point) checkpoint
)))
4294 ;; check if we are looking at an ObjC method def
4295 (or (not c-opt-method-key
)
4297 (goto-char checkpoint
)
4300 (c-backward-syntactic-ws lim
)
4301 (not (or (memq (char-before) '(?- ?
+))
4302 ;; or a class category
4305 (looking-at c-class-key
))
4309 (defun c-in-knr-argdecl (&optional lim
)
4310 ;; Return the position of the first argument declaration if point is
4311 ;; inside a K&R style argument declaration list, nil otherwise.
4312 ;; `c-recognize-knr-p' is not checked. If LIM is non-nil, it's a
4313 ;; position that bounds the backward search for the argument list.
4315 ;; Note: A declaration level context is assumed; the test can return
4316 ;; false positives for statements. This test is even more easily
4317 ;; fooled than `c-just-after-func-arglist-p'.
4322 ;; Go back to the closest preceding normal parenthesis sexp. We
4323 ;; take that as the argument list in the function header. Then
4324 ;; check that it's followed by some symbol before the next ';'
4325 ;; or '{'. If it does, it's the header of the K&R argdecl we're
4327 (if lim
(narrow-to-region lim
(point)))
4328 (let ((outside-macro (not (c-query-macro-start)))
4332 (while (if (and (c-safe (setq paren-end
4333 (c-down-list-backward (point))))
4334 (eq (char-after paren-end
) ?\
)))
4336 (goto-char (1+ paren-end
))
4338 (c-beginning-of-macro)))
4339 (throw 'done nil
))))
4342 (c-forward-syntactic-ws)
4343 (looking-at "\\w\\|\\s_"))
4344 (c-safe (c-up-list-backward paren-end
))
4347 ;; If it's a K&R declaration then we're now at the
4348 ;; beginning of the function arglist. Check that there
4349 ;; isn't a '=' before it in this statement since that
4350 ;; means it some kind of initialization instead.
4351 (c-syntactic-skip-backward "^;=}{")
4352 (not (eq (char-before) ?
=)))
4356 (defun c-skip-conditional ()
4357 ;; skip forward over conditional at point, including any predicate
4358 ;; statements in parentheses. No error checking is performed.
4359 (c-forward-sexp (cond
4361 ((looking-at (concat "\\<else"
4362 "\\([ \t\n]\\|\\\\\n\\)+"
4363 "if\\>\\([^_]\\|$\\)"))
4365 ;; do, else, try, finally
4366 ((looking-at (concat "\\<\\("
4367 "do\\|else\\|try\\|finally"
4368 "\\)\\>\\([^_]\\|$\\)"))
4370 ;; for, if, while, switch, catch, synchronized, foreach
4373 (defun c-after-conditional (&optional lim
)
4374 ;; If looking at the token after a conditional then return the
4375 ;; position of its start, otherwise return nil.
4377 (and (zerop (c-backward-token-2 1 t lim
))
4378 (or (looking-at c-block-stmt-1-key
)
4379 (and (eq (char-after) ?\
()
4380 (zerop (c-backward-token-2 1 t lim
))
4381 (looking-at c-block-stmt-2-key
)))
4384 (defsubst c-backward-to-block-anchor
(&optional lim
)
4385 ;; Assuming point is at a brace that opens a statement block of some
4386 ;; kind, move to the proper anchor point for that block. It might
4387 ;; need to be adjusted further by c-add-stmt-syntax, but the
4388 ;; position at return is suitable as start position for that
4390 (unless (= (point) (c-point 'boi
))
4391 (let ((start (c-after-conditional lim
)))
4393 (goto-char start
)))))
4395 (defun c-backward-to-decl-anchor (&optional lim
)
4396 ;; Assuming point is at a brace that opens the block of a top level
4397 ;; declaration of some kind, move to the proper anchor point for
4399 (unless (= (point) (c-point 'boi
))
4400 ;; What we have below is actually an extremely stripped variant of
4401 ;; c-beginning-of-statement-1.
4402 (let ((pos (point)))
4403 ;; Switch syntax table to avoid stopping at line continuations.
4405 (if lim
(narrow-to-region lim
(point-max)))
4407 (c-backward-syntactic-ws)
4408 (c-safe (goto-char (scan-sexps (point) -
1)) t
))
4409 (not (c-crosses-statement-barrier-p (point) pos
)))
4413 (defsubst c-search-decl-header-end
()
4414 ;; Search forward for the end of the "header" of the current
4415 ;; declaration. That's the position where the definition body
4416 ;; starts, or the first variable initializer, or the ending
4417 ;; semicolon. I.e. search forward for the closest following
4418 ;; (syntactically relevant) '{', '=' or ';' token. Point is left
4419 ;; _after_ the first found token, or at point-max if none is found.
4420 (if (c-major-mode-is 'c
++-mode
)
4421 ;; In C++ we need to take special care to handle those pesky
4422 ;; template brackets.
4423 (while (and (c-syntactic-re-search-forward "[;{=<]" nil
'move t
)
4424 (when (eq (char-before) ?
<)
4425 (c-with-syntax-table c
++-template-syntax-table
4426 (if (c-safe (goto-char (c-up-list-forward (point))))
4428 (goto-char (point-max))
4430 (c-syntactic-re-search-forward "[;{=]" nil
'move t t
)))
4432 (defun c-beginning-of-decl-1 (&optional lim
)
4433 ;; Go to the beginning of the current declaration, or the beginning
4434 ;; of the previous one if already at the start of it. Point won't
4435 ;; be moved out of any surrounding paren. Return a cons cell on the
4436 ;; form (MOVE . KNR-POS). MOVE is like the return value from
4437 ;; `c-beginning-of-statement-1'. If point skipped over some K&R
4438 ;; style argument declarations (and they are to be recognized) then
4439 ;; KNR-POS is set to the start of the first such argument
4440 ;; declaration, otherwise KNR-POS is nil. If LIM is non-nil, it's a
4441 ;; position that bounds the backward search.
4443 ;; NB: Cases where the declaration continues after the block, as in
4444 ;; "struct foo { ... } bar;", are currently recognized as two
4445 ;; declarations, e.g. "struct foo { ... }" and "bar;" in this case.
4447 (let* ((start (point))
4448 (last-stmt-start (point))
4449 (move (c-beginning-of-statement-1 lim t t
)))
4451 ;; `c-beginning-of-statement-1' stops at a block start, but we
4452 ;; want to continue if the block doesn't begin a top level
4453 ;; construct, i.e. if it isn't preceded by ';', '}', ':', or bob.
4454 (let ((beg (point)) tentative-move
)
4456 ;; Must check with c-opt-method-key in ObjC mode.
4457 (not (and c-opt-method-key
4458 (looking-at c-opt-method-key
)))
4459 (/= last-stmt-start
(point))
4461 (c-backward-syntactic-ws lim
)
4462 (not (memq (char-before) '(?\
; ?} ?: nil))))
4463 ;; Check that we don't move from the first thing in a
4464 ;; macro to its header.
4465 (not (eq (setq tentative-move
4466 (c-beginning-of-statement-1 lim t t
))
4468 (setq last-stmt-start beg
4470 move tentative-move
))
4473 (when c-recognize-knr-p
4474 (let ((fallback-pos (point)) knr-argdecl-start
)
4475 ;; Handle K&R argdecls. Back up after the "statement" jumped
4476 ;; over by `c-beginning-of-statement-1', unless it was the
4477 ;; function body, in which case we're sitting on the opening
4478 ;; brace now. Then test if we're in a K&R argdecl region and
4479 ;; that we started at the other side of the first argdecl in
4481 (unless (eq (char-after) ?
{)
4482 (goto-char last-stmt-start
))
4483 (if (and (setq knr-argdecl-start
(c-in-knr-argdecl lim
))
4484 (< knr-argdecl-start start
)
4486 (goto-char knr-argdecl-start
)
4487 (not (eq (c-beginning-of-statement-1 lim t t
) 'macro
))))
4489 (cons (if (eq (char-after fallback-pos
) ?
{)
4493 (goto-char fallback-pos
))))
4495 (when c-opt-access-key
4496 ;; Might have ended up before a protection label. This should
4497 ;; perhaps be checked before `c-recognize-knr-p' to be really
4498 ;; accurate, but we know that no language has both.
4499 (while (looking-at c-opt-access-key
)
4500 (goto-char (match-end 0))
4501 (c-forward-syntactic-ws)
4502 (when (>= (point) start
)
4504 (throw 'return
(cons 'same nil
)))))
4506 ;; `c-beginning-of-statement-1' counts each brace block as a
4507 ;; separate statement, so the result will be 'previous if we've
4508 ;; moved over any. If they were brace list initializers we might
4509 ;; not have moved over a declaration boundary though, so change it
4510 ;; to 'same if we've moved past a '=' before '{', but not ';'.
4511 ;; (This ought to be integrated into `c-beginning-of-statement-1',
4512 ;; so we avoid this extra pass which potentially can search over a
4513 ;; large amount of text.)
4514 (if (and (eq move
'previous
)
4515 (c-with-syntax-table (if (c-major-mode-is 'c
++-mode
)
4516 c
++-template-syntax-table
4519 (and (c-syntactic-re-search-forward "[;={]" start t t t
)
4520 (eq (char-before) ?
=)
4521 (c-syntactic-re-search-forward "[;{]" start t t
)
4522 (eq (char-before) ?
{)
4523 (c-safe (goto-char (c-up-list-forward (point))) t
)
4524 (not (c-syntactic-re-search-forward ";" start t t
))))))
4528 (defun c-end-of-decl-1 ()
4529 ;; Assuming point is at the start of a declaration (as detected by
4530 ;; e.g. `c-beginning-of-decl-1'), go to the end of it. Unlike
4531 ;; `c-beginning-of-decl-1', this function handles the case when a
4532 ;; block is followed by identifiers in e.g. struct declarations in C
4533 ;; or C++. If a proper end was found then t is returned, otherwise
4534 ;; point is moved as far as possible within the current sexp and nil
4535 ;; is returned. This function doesn't handle macros; use
4536 ;; `c-end-of-macro' instead in those cases.
4537 (let ((start (point))
4538 (decl-syntax-table (if (c-major-mode-is 'c
++-mode
)
4539 c
++-template-syntax-table
4542 (c-search-decl-header-end)
4544 (when (and c-recognize-knr-p
4545 (eq (char-before) ?\
;)
4546 (c-in-knr-argdecl start
))
4547 ;; Stopped at the ';' in a K&R argdecl section which is
4548 ;; detected using the same criteria as in
4549 ;; `c-beginning-of-decl-1'. Move to the following block
4551 (c-syntactic-re-search-forward "{" nil
'move t
))
4553 (when (eq (char-before) ?
{)
4554 ;; Encountered a block in the declaration. Jump over it.
4556 (goto-char (c-up-list-forward (point)))
4557 (error (goto-char (point-max))
4558 (throw 'return nil
)))
4559 (if (or (not c-opt-block-decls-with-vars-key
)
4561 (c-with-syntax-table decl-syntax-table
4562 (let ((lim (point)))
4565 ;; Check for `c-opt-block-decls-with-vars-key'
4566 ;; before the first paren.
4567 (c-syntactic-re-search-forward
4568 (concat "[;=\(\[{]\\|\\("
4569 c-opt-block-decls-with-vars-key
4573 (not (eq (char-before) ?_
))
4574 ;; Check that the first following paren is
4576 (c-syntactic-re-search-forward "[;=\(\[{]"
4578 (eq (char-before) ?
{)))))))
4579 ;; The declaration doesn't have any of the
4580 ;; `c-opt-block-decls-with-vars' keywords in the
4581 ;; beginning, so it ends here at the end of the block.
4584 (c-with-syntax-table decl-syntax-table
4586 (if (eq (char-before) ?\
;)
4588 (c-syntactic-re-search-forward ";" nil
'move t
))))
4591 (defun c-beginning-of-member-init-list (&optional limit
)
4592 ;; Goes to the beginning of a member init list (i.e. just after the
4593 ;; ':') if inside one. Returns t in that case, nil otherwise.
4595 (setq limit
(point-min)))
4596 (skip-chars-forward " \t")
4598 (if (eq (char-after) ?
,)
4600 (c-backward-syntactic-ws limit
))
4603 (while (and (< limit
(point))
4604 (eq (char-before) ?
,))
4606 ;; this will catch member inits with multiple
4609 (c-backward-syntactic-ws limit
)
4610 (if (eq (char-before) ?\
))
4611 (unless (c-safe (c-backward-sexp 1))
4613 (c-backward-syntactic-ws limit
)
4615 ;; Skip over any template arg to the class. This way with a
4616 ;; syntax table is bogus but it'll have to do for now.
4617 (if (and (eq (char-before) ?
>)
4618 (c-major-mode-is 'c
++-mode
))
4619 (c-with-syntax-table c
++-template-syntax-table
4620 (unless (c-safe (c-backward-sexp 1))
4621 (throw 'exit nil
))))
4622 (c-safe (c-backward-sexp 1))
4623 (c-backward-syntactic-ws limit
)
4625 ;; Skip backwards over a fully::qualified::name.
4626 (while (and (eq (char-before) ?
:)
4629 (eq (char-before) ?
:)))
4631 (c-safe (c-backward-sexp 1)))
4633 ;; If we've stepped over a number then this is a bitfield.
4634 (when (and c-opt-bitfield-key
4635 (looking-at "[0-9]"))
4638 ;; now continue checking
4639 (c-backward-syntactic-ws limit
))
4641 (and (< limit
(point))
4642 (eq (char-before) ?
:))))
4644 (defun c-search-uplist-for-classkey (paren-state)
4645 ;; search for the containing class, returning a 2 element vector if
4646 ;; found. aref 0 contains the bufpos of the boi of the class key
4647 ;; line, and aref 1 contains the bufpos of the open brace.
4648 (if (null paren-state
)
4649 ;; no paren-state means we cannot be inside a class
4651 (let ((carcache (car paren-state
))
4652 search-start search-end
)
4653 (if (consp carcache
)
4654 ;; a cons cell in the first element means that there is some
4655 ;; balanced sexp before the current bufpos. this we can
4656 ;; ignore. the nth 1 and nth 2 elements define for us the
4657 ;; search boundaries
4658 (setq search-start
(nth 2 paren-state
)
4659 search-end
(nth 1 paren-state
))
4660 ;; if the car was not a cons cell then nth 0 and nth 1 define
4661 ;; for us the search boundaries
4662 (setq search-start
(nth 1 paren-state
)
4663 search-end
(nth 0 paren-state
)))
4664 ;; if search-end is nil, or if the search-end character isn't an
4665 ;; open brace, we are definitely not in a class
4666 (if (or (not search-end
)
4667 (< search-end
(point-min))
4668 (not (eq (char-after search-end
) ?
{)))
4670 ;; now, we need to look more closely at search-start. if
4671 ;; search-start is nil, then our start boundary is really
4673 (if (not search-start
)
4674 (setq search-start
(point-min))
4675 ;; if search-start is a cons cell, then we can start
4676 ;; searching from the end of the balanced sexp just ahead of
4678 (if (consp search-start
)
4679 (setq search-start
(cdr search-start
))
4680 ;; Otherwise we start searching within the surrounding paren sexp.
4681 (setq search-start
(1+ search-start
))))
4682 ;; now we can do a quick regexp search from search-start to
4683 ;; search-end and see if we can find a class key. watch for
4684 ;; class like strings in literals
4687 (goto-char search-start
)
4688 (let (foundp class match-end
)
4689 (while (and (not foundp
)
4691 (c-forward-syntactic-ws search-end
)
4692 (> search-end
(point)))
4693 ;; Add one to the search limit, to allow
4694 ;; matching of the "{" in the regexp.
4695 (re-search-forward c-decl-block-key
4698 (setq class
(match-beginning 0)
4699 match-end
(match-end 0))
4701 (if (c-in-literal search-start
)
4702 (goto-char match-end
) ; its in a comment or string, ignore
4704 (setq foundp
(vector (c-point 'boi
) search-end
))
4706 ;; check for embedded keywords
4707 ((let ((char (char-after (1- class
))))
4709 (memq (char-syntax char
) '(?w ?_
))))
4710 (goto-char match-end
)
4712 ;; make sure we're really looking at the start of a
4713 ;; class definition, and not an ObjC method.
4714 ((and c-opt-method-key
4715 (re-search-forward c-opt-method-key search-end t
)
4716 (not (c-in-literal class
)))
4718 ;; Check if this is an anonymous inner class.
4719 ((and c-opt-inexpr-class-key
4720 (looking-at c-opt-inexpr-class-key
))
4721 (while (and (zerop (c-forward-token-2 1 t
))
4722 (looking-at "(\\|\\w\\|\\s_\\|\\.")))
4723 (if (eq (point) search-end
)
4724 ;; We're done. Just trap this case in the cond.
4726 ;; False alarm; all conditions aren't satisfied.
4728 ;; Its impossible to define a regexp for this, and
4729 ;; nearly so to do it programmatically.
4731 ;; ; picks up forward decls
4732 ;; = picks up init lists
4733 ;; ) picks up return types
4734 ;; > picks up templates, but remember that we can
4735 ;; inherit from templates!
4736 ((let ((skipchars "^;=)"))
4737 ;; try to see if we found the `class' keyword
4738 ;; inside a template arg list
4740 (skip-chars-backward "^<>" search-start
)
4741 (if (eq (char-before) ?
<)
4742 (setq skipchars
(concat skipchars
">"))))
4744 (skip-chars-forward skipchars search-end
)
4745 (c-in-literal class
))
4747 (/= (point) search-end
))
4753 (defun c-inside-bracelist-p (containing-sexp paren-state
)
4754 ;; return the buffer position of the beginning of the brace list
4755 ;; statement if we're inside a brace list, otherwise return nil.
4756 ;; CONTAINING-SEXP is the buffer pos of the innermost containing
4757 ;; paren. BRACE-STATE is the remainder of the state of enclosing
4760 ;; N.B.: This algorithm can potentially get confused by cpp macros
4761 ;; places in inconvenient locations. Its a trade-off we make for
4764 ;; This will pick up brace list declarations.
4767 (goto-char containing-sexp
)
4770 (if (and (or (looking-at c-brace-list-key
)
4771 (progn (c-forward-sexp -
1)
4772 (looking-at c-brace-list-key
)))
4773 (setq bracepos
(c-down-list-forward (point)))
4774 (not (c-crosses-statement-barrier-p (point)
4777 ;; this will pick up array/aggregate init lists, even if they are nested.
4780 ;; Pike can have class definitions anywhere, so we must
4781 ;; check for the class key here.
4782 (and (c-major-mode-is 'pike-mode
)
4784 bufpos braceassignp lim next-containing
)
4785 (while (and (not bufpos
)
4788 (if (consp (car paren-state
))
4789 (setq lim
(cdr (car paren-state
))
4790 paren-state
(cdr paren-state
))
4791 (setq lim
(car paren-state
)))
4793 (setq next-containing
(car paren-state
)
4794 paren-state
(cdr paren-state
))))
4795 (goto-char containing-sexp
)
4796 (if (c-looking-at-inexpr-block next-containing next-containing
)
4797 ;; We're in an in-expression block of some kind. Do not
4798 ;; check nesting. We deliberately set the limit to the
4799 ;; containing sexp, so that c-looking-at-inexpr-block
4800 ;; doesn't check for an identifier before it.
4801 (setq containing-sexp nil
)
4802 ;; see if the open brace is preceded by = or [...] in
4803 ;; this statement, but watch out for operator=
4804 (setq braceassignp
'dontknow
)
4805 (c-backward-token-2 1 t lim
)
4806 ;; Checks to do only on the first sexp before the brace.
4807 (when (and c-opt-inexpr-brace-list-key
4808 (eq (char-after) ?\
[))
4809 ;; In Java, an initialization brace list may follow
4810 ;; directly after "new Foo[]", so check for a "new"
4812 (while (eq braceassignp
'dontknow
)
4814 (cond ((/= (c-backward-token-2 1 t lim
) 0) nil
)
4815 ((looking-at c-opt-inexpr-brace-list-key
) t
)
4816 ((looking-at "\\sw\\|\\s_\\|[.[]")
4817 ;; Carry on looking if this is an
4818 ;; identifier (may contain "." in Java)
4819 ;; or another "[]" sexp.
4822 ;; Checks to do on all sexps before the brace, up to the
4823 ;; beginning of the statement.
4824 (while (eq braceassignp
'dontknow
)
4825 (cond ((eq (char-after) ?\
;)
4826 (setq braceassignp nil
))
4828 (looking-at class-key
))
4829 (setq braceassignp nil
))
4830 ((eq (char-after) ?
=)
4831 ;; We've seen a =, but must check earlier tokens so
4832 ;; that it isn't something that should be ignored.
4833 (setq braceassignp
'maybe
)
4834 (while (and (eq braceassignp
'maybe
)
4835 (zerop (c-backward-token-2 1 t lim
)))
4838 ;; Check for operator =
4839 ((looking-at "operator\\>[^_]") nil
)
4840 ;; Check for `<opchar>= in Pike.
4841 ((and (c-major-mode-is 'pike-mode
)
4842 (or (eq (char-after) ?
`)
4843 ;; Special case for Pikes
4844 ;; `[]=, since '[' is not in
4845 ;; the punctuation class.
4846 (and (eq (char-after) ?\
[)
4847 (eq (char-before) ?
`))))
4849 ((looking-at "\\s.") 'maybe
)
4850 ;; make sure we're not in a C++ template
4851 ;; argument assignment
4853 (c-major-mode-is 'c
++-mode
)
4855 (let ((here (point))
4857 (skip-chars-backward "^<>")
4859 (and (eq (char-before) ?
<)
4860 (not (c-crosses-statement-barrier-p
4862 (not (c-in-literal))
4866 (if (and (eq braceassignp
'dontknow
)
4867 (/= (c-backward-token-2 1 t lim
) 0))
4868 (setq braceassignp nil
)))
4869 (if (not braceassignp
)
4870 (if (eq (char-after) ?\
;)
4871 ;; Brace lists can't contain a semicolon, so we're done.
4872 (setq containing-sexp nil
)
4874 (setq containing-sexp next-containing
4876 next-containing nil
))
4877 ;; we've hit the beginning of the aggregate list
4878 (c-beginning-of-statement-1
4879 (c-most-enclosing-brace paren-state
))
4880 (setq bufpos
(point))))
4885 (defun c-looking-at-special-brace-list (&optional lim
)
4886 ;; If we're looking at the start of a pike-style list, ie `({Â })',
4887 ;; `([Â ])', `(<Â >)' etc, a cons of a cons of its starting and ending
4888 ;; positions and its entry in c-special-brace-lists is returned, nil
4889 ;; otherwise. The ending position is nil if the list is still open.
4890 ;; LIM is the limit for forward search. The point may either be at
4891 ;; the `(' or at the following paren character. Tries to check the
4892 ;; matching closer, but assumes it's correct if no balanced paren is
4893 ;; found (i.e. the case `({ ... } ... )' is detected as _not_ being
4894 ;; a special brace list).
4895 (if c-special-brace-lists
4900 (c-forward-syntactic-ws)
4901 (if (eq (char-after) ?\
()
4904 (c-forward-syntactic-ws)
4905 (setq type
(assq (char-after) c-special-brace-lists
)))
4906 (if (setq type
(assq (char-after) c-special-brace-lists
))
4908 (c-backward-syntactic-ws)
4910 (setq beg
(if (eq (char-after) ?\
()
4914 (if (and (c-safe (goto-char beg
)
4917 (= (char-before) ?\
)))
4918 (c-safe (goto-char beg
)
4921 ;; Kludges needed to handle inner
4922 ;; chars both with and without
4924 (or (/= (char-syntax (char-before)) ?\
))
4925 (= (char-before) (cdr type
)))))
4926 (if (or (/= (char-syntax (char-before)) ?\
))
4928 (c-forward-syntactic-ws)
4931 (cons (cons beg end
) type
))
4932 (cons (list beg
) type
)))))
4935 (defun c-looking-at-bos (&optional lim
)
4936 ;; Return non-nil if between two statements or declarations, assuming
4937 ;; point is not inside a literal or comment.
4939 (c-backward-syntactic-ws lim
)
4941 ;; Return t if at the start inside some parenthesis expression
4942 ;; too, to catch macros that have statements as arguments.
4943 (memq (char-before) '(?\
; ?} ?\())
4944 (and (eq (char-before) ?
{)
4945 (not (and c-special-brace-lists
4946 (progn (backward-char)
4947 (c-looking-at-special-brace-list))))))))
4949 (defun c-looking-at-inexpr-block (lim containing-sexp
)
4950 ;; Returns non-nil if we're looking at the beginning of a block
4951 ;; inside an expression. The value returned is actually a cons of
4952 ;; either 'inlambda, 'inexpr-statement or 'inexpr-class and the
4953 ;; position of the beginning of the construct. LIM limits the
4954 ;; backward search. CONTAINING-SEXP is the start position of the
4955 ;; closest containing list. If it's nil, the containing paren isn't
4956 ;; used to decide whether we're inside an expression or not. If
4957 ;; both LIM and CONTAINING-SEXP is used, LIM needs to be farther
4960 (let ((res 'maybe
) passed-bracket
4961 (closest-lim (or containing-sexp lim
(point-min)))
4962 ;; Look at the character after point only as a last resort
4963 ;; when we can't disambiguate.
4964 (block-follows (and (eq (char-after) ?
{) (point))))
4965 (while (and (eq res
'maybe
)
4966 (progn (c-backward-syntactic-ws)
4967 (> (point) closest-lim
))
4969 (progn (backward-char)
4970 (looking-at "[\]\).]\\|\\w\\|\\s_"))
4971 (progn (forward-char)
4972 (goto-char (scan-sexps (point) -
1))))
4976 c-opt-inexpr-class-key
4977 (looking-at c-opt-inexpr-class-key
))
4978 (and (not passed-bracket
)
4979 (or (not (looking-at c-class-key
))
4980 ;; If the class definition is at the start of
4981 ;; a statement, we don't consider it an
4982 ;; in-expression class.
4983 (let ((prev (point)))
4985 (= (c-backward-token-2 1 nil closest-lim
) 0)
4986 (eq (char-syntax (char-after)) ?w
))
4987 (setq prev
(point)))
4989 (not (c-looking-at-bos)))
4990 ;; Also, in Pike we treat it as an
4991 ;; in-expression class if it's used in an
4992 ;; object clone expression.
4994 (and (c-major-mode-is 'pike-mode
)
4995 (progn (goto-char block-follows
)
4996 (zerop (c-forward-token-2 1 t
)))
4997 (eq (char-after) ?\
())))
4998 (cons 'inexpr-class
(point))))
4999 ((and c-opt-inexpr-block-key
5000 (looking-at c-opt-inexpr-block-key
))
5001 (cons 'inexpr-statement
(point)))
5002 ((and c-opt-lambda-key
5003 (looking-at c-opt-lambda-key
))
5004 (cons 'inlambda
(point)))
5005 ((and c-opt-block-stmt-key
5006 (looking-at c-opt-block-stmt-key
))
5009 (if (eq (char-after) ?\
[)
5010 (setq passed-bracket t
))
5013 (when (and block-follows
5015 (eq (char-after containing-sexp
) ?\
())
5016 (goto-char containing-sexp
)
5017 (if (or (save-excursion
5018 (c-backward-syntactic-ws lim
)
5019 (and (> (point) (or lim
(point-min)))
5021 (and c-special-brace-lists
5022 (c-looking-at-special-brace-list)))
5024 (cons 'inexpr-statement
(point))))
5027 (defun c-looking-at-inexpr-block-backward (paren-state)
5028 ;; Returns non-nil if we're looking at the end of an in-expression
5029 ;; block, otherwise the same as `c-looking-at-inexpr-block'.
5030 ;; PAREN-STATE is the paren state relevant at the current position.
5032 ;; We currently only recognize a block.
5033 (let ((here (point))
5034 (elem (car-safe paren-state
))
5036 (when (and (consp elem
)
5037 (progn (goto-char (cdr elem
))
5038 (c-forward-syntactic-ws here
)
5040 (goto-char (car elem
))
5041 (if (setq paren-state
(cdr paren-state
))
5042 (setq containing-sexp
(car-safe paren-state
)))
5043 (c-looking-at-inexpr-block (c-safe-position containing-sexp
5045 containing-sexp
)))))
5047 (defun c-narrow-out-enclosing-class (paren-state lim
)
5048 ;; Narrow the buffer so that the enclosing class is hidden. Uses
5049 ;; and returns the value from c-search-uplist-for-classkey.
5050 (setq paren-state
(c-whack-state-after (point) paren-state
))
5053 (setq inclass-p
(c-search-uplist-for-classkey paren-state
))
5056 (goto-char (1+ (aref inclass-p
1)))
5057 (c-skip-ws-forward lim
)
5058 ;; if point is now left of the class opening brace, we're
5059 ;; hosed, so try a different tact
5060 (if (<= (point) (aref inclass-p
1))
5062 (goto-char (1+ (aref inclass-p
1)))
5063 (c-forward-syntactic-ws lim
)))
5065 ;; end point is the end of the current line
5069 ;; return the class vector
5073 ;; `c-guess-basic-syntax' and the functions that precedes it below
5074 ;; implements the main decision tree for determining the syntactic
5075 ;; analysis of the current line of code.
5077 ;; Dynamically bound to t when `c-guess-basic-syntax' is called during
5078 ;; auto newline analysis.
5079 (defvar c-auto-newline-analysis nil
)
5081 (defsubst c-add-syntax
(symbol &rest args
)
5082 ;; A simple function to prepend a new syntax element to
5083 ;; `c-syntactic-context'. Using `setq' on it is unsafe since it
5084 ;; should always be dynamically bound but since we read it first
5085 ;; we'll fail properly anyway if this function is misused.
5086 (setq c-syntactic-context
(cons (cons symbol args
)
5087 c-syntactic-context
)))
5089 (defsubst c-append-syntax
(symbol &rest args
)
5090 ;; Like `c-add-syntax' but appends to the end of the syntax list.
5091 ;; (Normally not necessary.)
5092 (setq c-syntactic-context
(nconc c-syntactic-context
5093 (list (cons symbol args
)))))
5095 (defun c-add-stmt-syntax (syntax-symbol
5101 ;; Do the generic processing to anchor the given syntax symbol on
5102 ;; the preceding statement: Skip over any labels and containing
5103 ;; statements on the same line, and then search backward until we
5104 ;; find a statement or block start that begins at boi without a
5105 ;; label or comment.
5107 ;; Point is assumed to be at the prospective anchor point for the
5108 ;; given SYNTAX-SYMBOL. More syntax entries are added if we need to
5109 ;; skip past open parens and containing statements. All the added
5110 ;; syntax elements will get the same anchor point.
5112 ;; SYNTAX-EXTRA-ARGS are a list of the extra arguments for the
5113 ;; syntax symbol. They are appended after the anchor point.
5115 ;; If STOP-AT-BOI-ONLY is nil, we might stop in the middle of the
5116 ;; line if another statement precedes the current one on this line.
5118 ;; If AT-BLOCK-START is non-nil, point is taken to be at the
5119 ;; beginning of a block or brace list, which then might be nested
5120 ;; inside an expression. If AT-BLOCK-START is nil, this is found
5121 ;; out by checking whether the character at point is "{" or not.
5122 (if (= (point) (c-point 'boi
))
5123 ;; This is by far the most common case, so let's give it special
5125 (apply 'c-add-syntax syntax-symbol
(point) syntax-extra-args
)
5127 (let ((savepos (point))
5128 (syntax-last c-syntactic-context
)
5129 (boi (c-point 'boi
))
5130 (prev-paren (if at-block-start ?
{ (char-after)))
5131 step-type step-tmp at-comment special-list
)
5132 (apply 'c-add-syntax syntax-symbol nil syntax-extra-args
)
5134 ;; Begin by skipping any labels and containing statements that
5135 ;; are on the same line.
5136 (while (and (/= (point) boi
)
5137 (if (memq (setq step-tmp
5138 (c-beginning-of-statement-1 boi nil t
))
5143 (/= (point) savepos
))
5144 (setq savepos
(point)
5145 step-type step-tmp
))
5148 ;; Loop if we have to back out of the containing block.
5152 ;; Loop if we have to back up another statement.
5156 ;; Always start by skipping over any comments that
5157 ;; stands between the statement and boi.
5158 (while (and (/= (setq savepos
(point)) boi
)
5159 (c-backward-single-comment))
5161 boi
(c-point 'boi
)))
5166 (eq step-type
'label
)
5170 ;; Current position might not be good enough;
5171 ;; skip backward another statement.
5172 (setq step-type
(c-beginning-of-statement-1
5175 (if (and (not stop-at-boi-only
)
5177 (memq step-type
'(up previous
)))
5178 ;; If stop-at-boi-only is nil, we shouldn't
5179 ;; back up over previous or containing
5180 ;; statements to try to reach boi, so go
5181 ;; back to the last position and exit.
5185 (if (and (not stop-at-boi-only
)
5186 (memq step-type
'(up previous beginning
)))
5187 ;; If we've moved into another statement
5188 ;; then we should no longer try to stop
5190 (setq stop-at-boi-only t
))
5192 ;; Record this a substatement if we skipped up
5193 ;; one level, but not if we're still on the
5194 ;; same line. This so e.g. a sequence of "else
5195 ;; if" clauses won't indent deeper and deeper.
5196 (when (and (eq step-type
'up
)
5198 (c-add-syntax 'substatement nil
))
5200 (setq boi
(c-point 'boi
))
5201 (/= (point) savepos
)))))
5203 (setq savepos
(point)
5205 (setq at-comment nil
)
5207 (when (and (eq step-type
'same
)
5209 (goto-char containing-sexp
)
5211 ;; Don't stop in the middle of a special brace list opener
5213 (when (and c-special-brace-lists
5215 (c-looking-at-special-brace-list)))
5216 (setq containing-sexp
(car (car special-list
)))
5217 (goto-char containing-sexp
))
5219 (setq paren-state
(c-whack-state-after containing-sexp
5221 containing-sexp
(c-most-enclosing-brace paren-state
)
5225 (if (eq (setq prev-paren
(char-after)) ?\
()
5227 (c-backward-syntactic-ws containing-sexp
)
5228 (when (/= savepos boi
)
5229 (if (and (or (not (looking-at "\\>"))
5230 (not (c-on-identifier)))
5233 (c-forward-syntactic-ws)
5235 (c-forward-syntactic-ws)
5236 (eq (char-after) ?
{)))
5237 ;; We're in an in-expression statement.
5238 ;; This syntactic element won't get an anchor pos.
5239 (c-add-syntax 'inexpr-statement
)
5240 (c-add-syntax 'arglist-cont-nonempty nil savepos
)))
5243 (1+ containing-sexp
)
5245 (setq step-type
'same
))
5247 (c-beginning-of-statement-1 containing-sexp
)))
5249 (let ((at-bod (and (eq step-type
'same
)
5250 (/= savepos
(point))
5251 (eq prev-paren ?
{))))
5253 (when (= savepos boi
)
5254 ;; If the open brace was at boi, we're always
5255 ;; done. The c-beginning-of-statement-1 call
5256 ;; above is necessary anyway, to decide the type
5257 ;; of block-intro to add.
5261 (when (eq prev-paren ?
{)
5262 (c-add-syntax (if at-bod
5264 'statement-block-intro
)
5267 (when (and (not at-bod
) savepos
)
5268 ;; Loop if the brace wasn't at boi, and we didn't
5269 ;; arrive at a defun block.
5270 (if (eq step-type
'same
)
5271 ;; Avoid backing up another sexp if the point
5272 ;; we're at now is found to be good enough in
5274 (setq step-type nil
))
5275 (if (and (not stop-at-boi-only
)
5276 (memq step-type
'(up previous beginning
)))
5277 (setq stop-at-boi-only t
))
5278 (setq boi
(c-point 'boi
)))))
5281 ;; Fill in the current point as the anchor for all the symbols
5283 (let ((p c-syntactic-context
))
5284 (while (not (eq p syntax-last
))
5286 (setcar (cdr (car p
)) (point)))
5291 (defun c-add-class-syntax (symbol classkey paren-state
)
5292 ;; The inclass and class-close syntactic symbols are added in
5293 ;; several places and some work is needed to fix everything.
5294 ;; Therefore it's collected here.
5297 (let (inexpr anchor containing-sexp
)
5298 (goto-char (aref classkey
1))
5299 (if (and (eq symbol
'inclass
) (= (point) (c-point 'boi
)))
5300 (c-add-syntax symbol
(setq anchor
(point)))
5301 (c-add-syntax symbol
(setq anchor
(aref classkey
0)))
5302 (if (and c-opt-inexpr-class-key
5303 (setq containing-sexp
(c-most-enclosing-brace paren-state
5305 inexpr
(cdr (c-looking-at-inexpr-block
5306 (c-safe-position containing-sexp
5309 (/= inexpr
(c-point 'boi inexpr
)))
5310 (c-add-syntax 'inexpr-class
)))
5313 (defun c-guess-continued-construct (indent-point
5315 beg-of-same-or-containing-stmt
5318 ;; This function contains the decision tree reached through both
5319 ;; cases 18 and 10. It's a continued statement or top level
5320 ;; construct of some kind.
5322 (let (special-brace-list)
5323 (goto-char indent-point
)
5324 (skip-chars-forward " \t")
5327 ;; (CASE A removed.)
5328 ;; CASE B: open braces for class or brace-lists
5329 ((setq special-brace-list
5330 (or (and c-special-brace-lists
5331 (c-looking-at-special-brace-list))
5332 (eq char-after-ip ?
{)))
5335 ;; CASE B.1: class-open
5337 (skip-chars-forward "{")
5338 (let ((decl (c-search-uplist-for-classkey (c-parse-state))))
5340 (setq beg-of-same-or-containing-stmt
(aref decl
0)))
5342 (c-add-syntax 'class-open beg-of-same-or-containing-stmt
))
5344 ;; CASE B.2: brace-list-open
5345 ((or (consp special-brace-list
)
5347 (goto-char beg-of-same-or-containing-stmt
)
5348 (c-syntactic-re-search-forward "=\\([^=]\\|$\\)"
5349 indent-point t t t
)))
5350 ;; The most semantically accurate symbol here is
5351 ;; brace-list-open, but we normally report it simply as a
5352 ;; statement-cont. The reason is that one normally adjusts
5353 ;; brace-list-open for brace lists as top-level constructs,
5354 ;; and brace lists inside statements is a completely different
5355 ;; context. C.f. case 5A.3.
5356 (c-beginning-of-statement-1 containing-sexp
)
5357 (c-add-stmt-syntax (if c-auto-newline-analysis
5358 ;; Turn off the dwim above when we're
5359 ;; analyzing the nature of the brace
5360 ;; for the auto newline feature.
5364 containing-sexp paren-state
))
5366 ;; CASE B.3: The body of a function declared inside a normal
5367 ;; block. Can occur e.g. in Pike and when using gcc
5368 ;; extensions. Might also trigger it with some macros followed
5369 ;; by blocks, and this gives sane indentation then too.
5370 ;; C.f. cases E, 16F and 17G.
5371 ((and (not (c-looking-at-bos))
5372 (eq (c-beginning-of-statement-1 containing-sexp nil nil t
)
5374 (c-add-stmt-syntax 'defun-open nil t nil
5375 containing-sexp paren-state
))
5377 ;; CASE B.4: Continued statement with block open.
5379 (goto-char beg-of-same-or-containing-stmt
)
5380 (c-add-stmt-syntax 'statement-cont nil nil nil
5381 containing-sexp paren-state
)
5382 (c-add-syntax 'block-open
))
5385 ;; CASE C: iostream insertion or extraction operator
5386 ((and (looking-at "\\(<<\\|>>\\)\\([^=]\\|$\\)")
5388 (goto-char beg-of-same-or-containing-stmt
)
5389 ;; If there is no preceding streamop in the statement
5390 ;; then indent this line as a normal statement-cont.
5391 (when (c-syntactic-re-search-forward
5392 "\\(<<\\|>>\\)\\([^=]\\|$\\)" indent-point
'move t t
)
5393 (c-add-syntax 'stream-op
(c-point 'boi
))
5396 ;; CASE E: In the "K&R region" of a function declared inside a
5397 ;; normal block. C.f. case B.3.
5398 ((and (save-excursion
5399 ;; Check that the next token is a '{'. This works as
5400 ;; long as no language that allows nested function
5401 ;; definitions doesn't allow stuff like member init
5402 ;; lists, K&R declarations or throws clauses there.
5404 ;; Note that we do a forward search for something ahead
5405 ;; of the indentation line here. That's not good since
5406 ;; the user might not have typed it yet. Unfortunately
5407 ;; it's exceedingly tricky to recognize a function
5408 ;; prototype in a code block without resorting to this.
5409 (c-forward-syntactic-ws)
5410 (eq (char-after) ?
{))
5411 (not (c-looking-at-bos))
5412 (eq (c-beginning-of-statement-1 containing-sexp nil nil t
)
5414 (c-add-stmt-syntax 'func-decl-cont nil t nil
5415 containing-sexp paren-state
))
5417 ;; CASE D: continued statement.
5419 (c-beginning-of-statement-1 containing-sexp
)
5420 (c-add-stmt-syntax 'statement-cont nil nil nil
5421 containing-sexp paren-state
))
5424 (defun c-guess-basic-syntax ()
5425 "Return the syntactic context of the current line."
5429 (let* ((indent-point (point))
5430 (case-fold-search nil
)
5431 (paren-state (c-parse-state))
5432 literal containing-sexp char-before-ip char-after-ip lim
5433 c-syntactic-context placeholder c-in-literal-cache step-type
5434 tmpsymbol keyword injava-inher special-brace-list
5435 ;; narrow out any enclosing class or extern "C" block
5436 (inclass-p (c-narrow-out-enclosing-class paren-state
5438 ;; `c-state-cache' is shadowed here so that we don't
5439 ;; throw it away due to the narrowing that might be done
5440 ;; by the function above. That means we must not do any
5441 ;; changes during the execution of this function, since
5442 ;; `c-invalidate-state-cache' then would change this local
5443 ;; variable and leave a bogus value in the global one.
5444 (c-state-cache (if inclass-p
5445 (c-whack-state-before (point-min) paren-state
)
5447 (c-state-cache-start (point-min))
5448 inenclosing-p macro-start in-macro-expr
5449 ;; There's always at most one syntactic element which got
5450 ;; a relpos. It's stored in syntactic-relpos.
5452 (c-stmt-delim-chars c-stmt-delim-chars
))
5453 ;; Check for meta top-level enclosing constructs such as
5454 ;; extern language definitions.
5458 (when (and inclass-p
5460 (goto-char (aref inclass-p
0))
5461 (looking-at c-other-decl-block-key
)))
5462 (setq inenclosing-p
(match-string 1))
5463 (if (string-equal inenclosing-p
"extern")
5464 ;; Compatibility with legacy choice of name for the
5465 ;; extern-lang syntactic symbols.
5466 (setq inenclosing-p
"extern-lang")))))
5468 ;; Init some position variables:
5470 ;; containing-sexp is the open paren of the closest
5471 ;; surrounding sexp or nil if there is none that hasn't been
5474 ;; lim is the position after the closest preceding brace sexp
5475 ;; (nested sexps are ignored), or the position after
5476 ;; containing-sexp if there is none, or (point-min) if
5477 ;; containing-sexp is nil.
5479 ;; c-state-cache is the state from c-parse-state at
5480 ;; indent-point, without any parens outside the region
5481 ;; narrowed by c-narrow-out-enclosing-class.
5483 ;; paren-state is the state from c-parse-state outside
5484 ;; containing-sexp, or at indent-point if containing-sexp is
5485 ;; nil. paren-state is not limited to the narrowed region, as
5486 ;; opposed to c-state-cache.
5489 (setq containing-sexp
(car paren-state
)
5490 paren-state
(cdr paren-state
))
5491 (if (consp containing-sexp
)
5493 (setq lim
(cdr containing-sexp
))
5494 (if (cdr c-state-cache
)
5495 ;; Ignore balanced paren. The next entry
5496 ;; can't be another one.
5497 (setq containing-sexp
(car (cdr c-state-cache
))
5498 paren-state
(cdr paren-state
))
5499 ;; If there is no surrounding open paren then
5500 ;; put the last balanced pair back on paren-state.
5501 (setq paren-state
(cons containing-sexp paren-state
)
5502 containing-sexp nil
)))
5503 (setq lim
(1+ containing-sexp
))))
5504 (setq lim
(point-min)))
5506 ;; If we're in a parenthesis list then ',' delimits the
5507 ;; "statements" rather than being an operator (with the
5508 ;; exception of the "for" clause). This difference is
5509 ;; typically only noticeable when statements are used in macro
5511 (when (and containing-sexp
5512 (eq (char-after containing-sexp
) ?\
())
5513 (setq c-stmt-delim-chars c-stmt-delim-chars-with-comma
))
5515 ;; cache char before and after indent point, and move point to
5516 ;; the most likely position to perform the majority of tests
5517 (goto-char indent-point
)
5518 (c-backward-syntactic-ws lim
)
5519 (setq char-before-ip
(char-before))
5520 (goto-char indent-point
)
5521 (skip-chars-forward " \t")
5522 (setq char-after-ip
(char-after))
5524 ;; are we in a literal?
5525 (setq literal
(c-in-literal lim
))
5527 ;; now figure out syntactic qualities of the current line
5529 ;; CASE 1: in a string.
5530 ((eq literal
'string
)
5531 (c-add-syntax 'string
(c-point 'bopl
)))
5532 ;; CASE 2: in a C or C++ style comment.
5533 ((and (memq literal
'(c c
++))
5534 ;; This is a kludge for XEmacs where we use
5535 ;; `buffer-syntactic-context', which doesn't correctly
5536 ;; recognize "\*/" to end a block comment.
5537 ;; `parse-partial-sexp' which is used by
5538 ;; `c-literal-limits' will however do that in most
5539 ;; versions, which results in that we get nil from
5540 ;; `c-literal-limits' even when `c-in-literal' claims
5541 ;; we're inside a comment.
5542 (setq placeholder
(c-literal-limits lim
)))
5543 (c-add-syntax literal
(car placeholder
)))
5544 ;; CASE 3: in a cpp preprocessor macro continuation.
5545 ((and (save-excursion
5546 (when (c-beginning-of-macro)
5547 (setq macro-start
(point))))
5548 (/= macro-start
(c-point 'boi
))
5550 (setq tmpsymbol
'cpp-macro-cont
)
5551 (or (not c-syntactic-indentation-in-macros
)
5553 (goto-char macro-start
)
5554 ;; If at the beginning of the body of a #define
5555 ;; directive then analyze as cpp-define-intro
5556 ;; only. Go on with the syntactic analysis
5557 ;; otherwise. in-macro-expr is set if we're in a
5558 ;; cpp expression, i.e. before the #define body
5559 ;; or anywhere in a non-#define directive.
5560 (if (c-forward-to-cpp-define-body)
5561 (let ((indent-boi (c-point 'boi indent-point
)))
5562 (setq in-macro-expr
(> (point) indent-boi
)
5563 tmpsymbol
'cpp-define-intro
)
5564 (= (point) indent-boi
))
5565 (setq in-macro-expr t
)
5567 (c-add-syntax tmpsymbol macro-start
)
5568 (setq macro-start nil
))
5569 ;; CASE 11: an else clause?
5570 ((looking-at "else\\>[^_]")
5571 (c-beginning-of-statement-1 containing-sexp
)
5572 (c-add-stmt-syntax 'else-clause nil t nil
5573 containing-sexp paren-state
))
5574 ;; CASE 12: while closure of a do/while construct?
5575 ((and (looking-at "while\\>[^_]")
5577 (prog1 (eq (c-beginning-of-statement-1 containing-sexp
)
5579 (setq placeholder
(point)))))
5580 (goto-char placeholder
)
5581 (c-add-stmt-syntax 'do-while-closure nil t nil
5582 containing-sexp paren-state
))
5583 ;; CASE 13: A catch or finally clause? This case is simpler
5584 ;; than if-else and do-while, because a block is required
5585 ;; after every try, catch and finally.
5587 (and (cond ((c-major-mode-is 'c
++-mode
)
5588 (looking-at "catch\\>[^_]"))
5589 ((c-major-mode-is 'java-mode
)
5590 (looking-at "\\(catch\\|finally\\)\\>[^_]")))
5591 (and (c-safe (c-backward-syntactic-ws)
5594 (eq (char-after) ?
{)
5595 (c-safe (c-backward-syntactic-ws)
5598 (if (eq (char-after) ?\
()
5599 (c-safe (c-backward-sexp) t
)
5601 (looking-at "\\(try\\|catch\\)\\>[^_]")
5602 (setq placeholder
(point))))
5603 (goto-char placeholder
)
5604 (c-add-stmt-syntax 'catch-clause nil t nil
5605 containing-sexp paren-state
))
5606 ;; CASE 18: A substatement we can recognize by keyword.
5608 (and c-opt-block-stmt-key
5609 (if (c-mode-is-new-awk-p)
5610 (c-awk-prev-line-incomplete-p containing-sexp
) ; ACM 2002/3/29
5611 (not (eq char-before-ip ?\
;)))
5612 (not (memq char-after-ip
'(?\
) ?\
] ?
,)))
5613 (or (not (eq char-before-ip ?
}))
5614 (c-looking-at-inexpr-block-backward c-state-cache
))
5617 ;; Ought to cache the result from the
5618 ;; c-beginning-of-statement-1 calls here.
5619 (setq placeholder
(point))
5620 (while (eq (setq step-type
5621 (c-beginning-of-statement-1 lim
))
5623 (if (eq step-type
'previous
)
5624 (goto-char placeholder
)
5625 (setq placeholder
(point))
5626 (if (and (eq step-type
'same
)
5627 (not (looking-at c-opt-block-stmt-key
)))
5628 ;; Step up to the containing statement if we
5629 ;; stayed in the same one.
5633 (c-beginning-of-statement-1 lim
))
5636 (setq placeholder
(point))
5637 ;; There was no containing statement afterall.
5638 (goto-char placeholder
)))))
5640 (if (looking-at c-block-stmt-2-key
)
5641 ;; Require a parenthesis after these keywords.
5642 ;; Necessary to catch e.g. synchronized in Java,
5643 ;; which can be used both as statement and
5645 (and (zerop (c-forward-token-2 1 nil
))
5646 (eq (char-after) ?\
())
5647 (looking-at c-opt-block-stmt-key
))))
5648 (if (eq step-type
'up
)
5649 ;; CASE 18A: Simple substatement.
5651 (goto-char placeholder
)
5653 ((eq char-after-ip ?
{)
5654 (c-add-stmt-syntax 'substatement-open nil nil nil
5655 containing-sexp paren-state
))
5657 (goto-char indent-point
)
5658 (back-to-indentation)
5659 (looking-at c-label-key
))
5660 (c-add-stmt-syntax 'substatement-label nil nil nil
5661 containing-sexp paren-state
))
5663 (c-add-stmt-syntax 'substatement nil nil nil
5664 containing-sexp paren-state
))))
5665 ;; CASE 18B: Some other substatement. This is shared
5667 (c-guess-continued-construct indent-point
5672 ;; CASE 4: In-expression statement. C.f. cases 7B, 16A and
5674 ((and (or c-opt-inexpr-class-key
5675 c-opt-inexpr-block-key
5677 (setq placeholder
(c-looking-at-inexpr-block
5678 (c-safe-position containing-sexp paren-state
)
5680 (setq tmpsymbol
(assq (car placeholder
)
5681 '((inexpr-class . class-open
)
5682 (inexpr-statement . block-open
))))
5684 ;; It's a statement block or an anonymous class.
5685 (setq tmpsymbol
(cdr tmpsymbol
))
5686 ;; It's a Pike lambda. Check whether we are between the
5687 ;; lambda keyword and the argument list or at the defun
5689 (setq tmpsymbol
(if (eq char-after-ip ?
{)
5691 'lambda-intro-cont
)))
5692 (goto-char (cdr placeholder
))
5693 (back-to-indentation)
5694 (c-add-stmt-syntax tmpsymbol nil t nil
5695 (c-most-enclosing-brace c-state-cache
(point))
5696 (c-whack-state-after (point) paren-state
))
5697 (unless (eq (point) (cdr placeholder
))
5698 (c-add-syntax (car placeholder
))))
5699 ;; CASE 5: Line is at top level.
5700 ((null containing-sexp
)
5702 ;; CASE 5A: we are looking at a defun, brace list, class,
5703 ;; or inline-inclass method opening brace
5704 ((setq special-brace-list
5705 (or (and c-special-brace-lists
5706 (c-looking-at-special-brace-list))
5707 (eq char-after-ip ?
{)))
5709 ;; CASE 5A.1: Non-class declaration block open.
5711 (goto-char indent-point
)
5712 (skip-chars-forward " \t")
5713 (and (c-safe (c-backward-sexp 2) t
)
5714 (looking-at c-other-decl-block-key
)
5715 (setq keyword
(match-string 1)
5716 placeholder
(point))
5717 (if (string-equal keyword
"extern")
5718 ;; Special case for extern-lang-open. The
5719 ;; check for a following string is disabled
5720 ;; since it doesn't disambiguate anything.
5722 ;; (c-forward-sexp 1)
5723 ;; (c-forward-syntactic-ws)
5724 ;; (eq (char-after) ?\"))
5725 (setq tmpsymbol
'extern-lang-open
))
5726 (setq tmpsymbol
(intern (concat keyword
"-open"))))
5728 (goto-char placeholder
)
5729 (c-add-syntax tmpsymbol
(c-point 'boi
)))
5730 ;; CASE 5A.2: we are looking at a class opening brace
5732 (goto-char indent-point
)
5733 (skip-chars-forward " \t{")
5734 (let ((decl (c-search-uplist-for-classkey (c-parse-state))))
5736 (setq placeholder
(aref decl
0)))
5738 (c-add-syntax 'class-open placeholder
))
5739 ;; CASE 5A.3: brace list open
5741 (c-beginning-of-decl-1 lim
)
5742 (while (looking-at c-specifier-key
)
5743 (goto-char (match-end 1))
5744 (c-forward-syntactic-ws indent-point
))
5745 (setq placeholder
(c-point 'boi
))
5746 (or (consp special-brace-list
)
5747 (and (or (save-excursion
5748 (goto-char indent-point
)
5749 (setq tmpsymbol nil
)
5750 (while (and (> (point) placeholder
)
5751 (zerop (c-backward-token-2 1 t
))
5752 (/= (char-after) ?
=))
5753 (and c-opt-inexpr-brace-list-key
5755 (looking-at c-opt-inexpr-brace-list-key
)
5756 (setq tmpsymbol
'topmost-intro-cont
)))
5757 (eq (char-after) ?
=))
5758 (looking-at c-brace-list-key
))
5760 (while (and (< (point) indent-point
)
5761 (zerop (c-forward-token-2 1 t
))
5762 (not (memq (char-after) '(?\
; ?\()))))
5763 (not (memq (char-after) '(?\
; ?\()))
5765 (if (and (not c-auto-newline-analysis
)
5766 (c-major-mode-is 'java-mode
)
5767 (eq tmpsymbol
'topmost-intro-cont
))
5768 ;; We're in Java and have found that the open brace
5769 ;; belongs to a "new Foo[]" initialization list,
5770 ;; which means the brace list is part of an
5771 ;; expression and not a top level definition. We
5772 ;; therefore treat it as any topmost continuation
5773 ;; even though the semantically correct symbol still
5774 ;; is brace-list-open, on the same grounds as in
5777 (c-beginning-of-statement-1 lim
)
5778 (c-add-syntax 'topmost-intro-cont
(c-point 'boi
)))
5779 (c-add-syntax 'brace-list-open placeholder
)))
5780 ;; CASE 5A.4: inline defun open
5781 ((and inclass-p
(not inenclosing-p
))
5782 (c-add-syntax 'inline-open
)
5783 (c-add-class-syntax 'inclass inclass-p paren-state
))
5784 ;; CASE 5A.5: ordinary defun open
5786 (goto-char placeholder
)
5787 (if (or inclass-p macro-start
)
5788 (c-add-syntax 'defun-open
(c-point 'boi
))
5789 ;; Bogus to use bol here, but it's the legacy.
5790 (c-add-syntax 'defun-open
(c-point 'bol
)))
5792 ;; CASE 5B: first K&R arg decl or member init
5793 ((c-just-after-func-arglist-p lim
)
5795 ;; CASE 5B.1: a member init
5796 ((or (eq char-before-ip ?
:)
5797 (eq char-after-ip ?
:))
5798 ;; this line should be indented relative to the beginning
5799 ;; of indentation for the topmost-intro line that contains
5800 ;; the prototype's open paren
5801 ;; TBD: is the following redundant?
5802 (if (eq char-before-ip ?
:)
5804 (c-backward-syntactic-ws lim
)
5805 ;; TBD: is the preceding redundant?
5806 (if (eq (char-before) ?
:)
5807 (progn (forward-char -
1)
5808 (c-backward-syntactic-ws lim
)))
5809 (if (eq (char-before) ?\
))
5810 (c-backward-sexp 1))
5811 (setq placeholder
(point))
5813 (and (c-safe (c-backward-sexp 1) t
)
5814 (looking-at "throw[^_]")
5815 (c-safe (c-backward-sexp 1) t
)
5816 (setq placeholder
(point))))
5817 (goto-char placeholder
)
5818 (c-add-syntax 'member-init-intro
(c-point 'boi
))
5819 ;; we don't need to add any class offset since this
5820 ;; should be relative to the ctor's indentation
5822 ;; CASE 5B.2: K&R arg decl intro
5824 (c-beginning-of-statement-1 lim
)
5825 (c-add-syntax 'knr-argdecl-intro
(c-point 'boi
))
5827 (c-add-class-syntax 'inclass inclass-p paren-state
)))
5828 ;; CASE 5B.3: Inside a member init list.
5829 ((c-beginning-of-member-init-list lim
)
5830 (c-forward-syntactic-ws)
5831 (c-add-syntax 'member-init-cont
(point)))
5832 ;; CASE 5B.4: Nether region after a C++ or Java func
5833 ;; decl, which could include a `throws' declaration.
5835 (c-beginning-of-statement-1 lim
)
5836 (c-add-syntax 'func-decl-cont
(c-point 'boi
))
5838 ;; CASE 5C: inheritance line. could be first inheritance
5839 ;; line, or continuation of a multiple inheritance
5840 ((or (and (c-major-mode-is 'c
++-mode
)
5842 (when (eq char-after-ip ?
,)
5843 (skip-chars-forward " \t")
5845 (looking-at c-opt-postfix-decl-spec-key
)))
5846 (and (or (eq char-before-ip ?
:)
5847 ;; watch out for scope operator
5849 (and (eq char-after-ip ?
:)
5850 (c-safe (forward-char 1) t
)
5851 (not (eq (char-after) ?
:))
5854 (c-backward-syntactic-ws lim
)
5855 (if (eq char-before-ip ?
:)
5858 (c-backward-syntactic-ws lim
)))
5859 (back-to-indentation)
5860 (looking-at c-class-key
)))
5862 (and (c-major-mode-is 'java-mode
)
5863 (let ((fence (save-excursion
5864 (c-beginning-of-statement-1 lim
)
5869 (cond ((looking-at c-opt-postfix-decl-spec-key
)
5870 (setq injava-inher
(cons cont
(point))
5872 ((or (not (c-safe (c-forward-sexp -
1) t
))
5878 (not (c-crosses-statement-barrier-p (cdr injava-inher
)
5882 ;; CASE 5C.1: non-hanging colon on an inher intro
5883 ((eq char-after-ip ?
:)
5884 (c-beginning-of-statement-1 lim
)
5885 (c-add-syntax 'inher-intro
(c-point 'boi
))
5886 ;; don't add inclass symbol since relative point already
5887 ;; contains any class offset
5889 ;; CASE 5C.2: hanging colon on an inher intro
5890 ((eq char-before-ip ?
:)
5891 (c-beginning-of-statement-1 lim
)
5892 (c-add-syntax 'inher-intro
(c-point 'boi
))
5894 (c-add-class-syntax 'inclass inclass-p paren-state
)))
5895 ;; CASE 5C.3: in a Java implements/extends
5897 (let ((where (cdr injava-inher
))
5898 (cont (car injava-inher
)))
5900 (cond ((looking-at "throws\\>[^_]")
5901 (c-add-syntax 'func-decl-cont
5902 (progn (c-beginning-of-statement-1 lim
)
5904 (cont (c-add-syntax 'inher-cont where
))
5905 (t (c-add-syntax 'inher-intro
5906 (progn (goto-char (cdr injava-inher
))
5907 (c-beginning-of-statement-1 lim
)
5910 ;; CASE 5C.4: a continued inheritance line
5912 (c-beginning-of-inheritance-list lim
)
5913 (c-add-syntax 'inher-cont
(point))
5914 ;; don't add inclass symbol since relative point already
5915 ;; contains any class offset
5917 ;; CASE 5D: this could be a top-level initialization, a
5918 ;; member init list continuation, or a template argument
5919 ;; list continuation.
5920 ((c-with-syntax-table (if (c-major-mode-is 'c
++-mode
)
5921 c
++-template-syntax-table
5924 ;; Note: We use the fact that lim is always after any
5925 ;; preceding brace sexp.
5926 (while (and (zerop (c-backward-token-2 1 t lim
))
5927 (not (looking-at "[;<,=]"))))
5928 (or (memq (char-after) '(?
, ?
=))
5929 (and (c-major-mode-is 'c
++-mode
)
5930 (zerop (c-backward-token-2 1 nil lim
))
5931 (eq (char-after) ?
<)))))
5932 (goto-char indent-point
)
5934 (c-beginning-of-member-init-list lim
))
5936 ;; CASE 5D.1: hanging member init colon, but watch out
5937 ;; for bogus matches on access specifiers inside classes.
5940 (setq placeholder
(point))
5941 (c-backward-token-2 1 t lim
)
5942 (and (eq (char-after) ?
:)
5943 (not (eq (char-before) ?
:))))
5945 (goto-char placeholder
)
5946 (back-to-indentation)
5948 (/= (car (save-excursion
5949 (parse-partial-sexp (point) placeholder
)))
5952 (if c-opt-access-key
5953 (not (looking-at c-opt-access-key
)) t
)
5954 (not (looking-at c-class-key
))
5955 (if c-opt-bitfield-key
5956 (not (looking-at c-opt-bitfield-key
)) t
))
5958 (goto-char placeholder
)
5959 (c-forward-syntactic-ws)
5960 (c-add-syntax 'member-init-cont
(point))
5961 ;; we do not need to add class offset since relative
5962 ;; point is the member init above us
5964 ;; CASE 5D.2: non-hanging member init colon
5966 (c-forward-syntactic-ws indent-point
)
5967 (eq (char-after) ?
:))
5968 (skip-chars-forward " \t:")
5969 (c-add-syntax 'member-init-cont
(point)))
5970 ;; CASE 5D.3: perhaps a template list continuation?
5971 ((and (c-major-mode-is 'c
++-mode
)
5974 (c-with-syntax-table c
++-template-syntax-table
5975 (goto-char indent-point
)
5976 (setq placeholder
(c-up-list-backward (point)))
5978 (eq (char-after placeholder
) ?
<))))))
5979 ;; we can probably indent it just like an arglist-cont
5980 (goto-char placeholder
)
5981 (c-beginning-of-statement-1 lim t
)
5982 (c-add-syntax 'template-args-cont
(c-point 'boi
)))
5983 ;; CASE 5D.4: perhaps a multiple inheritance line?
5984 ((and (c-major-mode-is 'c
++-mode
)
5986 (c-beginning-of-statement-1 lim
)
5987 (setq placeholder
(point))
5988 (if (looking-at "static\\>[^_]")
5989 (c-forward-token-2 1 nil indent-point
))
5990 (and (looking-at c-class-key
)
5991 (zerop (c-forward-token-2 2 nil indent-point
))
5992 (if (eq (char-after) ?
<)
5993 (c-with-syntax-table c
++-template-syntax-table
5994 (zerop (c-forward-token-2 1 t indent-point
)))
5996 (eq (char-after) ?
:))))
5997 (goto-char placeholder
)
5998 (c-add-syntax 'inher-cont
(c-point 'boi
)))
5999 ;; CASE 5D.5: Continuation of the "expression part" of a
6000 ;; top level construct.
6002 (while (and (eq (car (c-beginning-of-decl-1 containing-sexp
))
6005 (c-backward-syntactic-ws)
6006 (eq (char-before) ?
}))))
6008 (if (eq char-before-ip ?
,)
6009 ;; A preceding comma at the top level means that a
6010 ;; new variable declaration starts here. Use
6011 ;; topmost-intro-cont for it, for consistency with
6012 ;; the first variable declaration. C.f. case 5N.
6015 nil nil nil containing-sexp paren-state
))
6017 ;; CASE 5E: we are looking at a access specifier
6020 (looking-at c-opt-access-key
))
6021 (setq placeholder
(c-add-class-syntax 'inclass inclass-p
6023 ;; Append access-label with the same anchor point as inclass gets.
6024 (c-append-syntax 'access-label placeholder
))
6025 ;; CASE 5F: Close of a non-class declaration level block.
6027 (eq char-after-ip ?
}))
6028 (c-add-syntax (intern (concat inenclosing-p
"-close"))
6029 (aref inclass-p
0)))
6030 ;; CASE 5G: we are looking at the brace which closes the
6031 ;; enclosing nested class decl
6033 (eq char-after-ip ?
})
6038 (and (c-safe (c-backward-sexp 1) t
)
6039 (= (point) (aref inclass-p
1))
6041 (c-add-class-syntax 'class-close inclass-p paren-state
))
6042 ;; CASE 5H: we could be looking at subsequent knr-argdecls
6043 ((and c-recognize-knr-p
6044 (not (eq char-before-ip ?
}))
6046 (setq placeholder
(cdr (c-beginning-of-decl-1 lim
)))
6048 ;; Do an extra check to avoid tripping up on
6049 ;; statements that occur in invalid contexts
6050 ;; (e.g. in macro bodies where we don't really
6051 ;; know the context of what we're looking at).
6052 (not (and c-opt-block-stmt-key
6053 (looking-at c-opt-block-stmt-key
)))))
6054 (< placeholder indent-point
))
6055 (goto-char placeholder
)
6056 (c-add-syntax 'knr-argdecl
(point)))
6057 ;; CASE 5I: ObjC method definition.
6058 ((and c-opt-method-key
6059 (looking-at c-opt-method-key
))
6060 (c-beginning-of-statement-1 lim
)
6061 (c-add-syntax 'objc-method-intro
(c-point 'boi
)))
6062 ;; CASE 5P: AWK pattern or function or continuation
6064 ((c-mode-is-new-awk-p)
6065 (setq placeholder
(point))
6067 (if (and (eq (c-beginning-of-statement-1) 'same
)
6068 (/= (point) placeholder
))
6072 containing-sexp paren-state
))
6073 ;; CASE 5N: At a variable declaration that follows a class
6074 ;; definition or some other block declaration that doesn't
6075 ;; end at the closing '}'. C.f. case 5D.5.
6077 (c-backward-syntactic-ws lim
)
6078 (and (eq (char-before) ?
})
6080 (let ((start (point)))
6082 ;; Speed up the backward search a bit.
6083 (goto-char (car (car paren-state
))))
6084 (c-beginning-of-decl-1 containing-sexp
)
6085 (setq placeholder
(point))
6086 (if (= start
(point))
6087 ;; The '}' is unbalanced.
6090 (> (point) indent-point
))))))
6091 (goto-char placeholder
)
6092 (c-add-stmt-syntax 'topmost-intro-cont nil nil nil
6093 containing-sexp paren-state
))
6094 ;; CASE 5J: we are at the topmost level, make
6095 ;; sure we skip back past any access specifiers
6097 (while (and inclass-p
6101 (c-safe (c-backward-sexp 1) t
)
6102 (looking-at c-opt-access-key
)))
6104 (c-backward-syntactic-ws lim
))
6106 (if (c-mode-is-new-awk-p)
6107 (not (c-awk-prev-line-incomplete-p))
6108 (memq (char-before) '(?\
; ?})))
6109 (and (c-major-mode-is 'objc-mode
)
6111 (c-beginning-of-statement-1 lim
)
6112 (eq (char-after) ?
@)))))
6113 ;; real beginning-of-line could be narrowed out due to
6114 ;; enclosure in a class block
6117 (c-add-syntax 'topmost-intro
(c-point 'bol
))
6118 ;; Using bol instead of boi above is highly bogus, and
6119 ;; it makes our lives hard to remain compatible. :P
6122 (goto-char (aref inclass-p
1))
6123 (or (= (point) (c-point 'boi
))
6124 (goto-char (aref inclass-p
0)))
6126 (c-add-syntax (intern (concat "in" inenclosing-p
))
6128 (c-add-class-syntax 'inclass inclass-p paren-state
))
6130 (when (and c-syntactic-indentation-in-macros
6132 (/= macro-start
(c-point 'boi indent-point
)))
6133 (c-add-syntax 'cpp-define-intro
)
6134 (setq macro-start nil
))
6136 ;; CASE 5K: we are at an ObjC method definition
6137 ;; continuation line.
6138 ((and c-opt-method-key
6140 (c-beginning-of-statement-1 lim
)
6142 (looking-at c-opt-method-key
)))
6143 (c-add-syntax 'objc-method-args-cont
(point)))
6144 ;; CASE 5L: we are at the first argument of a template
6145 ;; arglist that begins on the previous line.
6146 ((eq (char-before) ?
<)
6147 (c-beginning-of-statement-1 (c-safe-position (point) paren-state
))
6148 (c-add-syntax 'template-args-cont
(c-point 'boi
)))
6149 ;; CASE 5M: we are at a topmost continuation line
6151 (c-beginning-of-statement-1 (c-safe-position (point) paren-state
))
6152 (c-add-syntax 'topmost-intro-cont
(c-point 'boi
)))
6154 ;; (CASE 6 has been removed.)
6155 ;; CASE 7: line is an expression, not a statement. Most
6156 ;; likely we are either in a function prototype or a function
6157 ;; call argument list
6158 ((not (or (and c-special-brace-lists
6160 (goto-char containing-sexp
)
6161 (c-looking-at-special-brace-list)))
6162 (eq (char-after containing-sexp
) ?
{)))
6164 ;; CASE 7A: we are looking at the arglist closing paren.
6166 ((memq char-after-ip
'(?\
) ?\
]))
6167 (goto-char containing-sexp
)
6168 (setq placeholder
(c-point 'boi
))
6169 (if (and (c-safe (backward-up-list 1) t
)
6170 (> (point) placeholder
))
6173 (skip-chars-forward " \t"))
6174 (goto-char placeholder
))
6175 (c-add-stmt-syntax 'arglist-close
(list containing-sexp
) t nil
6176 (c-most-enclosing-brace paren-state
(point))
6177 (c-whack-state-after (point) paren-state
)))
6178 ;; CASE 7B: Looking at the opening brace of an
6179 ;; in-expression block or brace list. C.f. cases 4, 16A
6181 ((and (eq char-after-ip ?
{)
6183 (setq placeholder
(c-inside-bracelist-p (point)
6186 (setq tmpsymbol
'(brace-list-open . inexpr-class
))
6187 (setq tmpsymbol
'(block-open . inexpr-statement
)
6189 (cdr-safe (c-looking-at-inexpr-block
6190 (c-safe-position containing-sexp
6193 ;; placeholder is nil if it's a block directly in
6194 ;; a function arglist. That makes us skip out of
6197 (goto-char placeholder
)
6198 (back-to-indentation)
6199 (c-add-stmt-syntax (car tmpsymbol
) nil t nil
6200 (c-most-enclosing-brace paren-state
(point))
6201 (c-whack-state-after (point) paren-state
))
6202 (if (/= (point) placeholder
)
6203 (c-add-syntax (cdr tmpsymbol
))))
6204 ;; CASE 7C: we are looking at the first argument in an empty
6205 ;; argument list. Use arglist-close if we're actually
6206 ;; looking at a close paren or bracket.
6207 ((memq char-before-ip
'(?\
( ?\
[))
6208 (goto-char containing-sexp
)
6209 (setq placeholder
(c-point 'boi
))
6210 (when (and (c-safe (backward-up-list 1) t
)
6211 (> (point) placeholder
))
6213 (skip-chars-forward " \t")
6214 (setq placeholder
(point)))
6215 (c-add-syntax 'arglist-intro placeholder
))
6216 ;; CASE 7D: we are inside a conditional test clause. treat
6217 ;; these things as statements
6219 (goto-char containing-sexp
)
6220 (and (c-safe (c-forward-sexp -
1) t
)
6221 (looking-at "\\<for\\>[^_]")))
6222 (goto-char (1+ containing-sexp
))
6223 (c-forward-syntactic-ws indent-point
)
6224 (if (eq char-before-ip ?\
;)
6225 (c-add-syntax 'statement
(point))
6226 (c-add-syntax 'statement-cont
(point))
6228 ;; CASE 7E: maybe a continued ObjC method call. This is the
6229 ;; case when we are inside a [] bracketed exp, and what
6230 ;; precede the opening bracket is not an identifier.
6231 ((and c-opt-method-key
6232 (eq (char-after containing-sexp
) ?\
[)
6234 (goto-char (1- containing-sexp
))
6235 (c-backward-syntactic-ws (c-point 'bod
))
6236 (if (not (looking-at c-symbol-key
))
6237 (c-add-syntax 'objc-method-call-cont containing-sexp
))
6239 ;; CASE 7F: we are looking at an arglist continuation line,
6240 ;; but the preceding argument is on the same line as the
6241 ;; opening paren. This case includes multi-line
6242 ;; mathematical paren groupings, but we could be on a
6243 ;; for-list continuation line. C.f. case 7A.
6245 (goto-char (1+ containing-sexp
))
6246 (skip-chars-forward " \t")
6248 (not (looking-at "\\\\$"))))
6249 (goto-char containing-sexp
)
6250 (setq placeholder
(c-point 'boi
))
6251 (if (and (c-safe (backward-up-list 1) t
)
6252 (> (point) placeholder
))
6255 (skip-chars-forward " \t"))
6256 (goto-char placeholder
))
6257 (c-add-stmt-syntax 'arglist-cont-nonempty
(list containing-sexp
)
6259 (c-most-enclosing-brace c-state-cache
(point))
6260 (c-whack-state-after (point) paren-state
)))
6261 ;; CASE 7G: we are looking at just a normal arglist
6262 ;; continuation line
6263 (t (c-forward-syntactic-ws indent-point
)
6264 (c-add-syntax 'arglist-cont
(c-point 'boi
)))
6266 ;; CASE 8: func-local multi-inheritance line
6267 ((and (c-major-mode-is 'c
++-mode
)
6269 (goto-char indent-point
)
6270 (skip-chars-forward " \t")
6271 (looking-at c-opt-postfix-decl-spec-key
)))
6272 (goto-char indent-point
)
6273 (skip-chars-forward " \t")
6275 ;; CASE 8A: non-hanging colon on an inher intro
6276 ((eq char-after-ip ?
:)
6277 (c-backward-syntactic-ws lim
)
6278 (c-add-syntax 'inher-intro
(c-point 'boi
)))
6279 ;; CASE 8B: hanging colon on an inher intro
6280 ((eq char-before-ip ?
:)
6281 (c-add-syntax 'inher-intro
(c-point 'boi
)))
6282 ;; CASE 8C: a continued inheritance line
6284 (c-beginning-of-inheritance-list lim
)
6285 (c-add-syntax 'inher-cont
(point))
6287 ;; CASE 9: we are inside a brace-list
6288 ((and (not (c-mode-is-new-awk-p)) ; Maybe this isn't needed (ACM, 2002/3/29)
6289 (setq special-brace-list
6290 (or (and c-special-brace-lists
6292 (goto-char containing-sexp
)
6293 (c-looking-at-special-brace-list)))
6294 (c-inside-bracelist-p containing-sexp paren-state
))))
6296 ;; CASE 9A: In the middle of a special brace list opener.
6297 ((and (consp special-brace-list
)
6299 (goto-char containing-sexp
)
6300 (eq (char-after) ?\
())
6301 (eq char-after-ip
(car (cdr special-brace-list
))))
6302 (goto-char (car (car special-brace-list
)))
6303 (skip-chars-backward " \t")
6305 (assoc 'statement-cont
6306 (setq placeholder
(c-guess-basic-syntax))))
6307 (setq c-syntactic-context placeholder
)
6308 (c-beginning-of-statement-1
6309 (c-safe-position (1- containing-sexp
) paren-state
))
6310 (c-forward-token-2 0)
6311 (while (looking-at c-specifier-key
)
6312 (goto-char (match-end 1))
6313 (c-forward-syntactic-ws))
6314 (c-add-syntax 'brace-list-open
(c-point 'boi
))))
6315 ;; CASE 9B: brace-list-close brace
6316 ((if (consp special-brace-list
)
6317 ;; Check special brace list closer.
6319 (goto-char (car (car special-brace-list
)))
6321 (goto-char indent-point
)
6322 (back-to-indentation)
6324 ;; We were between the special close char and the `)'.
6325 (and (eq (char-after) ?\
))
6326 (eq (1+ (point)) (cdr (car special-brace-list
))))
6327 ;; We were before the special close char.
6328 (and (eq (char-after) (cdr (cdr special-brace-list
)))
6329 (zerop (c-forward-token-2))
6330 (eq (1+ (point)) (cdr (car special-brace-list
)))))))
6331 ;; Normal brace list check.
6332 (and (eq char-after-ip ?
})
6333 (c-safe (goto-char (c-up-list-backward (point))) t
)
6334 (= (point) containing-sexp
)))
6335 (if (eq (point) (c-point 'boi
))
6336 (c-add-syntax 'brace-list-close
(point))
6337 (setq lim
(c-most-enclosing-brace c-state-cache
(point)))
6338 (c-beginning-of-statement-1 lim
)
6339 (c-add-stmt-syntax 'brace-list-close nil t t lim
6340 (c-whack-state-after (point) paren-state
))))
6342 ;; Prepare for the rest of the cases below by going to the
6343 ;; token following the opening brace
6344 (if (consp special-brace-list
)
6346 (goto-char (car (car special-brace-list
)))
6347 (c-forward-token-2 1 nil indent-point
))
6348 (goto-char containing-sexp
))
6350 (let ((start (point)))
6351 (c-forward-syntactic-ws indent-point
)
6352 (goto-char (max start
(c-point 'bol
))))
6353 (c-skip-ws-forward indent-point
)
6355 ;; CASE 9C: we're looking at the first line in a brace-list
6356 ((= (point) indent-point
)
6357 (if (consp special-brace-list
)
6358 (goto-char (car (car special-brace-list
)))
6359 (goto-char containing-sexp
))
6360 (if (eq (point) (c-point 'boi
))
6361 (c-add-syntax 'brace-list-intro
(point))
6362 (setq lim
(c-most-enclosing-brace c-state-cache
(point)))
6363 (c-beginning-of-statement-1 lim
)
6364 (c-add-stmt-syntax 'brace-list-intro nil t t lim
6365 (c-whack-state-after (point) paren-state
))))
6366 ;; CASE 9D: this is just a later brace-list-entry or
6368 (t (if (or (eq char-after-ip ?
{)
6369 (and c-special-brace-lists
6371 (goto-char indent-point
)
6372 (c-forward-syntactic-ws (c-point 'eol
))
6373 (c-looking-at-special-brace-list (point)))))
6374 (c-add-syntax 'brace-entry-open
(point))
6375 (c-add-syntax 'brace-list-entry
(point))
6378 ;; CASE 10: A continued statement or top level construct.
6379 ((and (if (c-mode-is-new-awk-p)
6380 (c-awk-prev-line-incomplete-p containing-sexp
) ; ACM 2002/3/29
6381 (and (not (memq char-before-ip
'(?\
; ?:)))
6382 (or (not (eq char-before-ip ?
}))
6383 (c-looking-at-inexpr-block-backward c-state-cache
))))
6386 (c-beginning-of-statement-1 containing-sexp
)
6387 (setq placeholder
(point))))
6388 (/= placeholder containing-sexp
))
6389 ;; This is shared with case 18.
6390 (c-guess-continued-construct indent-point
6395 ;; CASE 14: A case or default label
6396 ((looking-at c-label-kwds-regexp
)
6397 (goto-char containing-sexp
)
6398 (setq lim
(c-most-enclosing-brace c-state-cache containing-sexp
))
6399 (c-backward-to-block-anchor lim
)
6400 (c-add-stmt-syntax 'case-label nil t nil
6402 ;; CASE 15: any other label
6403 ((looking-at c-label-key
)
6404 (goto-char containing-sexp
)
6405 (setq lim
(c-most-enclosing-brace c-state-cache containing-sexp
))
6408 (if (and (eq (c-beginning-of-statement-1 lim
) 'up
)
6409 (looking-at "switch\\>[^_]"))
6410 ;; If the surrounding statement is a switch then
6411 ;; let's analyze all labels as switch labels, so
6412 ;; that they get lined up consistently.
6415 (c-backward-to-block-anchor lim
)
6416 (c-add-stmt-syntax tmpsymbol nil t nil
6418 ;; CASE 16: block close brace, possibly closing the defun or
6420 ((eq char-after-ip ?
})
6421 ;; From here on we have the next containing sexp in lim.
6422 (setq lim
(c-most-enclosing-brace paren-state
))
6423 (goto-char containing-sexp
)
6425 ;; CASE 16E: Closing a statement block? This catches
6426 ;; cases where it's preceded by a statement keyword,
6427 ;; which works even when used in an "invalid" context,
6428 ;; e.g. a macro argument.
6429 ((c-after-conditional)
6430 (c-backward-to-block-anchor lim
)
6431 (c-add-stmt-syntax 'block-close nil t nil
6433 ;; CASE 16A: closing a lambda defun or an in-expression
6434 ;; block? C.f. cases 4, 7B and 17E.
6435 ((setq placeholder
(c-looking-at-inexpr-block
6436 (c-safe-position containing-sexp paren-state
)
6438 (setq tmpsymbol
(if (eq (car placeholder
) 'inlambda
)
6441 (goto-char containing-sexp
)
6442 (back-to-indentation)
6443 (if (= containing-sexp
(point))
6444 (c-add-syntax tmpsymbol
(point))
6445 (goto-char (cdr placeholder
))
6446 (back-to-indentation)
6447 (c-add-stmt-syntax tmpsymbol nil t nil
6448 (c-most-enclosing-brace paren-state
(point))
6449 (c-whack-state-after (point) paren-state
))
6450 (if (/= (point) (cdr placeholder
))
6451 (c-add-syntax (car placeholder
)))))
6452 ;; CASE 16B: does this close an inline or a function in
6453 ;; a non-class declaration level block?
6454 ((setq placeholder
(c-search-uplist-for-classkey paren-state
))
6455 (c-backward-to-decl-anchor lim
)
6456 (back-to-indentation)
6458 (goto-char (aref placeholder
0))
6459 (looking-at c-other-decl-block-key
))
6460 (c-add-syntax 'defun-close
(point))
6461 (c-add-syntax 'inline-close
(point))))
6462 ;; CASE 16F: Can be a defun-close of a function declared
6463 ;; in a statement block, e.g. in Pike or when using gcc
6464 ;; extensions. Might also trigger it with some macros
6465 ;; followed by blocks, and this gives sane indentation
6466 ;; then too. Let it through to be handled below.
6467 ;; C.f. cases B.3 and 17G.
6468 ((and (not inenclosing-p
)
6471 (and (not (c-looking-at-bos))
6472 (eq (c-beginning-of-statement-1 lim nil nil t
) 'same
)
6473 (setq placeholder
(point)))))
6474 (back-to-indentation)
6475 (if (/= (point) containing-sexp
)
6476 (goto-char placeholder
))
6477 (c-add-stmt-syntax 'defun-close nil t nil
6479 ;; CASE 16C: if there an enclosing brace that hasn't
6480 ;; been narrowed out by a class, then this is a
6481 ;; block-close. C.f. case 17H.
6482 ((and (not inenclosing-p
) lim
)
6483 ;; If the block is preceded by a case/switch label on
6484 ;; the same line, we anchor at the first preceding label
6485 ;; at boi. The default handling in c-add-stmt-syntax is
6486 ;; really fixes it better, but we do like this to keep
6487 ;; the indentation compatible with version 5.28 and
6489 (while (and (/= (setq placeholder
(point)) (c-point 'boi
))
6490 (eq (c-beginning-of-statement-1 lim
) 'label
)))
6491 (goto-char placeholder
)
6492 (if (looking-at c-label-kwds-regexp
)
6493 (c-add-syntax 'block-close
(point))
6494 (goto-char containing-sexp
)
6495 ;; c-backward-to-block-anchor not necessary here; those
6496 ;; situations are handled in case 16E above.
6497 (c-add-stmt-syntax 'block-close nil t nil
6499 ;; CASE 16D: find out whether we're closing a top-level
6503 (narrow-to-region (point-min) indent-point
)
6504 (let ((decl (c-search-uplist-for-classkey (c-parse-state))))
6506 (c-add-class-syntax 'class-close decl paren-state
)
6507 (goto-char containing-sexp
)
6508 (c-backward-to-decl-anchor lim
)
6509 (back-to-indentation)
6510 (c-add-syntax 'defun-close
(point)))))
6512 ;; CASE 17: Statement or defun catchall.
6514 (goto-char indent-point
)
6515 ;; Back up statements until we find one that starts at boi.
6516 (while (let* ((prev-point (point))
6517 (last-step-type (c-beginning-of-statement-1
6519 (if (= (point) prev-point
)
6521 (setq step-type
(or step-type last-step-type
))
6523 (setq step-type last-step-type
)
6524 (/= (point) (c-point 'boi
)))))
6526 ;; CASE 17B: continued statement
6527 ((and (eq step-type
'same
)
6528 (/= (point) indent-point
))
6529 (c-add-stmt-syntax 'statement-cont nil nil nil
6530 containing-sexp paren-state
))
6531 ;; CASE 17A: After a case/default label?
6533 (while (and (eq step-type
'label
)
6534 (not (looking-at c-label-kwds-regexp
)))
6536 (c-beginning-of-statement-1 containing-sexp
)))
6537 (eq step-type
'label
))
6538 (c-add-stmt-syntax (if (eq char-after-ip ?
{)
6539 'statement-case-open
6540 'statement-case-intro
)
6541 nil t nil containing-sexp paren-state
))
6542 ;; CASE 17D: any old statement
6544 (while (eq step-type
'label
)
6546 (c-beginning-of-statement-1 containing-sexp
)))
6547 (eq step-type
'previous
))
6548 (c-add-stmt-syntax 'statement nil t nil
6549 containing-sexp paren-state
)
6550 (if (eq char-after-ip ?
{)
6551 (c-add-syntax 'block-open
)))
6552 ;; CASE 17I: Inside a substatement block.
6554 ;; The following tests are all based on containing-sexp.
6555 (goto-char containing-sexp
)
6556 ;; From here on we have the next containing sexp in lim.
6557 (setq lim
(c-most-enclosing-brace paren-state containing-sexp
))
6558 (c-after-conditional))
6559 (c-backward-to-block-anchor lim
)
6560 (c-add-stmt-syntax 'statement-block-intro nil t nil
6562 (if (eq char-after-ip ?
{)
6563 (c-add-syntax 'block-open
)))
6564 ;; CASE 17E: first statement in an in-expression block.
6565 ;; C.f. cases 4, 7B and 16A.
6566 ((setq placeholder
(c-looking-at-inexpr-block
6567 (c-safe-position containing-sexp paren-state
)
6569 (setq tmpsymbol
(if (eq (car placeholder
) 'inlambda
)
6571 'statement-block-intro
))
6572 (back-to-indentation)
6573 (if (= containing-sexp
(point))
6574 (c-add-syntax tmpsymbol
(point))
6575 (goto-char (cdr placeholder
))
6576 (back-to-indentation)
6577 (c-add-stmt-syntax tmpsymbol nil t nil
6578 (c-most-enclosing-brace c-state-cache
(point))
6579 (c-whack-state-after (point) paren-state
))
6580 (if (/= (point) (cdr placeholder
))
6581 (c-add-syntax (car placeholder
))))
6582 (if (eq char-after-ip ?
{)
6583 (c-add-syntax 'block-open
)))
6584 ;; CASE 17F: first statement in an inline, or first
6585 ;; statement in a top-level defun. we can tell this is it
6586 ;; if there are no enclosing braces that haven't been
6587 ;; narrowed out by a class (i.e. don't use bod here).
6591 (c-narrow-out-enclosing-class paren-state containing-sexp
)
6592 (not (c-most-enclosing-brace paren-state
))))
6593 (c-backward-to-decl-anchor lim
)
6594 (back-to-indentation)
6595 (c-add-syntax 'defun-block-intro
(point)))
6596 ;; CASE 17G: First statement in a function declared inside
6597 ;; a normal block. This can occur in Pike and with
6598 ;; e.g. the gcc extensions. Might also trigger it with
6599 ;; some macros followed by blocks, and this gives sane
6600 ;; indentation then too. C.f. cases B.3 and 16F.
6602 (and (not (c-looking-at-bos))
6603 (eq (c-beginning-of-statement-1 lim nil nil t
) 'same
)
6604 (setq placeholder
(point))))
6605 (back-to-indentation)
6606 (if (/= (point) containing-sexp
)
6607 (goto-char placeholder
))
6608 (c-add-stmt-syntax 'defun-block-intro nil t nil
6610 ;; CASE 17H: First statement in a block. C.f. case 16C.
6612 ;; If the block is preceded by a case/switch label on the
6613 ;; same line, we anchor at the first preceding label at
6614 ;; boi. The default handling in c-add-stmt-syntax is
6615 ;; really fixes it better, but we do like this to keep the
6616 ;; indentation compatible with version 5.28 and earlier.
6617 (while (and (/= (setq placeholder
(point)) (c-point 'boi
))
6618 (eq (c-beginning-of-statement-1 lim
) 'label
)))
6619 (goto-char placeholder
)
6620 (if (looking-at c-label-kwds-regexp
)
6621 (c-add-syntax 'statement-block-intro
(point))
6622 (goto-char containing-sexp
)
6623 ;; c-backward-to-block-anchor not necessary here; those
6624 ;; situations are handled in case 17I above.
6625 (c-add-stmt-syntax 'statement-block-intro nil t nil
6627 (if (eq char-after-ip ?
{)
6628 (c-add-syntax 'block-open
)))
6631 ;; now we need to look at any modifiers
6632 (goto-char indent-point
)
6633 (skip-chars-forward " \t")
6634 ;; are we looking at a comment only line?
6635 (when (and (looking-at c-comment-start-regexp
)
6636 (/= (c-forward-token-2 0 nil
(c-point 'eol
)) 0))
6637 (c-append-syntax 'comment-intro
))
6638 ;; we might want to give additional offset to friends (in C++).
6639 (when (and c-opt-friend-key
6640 (looking-at c-opt-friend-key
))
6641 (c-append-syntax 'friend
))
6643 ;; Set syntactic-relpos.
6644 (let ((p c-syntactic-context
))
6646 (if (integerp (car-safe (cdr-safe (car p
))))
6648 (setq syntactic-relpos
(car (cdr (car p
))))
6653 ;; Start of or a continuation of a preprocessor directive?
6654 (if (and macro-start
6655 (eq macro-start
(c-point 'boi
))
6656 (not (and (c-major-mode-is 'pike-mode
)
6657 (eq (char-after (1+ macro-start
)) ?
\"))))
6658 (c-append-syntax 'cpp-macro
)
6659 (when (and c-syntactic-indentation-in-macros macro-start
)
6662 (< syntactic-relpos macro-start
)
6664 (assq 'arglist-intro c-syntactic-context
)
6665 (assq 'arglist-cont c-syntactic-context
)
6666 (assq 'arglist-cont-nonempty c-syntactic-context
)
6667 (assq 'arglist-close c-syntactic-context
))))
6668 ;; If inside a cpp expression, i.e. anywhere in a
6669 ;; cpp directive except a #define body, we only let
6670 ;; through the syntactic analysis that is internal
6671 ;; in the expression. That means the arglist
6672 ;; elements, if they are anchored inside the cpp
6674 (setq c-syntactic-context nil
)
6675 (c-add-syntax 'cpp-macro-cont macro-start
))
6676 (when (and (eq macro-start syntactic-relpos
)
6677 (not (assq 'cpp-define-intro c-syntactic-context
))
6679 (goto-char macro-start
)
6680 (or (not (c-forward-to-cpp-define-body))
6681 (<= (point) (c-point 'boi indent-point
)))))
6682 ;; Inside a #define body and the syntactic analysis is
6683 ;; anchored on the start of the #define. In this case
6684 ;; we add cpp-define-intro to get the extra
6685 ;; indentation of the #define body.
6686 (c-add-syntax 'cpp-define-intro
)))))
6687 ;; return the syntax
6688 c-syntactic-context
))))
6691 ;; Indentation calculation.
6693 (defun c-evaluate-offset (offset langelem symbol
)
6694 ;; offset can be a number, a function, a variable, a list, or one of
6695 ;; the symbols + or -
6697 ((eq offset
'+) c-basic-offset
)
6698 ((eq offset
'-
) (- c-basic-offset
))
6699 ((eq offset
'++) (* 2 c-basic-offset
))
6700 ((eq offset
'--
) (* 2 (- c-basic-offset
)))
6701 ((eq offset
'*) (/ c-basic-offset
2))
6702 ((eq offset
'/) (/ (- c-basic-offset
) 2))
6703 ((numberp offset
) offset
)
6704 ((functionp offset
) (c-evaluate-offset
6706 (cons (car langelem
)
6707 (car-safe (cdr langelem
))))
6709 ((vectorp offset
) offset
)
6713 (while (and (not done
) offset
)
6714 (setq done
(c-evaluate-offset (car offset
) langelem symbol
)
6715 offset
(cdr offset
)))
6716 (if (and c-strict-syntax-p
(not done
))
6717 (c-benign-error "No offset found for syntactic symbol %s" symbol
))
6719 (t (symbol-value offset
))
6722 (defun c-calc-offset (langelem)
6723 ;; Get offset from LANGELEM which is a list beginning with the
6724 ;; syntactic symbol and followed by any analysis data it provides.
6725 ;; That data may be zero or more elements, but if at least one is
6726 ;; given then the first is the relpos (or nil). The symbol is
6727 ;; matched against `c-offsets-alist' and the offset calculated from
6728 ;; that is returned.
6729 (let* ((symbol (car langelem
))
6730 (match (assq symbol c-offsets-alist
))
6731 (offset (cdr-safe match
)))
6733 (setq offset
(c-evaluate-offset offset langelem symbol
))
6734 (if c-strict-syntax-p
6735 (c-benign-error "No offset found for syntactic symbol %s" symbol
))
6737 (if (vectorp offset
)
6739 (or (and (numberp offset
) offset
)
6740 (and (symbolp offset
) (symbol-value offset
))
6744 (defun c-get-offset (langelem)
6745 ;; This is a compatibility wrapper for `c-calc-offset' in case
6746 ;; someone is calling it directly. It takes an old style syntactic
6747 ;; element on the form (SYMBOL . RELPOS) and converts it to the new
6750 (c-calc-offset (list (car langelem
) (cdr langelem
)))
6751 (c-calc-offset langelem
)))
6753 (defun c-get-syntactic-indentation (langelems)
6754 ;; Calculate the syntactic indentation from a syntactic description
6755 ;; as returned by `c-guess-syntax'.
6757 ;; Note that topmost-intro always has a relpos at bol, for
6758 ;; historical reasons. It's often used together with other symbols
6759 ;; that has more sane positions. Since we always use the first
6760 ;; found relpos, we rely on that these other symbols always precede
6761 ;; topmost-intro in the LANGELEMS list.
6762 (let ((indent 0) anchor
)
6765 (let* ((c-syntactic-element (car langelems
))
6766 (res (c-calc-offset c-syntactic-element
)))
6769 ;; Got an absolute column that overrides any indentation
6770 ;; we've collected so far, but not the relative
6771 ;; indentation we might get for the nested structures
6772 ;; further down the langelems list.
6773 (setq indent
(elt res
0)
6774 anchor
(point-min)) ; A position at column 0.
6776 ;; Got a relative change of the current calculated
6778 (setq indent
(+ indent res
))
6780 ;; Use the anchor position from the first syntactic
6781 ;; element with one.
6783 (let ((relpos (car-safe (cdr (car langelems
)))))
6785 (setq anchor relpos
)))))
6787 (setq langelems
(cdr langelems
))))
6790 (+ indent
(save-excursion
6796 (cc-provide 'cc-engine
)
6798 ;;; cc-engine.el ends here