Initial revision
[emacs.git] / lisp / progmodes / cc-engine.el
blob3ac0ed52b741fd37443f735e583c2751f224f7cf
1 ;;; cc-engine.el --- core syntax guessing engine for CC mode
3 ;; Copyright (C) 1985,87,92,93,94,95,96,97 Free Software Foundation, Inc.
5 ;; Authors: 1992-1997 Barry A. Warsaw
6 ;; 1987 Dave Detlefs and Stewart Clamen
7 ;; 1985 Richard M. Stallman
8 ;; Maintainer: cc-mode-help@python.org
9 ;; Created: 22-Apr-1997 (split from cc-mode.el)
10 ;; Version: 5.12
11 ;; Keywords: c languages oop
13 ;; This file is part of GNU Emacs.
15 ;; GNU Emacs is free software; you can redistribute it and/or modify
16 ;; it under the terms of the GNU General Public License as published by
17 ;; the Free Software Foundation; either version 2, or (at your option)
18 ;; any later version.
20 ;; GNU Emacs is distributed in the hope that it will be useful,
21 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
22 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 ;; GNU General Public License for more details.
25 ;; You should have received a copy of the GNU General Public License
26 ;; along with GNU Emacs; see the file COPYING. If not, write to the
27 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
28 ;; Boston, MA 02111-1307, USA.
31 ;; WARNING: Be *exceptionally* careful about modifications to this
32 ;; function! Much of CC Mode depends on this Doing The Right Thing.
33 ;; If you break it you will be sorry.
35 (defun c-beginning-of-statement-1 (&optional lim)
36 ;; move to the start of the current statement, or the previous
37 ;; statement if already at the beginning of one.
38 (let ((firstp t)
39 (substmt-p t)
40 donep c-in-literal-cache
41 ;; KLUDGE ALERT: maybe-labelp is used to pass information
42 ;; between c-crosses-statement-barrier-p and
43 ;; c-beginning-of-statement-1. A better way should be
44 ;; implemented.
45 maybe-labelp saved
46 (last-begin (point)))
47 ;; first check for bare semicolon
48 (if (and (progn (c-backward-syntactic-ws lim)
49 (eq (char-before) ?\;))
50 (c-safe (progn (forward-char -1)
51 (setq saved (point))
52 t))
53 (progn (c-backward-syntactic-ws lim)
54 (memq (char-before) '(?\; ?{ ?} ?:)))
56 (setq last-begin saved)
57 (goto-char last-begin)
58 (while (not donep)
59 ;; stop at beginning of buffer
60 (if (bobp) (setq donep t)
61 ;; go backwards one balanced expression, but be careful of
62 ;; unbalanced paren being reached
63 (if (not (c-safe (progn (backward-sexp 1) t)))
64 (progn
65 (if firstp
66 (backward-up-list 1)
67 (goto-char last-begin))
68 ;; skip over any unary operators, or other special
69 ;; characters appearing at front of identifier
70 (save-excursion
71 (c-backward-syntactic-ws lim)
72 (skip-chars-backward "-+!*&:.~ \t\n")
73 (if (eq (char-before) ?\()
74 (setq last-begin (point))))
75 (goto-char last-begin)
76 (setq last-begin (point)
77 donep t)))
79 (setq maybe-labelp nil)
80 ;; see if we're in a literal. if not, then this bufpos may be
81 ;; a candidate for stopping
82 (cond
83 ;; CASE 0: did we hit the error condition above?
84 (donep)
85 ;; CASE 1: are we in a literal?
86 ((eq (c-in-literal lim) 'pound)
87 (beginning-of-line))
88 ;; CASE 2: some other kind of literal?
89 ((c-in-literal lim))
90 ;; CASE 3: are we looking at a conditional keyword?
91 ((or (looking-at c-conditional-key)
92 (and (eq (char-after) ?\()
93 (save-excursion
94 (forward-sexp 1)
95 (c-forward-syntactic-ws)
96 (not (eq (char-after) ?\;)))
97 (let ((here (point))
98 (foundp (progn
99 (c-backward-syntactic-ws lim)
100 (forward-word -1)
101 (and lim
102 (<= lim (point))
103 (not (c-in-literal lim))
104 (looking-at c-conditional-key)
105 ))))
106 ;; did we find a conditional?
107 (if (not foundp)
108 (goto-char here))
109 foundp)))
110 ;; are we in the middle of an else-if clause?
111 (if (save-excursion
112 (and (not substmt-p)
113 (c-safe (progn (forward-sexp -1) t))
114 (looking-at "\\<else\\>[ \t\n]+\\<if\\>")
115 (not (c-in-literal lim))))
116 (progn
117 (forward-sexp -1)
118 (c-backward-to-start-of-if lim)))
119 ;; are we sitting at an else clause, that we are not a
120 ;; substatement of?
121 (if (and (not substmt-p)
122 (looking-at "\\<else\\>[^_]"))
123 (c-backward-to-start-of-if lim))
124 ;; are we sitting at the while of a do-while?
125 (if (and (looking-at "\\<while\\>[^_]")
126 (c-backward-to-start-of-do lim))
127 (setq substmt-p nil))
128 (setq last-begin (point)
129 donep substmt-p))
130 ;; CASE 4: are we looking at a label?
131 ((looking-at c-label-key))
132 ;; CASE 5: is this the first time we're checking?
133 (firstp (setq firstp nil
134 substmt-p (not (c-crosses-statement-barrier-p
135 (point) last-begin))
136 last-begin (point)))
137 ;; CASE 6: have we crossed a statement barrier?
138 ((c-crosses-statement-barrier-p (point) last-begin)
139 (setq donep t))
140 ;; CASE 7: ignore labels
141 ((and maybe-labelp
142 (or (and c-access-key (looking-at c-access-key))
143 ;; with switch labels, we have to go back further
144 ;; to try to pick up the case or default
145 ;; keyword. Potential bogosity alert: we assume
146 ;; `case' or `default' is first thing on line
147 (let ((here (point)))
148 (beginning-of-line)
149 (c-forward-syntactic-ws)
150 (if (looking-at c-switch-label-key)
152 (goto-char here)
153 nil))
154 (looking-at c-label-key))))
155 ;; CASE 8: ObjC or Java method def
156 ((and c-method-key
157 (setq last-begin (c-in-method-def-p)))
158 (setq donep t))
159 ;; CASE 9: nothing special
160 (t (setq last-begin (point)))
161 ))))
162 (goto-char last-begin)
163 ;; we always do want to skip over non-whitespace modifier
164 ;; characters that didn't get skipped above
165 (skip-chars-backward "-+!*&:.~" (c-point 'boi))))
167 (defun c-end-of-statement-1 ()
168 (condition-case ()
169 (progn
170 (while (and (not (eobp))
171 (let ((beg (point)))
172 (forward-sexp 1)
173 (let ((end (point)))
174 (save-excursion
175 (goto-char beg)
176 (not (re-search-forward "[;{}]" end t)))))))
177 (re-search-backward "[;}]")
178 (forward-char 1))
179 (error
180 (let ((beg (point)))
181 (backward-up-list -1)
182 (let ((end (point)))
183 (goto-char beg)
184 (search-forward ";" end 'move))))))
188 (defun c-crosses-statement-barrier-p (from to)
189 ;; Does buffer positions FROM to TO cross a C statement boundary?
190 (let ((here (point))
191 (lim from)
192 crossedp)
193 (condition-case ()
194 (progn
195 (goto-char from)
196 (while (and (not crossedp)
197 (< (point) to))
198 (skip-chars-forward "^;{}:" to)
199 (if (not (c-in-literal lim))
200 (progn
201 (if (memq (char-after) '(?\; ?{ ?}))
202 (setq crossedp t)
203 (if (eq (char-after) ?:)
204 (setq maybe-labelp t))
205 (forward-char 1))
206 (setq lim (point)))
207 (forward-char 1))))
208 (error (setq crossedp nil)))
209 (goto-char here)
210 crossedp))
213 ;; Skipping of "syntactic whitespace", defined as lexical whitespace,
214 ;; C and C++ style comments, and preprocessor directives. Search no
215 ;; farther back or forward than optional LIM. If LIM is omitted,
216 ;; `beginning-of-defun' is used for backward skipping, point-max is
217 ;; used for forward skipping.
219 (defun c-forward-syntactic-ws (&optional lim)
220 ;; Forward skip of syntactic whitespace for Emacs 19.
221 (save-restriction
222 (let* ((lim (or lim (point-max)))
223 (here lim)
224 (hugenum (point-max)))
225 (narrow-to-region lim (point))
226 (while (/= here (point))
227 (setq here (point))
228 (forward-comment hugenum)
229 ;; skip preprocessor directives
230 (if (and (eq (char-after) ?#)
231 (= (c-point 'boi) (point)))
232 (end-of-line)
233 )))))
235 (defun c-backward-syntactic-ws (&optional lim)
236 ;; Backward skip over syntactic whitespace for Emacs 19.
237 (save-restriction
238 (let* ((lim (or lim (c-point 'bod)))
239 (here lim)
240 (hugenum (- (point-max))))
241 (if (< lim (point))
242 (progn
243 (narrow-to-region lim (point))
244 (while (/= here (point))
245 (setq here (point))
246 (forward-comment hugenum)
247 (if (eq (c-in-literal lim) 'pound)
248 (beginning-of-line))
253 ;; Return `c' if in a C-style comment, `c++' if in a C++ style
254 ;; comment, `string' if in a string literal, `pound' if on a
255 ;; preprocessor line, or nil if not in a comment at all. Optional LIM
256 ;; is used as the backward limit of the search. If omitted, or nil,
257 ;; `beginning-of-defun' is used."
259 (defun c-in-literal (&optional lim)
260 ;; Determine if point is in a C++ literal. we cache the last point
261 ;; calculated if the cache is enabled
262 (if (and (boundp 'c-in-literal-cache)
263 c-in-literal-cache
264 (= (point) (aref c-in-literal-cache 0)))
265 (aref c-in-literal-cache 1)
266 (let ((rtn (save-excursion
267 (let* ((lim (or lim (c-point 'bod)))
268 (here (point))
269 (state (parse-partial-sexp lim (point))))
270 (cond
271 ((nth 3 state) 'string)
272 ((nth 4 state) (if (nth 7 state) 'c++ 'c))
273 ((progn
274 (goto-char here)
275 (beginning-of-line)
276 (looking-at "[ \t]*#"))
277 'pound)
278 (t nil))))))
279 ;; cache this result if the cache is enabled
280 (and (boundp 'c-in-literal-cache)
281 (setq c-in-literal-cache (vector (point) rtn)))
282 rtn)))
285 ;; utilities for moving and querying around syntactic elements
286 (defvar c-parsing-error nil)
288 (defun c-parse-state ()
289 ;; Finds and records all open parens between some important point
290 ;; earlier in the file and point.
292 ;; if there's a state cache, return it
293 (setq c-parsing-error nil)
294 (if (boundp 'c-state-cache) c-state-cache
295 (let* (at-bob
296 (pos (save-excursion
297 ;; go back 2 bods, but ignore any bogus positions
298 ;; returned by beginning-of-defun (i.e. open paren
299 ;; in column zero)
300 (let ((cnt 2))
301 (while (not (or at-bob (zerop cnt)))
302 (beginning-of-defun)
303 (if (eq (char-after) ?\{)
304 (setq cnt (1- cnt)))
305 (if (bobp)
306 (setq at-bob t))))
307 (point)))
308 (here (save-excursion
309 ;;(skip-chars-forward " \t}")
310 (point)))
311 (last-bod pos) (last-pos pos)
312 placeholder state sexp-end)
313 ;; cache last bod position
314 (while (catch 'backup-bod
315 (setq state nil)
316 (while (and pos (< pos here))
317 (setq last-pos pos)
318 (if (and (setq pos (c-safe (scan-lists pos 1 -1)))
319 (<= pos here))
320 (progn
321 (setq sexp-end (c-safe (scan-sexps (1- pos) 1)))
322 (if (and sexp-end
323 (<= sexp-end here))
324 ;; we want to record both the start and end
325 ;; of this sexp, but we only want to record
326 ;; the last-most of any of them before here
327 (progn
328 (if (eq (char-after (1- pos)) ?\{)
329 (setq state (cons (cons (1- pos) sexp-end)
330 (if (consp (car state))
331 (cdr state)
332 state))))
333 (setq pos sexp-end))
334 ;; we're contained in this sexp so put pos on
335 ;; front of list
336 (setq state (cons (1- pos) state))))
337 ;; something bad happened. check to see if we
338 ;; crossed an unbalanced close brace. if so, we
339 ;; didn't really find the right `important bufpos'
340 ;; so lets back up and try again
341 (if (and (not pos) (not at-bob)
342 (setq placeholder
343 (c-safe (scan-lists last-pos 1 1)))
344 ;;(char-after (1- placeholder))
345 (<= placeholder here)
346 (eq (char-after (1- placeholder)) ?\}))
347 (while t
348 (setq last-bod (c-safe (scan-lists last-bod -1 1)))
349 (if (not last-bod)
350 (progn
351 ;; bogus, but what can we do here?
352 (setq c-parsing-error (1- placeholder))
353 (throw 'backup-bod nil))
354 (setq at-bob (= last-bod (point-min))
355 pos last-bod)
356 (if (= (char-after last-bod) ?\{)
357 (throw 'backup-bod t)))
358 )) ;end-if
359 )) ;end-while
360 nil))
361 state)))
363 (defun c-whack-state (bufpos state)
364 ;; whack off any state information that appears on STATE which lies
365 ;; after the bounds of BUFPOS.
366 (let (newstate car)
367 (while state
368 (setq car (car state)
369 state (cdr state))
370 (if (consp car)
371 ;; just check the car, because in a balanced brace
372 ;; expression, it must be impossible for the corresponding
373 ;; close brace to be before point, but the open brace to be
374 ;; after.
375 (if (<= bufpos (car car))
376 nil ; whack it off
377 ;; its possible that the open brace is before bufpos, but
378 ;; the close brace is after. In that case, convert this
379 ;; to a non-cons element.
380 (if (<= bufpos (cdr car))
381 (setq newstate (append newstate (list (car car))))
382 ;; we know that both the open and close braces are
383 ;; before bufpos, so we also know that everything else
384 ;; on state is before bufpos, so we can glom up the
385 ;; whole thing and exit.
386 (setq newstate (append newstate (list car) state)
387 state nil)))
388 (if (<= bufpos car)
389 nil ; whack it off
390 ;; it's before bufpos, so everything else should too
391 (setq newstate (append newstate (list car) state)
392 state nil))))
393 newstate))
395 (defun c-hack-state (bufpos which state)
396 ;; Using BUFPOS buffer position, and WHICH (must be 'open or
397 ;; 'close), hack the c-parse-state STATE and return the results.
398 (if (eq which 'open)
399 (let ((car (car state)))
400 (if (or (null car)
401 (consp car)
402 (/= bufpos car))
403 (cons bufpos state)
404 state))
405 (if (not (eq which 'close))
406 (error "c-hack-state, bad argument: %s" which))
407 ;; 'close brace
408 (let ((car (car state))
409 (cdr (cdr state)))
410 (if (consp car)
411 (setq car (car cdr)
412 cdr (cdr cdr)))
413 ;; TBD: is this test relevant???
414 (if (consp car)
415 state ;on error, don't change
416 ;; watch out for balanced expr already on cdr of list
417 (cons (cons car bufpos)
418 (if (consp (car cdr))
419 (cdr cdr) cdr))
420 ))))
422 (defun c-adjust-state (from to shift state)
423 ;; Adjust all points in state that lie in the region FROM..TO by
424 ;; SHIFT amount (as would be returned by c-indent-line).
425 (mapcar
426 (function
427 (lambda (e)
428 (if (consp e)
429 (let ((car (car e))
430 (cdr (cdr e)))
431 (if (and (<= from car) (< car to))
432 (setcar e (+ shift car)))
433 (if (and (<= from cdr) (< cdr to))
434 (setcdr e (+ shift cdr))))
435 (if (and (<= from e) (< e to))
436 (setq e (+ shift e))))
438 state))
441 (defun c-beginning-of-inheritance-list (&optional lim)
442 ;; Go to the first non-whitespace after the colon that starts a
443 ;; multiple inheritance introduction. Optional LIM is the farthest
444 ;; back we should search.
445 (let ((lim (or lim (c-point 'bod)))
446 (placeholder (progn
447 (back-to-indentation)
448 (point))))
449 (c-backward-syntactic-ws lim)
450 (while (and (> (point) lim)
451 (memq (char-before) '(?, ?:))
452 (progn
453 (beginning-of-line)
454 (setq placeholder (point))
455 (skip-chars-forward " \t")
456 (not (looking-at c-class-key))
458 (c-backward-syntactic-ws lim))
459 (goto-char placeholder)
460 (skip-chars-forward "^:" (c-point 'eol))))
462 (defun c-beginning-of-macro (&optional lim)
463 ;; Go to the beginning of the macro. Right now we don't support
464 ;; multi-line macros too well
465 (back-to-indentation))
467 (defun c-in-method-def-p ()
468 ;; Return nil if we aren't in a method definition, otherwise the
469 ;; position of the initial [+-].
470 (save-excursion
471 (beginning-of-line)
472 (and c-method-key
473 (looking-at c-method-key)
474 (point))
477 (defun c-just-after-func-arglist-p (&optional containing)
478 ;; Return t if we are between a function's argument list closing
479 ;; paren and its opening brace. Note that the list close brace
480 ;; could be followed by a "const" specifier or a member init hanging
481 ;; colon. Optional CONTAINING is position of containing s-exp open
482 ;; brace. If not supplied, point is used as search start.
483 (save-excursion
484 (c-backward-syntactic-ws)
485 (let ((checkpoint (or containing (point))))
486 (goto-char checkpoint)
487 ;; could be looking at const specifier
488 (if (and (eq (char-before) ?t)
489 (forward-word -1)
490 (looking-at "\\<const\\>"))
491 (c-backward-syntactic-ws)
492 ;; otherwise, we could be looking at a hanging member init
493 ;; colon
494 (goto-char checkpoint)
495 (if (and (eq (char-before) ?:)
496 (progn
497 (forward-char -1)
498 (c-backward-syntactic-ws)
499 (looking-at "[ \t\n]*:\\([^:]+\\|$\\)")))
501 (goto-char checkpoint))
503 (and (eq (char-before) ?\))
504 ;; check if we are looking at a method def
505 (or (not c-method-key)
506 (progn
507 (forward-sexp -1)
508 (forward-char -1)
509 (c-backward-syntactic-ws)
510 (not (or (memq (char-before) '(?- ?+))
511 ;; or a class category
512 (progn
513 (forward-sexp -2)
514 (looking-at c-class-key))
515 )))))
518 ;; defuns to look backwards for things
519 (defun c-backward-to-start-of-do (&optional lim)
520 ;; Move to the start of the last "unbalanced" do expression.
521 ;; Optional LIM is the farthest back to search. If none is found,
522 ;; nil is returned and point is left unchanged, otherwise t is returned.
523 (let ((do-level 1)
524 (case-fold-search nil)
525 (lim (or lim (c-point 'bod)))
526 (here (point))
527 foundp)
528 (while (not (zerop do-level))
529 ;; we protect this call because trying to execute this when the
530 ;; while is not associated with a do will throw an error
531 (condition-case nil
532 (progn
533 (backward-sexp 1)
534 (cond
535 ((memq (c-in-literal lim) '(c c++)))
536 ((looking-at "while\\b[^_]")
537 (setq do-level (1+ do-level)))
538 ((looking-at "do\\b[^_]")
539 (if (zerop (setq do-level (1- do-level)))
540 (setq foundp t)))
541 ((<= (point) lim)
542 (setq do-level 0)
543 (goto-char lim))))
544 (error
545 (goto-char lim)
546 (setq do-level 0))))
547 (if (not foundp)
548 (goto-char here))
549 foundp))
551 (defun c-backward-to-start-of-if (&optional lim)
552 ;; Move to the start of the last "unbalanced" if and return t. If
553 ;; none is found, and we are looking at an if clause, nil is
554 ;; returned. If none is found and we are looking at an else clause,
555 ;; an error is thrown.
556 (let ((if-level 1)
557 (here (c-point 'bol))
558 (case-fold-search nil)
559 (lim (or lim (c-point 'bod)))
560 (at-if (looking-at "if\\b[^_]")))
561 (catch 'orphan-if
562 (while (and (not (bobp))
563 (not (zerop if-level)))
564 (c-backward-syntactic-ws)
565 (condition-case nil
566 (backward-sexp 1)
567 (error
568 (if at-if
569 (throw 'orphan-if nil)
570 (error "No matching `if' found for `else' on line %d."
571 (1+ (count-lines 1 here))))))
572 (cond
573 ((looking-at "else\\b[^_]")
574 (setq if-level (1+ if-level)))
575 ((looking-at "if\\b[^_]")
576 ;; check for else if... skip over
577 (let ((here (point)))
578 (c-safe (forward-sexp -1))
579 (if (looking-at "\\<else\\>[ \t]+\\<if\\>")
581 (setq if-level (1- if-level))
582 (goto-char here))))
583 ((< (point) lim)
584 (setq if-level 0)
585 (goto-char lim))
587 t)))
589 (defun c-skip-conditional ()
590 ;; skip forward over conditional at point, including any predicate
591 ;; statements in parentheses. No error checking is performed.
592 (forward-sexp (cond
593 ;; else if()
594 ((looking-at "\\<else\\>[ \t]+\\<if\\>") 3)
595 ;; do, else, try, finally
596 ((looking-at "\\<\\(do\\|else\\|try\\|finally\\)\\>") 1)
597 ;; for, if, while, switch, catch, synchronized
598 (t 2))))
600 (defun c-skip-case-statement-forward (state &optional lim)
601 ;; skip forward over case/default bodies, with optional maximal
602 ;; limit. if no next case body is found, nil is returned and point
603 ;; is not moved
604 (let ((lim (or lim (point-max)))
605 (here (point))
606 donep foundp bufpos
607 (safepos (point))
608 (balanced (car state)))
609 ;; search until we've passed the limit, or we've found our match
610 (while (and (< (point) lim)
611 (not donep))
612 (setq safepos (point))
613 ;; see if we can find a case statement, not in a literal
614 (if (and (re-search-forward c-switch-label-key lim 'move)
615 (setq bufpos (match-beginning 0))
616 (not (c-in-literal safepos))
617 (/= bufpos here))
618 ;; if we crossed into a balanced sexp, we know the case is
619 ;; not part of our switch statement, so just bound over the
620 ;; sexp and keep looking.
621 (if (and (consp balanced)
622 (> bufpos (car balanced))
623 (< bufpos (cdr balanced)))
624 (goto-char (cdr balanced))
625 (goto-char bufpos)
626 (setq donep t
627 foundp t))))
628 (if (not foundp)
629 (goto-char here))
630 foundp))
632 (defun c-search-uplist-for-classkey (brace-state)
633 ;; search for the containing class, returning a 2 element vector if
634 ;; found. aref 0 contains the bufpos of the class key, and aref 1
635 ;; contains the bufpos of the open brace.
636 (if (null brace-state)
637 ;; no brace-state means we cannot be inside a class
639 (let ((carcache (car brace-state))
640 search-start search-end)
641 (if (consp carcache)
642 ;; a cons cell in the first element means that there is some
643 ;; balanced sexp before the current bufpos. this we can
644 ;; ignore. the nth 1 and nth 2 elements define for us the
645 ;; search boundaries
646 (setq search-start (nth 2 brace-state)
647 search-end (nth 1 brace-state))
648 ;; if the car was not a cons cell then nth 0 and nth 1 define
649 ;; for us the search boundaries
650 (setq search-start (nth 1 brace-state)
651 search-end (nth 0 brace-state)))
652 ;; search-end cannot be a cons cell
653 (and (consp search-end)
654 (error "consp search-end: %s" search-end))
655 ;; if search-end is nil, or if the search-end character isn't an
656 ;; open brace, we are definitely not in a class
657 (if (or (not search-end)
658 (< search-end (point-min))
659 (not (eq (char-after search-end) ?{)))
661 ;; now, we need to look more closely at search-start. if
662 ;; search-start is nil, then our start boundary is really
663 ;; point-min.
664 (if (not search-start)
665 (setq search-start (point-min))
666 ;; if search-start is a cons cell, then we can start
667 ;; searching from the end of the balanced sexp just ahead of
668 ;; us
669 (if (consp search-start)
670 (setq search-start (cdr search-start))))
671 ;; now we can do a quick regexp search from search-start to
672 ;; search-end and see if we can find a class key. watch for
673 ;; class like strings in literals
674 (save-excursion
675 (save-restriction
676 (goto-char search-start)
677 (let ((search-key (concat c-class-key "\\|extern[^_]"))
678 foundp class match-end)
679 (while (and (not foundp)
680 (progn
681 (c-forward-syntactic-ws)
682 (> search-end (point)))
683 (re-search-forward search-key search-end t))
684 (setq class (match-beginning 0)
685 match-end (match-end 0))
686 (if (c-in-literal search-start)
687 nil ; its in a comment or string, ignore
688 (goto-char class)
689 (skip-chars-forward " \t\n")
690 (setq foundp (vector (c-point 'boi) search-end))
691 (cond
692 ;; check for embedded keywords
693 ((let ((char (char-after (1- class))))
694 (and char
695 (memq (char-syntax char) '(?w ?_))))
696 (goto-char match-end)
697 (setq foundp nil))
698 ;; make sure we're really looking at the start of a
699 ;; class definition, and not a forward decl, return
700 ;; arg, template arg list, or an ObjC or Java method.
701 ((and c-method-key
702 (re-search-forward c-method-key search-end t))
703 (setq foundp nil))
704 ;; Its impossible to define a regexp for this, and
705 ;; nearly so to do it programmatically.
707 ;; ; picks up forward decls
708 ;; = picks up init lists
709 ;; ) picks up return types
710 ;; > picks up templates, but remember that we can
711 ;; inherit from templates!
712 ((let ((skipchars "^;=)"))
713 ;; try to see if we found the `class' keyword
714 ;; inside a template arg list
715 (save-excursion
716 (skip-chars-backward "^<>" search-start)
717 (if (eq (char-before) ?<)
718 (setq skipchars (concat skipchars ">"))))
719 (skip-chars-forward skipchars search-end)
720 (/= (point) search-end))
721 (setq foundp nil))
723 foundp))
724 )))))
726 (defun c-inside-bracelist-p (containing-sexp brace-state)
727 ;; return the buffer position of the beginning of the brace list
728 ;; statement if we're inside a brace list, otherwise return nil.
729 ;; CONTAINING-SEXP is the buffer pos of the innermost containing
730 ;; paren. BRACE-STATE is the remainder of the state of enclosing braces
732 ;; N.B.: This algorithm can potentially get confused by cpp macros
733 ;; places in inconvenient locations. Its a trade-off we make for
734 ;; speed.
736 ;; this will pick up enum lists
737 (condition-case ()
738 (save-excursion
739 (goto-char containing-sexp)
740 (forward-sexp -1)
741 (if (or (looking-at "enum[\t\n ]+")
742 (progn (forward-sexp -1)
743 (looking-at "enum[\t\n ]+")))
744 (point)))
745 (error nil))
746 ;; this will pick up array/aggregate init lists, even if they are nested.
747 (save-excursion
748 (let (bufpos failedp)
749 (while (and (not bufpos)
750 containing-sexp)
751 (if (consp containing-sexp)
752 (setq containing-sexp (car brace-state)
753 brace-state (cdr brace-state))
754 ;; see if significant character just before brace is an equal
755 (goto-char containing-sexp)
756 (setq failedp nil)
757 (condition-case ()
758 (progn
759 (forward-sexp -1)
760 (forward-sexp 1)
761 (c-forward-syntactic-ws containing-sexp))
762 (error (setq failedp t)))
763 (if (or failedp (not (eq (char-after) ?=)))
764 ;; lets see if we're nested. find the most nested
765 ;; containing brace
766 (setq containing-sexp (car brace-state)
767 brace-state (cdr brace-state))
768 ;; we've hit the beginning of the aggregate list
769 (c-beginning-of-statement-1 (c-most-enclosing-brace brace-state))
770 (setq bufpos (point)))
772 bufpos))
776 (defun c-most-enclosing-brace (state)
777 ;; return the bufpos of the most enclosing brace that hasn't been
778 ;; narrowed out by any enclosing class, or nil if none was found
779 (let (enclosingp)
780 (while (and state (not enclosingp))
781 (setq enclosingp (car state)
782 state (cdr state))
783 (if (consp enclosingp)
784 (setq enclosingp nil)
785 (if (> (point-min) enclosingp)
786 (setq enclosingp nil))
787 (setq state nil)))
788 enclosingp))
790 (defun c-least-enclosing-brace (state)
791 ;; return the bufpos of the least (highest) enclosing brace that
792 ;; hasn't been narrowed out by any enclosing class, or nil if none
793 ;; was found.
794 (c-most-enclosing-brace (nreverse state)))
796 (defun c-safe-position (bufpos state)
797 ;; return the closest known safe position higher up than point
798 (let ((safepos nil))
799 (while state
800 (setq safepos
801 (if (consp (car state))
802 (cdr (car state))
803 (car state)))
804 (if (< safepos bufpos)
805 (setq state nil)
806 (setq state (cdr state))))
807 safepos))
809 (defun c-narrow-out-enclosing-class (state lim)
810 ;; narrow the buffer so that the enclosing class is hidden
811 (let (inclass-p)
812 (and state
813 (setq inclass-p (c-search-uplist-for-classkey state))
814 (narrow-to-region
815 (progn
816 (goto-char (1+ (aref inclass-p 1)))
817 (skip-chars-forward " \t\n" lim)
818 ;; if point is now left of the class opening brace, we're
819 ;; hosed, so try a different tact
820 (if (<= (point) (aref inclass-p 1))
821 (progn
822 (goto-char (1+ (aref inclass-p 1)))
823 (c-forward-syntactic-ws lim)))
824 (point))
825 ;; end point is the end of the current line
826 (progn
827 (goto-char lim)
828 (c-point 'eol))))
829 ;; return the class vector
830 inclass-p))
833 ;; This function implements the main decision tree for determining the
834 ;; syntactic analysis of the current line of code. Yes, it's huge and
835 ;; bloated!
837 (defun c-guess-basic-syntax ()
838 (save-excursion
839 (save-restriction
840 (beginning-of-line)
841 (let* ((indent-point (point))
842 (case-fold-search nil)
843 (fullstate (c-parse-state))
844 (state fullstate)
845 (in-method-intro-p (and (eq major-mode 'objc-mode)
846 c-method-key
847 (looking-at c-method-key)))
848 literal containing-sexp char-before-ip char-after-ip lim
849 syntax placeholder c-in-literal-cache inswitch-p
850 injava-inher
851 ;; narrow out any enclosing class or extern "C" block
852 (inclass-p (c-narrow-out-enclosing-class state indent-point))
853 (inextern-p (and inclass-p
854 (save-excursion
855 (save-restriction
856 (widen)
857 (goto-char (aref inclass-p 0))
858 (looking-at "extern[^_]")))))
861 ;; get the buffer position of the most nested opening brace,
862 ;; if there is one, and it hasn't been narrowed out
863 (save-excursion
864 (goto-char indent-point)
865 (skip-chars-forward " \t}")
866 (skip-chars-backward " \t")
867 (while (and state
868 (not in-method-intro-p)
869 (not containing-sexp))
870 (setq containing-sexp (car state)
871 state (cdr state))
872 (if (consp containing-sexp)
873 ;; if cdr == point, then containing sexp is the brace
874 ;; that opens the sexp we close
875 (if (= (cdr containing-sexp) (point))
876 (setq containing-sexp (car containing-sexp))
877 ;; otherwise, ignore this element
878 (setq containing-sexp nil))
879 ;; ignore the bufpos if its been narrowed out by the
880 ;; containing class
881 (if (<= containing-sexp (point-min))
882 (setq containing-sexp nil)))))
884 ;; set the limit on the farthest back we need to search
885 (setq lim (or containing-sexp
886 (if (consp (car fullstate))
887 (cdr (car fullstate))
888 nil)
889 (point-min)))
891 ;; cache char before and after indent point, and move point to
892 ;; the most likely position to perform the majority of tests
893 (goto-char indent-point)
894 (skip-chars-forward " \t")
895 (setq char-after-ip (char-after))
896 (c-backward-syntactic-ws lim)
897 (setq char-before-ip (char-before))
898 (goto-char indent-point)
899 (skip-chars-forward " \t")
901 ;; are we in a literal?
902 (setq literal (c-in-literal lim))
904 ;; now figure out syntactic qualities of the current line
905 (cond
906 ;; CASE 1: in a string.
907 ((memq literal '(string))
908 (c-add-syntax 'string (c-point 'bopl)))
909 ;; CASE 2: in a C or C++ style comment.
910 ((memq literal '(c c++))
911 ;; we need to catch multi-paragraph C comments
912 (while (and (zerop (forward-line -1))
913 (looking-at "^[ \t]*$")))
914 (c-add-syntax literal (c-point 'boi)))
915 ;; CASE 3: in a cpp preprocessor
916 ((eq literal 'pound)
917 (c-beginning-of-macro lim)
918 (c-add-syntax 'cpp-macro (c-point 'boi)))
919 ;; CASE 4: in an objective-c method intro
920 (in-method-intro-p
921 (c-add-syntax 'objc-method-intro (c-point 'boi)))
922 ;; CASE 5: Line is at top level.
923 ((null containing-sexp)
924 (cond
925 ;; CASE 5A: we are looking at a defun, class, or
926 ;; inline-inclass method opening brace
927 ((eq char-after-ip ?{)
928 (cond
929 ;; CASE 5A.1: extern declaration
930 ((save-excursion
931 (goto-char indent-point)
932 (skip-chars-forward " \t")
933 (and (c-safe (progn (backward-sexp 2) t))
934 (looking-at "extern[^_]")
935 (progn
936 (setq placeholder (point))
937 (forward-sexp 1)
938 (c-forward-syntactic-ws)
939 (eq (char-after) ?\"))))
940 (goto-char placeholder)
941 (c-add-syntax 'extern-lang-open (c-point 'boi)))
942 ;; CASE 5A.2: we are looking at a class opening brace
943 ((save-excursion
944 (goto-char indent-point)
945 (skip-chars-forward " \t{")
946 ;; TBD: watch out! there could be a bogus
947 ;; c-state-cache in place when we get here. we have
948 ;; to go through much chicanery to ignore the cache.
949 ;; But of course, there may not be! BLECH! BOGUS!
950 (let ((decl
951 (if (boundp 'c-state-cache)
952 (let ((old-cache c-state-cache))
953 (prog2
954 (makunbound 'c-state-cache)
955 (c-search-uplist-for-classkey (c-parse-state))
956 (setq c-state-cache old-cache)))
957 (c-search-uplist-for-classkey (c-parse-state))
959 (and decl
960 (setq placeholder (aref decl 0)))
962 (c-add-syntax 'class-open placeholder))
963 ;; CASE 5A.3: brace list open
964 ((save-excursion
965 (c-beginning-of-statement-1 lim)
966 ;; c-b-o-s could have left us at point-min
967 (and (bobp)
968 (c-forward-syntactic-ws indent-point))
969 (if (looking-at "typedef[^_]")
970 (progn (forward-sexp 1)
971 (c-forward-syntactic-ws indent-point)))
972 (setq placeholder (c-point 'boi))
973 (and (or (looking-at "enum[ \t\n]+")
974 (eq char-before-ip ?=))
975 (save-excursion
976 (skip-chars-forward "^;(" indent-point)
977 (not (memq (char-after) '(?\; ?\()))
979 (c-add-syntax 'brace-list-open placeholder))
980 ;; CASE 5A.4: inline defun open
981 ((and inclass-p (not inextern-p))
982 (c-add-syntax 'inline-open)
983 (c-add-syntax 'inclass (aref inclass-p 0)))
984 ;; CASE 5A.5: ordinary defun open
986 (goto-char placeholder)
987 (c-add-syntax 'defun-open (c-point 'bol))
989 ;; CASE 5B: first K&R arg decl or member init
990 ((c-just-after-func-arglist-p)
991 (cond
992 ;; CASE 5B.1: a member init
993 ((or (eq char-before-ip ?:)
994 (eq char-after-ip ?:))
995 ;; this line should be indented relative to the beginning
996 ;; of indentation for the topmost-intro line that contains
997 ;; the prototype's open paren
998 ;; TBD: is the following redundant?
999 (if (eq char-before-ip ?:)
1000 (forward-char -1))
1001 (c-backward-syntactic-ws lim)
1002 ;; TBD: is the preceding redundant?
1003 (if (eq (char-before) ?:)
1004 (progn (forward-char -1)
1005 (c-backward-syntactic-ws lim)))
1006 (if (eq (char-before) ?\))
1007 (backward-sexp 1))
1008 (setq placeholder (point))
1009 (save-excursion
1010 (and (c-safe (backward-sexp 1) t)
1011 (looking-at "throw[^_]")
1012 (c-safe (backward-sexp 1) t)
1013 (setq placeholder (point))))
1014 (goto-char placeholder)
1015 (c-add-syntax 'member-init-intro (c-point 'boi))
1016 ;; we don't need to add any class offset since this
1017 ;; should be relative to the ctor's indentation
1019 ;; CASE 5B.2: K&R arg decl intro
1020 (c-recognize-knr-p
1021 (c-add-syntax 'knr-argdecl-intro (c-point 'boi))
1022 (and inclass-p (c-add-syntax 'inclass (aref inclass-p 0))))
1023 ;; CASE 5B.3: Nether region after a C++ or Java func
1024 ;; decl, which could include a `throws' declaration.
1026 (c-beginning-of-statement-1 lim)
1027 (c-add-syntax 'func-decl-cont (c-point 'boi))
1029 ;; CASE 5C: inheritance line. could be first inheritance
1030 ;; line, or continuation of a multiple inheritance
1031 ((or (and c-baseclass-key (looking-at c-baseclass-key))
1032 (and (or (eq char-before-ip ?:)
1033 ;; watch out for scope operator
1034 (save-excursion
1035 (and (eq char-after-ip ?:)
1036 (c-safe (progn (forward-char 1) t))
1037 (not (eq (char-after) ?:))
1039 (save-excursion
1040 (c-backward-syntactic-ws lim)
1041 (if (eq char-before-ip ?:)
1042 (progn
1043 (forward-char -1)
1044 (c-backward-syntactic-ws lim)))
1045 (back-to-indentation)
1046 (looking-at c-class-key)))
1047 ;; for Java
1048 (and (eq major-mode 'java-mode)
1049 (let ((fence (save-excursion
1050 (c-beginning-of-statement-1 lim)
1051 (point)))
1052 cont done)
1053 (save-excursion
1054 (while (not done)
1055 (cond ((looking-at c-Java-special-key)
1056 (setq injava-inher (cons cont (point))
1057 done t))
1058 ((or (not (c-safe (forward-sexp -1) t))
1059 (<= (point) fence))
1060 (setq done t))
1062 (setq cont t)))
1063 injava-inher)
1064 (not (c-crosses-statement-barrier-p (cdr injava-inher)
1065 (point)))
1067 (cond
1068 ;; CASE 5C.1: non-hanging colon on an inher intro
1069 ((eq char-after-ip ?:)
1070 (c-backward-syntactic-ws lim)
1071 (c-add-syntax 'inher-intro (c-point 'boi))
1072 ;; don't add inclass symbol since relative point already
1073 ;; contains any class offset
1075 ;; CASE 5C.2: hanging colon on an inher intro
1076 ((eq char-before-ip ?:)
1077 (c-add-syntax 'inher-intro (c-point 'boi))
1078 (and inclass-p (c-add-syntax 'inclass (aref inclass-p 0))))
1079 ;; CASE 5C.3: in a Java implements/extends
1080 (injava-inher
1081 (let ((where (cdr injava-inher))
1082 (cont (car injava-inher)))
1083 (goto-char where)
1084 (cond ((looking-at "throws[ \t\n]")
1085 (c-add-syntax 'func-decl-cont
1086 (progn (c-beginning-of-statement-1 lim)
1087 (c-point 'boi))))
1088 (cont (c-add-syntax 'inher-cont where))
1089 (t (c-add-syntax 'inher-intro
1090 (progn (goto-char (cdr injava-inher))
1091 (c-beginning-of-statement-1 lim)
1092 (point))))
1094 ;; CASE 5C.4: a continued inheritance line
1096 (c-beginning-of-inheritance-list lim)
1097 (c-add-syntax 'inher-cont (point))
1098 ;; don't add inclass symbol since relative point already
1099 ;; contains any class offset
1101 ;; CASE 5D: this could be a top-level compound statement or a
1102 ;; member init list continuation
1103 ((eq char-before-ip ?,)
1104 (goto-char indent-point)
1105 (c-backward-syntactic-ws lim)
1106 (while (and (< lim (point))
1107 (eq (char-before) ?,))
1108 ;; this will catch member inits with multiple
1109 ;; line arglists
1110 (forward-char -1)
1111 (c-backward-syntactic-ws (c-point 'bol))
1112 (if (eq (char-before) ?\))
1113 (backward-sexp 1))
1114 ;; now continue checking
1115 (beginning-of-line)
1116 (c-backward-syntactic-ws lim))
1117 (cond
1118 ;; CASE 5D.1: hanging member init colon, but watch out
1119 ;; for bogus matches on access specifiers inside classes.
1120 ((and (eq (char-before) ?:)
1121 (save-excursion
1122 (forward-word -1)
1123 (not (looking-at c-access-key))))
1124 (goto-char indent-point)
1125 (c-backward-syntactic-ws lim)
1126 (c-safe (backward-sexp 1))
1127 (c-add-syntax 'member-init-cont (c-point 'boi))
1128 ;; we do not need to add class offset since relative
1129 ;; point is the member init above us
1131 ;; CASE 5D.2: non-hanging member init colon
1132 ((progn
1133 (c-forward-syntactic-ws indent-point)
1134 (eq (char-after) ?:))
1135 (skip-chars-forward " \t:")
1136 (c-add-syntax 'member-init-cont (point)))
1137 ;; CASE 5D.3: perhaps a multiple inheritance line?
1138 ((looking-at c-inher-key)
1139 (c-add-syntax 'inher-cont (c-point 'boi)))
1140 ;; CASE 5D.4: perhaps a template list continuation?
1141 ((save-excursion
1142 (skip-chars-backward "^<" lim)
1143 ;; not sure if this is the right test, but it should
1144 ;; be fast and mostly accurate.
1145 (and (eq (char-before) ?<)
1146 (not (c-in-literal lim))))
1147 ;; we can probably indent it just like and arglist-cont
1148 (c-add-syntax 'arglist-cont (point)))
1149 ;; CASE 5D.5: perhaps a top-level statement-cont
1151 (c-beginning-of-statement-1 lim)
1152 ;; skip over any access-specifiers
1153 (and inclass-p c-access-key
1154 (while (looking-at c-access-key)
1155 (forward-line 1)))
1156 ;; skip over comments, whitespace
1157 (c-forward-syntactic-ws indent-point)
1158 (c-add-syntax 'statement-cont (c-point 'boi)))
1160 ;; CASE 5E: we are looking at a access specifier
1161 ((and inclass-p
1162 c-access-key
1163 (looking-at c-access-key))
1164 (c-add-syntax 'access-label (c-point 'bonl))
1165 (c-add-syntax 'inclass (aref inclass-p 0)))
1166 ;; CASE 5F: extern-lang-close?
1167 ((and inextern-p
1168 (eq char-after-ip ?}))
1169 (c-add-syntax 'extern-lang-close (aref inclass-p 1)))
1170 ;; CASE 5G: we are looking at the brace which closes the
1171 ;; enclosing nested class decl
1172 ((and inclass-p
1173 (eq char-after-ip ?})
1174 (save-excursion
1175 (save-restriction
1176 (widen)
1177 (forward-char 1)
1178 (and
1179 (condition-case nil
1180 (progn (backward-sexp 1) t)
1181 (error nil))
1182 (= (point) (aref inclass-p 1))
1183 ))))
1184 (save-restriction
1185 (widen)
1186 (goto-char (aref inclass-p 0))
1187 (c-add-syntax 'class-close (c-point 'boi))))
1188 ;; CASE 5H: we could be looking at subsequent knr-argdecls
1189 ((and c-recognize-knr-p
1190 ;; here we essentially use the hack that is used in
1191 ;; Emacs' c-mode.el to limit how far back we should
1192 ;; look. The assumption is made that argdecls are
1193 ;; indented at least one space and that function
1194 ;; headers are not indented.
1195 (let ((limit (save-excursion
1196 (re-search-backward "^[^ \^L\t\n#]" nil 'move)
1197 (point))))
1198 (save-excursion
1199 (c-backward-syntactic-ws limit)
1200 (setq placeholder (point))
1201 (while (and (memq (char-before) '(?\; ?,))
1202 (> (point) limit))
1203 (beginning-of-line)
1204 (setq placeholder (point))
1205 (c-backward-syntactic-ws limit))
1206 (and (eq (char-before) ?\))
1207 (or (not c-method-key)
1208 (progn
1209 (forward-sexp -1)
1210 (forward-char -1)
1211 (c-backward-syntactic-ws)
1212 (not (or (memq (char-before) '(?- ?+))
1213 ;; or a class category
1214 (progn
1215 (forward-sexp -2)
1216 (looking-at c-class-key))
1217 )))))
1219 (save-excursion
1220 (c-beginning-of-statement-1)
1221 (not (looking-at "typedef[ \t\n]+"))))
1222 (goto-char placeholder)
1223 (c-add-syntax 'knr-argdecl (c-point 'boi)))
1224 ;; CASE 5I: we are at the topmost level, make sure we skip
1225 ;; back past any access specifiers
1226 ((progn
1227 (c-backward-syntactic-ws lim)
1228 (while (and inclass-p
1229 c-access-key
1230 (not (bobp))
1231 (save-excursion
1232 (c-safe (progn (backward-sexp 1) t))
1233 (looking-at c-access-key)))
1234 (backward-sexp 1)
1235 (c-backward-syntactic-ws lim))
1236 (or (bobp)
1237 (memq (char-before) '(?\; ?\}))))
1238 ;; real beginning-of-line could be narrowed out due to
1239 ;; enclosure in a class block
1240 (save-restriction
1241 (widen)
1242 (c-add-syntax 'topmost-intro (c-point 'bol))
1243 (if inclass-p
1244 (progn
1245 (goto-char (aref inclass-p 1))
1246 (if inextern-p
1247 (c-add-syntax 'inextern-lang)
1248 (c-add-syntax 'inclass (c-point 'boi)))))
1250 ;; CASE 5J: we are at an ObjC or Java method definition
1251 ;; continuation line.
1252 ((and c-method-key
1253 (progn
1254 (c-beginning-of-statement-1 lim)
1255 (beginning-of-line)
1256 (looking-at c-method-key)))
1257 (c-add-syntax 'objc-method-args-cont (point)))
1258 ;; CASE 5K: we are at a topmost continuation line
1260 (c-beginning-of-statement-1 lim)
1261 (c-forward-syntactic-ws)
1262 (c-add-syntax 'topmost-intro-cont (c-point 'boi)))
1263 )) ; end CASE 5
1264 ;; CASE 6: line is an expression, not a statement. Most
1265 ;; likely we are either in a function prototype or a function
1266 ;; call argument list
1267 ((not (eq (char-after containing-sexp) ?{))
1268 (c-backward-syntactic-ws containing-sexp)
1269 (cond
1270 ;; CASE 6A: we are looking at the arglist closing paren
1271 ((and (not (eq char-before-ip ?,))
1272 (memq char-after-ip '(?\) ?\])))
1273 (goto-char containing-sexp)
1274 (c-add-syntax 'arglist-close (c-point 'boi)))
1275 ;; CASE 6B: we are looking at the first argument in an empty
1276 ;; argument list. Use arglist-close if we're actually
1277 ;; looking at a close paren or bracket.
1278 ((memq char-before-ip '(?\( ?\[))
1279 (goto-char containing-sexp)
1280 (c-add-syntax 'arglist-intro (c-point 'boi)))
1281 ;; CASE 6C: we are inside a conditional test clause. treat
1282 ;; these things as statements
1283 ((save-excursion
1284 (goto-char containing-sexp)
1285 (and (c-safe (progn (forward-sexp -1) t))
1286 (looking-at "\\<for\\>[^_]")))
1287 (goto-char (1+ containing-sexp))
1288 (c-forward-syntactic-ws indent-point)
1289 (c-beginning-of-statement-1 containing-sexp)
1290 (if (eq char-before-ip ?\;)
1291 (c-add-syntax 'statement (point))
1292 (c-add-syntax 'statement-cont (point))
1294 ;; CASE 6D: maybe a continued method call. This is the case
1295 ;; when we are inside a [] bracketed exp, and what precede
1296 ;; the opening bracket is not an identifier.
1297 ((and c-method-key
1298 (eq (char-after containing-sexp) ?\[)
1299 (save-excursion
1300 (goto-char (1- containing-sexp))
1301 (c-backward-syntactic-ws (c-point 'bod))
1302 (if (not (looking-at c-symbol-key))
1303 (c-add-syntax 'objc-method-call-cont containing-sexp))
1305 ;; CASE 6E: we are looking at an arglist continuation line,
1306 ;; but the preceding argument is on the same line as the
1307 ;; opening paren. This case includes multi-line
1308 ;; mathematical paren groupings, but we could be on a
1309 ;; for-list continuation line
1310 ((and (save-excursion
1311 (goto-char (1+ containing-sexp))
1312 (skip-chars-forward " \t")
1313 (not (eolp)))
1314 (save-excursion
1315 (c-beginning-of-statement-1 lim)
1316 (skip-chars-backward " \t([")
1317 (<= (point) containing-sexp)))
1318 (goto-char containing-sexp)
1319 (c-add-syntax 'arglist-cont-nonempty (c-point 'boi)))
1320 ;; CASE 6F: we are looking at just a normal arglist
1321 ;; continuation line
1322 (t (c-beginning-of-statement-1 containing-sexp)
1323 (forward-char 1)
1324 (c-forward-syntactic-ws indent-point)
1325 (c-add-syntax 'arglist-cont (c-point 'boi)))
1327 ;; CASE 7: func-local multi-inheritance line
1328 ((and c-baseclass-key
1329 (save-excursion
1330 (goto-char indent-point)
1331 (skip-chars-forward " \t")
1332 (looking-at c-baseclass-key)))
1333 (goto-char indent-point)
1334 (skip-chars-forward " \t")
1335 (cond
1336 ;; CASE 7A: non-hanging colon on an inher intro
1337 ((eq char-after-ip ?:)
1338 (c-backward-syntactic-ws lim)
1339 (c-add-syntax 'inher-intro (c-point 'boi)))
1340 ;; CASE 7B: hanging colon on an inher intro
1341 ((eq char-before-ip ?:)
1342 (c-add-syntax 'inher-intro (c-point 'boi)))
1343 ;; CASE 7C: a continued inheritance line
1345 (c-beginning-of-inheritance-list lim)
1346 (c-add-syntax 'inher-cont (point))
1348 ;; CASE 8: we are inside a brace-list
1349 ((setq placeholder (c-inside-bracelist-p containing-sexp state))
1350 (cond
1351 ;; CASE 8A: brace-list-close brace
1352 ((and (eq char-after-ip ?})
1353 (c-safe (progn (forward-char 1)
1354 (backward-sexp 1)
1356 (= (point) containing-sexp))
1357 (c-add-syntax 'brace-list-close (c-point 'boi)))
1358 ;; CASE 8B: we're looking at the first line in a brace-list
1359 ((save-excursion
1360 (goto-char indent-point)
1361 (c-backward-syntactic-ws containing-sexp)
1362 (= (point) (1+ containing-sexp)))
1363 (goto-char containing-sexp)
1364 (c-add-syntax 'brace-list-intro (c-point 'boi))
1366 ;;)) ; end CASE 8B
1367 ;; CASE 8C: this is just a later brace-list-entry
1368 (t (goto-char (1+ containing-sexp))
1369 (c-forward-syntactic-ws indent-point)
1370 (if (eq char-after-ip ?{)
1371 (c-add-syntax 'brace-list-open (point))
1372 (c-add-syntax 'brace-list-entry (point))
1373 )) ; end CASE 8C
1374 )) ; end CASE 8
1375 ;; CASE 9: A continued statement
1376 ((and (not (memq char-before-ip '(?\; ?} ?:)))
1377 (> (point)
1378 (save-excursion
1379 (c-beginning-of-statement-1 containing-sexp)
1380 (setq placeholder (point))))
1381 (/= placeholder containing-sexp))
1382 (goto-char indent-point)
1383 (skip-chars-forward " \t")
1384 (let ((after-cond-placeholder
1385 (save-excursion
1386 (goto-char placeholder)
1387 (if (looking-at c-conditional-key)
1388 (progn
1389 (c-safe (c-skip-conditional))
1390 (c-forward-syntactic-ws)
1391 (if (eq (char-after) ?\;)
1392 (progn
1393 (forward-char 1)
1394 (c-forward-syntactic-ws)))
1395 (point))
1396 nil))))
1397 (cond
1398 ;; CASE 9A: substatement
1399 ((and after-cond-placeholder
1400 (>= after-cond-placeholder indent-point))
1401 (goto-char placeholder)
1402 (if (eq char-after-ip ?{)
1403 (c-add-syntax 'substatement-open (c-point 'boi))
1404 (c-add-syntax 'substatement (c-point 'boi))))
1405 ;; CASE 9B: open braces for class or brace-lists
1406 ((eq char-after-ip ?{)
1407 (cond
1408 ;; CASE 9B.1: class-open
1409 ((save-excursion
1410 (goto-char indent-point)
1411 (skip-chars-forward " \t{")
1412 (let ((decl (c-search-uplist-for-classkey (c-parse-state))))
1413 (and decl
1414 (setq placeholder (aref decl 0)))
1416 (c-add-syntax 'class-open placeholder))
1417 ;; CASE 9B.2: brace-list-open
1418 ((or (save-excursion
1419 (goto-char placeholder)
1420 (looking-at "\\<enum\\>"))
1421 (eq char-before-ip ?=))
1422 (c-add-syntax 'brace-list-open placeholder))
1423 ;; CASE 9B.3: catch-all for unknown construct.
1425 ;; Can and should I add an extensibility hook here?
1426 ;; Something like c-recognize-hook so support for
1427 ;; unknown constructs could be added. It's probably a
1428 ;; losing proposition, so I dunno.
1429 (goto-char placeholder)
1430 (c-add-syntax 'statement-cont (c-point 'boi))
1431 (c-add-syntax 'block-open))
1433 ;; CASE 9C: iostream insertion or extraction operator
1434 ((looking-at "<<\\|>>")
1435 (goto-char placeholder)
1436 (and after-cond-placeholder
1437 (goto-char after-cond-placeholder))
1438 (while (and (re-search-forward "<<\\|>>" indent-point 'move)
1439 (c-in-literal placeholder)))
1440 ;; if we ended up at indent-point, then the first
1441 ;; streamop is on a separate line. Indent the line like
1442 ;; a statement-cont instead
1443 (if (/= (point) indent-point)
1444 (c-add-syntax 'stream-op (c-point 'boi))
1445 (c-backward-syntactic-ws lim)
1446 (c-add-syntax 'statement-cont (c-point 'boi))))
1447 ;; CASE 9D: continued statement. find the accurate
1448 ;; beginning of statement or substatement
1450 (c-beginning-of-statement-1 after-cond-placeholder)
1451 ;; KLUDGE ALERT! c-beginning-of-statement-1 can leave
1452 ;; us before the lim we're passing in. It should be
1453 ;; fixed, but I'm worried about side-effects at this
1454 ;; late date. Fix for v5.
1455 (goto-char (or (and after-cond-placeholder
1456 (max after-cond-placeholder (point)))
1457 (point)))
1458 (c-add-syntax 'statement-cont (point)))
1460 ;; CASE 10: an else clause?
1461 ((looking-at "\\<else\\>[^_]")
1462 (c-backward-to-start-of-if containing-sexp)
1463 (c-add-syntax 'else-clause (c-point 'boi)))
1464 ;; CASE 11: Statement. But what kind? Lets see if its a
1465 ;; while closure of a do/while construct
1466 ((progn
1467 (goto-char indent-point)
1468 (skip-chars-forward " \t")
1469 (and (looking-at "while\\b[^_]")
1470 (save-excursion
1471 (c-backward-to-start-of-do containing-sexp)
1472 (setq placeholder (point))
1473 (looking-at "do\\b[^_]"))
1475 (c-add-syntax 'do-while-closure placeholder))
1476 ;; CASE 12: A case or default label
1477 ((looking-at c-switch-label-key)
1478 (goto-char containing-sexp)
1479 ;; check for hanging braces
1480 (if (/= (point) (c-point 'boi))
1481 (forward-sexp -1))
1482 (c-add-syntax 'case-label (c-point 'boi)))
1483 ;; CASE 13: any other label
1484 ((looking-at c-label-key)
1485 (goto-char containing-sexp)
1486 (c-add-syntax 'label (c-point 'boi)))
1487 ;; CASE 14: block close brace, possibly closing the defun or
1488 ;; the class
1489 ((eq char-after-ip ?})
1490 (let* ((lim (c-safe-position containing-sexp fullstate))
1491 (relpos (save-excursion
1492 (goto-char containing-sexp)
1493 (if (/= (point) (c-point 'boi))
1494 (c-beginning-of-statement-1 lim))
1495 (c-point 'boi))))
1496 (cond
1497 ;; CASE 14A: does this close an inline?
1498 ((let ((inclass-p (progn
1499 (goto-char containing-sexp)
1500 (c-search-uplist-for-classkey state))))
1501 ;; inextern-p in higher level let*
1502 (setq inextern-p (and inclass-p
1503 (progn
1504 (goto-char (aref inclass-p 0))
1505 (looking-at "extern[^_]"))))
1506 (and inclass-p (not inextern-p)))
1507 (c-add-syntax 'inline-close relpos))
1508 ;; CASE 14B: if there an enclosing brace that hasn't
1509 ;; been narrowed out by a class, then this is a
1510 ;; block-close
1511 ((and (not inextern-p)
1512 (c-most-enclosing-brace state))
1513 (c-add-syntax 'block-close relpos))
1514 ;; CASE 14C: find out whether we're closing a top-level
1515 ;; class or a defun
1517 (save-restriction
1518 (narrow-to-region (point-min) indent-point)
1519 (let ((decl (c-search-uplist-for-classkey (c-parse-state))))
1520 (if decl
1521 (c-add-syntax 'class-close (aref decl 0))
1522 (c-add-syntax 'defun-close relpos)))))
1524 ;; CASE 15: statement catchall
1526 ;; we know its a statement, but we need to find out if it is
1527 ;; the first statement in a block
1528 (goto-char containing-sexp)
1529 (forward-char 1)
1530 (c-forward-syntactic-ws indent-point)
1531 ;; now skip forward past any case/default clauses we might find.
1532 (while (or (c-skip-case-statement-forward fullstate indent-point)
1533 (and (looking-at c-switch-label-key)
1534 (not inswitch-p)))
1535 (setq inswitch-p t))
1536 ;; we want to ignore non-case labels when skipping forward
1537 (while (and (looking-at c-label-key)
1538 (goto-char (match-end 0)))
1539 (c-forward-syntactic-ws indent-point))
1540 (cond
1541 ;; CASE 15A: we are inside a case/default clause inside a
1542 ;; switch statement. find out if we are at the statement
1543 ;; just after the case/default label.
1544 ((and inswitch-p
1545 (progn
1546 (goto-char indent-point)
1547 (c-backward-syntactic-ws containing-sexp)
1548 (back-to-indentation)
1549 (setq placeholder (point))
1550 (looking-at c-switch-label-key)))
1551 (goto-char indent-point)
1552 (skip-chars-forward " \t")
1553 (if (eq (char-after) ?{)
1554 (c-add-syntax 'statement-case-open placeholder)
1555 (c-add-syntax 'statement-case-intro placeholder)))
1556 ;; CASE 15B: continued statement
1557 ((eq char-before-ip ?,)
1558 (c-add-syntax 'statement-cont (c-point 'boi)))
1559 ;; CASE 15C: a question/colon construct? But make sure
1560 ;; what came before was not a label, and what comes after
1561 ;; is not a globally scoped function call!
1562 ((or (and (memq char-before-ip '(?: ??))
1563 (save-excursion
1564 (goto-char indent-point)
1565 (c-backward-syntactic-ws lim)
1566 (back-to-indentation)
1567 (not (looking-at c-label-key))))
1568 (and (memq char-after-ip '(?: ??))
1569 (save-excursion
1570 (goto-char indent-point)
1571 (skip-chars-forward " \t")
1572 ;; watch out for scope operator
1573 (not (looking-at "::")))))
1574 (c-add-syntax 'statement-cont (c-point 'boi)))
1575 ;; CASE 15D: any old statement
1576 ((< (point) indent-point)
1577 (let ((safepos (c-most-enclosing-brace fullstate))
1578 relpos done)
1579 (goto-char indent-point)
1580 (c-beginning-of-statement-1 safepos)
1581 ;; It is possible we're on the brace that opens a nested
1582 ;; function.
1583 (if (and (eq (char-after) ?{)
1584 (save-excursion
1585 (c-backward-syntactic-ws safepos)
1586 (not (eq (char-before) ?\;))))
1587 (c-beginning-of-statement-1 safepos))
1588 (if (and inswitch-p
1589 (looking-at c-switch-label-key))
1590 (progn
1591 (goto-char placeholder)
1592 (end-of-line)
1593 (forward-sexp -1)))
1594 (setq relpos (c-point 'boi))
1595 (while (and (not done)
1596 (<= safepos (point))
1597 (/= relpos (point)))
1598 (c-beginning-of-statement-1 safepos)
1599 (if (= relpos (c-point 'boi))
1600 (setq done t))
1601 (setq relpos (c-point 'boi)))
1602 (c-add-syntax 'statement relpos)
1603 (if (eq char-after-ip ?{)
1604 (c-add-syntax 'block-open))))
1605 ;; CASE 15E: first statement in an inline, or first
1606 ;; statement in a top-level defun. we can tell this is it
1607 ;; if there are no enclosing braces that haven't been
1608 ;; narrowed out by a class (i.e. don't use bod here!)
1609 ((save-excursion
1610 (save-restriction
1611 (widen)
1612 (goto-char containing-sexp)
1613 (c-narrow-out-enclosing-class state containing-sexp)
1614 (not (c-most-enclosing-brace state))))
1615 (goto-char containing-sexp)
1616 ;; if not at boi, then defun-opening braces are hung on
1617 ;; right side, so we need a different relpos
1618 (if (/= (point) (c-point 'boi))
1619 (progn
1620 (c-backward-syntactic-ws)
1621 (c-safe (forward-sexp (if (eq (char-before) ?\))
1622 -1 -2)))
1623 ;; looking at a Java throws clause following a
1624 ;; method's parameter list
1625 (c-beginning-of-statement-1)
1627 (c-add-syntax 'defun-block-intro (c-point 'boi)))
1628 ;; CASE 15F: first statement in a block
1629 (t (goto-char containing-sexp)
1630 (if (/= (point) (c-point 'boi))
1631 (c-beginning-of-statement-1
1632 (if (= (point) lim)
1633 (c-safe-position (point) state) lim)))
1634 (c-add-syntax 'statement-block-intro (c-point 'boi))
1635 (if (eq char-after-ip ?{)
1636 (c-add-syntax 'block-open)))
1640 ;; now we need to look at any modifiers
1641 (goto-char indent-point)
1642 (skip-chars-forward " \t")
1643 ;; are we looking at a comment only line?
1644 (if (looking-at c-comment-start-regexp)
1645 (c-add-syntax 'comment-intro))
1646 ;; we might want to give additional offset to friends (in C++).
1647 (if (and (eq major-mode 'c++-mode)
1648 (looking-at c-C++-friend-key))
1649 (c-add-syntax 'friend))
1650 ;; return the syntax
1651 syntax))))
1654 (defun c-echo-parsing-error ()
1655 (if (not c-parsing-error)
1657 (message "unbalanced close brace at bufpos %d -- INDENTATION IS SUSPECT!"
1658 c-parsing-error)
1659 (ding))
1660 c-parsing-error)
1662 ;; indent via syntactic language elements
1663 (defun c-indent-line (&optional syntax)
1664 ;; indent the current line as C/C++/ObjC code. Optional SYNTAX is the
1665 ;; syntactic information for the current line. Returns the amount of
1666 ;; indentation change
1667 (let* ((c-syntactic-context (or syntax (c-guess-basic-syntax)))
1668 (pos (- (point-max) (point)))
1669 (indent (apply '+ (mapcar 'c-get-offset c-syntactic-context)))
1670 (shift-amt (- (current-indentation) indent)))
1671 (and c-echo-syntactic-information-p
1672 (not (c-echo-parsing-error))
1673 (message "syntax: %s, indent= %d" c-syntactic-context indent))
1674 (if (zerop shift-amt)
1676 (delete-region (c-point 'bol) (c-point 'boi))
1677 (beginning-of-line)
1678 (indent-to indent))
1679 (if (< (point) (c-point 'boi))
1680 (back-to-indentation)
1681 ;; If initial point was within line's indentation, position after
1682 ;; the indentation. Else stay at same point in text.
1683 (if (> (- (point-max) pos) (point))
1684 (goto-char (- (point-max) pos)))
1686 (run-hooks 'c-special-indent-hook)
1687 shift-amt))
1689 (defun c-show-syntactic-information (arg)
1690 "Show syntactic information for current line.
1691 With universal argument, inserts the analysis as a comment on that line."
1692 (interactive "P")
1693 (let ((syntax (c-guess-basic-syntax)))
1694 (if (not (consp arg))
1695 (if (not (c-echo-parsing-error))
1696 (message "syntactic analysis: %s" syntax))
1697 (indent-for-comment)
1698 (insert (format "%s" syntax))
1700 (c-keep-region-active))
1703 (provide 'cc-engine)
1704 ;;; cc-engine.el ends here