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