165
|
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)
|
177
|
10 ;; Version: 5.14
|
165
|
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
|
175
|
31 ;; KLUDGE ALERT: c-maybe-labelp is used to pass information between
|
|
32 ;; c-crosses-statement-barrier-p and c-beginning-of-statement-1. A
|
|
33 ;; better way should be implemented, but this will at least shut up
|
|
34 ;; the byte compiler.
|
|
35 (defvar c-maybe-labelp nil)
|
|
36
|
|
37 ;; WARNING WARNING WARNING
|
|
38 ;;
|
|
39 ;; Be *exceptionally* careful about modifications to this function!
|
|
40 ;; Much of CC Mode depends on this Doing The Right Thing. If you
|
|
41 ;; break it you will be sorry. If you think you know how this works,
|
|
42 ;; you probably don't. No human on Earth does! :-)
|
|
43 ;;
|
|
44 ;; WARNING WARNING WARNING
|
165
|
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)
|
175
|
51 donep c-in-literal-cache saved
|
165
|
52 (last-begin (point)))
|
|
53 ;; first check for bare semicolon
|
|
54 (if (and (progn (c-backward-syntactic-ws lim)
|
171
|
55 (eq (char-before) ?\;))
|
165
|
56 (c-safe (progn (forward-char -1)
|
|
57 (setq saved (point))
|
|
58 t))
|
|
59 (progn (c-backward-syntactic-ws lim)
|
177
|
60 (memq (char-before) '(?\; ?{ ?:)))
|
165
|
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")
|
171
|
79 (if (eq (char-before) ?\()
|
165
|
80 (setq last-begin (point))))
|
|
81 (goto-char last-begin)
|
|
82 (setq last-begin (point)
|
|
83 donep t)))
|
|
84
|
175
|
85 (setq c-maybe-labelp nil)
|
165
|
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)
|
171
|
98 (and (eq (char-after) ?\()
|
165
|
99 (save-excursion
|
|
100 (forward-sexp 1)
|
|
101 (c-forward-syntactic-ws)
|
171
|
102 (not (eq (char-after) ?\;)))
|
165
|
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
|
175
|
147 ((and c-maybe-labelp
|
165
|
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
|
171
|
207 (if (memq (char-after) '(?\; ?{ ?}))
|
165
|
208 (setq crossedp t)
|
171
|
209 (if (eq (char-after) ?:)
|
175
|
210 (setq c-maybe-labelp t))
|
165
|
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
|
171
|
236 (if (and (eq (char-after) ?#)
|
165
|
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
|
171
|
292 (defvar c-parsing-error nil)
|
165
|
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
|
171
|
299 (setq c-parsing-error nil)
|
165
|
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)
|
171
|
309 (if (eq (char-after) ?\{)
|
165
|
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
|
171
|
334 (if (eq (char-after (1- pos)) ?\{)
|
165
|
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)
|
171
|
352 (eq (char-after (1- placeholder)) ?\}))
|
165
|
353 (while t
|
|
354 (setq last-bod (c-safe (scan-lists last-bod -1 1)))
|
|
355 (if (not last-bod)
|
171
|
356 (progn
|
|
357 ;; bogus, but what can we do here?
|
|
358 (setq c-parsing-error (1- placeholder))
|
|
359 (throw 'backup-bod nil))
|
165
|
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)
|
171
|
457 (memq (char-before) '(?, ?:))
|
165
|
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
|
171
|
494 (if (and (eq (char-before) ?t)
|
165
|
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)
|
171
|
501 (if (and (eq (char-before) ?:)
|
165
|
502 (progn
|
|
503 (forward-char -1)
|
|
504 (c-backward-syntactic-ws)
|
|
505 (looking-at "[ \t\n]*:\\([^:]+\\|$\\)")))
|
|
506 nil
|
|
507 (goto-char checkpoint))
|
|
508 )
|
171
|
509 (and (eq (char-before) ?\))
|
165
|
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)
|
171
|
516 (not (or (memq (char-before) '(?- ?+))
|
165
|
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))
|
171
|
665 (not (eq (char-after search-end) ?{)))
|
165
|
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)
|
171
|
723 (if (eq (char-before) ?<)
|
165
|
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)))
|
171
|
769 (if (or failedp (not (eq (char-after) ?=)))
|
165
|
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")
|
171
|
901 (setq char-after-ip (char-after))
|
165
|
902 (c-backward-syntactic-ws lim)
|
171
|
903 (setq char-before-ip (char-before))
|
165
|
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
|
171
|
933 ((eq char-after-ip ?{)
|
165
|
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)
|
171
|
945 (eq (char-after) ?\"))))
|
165
|
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]+")
|
171
|
980 (eq char-before-ip ?=))
|
165
|
981 (save-excursion
|
|
982 (skip-chars-forward "^;(" indent-point)
|
171
|
983 (not (memq (char-after) '(?\; ?\()))
|
165
|
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
|
171
|
999 ((or (eq char-before-ip ?:)
|
|
1000 (eq char-after-ip ?:))
|
165
|
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?
|
171
|
1005 (if (eq char-before-ip ?:)
|
165
|
1006 (forward-char -1))
|
|
1007 (c-backward-syntactic-ws lim)
|
|
1008 ;; TBD: is the preceding redundant?
|
171
|
1009 (if (eq (char-before) ?:)
|
165
|
1010 (progn (forward-char -1)
|
|
1011 (c-backward-syntactic-ws lim)))
|
171
|
1012 (if (eq (char-before) ?\))
|
165
|
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))
|
171
|
1038 (and (or (eq char-before-ip ?:)
|
165
|
1039 ;; watch out for scope operator
|
|
1040 (save-excursion
|
171
|
1041 (and (eq char-after-ip ?:)
|
165
|
1042 (c-safe (progn (forward-char 1) t))
|
171
|
1043 (not (eq (char-after) ?:))
|
165
|
1044 )))
|
|
1045 (save-excursion
|
|
1046 (c-backward-syntactic-ws lim)
|
171
|
1047 (if (eq char-before-ip ?:)
|
165
|
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
|
171
|
1075 ((eq char-after-ip ?:)
|
165
|
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
|
171
|
1082 ((eq char-before-ip ?:)
|
165
|
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
|
171
|
1109 ((eq char-before-ip ?,)
|
165
|
1110 (goto-char indent-point)
|
|
1111 (c-backward-syntactic-ws lim)
|
|
1112 (while (and (< lim (point))
|
171
|
1113 (eq (char-before) ?,))
|
165
|
1114 ;; this will catch member inits with multiple
|
|
1115 ;; line arglists
|
|
1116 (forward-char -1)
|
|
1117 (c-backward-syntactic-ws (c-point 'bol))
|
171
|
1118 (if (eq (char-before) ?\))
|
165
|
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.
|
171
|
1126 ((and (eq (char-before) ?:)
|
165
|
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)
|
171
|
1140 (eq (char-after) ?:))
|
165
|
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.
|
171
|
1151 (and (eq (char-before) ?<)
|
165
|
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
|
171
|
1174 (eq char-after-ip ?}))
|
165
|
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
|
171
|
1179 (eq char-after-ip ?})
|
165
|
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))
|
171
|
1207 (while (and (memq (char-before) '(?\; ?,))
|
165
|
1208 (> (point) limit))
|
|
1209 (beginning-of-line)
|
|
1210 (setq placeholder (point))
|
|
1211 (c-backward-syntactic-ws limit))
|
171
|
1212 (and (eq (char-before) ?\))
|
165
|
1213 (or (not c-method-key)
|
|
1214 (progn
|
|
1215 (forward-sexp -1)
|
|
1216 (forward-char -1)
|
|
1217 (c-backward-syntactic-ws)
|
171
|
1218 (not (or (memq (char-before) '(?- ?+))
|
165
|
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)
|
171
|
1243 (memq (char-before) '(?\; ?\}))))
|
165
|
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))
|
177
|
1252 (or (= (point) (c-point 'boi))
|
|
1253 (goto-char (aref inclass-p 0)))
|
165
|
1254 (if inextern-p
|
|
1255 (c-add-syntax 'inextern-lang)
|
|
1256 (c-add-syntax 'inclass (c-point 'boi)))))
|
|
1257 ))
|
|
1258 ;; CASE 5J: we are at an ObjC or Java method definition
|
|
1259 ;; continuation line.
|
|
1260 ((and c-method-key
|
|
1261 (progn
|
|
1262 (c-beginning-of-statement-1 lim)
|
|
1263 (beginning-of-line)
|
|
1264 (looking-at c-method-key)))
|
|
1265 (c-add-syntax 'objc-method-args-cont (point)))
|
|
1266 ;; CASE 5K: we are at a topmost continuation line
|
|
1267 (t
|
|
1268 (c-beginning-of-statement-1 lim)
|
|
1269 (c-forward-syntactic-ws)
|
|
1270 (c-add-syntax 'topmost-intro-cont (c-point 'boi)))
|
|
1271 )) ; end CASE 5
|
|
1272 ;; CASE 6: line is an expression, not a statement. Most
|
|
1273 ;; likely we are either in a function prototype or a function
|
|
1274 ;; call argument list
|
171
|
1275 ((not (eq (char-after containing-sexp) ?{))
|
165
|
1276 (c-backward-syntactic-ws containing-sexp)
|
|
1277 (cond
|
|
1278 ;; CASE 6A: we are looking at the arglist closing paren
|
171
|
1279 ((and (not (eq char-before-ip ?,))
|
165
|
1280 (memq char-after-ip '(?\) ?\])))
|
|
1281 (goto-char containing-sexp)
|
|
1282 (c-add-syntax 'arglist-close (c-point 'boi)))
|
|
1283 ;; CASE 6B: we are looking at the first argument in an empty
|
|
1284 ;; argument list. Use arglist-close if we're actually
|
|
1285 ;; looking at a close paren or bracket.
|
|
1286 ((memq char-before-ip '(?\( ?\[))
|
|
1287 (goto-char containing-sexp)
|
|
1288 (c-add-syntax 'arglist-intro (c-point 'boi)))
|
|
1289 ;; CASE 6C: we are inside a conditional test clause. treat
|
|
1290 ;; these things as statements
|
|
1291 ((save-excursion
|
|
1292 (goto-char containing-sexp)
|
|
1293 (and (c-safe (progn (forward-sexp -1) t))
|
|
1294 (looking-at "\\<for\\>[^_]")))
|
|
1295 (goto-char (1+ containing-sexp))
|
|
1296 (c-forward-syntactic-ws indent-point)
|
|
1297 (c-beginning-of-statement-1 containing-sexp)
|
171
|
1298 (if (eq char-before-ip ?\;)
|
165
|
1299 (c-add-syntax 'statement (point))
|
|
1300 (c-add-syntax 'statement-cont (point))
|
|
1301 ))
|
|
1302 ;; CASE 6D: maybe a continued method call. This is the case
|
|
1303 ;; when we are inside a [] bracketed exp, and what precede
|
|
1304 ;; the opening bracket is not an identifier.
|
|
1305 ((and c-method-key
|
171
|
1306 (eq (char-after containing-sexp) ?\[)
|
165
|
1307 (save-excursion
|
|
1308 (goto-char (1- containing-sexp))
|
|
1309 (c-backward-syntactic-ws (c-point 'bod))
|
|
1310 (if (not (looking-at c-symbol-key))
|
|
1311 (c-add-syntax 'objc-method-call-cont containing-sexp))
|
|
1312 )))
|
|
1313 ;; CASE 6E: we are looking at an arglist continuation line,
|
|
1314 ;; but the preceding argument is on the same line as the
|
|
1315 ;; opening paren. This case includes multi-line
|
|
1316 ;; mathematical paren groupings, but we could be on a
|
|
1317 ;; for-list continuation line
|
|
1318 ((and (save-excursion
|
|
1319 (goto-char (1+ containing-sexp))
|
|
1320 (skip-chars-forward " \t")
|
|
1321 (not (eolp)))
|
|
1322 (save-excursion
|
|
1323 (c-beginning-of-statement-1 lim)
|
|
1324 (skip-chars-backward " \t([")
|
|
1325 (<= (point) containing-sexp)))
|
|
1326 (goto-char containing-sexp)
|
|
1327 (c-add-syntax 'arglist-cont-nonempty (c-point 'boi)))
|
|
1328 ;; CASE 6F: we are looking at just a normal arglist
|
|
1329 ;; continuation line
|
|
1330 (t (c-beginning-of-statement-1 containing-sexp)
|
|
1331 (forward-char 1)
|
|
1332 (c-forward-syntactic-ws indent-point)
|
|
1333 (c-add-syntax 'arglist-cont (c-point 'boi)))
|
|
1334 ))
|
|
1335 ;; CASE 7: func-local multi-inheritance line
|
|
1336 ((and c-baseclass-key
|
|
1337 (save-excursion
|
|
1338 (goto-char indent-point)
|
|
1339 (skip-chars-forward " \t")
|
|
1340 (looking-at c-baseclass-key)))
|
|
1341 (goto-char indent-point)
|
|
1342 (skip-chars-forward " \t")
|
|
1343 (cond
|
|
1344 ;; CASE 7A: non-hanging colon on an inher intro
|
171
|
1345 ((eq char-after-ip ?:)
|
165
|
1346 (c-backward-syntactic-ws lim)
|
|
1347 (c-add-syntax 'inher-intro (c-point 'boi)))
|
|
1348 ;; CASE 7B: hanging colon on an inher intro
|
171
|
1349 ((eq char-before-ip ?:)
|
165
|
1350 (c-add-syntax 'inher-intro (c-point 'boi)))
|
|
1351 ;; CASE 7C: a continued inheritance line
|
|
1352 (t
|
|
1353 (c-beginning-of-inheritance-list lim)
|
|
1354 (c-add-syntax 'inher-cont (point))
|
|
1355 )))
|
|
1356 ;; CASE 8: we are inside a brace-list
|
|
1357 ((setq placeholder (c-inside-bracelist-p containing-sexp state))
|
|
1358 (cond
|
|
1359 ;; CASE 8A: brace-list-close brace
|
171
|
1360 ((and (eq char-after-ip ?})
|
165
|
1361 (c-safe (progn (forward-char 1)
|
|
1362 (backward-sexp 1)
|
|
1363 t))
|
|
1364 (= (point) containing-sexp))
|
|
1365 (c-add-syntax 'brace-list-close (c-point 'boi)))
|
|
1366 ;; CASE 8B: we're looking at the first line in a brace-list
|
|
1367 ((save-excursion
|
|
1368 (goto-char indent-point)
|
|
1369 (c-backward-syntactic-ws containing-sexp)
|
|
1370 (= (point) (1+ containing-sexp)))
|
|
1371 (goto-char containing-sexp)
|
|
1372 (c-add-syntax 'brace-list-intro (c-point 'boi))
|
|
1373 )
|
|
1374 ;;)) ; end CASE 8B
|
|
1375 ;; CASE 8C: this is just a later brace-list-entry
|
|
1376 (t (goto-char (1+ containing-sexp))
|
|
1377 (c-forward-syntactic-ws indent-point)
|
171
|
1378 (if (eq char-after-ip ?{)
|
165
|
1379 (c-add-syntax 'brace-list-open (point))
|
|
1380 (c-add-syntax 'brace-list-entry (point))
|
|
1381 )) ; end CASE 8C
|
|
1382 )) ; end CASE 8
|
|
1383 ;; CASE 9: A continued statement
|
|
1384 ((and (not (memq char-before-ip '(?\; ?} ?:)))
|
|
1385 (> (point)
|
|
1386 (save-excursion
|
|
1387 (c-beginning-of-statement-1 containing-sexp)
|
|
1388 (setq placeholder (point))))
|
|
1389 (/= placeholder containing-sexp))
|
|
1390 (goto-char indent-point)
|
|
1391 (skip-chars-forward " \t")
|
|
1392 (let ((after-cond-placeholder
|
|
1393 (save-excursion
|
|
1394 (goto-char placeholder)
|
|
1395 (if (looking-at c-conditional-key)
|
|
1396 (progn
|
|
1397 (c-safe (c-skip-conditional))
|
|
1398 (c-forward-syntactic-ws)
|
171
|
1399 (if (eq (char-after) ?\;)
|
165
|
1400 (progn
|
|
1401 (forward-char 1)
|
|
1402 (c-forward-syntactic-ws)))
|
|
1403 (point))
|
|
1404 nil))))
|
|
1405 (cond
|
|
1406 ;; CASE 9A: substatement
|
|
1407 ((and after-cond-placeholder
|
|
1408 (>= after-cond-placeholder indent-point))
|
|
1409 (goto-char placeholder)
|
171
|
1410 (if (eq char-after-ip ?{)
|
165
|
1411 (c-add-syntax 'substatement-open (c-point 'boi))
|
|
1412 (c-add-syntax 'substatement (c-point 'boi))))
|
|
1413 ;; CASE 9B: open braces for class or brace-lists
|
171
|
1414 ((eq char-after-ip ?{)
|
165
|
1415 (cond
|
|
1416 ;; CASE 9B.1: class-open
|
|
1417 ((save-excursion
|
|
1418 (goto-char indent-point)
|
|
1419 (skip-chars-forward " \t{")
|
|
1420 (let ((decl (c-search-uplist-for-classkey (c-parse-state))))
|
|
1421 (and decl
|
|
1422 (setq placeholder (aref decl 0)))
|
|
1423 ))
|
|
1424 (c-add-syntax 'class-open placeholder))
|
|
1425 ;; CASE 9B.2: brace-list-open
|
|
1426 ((or (save-excursion
|
|
1427 (goto-char placeholder)
|
|
1428 (looking-at "\\<enum\\>"))
|
171
|
1429 (eq char-before-ip ?=))
|
165
|
1430 (c-add-syntax 'brace-list-open placeholder))
|
|
1431 ;; CASE 9B.3: catch-all for unknown construct.
|
|
1432 (t
|
|
1433 ;; Can and should I add an extensibility hook here?
|
|
1434 ;; Something like c-recognize-hook so support for
|
|
1435 ;; unknown constructs could be added. It's probably a
|
|
1436 ;; losing proposition, so I dunno.
|
|
1437 (goto-char placeholder)
|
|
1438 (c-add-syntax 'statement-cont (c-point 'boi))
|
|
1439 (c-add-syntax 'block-open))
|
|
1440 ))
|
|
1441 ;; CASE 9C: iostream insertion or extraction operator
|
|
1442 ((looking-at "<<\\|>>")
|
|
1443 (goto-char placeholder)
|
|
1444 (and after-cond-placeholder
|
|
1445 (goto-char after-cond-placeholder))
|
|
1446 (while (and (re-search-forward "<<\\|>>" indent-point 'move)
|
|
1447 (c-in-literal placeholder)))
|
|
1448 ;; if we ended up at indent-point, then the first
|
|
1449 ;; streamop is on a separate line. Indent the line like
|
|
1450 ;; a statement-cont instead
|
|
1451 (if (/= (point) indent-point)
|
|
1452 (c-add-syntax 'stream-op (c-point 'boi))
|
|
1453 (c-backward-syntactic-ws lim)
|
|
1454 (c-add-syntax 'statement-cont (c-point 'boi))))
|
|
1455 ;; CASE 9D: continued statement. find the accurate
|
|
1456 ;; beginning of statement or substatement
|
|
1457 (t
|
|
1458 (c-beginning-of-statement-1 after-cond-placeholder)
|
|
1459 ;; KLUDGE ALERT! c-beginning-of-statement-1 can leave
|
|
1460 ;; us before the lim we're passing in. It should be
|
|
1461 ;; fixed, but I'm worried about side-effects at this
|
|
1462 ;; late date. Fix for v5.
|
|
1463 (goto-char (or (and after-cond-placeholder
|
|
1464 (max after-cond-placeholder (point)))
|
|
1465 (point)))
|
|
1466 (c-add-syntax 'statement-cont (point)))
|
|
1467 )))
|
|
1468 ;; CASE 10: an else clause?
|
|
1469 ((looking-at "\\<else\\>[^_]")
|
|
1470 (c-backward-to-start-of-if containing-sexp)
|
|
1471 (c-add-syntax 'else-clause (c-point 'boi)))
|
|
1472 ;; CASE 11: Statement. But what kind? Lets see if its a
|
|
1473 ;; while closure of a do/while construct
|
|
1474 ((progn
|
|
1475 (goto-char indent-point)
|
|
1476 (skip-chars-forward " \t")
|
|
1477 (and (looking-at "while\\b[^_]")
|
|
1478 (save-excursion
|
|
1479 (c-backward-to-start-of-do containing-sexp)
|
|
1480 (setq placeholder (point))
|
|
1481 (looking-at "do\\b[^_]"))
|
|
1482 ))
|
|
1483 (c-add-syntax 'do-while-closure placeholder))
|
|
1484 ;; CASE 12: A case or default label
|
|
1485 ((looking-at c-switch-label-key)
|
|
1486 (goto-char containing-sexp)
|
|
1487 ;; check for hanging braces
|
|
1488 (if (/= (point) (c-point 'boi))
|
|
1489 (forward-sexp -1))
|
|
1490 (c-add-syntax 'case-label (c-point 'boi)))
|
|
1491 ;; CASE 13: any other label
|
|
1492 ((looking-at c-label-key)
|
|
1493 (goto-char containing-sexp)
|
|
1494 (c-add-syntax 'label (c-point 'boi)))
|
|
1495 ;; CASE 14: block close brace, possibly closing the defun or
|
|
1496 ;; the class
|
171
|
1497 ((eq char-after-ip ?})
|
165
|
1498 (let* ((lim (c-safe-position containing-sexp fullstate))
|
|
1499 (relpos (save-excursion
|
|
1500 (goto-char containing-sexp)
|
|
1501 (if (/= (point) (c-point 'boi))
|
|
1502 (c-beginning-of-statement-1 lim))
|
|
1503 (c-point 'boi))))
|
|
1504 (cond
|
|
1505 ;; CASE 14A: does this close an inline?
|
|
1506 ((let ((inclass-p (progn
|
|
1507 (goto-char containing-sexp)
|
|
1508 (c-search-uplist-for-classkey state))))
|
|
1509 ;; inextern-p in higher level let*
|
|
1510 (setq inextern-p (and inclass-p
|
|
1511 (progn
|
|
1512 (goto-char (aref inclass-p 0))
|
|
1513 (looking-at "extern[^_]"))))
|
|
1514 (and inclass-p (not inextern-p)))
|
|
1515 (c-add-syntax 'inline-close relpos))
|
|
1516 ;; CASE 14B: if there an enclosing brace that hasn't
|
|
1517 ;; been narrowed out by a class, then this is a
|
|
1518 ;; block-close
|
|
1519 ((and (not inextern-p)
|
|
1520 (c-most-enclosing-brace state))
|
|
1521 (c-add-syntax 'block-close relpos))
|
|
1522 ;; CASE 14C: find out whether we're closing a top-level
|
|
1523 ;; class or a defun
|
|
1524 (t
|
|
1525 (save-restriction
|
|
1526 (narrow-to-region (point-min) indent-point)
|
|
1527 (let ((decl (c-search-uplist-for-classkey (c-parse-state))))
|
|
1528 (if decl
|
|
1529 (c-add-syntax 'class-close (aref decl 0))
|
|
1530 (c-add-syntax 'defun-close relpos)))))
|
|
1531 )))
|
|
1532 ;; CASE 15: statement catchall
|
|
1533 (t
|
|
1534 ;; we know its a statement, but we need to find out if it is
|
|
1535 ;; the first statement in a block
|
|
1536 (goto-char containing-sexp)
|
|
1537 (forward-char 1)
|
|
1538 (c-forward-syntactic-ws indent-point)
|
|
1539 ;; now skip forward past any case/default clauses we might find.
|
|
1540 (while (or (c-skip-case-statement-forward fullstate indent-point)
|
|
1541 (and (looking-at c-switch-label-key)
|
|
1542 (not inswitch-p)))
|
|
1543 (setq inswitch-p t))
|
|
1544 ;; we want to ignore non-case labels when skipping forward
|
|
1545 (while (and (looking-at c-label-key)
|
|
1546 (goto-char (match-end 0)))
|
|
1547 (c-forward-syntactic-ws indent-point))
|
|
1548 (cond
|
|
1549 ;; CASE 15A: we are inside a case/default clause inside a
|
|
1550 ;; switch statement. find out if we are at the statement
|
|
1551 ;; just after the case/default label.
|
|
1552 ((and inswitch-p
|
|
1553 (progn
|
|
1554 (goto-char indent-point)
|
|
1555 (c-backward-syntactic-ws containing-sexp)
|
|
1556 (back-to-indentation)
|
|
1557 (setq placeholder (point))
|
|
1558 (looking-at c-switch-label-key)))
|
|
1559 (goto-char indent-point)
|
|
1560 (skip-chars-forward " \t")
|
171
|
1561 (if (eq (char-after) ?{)
|
165
|
1562 (c-add-syntax 'statement-case-open placeholder)
|
|
1563 (c-add-syntax 'statement-case-intro placeholder)))
|
|
1564 ;; CASE 15B: continued statement
|
171
|
1565 ((eq char-before-ip ?,)
|
165
|
1566 (c-add-syntax 'statement-cont (c-point 'boi)))
|
|
1567 ;; CASE 15C: a question/colon construct? But make sure
|
|
1568 ;; what came before was not a label, and what comes after
|
|
1569 ;; is not a globally scoped function call!
|
|
1570 ((or (and (memq char-before-ip '(?: ??))
|
|
1571 (save-excursion
|
|
1572 (goto-char indent-point)
|
|
1573 (c-backward-syntactic-ws lim)
|
|
1574 (back-to-indentation)
|
|
1575 (not (looking-at c-label-key))))
|
|
1576 (and (memq char-after-ip '(?: ??))
|
|
1577 (save-excursion
|
|
1578 (goto-char indent-point)
|
|
1579 (skip-chars-forward " \t")
|
|
1580 ;; watch out for scope operator
|
|
1581 (not (looking-at "::")))))
|
|
1582 (c-add-syntax 'statement-cont (c-point 'boi)))
|
|
1583 ;; CASE 15D: any old statement
|
|
1584 ((< (point) indent-point)
|
|
1585 (let ((safepos (c-most-enclosing-brace fullstate))
|
|
1586 relpos done)
|
|
1587 (goto-char indent-point)
|
|
1588 (c-beginning-of-statement-1 safepos)
|
|
1589 ;; It is possible we're on the brace that opens a nested
|
|
1590 ;; function.
|
171
|
1591 (if (and (eq (char-after) ?{)
|
165
|
1592 (save-excursion
|
|
1593 (c-backward-syntactic-ws safepos)
|
171
|
1594 (not (eq (char-before) ?\;))))
|
165
|
1595 (c-beginning-of-statement-1 safepos))
|
|
1596 (if (and inswitch-p
|
|
1597 (looking-at c-switch-label-key))
|
|
1598 (progn
|
|
1599 (goto-char placeholder)
|
|
1600 (end-of-line)
|
|
1601 (forward-sexp -1)))
|
|
1602 (setq relpos (c-point 'boi))
|
|
1603 (while (and (not done)
|
|
1604 (<= safepos (point))
|
|
1605 (/= relpos (point)))
|
|
1606 (c-beginning-of-statement-1 safepos)
|
|
1607 (if (= relpos (c-point 'boi))
|
|
1608 (setq done t))
|
|
1609 (setq relpos (c-point 'boi)))
|
|
1610 (c-add-syntax 'statement relpos)
|
171
|
1611 (if (eq char-after-ip ?{)
|
165
|
1612 (c-add-syntax 'block-open))))
|
|
1613 ;; CASE 15E: first statement in an inline, or first
|
|
1614 ;; statement in a top-level defun. we can tell this is it
|
|
1615 ;; if there are no enclosing braces that haven't been
|
|
1616 ;; narrowed out by a class (i.e. don't use bod here!)
|
|
1617 ((save-excursion
|
|
1618 (save-restriction
|
|
1619 (widen)
|
|
1620 (goto-char containing-sexp)
|
|
1621 (c-narrow-out-enclosing-class state containing-sexp)
|
|
1622 (not (c-most-enclosing-brace state))))
|
|
1623 (goto-char containing-sexp)
|
|
1624 ;; if not at boi, then defun-opening braces are hung on
|
|
1625 ;; right side, so we need a different relpos
|
|
1626 (if (/= (point) (c-point 'boi))
|
|
1627 (progn
|
|
1628 (c-backward-syntactic-ws)
|
171
|
1629 (c-safe (forward-sexp (if (eq (char-before) ?\))
|
165
|
1630 -1 -2)))
|
|
1631 ;; looking at a Java throws clause following a
|
|
1632 ;; method's parameter list
|
|
1633 (c-beginning-of-statement-1)
|
|
1634 ))
|
|
1635 (c-add-syntax 'defun-block-intro (c-point 'boi)))
|
|
1636 ;; CASE 15F: first statement in a block
|
|
1637 (t (goto-char containing-sexp)
|
|
1638 (if (/= (point) (c-point 'boi))
|
|
1639 (c-beginning-of-statement-1
|
|
1640 (if (= (point) lim)
|
|
1641 (c-safe-position (point) state) lim)))
|
|
1642 (c-add-syntax 'statement-block-intro (c-point 'boi))
|
171
|
1643 (if (eq char-after-ip ?{)
|
165
|
1644 (c-add-syntax 'block-open)))
|
|
1645 ))
|
|
1646 )
|
|
1647
|
|
1648 ;; now we need to look at any modifiers
|
|
1649 (goto-char indent-point)
|
|
1650 (skip-chars-forward " \t")
|
|
1651 ;; are we looking at a comment only line?
|
|
1652 (if (looking-at c-comment-start-regexp)
|
|
1653 (c-add-syntax 'comment-intro))
|
|
1654 ;; we might want to give additional offset to friends (in C++).
|
|
1655 (if (and (eq major-mode 'c++-mode)
|
|
1656 (looking-at c-C++-friend-key))
|
|
1657 (c-add-syntax 'friend))
|
|
1658 ;; return the syntax
|
|
1659 syntax))))
|
|
1660
|
|
1661
|
171
|
1662 (defun c-echo-parsing-error ()
|
|
1663 (if (not c-parsing-error)
|
|
1664 nil
|
|
1665 (message "unbalanced close brace at bufpos %d -- INDENTATION IS SUSPECT!"
|
|
1666 c-parsing-error)
|
|
1667 (ding))
|
|
1668 c-parsing-error)
|
|
1669
|
165
|
1670 ;; indent via syntactic language elements
|
|
1671 (defun c-indent-line (&optional syntax)
|
|
1672 ;; indent the current line as C/C++/ObjC code. Optional SYNTAX is the
|
|
1673 ;; syntactic information for the current line. Returns the amount of
|
|
1674 ;; indentation change
|
|
1675 (let* ((c-syntactic-context (or syntax (c-guess-basic-syntax)))
|
|
1676 (pos (- (point-max) (point)))
|
|
1677 (indent (apply '+ (mapcar 'c-get-offset c-syntactic-context)))
|
|
1678 (shift-amt (- (current-indentation) indent)))
|
|
1679 (and c-echo-syntactic-information-p
|
171
|
1680 (not (c-echo-parsing-error))
|
165
|
1681 (message "syntax: %s, indent= %d" c-syntactic-context indent))
|
|
1682 (if (zerop shift-amt)
|
|
1683 nil
|
|
1684 (delete-region (c-point 'bol) (c-point 'boi))
|
|
1685 (beginning-of-line)
|
|
1686 (indent-to indent))
|
|
1687 (if (< (point) (c-point 'boi))
|
|
1688 (back-to-indentation)
|
|
1689 ;; If initial point was within line's indentation, position after
|
|
1690 ;; the indentation. Else stay at same point in text.
|
|
1691 (if (> (- (point-max) pos) (point))
|
|
1692 (goto-char (- (point-max) pos)))
|
|
1693 )
|
|
1694 (run-hooks 'c-special-indent-hook)
|
|
1695 shift-amt))
|
|
1696
|
|
1697 (defun c-show-syntactic-information (arg)
|
|
1698 "Show syntactic information for current line.
|
|
1699 With universal argument, inserts the analysis as a comment on that line."
|
|
1700 (interactive "P")
|
|
1701 (let ((syntax (c-guess-basic-syntax)))
|
|
1702 (if (not (consp arg))
|
171
|
1703 (if (not (c-echo-parsing-error))
|
|
1704 (message "syntactic analysis: %s" syntax))
|
165
|
1705 (indent-for-comment)
|
|
1706 (insert (format "%s" syntax))
|
|
1707 ))
|
|
1708 (c-keep-region-active))
|
|
1709
|
|
1710
|
|
1711 (provide 'cc-engine)
|
|
1712 ;;; cc-engine.el ends here
|