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