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