Commit | Line | Data |
---|---|---|
785eecbb RS |
1 | ;;; cc-engine.el --- core syntax guessing engine for CC mode |
2 | ||
ab422c4d | 3 | ;; Copyright (C) 1985, 1987, 1992-2013 Free Software Foundation, Inc. |
785eecbb | 4 | |
e309f66c AM |
5 | ;; Authors: 2001- Alan Mackenzie |
6 | ;; 1998- Martin Stjernholm | |
d9e94c22 | 7 | ;; 1992-1999 Barry A. Warsaw |
5858f68c GM |
8 | ;; 1987 Dave Detlefs |
9 | ;; 1987 Stewart Clamen | |
785eecbb | 10 | ;; 1985 Richard M. Stallman |
0ec8351b | 11 | ;; Maintainer: bug-cc-mode@gnu.org |
785eecbb | 12 | ;; Created: 22-Apr-1997 (split from cc-mode.el) |
bd78fa1d CY |
13 | ;; Keywords: c languages |
14 | ;; Package: cc-mode | |
785eecbb RS |
15 | |
16 | ;; This file is part of GNU Emacs. | |
17 | ||
b1fc2b50 | 18 | ;; GNU Emacs is free software: you can redistribute it and/or modify |
785eecbb | 19 | ;; it under the terms of the GNU General Public License as published by |
b1fc2b50 GM |
20 | ;; the Free Software Foundation, either version 3 of the License, or |
21 | ;; (at your option) any later version. | |
785eecbb RS |
22 | |
23 | ;; GNU Emacs is distributed in the hope that it will be useful, | |
24 | ;; but WITHOUT ANY WARRANTY; without even the implied warranty of | |
25 | ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
26 | ;; GNU General Public License for more details. | |
27 | ||
28 | ;; You should have received a copy of the GNU General Public License | |
b1fc2b50 | 29 | ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. |
785eecbb | 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 | |
0386b551 AM |
47 | ;; properties but still don't modify the buffer in a visible way are |
48 | ;; said to do "hidden buffer changes". They should be used within | |
49 | ;; `c-save-buffer-state' or a similar function that saves and restores | |
50 | ;; buffer modifiedness, disables buffer change hooks, etc. | |
d9e94c22 | 51 | ;; |
0386b551 AM |
52 | ;; Interactive functions are assumed to not do hidden buffer changes, |
53 | ;; except in the specific parts of them that do real changes. | |
d9e94c22 | 54 | ;; |
0386b551 AM |
55 | ;; Lineup functions are assumed to do hidden buffer changes. They |
56 | ;; must not do real changes, though. | |
d9e94c22 | 57 | ;; |
0386b551 AM |
58 | ;; All other functions that do hidden buffer changes have that noted |
59 | ;; in their doc string or comment. | |
60 | ;; | |
61 | ;; The intention with this system is to avoid wrapping every leaf | |
62 | ;; function that do hidden buffer changes inside | |
63 | ;; `c-save-buffer-state'. It should be used as near the top of the | |
64 | ;; interactive functions as possible. | |
65 | ;; | |
66 | ;; Functions called during font locking are allowed to do hidden | |
67 | ;; buffer changes since the font-lock package run them in a context | |
68 | ;; similar to `c-save-buffer-state' (in fact, that function is heavily | |
69 | ;; inspired by `save-buffer-state' in the font-lock package). | |
d9e94c22 MS |
70 | |
71 | ;; Use of text properties | |
72 | ;; | |
73 | ;; CC Mode uses several text properties internally to mark up various | |
74 | ;; positions, e.g. to improve speed and to eliminate glitches in | |
75 | ;; interactive refontification. | |
76 | ;; | |
7bfc3fdb MS |
77 | ;; Note: This doc is for internal use only. Other packages should not |
78 | ;; assume that these text properties are used as described here. | |
79 | ;; | |
0ec1d2c5 AM |
80 | ;; 'category |
81 | ;; Used for "indirection". With its help, some other property can | |
82 | ;; be cheaply and easily switched on or off everywhere it occurs. | |
83 | ;; | |
d9e94c22 | 84 | ;; 'syntax-table |
c8018ede AM |
85 | ;; Used to modify the syntax of some characters. It is used to |
86 | ;; mark the "<" and ">" of angle bracket parens with paren syntax, and | |
87 | ;; to "hide" obtrusive characters in preprocessor lines. | |
d9e94c22 MS |
88 | ;; |
89 | ;; This property is used on single characters and is therefore | |
90 | ;; always treated as front and rear nonsticky (or start and end open | |
91 | ;; in XEmacs vocabulary). It's therefore installed on | |
92 | ;; `text-property-default-nonsticky' if that variable exists (Emacs | |
93 | ;; >= 21). | |
94 | ;; | |
95 | ;; 'c-is-sws and 'c-in-sws | |
96 | ;; Used by `c-forward-syntactic-ws' and `c-backward-syntactic-ws' to | |
97 | ;; speed them up. See the comment blurb before `c-put-is-sws' | |
98 | ;; below for further details. | |
99 | ;; | |
100 | ;; 'c-type | |
101 | ;; This property is used on single characters to mark positions with | |
0386b551 AM |
102 | ;; special syntactic relevance of various sorts. Its primary use is |
103 | ;; to avoid glitches when multiline constructs are refontified | |
d9e94c22 MS |
104 | ;; interactively (on font lock decoration level 3). It's cleared in |
105 | ;; a region before it's fontified and is then put on relevant chars | |
106 | ;; in that region as they are encountered during the fontification. | |
107 | ;; The value specifies the kind of position: | |
108 | ;; | |
109 | ;; 'c-decl-arg-start | |
110 | ;; Put on the last char of the token preceding each declaration | |
111 | ;; inside a declaration style arglist (typically in a function | |
112 | ;; prototype). | |
113 | ;; | |
114 | ;; 'c-decl-end | |
115 | ;; Put on the last char of the token preceding a declaration. | |
116 | ;; This is used in cases where declaration boundaries can't be | |
117 | ;; recognized simply by looking for a token like ";" or "}". | |
118 | ;; `c-type-decl-end-used' must be set if this is used (see also | |
119 | ;; `c-find-decl-spots'). | |
120 | ;; | |
121 | ;; 'c-<>-arg-sep | |
122 | ;; Put on the commas that separate arguments in angle bracket | |
123 | ;; arglists like C++ template arglists. | |
124 | ;; | |
125 | ;; 'c-decl-id-start and 'c-decl-type-start | |
126 | ;; Put on the last char of the token preceding each declarator | |
127 | ;; in the declarator list of a declaration. They are also used | |
128 | ;; between the identifiers cases like enum declarations. | |
129 | ;; 'c-decl-type-start is used when the declarators are types, | |
130 | ;; 'c-decl-id-start otherwise. | |
131 | ;; | |
132 | ;; 'c-awk-NL-prop | |
133 | ;; Used in AWK mode to mark the various kinds of newlines. See | |
134 | ;; cc-awk.el. | |
135 | ||
3afbc435 PJ |
136 | ;;; Code: |
137 | ||
0ec8351b | 138 | (eval-when-compile |
51f606de | 139 | (let ((load-path |
130c507e GM |
140 | (if (and (boundp 'byte-compile-dest-file) |
141 | (stringp byte-compile-dest-file)) | |
142 | (cons (file-name-directory byte-compile-dest-file) load-path) | |
51f606de | 143 | load-path))) |
d9e94c22 | 144 | (load "cc-bytecomp" nil t))) |
130c507e GM |
145 | |
146 | (cc-require 'cc-defs) | |
d9e94c22 | 147 | (cc-require-when-compile 'cc-langs) |
130c507e | 148 | (cc-require 'cc-vars) |
d9e94c22 | 149 | |
130c507e GM |
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 | ||
0386b551 AM |
174 | ;; The electric flag (toggled by `c-toggle-electric-state'). |
175 | ;; If t, electric actions (like automatic reindentation, and (if | |
176 | ;; c-auto-newline is also set) auto newlining) will happen when an electric | |
177 | ;; key like `{' is pressed (or an electric keyword like `else'). | |
178 | (defvar c-electric-flag t) | |
179 | (make-variable-buffer-local 'c-electric-flag) | |
180 | ||
d9e94c22 MS |
181 | ;; Internal state of auto newline feature. |
182 | (defvar c-auto-newline nil) | |
183 | (make-variable-buffer-local 'c-auto-newline) | |
184 | ||
0386b551 | 185 | ;; Included in the mode line to indicate the active submodes. |
cb694ab7 AM |
186 | ;; (defvar c-submode-indicators nil) |
187 | ;; (make-variable-buffer-local 'c-submode-indicators) | |
d9e94c22 | 188 | |
a66cd3ee MS |
189 | (defun c-calculate-state (arg prevstate) |
190 | ;; Calculate the new state of PREVSTATE, t or nil, based on arg. If | |
191 | ;; arg is nil or zero, toggle the state. If arg is negative, turn | |
192 | ;; the state off, and if arg is positive, turn the state on | |
193 | (if (or (not arg) | |
194 | (zerop (setq arg (prefix-numeric-value arg)))) | |
195 | (not prevstate) | |
196 | (> arg 0))) | |
197 | ||
d9e94c22 | 198 | \f |
037558bf MS |
199 | ;; Basic handling of preprocessor directives. |
200 | ||
201 | ;; This is a dynamically bound cache used together with | |
202 | ;; `c-query-macro-start' and `c-query-and-set-macro-start'. It only | |
203 | ;; works as long as point doesn't cross a macro boundary. | |
204 | (defvar c-macro-start 'unknown) | |
205 | ||
206 | (defsubst c-query-and-set-macro-start () | |
037558bf MS |
207 | (if (symbolp c-macro-start) |
208 | (setq c-macro-start (save-excursion | |
0386b551 AM |
209 | (c-save-buffer-state () |
210 | (and (c-beginning-of-macro) | |
211 | (point))))) | |
037558bf MS |
212 | c-macro-start)) |
213 | ||
214 | (defsubst c-query-macro-start () | |
037558bf MS |
215 | (if (symbolp c-macro-start) |
216 | (save-excursion | |
0386b551 AM |
217 | (c-save-buffer-state () |
218 | (and (c-beginning-of-macro) | |
219 | (point)))) | |
037558bf MS |
220 | c-macro-start)) |
221 | ||
56d093a9 AM |
222 | ;; One element macro cache to cope with continual movement within very large |
223 | ;; CPP macros. | |
224 | (defvar c-macro-cache nil) | |
225 | (make-variable-buffer-local 'c-macro-cache) | |
226 | ;; Nil or cons of the bounds of the most recent CPP form probed by | |
227 | ;; `c-beginning-of-macro', `c-end-of-macro' or `c-syntactic-end-of-macro'. | |
228 | ;; The cdr will be nil if we know only the start of the CPP form. | |
229 | (defvar c-macro-cache-start-pos nil) | |
230 | (make-variable-buffer-local 'c-macro-cache-start-pos) | |
231 | ;; The starting position from where we determined `c-macro-cache'. | |
232 | (defvar c-macro-cache-syntactic nil) | |
233 | (make-variable-buffer-local 'c-macro-cache-syntactic) | |
234 | ;; non-nil iff `c-macro-cache' has both elements set AND the cdr is at a | |
235 | ;; syntactic end of macro, not merely an apparent one. | |
236 | ||
237 | (defun c-invalidate-macro-cache (beg end) | |
238 | ;; Called from a before-change function. If the change region is before or | |
f003f294 | 239 | ;; in the macro characterized by `c-macro-cache' etc., nullify it |
56d093a9 AM |
240 | ;; appropriately. BEG and END are the standard before-change-functions |
241 | ;; parameters. END isn't used. | |
242 | (cond | |
243 | ((null c-macro-cache)) | |
244 | ((< beg (car c-macro-cache)) | |
245 | (setq c-macro-cache nil | |
246 | c-macro-cache-start-pos nil | |
247 | c-macro-cache-syntactic nil)) | |
248 | ((and (cdr c-macro-cache) | |
249 | (< beg (cdr c-macro-cache))) | |
250 | (setcdr c-macro-cache nil) | |
251 | (setq c-macro-cache-start-pos beg | |
252 | c-macro-cache-syntactic nil)))) | |
253 | ||
037558bf MS |
254 | (defun c-beginning-of-macro (&optional lim) |
255 | "Go to the beginning of a preprocessor directive. | |
256 | Leave point at the beginning of the directive and return t if in one, | |
257 | otherwise return nil and leave point unchanged. | |
258 | ||
0386b551 AM |
259 | Note that this function might do hidden buffer changes. See the |
260 | comment at the start of cc-engine.el for more info." | |
56d093a9 AM |
261 | (let ((here (point))) |
262 | (when c-opt-cpp-prefix | |
263 | (if (and (car c-macro-cache) | |
264 | (>= (point) (car c-macro-cache)) | |
265 | (or (and (cdr c-macro-cache) | |
266 | (<= (point) (cdr c-macro-cache))) | |
267 | (<= (point) c-macro-cache-start-pos))) | |
268 | (unless (< (car c-macro-cache) (or lim (point-min))) | |
269 | (progn (goto-char (max (or lim (point-min)) (car c-macro-cache))) | |
270 | (setq c-macro-cache-start-pos | |
271 | (max c-macro-cache-start-pos here)) | |
272 | t)) | |
273 | (setq c-macro-cache nil | |
274 | c-macro-cache-start-pos nil | |
275 | c-macro-cache-syntactic nil) | |
276 | ||
277 | (save-restriction | |
278 | (if lim (narrow-to-region lim (point-max))) | |
279 | (beginning-of-line) | |
280 | (while (eq (char-before (1- (point))) ?\\) | |
281 | (forward-line -1)) | |
282 | (back-to-indentation) | |
283 | (if (and (<= (point) here) | |
284 | (looking-at c-opt-cpp-start)) | |
285 | (progn | |
286 | (setq c-macro-cache (cons (point) nil) | |
287 | c-macro-cache-start-pos here) | |
288 | t) | |
289 | (goto-char here) | |
290 | nil)))))) | |
037558bf MS |
291 | |
292 | (defun c-end-of-macro () | |
293 | "Go to the end of a preprocessor directive. | |
0386b551 AM |
294 | More accurately, move the point to the end of the closest following |
295 | line that doesn't end with a line continuation backslash - no check is | |
296 | done that the point is inside a cpp directive to begin with. | |
037558bf | 297 | |
0386b551 AM |
298 | Note that this function might do hidden buffer changes. See the |
299 | comment at the start of cc-engine.el for more info." | |
56d093a9 AM |
300 | (if (and (cdr c-macro-cache) |
301 | (<= (point) (cdr c-macro-cache)) | |
302 | (>= (point) (car c-macro-cache))) | |
303 | (goto-char (cdr c-macro-cache)) | |
304 | (unless (and (car c-macro-cache) | |
305 | (<= (point) c-macro-cache-start-pos) | |
306 | (>= (point) (car c-macro-cache))) | |
307 | (setq c-macro-cache nil | |
308 | c-macro-cache-start-pos nil | |
309 | c-macro-cache-syntactic nil)) | |
310 | (while (progn | |
311 | (end-of-line) | |
312 | (when (and (eq (char-before) ?\\) | |
313 | (not (eobp))) | |
314 | (forward-char) | |
315 | t))) | |
316 | (when (car c-macro-cache) | |
317 | (setcdr c-macro-cache (point))))) | |
037558bf | 318 | |
0ec1d2c5 AM |
319 | (defun c-syntactic-end-of-macro () |
320 | ;; Go to the end of a CPP directive, or a "safe" pos just before. | |
321 | ;; | |
322 | ;; This is normally the end of the next non-escaped line. A "safe" | |
323 | ;; position is one not within a string or comment. (The EOL on a line | |
324 | ;; comment is NOT "safe"). | |
325 | ;; | |
326 | ;; This function must only be called from the beginning of a CPP construct. | |
327 | ;; | |
328 | ;; Note that this function might do hidden buffer changes. See the comment | |
329 | ;; at the start of cc-engine.el for more info. | |
330 | (let* ((here (point)) | |
331 | (there (progn (c-end-of-macro) (point))) | |
56d093a9 AM |
332 | s) |
333 | (unless c-macro-cache-syntactic | |
334 | (setq s (parse-partial-sexp here there)) | |
335 | (while (and (or (nth 3 s) ; in a string | |
336 | (nth 4 s)) ; in a comment (maybe at end of line comment) | |
337 | (> there here)) ; No infinite loops, please. | |
338 | (setq there (1- (nth 8 s))) | |
339 | (setq s (parse-partial-sexp here there))) | |
340 | (setq c-macro-cache-syntactic (car c-macro-cache))) | |
0ec1d2c5 AM |
341 | (point))) |
342 | ||
51c9af45 AM |
343 | (defun c-forward-over-cpp-define-id () |
344 | ;; Assuming point is at the "#" that introduces a preprocessor | |
345 | ;; directive, it's moved forward to the end of the identifier which is | |
346 | ;; "#define"d (or whatever c-opt-cpp-macro-define specifies). Non-nil | |
347 | ;; is returned in this case, in all other cases nil is returned and | |
348 | ;; point isn't moved. | |
349 | ;; | |
350 | ;; This function might do hidden buffer changes. | |
351 | (when (and c-opt-cpp-macro-define-id | |
352 | (looking-at c-opt-cpp-macro-define-id)) | |
353 | (goto-char (match-end 0)))) | |
354 | ||
037558bf MS |
355 | (defun c-forward-to-cpp-define-body () |
356 | ;; Assuming point is at the "#" that introduces a preprocessor | |
357 | ;; directive, it's moved forward to the start of the definition body | |
0386b551 AM |
358 | ;; if it's a "#define" (or whatever c-opt-cpp-macro-define |
359 | ;; specifies). Non-nil is returned in this case, in all other cases | |
360 | ;; nil is returned and point isn't moved. | |
361 | ;; | |
362 | ;; This function might do hidden buffer changes. | |
363 | (when (and c-opt-cpp-macro-define-start | |
364 | (looking-at c-opt-cpp-macro-define-start) | |
037558bf MS |
365 | (not (= (match-end 0) (c-point 'eol)))) |
366 | (goto-char (match-end 0)))) | |
367 | ||
368 | \f | |
d9e94c22 MS |
369 | ;;; Basic utility functions. |
370 | ||
0386b551 | 371 | (defun c-syntactic-content (from to paren-level) |
d9e94c22 MS |
372 | ;; Return the given region as a string where all syntactic |
373 | ;; whitespace is removed or, where necessary, replaced with a single | |
0386b551 AM |
374 | ;; space. If PAREN-LEVEL is given then all parens in the region are |
375 | ;; collapsed to "()", "[]" etc. | |
376 | ;; | |
377 | ;; This function might do hidden buffer changes. | |
378 | ||
d9e94c22 | 379 | (save-excursion |
0386b551 AM |
380 | (save-restriction |
381 | (narrow-to-region from to) | |
382 | (goto-char from) | |
383 | (let* ((parts (list nil)) (tail parts) pos in-paren) | |
384 | ||
385 | (while (re-search-forward c-syntactic-ws-start to t) | |
386 | (goto-char (setq pos (match-beginning 0))) | |
387 | (c-forward-syntactic-ws) | |
388 | (if (= (point) pos) | |
389 | (forward-char) | |
390 | ||
391 | (when paren-level | |
392 | (save-excursion | |
393 | (setq in-paren (= (car (parse-partial-sexp from pos 1)) 1) | |
394 | pos (point)))) | |
395 | ||
396 | (if (and (> pos from) | |
397 | (< (point) to) | |
398 | (looking-at "\\w\\|\\s_") | |
399 | (save-excursion | |
400 | (goto-char (1- pos)) | |
401 | (looking-at "\\w\\|\\s_"))) | |
402 | (progn | |
403 | (setcdr tail (list (buffer-substring-no-properties from pos) | |
404 | " ")) | |
405 | (setq tail (cddr tail))) | |
406 | (setcdr tail (list (buffer-substring-no-properties from pos))) | |
407 | (setq tail (cdr tail))) | |
408 | ||
409 | (when in-paren | |
410 | (when (= (car (parse-partial-sexp pos to -1)) -1) | |
411 | (setcdr tail (list (buffer-substring-no-properties | |
412 | (1- (point)) (point)))) | |
413 | (setq tail (cdr tail)))) | |
414 | ||
415 | (setq from (point)))) | |
416 | ||
417 | (setcdr tail (list (buffer-substring-no-properties from to))) | |
418 | (apply 'concat (cdr parts)))))) | |
419 | ||
420 | (defun c-shift-line-indentation (shift-amt) | |
421 | ;; Shift the indentation of the current line with the specified | |
422 | ;; amount (positive inwards). The buffer is modified only if | |
423 | ;; SHIFT-AMT isn't equal to zero. | |
424 | (let ((pos (- (point-max) (point))) | |
425 | (c-macro-start c-macro-start) | |
426 | tmp-char-inserted) | |
427 | (if (zerop shift-amt) | |
428 | nil | |
429 | ;; If we're on an empty line inside a macro, we take the point | |
430 | ;; to be at the current indentation and shift it to the | |
431 | ;; appropriate column. This way we don't treat the extra | |
432 | ;; whitespace out to the line continuation as indentation. | |
433 | (when (and (c-query-and-set-macro-start) | |
434 | (looking-at "[ \t]*\\\\$") | |
435 | (save-excursion | |
436 | (skip-chars-backward " \t") | |
437 | (bolp))) | |
438 | (insert ?x) | |
439 | (backward-char) | |
440 | (setq tmp-char-inserted t)) | |
441 | (unwind-protect | |
442 | (let ((col (current-indentation))) | |
443 | (delete-region (c-point 'bol) (c-point 'boi)) | |
444 | (beginning-of-line) | |
445 | (indent-to (+ col shift-amt))) | |
446 | (when tmp-char-inserted | |
447 | (delete-char 1)))) | |
448 | ;; If initial point was within line's indentation and we're not on | |
449 | ;; a line with a line continuation in a macro, position after the | |
450 | ;; indentation. Else stay at same point in text. | |
451 | (if (and (< (point) (c-point 'boi)) | |
452 | (not tmp-char-inserted)) | |
453 | (back-to-indentation) | |
454 | (if (> (- (point-max) pos) (point)) | |
455 | (goto-char (- (point-max) pos)))))) | |
d9e94c22 MS |
456 | |
457 | (defsubst c-keyword-sym (keyword) | |
458 | ;; Return non-nil if the string KEYWORD is a known keyword. More | |
459 | ;; precisely, the value is the symbol for the keyword in | |
460 | ;; `c-keywords-obarray'. | |
461 | (intern-soft keyword c-keywords-obarray)) | |
462 | ||
463 | (defsubst c-keyword-member (keyword-sym lang-constant) | |
464 | ;; Return non-nil if the symbol KEYWORD-SYM, as returned by | |
465 | ;; `c-keyword-sym', is a member of LANG-CONSTANT, which is the name | |
466 | ;; of a language constant that ends with "-kwds". If KEYWORD-SYM is | |
467 | ;; nil then the result is nil. | |
468 | (get keyword-sym lang-constant)) | |
469 | ||
470 | ;; String syntax chars, suitable for skip-syntax-(forward|backward). | |
471 | (defconst c-string-syntax (if (memq 'gen-string-delim c-emacs-features) | |
472 | "\"|" | |
473 | "\"")) | |
474 | ||
0386b551 | 475 | ;; Regexp matching string limit syntax. |
d9e94c22 MS |
476 | (defconst c-string-limit-regexp (if (memq 'gen-string-delim c-emacs-features) |
477 | "\\s\"\\|\\s|" | |
478 | "\\s\"")) | |
479 | ||
0386b551 AM |
480 | ;; Regexp matching WS followed by string limit syntax. |
481 | (defconst c-ws*-string-limit-regexp | |
482 | (concat "[ \t]*\\(" c-string-limit-regexp "\\)")) | |
483 | ||
d9e94c22 MS |
484 | ;; Holds formatted error strings for the few cases where parse errors |
485 | ;; are reported. | |
a66cd3ee | 486 | (defvar c-parsing-error nil) |
d9e94c22 MS |
487 | (make-variable-buffer-local 'c-parsing-error) |
488 | ||
489 | (defun c-echo-parsing-error (&optional quiet) | |
d9e94c22 MS |
490 | (when (and c-report-syntactic-errors c-parsing-error (not quiet)) |
491 | (c-benign-error "%s" c-parsing-error)) | |
492 | c-parsing-error) | |
493 | ||
494 | ;; Faces given to comments and string literals. This is used in some | |
495 | ;; situations to speed up recognition; it isn't mandatory that font | |
496 | ;; locking is in use. This variable is extended with the face in | |
497 | ;; `c-doc-face-name' when fontification is activated in cc-fonts.el. | |
1e330469 | 498 | (defvar c-literal-faces |
0386b551 AM |
499 | (append '(font-lock-comment-face font-lock-string-face) |
500 | (when (facep 'font-lock-comment-delimiter-face) | |
501 | ;; New in Emacs 22. | |
502 | '(font-lock-comment-delimiter-face)))) | |
503 | ||
504 | (defsubst c-put-c-type-property (pos value) | |
505 | ;; Put a c-type property with the given value at POS. | |
506 | (c-put-char-property pos 'c-type value)) | |
507 | ||
508 | (defun c-clear-c-type-property (from to value) | |
5a89f0a7 | 509 | ;; Remove all occurrences of the c-type property that has the given |
0386b551 AM |
510 | ;; value in the region between FROM and TO. VALUE is assumed to not |
511 | ;; be nil. | |
512 | ;; | |
513 | ;; Note: This assumes that c-type is put on single chars only; it's | |
514 | ;; very inefficient if matching properties cover large regions. | |
515 | (save-excursion | |
516 | (goto-char from) | |
517 | (while (progn | |
518 | (when (eq (get-text-property (point) 'c-type) value) | |
519 | (c-clear-char-property (point) 'c-type)) | |
520 | (goto-char (next-single-property-change (point) 'c-type nil to)) | |
521 | (< (point) to))))) | |
037558bf | 522 | |
d9e94c22 MS |
523 | \f |
524 | ;; Some debug tools to visualize various special positions. This | |
525 | ;; debug code isn't as portable as the rest of CC Mode. | |
526 | ||
527 | (cc-bytecomp-defun overlays-in) | |
528 | (cc-bytecomp-defun overlay-get) | |
529 | (cc-bytecomp-defun overlay-start) | |
530 | (cc-bytecomp-defun overlay-end) | |
531 | (cc-bytecomp-defun delete-overlay) | |
532 | (cc-bytecomp-defun overlay-put) | |
533 | (cc-bytecomp-defun make-overlay) | |
534 | ||
535 | (defun c-debug-add-face (beg end face) | |
536 | (c-save-buffer-state ((overlays (overlays-in beg end)) overlay) | |
537 | (while overlays | |
538 | (setq overlay (car overlays) | |
539 | overlays (cdr overlays)) | |
540 | (when (eq (overlay-get overlay 'face) face) | |
541 | (setq beg (min beg (overlay-start overlay)) | |
542 | end (max end (overlay-end overlay))) | |
543 | (delete-overlay overlay))) | |
544 | (overlay-put (make-overlay beg end) 'face face))) | |
545 | ||
546 | (defun c-debug-remove-face (beg end face) | |
547 | (c-save-buffer-state ((overlays (overlays-in beg end)) overlay | |
548 | (ol-beg beg) (ol-end end)) | |
549 | (while overlays | |
550 | (setq overlay (car overlays) | |
551 | overlays (cdr overlays)) | |
552 | (when (eq (overlay-get overlay 'face) face) | |
553 | (setq ol-beg (min ol-beg (overlay-start overlay)) | |
554 | ol-end (max ol-end (overlay-end overlay))) | |
555 | (delete-overlay overlay))) | |
556 | (when (< ol-beg beg) | |
557 | (overlay-put (make-overlay ol-beg beg) 'face face)) | |
558 | (when (> ol-end end) | |
559 | (overlay-put (make-overlay end ol-end) 'face face)))) | |
560 | ||
561 | \f | |
562 | ;; `c-beginning-of-statement-1' and accompanying stuff. | |
130c507e | 563 | |
64001211 RS |
564 | ;; KLUDGE ALERT: c-maybe-labelp is used to pass information between |
565 | ;; c-crosses-statement-barrier-p and c-beginning-of-statement-1. A | |
566 | ;; better way should be implemented, but this will at least shut up | |
567 | ;; the byte compiler. | |
0386b551 | 568 | (defvar c-maybe-labelp) |
64001211 | 569 | |
d9e94c22 MS |
570 | ;; New awk-compatible version of c-beginning-of-statement-1, ACM 2002/6/22 |
571 | ||
a66cd3ee MS |
572 | ;; Macros used internally in c-beginning-of-statement-1 for the |
573 | ;; automaton actions. | |
574 | (defmacro c-bos-push-state () | |
575 | '(setq stack (cons (cons state saved-pos) | |
576 | stack))) | |
577 | (defmacro c-bos-pop-state (&optional do-if-done) | |
578 | `(if (setq state (car (car stack)) | |
579 | saved-pos (cdr (car stack)) | |
580 | stack (cdr stack)) | |
581 | t | |
582 | ,do-if-done | |
583 | (throw 'loop nil))) | |
584 | (defmacro c-bos-pop-state-and-retry () | |
585 | '(throw 'loop (setq state (car (car stack)) | |
586 | saved-pos (cdr (car stack)) | |
587 | ;; Throw nil if stack is empty, else throw non-nil. | |
588 | stack (cdr stack)))) | |
589 | (defmacro c-bos-save-pos () | |
590 | '(setq saved-pos (vector pos tok ptok pptok))) | |
591 | (defmacro c-bos-restore-pos () | |
592 | '(unless (eq (elt saved-pos 0) start) | |
593 | (setq pos (elt saved-pos 0) | |
594 | tok (elt saved-pos 1) | |
595 | ptok (elt saved-pos 2) | |
596 | pptok (elt saved-pos 3)) | |
597 | (goto-char pos) | |
598 | (setq sym nil))) | |
599 | (defmacro c-bos-save-error-info (missing got) | |
600 | `(setq saved-pos (vector pos ,missing ,got))) | |
601 | (defmacro c-bos-report-error () | |
602 | '(unless noerror | |
603 | (setq c-parsing-error | |
604 | (format "No matching `%s' found for `%s' on line %d" | |
605 | (elt saved-pos 1) | |
606 | (elt saved-pos 2) | |
607 | (1+ (count-lines (point-min) | |
608 | (c-point 'bol (elt saved-pos 0)))))))) | |
609 | ||
610 | (defun c-beginning-of-statement-1 (&optional lim ignore-labels | |
611 | noerror comma-delim) | |
612 | "Move to the start of the current statement or declaration, or to | |
613 | the previous one if already at the beginning of one. Only | |
614 | statements/declarations on the same level are considered, i.e. don't | |
615 | move into or out of sexps (not even normal expression parentheses). | |
616 | ||
5a89f0a7 | 617 | If point is already at the earliest statement within braces or parens, |
a85fd6da AM |
618 | this function doesn't move back into any whitespace preceding it; it |
619 | returns 'same in this case. | |
620 | ||
d9e94c22 MS |
621 | Stop at statement continuation tokens like \"else\", \"catch\", |
622 | \"finally\" and the \"while\" in \"do ... while\" if the start point | |
623 | is within the continuation. If starting at such a token, move to the | |
624 | corresponding statement start. If at the beginning of a statement, | |
625 | move to the closest containing statement if there is any. This might | |
626 | also stop at a continuation clause. | |
a66cd3ee | 627 | |
0386b551 AM |
628 | Labels are treated as part of the following statements if |
629 | IGNORE-LABELS is non-nil. (FIXME: Doesn't work if we stop at a known | |
cb694ab7 AM |
630 | statement start keyword.) Otherwise, each label is treated as a |
631 | separate statement. | |
a66cd3ee | 632 | |
cb694ab7 AM |
633 | Macros are ignored \(i.e. skipped over) unless point is within one, in |
634 | which case the content of the macro is treated as normal code. Aside | |
635 | from any normal statement starts found in it, stop at the first token | |
636 | of the content in the macro, i.e. the expression of an \"#if\" or the | |
637 | start of the definition in a \"#define\". Also stop at start of | |
638 | macros before leaving them. | |
a66cd3ee | 639 | |
a85fd6da | 640 | Return: |
d28e7f28 | 641 | 'label if stopped at a label or \"case...:\" or \"default:\"; |
a85fd6da AM |
642 | 'same if stopped at the beginning of the current statement; |
643 | 'up if stepped to a containing statement; | |
644 | 'previous if stepped to a preceding statement; | |
645 | 'beginning if stepped from a statement continuation clause to | |
646 | its start clause; or | |
647 | 'macro if stepped to a macro start. | |
648 | Note that 'same and not 'label is returned if stopped at the same | |
649 | label without crossing the colon character. | |
a66cd3ee MS |
650 | |
651 | LIM may be given to limit the search. If the search hits the limit, | |
652 | point will be left at the closest following token, or at the start | |
653 | position if that is less ('same is returned in this case). | |
654 | ||
655 | NOERROR turns off error logging to `c-parsing-error'. | |
656 | ||
cb694ab7 AM |
657 | Normally only ';' and virtual semicolons are considered to delimit |
658 | statements, but if COMMA-DELIM is non-nil then ',' is treated | |
659 | as a delimiter too. | |
0386b551 AM |
660 | |
661 | Note that this function might do hidden buffer changes. See the | |
662 | comment at the start of cc-engine.el for more info." | |
a66cd3ee | 663 | |
d9e94c22 MS |
664 | ;; The bulk of this function is a pushdown automaton that looks at statement |
665 | ;; boundaries and the tokens (such as "while") in c-opt-block-stmt-key. Its | |
666 | ;; purpose is to keep track of nested statements, ensuring that such | |
5a89f0a7 | 667 | ;; statements are skipped over in their entirety (somewhat akin to what C-M-p |
d9e94c22 | 668 | ;; does with nested braces/brackets/parentheses). |
a66cd3ee MS |
669 | ;; |
670 | ;; Note: The position of a boundary is the following token. | |
671 | ;; | |
d9e94c22 MS |
672 | ;; Beginning with the current token (the one following point), move back one |
673 | ;; sexp at a time (where a sexp is, more or less, either a token or the | |
674 | ;; entire contents of a brace/bracket/paren pair). Each time a statement | |
675 | ;; boundary is crossed or a "while"-like token is found, update the state of | |
676 | ;; the PDA. Stop at the beginning of a statement when the stack (holding | |
677 | ;; nested statement info) is empty and the position has been moved. | |
678 | ;; | |
679 | ;; The following variables constitute the PDA: | |
680 | ;; | |
681 | ;; sym: This is either the "while"-like token (e.g. 'for) we've just | |
682 | ;; scanned back over, 'boundary if we've just gone back over a | |
683 | ;; statement boundary, or nil otherwise. | |
684 | ;; state: takes one of the values (nil else else-boundary while | |
685 | ;; while-boundary catch catch-boundary). | |
686 | ;; nil means "no "while"-like token yet scanned". | |
687 | ;; 'else, for example, means "just gone back over an else". | |
688 | ;; 'else-boundary means "just gone back over a statement boundary | |
689 | ;; immediately after having gone back over an else". | |
690 | ;; saved-pos: A vector of either saved positions (tok ptok pptok, etc.) or | |
691 | ;; of error reporting information. | |
692 | ;; stack: The stack onto which the PDA pushes its state. Each entry | |
693 | ;; consists of a saved value of state and saved-pos. An entry is | |
694 | ;; pushed when we move back over a "continuation" token (e.g. else) | |
695 | ;; and popped when we encounter the corresponding opening token | |
696 | ;; (e.g. if). | |
697 | ;; | |
698 | ;; | |
b414f371 | 699 | ;; The following diagram briefly outlines the PDA. |
a66cd3ee MS |
700 | ;; |
701 | ;; Common state: | |
d9e94c22 MS |
702 | ;; "else": Push state, goto state `else'. |
703 | ;; "while": Push state, goto state `while'. | |
704 | ;; "catch" or "finally": Push state, goto state `catch'. | |
705 | ;; boundary: Pop state. | |
a66cd3ee MS |
706 | ;; other: Do nothing special. |
707 | ;; | |
d9e94c22 MS |
708 | ;; State `else': |
709 | ;; boundary: Goto state `else-boundary'. | |
710 | ;; other: Error, pop state, retry token. | |
711 | ;; | |
712 | ;; State `else-boundary': | |
713 | ;; "if": Pop state. | |
714 | ;; boundary: Error, pop state. | |
715 | ;; other: See common state. | |
716 | ;; | |
717 | ;; State `while': | |
718 | ;; boundary: Save position, goto state `while-boundary'. | |
719 | ;; other: Pop state, retry token. | |
720 | ;; | |
721 | ;; State `while-boundary': | |
722 | ;; "do": Pop state. | |
723 | ;; boundary: Restore position if it's not at start, pop state. [*see below] | |
724 | ;; other: See common state. | |
725 | ;; | |
726 | ;; State `catch': | |
727 | ;; boundary: Goto state `catch-boundary'. | |
728 | ;; other: Error, pop state, retry token. | |
729 | ;; | |
730 | ;; State `catch-boundary': | |
731 | ;; "try": Pop state. | |
732 | ;; "catch": Goto state `catch'. | |
733 | ;; boundary: Error, pop state. | |
734 | ;; other: See common state. | |
735 | ;; | |
736 | ;; [*] In the `while-boundary' state, we had pushed a 'while state, and were | |
737 | ;; searching for a "do" which would have opened a do-while. If we didn't | |
738 | ;; find it, we discard the analysis done since the "while", go back to this | |
739 | ;; token in the buffer and restart the scanning there, this time WITHOUT | |
740 | ;; pushing the 'while state onto the stack. | |
741 | ;; | |
a66cd3ee MS |
742 | ;; In addition to the above there is some special handling of labels |
743 | ;; and macros. | |
744 | ||
745 | (let ((case-fold-search nil) | |
746 | (start (point)) | |
747 | macro-start | |
748 | (delims (if comma-delim '(?\; ?,) '(?\;))) | |
749 | (c-stmt-delim-chars (if comma-delim | |
750 | c-stmt-delim-chars-with-comma | |
751 | c-stmt-delim-chars)) | |
d28e7f28 | 752 | c-in-literal-cache c-maybe-labelp after-case:-pos saved |
0386b551 AM |
753 | ;; Current position. |
754 | pos | |
755 | ;; Position of last stmt boundary character (e.g. ;). | |
756 | boundary-pos | |
757 | ;; The position of the last sexp or bound that follows the | |
758 | ;; first found colon, i.e. the start of the nonlabel part of | |
759 | ;; the statement. It's `start' if a colon is found just after | |
760 | ;; the start. | |
761 | after-labels-pos | |
762 | ;; Like `after-labels-pos', but the first such position inside | |
763 | ;; a label, i.e. the start of the last label before the start | |
764 | ;; of the nonlabel part of the statement. | |
765 | last-label-pos | |
766 | ;; The last position where a label is possible provided the | |
767 | ;; statement started there. It's nil as long as no invalid | |
768 | ;; label content has been found (according to | |
020716e1 | 769 | ;; `c-nonlabel-token-key'). It's `start' if no valid label |
0386b551 AM |
770 | ;; content was found in the label. Note that we might still |
771 | ;; regard it a label if it starts with `c-label-kwds'. | |
772 | label-good-pos | |
b248a85d AM |
773 | ;; Putative positions of the components of a bitfield declaration, |
774 | ;; e.g. "int foo : NUM_FOO_BITS ;" | |
775 | bitfield-type-pos bitfield-id-pos bitfield-size-pos | |
0386b551 AM |
776 | ;; Symbol just scanned back over (e.g. 'while or 'boundary). |
777 | ;; See above. | |
778 | sym | |
779 | ;; Current state in the automaton. See above. | |
780 | state | |
781 | ;; Current saved positions. See above. | |
782 | saved-pos | |
783 | ;; Stack of conses (state . saved-pos). | |
784 | stack | |
785 | ;; Regexp which matches "for", "if", etc. | |
786 | (cond-key (or c-opt-block-stmt-key | |
a66cd3ee | 787 | "\\<\\>")) ; Matches nothing. |
0386b551 AM |
788 | ;; Return value. |
789 | (ret 'same) | |
790 | ;; Positions of the last three sexps or bounds we've stopped at. | |
791 | tok ptok pptok) | |
a66cd3ee MS |
792 | |
793 | (save-restriction | |
794 | (if lim (narrow-to-region lim (point-max))) | |
795 | ||
796 | (if (save-excursion | |
797 | (and (c-beginning-of-macro) | |
798 | (/= (point) start))) | |
799 | (setq macro-start (point))) | |
800 | ||
d9e94c22 | 801 | ;; Try to skip back over unary operator characters, to register |
a66cd3ee MS |
802 | ;; that we've moved. |
803 | (while (progn | |
804 | (setq pos (point)) | |
0386b551 AM |
805 | (c-backward-syntactic-ws) |
806 | ;; Protect post-++/-- operators just before a virtual semicolon. | |
807 | (and (not (c-at-vsemi-p)) | |
808 | (/= (skip-chars-backward "-+!*&~@`#") 0)))) | |
d9e94c22 MS |
809 | |
810 | ;; Skip back over any semicolon here. If it was a bare semicolon, we're | |
0386b551 | 811 | ;; done. Later on we ignore the boundaries for statements that don't |
d9e94c22 MS |
812 | ;; contain any sexp. The only thing that is affected is that the error |
813 | ;; checking is a little less strict, and we really don't bother. | |
a66cd3ee MS |
814 | (if (and (memq (char-before) delims) |
815 | (progn (forward-char -1) | |
816 | (setq saved (point)) | |
0386b551 | 817 | (c-backward-syntactic-ws) |
a66cd3ee MS |
818 | (or (memq (char-before) delims) |
819 | (memq (char-before) '(?: nil)) | |
d9e94c22 | 820 | (eq (char-syntax (char-before)) ?\() |
0386b551 | 821 | (c-at-vsemi-p)))) |
a66cd3ee MS |
822 | (setq ret 'previous |
823 | pos saved) | |
824 | ||
825 | ;; Begin at start and not pos to detect macros if we stand | |
826 | ;; directly after the #. | |
827 | (goto-char start) | |
828 | (if (looking-at "\\<\\|\\W") | |
829 | ;; Record this as the first token if not starting inside it. | |
830 | (setq tok start)) | |
831 | ||
b248a85d AM |
832 | |
833 | ;; The following while loop goes back one sexp (balanced parens, | |
834 | ;; etc. with contents, or symbol or suchlike) each iteration. This | |
835 | ;; movement is accomplished with a call to c-backward-sexp approx 170 | |
836 | ;; lines below. | |
837 | ;; | |
838 | ;; The loop is exited only by throwing nil to the (catch 'loop ...): | |
839 | ;; 1. On reaching the start of a macro; | |
840 | ;; 2. On having passed a stmt boundary with the PDA stack empty; | |
841 | ;; 3. On reaching the start of an Objective C method def; | |
842 | ;; 4. From macro `c-bos-pop-state'; when the stack is empty; | |
843 | ;; 5. From macro `c-bos-pop-state-and-retry' when the stack is empty. | |
a66cd3ee MS |
844 | (while |
845 | (catch 'loop ;; Throw nil to break, non-nil to continue. | |
846 | (cond | |
b248a85d | 847 | ;; Are we in a macro, just after the opening #? |
a66cd3ee | 848 | ((save-excursion |
0386b551 | 849 | (and macro-start ; Always NIL for AWK. |
a66cd3ee MS |
850 | (progn (skip-chars-backward " \t") |
851 | (eq (char-before) ?#)) | |
852 | (progn (setq saved (1- (point))) | |
853 | (beginning-of-line) | |
854 | (not (eq (char-before (1- (point))) ?\\))) | |
d9e94c22 | 855 | (looking-at c-opt-cpp-start) |
a66cd3ee MS |
856 | (progn (skip-chars-forward " \t") |
857 | (eq (point) saved)))) | |
858 | (goto-char saved) | |
859 | (if (and (c-forward-to-cpp-define-body) | |
860 | (progn (c-forward-syntactic-ws start) | |
861 | (< (point) start))) | |
862 | ;; Stop at the first token in the content of the macro. | |
863 | (setq pos (point) | |
864 | ignore-labels t) ; Avoid the label check on exit. | |
865 | (setq pos saved | |
866 | ret 'macro | |
867 | ignore-labels t)) | |
b248a85d | 868 | (throw 'loop nil)) ; 1. Start of macro. |
a66cd3ee | 869 | |
d9e94c22 MS |
870 | ;; Do a round through the automaton if we've just passed a |
871 | ;; statement boundary or passed a "while"-like token. | |
a66cd3ee MS |
872 | ((or sym |
873 | (and (looking-at cond-key) | |
874 | (setq sym (intern (match-string 1))))) | |
875 | ||
876 | (when (and (< pos start) (null stack)) | |
b248a85d | 877 | (throw 'loop nil)) ; 2. Statement boundary. |
a66cd3ee | 878 | |
d9e94c22 MS |
879 | ;; The PDA state handling. |
880 | ;; | |
037558bf | 881 | ;; Refer to the description of the PDA in the opening |
d9e94c22 MS |
882 | ;; comments. In the following OR form, the first leaf |
883 | ;; attempts to handles one of the specific actions detailed | |
884 | ;; (e.g., finding token "if" whilst in state `else-boundary'). | |
885 | ;; We drop through to the second leaf (which handles common | |
886 | ;; state) if no specific handler is found in the first cond. | |
887 | ;; If a parsing error is detected (e.g. an "else" with no | |
888 | ;; preceding "if"), we throw to the enclosing catch. | |
889 | ;; | |
890 | ;; Note that the (eq state 'else) means | |
891 | ;; "we've just passed an else", NOT "we're looking for an | |
892 | ;; else". | |
a66cd3ee MS |
893 | (or (cond |
894 | ((eq state 'else) | |
895 | (if (eq sym 'boundary) | |
896 | (setq state 'else-boundary) | |
897 | (c-bos-report-error) | |
898 | (c-bos-pop-state-and-retry))) | |
899 | ||
900 | ((eq state 'else-boundary) | |
901 | (cond ((eq sym 'if) | |
902 | (c-bos-pop-state (setq ret 'beginning))) | |
903 | ((eq sym 'boundary) | |
904 | (c-bos-report-error) | |
905 | (c-bos-pop-state)))) | |
906 | ||
907 | ((eq state 'while) | |
908 | (if (and (eq sym 'boundary) | |
909 | ;; Since this can cause backtracking we do a | |
910 | ;; little more careful analysis to avoid it: | |
911 | ;; If there's a label in front of the while | |
912 | ;; it can't be part of a do-while. | |
913 | (not after-labels-pos)) | |
914 | (progn (c-bos-save-pos) | |
915 | (setq state 'while-boundary)) | |
d9e94c22 | 916 | (c-bos-pop-state-and-retry))) ; Can't be a do-while |
a66cd3ee MS |
917 | |
918 | ((eq state 'while-boundary) | |
919 | (cond ((eq sym 'do) | |
920 | (c-bos-pop-state (setq ret 'beginning))) | |
d9e94c22 MS |
921 | ((eq sym 'boundary) ; isn't a do-while |
922 | (c-bos-restore-pos) ; the position of the while | |
923 | (c-bos-pop-state)))) ; no longer searching for do. | |
a66cd3ee MS |
924 | |
925 | ((eq state 'catch) | |
926 | (if (eq sym 'boundary) | |
927 | (setq state 'catch-boundary) | |
928 | (c-bos-report-error) | |
929 | (c-bos-pop-state-and-retry))) | |
930 | ||
931 | ((eq state 'catch-boundary) | |
932 | (cond | |
933 | ((eq sym 'try) | |
934 | (c-bos-pop-state (setq ret 'beginning))) | |
935 | ((eq sym 'catch) | |
936 | (setq state 'catch)) | |
937 | ((eq sym 'boundary) | |
938 | (c-bos-report-error) | |
939 | (c-bos-pop-state))))) | |
940 | ||
d9e94c22 MS |
941 | ;; This is state common. We get here when the previous |
942 | ;; cond statement found no particular state handler. | |
a66cd3ee | 943 | (cond ((eq sym 'boundary) |
d9e94c22 MS |
944 | ;; If we have a boundary at the start |
945 | ;; position we push a frame to go to the | |
946 | ;; previous statement. | |
947 | (if (>= pos start) | |
948 | (c-bos-push-state) | |
949 | (c-bos-pop-state))) | |
a66cd3ee MS |
950 | ((eq sym 'else) |
951 | (c-bos-push-state) | |
952 | (c-bos-save-error-info 'if 'else) | |
953 | (setq state 'else)) | |
954 | ((eq sym 'while) | |
0386b551 AM |
955 | ;; Is this a real while, or a do-while? |
956 | ;; The next `when' triggers unless we are SURE that | |
40ba43b4 | 957 | ;; the `while' is not the tail end of a `do-while'. |
a66cd3ee | 958 | (when (or (not pptok) |
d9e94c22 | 959 | (memq (char-after pptok) delims) |
0386b551 AM |
960 | ;; The following kludge is to prevent |
961 | ;; infinite recursion when called from | |
962 | ;; c-awk-after-if-for-while-condition-p, | |
963 | ;; or the like. | |
964 | (and (eq (point) start) | |
965 | (c-vsemi-status-unknown-p)) | |
966 | (c-at-vsemi-p pptok)) | |
a66cd3ee MS |
967 | ;; Since this can cause backtracking we do a |
968 | ;; little more careful analysis to avoid it: If | |
0386b551 AM |
969 | ;; the while isn't followed by a (possibly |
970 | ;; virtual) semicolon it can't be a do-while. | |
a66cd3ee MS |
971 | (c-bos-push-state) |
972 | (setq state 'while))) | |
973 | ((memq sym '(catch finally)) | |
974 | (c-bos-push-state) | |
975 | (c-bos-save-error-info 'try sym) | |
976 | (setq state 'catch)))) | |
977 | ||
978 | (when c-maybe-labelp | |
979 | ;; We're either past a statement boundary or at the | |
980 | ;; start of a statement, so throw away any label data | |
981 | ;; for the previous one. | |
982 | (setq after-labels-pos nil | |
983 | last-label-pos nil | |
984 | c-maybe-labelp nil)))) | |
985 | ||
d9e94c22 MS |
986 | ;; Step to the previous sexp, but not if we crossed a |
987 | ;; boundary, since that doesn't consume an sexp. | |
a66cd3ee MS |
988 | (if (eq sym 'boundary) |
989 | (setq ret 'previous) | |
d9e94c22 MS |
990 | |
991 | ;; HERE IS THE SINGLE PLACE INSIDE THE PDA LOOP WHERE WE MOVE | |
0386b551 AM |
992 | ;; BACKWARDS THROUGH THE SOURCE. |
993 | ||
0386b551 | 994 | (c-backward-syntactic-ws) |
0386b551 | 995 | (let ((before-sws-pos (point)) |
0386b551 AM |
996 | ;; The end position of the area to search for statement |
997 | ;; barriers in this round. | |
b248a85d | 998 | (maybe-after-boundary-pos pos)) |
0386b551 | 999 | |
b248a85d AM |
1000 | ;; Go back over exactly one logical sexp, taking proper |
1001 | ;; account of macros and escaped EOLs. | |
0386b551 AM |
1002 | (while |
1003 | (progn | |
1004 | (unless (c-safe (c-backward-sexp) t) | |
1005 | ;; Give up if we hit an unbalanced block. Since the | |
1006 | ;; stack won't be empty the code below will report a | |
1007 | ;; suitable error. | |
a66cd3ee | 1008 | (throw 'loop nil)) |
b248a85d AM |
1009 | (cond |
1010 | ;; Have we moved into a macro? | |
1011 | ((and (not macro-start) | |
1012 | (c-beginning-of-macro)) | |
1013 | ;; Have we crossed a statement boundary? If not, | |
1014 | ;; keep going back until we find one or a "real" sexp. | |
1015 | (and | |
1016 | (save-excursion | |
1017 | (c-end-of-macro) | |
1018 | (not (c-crosses-statement-barrier-p | |
1019 | (point) maybe-after-boundary-pos))) | |
1020 | (setq maybe-after-boundary-pos (point)))) | |
1021 | ;; Have we just gone back over an escaped NL? This | |
1022 | ;; doesn't count as a sexp. | |
1023 | ((looking-at "\\\\$"))))) | |
1024 | ||
1025 | ;; Have we crossed a statement boundary? | |
1026 | (setq boundary-pos | |
1027 | (cond | |
1028 | ;; Are we at a macro beginning? | |
1029 | ((and (not macro-start) | |
1030 | c-opt-cpp-prefix | |
1031 | (looking-at c-opt-cpp-prefix)) | |
1032 | (save-excursion | |
1033 | (c-end-of-macro) | |
1034 | (c-crosses-statement-barrier-p | |
1035 | (point) maybe-after-boundary-pos))) | |
1036 | ;; Just gone back over a brace block? | |
1037 | ((and | |
1038 | (eq (char-after) ?{) | |
1039 | (not (c-looking-at-inexpr-block lim nil t))) | |
1040 | (save-excursion | |
1041 | (c-forward-sexp) (point))) | |
1042 | ;; Just gone back over some paren block? | |
1043 | ((looking-at "\\s\(") | |
1044 | (save-excursion | |
1045 | (goto-char (1+ (c-down-list-backward | |
1046 | before-sws-pos))) | |
1047 | (c-crosses-statement-barrier-p | |
1048 | (point) maybe-after-boundary-pos))) | |
1049 | ;; Just gone back over an ordinary symbol of some sort? | |
1050 | (t (c-crosses-statement-barrier-p | |
1051 | (point) maybe-after-boundary-pos)))) | |
91af3942 | 1052 | |
b248a85d AM |
1053 | (when boundary-pos |
1054 | (setq pptok ptok | |
1055 | ptok tok | |
1056 | tok boundary-pos | |
1057 | sym 'boundary) | |
1058 | ;; Like a C "continue". Analyze the next sexp. | |
1059 | (throw 'loop t)))) | |
a66cd3ee MS |
1060 | |
1061 | ;; ObjC method def? | |
1062 | (when (and c-opt-method-key | |
1063 | (setq saved (c-in-method-def-p))) | |
1064 | (setq pos saved | |
1065 | ignore-labels t) ; Avoid the label check on exit. | |
b248a85d AM |
1066 | (throw 'loop nil)) ; 3. ObjC method def. |
1067 | ||
1068 | ;; Might we have a bitfield declaration, "<type> <id> : <size>"? | |
1069 | (if c-has-bitfields | |
1070 | (cond | |
1071 | ;; The : <size> and <id> fields? | |
1072 | ((and (numberp c-maybe-labelp) | |
1073 | (not bitfield-size-pos) | |
1074 | (save-excursion | |
1075 | (goto-char (or tok start)) | |
1076 | (not (looking-at c-keywords-regexp))) | |
1077 | (not (looking-at c-keywords-regexp)) | |
1078 | (not (c-punctuation-in (point) c-maybe-labelp))) | |
1079 | (setq bitfield-size-pos (or tok start) | |
1080 | bitfield-id-pos (point))) | |
1081 | ;; The <type> field? | |
1082 | ((and bitfield-id-pos | |
1083 | (not bitfield-type-pos)) | |
1084 | (if (and (looking-at c-symbol-key) ; Can only be an integer type. :-) | |
1085 | (not (looking-at c-not-primitive-type-keywords-regexp)) | |
1086 | (not (c-punctuation-in (point) tok))) | |
1087 | (setq bitfield-type-pos (point)) | |
1088 | (setq bitfield-size-pos nil | |
1089 | bitfield-id-pos nil))))) | |
a66cd3ee | 1090 | |
0386b551 AM |
1091 | ;; Handle labels. |
1092 | (unless (eq ignore-labels t) | |
1093 | (when (numberp c-maybe-labelp) | |
cb694ab7 AM |
1094 | ;; `c-crosses-statement-barrier-p' has found a colon, so we |
1095 | ;; might be in a label now. Have we got a real label | |
1096 | ;; (including a case label) or something like C++'s "public:"? | |
d28e7f28 AM |
1097 | ;; A case label might use an expression rather than a token. |
1098 | (setq after-case:-pos (or tok start)) | |
020716e1 AM |
1099 | (if (or (looking-at c-nonlabel-token-key) ; e.g. "while" or "'a'" |
1100 | ;; Catch C++'s inheritance construct "class foo : bar". | |
1101 | (save-excursion | |
1102 | (and | |
1103 | (c-safe (c-backward-sexp) t) | |
1104 | (looking-at c-nonlabel-token-2-key)))) | |
d28e7f28 AM |
1105 | (setq c-maybe-labelp nil) |
1106 | (if after-labels-pos ; Have we already encountered a label? | |
1107 | (if (not last-label-pos) | |
1108 | (setq last-label-pos (or tok start))) | |
1109 | (setq after-labels-pos (or tok start))) | |
1110 | (setq c-maybe-labelp t | |
1111 | label-good-pos nil))) ; bogus "label" | |
cb694ab7 AM |
1112 | |
1113 | (when (and (not label-good-pos) ; i.e. no invalid "label"'s yet | |
1114 | ; been found. | |
1115 | (looking-at c-nonlabel-token-key)) ; e.g. "while :" | |
0386b551 AM |
1116 | ;; We're in a potential label and it's the first |
1117 | ;; time we've found something that isn't allowed in | |
1118 | ;; one. | |
1119 | (setq label-good-pos (or tok start)))) | |
1120 | ||
1121 | ;; We've moved back by a sexp, so update the token positions. | |
a66cd3ee MS |
1122 | (setq sym nil |
1123 | pptok ptok | |
1124 | ptok tok | |
1125 | tok (point) | |
b248a85d AM |
1126 | pos tok) ; always non-nil |
1127 | ) ; end of (catch loop ....) | |
1128 | ) ; end of sexp-at-a-time (while ....) | |
91af3942 | 1129 | |
a66cd3ee MS |
1130 | ;; If the stack isn't empty there might be errors to report. |
1131 | (while stack | |
1132 | (if (and (vectorp saved-pos) (eq (length saved-pos) 3)) | |
1133 | (c-bos-report-error)) | |
1134 | (setq saved-pos (cdr (car stack)) | |
1135 | stack (cdr stack))) | |
1136 | ||
1137 | (when (and (eq ret 'same) | |
1138 | (not (memq sym '(boundary ignore nil)))) | |
1139 | ;; Need to investigate closer whether we've crossed | |
1140 | ;; between a substatement and its containing statement. | |
1141 | (if (setq saved (if (looking-at c-block-stmt-1-key) | |
1142 | ptok | |
1143 | pptok)) | |
1144 | (cond ((> start saved) (setq pos saved)) | |
1145 | ((= start saved) (setq ret 'up))))) | |
1146 | ||
0386b551 AM |
1147 | (when (and (not ignore-labels) |
1148 | (eq c-maybe-labelp t) | |
d9e94c22 | 1149 | (not (eq ret 'beginning)) |
0386b551 | 1150 | after-labels-pos |
b248a85d | 1151 | (not bitfield-type-pos) ; Bitfields take precedence over labels. |
0386b551 AM |
1152 | (or (not label-good-pos) |
1153 | (<= label-good-pos pos) | |
1154 | (progn | |
1155 | (goto-char (if (and last-label-pos | |
1156 | (< last-label-pos start)) | |
1157 | last-label-pos | |
1158 | pos)) | |
1159 | (looking-at c-label-kwds-regexp)))) | |
a66cd3ee MS |
1160 | ;; We're in a label. Maybe we should step to the statement |
1161 | ;; after it. | |
1162 | (if (< after-labels-pos start) | |
1163 | (setq pos after-labels-pos) | |
1164 | (setq ret 'label) | |
0386b551 AM |
1165 | (if (and last-label-pos (< last-label-pos start)) |
1166 | ;; Might have jumped over several labels. Go to the last one. | |
a66cd3ee MS |
1167 | (setq pos last-label-pos))))) |
1168 | ||
d28e7f28 | 1169 | ;; Have we got "case <expression>:"? |
a66cd3ee | 1170 | (goto-char pos) |
d28e7f28 AM |
1171 | (when (and after-case:-pos |
1172 | (not (eq ret 'beginning)) | |
1173 | (looking-at c-case-kwds-regexp)) | |
1174 | (if (< after-case:-pos start) | |
0000ee90 AM |
1175 | (setq pos after-case:-pos)) |
1176 | (if (eq ret 'same) | |
1177 | (setq ret 'label))) | |
d28e7f28 AM |
1178 | |
1179 | ;; Skip over the unary operators that can start the statement. | |
a66cd3ee | 1180 | (while (progn |
0386b551 AM |
1181 | (c-backward-syntactic-ws) |
1182 | ;; protect AWK post-inc/decrement operators, etc. | |
1183 | (and (not (c-at-vsemi-p (point))) | |
1184 | (/= (skip-chars-backward "-+!*&~@`#") 0))) | |
a66cd3ee MS |
1185 | (setq pos (point))) |
1186 | (goto-char pos) | |
1187 | ret))) | |
785eecbb | 1188 | |
b248a85d AM |
1189 | (defun c-punctuation-in (from to) |
1190 | "Return non-nil if there is a non-comment non-macro punctuation character | |
1191 | between FROM and TO. FROM must not be in a string or comment. The returned | |
1192 | value is the position of the first such character." | |
1193 | (save-excursion | |
1194 | (goto-char from) | |
1195 | (let ((pos (point))) | |
1196 | (while (progn (skip-chars-forward c-symbol-chars to) | |
1197 | (c-forward-syntactic-ws to) | |
1198 | (> (point) pos)) | |
1199 | (setq pos (point)))) | |
1200 | (and (< (point) to) (point)))) | |
1201 | ||
785eecbb | 1202 | (defun c-crosses-statement-barrier-p (from to) |
a66cd3ee MS |
1203 | "Return non-nil if buffer positions FROM to TO cross one or more |
1204 | statement or declaration boundaries. The returned value is actually | |
d9e94c22 MS |
1205 | the position of the earliest boundary char. FROM must not be within |
1206 | a string or comment. | |
a66cd3ee MS |
1207 | |
1208 | The variable `c-maybe-labelp' is set to the position of the first `:' that | |
1209 | might start a label (i.e. not part of `::' and not preceded by `?'). If a | |
0386b551 AM |
1210 | single `?' is found, then `c-maybe-labelp' is cleared. |
1211 | ||
1212 | For AWK, a statement which is terminated by an EOL (not a \; or a }) is | |
1213 | regarded as having a \"virtual semicolon\" immediately after the last token on | |
b414f371 | 1214 | the line. If this virtual semicolon is _at_ from, the function recognizes it. |
0386b551 AM |
1215 | |
1216 | Note that this function might do hidden buffer changes. See the | |
1217 | comment at the start of cc-engine.el for more info." | |
536610a4 AM |
1218 | (let* ((skip-chars |
1219 | ;; If the current language has CPP macros, insert # into skip-chars. | |
1220 | (if c-opt-cpp-symbol | |
1221 | (concat (substring c-stmt-delim-chars 0 1) ; "^" | |
1222 | c-opt-cpp-symbol ; usually "#" | |
1223 | (substring c-stmt-delim-chars 1)) ; e.g. ";{}?:" | |
1224 | c-stmt-delim-chars)) | |
1225 | (non-skip-list | |
1226 | (append (substring skip-chars 1) nil)) ; e.g. (?# ?\; ?{ ?} ?? ?:) | |
1227 | lit-range vsemi-pos) | |
1228 | (save-restriction | |
1229 | (widen) | |
1230 | (save-excursion | |
1231 | (catch 'done | |
1232 | (goto-char from) | |
1233 | (while (progn (skip-chars-forward | |
1234 | skip-chars | |
1235 | (min to (c-point 'bonl))) | |
1236 | (< (point) to)) | |
1237 | (cond | |
1238 | ;; Virtual semicolon? | |
1239 | ((and (bolp) | |
1240 | (save-excursion | |
1241 | (progn | |
1242 | (if (setq lit-range (c-literal-limits from)) ; Have we landed in a string/comment? | |
1243 | (goto-char (car lit-range))) | |
1244 | (c-backward-syntactic-ws) ; ? put a limit here, maybe? | |
1245 | (setq vsemi-pos (point)) | |
1246 | (c-at-vsemi-p)))) | |
1247 | (throw 'done vsemi-pos)) | |
1248 | ;; In a string/comment? | |
2e492df3 | 1249 | ((setq lit-range (c-literal-limits from)) |
536610a4 AM |
1250 | (goto-char (cdr lit-range))) |
1251 | ((eq (char-after) ?:) | |
1252 | (forward-char) | |
1253 | (if (and (eq (char-after) ?:) | |
1254 | (< (point) to)) | |
1255 | ;; Ignore scope operators. | |
1256 | (forward-char) | |
1257 | (setq c-maybe-labelp (1- (point))))) | |
1258 | ((eq (char-after) ??) | |
1259 | ;; A question mark. Can't be a label, so stop | |
1260 | ;; looking for more : and ?. | |
1261 | (setq c-maybe-labelp nil | |
1262 | skip-chars (substring c-stmt-delim-chars 0 -2))) | |
1263 | ;; At a CPP construct? | |
1264 | ((and c-opt-cpp-symbol (looking-at c-opt-cpp-symbol) | |
1265 | (save-excursion | |
1266 | (forward-line 0) | |
1267 | (looking-at c-opt-cpp-prefix))) | |
1268 | (c-end-of-macro)) | |
1269 | ((memq (char-after) non-skip-list) | |
1270 | (throw 'done (point))))) | |
1271 | ;; In trailing space after an as yet undetected virtual semicolon? | |
1272 | (c-backward-syntactic-ws from) | |
1273 | (if (and (< (point) to) | |
1274 | (c-at-vsemi-p)) | |
1275 | (point) | |
1276 | nil)))))) | |
0386b551 AM |
1277 | |
1278 | (defun c-at-statement-start-p () | |
1279 | "Return non-nil if the point is at the first token in a statement | |
1280 | or somewhere in the syntactic whitespace before it. | |
1281 | ||
1282 | A \"statement\" here is not restricted to those inside code blocks. | |
1283 | Any kind of declaration-like construct that occur outside function | |
1284 | bodies is also considered a \"statement\". | |
1285 | ||
1286 | Note that this function might do hidden buffer changes. See the | |
1287 | comment at the start of cc-engine.el for more info." | |
1288 | ||
1289 | (save-excursion | |
1290 | (let ((end (point)) | |
1291 | c-maybe-labelp) | |
1292 | (c-syntactic-skip-backward (substring c-stmt-delim-chars 1) nil t) | |
1293 | (or (bobp) | |
1294 | (eq (char-before) ?}) | |
1295 | (and (eq (char-before) ?{) | |
1296 | (not (and c-special-brace-lists | |
1297 | (progn (backward-char) | |
1298 | (c-looking-at-special-brace-list))))) | |
1299 | (c-crosses-statement-barrier-p (point) end))))) | |
1300 | ||
1301 | (defun c-at-expression-start-p () | |
1302 | "Return non-nil if the point is at the first token in an expression or | |
1303 | statement, or somewhere in the syntactic whitespace before it. | |
1304 | ||
1305 | An \"expression\" here is a bit different from the normal language | |
1306 | grammar sense: It's any sequence of expression tokens except commas, | |
1307 | unless they are enclosed inside parentheses of some kind. Also, an | |
1308 | expression never continues past an enclosing parenthesis, but it might | |
1309 | contain parenthesis pairs of any sort except braces. | |
1310 | ||
1311 | Since expressions never cross statement boundaries, this function also | |
1312 | recognizes statement beginnings, just like `c-at-statement-start-p'. | |
1313 | ||
1314 | Note that this function might do hidden buffer changes. See the | |
1315 | comment at the start of cc-engine.el for more info." | |
1316 | ||
1317 | (save-excursion | |
1318 | (let ((end (point)) | |
1319 | (c-stmt-delim-chars c-stmt-delim-chars-with-comma) | |
1320 | c-maybe-labelp) | |
1321 | (c-syntactic-skip-backward (substring c-stmt-delim-chars 1) nil t) | |
1322 | (or (bobp) | |
1323 | (memq (char-before) '(?{ ?})) | |
1324 | (save-excursion (backward-char) | |
1325 | (looking-at "\\s(")) | |
1326 | (c-crosses-statement-barrier-p (point) end))))) | |
785eecbb RS |
1327 | |
1328 | \f | |
d9e94c22 MS |
1329 | ;; A set of functions that covers various idiosyncrasies in |
1330 | ;; implementations of `forward-comment'. | |
1331 | ||
1332 | ;; Note: Some emacsen considers incorrectly that any line comment | |
1333 | ;; ending with a backslash continues to the next line. I can't think | |
1334 | ;; of any way to work around that in a reliable way without changing | |
1335 | ;; the buffer, though. Suggestions welcome. ;) (No, temporarily | |
1336 | ;; changing the syntax for backslash doesn't work since we must treat | |
1337 | ;; escapes in string literals correctly.) | |
1338 | ||
1339 | (defun c-forward-single-comment () | |
1340 | "Move forward past whitespace and the closest following comment, if any. | |
1341 | Return t if a comment was found, nil otherwise. In either case, the | |
1342 | point is moved past the following whitespace. Line continuations, | |
1343 | i.e. a backslashes followed by line breaks, are treated as whitespace. | |
1344 | The line breaks that end line comments are considered to be the | |
1345 | comment enders, so the point will be put on the beginning of the next | |
1346 | line if it moved past a line comment. | |
1347 | ||
1348 | This function does not do any hidden buffer changes." | |
1349 | ||
1350 | (let ((start (point))) | |
1351 | (when (looking-at "\\([ \t\n\r\f\v]\\|\\\\[\n\r]\\)+") | |
1352 | (goto-char (match-end 0))) | |
1353 | ||
1354 | (when (forward-comment 1) | |
1355 | (if (eobp) | |
1356 | ;; Some emacsen (e.g. XEmacs 21) return t when moving | |
1357 | ;; forwards at eob. | |
1358 | nil | |
1359 | ||
1360 | ;; Emacs includes the ending newline in a b-style (c++) | |
1361 | ;; comment, but XEmacs doesn't. We depend on the Emacs | |
1362 | ;; behavior (which also is symmetric). | |
1363 | (if (and (eolp) (elt (parse-partial-sexp start (point)) 7)) | |
1364 | (condition-case nil (forward-char 1))) | |
1365 | ||
1366 | t)))) | |
1367 | ||
1368 | (defsubst c-forward-comments () | |
1369 | "Move forward past all following whitespace and comments. | |
1370 | Line continuations, i.e. a backslashes followed by line breaks, are | |
1371 | treated as whitespace. | |
1372 | ||
0386b551 AM |
1373 | Note that this function might do hidden buffer changes. See the |
1374 | comment at the start of cc-engine.el for more info." | |
d9e94c22 MS |
1375 | |
1376 | (while (or | |
1377 | ;; If forward-comment in at least XEmacs 21 is given a large | |
1378 | ;; positive value, it'll loop all the way through if it hits | |
1379 | ;; eob. | |
1380 | (and (forward-comment 5) | |
1381 | ;; Some emacsen (e.g. XEmacs 21) return t when moving | |
1382 | ;; forwards at eob. | |
1383 | (not (eobp))) | |
1384 | ||
1385 | (when (looking-at "\\\\[\n\r]") | |
1386 | (forward-char 2) | |
1387 | t)))) | |
1388 | ||
1389 | (defun c-backward-single-comment () | |
1390 | "Move backward past whitespace and the closest preceding comment, if any. | |
1391 | Return t if a comment was found, nil otherwise. In either case, the | |
1392 | point is moved past the preceding whitespace. Line continuations, | |
1393 | i.e. a backslashes followed by line breaks, are treated as whitespace. | |
1394 | The line breaks that end line comments are considered to be the | |
1395 | comment enders, so the point cannot be at the end of the same line to | |
1396 | move over a line comment. | |
1397 | ||
1398 | This function does not do any hidden buffer changes." | |
1399 | ||
1400 | (let ((start (point))) | |
1401 | ;; When we got newline terminated comments, forward-comment in all | |
1402 | ;; supported emacsen so far will stop at eol of each line not | |
1403 | ;; ending with a comment when moving backwards. This corrects for | |
1404 | ;; that, and at the same time handles line continuations. | |
1405 | (while (progn | |
1406 | (skip-chars-backward " \t\n\r\f\v") | |
1407 | (and (looking-at "[\n\r]") | |
0386b551 | 1408 | (eq (char-before) ?\\))) |
d9e94c22 MS |
1409 | (backward-char)) |
1410 | ||
1411 | (if (bobp) | |
1412 | ;; Some emacsen (e.g. Emacs 19.34) return t when moving | |
1413 | ;; backwards at bob. | |
1414 | nil | |
1415 | ||
1416 | ;; Leave point after the closest following newline if we've | |
1417 | ;; backed up over any above, since forward-comment won't move | |
1418 | ;; backward over a line comment if point is at the end of the | |
1419 | ;; same line. | |
1420 | (re-search-forward "\\=\\s *[\n\r]" start t) | |
1421 | ||
bf2c1571 | 1422 | (if (if (let (open-paren-in-column-0-is-defun-start) (forward-comment -1)) |
d9e94c22 MS |
1423 | (if (eolp) |
1424 | ;; If forward-comment above succeeded and we're at eol | |
1425 | ;; then the newline we moved over above didn't end a | |
1426 | ;; line comment, so we give it another go. | |
bf2c1571 AM |
1427 | (let (open-paren-in-column-0-is-defun-start) |
1428 | (forward-comment -1)) | |
d9e94c22 MS |
1429 | t)) |
1430 | ||
1431 | ;; Emacs <= 20 and XEmacs move back over the closer of a | |
1432 | ;; block comment that lacks an opener. | |
1433 | (if (looking-at "\\*/") | |
1434 | (progn (forward-char 2) nil) | |
1435 | t))))) | |
1436 | ||
1437 | (defsubst c-backward-comments () | |
1438 | "Move backward past all preceding whitespace and comments. | |
1439 | Line continuations, i.e. a backslashes followed by line breaks, are | |
1440 | treated as whitespace. The line breaks that end line comments are | |
1441 | considered to be the comment enders, so the point cannot be at the end | |
0386b551 AM |
1442 | of the same line to move over a line comment. Unlike |
1443 | c-backward-syntactic-ws, this function doesn't move back over | |
1444 | preprocessor directives. | |
d9e94c22 | 1445 | |
0386b551 AM |
1446 | Note that this function might do hidden buffer changes. See the |
1447 | comment at the start of cc-engine.el for more info." | |
d9e94c22 MS |
1448 | |
1449 | (let ((start (point))) | |
1450 | (while (and | |
0386b551 | 1451 | ;; `forward-comment' in some emacsen (e.g. XEmacs 21.4) |
d9e94c22 MS |
1452 | ;; return t when moving backwards at bob. |
1453 | (not (bobp)) | |
1454 | ||
14b8c3d9 AM |
1455 | (if (let (open-paren-in-column-0-is-defun-start moved-comment) |
1456 | (while | |
1457 | (and (not (setq moved-comment (forward-comment -1))) | |
1458 | ;; Cope specifically with ^M^J here - | |
1459 | ;; forward-comment sometimes gets stuck after ^Ms, | |
1460 | ;; sometimes after ^M^J. | |
1461 | (or | |
1462 | (when (eq (char-before) ?\r) | |
1463 | (backward-char) | |
1464 | t) | |
1465 | (when (and (eq (char-before) ?\n) | |
1466 | (eq (char-before (1- (point))) ?\r)) | |
1467 | (backward-char 2) | |
1468 | t)))) | |
1469 | moved-comment) | |
d9e94c22 MS |
1470 | (if (looking-at "\\*/") |
1471 | ;; Emacs <= 20 and XEmacs move back over the | |
1472 | ;; closer of a block comment that lacks an opener. | |
1473 | (progn (forward-char 2) nil) | |
1474 | t) | |
1475 | ||
1476 | ;; XEmacs treats line continuations as whitespace but | |
1477 | ;; only in the backward direction, which seems a bit | |
1478 | ;; odd. Anyway, this is necessary for Emacs. | |
1479 | (when (and (looking-at "[\n\r]") | |
1480 | (eq (char-before) ?\\) | |
1481 | (< (point) start)) | |
1482 | (backward-char) | |
1483 | t)))))) | |
1484 | ||
1485 | \f | |
d9e94c22 | 1486 | ;; Tools for skipping over syntactic whitespace. |
a66cd3ee | 1487 | |
d9e94c22 MS |
1488 | ;; The following functions use text properties to cache searches over |
1489 | ;; large regions of syntactic whitespace. It works as follows: | |
1490 | ;; | |
1491 | ;; o If a syntactic whitespace region contains anything but simple | |
1492 | ;; whitespace (i.e. space, tab and line breaks), the text property | |
1493 | ;; `c-in-sws' is put over it. At places where we have stopped | |
1494 | ;; within that region there's also a `c-is-sws' text property. | |
1495 | ;; That since there typically are nested whitespace inside that | |
1496 | ;; must be handled separately, e.g. whitespace inside a comment or | |
1497 | ;; cpp directive. Thus, from one point with `c-is-sws' it's safe | |
1498 | ;; to jump to another point with that property within the same | |
1499 | ;; `c-in-sws' region. It can be likened to a ladder where | |
1500 | ;; `c-in-sws' marks the bars and `c-is-sws' the rungs. | |
1501 | ;; | |
1502 | ;; o The `c-is-sws' property is put on the simple whitespace chars at | |
1503 | ;; a "rung position" and also maybe on the first following char. | |
1504 | ;; As many characters as can be conveniently found in this range | |
1505 | ;; are marked, but no assumption can be made that the whole range | |
1506 | ;; is marked (it could be clobbered by later changes, for | |
1507 | ;; instance). | |
1508 | ;; | |
1509 | ;; Note that some part of the beginning of a sequence of simple | |
1510 | ;; whitespace might be part of the end of a preceding line comment | |
1511 | ;; or cpp directive and must not be considered part of the "rung". | |
1512 | ;; Such whitespace is some amount of horizontal whitespace followed | |
1513 | ;; by a newline. In the case of cpp directives it could also be | |
1514 | ;; two newlines with horizontal whitespace between them. | |
1515 | ;; | |
1516 | ;; The reason to include the first following char is to cope with | |
1517 | ;; "rung positions" that doesn't have any ordinary whitespace. If | |
1518 | ;; `c-is-sws' is put on a token character it does not have | |
1519 | ;; `c-in-sws' set simultaneously. That's the only case when that | |
1520 | ;; can occur, and the reason for not extending the `c-in-sws' | |
1521 | ;; region to cover it is that the `c-in-sws' region could then be | |
1522 | ;; accidentally merged with a following one if the token is only | |
1523 | ;; one character long. | |
1524 | ;; | |
1525 | ;; o On buffer changes the `c-in-sws' and `c-is-sws' properties are | |
1526 | ;; removed in the changed region. If the change was inside | |
1527 | ;; syntactic whitespace that means that the "ladder" is broken, but | |
1528 | ;; a later call to `c-forward-sws' or `c-backward-sws' will use the | |
1529 | ;; parts on either side and use an ordinary search only to "repair" | |
1530 | ;; the gap. | |
1531 | ;; | |
1532 | ;; Special care needs to be taken if a region is removed: If there | |
1533 | ;; are `c-in-sws' on both sides of it which do not connect inside | |
1534 | ;; the region then they can't be joined. If e.g. a marked macro is | |
1535 | ;; broken, syntactic whitespace inside the new text might be | |
1536 | ;; marked. If those marks would become connected with the old | |
1537 | ;; `c-in-sws' range around the macro then we could get a ladder | |
1538 | ;; with one end outside the macro and the other at some whitespace | |
1539 | ;; within it. | |
1540 | ;; | |
1541 | ;; The main motivation for this system is to increase the speed in | |
1542 | ;; skipping over the large whitespace regions that can occur at the | |
1543 | ;; top level in e.g. header files that contain a lot of comments and | |
1544 | ;; cpp directives. For small comments inside code it's probably | |
1545 | ;; slower than using `forward-comment' straightforwardly, but speed is | |
1546 | ;; not a significant factor there anyway. | |
1547 | ||
1548 | ; (defface c-debug-is-sws-face | |
1549 | ; '((t (:background "GreenYellow"))) | |
1550 | ; "Debug face to mark the `c-is-sws' property.") | |
1551 | ; (defface c-debug-in-sws-face | |
1552 | ; '((t (:underline t))) | |
1553 | ; "Debug face to mark the `c-in-sws' property.") | |
1554 | ||
1555 | ; (defun c-debug-put-sws-faces () | |
1556 | ; ;; Put the sws debug faces on all the `c-is-sws' and `c-in-sws' | |
1557 | ; ;; properties in the buffer. | |
1558 | ; (interactive) | |
1559 | ; (save-excursion | |
0386b551 | 1560 | ; (c-save-buffer-state (in-face) |
d9e94c22 MS |
1561 | ; (goto-char (point-min)) |
1562 | ; (setq in-face (if (get-text-property (point) 'c-is-sws) | |
1563 | ; (point))) | |
1564 | ; (while (progn | |
1565 | ; (goto-char (next-single-property-change | |
1566 | ; (point) 'c-is-sws nil (point-max))) | |
1567 | ; (if in-face | |
1568 | ; (progn | |
1569 | ; (c-debug-add-face in-face (point) 'c-debug-is-sws-face) | |
1570 | ; (setq in-face nil)) | |
1571 | ; (setq in-face (point))) | |
1572 | ; (not (eobp)))) | |
1573 | ; (goto-char (point-min)) | |
1574 | ; (setq in-face (if (get-text-property (point) 'c-in-sws) | |
1575 | ; (point))) | |
1576 | ; (while (progn | |
1577 | ; (goto-char (next-single-property-change | |
1578 | ; (point) 'c-in-sws nil (point-max))) | |
1579 | ; (if in-face | |
1580 | ; (progn | |
1581 | ; (c-debug-add-face in-face (point) 'c-debug-in-sws-face) | |
1582 | ; (setq in-face nil)) | |
1583 | ; (setq in-face (point))) | |
1584 | ; (not (eobp))))))) | |
1585 | ||
1586 | (defmacro c-debug-sws-msg (&rest args) | |
1587 | ;;`(message ,@args) | |
1588 | ) | |
1589 | ||
1590 | (defmacro c-put-is-sws (beg end) | |
0386b551 | 1591 | ;; This macro does a hidden buffer change. |
d9e94c22 MS |
1592 | `(let ((beg ,beg) (end ,end)) |
1593 | (put-text-property beg end 'c-is-sws t) | |
1594 | ,@(when (facep 'c-debug-is-sws-face) | |
1595 | `((c-debug-add-face beg end 'c-debug-is-sws-face))))) | |
1596 | ||
1597 | (defmacro c-put-in-sws (beg end) | |
0386b551 | 1598 | ;; This macro does a hidden buffer change. |
d9e94c22 MS |
1599 | `(let ((beg ,beg) (end ,end)) |
1600 | (put-text-property beg end 'c-in-sws t) | |
1601 | ,@(when (facep 'c-debug-is-sws-face) | |
1602 | `((c-debug-add-face beg end 'c-debug-in-sws-face))))) | |
1603 | ||
1604 | (defmacro c-remove-is-sws (beg end) | |
0386b551 | 1605 | ;; This macro does a hidden buffer change. |
d9e94c22 MS |
1606 | `(let ((beg ,beg) (end ,end)) |
1607 | (remove-text-properties beg end '(c-is-sws nil)) | |
1608 | ,@(when (facep 'c-debug-is-sws-face) | |
1609 | `((c-debug-remove-face beg end 'c-debug-is-sws-face))))) | |
1610 | ||
1611 | (defmacro c-remove-in-sws (beg end) | |
0386b551 | 1612 | ;; This macro does a hidden buffer change. |
d9e94c22 MS |
1613 | `(let ((beg ,beg) (end ,end)) |
1614 | (remove-text-properties beg end '(c-in-sws nil)) | |
1615 | ,@(when (facep 'c-debug-is-sws-face) | |
1616 | `((c-debug-remove-face beg end 'c-debug-in-sws-face))))) | |
1617 | ||
1618 | (defmacro c-remove-is-and-in-sws (beg end) | |
0386b551 | 1619 | ;; This macro does a hidden buffer change. |
d9e94c22 MS |
1620 | `(let ((beg ,beg) (end ,end)) |
1621 | (remove-text-properties beg end '(c-is-sws nil c-in-sws nil)) | |
1622 | ,@(when (facep 'c-debug-is-sws-face) | |
1623 | `((c-debug-remove-face beg end 'c-debug-is-sws-face) | |
1624 | (c-debug-remove-face beg end 'c-debug-in-sws-face))))) | |
1625 | ||
1626 | (defsubst c-invalidate-sws-region-after (beg end) | |
1627 | ;; Called from `after-change-functions'. Note that if | |
1628 | ;; `c-forward-sws' or `c-backward-sws' are used outside | |
1629 | ;; `c-save-buffer-state' or similar then this will remove the cache | |
1630 | ;; properties right after they're added. | |
0386b551 AM |
1631 | ;; |
1632 | ;; This function does hidden buffer changes. | |
d9e94c22 MS |
1633 | |
1634 | (save-excursion | |
1635 | ;; Adjust the end to remove the properties in any following simple | |
1636 | ;; ws up to and including the next line break, if there is any | |
1637 | ;; after the changed region. This is necessary e.g. when a rung | |
1638 | ;; marked empty line is converted to a line comment by inserting | |
1639 | ;; "//" before the line break. In that case the line break would | |
1640 | ;; keep the rung mark which could make a later `c-backward-sws' | |
1641 | ;; move into the line comment instead of over it. | |
1642 | (goto-char end) | |
1643 | (skip-chars-forward " \t\f\v") | |
1644 | (when (and (eolp) (not (eobp))) | |
1645 | (setq end (1+ (point))))) | |
1646 | ||
1647 | (when (and (= beg end) | |
1648 | (get-text-property beg 'c-in-sws) | |
2a15eb73 | 1649 | (> beg (point-min)) |
d9e94c22 MS |
1650 | (get-text-property (1- beg) 'c-in-sws)) |
1651 | ;; Ensure that an `c-in-sws' range gets broken. Note that it isn't | |
1652 | ;; safe to keep a range that was continuous before the change. E.g: | |
1653 | ;; | |
1654 | ;; #define foo | |
1655 | ;; \ | |
1656 | ;; bar | |
1657 | ;; | |
1658 | ;; There can be a "ladder" between "#" and "b". Now, if the newline | |
1659 | ;; after "foo" is removed then "bar" will become part of the cpp | |
1660 | ;; directive instead of a syntactically relevant token. In that | |
1661 | ;; case there's no longer syntactic ws from "#" to "b". | |
1662 | (setq beg (1- beg))) | |
1663 | ||
1664 | (c-debug-sws-msg "c-invalidate-sws-region-after [%s..%s]" beg end) | |
1665 | (c-remove-is-and-in-sws beg end)) | |
1666 | ||
1667 | (defun c-forward-sws () | |
1668 | ;; Used by `c-forward-syntactic-ws' to implement the unbounded search. | |
0386b551 AM |
1669 | ;; |
1670 | ;; This function might do hidden buffer changes. | |
d9e94c22 MS |
1671 | |
1672 | (let (;; `rung-pos' is set to a position as early as possible in the | |
1673 | ;; unmarked part of the simple ws region. | |
1674 | (rung-pos (point)) next-rung-pos rung-end-pos last-put-in-sws-pos | |
1675 | rung-is-marked next-rung-is-marked simple-ws-end | |
1676 | ;; `safe-start' is set when it's safe to cache the start position. | |
1677 | ;; It's not set if we've initially skipped over comments and line | |
1678 | ;; continuations since we might have gone out through the end of a | |
1679 | ;; macro then. This provision makes `c-forward-sws' not populate the | |
1680 | ;; cache in the majority of cases, but otoh is `c-backward-sws' by far | |
1681 | ;; more common. | |
1682 | safe-start) | |
1683 | ||
1684 | ;; Skip simple ws and do a quick check on the following character to see | |
1685 | ;; if it's anything that can't start syntactic ws, so we can bail out | |
1686 | ;; early in the majority of cases when there just are a few ws chars. | |
1687 | (skip-chars-forward " \t\n\r\f\v") | |
1688 | (when (looking-at c-syntactic-ws-start) | |
1689 | ||
1690 | (setq rung-end-pos (min (1+ (point)) (point-max))) | |
1691 | (if (setq rung-is-marked (text-property-any rung-pos rung-end-pos | |
1692 | 'c-is-sws t)) | |
1693 | ;; Find the last rung position to avoid setting properties in all | |
1694 | ;; the cases when the marked rung is complete. | |
1695 | ;; (`next-single-property-change' is certain to move at least one | |
1696 | ;; step forward.) | |
1697 | (setq rung-pos (1- (next-single-property-change | |
1698 | rung-is-marked 'c-is-sws nil rung-end-pos))) | |
1699 | ;; Got no marked rung here. Since the simple ws might have started | |
1700 | ;; inside a line comment or cpp directive we must set `rung-pos' as | |
1701 | ;; high as possible. | |
1702 | (setq rung-pos (point))) | |
1703 | ||
2143fa32 | 1704 | (with-silent-modifications |
d9e94c22 MS |
1705 | (while |
1706 | (progn | |
1707 | (while | |
1708 | (when (and rung-is-marked | |
1709 | (get-text-property (point) 'c-in-sws)) | |
1710 | ||
1711 | ;; The following search is the main reason that `c-in-sws' | |
1712 | ;; and `c-is-sws' aren't combined to one property. | |
1713 | (goto-char (next-single-property-change | |
1714 | (point) 'c-in-sws nil (point-max))) | |
1715 | (unless (get-text-property (point) 'c-is-sws) | |
1716 | ;; If the `c-in-sws' region extended past the last | |
1717 | ;; `c-is-sws' char we have to go back a bit. | |
1718 | (or (get-text-property (1- (point)) 'c-is-sws) | |
1719 | (goto-char (previous-single-property-change | |
1720 | (point) 'c-is-sws))) | |
1721 | (backward-char)) | |
1722 | ||
1723 | (c-debug-sws-msg | |
1724 | "c-forward-sws cached move %s -> %s (max %s)" | |
1725 | rung-pos (point) (point-max)) | |
1726 | ||
1727 | (setq rung-pos (point)) | |
1728 | (and (> (skip-chars-forward " \t\n\r\f\v") 0) | |
1729 | (not (eobp)))) | |
1730 | ||
1731 | ;; We'll loop here if there is simple ws after the last rung. | |
1732 | ;; That means that there's been some change in it and it's | |
1733 | ;; possible that we've stepped into another ladder, so extend | |
1734 | ;; the previous one to join with it if there is one, and try to | |
1735 | ;; use the cache again. | |
1736 | (c-debug-sws-msg | |
1737 | "c-forward-sws extending rung with [%s..%s] (max %s)" | |
1738 | (1+ rung-pos) (1+ (point)) (point-max)) | |
1739 | (unless (get-text-property (point) 'c-is-sws) | |
1740 | ;; Remove any `c-in-sws' property from the last char of | |
1741 | ;; the rung before we mark it with `c-is-sws', so that we | |
1742 | ;; won't connect with the remains of a broken "ladder". | |
1743 | (c-remove-in-sws (point) (1+ (point)))) | |
1744 | (c-put-is-sws (1+ rung-pos) | |
1745 | (1+ (point))) | |
1746 | (c-put-in-sws rung-pos | |
1747 | (setq rung-pos (point) | |
1748 | last-put-in-sws-pos rung-pos))) | |
1749 | ||
1750 | (setq simple-ws-end (point)) | |
1751 | (c-forward-comments) | |
1752 | ||
1753 | (cond | |
1754 | ((/= (point) simple-ws-end) | |
1755 | ;; Skipped over comments. Don't cache at eob in case the buffer | |
1756 | ;; is narrowed. | |
1757 | (not (eobp))) | |
1758 | ||
1759 | ((save-excursion | |
1760 | (and c-opt-cpp-prefix | |
1761 | (looking-at c-opt-cpp-start) | |
1762 | (progn (skip-chars-backward " \t") | |
1763 | (bolp)) | |
1764 | (or (bobp) | |
1765 | (progn (backward-char) | |
1766 | (not (eq (char-before) ?\\)))))) | |
1767 | ;; Skip a preprocessor directive. | |
1768 | (end-of-line) | |
1769 | (while (and (eq (char-before) ?\\) | |
1770 | (= (forward-line 1) 0)) | |
1771 | (end-of-line)) | |
1772 | (forward-line 1) | |
1773 | (setq safe-start t) | |
1774 | ;; Don't cache at eob in case the buffer is narrowed. | |
1775 | (not (eobp))))) | |
1776 | ||
1777 | ;; We've searched over a piece of non-white syntactic ws. See if this | |
1778 | ;; can be cached. | |
1779 | (setq next-rung-pos (point)) | |
1780 | (skip-chars-forward " \t\n\r\f\v") | |
1781 | (setq rung-end-pos (min (1+ (point)) (point-max))) | |
1782 | ||
1783 | (if (or | |
1784 | ;; Cache if we haven't skipped comments only, and if we started | |
1785 | ;; either from a marked rung or from a completely uncached | |
1786 | ;; position. | |
1787 | (and safe-start | |
1788 | (or rung-is-marked | |
1789 | (not (get-text-property simple-ws-end 'c-in-sws)))) | |
1790 | ||
1791 | ;; See if there's a marked rung in the encountered simple ws. If | |
1792 | ;; so then we can cache, unless `safe-start' is nil. Even then | |
1793 | ;; we need to do this to check if the cache can be used for the | |
1794 | ;; next step. | |
1795 | (and (setq next-rung-is-marked | |
1796 | (text-property-any next-rung-pos rung-end-pos | |
1797 | 'c-is-sws t)) | |
1798 | safe-start)) | |
b2acd789 | 1799 | |
0ec8351b | 1800 | (progn |
d9e94c22 MS |
1801 | (c-debug-sws-msg |
1802 | "c-forward-sws caching [%s..%s] - [%s..%s] (max %s)" | |
1803 | rung-pos (1+ simple-ws-end) next-rung-pos rung-end-pos | |
1804 | (point-max)) | |
1805 | ||
1806 | ;; Remove the properties for any nested ws that might be cached. | |
1807 | ;; Only necessary for `c-is-sws' since `c-in-sws' will be set | |
1808 | ;; anyway. | |
1809 | (c-remove-is-sws (1+ simple-ws-end) next-rung-pos) | |
1810 | (unless (and rung-is-marked (= rung-pos simple-ws-end)) | |
1811 | (c-put-is-sws rung-pos | |
1812 | (1+ simple-ws-end)) | |
1813 | (setq rung-is-marked t)) | |
1814 | (c-put-in-sws rung-pos | |
1815 | (setq rung-pos (point) | |
1816 | last-put-in-sws-pos rung-pos)) | |
1817 | (unless (get-text-property (1- rung-end-pos) 'c-is-sws) | |
1818 | ;; Remove any `c-in-sws' property from the last char of | |
1819 | ;; the rung before we mark it with `c-is-sws', so that we | |
1820 | ;; won't connect with the remains of a broken "ladder". | |
1821 | (c-remove-in-sws (1- rung-end-pos) rung-end-pos)) | |
1822 | (c-put-is-sws next-rung-pos | |
1823 | rung-end-pos)) | |
1824 | ||
1825 | (c-debug-sws-msg | |
1826 | "c-forward-sws not caching [%s..%s] - [%s..%s] (max %s)" | |
1827 | rung-pos (1+ simple-ws-end) next-rung-pos rung-end-pos | |
1828 | (point-max)) | |
1829 | ||
1830 | ;; Set `rung-pos' for the next rung. It's the same thing here as | |
1831 | ;; initially, except that the rung position is set as early as | |
1832 | ;; possible since we can't be in the ending ws of a line comment or | |
1833 | ;; cpp directive now. | |
1834 | (if (setq rung-is-marked next-rung-is-marked) | |
1835 | (setq rung-pos (1- (next-single-property-change | |
1836 | rung-is-marked 'c-is-sws nil rung-end-pos))) | |
1837 | (setq rung-pos next-rung-pos)) | |
1838 | (setq safe-start t))) | |
1839 | ||
1840 | ;; Make sure that the newly marked `c-in-sws' region doesn't connect to | |
1841 | ;; another one after the point (which might occur when editing inside a | |
1842 | ;; comment or macro). | |
1843 | (when (eq last-put-in-sws-pos (point)) | |
1844 | (cond ((< last-put-in-sws-pos (point-max)) | |
1845 | (c-debug-sws-msg | |
1846 | "c-forward-sws clearing at %s for cache separation" | |
1847 | last-put-in-sws-pos) | |
1848 | (c-remove-in-sws last-put-in-sws-pos | |
1849 | (1+ last-put-in-sws-pos))) | |
1850 | (t | |
1851 | ;; If at eob we have to clear the last character before the end | |
1852 | ;; instead since the buffer might be narrowed and there might | |
1853 | ;; be a `c-in-sws' after (point-max). In this case it's | |
1854 | ;; necessary to clear both properties. | |
1855 | (c-debug-sws-msg | |
1856 | "c-forward-sws clearing thoroughly at %s for cache separation" | |
1857 | (1- last-put-in-sws-pos)) | |
1858 | (c-remove-is-and-in-sws (1- last-put-in-sws-pos) | |
1859 | last-put-in-sws-pos)))) | |
2143fa32 | 1860 | )))) |
b2acd789 | 1861 | |
d9e94c22 MS |
1862 | (defun c-backward-sws () |
1863 | ;; Used by `c-backward-syntactic-ws' to implement the unbounded search. | |
0386b551 AM |
1864 | ;; |
1865 | ;; This function might do hidden buffer changes. | |
d9e94c22 MS |
1866 | |
1867 | (let (;; `rung-pos' is set to a position as late as possible in the unmarked | |
1868 | ;; part of the simple ws region. | |
1869 | (rung-pos (point)) next-rung-pos last-put-in-sws-pos | |
1870 | rung-is-marked simple-ws-beg cmt-skip-pos) | |
1871 | ||
1872 | ;; Skip simple horizontal ws and do a quick check on the preceding | |
91af3942 | 1873 | ;; character to see if it's anything that can't end syntactic ws, so we can |
d9e94c22 MS |
1874 | ;; bail out early in the majority of cases when there just are a few ws |
1875 | ;; chars. Newlines are complicated in the backward direction, so we can't | |
1876 | ;; skip over them. | |
1877 | (skip-chars-backward " \t\f") | |
1878 | (when (and (not (bobp)) | |
1879 | (save-excursion | |
1880 | (backward-char) | |
1881 | (looking-at c-syntactic-ws-end))) | |
1882 | ||
1883 | ;; Try to find a rung position in the simple ws preceding point, so that | |
1884 | ;; we can get a cache hit even if the last bit of the simple ws has | |
1885 | ;; changed recently. | |
1886 | (setq simple-ws-beg (point)) | |
1887 | (skip-chars-backward " \t\n\r\f\v") | |
1888 | (if (setq rung-is-marked (text-property-any | |
1889 | (point) (min (1+ rung-pos) (point-max)) | |
1890 | 'c-is-sws t)) | |
1891 | ;; `rung-pos' will be the earliest marked position, which means that | |
1892 | ;; there might be later unmarked parts in the simple ws region. | |
1893 | ;; It's not worth the effort to fix that; the last part of the | |
1894 | ;; simple ws is also typically edited often, so it could be wasted. | |
1895 | (goto-char (setq rung-pos rung-is-marked)) | |
1896 | (goto-char simple-ws-beg)) | |
1897 | ||
2143fa32 | 1898 | (with-silent-modifications |
d9e94c22 MS |
1899 | (while |
1900 | (progn | |
1901 | (while | |
1902 | (when (and rung-is-marked | |
1903 | (not (bobp)) | |
1904 | (get-text-property (1- (point)) 'c-in-sws)) | |
1905 | ||
1906 | ;; The following search is the main reason that `c-in-sws' | |
1907 | ;; and `c-is-sws' aren't combined to one property. | |
1908 | (goto-char (previous-single-property-change | |
1909 | (point) 'c-in-sws nil (point-min))) | |
1910 | (unless (get-text-property (point) 'c-is-sws) | |
1911 | ;; If the `c-in-sws' region extended past the first | |
1912 | ;; `c-is-sws' char we have to go forward a bit. | |
1913 | (goto-char (next-single-property-change | |
1914 | (point) 'c-is-sws))) | |
1915 | ||
1916 | (c-debug-sws-msg | |
1917 | "c-backward-sws cached move %s <- %s (min %s)" | |
1918 | (point) rung-pos (point-min)) | |
1919 | ||
1920 | (setq rung-pos (point)) | |
1921 | (if (and (< (min (skip-chars-backward " \t\f\v") | |
1922 | (progn | |
1923 | (setq simple-ws-beg (point)) | |
1924 | (skip-chars-backward " \t\n\r\f\v"))) | |
1925 | 0) | |
1926 | (setq rung-is-marked | |
1927 | (text-property-any (point) rung-pos | |
1928 | 'c-is-sws t))) | |
1929 | t | |
1930 | (goto-char simple-ws-beg) | |
1931 | nil)) | |
1932 | ||
1933 | ;; We'll loop here if there is simple ws before the first rung. | |
1934 | ;; That means that there's been some change in it and it's | |
1935 | ;; possible that we've stepped into another ladder, so extend | |
1936 | ;; the previous one to join with it if there is one, and try to | |
1937 | ;; use the cache again. | |
1938 | (c-debug-sws-msg | |
1939 | "c-backward-sws extending rung with [%s..%s] (min %s)" | |
1940 | rung-is-marked rung-pos (point-min)) | |
1941 | (unless (get-text-property (1- rung-pos) 'c-is-sws) | |
1942 | ;; Remove any `c-in-sws' property from the last char of | |
1943 | ;; the rung before we mark it with `c-is-sws', so that we | |
1944 | ;; won't connect with the remains of a broken "ladder". | |
1945 | (c-remove-in-sws (1- rung-pos) rung-pos)) | |
1946 | (c-put-is-sws rung-is-marked | |
1947 | rung-pos) | |
1948 | (c-put-in-sws rung-is-marked | |
1949 | (1- rung-pos)) | |
1950 | (setq rung-pos rung-is-marked | |
1951 | last-put-in-sws-pos rung-pos)) | |
1952 | ||
1953 | (c-backward-comments) | |
1954 | (setq cmt-skip-pos (point)) | |
a66cd3ee | 1955 | |
d9e94c22 MS |
1956 | (cond |
1957 | ((and c-opt-cpp-prefix | |
1958 | (/= cmt-skip-pos simple-ws-beg) | |
1959 | (c-beginning-of-macro)) | |
1960 | ;; Inside a cpp directive. See if it should be skipped over. | |
1961 | (let ((cpp-beg (point))) | |
1962 | ||
1963 | ;; Move back over all line continuations in the region skipped | |
1964 | ;; over by `c-backward-comments'. If we go past it then we | |
1965 | ;; started inside the cpp directive. | |
1966 | (goto-char simple-ws-beg) | |
1967 | (beginning-of-line) | |
1968 | (while (and (> (point) cmt-skip-pos) | |
1969 | (progn (backward-char) | |
1970 | (eq (char-before) ?\\))) | |
1971 | (beginning-of-line)) | |
1972 | ||
1973 | (if (< (point) cmt-skip-pos) | |
1974 | ;; Don't move past the cpp directive if we began inside | |
1975 | ;; it. Note that the position at the end of the last line | |
1976 | ;; of the macro is also considered to be within it. | |
1977 | (progn (goto-char cmt-skip-pos) | |
1978 | nil) | |
1979 | ||
1980 | ;; It's worthwhile to spend a little bit of effort on finding | |
1981 | ;; the end of the macro, to get a good `simple-ws-beg' | |
1982 | ;; position for the cache. Note that `c-backward-comments' | |
1983 | ;; could have stepped over some comments before going into | |
1984 | ;; the macro, and then `simple-ws-beg' must be kept on the | |
1985 | ;; same side of those comments. | |
1986 | (goto-char simple-ws-beg) | |
1987 | (skip-chars-backward " \t\n\r\f\v") | |
1988 | (if (eq (char-before) ?\\) | |
1989 | (forward-char)) | |
1990 | (forward-line 1) | |
1991 | (if (< (point) simple-ws-beg) | |
1992 | ;; Might happen if comments after the macro were skipped | |
1993 | ;; over. | |
1994 | (setq simple-ws-beg (point))) | |
1995 | ||
1996 | (goto-char cpp-beg) | |
1997 | t))) | |
1998 | ||
1999 | ((/= (save-excursion | |
2000 | (skip-chars-forward " \t\n\r\f\v" simple-ws-beg) | |
2001 | (setq next-rung-pos (point))) | |
2002 | simple-ws-beg) | |
2003 | ;; Skipped over comments. Must put point at the end of | |
2004 | ;; the simple ws at point since we might be after a line | |
2005 | ;; comment or cpp directive that's been partially | |
2006 | ;; narrowed out, and we can't risk marking the simple ws | |
2007 | ;; at the end of it. | |
2008 | (goto-char next-rung-pos) | |
2009 | t))) | |
2010 | ||
2011 | ;; We've searched over a piece of non-white syntactic ws. See if this | |
2012 | ;; can be cached. | |
2013 | (setq next-rung-pos (point)) | |
2014 | (skip-chars-backward " \t\f\v") | |
2015 | ||
2016 | (if (or | |
2017 | ;; Cache if we started either from a marked rung or from a | |
2018 | ;; completely uncached position. | |
2019 | rung-is-marked | |
2020 | (not (get-text-property (1- simple-ws-beg) 'c-in-sws)) | |
2021 | ||
2022 | ;; Cache if there's a marked rung in the encountered simple ws. | |
2023 | (save-excursion | |
2024 | (skip-chars-backward " \t\n\r\f\v") | |
2025 | (text-property-any (point) (min (1+ next-rung-pos) (point-max)) | |
2026 | 'c-is-sws t))) | |
a66cd3ee | 2027 | |
d9e94c22 MS |
2028 | (progn |
2029 | (c-debug-sws-msg | |
2030 | "c-backward-sws caching [%s..%s] - [%s..%s] (min %s)" | |
2031 | (point) (1+ next-rung-pos) | |
2032 | simple-ws-beg (min (1+ rung-pos) (point-max)) | |
2033 | (point-min)) | |
2034 | ||
2035 | ;; Remove the properties for any nested ws that might be cached. | |
2036 | ;; Only necessary for `c-is-sws' since `c-in-sws' will be set | |
2037 | ;; anyway. | |
2038 | (c-remove-is-sws (1+ next-rung-pos) simple-ws-beg) | |
2039 | (unless (and rung-is-marked (= simple-ws-beg rung-pos)) | |
2040 | (let ((rung-end-pos (min (1+ rung-pos) (point-max)))) | |
2041 | (unless (get-text-property (1- rung-end-pos) 'c-is-sws) | |
2042 | ;; Remove any `c-in-sws' property from the last char of | |
2043 | ;; the rung before we mark it with `c-is-sws', so that we | |
2044 | ;; won't connect with the remains of a broken "ladder". | |
2045 | (c-remove-in-sws (1- rung-end-pos) rung-end-pos)) | |
2046 | (c-put-is-sws simple-ws-beg | |
2047 | rung-end-pos) | |
2048 | (setq rung-is-marked t))) | |
2049 | (c-put-in-sws (setq simple-ws-beg (point) | |
2050 | last-put-in-sws-pos simple-ws-beg) | |
2051 | rung-pos) | |
2052 | (c-put-is-sws (setq rung-pos simple-ws-beg) | |
2053 | (1+ next-rung-pos))) | |
2054 | ||
2055 | (c-debug-sws-msg | |
2056 | "c-backward-sws not caching [%s..%s] - [%s..%s] (min %s)" | |
2057 | (point) (1+ next-rung-pos) | |
2058 | simple-ws-beg (min (1+ rung-pos) (point-max)) | |
2059 | (point-min)) | |
2060 | (setq rung-pos next-rung-pos | |
2061 | simple-ws-beg (point)) | |
2062 | )) | |
2063 | ||
2064 | ;; Make sure that the newly marked `c-in-sws' region doesn't connect to | |
2065 | ;; another one before the point (which might occur when editing inside a | |
2066 | ;; comment or macro). | |
2067 | (when (eq last-put-in-sws-pos (point)) | |
2068 | (cond ((< (point-min) last-put-in-sws-pos) | |
2069 | (c-debug-sws-msg | |
2070 | "c-backward-sws clearing at %s for cache separation" | |
2071 | (1- last-put-in-sws-pos)) | |
2072 | (c-remove-in-sws (1- last-put-in-sws-pos) | |
2073 | last-put-in-sws-pos)) | |
2074 | ((> (point-min) 1) | |
2075 | ;; If at bob and the buffer is narrowed, we have to clear the | |
2076 | ;; character we're standing on instead since there might be a | |
2077 | ;; `c-in-sws' before (point-min). In this case it's necessary | |
2078 | ;; to clear both properties. | |
2079 | (c-debug-sws-msg | |
2080 | "c-backward-sws clearing thoroughly at %s for cache separation" | |
2081 | last-put-in-sws-pos) | |
2082 | (c-remove-is-and-in-sws last-put-in-sws-pos | |
2083 | (1+ last-put-in-sws-pos))))) | |
2143fa32 | 2084 | )))) |
785eecbb | 2085 | |
d9e94c22 | 2086 | \f |
580fba94 AM |
2087 | ;; Other whitespace tools |
2088 | (defun c-partial-ws-p (beg end) | |
2089 | ;; Is the region (beg end) WS, and is there WS (or BOB/EOB) next to the | |
2090 | ;; region? This is a "heuristic" function. ..... | |
b414f371 | 2091 | ;; |
c421028a AM |
2092 | ;; The motivation for the second bit is to check whether removing this |
2093 | ;; region would coalesce two symbols. | |
580fba94 AM |
2094 | ;; |
2095 | ;; FIXME!!! This function doesn't check virtual semicolons in any way. Be | |
2096 | ;; careful about using this function for, e.g. AWK. (2007/3/7) | |
2097 | (save-excursion | |
2098 | (let ((end+1 (min (1+ end) (point-max)))) | |
2099 | (or (progn (goto-char (max (point-min) (1- beg))) | |
2100 | (c-skip-ws-forward end) | |
2101 | (eq (point) end)) | |
2102 | (progn (goto-char beg) | |
2103 | (c-skip-ws-forward end+1) | |
2104 | (eq (point) end+1)))))) | |
2105 | \f | |
0386b551 | 2106 | ;; A system for finding noteworthy parens before the point. |
e1c458ae | 2107 | |
0ec1d2c5 AM |
2108 | (defconst c-state-cache-too-far 5000) |
2109 | ;; A maximum comfortable scanning distance, e.g. between | |
2110 | ;; `c-state-cache-good-pos' and "HERE" (where we call c-parse-state). When | |
0d26e0b6 | 2111 | ;; this distance is exceeded, we take "emergency measures", e.g. by clearing |
0ec1d2c5 AM |
2112 | ;; the cache and starting again from point-min or a beginning of defun. This |
2113 | ;; value can be tuned for efficiency or set to a lower value for testing. | |
2114 | ||
d9e94c22 MS |
2115 | (defvar c-state-cache nil) |
2116 | (make-variable-buffer-local 'c-state-cache) | |
2117 | ;; The state cache used by `c-parse-state' to cut down the amount of | |
0ec1d2c5 AM |
2118 | ;; searching. It's the result from some earlier `c-parse-state' call. See |
2119 | ;; `c-parse-state''s doc string for details of its structure. | |
0386b551 | 2120 | ;; |
d9e94c22 MS |
2121 | ;; The use of the cached info is more effective if the next |
2122 | ;; `c-parse-state' call is on a line close by the one the cached state | |
2123 | ;; was made at; the cache can actually slow down a little if the | |
2124 | ;; cached state was made very far back in the buffer. The cache is | |
2125 | ;; most effective if `c-parse-state' is used on each line while moving | |
2126 | ;; forward. | |
e1c458ae | 2127 | |
0386b551 AM |
2128 | (defvar c-state-cache-good-pos 1) |
2129 | (make-variable-buffer-local 'c-state-cache-good-pos) | |
0ec1d2c5 AM |
2130 | ;; This is a position where `c-state-cache' is known to be correct, or |
2131 | ;; nil (see below). It's a position inside one of the recorded unclosed | |
2132 | ;; parens or the top level, but not further nested inside any literal or | |
2133 | ;; subparen that is closed before the last recorded position. | |
0386b551 AM |
2134 | ;; |
2135 | ;; The exact position is chosen to try to be close to yet earlier than | |
2136 | ;; the position where `c-state-cache' will be called next. Right now | |
2137 | ;; the heuristic is to set it to the position after the last found | |
2138 | ;; closing paren (of any type) before the line on which | |
2139 | ;; `c-parse-state' was called. That is chosen primarily to work well | |
2140 | ;; with refontification of the current line. | |
0ec1d2c5 AM |
2141 | ;; |
2142 | ;; 2009-07-28: When `c-state-point-min' and the last position where | |
2143 | ;; `c-parse-state' or for which `c-invalidate-state-cache' was called, are | |
2144 | ;; both in the same literal, there is no such "good position", and | |
2145 | ;; c-state-cache-good-pos is then nil. This is the ONLY circumstance in which | |
2146 | ;; it can be nil. In this case, `c-state-point-min-literal' will be non-nil. | |
2147 | ;; | |
2148 | ;; 2009-06-12: In a brace desert, c-state-cache-good-pos may also be in | |
2149 | ;; the middle of the desert, as long as it is not within a brace pair | |
2150 | ;; recorded in `c-state-cache' or a paren/bracket pair. | |
2151 | ||
2152 | ||
2153 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | |
2154 | ;; We maintain a simple cache of positions which aren't in a literal, so as to | |
2155 | ;; speed up testing for non-literality. | |
9657183b | 2156 | (defconst c-state-nonlit-pos-interval 3000) |
0ec1d2c5 AM |
2157 | ;; The approximate interval between entries in `c-state-nonlit-pos-cache'. |
2158 | ||
2159 | (defvar c-state-nonlit-pos-cache nil) | |
2160 | (make-variable-buffer-local 'c-state-nonlit-pos-cache) | |
e84efb70 AM |
2161 | ;; A list of buffer positions which are known not to be in a literal or a cpp |
2162 | ;; construct. This is ordered with higher positions at the front of the list. | |
2163 | ;; Only those which are less than `c-state-nonlit-pos-cache-limit' are valid. | |
0ec1d2c5 AM |
2164 | |
2165 | (defvar c-state-nonlit-pos-cache-limit 1) | |
2166 | (make-variable-buffer-local 'c-state-nonlit-pos-cache-limit) | |
2167 | ;; An upper limit on valid entries in `c-state-nonlit-pos-cache'. This is | |
2168 | ;; reduced by buffer changes, and increased by invocations of | |
2169 | ;; `c-state-literal-at'. | |
2170 | ||
56d093a9 AM |
2171 | (defvar c-state-semi-nonlit-pos-cache nil) |
2172 | (make-variable-buffer-local 'c-state-semi-nonlit-pos-cache) | |
2173 | ;; A list of buffer positions which are known not to be in a literal. This is | |
2174 | ;; ordered with higher positions at the front of the list. Only those which | |
2175 | ;; are less than `c-state-semi-nonlit-pos-cache-limit' are valid. | |
2176 | ||
2177 | (defvar c-state-semi-nonlit-pos-cache-limit 1) | |
2178 | (make-variable-buffer-local 'c-state-semi-nonlit-pos-cache-limit) | |
2179 | ;; An upper limit on valid entries in `c-state-semi-nonlit-pos-cache'. This is | |
2180 | ;; reduced by buffer changes, and increased by invocations of | |
f003f294 | 2181 | ;; `c-state-literal-at'. FIXME!!! |
56d093a9 | 2182 | |
0ec1d2c5 | 2183 | (defsubst c-state-pp-to-literal (from to) |
13d49cbb AM |
2184 | ;; Do a parse-partial-sexp from FROM to TO, returning either |
2185 | ;; (STATE TYPE (BEG . END)) if TO is in a literal; or | |
2186 | ;; (STATE) otherwise, | |
2187 | ;; where STATE is the parsing state at TO, TYPE is the type of the literal | |
2188 | ;; (one of 'c, 'c++, 'string) and (BEG . END) is the boundaries of the literal. | |
2189 | ;; | |
2190 | ;; Only elements 3 (in a string), 4 (in a comment), 5 (following a quote), | |
2191 | ;; 7 (comment type) and 8 (start of comment/string) (and possibly 9) of | |
2192 | ;; STATE are valid. | |
0ec1d2c5 | 2193 | (save-excursion |
13d49cbb AM |
2194 | (let ((s (parse-partial-sexp from to)) |
2195 | ty) | |
0ec1d2c5 | 2196 | (when (or (nth 3 s) (nth 4 s)) ; in a string or comment |
13d49cbb AM |
2197 | (setq ty (cond |
2198 | ((nth 3 s) 'string) | |
2199 | ((eq (nth 7 s) t) 'c++) | |
2200 | (t 'c))) | |
0ec1d2c5 AM |
2201 | (parse-partial-sexp (point) (point-max) |
2202 | nil ; TARGETDEPTH | |
2203 | nil ; STOPBEFORE | |
2204 | s ; OLDSTATE | |
13d49cbb AM |
2205 | 'syntax-table)) ; stop at end of literal |
2206 | (if ty | |
2207 | `(,s ,ty (,(nth 8 s) . ,(point))) | |
2208 | `(,s))))) | |
2209 | ||
2210 | (defun c-state-safe-place (here) | |
2211 | ;; Return a buffer position before HERE which is "safe", i.e. outside any | |
2212 | ;; string, comment, or macro. | |
e84efb70 AM |
2213 | ;; |
2214 | ;; NOTE: This function manipulates `c-state-nonlit-pos-cache'. This cache | |
2215 | ;; MAY NOT contain any positions within macros, since macros are frequently | |
2216 | ;; turned into comments by use of the `c-cpp-delimiter' category properties. | |
2217 | ;; We cannot rely on this mechanism whilst determining a cache pos since | |
2218 | ;; this function is also called from outwith `c-parse-state'. | |
0ec1d2c5 AM |
2219 | (save-restriction |
2220 | (widen) | |
2221 | (save-excursion | |
2222 | (let ((c c-state-nonlit-pos-cache) | |
56d093a9 | 2223 | pos npos high-pos lit macro-beg macro-end) |
0ec1d2c5 AM |
2224 | ;; Trim the cache to take account of buffer changes. |
2225 | (while (and c (> (car c) c-state-nonlit-pos-cache-limit)) | |
2226 | (setq c (cdr c))) | |
2227 | (setq c-state-nonlit-pos-cache c) | |
2228 | ||
2229 | (while (and c (> (car c) here)) | |
56d093a9 | 2230 | (setq high-pos (car c)) |
0ec1d2c5 AM |
2231 | (setq c (cdr c))) |
2232 | (setq pos (or (car c) (point-min))) | |
2233 | ||
56d093a9 AM |
2234 | (unless high-pos |
2235 | (while | |
2236 | ;; Add an element to `c-state-nonlit-pos-cache' each iteration. | |
2237 | (and | |
2238 | (<= (setq npos (+ pos c-state-nonlit-pos-interval)) here) | |
2239 | ||
2240 | ;; Test for being in a literal. If so, go to after it. | |
2241 | (progn | |
2242 | (setq lit (car (cddr (c-state-pp-to-literal pos npos)))) | |
2243 | (or (null lit) | |
2244 | (prog1 (<= (cdr lit) here) | |
2245 | (setq npos (cdr lit))))) | |
2246 | ||
2247 | ;; Test for being in a macro. If so, go to after it. | |
2248 | (progn | |
2249 | (goto-char npos) | |
2250 | (setq macro-beg | |
2251 | (and (c-beginning-of-macro) (/= (point) npos) (point))) | |
2252 | (when macro-beg | |
2253 | (c-syntactic-end-of-macro) | |
2254 | (or (eobp) (forward-char)) | |
2255 | (setq macro-end (point))) | |
2256 | (or (null macro-beg) | |
2257 | (prog1 (<= macro-end here) | |
2258 | (setq npos macro-end))))) | |
2259 | ||
2260 | (setq pos npos) | |
2261 | (setq c-state-nonlit-pos-cache (cons pos c-state-nonlit-pos-cache))) | |
2262 | ;; Add one extra element above HERE so as to to avoid the previous | |
2263 | ;; expensive calculation when the next call is close to the current | |
2264 | ;; one. This is especially useful when inside a large macro. | |
2265 | (setq c-state-nonlit-pos-cache (cons npos c-state-nonlit-pos-cache))) | |
0ec1d2c5 AM |
2266 | |
2267 | (if (> pos c-state-nonlit-pos-cache-limit) | |
2268 | (setq c-state-nonlit-pos-cache-limit pos)) | |
13d49cbb | 2269 | pos)))) |
9858f6c3 | 2270 | |
56d093a9 AM |
2271 | (defun c-state-semi-safe-place (here) |
2272 | ;; Return a buffer position before HERE which is "safe", i.e. outside any | |
2273 | ;; string or comment. It may be in a macro. | |
2274 | (save-restriction | |
2275 | (widen) | |
2276 | (save-excursion | |
2277 | (let ((c c-state-semi-nonlit-pos-cache) | |
2278 | pos npos high-pos lit macro-beg macro-end) | |
2279 | ;; Trim the cache to take account of buffer changes. | |
2280 | (while (and c (> (car c) c-state-semi-nonlit-pos-cache-limit)) | |
2281 | (setq c (cdr c))) | |
2282 | (setq c-state-semi-nonlit-pos-cache c) | |
2283 | ||
2284 | (while (and c (> (car c) here)) | |
2285 | (setq high-pos (car c)) | |
2286 | (setq c (cdr c))) | |
2287 | (setq pos (or (car c) (point-min))) | |
2288 | ||
2289 | (unless high-pos | |
2290 | (while | |
2291 | ;; Add an element to `c-state-semi-nonlit-pos-cache' each iteration. | |
2292 | (and | |
2293 | (<= (setq npos (+ pos c-state-nonlit-pos-interval)) here) | |
2294 | ||
2295 | ;; Test for being in a literal. If so, go to after it. | |
2296 | (progn | |
2297 | (setq lit (car (cddr (c-state-pp-to-literal pos npos)))) | |
2298 | (or (null lit) | |
2299 | (prog1 (<= (cdr lit) here) | |
2300 | (setq npos (cdr lit)))))) | |
2301 | ||
2302 | (setq pos npos) | |
2303 | (setq c-state-semi-nonlit-pos-cache | |
2304 | (cons pos c-state-semi-nonlit-pos-cache)))) | |
2305 | ||
2306 | (if (> pos c-state-semi-nonlit-pos-cache-limit) | |
2307 | (setq c-state-semi-nonlit-pos-cache-limit pos)) | |
2308 | pos)))) | |
2309 | ||
13d49cbb AM |
2310 | (defun c-state-literal-at (here) |
2311 | ;; If position HERE is inside a literal, return (START . END), the | |
2312 | ;; boundaries of the literal (which may be outside the accessible bit of the | |
2313 | ;; buffer). Otherwise, return nil. | |
2314 | ;; | |
2315 | ;; This function is almost the same as `c-literal-limits'. Previously, it | |
9858f6c3 | 2316 | ;; differed in that it was a lower level function, and that it rigorously |
13d49cbb AM |
2317 | ;; followed the syntax from BOB. `c-literal-limits' is now (2011-12) |
2318 | ;; virtually identical to this function. | |
2319 | (save-restriction | |
2320 | (widen) | |
2321 | (save-excursion | |
2322 | (let ((pos (c-state-safe-place here))) | |
2323 | (car (cddr (c-state-pp-to-literal pos here))))))) | |
0ec1d2c5 AM |
2324 | |
2325 | (defsubst c-state-lit-beg (pos) | |
2326 | ;; Return the start of the literal containing POS, or POS itself. | |
2327 | (or (car (c-state-literal-at pos)) | |
2328 | pos)) | |
2329 | ||
2330 | (defsubst c-state-cache-non-literal-place (pos state) | |
9c4aeabf | 2331 | ;; Return a position outside of a string/comment/macro at or before POS. |
0ec1d2c5 | 2332 | ;; STATE is the parse-partial-sexp state at POS. |
9c4aeabf AM |
2333 | (let ((res (if (or (nth 3 state) ; in a string? |
2334 | (nth 4 state)) ; in a comment? | |
2335 | (nth 8 state) | |
2336 | pos))) | |
2337 | (save-excursion | |
2338 | (goto-char res) | |
2339 | (if (c-beginning-of-macro) | |
2340 | (point) | |
2341 | res)))) | |
0ec1d2c5 AM |
2342 | |
2343 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | |
2344 | ;; Stuff to do with point-min, and coping with any literal there. | |
2345 | (defvar c-state-point-min 1) | |
2346 | (make-variable-buffer-local 'c-state-point-min) | |
2347 | ;; This is (point-min) when `c-state-cache' was last calculated. A change of | |
2348 | ;; narrowing is likely to affect the parens that are visible before the point. | |
2349 | ||
2350 | (defvar c-state-point-min-lit-type nil) | |
2351 | (make-variable-buffer-local 'c-state-point-min-lit-type) | |
2352 | (defvar c-state-point-min-lit-start nil) | |
2353 | (make-variable-buffer-local 'c-state-point-min-lit-start) | |
2354 | ;; These two variables define the literal, if any, containing point-min. | |
2355 | ;; Their values are, respectively, 'string, c, or c++, and the start of the | |
2356 | ;; literal. If there's no literal there, they're both nil. | |
2357 | ||
2358 | (defvar c-state-min-scan-pos 1) | |
2359 | (make-variable-buffer-local 'c-state-min-scan-pos) | |
2360 | ;; This is the earliest buffer-pos from which scanning can be done. It is | |
2361 | ;; either the end of the literal containing point-min, or point-min itself. | |
2362 | ;; It becomes nil if the buffer is changed earlier than this point. | |
2363 | (defun c-state-get-min-scan-pos () | |
2364 | ;; Return the lowest valid scanning pos. This will be the end of the | |
2365 | ;; literal enclosing point-min, or point-min itself. | |
2366 | (or c-state-min-scan-pos | |
2367 | (save-restriction | |
2368 | (save-excursion | |
2369 | (widen) | |
2370 | (goto-char c-state-point-min-lit-start) | |
2371 | (if (eq c-state-point-min-lit-type 'string) | |
2372 | (forward-sexp) | |
2373 | (forward-comment 1)) | |
2374 | (setq c-state-min-scan-pos (point)))))) | |
2375 | ||
2376 | (defun c-state-mark-point-min-literal () | |
2377 | ;; Determine the properties of any literal containing POINT-MIN, setting the | |
2378 | ;; variables `c-state-point-min-lit-type', `c-state-point-min-lit-start', | |
2379 | ;; and `c-state-min-scan-pos' accordingly. The return value is meaningless. | |
2380 | (let ((p-min (point-min)) | |
2381 | lit) | |
2382 | (save-restriction | |
2383 | (widen) | |
2384 | (setq lit (c-state-literal-at p-min)) | |
2385 | (if lit | |
2386 | (setq c-state-point-min-lit-type | |
2387 | (save-excursion | |
2388 | (goto-char (car lit)) | |
2389 | (cond | |
2390 | ((looking-at c-block-comment-start-regexp) 'c) | |
2391 | ((looking-at c-line-comment-starter) 'c++) | |
2392 | (t 'string))) | |
2393 | c-state-point-min-lit-start (car lit) | |
2394 | c-state-min-scan-pos (cdr lit)) | |
2395 | (setq c-state-point-min-lit-type nil | |
2396 | c-state-point-min-lit-start nil | |
2397 | c-state-min-scan-pos p-min))))) | |
2398 | ||
2399 | ||
2400 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | |
2401 | ;; A variable which signals a brace dessert - helpful for reducing the number | |
2402 | ;; of fruitless backward scans. | |
2403 | (defvar c-state-brace-pair-desert nil) | |
2404 | (make-variable-buffer-local 'c-state-brace-pair-desert) | |
e84efb70 | 2405 | ;; Used only in `c-append-lower-brace-pair-to-state-cache'. It is set when |
0ec1d2c5 AM |
2406 | ;; that defun has searched backwards for a brace pair and not found one. Its |
2407 | ;; value is either nil or a cons (PA . FROM), where PA is the position of the | |
2408 | ;; enclosing opening paren/brace/bracket which bounds the backwards search (or | |
2409 | ;; nil when at top level) and FROM is where the backward search started. It | |
2410 | ;; is reset to nil in `c-invalidate-state-cache'. | |
2411 | ||
2412 | ||
2413 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | |
2414 | ;; Lowish level functions/macros which work directly on `c-state-cache', or a | |
2415 | ;; list of like structure. | |
2416 | (defmacro c-state-cache-top-lparen (&optional cache) | |
2417 | ;; Return the address of the top left brace/bracket/paren recorded in CACHE | |
2418 | ;; (default `c-state-cache') (or nil). | |
2419 | (let ((cash (or cache 'c-state-cache))) | |
2420 | `(if (consp (car ,cash)) | |
2421 | (caar ,cash) | |
2422 | (car ,cash)))) | |
2423 | ||
2424 | (defmacro c-state-cache-top-paren (&optional cache) | |
2425 | ;; Return the address of the latest brace/bracket/paren (whether left or | |
2426 | ;; right) recorded in CACHE (default `c-state-cache') or nil. | |
2427 | (let ((cash (or cache 'c-state-cache))) | |
2428 | `(if (consp (car ,cash)) | |
2429 | (cdar ,cash) | |
2430 | (car ,cash)))) | |
2431 | ||
2432 | (defmacro c-state-cache-after-top-paren (&optional cache) | |
2433 | ;; Return the position just after the latest brace/bracket/paren (whether | |
2434 | ;; left or right) recorded in CACHE (default `c-state-cache') or nil. | |
2435 | (let ((cash (or cache 'c-state-cache))) | |
2436 | `(if (consp (car ,cash)) | |
2437 | (cdar ,cash) | |
2438 | (and (car ,cash) | |
2439 | (1+ (car ,cash)))))) | |
2440 | ||
2441 | (defun c-get-cache-scan-pos (here) | |
2442 | ;; From the state-cache, determine the buffer position from which we might | |
2443 | ;; scan forward to HERE to update this cache. This position will be just | |
2444 | ;; after a paren/brace/bracket recorded in the cache, if possible, otherwise | |
2445 | ;; return the earliest position in the accessible region which isn't within | |
2446 | ;; a literal. If the visible portion of the buffer is entirely within a | |
2447 | ;; literal, return NIL. | |
2448 | (let ((c c-state-cache) elt) | |
2449 | ;(while (>= (or (c-state-cache-top-lparen c) 1) here) | |
2450 | (while (and c | |
2451 | (>= (c-state-cache-top-lparen c) here)) | |
2452 | (setq c (cdr c))) | |
2453 | ||
2454 | (setq elt (car c)) | |
2455 | (cond | |
2456 | ((consp elt) | |
2457 | (if (> (cdr elt) here) | |
2458 | (1+ (car elt)) | |
2459 | (cdr elt))) | |
2460 | (elt (1+ elt)) | |
2461 | ((<= (c-state-get-min-scan-pos) here) | |
2462 | (c-state-get-min-scan-pos)) | |
2463 | (t nil)))) | |
2464 | ||
2465 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | |
2466 | ;; Variables which keep track of preprocessor constructs. | |
8cff5feb AM |
2467 | (defvar c-state-old-cpp-beg-marker nil) |
2468 | (make-variable-buffer-local 'c-state-old-cpp-beg-marker) | |
0ec1d2c5 AM |
2469 | (defvar c-state-old-cpp-beg nil) |
2470 | (make-variable-buffer-local 'c-state-old-cpp-beg) | |
8cff5feb AM |
2471 | (defvar c-state-old-cpp-end-marker nil) |
2472 | (make-variable-buffer-local 'c-state-old-cpp-end-marker) | |
0ec1d2c5 AM |
2473 | (defvar c-state-old-cpp-end nil) |
2474 | (make-variable-buffer-local 'c-state-old-cpp-end) | |
2475 | ;; These are the limits of the macro containing point at the previous call of | |
2476 | ;; `c-parse-state', or nil. | |
2477 | ||
2478 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | |
c7015153 | 2479 | ;; Defuns which analyze the buffer, yet don't change `c-state-cache'. |
0ec1d2c5 AM |
2480 | (defun c-get-fallback-scan-pos (here) |
2481 | ;; Return a start position for building `c-state-cache' from | |
2482 | ;; scratch. This will be at the top level, 2 defuns back. | |
0386b551 AM |
2483 | (save-excursion |
2484 | ;; Go back 2 bods, but ignore any bogus positions returned by | |
2485 | ;; beginning-of-defun (i.e. open paren in column zero). | |
2486 | (goto-char here) | |
2487 | (let ((cnt 2)) | |
2488 | (while (not (or (bobp) (zerop cnt))) | |
0ec1d2c5 | 2489 | (c-beginning-of-defun-1) ; Pure elisp BOD. |
0386b551 AM |
2490 | (if (eq (char-after) ?\{) |
2491 | (setq cnt (1- cnt))))) | |
2492 | (point))) | |
2493 | ||
c0209c2c AM |
2494 | (defun c-state-balance-parens-backwards (here- here+ top) |
2495 | ;; Return the position of the opening paren/brace/bracket before HERE- which | |
2496 | ;; matches the outermost close p/b/b between HERE+ and TOP. Except when | |
2497 | ;; there's a macro, HERE- and HERE+ are the same. Like this: | |
0ec1d2c5 | 2498 | ;; |
c0209c2c AM |
2499 | ;; ............................................ |
2500 | ;; | | | |
2501 | ;; ( [ ( .........#macro.. ) ( ) ] ) | |
2502 | ;; ^ ^ ^ ^ | |
2503 | ;; | | | | | |
2504 | ;; return HERE- HERE+ TOP | |
0ec1d2c5 AM |
2505 | ;; |
2506 | ;; If there aren't enough opening paren/brace/brackets, return the position | |
c0209c2c | 2507 | ;; of the outermost one found, or HERE- if there are none. If there are no |
da6062e6 | 2508 | ;; closing p/b/bs between HERE+ and TOP, return HERE-. HERE-/+ and TOP |
c0209c2c AM |
2509 | ;; must not be inside literals. Only the accessible portion of the buffer |
2510 | ;; will be scanned. | |
2511 | ||
2512 | ;; PART 1: scan from `here+' up to `top', accumulating ")"s which enclose | |
2513 | ;; `here'. Go round the next loop each time we pass over such a ")". These | |
2514 | ;; probably match "("s before `here-'. | |
0ec1d2c5 AM |
2515 | (let (pos pa ren+1 lonely-rens) |
2516 | (save-excursion | |
2517 | (save-restriction | |
2518 | (narrow-to-region (point-min) top) ; This can move point, sometimes. | |
c0209c2c | 2519 | (setq pos here+) |
0ec1d2c5 AM |
2520 | (c-safe |
2521 | (while | |
2522 | (setq ren+1 (scan-lists pos 1 1)) ; might signal | |
2523 | (setq lonely-rens (cons ren+1 lonely-rens) | |
2524 | pos ren+1))))) | |
2525 | ||
c0209c2c | 2526 | ;; PART 2: Scan back before `here-' searching for the "("s |
0ec1d2c5 AM |
2527 | ;; matching/mismatching the ")"s found above. We only need to direct the |
2528 | ;; caller to scan when we've encountered unmatched right parens. | |
c0209c2c AM |
2529 | (setq pos here-) |
2530 | (when lonely-rens | |
2531 | (c-safe | |
2532 | (while | |
2533 | (and lonely-rens ; actual values aren't used. | |
2534 | (setq pa (scan-lists pos -1 1))) | |
2535 | (setq pos pa) | |
2536 | (setq lonely-rens (cdr lonely-rens))))) | |
2537 | pos)) | |
0ec1d2c5 AM |
2538 | |
2539 | (defun c-parse-state-get-strategy (here good-pos) | |
2540 | ;; Determine the scanning strategy for adjusting `c-parse-state', attempting | |
53964682 | 2541 | ;; to minimize the amount of scanning. HERE is the pertinent position in |
0ec1d2c5 AM |
2542 | ;; the buffer, GOOD-POS is a position where `c-state-cache' (possibly with |
2543 | ;; its head trimmed) is known to be good, or nil if there is no such | |
2544 | ;; position. | |
2545 | ;; | |
2546 | ;; The return value is a list, one of the following: | |
2547 | ;; | |
267eacb5 AM |
2548 | ;; o - ('forward START-POINT) - scan forward from START-POINT, |
2549 | ;; which is not less than the highest position in `c-state-cache' below here. | |
2550 | ;; o - ('backward nil) - scan backwards (from HERE). | |
2551 | ;; o - ('BOD START-POINT) - scan forwards from START-POINT, which is at the | |
0ec1d2c5 | 2552 | ;; top level. |
267eacb5 | 2553 | ;; o - ('IN-LIT nil) - point is inside the literal containing point-min. |
0ec1d2c5 AM |
2554 | (let ((cache-pos (c-get-cache-scan-pos here)) ; highest position below HERE in cache (or 1) |
2555 | BOD-pos ; position of 2nd BOD before HERE. | |
2556 | strategy ; 'forward, 'backward, 'BOD, or 'IN-LIT. | |
2557 | start-point | |
2558 | how-far) ; putative scanning distance. | |
2559 | (setq good-pos (or good-pos (c-state-get-min-scan-pos))) | |
2560 | (cond | |
2561 | ((< here (c-state-get-min-scan-pos)) | |
2562 | (setq strategy 'IN-LIT | |
2563 | start-point nil | |
2564 | cache-pos nil | |
2565 | how-far 0)) | |
2566 | ((<= good-pos here) | |
2567 | (setq strategy 'forward | |
2568 | start-point (max good-pos cache-pos) | |
2569 | how-far (- here start-point))) | |
2570 | ((< (- good-pos here) (- here cache-pos)) ; FIXME!!! ; apply some sort of weighting. | |
2571 | (setq strategy 'backward | |
2572 | how-far (- good-pos here))) | |
2573 | (t | |
2574 | (setq strategy 'forward | |
2575 | how-far (- here cache-pos) | |
2576 | start-point cache-pos))) | |
2577 | ||
2578 | ;; Might we be better off starting from the top level, two defuns back, | |
b03aabda AM |
2579 | ;; instead? This heuristic no longer works well in C++, where |
2580 | ;; declarations inside namespace brace blocks are frequently placed at | |
2581 | ;; column zero. | |
2582 | (when (and (not (c-major-mode-is 'c++-mode)) | |
2583 | (> how-far c-state-cache-too-far)) | |
0ec1d2c5 AM |
2584 | (setq BOD-pos (c-get-fallback-scan-pos here)) ; somewhat EXPENSIVE!!! |
2585 | (if (< (- here BOD-pos) how-far) | |
2586 | (setq strategy 'BOD | |
2587 | start-point BOD-pos))) | |
2588 | ||
2589 | (list | |
2590 | strategy | |
0ec1d2c5 AM |
2591 | (and (memq strategy '(forward BOD)) start-point)))) |
2592 | ||
2593 | ||
2594 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | |
2595 | ;; Routines which change `c-state-cache' and associated values. | |
2596 | (defun c-renarrow-state-cache () | |
2597 | ;; The region (more precisely, point-min) has changed since we | |
2598 | ;; calculated `c-state-cache'. Amend `c-state-cache' accordingly. | |
2599 | (if (< (point-min) c-state-point-min) | |
2600 | ;; If point-min has MOVED BACKWARDS then we drop the state completely. | |
2601 | ;; It would be possible to do a better job here and recalculate the top | |
2602 | ;; only. | |
2603 | (progn | |
2604 | (c-state-mark-point-min-literal) | |
2605 | (setq c-state-cache nil | |
2606 | c-state-cache-good-pos c-state-min-scan-pos | |
2607 | c-state-brace-pair-desert nil)) | |
2608 | ||
2609 | ;; point-min has MOVED FORWARD. | |
2610 | ||
2611 | ;; Is the new point-min inside a (different) literal? | |
2612 | (unless (and c-state-point-min-lit-start ; at prev. point-min | |
2613 | (< (point-min) (c-state-get-min-scan-pos))) | |
2614 | (c-state-mark-point-min-literal)) | |
2615 | ||
2616 | ;; Cut off a bit of the tail from `c-state-cache'. | |
2617 | (let ((ptr (cons nil c-state-cache)) | |
2618 | pa) | |
2619 | (while (and (setq pa (c-state-cache-top-lparen (cdr ptr))) | |
2620 | (>= pa (point-min))) | |
2621 | (setq ptr (cdr ptr))) | |
2622 | ||
2623 | (when (consp ptr) | |
2624 | (if (eq (cdr ptr) c-state-cache) | |
2625 | (setq c-state-cache nil | |
2626 | c-state-cache-good-pos c-state-min-scan-pos) | |
2627 | (setcdr ptr nil) | |
2628 | (setq c-state-cache-good-pos (1+ (c-state-cache-top-lparen)))) | |
2629 | ))) | |
2630 | ||
2631 | (setq c-state-point-min (point-min))) | |
2632 | ||
2633 | (defun c-append-lower-brace-pair-to-state-cache (from &optional upper-lim) | |
583e23bd AM |
2634 | ;; If there is a brace pair preceding FROM in the buffer, at the same level |
2635 | ;; of nesting (not necessarily immediately preceding), push a cons onto | |
2636 | ;; `c-state-cache' to represent it. FROM must not be inside a literal. If | |
2637 | ;; UPPER-LIM is non-nil, we append the highest brace pair whose "}" is below | |
2638 | ;; UPPER-LIM. | |
0ec1d2c5 AM |
2639 | ;; |
2640 | ;; Return non-nil when this has been done. | |
2641 | ;; | |
583e23bd AM |
2642 | ;; The situation it copes with is this transformation: |
2643 | ;; | |
2644 | ;; OLD: { (.) {...........} | |
2645 | ;; ^ ^ | |
2646 | ;; FROM HERE | |
2647 | ;; | |
2648 | ;; NEW: { {....} (.) {......... | |
2649 | ;; ^ ^ ^ | |
2650 | ;; LOWER BRACE PAIR HERE or HERE | |
2651 | ;; | |
0ec1d2c5 AM |
2652 | ;; This routine should be fast. Since it can get called a LOT, we maintain |
2653 | ;; `c-state-brace-pair-desert', a small cache of "failures", such that we | |
2654 | ;; reduce the time wasted in repeated fruitless searches in brace deserts. | |
2655 | (save-excursion | |
2656 | (save-restriction | |
267eacb5 AM |
2657 | (let* ((here (point-max)) |
2658 | new-cons | |
8cff5feb AM |
2659 | (cache-pos (c-state-cache-top-lparen)) ; might be nil. |
2660 | (macro-start-or-from | |
2661 | (progn (goto-char from) | |
2662 | (c-beginning-of-macro) | |
2663 | (point))) | |
2664 | (bra ; Position of "{". | |
2665 | ;; Don't start scanning in the middle of a CPP construct unless | |
2666 | ;; it contains HERE - these constructs, in Emacs, are "commented | |
2667 | ;; out" with category properties. | |
2668 | (if (eq (c-get-char-property macro-start-or-from 'category) | |
2669 | 'c-cpp-delimiter) | |
2670 | macro-start-or-from | |
2671 | from)) | |
2672 | ce) ; Position of "}" | |
0ec1d2c5 AM |
2673 | (or upper-lim (setq upper-lim from)) |
2674 | ||
2675 | ;; If we're essentially repeating a fruitless search, just give up. | |
2676 | (unless (and c-state-brace-pair-desert | |
2677 | (eq cache-pos (car c-state-brace-pair-desert)) | |
b03aabda AM |
2678 | (or (null (car c-state-brace-pair-desert)) |
2679 | (> from (car c-state-brace-pair-desert))) | |
0ec1d2c5 | 2680 | (<= from (cdr c-state-brace-pair-desert))) |
b03aabda | 2681 | ;; DESERT-LIM. Avoid repeated searching through the cached desert. |
583e23bd AM |
2682 | (let ((desert-lim |
2683 | (and c-state-brace-pair-desert | |
2684 | (eq cache-pos (car c-state-brace-pair-desert)) | |
b03aabda | 2685 | (>= from (cdr c-state-brace-pair-desert)) |
583e23bd AM |
2686 | (cdr c-state-brace-pair-desert))) |
2687 | ;; CACHE-LIM. This limit will be necessary when an opening | |
2688 | ;; paren at `cache-pos' has just had its matching close paren | |
b03aabda AM |
2689 | ;; inserted into the buffer. `cache-pos' continues to be a |
2690 | ;; search bound, even though the algorithm below would skip | |
2691 | ;; over the new paren pair. | |
583e23bd | 2692 | (cache-lim (and cache-pos (< cache-pos from) cache-pos))) |
267eacb5 | 2693 | (widen) |
583e23bd AM |
2694 | (narrow-to-region |
2695 | (cond | |
2696 | ((and desert-lim cache-lim) | |
2697 | (max desert-lim cache-lim)) | |
2698 | (desert-lim) | |
2699 | (cache-lim) | |
2700 | ((point-min))) | |
2701 | (point-max))) | |
0ec1d2c5 AM |
2702 | |
2703 | ;; In the next pair of nested loops, the inner one moves back past a | |
2704 | ;; pair of (mis-)matching parens or brackets; the outer one moves | |
2705 | ;; back over a sequence of unmatched close brace/paren/bracket each | |
2706 | ;; time round. | |
2707 | (while | |
2708 | (progn | |
2709 | (c-safe | |
2710 | (while | |
2711 | (and (setq ce (scan-lists bra -1 -1)) ; back past )/]/}; might signal | |
2712 | (setq bra (scan-lists ce -1 1)) ; back past (/[/{; might signal | |
267eacb5 AM |
2713 | (or (> bra here) ;(> ce here) |
2714 | (and | |
2715 | (< ce here) | |
2716 | (or (not (eq (char-after bra) ?\{)) | |
2717 | (and (goto-char bra) | |
2718 | (c-beginning-of-macro) | |
2719 | (< (point) macro-start-or-from)))))))) | |
0ec1d2c5 AM |
2720 | (and ce (< ce bra))) |
2721 | (setq bra ce)) ; If we just backed over an unbalanced closing | |
2722 | ; brace, ignore it. | |
2723 | ||
267eacb5 | 2724 | (if (and ce (< ce here) (< bra ce) (eq (char-after bra) ?\{)) |
0ec1d2c5 AM |
2725 | ;; We've found the desired brace-pair. |
2726 | (progn | |
2727 | (setq new-cons (cons bra (1+ ce))) | |
2728 | (cond | |
2729 | ((consp (car c-state-cache)) | |
2730 | (setcar c-state-cache new-cons)) | |
2731 | ((and (numberp (car c-state-cache)) ; probably never happens | |
2732 | (< ce (car c-state-cache))) | |
2733 | (setcdr c-state-cache | |
2734 | (cons new-cons (cdr c-state-cache)))) | |
2735 | (t (setq c-state-cache (cons new-cons c-state-cache))))) | |
2736 | ||
583e23bd | 2737 | ;; We haven't found a brace pair. Record this in the cache. |
267eacb5 AM |
2738 | (setq c-state-brace-pair-desert |
2739 | (cons (if (and ce (< bra ce) (> ce here)) ; {..} straddling HERE? | |
2740 | bra | |
2741 | (point-min)) | |
2742 | (min here from))))))))) | |
0ec1d2c5 AM |
2743 | |
2744 | (defsubst c-state-push-any-brace-pair (bra+1 macro-start-or-here) | |
2745 | ;; If BRA+1 is nil, do nothing. Otherwise, BRA+1 is the buffer position | |
2746 | ;; following a {, and that brace has a (mis-)matching } (or ]), and we | |
2747 | ;; "push" "a" brace pair onto `c-state-cache'. | |
2748 | ;; | |
2749 | ;; Here "push" means overwrite the top element if it's itself a brace-pair, | |
2750 | ;; otherwise push it normally. | |
2751 | ;; | |
2752 | ;; The brace pair we push is normally the one surrounding BRA+1, but if the | |
2753 | ;; latter is inside a macro, not being a macro containing | |
2754 | ;; MACRO-START-OR-HERE, we scan backwards through the buffer for a non-macro | |
2755 | ;; base pair. This latter case is assumed to be rare. | |
2756 | ;; | |
2757 | ;; Note: POINT is not preserved in this routine. | |
2758 | (if bra+1 | |
2759 | (if (or (> bra+1 macro-start-or-here) | |
2760 | (progn (goto-char bra+1) | |
2761 | (not (c-beginning-of-macro)))) | |
2762 | (setq c-state-cache | |
2763 | (cons (cons (1- bra+1) | |
2764 | (scan-lists bra+1 1 1)) | |
2765 | (if (consp (car c-state-cache)) | |
2766 | (cdr c-state-cache) | |
2767 | c-state-cache))) | |
2768 | ;; N.B. This defsubst codes one method for the simple, normal case, | |
2769 | ;; and a more sophisticated, slower way for the general case. Don't | |
e1dbe924 | 2770 | ;; eliminate this defsubst - it's a speed optimization. |
0ec1d2c5 AM |
2771 | (c-append-lower-brace-pair-to-state-cache (1- bra+1))))) |
2772 | ||
2773 | (defun c-append-to-state-cache (from) | |
2774 | ;; Scan the buffer from FROM to (point-max), adding elements into | |
2775 | ;; `c-state-cache' for braces etc. Return a candidate for | |
2776 | ;; `c-state-cache-good-pos'. | |
2777 | ;; | |
2778 | ;; FROM must be after the latest brace/paren/bracket in `c-state-cache', if | |
2779 | ;; any. Typically, it is immediately after it. It must not be inside a | |
2780 | ;; literal. | |
2781 | (let ((here-bol (c-point 'bol (point-max))) | |
2782 | (macro-start-or-here | |
2783 | (save-excursion (goto-char (point-max)) | |
2784 | (if (c-beginning-of-macro) | |
2785 | (point) | |
2786 | (point-max)))) | |
2787 | pa+1 ; pos just after an opening PAren (or brace). | |
2788 | (ren+1 from) ; usually a pos just after an closing paREN etc. | |
2789 | ; Is actually the pos. to scan for a (/{/[ from, | |
2790 | ; which sometimes is after a silly )/}/]. | |
2791 | paren+1 ; Pos after some opening or closing paren. | |
2792 | paren+1s ; A list of `paren+1's; used to determine a | |
2793 | ; good-pos. | |
2794 | bra+1 ce+1 ; just after L/R bra-ces. | |
2795 | bra+1s ; list of OLD values of bra+1. | |
2796 | mstart) ; start of a macro. | |
2797 | ||
2798 | (save-excursion | |
22bcf204 | 2799 | ;; Each time round the following loop, we enter a successively deeper |
0ec1d2c5 AM |
2800 | ;; level of brace/paren nesting. (Except sometimes we "continue at |
2801 | ;; the existing level".) `pa+1' is a pos inside an opening | |
2802 | ;; brace/paren/bracket, usually just after it. | |
2803 | (while | |
2804 | (progn | |
2805 | ;; Each time round the next loop moves forward over an opening then | |
2806 | ;; a closing brace/bracket/paren. This loop is white hot, so it | |
2807 | ;; plays ugly tricks to go fast. DON'T PUT ANYTHING INTO THIS | |
2808 | ;; LOOP WHICH ISN'T ABSOLUTELY NECESSARY!!! It terminates when a | |
2809 | ;; call of `scan-lists' signals an error, which happens when there | |
2810 | ;; are no more b/b/p's to scan. | |
2811 | (c-safe | |
2812 | (while t | |
2813 | (setq pa+1 (scan-lists ren+1 1 -1) ; Into (/{/[; might signal | |
2814 | paren+1s (cons pa+1 paren+1s)) | |
2815 | (setq ren+1 (scan-lists pa+1 1 1)) ; Out of )/}/]; might signal | |
2816 | (if (and (eq (char-before pa+1) ?{)) ; Check for a macro later. | |
2817 | (setq bra+1 pa+1)) | |
2818 | (setcar paren+1s ren+1))) | |
2819 | ||
2820 | (if (and pa+1 (> pa+1 ren+1)) | |
2821 | ;; We've just entered a deeper nesting level. | |
2822 | (progn | |
2823 | ;; Insert the brace pair (if present) and the single open | |
2824 | ;; paren/brace/bracket into `c-state-cache' It cannot be | |
2825 | ;; inside a macro, except one around point, because of what | |
2826 | ;; `c-neutralize-syntax-in-CPP' has done. | |
2827 | (c-state-push-any-brace-pair bra+1 macro-start-or-here) | |
2828 | ;; Insert the opening brace/bracket/paren position. | |
2829 | (setq c-state-cache (cons (1- pa+1) c-state-cache)) | |
2830 | ;; Clear admin stuff for the next more nested part of the scan. | |
2831 | (setq ren+1 pa+1 pa+1 nil bra+1 nil bra+1s nil) | |
2832 | t) ; Carry on the loop | |
2833 | ||
2834 | ;; All open p/b/b's at this nesting level, if any, have probably | |
2835 | ;; been closed by matching/mismatching ones. We're probably | |
2836 | ;; finished - we just need to check for having found an | |
2837 | ;; unmatched )/}/], which we ignore. Such a )/}/] can't be in a | |
2838 | ;; macro, due the action of `c-neutralize-syntax-in-CPP'. | |
2839 | (c-safe (setq ren+1 (scan-lists ren+1 1 1)))))) ; acts as loop control. | |
2840 | ||
2841 | ;; Record the final, innermost, brace-pair if there is one. | |
2842 | (c-state-push-any-brace-pair bra+1 macro-start-or-here) | |
2843 | ||
2844 | ;; Determine a good pos | |
2845 | (while (and (setq paren+1 (car paren+1s)) | |
2846 | (> (if (> paren+1 macro-start-or-here) | |
2847 | paren+1 | |
2848 | (goto-char paren+1) | |
2849 | (setq mstart (and (c-beginning-of-macro) | |
2850 | (point))) | |
2851 | (or mstart paren+1)) | |
2852 | here-bol)) | |
2853 | (setq paren+1s (cdr paren+1s))) | |
2854 | (cond | |
2855 | ((and paren+1 mstart) | |
2856 | (min paren+1 mstart)) | |
2857 | (paren+1) | |
2858 | (t from))))) | |
2859 | ||
267eacb5 | 2860 | (defun c-remove-stale-state-cache (start-point pps-point) |
0ec1d2c5 AM |
2861 | ;; Remove stale entries from the `c-cache-state', i.e. those which will |
2862 | ;; not be in it when it is amended for position (point-max). | |
2863 | ;; Additionally, the "outermost" open-brace entry before (point-max) | |
2864 | ;; will be converted to a cons if the matching close-brace is scanned. | |
2865 | ;; | |
267eacb5 AM |
2866 | ;; START-POINT is a "maximal" "safe position" - there must be no open |
2867 | ;; parens/braces/brackets between START-POINT and (point-max). | |
0ec1d2c5 AM |
2868 | ;; |
2869 | ;; As a second thing, calculate the result of parse-partial-sexp at | |
267eacb5 | 2870 | ;; PPS-POINT, w.r.t. START-POINT. The motivation here is that |
0ec1d2c5 | 2871 | ;; `c-state-cache-good-pos' may become PPS-POINT, but the caller may need to |
267eacb5 | 2872 | ;; adjust it to get outside a string/comment. (Sorry about this! The code |
0ec1d2c5 AM |
2873 | ;; needs to be FAST). |
2874 | ;; | |
2875 | ;; Return a list (GOOD-POS SCAN-BACK-POS PPS-STATE), where | |
2876 | ;; o - GOOD-POS is a position where the new value `c-state-cache' is known | |
2877 | ;; to be good (we aim for this to be as high as possible); | |
2878 | ;; o - SCAN-BACK-POS, if not nil, indicates there may be a brace pair | |
2879 | ;; preceding POS which needs to be recorded in `c-state-cache'. It is a | |
267eacb5 AM |
2880 | ;; position to scan backwards from. It is the position of the "{" of the |
2881 | ;; last element to be removed from `c-state-cache', when that elt is a | |
2882 | ;; cons, otherwise nil. | |
0ec1d2c5 AM |
2883 | ;; o - PPS-STATE is the parse-partial-sexp state at PPS-POINT. |
2884 | (save-restriction | |
2885 | (narrow-to-region 1 (point-max)) | |
2886 | (save-excursion | |
eb2f6eeb | 2887 | (let* ((in-macro-start ; start of macro containing (point-max) or nil. |
0ec1d2c5 AM |
2888 | (save-excursion |
2889 | (goto-char (point-max)) | |
2890 | (and (c-beginning-of-macro) | |
2891 | (point)))) | |
267eacb5 AM |
2892 | (start-point-actual-macro-start ; Start of macro containing |
2893 | ; start-point or nil | |
2894 | (and (< start-point (point-max)) | |
0ec1d2c5 | 2895 | (save-excursion |
267eacb5 | 2896 | (goto-char start-point) |
0ec1d2c5 AM |
2897 | (and (c-beginning-of-macro) |
2898 | (point))))) | |
267eacb5 | 2899 | (start-point-actual-macro-end ; End of this macro, (maybe |
0ec1d2c5 | 2900 | ; (point-max)), or nil. |
267eacb5 | 2901 | (and start-point-actual-macro-start |
0ec1d2c5 | 2902 | (save-excursion |
267eacb5 | 2903 | (goto-char start-point-actual-macro-start) |
0ec1d2c5 AM |
2904 | (c-end-of-macro) |
2905 | (point)))) | |
267eacb5 | 2906 | pps-state ; Will be 9 or 10 elements long. |
0ec1d2c5 AM |
2907 | pos |
2908 | upper-lim ; ,beyond which `c-state-cache' entries are removed | |
2909 | scan-back-pos | |
2910 | pair-beg pps-point-state target-depth) | |
2911 | ||
2912 | ;; Remove entries beyond (point-max). Also remove any entries inside | |
2913 | ;; a macro, unless (point-max) is in the same macro. | |
2914 | (setq upper-lim | |
2915 | (if (or (null c-state-old-cpp-beg) | |
2916 | (and (> (point-max) c-state-old-cpp-beg) | |
2917 | (< (point-max) c-state-old-cpp-end))) | |
2918 | (point-max) | |
2919 | (min (point-max) c-state-old-cpp-beg))) | |
657071fc | 2920 | (while (and c-state-cache (>= (c-state-cache-top-lparen) upper-lim)) |
8cff5feb | 2921 | (setq scan-back-pos (car-safe (car c-state-cache))) |
0ec1d2c5 | 2922 | (setq c-state-cache (cdr c-state-cache))) |
8cff5feb | 2923 | |
0ec1d2c5 AM |
2924 | ;; If `upper-lim' is inside the last recorded brace pair, remove its |
2925 | ;; RBrace and indicate we'll need to search backwards for a previous | |
2926 | ;; brace pair. | |
2927 | (when (and c-state-cache | |
2928 | (consp (car c-state-cache)) | |
2929 | (> (cdar c-state-cache) upper-lim)) | |
2930 | (setcar c-state-cache (caar c-state-cache)) | |
2931 | (setq scan-back-pos (car c-state-cache))) | |
2932 | ||
2933 | ;; The next loop jumps forward out of a nested level of parens each | |
2934 | ;; time round; the corresponding elements in `c-state-cache' are | |
2935 | ;; removed. `pos' is just after the brace-pair or the open paren at | |
267eacb5 AM |
2936 | ;; (car c-state-cache). There can be no open parens/braces/brackets |
2937 | ;; between `start-point'/`start-point-actual-macro-start' and (point-max), | |
0ec1d2c5 | 2938 | ;; due to the interface spec to this function. |
267eacb5 AM |
2939 | (setq pos (if (and start-point-actual-macro-end |
2940 | (not (eq start-point-actual-macro-start | |
eb2f6eeb | 2941 | in-macro-start))) |
267eacb5 | 2942 | (1+ start-point-actual-macro-end) ; get outside the macro as |
0ec1d2c5 | 2943 | ; marked by a `category' text property. |
267eacb5 | 2944 | start-point)) |
0ec1d2c5 AM |
2945 | (goto-char pos) |
2946 | (while (and c-state-cache | |
2947 | (< (point) (point-max))) | |
2948 | (cond | |
2949 | ((null pps-state) ; first time through | |
2950 | (setq target-depth -1)) | |
2951 | ((eq (car pps-state) target-depth) ; found closing ),},] | |
2952 | (setq target-depth (1- (car pps-state)))) | |
2953 | ;; Do nothing when we've merely reached pps-point. | |
2954 | ) | |
2955 | ||
2956 | ;; Scan! | |
2957 | (setq pps-state | |
2958 | (parse-partial-sexp | |
2959 | (point) (if (< (point) pps-point) pps-point (point-max)) | |
2960 | target-depth | |
2961 | nil pps-state)) | |
2962 | ||
2963 | (if (= (point) pps-point) | |
2964 | (setq pps-point-state pps-state)) | |
2965 | ||
2966 | (when (eq (car pps-state) target-depth) | |
2967 | (setq pos (point)) ; POS is now just after an R-paren/brace. | |
2968 | (cond | |
2969 | ((and (consp (car c-state-cache)) | |
2970 | (eq (point) (cdar c-state-cache))) | |
2971 | ;; We've just moved out of the paren pair containing the brace-pair | |
2972 | ;; at (car c-state-cache). `pair-beg' is where the open paren is, | |
2973 | ;; and is potentially where the open brace of a cons in | |
2974 | ;; c-state-cache will be. | |
2975 | (setq pair-beg (car-safe (cdr c-state-cache)) | |
2976 | c-state-cache (cdr-safe (cdr c-state-cache)))) ; remove {}pair + containing Lparen. | |
2977 | ((numberp (car c-state-cache)) | |
2978 | (setq pair-beg (car c-state-cache) | |
2979 | c-state-cache (cdr c-state-cache))) ; remove this | |
2980 | ; containing Lparen | |
2981 | ((numberp (cadr c-state-cache)) | |
2982 | (setq pair-beg (cadr c-state-cache) | |
2983 | c-state-cache (cddr c-state-cache))) ; Remove a paren pair | |
2984 | ; together with enclosed brace pair. | |
2985 | ;; (t nil) ; Ignore an unmated Rparen. | |
2986 | ))) | |
2987 | ||
2988 | (if (< (point) pps-point) | |
2989 | (setq pps-state (parse-partial-sexp (point) pps-point | |
2990 | nil nil ; TARGETDEPTH, STOPBEFORE | |
2991 | pps-state))) | |
2992 | ||
2993 | ;; If the last paren pair we moved out of was actually a brace pair, | |
2994 | ;; insert it into `c-state-cache'. | |
2995 | (when (and pair-beg (eq (char-after pair-beg) ?{)) | |
2996 | (if (consp (car-safe c-state-cache)) | |
2997 | (setq c-state-cache (cdr c-state-cache))) | |
2998 | (setq c-state-cache (cons (cons pair-beg pos) | |
2999 | c-state-cache))) | |
3000 | ||
3001 | (list pos scan-back-pos pps-state))))) | |
3002 | ||
267eacb5 | 3003 | (defun c-remove-stale-state-cache-backwards (here) |
0ec1d2c5 AM |
3004 | ;; Strip stale elements of `c-state-cache' by moving backwards through the |
3005 | ;; buffer, and inform the caller of the scenario detected. | |
3006 | ;; | |
3007 | ;; HERE is the position we're setting `c-state-cache' for. | |
267eacb5 AM |
3008 | ;; CACHE-POS (a locally bound variable) is just after the latest recorded |
3009 | ;; position in `c-state-cache' before HERE, or a position at or near | |
3010 | ;; point-min which isn't in a literal. | |
0ec1d2c5 AM |
3011 | ;; |
3012 | ;; This function must only be called only when (> `c-state-cache-good-pos' | |
3013 | ;; HERE). Usually the gap between CACHE-POS and HERE is large. It is thus | |
e1dbe924 | 3014 | ;; optimized to eliminate (or minimize) scanning between these two |
0ec1d2c5 AM |
3015 | ;; positions. |
3016 | ;; | |
3017 | ;; Return a three element list (GOOD-POS SCAN-BACK-POS FWD-FLAG), where: | |
3018 | ;; o - GOOD-POS is a "good position", where `c-state-cache' is valid, or | |
3019 | ;; could become so after missing elements are inserted into | |
3020 | ;; `c-state-cache'. This is JUST AFTER an opening or closing | |
3021 | ;; brace/paren/bracket which is already in `c-state-cache' or just before | |
3022 | ;; one otherwise. exceptionally (when there's no such b/p/b handy) the BOL | |
3023 | ;; before `here''s line, or the start of the literal containing it. | |
3024 | ;; o - SCAN-BACK-POS, if non-nil, indicates there may be a brace pair | |
3025 | ;; preceding POS which isn't recorded in `c-state-cache'. It is a position | |
3026 | ;; to scan backwards from. | |
3027 | ;; o - FWD-FLAG, if non-nil, indicates there may be parens/braces between | |
3028 | ;; POS and HERE which aren't recorded in `c-state-cache'. | |
3029 | ;; | |
3030 | ;; The comments in this defun use "paren" to mean parenthesis or square | |
3031 | ;; bracket (as contrasted with a brace), and "(" and ")" likewise. | |
3032 | ;; | |
267eacb5 AM |
3033 | ;; . {..} (..) (..) ( .. { } ) (...) ( .... . ..) |
3034 | ;; | | | | | | | |
3035 | ;; CP E here D C good | |
3036 | (let ((cache-pos (c-get-cache-scan-pos here)) ; highest position below HERE in cache (or 1) | |
3037 | (pos c-state-cache-good-pos) | |
0ec1d2c5 AM |
3038 | pa ren ; positions of "(" and ")" |
3039 | dropped-cons ; whether the last element dropped from `c-state-cache' | |
3040 | ; was a cons (representing a brace-pair) | |
3041 | good-pos ; see above. | |
3042 | lit ; (START . END) of a literal containing some point. | |
3043 | here-lit-start here-lit-end ; bounds of literal containing `here' | |
3044 | ; or `here' itself. | |
c0209c2c | 3045 | here- here+ ; start/end of macro around HERE, or HERE |
0ec1d2c5 | 3046 | (here-bol (c-point 'bol here)) |
c400c4d7 | 3047 | (too-far-back (max (- here c-state-cache-too-far) (point-min)))) |
0ec1d2c5 AM |
3048 | |
3049 | ;; Remove completely irrelevant entries from `c-state-cache'. | |
3050 | (while (and c-state-cache | |
3051 | (>= (setq pa (c-state-cache-top-lparen)) here)) | |
3052 | (setq dropped-cons (consp (car c-state-cache))) | |
3053 | (setq c-state-cache (cdr c-state-cache)) | |
3054 | (setq pos pa)) | |
3055 | ;; At this stage, (> pos here); | |
3056 | ;; (< (c-state-cache-top-lparen) here) (or is nil). | |
3057 | ||
c0209c2c AM |
3058 | (cond |
3059 | ((and (consp (car c-state-cache)) | |
3060 | (> (cdar c-state-cache) here)) | |
3061 | ;; CASE 1: The top of the cache is a brace pair which now encloses | |
3062 | ;; `here'. As good-pos, return the address. of the "{". Since we've no | |
3063 | ;; knowledge of what's inside these braces, we have no alternative but | |
3064 | ;; to direct the caller to scan the buffer from the opening brace. | |
3065 | (setq pos (caar c-state-cache)) | |
3066 | (setcar c-state-cache pos) | |
3067 | (list (1+ pos) pos t)) ; return value. We've just converted a brace pair | |
3068 | ; entry into a { entry, so the caller needs to | |
3069 | ; search for a brace pair before the {. | |
3070 | ||
3071 | ;; `here' might be inside a literal. Check for this. | |
3072 | ((progn | |
3073 | (setq lit (c-state-literal-at here) | |
3074 | here-lit-start (or (car lit) here) | |
3075 | here-lit-end (or (cdr lit) here)) | |
3076 | ;; Has `here' just "newly entered" a macro? | |
3077 | (save-excursion | |
3078 | (goto-char here-lit-start) | |
3079 | (if (and (c-beginning-of-macro) | |
3080 | (or (null c-state-old-cpp-beg) | |
3081 | (not (= (point) c-state-old-cpp-beg)))) | |
3082 | (progn | |
3083 | (setq here- (point)) | |
3084 | (c-end-of-macro) | |
3085 | (setq here+ (point))) | |
3086 | (setq here- here-lit-start | |
3087 | here+ here-lit-end))) | |
3088 | ||
3089 | ;; `here' might be nested inside any depth of parens (or brackets but | |
3090 | ;; not braces). Scan backwards to find the outermost such opening | |
3091 | ;; paren, if there is one. This will be the scan position to return. | |
3092 | (save-restriction | |
3093 | (narrow-to-region cache-pos (point-max)) | |
3094 | (setq pos (c-state-balance-parens-backwards here- here+ pos))) | |
3095 | nil)) ; for the cond | |
3096 | ||
3097 | ((< pos here-lit-start) | |
3098 | ;; CASE 2: Address of outermost ( or [ which now encloses `here', but | |
3099 | ;; didn't enclose the (previous) `c-state-cache-good-pos'. If there is | |
3100 | ;; a brace pair preceding this, it will already be in `c-state-cache', | |
3101 | ;; unless there was a brace pair after it, i.e. there'll only be one to | |
3102 | ;; scan for if we've just deleted one. | |
3103 | (list pos (and dropped-cons pos) t)) ; Return value. | |
3104 | ||
3105 | ;; `here' isn't enclosed in a (previously unrecorded) bracket/paren. | |
3106 | ;; Further forward scanning isn't needed, but we still need to find a | |
3107 | ;; GOOD-POS. Step out of all enclosing "("s on HERE's line. | |
3108 | ((progn | |
0ec1d2c5 AM |
3109 | (save-restriction |
3110 | (narrow-to-region here-bol (point-max)) | |
3111 | (setq pos here-lit-start) | |
3112 | (c-safe (while (setq pa (scan-lists pos -1 1)) | |
3113 | (setq pos pa)))) ; might signal | |
c0209c2c AM |
3114 | nil)) ; for the cond |
3115 | ||
3116 | ((setq ren (c-safe-scan-lists pos -1 -1 too-far-back)) | |
3117 | ;; CASE 3: After a }/)/] before `here''s BOL. | |
3118 | (list (1+ ren) (and dropped-cons pos) nil)) ; Return value | |
3119 | ||
3120 | (t | |
3121 | ;; CASE 4; Best of a bad job: BOL before `here-bol', or beginning of | |
3122 | ;; literal containing it. | |
3123 | (setq good-pos (c-state-lit-beg (c-point 'bopl here-bol))) | |
3124 | (list good-pos (and dropped-cons good-pos) nil))))) | |
0ec1d2c5 AM |
3125 | |
3126 | ||
3127 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | |
3128 | ;; Externally visible routines. | |
3129 | ||
3130 | (defun c-state-cache-init () | |
3131 | (setq c-state-cache nil | |
3132 | c-state-cache-good-pos 1 | |
3133 | c-state-nonlit-pos-cache nil | |
3134 | c-state-nonlit-pos-cache-limit 1 | |
8ed43f15 AM |
3135 | c-state-semi-nonlit-pos-cache nil |
3136 | c-state-semi-nonlit-pos-cache-limit 1 | |
0ec1d2c5 AM |
3137 | c-state-brace-pair-desert nil |
3138 | c-state-point-min 1 | |
3139 | c-state-point-min-lit-type nil | |
3140 | c-state-point-min-lit-start nil | |
3141 | c-state-min-scan-pos 1 | |
3142 | c-state-old-cpp-beg nil | |
3143 | c-state-old-cpp-end nil) | |
3144 | (c-state-mark-point-min-literal)) | |
3145 | ||
e84efb70 AM |
3146 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
3147 | ;; Debugging routines to dump `c-state-cache' in a "replayable" form. | |
3148 | ;; (defmacro c-sc-de (elt) ; "c-state-cache-dump-element" | |
3149 | ;; `(format ,(concat "(setq " (symbol-name elt) " %s) ") ,elt)) | |
3150 | ;; (defmacro c-sc-qde (elt) ; "c-state-cache-quote-dump-element" | |
3151 | ;; `(format ,(concat "(setq " (symbol-name elt) " '%s) ") ,elt)) | |
3152 | ;; (defun c-state-dump () | |
3153 | ;; ;; For debugging. | |
3154 | ;; ;(message | |
3155 | ;; (concat | |
3156 | ;; (c-sc-qde c-state-cache) | |
3157 | ;; (c-sc-de c-state-cache-good-pos) | |
3158 | ;; (c-sc-qde c-state-nonlit-pos-cache) | |
3159 | ;; (c-sc-de c-state-nonlit-pos-cache-limit) | |
3160 | ;; (c-sc-qde c-state-brace-pair-desert) | |
3161 | ;; (c-sc-de c-state-point-min) | |
3162 | ;; (c-sc-de c-state-point-min-lit-type) | |
3163 | ;; (c-sc-de c-state-point-min-lit-start) | |
3164 | ;; (c-sc-de c-state-min-scan-pos) | |
3165 | ;; (c-sc-de c-state-old-cpp-beg) | |
3166 | ;; (c-sc-de c-state-old-cpp-end))) | |
3167 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | |
3168 | ||
0ec1d2c5 AM |
3169 | (defun c-invalidate-state-cache-1 (here) |
3170 | ;; Invalidate all info on `c-state-cache' that applies to the buffer at HERE | |
3171 | ;; or higher and set `c-state-cache-good-pos' accordingly. The cache is | |
3172 | ;; left in a consistent state. | |
3173 | ;; | |
3174 | ;; This is much like `c-whack-state-after', but it never changes a paren | |
3175 | ;; pair element into an open paren element. Doing that would mean that the | |
3176 | ;; new open paren wouldn't have the required preceding paren pair element. | |
785eecbb | 3177 | ;; |
0ec1d2c5 AM |
3178 | ;; This function is called from c-after-change. |
3179 | ||
56d093a9 | 3180 | ;; The caches of non-literals: |
0ec1d2c5 AM |
3181 | (if (< here c-state-nonlit-pos-cache-limit) |
3182 | (setq c-state-nonlit-pos-cache-limit here)) | |
56d093a9 AM |
3183 | (if (< here c-state-semi-nonlit-pos-cache-limit) |
3184 | (setq c-state-semi-nonlit-pos-cache-limit here)) | |
0ec1d2c5 AM |
3185 | |
3186 | ;; `c-state-cache': | |
3187 | ;; Case 1: if `here' is in a literal containing point-min, everything | |
3188 | ;; becomes (or is already) nil. | |
3189 | (if (or (null c-state-cache-good-pos) | |
3190 | (< here (c-state-get-min-scan-pos))) | |
3191 | (setq c-state-cache nil | |
3192 | c-state-cache-good-pos nil | |
3193 | c-state-min-scan-pos nil) | |
3194 | ||
c400c4d7 SM |
3195 | ;; Truncate `c-state-cache' and set `c-state-cache-good-pos' to a value |
3196 | ;; below `here'. To maintain its consistency, we may need to insert a new | |
3197 | ;; brace pair. | |
0ec1d2c5 AM |
3198 | (let ((here-bol (c-point 'bol here)) |
3199 | too-high-pa ; recorded {/(/[ next above here, or nil. | |
3200 | dropped-cons ; was the last removed element a brace pair? | |
3201 | pa) | |
3202 | ;; The easy bit - knock over-the-top bits off `c-state-cache'. | |
3203 | (while (and c-state-cache | |
3204 | (>= (setq pa (c-state-cache-top-paren)) here)) | |
3205 | (setq dropped-cons (consp (car c-state-cache)) | |
3206 | too-high-pa (c-state-cache-top-lparen) | |
3207 | c-state-cache (cdr c-state-cache))) | |
3208 | ||
3209 | ;; Do we need to add in an earlier brace pair, having lopped one off? | |
3210 | (if (and dropped-cons | |
3211 | (< too-high-pa (+ here c-state-cache-too-far))) | |
3212 | (c-append-lower-brace-pair-to-state-cache too-high-pa here-bol)) | |
3213 | (setq c-state-cache-good-pos (or (c-state-cache-after-top-paren) | |
3214 | (c-state-get-min-scan-pos))))) | |
3215 | ||
3216 | ;; The brace-pair desert marker: | |
3217 | (when (car c-state-brace-pair-desert) | |
3218 | (if (< here (car c-state-brace-pair-desert)) | |
3219 | (setq c-state-brace-pair-desert nil) | |
3220 | (if (< here (cdr c-state-brace-pair-desert)) | |
3221 | (setcdr c-state-brace-pair-desert here))))) | |
3222 | ||
3223 | (defun c-parse-state-1 () | |
3224 | ;; Find and record all noteworthy parens between some good point earlier in | |
3225 | ;; the file and point. That good point is at least the beginning of the | |
3226 | ;; top-level construct we are in, or the beginning of the preceding | |
3227 | ;; top-level construct if we aren't in one. | |
3228 | ;; | |
3229 | ;; The returned value is a list of the noteworthy parens with the last one | |
3230 | ;; first. If an element in the list is an integer, it's the position of an | |
3231 | ;; open paren (of any type) which has not been closed before the point. If | |
3232 | ;; an element is a cons, it gives the position of a closed BRACE paren | |
3233 | ;; pair[*]; the car is the start brace position and the cdr is the position | |
3234 | ;; following the closing brace. Only the last closed brace paren pair | |
3235 | ;; before each open paren and before the point is recorded, and thus the | |
3236 | ;; state never contains two cons elements in succession. When a close brace | |
3237 | ;; has no matching open brace (e.g., the matching brace is outside the | |
3238 | ;; visible region), it is not represented in the returned value. | |
3239 | ;; | |
3240 | ;; [*] N.B. The close "brace" might be a mismatching close bracket or paren. | |
3241 | ;; This defun explicitly treats mismatching parens/braces/brackets as | |
3242 | ;; matching. It is the open brace which makes it a "brace" pair. | |
3243 | ;; | |
3244 | ;; If POINT is within a macro, open parens and brace pairs within | |
3245 | ;; THIS macro MIGHT be recorded. This depends on whether their | |
3246 | ;; syntactic properties have been suppressed by | |
3247 | ;; `c-neutralize-syntax-in-CPP'. This might need fixing (2008-12-11). | |
d9e94c22 MS |
3248 | ;; |
3249 | ;; Currently no characters which are given paren syntax with the | |
3250 | ;; syntax-table property are recorded, i.e. angle bracket arglist | |
3251 | ;; parens are never present here. Note that this might change. | |
3252 | ;; | |
0386b551 | 3253 | ;; BUG: This function doesn't cope entirely well with unbalanced |
0ec1d2c5 AM |
3254 | ;; parens in macros. (2008-12-11: this has probably been resolved |
3255 | ;; by the function `c-neutralize-syntax-in-CPP'.) E.g. in the | |
3256 | ;; following case the brace before the macro isn't balanced with the | |
3257 | ;; one after it: | |
0386b551 AM |
3258 | ;; |
3259 | ;; { | |
3260 | ;; #define X { | |
3261 | ;; } | |
3262 | ;; | |
0ec1d2c5 AM |
3263 | ;; Note to maintainers: this function DOES get called with point |
3264 | ;; within comments and strings, so don't assume it doesn't! | |
3265 | ;; | |
0386b551 | 3266 | ;; This function might do hidden buffer changes. |
0ec1d2c5 AM |
3267 | (let* ((here (point)) |
3268 | (here-bopl (c-point 'bopl)) | |
3269 | strategy ; 'forward, 'backward etc.. | |
3270 | ;; Candidate positions to start scanning from: | |
3271 | cache-pos ; highest position below HERE already existing in | |
3272 | ; cache (or 1). | |
3273 | good-pos | |
267eacb5 AM |
3274 | start-point ; (when scanning forward) a place below HERE where there |
3275 | ; are no open parens/braces between it and HERE. | |
0ec1d2c5 AM |
3276 | bopl-state |
3277 | res | |
3278 | scan-backward-pos scan-forward-p) ; used for 'backward. | |
3279 | ;; If POINT-MIN has changed, adjust the cache | |
3280 | (unless (= (point-min) c-state-point-min) | |
3281 | (c-renarrow-state-cache)) | |
3282 | ||
3283 | ;; Strategy? | |
3284 | (setq res (c-parse-state-get-strategy here c-state-cache-good-pos) | |
3285 | strategy (car res) | |
267eacb5 | 3286 | start-point (cadr res)) |
0ec1d2c5 AM |
3287 | |
3288 | (when (eq strategy 'BOD) | |
3289 | (setq c-state-cache nil | |
3290 | c-state-cache-good-pos start-point)) | |
3291 | ||
3292 | ;; SCAN! | |
3293 | (save-restriction | |
3294 | (cond | |
3295 | ((memq strategy '(forward BOD)) | |
0386b551 | 3296 | (narrow-to-region (point-min) here) |
0ec1d2c5 AM |
3297 | (setq res (c-remove-stale-state-cache start-point here-bopl)) |
3298 | (setq cache-pos (car res) | |
3299 | scan-backward-pos (cadr res) | |
3300 | bopl-state (car (cddr res))) ; will be nil if (< here-bopl | |
3301 | ; start-point) | |
3302 | (if scan-backward-pos | |
3303 | (c-append-lower-brace-pair-to-state-cache scan-backward-pos)) | |
3304 | (setq good-pos | |
3305 | (c-append-to-state-cache cache-pos)) | |
3306 | (setq c-state-cache-good-pos | |
3307 | (if (and bopl-state | |
3308 | (< good-pos (- here c-state-cache-too-far))) | |
3309 | (c-state-cache-non-literal-place here-bopl bopl-state) | |
3310 | good-pos))) | |
3311 | ||
3312 | ((eq strategy 'backward) | |
267eacb5 | 3313 | (setq res (c-remove-stale-state-cache-backwards here) |
0ec1d2c5 AM |
3314 | good-pos (car res) |
3315 | scan-backward-pos (cadr res) | |
3316 | scan-forward-p (car (cddr res))) | |
3317 | (if scan-backward-pos | |
3318 | (c-append-lower-brace-pair-to-state-cache | |
3319 | scan-backward-pos)) | |
3320 | (setq c-state-cache-good-pos | |
3321 | (if scan-forward-p | |
3322 | (progn (narrow-to-region (point-min) here) | |
3323 | (c-append-to-state-cache good-pos)) | |
2e492df3 | 3324 | good-pos))) |
0ec1d2c5 AM |
3325 | |
3326 | (t ; (eq strategy 'IN-LIT) | |
3327 | (setq c-state-cache nil | |
3328 | c-state-cache-good-pos nil))))) | |
3329 | ||
3330 | c-state-cache) | |
3331 | ||
3332 | (defun c-invalidate-state-cache (here) | |
3333 | ;; This is a wrapper over `c-invalidate-state-cache-1'. | |
3334 | ;; | |
3335 | ;; It suppresses the syntactic effect of the < and > (template) brackets and | |
3336 | ;; of all parens in preprocessor constructs, except for any such construct | |
3337 | ;; containing point. We can then call `c-invalidate-state-cache-1' without | |
3338 | ;; worrying further about macros and template delimiters. | |
3339 | (c-with-<->-as-parens-suppressed | |
3e8f7d91 AM |
3340 | (if (and c-state-old-cpp-beg |
3341 | (< c-state-old-cpp-beg here)) | |
0ec1d2c5 | 3342 | (c-with-all-but-one-cpps-commented-out |
3e8f7d91 AM |
3343 | c-state-old-cpp-beg |
3344 | (min c-state-old-cpp-end here) | |
0ec1d2c5 AM |
3345 | (c-invalidate-state-cache-1 here)) |
3346 | (c-with-cpps-commented-out | |
3347 | (c-invalidate-state-cache-1 here))))) | |
3348 | ||
8cff5feb AM |
3349 | (defmacro c-state-maybe-marker (place marker) |
3350 | ;; If PLACE is non-nil, return a marker marking it, otherwise nil. | |
3351 | ;; We (re)use MARKER. | |
3352 | `(and ,place | |
3353 | (or ,marker (setq ,marker (make-marker))) | |
3354 | (set-marker ,marker ,place))) | |
3355 | ||
0ec1d2c5 AM |
3356 | (defun c-parse-state () |
3357 | ;; This is a wrapper over `c-parse-state-1'. See that function for a | |
3358 | ;; description of the functionality and return value. | |
3359 | ;; | |
3360 | ;; It suppresses the syntactic effect of the < and > (template) brackets and | |
3361 | ;; of all parens in preprocessor constructs, except for any such construct | |
3362 | ;; containing point. We can then call `c-parse-state-1' without worrying | |
3363 | ;; further about macros and template delimiters. | |
3364 | (let (here-cpp-beg here-cpp-end) | |
3365 | (save-excursion | |
3366 | (when (c-beginning-of-macro) | |
3367 | (setq here-cpp-beg (point)) | |
3368 | (unless | |
3369 | (> (setq here-cpp-end (c-syntactic-end-of-macro)) | |
3370 | here-cpp-beg) | |
3371 | (setq here-cpp-beg nil here-cpp-end nil)))) | |
3372 | ;; FIXME!!! Put in a `condition-case' here to protect the integrity of the | |
3373 | ;; subsystem. | |
3374 | (prog1 | |
3375 | (c-with-<->-as-parens-suppressed | |
3376 | (if (and here-cpp-beg (> here-cpp-end here-cpp-beg)) | |
3377 | (c-with-all-but-one-cpps-commented-out | |
3378 | here-cpp-beg here-cpp-end | |
3379 | (c-parse-state-1)) | |
3380 | (c-with-cpps-commented-out | |
3381 | (c-parse-state-1)))) | |
8cff5feb AM |
3382 | (setq c-state-old-cpp-beg |
3383 | (c-state-maybe-marker here-cpp-beg c-state-old-cpp-beg-marker) | |
3384 | c-state-old-cpp-end | |
3385 | (c-state-maybe-marker here-cpp-end c-state-old-cpp-end-marker))))) | |
0ec1d2c5 AM |
3386 | |
3387 | ;; Debug tool to catch cache inconsistencies. This is called from | |
3388 | ;; 000tests.el. | |
a66cd3ee MS |
3389 | (defvar c-debug-parse-state nil) |
3390 | (unless (fboundp 'c-real-parse-state) | |
3391 | (fset 'c-real-parse-state (symbol-function 'c-parse-state))) | |
3392 | (cc-bytecomp-defun c-real-parse-state) | |
cd5495ff | 3393 | |
b03aabda | 3394 | (defvar c-parse-state-point nil) |
cd5495ff AM |
3395 | (defvar c-parse-state-state nil) |
3396 | (defun c-record-parse-state-state () | |
b03aabda | 3397 | (setq c-parse-state-point (point)) |
cd5495ff AM |
3398 | (setq c-parse-state-state |
3399 | (mapcar | |
3400 | (lambda (arg) | |
b03aabda AM |
3401 | (let ((val (symbol-value arg))) |
3402 | (cons arg | |
3403 | (if (consp val) | |
3404 | (copy-tree val) | |
3405 | val)))) | |
cd5495ff AM |
3406 | '(c-state-cache |
3407 | c-state-cache-good-pos | |
3408 | c-state-nonlit-pos-cache | |
3409 | c-state-nonlit-pos-cache-limit | |
8ed43f15 AM |
3410 | c-state-semi-nonlit-pos-cache |
3411 | c-state-semi-nonlit-pos-cache-limit | |
cd5495ff AM |
3412 | c-state-brace-pair-desert |
3413 | c-state-point-min | |
3414 | c-state-point-min-lit-type | |
3415 | c-state-point-min-lit-start | |
3416 | c-state-min-scan-pos | |
3417 | c-state-old-cpp-beg | |
b03aabda AM |
3418 | c-state-old-cpp-end |
3419 | c-parse-state-point)))) | |
cd5495ff AM |
3420 | (defun c-replay-parse-state-state () |
3421 | (message | |
3422 | (concat "(setq " | |
3423 | (mapconcat | |
3424 | (lambda (arg) | |
3425 | (format "%s %s%s" (car arg) (if (atom (cdr arg)) "" "'") (cdr arg))) | |
3426 | c-parse-state-state " ") | |
3427 | ")"))) | |
3428 | ||
b03aabda AM |
3429 | (defun c-debug-parse-state-double-cons (state) |
3430 | (let (state-car conses-not-ok) | |
3431 | (while state | |
3432 | (setq state-car (car state) | |
3433 | state (cdr state)) | |
3434 | (if (and (consp state-car) | |
3435 | (consp (car state))) | |
3436 | (setq conses-not-ok t))) | |
3437 | conses-not-ok)) | |
3438 | ||
a66cd3ee | 3439 | (defun c-debug-parse-state () |
0ec1d2c5 | 3440 | (let ((here (point)) (res1 (c-real-parse-state)) res2) |
0386b551 | 3441 | (let ((c-state-cache nil) |
0ec1d2c5 AM |
3442 | (c-state-cache-good-pos 1) |
3443 | (c-state-nonlit-pos-cache nil) | |
3444 | (c-state-nonlit-pos-cache-limit 1) | |
3445 | (c-state-brace-pair-desert nil) | |
3446 | (c-state-point-min 1) | |
3447 | (c-state-point-min-lit-type nil) | |
3448 | (c-state-point-min-lit-start nil) | |
3449 | (c-state-min-scan-pos 1) | |
3450 | (c-state-old-cpp-beg nil) | |
3451 | (c-state-old-cpp-end nil)) | |
a66cd3ee MS |
3452 | (setq res2 (c-real-parse-state))) |
3453 | (unless (equal res1 res2) | |
0386b551 AM |
3454 | ;; The cache can actually go further back due to the ad-hoc way |
3455 | ;; the first paren is found, so try to whack off a bit of its | |
3456 | ;; start before complaining. | |
cd5495ff AM |
3457 | ;; (save-excursion |
3458 | ;; (goto-char (or (c-least-enclosing-brace res2) (point))) | |
3459 | ;; (c-beginning-of-defun-1) | |
3460 | ;; (while (not (or (bobp) (eq (char-after) ?{))) | |
3461 | ;; (c-beginning-of-defun-1)) | |
3462 | ;; (unless (equal (c-whack-state-before (point) res1) res2) | |
3463 | ;; (message (concat "c-parse-state inconsistency at %s: " | |
3464 | ;; "using cache: %s, from scratch: %s") | |
3465 | ;; here res1 res2))) | |
3466 | (message (concat "c-parse-state inconsistency at %s: " | |
3467 | "using cache: %s, from scratch: %s") | |
3468 | here res1 res2) | |
3469 | (message "Old state:") | |
3470 | (c-replay-parse-state-state)) | |
b03aabda AM |
3471 | |
3472 | (when (c-debug-parse-state-double-cons res1) | |
3473 | (message "c-parse-state INVALIDITY at %s: %s" | |
3474 | here res1) | |
3475 | (message "Old state:") | |
3476 | (c-replay-parse-state-state)) | |
3477 | ||
cd5495ff | 3478 | (c-record-parse-state-state) |
b03aabda AM |
3479 | res2 ; res1 correct a cascading series of errors ASAP |
3480 | )) | |
0ec1d2c5 | 3481 | |
a66cd3ee MS |
3482 | (defun c-toggle-parse-state-debug (&optional arg) |
3483 | (interactive "P") | |
3484 | (setq c-debug-parse-state (c-calculate-state arg c-debug-parse-state)) | |
3485 | (fset 'c-parse-state (symbol-function (if c-debug-parse-state | |
3486 | 'c-debug-parse-state | |
3487 | 'c-real-parse-state))) | |
b03aabda AM |
3488 | (c-keep-region-active) |
3489 | (message "c-debug-parse-state %sabled" | |
3490 | (if c-debug-parse-state "en" "dis"))) | |
0386b551 AM |
3491 | (when c-debug-parse-state |
3492 | (c-toggle-parse-state-debug 1)) | |
a66cd3ee | 3493 | |
0ec1d2c5 | 3494 | \f |
d9e94c22 MS |
3495 | (defun c-whack-state-before (bufpos paren-state) |
3496 | ;; Whack off any state information from PAREN-STATE which lies | |
3497 | ;; before BUFPOS. Not destructive on PAREN-STATE. | |
d9e94c22 MS |
3498 | (let* ((newstate (list nil)) |
3499 | (ptr newstate) | |
3500 | car) | |
3501 | (while paren-state | |
3502 | (setq car (car paren-state) | |
3503 | paren-state (cdr paren-state)) | |
3504 | (if (< (if (consp car) (car car) car) bufpos) | |
3505 | (setq paren-state nil) | |
3506 | (setcdr ptr (list car)) | |
3507 | (setq ptr (cdr ptr)))) | |
3508 | (cdr newstate))) | |
3509 | ||
3510 | (defun c-whack-state-after (bufpos paren-state) | |
3511 | ;; Whack off any state information from PAREN-STATE which lies at or | |
3512 | ;; after BUFPOS. Not destructive on PAREN-STATE. | |
d9e94c22 MS |
3513 | (catch 'done |
3514 | (while paren-state | |
3515 | (let ((car (car paren-state))) | |
3516 | (if (consp car) | |
3517 | ;; just check the car, because in a balanced brace | |
3518 | ;; expression, it must be impossible for the corresponding | |
3519 | ;; close brace to be before point, but the open brace to | |
3520 | ;; be after. | |
3521 | (if (<= bufpos (car car)) | |
3522 | nil ; whack it off | |
3523 | (if (< bufpos (cdr car)) | |
3524 | ;; its possible that the open brace is before | |
3525 | ;; bufpos, but the close brace is after. In that | |
3526 | ;; case, convert this to a non-cons element. The | |
3527 | ;; rest of the state is before bufpos, so we're | |
3528 | ;; done. | |
3529 | (throw 'done (cons (car car) (cdr paren-state))) | |
3530 | ;; we know that both the open and close braces are | |
3531 | ;; before bufpos, so we also know that everything else | |
3532 | ;; on state is before bufpos. | |
3533 | (throw 'done paren-state))) | |
3534 | (if (<= bufpos car) | |
3535 | nil ; whack it off | |
3536 | ;; it's before bufpos, so everything else should too. | |
3537 | (throw 'done paren-state))) | |
3538 | (setq paren-state (cdr paren-state))) | |
3539 | nil))) | |
3540 | ||
3541 | (defun c-most-enclosing-brace (paren-state &optional bufpos) | |
3542 | ;; Return the bufpos of the innermost enclosing open paren before | |
0386b551 | 3543 | ;; bufpos, or nil if none was found. |
d9e94c22 MS |
3544 | (let (enclosingp) |
3545 | (or bufpos (setq bufpos 134217727)) | |
3546 | (while paren-state | |
3547 | (setq enclosingp (car paren-state) | |
3548 | paren-state (cdr paren-state)) | |
3549 | (if (or (consp enclosingp) | |
3550 | (>= enclosingp bufpos)) | |
3551 | (setq enclosingp nil) | |
d9e94c22 MS |
3552 | (setq paren-state nil))) |
3553 | enclosingp)) | |
3554 | ||
0386b551 AM |
3555 | (defun c-least-enclosing-brace (paren-state) |
3556 | ;; Return the bufpos of the outermost enclosing open paren, or nil | |
3557 | ;; if none was found. | |
d9e94c22 | 3558 | (let (pos elem) |
d9e94c22 MS |
3559 | (while paren-state |
3560 | (setq elem (car paren-state) | |
3561 | paren-state (cdr paren-state)) | |
0386b551 AM |
3562 | (if (integerp elem) |
3563 | (setq pos elem))) | |
d9e94c22 MS |
3564 | pos)) |
3565 | ||
3566 | (defun c-safe-position (bufpos paren-state) | |
0386b551 AM |
3567 | ;; Return the closest "safe" position recorded on PAREN-STATE that |
3568 | ;; is higher up than BUFPOS. Return nil if PAREN-STATE doesn't | |
3569 | ;; contain any. Return nil if BUFPOS is nil, which is useful to | |
3570 | ;; find the closest limit before a given limit that might be nil. | |
d9e94c22 | 3571 | ;; |
0386b551 AM |
3572 | ;; A "safe" position is a position at or after a recorded open |
3573 | ;; paren, or after a recorded close paren. The returned position is | |
3574 | ;; thus either the first position after a close brace, or the first | |
3575 | ;; position after an enclosing paren, or at the enclosing paren in | |
3576 | ;; case BUFPOS is immediately after it. | |
d9e94c22 MS |
3577 | (when bufpos |
3578 | (let (elem) | |
3579 | (catch 'done | |
3580 | (while paren-state | |
3581 | (setq elem (car paren-state)) | |
3582 | (if (consp elem) | |
3583 | (cond ((< (cdr elem) bufpos) | |
3584 | (throw 'done (cdr elem))) | |
3585 | ((< (car elem) bufpos) | |
3586 | ;; See below. | |
3587 | (throw 'done (min (1+ (car elem)) bufpos)))) | |
3588 | (if (< elem bufpos) | |
3589 | ;; elem is the position at and not after the opening paren, so | |
3590 | ;; we can go forward one more step unless it's equal to | |
3591 | ;; bufpos. This is useful in some cases avoid an extra paren | |
3592 | ;; level between the safe position and bufpos. | |
3593 | (throw 'done (min (1+ elem) bufpos)))) | |
3594 | (setq paren-state (cdr paren-state))))))) | |
3595 | ||
3596 | (defun c-beginning-of-syntax () | |
3597 | ;; This is used for `font-lock-beginning-of-syntax-function'. It | |
3598 | ;; goes to the closest previous point that is known to be outside | |
3599 | ;; any string literal or comment. `c-state-cache' is used if it has | |
3600 | ;; a position in the vicinity. | |
3601 | (let* ((paren-state c-state-cache) | |
3602 | elem | |
3603 | ||
3604 | (pos (catch 'done | |
3605 | ;; Note: Similar code in `c-safe-position'. The | |
3606 | ;; difference is that we accept a safe position at | |
3607 | ;; the point and don't bother to go forward past open | |
3608 | ;; parens. | |
3609 | (while paren-state | |
3610 | (setq elem (car paren-state)) | |
3611 | (if (consp elem) | |
3612 | (cond ((<= (cdr elem) (point)) | |
3613 | (throw 'done (cdr elem))) | |
3614 | ((<= (car elem) (point)) | |
3615 | (throw 'done (car elem)))) | |
3616 | (if (<= elem (point)) | |
3617 | (throw 'done elem))) | |
3618 | (setq paren-state (cdr paren-state))) | |
3619 | (point-min)))) | |
3620 | ||
3621 | (if (> pos (- (point) 4000)) | |
3622 | (goto-char pos) | |
3623 | ;; The position is far back. Try `c-beginning-of-defun-1' | |
3624 | ;; (although we can't be entirely sure it will go to a position | |
3625 | ;; outside a comment or string in current emacsen). FIXME: | |
3626 | ;; Consult `syntax-ppss' here. | |
3627 | (c-beginning-of-defun-1) | |
3628 | (if (< (point) pos) | |
3629 | (goto-char pos))))) | |
3630 | ||
3631 | \f | |
3632 | ;; Tools for scanning identifiers and other tokens. | |
3633 | ||
3634 | (defun c-on-identifier () | |
3635 | "Return non-nil if the point is on or directly after an identifier. | |
3636 | Keywords are recognized and not considered identifiers. If an | |
3637 | identifier is detected, the returned value is its starting position. | |
0386b551 AM |
3638 | If an identifier ends at the point and another begins at it \(can only |
3639 | happen in Pike) then the point for the preceding one is returned. | |
d9e94c22 | 3640 | |
0386b551 AM |
3641 | Note that this function might do hidden buffer changes. See the |
3642 | comment at the start of cc-engine.el for more info." | |
3643 | ||
3644 | ;; FIXME: Shouldn't this function handle "operator" in C++? | |
d9e94c22 MS |
3645 | |
3646 | (save-excursion | |
0386b551 AM |
3647 | (skip-syntax-backward "w_") |
3648 | ||
3649 | (or | |
3650 | ||
3651 | ;; Check for a normal (non-keyword) identifier. | |
3652 | (and (looking-at c-symbol-start) | |
3653 | (not (looking-at c-keywords-regexp)) | |
3654 | (point)) | |
3655 | ||
3656 | (when (c-major-mode-is 'pike-mode) | |
3657 | ;; Handle the `<operator> syntax in Pike. | |
3658 | (let ((pos (point))) | |
3659 | (skip-chars-backward "-!%&*+/<=>^|~[]()") | |
3660 | (and (if (< (skip-chars-backward "`") 0) | |
3661 | t | |
3662 | (goto-char pos) | |
3663 | (eq (char-after) ?\`)) | |
3664 | (looking-at c-symbol-key) | |
3665 | (>= (match-end 0) pos) | |
3666 | (point)))) | |
3667 | ||
3668 | ;; Handle the "operator +" syntax in C++. | |
3669 | (when (and c-overloadable-operators-regexp | |
3670 | (= (c-backward-token-2 0) 0)) | |
3671 | ||
3672 | (cond ((and (looking-at c-overloadable-operators-regexp) | |
51c9af45 | 3673 | (or (not c-opt-op-identifier-prefix) |
0386b551 | 3674 | (and (= (c-backward-token-2 1) 0) |
51c9af45 | 3675 | (looking-at c-opt-op-identifier-prefix)))) |
0386b551 AM |
3676 | (point)) |
3677 | ||
3678 | ((save-excursion | |
51c9af45 AM |
3679 | (and c-opt-op-identifier-prefix |
3680 | (looking-at c-opt-op-identifier-prefix) | |
0386b551 AM |
3681 | (= (c-forward-token-2 1) 0) |
3682 | (looking-at c-overloadable-operators-regexp))) | |
3683 | (point)))) | |
3684 | ||
3685 | ))) | |
d9e94c22 MS |
3686 | |
3687 | (defsubst c-simple-skip-symbol-backward () | |
3688 | ;; If the point is at the end of a symbol then skip backward to the | |
3689 | ;; beginning of it. Don't move otherwise. Return non-nil if point | |
3690 | ;; moved. | |
0386b551 AM |
3691 | ;; |
3692 | ;; This function might do hidden buffer changes. | |
d9e94c22 MS |
3693 | (or (< (skip-syntax-backward "w_") 0) |
3694 | (and (c-major-mode-is 'pike-mode) | |
3695 | ;; Handle the `<operator> syntax in Pike. | |
3696 | (let ((pos (point))) | |
2a15eb73 | 3697 | (if (and (< (skip-chars-backward "-!%&*+/<=>^|~[]()") 0) |
d9e94c22 MS |
3698 | (< (skip-chars-backward "`") 0) |
3699 | (looking-at c-symbol-key) | |
3700 | (>= (match-end 0) pos)) | |
3701 | t | |
3702 | (goto-char pos) | |
3703 | nil))))) | |
3704 | ||
0386b551 | 3705 | (defun c-beginning-of-current-token (&optional back-limit) |
d9e94c22 MS |
3706 | ;; Move to the beginning of the current token. Do not move if not |
3707 | ;; in the middle of one. BACK-LIMIT may be used to bound the | |
3708 | ;; backward search; if given it's assumed to be at the boundary | |
a85fd6da | 3709 | ;; between two tokens. Return non-nil if the point is moved, nil |
580fba94 | 3710 | ;; otherwise. |
0386b551 AM |
3711 | ;; |
3712 | ;; This function might do hidden buffer changes. | |
d9e94c22 | 3713 | (let ((start (point))) |
580fba94 AM |
3714 | (if (looking-at "\\w\\|\\s_") |
3715 | (skip-syntax-backward "w_" back-limit) | |
3716 | (when (< (skip-syntax-backward ".()" back-limit) 0) | |
3717 | (while (let ((pos (or (and (looking-at c-nonsymbol-token-regexp) | |
3718 | (match-end 0)) | |
3719 | ;; `c-nonsymbol-token-regexp' should always match | |
3720 | ;; since we've skipped backward over punctuator | |
3721 | ;; or paren syntax, but consume one char in case | |
3722 | ;; it doesn't so that we don't leave point before | |
3723 | ;; some earlier incorrect token. | |
3724 | (1+ (point))))) | |
3725 | (if (<= pos start) | |
3726 | (goto-char pos)))))) | |
3727 | (< (point) start))) | |
d9e94c22 | 3728 | |
ff959bab | 3729 | (defun c-end-of-current-token (&optional back-limit) |
d9e94c22 MS |
3730 | ;; Move to the end of the current token. Do not move if not in the |
3731 | ;; middle of one. BACK-LIMIT may be used to bound the backward | |
3732 | ;; search; if given it's assumed to be at the boundary between two | |
ff959bab | 3733 | ;; tokens. Return non-nil if the point is moved, nil otherwise. |
0386b551 AM |
3734 | ;; |
3735 | ;; This function might do hidden buffer changes. | |
d9e94c22 MS |
3736 | (let ((start (point))) |
3737 | (cond ((< (skip-syntax-backward "w_" (1- start)) 0) | |
3738 | (skip-syntax-forward "w_")) | |
3739 | ((< (skip-syntax-backward ".()" back-limit) 0) | |
3740 | (while (progn | |
3741 | (if (looking-at c-nonsymbol-token-regexp) | |
3742 | (goto-char (match-end 0)) | |
3743 | ;; `c-nonsymbol-token-regexp' should always match since | |
3744 | ;; we've skipped backward over punctuator or paren | |
3745 | ;; syntax, but move forward in case it doesn't so that | |
3746 | ;; we don't leave point earlier than we started with. | |
3747 | (forward-char)) | |
ff959bab MS |
3748 | (< (point) start))))) |
3749 | (> (point) start))) | |
d9e94c22 MS |
3750 | |
3751 | (defconst c-jump-syntax-balanced | |
3752 | (if (memq 'gen-string-delim c-emacs-features) | |
3753 | "\\w\\|\\s_\\|\\s\(\\|\\s\)\\|\\s\"\\|\\s|" | |
3754 | "\\w\\|\\s_\\|\\s\(\\|\\s\)\\|\\s\"")) | |
3755 | ||
3756 | (defconst c-jump-syntax-unbalanced | |
3757 | (if (memq 'gen-string-delim c-emacs-features) | |
3758 | "\\w\\|\\s_\\|\\s\"\\|\\s|" | |
3759 | "\\w\\|\\s_\\|\\s\"")) | |
3760 | ||
3761 | (defun c-forward-token-2 (&optional count balanced limit) | |
3762 | "Move forward by tokens. | |
3763 | A token is defined as all symbols and identifiers which aren't | |
3764 | syntactic whitespace \(note that multicharacter tokens like \"==\" are | |
3765 | treated properly). Point is always either left at the beginning of a | |
3766 | token or not moved at all. COUNT specifies the number of tokens to | |
3767 | move; a negative COUNT moves in the opposite direction. A COUNT of 0 | |
3768 | moves to the next token beginning only if not already at one. If | |
3769 | BALANCED is true, move over balanced parens, otherwise move into them. | |
3770 | Also, if BALANCED is true, never move out of an enclosing paren. | |
3771 | ||
3772 | LIMIT sets the limit for the movement and defaults to the point limit. | |
3773 | The case when LIMIT is set in the middle of a token, comment or macro | |
3774 | is handled correctly, i.e. the point won't be left there. | |
3775 | ||
3776 | Return the number of tokens left to move \(positive or negative). If | |
3777 | BALANCED is true, a move over a balanced paren counts as one. Note | |
3778 | that if COUNT is 0 and no appropriate token beginning is found, 1 will | |
3779 | be returned. Thus, a return value of 0 guarantees that point is at | |
3780 | the requested position and a return value less \(without signs) than | |
0386b551 AM |
3781 | COUNT guarantees that point is at the beginning of some token. |
3782 | ||
3783 | Note that this function might do hidden buffer changes. See the | |
3784 | comment at the start of cc-engine.el for more info." | |
d9e94c22 MS |
3785 | |
3786 | (or count (setq count 1)) | |
3787 | (if (< count 0) | |
3788 | (- (c-backward-token-2 (- count) balanced limit)) | |
3789 | ||
3790 | (let ((jump-syntax (if balanced | |
3791 | c-jump-syntax-balanced | |
3792 | c-jump-syntax-unbalanced)) | |
3793 | (last (point)) | |
3794 | (prev (point))) | |
3795 | ||
3796 | (if (zerop count) | |
3797 | ;; If count is zero we should jump if in the middle of a token. | |
3798 | (c-end-of-current-token)) | |
3799 | ||
3800 | (save-restriction | |
3801 | (if limit (narrow-to-region (point-min) limit)) | |
3802 | (if (/= (point) | |
3803 | (progn (c-forward-syntactic-ws) (point))) | |
3804 | ;; Skip whitespace. Count this as a move if we did in | |
3805 | ;; fact move. | |
3806 | (setq count (max (1- count) 0))) | |
3807 | ||
3808 | (if (eobp) | |
3809 | ;; Moved out of bounds. Make sure the returned count isn't zero. | |
3810 | (progn | |
3811 | (if (zerop count) (setq count 1)) | |
3812 | (goto-char last)) | |
3813 | ||
3814 | ;; Use `condition-case' to avoid having the limit tests | |
3815 | ;; inside the loop. | |
3816 | (condition-case nil | |
3817 | (while (and | |
3818 | (> count 0) | |
3819 | (progn | |
3820 | (setq last (point)) | |
3821 | (cond ((looking-at jump-syntax) | |
3822 | (goto-char (scan-sexps (point) 1)) | |
3823 | t) | |
3824 | ((looking-at c-nonsymbol-token-regexp) | |
3825 | (goto-char (match-end 0)) | |
3826 | t) | |
3827 | ;; `c-nonsymbol-token-regexp' above should always | |
3828 | ;; match if there are correct tokens. Try to | |
3829 | ;; widen to see if the limit was set in the | |
3830 | ;; middle of one, else fall back to treating | |
3831 | ;; the offending thing as a one character token. | |
3832 | ((and limit | |
3833 | (save-restriction | |
3834 | (widen) | |
3835 | (looking-at c-nonsymbol-token-regexp))) | |
3836 | nil) | |
3837 | (t | |
3838 | (forward-char) | |
3839 | t)))) | |
3840 | (c-forward-syntactic-ws) | |
3841 | (setq prev last | |
3842 | count (1- count))) | |
3843 | (error (goto-char last))) | |
3844 | ||
3845 | (when (eobp) | |
3846 | (goto-char prev) | |
3847 | (setq count (1+ count))))) | |
3848 | ||
3849 | count))) | |
3850 | ||
3851 | (defun c-backward-token-2 (&optional count balanced limit) | |
3852 | "Move backward by tokens. | |
3853 | See `c-forward-token-2' for details." | |
3854 | ||
3855 | (or count (setq count 1)) | |
3856 | (if (< count 0) | |
3857 | (- (c-forward-token-2 (- count) balanced limit)) | |
3858 | ||
3859 | (or limit (setq limit (point-min))) | |
3860 | (let ((jump-syntax (if balanced | |
3861 | c-jump-syntax-balanced | |
3862 | c-jump-syntax-unbalanced)) | |
3863 | (last (point))) | |
3864 | ||
3865 | (if (zerop count) | |
3866 | ;; The count is zero so try to skip to the beginning of the | |
3867 | ;; current token. | |
3868 | (if (> (point) | |
3869 | (progn (c-beginning-of-current-token) (point))) | |
3870 | (if (< (point) limit) | |
3871 | ;; The limit is inside the same token, so return 1. | |
3872 | (setq count 1)) | |
3873 | ||
3874 | ;; We're not in the middle of a token. If there's | |
3875 | ;; whitespace after the point then we must move backward, | |
3876 | ;; so set count to 1 in that case. | |
3877 | (and (looking-at c-syntactic-ws-start) | |
3878 | ;; If we're looking at a '#' that might start a cpp | |
3879 | ;; directive then we have to do a more elaborate check. | |
3880 | (or (/= (char-after) ?#) | |
3881 | (not c-opt-cpp-prefix) | |
3882 | (save-excursion | |
3883 | (and (= (point) | |
3884 | (progn (beginning-of-line) | |
3885 | (looking-at "[ \t]*") | |
3886 | (match-end 0))) | |
3887 | (or (bobp) | |
3888 | (progn (backward-char) | |
3889 | (not (eq (char-before) ?\\))))))) | |
3890 | (setq count 1)))) | |
3891 | ||
3892 | ;; Use `condition-case' to avoid having to check for buffer | |
3893 | ;; limits in `backward-char', `scan-sexps' and `goto-char' below. | |
3894 | (condition-case nil | |
3895 | (while (and | |
3896 | (> count 0) | |
3897 | (progn | |
3898 | (c-backward-syntactic-ws) | |
3899 | (backward-char) | |
3900 | (if (looking-at jump-syntax) | |
3901 | (goto-char (scan-sexps (1+ (point)) -1)) | |
3902 | ;; This can be very inefficient if there's a long | |
3903 | ;; sequence of operator tokens without any separation. | |
3904 | ;; That doesn't happen in practice, anyway. | |
3905 | (c-beginning-of-current-token)) | |
3906 | (>= (point) limit))) | |
3907 | (setq last (point) | |
3908 | count (1- count))) | |
3909 | (error (goto-char last))) | |
3910 | ||
3911 | (if (< (point) limit) | |
3912 | (goto-char last)) | |
3913 | ||
3914 | count))) | |
3915 | ||
3916 | (defun c-forward-token-1 (&optional count balanced limit) | |
3917 | "Like `c-forward-token-2' but doesn't treat multicharacter operator | |
3918 | tokens like \"==\" as single tokens, i.e. all sequences of symbol | |
3919 | characters are jumped over character by character. This function is | |
3920 | for compatibility only; it's only a wrapper over `c-forward-token-2'." | |
3921 | (let ((c-nonsymbol-token-regexp "\\s.\\|\\s\(\\|\\s\)")) | |
3922 | (c-forward-token-2 count balanced limit))) | |
3923 | ||
3924 | (defun c-backward-token-1 (&optional count balanced limit) | |
3925 | "Like `c-backward-token-2' but doesn't treat multicharacter operator | |
3926 | tokens like \"==\" as single tokens, i.e. all sequences of symbol | |
3927 | characters are jumped over character by character. This function is | |
3928 | for compatibility only; it's only a wrapper over `c-backward-token-2'." | |
3929 | (let ((c-nonsymbol-token-regexp "\\s.\\|\\s\(\\|\\s\)")) | |
3930 | (c-backward-token-2 count balanced limit))) | |
3931 | ||
3932 | \f | |
3933 | ;; Tools for doing searches restricted to syntactically relevant text. | |
3934 | ||
3935 | (defun c-syntactic-re-search-forward (regexp &optional bound noerror | |
3936 | paren-level not-inside-token | |
3937 | lookbehind-submatch) | |
3938 | "Like `re-search-forward', but only report matches that are found | |
3939 | in syntactically significant text. I.e. matches in comments, macros | |
3940 | or string literals are ignored. The start point is assumed to be | |
3941 | outside any comment, macro or string literal, or else the content of | |
3942 | that region is taken as syntactically significant text. | |
3943 | ||
3944 | If PAREN-LEVEL is non-nil, an additional restriction is added to | |
2a15eb73 MS |
3945 | ignore matches in nested paren sexps. The search will also not go |
3946 | outside the current list sexp, which has the effect that if the point | |
3947 | should be moved to BOUND when no match is found \(i.e. NOERROR is | |
3948 | neither nil nor t), then it will be at the closing paren if the end of | |
3949 | the current list sexp is encountered first. | |
d9e94c22 MS |
3950 | |
3951 | If NOT-INSIDE-TOKEN is non-nil, matches in the middle of tokens are | |
3952 | ignored. Things like multicharacter operators and special symbols | |
3953 | \(e.g. \"`()\" in Pike) are handled but currently not floating point | |
3954 | constants. | |
3955 | ||
3956 | If LOOKBEHIND-SUBMATCH is non-nil, it's taken as a number of a | |
3957 | subexpression in REGEXP. The end of that submatch is used as the | |
3958 | position to check for syntactic significance. If LOOKBEHIND-SUBMATCH | |
3959 | isn't used or if that subexpression didn't match then the start | |
3960 | position of the whole match is used instead. The \"look behind\" | |
3961 | subexpression is never tested before the starting position, so it | |
3962 | might be a good idea to include \\=\\= as a match alternative in it. | |
3963 | ||
3964 | Optimization note: Matches might be missed if the \"look behind\" | |
2a15eb73 | 3965 | subexpression can match the end of nonwhite syntactic whitespace, |
d9e94c22 | 3966 | i.e. the end of comments or cpp directives. This since the function |
2a15eb73 MS |
3967 | skips over such things before resuming the search. It's on the other |
3968 | hand not safe to assume that the \"look behind\" subexpression never | |
3969 | matches syntactic whitespace. | |
3970 | ||
3971 | Bug: Unbalanced parens inside cpp directives are currently not handled | |
3972 | correctly \(i.e. they don't get ignored as they should) when | |
0386b551 AM |
3973 | PAREN-LEVEL is set. |
3974 | ||
3975 | Note that this function might do hidden buffer changes. See the | |
3976 | comment at the start of cc-engine.el for more info." | |
d9e94c22 MS |
3977 | |
3978 | (or bound (setq bound (point-max))) | |
3979 | (if paren-level (setq paren-level -1)) | |
3980 | ||
3981 | ;;(message "c-syntactic-re-search-forward %s %s %S" (point) bound regexp) | |
3982 | ||
3983 | (let ((start (point)) | |
2a15eb73 MS |
3984 | tmp |
3985 | ;; Start position for the last search. | |
3986 | search-pos | |
3987 | ;; The `parse-partial-sexp' state between the start position | |
3988 | ;; and the point. | |
3989 | state | |
3990 | ;; The current position after the last state update. The next | |
3991 | ;; `parse-partial-sexp' continues from here. | |
3992 | (state-pos (point)) | |
3993 | ;; The position at which to check the state and the state | |
3994 | ;; there. This is separate from `state-pos' since we might | |
3995 | ;; need to back up before doing the next search round. | |
3996 | check-pos check-state | |
3997 | ;; Last position known to end a token. | |
d9e94c22 | 3998 | (last-token-end-pos (point-min)) |
2a15eb73 MS |
3999 | ;; Set when a valid match is found. |
4000 | found) | |
d9e94c22 MS |
4001 | |
4002 | (condition-case err | |
4003 | (while | |
4004 | (and | |
2a15eb73 MS |
4005 | (progn |
4006 | (setq search-pos (point)) | |
4007 | (re-search-forward regexp bound noerror)) | |
d9e94c22 MS |
4008 | |
4009 | (progn | |
2a15eb73 MS |
4010 | (setq state (parse-partial-sexp |
4011 | state-pos (match-beginning 0) paren-level nil state) | |
4012 | state-pos (point)) | |
d9e94c22 | 4013 | (if (setq check-pos (and lookbehind-submatch |
2a15eb73 MS |
4014 | (or (not paren-level) |
4015 | (>= (car state) 0)) | |
d9e94c22 MS |
4016 | (match-end lookbehind-submatch))) |
4017 | (setq check-state (parse-partial-sexp | |
2a15eb73 MS |
4018 | state-pos check-pos paren-level nil state)) |
4019 | (setq check-pos state-pos | |
d9e94c22 MS |
4020 | check-state state)) |
4021 | ||
2a15eb73 MS |
4022 | ;; NOTE: If we got a look behind subexpression and get |
4023 | ;; an insignificant match in something that isn't | |
d9e94c22 MS |
4024 | ;; syntactic whitespace (i.e. strings or in nested |
4025 | ;; parentheses), then we can never skip more than a | |
2a15eb73 MS |
4026 | ;; single character from the match start position |
4027 | ;; (i.e. `state-pos' here) before continuing the | |
4028 | ;; search. That since the look behind subexpression | |
4029 | ;; might match the end of the insignificant region in | |
4030 | ;; the next search. | |
d9e94c22 MS |
4031 | |
4032 | (cond | |
d9e94c22 MS |
4033 | ((elt check-state 7) |
4034 | ;; Match inside a line comment. Skip to eol. Use | |
4035 | ;; `re-search-forward' instead of `skip-chars-forward' to get | |
4036 | ;; the right bound behavior. | |
4037 | (re-search-forward "[\n\r]" bound noerror)) | |
4038 | ||
4039 | ((elt check-state 4) | |
4040 | ;; Match inside a block comment. Skip to the '*/'. | |
4041 | (search-forward "*/" bound noerror)) | |
4042 | ||
4043 | ((and (not (elt check-state 5)) | |
4044 | (eq (char-before check-pos) ?/) | |
2a15eb73 | 4045 | (not (c-get-char-property (1- check-pos) 'syntax-table)) |
d9e94c22 MS |
4046 | (memq (char-after check-pos) '(?/ ?*))) |
4047 | ;; Match in the middle of the opener of a block or line | |
4048 | ;; comment. | |
4049 | (if (= (char-after check-pos) ?/) | |
4050 | (re-search-forward "[\n\r]" bound noerror) | |
4051 | (search-forward "*/" bound noerror))) | |
4052 | ||
2a15eb73 MS |
4053 | ;; The last `parse-partial-sexp' above might have |
4054 | ;; stopped short of the real check position if the end | |
4055 | ;; of the current sexp was encountered in paren-level | |
4056 | ;; mode. The checks above are always false in that | |
4057 | ;; case, and since they can do better skipping in | |
4058 | ;; lookbehind-submatch mode, we do them before | |
4059 | ;; checking the paren level. | |
4060 | ||
4061 | ((and paren-level | |
4062 | (/= (setq tmp (car check-state)) 0)) | |
4063 | ;; Check the paren level first since we're short of the | |
4064 | ;; syntactic checking position if the end of the | |
4065 | ;; current sexp was encountered by `parse-partial-sexp'. | |
4066 | (if (> tmp 0) | |
4067 | ||
4068 | ;; Inside a nested paren sexp. | |
4069 | (if lookbehind-submatch | |
4070 | ;; See the NOTE above. | |
4071 | (progn (goto-char state-pos) t) | |
4072 | ;; Skip out of the paren quickly. | |
4073 | (setq state (parse-partial-sexp state-pos bound 0 nil state) | |
4074 | state-pos (point))) | |
4075 | ||
4076 | ;; Have exited the current paren sexp. | |
4077 | (if noerror | |
4078 | (progn | |
4079 | ;; The last `parse-partial-sexp' call above | |
4080 | ;; has left us just after the closing paren | |
4081 | ;; in this case, so we can modify the bound | |
4082 | ;; to leave the point at the right position | |
4083 | ;; upon return. | |
4084 | (setq bound (1- (point))) | |
4085 | nil) | |
4086 | (signal 'search-failed (list regexp))))) | |
4087 | ||
4088 | ((setq tmp (elt check-state 3)) | |
4089 | ;; Match inside a string. | |
4090 | (if (or lookbehind-submatch | |
4091 | (not (integerp tmp))) | |
4092 | ;; See the NOTE above. | |
4093 | (progn (goto-char state-pos) t) | |
4094 | ;; Skip to the end of the string before continuing. | |
4095 | (let ((ender (make-string 1 tmp)) (continue t)) | |
4096 | (while (if (search-forward ender bound noerror) | |
4097 | (progn | |
4098 | (setq state (parse-partial-sexp | |
4099 | state-pos (point) nil nil state) | |
4100 | state-pos (point)) | |
4101 | (elt state 3)) | |
4102 | (setq continue nil))) | |
4103 | continue))) | |
d9e94c22 MS |
4104 | |
4105 | ((save-excursion | |
4106 | (save-match-data | |
4107 | (c-beginning-of-macro start))) | |
4108 | ;; Match inside a macro. Skip to the end of it. | |
4109 | (c-end-of-macro) | |
4110 | (cond ((<= (point) bound) t) | |
4111 | (noerror nil) | |
2a15eb73 | 4112 | (t (signal 'search-failed (list regexp))))) |
d9e94c22 | 4113 | |
2a15eb73 MS |
4114 | ((and not-inside-token |
4115 | (or (< check-pos last-token-end-pos) | |
4116 | (< check-pos | |
4117 | (save-excursion | |
4118 | (goto-char check-pos) | |
4119 | (save-match-data | |
4120 | (c-end-of-current-token last-token-end-pos)) | |
4121 | (setq last-token-end-pos (point)))))) | |
4122 | ;; Inside a token. | |
4123 | (if lookbehind-submatch | |
4124 | ;; See the NOTE above. | |
4125 | (goto-char state-pos) | |
4126 | (goto-char (min last-token-end-pos bound)))) | |
d9e94c22 MS |
4127 | |
4128 | (t | |
4129 | ;; A real match. | |
4130 | (setq found t) | |
2a15eb73 MS |
4131 | nil))) |
4132 | ||
4133 | ;; Should loop to search again, but take care to avoid | |
4134 | ;; looping on the same spot. | |
4135 | (or (/= search-pos (point)) | |
4136 | (if (= (point) bound) | |
4137 | (if noerror | |
4138 | nil | |
4139 | (signal 'search-failed (list regexp))) | |
4140 | (forward-char) | |
4141 | t)))) | |
d9e94c22 MS |
4142 | |
4143 | (error | |
4144 | (goto-char start) | |
4145 | (signal (car err) (cdr err)))) | |
4146 | ||
2a15eb73 | 4147 | ;;(message "c-syntactic-re-search-forward done %s" (or (match-end 0) (point))) |
d9e94c22 MS |
4148 | |
4149 | (if found | |
4150 | (progn | |
2a15eb73 MS |
4151 | (goto-char (match-end 0)) |
4152 | (match-end 0)) | |
d9e94c22 MS |
4153 | |
4154 | ;; Search failed. Set point as appropriate. | |
2a15eb73 MS |
4155 | (if (eq noerror t) |
4156 | (goto-char start) | |
4157 | (goto-char bound)) | |
d9e94c22 MS |
4158 | nil))) |
4159 | ||
47641aac GM |
4160 | (defvar safe-pos-list) ; bound in c-syntactic-skip-backward |
4161 | ||
d0fcee66 AM |
4162 | (defsubst c-ssb-lit-begin () |
4163 | ;; Return the start of the literal point is in, or nil. | |
4164 | ;; We read and write the variables `safe-pos', `safe-pos-list', `state' | |
4165 | ;; bound in the caller. | |
4166 | ||
4167 | ;; Use `parse-partial-sexp' from a safe position down to the point to check | |
4168 | ;; if it's outside comments and strings. | |
4169 | (save-excursion | |
4170 | (let ((pos (point)) safe-pos state pps-end-pos) | |
4171 | ;; Pick a safe position as close to the point as possible. | |
4172 | ;; | |
4173 | ;; FIXME: Consult `syntax-ppss' here if our cache doesn't give a good | |
4174 | ;; position. | |
47641aac | 4175 | |
d0fcee66 AM |
4176 | (while (and safe-pos-list |
4177 | (> (car safe-pos-list) (point))) | |
4178 | (setq safe-pos-list (cdr safe-pos-list))) | |
4179 | (unless (setq safe-pos (car-safe safe-pos-list)) | |
4180 | (setq safe-pos (max (or (c-safe-position | |
4181 | (point) (or c-state-cache | |
4182 | (c-parse-state))) | |
4183 | 0) | |
4184 | (point-min)) | |
4185 | safe-pos-list (list safe-pos))) | |
4186 | ||
4187 | ;; Cache positions along the way to use if we have to back up more. We | |
4188 | ;; cache every closing paren on the same level. If the paren cache is | |
4189 | ;; relevant in this region then we're typically already on the same | |
4190 | ;; level as the target position. Note that we might cache positions | |
4191 | ;; after opening parens in case safe-pos is in a nested list. That's | |
4192 | ;; both uncommon and harmless. | |
4193 | (while (progn | |
4194 | (setq state (parse-partial-sexp | |
4195 | safe-pos pos 0)) | |
4196 | (< (point) pos)) | |
4197 | (setq safe-pos (point) | |
4198 | safe-pos-list (cons safe-pos safe-pos-list))) | |
4199 | ||
4200 | ;; If the state contains the start of the containing sexp we cache that | |
4201 | ;; position too, so that parse-partial-sexp in the next run has a bigger | |
4202 | ;; chance of starting at the same level as the target position and thus | |
4203 | ;; will get more good safe positions into the list. | |
4204 | (if (elt state 1) | |
4205 | (setq safe-pos (1+ (elt state 1)) | |
4206 | safe-pos-list (cons safe-pos safe-pos-list))) | |
4207 | ||
4208 | (if (or (elt state 3) (elt state 4)) | |
4209 | ;; Inside string or comment. Continue search at the | |
4210 | ;; beginning of it. | |
4211 | (elt state 8))))) | |
4212 | ||
0386b551 | 4213 | (defun c-syntactic-skip-backward (skip-chars &optional limit paren-level) |
d9e94c22 MS |
4214 | "Like `skip-chars-backward' but only look at syntactically relevant chars, |
4215 | i.e. don't stop at positions inside syntactic whitespace or string | |
4216 | literals. Preprocessor directives are also ignored, with the exception | |
4217 | of the one that the point starts within, if any. If LIMIT is given, | |
0386b551 AM |
4218 | it's assumed to be at a syntactically relevant position. |
4219 | ||
4220 | If PAREN-LEVEL is non-nil, the function won't stop in nested paren | |
4221 | sexps, and the search will also not go outside the current paren sexp. | |
4222 | However, if LIMIT or the buffer limit is reached inside a nested paren | |
4223 | then the point will be left at the limit. | |
4224 | ||
4225 | Non-nil is returned if the point moved, nil otherwise. | |
4226 | ||
4227 | Note that this function might do hidden buffer changes. See the | |
4228 | comment at the start of cc-engine.el for more info." | |
d9e94c22 MS |
4229 | |
4230 | (let ((start (point)) | |
d0fcee66 | 4231 | state-2 |
d9e94c22 MS |
4232 | ;; A list of syntactically relevant positions in descending |
4233 | ;; order. It's used to avoid scanning repeatedly over | |
4234 | ;; potentially large regions with `parse-partial-sexp' to verify | |
d0fcee66 | 4235 | ;; each position. Used in `c-ssb-lit-begin' |
d9e94c22 MS |
4236 | safe-pos-list |
4237 | ;; The result from `c-beginning-of-macro' at the start position or the | |
4238 | ;; start position itself if it isn't within a macro. Evaluated on | |
4239 | ;; demand. | |
0386b551 AM |
4240 | start-macro-beg |
4241 | ;; The earliest position after the current one with the same paren | |
4242 | ;; level. Used only when `paren-level' is set. | |
d0fcee66 | 4243 | lit-beg |
0386b551 | 4244 | (paren-level-pos (point))) |
d9e94c22 | 4245 | |
d0fcee66 AM |
4246 | (while |
4247 | (progn | |
4248 | ;; The next loop "tries" to find the end point each time round, | |
4249 | ;; loops when it hasn't succeeded. | |
4250 | (while | |
4251 | (and | |
4252 | (< (skip-chars-backward skip-chars limit) 0) | |
4253 | ||
4254 | (let ((pos (point)) state-2 pps-end-pos) | |
4255 | ||
4256 | (cond | |
4257 | ;; Don't stop inside a literal | |
4258 | ((setq lit-beg (c-ssb-lit-begin)) | |
4259 | (goto-char lit-beg) | |
4260 | t) | |
4261 | ||
4262 | ((and paren-level | |
4263 | (save-excursion | |
4264 | (setq state-2 (parse-partial-sexp | |
4265 | pos paren-level-pos -1) | |
4266 | pps-end-pos (point)) | |
4267 | (/= (car state-2) 0))) | |
4268 | ;; Not at the right level. | |
4269 | ||
4270 | (if (and (< (car state-2) 0) | |
4271 | ;; We stop above if we go out of a paren. | |
4272 | ;; Now check whether it precedes or is | |
4273 | ;; nested in the starting sexp. | |
4274 | (save-excursion | |
4275 | (setq state-2 | |
4276 | (parse-partial-sexp | |
4277 | pps-end-pos paren-level-pos | |
4278 | nil nil state-2)) | |
4279 | (< (car state-2) 0))) | |
4280 | ||
4281 | ;; We've stopped short of the starting position | |
4282 | ;; so the hit was inside a nested list. Go up | |
4283 | ;; until we are at the right level. | |
4284 | (condition-case nil | |
4285 | (progn | |
4286 | (goto-char (scan-lists pos -1 | |
4287 | (- (car state-2)))) | |
4288 | (setq paren-level-pos (point)) | |
4289 | (if (and limit (>= limit paren-level-pos)) | |
0386b551 | 4290 | (progn |
d0fcee66 AM |
4291 | (goto-char limit) |
4292 | nil) | |
4293 | t)) | |
4294 | (error | |
4295 | (goto-char (or limit (point-min))) | |
4296 | nil)) | |
4297 | ||
4298 | ;; The hit was outside the list at the start | |
4299 | ;; position. Go to the start of the list and exit. | |
4300 | (goto-char (1+ (elt state-2 1))) | |
4301 | nil)) | |
4302 | ||
4303 | ((c-beginning-of-macro limit) | |
4304 | ;; Inside a macro. | |
4305 | (if (< (point) | |
4306 | (or start-macro-beg | |
4307 | (setq start-macro-beg | |
4308 | (save-excursion | |
4309 | (goto-char start) | |
4310 | (c-beginning-of-macro limit) | |
4311 | (point))))) | |
4312 | t | |
4313 | ||
4314 | ;; It's inside the same macro we started in so it's | |
4315 | ;; a relevant match. | |
4316 | (goto-char pos) | |
4317 | nil)))))) | |
91af3942 | 4318 | |
d0fcee66 AM |
4319 | (> (point) |
4320 | (progn | |
4321 | ;; Skip syntactic ws afterwards so that we don't stop at the | |
4322 | ;; end of a comment if `skip-chars' is something like "^/". | |
4323 | (c-backward-syntactic-ws) | |
4324 | (point))))) | |
d9e94c22 | 4325 | |
0386b551 AM |
4326 | ;; We might want to extend this with more useful return values in |
4327 | ;; the future. | |
4328 | (/= (point) start))) | |
4329 | ||
4330 | ;; The following is an alternative implementation of | |
4331 | ;; `c-syntactic-skip-backward' that uses backward movement to keep | |
4332 | ;; track of the syntactic context. It turned out to be generally | |
4333 | ;; slower than the one above which uses forward checks from earlier | |
4334 | ;; safe positions. | |
4335 | ;; | |
4336 | ;;(defconst c-ssb-stop-re | |
4337 | ;; ;; The regexp matching chars `c-syntactic-skip-backward' needs to | |
4338 | ;; ;; stop at to avoid going into comments and literals. | |
4339 | ;; (concat | |
4340 | ;; ;; Match comment end syntax and string literal syntax. Also match | |
4341 | ;; ;; '/' for block comment endings (not covered by comment end | |
4342 | ;; ;; syntax). | |
4343 | ;; "\\s>\\|/\\|\\s\"" | |
4344 | ;; (if (memq 'gen-string-delim c-emacs-features) | |
4345 | ;; "\\|\\s|" | |
4346 | ;; "") | |
4347 | ;; (if (memq 'gen-comment-delim c-emacs-features) | |
4348 | ;; "\\|\\s!" | |
4349 | ;; ""))) | |
4350 | ;; | |
4351 | ;;(defconst c-ssb-stop-paren-re | |
4352 | ;; ;; Like `c-ssb-stop-re' but also stops at paren chars. | |
4353 | ;; (concat c-ssb-stop-re "\\|\\s(\\|\\s)")) | |
4354 | ;; | |
4355 | ;;(defconst c-ssb-sexp-end-re | |
4356 | ;; ;; Regexp matching the ending syntax of a complex sexp. | |
4357 | ;; (concat c-string-limit-regexp "\\|\\s)")) | |
4358 | ;; | |
4359 | ;;(defun c-syntactic-skip-backward (skip-chars &optional limit paren-level) | |
4360 | ;; "Like `skip-chars-backward' but only look at syntactically relevant chars, | |
4361 | ;;i.e. don't stop at positions inside syntactic whitespace or string | |
4362 | ;;literals. Preprocessor directives are also ignored. However, if the | |
4363 | ;;point is within a comment, string literal or preprocessor directory to | |
4364 | ;;begin with, its contents is treated as syntactically relevant chars. | |
4365 | ;;If LIMIT is given, it limits the backward search and the point will be | |
4366 | ;;left there if no earlier position is found. | |
4367 | ;; | |
4368 | ;;If PAREN-LEVEL is non-nil, the function won't stop in nested paren | |
4369 | ;;sexps, and the search will also not go outside the current paren sexp. | |
4370 | ;;However, if LIMIT or the buffer limit is reached inside a nested paren | |
4371 | ;;then the point will be left at the limit. | |
4372 | ;; | |
4373 | ;;Non-nil is returned if the point moved, nil otherwise. | |
4374 | ;; | |
4375 | ;;Note that this function might do hidden buffer changes. See the | |
4376 | ;;comment at the start of cc-engine.el for more info." | |
4377 | ;; | |
4378 | ;; (save-restriction | |
4379 | ;; (when limit | |
4380 | ;; (narrow-to-region limit (point-max))) | |
4381 | ;; | |
4382 | ;; (let ((start (point))) | |
4383 | ;; (catch 'done | |
4384 | ;; (while (let ((last-pos (point)) | |
4385 | ;; (stop-pos (progn | |
4386 | ;; (skip-chars-backward skip-chars) | |
4387 | ;; (point)))) | |
4388 | ;; | |
4389 | ;; ;; Skip back over the same region as | |
4390 | ;; ;; `skip-chars-backward' above, but keep to | |
4391 | ;; ;; syntactically relevant positions. | |
4392 | ;; (goto-char last-pos) | |
4393 | ;; (while (and | |
4394 | ;; ;; `re-search-backward' with a single char regexp | |
4395 | ;; ;; should be fast. | |
4396 | ;; (re-search-backward | |
4397 | ;; (if paren-level c-ssb-stop-paren-re c-ssb-stop-re) | |
4398 | ;; stop-pos 'move) | |
4399 | ;; | |
4400 | ;; (progn | |
4401 | ;; (cond | |
4402 | ;; ((looking-at "\\s(") | |
4403 | ;; ;; `paren-level' is set and we've found the | |
4404 | ;; ;; start of the containing paren. | |
4405 | ;; (forward-char) | |
4406 | ;; (throw 'done t)) | |
4407 | ;; | |
4408 | ;; ((looking-at c-ssb-sexp-end-re) | |
4409 | ;; ;; We're at the end of a string literal or paren | |
4410 | ;; ;; sexp (if `paren-level' is set). | |
4411 | ;; (forward-char) | |
4412 | ;; (condition-case nil | |
4413 | ;; (c-backward-sexp) | |
4414 | ;; (error | |
4415 | ;; (goto-char limit) | |
4416 | ;; (throw 'done t)))) | |
4417 | ;; | |
4418 | ;; (t | |
4419 | ;; (forward-char) | |
4420 | ;; ;; At the end of some syntactic ws or possibly | |
4421 | ;; ;; after a plain '/' operator. | |
4422 | ;; (let ((pos (point))) | |
4423 | ;; (c-backward-syntactic-ws) | |
4424 | ;; (if (= pos (point)) | |
4425 | ;; ;; Was a plain '/' operator. Go past it. | |
4426 | ;; (backward-char))))) | |
4427 | ;; | |
4428 | ;; (> (point) stop-pos)))) | |
4429 | ;; | |
4430 | ;; ;; Now the point is either at `stop-pos' or at some | |
4431 | ;; ;; position further back if `stop-pos' was at a | |
4432 | ;; ;; syntactically irrelevant place. | |
4433 | ;; | |
4434 | ;; ;; Skip additional syntactic ws so that we don't stop | |
4435 | ;; ;; at the end of a comment if `skip-chars' is | |
4436 | ;; ;; something like "^/". | |
4437 | ;; (c-backward-syntactic-ws) | |
4438 | ;; | |
4439 | ;; (< (point) stop-pos)))) | |
4440 | ;; | |
4441 | ;; ;; We might want to extend this with more useful return values | |
4442 | ;; ;; in the future. | |
4443 | ;; (/= (point) start)))) | |
d9e94c22 MS |
4444 | |
4445 | \f | |
4446 | ;; Tools for handling comments and string literals. | |
4447 | ||
13d49cbb | 4448 | (defun c-in-literal (&optional lim detect-cpp) |
d9e94c22 MS |
4449 | "Return the type of literal point is in, if any. |
4450 | The return value is `c' if in a C-style comment, `c++' if in a C++ | |
4451 | style comment, `string' if in a string literal, `pound' if DETECT-CPP | |
4452 | is non-nil and in a preprocessor line, or nil if somewhere else. | |
4453 | Optional LIM is used as the backward limit of the search. If omitted, | |
4454 | or nil, `c-beginning-of-defun' is used. | |
4455 | ||
4456 | The last point calculated is cached if the cache is enabled, i.e. if | |
4457 | `c-in-literal-cache' is bound to a two element vector. | |
4458 | ||
0386b551 AM |
4459 | Note that this function might do hidden buffer changes. See the |
4460 | comment at the start of cc-engine.el for more info." | |
2cc769a8 AM |
4461 | (save-restriction |
4462 | (widen) | |
56d093a9 | 4463 | (let* ((safe-place (c-state-semi-safe-place (point))) |
2cc769a8 AM |
4464 | (lit (c-state-pp-to-literal safe-place (point)))) |
4465 | (or (cadr lit) | |
4466 | (and detect-cpp | |
4467 | (save-excursion (c-beginning-of-macro)) | |
4468 | 'pound))))) | |
d9e94c22 MS |
4469 | |
4470 | (defun c-literal-limits (&optional lim near not-in-delimiter) | |
4471 | "Return a cons of the beginning and end positions of the comment or | |
4472 | string surrounding point (including both delimiters), or nil if point | |
4473 | isn't in one. If LIM is non-nil, it's used as the \"safe\" position | |
4474 | to start parsing from. If NEAR is non-nil, then the limits of any | |
4475 | literal next to point is returned. \"Next to\" means there's only | |
4476 | spaces and tabs between point and the literal. The search for such a | |
4477 | literal is done first in forward direction. If NOT-IN-DELIMITER is | |
4478 | non-nil, the case when point is inside a starting delimiter won't be | |
a85fd6da | 4479 | recognized. This only has effect for comments which have starting |
d9e94c22 MS |
4480 | delimiters with more than one character. |
4481 | ||
0386b551 AM |
4482 | Note that this function might do hidden buffer changes. See the |
4483 | comment at the start of cc-engine.el for more info." | |
d9e94c22 MS |
4484 | |
4485 | (save-excursion | |
4486 | (let* ((pos (point)) | |
56d093a9 | 4487 | (lim (or lim (c-state-semi-safe-place pos))) |
2cc769a8 AM |
4488 | (pp-to-lit (save-restriction |
4489 | (widen) | |
4490 | (c-state-pp-to-literal lim pos))) | |
13d49cbb | 4491 | (state (car pp-to-lit)) |
13d49cbb | 4492 | (lit-limits (car (cddr pp-to-lit)))) |
d9e94c22 | 4493 | |
13d49cbb AM |
4494 | (cond |
4495 | (lit-limits) | |
4496 | ((and (not not-in-delimiter) | |
4497 | (not (elt state 5)) | |
4498 | (eq (char-before) ?/) | |
4499 | (looking-at "[/*]")) ; FIXME!!! use c-line/block-comment-starter. 2008-09-28. | |
4500 | ;; We're standing in a comment starter. | |
4501 | (backward-char 1) | |
4502 | (cons (point) (progn (c-forward-single-comment) (point)))) | |
4503 | ||
4504 | (near | |
4505 | (goto-char pos) | |
4506 | ;; Search forward for a literal. | |
4507 | (skip-chars-forward " \t") | |
4508 | (cond | |
4509 | ((looking-at c-string-limit-regexp) ; String. | |
4510 | (cons (point) (or (c-safe (c-forward-sexp 1) (point)) | |
4511 | (point-max)))) | |
d9e94c22 | 4512 | |
13d49cbb AM |
4513 | ((looking-at c-comment-start-regexp) ; Line or block comment. |
4514 | (cons (point) (progn (c-forward-single-comment) (point)))) | |
4515 | ||
4516 | (t | |
4517 | ;; Search backward. | |
4518 | (skip-chars-backward " \t") | |
4519 | ||
4520 | (let ((end (point)) beg) | |
4521 | (cond | |
4522 | ((save-excursion | |
4523 | (< (skip-syntax-backward c-string-syntax) 0)) ; String. | |
4524 | (setq beg (c-safe (c-backward-sexp 1) (point)))) | |
4525 | ||
4526 | ((and (c-safe (forward-char -2) t) | |
4527 | (looking-at "*/")) | |
4528 | ;; Block comment. Due to the nature of line | |
4529 | ;; comments, they will always be covered by the | |
4530 | ;; normal case above. | |
4531 | (goto-char end) | |
4532 | (c-backward-single-comment) | |
4533 | ;; If LIM is bogus, beg will be bogus. | |
4534 | (setq beg (point)))) | |
4535 | ||
4536 | (if beg (cons beg end)))))) | |
4537 | )))) | |
d9e94c22 | 4538 | |
0386b551 AM |
4539 | ;; In case external callers use this; it did have a docstring. |
4540 | (defalias 'c-literal-limits-fast 'c-literal-limits) | |
d9e94c22 | 4541 | |
0386b551 AM |
4542 | (defun c-collect-line-comments (range) |
4543 | "If the argument is a cons of two buffer positions (such as returned by | |
4544 | `c-literal-limits'), and that range contains a C++ style line comment, | |
4545 | then an extended range is returned that contains all adjacent line | |
4546 | comments (i.e. all comments that starts in the same column with no | |
4547 | empty lines or non-whitespace characters between them). Otherwise the | |
4548 | argument is returned. | |
d9e94c22 | 4549 | |
0386b551 AM |
4550 | Note that this function might do hidden buffer changes. See the |
4551 | comment at the start of cc-engine.el for more info." | |
d9e94c22 MS |
4552 | |
4553 | (save-excursion | |
0386b551 AM |
4554 | (condition-case nil |
4555 | (if (and (consp range) (progn | |
4556 | (goto-char (car range)) | |
4557 | (looking-at c-line-comment-starter))) | |
b414f371 | 4558 | (let ((col (current-column)) |
0386b551 AM |
4559 | (beg (point)) |
4560 | (bopl (c-point 'bopl)) | |
4561 | (end (cdr range))) | |
4562 | ;; Got to take care in the backward direction to handle | |
4563 | ;; comments which are preceded by code. | |
4564 | (while (and (c-backward-single-comment) | |
4565 | (>= (point) bopl) | |
4566 | (looking-at c-line-comment-starter) | |
4567 | (= col (current-column))) | |
4568 | (setq beg (point) | |
4569 | bopl (c-point 'bopl))) | |
4570 | (goto-char end) | |
4571 | (while (and (progn (skip-chars-forward " \t") | |
4572 | (looking-at c-line-comment-starter)) | |
4573 | (= col (current-column)) | |
4574 | (prog1 (zerop (forward-line 1)) | |
4575 | (setq end (point))))) | |
4576 | (cons beg end)) | |
4577 | range) | |
4578 | (error range)))) | |
d9e94c22 MS |
4579 | |
4580 | (defun c-literal-type (range) | |
4581 | "Convenience function that given the result of `c-literal-limits', | |
a85fd6da AM |
4582 | returns nil or the type of literal that the range surrounds, one |
4583 | of the symbols 'c, 'c++ or 'string. It's much faster than using | |
4584 | `c-in-literal' and is intended to be used when you need both the | |
4585 | type of a literal and its limits. | |
d9e94c22 | 4586 | |
0386b551 AM |
4587 | Note that this function might do hidden buffer changes. See the |
4588 | comment at the start of cc-engine.el for more info." | |
4589 | ||
d9e94c22 MS |
4590 | (if (consp range) |
4591 | (save-excursion | |
4592 | (goto-char (car range)) | |
4593 | (cond ((looking-at c-string-limit-regexp) 'string) | |
4594 | ((or (looking-at "//") ; c++ line comment | |
4595 | (and (looking-at "\\s<") ; comment starter | |
4596 | (looking-at "#"))) ; awk comment. | |
4597 | 'c++) | |
4598 | (t 'c))) ; Assuming the range is valid. | |
4599 | range)) | |
4600 | ||
9657183b AM |
4601 | (defsubst c-determine-limit-get-base (start try-size) |
4602 | ;; Get a "safe place" approximately TRY-SIZE characters before START. | |
4603 | ;; This doesn't preserve point. | |
4604 | (let* ((pos (max (- start try-size) (point-min))) | |
56d093a9 | 4605 | (base (c-state-semi-safe-place pos)) |
9657183b AM |
4606 | (s (parse-partial-sexp base pos))) |
4607 | (if (or (nth 4 s) (nth 3 s)) ; comment or string | |
4608 | (nth 8 s) | |
4609 | (point)))) | |
4610 | ||
4611 | (defun c-determine-limit (how-far-back &optional start try-size) | |
4612 | ;; Return a buffer position HOW-FAR-BACK non-literal characters from START | |
4613 | ;; (default point). This is done by going back further in the buffer then | |
4614 | ;; searching forward for literals. The position found won't be in a | |
4615 | ;; literal. We start searching for the sought position TRY-SIZE (default | |
4616 | ;; twice HOW-FAR-BACK) bytes back from START. This function must be fast. | |
4617 | ;; :-) | |
4618 | (save-excursion | |
4619 | (let* ((start (or start (point))) | |
4620 | (try-size (or try-size (* 2 how-far-back))) | |
4621 | (base (c-determine-limit-get-base start try-size)) | |
4622 | (pos base) | |
4623 | ||
4624 | (s (parse-partial-sexp pos pos)) ; null state. | |
4625 | stack elt size | |
4626 | (count 0)) | |
4627 | (while (< pos start) | |
4628 | ;; Move forward one literal each time round this loop. | |
4629 | ;; Move forward to the start of a comment or string. | |
4630 | (setq s (parse-partial-sexp | |
4631 | pos | |
4632 | start | |
4633 | nil ; target-depth | |
4634 | nil ; stop-before | |
4635 | s ; state | |
4636 | 'syntax-table)) ; stop-comment | |
4637 | ||
4638 | ;; Gather details of the non-literal-bit - starting pos and size. | |
4639 | (setq size (- (if (or (nth 4 s) (nth 3 s)) | |
4640 | (nth 8 s) | |
4641 | (point)) | |
4642 | pos)) | |
4643 | (if (> size 0) | |
4644 | (setq stack (cons (cons pos size) stack))) | |
4645 | ||
4646 | ;; Move forward to the end of the comment/string. | |
4647 | (if (or (nth 4 s) (nth 3 s)) | |
4648 | (setq s (parse-partial-sexp | |
4649 | (point) | |
4650 | start | |
4651 | nil ; target-depth | |
4652 | nil ; stop-before | |
4653 | s ; state | |
4654 | 'syntax-table))) ; stop-comment | |
4655 | (setq pos (point))) | |
4656 | ||
4657 | ;; Now try and find enough non-literal characters recorded on the stack. | |
4658 | ;; Go back one recorded literal each time round this loop. | |
4659 | (while (and (< count how-far-back) | |
4660 | stack) | |
4661 | (setq elt (car stack) | |
4662 | stack (cdr stack)) | |
4663 | (setq count (+ count (cdr elt)))) | |
4664 | ||
4665 | ;; Have we found enough yet? | |
4666 | (cond | |
4667 | ((>= count how-far-back) | |
4668 | (+ (car elt) (- count how-far-back))) | |
4669 | ((eq base (point-min)) | |
4670 | (point-min)) | |
4671 | (t | |
4672 | (c-determine-limit (- how-far-back count) base try-size)))))) | |
2e492df3 AM |
4673 | |
4674 | (defun c-determine-+ve-limit (how-far &optional start-pos) | |
4675 | ;; Return a buffer position about HOW-FAR non-literal characters forward | |
4676 | ;; from START-POS (default point), which must not be inside a literal. | |
4677 | (save-excursion | |
4678 | (let ((pos (or start-pos (point))) | |
4679 | (count how-far) | |
4680 | (s (parse-partial-sexp (point) (point)))) ; null state | |
4681 | (while (and (not (eobp)) | |
4682 | (> count 0)) | |
4683 | ;; Scan over counted characters. | |
4684 | (setq s (parse-partial-sexp | |
4685 | pos | |
4686 | (min (+ pos count) (point-max)) | |
4687 | nil ; target-depth | |
4688 | nil ; stop-before | |
4689 | s ; state | |
4690 | 'syntax-table)) ; stop-comment | |
4691 | (setq count (- count (- (point) pos) 1) | |
4692 | pos (point)) | |
4693 | ;; Scan over literal characters. | |
4694 | (if (nth 8 s) | |
4695 | (setq s (parse-partial-sexp | |
4696 | pos | |
4697 | (point-max) | |
4698 | nil ; target-depth | |
4699 | nil ; stop-before | |
4700 | s ; state | |
4701 | 'syntax-table) ; stop-comment | |
4702 | pos (point)))) | |
4703 | (point)))) | |
4704 | ||
d9e94c22 MS |
4705 | \f |
4706 | ;; `c-find-decl-spots' and accompanying stuff. | |
4707 | ||
4708 | ;; Variables used in `c-find-decl-spots' to cache the search done for | |
4709 | ;; the first declaration in the last call. When that function starts, | |
4710 | ;; it needs to back up over syntactic whitespace to look at the last | |
4711 | ;; token before the region being searched. That can sometimes cause | |
4712 | ;; moves back and forth over a quite large region of comments and | |
4713 | ;; macros, which would be repeated for each changed character when | |
4714 | ;; we're called during fontification, since font-lock refontifies the | |
4715 | ;; current line for each change. Thus it's worthwhile to cache the | |
4716 | ;; first match. | |
4717 | ;; | |
4718 | ;; `c-find-decl-syntactic-pos' is a syntactically relevant position in | |
4719 | ;; the syntactic whitespace less or equal to some start position. | |
4720 | ;; There's no cached value if it's nil. | |
4721 | ;; | |
4722 | ;; `c-find-decl-match-pos' is the match position if | |
4723 | ;; `c-find-decl-prefix-search' matched before the syntactic whitespace | |
4724 | ;; at `c-find-decl-syntactic-pos', or nil if there's no such match. | |
4725 | (defvar c-find-decl-syntactic-pos nil) | |
4726 | (make-variable-buffer-local 'c-find-decl-syntactic-pos) | |
4727 | (defvar c-find-decl-match-pos nil) | |
4728 | (make-variable-buffer-local 'c-find-decl-match-pos) | |
4729 | ||
4730 | (defsubst c-invalidate-find-decl-cache (change-min-pos) | |
4731 | (and c-find-decl-syntactic-pos | |
4732 | (< change-min-pos c-find-decl-syntactic-pos) | |
4733 | (setq c-find-decl-syntactic-pos nil))) | |
4734 | ||
4735 | ; (defface c-debug-decl-spot-face | |
4736 | ; '((t (:background "Turquoise"))) | |
4737 | ; "Debug face to mark the spots where `c-find-decl-spots' stopped.") | |
4738 | ; (defface c-debug-decl-sws-face | |
4739 | ; '((t (:background "Khaki"))) | |
4740 | ; "Debug face to mark the syntactic whitespace between the declaration | |
4741 | ; spots and the preceding token end.") | |
4742 | ||
4743 | (defmacro c-debug-put-decl-spot-faces (match-pos decl-pos) | |
4744 | (when (facep 'c-debug-decl-spot-face) | |
0386b551 | 4745 | `(c-save-buffer-state ((match-pos ,match-pos) (decl-pos ,decl-pos)) |
d9e94c22 MS |
4746 | (c-debug-add-face (max match-pos (point-min)) decl-pos |
4747 | 'c-debug-decl-sws-face) | |
4748 | (c-debug-add-face decl-pos (min (1+ decl-pos) (point-max)) | |
4749 | 'c-debug-decl-spot-face)))) | |
4750 | (defmacro c-debug-remove-decl-spot-faces (beg end) | |
4751 | (when (facep 'c-debug-decl-spot-face) | |
0386b551 | 4752 | `(c-save-buffer-state () |
d9e94c22 MS |
4753 | (c-debug-remove-face ,beg ,end 'c-debug-decl-spot-face) |
4754 | (c-debug-remove-face ,beg ,end 'c-debug-decl-sws-face)))) | |
4755 | ||
4756 | (defmacro c-find-decl-prefix-search () | |
4757 | ;; Macro used inside `c-find-decl-spots'. It ought to be a defun, | |
4758 | ;; but it contains lots of free variables that refer to things | |
4759 | ;; inside `c-find-decl-spots'. The point is left at `cfd-match-pos' | |
4760 | ;; if there is a match, otherwise at `cfd-limit'. | |
0386b551 AM |
4761 | ;; |
4762 | ;; This macro might do hidden buffer changes. | |
d9e94c22 MS |
4763 | |
4764 | '(progn | |
4765 | ;; Find the next property match position if we haven't got one already. | |
4766 | (unless cfd-prop-match | |
4767 | (save-excursion | |
4768 | (while (progn | |
4769 | (goto-char (next-single-property-change | |
4770 | (point) 'c-type nil cfd-limit)) | |
4771 | (and (< (point) cfd-limit) | |
4772 | (not (eq (c-get-char-property (1- (point)) 'c-type) | |
4773 | 'c-decl-end))))) | |
4774 | (setq cfd-prop-match (point)))) | |
4775 | ||
0386b551 AM |
4776 | ;; Find the next `c-decl-prefix-or-start-re' match if we haven't |
4777 | ;; got one already. | |
d9e94c22 | 4778 | (unless cfd-re-match |
0386b551 AM |
4779 | |
4780 | (if (> cfd-re-match-end (point)) | |
4781 | (goto-char cfd-re-match-end)) | |
4782 | ||
4783 | (while (if (setq cfd-re-match-end | |
4784 | (re-search-forward c-decl-prefix-or-start-re | |
4785 | cfd-limit 'move)) | |
4786 | ||
4787 | ;; Match. Check if it's inside a comment or string literal. | |
4788 | (c-got-face-at | |
4789 | (if (setq cfd-re-match (match-end 1)) | |
4790 | ;; Matched the end of a token preceding a decl spot. | |
4791 | (progn | |
4792 | (goto-char cfd-re-match) | |
4793 | (1- cfd-re-match)) | |
4794 | ;; Matched a token that start a decl spot. | |
4795 | (goto-char (match-beginning 0)) | |
4796 | (point)) | |
4797 | c-literal-faces) | |
4798 | ||
4799 | ;; No match. Finish up and exit the loop. | |
4800 | (setq cfd-re-match cfd-limit) | |
4801 | nil) | |
4802 | ||
4803 | ;; Skip out of comments and string literals. | |
d9e94c22 MS |
4804 | (while (progn |
4805 | (goto-char (next-single-property-change | |
0386b551 | 4806 | (point) 'face nil cfd-limit)) |
d9e94c22 | 4807 | (and (< (point) cfd-limit) |
0386b551 AM |
4808 | (c-got-face-at (point) c-literal-faces))))) |
4809 | ||
4810 | ;; If we matched at the decl start, we have to back up over the | |
4811 | ;; preceding syntactic ws to set `cfd-match-pos' and to catch | |
4812 | ;; any decl spots in the syntactic ws. | |
d9e94c22 | 4813 | (unless cfd-re-match |
0386b551 AM |
4814 | (c-backward-syntactic-ws) |
4815 | (setq cfd-re-match (point)))) | |
d9e94c22 MS |
4816 | |
4817 | ;; Choose whichever match is closer to the start. | |
4818 | (if (< cfd-re-match cfd-prop-match) | |
4819 | (setq cfd-match-pos cfd-re-match | |
4820 | cfd-re-match nil) | |
4821 | (setq cfd-match-pos cfd-prop-match | |
4822 | cfd-prop-match nil)) | |
4823 | ||
4824 | (goto-char cfd-match-pos) | |
4825 | ||
4826 | (when (< cfd-match-pos cfd-limit) | |
4827 | ;; Skip forward past comments only so we don't skip macros. | |
4828 | (c-forward-comments) | |
4829 | ;; Set the position to continue at. We can avoid going over | |
4830 | ;; the comments skipped above a second time, but it's possible | |
4831 | ;; that the comment skipping has taken us past `cfd-prop-match' | |
4832 | ;; since the property might be used inside comments. | |
4833 | (setq cfd-continue-pos (if cfd-prop-match | |
4834 | (min cfd-prop-match (point)) | |
4835 | (point)))))) | |
4836 | ||
4837 | (defun c-find-decl-spots (cfd-limit cfd-decl-re cfd-face-checklist cfd-fun) | |
0386b551 | 4838 | ;; Call CFD-FUN for each possible spot for a declaration, cast or |
a85fd6da AM |
4839 | ;; label from the point to CFD-LIMIT. |
4840 | ;; | |
9657183b AM |
4841 | ;; CFD-FUN is called with point at the start of the spot. It's passed two |
4842 | ;; arguments: The first is the end position of the token preceding the spot, | |
4843 | ;; or 0 for the implicit match at bob. The second is a flag that is t when | |
4844 | ;; the match is inside a macro. Point should be moved forward by at least | |
4845 | ;; one token. | |
4846 | ;; | |
4847 | ;; If CFD-FUN adds `c-decl-end' properties somewhere below the current spot, | |
4848 | ;; it should return non-nil to ensure that the next search will find them. | |
0386b551 | 4849 | ;; |
a85fd6da | 4850 | ;; Such a spot is: |
0386b551 AM |
4851 | ;; o The first token after bob. |
4852 | ;; o The first token after the end of submatch 1 in | |
4853 | ;; `c-decl-prefix-or-start-re' when that submatch matches. | |
4854 | ;; o The start of each `c-decl-prefix-or-start-re' match when | |
4855 | ;; submatch 1 doesn't match. | |
5a89f0a7 | 4856 | ;; o The first token after the end of each occurrence of the |
0386b551 AM |
4857 | ;; `c-type' text property with the value `c-decl-end', provided |
4858 | ;; `c-type-decl-end-used' is set. | |
4859 | ;; | |
4860 | ;; Only a spot that match CFD-DECL-RE and whose face is in the | |
4861 | ;; CFD-FACE-CHECKLIST list causes CFD-FUN to be called. The face | |
4862 | ;; check is disabled if CFD-FACE-CHECKLIST is nil. | |
d9e94c22 MS |
4863 | ;; |
4864 | ;; If the match is inside a macro then the buffer is narrowed to the | |
4865 | ;; end of it, so that CFD-FUN can investigate the following tokens | |
4866 | ;; without matching something that begins inside a macro and ends | |
4867 | ;; outside it. It's to avoid this work that the CFD-DECL-RE and | |
4868 | ;; CFD-FACE-CHECKLIST checks exist. | |
4869 | ;; | |
0386b551 AM |
4870 | ;; The spots are visited approximately in order from top to bottom. |
4871 | ;; It's however the positions where `c-decl-prefix-or-start-re' | |
4872 | ;; matches and where `c-decl-end' properties are found that are in | |
4873 | ;; order. Since the spots often are at the following token, they | |
4874 | ;; might be visited out of order insofar as more spots are reported | |
4875 | ;; later on within the syntactic whitespace between the match | |
4876 | ;; positions and their spots. | |
4877 | ;; | |
4878 | ;; It's assumed that comments and strings are fontified in the | |
d9e94c22 MS |
4879 | ;; searched range. |
4880 | ;; | |
4881 | ;; This is mainly used in fontification, and so has an elaborate | |
4882 | ;; cache to handle repeated calls from the same start position; see | |
4883 | ;; the variables above. | |
4884 | ;; | |
4885 | ;; All variables in this function begin with `cfd-' to avoid name | |
4886 | ;; collision with the (dynamically bound) variables used in CFD-FUN. | |
0386b551 AM |
4887 | ;; |
4888 | ;; This function might do hidden buffer changes. | |
d9e94c22 | 4889 | |
0386b551 AM |
4890 | (let ((cfd-start-pos (point)) |
4891 | (cfd-buffer-end (point-max)) | |
4892 | ;; The end of the token preceding the decl spot last found | |
4893 | ;; with `c-decl-prefix-or-start-re'. `cfd-limit' if there's | |
4894 | ;; no match. | |
d9e94c22 | 4895 | cfd-re-match |
0386b551 AM |
4896 | ;; The end position of the last `c-decl-prefix-or-start-re' |
4897 | ;; match. If this is greater than `cfd-continue-pos', the | |
4898 | ;; next regexp search is started here instead. | |
4899 | (cfd-re-match-end (point-min)) | |
4900 | ;; The end of the last `c-decl-end' found by | |
4901 | ;; `c-find-decl-prefix-search'. `cfd-limit' if there's no | |
4902 | ;; match. If searching for the property isn't needed then we | |
4903 | ;; disable it by setting it to `cfd-limit' directly. | |
d9e94c22 | 4904 | (cfd-prop-match (unless c-type-decl-end-used cfd-limit)) |
0386b551 AM |
4905 | ;; The end of the token preceding the decl spot last found by |
4906 | ;; `c-find-decl-prefix-search'. 0 for the implicit match at | |
4907 | ;; bob. `cfd-limit' if there's no match. In other words, | |
4908 | ;; this is the minimum of `cfd-re-match' and `cfd-prop-match'. | |
d9e94c22 MS |
4909 | (cfd-match-pos cfd-limit) |
4910 | ;; The position to continue searching at. | |
4911 | cfd-continue-pos | |
4912 | ;; The position of the last "real" token we've stopped at. | |
4913 | ;; This can be greater than `cfd-continue-pos' when we get | |
4914 | ;; hits inside macros or at `c-decl-end' positions inside | |
4915 | ;; comments. | |
4916 | (cfd-token-pos 0) | |
4917 | ;; The end position of the last entered macro. | |
4918 | (cfd-macro-end 0)) | |
4919 | ||
4920 | ;; Initialize by finding a syntactically relevant start position | |
0386b551 AM |
4921 | ;; before the point, and do the first `c-decl-prefix-or-start-re' |
4922 | ;; search unless we're at bob. | |
d9e94c22 | 4923 | |
0386b551 | 4924 | (let (start-in-literal start-in-macro syntactic-pos) |
d9e94c22 MS |
4925 | ;; Must back up a bit since we look for the end of the previous |
4926 | ;; statement or declaration, which is earlier than the first | |
4927 | ;; returned match. | |
4928 | ||
0386b551 AM |
4929 | (cond |
4930 | ;; First we need to move to a syntactically relevant position. | |
4931 | ;; Begin by backing out of comment or string literals. | |
4932 | ((and | |
4933 | (when (c-got-face-at (point) c-literal-faces) | |
4934 | ;; Try to use the faces to back up to the start of the | |
4935 | ;; literal. FIXME: What if the point is on a declaration | |
4936 | ;; inside a comment? | |
4937 | (while (and (not (bobp)) | |
4938 | (c-got-face-at (1- (point)) c-literal-faces)) | |
4939 | (goto-char (previous-single-property-change | |
4940 | (point) 'face nil (point-min)))) | |
4941 | ||
4942 | ;; XEmacs doesn't fontify the quotes surrounding string | |
4943 | ;; literals. | |
4944 | (and (featurep 'xemacs) | |
4945 | (eq (get-text-property (point) 'face) | |
4946 | 'font-lock-string-face) | |
4947 | (not (bobp)) | |
4948 | (progn (backward-char) | |
4949 | (not (looking-at c-string-limit-regexp))) | |
4950 | (forward-char)) | |
4951 | ||
4952 | ;; Don't trust the literal to contain only literal faces | |
4953 | ;; (the font lock package might not have fontified the | |
4954 | ;; start of it at all, for instance) so check that we have | |
4955 | ;; arrived at something that looks like a start or else | |
4956 | ;; resort to `c-literal-limits'. | |
4957 | (unless (looking-at c-literal-start-regexp) | |
4958 | (let ((range (c-literal-limits))) | |
4959 | (if range (goto-char (car range))))) | |
4960 | ||
4961 | (setq start-in-literal (point))) | |
4962 | ||
4963 | ;; The start is in a literal. If the limit is in the same | |
4964 | ;; one we don't have to find a syntactic position etc. We | |
4965 | ;; only check that if the limit is at or before bonl to save | |
4966 | ;; time; it covers the by far most common case when font-lock | |
4967 | ;; refontifies the current line only. | |
4968 | (<= cfd-limit (c-point 'bonl cfd-start-pos)) | |
4969 | (save-excursion | |
4970 | (goto-char cfd-start-pos) | |
4971 | (while (progn | |
4972 | (goto-char (next-single-property-change | |
4973 | (point) 'face nil cfd-limit)) | |
4974 | (and (< (point) cfd-limit) | |
4975 | (c-got-face-at (point) c-literal-faces)))) | |
4976 | (= (point) cfd-limit))) | |
4977 | ||
4978 | ;; Completely inside a literal. Set up variables to trig the | |
4979 | ;; (< cfd-continue-pos cfd-start-pos) case below and it'll | |
4980 | ;; find a suitable start position. | |
4981 | (setq cfd-continue-pos start-in-literal)) | |
4982 | ||
4983 | ;; Check if the region might be completely inside a macro, to | |
4984 | ;; optimize that like the completely-inside-literal above. | |
4985 | ((save-excursion | |
4986 | (and (= (forward-line 1) 0) | |
4987 | (bolp) ; forward-line has funny behavior at eob. | |
4988 | (>= (point) cfd-limit) | |
4989 | (progn (backward-char) | |
4990 | (eq (char-before) ?\\)))) | |
4991 | ;; (Maybe) completely inside a macro. Only need to trig the | |
4992 | ;; (< cfd-continue-pos cfd-start-pos) case below to make it | |
4993 | ;; set things up. | |
4994 | (setq cfd-continue-pos (1- cfd-start-pos) | |
4995 | start-in-macro t)) | |
d9e94c22 | 4996 | |
0386b551 AM |
4997 | (t |
4998 | ;; Back out of any macro so we don't miss any declaration | |
4999 | ;; that could follow after it. | |
5000 | (when (c-beginning-of-macro) | |
5001 | (setq start-in-macro t)) | |
5002 | ||
5003 | ;; Now we're at a proper syntactically relevant position so we | |
5004 | ;; can use the cache. But first clear it if it applied | |
5005 | ;; further down. | |
5006 | (c-invalidate-find-decl-cache cfd-start-pos) | |
5007 | ||
5008 | (setq syntactic-pos (point)) | |
5009 | (unless (eq syntactic-pos c-find-decl-syntactic-pos) | |
5010 | ;; Don't have to do this if the cache is relevant here, | |
5011 | ;; typically if the same line is refontified again. If | |
5012 | ;; we're just some syntactic whitespace further down we can | |
5013 | ;; still use the cache to limit the skipping. | |
5014 | (c-backward-syntactic-ws c-find-decl-syntactic-pos)) | |
5015 | ||
5016 | ;; If we hit `c-find-decl-syntactic-pos' and | |
5017 | ;; `c-find-decl-match-pos' is set then we install the cached | |
5018 | ;; values. If we hit `c-find-decl-syntactic-pos' and | |
5019 | ;; `c-find-decl-match-pos' is nil then we know there's no decl | |
5020 | ;; prefix in the whitespace before `c-find-decl-syntactic-pos' | |
5021 | ;; and so we can continue the search from this point. If we | |
5022 | ;; didn't hit `c-find-decl-syntactic-pos' then we're now in | |
5023 | ;; the right spot to begin searching anyway. | |
5024 | (if (and (eq (point) c-find-decl-syntactic-pos) | |
5025 | c-find-decl-match-pos) | |
d9e94c22 MS |
5026 | (setq cfd-match-pos c-find-decl-match-pos |
5027 | cfd-continue-pos syntactic-pos) | |
0386b551 AM |
5028 | |
5029 | (setq c-find-decl-syntactic-pos syntactic-pos) | |
5030 | ||
5031 | (when (if (bobp) | |
5032 | ;; Always consider bob a match to get the first | |
5033 | ;; declaration in the file. Do this separately instead of | |
5034 | ;; letting `c-decl-prefix-or-start-re' match bob, so that | |
5035 | ;; regexp always can consume at least one character to | |
5036 | ;; ensure that we won't get stuck in an infinite loop. | |
5037 | (setq cfd-re-match 0) | |
5038 | (backward-char) | |
5039 | (c-beginning-of-current-token) | |
5040 | (< (point) cfd-limit)) | |
5041 | ;; Do an initial search now. In the bob case above it's | |
5042 | ;; only done to search for a `c-decl-end' spot. | |
5043 | (c-find-decl-prefix-search)) | |
5044 | ||
5045 | (setq c-find-decl-match-pos (and (< cfd-match-pos cfd-start-pos) | |
5046 | cfd-match-pos))))) | |
5047 | ||
5048 | ;; Advance `cfd-continue-pos' if it's before the start position. | |
5049 | ;; The closest continue position that might have effect at or | |
5050 | ;; after the start depends on what we started in. This also | |
5051 | ;; finds a suitable start position in the special cases when the | |
5052 | ;; region is completely within a literal or macro. | |
5053 | (when (and cfd-continue-pos (< cfd-continue-pos cfd-start-pos)) | |
5054 | ||
5055 | (cond | |
5056 | (start-in-macro | |
5057 | ;; If we're in a macro then it's the closest preceding token | |
5058 | ;; in the macro. Check this before `start-in-literal', | |
5059 | ;; since if we're inside a literal in a macro, the preceding | |
5060 | ;; token is earlier than any `c-decl-end' spot inside the | |
5061 | ;; literal (comment). | |
5062 | (goto-char (or start-in-literal cfd-start-pos)) | |
5063 | ;; The only syntactic ws in macros are comments. | |
d9e94c22 | 5064 | (c-backward-comments) |
0386b551 AM |
5065 | (backward-char) |
5066 | (c-beginning-of-current-token)) | |
5067 | ||
5068 | (start-in-literal | |
5069 | ;; If we're in a comment it can only be the closest | |
5070 | ;; preceding `c-decl-end' position within that comment, if | |
5071 | ;; any. Go back to the beginning of such a property so that | |
5072 | ;; `c-find-decl-prefix-search' will find the end of it. | |
5073 | ;; (Can't stop at the end and install it directly on | |
5074 | ;; `cfd-prop-match' since that variable might be cleared | |
5075 | ;; after `cfd-fun' below.) | |
5076 | ;; | |
5077 | ;; Note that if the literal is a string then the property | |
5078 | ;; search will simply skip to the beginning of it right | |
5079 | ;; away. | |
5080 | (if (not c-type-decl-end-used) | |
5081 | (goto-char start-in-literal) | |
5082 | (goto-char cfd-start-pos) | |
5083 | (while (progn | |
5084 | (goto-char (previous-single-property-change | |
5085 | (point) 'c-type nil start-in-literal)) | |
5086 | (and (> (point) start-in-literal) | |
5087 | (not (eq (c-get-char-property (point) 'c-type) | |
5088 | 'c-decl-end)))))) | |
5089 | ||
5090 | (when (= (point) start-in-literal) | |
5091 | ;; Didn't find any property inside the comment, so we can | |
5092 | ;; skip it entirely. (This won't skip past a string, but | |
5093 | ;; that'll be handled quickly by the next | |
5094 | ;; `c-find-decl-prefix-search' anyway.) | |
5095 | (c-forward-single-comment) | |
5096 | (if (> (point) cfd-limit) | |
5097 | (goto-char cfd-limit)))) | |
d9e94c22 | 5098 | |
0386b551 AM |
5099 | (t |
5100 | ;; If we started in normal code, the only match that might | |
5101 | ;; apply before the start is what we already got in | |
5102 | ;; `cfd-match-pos' so we can continue at the start position. | |
5103 | ;; (Note that we don't get here if the first match is below | |
5104 | ;; it.) | |
5105 | (goto-char cfd-start-pos))) | |
5106 | ||
5107 | ;; Delete found matches if they are before our new continue | |
5108 | ;; position, so that `c-find-decl-prefix-search' won't back up | |
5109 | ;; to them later on. | |
5110 | (setq cfd-continue-pos (point)) | |
5111 | (when (and cfd-re-match (< cfd-re-match cfd-continue-pos)) | |
5112 | (setq cfd-re-match nil)) | |
5113 | (when (and cfd-prop-match (< cfd-prop-match cfd-continue-pos)) | |
5114 | (setq cfd-prop-match nil))) | |
5115 | ||
5116 | (if syntactic-pos | |
5117 | ;; This is the normal case and we got a proper syntactic | |
5118 | ;; position. If there's a match then it's always outside | |
5119 | ;; macros and comments, so advance to the next token and set | |
5120 | ;; `cfd-token-pos'. The loop below will later go back using | |
5121 | ;; `cfd-continue-pos' to fix declarations inside the | |
5122 | ;; syntactic ws. | |
5123 | (when (and cfd-match-pos (< cfd-match-pos syntactic-pos)) | |
5124 | (goto-char syntactic-pos) | |
5125 | (c-forward-syntactic-ws) | |
5126 | (and cfd-continue-pos | |
5127 | (< cfd-continue-pos (point)) | |
5128 | (setq cfd-token-pos (point)))) | |
5129 | ||
5130 | ;; Have one of the special cases when the region is completely | |
5131 | ;; within a literal or macro. `cfd-continue-pos' is set to a | |
5132 | ;; good start position for the search, so do it. | |
5133 | (c-find-decl-prefix-search))) | |
d9e94c22 | 5134 | |
51c9af45 | 5135 | ;; Now loop. Round what? (ACM, 2006/7/5). We already got the first match. |
d9e94c22 MS |
5136 | |
5137 | (while (progn | |
5138 | (while (and | |
5139 | (< cfd-match-pos cfd-limit) | |
5140 | ||
5141 | (or | |
5142 | ;; Kludge to filter out matches on the "<" that | |
5143 | ;; aren't open parens, for the sake of languages | |
5144 | ;; that got `c-recognize-<>-arglists' set. | |
5145 | (and (eq (char-before cfd-match-pos) ?<) | |
5146 | (not (c-get-char-property (1- cfd-match-pos) | |
5147 | 'syntax-table))) | |
5148 | ||
5149 | ;; If `cfd-continue-pos' is less or equal to | |
5150 | ;; `cfd-token-pos', we've got a hit inside a macro | |
5151 | ;; that's in the syntactic whitespace before the last | |
5152 | ;; "real" declaration we've checked. If they're equal | |
5153 | ;; we've arrived at the declaration a second time, so | |
5154 | ;; there's nothing to do. | |
5155 | (= cfd-continue-pos cfd-token-pos) | |
5156 | ||
5157 | (progn | |
5158 | ;; If `cfd-continue-pos' is less than `cfd-token-pos' | |
5159 | ;; we're still searching for declarations embedded in | |
5160 | ;; the syntactic whitespace. In that case we need | |
5161 | ;; only to skip comments and not macros, since they | |
5162 | ;; can't be nested, and that's already been done in | |
5163 | ;; `c-find-decl-prefix-search'. | |
5164 | (when (> cfd-continue-pos cfd-token-pos) | |
5165 | (c-forward-syntactic-ws) | |
5166 | (setq cfd-token-pos (point))) | |
5167 | ||
5168 | ;; Continue if the following token fails the | |
5169 | ;; CFD-DECL-RE and CFD-FACE-CHECKLIST checks. | |
5170 | (when (or (>= (point) cfd-limit) | |
5171 | (not (looking-at cfd-decl-re)) | |
5172 | (and cfd-face-checklist | |
5173 | (not (c-got-face-at | |
5174 | (point) cfd-face-checklist)))) | |
5175 | (goto-char cfd-continue-pos) | |
5176 | t))) | |
5177 | ||
5178 | (< (point) cfd-limit)) | |
5179 | (c-find-decl-prefix-search)) | |
5180 | ||
5181 | (< (point) cfd-limit)) | |
5182 | ||
0386b551 AM |
5183 | (when (and |
5184 | (>= (point) cfd-start-pos) | |
d9e94c22 | 5185 | |
0386b551 AM |
5186 | (progn |
5187 | ;; Narrow to the end of the macro if we got a hit inside | |
5188 | ;; one, to avoid recognizing things that start inside the | |
5189 | ;; macro and end outside it. | |
5190 | (when (> cfd-match-pos cfd-macro-end) | |
5191 | ;; Not in the same macro as in the previous round. | |
5192 | (save-excursion | |
5193 | (goto-char cfd-match-pos) | |
5194 | (setq cfd-macro-end | |
5195 | (if (save-excursion (and (c-beginning-of-macro) | |
5196 | (< (point) cfd-match-pos))) | |
5197 | (progn (c-end-of-macro) | |
5198 | (point)) | |
5199 | 0)))) | |
5200 | ||
5201 | (if (zerop cfd-macro-end) | |
5202 | t | |
5203 | (if (> cfd-macro-end (point)) | |
5204 | (progn (narrow-to-region (point-min) cfd-macro-end) | |
5205 | t) | |
5206 | ;; The matched token was the last thing in the macro, | |
5207 | ;; so the whole match is bogus. | |
5208 | (setq cfd-macro-end 0) | |
5209 | nil)))) | |
d9e94c22 MS |
5210 | |
5211 | (c-debug-put-decl-spot-faces cfd-match-pos (point)) | |
0386b551 AM |
5212 | (if (funcall cfd-fun cfd-match-pos (/= cfd-macro-end 0)) |
5213 | (setq cfd-prop-match nil)) | |
d9e94c22 MS |
5214 | |
5215 | (when (/= cfd-macro-end 0) | |
e1dbe924 | 5216 | ;; Restore limits if we did macro narrowing above. |
d9e94c22 MS |
5217 | (narrow-to-region (point-min) cfd-buffer-end))) |
5218 | ||
5219 | (goto-char cfd-continue-pos) | |
5220 | (if (= cfd-continue-pos cfd-limit) | |
5221 | (setq cfd-match-pos cfd-limit) | |
9657183b AM |
5222 | (c-find-decl-prefix-search))))) ; Moves point, sets cfd-continue-pos, |
5223 | ; cfd-match-pos, etc. | |
d9e94c22 MS |
5224 | |
5225 | \f | |
5226 | ;; A cache for found types. | |
5227 | ||
5228 | ;; Buffer local variable that contains an obarray with the types we've | |
5229 | ;; found. If a declaration is recognized somewhere we record the | |
5230 | ;; fully qualified identifier in it to recognize it as a type | |
5231 | ;; elsewhere in the file too. This is not accurate since we do not | |
5232 | ;; bother with the scoping rules of the languages, but in practice the | |
5233 | ;; same name is seldom used as both a type and something else in a | |
5234 | ;; file, and we only use this as a last resort in ambiguous cases (see | |
0386b551 AM |
5235 | ;; `c-forward-decl-or-cast-1'). |
5236 | ;; | |
580fba94 AM |
5237 | ;; Not every type need be in this cache. However, things which have |
5238 | ;; ceased to be types must be removed from it. | |
5239 | ;; | |
0386b551 AM |
5240 | ;; Template types in C++ are added here too but with the template |
5241 | ;; arglist replaced with "<>" in references or "<" for the one in the | |
5242 | ;; primary type. E.g. the type "Foo<A,B>::Bar<C>" is stored as | |
5243 | ;; "Foo<>::Bar<". This avoids storing very long strings (since C++ | |
5244 | ;; template specs can be fairly sized programs in themselves) and | |
5245 | ;; improves the hit ratio (it's a type regardless of the template | |
5246 | ;; args; it's just not the same type, but we're only interested in | |
5247 | ;; recognizing types, not telling distinct types apart). Note that | |
5248 | ;; template types in references are added here too; from the example | |
5249 | ;; above there will also be an entry "Foo<". | |
d9e94c22 MS |
5250 | (defvar c-found-types nil) |
5251 | (make-variable-buffer-local 'c-found-types) | |
5252 | ||
5253 | (defsubst c-clear-found-types () | |
5254 | ;; Clears `c-found-types'. | |
d9e94c22 MS |
5255 | (setq c-found-types (make-vector 53 0))) |
5256 | ||
5257 | (defun c-add-type (from to) | |
5258 | ;; Add the given region as a type in `c-found-types'. If the region | |
5259 | ;; doesn't match an existing type but there is a type which is equal | |
5260 | ;; to the given one except that the last character is missing, then | |
5261 | ;; the shorter type is removed. That's done to avoid adding all | |
5262 | ;; prefixes of a type as it's being entered and font locked. This | |
5263 | ;; doesn't cover cases like when characters are removed from a type | |
5264 | ;; or added in the middle. We'd need the position of point when the | |
5265 | ;; font locking is invoked to solve this well. | |
0386b551 AM |
5266 | ;; |
5267 | ;; This function might do hidden buffer changes. | |
5268 | (let ((type (c-syntactic-content from to c-recognize-<>-arglists))) | |
5269 | (unless (intern-soft type c-found-types) | |
5270 | (unintern (substring type 0 -1) c-found-types) | |
5271 | (intern type c-found-types)))) | |
d9e94c22 | 5272 | |
580fba94 AM |
5273 | (defun c-unfind-type (name) |
5274 | ;; Remove the "NAME" from c-found-types, if present. | |
5275 | (unintern name c-found-types)) | |
5276 | ||
d9e94c22 MS |
5277 | (defsubst c-check-type (from to) |
5278 | ;; Return non-nil if the given region contains a type in | |
5279 | ;; `c-found-types'. | |
0386b551 AM |
5280 | ;; |
5281 | ;; This function might do hidden buffer changes. | |
5282 | (intern-soft (c-syntactic-content from to c-recognize-<>-arglists) | |
5283 | c-found-types)) | |
d9e94c22 MS |
5284 | |
5285 | (defun c-list-found-types () | |
5286 | ;; Return all the types in `c-found-types' as a sorted list of | |
5287 | ;; strings. | |
5288 | (let (type-list) | |
5289 | (mapatoms (lambda (type) | |
5290 | (setq type-list (cons (symbol-name type) | |
5291 | type-list))) | |
5292 | c-found-types) | |
5293 | (sort type-list 'string-lessp))) | |
a66cd3ee | 5294 | |
2f42c75f DN |
5295 | ;; Shut up the byte compiler. |
5296 | (defvar c-maybe-stale-found-type) | |
5297 | ||
580fba94 AM |
5298 | (defun c-trim-found-types (beg end old-len) |
5299 | ;; An after change function which, in conjunction with the info in | |
5300 | ;; c-maybe-stale-found-type (set in c-before-change), removes a type | |
5301 | ;; from `c-found-types', should this type have become stale. For | |
5302 | ;; example, this happens to "foo" when "foo \n bar();" becomes | |
5303 | ;; "foo(); \n bar();". Such stale types, if not removed, foul up | |
5304 | ;; the fontification. | |
b414f371 | 5305 | ;; |
580fba94 AM |
5306 | ;; Have we, perhaps, added non-ws characters to the front/back of a found |
5307 | ;; type? | |
5308 | (when (> end beg) | |
5309 | (save-excursion | |
5310 | (when (< end (point-max)) | |
5311 | (goto-char end) | |
5312 | (if (and (c-beginning-of-current-token) ; only moves when we started in the middle | |
5313 | (progn (goto-char end) | |
5314 | (c-end-of-current-token))) | |
5315 | (c-unfind-type (buffer-substring-no-properties | |
5316 | end (point))))) | |
5317 | (when (> beg (point-min)) | |
5318 | (goto-char beg) | |
5319 | (if (and (c-end-of-current-token) ; only moves when we started in the middle | |
5320 | (progn (goto-char beg) | |
5321 | (c-beginning-of-current-token))) | |
5322 | (c-unfind-type (buffer-substring-no-properties | |
5323 | (point) beg)))))) | |
b414f371 | 5324 | |
580fba94 AM |
5325 | (if c-maybe-stale-found-type ; e.g. (c-decl-id-start "foo" 97 107 " (* ooka) " "o") |
5326 | (cond | |
5327 | ;; Changing the amount of (already existing) whitespace - don't do anything. | |
5328 | ((and (c-partial-ws-p beg end) | |
5329 | (or (= beg end) ; removal of WS | |
580fba94 AM |
5330 | (string-match "^[ \t\n\r\f\v]*$" (nth 5 c-maybe-stale-found-type))))) |
5331 | ||
5332 | ;; The syntactic relationship which defined a "found type" has been | |
5333 | ;; destroyed. | |
5334 | ((eq (car c-maybe-stale-found-type) 'c-decl-id-start) | |
5335 | (c-unfind-type (cadr c-maybe-stale-found-type))) | |
5336 | ;; ((eq (car c-maybe-stale-found-type) 'c-decl-type-start) FIXME!!! | |
5337 | ))) | |
5338 | ||
d9e94c22 | 5339 | \f |
dd969a56 AM |
5340 | ;; Setting and removing syntax properties on < and > in languages (C++ |
5341 | ;; and Java) where they can be template/generic delimiters as well as | |
5342 | ;; their normal meaning of "less/greater than". | |
5343 | ||
5344 | ;; Normally, < and > have syntax 'punctuation'. When they are found to | |
5345 | ;; be delimiters, they are marked as such with the category properties | |
5346 | ;; c-<-as-paren-syntax, c->-as-paren-syntax respectively. | |
5347 | ||
5348 | ;; STRATEGY: | |
5349 | ;; | |
5350 | ;; It is impossible to determine with certainty whether a <..> pair in | |
5351 | ;; C++ is two comparison operators or is template delimiters, unless | |
5352 | ;; one duplicates a lot of a C++ compiler. For example, the following | |
5353 | ;; code fragment: | |
5354 | ;; | |
5355 | ;; foo (a < b, c > d) ; | |
5356 | ;; | |
5357 | ;; could be a function call with two integer parameters (each a | |
5358 | ;; relational expression), or it could be a constructor for class foo | |
5359 | ;; taking one parameter d of templated type "a < b, c >". They are | |
5360 | ;; somewhat easier to distinguish in Java. | |
5361 | ;; | |
5362 | ;; The strategy now (2010-01) adopted is to mark and unmark < and | |
5363 | ;; > IN MATCHING PAIRS ONLY. [Previously, they were marked | |
5364 | ;; individually when their context so indicated. This gave rise to | |
53964682 | 5365 | ;; intractable problems when one of a matching pair was deleted, or |
dd969a56 AM |
5366 | ;; pulled into a literal.] |
5367 | ;; | |
5368 | ;; At each buffer change, the syntax-table properties are removed in a | |
5369 | ;; before-change function and reapplied, when needed, in an | |
5370 | ;; after-change function. It is far more important that the | |
5371 | ;; properties get removed when they they are spurious than that they | |
5372 | ;; be present when wanted. | |
5373 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | |
5374 | (defun c-clear-<-pair-props (&optional pos) | |
5375 | ;; POS (default point) is at a < character. If it is marked with | |
5376 | ;; open paren syntax-table text property, remove the property, | |
5377 | ;; together with the close paren property on the matching > (if | |
5378 | ;; any). | |
5379 | (save-excursion | |
5380 | (if pos | |
5381 | (goto-char pos) | |
5382 | (setq pos (point))) | |
5383 | (when (equal (c-get-char-property (point) 'syntax-table) | |
5384 | c-<-as-paren-syntax) | |
5385 | (with-syntax-table c-no-parens-syntax-table ; ignore unbalanced [,{,(,.. | |
5386 | (c-go-list-forward)) | |
5387 | (when (equal (c-get-char-property (1- (point)) 'syntax-table) | |
5388 | c->-as-paren-syntax) ; should always be true. | |
8a249abc AM |
5389 | (c-clear-char-property (1- (point)) 'category)) |
5390 | (c-clear-char-property pos 'category)))) | |
dd969a56 AM |
5391 | |
5392 | (defun c-clear->-pair-props (&optional pos) | |
5393 | ;; POS (default point) is at a > character. If it is marked with | |
5394 | ;; close paren syntax-table property, remove the property, together | |
5395 | ;; with the open paren property on the matching < (if any). | |
5396 | (save-excursion | |
5397 | (if pos | |
5398 | (goto-char pos) | |
5399 | (setq pos (point))) | |
5400 | (when (equal (c-get-char-property (point) 'syntax-table) | |
5401 | c->-as-paren-syntax) | |
5402 | (with-syntax-table c-no-parens-syntax-table ; ignore unbalanced [,{,(,.. | |
5403 | (c-go-up-list-backward)) | |
5404 | (when (equal (c-get-char-property (point) 'syntax-table) | |
5405 | c-<-as-paren-syntax) ; should always be true. | |
8a249abc AM |
5406 | (c-clear-char-property (point) 'category)) |
5407 | (c-clear-char-property pos 'category)))) | |
dd969a56 AM |
5408 | |
5409 | (defun c-clear-<>-pair-props (&optional pos) | |
5410 | ;; POS (default point) is at a < or > character. If it has an | |
5411 | ;; open/close paren syntax-table property, remove this property both | |
5412 | ;; from the current character and its partner (which will also be | |
5413 | ;; thusly marked). | |
5414 | (cond | |
5415 | ((eq (char-after) ?\<) | |
5416 | (c-clear-<-pair-props pos)) | |
5417 | ((eq (char-after) ?\>) | |
5418 | (c-clear->-pair-props pos)) | |
5419 | (t (c-benign-error | |
5420 | "c-clear-<>-pair-props called from wrong position")))) | |
5421 | ||
5422 | (defun c-clear-<-pair-props-if-match-after (lim &optional pos) | |
5423 | ;; POS (default point) is at a < character. If it is both marked | |
5424 | ;; with open/close paren syntax-table property, and has a matching > | |
5425 | ;; (also marked) which is after LIM, remove the property both from | |
43a91810 AM |
5426 | ;; the current > and its partner. Return t when this happens, nil |
5427 | ;; when it doesn't. | |
dd969a56 AM |
5428 | (save-excursion |
5429 | (if pos | |
5430 | (goto-char pos) | |
5431 | (setq pos (point))) | |
5432 | (when (equal (c-get-char-property (point) 'syntax-table) | |
5433 | c-<-as-paren-syntax) | |
5434 | (with-syntax-table c-no-parens-syntax-table ; ignore unbalanced [,{,(,.. | |
5435 | (c-go-list-forward)) | |
5436 | (when (and (>= (point) lim) | |
5437 | (equal (c-get-char-property (1- (point)) 'syntax-table) | |
5438 | c->-as-paren-syntax)) ; should always be true. | |
5439 | (c-unmark-<->-as-paren (1- (point))) | |
43a91810 AM |
5440 | (c-unmark-<->-as-paren pos)) |
5441 | t))) | |
dd969a56 AM |
5442 | |
5443 | (defun c-clear->-pair-props-if-match-before (lim &optional pos) | |
5444 | ;; POS (default point) is at a > character. If it is both marked | |
5445 | ;; with open/close paren syntax-table property, and has a matching < | |
5446 | ;; (also marked) which is before LIM, remove the property both from | |
43a91810 AM |
5447 | ;; the current < and its partner. Return t when this happens, nil |
5448 | ;; when it doesn't. | |
dd969a56 AM |
5449 | (save-excursion |
5450 | (if pos | |
5451 | (goto-char pos) | |
5452 | (setq pos (point))) | |
5453 | (when (equal (c-get-char-property (point) 'syntax-table) | |
5454 | c->-as-paren-syntax) | |
5455 | (with-syntax-table c-no-parens-syntax-table ; ignore unbalanced [,{,(,.. | |
5456 | (c-go-up-list-backward)) | |
5457 | (when (and (<= (point) lim) | |
5458 | (equal (c-get-char-property (point) 'syntax-table) | |
5459 | c-<-as-paren-syntax)) ; should always be true. | |
5460 | (c-unmark-<->-as-paren (point)) | |
43a91810 AM |
5461 | (c-unmark-<->-as-paren pos)) |
5462 | t))) | |
dd969a56 | 5463 | |
f3b554af GM |
5464 | ;; Set by c-common-init in cc-mode.el. |
5465 | (defvar c-new-BEG) | |
5466 | (defvar c-new-END) | |
5467 | ||
dd969a56 AM |
5468 | (defun c-before-change-check-<>-operators (beg end) |
5469 | ;; Unmark certain pairs of "< .... >" which are currently marked as | |
5470 | ;; template/generic delimiters. (This marking is via syntax-table | |
5471 | ;; text properties). | |
5472 | ;; | |
5473 | ;; These pairs are those which are in the current "statement" (i.e., | |
5474 | ;; the region between the {, }, or ; before BEG and the one after | |
5475 | ;; END), and which enclose any part of the interval (BEG END). | |
5476 | ;; | |
5477 | ;; Note that in C++ (?and Java), template/generic parens cannot | |
5478 | ;; enclose a brace or semicolon, so we use these as bounds on the | |
5479 | ;; region we must work on. | |
5480 | ;; | |
5481 | ;; This function is called from before-change-functions (via | |
5482 | ;; c-get-state-before-change-functions). Thus the buffer is widened, | |
5483 | ;; and point is undefined, both at entry and exit. | |
5484 | ;; | |
5485 | ;; FIXME!!! This routine ignores the possibility of macros entirely. | |
5486 | ;; 2010-01-29. | |
5487 | (save-excursion | |
5488 | (let ((beg-lit-limits (progn (goto-char beg) (c-literal-limits))) | |
43a91810 AM |
5489 | (end-lit-limits (progn (goto-char end) (c-literal-limits))) |
5490 | new-beg new-end need-new-beg need-new-end) | |
dd969a56 AM |
5491 | ;; Locate the barrier before the changed region |
5492 | (goto-char (if beg-lit-limits (car beg-lit-limits) beg)) | |
0de3da9f | 5493 | (c-syntactic-skip-backward "^;{}" (c-determine-limit 512)) |
43a91810 | 5494 | (setq new-beg (point)) |
dd969a56 AM |
5495 | |
5496 | ;; Remove the syntax-table properties from each pertinent <...> pair. | |
5497 | ;; Firsly, the ones with the < before beg and > after beg. | |
5498 | (while (c-search-forward-char-property 'category 'c-<-as-paren-syntax beg) | |
43a91810 AM |
5499 | (if (c-clear-<-pair-props-if-match-after beg (1- (point))) |
5500 | (setq need-new-beg t))) | |
dd969a56 AM |
5501 | |
5502 | ;; Locate the barrier after END. | |
5503 | (goto-char (if end-lit-limits (cdr end-lit-limits) end)) | |
0de3da9f | 5504 | (c-syntactic-re-search-forward "[;{}]" (c-determine-+ve-limit 512) 'end) |
43a91810 | 5505 | (setq new-end (point)) |
dd969a56 AM |
5506 | |
5507 | ;; Remove syntax-table properties from the remaining pertinent <...> | |
5508 | ;; pairs, those with a > after end and < before end. | |
5509 | (while (c-search-backward-char-property 'category 'c->-as-paren-syntax end) | |
43a91810 AM |
5510 | (if (c-clear->-pair-props-if-match-before end) |
5511 | (setq need-new-end t))) | |
5512 | ||
5513 | ;; Extend the fontification region, if needed. | |
5514 | (when need-new-beg | |
5515 | (goto-char new-beg) | |
5516 | (c-forward-syntactic-ws) | |
5517 | (and (< (point) c-new-BEG) (setq c-new-BEG (point)))) | |
5518 | ||
5519 | (when need-new-end | |
5520 | (and (> new-end c-new-END) (setq c-new-END new-end)))))) | |
dd969a56 AM |
5521 | |
5522 | ||
d9e94c22 | 5523 | |
0386b551 AM |
5524 | (defun c-after-change-check-<>-operators (beg end) |
5525 | ;; This is called from `after-change-functions' when | |
5526 | ;; c-recognize-<>-arglists' is set. It ensures that no "<" or ">" | |
5527 | ;; chars with paren syntax become part of another operator like "<<" | |
5528 | ;; or ">=". | |
5529 | ;; | |
5530 | ;; This function might do hidden buffer changes. | |
5531 | ||
cb694ab7 AM |
5532 | (save-excursion |
5533 | (goto-char beg) | |
5534 | (when (or (looking-at "[<>]") | |
5535 | (< (skip-chars-backward "<>") 0)) | |
5536 | ||
0386b551 | 5537 | (goto-char beg) |
cb694ab7 AM |
5538 | (c-beginning-of-current-token) |
5539 | (when (and (< (point) beg) | |
5540 | (looking-at c-<>-multichar-token-regexp) | |
5541 | (< beg (setq beg (match-end 0)))) | |
5542 | (while (progn (skip-chars-forward "^<>" beg) | |
5543 | (< (point) beg)) | |
dd969a56 | 5544 | (c-clear-<>-pair-props) |
cb694ab7 AM |
5545 | (forward-char)))) |
5546 | ||
5547 | (when (< beg end) | |
5548 | (goto-char end) | |
0386b551 AM |
5549 | (when (or (looking-at "[<>]") |
5550 | (< (skip-chars-backward "<>") 0)) | |
5551 | ||
cb694ab7 | 5552 | (goto-char end) |
0386b551 | 5553 | (c-beginning-of-current-token) |
cb694ab7 | 5554 | (when (and (< (point) end) |
0386b551 | 5555 | (looking-at c-<>-multichar-token-regexp) |
cb694ab7 AM |
5556 | (< end (setq end (match-end 0)))) |
5557 | (while (progn (skip-chars-forward "^<>" end) | |
5558 | (< (point) end)) | |
dd969a56 | 5559 | (c-clear-<>-pair-props) |
cb694ab7 | 5560 | (forward-char))))))) |
d9e94c22 | 5561 | |
dd969a56 AM |
5562 | |
5563 | \f | |
5564 | ;; Handling of small scale constructs like types and names. | |
5565 | ||
d9e94c22 MS |
5566 | ;; Dynamically bound variable that instructs `c-forward-type' to also |
5567 | ;; treat possible types (i.e. those that it normally returns 'maybe or | |
5568 | ;; 'found for) as actual types (and always return 'found for them). | |
5569 | ;; This means that it records them in `c-record-type-identifiers' if | |
5570 | ;; that is set, and that it adds them to `c-found-types'. | |
5571 | (defvar c-promote-possible-types nil) | |
5572 | ||
0386b551 AM |
5573 | ;; Dynamically bound variable that instructs `c-forward-<>-arglist' to |
5574 | ;; mark up successfully parsed arglists with paren syntax properties on | |
5575 | ;; the surrounding angle brackets and with `c-<>-arg-sep' in the | |
5576 | ;; `c-type' property of each argument separating comma. | |
5577 | ;; | |
5578 | ;; Setting this variable also makes `c-forward-<>-arglist' recurse into | |
5579 | ;; all arglists for side effects (i.e. recording types), otherwise it | |
5580 | ;; exploits any existing paren syntax properties to quickly jump to the | |
5581 | ;; end of already parsed arglists. | |
5582 | ;; | |
5583 | ;; Marking up the arglists is not the default since doing that correctly | |
5584 | ;; depends on a proper value for `c-restricted-<>-arglists'. | |
5585 | (defvar c-parse-and-markup-<>-arglists nil) | |
5586 | ||
d9e94c22 | 5587 | ;; Dynamically bound variable that instructs `c-forward-<>-arglist' to |
037558bf MS |
5588 | ;; not accept arglists that contain binary operators. |
5589 | ;; | |
5590 | ;; This is primarily used to handle C++ template arglists. C++ | |
5591 | ;; disambiguates them by checking whether the preceding name is a | |
5592 | ;; template or not. We can't do that, so we assume it is a template | |
5593 | ;; if it can be parsed as one. That usually works well since | |
5594 | ;; comparison expressions on the forms "a < b > c" or "a < b, c > d" | |
5595 | ;; in almost all cases would be pointless. | |
5596 | ;; | |
5597 | ;; However, in function arglists, e.g. in "foo (a < b, c > d)", we | |
5598 | ;; should let the comma separate the function arguments instead. And | |
5599 | ;; in a context where the value of the expression is taken, e.g. in | |
5600 | ;; "if (a < b || c > d)", it's probably not a template. | |
5601 | (defvar c-restricted-<>-arglists nil) | |
d9e94c22 | 5602 | |
0386b551 AM |
5603 | ;; Dynamically bound variables that instructs |
5604 | ;; `c-forward-keyword-clause', `c-forward-<>-arglist', | |
5605 | ;; `c-forward-name', `c-forward-type', `c-forward-decl-or-cast-1', and | |
5606 | ;; `c-forward-label' to record the ranges of all the type and | |
5607 | ;; reference identifiers they encounter. They will build lists on | |
5608 | ;; these variables where each element is a cons of the buffer | |
5609 | ;; positions surrounding each identifier. This recording is only | |
5610 | ;; activated when `c-record-type-identifiers' is non-nil. | |
d9e94c22 MS |
5611 | ;; |
5612 | ;; All known types that can't be identifiers are recorded, and also | |
5613 | ;; other possible types if `c-promote-possible-types' is set. | |
5614 | ;; Recording is however disabled inside angle bracket arglists that | |
5615 | ;; are encountered inside names and other angle bracket arglists. | |
0386b551 | 5616 | ;; Such occurrences are taken care of by `c-font-lock-<>-arglists' |
d9e94c22 MS |
5617 | ;; instead. |
5618 | ;; | |
5619 | ;; Only the names in C++ template style references (e.g. "tmpl" in | |
5620 | ;; "tmpl<a,b>::foo") are recorded as references, other references | |
5621 | ;; aren't handled here. | |
0386b551 AM |
5622 | ;; |
5623 | ;; `c-forward-label' records the label identifier(s) on | |
5624 | ;; `c-record-ref-identifiers'. | |
d9e94c22 MS |
5625 | (defvar c-record-type-identifiers nil) |
5626 | (defvar c-record-ref-identifiers nil) | |
5627 | ||
0386b551 AM |
5628 | ;; This variable will receive a cons cell of the range of the last |
5629 | ;; single identifier symbol stepped over by `c-forward-name' if it's | |
5630 | ;; successful. This is the range that should be put on one of the | |
5631 | ;; record lists above by the caller. It's assigned nil if there's no | |
5632 | ;; such symbol in the name. | |
d9e94c22 MS |
5633 | (defvar c-last-identifier-range nil) |
5634 | ||
5635 | (defmacro c-record-type-id (range) | |
5636 | (if (eq (car-safe range) 'cons) | |
5637 | ;; Always true. | |
5638 | `(setq c-record-type-identifiers | |
5639 | (cons ,range c-record-type-identifiers)) | |
5640 | `(let ((range ,range)) | |
5641 | (if range | |
5642 | (setq c-record-type-identifiers | |
5643 | (cons range c-record-type-identifiers)))))) | |
5644 | ||
5645 | (defmacro c-record-ref-id (range) | |
5646 | (if (eq (car-safe range) 'cons) | |
5647 | ;; Always true. | |
5648 | `(setq c-record-ref-identifiers | |
5649 | (cons ,range c-record-ref-identifiers)) | |
5650 | `(let ((range ,range)) | |
5651 | (if range | |
5652 | (setq c-record-ref-identifiers | |
5653 | (cons range c-record-ref-identifiers)))))) | |
5654 | ||
5655 | ;; Dynamically bound variable that instructs `c-forward-type' to | |
5656 | ;; record the ranges of types that only are found. Behaves otherwise | |
5657 | ;; like `c-record-type-identifiers'. | |
5658 | (defvar c-record-found-types nil) | |
5659 | ||
5660 | (defmacro c-forward-keyword-prefixed-id (type) | |
5661 | ;; Used internally in `c-forward-keyword-clause' to move forward | |
5662 | ;; over a type (if TYPE is 'type) or a name (otherwise) which | |
5663 | ;; possibly is prefixed by keywords and their associated clauses. | |
5664 | ;; Try with a type/name first to not trip up on those that begin | |
5665 | ;; with a keyword. Return t if a known or found type is moved | |
5666 | ;; over. The point is clobbered if nil is returned. If range | |
5667 | ;; recording is enabled, the identifier is recorded on as a type | |
5668 | ;; if TYPE is 'type or as a reference if TYPE is 'ref. | |
0386b551 AM |
5669 | ;; |
5670 | ;; This macro might do hidden buffer changes. | |
d9e94c22 MS |
5671 | `(let (res) |
5672 | (while (if (setq res ,(if (eq type 'type) | |
5673 | `(c-forward-type) | |
5674 | `(c-forward-name))) | |
5675 | nil | |
5676 | (and (looking-at c-keywords-regexp) | |
0386b551 | 5677 | (c-forward-keyword-clause 1)))) |
d9e94c22 MS |
5678 | (when (memq res '(t known found prefix)) |
5679 | ,(when (eq type 'ref) | |
5680 | `(when c-record-type-identifiers | |
5681 | (c-record-ref-id c-last-identifier-range))) | |
5682 | t))) | |
5683 | ||
0386b551 | 5684 | (defmacro c-forward-id-comma-list (type update-safe-pos) |
d9e94c22 MS |
5685 | ;; Used internally in `c-forward-keyword-clause' to move forward |
5686 | ;; over a comma separated list of types or names using | |
5687 | ;; `c-forward-keyword-prefixed-id'. | |
0386b551 AM |
5688 | ;; |
5689 | ;; This macro might do hidden buffer changes. | |
d9e94c22 | 5690 | `(while (and (progn |
0386b551 AM |
5691 | ,(when update-safe-pos |
5692 | `(setq safe-pos (point))) | |
d9e94c22 MS |
5693 | (eq (char-after) ?,)) |
5694 | (progn | |
5695 | (forward-char) | |
5696 | (c-forward-syntactic-ws) | |
5697 | (c-forward-keyword-prefixed-id ,type))))) | |
5698 | ||
0386b551 AM |
5699 | (defun c-forward-keyword-clause (match) |
5700 | ;; Submatch MATCH in the current match data is assumed to surround a | |
5701 | ;; token. If it's a keyword, move over it and any immediately | |
5702 | ;; following clauses associated with it, stopping at the start of | |
5703 | ;; the next token. t is returned in that case, otherwise the point | |
d9e94c22 MS |
5704 | ;; stays and nil is returned. The kind of clauses that are |
5705 | ;; recognized are those specified by `c-type-list-kwds', | |
5706 | ;; `c-ref-list-kwds', `c-colon-type-list-kwds', | |
5707 | ;; `c-paren-nontype-kwds', `c-paren-type-kwds', `c-<>-type-kwds', | |
5708 | ;; and `c-<>-arglist-kwds'. | |
0386b551 AM |
5709 | ;; |
5710 | ;; This function records identifier ranges on | |
5711 | ;; `c-record-type-identifiers' and `c-record-ref-identifiers' if | |
5712 | ;; `c-record-type-identifiers' is non-nil. | |
5713 | ;; | |
5714 | ;; Note that for `c-colon-type-list-kwds', which doesn't necessary | |
5715 | ;; apply directly after the keyword, the type list is moved over | |
5716 | ;; only when there is no unaccounted token before it (i.e. a token | |
5717 | ;; that isn't moved over due to some other keyword list). The | |
5718 | ;; identifier ranges in the list are still recorded if that should | |
5719 | ;; be done, though. | |
5720 | ;; | |
5721 | ;; This function might do hidden buffer changes. | |
5722 | ||
5723 | (let ((kwd-sym (c-keyword-sym (match-string match))) safe-pos pos | |
5724 | ;; The call to `c-forward-<>-arglist' below is made after | |
5725 | ;; `c-<>-sexp-kwds' keywords, so we're certain they actually | |
5726 | ;; are angle bracket arglists and `c-restricted-<>-arglists' | |
5727 | ;; should therefore be nil. | |
5728 | (c-parse-and-markup-<>-arglists t) | |
5729 | c-restricted-<>-arglists) | |
d9e94c22 | 5730 | |
d9e94c22 | 5731 | (when kwd-sym |
0386b551 | 5732 | (goto-char (match-end match)) |
d9e94c22 MS |
5733 | (c-forward-syntactic-ws) |
5734 | (setq safe-pos (point)) | |
a66cd3ee | 5735 | |
d9e94c22 MS |
5736 | (cond |
5737 | ((and (c-keyword-member kwd-sym 'c-type-list-kwds) | |
5738 | (c-forward-keyword-prefixed-id type)) | |
5739 | ;; There's a type directly after a keyword in `c-type-list-kwds'. | |
0386b551 | 5740 | (c-forward-id-comma-list type t)) |
d9e94c22 MS |
5741 | |
5742 | ((and (c-keyword-member kwd-sym 'c-ref-list-kwds) | |
5743 | (c-forward-keyword-prefixed-id ref)) | |
5744 | ;; There's a name directly after a keyword in `c-ref-list-kwds'. | |
0386b551 | 5745 | (c-forward-id-comma-list ref t)) |
d9e94c22 MS |
5746 | |
5747 | ((and (c-keyword-member kwd-sym 'c-paren-any-kwds) | |
5748 | (eq (char-after) ?\()) | |
5749 | ;; There's an open paren after a keyword in `c-paren-any-kwds'. | |
5750 | ||
5751 | (forward-char) | |
5752 | (when (and (setq pos (c-up-list-forward)) | |
5753 | (eq (char-before pos) ?\))) | |
5754 | (when (and c-record-type-identifiers | |
5755 | (c-keyword-member kwd-sym 'c-paren-type-kwds)) | |
5756 | ;; Use `c-forward-type' on every identifier we can find | |
5757 | ;; inside the paren, to record the types. | |
5758 | (while (c-syntactic-re-search-forward c-symbol-start pos t) | |
5759 | (goto-char (match-beginning 0)) | |
5760 | (unless (c-forward-type) | |
5761 | (looking-at c-symbol-key) ; Always matches. | |
5762 | (goto-char (match-end 0))))) | |
5763 | ||
5764 | (goto-char pos) | |
5765 | (c-forward-syntactic-ws) | |
5766 | (setq safe-pos (point)))) | |
5767 | ||
5768 | ((and (c-keyword-member kwd-sym 'c-<>-sexp-kwds) | |
5769 | (eq (char-after) ?<) | |
0386b551 | 5770 | (c-forward-<>-arglist (c-keyword-member kwd-sym 'c-<>-type-kwds))) |
d9e94c22 MS |
5771 | (c-forward-syntactic-ws) |
5772 | (setq safe-pos (point))) | |
5773 | ||
5774 | ((and (c-keyword-member kwd-sym 'c-nonsymbol-sexp-kwds) | |
449a2b0d MS |
5775 | (not (looking-at c-symbol-start)) |
5776 | (c-safe (c-forward-sexp) t)) | |
d9e94c22 MS |
5777 | (c-forward-syntactic-ws) |
5778 | (setq safe-pos (point)))) | |
5779 | ||
0386b551 AM |
5780 | (when (c-keyword-member kwd-sym 'c-colon-type-list-kwds) |
5781 | (if (eq (char-after) ?:) | |
5782 | ;; If we are at the colon already, we move over the type | |
5783 | ;; list after it. | |
5784 | (progn | |
5785 | (forward-char) | |
5786 | (c-forward-syntactic-ws) | |
5787 | (when (c-forward-keyword-prefixed-id type) | |
5788 | (c-forward-id-comma-list type t))) | |
5789 | ;; Not at the colon, so stop here. But the identifier | |
5790 | ;; ranges in the type list later on should still be | |
5791 | ;; recorded. | |
5792 | (and c-record-type-identifiers | |
5793 | (progn | |
5794 | ;; If a keyword matched both one of the types above and | |
5795 | ;; this one, we match `c-colon-type-list-re' after the | |
5796 | ;; clause matched above. | |
5797 | (goto-char safe-pos) | |
5798 | (looking-at c-colon-type-list-re)) | |
5799 | (progn | |
5800 | (goto-char (match-end 0)) | |
5801 | (c-forward-syntactic-ws) | |
5802 | (c-forward-keyword-prefixed-id type)) | |
5803 | ;; There's a type after the `c-colon-type-list-re' match | |
5804 | ;; after a keyword in `c-colon-type-list-kwds'. | |
5805 | (c-forward-id-comma-list type nil)))) | |
d9e94c22 MS |
5806 | |
5807 | (goto-char safe-pos) | |
5808 | t))) | |
5809 | ||
f3b554af GM |
5810 | ;; cc-mode requires cc-fonts. |
5811 | (declare-function c-fontify-recorded-types-and-refs "cc-fonts" ()) | |
5812 | ||
0386b551 AM |
5813 | (defun c-forward-<>-arglist (all-types) |
5814 | ;; The point is assumed to be at a "<". Try to treat it as the open | |
b4dc7d98 | 5815 | ;; paren of an angle bracket arglist and move forward to the |
0386b551 AM |
5816 | ;; corresponding ">". If successful, the point is left after the |
5817 | ;; ">" and t is returned, otherwise the point isn't moved and nil is | |
d9e94c22 MS |
5818 | ;; returned. If ALL-TYPES is t then all encountered arguments in |
5819 | ;; the arglist that might be types are treated as found types. | |
5820 | ;; | |
0386b551 AM |
5821 | ;; The variable `c-parse-and-markup-<>-arglists' controls how this |
5822 | ;; function handles text properties on the angle brackets and argument | |
5823 | ;; separating commas. | |
d9e94c22 | 5824 | ;; |
0386b551 AM |
5825 | ;; `c-restricted-<>-arglists' controls how lenient the template |
5826 | ;; arglist recognition should be. | |
5827 | ;; | |
5828 | ;; This function records identifier ranges on | |
5829 | ;; `c-record-type-identifiers' and `c-record-ref-identifiers' if | |
5830 | ;; `c-record-type-identifiers' is non-nil. | |
5831 | ;; | |
5832 | ;; This function might do hidden buffer changes. | |
d9e94c22 MS |
5833 | |
5834 | (let ((start (point)) | |
5835 | ;; If `c-record-type-identifiers' is set then activate | |
5836 | ;; recording of any found types that constitute an argument in | |
5837 | ;; the arglist. | |
abfc152b | 5838 | (c-record-found-types (if c-record-type-identifiers t))) |
d9e94c22 MS |
5839 | (if (catch 'angle-bracket-arglist-escape |
5840 | (setq c-record-found-types | |
0386b551 | 5841 | (c-forward-<>-arglist-recur all-types))) |
d9e94c22 MS |
5842 | (progn |
5843 | (when (consp c-record-found-types) | |
5844 | (setq c-record-type-identifiers | |
5845 | ;; `nconc' doesn't mind that the tail of | |
5846 | ;; `c-record-found-types' is t. | |
5847 | (nconc c-record-found-types c-record-type-identifiers))) | |
452ea855 | 5848 | (if (c-major-mode-is 'java-mode) (c-fontify-recorded-types-and-refs)) |
d9e94c22 MS |
5849 | t) |
5850 | ||
5851 | (goto-char start) | |
a66cd3ee | 5852 | nil))) |
785eecbb | 5853 | |
0386b551 | 5854 | (defun c-forward-<>-arglist-recur (all-types) |
d9e94c22 | 5855 | ;; Recursive part of `c-forward-<>-arglist'. |
0386b551 AM |
5856 | ;; |
5857 | ;; This function might do hidden buffer changes. | |
d9e94c22 MS |
5858 | |
5859 | (let ((start (point)) res pos tmp | |
5860 | ;; Cover this so that any recorded found type ranges are | |
5861 | ;; automatically lost if it turns out to not be an angle | |
5862 | ;; bracket arglist. It's propagated through the return value | |
5863 | ;; on successful completion. | |
5864 | (c-record-found-types c-record-found-types) | |
5865 | ;; List that collects the positions after the argument | |
5866 | ;; separating ',' in the arglist. | |
5867 | arg-start-pos) | |
0386b551 AM |
5868 | ;; If the '<' has paren open syntax then we've marked it as an angle |
5869 | ;; bracket arglist before, so skip to the end. | |
5870 | (if (and (not c-parse-and-markup-<>-arglists) | |
5871 | (c-get-char-property (point) 'syntax-table)) | |
5872 | ||
5873 | (progn | |
5874 | (forward-char) | |
5875 | (if (and (c-go-up-list-forward) | |
5876 | (eq (char-before) ?>)) | |
5877 | t | |
0386b551 AM |
5878 | ;; Got unmatched paren angle brackets. We don't clear the paren |
5879 | ;; syntax properties and retry, on the basis that it's very | |
5880 | ;; unlikely that paren angle brackets become operators by code | |
5881 | ;; manipulation. It's far more likely that it doesn't match due | |
5882 | ;; to narrowing or some temporary change. | |
5883 | (goto-char start) | |
5884 | nil)) | |
d9e94c22 | 5885 | |
abfc152b | 5886 | (forward-char) ; Forward over the opening '<'. |
452ea855 | 5887 | |
d9e94c22 | 5888 | (unless (looking-at c-<-op-cont-regexp) |
abfc152b AM |
5889 | ;; go forward one non-alphanumeric character (group) per iteration of |
5890 | ;; this loop. | |
22c3ce97 | 5891 | (while (and |
d9e94c22 | 5892 | (progn |
22c3ce97 AM |
5893 | (c-forward-syntactic-ws) |
5894 | (let ((orig-record-found-types c-record-found-types)) | |
5895 | (when (or (and c-record-type-identifiers all-types) | |
5896 | (c-major-mode-is 'java-mode)) | |
5897 | ;; All encountered identifiers are types, so set the | |
5898 | ;; promote flag and parse the type. | |
5899 | (progn | |
5900 | (c-forward-syntactic-ws) | |
5901 | (if (looking-at "\\?") | |
5902 | (forward-char) | |
5903 | (when (looking-at c-identifier-start) | |
5904 | (let ((c-promote-possible-types t) | |
5905 | (c-record-found-types t)) | |
5906 | (c-forward-type)))) | |
5907 | ||
5908 | (c-forward-syntactic-ws) | |
5909 | ||
5910 | (when (or (looking-at "extends") | |
5911 | (looking-at "super")) | |
5912 | (forward-word) | |
5913 | (c-forward-syntactic-ws) | |
5914 | (let ((c-promote-possible-types t) | |
5915 | (c-record-found-types t)) | |
5916 | (c-forward-type) | |
5917 | (c-forward-syntactic-ws)))))) | |
5918 | ||
abfc152b | 5919 | (setq pos (point)) ; e.g. first token inside the '<' |
22c3ce97 | 5920 | |
a4ee83cc AM |
5921 | ;; Note: These regexps exploit the match order in \| so |
5922 | ;; that "<>" is matched by "<" rather than "[^>:-]>". | |
5923 | (c-syntactic-re-search-forward | |
5924 | ;; Stop on ',', '|', '&', '+' and '-' to catch | |
5925 | ;; common binary operators that could be between | |
5926 | ;; two comparison expressions "a<b" and "c>d". | |
5927 | "[<;{},|+&-]\\|[>)]" | |
5928 | nil t t)) | |
22c3ce97 AM |
5929 | |
5930 | (cond | |
5931 | ((eq (char-before) ?>) | |
d9e94c22 MS |
5932 | ;; Either an operator starting with '>' or the end of |
5933 | ;; the angle bracket arglist. | |
5934 | ||
0386b551 | 5935 | (if (looking-at c->-op-cont-regexp) |
d9e94c22 | 5936 | (progn |
0386b551 AM |
5937 | (goto-char (match-end 0)) |
5938 | t) ; Continue the loop. | |
d9e94c22 | 5939 | |
0386b551 AM |
5940 | ;; The angle bracket arglist is finished. |
5941 | (when c-parse-and-markup-<>-arglists | |
d9e94c22 | 5942 | (while arg-start-pos |
0386b551 AM |
5943 | (c-put-c-type-property (1- (car arg-start-pos)) |
5944 | 'c-<>-arg-sep) | |
d9e94c22 MS |
5945 | (setq arg-start-pos (cdr arg-start-pos))) |
5946 | (c-mark-<-as-paren start) | |
0386b551 AM |
5947 | (c-mark->-as-paren (1- (point)))) |
5948 | (setq res t) | |
5949 | nil)) ; Exit the loop. | |
d9e94c22 MS |
5950 | |
5951 | ((eq (char-before) ?<) | |
5952 | ;; Either an operator starting with '<' or a nested arglist. | |
d9e94c22 MS |
5953 | (setq pos (point)) |
5954 | (let (id-start id-end subres keyword-match) | |
abfc152b AM |
5955 | (cond |
5956 | ;; The '<' begins a multi-char operator. | |
5957 | ((looking-at c-<-op-cont-regexp) | |
5958 | (setq tmp (match-end 0)) | |
5959 | (goto-char (match-end 0))) | |
5960 | ;; We're at a nested <.....> | |
5961 | ((progn | |
5962 | (setq tmp pos) | |
5963 | (backward-char) ; to the '<' | |
5964 | (and | |
5965 | (save-excursion | |
5966 | ;; There's always an identifier before an angle | |
5967 | ;; bracket arglist, or a keyword in `c-<>-type-kwds' | |
5968 | ;; or `c-<>-arglist-kwds'. | |
5969 | (c-backward-syntactic-ws) | |
5970 | (setq id-end (point)) | |
5971 | (c-simple-skip-symbol-backward) | |
5972 | (when (or (setq keyword-match | |
5973 | (looking-at c-opt-<>-sexp-key)) | |
5974 | (not (looking-at c-keywords-regexp))) | |
5975 | (setq id-start (point)))) | |
5976 | (setq subres | |
5977 | (let ((c-promote-possible-types t) | |
5978 | (c-record-found-types t)) | |
5979 | (c-forward-<>-arglist-recur | |
5980 | (and keyword-match | |
5981 | (c-keyword-member | |
5982 | (c-keyword-sym (match-string 1)) | |
5983 | 'c-<>-type-kwds))))))) | |
d9e94c22 MS |
5984 | |
5985 | ;; It was an angle bracket arglist. | |
5986 | (setq c-record-found-types subres) | |
5987 | ||
5988 | ;; Record the identifier before the template as a type | |
5989 | ;; or reference depending on whether the arglist is last | |
5990 | ;; in a qualified identifier. | |
5991 | (when (and c-record-type-identifiers | |
5992 | (not keyword-match)) | |
5993 | (if (and c-opt-identifier-concat-key | |
5994 | (progn | |
5995 | (c-forward-syntactic-ws) | |
5996 | (looking-at c-opt-identifier-concat-key))) | |
5997 | (c-record-ref-id (cons id-start id-end)) | |
abfc152b AM |
5998 | (c-record-type-id (cons id-start id-end))))) |
5999 | ||
6000 | ;; At a "less than" operator. | |
6001 | (t | |
6002 | (forward-char) | |
6003 | ))) | |
6004 | t) ; carry on looping. | |
22c3ce97 AM |
6005 | |
6006 | ((and (not c-restricted-<>-arglists) | |
6007 | (or (and (eq (char-before) ?&) | |
6008 | (not (eq (char-after) ?&))) | |
6009 | (eq (char-before) ?,))) | |
6010 | ;; Just another argument. Record the position. The | |
6011 | ;; type check stuff that made us stop at it is at | |
6012 | ;; the top of the loop. | |
d9e94c22 MS |
6013 | (setq arg-start-pos (cons (point) arg-start-pos))) |
6014 | ||
6015 | (t | |
6016 | ;; Got a character that can't be in an angle bracket | |
6017 | ;; arglist argument. Abort using `throw', since | |
6018 | ;; it's useless to try to find a surrounding arglist | |
6019 | ;; if we're nested. | |
6020 | (throw 'angle-bracket-arglist-escape nil)))))) | |
d9e94c22 MS |
6021 | (if res |
6022 | (or c-record-found-types t))))) | |
6023 | ||
0386b551 AM |
6024 | (defun c-backward-<>-arglist (all-types &optional limit) |
6025 | ;; The point is assumed to be directly after a ">". Try to treat it | |
6026 | ;; as the close paren of an angle bracket arglist and move back to | |
6027 | ;; the corresponding "<". If successful, the point is left at | |
6028 | ;; the "<" and t is returned, otherwise the point isn't moved and | |
6029 | ;; nil is returned. ALL-TYPES is passed on to | |
6030 | ;; `c-forward-<>-arglist'. | |
6031 | ;; | |
6032 | ;; If the optional LIMIT is given, it bounds the backward search. | |
6033 | ;; It's then assumed to be at a syntactically relevant position. | |
6034 | ;; | |
6035 | ;; This is a wrapper around `c-forward-<>-arglist'. See that | |
6036 | ;; function for more details. | |
6037 | ||
6038 | (let ((start (point))) | |
6039 | (backward-char) | |
6040 | (if (and (not c-parse-and-markup-<>-arglists) | |
6041 | (c-get-char-property (point) 'syntax-table)) | |
6042 | ||
6043 | (if (and (c-go-up-list-backward) | |
6044 | (eq (char-after) ?<)) | |
6045 | t | |
6046 | ;; See corresponding note in `c-forward-<>-arglist'. | |
6047 | (goto-char start) | |
6048 | nil) | |
6049 | ||
51c9af45 | 6050 | (while (progn |
0386b551 AM |
6051 | (c-syntactic-skip-backward "^<;{}" limit t) |
6052 | ||
51c9af45 AM |
6053 | (and |
6054 | (if (eq (char-before) ?<) | |
6055 | t | |
6056 | ;; Stopped at bob or a char that isn't allowed in an | |
6057 | ;; arglist, so we've failed. | |
6058 | (goto-char start) | |
6059 | nil) | |
0386b551 | 6060 | |
51c9af45 AM |
6061 | (if (> (point) |
6062 | (progn (c-beginning-of-current-token) | |
6063 | (point))) | |
6064 | ;; If we moved then the "<" was part of some | |
6065 | ;; multicharacter token. | |
6066 | t | |
0386b551 | 6067 | |
51c9af45 AM |
6068 | (backward-char) |
6069 | (let ((beg-pos (point))) | |
6070 | (if (c-forward-<>-arglist all-types) | |
6071 | (cond ((= (point) start) | |
6072 | ;; Matched the arglist. Break the while. | |
6073 | (goto-char beg-pos) | |
6074 | nil) | |
6075 | ((> (point) start) | |
6076 | ;; We started from a non-paren ">" inside an | |
6077 | ;; arglist. | |
6078 | (goto-char start) | |
6079 | nil) | |
6080 | (t | |
6081 | ;; Matched a shorter arglist. Can be a nested | |
6082 | ;; one so continue looking. | |
6083 | (goto-char beg-pos) | |
6084 | t)) | |
6085 | t)))))) | |
0386b551 AM |
6086 | |
6087 | (/= (point) start)))) | |
6088 | ||
d9e94c22 MS |
6089 | (defun c-forward-name () |
6090 | ;; Move forward over a complete name if at the beginning of one, | |
e15f8aaa AM |
6091 | ;; stopping at the next following token. A keyword, as such, |
6092 | ;; doesn't count as a name. If the point is not at something that | |
6093 | ;; is recognized as a name then it stays put. | |
6094 | ;; | |
6095 | ;; A name could be something as simple as "foo" in C or something as | |
d9e94c22 MS |
6096 | ;; complex as "X<Y<class A<int>::B, BIT_MAX >> b>, ::operator<> :: |
6097 | ;; Z<(a>b)> :: operator const X<&foo>::T Q::G<unsigned short | |
6098 | ;; int>::*volatile const" in C++ (this function is actually little | |
6099 | ;; more than a `looking-at' call in all modes except those that, | |
e15f8aaa AM |
6100 | ;; like C++, have `c-recognize-<>-arglists' set). |
6101 | ;; | |
6102 | ;; Return | |
6103 | ;; o - nil if no name is found; | |
6104 | ;; o - 'template if it's an identifier ending with an angle bracket | |
6105 | ;; arglist; | |
6106 | ;; o - 'operator of it's an operator identifier; | |
6107 | ;; o - t if it's some other kind of name. | |
0386b551 AM |
6108 | ;; |
6109 | ;; This function records identifier ranges on | |
6110 | ;; `c-record-type-identifiers' and `c-record-ref-identifiers' if | |
6111 | ;; `c-record-type-identifiers' is non-nil. | |
6112 | ;; | |
6113 | ;; This function might do hidden buffer changes. | |
d9e94c22 | 6114 | |
0386b551 | 6115 | (let ((pos (point)) (start (point)) res id-start id-end |
d9e94c22 MS |
6116 | ;; Turn off `c-promote-possible-types' here since we might |
6117 | ;; call `c-forward-<>-arglist' and we don't want it to promote | |
6118 | ;; every suspect thing in the arglist to a type. We're | |
6119 | ;; typically called from `c-forward-type' in this case, and | |
6120 | ;; the caller only wants the top level type that it finds to | |
6121 | ;; be promoted. | |
6122 | c-promote-possible-types) | |
6123 | (while | |
6124 | (and | |
6125 | (looking-at c-identifier-key) | |
6126 | ||
6127 | (progn | |
6128 | ;; Check for keyword. We go to the last symbol in | |
6129 | ;; `c-identifier-key' first. | |
0386b551 AM |
6130 | (goto-char (setq id-end (match-end 0))) |
6131 | (c-simple-skip-symbol-backward) | |
6132 | (setq id-start (point)) | |
d9e94c22 MS |
6133 | |
6134 | (if (looking-at c-keywords-regexp) | |
6135 | (when (and (c-major-mode-is 'c++-mode) | |
6136 | (looking-at | |
6137 | (cc-eval-when-compile | |
6138 | (concat "\\(operator\\|\\(template\\)\\)" | |
6139 | "\\(" (c-lang-const c-nonsymbol-key c++) | |
6140 | "\\|$\\)"))) | |
6141 | (if (match-beginning 2) | |
6142 | ;; "template" is only valid inside an | |
6143 | ;; identifier if preceded by "::". | |
6144 | (save-excursion | |
6145 | (c-backward-syntactic-ws) | |
6146 | (and (c-safe (backward-char 2) t) | |
6147 | (looking-at "::"))) | |
6148 | t)) | |
6149 | ||
6150 | ;; Handle a C++ operator or template identifier. | |
6151 | (goto-char id-end) | |
6152 | (c-forward-syntactic-ws) | |
6153 | (cond ((eq (char-before id-end) ?e) | |
6154 | ;; Got "... ::template". | |
6155 | (let ((subres (c-forward-name))) | |
6156 | (when subres | |
6157 | (setq pos (point) | |
6158 | res subres)))) | |
6159 | ||
6160 | ((looking-at c-identifier-start) | |
6161 | ;; Got a cast operator. | |
6162 | (when (c-forward-type) | |
6163 | (setq pos (point) | |
6164 | res 'operator) | |
6165 | ;; Now we should match a sequence of either | |
6166 | ;; '*', '&' or a name followed by ":: *", | |
6167 | ;; where each can be followed by a sequence | |
6168 | ;; of `c-opt-type-modifier-key'. | |
6169 | (while (cond ((looking-at "[*&]") | |
6170 | (goto-char (match-end 0)) | |
6171 | t) | |
6172 | ((looking-at c-identifier-start) | |
6173 | (and (c-forward-name) | |
6174 | (looking-at "::") | |
6175 | (progn | |
6176 | (goto-char (match-end 0)) | |
6177 | (c-forward-syntactic-ws) | |
6178 | (eq (char-after) ?*)) | |
6179 | (progn | |
6180 | (forward-char) | |
6181 | t)))) | |
6182 | (while (progn | |
6183 | (c-forward-syntactic-ws) | |
6184 | (setq pos (point)) | |
6185 | (looking-at c-opt-type-modifier-key)) | |
6186 | (goto-char (match-end 1)))))) | |
6187 | ||
6188 | ((looking-at c-overloadable-operators-regexp) | |
6189 | ;; Got some other operator. | |
0386b551 AM |
6190 | (setq c-last-identifier-range |
6191 | (cons (point) (match-end 0))) | |
d9e94c22 MS |
6192 | (goto-char (match-end 0)) |
6193 | (c-forward-syntactic-ws) | |
6194 | (setq pos (point) | |
6195 | res 'operator))) | |
6196 | ||
6197 | nil) | |
6198 | ||
0386b551 AM |
6199 | ;; `id-start' is equal to `id-end' if we've jumped over |
6200 | ;; an identifier that doesn't end with a symbol token. | |
6201 | ;; That can occur e.g. for Java import directives on the | |
6202 | ;; form "foo.bar.*". | |
6203 | (when (and id-start (/= id-start id-end)) | |
d9e94c22 MS |
6204 | (setq c-last-identifier-range |
6205 | (cons id-start id-end))) | |
6206 | (goto-char id-end) | |
6207 | (c-forward-syntactic-ws) | |
6208 | (setq pos (point) | |
6209 | res t))) | |
6210 | ||
6211 | (progn | |
6212 | (goto-char pos) | |
6213 | (when (or c-opt-identifier-concat-key | |
6214 | c-recognize-<>-arglists) | |
6215 | ||
6216 | (cond | |
6217 | ((and c-opt-identifier-concat-key | |
6218 | (looking-at c-opt-identifier-concat-key)) | |
6219 | ;; Got a concatenated identifier. This handles the | |
6220 | ;; cases with tricky syntactic whitespace that aren't | |
6221 | ;; covered in `c-identifier-key'. | |
6222 | (goto-char (match-end 0)) | |
6223 | (c-forward-syntactic-ws) | |
6224 | t) | |
6225 | ||
6226 | ((and c-recognize-<>-arglists | |
6227 | (eq (char-after) ?<)) | |
6228 | ;; Maybe an angle bracket arglist. | |
452ea855 AM |
6229 | (when (let ((c-record-type-identifiers t) |
6230 | (c-record-found-types t)) | |
0386b551 AM |
6231 | (c-forward-<>-arglist nil)) |
6232 | ||
6233 | (c-add-type start (1+ pos)) | |
d9e94c22 | 6234 | (c-forward-syntactic-ws) |
0386b551 AM |
6235 | (setq pos (point) |
6236 | c-last-identifier-range nil) | |
6237 | ||
d9e94c22 MS |
6238 | (if (and c-opt-identifier-concat-key |
6239 | (looking-at c-opt-identifier-concat-key)) | |
0386b551 | 6240 | |
d9e94c22 MS |
6241 | ;; Continue if there's an identifier concatenation |
6242 | ;; operator after the template argument. | |
6243 | (progn | |
0386b551 AM |
6244 | (when (and c-record-type-identifiers id-start) |
6245 | (c-record-ref-id (cons id-start id-end))) | |
d9e94c22 MS |
6246 | (forward-char 2) |
6247 | (c-forward-syntactic-ws) | |
6248 | t) | |
0386b551 AM |
6249 | |
6250 | (when (and c-record-type-identifiers id-start) | |
6251 | (c-record-type-id (cons id-start id-end))) | |
d9e94c22 MS |
6252 | (setq res 'template) |
6253 | nil))) | |
6254 | ))))) | |
6255 | ||
6256 | (goto-char pos) | |
6257 | res)) | |
6258 | ||
e15f8aaa | 6259 | (defun c-forward-type (&optional brace-block-too) |
d9e94c22 | 6260 | ;; Move forward over a type spec if at the beginning of one, |
e15f8aaa AM |
6261 | ;; stopping at the next following token. The keyword "typedef" |
6262 | ;; isn't part of a type spec here. | |
6263 | ;; | |
6264 | ;; BRACE-BLOCK-TOO, when non-nil, means move over the brace block in | |
6265 | ;; constructs like "struct foo {...} bar ;" or "struct {...} bar;". | |
6266 | ;; The current (2009-03-10) intention is to convert all uses of | |
6267 | ;; `c-forward-type' to call with this parameter set, then to | |
6268 | ;; eliminate it. | |
6269 | ;; | |
6270 | ;; Return | |
6271 | ;; o - t if it's a known type that can't be a name or other | |
6272 | ;; expression; | |
6273 | ;; o - 'known if it's an otherwise known type (according to | |
6274 | ;; `*-font-lock-extra-types'); | |
6275 | ;; o - 'prefix if it's a known prefix of a type; | |
6276 | ;; o - 'found if it's a type that matches one in `c-found-types'; | |
53964682 | 6277 | ;; o - 'maybe if it's an identifier that might be a type; or |
e15f8aaa AM |
6278 | ;; o - nil if it can't be a type (the point isn't moved then). |
6279 | ;; | |
6280 | ;; The point is assumed to be at the beginning of a token. | |
d9e94c22 MS |
6281 | ;; |
6282 | ;; Note that this function doesn't skip past the brace definition | |
6283 | ;; that might be considered part of the type, e.g. | |
6284 | ;; "enum {a, b, c} foo". | |
0386b551 AM |
6285 | ;; |
6286 | ;; This function records identifier ranges on | |
6287 | ;; `c-record-type-identifiers' and `c-record-ref-identifiers' if | |
6288 | ;; `c-record-type-identifiers' is non-nil. | |
6289 | ;; | |
6290 | ;; This function might do hidden buffer changes. | |
0e5cf2b8 AM |
6291 | (when (and c-recognize-<>-arglists |
6292 | (looking-at "<")) | |
452ea855 AM |
6293 | (c-forward-<>-arglist t) |
6294 | (c-forward-syntactic-ws)) | |
0386b551 AM |
6295 | |
6296 | (let ((start (point)) pos res name-res id-start id-end id-range) | |
d9e94c22 MS |
6297 | |
6298 | ;; Skip leading type modifiers. If any are found we know it's a | |
6299 | ;; prefix of a type. | |
e15f8aaa | 6300 | (when c-opt-type-modifier-key ; e.g. "const" "volatile", but NOT "typedef" |
d9e94c22 MS |
6301 | (while (looking-at c-opt-type-modifier-key) |
6302 | (goto-char (match-end 1)) | |
6303 | (c-forward-syntactic-ws) | |
6304 | (setq res 'prefix))) | |
6305 | ||
6306 | (cond | |
e15f8aaa AM |
6307 | ((looking-at c-type-prefix-key) ; e.g. "struct", "class", but NOT |
6308 | ; "typedef". | |
d9e94c22 MS |
6309 | (goto-char (match-end 1)) |
6310 | (c-forward-syntactic-ws) | |
6311 | (setq pos (point)) | |
e15f8aaa AM |
6312 | |
6313 | (setq name-res (c-forward-name)) | |
6314 | (setq res (not (null name-res))) | |
6315 | (when (eq name-res t) | |
6316 | ;; In many languages the name can be used without the | |
6317 | ;; prefix, so we add it to `c-found-types'. | |
6318 | (c-add-type pos (point)) | |
6319 | (when (and c-record-type-identifiers | |
6320 | c-last-identifier-range) | |
6321 | (c-record-type-id c-last-identifier-range))) | |
6322 | (when (and brace-block-too | |
6323 | (memq res '(t nil)) | |
6324 | (eq (char-after) ?\{) | |
6325 | (save-excursion | |
6326 | (c-safe | |
6327 | (progn (c-forward-sexp) | |
6328 | (c-forward-syntactic-ws) | |
6329 | (setq pos (point)))))) | |
6330 | (goto-char pos) | |
6331 | (setq res t)) | |
6332 | (unless res (goto-char start))) ; invalid syntax | |
d9e94c22 MS |
6333 | |
6334 | ((progn | |
6335 | (setq pos nil) | |
6336 | (if (looking-at c-identifier-start) | |
6337 | (save-excursion | |
6338 | (setq id-start (point) | |
0386b551 AM |
6339 | name-res (c-forward-name)) |
6340 | (when name-res | |
d9e94c22 MS |
6341 | (setq id-end (point) |
6342 | id-range c-last-identifier-range)))) | |
6343 | (and (cond ((looking-at c-primitive-type-key) | |
6344 | (setq res t)) | |
6345 | ((c-with-syntax-table c-identifier-syntax-table | |
6346 | (looking-at c-known-type-key)) | |
6347 | (setq res 'known))) | |
6348 | (or (not id-end) | |
6349 | (>= (save-excursion | |
6350 | (save-match-data | |
6351 | (goto-char (match-end 1)) | |
6352 | (c-forward-syntactic-ws) | |
6353 | (setq pos (point)))) | |
6354 | id-end) | |
6355 | (setq res nil)))) | |
6356 | ;; Looking at a primitive or known type identifier. We've | |
6357 | ;; checked for a name first so that we don't go here if the | |
6358 | ;; known type match only is a prefix of another name. | |
6359 | ||
6360 | (setq id-end (match-end 1)) | |
6361 | ||
6362 | (when (and c-record-type-identifiers | |
6363 | (or c-promote-possible-types (eq res t))) | |
6364 | (c-record-type-id (cons (match-beginning 1) (match-end 1)))) | |
6365 | ||
6366 | (if (and c-opt-type-component-key | |
6367 | (save-match-data | |
6368 | (looking-at c-opt-type-component-key))) | |
6369 | ;; There might be more keywords for the type. | |
6370 | (let (safe-pos) | |
0386b551 | 6371 | (c-forward-keyword-clause 1) |
d9e94c22 MS |
6372 | (while (progn |
6373 | (setq safe-pos (point)) | |
6374 | (looking-at c-opt-type-component-key)) | |
6375 | (when (and c-record-type-identifiers | |
6376 | (looking-at c-primitive-type-key)) | |
6377 | (c-record-type-id (cons (match-beginning 1) | |
6378 | (match-end 1)))) | |
0386b551 | 6379 | (c-forward-keyword-clause 1)) |
d9e94c22 MS |
6380 | (if (looking-at c-primitive-type-key) |
6381 | (progn | |
6382 | (when c-record-type-identifiers | |
6383 | (c-record-type-id (cons (match-beginning 1) | |
6384 | (match-end 1)))) | |
0386b551 | 6385 | (c-forward-keyword-clause 1) |
d9e94c22 MS |
6386 | (setq res t)) |
6387 | (goto-char safe-pos) | |
6388 | (setq res 'prefix))) | |
0386b551 | 6389 | (unless (save-match-data (c-forward-keyword-clause 1)) |
d9e94c22 MS |
6390 | (if pos |
6391 | (goto-char pos) | |
6392 | (goto-char (match-end 1)) | |
6393 | (c-forward-syntactic-ws))))) | |
6394 | ||
0386b551 AM |
6395 | (name-res |
6396 | (cond ((eq name-res t) | |
d9e94c22 MS |
6397 | ;; A normal identifier. |
6398 | (goto-char id-end) | |
6399 | (if (or res c-promote-possible-types) | |
6400 | (progn | |
6401 | (c-add-type id-start id-end) | |
0386b551 | 6402 | (when (and c-record-type-identifiers id-range) |
d9e94c22 MS |
6403 | (c-record-type-id id-range)) |
6404 | (unless res | |
6405 | (setq res 'found))) | |
6406 | (setq res (if (c-check-type id-start id-end) | |
6407 | ;; It's an identifier that has been used as | |
6408 | ;; a type somewhere else. | |
6409 | 'found | |
6410 | ;; It's an identifier that might be a type. | |
6411 | 'maybe)))) | |
0386b551 | 6412 | ((eq name-res 'template) |
d9e94c22 MS |
6413 | ;; A template is a type. |
6414 | (goto-char id-end) | |
6415 | (setq res t)) | |
6416 | (t | |
6417 | ;; Otherwise it's an operator identifier, which is not a type. | |
6418 | (goto-char start) | |
6419 | (setq res nil))))) | |
6420 | ||
6421 | (when res | |
e15f8aaa | 6422 | ;; Skip trailing type modifiers. If any are found we know it's |
d9e94c22 MS |
6423 | ;; a type. |
6424 | (when c-opt-type-modifier-key | |
e15f8aaa | 6425 | (while (looking-at c-opt-type-modifier-key) ; e.g. "const", "volatile" |
d9e94c22 MS |
6426 | (goto-char (match-end 1)) |
6427 | (c-forward-syntactic-ws) | |
6428 | (setq res t))) | |
d9e94c22 MS |
6429 | ;; Step over any type suffix operator. Do not let the existence |
6430 | ;; of these alter the classification of the found type, since | |
6431 | ;; these operators typically are allowed in normal expressions | |
6432 | ;; too. | |
6433 | (when c-opt-type-suffix-key | |
6434 | (while (looking-at c-opt-type-suffix-key) | |
6435 | (goto-char (match-end 1)) | |
6436 | (c-forward-syntactic-ws))) | |
6437 | ||
e15f8aaa | 6438 | (when c-opt-type-concat-key ; Only/mainly for pike. |
0386b551 AM |
6439 | ;; Look for a trailing operator that concatenates the type |
6440 | ;; with a following one, and if so step past that one through | |
6441 | ;; a recursive call. Note that we don't record concatenated | |
6442 | ;; types in `c-found-types' - it's the component types that | |
6443 | ;; are recorded when appropriate. | |
d9e94c22 MS |
6444 | (setq pos (point)) |
6445 | (let* ((c-promote-possible-types (or (memq res '(t known)) | |
6446 | c-promote-possible-types)) | |
6447 | ;; If we can't promote then set `c-record-found-types' so that | |
6448 | ;; we can merge in the types from the second part afterwards if | |
6449 | ;; it turns out to be a known type there. | |
6450 | (c-record-found-types (and c-record-type-identifiers | |
0386b551 AM |
6451 | (not c-promote-possible-types))) |
6452 | subres) | |
d9e94c22 MS |
6453 | (if (and (looking-at c-opt-type-concat-key) |
6454 | ||
6455 | (progn | |
6456 | (goto-char (match-end 1)) | |
6457 | (c-forward-syntactic-ws) | |
0386b551 | 6458 | (setq subres (c-forward-type)))) |
d9e94c22 MS |
6459 | |
6460 | (progn | |
6461 | ;; If either operand certainly is a type then both are, but we | |
6462 | ;; don't let the existence of the operator itself promote two | |
6463 | ;; uncertain types to a certain one. | |
6464 | (cond ((eq res t)) | |
0386b551 AM |
6465 | ((eq subres t) |
6466 | (unless (eq name-res 'template) | |
6467 | (c-add-type id-start id-end)) | |
6468 | (when (and c-record-type-identifiers id-range) | |
d9e94c22 MS |
6469 | (c-record-type-id id-range)) |
6470 | (setq res t)) | |
2a15eb73 | 6471 | ((eq res 'known)) |
0386b551 | 6472 | ((eq subres 'known) |
2a15eb73 | 6473 | (setq res 'known)) |
d9e94c22 | 6474 | ((eq res 'found)) |
0386b551 | 6475 | ((eq subres 'found) |
d9e94c22 MS |
6476 | (setq res 'found)) |
6477 | (t | |
6478 | (setq res 'maybe))) | |
6479 | ||
6480 | (when (and (eq res t) | |
6481 | (consp c-record-found-types)) | |
6482 | ;; Merge in the ranges of any types found by the second | |
6483 | ;; `c-forward-type'. | |
6484 | (setq c-record-type-identifiers | |
6485 | ;; `nconc' doesn't mind that the tail of | |
6486 | ;; `c-record-found-types' is t. | |
6487 | (nconc c-record-found-types | |
6488 | c-record-type-identifiers)))) | |
6489 | ||
6490 | (goto-char pos)))) | |
6491 | ||
6492 | (when (and c-record-found-types (memq res '(known found)) id-range) | |
6493 | (setq c-record-found-types | |
6494 | (cons id-range c-record-found-types)))) | |
6495 | ||
6496 | ;;(message "c-forward-type %s -> %s: %s" start (point) res) | |
6497 | ||
6498 | res)) | |
6499 | ||
452ea855 AM |
6500 | (defun c-forward-annotation () |
6501 | ;; Used for Java code only at the moment. Assumes point is on the | |
6502 | ;; @, moves forward an annotation. returns nil if there is no | |
6503 | ;; annotation at point. | |
6504 | (and (looking-at "@") | |
6505 | (progn (forward-char) t) | |
6506 | (c-forward-type) | |
6507 | (progn (c-forward-syntactic-ws) t) | |
6508 | (if (looking-at "(") | |
6509 | (c-go-list-forward) | |
6510 | t))) | |
6511 | ||
785eecbb | 6512 | \f |
d9e94c22 MS |
6513 | ;; Handling of large scale constructs like statements and declarations. |
6514 | ||
0386b551 AM |
6515 | ;; Macro used inside `c-forward-decl-or-cast-1'. It ought to be a |
6516 | ;; defsubst or perhaps even a defun, but it contains lots of free | |
6517 | ;; variables that refer to things inside `c-forward-decl-or-cast-1'. | |
6518 | (defmacro c-fdoc-shift-type-backward (&optional short) | |
6519 | ;; `c-forward-decl-or-cast-1' can consume an arbitrary length list | |
6520 | ;; of types when parsing a declaration, which means that it | |
6521 | ;; sometimes consumes the identifier in the declaration as a type. | |
6522 | ;; This is used to "backtrack" and make the last type be treated as | |
6523 | ;; an identifier instead. | |
6524 | `(progn | |
6525 | ,(unless short | |
6526 | ;; These identifiers are bound only in the inner let. | |
6527 | '(setq identifier-type at-type | |
6528 | identifier-start type-start | |
6529 | got-parens nil | |
6530 | got-identifier t | |
6531 | got-suffix t | |
6532 | got-suffix-after-parens id-start | |
6533 | paren-depth 0)) | |
6534 | ||
6535 | (if (setq at-type (if (eq backup-at-type 'prefix) | |
6536 | t | |
6537 | backup-at-type)) | |
6538 | (setq type-start backup-type-start | |
6539 | id-start backup-id-start) | |
6540 | (setq type-start start-pos | |
6541 | id-start start-pos)) | |
6542 | ||
6543 | ;; When these flags already are set we've found specifiers that | |
6544 | ;; unconditionally signal these attributes - backtracking doesn't | |
6545 | ;; change that. So keep them set in that case. | |
6546 | (or at-type-decl | |
6547 | (setq at-type-decl backup-at-type-decl)) | |
6548 | (or maybe-typeless | |
6549 | (setq maybe-typeless backup-maybe-typeless)) | |
6550 | ||
6551 | ,(unless short | |
6552 | ;; This identifier is bound only in the inner let. | |
6553 | '(setq start id-start)))) | |
6554 | ||
6555 | (defun c-forward-decl-or-cast-1 (preceding-token-end context last-cast-end) | |
6556 | ;; Move forward over a declaration or a cast if at the start of one. | |
6557 | ;; The point is assumed to be at the start of some token. Nil is | |
6558 | ;; returned if no declaration or cast is recognized, and the point | |
6559 | ;; is clobbered in that case. | |
6560 | ;; | |
6561 | ;; If a declaration is parsed: | |
6562 | ;; | |
6563 | ;; The point is left at the first token after the first complete | |
6564 | ;; declarator, if there is one. The return value is a cons where | |
022d0cf4 AM |
6565 | ;; the car is the position of the first token in the declarator. (See |
6566 | ;; below for the cdr.) | |
0386b551 AM |
6567 | ;; Some examples: |
6568 | ;; | |
6569 | ;; void foo (int a, char *b) stuff ... | |
6570 | ;; car ^ ^ point | |
6571 | ;; float (*a)[], b; | |
6572 | ;; car ^ ^ point | |
6573 | ;; unsigned int a = c_style_initializer, b; | |
6574 | ;; car ^ ^ point | |
6575 | ;; unsigned int a (cplusplus_style_initializer), b; | |
6576 | ;; car ^ ^ point (might change) | |
6577 | ;; class Foo : public Bar {} | |
6578 | ;; car ^ ^ point | |
6579 | ;; class PikeClass (int a, string b) stuff ... | |
6580 | ;; car ^ ^ point | |
6581 | ;; enum bool; | |
6582 | ;; car ^ ^ point | |
6583 | ;; enum bool flag; | |
6584 | ;; car ^ ^ point | |
6585 | ;; void cplusplus_function (int x) throw (Bad); | |
6586 | ;; car ^ ^ point | |
6587 | ;; Foo::Foo (int b) : Base (b) {} | |
6588 | ;; car ^ ^ point | |
91af3942 | 6589 | ;; |
e15f8aaa AM |
6590 | ;; The cdr of the return value is non-nil when a |
6591 | ;; `c-typedef-decl-kwds' specifier is found in the declaration. | |
6592 | ;; Specifically it is a dotted pair (A . B) where B is t when a | |
6593 | ;; `c-typedef-kwds' ("typedef") is present, and A is t when some | |
6594 | ;; other `c-typedef-decl-kwds' (e.g. class, struct, enum) | |
6595 | ;; specifier is present. I.e., (some of) the declared | |
6596 | ;; identifier(s) are types. | |
91af3942 | 6597 | ;; |
0386b551 AM |
6598 | ;; If a cast is parsed: |
6599 | ;; | |
6600 | ;; The point is left at the first token after the closing paren of | |
6601 | ;; the cast. The return value is `cast'. Note that the start | |
6602 | ;; position must be at the first token inside the cast parenthesis | |
6603 | ;; to recognize it. | |
6604 | ;; | |
6605 | ;; PRECEDING-TOKEN-END is the first position after the preceding | |
6606 | ;; token, i.e. on the other side of the syntactic ws from the point. | |
6607 | ;; Use a value less than or equal to (point-min) if the point is at | |
6608 | ;; the first token in (the visible part of) the buffer. | |
6609 | ;; | |
6610 | ;; CONTEXT is a symbol that describes the context at the point: | |
022d0cf4 | 6611 | ;; 'decl In a comma-separated declaration context (typically |
0386b551 AM |
6612 | ;; inside a function declaration arglist). |
6613 | ;; '<> In an angle bracket arglist. | |
6614 | ;; 'arglist Some other type of arglist. | |
a85fd6da AM |
6615 | ;; nil Some other context or unknown context. Includes |
6616 | ;; within the parens of an if, for, ... construct. | |
0386b551 AM |
6617 | ;; |
6618 | ;; LAST-CAST-END is the first token after the closing paren of a | |
6619 | ;; preceding cast, or nil if none is known. If | |
6620 | ;; `c-forward-decl-or-cast-1' is used in succession, it should be | |
6621 | ;; the position after the closest preceding call where a cast was | |
6622 | ;; matched. In that case it's used to discover chains of casts like | |
6623 | ;; "(a) (b) c". | |
6624 | ;; | |
6625 | ;; This function records identifier ranges on | |
6626 | ;; `c-record-type-identifiers' and `c-record-ref-identifiers' if | |
6627 | ;; `c-record-type-identifiers' is non-nil. | |
6628 | ;; | |
6629 | ;; This function might do hidden buffer changes. | |
6630 | ||
6631 | (let (;; `start-pos' is used below to point to the start of the | |
6632 | ;; first type, i.e. after any leading specifiers. It might | |
6633 | ;; also point at the beginning of the preceding syntactic | |
6634 | ;; whitespace. | |
6635 | (start-pos (point)) | |
6636 | ;; Set to the result of `c-forward-type'. | |
6637 | at-type | |
6638 | ;; The position of the first token in what we currently | |
6639 | ;; believe is the type in the declaration or cast, after any | |
6640 | ;; specifiers and their associated clauses. | |
6641 | type-start | |
6642 | ;; The position of the first token in what we currently | |
6643 | ;; believe is the declarator for the first identifier. Set | |
6644 | ;; when the type is found, and moved forward over any | |
6645 | ;; `c-decl-hangon-kwds' and their associated clauses that | |
6646 | ;; occurs after the type. | |
6647 | id-start | |
6648 | ;; These store `at-type', `type-start' and `id-start' of the | |
6649 | ;; identifier before the one in those variables. The previous | |
6650 | ;; identifier might turn out to be the real type in a | |
6651 | ;; declaration if the last one has to be the declarator in it. | |
6652 | ;; If `backup-at-type' is nil then the other variables have | |
6653 | ;; undefined values. | |
6654 | backup-at-type backup-type-start backup-id-start | |
e15f8aaa AM |
6655 | ;; Set if we've found a specifier (apart from "typedef") that makes |
6656 | ;; the defined identifier(s) types. | |
0386b551 | 6657 | at-type-decl |
e15f8aaa AM |
6658 | ;; Set if we've a "typedef" keyword. |
6659 | at-typedef | |
0386b551 AM |
6660 | ;; Set if we've found a specifier that can start a declaration |
6661 | ;; where there's no type. | |
6662 | maybe-typeless | |
6663 | ;; If a specifier is found that also can be a type prefix, | |
6664 | ;; these flags are set instead of those above. If we need to | |
6665 | ;; back up an identifier, they are copied to the real flag | |
6666 | ;; variables. Thus they only take effect if we fail to | |
6667 | ;; interpret it as a type. | |
6668 | backup-at-type-decl backup-maybe-typeless | |
6669 | ;; Whether we've found a declaration or a cast. We might know | |
6670 | ;; this before we've found the type in it. It's 'ids if we've | |
6671 | ;; found two consecutive identifiers (usually a sure sign, but | |
6672 | ;; we should allow that in labels too), and t if we've found a | |
6673 | ;; specifier keyword (a 100% sure sign). | |
6674 | at-decl-or-cast | |
6675 | ;; Set when we need to back up to parse this as a declaration | |
6676 | ;; but not as a cast. | |
6677 | backup-if-not-cast | |
6678 | ;; For casts, the return position. | |
6679 | cast-end | |
6680 | ;; Save `c-record-type-identifiers' and | |
6681 | ;; `c-record-ref-identifiers' since ranges are recorded | |
6682 | ;; speculatively and should be thrown away if it turns out | |
6683 | ;; that it isn't a declaration or cast. | |
6684 | (save-rec-type-ids c-record-type-identifiers) | |
6685 | (save-rec-ref-ids c-record-ref-identifiers)) | |
6686 | ||
452ea855 AM |
6687 | (while (c-forward-annotation) |
6688 | (c-forward-syntactic-ws)) | |
6689 | ||
0386b551 AM |
6690 | ;; Check for a type. Unknown symbols are treated as possible |
6691 | ;; types, but they could also be specifiers disguised through | |
6692 | ;; macros like __INLINE__, so we recognize both types and known | |
6693 | ;; specifiers after them too. | |
6694 | (while | |
6695 | (let* ((start (point)) kwd-sym kwd-clause-end found-type) | |
6696 | ||
6697 | ;; Look for a specifier keyword clause. | |
e3715033 AM |
6698 | (when (or (looking-at c-prefix-spec-kwds-re) |
6699 | (and (c-major-mode-is 'java-mode) | |
6700 | (looking-at "@[A-Za-z0-9]+"))) | |
e15f8aaa AM |
6701 | (if (looking-at c-typedef-key) |
6702 | (setq at-typedef t)) | |
0386b551 AM |
6703 | (setq kwd-sym (c-keyword-sym (match-string 1))) |
6704 | (save-excursion | |
6705 | (c-forward-keyword-clause 1) | |
6706 | (setq kwd-clause-end (point)))) | |
6707 | ||
e15f8aaa | 6708 | (when (setq found-type (c-forward-type t)) ; brace-block-too |
0386b551 AM |
6709 | ;; Found a known or possible type or a prefix of a known type. |
6710 | ||
6711 | (when at-type | |
6712 | ;; Got two identifiers with nothing but whitespace | |
6713 | ;; between them. That can only happen in declarations. | |
6714 | (setq at-decl-or-cast 'ids) | |
6715 | ||
6716 | (when (eq at-type 'found) | |
6717 | ;; If the previous identifier is a found type we | |
6718 | ;; record it as a real one; it might be some sort of | |
6719 | ;; alias for a prefix like "unsigned". | |
6720 | (save-excursion | |
6721 | (goto-char type-start) | |
6722 | (let ((c-promote-possible-types t)) | |
6723 | (c-forward-type))))) | |
6724 | ||
6725 | (setq backup-at-type at-type | |
6726 | backup-type-start type-start | |
6727 | backup-id-start id-start | |
6728 | at-type found-type | |
6729 | type-start start | |
6730 | id-start (point) | |
6731 | ;; The previous ambiguous specifier/type turned out | |
6732 | ;; to be a type since we've parsed another one after | |
6733 | ;; it, so clear these backup flags. | |
6734 | backup-at-type-decl nil | |
6735 | backup-maybe-typeless nil)) | |
6736 | ||
6737 | (if kwd-sym | |
6738 | (progn | |
6739 | ;; Handle known specifier keywords and | |
6740 | ;; `c-decl-hangon-kwds' which can occur after known | |
6741 | ;; types. | |
6742 | ||
6743 | (if (c-keyword-member kwd-sym 'c-decl-hangon-kwds) | |
6744 | ;; It's a hang-on keyword that can occur anywhere. | |
6745 | (progn | |
6746 | (setq at-decl-or-cast t) | |
6747 | (if at-type | |
6748 | ;; Move the identifier start position if | |
6749 | ;; we've passed a type. | |
6750 | (setq id-start kwd-clause-end) | |
6751 | ;; Otherwise treat this as a specifier and | |
6752 | ;; move the fallback position. | |
6753 | (setq start-pos kwd-clause-end)) | |
6754 | (goto-char kwd-clause-end)) | |
6755 | ||
6756 | ;; It's an ordinary specifier so we know that | |
6757 | ;; anything before this can't be the type. | |
6758 | (setq backup-at-type nil | |
6759 | start-pos kwd-clause-end) | |
6760 | ||
6761 | (if found-type | |
6762 | ;; It's ambiguous whether this keyword is a | |
6763 | ;; specifier or a type prefix, so set the backup | |
6764 | ;; flags. (It's assumed that `c-forward-type' | |
6765 | ;; moved further than `c-forward-keyword-clause'.) | |
6766 | (progn | |
6767 | (when (c-keyword-member kwd-sym 'c-typedef-decl-kwds) | |
6768 | (setq backup-at-type-decl t)) | |
6769 | (when (c-keyword-member kwd-sym 'c-typeless-decl-kwds) | |
6770 | (setq backup-maybe-typeless t))) | |
6771 | ||
6772 | (when (c-keyword-member kwd-sym 'c-typedef-decl-kwds) | |
e15f8aaa AM |
6773 | ;; This test only happens after we've scanned a type. |
6774 | ;; So, with valid syntax, kwd-sym can't be 'typedef. | |
0386b551 AM |
6775 | (setq at-type-decl t)) |
6776 | (when (c-keyword-member kwd-sym 'c-typeless-decl-kwds) | |
6777 | (setq maybe-typeless t)) | |
6778 | ||
ee7683eb | 6779 | ;; Haven't matched a type so it's an unambiguous |
0386b551 AM |
6780 | ;; specifier keyword and we know we're in a |
6781 | ;; declaration. | |
6782 | (setq at-decl-or-cast t) | |
6783 | ||
6784 | (goto-char kwd-clause-end)))) | |
6785 | ||
6786 | ;; If the type isn't known we continue so that we'll jump | |
6787 | ;; over all specifiers and type identifiers. The reason | |
6788 | ;; to do this for a known type prefix is to make things | |
6789 | ;; like "unsigned INT16" work. | |
6790 | (and found-type (not (eq found-type t)))))) | |
6791 | ||
6792 | (cond | |
6793 | ((eq at-type t) | |
6794 | ;; If a known type was found, we still need to skip over any | |
6795 | ;; hangon keyword clauses after it. Otherwise it has already | |
6796 | ;; been done in the loop above. | |
6797 | (while (looking-at c-decl-hangon-key) | |
6798 | (c-forward-keyword-clause 1)) | |
6799 | (setq id-start (point))) | |
6800 | ||
6801 | ((eq at-type 'prefix) | |
6802 | ;; A prefix type is itself a primitive type when it's not | |
6803 | ;; followed by another type. | |
6804 | (setq at-type t)) | |
6805 | ||
6806 | ((not at-type) | |
6807 | ;; Got no type but set things up to continue anyway to handle | |
6808 | ;; the various cases when a declaration doesn't start with a | |
6809 | ;; type. | |
6810 | (setq id-start start-pos)) | |
6811 | ||
6812 | ((and (eq at-type 'maybe) | |
6813 | (c-major-mode-is 'c++-mode)) | |
6814 | ;; If it's C++ then check if the last "type" ends on the form | |
6815 | ;; "foo::foo" or "foo::~foo", i.e. if it's the name of a | |
6816 | ;; (con|de)structor. | |
6817 | (save-excursion | |
6818 | (let (name end-2 end-1) | |
6819 | (goto-char id-start) | |
6820 | (c-backward-syntactic-ws) | |
6821 | (setq end-2 (point)) | |
6822 | (when (and | |
6823 | (c-simple-skip-symbol-backward) | |
6824 | (progn | |
6825 | (setq name | |
6826 | (buffer-substring-no-properties (point) end-2)) | |
6827 | ;; Cheating in the handling of syntactic ws below. | |
6828 | (< (skip-chars-backward ":~ \t\n\r\v\f") 0)) | |
6829 | (progn | |
6830 | (setq end-1 (point)) | |
6831 | (c-simple-skip-symbol-backward)) | |
6832 | (>= (point) type-start) | |
6833 | (equal (buffer-substring-no-properties (point) end-1) | |
6834 | name)) | |
6835 | ;; It is a (con|de)structor name. In that case the | |
6836 | ;; declaration is typeless so zap out any preceding | |
6837 | ;; identifier(s) that we might have taken as types. | |
6838 | (goto-char type-start) | |
6839 | (setq at-type nil | |
6840 | backup-at-type nil | |
6841 | id-start type-start)))))) | |
6842 | ||
6843 | ;; Check for and step over a type decl expression after the thing | |
6844 | ;; that is or might be a type. This can't be skipped since we | |
6845 | ;; need the correct end position of the declarator for | |
6846 | ;; `max-type-decl-end-*'. | |
6847 | (let ((start (point)) (paren-depth 0) pos | |
6848 | ;; True if there's a non-open-paren match of | |
6849 | ;; `c-type-decl-prefix-key'. | |
6850 | got-prefix | |
6851 | ;; True if the declarator is surrounded by a parenthesis pair. | |
6852 | got-parens | |
6853 | ;; True if there is an identifier in the declarator. | |
6854 | got-identifier | |
6855 | ;; True if there's a non-close-paren match of | |
6856 | ;; `c-type-decl-suffix-key'. | |
6857 | got-suffix | |
6858 | ;; True if there's a prefix match outside the outermost | |
6859 | ;; paren pair that surrounds the declarator. | |
6860 | got-prefix-before-parens | |
1379f2c5 | 6861 | ;; True if there's a suffix match outside the outermost |
0386b551 AM |
6862 | ;; paren pair that surrounds the declarator. The value is |
6863 | ;; the position of the first suffix match. | |
6864 | got-suffix-after-parens | |
6865 | ;; True if we've parsed the type decl to a token that is | |
6866 | ;; known to end declarations in this context. | |
6867 | at-decl-end | |
6868 | ;; The earlier values of `at-type' and `type-start' if we've | |
6869 | ;; shifted the type backwards. | |
6870 | identifier-type identifier-start | |
6871 | ;; If `c-parse-and-markup-<>-arglists' is set we need to | |
6872 | ;; turn it off during the name skipping below to avoid | |
6873 | ;; getting `c-type' properties that might be bogus. That | |
6874 | ;; can happen since we don't know if | |
6875 | ;; `c-restricted-<>-arglists' will be correct inside the | |
6876 | ;; arglist paren that gets entered. | |
6877 | c-parse-and-markup-<>-arglists) | |
6878 | ||
6879 | (goto-char id-start) | |
6880 | ||
6881 | ;; Skip over type decl prefix operators. (Note similar code in | |
6882 | ;; `c-font-lock-declarators'.) | |
6883 | (while (and (looking-at c-type-decl-prefix-key) | |
6884 | (if (and (c-major-mode-is 'c++-mode) | |
05842630 | 6885 | (match-beginning 3)) |
0386b551 AM |
6886 | ;; If the second submatch matches in C++ then |
6887 | ;; we're looking at an identifier that's a | |
6888 | ;; prefix only if it specifies a member pointer. | |
6889 | (when (setq got-identifier (c-forward-name)) | |
6890 | (if (looking-at "\\(::\\)") | |
6891 | ;; We only check for a trailing "::" and | |
6892 | ;; let the "*" that should follow be | |
6893 | ;; matched in the next round. | |
6894 | (progn (setq got-identifier nil) t) | |
6895 | ;; It turned out to be the real identifier, | |
6896 | ;; so stop. | |
6897 | nil)) | |
6898 | t)) | |
6899 | ||
6900 | (if (eq (char-after) ?\() | |
6901 | (progn | |
6902 | (setq paren-depth (1+ paren-depth)) | |
6903 | (forward-char)) | |
6904 | (unless got-prefix-before-parens | |
6905 | (setq got-prefix-before-parens (= paren-depth 0))) | |
6906 | (setq got-prefix t) | |
6907 | (goto-char (match-end 1))) | |
6908 | (c-forward-syntactic-ws)) | |
6909 | ||
6910 | (setq got-parens (> paren-depth 0)) | |
6911 | ||
6912 | ;; Skip over an identifier. | |
6913 | (or got-identifier | |
6914 | (and (looking-at c-identifier-start) | |
6915 | (setq got-identifier (c-forward-name)))) | |
6916 | ||
6917 | ;; Skip over type decl suffix operators. | |
6918 | (while (if (looking-at c-type-decl-suffix-key) | |
6919 | ||
6920 | (if (eq (char-after) ?\)) | |
6921 | (when (> paren-depth 0) | |
6922 | (setq paren-depth (1- paren-depth)) | |
6923 | (forward-char) | |
6924 | t) | |
6925 | (when (if (save-match-data (looking-at "\\s\(")) | |
6926 | (c-safe (c-forward-sexp 1) t) | |
6927 | (goto-char (match-end 1)) | |
6928 | t) | |
6929 | (when (and (not got-suffix-after-parens) | |
6930 | (= paren-depth 0)) | |
6931 | (setq got-suffix-after-parens (match-beginning 0))) | |
6932 | (setq got-suffix t))) | |
6933 | ||
6934 | ;; No suffix matched. We might have matched the | |
6935 | ;; identifier as a type and the open paren of a | |
6936 | ;; function arglist as a type decl prefix. In that | |
6937 | ;; case we should "backtrack": Reinterpret the last | |
6938 | ;; type as the identifier, move out of the arglist and | |
6939 | ;; continue searching for suffix operators. | |
6940 | ;; | |
6941 | ;; Do this even if there's no preceding type, to cope | |
6942 | ;; with old style function declarations in K&R C, | |
6943 | ;; (con|de)structors in C++ and `c-typeless-decl-kwds' | |
6944 | ;; style declarations. That isn't applicable in an | |
6945 | ;; arglist context, though. | |
6946 | (when (and (= paren-depth 1) | |
6947 | (not got-prefix-before-parens) | |
6948 | (not (eq at-type t)) | |
6949 | (or backup-at-type | |
6950 | maybe-typeless | |
6951 | backup-maybe-typeless | |
6952 | (when c-recognize-typeless-decls | |
6953 | (not context))) | |
6954 | (setq pos (c-up-list-forward (point))) | |
6955 | (eq (char-before pos) ?\))) | |
6956 | (c-fdoc-shift-type-backward) | |
6957 | (goto-char pos) | |
6958 | t)) | |
6959 | ||
6960 | (c-forward-syntactic-ws)) | |
6961 | ||
6962 | (when (and (or maybe-typeless backup-maybe-typeless) | |
6963 | (not got-identifier) | |
6964 | (not got-prefix) | |
6965 | at-type) | |
6966 | ;; Have found no identifier but `c-typeless-decl-kwds' has | |
6967 | ;; matched so we know we're inside a declaration. The | |
6968 | ;; preceding type must be the identifier instead. | |
6969 | (c-fdoc-shift-type-backward)) | |
6970 | ||
6971 | (setq | |
6972 | at-decl-or-cast | |
6973 | (catch 'at-decl-or-cast | |
6974 | ||
a85fd6da | 6975 | ;; CASE 1 |
0386b551 AM |
6976 | (when (> paren-depth 0) |
6977 | ;; Encountered something inside parens that isn't matched by | |
6978 | ;; the `c-type-decl-*' regexps, so it's not a type decl | |
6979 | ;; expression. Try to skip out to the same paren depth to | |
6980 | ;; not confuse the cast check below. | |
6981 | (c-safe (goto-char (scan-lists (point) 1 paren-depth))) | |
6982 | ;; If we've found a specifier keyword then it's a | |
6983 | ;; declaration regardless. | |
6984 | (throw 'at-decl-or-cast (eq at-decl-or-cast t))) | |
6985 | ||
6986 | (setq at-decl-end | |
6987 | (looking-at (cond ((eq context '<>) "[,>]") | |
6988 | (context "[,\)]") | |
6989 | (t "[,;]")))) | |
6990 | ||
6991 | ;; Now we've collected info about various characteristics of | |
6992 | ;; the construct we're looking at. Below follows a decision | |
6993 | ;; tree based on that. It's ordered to check more certain | |
6994 | ;; signs before less certain ones. | |
6995 | ||
6996 | (if got-identifier | |
6997 | (progn | |
6998 | ||
a85fd6da | 6999 | ;; CASE 2 |
0386b551 AM |
7000 | (when (and (or at-type maybe-typeless) |
7001 | (not (or got-prefix got-parens))) | |
7002 | ;; Got another identifier directly after the type, so it's a | |
7003 | ;; declaration. | |
7004 | (throw 'at-decl-or-cast t)) | |
7005 | ||
7006 | (when (and got-parens | |
7007 | (not got-prefix) | |
7008 | (not got-suffix-after-parens) | |
7009 | (or backup-at-type | |
7010 | maybe-typeless | |
7011 | backup-maybe-typeless)) | |
7012 | ;; Got a declaration of the form "foo bar (gnu);" where we've | |
7013 | ;; recognized "bar" as the type and "gnu" as the declarator. | |
7014 | ;; In this case it's however more likely that "bar" is the | |
7015 | ;; declarator and "gnu" a function argument or initializer (if | |
7016 | ;; `c-recognize-paren-inits' is set), since the parens around | |
7017 | ;; "gnu" would be superfluous if it's a declarator. Shift the | |
7018 | ;; type one step backward. | |
7019 | (c-fdoc-shift-type-backward))) | |
7020 | ||
7021 | ;; Found no identifier. | |
7022 | ||
7023 | (if backup-at-type | |
7024 | (progn | |
7025 | ||
b248a85d AM |
7026 | |
7027 | ;; CASE 3 | |
7028 | (when (= (point) start) | |
7029 | ;; Got a plain list of identifiers. If a colon follows it's | |
7030 | ;; a valid label, or maybe a bitfield. Otherwise the last | |
7031 | ;; one probably is the declared identifier and we should | |
7032 | ;; back up to the previous type, providing it isn't a cast. | |
452ea855 AM |
7033 | (if (and (eq (char-after) ?:) |
7034 | (not (c-major-mode-is 'java-mode))) | |
b248a85d AM |
7035 | (cond |
7036 | ;; If we've found a specifier keyword then it's a | |
7037 | ;; declaration regardless. | |
7038 | ((eq at-decl-or-cast t) | |
7039 | (throw 'at-decl-or-cast t)) | |
7040 | ((and c-has-bitfields | |
7041 | (eq at-decl-or-cast 'ids)) ; bitfield. | |
7042 | (setq backup-if-not-cast t) | |
7043 | (throw 'at-decl-or-cast t))) | |
7044 | ||
7045 | (setq backup-if-not-cast t) | |
7046 | (throw 'at-decl-or-cast t))) | |
0386b551 | 7047 | |
a85fd6da | 7048 | ;; CASE 4 |
0386b551 AM |
7049 | (when (and got-suffix |
7050 | (not got-prefix) | |
7051 | (not got-parens)) | |
7052 | ;; Got a plain list of identifiers followed by some suffix. | |
7053 | ;; If this isn't a cast then the last identifier probably is | |
7054 | ;; the declared one and we should back up to the previous | |
7055 | ;; type. | |
7056 | (setq backup-if-not-cast t) | |
7057 | (throw 'at-decl-or-cast t))) | |
7058 | ||
a85fd6da | 7059 | ;; CASE 5 |
0386b551 AM |
7060 | (when (eq at-type t) |
7061 | ;; If the type is known we know that there can't be any | |
7062 | ;; identifier somewhere else, and it's only in declarations in | |
7063 | ;; e.g. function prototypes and in casts that the identifier may | |
7064 | ;; be left out. | |
7065 | (throw 'at-decl-or-cast t)) | |
7066 | ||
7067 | (when (= (point) start) | |
7068 | ;; Only got a single identifier (parsed as a type so far). | |
a85fd6da | 7069 | ;; CASE 6 |
0386b551 AM |
7070 | (if (and |
7071 | ;; Check that the identifier isn't at the start of an | |
7072 | ;; expression. | |
7073 | at-decl-end | |
7074 | (cond | |
7075 | ((eq context 'decl) | |
7076 | ;; Inside an arglist that contains declarations. If K&R | |
7077 | ;; style declarations and parenthesis style initializers | |
7078 | ;; aren't allowed then the single identifier must be a | |
7079 | ;; type, else we require that it's known or found | |
7080 | ;; (primitive types are handled above). | |
7081 | (or (and (not c-recognize-knr-p) | |
7082 | (not c-recognize-paren-inits)) | |
7083 | (memq at-type '(known found)))) | |
7084 | ((eq context '<>) | |
7085 | ;; Inside a template arglist. Accept known and found | |
7086 | ;; types; other identifiers could just as well be | |
7087 | ;; constants in C++. | |
7088 | (memq at-type '(known found))))) | |
7089 | (throw 'at-decl-or-cast t) | |
a85fd6da | 7090 | ;; CASE 7 |
0386b551 AM |
7091 | ;; Can't be a valid declaration or cast, but if we've found a |
7092 | ;; specifier it can't be anything else either, so treat it as | |
7093 | ;; an invalid/unfinished declaration or cast. | |
7094 | (throw 'at-decl-or-cast at-decl-or-cast)))) | |
7095 | ||
7096 | (if (and got-parens | |
7097 | (not got-prefix) | |
7098 | (not context) | |
7099 | (not (eq at-type t)) | |
7100 | (or backup-at-type | |
7101 | maybe-typeless | |
7102 | backup-maybe-typeless | |
7103 | (when c-recognize-typeless-decls | |
7104 | (or (not got-suffix) | |
7105 | (not (looking-at | |
7106 | c-after-suffixed-type-maybe-decl-key)))))) | |
7107 | ;; Got an empty paren pair and a preceding type that probably | |
7108 | ;; really is the identifier. Shift the type backwards to make | |
7109 | ;; the last one the identifier. This is analogous to the | |
7110 | ;; "backtracking" done inside the `c-type-decl-suffix-key' loop | |
7111 | ;; above. | |
7112 | ;; | |
7113 | ;; Exception: In addition to the conditions in that | |
7114 | ;; "backtracking" code, do not shift backward if we're not | |
7115 | ;; looking at either `c-after-suffixed-type-decl-key' or "[;,]". | |
7116 | ;; Since there's no preceding type, the shift would mean that | |
7117 | ;; the declaration is typeless. But if the regexp doesn't match | |
7118 | ;; then we will simply fall through in the tests below and not | |
7119 | ;; recognize it at all, so it's better to try it as an abstract | |
7120 | ;; declarator instead. | |
7121 | (c-fdoc-shift-type-backward) | |
7122 | ||
7123 | ;; Still no identifier. | |
a85fd6da | 7124 | ;; CASE 8 |
0386b551 AM |
7125 | (when (and got-prefix (or got-parens got-suffix)) |
7126 | ;; Require `got-prefix' together with either `got-parens' or | |
7127 | ;; `got-suffix' to recognize it as an abstract declarator: | |
7128 | ;; `got-parens' only is probably an empty function call. | |
7129 | ;; `got-suffix' only can build an ordinary expression together | |
7130 | ;; with the preceding identifier which we've taken as a type. | |
7131 | ;; We could actually accept on `got-prefix' only, but that can | |
7132 | ;; easily occur temporarily while writing an expression so we | |
7133 | ;; avoid that case anyway. We could do a better job if we knew | |
7134 | ;; the point when the fontification was invoked. | |
7135 | (throw 'at-decl-or-cast t)) | |
7136 | ||
a85fd6da | 7137 | ;; CASE 9 |
0386b551 AM |
7138 | (when (and at-type |
7139 | (not got-prefix) | |
7140 | (not got-parens) | |
7141 | got-suffix-after-parens | |
7142 | (eq (char-after got-suffix-after-parens) ?\()) | |
7143 | ;; Got a type, no declarator but a paren suffix. I.e. it's a | |
fa463103 | 7144 | ;; normal function call after all (or perhaps a C++ style object |
0386b551 AM |
7145 | ;; instantiation expression). |
7146 | (throw 'at-decl-or-cast nil)))) | |
7147 | ||
a85fd6da | 7148 | ;; CASE 10 |
0386b551 AM |
7149 | (when at-decl-or-cast |
7150 | ;; By now we've located the type in the declaration that we know | |
7151 | ;; we're in. | |
7152 | (throw 'at-decl-or-cast t)) | |
7153 | ||
a85fd6da | 7154 | ;; CASE 11 |
0386b551 AM |
7155 | (when (and got-identifier |
7156 | (not context) | |
7157 | (looking-at c-after-suffixed-type-decl-key) | |
7158 | (if (and got-parens | |
7159 | (not got-prefix) | |
7160 | (not got-suffix) | |
7161 | (not (eq at-type t))) | |
7162 | ;; Shift the type backward in the case that there's a | |
7163 | ;; single identifier inside parens. That can only | |
7164 | ;; occur in K&R style function declarations so it's | |
7165 | ;; more likely that it really is a function call. | |
7166 | ;; Therefore we only do this after | |
7167 | ;; `c-after-suffixed-type-decl-key' has matched. | |
7168 | (progn (c-fdoc-shift-type-backward) t) | |
7169 | got-suffix-after-parens)) | |
7170 | ;; A declaration according to `c-after-suffixed-type-decl-key'. | |
7171 | (throw 'at-decl-or-cast t)) | |
7172 | ||
a85fd6da | 7173 | ;; CASE 12 |
0386b551 AM |
7174 | (when (and (or got-prefix (not got-parens)) |
7175 | (memq at-type '(t known))) | |
7176 | ;; It's a declaration if a known type precedes it and it can't be a | |
7177 | ;; function call. | |
7178 | (throw 'at-decl-or-cast t)) | |
7179 | ||
7180 | ;; If we get here we can't tell if this is a type decl or a normal | |
7181 | ;; expression by looking at it alone. (That's under the assumption | |
7182 | ;; that normal expressions always can look like type decl expressions, | |
7183 | ;; which isn't really true but the cases where it doesn't hold are so | |
7184 | ;; uncommon (e.g. some placements of "const" in C++) it's not worth | |
7185 | ;; the effort to look for them.) | |
7186 | ||
7187 | (unless (or at-decl-end (looking-at "=[^=]")) | |
7188 | ;; If this is a declaration it should end here or its initializer(*) | |
7189 | ;; should start here, so check for allowed separation tokens. Note | |
7190 | ;; that this rule doesn't work e.g. with a K&R arglist after a | |
7191 | ;; function header. | |
7192 | ;; | |
7193 | ;; *) Don't check for C++ style initializers using parens | |
7194 | ;; since those already have been matched as suffixes. | |
7195 | ;; | |
7196 | ;; If `at-decl-or-cast' is then we've found some other sign that | |
7197 | ;; it's a declaration or cast, so then it's probably an | |
7198 | ;; invalid/unfinished one. | |
7199 | (throw 'at-decl-or-cast at-decl-or-cast)) | |
7200 | ||
7201 | ;; Below are tests that only should be applied when we're certain to | |
7202 | ;; not have parsed halfway through an expression. | |
7203 | ||
a85fd6da | 7204 | ;; CASE 14 |
0386b551 AM |
7205 | (when (memq at-type '(t known)) |
7206 | ;; The expression starts with a known type so treat it as a | |
7207 | ;; declaration. | |
7208 | (throw 'at-decl-or-cast t)) | |
7209 | ||
a85fd6da | 7210 | ;; CASE 15 |
0386b551 AM |
7211 | (when (and (c-major-mode-is 'c++-mode) |
7212 | ;; In C++ we check if the identifier is a known type, since | |
7213 | ;; (con|de)structors use the class name as identifier. | |
7214 | ;; We've always shifted over the identifier as a type and | |
7215 | ;; then backed up again in this case. | |
7216 | identifier-type | |
7217 | (or (memq identifier-type '(found known)) | |
7218 | (and (eq (char-after identifier-start) ?~) | |
7219 | ;; `at-type' probably won't be 'found for | |
7220 | ;; destructors since the "~" is then part of the | |
7221 | ;; type name being checked against the list of | |
7222 | ;; known types, so do a check without that | |
7223 | ;; operator. | |
7224 | (or (save-excursion | |
7225 | (goto-char (1+ identifier-start)) | |
7226 | (c-forward-syntactic-ws) | |
7227 | (c-with-syntax-table | |
7228 | c-identifier-syntax-table | |
7229 | (looking-at c-known-type-key))) | |
7230 | (save-excursion | |
7231 | (goto-char (1+ identifier-start)) | |
7232 | ;; We have already parsed the type earlier, | |
7233 | ;; so it'd be possible to cache the end | |
7234 | ;; position instead of redoing it here, but | |
7235 | ;; then we'd need to keep track of another | |
7236 | ;; position everywhere. | |
7237 | (c-check-type (point) | |
7238 | (progn (c-forward-type) | |
7239 | (point)))))))) | |
7240 | (throw 'at-decl-or-cast t)) | |
7241 | ||
7242 | (if got-identifier | |
7243 | (progn | |
a85fd6da | 7244 | ;; CASE 16 |
0386b551 AM |
7245 | (when (and got-prefix-before-parens |
7246 | at-type | |
7247 | (or at-decl-end (looking-at "=[^=]")) | |
7248 | (not context) | |
7249 | (not got-suffix)) | |
7250 | ;; Got something like "foo * bar;". Since we're not inside an | |
7251 | ;; arglist it would be a meaningless expression because the | |
7252 | ;; result isn't used. We therefore choose to recognize it as | |
7253 | ;; a declaration. Do not allow a suffix since it could then | |
7254 | ;; be a function call. | |
7255 | (throw 'at-decl-or-cast t)) | |
7256 | ||
a85fd6da | 7257 | ;; CASE 17 |
0386b551 AM |
7258 | (when (and (or got-suffix-after-parens |
7259 | (looking-at "=[^=]")) | |
7260 | (eq at-type 'found) | |
7261 | (not (eq context 'arglist))) | |
7262 | ;; Got something like "a (*b) (c);" or "a (b) = c;". It could | |
7263 | ;; be an odd expression or it could be a declaration. Treat | |
7264 | ;; it as a declaration if "a" has been used as a type | |
7265 | ;; somewhere else (if it's a known type we won't get here). | |
7266 | (throw 'at-decl-or-cast t))) | |
7267 | ||
a85fd6da | 7268 | ;; CASE 18 |
0386b551 AM |
7269 | (when (and context |
7270 | (or got-prefix | |
7271 | (and (eq context 'decl) | |
7272 | (not c-recognize-paren-inits) | |
7273 | (or got-parens got-suffix)))) | |
7274 | ;; Got a type followed by an abstract declarator. If `got-prefix' | |
7275 | ;; is set it's something like "a *" without anything after it. If | |
7276 | ;; `got-parens' or `got-suffix' is set it's "a()", "a[]", "a()[]", | |
7277 | ;; or similar, which we accept only if the context rules out | |
7278 | ;; expressions. | |
7279 | (throw 'at-decl-or-cast t))) | |
7280 | ||
7281 | ;; If we had a complete symbol table here (which rules out | |
7282 | ;; `c-found-types') we should return t due to the disambiguation rule | |
7283 | ;; (in at least C++) that anything that can be parsed as a declaration | |
7284 | ;; is a declaration. Now we're being more defensive and prefer to | |
7285 | ;; highlight things like "foo (bar);" as a declaration only if we're | |
7286 | ;; inside an arglist that contains declarations. | |
7287 | (eq context 'decl)))) | |
7288 | ||
7289 | ;; The point is now after the type decl expression. | |
7290 | ||
7291 | (cond | |
7292 | ;; Check for a cast. | |
7293 | ((save-excursion | |
7294 | (and | |
7295 | c-cast-parens | |
7296 | ||
7297 | ;; Should be the first type/identifier in a cast paren. | |
7298 | (> preceding-token-end (point-min)) | |
7299 | (memq (char-before preceding-token-end) c-cast-parens) | |
7300 | ||
7301 | ;; The closing paren should follow. | |
7302 | (progn | |
7303 | (c-forward-syntactic-ws) | |
7304 | (looking-at "\\s\)")) | |
7305 | ||
7306 | ;; There should be a primary expression after it. | |
7307 | (let (pos) | |
7308 | (forward-char) | |
7309 | (c-forward-syntactic-ws) | |
7310 | (setq cast-end (point)) | |
7311 | (and (looking-at c-primary-expr-regexp) | |
7312 | (progn | |
7313 | (setq pos (match-end 0)) | |
7314 | (or | |
7315 | ;; Check if the expression begins with a prefix keyword. | |
7316 | (match-beginning 2) | |
7317 | (if (match-beginning 1) | |
7318 | ;; Expression begins with an ambiguous operator. Treat | |
7319 | ;; it as a cast if it's a type decl or if we've | |
7320 | ;; recognized the type somewhere else. | |
7321 | (or at-decl-or-cast | |
7322 | (memq at-type '(t known found))) | |
7323 | ;; Unless it's a keyword, it's the beginning of a primary | |
7324 | ;; expression. | |
7325 | (not (looking-at c-keywords-regexp))))) | |
7326 | ;; If `c-primary-expr-regexp' matched a nonsymbol token, check | |
7327 | ;; that it matched a whole one so that we don't e.g. confuse | |
7328 | ;; the operator '-' with '->'. It's ok if it matches further, | |
7329 | ;; though, since it e.g. can match the float '.5' while the | |
7330 | ;; operator regexp only matches '.'. | |
7331 | (or (not (looking-at c-nonsymbol-token-regexp)) | |
7332 | (<= (match-end 0) pos)))) | |
7333 | ||
7334 | ;; There should either be a cast before it or something that isn't an | |
7335 | ;; identifier or close paren. | |
7336 | (> preceding-token-end (point-min)) | |
7337 | (progn | |
7338 | (goto-char (1- preceding-token-end)) | |
7339 | (or (eq (point) last-cast-end) | |
7340 | (progn | |
7341 | (c-backward-syntactic-ws) | |
7342 | (if (< (skip-syntax-backward "w_") 0) | |
7343 | ;; It's a symbol. Accept it only if it's one of the | |
7344 | ;; keywords that can precede an expression (without | |
7345 | ;; surrounding parens). | |
7346 | (looking-at c-simple-stmt-key) | |
7347 | (and | |
7348 | ;; Check that it isn't a close paren (block close is ok, | |
7349 | ;; though). | |
7350 | (not (memq (char-before) '(?\) ?\]))) | |
7351 | ;; Check that it isn't a nonsymbol identifier. | |
7352 | (not (c-on-identifier))))))))) | |
7353 | ||
7354 | ;; Handle the cast. | |
7355 | (when (and c-record-type-identifiers at-type (not (eq at-type t))) | |
7356 | (let ((c-promote-possible-types t)) | |
7357 | (goto-char type-start) | |
7358 | (c-forward-type))) | |
7359 | ||
7360 | (goto-char cast-end) | |
7361 | 'cast) | |
7362 | ||
7363 | (at-decl-or-cast | |
7364 | ;; We're at a declaration. Highlight the type and the following | |
7365 | ;; declarators. | |
7366 | ||
7367 | (when backup-if-not-cast | |
7368 | (c-fdoc-shift-type-backward t)) | |
7369 | ||
7370 | (when (and (eq context 'decl) (looking-at ",")) | |
7371 | ;; Make sure to propagate the `c-decl-arg-start' property to | |
7372 | ;; the next argument if it's set in this one, to cope with | |
7373 | ;; interactive refontification. | |
7374 | (c-put-c-type-property (point) 'c-decl-arg-start)) | |
7375 | ||
7376 | (when (and c-record-type-identifiers at-type (not (eq at-type t))) | |
7377 | (let ((c-promote-possible-types t)) | |
7378 | (save-excursion | |
7379 | (goto-char type-start) | |
7380 | (c-forward-type)))) | |
7381 | ||
e15f8aaa AM |
7382 | (cons id-start |
7383 | (and (or at-type-decl at-typedef) | |
7384 | (cons at-type-decl at-typedef)))) | |
0386b551 AM |
7385 | |
7386 | (t | |
7387 | ;; False alarm. Restore the recorded ranges. | |
7388 | (setq c-record-type-identifiers save-rec-type-ids | |
7389 | c-record-ref-identifiers save-rec-ref-ids) | |
7390 | nil)))) | |
7391 | ||
7392 | (defun c-forward-label (&optional assume-markup preceding-token-end limit) | |
51c9af45 | 7393 | ;; Assuming that point is at the beginning of a token, check if it starts a |
1379f2c5 AM |
7394 | ;; label and if so move over it and return non-nil (t in default situations, |
7395 | ;; specific symbols (see below) for interesting situations), otherwise don't | |
7396 | ;; move and return nil. "Label" here means "most things with a colon". | |
51c9af45 AM |
7397 | ;; |
7398 | ;; More precisely, a "label" is regarded as one of: | |
1379f2c5 AM |
7399 | ;; (i) a goto target like "foo:" - returns the symbol `goto-target'; |
7400 | ;; (ii) A case label - either the entire construct "case FOO:", or just the | |
7401 | ;; bare "case", should the colon be missing. We return t; | |
7402 | ;; (iii) a keyword which needs a colon, like "default:" or "private:"; We | |
7403 | ;; return t; | |
51c9af45 | 7404 | ;; (iv) One of QT's "extended" C++ variants of |
1379f2c5 AM |
7405 | ;; "private:"/"protected:"/"public:"/"more:" looking like "public slots:". |
7406 | ;; Returns the symbol `qt-2kwds-colon'. | |
7407 | ;; (v) QT's construct "signals:". Returns the symbol `qt-1kwd-colon'. | |
82ba65cf | 7408 | ;; (vi) One of the keywords matched by `c-opt-extra-label-key' (without any |
51c9af45 | 7409 | ;; colon). Currently (2006-03), this applies only to Objective C's |
1379f2c5 | 7410 | ;; keywords "@private", "@protected", and "@public". Returns t. |
51c9af45 | 7411 | ;; |
e1dbe924 | 7412 | ;; One of the things which will NOT be recognized as a label is a bit-field |
51c9af45 AM |
7413 | ;; element of a struct, something like "int foo:5". |
7414 | ;; | |
7415 | ;; The end of the label is taken to be just after the colon, or the end of | |
7416 | ;; the first submatch in `c-opt-extra-label-key'. The point is directly | |
7417 | ;; after the end on return. The terminating char gets marked with | |
7418 | ;; `c-decl-end' to improve recognition of the following declaration or | |
7419 | ;; statement. | |
0386b551 AM |
7420 | ;; |
7421 | ;; If ASSUME-MARKUP is non-nil, it's assumed that the preceding | |
51c9af45 | 7422 | ;; label, if any, has already been marked up like that. |
0386b551 AM |
7423 | ;; |
7424 | ;; If PRECEDING-TOKEN-END is given, it should be the first position | |
7425 | ;; after the preceding token, i.e. on the other side of the | |
7426 | ;; syntactic ws from the point. Use a value less than or equal to | |
7427 | ;; (point-min) if the point is at the first token in (the visible | |
7428 | ;; part of) the buffer. | |
7429 | ;; | |
7430 | ;; The optional LIMIT limits the forward scan for the colon. | |
7431 | ;; | |
7432 | ;; This function records the ranges of the label symbols on | |
7433 | ;; `c-record-ref-identifiers' if `c-record-type-identifiers' (!) is | |
7434 | ;; non-nil. | |
7435 | ;; | |
7436 | ;; This function might do hidden buffer changes. | |
7437 | ||
51c9af45 | 7438 | (let ((start (point)) |
1379f2c5 | 7439 | label-end |
51c9af45 | 7440 | qt-symbol-idx |
1379f2c5 | 7441 | macro-start ; if we're in one. |
f412a567 AM |
7442 | label-type |
7443 | kwd) | |
0386b551 | 7444 | (cond |
b414f371 | 7445 | ;; "case" or "default" (Doesn't apply to AWK). |
0386b551 AM |
7446 | ((looking-at c-label-kwds-regexp) |
7447 | (let ((kwd-end (match-end 1))) | |
7448 | ;; Record only the keyword itself for fontification, since in | |
7449 | ;; case labels the following is a constant expression and not | |
7450 | ;; a label. | |
7451 | (when c-record-type-identifiers | |
7452 | (c-record-ref-id (cons (match-beginning 1) kwd-end))) | |
7453 | ||
7454 | ;; Find the label end. | |
7455 | (goto-char kwd-end) | |
1379f2c5 AM |
7456 | (setq label-type |
7457 | (if (and (c-syntactic-re-search-forward | |
7458 | ;; Stop on chars that aren't allowed in expressions, | |
7459 | ;; and on operator chars that would be meaningless | |
7460 | ;; there. FIXME: This doesn't cope with ?: operators. | |
7461 | "[;{=,@]\\|\\(\\=\\|[^:]\\):\\([^:]\\|\\'\\)" | |
7462 | limit t t nil 1) | |
7463 | (match-beginning 2)) | |
7464 | ||
7465 | (progn ; there's a proper : | |
7466 | (goto-char (match-beginning 2)) ; just after the : | |
7467 | (c-put-c-type-property (1- (point)) 'c-decl-end) | |
7468 | t) | |
7469 | ||
7470 | ;; It's an unfinished label. We consider the keyword enough | |
7471 | ;; to recognize it as a label, so that it gets fontified. | |
7472 | ;; Leave the point at the end of it, but don't put any | |
7473 | ;; `c-decl-end' marker. | |
7474 | (goto-char kwd-end) | |
7475 | t)))) | |
0386b551 | 7476 | |
51c9af45 | 7477 | ;; @private, @protected, @public, in Objective C, or similar. |
0386b551 AM |
7478 | ((and c-opt-extra-label-key |
7479 | (looking-at c-opt-extra-label-key)) | |
7480 | ;; For a `c-opt-extra-label-key' match, we record the whole | |
7481 | ;; thing for fontification. That's to get the leading '@' in | |
7482 | ;; Objective-C protection labels fontified. | |
7483 | (goto-char (match-end 1)) | |
7484 | (when c-record-type-identifiers | |
7485 | (c-record-ref-id (cons (match-beginning 1) (point)))) | |
7486 | (c-put-c-type-property (1- (point)) 'c-decl-end) | |
1379f2c5 | 7487 | (setq label-type t)) |
0386b551 | 7488 | |
51c9af45 AM |
7489 | ;; All other cases of labels. |
7490 | ((and c-recognize-colon-labels ; nil for AWK and IDL, otherwise t. | |
0386b551 AM |
7491 | |
7492 | ;; A colon label must have something before the colon. | |
7493 | (not (eq (char-after) ?:)) | |
7494 | ||
7495 | ;; Check that we're not after a token that can't precede a label. | |
7496 | (or | |
7497 | ;; Trivially succeeds when there's no preceding token. | |
536610a4 | 7498 | ;; Succeeds when we're at a virtual semicolon. |
0386b551 AM |
7499 | (if preceding-token-end |
7500 | (<= preceding-token-end (point-min)) | |
7501 | (save-excursion | |
7502 | (c-backward-syntactic-ws) | |
7503 | (setq preceding-token-end (point)) | |
536610a4 AM |
7504 | (or (bobp) |
7505 | (c-at-vsemi-p)))) | |
0386b551 AM |
7506 | |
7507 | ;; Check if we're after a label, if we're after a closing | |
7508 | ;; paren that belong to statement, and with | |
7509 | ;; `c-label-prefix-re'. It's done in different order | |
7510 | ;; depending on `assume-markup' since the checks have | |
7511 | ;; different expensiveness. | |
7512 | (if assume-markup | |
7513 | (or | |
7514 | (eq (c-get-char-property (1- preceding-token-end) 'c-type) | |
7515 | 'c-decl-end) | |
7516 | ||
7517 | (save-excursion | |
7518 | (goto-char (1- preceding-token-end)) | |
7519 | (c-beginning-of-current-token) | |
51c9af45 AM |
7520 | (or (looking-at c-label-prefix-re) |
7521 | (looking-at c-block-stmt-1-key))) | |
0386b551 AM |
7522 | |
7523 | (and (eq (char-before preceding-token-end) ?\)) | |
7524 | (c-after-conditional))) | |
7525 | ||
7526 | (or | |
7527 | (save-excursion | |
7528 | (goto-char (1- preceding-token-end)) | |
7529 | (c-beginning-of-current-token) | |
51c9af45 AM |
7530 | (or (looking-at c-label-prefix-re) |
7531 | (looking-at c-block-stmt-1-key))) | |
0386b551 AM |
7532 | |
7533 | (cond | |
7534 | ((eq (char-before preceding-token-end) ?\)) | |
7535 | (c-after-conditional)) | |
7536 | ||
7537 | ((eq (char-before preceding-token-end) ?:) | |
7538 | ;; Might be after another label, so check it recursively. | |
51c9af45 AM |
7539 | (save-restriction |
7540 | (save-excursion | |
7541 | (goto-char (1- preceding-token-end)) | |
7542 | ;; Essentially the same as the | |
7543 | ;; `c-syntactic-re-search-forward' regexp below. | |
7544 | (setq macro-start | |
7545 | (save-excursion (and (c-beginning-of-macro) | |
7546 | (point)))) | |
7547 | (if macro-start (narrow-to-region macro-start (point-max))) | |
7548 | (c-syntactic-skip-backward "^-]:?;}=*/%&|,<>!@+" nil t) | |
7549 | ;; Note: the following should work instead of the | |
7550 | ;; narrow-to-region above. Investigate why not, | |
7551 | ;; sometime. ACM, 2006-03-31. | |
7552 | ;; (c-syntactic-skip-backward "^-]:?;}=*/%&|,<>!@+" | |
7553 | ;; macro-start t) | |
7554 | (let ((pte (point)) | |
7555 | ;; If the caller turned on recording for us, | |
7556 | ;; it shouldn't apply when we check the | |
7557 | ;; preceding label. | |
7558 | c-record-type-identifiers) | |
7559 | ;; A label can't start at a cpp directive. Check for | |
7560 | ;; this, since c-forward-syntactic-ws would foul up on it. | |
7561 | (unless (and c-opt-cpp-prefix (looking-at c-opt-cpp-prefix)) | |
7562 | (c-forward-syntactic-ws) | |
7563 | (c-forward-label nil pte start)))))))))) | |
7564 | ||
1379f2c5 | 7565 | ;; Point is still at the beginning of the possible label construct. |
b414f371 | 7566 | ;; |
51c9af45 AM |
7567 | ;; Check that the next nonsymbol token is ":", or that we're in one |
7568 | ;; of QT's "slots" declarations. Allow '(' for the sake of macro | |
7569 | ;; arguments. FIXME: Should build this regexp from the language | |
7570 | ;; constants. | |
1379f2c5 AM |
7571 | (cond |
7572 | ;; public: protected: private: | |
7573 | ((and | |
7574 | (c-major-mode-is 'c++-mode) | |
7575 | (search-forward-regexp | |
7576 | "\\=p\\(r\\(ivate\\|otected\\)\\|ublic\\)\\>[^_]" nil t) | |
7577 | (progn (backward-char) | |
7578 | (c-forward-syntactic-ws limit) | |
7579 | (looking-at ":\\([^:]\\|\\'\\)"))) ; A single colon. | |
7580 | (forward-char) | |
7581 | (setq label-type t)) | |
7582 | ;; QT double keyword like "protected slots:" or goto target. | |
7583 | ((progn (goto-char start) nil)) | |
7584 | ((when (c-syntactic-re-search-forward | |
7585 | "[ \t\n[:?;{=*/%&|,<>!@+-]" limit t t) ; not at EOB | |
7586 | (backward-char) | |
7587 | (setq label-end (point)) | |
7588 | (setq qt-symbol-idx | |
7589 | (and (c-major-mode-is 'c++-mode) | |
7590 | (string-match | |
7591 | "\\(p\\(r\\(ivate\\|otected\\)\\|ublic\\)\\|more\\)\\>" | |
b414f371 | 7592 | (buffer-substring start (point))))) |
1379f2c5 AM |
7593 | (c-forward-syntactic-ws limit) |
7594 | (cond | |
7595 | ((looking-at ":\\([^:]\\|\\'\\)") ; A single colon. | |
7596 | (forward-char) | |
7597 | (setq label-type | |
f412a567 AM |
7598 | (if (or (string= "signals" ; Special QT macro |
7599 | (setq kwd (buffer-substring-no-properties start label-end))) | |
7600 | (string= "Q_SIGNALS" kwd)) | |
1379f2c5 AM |
7601 | 'qt-1kwd-colon |
7602 | 'goto-target))) | |
7603 | ((and qt-symbol-idx | |
f412a567 | 7604 | (search-forward-regexp "\\=\\(slots\\|Q_SLOTS\\)\\>" limit t) |
1379f2c5 AM |
7605 | (progn (c-forward-syntactic-ws limit) |
7606 | (looking-at ":\\([^:]\\|\\'\\)"))) ; A single colon | |
7607 | (forward-char) | |
7608 | (setq label-type 'qt-2kwds-colon))))))) | |
0386b551 AM |
7609 | |
7610 | (save-restriction | |
7611 | (narrow-to-region start (point)) | |
7612 | ||
7613 | ;; Check that `c-nonlabel-token-key' doesn't match anywhere. | |
7614 | (catch 'check-label | |
7615 | (goto-char start) | |
7616 | (while (progn | |
7617 | (when (looking-at c-nonlabel-token-key) | |
7618 | (goto-char start) | |
1379f2c5 | 7619 | (setq label-type nil) |
0386b551 AM |
7620 | (throw 'check-label nil)) |
7621 | (and (c-safe (c-forward-sexp) | |
7622 | (c-forward-syntactic-ws) | |
7623 | t) | |
7624 | (not (eobp))))) | |
7625 | ||
7626 | ;; Record the identifiers in the label for fontification, unless | |
7627 | ;; it begins with `c-label-kwds' in which case the following | |
7628 | ;; identifiers are part of a (constant) expression that | |
7629 | ;; shouldn't be fontified. | |
7630 | (when (and c-record-type-identifiers | |
7631 | (progn (goto-char start) | |
7632 | (not (looking-at c-label-kwds-regexp)))) | |
7633 | (while (c-syntactic-re-search-forward c-symbol-key nil t) | |
7634 | (c-record-ref-id (cons (match-beginning 0) | |
7635 | (match-end 0))))) | |
7636 | ||
7637 | (c-put-c-type-property (1- (point-max)) 'c-decl-end) | |
82ba65cf | 7638 | (goto-char (point-max))))) |
0386b551 AM |
7639 | |
7640 | (t | |
7641 | ;; Not a label. | |
1379f2c5 AM |
7642 | (goto-char start))) |
7643 | label-type)) | |
0386b551 AM |
7644 | |
7645 | (defun c-forward-objc-directive () | |
7646 | ;; Assuming the point is at the beginning of a token, try to move | |
7647 | ;; forward to the end of the Objective-C directive that starts | |
7648 | ;; there. Return t if a directive was fully recognized, otherwise | |
7649 | ;; the point is moved as far as one could be successfully parsed and | |
7650 | ;; nil is returned. | |
7651 | ;; | |
7652 | ;; This function records identifier ranges on | |
7653 | ;; `c-record-type-identifiers' and `c-record-ref-identifiers' if | |
7654 | ;; `c-record-type-identifiers' is non-nil. | |
7655 | ;; | |
7656 | ;; This function might do hidden buffer changes. | |
7657 | ||
7658 | (let ((start (point)) | |
7659 | start-char | |
7660 | (c-promote-possible-types t) | |
667ced3a | 7661 | lim |
0386b551 AM |
7662 | ;; Turn off recognition of angle bracket arglists while parsing |
7663 | ;; types here since the protocol reference list might then be | |
7664 | ;; considered part of the preceding name or superclass-name. | |
7665 | c-recognize-<>-arglists) | |
7666 | ||
7667 | (if (or | |
7668 | (when (looking-at | |
7669 | (eval-when-compile | |
7670 | (c-make-keywords-re t | |
7671 | (append (c-lang-const c-protection-kwds objc) | |
7672 | '("@end")) | |
7673 | 'objc-mode))) | |
7674 | (goto-char (match-end 1)) | |
7675 | t) | |
7676 | ||
7677 | (and | |
7678 | (looking-at | |
7679 | (eval-when-compile | |
7680 | (c-make-keywords-re t | |
7681 | '("@interface" "@implementation" "@protocol") | |
7682 | 'objc-mode))) | |
7683 | ||
7684 | ;; Handle the name of the class itself. | |
7685 | (progn | |
cb694ab7 AM |
7686 | ; (c-forward-token-2) ; 2006/1/13 This doesn't move if the token's |
7687 | ; at EOB. | |
7688 | (goto-char (match-end 0)) | |
667ced3a | 7689 | (setq lim (point)) |
cb694ab7 | 7690 | (c-skip-ws-forward) |
0386b551 AM |
7691 | (c-forward-type)) |
7692 | ||
7693 | (catch 'break | |
7694 | ;; Look for ": superclass-name" or "( category-name )". | |
7695 | (when (looking-at "[:\(]") | |
7696 | (setq start-char (char-after)) | |
7697 | (forward-char) | |
7698 | (c-forward-syntactic-ws) | |
7699 | (unless (c-forward-type) (throw 'break nil)) | |
7700 | (when (eq start-char ?\() | |
7701 | (unless (eq (char-after) ?\)) (throw 'break nil)) | |
7702 | (forward-char) | |
7703 | (c-forward-syntactic-ws))) | |
7704 | ||
7705 | ;; Look for a protocol reference list. | |
7706 | (if (eq (char-after) ?<) | |
7707 | (let ((c-recognize-<>-arglists t) | |
7708 | (c-parse-and-markup-<>-arglists t) | |
7709 | c-restricted-<>-arglists) | |
7710 | (c-forward-<>-arglist t)) | |
7711 | t)))) | |
7712 | ||
7713 | (progn | |
667ced3a | 7714 | (c-backward-syntactic-ws lim) |
0386b551 AM |
7715 | (c-clear-c-type-property start (1- (point)) 'c-decl-end) |
7716 | (c-put-c-type-property (1- (point)) 'c-decl-end) | |
7717 | t) | |
7718 | ||
7719 | (c-clear-c-type-property start (point) 'c-decl-end) | |
7720 | nil))) | |
7721 | ||
785eecbb RS |
7722 | (defun c-beginning-of-inheritance-list (&optional lim) |
7723 | ;; Go to the first non-whitespace after the colon that starts a | |
7724 | ;; multiple inheritance introduction. Optional LIM is the farthest | |
7725 | ;; back we should search. | |
0386b551 AM |
7726 | ;; |
7727 | ;; This function might do hidden buffer changes. | |
7728 | (c-with-syntax-table c++-template-syntax-table | |
7729 | (c-backward-token-2 0 t lim) | |
7730 | (while (and (or (looking-at c-symbol-start) | |
7731 | (looking-at "[<,]\\|::")) | |
7732 | (zerop (c-backward-token-2 1 t lim)))))) | |
785eecbb | 7733 | |
785eecbb RS |
7734 | (defun c-in-method-def-p () |
7735 | ;; Return nil if we aren't in a method definition, otherwise the | |
7736 | ;; position of the initial [+-]. | |
0386b551 AM |
7737 | ;; |
7738 | ;; This function might do hidden buffer changes. | |
785eecbb RS |
7739 | (save-excursion |
7740 | (beginning-of-line) | |
a66cd3ee MS |
7741 | (and c-opt-method-key |
7742 | (looking-at c-opt-method-key) | |
785eecbb RS |
7743 | (point)) |
7744 | )) | |
7745 | ||
a66cd3ee MS |
7746 | ;; Contributed by Kevin Ryde <user42@zip.com.au>. |
7747 | (defun c-in-gcc-asm-p () | |
7748 | ;; Return non-nil if point is within a gcc \"asm\" block. | |
7749 | ;; | |
7750 | ;; This should be called with point inside an argument list. | |
7751 | ;; | |
7752 | ;; Only one level of enclosing parentheses is considered, so for | |
7753 | ;; instance `nil' is returned when in a function call within an asm | |
7754 | ;; operand. | |
0386b551 AM |
7755 | ;; |
7756 | ;; This function might do hidden buffer changes. | |
a66cd3ee MS |
7757 | |
7758 | (and c-opt-asm-stmt-key | |
7759 | (save-excursion | |
7760 | (beginning-of-line) | |
7761 | (backward-up-list 1) | |
7762 | (c-beginning-of-statement-1 (point-min) nil t) | |
7763 | (looking-at c-opt-asm-stmt-key)))) | |
7764 | ||
abb7e5cf | 7765 | (defun c-at-toplevel-p () |
a85fd6da AM |
7766 | "Return a determination as to whether point is \"at the top level\". |
7767 | Informally, \"at the top level\" is anywhere where you can write | |
7768 | a function. | |
7769 | ||
7770 | More precisely, being at the top-level means that point is either | |
7771 | outside any enclosing block (such as a function definition), or | |
7772 | directly inside a class, namespace or other block that contains | |
7773 | another declaration level. | |
abb7e5cf SM |
7774 | |
7775 | If point is not at the top-level (e.g. it is inside a method | |
7776 | definition), then nil is returned. Otherwise, if point is at a | |
7777 | top-level not enclosed within a class definition, t is returned. | |
7778 | Otherwise, a 2-vector is returned where the zeroth element is the | |
7779 | buffer position of the start of the class declaration, and the first | |
7780 | element is the buffer position of the enclosing class's opening | |
0386b551 AM |
7781 | brace. |
7782 | ||
7783 | Note that this function might do hidden buffer changes. See the | |
7784 | comment at the start of cc-engine.el for more info." | |
a66cd3ee MS |
7785 | (let ((paren-state (c-parse-state))) |
7786 | (or (not (c-most-enclosing-brace paren-state)) | |
7787 | (c-search-uplist-for-classkey paren-state)))) | |
7788 | ||
d9e94c22 | 7789 | (defun c-just-after-func-arglist-p (&optional lim) |
0386b551 AM |
7790 | ;; Return non-nil if the point is in the region after the argument |
7791 | ;; list of a function and its opening brace (or semicolon in case it | |
7792 | ;; got no body). If there are K&R style argument declarations in | |
7793 | ;; that region, the point has to be inside the first one for this | |
7794 | ;; function to recognize it. | |
a66cd3ee | 7795 | ;; |
0386b551 AM |
7796 | ;; If successful, the point is moved to the first token after the |
7797 | ;; function header (see `c-forward-decl-or-cast-1' for details) and | |
7798 | ;; the position of the opening paren of the function arglist is | |
7799 | ;; returned. | |
7800 | ;; | |
7801 | ;; The point is clobbered if not successful. | |
7802 | ;; | |
7803 | ;; LIM is used as bound for backward buffer searches. | |
7804 | ;; | |
7805 | ;; This function might do hidden buffer changes. | |
7806 | ||
7807 | (let ((beg (point)) end id-start) | |
7808 | (and | |
7809 | (eq (c-beginning-of-statement-1 lim) 'same) | |
7810 | ||
2e492df3 AM |
7811 | (not (and (c-major-mode-is 'objc-mode) |
7812 | (c-forward-objc-directive))) | |
0386b551 AM |
7813 | |
7814 | (setq id-start | |
7815 | (car-safe (c-forward-decl-or-cast-1 (c-point 'bosws) nil nil))) | |
7816 | (< id-start beg) | |
7817 | ||
7818 | ;; There should not be a '=' or ',' between beg and the | |
7819 | ;; start of the declaration since that means we were in the | |
7820 | ;; "expression part" of the declaration. | |
7821 | (or (> (point) beg) | |
7822 | (not (looking-at "[=,]"))) | |
7823 | ||
7824 | (save-excursion | |
7825 | ;; Check that there's an arglist paren in the | |
7826 | ;; declaration. | |
7827 | (goto-char id-start) | |
7828 | (cond ((eq (char-after) ?\() | |
7829 | ;; The declarator is a paren expression, so skip past it | |
7830 | ;; so that we don't get stuck on that instead of the | |
7831 | ;; function arglist. | |
7832 | (c-forward-sexp)) | |
51c9af45 AM |
7833 | ((and c-opt-op-identifier-prefix |
7834 | (looking-at c-opt-op-identifier-prefix)) | |
0386b551 AM |
7835 | ;; Don't trip up on "operator ()". |
7836 | (c-forward-token-2 2 t))) | |
7837 | (and (< (point) beg) | |
7838 | (c-syntactic-re-search-forward "(" beg t t) | |
7839 | (1- (point))))))) | |
785eecbb | 7840 | |
a66cd3ee MS |
7841 | (defun c-in-knr-argdecl (&optional lim) |
7842 | ;; Return the position of the first argument declaration if point is | |
7843 | ;; inside a K&R style argument declaration list, nil otherwise. | |
7844 | ;; `c-recognize-knr-p' is not checked. If LIM is non-nil, it's a | |
7845 | ;; position that bounds the backward search for the argument list. | |
7846 | ;; | |
9cf17ef1 AM |
7847 | ;; Point must be within a possible K&R region, e.g. just before a top-level |
7848 | ;; "{". It must be outside of parens and brackets. The test can return | |
7849 | ;; false positives otherwise. | |
0386b551 AM |
7850 | ;; |
7851 | ;; This function might do hidden buffer changes. | |
d9e94c22 | 7852 | |
a66cd3ee MS |
7853 | (save-excursion |
7854 | (save-restriction | |
9cf17ef1 AM |
7855 | ;; If we're in a macro, our search range is restricted to it. Narrow to |
7856 | ;; the searchable range. | |
98d7371e AM |
7857 | (let* ((macro-start (save-excursion (and (c-beginning-of-macro) (point)))) |
7858 | (macro-end (save-excursion (and macro-start (c-end-of-macro) (point)))) | |
7859 | (low-lim (max (or lim (point-min)) (or macro-start (point-min)))) | |
6b6481ed | 7860 | before-lparen after-rparen |
98d7371e AM |
7861 | (pp-count-out 20)) ; Max number of paren/brace constructs before |
7862 | ; we give up | |
7863 | (narrow-to-region low-lim (or macro-end (point-max))) | |
9cf17ef1 AM |
7864 | |
7865 | ;; Search backwards for the defun's argument list. We give up if we | |
98d7371e AM |
7866 | ;; encounter a "}" (end of a previous defun) an "=" (which can't be in |
7867 | ;; a knr region) or BOB. | |
9cf17ef1 AM |
7868 | ;; |
7869 | ;; The criterion for a paren structure being the arg list is: | |
7870 | ;; o - there is non-WS stuff after it but before any "{"; AND | |
7871 | ;; o - the token after it isn't a ";" AND | |
7872 | ;; o - it is preceded by either an identifier (the function name) or | |
7873 | ;; a macro expansion like "DEFUN (...)"; AND | |
7874 | ;; o - its content is a non-empty comma-separated list of identifiers | |
7875 | ;; (an empty arg list won't have a knr region). | |
7876 | ;; | |
7877 | ;; The following snippet illustrates these rules: | |
7878 | ;; int foo (bar, baz, yuk) | |
7879 | ;; int bar [] ; | |
7880 | ;; int (*baz) (my_type) ; | |
7881 | ;; int (*) (void) (*yuk) (void) ; | |
7882 | ;; { | |
7883 | ||
7884 | (catch 'knr | |
6b6481ed AM |
7885 | (while (> pp-count-out 0) ; go back one paren/bracket pair each time. |
7886 | (setq pp-count-out (1- pp-count-out)) | |
98d7371e | 7887 | (c-syntactic-skip-backward "^)]}=") |
9cf17ef1 AM |
7888 | (cond ((eq (char-before) ?\)) |
7889 | (setq after-rparen (point))) | |
10489fcb AM |
7890 | ((eq (char-before) ?\]) |
7891 | (setq after-rparen nil)) | |
98d7371e AM |
7892 | (t ; either } (hit previous defun) or = or no more |
7893 | ; parens/brackets. | |
10489fcb | 7894 | (throw 'knr nil))) |
9cf17ef1 AM |
7895 | |
7896 | (if after-rparen | |
7897 | ;; We're inside a paren. Could it be our argument list....? | |
7898 | (if | |
7899 | (and | |
7900 | (progn | |
7901 | (goto-char after-rparen) | |
7902 | (unless (c-go-list-backward) (throw 'knr nil)) ; | |
7903 | ;; FIXME!!! What about macros between the parens? 2007/01/20 | |
7904 | (setq before-lparen (point))) | |
d9e94c22 | 7905 | |
9cf17ef1 AM |
7906 | ;; It can't be the arg list if next token is ; or { |
7907 | (progn (goto-char after-rparen) | |
7908 | (c-forward-syntactic-ws) | |
98d7371e | 7909 | (not (memq (char-after) '(?\; ?\{ ?\=)))) |
d9e94c22 | 7910 | |
9cf17ef1 AM |
7911 | ;; Is the thing preceding the list an identifier (the |
7912 | ;; function name), or a macro expansion? | |
7913 | (progn | |
7914 | (goto-char before-lparen) | |
7915 | (eq (c-backward-token-2) 0) | |
98d7371e | 7916 | (or (eq (c-on-identifier) (point)) |
9cf17ef1 AM |
7917 | (and (eq (char-after) ?\)) |
7918 | (c-go-up-list-backward) | |
7919 | (eq (c-backward-token-2) 0) | |
98d7371e | 7920 | (eq (c-on-identifier) (point))))) |
9cf17ef1 AM |
7921 | |
7922 | ;; Have we got a non-empty list of comma-separated | |
7923 | ;; identifiers? | |
7924 | (progn | |
7925 | (goto-char before-lparen) | |
7926 | (c-forward-token-2) ; to first token inside parens | |
7927 | (and | |
7928 | (c-on-identifier) | |
7929 | (c-forward-token-2) | |
7930 | (catch 'id-list | |
7931 | (while (eq (char-after) ?\,) | |
7932 | (c-forward-token-2) | |
7933 | (unless (c-on-identifier) (throw 'id-list nil)) | |
7934 | (c-forward-token-2)) | |
7935 | (eq (char-after) ?\)))))) | |
7936 | ||
7937 | ;; ...Yes. We've identified the function's argument list. | |
7938 | (throw 'knr | |
7939 | (progn (goto-char after-rparen) | |
7940 | (c-forward-syntactic-ws) | |
7941 | (point))) | |
7942 | ||
7943 | ;; ...No. The current parens aren't the function's arg list. | |
7944 | (goto-char before-lparen)) | |
7945 | ||
7946 | (or (c-go-list-backward) ; backwards over [ .... ] | |
7947 | (throw 'knr nil))))))))) | |
785eecbb RS |
7948 | |
7949 | (defun c-skip-conditional () | |
7950 | ;; skip forward over conditional at point, including any predicate | |
7951 | ;; statements in parentheses. No error checking is performed. | |
0386b551 AM |
7952 | ;; |
7953 | ;; This function might do hidden buffer changes. | |
0ec8351b BW |
7954 | (c-forward-sexp (cond |
7955 | ;; else if() | |
a66cd3ee MS |
7956 | ((looking-at (concat "\\<else" |
7957 | "\\([ \t\n]\\|\\\\\n\\)+" | |
7958 | "if\\>\\([^_]\\|$\\)")) | |
7959 | 3) | |
0ec8351b | 7960 | ;; do, else, try, finally |
a66cd3ee MS |
7961 | ((looking-at (concat "\\<\\(" |
7962 | "do\\|else\\|try\\|finally" | |
7963 | "\\)\\>\\([^_]\\|$\\)")) | |
130c507e | 7964 | 1) |
ce8c7486 | 7965 | ;; for, if, while, switch, catch, synchronized, foreach |
0ec8351b | 7966 | (t 2)))) |
785eecbb | 7967 | |
a66cd3ee MS |
7968 | (defun c-after-conditional (&optional lim) |
7969 | ;; If looking at the token after a conditional then return the | |
7970 | ;; position of its start, otherwise return nil. | |
0386b551 AM |
7971 | ;; |
7972 | ;; This function might do hidden buffer changes. | |
a66cd3ee | 7973 | (save-excursion |
d9e94c22 | 7974 | (and (zerop (c-backward-token-2 1 t lim)) |
a66cd3ee MS |
7975 | (or (looking-at c-block-stmt-1-key) |
7976 | (and (eq (char-after) ?\() | |
d9e94c22 | 7977 | (zerop (c-backward-token-2 1 t lim)) |
a66cd3ee MS |
7978 | (looking-at c-block-stmt-2-key))) |
7979 | (point)))) | |
7980 | ||
0386b551 AM |
7981 | (defun c-after-special-operator-id (&optional lim) |
7982 | ;; If the point is after an operator identifier that isn't handled | |
7983 | ;; like an ordinary symbol (i.e. like "operator =" in C++) then the | |
7984 | ;; position of the start of that identifier is returned. nil is | |
7985 | ;; returned otherwise. The point may be anywhere in the syntactic | |
7986 | ;; whitespace after the last token of the operator identifier. | |
7987 | ;; | |
7988 | ;; This function might do hidden buffer changes. | |
7989 | (save-excursion | |
7990 | (and c-overloadable-operators-regexp | |
7991 | (zerop (c-backward-token-2 1 nil lim)) | |
7992 | (looking-at c-overloadable-operators-regexp) | |
51c9af45 | 7993 | (or (not c-opt-op-identifier-prefix) |
0386b551 AM |
7994 | (and |
7995 | (zerop (c-backward-token-2 1 nil lim)) | |
51c9af45 | 7996 | (looking-at c-opt-op-identifier-prefix))) |
0386b551 AM |
7997 | (point)))) |
7998 | ||
a66cd3ee MS |
7999 | (defsubst c-backward-to-block-anchor (&optional lim) |
8000 | ;; Assuming point is at a brace that opens a statement block of some | |
8001 | ;; kind, move to the proper anchor point for that block. It might | |
8002 | ;; need to be adjusted further by c-add-stmt-syntax, but the | |
8003 | ;; position at return is suitable as start position for that | |
8004 | ;; function. | |
0386b551 AM |
8005 | ;; |
8006 | ;; This function might do hidden buffer changes. | |
a66cd3ee MS |
8007 | (unless (= (point) (c-point 'boi)) |
8008 | (let ((start (c-after-conditional lim))) | |
8009 | (if start | |
8010 | (goto-char start))))) | |
8011 | ||
037558bf | 8012 | (defsubst c-backward-to-decl-anchor (&optional lim) |
a66cd3ee MS |
8013 | ;; Assuming point is at a brace that opens the block of a top level |
8014 | ;; declaration of some kind, move to the proper anchor point for | |
8015 | ;; that block. | |
0386b551 AM |
8016 | ;; |
8017 | ;; This function might do hidden buffer changes. | |
a66cd3ee | 8018 | (unless (= (point) (c-point 'boi)) |
037558bf | 8019 | (c-beginning-of-statement-1 lim))) |
a66cd3ee | 8020 | |
ff959bab | 8021 | (defun c-search-decl-header-end () |
a66cd3ee MS |
8022 | ;; Search forward for the end of the "header" of the current |
8023 | ;; declaration. That's the position where the definition body | |
8024 | ;; starts, or the first variable initializer, or the ending | |
8025 | ;; semicolon. I.e. search forward for the closest following | |
8026 | ;; (syntactically relevant) '{', '=' or ';' token. Point is left | |
8027 | ;; _after_ the first found token, or at point-max if none is found. | |
0386b551 AM |
8028 | ;; |
8029 | ;; This function might do hidden buffer changes. | |
ff959bab MS |
8030 | |
8031 | (let ((base (point))) | |
8032 | (if (c-major-mode-is 'c++-mode) | |
8033 | ||
8034 | ;; In C++ we need to take special care to handle operator | |
8035 | ;; tokens and those pesky template brackets. | |
8036 | (while (and | |
8037 | (c-syntactic-re-search-forward "[;{<=]" nil 'move t t) | |
8038 | (or | |
8039 | (c-end-of-current-token base) | |
8040 | ;; Handle operator identifiers, i.e. ignore any | |
8041 | ;; operator token preceded by "operator". | |
8042 | (save-excursion | |
8043 | (and (c-safe (c-backward-sexp) t) | |
51c9af45 | 8044 | (looking-at c-opt-op-identifier-prefix))) |
ff959bab MS |
8045 | (and (eq (char-before) ?<) |
8046 | (c-with-syntax-table c++-template-syntax-table | |
8047 | (if (c-safe (goto-char (c-up-list-forward (point)))) | |
8048 | t | |
8049 | (goto-char (point-max)) | |
8050 | nil))))) | |
8051 | (setq base (point))) | |
8052 | ||
8053 | (while (and | |
8054 | (c-syntactic-re-search-forward "[;{=]" nil 'move t t) | |
8055 | (c-end-of-current-token base)) | |
8056 | (setq base (point)))))) | |
a66cd3ee MS |
8057 | |
8058 | (defun c-beginning-of-decl-1 (&optional lim) | |
8059 | ;; Go to the beginning of the current declaration, or the beginning | |
8060 | ;; of the previous one if already at the start of it. Point won't | |
0386b551 | 8061 | ;; be moved out of any surrounding paren. Return a cons cell of the |
a66cd3ee MS |
8062 | ;; form (MOVE . KNR-POS). MOVE is like the return value from |
8063 | ;; `c-beginning-of-statement-1'. If point skipped over some K&R | |
8064 | ;; style argument declarations (and they are to be recognized) then | |
8065 | ;; KNR-POS is set to the start of the first such argument | |
8066 | ;; declaration, otherwise KNR-POS is nil. If LIM is non-nil, it's a | |
8067 | ;; position that bounds the backward search. | |
8068 | ;; | |
8069 | ;; NB: Cases where the declaration continues after the block, as in | |
8070 | ;; "struct foo { ... } bar;", are currently recognized as two | |
8071 | ;; declarations, e.g. "struct foo { ... }" and "bar;" in this case. | |
0386b551 AM |
8072 | ;; |
8073 | ;; This function might do hidden buffer changes. | |
a66cd3ee MS |
8074 | (catch 'return |
8075 | (let* ((start (point)) | |
d9e94c22 | 8076 | (last-stmt-start (point)) |
0386b551 | 8077 | (move (c-beginning-of-statement-1 lim nil t))) |
a66cd3ee | 8078 | |
a66cd3ee MS |
8079 | ;; `c-beginning-of-statement-1' stops at a block start, but we |
8080 | ;; want to continue if the block doesn't begin a top level | |
2a15eb73 MS |
8081 | ;; construct, i.e. if it isn't preceded by ';', '}', ':', bob, |
8082 | ;; or an open paren. | |
d9e94c22 | 8083 | (let ((beg (point)) tentative-move) |
51c9af45 AM |
8084 | ;; Go back one "statement" each time round the loop until we're just |
8085 | ;; after a ;, }, or :, or at BOB or the start of a macro or start of | |
8086 | ;; an ObjC method. This will move over a multiple declaration whose | |
8087 | ;; components are comma separated. | |
d9e94c22 MS |
8088 | (while (and |
8089 | ;; Must check with c-opt-method-key in ObjC mode. | |
8090 | (not (and c-opt-method-key | |
8091 | (looking-at c-opt-method-key))) | |
8092 | (/= last-stmt-start (point)) | |
8093 | (progn | |
8094 | (c-backward-syntactic-ws lim) | |
8095 | (not (memq (char-before) '(?\; ?} ?: nil)))) | |
2a15eb73 MS |
8096 | (save-excursion |
8097 | (backward-char) | |
8098 | (not (looking-at "\\s("))) | |
d9e94c22 MS |
8099 | ;; Check that we don't move from the first thing in a |
8100 | ;; macro to its header. | |
8101 | (not (eq (setq tentative-move | |
0386b551 | 8102 | (c-beginning-of-statement-1 lim nil t)) |
d9e94c22 MS |
8103 | 'macro))) |
8104 | (setq last-stmt-start beg | |
8105 | beg (point) | |
8106 | move tentative-move)) | |
8107 | (goto-char beg)) | |
8108 | ||
8109 | (when c-recognize-knr-p | |
8110 | (let ((fallback-pos (point)) knr-argdecl-start) | |
8111 | ;; Handle K&R argdecls. Back up after the "statement" jumped | |
8112 | ;; over by `c-beginning-of-statement-1', unless it was the | |
8113 | ;; function body, in which case we're sitting on the opening | |
8114 | ;; brace now. Then test if we're in a K&R argdecl region and | |
8115 | ;; that we started at the other side of the first argdecl in | |
8116 | ;; it. | |
8117 | (unless (eq (char-after) ?{) | |
8118 | (goto-char last-stmt-start)) | |
8119 | (if (and (setq knr-argdecl-start (c-in-knr-argdecl lim)) | |
8120 | (< knr-argdecl-start start) | |
8121 | (progn | |
8122 | (goto-char knr-argdecl-start) | |
0386b551 | 8123 | (not (eq (c-beginning-of-statement-1 lim nil t) 'macro)))) |
d9e94c22 MS |
8124 | (throw 'return |
8125 | (cons (if (eq (char-after fallback-pos) ?{) | |
8126 | 'previous | |
8127 | 'same) | |
8128 | knr-argdecl-start)) | |
8129 | (goto-char fallback-pos)))) | |
8130 | ||
51c9af45 AM |
8131 | ;; `c-beginning-of-statement-1' counts each brace block as a separate |
8132 | ;; statement, so the result will be 'previous if we've moved over any. | |
8133 | ;; So change our result back to 'same if necessary. | |
8134 | ;; | |
8135 | ;; If they were brace list initializers we might not have moved over a | |
8136 | ;; declaration boundary though, so change it to 'same if we've moved | |
8137 | ;; past a '=' before '{', but not ';'. (This ought to be integrated | |
8138 | ;; into `c-beginning-of-statement-1', so we avoid this extra pass which | |
8139 | ;; potentially can search over a large amount of text.). Take special | |
8140 | ;; pains not to get mislead by C++'s "operator=", and the like. | |
d9e94c22 MS |
8141 | (if (and (eq move 'previous) |
8142 | (c-with-syntax-table (if (c-major-mode-is 'c++-mode) | |
8143 | c++-template-syntax-table | |
8144 | (syntax-table)) | |
8145 | (save-excursion | |
51c9af45 AM |
8146 | (and |
8147 | (progn | |
8148 | (while ; keep going back to "[;={"s until we either find | |
8149 | ; no more, or get to one which isn't an "operator =" | |
8150 | (and (c-syntactic-re-search-forward "[;={]" start t t t) | |
8151 | (eq (char-before) ?=) | |
8152 | c-overloadable-operators-regexp | |
8153 | c-opt-op-identifier-prefix | |
8154 | (save-excursion | |
8155 | (eq (c-backward-token-2) 0) | |
8156 | (looking-at c-overloadable-operators-regexp) | |
8157 | (eq (c-backward-token-2) 0) | |
8158 | (looking-at c-opt-op-identifier-prefix)))) | |
8159 | (eq (char-before) ?=)) | |
8160 | (c-syntactic-re-search-forward "[;{]" start t t) | |
8161 | (eq (char-before) ?{) | |
8162 | (c-safe (goto-char (c-up-list-forward (point))) t) | |
8163 | (not (c-syntactic-re-search-forward ";" start t t)))))) | |
d9e94c22 MS |
8164 | (cons 'same nil) |
8165 | (cons move nil))))) | |
a66cd3ee MS |
8166 | |
8167 | (defun c-end-of-decl-1 () | |
8168 | ;; Assuming point is at the start of a declaration (as detected by | |
8169 | ;; e.g. `c-beginning-of-decl-1'), go to the end of it. Unlike | |
8170 | ;; `c-beginning-of-decl-1', this function handles the case when a | |
8171 | ;; block is followed by identifiers in e.g. struct declarations in C | |
8172 | ;; or C++. If a proper end was found then t is returned, otherwise | |
8173 | ;; point is moved as far as possible within the current sexp and nil | |
8174 | ;; is returned. This function doesn't handle macros; use | |
8175 | ;; `c-end-of-macro' instead in those cases. | |
0386b551 AM |
8176 | ;; |
8177 | ;; This function might do hidden buffer changes. | |
ce8c7486 | 8178 | (let ((start (point)) |
a66cd3ee MS |
8179 | (decl-syntax-table (if (c-major-mode-is 'c++-mode) |
8180 | c++-template-syntax-table | |
8181 | (syntax-table)))) | |
8182 | (catch 'return | |
8183 | (c-search-decl-header-end) | |
8184 | ||
8185 | (when (and c-recognize-knr-p | |
8186 | (eq (char-before) ?\;) | |
8187 | (c-in-knr-argdecl start)) | |
8188 | ;; Stopped at the ';' in a K&R argdecl section which is | |
8189 | ;; detected using the same criteria as in | |
8190 | ;; `c-beginning-of-decl-1'. Move to the following block | |
8191 | ;; start. | |
d9e94c22 | 8192 | (c-syntactic-re-search-forward "{" nil 'move t)) |
a66cd3ee MS |
8193 | |
8194 | (when (eq (char-before) ?{) | |
8195 | ;; Encountered a block in the declaration. Jump over it. | |
8196 | (condition-case nil | |
8197 | (goto-char (c-up-list-forward (point))) | |
d9e94c22 MS |
8198 | (error (goto-char (point-max)) |
8199 | (throw 'return nil))) | |
a66cd3ee MS |
8200 | (if (or (not c-opt-block-decls-with-vars-key) |
8201 | (save-excursion | |
8202 | (c-with-syntax-table decl-syntax-table | |
8203 | (let ((lim (point))) | |
8204 | (goto-char start) | |
b3cf7e18 MS |
8205 | (not (and |
8206 | ;; Check for `c-opt-block-decls-with-vars-key' | |
8207 | ;; before the first paren. | |
8208 | (c-syntactic-re-search-forward | |
d9e94c22 | 8209 | (concat "[;=\(\[{]\\|\\(" |
b3cf7e18 MS |
8210 | c-opt-block-decls-with-vars-key |
8211 | "\\)") | |
d9e94c22 | 8212 | lim t t t) |
b3cf7e18 MS |
8213 | (match-beginning 1) |
8214 | (not (eq (char-before) ?_)) | |
d9e94c22 MS |
8215 | ;; Check that the first following paren is |
8216 | ;; the block. | |
8217 | (c-syntactic-re-search-forward "[;=\(\[{]" | |
8218 | lim t t t) | |
b3cf7e18 | 8219 | (eq (char-before) ?{))))))) |
a66cd3ee MS |
8220 | ;; The declaration doesn't have any of the |
8221 | ;; `c-opt-block-decls-with-vars' keywords in the | |
8222 | ;; beginning, so it ends here at the end of the block. | |
8223 | (throw 'return t))) | |
8224 | ||
8225 | (c-with-syntax-table decl-syntax-table | |
8226 | (while (progn | |
8227 | (if (eq (char-before) ?\;) | |
8228 | (throw 'return t)) | |
d9e94c22 | 8229 | (c-syntactic-re-search-forward ";" nil 'move t)))) |
a66cd3ee | 8230 | nil))) |
ce8c7486 | 8231 | |
0386b551 AM |
8232 | (defun c-looking-at-decl-block (containing-sexp goto-start &optional limit) |
8233 | ;; Assuming the point is at an open brace, check if it starts a | |
8234 | ;; block that contains another declaration level, i.e. that isn't a | |
8235 | ;; statement block or a brace list, and if so return non-nil. | |
8236 | ;; | |
8237 | ;; If the check is successful, the return value is the start of the | |
8238 | ;; keyword that tells what kind of construct it is, i.e. typically | |
8239 | ;; what `c-decl-block-key' matched. Also, if GOTO-START is set then | |
8240 | ;; the point will be at the start of the construct, before any | |
8241 | ;; leading specifiers, otherwise it's at the returned position. | |
8242 | ;; | |
8243 | ;; The point is clobbered if the check is unsuccessful. | |
8244 | ;; | |
8245 | ;; CONTAINING-SEXP is the position of the open of the surrounding | |
8246 | ;; paren, or nil if none. | |
8247 | ;; | |
8248 | ;; The optional LIMIT limits the backward search for the start of | |
8249 | ;; the construct. It's assumed to be at a syntactically relevant | |
8250 | ;; position. | |
8251 | ;; | |
8252 | ;; If any template arglists are found in the searched region before | |
8253 | ;; the open brace, they get marked with paren syntax. | |
8254 | ;; | |
8255 | ;; This function might do hidden buffer changes. | |
8256 | ||
8257 | (let ((open-brace (point)) kwd-start first-specifier-pos) | |
8258 | (c-syntactic-skip-backward c-block-prefix-charset limit t) | |
8259 | ||
8260 | (when (and c-recognize-<>-arglists | |
8261 | (eq (char-before) ?>)) | |
8262 | ;; Could be at the end of a template arglist. | |
8263 | (let ((c-parse-and-markup-<>-arglists t) | |
8264 | (c-disallow-comma-in-<>-arglists | |
8265 | (and containing-sexp | |
8266 | (not (eq (char-after containing-sexp) ?{))))) | |
8267 | (while (and | |
8268 | (c-backward-<>-arglist nil limit) | |
8269 | (progn | |
8270 | (c-syntactic-skip-backward c-block-prefix-charset limit t) | |
8271 | (eq (char-before) ?>)))))) | |
8272 | ||
8273 | ;; Note: Can't get bogus hits inside template arglists below since they | |
8274 | ;; have gotten paren syntax above. | |
8275 | (when (and | |
8276 | ;; If `goto-start' is set we begin by searching for the | |
8277 | ;; first possible position of a leading specifier list. | |
8278 | ;; The `c-decl-block-key' search continues from there since | |
8279 | ;; we know it can't match earlier. | |
8280 | (if goto-start | |
8281 | (when (c-syntactic-re-search-forward c-symbol-start | |
8282 | open-brace t t) | |
8283 | (goto-char (setq first-specifier-pos (match-beginning 0))) | |
8284 | t) | |
8285 | t) | |
8286 | ||
8287 | (cond | |
8288 | ((c-syntactic-re-search-forward c-decl-block-key open-brace t t t) | |
8289 | (goto-char (setq kwd-start (match-beginning 0))) | |
8290 | (or | |
8291 | ||
8292 | ;; Found a keyword that can't be a type? | |
8293 | (match-beginning 1) | |
8294 | ||
8295 | ;; Can be a type too, in which case it's the return type of a | |
8296 | ;; function (under the assumption that no declaration level | |
8297 | ;; block construct starts with a type). | |
8298 | (not (c-forward-type)) | |
8299 | ||
8300 | ;; Jumped over a type, but it could be a declaration keyword | |
8301 | ;; followed by the declared identifier that we've jumped over | |
8302 | ;; instead (e.g. in "class Foo {"). If it indeed is a type | |
8303 | ;; then we should be at the declarator now, so check for a | |
8304 | ;; valid declarator start. | |
8305 | ;; | |
8306 | ;; Note: This doesn't cope with the case when a declared | |
8307 | ;; identifier is followed by e.g. '(' in a language where '(' | |
8308 | ;; also might be part of a declarator expression. Currently | |
8309 | ;; there's no such language. | |
8310 | (not (or (looking-at c-symbol-start) | |
8311 | (looking-at c-type-decl-prefix-key))))) | |
8312 | ||
8313 | ;; In Pike a list of modifiers may be followed by a brace | |
8314 | ;; to make them apply to many identifiers. Note that the | |
8315 | ;; match data will be empty on return in this case. | |
8316 | ((and (c-major-mode-is 'pike-mode) | |
8317 | (progn | |
8318 | (goto-char open-brace) | |
8319 | (= (c-backward-token-2) 0)) | |
8320 | (looking-at c-specifier-key) | |
8321 | ;; Use this variant to avoid yet another special regexp. | |
8322 | (c-keyword-member (c-keyword-sym (match-string 1)) | |
8323 | 'c-modifier-kwds)) | |
8324 | (setq kwd-start (point)) | |
8325 | t))) | |
8326 | ||
8327 | ;; Got a match. | |
8328 | ||
8329 | (if goto-start | |
8330 | ;; Back up over any preceding specifiers and their clauses | |
8331 | ;; by going forward from `first-specifier-pos', which is the | |
8332 | ;; earliest possible position where the specifier list can | |
8333 | ;; start. | |
8334 | (progn | |
8335 | (goto-char first-specifier-pos) | |
8336 | ||
8337 | (while (< (point) kwd-start) | |
8338 | (if (looking-at c-symbol-key) | |
8339 | ;; Accept any plain symbol token on the ground that | |
8340 | ;; it's a specifier masked through a macro (just | |
8341 | ;; like `c-forward-decl-or-cast-1' skip forward over | |
8342 | ;; such tokens). | |
8343 | ;; | |
8344 | ;; Could be more restrictive wrt invalid keywords, | |
8345 | ;; but that'd only occur in invalid code so there's | |
8346 | ;; no use spending effort on it. | |
8347 | (let ((end (match-end 0))) | |
8348 | (unless (c-forward-keyword-clause 0) | |
8349 | (goto-char end) | |
8350 | (c-forward-syntactic-ws))) | |
8351 | ||
8352 | ;; Can't parse a declaration preamble and is still | |
8353 | ;; before `kwd-start'. That means `first-specifier-pos' | |
8354 | ;; was in some earlier construct. Search again. | |
8355 | (if (c-syntactic-re-search-forward c-symbol-start | |
8356 | kwd-start 'move t) | |
8357 | (goto-char (setq first-specifier-pos (match-beginning 0))) | |
8358 | ;; Got no preamble before the block declaration keyword. | |
8359 | (setq first-specifier-pos kwd-start)))) | |
8360 | ||
8361 | (goto-char first-specifier-pos)) | |
8362 | (goto-char kwd-start)) | |
8363 | ||
8364 | kwd-start))) | |
ce8c7486 | 8365 | |
a66cd3ee | 8366 | (defun c-search-uplist-for-classkey (paren-state) |
0386b551 AM |
8367 | ;; Check if the closest containing paren sexp is a declaration |
8368 | ;; block, returning a 2 element vector in that case. Aref 0 | |
8369 | ;; contains the bufpos at boi of the class key line, and aref 1 | |
8370 | ;; contains the bufpos of the open brace. This function is an | |
8371 | ;; obsolete wrapper for `c-looking-at-decl-block'. | |
8372 | ;; | |
8373 | ;; This function might do hidden buffer changes. | |
8374 | (let ((open-paren-pos (c-most-enclosing-brace paren-state))) | |
8375 | (when open-paren-pos | |
8376 | (save-excursion | |
8377 | (goto-char open-paren-pos) | |
8378 | (when (and (eq (char-after) ?{) | |
8379 | (c-looking-at-decl-block | |
8380 | (c-safe-position open-paren-pos paren-state) | |
8381 | nil)) | |
8382 | (back-to-indentation) | |
8383 | (vector (point) open-paren-pos)))))) | |
785eecbb | 8384 | |
020716e1 AM |
8385 | (defmacro c-pull-open-brace (ps) |
8386 | ;; Pull the next open brace from PS (which has the form of paren-state), | |
8387 | ;; skipping over any brace pairs. Returns NIL when PS is exhausted. | |
8388 | `(progn | |
8389 | (while (consp (car ,ps)) | |
8390 | (setq ,ps (cdr ,ps))) | |
8391 | (prog1 (car ,ps) | |
8392 | (setq ,ps (cdr ,ps))))) | |
8393 | ||
8394 | (defun c-most-enclosing-decl-block (paren-state) | |
8395 | ;; Return the buffer position of the most enclosing decl-block brace (in the | |
8396 | ;; sense of c-looking-at-decl-block) in the PAREN-STATE structure, or nil if | |
8397 | ;; none was found. | |
8398 | (let* ((open-brace (c-pull-open-brace paren-state)) | |
8399 | (next-open-brace (c-pull-open-brace paren-state))) | |
8400 | (while (and open-brace | |
8401 | (save-excursion | |
8402 | (goto-char open-brace) | |
8403 | (not (c-looking-at-decl-block next-open-brace nil)))) | |
8404 | (setq open-brace next-open-brace | |
8405 | next-open-brace (c-pull-open-brace paren-state))) | |
8406 | open-brace)) | |
8407 | ||
9657183b AM |
8408 | (defun c-cheap-inside-bracelist-p (paren-state) |
8409 | ;; Return the position of the L-brace if point is inside a brace list | |
8410 | ;; initialization of an array, etc. This is an approximate function, | |
8411 | ;; designed for speed over accuracy. It will not find every bracelist, but | |
8412 | ;; a non-nil result is reliable. We simply search for "= {" (naturally with | |
8413 | ;; syntactic whitespace allowed). PAREN-STATE is the normal thing that it | |
8414 | ;; is everywhere else. | |
8415 | (let (b-pos) | |
8416 | (save-excursion | |
8417 | (while | |
8418 | (and (setq b-pos (c-pull-open-brace paren-state)) | |
8419 | (progn (goto-char b-pos) | |
8420 | (c-backward-sws) | |
8421 | (c-backward-token-2) | |
8422 | (not (looking-at "="))))) | |
8423 | b-pos))) | |
8424 | ||
a66cd3ee | 8425 | (defun c-inside-bracelist-p (containing-sexp paren-state) |
785eecbb RS |
8426 | ;; return the buffer position of the beginning of the brace list |
8427 | ;; statement if we're inside a brace list, otherwise return nil. | |
8428 | ;; CONTAINING-SEXP is the buffer pos of the innermost containing | |
0386b551 | 8429 | ;; paren. PAREN-STATE is the remainder of the state of enclosing |
130c507e | 8430 | ;; braces |
785eecbb RS |
8431 | ;; |
8432 | ;; N.B.: This algorithm can potentially get confused by cpp macros | |
0386b551 | 8433 | ;; placed in inconvenient locations. It's a trade-off we make for |
785eecbb | 8434 | ;; speed. |
0386b551 AM |
8435 | ;; |
8436 | ;; This function might do hidden buffer changes. | |
785eecbb | 8437 | (or |
d9e94c22 | 8438 | ;; This will pick up brace list declarations. |
b2acd789 RS |
8439 | (c-safe |
8440 | (save-excursion | |
8441 | (goto-char containing-sexp) | |
0ec8351b | 8442 | (c-forward-sexp -1) |
b2acd789 | 8443 | (let (bracepos) |
d9e94c22 | 8444 | (if (and (or (looking-at c-brace-list-key) |
0ec8351b | 8445 | (progn (c-forward-sexp -1) |
d9e94c22 | 8446 | (looking-at c-brace-list-key))) |
a66cd3ee | 8447 | (setq bracepos (c-down-list-forward (point))) |
b2acd789 RS |
8448 | (not (c-crosses-statement-barrier-p (point) |
8449 | (- bracepos 2)))) | |
8450 | (point))))) | |
785eecbb RS |
8451 | ;; this will pick up array/aggregate init lists, even if they are nested. |
8452 | (save-excursion | |
0ec8351b BW |
8453 | (let ((class-key |
8454 | ;; Pike can have class definitions anywhere, so we must | |
8455 | ;; check for the class key here. | |
8456 | (and (c-major-mode-is 'pike-mode) | |
a66cd3ee MS |
8457 | c-decl-block-key)) |
8458 | bufpos braceassignp lim next-containing) | |
785eecbb RS |
8459 | (while (and (not bufpos) |
8460 | containing-sexp) | |
a66cd3ee MS |
8461 | (when paren-state |
8462 | (if (consp (car paren-state)) | |
8463 | (setq lim (cdr (car paren-state)) | |
8464 | paren-state (cdr paren-state)) | |
8465 | (setq lim (car paren-state))) | |
8466 | (when paren-state | |
8467 | (setq next-containing (car paren-state) | |
8468 | paren-state (cdr paren-state)))) | |
785eecbb | 8469 | (goto-char containing-sexp) |
a66cd3ee MS |
8470 | (if (c-looking-at-inexpr-block next-containing next-containing) |
8471 | ;; We're in an in-expression block of some kind. Do not | |
8472 | ;; check nesting. We deliberately set the limit to the | |
8473 | ;; containing sexp, so that c-looking-at-inexpr-block | |
8474 | ;; doesn't check for an identifier before it. | |
0ec8351b BW |
8475 | (setq containing-sexp nil) |
8476 | ;; see if the open brace is preceded by = or [...] in | |
8477 | ;; this statement, but watch out for operator= | |
a66cd3ee | 8478 | (setq braceassignp 'dontknow) |
d9e94c22 | 8479 | (c-backward-token-2 1 t lim) |
6393fef2 | 8480 | ;; Checks to do only on the first sexp before the brace. |
d9e94c22 | 8481 | (when (and c-opt-inexpr-brace-list-key |
6393fef2 RS |
8482 | (eq (char-after) ?\[)) |
8483 | ;; In Java, an initialization brace list may follow | |
8484 | ;; directly after "new Foo[]", so check for a "new" | |
8485 | ;; earlier. | |
8486 | (while (eq braceassignp 'dontknow) | |
8487 | (setq braceassignp | |
d9e94c22 MS |
8488 | (cond ((/= (c-backward-token-2 1 t lim) 0) nil) |
8489 | ((looking-at c-opt-inexpr-brace-list-key) t) | |
6393fef2 RS |
8490 | ((looking-at "\\sw\\|\\s_\\|[.[]") |
8491 | ;; Carry on looking if this is an | |
8492 | ;; identifier (may contain "." in Java) | |
8493 | ;; or another "[]" sexp. | |
8494 | 'dontknow) | |
8495 | (t nil))))) | |
8496 | ;; Checks to do on all sexps before the brace, up to the | |
8497 | ;; beginning of the statement. | |
8498 | (while (eq braceassignp 'dontknow) | |
0ec8351b BW |
8499 | (cond ((eq (char-after) ?\;) |
8500 | (setq braceassignp nil)) | |
8501 | ((and class-key | |
8502 | (looking-at class-key)) | |
8503 | (setq braceassignp nil)) | |
8504 | ((eq (char-after) ?=) | |
8505 | ;; We've seen a =, but must check earlier tokens so | |
8506 | ;; that it isn't something that should be ignored. | |
8507 | (setq braceassignp 'maybe) | |
8508 | (while (and (eq braceassignp 'maybe) | |
d9e94c22 | 8509 | (zerop (c-backward-token-2 1 t lim))) |
0ec8351b BW |
8510 | (setq braceassignp |
8511 | (cond | |
8512 | ;; Check for operator = | |
51c9af45 AM |
8513 | ((and c-opt-op-identifier-prefix |
8514 | (looking-at c-opt-op-identifier-prefix)) | |
0386b551 | 8515 | nil) |
130c507e GM |
8516 | ;; Check for `<opchar>= in Pike. |
8517 | ((and (c-major-mode-is 'pike-mode) | |
8518 | (or (eq (char-after) ?`) | |
8519 | ;; Special case for Pikes | |
8520 | ;; `[]=, since '[' is not in | |
8521 | ;; the punctuation class. | |
8522 | (and (eq (char-after) ?\[) | |
8523 | (eq (char-before) ?`)))) | |
8524 | nil) | |
0ec8351b BW |
8525 | ((looking-at "\\s.") 'maybe) |
8526 | ;; make sure we're not in a C++ template | |
8527 | ;; argument assignment | |
a66cd3ee MS |
8528 | ((and |
8529 | (c-major-mode-is 'c++-mode) | |
8530 | (save-excursion | |
8531 | (let ((here (point)) | |
8532 | (pos< (progn | |
8533 | (skip-chars-backward "^<>") | |
8534 | (point)))) | |
8535 | (and (eq (char-before) ?<) | |
8536 | (not (c-crosses-statement-barrier-p | |
8537 | pos< here)) | |
8538 | (not (c-in-literal)) | |
8539 | )))) | |
0ec8351b | 8540 | nil) |
6393fef2 RS |
8541 | (t t)))))) |
8542 | (if (and (eq braceassignp 'dontknow) | |
d9e94c22 | 8543 | (/= (c-backward-token-2 1 t lim) 0)) |
6393fef2 RS |
8544 | (setq braceassignp nil))) |
8545 | (if (not braceassignp) | |
0ec8351b BW |
8546 | (if (eq (char-after) ?\;) |
8547 | ;; Brace lists can't contain a semicolon, so we're done. | |
8548 | (setq containing-sexp nil) | |
a66cd3ee MS |
8549 | ;; Go up one level. |
8550 | (setq containing-sexp next-containing | |
8551 | lim nil | |
8552 | next-containing nil)) | |
0ec8351b BW |
8553 | ;; we've hit the beginning of the aggregate list |
8554 | (c-beginning-of-statement-1 | |
a66cd3ee | 8555 | (c-most-enclosing-brace paren-state)) |
0ec8351b | 8556 | (setq bufpos (point)))) |
a66cd3ee | 8557 | ) |
785eecbb RS |
8558 | bufpos)) |
8559 | )) | |
8560 | ||
0ec8351b BW |
8561 | (defun c-looking-at-special-brace-list (&optional lim) |
8562 | Content-type: text/html