]> code.delx.au - gnu-emacs/blob - lisp/progmodes/cc-engine.el
(sh-mode-syntax-table): Function restored.
[gnu-emacs] / lisp / progmodes / cc-engine.el
1 ;;; cc-engine.el --- core syntax guessing engine for CC mode
2
3 ;; Copyright (C) 1985,1987,1992-2001 Free Software Foundation, Inc.
4
5 ;; Authors: 2000- Martin Stjernholm
6 ;; 1998-1999 Barry A. Warsaw and Martin Stjernholm
7 ;; 1992-1997 Barry A. Warsaw
8 ;; 1987 Dave Detlefs and Stewart Clamen
9 ;; 1985 Richard M. Stallman
10 ;; Maintainer: bug-cc-mode@gnu.org
11 ;; Created: 22-Apr-1997 (split from cc-mode.el)
12 ;; Version: See cc-mode.el
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
28 ;; along with this program; see the file COPYING. If not, write to
29 ;; the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
30 ;; Boston, MA 02111-1307, USA.
31
32 ;;; Commentary:
33
34 ;;; Code:
35
36 (eval-when-compile
37 (let ((load-path
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)
41 load-path)))
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
50
51 \f
52 (defvar c-state-cache nil)
53 (defvar c-in-literal-cache t)
54
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
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)
75 donep c-in-literal-cache saved
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)
84 (memq (char-before) '(?\; ?{ ?:)))
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
93 (if (not (c-safe (progn (c-backward-sexp 1) t)))
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)
102 (skip-chars-backward "-+!*&:.~@ \t\n")
103 (if (eq (char-before) ?\()
104 (setq last-begin (point))))
105 (goto-char last-begin)
106 (setq donep t)))
107
108 (setq c-maybe-labelp nil)
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?
120 ((or (and c-conditional-key (looking-at c-conditional-key))
121 (and (eq (char-after) ?\()
122 (save-excursion
123 (c-forward-sexp 1)
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))
133 (not (eq (char-before) ?_))
134 c-conditional-key
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)
144 (c-safe (progn (c-forward-sexp -1) t))
145 (looking-at "\\<else\\>[ \t\n]+\\<if\\>")
146 (not (c-in-literal lim))))
147 (progn
148 (c-forward-sexp -1)
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))
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)))))
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))
167 ;; CASE 4: are we looking at a label? (But we handle
168 ;; switch labels later.)
169 ((and (looking-at c-label-key)
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) ?\()))))))
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?
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))
190 (setq donep t))
191 ;; CASE 7: ignore labels
192 ((and c-maybe-labelp
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)
200 (c-forward-syntactic-ws here)
201 (if (looking-at c-switch-label-key)
202 t
203 (goto-char here)
204 nil)))))
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))
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)))
213 ))))
214 (goto-char last-begin)
215 ;; We always want to skip over the non-whitespace modifier
216 ;; characters that can start a statement.
217 (let ((lim (point)))
218 (skip-chars-backward "-+!*&~@`# \t\n" (c-point 'boi))
219 (skip-chars-forward " \t\n" lim))))
220
221 (defun c-end-of-statement-1 ()
222 (condition-case nil
223 (let (beg end found)
224 (while (and (not (eobp))
225 (progn
226 (setq beg (point))
227 (c-forward-sexp 1)
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 "[;{}]")
238 (forward-char 1))
239 (error
240 (let ((beg (point)))
241 (c-safe (backward-up-list -1))
242 (let ((end (point)))
243 (goto-char beg)
244 (search-forward ";" end 'move)))
245 )))
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))
258 (skip-chars-forward "^;{}:" (1- to))
259 (if (not (c-in-literal lim))
260 (progn
261 (if (memq (char-after) '(?\; ?{ ?}))
262 (setq crossedp t)
263 (if (eq (char-after) ?:)
264 (setq c-maybe-labelp t))
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
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
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.
297 (let* ((here (point-max))
298 (hugenum (point-max)))
299 (while (/= here (point))
300 (setq here (point))
301 (c-forward-comment hugenum)
302 ;; skip preprocessor directives
303 (when (and (eq (char-after) ?#)
304 (= (c-point 'boi) (point)))
305 (while (and (eq (char-before (c-point 'eol)) ?\\)
306 (= (forward-line 1) 0)))
307 (end-of-line))
308 )
309 (if lim (goto-char (min (point) lim)))))
310
311 (defun c-backward-syntactic-ws (&optional lim)
312 ;; Backward skip over syntactic whitespace for Emacs 19.
313 (let* ((here (point-min))
314 (hugenum (- (point-max))))
315 (while (/= here (point))
316 (setq here (point))
317 (c-forward-comment hugenum)
318 (c-beginning-of-macro))
319 (if lim (goto-char (max (point) lim)))))
320
321 \f
322 ;; Moving by tokens, where a token is defined as all symbols and
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
332 ;; tokens left to move (positive or negative). If BALANCED is true, a
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.
338
339 (defun c-forward-token-1 (&optional count balanced lim)
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)))
348 (save-restriction
349 (if lim (narrow-to-region (point-min) lim))
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))
364 (if (eobp)
365 (goto-char last)
366 ;; Avoid having the limit tests inside the loop.
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)))
381
382 (defun c-backward-token-1 (&optional count balanced lim)
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))
400 (save-restriction
401 (if lim (narrow-to-region lim (point-max)))
402 (or (bobp)
403 (progn
404 ;; Avoid having the limit tests inside the loop.
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)))
417
418 \f
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
428 (if (and (vectorp c-in-literal-cache)
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)))
433 (state (parse-partial-sexp lim (point))))
434 (cond
435 ((nth 3 state) 'string)
436 ((nth 4 state) (if (nth 7 state) 'c++ 'c))
437 ((c-beginning-of-macro lim) 'pound)
438 (t nil))))))
439 ;; cache this result if the cache is enabled
440 (if (not c-in-literal-cache)
441 (setq c-in-literal-cache (vector (point) rtn)))
442 rtn)))
443
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
458 (defun c-literal-limits (&optional lim near not-in-delimiter)
459 ;; Returns a cons of the beginning and end positions of the comment
460 ;; or string surrounding point (including both delimiters), or nil
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
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.
470 (save-excursion
471 (let* ((pos (point))
472 (lim (or lim (c-point 'bod)))
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))))
479 (cons (point) (or (c-safe (c-forward-sexp 1) (point))
480 (point-max))))
481 ((nth 7 state)
482 ;; Line comment. Search from bol for the comment starter.
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)
492 (cons (point) (progn (c-forward-comment 1) (point))))
493 ((nth 4 state)
494 ;; Block comment. Search backward for the comment starter.
495 (while (nth 4 state)
496 (search-backward "/*") ; Should never fail.
497 (setq state (parse-partial-sexp lim (point))))
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 "[/*]"))
503 ;; We're standing in a comment starter.
504 (backward-char 1)
505 (cons (point) (progn (c-forward-comment 1) (point))))
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.
515 (cons (point) (progn (c-forward-comment 1) (point))))
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)
529 (c-forward-comment -1)
530 ;; If LIM is bogus, beg will be bogus.
531 (setq beg (point))))
532 (if beg (cons beg end))))))
533 ))))
534
535 (defun c-literal-limits-fast (&optional lim near not-in-delimiter)
536 ;; Like c-literal-limits, but for emacsen whose `parse-partial-sexp'
537 ;; returns the pos of the comment start.
538 (save-excursion
539 (let* ((pos (point))
540 (lim (or lim (c-point 'bod)))
541 (state (parse-partial-sexp lim (point))))
542 (cond ((nth 3 state) ; String.
543 (goto-char (nth 8 state))
544 (cons (point) (or (c-safe (c-forward-sexp 1) (point))
545 (point-max))))
546 ((nth 4 state) ; Comment.
547 (goto-char (nth 8 state))
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))))))
583 ))))
584
585 (if (c-safe (> (length (save-excursion (parse-partial-sexp 1 1))) 8))
586 (defalias 'c-literal-limits 'c-literal-limits-fast))
587
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
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.
595 (save-excursion
596 (condition-case nil
597 (if (and (consp range) (progn
598 (goto-char (car range))
599 (looking-at "//")))
600 (let ((col (current-column))
601 (beg (point))
602 (bopl (c-point 'bopl))
603 (end (cdr range)))
604 ;; Got to take care in the backward direction to handle
605 ;; comments which are preceded by code.
606 (while (and (c-forward-comment -1)
607 (>= (point) bopl)
608 (looking-at "//")
609 (= col (current-column)))
610 (setq beg (point)
611 bopl (c-point 'bopl)))
612 (goto-char end)
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)))))
618 (cons beg end))
619 range)
620 (error range))))
621
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)
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.
633 range))
634
635
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
645 (if c-state-cache c-state-cache
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)))
653 (goto-char (c-point 'bod))
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) ?\())))
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)))
674 (last-bod here) (last-pos pos)
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
711 (setq last-bod (c-safe (scan-lists last-pos -1 1)))
712 (if (not last-bod)
713 (save-excursion
714 ;; bogus, but what can we do here?
715 (goto-char placeholder)
716 (beginning-of-line)
717 (setq c-parsing-error
718 (format "\
719 Unbalanced close brace at line %d" (1+ (count-lines 1 (point)))))
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
791 ;; SHIFT amount.
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.
812 (let* ((lim (or lim (c-point 'bod)))
813 (placeholder (progn
814 (back-to-indentation)
815 (point)))
816 (chr (char-after)))
817 (c-backward-syntactic-ws lim)
818 (while (and (> (point) lim)
819 (or (eq chr ?,)
820 (memq (char-before) '(?, ?:)))
821 (progn
822 (beginning-of-line)
823 (setq placeholder (point))
824 (skip-chars-forward " \t")
825 (setq chr (char-after))
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
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
842 (defun c-at-toplevel-p ()
843 "Return a determination as to whether point is at the `top-level'.
844 Being at the top-level means that point is either outside any
845 enclosing block (such function definition), or inside a class
846 definition, but outside any method blocks.
847
848 If point is not at the top-level (e.g. it is inside a method
849 definition), then nil is returned. Otherwise, if point is at a
850 top-level not enclosed within a class definition, t is returned.
851 Otherwise, a 2-vector is returned where the zeroth element is the
852 buffer position of the start of the class declaration, and the first
853 element is the buffer position of the enclosing class's opening
854 brace."
855 (let ((state (c-parse-state)))
856 (or (not (c-most-enclosing-brace state))
857 (c-search-uplist-for-classkey state))))
858
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)
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))
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
898 (c-forward-sexp -1)
899 (forward-char -1)
900 (c-backward-syntactic-ws)
901 (not (or (memq (char-before) '(?- ?+))
902 ;; or a class category
903 (progn
904 (c-forward-sexp -2)
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
924 (c-backward-sexp 1)
925 (cond
926 ;; break infloop for illegal C code
927 ((bobp) (setq do-level 0))
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
947 ;; returned.
948 (let ((if-level 1)
949 (here (c-point 'bol))
950 (case-fold-search nil)
951 (lim (or (and lim (>= (point) lim) lim)
952 (c-point 'bod)))
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
959 (c-backward-sexp 1)
960 (error
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"
966 (1+ (count-lines 1 here))))
967 (throw 'orphan-if nil))))
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)))
974 (c-safe (c-forward-sexp -1))
975 (if (looking-at "\\<else\\>[ \t]+\\<if\\>[^_]")
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.
988 (c-forward-sexp (cond
989 ;; else if()
990 ((looking-at "\\<else\\>[ \t]+\\<if\\>\\([^_]\\|$\\)") 3)
991 ;; do, else, try, finally
992 ((looking-at
993 "\\<\\(do\\|else\\|try\\|finally\\)\\>\\([^_]\\|$\\)")
994 1)
995 ;; for, if, while, switch, catch, synchronized, foreach
996 (t 2))))
997
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))
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) ?\))
1038 (c-backward-sexp 1))
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))
1055 (and (< limit (point))
1056 (eq (char-before) ?:)))
1057
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
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.
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)
1135 (let ((search-key (concat c-class-key "\\|" c-extra-toplevel-key))
1136 foundp class match-end)
1137 (if c-inexpr-class-key
1138 (setq search-key (concat search-key "\\|"
1139 c-inexpr-class-key)))
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
1163 (re-search-forward c-method-key search-end t)
1164 (not (c-in-literal class)))
1165 (setq foundp nil))
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)))
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 ">"))))
1191 (while (progn
1192 (skip-chars-forward skipchars search-end)
1193 (c-in-literal class))
1194 (forward-char))
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
1205 ;; paren. BRACE-STATE is the remainder of the state of enclosing
1206 ;; braces
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
1213 (c-safe
1214 (save-excursion
1215 (goto-char containing-sexp)
1216 (c-forward-sexp -1)
1217 (let (bracepos)
1218 (if (and (or (looking-at "enum[\t\n ]+")
1219 (progn (c-forward-sexp -1)
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)))))
1225 ;; this will pick up array/aggregate init lists, even if they are nested.
1226 (save-excursion
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)
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))
1238 (goto-char containing-sexp)
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)
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)
1259 ((looking-at "new\\>[^_]") t)
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)
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)
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)
1293 ((looking-at "\\s.") 'maybe)
1294 ;; make sure we're not in a C++ template
1295 ;; argument assignment
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 ))))
1307 nil)
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)
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))))
1324 ))
1325 bufpos))
1326 ))
1327
1328 (defun c-looking-at-special-brace-list (&optional lim)
1329 ;; If we're looking at the start of a pike-style list, ie `({ })',
1330 ;; `([ ])', `(< >)' etc, a cons of a cons of its starting and ending
1331 ;; positions and its entry in c-special-brace-lists is returned, nil
1332 ;; otherwise. The ending position is nil if the list is still open.
1333 ;; LIM is the limit for forward search. The point may either be at
1334 ;; the `(' or at the following paren character. Tries to check the
1335 ;; matching closer, but assumes it's correct if no balanced paren is
1336 ;; found (i.e. the case `({ ... } ... )' is detected as _not_ being
1337 ;; a special brace list).
1338 (if c-special-brace-lists
1339 (condition-case ()
1340 (save-excursion
1341 (let ((beg (point))
1342 end type)
1343 (c-forward-syntactic-ws)
1344 (if (eq (char-after) ?\()
1345 (progn
1346 (forward-char 1)
1347 (c-forward-syntactic-ws)
1348 (setq type (assq (char-after) c-special-brace-lists)))
1349 (if (setq type (assq (char-after) c-special-brace-lists))
1350 (progn
1351 (c-backward-syntactic-ws)
1352 (forward-char -1)
1353 (setq beg (if (eq (char-after) ?\()
1354 (point)
1355 nil)))))
1356 (if (and beg type)
1357 (if (and (c-safe (goto-char beg)
1358 (c-forward-sexp 1)
1359 (setq end (point))
1360 (= (char-before) ?\)))
1361 (c-safe (goto-char beg)
1362 (forward-char 1)
1363 (c-forward-sexp 1)
1364 ;; Kludges needed to handle inner
1365 ;; chars both with and without
1366 ;; paren syntax.
1367 (or (/= (char-syntax (char-before)) ?\))
1368 (= (char-before) (cdr type)))))
1369 (if (or (/= (char-syntax (char-before)) ?\))
1370 (= (progn
1371 (c-forward-syntactic-ws)
1372 (point))
1373 (1- end)))
1374 (cons (cons beg end) type))
1375 (cons (list beg) type)))))
1376 (error nil))))
1377
1378 (defun c-looking-at-bos ()
1379 ;; Returns nil if inside a statement or declaration.
1380 (save-excursion
1381 (c-backward-syntactic-ws)
1382 (or (bobp)
1383 (memq (char-before) '(?\; ?}))
1384 (and (eq (char-before) ?{)
1385 (not (and c-special-brace-lists
1386 (progn (backward-char)
1387 (c-looking-at-special-brace-list))))))))
1388
1389 (defun c-looking-at-inexpr-block (&optional lim)
1390 ;; Returns non-nil if we're looking at the beginning of a block
1391 ;; inside an expression. The value returned is actually a cons of
1392 ;; either 'inlambda, 'inexpr-statement or 'inexpr-class and the
1393 ;; position of the beginning of the construct. LIM limits the
1394 ;; backward search.
1395 (save-excursion
1396 (or lim (setq lim (point-min)))
1397 (let ((block-follows (eq (char-after) ?{)))
1398 ;; Look at the character after point only as a last resort when
1399 ;; we can't disambiguate.
1400 (if (and block-follows
1401 (progn (c-backward-syntactic-ws) (> (point) lim))
1402 (eq (char-before) ?\()
1403 (not (and c-special-brace-lists
1404 (c-looking-at-special-brace-list))))
1405 (cons 'inexpr-statement (point))
1406 (let (res)
1407 (while (and (not res)
1408 (= (c-backward-token-1 1 t lim) 0)
1409 (>= (point) lim)
1410 (looking-at "(\\|\\w\\|\\s_\\|\\."))
1411 (setq res
1412 (cond ((and block-follows
1413 c-inexpr-class-key
1414 (looking-at c-inexpr-class-key)
1415 (or (not (looking-at c-class-key))
1416 (let ((prev (point)))
1417 (while (and (= (c-backward-token-1 1 t lim)
1418 0)
1419 (>= (point) lim)
1420 (eq (char-syntax (char-after))
1421 ?w))
1422 (setq prev (point)))
1423 (goto-char prev)
1424 (not (c-looking-at-bos)))))
1425 (cons 'inexpr-class (point)))
1426 ((and c-inexpr-block-key
1427 (looking-at c-inexpr-block-key))
1428 (cons 'inexpr-statement (point)))
1429 ((and c-lambda-key
1430 (looking-at c-lambda-key))
1431 (cons 'inlambda (point))))))
1432 res)))))
1433
1434 (defun c-looking-at-inexpr-block-backward (&optional lim)
1435 ;; Returns non-nil if we're looking at the end of an in-expression
1436 ;; block, otherwise the same as `c-looking-at-inexpr-block'.
1437 (save-excursion
1438 (let ((lim (or lim (c-point 'bod))))
1439 (c-safe
1440 (c-backward-syntactic-ws lim)
1441 (if (eq (char-before) ?}) ; Recognize only a block currently.
1442 (progn
1443 (c-forward-sexp -1)
1444 (if (>= (point) lim)
1445 (c-looking-at-inexpr-block lim))))))))
1446
1447 (defun c-on-identifier ()
1448 ;; Returns non-nil if we're on or directly after an identifier.
1449 (if (or (memq (char-syntax (or (char-after) ? )) '(?w ?_))
1450 (memq (char-syntax (or (char-before) ? )) '(?w ?_)))
1451 (save-excursion
1452 (skip-syntax-backward "w_")
1453 (not (looking-at c-keywords)))
1454 (if (c-major-mode-is 'pike-mode)
1455 ;; Handle the `<operator> syntax in Pike.
1456 (save-excursion
1457 (if (eq (char-after) ?\`) (forward-char))
1458 (skip-chars-backward "!%&*+\\-/<=>^|~")
1459 (let ((pos (point)))
1460 (cond ((memq (char-before) '(?\) ?\]))
1461 (c-safe (backward-char 2)))
1462 ((memq (char-before) '(?\( ?\[))
1463 (c-safe (backward-char 1))))
1464 (if (not (looking-at "()\\|\\[]"))
1465 (goto-char pos)))
1466 (and (eq (char-before) ?\`)
1467 (looking-at "[-!%&*+/<=>^|~]\\|()\\|\\[]"))))))
1468
1469 \f
1470 (defun c-most-enclosing-brace (state)
1471 ;; return the bufpos of the most enclosing brace that hasn't been
1472 ;; narrowed out by any enclosing class, or nil if none was found
1473 (let (enclosingp)
1474 (while (and state (not enclosingp))
1475 (setq enclosingp (car state)
1476 state (cdr state))
1477 (if (consp enclosingp)
1478 (setq enclosingp nil)
1479 (if (> (point-min) enclosingp)
1480 (setq enclosingp nil))
1481 (setq state nil)))
1482 enclosingp))
1483
1484 (defun c-least-enclosing-brace (state)
1485 ;; return the bufpos of the least (highest) enclosing brace that
1486 ;; hasn't been narrowed out by any enclosing class, or nil if none
1487 ;; was found.
1488 (c-most-enclosing-brace (nreverse state)))
1489
1490 (defun c-safe-position (bufpos state)
1491 ;; return the closest known safe position higher up than point
1492 (let ((safepos nil))
1493 (while state
1494 (setq safepos
1495 (if (consp (car state))
1496 (cdr (car state))
1497 (car state)))
1498 (if (< safepos bufpos)
1499 (setq state nil)
1500 (setq state (cdr state))))
1501 safepos))
1502
1503 (defun c-narrow-out-enclosing-class (state lim)
1504 ;; narrow the buffer so that the enclosing class is hidden
1505 (setq state (c-whack-state (point) state))
1506 (let (inclass-p)
1507 (and state
1508 (setq inclass-p (c-search-uplist-for-classkey state))
1509 (narrow-to-region
1510 (progn
1511 (goto-char (1+ (aref inclass-p 1)))
1512 (skip-chars-forward " \t\n" lim)
1513 ;; if point is now left of the class opening brace, we're
1514 ;; hosed, so try a different tact
1515 (if (<= (point) (aref inclass-p 1))
1516 (progn
1517 (goto-char (1+ (aref inclass-p 1)))
1518 (c-forward-syntactic-ws lim)))
1519 (point))
1520 ;; end point is the end of the current line
1521 (progn
1522 (goto-char lim)
1523 (c-point 'eol))))
1524 ;; return the class vector
1525 inclass-p))
1526
1527 \f
1528 ;; This function implements the main decision tree for determining the
1529 ;; syntactic analysis of the current line of code. Yes, it's huge and
1530 ;; bloated!
1531
1532 (defun c-guess-basic-syntax ()
1533 (save-excursion
1534 (save-restriction
1535 (beginning-of-line)
1536 (let* ((indent-point (point))
1537 (case-fold-search nil)
1538 (fullstate (c-parse-state))
1539 (state fullstate)
1540 literal containing-sexp char-before-ip char-after-ip lim
1541 syntax placeholder c-in-literal-cache inswitch-p
1542 tmpsymbol keyword injava-inher special-brace-list
1543 ;; narrow out any enclosing class or extern "C" block
1544 (inclass-p (c-narrow-out-enclosing-class state indent-point))
1545 inenclosing-p)
1546 ;; check for meta top-level enclosing constructs, possible
1547 ;; extern language definitions, possibly (in C++) namespace
1548 ;; definitions.
1549 (save-excursion
1550 (save-restriction
1551 (widen)
1552 (if (and inclass-p
1553 (progn
1554 (goto-char (aref inclass-p 0))
1555 (looking-at (concat c-extra-toplevel-key "[^_]"))))
1556 (let ((enclosing (match-string 1)))
1557 (cond
1558 ((string-equal enclosing "extern")
1559 (setq inenclosing-p 'extern))
1560 ((string-equal enclosing "namespace")
1561 (setq inenclosing-p 'namespace))
1562 )))))
1563 ;; get the buffer position of the most nested opening brace,
1564 ;; if there is one, and it hasn't been narrowed out
1565 (save-excursion
1566 (goto-char indent-point)
1567 (skip-chars-forward " \t}")
1568 (skip-chars-backward " \t")
1569 (while (and state
1570 (not containing-sexp))
1571 (setq containing-sexp (car state)
1572 state (cdr state))
1573 (if (consp containing-sexp)
1574 ;; if cdr == point, then containing sexp is the brace
1575 ;; that opens the sexp we close
1576 (if (= (cdr containing-sexp) (point))
1577 (setq containing-sexp (car containing-sexp))
1578 ;; otherwise, ignore this element
1579 (setq containing-sexp nil))
1580 ;; ignore the bufpos if its been narrowed out by the
1581 ;; containing class or does not contain the indent point
1582 (if (or (<= containing-sexp (point-min))
1583 (>= containing-sexp indent-point))
1584 (setq containing-sexp nil)))))
1585
1586 ;; set the limit on the farthest back we need to search
1587 (setq lim (or containing-sexp
1588 (if (consp (car fullstate))
1589 (cdr (car fullstate))
1590 nil)
1591 (point-min)))
1592
1593 ;; cache char before and after indent point, and move point to
1594 ;; the most likely position to perform the majority of tests
1595 (goto-char indent-point)
1596 (skip-chars-forward " \t")
1597 (setq char-after-ip (char-after))
1598 (c-backward-syntactic-ws lim)
1599 (setq char-before-ip (char-before))
1600 (goto-char indent-point)
1601 (skip-chars-forward " \t")
1602
1603 ;; are we in a literal?
1604 (setq literal (c-in-literal lim))
1605
1606 ;; now figure out syntactic qualities of the current line
1607 (cond
1608 ;; CASE 1: in a string.
1609 ((memq literal '(string))
1610 (c-add-syntax 'string (c-point 'bopl)))
1611 ;; CASE 2: in a C or C++ style comment.
1612 ((memq literal '(c c++))
1613 (c-add-syntax literal (car (c-literal-limits lim))))
1614 ;; CASE 3: in a cpp preprocessor macro continuation.
1615 ((and (eq literal 'pound)
1616 (/= (save-excursion
1617 (c-beginning-of-macro lim)
1618 (setq placeholder (point)))
1619 (c-point 'boi)))
1620 (c-add-syntax 'cpp-macro-cont placeholder))
1621 ;; CASE 4: In-expression statement.
1622 ((and (or c-inexpr-class-key c-inexpr-block-key c-lambda-key)
1623 (setq placeholder (c-looking-at-inexpr-block)))
1624 (setq tmpsymbol (assq (car placeholder)
1625 '((inexpr-class . class-open)
1626 (inexpr-statement . block-open))))
1627 (if tmpsymbol
1628 ;; It's a statement block or an anonymous class.
1629 (setq tmpsymbol (cdr tmpsymbol))
1630 ;; It's a Pike lambda. Check whether we are between the
1631 ;; lambda keyword and the argument list or at the defun
1632 ;; opener.
1633 (setq tmpsymbol (if (eq char-after-ip ?{)
1634 'inline-open
1635 'lambda-intro-cont)))
1636 (goto-char (cdr placeholder))
1637 (back-to-indentation)
1638 (c-add-syntax tmpsymbol (point))
1639 (unless (eq (point) (cdr placeholder))
1640 (c-add-syntax (car placeholder))))
1641 ;; CASE 5: Line is at top level.
1642 ((null containing-sexp)
1643 (cond
1644 ;; CASE 5A: we are looking at a defun, brace list, class,
1645 ;; or inline-inclass method opening brace
1646 ((setq special-brace-list
1647 (or (and c-special-brace-lists
1648 (c-looking-at-special-brace-list))
1649 (eq char-after-ip ?{)))
1650 (cond
1651 ;; CASE 5A.1: extern language or namespace construct
1652 ((save-excursion
1653 (goto-char indent-point)
1654 (skip-chars-forward " \t")
1655 (and (c-safe (progn (c-backward-sexp 2) t))
1656 (looking-at (concat c-extra-toplevel-key "[^_]"))
1657 (setq keyword (match-string 1)
1658 placeholder (point))
1659 (or (and (string-equal keyword "namespace")
1660 (setq tmpsymbol 'namespace-open))
1661 (and (string-equal keyword "extern")
1662 (progn
1663 (c-forward-sexp 1)
1664 (c-forward-syntactic-ws)
1665 (eq (char-after) ?\"))
1666 (setq tmpsymbol 'extern-lang-open)))
1667 ))
1668 (goto-char placeholder)
1669 (c-add-syntax tmpsymbol (c-point 'boi)))
1670 ;; CASE 5A.2: we are looking at a class opening brace
1671 ((save-excursion
1672 (goto-char indent-point)
1673 (skip-chars-forward " \t{")
1674 ;; TBD: watch out! there could be a bogus
1675 ;; c-state-cache in place when we get here. we have
1676 ;; to go through much chicanery to ignore the cache.
1677 ;; But of course, there may not be! BLECH! BOGUS!
1678 (let ((decl
1679 (let ((c-state-cache nil))
1680 (c-search-uplist-for-classkey (c-parse-state))
1681 )))
1682 (and decl
1683 (setq placeholder (aref decl 0)))
1684 ))
1685 (c-add-syntax 'class-open placeholder))
1686 ;; CASE 5A.3: brace list open
1687 ((save-excursion
1688 (c-beginning-of-statement-1 lim)
1689 ;; c-b-o-s could have left us at point-min
1690 (and (bobp)
1691 (c-forward-syntactic-ws indent-point))
1692 (if (looking-at "typedef[^_]")
1693 (progn (c-forward-sexp 1)
1694 (c-forward-syntactic-ws indent-point)))
1695 (setq placeholder (c-point 'boi))
1696 (or (consp special-brace-list)
1697 (and (or (save-excursion
1698 (goto-char indent-point)
1699 (setq tmpsymbol nil)
1700 (while (and (> (point) placeholder)
1701 (= (c-backward-token-1 1 t) 0)
1702 (/= (char-after) ?=))
1703 (if (and (not tmpsymbol)
1704 (looking-at "new\\>[^_]"))
1705 (setq tmpsymbol 'topmost-intro-cont)))
1706 (eq (char-after) ?=))
1707 (looking-at "enum[ \t\n]+"))
1708 (save-excursion
1709 (while (and (< (point) indent-point)
1710 (= (c-forward-token-1 1 t) 0)
1711 (not (memq (char-after) '(?\; ?\()))))
1712 (not (memq (char-after) '(?\; ?\()))
1713 ))))
1714 (if (and (c-major-mode-is 'java-mode)
1715 (eq tmpsymbol 'topmost-intro-cont))
1716 ;; We're in Java and have found that the open brace
1717 ;; belongs to a "new Foo[]" initialization list,
1718 ;; which means the brace list is part of an
1719 ;; expression and not a top level definition. We
1720 ;; therefore treat it as any topmost continuation
1721 ;; even though the semantically correct symbol still
1722 ;; is brace-list-open, on the same grounds as in
1723 ;; case 10B.2.
1724 (progn
1725 (c-beginning-of-statement-1 lim)
1726 (c-forward-syntactic-ws)
1727 (c-add-syntax 'topmost-intro-cont (c-point 'boi)))
1728 (c-add-syntax 'brace-list-open placeholder)))
1729 ;; CASE 5A.4: inline defun open
1730 ((and inclass-p (not inenclosing-p))
1731 (c-add-syntax 'inline-open)
1732 (c-add-class-syntax 'inclass inclass-p))
1733 ;; CASE 5A.5: ordinary defun open
1734 (t
1735 (goto-char placeholder)
1736 (if inclass-p
1737 (c-add-syntax 'defun-open (c-point 'boi))
1738 (c-add-syntax 'defun-open (c-point 'bol)))
1739 )))
1740 ;; CASE 5B: first K&R arg decl or member init
1741 ((c-just-after-func-arglist-p)
1742 (cond
1743 ;; CASE 5B.1: a member init
1744 ((or (eq char-before-ip ?:)
1745 (eq char-after-ip ?:))
1746 ;; this line should be indented relative to the beginning
1747 ;; of indentation for the topmost-intro line that contains
1748 ;; the prototype's open paren
1749 ;; TBD: is the following redundant?
1750 (if (eq char-before-ip ?:)
1751 (forward-char -1))
1752 (c-backward-syntactic-ws lim)
1753 ;; TBD: is the preceding redundant?
1754 (if (eq (char-before) ?:)
1755 (progn (forward-char -1)
1756 (c-backward-syntactic-ws lim)))
1757 (if (eq (char-before) ?\))
1758 (c-backward-sexp 1))
1759 (setq placeholder (point))
1760 (save-excursion
1761 (and (c-safe (c-backward-sexp 1) t)
1762 (looking-at "throw[^_]")
1763 (c-safe (c-backward-sexp 1) t)
1764 (setq placeholder (point))))
1765 (goto-char placeholder)
1766 (c-add-syntax 'member-init-intro (c-point 'boi))
1767 ;; we don't need to add any class offset since this
1768 ;; should be relative to the ctor's indentation
1769 )
1770 ;; CASE 5B.2: K&R arg decl intro
1771 (c-recognize-knr-p
1772 (c-add-syntax 'knr-argdecl-intro (c-point 'boi))
1773 (if inclass-p (c-add-class-syntax 'inclass inclass-p)))
1774 ;; CASE 5B.3: Inside a member init list.
1775 ((c-beginning-of-member-init-list lim)
1776 (c-forward-syntactic-ws)
1777 (c-add-syntax 'member-init-cont (point)))
1778 ;; CASE 5B.4: Nether region after a C++ or Java func
1779 ;; decl, which could include a `throws' declaration.
1780 (t
1781 (c-beginning-of-statement-1 lim)
1782 (c-add-syntax 'func-decl-cont (c-point 'boi))
1783 )))
1784 ;; CASE 5C: inheritance line. could be first inheritance
1785 ;; line, or continuation of a multiple inheritance
1786 ((or (and c-baseclass-key
1787 (progn
1788 (when (eq char-after-ip ?,)
1789 (skip-chars-forward " \t")
1790 (forward-char))
1791 (looking-at c-baseclass-key)))
1792 (and (or (eq char-before-ip ?:)
1793 ;; watch out for scope operator
1794 (save-excursion
1795 (and (eq char-after-ip ?:)
1796 (c-safe (progn (forward-char 1) t))
1797 (not (eq (char-after) ?:))
1798 )))
1799 (save-excursion
1800 (c-backward-syntactic-ws lim)
1801 (if (eq char-before-ip ?:)
1802 (progn
1803 (forward-char -1)
1804 (c-backward-syntactic-ws lim)))
1805 (back-to-indentation)
1806 (looking-at c-class-key)))
1807 ;; for Java
1808 (and (c-major-mode-is 'java-mode)
1809 (let ((fence (save-excursion
1810 (c-beginning-of-statement-1 lim)
1811 (point)))
1812 cont done)
1813 (save-excursion
1814 (while (not done)
1815 (cond ((looking-at c-Java-special-key)
1816 (setq injava-inher (cons cont (point))
1817 done t))
1818 ((or (not (c-safe (c-forward-sexp -1) t))
1819 (<= (point) fence))
1820 (setq done t))
1821 )
1822 (setq cont t)))
1823 injava-inher)
1824 (not (c-crosses-statement-barrier-p (cdr injava-inher)
1825 (point)))
1826 ))
1827 (cond
1828 ;; CASE 5C.1: non-hanging colon on an inher intro
1829 ((eq char-after-ip ?:)
1830 (c-backward-syntactic-ws lim)
1831 (c-add-syntax 'inher-intro (c-point 'boi))
1832 ;; don't add inclass symbol since relative point already
1833 ;; contains any class offset
1834 )
1835 ;; CASE 5C.2: hanging colon on an inher intro
1836 ((eq char-before-ip ?:)
1837 (c-add-syntax 'inher-intro (c-point 'boi))
1838 (if inclass-p (c-add-class-syntax 'inclass inclass-p)))
1839 ;; CASE 5C.3: in a Java implements/extends
1840 (injava-inher
1841 (let ((where (cdr injava-inher))
1842 (cont (car injava-inher)))
1843 (goto-char where)
1844 (cond ((looking-at "throws[ \t\n]")
1845 (c-add-syntax 'func-decl-cont
1846 (progn (c-beginning-of-statement-1 lim)
1847 (c-point 'boi))))
1848 (cont (c-add-syntax 'inher-cont where))
1849 (t (c-add-syntax 'inher-intro
1850 (progn (goto-char (cdr injava-inher))
1851 (c-beginning-of-statement-1 lim)
1852 (point))))
1853 )))
1854 ;; CASE 5C.4: a continued inheritance line
1855 (t
1856 (c-beginning-of-inheritance-list lim)
1857 (c-add-syntax 'inher-cont (point))
1858 ;; don't add inclass symbol since relative point already
1859 ;; contains any class offset
1860 )))
1861 ;; CASE 5D: this could be a top-level compound statement, a
1862 ;; member init list continuation, or a template argument
1863 ;; list continuation.
1864 ((c-with-syntax-table (if (c-major-mode-is 'c++-mode)
1865 c++-template-syntax-table
1866 (syntax-table))
1867 (save-excursion
1868 (while (and (= (c-backward-token-1 1 t lim) 0)
1869 (not (looking-at "[;{<,]"))))
1870 (eq (char-after) ?,)))
1871 (goto-char indent-point)
1872 (c-beginning-of-member-init-list lim)
1873 (cond
1874 ;; CASE 5D.1: hanging member init colon, but watch out
1875 ;; for bogus matches on access specifiers inside classes.
1876 ((and (save-excursion
1877 (setq placeholder (point))
1878 (c-backward-token-1 1 t lim)
1879 (and (eq (char-after) ?:)
1880 (not (eq (char-before) ?:))))
1881 (save-excursion
1882 (goto-char placeholder)
1883 (back-to-indentation)
1884 (or
1885 (/= (car (save-excursion
1886 (parse-partial-sexp (point) placeholder)))
1887 0)
1888 (and
1889 (if c-access-key (not (looking-at c-access-key)) t)
1890 (not (looking-at c-class-key))
1891 (if c-bitfield-key (not (looking-at c-bitfield-key)) t))
1892 )))
1893 (goto-char placeholder)
1894 (c-forward-syntactic-ws)
1895 (c-add-syntax 'member-init-cont (point))
1896 ;; we do not need to add class offset since relative
1897 ;; point is the member init above us
1898 )
1899 ;; CASE 5D.2: non-hanging member init colon
1900 ((progn
1901 (c-forward-syntactic-ws indent-point)
1902 (eq (char-after) ?:))
1903 (skip-chars-forward " \t:")
1904 (c-add-syntax 'member-init-cont (point)))
1905 ;; CASE 5D.3: perhaps a multiple inheritance line?
1906 ((save-excursion
1907 (c-beginning-of-statement-1 lim)
1908 (setq placeholder (point))
1909 (looking-at c-inher-key))
1910 (goto-char placeholder)
1911 (c-add-syntax 'inher-cont (c-point 'boi)))
1912 ;; CASE 5D.4: perhaps a template list continuation?
1913 ((save-excursion
1914 (goto-char indent-point)
1915 (skip-chars-backward "^<" lim)
1916 ;; not sure if this is the right test, but it should
1917 ;; be fast and mostly accurate.
1918 (setq placeholder (point))
1919 (and (eq (char-before) ?<)
1920 (not (c-in-literal lim))))
1921 ;; we can probably indent it just like an arglist-cont
1922 (goto-char placeholder)
1923 (c-beginning-of-statement-1 lim)
1924 (c-add-syntax 'template-args-cont (c-point 'boi)))
1925 ;; CASE 5D.5: perhaps a top-level statement-cont
1926 (t
1927 (c-beginning-of-statement-1 lim)
1928 ;; skip over any access-specifiers
1929 (and inclass-p c-access-key
1930 (while (looking-at c-access-key)
1931 (forward-line 1)))
1932 ;; skip over comments, whitespace
1933 (c-forward-syntactic-ws indent-point)
1934 (c-add-syntax 'statement-cont (c-point 'boi)))
1935 ))
1936 ;; CASE 5E: we are looking at a access specifier
1937 ((and inclass-p
1938 c-access-key
1939 (looking-at c-access-key))
1940 (c-add-syntax 'access-label (c-point 'bonl))
1941 (c-add-class-syntax 'inclass inclass-p))
1942 ;; CASE 5F: extern-lang-close or namespace-close?
1943 ((and inenclosing-p
1944 (eq char-after-ip ?}))
1945 (setq tmpsymbol (if (eq inenclosing-p 'extern)
1946 'extern-lang-close
1947 'namespace-close))
1948 (c-add-syntax tmpsymbol (aref inclass-p 0)))
1949 ;; CASE 5G: we are looking at the brace which closes the
1950 ;; enclosing nested class decl
1951 ((and inclass-p
1952 (eq char-after-ip ?})
1953 (save-excursion
1954 (save-restriction
1955 (widen)
1956 (forward-char 1)
1957 (and (c-safe (progn (c-backward-sexp 1) t))
1958 (= (point) (aref inclass-p 1))
1959 ))))
1960 (c-add-class-syntax 'class-close inclass-p))
1961 ;; CASE 5H: we could be looking at subsequent knr-argdecls
1962 ((and c-recognize-knr-p
1963 ;; here we essentially use the hack that is used in
1964 ;; Emacs' c-mode.el to limit how far back we should
1965 ;; look. The assumption is made that argdecls are
1966 ;; indented at least one space and that function
1967 ;; headers are not indented.
1968 (let ((limit (save-excursion
1969 (re-search-backward "^[^ \^L\t\n#]" nil 'move)
1970 (point))))
1971 (save-excursion
1972 (c-backward-syntactic-ws limit)
1973 (setq placeholder (point))
1974 (while (and (memq (char-before) '(?\; ?,))
1975 (> (point) limit))
1976 (beginning-of-line)
1977 (setq placeholder (point))
1978 (c-backward-syntactic-ws limit))
1979 (and (eq (char-before) ?\))
1980 (or (not c-method-key)
1981 (progn
1982 (c-forward-sexp -1)
1983 (forward-char -1)
1984 (c-backward-syntactic-ws)
1985 (not (or (memq (char-before) '(?- ?+))
1986 ;; or a class category
1987 (progn
1988 (c-forward-sexp -2)
1989 (looking-at c-class-key))
1990 )))))
1991 ))
1992 (save-excursion
1993 (c-beginning-of-statement-1)
1994 (not (looking-at "typedef[ \t\n]+"))))
1995 (goto-char placeholder)
1996 (c-add-syntax 'knr-argdecl (c-point 'boi)))
1997 ;; CASE 5I: ObjC method definition.
1998 ((and c-method-key
1999 (looking-at c-method-key))
2000 (c-add-syntax 'objc-method-intro (c-point 'boi)))
2001 ;; CASE 5J: we are at the topmost level, make sure we skip
2002 ;; back past any access specifiers
2003 ((progn
2004 (c-backward-syntactic-ws lim)
2005 (while (and inclass-p
2006 c-access-key
2007 (not (bobp))
2008 (save-excursion
2009 (c-safe (progn (c-backward-sexp 1) t))
2010 (looking-at c-access-key)))
2011 (c-backward-sexp 1)
2012 (c-backward-syntactic-ws lim))
2013 (or (bobp)
2014 (memq (char-before) '(?\; ?\}))))
2015 ;; real beginning-of-line could be narrowed out due to
2016 ;; enclosure in a class block
2017 (save-restriction
2018 (widen)
2019 (c-add-syntax 'topmost-intro (c-point 'bol))
2020 (if inclass-p
2021 (progn
2022 (goto-char (aref inclass-p 1))
2023 (or (= (point) (c-point 'boi))
2024 (goto-char (aref inclass-p 0)))
2025 (cond
2026 ((eq inenclosing-p 'extern)
2027 (c-add-syntax 'inextern-lang (c-point 'boi)))
2028 ((eq inenclosing-p 'namespace)
2029 (c-add-syntax 'innamespace (c-point 'boi)))
2030 (t (c-add-class-syntax 'inclass inclass-p)))
2031 ))
2032 ))
2033 ;; CASE 5K: we are at an ObjC or Java method definition
2034 ;; continuation line.
2035 ((and c-method-key
2036 (progn
2037 (c-beginning-of-statement-1 lim)
2038 (beginning-of-line)
2039 (looking-at c-method-key)))
2040 (c-add-syntax 'objc-method-args-cont (point)))
2041 ;; CASE 5L: we are at the first argument of a template
2042 ;; arglist that begins on the previous line.
2043 ((eq (char-before) ?<)
2044 (c-beginning-of-statement-1 lim)
2045 (c-forward-syntactic-ws)
2046 (c-add-syntax 'template-args-cont (c-point 'boi)))
2047 ;; CASE 5M: we are at a topmost continuation line
2048 (t
2049 (c-beginning-of-statement-1 lim)
2050 (c-forward-syntactic-ws)
2051 (c-add-syntax 'topmost-intro-cont (c-point 'boi)))
2052 )) ; end CASE 5
2053 ;; (CASE 6 has been removed.)
2054 ;; CASE 7: line is an expression, not a statement. Most
2055 ;; likely we are either in a function prototype or a function
2056 ;; call argument list
2057 ((not (or (and c-special-brace-lists
2058 (save-excursion
2059 (goto-char containing-sexp)
2060 (c-looking-at-special-brace-list)))
2061 (eq (char-after containing-sexp) ?{)))
2062 (c-backward-syntactic-ws containing-sexp)
2063 (cond
2064 ;; CASE 7A: we are looking at the arglist closing paren
2065 ((and (or (c-major-mode-is 'pike-mode)
2066 ;; Don't check this in Pike since it allows a
2067 ;; comma after the last arg.
2068 (not (eq char-before-ip ?,)))
2069 (memq char-after-ip '(?\) ?\])))
2070 (goto-char containing-sexp)
2071 (setq placeholder (c-point 'boi))
2072 (when (and (c-safe (backward-up-list 1) t)
2073 (> (point) placeholder))
2074 (forward-char)
2075 (skip-chars-forward " \t")
2076 (setq placeholder (point)))
2077 (c-add-syntax 'arglist-close placeholder))
2078 ;; CASE 7B: Looking at the opening brace of an
2079 ;; in-expression block or brace list.
2080 ((eq char-after-ip ?{)
2081 (goto-char indent-point)
2082 (setq placeholder (c-point 'boi))
2083 (goto-char containing-sexp)
2084 (if (c-inside-bracelist-p placeholder
2085 (cons containing-sexp state))
2086 (progn
2087 (c-add-syntax 'brace-list-open (c-point 'boi))
2088 (c-add-syntax 'inexpr-class))
2089 (c-add-syntax 'block-open (c-point 'boi))
2090 (c-add-syntax 'inexpr-statement)))
2091 ;; CASE 7C: we are looking at the first argument in an empty
2092 ;; argument list. Use arglist-close if we're actually
2093 ;; looking at a close paren or bracket.
2094 ((memq char-before-ip '(?\( ?\[))
2095 (goto-char containing-sexp)
2096 (setq placeholder (c-point 'boi))
2097 (when (and (c-safe (backward-up-list 1) t)
2098 (> (point) placeholder))
2099 (forward-char)
2100 (skip-chars-forward " \t")
2101 (setq placeholder (point)))
2102 (c-add-syntax 'arglist-intro placeholder))
2103 ;; CASE 7D: we are inside a conditional test clause. treat
2104 ;; these things as statements
2105 ((save-excursion
2106 (goto-char containing-sexp)
2107 (and (c-safe (progn (c-forward-sexp -1) t))
2108 (looking-at "\\<for\\>[^_]")))
2109 (goto-char (1+ containing-sexp))
2110 (c-forward-syntactic-ws indent-point)
2111 (c-beginning-of-statement-1 containing-sexp)
2112 (if (eq char-before-ip ?\;)
2113 (c-add-syntax 'statement (point))
2114 (c-add-syntax 'statement-cont (point))
2115 ))
2116 ;; CASE 7E: maybe a continued method call. This is the case
2117 ;; when we are inside a [] bracketed exp, and what precede
2118 ;; the opening bracket is not an identifier.
2119 ((and c-method-key
2120 (eq (char-after containing-sexp) ?\[)
2121 (save-excursion
2122 (goto-char (1- containing-sexp))
2123 (c-backward-syntactic-ws (c-point 'bod))
2124 (if (not (looking-at c-symbol-key))
2125 (c-add-syntax 'objc-method-call-cont containing-sexp))
2126 )))
2127 ;; CASE 7F: we are looking at an arglist continuation line,
2128 ;; but the preceding argument is on the same line as the
2129 ;; opening paren. This case includes multi-line
2130 ;; mathematical paren groupings, but we could be on a
2131 ;; for-list continuation line
2132 ((save-excursion
2133 (goto-char (1+ containing-sexp))
2134 (skip-chars-forward " \t")
2135 (not (eolp)))
2136 (goto-char containing-sexp)
2137 (setq placeholder (c-point 'boi))
2138 (when (and (c-safe (backward-up-list 1) t)
2139 (> (point) placeholder))
2140 (forward-char)
2141 (skip-chars-forward " \t")
2142 (setq placeholder (point)))
2143 (c-add-syntax 'arglist-cont-nonempty placeholder))
2144 ;; CASE 7G: we are looking at just a normal arglist
2145 ;; continuation line
2146 (t (c-beginning-of-statement-1 containing-sexp)
2147 (forward-char 1)
2148 (c-forward-syntactic-ws indent-point)
2149 (c-add-syntax 'arglist-cont (c-point 'boi)))
2150 ))
2151 ;; CASE 8: func-local multi-inheritance line
2152 ((and c-baseclass-key
2153 (save-excursion
2154 (goto-char indent-point)
2155 (skip-chars-forward " \t")
2156 (looking-at c-baseclass-key)))
2157 (goto-char indent-point)
2158 (skip-chars-forward " \t")
2159 (cond
2160 ;; CASE 8A: non-hanging colon on an inher intro
2161 ((eq char-after-ip ?:)
2162 (c-backward-syntactic-ws lim)
2163 (c-add-syntax 'inher-intro (c-point 'boi)))
2164 ;; CASE 8B: hanging colon on an inher intro
2165 ((eq char-before-ip ?:)
2166 (c-add-syntax 'inher-intro (c-point 'boi)))
2167 ;; CASE 8C: a continued inheritance line
2168 (t
2169 (c-beginning-of-inheritance-list lim)
2170 (c-add-syntax 'inher-cont (point))
2171 )))
2172 ;; CASE 9: we are inside a brace-list
2173 ((setq special-brace-list
2174 (or (and c-special-brace-lists
2175 (save-excursion
2176 (goto-char containing-sexp)
2177 (c-looking-at-special-brace-list)))
2178 (c-inside-bracelist-p containing-sexp state)))
2179 (cond
2180 ;; CASE 9A: In the middle of a special brace list opener.
2181 ((and (consp special-brace-list)
2182 (save-excursion
2183 (goto-char containing-sexp)
2184 (eq (char-after) ?\())
2185 (eq char-after-ip (car (cdr special-brace-list))))
2186 (goto-char (car (car special-brace-list)))
2187 (skip-chars-backward " \t")
2188 (if (and (bolp)
2189 (assoc 'statement-cont
2190 (setq placeholder (c-guess-basic-syntax))))
2191 (setq syntax placeholder)
2192 (c-beginning-of-statement-1 lim)
2193 (c-forward-token-1 0)
2194 (if (looking-at "typedef\\>") (c-forward-token-1 1))
2195 (c-add-syntax 'brace-list-open (c-point 'boi))))
2196 ;; CASE 9B: brace-list-close brace
2197 ((if (consp special-brace-list)
2198 ;; Check special brace list closer.
2199 (progn
2200 (goto-char (car (car special-brace-list)))
2201 (save-excursion
2202 (goto-char indent-point)
2203 (back-to-indentation)
2204 (or
2205 ;; We were between the special close char and the `)'.
2206 (and (eq (char-after) ?\))
2207 (eq (1+ (point)) (cdr (car special-brace-list))))
2208 ;; We were before the special close char.
2209 (and (eq (char-after) (cdr (cdr special-brace-list)))
2210 (= (c-forward-token-1) 0)
2211 (eq (1+ (point)) (cdr (car special-brace-list)))))))
2212 ;; Normal brace list check.
2213 (and (eq char-after-ip ?})
2214 (c-safe (progn (forward-char 1)
2215 (c-backward-sexp 1)
2216 t))
2217 (= (point) containing-sexp)))
2218 (c-add-syntax 'brace-list-close (c-point 'boi)))
2219 (t
2220 ;; Prepare for the rest of the cases below by going to the
2221 ;; token following the opening brace
2222 (if (consp special-brace-list)
2223 (progn
2224 (goto-char (car (car special-brace-list)))
2225 (c-forward-token-1 1 nil indent-point))
2226 (goto-char containing-sexp))
2227 (forward-char)
2228 (let ((start (point)))
2229 (c-forward-syntactic-ws indent-point)
2230 (goto-char (max start (c-point 'bol))))
2231 (skip-chars-forward " \t\n\r" indent-point)
2232 (cond
2233 ;; CASE 9C: we're looking at the first line in a brace-list
2234 ((= (point) indent-point)
2235 (goto-char containing-sexp)
2236 (c-add-syntax 'brace-list-intro (c-point 'boi))
2237 ) ; end CASE 9C
2238 ;; CASE 9D: this is just a later brace-list-entry or
2239 ;; brace-entry-open
2240 (t (if (or (eq char-after-ip ?{)
2241 (and c-special-brace-lists
2242 (save-excursion
2243 (goto-char indent-point)
2244 (c-forward-syntactic-ws (c-point 'eol))
2245 (c-looking-at-special-brace-list (point)))))
2246 (c-add-syntax 'brace-entry-open (point))
2247 (c-add-syntax 'brace-list-entry (point))
2248 )) ; end CASE 9D
2249 )))) ; end CASE 9
2250 ;; CASE 10: A continued statement
2251 ((and (not (memq char-before-ip '(?\; ?:)))
2252 (or (not (eq char-before-ip ?}))
2253 (c-looking-at-inexpr-block-backward containing-sexp))
2254 (> (point)
2255 (save-excursion
2256 (c-beginning-of-statement-1 containing-sexp)
2257 (c-forward-syntactic-ws)
2258 (setq placeholder (point))))
2259 (/= placeholder containing-sexp))
2260 (goto-char indent-point)
2261 (skip-chars-forward " \t")
2262 (let ((after-cond-placeholder
2263 (save-excursion
2264 (goto-char placeholder)
2265 (if (and c-conditional-key (looking-at c-conditional-key))
2266 (progn
2267 (c-safe (c-skip-conditional))
2268 (c-forward-syntactic-ws)
2269 (if (eq (char-after) ?\;)
2270 (progn
2271 (forward-char 1)
2272 (c-forward-syntactic-ws)))
2273 (point))
2274 nil))))
2275 (cond
2276 ;; CASE 10A: substatement
2277 ((and after-cond-placeholder
2278 (>= after-cond-placeholder indent-point))
2279 (goto-char placeholder)
2280 (if (eq char-after-ip ?{)
2281 (c-add-syntax 'substatement-open (c-point 'boi))
2282 (c-add-syntax 'substatement (c-point 'boi))))
2283 ;; CASE 10B: open braces for class or brace-lists
2284 ((setq special-brace-list
2285 (or (and c-special-brace-lists
2286 (c-looking-at-special-brace-list))
2287 (eq char-after-ip ?{)))
2288 (cond
2289 ;; CASE 10B.1: class-open
2290 ((save-excursion
2291 (goto-char indent-point)
2292 (skip-chars-forward " \t{")
2293 (let ((decl (c-search-uplist-for-classkey (c-parse-state))))
2294 (and decl
2295 (setq placeholder (aref decl 0)))
2296 ))
2297 (c-add-syntax 'class-open placeholder))
2298 ;; CASE 10B.2: brace-list-open
2299 ((or (consp special-brace-list)
2300 (save-excursion
2301 (goto-char placeholder)
2302 (looking-at "\\<enum\\>"))
2303 (save-excursion
2304 (goto-char indent-point)
2305 (while (and (> (point) placeholder)
2306 (= (c-backward-token-1 1 t) 0)
2307 (/= (char-after) ?=)))
2308 (eq (char-after) ?=)))
2309 ;; The most semantically accurate symbol here is
2310 ;; brace-list-open, but we report it simply as a
2311 ;; statement-cont. The reason is that one normally
2312 ;; adjusts brace-list-open for brace lists as
2313 ;; top-level constructs, and brace lists inside
2314 ;; statements is a completely different context.
2315 (goto-char indent-point)
2316 (c-beginning-of-closest-statement)
2317 (c-add-syntax 'statement-cont (c-point 'boi)))
2318 ;; CASE 10B.3: The body of a function declared inside a
2319 ;; normal block. This can only occur in Pike.
2320 ((and (c-major-mode-is 'pike-mode)
2321 (progn
2322 (goto-char indent-point)
2323 (not (c-looking-at-bos))))
2324 (c-beginning-of-closest-statement)
2325 (c-add-syntax 'defun-open (c-point 'boi)))
2326 ;; CASE 10B.4: catch-all for unknown construct.
2327 (t
2328 ;; Can and should I add an extensibility hook here?
2329 ;; Something like c-recognize-hook so support for
2330 ;; unknown constructs could be added. It's probably a
2331 ;; losing proposition, so I dunno.
2332 (goto-char placeholder)
2333 (c-add-syntax 'statement-cont (c-point 'boi))
2334 (c-add-syntax 'block-open))
2335 ))
2336 ;; CASE 10C: iostream insertion or extraction operator
2337 ((looking-at "<<\\|>>")
2338 (goto-char placeholder)
2339 (and after-cond-placeholder
2340 (goto-char after-cond-placeholder))
2341 (while (and (re-search-forward "<<\\|>>" indent-point 'move)
2342 (c-in-literal placeholder)))
2343 ;; if we ended up at indent-point, then the first
2344 ;; streamop is on a separate line. Indent the line like
2345 ;; a statement-cont instead
2346 (if (/= (point) indent-point)
2347 (c-add-syntax 'stream-op (c-point 'boi))
2348 (c-backward-syntactic-ws lim)
2349 (c-add-syntax 'statement-cont (c-point 'boi))))
2350 ;; CASE 10D: continued statement. find the accurate
2351 ;; beginning of statement or substatement
2352 (t
2353 (c-beginning-of-statement-1 after-cond-placeholder)
2354 ;; KLUDGE ALERT! c-beginning-of-statement-1 can leave
2355 ;; us before the lim we're passing in. It should be
2356 ;; fixed, but I'm worried about side-effects at this
2357 ;; late date. Fix for v5.
2358 (goto-char (or (and after-cond-placeholder
2359 (max after-cond-placeholder (point)))
2360 (point)))
2361 (c-add-syntax 'statement-cont (point)))
2362 )))
2363 ;; CASE 11: an else clause?
2364 ((looking-at "\\<else\\>[^_]")
2365 (c-backward-to-start-of-if containing-sexp)
2366 (c-add-syntax 'else-clause (c-point 'boi)))
2367 ;; CASE 12: Statement. But what kind? Lets see if its a
2368 ;; while closure of a do/while construct
2369 ((progn
2370 (goto-char indent-point)
2371 (skip-chars-forward " \t")
2372 (and (looking-at "while\\b[^_]")
2373 (save-excursion
2374 (c-backward-to-start-of-do containing-sexp)
2375 (setq placeholder (point))
2376 (looking-at "do\\b[^_]"))
2377 ))
2378 (goto-char placeholder)
2379 (c-add-syntax 'do-while-closure (c-point 'boi)))
2380 ;; CASE 13: A catch or finally clause? This case is simpler
2381 ;; than if-else and do-while, because a block is required
2382 ;; after every try, catch and finally.
2383 ((save-excursion
2384 (and (cond ((c-major-mode-is 'c++-mode)
2385 (looking-at "\\<catch\\>[^_]"))
2386 ((c-major-mode-is 'java-mode)
2387 (looking-at "\\<\\(catch\\|finally\\)\\>[^_]")))
2388 (c-safe (c-backward-sexp) t)
2389 (eq (char-after) ?{)
2390 (c-safe (c-backward-sexp) t)
2391 (if (eq (char-after) ?\()
2392 (c-safe (c-backward-sexp) t)
2393 t)
2394 (looking-at "\\<\\(try\\|catch\\)\\>[^_]")
2395 (setq placeholder (c-point 'boi))))
2396 (c-add-syntax 'catch-clause placeholder))
2397 ;; CASE 14: A case or default label
2398 ((looking-at c-switch-label-key)
2399 (goto-char containing-sexp)
2400 ;; check for hanging braces
2401 (if (/= (point) (c-point 'boi))
2402 (c-forward-sexp -1))
2403 (c-add-syntax 'case-label (c-point 'boi)))
2404 ;; CASE 15: any other label
2405 ((looking-at c-label-key)
2406 (goto-char containing-sexp)
2407 ;; check for hanging braces
2408 (if (/= (point) (c-point 'boi))
2409 (c-forward-sexp -1))
2410 (c-add-syntax 'label (c-point 'boi)))
2411 ;; CASE 16: block close brace, possibly closing the defun or
2412 ;; the class
2413 ((eq char-after-ip ?})
2414 (let* ((lim (c-safe-position containing-sexp fullstate))
2415 (relpos (save-excursion
2416 (goto-char containing-sexp)
2417 (if (/= (point) (c-point 'boi))
2418 (c-beginning-of-statement-1 lim))
2419 (c-point 'boi))))
2420 (cond
2421 ;; CASE 16A: closing a lambda defun or an in-expression
2422 ;; block?
2423 ((save-excursion
2424 (goto-char containing-sexp)
2425 (setq placeholder (c-looking-at-inexpr-block)))
2426 (setq tmpsymbol (if (eq (car placeholder) 'inlambda)
2427 'inline-close
2428 'block-close))
2429 (goto-char containing-sexp)
2430 (back-to-indentation)
2431 (if (= containing-sexp (point))
2432 (c-add-syntax tmpsymbol (point))
2433 (goto-char (cdr placeholder))
2434 (back-to-indentation)
2435 (c-add-syntax tmpsymbol (point))
2436 (if (/= (point) (cdr placeholder))
2437 (c-add-syntax (car placeholder)))))
2438 ;; CASE 16B: does this close an inline or a function in
2439 ;; an extern block or namespace?
2440 ((progn
2441 (goto-char containing-sexp)
2442 (setq placeholder (c-search-uplist-for-classkey state)))
2443 (goto-char (aref placeholder 0))
2444 (if (looking-at (concat c-extra-toplevel-key "[^_]"))
2445 (c-add-syntax 'defun-close relpos)
2446 (c-add-syntax 'inline-close relpos)))
2447 ;; CASE 16C: if there an enclosing brace that hasn't
2448 ;; been narrowed out by a class, then this is a
2449 ;; block-close
2450 ((and (not inenclosing-p)
2451 (c-most-enclosing-brace state)
2452 (or (not (c-major-mode-is 'pike-mode))
2453 ;; In Pike it can be a defun-close of a
2454 ;; function declared in a statement block. Let
2455 ;; it through to be handled below.
2456 (or (c-looking-at-bos)
2457 (progn
2458 (c-beginning-of-statement-1)
2459 (looking-at c-conditional-key)))))
2460 (c-add-syntax 'block-close relpos))
2461 ;; CASE 16D: find out whether we're closing a top-level
2462 ;; class or a defun
2463 (t
2464 (save-restriction
2465 (narrow-to-region (point-min) indent-point)
2466 (let ((decl (c-search-uplist-for-classkey (c-parse-state))))
2467 (if decl
2468 (c-add-class-syntax 'class-close decl)
2469 (c-add-syntax 'defun-close relpos)))))
2470 )))
2471 ;; CASE 17: statement catchall
2472 (t
2473 ;; we know its a statement, but we need to find out if it is
2474 ;; the first statement in a block
2475 (goto-char containing-sexp)
2476 (forward-char 1)
2477 (c-forward-syntactic-ws indent-point)
2478 ;; now skip forward past any case/default clauses we might find.
2479 (while (or (c-skip-case-statement-forward fullstate indent-point)
2480 (and (looking-at c-switch-label-key)
2481 (not inswitch-p)))
2482 (setq inswitch-p t))
2483 ;; we want to ignore non-case labels when skipping forward
2484 (while (and (looking-at c-label-key)
2485 (goto-char (match-end 0)))
2486 (c-forward-syntactic-ws indent-point))
2487 (cond
2488 ;; CASE 17A: we are inside a case/default clause inside a
2489 ;; switch statement. find out if we are at the statement
2490 ;; just after the case/default label.
2491 ((and inswitch-p
2492 (progn
2493 (goto-char indent-point)
2494 (c-beginning-of-statement-1 containing-sexp)
2495 (setq placeholder (point))
2496 (beginning-of-line)
2497 (when (re-search-forward c-switch-label-key
2498 (max placeholder (c-point 'eol)) t)
2499 (setq placeholder (match-beginning 0)))))
2500 (goto-char indent-point)
2501 (skip-chars-forward " \t")
2502 (if (eq (char-after) ?{)
2503 (c-add-syntax 'statement-case-open placeholder)
2504 (c-add-syntax 'statement-case-intro placeholder)))
2505 ;; CASE 17B: continued statement
2506 ((eq char-before-ip ?,)
2507 (goto-char indent-point)
2508 (c-beginning-of-closest-statement)
2509 (c-add-syntax 'statement-cont (c-point 'boi)))
2510 ;; CASE 17C: a question/colon construct? But make sure
2511 ;; what came before was not a label, and what comes after
2512 ;; is not a globally scoped function call!
2513 ((or (and (memq char-before-ip '(?: ??))
2514 (save-excursion
2515 (goto-char indent-point)
2516 (c-backward-syntactic-ws lim)
2517 (back-to-indentation)
2518 (not (looking-at c-label-key))))
2519 (and (memq char-after-ip '(?: ??))
2520 (save-excursion
2521 (goto-char indent-point)
2522 (skip-chars-forward " \t")
2523 ;; watch out for scope operator
2524 (not (looking-at "::")))))
2525 (goto-char indent-point)
2526 (c-beginning-of-closest-statement)
2527 (c-add-syntax 'statement-cont (c-point 'boi)))
2528 ;; CASE 17D: any old statement
2529 ((< (point) indent-point)
2530 (let ((safepos (c-most-enclosing-brace fullstate))
2531 relpos done)
2532 (goto-char indent-point)
2533 (c-beginning-of-statement-1 safepos)
2534 ;; It is possible we're on the brace that opens a nested
2535 ;; function.
2536 (if (and (eq (char-after) ?{)
2537 (save-excursion
2538 (c-backward-syntactic-ws safepos)
2539 (not (eq (char-before) ?\;))))
2540 (c-beginning-of-statement-1 safepos))
2541 (if (and inswitch-p
2542 (looking-at c-switch-label-key))
2543 (progn
2544 (goto-char (match-end 0))
2545 (c-forward-syntactic-ws)))
2546 (setq relpos (c-point 'boi))
2547 (while (and (not done)
2548 (<= safepos (point))
2549 (/= relpos (point)))
2550 (c-beginning-of-statement-1 safepos)
2551 (if (= relpos (c-point 'boi))
2552 (setq done t))
2553 (setq relpos (c-point 'boi)))
2554 (c-add-syntax 'statement relpos)
2555 (if (eq char-after-ip ?{)
2556 (c-add-syntax 'block-open))))
2557 ;; CASE 17E: first statement in an in-expression block
2558 ((setq placeholder
2559 (save-excursion
2560 (goto-char containing-sexp)
2561 (c-looking-at-inexpr-block)))
2562 (goto-char containing-sexp)
2563 (back-to-indentation)
2564 (let ((block-intro (if (eq (car placeholder) 'inlambda)
2565 'defun-block-intro
2566 'statement-block-intro)))
2567 (if (= containing-sexp (point))
2568 (c-add-syntax block-intro (point))
2569 (goto-char (cdr placeholder))
2570 (back-to-indentation)
2571 (c-add-syntax block-intro (point))
2572 (if (/= (point) (cdr placeholder))
2573 (c-add-syntax (car placeholder)))))
2574 (if (eq char-after-ip ?{)
2575 (c-add-syntax 'block-open)))
2576 ;; CASE 17F: first statement in an inline, or first
2577 ;; statement in a top-level defun. we can tell this is it
2578 ;; if there are no enclosing braces that haven't been
2579 ;; narrowed out by a class (i.e. don't use bod here!)
2580 ((save-excursion
2581 (save-restriction
2582 (widen)
2583 (goto-char containing-sexp)
2584 (c-narrow-out-enclosing-class state containing-sexp)
2585 (not (c-most-enclosing-brace state))))
2586 (goto-char containing-sexp)
2587 ;; if not at boi, then defun-opening braces are hung on
2588 ;; right side, so we need a different relpos
2589 (if (/= (point) (c-point 'boi))
2590 (progn
2591 (c-backward-syntactic-ws)
2592 (c-safe (c-forward-sexp (if (eq (char-before) ?\))
2593 -1 -2)))
2594 ;; looking at a Java throws clause following a
2595 ;; method's parameter list
2596 (c-beginning-of-statement-1)
2597 ))
2598 (c-add-syntax 'defun-block-intro (c-point 'boi)))
2599 ;; CASE 17G: First statement in a function declared inside
2600 ;; a normal block. This can only occur in Pike.
2601 ((and (c-major-mode-is 'pike-mode)
2602 (progn
2603 (goto-char containing-sexp)
2604 (and (not (c-looking-at-bos))
2605 (progn
2606 (c-beginning-of-statement-1)
2607 (not (looking-at c-conditional-key))))))
2608 (c-add-syntax 'defun-block-intro (c-point 'boi)))
2609 ;; CASE 17H: first statement in a block
2610 (t (goto-char containing-sexp)
2611 (if (/= (point) (c-point 'boi))
2612 (c-beginning-of-statement-1
2613 (if (= (point) lim)
2614 (c-safe-position (point) state) lim)))
2615 (c-add-syntax 'statement-block-intro (c-point 'boi))
2616 (if (eq char-after-ip ?{)
2617 (c-add-syntax 'block-open)))
2618 ))
2619 )
2620 ;; now we need to look at any modifiers
2621 (goto-char indent-point)
2622 (skip-chars-forward " \t")
2623 (cond
2624 ;; are we looking at a comment only line?
2625 ((and (looking-at c-comment-start-regexp)
2626 (/= (c-forward-token-1 0 nil (c-point 'eol)) 0))
2627 (c-add-syntax 'comment-intro))
2628 ;; we might want to give additional offset to friends (in C++).
2629 ((and (c-major-mode-is 'c++-mode)
2630 (looking-at c-C++-friend-key))
2631 (c-add-syntax 'friend))
2632 ;; Start of a preprocessor directive?
2633 ((and (eq literal 'pound)
2634 (= (save-excursion
2635 (c-beginning-of-macro lim)
2636 (setq placeholder (point)))
2637 (c-point 'boi))
2638 (not (and (c-major-mode-is 'pike-mode)
2639 (eq (char-after (1+ placeholder)) ?\"))))
2640 (c-add-syntax 'cpp-macro)))
2641 ;; return the syntax
2642 syntax))))
2643
2644 \f
2645 (defun c-echo-parsing-error (&optional quiet)
2646 (when (and c-parsing-error (not quiet))
2647 (message "%s" c-parsing-error)
2648 (ding))
2649 c-parsing-error)
2650
2651 (defun c-shift-line-indentation (shift-amt)
2652 (let ((pos (- (point-max) (point)))
2653 (col (current-indentation)))
2654 (if (zerop shift-amt)
2655 nil
2656 (delete-region (c-point 'bol) (c-point 'boi))
2657 (beginning-of-line)
2658 (indent-to (+ col shift-amt)))
2659 (if (< (point) (c-point 'boi))
2660 (back-to-indentation)
2661 ;; If initial point was within line's indentation, position after
2662 ;; the indentation. Else stay at same point in text.
2663 (if (> (- (point-max) pos) (point))
2664 (goto-char (- (point-max) pos))))))
2665
2666 (defun c-evaluate-offset (offset langelem symbol)
2667 ;; offset can be a number, a function, a variable, a list, or one of
2668 ;; the symbols + or -
2669 (cond
2670 ((eq offset '+) c-basic-offset)
2671 ((eq offset '-) (- c-basic-offset))
2672 ((eq offset '++) (* 2 c-basic-offset))
2673 ((eq offset '--) (* 2 (- c-basic-offset)))
2674 ((eq offset '*) (/ c-basic-offset 2))
2675 ((eq offset '/) (/ (- c-basic-offset) 2))
2676 ((numberp offset) offset)
2677 ((functionp offset) (c-evaluate-offset
2678 (funcall offset langelem) langelem symbol))
2679 ((vectorp offset) offset)
2680 ((null offset) nil)
2681 ((listp offset)
2682 (let (done)
2683 (while (and (not done) offset)
2684 (setq done (c-evaluate-offset (car offset) langelem symbol)
2685 offset (cdr offset)))
2686 (if (not done)
2687 (if c-strict-syntax-p
2688 (error "No offset found for syntactic symbol %s" symbol))
2689 done)))
2690 (t (symbol-value offset))
2691 ))
2692
2693 (defun c-get-offset (langelem)
2694 ;; Get offset from LANGELEM which is a cons cell of the form:
2695 ;; (SYMBOL . RELPOS). The symbol is matched against
2696 ;; c-offsets-alist and the offset found there is either returned,
2697 ;; or added to the indentation at RELPOS. If RELPOS is nil, then
2698 ;; the offset is simply returned.
2699 (let* ((symbol (car langelem))
2700 (relpos (cdr langelem))
2701 (match (assq symbol c-offsets-alist))
2702 (offset (cdr-safe match)))
2703 (if (not match)
2704 (if c-strict-syntax-p
2705 (error "No offset found for syntactic symbol %s" symbol)
2706 (setq offset 0
2707 relpos 0))
2708 (setq offset (c-evaluate-offset offset langelem symbol)))
2709 (if (vectorp offset)
2710 offset
2711 (+ (if (and relpos
2712 (< relpos (c-point 'bol)))
2713 (save-excursion
2714 (goto-char relpos)
2715 (current-column))
2716 0)
2717 (or (and (numberp offset) offset)
2718 (and (symbolp offset) (symbol-value offset))
2719 0)))
2720 ))
2721
2722 (defun c-get-syntactic-indentation (langelems)
2723 ;; Apply c-get-offset to a list of langelem cells to get the total
2724 ;; syntactic indentation. Special treatment is needed for vectors
2725 ;; containing absolute columns.
2726 (let ((indent 0))
2727 (catch 'done
2728 (while langelems
2729 (let ((res (c-get-offset (car langelems))))
2730 (if (vectorp res)
2731 (throw 'done (elt res 0))
2732 (setq indent (+ indent res)
2733 langelems (cdr langelems)))))
2734 indent)))
2735
2736 (defun c-indent-line (&optional syntax quiet)
2737 ;; Indent the current line according to the syntactic context, if
2738 ;; c-syntactic-indentation is non-nil. Optional SYNTAX is the
2739 ;; syntactic information for the current line. Be silent about
2740 ;; syntactic errors if the optional argument QUIET is non-nil.
2741 ;; Returns the amount of indentation change (in columns).
2742 (let (shift-amt)
2743 (if c-syntactic-indentation
2744 (setq c-parsing-error
2745 (or (let* ((c-parsing-error nil)
2746 (c-syntactic-context (or syntax
2747 c-syntactic-context
2748 (c-guess-basic-syntax)))
2749 (indent (c-get-syntactic-indentation c-syntactic-context)))
2750 (and (not (c-echo-parsing-error quiet))
2751 c-echo-syntactic-information-p
2752 (message "syntax: %s, indent: %d"
2753 c-syntactic-context indent))
2754 (setq shift-amt (- indent (current-indentation)))
2755 (c-shift-line-indentation shift-amt)
2756 (run-hooks 'c-special-indent-hook)
2757 c-parsing-error)
2758 c-parsing-error))
2759 (let ((indent 0))
2760 (save-excursion
2761 (while (and (= (forward-line -1) 0)
2762 (if (looking-at "\\s-*$")
2763 t
2764 (back-to-indentation)
2765 (setq indent (current-indentation))
2766 nil))))
2767 (setq shift-amt (- indent (current-indentation)))
2768 (c-shift-line-indentation shift-amt)))
2769 shift-amt))
2770
2771 (defun c-show-syntactic-information (arg)
2772 "Show syntactic information for current line.
2773 With universal argument, inserts the analysis as a comment on that line."
2774 (interactive "P")
2775 (let ((syntax (c-guess-basic-syntax)))
2776 (if (not (consp arg))
2777 (message "syntactic analysis: %s" syntax)
2778 (indent-for-comment)
2779 (insert (format "%s" syntax))
2780 ))
2781 (c-keep-region-active))
2782
2783 (defun c-syntactic-information-on-region (from to)
2784 "Inserts a comment with the syntactic analysis on every line in the region."
2785 (interactive "*r")
2786 (save-excursion
2787 (save-restriction
2788 (narrow-to-region from to)
2789 (goto-char (point-min))
2790 (while (not (eobp))
2791 (c-show-syntactic-information '(0))
2792 (forward-line)))))
2793
2794 \f
2795 (cc-provide 'cc-engine)
2796
2797 ;;; cc-engine.el ends here