Merge from `emacs-23'.
[bpt/emacs.git] / lisp / progmodes / cc-engine.el
1 ;;; cc-engine.el --- core syntax guessing engine for CC mode
2
3 ;; Copyright (C) 1985, 1987, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
4 ;; 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010
5 ;; Free Software Foundation, Inc.
6
7 ;; Authors: 2001- Alan Mackenzie
8 ;; 1998- Martin Stjernholm
9 ;; 1992-1999 Barry A. Warsaw
10 ;; 1987 Dave Detlefs
11 ;; 1987 Stewart Clamen
12 ;; 1985 Richard M. Stallman
13 ;; Maintainer: bug-cc-mode@gnu.org
14 ;; Created: 22-Apr-1997 (split from cc-mode.el)
15 ;; Version: See cc-mode.el
16 ;; Keywords: c languages oop
17
18 ;; This file is part of GNU Emacs.
19
20 ;; GNU Emacs is free software: you can redistribute it and/or modify
21 ;; it under the terms of the GNU General Public License as published by
22 ;; the Free Software Foundation, either version 3 of the License, or
23 ;; (at your option) any later version.
24
25 ;; GNU Emacs is distributed in the hope that it will be useful,
26 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
27 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
28 ;; GNU General Public License for more details.
29
30 ;; You should have received a copy of the GNU General Public License
31 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
32
33 ;;; Commentary:
34
35 ;; The functions which have docstring documentation can be considered
36 ;; part of an API which other packages can use in CC Mode buffers.
37 ;; Otoh, undocumented functions and functions with the documentation
38 ;; in comments are considered purely internal and can change semantics
39 ;; or even disappear in the future.
40 ;;
41 ;; (This policy applies to CC Mode as a whole, not just this file. It
42 ;; probably also applies to many other Emacs packages, but here it's
43 ;; clearly spelled out.)
44
45 ;; Hidden buffer changes
46 ;;
47 ;; Various functions in CC Mode use text properties for caching and
48 ;; syntactic markup purposes, and those of them that might modify such
49 ;; properties but still don't modify the buffer in a visible way are
50 ;; said to do "hidden buffer changes". They should be used within
51 ;; `c-save-buffer-state' or a similar function that saves and restores
52 ;; buffer modifiedness, disables buffer change hooks, etc.
53 ;;
54 ;; Interactive functions are assumed to not do hidden buffer changes,
55 ;; except in the specific parts of them that do real changes.
56 ;;
57 ;; Lineup functions are assumed to do hidden buffer changes. They
58 ;; must not do real changes, though.
59 ;;
60 ;; All other functions that do hidden buffer changes have that noted
61 ;; in their doc string or comment.
62 ;;
63 ;; The intention with this system is to avoid wrapping every leaf
64 ;; function that do hidden buffer changes inside
65 ;; `c-save-buffer-state'. It should be used as near the top of the
66 ;; interactive functions as possible.
67 ;;
68 ;; Functions called during font locking are allowed to do hidden
69 ;; buffer changes since the font-lock package run them in a context
70 ;; similar to `c-save-buffer-state' (in fact, that function is heavily
71 ;; inspired by `save-buffer-state' in the font-lock package).
72
73 ;; Use of text properties
74 ;;
75 ;; CC Mode uses several text properties internally to mark up various
76 ;; positions, e.g. to improve speed and to eliminate glitches in
77 ;; interactive refontification.
78 ;;
79 ;; Note: This doc is for internal use only. Other packages should not
80 ;; assume that these text properties are used as described here.
81 ;;
82 ;; 'category
83 ;; Used for "indirection". With its help, some other property can
84 ;; be cheaply and easily switched on or off everywhere it occurs.
85 ;;
86 ;; 'syntax-table
87 ;; Used to modify the syntax of some characters. It is used to
88 ;; mark the "<" and ">" of angle bracket parens with paren syntax, and
89 ;; to "hide" obtrusive characters in preprocessor lines.
90 ;;
91 ;; This property is used on single characters and is therefore
92 ;; always treated as front and rear nonsticky (or start and end open
93 ;; in XEmacs vocabulary). It's therefore installed on
94 ;; `text-property-default-nonsticky' if that variable exists (Emacs
95 ;; >= 21).
96 ;;
97 ;; 'c-is-sws and 'c-in-sws
98 ;; Used by `c-forward-syntactic-ws' and `c-backward-syntactic-ws' to
99 ;; speed them up. See the comment blurb before `c-put-is-sws'
100 ;; below for further details.
101 ;;
102 ;; 'c-type
103 ;; This property is used on single characters to mark positions with
104 ;; special syntactic relevance of various sorts. Its primary use is
105 ;; to avoid glitches when multiline constructs are refontified
106 ;; interactively (on font lock decoration level 3). It's cleared in
107 ;; a region before it's fontified and is then put on relevant chars
108 ;; in that region as they are encountered during the fontification.
109 ;; The value specifies the kind of position:
110 ;;
111 ;; 'c-decl-arg-start
112 ;; Put on the last char of the token preceding each declaration
113 ;; inside a declaration style arglist (typically in a function
114 ;; prototype).
115 ;;
116 ;; 'c-decl-end
117 ;; Put on the last char of the token preceding a declaration.
118 ;; This is used in cases where declaration boundaries can't be
119 ;; recognized simply by looking for a token like ";" or "}".
120 ;; `c-type-decl-end-used' must be set if this is used (see also
121 ;; `c-find-decl-spots').
122 ;;
123 ;; 'c-<>-arg-sep
124 ;; Put on the commas that separate arguments in angle bracket
125 ;; arglists like C++ template arglists.
126 ;;
127 ;; 'c-decl-id-start and 'c-decl-type-start
128 ;; Put on the last char of the token preceding each declarator
129 ;; in the declarator list of a declaration. They are also used
130 ;; between the identifiers cases like enum declarations.
131 ;; 'c-decl-type-start is used when the declarators are types,
132 ;; 'c-decl-id-start otherwise.
133 ;;
134 ;; 'c-awk-NL-prop
135 ;; Used in AWK mode to mark the various kinds of newlines. See
136 ;; cc-awk.el.
137
138 ;;; Code:
139
140 (eval-when-compile
141 (let ((load-path
142 (if (and (boundp 'byte-compile-dest-file)
143 (stringp byte-compile-dest-file))
144 (cons (file-name-directory byte-compile-dest-file) load-path)
145 load-path)))
146 (load "cc-bytecomp" nil t)))
147
148 (cc-require 'cc-defs)
149 (cc-require-when-compile 'cc-langs)
150 (cc-require 'cc-vars)
151
152 ;; Silence the compiler.
153 (cc-bytecomp-defun buffer-syntactic-context) ; XEmacs
154
155 \f
156 ;; Make declarations for all the `c-lang-defvar' variables in cc-langs.
157
158 (defmacro c-declare-lang-variables ()
159 `(progn
160 ,@(apply 'nconc
161 (mapcar (lambda (init)
162 `(,(if (elt init 2)
163 `(defvar ,(car init) nil ,(elt init 2))
164 `(defvar ,(car init) nil))
165 (make-variable-buffer-local ',(car init))))
166 (cdr c-lang-variable-inits)))))
167 (c-declare-lang-variables)
168
169 \f
170 ;;; Internal state variables.
171
172 ;; Internal state of hungry delete key feature
173 (defvar c-hungry-delete-key nil)
174 (make-variable-buffer-local 'c-hungry-delete-key)
175
176 ;; The electric flag (toggled by `c-toggle-electric-state').
177 ;; If t, electric actions (like automatic reindentation, and (if
178 ;; c-auto-newline is also set) auto newlining) will happen when an electric
179 ;; key like `{' is pressed (or an electric keyword like `else').
180 (defvar c-electric-flag t)
181 (make-variable-buffer-local 'c-electric-flag)
182
183 ;; Internal state of auto newline feature.
184 (defvar c-auto-newline nil)
185 (make-variable-buffer-local 'c-auto-newline)
186
187 ;; Included in the mode line to indicate the active submodes.
188 ;; (defvar c-submode-indicators nil)
189 ;; (make-variable-buffer-local 'c-submode-indicators)
190
191 (defun c-calculate-state (arg prevstate)
192 ;; Calculate the new state of PREVSTATE, t or nil, based on arg. If
193 ;; arg is nil or zero, toggle the state. If arg is negative, turn
194 ;; the state off, and if arg is positive, turn the state on
195 (if (or (not arg)
196 (zerop (setq arg (prefix-numeric-value arg))))
197 (not prevstate)
198 (> arg 0)))
199
200 ;; Dynamically bound cache for `c-in-literal'.
201 (defvar c-in-literal-cache t)
202
203 \f
204 ;; Basic handling of preprocessor directives.
205
206 ;; This is a dynamically bound cache used together with
207 ;; `c-query-macro-start' and `c-query-and-set-macro-start'. It only
208 ;; works as long as point doesn't cross a macro boundary.
209 (defvar c-macro-start 'unknown)
210
211 (defsubst c-query-and-set-macro-start ()
212 (if (symbolp c-macro-start)
213 (setq c-macro-start (save-excursion
214 (c-save-buffer-state ()
215 (and (c-beginning-of-macro)
216 (point)))))
217 c-macro-start))
218
219 (defsubst c-query-macro-start ()
220 (if (symbolp c-macro-start)
221 (save-excursion
222 (c-save-buffer-state ()
223 (and (c-beginning-of-macro)
224 (point))))
225 c-macro-start))
226
227 (defun c-beginning-of-macro (&optional lim)
228 "Go to the beginning of a preprocessor directive.
229 Leave point at the beginning of the directive and return t if in one,
230 otherwise return nil and leave point unchanged.
231
232 Note that this function might do hidden buffer changes. See the
233 comment at the start of cc-engine.el for more info."
234 (when c-opt-cpp-prefix
235 (let ((here (point)))
236 (save-restriction
237 (if lim (narrow-to-region lim (point-max)))
238 (beginning-of-line)
239 (while (eq (char-before (1- (point))) ?\\)
240 (forward-line -1))
241 (back-to-indentation)
242 (if (and (<= (point) here)
243 (looking-at c-opt-cpp-start))
244 t
245 (goto-char here)
246 nil)))))
247
248 (defun c-end-of-macro ()
249 "Go to the end of a preprocessor directive.
250 More accurately, move the point to the end of the closest following
251 line that doesn't end with a line continuation backslash - no check is
252 done that the point is inside a cpp directive to begin with.
253
254 Note that this function might do hidden buffer changes. See the
255 comment at the start of cc-engine.el for more info."
256 (while (progn
257 (end-of-line)
258 (when (and (eq (char-before) ?\\)
259 (not (eobp)))
260 (forward-char)
261 t))))
262
263 (defun c-syntactic-end-of-macro ()
264 ;; Go to the end of a CPP directive, or a "safe" pos just before.
265 ;;
266 ;; This is normally the end of the next non-escaped line. A "safe"
267 ;; position is one not within a string or comment. (The EOL on a line
268 ;; comment is NOT "safe").
269 ;;
270 ;; This function must only be called from the beginning of a CPP construct.
271 ;;
272 ;; Note that this function might do hidden buffer changes. See the comment
273 ;; at the start of cc-engine.el for more info.
274 (let* ((here (point))
275 (there (progn (c-end-of-macro) (point)))
276 (s (parse-partial-sexp here there)))
277 (while (and (or (nth 3 s) ; in a string
278 (nth 4 s)) ; in a comment (maybe at end of line comment)
279 (> there here)) ; No infinite loops, please.
280 (setq there (1- (nth 8 s)))
281 (setq s (parse-partial-sexp here there)))
282 (point)))
283
284 (defun c-forward-over-cpp-define-id ()
285 ;; Assuming point is at the "#" that introduces a preprocessor
286 ;; directive, it's moved forward to the end of the identifier which is
287 ;; "#define"d (or whatever c-opt-cpp-macro-define specifies). Non-nil
288 ;; is returned in this case, in all other cases nil is returned and
289 ;; point isn't moved.
290 ;;
291 ;; This function might do hidden buffer changes.
292 (when (and c-opt-cpp-macro-define-id
293 (looking-at c-opt-cpp-macro-define-id))
294 (goto-char (match-end 0))))
295
296 (defun c-forward-to-cpp-define-body ()
297 ;; Assuming point is at the "#" that introduces a preprocessor
298 ;; directive, it's moved forward to the start of the definition body
299 ;; if it's a "#define" (or whatever c-opt-cpp-macro-define
300 ;; specifies). Non-nil is returned in this case, in all other cases
301 ;; nil is returned and point isn't moved.
302 ;;
303 ;; This function might do hidden buffer changes.
304 (when (and c-opt-cpp-macro-define-start
305 (looking-at c-opt-cpp-macro-define-start)
306 (not (= (match-end 0) (c-point 'eol))))
307 (goto-char (match-end 0))))
308
309 \f
310 ;;; Basic utility functions.
311
312 (defun c-syntactic-content (from to paren-level)
313 ;; Return the given region as a string where all syntactic
314 ;; whitespace is removed or, where necessary, replaced with a single
315 ;; space. If PAREN-LEVEL is given then all parens in the region are
316 ;; collapsed to "()", "[]" etc.
317 ;;
318 ;; This function might do hidden buffer changes.
319
320 (save-excursion
321 (save-restriction
322 (narrow-to-region from to)
323 (goto-char from)
324 (let* ((parts (list nil)) (tail parts) pos in-paren)
325
326 (while (re-search-forward c-syntactic-ws-start to t)
327 (goto-char (setq pos (match-beginning 0)))
328 (c-forward-syntactic-ws)
329 (if (= (point) pos)
330 (forward-char)
331
332 (when paren-level
333 (save-excursion
334 (setq in-paren (= (car (parse-partial-sexp from pos 1)) 1)
335 pos (point))))
336
337 (if (and (> pos from)
338 (< (point) to)
339 (looking-at "\\w\\|\\s_")
340 (save-excursion
341 (goto-char (1- pos))
342 (looking-at "\\w\\|\\s_")))
343 (progn
344 (setcdr tail (list (buffer-substring-no-properties from pos)
345 " "))
346 (setq tail (cddr tail)))
347 (setcdr tail (list (buffer-substring-no-properties from pos)))
348 (setq tail (cdr tail)))
349
350 (when in-paren
351 (when (= (car (parse-partial-sexp pos to -1)) -1)
352 (setcdr tail (list (buffer-substring-no-properties
353 (1- (point)) (point))))
354 (setq tail (cdr tail))))
355
356 (setq from (point))))
357
358 (setcdr tail (list (buffer-substring-no-properties from to)))
359 (apply 'concat (cdr parts))))))
360
361 (defun c-shift-line-indentation (shift-amt)
362 ;; Shift the indentation of the current line with the specified
363 ;; amount (positive inwards). The buffer is modified only if
364 ;; SHIFT-AMT isn't equal to zero.
365 (let ((pos (- (point-max) (point)))
366 (c-macro-start c-macro-start)
367 tmp-char-inserted)
368 (if (zerop shift-amt)
369 nil
370 ;; If we're on an empty line inside a macro, we take the point
371 ;; to be at the current indentation and shift it to the
372 ;; appropriate column. This way we don't treat the extra
373 ;; whitespace out to the line continuation as indentation.
374 (when (and (c-query-and-set-macro-start)
375 (looking-at "[ \t]*\\\\$")
376 (save-excursion
377 (skip-chars-backward " \t")
378 (bolp)))
379 (insert ?x)
380 (backward-char)
381 (setq tmp-char-inserted t))
382 (unwind-protect
383 (let ((col (current-indentation)))
384 (delete-region (c-point 'bol) (c-point 'boi))
385 (beginning-of-line)
386 (indent-to (+ col shift-amt)))
387 (when tmp-char-inserted
388 (delete-char 1))))
389 ;; If initial point was within line's indentation and we're not on
390 ;; a line with a line continuation in a macro, position after the
391 ;; indentation. Else stay at same point in text.
392 (if (and (< (point) (c-point 'boi))
393 (not tmp-char-inserted))
394 (back-to-indentation)
395 (if (> (- (point-max) pos) (point))
396 (goto-char (- (point-max) pos))))))
397
398 (defsubst c-keyword-sym (keyword)
399 ;; Return non-nil if the string KEYWORD is a known keyword. More
400 ;; precisely, the value is the symbol for the keyword in
401 ;; `c-keywords-obarray'.
402 (intern-soft keyword c-keywords-obarray))
403
404 (defsubst c-keyword-member (keyword-sym lang-constant)
405 ;; Return non-nil if the symbol KEYWORD-SYM, as returned by
406 ;; `c-keyword-sym', is a member of LANG-CONSTANT, which is the name
407 ;; of a language constant that ends with "-kwds". If KEYWORD-SYM is
408 ;; nil then the result is nil.
409 (get keyword-sym lang-constant))
410
411 ;; String syntax chars, suitable for skip-syntax-(forward|backward).
412 (defconst c-string-syntax (if (memq 'gen-string-delim c-emacs-features)
413 "\"|"
414 "\""))
415
416 ;; Regexp matching string limit syntax.
417 (defconst c-string-limit-regexp (if (memq 'gen-string-delim c-emacs-features)
418 "\\s\"\\|\\s|"
419 "\\s\""))
420
421 ;; Regexp matching WS followed by string limit syntax.
422 (defconst c-ws*-string-limit-regexp
423 (concat "[ \t]*\\(" c-string-limit-regexp "\\)"))
424
425 ;; Holds formatted error strings for the few cases where parse errors
426 ;; are reported.
427 (defvar c-parsing-error nil)
428 (make-variable-buffer-local 'c-parsing-error)
429
430 (defun c-echo-parsing-error (&optional quiet)
431 (when (and c-report-syntactic-errors c-parsing-error (not quiet))
432 (c-benign-error "%s" c-parsing-error))
433 c-parsing-error)
434
435 ;; Faces given to comments and string literals. This is used in some
436 ;; situations to speed up recognition; it isn't mandatory that font
437 ;; locking is in use. This variable is extended with the face in
438 ;; `c-doc-face-name' when fontification is activated in cc-fonts.el.
439 (defvar c-literal-faces
440 (append '(font-lock-comment-face font-lock-string-face)
441 (when (facep 'font-lock-comment-delimiter-face)
442 ;; New in Emacs 22.
443 '(font-lock-comment-delimiter-face))))
444
445 (defsubst c-put-c-type-property (pos value)
446 ;; Put a c-type property with the given value at POS.
447 (c-put-char-property pos 'c-type value))
448
449 (defun c-clear-c-type-property (from to value)
450 ;; Remove all occurrences of the c-type property that has the given
451 ;; value in the region between FROM and TO. VALUE is assumed to not
452 ;; be nil.
453 ;;
454 ;; Note: This assumes that c-type is put on single chars only; it's
455 ;; very inefficient if matching properties cover large regions.
456 (save-excursion
457 (goto-char from)
458 (while (progn
459 (when (eq (get-text-property (point) 'c-type) value)
460 (c-clear-char-property (point) 'c-type))
461 (goto-char (next-single-property-change (point) 'c-type nil to))
462 (< (point) to)))))
463
464 \f
465 ;; Some debug tools to visualize various special positions. This
466 ;; debug code isn't as portable as the rest of CC Mode.
467
468 (cc-bytecomp-defun overlays-in)
469 (cc-bytecomp-defun overlay-get)
470 (cc-bytecomp-defun overlay-start)
471 (cc-bytecomp-defun overlay-end)
472 (cc-bytecomp-defun delete-overlay)
473 (cc-bytecomp-defun overlay-put)
474 (cc-bytecomp-defun make-overlay)
475
476 (defun c-debug-add-face (beg end face)
477 (c-save-buffer-state ((overlays (overlays-in beg end)) overlay)
478 (while overlays
479 (setq overlay (car overlays)
480 overlays (cdr overlays))
481 (when (eq (overlay-get overlay 'face) face)
482 (setq beg (min beg (overlay-start overlay))
483 end (max end (overlay-end overlay)))
484 (delete-overlay overlay)))
485 (overlay-put (make-overlay beg end) 'face face)))
486
487 (defun c-debug-remove-face (beg end face)
488 (c-save-buffer-state ((overlays (overlays-in beg end)) overlay
489 (ol-beg beg) (ol-end end))
490 (while overlays
491 (setq overlay (car overlays)
492 overlays (cdr overlays))
493 (when (eq (overlay-get overlay 'face) face)
494 (setq ol-beg (min ol-beg (overlay-start overlay))
495 ol-end (max ol-end (overlay-end overlay)))
496 (delete-overlay overlay)))
497 (when (< ol-beg beg)
498 (overlay-put (make-overlay ol-beg beg) 'face face))
499 (when (> ol-end end)
500 (overlay-put (make-overlay end ol-end) 'face face))))
501
502 \f
503 ;; `c-beginning-of-statement-1' and accompanying stuff.
504
505 ;; KLUDGE ALERT: c-maybe-labelp is used to pass information between
506 ;; c-crosses-statement-barrier-p and c-beginning-of-statement-1. A
507 ;; better way should be implemented, but this will at least shut up
508 ;; the byte compiler.
509 (defvar c-maybe-labelp)
510
511 ;; New awk-compatible version of c-beginning-of-statement-1, ACM 2002/6/22
512
513 ;; Macros used internally in c-beginning-of-statement-1 for the
514 ;; automaton actions.
515 (defmacro c-bos-push-state ()
516 '(setq stack (cons (cons state saved-pos)
517 stack)))
518 (defmacro c-bos-pop-state (&optional do-if-done)
519 `(if (setq state (car (car stack))
520 saved-pos (cdr (car stack))
521 stack (cdr stack))
522 t
523 ,do-if-done
524 (throw 'loop nil)))
525 (defmacro c-bos-pop-state-and-retry ()
526 '(throw 'loop (setq state (car (car stack))
527 saved-pos (cdr (car stack))
528 ;; Throw nil if stack is empty, else throw non-nil.
529 stack (cdr stack))))
530 (defmacro c-bos-save-pos ()
531 '(setq saved-pos (vector pos tok ptok pptok)))
532 (defmacro c-bos-restore-pos ()
533 '(unless (eq (elt saved-pos 0) start)
534 (setq pos (elt saved-pos 0)
535 tok (elt saved-pos 1)
536 ptok (elt saved-pos 2)
537 pptok (elt saved-pos 3))
538 (goto-char pos)
539 (setq sym nil)))
540 (defmacro c-bos-save-error-info (missing got)
541 `(setq saved-pos (vector pos ,missing ,got)))
542 (defmacro c-bos-report-error ()
543 '(unless noerror
544 (setq c-parsing-error
545 (format "No matching `%s' found for `%s' on line %d"
546 (elt saved-pos 1)
547 (elt saved-pos 2)
548 (1+ (count-lines (point-min)
549 (c-point 'bol (elt saved-pos 0))))))))
550
551 (defun c-beginning-of-statement-1 (&optional lim ignore-labels
552 noerror comma-delim)
553 "Move to the start of the current statement or declaration, or to
554 the previous one if already at the beginning of one. Only
555 statements/declarations on the same level are considered, i.e. don't
556 move into or out of sexps (not even normal expression parentheses).
557
558 If point is already at the earliest statement within braces or parens,
559 this function doesn't move back into any whitespace preceding it; it
560 returns 'same in this case.
561
562 Stop at statement continuation tokens like \"else\", \"catch\",
563 \"finally\" and the \"while\" in \"do ... while\" if the start point
564 is within the continuation. If starting at such a token, move to the
565 corresponding statement start. If at the beginning of a statement,
566 move to the closest containing statement if there is any. This might
567 also stop at a continuation clause.
568
569 Labels are treated as part of the following statements if
570 IGNORE-LABELS is non-nil. (FIXME: Doesn't work if we stop at a known
571 statement start keyword.) Otherwise, each label is treated as a
572 separate statement.
573
574 Macros are ignored \(i.e. skipped over) unless point is within one, in
575 which case the content of the macro is treated as normal code. Aside
576 from any normal statement starts found in it, stop at the first token
577 of the content in the macro, i.e. the expression of an \"#if\" or the
578 start of the definition in a \"#define\". Also stop at start of
579 macros before leaving them.
580
581 Return:
582 'label if stopped at a label or \"case...:\" or \"default:\";
583 'same if stopped at the beginning of the current statement;
584 'up if stepped to a containing statement;
585 'previous if stepped to a preceding statement;
586 'beginning if stepped from a statement continuation clause to
587 its start clause; or
588 'macro if stepped to a macro start.
589 Note that 'same and not 'label is returned if stopped at the same
590 label without crossing the colon character.
591
592 LIM may be given to limit the search. If the search hits the limit,
593 point will be left at the closest following token, or at the start
594 position if that is less ('same is returned in this case).
595
596 NOERROR turns off error logging to `c-parsing-error'.
597
598 Normally only ';' and virtual semicolons are considered to delimit
599 statements, but if COMMA-DELIM is non-nil then ',' is treated
600 as a delimiter too.
601
602 Note that this function might do hidden buffer changes. See the
603 comment at the start of cc-engine.el for more info."
604
605 ;; The bulk of this function is a pushdown automaton that looks at statement
606 ;; boundaries and the tokens (such as "while") in c-opt-block-stmt-key. Its
607 ;; purpose is to keep track of nested statements, ensuring that such
608 ;; statements are skipped over in their entirety (somewhat akin to what C-M-p
609 ;; does with nested braces/brackets/parentheses).
610 ;;
611 ;; Note: The position of a boundary is the following token.
612 ;;
613 ;; Beginning with the current token (the one following point), move back one
614 ;; sexp at a time (where a sexp is, more or less, either a token or the
615 ;; entire contents of a brace/bracket/paren pair). Each time a statement
616 ;; boundary is crossed or a "while"-like token is found, update the state of
617 ;; the PDA. Stop at the beginning of a statement when the stack (holding
618 ;; nested statement info) is empty and the position has been moved.
619 ;;
620 ;; The following variables constitute the PDA:
621 ;;
622 ;; sym: This is either the "while"-like token (e.g. 'for) we've just
623 ;; scanned back over, 'boundary if we've just gone back over a
624 ;; statement boundary, or nil otherwise.
625 ;; state: takes one of the values (nil else else-boundary while
626 ;; while-boundary catch catch-boundary).
627 ;; nil means "no "while"-like token yet scanned".
628 ;; 'else, for example, means "just gone back over an else".
629 ;; 'else-boundary means "just gone back over a statement boundary
630 ;; immediately after having gone back over an else".
631 ;; saved-pos: A vector of either saved positions (tok ptok pptok, etc.) or
632 ;; of error reporting information.
633 ;; stack: The stack onto which the PDA pushes its state. Each entry
634 ;; consists of a saved value of state and saved-pos. An entry is
635 ;; pushed when we move back over a "continuation" token (e.g. else)
636 ;; and popped when we encounter the corresponding opening token
637 ;; (e.g. if).
638 ;;
639 ;;
640 ;; The following diagram briefly outlines the PDA.
641 ;;
642 ;; Common state:
643 ;; "else": Push state, goto state `else'.
644 ;; "while": Push state, goto state `while'.
645 ;; "catch" or "finally": Push state, goto state `catch'.
646 ;; boundary: Pop state.
647 ;; other: Do nothing special.
648 ;;
649 ;; State `else':
650 ;; boundary: Goto state `else-boundary'.
651 ;; other: Error, pop state, retry token.
652 ;;
653 ;; State `else-boundary':
654 ;; "if": Pop state.
655 ;; boundary: Error, pop state.
656 ;; other: See common state.
657 ;;
658 ;; State `while':
659 ;; boundary: Save position, goto state `while-boundary'.
660 ;; other: Pop state, retry token.
661 ;;
662 ;; State `while-boundary':
663 ;; "do": Pop state.
664 ;; boundary: Restore position if it's not at start, pop state. [*see below]
665 ;; other: See common state.
666 ;;
667 ;; State `catch':
668 ;; boundary: Goto state `catch-boundary'.
669 ;; other: Error, pop state, retry token.
670 ;;
671 ;; State `catch-boundary':
672 ;; "try": Pop state.
673 ;; "catch": Goto state `catch'.
674 ;; boundary: Error, pop state.
675 ;; other: See common state.
676 ;;
677 ;; [*] In the `while-boundary' state, we had pushed a 'while state, and were
678 ;; searching for a "do" which would have opened a do-while. If we didn't
679 ;; find it, we discard the analysis done since the "while", go back to this
680 ;; token in the buffer and restart the scanning there, this time WITHOUT
681 ;; pushing the 'while state onto the stack.
682 ;;
683 ;; In addition to the above there is some special handling of labels
684 ;; and macros.
685
686 (let ((case-fold-search nil)
687 (start (point))
688 macro-start
689 (delims (if comma-delim '(?\; ?,) '(?\;)))
690 (c-stmt-delim-chars (if comma-delim
691 c-stmt-delim-chars-with-comma
692 c-stmt-delim-chars))
693 c-in-literal-cache c-maybe-labelp after-case:-pos saved
694 ;; Current position.
695 pos
696 ;; Position of last stmt boundary character (e.g. ;).
697 boundary-pos
698 ;; The position of the last sexp or bound that follows the
699 ;; first found colon, i.e. the start of the nonlabel part of
700 ;; the statement. It's `start' if a colon is found just after
701 ;; the start.
702 after-labels-pos
703 ;; Like `after-labels-pos', but the first such position inside
704 ;; a label, i.e. the start of the last label before the start
705 ;; of the nonlabel part of the statement.
706 last-label-pos
707 ;; The last position where a label is possible provided the
708 ;; statement started there. It's nil as long as no invalid
709 ;; label content has been found (according to
710 ;; `c-nonlabel-token-key'. It's `start' if no valid label
711 ;; content was found in the label. Note that we might still
712 ;; regard it a label if it starts with `c-label-kwds'.
713 label-good-pos
714 ;; Symbol just scanned back over (e.g. 'while or 'boundary).
715 ;; See above.
716 sym
717 ;; Current state in the automaton. See above.
718 state
719 ;; Current saved positions. See above.
720 saved-pos
721 ;; Stack of conses (state . saved-pos).
722 stack
723 ;; Regexp which matches "for", "if", etc.
724 (cond-key (or c-opt-block-stmt-key
725 "\\<\\>")) ; Matches nothing.
726 ;; Return value.
727 (ret 'same)
728 ;; Positions of the last three sexps or bounds we've stopped at.
729 tok ptok pptok)
730
731 (save-restriction
732 (if lim (narrow-to-region lim (point-max)))
733
734 (if (save-excursion
735 (and (c-beginning-of-macro)
736 (/= (point) start)))
737 (setq macro-start (point)))
738
739 ;; Try to skip back over unary operator characters, to register
740 ;; that we've moved.
741 (while (progn
742 (setq pos (point))
743 (c-backward-syntactic-ws)
744 ;; Protect post-++/-- operators just before a virtual semicolon.
745 (and (not (c-at-vsemi-p))
746 (/= (skip-chars-backward "-+!*&~@`#") 0))))
747
748 ;; Skip back over any semicolon here. If it was a bare semicolon, we're
749 ;; done. Later on we ignore the boundaries for statements that don't
750 ;; contain any sexp. The only thing that is affected is that the error
751 ;; checking is a little less strict, and we really don't bother.
752 (if (and (memq (char-before) delims)
753 (progn (forward-char -1)
754 (setq saved (point))
755 (c-backward-syntactic-ws)
756 (or (memq (char-before) delims)
757 (memq (char-before) '(?: nil))
758 (eq (char-syntax (char-before)) ?\()
759 (c-at-vsemi-p))))
760 (setq ret 'previous
761 pos saved)
762
763 ;; Begin at start and not pos to detect macros if we stand
764 ;; directly after the #.
765 (goto-char start)
766 (if (looking-at "\\<\\|\\W")
767 ;; Record this as the first token if not starting inside it.
768 (setq tok start))
769
770 ;; The following while loop goes back one sexp (balanced parens,
771 ;; etc. with contents, or symbol or suchlike) each iteration. This
772 ;; movement is accomplished with a call to scan-sexps approx 130 lines
773 ;; below.
774 (while
775 (catch 'loop ;; Throw nil to break, non-nil to continue.
776 (cond
777 ((save-excursion
778 (and macro-start ; Always NIL for AWK.
779 (progn (skip-chars-backward " \t")
780 (eq (char-before) ?#))
781 (progn (setq saved (1- (point)))
782 (beginning-of-line)
783 (not (eq (char-before (1- (point))) ?\\)))
784 (looking-at c-opt-cpp-start)
785 (progn (skip-chars-forward " \t")
786 (eq (point) saved))))
787 (goto-char saved)
788 (if (and (c-forward-to-cpp-define-body)
789 (progn (c-forward-syntactic-ws start)
790 (< (point) start)))
791 ;; Stop at the first token in the content of the macro.
792 (setq pos (point)
793 ignore-labels t) ; Avoid the label check on exit.
794 (setq pos saved
795 ret 'macro
796 ignore-labels t))
797 (throw 'loop nil))
798
799 ;; Do a round through the automaton if we've just passed a
800 ;; statement boundary or passed a "while"-like token.
801 ((or sym
802 (and (looking-at cond-key)
803 (setq sym (intern (match-string 1)))))
804
805 (when (and (< pos start) (null stack))
806 (throw 'loop nil))
807
808 ;; The PDA state handling.
809 ;;
810 ;; Refer to the description of the PDA in the opening
811 ;; comments. In the following OR form, the first leaf
812 ;; attempts to handles one of the specific actions detailed
813 ;; (e.g., finding token "if" whilst in state `else-boundary').
814 ;; We drop through to the second leaf (which handles common
815 ;; state) if no specific handler is found in the first cond.
816 ;; If a parsing error is detected (e.g. an "else" with no
817 ;; preceding "if"), we throw to the enclosing catch.
818 ;;
819 ;; Note that the (eq state 'else) means
820 ;; "we've just passed an else", NOT "we're looking for an
821 ;; else".
822 (or (cond
823 ((eq state 'else)
824 (if (eq sym 'boundary)
825 (setq state 'else-boundary)
826 (c-bos-report-error)
827 (c-bos-pop-state-and-retry)))
828
829 ((eq state 'else-boundary)
830 (cond ((eq sym 'if)
831 (c-bos-pop-state (setq ret 'beginning)))
832 ((eq sym 'boundary)
833 (c-bos-report-error)
834 (c-bos-pop-state))))
835
836 ((eq state 'while)
837 (if (and (eq sym 'boundary)
838 ;; Since this can cause backtracking we do a
839 ;; little more careful analysis to avoid it:
840 ;; If there's a label in front of the while
841 ;; it can't be part of a do-while.
842 (not after-labels-pos))
843 (progn (c-bos-save-pos)
844 (setq state 'while-boundary))
845 (c-bos-pop-state-and-retry))) ; Can't be a do-while
846
847 ((eq state 'while-boundary)
848 (cond ((eq sym 'do)
849 (c-bos-pop-state (setq ret 'beginning)))
850 ((eq sym 'boundary) ; isn't a do-while
851 (c-bos-restore-pos) ; the position of the while
852 (c-bos-pop-state)))) ; no longer searching for do.
853
854 ((eq state 'catch)
855 (if (eq sym 'boundary)
856 (setq state 'catch-boundary)
857 (c-bos-report-error)
858 (c-bos-pop-state-and-retry)))
859
860 ((eq state 'catch-boundary)
861 (cond
862 ((eq sym 'try)
863 (c-bos-pop-state (setq ret 'beginning)))
864 ((eq sym 'catch)
865 (setq state 'catch))
866 ((eq sym 'boundary)
867 (c-bos-report-error)
868 (c-bos-pop-state)))))
869
870 ;; This is state common. We get here when the previous
871 ;; cond statement found no particular state handler.
872 (cond ((eq sym 'boundary)
873 ;; If we have a boundary at the start
874 ;; position we push a frame to go to the
875 ;; previous statement.
876 (if (>= pos start)
877 (c-bos-push-state)
878 (c-bos-pop-state)))
879 ((eq sym 'else)
880 (c-bos-push-state)
881 (c-bos-save-error-info 'if 'else)
882 (setq state 'else))
883 ((eq sym 'while)
884 ;; Is this a real while, or a do-while?
885 ;; The next `when' triggers unless we are SURE that
886 ;; the `while' is not the tailend of a `do-while'.
887 (when (or (not pptok)
888 (memq (char-after pptok) delims)
889 ;; The following kludge is to prevent
890 ;; infinite recursion when called from
891 ;; c-awk-after-if-for-while-condition-p,
892 ;; or the like.
893 (and (eq (point) start)
894 (c-vsemi-status-unknown-p))
895 (c-at-vsemi-p pptok))
896 ;; Since this can cause backtracking we do a
897 ;; little more careful analysis to avoid it: If
898 ;; the while isn't followed by a (possibly
899 ;; virtual) semicolon it can't be a do-while.
900 (c-bos-push-state)
901 (setq state 'while)))
902 ((memq sym '(catch finally))
903 (c-bos-push-state)
904 (c-bos-save-error-info 'try sym)
905 (setq state 'catch))))
906
907 (when c-maybe-labelp
908 ;; We're either past a statement boundary or at the
909 ;; start of a statement, so throw away any label data
910 ;; for the previous one.
911 (setq after-labels-pos nil
912 last-label-pos nil
913 c-maybe-labelp nil))))
914
915 ;; Step to the previous sexp, but not if we crossed a
916 ;; boundary, since that doesn't consume an sexp.
917 (if (eq sym 'boundary)
918 (setq ret 'previous)
919
920 ;; HERE IS THE SINGLE PLACE INSIDE THE PDA LOOP WHERE WE MOVE
921 ;; BACKWARDS THROUGH THE SOURCE.
922
923 ;; This is typically fast with the caching done by
924 ;; c-(backward|forward)-sws.
925 (c-backward-syntactic-ws)
926
927 (let ((before-sws-pos (point))
928 ;; Set as long as we have to continue jumping by sexps.
929 ;; It's the position to use as end in the next round.
930 sexp-loop-continue-pos
931 ;; The end position of the area to search for statement
932 ;; barriers in this round.
933 (sexp-loop-end-pos pos))
934
935 ;; The following while goes back one sexp per iteration.
936 (while
937 (progn
938 (unless (c-safe (c-backward-sexp) t)
939 ;; Give up if we hit an unbalanced block. Since the
940 ;; stack won't be empty the code below will report a
941 ;; suitable error.
942 (throw 'loop nil))
943
944 ;; Check if the sexp movement crossed a statement or
945 ;; declaration boundary. But first modify the point
946 ;; so that `c-crosses-statement-barrier-p' only looks
947 ;; at the non-sexp chars following the sexp.
948 (save-excursion
949 (when (setq
950 boundary-pos
951 (cond
952 ((if macro-start
953 nil
954 (save-excursion
955 (when (c-beginning-of-macro)
956 ;; Set continuation position in case
957 ;; `c-crosses-statement-barrier-p'
958 ;; doesn't detect anything below.
959 (setq sexp-loop-continue-pos (point)))))
960 ;; If the sexp movement took us into a
961 ;; macro then there were only some non-sexp
962 ;; chars after it. Skip out of the macro
963 ;; to analyze them but not the non-sexp
964 ;; chars that might be inside the macro.
965 (c-end-of-macro)
966 (c-crosses-statement-barrier-p
967 (point) sexp-loop-end-pos))
968
969 ((and
970 (eq (char-after) ?{)
971 (not (c-looking-at-inexpr-block lim nil t)))
972 ;; Passed a block sexp. That's a boundary
973 ;; alright.
974 (point))
975
976 ((looking-at "\\s\(")
977 ;; Passed some other paren. Only analyze
978 ;; the non-sexp chars after it.
979 (goto-char (1+ (c-down-list-backward
980 before-sws-pos)))
981 ;; We're at a valid token start position
982 ;; (outside the `save-excursion') if
983 ;; `c-crosses-statement-barrier-p' failed.
984 (c-crosses-statement-barrier-p
985 (point) sexp-loop-end-pos))
986
987 (t
988 ;; Passed a symbol sexp or line
989 ;; continuation. It doesn't matter that
990 ;; it's included in the analyzed region.
991 (if (c-crosses-statement-barrier-p
992 (point) sexp-loop-end-pos)
993 t
994 ;; If it was a line continuation then we
995 ;; have to continue looping.
996 (if (looking-at "\\\\$")
997 (setq sexp-loop-continue-pos (point)))
998 nil))))
999
1000 (setq pptok ptok
1001 ptok tok
1002 tok boundary-pos
1003 sym 'boundary)
1004 ;; Like a C "continue". Analyze the next sexp.
1005 (throw 'loop t)))
1006
1007 sexp-loop-continue-pos) ; End of "go back a sexp" loop condition.
1008 (goto-char sexp-loop-continue-pos)
1009 (setq sexp-loop-end-pos sexp-loop-continue-pos
1010 sexp-loop-continue-pos nil))))
1011
1012 ;; ObjC method def?
1013 (when (and c-opt-method-key
1014 (setq saved (c-in-method-def-p)))
1015 (setq pos saved
1016 ignore-labels t) ; Avoid the label check on exit.
1017 (throw 'loop nil))
1018
1019 ;; Handle labels.
1020 (unless (eq ignore-labels t)
1021 (when (numberp c-maybe-labelp)
1022 ;; `c-crosses-statement-barrier-p' has found a colon, so we
1023 ;; might be in a label now. Have we got a real label
1024 ;; (including a case label) or something like C++'s "public:"?
1025 ;; A case label might use an expression rather than a token.
1026 (setq after-case:-pos (or tok start))
1027 (if (looking-at c-nonlabel-token-key) ; e.g. "while" or "'a'"
1028 (setq c-maybe-labelp nil)
1029 (if after-labels-pos ; Have we already encountered a label?
1030 (if (not last-label-pos)
1031 (setq last-label-pos (or tok start)))
1032 (setq after-labels-pos (or tok start)))
1033 (setq c-maybe-labelp t
1034 label-good-pos nil))) ; bogus "label"
1035
1036 (when (and (not label-good-pos) ; i.e. no invalid "label"'s yet
1037 ; been found.
1038 (looking-at c-nonlabel-token-key)) ; e.g. "while :"
1039 ;; We're in a potential label and it's the first
1040 ;; time we've found something that isn't allowed in
1041 ;; one.
1042 (setq label-good-pos (or tok start))))
1043
1044 ;; We've moved back by a sexp, so update the token positions.
1045 (setq sym nil
1046 pptok ptok
1047 ptok tok
1048 tok (point)
1049 pos tok))) ; Not nil (for the while loop).
1050
1051 ;; If the stack isn't empty there might be errors to report.
1052 (while stack
1053 (if (and (vectorp saved-pos) (eq (length saved-pos) 3))
1054 (c-bos-report-error))
1055 (setq saved-pos (cdr (car stack))
1056 stack (cdr stack)))
1057
1058 (when (and (eq ret 'same)
1059 (not (memq sym '(boundary ignore nil))))
1060 ;; Need to investigate closer whether we've crossed
1061 ;; between a substatement and its containing statement.
1062 (if (setq saved (if (looking-at c-block-stmt-1-key)
1063 ptok
1064 pptok))
1065 (cond ((> start saved) (setq pos saved))
1066 ((= start saved) (setq ret 'up)))))
1067
1068 (when (and (not ignore-labels)
1069 (eq c-maybe-labelp t)
1070 (not (eq ret 'beginning))
1071 after-labels-pos
1072 (or (not label-good-pos)
1073 (<= label-good-pos pos)
1074 (progn
1075 (goto-char (if (and last-label-pos
1076 (< last-label-pos start))
1077 last-label-pos
1078 pos))
1079 (looking-at c-label-kwds-regexp))))
1080 ;; We're in a label. Maybe we should step to the statement
1081 ;; after it.
1082 (if (< after-labels-pos start)
1083 (setq pos after-labels-pos)
1084 (setq ret 'label)
1085 (if (and last-label-pos (< last-label-pos start))
1086 ;; Might have jumped over several labels. Go to the last one.
1087 (setq pos last-label-pos)))))
1088
1089 ;; Have we got "case <expression>:"?
1090 (goto-char pos)
1091 (when (and after-case:-pos
1092 (not (eq ret 'beginning))
1093 (looking-at c-case-kwds-regexp))
1094 (if (< after-case:-pos start)
1095 (setq pos after-case:-pos))
1096 (if (eq ret 'same)
1097 (setq ret 'label)))
1098
1099 ;; Skip over the unary operators that can start the statement.
1100 (while (progn
1101 (c-backward-syntactic-ws)
1102 ;; protect AWK post-inc/decrement operators, etc.
1103 (and (not (c-at-vsemi-p (point)))
1104 (/= (skip-chars-backward "-+!*&~@`#") 0)))
1105 (setq pos (point)))
1106 (goto-char pos)
1107 ret)))
1108
1109 (defun c-crosses-statement-barrier-p (from to)
1110 "Return non-nil if buffer positions FROM to TO cross one or more
1111 statement or declaration boundaries. The returned value is actually
1112 the position of the earliest boundary char. FROM must not be within
1113 a string or comment.
1114
1115 The variable `c-maybe-labelp' is set to the position of the first `:' that
1116 might start a label (i.e. not part of `::' and not preceded by `?'). If a
1117 single `?' is found, then `c-maybe-labelp' is cleared.
1118
1119 For AWK, a statement which is terminated by an EOL (not a \; or a }) is
1120 regarded as having a \"virtual semicolon\" immediately after the last token on
1121 the line. If this virtual semicolon is _at_ from, the function recognizes it.
1122
1123 Note that this function might do hidden buffer changes. See the
1124 comment at the start of cc-engine.el for more info."
1125 (let ((skip-chars c-stmt-delim-chars)
1126 lit-range)
1127 (save-excursion
1128 (catch 'done
1129 (goto-char from)
1130 (while (progn (skip-chars-forward skip-chars to)
1131 (< (point) to))
1132 (cond
1133 ((setq lit-range (c-literal-limits from)) ; Have we landed in a string/comment?
1134 (goto-char (cdr lit-range)))
1135 ((eq (char-after) ?:)
1136 (forward-char)
1137 (if (and (eq (char-after) ?:)
1138 (< (point) to))
1139 ;; Ignore scope operators.
1140 (forward-char)
1141 (setq c-maybe-labelp (1- (point)))))
1142 ((eq (char-after) ??)
1143 ;; A question mark. Can't be a label, so stop
1144 ;; looking for more : and ?.
1145 (setq c-maybe-labelp nil
1146 skip-chars (substring c-stmt-delim-chars 0 -2)))
1147 ((memq (char-after) '(?# ?\n ?\r)) ; A virtual semicolon?
1148 (if (and (eq (char-before) ?\\) (memq (char-after) '(?\n ?\r)))
1149 (backward-char))
1150 (skip-chars-backward " \t" from)
1151 (if (c-at-vsemi-p)
1152 (throw 'done (point))
1153 (forward-line)))
1154 (t (throw 'done (point)))))
1155 ;; In trailing space after an as yet undetected virtual semicolon?
1156 (c-backward-syntactic-ws from)
1157 (if (and (< (point) to)
1158 (c-at-vsemi-p))
1159 (point)
1160 nil)))))
1161
1162 (defun c-at-statement-start-p ()
1163 "Return non-nil if the point is at the first token in a statement
1164 or somewhere in the syntactic whitespace before it.
1165
1166 A \"statement\" here is not restricted to those inside code blocks.
1167 Any kind of declaration-like construct that occur outside function
1168 bodies is also considered a \"statement\".
1169
1170 Note that this function might do hidden buffer changes. See the
1171 comment at the start of cc-engine.el for more info."
1172
1173 (save-excursion
1174 (let ((end (point))
1175 c-maybe-labelp)
1176 (c-syntactic-skip-backward (substring c-stmt-delim-chars 1) nil t)
1177 (or (bobp)
1178 (eq (char-before) ?})
1179 (and (eq (char-before) ?{)
1180 (not (and c-special-brace-lists
1181 (progn (backward-char)
1182 (c-looking-at-special-brace-list)))))
1183 (c-crosses-statement-barrier-p (point) end)))))
1184
1185 (defun c-at-expression-start-p ()
1186 "Return non-nil if the point is at the first token in an expression or
1187 statement, or somewhere in the syntactic whitespace before it.
1188
1189 An \"expression\" here is a bit different from the normal language
1190 grammar sense: It's any sequence of expression tokens except commas,
1191 unless they are enclosed inside parentheses of some kind. Also, an
1192 expression never continues past an enclosing parenthesis, but it might
1193 contain parenthesis pairs of any sort except braces.
1194
1195 Since expressions never cross statement boundaries, this function also
1196 recognizes statement beginnings, just like `c-at-statement-start-p'.
1197
1198 Note that this function might do hidden buffer changes. See the
1199 comment at the start of cc-engine.el for more info."
1200
1201 (save-excursion
1202 (let ((end (point))
1203 (c-stmt-delim-chars c-stmt-delim-chars-with-comma)
1204 c-maybe-labelp)
1205 (c-syntactic-skip-backward (substring c-stmt-delim-chars 1) nil t)
1206 (or (bobp)
1207 (memq (char-before) '(?{ ?}))
1208 (save-excursion (backward-char)
1209 (looking-at "\\s("))
1210 (c-crosses-statement-barrier-p (point) end)))))
1211
1212 \f
1213 ;; A set of functions that covers various idiosyncrasies in
1214 ;; implementations of `forward-comment'.
1215
1216 ;; Note: Some emacsen considers incorrectly that any line comment
1217 ;; ending with a backslash continues to the next line. I can't think
1218 ;; of any way to work around that in a reliable way without changing
1219 ;; the buffer, though. Suggestions welcome. ;) (No, temporarily
1220 ;; changing the syntax for backslash doesn't work since we must treat
1221 ;; escapes in string literals correctly.)
1222
1223 (defun c-forward-single-comment ()
1224 "Move forward past whitespace and the closest following comment, if any.
1225 Return t if a comment was found, nil otherwise. In either case, the
1226 point is moved past the following whitespace. Line continuations,
1227 i.e. a backslashes followed by line breaks, are treated as whitespace.
1228 The line breaks that end line comments are considered to be the
1229 comment enders, so the point will be put on the beginning of the next
1230 line if it moved past a line comment.
1231
1232 This function does not do any hidden buffer changes."
1233
1234 (let ((start (point)))
1235 (when (looking-at "\\([ \t\n\r\f\v]\\|\\\\[\n\r]\\)+")
1236 (goto-char (match-end 0)))
1237
1238 (when (forward-comment 1)
1239 (if (eobp)
1240 ;; Some emacsen (e.g. XEmacs 21) return t when moving
1241 ;; forwards at eob.
1242 nil
1243
1244 ;; Emacs includes the ending newline in a b-style (c++)
1245 ;; comment, but XEmacs doesn't. We depend on the Emacs
1246 ;; behavior (which also is symmetric).
1247 (if (and (eolp) (elt (parse-partial-sexp start (point)) 7))
1248 (condition-case nil (forward-char 1)))
1249
1250 t))))
1251
1252 (defsubst c-forward-comments ()
1253 "Move forward past all following whitespace and comments.
1254 Line continuations, i.e. a backslashes followed by line breaks, are
1255 treated as whitespace.
1256
1257 Note that this function might do hidden buffer changes. See the
1258 comment at the start of cc-engine.el for more info."
1259
1260 (while (or
1261 ;; If forward-comment in at least XEmacs 21 is given a large
1262 ;; positive value, it'll loop all the way through if it hits
1263 ;; eob.
1264 (and (forward-comment 5)
1265 ;; Some emacsen (e.g. XEmacs 21) return t when moving
1266 ;; forwards at eob.
1267 (not (eobp)))
1268
1269 (when (looking-at "\\\\[\n\r]")
1270 (forward-char 2)
1271 t))))
1272
1273 (defun c-backward-single-comment ()
1274 "Move backward past whitespace and the closest preceding comment, if any.
1275 Return t if a comment was found, nil otherwise. In either case, the
1276 point is moved past the preceding whitespace. Line continuations,
1277 i.e. a backslashes followed by line breaks, are treated as whitespace.
1278 The line breaks that end line comments are considered to be the
1279 comment enders, so the point cannot be at the end of the same line to
1280 move over a line comment.
1281
1282 This function does not do any hidden buffer changes."
1283
1284 (let ((start (point)))
1285 ;; When we got newline terminated comments, forward-comment in all
1286 ;; supported emacsen so far will stop at eol of each line not
1287 ;; ending with a comment when moving backwards. This corrects for
1288 ;; that, and at the same time handles line continuations.
1289 (while (progn
1290 (skip-chars-backward " \t\n\r\f\v")
1291 (and (looking-at "[\n\r]")
1292 (eq (char-before) ?\\)))
1293 (backward-char))
1294
1295 (if (bobp)
1296 ;; Some emacsen (e.g. Emacs 19.34) return t when moving
1297 ;; backwards at bob.
1298 nil
1299
1300 ;; Leave point after the closest following newline if we've
1301 ;; backed up over any above, since forward-comment won't move
1302 ;; backward over a line comment if point is at the end of the
1303 ;; same line.
1304 (re-search-forward "\\=\\s *[\n\r]" start t)
1305
1306 (if (if (forward-comment -1)
1307 (if (eolp)
1308 ;; If forward-comment above succeeded and we're at eol
1309 ;; then the newline we moved over above didn't end a
1310 ;; line comment, so we give it another go.
1311 (forward-comment -1)
1312 t))
1313
1314 ;; Emacs <= 20 and XEmacs move back over the closer of a
1315 ;; block comment that lacks an opener.
1316 (if (looking-at "\\*/")
1317 (progn (forward-char 2) nil)
1318 t)))))
1319
1320 (defsubst c-backward-comments ()
1321 "Move backward past all preceding whitespace and comments.
1322 Line continuations, i.e. a backslashes followed by line breaks, are
1323 treated as whitespace. The line breaks that end line comments are
1324 considered to be the comment enders, so the point cannot be at the end
1325 of the same line to move over a line comment. Unlike
1326 c-backward-syntactic-ws, this function doesn't move back over
1327 preprocessor directives.
1328
1329 Note that this function might do hidden buffer changes. See the
1330 comment at the start of cc-engine.el for more info."
1331
1332 (let ((start (point)))
1333 (while (and
1334 ;; `forward-comment' in some emacsen (e.g. XEmacs 21.4)
1335 ;; return t when moving backwards at bob.
1336 (not (bobp))
1337
1338 (if (forward-comment -1)
1339 (if (looking-at "\\*/")
1340 ;; Emacs <= 20 and XEmacs move back over the
1341 ;; closer of a block comment that lacks an opener.
1342 (progn (forward-char 2) nil)
1343 t)
1344
1345 ;; XEmacs treats line continuations as whitespace but
1346 ;; only in the backward direction, which seems a bit
1347 ;; odd. Anyway, this is necessary for Emacs.
1348 (when (and (looking-at "[\n\r]")
1349 (eq (char-before) ?\\)
1350 (< (point) start))
1351 (backward-char)
1352 t))))))
1353
1354 \f
1355 ;; Tools for skipping over syntactic whitespace.
1356
1357 ;; The following functions use text properties to cache searches over
1358 ;; large regions of syntactic whitespace. It works as follows:
1359 ;;
1360 ;; o If a syntactic whitespace region contains anything but simple
1361 ;; whitespace (i.e. space, tab and line breaks), the text property
1362 ;; `c-in-sws' is put over it. At places where we have stopped
1363 ;; within that region there's also a `c-is-sws' text property.
1364 ;; That since there typically are nested whitespace inside that
1365 ;; must be handled separately, e.g. whitespace inside a comment or
1366 ;; cpp directive. Thus, from one point with `c-is-sws' it's safe
1367 ;; to jump to another point with that property within the same
1368 ;; `c-in-sws' region. It can be likened to a ladder where
1369 ;; `c-in-sws' marks the bars and `c-is-sws' the rungs.
1370 ;;
1371 ;; o The `c-is-sws' property is put on the simple whitespace chars at
1372 ;; a "rung position" and also maybe on the first following char.
1373 ;; As many characters as can be conveniently found in this range
1374 ;; are marked, but no assumption can be made that the whole range
1375 ;; is marked (it could be clobbered by later changes, for
1376 ;; instance).
1377 ;;
1378 ;; Note that some part of the beginning of a sequence of simple
1379 ;; whitespace might be part of the end of a preceding line comment
1380 ;; or cpp directive and must not be considered part of the "rung".
1381 ;; Such whitespace is some amount of horizontal whitespace followed
1382 ;; by a newline. In the case of cpp directives it could also be
1383 ;; two newlines with horizontal whitespace between them.
1384 ;;
1385 ;; The reason to include the first following char is to cope with
1386 ;; "rung positions" that doesn't have any ordinary whitespace. If
1387 ;; `c-is-sws' is put on a token character it does not have
1388 ;; `c-in-sws' set simultaneously. That's the only case when that
1389 ;; can occur, and the reason for not extending the `c-in-sws'
1390 ;; region to cover it is that the `c-in-sws' region could then be
1391 ;; accidentally merged with a following one if the token is only
1392 ;; one character long.
1393 ;;
1394 ;; o On buffer changes the `c-in-sws' and `c-is-sws' properties are
1395 ;; removed in the changed region. If the change was inside
1396 ;; syntactic whitespace that means that the "ladder" is broken, but
1397 ;; a later call to `c-forward-sws' or `c-backward-sws' will use the
1398 ;; parts on either side and use an ordinary search only to "repair"
1399 ;; the gap.
1400 ;;
1401 ;; Special care needs to be taken if a region is removed: If there
1402 ;; are `c-in-sws' on both sides of it which do not connect inside
1403 ;; the region then they can't be joined. If e.g. a marked macro is
1404 ;; broken, syntactic whitespace inside the new text might be
1405 ;; marked. If those marks would become connected with the old
1406 ;; `c-in-sws' range around the macro then we could get a ladder
1407 ;; with one end outside the macro and the other at some whitespace
1408 ;; within it.
1409 ;;
1410 ;; The main motivation for this system is to increase the speed in
1411 ;; skipping over the large whitespace regions that can occur at the
1412 ;; top level in e.g. header files that contain a lot of comments and
1413 ;; cpp directives. For small comments inside code it's probably
1414 ;; slower than using `forward-comment' straightforwardly, but speed is
1415 ;; not a significant factor there anyway.
1416
1417 ; (defface c-debug-is-sws-face
1418 ; '((t (:background "GreenYellow")))
1419 ; "Debug face to mark the `c-is-sws' property.")
1420 ; (defface c-debug-in-sws-face
1421 ; '((t (:underline t)))
1422 ; "Debug face to mark the `c-in-sws' property.")
1423
1424 ; (defun c-debug-put-sws-faces ()
1425 ; ;; Put the sws debug faces on all the `c-is-sws' and `c-in-sws'
1426 ; ;; properties in the buffer.
1427 ; (interactive)
1428 ; (save-excursion
1429 ; (c-save-buffer-state (in-face)
1430 ; (goto-char (point-min))
1431 ; (setq in-face (if (get-text-property (point) 'c-is-sws)
1432 ; (point)))
1433 ; (while (progn
1434 ; (goto-char (next-single-property-change
1435 ; (point) 'c-is-sws nil (point-max)))
1436 ; (if in-face
1437 ; (progn
1438 ; (c-debug-add-face in-face (point) 'c-debug-is-sws-face)
1439 ; (setq in-face nil))
1440 ; (setq in-face (point)))
1441 ; (not (eobp))))
1442 ; (goto-char (point-min))
1443 ; (setq in-face (if (get-text-property (point) 'c-in-sws)
1444 ; (point)))
1445 ; (while (progn
1446 ; (goto-char (next-single-property-change
1447 ; (point) 'c-in-sws nil (point-max)))
1448 ; (if in-face
1449 ; (progn
1450 ; (c-debug-add-face in-face (point) 'c-debug-in-sws-face)
1451 ; (setq in-face nil))
1452 ; (setq in-face (point)))
1453 ; (not (eobp)))))))
1454
1455 (defmacro c-debug-sws-msg (&rest args)
1456 ;;`(message ,@args)
1457 )
1458
1459 (defmacro c-put-is-sws (beg end)
1460 ;; This macro does a hidden buffer change.
1461 `(let ((beg ,beg) (end ,end))
1462 (put-text-property beg end 'c-is-sws t)
1463 ,@(when (facep 'c-debug-is-sws-face)
1464 `((c-debug-add-face beg end 'c-debug-is-sws-face)))))
1465
1466 (defmacro c-put-in-sws (beg end)
1467 ;; This macro does a hidden buffer change.
1468 `(let ((beg ,beg) (end ,end))
1469 (put-text-property beg end 'c-in-sws t)
1470 ,@(when (facep 'c-debug-is-sws-face)
1471 `((c-debug-add-face beg end 'c-debug-in-sws-face)))))
1472
1473 (defmacro c-remove-is-sws (beg end)
1474 ;; This macro does a hidden buffer change.
1475 `(let ((beg ,beg) (end ,end))
1476 (remove-text-properties beg end '(c-is-sws nil))
1477 ,@(when (facep 'c-debug-is-sws-face)
1478 `((c-debug-remove-face beg end 'c-debug-is-sws-face)))))
1479
1480 (defmacro c-remove-in-sws (beg end)
1481 ;; This macro does a hidden buffer change.
1482 `(let ((beg ,beg) (end ,end))
1483 (remove-text-properties beg end '(c-in-sws nil))
1484 ,@(when (facep 'c-debug-is-sws-face)
1485 `((c-debug-remove-face beg end 'c-debug-in-sws-face)))))
1486
1487 (defmacro c-remove-is-and-in-sws (beg end)
1488 ;; This macro does a hidden buffer change.
1489 `(let ((beg ,beg) (end ,end))
1490 (remove-text-properties beg end '(c-is-sws nil c-in-sws nil))
1491 ,@(when (facep 'c-debug-is-sws-face)
1492 `((c-debug-remove-face beg end 'c-debug-is-sws-face)
1493 (c-debug-remove-face beg end 'c-debug-in-sws-face)))))
1494
1495 (defsubst c-invalidate-sws-region-after (beg end)
1496 ;; Called from `after-change-functions'. Note that if
1497 ;; `c-forward-sws' or `c-backward-sws' are used outside
1498 ;; `c-save-buffer-state' or similar then this will remove the cache
1499 ;; properties right after they're added.
1500 ;;
1501 ;; This function does hidden buffer changes.
1502
1503 (save-excursion
1504 ;; Adjust the end to remove the properties in any following simple
1505 ;; ws up to and including the next line break, if there is any
1506 ;; after the changed region. This is necessary e.g. when a rung
1507 ;; marked empty line is converted to a line comment by inserting
1508 ;; "//" before the line break. In that case the line break would
1509 ;; keep the rung mark which could make a later `c-backward-sws'
1510 ;; move into the line comment instead of over it.
1511 (goto-char end)
1512 (skip-chars-forward " \t\f\v")
1513 (when (and (eolp) (not (eobp)))
1514 (setq end (1+ (point)))))
1515
1516 (when (and (= beg end)
1517 (get-text-property beg 'c-in-sws)
1518 (> beg (point-min))
1519 (get-text-property (1- beg) 'c-in-sws))
1520 ;; Ensure that an `c-in-sws' range gets broken. Note that it isn't
1521 ;; safe to keep a range that was continuous before the change. E.g:
1522 ;;
1523 ;; #define foo
1524 ;; \
1525 ;; bar
1526 ;;
1527 ;; There can be a "ladder" between "#" and "b". Now, if the newline
1528 ;; after "foo" is removed then "bar" will become part of the cpp
1529 ;; directive instead of a syntactically relevant token. In that
1530 ;; case there's no longer syntactic ws from "#" to "b".
1531 (setq beg (1- beg)))
1532
1533 (c-debug-sws-msg "c-invalidate-sws-region-after [%s..%s]" beg end)
1534 (c-remove-is-and-in-sws beg end))
1535
1536 (defun c-forward-sws ()
1537 ;; Used by `c-forward-syntactic-ws' to implement the unbounded search.
1538 ;;
1539 ;; This function might do hidden buffer changes.
1540
1541 (let (;; `rung-pos' is set to a position as early as possible in the
1542 ;; unmarked part of the simple ws region.
1543 (rung-pos (point)) next-rung-pos rung-end-pos last-put-in-sws-pos
1544 rung-is-marked next-rung-is-marked simple-ws-end
1545 ;; `safe-start' is set when it's safe to cache the start position.
1546 ;; It's not set if we've initially skipped over comments and line
1547 ;; continuations since we might have gone out through the end of a
1548 ;; macro then. This provision makes `c-forward-sws' not populate the
1549 ;; cache in the majority of cases, but otoh is `c-backward-sws' by far
1550 ;; more common.
1551 safe-start)
1552
1553 ;; Skip simple ws and do a quick check on the following character to see
1554 ;; if it's anything that can't start syntactic ws, so we can bail out
1555 ;; early in the majority of cases when there just are a few ws chars.
1556 (skip-chars-forward " \t\n\r\f\v")
1557 (when (looking-at c-syntactic-ws-start)
1558
1559 (setq rung-end-pos (min (1+ (point)) (point-max)))
1560 (if (setq rung-is-marked (text-property-any rung-pos rung-end-pos
1561 'c-is-sws t))
1562 ;; Find the last rung position to avoid setting properties in all
1563 ;; the cases when the marked rung is complete.
1564 ;; (`next-single-property-change' is certain to move at least one
1565 ;; step forward.)
1566 (setq rung-pos (1- (next-single-property-change
1567 rung-is-marked 'c-is-sws nil rung-end-pos)))
1568 ;; Got no marked rung here. Since the simple ws might have started
1569 ;; inside a line comment or cpp directive we must set `rung-pos' as
1570 ;; high as possible.
1571 (setq rung-pos (point)))
1572
1573 (while
1574 (progn
1575 (while
1576 (when (and rung-is-marked
1577 (get-text-property (point) 'c-in-sws))
1578
1579 ;; The following search is the main reason that `c-in-sws'
1580 ;; and `c-is-sws' aren't combined to one property.
1581 (goto-char (next-single-property-change
1582 (point) 'c-in-sws nil (point-max)))
1583 (unless (get-text-property (point) 'c-is-sws)
1584 ;; If the `c-in-sws' region extended past the last
1585 ;; `c-is-sws' char we have to go back a bit.
1586 (or (get-text-property (1- (point)) 'c-is-sws)
1587 (goto-char (previous-single-property-change
1588 (point) 'c-is-sws)))
1589 (backward-char))
1590
1591 (c-debug-sws-msg
1592 "c-forward-sws cached move %s -> %s (max %s)"
1593 rung-pos (point) (point-max))
1594
1595 (setq rung-pos (point))
1596 (and (> (skip-chars-forward " \t\n\r\f\v") 0)
1597 (not (eobp))))
1598
1599 ;; We'll loop here if there is simple ws after the last rung.
1600 ;; That means that there's been some change in it and it's
1601 ;; possible that we've stepped into another ladder, so extend
1602 ;; the previous one to join with it if there is one, and try to
1603 ;; use the cache again.
1604 (c-debug-sws-msg
1605 "c-forward-sws extending rung with [%s..%s] (max %s)"
1606 (1+ rung-pos) (1+ (point)) (point-max))
1607 (unless (get-text-property (point) 'c-is-sws)
1608 ;; Remove any `c-in-sws' property from the last char of
1609 ;; the rung before we mark it with `c-is-sws', so that we
1610 ;; won't connect with the remains of a broken "ladder".
1611 (c-remove-in-sws (point) (1+ (point))))
1612 (c-put-is-sws (1+ rung-pos)
1613 (1+ (point)))
1614 (c-put-in-sws rung-pos
1615 (setq rung-pos (point)
1616 last-put-in-sws-pos rung-pos)))
1617
1618 (setq simple-ws-end (point))
1619 (c-forward-comments)
1620
1621 (cond
1622 ((/= (point) simple-ws-end)
1623 ;; Skipped over comments. Don't cache at eob in case the buffer
1624 ;; is narrowed.
1625 (not (eobp)))
1626
1627 ((save-excursion
1628 (and c-opt-cpp-prefix
1629 (looking-at c-opt-cpp-start)
1630 (progn (skip-chars-backward " \t")
1631 (bolp))
1632 (or (bobp)
1633 (progn (backward-char)
1634 (not (eq (char-before) ?\\))))))
1635 ;; Skip a preprocessor directive.
1636 (end-of-line)
1637 (while (and (eq (char-before) ?\\)
1638 (= (forward-line 1) 0))
1639 (end-of-line))
1640 (forward-line 1)
1641 (setq safe-start t)
1642 ;; Don't cache at eob in case the buffer is narrowed.
1643 (not (eobp)))))
1644
1645 ;; We've searched over a piece of non-white syntactic ws. See if this
1646 ;; can be cached.
1647 (setq next-rung-pos (point))
1648 (skip-chars-forward " \t\n\r\f\v")
1649 (setq rung-end-pos (min (1+ (point)) (point-max)))
1650
1651 (if (or
1652 ;; Cache if we haven't skipped comments only, and if we started
1653 ;; either from a marked rung or from a completely uncached
1654 ;; position.
1655 (and safe-start
1656 (or rung-is-marked
1657 (not (get-text-property simple-ws-end 'c-in-sws))))
1658
1659 ;; See if there's a marked rung in the encountered simple ws. If
1660 ;; so then we can cache, unless `safe-start' is nil. Even then
1661 ;; we need to do this to check if the cache can be used for the
1662 ;; next step.
1663 (and (setq next-rung-is-marked
1664 (text-property-any next-rung-pos rung-end-pos
1665 'c-is-sws t))
1666 safe-start))
1667
1668 (progn
1669 (c-debug-sws-msg
1670 "c-forward-sws caching [%s..%s] - [%s..%s] (max %s)"
1671 rung-pos (1+ simple-ws-end) next-rung-pos rung-end-pos
1672 (point-max))
1673
1674 ;; Remove the properties for any nested ws that might be cached.
1675 ;; Only necessary for `c-is-sws' since `c-in-sws' will be set
1676 ;; anyway.
1677 (c-remove-is-sws (1+ simple-ws-end) next-rung-pos)
1678 (unless (and rung-is-marked (= rung-pos simple-ws-end))
1679 (c-put-is-sws rung-pos
1680 (1+ simple-ws-end))
1681 (setq rung-is-marked t))
1682 (c-put-in-sws rung-pos
1683 (setq rung-pos (point)
1684 last-put-in-sws-pos rung-pos))
1685 (unless (get-text-property (1- rung-end-pos) 'c-is-sws)
1686 ;; Remove any `c-in-sws' property from the last char of
1687 ;; the rung before we mark it with `c-is-sws', so that we
1688 ;; won't connect with the remains of a broken "ladder".
1689 (c-remove-in-sws (1- rung-end-pos) rung-end-pos))
1690 (c-put-is-sws next-rung-pos
1691 rung-end-pos))
1692
1693 (c-debug-sws-msg
1694 "c-forward-sws not caching [%s..%s] - [%s..%s] (max %s)"
1695 rung-pos (1+ simple-ws-end) next-rung-pos rung-end-pos
1696 (point-max))
1697
1698 ;; Set `rung-pos' for the next rung. It's the same thing here as
1699 ;; initially, except that the rung position is set as early as
1700 ;; possible since we can't be in the ending ws of a line comment or
1701 ;; cpp directive now.
1702 (if (setq rung-is-marked next-rung-is-marked)
1703 (setq rung-pos (1- (next-single-property-change
1704 rung-is-marked 'c-is-sws nil rung-end-pos)))
1705 (setq rung-pos next-rung-pos))
1706 (setq safe-start t)))
1707
1708 ;; Make sure that the newly marked `c-in-sws' region doesn't connect to
1709 ;; another one after the point (which might occur when editing inside a
1710 ;; comment or macro).
1711 (when (eq last-put-in-sws-pos (point))
1712 (cond ((< last-put-in-sws-pos (point-max))
1713 (c-debug-sws-msg
1714 "c-forward-sws clearing at %s for cache separation"
1715 last-put-in-sws-pos)
1716 (c-remove-in-sws last-put-in-sws-pos
1717 (1+ last-put-in-sws-pos)))
1718 (t
1719 ;; If at eob we have to clear the last character before the end
1720 ;; instead since the buffer might be narrowed and there might
1721 ;; be a `c-in-sws' after (point-max). In this case it's
1722 ;; necessary to clear both properties.
1723 (c-debug-sws-msg
1724 "c-forward-sws clearing thoroughly at %s for cache separation"
1725 (1- last-put-in-sws-pos))
1726 (c-remove-is-and-in-sws (1- last-put-in-sws-pos)
1727 last-put-in-sws-pos))))
1728 )))
1729
1730 (defun c-backward-sws ()
1731 ;; Used by `c-backward-syntactic-ws' to implement the unbounded search.
1732 ;;
1733 ;; This function might do hidden buffer changes.
1734
1735 (let (;; `rung-pos' is set to a position as late as possible in the unmarked
1736 ;; part of the simple ws region.
1737 (rung-pos (point)) next-rung-pos last-put-in-sws-pos
1738 rung-is-marked simple-ws-beg cmt-skip-pos)
1739
1740 ;; Skip simple horizontal ws and do a quick check on the preceding
1741 ;; character to see if it's anying that can't end syntactic ws, so we can
1742 ;; bail out early in the majority of cases when there just are a few ws
1743 ;; chars. Newlines are complicated in the backward direction, so we can't
1744 ;; skip over them.
1745 (skip-chars-backward " \t\f")
1746 (when (and (not (bobp))
1747 (save-excursion
1748 (backward-char)
1749 (looking-at c-syntactic-ws-end)))
1750
1751 ;; Try to find a rung position in the simple ws preceding point, so that
1752 ;; we can get a cache hit even if the last bit of the simple ws has
1753 ;; changed recently.
1754 (setq simple-ws-beg (point))
1755 (skip-chars-backward " \t\n\r\f\v")
1756 (if (setq rung-is-marked (text-property-any
1757 (point) (min (1+ rung-pos) (point-max))
1758 'c-is-sws t))
1759 ;; `rung-pos' will be the earliest marked position, which means that
1760 ;; there might be later unmarked parts in the simple ws region.
1761 ;; It's not worth the effort to fix that; the last part of the
1762 ;; simple ws is also typically edited often, so it could be wasted.
1763 (goto-char (setq rung-pos rung-is-marked))
1764 (goto-char simple-ws-beg))
1765
1766 (while
1767 (progn
1768 (while
1769 (when (and rung-is-marked
1770 (not (bobp))
1771 (get-text-property (1- (point)) 'c-in-sws))
1772
1773 ;; The following search is the main reason that `c-in-sws'
1774 ;; and `c-is-sws' aren't combined to one property.
1775 (goto-char (previous-single-property-change
1776 (point) 'c-in-sws nil (point-min)))
1777 (unless (get-text-property (point) 'c-is-sws)
1778 ;; If the `c-in-sws' region extended past the first
1779 ;; `c-is-sws' char we have to go forward a bit.
1780 (goto-char (next-single-property-change
1781 (point) 'c-is-sws)))
1782
1783 (c-debug-sws-msg
1784 "c-backward-sws cached move %s <- %s (min %s)"
1785 (point) rung-pos (point-min))
1786
1787 (setq rung-pos (point))
1788 (if (and (< (min (skip-chars-backward " \t\f\v")
1789 (progn
1790 (setq simple-ws-beg (point))
1791 (skip-chars-backward " \t\n\r\f\v")))
1792 0)
1793 (setq rung-is-marked
1794 (text-property-any (point) rung-pos
1795 'c-is-sws t)))
1796 t
1797 (goto-char simple-ws-beg)
1798 nil))
1799
1800 ;; We'll loop here if there is simple ws before the first rung.
1801 ;; That means that there's been some change in it and it's
1802 ;; possible that we've stepped into another ladder, so extend
1803 ;; the previous one to join with it if there is one, and try to
1804 ;; use the cache again.
1805 (c-debug-sws-msg
1806 "c-backward-sws extending rung with [%s..%s] (min %s)"
1807 rung-is-marked rung-pos (point-min))
1808 (unless (get-text-property (1- rung-pos) 'c-is-sws)
1809 ;; Remove any `c-in-sws' property from the last char of
1810 ;; the rung before we mark it with `c-is-sws', so that we
1811 ;; won't connect with the remains of a broken "ladder".
1812 (c-remove-in-sws (1- rung-pos) rung-pos))
1813 (c-put-is-sws rung-is-marked
1814 rung-pos)
1815 (c-put-in-sws rung-is-marked
1816 (1- rung-pos))
1817 (setq rung-pos rung-is-marked
1818 last-put-in-sws-pos rung-pos))
1819
1820 (c-backward-comments)
1821 (setq cmt-skip-pos (point))
1822
1823 (cond
1824 ((and c-opt-cpp-prefix
1825 (/= cmt-skip-pos simple-ws-beg)
1826 (c-beginning-of-macro))
1827 ;; Inside a cpp directive. See if it should be skipped over.
1828 (let ((cpp-beg (point)))
1829
1830 ;; Move back over all line continuations in the region skipped
1831 ;; over by `c-backward-comments'. If we go past it then we
1832 ;; started inside the cpp directive.
1833 (goto-char simple-ws-beg)
1834 (beginning-of-line)
1835 (while (and (> (point) cmt-skip-pos)
1836 (progn (backward-char)
1837 (eq (char-before) ?\\)))
1838 (beginning-of-line))
1839
1840 (if (< (point) cmt-skip-pos)
1841 ;; Don't move past the cpp directive if we began inside
1842 ;; it. Note that the position at the end of the last line
1843 ;; of the macro is also considered to be within it.
1844 (progn (goto-char cmt-skip-pos)
1845 nil)
1846
1847 ;; It's worthwhile to spend a little bit of effort on finding
1848 ;; the end of the macro, to get a good `simple-ws-beg'
1849 ;; position for the cache. Note that `c-backward-comments'
1850 ;; could have stepped over some comments before going into
1851 ;; the macro, and then `simple-ws-beg' must be kept on the
1852 ;; same side of those comments.
1853 (goto-char simple-ws-beg)
1854 (skip-chars-backward " \t\n\r\f\v")
1855 (if (eq (char-before) ?\\)
1856 (forward-char))
1857 (forward-line 1)
1858 (if (< (point) simple-ws-beg)
1859 ;; Might happen if comments after the macro were skipped
1860 ;; over.
1861 (setq simple-ws-beg (point)))
1862
1863 (goto-char cpp-beg)
1864 t)))
1865
1866 ((/= (save-excursion
1867 (skip-chars-forward " \t\n\r\f\v" simple-ws-beg)
1868 (setq next-rung-pos (point)))
1869 simple-ws-beg)
1870 ;; Skipped over comments. Must put point at the end of
1871 ;; the simple ws at point since we might be after a line
1872 ;; comment or cpp directive that's been partially
1873 ;; narrowed out, and we can't risk marking the simple ws
1874 ;; at the end of it.
1875 (goto-char next-rung-pos)
1876 t)))
1877
1878 ;; We've searched over a piece of non-white syntactic ws. See if this
1879 ;; can be cached.
1880 (setq next-rung-pos (point))
1881 (skip-chars-backward " \t\f\v")
1882
1883 (if (or
1884 ;; Cache if we started either from a marked rung or from a
1885 ;; completely uncached position.
1886 rung-is-marked
1887 (not (get-text-property (1- simple-ws-beg) 'c-in-sws))
1888
1889 ;; Cache if there's a marked rung in the encountered simple ws.
1890 (save-excursion
1891 (skip-chars-backward " \t\n\r\f\v")
1892 (text-property-any (point) (min (1+ next-rung-pos) (point-max))
1893 'c-is-sws t)))
1894
1895 (progn
1896 (c-debug-sws-msg
1897 "c-backward-sws caching [%s..%s] - [%s..%s] (min %s)"
1898 (point) (1+ next-rung-pos)
1899 simple-ws-beg (min (1+ rung-pos) (point-max))
1900 (point-min))
1901
1902 ;; Remove the properties for any nested ws that might be cached.
1903 ;; Only necessary for `c-is-sws' since `c-in-sws' will be set
1904 ;; anyway.
1905 (c-remove-is-sws (1+ next-rung-pos) simple-ws-beg)
1906 (unless (and rung-is-marked (= simple-ws-beg rung-pos))
1907 (let ((rung-end-pos (min (1+ rung-pos) (point-max))))
1908 (unless (get-text-property (1- rung-end-pos) 'c-is-sws)
1909 ;; Remove any `c-in-sws' property from the last char of
1910 ;; the rung before we mark it with `c-is-sws', so that we
1911 ;; won't connect with the remains of a broken "ladder".
1912 (c-remove-in-sws (1- rung-end-pos) rung-end-pos))
1913 (c-put-is-sws simple-ws-beg
1914 rung-end-pos)
1915 (setq rung-is-marked t)))
1916 (c-put-in-sws (setq simple-ws-beg (point)
1917 last-put-in-sws-pos simple-ws-beg)
1918 rung-pos)
1919 (c-put-is-sws (setq rung-pos simple-ws-beg)
1920 (1+ next-rung-pos)))
1921
1922 (c-debug-sws-msg
1923 "c-backward-sws not caching [%s..%s] - [%s..%s] (min %s)"
1924 (point) (1+ next-rung-pos)
1925 simple-ws-beg (min (1+ rung-pos) (point-max))
1926 (point-min))
1927 (setq rung-pos next-rung-pos
1928 simple-ws-beg (point))
1929 ))
1930
1931 ;; Make sure that the newly marked `c-in-sws' region doesn't connect to
1932 ;; another one before the point (which might occur when editing inside a
1933 ;; comment or macro).
1934 (when (eq last-put-in-sws-pos (point))
1935 (cond ((< (point-min) last-put-in-sws-pos)
1936 (c-debug-sws-msg
1937 "c-backward-sws clearing at %s for cache separation"
1938 (1- last-put-in-sws-pos))
1939 (c-remove-in-sws (1- last-put-in-sws-pos)
1940 last-put-in-sws-pos))
1941 ((> (point-min) 1)
1942 ;; If at bob and the buffer is narrowed, we have to clear the
1943 ;; character we're standing on instead since there might be a
1944 ;; `c-in-sws' before (point-min). In this case it's necessary
1945 ;; to clear both properties.
1946 (c-debug-sws-msg
1947 "c-backward-sws clearing thoroughly at %s for cache separation"
1948 last-put-in-sws-pos)
1949 (c-remove-is-and-in-sws last-put-in-sws-pos
1950 (1+ last-put-in-sws-pos)))))
1951 )))
1952
1953 \f
1954 ;; Other whitespace tools
1955 (defun c-partial-ws-p (beg end)
1956 ;; Is the region (beg end) WS, and is there WS (or BOB/EOB) next to the
1957 ;; region? This is a "heuristic" function. .....
1958 ;;
1959 ;; The motivation for the second bit is to check whether removing this
1960 ;; region would coalesce two symbols.
1961 ;;
1962 ;; FIXME!!! This function doesn't check virtual semicolons in any way. Be
1963 ;; careful about using this function for, e.g. AWK. (2007/3/7)
1964 (save-excursion
1965 (let ((end+1 (min (1+ end) (point-max))))
1966 (or (progn (goto-char (max (point-min) (1- beg)))
1967 (c-skip-ws-forward end)
1968 (eq (point) end))
1969 (progn (goto-char beg)
1970 (c-skip-ws-forward end+1)
1971 (eq (point) end+1))))))
1972 \f
1973 ;; A system for finding noteworthy parens before the point.
1974
1975 (defconst c-state-cache-too-far 5000)
1976 ;; A maximum comfortable scanning distance, e.g. between
1977 ;; `c-state-cache-good-pos' and "HERE" (where we call c-parse-state). When
1978 ;; this distance is exceeded, we take "emergency meausures", e.g. by clearing
1979 ;; the cache and starting again from point-min or a beginning of defun. This
1980 ;; value can be tuned for efficiency or set to a lower value for testing.
1981
1982 (defvar c-state-cache nil)
1983 (make-variable-buffer-local 'c-state-cache)
1984 ;; The state cache used by `c-parse-state' to cut down the amount of
1985 ;; searching. It's the result from some earlier `c-parse-state' call. See
1986 ;; `c-parse-state''s doc string for details of its structure.
1987 ;;
1988 ;; The use of the cached info is more effective if the next
1989 ;; `c-parse-state' call is on a line close by the one the cached state
1990 ;; was made at; the cache can actually slow down a little if the
1991 ;; cached state was made very far back in the buffer. The cache is
1992 ;; most effective if `c-parse-state' is used on each line while moving
1993 ;; forward.
1994
1995 (defvar c-state-cache-good-pos 1)
1996 (make-variable-buffer-local 'c-state-cache-good-pos)
1997 ;; This is a position where `c-state-cache' is known to be correct, or
1998 ;; nil (see below). It's a position inside one of the recorded unclosed
1999 ;; parens or the top level, but not further nested inside any literal or
2000 ;; subparen that is closed before the last recorded position.
2001 ;;
2002 ;; The exact position is chosen to try to be close to yet earlier than
2003 ;; the position where `c-state-cache' will be called next. Right now
2004 ;; the heuristic is to set it to the position after the last found
2005 ;; closing paren (of any type) before the line on which
2006 ;; `c-parse-state' was called. That is chosen primarily to work well
2007 ;; with refontification of the current line.
2008 ;;
2009 ;; 2009-07-28: When `c-state-point-min' and the last position where
2010 ;; `c-parse-state' or for which `c-invalidate-state-cache' was called, are
2011 ;; both in the same literal, there is no such "good position", and
2012 ;; c-state-cache-good-pos is then nil. This is the ONLY circumstance in which
2013 ;; it can be nil. In this case, `c-state-point-min-literal' will be non-nil.
2014 ;;
2015 ;; 2009-06-12: In a brace desert, c-state-cache-good-pos may also be in
2016 ;; the middle of the desert, as long as it is not within a brace pair
2017 ;; recorded in `c-state-cache' or a paren/bracket pair.
2018
2019
2020 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2021 ;; We maintain a simple cache of positions which aren't in a literal, so as to
2022 ;; speed up testing for non-literality.
2023 (defconst c-state-nonlit-pos-interval 10000)
2024 ;; The approximate interval between entries in `c-state-nonlit-pos-cache'.
2025
2026 (defvar c-state-nonlit-pos-cache nil)
2027 (make-variable-buffer-local 'c-state-nonlit-pos-cache)
2028 ;; A list of buffer positions which are known not to be in a literal. This is
2029 ;; ordered with higher positions at the front of the list. Only those which
2030 ;; are less than `c-state-nonlit-pos-cache-limit' are valid.
2031
2032 (defvar c-state-nonlit-pos-cache-limit 1)
2033 (make-variable-buffer-local 'c-state-nonlit-pos-cache-limit)
2034 ;; An upper limit on valid entries in `c-state-nonlit-pos-cache'. This is
2035 ;; reduced by buffer changes, and increased by invocations of
2036 ;; `c-state-literal-at'.
2037
2038 (defsubst c-state-pp-to-literal (from to)
2039 ;; Do a parse-partial-sexp from FROM to TO, returning the bounds of any
2040 ;; literal at TO as a cons, otherwise NIL.
2041 ;; FROM must not be in a literal, and the buffer should already be wide
2042 ;; enough.
2043 (save-excursion
2044 (let ((s (parse-partial-sexp from to)))
2045 (when (or (nth 3 s) (nth 4 s)) ; in a string or comment
2046 (parse-partial-sexp (point) (point-max)
2047 nil ; TARGETDEPTH
2048 nil ; STOPBEFORE
2049 s ; OLDSTATE
2050 'syntax-table) ; stop at end of literal
2051 (cons (nth 8 s) (point))))))
2052
2053 (defun c-state-literal-at (here)
2054 ;; If position HERE is inside a literal, return (START . END), the
2055 ;; boundaries of the literal (which may be outside the accessible bit of the
2056 ;; buffer). Otherwise, return nil.
2057 ;;
2058 ;; This function is almost the same as `c-literal-limits'. It differs in
2059 ;; that it is a lower level function, and that it rigourously follows the
2060 ;; syntax from BOB, whereas `c-literal-limits' uses a "local" safe position.
2061 (save-restriction
2062 (widen)
2063 (save-excursion
2064 (let ((c c-state-nonlit-pos-cache)
2065 pos npos lit)
2066 ;; Trim the cache to take account of buffer changes.
2067 (while (and c (> (car c) c-state-nonlit-pos-cache-limit))
2068 (setq c (cdr c)))
2069 (setq c-state-nonlit-pos-cache c)
2070
2071 (while (and c (> (car c) here))
2072 (setq c (cdr c)))
2073 (setq pos (or (car c) (point-min)))
2074
2075 (while (<= (setq npos (+ pos c-state-nonlit-pos-interval))
2076 here)
2077 (setq lit (c-state-pp-to-literal pos npos))
2078 (setq pos (or (cdr lit) npos)) ; end of literal containing npos.
2079 (setq c-state-nonlit-pos-cache (cons pos c-state-nonlit-pos-cache)))
2080
2081 (if (> pos c-state-nonlit-pos-cache-limit)
2082 (setq c-state-nonlit-pos-cache-limit pos))
2083 (if (< pos here)
2084 (setq lit (c-state-pp-to-literal pos here)))
2085 lit))))
2086
2087 (defsubst c-state-lit-beg (pos)
2088 ;; Return the start of the literal containing POS, or POS itself.
2089 (or (car (c-state-literal-at pos))
2090 pos))
2091
2092 (defsubst c-state-cache-non-literal-place (pos state)
2093 ;; Return a position outside of a string/comment at or before POS.
2094 ;; STATE is the parse-partial-sexp state at POS.
2095 (if (or (nth 3 state) ; in a string?
2096 (nth 4 state)) ; in a comment?
2097 (nth 8 state)
2098 pos))
2099
2100
2101 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2102 ;; Stuff to do with point-min, and coping with any literal there.
2103 (defvar c-state-point-min 1)
2104 (make-variable-buffer-local 'c-state-point-min)
2105 ;; This is (point-min) when `c-state-cache' was last calculated. A change of
2106 ;; narrowing is likely to affect the parens that are visible before the point.
2107
2108 (defvar c-state-point-min-lit-type nil)
2109 (make-variable-buffer-local 'c-state-point-min-lit-type)
2110 (defvar c-state-point-min-lit-start nil)
2111 (make-variable-buffer-local 'c-state-point-min-lit-start)
2112 ;; These two variables define the literal, if any, containing point-min.
2113 ;; Their values are, respectively, 'string, c, or c++, and the start of the
2114 ;; literal. If there's no literal there, they're both nil.
2115
2116 (defvar c-state-min-scan-pos 1)
2117 (make-variable-buffer-local 'c-state-min-scan-pos)
2118 ;; This is the earliest buffer-pos from which scanning can be done. It is
2119 ;; either the end of the literal containing point-min, or point-min itself.
2120 ;; It becomes nil if the buffer is changed earlier than this point.
2121 (defun c-state-get-min-scan-pos ()
2122 ;; Return the lowest valid scanning pos. This will be the end of the
2123 ;; literal enclosing point-min, or point-min itself.
2124 (or c-state-min-scan-pos
2125 (save-restriction
2126 (save-excursion
2127 (widen)
2128 (goto-char c-state-point-min-lit-start)
2129 (if (eq c-state-point-min-lit-type 'string)
2130 (forward-sexp)
2131 (forward-comment 1))
2132 (setq c-state-min-scan-pos (point))))))
2133
2134 (defun c-state-mark-point-min-literal ()
2135 ;; Determine the properties of any literal containing POINT-MIN, setting the
2136 ;; variables `c-state-point-min-lit-type', `c-state-point-min-lit-start',
2137 ;; and `c-state-min-scan-pos' accordingly. The return value is meaningless.
2138 (let ((p-min (point-min))
2139 lit)
2140 (save-restriction
2141 (widen)
2142 (setq lit (c-state-literal-at p-min))
2143 (if lit
2144 (setq c-state-point-min-lit-type
2145 (save-excursion
2146 (goto-char (car lit))
2147 (cond
2148 ((looking-at c-block-comment-start-regexp) 'c)
2149 ((looking-at c-line-comment-starter) 'c++)
2150 (t 'string)))
2151 c-state-point-min-lit-start (car lit)
2152 c-state-min-scan-pos (cdr lit))
2153 (setq c-state-point-min-lit-type nil
2154 c-state-point-min-lit-start nil
2155 c-state-min-scan-pos p-min)))))
2156
2157
2158 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2159 ;; A variable which signals a brace dessert - helpful for reducing the number
2160 ;; of fruitless backward scans.
2161 (defvar c-state-brace-pair-desert nil)
2162 (make-variable-buffer-local 'c-state-brace-pair-desert)
2163 ;; Used only in `c-append-lower-brace-pair-to-state-cache'. It is set when an
2164 ;; that defun has searched backwards for a brace pair and not found one. Its
2165 ;; value is either nil or a cons (PA . FROM), where PA is the position of the
2166 ;; enclosing opening paren/brace/bracket which bounds the backwards search (or
2167 ;; nil when at top level) and FROM is where the backward search started. It
2168 ;; is reset to nil in `c-invalidate-state-cache'.
2169
2170
2171 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2172 ;; Lowish level functions/macros which work directly on `c-state-cache', or a
2173 ;; list of like structure.
2174 (defmacro c-state-cache-top-lparen (&optional cache)
2175 ;; Return the address of the top left brace/bracket/paren recorded in CACHE
2176 ;; (default `c-state-cache') (or nil).
2177 (let ((cash (or cache 'c-state-cache)))
2178 `(if (consp (car ,cash))
2179 (caar ,cash)
2180 (car ,cash))))
2181
2182 (defmacro c-state-cache-top-paren (&optional cache)
2183 ;; Return the address of the latest brace/bracket/paren (whether left or
2184 ;; right) recorded in CACHE (default `c-state-cache') or nil.
2185 (let ((cash (or cache 'c-state-cache)))
2186 `(if (consp (car ,cash))
2187 (cdar ,cash)
2188 (car ,cash))))
2189
2190 (defmacro c-state-cache-after-top-paren (&optional cache)
2191 ;; Return the position just after the latest brace/bracket/paren (whether
2192 ;; left or right) recorded in CACHE (default `c-state-cache') or nil.
2193 (let ((cash (or cache 'c-state-cache)))
2194 `(if (consp (car ,cash))
2195 (cdar ,cash)
2196 (and (car ,cash)
2197 (1+ (car ,cash))))))
2198
2199 (defun c-get-cache-scan-pos (here)
2200 ;; From the state-cache, determine the buffer position from which we might
2201 ;; scan forward to HERE to update this cache. This position will be just
2202 ;; after a paren/brace/bracket recorded in the cache, if possible, otherwise
2203 ;; return the earliest position in the accessible region which isn't within
2204 ;; a literal. If the visible portion of the buffer is entirely within a
2205 ;; literal, return NIL.
2206 (let ((c c-state-cache) elt)
2207 ;(while (>= (or (c-state-cache-top-lparen c) 1) here)
2208 (while (and c
2209 (>= (c-state-cache-top-lparen c) here))
2210 (setq c (cdr c)))
2211
2212 (setq elt (car c))
2213 (cond
2214 ((consp elt)
2215 (if (> (cdr elt) here)
2216 (1+ (car elt))
2217 (cdr elt)))
2218 (elt (1+ elt))
2219 ((<= (c-state-get-min-scan-pos) here)
2220 (c-state-get-min-scan-pos))
2221 (t nil))))
2222
2223 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2224 ;; Variables which keep track of preprocessor constructs.
2225 (defvar c-state-old-cpp-beg nil)
2226 (make-variable-buffer-local 'c-state-old-cpp-beg)
2227 (defvar c-state-old-cpp-end nil)
2228 (make-variable-buffer-local 'c-state-old-cpp-end)
2229 ;; These are the limits of the macro containing point at the previous call of
2230 ;; `c-parse-state', or nil.
2231
2232 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2233 ;; Defuns which analyse the buffer, yet don't change `c-state-cache'.
2234 (defun c-get-fallback-scan-pos (here)
2235 ;; Return a start position for building `c-state-cache' from
2236 ;; scratch. This will be at the top level, 2 defuns back.
2237 (save-excursion
2238 ;; Go back 2 bods, but ignore any bogus positions returned by
2239 ;; beginning-of-defun (i.e. open paren in column zero).
2240 (goto-char here)
2241 (let ((cnt 2))
2242 (while (not (or (bobp) (zerop cnt)))
2243 (c-beginning-of-defun-1) ; Pure elisp BOD.
2244 (if (eq (char-after) ?\{)
2245 (setq cnt (1- cnt)))))
2246 (point)))
2247
2248 (defun c-state-balance-parens-backwards (here top)
2249 ;; Return the position of the opening paren/brace/bracket before HERE which
2250 ;; matches the outermost close p/b/b between HERE and TOP, like this:
2251 ;;
2252 ;; ......................................
2253 ;; | |
2254 ;; ( [ ( ........... ) ( ) ] )
2255 ;; ^ ^ ^
2256 ;; | | |
2257 ;; return HERE TOP
2258 ;;
2259 ;; If there aren't enough opening paren/brace/brackets, return the position
2260 ;; of the outermost one found, or HERE it there are none. If there are no
2261 ;; closeing p/b/bs between HERE and TOP, return HERE. HERE and TOP must not
2262 ;; be inside literals. Only the accessible portion of the buffer will be
2263 ;; scanned.
2264
2265 ;; PART 1: scan from `here' up to `top', accumulating ")"s which enclose
2266 ;; `here'. Go round the next loop each time we pass over such a ")". These
2267 ;; probably match "("s before `here'.
2268 (let (pos pa ren+1 lonely-rens)
2269 (save-excursion
2270 (save-restriction
2271 (narrow-to-region (point-min) top) ; This can move point, sometimes.
2272 (setq pos here)
2273 (c-safe
2274 (while
2275 (setq ren+1 (scan-lists pos 1 1)) ; might signal
2276 (setq lonely-rens (cons ren+1 lonely-rens)
2277 pos ren+1)))))
2278
2279 ;; PART 2: Scan back before `here' searching for the "("s
2280 ;; matching/mismatching the ")"s found above. We only need to direct the
2281 ;; caller to scan when we've encountered unmatched right parens.
2282 (when lonely-rens
2283 (setq pos here)
2284 (c-safe
2285 (while
2286 (and lonely-rens ; actual values aren't used.
2287 (setq pa (scan-lists pos -1 1)))
2288 (setq pos pa)
2289 (setq lonely-rens (cdr lonely-rens)))) ;)
2290 )
2291 pos))
2292
2293 (defun c-parse-state-get-strategy (here good-pos)
2294 ;; Determine the scanning strategy for adjusting `c-parse-state', attempting
2295 ;; to minimise the amount of scanning. HERE is the pertinent position in
2296 ;; the buffer, GOOD-POS is a position where `c-state-cache' (possibly with
2297 ;; its head trimmed) is known to be good, or nil if there is no such
2298 ;; position.
2299 ;;
2300 ;; The return value is a list, one of the following:
2301 ;;
2302 ;; o - ('forward CACHE-POS START-POINT) - scan forward from START-POINT,
2303 ;; which is not less than CACHE-POS.
2304 ;; o - ('backward CACHE-POS nil) - scan backwards (from HERE).
2305 ;; o - ('BOD nil START-POINT) - scan forwards from START-POINT, which is at the
2306 ;; top level.
2307 ;; o - ('IN-LIT nil nil) - point is inside the literal containing point-min.
2308 ;; , where CACHE-POS is the highest position recorded in `c-state-cache' at
2309 ;; or below HERE.
2310 (let ((cache-pos (c-get-cache-scan-pos here)) ; highest position below HERE in cache (or 1)
2311 BOD-pos ; position of 2nd BOD before HERE.
2312 strategy ; 'forward, 'backward, 'BOD, or 'IN-LIT.
2313 start-point
2314 how-far) ; putative scanning distance.
2315 (setq good-pos (or good-pos (c-state-get-min-scan-pos)))
2316 (cond
2317 ((< here (c-state-get-min-scan-pos))
2318 (setq strategy 'IN-LIT
2319 start-point nil
2320 cache-pos nil
2321 how-far 0))
2322 ((<= good-pos here)
2323 (setq strategy 'forward
2324 start-point (max good-pos cache-pos)
2325 how-far (- here start-point)))
2326 ((< (- good-pos here) (- here cache-pos)) ; FIXME!!! ; apply some sort of weighting.
2327 (setq strategy 'backward
2328 how-far (- good-pos here)))
2329 (t
2330 (setq strategy 'forward
2331 how-far (- here cache-pos)
2332 start-point cache-pos)))
2333
2334 ;; Might we be better off starting from the top level, two defuns back,
2335 ;; instead?
2336 (when (> how-far c-state-cache-too-far)
2337 (setq BOD-pos (c-get-fallback-scan-pos here)) ; somewhat EXPENSIVE!!!
2338 (if (< (- here BOD-pos) how-far)
2339 (setq strategy 'BOD
2340 start-point BOD-pos)))
2341
2342 (list
2343 strategy
2344 (and (memq strategy '(forward backward)) cache-pos)
2345 (and (memq strategy '(forward BOD)) start-point))))
2346
2347
2348 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2349 ;; Routines which change `c-state-cache' and associated values.
2350 (defun c-renarrow-state-cache ()
2351 ;; The region (more precisely, point-min) has changed since we
2352 ;; calculated `c-state-cache'. Amend `c-state-cache' accordingly.
2353 (if (< (point-min) c-state-point-min)
2354 ;; If point-min has MOVED BACKWARDS then we drop the state completely.
2355 ;; It would be possible to do a better job here and recalculate the top
2356 ;; only.
2357 (progn
2358 (c-state-mark-point-min-literal)
2359 (setq c-state-cache nil
2360 c-state-cache-good-pos c-state-min-scan-pos
2361 c-state-brace-pair-desert nil))
2362
2363 ;; point-min has MOVED FORWARD.
2364
2365 ;; Is the new point-min inside a (different) literal?
2366 (unless (and c-state-point-min-lit-start ; at prev. point-min
2367 (< (point-min) (c-state-get-min-scan-pos)))
2368 (c-state-mark-point-min-literal))
2369
2370 ;; Cut off a bit of the tail from `c-state-cache'.
2371 (let ((ptr (cons nil c-state-cache))
2372 pa)
2373 (while (and (setq pa (c-state-cache-top-lparen (cdr ptr)))
2374 (>= pa (point-min)))
2375 (setq ptr (cdr ptr)))
2376
2377 (when (consp ptr)
2378 (if (eq (cdr ptr) c-state-cache)
2379 (setq c-state-cache nil
2380 c-state-cache-good-pos c-state-min-scan-pos)
2381 (setcdr ptr nil)
2382 (setq c-state-cache-good-pos (1+ (c-state-cache-top-lparen))))
2383 )))
2384
2385 (setq c-state-point-min (point-min)))
2386
2387 (defun c-append-lower-brace-pair-to-state-cache (from &optional upper-lim)
2388 ;; If there is a brace pair preceding FROM in the buffer (not necessarily
2389 ;; immediately preceding), push a cons onto `c-state-cache' to represent it.
2390 ;; FROM must not be inside a literal. If UPPER-LIM is non-nil, we append
2391 ;; the highest brace pair whose "}" is below UPPER-LIM.
2392 ;;
2393 ;; Return non-nil when this has been done.
2394 ;;
2395 ;; This routine should be fast. Since it can get called a LOT, we maintain
2396 ;; `c-state-brace-pair-desert', a small cache of "failures", such that we
2397 ;; reduce the time wasted in repeated fruitless searches in brace deserts.
2398 (save-excursion
2399 (save-restriction
2400 (let ((bra from) ce ; Positions of "{" and "}".
2401 new-cons
2402 (cache-pos (c-state-cache-top-lparen)) ; might be nil.
2403 (macro-start-or-from
2404 (progn (goto-char from)
2405 (c-beginning-of-macro)
2406 (point))))
2407 (or upper-lim (setq upper-lim from))
2408
2409 ;; If we're essentially repeating a fruitless search, just give up.
2410 (unless (and c-state-brace-pair-desert
2411 (eq cache-pos (car c-state-brace-pair-desert))
2412 (<= from (cdr c-state-brace-pair-desert)))
2413 ;; Only search what we absolutely need to:
2414 (if (and c-state-brace-pair-desert
2415 (> from (cdr c-state-brace-pair-desert)))
2416 (narrow-to-region (cdr c-state-brace-pair-desert) (point-max)))
2417
2418 ;; In the next pair of nested loops, the inner one moves back past a
2419 ;; pair of (mis-)matching parens or brackets; the outer one moves
2420 ;; back over a sequence of unmatched close brace/paren/bracket each
2421 ;; time round.
2422 (while
2423 (progn
2424 (c-safe
2425 (while
2426 (and (setq ce (scan-lists bra -1 -1)) ; back past )/]/}; might signal
2427 (setq bra (scan-lists ce -1 1)) ; back past (/[/{; might signal
2428 (or (> ce upper-lim)
2429 (not (eq (char-after bra) ?\{))
2430 (and (goto-char bra)
2431 (c-beginning-of-macro)
2432 (< (point) macro-start-or-from))))))
2433 (and ce (< ce bra)))
2434 (setq bra ce)) ; If we just backed over an unbalanced closing
2435 ; brace, ignore it.
2436
2437 (if (and ce (< bra ce) (eq (char-after bra) ?\{))
2438 ;; We've found the desired brace-pair.
2439 (progn
2440 (setq new-cons (cons bra (1+ ce)))
2441 (cond
2442 ((consp (car c-state-cache))
2443 (setcar c-state-cache new-cons))
2444 ((and (numberp (car c-state-cache)) ; probably never happens
2445 (< ce (car c-state-cache)))
2446 (setcdr c-state-cache
2447 (cons new-cons (cdr c-state-cache))))
2448 (t (setq c-state-cache (cons new-cons c-state-cache)))))
2449
2450 ;; We haven't found a brace pair. Record this.
2451 (setq c-state-brace-pair-desert (cons cache-pos from))))))))
2452
2453 (defsubst c-state-push-any-brace-pair (bra+1 macro-start-or-here)
2454 ;; If BRA+1 is nil, do nothing. Otherwise, BRA+1 is the buffer position
2455 ;; following a {, and that brace has a (mis-)matching } (or ]), and we
2456 ;; "push" "a" brace pair onto `c-state-cache'.
2457 ;;
2458 ;; Here "push" means overwrite the top element if it's itself a brace-pair,
2459 ;; otherwise push it normally.
2460 ;;
2461 ;; The brace pair we push is normally the one surrounding BRA+1, but if the
2462 ;; latter is inside a macro, not being a macro containing
2463 ;; MACRO-START-OR-HERE, we scan backwards through the buffer for a non-macro
2464 ;; base pair. This latter case is assumed to be rare.
2465 ;;
2466 ;; Note: POINT is not preserved in this routine.
2467 (if bra+1
2468 (if (or (> bra+1 macro-start-or-here)
2469 (progn (goto-char bra+1)
2470 (not (c-beginning-of-macro))))
2471 (setq c-state-cache
2472 (cons (cons (1- bra+1)
2473 (scan-lists bra+1 1 1))
2474 (if (consp (car c-state-cache))
2475 (cdr c-state-cache)
2476 c-state-cache)))
2477 ;; N.B. This defsubst codes one method for the simple, normal case,
2478 ;; and a more sophisticated, slower way for the general case. Don't
2479 ;; eliminate this defsubst - it's a speed optimisation.
2480 (c-append-lower-brace-pair-to-state-cache (1- bra+1)))))
2481
2482 (defun c-append-to-state-cache (from)
2483 ;; Scan the buffer from FROM to (point-max), adding elements into
2484 ;; `c-state-cache' for braces etc. Return a candidate for
2485 ;; `c-state-cache-good-pos'.
2486 ;;
2487 ;; FROM must be after the latest brace/paren/bracket in `c-state-cache', if
2488 ;; any. Typically, it is immediately after it. It must not be inside a
2489 ;; literal.
2490 (let ((here-bol (c-point 'bol (point-max)))
2491 (macro-start-or-here
2492 (save-excursion (goto-char (point-max))
2493 (if (c-beginning-of-macro)
2494 (point)
2495 (point-max))))
2496 pa+1 ; pos just after an opening PAren (or brace).
2497 (ren+1 from) ; usually a pos just after an closing paREN etc.
2498 ; Is actually the pos. to scan for a (/{/[ from,
2499 ; which sometimes is after a silly )/}/].
2500 paren+1 ; Pos after some opening or closing paren.
2501 paren+1s ; A list of `paren+1's; used to determine a
2502 ; good-pos.
2503 bra+1 ce+1 ; just after L/R bra-ces.
2504 bra+1s ; list of OLD values of bra+1.
2505 mstart) ; start of a macro.
2506
2507 (save-excursion
2508 ;; Each time round the following loop, we enter a succesively deeper
2509 ;; level of brace/paren nesting. (Except sometimes we "continue at
2510 ;; the existing level".) `pa+1' is a pos inside an opening
2511 ;; brace/paren/bracket, usually just after it.
2512 (while
2513 (progn
2514 ;; Each time round the next loop moves forward over an opening then
2515 ;; a closing brace/bracket/paren. This loop is white hot, so it
2516 ;; plays ugly tricks to go fast. DON'T PUT ANYTHING INTO THIS
2517 ;; LOOP WHICH ISN'T ABSOLUTELY NECESSARY!!! It terminates when a
2518 ;; call of `scan-lists' signals an error, which happens when there
2519 ;; are no more b/b/p's to scan.
2520 (c-safe
2521 (while t
2522 (setq pa+1 (scan-lists ren+1 1 -1) ; Into (/{/[; might signal
2523 paren+1s (cons pa+1 paren+1s))
2524 (setq ren+1 (scan-lists pa+1 1 1)) ; Out of )/}/]; might signal
2525 (if (and (eq (char-before pa+1) ?{)) ; Check for a macro later.
2526 (setq bra+1 pa+1))
2527 (setcar paren+1s ren+1)))
2528
2529 (if (and pa+1 (> pa+1 ren+1))
2530 ;; We've just entered a deeper nesting level.
2531 (progn
2532 ;; Insert the brace pair (if present) and the single open
2533 ;; paren/brace/bracket into `c-state-cache' It cannot be
2534 ;; inside a macro, except one around point, because of what
2535 ;; `c-neutralize-syntax-in-CPP' has done.
2536 (c-state-push-any-brace-pair bra+1 macro-start-or-here)
2537 ;; Insert the opening brace/bracket/paren position.
2538 (setq c-state-cache (cons (1- pa+1) c-state-cache))
2539 ;; Clear admin stuff for the next more nested part of the scan.
2540 (setq ren+1 pa+1 pa+1 nil bra+1 nil bra+1s nil)
2541 t) ; Carry on the loop
2542
2543 ;; All open p/b/b's at this nesting level, if any, have probably
2544 ;; been closed by matching/mismatching ones. We're probably
2545 ;; finished - we just need to check for having found an
2546 ;; unmatched )/}/], which we ignore. Such a )/}/] can't be in a
2547 ;; macro, due the action of `c-neutralize-syntax-in-CPP'.
2548 (c-safe (setq ren+1 (scan-lists ren+1 1 1)))))) ; acts as loop control.
2549
2550 ;; Record the final, innermost, brace-pair if there is one.
2551 (c-state-push-any-brace-pair bra+1 macro-start-or-here)
2552
2553 ;; Determine a good pos
2554 (while (and (setq paren+1 (car paren+1s))
2555 (> (if (> paren+1 macro-start-or-here)
2556 paren+1
2557 (goto-char paren+1)
2558 (setq mstart (and (c-beginning-of-macro)
2559 (point)))
2560 (or mstart paren+1))
2561 here-bol))
2562 (setq paren+1s (cdr paren+1s)))
2563 (cond
2564 ((and paren+1 mstart)
2565 (min paren+1 mstart))
2566 (paren+1)
2567 (t from)))))
2568
2569 (defun c-remove-stale-state-cache (good-pos pps-point)
2570 ;; Remove stale entries from the `c-cache-state', i.e. those which will
2571 ;; not be in it when it is amended for position (point-max).
2572 ;; Additionally, the "outermost" open-brace entry before (point-max)
2573 ;; will be converted to a cons if the matching close-brace is scanned.
2574 ;;
2575 ;; GOOD-POS is a "maximal" "safe position" - there must be no open
2576 ;; parens/braces/brackets between GOOD-POS and (point-max).
2577 ;;
2578 ;; As a second thing, calculate the result of parse-partial-sexp at
2579 ;; PPS-POINT, w.r.t. GOOD-POS. The motivation here is that
2580 ;; `c-state-cache-good-pos' may become PPS-POINT, but the caller may need to
2581 ;; adjust it to get outside a string/comment. (Sorry about this! The code
2582 ;; needs to be FAST).
2583 ;;
2584 ;; Return a list (GOOD-POS SCAN-BACK-POS PPS-STATE), where
2585 ;; o - GOOD-POS is a position where the new value `c-state-cache' is known
2586 ;; to be good (we aim for this to be as high as possible);
2587 ;; o - SCAN-BACK-POS, if not nil, indicates there may be a brace pair
2588 ;; preceding POS which needs to be recorded in `c-state-cache'. It is a
2589 ;; position to scan backwards from.
2590 ;; o - PPS-STATE is the parse-partial-sexp state at PPS-POINT.
2591 (save-restriction
2592 (narrow-to-region 1 (point-max))
2593 (save-excursion
2594 (let* ((in-macro-start ; start of macro containing (point-max) or nil.
2595 (save-excursion
2596 (goto-char (point-max))
2597 (and (c-beginning-of-macro)
2598 (point))))
2599 (good-pos-actual-macro-start ; Start of macro containing good-pos
2600 ; or nil
2601 (and (< good-pos (point-max))
2602 (save-excursion
2603 (goto-char good-pos)
2604 (and (c-beginning-of-macro)
2605 (point)))))
2606 (good-pos-actual-macro-end ; End of this macro, (maybe
2607 ; (point-max)), or nil.
2608 (and good-pos-actual-macro-start
2609 (save-excursion
2610 (goto-char good-pos-actual-macro-start)
2611 (c-end-of-macro)
2612 (point))))
2613 pps-state ; Will be 9 or 10 elements long.
2614 pos
2615 upper-lim ; ,beyond which `c-state-cache' entries are removed
2616 scan-back-pos
2617 pair-beg pps-point-state target-depth)
2618
2619 ;; Remove entries beyond (point-max). Also remove any entries inside
2620 ;; a macro, unless (point-max) is in the same macro.
2621 (setq upper-lim
2622 (if (or (null c-state-old-cpp-beg)
2623 (and (> (point-max) c-state-old-cpp-beg)
2624 (< (point-max) c-state-old-cpp-end)))
2625 (point-max)
2626 (min (point-max) c-state-old-cpp-beg)))
2627 (while (and c-state-cache (>= (c-state-cache-top-lparen) upper-lim))
2628 (setq c-state-cache (cdr c-state-cache)))
2629 ;; If `upper-lim' is inside the last recorded brace pair, remove its
2630 ;; RBrace and indicate we'll need to search backwards for a previous
2631 ;; brace pair.
2632 (when (and c-state-cache
2633 (consp (car c-state-cache))
2634 (> (cdar c-state-cache) upper-lim))
2635 (setcar c-state-cache (caar c-state-cache))
2636 (setq scan-back-pos (car c-state-cache)))
2637
2638 ;; The next loop jumps forward out of a nested level of parens each
2639 ;; time round; the corresponding elements in `c-state-cache' are
2640 ;; removed. `pos' is just after the brace-pair or the open paren at
2641 ;; (car c-state-cache). There can be no open parens/braces/brackets
2642 ;; between `good-pos'/`good-pos-actual-macro-start' and (point-max),
2643 ;; due to the interface spec to this function.
2644 (setq pos (if (and good-pos-actual-macro-end
2645 (not (eq good-pos-actual-macro-start
2646 in-macro-start)))
2647 (1+ good-pos-actual-macro-end) ; get outside the macro as
2648 ; marked by a `category' text property.
2649 good-pos))
2650 (goto-char pos)
2651 (while (and c-state-cache
2652 (< (point) (point-max)))
2653 (cond
2654 ((null pps-state) ; first time through
2655 (setq target-depth -1))
2656 ((eq (car pps-state) target-depth) ; found closing ),},]
2657 (setq target-depth (1- (car pps-state))))
2658 ;; Do nothing when we've merely reached pps-point.
2659 )
2660
2661 ;; Scan!
2662 (setq pps-state
2663 (parse-partial-sexp
2664 (point) (if (< (point) pps-point) pps-point (point-max))
2665 target-depth
2666 nil pps-state))
2667
2668 (if (= (point) pps-point)
2669 (setq pps-point-state pps-state))
2670
2671 (when (eq (car pps-state) target-depth)
2672 (setq pos (point)) ; POS is now just after an R-paren/brace.
2673 (cond
2674 ((and (consp (car c-state-cache))
2675 (eq (point) (cdar c-state-cache)))
2676 ;; We've just moved out of the paren pair containing the brace-pair
2677 ;; at (car c-state-cache). `pair-beg' is where the open paren is,
2678 ;; and is potentially where the open brace of a cons in
2679 ;; c-state-cache will be.
2680 (setq pair-beg (car-safe (cdr c-state-cache))
2681 c-state-cache (cdr-safe (cdr c-state-cache)))) ; remove {}pair + containing Lparen.
2682 ((numberp (car c-state-cache))
2683 (setq pair-beg (car c-state-cache)
2684 c-state-cache (cdr c-state-cache))) ; remove this
2685 ; containing Lparen
2686 ((numberp (cadr c-state-cache))
2687 (setq pair-beg (cadr c-state-cache)
2688 c-state-cache (cddr c-state-cache))) ; Remove a paren pair
2689 ; together with enclosed brace pair.
2690 ;; (t nil) ; Ignore an unmated Rparen.
2691 )))
2692
2693 (if (< (point) pps-point)
2694 (setq pps-state (parse-partial-sexp (point) pps-point
2695 nil nil ; TARGETDEPTH, STOPBEFORE
2696 pps-state)))
2697
2698 ;; If the last paren pair we moved out of was actually a brace pair,
2699 ;; insert it into `c-state-cache'.
2700 (when (and pair-beg (eq (char-after pair-beg) ?{))
2701 (if (consp (car-safe c-state-cache))
2702 (setq c-state-cache (cdr c-state-cache)))
2703 (setq c-state-cache (cons (cons pair-beg pos)
2704 c-state-cache)))
2705
2706 (list pos scan-back-pos pps-state)))))
2707
2708 (defun c-remove-stale-state-cache-backwards (here cache-pos)
2709 ;; Strip stale elements of `c-state-cache' by moving backwards through the
2710 ;; buffer, and inform the caller of the scenario detected.
2711 ;;
2712 ;; HERE is the position we're setting `c-state-cache' for.
2713 ;; CACHE-POS is just after the latest recorded position in `c-state-cache'
2714 ;; before HERE, or a position at or near point-min which isn't in a
2715 ;; literal.
2716 ;;
2717 ;; This function must only be called only when (> `c-state-cache-good-pos'
2718 ;; HERE). Usually the gap between CACHE-POS and HERE is large. It is thus
2719 ;; optimised to eliminate (or minimise) scanning between these two
2720 ;; positions.
2721 ;;
2722 ;; Return a three element list (GOOD-POS SCAN-BACK-POS FWD-FLAG), where:
2723 ;; o - GOOD-POS is a "good position", where `c-state-cache' is valid, or
2724 ;; could become so after missing elements are inserted into
2725 ;; `c-state-cache'. This is JUST AFTER an opening or closing
2726 ;; brace/paren/bracket which is already in `c-state-cache' or just before
2727 ;; one otherwise. exceptionally (when there's no such b/p/b handy) the BOL
2728 ;; before `here''s line, or the start of the literal containing it.
2729 ;; o - SCAN-BACK-POS, if non-nil, indicates there may be a brace pair
2730 ;; preceding POS which isn't recorded in `c-state-cache'. It is a position
2731 ;; to scan backwards from.
2732 ;; o - FWD-FLAG, if non-nil, indicates there may be parens/braces between
2733 ;; POS and HERE which aren't recorded in `c-state-cache'.
2734 ;;
2735 ;; The comments in this defun use "paren" to mean parenthesis or square
2736 ;; bracket (as contrasted with a brace), and "(" and ")" likewise.
2737 ;;
2738 ;; . {..} (..) (..) ( .. { } ) (...) ( .... . ..)
2739 ;; | | | | | |
2740 ;; CP E here D C good
2741 (let ((pos c-state-cache-good-pos)
2742 pa ren ; positions of "(" and ")"
2743 dropped-cons ; whether the last element dropped from `c-state-cache'
2744 ; was a cons (representing a brace-pair)
2745 good-pos ; see above.
2746 lit ; (START . END) of a literal containing some point.
2747 here-lit-start here-lit-end ; bounds of literal containing `here'
2748 ; or `here' itself.
2749 (here-bol (c-point 'bol here))
2750 (too-far-back (max (- here c-state-cache-too-far) 1)))
2751
2752 ;; Remove completely irrelevant entries from `c-state-cache'.
2753 (while (and c-state-cache
2754 (>= (setq pa (c-state-cache-top-lparen)) here))
2755 (setq dropped-cons (consp (car c-state-cache)))
2756 (setq c-state-cache (cdr c-state-cache))
2757 (setq pos pa))
2758 ;; At this stage, (> pos here);
2759 ;; (< (c-state-cache-top-lparen) here) (or is nil).
2760
2761 ;; CASE 1: The top of the cache is a brace pair which now encloses `here'.
2762 ;; As good-pos, return the address. of the "{".
2763 (if (and (consp (car c-state-cache))
2764 (> (cdar c-state-cache) here))
2765 ;; Since we've no knowledge of what's inside these braces, we have no
2766 ;; alternative but to direct the caller to scan the buffer from the
2767 ;; opening brace.
2768 (progn
2769 (setq pos (caar c-state-cache))
2770 (setcar c-state-cache pos)
2771 (list (1+ pos) pos t)) ; return value. We've just converted a brace
2772 ; pair entry into a { entry, so the caller
2773 ; needs to search for a brace pair before the
2774 ; {.
2775
2776 ;; ;; `here' might be inside a literal. Check for this.
2777 (setq lit (c-state-literal-at here)
2778 here-lit-start (or (car lit) here)
2779 here-lit-end (or (cdr lit) here))
2780
2781 ;; `here' might be nested inside any depth of parens (or brackets but
2782 ;; not braces). Scan backwards to find the outermost such opening
2783 ;; paren, if there is one. This will be the scan position to return.
2784 (save-restriction
2785 (narrow-to-region cache-pos (point-max))
2786 (setq pos (c-state-balance-parens-backwards here-lit-end pos)))
2787
2788 (if (< pos here-lit-start)
2789 ;; CASE 2: Address of outermost ( or [ which now encloses `here',
2790 ;; but didn't enclose the (previous) `c-state-cache-good-pos'. If
2791 ;; there is a brace pair preceding this, it will already be in
2792 ;; `c-state-cache', unless there was a brace pair after it,
2793 ;; i.e. there'll only be one to scan for if we've just deleted one.
2794 (list pos (and dropped-cons pos) t) ; Return value.
2795
2796 ;; `here' isn't enclosed in a (previously unrecorded) bracket/paren.
2797 ;; Further forward scanning isn't needed, but we still need to find a
2798 ;; GOOD-POS. Step out of all enclosing "("s on HERE's line.
2799 (save-restriction
2800 (narrow-to-region here-bol (point-max))
2801 (setq pos here-lit-start)
2802 (c-safe (while (setq pa (scan-lists pos -1 1))
2803 (setq pos pa)))) ; might signal
2804 (if (setq ren (c-safe-scan-lists pos -1 -1 too-far-back))
2805 ;; CASE 3: After a }/)/] before `here''s BOL.
2806 (list (1+ ren) (and dropped-cons pos) nil) ; Return value
2807
2808 ;; CASE 4; Best of a bad job: BOL before `here-bol', or beginning of
2809 ;; literal containing it.
2810 (setq good-pos (c-state-lit-beg (c-point 'bopl here-bol)))
2811 (list good-pos (and dropped-cons good-pos) nil))))))
2812
2813
2814 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2815 ;; Externally visible routines.
2816
2817 (defun c-state-cache-init ()
2818 (setq c-state-cache nil
2819 c-state-cache-good-pos 1
2820 c-state-nonlit-pos-cache nil
2821 c-state-nonlit-pos-cache-limit 1
2822 c-state-brace-pair-desert nil
2823 c-state-point-min 1
2824 c-state-point-min-lit-type nil
2825 c-state-point-min-lit-start nil
2826 c-state-min-scan-pos 1
2827 c-state-old-cpp-beg nil
2828 c-state-old-cpp-end nil)
2829 (c-state-mark-point-min-literal))
2830
2831 (defun c-invalidate-state-cache-1 (here)
2832 ;; Invalidate all info on `c-state-cache' that applies to the buffer at HERE
2833 ;; or higher and set `c-state-cache-good-pos' accordingly. The cache is
2834 ;; left in a consistent state.
2835 ;;
2836 ;; This is much like `c-whack-state-after', but it never changes a paren
2837 ;; pair element into an open paren element. Doing that would mean that the
2838 ;; new open paren wouldn't have the required preceding paren pair element.
2839 ;;
2840 ;; This function is called from c-after-change.
2841
2842 ;; The cache of non-literals:
2843 (if (< here c-state-nonlit-pos-cache-limit)
2844 (setq c-state-nonlit-pos-cache-limit here))
2845
2846 ;; `c-state-cache':
2847 ;; Case 1: if `here' is in a literal containing point-min, everything
2848 ;; becomes (or is already) nil.
2849 (if (or (null c-state-cache-good-pos)
2850 (< here (c-state-get-min-scan-pos)))
2851 (setq c-state-cache nil
2852 c-state-cache-good-pos nil
2853 c-state-min-scan-pos nil)
2854
2855 ;;; Truncate `c-state-cache' and set `c-state-cache-good-pos' to a value below
2856 ;;; `here'. To maintain its consistency, we may need to insert a new brace
2857 ;;; pair.
2858 (let ((here-bol (c-point 'bol here))
2859 too-high-pa ; recorded {/(/[ next above here, or nil.
2860 dropped-cons ; was the last removed element a brace pair?
2861 pa)
2862 ;; The easy bit - knock over-the-top bits off `c-state-cache'.
2863 (while (and c-state-cache
2864 (>= (setq pa (c-state-cache-top-paren)) here))
2865 (setq dropped-cons (consp (car c-state-cache))
2866 too-high-pa (c-state-cache-top-lparen)
2867 c-state-cache (cdr c-state-cache)))
2868
2869 ;; Do we need to add in an earlier brace pair, having lopped one off?
2870 (if (and dropped-cons
2871 (< too-high-pa (+ here c-state-cache-too-far)))
2872 (c-append-lower-brace-pair-to-state-cache too-high-pa here-bol))
2873 (setq c-state-cache-good-pos (or (c-state-cache-after-top-paren)
2874 (c-state-get-min-scan-pos)))))
2875
2876 ;; The brace-pair desert marker:
2877 (when (car c-state-brace-pair-desert)
2878 (if (< here (car c-state-brace-pair-desert))
2879 (setq c-state-brace-pair-desert nil)
2880 (if (< here (cdr c-state-brace-pair-desert))
2881 (setcdr c-state-brace-pair-desert here)))))
2882
2883 (defun c-parse-state-1 ()
2884 ;; Find and record all noteworthy parens between some good point earlier in
2885 ;; the file and point. That good point is at least the beginning of the
2886 ;; top-level construct we are in, or the beginning of the preceding
2887 ;; top-level construct if we aren't in one.
2888 ;;
2889 ;; The returned value is a list of the noteworthy parens with the last one
2890 ;; first. If an element in the list is an integer, it's the position of an
2891 ;; open paren (of any type) which has not been closed before the point. If
2892 ;; an element is a cons, it gives the position of a closed BRACE paren
2893 ;; pair[*]; the car is the start brace position and the cdr is the position
2894 ;; following the closing brace. Only the last closed brace paren pair
2895 ;; before each open paren and before the point is recorded, and thus the
2896 ;; state never contains two cons elements in succession. When a close brace
2897 ;; has no matching open brace (e.g., the matching brace is outside the
2898 ;; visible region), it is not represented in the returned value.
2899 ;;
2900 ;; [*] N.B. The close "brace" might be a mismatching close bracket or paren.
2901 ;; This defun explicitly treats mismatching parens/braces/brackets as
2902 ;; matching. It is the open brace which makes it a "brace" pair.
2903 ;;
2904 ;; If POINT is within a macro, open parens and brace pairs within
2905 ;; THIS macro MIGHT be recorded. This depends on whether their
2906 ;; syntactic properties have been suppressed by
2907 ;; `c-neutralize-syntax-in-CPP'. This might need fixing (2008-12-11).
2908 ;;
2909 ;; Currently no characters which are given paren syntax with the
2910 ;; syntax-table property are recorded, i.e. angle bracket arglist
2911 ;; parens are never present here. Note that this might change.
2912 ;;
2913 ;; BUG: This function doesn't cope entirely well with unbalanced
2914 ;; parens in macros. (2008-12-11: this has probably been resolved
2915 ;; by the function `c-neutralize-syntax-in-CPP'.) E.g. in the
2916 ;; following case the brace before the macro isn't balanced with the
2917 ;; one after it:
2918 ;;
2919 ;; {
2920 ;; #define X {
2921 ;; }
2922 ;;
2923 ;; Note to maintainers: this function DOES get called with point
2924 ;; within comments and strings, so don't assume it doesn't!
2925 ;;
2926 ;; This function might do hidden buffer changes.
2927 (let* ((here (point))
2928 (here-bopl (c-point 'bopl))
2929 strategy ; 'forward, 'backward etc..
2930 ;; Candidate positions to start scanning from:
2931 cache-pos ; highest position below HERE already existing in
2932 ; cache (or 1).
2933 good-pos
2934 start-point
2935 bopl-state
2936 res
2937 scan-backward-pos scan-forward-p) ; used for 'backward.
2938 ;; If POINT-MIN has changed, adjust the cache
2939 (unless (= (point-min) c-state-point-min)
2940 (c-renarrow-state-cache))
2941
2942 ;; Strategy?
2943 (setq res (c-parse-state-get-strategy here c-state-cache-good-pos)
2944 strategy (car res)
2945 cache-pos (cadr res)
2946 start-point (nth 2 res))
2947
2948 (when (eq strategy 'BOD)
2949 (setq c-state-cache nil
2950 c-state-cache-good-pos start-point))
2951
2952 ;; SCAN!
2953 (save-restriction
2954 (cond
2955 ((memq strategy '(forward BOD))
2956 (narrow-to-region (point-min) here)
2957 (setq res (c-remove-stale-state-cache start-point here-bopl))
2958 (setq cache-pos (car res)
2959 scan-backward-pos (cadr res)
2960 bopl-state (car (cddr res))) ; will be nil if (< here-bopl
2961 ; start-point)
2962 (if scan-backward-pos
2963 (c-append-lower-brace-pair-to-state-cache scan-backward-pos))
2964 (setq good-pos
2965 (c-append-to-state-cache cache-pos))
2966 (setq c-state-cache-good-pos
2967 (if (and bopl-state
2968 (< good-pos (- here c-state-cache-too-far)))
2969 (c-state-cache-non-literal-place here-bopl bopl-state)
2970 good-pos)))
2971
2972 ((eq strategy 'backward)
2973 (setq res (c-remove-stale-state-cache-backwards here cache-pos)
2974 good-pos (car res)
2975 scan-backward-pos (cadr res)
2976 scan-forward-p (car (cddr res)))
2977 (if scan-backward-pos
2978 (c-append-lower-brace-pair-to-state-cache
2979 scan-backward-pos))
2980 (setq c-state-cache-good-pos
2981 (if scan-forward-p
2982 (progn (narrow-to-region (point-min) here)
2983 (c-append-to-state-cache good-pos))
2984
2985 (c-get-cache-scan-pos good-pos))))
2986
2987 (t ; (eq strategy 'IN-LIT)
2988 (setq c-state-cache nil
2989 c-state-cache-good-pos nil)))))
2990
2991 c-state-cache)
2992
2993 (defun c-invalidate-state-cache (here)
2994 ;; This is a wrapper over `c-invalidate-state-cache-1'.
2995 ;;
2996 ;; It suppresses the syntactic effect of the < and > (template) brackets and
2997 ;; of all parens in preprocessor constructs, except for any such construct
2998 ;; containing point. We can then call `c-invalidate-state-cache-1' without
2999 ;; worrying further about macros and template delimiters.
3000 (c-with-<->-as-parens-suppressed
3001 (if (and c-state-old-cpp-beg
3002 (< c-state-old-cpp-beg here))
3003 (c-with-all-but-one-cpps-commented-out
3004 c-state-old-cpp-beg
3005 (min c-state-old-cpp-end here)
3006 (c-invalidate-state-cache-1 here))
3007 (c-with-cpps-commented-out
3008 (c-invalidate-state-cache-1 here)))))
3009
3010 (defun c-parse-state ()
3011 ;; This is a wrapper over `c-parse-state-1'. See that function for a
3012 ;; description of the functionality and return value.
3013 ;;
3014 ;; It suppresses the syntactic effect of the < and > (template) brackets and
3015 ;; of all parens in preprocessor constructs, except for any such construct
3016 ;; containing point. We can then call `c-parse-state-1' without worrying
3017 ;; further about macros and template delimiters.
3018 (let (here-cpp-beg here-cpp-end)
3019 (save-excursion
3020 (when (c-beginning-of-macro)
3021 (setq here-cpp-beg (point))
3022 (unless
3023 (> (setq here-cpp-end (c-syntactic-end-of-macro))
3024 here-cpp-beg)
3025 (setq here-cpp-beg nil here-cpp-end nil))))
3026 ;; FIXME!!! Put in a `condition-case' here to protect the integrity of the
3027 ;; subsystem.
3028 (prog1
3029 (c-with-<->-as-parens-suppressed
3030 (if (and here-cpp-beg (> here-cpp-end here-cpp-beg))
3031 (c-with-all-but-one-cpps-commented-out
3032 here-cpp-beg here-cpp-end
3033 (c-parse-state-1))
3034 (c-with-cpps-commented-out
3035 (c-parse-state-1))))
3036 (setq c-state-old-cpp-beg (and here-cpp-beg (copy-marker here-cpp-beg t))
3037 c-state-old-cpp-end (and here-cpp-end (copy-marker here-cpp-end t)))
3038 )))
3039
3040 ;; Debug tool to catch cache inconsistencies. This is called from
3041 ;; 000tests.el.
3042 (defvar c-debug-parse-state nil)
3043 (unless (fboundp 'c-real-parse-state)
3044 (fset 'c-real-parse-state (symbol-function 'c-parse-state)))
3045 (cc-bytecomp-defun c-real-parse-state)
3046 (defun c-debug-parse-state ()
3047 (let ((here (point)) (res1 (c-real-parse-state)) res2)
3048 (let ((c-state-cache nil)
3049 (c-state-cache-good-pos 1)
3050 (c-state-nonlit-pos-cache nil)
3051 (c-state-nonlit-pos-cache-limit 1)
3052 (c-state-brace-pair-desert nil)
3053 (c-state-point-min 1)
3054 (c-state-point-min-lit-type nil)
3055 (c-state-point-min-lit-start nil)
3056 (c-state-min-scan-pos 1)
3057 (c-state-old-cpp-beg nil)
3058 (c-state-old-cpp-end nil))
3059 (setq res2 (c-real-parse-state)))
3060 (unless (equal res1 res2)
3061 ;; The cache can actually go further back due to the ad-hoc way
3062 ;; the first paren is found, so try to whack off a bit of its
3063 ;; start before complaining.
3064 (save-excursion
3065 (goto-char (or (c-least-enclosing-brace res2) (point)))
3066 (c-beginning-of-defun-1)
3067 (while (not (or (bobp) (eq (char-after) ?{)))
3068 (c-beginning-of-defun-1))
3069 (unless (equal (c-whack-state-before (point) res1) res2)
3070 (message (concat "c-parse-state inconsistency at %s: "
3071 "using cache: %s, from scratch: %s")
3072 here res1 res2))))
3073 res1))
3074
3075 (defun c-toggle-parse-state-debug (&optional arg)
3076 (interactive "P")
3077 (setq c-debug-parse-state (c-calculate-state arg c-debug-parse-state))
3078 (fset 'c-parse-state (symbol-function (if c-debug-parse-state
3079 'c-debug-parse-state
3080 'c-real-parse-state)))
3081 (c-keep-region-active))
3082 (when c-debug-parse-state
3083 (c-toggle-parse-state-debug 1))
3084
3085 \f
3086 (defun c-whack-state-before (bufpos paren-state)
3087 ;; Whack off any state information from PAREN-STATE which lies
3088 ;; before BUFPOS. Not destructive on PAREN-STATE.
3089 (let* ((newstate (list nil))
3090 (ptr newstate)
3091 car)
3092 (while paren-state
3093 (setq car (car paren-state)
3094 paren-state (cdr paren-state))
3095 (if (< (if (consp car) (car car) car) bufpos)
3096 (setq paren-state nil)
3097 (setcdr ptr (list car))
3098 (setq ptr (cdr ptr))))
3099 (cdr newstate)))
3100
3101 (defun c-whack-state-after (bufpos paren-state)
3102 ;; Whack off any state information from PAREN-STATE which lies at or
3103 ;; after BUFPOS. Not destructive on PAREN-STATE.
3104 (catch 'done
3105 (while paren-state
3106 (let ((car (car paren-state)))
3107 (if (consp car)
3108 ;; just check the car, because in a balanced brace
3109 ;; expression, it must be impossible for the corresponding
3110 ;; close brace to be before point, but the open brace to
3111 ;; be after.
3112 (if (<= bufpos (car car))
3113 nil ; whack it off
3114 (if (< bufpos (cdr car))
3115 ;; its possible that the open brace is before
3116 ;; bufpos, but the close brace is after. In that
3117 ;; case, convert this to a non-cons element. The
3118 ;; rest of the state is before bufpos, so we're
3119 ;; done.
3120 (throw 'done (cons (car car) (cdr paren-state)))
3121 ;; we know that both the open and close braces are
3122 ;; before bufpos, so we also know that everything else
3123 ;; on state is before bufpos.
3124 (throw 'done paren-state)))
3125 (if (<= bufpos car)
3126 nil ; whack it off
3127 ;; it's before bufpos, so everything else should too.
3128 (throw 'done paren-state)))
3129 (setq paren-state (cdr paren-state)))
3130 nil)))
3131
3132 (defun c-most-enclosing-brace (paren-state &optional bufpos)
3133 ;; Return the bufpos of the innermost enclosing open paren before
3134 ;; bufpos, or nil if none was found.
3135 (let (enclosingp)
3136 (or bufpos (setq bufpos 134217727))
3137 (while paren-state
3138 (setq enclosingp (car paren-state)
3139 paren-state (cdr paren-state))
3140 (if (or (consp enclosingp)
3141 (>= enclosingp bufpos))
3142 (setq enclosingp nil)
3143 (setq paren-state nil)))
3144 enclosingp))
3145
3146 (defun c-least-enclosing-brace (paren-state)
3147 ;; Return the bufpos of the outermost enclosing open paren, or nil
3148 ;; if none was found.
3149 (let (pos elem)
3150 (while paren-state
3151 (setq elem (car paren-state)
3152 paren-state (cdr paren-state))
3153 (if (integerp elem)
3154 (setq pos elem)))
3155 pos))
3156
3157 (defun c-safe-position (bufpos paren-state)
3158 ;; Return the closest "safe" position recorded on PAREN-STATE that
3159 ;; is higher up than BUFPOS. Return nil if PAREN-STATE doesn't
3160 ;; contain any. Return nil if BUFPOS is nil, which is useful to
3161 ;; find the closest limit before a given limit that might be nil.
3162 ;;
3163 ;; A "safe" position is a position at or after a recorded open
3164 ;; paren, or after a recorded close paren. The returned position is
3165 ;; thus either the first position after a close brace, or the first
3166 ;; position after an enclosing paren, or at the enclosing paren in
3167 ;; case BUFPOS is immediately after it.
3168 (when bufpos
3169 (let (elem)
3170 (catch 'done
3171 (while paren-state
3172 (setq elem (car paren-state))
3173 (if (consp elem)
3174 (cond ((< (cdr elem) bufpos)
3175 (throw 'done (cdr elem)))
3176 ((< (car elem) bufpos)
3177 ;; See below.
3178 (throw 'done (min (1+ (car elem)) bufpos))))
3179 (if (< elem bufpos)
3180 ;; elem is the position at and not after the opening paren, so
3181 ;; we can go forward one more step unless it's equal to
3182 ;; bufpos. This is useful in some cases avoid an extra paren
3183 ;; level between the safe position and bufpos.
3184 (throw 'done (min (1+ elem) bufpos))))
3185 (setq paren-state (cdr paren-state)))))))
3186
3187 (defun c-beginning-of-syntax ()
3188 ;; This is used for `font-lock-beginning-of-syntax-function'. It
3189 ;; goes to the closest previous point that is known to be outside
3190 ;; any string literal or comment. `c-state-cache' is used if it has
3191 ;; a position in the vicinity.
3192 (let* ((paren-state c-state-cache)
3193 elem
3194
3195 (pos (catch 'done
3196 ;; Note: Similar code in `c-safe-position'. The
3197 ;; difference is that we accept a safe position at
3198 ;; the point and don't bother to go forward past open
3199 ;; parens.
3200 (while paren-state
3201 (setq elem (car paren-state))
3202 (if (consp elem)
3203 (cond ((<= (cdr elem) (point))
3204 (throw 'done (cdr elem)))
3205 ((<= (car elem) (point))
3206 (throw 'done (car elem))))
3207 (if (<= elem (point))
3208 (throw 'done elem)))
3209 (setq paren-state (cdr paren-state)))
3210 (point-min))))
3211
3212 (if (> pos (- (point) 4000))
3213 (goto-char pos)
3214 ;; The position is far back. Try `c-beginning-of-defun-1'
3215 ;; (although we can't be entirely sure it will go to a position
3216 ;; outside a comment or string in current emacsen). FIXME:
3217 ;; Consult `syntax-ppss' here.
3218 (c-beginning-of-defun-1)
3219 (if (< (point) pos)
3220 (goto-char pos)))))
3221
3222 \f
3223 ;; Tools for scanning identifiers and other tokens.
3224
3225 (defun c-on-identifier ()
3226 "Return non-nil if the point is on or directly after an identifier.
3227 Keywords are recognized and not considered identifiers. If an
3228 identifier is detected, the returned value is its starting position.
3229 If an identifier ends at the point and another begins at it \(can only
3230 happen in Pike) then the point for the preceding one is returned.
3231
3232 Note that this function might do hidden buffer changes. See the
3233 comment at the start of cc-engine.el for more info."
3234
3235 ;; FIXME: Shouldn't this function handle "operator" in C++?
3236
3237 (save-excursion
3238 (skip-syntax-backward "w_")
3239
3240 (or
3241
3242 ;; Check for a normal (non-keyword) identifier.
3243 (and (looking-at c-symbol-start)
3244 (not (looking-at c-keywords-regexp))
3245 (point))
3246
3247 (when (c-major-mode-is 'pike-mode)
3248 ;; Handle the `<operator> syntax in Pike.
3249 (let ((pos (point)))
3250 (skip-chars-backward "-!%&*+/<=>^|~[]()")
3251 (and (if (< (skip-chars-backward "`") 0)
3252 t
3253 (goto-char pos)
3254 (eq (char-after) ?\`))
3255 (looking-at c-symbol-key)
3256 (>= (match-end 0) pos)
3257 (point))))
3258
3259 ;; Handle the "operator +" syntax in C++.
3260 (when (and c-overloadable-operators-regexp
3261 (= (c-backward-token-2 0) 0))
3262
3263 (cond ((and (looking-at c-overloadable-operators-regexp)
3264 (or (not c-opt-op-identifier-prefix)
3265 (and (= (c-backward-token-2 1) 0)
3266 (looking-at c-opt-op-identifier-prefix))))
3267 (point))
3268
3269 ((save-excursion
3270 (and c-opt-op-identifier-prefix
3271 (looking-at c-opt-op-identifier-prefix)
3272 (= (c-forward-token-2 1) 0)
3273 (looking-at c-overloadable-operators-regexp)))
3274 (point))))
3275
3276 )))
3277
3278 (defsubst c-simple-skip-symbol-backward ()
3279 ;; If the point is at the end of a symbol then skip backward to the
3280 ;; beginning of it. Don't move otherwise. Return non-nil if point
3281 ;; moved.
3282 ;;
3283 ;; This function might do hidden buffer changes.
3284 (or (< (skip-syntax-backward "w_") 0)
3285 (and (c-major-mode-is 'pike-mode)
3286 ;; Handle the `<operator> syntax in Pike.
3287 (let ((pos (point)))
3288 (if (and (< (skip-chars-backward "-!%&*+/<=>^|~[]()") 0)
3289 (< (skip-chars-backward "`") 0)
3290 (looking-at c-symbol-key)
3291 (>= (match-end 0) pos))
3292 t
3293 (goto-char pos)
3294 nil)))))
3295
3296 (defun c-beginning-of-current-token (&optional back-limit)
3297 ;; Move to the beginning of the current token. Do not move if not
3298 ;; in the middle of one. BACK-LIMIT may be used to bound the
3299 ;; backward search; if given it's assumed to be at the boundary
3300 ;; between two tokens. Return non-nil if the point is moved, nil
3301 ;; otherwise.
3302 ;;
3303 ;; This function might do hidden buffer changes.
3304 (let ((start (point)))
3305 (if (looking-at "\\w\\|\\s_")
3306 (skip-syntax-backward "w_" back-limit)
3307 (when (< (skip-syntax-backward ".()" back-limit) 0)
3308 (while (let ((pos (or (and (looking-at c-nonsymbol-token-regexp)
3309 (match-end 0))
3310 ;; `c-nonsymbol-token-regexp' should always match
3311 ;; since we've skipped backward over punctuator
3312 ;; or paren syntax, but consume one char in case
3313 ;; it doesn't so that we don't leave point before
3314 ;; some earlier incorrect token.
3315 (1+ (point)))))
3316 (if (<= pos start)
3317 (goto-char pos))))))
3318 (< (point) start)))
3319
3320 (defun c-end-of-current-token (&optional back-limit)
3321 ;; Move to the end of the current token. Do not move if not in the
3322 ;; middle of one. BACK-LIMIT may be used to bound the backward
3323 ;; search; if given it's assumed to be at the boundary between two
3324 ;; tokens. Return non-nil if the point is moved, nil otherwise.
3325 ;;
3326 ;; This function might do hidden buffer changes.
3327 (let ((start (point)))
3328 (cond ((< (skip-syntax-backward "w_" (1- start)) 0)
3329 (skip-syntax-forward "w_"))
3330 ((< (skip-syntax-backward ".()" back-limit) 0)
3331 (while (progn
3332 (if (looking-at c-nonsymbol-token-regexp)
3333 (goto-char (match-end 0))
3334 ;; `c-nonsymbol-token-regexp' should always match since
3335 ;; we've skipped backward over punctuator or paren
3336 ;; syntax, but move forward in case it doesn't so that
3337 ;; we don't leave point earlier than we started with.
3338 (forward-char))
3339 (< (point) start)))))
3340 (> (point) start)))
3341
3342 (defconst c-jump-syntax-balanced
3343 (if (memq 'gen-string-delim c-emacs-features)
3344 "\\w\\|\\s_\\|\\s\(\\|\\s\)\\|\\s\"\\|\\s|"
3345 "\\w\\|\\s_\\|\\s\(\\|\\s\)\\|\\s\""))
3346
3347 (defconst c-jump-syntax-unbalanced
3348 (if (memq 'gen-string-delim c-emacs-features)
3349 "\\w\\|\\s_\\|\\s\"\\|\\s|"
3350 "\\w\\|\\s_\\|\\s\""))
3351
3352 (defun c-forward-token-2 (&optional count balanced limit)
3353 "Move forward by tokens.
3354 A token is defined as all symbols and identifiers which aren't
3355 syntactic whitespace \(note that multicharacter tokens like \"==\" are
3356 treated properly). Point is always either left at the beginning of a
3357 token or not moved at all. COUNT specifies the number of tokens to
3358 move; a negative COUNT moves in the opposite direction. A COUNT of 0
3359 moves to the next token beginning only if not already at one. If
3360 BALANCED is true, move over balanced parens, otherwise move into them.
3361 Also, if BALANCED is true, never move out of an enclosing paren.
3362
3363 LIMIT sets the limit for the movement and defaults to the point limit.
3364 The case when LIMIT is set in the middle of a token, comment or macro
3365 is handled correctly, i.e. the point won't be left there.
3366
3367 Return the number of tokens left to move \(positive or negative). If
3368 BALANCED is true, a move over a balanced paren counts as one. Note
3369 that if COUNT is 0 and no appropriate token beginning is found, 1 will
3370 be returned. Thus, a return value of 0 guarantees that point is at
3371 the requested position and a return value less \(without signs) than
3372 COUNT guarantees that point is at the beginning of some token.
3373
3374 Note that this function might do hidden buffer changes. See the
3375 comment at the start of cc-engine.el for more info."
3376
3377 (or count (setq count 1))
3378 (if (< count 0)
3379 (- (c-backward-token-2 (- count) balanced limit))
3380
3381 (let ((jump-syntax (if balanced
3382 c-jump-syntax-balanced
3383 c-jump-syntax-unbalanced))
3384 (last (point))
3385 (prev (point)))
3386
3387 (if (zerop count)
3388 ;; If count is zero we should jump if in the middle of a token.
3389 (c-end-of-current-token))
3390
3391 (save-restriction
3392 (if limit (narrow-to-region (point-min) limit))
3393 (if (/= (point)
3394 (progn (c-forward-syntactic-ws) (point)))
3395 ;; Skip whitespace. Count this as a move if we did in
3396 ;; fact move.
3397 (setq count (max (1- count) 0)))
3398
3399 (if (eobp)
3400 ;; Moved out of bounds. Make sure the returned count isn't zero.
3401 (progn
3402 (if (zerop count) (setq count 1))
3403 (goto-char last))
3404
3405 ;; Use `condition-case' to avoid having the limit tests
3406 ;; inside the loop.
3407 (condition-case nil
3408 (while (and
3409 (> count 0)
3410 (progn
3411 (setq last (point))
3412 (cond ((looking-at jump-syntax)
3413 (goto-char (scan-sexps (point) 1))
3414 t)
3415 ((looking-at c-nonsymbol-token-regexp)
3416 (goto-char (match-end 0))
3417 t)
3418 ;; `c-nonsymbol-token-regexp' above should always
3419 ;; match if there are correct tokens. Try to
3420 ;; widen to see if the limit was set in the
3421 ;; middle of one, else fall back to treating
3422 ;; the offending thing as a one character token.
3423 ((and limit
3424 (save-restriction
3425 (widen)
3426 (looking-at c-nonsymbol-token-regexp)))
3427 nil)
3428 (t
3429 (forward-char)
3430 t))))
3431 (c-forward-syntactic-ws)
3432 (setq prev last
3433 count (1- count)))
3434 (error (goto-char last)))
3435
3436 (when (eobp)
3437 (goto-char prev)
3438 (setq count (1+ count)))))
3439
3440 count)))
3441
3442 (defun c-backward-token-2 (&optional count balanced limit)
3443 "Move backward by tokens.
3444 See `c-forward-token-2' for details."
3445
3446 (or count (setq count 1))
3447 (if (< count 0)
3448 (- (c-forward-token-2 (- count) balanced limit))
3449
3450 (or limit (setq limit (point-min)))
3451 (let ((jump-syntax (if balanced
3452 c-jump-syntax-balanced
3453 c-jump-syntax-unbalanced))
3454 (last (point)))
3455
3456 (if (zerop count)
3457 ;; The count is zero so try to skip to the beginning of the
3458 ;; current token.
3459 (if (> (point)
3460 (progn (c-beginning-of-current-token) (point)))
3461 (if (< (point) limit)
3462 ;; The limit is inside the same token, so return 1.
3463 (setq count 1))
3464
3465 ;; We're not in the middle of a token. If there's
3466 ;; whitespace after the point then we must move backward,
3467 ;; so set count to 1 in that case.
3468 (and (looking-at c-syntactic-ws-start)
3469 ;; If we're looking at a '#' that might start a cpp
3470 ;; directive then we have to do a more elaborate check.
3471 (or (/= (char-after) ?#)
3472 (not c-opt-cpp-prefix)
3473 (save-excursion
3474 (and (= (point)
3475 (progn (beginning-of-line)
3476 (looking-at "[ \t]*")
3477 (match-end 0)))
3478 (or (bobp)
3479 (progn (backward-char)
3480 (not (eq (char-before) ?\\)))))))
3481 (setq count 1))))
3482
3483 ;; Use `condition-case' to avoid having to check for buffer
3484 ;; limits in `backward-char', `scan-sexps' and `goto-char' below.
3485 (condition-case nil
3486 (while (and
3487 (> count 0)
3488 (progn
3489 (c-backward-syntactic-ws)
3490 (backward-char)
3491 (if (looking-at jump-syntax)
3492 (goto-char (scan-sexps (1+ (point)) -1))
3493 ;; This can be very inefficient if there's a long
3494 ;; sequence of operator tokens without any separation.
3495 ;; That doesn't happen in practice, anyway.
3496 (c-beginning-of-current-token))
3497 (>= (point) limit)))
3498 (setq last (point)
3499 count (1- count)))
3500 (error (goto-char last)))
3501
3502 (if (< (point) limit)
3503 (goto-char last))
3504
3505 count)))
3506
3507 (defun c-forward-token-1 (&optional count balanced limit)
3508 "Like `c-forward-token-2' but doesn't treat multicharacter operator
3509 tokens like \"==\" as single tokens, i.e. all sequences of symbol
3510 characters are jumped over character by character. This function is
3511 for compatibility only; it's only a wrapper over `c-forward-token-2'."
3512 (let ((c-nonsymbol-token-regexp "\\s.\\|\\s\(\\|\\s\)"))
3513 (c-forward-token-2 count balanced limit)))
3514
3515 (defun c-backward-token-1 (&optional count balanced limit)
3516 "Like `c-backward-token-2' but doesn't treat multicharacter operator
3517 tokens like \"==\" as single tokens, i.e. all sequences of symbol
3518 characters are jumped over character by character. This function is
3519 for compatibility only; it's only a wrapper over `c-backward-token-2'."
3520 (let ((c-nonsymbol-token-regexp "\\s.\\|\\s\(\\|\\s\)"))
3521 (c-backward-token-2 count balanced limit)))
3522
3523 \f
3524 ;; Tools for doing searches restricted to syntactically relevant text.
3525
3526 (defun c-syntactic-re-search-forward (regexp &optional bound noerror
3527 paren-level not-inside-token
3528 lookbehind-submatch)
3529 "Like `re-search-forward', but only report matches that are found
3530 in syntactically significant text. I.e. matches in comments, macros
3531 or string literals are ignored. The start point is assumed to be
3532 outside any comment, macro or string literal, or else the content of
3533 that region is taken as syntactically significant text.
3534
3535 If PAREN-LEVEL is non-nil, an additional restriction is added to
3536 ignore matches in nested paren sexps. The search will also not go
3537 outside the current list sexp, which has the effect that if the point
3538 should be moved to BOUND when no match is found \(i.e. NOERROR is
3539 neither nil nor t), then it will be at the closing paren if the end of
3540 the current list sexp is encountered first.
3541
3542 If NOT-INSIDE-TOKEN is non-nil, matches in the middle of tokens are
3543 ignored. Things like multicharacter operators and special symbols
3544 \(e.g. \"`()\" in Pike) are handled but currently not floating point
3545 constants.
3546
3547 If LOOKBEHIND-SUBMATCH is non-nil, it's taken as a number of a
3548 subexpression in REGEXP. The end of that submatch is used as the
3549 position to check for syntactic significance. If LOOKBEHIND-SUBMATCH
3550 isn't used or if that subexpression didn't match then the start
3551 position of the whole match is used instead. The \"look behind\"
3552 subexpression is never tested before the starting position, so it
3553 might be a good idea to include \\=\\= as a match alternative in it.
3554
3555 Optimization note: Matches might be missed if the \"look behind\"
3556 subexpression can match the end of nonwhite syntactic whitespace,
3557 i.e. the end of comments or cpp directives. This since the function
3558 skips over such things before resuming the search. It's on the other
3559 hand not safe to assume that the \"look behind\" subexpression never
3560 matches syntactic whitespace.
3561
3562 Bug: Unbalanced parens inside cpp directives are currently not handled
3563 correctly \(i.e. they don't get ignored as they should) when
3564 PAREN-LEVEL is set.
3565
3566 Note that this function might do hidden buffer changes. See the
3567 comment at the start of cc-engine.el for more info."
3568
3569 (or bound (setq bound (point-max)))
3570 (if paren-level (setq paren-level -1))
3571
3572 ;;(message "c-syntactic-re-search-forward %s %s %S" (point) bound regexp)
3573
3574 (let ((start (point))
3575 tmp
3576 ;; Start position for the last search.
3577 search-pos
3578 ;; The `parse-partial-sexp' state between the start position
3579 ;; and the point.
3580 state
3581 ;; The current position after the last state update. The next
3582 ;; `parse-partial-sexp' continues from here.
3583 (state-pos (point))
3584 ;; The position at which to check the state and the state
3585 ;; there. This is separate from `state-pos' since we might
3586 ;; need to back up before doing the next search round.
3587 check-pos check-state
3588 ;; Last position known to end a token.
3589 (last-token-end-pos (point-min))
3590 ;; Set when a valid match is found.
3591 found)
3592
3593 (condition-case err
3594 (while
3595 (and
3596 (progn
3597 (setq search-pos (point))
3598 (re-search-forward regexp bound noerror))
3599
3600 (progn
3601 (setq state (parse-partial-sexp
3602 state-pos (match-beginning 0) paren-level nil state)
3603 state-pos (point))
3604 (if (setq check-pos (and lookbehind-submatch
3605 (or (not paren-level)
3606 (>= (car state) 0))
3607 (match-end lookbehind-submatch)))
3608 (setq check-state (parse-partial-sexp
3609 state-pos check-pos paren-level nil state))
3610 (setq check-pos state-pos
3611 check-state state))
3612
3613 ;; NOTE: If we got a look behind subexpression and get
3614 ;; an insignificant match in something that isn't
3615 ;; syntactic whitespace (i.e. strings or in nested
3616 ;; parentheses), then we can never skip more than a
3617 ;; single character from the match start position
3618 ;; (i.e. `state-pos' here) before continuing the
3619 ;; search. That since the look behind subexpression
3620 ;; might match the end of the insignificant region in
3621 ;; the next search.
3622
3623 (cond
3624 ((elt check-state 7)
3625 ;; Match inside a line comment. Skip to eol. Use
3626 ;; `re-search-forward' instead of `skip-chars-forward' to get
3627 ;; the right bound behavior.
3628 (re-search-forward "[\n\r]" bound noerror))
3629
3630 ((elt check-state 4)
3631 ;; Match inside a block comment. Skip to the '*/'.
3632 (search-forward "*/" bound noerror))
3633
3634 ((and (not (elt check-state 5))
3635 (eq (char-before check-pos) ?/)
3636 (not (c-get-char-property (1- check-pos) 'syntax-table))
3637 (memq (char-after check-pos) '(?/ ?*)))
3638 ;; Match in the middle of the opener of a block or line
3639 ;; comment.
3640 (if (= (char-after check-pos) ?/)
3641 (re-search-forward "[\n\r]" bound noerror)
3642 (search-forward "*/" bound noerror)))
3643
3644 ;; The last `parse-partial-sexp' above might have
3645 ;; stopped short of the real check position if the end
3646 ;; of the current sexp was encountered in paren-level
3647 ;; mode. The checks above are always false in that
3648 ;; case, and since they can do better skipping in
3649 ;; lookbehind-submatch mode, we do them before
3650 ;; checking the paren level.
3651
3652 ((and paren-level
3653 (/= (setq tmp (car check-state)) 0))
3654 ;; Check the paren level first since we're short of the
3655 ;; syntactic checking position if the end of the
3656 ;; current sexp was encountered by `parse-partial-sexp'.
3657 (if (> tmp 0)
3658
3659 ;; Inside a nested paren sexp.
3660 (if lookbehind-submatch
3661 ;; See the NOTE above.
3662 (progn (goto-char state-pos) t)
3663 ;; Skip out of the paren quickly.
3664 (setq state (parse-partial-sexp state-pos bound 0 nil state)
3665 state-pos (point)))
3666
3667 ;; Have exited the current paren sexp.
3668 (if noerror
3669 (progn
3670 ;; The last `parse-partial-sexp' call above
3671 ;; has left us just after the closing paren
3672 ;; in this case, so we can modify the bound
3673 ;; to leave the point at the right position
3674 ;; upon return.
3675 (setq bound (1- (point)))
3676 nil)
3677 (signal 'search-failed (list regexp)))))
3678
3679 ((setq tmp (elt check-state 3))
3680 ;; Match inside a string.
3681 (if (or lookbehind-submatch
3682 (not (integerp tmp)))
3683 ;; See the NOTE above.
3684 (progn (goto-char state-pos) t)
3685 ;; Skip to the end of the string before continuing.
3686 (let ((ender (make-string 1 tmp)) (continue t))
3687 (while (if (search-forward ender bound noerror)
3688 (progn
3689 (setq state (parse-partial-sexp
3690 state-pos (point) nil nil state)
3691 state-pos (point))
3692 (elt state 3))
3693 (setq continue nil)))
3694 continue)))
3695
3696 ((save-excursion
3697 (save-match-data
3698 (c-beginning-of-macro start)))
3699 ;; Match inside a macro. Skip to the end of it.
3700 (c-end-of-macro)
3701 (cond ((<= (point) bound) t)
3702 (noerror nil)
3703 (t (signal 'search-failed (list regexp)))))
3704
3705 ((and not-inside-token
3706 (or (< check-pos last-token-end-pos)
3707 (< check-pos
3708 (save-excursion
3709 (goto-char check-pos)
3710 (save-match-data
3711 (c-end-of-current-token last-token-end-pos))
3712 (setq last-token-end-pos (point))))))
3713 ;; Inside a token.
3714 (if lookbehind-submatch
3715 ;; See the NOTE above.
3716 (goto-char state-pos)
3717 (goto-char (min last-token-end-pos bound))))
3718
3719 (t
3720 ;; A real match.
3721 (setq found t)
3722 nil)))
3723
3724 ;; Should loop to search again, but take care to avoid
3725 ;; looping on the same spot.
3726 (or (/= search-pos (point))
3727 (if (= (point) bound)
3728 (if noerror
3729 nil
3730 (signal 'search-failed (list regexp)))
3731 (forward-char)
3732 t))))
3733
3734 (error
3735 (goto-char start)
3736 (signal (car err) (cdr err))))
3737
3738 ;;(message "c-syntactic-re-search-forward done %s" (or (match-end 0) (point)))
3739
3740 (if found
3741 (progn
3742 (goto-char (match-end 0))
3743 (match-end 0))
3744
3745 ;; Search failed. Set point as appropriate.
3746 (if (eq noerror t)
3747 (goto-char start)
3748 (goto-char bound))
3749 nil)))
3750
3751 (defvar safe-pos-list) ; bound in c-syntactic-skip-backward
3752
3753 (defsubst c-ssb-lit-begin ()
3754 ;; Return the start of the literal point is in, or nil.
3755 ;; We read and write the variables `safe-pos', `safe-pos-list', `state'
3756 ;; bound in the caller.
3757
3758 ;; Use `parse-partial-sexp' from a safe position down to the point to check
3759 ;; if it's outside comments and strings.
3760 (save-excursion
3761 (let ((pos (point)) safe-pos state pps-end-pos)
3762 ;; Pick a safe position as close to the point as possible.
3763 ;;
3764 ;; FIXME: Consult `syntax-ppss' here if our cache doesn't give a good
3765 ;; position.
3766
3767 (while (and safe-pos-list
3768 (> (car safe-pos-list) (point)))
3769 (setq safe-pos-list (cdr safe-pos-list)))
3770 (unless (setq safe-pos (car-safe safe-pos-list))
3771 (setq safe-pos (max (or (c-safe-position
3772 (point) (or c-state-cache
3773 (c-parse-state)))
3774 0)
3775 (point-min))
3776 safe-pos-list (list safe-pos)))
3777
3778 ;; Cache positions along the way to use if we have to back up more. We
3779 ;; cache every closing paren on the same level. If the paren cache is
3780 ;; relevant in this region then we're typically already on the same
3781 ;; level as the target position. Note that we might cache positions
3782 ;; after opening parens in case safe-pos is in a nested list. That's
3783 ;; both uncommon and harmless.
3784 (while (progn
3785 (setq state (parse-partial-sexp
3786 safe-pos pos 0))
3787 (< (point) pos))
3788 (setq safe-pos (point)
3789 safe-pos-list (cons safe-pos safe-pos-list)))
3790
3791 ;; If the state contains the start of the containing sexp we cache that
3792 ;; position too, so that parse-partial-sexp in the next run has a bigger
3793 ;; chance of starting at the same level as the target position and thus
3794 ;; will get more good safe positions into the list.
3795 (if (elt state 1)
3796 (setq safe-pos (1+ (elt state 1))
3797 safe-pos-list (cons safe-pos safe-pos-list)))
3798
3799 (if (or (elt state 3) (elt state 4))
3800 ;; Inside string or comment. Continue search at the
3801 ;; beginning of it.
3802 (elt state 8)))))
3803
3804 (defun c-syntactic-skip-backward (skip-chars &optional limit paren-level)
3805 "Like `skip-chars-backward' but only look at syntactically relevant chars,
3806 i.e. don't stop at positions inside syntactic whitespace or string
3807 literals. Preprocessor directives are also ignored, with the exception
3808 of the one that the point starts within, if any. If LIMIT is given,
3809 it's assumed to be at a syntactically relevant position.
3810
3811 If PAREN-LEVEL is non-nil, the function won't stop in nested paren
3812 sexps, and the search will also not go outside the current paren sexp.
3813 However, if LIMIT or the buffer limit is reached inside a nested paren
3814 then the point will be left at the limit.
3815
3816 Non-nil is returned if the point moved, nil otherwise.
3817
3818 Note that this function might do hidden buffer changes. See the
3819 comment at the start of cc-engine.el for more info."
3820
3821 (let ((start (point))
3822 state-2
3823 ;; A list of syntactically relevant positions in descending
3824 ;; order. It's used to avoid scanning repeatedly over
3825 ;; potentially large regions with `parse-partial-sexp' to verify
3826 ;; each position. Used in `c-ssb-lit-begin'
3827 safe-pos-list
3828 ;; The result from `c-beginning-of-macro' at the start position or the
3829 ;; start position itself if it isn't within a macro. Evaluated on
3830 ;; demand.
3831 start-macro-beg
3832 ;; The earliest position after the current one with the same paren
3833 ;; level. Used only when `paren-level' is set.
3834 lit-beg
3835 (paren-level-pos (point)))
3836
3837 (while
3838 (progn
3839 ;; The next loop "tries" to find the end point each time round,
3840 ;; loops when it hasn't succeeded.
3841 (while
3842 (and
3843 (< (skip-chars-backward skip-chars limit) 0)
3844
3845 (let ((pos (point)) state-2 pps-end-pos)
3846
3847 (cond
3848 ;; Don't stop inside a literal
3849 ((setq lit-beg (c-ssb-lit-begin))
3850 (goto-char lit-beg)
3851 t)
3852
3853 ((and paren-level
3854 (save-excursion
3855 (setq state-2 (parse-partial-sexp
3856 pos paren-level-pos -1)
3857 pps-end-pos (point))
3858 (/= (car state-2) 0)))
3859 ;; Not at the right level.
3860
3861 (if (and (< (car state-2) 0)
3862 ;; We stop above if we go out of a paren.
3863 ;; Now check whether it precedes or is
3864 ;; nested in the starting sexp.
3865 (save-excursion
3866 (setq state-2
3867 (parse-partial-sexp
3868 pps-end-pos paren-level-pos
3869 nil nil state-2))
3870 (< (car state-2) 0)))
3871
3872 ;; We've stopped short of the starting position
3873 ;; so the hit was inside a nested list. Go up
3874 ;; until we are at the right level.
3875 (condition-case nil
3876 (progn
3877 (goto-char (scan-lists pos -1
3878 (- (car state-2))))
3879 (setq paren-level-pos (point))
3880 (if (and limit (>= limit paren-level-pos))
3881 (progn
3882 (goto-char limit)
3883 nil)
3884 t))
3885 (error
3886 (goto-char (or limit (point-min)))
3887 nil))
3888
3889 ;; The hit was outside the list at the start
3890 ;; position. Go to the start of the list and exit.
3891 (goto-char (1+ (elt state-2 1)))
3892 nil))
3893
3894 ((c-beginning-of-macro limit)
3895 ;; Inside a macro.
3896 (if (< (point)
3897 (or start-macro-beg
3898 (setq start-macro-beg
3899 (save-excursion
3900 (goto-char start)
3901 (c-beginning-of-macro limit)
3902 (point)))))
3903 t
3904
3905 ;; It's inside the same macro we started in so it's
3906 ;; a relevant match.
3907 (goto-char pos)
3908 nil))))))
3909
3910 (> (point)
3911 (progn
3912 ;; Skip syntactic ws afterwards so that we don't stop at the
3913 ;; end of a comment if `skip-chars' is something like "^/".
3914 (c-backward-syntactic-ws)
3915 (point)))))
3916
3917 ;; We might want to extend this with more useful return values in
3918 ;; the future.
3919 (/= (point) start)))
3920
3921 ;; The following is an alternative implementation of
3922 ;; `c-syntactic-skip-backward' that uses backward movement to keep
3923 ;; track of the syntactic context. It turned out to be generally
3924 ;; slower than the one above which uses forward checks from earlier
3925 ;; safe positions.
3926 ;;
3927 ;;(defconst c-ssb-stop-re
3928 ;; ;; The regexp matching chars `c-syntactic-skip-backward' needs to
3929 ;; ;; stop at to avoid going into comments and literals.
3930 ;; (concat
3931 ;; ;; Match comment end syntax and string literal syntax. Also match
3932 ;; ;; '/' for block comment endings (not covered by comment end
3933 ;; ;; syntax).
3934 ;; "\\s>\\|/\\|\\s\""
3935 ;; (if (memq 'gen-string-delim c-emacs-features)
3936 ;; "\\|\\s|"
3937 ;; "")
3938 ;; (if (memq 'gen-comment-delim c-emacs-features)
3939 ;; "\\|\\s!"
3940 ;; "")))
3941 ;;
3942 ;;(defconst c-ssb-stop-paren-re
3943 ;; ;; Like `c-ssb-stop-re' but also stops at paren chars.
3944 ;; (concat c-ssb-stop-re "\\|\\s(\\|\\s)"))
3945 ;;
3946 ;;(defconst c-ssb-sexp-end-re
3947 ;; ;; Regexp matching the ending syntax of a complex sexp.
3948 ;; (concat c-string-limit-regexp "\\|\\s)"))
3949 ;;
3950 ;;(defun c-syntactic-skip-backward (skip-chars &optional limit paren-level)
3951 ;; "Like `skip-chars-backward' but only look at syntactically relevant chars,
3952 ;;i.e. don't stop at positions inside syntactic whitespace or string
3953 ;;literals. Preprocessor directives are also ignored. However, if the
3954 ;;point is within a comment, string literal or preprocessor directory to
3955 ;;begin with, its contents is treated as syntactically relevant chars.
3956 ;;If LIMIT is given, it limits the backward search and the point will be
3957 ;;left there if no earlier position is found.
3958 ;;
3959 ;;If PAREN-LEVEL is non-nil, the function won't stop in nested paren
3960 ;;sexps, and the search will also not go outside the current paren sexp.
3961 ;;However, if LIMIT or the buffer limit is reached inside a nested paren
3962 ;;then the point will be left at the limit.
3963 ;;
3964 ;;Non-nil is returned if the point moved, nil otherwise.
3965 ;;
3966 ;;Note that this function might do hidden buffer changes. See the
3967 ;;comment at the start of cc-engine.el for more info."
3968 ;;
3969 ;; (save-restriction
3970 ;; (when limit
3971 ;; (narrow-to-region limit (point-max)))
3972 ;;
3973 ;; (let ((start (point)))
3974 ;; (catch 'done
3975 ;; (while (let ((last-pos (point))
3976 ;; (stop-pos (progn
3977 ;; (skip-chars-backward skip-chars)
3978 ;; (point))))
3979 ;;
3980 ;; ;; Skip back over the same region as
3981 ;; ;; `skip-chars-backward' above, but keep to
3982 ;; ;; syntactically relevant positions.
3983 ;; (goto-char last-pos)
3984 ;; (while (and
3985 ;; ;; `re-search-backward' with a single char regexp
3986 ;; ;; should be fast.
3987 ;; (re-search-backward
3988 ;; (if paren-level c-ssb-stop-paren-re c-ssb-stop-re)
3989 ;; stop-pos 'move)
3990 ;;
3991 ;; (progn
3992 ;; (cond
3993 ;; ((looking-at "\\s(")
3994 ;; ;; `paren-level' is set and we've found the
3995 ;; ;; start of the containing paren.
3996 ;; (forward-char)
3997 ;; (throw 'done t))
3998 ;;
3999 ;; ((looking-at c-ssb-sexp-end-re)
4000 ;; ;; We're at the end of a string literal or paren
4001 ;; ;; sexp (if `paren-level' is set).
4002 ;; (forward-char)
4003 ;; (condition-case nil
4004 ;; (c-backward-sexp)
4005 ;; (error
4006 ;; (goto-char limit)
4007 ;; (throw 'done t))))
4008 ;;
4009 ;; (t
4010 ;; (forward-char)
4011 ;; ;; At the end of some syntactic ws or possibly
4012 ;; ;; after a plain '/' operator.
4013 ;; (let ((pos (point)))
4014 ;; (c-backward-syntactic-ws)
4015 ;; (if (= pos (point))
4016 ;; ;; Was a plain '/' operator. Go past it.
4017 ;; (backward-char)))))
4018 ;;
4019 ;; (> (point) stop-pos))))
4020 ;;
4021 ;; ;; Now the point is either at `stop-pos' or at some
4022 ;; ;; position further back if `stop-pos' was at a
4023 ;; ;; syntactically irrelevant place.
4024 ;;
4025 ;; ;; Skip additional syntactic ws so that we don't stop
4026 ;; ;; at the end of a comment if `skip-chars' is
4027 ;; ;; something like "^/".
4028 ;; (c-backward-syntactic-ws)
4029 ;;
4030 ;; (< (point) stop-pos))))
4031 ;;
4032 ;; ;; We might want to extend this with more useful return values
4033 ;; ;; in the future.
4034 ;; (/= (point) start))))
4035
4036 \f
4037 ;; Tools for handling comments and string literals.
4038
4039 (defun c-slow-in-literal (&optional lim detect-cpp)
4040 "Return the type of literal point is in, if any.
4041 The return value is `c' if in a C-style comment, `c++' if in a C++
4042 style comment, `string' if in a string literal, `pound' if DETECT-CPP
4043 is non-nil and in a preprocessor line, or nil if somewhere else.
4044 Optional LIM is used as the backward limit of the search. If omitted,
4045 or nil, `c-beginning-of-defun' is used.
4046
4047 The last point calculated is cached if the cache is enabled, i.e. if
4048 `c-in-literal-cache' is bound to a two element vector.
4049
4050 Note that this function might do hidden buffer changes. See the
4051 comment at the start of cc-engine.el for more info."
4052
4053 (if (and (vectorp c-in-literal-cache)
4054 (= (point) (aref c-in-literal-cache 0)))
4055 (aref c-in-literal-cache 1)
4056 (let ((rtn (save-excursion
4057 (let* ((pos (point))
4058 (lim (or lim (progn
4059 (c-beginning-of-syntax)
4060 (point))))
4061 (state (parse-partial-sexp lim pos)))
4062 (cond
4063 ((elt state 3) 'string)
4064 ((elt state 4) (if (elt state 7) 'c++ 'c))
4065 ((and detect-cpp (c-beginning-of-macro lim)) 'pound)
4066 (t nil))))))
4067 ;; cache this result if the cache is enabled
4068 (if (not c-in-literal-cache)
4069 (setq c-in-literal-cache (vector (point) rtn)))
4070 rtn)))
4071
4072 ;; XEmacs has a built-in function that should make this much quicker.
4073 ;; I don't think we even need the cache, which makes our lives more
4074 ;; complicated anyway. In this case, lim is only used to detect
4075 ;; cpp directives.
4076 ;;
4077 ;; Note that there is a bug in Xemacs's buffer-syntactic-context when used in
4078 ;; conjunction with syntax-table-properties. The bug is present in, e.g.,
4079 ;; Xemacs 21.4.4. It manifested itself thus:
4080 ;;
4081 ;; Starting with an empty AWK Mode buffer, type
4082 ;; /regexp/ {<C-j>
4083 ;; Point gets wrongly left at column 0, rather than being indented to tab-width.
4084 ;;
4085 ;; AWK Mode is designed such that when the first / is typed, it gets the
4086 ;; syntax-table property "string fence". When the second / is typed, BOTH /s
4087 ;; are given the s-t property "string". However, buffer-syntactic-context
4088 ;; fails to take account of the change of the s-t property on the opening / to
4089 ;; "string", and reports that the { is within a string started by the second /.
4090 ;;
4091 ;; The workaround for this is for the AWK Mode initialisation to switch the
4092 ;; defalias for c-in-literal to c-slow-in-literal. This will slow down other
4093 ;; cc-modes in Xemacs whenever an awk-buffer has been initialised.
4094 ;;
4095 ;; (Alan Mackenzie, 2003/4/30).
4096
4097 (defun c-fast-in-literal (&optional lim detect-cpp)
4098 ;; This function might do hidden buffer changes.
4099 (let ((context (buffer-syntactic-context)))
4100 (cond
4101 ((eq context 'string) 'string)
4102 ((eq context 'comment) 'c++)
4103 ((eq context 'block-comment) 'c)
4104 ((and detect-cpp (save-excursion (c-beginning-of-macro lim))) 'pound))))
4105
4106 (defalias 'c-in-literal
4107 (if (fboundp 'buffer-syntactic-context)
4108 'c-fast-in-literal ; XEmacs
4109 'c-slow-in-literal)) ; GNU Emacs
4110
4111 ;; The defalias above isn't enough to shut up the byte compiler.
4112 (cc-bytecomp-defun c-in-literal)
4113
4114 (defun c-literal-limits (&optional lim near not-in-delimiter)
4115 "Return a cons of the beginning and end positions of the comment or
4116 string surrounding point (including both delimiters), or nil if point
4117 isn't in one. If LIM is non-nil, it's used as the \"safe\" position
4118 to start parsing from. If NEAR is non-nil, then the limits of any
4119 literal next to point is returned. \"Next to\" means there's only
4120 spaces and tabs between point and the literal. The search for such a
4121 literal is done first in forward direction. If NOT-IN-DELIMITER is
4122 non-nil, the case when point is inside a starting delimiter won't be
4123 recognized. This only has effect for comments which have starting
4124 delimiters with more than one character.
4125
4126 Note that this function might do hidden buffer changes. See the
4127 comment at the start of cc-engine.el for more info."
4128
4129 (save-excursion
4130 (let* ((pos (point))
4131 (lim (or lim (progn
4132 (c-beginning-of-syntax)
4133 (point))))
4134 (state (parse-partial-sexp lim pos)))
4135
4136 (cond ((elt state 3) ; String.
4137 (goto-char (elt state 8))
4138 (cons (point) (or (c-safe (c-forward-sexp 1) (point))
4139 (point-max))))
4140
4141 ((elt state 4) ; Comment.
4142 (goto-char (elt state 8))
4143 (cons (point) (progn (c-forward-single-comment) (point))))
4144
4145 ((and (not not-in-delimiter)
4146 (not (elt state 5))
4147 (eq (char-before) ?/)
4148 (looking-at "[/*]"))
4149 ;; We're standing in a comment starter.
4150 (backward-char 1)
4151 (cons (point) (progn (c-forward-single-comment) (point))))
4152
4153 (near
4154 (goto-char pos)
4155
4156 ;; Search forward for a literal.
4157 (skip-chars-forward " \t")
4158
4159 (cond
4160 ((looking-at c-string-limit-regexp) ; String.
4161 (cons (point) (or (c-safe (c-forward-sexp 1) (point))
4162 (point-max))))
4163
4164 ((looking-at c-comment-start-regexp) ; Line or block comment.
4165 (cons (point) (progn (c-forward-single-comment) (point))))
4166
4167 (t
4168 ;; Search backward.
4169 (skip-chars-backward " \t")
4170
4171 (let ((end (point)) beg)
4172 (cond
4173 ((save-excursion
4174 (< (skip-syntax-backward c-string-syntax) 0)) ; String.
4175 (setq beg (c-safe (c-backward-sexp 1) (point))))
4176
4177 ((and (c-safe (forward-char -2) t)
4178 (looking-at "*/"))
4179 ;; Block comment. Due to the nature of line
4180 ;; comments, they will always be covered by the
4181 ;; normal case above.
4182 (goto-char end)
4183 (c-backward-single-comment)
4184 ;; If LIM is bogus, beg will be bogus.
4185 (setq beg (point))))
4186
4187 (if beg (cons beg end))))))
4188 ))))
4189
4190 ;; In case external callers use this; it did have a docstring.
4191 (defalias 'c-literal-limits-fast 'c-literal-limits)
4192
4193 (defun c-collect-line-comments (range)
4194 "If the argument is a cons of two buffer positions (such as returned by
4195 `c-literal-limits'), and that range contains a C++ style line comment,
4196 then an extended range is returned that contains all adjacent line
4197 comments (i.e. all comments that starts in the same column with no
4198 empty lines or non-whitespace characters between them). Otherwise the
4199 argument is returned.
4200
4201 Note that this function might do hidden buffer changes. See the
4202 comment at the start of cc-engine.el for more info."
4203
4204 (save-excursion
4205 (condition-case nil
4206 (if (and (consp range) (progn
4207 (goto-char (car range))
4208 (looking-at c-line-comment-starter)))
4209 (let ((col (current-column))
4210 (beg (point))
4211 (bopl (c-point 'bopl))
4212 (end (cdr range)))
4213 ;; Got to take care in the backward direction to handle
4214 ;; comments which are preceded by code.
4215 (while (and (c-backward-single-comment)
4216 (>= (point) bopl)
4217 (looking-at c-line-comment-starter)
4218 (= col (current-column)))
4219 (setq beg (point)
4220 bopl (c-point 'bopl)))
4221 (goto-char end)
4222 (while (and (progn (skip-chars-forward " \t")
4223 (looking-at c-line-comment-starter))
4224 (= col (current-column))
4225 (prog1 (zerop (forward-line 1))
4226 (setq end (point)))))
4227 (cons beg end))
4228 range)
4229 (error range))))
4230
4231 (defun c-literal-type (range)
4232 "Convenience function that given the result of `c-literal-limits',
4233 returns nil or the type of literal that the range surrounds, one
4234 of the symbols 'c, 'c++ or 'string. It's much faster than using
4235 `c-in-literal' and is intended to be used when you need both the
4236 type of a literal and its limits.
4237
4238 Note that this function might do hidden buffer changes. See the
4239 comment at the start of cc-engine.el for more info."
4240
4241 (if (consp range)
4242 (save-excursion
4243 (goto-char (car range))
4244 (cond ((looking-at c-string-limit-regexp) 'string)
4245 ((or (looking-at "//") ; c++ line comment
4246 (and (looking-at "\\s<") ; comment starter
4247 (looking-at "#"))) ; awk comment.
4248 'c++)
4249 (t 'c))) ; Assuming the range is valid.
4250 range))
4251
4252 \f
4253 ;; `c-find-decl-spots' and accompanying stuff.
4254
4255 ;; Variables used in `c-find-decl-spots' to cache the search done for
4256 ;; the first declaration in the last call. When that function starts,
4257 ;; it needs to back up over syntactic whitespace to look at the last
4258 ;; token before the region being searched. That can sometimes cause
4259 ;; moves back and forth over a quite large region of comments and
4260 ;; macros, which would be repeated for each changed character when
4261 ;; we're called during fontification, since font-lock refontifies the
4262 ;; current line for each change. Thus it's worthwhile to cache the
4263 ;; first match.
4264 ;;
4265 ;; `c-find-decl-syntactic-pos' is a syntactically relevant position in
4266 ;; the syntactic whitespace less or equal to some start position.
4267 ;; There's no cached value if it's nil.
4268 ;;
4269 ;; `c-find-decl-match-pos' is the match position if
4270 ;; `c-find-decl-prefix-search' matched before the syntactic whitespace
4271 ;; at `c-find-decl-syntactic-pos', or nil if there's no such match.
4272 (defvar c-find-decl-syntactic-pos nil)
4273 (make-variable-buffer-local 'c-find-decl-syntactic-pos)
4274 (defvar c-find-decl-match-pos nil)
4275 (make-variable-buffer-local 'c-find-decl-match-pos)
4276
4277 (defsubst c-invalidate-find-decl-cache (change-min-pos)
4278 (and c-find-decl-syntactic-pos
4279 (< change-min-pos c-find-decl-syntactic-pos)
4280 (setq c-find-decl-syntactic-pos nil)))
4281
4282 ; (defface c-debug-decl-spot-face
4283 ; '((t (:background "Turquoise")))
4284 ; "Debug face to mark the spots where `c-find-decl-spots' stopped.")
4285 ; (defface c-debug-decl-sws-face
4286 ; '((t (:background "Khaki")))
4287 ; "Debug face to mark the syntactic whitespace between the declaration
4288 ; spots and the preceding token end.")
4289
4290 (defmacro c-debug-put-decl-spot-faces (match-pos decl-pos)
4291 (when (facep 'c-debug-decl-spot-face)
4292 `(c-save-buffer-state ((match-pos ,match-pos) (decl-pos ,decl-pos))
4293 (c-debug-add-face (max match-pos (point-min)) decl-pos
4294 'c-debug-decl-sws-face)
4295 (c-debug-add-face decl-pos (min (1+ decl-pos) (point-max))
4296 'c-debug-decl-spot-face))))
4297 (defmacro c-debug-remove-decl-spot-faces (beg end)
4298 (when (facep 'c-debug-decl-spot-face)
4299 `(c-save-buffer-state ()
4300 (c-debug-remove-face ,beg ,end 'c-debug-decl-spot-face)
4301 (c-debug-remove-face ,beg ,end 'c-debug-decl-sws-face))))
4302
4303 (defmacro c-find-decl-prefix-search ()
4304 ;; Macro used inside `c-find-decl-spots'. It ought to be a defun,
4305 ;; but it contains lots of free variables that refer to things
4306 ;; inside `c-find-decl-spots'. The point is left at `cfd-match-pos'
4307 ;; if there is a match, otherwise at `cfd-limit'.
4308 ;;
4309 ;; This macro might do hidden buffer changes.
4310
4311 '(progn
4312 ;; Find the next property match position if we haven't got one already.
4313 (unless cfd-prop-match
4314 (save-excursion
4315 (while (progn
4316 (goto-char (next-single-property-change
4317 (point) 'c-type nil cfd-limit))
4318 (and (< (point) cfd-limit)
4319 (not (eq (c-get-char-property (1- (point)) 'c-type)
4320 'c-decl-end)))))
4321 (setq cfd-prop-match (point))))
4322
4323 ;; Find the next `c-decl-prefix-or-start-re' match if we haven't
4324 ;; got one already.
4325 (unless cfd-re-match
4326
4327 (if (> cfd-re-match-end (point))
4328 (goto-char cfd-re-match-end))
4329
4330 (while (if (setq cfd-re-match-end
4331 (re-search-forward c-decl-prefix-or-start-re
4332 cfd-limit 'move))
4333
4334 ;; Match. Check if it's inside a comment or string literal.
4335 (c-got-face-at
4336 (if (setq cfd-re-match (match-end 1))
4337 ;; Matched the end of a token preceding a decl spot.
4338 (progn
4339 (goto-char cfd-re-match)
4340 (1- cfd-re-match))
4341 ;; Matched a token that start a decl spot.
4342 (goto-char (match-beginning 0))
4343 (point))
4344 c-literal-faces)
4345
4346 ;; No match. Finish up and exit the loop.
4347 (setq cfd-re-match cfd-limit)
4348 nil)
4349
4350 ;; Skip out of comments and string literals.
4351 (while (progn
4352 (goto-char (next-single-property-change
4353 (point) 'face nil cfd-limit))
4354 (and (< (point) cfd-limit)
4355 (c-got-face-at (point) c-literal-faces)))))
4356
4357 ;; If we matched at the decl start, we have to back up over the
4358 ;; preceding syntactic ws to set `cfd-match-pos' and to catch
4359 ;; any decl spots in the syntactic ws.
4360 (unless cfd-re-match
4361 (c-backward-syntactic-ws)
4362 (setq cfd-re-match (point))))
4363
4364 ;; Choose whichever match is closer to the start.
4365 (if (< cfd-re-match cfd-prop-match)
4366 (setq cfd-match-pos cfd-re-match
4367 cfd-re-match nil)
4368 (setq cfd-match-pos cfd-prop-match
4369 cfd-prop-match nil))
4370
4371 (goto-char cfd-match-pos)
4372
4373 (when (< cfd-match-pos cfd-limit)
4374 ;; Skip forward past comments only so we don't skip macros.
4375 (c-forward-comments)
4376 ;; Set the position to continue at. We can avoid going over
4377 ;; the comments skipped above a second time, but it's possible
4378 ;; that the comment skipping has taken us past `cfd-prop-match'
4379 ;; since the property might be used inside comments.
4380 (setq cfd-continue-pos (if cfd-prop-match
4381 (min cfd-prop-match (point))
4382 (point))))))
4383
4384 (defun c-find-decl-spots (cfd-limit cfd-decl-re cfd-face-checklist cfd-fun)
4385 ;; Call CFD-FUN for each possible spot for a declaration, cast or
4386 ;; label from the point to CFD-LIMIT.
4387 ;;
4388 ;; CFD-FUN is called with point at the start of the spot. It's
4389 ;; passed two arguments: The first is the end position of the token
4390 ;; preceding the spot, or 0 for the implicit match at bob. The
4391 ;; second is a flag that is t when the match is inside a macro. If
4392 ;; CFD-FUN adds `c-decl-end' properties somewhere below the current
4393 ;; spot, it should return non-nil to ensure that the next search
4394 ;; will find them.
4395 ;;
4396 ;; Such a spot is:
4397 ;; o The first token after bob.
4398 ;; o The first token after the end of submatch 1 in
4399 ;; `c-decl-prefix-or-start-re' when that submatch matches.
4400 ;; o The start of each `c-decl-prefix-or-start-re' match when
4401 ;; submatch 1 doesn't match.
4402 ;; o The first token after the end of each occurrence of the
4403 ;; `c-type' text property with the value `c-decl-end', provided
4404 ;; `c-type-decl-end-used' is set.
4405 ;;
4406 ;; Only a spot that match CFD-DECL-RE and whose face is in the
4407 ;; CFD-FACE-CHECKLIST list causes CFD-FUN to be called. The face
4408 ;; check is disabled if CFD-FACE-CHECKLIST is nil.
4409 ;;
4410 ;; If the match is inside a macro then the buffer is narrowed to the
4411 ;; end of it, so that CFD-FUN can investigate the following tokens
4412 ;; without matching something that begins inside a macro and ends
4413 ;; outside it. It's to avoid this work that the CFD-DECL-RE and
4414 ;; CFD-FACE-CHECKLIST checks exist.
4415 ;;
4416 ;; The spots are visited approximately in order from top to bottom.
4417 ;; It's however the positions where `c-decl-prefix-or-start-re'
4418 ;; matches and where `c-decl-end' properties are found that are in
4419 ;; order. Since the spots often are at the following token, they
4420 ;; might be visited out of order insofar as more spots are reported
4421 ;; later on within the syntactic whitespace between the match
4422 ;; positions and their spots.
4423 ;;
4424 ;; It's assumed that comments and strings are fontified in the
4425 ;; searched range.
4426 ;;
4427 ;; This is mainly used in fontification, and so has an elaborate
4428 ;; cache to handle repeated calls from the same start position; see
4429 ;; the variables above.
4430 ;;
4431 ;; All variables in this function begin with `cfd-' to avoid name
4432 ;; collision with the (dynamically bound) variables used in CFD-FUN.
4433 ;;
4434 ;; This function might do hidden buffer changes.
4435
4436 (let ((cfd-start-pos (point))
4437 (cfd-buffer-end (point-max))
4438 ;; The end of the token preceding the decl spot last found
4439 ;; with `c-decl-prefix-or-start-re'. `cfd-limit' if there's
4440 ;; no match.
4441 cfd-re-match
4442 ;; The end position of the last `c-decl-prefix-or-start-re'
4443 ;; match. If this is greater than `cfd-continue-pos', the
4444 ;; next regexp search is started here instead.
4445 (cfd-re-match-end (point-min))
4446 ;; The end of the last `c-decl-end' found by
4447 ;; `c-find-decl-prefix-search'. `cfd-limit' if there's no
4448 ;; match. If searching for the property isn't needed then we
4449 ;; disable it by setting it to `cfd-limit' directly.
4450 (cfd-prop-match (unless c-type-decl-end-used cfd-limit))
4451 ;; The end of the token preceding the decl spot last found by
4452 ;; `c-find-decl-prefix-search'. 0 for the implicit match at
4453 ;; bob. `cfd-limit' if there's no match. In other words,
4454 ;; this is the minimum of `cfd-re-match' and `cfd-prop-match'.
4455 (cfd-match-pos cfd-limit)
4456 ;; The position to continue searching at.
4457 cfd-continue-pos
4458 ;; The position of the last "real" token we've stopped at.
4459 ;; This can be greater than `cfd-continue-pos' when we get
4460 ;; hits inside macros or at `c-decl-end' positions inside
4461 ;; comments.
4462 (cfd-token-pos 0)
4463 ;; The end position of the last entered macro.
4464 (cfd-macro-end 0))
4465
4466 ;; Initialize by finding a syntactically relevant start position
4467 ;; before the point, and do the first `c-decl-prefix-or-start-re'
4468 ;; search unless we're at bob.
4469
4470 (let (start-in-literal start-in-macro syntactic-pos)
4471 ;; Must back up a bit since we look for the end of the previous
4472 ;; statement or declaration, which is earlier than the first
4473 ;; returned match.
4474
4475 (cond
4476 ;; First we need to move to a syntactically relevant position.
4477 ;; Begin by backing out of comment or string literals.
4478 ((and
4479 (when (c-got-face-at (point) c-literal-faces)
4480 ;; Try to use the faces to back up to the start of the
4481 ;; literal. FIXME: What if the point is on a declaration
4482 ;; inside a comment?
4483 (while (and (not (bobp))
4484 (c-got-face-at (1- (point)) c-literal-faces))
4485 (goto-char (previous-single-property-change
4486 (point) 'face nil (point-min))))
4487
4488 ;; XEmacs doesn't fontify the quotes surrounding string
4489 ;; literals.
4490 (and (featurep 'xemacs)
4491 (eq (get-text-property (point) 'face)
4492 'font-lock-string-face)
4493 (not (bobp))
4494 (progn (backward-char)
4495 (not (looking-at c-string-limit-regexp)))
4496 (forward-char))
4497
4498 ;; Don't trust the literal to contain only literal faces
4499 ;; (the font lock package might not have fontified the
4500 ;; start of it at all, for instance) so check that we have
4501 ;; arrived at something that looks like a start or else
4502 ;; resort to `c-literal-limits'.
4503 (unless (looking-at c-literal-start-regexp)
4504 (let ((range (c-literal-limits)))
4505 (if range (goto-char (car range)))))
4506
4507 (setq start-in-literal (point)))
4508
4509 ;; The start is in a literal. If the limit is in the same
4510 ;; one we don't have to find a syntactic position etc. We
4511 ;; only check that if the limit is at or before bonl to save
4512 ;; time; it covers the by far most common case when font-lock
4513 ;; refontifies the current line only.
4514 (<= cfd-limit (c-point 'bonl cfd-start-pos))
4515 (save-excursion
4516 (goto-char cfd-start-pos)
4517 (while (progn
4518 (goto-char (next-single-property-change
4519 (point) 'face nil cfd-limit))
4520 (and (< (point) cfd-limit)
4521 (c-got-face-at (point) c-literal-faces))))
4522 (= (point) cfd-limit)))
4523
4524 ;; Completely inside a literal. Set up variables to trig the
4525 ;; (< cfd-continue-pos cfd-start-pos) case below and it'll
4526 ;; find a suitable start position.
4527 (setq cfd-continue-pos start-in-literal))
4528
4529 ;; Check if the region might be completely inside a macro, to
4530 ;; optimize that like the completely-inside-literal above.
4531 ((save-excursion
4532 (and (= (forward-line 1) 0)
4533 (bolp) ; forward-line has funny behavior at eob.
4534 (>= (point) cfd-limit)
4535 (progn (backward-char)
4536 (eq (char-before) ?\\))))
4537 ;; (Maybe) completely inside a macro. Only need to trig the
4538 ;; (< cfd-continue-pos cfd-start-pos) case below to make it
4539 ;; set things up.
4540 (setq cfd-continue-pos (1- cfd-start-pos)
4541 start-in-macro t))
4542
4543 (t
4544 ;; Back out of any macro so we don't miss any declaration
4545 ;; that could follow after it.
4546 (when (c-beginning-of-macro)
4547 (setq start-in-macro t))
4548
4549 ;; Now we're at a proper syntactically relevant position so we
4550 ;; can use the cache. But first clear it if it applied
4551 ;; further down.
4552 (c-invalidate-find-decl-cache cfd-start-pos)
4553
4554 (setq syntactic-pos (point))
4555 (unless (eq syntactic-pos c-find-decl-syntactic-pos)
4556 ;; Don't have to do this if the cache is relevant here,
4557 ;; typically if the same line is refontified again. If
4558 ;; we're just some syntactic whitespace further down we can
4559 ;; still use the cache to limit the skipping.
4560 (c-backward-syntactic-ws c-find-decl-syntactic-pos))
4561
4562 ;; If we hit `c-find-decl-syntactic-pos' and
4563 ;; `c-find-decl-match-pos' is set then we install the cached
4564 ;; values. If we hit `c-find-decl-syntactic-pos' and
4565 ;; `c-find-decl-match-pos' is nil then we know there's no decl
4566 ;; prefix in the whitespace before `c-find-decl-syntactic-pos'
4567 ;; and so we can continue the search from this point. If we
4568 ;; didn't hit `c-find-decl-syntactic-pos' then we're now in
4569 ;; the right spot to begin searching anyway.
4570 (if (and (eq (point) c-find-decl-syntactic-pos)
4571 c-find-decl-match-pos)
4572 (setq cfd-match-pos c-find-decl-match-pos
4573 cfd-continue-pos syntactic-pos)
4574
4575 (setq c-find-decl-syntactic-pos syntactic-pos)
4576
4577 (when (if (bobp)
4578 ;; Always consider bob a match to get the first
4579 ;; declaration in the file. Do this separately instead of
4580 ;; letting `c-decl-prefix-or-start-re' match bob, so that
4581 ;; regexp always can consume at least one character to
4582 ;; ensure that we won't get stuck in an infinite loop.
4583 (setq cfd-re-match 0)
4584 (backward-char)
4585 (c-beginning-of-current-token)
4586 (< (point) cfd-limit))
4587 ;; Do an initial search now. In the bob case above it's
4588 ;; only done to search for a `c-decl-end' spot.
4589 (c-find-decl-prefix-search))
4590
4591 (setq c-find-decl-match-pos (and (< cfd-match-pos cfd-start-pos)
4592 cfd-match-pos)))))
4593
4594 ;; Advance `cfd-continue-pos' if it's before the start position.
4595 ;; The closest continue position that might have effect at or
4596 ;; after the start depends on what we started in. This also
4597 ;; finds a suitable start position in the special cases when the
4598 ;; region is completely within a literal or macro.
4599 (when (and cfd-continue-pos (< cfd-continue-pos cfd-start-pos))
4600
4601 (cond
4602 (start-in-macro
4603 ;; If we're in a macro then it's the closest preceding token
4604 ;; in the macro. Check this before `start-in-literal',
4605 ;; since if we're inside a literal in a macro, the preceding
4606 ;; token is earlier than any `c-decl-end' spot inside the
4607 ;; literal (comment).
4608 (goto-char (or start-in-literal cfd-start-pos))
4609 ;; The only syntactic ws in macros are comments.
4610 (c-backward-comments)
4611 (backward-char)
4612 (c-beginning-of-current-token))
4613
4614 (start-in-literal
4615 ;; If we're in a comment it can only be the closest
4616 ;; preceding `c-decl-end' position within that comment, if
4617 ;; any. Go back to the beginning of such a property so that
4618 ;; `c-find-decl-prefix-search' will find the end of it.
4619 ;; (Can't stop at the end and install it directly on
4620 ;; `cfd-prop-match' since that variable might be cleared
4621 ;; after `cfd-fun' below.)
4622 ;;
4623 ;; Note that if the literal is a string then the property
4624 ;; search will simply skip to the beginning of it right
4625 ;; away.
4626 (if (not c-type-decl-end-used)
4627 (goto-char start-in-literal)
4628 (goto-char cfd-start-pos)
4629 (while (progn
4630 (goto-char (previous-single-property-change
4631 (point) 'c-type nil start-in-literal))
4632 (and (> (point) start-in-literal)
4633 (not (eq (c-get-char-property (point) 'c-type)
4634 'c-decl-end))))))
4635
4636 (when (= (point) start-in-literal)
4637 ;; Didn't find any property inside the comment, so we can
4638 ;; skip it entirely. (This won't skip past a string, but
4639 ;; that'll be handled quickly by the next
4640 ;; `c-find-decl-prefix-search' anyway.)
4641 (c-forward-single-comment)
4642 (if (> (point) cfd-limit)
4643 (goto-char cfd-limit))))
4644
4645 (t
4646 ;; If we started in normal code, the only match that might
4647 ;; apply before the start is what we already got in
4648 ;; `cfd-match-pos' so we can continue at the start position.
4649 ;; (Note that we don't get here if the first match is below
4650 ;; it.)
4651 (goto-char cfd-start-pos)))
4652
4653 ;; Delete found matches if they are before our new continue
4654 ;; position, so that `c-find-decl-prefix-search' won't back up
4655 ;; to them later on.
4656 (setq cfd-continue-pos (point))
4657 (when (and cfd-re-match (< cfd-re-match cfd-continue-pos))
4658 (setq cfd-re-match nil))
4659 (when (and cfd-prop-match (< cfd-prop-match cfd-continue-pos))
4660 (setq cfd-prop-match nil)))
4661
4662 (if syntactic-pos
4663 ;; This is the normal case and we got a proper syntactic
4664 ;; position. If there's a match then it's always outside
4665 ;; macros and comments, so advance to the next token and set
4666 ;; `cfd-token-pos'. The loop below will later go back using
4667 ;; `cfd-continue-pos' to fix declarations inside the
4668 ;; syntactic ws.
4669 (when (and cfd-match-pos (< cfd-match-pos syntactic-pos))
4670 (goto-char syntactic-pos)
4671 (c-forward-syntactic-ws)
4672 (and cfd-continue-pos
4673 (< cfd-continue-pos (point))
4674 (setq cfd-token-pos (point))))
4675
4676 ;; Have one of the special cases when the region is completely
4677 ;; within a literal or macro. `cfd-continue-pos' is set to a
4678 ;; good start position for the search, so do it.
4679 (c-find-decl-prefix-search)))
4680
4681 ;; Now loop. Round what? (ACM, 2006/7/5). We already got the first match.
4682
4683 (while (progn
4684 (while (and
4685 (< cfd-match-pos cfd-limit)
4686
4687 (or
4688 ;; Kludge to filter out matches on the "<" that
4689 ;; aren't open parens, for the sake of languages
4690 ;; that got `c-recognize-<>-arglists' set.
4691 (and (eq (char-before cfd-match-pos) ?<)
4692 (not (c-get-char-property (1- cfd-match-pos)
4693 'syntax-table)))
4694
4695 ;; If `cfd-continue-pos' is less or equal to
4696 ;; `cfd-token-pos', we've got a hit inside a macro
4697 ;; that's in the syntactic whitespace before the last
4698 ;; "real" declaration we've checked. If they're equal
4699 ;; we've arrived at the declaration a second time, so
4700 ;; there's nothing to do.
4701 (= cfd-continue-pos cfd-token-pos)
4702
4703 (progn
4704 ;; If `cfd-continue-pos' is less than `cfd-token-pos'
4705 ;; we're still searching for declarations embedded in
4706 ;; the syntactic whitespace. In that case we need
4707 ;; only to skip comments and not macros, since they
4708 ;; can't be nested, and that's already been done in
4709 ;; `c-find-decl-prefix-search'.
4710 (when (> cfd-continue-pos cfd-token-pos)
4711 (c-forward-syntactic-ws)
4712 (setq cfd-token-pos (point)))
4713
4714 ;; Continue if the following token fails the
4715 ;; CFD-DECL-RE and CFD-FACE-CHECKLIST checks.
4716 (when (or (>= (point) cfd-limit)
4717 (not (looking-at cfd-decl-re))
4718 (and cfd-face-checklist
4719 (not (c-got-face-at
4720 (point) cfd-face-checklist))))
4721 (goto-char cfd-continue-pos)
4722 t)))
4723
4724 (< (point) cfd-limit))
4725 (c-find-decl-prefix-search))
4726
4727 (< (point) cfd-limit))
4728
4729 (when (and
4730 (>= (point) cfd-start-pos)
4731
4732 (progn
4733 ;; Narrow to the end of the macro if we got a hit inside
4734 ;; one, to avoid recognizing things that start inside the
4735 ;; macro and end outside it.
4736 (when (> cfd-match-pos cfd-macro-end)
4737 ;; Not in the same macro as in the previous round.
4738 (save-excursion
4739 (goto-char cfd-match-pos)
4740 (setq cfd-macro-end
4741 (if (save-excursion (and (c-beginning-of-macro)
4742 (< (point) cfd-match-pos)))
4743 (progn (c-end-of-macro)
4744 (point))
4745 0))))
4746
4747 (if (zerop cfd-macro-end)
4748 t
4749 (if (> cfd-macro-end (point))
4750 (progn (narrow-to-region (point-min) cfd-macro-end)
4751 t)
4752 ;; The matched token was the last thing in the macro,
4753 ;; so the whole match is bogus.
4754 (setq cfd-macro-end 0)
4755 nil))))
4756
4757 (c-debug-put-decl-spot-faces cfd-match-pos (point))
4758 (if (funcall cfd-fun cfd-match-pos (/= cfd-macro-end 0))
4759 (setq cfd-prop-match nil))
4760
4761 (when (/= cfd-macro-end 0)
4762 ;; Restore limits if we did macro narrowment above.
4763 (narrow-to-region (point-min) cfd-buffer-end)))
4764
4765 (goto-char cfd-continue-pos)
4766 (if (= cfd-continue-pos cfd-limit)
4767 (setq cfd-match-pos cfd-limit)
4768 (c-find-decl-prefix-search)))))
4769
4770 \f
4771 ;; A cache for found types.
4772
4773 ;; Buffer local variable that contains an obarray with the types we've
4774 ;; found. If a declaration is recognized somewhere we record the
4775 ;; fully qualified identifier in it to recognize it as a type
4776 ;; elsewhere in the file too. This is not accurate since we do not
4777 ;; bother with the scoping rules of the languages, but in practice the
4778 ;; same name is seldom used as both a type and something else in a
4779 ;; file, and we only use this as a last resort in ambiguous cases (see
4780 ;; `c-forward-decl-or-cast-1').
4781 ;;
4782 ;; Not every type need be in this cache. However, things which have
4783 ;; ceased to be types must be removed from it.
4784 ;;
4785 ;; Template types in C++ are added here too but with the template
4786 ;; arglist replaced with "<>" in references or "<" for the one in the
4787 ;; primary type. E.g. the type "Foo<A,B>::Bar<C>" is stored as
4788 ;; "Foo<>::Bar<". This avoids storing very long strings (since C++
4789 ;; template specs can be fairly sized programs in themselves) and
4790 ;; improves the hit ratio (it's a type regardless of the template
4791 ;; args; it's just not the same type, but we're only interested in
4792 ;; recognizing types, not telling distinct types apart). Note that
4793 ;; template types in references are added here too; from the example
4794 ;; above there will also be an entry "Foo<".
4795 (defvar c-found-types nil)
4796 (make-variable-buffer-local 'c-found-types)
4797
4798 (defsubst c-clear-found-types ()
4799 ;; Clears `c-found-types'.
4800 (setq c-found-types (make-vector 53 0)))
4801
4802 (defun c-add-type (from to)
4803 ;; Add the given region as a type in `c-found-types'. If the region
4804 ;; doesn't match an existing type but there is a type which is equal
4805 ;; to the given one except that the last character is missing, then
4806 ;; the shorter type is removed. That's done to avoid adding all
4807 ;; prefixes of a type as it's being entered and font locked. This
4808 ;; doesn't cover cases like when characters are removed from a type
4809 ;; or added in the middle. We'd need the position of point when the
4810 ;; font locking is invoked to solve this well.
4811 ;;
4812 ;; This function might do hidden buffer changes.
4813 (let ((type (c-syntactic-content from to c-recognize-<>-arglists)))
4814 (unless (intern-soft type c-found-types)
4815 (unintern (substring type 0 -1) c-found-types)
4816 (intern type c-found-types))))
4817
4818 (defun c-unfind-type (name)
4819 ;; Remove the "NAME" from c-found-types, if present.
4820 (unintern name c-found-types))
4821
4822 (defsubst c-check-type (from to)
4823 ;; Return non-nil if the given region contains a type in
4824 ;; `c-found-types'.
4825 ;;
4826 ;; This function might do hidden buffer changes.
4827 (intern-soft (c-syntactic-content from to c-recognize-<>-arglists)
4828 c-found-types))
4829
4830 (defun c-list-found-types ()
4831 ;; Return all the types in `c-found-types' as a sorted list of
4832 ;; strings.
4833 (let (type-list)
4834 (mapatoms (lambda (type)
4835 (setq type-list (cons (symbol-name type)
4836 type-list)))
4837 c-found-types)
4838 (sort type-list 'string-lessp)))
4839
4840 ;; Shut up the byte compiler.
4841 (defvar c-maybe-stale-found-type)
4842
4843 (defun c-trim-found-types (beg end old-len)
4844 ;; An after change function which, in conjunction with the info in
4845 ;; c-maybe-stale-found-type (set in c-before-change), removes a type
4846 ;; from `c-found-types', should this type have become stale. For
4847 ;; example, this happens to "foo" when "foo \n bar();" becomes
4848 ;; "foo(); \n bar();". Such stale types, if not removed, foul up
4849 ;; the fontification.
4850 ;;
4851 ;; Have we, perhaps, added non-ws characters to the front/back of a found
4852 ;; type?
4853 (when (> end beg)
4854 (save-excursion
4855 (when (< end (point-max))
4856 (goto-char end)
4857 (if (and (c-beginning-of-current-token) ; only moves when we started in the middle
4858 (progn (goto-char end)
4859 (c-end-of-current-token)))
4860 (c-unfind-type (buffer-substring-no-properties
4861 end (point)))))
4862 (when (> beg (point-min))
4863 (goto-char beg)
4864 (if (and (c-end-of-current-token) ; only moves when we started in the middle
4865 (progn (goto-char beg)
4866 (c-beginning-of-current-token)))
4867 (c-unfind-type (buffer-substring-no-properties
4868 (point) beg))))))
4869
4870 (if c-maybe-stale-found-type ; e.g. (c-decl-id-start "foo" 97 107 " (* ooka) " "o")
4871 (cond
4872 ;; Changing the amount of (already existing) whitespace - don't do anything.
4873 ((and (c-partial-ws-p beg end)
4874 (or (= beg end) ; removal of WS
4875 (string-match "^[ \t\n\r\f\v]*$" (nth 5 c-maybe-stale-found-type)))))
4876
4877 ;; The syntactic relationship which defined a "found type" has been
4878 ;; destroyed.
4879 ((eq (car c-maybe-stale-found-type) 'c-decl-id-start)
4880 (c-unfind-type (cadr c-maybe-stale-found-type)))
4881 ;; ((eq (car c-maybe-stale-found-type) 'c-decl-type-start) FIXME!!!
4882 )))
4883
4884 \f
4885 ;; Setting and removing syntax properties on < and > in languages (C++
4886 ;; and Java) where they can be template/generic delimiters as well as
4887 ;; their normal meaning of "less/greater than".
4888
4889 ;; Normally, < and > have syntax 'punctuation'. When they are found to
4890 ;; be delimiters, they are marked as such with the category properties
4891 ;; c-<-as-paren-syntax, c->-as-paren-syntax respectively.
4892
4893 ;; STRATEGY:
4894 ;;
4895 ;; It is impossible to determine with certainty whether a <..> pair in
4896 ;; C++ is two comparison operators or is template delimiters, unless
4897 ;; one duplicates a lot of a C++ compiler. For example, the following
4898 ;; code fragment:
4899 ;;
4900 ;; foo (a < b, c > d) ;
4901 ;;
4902 ;; could be a function call with two integer parameters (each a
4903 ;; relational expression), or it could be a constructor for class foo
4904 ;; taking one parameter d of templated type "a < b, c >". They are
4905 ;; somewhat easier to distinguish in Java.
4906 ;;
4907 ;; The strategy now (2010-01) adopted is to mark and unmark < and
4908 ;; > IN MATCHING PAIRS ONLY. [Previously, they were marked
4909 ;; individually when their context so indicated. This gave rise to
4910 ;; intractible problems when one of a matching pair was deleted, or
4911 ;; pulled into a literal.]
4912 ;;
4913 ;; At each buffer change, the syntax-table properties are removed in a
4914 ;; before-change function and reapplied, when needed, in an
4915 ;; after-change function. It is far more important that the
4916 ;; properties get removed when they they are spurious than that they
4917 ;; be present when wanted.
4918 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
4919 (defun c-clear-<-pair-props (&optional pos)
4920 ;; POS (default point) is at a < character. If it is marked with
4921 ;; open paren syntax-table text property, remove the property,
4922 ;; together with the close paren property on the matching > (if
4923 ;; any).
4924 (save-excursion
4925 (if pos
4926 (goto-char pos)
4927 (setq pos (point)))
4928 (when (equal (c-get-char-property (point) 'syntax-table)
4929 c-<-as-paren-syntax)
4930 (with-syntax-table c-no-parens-syntax-table ; ignore unbalanced [,{,(,..
4931 (c-go-list-forward))
4932 (when (equal (c-get-char-property (1- (point)) 'syntax-table)
4933 c->-as-paren-syntax) ; should always be true.
4934 (c-clear-char-property (1- (point)) 'category))
4935 (c-clear-char-property pos 'category))))
4936
4937 (defun c-clear->-pair-props (&optional pos)
4938 ;; POS (default point) is at a > character. If it is marked with
4939 ;; close paren syntax-table property, remove the property, together
4940 ;; with the open paren property on the matching < (if any).
4941 (save-excursion
4942 (if pos
4943 (goto-char pos)
4944 (setq pos (point)))
4945 (when (equal (c-get-char-property (point) 'syntax-table)
4946 c->-as-paren-syntax)
4947 (with-syntax-table c-no-parens-syntax-table ; ignore unbalanced [,{,(,..
4948 (c-go-up-list-backward))
4949 (when (equal (c-get-char-property (point) 'syntax-table)
4950 c-<-as-paren-syntax) ; should always be true.
4951 (c-clear-char-property (point) 'category))
4952 (c-clear-char-property pos 'category))))
4953
4954 (defun c-clear-<>-pair-props (&optional pos)
4955 ;; POS (default point) is at a < or > character. If it has an
4956 ;; open/close paren syntax-table property, remove this property both
4957 ;; from the current character and its partner (which will also be
4958 ;; thusly marked).
4959 (cond
4960 ((eq (char-after) ?\<)
4961 (c-clear-<-pair-props pos))
4962 ((eq (char-after) ?\>)
4963 (c-clear->-pair-props pos))
4964 (t (c-benign-error
4965 "c-clear-<>-pair-props called from wrong position"))))
4966
4967 (defun c-clear-<-pair-props-if-match-after (lim &optional pos)
4968 ;; POS (default point) is at a < character. If it is both marked
4969 ;; with open/close paren syntax-table property, and has a matching >
4970 ;; (also marked) which is after LIM, remove the property both from
4971 ;; the current > and its partner.
4972 (save-excursion
4973 (if pos
4974 (goto-char pos)
4975 (setq pos (point)))
4976 (when (equal (c-get-char-property (point) 'syntax-table)
4977 c-<-as-paren-syntax)
4978 (with-syntax-table c-no-parens-syntax-table ; ignore unbalanced [,{,(,..
4979 (c-go-list-forward))
4980 (when (and (>= (point) lim)
4981 (equal (c-get-char-property (1- (point)) 'syntax-table)
4982 c->-as-paren-syntax)) ; should always be true.
4983 (c-unmark-<->-as-paren (1- (point)))
4984 (c-unmark-<->-as-paren pos)))))
4985
4986 (defun c-clear->-pair-props-if-match-before (lim &optional pos)
4987 ;; POS (default point) is at a > character. If it is both marked
4988 ;; with open/close paren syntax-table property, and has a matching <
4989 ;; (also marked) which is before LIM, remove the property both from
4990 ;; the current < and its partner.
4991 (save-excursion
4992 (if pos
4993 (goto-char pos)
4994 (setq pos (point)))
4995 (when (equal (c-get-char-property (point) 'syntax-table)
4996 c->-as-paren-syntax)
4997 (with-syntax-table c-no-parens-syntax-table ; ignore unbalanced [,{,(,..
4998 (c-go-up-list-backward))
4999 (when (and (<= (point) lim)
5000 (equal (c-get-char-property (point) 'syntax-table)
5001 c-<-as-paren-syntax)) ; should always be true.
5002 (c-unmark-<->-as-paren (point))
5003 (c-unmark-<->-as-paren pos)))))
5004
5005 (defun c-before-change-check-<>-operators (beg end)
5006 ;; Unmark certain pairs of "< .... >" which are currently marked as
5007 ;; template/generic delimiters. (This marking is via syntax-table
5008 ;; text properties).
5009 ;;
5010 ;; These pairs are those which are in the current "statement" (i.e.,
5011 ;; the region between the {, }, or ; before BEG and the one after
5012 ;; END), and which enclose any part of the interval (BEG END).
5013 ;;
5014 ;; Note that in C++ (?and Java), template/generic parens cannot
5015 ;; enclose a brace or semicolon, so we use these as bounds on the
5016 ;; region we must work on.
5017 ;;
5018 ;; This function is called from before-change-functions (via
5019 ;; c-get-state-before-change-functions). Thus the buffer is widened,
5020 ;; and point is undefined, both at entry and exit.
5021 ;;
5022 ;; FIXME!!! This routine ignores the possibility of macros entirely.
5023 ;; 2010-01-29.
5024 (save-excursion
5025 (let ((beg-lit-limits (progn (goto-char beg) (c-literal-limits)))
5026 (end-lit-limits (progn (goto-char end) (c-literal-limits))))
5027 ;; Locate the barrier before the changed region
5028 (goto-char (if beg-lit-limits (car beg-lit-limits) beg))
5029 (c-syntactic-skip-backward "^;{}" (max (- beg 2048) (point-min)))
5030
5031 ;; Remove the syntax-table properties from each pertinent <...> pair.
5032 ;; Firsly, the ones with the < before beg and > after beg.
5033 (while (c-search-forward-char-property 'category 'c-<-as-paren-syntax beg)
5034 (c-clear-<-pair-props-if-match-after beg (1- (point))))
5035
5036 ;; Locate the barrier after END.
5037 (goto-char (if end-lit-limits (cdr end-lit-limits) end))
5038 (c-syntactic-re-search-forward "[;{}]"
5039 (min (+ end 2048) (point-max)) 'end)
5040
5041 ;; Remove syntax-table properties from the remaining pertinent <...>
5042 ;; pairs, those with a > after end and < before end.
5043 (while (c-search-backward-char-property 'category 'c->-as-paren-syntax end)
5044 (c-clear->-pair-props-if-match-before end)))))
5045
5046
5047
5048 (defun c-after-change-check-<>-operators (beg end)
5049 ;; This is called from `after-change-functions' when
5050 ;; c-recognize-<>-arglists' is set. It ensures that no "<" or ">"
5051 ;; chars with paren syntax become part of another operator like "<<"
5052 ;; or ">=".
5053 ;;
5054 ;; This function might do hidden buffer changes.
5055
5056 (save-excursion
5057 (goto-char beg)
5058 (when (or (looking-at "[<>]")
5059 (< (skip-chars-backward "<>") 0))
5060
5061 (goto-char beg)
5062 (c-beginning-of-current-token)
5063 (when (and (< (point) beg)
5064 (looking-at c-<>-multichar-token-regexp)
5065 (< beg (setq beg (match-end 0))))
5066 (while (progn (skip-chars-forward "^<>" beg)
5067 (< (point) beg))
5068 (c-clear-<>-pair-props)
5069 (forward-char))))
5070
5071 (when (< beg end)
5072 (goto-char end)
5073 (when (or (looking-at "[<>]")
5074 (< (skip-chars-backward "<>") 0))
5075
5076 (goto-char end)
5077 (c-beginning-of-current-token)
5078 (when (and (< (point) end)
5079 (looking-at c-<>-multichar-token-regexp)
5080 (< end (setq end (match-end 0))))
5081 (while (progn (skip-chars-forward "^<>" end)
5082 (< (point) end))
5083 (c-clear-<>-pair-props)
5084 (forward-char)))))))
5085
5086
5087 \f
5088 ;; Handling of small scale constructs like types and names.
5089
5090 ;; Dynamically bound variable that instructs `c-forward-type' to also
5091 ;; treat possible types (i.e. those that it normally returns 'maybe or
5092 ;; 'found for) as actual types (and always return 'found for them).
5093 ;; This means that it records them in `c-record-type-identifiers' if
5094 ;; that is set, and that it adds them to `c-found-types'.
5095 (defvar c-promote-possible-types nil)
5096
5097 ;; Dynamically bound variable that instructs `c-forward-<>-arglist' to
5098 ;; mark up successfully parsed arglists with paren syntax properties on
5099 ;; the surrounding angle brackets and with `c-<>-arg-sep' in the
5100 ;; `c-type' property of each argument separating comma.
5101 ;;
5102 ;; Setting this variable also makes `c-forward-<>-arglist' recurse into
5103 ;; all arglists for side effects (i.e. recording types), otherwise it
5104 ;; exploits any existing paren syntax properties to quickly jump to the
5105 ;; end of already parsed arglists.
5106 ;;
5107 ;; Marking up the arglists is not the default since doing that correctly
5108 ;; depends on a proper value for `c-restricted-<>-arglists'.
5109 (defvar c-parse-and-markup-<>-arglists nil)
5110
5111 ;; Dynamically bound variable that instructs `c-forward-<>-arglist' to
5112 ;; not accept arglists that contain binary operators.
5113 ;;
5114 ;; This is primarily used to handle C++ template arglists. C++
5115 ;; disambiguates them by checking whether the preceding name is a
5116 ;; template or not. We can't do that, so we assume it is a template
5117 ;; if it can be parsed as one. That usually works well since
5118 ;; comparison expressions on the forms "a < b > c" or "a < b, c > d"
5119 ;; in almost all cases would be pointless.
5120 ;;
5121 ;; However, in function arglists, e.g. in "foo (a < b, c > d)", we
5122 ;; should let the comma separate the function arguments instead. And
5123 ;; in a context where the value of the expression is taken, e.g. in
5124 ;; "if (a < b || c > d)", it's probably not a template.
5125 (defvar c-restricted-<>-arglists nil)
5126
5127 ;; Dynamically bound variables that instructs
5128 ;; `c-forward-keyword-clause', `c-forward-<>-arglist',
5129 ;; `c-forward-name', `c-forward-type', `c-forward-decl-or-cast-1', and
5130 ;; `c-forward-label' to record the ranges of all the type and
5131 ;; reference identifiers they encounter. They will build lists on
5132 ;; these variables where each element is a cons of the buffer
5133 ;; positions surrounding each identifier. This recording is only
5134 ;; activated when `c-record-type-identifiers' is non-nil.
5135 ;;
5136 ;; All known types that can't be identifiers are recorded, and also
5137 ;; other possible types if `c-promote-possible-types' is set.
5138 ;; Recording is however disabled inside angle bracket arglists that
5139 ;; are encountered inside names and other angle bracket arglists.
5140 ;; Such occurrences are taken care of by `c-font-lock-<>-arglists'
5141 ;; instead.
5142 ;;
5143 ;; Only the names in C++ template style references (e.g. "tmpl" in
5144 ;; "tmpl<a,b>::foo") are recorded as references, other references
5145 ;; aren't handled here.
5146 ;;
5147 ;; `c-forward-label' records the label identifier(s) on
5148 ;; `c-record-ref-identifiers'.
5149 (defvar c-record-type-identifiers nil)
5150 (defvar c-record-ref-identifiers nil)
5151
5152 ;; This variable will receive a cons cell of the range of the last
5153 ;; single identifier symbol stepped over by `c-forward-name' if it's
5154 ;; successful. This is the range that should be put on one of the
5155 ;; record lists above by the caller. It's assigned nil if there's no
5156 ;; such symbol in the name.
5157 (defvar c-last-identifier-range nil)
5158
5159 (defmacro c-record-type-id (range)
5160 (if (eq (car-safe range) 'cons)
5161 ;; Always true.
5162 `(setq c-record-type-identifiers
5163 (cons ,range c-record-type-identifiers))
5164 `(let ((range ,range))
5165 (if range
5166 (setq c-record-type-identifiers
5167 (cons range c-record-type-identifiers))))))
5168
5169 (defmacro c-record-ref-id (range)
5170 (if (eq (car-safe range) 'cons)
5171 ;; Always true.
5172 `(setq c-record-ref-identifiers
5173 (cons ,range c-record-ref-identifiers))
5174 `(let ((range ,range))
5175 (if range
5176 (setq c-record-ref-identifiers
5177 (cons range c-record-ref-identifiers))))))
5178
5179 ;; Dynamically bound variable that instructs `c-forward-type' to
5180 ;; record the ranges of types that only are found. Behaves otherwise
5181 ;; like `c-record-type-identifiers'.
5182 (defvar c-record-found-types nil)
5183
5184 (defmacro c-forward-keyword-prefixed-id (type)
5185 ;; Used internally in `c-forward-keyword-clause' to move forward
5186 ;; over a type (if TYPE is 'type) or a name (otherwise) which
5187 ;; possibly is prefixed by keywords and their associated clauses.
5188 ;; Try with a type/name first to not trip up on those that begin
5189 ;; with a keyword. Return t if a known or found type is moved
5190 ;; over. The point is clobbered if nil is returned. If range
5191 ;; recording is enabled, the identifier is recorded on as a type
5192 ;; if TYPE is 'type or as a reference if TYPE is 'ref.
5193 ;;
5194 ;; This macro might do hidden buffer changes.
5195 `(let (res)
5196 (while (if (setq res ,(if (eq type 'type)
5197 `(c-forward-type)
5198 `(c-forward-name)))
5199 nil
5200 (and (looking-at c-keywords-regexp)
5201 (c-forward-keyword-clause 1))))
5202 (when (memq res '(t known found prefix))
5203 ,(when (eq type 'ref)
5204 `(when c-record-type-identifiers
5205 (c-record-ref-id c-last-identifier-range)))
5206 t)))
5207
5208 (defmacro c-forward-id-comma-list (type update-safe-pos)
5209 ;; Used internally in `c-forward-keyword-clause' to move forward
5210 ;; over a comma separated list of types or names using
5211 ;; `c-forward-keyword-prefixed-id'.
5212 ;;
5213 ;; This macro might do hidden buffer changes.
5214 `(while (and (progn
5215 ,(when update-safe-pos
5216 `(setq safe-pos (point)))
5217 (eq (char-after) ?,))
5218 (progn
5219 (forward-char)
5220 (c-forward-syntactic-ws)
5221 (c-forward-keyword-prefixed-id ,type)))))
5222
5223 (defun c-forward-keyword-clause (match)
5224 ;; Submatch MATCH in the current match data is assumed to surround a
5225 ;; token. If it's a keyword, move over it and any immediately
5226 ;; following clauses associated with it, stopping at the start of
5227 ;; the next token. t is returned in that case, otherwise the point
5228 ;; stays and nil is returned. The kind of clauses that are
5229 ;; recognized are those specified by `c-type-list-kwds',
5230 ;; `c-ref-list-kwds', `c-colon-type-list-kwds',
5231 ;; `c-paren-nontype-kwds', `c-paren-type-kwds', `c-<>-type-kwds',
5232 ;; and `c-<>-arglist-kwds'.
5233 ;;
5234 ;; This function records identifier ranges on
5235 ;; `c-record-type-identifiers' and `c-record-ref-identifiers' if
5236 ;; `c-record-type-identifiers' is non-nil.
5237 ;;
5238 ;; Note that for `c-colon-type-list-kwds', which doesn't necessary
5239 ;; apply directly after the keyword, the type list is moved over
5240 ;; only when there is no unaccounted token before it (i.e. a token
5241 ;; that isn't moved over due to some other keyword list). The
5242 ;; identifier ranges in the list are still recorded if that should
5243 ;; be done, though.
5244 ;;
5245 ;; This function might do hidden buffer changes.
5246
5247 (let ((kwd-sym (c-keyword-sym (match-string match))) safe-pos pos
5248 ;; The call to `c-forward-<>-arglist' below is made after
5249 ;; `c-<>-sexp-kwds' keywords, so we're certain they actually
5250 ;; are angle bracket arglists and `c-restricted-<>-arglists'
5251 ;; should therefore be nil.
5252 (c-parse-and-markup-<>-arglists t)
5253 c-restricted-<>-arglists)
5254
5255 (when kwd-sym
5256 (goto-char (match-end match))
5257 (c-forward-syntactic-ws)
5258 (setq safe-pos (point))
5259
5260 (cond
5261 ((and (c-keyword-member kwd-sym 'c-type-list-kwds)
5262 (c-forward-keyword-prefixed-id type))
5263 ;; There's a type directly after a keyword in `c-type-list-kwds'.
5264 (c-forward-id-comma-list type t))
5265
5266 ((and (c-keyword-member kwd-sym 'c-ref-list-kwds)
5267 (c-forward-keyword-prefixed-id ref))
5268 ;; There's a name directly after a keyword in `c-ref-list-kwds'.
5269 (c-forward-id-comma-list ref t))
5270
5271 ((and (c-keyword-member kwd-sym 'c-paren-any-kwds)
5272 (eq (char-after) ?\())
5273 ;; There's an open paren after a keyword in `c-paren-any-kwds'.
5274
5275 (forward-char)
5276 (when (and (setq pos (c-up-list-forward))
5277 (eq (char-before pos) ?\)))
5278 (when (and c-record-type-identifiers
5279 (c-keyword-member kwd-sym 'c-paren-type-kwds))
5280 ;; Use `c-forward-type' on every identifier we can find
5281 ;; inside the paren, to record the types.
5282 (while (c-syntactic-re-search-forward c-symbol-start pos t)
5283 (goto-char (match-beginning 0))
5284 (unless (c-forward-type)
5285 (looking-at c-symbol-key) ; Always matches.
5286 (goto-char (match-end 0)))))
5287
5288 (goto-char pos)
5289 (c-forward-syntactic-ws)
5290 (setq safe-pos (point))))
5291
5292 ((and (c-keyword-member kwd-sym 'c-<>-sexp-kwds)
5293 (eq (char-after) ?<)
5294 (c-forward-<>-arglist (c-keyword-member kwd-sym 'c-<>-type-kwds)))
5295 (c-forward-syntactic-ws)
5296 (setq safe-pos (point)))
5297
5298 ((and (c-keyword-member kwd-sym 'c-nonsymbol-sexp-kwds)
5299 (not (looking-at c-symbol-start))
5300 (c-safe (c-forward-sexp) t))
5301 (c-forward-syntactic-ws)
5302 (setq safe-pos (point))))
5303
5304 (when (c-keyword-member kwd-sym 'c-colon-type-list-kwds)
5305 (if (eq (char-after) ?:)
5306 ;; If we are at the colon already, we move over the type
5307 ;; list after it.
5308 (progn
5309 (forward-char)
5310 (c-forward-syntactic-ws)
5311 (when (c-forward-keyword-prefixed-id type)
5312 (c-forward-id-comma-list type t)))
5313 ;; Not at the colon, so stop here. But the identifier
5314 ;; ranges in the type list later on should still be
5315 ;; recorded.
5316 (and c-record-type-identifiers
5317 (progn
5318 ;; If a keyword matched both one of the types above and
5319 ;; this one, we match `c-colon-type-list-re' after the
5320 ;; clause matched above.
5321 (goto-char safe-pos)
5322 (looking-at c-colon-type-list-re))
5323 (progn
5324 (goto-char (match-end 0))
5325 (c-forward-syntactic-ws)
5326 (c-forward-keyword-prefixed-id type))
5327 ;; There's a type after the `c-colon-type-list-re' match
5328 ;; after a keyword in `c-colon-type-list-kwds'.
5329 (c-forward-id-comma-list type nil))))
5330
5331 (goto-char safe-pos)
5332 t)))
5333
5334 (defun c-forward-<>-arglist (all-types)
5335 ;; The point is assumed to be at a "<". Try to treat it as the open
5336 ;; paren of an angle bracket arglist and move forward to the
5337 ;; corresponding ">". If successful, the point is left after the
5338 ;; ">" and t is returned, otherwise the point isn't moved and nil is
5339 ;; returned. If ALL-TYPES is t then all encountered arguments in
5340 ;; the arglist that might be types are treated as found types.
5341 ;;
5342 ;; The variable `c-parse-and-markup-<>-arglists' controls how this
5343 ;; function handles text properties on the angle brackets and argument
5344 ;; separating commas.
5345 ;;
5346 ;; `c-restricted-<>-arglists' controls how lenient the template
5347 ;; arglist recognition should be.
5348 ;;
5349 ;; This function records identifier ranges on
5350 ;; `c-record-type-identifiers' and `c-record-ref-identifiers' if
5351 ;; `c-record-type-identifiers' is non-nil.
5352 ;;
5353 ;; This function might do hidden buffer changes.
5354
5355 (let ((start (point))
5356 ;; If `c-record-type-identifiers' is set then activate
5357 ;; recording of any found types that constitute an argument in
5358 ;; the arglist.
5359 (c-record-found-types (if c-record-type-identifiers t)))
5360 (if (catch 'angle-bracket-arglist-escape
5361 (setq c-record-found-types
5362 (c-forward-<>-arglist-recur all-types)))
5363 (progn
5364 (when (consp c-record-found-types)
5365 (setq c-record-type-identifiers
5366 ;; `nconc' doesn't mind that the tail of
5367 ;; `c-record-found-types' is t.
5368 (nconc c-record-found-types c-record-type-identifiers)))
5369 t)
5370
5371 (goto-char start)
5372 nil)))
5373
5374 (defun c-forward-<>-arglist-recur (all-types)
5375 ;; Recursive part of `c-forward-<>-arglist'.
5376 ;;
5377 ;; This function might do hidden buffer changes.
5378
5379 (let ((start (point)) res pos tmp
5380 ;; Cover this so that any recorded found type ranges are
5381 ;; automatically lost if it turns out to not be an angle
5382 ;; bracket arglist. It's propagated through the return value
5383 ;; on successful completion.
5384 (c-record-found-types c-record-found-types)
5385 ;; List that collects the positions after the argument
5386 ;; separating ',' in the arglist.
5387 arg-start-pos)
5388
5389 ;; If the '<' has paren open syntax then we've marked it as an angle
5390 ;; bracket arglist before, so skip to the end.
5391 (if (and (not c-parse-and-markup-<>-arglists)
5392 (c-get-char-property (point) 'syntax-table))
5393
5394 (progn
5395 (forward-char)
5396 (if (and (c-go-up-list-forward)
5397 (eq (char-before) ?>))
5398 t
5399
5400 ;; Got unmatched paren angle brackets. We don't clear the paren
5401 ;; syntax properties and retry, on the basis that it's very
5402 ;; unlikely that paren angle brackets become operators by code
5403 ;; manipulation. It's far more likely that it doesn't match due
5404 ;; to narrowing or some temporary change.
5405 (goto-char start)
5406 nil))
5407
5408 (forward-char)
5409 (unless (looking-at c-<-op-cont-regexp)
5410 (while (and
5411 (progn
5412
5413 (when c-record-type-identifiers
5414 (if all-types
5415
5416 ;; All encountered identifiers are types, so set the
5417 ;; promote flag and parse the type.
5418 (progn
5419 (c-forward-syntactic-ws)
5420 (when (looking-at c-identifier-start)
5421 (let ((c-promote-possible-types t))
5422 (c-forward-type))))
5423
5424 ;; Check if this arglist argument is a sole type. If
5425 ;; it's known then it's recorded in
5426 ;; `c-record-type-identifiers'. If it only is found
5427 ;; then it's recorded in `c-record-found-types' which we
5428 ;; might roll back if it turns out that this isn't an
5429 ;; angle bracket arglist afterall.
5430 (when (memq (char-before) '(?, ?<))
5431 (let ((orig-record-found-types c-record-found-types))
5432 (c-forward-syntactic-ws)
5433 (and (memq (c-forward-type) '(known found))
5434 (not (looking-at "[,>]"))
5435 ;; A found type was recorded but it's not the
5436 ;; only thing in the arglist argument, so reset
5437 ;; `c-record-found-types'.
5438 (setq c-record-found-types
5439 orig-record-found-types))))))
5440
5441 (setq pos (point))
5442 (or (when (eq (char-after) ?>)
5443 ;; Must check for '>' at the very start separately,
5444 ;; since the regexp below has to avoid ">>" without
5445 ;; using \\=.
5446 (forward-char)
5447 t)
5448
5449 ;; Note: These regexps exploit the match order in \| so
5450 ;; that "<>" is matched by "<" rather than "[^>:-]>".
5451 (c-syntactic-re-search-forward
5452 (if c-restricted-<>-arglists
5453 ;; Stop on ',', '|', '&', '+' and '-' to catch
5454 ;; common binary operators that could be between
5455 ;; two comparison expressions "a<b" and "c>d".
5456 "[<;{},|&+-]\\|\\([^>:-]>\\)"
5457 ;; Otherwise we still stop on ',' to find the
5458 ;; argument start positions.
5459 "[<;{},]\\|\\([^>:-]>\\)")
5460 nil 'move t t 1)
5461
5462 ;; If the arglist starter has lost its open paren
5463 ;; syntax but not the closer, we won't find the
5464 ;; closer above since we only search in the
5465 ;; balanced sexp. In that case we stop just short
5466 ;; of it so check if the following char is the closer.
5467 (when (eq (char-after) ?>)
5468 (forward-char)
5469 t)))
5470
5471 (cond
5472 ((eq (char-before) ?>)
5473 ;; Either an operator starting with '>' or the end of
5474 ;; the angle bracket arglist.
5475
5476 (if (looking-at c->-op-cont-regexp)
5477 (progn
5478 (goto-char (match-end 0))
5479 t) ; Continue the loop.
5480
5481 ;; The angle bracket arglist is finished.
5482 (when c-parse-and-markup-<>-arglists
5483 (while arg-start-pos
5484 (c-put-c-type-property (1- (car arg-start-pos))
5485 'c-<>-arg-sep)
5486 (setq arg-start-pos (cdr arg-start-pos)))
5487 (c-mark-<-as-paren start)
5488 (c-mark->-as-paren (1- (point))))
5489 (setq res t)
5490 nil)) ; Exit the loop.
5491
5492 ((eq (char-before) ?<)
5493 ;; Either an operator starting with '<' or a nested arglist.
5494
5495 (setq pos (point))
5496 (let (id-start id-end subres keyword-match)
5497 (if (if (looking-at c-<-op-cont-regexp)
5498 (setq tmp (match-end 0))
5499 (setq tmp pos)
5500 (backward-char)
5501 (not
5502 (and
5503
5504 (save-excursion
5505 ;; There's always an identifier before an angle
5506 ;; bracket arglist, or a keyword in
5507 ;; `c-<>-type-kwds' or `c-<>-arglist-kwds'.
5508 (c-backward-syntactic-ws)
5509 (setq id-end (point))
5510 (c-simple-skip-symbol-backward)
5511 (when (or (setq keyword-match
5512 (looking-at c-opt-<>-sexp-key))
5513 (not (looking-at c-keywords-regexp)))
5514 (setq id-start (point))))
5515
5516 (setq subres
5517 (let ((c-record-type-identifiers nil)
5518 (c-record-found-types nil))
5519 (c-forward-<>-arglist-recur
5520 (and keyword-match
5521 (c-keyword-member
5522 (c-keyword-sym (match-string 1))
5523 'c-<>-type-kwds)))))
5524 )))
5525
5526 ;; It was not an angle bracket arglist.
5527 (goto-char tmp)
5528
5529 ;; It was an angle bracket arglist.
5530 (setq c-record-found-types subres)
5531
5532 ;; Record the identifier before the template as a type
5533 ;; or reference depending on whether the arglist is last
5534 ;; in a qualified identifier.
5535 (when (and c-record-type-identifiers
5536 (not keyword-match))
5537 (if (and c-opt-identifier-concat-key
5538 (progn
5539 (c-forward-syntactic-ws)
5540 (looking-at c-opt-identifier-concat-key)))
5541 (c-record-ref-id (cons id-start id-end))
5542 (c-record-type-id (cons id-start id-end))))))
5543 t)
5544
5545 ((and (eq (char-before) ?,)
5546 (not c-restricted-<>-arglists))
5547 ;; Just another argument. Record the position. The
5548 ;; type check stuff that made us stop at it is at
5549 ;; the top of the loop.
5550 (setq arg-start-pos (cons (point) arg-start-pos)))
5551
5552 (t
5553 ;; Got a character that can't be in an angle bracket
5554 ;; arglist argument. Abort using `throw', since
5555 ;; it's useless to try to find a surrounding arglist
5556 ;; if we're nested.
5557 (throw 'angle-bracket-arglist-escape nil))))))
5558
5559 (if res
5560 (or c-record-found-types t)))))
5561
5562 (defun c-backward-<>-arglist (all-types &optional limit)
5563 ;; The point is assumed to be directly after a ">". Try to treat it
5564 ;; as the close paren of an angle bracket arglist and move back to
5565 ;; the corresponding "<". If successful, the point is left at
5566 ;; the "<" and t is returned, otherwise the point isn't moved and
5567 ;; nil is returned. ALL-TYPES is passed on to
5568 ;; `c-forward-<>-arglist'.
5569 ;;
5570 ;; If the optional LIMIT is given, it bounds the backward search.
5571 ;; It's then assumed to be at a syntactically relevant position.
5572 ;;
5573 ;; This is a wrapper around `c-forward-<>-arglist'. See that
5574 ;; function for more details.
5575
5576 (let ((start (point)))
5577 (backward-char)
5578 (if (and (not c-parse-and-markup-<>-arglists)
5579 (c-get-char-property (point) 'syntax-table))
5580
5581 (if (and (c-go-up-list-backward)
5582 (eq (char-after) ?<))
5583 t
5584 ;; See corresponding note in `c-forward-<>-arglist'.
5585 (goto-char start)
5586 nil)
5587
5588 (while (progn
5589 (c-syntactic-skip-backward "^<;{}" limit t)
5590
5591 (and
5592 (if (eq (char-before) ?<)
5593 t
5594 ;; Stopped at bob or a char that isn't allowed in an
5595 ;; arglist, so we've failed.
5596 (goto-char start)
5597 nil)
5598
5599 (if (> (point)
5600 (progn (c-beginning-of-current-token)
5601 (point)))
5602 ;; If we moved then the "<" was part of some
5603 ;; multicharacter token.
5604 t
5605
5606 (backward-char)
5607 (let ((beg-pos (point)))
5608 (if (c-forward-<>-arglist all-types)
5609 (cond ((= (point) start)
5610 ;; Matched the arglist. Break the while.
5611 (goto-char beg-pos)
5612 nil)
5613 ((> (point) start)
5614 ;; We started from a non-paren ">" inside an
5615 ;; arglist.
5616 (goto-char start)
5617 nil)
5618 (t
5619 ;; Matched a shorter arglist. Can be a nested
5620 ;; one so continue looking.
5621 (goto-char beg-pos)
5622 t))
5623 t))))))
5624
5625 (/= (point) start))))
5626
5627 (defun c-forward-name ()
5628 ;; Move forward over a complete name if at the beginning of one,
5629 ;; stopping at the next following token. If the point is not at
5630 ;; something that are recognized as name then it stays put. A name
5631 ;; could be something as simple as "foo" in C or something as
5632 ;; complex as "X<Y<class A<int>::B, BIT_MAX >> b>, ::operator<> ::
5633 ;; Z<(a>b)> :: operator const X<&foo>::T Q::G<unsigned short
5634 ;; int>::*volatile const" in C++ (this function is actually little
5635 ;; more than a `looking-at' call in all modes except those that,
5636 ;; like C++, have `c-recognize-<>-arglists' set). Return nil if no
5637 ;; name is found, 'template if it's an identifier ending with an
5638 ;; angle bracket arglist, 'operator of it's an operator identifier,
5639 ;; or t if it's some other kind of name.
5640 ;;
5641 ;; This function records identifier ranges on
5642 ;; `c-record-type-identifiers' and `c-record-ref-identifiers' if
5643 ;; `c-record-type-identifiers' is non-nil.
5644 ;;
5645 ;; This function might do hidden buffer changes.
5646
5647 (let ((pos (point)) (start (point)) res id-start id-end
5648 ;; Turn off `c-promote-possible-types' here since we might
5649 ;; call `c-forward-<>-arglist' and we don't want it to promote
5650 ;; every suspect thing in the arglist to a type. We're
5651 ;; typically called from `c-forward-type' in this case, and
5652 ;; the caller only wants the top level type that it finds to
5653 ;; be promoted.
5654 c-promote-possible-types)
5655 (while
5656 (and
5657 (looking-at c-identifier-key)
5658
5659 (progn
5660 ;; Check for keyword. We go to the last symbol in
5661 ;; `c-identifier-key' first.
5662 (goto-char (setq id-end (match-end 0)))
5663 (c-simple-skip-symbol-backward)
5664 (setq id-start (point))
5665
5666 (if (looking-at c-keywords-regexp)
5667 (when (and (c-major-mode-is 'c++-mode)
5668 (looking-at
5669 (cc-eval-when-compile
5670 (concat "\\(operator\\|\\(template\\)\\)"
5671 "\\(" (c-lang-const c-nonsymbol-key c++)
5672 "\\|$\\)")))
5673 (if (match-beginning 2)
5674 ;; "template" is only valid inside an
5675 ;; identifier if preceded by "::".
5676 (save-excursion
5677 (c-backward-syntactic-ws)
5678 (and (c-safe (backward-char 2) t)
5679 (looking-at "::")))
5680 t))
5681
5682 ;; Handle a C++ operator or template identifier.
5683 (goto-char id-end)
5684 (c-forward-syntactic-ws)
5685 (cond ((eq (char-before id-end) ?e)
5686 ;; Got "... ::template".
5687 (let ((subres (c-forward-name)))
5688 (when subres
5689 (setq pos (point)
5690 res subres))))
5691
5692 ((looking-at c-identifier-start)
5693 ;; Got a cast operator.
5694 (when (c-forward-type)
5695 (setq pos (point)
5696 res 'operator)
5697 ;; Now we should match a sequence of either
5698 ;; '*', '&' or a name followed by ":: *",
5699 ;; where each can be followed by a sequence
5700 ;; of `c-opt-type-modifier-key'.
5701 (while (cond ((looking-at "[*&]")
5702 (goto-char (match-end 0))
5703 t)
5704 ((looking-at c-identifier-start)
5705 (and (c-forward-name)
5706 (looking-at "::")
5707 (progn
5708 (goto-char (match-end 0))
5709 (c-forward-syntactic-ws)
5710 (eq (char-after) ?*))
5711 (progn
5712 (forward-char)
5713 t))))
5714 (while (progn
5715 (c-forward-syntactic-ws)
5716 (setq pos (point))
5717 (looking-at c-opt-type-modifier-key))
5718 (goto-char (match-end 1))))))
5719
5720 ((looking-at c-overloadable-operators-regexp)
5721 ;; Got some other operator.
5722 (setq c-last-identifier-range
5723 (cons (point) (match-end 0)))
5724 (goto-char (match-end 0))
5725 (c-forward-syntactic-ws)
5726 (setq pos (point)
5727 res 'operator)))
5728
5729 nil)
5730
5731 ;; `id-start' is equal to `id-end' if we've jumped over
5732 ;; an identifier that doesn't end with a symbol token.
5733 ;; That can occur e.g. for Java import directives on the
5734 ;; form "foo.bar.*".
5735 (when (and id-start (/= id-start id-end))
5736 (setq c-last-identifier-range
5737 (cons id-start id-end)))
5738 (goto-char id-end)
5739 (c-forward-syntactic-ws)
5740 (setq pos (point)
5741 res t)))
5742
5743 (progn
5744 (goto-char pos)
5745 (when (or c-opt-identifier-concat-key
5746 c-recognize-<>-arglists)
5747
5748 (cond
5749 ((and c-opt-identifier-concat-key
5750 (looking-at c-opt-identifier-concat-key))
5751 ;; Got a concatenated identifier. This handles the
5752 ;; cases with tricky syntactic whitespace that aren't
5753 ;; covered in `c-identifier-key'.
5754 (goto-char (match-end 0))
5755 (c-forward-syntactic-ws)
5756 t)
5757
5758 ((and c-recognize-<>-arglists
5759 (eq (char-after) ?<))
5760 ;; Maybe an angle bracket arglist.
5761
5762 (when (let (c-record-type-identifiers
5763 c-record-found-types)
5764 (c-forward-<>-arglist nil))
5765
5766 (c-add-type start (1+ pos))
5767 (c-forward-syntactic-ws)
5768 (setq pos (point)
5769 c-last-identifier-range nil)
5770
5771 (if (and c-opt-identifier-concat-key
5772 (looking-at c-opt-identifier-concat-key))
5773
5774 ;; Continue if there's an identifier concatenation
5775 ;; operator after the template argument.
5776 (progn
5777 (when (and c-record-type-identifiers id-start)
5778 (c-record-ref-id (cons id-start id-end)))
5779 (forward-char 2)
5780 (c-forward-syntactic-ws)
5781 t)
5782
5783 (when (and c-record-type-identifiers id-start)
5784 (c-record-type-id (cons id-start id-end)))
5785 (setq res 'template)
5786 nil)))
5787 )))))
5788
5789 (goto-char pos)
5790 res))
5791
5792 (defun c-forward-type ()
5793 ;; Move forward over a type spec if at the beginning of one,
5794 ;; stopping at the next following token. Return t if it's a known
5795 ;; type that can't be a name or other expression, 'known if it's an
5796 ;; otherwise known type (according to `*-font-lock-extra-types'),
5797 ;; 'prefix if it's a known prefix of a type, 'found if it's a type
5798 ;; that matches one in `c-found-types', 'maybe if it's an identfier
5799 ;; that might be a type, or nil if it can't be a type (the point
5800 ;; isn't moved then). The point is assumed to be at the beginning
5801 ;; of a token.
5802 ;;
5803 ;; Note that this function doesn't skip past the brace definition
5804 ;; that might be considered part of the type, e.g.
5805 ;; "enum {a, b, c} foo".
5806 ;;
5807 ;; This function records identifier ranges on
5808 ;; `c-record-type-identifiers' and `c-record-ref-identifiers' if
5809 ;; `c-record-type-identifiers' is non-nil.
5810 ;;
5811 ;; This function might do hidden buffer changes.
5812
5813 (let ((start (point)) pos res name-res id-start id-end id-range)
5814
5815 ;; Skip leading type modifiers. If any are found we know it's a
5816 ;; prefix of a type.
5817 (when c-opt-type-modifier-key
5818 (while (looking-at c-opt-type-modifier-key)
5819 (goto-char (match-end 1))
5820 (c-forward-syntactic-ws)
5821 (setq res 'prefix)))
5822
5823 (cond
5824 ((looking-at c-type-prefix-key)
5825 ;; Looking at a keyword that prefixes a type identifier,
5826 ;; e.g. "class".
5827 (goto-char (match-end 1))
5828 (c-forward-syntactic-ws)
5829 (setq pos (point))
5830 (if (memq (setq name-res (c-forward-name)) '(t template))
5831 (progn
5832 (when (eq name-res t)
5833 ;; In many languages the name can be used without the
5834 ;; prefix, so we add it to `c-found-types'.
5835 (c-add-type pos (point))
5836 (when (and c-record-type-identifiers
5837 c-last-identifier-range)
5838 (c-record-type-id c-last-identifier-range)))
5839 (setq res t))
5840 ;; Invalid syntax.
5841 (goto-char start)
5842 (setq res nil)))
5843
5844 ((progn
5845 (setq pos nil)
5846 (if (looking-at c-identifier-start)
5847 (save-excursion
5848 (setq id-start (point)
5849 name-res (c-forward-name))
5850 (when name-res
5851 (setq id-end (point)
5852 id-range c-last-identifier-range))))
5853 (and (cond ((looking-at c-primitive-type-key)
5854 (setq res t))
5855 ((c-with-syntax-table c-identifier-syntax-table
5856 (looking-at c-known-type-key))
5857 (setq res 'known)))
5858 (or (not id-end)
5859 (>= (save-excursion
5860 (save-match-data
5861 (goto-char (match-end 1))
5862 (c-forward-syntactic-ws)
5863 (setq pos (point))))
5864 id-end)
5865 (setq res nil))))
5866 ;; Looking at a primitive or known type identifier. We've
5867 ;; checked for a name first so that we don't go here if the
5868 ;; known type match only is a prefix of another name.
5869
5870 (setq id-end (match-end 1))
5871
5872 (when (and c-record-type-identifiers
5873 (or c-promote-possible-types (eq res t)))
5874 (c-record-type-id (cons (match-beginning 1) (match-end 1))))
5875
5876 (if (and c-opt-type-component-key
5877 (save-match-data
5878 (looking-at c-opt-type-component-key)))
5879 ;; There might be more keywords for the type.
5880 (let (safe-pos)
5881 (c-forward-keyword-clause 1)
5882 (while (progn
5883 (setq safe-pos (point))
5884 (looking-at c-opt-type-component-key))
5885 (when (and c-record-type-identifiers
5886 (looking-at c-primitive-type-key))
5887 (c-record-type-id (cons (match-beginning 1)
5888 (match-end 1))))
5889 (c-forward-keyword-clause 1))
5890 (if (looking-at c-primitive-type-key)
5891 (progn
5892 (when c-record-type-identifiers
5893 (c-record-type-id (cons (match-beginning 1)
5894 (match-end 1))))
5895 (c-forward-keyword-clause 1)
5896 (setq res t))
5897 (goto-char safe-pos)
5898 (setq res 'prefix)))
5899 (unless (save-match-data (c-forward-keyword-clause 1))
5900 (if pos
5901 (goto-char pos)
5902 (goto-char (match-end 1))
5903 (c-forward-syntactic-ws)))))
5904
5905 (name-res
5906 (cond ((eq name-res t)
5907 ;; A normal identifier.
5908 (goto-char id-end)
5909 (if (or res c-promote-possible-types)
5910 (progn
5911 (c-add-type id-start id-end)
5912 (when (and c-record-type-identifiers id-range)
5913 (c-record-type-id id-range))
5914 (unless res
5915 (setq res 'found)))
5916 (setq res (if (c-check-type id-start id-end)
5917 ;; It's an identifier that has been used as
5918 ;; a type somewhere else.
5919 'found
5920 ;; It's an identifier that might be a type.
5921 'maybe))))
5922 ((eq name-res 'template)
5923 ;; A template is a type.
5924 (goto-char id-end)
5925 (setq res t))
5926 (t
5927 ;; Otherwise it's an operator identifier, which is not a type.
5928 (goto-char start)
5929 (setq res nil)))))
5930
5931 (when res
5932 ;; Skip trailing type modifiers. If any are found we know it's
5933 ;; a type.
5934 (when c-opt-type-modifier-key
5935 (while (looking-at c-opt-type-modifier-key)
5936 (goto-char (match-end 1))
5937 (c-forward-syntactic-ws)
5938 (setq res t)))
5939
5940 ;; Step over any type suffix operator. Do not let the existence
5941 ;; of these alter the classification of the found type, since
5942 ;; these operators typically are allowed in normal expressions
5943 ;; too.
5944 (when c-opt-type-suffix-key
5945 (while (looking-at c-opt-type-suffix-key)
5946 (goto-char (match-end 1))
5947 (c-forward-syntactic-ws)))
5948
5949 (when c-opt-type-concat-key
5950 ;; Look for a trailing operator that concatenates the type
5951 ;; with a following one, and if so step past that one through
5952 ;; a recursive call. Note that we don't record concatenated
5953 ;; types in `c-found-types' - it's the component types that
5954 ;; are recorded when appropriate.
5955 (setq pos (point))
5956 (let* ((c-promote-possible-types (or (memq res '(t known))
5957 c-promote-possible-types))
5958 ;; If we can't promote then set `c-record-found-types' so that
5959 ;; we can merge in the types from the second part afterwards if
5960 ;; it turns out to be a known type there.
5961 (c-record-found-types (and c-record-type-identifiers
5962 (not c-promote-possible-types)))
5963 subres)
5964 (if (and (looking-at c-opt-type-concat-key)
5965
5966 (progn
5967 (goto-char (match-end 1))
5968 (c-forward-syntactic-ws)
5969 (setq subres (c-forward-type))))
5970
5971 (progn
5972 ;; If either operand certainly is a type then both are, but we
5973 ;; don't let the existence of the operator itself promote two
5974 ;; uncertain types to a certain one.
5975 (cond ((eq res t))
5976 ((eq subres t)
5977 (unless (eq name-res 'template)
5978 (c-add-type id-start id-end))
5979 (when (and c-record-type-identifiers id-range)
5980 (c-record-type-id id-range))
5981 (setq res t))
5982 ((eq res 'known))
5983 ((eq subres 'known)
5984 (setq res 'known))
5985 ((eq res 'found))
5986 ((eq subres 'found)
5987 (setq res 'found))
5988 (t
5989 (setq res 'maybe)))
5990
5991 (when (and (eq res t)
5992 (consp c-record-found-types))
5993 ;; Merge in the ranges of any types found by the second
5994 ;; `c-forward-type'.
5995 (setq c-record-type-identifiers
5996 ;; `nconc' doesn't mind that the tail of
5997 ;; `c-record-found-types' is t.
5998 (nconc c-record-found-types
5999 c-record-type-identifiers))))
6000
6001 (goto-char pos))))
6002
6003 (when (and c-record-found-types (memq res '(known found)) id-range)
6004 (setq c-record-found-types
6005 (cons id-range c-record-found-types))))
6006
6007 ;;(message "c-forward-type %s -> %s: %s" start (point) res)
6008
6009 res))
6010
6011 \f
6012 ;; Handling of large scale constructs like statements and declarations.
6013
6014 ;; Macro used inside `c-forward-decl-or-cast-1'. It ought to be a
6015 ;; defsubst or perhaps even a defun, but it contains lots of free
6016 ;; variables that refer to things inside `c-forward-decl-or-cast-1'.
6017 (defmacro c-fdoc-shift-type-backward (&optional short)
6018 ;; `c-forward-decl-or-cast-1' can consume an arbitrary length list
6019 ;; of types when parsing a declaration, which means that it
6020 ;; sometimes consumes the identifier in the declaration as a type.
6021 ;; This is used to "backtrack" and make the last type be treated as
6022 ;; an identifier instead.
6023 `(progn
6024 ,(unless short
6025 ;; These identifiers are bound only in the inner let.
6026 '(setq identifier-type at-type
6027 identifier-start type-start
6028 got-parens nil
6029 got-identifier t
6030 got-suffix t
6031 got-suffix-after-parens id-start
6032 paren-depth 0))
6033
6034 (if (setq at-type (if (eq backup-at-type 'prefix)
6035 t
6036 backup-at-type))
6037 (setq type-start backup-type-start
6038 id-start backup-id-start)
6039 (setq type-start start-pos
6040 id-start start-pos))
6041
6042 ;; When these flags already are set we've found specifiers that
6043 ;; unconditionally signal these attributes - backtracking doesn't
6044 ;; change that. So keep them set in that case.
6045 (or at-type-decl
6046 (setq at-type-decl backup-at-type-decl))
6047 (or maybe-typeless
6048 (setq maybe-typeless backup-maybe-typeless))
6049
6050 ,(unless short
6051 ;; This identifier is bound only in the inner let.
6052 '(setq start id-start))))
6053
6054 (defun c-forward-decl-or-cast-1 (preceding-token-end context last-cast-end)
6055 ;; Move forward over a declaration or a cast if at the start of one.
6056 ;; The point is assumed to be at the start of some token. Nil is
6057 ;; returned if no declaration or cast is recognized, and the point
6058 ;; is clobbered in that case.
6059 ;;
6060 ;; If a declaration is parsed:
6061 ;;
6062 ;; The point is left at the first token after the first complete
6063 ;; declarator, if there is one. The return value is a cons where
6064 ;; the car is the position of the first token in the declarator. (See
6065 ;; below for the cdr.)
6066 ;; Some examples:
6067 ;;
6068 ;; void foo (int a, char *b) stuff ...
6069 ;; car ^ ^ point
6070 ;; float (*a)[], b;
6071 ;; car ^ ^ point
6072 ;; unsigned int a = c_style_initializer, b;
6073 ;; car ^ ^ point
6074 ;; unsigned int a (cplusplus_style_initializer), b;
6075 ;; car ^ ^ point (might change)
6076 ;; class Foo : public Bar {}
6077 ;; car ^ ^ point
6078 ;; class PikeClass (int a, string b) stuff ...
6079 ;; car ^ ^ point
6080 ;; enum bool;
6081 ;; car ^ ^ point
6082 ;; enum bool flag;
6083 ;; car ^ ^ point
6084 ;; void cplusplus_function (int x) throw (Bad);
6085 ;; car ^ ^ point
6086 ;; Foo::Foo (int b) : Base (b) {}
6087 ;; car ^ ^ point
6088 ;;
6089 ;; The cdr of the return value is non-nil iff a `c-typedef-decl-kwds'
6090 ;; specifier (e.g. class, struct, enum, typedef) is found in the
6091 ;; declaration, i.e. the declared identifier(s) are types.
6092 ;;
6093 ;; If a cast is parsed:
6094 ;;
6095 ;; The point is left at the first token after the closing paren of
6096 ;; the cast. The return value is `cast'. Note that the start
6097 ;; position must be at the first token inside the cast parenthesis
6098 ;; to recognize it.
6099 ;;
6100 ;; PRECEDING-TOKEN-END is the first position after the preceding
6101 ;; token, i.e. on the other side of the syntactic ws from the point.
6102 ;; Use a value less than or equal to (point-min) if the point is at
6103 ;; the first token in (the visible part of) the buffer.
6104 ;;
6105 ;; CONTEXT is a symbol that describes the context at the point:
6106 ;; 'decl In a comma-separated declaration context (typically
6107 ;; inside a function declaration arglist).
6108 ;; '<> In an angle bracket arglist.
6109 ;; 'arglist Some other type of arglist.
6110 ;; nil Some other context or unknown context. Includes
6111 ;; within the parens of an if, for, ... construct.
6112 ;;
6113 ;; LAST-CAST-END is the first token after the closing paren of a
6114 ;; preceding cast, or nil if none is known. If
6115 ;; `c-forward-decl-or-cast-1' is used in succession, it should be
6116 ;; the position after the closest preceding call where a cast was
6117 ;; matched. In that case it's used to discover chains of casts like
6118 ;; "(a) (b) c".
6119 ;;
6120 ;; This function records identifier ranges on
6121 ;; `c-record-type-identifiers' and `c-record-ref-identifiers' if
6122 ;; `c-record-type-identifiers' is non-nil.
6123 ;;
6124 ;; This function might do hidden buffer changes.
6125
6126 (let (;; `start-pos' is used below to point to the start of the
6127 ;; first type, i.e. after any leading specifiers. It might
6128 ;; also point at the beginning of the preceding syntactic
6129 ;; whitespace.
6130 (start-pos (point))
6131 ;; Set to the result of `c-forward-type'.
6132 at-type
6133 ;; The position of the first token in what we currently
6134 ;; believe is the type in the declaration or cast, after any
6135 ;; specifiers and their associated clauses.
6136 type-start
6137 ;; The position of the first token in what we currently
6138 ;; believe is the declarator for the first identifier. Set
6139 ;; when the type is found, and moved forward over any
6140 ;; `c-decl-hangon-kwds' and their associated clauses that
6141 ;; occurs after the type.
6142 id-start
6143 ;; These store `at-type', `type-start' and `id-start' of the
6144 ;; identifier before the one in those variables. The previous
6145 ;; identifier might turn out to be the real type in a
6146 ;; declaration if the last one has to be the declarator in it.
6147 ;; If `backup-at-type' is nil then the other variables have
6148 ;; undefined values.
6149 backup-at-type backup-type-start backup-id-start
6150 ;; Set if we've found a specifier that makes the defined
6151 ;; identifier(s) types.
6152 at-type-decl
6153 ;; Set if we've found a specifier that can start a declaration
6154 ;; where there's no type.
6155 maybe-typeless
6156 ;; If a specifier is found that also can be a type prefix,
6157 ;; these flags are set instead of those above. If we need to
6158 ;; back up an identifier, they are copied to the real flag
6159 ;; variables. Thus they only take effect if we fail to
6160 ;; interpret it as a type.
6161 backup-at-type-decl backup-maybe-typeless
6162 ;; Whether we've found a declaration or a cast. We might know
6163 ;; this before we've found the type in it. It's 'ids if we've
6164 ;; found two consecutive identifiers (usually a sure sign, but
6165 ;; we should allow that in labels too), and t if we've found a
6166 ;; specifier keyword (a 100% sure sign).
6167 at-decl-or-cast
6168 ;; Set when we need to back up to parse this as a declaration
6169 ;; but not as a cast.
6170 backup-if-not-cast
6171 ;; For casts, the return position.
6172 cast-end
6173 ;; Save `c-record-type-identifiers' and
6174 ;; `c-record-ref-identifiers' since ranges are recorded
6175 ;; speculatively and should be thrown away if it turns out
6176 ;; that it isn't a declaration or cast.
6177 (save-rec-type-ids c-record-type-identifiers)
6178 (save-rec-ref-ids c-record-ref-identifiers))
6179
6180 ;; Check for a type. Unknown symbols are treated as possible
6181 ;; types, but they could also be specifiers disguised through
6182 ;; macros like __INLINE__, so we recognize both types and known
6183 ;; specifiers after them too.
6184 (while
6185 (let* ((start (point)) kwd-sym kwd-clause-end found-type)
6186
6187 ;; Look for a specifier keyword clause.
6188 (when (looking-at c-prefix-spec-kwds-re)
6189 (setq kwd-sym (c-keyword-sym (match-string 1)))
6190 (save-excursion
6191 (c-forward-keyword-clause 1)
6192 (setq kwd-clause-end (point))))
6193
6194 (when (setq found-type (c-forward-type))
6195 ;; Found a known or possible type or a prefix of a known type.
6196
6197 (when at-type
6198 ;; Got two identifiers with nothing but whitespace
6199 ;; between them. That can only happen in declarations.
6200 (setq at-decl-or-cast 'ids)
6201
6202 (when (eq at-type 'found)
6203 ;; If the previous identifier is a found type we
6204 ;; record it as a real one; it might be some sort of
6205 ;; alias for a prefix like "unsigned".
6206 (save-excursion
6207 (goto-char type-start)
6208 (let ((c-promote-possible-types t))
6209 (c-forward-type)))))
6210
6211 (setq backup-at-type at-type
6212 backup-type-start type-start
6213 backup-id-start id-start
6214 at-type found-type
6215 type-start start
6216 id-start (point)
6217 ;; The previous ambiguous specifier/type turned out
6218 ;; to be a type since we've parsed another one after
6219 ;; it, so clear these backup flags.
6220 backup-at-type-decl nil
6221 backup-maybe-typeless nil))
6222
6223 (if kwd-sym
6224 (progn
6225 ;; Handle known specifier keywords and
6226 ;; `c-decl-hangon-kwds' which can occur after known
6227 ;; types.
6228
6229 (if (c-keyword-member kwd-sym 'c-decl-hangon-kwds)
6230 ;; It's a hang-on keyword that can occur anywhere.
6231 (progn
6232 (setq at-decl-or-cast t)
6233 (if at-type
6234 ;; Move the identifier start position if
6235 ;; we've passed a type.
6236 (setq id-start kwd-clause-end)
6237 ;; Otherwise treat this as a specifier and
6238 ;; move the fallback position.
6239 (setq start-pos kwd-clause-end))
6240 (goto-char kwd-clause-end))
6241
6242 ;; It's an ordinary specifier so we know that
6243 ;; anything before this can't be the type.
6244 (setq backup-at-type nil
6245 start-pos kwd-clause-end)
6246
6247 (if found-type
6248 ;; It's ambiguous whether this keyword is a
6249 ;; specifier or a type prefix, so set the backup
6250 ;; flags. (It's assumed that `c-forward-type'
6251 ;; moved further than `c-forward-keyword-clause'.)
6252 (progn
6253 (when (c-keyword-member kwd-sym 'c-typedef-decl-kwds)
6254 (setq backup-at-type-decl t))
6255 (when (c-keyword-member kwd-sym 'c-typeless-decl-kwds)
6256 (setq backup-maybe-typeless t)))
6257
6258 (when (c-keyword-member kwd-sym 'c-typedef-decl-kwds)
6259 (setq at-type-decl t))
6260 (when (c-keyword-member kwd-sym 'c-typeless-decl-kwds)
6261 (setq maybe-typeless t))
6262
6263 ;; Haven't matched a type so it's an umambiguous
6264 ;; specifier keyword and we know we're in a
6265 ;; declaration.
6266 (setq at-decl-or-cast t)
6267
6268 (goto-char kwd-clause-end))))
6269
6270 ;; If the type isn't known we continue so that we'll jump
6271 ;; over all specifiers and type identifiers. The reason
6272 ;; to do this for a known type prefix is to make things
6273 ;; like "unsigned INT16" work.
6274 (and found-type (not (eq found-type t))))))
6275
6276 (cond
6277 ((eq at-type t)
6278 ;; If a known type was found, we still need to skip over any
6279 ;; hangon keyword clauses after it. Otherwise it has already
6280 ;; been done in the loop above.
6281 (while (looking-at c-decl-hangon-key)
6282 (c-forward-keyword-clause 1))
6283 (setq id-start (point)))
6284
6285 ((eq at-type 'prefix)
6286 ;; A prefix type is itself a primitive type when it's not
6287 ;; followed by another type.
6288 (setq at-type t))
6289
6290 ((not at-type)
6291 ;; Got no type but set things up to continue anyway to handle
6292 ;; the various cases when a declaration doesn't start with a
6293 ;; type.
6294 (setq id-start start-pos))
6295
6296 ((and (eq at-type 'maybe)
6297 (c-major-mode-is 'c++-mode))
6298 ;; If it's C++ then check if the last "type" ends on the form
6299 ;; "foo::foo" or "foo::~foo", i.e. if it's the name of a
6300 ;; (con|de)structor.
6301 (save-excursion
6302 (let (name end-2 end-1)
6303 (goto-char id-start)
6304 (c-backward-syntactic-ws)
6305 (setq end-2 (point))
6306 (when (and
6307 (c-simple-skip-symbol-backward)
6308 (progn
6309 (setq name
6310 (buffer-substring-no-properties (point) end-2))
6311 ;; Cheating in the handling of syntactic ws below.
6312 (< (skip-chars-backward ":~ \t\n\r\v\f") 0))
6313 (progn
6314 (setq end-1 (point))
6315 (c-simple-skip-symbol-backward))
6316 (>= (point) type-start)
6317 (equal (buffer-substring-no-properties (point) end-1)
6318 name))
6319 ;; It is a (con|de)structor name. In that case the
6320 ;; declaration is typeless so zap out any preceding
6321 ;; identifier(s) that we might have taken as types.
6322 (goto-char type-start)
6323 (setq at-type nil
6324 backup-at-type nil
6325 id-start type-start))))))
6326
6327 ;; Check for and step over a type decl expression after the thing
6328 ;; that is or might be a type. This can't be skipped since we
6329 ;; need the correct end position of the declarator for
6330 ;; `max-type-decl-end-*'.
6331 (let ((start (point)) (paren-depth 0) pos
6332 ;; True if there's a non-open-paren match of
6333 ;; `c-type-decl-prefix-key'.
6334 got-prefix
6335 ;; True if the declarator is surrounded by a parenthesis pair.
6336 got-parens
6337 ;; True if there is an identifier in the declarator.
6338 got-identifier
6339 ;; True if there's a non-close-paren match of
6340 ;; `c-type-decl-suffix-key'.
6341 got-suffix
6342 ;; True if there's a prefix match outside the outermost
6343 ;; paren pair that surrounds the declarator.
6344 got-prefix-before-parens
6345 ;; True if there's a suffix match outside the outermost
6346 ;; paren pair that surrounds the declarator. The value is
6347 ;; the position of the first suffix match.
6348 got-suffix-after-parens
6349 ;; True if we've parsed the type decl to a token that is
6350 ;; known to end declarations in this context.
6351 at-decl-end
6352 ;; The earlier values of `at-type' and `type-start' if we've
6353 ;; shifted the type backwards.
6354 identifier-type identifier-start
6355 ;; If `c-parse-and-markup-<>-arglists' is set we need to
6356 ;; turn it off during the name skipping below to avoid
6357 ;; getting `c-type' properties that might be bogus. That
6358 ;; can happen since we don't know if
6359 ;; `c-restricted-<>-arglists' will be correct inside the
6360 ;; arglist paren that gets entered.
6361 c-parse-and-markup-<>-arglists)
6362
6363 (goto-char id-start)
6364
6365 ;; Skip over type decl prefix operators. (Note similar code in
6366 ;; `c-font-lock-declarators'.)
6367 (while (and (looking-at c-type-decl-prefix-key)
6368 (if (and (c-major-mode-is 'c++-mode)
6369 (match-beginning 2))
6370 ;; If the second submatch matches in C++ then
6371 ;; we're looking at an identifier that's a
6372 ;; prefix only if it specifies a member pointer.
6373 (when (setq got-identifier (c-forward-name))
6374 (if (looking-at "\\(::\\)")
6375 ;; We only check for a trailing "::" and
6376 ;; let the "*" that should follow be
6377 ;; matched in the next round.
6378 (progn (setq got-identifier nil) t)
6379 ;; It turned out to be the real identifier,
6380 ;; so stop.
6381 nil))
6382 t))
6383
6384 (if (eq (char-after) ?\()
6385 (progn
6386 (setq paren-depth (1+ paren-depth))
6387 (forward-char))
6388 (unless got-prefix-before-parens
6389 (setq got-prefix-before-parens (= paren-depth 0)))
6390 (setq got-prefix t)
6391 (goto-char (match-end 1)))
6392 (c-forward-syntactic-ws))
6393
6394 (setq got-parens (> paren-depth 0))
6395
6396 ;; Skip over an identifier.
6397 (or got-identifier
6398 (and (looking-at c-identifier-start)
6399 (setq got-identifier (c-forward-name))))
6400
6401 ;; Skip over type decl suffix operators.
6402 (while (if (looking-at c-type-decl-suffix-key)
6403
6404 (if (eq (char-after) ?\))
6405 (when (> paren-depth 0)
6406 (setq paren-depth (1- paren-depth))
6407 (forward-char)
6408 t)
6409 (when (if (save-match-data (looking-at "\\s\("))
6410 (c-safe (c-forward-sexp 1) t)
6411 (goto-char (match-end 1))
6412 t)
6413 (when (and (not got-suffix-after-parens)
6414 (= paren-depth 0))
6415 (setq got-suffix-after-parens (match-beginning 0)))
6416 (setq got-suffix t)))
6417
6418 ;; No suffix matched. We might have matched the
6419 ;; identifier as a type and the open paren of a
6420 ;; function arglist as a type decl prefix. In that
6421 ;; case we should "backtrack": Reinterpret the last
6422 ;; type as the identifier, move out of the arglist and
6423 ;; continue searching for suffix operators.
6424 ;;
6425 ;; Do this even if there's no preceding type, to cope
6426 ;; with old style function declarations in K&R C,
6427 ;; (con|de)structors in C++ and `c-typeless-decl-kwds'
6428 ;; style declarations. That isn't applicable in an
6429 ;; arglist context, though.
6430 (when (and (= paren-depth 1)
6431 (not got-prefix-before-parens)
6432 (not (eq at-type t))
6433 (or backup-at-type
6434 maybe-typeless
6435 backup-maybe-typeless
6436 (when c-recognize-typeless-decls
6437 (not context)))
6438 (setq pos (c-up-list-forward (point)))
6439 (eq (char-before pos) ?\)))
6440 (c-fdoc-shift-type-backward)
6441 (goto-char pos)
6442 t))
6443
6444 (c-forward-syntactic-ws))
6445
6446 (when (and (or maybe-typeless backup-maybe-typeless)
6447 (not got-identifier)
6448 (not got-prefix)
6449 at-type)
6450 ;; Have found no identifier but `c-typeless-decl-kwds' has
6451 ;; matched so we know we're inside a declaration. The
6452 ;; preceding type must be the identifier instead.
6453 (c-fdoc-shift-type-backward))
6454
6455 (setq
6456 at-decl-or-cast
6457 (catch 'at-decl-or-cast
6458
6459 ;; CASE 1
6460 (when (> paren-depth 0)
6461 ;; Encountered something inside parens that isn't matched by
6462 ;; the `c-type-decl-*' regexps, so it's not a type decl
6463 ;; expression. Try to skip out to the same paren depth to
6464 ;; not confuse the cast check below.
6465 (c-safe (goto-char (scan-lists (point) 1 paren-depth)))
6466 ;; If we've found a specifier keyword then it's a
6467 ;; declaration regardless.
6468 (throw 'at-decl-or-cast (eq at-decl-or-cast t)))
6469
6470 (setq at-decl-end
6471 (looking-at (cond ((eq context '<>) "[,>]")
6472 (context "[,\)]")
6473 (t "[,;]"))))
6474
6475 ;; Now we've collected info about various characteristics of
6476 ;; the construct we're looking at. Below follows a decision
6477 ;; tree based on that. It's ordered to check more certain
6478 ;; signs before less certain ones.
6479
6480 (if got-identifier
6481 (progn
6482
6483 ;; CASE 2
6484 (when (and (or at-type maybe-typeless)
6485 (not (or got-prefix got-parens)))
6486 ;; Got another identifier directly after the type, so it's a
6487 ;; declaration.
6488 (throw 'at-decl-or-cast t))
6489
6490 (when (and got-parens
6491 (not got-prefix)
6492 (not got-suffix-after-parens)
6493 (or backup-at-type
6494 maybe-typeless
6495 backup-maybe-typeless))
6496 ;; Got a declaration of the form "foo bar (gnu);" where we've
6497 ;; recognized "bar" as the type and "gnu" as the declarator.
6498 ;; In this case it's however more likely that "bar" is the
6499 ;; declarator and "gnu" a function argument or initializer (if
6500 ;; `c-recognize-paren-inits' is set), since the parens around
6501 ;; "gnu" would be superfluous if it's a declarator. Shift the
6502 ;; type one step backward.
6503 (c-fdoc-shift-type-backward)))
6504
6505 ;; Found no identifier.
6506
6507 (if backup-at-type
6508 (progn
6509
6510 ;; CASE 3
6511 (when (= (point) start)
6512 ;; Got a plain list of identifiers. If a colon follows it's
6513 ;; a valid label. Otherwise the last one probably is the
6514 ;; declared identifier and we should back up to the previous
6515 ;; type, providing it isn't a cast.
6516 (if (eq (char-after) ?:)
6517 ;; If we've found a specifier keyword then it's a
6518 ;; declaration regardless.
6519 (throw 'at-decl-or-cast (eq at-decl-or-cast t))
6520 (setq backup-if-not-cast t)
6521 (throw 'at-decl-or-cast t)))
6522
6523 ;; CASE 4
6524 (when (and got-suffix
6525 (not got-prefix)
6526 (not got-parens))
6527 ;; Got a plain list of identifiers followed by some suffix.
6528 ;; If this isn't a cast then the last identifier probably is
6529 ;; the declared one and we should back up to the previous
6530 ;; type.
6531 (setq backup-if-not-cast t)
6532 (throw 'at-decl-or-cast t)))
6533
6534 ;; CASE 5
6535 (when (eq at-type t)
6536 ;; If the type is known we know that there can't be any
6537 ;; identifier somewhere else, and it's only in declarations in
6538 ;; e.g. function prototypes and in casts that the identifier may
6539 ;; be left out.
6540 (throw 'at-decl-or-cast t))
6541
6542 (when (= (point) start)
6543 ;; Only got a single identifier (parsed as a type so far).
6544 ;; CASE 6
6545 (if (and
6546 ;; Check that the identifier isn't at the start of an
6547 ;; expression.
6548 at-decl-end
6549 (cond
6550 ((eq context 'decl)
6551 ;; Inside an arglist that contains declarations. If K&R
6552 ;; style declarations and parenthesis style initializers
6553 ;; aren't allowed then the single identifier must be a
6554 ;; type, else we require that it's known or found
6555 ;; (primitive types are handled above).
6556 (or (and (not c-recognize-knr-p)
6557 (not c-recognize-paren-inits))
6558 (memq at-type '(known found))))
6559 ((eq context '<>)
6560 ;; Inside a template arglist. Accept known and found
6561 ;; types; other identifiers could just as well be
6562 ;; constants in C++.
6563 (memq at-type '(known found)))))
6564 (throw 'at-decl-or-cast t)
6565 ;; CASE 7
6566 ;; Can't be a valid declaration or cast, but if we've found a
6567 ;; specifier it can't be anything else either, so treat it as
6568 ;; an invalid/unfinished declaration or cast.
6569 (throw 'at-decl-or-cast at-decl-or-cast))))
6570
6571 (if (and got-parens
6572 (not got-prefix)
6573 (not context)
6574 (not (eq at-type t))
6575 (or backup-at-type
6576 maybe-typeless
6577 backup-maybe-typeless
6578 (when c-recognize-typeless-decls
6579 (or (not got-suffix)
6580 (not (looking-at
6581 c-after-suffixed-type-maybe-decl-key))))))
6582 ;; Got an empty paren pair and a preceding type that probably
6583 ;; really is the identifier. Shift the type backwards to make
6584 ;; the last one the identifier. This is analogous to the
6585 ;; "backtracking" done inside the `c-type-decl-suffix-key' loop
6586 ;; above.
6587 ;;
6588 ;; Exception: In addition to the conditions in that
6589 ;; "backtracking" code, do not shift backward if we're not
6590 ;; looking at either `c-after-suffixed-type-decl-key' or "[;,]".
6591 ;; Since there's no preceding type, the shift would mean that
6592 ;; the declaration is typeless. But if the regexp doesn't match
6593 ;; then we will simply fall through in the tests below and not
6594 ;; recognize it at all, so it's better to try it as an abstract
6595 ;; declarator instead.
6596 (c-fdoc-shift-type-backward)
6597
6598 ;; Still no identifier.
6599 ;; CASE 8
6600 (when (and got-prefix (or got-parens got-suffix))
6601 ;; Require `got-prefix' together with either `got-parens' or
6602 ;; `got-suffix' to recognize it as an abstract declarator:
6603 ;; `got-parens' only is probably an empty function call.
6604 ;; `got-suffix' only can build an ordinary expression together
6605 ;; with the preceding identifier which we've taken as a type.
6606 ;; We could actually accept on `got-prefix' only, but that can
6607 ;; easily occur temporarily while writing an expression so we
6608 ;; avoid that case anyway. We could do a better job if we knew
6609 ;; the point when the fontification was invoked.
6610 (throw 'at-decl-or-cast t))
6611
6612 ;; CASE 9
6613 (when (and at-type
6614 (not got-prefix)
6615 (not got-parens)
6616 got-suffix-after-parens
6617 (eq (char-after got-suffix-after-parens) ?\())
6618 ;; Got a type, no declarator but a paren suffix. I.e. it's a
6619 ;; normal function call afterall (or perhaps a C++ style object
6620 ;; instantiation expression).
6621 (throw 'at-decl-or-cast nil))))
6622
6623 ;; CASE 10
6624 (when at-decl-or-cast
6625 ;; By now we've located the type in the declaration that we know
6626 ;; we're in.
6627 (throw 'at-decl-or-cast t))
6628
6629 ;; CASE 11
6630 (when (and got-identifier
6631 (not context)
6632 (looking-at c-after-suffixed-type-decl-key)
6633 (if (and got-parens
6634 (not got-prefix)
6635 (not got-suffix)
6636 (not (eq at-type t)))
6637 ;; Shift the type backward in the case that there's a
6638 ;; single identifier inside parens. That can only
6639 ;; occur in K&R style function declarations so it's
6640 ;; more likely that it really is a function call.
6641 ;; Therefore we only do this after
6642 ;; `c-after-suffixed-type-decl-key' has matched.
6643 (progn (c-fdoc-shift-type-backward) t)
6644 got-suffix-after-parens))
6645 ;; A declaration according to `c-after-suffixed-type-decl-key'.
6646 (throw 'at-decl-or-cast t))
6647
6648 ;; CASE 12
6649 (when (and (or got-prefix (not got-parens))
6650 (memq at-type '(t known)))
6651 ;; It's a declaration if a known type precedes it and it can't be a
6652 ;; function call.
6653 (throw 'at-decl-or-cast t))
6654
6655 ;; If we get here we can't tell if this is a type decl or a normal
6656 ;; expression by looking at it alone. (That's under the assumption
6657 ;; that normal expressions always can look like type decl expressions,
6658 ;; which isn't really true but the cases where it doesn't hold are so
6659 ;; uncommon (e.g. some placements of "const" in C++) it's not worth
6660 ;; the effort to look for them.)
6661
6662 (unless (or at-decl-end (looking-at "=[^=]"))
6663 ;; If this is a declaration it should end here or its initializer(*)
6664 ;; should start here, so check for allowed separation tokens. Note
6665 ;; that this rule doesn't work e.g. with a K&R arglist after a
6666 ;; function header.
6667 ;;
6668 ;; *) Don't check for C++ style initializers using parens
6669 ;; since those already have been matched as suffixes.
6670 ;;
6671 ;; If `at-decl-or-cast' is then we've found some other sign that
6672 ;; it's a declaration or cast, so then it's probably an
6673 ;; invalid/unfinished one.
6674 (throw 'at-decl-or-cast at-decl-or-cast))
6675
6676 ;; Below are tests that only should be applied when we're certain to
6677 ;; not have parsed halfway through an expression.
6678
6679 ;; CASE 14
6680 (when (memq at-type '(t known))
6681 ;; The expression starts with a known type so treat it as a
6682 ;; declaration.
6683 (throw 'at-decl-or-cast t))
6684
6685 ;; CASE 15
6686 (when (and (c-major-mode-is 'c++-mode)
6687 ;; In C++ we check if the identifier is a known type, since
6688 ;; (con|de)structors use the class name as identifier.
6689 ;; We've always shifted over the identifier as a type and
6690 ;; then backed up again in this case.
6691 identifier-type
6692 (or (memq identifier-type '(found known))
6693 (and (eq (char-after identifier-start) ?~)
6694 ;; `at-type' probably won't be 'found for
6695 ;; destructors since the "~" is then part of the
6696 ;; type name being checked against the list of
6697 ;; known types, so do a check without that
6698 ;; operator.
6699 (or (save-excursion
6700 (goto-char (1+ identifier-start))
6701 (c-forward-syntactic-ws)
6702 (c-with-syntax-table
6703 c-identifier-syntax-table
6704 (looking-at c-known-type-key)))
6705 (save-excursion
6706 (goto-char (1+ identifier-start))
6707 ;; We have already parsed the type earlier,
6708 ;; so it'd be possible to cache the end
6709 ;; position instead of redoing it here, but
6710 ;; then we'd need to keep track of another
6711 ;; position everywhere.
6712 (c-check-type (point)
6713 (progn (c-forward-type)
6714 (point))))))))
6715 (throw 'at-decl-or-cast t))
6716
6717 (if got-identifier
6718 (progn
6719 ;; CASE 16
6720 (when (and got-prefix-before-parens
6721 at-type
6722 (or at-decl-end (looking-at "=[^=]"))
6723 (not context)
6724 (not got-suffix))
6725 ;; Got something like "foo * bar;". Since we're not inside an
6726 ;; arglist it would be a meaningless expression because the
6727 ;; result isn't used. We therefore choose to recognize it as
6728 ;; a declaration. Do not allow a suffix since it could then
6729 ;; be a function call.
6730 (throw 'at-decl-or-cast t))
6731
6732 ;; CASE 17
6733 (when (and (or got-suffix-after-parens
6734 (looking-at "=[^=]"))
6735 (eq at-type 'found)
6736 (not (eq context 'arglist)))
6737 ;; Got something like "a (*b) (c);" or "a (b) = c;". It could
6738 ;; be an odd expression or it could be a declaration. Treat
6739 ;; it as a declaration if "a" has been used as a type
6740 ;; somewhere else (if it's a known type we won't get here).
6741 (throw 'at-decl-or-cast t)))
6742
6743 ;; CASE 18
6744 (when (and context
6745 (or got-prefix
6746 (and (eq context 'decl)
6747 (not c-recognize-paren-inits)
6748 (or got-parens got-suffix))))
6749 ;; Got a type followed by an abstract declarator. If `got-prefix'
6750 ;; is set it's something like "a *" without anything after it. If
6751 ;; `got-parens' or `got-suffix' is set it's "a()", "a[]", "a()[]",
6752 ;; or similar, which we accept only if the context rules out
6753 ;; expressions.
6754 (throw 'at-decl-or-cast t)))
6755
6756 ;; If we had a complete symbol table here (which rules out
6757 ;; `c-found-types') we should return t due to the disambiguation rule
6758 ;; (in at least C++) that anything that can be parsed as a declaration
6759 ;; is a declaration. Now we're being more defensive and prefer to
6760 ;; highlight things like "foo (bar);" as a declaration only if we're
6761 ;; inside an arglist that contains declarations.
6762 (eq context 'decl))))
6763
6764 ;; The point is now after the type decl expression.
6765
6766 (cond
6767 ;; Check for a cast.
6768 ((save-excursion
6769 (and
6770 c-cast-parens
6771
6772 ;; Should be the first type/identifier in a cast paren.
6773 (> preceding-token-end (point-min))
6774 (memq (char-before preceding-token-end) c-cast-parens)
6775
6776 ;; The closing paren should follow.
6777 (progn
6778 (c-forward-syntactic-ws)
6779 (looking-at "\\s\)"))
6780
6781 ;; There should be a primary expression after it.
6782 (let (pos)
6783 (forward-char)
6784 (c-forward-syntactic-ws)
6785 (setq cast-end (point))
6786 (and (looking-at c-primary-expr-regexp)
6787 (progn
6788 (setq pos (match-end 0))
6789 (or
6790 ;; Check if the expression begins with a prefix keyword.
6791 (match-beginning 2)
6792 (if (match-beginning 1)
6793 ;; Expression begins with an ambiguous operator. Treat
6794 ;; it as a cast if it's a type decl or if we've
6795 ;; recognized the type somewhere else.
6796 (or at-decl-or-cast
6797 (memq at-type '(t known found)))
6798 ;; Unless it's a keyword, it's the beginning of a primary
6799 ;; expression.
6800 (not (looking-at c-keywords-regexp)))))
6801 ;; If `c-primary-expr-regexp' matched a nonsymbol token, check
6802 ;; that it matched a whole one so that we don't e.g. confuse
6803 ;; the operator '-' with '->'. It's ok if it matches further,
6804 ;; though, since it e.g. can match the float '.5' while the
6805 ;; operator regexp only matches '.'.
6806 (or (not (looking-at c-nonsymbol-token-regexp))
6807 (<= (match-end 0) pos))))
6808
6809 ;; There should either be a cast before it or something that isn't an
6810 ;; identifier or close paren.
6811 (> preceding-token-end (point-min))
6812 (progn
6813 (goto-char (1- preceding-token-end))
6814 (or (eq (point) last-cast-end)
6815 (progn
6816 (c-backward-syntactic-ws)
6817 (if (< (skip-syntax-backward "w_") 0)
6818 ;; It's a symbol. Accept it only if it's one of the
6819 ;; keywords that can precede an expression (without
6820 ;; surrounding parens).
6821 (looking-at c-simple-stmt-key)
6822 (and
6823 ;; Check that it isn't a close paren (block close is ok,
6824 ;; though).
6825 (not (memq (char-before) '(?\) ?\])))
6826 ;; Check that it isn't a nonsymbol identifier.
6827 (not (c-on-identifier)))))))))
6828
6829 ;; Handle the cast.
6830 (when (and c-record-type-identifiers at-type (not (eq at-type t)))
6831 (let ((c-promote-possible-types t))
6832 (goto-char type-start)
6833 (c-forward-type)))
6834
6835 (goto-char cast-end)
6836 'cast)
6837
6838 (at-decl-or-cast
6839 ;; We're at a declaration. Highlight the type and the following
6840 ;; declarators.
6841
6842 (when backup-if-not-cast
6843 (c-fdoc-shift-type-backward t))
6844
6845 (when (and (eq context 'decl) (looking-at ","))
6846 ;; Make sure to propagate the `c-decl-arg-start' property to
6847 ;; the next argument if it's set in this one, to cope with
6848 ;; interactive refontification.
6849 (c-put-c-type-property (point) 'c-decl-arg-start))
6850
6851 (when (and c-record-type-identifiers at-type (not (eq at-type t)))
6852 (let ((c-promote-possible-types t))
6853 (save-excursion
6854 (goto-char type-start)
6855 (c-forward-type))))
6856
6857 (cons id-start at-type-decl))
6858
6859 (t
6860 ;; False alarm. Restore the recorded ranges.
6861 (setq c-record-type-identifiers save-rec-type-ids
6862 c-record-ref-identifiers save-rec-ref-ids)
6863 nil))))
6864
6865 (defun c-forward-label (&optional assume-markup preceding-token-end limit)
6866 ;; Assuming that point is at the beginning of a token, check if it starts a
6867 ;; label and if so move over it and return non-nil (t in default situations,
6868 ;; specific symbols (see below) for interesting situations), otherwise don't
6869 ;; move and return nil. "Label" here means "most things with a colon".
6870 ;;
6871 ;; More precisely, a "label" is regarded as one of:
6872 ;; (i) a goto target like "foo:" - returns the symbol `goto-target';
6873 ;; (ii) A case label - either the entire construct "case FOO:", or just the
6874 ;; bare "case", should the colon be missing. We return t;
6875 ;; (iii) a keyword which needs a colon, like "default:" or "private:"; We
6876 ;; return t;
6877 ;; (iv) One of QT's "extended" C++ variants of
6878 ;; "private:"/"protected:"/"public:"/"more:" looking like "public slots:".
6879 ;; Returns the symbol `qt-2kwds-colon'.
6880 ;; (v) QT's construct "signals:". Returns the symbol `qt-1kwd-colon'.
6881 ;; (vi) One of the keywords matched by `c-opt-extra-label-key' (without any
6882 ;; colon). Currently (2006-03), this applies only to Objective C's
6883 ;; keywords "@private", "@protected", and "@public". Returns t.
6884 ;;
6885 ;; One of the things which will NOT be recognised as a label is a bit-field
6886 ;; element of a struct, something like "int foo:5".
6887 ;;
6888 ;; The end of the label is taken to be just after the colon, or the end of
6889 ;; the first submatch in `c-opt-extra-label-key'. The point is directly
6890 ;; after the end on return. The terminating char gets marked with
6891 ;; `c-decl-end' to improve recognition of the following declaration or
6892 ;; statement.
6893 ;;
6894 ;; If ASSUME-MARKUP is non-nil, it's assumed that the preceding
6895 ;; label, if any, has already been marked up like that.
6896 ;;
6897 ;; If PRECEDING-TOKEN-END is given, it should be the first position
6898 ;; after the preceding token, i.e. on the other side of the
6899 ;; syntactic ws from the point. Use a value less than or equal to
6900 ;; (point-min) if the point is at the first token in (the visible
6901 ;; part of) the buffer.
6902 ;;
6903 ;; The optional LIMIT limits the forward scan for the colon.
6904 ;;
6905 ;; This function records the ranges of the label symbols on
6906 ;; `c-record-ref-identifiers' if `c-record-type-identifiers' (!) is
6907 ;; non-nil.
6908 ;;
6909 ;; This function might do hidden buffer changes.
6910
6911 (let ((start (point))
6912 label-end
6913 qt-symbol-idx
6914 macro-start ; if we're in one.
6915 label-type
6916 kwd)
6917 (cond
6918 ;; "case" or "default" (Doesn't apply to AWK).
6919 ((looking-at c-label-kwds-regexp)
6920 (let ((kwd-end (match-end 1)))
6921 ;; Record only the keyword itself for fontification, since in
6922 ;; case labels the following is a constant expression and not
6923 ;; a label.
6924 (when c-record-type-identifiers
6925 (c-record-ref-id (cons (match-beginning 1) kwd-end)))
6926
6927 ;; Find the label end.
6928 (goto-char kwd-end)
6929 (setq label-type
6930 (if (and (c-syntactic-re-search-forward
6931 ;; Stop on chars that aren't allowed in expressions,
6932 ;; and on operator chars that would be meaningless
6933 ;; there. FIXME: This doesn't cope with ?: operators.
6934 "[;{=,@]\\|\\(\\=\\|[^:]\\):\\([^:]\\|\\'\\)"
6935 limit t t nil 1)
6936 (match-beginning 2))
6937
6938 (progn ; there's a proper :
6939 (goto-char (match-beginning 2)) ; just after the :
6940 (c-put-c-type-property (1- (point)) 'c-decl-end)
6941 t)
6942
6943 ;; It's an unfinished label. We consider the keyword enough
6944 ;; to recognize it as a label, so that it gets fontified.
6945 ;; Leave the point at the end of it, but don't put any
6946 ;; `c-decl-end' marker.
6947 (goto-char kwd-end)
6948 t))))
6949
6950 ;; @private, @protected, @public, in Objective C, or similar.
6951 ((and c-opt-extra-label-key
6952 (looking-at c-opt-extra-label-key))
6953 ;; For a `c-opt-extra-label-key' match, we record the whole
6954 ;; thing for fontification. That's to get the leading '@' in
6955 ;; Objective-C protection labels fontified.
6956 (goto-char (match-end 1))
6957 (when c-record-type-identifiers
6958 (c-record-ref-id (cons (match-beginning 1) (point))))
6959 (c-put-c-type-property (1- (point)) 'c-decl-end)
6960 (setq label-type t))
6961
6962 ;; All other cases of labels.
6963 ((and c-recognize-colon-labels ; nil for AWK and IDL, otherwise t.
6964
6965 ;; A colon label must have something before the colon.
6966 (not (eq (char-after) ?:))
6967
6968 ;; Check that we're not after a token that can't precede a label.
6969 (or
6970 ;; Trivially succeeds when there's no preceding token.
6971 (if preceding-token-end
6972 (<= preceding-token-end (point-min))
6973 (save-excursion
6974 (c-backward-syntactic-ws)
6975 (setq preceding-token-end (point))
6976 (bobp)))
6977
6978 ;; Check if we're after a label, if we're after a closing
6979 ;; paren that belong to statement, and with
6980 ;; `c-label-prefix-re'. It's done in different order
6981 ;; depending on `assume-markup' since the checks have
6982 ;; different expensiveness.
6983 (if assume-markup
6984 (or
6985 (eq (c-get-char-property (1- preceding-token-end) 'c-type)
6986 'c-decl-end)
6987
6988 (save-excursion
6989 (goto-char (1- preceding-token-end))
6990 (c-beginning-of-current-token)
6991 (or (looking-at c-label-prefix-re)
6992 (looking-at c-block-stmt-1-key)))
6993
6994 (and (eq (char-before preceding-token-end) ?\))
6995 (c-after-conditional)))
6996
6997 (or
6998 (save-excursion
6999 (goto-char (1- preceding-token-end))
7000 (c-beginning-of-current-token)
7001 (or (looking-at c-label-prefix-re)
7002 (looking-at c-block-stmt-1-key)))
7003
7004 (cond
7005 ((eq (char-before preceding-token-end) ?\))
7006 (c-after-conditional))
7007
7008 ((eq (char-before preceding-token-end) ?:)
7009 ;; Might be after another label, so check it recursively.
7010 (save-restriction
7011 (save-excursion
7012 (goto-char (1- preceding-token-end))
7013 ;; Essentially the same as the
7014 ;; `c-syntactic-re-search-forward' regexp below.
7015 (setq macro-start
7016 (save-excursion (and (c-beginning-of-macro)
7017 (point))))
7018 (if macro-start (narrow-to-region macro-start (point-max)))
7019 (c-syntactic-skip-backward "^-]:?;}=*/%&|,<>!@+" nil t)
7020 ;; Note: the following should work instead of the
7021 ;; narrow-to-region above. Investigate why not,
7022 ;; sometime. ACM, 2006-03-31.
7023 ;; (c-syntactic-skip-backward "^-]:?;}=*/%&|,<>!@+"
7024 ;; macro-start t)
7025 (let ((pte (point))
7026 ;; If the caller turned on recording for us,
7027 ;; it shouldn't apply when we check the
7028 ;; preceding label.
7029 c-record-type-identifiers)
7030 ;; A label can't start at a cpp directive. Check for
7031 ;; this, since c-forward-syntactic-ws would foul up on it.
7032 (unless (and c-opt-cpp-prefix (looking-at c-opt-cpp-prefix))
7033 (c-forward-syntactic-ws)
7034 (c-forward-label nil pte start))))))))))
7035
7036 ;; Point is still at the beginning of the possible label construct.
7037 ;;
7038 ;; Check that the next nonsymbol token is ":", or that we're in one
7039 ;; of QT's "slots" declarations. Allow '(' for the sake of macro
7040 ;; arguments. FIXME: Should build this regexp from the language
7041 ;; constants.
7042 (cond
7043 ;; public: protected: private:
7044 ((and
7045 (c-major-mode-is 'c++-mode)
7046 (search-forward-regexp
7047 "\\=p\\(r\\(ivate\\|otected\\)\\|ublic\\)\\>[^_]" nil t)
7048 (progn (backward-char)
7049 (c-forward-syntactic-ws limit)
7050 (looking-at ":\\([^:]\\|\\'\\)"))) ; A single colon.
7051 (forward-char)
7052 (setq label-type t))
7053 ;; QT double keyword like "protected slots:" or goto target.
7054 ((progn (goto-char start) nil))
7055 ((when (c-syntactic-re-search-forward
7056 "[ \t\n[:?;{=*/%&|,<>!@+-]" limit t t) ; not at EOB
7057 (backward-char)
7058 (setq label-end (point))
7059 (setq qt-symbol-idx
7060 (and (c-major-mode-is 'c++-mode)
7061 (string-match
7062 "\\(p\\(r\\(ivate\\|otected\\)\\|ublic\\)\\|more\\)\\>"
7063 (buffer-substring start (point)))))
7064 (c-forward-syntactic-ws limit)
7065 (cond
7066 ((looking-at ":\\([^:]\\|\\'\\)") ; A single colon.
7067 (forward-char)
7068 (setq label-type
7069 (if (or (string= "signals" ; Special QT macro
7070 (setq kwd (buffer-substring-no-properties start label-end)))
7071 (string= "Q_SIGNALS" kwd))
7072 'qt-1kwd-colon
7073 'goto-target)))
7074 ((and qt-symbol-idx
7075 (search-forward-regexp "\\=\\(slots\\|Q_SLOTS\\)\\>" limit t)
7076 (progn (c-forward-syntactic-ws limit)
7077 (looking-at ":\\([^:]\\|\\'\\)"))) ; A single colon
7078 (forward-char)
7079 (setq label-type 'qt-2kwds-colon)))))))
7080
7081 (save-restriction
7082 (narrow-to-region start (point))
7083
7084 ;; Check that `c-nonlabel-token-key' doesn't match anywhere.
7085 (catch 'check-label
7086 (goto-char start)
7087 (while (progn
7088 (when (looking-at c-nonlabel-token-key)
7089 (goto-char start)
7090 (setq label-type nil)
7091 (throw 'check-label nil))
7092 (and (c-safe (c-forward-sexp)
7093 (c-forward-syntactic-ws)
7094 t)
7095 (not (eobp)))))
7096
7097 ;; Record the identifiers in the label for fontification, unless
7098 ;; it begins with `c-label-kwds' in which case the following
7099 ;; identifiers are part of a (constant) expression that
7100 ;; shouldn't be fontified.
7101 (when (and c-record-type-identifiers
7102 (progn (goto-char start)
7103 (not (looking-at c-label-kwds-regexp))))
7104 (while (c-syntactic-re-search-forward c-symbol-key nil t)
7105 (c-record-ref-id (cons (match-beginning 0)
7106 (match-end 0)))))
7107
7108 (c-put-c-type-property (1- (point-max)) 'c-decl-end)
7109 (goto-char (point-max)))))
7110
7111 (t
7112 ;; Not a label.
7113 (goto-char start)))
7114 label-type))
7115
7116 (defun c-forward-objc-directive ()
7117 ;; Assuming the point is at the beginning of a token, try to move
7118 ;; forward to the end of the Objective-C directive that starts
7119 ;; there. Return t if a directive was fully recognized, otherwise
7120 ;; the point is moved as far as one could be successfully parsed and
7121 ;; nil is returned.
7122 ;;
7123 ;; This function records identifier ranges on
7124 ;; `c-record-type-identifiers' and `c-record-ref-identifiers' if
7125 ;; `c-record-type-identifiers' is non-nil.
7126 ;;
7127 ;; This function might do hidden buffer changes.
7128
7129 (let ((start (point))
7130 start-char
7131 (c-promote-possible-types t)
7132 ;; Turn off recognition of angle bracket arglists while parsing
7133 ;; types here since the protocol reference list might then be
7134 ;; considered part of the preceding name or superclass-name.
7135 c-recognize-<>-arglists)
7136
7137 (if (or
7138 (when (looking-at
7139 (eval-when-compile
7140 (c-make-keywords-re t
7141 (append (c-lang-const c-protection-kwds objc)
7142 '("@end"))
7143 'objc-mode)))
7144 (goto-char (match-end 1))
7145 t)
7146
7147 (and
7148 (looking-at
7149 (eval-when-compile
7150 (c-make-keywords-re t
7151 '("@interface" "@implementation" "@protocol")
7152 'objc-mode)))
7153
7154 ;; Handle the name of the class itself.
7155 (progn
7156 ; (c-forward-token-2) ; 2006/1/13 This doesn't move if the token's
7157 ; at EOB.
7158 (goto-char (match-end 0))
7159 (c-skip-ws-forward)
7160 (c-forward-type))
7161
7162 (catch 'break
7163 ;; Look for ": superclass-name" or "( category-name )".
7164 (when (looking-at "[:\(]")
7165 (setq start-char (char-after))
7166 (forward-char)
7167 (c-forward-syntactic-ws)
7168 (unless (c-forward-type) (throw 'break nil))
7169 (when (eq start-char ?\()
7170 (unless (eq (char-after) ?\)) (throw 'break nil))
7171 (forward-char)
7172 (c-forward-syntactic-ws)))
7173
7174 ;; Look for a protocol reference list.
7175 (if (eq (char-after) ?<)
7176 (let ((c-recognize-<>-arglists t)
7177 (c-parse-and-markup-<>-arglists t)
7178 c-restricted-<>-arglists)
7179 (c-forward-<>-arglist t))
7180 t))))
7181
7182 (progn
7183 (c-backward-syntactic-ws)
7184 (c-clear-c-type-property start (1- (point)) 'c-decl-end)
7185 (c-put-c-type-property (1- (point)) 'c-decl-end)
7186 t)
7187
7188 (c-clear-c-type-property start (point) 'c-decl-end)
7189 nil)))
7190
7191 (defun c-beginning-of-inheritance-list (&optional lim)
7192 ;; Go to the first non-whitespace after the colon that starts a
7193 ;; multiple inheritance introduction. Optional LIM is the farthest
7194 ;; back we should search.
7195 ;;
7196 ;; This function might do hidden buffer changes.
7197 (c-with-syntax-table c++-template-syntax-table
7198 (c-backward-token-2 0 t lim)
7199 (while (and (or (looking-at c-symbol-start)
7200 (looking-at "[<,]\\|::"))
7201 (zerop (c-backward-token-2 1 t lim))))))
7202
7203 (defun c-in-method-def-p ()
7204 ;; Return nil if we aren't in a method definition, otherwise the
7205 ;; position of the initial [+-].
7206 ;;
7207 ;; This function might do hidden buffer changes.
7208 (save-excursion
7209 (beginning-of-line)
7210 (and c-opt-method-key
7211 (looking-at c-opt-method-key)
7212 (point))
7213 ))
7214
7215 ;; Contributed by Kevin Ryde <user42@zip.com.au>.
7216 (defun c-in-gcc-asm-p ()
7217 ;; Return non-nil if point is within a gcc \"asm\" block.
7218 ;;
7219 ;; This should be called with point inside an argument list.
7220 ;;
7221 ;; Only one level of enclosing parentheses is considered, so for
7222 ;; instance `nil' is returned when in a function call within an asm
7223 ;; operand.
7224 ;;
7225 ;; This function might do hidden buffer changes.
7226
7227 (and c-opt-asm-stmt-key
7228 (save-excursion
7229 (beginning-of-line)
7230 (backward-up-list 1)
7231 (c-beginning-of-statement-1 (point-min) nil t)
7232 (looking-at c-opt-asm-stmt-key))))
7233
7234 (defun c-at-toplevel-p ()
7235 "Return a determination as to whether point is \"at the top level\".
7236 Informally, \"at the top level\" is anywhere where you can write
7237 a function.
7238
7239 More precisely, being at the top-level means that point is either
7240 outside any enclosing block (such as a function definition), or
7241 directly inside a class, namespace or other block that contains
7242 another declaration level.
7243
7244 If point is not at the top-level (e.g. it is inside a method
7245 definition), then nil is returned. Otherwise, if point is at a
7246 top-level not enclosed within a class definition, t is returned.
7247 Otherwise, a 2-vector is returned where the zeroth element is the
7248 buffer position of the start of the class declaration, and the first
7249 element is the buffer position of the enclosing class's opening
7250 brace.
7251
7252 Note that this function might do hidden buffer changes. See the
7253 comment at the start of cc-engine.el for more info."
7254 (let ((paren-state (c-parse-state)))
7255 (or (not (c-most-enclosing-brace paren-state))
7256 (c-search-uplist-for-classkey paren-state))))
7257
7258 (defun c-just-after-func-arglist-p (&optional lim)
7259 ;; Return non-nil if the point is in the region after the argument
7260 ;; list of a function and its opening brace (or semicolon in case it
7261 ;; got no body). If there are K&R style argument declarations in
7262 ;; that region, the point has to be inside the first one for this
7263 ;; function to recognize it.
7264 ;;
7265 ;; If successful, the point is moved to the first token after the
7266 ;; function header (see `c-forward-decl-or-cast-1' for details) and
7267 ;; the position of the opening paren of the function arglist is
7268 ;; returned.
7269 ;;
7270 ;; The point is clobbered if not successful.
7271 ;;
7272 ;; LIM is used as bound for backward buffer searches.
7273 ;;
7274 ;; This function might do hidden buffer changes.
7275
7276 (let ((beg (point)) end id-start)
7277 (and
7278 (eq (c-beginning-of-statement-1 lim) 'same)
7279
7280 (not (or (c-major-mode-is 'objc-mode)
7281 (c-forward-objc-directive)))
7282
7283 (setq id-start
7284 (car-safe (c-forward-decl-or-cast-1 (c-point 'bosws) nil nil)))
7285 (< id-start beg)
7286
7287 ;; There should not be a '=' or ',' between beg and the
7288 ;; start of the declaration since that means we were in the
7289 ;; "expression part" of the declaration.
7290 (or (> (point) beg)
7291 (not (looking-at "[=,]")))
7292
7293 (save-excursion
7294 ;; Check that there's an arglist paren in the
7295 ;; declaration.
7296 (goto-char id-start)
7297 (cond ((eq (char-after) ?\()
7298 ;; The declarator is a paren expression, so skip past it
7299 ;; so that we don't get stuck on that instead of the
7300 ;; function arglist.
7301 (c-forward-sexp))
7302 ((and c-opt-op-identifier-prefix
7303 (looking-at c-opt-op-identifier-prefix))
7304 ;; Don't trip up on "operator ()".
7305 (c-forward-token-2 2 t)))
7306 (and (< (point) beg)
7307 (c-syntactic-re-search-forward "(" beg t t)
7308 (1- (point)))))))
7309
7310 (defun c-in-knr-argdecl (&optional lim)
7311 ;; Return the position of the first argument declaration if point is
7312 ;; inside a K&R style argument declaration list, nil otherwise.
7313 ;; `c-recognize-knr-p' is not checked. If LIM is non-nil, it's a
7314 ;; position that bounds the backward search for the argument list.
7315 ;;
7316 ;; Point must be within a possible K&R region, e.g. just before a top-level
7317 ;; "{". It must be outside of parens and brackets. The test can return
7318 ;; false positives otherwise.
7319 ;;
7320 ;; This function might do hidden buffer changes.
7321
7322 (save-excursion
7323 (save-restriction
7324 ;; If we're in a macro, our search range is restricted to it. Narrow to
7325 ;; the searchable range.
7326 (let* ((macro-start (c-query-macro-start))
7327 (lim (max (or lim (point-min)) (or macro-start (point-min))))
7328 before-lparen after-rparen
7329 (pp-count-out 20)) ; Max number of paren/brace constructs before we give up
7330 (narrow-to-region lim (c-point 'eol))
7331
7332 ;; Search backwards for the defun's argument list. We give up if we
7333 ;; encounter a "}" (end of a previous defun) or BOB.
7334 ;;
7335 ;; The criterion for a paren structure being the arg list is:
7336 ;; o - there is non-WS stuff after it but before any "{"; AND
7337 ;; o - the token after it isn't a ";" AND
7338 ;; o - it is preceded by either an identifier (the function name) or
7339 ;; a macro expansion like "DEFUN (...)"; AND
7340 ;; o - its content is a non-empty comma-separated list of identifiers
7341 ;; (an empty arg list won't have a knr region).
7342 ;;
7343 ;; The following snippet illustrates these rules:
7344 ;; int foo (bar, baz, yuk)
7345 ;; int bar [] ;
7346 ;; int (*baz) (my_type) ;
7347 ;; int (*) (void) (*yuk) (void) ;
7348 ;; {
7349
7350 (catch 'knr
7351 (while (> pp-count-out 0) ; go back one paren/bracket pair each time.
7352 (setq pp-count-out (1- pp-count-out))
7353 (c-syntactic-skip-backward "^)]}")
7354 (cond ((eq (char-before) ?\))
7355 (setq after-rparen (point)))
7356 ((eq (char-before) ?\])
7357 (setq after-rparen nil))
7358 (t ; either } (hit previous defun) or no more parens/brackets
7359 (throw 'knr nil)))
7360
7361 (if after-rparen
7362 ;; We're inside a paren. Could it be our argument list....?
7363 (if
7364 (and
7365 (progn
7366 (goto-char after-rparen)
7367 (unless (c-go-list-backward) (throw 'knr nil)) ;
7368 ;; FIXME!!! What about macros between the parens? 2007/01/20
7369 (setq before-lparen (point)))
7370
7371 ;; It can't be the arg list if next token is ; or {
7372 (progn (goto-char after-rparen)
7373 (c-forward-syntactic-ws)
7374 (not (memq (char-after) '(?\; ?\{))))
7375
7376 ;; Is the thing preceding the list an identifier (the
7377 ;; function name), or a macro expansion?
7378 (progn
7379 (goto-char before-lparen)
7380 (eq (c-backward-token-2) 0)
7381 (or (c-on-identifier)
7382 (and (eq (char-after) ?\))
7383 (c-go-up-list-backward)
7384 (eq (c-backward-token-2) 0)
7385 (c-on-identifier))))
7386
7387 ;; Have we got a non-empty list of comma-separated
7388 ;; identifiers?
7389 (progn
7390 (goto-char before-lparen)
7391 (c-forward-token-2) ; to first token inside parens
7392 (and
7393 (c-on-identifier)
7394 (c-forward-token-2)
7395 (catch 'id-list
7396 (while (eq (char-after) ?\,)
7397 (c-forward-token-2)
7398 (unless (c-on-identifier) (throw 'id-list nil))
7399 (c-forward-token-2))
7400 (eq (char-after) ?\))))))
7401
7402 ;; ...Yes. We've identified the function's argument list.
7403 (throw 'knr
7404 (progn (goto-char after-rparen)
7405 (c-forward-syntactic-ws)
7406 (point)))
7407
7408 ;; ...No. The current parens aren't the function's arg list.
7409 (goto-char before-lparen))
7410
7411 (or (c-go-list-backward) ; backwards over [ .... ]
7412 (throw 'knr nil)))))))))
7413
7414 (defun c-skip-conditional ()
7415 ;; skip forward over conditional at point, including any predicate
7416 ;; statements in parentheses. No error checking is performed.
7417 ;;
7418 ;; This function might do hidden buffer changes.
7419 (c-forward-sexp (cond
7420 ;; else if()
7421 ((looking-at (concat "\\<else"
7422 "\\([ \t\n]\\|\\\\\n\\)+"
7423 "if\\>\\([^_]\\|$\\)"))
7424 3)
7425 ;; do, else, try, finally
7426 ((looking-at (concat "\\<\\("
7427 "do\\|else\\|try\\|finally"
7428 "\\)\\>\\([^_]\\|$\\)"))
7429 1)
7430 ;; for, if, while, switch, catch, synchronized, foreach
7431 (t 2))))
7432
7433 (defun c-after-conditional (&optional lim)
7434 ;; If looking at the token after a conditional then return the
7435 ;; position of its start, otherwise return nil.
7436 ;;
7437 ;; This function might do hidden buffer changes.
7438 (save-excursion
7439 (and (zerop (c-backward-token-2 1 t lim))
7440 (or (looking-at c-block-stmt-1-key)
7441 (and (eq (char-after) ?\()
7442 (zerop (c-backward-token-2 1 t lim))
7443 (looking-at c-block-stmt-2-key)))
7444 (point))))
7445
7446 (defun c-after-special-operator-id (&optional lim)
7447 ;; If the point is after an operator identifier that isn't handled
7448 ;; like an ordinary symbol (i.e. like "operator =" in C++) then the
7449 ;; position of the start of that identifier is returned. nil is
7450 ;; returned otherwise. The point may be anywhere in the syntactic
7451 ;; whitespace after the last token of the operator identifier.
7452 ;;
7453 ;; This function might do hidden buffer changes.
7454 (save-excursion
7455 (and c-overloadable-operators-regexp
7456 (zerop (c-backward-token-2 1 nil lim))
7457 (looking-at c-overloadable-operators-regexp)
7458 (or (not c-opt-op-identifier-prefix)
7459 (and
7460 (zerop (c-backward-token-2 1 nil lim))
7461 (looking-at c-opt-op-identifier-prefix)))
7462 (point))))
7463
7464 (defsubst c-backward-to-block-anchor (&optional lim)
7465 ;; Assuming point is at a brace that opens a statement block of some
7466 ;; kind, move to the proper anchor point for that block. It might
7467 ;; need to be adjusted further by c-add-stmt-syntax, but the
7468 ;; position at return is suitable as start position for that
7469 ;; function.
7470 ;;
7471 ;; This function might do hidden buffer changes.
7472 (unless (= (point) (c-point 'boi))
7473 (let ((start (c-after-conditional lim)))
7474 (if start
7475 (goto-char start)))))
7476
7477 (defsubst c-backward-to-decl-anchor (&optional lim)
7478 ;; Assuming point is at a brace that opens the block of a top level
7479 ;; declaration of some kind, move to the proper anchor point for
7480 ;; that block.
7481 ;;
7482 ;; This function might do hidden buffer changes.
7483 (unless (= (point) (c-point 'boi))
7484 (c-beginning-of-statement-1 lim)))
7485
7486 (defun c-search-decl-header-end ()
7487 ;; Search forward for the end of the "header" of the current
7488 ;; declaration. That's the position where the definition body
7489 ;; starts, or the first variable initializer, or the ending
7490 ;; semicolon. I.e. search forward for the closest following
7491 ;; (syntactically relevant) '{', '=' or ';' token. Point is left
7492 ;; _after_ the first found token, or at point-max if none is found.
7493 ;;
7494 ;; This function might do hidden buffer changes.
7495
7496 (let ((base (point)))
7497 (if (c-major-mode-is 'c++-mode)
7498
7499 ;; In C++ we need to take special care to handle operator
7500 ;; tokens and those pesky template brackets.
7501 (while (and
7502 (c-syntactic-re-search-forward "[;{<=]" nil 'move t t)
7503 (or
7504 (c-end-of-current-token base)
7505 ;; Handle operator identifiers, i.e. ignore any
7506 ;; operator token preceded by "operator".
7507 (save-excursion
7508 (and (c-safe (c-backward-sexp) t)
7509 (looking-at c-opt-op-identifier-prefix)))
7510 (and (eq (char-before) ?<)
7511 (c-with-syntax-table c++-template-syntax-table
7512 (if (c-safe (goto-char (c-up-list-forward (point))))
7513 t
7514 (goto-char (point-max))
7515 nil)))))
7516 (setq base (point)))
7517
7518 (while (and
7519 (c-syntactic-re-search-forward "[;{=]" nil 'move t t)
7520 (c-end-of-current-token base))
7521 (setq base (point))))))
7522
7523 (defun c-beginning-of-decl-1 (&optional lim)
7524 ;; Go to the beginning of the current declaration, or the beginning
7525 ;; of the previous one if already at the start of it. Point won't
7526 ;; be moved out of any surrounding paren. Return a cons cell of the
7527 ;; form (MOVE . KNR-POS). MOVE is like the return value from
7528 ;; `c-beginning-of-statement-1'. If point skipped over some K&R
7529 ;; style argument declarations (and they are to be recognized) then
7530 ;; KNR-POS is set to the start of the first such argument
7531 ;; declaration, otherwise KNR-POS is nil. If LIM is non-nil, it's a
7532 ;; position that bounds the backward search.
7533 ;;
7534 ;; NB: Cases where the declaration continues after the block, as in
7535 ;; "struct foo { ... } bar;", are currently recognized as two
7536 ;; declarations, e.g. "struct foo { ... }" and "bar;" in this case.
7537 ;;
7538 ;; This function might do hidden buffer changes.
7539 (catch 'return
7540 (let* ((start (point))
7541 (last-stmt-start (point))
7542 (move (c-beginning-of-statement-1 lim nil t)))
7543
7544 ;; `c-beginning-of-statement-1' stops at a block start, but we
7545 ;; want to continue if the block doesn't begin a top level
7546 ;; construct, i.e. if it isn't preceded by ';', '}', ':', bob,
7547 ;; or an open paren.
7548 (let ((beg (point)) tentative-move)
7549 ;; Go back one "statement" each time round the loop until we're just
7550 ;; after a ;, }, or :, or at BOB or the start of a macro or start of
7551 ;; an ObjC method. This will move over a multiple declaration whose
7552 ;; components are comma separated.
7553 (while (and
7554 ;; Must check with c-opt-method-key in ObjC mode.
7555 (not (and c-opt-method-key
7556 (looking-at c-opt-method-key)))
7557 (/= last-stmt-start (point))
7558 (progn
7559 (c-backward-syntactic-ws lim)
7560 (not (memq (char-before) '(?\; ?} ?: nil))))
7561 (save-excursion
7562 (backward-char)
7563 (not (looking-at "\\s(")))
7564 ;; Check that we don't move from the first thing in a
7565 ;; macro to its header.
7566 (not (eq (setq tentative-move
7567 (c-beginning-of-statement-1 lim nil t))
7568 'macro)))
7569 (setq last-stmt-start beg
7570 beg (point)
7571 move tentative-move))
7572 (goto-char beg))
7573
7574 (when c-recognize-knr-p
7575 (let ((fallback-pos (point)) knr-argdecl-start)
7576 ;; Handle K&R argdecls. Back up after the "statement" jumped
7577 ;; over by `c-beginning-of-statement-1', unless it was the
7578 ;; function body, in which case we're sitting on the opening
7579 ;; brace now. Then test if we're in a K&R argdecl region and
7580 ;; that we started at the other side of the first argdecl in
7581 ;; it.
7582 (unless (eq (char-after) ?{)
7583 (goto-char last-stmt-start))
7584 (if (and (setq knr-argdecl-start (c-in-knr-argdecl lim))
7585 (< knr-argdecl-start start)
7586 (progn
7587 (goto-char knr-argdecl-start)
7588 (not (eq (c-beginning-of-statement-1 lim nil t) 'macro))))
7589 (throw 'return
7590 (cons (if (eq (char-after fallback-pos) ?{)
7591 'previous
7592 'same)
7593 knr-argdecl-start))
7594 (goto-char fallback-pos))))
7595
7596 ;; `c-beginning-of-statement-1' counts each brace block as a separate
7597 ;; statement, so the result will be 'previous if we've moved over any.
7598 ;; So change our result back to 'same if necessary.
7599 ;;
7600 ;; If they were brace list initializers we might not have moved over a
7601 ;; declaration boundary though, so change it to 'same if we've moved
7602 ;; past a '=' before '{', but not ';'. (This ought to be integrated
7603 ;; into `c-beginning-of-statement-1', so we avoid this extra pass which
7604 ;; potentially can search over a large amount of text.). Take special
7605 ;; pains not to get mislead by C++'s "operator=", and the like.
7606 (if (and (eq move 'previous)
7607 (c-with-syntax-table (if (c-major-mode-is 'c++-mode)
7608 c++-template-syntax-table
7609 (syntax-table))
7610 (save-excursion
7611 (and
7612 (progn
7613 (while ; keep going back to "[;={"s until we either find
7614 ; no more, or get to one which isn't an "operator ="
7615 (and (c-syntactic-re-search-forward "[;={]" start t t t)
7616 (eq (char-before) ?=)
7617 c-overloadable-operators-regexp
7618 c-opt-op-identifier-prefix
7619 (save-excursion
7620 (eq (c-backward-token-2) 0)
7621 (looking-at c-overloadable-operators-regexp)
7622 (eq (c-backward-token-2) 0)
7623 (looking-at c-opt-op-identifier-prefix))))
7624 (eq (char-before) ?=))
7625 (c-syntactic-re-search-forward "[;{]" start t t)
7626 (eq (char-before) ?{)
7627 (c-safe (goto-char (c-up-list-forward (point))) t)
7628 (not (c-syntactic-re-search-forward ";" start t t))))))
7629 (cons 'same nil)
7630 (cons move nil)))))
7631
7632 (defun c-end-of-decl-1 ()
7633 ;; Assuming point is at the start of a declaration (as detected by
7634 ;; e.g. `c-beginning-of-decl-1'), go to the end of it. Unlike
7635 ;; `c-beginning-of-decl-1', this function handles the case when a
7636 ;; block is followed by identifiers in e.g. struct declarations in C
7637 ;; or C++. If a proper end was found then t is returned, otherwise
7638 ;; point is moved as far as possible within the current sexp and nil
7639 ;; is returned. This function doesn't handle macros; use
7640 ;; `c-end-of-macro' instead in those cases.
7641 ;;
7642 ;; This function might do hidden buffer changes.
7643 (let ((start (point))
7644 (decl-syntax-table (if (c-major-mode-is 'c++-mode)
7645 c++-template-syntax-table
7646 (syntax-table))))
7647 (catch 'return
7648 (c-search-decl-header-end)
7649
7650 (when (and c-recognize-knr-p
7651 (eq (char-before) ?\;)
7652 (c-in-knr-argdecl start))
7653 ;; Stopped at the ';' in a K&R argdecl section which is
7654 ;; detected using the same criteria as in
7655 ;; `c-beginning-of-decl-1'. Move to the following block
7656 ;; start.
7657 (c-syntactic-re-search-forward "{" nil 'move t))
7658
7659 (when (eq (char-before) ?{)
7660 ;; Encountered a block in the declaration. Jump over it.
7661 (condition-case nil
7662 (goto-char (c-up-list-forward (point)))
7663 (error (goto-char (point-max))
7664 (throw 'return nil)))
7665 (if (or (not c-opt-block-decls-with-vars-key)
7666 (save-excursion
7667 (c-with-syntax-table decl-syntax-table
7668 (let ((lim (point)))
7669 (goto-char start)
7670 (not (and
7671 ;; Check for `c-opt-block-decls-with-vars-key'
7672 ;; before the first paren.
7673 (c-syntactic-re-search-forward
7674 (concat "[;=\(\[{]\\|\\("
7675 c-opt-block-decls-with-vars-key
7676 "\\)")
7677 lim t t t)
7678 (match-beginning 1)
7679 (not (eq (char-before) ?_))
7680 ;; Check that the first following paren is
7681 ;; the block.
7682 (c-syntactic-re-search-forward "[;=\(\[{]"
7683 lim t t t)
7684 (eq (char-before) ?{)))))))
7685 ;; The declaration doesn't have any of the
7686 ;; `c-opt-block-decls-with-vars' keywords in the
7687 ;; beginning, so it ends here at the end of the block.
7688 (throw 'return t)))
7689
7690 (c-with-syntax-table decl-syntax-table
7691 (while (progn
7692 (if (eq (char-before) ?\;)
7693 (throw 'return t))
7694 (c-syntactic-re-search-forward ";" nil 'move t))))
7695 nil)))
7696
7697 (defun c-looking-at-decl-block (containing-sexp goto-start &optional limit)
7698 ;; Assuming the point is at an open brace, check if it starts a
7699 ;; block that contains another declaration level, i.e. that isn't a
7700 ;; statement block or a brace list, and if so return non-nil.
7701 ;;
7702 ;; If the check is successful, the return value is the start of the
7703 ;; keyword that tells what kind of construct it is, i.e. typically
7704 ;; what `c-decl-block-key' matched. Also, if GOTO-START is set then
7705 ;; the point will be at the start of the construct, before any
7706 ;; leading specifiers, otherwise it's at the returned position.
7707 ;;
7708 ;; The point is clobbered if the check is unsuccessful.
7709 ;;
7710 ;; CONTAINING-SEXP is the position of the open of the surrounding
7711 ;; paren, or nil if none.
7712 ;;
7713 ;; The optional LIMIT limits the backward search for the start of
7714 ;; the construct. It's assumed to be at a syntactically relevant
7715 ;; position.
7716 ;;
7717 ;; If any template arglists are found in the searched region before
7718 ;; the open brace, they get marked with paren syntax.
7719 ;;
7720 ;; This function might do hidden buffer changes.
7721
7722 (let ((open-brace (point)) kwd-start first-specifier-pos)
7723 (c-syntactic-skip-backward c-block-prefix-charset limit t)
7724
7725 (when (and c-recognize-<>-arglists
7726 (eq (char-before) ?>))
7727 ;; Could be at the end of a template arglist.
7728 (let ((c-parse-and-markup-<>-arglists t)
7729 (c-disallow-comma-in-<>-arglists
7730 (and containing-sexp
7731 (not (eq (char-after containing-sexp) ?{)))))
7732 (while (and
7733 (c-backward-<>-arglist nil limit)
7734 (progn
7735 (c-syntactic-skip-backward c-block-prefix-charset limit t)
7736 (eq (char-before) ?>))))))
7737
7738 ;; Note: Can't get bogus hits inside template arglists below since they
7739 ;; have gotten paren syntax above.
7740 (when (and
7741 ;; If `goto-start' is set we begin by searching for the
7742 ;; first possible position of a leading specifier list.
7743 ;; The `c-decl-block-key' search continues from there since
7744 ;; we know it can't match earlier.
7745 (if goto-start
7746 (when (c-syntactic-re-search-forward c-symbol-start
7747 open-brace t t)
7748 (goto-char (setq first-specifier-pos (match-beginning 0)))
7749 t)
7750 t)
7751
7752 (cond
7753 ((c-syntactic-re-search-forward c-decl-block-key open-brace t t t)
7754 (goto-char (setq kwd-start (match-beginning 0)))
7755 (or
7756
7757 ;; Found a keyword that can't be a type?
7758 (match-beginning 1)
7759
7760 ;; Can be a type too, in which case it's the return type of a
7761 ;; function (under the assumption that no declaration level
7762 ;; block construct starts with a type).
7763 (not (c-forward-type))
7764
7765 ;; Jumped over a type, but it could be a declaration keyword
7766 ;; followed by the declared identifier that we've jumped over
7767 ;; instead (e.g. in "class Foo {"). If it indeed is a type
7768 ;; then we should be at the declarator now, so check for a
7769 ;; valid declarator start.
7770 ;;
7771 ;; Note: This doesn't cope with the case when a declared
7772 ;; identifier is followed by e.g. '(' in a language where '('
7773 ;; also might be part of a declarator expression. Currently
7774 ;; there's no such language.
7775 (not (or (looking-at c-symbol-start)
7776 (looking-at c-type-decl-prefix-key)))))
7777
7778 ;; In Pike a list of modifiers may be followed by a brace
7779 ;; to make them apply to many identifiers. Note that the
7780 ;; match data will be empty on return in this case.
7781 ((and (c-major-mode-is 'pike-mode)
7782 (progn
7783 (goto-char open-brace)
7784 (= (c-backward-token-2) 0))
7785 (looking-at c-specifier-key)
7786 ;; Use this variant to avoid yet another special regexp.
7787 (c-keyword-member (c-keyword-sym (match-string 1))
7788 'c-modifier-kwds))
7789 (setq kwd-start (point))
7790 t)))
7791
7792 ;; Got a match.
7793
7794 (if goto-start
7795 ;; Back up over any preceding specifiers and their clauses
7796 ;; by going forward from `first-specifier-pos', which is the
7797 ;; earliest possible position where the specifier list can
7798 ;; start.
7799 (progn
7800 (goto-char first-specifier-pos)
7801
7802 (while (< (point) kwd-start)
7803 (if (looking-at c-symbol-key)
7804 ;; Accept any plain symbol token on the ground that
7805 ;; it's a specifier masked through a macro (just
7806 ;; like `c-forward-decl-or-cast-1' skip forward over
7807 ;; such tokens).
7808 ;;
7809 ;; Could be more restrictive wrt invalid keywords,
7810 ;; but that'd only occur in invalid code so there's
7811 ;; no use spending effort on it.
7812 (let ((end (match-end 0)))
7813 (unless (c-forward-keyword-clause 0)
7814 (goto-char end)
7815 (c-forward-syntactic-ws)))
7816
7817 ;; Can't parse a declaration preamble and is still
7818 ;; before `kwd-start'. That means `first-specifier-pos'
7819 ;; was in some earlier construct. Search again.
7820 (if (c-syntactic-re-search-forward c-symbol-start
7821 kwd-start 'move t)
7822 (goto-char (setq first-specifier-pos (match-beginning 0)))
7823 ;; Got no preamble before the block declaration keyword.
7824 (setq first-specifier-pos kwd-start))))
7825
7826 (goto-char first-specifier-pos))
7827 (goto-char kwd-start))
7828
7829 kwd-start)))
7830
7831 (defun c-search-uplist-for-classkey (paren-state)
7832 ;; Check if the closest containing paren sexp is a declaration
7833 ;; block, returning a 2 element vector in that case. Aref 0
7834 ;; contains the bufpos at boi of the class key line, and aref 1
7835 ;; contains the bufpos of the open brace. This function is an
7836 ;; obsolete wrapper for `c-looking-at-decl-block'.
7837 ;;
7838 ;; This function might do hidden buffer changes.
7839 (let ((open-paren-pos (c-most-enclosing-brace paren-state)))
7840 (when open-paren-pos
7841 (save-excursion
7842 (goto-char open-paren-pos)
7843 (when (and (eq (char-after) ?{)
7844 (c-looking-at-decl-block
7845 (c-safe-position open-paren-pos paren-state)
7846 nil))
7847 (back-to-indentation)
7848 (vector (point) open-paren-pos))))))
7849
7850 (defun c-inside-bracelist-p (containing-sexp paren-state)
7851 ;; return the buffer position of the beginning of the brace list
7852 ;; statement if we're inside a brace list, otherwise return nil.
7853 ;; CONTAINING-SEXP is the buffer pos of the innermost containing
7854 ;; paren. PAREN-STATE is the remainder of the state of enclosing
7855 ;; braces
7856 ;;
7857 ;; N.B.: This algorithm can potentially get confused by cpp macros
7858 ;; placed in inconvenient locations. It's a trade-off we make for
7859 ;; speed.
7860 ;;
7861 ;; This function might do hidden buffer changes.
7862 (or
7863 ;; This will pick up brace list declarations.
7864 (c-safe
7865 (save-excursion
7866 (goto-char containing-sexp)
7867 (c-forward-sexp -1)
7868 (let (bracepos)
7869 (if (and (or (looking-at c-brace-list-key)
7870 (progn (c-forward-sexp -1)
7871 (looking-at c-brace-list-key)))
7872 (setq bracepos (c-down-list-forward (point)))
7873 (not (c-crosses-statement-barrier-p (point)
7874 (- bracepos 2))))
7875 (point)))))
7876 ;; this will pick up array/aggregate init lists, even if they are nested.
7877 (save-excursion
7878 (let ((class-key
7879 ;; Pike can have class definitions anywhere, so we must
7880 ;; check for the class key here.
7881 (and (c-major-mode-is 'pike-mode)
7882 c-decl-block-key))
7883 bufpos braceassignp lim next-containing)
7884 (while (and (not bufpos)
7885 containing-sexp)
7886 (when paren-state
7887 (if (consp (car paren-state))
7888 (setq lim (cdr (car paren-state))
7889 paren-state (cdr paren-state))
7890 (setq lim (car paren-state)))
7891 (when paren-state
7892 (setq next-containing (car paren-state)
7893 paren-state (cdr paren-state))))
7894 (goto-char containing-sexp)
7895 (if (c-looking-at-inexpr-block next-containing next-containing)
7896 ;; We're in an in-expression block of some kind. Do not
7897 ;; check nesting. We deliberately set the limit to the
7898 ;; containing sexp, so that c-looking-at-inexpr-block
7899 ;; doesn't check for an identifier before it.
7900 (setq containing-sexp nil)
7901 ;; see if the open brace is preceded by = or [...] in
7902 ;; this statement, but watch out for operator=
7903 (setq braceassignp 'dontknow)
7904 (c-backward-token-2 1 t lim)
7905 ;; Checks to do only on the first sexp before the brace.
7906 (when (and c-opt-inexpr-brace-list-key
7907 (eq (char-after) ?\[))
7908 ;; In Java, an initialization brace list may follow
7909 ;; directly after "new Foo[]", so check for a "new"
7910 ;; earlier.
7911 (while (eq braceassignp 'dontknow)
7912 (setq braceassignp
7913 (cond ((/= (c-backward-token-2 1 t lim) 0) nil)
7914 ((looking-at c-opt-inexpr-brace-list-key) t)
7915 ((looking-at "\\sw\\|\\s_\\|[.[]")
7916 ;; Carry on looking if this is an
7917 ;; identifier (may contain "." in Java)
7918 ;; or another "[]" sexp.
7919 'dontknow)
7920 (t nil)))))
7921 ;; Checks to do on all sexps before the brace, up to the
7922 ;; beginning of the statement.
7923 (while (eq braceassignp 'dontknow)
7924 (cond ((eq (char-after) ?\;)
7925 (setq braceassignp nil))
7926 ((and class-key
7927 (looking-at class-key))
7928 (setq braceassignp nil))
7929 ((eq (char-after) ?=)
7930 ;; We've seen a =, but must check earlier tokens so
7931 ;; that it isn't something that should be ignored.
7932 (setq braceassignp 'maybe)
7933 (while (and (eq braceassignp 'maybe)
7934 (zerop (c-backward-token-2 1 t lim)))
7935 (setq braceassignp
7936 (cond
7937 ;; Check for operator =
7938 ((and c-opt-op-identifier-prefix
7939 (looking-at c-opt-op-identifier-prefix))
7940 nil)
7941 ;; Check for `<opchar>= in Pike.
7942 ((and (c-major-mode-is 'pike-mode)
7943 (or (eq (char-after) ?`)
7944 ;; Special case for Pikes
7945 ;; `[]=, since '[' is not in
7946 ;; the punctuation class.
7947 (and (eq (char-after) ?\[)
7948 (eq (char-before) ?`))))
7949 nil)
7950 ((looking-at "\\s.") 'maybe)
7951 ;; make sure we're not in a C++ template
7952 ;; argument assignment
7953 ((and
7954 (c-major-mode-is 'c++-mode)
7955 (save-excursion
7956 (let ((here (point))
7957 (pos< (progn
7958 (skip-chars-backward "^<>")
7959 (point))))
7960 (and (eq (char-before) ?<)
7961 (not (c-crosses-statement-barrier-p
7962 pos< here))
7963 (not (c-in-literal))
7964 ))))
7965 nil)
7966 (t t))))))
7967 (if (and (eq braceassignp 'dontknow)
7968 (/= (c-backward-token-2 1 t lim) 0))
7969 (setq braceassignp nil)))
7970 (if (not braceassignp)
7971 (if (eq (char-after) ?\;)
7972 ;; Brace lists can't contain a semicolon, so we're done.
7973 (setq containing-sexp nil)
7974 ;; Go up one level.
7975 (setq containing-sexp next-containing
7976 lim nil
7977 next-containing nil))
7978 ;; we've hit the beginning of the aggregate list
7979 (c-beginning-of-statement-1
7980 (c-most-enclosing-brace paren-state))
7981 (setq bufpos (point))))
7982 )
7983 bufpos))
7984 ))
7985
7986 (defun c-looking-at-special-brace-list (&optional lim)
7987 ;; If we're looking at the start of a pike-style list, ie `({ })',
7988 ;; `([ ])', `(< >)' etc, a cons of a cons of its starting and ending
7989 ;; positions and its entry in c-special-brace-lists is returned, nil
7990 ;; otherwise. The ending position is nil if the list is still open.
7991 ;; LIM is the limit for forward search. The point may either be at
7992 ;; the `(' or at the following paren character. Tries to check the
7993 ;; matching closer, but assumes it's correct if no balanced paren is
7994 ;; found (i.e. the case `({ ... } ... )' is detected as _not_ being
7995 ;; a special brace list).
7996 ;;
7997 ;; This function might do hidden buffer changes.
7998 (if c-special-brace-lists
7999 (condition-case ()
8000 (save-excursion
8001 (let ((beg (point))
8002 inner-beg end type)
8003 (c-forward-syntactic-ws)
8004 (if (eq (char-after) ?\()
8005 (progn
8006 (forward-char 1)
8007 (c-forward-syntactic-ws)
8008 (setq inner-beg (point))
8009 (setq type (assq (char-after) c-special-brace-lists)))
8010 (if (setq type (assq (char-after) c-special-brace-lists))
8011 (progn
8012 (setq inner-beg (point))
8013 (c-backward-syntactic-ws)
8014 (forward-char -1)
8015 (setq beg (if (eq (char-after) ?\()
8016 (point)
8017 nil)))))
8018 (if (and beg type)
8019 (if (and (c-safe
8020 (goto-char beg)
8021 (c-forward-sexp 1)
8022 (setq end (point))
8023 (= (char-before) ?\)))
8024 (c-safe
8025 (goto-char inner-beg)
8026 (if (looking-at "\\s(")
8027 ;; Check balancing of the inner paren
8028 ;; below.
8029 (progn
8030 (c-forward-sexp 1)
8031 t)
8032 ;; If the inner char isn't a paren then
8033 ;; we can't check balancing, so just
8034 ;; check the char before the outer
8035 ;; closing paren.
8036 (goto-char end)
8037 (backward-char)
8038 (c-backward-syntactic-ws)
8039 (= (char-before) (cdr type)))))
8040 (if (or (/= (char-syntax (char-before)) ?\))
8041 (= (progn
8042 (c-forward-syntactic-ws)
8043 (point))
8044 (1- end)))
8045 (cons (cons beg end) type))
8046 (cons (list beg) type)))))
8047 (error nil))))
8048
8049 (defun c-looking-at-bos (&optional lim)
8050 ;; Return non-nil if between two statements or declarations, assuming
8051 ;; point is not inside a literal or comment.
8052 ;;
8053 ;; Obsolete - `c-at-statement-start-p' or `c-at-expression-start-p'
8054 ;; are recommended instead.
8055 ;;
8056 ;; This function might do hidden buffer changes.
8057 (c-at-statement-start-p))
8058 (make-obsolete 'c-looking-at-bos 'c-at-statement-start-p "22.1")
8059
8060 (defun c-looking-at-inexpr-block (lim containing-sexp &optional check-at-end)
8061 ;; Return non-nil if we're looking at the beginning of a block
8062 ;; inside an expression. The value returned is actually a cons of
8063 ;; either 'inlambda, 'inexpr-statement or 'inexpr-class and the
8064 ;; position of the beginning of the construct.
8065 ;;
8066 ;; LIM limits the backward search. CONTAINING-SEXP is the start
8067 ;; position of the closest containing list. If it's nil, the
8068 ;; containing paren isn't used to decide whether we're inside an
8069 ;; expression or not. If both LIM and CONTAINING-SEXP are used, LIM
8070 ;; needs to be farther back.
8071 ;;
8072 ;; If CHECK-AT-END is non-nil then extra checks at the end of the
8073 ;; brace block might be done. It should only be used when the
8074 ;; construct can be assumed to be complete, i.e. when the original
8075 ;; starting position was further down than that.
8076 ;;
8077 ;; This function might do hidden buffer changes.
8078
8079 (save-excursion
8080 (let ((res 'maybe) passed-paren
8081 (closest-lim (or containing-sexp lim (point-min)))
8082 ;; Look at the character after point only as a last resort
8083 ;; when we can't disambiguate.
8084 (block-follows (and (eq (char-after) ?{) (point))))
8085
8086 (while (and (eq res 'maybe)
8087 (progn (c-backward-syntactic-ws)
8088 (> (point) closest-lim))
8089 (not (bobp))
8090 (progn (backward-char)
8091 (looking-at "[\]\).]\\|\\w\\|\\s_"))
8092 (c-safe (forward-char)
8093 (goto-char (scan-sexps (point) -1))))
8094
8095 (setq res
8096 (if (looking-at c-keywords-regexp)
8097 (let ((kw-sym (c-keyword-sym (match-string 1))))
8098 (cond
8099 ((and block-follows
8100 (c-keyword-member kw-sym 'c-inexpr-class-kwds))
8101 (and (not (eq passed-paren ?\[))
8102 (or (not (looking-at c-class-key))
8103 ;; If the class definition is at the start of
8104 ;; a statement, we don't consider it an
8105 ;; in-expression class.
8106 (let ((prev (point)))
8107 (while (and
8108 (= (c-backward-token-2 1 nil closest-lim) 0)
8109 (eq (char-syntax (char-after)) ?w))
8110 (setq prev (point)))
8111 (goto-char prev)
8112 (not (c-at-statement-start-p)))
8113 ;; Also, in Pike we treat it as an
8114 ;; in-expression class if it's used in an
8115 ;; object clone expression.
8116 (save-excursion
8117 (and check-at-end
8118 (c-major-mode-is 'pike-mode)
8119 (progn (goto-char block-follows)
8120 (zerop (c-forward-token-2 1 t)))
8121 (eq (char-after) ?\())))
8122 (cons 'inexpr-class (point))))
8123 ((c-keyword-member kw-sym 'c-inexpr-block-kwds)
8124 (when (not passed-paren)
8125 (cons 'inexpr-statement (point))))
8126 ((c-keyword-member kw-sym 'c-lambda-kwds)
8127 (when (or (not passed-paren)
8128 (eq passed-paren ?\())
8129 (cons 'inlambda (point))))
8130 ((c-keyword-member kw-sym 'c-block-stmt-kwds)
8131 nil)
8132 (t
8133 'maybe)))
8134
8135 (if (looking-at "\\s(")
8136 (if passed-paren
8137 (if (and (eq passed-paren ?\[)
8138 (eq (char-after) ?\[))
8139 ;; Accept several square bracket sexps for
8140 ;; Java array initializations.
8141 'maybe)
8142 (setq passed-paren (char-after))
8143 'maybe)
8144 'maybe))))
8145
8146 (if (eq res 'maybe)
8147 (when (and c-recognize-paren-inexpr-blocks
8148 block-follows
8149 containing-sexp
8150 (eq (char-after containing-sexp) ?\())
8151 (goto-char containing-sexp)
8152 (if (or (save-excursion
8153 (c-backward-syntactic-ws lim)
8154 (and (> (point) (or lim (point-min)))
8155 (c-on-identifier)))
8156 (and c-special-brace-lists
8157 (c-looking-at-special-brace-list)))
8158 nil
8159 (cons 'inexpr-statement (point))))
8160
8161 res))))
8162
8163 (defun c-looking-at-inexpr-block-backward (paren-state)
8164 ;; Returns non-nil if we're looking at the end of an in-expression
8165 ;; block, otherwise the same as `c-looking-at-inexpr-block'.
8166 ;; PAREN-STATE is the paren state relevant at the current position.
8167 ;;
8168 ;; This function might do hidden buffer changes.
8169 (save-excursion
8170 ;; We currently only recognize a block.
8171 (let ((here (point))
8172 (elem (car-safe paren-state))
8173 containing-sexp)
8174 (when (and (consp elem)
8175 (progn (goto-char (cdr elem))
8176 (c-forward-syntactic-ws here)
8177 (= (point) here)))
8178 (goto-char (car elem))
8179 (if (setq paren-state (cdr paren-state))
8180 (setq containing-sexp (car-safe paren-state)))
8181 (c-looking-at-inexpr-block (c-safe-position containing-sexp
8182 paren-state)
8183 containing-sexp)))))
8184
8185 \f
8186 ;; `c-guess-basic-syntax' and the functions that precedes it below
8187 ;; implements the main decision tree for determining the syntactic
8188 ;; analysis of the current line of code.
8189
8190 ;; Dynamically bound to t when `c-guess-basic-syntax' is called during
8191 ;; auto newline analysis.
8192 (defvar c-auto-newline-analysis nil)
8193
8194 (defun c-brace-anchor-point (bracepos)
8195 ;; BRACEPOS is the position of a brace in a construct like "namespace
8196 ;; Bar {". Return the anchor point in this construct; this is the
8197 ;; earliest symbol on the brace's line which isn't earlier than
8198 ;; "namespace".
8199 ;;
8200 ;; Currently (2007-08-17), "like namespace" means "matches
8201 ;; c-other-block-decl-kwds". It doesn't work with "class" or "struct"
8202 ;; or anything like that.
8203 (save-excursion
8204 (let ((boi (c-point 'boi bracepos)))
8205 (goto-char bracepos)
8206 (while (and (> (point) boi)
8207 (not (looking-at c-other-decl-block-key)))
8208 (c-backward-token-2))
8209 (if (> (point) boi) (point) boi))))
8210
8211 (defsubst c-add-syntax (symbol &rest args)
8212 ;; A simple function to prepend a new syntax element to
8213 ;; `c-syntactic-context'. Using `setq' on it is unsafe since it
8214 ;; should always be dynamically bound but since we read it first
8215 ;; we'll fail properly anyway if this function is misused.
8216 (setq c-syntactic-context (cons (cons symbol args)
8217 c-syntactic-context)))
8218
8219 (defsubst c-append-syntax (symbol &rest args)
8220 ;; Like `c-add-syntax' but appends to the end of the syntax list.
8221 ;; (Normally not necessary.)
8222 (setq c-syntactic-context (nconc c-syntactic-context
8223 (list (cons symbol args)))))
8224
8225 (defun c-add-stmt-syntax (syntax-symbol
8226 syntax-extra-args
8227 stop-at-boi-only
8228 containing-sexp
8229 paren-state)
8230 ;; Add the indicated SYNTAX-SYMBOL to `c-syntactic-context', extending it as
8231 ;; needed with further syntax elements of the types `substatement',
8232 ;; `inexpr-statement', `arglist-cont-nonempty', `statement-block-intro', and
8233 ;; `defun-block-intro'.
8234 ;;
8235 ;; Do the generic processing to anchor the given syntax symbol on
8236 ;; the preceding statement: Skip over any labels and containing
8237 ;; statements on the same line, and then search backward until we
8238 ;; find a statement or block start that begins at boi without a
8239 ;; label or comment.
8240 ;;
8241 ;; Point is assumed to be at the prospective anchor point for the
8242 ;; given SYNTAX-SYMBOL. More syntax entries are added if we need to
8243 ;; skip past open parens and containing statements. Most of the added
8244 ;; syntax elements will get the same anchor point - the exception is
8245 ;; for an anchor in a construct like "namespace"[*] - this is as early
8246 ;; as possible in the construct but on the same line as the {.
8247 ;;
8248 ;; [*] i.e. with a keyword matching c-other-block-decl-kwds.
8249 ;;
8250 ;; SYNTAX-EXTRA-ARGS are a list of the extra arguments for the
8251 ;; syntax symbol. They are appended after the anchor point.
8252 ;;
8253 ;; If STOP-AT-BOI-ONLY is nil, we can stop in the middle of the line
8254 ;; if the current statement starts there.
8255 ;;
8256 ;; Note: It's not a problem if PAREN-STATE "overshoots"
8257 ;; CONTAINING-SEXP, i.e. contains info about parens further down.
8258 ;;
8259 ;; This function might do hidden buffer changes.
8260
8261 (if (= (point) (c-point 'boi))
8262 ;; This is by far the most common case, so let's give it special
8263 ;; treatment.
8264 (apply 'c-add-syntax syntax-symbol (point) syntax-extra-args)
8265
8266 (let ((syntax-last c-syntactic-context)
8267 (boi (c-point 'boi))
8268 ;; Set when we're on a label, so that we don't stop there.
8269 ;; FIXME: To be complete we should check if we're on a label
8270 ;; now at the start.
8271 on-label)
8272
8273 ;; Use point as the anchor point for "namespace", "extern", etc.
8274 (apply 'c-add-syntax syntax-symbol
8275 (if (rassq syntax-symbol c-other-decl-block-key-in-symbols-alist)
8276 (point) nil)
8277 syntax-extra-args)
8278
8279 ;; Loop while we have to back out of containing blocks.
8280 (while
8281 (and
8282 (catch 'back-up-block
8283
8284 ;; Loop while we have to back up statements.
8285 (while (or (/= (point) boi)
8286 on-label
8287 (looking-at c-comment-start-regexp))
8288
8289 ;; Skip past any comments that stands between the
8290 ;; statement start and boi.
8291 (let ((savepos (point)))
8292 (while (and (/= savepos boi)
8293 (c-backward-single-comment))
8294 (setq savepos (point)
8295 boi (c-point 'boi)))
8296 (goto-char savepos))
8297
8298 ;; Skip to the beginning of this statement or backward
8299 ;; another one.
8300 (let ((old-pos (point))
8301 (old-boi boi)
8302 (step-type (c-beginning-of-statement-1 containing-sexp)))
8303 (setq boi (c-point 'boi)
8304 on-label (eq step-type 'label))
8305
8306 (cond ((= (point) old-pos)
8307 ;; If we didn't move we're at the start of a block and
8308 ;; have to continue outside it.
8309 (throw 'back-up-block t))
8310
8311 ((and (eq step-type 'up)
8312 (>= (point) old-boi)
8313 (looking-at "else\\>[^_]")
8314 (save-excursion
8315 (goto-char old-pos)
8316 (looking-at "if\\>[^_]")))
8317 ;; Special case to avoid deeper and deeper indentation
8318 ;; of "else if" clauses.
8319 )
8320
8321 ((and (not stop-at-boi-only)
8322 (/= old-pos old-boi)
8323 (memq step-type '(up previous)))
8324 ;; If stop-at-boi-only is nil, we shouldn't back up
8325 ;; over previous or containing statements to try to
8326 ;; reach boi, so go back to the last position and
8327 ;; exit.
8328 (goto-char old-pos)
8329 (throw 'back-up-block nil))
8330
8331 (t
8332 (if (and (not stop-at-boi-only)
8333 (memq step-type '(up previous beginning)))
8334 ;; If we've moved into another statement then we
8335 ;; should no longer try to stop in the middle of a
8336 ;; line.
8337 (setq stop-at-boi-only t))
8338
8339 ;; Record this as a substatement if we skipped up one
8340 ;; level.
8341 (when (eq step-type 'up)
8342 (c-add-syntax 'substatement nil))))
8343 )))
8344
8345 containing-sexp)
8346
8347 ;; Now we have to go out of this block.
8348 (goto-char containing-sexp)
8349
8350 ;; Don't stop in the middle of a special brace list opener
8351 ;; like "({".
8352 (when c-special-brace-lists
8353 (let ((special-list (c-looking-at-special-brace-list)))
8354 (when (and special-list
8355 (< (car (car special-list)) (point)))
8356 (setq containing-sexp (car (car special-list)))
8357 (goto-char containing-sexp))))
8358
8359 (setq paren-state (c-whack-state-after containing-sexp paren-state)
8360 containing-sexp (c-most-enclosing-brace paren-state)
8361 boi (c-point 'boi))
8362
8363 ;; Analyze the construct in front of the block we've stepped out
8364 ;; from and add the right syntactic element for it.
8365 (let ((paren-pos (point))
8366 (paren-char (char-after))
8367 step-type)
8368
8369 (if (eq paren-char ?\()
8370 ;; Stepped out of a parenthesis block, so we're in an
8371 ;; expression now.
8372 (progn
8373 (when (/= paren-pos boi)
8374 (if (and c-recognize-paren-inexpr-blocks
8375 (progn
8376 (c-backward-syntactic-ws containing-sexp)
8377 (or (not (looking-at "\\>"))
8378 (not (c-on-identifier))))
8379 (save-excursion
8380 (goto-char (1+ paren-pos))
8381 (c-forward-syntactic-ws)
8382 (eq (char-after) ?{)))
8383 ;; Stepped out of an in-expression statement. This
8384 ;; syntactic element won't get an anchor pos.
8385 (c-add-syntax 'inexpr-statement)
8386
8387 ;; A parenthesis normally belongs to an arglist.
8388 (c-add-syntax 'arglist-cont-nonempty nil paren-pos)))
8389
8390 (goto-char (max boi
8391 (if containing-sexp
8392 (1+ containing-sexp)
8393 (point-min))))
8394 (setq step-type 'same
8395 on-label nil))
8396
8397 ;; Stepped out of a brace block.
8398 (setq step-type (c-beginning-of-statement-1 containing-sexp)
8399 on-label (eq step-type 'label))
8400
8401 (if (and (eq step-type 'same)
8402 (/= paren-pos (point)))
8403 (let (inexpr)
8404 (cond
8405 ((save-excursion
8406 (goto-char paren-pos)
8407 (setq inexpr (c-looking-at-inexpr-block
8408 (c-safe-position containing-sexp paren-state)
8409 containing-sexp)))
8410 (c-add-syntax (if (eq (car inexpr) 'inlambda)
8411 'defun-block-intro
8412 'statement-block-intro)
8413 nil))
8414 ((looking-at c-other-decl-block-key)
8415 (c-add-syntax
8416 (cdr (assoc (match-string 1)
8417 c-other-decl-block-key-in-symbols-alist))
8418 (max (c-point 'boi paren-pos) (point))))
8419 (t (c-add-syntax 'defun-block-intro nil))))
8420
8421 (c-add-syntax 'statement-block-intro nil)))
8422
8423 (if (= paren-pos boi)
8424 ;; Always done if the open brace was at boi. The
8425 ;; c-beginning-of-statement-1 call above is necessary
8426 ;; anyway, to decide the type of block-intro to add.
8427 (goto-char paren-pos)
8428 (setq boi (c-point 'boi)))
8429 ))
8430
8431 ;; Fill in the current point as the anchor for all the symbols
8432 ;; added above.
8433 (let ((p c-syntactic-context) q)
8434 (while (not (eq p syntax-last))
8435 (setq q (cdr (car p))) ; e.g. (nil 28) [from (arglist-cont-nonempty nil 28)]
8436 (while q
8437 (unless (car q)
8438 (setcar q (point)))
8439 (setq q (cdr q)))
8440 (setq p (cdr p))))
8441 )))
8442
8443 (defun c-add-class-syntax (symbol
8444 containing-decl-open
8445 containing-decl-start
8446 containing-decl-kwd
8447 paren-state)
8448 ;; The inclass and class-close syntactic symbols are added in
8449 ;; several places and some work is needed to fix everything.
8450 ;; Therefore it's collected here.
8451 ;;
8452 ;; This function might do hidden buffer changes.
8453 (goto-char containing-decl-open)
8454 (if (and (eq symbol 'inclass) (= (point) (c-point 'boi)))
8455 (progn
8456 (c-add-syntax symbol containing-decl-open)
8457 containing-decl-open)
8458 (goto-char containing-decl-start)
8459 ;; Ought to use `c-add-stmt-syntax' instead of backing up to boi
8460 ;; here, but we have to do like this for compatibility.
8461 (back-to-indentation)
8462 (c-add-syntax symbol (point))
8463 (if (and (c-keyword-member containing-decl-kwd
8464 'c-inexpr-class-kwds)
8465 (/= containing-decl-start (c-point 'boi containing-decl-start)))
8466 (c-add-syntax 'inexpr-class))
8467 (point)))
8468
8469 (defun c-guess-continued-construct (indent-point
8470 char-after-ip
8471 beg-of-same-or-containing-stmt
8472 containing-sexp
8473 paren-state)
8474 ;; This function contains the decision tree reached through both
8475 ;; cases 18 and 10. It's a continued statement or top level
8476 ;; construct of some kind.
8477 ;;
8478 ;; This function might do hidden buffer changes.
8479
8480 (let (special-brace-list)
8481 (goto-char indent-point)
8482 (skip-chars-forward " \t")
8483
8484 (cond
8485 ;; (CASE A removed.)
8486 ;; CASE B: open braces for class or brace-lists
8487 ((setq special-brace-list
8488 (or (and c-special-brace-lists
8489 (c-looking-at-special-brace-list))
8490 (eq char-after-ip ?{)))
8491
8492 (cond
8493 ;; CASE B.1: class-open
8494 ((save-excursion
8495 (and (eq (char-after) ?{)
8496 (c-looking-at-decl-block containing-sexp t)
8497 (setq beg-of-same-or-containing-stmt (point))))
8498 (c-add-syntax 'class-open beg-of-same-or-containing-stmt))
8499
8500 ;; CASE B.2: brace-list-open
8501 ((or (consp special-brace-list)
8502 (save-excursion
8503 (goto-char beg-of-same-or-containing-stmt)
8504 (c-syntactic-re-search-forward "=\\([^=]\\|$\\)"
8505 indent-point t t t)))
8506 ;; The most semantically accurate symbol here is
8507 ;; brace-list-open, but we normally report it simply as a
8508 ;; statement-cont. The reason is that one normally adjusts
8509 ;; brace-list-open for brace lists as top-level constructs,
8510 ;; and brace lists inside statements is a completely different
8511 ;; context. C.f. case 5A.3.
8512 (c-beginning-of-statement-1 containing-sexp)
8513 (c-add-stmt-syntax (if c-auto-newline-analysis
8514 ;; Turn off the dwim above when we're
8515 ;; analyzing the nature of the brace
8516 ;; for the auto newline feature.
8517 'brace-list-open
8518 'statement-cont)
8519 nil nil
8520 containing-sexp paren-state))
8521
8522 ;; CASE B.3: The body of a function declared inside a normal
8523 ;; block. Can occur e.g. in Pike and when using gcc
8524 ;; extensions, but watch out for macros followed by blocks.
8525 ;; C.f. cases E, 16F and 17G.
8526 ((and (not (c-at-statement-start-p))
8527 (eq (c-beginning-of-statement-1 containing-sexp nil nil t)
8528 'same)
8529 (save-excursion
8530 (let ((c-recognize-typeless-decls nil))
8531 ;; Turn off recognition of constructs that lacks a
8532 ;; type in this case, since that's more likely to be
8533 ;; a macro followed by a block.
8534 (c-forward-decl-or-cast-1 (c-point 'bosws) nil nil))))
8535 (c-add-stmt-syntax 'defun-open nil t
8536 containing-sexp paren-state))
8537
8538 ;; CASE B.4: Continued statement with block open. The most
8539 ;; accurate analysis is perhaps `statement-cont' together with
8540 ;; `block-open' but we play DWIM and use `substatement-open'
8541 ;; instead. The rationaly is that this typically is a macro
8542 ;; followed by a block which makes it very similar to a
8543 ;; statement with a substatement block.
8544 (t
8545 (c-add-stmt-syntax 'substatement-open nil nil
8546 containing-sexp paren-state))
8547 ))
8548
8549 ;; CASE C: iostream insertion or extraction operator
8550 ((and (looking-at "\\(<<\\|>>\\)\\([^=]\\|$\\)")
8551 (save-excursion
8552 (goto-char beg-of-same-or-containing-stmt)
8553 ;; If there is no preceding streamop in the statement
8554 ;; then indent this line as a normal statement-cont.
8555 (when (c-syntactic-re-search-forward
8556 "\\(<<\\|>>\\)\\([^=]\\|$\\)" indent-point 'move t t)
8557 (c-add-syntax 'stream-op (c-point 'boi))
8558 t))))
8559
8560 ;; CASE E: In the "K&R region" of a function declared inside a
8561 ;; normal block. C.f. case B.3.
8562 ((and (save-excursion
8563 ;; Check that the next token is a '{'. This works as
8564 ;; long as no language that allows nested function
8565 ;; definitions allows stuff like member init lists, K&R
8566 ;; declarations or throws clauses there.
8567 ;;
8568 ;; Note that we do a forward search for something ahead
8569 ;; of the indentation line here. That's not good since
8570 ;; the user might not have typed it yet. Unfortunately
8571 ;; it's exceedingly tricky to recognize a function
8572 ;; prototype in a code block without resorting to this.
8573 (c-forward-syntactic-ws)
8574 (eq (char-after) ?{))
8575 (not (c-at-statement-start-p))
8576 (eq (c-beginning-of-statement-1 containing-sexp nil nil t)
8577 'same)
8578 (save-excursion
8579 (let ((c-recognize-typeless-decls nil))
8580 ;; Turn off recognition of constructs that lacks a
8581 ;; type in this case, since that's more likely to be
8582 ;; a macro followed by a block.
8583 (c-forward-decl-or-cast-1 (c-point 'bosws) nil nil))))
8584 (c-add-stmt-syntax 'func-decl-cont nil t
8585 containing-sexp paren-state))
8586
8587 ;; CASE D: continued statement.
8588 (t
8589 (c-beginning-of-statement-1 containing-sexp)
8590 (c-add-stmt-syntax 'statement-cont nil nil
8591 containing-sexp paren-state))
8592 )))
8593
8594 ;; The next autoload was added by RMS on 2005/8/9 - don't know why (ACM,
8595 ;; 2005/11/29).
8596 ;;;###autoload
8597 (defun c-guess-basic-syntax ()
8598 "Return the syntactic context of the current line."
8599 (save-excursion
8600 (beginning-of-line)
8601 (c-save-buffer-state
8602 ((indent-point (point))
8603 (case-fold-search nil)
8604 ;; A whole ugly bunch of various temporary variables. Have
8605 ;; to declare them here since it's not possible to declare
8606 ;; a variable with only the scope of a cond test and the
8607 ;; following result clauses, and most of this function is a
8608 ;; single gigantic cond. :P
8609 literal char-before-ip before-ws-ip char-after-ip macro-start
8610 in-macro-expr c-syntactic-context placeholder c-in-literal-cache
8611 step-type tmpsymbol keyword injava-inher special-brace-list tmp-pos
8612 containing-<
8613 ;; The following record some positions for the containing
8614 ;; declaration block if we're directly within one:
8615 ;; `containing-decl-open' is the position of the open
8616 ;; brace. `containing-decl-start' is the start of the
8617 ;; declaration. `containing-decl-kwd' is the keyword
8618 ;; symbol of the keyword that tells what kind of block it
8619 ;; is.
8620 containing-decl-open
8621 containing-decl-start
8622 containing-decl-kwd
8623 ;; The open paren of the closest surrounding sexp or nil if
8624 ;; there is none.
8625 containing-sexp
8626 ;; The position after the closest preceding brace sexp
8627 ;; (nested sexps are ignored), or the position after
8628 ;; `containing-sexp' if there is none, or (point-min) if
8629 ;; `containing-sexp' is nil.
8630 lim
8631 ;; The paren state outside `containing-sexp', or at
8632 ;; `indent-point' if `containing-sexp' is nil.
8633 (paren-state (c-parse-state))
8634 ;; There's always at most one syntactic element which got
8635 ;; an anchor pos. It's stored in syntactic-relpos.
8636 syntactic-relpos
8637 (c-stmt-delim-chars c-stmt-delim-chars))
8638
8639 ;; Check if we're directly inside an enclosing declaration
8640 ;; level block.
8641 (when (and (setq containing-sexp
8642 (c-most-enclosing-brace paren-state))
8643 (progn
8644 (goto-char containing-sexp)
8645 (eq (char-after) ?{))
8646 (setq placeholder
8647 (c-looking-at-decl-block
8648 (c-most-enclosing-brace paren-state
8649 containing-sexp)
8650 t)))
8651 (setq containing-decl-open containing-sexp
8652 containing-decl-start (point)
8653 containing-sexp nil)
8654 (goto-char placeholder)
8655 (setq containing-decl-kwd (and (looking-at c-keywords-regexp)
8656 (c-keyword-sym (match-string 1)))))
8657
8658 ;; Init some position variables.
8659 (if c-state-cache
8660 (progn
8661 (setq containing-sexp (car paren-state)
8662 paren-state (cdr paren-state))
8663 (if (consp containing-sexp)
8664 (progn
8665 (setq lim (cdr containing-sexp))
8666 (if (cdr c-state-cache)
8667 ;; Ignore balanced paren. The next entry
8668 ;; can't be another one.
8669 (setq containing-sexp (car (cdr c-state-cache))
8670 paren-state (cdr paren-state))
8671 ;; If there is no surrounding open paren then
8672 ;; put the last balanced pair back on paren-state.
8673 (setq paren-state (cons containing-sexp paren-state)
8674 containing-sexp nil)))
8675 (setq lim (1+ containing-sexp))))
8676 (setq lim (point-min)))
8677
8678 ;; If we're in a parenthesis list then ',' delimits the
8679 ;; "statements" rather than being an operator (with the
8680 ;; exception of the "for" clause). This difference is
8681 ;; typically only noticeable when statements are used in macro
8682 ;; arglists.
8683 (when (and containing-sexp
8684 (eq (char-after containing-sexp) ?\())
8685 (setq c-stmt-delim-chars c-stmt-delim-chars-with-comma))
8686
8687 ;; cache char before and after indent point, and move point to
8688 ;; the most likely position to perform the majority of tests
8689 (goto-char indent-point)
8690 (c-backward-syntactic-ws lim)
8691 (setq before-ws-ip (point)
8692 char-before-ip (char-before))
8693 (goto-char indent-point)
8694 (skip-chars-forward " \t")
8695 (setq char-after-ip (char-after))
8696
8697 ;; are we in a literal?
8698 (setq literal (c-in-literal lim))
8699
8700 ;; now figure out syntactic qualities of the current line
8701 (cond
8702
8703 ;; CASE 1: in a string.
8704 ((eq literal 'string)
8705 (c-add-syntax 'string (c-point 'bopl)))
8706
8707 ;; CASE 2: in a C or C++ style comment.
8708 ((and (memq literal '(c c++))
8709 ;; This is a kludge for XEmacs where we use
8710 ;; `buffer-syntactic-context', which doesn't correctly
8711 ;; recognize "\*/" to end a block comment.
8712 ;; `parse-partial-sexp' which is used by
8713 ;; `c-literal-limits' will however do that in most
8714 ;; versions, which results in that we get nil from
8715 ;; `c-literal-limits' even when `c-in-literal' claims
8716 ;; we're inside a comment.
8717 (setq placeholder (c-literal-limits lim)))
8718 (c-add-syntax literal (car placeholder)))
8719
8720 ;; CASE 3: in a cpp preprocessor macro continuation.
8721 ((and (save-excursion
8722 (when (c-beginning-of-macro)
8723 (setq macro-start (point))))
8724 (/= macro-start (c-point 'boi))
8725 (progn
8726 (setq tmpsymbol 'cpp-macro-cont)
8727 (or (not c-syntactic-indentation-in-macros)
8728 (save-excursion
8729 (goto-char macro-start)
8730 ;; If at the beginning of the body of a #define
8731 ;; directive then analyze as cpp-define-intro
8732 ;; only. Go on with the syntactic analysis
8733 ;; otherwise. in-macro-expr is set if we're in a
8734 ;; cpp expression, i.e. before the #define body
8735 ;; or anywhere in a non-#define directive.
8736 (if (c-forward-to-cpp-define-body)
8737 (let ((indent-boi (c-point 'boi indent-point)))
8738 (setq in-macro-expr (> (point) indent-boi)
8739 tmpsymbol 'cpp-define-intro)
8740 (= (point) indent-boi))
8741 (setq in-macro-expr t)
8742 nil)))))
8743 (c-add-syntax tmpsymbol macro-start)
8744 (setq macro-start nil))
8745
8746 ;; CASE 11: an else clause?
8747 ((looking-at "else\\>[^_]")
8748 (c-beginning-of-statement-1 containing-sexp)
8749 (c-add-stmt-syntax 'else-clause nil t
8750 containing-sexp paren-state))
8751
8752 ;; CASE 12: while closure of a do/while construct?
8753 ((and (looking-at "while\\>[^_]")
8754 (save-excursion
8755 (prog1 (eq (c-beginning-of-statement-1 containing-sexp)
8756 'beginning)
8757 (setq placeholder (point)))))
8758 (goto-char placeholder)
8759 (c-add-stmt-syntax 'do-while-closure nil t
8760 containing-sexp paren-state))
8761
8762 ;; CASE 13: A catch or finally clause? This case is simpler
8763 ;; than if-else and do-while, because a block is required
8764 ;; after every try, catch and finally.
8765 ((save-excursion
8766 (and (cond ((c-major-mode-is 'c++-mode)
8767 (looking-at "catch\\>[^_]"))
8768 ((c-major-mode-is 'java-mode)
8769 (looking-at "\\(catch\\|finally\\)\\>[^_]")))
8770 (and (c-safe (c-backward-syntactic-ws)
8771 (c-backward-sexp)
8772 t)
8773 (eq (char-after) ?{)
8774 (c-safe (c-backward-syntactic-ws)
8775 (c-backward-sexp)
8776 t)
8777 (if (eq (char-after) ?\()
8778 (c-safe (c-backward-sexp) t)
8779 t))
8780 (looking-at "\\(try\\|catch\\)\\>[^_]")
8781 (setq placeholder (point))))
8782 (goto-char placeholder)
8783 (c-add-stmt-syntax 'catch-clause nil t
8784 containing-sexp paren-state))
8785
8786 ;; CASE 18: A substatement we can recognize by keyword.
8787 ((save-excursion
8788 (and c-opt-block-stmt-key
8789 (not (eq char-before-ip ?\;))
8790 (not (c-at-vsemi-p before-ws-ip))
8791 (not (memq char-after-ip '(?\) ?\] ?,)))
8792 (or (not (eq char-before-ip ?}))
8793 (c-looking-at-inexpr-block-backward c-state-cache))
8794 (> (point)
8795 (progn
8796 ;; Ought to cache the result from the
8797 ;; c-beginning-of-statement-1 calls here.
8798 (setq placeholder (point))
8799 (while (eq (setq step-type
8800 (c-beginning-of-statement-1 lim))
8801 'label))
8802 (if (eq step-type 'previous)
8803 (goto-char placeholder)
8804 (setq placeholder (point))
8805 (if (and (eq step-type 'same)
8806 (not (looking-at c-opt-block-stmt-key)))
8807 ;; Step up to the containing statement if we
8808 ;; stayed in the same one.
8809 (let (step)
8810 (while (eq
8811 (setq step
8812 (c-beginning-of-statement-1 lim))
8813 'label))
8814 (if (eq step 'up)
8815 (setq placeholder (point))
8816 ;; There was no containing statement afterall.
8817 (goto-char placeholder)))))
8818 placeholder))
8819 (if (looking-at c-block-stmt-2-key)
8820 ;; Require a parenthesis after these keywords.
8821 ;; Necessary to catch e.g. synchronized in Java,
8822 ;; which can be used both as statement and
8823 ;; modifier.
8824 (and (zerop (c-forward-token-2 1 nil))
8825 (eq (char-after) ?\())
8826 (looking-at c-opt-block-stmt-key))))
8827
8828 (if (eq step-type 'up)
8829 ;; CASE 18A: Simple substatement.
8830 (progn
8831 (goto-char placeholder)
8832 (cond
8833 ((eq char-after-ip ?{)
8834 (c-add-stmt-syntax 'substatement-open nil nil
8835 containing-sexp paren-state))
8836 ((save-excursion
8837 (goto-char indent-point)
8838 (back-to-indentation)
8839 (c-forward-label))
8840 (c-add-stmt-syntax 'substatement-label nil nil
8841 containing-sexp paren-state))
8842 (t
8843 (c-add-stmt-syntax 'substatement nil nil
8844 containing-sexp paren-state))))
8845
8846 ;; CASE 18B: Some other substatement. This is shared
8847 ;; with case 10.
8848 (c-guess-continued-construct indent-point
8849 char-after-ip
8850 placeholder
8851 lim
8852 paren-state)))
8853
8854 ;; CASE 14: A case or default label
8855 ((looking-at c-label-kwds-regexp)
8856 (if containing-sexp
8857 (progn
8858 (goto-char containing-sexp)
8859 (setq lim (c-most-enclosing-brace c-state-cache
8860 containing-sexp))
8861 (c-backward-to-block-anchor lim)
8862 (c-add-stmt-syntax 'case-label nil t lim paren-state))
8863 ;; Got a bogus label at the top level. In lack of better
8864 ;; alternatives, anchor it on (point-min).
8865 (c-add-syntax 'case-label (point-min))))
8866
8867 ;; CASE 15: any other label
8868 ((save-excursion
8869 (back-to-indentation)
8870 (and (not (looking-at c-syntactic-ws-start))
8871 (c-forward-label)))
8872 (cond (containing-decl-open
8873 (setq placeholder (c-add-class-syntax 'inclass
8874 containing-decl-open
8875 containing-decl-start
8876 containing-decl-kwd
8877 paren-state))
8878 ;; Append access-label with the same anchor point as
8879 ;; inclass gets.
8880 (c-append-syntax 'access-label placeholder))
8881
8882 (containing-sexp
8883 (goto-char containing-sexp)
8884 (setq lim (c-most-enclosing-brace c-state-cache
8885 containing-sexp))
8886 (save-excursion
8887 (setq tmpsymbol
8888 (if (and (eq (c-beginning-of-statement-1 lim) 'up)
8889 (looking-at "switch\\>[^_]"))
8890 ;; If the surrounding statement is a switch then
8891 ;; let's analyze all labels as switch labels, so
8892 ;; that they get lined up consistently.
8893 'case-label
8894 'label)))
8895 (c-backward-to-block-anchor lim)
8896 (c-add-stmt-syntax tmpsymbol nil t lim paren-state))
8897
8898 (t
8899 ;; A label on the top level. Treat it as a class
8900 ;; context. (point-min) is the closest we get to the
8901 ;; class open brace.
8902 (c-add-syntax 'access-label (point-min)))))
8903
8904 ;; CASE 4: In-expression statement. C.f. cases 7B, 16A and
8905 ;; 17E.
8906 ((setq placeholder (c-looking-at-inexpr-block
8907 (c-safe-position containing-sexp paren-state)
8908 containing-sexp
8909 ;; Have to turn on the heuristics after
8910 ;; the point even though it doesn't work
8911 ;; very well. C.f. test case class-16.pike.
8912 t))
8913 (setq tmpsymbol (assq (car placeholder)
8914 '((inexpr-class . class-open)
8915 (inexpr-statement . block-open))))
8916 (if tmpsymbol
8917 ;; It's a statement block or an anonymous class.
8918 (setq tmpsymbol (cdr tmpsymbol))
8919 ;; It's a Pike lambda. Check whether we are between the
8920 ;; lambda keyword and the argument list or at the defun
8921 ;; opener.
8922 (setq tmpsymbol (if (eq char-after-ip ?{)
8923 'inline-open
8924 'lambda-intro-cont)))
8925 (goto-char (cdr placeholder))
8926 (back-to-indentation)
8927 (c-add-stmt-syntax tmpsymbol nil t
8928 (c-most-enclosing-brace c-state-cache (point))
8929 paren-state)
8930 (unless (eq (point) (cdr placeholder))
8931 (c-add-syntax (car placeholder))))
8932
8933 ;; CASE 5: Line is inside a declaration level block or at top level.
8934 ((or containing-decl-open (null containing-sexp))
8935 (cond
8936
8937 ;; CASE 5A: we are looking at a defun, brace list, class,
8938 ;; or inline-inclass method opening brace
8939 ((setq special-brace-list
8940 (or (and c-special-brace-lists
8941 (c-looking-at-special-brace-list))
8942 (eq char-after-ip ?{)))
8943 (cond
8944
8945 ;; CASE 5A.1: Non-class declaration block open.
8946 ((save-excursion
8947 (let (tmp)
8948 (and (eq char-after-ip ?{)
8949 (setq tmp (c-looking-at-decl-block containing-sexp t))
8950 (progn
8951 (setq placeholder (point))
8952 (goto-char tmp)
8953 (looking-at c-symbol-key))
8954 (c-keyword-member
8955 (c-keyword-sym (setq keyword (match-string 0)))
8956 'c-other-block-decl-kwds))))
8957 (goto-char placeholder)
8958 (c-add-stmt-syntax
8959 (if (string-equal keyword "extern")
8960 ;; Special case for extern-lang-open.
8961 'extern-lang-open
8962 (intern (concat keyword "-open")))
8963 nil t containing-sexp paren-state))
8964
8965 ;; CASE 5A.2: we are looking at a class opening brace
8966 ((save-excursion
8967 (goto-char indent-point)
8968 (skip-chars-forward " \t")
8969 (and (eq (char-after) ?{)
8970 (c-looking-at-decl-block containing-sexp t)
8971 (setq placeholder (point))))
8972 (c-add-syntax 'class-open placeholder))
8973
8974 ;; CASE 5A.3: brace list open
8975 ((save-excursion
8976 (c-beginning-of-decl-1 lim)
8977 (while (looking-at c-specifier-key)
8978 (goto-char (match-end 1))
8979 (c-forward-syntactic-ws indent-point))
8980 (setq placeholder (c-point 'boi))
8981 (or (consp special-brace-list)
8982 (and (or (save-excursion
8983 (goto-char indent-point)
8984 (setq tmpsymbol nil)
8985 (while (and (> (point) placeholder)
8986 (zerop (c-backward-token-2 1 t))
8987 (/= (char-after) ?=))
8988 (and c-opt-inexpr-brace-list-key
8989 (not tmpsymbol)
8990 (looking-at c-opt-inexpr-brace-list-key)
8991 (setq tmpsymbol 'topmost-intro-cont)))
8992 (eq (char-after) ?=))
8993 (looking-at c-brace-list-key))
8994 (save-excursion
8995 (while (and (< (point) indent-point)
8996 (zerop (c-forward-token-2 1 t))
8997 (not (memq (char-after) '(?\; ?\()))))
8998 (not (memq (char-after) '(?\; ?\()))
8999 ))))
9000 (if (and (not c-auto-newline-analysis)
9001 (c-major-mode-is 'java-mode)
9002 (eq tmpsymbol 'topmost-intro-cont))
9003 ;; We're in Java and have found that the open brace
9004 ;; belongs to a "new Foo[]" initialization list,
9005 ;; which means the brace list is part of an
9006 ;; expression and not a top level definition. We
9007 ;; therefore treat it as any topmost continuation
9008 ;; even though the semantically correct symbol still
9009 ;; is brace-list-open, on the same grounds as in
9010 ;; case B.2.
9011 (progn
9012 (c-beginning-of-statement-1 lim)
9013 (c-add-syntax 'topmost-intro-cont (c-point 'boi)))
9014 (c-add-syntax 'brace-list-open placeholder)))
9015
9016 ;; CASE 5A.4: inline defun open
9017 ((and containing-decl-open
9018 (not (c-keyword-member containing-decl-kwd
9019 'c-other-block-decl-kwds)))
9020 (c-add-syntax 'inline-open)
9021 (c-add-class-syntax 'inclass
9022 containing-decl-open
9023 containing-decl-start
9024 containing-decl-kwd
9025 paren-state))
9026
9027 ;; CASE 5A.5: ordinary defun open
9028 (t
9029 (save-excursion
9030 (c-beginning-of-decl-1 lim)
9031 (while (looking-at c-specifier-key)
9032 (goto-char (match-end 1))
9033 (c-forward-syntactic-ws indent-point))
9034 (c-add-syntax 'defun-open (c-point 'boi))
9035 ;; Bogus to use bol here, but it's the legacy. (Resolved,
9036 ;; 2007-11-09)
9037 ))))
9038
9039 ;; CASE 5B: After a function header but before the body (or
9040 ;; the ending semicolon if there's no body).
9041 ((save-excursion
9042 (when (setq placeholder (c-just-after-func-arglist-p lim))
9043 (setq tmp-pos (point))))
9044 (cond
9045
9046 ;; CASE 5B.1: Member init list.
9047 ((eq (char-after tmp-pos) ?:)
9048 (if (or (> tmp-pos indent-point)
9049 (= (c-point 'bosws) (1+ tmp-pos)))
9050 (progn
9051 ;; There is no preceding member init clause.
9052 ;; Indent relative to the beginning of indentation
9053 ;; for the topmost-intro line that contains the
9054 ;; prototype's open paren.
9055 (goto-char placeholder)
9056 (c-add-syntax 'member-init-intro (c-point 'boi)))
9057 ;; Indent relative to the first member init clause.
9058 (goto-char (1+ tmp-pos))
9059 (c-forward-syntactic-ws)
9060 (c-add-syntax 'member-init-cont (point))))
9061
9062 ;; CASE 5B.2: K&R arg decl intro
9063 ((and c-recognize-knr-p
9064 (c-in-knr-argdecl lim))
9065 (c-beginning-of-statement-1 lim)
9066 (c-add-syntax 'knr-argdecl-intro (c-point 'boi))
9067 (if containing-decl-open
9068 (c-add-class-syntax 'inclass
9069 containing-decl-open
9070 containing-decl-start
9071 containing-decl-kwd
9072 paren-state)))
9073
9074 ;; CASE 5B.4: Nether region after a C++ or Java func
9075 ;; decl, which could include a `throws' declaration.
9076 (t
9077 (c-beginning-of-statement-1 lim)
9078 (c-add-syntax 'func-decl-cont (c-point 'boi))
9079 )))
9080
9081 ;; CASE 5C: inheritance line. could be first inheritance
9082 ;; line, or continuation of a multiple inheritance
9083 ((or (and (c-major-mode-is 'c++-mode)
9084 (progn
9085 (when (eq char-after-ip ?,)
9086 (skip-chars-forward " \t")
9087 (forward-char))
9088 (looking-at c-opt-postfix-decl-spec-key)))
9089 (and (or (eq char-before-ip ?:)
9090 ;; watch out for scope operator
9091 (save-excursion
9092 (and (eq char-after-ip ?:)
9093 (c-safe (forward-char 1) t)
9094 (not (eq (char-after) ?:))
9095 )))
9096 (save-excursion
9097 (c-backward-syntactic-ws lim)
9098 (if (eq char-before-ip ?:)
9099 (progn
9100 (forward-char -1)
9101 (c-backward-syntactic-ws lim)))
9102 (back-to-indentation)
9103 (looking-at c-class-key)))
9104 ;; for Java
9105 (and (c-major-mode-is 'java-mode)
9106 (let ((fence (save-excursion
9107 (c-beginning-of-statement-1 lim)
9108 (point)))
9109 cont done)
9110 (save-excursion
9111 (while (not done)
9112 (cond ((looking-at c-opt-postfix-decl-spec-key)
9113 (setq injava-inher (cons cont (point))
9114 done t))
9115 ((or (not (c-safe (c-forward-sexp -1) t))
9116 (<= (point) fence))
9117 (setq done t))
9118 )
9119 (setq cont t)))
9120 injava-inher)
9121 (not (c-crosses-statement-barrier-p (cdr injava-inher)
9122 (point)))
9123 ))
9124 (cond
9125
9126 ;; CASE 5C.1: non-hanging colon on an inher intro
9127 ((eq char-after-ip ?:)
9128 (c-beginning-of-statement-1 lim)
9129 (c-add-syntax 'inher-intro (c-point 'boi))
9130 ;; don't add inclass symbol since relative point already
9131 ;; contains any class offset
9132 )
9133
9134 ;; CASE 5C.2: hanging colon on an inher intro
9135 ((eq char-before-ip ?:)
9136 (c-beginning-of-statement-1 lim)
9137 (c-add-syntax 'inher-intro (c-point 'boi))
9138 (if containing-decl-open
9139 (c-add-class-syntax 'inclass
9140 containing-decl-open
9141 containing-decl-start
9142 containing-decl-kwd
9143 paren-state)))
9144
9145 ;; CASE 5C.3: in a Java implements/extends
9146 (injava-inher
9147 (let ((where (cdr injava-inher))
9148 (cont (car injava-inher)))
9149 (goto-char where)
9150 (cond ((looking-at "throws\\>[^_]")
9151 (c-add-syntax 'func-decl-cont
9152 (progn (c-beginning-of-statement-1 lim)
9153 (c-point 'boi))))
9154 (cont (c-add-syntax 'inher-cont where))
9155 (t (c-add-syntax 'inher-intro
9156 (progn (goto-char (cdr injava-inher))
9157 (c-beginning-of-statement-1 lim)
9158 (point))))
9159 )))
9160
9161 ;; CASE 5C.4: a continued inheritance line
9162 (t
9163 (c-beginning-of-inheritance-list lim)
9164 (c-add-syntax 'inher-cont (point))
9165 ;; don't add inclass symbol since relative point already
9166 ;; contains any class offset
9167 )))
9168
9169 ;; CASE 5D: this could be a top-level initialization, a
9170 ;; member init list continuation, or a template argument
9171 ;; list continuation.
9172 ((save-excursion
9173 ;; Note: We use the fact that lim is always after any
9174 ;; preceding brace sexp.
9175 (if c-recognize-<>-arglists
9176 (while (and
9177 (progn
9178 (c-syntactic-skip-backward "^;,=<>" lim t)
9179 (> (point) lim))
9180 (or
9181 (when c-overloadable-operators-regexp
9182 (when (setq placeholder (c-after-special-operator-id lim))
9183 (goto-char placeholder)
9184 t))
9185 (cond
9186 ((eq (char-before) ?>)
9187 (or (c-backward-<>-arglist nil lim)
9188 (backward-char))
9189 t)
9190 ((eq (char-before) ?<)
9191 (backward-char)
9192 (if (save-excursion
9193 (c-forward-<>-arglist nil))
9194 (progn (forward-char)
9195 nil)
9196 t))
9197 (t nil)))))
9198 ;; NB: No c-after-special-operator-id stuff in this
9199 ;; clause - we assume only C++ needs it.
9200 (c-syntactic-skip-backward "^;,=" lim t))
9201 (memq (char-before) '(?, ?= ?<)))
9202 (cond
9203
9204 ;; CASE 5D.3: perhaps a template list continuation?
9205 ((and (c-major-mode-is 'c++-mode)
9206 (save-excursion
9207 (save-restriction
9208 (c-with-syntax-table c++-template-syntax-table
9209 (goto-char indent-point)
9210 (setq placeholder (c-up-list-backward))
9211 (and placeholder
9212 (eq (char-after placeholder) ?<))))))
9213 (c-with-syntax-table c++-template-syntax-table
9214 (goto-char placeholder)
9215 (c-beginning-of-statement-1 lim t)
9216 (if (save-excursion
9217 (c-backward-syntactic-ws lim)
9218 (eq (char-before) ?<))
9219 ;; In a nested template arglist.
9220 (progn
9221 (goto-char placeholder)
9222 (c-syntactic-skip-backward "^,;" lim t)
9223 (c-forward-syntactic-ws))
9224 (back-to-indentation)))
9225 ;; FIXME: Should use c-add-stmt-syntax, but it's not yet
9226 ;; template aware.
9227 (c-add-syntax 'template-args-cont (point) placeholder))
9228
9229 ;; CASE 5D.4: perhaps a multiple inheritance line?
9230 ((and (c-major-mode-is 'c++-mode)
9231 (save-excursion
9232 (c-beginning-of-statement-1 lim)
9233 (setq placeholder (point))
9234 (if (looking-at "static\\>[^_]")
9235 (c-forward-token-2 1 nil indent-point))
9236 (and (looking-at c-class-key)
9237 (zerop (c-forward-token-2 2 nil indent-point))
9238 (if (eq (char-after) ?<)
9239 (c-with-syntax-table c++-template-syntax-table
9240 (zerop (c-forward-token-2 1 t indent-point)))
9241 t)
9242 (eq (char-after) ?:))))
9243 (goto-char placeholder)
9244 (c-add-syntax 'inher-cont (c-point 'boi)))
9245
9246 ;; CASE 5D.5: Continuation of the "expression part" of a
9247 ;; top level construct. Or, perhaps, an unrecognised construct.
9248 (t
9249 (while (and (setq placeholder (point))
9250 (eq (car (c-beginning-of-decl-1 containing-sexp))
9251 'same)
9252 (save-excursion
9253 (c-backward-syntactic-ws)
9254 (eq (char-before) ?}))
9255 (< (point) placeholder)))
9256 (c-add-stmt-syntax
9257 (cond
9258 ((eq (point) placeholder) 'statement) ; unrecognised construct
9259 ;; A preceding comma at the top level means that a
9260 ;; new variable declaration starts here. Use
9261 ;; topmost-intro-cont for it, for consistency with
9262 ;; the first variable declaration. C.f. case 5N.
9263 ((eq char-before-ip ?,) 'topmost-intro-cont)
9264 (t 'statement-cont))
9265 nil nil containing-sexp paren-state))
9266 ))
9267
9268 ;; CASE 5F: Close of a non-class declaration level block.
9269 ((and (eq char-after-ip ?})
9270 (c-keyword-member containing-decl-kwd
9271 'c-other-block-decl-kwds))
9272 ;; This is inconsistent: Should use `containing-decl-open'
9273 ;; here if it's at boi, like in case 5J.
9274 (goto-char containing-decl-start)
9275 (c-add-stmt-syntax
9276 (if (string-equal (symbol-name containing-decl-kwd) "extern")
9277 ;; Special case for compatibility with the
9278 ;; extern-lang syntactic symbols.
9279 'extern-lang-close
9280 (intern (concat (symbol-name containing-decl-kwd)
9281 "-close")))
9282 nil t
9283 (c-most-enclosing-brace paren-state (point))
9284 paren-state))
9285
9286 ;; CASE 5G: we are looking at the brace which closes the
9287 ;; enclosing nested class decl
9288 ((and containing-sexp
9289 (eq char-after-ip ?})
9290 (eq containing-decl-open containing-sexp))
9291 (c-add-class-syntax 'class-close
9292 containing-decl-open
9293 containing-decl-start
9294 containing-decl-kwd
9295 paren-state))
9296
9297 ;; CASE 5H: we could be looking at subsequent knr-argdecls
9298 ((and c-recognize-knr-p
9299 (not containing-sexp) ; can't be knr inside braces.
9300 (not (eq char-before-ip ?}))
9301 (save-excursion
9302 (setq placeholder (cdr (c-beginning-of-decl-1 lim)))
9303 (and placeholder
9304 ;; Do an extra check to avoid tripping up on
9305 ;; statements that occur in invalid contexts
9306 ;; (e.g. in macro bodies where we don't really
9307 ;; know the context of what we're looking at).
9308 (not (and c-opt-block-stmt-key
9309 (looking-at c-opt-block-stmt-key)))))
9310 (< placeholder indent-point))
9311 (goto-char placeholder)
9312 (c-add-syntax 'knr-argdecl (point)))
9313
9314 ;; CASE 5I: ObjC method definition.
9315 ((and c-opt-method-key
9316 (looking-at c-opt-method-key))
9317 (c-beginning-of-statement-1 nil t)
9318 (if (= (point) indent-point)
9319 ;; Handle the case when it's the first (non-comment)
9320 ;; thing in the buffer. Can't look for a 'same return
9321 ;; value from cbos1 since ObjC directives currently
9322 ;; aren't recognized fully, so that we get 'same
9323 ;; instead of 'previous if it moved over a preceding
9324 ;; directive.
9325 (goto-char (point-min)))
9326 (c-add-syntax 'objc-method-intro (c-point 'boi)))
9327
9328 ;; CASE 5P: AWK pattern or function or continuation
9329 ;; thereof.
9330 ((c-major-mode-is 'awk-mode)
9331 (setq placeholder (point))
9332 (c-add-stmt-syntax
9333 (if (and (eq (c-beginning-of-statement-1) 'same)
9334 (/= (point) placeholder))
9335 'topmost-intro-cont
9336 'topmost-intro)
9337 nil nil
9338 containing-sexp paren-state))
9339
9340 ;; CASE 5N: At a variable declaration that follows a class
9341 ;; definition or some other block declaration that doesn't
9342 ;; end at the closing '}'. C.f. case 5D.5.
9343 ((progn
9344 (c-backward-syntactic-ws lim)
9345 (and (eq (char-before) ?})
9346 (save-excursion
9347 (let ((start (point)))
9348 (if (and c-state-cache
9349 (consp (car c-state-cache))
9350 (eq (cdar c-state-cache) (point)))
9351 ;; Speed up the backward search a bit.
9352 (goto-char (caar c-state-cache)))
9353 (c-beginning-of-decl-1 containing-sexp)
9354 (setq placeholder (point))
9355 (if (= start (point))
9356 ;; The '}' is unbalanced.
9357 nil
9358 (c-end-of-decl-1)
9359 (>= (point) indent-point))))))
9360 (goto-char placeholder)
9361 (c-add-stmt-syntax 'topmost-intro-cont nil nil
9362 containing-sexp paren-state))
9363
9364 ;; NOTE: The point is at the end of the previous token here.
9365
9366 ;; CASE 5J: we are at the topmost level, make
9367 ;; sure we skip back past any access specifiers
9368 ((and
9369 ;; A macro continuation line is never at top level.
9370 (not (and macro-start
9371 (> indent-point macro-start)))
9372 (save-excursion
9373 (setq placeholder (point))
9374 (or (memq char-before-ip '(?\; ?{ ?} nil))
9375 (c-at-vsemi-p before-ws-ip)
9376 (when (and (eq char-before-ip ?:)
9377 (eq (c-beginning-of-statement-1 lim)
9378 'label))
9379 (c-backward-syntactic-ws lim)
9380 (setq placeholder (point)))
9381 (and (c-major-mode-is 'objc-mode)
9382 (catch 'not-in-directive
9383 (c-beginning-of-statement-1 lim)
9384 (setq placeholder (point))
9385 (while (and (c-forward-objc-directive)
9386 (< (point) indent-point))
9387 (c-forward-syntactic-ws)
9388 (if (>= (point) indent-point)
9389 (throw 'not-in-directive t))
9390 (setq placeholder (point)))
9391 nil)))))
9392 ;; For historic reasons we anchor at bol of the last
9393 ;; line of the previous declaration. That's clearly
9394 ;; highly bogus and useless, and it makes our lives hard
9395 ;; to remain compatible. :P
9396 (goto-char placeholder)
9397 (c-add-syntax 'topmost-intro (c-point 'bol))
9398 (if containing-decl-open
9399 (if (c-keyword-member containing-decl-kwd
9400 'c-other-block-decl-kwds)
9401 (progn
9402 (goto-char (c-brace-anchor-point containing-decl-open))
9403 (c-add-stmt-syntax
9404 (if (string-equal (symbol-name containing-decl-kwd)
9405 "extern")
9406 ;; Special case for compatibility with the
9407 ;; extern-lang syntactic symbols.
9408 'inextern-lang
9409 (intern (concat "in"
9410 (symbol-name containing-decl-kwd))))
9411 nil t
9412 (c-most-enclosing-brace paren-state (point))
9413 paren-state))
9414 (c-add-class-syntax 'inclass
9415 containing-decl-open
9416 containing-decl-start
9417 containing-decl-kwd
9418 paren-state)))
9419 (when (and c-syntactic-indentation-in-macros
9420 macro-start
9421 (/= macro-start (c-point 'boi indent-point)))
9422 (c-add-syntax 'cpp-define-intro)
9423 (setq macro-start nil)))
9424
9425 ;; CASE 5K: we are at an ObjC method definition
9426 ;; continuation line.
9427 ((and c-opt-method-key
9428 (save-excursion
9429 (c-beginning-of-statement-1 lim)
9430 (beginning-of-line)
9431 (when (looking-at c-opt-method-key)
9432 (setq placeholder (point)))))
9433 (c-add-syntax 'objc-method-args-cont placeholder))
9434
9435 ;; CASE 5L: we are at the first argument of a template
9436 ;; arglist that begins on the previous line.
9437 ((and c-recognize-<>-arglists
9438 (eq (char-before) ?<)
9439 (setq placeholder (1- (point)))
9440 (not (and c-overloadable-operators-regexp
9441 (c-after-special-operator-id lim))))
9442 (c-beginning-of-statement-1 (c-safe-position (point) paren-state))
9443 (c-add-syntax 'template-args-cont (c-point 'boi) placeholder))
9444
9445 ;; CASE 5Q: we are at a statement within a macro.
9446 (macro-start
9447 (c-beginning-of-statement-1 containing-sexp)
9448 (c-add-stmt-syntax 'statement nil t containing-sexp paren-state))
9449
9450 ;; CASE 5M: we are at a topmost continuation line
9451 (t
9452 (c-beginning-of-statement-1 (c-safe-position (point) paren-state))
9453 (when (c-major-mode-is 'objc-mode)
9454 (setq placeholder (point))
9455 (while (and (c-forward-objc-directive)
9456 (< (point) indent-point))
9457 (c-forward-syntactic-ws)
9458 (setq placeholder (point)))
9459 (goto-char placeholder))
9460 (c-add-syntax 'topmost-intro-cont (c-point 'boi)))
9461 ))
9462
9463 ;; (CASE 6 has been removed.)
9464
9465 ;; CASE 19: line is an expression, not a statement, and is directly
9466 ;; contained by a template delimiter. Most likely, we are in a
9467 ;; template arglist within a statement. This case is based on CASE
9468 ;; 7. At some point in the future, we may wish to create more
9469 ;; syntactic symbols such as `template-intro',
9470 ;; `template-cont-nonempty', etc., and distinguish between them as we
9471 ;; do for `arglist-intro' etc. (2009-12-07).
9472 ((and c-recognize-<>-arglists
9473 (setq containing-< (c-up-list-backward indent-point containing-sexp))
9474 (eq (char-after containing-<) ?\<))
9475 (setq placeholder (c-point 'boi containing-<))
9476 (goto-char containing-sexp) ; Most nested Lbrace/Lparen (but not
9477 ; '<') before indent-point.
9478 (if (>= (point) placeholder)
9479 (progn
9480 (forward-char)
9481 (skip-chars-forward " \t"))
9482 (goto-char placeholder))
9483 (c-add-stmt-syntax 'template-args-cont (list containing-<) t
9484 (c-most-enclosing-brace c-state-cache (point))
9485 paren-state))
9486
9487
9488 ;; CASE 7: line is an expression, not a statement. Most
9489 ;; likely we are either in a function prototype or a function
9490 ;; call argument list, or a template argument list.
9491 ((not (or (and c-special-brace-lists
9492 (save-excursion
9493 (goto-char containing-sexp)
9494 (c-looking-at-special-brace-list)))
9495 (eq (char-after containing-sexp) ?{)
9496 (eq (char-after containing-sexp) ?<)))
9497 (cond
9498
9499 ;; CASE 7A: we are looking at the arglist closing paren.
9500 ;; C.f. case 7F.
9501 ((memq char-after-ip '(?\) ?\]))
9502 (goto-char containing-sexp)
9503 (setq placeholder (c-point 'boi))
9504 (if (and (c-safe (backward-up-list 1) t)
9505 (>= (point) placeholder))
9506 (progn
9507 (forward-char)
9508 (skip-chars-forward " \t"))
9509 (goto-char placeholder))
9510 (c-add-stmt-syntax 'arglist-close (list containing-sexp) t
9511 (c-most-enclosing-brace paren-state (point))
9512 paren-state))
9513
9514 ;; CASE 7B: Looking at the opening brace of an
9515 ;; in-expression block or brace list. C.f. cases 4, 16A
9516 ;; and 17E.
9517 ((and (eq char-after-ip ?{)
9518 (progn
9519 (setq placeholder (c-inside-bracelist-p (point)
9520 paren-state))
9521 (if placeholder
9522 (setq tmpsymbol '(brace-list-open . inexpr-class))
9523 (setq tmpsymbol '(block-open . inexpr-statement)
9524 placeholder
9525 (cdr-safe (c-looking-at-inexpr-block
9526 (c-safe-position containing-sexp
9527 paren-state)
9528 containing-sexp)))
9529 ;; placeholder is nil if it's a block directly in
9530 ;; a function arglist. That makes us skip out of
9531 ;; this case.
9532 )))
9533 (goto-char placeholder)
9534 (back-to-indentation)
9535 (c-add-stmt-syntax (car tmpsymbol) nil t
9536 (c-most-enclosing-brace paren-state (point))
9537 paren-state)
9538 (if (/= (point) placeholder)
9539 (c-add-syntax (cdr tmpsymbol))))
9540
9541 ;; CASE 7C: we are looking at the first argument in an empty
9542 ;; argument list. Use arglist-close if we're actually
9543 ;; looking at a close paren or bracket.
9544 ((memq char-before-ip '(?\( ?\[))
9545 (goto-char containing-sexp)
9546 (setq placeholder (c-point 'boi))
9547 (if (and (c-safe (backward-up-list 1) t)
9548 (>= (point) placeholder))
9549 (progn
9550 (forward-char)
9551 (skip-chars-forward " \t"))
9552 (goto-char placeholder))
9553 (c-add-stmt-syntax 'arglist-intro (list containing-sexp) t
9554 (c-most-enclosing-brace paren-state (point))
9555 paren-state))
9556
9557 ;; CASE 7D: we are inside a conditional test clause. treat
9558 ;; these things as statements
9559 ((progn
9560 (goto-char containing-sexp)
9561 (and (c-safe (c-forward-sexp -1) t)
9562 (looking-at "\\<for\\>[^_]")))
9563 (goto-char (1+ containing-sexp))
9564 (c-forward-syntactic-ws indent-point)
9565 (if (eq char-before-ip ?\;)
9566 (c-add-syntax 'statement (point))
9567 (c-add-syntax 'statement-cont (point))
9568 ))
9569
9570 ;; CASE 7E: maybe a continued ObjC method call. This is the
9571 ;; case when we are inside a [] bracketed exp, and what
9572 ;; precede the opening bracket is not an identifier.
9573 ((and c-opt-method-key
9574 (eq (char-after containing-sexp) ?\[)
9575 (progn
9576 (goto-char (1- containing-sexp))
9577 (c-backward-syntactic-ws (c-point 'bod))
9578 (if (not (looking-at c-symbol-key))
9579 (c-add-syntax 'objc-method-call-cont containing-sexp))
9580 )))
9581
9582 ;; CASE 7F: we are looking at an arglist continuation line,
9583 ;; but the preceding argument is on the same line as the
9584 ;; opening paren. This case includes multi-line
9585 ;; mathematical paren groupings, but we could be on a
9586 ;; for-list continuation line. C.f. case 7A.
9587 ((progn
9588 (goto-char (1+ containing-sexp))
9589 (< (save-excursion
9590 (c-forward-syntactic-ws)
9591 (point))
9592 (c-point 'bonl)))
9593 (goto-char containing-sexp) ; paren opening the arglist
9594 (setq placeholder (c-point 'boi))
9595 (if (and (c-safe (backward-up-list 1) t)
9596 (>= (point) placeholder))
9597 (progn
9598 (forward-char)
9599 (skip-chars-forward " \t"))
9600 (goto-char placeholder))
9601 (c-add-stmt-syntax 'arglist-cont-nonempty (list containing-sexp) t
9602 (c-most-enclosing-brace c-state-cache (point))
9603 paren-state))
9604
9605 ;; CASE 7G: we are looking at just a normal arglist
9606 ;; continuation line
9607 (t (c-forward-syntactic-ws indent-point)
9608 (c-add-syntax 'arglist-cont (c-point 'boi)))
9609 ))
9610
9611 ;; CASE 8: func-local multi-inheritance line
9612 ((and (c-major-mode-is 'c++-mode)
9613 (save-excursion
9614 (goto-char indent-point)
9615 (skip-chars-forward " \t")
9616 (looking-at c-opt-postfix-decl-spec-key)))
9617 (goto-char indent-point)
9618 (skip-chars-forward " \t")
9619 (cond
9620
9621 ;; CASE 8A: non-hanging colon on an inher intro
9622 ((eq char-after-ip ?:)
9623 (c-backward-syntactic-ws lim)
9624 (c-add-syntax 'inher-intro (c-point 'boi)))
9625
9626 ;; CASE 8B: hanging colon on an inher intro
9627 ((eq char-before-ip ?:)
9628 (c-add-syntax 'inher-intro (c-point 'boi)))
9629
9630 ;; CASE 8C: a continued inheritance line
9631 (t
9632 (c-beginning-of-inheritance-list lim)
9633 (c-add-syntax 'inher-cont (point))
9634 )))
9635
9636 ;; CASE 9: we are inside a brace-list
9637 ((and (not (c-major-mode-is 'awk-mode)) ; Maybe this isn't needed (ACM, 2002/3/29)
9638 (setq special-brace-list
9639 (or (and c-special-brace-lists ;;;; ALWAYS NIL FOR AWK!!
9640 (save-excursion
9641 (goto-char containing-sexp)
9642 (c-looking-at-special-brace-list)))
9643 (c-inside-bracelist-p containing-sexp paren-state))))
9644 (cond
9645
9646 ;; CASE 9A: In the middle of a special brace list opener.
9647 ((and (consp special-brace-list)
9648 (save-excursion
9649 (goto-char containing-sexp)
9650 (eq (char-after) ?\())
9651 (eq char-after-ip (car (cdr special-brace-list))))
9652 (goto-char (car (car special-brace-list)))
9653 (skip-chars-backward " \t")
9654 (if (and (bolp)
9655 (assoc 'statement-cont
9656 (setq placeholder (c-guess-basic-syntax))))
9657 (setq c-syntactic-context placeholder)
9658 (c-beginning-of-statement-1
9659 (c-safe-position (1- containing-sexp) paren-state))
9660 (c-forward-token-2 0)
9661 (while (looking-at c-specifier-key)
9662 (goto-char (match-end 1))
9663 (c-forward-syntactic-ws))
9664 (c-add-syntax 'brace-list-open (c-point 'boi))))
9665
9666 ;; CASE 9B: brace-list-close brace
9667 ((if (consp special-brace-list)
9668 ;; Check special brace list closer.
9669 (progn
9670 (goto-char (car (car special-brace-list)))
9671 (save-excursion
9672 (goto-char indent-point)
9673 (back-to-indentation)
9674 (or
9675 ;; We were between the special close char and the `)'.
9676 (and (eq (char-after) ?\))
9677 (eq (1+ (point)) (cdr (car special-brace-list))))
9678 ;; We were before the special close char.
9679 (and (eq (char-after) (cdr (cdr special-brace-list)))
9680 (zerop (c-forward-token-2))
9681 (eq (1+ (point)) (cdr (car special-brace-list)))))))
9682 ;; Normal brace list check.
9683 (and (eq char-after-ip ?})
9684 (c-safe (goto-char (c-up-list-backward (point))) t)
9685 (= (point) containing-sexp)))
9686 (if (eq (point) (c-point 'boi))
9687 (c-add-syntax 'brace-list-close (point))
9688 (setq lim (c-most-enclosing-brace c-state-cache (point)))
9689 (c-beginning-of-statement-1 lim)
9690 (c-add-stmt-syntax 'brace-list-close nil t lim paren-state)))
9691
9692 (t
9693 ;; Prepare for the rest of the cases below by going to the
9694 ;; token following the opening brace
9695 (if (consp special-brace-list)
9696 (progn
9697 (goto-char (car (car special-brace-list)))
9698 (c-forward-token-2 1 nil indent-point))
9699 (goto-char containing-sexp))
9700 (forward-char)
9701 (let ((start (point)))
9702 (c-forward-syntactic-ws indent-point)
9703 (goto-char (max start (c-point 'bol))))
9704 (c-skip-ws-forward indent-point)
9705 (cond
9706
9707 ;; CASE 9C: we're looking at the first line in a brace-list
9708 ((= (point) indent-point)
9709 (if (consp special-brace-list)
9710 (goto-char (car (car special-brace-list)))
9711 (goto-char containing-sexp))
9712 (if (eq (point) (c-point 'boi))
9713 (c-add-syntax 'brace-list-intro (point))
9714 (setq lim (c-most-enclosing-brace c-state-cache (point)))
9715 (c-beginning-of-statement-1 lim)
9716 (c-add-stmt-syntax 'brace-list-intro nil t lim paren-state)))
9717
9718 ;; CASE 9D: this is just a later brace-list-entry or
9719 ;; brace-entry-open
9720 (t (if (or (eq char-after-ip ?{)
9721 (and c-special-brace-lists
9722 (save-excursion
9723 (goto-char indent-point)
9724 (c-forward-syntactic-ws (c-point 'eol))
9725 (c-looking-at-special-brace-list (point)))))
9726 (c-add-syntax 'brace-entry-open (point))
9727 (c-add-syntax 'brace-list-entry (point))
9728 ))
9729 ))))
9730
9731 ;; CASE 10: A continued statement or top level construct.
9732 ((and (not (memq char-before-ip '(?\; ?:)))
9733 (not (c-at-vsemi-p before-ws-ip))
9734 (or (not (eq char-before-ip ?}))
9735 (c-looking-at-inexpr-block-backward c-state-cache))
9736 (> (point)
9737 (save-excursion
9738 (c-beginning-of-statement-1 containing-sexp)
9739 (setq placeholder (point))))
9740 (/= placeholder containing-sexp))
9741 ;; This is shared with case 18.
9742 (c-guess-continued-construct indent-point
9743 char-after-ip
9744 placeholder
9745 containing-sexp
9746 paren-state))
9747
9748 ;; CASE 16: block close brace, possibly closing the defun or
9749 ;; the class
9750 ((eq char-after-ip ?})
9751 ;; From here on we have the next containing sexp in lim.
9752 (setq lim (c-most-enclosing-brace paren-state))
9753 (goto-char containing-sexp)
9754 (cond
9755
9756 ;; CASE 16E: Closing a statement block? This catches
9757 ;; cases where it's preceded by a statement keyword,
9758 ;; which works even when used in an "invalid" context,
9759 ;; e.g. a macro argument.
9760 ((c-after-conditional)
9761 (c-backward-to-block-anchor lim)
9762 (c-add-stmt-syntax 'block-close nil t lim paren-state))
9763
9764 ;; CASE 16A: closing a lambda defun or an in-expression
9765 ;; block? C.f. cases 4, 7B and 17E.
9766 ((setq placeholder (c-looking-at-inexpr-block
9767 (c-safe-position containing-sexp paren-state)
9768 nil))
9769 (setq tmpsymbol (if (eq (car placeholder) 'inlambda)
9770 'inline-close
9771 'block-close))
9772 (goto-char containing-sexp)
9773 (back-to-indentation)
9774 (if (= containing-sexp (point))
9775 (c-add-syntax tmpsymbol (point))
9776 (goto-char (cdr placeholder))
9777 (back-to-indentation)
9778 (c-add-stmt-syntax tmpsymbol nil t
9779 (c-most-enclosing-brace paren-state (point))
9780 paren-state)
9781 (if (/= (point) (cdr placeholder))
9782 (c-add-syntax (car placeholder)))))
9783
9784 ;; CASE 16B: does this close an inline or a function in
9785 ;; a non-class declaration level block?
9786 ((save-excursion
9787 (and lim
9788 (progn
9789 (goto-char lim)
9790 (c-looking-at-decl-block
9791 (c-most-enclosing-brace paren-state lim)
9792 nil))
9793 (setq placeholder (point))))
9794 (c-backward-to-decl-anchor lim)
9795 (back-to-indentation)
9796 (if (save-excursion
9797 (goto-char placeholder)
9798 (looking-at c-other-decl-block-key))
9799 (c-add-syntax 'defun-close (point))
9800 (c-add-syntax 'inline-close (point))))
9801
9802 ;; CASE 16F: Can be a defun-close of a function declared
9803 ;; in a statement block, e.g. in Pike or when using gcc
9804 ;; extensions, but watch out for macros followed by
9805 ;; blocks. Let it through to be handled below.
9806 ;; C.f. cases B.3 and 17G.
9807 ((save-excursion
9808 (and (not (c-at-statement-start-p))
9809 (eq (c-beginning-of-statement-1 lim nil nil t) 'same)
9810 (setq placeholder (point))
9811 (let ((c-recognize-typeless-decls nil))
9812 ;; Turn off recognition of constructs that
9813 ;; lacks a type in this case, since that's more
9814 ;; likely to be a macro followed by a block.
9815 (c-forward-decl-or-cast-1 (c-point 'bosws) nil nil))))
9816 (back-to-indentation)
9817 (if (/= (point) containing-sexp)
9818 (goto-char placeholder))
9819 (c-add-stmt-syntax 'defun-close nil t lim paren-state))
9820
9821 ;; CASE 16C: If there is an enclosing brace then this is
9822 ;; a block close since defun closes inside declaration
9823 ;; level blocks have been handled above.
9824 (lim
9825 ;; If the block is preceded by a case/switch label on
9826 ;; the same line, we anchor at the first preceding label
9827 ;; at boi. The default handling in c-add-stmt-syntax
9828 ;; really fixes it better, but we do like this to keep
9829 ;; the indentation compatible with version 5.28 and
9830 ;; earlier. C.f. case 17H.
9831 (while (and (/= (setq placeholder (point)) (c-point 'boi))
9832 (eq (c-beginning-of-statement-1 lim) 'label)))
9833 (goto-char placeholder)
9834 (if (looking-at c-label-kwds-regexp)
9835 (c-add-syntax 'block-close (point))
9836 (goto-char containing-sexp)
9837 ;; c-backward-to-block-anchor not necessary here; those
9838 ;; situations are handled in case 16E above.
9839 (c-add-stmt-syntax 'block-close nil t lim paren-state)))
9840
9841 ;; CASE 16D: Only top level defun close left.
9842 (t
9843 (goto-char containing-sexp)
9844 (c-backward-to-decl-anchor lim)
9845 (c-add-stmt-syntax 'defun-close nil nil
9846 (c-most-enclosing-brace paren-state)
9847 paren-state))
9848 ))
9849
9850 ;; CASE 17: Statement or defun catchall.
9851 (t
9852 (goto-char indent-point)
9853 ;; Back up statements until we find one that starts at boi.
9854 (while (let* ((prev-point (point))
9855 (last-step-type (c-beginning-of-statement-1
9856 containing-sexp)))
9857 (if (= (point) prev-point)
9858 (progn
9859 (setq step-type (or step-type last-step-type))
9860 nil)
9861 (setq step-type last-step-type)
9862 (/= (point) (c-point 'boi)))))
9863 (cond
9864
9865 ;; CASE 17B: continued statement
9866 ((and (eq step-type 'same)
9867 (/= (point) indent-point))
9868 (c-add-stmt-syntax 'statement-cont nil nil
9869 containing-sexp paren-state))
9870
9871 ;; CASE 17A: After a case/default label?
9872 ((progn
9873 (while (and (eq step-type 'label)
9874 (not (looking-at c-label-kwds-regexp)))
9875 (setq step-type
9876 (c-beginning-of-statement-1 containing-sexp)))
9877 (eq step-type 'label))
9878 (c-add-stmt-syntax (if (eq char-after-ip ?{)
9879 'statement-case-open
9880 'statement-case-intro)
9881 nil t containing-sexp paren-state))
9882
9883 ;; CASE 17D: any old statement
9884 ((progn
9885 (while (eq step-type 'label)
9886 (setq step-type
9887 (c-beginning-of-statement-1 containing-sexp)))
9888 (eq step-type 'previous))
9889 (c-add-stmt-syntax 'statement nil t
9890 containing-sexp paren-state)
9891 (if (eq char-after-ip ?{)
9892 (c-add-syntax 'block-open)))
9893
9894 ;; CASE 17I: Inside a substatement block.
9895 ((progn
9896 ;; The following tests are all based on containing-sexp.
9897 (goto-char containing-sexp)
9898 ;; From here on we have the next containing sexp in lim.
9899 (setq lim (c-most-enclosing-brace paren-state containing-sexp))
9900 (c-after-conditional))
9901 (c-backward-to-block-anchor lim)
9902 (c-add-stmt-syntax 'statement-block-intro nil t
9903 lim paren-state)
9904 (if (eq char-after-ip ?{)
9905 (c-add-syntax 'block-open)))
9906
9907 ;; CASE 17E: first statement in an in-expression block.
9908 ;; C.f. cases 4, 7B and 16A.
9909 ((setq placeholder (c-looking-at-inexpr-block
9910 (c-safe-position containing-sexp paren-state)
9911 nil))
9912 (setq tmpsymbol (if (eq (car placeholder) 'inlambda)
9913 'defun-block-intro
9914 'statement-block-intro))
9915 (back-to-indentation)
9916 (if (= containing-sexp (point))
9917 (c-add-syntax tmpsymbol (point))
9918 (goto-char (cdr placeholder))
9919 (back-to-indentation)
9920 (c-add-stmt-syntax tmpsymbol nil t
9921 (c-most-enclosing-brace c-state-cache (point))
9922 paren-state)
9923 (if (/= (point) (cdr placeholder))
9924 (c-add-syntax (car placeholder))))
9925 (if (eq char-after-ip ?{)
9926 (c-add-syntax 'block-open)))
9927
9928 ;; CASE 17F: first statement in an inline, or first
9929 ;; statement in a top-level defun. we can tell this is it
9930 ;; if there are no enclosing braces that haven't been
9931 ;; narrowed out by a class (i.e. don't use bod here).
9932 ((save-excursion
9933 (or (not (setq placeholder (c-most-enclosing-brace
9934 paren-state)))
9935 (and (progn
9936 (goto-char placeholder)
9937 (eq (char-after) ?{))
9938 (c-looking-at-decl-block (c-most-enclosing-brace
9939 paren-state (point))
9940 nil))))
9941 (c-backward-to-decl-anchor lim)
9942 (back-to-indentation)
9943 (c-add-syntax 'defun-block-intro (point)))
9944
9945 ;; CASE 17G: First statement in a function declared inside
9946 ;; a normal block. This can occur in Pike and with
9947 ;; e.g. the gcc extensions, but watch out for macros
9948 ;; followed by blocks. C.f. cases B.3 and 16F.
9949 ((save-excursion
9950 (and (not (c-at-statement-start-p))
9951 (eq (c-beginning-of-statement-1 lim nil nil t) 'same)
9952 (setq placeholder (point))
9953 (let ((c-recognize-typeless-decls nil))
9954 ;; Turn off recognition of constructs that lacks
9955 ;; a type in this case, since that's more likely
9956 ;; to be a macro followed by a block.
9957 (c-forward-decl-or-cast-1 (c-point 'bosws) nil nil))))
9958 (back-to-indentation)
9959 (if (/= (point) containing-sexp)
9960 (goto-char placeholder))
9961 (c-add-stmt-syntax 'defun-block-intro nil t
9962 lim paren-state))
9963
9964 ;; CASE 17H: First statement in a block.
9965 (t
9966 ;; If the block is preceded by a case/switch label on the
9967 ;; same line, we anchor at the first preceding label at
9968 ;; boi. The default handling in c-add-stmt-syntax is
9969 ;; really fixes it better, but we do like this to keep the
9970 ;; indentation compatible with version 5.28 and earlier.
9971 ;; C.f. case 16C.
9972 (while (and (/= (setq placeholder (point)) (c-point 'boi))
9973 (eq (c-beginning-of-statement-1 lim) 'label)))
9974 (goto-char placeholder)
9975 (if (looking-at c-label-kwds-regexp)
9976 (c-add-syntax 'statement-block-intro (point))
9977 (goto-char containing-sexp)
9978 ;; c-backward-to-block-anchor not necessary here; those
9979 ;; situations are handled in case 17I above.
9980 (c-add-stmt-syntax 'statement-block-intro nil t
9981 lim paren-state))
9982 (if (eq char-after-ip ?{)
9983 (c-add-syntax 'block-open)))
9984 ))
9985 )
9986
9987 ;; now we need to look at any modifiers
9988 (goto-char indent-point)
9989 (skip-chars-forward " \t")
9990
9991 ;; are we looking at a comment only line?
9992 (when (and (looking-at c-comment-start-regexp)
9993 (/= (c-forward-token-2 0 nil (c-point 'eol)) 0))
9994 (c-append-syntax 'comment-intro))
9995
9996 ;; we might want to give additional offset to friends (in C++).
9997 (when (and c-opt-friend-key
9998 (looking-at c-opt-friend-key))
9999 (c-append-syntax 'friend))
10000
10001 ;; Set syntactic-relpos.
10002 (let ((p c-syntactic-context))
10003 (while (and p
10004 (if (integerp (c-langelem-pos (car p)))
10005 (progn
10006 (setq syntactic-relpos (c-langelem-pos (car p)))
10007 nil)
10008 t))
10009 (setq p (cdr p))))
10010
10011 ;; Start of or a continuation of a preprocessor directive?
10012 (if (and macro-start
10013 (eq macro-start (c-point 'boi))
10014 (not (and (c-major-mode-is 'pike-mode)
10015 (eq (char-after (1+ macro-start)) ?\"))))
10016 (c-append-syntax 'cpp-macro)
10017 (when (and c-syntactic-indentation-in-macros macro-start)
10018 (if in-macro-expr
10019 (when (or
10020 (< syntactic-relpos macro-start)
10021 (not (or
10022 (assq 'arglist-intro c-syntactic-context)
10023 (assq 'arglist-cont c-syntactic-context)
10024 (assq 'arglist-cont-nonempty c-syntactic-context)
10025 (assq 'arglist-close c-syntactic-context))))
10026 ;; If inside a cpp expression, i.e. anywhere in a
10027 ;; cpp directive except a #define body, we only let
10028 ;; through the syntactic analysis that is internal
10029 ;; in the expression. That means the arglist
10030 ;; elements, if they are anchored inside the cpp
10031 ;; expression.
10032 (setq c-syntactic-context nil)
10033 (c-add-syntax 'cpp-macro-cont macro-start))
10034 (when (and (eq macro-start syntactic-relpos)
10035 (not (assq 'cpp-define-intro c-syntactic-context))
10036 (save-excursion
10037 (goto-char macro-start)
10038 (or (not (c-forward-to-cpp-define-body))
10039 (<= (point) (c-point 'boi indent-point)))))
10040 ;; Inside a #define body and the syntactic analysis is
10041 ;; anchored on the start of the #define. In this case
10042 ;; we add cpp-define-intro to get the extra
10043 ;; indentation of the #define body.
10044 (c-add-syntax 'cpp-define-intro)))))
10045
10046 ;; return the syntax
10047 c-syntactic-context)))
10048
10049 \f
10050 ;; Indentation calculation.
10051
10052 (defun c-evaluate-offset (offset langelem symbol)
10053 ;; offset can be a number, a function, a variable, a list, or one of
10054 ;; the symbols + or -
10055 ;;
10056 ;; This function might do hidden buffer changes.
10057 (let ((res
10058 (cond
10059 ((numberp offset) offset)
10060 ((vectorp offset) offset)
10061 ((null offset) nil)
10062
10063 ((eq offset '+) c-basic-offset)
10064 ((eq offset '-) (- c-basic-offset))
10065 ((eq offset '++) (* 2 c-basic-offset))
10066 ((eq offset '--) (* 2 (- c-basic-offset)))
10067 ((eq offset '*) (/ c-basic-offset 2))
10068 ((eq offset '/) (/ (- c-basic-offset) 2))
10069
10070 ((functionp offset)
10071 (c-evaluate-offset
10072 (funcall offset
10073 (cons (c-langelem-sym langelem)
10074 (c-langelem-pos langelem)))
10075 langelem symbol))
10076
10077 ((listp offset)
10078 (cond
10079 ((eq (car offset) 'quote)
10080 (c-benign-error "The offset %S for %s was mistakenly quoted"
10081 offset symbol)
10082 nil)
10083
10084 ((memq (car offset) '(min max))
10085 (let (res val (method (car offset)))
10086 (setq offset (cdr offset))
10087 (while offset
10088 (setq val (c-evaluate-offset (car offset) langelem symbol))
10089 (cond
10090 ((not val))
10091 ((not res)
10092 (setq res val))
10093 ((integerp val)
10094 (if (vectorp res)
10095 (c-benign-error "\
10096 Error evaluating offset %S for %s: \
10097 Cannot combine absolute offset %S with relative %S in `%s' method"
10098 (car offset) symbol res val method)
10099 (setq res (funcall method res val))))
10100 (t
10101 (if (integerp res)
10102 (c-benign-error "\
10103 Error evaluating offset %S for %s: \
10104 Cannot combine relative offset %S with absolute %S in `%s' method"
10105 (car offset) symbol res val method)
10106 (setq res (vector (funcall method (aref res 0)
10107 (aref val 0)))))))
10108 (setq offset (cdr offset)))
10109 res))
10110
10111 ((eq (car offset) 'add)
10112 (let (res val)
10113 (setq offset (cdr offset))
10114 (while offset
10115 (setq val (c-evaluate-offset (car offset) langelem symbol))
10116 (cond
10117 ((not val))
10118 ((not res)
10119 (setq res val))
10120 ((integerp val)
10121 (if (vectorp res)
10122 (setq res (vector (+ (aref res 0) val)))
10123 (setq res (+ res val))))
10124 (t
10125 (if (vectorp res)
10126 (c-benign-error "\
10127 Error evaluating offset %S for %s: \
10128 Cannot combine absolute offsets %S and %S in `add' method"
10129 (car offset) symbol res val)
10130 (setq res val)))) ; Override.
10131 (setq offset (cdr offset)))
10132 res))
10133
10134 (t
10135 (let (res)
10136 (when (eq (car offset) 'first)
10137 (setq offset (cdr offset)))
10138 (while (and (not res) offset)
10139 (setq res (c-evaluate-offset (car offset) langelem symbol)
10140 offset (cdr offset)))
10141 res))))
10142
10143 ((and (symbolp offset) (boundp offset))
10144 (symbol-value offset))
10145
10146 (t
10147 (c-benign-error "Unknown offset format %S for %s" offset symbol)
10148 nil))))
10149
10150 (if (or (null res) (integerp res)
10151 (and (vectorp res) (= (length res) 1) (integerp (aref res 0))))
10152 res
10153 (c-benign-error "Error evaluating offset %S for %s: Got invalid value %S"
10154 offset symbol res)
10155 nil)))
10156
10157 (defun c-calc-offset (langelem)
10158 ;; Get offset from LANGELEM which is a list beginning with the
10159 ;; syntactic symbol and followed by any analysis data it provides.
10160 ;; That data may be zero or more elements, but if at least one is
10161 ;; given then the first is the anchor position (or nil). The symbol
10162 ;; is matched against `c-offsets-alist' and the offset calculated
10163 ;; from that is returned.
10164 ;;
10165 ;; This function might do hidden buffer changes.
10166 (let* ((symbol (c-langelem-sym langelem))
10167 (match (assq symbol c-offsets-alist))
10168 (offset (cdr-safe match)))
10169 (if match
10170 (setq offset (c-evaluate-offset offset langelem symbol))
10171 (if c-strict-syntax-p
10172 (c-benign-error "No offset found for syntactic symbol %s" symbol))
10173 (setq offset 0))
10174 (if (vectorp offset)
10175 offset
10176 (or (and (numberp offset) offset)
10177 (and (symbolp offset) (symbol-value offset))
10178 0))
10179 ))
10180
10181 (defun c-get-offset (langelem)
10182 ;; This is a compatibility wrapper for `c-calc-offset' in case
10183 ;; someone is calling it directly. It takes an old style syntactic
10184 ;; element on the form (SYMBOL . ANCHOR-POS) and converts it to the
10185 ;; new list form.
10186 ;;
10187 ;; This function might do hidden buffer changes.
10188 (if (c-langelem-pos langelem)
10189 (c-calc-offset (list (c-langelem-sym langelem)
10190 (c-langelem-pos langelem)))
10191 (c-calc-offset langelem)))
10192
10193 (defun c-get-syntactic-indentation (langelems)
10194 ;; Calculate the syntactic indentation from a syntactic description
10195 ;; as returned by `c-guess-syntax'.
10196 ;;
10197 ;; Note that topmost-intro always has an anchor position at bol, for
10198 ;; historical reasons. It's often used together with other symbols
10199 ;; that has more sane positions. Since we always use the first
10200 ;; found anchor position, we rely on that these other symbols always
10201 ;; precede topmost-intro in the LANGELEMS list.
10202 ;;
10203 ;; This function might do hidden buffer changes.
10204 (let ((indent 0) anchor)
10205
10206 (while langelems
10207 (let* ((c-syntactic-element (car langelems))
10208 (res (c-calc-offset c-syntactic-element)))
10209
10210 (if (vectorp res)
10211 ;; Got an absolute column that overrides any indentation
10212 ;; we've collected so far, but not the relative
10213 ;; indentation we might get for the nested structures
10214 ;; further down the langelems list.
10215 (setq indent (elt res 0)
10216 anchor (point-min)) ; A position at column 0.
10217
10218 ;; Got a relative change of the current calculated
10219 ;; indentation.
10220 (setq indent (+ indent res))
10221
10222 ;; Use the anchor position from the first syntactic
10223 ;; element with one.
10224 (unless anchor
10225 (setq anchor (c-langelem-pos (car langelems)))))
10226
10227 (setq langelems (cdr langelems))))
10228
10229 (if anchor
10230 (+ indent (save-excursion
10231 (goto-char anchor)
10232 (current-column)))
10233 indent)))
10234
10235 \f
10236 (cc-provide 'cc-engine)
10237
10238 ;; arch-tag: 149add18-4673-4da5-ac47-6805e4eae089
10239 ;;; cc-engine.el ends here