]> code.delx.au - gnu-emacs/blob - lisp/progmodes/cc-engine.el
(sh-mark-line): Add help-echo to mouse-highlighted text.
[gnu-emacs] / lisp / progmodes / cc-engine.el
1 ;;; cc-engine.el --- core syntax guessing engine for CC mode
2
3 ;; Copyright (C) 1985,1987,1992-2001 Free Software Foundation, Inc.
4
5 ;; Authors: 2000- Martin Stjernholm
6 ;; 1998-1999 Barry A. Warsaw and Martin Stjernholm
7 ;; 1992-1997 Barry A. Warsaw
8 ;; 1987 Dave Detlefs and Stewart Clamen
9 ;; 1985 Richard M. Stallman
10 ;; Maintainer: bug-cc-mode@gnu.org
11 ;; Created: 22-Apr-1997 (split from cc-mode.el)
12 ;; Version: See cc-mode.el
13 ;; Keywords: c languages oop
14
15 ;; This file is part of GNU Emacs.
16
17 ;; GNU Emacs is free software; you can redistribute it and/or modify
18 ;; it under the terms of the GNU General Public License as published by
19 ;; the Free Software Foundation; either version 2, or (at your option)
20 ;; any later version.
21
22 ;; GNU Emacs is distributed in the hope that it will be useful,
23 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
24 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 ;; GNU General Public License for more details.
26
27 ;; You should have received a copy of the GNU General Public License
28 ;; along with this program; see the file COPYING. If not, write to
29 ;; the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
30 ;; Boston, MA 02111-1307, USA.
31
32 (eval-when-compile
33 (let ((load-path
34 (if (and (boundp 'byte-compile-dest-file)
35 (stringp byte-compile-dest-file))
36 (cons (file-name-directory byte-compile-dest-file) load-path)
37 load-path)))
38 (require 'cc-bytecomp)))
39
40 (cc-require 'cc-defs)
41 (cc-require 'cc-vars)
42 (cc-require 'cc-langs)
43
44 ;; Silence the compiler.
45 (cc-bytecomp-defun buffer-syntactic-context) ; XEmacs
46
47 \f
48 (defvar c-state-cache nil)
49 (defvar c-in-literal-cache t)
50
51 ;; KLUDGE ALERT: c-maybe-labelp is used to pass information between
52 ;; c-crosses-statement-barrier-p and c-beginning-of-statement-1. A
53 ;; better way should be implemented, but this will at least shut up
54 ;; the byte compiler.
55 (defvar c-maybe-labelp nil)
56
57 ;; WARNING WARNING WARNING
58 ;;
59 ;; Be *exceptionally* careful about modifications to this function!
60 ;; Much of CC Mode depends on this Doing The Right Thing. If you
61 ;; break it you will be sorry. If you think you know how this works,
62 ;; you probably don't. No human on Earth does! :-)
63 ;;
64 ;; WARNING WARNING WARNING
65
66 (defun c-beginning-of-statement-1 (&optional lim)
67 ;; move to the start of the current statement, or the previous
68 ;; statement if already at the beginning of one.
69 (let ((firstp t)
70 (substmt-p t)
71 donep c-in-literal-cache saved
72 (last-begin (point)))
73 ;; first check for bare semicolon
74 (if (and (progn (c-backward-syntactic-ws lim)
75 (eq (char-before) ?\;))
76 (c-safe (progn (forward-char -1)
77 (setq saved (point))
78 t))
79 (progn (c-backward-syntactic-ws lim)
80 (memq (char-before) '(?\; ?{ ?:)))
81 )
82 (setq last-begin saved)
83 (goto-char last-begin)
84 (while (not donep)
85 ;; stop at beginning of buffer
86 (if (bobp) (setq donep t)
87 ;; go backwards one balanced expression, but be careful of
88 ;; unbalanced paren being reached
89 (if (not (c-safe (progn (c-backward-sexp 1) t)))
90 (progn
91 (if firstp
92 (backward-up-list 1)
93 (goto-char last-begin))
94 ;; skip over any unary operators, or other special
95 ;; characters appearing at front of identifier
96 (save-excursion
97 (c-backward-syntactic-ws lim)
98 (skip-chars-backward "-+!*&:.~@ \t\n")
99 (if (eq (char-before) ?\()
100 (setq last-begin (point))))
101 (goto-char last-begin)
102 (setq donep t)))
103
104 (setq c-maybe-labelp nil)
105 ;; see if we're in a literal. if not, then this bufpos may be
106 ;; a candidate for stopping
107 (cond
108 ;; CASE 0: did we hit the error condition above?
109 (donep)
110 ;; CASE 1: are we in a literal?
111 ((eq (c-in-literal lim) 'pound)
112 (beginning-of-line))
113 ;; CASE 2: some other kind of literal?
114 ((c-in-literal lim))
115 ;; CASE 3: are we looking at a conditional keyword?
116 ((or (and c-conditional-key (looking-at c-conditional-key))
117 (and (eq (char-after) ?\()
118 (save-excursion
119 (c-forward-sexp 1)
120 (c-forward-syntactic-ws)
121 (not (eq (char-after) ?\;)))
122 (let ((here (point))
123 (foundp (progn
124 (c-backward-syntactic-ws lim)
125 (forward-word -1)
126 (and lim
127 (<= lim (point))
128 (not (c-in-literal lim))
129 (not (eq (char-before) ?_))
130 c-conditional-key
131 (looking-at c-conditional-key)
132 ))))
133 ;; did we find a conditional?
134 (if (not foundp)
135 (goto-char here))
136 foundp)))
137 ;; are we in the middle of an else-if clause?
138 (if (save-excursion
139 (and (not substmt-p)
140 (c-safe (progn (c-forward-sexp -1) t))
141 (looking-at "\\<else\\>[ \t\n]+\\<if\\>")
142 (not (c-in-literal lim))))
143 (progn
144 (c-forward-sexp -1)
145 (c-backward-to-start-of-if lim)))
146 ;; are we sitting at an else clause, that we are not a
147 ;; substatement of?
148 (if (and (not substmt-p)
149 (looking-at "\\<else\\>[^_]"))
150 (c-backward-to-start-of-if lim))
151 ;; a finally or a series of catches?
152 (if (not substmt-p)
153 (while (looking-at "\\<\\(catch\\|finally\\)\\>[^_]")
154 (c-safe (c-backward-sexp 2))
155 (if (eq (char-after) ?\()
156 (c-safe (c-backward-sexp)))))
157 ;; are we sitting at the while of a do-while?
158 (if (and (looking-at "\\<while\\>[^_]")
159 (c-backward-to-start-of-do lim))
160 (setq substmt-p nil))
161 (setq last-begin (point)
162 donep substmt-p))
163 ;; CASE 4: are we looking at a label? (But we handle
164 ;; switch labels later.)
165 ((and (looking-at c-label-key)
166 (not (looking-at "default\\>"))
167 (not (and (c-major-mode-is 'pike-mode)
168 (save-excursion
169 ;; Not inside a Pike type declaration?
170 (and (c-safe (backward-up-list 1) t)
171 (eq (char-after) ?\()))))))
172 ;; CASE 5: is this the first time we're checking?
173 (firstp (setq firstp nil
174 substmt-p (not (c-crosses-statement-barrier-p
175 (point) last-begin))
176 last-begin (point)))
177 ;; CASE 6: have we crossed a statement barrier?
178 ((save-excursion
179 ;; Move over in-expression blocks before checking the
180 ;; barrier
181 (if (or (memq (char-after) '(?\( ?\[))
182 (and (eq (char-after) ?{)
183 (c-looking-at-inexpr-block lim)))
184 (c-forward-sexp 1))
185 (c-crosses-statement-barrier-p (point) last-begin))
186 (setq donep t))
187 ;; CASE 7: ignore labels
188 ((and c-maybe-labelp
189 (or (and c-access-key (looking-at c-access-key))
190 ;; with switch labels, we have to go back further
191 ;; to try to pick up the case or default
192 ;; keyword. Potential bogosity alert: we assume
193 ;; `case' or `default' is first thing on line
194 (let ((here (point)))
195 (beginning-of-line)
196 (c-forward-syntactic-ws here)
197 (if (looking-at c-switch-label-key)
198 t
199 (goto-char here)
200 nil)))))
201 ;; CASE 8: ObjC or Java method def
202 ((and c-method-key
203 (setq last-begin (c-in-method-def-p)))
204 (setq donep t))
205 ;; CASE 9: Normal token. At bob, we can end up at ws or a
206 ;; comment, and last-begin shouldn't be updated then.
207 ((not (looking-at "\\s \\|/[/*]"))
208 (setq last-begin (point)))
209 ))))
210 (goto-char last-begin)
211 ;; We always want to skip over the non-whitespace modifier
212 ;; characters that can start a statement.
213 (let ((lim (point)))
214 (skip-chars-backward "-+!*&~@`# \t\n" (c-point 'boi))
215 (skip-chars-forward " \t\n" lim))))
216
217 (defun c-end-of-statement-1 ()
218 (condition-case nil
219 (let (beg end found)
220 (while (and (not (eobp))
221 (progn
222 (setq beg (point))
223 (c-forward-sexp 1)
224 (setq end (point))
225 (goto-char beg)
226 (setq found nil)
227 (while (and (not found)
228 (re-search-forward "[;{}]" end t))
229 (if (not (c-in-literal beg))
230 (setq found t)))
231 (not found)))
232 (goto-char end))
233 (re-search-backward "[;{}]")
234 (forward-char 1))
235 (error
236 (let ((beg (point)))
237 (c-safe (backward-up-list -1))
238 (let ((end (point)))
239 (goto-char beg)
240 (search-forward ";" end 'move)))
241 )))
242
243 \f
244 (defun c-crosses-statement-barrier-p (from to)
245 ;; Does buffer positions FROM to TO cross a C statement boundary?
246 (let ((here (point))
247 (lim from)
248 crossedp)
249 (condition-case ()
250 (progn
251 (goto-char from)
252 (while (and (not crossedp)
253 (< (point) to))
254 (skip-chars-forward "^;{}:" (1- to))
255 (if (not (c-in-literal lim))
256 (progn
257 (if (memq (char-after) '(?\; ?{ ?}))
258 (setq crossedp t)
259 (if (eq (char-after) ?:)
260 (setq c-maybe-labelp t))
261 (forward-char 1))
262 (setq lim (point)))
263 (forward-char 1))))
264 (error (setq crossedp nil)))
265 (goto-char here)
266 crossedp))
267
268 \f
269 (defun c-beginning-of-macro (&optional lim)
270 ;; Go to the beginning of a cpp macro definition. Leaves point at
271 ;; the beginning of the macro and returns t if in a cpp macro
272 ;; definition, otherwise returns nil and leaves point unchanged.
273 ;; `lim' is currently ignored, but the interface requires it.
274 (let ((here (point)))
275 (beginning-of-line)
276 (while (eq (char-before (1- (point))) ?\\)
277 (forward-line -1))
278 (back-to-indentation)
279 (if (and (<= (point) here)
280 (eq (char-after) ?#))
281 t
282 (goto-char here)
283 nil)))
284
285 ;; Skipping of "syntactic whitespace", defined as lexical whitespace,
286 ;; C and C++ style comments, and preprocessor directives. Search no
287 ;; farther back or forward than optional LIM. If LIM is omitted,
288 ;; `beginning-of-defun' is used for backward skipping, point-max is
289 ;; used for forward skipping.
290
291 (defun c-forward-syntactic-ws (&optional lim)
292 ;; Forward skip of syntactic whitespace for Emacs 19.
293 (let* ((here (point-max))
294 (hugenum (point-max)))
295 (while (/= here (point))
296 (setq here (point))
297 (c-forward-comment hugenum)
298 ;; skip preprocessor directives
299 (when (and (eq (char-after) ?#)
300 (= (c-point 'boi) (point)))
301 (while (and (eq (char-before (c-point 'eol)) ?\\)
302 (= (forward-line 1) 0)))
303 (end-of-line))
304 )
305 (if lim (goto-char (min (point) lim)))))
306
307 (defun c-backward-syntactic-ws (&optional lim)
308 ;; Backward skip over syntactic whitespace for Emacs 19.
309 (let* ((here (point-min))
310 (hugenum (- (point-max))))
311 (while (/= here (point))
312 (setq here (point))
313 (c-forward-comment hugenum)
314 (c-beginning-of-macro))
315 (if lim (goto-char (max (point) lim)))))
316
317 \f
318 ;; Moving by tokens, where a token is defined as all symbols and
319 ;; identifiers which aren't syntactic whitespace (note that "->" is
320 ;; considered to be two tokens). Point is always either left at the
321 ;; beginning of a token or not moved at all. COUNT specifies the
322 ;; number of tokens to move; a negative COUNT moves in the opposite
323 ;; direction. A COUNT of 0 moves to the next token beginning only if
324 ;; not already at one. If BALANCED is true, move over balanced
325 ;; parens, otherwise move into them. Also, if BALANCED is true, never
326 ;; move out of an enclosing paren. LIM sets the limit for the
327 ;; movement and defaults to the point limit. Returns the number of
328 ;; tokens left to move (positive or negative). If BALANCED is true, a
329 ;; move over a balanced paren counts as one. Note that if COUNT is 0
330 ;; and no appropriate token beginning is found, 1 will be returned.
331 ;; Thus, a return value of 0 guarantees that point is at the requested
332 ;; position and a return value less (without signs) than COUNT
333 ;; guarantees that point is at the beginning of some token.
334
335 (defun c-forward-token-1 (&optional count balanced lim)
336 (or count (setq count 1))
337 (if (< count 0)
338 (- (c-backward-token-1 (- count) balanced lim))
339 (let ((jump-syntax (if balanced
340 '(?w ?_ ?\( ?\) ?\" ?\\ ?/ ?$ ?')
341 '(?w ?_ ?\" ?\\ ?/ ?')))
342 (last (point))
343 (prev (point)))
344 (save-restriction
345 (if lim (narrow-to-region (point-min) lim))
346 (if (/= (point)
347 (progn (c-forward-syntactic-ws) (point)))
348 ;; Skip whitespace. Count this as a move if we did in fact
349 ;; move and aren't out of bounds.
350 (or (eobp)
351 (setq count (max (1- count) 0))))
352 (if (and (= count 0)
353 (or (and (memq (char-syntax (or (char-after) ? )) '(?w ?_))
354 (memq (char-syntax (or (char-before) ? )) '(?w ?_)))
355 (eobp)))
356 ;; If count is zero we should jump if in the middle of a
357 ;; token or if there is whitespace between point and the
358 ;; following token beginning.
359 (setq count 1))
360 (if (eobp)
361 (goto-char last)
362 ;; Avoid having the limit tests inside the loop.
363 (condition-case nil
364 (while (> count 0)
365 (setq prev last
366 last (point))
367 (if (memq (char-syntax (char-after)) jump-syntax)
368 (goto-char (scan-sexps (point) 1))
369 (forward-char))
370 (c-forward-syntactic-ws lim)
371 (setq count (1- count)))
372 (error (goto-char last)))
373 (when (eobp)
374 (goto-char prev)
375 (setq count (1+ count)))))
376 count)))
377
378 (defun c-backward-token-1 (&optional count balanced lim)
379 (or count (setq count 1))
380 (if (< count 0)
381 (- (c-forward-token-1 (- count) balanced lim))
382 (let ((jump-syntax (if balanced
383 '(?w ?_ ?\( ?\) ?\" ?\\ ?/ ?$ ?')
384 '(?w ?_ ?\" ?\\ ?/ ?')))
385 last)
386 (if (and (= count 0)
387 (or (and (memq (char-syntax (or (char-after) ? )) '(?w ?_))
388 (memq (char-syntax (or (char-before) ? )) '(?w ?_)))
389 (/= (point)
390 (save-excursion (c-forward-syntactic-ws) (point)))
391 (eobp)))
392 ;; If count is zero we should jump if in the middle of a
393 ;; token or if there is whitespace between point and the
394 ;; following token beginning.
395 (setq count 1))
396 (save-restriction
397 (if lim (narrow-to-region lim (point-max)))
398 (or (bobp)
399 (progn
400 ;; Avoid having the limit tests inside the loop.
401 (condition-case nil
402 (while (progn
403 (setq last (point))
404 (> count 0))
405 (c-backward-syntactic-ws lim)
406 (if (memq (char-syntax (char-before)) jump-syntax)
407 (goto-char (scan-sexps (point) -1))
408 (backward-char))
409 (setq count (1- count)))
410 (error (goto-char last)))
411 (if (bobp) (goto-char last)))))
412 count)))
413
414 \f
415 ;; Return `c' if in a C-style comment, `c++' if in a C++ style
416 ;; comment, `string' if in a string literal, `pound' if on a
417 ;; preprocessor line, or nil if not in a comment at all. Optional LIM
418 ;; is used as the backward limit of the search. If omitted, or nil,
419 ;; `beginning-of-defun' is used."
420
421 (defun c-in-literal (&optional lim)
422 ;; Determine if point is in a C++ literal. we cache the last point
423 ;; calculated if the cache is enabled
424 (if (and (vectorp c-in-literal-cache)
425 (= (point) (aref c-in-literal-cache 0)))
426 (aref c-in-literal-cache 1)
427 (let ((rtn (save-excursion
428 (let* ((lim (or lim (c-point 'bod)))
429 (state (parse-partial-sexp lim (point))))
430 (cond
431 ((nth 3 state) 'string)
432 ((nth 4 state) (if (nth 7 state) 'c++ 'c))
433 ((c-beginning-of-macro lim) 'pound)
434 (t nil))))))
435 ;; cache this result if the cache is enabled
436 (if (not c-in-literal-cache)
437 (setq c-in-literal-cache (vector (point) rtn)))
438 rtn)))
439
440 ;; XEmacs has a built-in function that should make this much quicker.
441 ;; I don't think we even need the cache, which makes our lives more
442 ;; complicated anyway. In this case, lim is ignored.
443 (defun c-fast-in-literal (&optional lim)
444 (let ((context (buffer-syntactic-context)))
445 (cond
446 ((eq context 'string) 'string)
447 ((eq context 'comment) 'c++)
448 ((eq context 'block-comment) 'c)
449 ((save-excursion (c-beginning-of-macro lim)) 'pound))))
450
451 (if (fboundp 'buffer-syntactic-context)
452 (defalias 'c-in-literal 'c-fast-in-literal))
453
454 (defun c-literal-limits (&optional lim near not-in-delimiter)
455 ;; Returns a cons of the beginning and end positions of the comment
456 ;; or string surrounding point (including both delimiters), or nil
457 ;; if point isn't in one. If LIM is non-nil, it's used as the
458 ;; "safe" position to start parsing from. If NEAR is non-nil, then
459 ;; the limits of any literal next to point is returned. "Next to"
460 ;; means there's only [ \t] between point and the literal. The
461 ;; search for such a literal is done first in forward direction. If
462 ;; NOT-IN-DELIMITER is non-nil, the case when point is inside a
463 ;; starting delimiter won't be recognized. This only has effect for
464 ;; comments, which have starting delimiters with more than one
465 ;; character.
466 (save-excursion
467 (let* ((pos (point))
468 (lim (or lim (c-point 'bod)))
469 (state (parse-partial-sexp lim (point))))
470 (cond ((nth 3 state)
471 ;; String. Search backward for the start.
472 (while (nth 3 state)
473 (search-backward (make-string 1 (nth 3 state)))
474 (setq state (parse-partial-sexp lim (point))))
475 (cons (point) (or (c-safe (c-forward-sexp 1) (point))
476 (point-max))))
477 ((nth 7 state)
478 ;; Line comment. Search from bol for the comment starter.
479 (beginning-of-line)
480 (setq state (parse-partial-sexp lim (point))
481 lim (point))
482 (while (not (nth 7 state))
483 (search-forward "//") ; Should never fail.
484 (setq state (parse-partial-sexp
485 lim (point) nil nil state)
486 lim (point)))
487 (backward-char 2)
488 (cons (point) (progn (c-forward-comment 1) (point))))
489 ((nth 4 state)
490 ;; Block comment. Search backward for the comment starter.
491 (while (nth 4 state)
492 (search-backward "/*") ; Should never fail.
493 (setq state (parse-partial-sexp lim (point))))
494 (cons (point) (progn (c-forward-comment 1) (point))))
495 ((and (not not-in-delimiter)
496 (not (nth 5 state))
497 (eq (char-before) ?/)
498 (looking-at "[/*]"))
499 ;; We're standing in a comment starter.
500 (backward-char 1)
501 (cons (point) (progn (c-forward-comment 1) (point))))
502 (near
503 (goto-char pos)
504 ;; Search forward for a literal.
505 (skip-chars-forward " \t")
506 (cond
507 ((eq (char-syntax (or (char-after) ?\ )) ?\") ; String.
508 (cons (point) (or (c-safe (c-forward-sexp 1) (point))
509 (point-max))))
510 ((looking-at "/[/*]") ; Line or block comment.
511 (cons (point) (progn (c-forward-comment 1) (point))))
512 (t
513 ;; Search backward.
514 (skip-chars-backward " \t")
515 (let ((end (point)) beg)
516 (cond
517 ((eq (char-syntax (or (char-before) ?\ )) ?\") ; String.
518 (setq beg (c-safe (c-backward-sexp 1) (point))))
519 ((and (c-safe (forward-char -2) t)
520 (looking-at "*/"))
521 ;; Block comment. Due to the nature of line
522 ;; comments, they will always be covered by the
523 ;; normal case above.
524 (goto-char end)
525 (c-forward-comment -1)
526 ;; If LIM is bogus, beg will be bogus.
527 (setq beg (point))))
528 (if beg (cons beg end))))))
529 ))))
530
531 (defun c-literal-limits-fast (&optional lim near not-in-delimiter)
532 ;; Like c-literal-limits, but for emacsen whose `parse-partial-sexp'
533 ;; returns the pos of the comment start.
534 (save-excursion
535 (let* ((pos (point))
536 (lim (or lim (c-point 'bod)))
537 (state (parse-partial-sexp lim (point))))
538 (cond ((nth 3 state) ; String.
539 (goto-char (nth 8 state))
540 (cons (point) (or (c-safe (c-forward-sexp 1) (point))
541 (point-max))))
542 ((nth 4 state) ; Comment.
543 (goto-char (nth 8 state))
544 (cons (point) (progn (c-forward-comment 1) (point))))
545 ((and (not not-in-delimiter)
546 (not (nth 5 state))
547 (eq (char-before) ?/)
548 (looking-at "[/*]"))
549 ;; We're standing in a comment starter.
550 (backward-char 1)
551 (cons (point) (progn (c-forward-comment 1) (point))))
552 (near
553 (goto-char pos)
554 ;; Search forward for a literal.
555 (skip-chars-forward " \t")
556 (cond
557 ((eq (char-syntax (or (char-after) ?\ )) ?\") ; String.
558 (cons (point) (or (c-safe (c-forward-sexp 1) (point))
559 (point-max))))
560 ((looking-at "/[/*]") ; Line or block comment.
561 (cons (point) (progn (c-forward-comment 1) (point))))
562 (t
563 ;; Search backward.
564 (skip-chars-backward " \t")
565 (let ((end (point)) beg)
566 (cond
567 ((eq (char-syntax (or (char-before) ?\ )) ?\") ; String.
568 (setq beg (c-safe (c-backward-sexp 1) (point))))
569 ((and (c-safe (forward-char -2) t)
570 (looking-at "*/"))
571 ;; Block comment. Due to the nature of line
572 ;; comments, they will always be covered by the
573 ;; normal case above.
574 (goto-char end)
575 (c-forward-comment -1)
576 ;; If LIM is bogus, beg will be bogus.
577 (setq beg (point))))
578 (if beg (cons beg end))))))
579 ))))
580
581 (if (c-safe (> (length (save-excursion (parse-partial-sexp 1 1))) 8))
582 (defalias 'c-literal-limits 'c-literal-limits-fast))
583
584 (defun c-collect-line-comments (range)
585 ;; If the argument is a cons of two buffer positions (such as
586 ;; returned by c-literal-limits), and that range contains a C++
587 ;; style line comment, then an extended range is returned that
588 ;; contains all adjacent line comments (i.e. all comments that
589 ;; starts in the same column with no empty lines or non-whitespace
590 ;; characters between them). Otherwise the argument is returned.
591 (save-excursion
592 (condition-case nil
593 (if (and (consp range) (progn
594 (goto-char (car range))
595 (looking-at "//")))
596 (let ((col (current-column))
597 (beg (point))
598 (bopl (c-point 'bopl))
599 (end (cdr range)))
600 ;; Got to take care in the backward direction to handle
601 ;; comments which are preceded by code.
602 (while (and (c-forward-comment -1)
603 (>= (point) bopl)
604 (looking-at "//")
605 (= col (current-column)))
606 (setq beg (point)
607 bopl (c-point 'bopl)))
608 (goto-char end)
609 (while (and (progn (skip-chars-forward " \t")
610 (looking-at "//"))
611 (= col (current-column))
612 (prog1 (zerop (forward-line 1))
613 (setq end (point)))))
614 (cons beg end))
615 range)
616 (error range))))
617
618 (defun c-literal-type (range)
619 ;; Convenience function that given the result of c-literal-limits,
620 ;; returns nil or the type of literal that the range surrounds.
621 ;; It's much faster than using c-in-literal and is intended to be
622 ;; used when you need both the type of a literal and its limits.
623 (if (consp range)
624 (save-excursion
625 (goto-char (car range))
626 (cond ((eq (char-syntax (or (char-after) ?\ )) ?\") 'string)
627 ((looking-at "//") 'c++)
628 (t 'c))) ; Assuming the range is valid.
629 range))
630
631
632 \f
633 ;; utilities for moving and querying around syntactic elements
634 (defvar c-parsing-error nil)
635
636 (defun c-parse-state ()
637 ;; Finds and records all open parens between some important point
638 ;; earlier in the file and point.
639 ;;
640 ;; if there's a state cache, return it
641 (if c-state-cache c-state-cache
642 (let* (at-bob
643 (pos (save-excursion
644 ;; go back 2 bods, but ignore any bogus positions
645 ;; returned by beginning-of-defun (i.e. open paren
646 ;; in column zero)
647 (let ((cnt 2))
648 (while (not (or at-bob (zerop cnt)))
649 (goto-char (c-point 'bod))
650 (if (and
651 (eq (char-after) ?\{)
652 ;; The following catches an obscure special
653 ;; case where the brace is preceded by an
654 ;; open paren. That can only legally occur
655 ;; with blocks inside expressions and in
656 ;; Pike special brace lists. Even so, this
657 ;; test is still bogus then, but hopefully
658 ;; good enough. (We don't want to use
659 ;; up-list here since it might be slow.)
660 (save-excursion
661 (c-backward-syntactic-ws)
662 (not (eq (char-before) ?\())))
663 (setq cnt (1- cnt)))
664 (if (bobp)
665 (setq at-bob t))))
666 (point)))
667 (here (save-excursion
668 ;;(skip-chars-forward " \t}")
669 (point)))
670 (last-bod here) (last-pos pos)
671 placeholder state sexp-end)
672 ;; cache last bod position
673 (while (catch 'backup-bod
674 (setq state nil)
675 (while (and pos (< pos here))
676 (setq last-pos pos)
677 (if (and (setq pos (c-safe (scan-lists pos 1 -1)))
678 (<= pos here))
679 (progn
680 (setq sexp-end (c-safe (scan-sexps (1- pos) 1)))
681 (if (and sexp-end
682 (<= sexp-end here))
683 ;; we want to record both the start and end
684 ;; of this sexp, but we only want to record
685 ;; the last-most of any of them before here
686 (progn
687 (if (eq (char-after (1- pos)) ?\{)
688 (setq state (cons (cons (1- pos) sexp-end)
689 (if (consp (car state))
690 (cdr state)
691 state))))
692 (setq pos sexp-end))
693 ;; we're contained in this sexp so put pos on
694 ;; front of list
695 (setq state (cons (1- pos) state))))
696 ;; something bad happened. check to see if we
697 ;; crossed an unbalanced close brace. if so, we
698 ;; didn't really find the right `important bufpos'
699 ;; so lets back up and try again
700 (if (and (not pos) (not at-bob)
701 (setq placeholder
702 (c-safe (scan-lists last-pos 1 1)))
703 ;;(char-after (1- placeholder))
704 (<= placeholder here)
705 (eq (char-after (1- placeholder)) ?\}))
706 (while t
707 (setq last-bod (c-safe (scan-lists last-bod -1 1)))
708 (if (not last-bod)
709 (save-excursion
710 ;; bogus, but what can we do here?
711 (goto-char placeholder)
712 (beginning-of-line)
713 (setq c-parsing-error
714 (format "\
715 Unbalanced close brace at line %d" (1+ (count-lines 1 (point)))))
716 (throw 'backup-bod nil))
717 (setq at-bob (= last-bod (point-min))
718 pos last-bod)
719 (if (= (char-after last-bod) ?\{)
720 (throw 'backup-bod t)))
721 )) ;end-if
722 )) ;end-while
723 nil))
724 state)))
725
726 (defun c-whack-state (bufpos state)
727 ;; whack off any state information that appears on STATE which lies
728 ;; after the bounds of BUFPOS.
729 (let (newstate car)
730 (while state
731 (setq car (car state)
732 state (cdr state))
733 (if (consp car)
734 ;; just check the car, because in a balanced brace
735 ;; expression, it must be impossible for the corresponding
736 ;; close brace to be before point, but the open brace to be
737 ;; after.
738 (if (<= bufpos (car car))
739 nil ; whack it off
740 ;; its possible that the open brace is before bufpos, but
741 ;; the close brace is after. In that case, convert this
742 ;; to a non-cons element.
743 (if (<= bufpos (cdr car))
744 (setq newstate (append newstate (list (car car))))
745 ;; we know that both the open and close braces are
746 ;; before bufpos, so we also know that everything else
747 ;; on state is before bufpos, so we can glom up the
748 ;; whole thing and exit.
749 (setq newstate (append newstate (list car) state)
750 state nil)))
751 (if (<= bufpos car)
752 nil ; whack it off
753 ;; it's before bufpos, so everything else should too
754 (setq newstate (append newstate (list car) state)
755 state nil))))
756 newstate))
757
758 (defun c-hack-state (bufpos which state)
759 ;; Using BUFPOS buffer position, and WHICH (must be 'open or
760 ;; 'close), hack the c-parse-state STATE and return the results.
761 (if (eq which 'open)
762 (let ((car (car state)))
763 (if (or (null car)
764 (consp car)
765 (/= bufpos car))
766 (cons bufpos state)
767 state))
768 (if (not (eq which 'close))
769 (error "c-hack-state, bad argument: %s" which))
770 ;; 'close brace
771 (let ((car (car state))
772 (cdr (cdr state)))
773 (if (consp car)
774 (setq car (car cdr)
775 cdr (cdr cdr)))
776 ;; TBD: is this test relevant???
777 (if (consp car)
778 state ;on error, don't change
779 ;; watch out for balanced expr already on cdr of list
780 (cons (cons car bufpos)
781 (if (consp (car cdr))
782 (cdr cdr) cdr))
783 ))))
784
785 (defun c-adjust-state (from to shift state)
786 ;; Adjust all points in state that lie in the region FROM..TO by
787 ;; SHIFT amount.
788 (mapcar
789 (function
790 (lambda (e)
791 (if (consp e)
792 (let ((car (car e))
793 (cdr (cdr e)))
794 (if (and (<= from car) (< car to))
795 (setcar e (+ shift car)))
796 (if (and (<= from cdr) (< cdr to))
797 (setcdr e (+ shift cdr))))
798 (if (and (<= from e) (< e to))
799 (setq e (+ shift e))))
800 e))
801 state))
802
803 \f
804 (defun c-beginning-of-inheritance-list (&optional lim)
805 ;; Go to the first non-whitespace after the colon that starts a
806 ;; multiple inheritance introduction. Optional LIM is the farthest
807 ;; back we should search.
808 (let* ((lim (or lim (c-point 'bod)))
809 (placeholder (progn
810 (back-to-indentation)
811 (point)))
812 (chr (char-after)))
813 (c-backward-syntactic-ws lim)
814 (while (and (> (point) lim)
815 (or (eq chr ?,)
816 (memq (char-before) '(?, ?:)))
817 (progn
818 (beginning-of-line)
819 (setq placeholder (point))
820 (skip-chars-forward " \t")
821 (setq chr (char-after))
822 (not (looking-at c-class-key))
823 ))
824 (c-backward-syntactic-ws lim))
825 (goto-char placeholder)
826 (skip-chars-forward "^:" (c-point 'eol))))
827
828 (defun c-in-method-def-p ()
829 ;; Return nil if we aren't in a method definition, otherwise the
830 ;; position of the initial [+-].
831 (save-excursion
832 (beginning-of-line)
833 (and c-method-key
834 (looking-at c-method-key)
835 (point))
836 ))
837
838 (defun c-at-toplevel-p ()
839 "Return a determination as to whether point is at the `top-level'.
840 Being at the top-level means that point is either outside any
841 enclosing block (such function definition), or inside a class
842 definition, but outside any method blocks.
843
844 If point is not at the top-level (e.g. it is inside a method
845 definition), then nil is returned. Otherwise, if point is at a
846 top-level not enclosed within a class definition, t is returned.
847 Otherwise, a 2-vector is returned where the zeroth element is the
848 buffer position of the start of the class declaration, and the first
849 element is the buffer position of the enclosing class's opening
850 brace."
851 (let ((state (c-parse-state)))
852 (or (not (c-most-enclosing-brace state))
853 (c-search-uplist-for-classkey state))))
854
855 (defun c-just-after-func-arglist-p (&optional containing)
856 ;; Return t if we are between a function's argument list closing
857 ;; paren and its opening brace. Note that the list close brace
858 ;; could be followed by a "const" specifier or a member init hanging
859 ;; colon. Optional CONTAINING is position of containing s-exp open
860 ;; brace. If not supplied, point is used as search start.
861 (save-excursion
862 (c-backward-syntactic-ws)
863 (let ((checkpoint (or containing (point))))
864 (goto-char checkpoint)
865 ;; could be looking at const specifier
866 (if (and (eq (char-before) ?t)
867 (forward-word -1)
868 (looking-at "\\<const\\>"))
869 (c-backward-syntactic-ws)
870 ;; otherwise, we could be looking at a hanging member init
871 ;; colon
872 (goto-char checkpoint)
873 (while (eq (char-before) ?,)
874 ;; this will catch member inits with multiple
875 ;; line arglists
876 (forward-char -1)
877 (c-backward-syntactic-ws (c-point 'bol))
878 (if (eq (char-before) ?\))
879 (c-backward-sexp 2)
880 (c-backward-sexp 1))
881 (c-backward-syntactic-ws))
882 (if (and (eq (char-before) ?:)
883 (progn
884 (forward-char -1)
885 (c-backward-syntactic-ws)
886 (looking-at "[ \t\n]*:\\([^:]+\\|$\\)")))
887 nil
888 (goto-char checkpoint))
889 )
890 (and (eq (char-before) ?\))
891 ;; check if we are looking at a method def
892 (or (not c-method-key)
893 (progn
894 (c-forward-sexp -1)
895 (forward-char -1)
896 (c-backward-syntactic-ws)
897 (not (or (memq (char-before) '(?- ?+))
898 ;; or a class category
899 (progn
900 (c-forward-sexp -2)
901 (looking-at c-class-key))
902 )))))
903 )))
904
905 ;; defuns to look backwards for things
906 (defun c-backward-to-start-of-do (&optional lim)
907 ;; Move to the start of the last "unbalanced" do expression.
908 ;; Optional LIM is the farthest back to search. If none is found,
909 ;; nil is returned and point is left unchanged, otherwise t is returned.
910 (let ((do-level 1)
911 (case-fold-search nil)
912 (lim (or lim (c-point 'bod)))
913 (here (point))
914 foundp)
915 (while (not (zerop do-level))
916 ;; we protect this call because trying to execute this when the
917 ;; while is not associated with a do will throw an error
918 (condition-case nil
919 (progn
920 (c-backward-sexp 1)
921 (cond
922 ;; break infloop for illegal C code
923 ((bobp) (setq do-level 0))
924 ((memq (c-in-literal lim) '(c c++)))
925 ((looking-at "while\\b[^_]")
926 (setq do-level (1+ do-level)))
927 ((looking-at "do\\b[^_]")
928 (if (zerop (setq do-level (1- do-level)))
929 (setq foundp t)))
930 ((<= (point) lim)
931 (setq do-level 0)
932 (goto-char lim))))
933 (error
934 (goto-char lim)
935 (setq do-level 0))))
936 (if (not foundp)
937 (goto-char here))
938 foundp))
939
940 (defun c-backward-to-start-of-if (&optional lim)
941 ;; Move to the start of the last "unbalanced" if and return t. If
942 ;; none is found, and we are looking at an if clause, nil is
943 ;; returned.
944 (let ((if-level 1)
945 (here (c-point 'bol))
946 (case-fold-search nil)
947 (lim (or (and lim (>= (point) lim) lim)
948 (c-point 'bod)))
949 (at-if (looking-at "if\\b[^_]")))
950 (catch 'orphan-if
951 (while (and (not (bobp))
952 (not (zerop if-level)))
953 (c-backward-syntactic-ws)
954 (condition-case nil
955 (c-backward-sexp 1)
956 (error
957 (unless at-if
958 (goto-char here)
959 (c-beginning-of-statement-1)
960 (setq c-parsing-error
961 (format "No matching `if' found for `else' on line %d"
962 (1+ (count-lines 1 here))))
963 (throw 'orphan-if nil))))
964 (cond
965 ((looking-at "else\\b[^_]")
966 (setq if-level (1+ if-level)))
967 ((looking-at "if\\b[^_]")
968 ;; check for else if... skip over
969 (let ((here (point)))
970 (c-safe (c-forward-sexp -1))
971 (if (looking-at "\\<else\\>[ \t]+\\<if\\>[^_]")
972 nil
973 (setq if-level (1- if-level))
974 (goto-char here))))
975 ((< (point) lim)
976 (setq if-level 0)
977 (goto-char lim))
978 ))
979 t)))
980
981 (defun c-skip-conditional ()
982 ;; skip forward over conditional at point, including any predicate
983 ;; statements in parentheses. No error checking is performed.
984 (c-forward-sexp (cond
985 ;; else if()
986 ((looking-at "\\<else\\>[ \t]+\\<if\\>\\([^_]\\|$\\)") 3)
987 ;; do, else, try, finally
988 ((looking-at
989 "\\<\\(do\\|else\\|try\\|finally\\)\\>\\([^_]\\|$\\)")
990 1)
991 ;; for, if, while, switch, catch, synchronized, foreach
992 (t 2))))
993
994 (defun c-beginning-of-closest-statement (&optional lim)
995 ;; Go back to the closest preceding statement start.
996 (let ((start (point))
997 (label-re (concat c-label-key "\\|"
998 c-switch-label-key))
999 stmtbeg)
1000 (if c-access-key
1001 (setq label-re (concat label-re "\\|" c-access-key)))
1002 (c-beginning-of-statement-1 lim)
1003 (while (and (when (<= (point) start)
1004 (setq stmtbeg (point)))
1005 (cond
1006 ((looking-at label-re)
1007 ;; Skip a label.
1008 (goto-char (match-end 0))
1009 t)
1010 ((looking-at c-conditional-key)
1011 ;; Skip a conditional statement.
1012 (c-safe (c-skip-conditional) t))
1013 (t nil)))
1014 (c-forward-syntactic-ws start))
1015 (if stmtbeg
1016 (goto-char stmtbeg))))
1017
1018 (defun c-beginning-of-member-init-list (&optional limit)
1019 ;; Goes to the beginning of a member init list (i.e. just after the
1020 ;; ':') if inside one. Returns t in that case, nil otherwise.
1021 (or limit
1022 (setq limit (point-min)))
1023 (skip-chars-forward " \t")
1024 (if (eq (char-after) ?,)
1025 (forward-char 1)
1026 (c-backward-syntactic-ws limit))
1027 (c-with-syntax-table (if (c-major-mode-is 'c++-mode)
1028 c++-template-syntax-table
1029 (syntax-table))
1030 (while (and (< limit (point))
1031 (eq (char-before) ?,))
1032 ;; this will catch member inits with multiple
1033 ;; line arglists
1034 (forward-char -1)
1035 (c-backward-syntactic-ws)
1036 (if (eq (char-before) ?\))
1037 (c-backward-sexp 2)
1038 (c-backward-sexp 1))
1039 ;; Skip over any template arg to the class.
1040 (if (eq (char-after) ?<)
1041 (c-backward-sexp 1))
1042 ;; Skip backwards over a fully::qualified::name.
1043 (c-backward-syntactic-ws limit)
1044 (while (and (eq (char-before) ?:)
1045 (save-excursion
1046 (forward-char -1)
1047 (eq (char-before) ?:)))
1048 (backward-char 2)
1049 (c-backward-sexp 1))
1050 ;; now continue checking
1051 (c-backward-syntactic-ws limit)))
1052 (and (< limit (point))
1053 (eq (char-before) ?:)))
1054
1055 (defun c-skip-case-statement-forward (state &optional lim)
1056 ;; skip forward over case/default bodies, with optional maximal
1057 ;; limit. if no next case body is found, nil is returned and point
1058 ;; is not moved
1059 (let ((lim (or lim (point-max)))
1060 (here (point))
1061 donep foundp bufpos
1062 (safepos (point))
1063 (balanced (car state)))
1064 ;; search until we've passed the limit, or we've found our match
1065 (while (and (< (point) lim)
1066 (not donep))
1067 (setq safepos (point))
1068 ;; see if we can find a case statement, not in a literal
1069 (if (and (re-search-forward c-switch-label-key lim 'move)
1070 (setq bufpos (match-beginning 0))
1071 (not (c-in-literal safepos))
1072 (/= bufpos here))
1073 ;; if we crossed into a balanced sexp, we know the case is
1074 ;; not part of our switch statement, so just bound over the
1075 ;; sexp and keep looking.
1076 (if (and (consp balanced)
1077 (> bufpos (car balanced))
1078 (< bufpos (cdr balanced)))
1079 (goto-char (cdr balanced))
1080 (goto-char bufpos)
1081 (setq donep t
1082 foundp t))))
1083 (if (not foundp)
1084 (goto-char here))
1085 foundp))
1086
1087 (defun c-search-uplist-for-classkey (brace-state)
1088 ;; search for the containing class, returning a 2 element vector if
1089 ;; found. aref 0 contains the bufpos of the boi of the class key
1090 ;; line, and aref 1 contains the bufpos of the open brace.
1091 (if (null brace-state)
1092 ;; no brace-state means we cannot be inside a class
1093 nil
1094 (let ((carcache (car brace-state))
1095 search-start search-end)
1096 (if (consp carcache)
1097 ;; a cons cell in the first element means that there is some
1098 ;; balanced sexp before the current bufpos. this we can
1099 ;; ignore. the nth 1 and nth 2 elements define for us the
1100 ;; search boundaries
1101 (setq search-start (nth 2 brace-state)
1102 search-end (nth 1 brace-state))
1103 ;; if the car was not a cons cell then nth 0 and nth 1 define
1104 ;; for us the search boundaries
1105 (setq search-start (nth 1 brace-state)
1106 search-end (nth 0 brace-state)))
1107 ;; search-end cannot be a cons cell
1108 (and (consp search-end)
1109 (error "consp search-end: %s" search-end))
1110 ;; if search-end is nil, or if the search-end character isn't an
1111 ;; open brace, we are definitely not in a class
1112 (if (or (not search-end)
1113 (< search-end (point-min))
1114 (not (eq (char-after search-end) ?{)))
1115 nil
1116 ;; now, we need to look more closely at search-start. if
1117 ;; search-start is nil, then our start boundary is really
1118 ;; point-min.
1119 (if (not search-start)
1120 (setq search-start (point-min))
1121 ;; if search-start is a cons cell, then we can start
1122 ;; searching from the end of the balanced sexp just ahead of
1123 ;; us
1124 (if (consp search-start)
1125 (setq search-start (cdr search-start))))
1126 ;; now we can do a quick regexp search from search-start to
1127 ;; search-end and see if we can find a class key. watch for
1128 ;; class like strings in literals
1129 (save-excursion
1130 (save-restriction
1131 (goto-char search-start)
1132 (let ((search-key (concat c-class-key "\\|" c-extra-toplevel-key))
1133 foundp class match-end)
1134 (if c-inexpr-class-key
1135 (setq search-key (concat search-key "\\|"
1136 c-inexpr-class-key)))
1137 (while (and (not foundp)
1138 (progn
1139 (c-forward-syntactic-ws)
1140 (> search-end (point)))
1141 (re-search-forward search-key search-end t))
1142 (setq class (match-beginning 0)
1143 match-end (match-end 0))
1144 (if (c-in-literal search-start)
1145 nil ; its in a comment or string, ignore
1146 (goto-char class)
1147 (skip-chars-forward " \t\n")
1148 (setq foundp (vector (c-point 'boi) search-end))
1149 (cond
1150 ;; check for embedded keywords
1151 ((let ((char (char-after (1- class))))
1152 (and char
1153 (memq (char-syntax char) '(?w ?_))))
1154 (goto-char match-end)
1155 (setq foundp nil))
1156 ;; make sure we're really looking at the start of a
1157 ;; class definition, and not a forward decl, return
1158 ;; arg, template arg list, or an ObjC or Java method.
1159 ((and c-method-key
1160 (re-search-forward c-method-key search-end t)
1161 (not (c-in-literal class)))
1162 (setq foundp nil))
1163 ;; Check if this is an anonymous inner class.
1164 ((and c-inexpr-class-key
1165 (looking-at c-inexpr-class-key))
1166 (while (and (= (c-forward-token-1 1 t) 0)
1167 (looking-at "(\\|\\w\\|\\s_\\|\\.")))
1168 (if (eq (point) search-end)
1169 ;; We're done. Just trap this case in the cond.
1170 nil
1171 ;; False alarm; all conditions aren't satisfied.
1172 (setq foundp nil)))
1173 ;; Its impossible to define a regexp for this, and
1174 ;; nearly so to do it programmatically.
1175 ;;
1176 ;; ; picks up forward decls
1177 ;; = picks up init lists
1178 ;; ) picks up return types
1179 ;; > picks up templates, but remember that we can
1180 ;; inherit from templates!
1181 ((let ((skipchars "^;=)"))
1182 ;; try to see if we found the `class' keyword
1183 ;; inside a template arg list
1184 (save-excursion
1185 (skip-chars-backward "^<>" search-start)
1186 (if (eq (char-before) ?<)
1187 (setq skipchars (concat skipchars ">"))))
1188 (while (progn
1189 (skip-chars-forward skipchars search-end)
1190 (c-in-literal class))
1191 (forward-char))
1192 (/= (point) search-end))
1193 (setq foundp nil))
1194 )))
1195 foundp))
1196 )))))
1197
1198 (defun c-inside-bracelist-p (containing-sexp brace-state)
1199 ;; return the buffer position of the beginning of the brace list
1200 ;; statement if we're inside a brace list, otherwise return nil.
1201 ;; CONTAINING-SEXP is the buffer pos of the innermost containing
1202 ;; paren. BRACE-STATE is the remainder of the state of enclosing
1203 ;; braces
1204 ;;
1205 ;; N.B.: This algorithm can potentially get confused by cpp macros
1206 ;; places in inconvenient locations. Its a trade-off we make for
1207 ;; speed.
1208 (or
1209 ;; this will pick up enum lists
1210 (c-safe
1211 (save-excursion
1212 (goto-char containing-sexp)
1213 (c-forward-sexp -1)
1214 (let (bracepos)
1215 (if (and (or (looking-at "enum[\t\n ]+")
1216 (progn (c-forward-sexp -1)
1217 (looking-at "enum[\t\n ]+")))
1218 (setq bracepos (c-safe (scan-lists (point) 1 -1)))
1219 (not (c-crosses-statement-barrier-p (point)
1220 (- bracepos 2))))
1221 (point)))))
1222 ;; this will pick up array/aggregate init lists, even if they are nested.
1223 (save-excursion
1224 (let ((class-key
1225 ;; Pike can have class definitions anywhere, so we must
1226 ;; check for the class key here.
1227 (and (c-major-mode-is 'pike-mode)
1228 (concat c-class-key "\\|" c-extra-toplevel-key)))
1229 bufpos lim braceassignp)
1230 (while (and (not bufpos)
1231 containing-sexp)
1232 (if (consp containing-sexp)
1233 (setq containing-sexp (car brace-state)
1234 brace-state (cdr brace-state))
1235 (goto-char containing-sexp)
1236 (if (c-looking-at-inexpr-block)
1237 ;; We're in an in-expression block of some kind. Do
1238 ;; not check nesting.
1239 (setq containing-sexp nil)
1240 ;; see if the open brace is preceded by = or [...] in
1241 ;; this statement, but watch out for operator=
1242 (setq lim (if (consp (car brace-state))
1243 (cdr (car brace-state))
1244 (car brace-state))
1245 braceassignp 'dontknow)
1246 (c-backward-token-1 1 t lim)
1247 ;; Checks to do only on the first sexp before the brace.
1248 (when (and (c-major-mode-is 'java-mode)
1249 (eq (char-after) ?\[))
1250 ;; In Java, an initialization brace list may follow
1251 ;; directly after "new Foo[]", so check for a "new"
1252 ;; earlier.
1253 (while (eq braceassignp 'dontknow)
1254 (setq braceassignp
1255 (cond ((/= (c-backward-token-1 1 t lim) 0) nil)
1256 ((looking-at "new\\>[^_]") t)
1257 ((looking-at "\\sw\\|\\s_\\|[.[]")
1258 ;; Carry on looking if this is an
1259 ;; identifier (may contain "." in Java)
1260 ;; or another "[]" sexp.
1261 'dontknow)
1262 (t nil)))))
1263 ;; Checks to do on all sexps before the brace, up to the
1264 ;; beginning of the statement.
1265 (while (eq braceassignp 'dontknow)
1266 (cond ((eq (char-after) ?\;)
1267 (setq braceassignp nil))
1268 ((and class-key
1269 (looking-at class-key))
1270 (setq braceassignp nil))
1271 ((eq (char-after) ?=)
1272 ;; We've seen a =, but must check earlier tokens so
1273 ;; that it isn't something that should be ignored.
1274 (setq braceassignp 'maybe)
1275 (while (and (eq braceassignp 'maybe)
1276 (zerop (c-backward-token-1 1 t lim)))
1277 (setq braceassignp
1278 (cond
1279 ;; Check for operator =
1280 ((looking-at "operator\\>") nil)
1281 ;; Check for `<opchar>= in Pike.
1282 ((and (c-major-mode-is 'pike-mode)
1283 (or (eq (char-after) ?`)
1284 ;; Special case for Pikes
1285 ;; `[]=, since '[' is not in
1286 ;; the punctuation class.
1287 (and (eq (char-after) ?\[)
1288 (eq (char-before) ?`))))
1289 nil)
1290 ((looking-at "\\s.") 'maybe)
1291 ;; make sure we're not in a C++ template
1292 ;; argument assignment
1293 ((and (c-major-mode-is 'c++-mode)
1294 (save-excursion
1295 (let ((here (point))
1296 (pos< (progn
1297 (skip-chars-backward "^<>")
1298 (point))))
1299 (and (eq (char-before) ?<)
1300 (not (c-crosses-statement-barrier-p
1301 pos< here))
1302 (not (c-in-literal))
1303 ))))
1304 nil)
1305 (t t))))))
1306 (if (and (eq braceassignp 'dontknow)
1307 (/= (c-backward-token-1 1 t lim) 0))
1308 (setq braceassignp nil)))
1309 (if (not braceassignp)
1310 (if (eq (char-after) ?\;)
1311 ;; Brace lists can't contain a semicolon, so we're done.
1312 (setq containing-sexp nil)
1313 ;; lets see if we're nested. find the most nested
1314 ;; containing brace
1315 (setq containing-sexp (car brace-state)
1316 brace-state (cdr brace-state)))
1317 ;; we've hit the beginning of the aggregate list
1318 (c-beginning-of-statement-1
1319 (c-most-enclosing-brace brace-state))
1320 (setq bufpos (point))))
1321 ))
1322 bufpos))
1323 ))
1324
1325 (defun c-looking-at-special-brace-list (&optional lim)
1326 ;; If we're looking at the start of a pike-style list, ie `({ })',
1327 ;; `([ ])', `(< >)' etc, a cons of a cons of its starting and ending
1328 ;; positions and its entry in c-special-brace-lists is returned, nil
1329 ;; otherwise. The ending position is nil if the list is still open.
1330 ;; LIM is the limit for forward search. The point may either be at
1331 ;; the `(' or at the following paren character. Tries to check the
1332 ;; matching closer, but assumes it's correct if no balanced paren is
1333 ;; found (i.e. the case `({ ... } ... )' is detected as _not_ being
1334 ;; a special brace list).
1335 (if c-special-brace-lists
1336 (condition-case ()
1337 (save-excursion
1338 (let ((beg (point))
1339 end type)
1340 (c-forward-syntactic-ws)
1341 (if (eq (char-after) ?\()
1342 (progn
1343 (forward-char 1)
1344 (c-forward-syntactic-ws)
1345 (setq type (assq (char-after) c-special-brace-lists)))
1346 (if (setq type (assq (char-after) c-special-brace-lists))
1347 (progn
1348 (c-backward-syntactic-ws)
1349 (forward-char -1)
1350 (setq beg (if (eq (char-after) ?\()
1351 (point)
1352 nil)))))
1353 (if (and beg type)
1354 (if (and (c-safe (goto-char beg)
1355 (c-forward-sexp 1)
1356 (setq end (point))
1357 (= (char-before) ?\)))
1358 (c-safe (goto-char beg)
1359 (forward-char 1)
1360 (c-forward-sexp 1)
1361 ;; Kludges needed to handle inner
1362 ;; chars both with and without
1363 ;; paren syntax.
1364 (or (/= (char-syntax (char-before)) ?\))
1365 (= (char-before) (cdr type)))))
1366 (if (or (/= (char-syntax (char-before)) ?\))
1367 (= (progn
1368 (c-forward-syntactic-ws)
1369 (point))
1370 (1- end)))
1371 (cons (cons beg end) type))
1372 (cons (list beg) type)))))
1373 (error nil))))
1374
1375 (defun c-looking-at-bos ()
1376 ;; Returns nil if inside a statement or declaration.
1377 (save-excursion
1378 (c-backward-syntactic-ws)
1379 (or (bobp)
1380 (memq (char-before) '(?\; ?}))
1381 (and (eq (char-before) ?{)
1382 (not (and c-special-brace-lists
1383 (progn (backward-char)
1384 (c-looking-at-special-brace-list))))))))
1385
1386 (defun c-looking-at-inexpr-block (&optional lim)
1387 ;; Returns non-nil if we're looking at the beginning of a block
1388 ;; inside an expression. The value returned is actually a cons of
1389 ;; either 'inlambda, 'inexpr-statement or 'inexpr-class and the
1390 ;; position of the beginning of the construct. LIM limits the
1391 ;; backward search.
1392 (save-excursion
1393 (or lim (setq lim (point-min)))
1394 (let ((block-follows (eq (char-after) ?{)))
1395 ;; Look at the character after point only as a last resort when
1396 ;; we can't disambiguate.
1397 (if (and block-follows
1398 (progn (c-backward-syntactic-ws) (> (point) lim))
1399 (eq (char-before) ?\()
1400 (not (and c-special-brace-lists
1401 (c-looking-at-special-brace-list))))
1402 (cons 'inexpr-statement (point))
1403 (let (res)
1404 (while (and (not res)
1405 (= (c-backward-token-1 1 t lim) 0)
1406 (>= (point) lim)
1407 (looking-at "(\\|\\w\\|\\s_\\|\\."))
1408 (setq res
1409 (cond ((and block-follows
1410 c-inexpr-class-key
1411 (looking-at c-inexpr-class-key)
1412 (or (not (looking-at c-class-key))
1413 (let ((prev (point)))
1414 (while (and (= (c-backward-token-1 1 t lim)
1415 0)
1416 (>= (point) lim)
1417 (eq (char-syntax (char-after))
1418 ?w))
1419 (setq prev (point)))
1420 (goto-char prev)
1421 (not (c-looking-at-bos)))))
1422 (cons 'inexpr-class (point)))
1423 ((and c-inexpr-block-key
1424 (looking-at c-inexpr-block-key))
1425 (cons 'inexpr-statement (point)))
1426 ((and c-lambda-key
1427 (looking-at c-lambda-key))
1428 (cons 'inlambda (point))))))
1429 res)))))
1430
1431 (defun c-looking-at-inexpr-block-backward (&optional lim)
1432 ;; Returns non-nil if we're looking at the end of an in-expression
1433 ;; block, otherwise the same as `c-looking-at-inexpr-block'.
1434 (save-excursion
1435 (let ((lim (or lim (c-point 'bod))))
1436 (c-safe
1437 (c-backward-syntactic-ws lim)
1438 (if (eq (char-before) ?}) ; Recognize only a block currently.
1439 (progn
1440 (c-forward-sexp -1)
1441 (if (>= (point) lim)
1442 (c-looking-at-inexpr-block lim))))))))
1443
1444 (defun c-on-identifier ()
1445 ;; Returns non-nil if we're on or directly after an identifier.
1446 (if (or (memq (char-syntax (or (char-after) ? )) '(?w ?_))
1447 (memq (char-syntax (or (char-before) ? )) '(?w ?_)))
1448 (save-excursion
1449 (skip-syntax-backward "w_")
1450 (not (looking-at c-keywords)))
1451 (if (c-major-mode-is 'pike-mode)
1452 ;; Handle the `<operator> syntax in Pike.
1453 (save-excursion
1454 (if (eq (char-after) ?\`) (forward-char))
1455 (skip-chars-backward "!%&*+\\-/<=>^|~")
1456 (let ((pos (point)))
1457 (cond ((memq (char-before) '(?\) ?\]))
1458 (c-safe (backward-char 2)))
1459 ((memq (char-before) '(?\( ?\[))
1460 (c-safe (backward-char 1))))
1461 (if (not (looking-at "()\\|\\[]"))
1462 (goto-char pos)))
1463 (and (eq (char-before) ?\`)
1464 (looking-at "[-!%&*+/<=>^|~]\\|()\\|\\[]"))))))
1465
1466 \f
1467 (defun c-most-enclosing-brace (state)
1468 ;; return the bufpos of the most enclosing brace that hasn't been
1469 ;; narrowed out by any enclosing class, or nil if none was found
1470 (let (enclosingp)
1471 (while (and state (not enclosingp))
1472 (setq enclosingp (car state)
1473 state (cdr state))
1474 (if (consp enclosingp)
1475 (setq enclosingp nil)
1476 (if (> (point-min) enclosingp)
1477 (setq enclosingp nil))
1478 (setq state nil)))
1479 enclosingp))
1480
1481 (defun c-least-enclosing-brace (state)
1482 ;; return the bufpos of the least (highest) enclosing brace that
1483 ;; hasn't been narrowed out by any enclosing class, or nil if none
1484 ;; was found.
1485 (c-most-enclosing-brace (nreverse state)))
1486
1487 (defun c-safe-position (bufpos state)
1488 ;; return the closest known safe position higher up than point
1489 (let ((safepos nil))
1490 (while state
1491 (setq safepos
1492 (if (consp (car state))
1493 (cdr (car state))
1494 (car state)))
1495 (if (< safepos bufpos)
1496 (setq state nil)
1497 (setq state (cdr state))))
1498 safepos))
1499
1500 (defun c-narrow-out-enclosing-class (state lim)
1501 ;; narrow the buffer so that the enclosing class is hidden
1502 (setq state (c-whack-state (point) state))
1503 (let (inclass-p)
1504 (and state
1505 (setq inclass-p (c-search-uplist-for-classkey state))
1506 (narrow-to-region
1507 (progn
1508 (goto-char (1+ (aref inclass-p 1)))
1509 (skip-chars-forward " \t\n" lim)
1510 ;; if point is now left of the class opening brace, we're
1511 ;; hosed, so try a different tact
1512 (if (<= (point) (aref inclass-p 1))
1513 (progn
1514 (goto-char (1+ (aref inclass-p 1)))
1515 (c-forward-syntactic-ws lim)))
1516 (point))
1517 ;; end point is the end of the current line
1518 (progn
1519 (goto-char lim)
1520 (c-point 'eol))))
1521 ;; return the class vector
1522 inclass-p))
1523
1524 \f
1525 ;; This function implements the main decision tree for determining the
1526 ;; syntactic analysis of the current line of code. Yes, it's huge and
1527 ;; bloated!
1528
1529 (defun c-guess-basic-syntax ()
1530 (save-excursion
1531 (save-restriction
1532 (beginning-of-line)
1533 (let* ((indent-point (point))
1534 (case-fold-search nil)
1535 (fullstate (c-parse-state))
1536 (state fullstate)
1537 literal containing-sexp char-before-ip char-after-ip lim
1538 syntax placeholder c-in-literal-cache inswitch-p
1539 tmpsymbol keyword injava-inher special-brace-list
1540 ;; narrow out any enclosing class or extern "C" block
1541 (inclass-p (c-narrow-out-enclosing-class state indent-point))
1542 inenclosing-p)
1543 ;; check for meta top-level enclosing constructs, possible
1544 ;; extern language definitions, possibly (in C++) namespace
1545 ;; definitions.
1546 (save-excursion
1547 (save-restriction
1548 (widen)
1549 (if (and inclass-p
1550 (progn
1551 (goto-char (aref inclass-p 0))
1552 (looking-at (concat c-extra-toplevel-key "[^_]"))))
1553 (let ((enclosing (match-string 1)))
1554 (cond
1555 ((string-equal enclosing "extern")
1556 (setq inenclosing-p 'extern))
1557 ((string-equal enclosing "namespace")
1558 (setq inenclosing-p 'namespace))
1559 )))))
1560 ;; get the buffer position of the most nested opening brace,
1561 ;; if there is one, and it hasn't been narrowed out
1562 (save-excursion
1563 (goto-char indent-point)
1564 (skip-chars-forward " \t}")
1565 (skip-chars-backward " \t")
1566 (while (and state
1567 (not containing-sexp))
1568 (setq containing-sexp (car state)
1569 state (cdr state))
1570 (if (consp containing-sexp)
1571 ;; if cdr == point, then containing sexp is the brace
1572 ;; that opens the sexp we close
1573 (if (= (cdr containing-sexp) (point))
1574 (setq containing-sexp (car containing-sexp))
1575 ;; otherwise, ignore this element
1576 (setq containing-sexp nil))
1577 ;; ignore the bufpos if its been narrowed out by the
1578 ;; containing class or does not contain the indent point
1579 (if (or (<= containing-sexp (point-min))
1580 (>= containing-sexp indent-point))
1581 (setq containing-sexp nil)))))
1582
1583 ;; set the limit on the farthest back we need to search
1584 (setq lim (or containing-sexp
1585 (if (consp (car fullstate))
1586 (cdr (car fullstate))
1587 nil)
1588 (point-min)))
1589
1590 ;; cache char before and after indent point, and move point to
1591 ;; the most likely position to perform the majority of tests
1592 (goto-char indent-point)
1593 (skip-chars-forward " \t")
1594 (setq char-after-ip (char-after))
1595 (c-backward-syntactic-ws lim)
1596 (setq char-before-ip (char-before))
1597 (goto-char indent-point)
1598 (skip-chars-forward " \t")
1599
1600 ;; are we in a literal?
1601 (setq literal (c-in-literal lim))
1602
1603 ;; now figure out syntactic qualities of the current line
1604 (cond
1605 ;; CASE 1: in a string.
1606 ((memq literal '(string))
1607 (c-add-syntax 'string (c-point 'bopl)))
1608 ;; CASE 2: in a C or C++ style comment.
1609 ((memq literal '(c c++))
1610 (c-add-syntax literal (car (c-literal-limits lim))))
1611 ;; CASE 3: in a cpp preprocessor macro continuation.
1612 ((and (eq literal 'pound)
1613 (/= (save-excursion
1614 (c-beginning-of-macro lim)
1615 (setq placeholder (point)))
1616 (c-point 'boi)))
1617 (c-add-syntax 'cpp-macro-cont placeholder))
1618 ;; CASE 4: In-expression statement.
1619 ((and (or c-inexpr-class-key c-inexpr-block-key c-lambda-key)
1620 (setq placeholder (c-looking-at-inexpr-block)))
1621 (setq tmpsymbol (assq (car placeholder)
1622 '((inexpr-class . class-open)
1623 (inexpr-statement . block-open))))
1624 (if tmpsymbol
1625 ;; It's a statement block or an anonymous class.
1626 (setq tmpsymbol (cdr tmpsymbol))
1627 ;; It's a Pike lambda. Check whether we are between the
1628 ;; lambda keyword and the argument list or at the defun
1629 ;; opener.
1630 (setq tmpsymbol (if (eq char-after-ip ?{)
1631 'inline-open
1632 'lambda-intro-cont)))
1633 (goto-char (cdr placeholder))
1634 (back-to-indentation)
1635 (c-add-syntax tmpsymbol (point))
1636 (unless (eq (point) (cdr placeholder))
1637 (c-add-syntax (car placeholder))))
1638 ;; CASE 5: Line is at top level.
1639 ((null containing-sexp)
1640 (cond
1641 ;; CASE 5A: we are looking at a defun, brace list, class,
1642 ;; or inline-inclass method opening brace
1643 ((setq special-brace-list
1644 (or (and c-special-brace-lists
1645 (c-looking-at-special-brace-list))
1646 (eq char-after-ip ?{)))
1647 (cond
1648 ;; CASE 5A.1: extern language or namespace construct
1649 ((save-excursion
1650 (goto-char indent-point)
1651 (skip-chars-forward " \t")
1652 (and (c-safe (progn (c-backward-sexp 2) t))
1653 (looking-at (concat c-extra-toplevel-key "[^_]"))
1654 (setq keyword (match-string 1)
1655 placeholder (point))
1656 (or (and (string-equal keyword "namespace")
1657 (setq tmpsymbol 'namespace-open))
1658 (and (string-equal keyword "extern")
1659 (progn
1660 (c-forward-sexp 1)
1661 (c-forward-syntactic-ws)
1662 (eq (char-after) ?\"))
1663 (setq tmpsymbol 'extern-lang-open)))
1664 ))
1665 (goto-char placeholder)
1666 (c-add-syntax tmpsymbol (c-point 'boi)))
1667 ;; CASE 5A.2: we are looking at a class opening brace
1668 ((save-excursion
1669 (goto-char indent-point)
1670 (skip-chars-forward " \t{")
1671 ;; TBD: watch out! there could be a bogus
1672 ;; c-state-cache in place when we get here. we have
1673 ;; to go through much chicanery to ignore the cache.
1674 ;; But of course, there may not be! BLECH! BOGUS!
1675 (let ((decl
1676 (let ((c-state-cache nil))
1677 (c-search-uplist-for-classkey (c-parse-state))
1678 )))
1679 (and decl
1680 (setq placeholder (aref decl 0)))
1681 ))
1682 (c-add-syntax 'class-open placeholder))
1683 ;; CASE 5A.3: brace list open
1684 ((save-excursion
1685 (c-beginning-of-statement-1 lim)
1686 ;; c-b-o-s could have left us at point-min
1687 (and (bobp)
1688 (c-forward-syntactic-ws indent-point))
1689 (if (looking-at "typedef[^_]")
1690 (progn (c-forward-sexp 1)
1691 (c-forward-syntactic-ws indent-point)))
1692 (setq placeholder (c-point 'boi))
1693 (or (consp special-brace-list)
1694 (and (or (save-excursion
1695 (goto-char indent-point)
1696 (setq tmpsymbol nil)
1697 (while (and (> (point) placeholder)
1698 (= (c-backward-token-1 1 t) 0)
1699 (/= (char-after) ?=))
1700 (if (and (not tmpsymbol)
1701 (looking-at "new\\>[^_]"))
1702 (setq tmpsymbol 'topmost-intro-cont)))
1703 (eq (char-after) ?=))
1704 (looking-at "enum[ \t\n]+"))
1705 (save-excursion
1706 (while (and (< (point) indent-point)
1707 (= (c-forward-token-1 1 t) 0)
1708 (not (memq (char-after) '(?\; ?\()))))
1709 (not (memq (char-after) '(?\; ?\()))
1710 ))))
1711 (if (and (c-major-mode-is 'java-mode)
1712 (eq tmpsymbol 'topmost-intro-cont))
1713 ;; We're in Java and have found that the open brace
1714 ;; belongs to a "new Foo[]" initialization list,
1715 ;; which means the brace list is part of an
1716 ;; expression and not a top level definition. We
1717 ;; therefore treat it as any topmost continuation
1718 ;; even though the semantically correct symbol still
1719 ;; is brace-list-open, on the same grounds as in
1720 ;; case 10B.2.
1721 (progn
1722 (c-beginning-of-statement-1 lim)
1723 (c-forward-syntactic-ws)
1724 (c-add-syntax 'topmost-intro-cont (c-point 'boi)))
1725 (c-add-syntax 'brace-list-open placeholder)))
1726 ;; CASE 5A.4: inline defun open
1727 ((and inclass-p (not inenclosing-p))
1728 (c-add-syntax 'inline-open)
1729 (c-add-class-syntax 'inclass inclass-p))
1730 ;; CASE 5A.5: ordinary defun open
1731 (t
1732 (goto-char placeholder)
1733 (if inclass-p
1734 (c-add-syntax 'defun-open (c-point 'boi))
1735 (c-add-syntax 'defun-open (c-point 'bol)))
1736 )))
1737 ;; CASE 5B: first K&R arg decl or member init
1738 ((c-just-after-func-arglist-p)
1739 (cond
1740 ;; CASE 5B.1: a member init
1741 ((or (eq char-before-ip ?:)
1742 (eq char-after-ip ?:))
1743 ;; this line should be indented relative to the beginning
1744 ;; of indentation for the topmost-intro line that contains
1745 ;; the prototype's open paren
1746 ;; TBD: is the following redundant?
1747 (if (eq char-before-ip ?:)
1748 (forward-char -1))
1749 (c-backward-syntactic-ws lim)
1750 ;; TBD: is the preceding redundant?
1751 (if (eq (char-before) ?:)
1752 (progn (forward-char -1)
1753 (c-backward-syntactic-ws lim)))
1754 (if (eq (char-before) ?\))
1755 (c-backward-sexp 1))
1756 (setq placeholder (point))
1757 (save-excursion
1758 (and (c-safe (c-backward-sexp 1) t)
1759 (looking-at "throw[^_]")
1760 (c-safe (c-backward-sexp 1) t)
1761 (setq placeholder (point))))
1762 (goto-char placeholder)
1763 (c-add-syntax 'member-init-intro (c-point 'boi))
1764 ;; we don't need to add any class offset since this
1765 ;; should be relative to the ctor's indentation
1766 )
1767 ;; CASE 5B.2: K&R arg decl intro
1768 (c-recognize-knr-p
1769 (c-add-syntax 'knr-argdecl-intro (c-point 'boi))
1770 (if inclass-p (c-add-class-syntax 'inclass inclass-p)))
1771 ;; CASE 5B.3: Inside a member init list.
1772 ((c-beginning-of-member-init-list lim)
1773 (c-forward-syntactic-ws)
1774 (c-add-syntax 'member-init-cont (point)))
1775 ;; CASE 5B.4: Nether region after a C++ or Java func
1776 ;; decl, which could include a `throws' declaration.
1777 (t
1778 (c-beginning-of-statement-1 lim)
1779 (c-add-syntax 'func-decl-cont (c-point 'boi))
1780 )))
1781 ;; CASE 5C: inheritance line. could be first inheritance
1782 ;; line, or continuation of a multiple inheritance
1783 ((or (and c-baseclass-key
1784 (progn
1785 (when (eq char-after-ip ?,)
1786 (skip-chars-forward " \t")
1787 (forward-char))
1788 (looking-at c-baseclass-key)))
1789 (and (or (eq char-before-ip ?:)
1790 ;; watch out for scope operator
1791 (save-excursion
1792 (and (eq char-after-ip ?:)
1793 (c-safe (progn (forward-char 1) t))
1794 (not (eq (char-after) ?:))
1795 )))
1796 (save-excursion
1797 (c-backward-syntactic-ws lim)
1798 (if (eq char-before-ip ?:)
1799 (progn
1800 (forward-char -1)
1801 (c-backward-syntactic-ws lim)))
1802 (back-to-indentation)
1803 (looking-at c-class-key)))
1804 ;; for Java
1805 (and (c-major-mode-is 'java-mode)
1806 (let ((fence (save-excursion
1807 (c-beginning-of-statement-1 lim)
1808 (point)))
1809 cont done)
1810 (save-excursion
1811 (while (not done)
1812 (cond ((looking-at c-Java-special-key)
1813 (setq injava-inher (cons cont (point))
1814 done t))
1815 ((or (not (c-safe (c-forward-sexp -1) t))
1816 (<= (point) fence))
1817 (setq done t))
1818 )
1819 (setq cont t)))
1820 injava-inher)
1821 (not (c-crosses-statement-barrier-p (cdr injava-inher)
1822 (point)))
1823 ))
1824 (cond
1825 ;; CASE 5C.1: non-hanging colon on an inher intro
1826 ((eq char-after-ip ?:)
1827 (c-backward-syntactic-ws lim)
1828 (c-add-syntax 'inher-intro (c-point 'boi))
1829 ;; don't add inclass symbol since relative point already
1830 ;; contains any class offset
1831 )
1832 ;; CASE 5C.2: hanging colon on an inher intro
1833 ((eq char-before-ip ?:)
1834 (c-add-syntax 'inher-intro (c-point 'boi))
1835 (if inclass-p (c-add-class-syntax 'inclass inclass-p)))
1836 ;; CASE 5C.3: in a Java implements/extends
1837 (injava-inher
1838 (let ((where (cdr injava-inher))
1839 (cont (car injava-inher)))
1840 (goto-char where)
1841 (cond ((looking-at "throws[ \t\n]")
1842 (c-add-syntax 'func-decl-cont
1843 (progn (c-beginning-of-statement-1 lim)
1844 (c-point 'boi))))
1845 (cont (c-add-syntax 'inher-cont where))
1846 (t (c-add-syntax 'inher-intro
1847 (progn (goto-char (cdr injava-inher))
1848 (c-beginning-of-statement-1 lim)
1849 (point))))
1850 )))
1851 ;; CASE 5C.4: a continued inheritance line
1852 (t
1853 (c-beginning-of-inheritance-list lim)
1854 (c-add-syntax 'inher-cont (point))
1855 ;; don't add inclass symbol since relative point already
1856 ;; contains any class offset
1857 )))
1858 ;; CASE 5D: this could be a top-level compound statement, a
1859 ;; member init list continuation, or a template argument
1860 ;; list continuation.
1861 ((c-with-syntax-table (if (c-major-mode-is 'c++-mode)
1862 c++-template-syntax-table
1863 (syntax-table))
1864 (save-excursion
1865 (while (and (= (c-backward-token-1 1 t lim) 0)
1866 (not (looking-at "[;{<,]"))))
1867 (eq (char-after) ?,)))
1868 (goto-char indent-point)
1869 (c-beginning-of-member-init-list lim)
1870 (cond
1871 ;; CASE 5D.1: hanging member init colon, but watch out
1872 ;; for bogus matches on access specifiers inside classes.
1873 ((and (save-excursion
1874 (setq placeholder (point))
1875 (c-backward-token-1 1 t lim)
1876 (and (eq (char-after) ?:)
1877 (not (eq (char-before) ?:))))
1878 (save-excursion
1879 (goto-char placeholder)
1880 (back-to-indentation)
1881 (or
1882 (/= (car (save-excursion
1883 (parse-partial-sexp (point) placeholder)))
1884 0)
1885 (and
1886 (if c-access-key (not (looking-at c-access-key)) t)
1887 (not (looking-at c-class-key))
1888 (if c-bitfield-key (not (looking-at c-bitfield-key)) t))
1889 )))
1890 (goto-char placeholder)
1891 (c-forward-syntactic-ws)
1892 (c-add-syntax 'member-init-cont (point))
1893 ;; we do not need to add class offset since relative
1894 ;; point is the member init above us
1895 )
1896 ;; CASE 5D.2: non-hanging member init colon
1897 ((progn
1898 (c-forward-syntactic-ws indent-point)
1899 (eq (char-after) ?:))
1900 (skip-chars-forward " \t:")
1901 (c-add-syntax 'member-init-cont (point)))
1902 ;; CASE 5D.3: perhaps a multiple inheritance line?
1903 ((save-excursion
1904 (c-beginning-of-statement-1 lim)
1905 (setq placeholder (point))
1906 (looking-at c-inher-key))
1907 (goto-char placeholder)
1908 (c-add-syntax 'inher-cont (c-point 'boi)))
1909 ;; CASE 5D.4: perhaps a template list continuation?
1910 ((save-excursion
1911 (goto-char indent-point)
1912 (skip-chars-backward "^<" lim)
1913 ;; not sure if this is the right test, but it should
1914 ;; be fast and mostly accurate.
1915 (setq placeholder (point))
1916 (and (eq (char-before) ?<)
1917 (not (c-in-literal lim))))
1918 ;; we can probably indent it just like an arglist-cont
1919 (goto-char placeholder)
1920 (c-beginning-of-statement-1 lim)
1921 (c-add-syntax 'template-args-cont (c-point 'boi)))
1922 ;; CASE 5D.5: perhaps a top-level statement-cont
1923 (t
1924 (c-beginning-of-statement-1 lim)
1925 ;; skip over any access-specifiers
1926 (and inclass-p c-access-key
1927 (while (looking-at c-access-key)
1928 (forward-line 1)))
1929 ;; skip over comments, whitespace
1930 (c-forward-syntactic-ws indent-point)
1931 (c-add-syntax 'statement-cont (c-point 'boi)))
1932 ))
1933 ;; CASE 5E: we are looking at a access specifier
1934 ((and inclass-p
1935 c-access-key
1936 (looking-at c-access-key))
1937 (c-add-syntax 'access-label (c-point 'bonl))
1938 (c-add-class-syntax 'inclass inclass-p))
1939 ;; CASE 5F: extern-lang-close or namespace-close?
1940 ((and inenclosing-p
1941 (eq char-after-ip ?}))
1942 (setq tmpsymbol (if (eq inenclosing-p 'extern)
1943 'extern-lang-close
1944 'namespace-close))
1945 (c-add-syntax tmpsymbol (aref inclass-p 0)))
1946 ;; CASE 5G: we are looking at the brace which closes the
1947 ;; enclosing nested class decl
1948 ((and inclass-p
1949 (eq char-after-ip ?})
1950 (save-excursion
1951 (save-restriction
1952 (widen)
1953 (forward-char 1)
1954 (and (c-safe (progn (c-backward-sexp 1) t))
1955 (= (point) (aref inclass-p 1))
1956 ))))
1957 (c-add-class-syntax 'class-close inclass-p))
1958 ;; CASE 5H: we could be looking at subsequent knr-argdecls
1959 ((and c-recognize-knr-p
1960 ;; here we essentially use the hack that is used in
1961 ;; Emacs' c-mode.el to limit how far back we should
1962 ;; look. The assumption is made that argdecls are
1963 ;; indented at least one space and that function
1964 ;; headers are not indented.
1965 (let ((limit (save-excursion
1966 (re-search-backward "^[^ \^L\t\n#]" nil 'move)
1967 (point))))
1968 (save-excursion
1969 (c-backward-syntactic-ws limit)
1970 (setq placeholder (point))
1971 (while (and (memq (char-before) '(?\; ?,))
1972 (> (point) limit))
1973 (beginning-of-line)
1974 (setq placeholder (point))
1975 (c-backward-syntactic-ws limit))
1976 (and (eq (char-before) ?\))
1977 (or (not c-method-key)
1978 (progn
1979 (c-forward-sexp -1)
1980 (forward-char -1)
1981 (c-backward-syntactic-ws)
1982 (not (or (memq (char-before) '(?- ?+))
1983 ;; or a class category
1984 (progn
1985 (c-forward-sexp -2)
1986 (looking-at c-class-key))
1987 )))))
1988 ))
1989 (save-excursion
1990 (c-beginning-of-statement-1)
1991 (not (looking-at "typedef[ \t\n]+"))))
1992 (goto-char placeholder)
1993 (c-add-syntax 'knr-argdecl (c-point 'boi)))
1994 ;; CASE 5I: ObjC method definition.
1995 ((and c-method-key
1996 (looking-at c-method-key))
1997 (c-add-syntax 'objc-method-intro (c-point 'boi)))
1998 ;; CASE 5J: we are at the topmost level, make sure we skip
1999 ;; back past any access specifiers
2000 ((progn
2001 (c-backward-syntactic-ws lim)
2002 (while (and inclass-p
2003 c-access-key
2004 (not (bobp))
2005 (save-excursion
2006 (c-safe (progn (c-backward-sexp 1) t))
2007 (looking-at c-access-key)))
2008 (c-backward-sexp 1)
2009 (c-backward-syntactic-ws lim))
2010 (or (bobp)
2011 (memq (char-before) '(?\; ?\}))))
2012 ;; real beginning-of-line could be narrowed out due to
2013 ;; enclosure in a class block
2014 (save-restriction
2015 (widen)
2016 (c-add-syntax 'topmost-intro (c-point 'bol))
2017 (if inclass-p
2018 (progn
2019 (goto-char (aref inclass-p 1))
2020 (or (= (point) (c-point 'boi))
2021 (goto-char (aref inclass-p 0)))
2022 (cond
2023 ((eq inenclosing-p 'extern)
2024 (c-add-syntax 'inextern-lang (c-point 'boi)))
2025 ((eq inenclosing-p 'namespace)
2026 (c-add-syntax 'innamespace (c-point 'boi)))
2027 (t (c-add-class-syntax 'inclass inclass-p)))
2028 ))
2029 ))
2030 ;; CASE 5K: we are at an ObjC or Java method definition
2031 ;; continuation line.
2032 ((and c-method-key
2033 (progn
2034 (c-beginning-of-statement-1 lim)
2035 (beginning-of-line)
2036 (looking-at c-method-key)))
2037 (c-add-syntax 'objc-method-args-cont (point)))
2038 ;; CASE 5L: we are at the first argument of a template
2039 ;; arglist that begins on the previous line.
2040 ((eq (char-before) ?<)
2041 (c-beginning-of-statement-1 lim)
2042 (c-forward-syntactic-ws)
2043 (c-add-syntax 'template-args-cont (c-point 'boi)))
2044 ;; CASE 5M: we are at a topmost continuation line
2045 (t
2046 (c-beginning-of-statement-1 lim)
2047 (c-forward-syntactic-ws)
2048 (c-add-syntax 'topmost-intro-cont (c-point 'boi)))
2049 )) ; end CASE 5
2050 ;; (CASE 6 has been removed.)
2051 ;; CASE 7: line is an expression, not a statement. Most
2052 ;; likely we are either in a function prototype or a function
2053 ;; call argument list
2054 ((not (or (and c-special-brace-lists
2055 (save-excursion
2056 (goto-char containing-sexp)
2057 (c-looking-at-special-brace-list)))
2058 (eq (char-after containing-sexp) ?{)))
2059 (c-backward-syntactic-ws containing-sexp)
2060 (cond
2061 ;; CASE 7A: we are looking at the arglist closing paren
2062 ((and (or (c-major-mode-is 'pike-mode)
2063 ;; Don't check this in Pike since it allows a
2064 ;; comma after the last arg.
2065 (not (eq char-before-ip ?,)))
2066 (memq char-after-ip '(?\) ?\])))
2067 (goto-char containing-sexp)
2068 (setq placeholder (c-point 'boi))
2069 (when (and (c-safe (backward-up-list 1) t)
2070 (> (point) placeholder))
2071 (forward-char)
2072 (skip-chars-forward " \t")
2073 (setq placeholder (point)))
2074 (c-add-syntax 'arglist-close placeholder))
2075 ;; CASE 7B: Looking at the opening brace of an
2076 ;; in-expression block or brace list.
2077 ((eq char-after-ip ?{)
2078 (goto-char indent-point)
2079 (setq placeholder (c-point 'boi))
2080 (goto-char containing-sexp)
2081 (if (c-inside-bracelist-p placeholder
2082 (cons containing-sexp state))
2083 (progn
2084 (c-add-syntax 'brace-list-open (c-point 'boi))
2085 (c-add-syntax 'inexpr-class))
2086 (c-add-syntax 'block-open (c-point 'boi))
2087 (c-add-syntax 'inexpr-statement)))
2088 ;; CASE 7C: we are looking at the first argument in an empty
2089 ;; argument list. Use arglist-close if we're actually
2090 ;; looking at a close paren or bracket.
2091 ((memq char-before-ip '(?\( ?\[))
2092 (goto-char containing-sexp)
2093 (setq placeholder (c-point 'boi))
2094 (when (and (c-safe (backward-up-list 1) t)
2095 (> (point) placeholder))
2096 (forward-char)
2097 (skip-chars-forward " \t")
2098 (setq placeholder (point)))
2099 (c-add-syntax 'arglist-intro placeholder))
2100 ;; CASE 7D: we are inside a conditional test clause. treat
2101 ;; these things as statements
2102 ((save-excursion
2103 (goto-char containing-sexp)
2104 (and (c-safe (progn (c-forward-sexp -1) t))
2105 (looking-at "\\<for\\>[^_]")))
2106 (goto-char (1+ containing-sexp))
2107 (c-forward-syntactic-ws indent-point)
2108 (c-beginning-of-statement-1 containing-sexp)
2109 (if (eq char-before-ip ?\;)
2110 (c-add-syntax 'statement (point))
2111 (c-add-syntax 'statement-cont (point))
2112 ))
2113 ;; CASE 7E: maybe a continued method call. This is the case
2114 ;; when we are inside a [] bracketed exp, and what precede
2115 ;; the opening bracket is not an identifier.
2116 ((and c-method-key
2117 (eq (char-after containing-sexp) ?\[)
2118 (save-excursion
2119 (goto-char (1- containing-sexp))
2120 (c-backward-syntactic-ws (c-point 'bod))
2121 (if (not (looking-at c-symbol-key))
2122 (c-add-syntax 'objc-method-call-cont containing-sexp))
2123 )))
2124 ;; CASE 7F: we are looking at an arglist continuation line,
2125 ;; but the preceding argument is on the same line as the
2126 ;; opening paren. This case includes multi-line
2127 ;; mathematical paren groupings, but we could be on a
2128 ;; for-list continuation line
2129 ((save-excursion
2130 (goto-char (1+ containing-sexp))
2131 (skip-chars-forward " \t")
2132 (not (eolp)))
2133 (goto-char containing-sexp)
2134 (setq placeholder (c-point 'boi))
2135 (when (and (c-safe (backward-up-list 1) t)
2136 (> (point) placeholder))
2137 (forward-char)
2138 (skip-chars-forward " \t")
2139 (setq placeholder (point)))
2140 (c-add-syntax 'arglist-cont-nonempty placeholder))
2141 ;; CASE 7G: we are looking at just a normal arglist
2142 ;; continuation line
2143 (t (c-beginning-of-statement-1 containing-sexp)
2144 (forward-char 1)
2145 (c-forward-syntactic-ws indent-point)
2146 (c-add-syntax 'arglist-cont (c-point 'boi)))
2147 ))
2148 ;; CASE 8: func-local multi-inheritance line
2149 ((and c-baseclass-key
2150 (save-excursion
2151 (goto-char indent-point)
2152 (skip-chars-forward " \t")
2153 (looking-at c-baseclass-key)))
2154 (goto-char indent-point)
2155 (skip-chars-forward " \t")
2156 (cond
2157 ;; CASE 8A: non-hanging colon on an inher intro
2158 ((eq char-after-ip ?:)
2159 (c-backward-syntactic-ws lim)
2160 (c-add-syntax 'inher-intro (c-point 'boi)))
2161 ;; CASE 8B: hanging colon on an inher intro
2162 ((eq char-before-ip ?:)
2163 (c-add-syntax 'inher-intro (c-point 'boi)))
2164 ;; CASE 8C: a continued inheritance line
2165 (t
2166 (c-beginning-of-inheritance-list lim)
2167 (c-add-syntax 'inher-cont (point))
2168 )))
2169 ;; CASE 9: we are inside a brace-list
2170 ((setq special-brace-list
2171 (or (and c-special-brace-lists
2172 (save-excursion
2173 (goto-char containing-sexp)
2174 (c-looking-at-special-brace-list)))
2175 (c-inside-bracelist-p containing-sexp state)))
2176 (cond
2177 ;; CASE 9A: In the middle of a special brace list opener.
2178 ((and (consp special-brace-list)
2179 (save-excursion
2180 (goto-char containing-sexp)
2181 (eq (char-after) ?\())
2182 (eq char-after-ip (car (cdr special-brace-list))))
2183 (goto-char (car (car special-brace-list)))
2184 (skip-chars-backward " \t")
2185 (if (and (bolp)
2186 (assoc 'statement-cont
2187 (setq placeholder (c-guess-basic-syntax))))
2188 (setq syntax placeholder)
2189 (c-beginning-of-statement-1 lim)
2190 (c-forward-token-1 0)
2191 (if (looking-at "typedef\\>") (c-forward-token-1 1))
2192 (c-add-syntax 'brace-list-open (c-point 'boi))))
2193 ;; CASE 9B: brace-list-close brace
2194 ((if (consp special-brace-list)
2195 ;; Check special brace list closer.
2196 (progn
2197 (goto-char (car (car special-brace-list)))
2198 (save-excursion
2199 (goto-char indent-point)
2200 (back-to-indentation)
2201 (or
2202 ;; We were between the special close char and the `)'.
2203 (and (eq (char-after) ?\))
2204 (eq (1+ (point)) (cdr (car special-brace-list))))
2205 ;; We were before the special close char.
2206 (and (eq (char-after) (cdr (cdr special-brace-list)))
2207 (= (c-forward-token-1) 0)
2208 (eq (1+ (point)) (cdr (car special-brace-list)))))))
2209 ;; Normal brace list check.
2210 (and (eq char-after-ip ?})
2211 (c-safe (progn (forward-char 1)
2212 (c-backward-sexp 1)
2213 t))
2214 (= (point) containing-sexp)))
2215 (c-add-syntax 'brace-list-close (c-point 'boi)))
2216 (t
2217 ;; Prepare for the rest of the cases below by going to the
2218 ;; token following the opening brace
2219 (if (consp special-brace-list)
2220 (progn
2221 (goto-char (car (car special-brace-list)))
2222 (c-forward-token-1 1 nil indent-point))
2223 (goto-char containing-sexp))
2224 (forward-char)
2225 (let ((start (point)))
2226 (c-forward-syntactic-ws indent-point)
2227 (goto-char (max start (c-point 'bol))))
2228 (skip-chars-forward " \t\n\r" indent-point)
2229 (cond
2230 ;; CASE 9C: we're looking at the first line in a brace-list
2231 ((= (point) indent-point)
2232 (goto-char containing-sexp)
2233 (c-add-syntax 'brace-list-intro (c-point 'boi))
2234 ) ; end CASE 9C
2235 ;; CASE 9D: this is just a later brace-list-entry or
2236 ;; brace-entry-open
2237 (t (if (or (eq char-after-ip ?{)
2238 (and c-special-brace-lists
2239 (save-excursion
2240 (goto-char indent-point)
2241 (c-forward-syntactic-ws (c-point 'eol))
2242 (c-looking-at-special-brace-list (point)))))
2243 (c-add-syntax 'brace-entry-open (point))
2244 (c-add-syntax 'brace-list-entry (point))
2245 )) ; end CASE 9D
2246 )))) ; end CASE 9
2247 ;; CASE 10: A continued statement
2248 ((and (not (memq char-before-ip '(?\; ?:)))
2249 (or (not (eq char-before-ip ?}))
2250 (c-looking-at-inexpr-block-backward containing-sexp))
2251 (> (point)
2252 (save-excursion
2253 (c-beginning-of-statement-1 containing-sexp)
2254 (c-forward-syntactic-ws)
2255 (setq placeholder (point))))
2256 (/= placeholder containing-sexp))
2257 (goto-char indent-point)
2258 (skip-chars-forward " \t")
2259 (let ((after-cond-placeholder
2260 (save-excursion
2261 (goto-char placeholder)
2262 (if (and c-conditional-key (looking-at c-conditional-key))
2263 (progn
2264 (c-safe (c-skip-conditional))
2265 (c-forward-syntactic-ws)
2266 (if (eq (char-after) ?\;)
2267 (progn
2268 (forward-char 1)
2269 (c-forward-syntactic-ws)))
2270 (point))
2271 nil))))
2272 (cond
2273 ;; CASE 10A: substatement
2274 ((and after-cond-placeholder
2275 (>= after-cond-placeholder indent-point))
2276 (goto-char placeholder)
2277 (if (eq char-after-ip ?{)
2278 (c-add-syntax 'substatement-open (c-point 'boi))
2279 (c-add-syntax 'substatement (c-point 'boi))))
2280 ;; CASE 10B: open braces for class or brace-lists
2281 ((setq special-brace-list
2282 (or (and c-special-brace-lists
2283 (c-looking-at-special-brace-list))
2284 (eq char-after-ip ?{)))
2285 (cond
2286 ;; CASE 10B.1: class-open
2287 ((save-excursion
2288 (goto-char indent-point)
2289 (skip-chars-forward " \t{")
2290 (let ((decl (c-search-uplist-for-classkey (c-parse-state))))
2291 (and decl
2292 (setq placeholder (aref decl 0)))
2293 ))
2294 (c-add-syntax 'class-open placeholder))
2295 ;; CASE 10B.2: brace-list-open
2296 ((or (consp special-brace-list)
2297 (save-excursion
2298 (goto-char placeholder)
2299 (looking-at "\\<enum\\>"))
2300 (save-excursion
2301 (goto-char indent-point)
2302 (while (and (> (point) placeholder)
2303 (= (c-backward-token-1 1 t) 0)
2304 (/= (char-after) ?=)))
2305 (eq (char-after) ?=)))
2306 ;; The most semantically accurate symbol here is
2307 ;; brace-list-open, but we report it simply as a
2308 ;; statement-cont. The reason is that one normally
2309 ;; adjusts brace-list-open for brace lists as
2310 ;; top-level constructs, and brace lists inside
2311 ;; statements is a completely different context.
2312 (goto-char indent-point)
2313 (c-beginning-of-closest-statement)
2314 (c-add-syntax 'statement-cont (c-point 'boi)))
2315 ;; CASE 10B.3: The body of a function declared inside a
2316 ;; normal block. This can only occur in Pike.
2317 ((and (c-major-mode-is 'pike-mode)
2318 (progn
2319 (goto-char indent-point)
2320 (not (c-looking-at-bos))))
2321 (c-beginning-of-closest-statement)
2322 (c-add-syntax 'defun-open (c-point 'boi)))
2323 ;; CASE 10B.4: catch-all for unknown construct.
2324 (t
2325 ;; Can and should I add an extensibility hook here?
2326 ;; Something like c-recognize-hook so support for
2327 ;; unknown constructs could be added. It's probably a
2328 ;; losing proposition, so I dunno.
2329 (goto-char placeholder)
2330 (c-add-syntax 'statement-cont (c-point 'boi))
2331 (c-add-syntax 'block-open))
2332 ))
2333 ;; CASE 10C: iostream insertion or extraction operator
2334 ((looking-at "<<\\|>>")
2335 (goto-char placeholder)
2336 (and after-cond-placeholder
2337 (goto-char after-cond-placeholder))
2338 (while (and (re-search-forward "<<\\|>>" indent-point 'move)
2339 (c-in-literal placeholder)))
2340 ;; if we ended up at indent-point, then the first
2341 ;; streamop is on a separate line. Indent the line like
2342 ;; a statement-cont instead
2343 (if (/= (point) indent-point)
2344 (c-add-syntax 'stream-op (c-point 'boi))
2345 (c-backward-syntactic-ws lim)
2346 (c-add-syntax 'statement-cont (c-point 'boi))))
2347 ;; CASE 10D: continued statement. find the accurate
2348 ;; beginning of statement or substatement
2349 (t
2350 (c-beginning-of-statement-1 after-cond-placeholder)
2351 ;; KLUDGE ALERT! c-beginning-of-statement-1 can leave
2352 ;; us before the lim we're passing in. It should be
2353 ;; fixed, but I'm worried about side-effects at this
2354 ;; late date. Fix for v5.
2355 (goto-char (or (and after-cond-placeholder
2356 (max after-cond-placeholder (point)))
2357 (point)))
2358 (c-add-syntax 'statement-cont (point)))
2359 )))
2360 ;; CASE 11: an else clause?
2361 ((looking-at "\\<else\\>[^_]")
2362 (c-backward-to-start-of-if containing-sexp)
2363 (c-add-syntax 'else-clause (c-point 'boi)))
2364 ;; CASE 12: Statement. But what kind? Lets see if its a
2365 ;; while closure of a do/while construct
2366 ((progn
2367 (goto-char indent-point)
2368 (skip-chars-forward " \t")
2369 (and (looking-at "while\\b[^_]")
2370 (save-excursion
2371 (c-backward-to-start-of-do containing-sexp)
2372 (setq placeholder (point))
2373 (looking-at "do\\b[^_]"))
2374 ))
2375 (goto-char placeholder)
2376 (c-add-syntax 'do-while-closure (c-point 'boi)))
2377 ;; CASE 13: A catch or finally clause? This case is simpler
2378 ;; than if-else and do-while, because a block is required
2379 ;; after every try, catch and finally.
2380 ((save-excursion
2381 (and (cond ((c-major-mode-is 'c++-mode)
2382 (looking-at "\\<catch\\>[^_]"))
2383 ((c-major-mode-is 'java-mode)
2384 (looking-at "\\<\\(catch\\|finally\\)\\>[^_]")))
2385 (c-safe (c-backward-sexp) t)
2386 (eq (char-after) ?{)
2387 (c-safe (c-backward-sexp) t)
2388 (if (eq (char-after) ?\()
2389 (c-safe (c-backward-sexp) t)
2390 t)
2391 (looking-at "\\<\\(try\\|catch\\)\\>[^_]")
2392 (setq placeholder (c-point 'boi))))
2393 (c-add-syntax 'catch-clause placeholder))
2394 ;; CASE 14: A case or default label
2395 ((looking-at c-switch-label-key)
2396 (goto-char containing-sexp)
2397 ;; check for hanging braces
2398 (if (/= (point) (c-point 'boi))
2399 (c-forward-sexp -1))
2400 (c-add-syntax 'case-label (c-point 'boi)))
2401 ;; CASE 15: any other label
2402 ((looking-at c-label-key)
2403 (goto-char containing-sexp)
2404 ;; check for hanging braces
2405 (if (/= (point) (c-point 'boi))
2406 (c-forward-sexp -1))
2407 (c-add-syntax 'label (c-point 'boi)))
2408 ;; CASE 16: block close brace, possibly closing the defun or
2409 ;; the class
2410 ((eq char-after-ip ?})
2411 (let* ((lim (c-safe-position containing-sexp fullstate))
2412 (relpos (save-excursion
2413 (goto-char containing-sexp)
2414 (if (/= (point) (c-point 'boi))
2415 (c-beginning-of-statement-1 lim))
2416 (c-point 'boi))))
2417 (cond
2418 ;; CASE 16A: closing a lambda defun or an in-expression
2419 ;; block?
2420 ((save-excursion
2421 (goto-char containing-sexp)
2422 (setq placeholder (c-looking-at-inexpr-block)))
2423 (setq tmpsymbol (if (eq (car placeholder) 'inlambda)
2424 'inline-close
2425 'block-close))
2426 (goto-char containing-sexp)
2427 (back-to-indentation)
2428 (if (= containing-sexp (point))
2429 (c-add-syntax tmpsymbol (point))
2430 (goto-char (cdr placeholder))
2431 (back-to-indentation)
2432 (c-add-syntax tmpsymbol (point))
2433 (if (/= (point) (cdr placeholder))
2434 (c-add-syntax (car placeholder)))))
2435 ;; CASE 16B: does this close an inline or a function in
2436 ;; an extern block or namespace?
2437 ((progn
2438 (goto-char containing-sexp)
2439 (setq placeholder (c-search-uplist-for-classkey state)))
2440 (goto-char (aref placeholder 0))
2441 (if (looking-at (concat c-extra-toplevel-key "[^_]"))
2442 (c-add-syntax 'defun-close relpos)
2443 (c-add-syntax 'inline-close relpos)))
2444 ;; CASE 16C: if there an enclosing brace that hasn't
2445 ;; been narrowed out by a class, then this is a
2446 ;; block-close
2447 ((and (not inenclosing-p)
2448 (c-most-enclosing-brace state)
2449 (or (not (c-major-mode-is 'pike-mode))
2450 ;; In Pike it can be a defun-close of a
2451 ;; function declared in a statement block. Let
2452 ;; it through to be handled below.
2453 (or (c-looking-at-bos)
2454 (progn
2455 (c-beginning-of-statement-1)
2456 (looking-at c-conditional-key)))))
2457 (c-add-syntax 'block-close relpos))
2458 ;; CASE 16D: find out whether we're closing a top-level
2459 ;; class or a defun
2460 (t
2461 (save-restriction
2462 (narrow-to-region (point-min) indent-point)
2463 (let ((decl (c-search-uplist-for-classkey (c-parse-state))))
2464 (if decl
2465 (c-add-class-syntax 'class-close decl)
2466 (c-add-syntax 'defun-close relpos)))))
2467 )))
2468 ;; CASE 17: statement catchall
2469 (t
2470 ;; we know its a statement, but we need to find out if it is
2471 ;; the first statement in a block
2472 (goto-char containing-sexp)
2473 (forward-char 1)
2474 (c-forward-syntactic-ws indent-point)
2475 ;; now skip forward past any case/default clauses we might find.
2476 (while (or (c-skip-case-statement-forward fullstate indent-point)
2477 (and (looking-at c-switch-label-key)
2478 (not inswitch-p)))
2479 (setq inswitch-p t))
2480 ;; we want to ignore non-case labels when skipping forward
2481 (while (and (looking-at c-label-key)
2482 (goto-char (match-end 0)))
2483 (c-forward-syntactic-ws indent-point))
2484 (cond
2485 ;; CASE 17A: we are inside a case/default clause inside a
2486 ;; switch statement. find out if we are at the statement
2487 ;; just after the case/default label.
2488 ((and inswitch-p
2489 (progn
2490 (goto-char indent-point)
2491 (c-beginning-of-statement-1 containing-sexp)
2492 (setq placeholder (point))
2493 (beginning-of-line)
2494 (when (re-search-forward c-switch-label-key
2495 (max placeholder (c-point 'eol)) t)
2496 (setq placeholder (match-beginning 0)))))
2497 (goto-char indent-point)
2498 (skip-chars-forward " \t")
2499 (if (eq (char-after) ?{)
2500 (c-add-syntax 'statement-case-open placeholder)
2501 (c-add-syntax 'statement-case-intro placeholder)))
2502 ;; CASE 17B: continued statement
2503 ((eq char-before-ip ?,)
2504 (goto-char indent-point)
2505 (c-beginning-of-closest-statement)
2506 (c-add-syntax 'statement-cont (c-point 'boi)))
2507 ;; CASE 17C: a question/colon construct? But make sure
2508 ;; what came before was not a label, and what comes after
2509 ;; is not a globally scoped function call!
2510 ((or (and (memq char-before-ip '(?: ??))
2511 (save-excursion
2512 (goto-char indent-point)
2513 (c-backward-syntactic-ws lim)
2514 (back-to-indentation)
2515 (not (looking-at c-label-key))))
2516 (and (memq char-after-ip '(?: ??))
2517 (save-excursion
2518 (goto-char indent-point)
2519 (skip-chars-forward " \t")
2520 ;; watch out for scope operator
2521 (not (looking-at "::")))))
2522 (goto-char indent-point)
2523 (c-beginning-of-closest-statement)
2524 (c-add-syntax 'statement-cont (c-point 'boi)))
2525 ;; CASE 17D: any old statement
2526 ((< (point) indent-point)
2527 (let ((safepos (c-most-enclosing-brace fullstate))
2528 relpos done)
2529 (goto-char indent-point)
2530 (c-beginning-of-statement-1 safepos)
2531 ;; It is possible we're on the brace that opens a nested
2532 ;; function.
2533 (if (and (eq (char-after) ?{)
2534 (save-excursion
2535 (c-backward-syntactic-ws safepos)
2536 (not (eq (char-before) ?\;))))
2537 (c-beginning-of-statement-1 safepos))
2538 (if (and inswitch-p
2539 (looking-at c-switch-label-key))
2540 (progn
2541 (goto-char (match-end 0))
2542 (c-forward-syntactic-ws)))
2543 (setq relpos (c-point 'boi))
2544 (while (and (not done)
2545 (<= safepos (point))
2546 (/= relpos (point)))
2547 (c-beginning-of-statement-1 safepos)
2548 (if (= relpos (c-point 'boi))
2549 (setq done t))
2550 (setq relpos (c-point 'boi)))
2551 (c-add-syntax 'statement relpos)
2552 (if (eq char-after-ip ?{)
2553 (c-add-syntax 'block-open))))
2554 ;; CASE 17E: first statement in an in-expression block
2555 ((setq placeholder
2556 (save-excursion
2557 (goto-char containing-sexp)
2558 (c-looking-at-inexpr-block)))
2559 (goto-char containing-sexp)
2560 (back-to-indentation)
2561 (let ((block-intro (if (eq (car placeholder) 'inlambda)
2562 'defun-block-intro
2563 'statement-block-intro)))
2564 (if (= containing-sexp (point))
2565 (c-add-syntax block-intro (point))
2566 (goto-char (cdr placeholder))
2567 (back-to-indentation)
2568 (c-add-syntax block-intro (point))
2569 (if (/= (point) (cdr placeholder))
2570 (c-add-syntax (car placeholder)))))
2571 (if (eq char-after-ip ?{)
2572 (c-add-syntax 'block-open)))
2573 ;; CASE 17F: first statement in an inline, or first
2574 ;; statement in a top-level defun. we can tell this is it
2575 ;; if there are no enclosing braces that haven't been
2576 ;; narrowed out by a class (i.e. don't use bod here!)
2577 ((save-excursion
2578 (save-restriction
2579 (widen)
2580 (goto-char containing-sexp)
2581 (c-narrow-out-enclosing-class state containing-sexp)
2582 (not (c-most-enclosing-brace state))))
2583 (goto-char containing-sexp)
2584 ;; if not at boi, then defun-opening braces are hung on
2585 ;; right side, so we need a different relpos
2586 (if (/= (point) (c-point 'boi))
2587 (progn
2588 (c-backward-syntactic-ws)
2589 (c-safe (c-forward-sexp (if (eq (char-before) ?\))
2590 -1 -2)))
2591 ;; looking at a Java throws clause following a
2592 ;; method's parameter list
2593 (c-beginning-of-statement-1)
2594 ))
2595 (c-add-syntax 'defun-block-intro (c-point 'boi)))
2596 ;; CASE 17G: First statement in a function declared inside
2597 ;; a normal block. This can only occur in Pike.
2598 ((and (c-major-mode-is 'pike-mode)
2599 (progn
2600 (goto-char containing-sexp)
2601 (and (not (c-looking-at-bos))
2602 (progn
2603 (c-beginning-of-statement-1)
2604 (not (looking-at c-conditional-key))))))
2605 (c-add-syntax 'defun-block-intro (c-point 'boi)))
2606 ;; CASE 17H: first statement in a block
2607 (t (goto-char containing-sexp)
2608 (if (/= (point) (c-point 'boi))
2609 (c-beginning-of-statement-1
2610 (if (= (point) lim)
2611 (c-safe-position (point) state) lim)))
2612 (c-add-syntax 'statement-block-intro (c-point 'boi))
2613 (if (eq char-after-ip ?{)
2614 (c-add-syntax 'block-open)))
2615 ))
2616 )
2617 ;; now we need to look at any modifiers
2618 (goto-char indent-point)
2619 (skip-chars-forward " \t")
2620 (cond
2621 ;; are we looking at a comment only line?
2622 ((and (looking-at c-comment-start-regexp)
2623 (/= (c-forward-token-1 0 nil (c-point 'eol)) 0))
2624 (c-add-syntax 'comment-intro))
2625 ;; we might want to give additional offset to friends (in C++).
2626 ((and (c-major-mode-is 'c++-mode)
2627 (looking-at c-C++-friend-key))
2628 (c-add-syntax 'friend))
2629 ;; Start of a preprocessor directive?
2630 ((and (eq literal 'pound)
2631 (= (save-excursion
2632 (c-beginning-of-macro lim)
2633 (setq placeholder (point)))
2634 (c-point 'boi))
2635 (not (and (c-major-mode-is 'pike-mode)
2636 (eq (char-after (1+ placeholder)) ?\"))))
2637 (c-add-syntax 'cpp-macro)))
2638 ;; return the syntax
2639 syntax))))
2640
2641 \f
2642 (defun c-echo-parsing-error (&optional quiet)
2643 (when (and c-parsing-error (not quiet))
2644 (message "%s" c-parsing-error)
2645 (ding))
2646 c-parsing-error)
2647
2648 (defun c-shift-line-indentation (shift-amt)
2649 (let ((pos (- (point-max) (point)))
2650 (col (current-indentation)))
2651 (if (zerop shift-amt)
2652 nil
2653 (delete-region (c-point 'bol) (c-point 'boi))
2654 (beginning-of-line)
2655 (indent-to (+ col shift-amt)))
2656 (if (< (point) (c-point 'boi))
2657 (back-to-indentation)
2658 ;; If initial point was within line's indentation, position after
2659 ;; the indentation. Else stay at same point in text.
2660 (if (> (- (point-max) pos) (point))
2661 (goto-char (- (point-max) pos))))))
2662
2663 (defun c-evaluate-offset (offset langelem symbol)
2664 ;; offset can be a number, a function, a variable, a list, or one of
2665 ;; the symbols + or -
2666 (cond
2667 ((eq offset '+) c-basic-offset)
2668 ((eq offset '-) (- c-basic-offset))
2669 ((eq offset '++) (* 2 c-basic-offset))
2670 ((eq offset '--) (* 2 (- c-basic-offset)))
2671 ((eq offset '*) (/ c-basic-offset 2))
2672 ((eq offset '/) (/ (- c-basic-offset) 2))
2673 ((numberp offset) offset)
2674 ((functionp offset) (c-evaluate-offset
2675 (funcall offset langelem) langelem symbol))
2676 ((vectorp offset) offset)
2677 ((null offset) nil)
2678 ((listp offset)
2679 (let (done)
2680 (while (and (not done) offset)
2681 (setq done (c-evaluate-offset (car offset) langelem symbol)
2682 offset (cdr offset)))
2683 (if (not done)
2684 (if c-strict-syntax-p
2685 (error "No offset found for syntactic symbol %s" symbol))
2686 done)))
2687 (t (symbol-value offset))
2688 ))
2689
2690 (defun c-get-offset (langelem)
2691 ;; Get offset from LANGELEM which is a cons cell of the form:
2692 ;; (SYMBOL . RELPOS). The symbol is matched against
2693 ;; c-offsets-alist and the offset found there is either returned,
2694 ;; or added to the indentation at RELPOS. If RELPOS is nil, then
2695 ;; the offset is simply returned.
2696 (let* ((symbol (car langelem))
2697 (relpos (cdr langelem))
2698 (match (assq symbol c-offsets-alist))
2699 (offset (cdr-safe match)))
2700 (if (not match)
2701 (if c-strict-syntax-p
2702 (error "No offset found for syntactic symbol %s" symbol)
2703 (setq offset 0
2704 relpos 0))
2705 (setq offset (c-evaluate-offset offset langelem symbol)))
2706 (if (vectorp offset)
2707 offset
2708 (+ (if (and relpos
2709 (< relpos (c-point 'bol)))
2710 (save-excursion
2711 (goto-char relpos)
2712 (current-column))
2713 0)
2714 (or (and (numberp offset) offset)
2715 (and (symbolp offset) (symbol-value offset))
2716 0)))
2717 ))
2718
2719 (defun c-get-syntactic-indentation (langelems)
2720 ;; Apply c-get-offset to a list of langelem cells to get the total
2721 ;; syntactic indentation. Special treatment is needed for vectors
2722 ;; containing absolute columns.
2723 (let ((indent 0))
2724 (catch 'done
2725 (while langelems
2726 (let ((res (c-get-offset (car langelems))))
2727 (if (vectorp res)
2728 (throw 'done (elt res 0))
2729 (setq indent (+ indent res)
2730 langelems (cdr langelems)))))
2731 indent)))
2732
2733 (defun c-indent-line (&optional syntax quiet)
2734 ;; Indent the current line according to the syntactic context, if
2735 ;; c-syntactic-indentation is non-nil. Optional SYNTAX is the
2736 ;; syntactic information for the current line. Be silent about
2737 ;; syntactic errors if the optional argument QUIET is non-nil.
2738 ;; Returns the amount of indentation change (in columns).
2739 (let (shift-amt)
2740 (if c-syntactic-indentation
2741 (setq c-parsing-error
2742 (or (let* ((c-parsing-error nil)
2743 (c-syntactic-context (or syntax
2744 c-syntactic-context
2745 (c-guess-basic-syntax)))
2746 (indent (c-get-syntactic-indentation c-syntactic-context)))
2747 (and (not (c-echo-parsing-error quiet))
2748 c-echo-syntactic-information-p
2749 (message "syntax: %s, indent: %d"
2750 c-syntactic-context indent))
2751 (setq shift-amt (- indent (current-indentation)))
2752 (c-shift-line-indentation shift-amt)
2753 (run-hooks 'c-special-indent-hook)
2754 c-parsing-error)
2755 c-parsing-error))
2756 (let ((indent 0))
2757 (save-excursion
2758 (while (and (= (forward-line -1) 0)
2759 (if (looking-at "\\s-*$")
2760 t
2761 (back-to-indentation)
2762 (setq indent (current-indentation))
2763 nil))))
2764 (setq shift-amt (- indent (current-indentation)))
2765 (c-shift-line-indentation shift-amt)))
2766 shift-amt))
2767
2768 (defun c-show-syntactic-information (arg)
2769 "Show syntactic information for current line.
2770 With universal argument, inserts the analysis as a comment on that line."
2771 (interactive "P")
2772 (let ((syntax (c-guess-basic-syntax)))
2773 (if (not (consp arg))
2774 (message "syntactic analysis: %s" syntax)
2775 (indent-for-comment)
2776 (insert (format "%s" syntax))
2777 ))
2778 (c-keep-region-active))
2779
2780 (defun c-syntactic-information-on-region (from to)
2781 "Inserts a comment with the syntactic analysis on every line in the region."
2782 (interactive "*r")
2783 (save-excursion
2784 (save-restriction
2785 (narrow-to-region from to)
2786 (goto-char (point-min))
2787 (while (not (eobp))
2788 (c-show-syntactic-information '(0))
2789 (forward-line)))))
2790
2791 \f
2792 (cc-provide 'cc-engine)
2793 ;;; cc-engine.el ends here