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