Some fixes to follow coding conventions.
[bpt/emacs.git] / lisp / progmodes / cc-engine.el
1 ;;; cc-engine.el --- core syntax guessing engine for CC mode
2
3 ;; Copyright (C) 1985,1987,1992-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-bod -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 (c-with-syntax-table (if (c-major-mode-is 'c++-mode)
1032 c++-template-syntax-table
1033 (syntax-table))
1034 (while (and (< limit (point))
1035 (eq (char-before) ?,))
1036 ;; this will catch member inits with multiple
1037 ;; line arglists
1038 (forward-char -1)
1039 (c-backward-syntactic-ws)
1040 (if (eq (char-before) ?\))
1041 (c-backward-sexp 2)
1042 (c-backward-sexp 1))
1043 ;; Skip over any template arg to the class.
1044 (if (eq (char-after) ?<)
1045 (c-backward-sexp 1))
1046 ;; Skip backwards over a fully::qualified::name.
1047 (c-backward-syntactic-ws limit)
1048 (while (and (eq (char-before) ?:)
1049 (save-excursion
1050 (forward-char -1)
1051 (eq (char-before) ?:)))
1052 (backward-char 2)
1053 (c-backward-sexp 1))
1054 ;; now continue checking
1055 (c-backward-syntactic-ws limit)))
1056 (and (< limit (point))
1057 (eq (char-before) ?:)))
1058
1059 (defun c-skip-case-statement-forward (state &optional lim)
1060 ;; skip forward over case/default bodies, with optional maximal
1061 ;; limit. if no next case body is found, nil is returned and point
1062 ;; is not moved
1063 (let ((lim (or lim (point-max)))
1064 (here (point))
1065 donep foundp bufpos
1066 (safepos (point))
1067 (balanced (car state)))
1068 ;; search until we've passed the limit, or we've found our match
1069 (while (and (< (point) lim)
1070 (not donep))
1071 (setq safepos (point))
1072 ;; see if we can find a case statement, not in a literal
1073 (if (and (re-search-forward c-switch-label-key lim 'move)
1074 (setq bufpos (match-beginning 0))
1075 (not (c-in-literal safepos))
1076 (/= bufpos here))
1077 ;; if we crossed into a balanced sexp, we know the case is
1078 ;; not part of our switch statement, so just bound over the
1079 ;; sexp and keep looking.
1080 (if (and (consp balanced)
1081 (> bufpos (car balanced))
1082 (< bufpos (cdr balanced)))
1083 (goto-char (cdr balanced))
1084 (goto-char bufpos)
1085 (setq donep t
1086 foundp t))))
1087 (if (not foundp)
1088 (goto-char here))
1089 foundp))
1090
1091 (defun c-search-uplist-for-classkey (brace-state)
1092 ;; search for the containing class, returning a 2 element vector if
1093 ;; found. aref 0 contains the bufpos of the boi of the class key
1094 ;; line, and aref 1 contains the bufpos of the open brace.
1095 (if (null brace-state)
1096 ;; no brace-state means we cannot be inside a class
1097 nil
1098 (let ((carcache (car brace-state))
1099 search-start search-end)
1100 (if (consp carcache)
1101 ;; a cons cell in the first element means that there is some
1102 ;; balanced sexp before the current bufpos. this we can
1103 ;; ignore. the nth 1 and nth 2 elements define for us the
1104 ;; search boundaries
1105 (setq search-start (nth 2 brace-state)
1106 search-end (nth 1 brace-state))
1107 ;; if the car was not a cons cell then nth 0 and nth 1 define
1108 ;; for us the search boundaries
1109 (setq search-start (nth 1 brace-state)
1110 search-end (nth 0 brace-state)))
1111 ;; search-end cannot be a cons cell
1112 (and (consp search-end)
1113 (error "consp search-end: %s" search-end))
1114 ;; if search-end is nil, or if the search-end character isn't an
1115 ;; open brace, we are definitely not in a class
1116 (if (or (not search-end)
1117 (< search-end (point-min))
1118 (not (eq (char-after search-end) ?{)))
1119 nil
1120 ;; now, we need to look more closely at search-start. if
1121 ;; search-start is nil, then our start boundary is really
1122 ;; point-min.
1123 (if (not search-start)
1124 (setq search-start (point-min))
1125 ;; if search-start is a cons cell, then we can start
1126 ;; searching from the end of the balanced sexp just ahead of
1127 ;; us
1128 (if (consp search-start)
1129 (setq search-start (cdr search-start))))
1130 ;; now we can do a quick regexp search from search-start to
1131 ;; search-end and see if we can find a class key. watch for
1132 ;; class like strings in literals
1133 (save-excursion
1134 (save-restriction
1135 (goto-char search-start)
1136 (let ((search-key (concat c-class-key "\\|" c-extra-toplevel-key))
1137 foundp class match-end)
1138 (if c-inexpr-class-key
1139 (setq search-key (concat search-key "\\|"
1140 c-inexpr-class-key)))
1141 (while (and (not foundp)
1142 (progn
1143 (c-forward-syntactic-ws)
1144 (> search-end (point)))
1145 (re-search-forward search-key search-end t))
1146 (setq class (match-beginning 0)
1147 match-end (match-end 0))
1148 (if (c-in-literal search-start)
1149 nil ; its in a comment or string, ignore
1150 (goto-char class)
1151 (skip-chars-forward " \t\n")
1152 (setq foundp (vector (c-point 'boi) search-end))
1153 (cond
1154 ;; check for embedded keywords
1155 ((let ((char (char-after (1- class))))
1156 (and char
1157 (memq (char-syntax char) '(?w ?_))))
1158 (goto-char match-end)
1159 (setq foundp nil))
1160 ;; make sure we're really looking at the start of a
1161 ;; class definition, and not a forward decl, return
1162 ;; arg, template arg list, or an ObjC or Java method.
1163 ((and c-method-key
1164 (re-search-forward c-method-key search-end t)
1165 (not (c-in-literal class)))
1166 (setq foundp nil))
1167 ;; Check if this is an anonymous inner class.
1168 ((and c-inexpr-class-key
1169 (looking-at c-inexpr-class-key))
1170 (while (and (= (c-forward-token-1 1 t) 0)
1171 (looking-at "(\\|\\w\\|\\s_\\|\\.")))
1172 (if (eq (point) search-end)
1173 ;; We're done. Just trap this case in the cond.
1174 nil
1175 ;; False alarm; all conditions aren't satisfied.
1176 (setq foundp nil)))
1177 ;; Its impossible to define a regexp for this, and
1178 ;; nearly so to do it programmatically.
1179 ;;
1180 ;; ; picks up forward decls
1181 ;; = picks up init lists
1182 ;; ) picks up return types
1183 ;; > picks up templates, but remember that we can
1184 ;; inherit from templates!
1185 ((let ((skipchars "^;=)"))
1186 ;; try to see if we found the `class' keyword
1187 ;; inside a template arg list
1188 (save-excursion
1189 (skip-chars-backward "^<>" search-start)
1190 (if (eq (char-before) ?<)
1191 (setq skipchars (concat skipchars ">"))))
1192 (while (progn
1193 (skip-chars-forward skipchars search-end)
1194 (c-in-literal class))
1195 (forward-char))
1196 (/= (point) search-end))
1197 (setq foundp nil))
1198 )))
1199 foundp))
1200 )))))
1201
1202 (defun c-inside-bracelist-p (containing-sexp brace-state)
1203 ;; return the buffer position of the beginning of the brace list
1204 ;; statement if we're inside a brace list, otherwise return nil.
1205 ;; CONTAINING-SEXP is the buffer pos of the innermost containing
1206 ;; paren. BRACE-STATE is the remainder of the state of enclosing
1207 ;; braces
1208 ;;
1209 ;; N.B.: This algorithm can potentially get confused by cpp macros
1210 ;; places in inconvenient locations. Its a trade-off we make for
1211 ;; speed.
1212 (or
1213 ;; this will pick up enum lists
1214 (c-safe
1215 (save-excursion
1216 (goto-char containing-sexp)
1217 (c-forward-sexp -1)
1218 (let (bracepos)
1219 (if (and (or (looking-at "enum[\t\n ]+")
1220 (progn (c-forward-sexp -1)
1221 (looking-at "enum[\t\n ]+")))
1222 (setq bracepos (c-safe (scan-lists (point) 1 -1)))
1223 (not (c-crosses-statement-barrier-p (point)
1224 (- bracepos 2))))
1225 (point)))))
1226 ;; this will pick up array/aggregate init lists, even if they are nested.
1227 (save-excursion
1228 (let ((class-key
1229 ;; Pike can have class definitions anywhere, so we must
1230 ;; check for the class key here.
1231 (and (c-major-mode-is 'pike-mode)
1232 (concat c-class-key "\\|" c-extra-toplevel-key)))
1233 bufpos lim braceassignp)
1234 (while (and (not bufpos)
1235 containing-sexp)
1236 (if (consp containing-sexp)
1237 (setq containing-sexp (car brace-state)
1238 brace-state (cdr brace-state))
1239 (goto-char containing-sexp)
1240 (if (c-looking-at-inexpr-block)
1241 ;; We're in an in-expression block of some kind. Do
1242 ;; not check nesting.
1243 (setq containing-sexp nil)
1244 ;; see if the open brace is preceded by = or [...] in
1245 ;; this statement, but watch out for operator=
1246 (setq lim (if (consp (car brace-state))
1247 (cdr (car brace-state))
1248 (car brace-state))
1249 braceassignp 'dontknow)
1250 (c-backward-token-1 1 t lim)
1251 ;; Checks to do only on the first sexp before the brace.
1252 (when (and (c-major-mode-is 'java-mode)
1253 (eq (char-after) ?\[))
1254 ;; In Java, an initialization brace list may follow
1255 ;; directly after "new Foo[]", so check for a "new"
1256 ;; earlier.
1257 (while (eq braceassignp 'dontknow)
1258 (setq braceassignp
1259 (cond ((/= (c-backward-token-1 1 t lim) 0) nil)
1260 ((looking-at "new\\>[^_]") t)
1261 ((looking-at "\\sw\\|\\s_\\|[.[]")
1262 ;; Carry on looking if this is an
1263 ;; identifier (may contain "." in Java)
1264 ;; or another "[]" sexp.
1265 'dontknow)
1266 (t nil)))))
1267 ;; Checks to do on all sexps before the brace, up to the
1268 ;; beginning of the statement.
1269 (while (eq braceassignp 'dontknow)
1270 (cond ((eq (char-after) ?\;)
1271 (setq braceassignp nil))
1272 ((and class-key
1273 (looking-at class-key))
1274 (setq braceassignp nil))
1275 ((eq (char-after) ?=)
1276 ;; We've seen a =, but must check earlier tokens so
1277 ;; that it isn't something that should be ignored.
1278 (setq braceassignp 'maybe)
1279 (while (and (eq braceassignp 'maybe)
1280 (zerop (c-backward-token-1 1 t lim)))
1281 (setq braceassignp
1282 (cond
1283 ;; Check for operator =
1284 ((looking-at "operator\\>") nil)
1285 ;; Check for `<opchar>= in Pike.
1286 ((and (c-major-mode-is 'pike-mode)
1287 (or (eq (char-after) ?`)
1288 ;; Special case for Pikes
1289 ;; `[]=, since '[' is not in
1290 ;; the punctuation class.
1291 (and (eq (char-after) ?\[)
1292 (eq (char-before) ?`))))
1293 nil)
1294 ((looking-at "\\s.") 'maybe)
1295 ;; make sure we're not in a C++ template
1296 ;; argument assignment
1297 ((and (c-major-mode-is 'c++-mode)
1298 (save-excursion
1299 (let ((here (point))
1300 (pos< (progn
1301 (skip-chars-backward "^<>")
1302 (point))))
1303 (and (eq (char-before) ?<)
1304 (not (c-crosses-statement-barrier-p
1305 pos< here))
1306 (not (c-in-literal))
1307 ))))
1308 nil)
1309 (t t))))))
1310 (if (and (eq braceassignp 'dontknow)
1311 (/= (c-backward-token-1 1 t lim) 0))
1312 (setq braceassignp nil)))
1313 (if (not braceassignp)
1314 (if (eq (char-after) ?\;)
1315 ;; Brace lists can't contain a semicolon, so we're done.
1316 (setq containing-sexp nil)
1317 ;; lets see if we're nested. find the most nested
1318 ;; containing brace
1319 (setq containing-sexp (car brace-state)
1320 brace-state (cdr brace-state)))
1321 ;; we've hit the beginning of the aggregate list
1322 (c-beginning-of-statement-1
1323 (c-most-enclosing-brace brace-state))
1324 (setq bufpos (point))))
1325 ))
1326 bufpos))
1327 ))
1328
1329 (defun c-looking-at-special-brace-list (&optional lim)
1330 ;; If we're looking at the start of a pike-style list, ie `({ })',
1331 ;; `([ ])', `(< >)' etc, a cons of a cons of its starting and ending
1332 ;; positions and its entry in c-special-brace-lists is returned, nil
1333 ;; otherwise. The ending position is nil if the list is still open.
1334 ;; LIM is the limit for forward search. The point may either be at
1335 ;; the `(' or at the following paren character. Tries to check the
1336 ;; matching closer, but assumes it's correct if no balanced paren is
1337 ;; found (i.e. the case `({ ... } ... )' is detected as _not_ being
1338 ;; a special brace list).
1339 (if c-special-brace-lists
1340 (condition-case ()
1341 (save-excursion
1342 (let ((beg (point))
1343 end type)
1344 (c-forward-syntactic-ws)
1345 (if (eq (char-after) ?\()
1346 (progn
1347 (forward-char 1)
1348 (c-forward-syntactic-ws)
1349 (setq type (assq (char-after) c-special-brace-lists)))
1350 (if (setq type (assq (char-after) c-special-brace-lists))
1351 (progn
1352 (c-backward-syntactic-ws)
1353 (forward-char -1)
1354 (setq beg (if (eq (char-after) ?\()
1355 (point)
1356 nil)))))
1357 (if (and beg type)
1358 (if (and (c-safe (goto-char beg)
1359 (c-forward-sexp 1)
1360 (setq end (point))
1361 (= (char-before) ?\)))
1362 (c-safe (goto-char beg)
1363 (forward-char 1)
1364 (c-forward-sexp 1)
1365 ;; Kludges needed to handle inner
1366 ;; chars both with and without
1367 ;; paren syntax.
1368 (or (/= (char-syntax (char-before)) ?\))
1369 (= (char-before) (cdr type)))))
1370 (if (or (/= (char-syntax (char-before)) ?\))
1371 (= (progn
1372 (c-forward-syntactic-ws)
1373 (point))
1374 (1- end)))
1375 (cons (cons beg end) type))
1376 (cons (list beg) type)))))
1377 (error nil))))
1378
1379 (defun c-looking-at-bos ()
1380 ;; Returns nil if inside a statement or declaration.
1381 (save-excursion
1382 (c-backward-syntactic-ws)
1383 (or (bobp)
1384 (memq (char-before) '(?\; ?}))
1385 (and (eq (char-before) ?{)
1386 (not (and c-special-brace-lists
1387 (progn (backward-char)
1388 (c-looking-at-special-brace-list))))))))
1389
1390 (defun c-looking-at-inexpr-block (&optional lim)
1391 ;; Returns non-nil if we're looking at the beginning of a block
1392 ;; inside an expression. The value returned is actually a cons of
1393 ;; either 'inlambda, 'inexpr-statement or 'inexpr-class and the
1394 ;; position of the beginning of the construct. LIM limits the
1395 ;; backward search.
1396 (save-excursion
1397 (or lim (setq lim (point-min)))
1398 (let ((block-follows (eq (char-after) ?{)))
1399 ;; Look at the character after point only as a last resort when
1400 ;; we can't disambiguate.
1401 (if (and block-follows
1402 (progn (c-backward-syntactic-ws) (> (point) lim))
1403 (eq (char-before) ?\()
1404 (not (and c-special-brace-lists
1405 (c-looking-at-special-brace-list))))
1406 (cons 'inexpr-statement (point))
1407 (let (res)
1408 (while (and (not res)
1409 (= (c-backward-token-1 1 t lim) 0)
1410 (>= (point) lim)
1411 (looking-at "(\\|\\w\\|\\s_\\|\\."))
1412 (setq res
1413 (cond ((and block-follows
1414 c-inexpr-class-key
1415 (looking-at c-inexpr-class-key)
1416 (or (not (looking-at c-class-key))
1417 (let ((prev (point)))
1418 (while (and (= (c-backward-token-1 1 t lim)
1419 0)
1420 (>= (point) lim)
1421 (eq (char-syntax (char-after))
1422 ?w))
1423 (setq prev (point)))
1424 (goto-char prev)
1425 (not (c-looking-at-bos)))))
1426 (cons 'inexpr-class (point)))
1427 ((and c-inexpr-block-key
1428 (looking-at c-inexpr-block-key))
1429 (cons 'inexpr-statement (point)))
1430 ((and c-lambda-key
1431 (looking-at c-lambda-key))
1432 (cons 'inlambda (point))))))
1433 res)))))
1434
1435 (defun c-looking-at-inexpr-block-backward (&optional lim)
1436 ;; Returns non-nil if we're looking at the end of an in-expression
1437 ;; block, otherwise the same as `c-looking-at-inexpr-block'.
1438 (save-excursion
1439 (let ((lim (or lim (c-point 'bod))))
1440 (c-safe
1441 (c-backward-syntactic-ws lim)
1442 (if (eq (char-before) ?}) ; Recognize only a block currently.
1443 (progn
1444 (c-forward-sexp -1)
1445 (if (>= (point) lim)
1446 (c-looking-at-inexpr-block lim))))))))
1447
1448 (defun c-on-identifier ()
1449 ;; Returns non-nil if we're on or directly after an identifier.
1450 (if (or (memq (char-syntax (or (char-after) ? )) '(?w ?_))
1451 (memq (char-syntax (or (char-before) ? )) '(?w ?_)))
1452 (save-excursion
1453 (skip-syntax-backward "w_")
1454 (not (looking-at c-keywords)))
1455 (if (c-major-mode-is 'pike-mode)
1456 ;; Handle the `<operator> syntax in Pike.
1457 (save-excursion
1458 (if (eq (char-after) ?\`) (forward-char))
1459 (skip-chars-backward "!%&*+\\-/<=>^|~")
1460 (let ((pos (point)))
1461 (cond ((memq (char-before) '(?\) ?\]))
1462 (c-safe (backward-char 2)))
1463 ((memq (char-before) '(?\( ?\[))
1464 (c-safe (backward-char 1))))
1465 (if (not (looking-at "()\\|\\[]"))
1466 (goto-char pos)))
1467 (and (eq (char-before) ?\`)
1468 (looking-at "[-!%&*+/<=>^|~]\\|()\\|\\[]"))))))
1469
1470 \f
1471 (defun c-most-enclosing-brace (state)
1472 ;; return the bufpos of the most enclosing brace that hasn't been
1473 ;; narrowed out by any enclosing class, or nil if none was found
1474 (let (enclosingp)
1475 (while (and state (not enclosingp))
1476 (setq enclosingp (car state)
1477 state (cdr state))
1478 (if (consp enclosingp)
1479 (setq enclosingp nil)
1480 (if (> (point-min) enclosingp)
1481 (setq enclosingp nil))
1482 (setq state nil)))
1483 enclosingp))
1484
1485 (defun c-least-enclosing-brace (state)
1486 ;; return the bufpos of the least (highest) enclosing brace that
1487 ;; hasn't been narrowed out by any enclosing class, or nil if none
1488 ;; was found.
1489 (c-most-enclosing-brace (nreverse state)))
1490
1491 (defun c-safe-position (bufpos state)
1492 ;; return the closest known safe position higher up than point
1493 (let ((safepos nil))
1494 (while state
1495 (setq safepos
1496 (if (consp (car state))
1497 (cdr (car state))
1498 (car state)))
1499 (if (< safepos bufpos)
1500 (setq state nil)
1501 (setq state (cdr state))))
1502 safepos))
1503
1504 (defun c-narrow-out-enclosing-class (state lim)
1505 ;; narrow the buffer so that the enclosing class is hidden
1506 (setq state (c-whack-state (point) state))
1507 (let (inclass-p)
1508 (and state
1509 (setq inclass-p (c-search-uplist-for-classkey state))
1510 (narrow-to-region
1511 (progn
1512 (goto-char (1+ (aref inclass-p 1)))
1513 (skip-chars-forward " \t\n" lim)
1514 ;; if point is now left of the class opening brace, we're
1515 ;; hosed, so try a different tact
1516 (if (<= (point) (aref inclass-p 1))
1517 (progn
1518 (goto-char (1+ (aref inclass-p 1)))
1519 (c-forward-syntactic-ws lim)))
1520 (point))
1521 ;; end point is the end of the current line
1522 (progn
1523 (goto-char lim)
1524 (c-point 'eol))))
1525 ;; return the class vector
1526 inclass-p))
1527
1528 \f
1529 ;; This function implements the main decision tree for determining the
1530 ;; syntactic analysis of the current line of code. Yes, it's huge and
1531 ;; bloated!
1532
1533 (defun c-guess-basic-syntax ()
1534 (save-excursion
1535 (save-restriction
1536 (beginning-of-line)
1537 (let* ((indent-point (point))
1538 (case-fold-search nil)
1539 (fullstate (c-parse-state))
1540 (state fullstate)
1541 literal containing-sexp char-before-ip char-after-ip lim
1542 syntax placeholder c-in-literal-cache inswitch-p
1543 tmpsymbol keyword injava-inher special-brace-list
1544 ;; narrow out any enclosing class or extern "C" block
1545 (inclass-p (c-narrow-out-enclosing-class state indent-point))
1546 inenclosing-p)
1547 ;; check for meta top-level enclosing constructs, possible
1548 ;; extern language definitions, possibly (in C++) namespace
1549 ;; definitions.
1550 (save-excursion
1551 (save-restriction
1552 (widen)
1553 (if (and inclass-p
1554 (progn
1555 (goto-char (aref inclass-p 0))
1556 (looking-at (concat c-extra-toplevel-key "[^_]"))))
1557 (let ((enclosing (match-string 1)))
1558 (cond
1559 ((string-equal enclosing "extern")
1560 (setq inenclosing-p 'extern))
1561 ((string-equal enclosing "namespace")
1562 (setq inenclosing-p 'namespace))
1563 )))))
1564 ;; get the buffer position of the most nested opening brace,
1565 ;; if there is one, and it hasn't been narrowed out
1566 (save-excursion
1567 (goto-char indent-point)
1568 (skip-chars-forward " \t}")
1569 (skip-chars-backward " \t")
1570 (while (and state
1571 (not containing-sexp))
1572 (setq containing-sexp (car state)
1573 state (cdr state))
1574 (if (consp containing-sexp)
1575 ;; if cdr == point, then containing sexp is the brace
1576 ;; that opens the sexp we close
1577 (if (= (cdr containing-sexp) (point))
1578 (setq containing-sexp (car containing-sexp))
1579 ;; otherwise, ignore this element
1580 (setq containing-sexp nil))
1581 ;; ignore the bufpos if its been narrowed out by the
1582 ;; containing class or does not contain the indent point
1583 (if (or (<= containing-sexp (point-min))
1584 (>= containing-sexp indent-point))
1585 (setq containing-sexp nil)))))
1586
1587 ;; set the limit on the farthest back we need to search
1588 (setq lim (or containing-sexp
1589 (if (consp (car fullstate))
1590 (cdr (car fullstate))
1591 nil)
1592 (point-min)))
1593
1594 ;; cache char before and after indent point, and move point to
1595 ;; the most likely position to perform the majority of tests
1596 (goto-char indent-point)
1597 (skip-chars-forward " \t")
1598 (setq char-after-ip (char-after))
1599 (c-backward-syntactic-ws lim)
1600 (setq char-before-ip (char-before))
1601 (goto-char indent-point)
1602 (skip-chars-forward " \t")
1603
1604 ;; are we in a literal?
1605 (setq literal (c-in-literal lim))
1606
1607 ;; now figure out syntactic qualities of the current line
1608 (cond
1609 ;; CASE 1: in a string.
1610 ((memq literal '(string))
1611 (c-add-syntax 'string (c-point 'bopl)))
1612 ;; CASE 2: in a C or C++ style comment.
1613 ((memq literal '(c c++))
1614 (c-add-syntax literal (car (c-literal-limits lim))))
1615 ;; CASE 3: in a cpp preprocessor macro continuation.
1616 ((and (eq literal 'pound)
1617 (/= (save-excursion
1618 (c-beginning-of-macro lim)
1619 (setq placeholder (point)))
1620 (c-point 'boi)))
1621 (c-add-syntax 'cpp-macro-cont placeholder))
1622 ;; CASE 4: In-expression statement.
1623 ((and (or c-inexpr-class-key c-inexpr-block-key c-lambda-key)
1624 (setq placeholder (c-looking-at-inexpr-block)))
1625 (setq tmpsymbol (assq (car placeholder)
1626 '((inexpr-class . class-open)
1627 (inexpr-statement . block-open))))
1628 (if tmpsymbol
1629 ;; It's a statement block or an anonymous class.
1630 (setq tmpsymbol (cdr tmpsymbol))
1631 ;; It's a Pike lambda. Check whether we are between the
1632 ;; lambda keyword and the argument list or at the defun
1633 ;; opener.
1634 (setq tmpsymbol (if (eq char-after-ip ?{)
1635 'inline-open
1636 'lambda-intro-cont)))
1637 (goto-char (cdr placeholder))
1638 (back-to-indentation)
1639 (c-add-syntax tmpsymbol (point))
1640 (unless (eq (point) (cdr placeholder))
1641 (c-add-syntax (car placeholder))))
1642 ;; CASE 5: Line is at top level.
1643 ((null containing-sexp)
1644 (cond
1645 ;; CASE 5A: we are looking at a defun, brace list, class,
1646 ;; or inline-inclass method opening brace
1647 ((setq special-brace-list
1648 (or (and c-special-brace-lists
1649 (c-looking-at-special-brace-list))
1650 (eq char-after-ip ?{)))
1651 (cond
1652 ;; CASE 5A.1: extern language or namespace construct
1653 ((save-excursion
1654 (goto-char indent-point)
1655 (skip-chars-forward " \t")
1656 (and (c-safe (progn (c-backward-sexp 2) t))
1657 (looking-at (concat c-extra-toplevel-key "[^_]"))
1658 (setq keyword (match-string 1)
1659 placeholder (point))
1660 (or (and (string-equal keyword "namespace")
1661 (setq tmpsymbol 'namespace-open))
1662 (and (string-equal keyword "extern")
1663 (progn
1664 (c-forward-sexp 1)
1665 (c-forward-syntactic-ws)
1666 (eq (char-after) ?\"))
1667 (setq tmpsymbol 'extern-lang-open)))
1668 ))
1669 (goto-char placeholder)
1670 (c-add-syntax tmpsymbol (c-point 'boi)))
1671 ;; CASE 5A.2: we are looking at a class opening brace
1672 ((save-excursion
1673 (goto-char indent-point)
1674 (skip-chars-forward " \t{")
1675 ;; TBD: watch out! there could be a bogus
1676 ;; c-state-cache in place when we get here. we have
1677 ;; to go through much chicanery to ignore the cache.
1678 ;; But of course, there may not be! BLECH! BOGUS!
1679 (let ((decl
1680 (let ((c-state-cache nil))
1681 (c-search-uplist-for-classkey (c-parse-state))
1682 )))
1683 (and decl
1684 (setq placeholder (aref decl 0)))
1685 ))
1686 (c-add-syntax 'class-open placeholder))
1687 ;; CASE 5A.3: brace list open
1688 ((save-excursion
1689 (c-beginning-of-statement-1 lim)
1690 ;; c-b-o-s could have left us at point-min
1691 (and (bobp)
1692 (c-forward-syntactic-ws indent-point))
1693 (if (looking-at "typedef[^_]")
1694 (progn (c-forward-sexp 1)
1695 (c-forward-syntactic-ws indent-point)))
1696 (setq placeholder (c-point 'boi))
1697 (or (consp special-brace-list)
1698 (and (or (save-excursion
1699 (goto-char indent-point)
1700 (setq tmpsymbol nil)
1701 (while (and (> (point) placeholder)
1702 (= (c-backward-token-1 1 t) 0)
1703 (/= (char-after) ?=))
1704 (if (and (not tmpsymbol)
1705 (looking-at "new\\>[^_]"))
1706 (setq tmpsymbol 'topmost-intro-cont)))
1707 (eq (char-after) ?=))
1708 (looking-at "enum[ \t\n]+"))
1709 (save-excursion
1710 (while (and (< (point) indent-point)
1711 (= (c-forward-token-1 1 t) 0)
1712 (not (memq (char-after) '(?\; ?\()))))
1713 (not (memq (char-after) '(?\; ?\()))
1714 ))))
1715 (if (and (c-major-mode-is 'java-mode)
1716 (eq tmpsymbol 'topmost-intro-cont))
1717 ;; We're in Java and have found that the open brace
1718 ;; belongs to a "new Foo[]" initialization list,
1719 ;; which means the brace list is part of an
1720 ;; expression and not a top level definition. We
1721 ;; therefore treat it as any topmost continuation
1722 ;; even though the semantically correct symbol still
1723 ;; is brace-list-open, on the same grounds as in
1724 ;; case 10B.2.
1725 (progn
1726 (c-beginning-of-statement-1 lim)
1727 (c-forward-syntactic-ws)
1728 (c-add-syntax 'topmost-intro-cont (c-point 'boi)))
1729 (c-add-syntax 'brace-list-open placeholder)))
1730 ;; CASE 5A.4: inline defun open
1731 ((and inclass-p (not inenclosing-p))
1732 (c-add-syntax 'inline-open)
1733 (c-add-class-syntax 'inclass inclass-p))
1734 ;; CASE 5A.5: ordinary defun open
1735 (t
1736 (goto-char placeholder)
1737 (if inclass-p
1738 (c-add-syntax 'defun-open (c-point 'boi))
1739 (c-add-syntax 'defun-open (c-point 'bol)))
1740 )))
1741 ;; CASE 5B: first K&R arg decl or member init
1742 ((c-just-after-func-arglist-p)
1743 (cond
1744 ;; CASE 5B.1: a member init
1745 ((or (eq char-before-ip ?:)
1746 (eq char-after-ip ?:))
1747 ;; this line should be indented relative to the beginning
1748 ;; of indentation for the topmost-intro line that contains
1749 ;; the prototype's open paren
1750 ;; TBD: is the following redundant?
1751 (if (eq char-before-ip ?:)
1752 (forward-char -1))
1753 (c-backward-syntactic-ws lim)
1754 ;; TBD: is the preceding redundant?
1755 (if (eq (char-before) ?:)
1756 (progn (forward-char -1)
1757 (c-backward-syntactic-ws lim)))
1758 (if (eq (char-before) ?\))
1759 (c-backward-sexp 1))
1760 (setq placeholder (point))
1761 (save-excursion
1762 (and (c-safe (c-backward-sexp 1) t)
1763 (looking-at "throw[^_]")
1764 (c-safe (c-backward-sexp 1) t)
1765 (setq placeholder (point))))
1766 (goto-char placeholder)
1767 (c-add-syntax 'member-init-intro (c-point 'boi))
1768 ;; we don't need to add any class offset since this
1769 ;; should be relative to the ctor's indentation
1770 )
1771 ;; CASE 5B.2: K&R arg decl intro
1772 (c-recognize-knr-p
1773 (c-add-syntax 'knr-argdecl-intro (c-point 'boi))
1774 (if inclass-p (c-add-class-syntax 'inclass inclass-p)))
1775 ;; CASE 5B.3: Inside a member init list.
1776 ((c-beginning-of-member-init-list lim)
1777 (c-forward-syntactic-ws)
1778 (c-add-syntax 'member-init-cont (point)))
1779 ;; CASE 5B.4: Nether region after a C++ or Java func
1780 ;; decl, which could include a `throws' declaration.
1781 (t
1782 (c-beginning-of-statement-1 lim)
1783 (c-add-syntax 'func-decl-cont (c-point 'boi))
1784 )))
1785 ;; CASE 5C: inheritance line. could be first inheritance
1786 ;; line, or continuation of a multiple inheritance
1787 ((or (and c-baseclass-key
1788 (progn
1789 (when (eq char-after-ip ?,)
1790 (skip-chars-forward " \t")
1791 (forward-char))
1792 (looking-at c-baseclass-key)))
1793 (and (or (eq char-before-ip ?:)
1794 ;; watch out for scope operator
1795 (save-excursion
1796 (and (eq char-after-ip ?:)
1797 (c-safe (progn (forward-char 1) t))
1798 (not (eq (char-after) ?:))
1799 )))
1800 (save-excursion
1801 (c-backward-syntactic-ws lim)
1802 (if (eq char-before-ip ?:)
1803 (progn
1804 (forward-char -1)
1805 (c-backward-syntactic-ws lim)))
1806 (back-to-indentation)
1807 (looking-at c-class-key)))
1808 ;; for Java
1809 (and (c-major-mode-is 'java-mode)
1810 (let ((fence (save-excursion
1811 (c-beginning-of-statement-1 lim)
1812 (point)))
1813 cont done)
1814 (save-excursion
1815 (while (not done)
1816 (cond ((looking-at c-Java-special-key)
1817 (setq injava-inher (cons cont (point))
1818 done t))
1819 ((or (not (c-safe (c-forward-sexp -1) t))
1820 (<= (point) fence))
1821 (setq done t))
1822 )
1823 (setq cont t)))
1824 injava-inher)
1825 (not (c-crosses-statement-barrier-p (cdr injava-inher)
1826 (point)))
1827 ))
1828 (cond
1829 ;; CASE 5C.1: non-hanging colon on an inher intro
1830 ((eq char-after-ip ?:)
1831 (c-backward-syntactic-ws lim)
1832 (c-add-syntax 'inher-intro (c-point 'boi))
1833 ;; don't add inclass symbol since relative point already
1834 ;; contains any class offset
1835 )
1836 ;; CASE 5C.2: hanging colon on an inher intro
1837 ((eq char-before-ip ?:)
1838 (c-add-syntax 'inher-intro (c-point 'boi))
1839 (if inclass-p (c-add-class-syntax 'inclass inclass-p)))
1840 ;; CASE 5C.3: in a Java implements/extends
1841 (injava-inher
1842 (let ((where (cdr injava-inher))
1843 (cont (car injava-inher)))
1844 (goto-char where)
1845 (cond ((looking-at "throws[ \t\n]")
1846 (c-add-syntax 'func-decl-cont
1847 (progn (c-beginning-of-statement-1 lim)
1848 (c-point 'boi))))
1849 (cont (c-add-syntax 'inher-cont where))
1850 (t (c-add-syntax 'inher-intro
1851 (progn (goto-char (cdr injava-inher))
1852 (c-beginning-of-statement-1 lim)
1853 (point))))
1854 )))
1855 ;; CASE 5C.4: a continued inheritance line
1856 (t
1857 (c-beginning-of-inheritance-list lim)
1858 (c-add-syntax 'inher-cont (point))
1859 ;; don't add inclass symbol since relative point already
1860 ;; contains any class offset
1861 )))
1862 ;; CASE 5D: this could be a top-level compound statement, a
1863 ;; member init list continuation, or a template argument
1864 ;; list continuation.
1865 ((c-with-syntax-table (if (c-major-mode-is 'c++-mode)
1866 c++-template-syntax-table
1867 (syntax-table))
1868 (save-excursion
1869 (while (and (= (c-backward-token-1 1 t lim) 0)
1870 (not (looking-at "[;{<,]"))))
1871 (eq (char-after) ?,)))
1872 (goto-char indent-point)
1873 (c-beginning-of-member-init-list lim)
1874 (cond
1875 ;; CASE 5D.1: hanging member init colon, but watch out
1876 ;; for bogus matches on access specifiers inside classes.
1877 ((and (save-excursion
1878 (setq placeholder (point))
1879 (c-backward-token-1 1 t lim)
1880 (and (eq (char-after) ?:)
1881 (not (eq (char-before) ?:))))
1882 (save-excursion
1883 (goto-char placeholder)
1884 (back-to-indentation)
1885 (or
1886 (/= (car (save-excursion
1887 (parse-partial-sexp (point) placeholder)))
1888 0)
1889 (and
1890 (if c-access-key (not (looking-at c-access-key)) t)
1891 (not (looking-at c-class-key))
1892 (if c-bitfield-key (not (looking-at c-bitfield-key)) t))
1893 )))
1894 (goto-char placeholder)
1895 (c-forward-syntactic-ws)
1896 (c-add-syntax 'member-init-cont (point))
1897 ;; we do not need to add class offset since relative
1898 ;; point is the member init above us
1899 )
1900 ;; CASE 5D.2: non-hanging member init colon
1901 ((progn
1902 (c-forward-syntactic-ws indent-point)
1903 (eq (char-after) ?:))
1904 (skip-chars-forward " \t:")
1905 (c-add-syntax 'member-init-cont (point)))
1906 ;; CASE 5D.3: perhaps a multiple inheritance line?
1907 ((save-excursion
1908 (c-beginning-of-statement-1 lim)
1909 (setq placeholder (point))
1910 (looking-at c-inher-key))
1911 (goto-char placeholder)
1912 (c-add-syntax 'inher-cont (c-point 'boi)))
1913 ;; CASE 5D.4: perhaps a template list continuation?
1914 ((save-excursion
1915 (goto-char indent-point)
1916 (skip-chars-backward "^<" lim)
1917 ;; not sure if this is the right test, but it should
1918 ;; be fast and mostly accurate.
1919 (setq placeholder (point))
1920 (and (eq (char-before) ?<)
1921 (not (c-in-literal lim))))
1922 ;; we can probably indent it just like an arglist-cont
1923 (goto-char placeholder)
1924 (c-beginning-of-statement-1 lim)
1925 (c-add-syntax 'template-args-cont (c-point 'boi)))
1926 ;; CASE 5D.5: perhaps a top-level statement-cont
1927 (t
1928 (c-beginning-of-statement-1 lim)
1929 ;; skip over any access-specifiers
1930 (and inclass-p c-access-key
1931 (while (looking-at c-access-key)
1932 (forward-line 1)))
1933 ;; skip over comments, whitespace
1934 (c-forward-syntactic-ws indent-point)
1935 (c-add-syntax 'statement-cont (c-point 'boi)))
1936 ))
1937 ;; CASE 5E: we are looking at a access specifier
1938 ((and inclass-p
1939 c-access-key
1940 (looking-at c-access-key))
1941 (c-add-syntax 'access-label (c-point 'bonl))
1942 (c-add-class-syntax 'inclass inclass-p))
1943 ;; CASE 5F: extern-lang-close or namespace-close?
1944 ((and inenclosing-p
1945 (eq char-after-ip ?}))
1946 (setq tmpsymbol (if (eq inenclosing-p 'extern)
1947 'extern-lang-close
1948 'namespace-close))
1949 (c-add-syntax tmpsymbol (aref inclass-p 0)))
1950 ;; CASE 5G: we are looking at the brace which closes the
1951 ;; enclosing nested class decl
1952 ((and inclass-p
1953 (eq char-after-ip ?})
1954 (save-excursion
1955 (save-restriction
1956 (widen)
1957 (forward-char 1)
1958 (and (c-safe (progn (c-backward-sexp 1) t))
1959 (= (point) (aref inclass-p 1))
1960 ))))
1961 (c-add-class-syntax 'class-close inclass-p))
1962 ;; CASE 5H: we could be looking at subsequent knr-argdecls
1963 ((and c-recognize-knr-p
1964 ;; here we essentially use the hack that is used in
1965 ;; Emacs' c-mode.el to limit how far back we should
1966 ;; look. The assumption is made that argdecls are
1967 ;; indented at least one space and that function
1968 ;; headers are not indented.
1969 (let ((limit (save-excursion
1970 (re-search-backward "^[^ \^L\t\n#]" nil 'move)
1971 (point))))
1972 (save-excursion
1973 (c-backward-syntactic-ws limit)
1974 (setq placeholder (point))
1975 (while (and (memq (char-before) '(?\; ?,))
1976 (> (point) limit))
1977 (beginning-of-line)
1978 (setq placeholder (point))
1979 (c-backward-syntactic-ws limit))
1980 (and (eq (char-before) ?\))
1981 (or (not c-method-key)
1982 (progn
1983 (c-forward-sexp -1)
1984 (forward-char -1)
1985 (c-backward-syntactic-ws)
1986 (not (or (memq (char-before) '(?- ?+))
1987 ;; or a class category
1988 (progn
1989 (c-forward-sexp -2)
1990 (looking-at c-class-key))
1991 )))))
1992 ))
1993 (save-excursion
1994 (c-beginning-of-statement-1)
1995 (not (looking-at "typedef[ \t\n]+"))))
1996 (goto-char placeholder)
1997 (c-add-syntax 'knr-argdecl (c-point 'boi)))
1998 ;; CASE 5I: ObjC method definition.
1999 ((and c-method-key
2000 (looking-at c-method-key))
2001 (c-add-syntax 'objc-method-intro (c-point 'boi)))
2002 ;; CASE 5J: we are at the topmost level, make sure we skip
2003 ;; back past any access specifiers
2004 ((progn
2005 (c-backward-syntactic-ws lim)
2006 (while (and inclass-p
2007 c-access-key
2008 (not (bobp))
2009 (save-excursion
2010 (c-safe (progn (c-backward-sexp 1) t))
2011 (looking-at c-access-key)))
2012 (c-backward-sexp 1)
2013 (c-backward-syntactic-ws lim))
2014 (or (bobp)
2015 (memq (char-before) '(?\; ?\}))))
2016 ;; real beginning-of-line could be narrowed out due to
2017 ;; enclosure in a class block
2018 (save-restriction
2019 (widen)
2020 (c-add-syntax 'topmost-intro (c-point 'bol))
2021 (if inclass-p
2022 (progn
2023 (goto-char (aref inclass-p 1))
2024 (or (= (point) (c-point 'boi))
2025 (goto-char (aref inclass-p 0)))
2026 (cond
2027 ((eq inenclosing-p 'extern)
2028 (c-add-syntax 'inextern-lang (c-point 'boi)))
2029 ((eq inenclosing-p 'namespace)
2030 (c-add-syntax 'innamespace (c-point 'boi)))
2031 (t (c-add-class-syntax 'inclass inclass-p)))
2032 ))
2033 ))
2034 ;; CASE 5K: we are at an ObjC or Java method definition
2035 ;; continuation line.
2036 ((and c-method-key
2037 (progn
2038 (c-beginning-of-statement-1 lim)
2039 (beginning-of-line)
2040 (looking-at c-method-key)))
2041 (c-add-syntax 'objc-method-args-cont (point)))
2042 ;; CASE 5L: we are at the first argument of a template
2043 ;; arglist that begins on the previous line.
2044 ((eq (char-before) ?<)
2045 (c-beginning-of-statement-1 lim)
2046 (c-forward-syntactic-ws)
2047 (c-add-syntax 'template-args-cont (c-point 'boi)))
2048 ;; CASE 5M: we are at a topmost continuation line
2049 (t
2050 (c-beginning-of-statement-1 lim)
2051 (c-forward-syntactic-ws)
2052 (c-add-syntax 'topmost-intro-cont (c-point 'boi)))
2053 )) ; end CASE 5
2054 ;; (CASE 6 has been removed.)
2055 ;; CASE 7: line is an expression, not a statement. Most
2056 ;; likely we are either in a function prototype or a function
2057 ;; call argument list
2058 ((not (or (and c-special-brace-lists
2059 (save-excursion
2060 (goto-char containing-sexp)
2061 (c-looking-at-special-brace-list)))
2062 (eq (char-after containing-sexp) ?{)))
2063 (c-backward-syntactic-ws containing-sexp)
2064 (cond
2065 ;; CASE 7A: we are looking at the arglist closing paren
2066 ((and (or (c-major-mode-is 'pike-mode)
2067 ;; Don't check this in Pike since it allows a
2068 ;; comma after the last arg.
2069 (not (eq char-before-ip ?,)))
2070 (memq char-after-ip '(?\) ?\])))
2071 (goto-char containing-sexp)
2072 (setq placeholder (c-point 'boi))
2073 (when (and (c-safe (backward-up-list 1) t)
2074 (> (point) placeholder))
2075 (forward-char)
2076 (skip-chars-forward " \t")
2077 (setq placeholder (point)))
2078 (c-add-syntax 'arglist-close placeholder))
2079 ;; CASE 7B: Looking at the opening brace of an
2080 ;; in-expression block or brace list.
2081 ((eq char-after-ip ?{)
2082 (goto-char indent-point)
2083 (setq placeholder (c-point 'boi))
2084 (goto-char containing-sexp)
2085 (if (c-inside-bracelist-p placeholder
2086 (cons containing-sexp state))
2087 (progn
2088 (c-add-syntax 'brace-list-open (c-point 'boi))
2089 (c-add-syntax 'inexpr-class))
2090 (c-add-syntax 'block-open (c-point 'boi))
2091 (c-add-syntax 'inexpr-statement)))
2092 ;; CASE 7C: we are looking at the first argument in an empty
2093 ;; argument list. Use arglist-close if we're actually
2094 ;; looking at a close paren or bracket.
2095 ((memq char-before-ip '(?\( ?\[))
2096 (goto-char containing-sexp)
2097 (setq placeholder (c-point 'boi))
2098 (when (and (c-safe (backward-up-list 1) t)
2099 (> (point) placeholder))
2100 (forward-char)
2101 (skip-chars-forward " \t")
2102 (setq placeholder (point)))
2103 (c-add-syntax 'arglist-intro placeholder))
2104 ;; CASE 7D: we are inside a conditional test clause. treat
2105 ;; these things as statements
2106 ((save-excursion
2107 (goto-char containing-sexp)
2108 (and (c-safe (progn (c-forward-sexp -1) t))
2109 (looking-at "\\<for\\>[^_]")))
2110 (goto-char (1+ containing-sexp))
2111 (c-forward-syntactic-ws indent-point)
2112 (c-beginning-of-statement-1 containing-sexp)
2113 (if (eq char-before-ip ?\;)
2114 (c-add-syntax 'statement (point))
2115 (c-add-syntax 'statement-cont (point))
2116 ))
2117 ;; CASE 7E: maybe a continued method call. This is the case
2118 ;; when we are inside a [] bracketed exp, and what precede
2119 ;; the opening bracket is not an identifier.
2120 ((and c-method-key
2121 (eq (char-after containing-sexp) ?\[)
2122 (save-excursion
2123 (goto-char (1- containing-sexp))
2124 (c-backward-syntactic-ws (c-point 'bod))
2125 (if (not (looking-at c-symbol-key))
2126 (c-add-syntax 'objc-method-call-cont containing-sexp))
2127 )))
2128 ;; CASE 7F: we are looking at an arglist continuation line,
2129 ;; but the preceding argument is on the same line as the
2130 ;; opening paren. This case includes multi-line
2131 ;; mathematical paren groupings, but we could be on a
2132 ;; for-list continuation line
2133 ((save-excursion
2134 (goto-char (1+ containing-sexp))
2135 (skip-chars-forward " \t")
2136 (not (eolp)))
2137 (goto-char containing-sexp)
2138 (setq placeholder (c-point 'boi))
2139 (when (and (c-safe (backward-up-list 1) t)
2140 (> (point) placeholder))
2141 (forward-char)
2142 (skip-chars-forward " \t")
2143 (setq placeholder (point)))
2144 (c-add-syntax 'arglist-cont-nonempty placeholder))
2145 ;; CASE 7G: we are looking at just a normal arglist
2146 ;; continuation line
2147 (t (c-beginning-of-statement-1 containing-sexp)
2148 (forward-char 1)
2149 (c-forward-syntactic-ws indent-point)
2150 (c-add-syntax 'arglist-cont (c-point 'boi)))
2151 ))
2152 ;; CASE 8: func-local multi-inheritance line
2153 ((and c-baseclass-key
2154 (save-excursion
2155 (goto-char indent-point)
2156 (skip-chars-forward " \t")
2157 (looking-at c-baseclass-key)))
2158 (goto-char indent-point)
2159 (skip-chars-forward " \t")
2160 (cond
2161 ;; CASE 8A: non-hanging colon on an inher intro
2162 ((eq char-after-ip ?:)
2163 (c-backward-syntactic-ws lim)
2164 (c-add-syntax 'inher-intro (c-point 'boi)))
2165 ;; CASE 8B: hanging colon on an inher intro
2166 ((eq char-before-ip ?:)
2167 (c-add-syntax 'inher-intro (c-point 'boi)))
2168 ;; CASE 8C: a continued inheritance line
2169 (t
2170 (c-beginning-of-inheritance-list lim)
2171 (c-add-syntax 'inher-cont (point))
2172 )))
2173 ;; CASE 9: we are inside a brace-list
2174 ((setq special-brace-list
2175 (or (and c-special-brace-lists
2176 (save-excursion
2177 (goto-char containing-sexp)
2178 (c-looking-at-special-brace-list)))
2179 (c-inside-bracelist-p containing-sexp state)))
2180 (cond
2181 ;; CASE 9A: In the middle of a special brace list opener.
2182 ((and (consp special-brace-list)
2183 (save-excursion
2184 (goto-char containing-sexp)
2185 (eq (char-after) ?\())
2186 (eq char-after-ip (car (cdr special-brace-list))))
2187 (goto-char (car (car special-brace-list)))
2188 (skip-chars-backward " \t")
2189 (if (and (bolp)
2190 (assoc 'statement-cont
2191 (setq placeholder (c-guess-basic-syntax))))
2192 (setq syntax placeholder)
2193 (c-beginning-of-statement-1 lim)
2194 (c-forward-token-1 0)
2195 (if (looking-at "typedef\\>") (c-forward-token-1 1))
2196 (c-add-syntax 'brace-list-open (c-point 'boi))))
2197 ;; CASE 9B: brace-list-close brace
2198 ((if (consp special-brace-list)
2199 ;; Check special brace list closer.
2200 (progn
2201 (goto-char (car (car special-brace-list)))
2202 (save-excursion
2203 (goto-char indent-point)
2204 (back-to-indentation)
2205 (or
2206 ;; We were between the special close char and the `)'.
2207 (and (eq (char-after) ?\))
2208 (eq (1+ (point)) (cdr (car special-brace-list))))
2209 ;; We were before the special close char.
2210 (and (eq (char-after) (cdr (cdr special-brace-list)))
2211 (= (c-forward-token-1) 0)
2212 (eq (1+ (point)) (cdr (car special-brace-list)))))))
2213 ;; Normal brace list check.
2214 (and (eq char-after-ip ?})
2215 (c-safe (progn (forward-char 1)
2216 (c-backward-sexp 1)
2217 t))
2218 (= (point) containing-sexp)))
2219 (c-add-syntax 'brace-list-close (c-point 'boi)))
2220 (t
2221 ;; Prepare for the rest of the cases below by going to the
2222 ;; token following the opening brace
2223 (if (consp special-brace-list)
2224 (progn
2225 (goto-char (car (car special-brace-list)))
2226 (c-forward-token-1 1 nil indent-point))
2227 (goto-char containing-sexp))
2228 (forward-char)
2229 (let ((start (point)))
2230 (c-forward-syntactic-ws indent-point)
2231 (goto-char (max start (c-point 'bol))))
2232 (skip-chars-forward " \t\n\r" indent-point)
2233 (cond
2234 ;; CASE 9C: we're looking at the first line in a brace-list
2235 ((= (point) indent-point)
2236 (goto-char containing-sexp)
2237 (c-add-syntax 'brace-list-intro (c-point 'boi))
2238 ) ; end CASE 9C
2239 ;; CASE 9D: this is just a later brace-list-entry or
2240 ;; brace-entry-open
2241 (t (if (or (eq char-after-ip ?{)
2242 (and c-special-brace-lists
2243 (save-excursion
2244 (goto-char indent-point)
2245 (c-forward-syntactic-ws (c-point 'eol))
2246 (c-looking-at-special-brace-list (point)))))
2247 (c-add-syntax 'brace-entry-open (point))
2248 (c-add-syntax 'brace-list-entry (point))
2249 )) ; end CASE 9D
2250 )))) ; end CASE 9
2251 ;; CASE 10: A continued statement
2252 ((and (not (memq char-before-ip '(?\; ?:)))
2253 (or (not (eq char-before-ip ?}))
2254 (c-looking-at-inexpr-block-backward containing-sexp))
2255 (> (point)
2256 (save-excursion
2257 (c-beginning-of-statement-1 containing-sexp)
2258 (c-forward-syntactic-ws)
2259 (setq placeholder (point))))
2260 (/= placeholder containing-sexp))
2261 (goto-char indent-point)
2262 (skip-chars-forward " \t")
2263 (let ((after-cond-placeholder
2264 (save-excursion
2265 (goto-char placeholder)
2266 (if (and c-conditional-key (looking-at c-conditional-key))
2267 (progn
2268 (c-safe (c-skip-conditional))
2269 (c-forward-syntactic-ws)
2270 (if (eq (char-after) ?\;)
2271 (progn
2272 (forward-char 1)
2273 (c-forward-syntactic-ws)))
2274 (point))
2275 nil))))
2276 (cond
2277 ;; CASE 10A: substatement
2278 ((and after-cond-placeholder
2279 (>= after-cond-placeholder indent-point))
2280 (goto-char placeholder)
2281 (if (eq char-after-ip ?{)
2282 (c-add-syntax 'substatement-open (c-point 'boi))
2283 (c-add-syntax 'substatement (c-point 'boi))))
2284 ;; CASE 10B: open braces for class or brace-lists
2285 ((setq special-brace-list
2286 (or (and c-special-brace-lists
2287 (c-looking-at-special-brace-list))
2288 (eq char-after-ip ?{)))
2289 (cond
2290 ;; CASE 10B.1: class-open
2291 ((save-excursion
2292 (goto-char indent-point)
2293 (skip-chars-forward " \t{")
2294 (let ((decl (c-search-uplist-for-classkey (c-parse-state))))
2295 (and decl
2296 (setq placeholder (aref decl 0)))
2297 ))
2298 (c-add-syntax 'class-open placeholder))
2299 ;; CASE 10B.2: brace-list-open
2300 ((or (consp special-brace-list)
2301 (save-excursion
2302 (goto-char placeholder)
2303 (looking-at "\\<enum\\>"))
2304 (save-excursion
2305 (goto-char indent-point)
2306 (while (and (> (point) placeholder)
2307 (= (c-backward-token-1 1 t) 0)
2308 (/= (char-after) ?=)))
2309 (eq (char-after) ?=)))
2310 ;; The most semantically accurate symbol here is
2311 ;; brace-list-open, but we report it simply as a
2312 ;; statement-cont. The reason is that one normally
2313 ;; adjusts brace-list-open for brace lists as
2314 ;; top-level constructs, and brace lists inside
2315 ;; statements is a completely different context.
2316 (goto-char indent-point)
2317 (c-beginning-of-closest-statement)
2318 (c-add-syntax 'statement-cont (c-point 'boi)))
2319 ;; CASE 10B.3: The body of a function declared inside a
2320 ;; normal block. This can only occur in Pike.
2321 ((and (c-major-mode-is 'pike-mode)
2322 (progn
2323 (goto-char indent-point)
2324 (not (c-looking-at-bos))))
2325 (c-beginning-of-closest-statement)
2326 (c-add-syntax 'defun-open (c-point 'boi)))
2327 ;; CASE 10B.4: catch-all for unknown construct.
2328 (t
2329 ;; Can and should I add an extensibility hook here?
2330 ;; Something like c-recognize-hook so support for
2331 ;; unknown constructs could be added. It's probably a
2332 ;; losing proposition, so I dunno.
2333 (goto-char placeholder)
2334 (c-add-syntax 'statement-cont (c-point 'boi))
2335 (c-add-syntax 'block-open))
2336 ))
2337 ;; CASE 10C: iostream insertion or extraction operator
2338 ((looking-at "<<\\|>>")
2339 (goto-char placeholder)
2340 (and after-cond-placeholder
2341 (goto-char after-cond-placeholder))
2342 (while (and (re-search-forward "<<\\|>>" indent-point 'move)
2343 (c-in-literal placeholder)))
2344 ;; if we ended up at indent-point, then the first
2345 ;; streamop is on a separate line. Indent the line like
2346 ;; a statement-cont instead
2347 (if (/= (point) indent-point)
2348 (c-add-syntax 'stream-op (c-point 'boi))
2349 (c-backward-syntactic-ws lim)
2350 (c-add-syntax 'statement-cont (c-point 'boi))))
2351 ;; CASE 10D: continued statement. find the accurate
2352 ;; beginning of statement or substatement
2353 (t
2354 (c-beginning-of-statement-1 after-cond-placeholder)
2355 ;; KLUDGE ALERT! c-beginning-of-statement-1 can leave
2356 ;; us before the lim we're passing in. It should be
2357 ;; fixed, but I'm worried about side-effects at this
2358 ;; late date. Fix for v5.
2359 (goto-char (or (and after-cond-placeholder
2360 (max after-cond-placeholder (point)))
2361 (point)))
2362 (c-add-syntax 'statement-cont (point)))
2363 )))
2364 ;; CASE 11: an else clause?
2365 ((looking-at "\\<else\\>[^_]")
2366 (c-backward-to-start-of-if containing-sexp)
2367 (c-add-syntax 'else-clause (c-point 'boi)))
2368 ;; CASE 12: Statement. But what kind? Lets see if its a
2369 ;; while closure of a do/while construct
2370 ((progn
2371 (goto-char indent-point)
2372 (skip-chars-forward " \t")
2373 (and (looking-at "while\\b[^_]")
2374 (save-excursion
2375 (c-backward-to-start-of-do containing-sexp)
2376 (setq placeholder (point))
2377 (looking-at "do\\b[^_]"))
2378 ))
2379 (goto-char placeholder)
2380 (c-add-syntax 'do-while-closure (c-point 'boi)))
2381 ;; CASE 13: A catch or finally clause? This case is simpler
2382 ;; than if-else and do-while, because a block is required
2383 ;; after every try, catch and finally.
2384 ((save-excursion
2385 (and (cond ((c-major-mode-is 'c++-mode)
2386 (looking-at "\\<catch\\>[^_]"))
2387 ((c-major-mode-is 'java-mode)
2388 (looking-at "\\<\\(catch\\|finally\\)\\>[^_]")))
2389 (c-safe (c-backward-sexp) t)
2390 (eq (char-after) ?{)
2391 (c-safe (c-backward-sexp) t)
2392 (if (eq (char-after) ?\()
2393 (c-safe (c-backward-sexp) t)
2394 t)
2395 (looking-at "\\<\\(try\\|catch\\)\\>[^_]")
2396 (setq placeholder (c-point 'boi))))
2397 (c-add-syntax 'catch-clause placeholder))
2398 ;; CASE 14: A case or default label
2399 ((looking-at c-switch-label-key)
2400 (goto-char containing-sexp)
2401 ;; check for hanging braces
2402 (if (/= (point) (c-point 'boi))
2403 (c-forward-sexp -1))
2404 (c-add-syntax 'case-label (c-point 'boi)))
2405 ;; CASE 15: any other label
2406 ((looking-at c-label-key)
2407 (goto-char containing-sexp)
2408 ;; check for hanging braces
2409 (if (/= (point) (c-point 'boi))
2410 (c-forward-sexp -1))
2411 (c-add-syntax 'label (c-point 'boi)))
2412 ;; CASE 16: block close brace, possibly closing the defun or
2413 ;; the class
2414 ((eq char-after-ip ?})
2415 (let* ((lim (c-safe-position containing-sexp fullstate))
2416 (relpos (save-excursion
2417 (goto-char containing-sexp)
2418 (if (/= (point) (c-point 'boi))
2419 (c-beginning-of-statement-1 lim))
2420 (c-point 'boi))))
2421 (cond
2422 ;; CASE 16A: closing a lambda defun or an in-expression
2423 ;; block?
2424 ((save-excursion
2425 (goto-char containing-sexp)
2426 (setq placeholder (c-looking-at-inexpr-block)))
2427 (setq tmpsymbol (if (eq (car placeholder) 'inlambda)
2428 'inline-close
2429 'block-close))
2430 (goto-char containing-sexp)
2431 (back-to-indentation)
2432 (if (= containing-sexp (point))
2433 (c-add-syntax tmpsymbol (point))
2434 (goto-char (cdr placeholder))
2435 (back-to-indentation)
2436 (c-add-syntax tmpsymbol (point))
2437 (if (/= (point) (cdr placeholder))
2438 (c-add-syntax (car placeholder)))))
2439 ;; CASE 16B: does this close an inline or a function in
2440 ;; an extern block or namespace?
2441 ((progn
2442 (goto-char containing-sexp)
2443 (setq placeholder (c-search-uplist-for-classkey state)))
2444 (goto-char (aref placeholder 0))
2445 (if (looking-at (concat c-extra-toplevel-key "[^_]"))
2446 (c-add-syntax 'defun-close relpos)
2447 (c-add-syntax 'inline-close relpos)))
2448 ;; CASE 16C: if there an enclosing brace that hasn't
2449 ;; been narrowed out by a class, then this is a
2450 ;; block-close
2451 ((and (not inenclosing-p)
2452 (c-most-enclosing-brace state)
2453 (or (not (c-major-mode-is 'pike-mode))
2454 ;; In Pike it can be a defun-close of a
2455 ;; function declared in a statement block. Let
2456 ;; it through to be handled below.
2457 (or (c-looking-at-bos)
2458 (progn
2459 (c-beginning-of-statement-1)
2460 (looking-at c-conditional-key)))))
2461 (c-add-syntax 'block-close relpos))
2462 ;; CASE 16D: find out whether we're closing a top-level
2463 ;; class or a defun
2464 (t
2465 (save-restriction
2466 (narrow-to-region (point-min) indent-point)
2467 (let ((decl (c-search-uplist-for-classkey (c-parse-state))))
2468 (if decl
2469 (c-add-class-syntax 'class-close decl)
2470 (c-add-syntax 'defun-close relpos)))))
2471 )))
2472 ;; CASE 17: statement catchall
2473 (t
2474 ;; we know its a statement, but we need to find out if it is
2475 ;; the first statement in a block
2476 (goto-char containing-sexp)
2477 (forward-char 1)
2478 (c-forward-syntactic-ws indent-point)
2479 ;; now skip forward past any case/default clauses we might find.
2480 (while (or (c-skip-case-statement-forward fullstate indent-point)
2481 (and (looking-at c-switch-label-key)
2482 (not inswitch-p)))
2483 (setq inswitch-p t))
2484 ;; we want to ignore non-case labels when skipping forward
2485 (while (and (looking-at c-label-key)
2486 (goto-char (match-end 0)))
2487 (c-forward-syntactic-ws indent-point))
2488 (cond
2489 ;; CASE 17A: we are inside a case/default clause inside a
2490 ;; switch statement. find out if we are at the statement
2491 ;; just after the case/default label.
2492 ((and inswitch-p
2493 (progn
2494 (goto-char indent-point)
2495 (c-beginning-of-statement-1 containing-sexp)
2496 (setq placeholder (point))
2497 (beginning-of-line)
2498 (when (re-search-forward c-switch-label-key
2499 (max placeholder (c-point 'eol)) t)
2500 (setq placeholder (match-beginning 0)))))
2501 (goto-char indent-point)
2502 (skip-chars-forward " \t")
2503 (if (eq (char-after) ?{)
2504 (c-add-syntax 'statement-case-open placeholder)
2505 (c-add-syntax 'statement-case-intro placeholder)))
2506 ;; CASE 17B: continued statement
2507 ((eq char-before-ip ?,)
2508 (goto-char indent-point)
2509 (c-beginning-of-closest-statement)
2510 (c-add-syntax 'statement-cont (c-point 'boi)))
2511 ;; CASE 17C: a question/colon construct? But make sure
2512 ;; what came before was not a label, and what comes after
2513 ;; is not a globally scoped function call!
2514 ((or (and (memq char-before-ip '(?: ??))
2515 (save-excursion
2516 (goto-char indent-point)
2517 (c-backward-syntactic-ws lim)
2518 (back-to-indentation)
2519 (not (looking-at c-label-key))))
2520 (and (memq char-after-ip '(?: ??))
2521 (save-excursion
2522 (goto-char indent-point)
2523 (skip-chars-forward " \t")
2524 ;; watch out for scope operator
2525 (not (looking-at "::")))))
2526 (goto-char indent-point)
2527 (c-beginning-of-closest-statement)
2528 (c-add-syntax 'statement-cont (c-point 'boi)))
2529 ;; CASE 17D: any old statement
2530 ((< (point) indent-point)
2531 (let ((safepos (c-most-enclosing-brace fullstate))
2532 relpos done)
2533 (goto-char indent-point)
2534 (c-beginning-of-statement-1 safepos)
2535 ;; It is possible we're on the brace that opens a nested
2536 ;; function.
2537 (if (and (eq (char-after) ?{)
2538 (save-excursion
2539 (c-backward-syntactic-ws safepos)
2540 (not (eq (char-before) ?\;))))
2541 (c-beginning-of-statement-1 safepos))
2542 (if (and inswitch-p
2543 (looking-at c-switch-label-key))
2544 (progn
2545 (goto-char (match-end 0))
2546 (c-forward-syntactic-ws)))
2547 (setq relpos (c-point 'boi))
2548 (while (and (not done)
2549 (<= safepos (point))
2550 (/= relpos (point)))
2551 (c-beginning-of-statement-1 safepos)
2552 (if (= relpos (c-point 'boi))
2553 (setq done t))
2554 (setq relpos (c-point 'boi)))
2555 (c-add-syntax 'statement relpos)
2556 (if (eq char-after-ip ?{)
2557 (c-add-syntax 'block-open))))
2558 ;; CASE 17E: first statement in an in-expression block
2559 ((setq placeholder
2560 (save-excursion
2561 (goto-char containing-sexp)
2562 (c-looking-at-inexpr-block)))
2563 (goto-char containing-sexp)
2564 (back-to-indentation)
2565 (let ((block-intro (if (eq (car placeholder) 'inlambda)
2566 'defun-block-intro
2567 'statement-block-intro)))
2568 (if (= containing-sexp (point))
2569 (c-add-syntax block-intro (point))
2570 (goto-char (cdr placeholder))
2571 (back-to-indentation)
2572 (c-add-syntax block-intro (point))
2573 (if (/= (point) (cdr placeholder))
2574 (c-add-syntax (car placeholder)))))
2575 (if (eq char-after-ip ?{)
2576 (c-add-syntax 'block-open)))
2577 ;; CASE 17F: first statement in an inline, or first
2578 ;; statement in a top-level defun. we can tell this is it
2579 ;; if there are no enclosing braces that haven't been
2580 ;; narrowed out by a class (i.e. don't use bod here!)
2581 ((save-excursion
2582 (save-restriction
2583 (widen)
2584 (goto-char containing-sexp)
2585 (c-narrow-out-enclosing-class state containing-sexp)
2586 (not (c-most-enclosing-brace state))))
2587 (goto-char containing-sexp)
2588 ;; if not at boi, then defun-opening braces are hung on
2589 ;; right side, so we need a different relpos
2590 (if (/= (point) (c-point 'boi))
2591 (progn
2592 (c-backward-syntactic-ws)
2593 (c-safe (c-forward-sexp (if (eq (char-before) ?\))
2594 -1 -2)))
2595 ;; looking at a Java throws clause following a
2596 ;; method's parameter list
2597 (c-beginning-of-statement-1)
2598 ))
2599 (c-add-syntax 'defun-block-intro (c-point 'boi)))
2600 ;; CASE 17G: First statement in a function declared inside
2601 ;; a normal block. This can only occur in Pike.
2602 ((and (c-major-mode-is 'pike-mode)
2603 (progn
2604 (goto-char containing-sexp)
2605 (and (not (c-looking-at-bos))
2606 (progn
2607 (c-beginning-of-statement-1)
2608 (not (looking-at c-conditional-key))))))
2609 (c-add-syntax 'defun-block-intro (c-point 'boi)))
2610 ;; CASE 17H: first statement in a block
2611 (t (goto-char containing-sexp)
2612 (if (/= (point) (c-point 'boi))
2613 (c-beginning-of-statement-1
2614 (if (= (point) lim)
2615 (c-safe-position (point) state) lim)))
2616 (c-add-syntax 'statement-block-intro (c-point 'boi))
2617 (if (eq char-after-ip ?{)
2618 (c-add-syntax 'block-open)))
2619 ))
2620 )
2621 ;; now we need to look at any modifiers
2622 (goto-char indent-point)
2623 (skip-chars-forward " \t")
2624 (cond
2625 ;; are we looking at a comment only line?
2626 ((and (looking-at c-comment-start-regexp)
2627 (/= (c-forward-token-1 0 nil (c-point 'eol)) 0))
2628 (c-add-syntax 'comment-intro))
2629 ;; we might want to give additional offset to friends (in C++).
2630 ((and (c-major-mode-is 'c++-mode)
2631 (looking-at c-C++-friend-key))
2632 (c-add-syntax 'friend))
2633 ;; Start of a preprocessor directive?
2634 ((and (eq literal 'pound)
2635 (= (save-excursion
2636 (c-beginning-of-macro lim)
2637 (setq placeholder (point)))
2638 (c-point 'boi))
2639 (not (and (c-major-mode-is 'pike-mode)
2640 (eq (char-after (1+ placeholder)) ?\"))))
2641 (c-add-syntax 'cpp-macro)))
2642 ;; return the syntax
2643 syntax))))
2644
2645 \f
2646 (defun c-echo-parsing-error (&optional quiet)
2647 (when (and c-parsing-error (not quiet))
2648 (message "%s" c-parsing-error)
2649 (ding))
2650 c-parsing-error)
2651
2652 (defun c-shift-line-indentation (shift-amt)
2653 (let ((pos (- (point-max) (point)))
2654 (col (current-indentation)))
2655 (if (zerop shift-amt)
2656 nil
2657 (delete-region (c-point 'bol) (c-point 'boi))
2658 (beginning-of-line)
2659 (indent-to (+ col shift-amt)))
2660 (if (< (point) (c-point 'boi))
2661 (back-to-indentation)
2662 ;; If initial point was within line's indentation, position after
2663 ;; the indentation. Else stay at same point in text.
2664 (if (> (- (point-max) pos) (point))
2665 (goto-char (- (point-max) pos))))))
2666
2667 (defun c-evaluate-offset (offset langelem symbol)
2668 ;; offset can be a number, a function, a variable, a list, or one of
2669 ;; the symbols + or -
2670 (cond
2671 ((eq offset '+) c-basic-offset)
2672 ((eq offset '-) (- c-basic-offset))
2673 ((eq offset '++) (* 2 c-basic-offset))
2674 ((eq offset '--) (* 2 (- c-basic-offset)))
2675 ((eq offset '*) (/ c-basic-offset 2))
2676 ((eq offset '/) (/ (- c-basic-offset) 2))
2677 ((numberp offset) offset)
2678 ((functionp offset) (c-evaluate-offset
2679 (funcall offset langelem) langelem symbol))
2680 ((vectorp offset) offset)
2681 ((null offset) nil)
2682 ((listp offset)
2683 (let (done)
2684 (while (and (not done) offset)
2685 (setq done (c-evaluate-offset (car offset) langelem symbol)
2686 offset (cdr offset)))
2687 (if (not done)
2688 (if c-strict-syntax-p
2689 (error "No offset found for syntactic symbol %s" symbol))
2690 done)))
2691 (t (symbol-value offset))
2692 ))
2693
2694 (defun c-get-offset (langelem)
2695 ;; Get offset from LANGELEM which is a cons cell of the form:
2696 ;; (SYMBOL . RELPOS). The symbol is matched against
2697 ;; c-offsets-alist and the offset found there is either returned,
2698 ;; or added to the indentation at RELPOS. If RELPOS is nil, then
2699 ;; the offset is simply returned.
2700 (let* ((symbol (car langelem))
2701 (relpos (cdr langelem))
2702 (match (assq symbol c-offsets-alist))
2703 (offset (cdr-safe match)))
2704 (if (not match)
2705 (if c-strict-syntax-p
2706 (error "No offset found for syntactic symbol %s" symbol)
2707 (setq offset 0
2708 relpos 0))
2709 (setq offset (c-evaluate-offset offset langelem symbol)))
2710 (if (vectorp offset)
2711 offset
2712 (+ (if (and relpos
2713 (< relpos (c-point 'bol)))
2714 (save-excursion
2715 (goto-char relpos)
2716 (current-column))
2717 0)
2718 (or (and (numberp offset) offset)
2719 (and (symbolp offset) (symbol-value offset))
2720 0)))
2721 ))
2722
2723 (defun c-get-syntactic-indentation (langelems)
2724 ;; Apply c-get-offset to a list of langelem cells to get the total
2725 ;; syntactic indentation. Special treatment is needed for vectors
2726 ;; containing absolute columns.
2727 (let ((indent 0))
2728 (catch 'done
2729 (while langelems
2730 (let ((res (c-get-offset (car langelems))))
2731 (if (vectorp res)
2732 (throw 'done (elt res 0))
2733 (setq indent (+ indent res)
2734 langelems (cdr langelems)))))
2735 indent)))
2736
2737 (defun c-indent-line (&optional syntax quiet)
2738 ;; Indent the current line according to the syntactic context, if
2739 ;; c-syntactic-indentation is non-nil. Optional SYNTAX is the
2740 ;; syntactic information for the current line. Be silent about
2741 ;; syntactic errors if the optional argument QUIET is non-nil.
2742 ;; Returns the amount of indentation change (in columns).
2743 (let (shift-amt)
2744 (if c-syntactic-indentation
2745 (setq c-parsing-error
2746 (or (let* ((c-parsing-error nil)
2747 (c-syntactic-context (or syntax
2748 c-syntactic-context
2749 (c-guess-basic-syntax)))
2750 (indent (c-get-syntactic-indentation c-syntactic-context)))
2751 (and (not (c-echo-parsing-error quiet))
2752 c-echo-syntactic-information-p
2753 (message "syntax: %s, indent: %d"
2754 c-syntactic-context indent))
2755 (setq shift-amt (- indent (current-indentation)))
2756 (c-shift-line-indentation shift-amt)
2757 (run-hooks 'c-special-indent-hook)
2758 c-parsing-error)
2759 c-parsing-error))
2760 (let ((indent 0))
2761 (save-excursion
2762 (while (and (= (forward-line -1) 0)
2763 (if (looking-at "\\s-*$")
2764 t
2765 (back-to-indentation)
2766 (setq indent (current-indentation))
2767 nil))))
2768 (setq shift-amt (- indent (current-indentation)))
2769 (c-shift-line-indentation shift-amt)))
2770 shift-amt))
2771
2772 (defun c-show-syntactic-information (arg)
2773 "Show syntactic information for current line.
2774 With universal argument, inserts the analysis as a comment on that line."
2775 (interactive "P")
2776 (let ((syntax (c-guess-basic-syntax)))
2777 (if (not (consp arg))
2778 (message "syntactic analysis: %s" syntax)
2779 (indent-for-comment)
2780 (insert (format "%s" syntax))
2781 ))
2782 (c-keep-region-active))
2783
2784 (defun c-syntactic-information-on-region (from to)
2785 "Inserts a comment with the syntactic analysis on every line in the region."
2786 (interactive "*r")
2787 (save-excursion
2788 (save-restriction
2789 (narrow-to-region from to)
2790 (goto-char (point-min))
2791 (while (not (eobp))
2792 (c-show-syntactic-information '(0))
2793 (forward-line)))))
2794
2795 \f
2796 (cc-provide 'cc-engine)
2797
2798 ;;; cc-engine.el ends here