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