(auto-mode-alist): .f95 files in f90-mode.
[bpt/emacs.git] / lisp / progmodes / cc-engine.el
Content-type: text/html HCoop Git - bpt/emacs.git/blame - lisp/progmodes/cc-engine.el


500 - Internal Server Error

Malformed UTF-8 character (fatal) at (eval 8) line 1, <$fd> line 2855.
CommitLineData
785eecbb
RS
1;;; cc-engine.el --- core syntax guessing engine for CC mode
2
130c507e 3;; Copyright (C) 1985,1987,1992-2001 Free Software Foundation, Inc.
785eecbb 4
ce8c7486
GM
5;; Authors: 2000- Martin Stjernholm
6;; 1998-1999 Barry A. Warsaw and Martin Stjernholm
0ec8351b 7;; 1992-1997 Barry A. Warsaw
785eecbb
RS
8;; 1987 Dave Detlefs and Stewart Clamen
9;; 1985 Richard M. Stallman
0ec8351b 10;; Maintainer: bug-cc-mode@gnu.org
785eecbb 11;; Created: 22-Apr-1997 (split from cc-mode.el)
6430c434 12;; Version: See cc-mode.el
785eecbb
RS
13;; Keywords: c languages oop
14
15;; This file is part of GNU Emacs.
16
17;; GNU Emacs is free software; you can redistribute it and/or modify
18;; it under the terms of the GNU General Public License as published by
19;; the Free Software Foundation; either version 2, or (at your option)
20;; any later version.
21
22;; GNU Emacs is distributed in the hope that it will be useful,
23;; but WITHOUT ANY WARRANTY; without even the implied warranty of
24;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25;; GNU General Public License for more details.
26
27;; You should have received a copy of the GNU General Public License
130c507e
GM
28;; along with this program; see the file COPYING. If not, write to
29;; the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
785eecbb
RS
30;; Boston, MA 02111-1307, USA.
31
3afbc435
PJ
32;;; Commentary:
33
34;;; Code:
35
0ec8351b 36(eval-when-compile
51f606de 37 (let ((load-path
130c507e
GM
38 (if (and (boundp 'byte-compile-dest-file)
39 (stringp byte-compile-dest-file))
40 (cons (file-name-directory byte-compile-dest-file) load-path)
51f606de 41 load-path)))
130c507e
GM
42 (require 'cc-bytecomp)))
43
44(cc-require 'cc-defs)
45(cc-require 'cc-vars)
46(cc-require 'cc-langs)
47
48;; Silence the compiler.
49(cc-bytecomp-defun buffer-syntactic-context) ; XEmacs
0ec8351b 50
51f606de 51\f
130c507e
GM
52(defvar c-state-cache nil)
53(defvar c-in-literal-cache t)
54
64001211
RS
55;; KLUDGE ALERT: c-maybe-labelp is used to pass information between
56;; c-crosses-statement-barrier-p and c-beginning-of-statement-1. A
57;; better way should be implemented, but this will at least shut up
58;; the byte compiler.
59(defvar c-maybe-labelp nil)
60
61;; WARNING WARNING WARNING
62;;
63;; Be *exceptionally* careful about modifications to this function!
64;; Much of CC Mode depends on this Doing The Right Thing. If you
65;; break it you will be sorry. If you think you know how this works,
66;; you probably don't. No human on Earth does! :-)
67;;
68;; WARNING WARNING WARNING
785eecbb
RS
69
70(defun c-beginning-of-statement-1 (&optional lim)
71 ;; move to the start of the current statement, or the previous
72 ;; statement if already at the beginning of one.
73 (let ((firstp t)
74 (substmt-p t)
64001211 75 donep c-in-literal-cache saved
785eecbb
RS
76 (last-begin (point)))
77 ;; first check for bare semicolon
78 (if (and (progn (c-backward-syntactic-ws lim)
79 (eq (char-before) ?\;))
80 (c-safe (progn (forward-char -1)
81 (setq saved (point))
82 t))
83 (progn (c-backward-syntactic-ws lim)
d0c50a29 84 (memq (char-before) '(?\; ?{ ?:)))
785eecbb
RS
85 )
86 (setq last-begin saved)
87 (goto-char last-begin)
88 (while (not donep)
89 ;; stop at beginning of buffer
90 (if (bobp) (setq donep t)
91 ;; go backwards one balanced expression, but be careful of
92 ;; unbalanced paren being reached
0ec8351b 93 (if (not (c-safe (progn (c-backward-sexp 1) t)))
785eecbb
RS
94 (progn
95 (if firstp
96 (backward-up-list 1)
97 (goto-char last-begin))
98 ;; skip over any unary operators, or other special
99 ;; characters appearing at front of identifier
100 (save-excursion
101 (c-backward-syntactic-ws lim)
0ec8351b 102 (skip-chars-backward "-+!*&:.~@ \t\n")
785eecbb
RS
103 (if (eq (char-before) ?\()
104 (setq last-begin (point))))
105 (goto-char last-begin)
ce8c7486 106 (setq donep t)))
785eecbb 107
64001211 108 (setq c-maybe-labelp nil)
785eecbb
RS
109 ;; see if we're in a literal. if not, then this bufpos may be
110 ;; a candidate for stopping
111 (cond
112 ;; CASE 0: did we hit the error condition above?
113 (donep)
114 ;; CASE 1: are we in a literal?
115 ((eq (c-in-literal lim) 'pound)
116 (beginning-of-line))
117 ;; CASE 2: some other kind of literal?
118 ((c-in-literal lim))
119 ;; CASE 3: are we looking at a conditional keyword?
51f606de 120 ((or (and c-conditional-key (looking-at c-conditional-key))
785eecbb
RS
121 (and (eq (char-after) ?\()
122 (save-excursion
0ec8351b 123 (c-forward-sexp 1)
785eecbb
RS
124 (c-forward-syntactic-ws)
125 (not (eq (char-after) ?\;)))
126 (let ((here (point))
127 (foundp (progn
128 (c-backward-syntactic-ws lim)
129 (forward-word -1)
130 (and lim
131 (<= lim (point))
132 (not (c-in-literal lim))
6430c434 133 (not (eq (char-before) ?_))
51f606de 134 c-conditional-key
785eecbb
RS
135 (looking-at c-conditional-key)
136 ))))
137 ;; did we find a conditional?
138 (if (not foundp)
139 (goto-char here))
140 foundp)))
141 ;; are we in the middle of an else-if clause?
142 (if (save-excursion
143 (and (not substmt-p)
0ec8351b 144 (c-safe (progn (c-forward-sexp -1) t))
785eecbb
RS
145 (looking-at "\\<else\\>[ \t\n]+\\<if\\>")
146 (not (c-in-literal lim))))
147 (progn
0ec8351b 148 (c-forward-sexp -1)
785eecbb
RS
149 (c-backward-to-start-of-if lim)))
150 ;; are we sitting at an else clause, that we are not a
151 ;; substatement of?
152 (if (and (not substmt-p)
153 (looking-at "\\<else\\>[^_]"))
154 (c-backward-to-start-of-if lim))
0ec8351b
BW
155 ;; a finally or a series of catches?
156 (if (not substmt-p)
157 (while (looking-at "\\<\\(catch\\|finally\\)\\>[^_]")
158 (c-safe (c-backward-sexp 2))
159 (if (eq (char-after) ?\()
160 (c-safe (c-backward-sexp)))))
785eecbb
RS
161 ;; are we sitting at the while of a do-while?
162 (if (and (looking-at "\\<while\\>[^_]")
163 (c-backward-to-start-of-do lim))
164 (setq substmt-p nil))
165 (setq last-begin (point)
166 donep substmt-p))
51f606de
GM
167 ;; CASE 4: are we looking at a label? (But we handle
168 ;; switch labels later.)
169 ((and (looking-at c-label-key)
ce8c7486
GM
170 (not (looking-at "default\\>"))
171 (not (and (c-major-mode-is 'pike-mode)
172 (save-excursion
173 ;; Not inside a Pike type declaration?
174 (and (c-safe (backward-up-list 1) t)
175 (eq (char-after) ?\()))))))
785eecbb
RS
176 ;; CASE 5: is this the first time we're checking?
177 (firstp (setq firstp nil
178 substmt-p (not (c-crosses-statement-barrier-p
179 (point) last-begin))
180 last-begin (point)))
181 ;; CASE 6: have we crossed a statement barrier?
0ec8351b
BW
182 ((save-excursion
183 ;; Move over in-expression blocks before checking the
184 ;; barrier
185 (if (or (memq (char-after) '(?\( ?\[))
186 (and (eq (char-after) ?{)
187 (c-looking-at-inexpr-block lim)))
188 (c-forward-sexp 1))
189 (c-crosses-statement-barrier-p (point) last-begin))
785eecbb
RS
190 (setq donep t))
191 ;; CASE 7: ignore labels
64001211 192 ((and c-maybe-labelp
785eecbb
RS
193 (or (and c-access-key (looking-at c-access-key))
194 ;; with switch labels, we have to go back further
195 ;; to try to pick up the case or default
196 ;; keyword. Potential bogosity alert: we assume
197 ;; `case' or `default' is first thing on line
198 (let ((here (point)))
199 (beginning-of-line)
51f606de 200 (c-forward-syntactic-ws here)
785eecbb
RS
201 (if (looking-at c-switch-label-key)
202 t
203 (goto-char here)
ce8c7486 204 nil)))))
785eecbb
RS
205 ;; CASE 8: ObjC or Java method def
206 ((and c-method-key
207 (setq last-begin (c-in-method-def-p)))
208 (setq donep t))
ce8c7486
GM
209 ;; CASE 9: Normal token. At bob, we can end up at ws or a
210 ;; comment, and last-begin shouldn't be updated then.
211 ((not (looking-at "\\s \\|/[/*]"))
212 (setq last-begin (point)))
785eecbb
RS
213 ))))
214 (goto-char last-begin)
0ec8351b
BW
215 ;; We always want to skip over the non-whitespace modifier
216 ;; characters that can start a statement.
217 (let ((lim (point)))
130c507e 218 (skip-chars-backward "-+!*&~@`# \t\n" (c-point 'boi))
0ec8351b 219 (skip-chars-forward " \t\n" lim))))
785eecbb
RS
220
221(defun c-end-of-statement-1 ()
041ec7f6
RS
222 (condition-case nil
223 (let (beg end found)
785eecbb 224 (while (and (not (eobp))
041ec7f6
RS
225 (progn
226 (setq beg (point))
0ec8351b 227 (c-forward-sexp 1)
041ec7f6
RS
228 (setq end (point))
229 (goto-char beg)
230 (setq found nil)
231 (while (and (not found)
232 (re-search-forward "[;{}]" end t))
233 (if (not (c-in-literal beg))
234 (setq found t)))
235 (not found)))
236 (goto-char end))
237 (re-search-backward "[;{}]")
785eecbb 238 (forward-char 1))
9796cb5b 239 (error
785eecbb 240 (let ((beg (point)))
9796cb5b 241 (c-safe (backward-up-list -1))
785eecbb
RS
242 (let ((end (point)))
243 (goto-char beg)
9796cb5b
RS
244 (search-forward ";" end 'move)))
245 )))
785eecbb
RS
246
247\f
248(defun c-crosses-statement-barrier-p (from to)
249 ;; Does buffer positions FROM to TO cross a C statement boundary?
250 (let ((here (point))
251 (lim from)
252 crossedp)
253 (condition-case ()
254 (progn
255 (goto-char from)
256 (while (and (not crossedp)
257 (< (point) to))
0ec8351b 258 (skip-chars-forward "^;{}:" (1- to))
785eecbb
RS
259 (if (not (c-in-literal lim))
260 (progn
261 (if (memq (char-after) '(?\; ?{ ?}))
262 (setq crossedp t)
263 (if (eq (char-after) ?:)
64001211 264 (setq c-maybe-labelp t))
785eecbb
RS
265 (forward-char 1))
266 (setq lim (point)))
267 (forward-char 1))))
268 (error (setq crossedp nil)))
269 (goto-char here)
270 crossedp))
271
272\f
130c507e
GM
273(defun c-beginning-of-macro (&optional lim)
274 ;; Go to the beginning of a cpp macro definition. Leaves point at
275 ;; the beginning of the macro and returns t if in a cpp macro
276 ;; definition, otherwise returns nil and leaves point unchanged.
277 ;; `lim' is currently ignored, but the interface requires it.
278 (let ((here (point)))
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 (eq (char-after) ?#))
285 t
286 (goto-char here)
287 nil)))
288
785eecbb
RS
289;; Skipping of "syntactic whitespace", defined as lexical whitespace,
290;; C and C++ style comments, and preprocessor directives. Search no
291;; farther back or forward than optional LIM. If LIM is omitted,
292;; `beginning-of-defun' is used for backward skipping, point-max is
293;; used for forward skipping.
294
295(defun c-forward-syntactic-ws (&optional lim)
296 ;; Forward skip of syntactic whitespace for Emacs 19.
e1c458ae
RS
297 (let* ((here (point-max))
298 (hugenum (point-max)))
299 (while (/= here (point))
300 (setq here (point))
51f606de 301 (c-forward-comment hugenum)
e1c458ae
RS
302 ;; skip preprocessor directives
303 (when (and (eq (char-after) ?#)
785eecbb 304 (= (c-point 'boi) (point)))
ce8c7486
GM
305 (while (and (eq (char-before (c-point 'eol)) ?\\)
306 (= (forward-line 1) 0)))
e1c458ae
RS
307 (end-of-line))
308 )
309 (if lim (goto-char (min (point) lim)))))
310
785eecbb
RS
311(defun c-backward-syntactic-ws (&optional lim)
312 ;; Backward skip over syntactic whitespace for Emacs 19.
e1c458ae
RS
313 (let* ((here (point-min))
314 (hugenum (- (point-max))))
315 (while (/= here (point))
316 (setq here (point))
51f606de 317 (c-forward-comment hugenum)
e1c458ae
RS
318 (c-beginning-of-macro))
319 (if lim (goto-char (max (point) lim)))))
785eecbb
RS
320
321\f
b2acd789 322;; Moving by tokens, where a token is defined as all symbols and
0ec8351b
BW
323;; identifiers which aren't syntactic whitespace (note that "->" is
324;; considered to be two tokens). Point is always either left at the
325;; beginning of a token or not moved at all. COUNT specifies the
326;; number of tokens to move; a negative COUNT moves in the opposite
327;; direction. A COUNT of 0 moves to the next token beginning only if
328;; not already at one. If BALANCED is true, move over balanced
329;; parens, otherwise move into them. Also, if BALANCED is true, never
330;; move out of an enclosing paren. LIM sets the limit for the
331;; movement and defaults to the point limit. Returns the number of
b2acd789 332;; tokens left to move (positive or negative). If BALANCED is true, a
0ec8351b
BW
333;; move over a balanced paren counts as one. Note that if COUNT is 0
334;; and no appropriate token beginning is found, 1 will be returned.
335;; Thus, a return value of 0 guarantees that point is at the requested
336;; position and a return value less (without signs) than COUNT
337;; guarantees that point is at the beginning of some token.
b2acd789
RS
338
339(defun c-forward-token-1 (&optional count balanced lim)
0ec8351b
BW
340 (or count (setq count 1))
341 (if (< count 0)
342 (- (c-backward-token-1 (- count) balanced lim))
343 (let ((jump-syntax (if balanced
344 '(?w ?_ ?\( ?\) ?\" ?\\ ?/ ?$ ?')
345 '(?w ?_ ?\" ?\\ ?/ ?')))
346 (last (point))
347 (prev (point)))
0ec8351b
BW
348 (save-restriction
349 (if lim (narrow-to-region (point-min) lim))
130c507e
GM
350 (if (/= (point)
351 (progn (c-forward-syntactic-ws) (point)))
352 ;; Skip whitespace. Count this as a move if we did in fact
353 ;; move and aren't out of bounds.
354 (or (eobp)
355 (setq count (max (1- count) 0))))
356 (if (and (= count 0)
357 (or (and (memq (char-syntax (or (char-after) ? )) '(?w ?_))
358 (memq (char-syntax (or (char-before) ? )) '(?w ?_)))
359 (eobp)))
360 ;; If count is zero we should jump if in the middle of a
361 ;; token or if there is whitespace between point and the
362 ;; following token beginning.
363 (setq count 1))
0ec8351b
BW
364 (if (eobp)
365 (goto-char last)
130c507e 366 ;; Avoid having the limit tests inside the loop.
0ec8351b
BW
367 (condition-case nil
368 (while (> count 0)
369 (setq prev last
370 last (point))
371 (if (memq (char-syntax (char-after)) jump-syntax)
372 (goto-char (scan-sexps (point) 1))
373 (forward-char))
374 (c-forward-syntactic-ws lim)
375 (setq count (1- count)))
376 (error (goto-char last)))
377 (when (eobp)
378 (goto-char prev)
379 (setq count (1+ count)))))
380 count)))
b2acd789
RS
381
382(defun c-backward-token-1 (&optional count balanced lim)
0ec8351b
BW
383 (or count (setq count 1))
384 (if (< count 0)
385 (- (c-forward-token-1 (- count) balanced lim))
386 (let ((jump-syntax (if balanced
387 '(?w ?_ ?\( ?\) ?\" ?\\ ?/ ?$ ?')
388 '(?w ?_ ?\" ?\\ ?/ ?')))
389 last)
390 (if (and (= count 0)
391 (or (and (memq (char-syntax (or (char-after) ? )) '(?w ?_))
392 (memq (char-syntax (or (char-before) ? )) '(?w ?_)))
393 (/= (point)
394 (save-excursion (c-forward-syntactic-ws) (point)))
395 (eobp)))
396 ;; If count is zero we should jump if in the middle of a
397 ;; token or if there is whitespace between point and the
398 ;; following token beginning.
399 (setq count 1))
0ec8351b
BW
400 (save-restriction
401 (if lim (narrow-to-region lim (point-max)))
402 (or (bobp)
403 (progn
130c507e 404 ;; Avoid having the limit tests inside the loop.
0ec8351b
BW
405 (condition-case nil
406 (while (progn
407 (setq last (point))
408 (> count 0))
409 (c-backward-syntactic-ws lim)
410 (if (memq (char-syntax (char-before)) jump-syntax)
411 (goto-char (scan-sexps (point) -1))
412 (backward-char))
413 (setq count (1- count)))
414 (error (goto-char last)))
415 (if (bobp) (goto-char last)))))
416 count)))
b2acd789
RS
417
418\f
785eecbb
RS
419;; Return `c' if in a C-style comment, `c++' if in a C++ style
420;; comment, `string' if in a string literal, `pound' if on a
421;; preprocessor line, or nil if not in a comment at all. Optional LIM
422;; is used as the backward limit of the search. If omitted, or nil,
423;; `beginning-of-defun' is used."
424
425(defun c-in-literal (&optional lim)
426 ;; Determine if point is in a C++ literal. we cache the last point
427 ;; calculated if the cache is enabled
130c507e 428 (if (and (vectorp c-in-literal-cache)
785eecbb
RS
429 (= (point) (aref c-in-literal-cache 0)))
430 (aref c-in-literal-cache 1)
431 (let ((rtn (save-excursion
432 (let* ((lim (or lim (c-point 'bod)))
785eecbb
RS
433 (state (parse-partial-sexp lim (point))))
434 (cond
435 ((nth 3 state) 'string)
436 ((nth 4 state) (if (nth 7 state) 'c++ 'c))
e1c458ae 437 ((c-beginning-of-macro lim) 'pound)
785eecbb
RS
438 (t nil))))))
439 ;; cache this result if the cache is enabled
130c507e
GM
440 (if (not c-in-literal-cache)
441 (setq c-in-literal-cache (vector (point) rtn)))
785eecbb
RS
442 rtn)))
443
e1c458ae
RS
444;; XEmacs has a built-in function that should make this much quicker.
445;; I don't think we even need the cache, which makes our lives more
446;; complicated anyway. In this case, lim is ignored.
447(defun c-fast-in-literal (&optional lim)
448 (let ((context (buffer-syntactic-context)))
449 (cond
450 ((eq context 'string) 'string)
451 ((eq context 'comment) 'c++)
452 ((eq context 'block-comment) 'c)
453 ((save-excursion (c-beginning-of-macro lim)) 'pound))))
454
455(if (fboundp 'buffer-syntactic-context)
456 (defalias 'c-in-literal 'c-fast-in-literal))
457
51f606de 458(defun c-literal-limits (&optional lim near not-in-delimiter)
e1c458ae
RS
459 ;; Returns a cons of the beginning and end positions of the comment
460 ;; or string surrounding point (including both delimiters), or nil
0ec8351b
BW
461 ;; if point isn't in one. If LIM is non-nil, it's used as the
462 ;; "safe" position to start parsing from. If NEAR is non-nil, then
463 ;; the limits of any literal next to point is returned. "Next to"
464 ;; means there's only [ \t] between point and the literal. The
51f606de
GM
465 ;; search for such a literal is done first in forward direction. If
466 ;; NOT-IN-DELIMITER is non-nil, the case when point is inside a
467 ;; starting delimiter won't be recognized. This only has effect for
468 ;; comments, which have starting delimiters with more than one
469 ;; character.
e1c458ae 470 (save-excursion
0ec8351b
BW
471 (let* ((pos (point))
472 (lim (or lim (c-point 'bod)))
e1c458ae
RS
473 (state (parse-partial-sexp lim (point))))
474 (cond ((nth 3 state)
475 ;; String. Search backward for the start.
476 (while (nth 3 state)
477 (search-backward (make-string 1 (nth 3 state)))
478 (setq state (parse-partial-sexp lim (point))))
0ec8351b 479 (cons (point) (or (c-safe (c-forward-sexp 1) (point))
e1c458ae
RS
480 (point-max))))
481 ((nth 7 state)
0ec8351b 482 ;; Line comment. Search from bol for the comment starter.
e1c458ae
RS
483 (beginning-of-line)
484 (setq state (parse-partial-sexp lim (point))
485 lim (point))
486 (while (not (nth 7 state))
487 (search-forward "//") ; Should never fail.
488 (setq state (parse-partial-sexp
489 lim (point) nil nil state)
490 lim (point)))
491 (backward-char 2)
51f606de 492 (cons (point) (progn (c-forward-comment 1) (point))))
e1c458ae 493 ((nth 4 state)
0ec8351b 494 ;; Block comment. Search backward for the comment starter.
e1c458ae
RS
495 (while (nth 4 state)
496 (search-backward "/*") ; Should never fail.
497 (setq state (parse-partial-sexp lim (point))))
51f606de
GM
498 (cons (point) (progn (c-forward-comment 1) (point))))
499 ((and (not not-in-delimiter)
500 (not (nth 5 state))
501 (eq (char-before) ?/)
502 (looking-at "[/*]"))
e1c458ae 503 ;; We're standing in a comment starter.
51f606de
GM
504 (backward-char 1)
505 (cons (point) (progn (c-forward-comment 1) (point))))
0ec8351b
BW
506 (near
507 (goto-char pos)
508 ;; Search forward for a literal.
509 (skip-chars-forward " \t")
510 (cond
511 ((eq (char-syntax (or (char-after) ?\ )) ?\") ; String.
512 (cons (point) (or (c-safe (c-forward-sexp 1) (point))
513 (point-max))))
514 ((looking-at "/[/*]") ; Line or block comment.
51f606de 515 (cons (point) (progn (c-forward-comment 1) (point))))
0ec8351b
BW
516 (t
517 ;; Search backward.
518 (skip-chars-backward " \t")
519 (let ((end (point)) beg)
520 (cond
521 ((eq (char-syntax (or (char-before) ?\ )) ?\") ; String.
522 (setq beg (c-safe (c-backward-sexp 1) (point))))
523 ((and (c-safe (forward-char -2) t)
524 (looking-at "*/"))
525 ;; Block comment. Due to the nature of line
526 ;; comments, they will always be covered by the
527 ;; normal case above.
528 (goto-char end)
51f606de 529 (c-forward-comment -1)
0ec8351b
BW
530 ;; If LIM is bogus, beg will be bogus.
531 (setq beg (point))))
532 (if beg (cons beg end))))))
e1c458ae
RS
533 ))))
534
51f606de 535(defun c-literal-limits-fast (&optional lim near not-in-delimiter)
0ec8351b 536 ;; Like c-literal-limits, but for emacsen whose `parse-partial-sexp'
51f606de 537 ;; returns the pos of the comment start.
e1c458ae 538 (save-excursion
51f606de
GM
539 (let* ((pos (point))
540 (lim (or lim (c-point 'bod)))
541 (state (parse-partial-sexp lim (point))))
e1c458ae
RS
542 (cond ((nth 3 state) ; String.
543 (goto-char (nth 8 state))
0ec8351b 544 (cons (point) (or (c-safe (c-forward-sexp 1) (point))
e1c458ae
RS
545 (point-max))))
546 ((nth 4 state) ; Comment.
547 (goto-char (nth 8 state))
51f606de
GM
548 (cons (point) (progn (c-forward-comment 1) (point))))
549 ((and (not not-in-delimiter)
550 (not (nth 5 state))
551 (eq (char-before) ?/)
552 (looking-at "[/*]"))
553 ;; We're standing in a comment starter.
554 (backward-char 1)
555 (cons (point) (progn (c-forward-comment 1) (point))))
556 (near
557 (goto-char pos)
558 ;; Search forward for a literal.
559 (skip-chars-forward " \t")
560 (cond
561 ((eq (char-syntax (or (char-after) ?\ )) ?\") ; String.
562 (cons (point) (or (c-safe (c-forward-sexp 1) (point))
563 (point-max))))
564 ((looking-at "/[/*]") ; Line or block comment.
565 (cons (point) (progn (c-forward-comment 1) (point))))
566 (t
567 ;; Search backward.
568 (skip-chars-backward " \t")
569 (let ((end (point)) beg)
570 (cond
571 ((eq (char-syntax (or (char-before) ?\ )) ?\") ; String.
572 (setq beg (c-safe (c-backward-sexp 1) (point))))
573 ((and (c-safe (forward-char -2) t)
574 (looking-at "*/"))
575 ;; Block comment. Due to the nature of line
576 ;; comments, they will always be covered by the
577 ;; normal case above.
578 (goto-char end)
579 (c-forward-comment -1)
580 ;; If LIM is bogus, beg will be bogus.
581 (setq beg (point))))
582 (if beg (cons beg end))))))
e1c458ae
RS
583 ))))
584
51f606de
GM
585(if (c-safe (> (length (save-excursion (parse-partial-sexp 1 1))) 8))
586 (defalias 'c-literal-limits 'c-literal-limits-fast))
587
e1c458ae
RS
588(defun c-collect-line-comments (range)
589 ;; If the argument is a cons of two buffer positions (such as
590 ;; returned by c-literal-limits), and that range contains a C++
591 ;; style line comment, then an extended range is returned that
b2acd789
RS
592 ;; contains all adjacent line comments (i.e. all comments that
593 ;; starts in the same column with no empty lines or non-whitespace
594 ;; characters between them). Otherwise the argument is returned.
e1c458ae
RS
595 (save-excursion
596 (condition-case nil
597 (if (and (consp range) (progn
598 (goto-char (car range))
599 (looking-at "//")))
b2acd789
RS
600 (let ((col (current-column))
601 (beg (point))
130c507e 602 (bopl (c-point 'bopl))
b2acd789 603 (end (cdr range)))
130c507e
GM
604 ;; Got to take care in the backward direction to handle
605 ;; comments which are preceded by code.
51f606de 606 (while (and (c-forward-comment -1)
130c507e 607 (>= (point) bopl)
b2acd789
RS
608 (looking-at "//")
609 (= col (current-column)))
130c507e
GM
610 (setq beg (point)
611 bopl (c-point 'bopl)))
b2acd789 612 (goto-char end)
51f606de
GM
613 (while (and (progn (skip-chars-forward " \t")
614 (looking-at "//"))
615 (= col (current-column))
616 (prog1 (zerop (forward-line 1))
617 (setq end (point)))))
b2acd789 618 (cons beg end))
e1c458ae
RS
619 range)
620 (error range))))
621
0ec8351b
BW
622(defun c-literal-type (range)
623 ;; Convenience function that given the result of c-literal-limits,
624 ;; returns nil or the type of literal that the range surrounds.
625 ;; It's much faster than using c-in-literal and is intended to be
626 ;; used when you need both the type of a literal and its limits.
627 (if (consp range)
51f606de
GM
628 (save-excursion
629 (goto-char (car range))
630 (cond ((eq (char-syntax (or (char-after) ?\ )) ?\") 'string)
631 ((looking-at "//") 'c++)
632 (t 'c))) ; Assuming the range is valid.
0ec8351b
BW
633 range))
634
e1c458ae 635
785eecbb
RS
636\f
637;; utilities for moving and querying around syntactic elements
638(defvar c-parsing-error nil)
639
640(defun c-parse-state ()
641 ;; Finds and records all open parens between some important point
642 ;; earlier in the file and point.
643 ;;
644 ;; if there's a state cache, return it
130c507e 645 (if c-state-cache c-state-cache
785eecbb
RS
646 (let* (at-bob
647 (pos (save-excursion
648 ;; go back 2 bods, but ignore any bogus positions
649 ;; returned by beginning-of-defun (i.e. open paren
650 ;; in column zero)
651 (let ((cnt 2))
652 (while (not (or at-bob (zerop cnt)))
e1c458ae 653 (goto-char (c-point 'bod))
0ec8351b
BW
654 (if (and
655 (eq (char-after) ?\{)
656 ;; The following catches an obscure special
657 ;; case where the brace is preceded by an
658 ;; open paren. That can only legally occur
659 ;; with blocks inside expressions and in
660 ;; Pike special brace lists. Even so, this
661 ;; test is still bogus then, but hopefully
662 ;; good enough. (We don't want to use
663 ;; up-list here since it might be slow.)
664 (save-excursion
665 (c-backward-syntactic-ws)
666 (not (eq (char-before) ?\())))
785eecbb
RS
667 (setq cnt (1- cnt)))
668 (if (bobp)
669 (setq at-bob t))))
670 (point)))
671 (here (save-excursion
672 ;;(skip-chars-forward " \t}")
673 (point)))
130c507e 674 (last-bod here) (last-pos pos)
785eecbb
RS
675 placeholder state sexp-end)
676 ;; cache last bod position
677 (while (catch 'backup-bod
678 (setq state nil)
679 (while (and pos (< pos here))
680 (setq last-pos pos)
681 (if (and (setq pos (c-safe (scan-lists pos 1 -1)))
682 (<= pos here))
683 (progn
684 (setq sexp-end (c-safe (scan-sexps (1- pos) 1)))
685 (if (and sexp-end
686 (<= sexp-end here))
687 ;; we want to record both the start and end
688 ;; of this sexp, but we only want to record
689 ;; the last-most of any of them before here
690 (progn
691 (if (eq (char-after (1- pos)) ?\{)
692 (setq state (cons (cons (1- pos) sexp-end)
693 (if (consp (car state))
694 (cdr state)
695 state))))
696 (setq pos sexp-end))
697 ;; we're contained in this sexp so put pos on
698 ;; front of list
699 (setq state (cons (1- pos) state))))
700 ;; something bad happened. check to see if we
701 ;; crossed an unbalanced close brace. if so, we
702 ;; didn't really find the right `important bufpos'
703 ;; so lets back up and try again
704 (if (and (not pos) (not at-bob)
705 (setq placeholder
706 (c-safe (scan-lists last-pos 1 1)))
707 ;;(char-after (1- placeholder))
708 (<= placeholder here)
709 (eq (char-after (1- placeholder)) ?\}))
710 (while t
244db8a2 711 (setq last-bod (c-safe (scan-lists last-pos -1 1)))
785eecbb 712 (if (not last-bod)
130c507e 713 (save-excursion
785eecbb 714 ;; bogus, but what can we do here?
130c507e
GM
715 (goto-char placeholder)
716 (beginning-of-line)
717 (setq c-parsing-error
718 (format "\
719Unbalanced close brace at line %d" (1+ (count-lines 1 (point)))))
785eecbb
RS
720 (throw 'backup-bod nil))
721 (setq at-bob (= last-bod (point-min))
722 pos last-bod)
723 (if (= (char-after last-bod) ?\{)
724 (throw 'backup-bod t)))
725 )) ;end-if
726 )) ;end-while
727 nil))
728 state)))
729
730(defun c-whack-state (bufpos state)
731 ;; whack off any state information that appears on STATE which lies
732 ;; after the bounds of BUFPOS.
733 (let (newstate car)
734 (while state
735 (setq car (car state)
736 state (cdr state))
737 (if (consp car)
738 ;; just check the car, because in a balanced brace
739 ;; expression, it must be impossible for the corresponding
740 ;; close brace to be before point, but the open brace to be
741 ;; after.
742 (if (<= bufpos (car car))
743 nil ; whack it off
744 ;; its possible that the open brace is before bufpos, but
745 ;; the close brace is after. In that case, convert this
746 ;; to a non-cons element.
747 (if (<= bufpos (cdr car))
748 (setq newstate (append newstate (list (car car))))
749 ;; we know that both the open and close braces are
750 ;; before bufpos, so we also know that everything else
751 ;; on state is before bufpos, so we can glom up the
752 ;; whole thing and exit.
753 (setq newstate (append newstate (list car) state)
754 state nil)))
755 (if (<= bufpos car)
756 nil ; whack it off
757 ;; it's before bufpos, so everything else should too
758 (setq newstate (append newstate (list car) state)
759 state nil))))
760 newstate))
761
762(defun c-hack-state (bufpos which state)
763 ;; Using BUFPOS buffer position, and WHICH (must be 'open or
764 ;; 'close), hack the c-parse-state STATE and return the results.
765 (if (eq which 'open)
766 (let ((car (car state)))
767 (if (or (null car)
768 (consp car)
769 (/= bufpos car))
770 (cons bufpos state)
771 state))
772 (if (not (eq which 'close))
773 (error "c-hack-state, bad argument: %s" which))
774 ;; 'close brace
775 (let ((car (car state))
776 (cdr (cdr state)))
777 (if (consp car)
778 (setq car (car cdr)
779 cdr (cdr cdr)))
780 ;; TBD: is this test relevant???
781 (if (consp car)
782 state ;on error, don't change
783 ;; watch out for balanced expr already on cdr of list
784 (cons (cons car bufpos)
785 (if (consp (car cdr))
786 (cdr cdr) cdr))
787 ))))
788
789(defun c-adjust-state (from to shift state)
790 ;; Adjust all points in state that lie in the region FROM..TO by
0ec8351b 791 ;; SHIFT amount.
785eecbb
RS
792 (mapcar
793 (function
794 (lambda (e)
795 (if (consp e)
796 (let ((car (car e))
797 (cdr (cdr e)))
798 (if (and (<= from car) (< car to))
799 (setcar e (+ shift car)))
800 (if (and (<= from cdr) (< cdr to))
801 (setcdr e (+ shift cdr))))
802 (if (and (<= from e) (< e to))
803 (setq e (+ shift e))))
804 e))
805 state))
806
807\f
808(defun c-beginning-of-inheritance-list (&optional lim)
809 ;; Go to the first non-whitespace after the colon that starts a
810 ;; multiple inheritance introduction. Optional LIM is the farthest
811 ;; back we should search.
ce8c7486
GM
812 (let* ((lim (or lim (c-point 'bod)))
813 (placeholder (progn
814 (back-to-indentation)
815 (point)))
816 (chr (char-after)))
785eecbb
RS
817 (c-backward-syntactic-ws lim)
818 (while (and (> (point) lim)
ce8c7486
GM
819 (or (eq chr ?,)
820 (memq (char-before) '(?, ?:)))
785eecbb
RS
821 (progn
822 (beginning-of-line)
823 (setq placeholder (point))
824 (skip-chars-forward " \t")
ce8c7486 825 (setq chr (char-after))
785eecbb
RS
826 (not (looking-at c-class-key))
827 ))
828 (c-backward-syntactic-ws lim))
829 (goto-char placeholder)
830 (skip-chars-forward "^:" (c-point 'eol))))
831
785eecbb
RS
832(defun c-in-method-def-p ()
833 ;; Return nil if we aren't in a method definition, otherwise the
834 ;; position of the initial [+-].
835 (save-excursion
836 (beginning-of-line)
837 (and c-method-key
838 (looking-at c-method-key)
839 (point))
840 ))
841
abb7e5cf
SM
842(defun c-at-toplevel-p ()
843 "Return a determination as to whether point is at the `top-level'.
844Being at the top-level means that point is either outside any
845enclosing block (such function definition), or inside a class
846definition, but outside any method blocks.
847
848If point is not at the top-level (e.g. it is inside a method
849definition), then nil is returned. Otherwise, if point is at a
850top-level not enclosed within a class definition, t is returned.
851Otherwise, a 2-vector is returned where the zeroth element is the
852buffer position of the start of the class declaration, and the first
853element is the buffer position of the enclosing class's opening
854brace."
855 (let ((state (c-parse-state)))
856 (or (not (c-most-enclosing-brace state))
857 (c-search-uplist-for-classkey state))))
858
785eecbb
RS
859(defun c-just-after-func-arglist-p (&optional containing)
860 ;; Return t if we are between a function's argument list closing
861 ;; paren and its opening brace. Note that the list close brace
862 ;; could be followed by a "const" specifier or a member init hanging
863 ;; colon. Optional CONTAINING is position of containing s-exp open
864 ;; brace. If not supplied, point is used as search start.
865 (save-excursion
866 (c-backward-syntactic-ws)
867 (let ((checkpoint (or containing (point))))
868 (goto-char checkpoint)
869 ;; could be looking at const specifier
870 (if (and (eq (char-before) ?t)
871 (forward-word -1)
872 (looking-at "\\<const\\>"))
873 (c-backward-syntactic-ws)
874 ;; otherwise, we could be looking at a hanging member init
875 ;; colon
876 (goto-char checkpoint)
ce8c7486
GM
877 (while (eq (char-before) ?,)
878 ;; this will catch member inits with multiple
879 ;; line arglists
880 (forward-char -1)
881 (c-backward-syntactic-ws (c-point 'bol))
882 (if (eq (char-before) ?\))
883 (c-backward-sexp 2)
884 (c-backward-sexp 1))
885 (c-backward-syntactic-ws))
785eecbb
RS
886 (if (and (eq (char-before) ?:)
887 (progn
888 (forward-char -1)
889 (c-backward-syntactic-ws)
890 (looking-at "[ \t\n]*:\\([^:]+\\|$\\)")))
891 nil
892 (goto-char checkpoint))
893 )
894 (and (eq (char-before) ?\))
895 ;; check if we are looking at a method def
896 (or (not c-method-key)
897 (progn
0ec8351b 898 (c-forward-sexp -1)
785eecbb
RS
899 (forward-char -1)
900 (c-backward-syntactic-ws)
901 (not (or (memq (char-before) '(?- ?+))
902 ;; or a class category
903 (progn
0ec8351b 904 (c-forward-sexp -2)
785eecbb
RS
905 (looking-at c-class-key))
906 )))))
907 )))
908
909;; defuns to look backwards for things
910(defun c-backward-to-start-of-do (&optional lim)
911 ;; Move to the start of the last "unbalanced" do expression.
912 ;; Optional LIM is the farthest back to search. If none is found,
913 ;; nil is returned and point is left unchanged, otherwise t is returned.
914 (let ((do-level 1)
915 (case-fold-search nil)
916 (lim (or lim (c-point 'bod)))
917 (here (point))
918 foundp)
919 (while (not (zerop do-level))
920 ;; we protect this call because trying to execute this when the
921 ;; while is not associated with a do will throw an error
922 (condition-case nil
923 (progn
0ec8351b 924 (c-backward-sexp 1)
785eecbb 925 (cond
e1c458ae
RS
926 ;; break infloop for illegal C code
927 ((bobp) (setq do-level 0))
785eecbb
RS
928 ((memq (c-in-literal lim) '(c c++)))
929 ((looking-at "while\\b[^_]")
930 (setq do-level (1+ do-level)))
931 ((looking-at "do\\b[^_]")
932 (if (zerop (setq do-level (1- do-level)))
933 (setq foundp t)))
934 ((<= (point) lim)
935 (setq do-level 0)
936 (goto-char lim))))
937 (error
938 (goto-char lim)
939 (setq do-level 0))))
940 (if (not foundp)
941 (goto-char here))
942 foundp))
943
944(defun c-backward-to-start-of-if (&optional lim)
945 ;; Move to the start of the last "unbalanced" if and return t. If
946 ;; none is found, and we are looking at an if clause, nil is
130c507e 947 ;; returned.
785eecbb
RS
948 (let ((if-level 1)
949 (here (c-point 'bol))
950 (case-fold-search nil)
130c507e 951 (lim (or (and lim (>= (point) lim) lim)
e1c458ae 952 (c-point 'bod)))
785eecbb
RS
953 (at-if (looking-at "if\\b[^_]")))
954 (catch 'orphan-if
955 (while (and (not (bobp))
956 (not (zerop if-level)))
957 (c-backward-syntactic-ws)
958 (condition-case nil
0ec8351b 959 (c-backward-sexp 1)
785eecbb 960 (error
130c507e
GM
961 (unless at-if
962 (goto-char here)
963 (c-beginning-of-statement-1)
964 (setq c-parsing-error
965 (format "No matching `if' found for `else' on line %d"
2f85befd 966 (1+ (count-lines (point-min) here))))
130c507e 967 (throw 'orphan-if nil))))
785eecbb
RS
968 (cond
969 ((looking-at "else\\b[^_]")
970 (setq if-level (1+ if-level)))
971 ((looking-at "if\\b[^_]")
972 ;; check for else if... skip over
973 (let ((here (point)))
0ec8351b 974 (c-safe (c-forward-sexp -1))
130c507e 975 (if (looking-at "\\<else\\>[ \t]+\\<if\\>[^_]")
785eecbb
RS
976 nil
977 (setq if-level (1- if-level))
978 (goto-char here))))
979 ((< (point) lim)
980 (setq if-level 0)
981 (goto-char lim))
982 ))
983 t)))
984
985(defun c-skip-conditional ()
986 ;; skip forward over conditional at point, including any predicate
987 ;; statements in parentheses. No error checking is performed.
0ec8351b
BW
988 (c-forward-sexp (cond
989 ;; else if()
130c507e 990 ((looking-at "\\<else\\>[ \t]+\\<if\\>\\([^_]\\|$\\)") 3)
0ec8351b 991 ;; do, else, try, finally
130c507e
GM
992 ((looking-at
993 "\\<\\(do\\|else\\|try\\|finally\\)\\>\\([^_]\\|$\\)")
994 1)
ce8c7486 995 ;; for, if, while, switch, catch, synchronized, foreach
0ec8351b 996 (t 2))))
785eecbb 997
ce8c7486
GM
998(defun c-beginning-of-closest-statement (&optional lim)
999 ;; Go back to the closest preceding statement start.
1000 (let ((start (point))
1001 (label-re (concat c-label-key "\\|"
1002 c-switch-label-key))
1003 stmtbeg)
1004 (if c-access-key
1005 (setq label-re (concat label-re "\\|" c-access-key)))
1006 (c-beginning-of-statement-1 lim)
1007 (while (and (when (<= (point) start)
1008 (setq stmtbeg (point)))
1009 (cond
1010 ((looking-at label-re)
1011 ;; Skip a label.
1012 (goto-char (match-end 0))
1013 t)
1014 ((looking-at c-conditional-key)
1015 ;; Skip a conditional statement.
1016 (c-safe (c-skip-conditional) t))
1017 (t nil)))
1018 (c-forward-syntactic-ws start))
1019 (if stmtbeg
1020 (goto-char stmtbeg))))
1021
1022(defun c-beginning-of-member-init-list (&optional limit)
1023 ;; Goes to the beginning of a member init list (i.e. just after the
1024 ;; ':') if inside one. Returns t in that case, nil otherwise.
1025 (or limit
1026 (setq limit (point-min)))
1027 (skip-chars-forward " \t")
1028 (if (eq (char-after) ?,)
1029 (forward-char 1)
1030 (c-backward-syntactic-ws limit))
174acba3
SM
1031 (while (and (< limit (point))
1032 (eq (char-before) ?,))
1033 ;; this will catch member inits with multiple
1034 ;; line arglists
1035 (forward-char -1)
1036 (c-backward-syntactic-ws limit)
1037 (if (eq (char-before) ?\))
130c507e 1038 (c-backward-sexp 1))
174acba3
SM
1039 (c-backward-syntactic-ws limit)
1040 ;; Skip over any template arg to the class.
1041 (if (eq (char-before) ?>)
1042 (c-with-syntax-table c++-template-syntax-table
1043 (c-backward-sexp 1)))
1044 (c-backward-sexp 1)
1045 (c-backward-syntactic-ws limit)
1046 ;; Skip backwards over a fully::qualified::name.
1047 (while (and (eq (char-before) ?:)
1048 (save-excursion
1049 (forward-char -1)
1050 (eq (char-before) ?:)))
1051 (backward-char 2)
1052 (c-backward-sexp 1))
1053 ;; now continue checking
1054 (c-backward-syntactic-ws limit))
ce8c7486
GM
1055 (and (< limit (point))
1056 (eq (char-before) ?:)))
1057
785eecbb
RS
1058(defun c-skip-case-statement-forward (state &optional lim)
1059 ;; skip forward over case/default bodies, with optional maximal
1060 ;; limit. if no next case body is found, nil is returned and point
1061 ;; is not moved
1062 (let ((lim (or lim (point-max)))
1063 (here (point))
1064 donep foundp bufpos
1065 (safepos (point))
1066 (balanced (car state)))
1067 ;; search until we've passed the limit, or we've found our match
1068 (while (and (< (point) lim)
1069 (not donep))
1070 (setq safepos (point))
1071 ;; see if we can find a case statement, not in a literal
1072 (if (and (re-search-forward c-switch-label-key lim 'move)
1073 (setq bufpos (match-beginning 0))
1074 (not (c-in-literal safepos))
1075 (/= bufpos here))
1076 ;; if we crossed into a balanced sexp, we know the case is
1077 ;; not part of our switch statement, so just bound over the
1078 ;; sexp and keep looking.
1079 (if (and (consp balanced)
1080 (> bufpos (car balanced))
1081 (< bufpos (cdr balanced)))
1082 (goto-char (cdr balanced))
1083 (goto-char bufpos)
1084 (setq donep t
1085 foundp t))))
1086 (if (not foundp)
1087 (goto-char here))
1088 foundp))
1089
1090(defun c-search-uplist-for-classkey (brace-state)
1091 ;; search for the containing class, returning a 2 element vector if
0ec8351b
BW
1092 ;; found. aref 0 contains the bufpos of the boi of the class key
1093 ;; line, and aref 1 contains the bufpos of the open brace.
785eecbb
RS
1094 (if (null brace-state)
1095 ;; no brace-state means we cannot be inside a class
1096 nil
1097 (let ((carcache (car brace-state))
1098 search-start search-end)
1099 (if (consp carcache)
1100 ;; a cons cell in the first element means that there is some
1101 ;; balanced sexp before the current bufpos. this we can
1102 ;; ignore. the nth 1 and nth 2 elements define for us the
1103 ;; search boundaries
1104 (setq search-start (nth 2 brace-state)
1105 search-end (nth 1 brace-state))
1106 ;; if the car was not a cons cell then nth 0 and nth 1 define
1107 ;; for us the search boundaries
1108 (setq search-start (nth 1 brace-state)
1109 search-end (nth 0 brace-state)))
1110 ;; search-end cannot be a cons cell
1111 (and (consp search-end)
1112 (error "consp search-end: %s" search-end))
1113 ;; if search-end is nil, or if the search-end character isn't an
1114 ;; open brace, we are definitely not in a class
1115 (if (or (not search-end)
1116 (< search-end (point-min))
1117 (not (eq (char-after search-end) ?{)))
1118 nil
1119 ;; now, we need to look more closely at search-start. if
1120 ;; search-start is nil, then our start boundary is really
1121 ;; point-min.
1122 (if (not search-start)
1123 (setq search-start (point-min))
1124 ;; if search-start is a cons cell, then we can start
1125 ;; searching from the end of the balanced sexp just ahead of
1126 ;; us
1127 (if (consp search-start)
1128 (setq search-start (cdr search-start))))
1129 ;; now we can do a quick regexp search from search-start to
1130 ;; search-end and see if we can find a class key. watch for
1131 ;; class like strings in literals
1132 (save-excursion
1133 (save-restriction
1134 (goto-char search-start)
e1c458ae 1135 (let ((search-key (concat c-class-key "\\|" c-extra-toplevel-key))
785eecbb 1136 foundp class match-end)
0ec8351b
BW
1137 (if c-inexpr-class-key
1138 (setq search-key (concat search-key "\\|"
1139 c-inexpr-class-key)))
785eecbb
RS
1140 (while (and (not foundp)
1141 (progn
1142 (c-forward-syntactic-ws)
1143 (> search-end (point)))
1144 (re-search-forward search-key search-end t))
1145 (setq class (match-beginning 0)
1146 match-end (match-end 0))
1147 (if (c-in-literal search-start)
1148 nil ; its in a comment or string, ignore
1149 (goto-char class)
1150 (skip-chars-forward " \t\n")
1151 (setq foundp (vector (c-point 'boi) search-end))
1152 (cond
1153 ;; check for embedded keywords
1154 ((let ((char (char-after (1- class))))
1155 (and char
1156 (memq (char-syntax char) '(?w ?_))))
1157 (goto-char match-end)
1158 (setq foundp nil))
1159 ;; make sure we're really looking at the start of a
1160 ;; class definition, and not a forward decl, return
1161 ;; arg, template arg list, or an ObjC or Java method.
1162 ((and c-method-key
0ec8351b
BW
1163 (re-search-forward c-method-key search-end t)
1164 (not (c-in-literal class)))
785eecbb 1165 (setq foundp nil))
0ec8351b
BW
1166 ;; Check if this is an anonymous inner class.
1167 ((and c-inexpr-class-key
1168 (looking-at c-inexpr-class-key))
1169 (while (and (= (c-forward-token-1 1 t) 0)
1170 (looking-at "(\\|\\w\\|\\s_\\|\\.")))
1171 (if (eq (point) search-end)
1172 ;; We're done. Just trap this case in the cond.
1173 nil
1174 ;; False alarm; all conditions aren't satisfied.
1175 (setq foundp nil)))
785eecbb
RS
1176 ;; Its impossible to define a regexp for this, and
1177 ;; nearly so to do it programmatically.
1178 ;;
1179 ;; ; picks up forward decls
1180 ;; = picks up init lists
1181 ;; ) picks up return types
1182 ;; > picks up templates, but remember that we can
1183 ;; inherit from templates!
1184 ((let ((skipchars "^;=)"))
1185 ;; try to see if we found the `class' keyword
1186 ;; inside a template arg list
1187 (save-excursion
1188 (skip-chars-backward "^<>" search-start)
1189 (if (eq (char-before) ?<)
1190 (setq skipchars (concat skipchars ">"))))
0ec8351b
BW
1191 (while (progn
1192 (skip-chars-forward skipchars search-end)
1193 (c-in-literal class))
1194 (forward-char))
785eecbb
RS
1195 (/= (point) search-end))
1196 (setq foundp nil))
1197 )))
1198 foundp))
1199 )))))
1200
1201(defun c-inside-bracelist-p (containing-sexp brace-state)
1202 ;; return the buffer position of the beginning of the brace list
1203 ;; statement if we're inside a brace list, otherwise return nil.
1204 ;; CONTAINING-SEXP is the buffer pos of the innermost containing
130c507e
GM
1205 ;; paren. BRACE-STATE is the remainder of the state of enclosing
1206 ;; braces
785eecbb
RS
1207 ;;
1208 ;; N.B.: This algorithm can potentially get confused by cpp macros
1209 ;; places in inconvenient locations. Its a trade-off we make for
1210 ;; speed.
1211 (or
1212 ;; this will pick up enum lists
b2acd789
RS
1213 (c-safe
1214 (save-excursion
1215 (goto-char containing-sexp)
0ec8351b 1216 (c-forward-sexp -1)
b2acd789
RS
1217 (let (bracepos)
1218 (if (and (or (looking-at "enum[\t\n ]+")
0ec8351b 1219 (progn (c-forward-sexp -1)
b2acd789
RS
1220 (looking-at "enum[\t\n ]+")))
1221 (setq bracepos (c-safe (scan-lists (point) 1 -1)))
1222 (not (c-crosses-statement-barrier-p (point)
1223 (- bracepos 2))))
1224 (point)))))
785eecbb
RS
1225 ;; this will pick up array/aggregate init lists, even if they are nested.
1226 (save-excursion
0ec8351b
BW
1227 (let ((class-key
1228 ;; Pike can have class definitions anywhere, so we must
1229 ;; check for the class key here.
1230 (and (c-major-mode-is 'pike-mode)
1231 (concat c-class-key "\\|" c-extra-toplevel-key)))
1232 bufpos lim braceassignp)
785eecbb
RS
1233 (while (and (not bufpos)
1234 containing-sexp)
1235 (if (consp containing-sexp)
1236 (setq containing-sexp (car brace-state)
1237 brace-state (cdr brace-state))
785eecbb 1238 (goto-char containing-sexp)
0ec8351b
BW
1239 (if (c-looking-at-inexpr-block)
1240 ;; We're in an in-expression block of some kind. Do
1241 ;; not check nesting.
1242 (setq containing-sexp nil)
1243 ;; see if the open brace is preceded by = or [...] in
1244 ;; this statement, but watch out for operator=
1245 (setq lim (if (consp (car brace-state))
1246 (cdr (car brace-state))
1247 (car brace-state))
1248 braceassignp 'dontknow)
6393fef2
RS
1249 (c-backward-token-1 1 t lim)
1250 ;; Checks to do only on the first sexp before the brace.
1251 (when (and (c-major-mode-is 'java-mode)
1252 (eq (char-after) ?\[))
1253 ;; In Java, an initialization brace list may follow
1254 ;; directly after "new Foo[]", so check for a "new"
1255 ;; earlier.
1256 (while (eq braceassignp 'dontknow)
1257 (setq braceassignp
1258 (cond ((/= (c-backward-token-1 1 t lim) 0) nil)
130c507e 1259 ((looking-at "new\\>[^_]") t)
6393fef2
RS
1260 ((looking-at "\\sw\\|\\s_\\|[.[]")
1261 ;; Carry on looking if this is an
1262 ;; identifier (may contain "." in Java)
1263 ;; or another "[]" sexp.
1264 'dontknow)
1265 (t nil)))))
1266 ;; Checks to do on all sexps before the brace, up to the
1267 ;; beginning of the statement.
1268 (while (eq braceassignp 'dontknow)
0ec8351b
BW
1269 (cond ((eq (char-after) ?\;)
1270 (setq braceassignp nil))
1271 ((and class-key
1272 (looking-at class-key))
1273 (setq braceassignp nil))
1274 ((eq (char-after) ?=)
1275 ;; We've seen a =, but must check earlier tokens so
1276 ;; that it isn't something that should be ignored.
1277 (setq braceassignp 'maybe)
1278 (while (and (eq braceassignp 'maybe)
1279 (zerop (c-backward-token-1 1 t lim)))
1280 (setq braceassignp
1281 (cond
1282 ;; Check for operator =
1283 ((looking-at "operator\\>") nil)
130c507e
GM
1284 ;; Check for `<opchar>= in Pike.
1285 ((and (c-major-mode-is 'pike-mode)
1286 (or (eq (char-after) ?`)
1287 ;; Special case for Pikes
1288 ;; `[]=, since '[' is not in
1289 ;; the punctuation class.
1290 (and (eq (char-after) ?\[)
1291 (eq (char-before) ?`))))
1292 nil)
0ec8351b
BW
1293 ((looking-at "\\s.") 'maybe)
1294 ;; make sure we're not in a C++ template
1295 ;; argument assignment
130c507e
GM
1296 ((and (c-major-mode-is 'c++-mode)
1297 (save-excursion
1298 (let ((here (point))
1299 (pos< (progn
1300 (skip-chars-backward "^<>")
1301 (point))))
1302 (and (eq (char-before) ?<)
1303 (not (c-crosses-statement-barrier-p
1304 pos< here))
1305 (not (c-in-literal))
1306 ))))
0ec8351b 1307 nil)
6393fef2
RS
1308 (t t))))))
1309 (if (and (eq braceassignp 'dontknow)
1310 (/= (c-backward-token-1 1 t lim) 0))
1311 (setq braceassignp nil)))
1312 (if (not braceassignp)
0ec8351b
BW
1313 (if (eq (char-after) ?\;)
1314 ;; Brace lists can't contain a semicolon, so we're done.
1315 (setq containing-sexp nil)
1316 ;; lets see if we're nested. find the most nested
1317 ;; containing brace
1318 (setq containing-sexp (car brace-state)
1319 brace-state (cdr brace-state)))
1320 ;; we've hit the beginning of the aggregate list
1321 (c-beginning-of-statement-1
1322 (c-most-enclosing-brace brace-state))
1323 (setq bufpos (point))))
785eecbb
RS
1324 ))
1325 bufpos))
1326 ))
1327
0ec8351b
BW
1328(defun c-looking-at-special-brace-list (&optional lim)
1329