(c-electric-brace): Fixed a bug in the expand-abbrev' workaround
[bpt/emacs.git] / lisp / progmodes / cc-engine.el
Content-type: text/html HCoop Git - bpt/emacs.git/blame - lisp/progmodes/cc-engine.el


500 - Internal Server Error

Malformed UTF-8 character (fatal) at (eval 8) line 1, <$fd> line 10074.
CommitLineData
785eecbb
RS
1;;; cc-engine.el --- core syntax guessing engine for CC mode
2
d9e94c22 3;; Copyright (C) 1985,1987,1992-2003 Free Software Foundation, Inc.
785eecbb 4
d9e94c22
MS
5;; Authors: 1998- Martin Stjernholm
6;; 1992-1999 Barry A. Warsaw
785eecbb
RS
7;; 1987 Dave Detlefs and Stewart Clamen
8;; 1985 Richard M. Stallman
0ec8351b 9;; Maintainer: bug-cc-mode@gnu.org
785eecbb 10;; Created: 22-Apr-1997 (split from cc-mode.el)
6430c434 11;; Version: See cc-mode.el
785eecbb
RS
12;; Keywords: c languages oop
13
14;; This file is part of GNU Emacs.
15
16;; GNU Emacs is free software; you can redistribute it and/or modify
17;; it under the terms of the GNU General Public License as published by
18;; the Free Software Foundation; either version 2, or (at your option)
19;; any later version.
20
21;; GNU Emacs is distributed in the hope that it will be useful,
22;; but WITHOUT ANY WARRANTY; without even the implied warranty of
23;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24;; GNU General Public License for more details.
25
26;; You should have received a copy of the GNU General Public License
a66cd3ee 27;; along with GNU Emacs; see the file COPYING. If not, write to
130c507e 28;; the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
785eecbb
RS
29;; Boston, MA 02111-1307, USA.
30
3afbc435
PJ
31;;; Commentary:
32
a66cd3ee
MS
33;; The functions which have docstring documentation can be considered
34;; part of an API which other packages can use in CC Mode buffers.
35;; Otoh, undocumented functions and functions with the documentation
36;; in comments are considered purely internal and can change semantics
37;; or even disappear in the future.
38;;
39;; (This policy applies to CC Mode as a whole, not just this file. It
40;; probably also applies to many other Emacs packages, but here it's
41;; clearly spelled out.)
42
d9e94c22
MS
43;; Hidden buffer changes
44;;
45;; Various functions in CC Mode use text properties for caching and
46;; syntactic markup purposes, and those of them that might modify such
47;; properties are said to do "hidden buffer changes". They should be
48;; used within `c-save-buffer-state' or a similar function that saves
49;; and restores buffer modifiedness etc.
50;;
51;; Interactive functions are assumed to not do hidden buffer changes
52;; (this isn't applicable in the specific parts of them that do real
53;; changes, though).
54;;
55;; All other functions are assumed to do hidden buffer changes and
56;; must thus be wrapped inside `c-save-buffer-state' if they're used
57;; from any function that does not do hidden buffer changes.
58;;
59;; Every function, except the interactive ones, that doesn't do hidden
60;; buffer changes have that explicitly stated in their docstring or
61;; comment.
62
63;; Use of text properties
64;;
65;; CC Mode uses several text properties internally to mark up various
66;; positions, e.g. to improve speed and to eliminate glitches in
67;; interactive refontification.
68;;
7bfc3fdb
MS
69;; Note: This doc is for internal use only. Other packages should not
70;; assume that these text properties are used as described here.
71;;
d9e94c22
MS
72;; 'syntax-table
73;; Used to modify the syntax of some characters. Currently used to
74;; mark the "<" and ">" of angle bracket parens with paren syntax.
75;;
76;; This property is used on single characters and is therefore
77;; always treated as front and rear nonsticky (or start and end open
78;; in XEmacs vocabulary). It's therefore installed on
79;; `text-property-default-nonsticky' if that variable exists (Emacs
80;; >= 21).
81;;
82;; 'c-is-sws and 'c-in-sws
83;; Used by `c-forward-syntactic-ws' and `c-backward-syntactic-ws' to
84;; speed them up. See the comment blurb before `c-put-is-sws'
85;; below for further details.
86;;
87;; 'c-type
88;; This property is used on single characters to mark positions with
89;; special syntactic relevance of various sorts. It's primary use
90;; is to avoid glitches when multiline constructs are refontified
91;; interactively (on font lock decoration level 3). It's cleared in
92;; a region before it's fontified and is then put on relevant chars
93;; in that region as they are encountered during the fontification.
94;; The value specifies the kind of position:
95;;
96;; 'c-decl-arg-start
97;; Put on the last char of the token preceding each declaration
98;; inside a declaration style arglist (typically in a function
99;; prototype).
100;;
101;; 'c-decl-end
102;; Put on the last char of the token preceding a declaration.
103;; This is used in cases where declaration boundaries can't be
104;; recognized simply by looking for a token like ";" or "}".
105;; `c-type-decl-end-used' must be set if this is used (see also
106;; `c-find-decl-spots').
107;;
108;; 'c-<>-arg-sep
109;; Put on the commas that separate arguments in angle bracket
110;; arglists like C++ template arglists.
111;;
112;; 'c-decl-id-start and 'c-decl-type-start
113;; Put on the last char of the token preceding each declarator
114;; in the declarator list of a declaration. They are also used
115;; between the identifiers cases like enum declarations.
116;; 'c-decl-type-start is used when the declarators are types,
117;; 'c-decl-id-start otherwise.
118;;
119;; 'c-awk-NL-prop
120;; Used in AWK mode to mark the various kinds of newlines. See
121;; cc-awk.el.
122
3afbc435
PJ
123;;; Code:
124
0ec8351b 125(eval-when-compile
51f606de 126 (let ((load-path
130c507e
GM
127 (if (and (boundp 'byte-compile-dest-file)
128 (stringp byte-compile-dest-file))
129 (cons (file-name-directory byte-compile-dest-file) load-path)
51f606de 130 load-path)))
d9e94c22 131 (load "cc-bytecomp" nil t)))
130c507e
GM
132
133(cc-require 'cc-defs)
d9e94c22 134(cc-require-when-compile 'cc-langs)
130c507e 135(cc-require 'cc-vars)
d9e94c22
MS
136
137;; Some functions/constants in cc-awk.el that are called/referenced here.
138;; (Can't use cc-require due to cyclicity.)
139(cc-bytecomp-defun c-awk-unstick-NL-prop)
140(cc-bytecomp-defun c-awk-clear-NL-props)
141(cc-bytecomp-defvar awk-mode-syntax-table)
142(cc-bytecomp-defun c-awk-backward-syntactic-ws)
143(cc-bytecomp-defun c-awk-after-logical-semicolon)
144(cc-bytecomp-defun c-awk-NL-prop-not-set)
145(cc-bytecomp-defun c-awk-completed-stmt-ws-ends-line-p)
146(cc-bytecomp-defun c-awk-completed-stmt-ws-ends-prev-line-p)
147(cc-bytecomp-defun c-awk-prev-line-incomplete-p)
148(cc-bytecomp-defun c-awk-after-change)
130c507e
GM
149
150;; Silence the compiler.
151(cc-bytecomp-defun buffer-syntactic-context) ; XEmacs
0ec8351b 152
51f606de 153\f
d9e94c22
MS
154;; Make declarations for all the `c-lang-defvar' variables in cc-langs.
155
156(defmacro c-declare-lang-variables ()
157 `(progn
485fe977
RS
158 ,@(apply 'nconc
159 (mapcar (lambda (init)
160 `(,(if (elt init 2)
161 `(defvar ,(car init) nil ,(elt init 2))
162 `(defvar ,(car init) nil))
163 (make-variable-buffer-local ',(car init))))
164 (cdr c-lang-variable-inits)))))
d9e94c22
MS
165(c-declare-lang-variables)
166
167\f
168;;; Internal state variables.
169
170;; Internal state of hungry delete key feature
171(defvar c-hungry-delete-key nil)
172(make-variable-buffer-local 'c-hungry-delete-key)
173
174;; Internal state of auto newline feature.
175(defvar c-auto-newline nil)
176(make-variable-buffer-local 'c-auto-newline)
177
178;; Internal auto-newline/hungry-delete designation string for mode line.
179(defvar c-auto-hungry-string nil)
180(make-variable-buffer-local 'c-auto-hungry-string)
181
a66cd3ee
MS
182(defun c-calculate-state (arg prevstate)
183 ;; Calculate the new state of PREVSTATE, t or nil, based on arg. If
184 ;; arg is nil or zero, toggle the state. If arg is negative, turn
185 ;; the state off, and if arg is positive, turn the state on
186 (if (or (not arg)
187 (zerop (setq arg (prefix-numeric-value arg))))
188 (not prevstate)
189 (> arg 0)))
190
d9e94c22 191;; Dynamically bound cache for `c-in-literal'.
130c507e 192(defvar c-in-literal-cache t)
d9e94c22
MS
193
194;; Must be set in buffers where the `c-type' text property might be used
195;; with the value `c-decl-end'.
196(defvar c-type-decl-end-used nil)
197(make-variable-buffer-local 'c-type-decl-end-used)
198
199\f
200;;; Basic utility functions.
201
202(defun c-syntactic-content (from to)
203 ;; Return the given region as a string where all syntactic
204 ;; whitespace is removed or, where necessary, replaced with a single
205 ;; space.
206 (save-excursion
207 (goto-char from)
208 (let* ((parts (list nil)) (tail parts) pos)
209 (while (re-search-forward c-syntactic-ws-start to t)
210 (goto-char (setq pos (match-beginning 0)))
211 (c-forward-syntactic-ws to)
212 (if (= (point) pos)
213 (forward-char)
214 (if (and (> pos from)
215 (< (point) to)
216 (looking-at "\\w\\|\\s_")
217 (save-excursion
218 (goto-char (1- pos))
219 (looking-at "\\w\\|\\s_")))
220 (progn
221 (setcdr tail (list (buffer-substring-no-properties from pos)
222 " "))
223 (setq tail (cddr tail)))
224 (setcdr tail (list (buffer-substring-no-properties from pos)))
225 (setq tail (cdr tail)))
226 (setq from (point))))
227 (setcdr tail (list (buffer-substring-no-properties from to)))
228 (apply 'concat (cdr parts)))))
229
230(defsubst c-keyword-sym (keyword)
231 ;; Return non-nil if the string KEYWORD is a known keyword. More
232 ;; precisely, the value is the symbol for the keyword in
233 ;; `c-keywords-obarray'.
234 (intern-soft keyword c-keywords-obarray))
235
236(defsubst c-keyword-member (keyword-sym lang-constant)
237 ;; Return non-nil if the symbol KEYWORD-SYM, as returned by
238 ;; `c-keyword-sym', is a member of LANG-CONSTANT, which is the name
239 ;; of a language constant that ends with "-kwds". If KEYWORD-SYM is
240 ;; nil then the result is nil.
241 (get keyword-sym lang-constant))
242
243;; String syntax chars, suitable for skip-syntax-(forward|backward).
244(defconst c-string-syntax (if (memq 'gen-string-delim c-emacs-features)
245 "\"|"
246 "\""))
247
248;; Regexp matching string start syntax.
249(defconst c-string-limit-regexp (if (memq 'gen-string-delim c-emacs-features)
250 "\\s\"\\|\\s|"
251 "\\s\""))
252
253;; Holds formatted error strings for the few cases where parse errors
254;; are reported.
a66cd3ee 255(defvar c-parsing-error nil)
d9e94c22
MS
256(make-variable-buffer-local 'c-parsing-error)
257
258(defun c-echo-parsing-error (&optional quiet)
259 ;; This function does not do any hidden buffer changes.
260 (when (and c-report-syntactic-errors c-parsing-error (not quiet))
261 (c-benign-error "%s" c-parsing-error))
262 c-parsing-error)
263
264;; Faces given to comments and string literals. This is used in some
265;; situations to speed up recognition; it isn't mandatory that font
266;; locking is in use. This variable is extended with the face in
267;; `c-doc-face-name' when fontification is activated in cc-fonts.el.
1e330469 268(defvar c-literal-faces
d9e94c22
MS
269 '(font-lock-comment-face font-lock-string-face))
270
271\f
272;; Some debug tools to visualize various special positions. This
273;; debug code isn't as portable as the rest of CC Mode.
274
275(cc-bytecomp-defun overlays-in)
276(cc-bytecomp-defun overlay-get)
277(cc-bytecomp-defun overlay-start)
278(cc-bytecomp-defun overlay-end)
279(cc-bytecomp-defun delete-overlay)
280(cc-bytecomp-defun overlay-put)
281(cc-bytecomp-defun make-overlay)
282
283(defun c-debug-add-face (beg end face)
284 (c-save-buffer-state ((overlays (overlays-in beg end)) overlay)
285 (while overlays
286 (setq overlay (car overlays)
287 overlays (cdr overlays))
288 (when (eq (overlay-get overlay 'face) face)
289 (setq beg (min beg (overlay-start overlay))
290 end (max end (overlay-end overlay)))
291 (delete-overlay overlay)))
292 (overlay-put (make-overlay beg end) 'face face)))
293
294(defun c-debug-remove-face (beg end face)
295 (c-save-buffer-state ((overlays (overlays-in beg end)) overlay
296 (ol-beg beg) (ol-end end))
297 (while overlays
298 (setq overlay (car overlays)
299 overlays (cdr overlays))
300 (when (eq (overlay-get overlay 'face) face)
301 (setq ol-beg (min ol-beg (overlay-start overlay))
302 ol-end (max ol-end (overlay-end overlay)))
303 (delete-overlay overlay)))
304 (when (< ol-beg beg)
305 (overlay-put (make-overlay ol-beg beg) 'face face))
306 (when (> ol-end end)
307 (overlay-put (make-overlay end ol-end) 'face face))))
308
309\f
310;; `c-beginning-of-statement-1' and accompanying stuff.
130c507e 311
64001211
RS
312;; KLUDGE ALERT: c-maybe-labelp is used to pass information between
313;; c-crosses-statement-barrier-p and c-beginning-of-statement-1. A
314;; better way should be implemented, but this will at least shut up
315;; the byte compiler.
316(defvar c-maybe-labelp nil)
317
d9e94c22
MS
318;; New awk-compatible version of c-beginning-of-statement-1, ACM 2002/6/22
319
a66cd3ee
MS
320;; Macros used internally in c-beginning-of-statement-1 for the
321;; automaton actions.
322(defmacro c-bos-push-state ()
323 '(setq stack (cons (cons state saved-pos)
324 stack)))
325(defmacro c-bos-pop-state (&optional do-if-done)
326 `(if (setq state (car (car stack))
327 saved-pos (cdr (car stack))
328 stack (cdr stack))
329 t
330 ,do-if-done
331 (throw 'loop nil)))
332(defmacro c-bos-pop-state-and-retry ()
333 '(throw 'loop (setq state (car (car stack))
334 saved-pos (cdr (car stack))
335 ;; Throw nil if stack is empty, else throw non-nil.
336 stack (cdr stack))))
337(defmacro c-bos-save-pos ()
338 '(setq saved-pos (vector pos tok ptok pptok)))
339(defmacro c-bos-restore-pos ()
340 '(unless (eq (elt saved-pos 0) start)
341 (setq pos (elt saved-pos 0)
342 tok (elt saved-pos 1)
343 ptok (elt saved-pos 2)
344 pptok (elt saved-pos 3))
345 (goto-char pos)
346 (setq sym nil)))
347(defmacro c-bos-save-error-info (missing got)
348 `(setq saved-pos (vector pos ,missing ,got)))
349(defmacro c-bos-report-error ()
350 '(unless noerror
351 (setq c-parsing-error
352 (format "No matching `%s' found for `%s' on line %d"
353 (elt saved-pos 1)
354 (elt saved-pos 2)
355 (1+ (count-lines (point-min)
356 (c-point 'bol (elt saved-pos 0))))))))
357
358(defun c-beginning-of-statement-1 (&optional lim ignore-labels
359 noerror comma-delim)
360 "Move to the start of the current statement or declaration, or to
361the previous one if already at the beginning of one. Only
362statements/declarations on the same level are considered, i.e. don't
363move into or out of sexps (not even normal expression parentheses).
364
d9e94c22
MS
365Stop at statement continuation tokens like \"else\", \"catch\",
366\"finally\" and the \"while\" in \"do ... while\" if the start point
367is within the continuation. If starting at such a token, move to the
368corresponding statement start. If at the beginning of a statement,
369move to the closest containing statement if there is any. This might
370also stop at a continuation clause.
a66cd3ee
MS
371
372Labels are treated as separate statements if IGNORE-LABELS is non-nil.
373The function is not overly intelligent in telling labels from other
374uses of colons; if used outside a statement context it might trip up
375on e.g. inherit colons, so IGNORE-LABELS should be used then. There
376should be no such mistakes in a statement context, however.
377
378Macros are ignored unless point is within one, in which case the
379content of the macro is treated as normal code. Aside from any normal
380statement starts found in it, stop at the first token of the content
381in the macro, i.e. the expression of an \"#if\" or the start of the
382definition in a \"#define\". Also stop at start of macros before
383leaving them.
384
385Return 'label if stopped at a label, 'same if stopped at the beginning
386of the current statement, 'up if stepped to a containing statement,
387'previous if stepped to a preceding statement, 'beginning if stepped
388from a statement continuation clause to its start clause, or 'macro if
389stepped to a macro start. Note that 'same and not 'label is returned
390if stopped at the same label without crossing the colon character.
391
392LIM may be given to limit the search. If the search hits the limit,
393point will be left at the closest following token, or at the start
394position if that is less ('same is returned in this case).
395
396NOERROR turns off error logging to `c-parsing-error'.
397
398Normally only ';' is considered to delimit statements, but if
399COMMA-DELIM is non-nil then ',' is treated likewise."
400
d9e94c22
MS
401 ;; The bulk of this function is a pushdown automaton that looks at statement
402 ;; boundaries and the tokens (such as "while") in c-opt-block-stmt-key. Its
403 ;; purpose is to keep track of nested statements, ensuring that such
404 ;; statments are skipped over in their entirety (somewhat akin to what C-M-p
405 ;; does with nested braces/brackets/parentheses).
a66cd3ee
MS
406 ;;
407 ;; Note: The position of a boundary is the following token.
408 ;;
d9e94c22
MS
409 ;; Beginning with the current token (the one following point), move back one
410 ;; sexp at a time (where a sexp is, more or less, either a token or the
411 ;; entire contents of a brace/bracket/paren pair). Each time a statement
412 ;; boundary is crossed or a "while"-like token is found, update the state of
413 ;; the PDA. Stop at the beginning of a statement when the stack (holding
414 ;; nested statement info) is empty and the position has been moved.
415 ;;
416 ;; The following variables constitute the PDA:
417 ;;
418 ;; sym: This is either the "while"-like token (e.g. 'for) we've just
419 ;; scanned back over, 'boundary if we've just gone back over a
420 ;; statement boundary, or nil otherwise.
421 ;; state: takes one of the values (nil else else-boundary while
422 ;; while-boundary catch catch-boundary).
423 ;; nil means "no "while"-like token yet scanned".
424 ;; 'else, for example, means "just gone back over an else".
425 ;; 'else-boundary means "just gone back over a statement boundary
426 ;; immediately after having gone back over an else".
427 ;; saved-pos: A vector of either saved positions (tok ptok pptok, etc.) or
428 ;; of error reporting information.
429 ;; stack: The stack onto which the PDA pushes its state. Each entry
430 ;; consists of a saved value of state and saved-pos. An entry is
431 ;; pushed when we move back over a "continuation" token (e.g. else)
432 ;; and popped when we encounter the corresponding opening token
433 ;; (e.g. if).
434 ;;
435 ;;
436 ;; The following diagram briefly outlines the PDA.
a66cd3ee
MS
437 ;;
438 ;; Common state:
d9e94c22
MS
439 ;; "else": Push state, goto state `else'.
440 ;; "while": Push state, goto state `while'.
441 ;; "catch" or "finally": Push state, goto state `catch'.
442 ;; boundary: Pop state.
a66cd3ee
MS
443 ;; other: Do nothing special.
444 ;;
d9e94c22
MS
445 ;; State `else':
446 ;; boundary: Goto state `else-boundary'.
447 ;; other: Error, pop state, retry token.
448 ;;
449 ;; State `else-boundary':
450 ;; "if": Pop state.
451 ;; boundary: Error, pop state.
452 ;; other: See common state.
453 ;;
454 ;; State `while':
455 ;; boundary: Save position, goto state `while-boundary'.
456 ;; other: Pop state, retry token.
457 ;;
458 ;; State `while-boundary':
459 ;; "do": Pop state.
460 ;; boundary: Restore position if it's not at start, pop state. [*see below]
461 ;; other: See common state.
462 ;;
463 ;; State `catch':
464 ;; boundary: Goto state `catch-boundary'.
465 ;; other: Error, pop state, retry token.
466 ;;
467 ;; State `catch-boundary':
468 ;; "try": Pop state.
469 ;; "catch": Goto state `catch'.
470 ;; boundary: Error, pop state.
471 ;; other: See common state.
472 ;;
473 ;; [*] In the `while-boundary' state, we had pushed a 'while state, and were
474 ;; searching for a "do" which would have opened a do-while. If we didn't
475 ;; find it, we discard the analysis done since the "while", go back to this
476 ;; token in the buffer and restart the scanning there, this time WITHOUT
477 ;; pushing the 'while state onto the stack.
478 ;;
a66cd3ee
MS
479 ;; In addition to the above there is some special handling of labels
480 ;; and macros.
481
482 (let ((case-fold-search nil)
483 (start (point))
484 macro-start
485 (delims (if comma-delim '(?\; ?,) '(?\;)))
486 (c-stmt-delim-chars (if comma-delim
487 c-stmt-delim-chars-with-comma
488 c-stmt-delim-chars))
489 pos ; Current position.
d9e94c22 490 boundary-pos ; Position of last stmt boundary character (e.g. ;).
a66cd3ee
MS
491 after-labels-pos ; Value of tok after first found colon.
492 last-label-pos ; Value of tok after last found colon.
d9e94c22
MS
493 sym ; Symbol just scanned back over (e.g. 'while or
494 ; 'boundary). See above
495 state ; Current state in the automaton. See above.
496 saved-pos ; Current saved positions. See above
a66cd3ee 497 stack ; Stack of conses (state . saved-pos).
d9e94c22 498 (cond-key (or c-opt-block-stmt-key ; regexp which matches "for", "if", etc.
a66cd3ee 499 "\\<\\>")) ; Matches nothing.
d9e94c22 500 (ret 'same) ; Return value.
a66cd3ee
MS
501 tok ptok pptok ; Pos of last three sexps or bounds.
502 c-in-literal-cache c-maybe-labelp saved)
503
504 (save-restriction
505 (if lim (narrow-to-region lim (point-max)))
506
507 (if (save-excursion
508 (and (c-beginning-of-macro)
509 (/= (point) start)))
510 (setq macro-start (point)))
511
d9e94c22 512 ;; Try to skip back over unary operator characters, to register
a66cd3ee
MS
513 ;; that we've moved.
514 (while (progn
515 (setq pos (point))
7bfc3fdb
MS
516 (if (c-mode-is-new-awk-p)
517 (c-awk-backward-syntactic-ws)
518 (c-backward-syntactic-ws))
d9e94c22
MS
519 (/= (skip-chars-backward "-+!*&~@`#") 0))) ; ACM, 2002/5/31;
520 ; Make a variable in
521 ; cc-langs.el, maybe
522
523 ;; Skip back over any semicolon here. If it was a bare semicolon, we're
524 ;; done. Later on we ignore the boundaries for statements that doesn't
525 ;; contain any sexp. The only thing that is affected is that the error
526 ;; checking is a little less strict, and we really don't bother.
a66cd3ee
MS
527 (if (and (memq (char-before) delims)
528 (progn (forward-char -1)
529 (setq saved (point))
d9e94c22
MS
530 (if (c-mode-is-new-awk-p)
531 (c-awk-backward-syntactic-ws)
532 (c-backward-syntactic-ws))
a66cd3ee
MS
533 (or (memq (char-before) delims)
534 (memq (char-before) '(?: nil))
d9e94c22
MS
535 (eq (char-syntax (char-before)) ?\()
536 (and (c-mode-is-new-awk-p)
537 (c-awk-after-logical-semicolon))))) ; ACM 2002/6/22
538 ;; ACM, 2002/7/20: What about giving a limit to the above function?
539 ;; ACM, 2003/6/16: The above two lines (checking for
540 ;; awk-logical-semicolon) are probably redundant after rewriting
541 ;; c-awk-backward-syntactic-ws.
a66cd3ee
MS
542 (setq ret 'previous
543 pos saved)
544
545 ;; Begin at start and not pos to detect macros if we stand
546 ;; directly after the #.
547 (goto-char start)
548 (if (looking-at "\\<\\|\\W")
549 ;; Record this as the first token if not starting inside it.
550 (setq tok start))
551
d9e94c22
MS
552 ;; The following while loop goes back one sexp (balanced parens,
553 ;; etc. with contents, or symbol or suchlike) each iteration. This
554 ;; movement is accomplished with a call to scan-sexps approx 130 lines
555 ;; below.
a66cd3ee
MS
556 (while
557 (catch 'loop ;; Throw nil to break, non-nil to continue.
558 (cond
d9e94c22
MS
559 ;; Check for macro start. Take this out for AWK Mode (ACM, 2002/5/31)
560 ;; NO!! just make sure macro-start is nil in AWK Mode (ACM, 2002/6/22)
561 ;; It always is (ACM, 2002/6/23)
a66cd3ee
MS
562 ((save-excursion
563 (and macro-start
a66cd3ee
MS
564 (progn (skip-chars-backward " \t")
565 (eq (char-before) ?#))
566 (progn (setq saved (1- (point)))
567 (beginning-of-line)
568 (not (eq (char-before (1- (point))) ?\\)))
d9e94c22 569 (looking-at c-opt-cpp-start)
a66cd3ee
MS
570 (progn (skip-chars-forward " \t")
571 (eq (point) saved))))
572 (goto-char saved)
573 (if (and (c-forward-to-cpp-define-body)
574 (progn (c-forward-syntactic-ws start)
575 (< (point) start)))
576 ;; Stop at the first token in the content of the macro.
577 (setq pos (point)
578 ignore-labels t) ; Avoid the label check on exit.
579 (setq pos saved
580 ret 'macro
581 ignore-labels t))
582 (throw 'loop nil))
583
d9e94c22
MS
584 ;; Do a round through the automaton if we've just passed a
585 ;; statement boundary or passed a "while"-like token.
a66cd3ee
MS
586 ((or sym
587 (and (looking-at cond-key)
588 (setq sym (intern (match-string 1)))))
589
590 (when (and (< pos start) (null stack))
591 (throw 'loop nil))
592
d9e94c22
MS
593 ;; The PDA state handling.
594 ;;
595 ;; Refer to the description of the PDA in the openining
596 ;; comments. In the following OR form, the first leaf
597 ;; attempts to handles one of the specific actions detailed
598 ;; (e.g., finding token "if" whilst in state `else-boundary').
599 ;; We drop through to the second leaf (which handles common
600 ;; state) if no specific handler is found in the first cond.
601 ;; If a parsing error is detected (e.g. an "else" with no
602 ;; preceding "if"), we throw to the enclosing catch.
603 ;;
604 ;; Note that the (eq state 'else) means
605 ;; "we've just passed an else", NOT "we're looking for an
606 ;; else".
a66cd3ee
MS
607 (or (cond
608 ((eq state 'else)
609 (if (eq sym 'boundary)
610 (setq state 'else-boundary)
611 (c-bos-report-error)
612 (c-bos-pop-state-and-retry)))
613
614 ((eq state 'else-boundary)
615 (cond ((eq sym 'if)
616 (c-bos-pop-state (setq ret 'beginning)))
617 ((eq sym 'boundary)
618 (c-bos-report-error)
619 (c-bos-pop-state))))
620
621 ((eq state 'while)
622 (if (and (eq sym 'boundary)
623 ;; Since this can cause backtracking we do a
624 ;; little more careful analysis to avoid it:
625 ;; If there's a label in front of the while
626 ;; it can't be part of a do-while.
627 (not after-labels-pos))
628 (progn (c-bos-save-pos)
629 (setq state 'while-boundary))
d9e94c22 630 (c-bos-pop-state-and-retry))) ; Can't be a do-while
a66cd3ee
MS
631
632 ((eq state 'while-boundary)
633 (cond ((eq sym 'do)
634 (c-bos-pop-state (setq ret 'beginning)))
d9e94c22
MS
635 ((eq sym 'boundary) ; isn't a do-while
636 (c-bos-restore-pos) ; the position of the while
637 (c-bos-pop-state)))) ; no longer searching for do.
a66cd3ee
MS
638
639 ((eq state 'catch)
640 (if (eq sym 'boundary)
641 (setq state 'catch-boundary)
642 (c-bos-report-error)
643 (c-bos-pop-state-and-retry)))
644
645 ((eq state 'catch-boundary)
646 (cond
647 ((eq sym 'try)
648 (c-bos-pop-state (setq ret 'beginning)))
649 ((eq sym 'catch)
650 (setq state 'catch))
651 ((eq sym 'boundary)
652 (c-bos-report-error)
653 (c-bos-pop-state)))))
654
d9e94c22
MS
655 ;; This is state common. We get here when the previous
656 ;; cond statement found no particular state handler.
a66cd3ee 657 (cond ((eq sym 'boundary)
d9e94c22
MS
658 ;; If we have a boundary at the start
659 ;; position we push a frame to go to the
660 ;; previous statement.
661 (if (>= pos start)
662 (c-bos-push-state)
663 (c-bos-pop-state)))
a66cd3ee
MS
664 ((eq sym 'else)
665 (c-bos-push-state)
666 (c-bos-save-error-info 'if 'else)
667 (setq state 'else))
668 ((eq sym 'while)
669 (when (or (not pptok)
d9e94c22
MS
670 (memq (char-after pptok) delims)
671 (and (c-mode-is-new-awk-p)
672 (or
673 ;; might we be calling this from
674 ;; c-awk-after-if-do-for-while-condition-p?
675 ;; If so, avoid infinite recursion.
676 (and (eq (point) start)
677 (c-awk-NL-prop-not-set))
678 ;; The following may recursively
679 ;; call this function.
680 (c-awk-completed-stmt-ws-ends-line-p pptok))))
a66cd3ee
MS
681 ;; Since this can cause backtracking we do a
682 ;; little more careful analysis to avoid it: If
683 ;; the while isn't followed by a semicolon it
684 ;; can't be a do-while.
d9e94c22 685 ;; ACM, 2002/5/31; IT CAN IN AWK Mode. ;-(
a66cd3ee
MS
686 (c-bos-push-state)
687 (setq state 'while)))
688 ((memq sym '(catch finally))
689 (c-bos-push-state)
690 (c-bos-save-error-info 'try sym)
691 (setq state 'catch))))
692
693 (when c-maybe-labelp
694 ;; We're either past a statement boundary or at the
695 ;; start of a statement, so throw away any label data
696 ;; for the previous one.
697 (setq after-labels-pos nil
698 last-label-pos nil
699 c-maybe-labelp nil))))
700
d9e94c22
MS
701 ;; Step to the previous sexp, but not if we crossed a
702 ;; boundary, since that doesn't consume an sexp.
a66cd3ee
MS
703 (if (eq sym 'boundary)
704 (setq ret 'previous)
d9e94c22
MS
705
706 ;; HERE IS THE SINGLE PLACE INSIDE THE PDA LOOP WHERE WE MOVE
707 ;; BACKWARDS THROUGH THE SOURCE. The following loop goes back
708 ;; one sexp and then only loops in special circumstances (line
709 ;; continuations and skipping past entire macros).
a66cd3ee 710 (while
041ec7f6 711 (progn
a66cd3ee 712 (or (c-safe (goto-char (scan-sexps (point) -1)) t)
d9e94c22
MS
713 ;; Give up if we hit an unbalanced block.
714 ;; Since the stack won't be empty the code
715 ;; below will report a suitable error.
a66cd3ee
MS
716 (throw 'loop nil))
717 (cond ((looking-at "\\\\$")
718 ;; Step again if we hit a line continuation.
719 t)
720 (macro-start
721 ;; If we started inside a macro then this
722 ;; sexp is always interesting.
723 nil)
d9e94c22 724 ((not (c-mode-is-new-awk-p)) ; Changed from t, ACM 2002/6/25
a66cd3ee
MS
725 ;; Otherwise check that we didn't step
726 ;; into a macro from the end.
727 (let ((macro-start
728 (save-excursion
729 (and (c-beginning-of-macro)
730 (point)))))
731 (when macro-start
732 (goto-char macro-start)
733 t))))))
734
d9e94c22 735 ;; Did the last movement by a sexp cross a statement boundary?
a66cd3ee
MS
736 (when (save-excursion
737 (if (if (eq (char-after) ?{)
738 (c-looking-at-inexpr-block lim nil)
d9e94c22
MS
739 (looking-at "\\s\("))
740
741 ;; Should not include the paren sexp we've
742 ;; passed over in the boundary check.
743 (if (> (point) (- pos 100))
744 (c-forward-sexp 1)
745
746 ;; Find its end position this way instead of
747 ;; moving forward if the sexp is large.
748 (goto-char pos)
749 (while
750 (progn
751 (goto-char (1+ (c-down-list-backward)))
752 (unless macro-start
753 ;; Check that we didn't step into
754 ;; a macro from the end.
755 (let ((macro-start
756 (save-excursion
757 (and (c-beginning-of-macro)
758 (point)))))
759 (when macro-start
760 (goto-char macro-start)
761 t)))))))
762
a66cd3ee
MS
763 (setq boundary-pos (c-crosses-statement-barrier-p
764 (point) pos)))
d9e94c22 765
a66cd3ee
MS
766 (setq pptok ptok
767 ptok tok
768 tok boundary-pos
769 sym 'boundary)
d9e94c22 770 (throw 'loop t))) ; like a C "continue". Analyze the next sexp.
a66cd3ee
MS
771
772 (when (and (numberp c-maybe-labelp) (not ignore-labels))
773 ;; c-crosses-statement-barrier-p has found a colon, so
774 ;; we might be in a label now.
775 (if (not after-labels-pos)
776 (setq after-labels-pos tok))
777 (setq last-label-pos tok
778 c-maybe-labelp t))
779
780 ;; ObjC method def?
781 (when (and c-opt-method-key
782 (setq saved (c-in-method-def-p)))
783 (setq pos saved
784 ignore-labels t) ; Avoid the label check on exit.
785 (throw 'loop nil))
786
d9e94c22 787 ;; We've moved back by a sexp, so update the token positions.
a66cd3ee
MS
788 (setq sym nil
789 pptok ptok
790 ptok tok
791 tok (point)
d9e94c22 792 pos tok))) ; Not nil (for the while loop).
a66cd3ee
MS
793
794 ;; If the stack isn't empty there might be errors to report.
795 (while stack
796 (if (and (vectorp saved-pos) (eq (length saved-pos) 3))
797 (c-bos-report-error))
798 (setq saved-pos (cdr (car stack))
799 stack (cdr stack)))
800
801 (when (and (eq ret 'same)
802 (not (memq sym '(boundary ignore nil))))
803 ;; Need to investigate closer whether we've crossed
804 ;; between a substatement and its containing statement.
805 (if (setq saved (if (looking-at c-block-stmt-1-key)
806 ptok
807 pptok))
808 (cond ((> start saved) (setq pos saved))
809 ((= start saved) (setq ret 'up)))))
810
d9e94c22
MS
811 (when (and c-maybe-labelp
812 (not ignore-labels)
813 (not (eq ret 'beginning))
814 after-labels-pos)
a66cd3ee
MS
815 ;; We're in a label. Maybe we should step to the statement
816 ;; after it.
817 (if (< after-labels-pos start)
818 (setq pos after-labels-pos)
819 (setq ret 'label)
820 (if (< last-label-pos start)
821 (setq pos last-label-pos)))))
822
823 ;; Skip over the unary operators that can start the statement.
824 (goto-char pos)
825 (while (progn
7bfc3fdb
MS
826 (if (c-mode-is-new-awk-p)
827 (c-awk-backward-syntactic-ws)
828 (c-backward-syntactic-ws))
d9e94c22 829 (/= (skip-chars-backward "-+!*&~@`#") 0)) ; Hopefully the # won't hurt awk.
a66cd3ee
MS
830 (setq pos (point)))
831 (goto-char pos)
832 ret)))
785eecbb 833
785eecbb 834(defun c-crosses-statement-barrier-p (from to)
a66cd3ee
MS
835 "Return non-nil if buffer positions FROM to TO cross one or more
836statement or declaration boundaries. The returned value is actually
d9e94c22
MS
837the position of the earliest boundary char. FROM must not be within
838a string or comment.
a66cd3ee
MS
839
840The variable `c-maybe-labelp' is set to the position of the first `:' that
841might start a label (i.e. not part of `::' and not preceded by `?'). If a
842single `?' is found, then `c-maybe-labelp' is cleared."
843 (let ((skip-chars c-stmt-delim-chars)
844 lit-range)
845 (save-excursion
846 (catch 'done
847 (goto-char from)
848 (while (progn (skip-chars-forward skip-chars to)
785eecbb 849 (< (point) to))
d9e94c22
MS
850 (if (setq lit-range (c-literal-limits from)) ; Have we landed in a string/comment?
851 (progn (goto-char (setq from (cdr lit-range)))
852 (if (and (c-mode-is-new-awk-p) (bolp)) ; ACM 2002/7/17. Make sure we
853 (backward-char))) ; don't skip over a virtual semi-colon after an awk comment. :-(
a66cd3ee
MS
854 (cond ((eq (char-after) ?:)
855 (forward-char)
856 (if (and (eq (char-after) ?:)
857 (< (point) to))
858 ;; Ignore scope operators.
859 (forward-char)
860 (setq c-maybe-labelp (1- (point)))))
861 ((eq (char-after) ??)
862 ;; A question mark. Can't be a label, so stop
863 ;; looking for more : and ?.
864 (setq c-maybe-labelp nil
865 skip-chars (substring c-stmt-delim-chars 0 -2)))
d9e94c22
MS
866 ((and (eolp) ; Can only happen in AWK Mode
867 (not (c-awk-completed-stmt-ws-ends-line-p)))
868 (forward-char))
869 ((and (c-mode-is-new-awk-p)
870 (bolp) lit-range ; awk: comment/string ended prev line.
871 (not (c-awk-completed-stmt-ws-ends-prev-line-p))))
a66cd3ee
MS
872 (t (throw 'done (point))))))
873 nil))))
785eecbb
RS
874
875\f
d9e94c22
MS
876;; A set of functions that covers various idiosyncrasies in
877;; implementations of `forward-comment'.
878
879;; Note: Some emacsen considers incorrectly that any line comment
880;; ending with a backslash continues to the next line. I can't think
881;; of any way to work around that in a reliable way without changing
882;; the buffer, though. Suggestions welcome. ;) (No, temporarily
883;; changing the syntax for backslash doesn't work since we must treat
884;; escapes in string literals correctly.)
885
886(defun c-forward-single-comment ()
887 "Move forward past whitespace and the closest following comment, if any.
888Return t if a comment was found, nil otherwise. In either case, the
889point is moved past the following whitespace. Line continuations,
890i.e. a backslashes followed by line breaks, are treated as whitespace.
891The line breaks that end line comments are considered to be the
892comment enders, so the point will be put on the beginning of the next
893line if it moved past a line comment.
894
895This function does not do any hidden buffer changes."
896
897 (let ((start (point)))
898 (when (looking-at "\\([ \t\n\r\f\v]\\|\\\\[\n\r]\\)+")
899 (goto-char (match-end 0)))
900
901 (when (forward-comment 1)
902 (if (eobp)
903 ;; Some emacsen (e.g. XEmacs 21) return t when moving
904 ;; forwards at eob.
905 nil
906
907 ;; Emacs includes the ending newline in a b-style (c++)
908 ;; comment, but XEmacs doesn't. We depend on the Emacs
909 ;; behavior (which also is symmetric).
910 (if (and (eolp) (elt (parse-partial-sexp start (point)) 7))
911 (condition-case nil (forward-char 1)))
912
913 t))))
914
915(defsubst c-forward-comments ()
916 "Move forward past all following whitespace and comments.
917Line continuations, i.e. a backslashes followed by line breaks, are
918treated as whitespace.
919
920This function does not do any hidden buffer changes."
921
922 (while (or
923 ;; If forward-comment in at least XEmacs 21 is given a large
924 ;; positive value, it'll loop all the way through if it hits
925 ;; eob.
926 (and (forward-comment 5)
927 ;; Some emacsen (e.g. XEmacs 21) return t when moving
928 ;; forwards at eob.
929 (not (eobp)))
930
931 (when (looking-at "\\\\[\n\r]")
932 (forward-char 2)
933 t))))
934
935(defun c-backward-single-comment ()
936 "Move backward past whitespace and the closest preceding comment, if any.
937Return t if a comment was found, nil otherwise. In either case, the
938point is moved past the preceding whitespace. Line continuations,
939i.e. a backslashes followed by line breaks, are treated as whitespace.
940The line breaks that end line comments are considered to be the
941comment enders, so the point cannot be at the end of the same line to
942move over a line comment.
943
944This function does not do any hidden buffer changes."
945
946 (let ((start (point)))
947 ;; When we got newline terminated comments, forward-comment in all
948 ;; supported emacsen so far will stop at eol of each line not
949 ;; ending with a comment when moving backwards. This corrects for
950 ;; that, and at the same time handles line continuations.
951 (while (progn
952 (skip-chars-backward " \t\n\r\f\v")
953 (and (looking-at "[\n\r]")
954 (eq (char-before) ?\\)
955 (< (point) start)))
956 (backward-char))
957
958 (if (bobp)
959 ;; Some emacsen (e.g. Emacs 19.34) return t when moving
960 ;; backwards at bob.
961 nil
962
963 ;; Leave point after the closest following newline if we've
964 ;; backed up over any above, since forward-comment won't move
965 ;; backward over a line comment if point is at the end of the
966 ;; same line.
967 (re-search-forward "\\=\\s *[\n\r]" start t)
968
969 (if (if (forward-comment -1)
970 (if (eolp)
971 ;; If forward-comment above succeeded and we're at eol
972 ;; then the newline we moved over above didn't end a
973 ;; line comment, so we give it another go.
974 (forward-comment -1)
975 t))
976
977 ;; Emacs <= 20 and XEmacs move back over the closer of a
978 ;; block comment that lacks an opener.
979 (if (looking-at "\\*/")
980 (progn (forward-char 2) nil)
981 t)))))
982
983(defsubst c-backward-comments ()
984 "Move backward past all preceding whitespace and comments.
985Line continuations, i.e. a backslashes followed by line breaks, are
986treated as whitespace. The line breaks that end line comments are
987considered to be the comment enders, so the point cannot be at the end
988of the same line to move over a line comment.
989
990This function does not do any hidden buffer changes."
991
992 (let ((start (point)))
993 (while (and
994 ;; `forward-comment' in some emacsen (e.g. Emacs 19.34)
995 ;; return t when moving backwards at bob.
996 (not (bobp))
997
998 (if (forward-comment -1)
999 (if (looking-at "\\*/")
1000 ;; Emacs <= 20 and XEmacs move back over the
1001 ;; closer of a block comment that lacks an opener.
1002 (progn (forward-char 2) nil)
1003 t)
1004
1005 ;; XEmacs treats line continuations as whitespace but
1006 ;; only in the backward direction, which seems a bit
1007 ;; odd. Anyway, this is necessary for Emacs.
1008 (when (and (looking-at "[\n\r]")
1009 (eq (char-before) ?\\)
1010 (< (point) start))
1011 (backward-char)
1012 t))))))
1013
1014\f
1015;; Basic handling of preprocessor directives.
1016
a66cd3ee 1017;; This is a dynamically bound cache used together with
d9e94c22
MS
1018;; `c-query-macro-start' and `c-query-and-set-macro-start'. It only
1019;; works as long as point doesn't cross a macro boundary.
a66cd3ee
MS
1020(defvar c-macro-start 'unknown)
1021
1022(defsubst c-query-and-set-macro-start ()
d9e94c22 1023 ;; This function does not do any hidden buffer changes.
a66cd3ee
MS
1024 (if (symbolp c-macro-start)
1025 (setq c-macro-start (save-excursion
1026 (and (c-beginning-of-macro)
1027 (point))))
1028 c-macro-start))
1029
1030(defsubst c-query-macro-start ()
d9e94c22 1031 ;; This function does not do any hidden buffer changes.
a66cd3ee
MS
1032 (if (symbolp c-macro-start)
1033 (save-excursion
1034 (and (c-beginning-of-macro)
1035 (point)))
1036 c-macro-start))
1037
130c507e 1038(defun c-beginning-of-macro (&optional lim)
d9e94c22
MS
1039 "Go to the beginning of a preprocessor directive.
1040Leave point at the beginning of the directive and return t if in one,
1041otherwise return nil and leave point unchanged.
1042
1043This function does not do any hidden buffer changes."
1044 (when c-opt-cpp-prefix
1045 (let ((here (point)))
1046 (save-restriction
1047 (if lim (narrow-to-region lim (point-max)))
1048 (beginning-of-line)
1049 (while (eq (char-before (1- (point))) ?\\)
1050 (forward-line -1))
1051 (back-to-indentation)
1052 (if (and (<= (point) here)
1053 (looking-at c-opt-cpp-start))
1054 t
1055 (goto-char here)
1056 nil)))))
a66cd3ee
MS
1057
1058(defun c-end-of-macro ()
d9e94c22 1059 "Go to the end of a preprocessor directive.
a66cd3ee 1060More accurately, move point to the end of the closest following line
d9e94c22
MS
1061that doesn't end with a line continuation backslash.
1062
1063This function does not do any hidden buffer changes."
a66cd3ee
MS
1064 (while (progn
1065 (end-of-line)
1066 (when (and (eq (char-before) ?\\)
1067 (not (eobp)))
1068 (forward-char)
1069 t))))
1070
d9e94c22
MS
1071(defun c-forward-to-cpp-define-body ()
1072 ;; Assuming point is at the "#" that introduces a preprocessor
1073 ;; directive, it's moved forward to the start of the definition body
1074 ;; if it's a "#define". Non-nil is returned in this case, in all
1075 ;; other cases nil is returned and point isn't moved.
1076 (when (and (looking-at
1077 (concat "#[ \t]*"
1078 "define[ \t]+\\(\\sw\\|_\\)+\\(\([^\)]*\)\\)?"
1079 "\\([ \t]\\|\\\\\n\\)*"))
1080 (not (= (match-end 0) (c-point 'eol))))
1081 (goto-char (match-end 0))))
b2acd789 1082
d9e94c22
MS
1083\f
1084;; Tools for skipping over syntactic whitespace.
a66cd3ee 1085
d9e94c22
MS
1086;; The following functions use text properties to cache searches over
1087;; large regions of syntactic whitespace. It works as follows:
1088;;
1089;; o If a syntactic whitespace region contains anything but simple
1090;; whitespace (i.e. space, tab and line breaks), the text property
1091;; `c-in-sws' is put over it. At places where we have stopped
1092;; within that region there's also a `c-is-sws' text property.
1093;; That since there typically are nested whitespace inside that
1094;; must be handled separately, e.g. whitespace inside a comment or
1095;; cpp directive. Thus, from one point with `c-is-sws' it's safe
1096;; to jump to another point with that property within the same
1097;; `c-in-sws' region. It can be likened to a ladder where
1098;; `c-in-sws' marks the bars and `c-is-sws' the rungs.
1099;;
1100;; o The `c-is-sws' property is put on the simple whitespace chars at
1101;; a "rung position" and also maybe on the first following char.
1102;; As many characters as can be conveniently found in this range
1103;; are marked, but no assumption can be made that the whole range
1104;; is marked (it could be clobbered by later changes, for
1105;; instance).
1106;;
1107;; Note that some part of the beginning of a sequence of simple
1108;; whitespace might be part of the end of a preceding line comment
1109;; or cpp directive and must not be considered part of the "rung".
1110;; Such whitespace is some amount of horizontal whitespace followed
1111;; by a newline. In the case of cpp directives it could also be
1112;; two newlines with horizontal whitespace between them.
1113;;
1114;; The reason to include the first following char is to cope with
1115;; "rung positions" that doesn't have any ordinary whitespace. If
1116;; `c-is-sws' is put on a token character it does not have
1117;; `c-in-sws' set simultaneously. That's the only case when that
1118;; can occur, and the reason for not extending the `c-in-sws'
1119;; region to cover it is that the `c-in-sws' region could then be
1120;; accidentally merged with a following one if the token is only
1121;; one character long.
1122;;
1123;; o On buffer changes the `c-in-sws' and `c-is-sws' properties are
1124;; removed in the changed region. If the change was inside
1125;; syntactic whitespace that means that the "ladder" is broken, but
1126;; a later call to `c-forward-sws' or `c-backward-sws' will use the
1127;; parts on either side and use an ordinary search only to "repair"
1128;; the gap.
1129;;
1130;; Special care needs to be taken if a region is removed: If there
1131;; are `c-in-sws' on both sides of it which do not connect inside
1132;; the region then they can't be joined. If e.g. a marked macro is
1133;; broken, syntactic whitespace inside the new text might be
1134;; marked. If those marks would become connected with the old
1135;; `c-in-sws' range around the macro then we could get a ladder
1136;; with one end outside the macro and the other at some whitespace
1137;; within it.
1138;;
1139;; The main motivation for this system is to increase the speed in
1140;; skipping over the large whitespace regions that can occur at the
1141;; top level in e.g. header files that contain a lot of comments and
1142;; cpp directives. For small comments inside code it's probably
1143;; slower than using `forward-comment' straightforwardly, but speed is
1144;; not a significant factor there anyway.
1145
1146; (defface c-debug-is-sws-face
1147; '((t (:background "GreenYellow")))
1148; "Debug face to mark the `c-is-sws' property.")
1149; (defface c-debug-in-sws-face
1150; '((t (:underline t)))
1151; "Debug face to mark the `c-in-sws' property.")
1152
1153; (defun c-debug-put-sws-faces ()
1154; ;; Put the sws debug faces on all the `c-is-sws' and `c-in-sws'
1155; ;; properties in the buffer.
1156; (interactive)
1157; (save-excursion
1158; (let (in-face)
1159; (goto-char (point-min))
1160; (setq in-face (if (get-text-property (point) 'c-is-sws)
1161; (point)))
1162; (while (progn
1163; (goto-char (next-single-property-change
1164; (point) 'c-is-sws nil (point-max)))
1165; (if in-face
1166; (progn
1167; (c-debug-add-face in-face (point) 'c-debug-is-sws-face)
1168; (setq in-face nil))
1169; (setq in-face (point)))
1170; (not (eobp))))
1171; (goto-char (point-min))
1172; (setq in-face (if (get-text-property (point) 'c-in-sws)
1173; (point)))
1174; (while (progn
1175; (goto-char (next-single-property-change
1176; (point) 'c-in-sws nil (point-max)))
1177; (if in-face
1178; (progn
1179; (c-debug-add-face in-face (point) 'c-debug-in-sws-face)
1180; (setq in-face nil))
1181; (setq in-face (point)))
1182; (not (eobp)))))))
1183
1184(defmacro c-debug-sws-msg (&rest args)
1185 ;;`(message ,@args)
1186 )
1187
1188(defmacro c-put-is-sws (beg end)
1189 `(let ((beg ,beg) (end ,end))
1190 (put-text-property beg end 'c-is-sws t)
1191 ,@(when (facep 'c-debug-is-sws-face)
1192 `((c-debug-add-face beg end 'c-debug-is-sws-face)))))
1193
1194(defmacro c-put-in-sws (beg end)
1195 `(let ((beg ,beg) (end ,end))
1196 (put-text-property beg end 'c-in-sws t)
1197 ,@(when (facep 'c-debug-is-sws-face)
1198 `((c-debug-add-face beg end 'c-debug-in-sws-face)))))
1199
1200(defmacro c-remove-is-sws (beg end)
1201 `(let ((beg ,beg) (end ,end))
1202 (remove-text-properties beg end '(c-is-sws nil))
1203 ,@(when (facep 'c-debug-is-sws-face)
1204 `((c-debug-remove-face beg end 'c-debug-is-sws-face)))))
1205
1206(defmacro c-remove-in-sws (beg end)
1207 `(let ((beg ,beg) (end ,end))
1208 (remove-text-properties beg end '(c-in-sws nil))
1209 ,@(when (facep 'c-debug-is-sws-face)
1210 `((c-debug-remove-face beg end 'c-debug-in-sws-face)))))
1211
1212(defmacro c-remove-is-and-in-sws (beg end)
1213 `(let ((beg ,beg) (end ,end))
1214 (remove-text-properties beg end '(c-is-sws nil c-in-sws nil))
1215 ,@(when (facep 'c-debug-is-sws-face)
1216 `((c-debug-remove-face beg end 'c-debug-is-sws-face)
1217 (c-debug-remove-face beg end 'c-debug-in-sws-face)))))
1218
1219(defsubst c-invalidate-sws-region-after (beg end)
1220 ;; Called from `after-change-functions'. Note that if
1221 ;; `c-forward-sws' or `c-backward-sws' are used outside
1222 ;; `c-save-buffer-state' or similar then this will remove the cache
1223 ;; properties right after they're added.
1224
1225 (save-excursion
1226 ;; Adjust the end to remove the properties in any following simple
1227 ;; ws up to and including the next line break, if there is any
1228 ;; after the changed region. This is necessary e.g. when a rung
1229 ;; marked empty line is converted to a line comment by inserting
1230 ;; "//" before the line break. In that case the line break would
1231 ;; keep the rung mark which could make a later `c-backward-sws'
1232 ;; move into the line comment instead of over it.
1233 (goto-char end)
1234 (skip-chars-forward " \t\f\v")
1235 (when (and (eolp) (not (eobp)))
1236 (setq end (1+ (point)))))
1237
1238 (when (and (= beg end)
1239 (get-text-property beg 'c-in-sws)
1240 (not (bobp))
1241 (get-text-property (1- beg) 'c-in-sws))
1242 ;; Ensure that an `c-in-sws' range gets broken. Note that it isn't
1243 ;; safe to keep a range that was continuous before the change. E.g:
1244 ;;
1245 ;; #define foo
1246 ;; \
1247 ;; bar
1248 ;;
1249 ;; There can be a "ladder" between "#" and "b". Now, if the newline
1250 ;; after "foo" is removed then "bar" will become part of the cpp
1251 ;; directive instead of a syntactically relevant token. In that
1252 ;; case there's no longer syntactic ws from "#" to "b".
1253 (setq beg (1- beg)))
1254
1255 (c-debug-sws-msg "c-invalidate-sws-region-after [%s..%s]" beg end)
1256 (c-remove-is-and-in-sws beg end))
1257
1258(defun c-forward-sws ()
1259 ;; Used by `c-forward-syntactic-ws' to implement the unbounded search.
1260
1261 (let (;; `rung-pos' is set to a position as early as possible in the
1262 ;; unmarked part of the simple ws region.
1263 (rung-pos (point)) next-rung-pos rung-end-pos last-put-in-sws-pos
1264 rung-is-marked next-rung-is-marked simple-ws-end
1265 ;; `safe-start' is set when it's safe to cache the start position.
1266 ;; It's not set if we've initially skipped over comments and line
1267 ;; continuations since we might have gone out through the end of a
1268 ;; macro then. This provision makes `c-forward-sws' not populate the
1269 ;; cache in the majority of cases, but otoh is `c-backward-sws' by far
1270 ;; more common.
1271 safe-start)
1272
1273 ;; Skip simple ws and do a quick check on the following character to see
1274 ;; if it's anything that can't start syntactic ws, so we can bail out
1275 ;; early in the majority of cases when there just are a few ws chars.
1276 (skip-chars-forward " \t\n\r\f\v")
1277 (when (looking-at c-syntactic-ws-start)
1278
1279 (setq rung-end-pos (min (1+ (point)) (point-max)))
1280 (if (setq rung-is-marked (text-property-any rung-pos rung-end-pos
1281 'c-is-sws t))
1282 ;; Find the last rung position to avoid setting properties in all
1283 ;; the cases when the marked rung is complete.
1284 ;; (`next-single-property-change' is certain to move at least one
1285 ;; step forward.)
1286 (setq rung-pos (1- (next-single-property-change
1287 rung-is-marked 'c-is-sws nil rung-end-pos)))
1288 ;; Got no marked rung here. Since the simple ws might have started
1289 ;; inside a line comment or cpp directive we must set `rung-pos' as
1290 ;; high as possible.
1291 (setq rung-pos (point)))
1292
1293 (while
1294 (progn
1295 (while
1296 (when (and rung-is-marked
1297 (get-text-property (point) 'c-in-sws))
1298
1299 ;; The following search is the main reason that `c-in-sws'
1300 ;; and `c-is-sws' aren't combined to one property.
1301 (goto-char (next-single-property-change
1302 (point) 'c-in-sws nil (point-max)))
1303 (unless (get-text-property (point) 'c-is-sws)
1304 ;; If the `c-in-sws' region extended past the last
1305 ;; `c-is-sws' char we have to go back a bit.
1306 (or (get-text-property (1- (point)) 'c-is-sws)
1307 (goto-char (previous-single-property-change
1308 (point) 'c-is-sws)))
1309 (backward-char))
1310
1311 (c-debug-sws-msg
1312 "c-forward-sws cached move %s -> %s (max %s)"
1313 rung-pos (point) (point-max))
1314
1315 (setq rung-pos (point))
1316 (and (> (skip-chars-forward " \t\n\r\f\v") 0)
1317 (not (eobp))))
1318
1319 ;; We'll loop here if there is simple ws after the last rung.
1320 ;; That means that there's been some change in it and it's
1321 ;; possible that we've stepped into another ladder, so extend
1322 ;; the previous one to join with it if there is one, and try to
1323 ;; use the cache again.
1324 (c-debug-sws-msg
1325 "c-forward-sws extending rung with [%s..%s] (max %s)"
1326 (1+ rung-pos) (1+ (point)) (point-max))
1327 (unless (get-text-property (point) 'c-is-sws)
1328 ;; Remove any `c-in-sws' property from the last char of
1329 ;; the rung before we mark it with `c-is-sws', so that we
1330 ;; won't connect with the remains of a broken "ladder".
1331 (c-remove-in-sws (point) (1+ (point))))
1332 (c-put-is-sws (1+ rung-pos)
1333 (1+ (point)))
1334 (c-put-in-sws rung-pos
1335 (setq rung-pos (point)
1336 last-put-in-sws-pos rung-pos)))
1337
1338 (setq simple-ws-end (point))
1339 (c-forward-comments)
1340
1341 (cond
1342 ((/= (point) simple-ws-end)
1343 ;; Skipped over comments. Don't cache at eob in case the buffer
1344 ;; is narrowed.
1345 (not (eobp)))
1346
1347 ((save-excursion
1348 (and c-opt-cpp-prefix
1349 (looking-at c-opt-cpp-start)
1350 (progn (skip-chars-backward " \t")
1351 (bolp))
1352 (or (bobp)
1353 (progn (backward-char)
1354 (not (eq (char-before) ?\\))))))
1355 ;; Skip a preprocessor directive.
1356 (end-of-line)
1357 (while (and (eq (char-before) ?\\)
1358 (= (forward-line 1) 0))
1359 (end-of-line))
1360 (forward-line 1)
1361 (setq safe-start t)
1362 ;; Don't cache at eob in case the buffer is narrowed.
1363 (not (eobp)))))
1364
1365 ;; We've searched over a piece of non-white syntactic ws. See if this
1366 ;; can be cached.
1367 (setq next-rung-pos (point))
1368 (skip-chars-forward " \t\n\r\f\v")
1369 (setq rung-end-pos (min (1+ (point)) (point-max)))
1370
1371 (if (or
1372 ;; Cache if we haven't skipped comments only, and if we started
1373 ;; either from a marked rung or from a completely uncached
1374 ;; position.
1375 (and safe-start
1376 (or rung-is-marked
1377 (not (get-text-property simple-ws-end 'c-in-sws))))
1378
1379 ;; See if there's a marked rung in the encountered simple ws. If
1380 ;; so then we can cache, unless `safe-start' is nil. Even then
1381 ;; we need to do this to check if the cache can be used for the
1382 ;; next step.
1383 (and (setq next-rung-is-marked
1384 (text-property-any next-rung-pos rung-end-pos
1385 'c-is-sws t))
1386 safe-start))
b2acd789 1387
0ec8351b 1388 (progn
d9e94c22
MS
1389 (c-debug-sws-msg
1390 "c-forward-sws caching [%s..%s] - [%s..%s] (max %s)"
1391 rung-pos (1+ simple-ws-end) next-rung-pos rung-end-pos
1392 (point-max))
1393
1394 ;; Remove the properties for any nested ws that might be cached.
1395 ;; Only necessary for `c-is-sws' since `c-in-sws' will be set
1396 ;; anyway.
1397 (c-remove-is-sws (1+ simple-ws-end) next-rung-pos)
1398 (unless (and rung-is-marked (= rung-pos simple-ws-end))
1399 (c-put-is-sws rung-pos
1400 (1+ simple-ws-end))
1401 (setq rung-is-marked t))
1402 (c-put-in-sws rung-pos
1403 (setq rung-pos (point)
1404 last-put-in-sws-pos rung-pos))
1405 (unless (get-text-property (1- rung-end-pos) 'c-is-sws)
1406 ;; Remove any `c-in-sws' property from the last char of
1407 ;; the rung before we mark it with `c-is-sws', so that we
1408 ;; won't connect with the remains of a broken "ladder".
1409 (c-remove-in-sws (1- rung-end-pos) rung-end-pos))
1410 (c-put-is-sws next-rung-pos
1411 rung-end-pos))
1412
1413 (c-debug-sws-msg
1414 "c-forward-sws not caching [%s..%s] - [%s..%s] (max %s)"
1415 rung-pos (1+ simple-ws-end) next-rung-pos rung-end-pos
1416 (point-max))
1417
1418 ;; Set `rung-pos' for the next rung. It's the same thing here as
1419 ;; initially, except that the rung position is set as early as
1420 ;; possible since we can't be in the ending ws of a line comment or
1421 ;; cpp directive now.
1422 (if (setq rung-is-marked next-rung-is-marked)
1423 (setq rung-pos (1- (next-single-property-change
1424 rung-is-marked 'c-is-sws nil rung-end-pos)))
1425 (setq rung-pos next-rung-pos))
1426 (setq safe-start t)))
1427
1428 ;; Make sure that the newly marked `c-in-sws' region doesn't connect to
1429 ;; another one after the point (which might occur when editing inside a
1430 ;; comment or macro).
1431 (when (eq last-put-in-sws-pos (point))
1432 (cond ((< last-put-in-sws-pos (point-max))
1433 (c-debug-sws-msg
1434 "c-forward-sws clearing at %s for cache separation"
1435 last-put-in-sws-pos)
1436 (c-remove-in-sws last-put-in-sws-pos
1437 (1+ last-put-in-sws-pos)))
1438 (t
1439 ;; If at eob we have to clear the last character before the end
1440 ;; instead since the buffer might be narrowed and there might
1441 ;; be a `c-in-sws' after (point-max). In this case it's
1442 ;; necessary to clear both properties.
1443 (c-debug-sws-msg
1444 "c-forward-sws clearing thoroughly at %s for cache separation"
1445 (1- last-put-in-sws-pos))
1446 (c-remove-is-and-in-sws (1- last-put-in-sws-pos)
1447 last-put-in-sws-pos))))
1448 )))
b2acd789 1449
d9e94c22
MS
1450(defun c-backward-sws ()
1451 ;; Used by `c-backward-syntactic-ws' to implement the unbounded search.
1452
1453 (let (;; `rung-pos' is set to a position as late as possible in the unmarked
1454 ;; part of the simple ws region.
1455 (rung-pos (point)) next-rung-pos last-put-in-sws-pos
1456 rung-is-marked simple-ws-beg cmt-skip-pos)
1457
1458 ;; Skip simple horizontal ws and do a quick check on the preceding
1459 ;; character to see if it's anying that can't end syntactic ws, so we can
1460 ;; bail out early in the majority of cases when there just are a few ws
1461 ;; chars. Newlines are complicated in the backward direction, so we can't
1462 ;; skip over them.
1463 (skip-chars-backward " \t\f")
1464 (when (and (not (bobp))
1465 (save-excursion
1466 (backward-char)
1467 (looking-at c-syntactic-ws-end)))
1468
1469 ;; Try to find a rung position in the simple ws preceding point, so that
1470 ;; we can get a cache hit even if the last bit of the simple ws has
1471 ;; changed recently.
1472 (setq simple-ws-beg (point))
1473 (skip-chars-backward " \t\n\r\f\v")
1474 (if (setq rung-is-marked (text-property-any
1475 (point) (min (1+ rung-pos) (point-max))
1476 'c-is-sws t))
1477 ;; `rung-pos' will be the earliest marked position, which means that
1478 ;; there might be later unmarked parts in the simple ws region.
1479 ;; It's not worth the effort to fix that; the last part of the
1480 ;; simple ws is also typically edited often, so it could be wasted.
1481 (goto-char (setq rung-pos rung-is-marked))
1482 (goto-char simple-ws-beg))
1483
1484 (while
1485 (progn
1486 (while
1487 (when (and rung-is-marked
1488 (not (bobp))
1489 (get-text-property (1- (point)) 'c-in-sws))
1490
1491 ;; The following search is the main reason that `c-in-sws'
1492 ;; and `c-is-sws' aren't combined to one property.
1493 (goto-char (previous-single-property-change
1494 (point) 'c-in-sws nil (point-min)))
1495 (unless (get-text-property (point) 'c-is-sws)
1496 ;; If the `c-in-sws' region extended past the first
1497 ;; `c-is-sws' char we have to go forward a bit.
1498 (goto-char (next-single-property-change
1499 (point) 'c-is-sws)))
1500
1501 (c-debug-sws-msg
1502 "c-backward-sws cached move %s <- %s (min %s)"
1503 (point) rung-pos (point-min))
1504
1505 (setq rung-pos (point))
1506 (if (and (< (min (skip-chars-backward " \t\f\v")
1507 (progn
1508 (setq simple-ws-beg (point))
1509 (skip-chars-backward " \t\n\r\f\v")))
1510 0)
1511 (setq rung-is-marked
1512 (text-property-any (point) rung-pos
1513 'c-is-sws t)))
1514 t
1515 (goto-char simple-ws-beg)
1516 nil))
1517
1518 ;; We'll loop here if there is simple ws before the first rung.
1519 ;; That means that there's been some change in it and it's
1520 ;; possible that we've stepped into another ladder, so extend
1521 ;; the previous one to join with it if there is one, and try to
1522 ;; use the cache again.
1523 (c-debug-sws-msg
1524 "c-backward-sws extending rung with [%s..%s] (min %s)"
1525 rung-is-marked rung-pos (point-min))
1526 (unless (get-text-property (1- rung-pos) 'c-is-sws)
1527 ;; Remove any `c-in-sws' property from the last char of
1528 ;; the rung before we mark it with `c-is-sws', so that we
1529 ;; won't connect with the remains of a broken "ladder".
1530 (c-remove-in-sws (1- rung-pos) rung-pos))
1531 (c-put-is-sws rung-is-marked
1532 rung-pos)
1533 (c-put-in-sws rung-is-marked
1534 (1- rung-pos))
1535 (setq rung-pos rung-is-marked
1536 last-put-in-sws-pos rung-pos))
1537
1538 (c-backward-comments)
1539 (setq cmt-skip-pos (point))
a66cd3ee 1540
d9e94c22
MS
1541 (cond
1542 ((and c-opt-cpp-prefix
1543 (/= cmt-skip-pos simple-ws-beg)
1544 (c-beginning-of-macro))
1545 ;; Inside a cpp directive. See if it should be skipped over.
1546 (let ((cpp-beg (point)))
1547
1548 ;; Move back over all line continuations in the region skipped
1549 ;; over by `c-backward-comments'. If we go past it then we
1550 ;; started inside the cpp directive.
1551 (goto-char simple-ws-beg)
1552 (beginning-of-line)
1553 (while (and (> (point) cmt-skip-pos)
1554 (progn (backward-char)
1555 (eq (char-before) ?\\)))
1556 (beginning-of-line))
1557
1558 (if (< (point) cmt-skip-pos)
1559 ;; Don't move past the cpp directive if we began inside
1560 ;; it. Note that the position at the end of the last line
1561 ;; of the macro is also considered to be within it.
1562 (progn (goto-char cmt-skip-pos)
1563 nil)
1564
1565 ;; It's worthwhile to spend a little bit of effort on finding
1566 ;; the end of the macro, to get a good `simple-ws-beg'
1567 ;; position for the cache. Note that `c-backward-comments'
1568 ;; could have stepped over some comments before going into
1569 ;; the macro, and then `simple-ws-beg' must be kept on the
1570 ;; same side of those comments.
1571 (goto-char simple-ws-beg)
1572 (skip-chars-backward " \t\n\r\f\v")
1573 (if (eq (char-before) ?\\)
1574 (forward-char))
1575 (forward-line 1)
1576 (if (< (point) simple-ws-beg)
1577 ;; Might happen if comments after the macro were skipped
1578 ;; over.
1579 (setq simple-ws-beg (point)))
1580
1581 (goto-char cpp-beg)
1582 t)))
1583
1584 ((/= (save-excursion
1585 (skip-chars-forward " \t\n\r\f\v" simple-ws-beg)
1586 (setq next-rung-pos (point)))
1587 simple-ws-beg)
1588 ;; Skipped over comments. Must put point at the end of
1589 ;; the simple ws at point since we might be after a line
1590 ;; comment or cpp directive that's been partially
1591 ;; narrowed out, and we can't risk marking the simple ws
1592 ;; at the end of it.
1593 (goto-char next-rung-pos)
1594 t)))
1595
1596 ;; We've searched over a piece of non-white syntactic ws. See if this
1597 ;; can be cached.
1598 (setq next-rung-pos (point))
1599 (skip-chars-backward " \t\f\v")
1600
1601 (if (or
1602 ;; Cache if we started either from a marked rung or from a
1603 ;; completely uncached position.
1604 rung-is-marked
1605 (not (get-text-property (1- simple-ws-beg) 'c-in-sws))
1606
1607 ;; Cache if there's a marked rung in the encountered simple ws.
1608 (save-excursion
1609 (skip-chars-backward " \t\n\r\f\v")
1610 (text-property-any (point) (min (1+ next-rung-pos) (point-max))
1611 'c-is-sws t)))
a66cd3ee 1612
d9e94c22
MS
1613 (progn
1614 (c-debug-sws-msg
1615 "c-backward-sws caching [%s..%s] - [%s..%s] (min %s)"
1616 (point) (1+ next-rung-pos)
1617 simple-ws-beg (min (1+ rung-pos) (point-max))
1618 (point-min))
1619
1620 ;; Remove the properties for any nested ws that might be cached.
1621 ;; Only necessary for `c-is-sws' since `c-in-sws' will be set
1622 ;; anyway.
1623 (c-remove-is-sws (1+ next-rung-pos) simple-ws-beg)
1624 (unless (and rung-is-marked (= simple-ws-beg rung-pos))
1625 (let ((rung-end-pos (min (1+ rung-pos) (point-max))))
1626 (unless (get-text-property (1- rung-end-pos) 'c-is-sws)
1627 ;; Remove any `c-in-sws' property from the last char of
1628 ;; the rung before we mark it with `c-is-sws', so that we
1629 ;; won't connect with the remains of a broken "ladder".
1630 (c-remove-in-sws (1- rung-end-pos) rung-end-pos))
1631 (c-put-is-sws simple-ws-beg
1632 rung-end-pos)
1633 (setq rung-is-marked t)))
1634 (c-put-in-sws (setq simple-ws-beg (point)
1635 last-put-in-sws-pos simple-ws-beg)
1636 rung-pos)
1637 (c-put-is-sws (setq rung-pos simple-ws-beg)
1638 (1+ next-rung-pos)))
1639
1640 (c-debug-sws-msg
1641 "c-backward-sws not caching [%s..%s] - [%s..%s] (min %s)"
1642 (point) (1+ next-rung-pos)
1643 simple-ws-beg (min (1+ rung-pos) (point-max))
1644 (point-min))
1645 (setq rung-pos next-rung-pos
1646 simple-ws-beg (point))
1647 ))
1648
1649 ;; Make sure that the newly marked `c-in-sws' region doesn't connect to
1650 ;; another one before the point (which might occur when editing inside a
1651 ;; comment or macro).
1652 (when (eq last-put-in-sws-pos (point))
1653 (cond ((< (point-min) last-put-in-sws-pos)
1654 (c-debug-sws-msg
1655 "c-backward-sws clearing at %s for cache separation"
1656 (1- last-put-in-sws-pos))
1657 (c-remove-in-sws (1- last-put-in-sws-pos)
1658 last-put-in-sws-pos))
1659 ((> (point-min) 1)
1660 ;; If at bob and the buffer is narrowed, we have to clear the
1661 ;; character we're standing on instead since there might be a
1662 ;; `c-in-sws' before (point-min). In this case it's necessary
1663 ;; to clear both properties.
1664 (c-debug-sws-msg
1665 "c-backward-sws clearing thoroughly at %s for cache separation"
1666 last-put-in-sws-pos)
1667 (c-remove-is-and-in-sws last-put-in-sws-pos
1668 (1+ last-put-in-sws-pos)))))
1669 )))
785eecbb 1670
d9e94c22
MS
1671\f
1672;; A system for handling noteworthy parens before the point.
e1c458ae 1673
d9e94c22
MS
1674(defvar c-state-cache nil)
1675(make-variable-buffer-local 'c-state-cache)
1676;; The state cache used by `c-parse-state' to cut down the amount of
1677;; searching. It's the result from some earlier `c-parse-state' call.
1678;; The use of the cached info is more effective if the next
1679;; `c-parse-state' call is on a line close by the one the cached state
1680;; was made at; the cache can actually slow down a little if the
1681;; cached state was made very far back in the buffer. The cache is
1682;; most effective if `c-parse-state' is used on each line while moving
1683;; forward.
e1c458ae 1684
d9e94c22
MS
1685(defvar c-state-cache-start 1)
1686(make-variable-buffer-local 'c-state-cache-start)
1687;; This is (point-min) when `c-state-cache' was calculated, since a
1688;; change of narrowing is likely to affect the parens that are visible
1689;; before the point.
1690
1691(defsubst c-invalidate-state-cache (pos)
1692 ;; Invalidate all info on `c-state-cache' that applies to the buffer
1693 ;; at POS or higher. This is much like `c-whack-state-after', but
1694 ;; it never changes a paren pair element into an open paren element.
1695 ;; Doing that would mean that the new open paren wouldn't have the
1696 ;; required preceding paren pair element.
1697 ;;
1698 ;; This function does not do any hidden buffer changes.
1699 (while (and c-state-cache
1700 (let ((elem (car c-state-cache)))
1701 (if (consp elem)
1702 (or (<= pos (car elem))
1703 (< pos (cdr elem)))
1704 (<= pos elem))))
1705 (setq c-state-cache (cdr c-state-cache))))
785eecbb
RS
1706
1707(defun c-parse-state ()
a66cd3ee
MS
1708 ;; Finds and records all noteworthy parens between some good point
1709 ;; earlier in the file and point. That good point is at least the
1710 ;; beginning of the top-level construct we are in, or the beginning
1711 ;; of the preceding top-level construct if we aren't in one.
785eecbb 1712 ;;
a66cd3ee
MS
1713 ;; The returned value is a list of the noteworthy parens with the
1714 ;; last one first. If an element in the list is an integer, it's
1715 ;; the position of an open paren which has not been closed before
d9e94c22 1716 ;; the point. If an element is a cons, it gives the position of a
a66cd3ee
MS
1717 ;; closed brace paren pair; the car is the start paren position and
1718 ;; the cdr is the position following the closing paren. Only the
1719 ;; last closed brace paren pair before each open paren is recorded,
1720 ;; and thus the state never contains two cons elements in
1721 ;; succession.
d9e94c22
MS
1722 ;;
1723 ;; Currently no characters which are given paren syntax with the
1724 ;; syntax-table property are recorded, i.e. angle bracket arglist
1725 ;; parens are never present here. Note that this might change.
1726 ;;
1727 ;; This function does not do any hidden buffer changes.
1728
a66cd3ee
MS
1729 (save-restriction
1730 (let* ((here (point))
1731 (c-macro-start (c-query-macro-start))
1732 (in-macro-start (or c-macro-start (point)))
e33c01bb 1733 old-state last-pos pairs pos save-pos)
d9e94c22
MS
1734 (c-invalidate-state-cache (point))
1735
1736 ;; If the minimum position has changed due to narrowing then we
1737 ;; have to fix the tail of `c-state-cache' accordingly.
1738 (unless (= c-state-cache-start (point-min))
1739 (if (> (point-min) c-state-cache-start)
1740 ;; If point-min has moved forward then we just need to cut
1741 ;; off a bit of the tail.
1742 (let ((ptr (cons nil c-state-cache)) elem)
1743 (while (and (setq elem (cdr ptr))
1744 (>= (if (consp elem) (car elem) elem)
1745 (point-min)))
1746 (setq ptr elem))
1747 (when (consp ptr)
1748 (if (eq (cdr ptr) c-state-cache)
1749 (setq c-state-cache nil)
1750 (setcdr ptr nil))))
1751 ;; If point-min has moved backward then we drop the state
1752 ;; completely. It's possible to do a better job here and
1753 ;; recalculate the top only.
1754 (setq c-state-cache nil))
1755 (setq c-state-cache-start (point-min)))
1756
a66cd3ee
MS
1757 ;; Get the latest position we know are directly inside the
1758 ;; closest containing paren of the cached state.
1759 (setq last-pos (and c-state-cache
1760 (if (consp (car c-state-cache))
1761 (cdr (car c-state-cache))
1762 (1+ (car c-state-cache)))))
d9e94c22 1763
a66cd3ee
MS
1764 ;; Check if the found last-pos is in a macro. If it is, and
1765 ;; we're not in the same macro, we must discard everything on
1766 ;; c-state-cache that is inside the macro before using it.
1767 (when last-pos
1768 (save-excursion
1769 (goto-char last-pos)
1770 (when (and (c-beginning-of-macro)
1771 (/= (point) in-macro-start))
d9e94c22 1772 (c-invalidate-state-cache (point))
a66cd3ee
MS
1773 ;; Set last-pos again, just like above.
1774 (setq last-pos (and c-state-cache
1775 (if (consp (car c-state-cache))
1776 (cdr (car c-state-cache))
1777 (1+ (car c-state-cache))))))))
d9e94c22 1778
a66cd3ee
MS
1779 (setq pos
1780 ;; Find the start position for the forward search. (Can't
1781 ;; search in the backward direction since point might be
1782 ;; in some kind of literal.)
1783 (or (when last-pos
d9e94c22 1784
a66cd3ee
MS
1785 ;; There's a cached state with a containing paren. Pop
1786 ;; off the stale containing sexps from it by going
1787 ;; forward out of parens as far as possible.
1788 (narrow-to-region (point-min) here)
1789 (let (placeholder pair-beg)
1790 (while (and c-state-cache
1791 (setq placeholder
1792 (c-up-list-forward last-pos)))
1793 (setq last-pos placeholder)
1794 (if (consp (car c-state-cache))
1795 (setq pair-beg (car-safe (cdr c-state-cache))
1796 c-state-cache (cdr-safe (cdr c-state-cache)))
1797 (setq pair-beg (car c-state-cache)
1798 c-state-cache (cdr c-state-cache))))
d9e94c22 1799
a66cd3ee
MS
1800 (when (and pair-beg (eq (char-after pair-beg) ?{))
1801 ;; The last paren pair we moved out from was a brace
1802 ;; pair. Modify the state to record this as a closed
1803 ;; pair now.
1804 (if (consp (car-safe c-state-cache))
1805 (setq c-state-cache (cdr c-state-cache)))
1806 (setq c-state-cache (cons (cons pair-beg last-pos)
1807 c-state-cache))))
d9e94c22 1808
a66cd3ee
MS
1809 ;; Check if the preceding balanced paren is within a
1810 ;; macro; it should be ignored if we're outside the
1811 ;; macro. There's no need to check any further upwards;
1812 ;; if the macro contains an unbalanced opening paren then
1813 ;; we're smoked anyway.
1814 (when (and (<= (point) in-macro-start)
1815 (consp (car c-state-cache)))
1816 (save-excursion
1817 (goto-char (car (car c-state-cache)))
1818 (when (c-beginning-of-macro)
1819 (setq here (point)
1820 c-state-cache (cdr c-state-cache)))))
d9e94c22 1821
a66cd3ee
MS
1822 (when c-state-cache
1823 (setq old-state c-state-cache)
1824 last-pos))
d9e94c22 1825
a66cd3ee 1826 (save-excursion
785eecbb 1827 ;; go back 2 bods, but ignore any bogus positions
a66cd3ee
MS
1828 ;; returned by beginning-of-defun (i.e. open paren in
1829 ;; column zero)
1830 (goto-char here)
785eecbb 1831 (let ((cnt 2))
a66cd3ee
MS
1832 (while (not (or (bobp) (zerop cnt)))
1833 (c-beginning-of-defun-1)
1834 (if (eq (char-after) ?\{)
1835 (setq cnt (1- cnt)))))
1836 (point))))
d9e94c22 1837
a66cd3ee 1838 (narrow-to-region (point-min) here)
d9e94c22 1839
a66cd3ee
MS
1840 (while pos
1841 ;; Find the balanced brace pairs.
e33c01bb
MS
1842 (setq save-pos pos
1843 pairs nil)
a66cd3ee
MS
1844 (while (and (setq last-pos (c-down-list-forward pos))
1845 (setq pos (c-up-list-forward last-pos)))
1846 (if (eq (char-before last-pos) ?{)
1847 (setq pairs (cons (cons last-pos pos) pairs))))
d9e94c22 1848
a66cd3ee
MS
1849 ;; Should ignore any pairs that are in a macro, providing
1850 ;; we're not in the same one.
1851 (when (and pairs (< (car (car pairs)) in-macro-start))
1852 (while (and (save-excursion
1853 (goto-char (car (car pairs)))
1854 (c-beginning-of-macro))
1855 (setq pairs (cdr pairs)))))
d9e94c22 1856
a66cd3ee
MS
1857 ;; Record the last brace pair.
1858 (when pairs
1859 (if (and (eq c-state-cache old-state)
1860 (consp (car-safe c-state-cache)))
1861 ;; There's a closed pair on the cached state but we've
1862 ;; found a later one, so remove it.
1863 (setq c-state-cache (cdr c-state-cache)))
1864 (setq pairs (car pairs))
1865 (setcar pairs (1- (car pairs)))
0e35704f
MS
1866 (when (consp (car-safe c-state-cache))
1867 ;; There could already be a cons first in `c-state-cache'
d9e94c22 1868 ;; if we've e.g. jumped over an unbalanced open paren in a
0e35704f
MS
1869 ;; macro below.
1870 (setq c-state-cache (cdr c-state-cache)))
a66cd3ee 1871 (setq c-state-cache (cons pairs c-state-cache)))
d9e94c22 1872
a66cd3ee
MS
1873 (if last-pos
1874 ;; Prepare to loop, but record the open paren only if it's
d9e94c22
MS
1875 ;; outside a macro or within the same macro as point, and
1876 ;; if it is a "real" open paren and not some character
1877 ;; that got an open paren syntax-table property.
a66cd3ee
MS
1878 (progn
1879 (setq pos last-pos)
d9e94c22
MS
1880 (if (and (or (>= last-pos in-macro-start)
1881 (save-excursion
1882 (goto-char last-pos)
1883 (not (c-beginning-of-macro))))
1884 (= (char-syntax (char-before last-pos)) ?\())
1885 (setq c-state-cache (cons (1- last-pos) c-state-cache))))
1886
a66cd3ee
MS
1887 (if (setq last-pos (c-up-list-forward pos))
1888 ;; Found a close paren without a corresponding opening
1889 ;; one. Maybe we didn't go back far enough, so try to
1890 ;; scan backward for the start paren and then start over.
1891 (progn
1892 (setq pos (c-up-list-backward pos)
1893 c-state-cache nil)
e33c01bb
MS
1894 (when (or (not pos)
1895 ;; Emacs (up to at least 21.2) can get confused by
1896 ;; open parens in column zero inside comments: The
1897 ;; sexp functions can then misbehave and bring us
1898 ;; back to the same point again. Check this so that
1899 ;; we don't get an infinite loop.
1900 (>= pos save-pos))
a66cd3ee
MS
1901 (setq pos last-pos
1902 c-parsing-error
1903 (format "Unbalanced close paren at line %d"
1904 (1+ (count-lines (point-min)
1905 (c-point 'bol last-pos)))))))
1906 (setq pos nil))))
d9e94c22 1907
a66cd3ee
MS
1908 c-state-cache)))
1909
1910;; Debug tool to catch cache inconsistencies.
1911(defvar c-debug-parse-state nil)
1912(unless (fboundp 'c-real-parse-state)
1913 (fset 'c-real-parse-state (symbol-function 'c-parse-state)))
1914(cc-bytecomp-defun c-real-parse-state)
1915(defun c-debug-parse-state ()
1916 (let ((res1 (c-real-parse-state)) res2)
1917 (let ((c-state-cache nil))
1918 (setq res2 (c-real-parse-state)))
1919 (unless (equal res1 res2)
1920 (error "c-parse-state inconsistency: using cache: %s, from scratch: %s"
1921 res1 res2))
1922 res1))
1923(defun c-toggle-parse-state-debug (&optional arg)
1924 (interactive "P")
1925 (setq c-debug-parse-state (c-calculate-state arg c-debug-parse-state))
1926 (fset 'c-parse-state (symbol-function (if c-debug-parse-state
1927 'c-debug-parse-state
1928 'c-real-parse-state)))
1929 (c-keep-region-active))
1930
d9e94c22
MS
1931(defun c-whack-state-before (bufpos paren-state)
1932 ;; Whack off any state information from PAREN-STATE which lies
1933 ;; before BUFPOS. Not destructive on PAREN-STATE.
1934 ;;
1935 ;; This function does not do any hidden buffer changes.
1936 (let* ((newstate (list nil))
1937 (ptr newstate)
1938 car)
1939 (while paren-state
1940 (setq car (car paren-state)
1941 paren-state (cdr paren-state))
1942 (if (< (if (consp car) (car car) car) bufpos)
1943 (setq paren-state nil)
1944 (setcdr ptr (list car))
1945 (setq ptr (cdr ptr))))
1946 (cdr newstate)))
1947
1948(defun c-whack-state-after (bufpos paren-state)
1949 ;; Whack off any state information from PAREN-STATE which lies at or
1950 ;; after BUFPOS. Not destructive on PAREN-STATE.
1951 ;;
1952 ;; This function does not do any hidden buffer changes.
1953 (catch 'done
1954 (while paren-state
1955 (let ((car (car paren-state)))
1956 (if (consp car)
1957 ;; just check the car, because in a balanced brace
1958 ;; expression, it must be impossible for the corresponding
1959 ;; close brace to be before point, but the open brace to
1960 ;; be after.
1961 (if (<= bufpos (car car))
1962 nil ; whack it off
1963 (if (< bufpos (cdr car))
1964 ;; its possible that the open brace is before
1965 ;; bufpos, but the close brace is after. In that
1966 ;; case, convert this to a non-cons element. The
1967 ;; rest of the state is before bufpos, so we're
1968 ;; done.
1969 (throw 'done (cons (car car) (cdr paren-state)))
1970 ;; we know that both the open and close braces are
1971 ;; before bufpos, so we also know that everything else
1972 ;; on state is before bufpos.
1973 (throw 'done paren-state)))
1974 (if (<= bufpos car)
1975 nil ; whack it off
1976 ;; it's before bufpos, so everything else should too.
1977 (throw 'done paren-state)))
1978 (setq paren-state (cdr paren-state)))
1979 nil)))
1980
1981(defun c-most-enclosing-brace (paren-state &optional bufpos)
1982 ;; Return the bufpos of the innermost enclosing open paren before
1983 ;; bufpos that hasn't been narrowed out, or nil if none was found.
1984 ;;
1985 ;; This function does not do any hidden buffer changes.
1986 (let (enclosingp)
1987 (or bufpos (setq bufpos 134217727))
1988 (while paren-state
1989 (setq enclosingp (car paren-state)
1990 paren-state (cdr paren-state))
1991 (if (or (consp enclosingp)
1992 (>= enclosingp bufpos))
1993 (setq enclosingp nil)
1994 (if (< enclosingp (point-min))
1995 (setq enclosingp nil))
1996 (setq paren-state nil)))
1997 enclosingp))
1998
1999(defun c-least-enclosing-brace (paren-state &optional bufpos)
2000 ;; Return the bufpos of the outermost enclosing open paren before
2001 ;; bufpos that hasn't been narrowed out, or nil if none was found.
2002 ;;
2003 ;; This function does not do any hidden buffer changes.
2004 (let (pos elem)
2005 (or bufpos (setq bufpos 134217727))
2006 (while paren-state
2007 (setq elem (car paren-state)
2008 paren-state (cdr paren-state))
2009 (unless (or (consp elem)
2010 (>= elem bufpos))
2011 (if (>= elem (point-min))
2012 (setq pos elem))))
2013 pos))
2014
2015(defun c-safe-position (bufpos paren-state)
2016 ;; Return the closest known safe position higher up than BUFPOS, or
2017 ;; nil if PAREN-STATE doesn't contain one. Return nil if BUFPOS is
2018 ;; nil, which is useful to find the closest limit before a given
2019 ;; limit that might be nil.
2020 ;;
2021 ;; This function does not do any hidden buffer changes.
2022 (when bufpos
2023 (let (elem)
2024 (catch 'done
2025 (while paren-state
2026 (setq elem (car paren-state))
2027 (if (consp elem)
2028 (cond ((< (cdr elem) bufpos)
2029 (throw 'done (cdr elem)))
2030 ((< (car elem) bufpos)
2031 ;; See below.
2032 (throw 'done (min (1+ (car elem)) bufpos))))
2033 (if (< elem bufpos)
2034 ;; elem is the position at and not after the opening paren, so
2035 ;; we can go forward one more step unless it's equal to
2036 ;; bufpos. This is useful in some cases avoid an extra paren
2037 ;; level between the safe position and bufpos.
2038 (throw 'done (min (1+ elem) bufpos))))
2039 (setq paren-state (cdr paren-state)))))))
2040
2041(defun c-beginning-of-syntax ()
2042 ;; This is used for `font-lock-beginning-of-syntax-function'. It
2043 ;; goes to the closest previous point that is known to be outside
2044 ;; any string literal or comment. `c-state-cache' is used if it has
2045 ;; a position in the vicinity.
2046 (let* ((paren-state c-state-cache)
2047 elem
2048
2049 (pos (catch 'done
2050 ;; Note: Similar code in `c-safe-position'. The
2051 ;; difference is that we accept a safe position at
2052 ;; the point and don't bother to go forward past open
2053 ;; parens.
2054 (while paren-state
2055 (setq elem (car paren-state))
2056 (if (consp elem)
2057 (cond ((<= (cdr elem) (point))
2058 (throw 'done (cdr elem)))
2059 ((<= (car elem) (point))
2060 (throw 'done (car elem))))
2061 (if (<= elem (point))
2062 (throw 'done elem)))
2063 (setq paren-state (cdr paren-state)))
2064 (point-min))))
2065
2066 (if (> pos (- (point) 4000))
2067 (goto-char pos)
2068 ;; The position is far back. Try `c-beginning-of-defun-1'
2069 ;; (although we can't be entirely sure it will go to a position
2070 ;; outside a comment or string in current emacsen). FIXME:
2071 ;; Consult `syntax-ppss' here.
2072 (c-beginning-of-defun-1)
2073 (if (< (point) pos)
2074 (goto-char pos)))))
2075
2076\f
2077;; Tools for scanning identifiers and other tokens.
2078
2079(defun c-on-identifier ()
2080 "Return non-nil if the point is on or directly after an identifier.
2081Keywords are recognized and not considered identifiers. If an
2082identifier is detected, the returned value is its starting position.
2083If an identifier both starts and stops at the point \(can only happen
2084in Pike) then the point for the preceding one is returned.
2085
2086This function does not do any hidden buffer changes."
2087
2088 (save-excursion
2089 (if (zerop (skip-syntax-backward "w_"))
2090
2091 (when (c-major-mode-is 'pike-mode)
2092 ;; Handle the `<operator> syntax in Pike.
2093 (let ((pos (point)))
2094 (skip-chars-backward "!%&*+\\-/<=>^|~[]()")
2095 (and (if (< (skip-chars-backward "`") 0)
2096 t
2097 (goto-char pos)
2098 (eq (char-after) ?\`))
2099 (looking-at c-symbol-key)
2100 (>= (match-end 0) pos)
2101 (point))))
2102
2103 (and (not (looking-at c-keywords-regexp))
2104 (point)))))
2105
2106(defsubst c-simple-skip-symbol-backward ()
2107 ;; If the point is at the end of a symbol then skip backward to the
2108 ;; beginning of it. Don't move otherwise. Return non-nil if point
2109 ;; moved.
2110 (or (< (skip-syntax-backward "w_") 0)
2111 (and (c-major-mode-is 'pike-mode)
2112 ;; Handle the `<operator> syntax in Pike.
2113 (let ((pos (point)))
2114 (if (and (< (skip-chars-backward "!%&*+\\-/<=>^|~[]()") 0)
2115 (< (skip-chars-backward "`") 0)
2116 (looking-at c-symbol-key)
2117 (>= (match-end 0) pos))
2118 t
2119 (goto-char pos)
2120 nil)))))
2121
2122(defsubst c-beginning-of-current-token (&optional back-limit)
2123 ;; Move to the beginning of the current token. Do not move if not
2124 ;; in the middle of one. BACK-LIMIT may be used to bound the
2125 ;; backward search; if given it's assumed to be at the boundary
2126 ;; between two tokens.
2127 (if (looking-at "\\w\\|\\s_")
2128 (skip-syntax-backward "w_" back-limit)
2129 (let ((start (point)))
2130 (when (< (skip-syntax-backward ".()" back-limit) 0)
2131 (while (let ((pos (or (and (looking-at c-nonsymbol-token-regexp)
2132 (match-end 0))
2133 ;; `c-nonsymbol-token-regexp' should always match
2134 ;; since we've skipped backward over punctuator
2135 ;; or paren syntax, but consume one char in case
2136 ;; it doesn't so that we don't leave point before
2137 ;; some earlier incorrect token.
2138 (1+ (point)))))
2139 (if (<= pos start)
2140 (goto-char pos))
2141 (< pos start)))))))
2142
ff959bab 2143(defun c-end-of-current-token (&optional back-limit)
d9e94c22
MS
2144 ;; Move to the end of the current token. Do not move if not in the
2145 ;; middle of one. BACK-LIMIT may be used to bound the backward
2146 ;; search; if given it's assumed to be at the boundary between two
ff959bab 2147 ;; tokens. Return non-nil if the point is moved, nil otherwise.
d9e94c22
MS
2148 (let ((start (point)))
2149 (cond ((< (skip-syntax-backward "w_" (1- start)) 0)
2150 (skip-syntax-forward "w_"))
2151 ((< (skip-syntax-backward ".()" back-limit) 0)
2152 (while (progn
2153 (if (looking-at c-nonsymbol-token-regexp)
2154 (goto-char (match-end 0))
2155 ;; `c-nonsymbol-token-regexp' should always match since
2156 ;; we've skipped backward over punctuator or paren
2157 ;; syntax, but move forward in case it doesn't so that
2158 ;; we don't leave point earlier than we started with.
2159 (forward-char))
ff959bab
MS
2160 (< (point) start)))))
2161 (> (point) start)))
d9e94c22
MS
2162
2163(defconst c-jump-syntax-balanced
2164 (if (memq 'gen-string-delim c-emacs-features)
2165 "\\w\\|\\s_\\|\\s\(\\|\\s\)\\|\\s\"\\|\\s|"
2166 "\\w\\|\\s_\\|\\s\(\\|\\s\)\\|\\s\""))
2167
2168(defconst c-jump-syntax-unbalanced
2169 (if (memq 'gen-string-delim c-emacs-features)
2170 "\\w\\|\\s_\\|\\s\"\\|\\s|"
2171 "\\w\\|\\s_\\|\\s\""))
2172
2173(defun c-forward-token-2 (&optional count balanced limit)
2174 "Move forward by tokens.
2175A token is defined as all symbols and identifiers which aren't
2176syntactic whitespace \(note that multicharacter tokens like \"==\" are
2177treated properly). Point is always either left at the beginning of a
2178token or not moved at all. COUNT specifies the number of tokens to
2179move; a negative COUNT moves in the opposite direction. A COUNT of 0
2180moves to the next token beginning only if not already at one. If
2181BALANCED is true, move over balanced parens, otherwise move into them.
2182Also, if BALANCED is true, never move out of an enclosing paren.
2183
2184LIMIT sets the limit for the movement and defaults to the point limit.
2185The case when LIMIT is set in the middle of a token, comment or macro
2186is handled correctly, i.e. the point won't be left there.
2187
2188Return the number of tokens left to move \(positive or negative). If
2189BALANCED is true, a move over a balanced paren counts as one. Note
2190that if COUNT is 0 and no appropriate token beginning is found, 1 will
2191be returned. Thus, a return value of 0 guarantees that point is at
2192the requested position and a return value less \(without signs) than
2193COUNT guarantees that point is at the beginning of some token."
2194
2195 (or count (setq count 1))
2196 (if (< count 0)
2197 (- (c-backward-token-2 (- count) balanced limit))
2198
2199 (let ((jump-syntax (if balanced
2200 c-jump-syntax-balanced
2201 c-jump-syntax-unbalanced))
2202 (last (point))
2203 (prev (point)))
2204
2205 (if (zerop count)
2206 ;; If count is zero we should jump if in the middle of a token.
2207 (c-end-of-current-token))
2208
2209 (save-restriction
2210 (if limit (narrow-to-region (point-min) limit))
2211 (if (/= (point)
2212 (progn (c-forward-syntactic-ws) (point)))
2213 ;; Skip whitespace. Count this as a move if we did in
2214 ;; fact move.
2215 (setq count (max (1- count) 0)))
2216
2217 (if (eobp)
2218 ;; Moved out of bounds. Make sure the returned count isn't zero.
2219 (progn
2220 (if (zerop count) (setq count 1))
2221 (goto-char last))
2222
2223 ;; Use `condition-case' to avoid having the limit tests
2224 ;; inside the loop.
2225 (condition-case nil
2226 (while (and
2227 (> count 0)
2228 (progn
2229 (setq last (point))
2230 (cond ((looking-at jump-syntax)
2231 (goto-char (scan-sexps (point) 1))
2232 t)
2233 ((looking-at c-nonsymbol-token-regexp)
2234 (goto-char (match-end 0))
2235 t)
2236 ;; `c-nonsymbol-token-regexp' above should always
2237 ;; match if there are correct tokens. Try to
2238 ;; widen to see if the limit was set in the
2239 ;; middle of one, else fall back to treating
2240 ;; the offending thing as a one character token.
2241 ((and limit
2242 (save-restriction
2243 (widen)
2244 (looking-at c-nonsymbol-token-regexp)))
2245 nil)
2246 (t
2247 (forward-char)
2248 t))))
2249 (c-forward-syntactic-ws)
2250 (setq prev last
2251 count (1- count)))
2252 (error (goto-char last)))
2253
2254 (when (eobp)
2255 (goto-char prev)
2256 (setq count (1+ count)))))
2257
2258 count)))
2259
2260(defun c-backward-token-2 (&optional count balanced limit)
2261 "Move backward by tokens.
2262See `c-forward-token-2' for details."
2263
2264 (or count (setq count 1))
2265 (if (< count 0)
2266 (- (c-forward-token-2 (- count) balanced limit))
2267
2268 (or limit (setq limit (point-min)))
2269 (let ((jump-syntax (if balanced
2270 c-jump-syntax-balanced
2271 c-jump-syntax-unbalanced))
2272 (last (point)))
2273
2274 (if (zerop count)
2275 ;; The count is zero so try to skip to the beginning of the
2276 ;; current token.
2277 (if (> (point)
2278 (progn (c-beginning-of-current-token) (point)))
2279 (if (< (point) limit)
2280 ;; The limit is inside the same token, so return 1.
2281 (setq count 1))
2282
2283 ;; We're not in the middle of a token. If there's
2284 ;; whitespace after the point then we must move backward,
2285 ;; so set count to 1 in that case.
2286 (and (looking-at c-syntactic-ws-start)
2287 ;; If we're looking at a '#' that might start a cpp
2288 ;; directive then we have to do a more elaborate check.
2289 (or (/= (char-after) ?#)
2290 (not c-opt-cpp-prefix)
2291 (save-excursion
2292 (and (= (point)
2293 (progn (beginning-of-line)
2294 (looking-at "[ \t]*")
2295 (match-end 0)))
2296 (or (bobp)
2297 (progn (backward-char)
2298 (not (eq (char-before) ?\\)))))))
2299 (setq count 1))))
2300
2301 ;; Use `condition-case' to avoid having to check for buffer
2302 ;; limits in `backward-char', `scan-sexps' and `goto-char' below.
2303 (condition-case nil
2304 (while (and
2305 (> count 0)
2306 (progn
2307 (c-backward-syntactic-ws)
2308 (backward-char)
2309 (if (looking-at jump-syntax)
2310 (goto-char (scan-sexps (1+ (point)) -1))
2311 ;; This can be very inefficient if there's a long
2312 ;; sequence of operator tokens without any separation.
2313 ;; That doesn't happen in practice, anyway.
2314 (c-beginning-of-current-token))
2315 (>= (point) limit)))
2316 (setq last (point)
2317 count (1- count)))
2318 (error (goto-char last)))
2319
2320 (if (< (point) limit)
2321 (goto-char last))
2322
2323 count)))
2324
2325(defun c-forward-token-1 (&optional count balanced limit)
2326 "Like `c-forward-token-2' but doesn't treat multicharacter operator
2327tokens like \"==\" as single tokens, i.e. all sequences of symbol
2328characters are jumped over character by character. This function is
2329for compatibility only; it's only a wrapper over `c-forward-token-2'."
2330 (let ((c-nonsymbol-token-regexp "\\s.\\|\\s\(\\|\\s\)"))
2331 (c-forward-token-2 count balanced limit)))
2332
2333(defun c-backward-token-1 (&optional count balanced limit)
2334 "Like `c-backward-token-2' but doesn't treat multicharacter operator
2335tokens like \"==\" as single tokens, i.e. all sequences of symbol
2336characters are jumped over character by character. This function is
2337for compatibility only; it's only a wrapper over `c-backward-token-2'."
2338 (let ((c-nonsymbol-token-regexp "\\s.\\|\\s\(\\|\\s\)"))
2339 (c-backward-token-2 count balanced limit)))
2340
2341\f
2342;; Tools for doing searches restricted to syntactically relevant text.
2343
2344(defun c-syntactic-re-search-forward (regexp &optional bound noerror
2345 paren-level not-inside-token
2346 lookbehind-submatch)
2347 "Like `re-search-forward', but only report matches that are found
2348in syntactically significant text. I.e. matches in comments, macros
2349or string literals are ignored. The start point is assumed to be
2350outside any comment, macro or string literal, or else the content of
2351that region is taken as syntactically significant text.
2352
2353If PAREN-LEVEL is non-nil, an additional restriction is added to
2354ignore matches in nested paren sexps, and the search will also not go
2355outside the current paren sexp.
2356
2357If NOT-INSIDE-TOKEN is non-nil, matches in the middle of tokens are
2358ignored. Things like multicharacter operators and special symbols
2359\(e.g. \"`()\" in Pike) are handled but currently not floating point
2360constants.
2361
2362If LOOKBEHIND-SUBMATCH is non-nil, it's taken as a number of a
2363subexpression in REGEXP. The end of that submatch is used as the
2364position to check for syntactic significance. If LOOKBEHIND-SUBMATCH
2365isn't used or if that subexpression didn't match then the start
2366position of the whole match is used instead. The \"look behind\"
2367subexpression is never tested before the starting position, so it
2368might be a good idea to include \\=\\= as a match alternative in it.
2369
2370Optimization note: Matches might be missed if the \"look behind\"
2371subexpression should match the end of nonwhite syntactic whitespace,
2372i.e. the end of comments or cpp directives. This since the function
2373skips over such things before resuming the search. It's also not safe
2374to assume that the \"look behind\" subexpression never can match
2375syntactic whitespace."
2376
2377 (or bound (setq bound (point-max)))
2378 (if paren-level (setq paren-level -1))
2379
2380 ;;(message "c-syntactic-re-search-forward %s %s %S" (point) bound regexp)
2381
2382 (let ((start (point))
2383 (pos (point))
2384 (last-token-end-pos (point-min))
2385 match-pos found state check-pos check-state tmp)
2386
2387 (condition-case err
2388 (while
2389 (and
2390 (re-search-forward regexp bound noerror)
2391
2392 (progn
2393 (setq match-pos (point)
2394 state (parse-partial-sexp
2395 pos (match-beginning 0) paren-level nil state)
2396 pos (point))
2397 (if (setq check-pos (and lookbehind-submatch
2398 (match-end lookbehind-submatch)))
2399 (setq check-state (parse-partial-sexp
2400 pos check-pos paren-level nil state))
2401 (setq check-pos pos
2402 check-state state))
2403
2404 ;; If we got a look behind subexpression and get an
2405 ;; insignificant match in something that isn't
2406 ;; syntactic whitespace (i.e. strings or in nested
2407 ;; parentheses), then we can never skip more than a
2408 ;; single character from the match position before
2409 ;; continuing the search. That since the look behind
2410 ;; subexpression might match the end of the
2411 ;; insignificant region.
2412
2413 (cond
2414 ((setq tmp (elt check-state 3))
2415 ;; Match inside a string.
2416 (if (or lookbehind-submatch
2417 (not (integerp tmp)))
2418 (goto-char (min (1+ pos) bound))
2419 ;; Skip to the end of the string before continuing.
2420 (let ((ender (make-string 1 tmp)) (continue t))
2421 (while (if (search-forward ender bound noerror)
2422 (progn
2423 (setq state (parse-partial-sexp
2424 pos (point) nil nil state)
2425 pos (point))
2426 (elt state 3))
2427 (setq continue nil)))
2428 continue)))
2429
2430 ((elt check-state 7)
2431 ;; Match inside a line comment. Skip to eol. Use
2432 ;; `re-search-forward' instead of `skip-chars-forward' to get
2433 ;; the right bound behavior.
2434 (re-search-forward "[\n\r]" bound noerror))
2435
2436 ((elt check-state 4)
2437 ;; Match inside a block comment. Skip to the '*/'.
2438 (search-forward "*/" bound noerror))
2439
2440 ((and (not (elt check-state 5))
2441 (eq (char-before check-pos) ?/)
2442 (memq (char-after check-pos) '(?/ ?*)))
2443 ;; Match in the middle of the opener of a block or line
2444 ;; comment.
2445 (if (= (char-after check-pos) ?/)
2446 (re-search-forward "[\n\r]" bound noerror)
2447 (search-forward "*/" bound noerror)))
2448
2449 ((and not-inside-token
2450 (or (< check-pos last-token-end-pos)
2451 (< check-pos
2452 (save-excursion
2453 (goto-char check-pos)
2454 (c-end-of-current-token last-token-end-pos)
2455 (setq last-token-end-pos (point))))))
2456 ;; Match inside a token.
2457 (cond ((<= (point) bound)
2458 (goto-char (min (1+ pos) bound))
2459 t)
2460 (noerror nil)
2461 (t (signal 'search-failed "end of token"))))
2462
2463 ((save-excursion
2464 (save-match-data
2465 (c-beginning-of-macro start)))
2466 ;; Match inside a macro. Skip to the end of it.
2467 (c-end-of-macro)
2468 (cond ((<= (point) bound) t)
2469 (noerror nil)
2470 (t (signal 'search-failed "end of macro"))))
2471
2472 ((and paren-level
2473 (/= (setq tmp (car check-state)) 0))
2474 (if (> tmp 0)
2475 ;; Match inside a nested paren sexp.
2476 (if lookbehind-submatch
2477 (goto-char (min (1+ pos) bound))
2478 ;; Skip out of the paren quickly.
2479 (setq state (parse-partial-sexp pos bound 0 nil state)
2480 pos (point)))
2481 ;; Have exited the current paren sexp. The
2482 ;; `parse-partial-sexp' above has left us just after the
2483 ;; closing paren in this case. Just make
2484 ;; `re-search-forward' above fail in the appropriate way;
2485 ;; we'll adjust the leave off point below if necessary.
2486 (setq bound (point))))
2487
2488 (t
2489 ;; A real match.
2490 (setq found t)
2491 nil)))))
2492
2493 (error
2494 (goto-char start)
2495 (signal (car err) (cdr err))))
2496
2497 ;;(message "c-syntactic-re-search-forward done %s" (or match-pos (point)))
2498
2499 (if found
2500 (progn
2501 (goto-char match-pos)
2502 match-pos)
2503
2504 ;; Search failed. Set point as appropriate.
2505 (cond ((eq noerror t)
2506 (goto-char start))
2507 (paren-level
2508 (if (eq (car (parse-partial-sexp pos bound -1 nil state)) -1)
2509 (backward-char)))
2510 (t
2511 (goto-char bound)))
2512 nil)))
2513
2514(defun c-syntactic-skip-backward (skip-chars &optional limit)
2515 "Like `skip-chars-backward' but only look at syntactically relevant chars,
2516i.e. don't stop at positions inside syntactic whitespace or string
2517literals. Preprocessor directives are also ignored, with the exception
2518of the one that the point starts within, if any. If LIMIT is given,
2519it's assumed to be at a syntactically relevant position.
2520
2521This function does not do any hidden buffer changes."
2522
2523 (let ((start (point))
2524 ;; A list of syntactically relevant positions in descending
2525 ;; order. It's used to avoid scanning repeatedly over
2526 ;; potentially large regions with `parse-partial-sexp' to verify
2527 ;; each position.
2528 safe-pos-list
2529 ;; The result from `c-beginning-of-macro' at the start position or the
2530 ;; start position itself if it isn't within a macro. Evaluated on
2531 ;; demand.
2532 start-macro-beg)
2533
2534 (while (progn
2535 (while (and
2536 (< (skip-chars-backward skip-chars limit) 0)
2537
2538 ;; Use `parse-partial-sexp' from a safe position down to
2539 ;; the point to check if it's outside comments and
2540 ;; strings.
2541 (let ((pos (point)) safe-pos state)
2542 ;; Pick a safe position as close to the point as
2543 ;; possible.
2544 ;;
2545 ;; FIXME: Consult `syntax-ppss' here if our
2546 ;; cache doesn't give a good position.
2547 (while (and safe-pos-list
2548 (> (car safe-pos-list) (point)))
2549 (setq safe-pos-list (cdr safe-pos-list)))
2550 (unless (setq safe-pos (car-safe safe-pos-list))
2551 (setq safe-pos (max (or (c-safe-position
2552 (point) (or c-state-cache
2553 (c-parse-state)))
2554 0)
2555 (point-min))
2556 safe-pos-list (list safe-pos)))
2557
2558 (while (progn
2559 (setq state (parse-partial-sexp
2560 safe-pos pos 0))
2561 (< (point) pos))
2562 ;; Cache positions along the way to use if we have to
2563 ;; back up more. Every closing paren on the same
2564 ;; level seems like fairly well spaced positions.
2565 (setq safe-pos (point)
2566 safe-pos-list (cons safe-pos safe-pos-list)))
2567
2568 (cond
2569 ((or (elt state 3) (elt state 4))
2570 ;; Inside string or comment. Continue search at the
2571 ;; beginning of it.
2572 (if (setq pos (nth 8 state))
2573 ;; It's an emacs where `parse-partial-sexp'
2574 ;; supplies the starting position.
2575 (goto-char pos)
2576 (goto-char (car (c-literal-limits safe-pos))))
2577 t)
2578
2579 ((c-beginning-of-macro limit)
2580 ;; Inside a macro.
2581 (if (< (point)
2582 (or start-macro-beg
2583 (setq start-macro-beg
2584 (save-excursion
2585 (goto-char start)
2586 (c-beginning-of-macro limit)
2587 (point)))))
2588 t
2589 ;; It's inside the same macro we started in so it's
2590 ;; a relevant match.
2591 (goto-char pos)
2592 nil))))))
2593
2594 (> (point)
2595 (progn
2596 ;; Skip syntactic ws afterwards so that we don't stop at the
2597 ;; end of a comment if `skip-chars' is something like "^/".
2598 (c-backward-syntactic-ws)
2599 (point)))))
2600
2601 (- (point) start)))
2602
2603\f
2604;; Tools for handling comments and string literals.
2605
2606(defun c-slow-in-literal (&optional lim detect-cpp)
2607 "Return the type of literal point is in, if any.
2608The return value is `c' if in a C-style comment, `c++' if in a C++
2609style comment, `string' if in a string literal, `pound' if DETECT-CPP
2610is non-nil and in a preprocessor line, or nil if somewhere else.
2611Optional LIM is used as the backward limit of the search. If omitted,
2612or nil, `c-beginning-of-defun' is used.
2613
2614The last point calculated is cached if the cache is enabled, i.e. if
2615`c-in-literal-cache' is bound to a two element vector.
2616
2617This function does not do any hidden buffer changes."
2618 (if (and (vectorp c-in-literal-cache)
2619 (= (point) (aref c-in-literal-cache 0)))
2620 (aref c-in-literal-cache 1)
2621 (let ((rtn (save-excursion
2622 (let* ((pos (point))
2623 (lim (or lim (progn
2624 (c-beginning-of-syntax)
2625 (point))))
2626 (state (parse-partial-sexp lim pos)))
2627 (cond
2628 ((elt state 3) 'string)
2629 ((elt state 4) (if (elt state 7) 'c++ 'c))
2630 ((and detect-cpp (c-beginning-of-macro lim)) 'pound)
2631 (t nil))))))
2632 ;; cache this result if the cache is enabled
2633 (if (not c-in-literal-cache)
2634 (setq c-in-literal-cache (vector (point) rtn)))
2635 rtn)))
2636
2637;; XEmacs has a built-in function that should make this much quicker.
2638;; I don't think we even need the cache, which makes our lives more
2639;; complicated anyway. In this case, lim is only used to detect
2640;; cpp directives.
2641;;
2642;; Note that there is a bug in Xemacs's buffer-syntactic-context when used in
2643;; conjunction with syntax-table-properties. The bug is present in, e.g.,
2644;; Xemacs 21.4.4. It manifested itself thus:
2645;;
2646;; Starting with an empty AWK Mode buffer, type
2647;; /regexp/ {<C-j>
2648;; Point gets wrongly left at column 0, rather than being indented to tab-width.
2649;;
2650;; AWK Mode is designed such that when the first / is typed, it gets the
2651;; syntax-table property "string fence". When the second / is typed, BOTH /s
2652;; are given the s-t property "string". However, buffer-syntactic-context
2653;; fails to take account of the change of the s-t property on the opening / to
2654;; "string", and reports that the { is within a string started by the second /.
2655;;
2656;; The workaround for this is for the AWK Mode initialisation to switch the
2657;; defalias for c-in-literal to c-slow-in-literal. This will slow down other
2658;; cc-modes in Xemacs whenever an awk-buffer has been initialised.
2659;;
2660;; (Alan Mackenzie, 2003/4/30).
2661
2662(defun c-fast-in-literal (&optional lim detect-cpp)
2663 (let ((context (buffer-syntactic-context)))
2664 (cond
2665 ((eq context 'string) 'string)
2666 ((eq context 'comment) 'c++)
2667 ((eq context 'block-comment) 'c)
2668 ((and detect-cpp (save-excursion (c-beginning-of-macro lim))) 'pound))))
2669
2670(defalias 'c-in-literal
2671 (if (fboundp 'buffer-syntactic-context)
7bfc3fdb 2672 'c-fast-in-literal ; XEmacs
d9e94c22
MS
2673 'c-slow-in-literal)) ; GNU Emacs
2674
2675;; The defalias above isn't enough to shut up the byte compiler.
2676(cc-bytecomp-defun c-in-literal)
2677
2678(defun c-literal-limits (&optional lim near not-in-delimiter)
2679 "Return a cons of the beginning and end positions of the comment or
2680string surrounding point (including both delimiters), or nil if point
2681isn't in one. If LIM is non-nil, it's used as the \"safe\" position
2682to start parsing from. If NEAR is non-nil, then the limits of any
2683literal next to point is returned. \"Next to\" means there's only
2684spaces and tabs between point and the literal. The search for such a
2685literal is done first in forward direction. If NOT-IN-DELIMITER is
2686non-nil, the case when point is inside a starting delimiter won't be
2687recognized. This only has effect for comments, which have starting
2688delimiters with more than one character.
2689
2690This function does not do any hidden buffer changes."
2691
2692 (save-excursion
2693 (let* ((pos (point))
2694 (lim (or lim (progn
2695 (c-beginning-of-syntax)
2696 (point))))
2697 (state (parse-partial-sexp lim pos)))
2698
2699 (cond ((elt state 3)
2700 ;; String. Search backward for the start.
2701 (while (elt state 3)
2702 (search-backward (make-string 1 (elt state 3)))
2703 (setq state (parse-partial-sexp lim (point))))
2704 (cons (point) (or (c-safe (c-forward-sexp 1) (point))
2705 (point-max))))
2706
2707 ((elt state 7)
2708 ;; Line comment. Search from bol for the comment starter.
2709 (beginning-of-line)
2710 (setq state (parse-partial-sexp lim (point))
2711 lim (point))
2712 (while (not (elt state 7))
2713 (search-forward "//") ; Should never fail.
2714 (setq state (parse-partial-sexp
2715 lim (point) nil nil state)
2716 lim (point)))
2717 (backward-char 2)
2718 (cons (point) (progn (c-forward-single-comment) (point))))
2719
2720 ((elt state 4)
2721 ;; Block comment. Search backward for the comment starter.
2722 (while (elt state 4)
2723 (search-backward "/*") ; Should never fail.
2724 (setq state (parse-partial-sexp lim (point))))
2725 (cons (point) (progn (c-forward-single-comment) (point))))
2726
2727 ((and (not not-in-delimiter)
2728 (not (elt state 5))
2729 (eq (char-before) ?/)
2730 (looking-at "[/*]"))
2731 ;; We're standing in a comment starter.
2732 (backward-char 1)
2733 (cons (point) (progn (c-forward-single-comment) (point))))
2734
2735 (near
2736 (goto-char pos)
2737
2738 ;; Search forward for a literal.
2739 (skip-chars-forward " \t")
2740
2741 (cond
2742 ((looking-at c-string-limit-regexp) ; String.
2743 (cons (point) (or (c-safe (c-forward-sexp 1) (point))
2744 (point-max))))
2745
2746 ((looking-at c-comment-start-regexp) ; Line or block comment.
2747 (cons (point) (progn (c-forward-single-comment) (point))))
2748
2749 (t
2750 ;; Search backward.
2751 (skip-chars-backward " \t")
2752
2753 (let ((end (point)) beg)
2754 (cond
2755 ((save-excursion
2756 (< (skip-syntax-backward c-string-syntax) 0)) ; String.
2757 (setq beg (c-safe (c-backward-sexp 1) (point))))
2758
2759 ((and (c-safe (forward-char -2) t)
2760 (looking-at "*/"))
2761 ;; Block comment. Due to the nature of line
2762 ;; comments, they will always be covered by the
2763 ;; normal case above.
2764 (goto-char end)
2765 (c-backward-single-comment)
2766 ;; If LIM is bogus, beg will be bogus.
2767 (setq beg (point))))
2768
2769 (if beg (cons beg end))))))
2770 ))))
2771
2772(defun c-literal-limits-fast (&optional lim near not-in-delimiter)
2773 ;; Like c-literal-limits, but for emacsen whose `parse-partial-sexp'
2774 ;; returns the pos of the comment start.
2775
2776 "Return a cons of the beginning and end positions of the comment or
2777string surrounding point (including both delimiters), or nil if point
2778isn't in one. If LIM is non-nil, it's used as the \"safe\" position
2779to start parsing from. If NEAR is non-nil, then the limits of any
2780literal next to point is returned. \"Next to\" means there's only
2781spaces and tabs between point and the literal. The search for such a
2782literal is done first in forward direction. If NOT-IN-DELIMITER is
2783non-nil, the case when point is inside a starting delimiter won't be
2784recognized. This only has effect for comments, which have starting
2785delimiters with more than one character.
2786
2787This function does not do any hidden buffer changes."
2788
2789 (save-excursion
2790 (let* ((pos (point))
2791 (lim (or lim (progn
2792 (c-beginning-of-syntax)
2793 (point))))
2794 (state (parse-partial-sexp lim pos)))
2795
2796 (cond ((elt state 3) ; String.
2797 (goto-char (elt state 8))
2798 (cons (point) (or (c-safe (c-forward-sexp 1) (point))
2799 (point-max))))
2800
2801 ((elt state 4) ; Comment.
2802 (goto-char (elt state 8))
2803 (cons (point) (progn (c-forward-single-comment) (point))))
2804
2805 ((and (not not-in-delimiter)
2806 (not (elt state 5))
2807 (eq (char-before) ?/)
2808 (looking-at "[/*]"))
2809 ;; We're standing in a comment starter.
2810 (backward-char 1)
2811 (cons (point) (progn (c-forward-single-comment) (point))))
2812
2813 (near
2814 (goto-char pos)
2815
2816 ;; Search forward for a literal.
2817 (skip-chars-forward " \t")
2818
2819 (cond
2820 ((looking-at c-string-limit-regexp) ; String.
2821 (cons (point) (or (c-safe (c-forward-sexp 1) (point))
2822 (point-max))))
2823
2824 ((looking-at c-comment-start-regexp) ; Line or block comment.
2825 (cons (point) (progn (c-forward-single-comment) (point))))
2826
2827 (t
2828 ;; Search backward.
2829 (skip-chars-backward " \t")
2830
2831 (let ((end (point)) beg)
2832 (cond
2833 ((save-excursion
2834 (< (skip-syntax-backward c-string-syntax) 0)) ; String.
2835 (setq beg (c-safe (c-backward-sexp 1) (point))))
2836
2837 ((and (c-safe (forward-char -2) t)
2838 (looking-at "*/"))
2839 ;; Block comment. Due to the nature of line
2840 ;; comments, they will always be covered by the
2841 ;; normal case above.
2842 (goto-char end)
2843 (c-backward-single-comment)
2844 ;; If LIM is bogus, beg will be bogus.
2845 (setq beg (point))))
2846
2847 (if beg (cons beg end))))))
2848 ))))
2849
2850(if (memq 'pps-extended-state c-emacs-features)
2851 (defalias 'c-literal-limits 'c-literal-limits-fast))
2852
2853(defun c-collect-line-comments (range)
2854 "If the argument is a cons of two buffer positions (such as returned by
2855`c-literal-limits'), and that range contains a C++ style line comment,
2856then an extended range is returned that contains all adjacent line
2857comments (i.e. all comments that starts in the same column with no
2858empty lines or non-whitespace characters between them). Otherwise the
2859argument is returned.
2860
2861This function does not do any hidden buffer changes."
2862 (save-excursion
2863 (condition-case nil
2864 (if (and (consp range) (progn
2865 (goto-char (car range))
2866 (looking-at "//")))
2867 (let ((col (current-column))
2868 (beg (point))
2869 (bopl (c-point 'bopl))
2870 (end (cdr range)))
2871 ;; Got to take care in the backward direction to handle
2872 ;; comments which are preceded by code.
2873 (while (and (c-backward-single-comment)
2874 (>= (point) bopl)
2875 (looking-at "//")
2876 (= col (current-column)))
2877 (setq beg (point)
2878 bopl (c-point 'bopl)))
2879 (goto-char end)
2880 (while (and (progn (skip-chars-forward " \t")
2881 (looking-at "//"))
2882 (= col (current-column))
2883 (prog1 (zerop (forward-line 1))
2884 (setq end (point)))))
2885 (cons beg end))
2886 range)
2887 (error range))))
2888
2889(defun c-literal-type (range)
2890 "Convenience function that given the result of `c-literal-limits',
2891returns nil or the type of literal that the range surrounds. It's
2892much faster than using `c-in-literal' and is intended to be used when
2893you need both the type of a literal and its limits.
2894
2895This function does not do any hidden buffer changes."
2896 (if (consp range)
2897 (save-excursion
2898 (goto-char (car range))
2899 (cond ((looking-at c-string-limit-regexp) 'string)
2900 ((or (looking-at "//") ; c++ line comment
2901 (and (looking-at "\\s<") ; comment starter
2902 (looking-at "#"))) ; awk comment.
2903 'c++)
2904 (t 'c))) ; Assuming the range is valid.
2905 range))
2906
2907\f
2908;; `c-find-decl-spots' and accompanying stuff.
2909
2910;; Variables used in `c-find-decl-spots' to cache the search done for
2911;; the first declaration in the last call. When that function starts,
2912;; it needs to back up over syntactic whitespace to look at the last
2913;; token before the region being searched. That can sometimes cause
2914;; moves back and forth over a quite large region of comments and
2915;; macros, which would be repeated for each changed character when
2916;; we're called during fontification, since font-lock refontifies the
2917;; current line for each change. Thus it's worthwhile to cache the
2918;; first match.
2919;;
2920;; `c-find-decl-syntactic-pos' is a syntactically relevant position in
2921;; the syntactic whitespace less or equal to some start position.
2922;; There's no cached value if it's nil.
2923;;
2924;; `c-find-decl-match-pos' is the match position if
2925;; `c-find-decl-prefix-search' matched before the syntactic whitespace
2926;; at `c-find-decl-syntactic-pos', or nil if there's no such match.
2927(defvar c-find-decl-syntactic-pos nil)
2928(make-variable-buffer-local 'c-find-decl-syntactic-pos)
2929(defvar c-find-decl-match-pos nil)
2930(make-variable-buffer-local 'c-find-decl-match-pos)
2931
2932(defsubst c-invalidate-find-decl-cache (change-min-pos)
2933 (and c-find-decl-syntactic-pos
2934 (< change-min-pos c-find-decl-syntactic-pos)
2935 (setq c-find-decl-syntactic-pos nil)))
2936
2937; (defface c-debug-decl-spot-face
2938; '((t (:background "Turquoise")))
2939; "Debug face to mark the spots where `c-find-decl-spots' stopped.")
2940; (defface c-debug-decl-sws-face
2941; '((t (:background "Khaki")))
2942; "Debug face to mark the syntactic whitespace between the declaration
2943; spots and the preceding token end.")
2944
2945(defmacro c-debug-put-decl-spot-faces (match-pos decl-pos)
2946 (when (facep 'c-debug-decl-spot-face)
2947 `(let ((match-pos ,match-pos) (decl-pos ,decl-pos))
2948 (c-debug-add-face (max match-pos (point-min)) decl-pos
2949 'c-debug-decl-sws-face)
2950 (c-debug-add-face decl-pos (min (1+ decl-pos) (point-max))
2951 'c-debug-decl-spot-face))))
2952(defmacro c-debug-remove-decl-spot-faces (beg end)
2953 (when (facep 'c-debug-decl-spot-face)
2954 `(progn
2955 (c-debug-remove-face ,beg ,end 'c-debug-decl-spot-face)
2956 (c-debug-remove-face ,beg ,end 'c-debug-decl-sws-face))))
2957
2958(defmacro c-find-decl-prefix-search ()
2959 ;; Macro used inside `c-find-decl-spots'. It ought to be a defun,
2960 ;; but it contains lots of free variables that refer to things
2961 ;; inside `c-find-decl-spots'. The point is left at `cfd-match-pos'
2962 ;; if there is a match, otherwise at `cfd-limit'.
2963
2964 '(progn
2965 ;; Find the next property match position if we haven't got one already.
2966 (unless cfd-prop-match
2967 (save-excursion
2968 (while (progn
2969 (goto-char (next-single-property-change
2970 (point) 'c-type nil cfd-limit))
2971 (and (< (point) cfd-limit)
2972 (not (eq (c-get-char-property (1- (point)) 'c-type)
2973 'c-decl-end)))))
2974 (setq cfd-prop-match (point))))
2975
2976 ;; Find the next `c-decl-prefix-re' match if we haven't got one already.
2977 (unless cfd-re-match
2978 (while (and (setq cfd-re-match
2979 (re-search-forward c-decl-prefix-re cfd-limit 'move))
2980 (c-got-face-at (1- (setq cfd-re-match (match-end 1)))
2981 c-literal-faces))
2982 ;; Search again if the match is within a comment or a string literal.
2983 (while (progn
2984 (goto-char (next-single-property-change
2985 cfd-re-match 'face nil cfd-limit))
2986 (and (< (point) cfd-limit)
2987 (c-got-face-at (point) c-literal-faces)))
2988 (setq cfd-re-match (point))))
2989 (unless cfd-re-match
2990 (setq cfd-re-match cfd-limit)))
2991
2992 ;; Choose whichever match is closer to the start.
2993 (if (< cfd-re-match cfd-prop-match)
2994 (setq cfd-match-pos cfd-re-match
2995 cfd-re-match nil)
2996 (setq cfd-match-pos cfd-prop-match
2997 cfd-prop-match nil))
2998
2999 (goto-char cfd-match-pos)
3000
3001 (when (< cfd-match-pos cfd-limit)
3002 ;; Skip forward past comments only so we don't skip macros.
3003 (c-forward-comments)
3004 ;; Set the position to continue at. We can avoid going over
3005 ;; the comments skipped above a second time, but it's possible
3006 ;; that the comment skipping has taken us past `cfd-prop-match'
3007 ;; since the property might be used inside comments.
3008 (setq cfd-continue-pos (if cfd-prop-match
3009 (min cfd-prop-match (point))
3010 (point))))))
3011
3012(defun c-find-decl-spots (cfd-limit cfd-decl-re cfd-face-checklist cfd-fun)
3013 ;; Call CFD-FUN for each possible spot for a declaration from the
3014 ;; point to CFD-LIMIT. A spot for a declaration is the first token
3015 ;; in the buffer and each token after the ones matched by
3016 ;; `c-decl-prefix-re' and after the occurrences of the `c-type'
3017 ;; property with the value `c-decl-end' (if `c-type-decl-end-used'
3018 ;; is set). Only a spot that match CFD-DECL-RE and whose face is in
3019 ;; the CFD-FACE-CHECKLIST list causes CFD-FUN to be called. The
3020 ;; face check is disabled if CFD-FACE-CHECKLIST is nil.
3021 ;;
3022 ;; If the match is inside a macro then the buffer is narrowed to the
3023 ;; end of it, so that CFD-FUN can investigate the following tokens
3024 ;; without matching something that begins inside a macro and ends
3025 ;; outside it. It's to avoid this work that the CFD-DECL-RE and
3026 ;; CFD-FACE-CHECKLIST checks exist.
3027 ;;
3028 ;; CFD-FUN is called with point at the start of the spot. It's
3029 ;; passed two arguments: The first is the end position of the token
3030 ;; that `c-decl-prefix-re' matched, or 0 for the implicit match at
3031 ;; bob. The second is a flag that is t when the match is inside a
3032 ;; macro.
3033 ;;
3034 ;; It's assumed that comment and strings are fontified in the
3035 ;; searched range.
3036 ;;
3037 ;; This is mainly used in fontification, and so has an elaborate
3038 ;; cache to handle repeated calls from the same start position; see
3039 ;; the variables above.
3040 ;;
3041 ;; All variables in this function begin with `cfd-' to avoid name
3042 ;; collision with the (dynamically bound) variables used in CFD-FUN.
3043
3044 (let ((cfd-buffer-end (point-max))
3045 ;; The last regexp match found by `c-find-decl-prefix-search'.
3046 cfd-re-match
3047 ;; The last `c-decl-end' found by `c-find-decl-prefix-search'.
3048 ;; If searching for the property isn't needed then we disable
3049 ;; it by faking a first match at the limit.
3050 (cfd-prop-match (unless c-type-decl-end-used cfd-limit))
3051 ;; The position of the last match found by
3052 ;; `c-find-decl-prefix-search'. For regexp matches it's the
3053 ;; end of the matched token, for property matches it's the end
3054 ;; of the property. 0 for the implicit match at bob.
3055 ;; `cfd-limit' if there's no match.
3056 (cfd-match-pos cfd-limit)
3057 ;; The position to continue searching at.
3058 cfd-continue-pos
3059 ;; The position of the last "real" token we've stopped at.
3060 ;; This can be greater than `cfd-continue-pos' when we get
3061 ;; hits inside macros or at `c-decl-end' positions inside
3062 ;; comments.
3063 (cfd-token-pos 0)
3064 ;; The end position of the last entered macro.
3065 (cfd-macro-end 0))
3066
3067 ;; Initialize by finding a syntactically relevant start position
3068 ;; before the point, and do the first `c-decl-prefix-re' search
3069 ;; unless we're at bob.
3070
3071 (let ((start-pos (point)) syntactic-pos)
3072 ;; Must back up a bit since we look for the end of the previous
3073 ;; statement or declaration, which is earlier than the first
3074 ;; returned match.
3075
3076 (when (c-got-face-at (point) c-literal-faces)
3077 ;; But first we need to move to a syntactically relevant
3078 ;; position. Use the faces to back up to the start of the
3079 ;; comment or string literal.
3080 (when (and (not (bobp))
3081 (c-got-face-at (1- (point)) c-literal-faces))
3082 (while (progn
3083 (goto-char (previous-single-property-change
3084 (point) 'face nil (point-min)))
3085 (and (> (point) (point-min))
3086 (c-got-face-at (point) c-literal-faces)))))
3087
3088 ;; XEmacs doesn't fontify the quotes surrounding string
3089 ;; literals.
3090 (and (featurep 'xemacs)
3091 (eq (get-text-property (point) 'face)
3092 'font-lock-string-face)
3093 (not (bobp))
3094 (progn (backward-char)
3095 (not (looking-at c-string-limit-regexp)))
3096 (forward-char))
3097
3098 ;; The font lock package might not have fontified the start of
3099 ;; the literal at all so check that we have arrived at
3100 ;; something that looks like a start or else resort to
3101 ;; `c-literal-limits'.
3102 (unless (looking-at c-literal-start-regexp)
3103 (let ((range (c-literal-limits)))
3104 (if range (goto-char (car range))))))
3105
3106 ;; Must back out of any macro so that we don't miss any
3107 ;; declaration that could follow after it, unless the limit is
3108 ;; inside the macro. We only check that for the current line to
3109 ;; save some time; it's enough for the by far most common case
3110 ;; when font-lock refontifies the current line only.
3111 (when (save-excursion
3112 (and (= (forward-line 1) 0)
3113 (or (< (c-point 'eol) cfd-limit)
3114 (progn (backward-char)
3115 (not (eq (char-before) ?\\))))))
3116 (c-beginning-of-macro))
3117
3118 ;; Clear the cache if it applied further down.
3119 (c-invalidate-find-decl-cache start-pos)
3120
3121 (setq syntactic-pos (point))
3122 (c-backward-syntactic-ws c-find-decl-syntactic-pos)
3123
3124 ;; If we hit `c-find-decl-syntactic-pos' and
3125 ;; `c-find-decl-match-pos' is set then we install the cached
3126 ;; values. If we hit `c-find-decl-syntactic-pos' and
3127 ;; `c-find-decl-match-pos' is nil then we know there's no decl
3128 ;; prefix in the whitespace before `c-find-decl-syntactic-pos'
3129 ;; and so we can continue the search from this point. If we
3130 ;; didn't hit `c-find-decl-syntactic-pos' then we're now in the
3131 ;; right spot to begin searching anyway.
3132 (if (and (eq (point) c-find-decl-syntactic-pos)
3133 c-find-decl-match-pos)
3134
3135 (progn
3136 ;; The match is always outside macros and comments so we
3137 ;; start at the next token. The loop below will later go
3138 ;; back using `cfd-continue-pos' to fix declarations inside
3139 ;; the syntactic ws.
3140 (goto-char syntactic-pos)
3141 (c-forward-syntactic-ws)
3142 (setq cfd-match-pos c-find-decl-match-pos
3143 cfd-continue-pos syntactic-pos)
3144 (if (< cfd-continue-pos (point))
3145 (setq cfd-token-pos (point))))
3146
3147 (setq c-find-decl-syntactic-pos syntactic-pos)
3148
3149 (when (if (bobp)
3150 ;; Always consider bob a match to get the first declaration
3151 ;; in the file. Do this separately instead of letting
3152 ;; `c-decl-prefix-re' match bob, so that it always can
3153 ;; consume at least one character to ensure that we won't
3154 ;; get stuck in an infinite loop.
3155 (setq cfd-re-match 0)
3156 (backward-char)
3157 (c-beginning-of-current-token)
3158 (< (point) cfd-limit))
3159 ;; Do an initial search now. In the bob case above it's only done
3160 ;; to search for the `c-type' property.
3161 (c-find-decl-prefix-search))
3162
3163 ;; Advance `cfd-continue-pos' if we got a hit before the start
3164 ;; position. The earliest position that could affect after
3165 ;; the start position is the char before the preceding
3166 ;; comments.
3167 (when (and cfd-continue-pos (< cfd-continue-pos start-pos))
3168 (goto-char syntactic-pos)
3169 (c-backward-comments)
3170 (unless (bobp)
3171 (backward-char)
3172 (c-beginning-of-current-token))
3173 (setq cfd-continue-pos (max cfd-continue-pos (point))))
3174
3175 ;; If we got a match it's always outside macros and comments so
3176 ;; advance to the next token and set `cfd-token-pos'. The loop
3177 ;; below will later go back using `cfd-continue-pos' to fix
3178 ;; declarations inside the syntactic ws.
3179 (when (and (< cfd-match-pos cfd-limit) (< (point) syntactic-pos))
3180 (goto-char syntactic-pos)
3181 (c-forward-syntactic-ws)
3182 (and cfd-continue-pos
3183 (< cfd-continue-pos (point))
3184 (setq cfd-token-pos (point))))
3185
3186 (setq c-find-decl-match-pos (and (< cfd-match-pos start-pos)
3187 cfd-match-pos))))
3188
3189 ;; Now loop. We already got the first match.
3190
3191 (while (progn
3192 (while (and
3193 (< cfd-match-pos cfd-limit)
3194
3195 (or
3196 ;; Kludge to filter out matches on the "<" that
3197 ;; aren't open parens, for the sake of languages
3198 ;; that got `c-recognize-<>-arglists' set.
3199 (and (eq (char-before cfd-match-pos) ?<)
3200 (not (c-get-char-property (1- cfd-match-pos)
3201 'syntax-table)))
3202
3203 ;; If `cfd-continue-pos' is less or equal to
3204 ;; `cfd-token-pos', we've got a hit inside a macro
3205 ;; that's in the syntactic whitespace before the last
3206 ;; "real" declaration we've checked. If they're equal
3207 ;; we've arrived at the declaration a second time, so
3208 ;; there's nothing to do.
3209 (= cfd-continue-pos cfd-token-pos)
3210
3211 (progn
3212 ;; If `cfd-continue-pos' is less than `cfd-token-pos'
3213 ;; we're still searching for declarations embedded in
3214 ;; the syntactic whitespace. In that case we need
3215 ;; only to skip comments and not macros, since they
3216 ;; can't be nested, and that's already been done in
3217 ;; `c-find-decl-prefix-search'.
3218 (when (> cfd-continue-pos cfd-token-pos)
3219 (c-forward-syntactic-ws)
3220 (setq cfd-token-pos (point)))
3221
3222 ;; Continue if the following token fails the
3223 ;; CFD-DECL-RE and CFD-FACE-CHECKLIST checks.
3224 (when (or (>= (point) cfd-limit)
3225 (not (looking-at cfd-decl-re))
3226 (and cfd-face-checklist
3227 (not (c-got-face-at
3228 (point) cfd-face-checklist))))
3229 (goto-char cfd-continue-pos)
3230 t)))
3231
3232 (< (point) cfd-limit))
3233 (c-find-decl-prefix-search))
3234
3235 (< (point) cfd-limit))
3236
3237 (when (progn
3238 ;; Narrow to the end of the macro if we got a hit inside
3239 ;; one, to avoid recognizing things that start inside
3240 ;; the macro and end outside it.
3241 (when (> cfd-match-pos cfd-macro-end)
3242 ;; Not in the same macro as in the previous round.
3243 (save-excursion
3244 (goto-char cfd-match-pos)
3245 (setq cfd-macro-end
3246 (if (save-excursion (and (c-beginning-of-macro)
3247 (< (point) cfd-match-pos)))
3248 (progn (c-end-of-macro)
3249 (point))
3250 0))))
3251
3252 (if (zerop cfd-macro-end)
3253 t
3254 (if (> cfd-macro-end (point))
3255 (progn (narrow-to-region (point-min) cfd-macro-end)
3256 t)
3257 ;; The matched token was the last thing in the
3258 ;; macro, so the whole match is bogus.
3259 (setq cfd-macro-end 0)
3260 nil)))
3261
3262 (c-debug-put-decl-spot-faces cfd-match-pos (point))
3263 (funcall cfd-fun cfd-match-pos (/= cfd-macro-end 0))
3264
3265 (when (/= cfd-macro-end 0)
3266 ;; Restore limits if we did macro narrowment above.
3267 (narrow-to-region (point-min) cfd-buffer-end)))
3268
3269 (goto-char cfd-continue-pos)
3270 (if (= cfd-continue-pos cfd-limit)
3271 (setq cfd-match-pos cfd-limit)
3272 (c-find-decl-prefix-search)))))
3273
3274\f
3275;; A cache for found types.
3276
3277;; Buffer local variable that contains an obarray with the types we've
3278;; found. If a declaration is recognized somewhere we record the
3279;; fully qualified identifier in it to recognize it as a type
3280;; elsewhere in the file too. This is not accurate since we do not
3281;; bother with the scoping rules of the languages, but in practice the
3282;; same name is seldom used as both a type and something else in a
3283;; file, and we only use this as a last resort in ambiguous cases (see
3284;; `c-font-lock-declarations').
3285(defvar c-found-types nil)
3286(make-variable-buffer-local 'c-found-types)
3287
3288(defsubst c-clear-found-types ()
3289 ;; Clears `c-found-types'.
a66cd3ee 3290 ;;
d9e94c22
MS
3291 ;; This function does not do any hidden buffer changes.
3292 (setq c-found-types (make-vector 53 0)))
3293
3294(defun c-add-type (from to)
3295 ;; Add the given region as a type in `c-found-types'. If the region
3296 ;; doesn't match an existing type but there is a type which is equal
3297 ;; to the given one except that the last character is missing, then
3298 ;; the shorter type is removed. That's done to avoid adding all
3299 ;; prefixes of a type as it's being entered and font locked. This
3300 ;; doesn't cover cases like when characters are removed from a type
3301 ;; or added in the middle. We'd need the position of point when the
3302 ;; font locking is invoked to solve this well.
3303 (unless (and c-recognize-<>-arglists
3304 (save-excursion
3305 (goto-char from)
3306 (c-syntactic-re-search-forward "<" to t)))
3307 ;; To avoid storing very long strings, do not add a type that
3308 ;; contains '<' in languages with angle bracket arglists, since
3309 ;; the type then probably contains a C++ template spec and those
3310 ;; can be fairly sized programs in themselves.
3311 (let ((type (c-syntactic-content from to)))
3312 (unless (intern-soft type c-found-types)
3313 (unintern (substring type 0 -1) c-found-types)
3314 (intern type c-found-types)))))
3315
3316(defsubst c-check-type (from to)
3317 ;; Return non-nil if the given region contains a type in
3318 ;; `c-found-types'.
3319 (intern-soft (c-syntactic-content from to) c-found-types))
3320
3321(defun c-list-found-types ()
3322 ;; Return all the types in `c-found-types' as a sorted list of
3323 ;; strings.
3324 (let (type-list)
3325 (mapatoms (lambda (type)
3326 (setq type-list (cons (symbol-name type)
3327 type-list)))
3328 c-found-types)
3329 (sort type-list 'string-lessp)))
a66cd3ee 3330
d9e94c22
MS
3331\f
3332;; Handling of small scale constructs like types and names.
3333
3334(defun c-remove-<>-arglist-properties (from to)
3335 ;; Remove all the properties put by `c-forward-<>-arglist' in the
3336 ;; specified region. Point is clobbered.
3337 (goto-char from)
3338 (while (progn (skip-chars-forward "^<>," to)
3339 (< (point) to))
3340 (if (eq (char-after) ?,)
3341 (when (eq (c-get-char-property (point) 'c-type) 'c-<>-arg-sep)
3342 (c-clear-char-property (point) 'c-type))
3343 (c-clear-char-property (point) 'syntax-table))
3344 (forward-char)))
3345
3346;; Dynamically bound variable that instructs `c-forward-type' to also
3347;; treat possible types (i.e. those that it normally returns 'maybe or
3348;; 'found for) as actual types (and always return 'found for them).
3349;; This means that it records them in `c-record-type-identifiers' if
3350;; that is set, and that it adds them to `c-found-types'.
3351(defvar c-promote-possible-types nil)
3352
3353;; Dynamically bound variable that instructs `c-forward-<>-arglist' to
3354;; not accept arglists that contain more than one argument. It's used
3355;; to handle ambiguous cases like "foo (a < b, c > d)" better.
3356(defvar c-disallow-comma-in-<>-arglists nil)
3357
3358;; Dynamically bound variables that instructs `c-forward-name',
3359;; `c-forward-type' and `c-forward-<>-arglist' to record the ranges of
3360;; all the type and reference identifiers they encounter. They will
3361;; build lists on these variables where each element is a cons of the
3362;; buffer positions surrounding each identifier. This recording is
3363;; only activated when `c-record-type-identifiers' is non-nil.
3364;;
3365;; All known types that can't be identifiers are recorded, and also
3366;; other possible types if `c-promote-possible-types' is set.
3367;; Recording is however disabled inside angle bracket arglists that
3368;; are encountered inside names and other angle bracket arglists.
3369;; Such occurences are taken care of by `c-font-lock-<>-arglists'
3370;; instead.
3371;;
3372;; Only the names in C++ template style references (e.g. "tmpl" in
3373;; "tmpl<a,b>::foo") are recorded as references, other references
3374;; aren't handled here.
3375(defvar c-record-type-identifiers nil)
3376(defvar c-record-ref-identifiers nil)
3377
3378;; If `c-record-type-identifiers' is set, this will receive a cons
3379;; cell of the range of the last single identifier symbol stepped over
3380;; by `c-forward-name' if it's successful. This is the range that
3381;; should be put on one of the record lists by the caller. It's
3382;; assigned nil if there's no such symbol in the name.
3383(defvar c-last-identifier-range nil)
3384
3385(defmacro c-record-type-id (range)
3386 (if (eq (car-safe range) 'cons)
3387 ;; Always true.
3388 `(setq c-record-type-identifiers
3389 (cons ,range c-record-type-identifiers))
3390 `(let ((range ,range))
3391 (if range
3392 (setq c-record-type-identifiers
3393 (cons range c-record-type-identifiers))))))
3394
3395(defmacro c-record-ref-id (range)
3396 (if (eq (car-safe range) 'cons)
3397 ;; Always true.
3398 `(setq c-record-ref-identifiers
3399 (cons ,range c-record-ref-identifiers))
3400 `(let ((range ,range))
3401 (if range
3402 (setq c-record-ref-identifiers
3403 (cons range c-record-ref-identifiers))))))
3404
3405;; Dynamically bound variable that instructs `c-forward-type' to
3406;; record the ranges of types that only are found. Behaves otherwise
3407;; like `c-record-type-identifiers'.
3408(defvar c-record-found-types nil)
3409
3410(defmacro c-forward-keyword-prefixed-id (type)
3411 ;; Used internally in `c-forward-keyword-clause' to move forward
3412 ;; over a type (if TYPE is 'type) or a name (otherwise) which
3413 ;; possibly is prefixed by keywords and their associated clauses.
3414 ;; Try with a type/name first to not trip up on those that begin
3415 ;; with a keyword. Return t if a known or found type is moved
3416 ;; over. The point is clobbered if nil is returned. If range
3417 ;; recording is enabled, the identifier is recorded on as a type
3418 ;; if TYPE is 'type or as a reference if TYPE is 'ref.
3419 `(let (res)
3420 (while (if (setq res ,(if (eq type 'type)
3421 `(c-forward-type)
3422 `(c-forward-name)))
3423 nil
3424 (and (looking-at c-keywords-regexp)
3425 (c-forward-keyword-clause))))
3426 (when (memq res '(t known found prefix))
3427 ,(when (eq type 'ref)
3428 `(when c-record-type-identifiers
3429 (c-record-ref-id c-last-identifier-range)))
3430 t)))
3431
3432(defmacro c-forward-id-comma-list (type)
3433 ;; Used internally in `c-forward-keyword-clause' to move forward
3434 ;; over a comma separated list of types or names using
3435 ;; `c-forward-keyword-prefixed-id'.
3436 `(while (and (progn
3437 (setq safe-pos (point))
3438 (eq (char-after) ?,))
3439 (progn
3440 (forward-char)
3441 (c-forward-syntactic-ws)
3442 (c-forward-keyword-prefixed-id ,type)))))
3443
3444(defun c-forward-keyword-clause ()
3445 ;; The first submatch in the current match data is assumed to
3446 ;; surround a token. If it's a keyword, move over it and any
3447 ;; following clauses associated with it, stopping at the next
3448 ;; following token. t is returned in that case, otherwise the point
3449 ;; stays and nil is returned. The kind of clauses that are
3450 ;; recognized are those specified by `c-type-list-kwds',
3451 ;; `c-ref-list-kwds', `c-colon-type-list-kwds',
3452 ;; `c-paren-nontype-kwds', `c-paren-type-kwds', `c-<>-type-kwds',
3453 ;; and `c-<>-arglist-kwds'.
3454
3455 (let ((kwd-sym (c-keyword-sym (match-string 1))) safe-pos pos)
3456 (when kwd-sym
3457 (goto-char (match-end 1))
3458 (c-forward-syntactic-ws)
3459 (setq safe-pos (point))
a66cd3ee 3460
d9e94c22
MS
3461 (cond
3462 ((and (c-keyword-member kwd-sym 'c-type-list-kwds)
3463 (c-forward-keyword-prefixed-id type))
3464 ;; There's a type directly after a keyword in `c-type-list-kwds'.
3465 (c-forward-id-comma-list type))
3466
3467 ((and (c-keyword-member kwd-sym 'c-ref-list-kwds)
3468 (c-forward-keyword-prefixed-id ref))
3469 ;; There's a name directly after a keyword in `c-ref-list-kwds'.
3470 (c-forward-id-comma-list ref))
3471
3472 ((and (c-keyword-member kwd-sym 'c-paren-any-kwds)
3473 (eq (char-after) ?\())
3474 ;; There's an open paren after a keyword in `c-paren-any-kwds'.
3475
3476 (forward-char)
3477 (when (and (setq pos (c-up-list-forward))
3478 (eq (char-before pos) ?\)))
3479 (when (and c-record-type-identifiers
3480 (c-keyword-member kwd-sym 'c-paren-type-kwds))
3481 ;; Use `c-forward-type' on every identifier we can find
3482 ;; inside the paren, to record the types.
3483 (while (c-syntactic-re-search-forward c-symbol-start pos t)
3484 (goto-char (match-beginning 0))
3485 (unless (c-forward-type)
3486 (looking-at c-symbol-key) ; Always matches.
3487 (goto-char (match-end 0)))))
3488
3489 (goto-char pos)
3490 (c-forward-syntactic-ws)
3491 (setq safe-pos (point))))
3492
3493 ((and (c-keyword-member kwd-sym 'c-<>-sexp-kwds)
3494 (eq (char-after) ?<)
3495 (c-forward-<>-arglist (c-keyword-member kwd-sym 'c-<>-type-kwds)
3496 (or c-record-type-identifiers
3497 c-disallow-comma-in-<>-arglists)))
3498 (c-forward-syntactic-ws)
3499 (setq safe-pos (point)))
3500
3501 ((and (c-keyword-member kwd-sym 'c-nonsymbol-sexp-kwds)
3502 (not (looking-at c-symbol-start)))
3503 (c-forward-sexp)
3504 (c-forward-syntactic-ws)
3505 (setq safe-pos (point))))
3506
3507 (when (and (c-keyword-member kwd-sym 'c-colon-type-list-kwds)
3508 (progn
3509 ;; If a keyword matched both one of the types above and
3510 ;; this one, we match `c-colon-type-list-re' after the
3511 ;; clause matched above.
3512 (goto-char safe-pos)
3513 (looking-at c-colon-type-list-re))
3514 (progn
3515 (goto-char (match-end 0))
3516 (c-forward-syntactic-ws)
3517 (c-forward-keyword-prefixed-id type)))
3518 ;; There's a type after the `c-colon-type-list-re'
3519 ;; match after a keyword in `c-colon-type-list-kwds'.
3520 (c-forward-id-comma-list type))
3521
3522 (goto-char safe-pos)
3523 t)))
3524
3525(defun c-forward-<>-arglist (all-types reparse)
3526 ;; The point is assumed to be at a '<'. Try to treat it as the open
3527 ;; paren of an angle bracket arglist and move forward to the the
3528 ;; corresponding '>'. If successful, the point is left after the
3529 ;; '>' and t is returned, otherwise the point isn't moved and nil is
3530 ;; returned. If ALL-TYPES is t then all encountered arguments in
3531 ;; the arglist that might be types are treated as found types.
3532 ;;
3533 ;; The surrounding '<' and '>' are given syntax-table properties to
3534 ;; make them behave like parentheses. Each argument separating ','
3535 ;; is also set to `c-<>-arg-sep' in the `c-type' property. These
3536 ;; properties are also cleared in a relevant region forward from the
3537 ;; point if they seems to be set and it turns out to not be an
3538 ;; arglist.
3539 ;;
3540 ;; If the arglist has been successfully parsed before then paren
3541 ;; syntax properties will be exploited to quickly jump to the end,
3542 ;; but that can be disabled by setting REPARSE to t. That is
3543 ;; necessary if the various side effects, e.g. recording of type
3544 ;; ranges, are important. Setting REPARSE to t only applies
3545 ;; recursively to nested angle bracket arglists if
3546 ;; `c-disallow-comma-in-<>-arglists' is set.
3547 ;;
3548 ;; This is primarily used in C++ to mark up template arglists. C++
3549 ;; disambiguates them by checking whether the preceding name is a
3550 ;; template or not. We can't do that, so we assume it is a template
3551 ;; if it can be parsed as one. This usually works well since
3552 ;; comparison expressions on the forms "a < b > c" or "a < b, c > d"
3553 ;; in almost all cases would be pointless. Cases like function
3554 ;; calls on the form "foo (a < b, c > d)" needs to be handled
3555 ;; specially through the `c-disallow-comma-in-<>-arglists' variable.
3556
3557 (let ((start (point))
3558 ;; If `c-record-type-identifiers' is set then activate
3559 ;; recording of any found types that constitute an argument in
3560 ;; the arglist.
3561 (c-record-found-types (if c-record-type-identifiers t)))
3562 (if (catch 'angle-bracket-arglist-escape
3563 (setq c-record-found-types
3564 (c-forward-<>-arglist-recur all-types reparse)))
3565 (progn
3566 (when (consp c-record-found-types)
3567 (setq c-record-type-identifiers
3568 ;; `nconc' doesn't mind that the tail of
3569 ;; `c-record-found-types' is t.
3570 (nconc c-record-found-types c-record-type-identifiers)))
3571 t)
3572
3573 (goto-char start)
a66cd3ee 3574 nil)))
785eecbb 3575
d9e94c22
MS
3576(defun c-forward-<>-arglist-recur (all-types reparse)
3577 ;; Recursive part of `c-forward-<>-arglist'.
3578
3579 (let ((start (point)) res pos tmp
3580 ;; Cover this so that any recorded found type ranges are
3581 ;; automatically lost if it turns out to not be an angle
3582 ;; bracket arglist. It's propagated through the return value
3583 ;; on successful completion.
3584 (c-record-found-types c-record-found-types)
3585 ;; List that collects the positions after the argument
3586 ;; separating ',' in the arglist.
3587 arg-start-pos)
3588
3589 ;; If the '<' has paren open syntax then we've marked it as an
3590 ;; angle bracket arglist before, so try to skip to the end and see
3591 ;; that the close paren matches.
3592 (if (and (c-get-char-property (point) 'syntax-table)
3593 (progn
3594 (forward-char)
3595 (if (and (not (looking-at c-<-op-cont-regexp))
3596 (if (c-parse-sexp-lookup-properties)
3597 (c-go-up-list-forward)
3598 (catch 'at-end
3599 (let ((depth 1))
3600 (while (c-syntactic-re-search-forward
3601 "[<>]" nil t t)
3602 (when (c-get-char-property (1- (point))
3603 'syntax-table)
3604 (if (eq (char-before) ?<)
3605 (setq depth (1+ depth))
3606 (setq depth (1- depth))
3607 (when (= depth 0) (throw 'at-end t)))))
3608 nil)))
3609 (not (looking-at c->-op-cont-regexp))
3610 (save-excursion
3611 (backward-char)
3612 (= (point)
3613 (progn (c-beginning-of-current-token)
3614 (point)))))
3615
3616 ;; Got an arglist that appears to be valid.
3617 (if reparse
3618 ;; Reparsing is requested, so zap the properties in the
3619 ;; region and go on to redo it. It's done here to
3620 ;; avoid leaving it behind if we exit through
3621 ;; `angle-bracket-arglist-escape' below.
3622 (progn
3623 (c-remove-<>-arglist-properties start (point))
3624 (goto-char start)
3625 nil)
3626 t)
3627
3628 ;; Got unmatched paren brackets or either paren was
3629 ;; actually some other token. Recover by clearing the
3630 ;; syntax properties on all the '<' and '>' in the
3631 ;; range where we'll search for the arglist below.
3632 (goto-char start)
3633 (while (progn (skip-chars-forward "^<>,;{}")
3634 (looking-at "[<>,]"))
3635 (if (eq (char-after) ?,)
3636 (when (eq (c-get-char-property (point) 'c-type)
3637 'c-<>-arg-sep)
3638 (c-clear-char-property (point) 'c-type))
3639 (c-clear-char-property (point) 'syntax-table))
3640 (forward-char))
3641 (goto-char start)
3642 nil)))
3643 t
3644
3645 (forward-char)
3646 (unless (looking-at c-<-op-cont-regexp)
3647 (while (and
3648 (progn
3649
3650 (when c-record-type-identifiers
3651 (if all-types
3652
3653 ;; All encountered identifiers are types, so set the
3654 ;; promote flag and parse the type.
3655 (progn
3656 (c-forward-syntactic-ws)
3657 (when (looking-at c-identifier-start)
3658 (let ((c-promote-possible-types t))
3659 (c-forward-type))))
3660
3661 ;; Check if this arglist argument is a sole type. If
3662 ;; it's known then it's recorded in
3663 ;; `c-record-type-identifiers'. If it only is found
3664 ;; then it's recorded in `c-record-found-types' which we
3665 ;; might roll back if it turns out that this isn't an
3666 ;; angle bracket arglist afterall.
3667 (when (memq (char-before) '(?, ?<))
3668 (let ((orig-record-found-types c-record-found-types))
3669 (c-forward-syntactic-ws)
3670 (and (memq (c-forward-type) '(known found))
3671 (not (looking-at "[,>]"))
3672 ;; A found type was recorded but it's not the
3673 ;; only thing in the arglist argument, so reset
3674 ;; `c-record-found-types'.
3675 (setq c-record-found-types
3676 orig-record-found-types))))))
3677
3678 (setq pos (point))
3679 (or (when (eq (char-after) ?>)
3680 ;; Must check for '>' at the very start separately,
3681 ;; since the regexp below has to avoid ">>" without
3682 ;; using \\=.
3683 (forward-char)
3684 t)
3685
3686 ;; Note: This regexp exploits the match order in
3687 ;; \| so that "<>" is matched by "<" rather than
3688 ;; "[^>:-]>".
3689 (c-syntactic-re-search-forward
3690 "[<;{},]\\|\\([^>:-]>\\)" nil 'move t t 1)
3691
3692 ;; If the arglist starter has lost its open paren
3693 ;; syntax but not the closer, we won't find the
3694 ;; closer above since we only search in the
3695 ;; balanced sexp. In that case we stop just short
3696 ;; of it so check if the following char is the closer.
3697 (when (eq (char-after) ?>)
3698 ;; Remove its syntax so that we don't enter the
3699 ;; recovery code below. That's not necessary
3700 ;; since there's no real reason to suspect that
3701 ;; things inside the arglist are unbalanced.
3702 (c-clear-char-property (point) 'syntax-table)
3703 (forward-char)
3704 t)))
3705
3706 (cond
3707 ((eq (char-before) ?>)
3708 ;; Either an operator starting with '>' or the end of
3709 ;; the angle bracket arglist.
3710
3711 (if (and (/= (1- (point)) pos)
3712 (c-get-char-property (1- (point)) 'syntax-table)
3713 (progn
3714 (c-clear-char-property (1- (point)) 'syntax-table)
3715 (c-parse-sexp-lookup-properties)))
3716
3717 ;; We've skipped past a list that ended with '>'. It
3718 ;; must be unbalanced since nested arglists are handled
3719 ;; in the case below. Recover by removing all paren
3720 ;; properties on '<' and '>' in the searched region and
3721 ;; redo the search.
3722 (progn
3723 (c-remove-<>-arglist-properties pos (point))
3724 (goto-char pos)
3725 t)
3726
3727 (if (looking-at c->-op-cont-regexp)
3728 (progn
3729 (when (text-property-not-all
3730 (1- (point)) (match-end 0) 'syntax-table nil)
3731 (c-remove-<>-arglist-properties (1- (point))
3732 (match-end 0)))
3733 (goto-char (match-end 0))
3734 t)
3735
3736 ;; The angle bracket arglist is finished.
3737 (while arg-start-pos
3738 (c-put-char-property (1- (car arg-start-pos))
3739 'c-type 'c-<>-arg-sep)
3740 (setq arg-start-pos (cdr arg-start-pos)))
3741 (c-mark-<-as-paren start)
3742 (c-mark->-as-paren (1- (point)))
3743 (setq res t)
3744 nil)))
3745
3746 ((eq (char-before) ?<)
3747 ;; Either an operator starting with '<' or a nested arglist.
3748
3749 (setq pos (point))
3750 (let (id-start id-end subres keyword-match)
3751 (if (if (looking-at c-<-op-cont-regexp)
3752 (setq tmp (match-end 0))
3753 (setq tmp pos)
3754 (backward-char)
3755 (not
3756 (and
3757
3758 (save-excursion
3759 ;; There's always an identifier before a angle
3760 ;; bracket arglist, or a keyword in
3761 ;; `c-<>-type-kwds' or `c-<>-arglist-kwds'.
3762 (c-backward-syntactic-ws)
3763 (setq id-end (point))
3764 (c-simple-skip-symbol-backward)
3765 (when (or (setq keyword-match
3766 (looking-at c-opt-<>-sexp-key))
3767 (not (looking-at c-keywords-regexp)))
3768 (setq id-start (point))))
3769
3770 (setq subres
3771 (let ((c-record-type-identifiers nil)
3772 (c-record-found-types nil))
3773 (c-forward-<>-arglist-recur
3774 (and keyword-match
3775 (c-keyword-member
3776 (c-keyword-sym (match-string 1))
3777 'c-<>-type-kwds))
3778 (and reparse
3779 c-disallow-comma-in-<>-arglists))))
3780 )))
3781
3782 ;; It was not an angle bracket arglist.
3783 (progn
3784 (when (text-property-not-all
3785 (1- pos) tmp 'syntax-table nil)
3786 (if (c-parse-sexp-lookup-properties)
3787 ;; Got an invalid open paren syntax on this
3788 ;; '<'. We'll probably get an unbalanced '>'
3789 ;; further ahead if we just remove the syntax
3790 ;; here, so recover by removing all paren
3791 ;; properties up to and including the
3792 ;; balancing close paren.
3793 (parse-partial-sexp pos (point-max) -1)
3794 (goto-char tmp))
3795 (c-remove-<>-arglist-properties pos (point)))
3796 (goto-char tmp))
3797
3798 ;; It was an angle bracket arglist.
3799 (setq c-record-found-types subres)
3800
3801 ;; Record the identifier before the template as a type
3802 ;; or reference depending on whether the arglist is last
3803 ;; in a qualified identifier.
3804 (when (and c-record-type-identifiers
3805 (not keyword-match))
3806 (if (and c-opt-identifier-concat-key
3807 (progn
3808 (c-forward-syntactic-ws)
3809 (looking-at c-opt-identifier-concat-key)))
3810 (c-record-ref-id (cons id-start id-end))
3811 (c-record-type-id (cons id-start id-end))))))
3812 t)
3813
3814 ((and (eq (char-before) ?,)
3815 (not c-disallow-comma-in-<>-arglists))
3816 ;; Just another argument. Record the position. The
3817 ;; type check stuff that made us stop at it is at
3818 ;; the top of the loop.
3819 (setq arg-start-pos (cons (point) arg-start-pos)))
3820
3821 (t
3822 ;; Got a character that can't be in an angle bracket
3823 ;; arglist argument. Abort using `throw', since
3824 ;; it's useless to try to find a surrounding arglist
3825 ;; if we're nested.
3826 (throw 'angle-bracket-arglist-escape nil))))))
3827
3828 (if res
3829 (or c-record-found-types t)))))
3830
3831(defun c-forward-name ()
3832 ;; Move forward over a complete name if at the beginning of one,
3833 ;; stopping at the next following token. If the point is not at
3834 ;; something that are recognized as name then it stays put. A name
3835 ;; could be something as simple as "foo" in C or something as
3836 ;; complex as "X<Y<class A<int>::B, BIT_MAX >> b>, ::operator<> ::
3837 ;; Z<(a>b)> :: operator const X<&foo>::T Q::G<unsigned short
3838 ;; int>::*volatile const" in C++ (this function is actually little
3839 ;; more than a `looking-at' call in all modes except those that,
3840 ;; like C++, have `c-recognize-<>-arglists' set). Return nil if no
3841 ;; name is found, 'template if it's an identifier ending with an
3842 ;; angle bracket arglist, 'operator of it's an operator identifier,
3843 ;; or t if it's some other kind of name.
3844
3845 (let ((pos (point)) res id-start id-end
3846 ;; Turn off `c-promote-possible-types' here since we might
3847 ;; call `c-forward-<>-arglist' and we don't want it to promote
3848 ;; every suspect thing in the arglist to a type. We're
3849 ;; typically called from `c-forward-type' in this case, and
3850 ;; the caller only wants the top level type that it finds to
3851 ;; be promoted.
3852 c-promote-possible-types)
3853 (while
3854 (and
3855 (looking-at c-identifier-key)
3856
3857 (progn
3858 ;; Check for keyword. We go to the last symbol in
3859 ;; `c-identifier-key' first.
3860 (if (eq c-identifier-key c-symbol-key)
3861 (setq id-start (point)
3862 id-end (match-end 0))
3863 (goto-char (setq id-end (match-end 0)))
3864 (c-simple-skip-symbol-backward)
3865 (setq id-start (point)))
3866
3867 (if (looking-at c-keywords-regexp)
3868 (when (and (c-major-mode-is 'c++-mode)
3869 (looking-at
3870 (cc-eval-when-compile
3871 (concat "\\(operator\\|\\(template\\)\\)"
3872 "\\(" (c-lang-const c-nonsymbol-key c++)
3873 "\\|$\\)")))
3874 (if (match-beginning 2)
3875 ;; "template" is only valid inside an
3876 ;; identifier if preceded by "::".
3877 (save-excursion
3878 (c-backward-syntactic-ws)
3879 (and (c-safe (backward-char 2) t)
3880 (looking-at "::")))
3881 t))
3882
3883 ;; Handle a C++ operator or template identifier.
3884 (goto-char id-end)
3885 (c-forward-syntactic-ws)
3886 (cond ((eq (char-before id-end) ?e)
3887 ;; Got "... ::template".
3888 (let ((subres (c-forward-name)))
3889 (when subres
3890 (setq pos (point)
3891 res subres))))
3892
3893 ((looking-at c-identifier-start)
3894 ;; Got a cast operator.
3895 (when (c-forward-type)
3896 (setq pos (point)
3897 res 'operator)
3898 ;; Now we should match a sequence of either
3899 ;; '*', '&' or a name followed by ":: *",
3900 ;; where each can be followed by a sequence
3901 ;; of `c-opt-type-modifier-key'.
3902 (while (cond ((looking-at "[*&]")
3903 (goto-char (match-end 0))
3904 t)
3905 ((looking-at c-identifier-start)
3906 (and (c-forward-name)
3907 (looking-at "::")
3908 (progn
3909 (goto-char (match-end 0))
3910 (c-forward-syntactic-ws)
3911 (eq (char-after) ?*))
3912 (progn
3913 (forward-char)
3914 t))))
3915 (while (progn
3916 (c-forward-syntactic-ws)
3917 (setq pos (point))
3918 (looking-at c-opt-type-modifier-key))
3919 (goto-char (match-end 1))))))
3920
3921 ((looking-at c-overloadable-operators-regexp)
3922 ;; Got some other operator.
3923 (when c-record-type-identifiers
3924 (setq c-last-identifier-range
3925 (cons (point) (match-end 0))))
3926 (goto-char (match-end 0))
3927 (c-forward-syntactic-ws)
3928 (setq pos (point)
3929 res 'operator)))
3930
3931 nil)
3932
3933 (when c-record-type-identifiers
3934 (setq c-last-identifier-range
3935 (cons id-start id-end)))
3936 (goto-char id-end)
3937 (c-forward-syntactic-ws)
3938 (setq pos (point)
3939 res t)))
3940
3941 (progn
3942 (goto-char pos)
3943 (when (or c-opt-identifier-concat-key
3944 c-recognize-<>-arglists)
3945
3946 (cond
3947 ((and c-opt-identifier-concat-key
3948 (looking-at c-opt-identifier-concat-key))
3949 ;; Got a concatenated identifier. This handles the
3950 ;; cases with tricky syntactic whitespace that aren't
3951 ;; covered in `c-identifier-key'.
3952 (goto-char (match-end 0))
3953 (c-forward-syntactic-ws)
3954 t)
3955
3956 ((and c-recognize-<>-arglists
3957 (eq (char-after) ?<))
3958 ;; Maybe an angle bracket arglist.
3959 (when (let ((c-record-type-identifiers nil)
3960 (c-record-found-types nil))
3961 (c-forward-<>-arglist
3962 nil c-disallow-comma-in-<>-arglists))
3963 (c-forward-syntactic-ws)
3964 (setq pos (point))
3965 (if (and c-opt-identifier-concat-key
3966 (looking-at c-opt-identifier-concat-key))
3967 ;; Continue if there's an identifier concatenation
3968 ;; operator after the template argument.
3969 (progn
3970 (when c-record-type-identifiers
3971 (c-record-ref-id (cons id-start id-end))
3972 (setq c-last-identifier-range nil))
3973 (forward-char 2)
3974 (c-forward-syntactic-ws)
3975 t)
3976 ;; `c-add-type' isn't called here since we don't
3977 ;; want to add types containing angle bracket
3978 ;; arglists.
3979 (when c-record-type-identifiers
3980 (c-record-type-id (cons id-start id-end))
3981 (setq c-last-identifier-range nil))
3982 (setq res 'template)
3983 nil)))
3984 )))))
3985
3986 (goto-char pos)
3987 res))
3988
3989(defun c-forward-type ()
3990 ;; Move forward over a type spec if at the beginning of one,
3991 ;; stopping at the next following token. Return t if it's a known
3992 ;; type that can't be a name, 'known if it's an otherwise known type
3993 ;; (according to `*-font-lock-extra-types'), 'prefix if it's a known
3994 ;; prefix of a type, 'found if it's a type that matches one in
3995 ;; `c-found-types', 'maybe if it's an identfier that might be a
3996 ;; type, or nil if it can't be a type (the point isn't moved then).
3997 ;; The point is assumed to be at the beginning of a token.
3998 ;;
3999 ;; Note that this function doesn't skip past the brace definition
4000 ;; that might be considered part of the type, e.g.
4001 ;; "enum {a, b, c} foo".
4002 (let ((start (point)) pos res res2 id-start id-end id-range)
4003
4004 ;; Skip leading type modifiers. If any are found we know it's a
4005 ;; prefix of a type.
4006 (when c-opt-type-modifier-key
4007 (while (looking-at c-opt-type-modifier-key)
4008 (goto-char (match-end 1))
4009 (c-forward-syntactic-ws)
4010 (setq res 'prefix)))
4011
4012 (cond
4013 ((looking-at c-type-prefix-key)
4014 ;; Looking at a keyword that prefixes a type identifier,
4015 ;; e.g. "class".
4016 (goto-char (match-end 1))
4017 (c-forward-syntactic-ws)
4018 (setq pos (point))
4019 (if (memq (setq res2 (c-forward-name)) '(t template))
4020 (progn
4021 (when (eq res2 t)
4022 ;; In many languages the name can be used without the
4023 ;; prefix, so we add it to `c-found-types'.
4024 (c-add-type pos (point))
4025 (when c-record-type-identifiers
4026 (c-record-type-id c-last-identifier-range)))
4027 (setq res t))
4028 ;; Invalid syntax.
4029 (goto-char start)
4030 (setq res nil)))
4031
4032 ((progn
4033 (setq pos nil)
4034 (if (looking-at c-identifier-start)
4035 (save-excursion
4036 (setq id-start (point)
4037 res2 (c-forward-name))
4038 (when res2
4039 (setq id-end (point)
4040 id-range c-last-identifier-range))))
4041 (and (cond ((looking-at c-primitive-type-key)
4042 (setq res t))
4043 ((c-with-syntax-table c-identifier-syntax-table
4044 (looking-at c-known-type-key))
4045 (setq res 'known)))
4046 (or (not id-end)
4047 (>= (save-excursion
4048 (save-match-data
4049 (goto-char (match-end 1))
4050 (c-forward-syntactic-ws)
4051 (setq pos (point))))
4052 id-end)
4053 (setq res nil))))
4054 ;; Looking at a primitive or known type identifier. We've
4055 ;; checked for a name first so that we don't go here if the
4056 ;; known type match only is a prefix of another name.
4057
4058 (setq id-end (match-end 1))
4059
4060 (when (and c-record-type-identifiers
4061 (or c-promote-possible-types (eq res t)))
4062 (c-record-type-id (cons (match-beginning 1) (match-end 1))))
4063
4064 (if (and c-opt-type-component-key
4065 (save-match-data
4066 (looking-at c-opt-type-component-key)))
4067 ;; There might be more keywords for the type.
4068 (let (safe-pos)
4069 (c-forward-keyword-clause)
4070 (while (progn
4071 (setq safe-pos (point))
4072 (looking-at c-opt-type-component-key))
4073 (when (and c-record-type-identifiers
4074 (looking-at c-primitive-type-key))
4075 (c-record-type-id (cons (match-beginning 1)
4076 (match-end 1))))
4077 (c-forward-keyword-clause))
4078 (if (looking-at c-primitive-type-key)
4079 (progn
4080 (when c-record-type-identifiers
4081 (c-record-type-id (cons (match-beginning 1)
4082 (match-end 1))))
4083 (c-forward-keyword-clause)
4084 (setq res t))
4085 (goto-char safe-pos)
4086 (setq res 'prefix)))
4087 (unless (save-match-data (c-forward-keyword-clause))
4088 (if pos
4089 (goto-char pos)
4090 (goto-char (match-end 1))
4091 (c-forward-syntactic-ws)))))
4092
4093 (res2
4094 (cond ((eq res2 t)
4095 ;; A normal identifier.
4096 (goto-char id-end)
4097 (if (or res c-promote-possible-types)
4098 (progn
4099 (c-add-type id-start id-end)
4100 (when c-record-type-identifiers
4101 (c-record-type-id id-range))
4102 (unless res
4103 (setq res 'found)))
4104 (setq res (if (c-check-type id-start id-end)
4105 ;; It's an identifier that has been used as
4106 ;; a type somewhere else.
4107 'found
4108 ;; It's an identifier that might be a type.
4109 'maybe))))
4110 ((eq res2 'template)
4111 ;; A template is a type.
4112 (goto-char id-end)
4113 (setq res t))
4114 (t
4115 ;; Otherwise it's an operator identifier, which is not a type.
4116 (goto-char start)
4117 (setq res nil)))))
4118
4119 (when res
4120 ;; Skip trailing type modifiers. If any are found we know it's
4121 ;; a type.
4122 (when c-opt-type-modifier-key
4123 (while (looking-at c-opt-type-modifier-key)
4124 (goto-char (match-end 1))
4125 (c-forward-syntactic-ws)
4126 (setq res t)))
4127
4128 ;; Step over any type suffix operator. Do not let the existence
4129 ;; of these alter the classification of the found type, since
4130 ;; these operators typically are allowed in normal expressions
4131 ;; too.
4132 (when c-opt-type-suffix-key
4133 (while (looking-at c-opt-type-suffix-key)
4134 (goto-char (match-end 1))
4135 (c-forward-syntactic-ws)))
4136
4137 (when c-opt-type-concat-key
4138 ;; Look for a trailing operator that concatenate the type with
4139 ;; a following one, and if so step past that one through a
4140 ;; recursive call.
4141 (setq pos (point))
4142 (let* ((c-promote-possible-types (or (memq res '(t known))
4143 c-promote-possible-types))
4144 ;; If we can't promote then set `c-record-found-types' so that
4145 ;; we can merge in the types from the second part afterwards if
4146 ;; it turns out to be a known type there.
4147 (c-record-found-types (and c-record-type-identifiers
4148 (not c-promote-possible-types))))
4149 (if (and (looking-at c-opt-type-concat-key)
4150
4151 (progn
4152 (goto-char (match-end 1))
4153 (c-forward-syntactic-ws)
4154 (setq res2 (c-forward-type))))
4155
4156 (progn
4157 ;; If either operand certainly is a type then both are, but we
4158 ;; don't let the existence of the operator itself promote two
4159 ;; uncertain types to a certain one.
4160 (cond ((eq res t))
4161 ((or (eq res 'known) (memq res2 '(t known)))
4162 (c-add-type id-start id-end)
4163 (when c-record-type-identifiers
4164 (c-record-type-id id-range))
4165 (setq res t))
4166 ((eq res 'found))
4167 ((eq res2 'found)
4168 (setq res 'found))
4169 (t
4170 (setq res 'maybe)))
4171
4172 (when (and (eq res t)
4173 (consp c-record-found-types))
4174 ;; Merge in the ranges of any types found by the second
4175 ;; `c-forward-type'.
4176 (setq c-record-type-identifiers
4177 ;; `nconc' doesn't mind that the tail of
4178 ;; `c-record-found-types' is t.
4179 (nconc c-record-found-types
4180 c-record-type-identifiers))))
4181
4182 (goto-char pos))))
4183
4184 (when (and c-record-found-types (memq res '(known found)) id-range)
4185 (setq c-record-found-types
4186 (cons id-range c-record-found-types))))
4187
4188 ;;(message "c-forward-type %s -> %s: %s" start (point) res)
4189
4190 res))
4191
785eecbb 4192\f
d9e94c22
MS
4193;; Handling of large scale constructs like statements and declarations.
4194
785eecbb
RS
4195(defun c-beginning-of-inheritance-list (&optional lim)
4196 ;; Go to the first non-whitespace after the colon that starts a
4197 ;; multiple inheritance introduction. Optional LIM is the farthest
4198 ;; back we should search.
d9e94c22
MS
4199 (let* ((lim (or lim (save-excursion
4200 (c-beginning-of-syntax)
4201 (point)))))
a66cd3ee 4202 (c-with-syntax-table c++-template-syntax-table
d9e94c22
MS
4203 (c-backward-token-2 0 t lim)
4204 (while (and (or (looking-at c-symbol-start)
4205 (looking-at "[<,]"))
4206 (zerop (c-backward-token-2 1 t lim))))
a66cd3ee 4207 (skip-chars-forward "^:"))))
785eecbb 4208
785eecbb
RS
4209(defun c-in-method-def-p ()
4210 ;; Return nil if we aren't in a method definition, otherwise the
4211 ;; position of the initial [+-].
4212 (save-excursion
4213 (beginning-of-line)
a66cd3ee
MS
4214 (and c-opt-method-key
4215 (looking-at c-opt-method-key)
785eecbb
RS
4216 (point))
4217 ))
4218
a66cd3ee
MS
4219;; Contributed by Kevin Ryde <user42@zip.com.au>.
4220(defun c-in-gcc-asm-p ()
4221 ;; Return non-nil if point is within a gcc \"asm\" block.
4222 ;;
4223 ;; This should be called with point inside an argument list.
4224 ;;
4225 ;; Only one level of enclosing parentheses is considered, so for
4226 ;; instance `nil' is returned when in a function call within an asm
4227 ;; operand.
4228
4229 (and c-opt-asm-stmt-key
4230 (save-excursion
4231 (beginning-of-line)
4232 (backward-up-list 1)
4233 (c-beginning-of-statement-1 (point-min) nil t)
4234 (looking-at c-opt-asm-stmt-key))))
4235
abb7e5cf
SM
4236(defun c-at-toplevel-p ()
4237 "Return a determination as to whether point is at the `top-level'.
4238Being at the top-level means that point is either outside any
d9e94c22
MS
4239enclosing block (such function definition), or only inside a class,
4240namespace or other block that contains another declaration level.
abb7e5cf
SM
4241
4242If point is not at the top-level (e.g. it is inside a method
4243definition), then nil is returned. Otherwise, if point is at a
4244top-level not enclosed within a class definition, t is returned.
4245Otherwise, a 2-vector is returned where the zeroth element is the
4246buffer position of the start of the class declaration, and the first
4247element is the buffer position of the enclosing class's opening
4248brace."
a66cd3ee
MS
4249 (let ((paren-state (c-parse-state)))
4250 (or (not (c-most-enclosing-brace paren-state))
4251 (c-search-uplist-for-classkey paren-state))))
4252
d9e94c22 4253(defun c-just-after-func-arglist-p (&optional lim)
785eecbb
RS
4254 ;; Return t if we are between a function's argument list closing
4255 ;; paren and its opening brace. Note that the list close brace
4256 ;; could be followed by a "const" specifier or a member init hanging
d9e94c22
MS
4257 ;; colon. LIM is used as bound for some backward buffer searches;
4258 ;; the search might continue past it.
a66cd3ee
MS
4259 ;;
4260 ;; Note: This test is easily fooled. It only works reasonably well
4261 ;; in the situations where `c-guess-basic-syntax' uses it.
785eecbb 4262 (save-excursion
d9e94c22
MS
4263 (if (c-mode-is-new-awk-p)
4264 (c-awk-backward-syntactic-ws lim)
4265 (c-backward-syntactic-ws lim))
4266 (let ((checkpoint (point)))
785eecbb
RS
4267 ;; could be looking at const specifier
4268 (if (and (eq (char-before) ?t)
4269 (forward-word -1)
a66cd3ee
MS
4270 (looking-at "\\<const\\>[^_]"))
4271 (c-backward-syntactic-ws lim)
785eecbb
RS
4272 ;; otherwise, we could be looking at a hanging member init
4273 ;; colon
4274 (goto-char checkpoint)
2c9c1954
MS
4275 (while (and
4276 (eq (char-before) ?,)
4277 ;; this will catch member inits with multiple
4278 ;; line arglists
4279 (progn
4280 (forward-char -1)
4281 (c-backward-syntactic-ws (c-point 'bol))
4282 (c-safe (c-backward-sexp 1) t))
4283 (or (not (looking-at "\\s\("))
4284 (c-safe (c-backward-sexp 1) t)))
a66cd3ee 4285 (c-backward-syntactic-ws lim))
785eecbb
RS
4286 (if (and (eq (char-before) ?:)
4287 (progn
4288 (forward-char -1)
a66cd3ee
MS
4289 (c-backward-syntactic-ws lim)
4290 (looking-at "\\([ \t\n]\\|\\\\\n\\)*:\\([^:]+\\|$\\)")))
785eecbb
RS
4291 nil
4292 (goto-char checkpoint))
4293 )
a66cd3ee 4294 (setq checkpoint (point))
785eecbb 4295 (and (eq (char-before) ?\))
a66cd3ee
MS
4296 ;; Check that it isn't a cpp expression, e.g. the
4297 ;; expression of an #if directive or the "function header"
4298 ;; of a #define.
4299 (or (not (c-beginning-of-macro))
4300 (and (c-forward-to-cpp-define-body)
4301 (< (point) checkpoint)))
4302 ;; check if we are looking at an ObjC method def
4303 (or (not c-opt-method-key)
785eecbb 4304 (progn
a66cd3ee 4305 (goto-char checkpoint)
0ec8351b 4306 (c-forward-sexp -1)
785eecbb 4307 (forward-char -1)
a66cd3ee 4308 (c-backward-syntactic-ws lim)
785eecbb
RS
4309 (not (or (memq (char-before) '(?- ?+))
4310 ;; or a class category
4311 (progn
0ec8351b 4312 (c-forward-sexp -2)
785eecbb
RS
4313 (looking-at c-class-key))
4314 )))))
4315 )))
4316
a66cd3ee
MS
4317(defun c-in-knr-argdecl (&optional lim)
4318 ;; Return the position of the first argument declaration if point is
4319 ;; inside a K&R style argument declaration list, nil otherwise.
4320 ;; `c-recognize-knr-p' is not checked. If LIM is non-nil, it's a
4321 ;; position that bounds the backward search for the argument list.
4322 ;;
4323 ;; Note: A declaration level context is assumed; the test can return
d9e94c22
MS
4324 ;; false positives for statements. This test is even more easily
4325 ;; fooled than `c-just-after-func-arglist-p'.
4326
a66cd3ee
MS
4327 (save-excursion
4328 (save-restriction
d9e94c22 4329
a66cd3ee
MS
4330 ;; Go back to the closest preceding normal parenthesis sexp. We
4331 ;; take that as the argument list in the function header. Then
4332 ;; check that it's followed by some symbol before the next ';'
4333 ;; or '{'. If it does, it's the header of the K&R argdecl we're
4334 ;; in.
4335 (if lim (narrow-to-region lim (point)))
d9e94c22
MS
4336 (let ((outside-macro (not (c-query-macro-start)))
4337 paren-end)
4338
4339 (catch 'done
4340 (while (if (and (c-safe (setq paren-end
4341 (c-down-list-backward (point))))
4342 (eq (char-after paren-end) ?\)))
4343 (progn
4344 (goto-char (1+ paren-end))
4345 (if outside-macro
4346 (c-beginning-of-macro)))
4347 (throw 'done nil))))
4348
4349 (and (progn
a66cd3ee
MS
4350 (c-forward-syntactic-ws)
4351 (looking-at "\\w\\|\\s_"))
4352 (c-safe (c-up-list-backward paren-end))
d9e94c22
MS
4353
4354 (save-excursion
4355 ;; If it's a K&R declaration then we're now at the
4356 ;; beginning of the function arglist. Check that there
4357 ;; isn't a '=' before it in this statement since that
4358 ;; means it some kind of initialization instead.
4359 (c-syntactic-skip-backward "^;=}{")
4360 (not (eq (char-before) ?=)))
4361
a66cd3ee 4362 (point))))))
785eecbb
RS
4363
4364(defun c-skip-conditional ()
4365 ;; skip forward over conditional at point, including any predicate
4366 ;; statements in parentheses. No error checking is performed.
0ec8351b
BW
4367 (c-forward-sexp (cond
4368 ;; else if()
a66cd3ee
MS
4369 ((looking-at (concat "\\<else"
4370 "\\([ \t\n]\\|\\\\\n\\)+"
4371 "if\\>\\([^_]\\|$\\)"))
4372 3)
0ec8351b 4373 ;; do, else, try, finally
a66cd3ee
MS
4374 ((looking-at (concat "\\<\\("
4375 "do\\|else\\|try\\|finally"
4376 "\\)\\>\\([^_]\\|$\\)"))
130c507e 4377 1)
ce8c7486 4378 ;; for, if, while, switch, catch, synchronized, foreach
0ec8351b 4379 (t 2))))
785eecbb 4380
a66cd3ee
MS
4381(defun c-after-conditional (&optional lim)
4382 ;; If looking at the token after a conditional then return the
4383 ;; position of its start, otherwise return nil.
4384 (save-excursion
d9e94c22 4385 (and (zerop (c-backward-token-2 1 t lim))
a66cd3ee
MS
4386 (or (looking-at c-block-stmt-1-key)
4387 (and (eq (char-after) ?\()
d9e94c22 4388 (zerop (c-backward-token-2 1 t lim))
a66cd3ee
MS
4389 (looking-at c-block-stmt-2-key)))
4390 (point))))
4391
4392(defsubst c-backward-to-block-anchor (&optional lim)
4393 ;; Assuming point is at a brace that opens a statement block of some
4394 ;; kind, move to the proper anchor point for that block. It might
4395 ;; need to be adjusted further by c-add-stmt-syntax, but the
4396 ;; position at return is suitable as start position for that
4397 ;; function.
4398 (unless (= (point) (c-point 'boi))
4399 (let ((start (c-after-conditional lim)))
4400 (if start
4401 (goto-char start)))))
4402
4403(defun c-backward-to-decl-anchor (&optional lim)
4404 ;; Assuming point is at a brace that opens the block of a top level
4405 ;; declaration of some kind, move to the proper anchor point for
4406 ;; that block.
4407 (unless (= (point) (c-point 'boi))
4408 ;; What we have below is actually an extremely stripped variant of
4409 ;; c-beginning-of-statement-1.
ff959bab 4410 (let ((pos (point)) c-maybe-labelp)
a66cd3ee
MS
4411 ;; Switch syntax table to avoid stopping at line continuations.
4412 (save-restriction
4413 (if lim (narrow-to-region lim (point-max)))
4414 (while (and (progn
4415 (c-backward-syntactic-ws)
4416 (c-safe (goto-char (scan-sexps (point) -1)) t))
ff959bab
MS
4417 (not (c-crosses-statement-barrier-p (point) pos))
4418 (not c-maybe-labelp))
a66cd3ee
MS
4419 (setq pos (point)))
4420 (goto-char pos)))))
4421
ff959bab 4422(defun c-search-decl-header-end ()
a66cd3ee
MS
4423 ;; Search forward for the end of the "header" of the current
4424 ;; declaration. That's the position where the definition body
4425 ;; starts, or the first variable initializer, or the ending
4426 ;; semicolon. I.e. search forward for the closest following
4427 ;; (syntactically relevant) '{', '=' or ';' token. Point is left
4428 ;; _after_ the first found token, or at point-max if none is found.
ff959bab
MS
4429
4430 (let ((base (point)))
4431 (if (c-major-mode-is 'c++-mode)
4432
4433 ;; In C++ we need to take special care to handle operator
4434 ;; tokens and those pesky template brackets.
4435 (while (and
4436 (c-syntactic-re-search-forward "[;{<=]" nil 'move t t)
4437 (or
4438 (c-end-of-current-token base)
4439 ;; Handle operator identifiers, i.e. ignore any
4440 ;; operator token preceded by "operator".
4441 (save-excursion
4442 (and (c-safe (c-backward-sexp) t)
4443 (looking-at "operator\\([^_]\\|$\\)")))
4444 (and (eq (char-before) ?<)
4445 (c-with-syntax-table c++-template-syntax-table
4446 (if (c-safe (goto-char (c-up-list-forward (point))))
4447 t
4448 (goto-char (point-max))
4449 nil)))))
4450 (setq base (point)))
4451
4452 (while (and
4453 (c-syntactic-re-search-forward "[;{=]" nil 'move t t)
4454 (c-end-of-current-token base))
4455 (setq base (point))))))
a66cd3ee
MS
4456
4457(defun c-beginning-of-decl-1 (&optional lim)
4458 ;; Go to the beginning of the current declaration, or the beginning
4459 ;; of the previous one if already at the start of it. Point won't
4460 ;; be moved out of any surrounding paren. Return a cons cell on the
4461 ;; form (MOVE . KNR-POS). MOVE is like the return value from
4462 ;; `c-beginning-of-statement-1'. If point skipped over some K&R
4463 ;; style argument declarations (and they are to be recognized) then
4464 ;; KNR-POS is set to the start of the first such argument
4465 ;; declaration, otherwise KNR-POS is nil. If LIM is non-nil, it's a
4466 ;; position that bounds the backward search.
4467 ;;
4468 ;; NB: Cases where the declaration continues after the block, as in
4469 ;; "struct foo { ... } bar;", are currently recognized as two
4470 ;; declarations, e.g. "struct foo { ... }" and "bar;" in this case.
4471 (catch 'return
4472 (let* ((start (point))
d9e94c22
MS
4473 (last-stmt-start (point))
4474 (move (c-beginning-of-statement-1 lim t t)))
a66cd3ee 4475
a66cd3ee
MS
4476 ;; `c-beginning-of-statement-1' stops at a block start, but we
4477 ;; want to continue if the block doesn't begin a top level
4478 ;; construct, i.e. if it isn't preceded by ';', '}', ':', or bob.
d9e94c22
MS
4479 (let ((beg (point)) tentative-move)
4480 (while (and
4481 ;; Must check with c-opt-method-key in ObjC mode.
4482 (not (and c-opt-method-key
4483 (looking-at c-opt-method-key)))
4484 (/= last-stmt-start (point))
4485 (progn
4486 (c-backward-syntactic-ws lim)
4487 (not (memq (char-before) '(?\; ?} ?: nil))))
4488 ;; Check that we don't move from the first thing in a
4489 ;; macro to its header.
4490 (not (eq (setq tentative-move
4491 (c-beginning-of-statement-1 lim t t))
4492 'macro)))
4493 (setq last-stmt-start beg
4494 beg (point)
4495 move tentative-move))
4496 (goto-char beg))
4497
4498 (when c-recognize-knr-p
4499 (let ((fallback-pos (point)) knr-argdecl-start)
4500 ;; Handle K&R argdecls. Back up after the "statement" jumped
4501 ;; over by `c-beginning-of-statement-1', unless it was the
4502 ;; function body, in which case we're sitting on the opening
4503 ;; brace now. Then test if we're in a K&R argdecl region and
4504 ;; that we started at the other side of the first argdecl in
4505 ;; it.
4506 (unless (eq (char-after) ?{)
4507 (goto-char last-stmt-start))
4508 (if (and (setq knr-argdecl-start (c-in-knr-argdecl lim))
4509 (< knr-argdecl-start start)
4510 (progn
4511 (goto-char knr-argdecl-start)
4512 (not (eq (c-beginning-of-statement-1 lim t t) 'macro))))
4513 (throw 'return
4514 (cons (if (eq (char-after fallback-pos) ?{)
4515 'previous
4516 'same)
4517 knr-argdecl-start))
4518 (goto-char fallback-pos))))
4519
4520 (when c-opt-access-key
4521 ;; Might have ended up before a protection label. This should
4522 ;; perhaps be checked before `c-recognize-knr-p' to be really
4523 ;; accurate, but we know that no language has both.
4524 (while (looking-at c-opt-access-key)
4525 (goto-char (match-end 0))
4526 (c-forward-syntactic-ws)
4527 (when (>= (point) start)
4528 (goto-char start)
4529 (throw 'return (cons 'same nil)))))
4530
4531 ;; `c-beginning-of-statement-1' counts each brace block as a
4532 ;; separate statement, so the result will be 'previous if we've
4533 ;; moved over any. If they were brace list initializers we might
4534 ;; not have moved over a declaration boundary though, so change it
4535 ;; to 'same if we've moved past a '=' before '{', but not ';'.
4536 ;; (This ought to be integrated into `c-beginning-of-statement-1',
4537 ;; so we avoid this extra pass which potentially can search over a
4538 ;; large amount of text.)
4539 (if (and (eq move 'previous)
4540 (c-with-syntax-table (if (c-major-mode-is 'c++-mode)
4541 c++-template-syntax-table
4542 (syntax-table))
4543 (save-excursion
4544 (and (c-syntactic-re-search-forward "[;={]" start t t t)
4545 (eq (char-before) ?=)
4546 (c-syntactic-re-search-forward "[;{]" start t t)
4547 (eq (char-before) ?{)
4548 (c-safe (goto-char (c-up-list-forward (point))) t)
4549 (not (c-syntactic-re-search-forward ";" start t t))))))
4550 (cons 'same nil)
4551 (cons move nil)))))
a66cd3ee
MS
4552
4553(defun c-end-of-decl-1 ()
4554 ;; Assuming point is at the start of a declaration (as detected by
4555 ;; e.g. `c-beginning-of-decl-1'), go to the end of it. Unlike
4556 ;; `c-beginning-of-decl-1', this function handles the case when a
4557 ;; block is followed by identifiers in e.g. struct declarations in C
4558 ;; or C++. If a proper end was found then t is returned, otherwise
4559 ;; point is moved as far as possible within the current sexp and nil
4560 ;; is returned. This function doesn't handle macros; use
4561 ;; `c-end-of-macro' instead in those cases.
ce8c7486 4562 (let ((start (point))
a66cd3ee
MS
4563 (decl-syntax-table (if (c-major-mode-is 'c++-mode)
4564 c++-template-syntax-table
4565 (syntax-table))))
4566 (catch 'return
4567 (c-search-decl-header-end)
4568
4569 (when (and c-recognize-knr-p
4570 (eq (char-before) ?\;)
4571 (c-in-knr-argdecl start))
4572 ;; Stopped at the ';' in a K&R argdecl section which is
4573 ;; detected using the same criteria as in
4574 ;; `c-beginning-of-decl-1'. Move to the following block
4575 ;; start.
d9e94c22 4576 (c-syntactic-re-search-forward "{" nil 'move t))
a66cd3ee
MS
4577
4578 (when (eq (char-before) ?{)
4579 ;; Encountered a block in the declaration. Jump over it.
4580 (condition-case nil
4581 (goto-char (c-up-list-forward (point)))
d9e94c22
MS
4582 (error (goto-char (point-max))
4583 (throw 'return nil)))
a66cd3ee
MS
4584 (if (or (not c-opt-block-decls-with-vars-key)
4585 (save-excursion
4586 (c-with-syntax-table decl-syntax-table
4587 (let ((lim (point)))
4588 (goto-char start)
b3cf7e18
MS
4589 (not (and
4590 ;; Check for `c-opt-block-decls-with-vars-key'
4591 ;; before the first paren.
4592 (c-syntactic-re-search-forward
d9e94c22 4593 (concat "[;=\(\[{]\\|\\("
b3cf7e18
MS
4594 c-opt-block-decls-with-vars-key
4595 "\\)")
d9e94c22 4596 lim t t t)
b3cf7e18
MS
4597 (match-beginning 1)
4598 (not (eq (char-before) ?_))
d9e94c22
MS
4599 ;; Check that the first following paren is
4600 ;; the block.
4601 (c-syntactic-re-search-forward "[;=\(\[{]"
4602 lim t t t)
b3cf7e18 4603 (eq (char-before) ?{)))))))
a66cd3ee
MS
4604 ;; The declaration doesn't have any of the
4605 ;; `c-opt-block-decls-with-vars' keywords in the
4606 ;; beginning, so it ends here at the end of the block.
4607 (throw 'return t)))
4608
4609 (c-with-syntax-table decl-syntax-table
4610 (while (progn
4611 (if (eq (char-before) ?\;)
4612 (throw 'return t))
d9e94c22 4613 (c-syntactic-re-search-forward ";" nil 'move t))))
a66cd3ee 4614 nil)))
ce8c7486
GM
4615
4616(defun c-beginning-of-member-init-list (&optional limit)
4617 ;; Goes to the beginning of a member init list (i.e. just after the
d9e94c22 4618 ;; ':') if inside one. Returns t in that case, nil otherwise.
ce8c7486
GM
4619 (or limit
4620 (setq limit (point-min)))
4621 (skip-chars-forward " \t")
d9e94c22 4622
ce8c7486
GM
4623 (if (eq (char-after) ?,)
4624 (forward-char 1)
4625 (c-backward-syntactic-ws limit))
d9e94c22
MS
4626
4627 (catch 'exit
4628 (while (and (< limit (point))
4629 (eq (char-before) ?,))
4630
4631 ;; this will catch member inits with multiple
4632 ;; line arglists
4633 (forward-char -1)
4634 (c-backward-syntactic-ws limit)
4635 (if (eq (char-before) ?\))
4636 (unless (c-safe (c-backward-sexp 1))
4637 (throw 'exit nil)))
4638 (c-backward-syntactic-ws limit)
4639
4640 ;; Skip over any template arg to the class. This way with a
4641 ;; syntax table is bogus but it'll have to do for now.
4642 (if (and (eq (char-before) ?>)
4643 (c-major-mode-is 'c++-mode))
4644 (c-with-syntax-table c++-template-syntax-table
4645 (unless (c-safe (c-backward-sexp 1))
4646 (throw 'exit nil))))
4647 (c-safe (c-backward-sexp 1))
4648 (c-backward-syntactic-ws limit)
4649
4650 ;; Skip backwards over a fully::qualified::name.
4651 (while (and (eq (char-before) ?:)
4652 (save-excursion
4653 (forward-char -1)
4654 (eq (char-before) ?:)))
4655 (backward-char 2)
4656 (c-safe (c-backward-sexp 1)))
4657
4658 ;; If we've stepped over a number then this is a bitfield.
4659 (when (and c-opt-bitfield-key
4660 (looking-at "[0-9]"))
4661 (throw 'exit nil))
4662
4663 ;; now continue checking
4664 (c-backward-syntactic-ws limit))
4665
4666 (and (< limit (point))
4667 (eq (char-before) ?:))))
ce8c7486 4668
a66cd3ee 4669(defun c-search-uplist-for-classkey (paren-state)
785eecbb 4670 ;; search for the containing class, returning a 2 element vector if
0ec8351b
BW
4671 ;; found. aref 0 contains the bufpos of the boi of the class key
4672 ;; line, and aref 1 contains the bufpos of the open brace.
a66cd3ee
MS
4673 (if (null paren-state)
4674 ;; no paren-state means we cannot be inside a class
785eecbb 4675 nil
a66cd3ee 4676 (let ((carcache (car paren-state))
785eecbb
RS
4677 search-start search-end)
4678 (if (consp carcache)
4679 ;; a cons cell in the first element means that there is some
4680 ;; balanced sexp before the current bufpos. this we can
4681 ;; ignore. the nth 1 and nth 2 elements define for us the
4682 ;; search boundaries
a66cd3ee
MS
4683 (setq search-start (nth 2 paren-state)
4684 search-end (nth 1 paren-state))
785eecbb
RS
4685 ;; if the car was not a cons cell then nth 0 and nth 1 define
4686 ;; for us the search boundaries
a66cd3ee
MS
4687 (setq search-start (nth 1 paren-state)
4688 search-end (nth 0 paren-state)))
785eecbb
RS
4689 ;; if search-end is nil, or if the search-end character isn't an
4690 ;; open brace, we are definitely not in a class
d9e94c22
MS
4691 (if (or (not search-end)
4692 (< search-end (point-min))
4693 (not (eq (char-after search-end) ?{)))
4694 nil
785eecbb
RS
4695 ;; now, we need to look more closely at search-start. if
4696 ;; search-start is nil, then our start boundary is really
4697 ;; point-min.
4698 (if (not search-start)
4699 (setq search-start (point-min))
4700 ;; if search-start is a cons cell, then we can start
4701 ;; searching from the end of the balanced sexp just ahead of
4702 ;; us
4703 (if (consp search-start)
d9e94c22
MS
4704 (setq search-start (cdr search-start))
4705 ;; Otherwise we start searching within the surrounding paren sexp.
4706 (setq search-start (1+ search-start))))
785eecbb
RS
4707 ;; now we can do a quick regexp search from search-start to
4708 ;; search-end and see if we can find a class key. watch for
4709 ;; class like strings in literals
4710 (save-excursion
4711 (save-restriction
4712 (goto-char search-start)
a66cd3ee 4713 (let (foundp class match-end)
785eecbb
RS
4714 (while (and (not foundp)
4715 (progn
a66cd3ee 4716 (c-forward-syntactic-ws search-end)
785eecbb 4717 (> search-end (point)))
d9e94c22
MS
4718 ;; Add one to the search limit, to allow
4719 ;; matching of the "{" in the regexp.
4720 (re-search-forward c-decl-block-key
4721 (1+ search-end)
4722 t))
785eecbb
RS
4723 (setq class (match-beginning 0)
4724 match-end (match-end 0))
a66cd3ee 4725 (goto-char class)
785eecbb 4726 (if (c-in-literal search-start)
a66cd3ee
MS
4727 (goto-char match-end) ; its in a comment or string, ignore
4728 (c-skip-ws-forward)
785eecbb
RS
4729 (setq foundp (vector (c-point 'boi) search-end))
4730 (cond
4731 ;; check for embedded keywords
4732 ((let ((char (char-after (1- class))))
4733 (and char
4734 (memq (char-syntax char) '(?w ?_))))
4735 (goto-char match-end)
4736 (setq foundp nil))
4737 ;; make sure we're really looking at the start of a
a66cd3ee
MS
4738 ;; class definition, and not an ObjC method.
4739 ((and c-opt-method-key
4740 (re-search-forward c-opt-method-key search-end t)
0ec8351b 4741 (not (c-in-literal class)))
785eecbb 4742 (setq foundp nil))
0ec8351b 4743 ;; Check if this is an anonymous inner class.
a66cd3ee
MS
4744 ((and c-opt-inexpr-class-key
4745 (looking-at c-opt-inexpr-class-key))
d9e94c22 4746 (while (and (zerop (c-forward-token-2 1 t))
0ec8351b
BW
4747 (looking-at "(\\|\\w\\|\\s_\\|\\.")))
4748 (if (eq (point) search-end)
4749 ;; We're done. Just trap this case in the cond.
4750 nil
4751 ;; False alarm; all conditions aren't satisfied.
4752 (setq foundp nil)))
785eecbb
RS
4753 ;; Its impossible to define a regexp for this, and
4754 ;; nearly so to do it programmatically.
4755 ;;
4756 ;; ; picks up forward decls
4757 ;; = picks up init lists
4758 ;; ) picks up return types
4759 ;; > picks up templates, but remember that we can
4760 ;; inherit from templates!
4761 ((let ((skipchars "^;=)"))
4762 ;; try to see if we found the `class' keyword
4763 ;; inside a template arg list
4764 (save-excursion
4765 (skip-chars-backward "^<>" search-start)
4766 (if (eq (char-before) ?<)
4767 (setq skipchars (concat skipchars ">"))))
0ec8351b
BW
4768 (while (progn
4769 (skip-chars-forward skipchars search-end)
4770 (c-in-literal class))
4771 (forward-char))
785eecbb
RS
4772 (/= (point) search-end))
4773 (setq foundp nil))
4774 )))
4775 foundp))
4776 )))))
4777
a66cd3ee 4778(defun c-inside-bracelist-p (containing-sexp paren-state)
785eecbb
RS
4779 ;; return the buffer position of the beginning of the brace list
4780 ;; statement if we're inside a brace list, otherwise return nil.
4781 ;; CONTAINING-SEXP is the buffer pos of the innermost containing
130c507e
GM
4782 ;; paren. BRACE-STATE is the remainder of the state of enclosing
4783 ;; braces
785eecbb
RS
4784 ;;
4785 ;; N.B.: This algorithm can potentially get confused by cpp macros
4786 ;; places in inconvenient locations. Its a trade-off we make for
4787 ;; speed.
4788 (or
d9e94c22 4789 ;; This will pick up brace list declarations.
b2acd789
RS
4790 (c-safe
4791 (save-excursion
4792 (goto-char containing-sexp)
0ec8351b 4793 (c-forward-sexp -1)
b2acd789 4794 (let (bracepos)
d9e94c22 4795 (if (and (or (looking-at c-brace-list-key)
0ec8351b 4796 (progn (c-forward-sexp -1)
d9e94c22 4797 (looking-at c-brace-list-key)))
a66cd3ee 4798 (setq bracepos (c-down-list-forward (point)))
b2acd789
RS
4799 (not (c-crosses-statement-barrier-p (point)
4800 (- bracepos 2))))
4801 (point)))))
785eecbb
RS
4802 ;; this will pick up array/aggregate init lists, even if they are nested.
4803 (save-excursion
0ec8351b
BW
4804 (let ((class-key
4805 ;; Pike can have class definitions anywhere, so we must
4806 ;; check for the class key here.
4807 (and (c-major-mode-is 'pike-mode)
a66cd3ee
MS
4808 c-decl-block-key))
4809 bufpos braceassignp lim next-containing)
785eecbb
RS
4810 (while (and (not bufpos)
4811 containing-sexp)
a66cd3ee
MS
4812 (when paren-state
4813 (if (consp (car paren-state))
4814 (setq lim (cdr (car paren-state))
4815 paren-state (cdr paren-state))
4816 (setq lim (car paren-state)))
4817 (when paren-state
4818 (setq next-containing (car paren-state)
4819 paren-state (cdr paren-state))))
785eecbb 4820 (goto-char containing-sexp)
a66cd3ee
MS
4821 (if (c-looking-at-inexpr-block next-containing next-containing)
4822 ;; We're in an in-expression block of some kind. Do not
4823 ;; check nesting. We deliberately set the limit to the
4824 ;; containing sexp, so that c-looking-at-inexpr-block
4825 ;; doesn't check for an identifier before it.
0ec8351b
BW
4826 (setq containing-sexp nil)
4827 ;; see if the open brace is preceded by = or [...] in
4828 ;; this statement, but watch out for operator=
a66cd3ee 4829 (setq braceassignp 'dontknow)
d9e94c22 4830 (c-backward-token-2 1 t lim)
6393fef2 4831 ;; Checks to do only on the first sexp before the brace.
d9e94c22 4832 (when (and c-opt-inexpr-brace-list-key
6393fef2
RS
4833 (eq (char-after) ?\[))
4834 ;; In Java, an initialization brace list may follow
4835 ;; directly after "new Foo[]", so check for a "new"
4836 ;; earlier.
4837 (while (eq braceassignp 'dontknow)
4838 (setq braceassignp
d9e94c22
MS
4839 (cond ((/= (c-backward-token-2 1 t lim) 0) nil)
4840 ((looking-at c-opt-inexpr-brace-list-key) t)
6393fef2
RS
4841 ((looking-at "\\sw\\|\\s_\\|[.[]")
4842 ;; Carry on looking if this is an
4843 ;; identifier (may contain "." in Java)
4844 ;; or another "[]" sexp.
4845 'dontknow)
4846 (t nil)))))
4847 ;; Checks to do on all sexps before the brace, up to the
4848 ;; beginning of the statement.
4849 (while (eq braceassignp 'dontknow)
0ec8351b
BW
4850 (cond ((eq (char-after) ?\;)
4851 (setq braceassignp nil))
4852 ((and class-key
4853 (looking-at class-key))
4854 (setq braceassignp nil))
4855 ((eq (char-after) ?=)
4856 ;; We've seen a =, but must check earlier tokens so
4857 ;; that it isn't something that should be ignored.
4858 (setq braceassignp 'maybe)
4859 (while (and (eq braceassignp 'maybe)
d9e94c22 4860 (zerop (c-backward-token-2 1 t lim)))
0ec8351b
BW
4861 (setq braceassignp
4862 (cond
4863 ;; Check for operator =
a66cd3ee 4864 ((looking-at "operator\\>[^_]") nil)
130c507e
GM
4865 ;; Check for `<opchar>= in Pike.
4866 ((and (c-major-mode-is 'pike-mode)
4867 (or (eq (char-after) ?`)
4868 ;; Special case for Pikes
4869 ;; `[]=, since '[' is not in
4870 ;; the punctuation class.
4871 (and (eq (char-after) ?\[)
4872 (eq (char-before) ?`))))
4873 nil)
0ec8351b
BW
4874 ((looking-at "\\s.") 'maybe)
4875 ;; make sure we're not in a C++ template
4876 ;; argument assignment
a66cd3ee
MS
4877 ((and
4878 (c-major-mode-is 'c++-mode)
4879 (save-excursion
4880 (let ((here (point))
4881 (pos< (progn
4882 (skip-chars-backward "^<>")
4883 (point))))
4884 (and (eq (char-before) ?<)
4885 (not (c-crosses-statement-barrier-p
4886 pos< here))
4887 (not (c-in-literal))
4888 ))))
0ec8351b 4889 nil)
6393fef2
RS
4890 (t t))))))
4891 (if (and (eq braceassignp 'dontknow)
d9e94c22 4892 (/= (c-backward-token-2 1 t lim) 0))
6393fef2
RS
4893 (setq braceassignp nil)))
4894 (if (not braceassignp)
0ec8351b
BW
4895 (if (eq (char-after) ?\;)
4896 ;; Brace lists can't contain a semicolon, so we're done.
4897 (setq containing-sexp nil)
a66cd3ee
MS
4898 ;; Go up one level.
4899 (setq containing-sexp next-containing
4900 lim nil
4901 next-containing nil))
0ec8351b
BW
4902 ;; we've hit the beginning of the aggregate list
4903 (c-beginning-of-statement-1
a66cd3ee 4904 (c-most-enclosing-brace paren-state))
0ec8351b 4905 (setq bufpos (point))))
a66cd3ee 4906 )
785eecbb
RS
4907 bufpos))
4908 ))
4909
0ec8351b
BW
4910(defun c-looking-at-special-brace-list (&optional lim)
4911