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