Update copyright year to 2015
[emacs.git] / lisp / progmodes / cc-defs.el
blob2ea566a7a258954c2697008b56bf8294282e5f59
1 ;;; cc-defs.el --- compile time definitions for CC Mode
3 ;; Copyright (C) 1985, 1987, 1992-2015 Free Software Foundation, Inc.
5 ;; Authors: 2003- Alan Mackenzie
6 ;; 1998- Martin Stjernholm
7 ;; 1992-1999 Barry A. Warsaw
8 ;; 1987 Dave Detlefs
9 ;; 1987 Stewart Clamen
10 ;; 1985 Richard M. Stallman
11 ;; Maintainer: bug-cc-mode@gnu.org
12 ;; Created: 22-Apr-1997 (split from cc-mode.el)
13 ;; Keywords: c languages
14 ;; Package: cc-mode
16 ;; This file is part of GNU Emacs.
18 ;; GNU Emacs is free software: you can redistribute it and/or modify
19 ;; it under the terms of the GNU General Public License as published by
20 ;; the Free Software Foundation, either version 3 of the License, or
21 ;; (at your option) any later version.
23 ;; GNU Emacs is distributed in the hope that it will be useful,
24 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
25 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26 ;; GNU General Public License for more details.
28 ;; You should have received a copy of the GNU General Public License
29 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
31 ;;; Commentary:
33 ;; This file contains macros, defsubsts, and various other things that
34 ;; must be loaded early both during compilation and at runtime.
36 ;;; Code:
38 (eval-when-compile
39 (let ((load-path
40 (if (and (boundp 'byte-compile-dest-file)
41 (stringp byte-compile-dest-file))
42 (cons (file-name-directory byte-compile-dest-file) load-path)
43 load-path)))
44 (load "cc-bytecomp" nil t)))
46 (eval-when-compile (require 'cl)) ; was (cc-external-require 'cl). ACM 2005/11/29.
47 (cc-external-require 'regexp-opt)
49 ;; Silence the compiler.
50 (cc-bytecomp-defvar c-enable-xemacs-performance-kludge-p) ; In cc-vars.el
51 (cc-bytecomp-defun region-active-p) ; XEmacs
52 (cc-bytecomp-defvar mark-active) ; Emacs
53 (cc-bytecomp-defvar deactivate-mark) ; Emacs
54 (cc-bytecomp-defvar inhibit-point-motion-hooks) ; Emacs
55 (cc-bytecomp-defvar parse-sexp-lookup-properties) ; Emacs
56 (cc-bytecomp-defvar text-property-default-nonsticky) ; Emacs 21
57 (cc-bytecomp-defun string-to-syntax) ; Emacs 21
60 ;; cc-fix.el contains compatibility macros that should be used if
61 ;; needed.
62 (eval-and-compile
63 (if (or (/= (regexp-opt-depth "\\(\\(\\)\\)") 2)
64 (not (fboundp 'push)))
65 (cc-load "cc-fix")))
67 (when (featurep 'xemacs) ; There is now (2005/12) code in GNU Emacs CVS
68 ; to make the call to f-l-c-k throw an error.
69 (eval-after-load "font-lock"
70 '(if (and (not (featurep 'cc-fix)) ; only load the file once.
71 (let (font-lock-keywords)
72 (font-lock-compile-keywords '("\\<\\>"))
73 font-lock-keywords)) ; did the previous call foul this up?
74 (load "cc-fix"))))
76 ;; The above takes care of the delayed loading, but this is necessary
77 ;; to ensure correct byte compilation.
78 (eval-when-compile
79 (if (and (featurep 'xemacs)
80 (not (featurep 'cc-fix))
81 (progn
82 (require 'font-lock)
83 (let (font-lock-keywords)
84 (font-lock-compile-keywords '("\\<\\>"))
85 font-lock-keywords)))
86 (cc-load "cc-fix")))
88 ;; XEmacs 21.4 doesn't have `delete-dups'.
89 (eval-and-compile
90 (if (and (not (fboundp 'delete-dups))
91 (not (featurep 'cc-fix)))
92 (cc-load "cc-fix")))
94 ;;; Variables also used at compile time.
96 (defconst c-version "5.33"
97 "CC Mode version number.")
99 (defconst c-version-sym (intern c-version))
100 ;; A little more compact and faster in comparisons.
102 (defvar c-buffer-is-cc-mode nil
103 "Non-nil for all buffers with a major mode derived from CC Mode.
104 Otherwise, this variable is nil. I.e. this variable is non-nil for
105 `c-mode', `c++-mode', `objc-mode', `java-mode', `idl-mode',
106 `pike-mode', `awk-mode', and any other non-CC Mode mode that calls
107 `c-initialize-cc-mode'. The value is the mode symbol itself
108 \(i.e. `c-mode' etc) of the original CC Mode mode, or just t if it's
109 not known.")
110 (make-variable-buffer-local 'c-buffer-is-cc-mode)
112 ;; Have to make `c-buffer-is-cc-mode' permanently local so that it
113 ;; survives the initialization of the derived mode.
114 (put 'c-buffer-is-cc-mode 'permanent-local t)
117 ;; The following is used below during compilation.
118 (eval-and-compile
119 (defvar c-inside-eval-when-compile nil)
121 (defmacro cc-eval-when-compile (&rest body)
122 "Like `progn', but evaluates the body at compile time.
123 The result of the body appears to the compiler as a quoted constant.
125 This variant works around bugs in `eval-when-compile' in various
126 \(X)Emacs versions. See cc-defs.el for details."
128 (if c-inside-eval-when-compile
129 ;; XEmacs 21.4.6 has a bug in `eval-when-compile' in that it
130 ;; evaluates its body at macro expansion time if it's nested
131 ;; inside another `eval-when-compile'. So we use a dynamically
132 ;; bound variable to avoid nesting them.
133 `(progn ,@body)
135 `(eval-when-compile
136 ;; In all (X)Emacsen so far, `eval-when-compile' byte compiles
137 ;; its contents before evaluating it. That can cause forms to
138 ;; be compiled in situations they aren't intended to be
139 ;; compiled.
141 ;; Example: It's not possible to defsubst a primitive, e.g. the
142 ;; following will produce an error (in any emacs flavor), since
143 ;; `nthcdr' is a primitive function that's handled specially by
144 ;; the byte compiler and thus can't be redefined:
146 ;; (defsubst nthcdr (val) val)
148 ;; `defsubst', like `defmacro', needs to be evaluated at
149 ;; compile time, so this will produce an error during byte
150 ;; compilation.
152 ;; CC Mode occasionally needs to do things like this for
153 ;; cross-emacs compatibility. It therefore uses the following
154 ;; to conditionally do a `defsubst':
156 ;; (eval-when-compile
157 ;; (if (not (fboundp 'foo))
158 ;; (defsubst foo ...)))
160 ;; But `eval-when-compile' byte compiles its contents and
161 ;; _then_ evaluates it (in all current emacs versions, up to
162 ;; and including Emacs 20.6 and XEmacs 21.1 as of this
163 ;; writing). So this will still produce an error, since the
164 ;; byte compiler will get to the defsubst anyway. That's
165 ;; arguably a bug because the point with `eval-when-compile' is
166 ;; that it should evaluate rather than compile its contents.
168 ;; We get around it by expanding the body to a quoted
169 ;; constant that we eval. That otoh introduce a problem in
170 ;; that a returned lambda expression doesn't get byte
171 ;; compiled (even if `function' is used).
172 (eval '(let ((c-inside-eval-when-compile t)) ,@body)))))
174 (put 'cc-eval-when-compile 'lisp-indent-hook 0))
176 (eval-and-compile
177 (defalias 'c--macroexpand-all
178 (if (fboundp 'macroexpand-all)
179 'macroexpand-all 'cl-macroexpand-all)))
181 ;;; Macros.
183 (defmacro c-point (position &optional point)
184 "Return the value of certain commonly referenced POSITIONs relative to POINT.
185 The current point is used if POINT isn't specified. POSITION can be
186 one of the following symbols:
188 `bol' -- beginning of line
189 `eol' -- end of line
190 `bod' -- beginning of defun
191 `eod' -- end of defun
192 `boi' -- beginning of indentation
193 `ionl' -- indentation of next line
194 `iopl' -- indentation of previous line
195 `bonl' -- beginning of next line
196 `eonl' -- end of next line
197 `bopl' -- beginning of previous line
198 `eopl' -- end of previous line
199 `bosws' -- beginning of syntactic whitespace
200 `eosws' -- end of syntactic whitespace
202 If the referenced position doesn't exist, the closest accessible point
203 to it is returned. This function does not modify the point or the mark."
205 (if (eq (car-safe position) 'quote)
206 (let ((position (eval position)))
207 (cond
209 ((eq position 'bol)
210 (if (and (cc-bytecomp-fboundp 'line-beginning-position) (not point))
211 `(line-beginning-position)
212 `(save-excursion
213 ,@(if point `((goto-char ,point)))
214 (beginning-of-line)
215 (point))))
217 ((eq position 'eol)
218 (if (and (cc-bytecomp-fboundp 'line-end-position) (not point))
219 `(line-end-position)
220 `(save-excursion
221 ,@(if point `((goto-char ,point)))
222 (end-of-line)
223 (point))))
225 ((eq position 'boi)
226 `(save-excursion
227 ,@(if point `((goto-char ,point)))
228 (back-to-indentation)
229 (point)))
231 ((eq position 'bod)
232 `(save-excursion
233 ,@(if point `((goto-char ,point)))
234 (c-beginning-of-defun-1)
235 (point)))
237 ((eq position 'eod)
238 `(save-excursion
239 ,@(if point `((goto-char ,point)))
240 (c-end-of-defun-1)
241 (point)))
243 ((eq position 'bopl)
244 (if (and (cc-bytecomp-fboundp 'line-beginning-position) (not point))
245 `(line-beginning-position 0)
246 `(save-excursion
247 ,@(if point `((goto-char ,point)))
248 (forward-line -1)
249 (point))))
251 ((eq position 'bonl)
252 (if (and (cc-bytecomp-fboundp 'line-beginning-position) (not point))
253 `(line-beginning-position 2)
254 `(save-excursion
255 ,@(if point `((goto-char ,point)))
256 (forward-line 1)
257 (point))))
259 ((eq position 'eopl)
260 (if (and (cc-bytecomp-fboundp 'line-end-position) (not point))
261 `(line-end-position 0)
262 `(save-excursion
263 ,@(if point `((goto-char ,point)))
264 (beginning-of-line)
265 (or (bobp) (backward-char))
266 (point))))
268 ((eq position 'eonl)
269 (if (and (cc-bytecomp-fboundp 'line-end-position) (not point))
270 `(line-end-position 2)
271 `(save-excursion
272 ,@(if point `((goto-char ,point)))
273 (forward-line 1)
274 (end-of-line)
275 (point))))
277 ((eq position 'iopl)
278 `(save-excursion
279 ,@(if point `((goto-char ,point)))
280 (forward-line -1)
281 (back-to-indentation)
282 (point)))
284 ((eq position 'ionl)
285 `(save-excursion
286 ,@(if point `((goto-char ,point)))
287 (forward-line 1)
288 (back-to-indentation)
289 (point)))
291 ((eq position 'bosws)
292 `(save-excursion
293 ,@(if point `((goto-char ,point)))
294 (c-backward-syntactic-ws)
295 (point)))
297 ((eq position 'eosws)
298 `(save-excursion
299 ,@(if point `((goto-char ,point)))
300 (c-forward-syntactic-ws)
301 (point)))
303 (t (error "Unknown buffer position requested: %s" position))))
305 ;; The bulk of this should perhaps be in a function to avoid large
306 ;; expansions, but this case is not used anywhere in CC Mode (and
307 ;; probably not anywhere else either) so we only have it to be on
308 ;; the safe side.
309 (message "Warning: c-point long expansion")
310 `(save-excursion
311 ,@(if point `((goto-char ,point)))
312 (let ((position ,position))
313 (cond
314 ((eq position 'bol) (beginning-of-line))
315 ((eq position 'eol) (end-of-line))
316 ((eq position 'boi) (back-to-indentation))
317 ((eq position 'bod) (c-beginning-of-defun-1))
318 ((eq position 'eod) (c-end-of-defun-1))
319 ((eq position 'bopl) (forward-line -1))
320 ((eq position 'bonl) (forward-line 1))
321 ((eq position 'eopl) (progn
322 (beginning-of-line)
323 (or (bobp) (backward-char))))
324 ((eq position 'eonl) (progn
325 (forward-line 1)
326 (end-of-line)))
327 ((eq position 'iopl) (progn
328 (forward-line -1)
329 (back-to-indentation)))
330 ((eq position 'ionl) (progn
331 (forward-line 1)
332 (back-to-indentation)))
333 ((eq position 'bosws) (c-backward-syntactic-ws))
334 ((eq position 'eosws) (c-forward-syntactic-ws))
335 (t (error "Unknown buffer position requested: %s" position))))
336 (point))))
338 (eval-and-compile
339 ;; Constant to decide at compilation time whether to use category
340 ;; properties. Currently (2010-03) they're available only on GNU Emacs.
341 (defconst c-use-category
342 (with-temp-buffer
343 (let ((parse-sexp-lookup-properties t)
344 (lookup-syntax-properties t))
345 (set-syntax-table (make-syntax-table))
346 (insert "<()>")
347 (put-text-property (point-min) (1+ (point-min))
348 'category 'c-<-as-paren-syntax)
349 (put-text-property (+ 3 (point-min)) (+ 4 (point-min))
350 'category 'c->-as-paren-syntax)
351 (goto-char (point-min))
352 (forward-sexp)
353 (= (point) (+ 4 (point-min)))))))
355 (defvar c-use-extents)
357 (defmacro c-next-single-property-change (position prop &optional object limit)
358 ;; See the doc string for either of the defuns expanded to.
359 (if (and c-use-extents
360 (fboundp 'next-single-char-property-change))
361 ;; XEmacs >= 2005-01-25
362 `(next-single-char-property-change ,position ,prop ,object ,limit)
363 ;; Emacs and earlier XEmacs
364 `(next-single-property-change ,position ,prop ,object ,limit)))
366 (defmacro c-region-is-active-p ()
367 ;; Return t when the region is active. The determination of region
368 ;; activeness is different in both Emacs and XEmacs.
369 (if (cc-bytecomp-fboundp 'region-active-p)
370 ;; XEmacs.
371 '(region-active-p)
372 ;; Old Emacs.
373 'mark-active))
375 (defmacro c-set-region-active (activate)
376 ;; Activate the region if ACTIVE is non-nil, deactivate it
377 ;; otherwise. Covers the differences between Emacs and XEmacs.
378 (if (fboundp 'zmacs-activate-region)
379 ;; XEmacs.
380 `(if ,activate
381 (zmacs-activate-region)
382 (zmacs-deactivate-region))
383 ;; Emacs.
384 `(setq mark-active ,activate)))
386 (defmacro c-delete-and-extract-region (start end)
387 "Delete the text between START and END and return it."
388 (if (cc-bytecomp-fboundp 'delete-and-extract-region)
389 ;; Emacs 21.1 and later
390 `(delete-and-extract-region ,start ,end)
391 ;; XEmacs and Emacs 20.x
392 `(prog1
393 (buffer-substring ,start ,end)
394 (delete-region ,start ,end))))
396 (defmacro c-safe (&rest body)
397 ;; safely execute BODY, return nil if an error occurred
398 `(condition-case nil
399 (progn ,@body)
400 (error nil)))
401 (put 'c-safe 'lisp-indent-function 0)
403 (defmacro c-int-to-char (integer)
404 ;; In Emacs, a character is an integer. In XEmacs, a character is a
405 ;; type distinct from an integer. Sometimes we need to convert integers to
406 ;; characters. `c-int-to-char' makes this conversion, if necessary.
407 (if (fboundp 'int-to-char)
408 `(int-to-char ,integer)
409 integer))
411 (defmacro c-last-command-char ()
412 ;; The last character just typed. Note that `last-command-event' exists in
413 ;; both Emacs and XEmacs, but with confusingly different meanings.
414 (if (featurep 'xemacs)
415 'last-command-char
416 'last-command-event))
418 (defmacro c-sentence-end ()
419 ;; Get the regular expression `sentence-end'.
420 (if (cc-bytecomp-fboundp 'sentence-end)
421 ;; Emacs 22:
422 `(sentence-end)
423 ;; Emacs <22 + XEmacs
424 `sentence-end))
426 (defmacro c-default-value-sentence-end ()
427 ;; Get the default value of the variable sentence end.
428 (if (cc-bytecomp-fboundp 'sentence-end)
429 ;; Emacs 22:
430 `(let (sentence-end) (sentence-end))
431 ;; Emacs <22 + XEmacs
432 `(default-value 'sentence-end)))
434 ;; The following is essentially `save-buffer-state' from lazy-lock.el.
435 ;; It ought to be a standard macro.
436 (defmacro c-save-buffer-state (varlist &rest body)
437 "Bind variables according to VARLIST (in `let*' style) and eval BODY,
438 then restore the buffer state under the assumption that no significant
439 modification has been made in BODY. A change is considered
440 significant if it affects the buffer text in any way that isn't
441 completely restored again. Changes in text properties like `face' or
442 `syntax-table' are considered insignificant. This macro allows text
443 properties to be changed, even in a read-only buffer.
445 This macro should be placed around all calculations which set
446 \"insignificant\" text properties in a buffer, even when the buffer is
447 known to be writable. That way, these text properties remain set
448 even if the user undoes the command which set them.
450 This macro should ALWAYS be placed around \"temporary\" internal buffer
451 changes \(like adding a newline to calculate a text-property then
452 deleting it again\), so that the user never sees them on his
453 `buffer-undo-list'. See also `c-tentative-buffer-changes'.
455 However, any user-visible changes to the buffer \(like auto-newlines\)
456 must not be within a `c-save-buffer-state', since the user then
457 wouldn't be able to undo them.
459 The return value is the value of the last form in BODY."
460 `(let* ((modified (buffer-modified-p)) (buffer-undo-list t)
461 (inhibit-read-only t) (inhibit-point-motion-hooks t)
462 before-change-functions after-change-functions
463 deactivate-mark
464 buffer-file-name buffer-file-truename ; Prevent primitives checking
465 ; for file modification
466 ,@varlist)
467 (unwind-protect
468 (progn ,@body)
469 (and (not modified)
470 (buffer-modified-p)
471 (set-buffer-modified-p nil)))))
472 (put 'c-save-buffer-state 'lisp-indent-function 1)
474 (defmacro c-tentative-buffer-changes (&rest body)
475 "Eval BODY and optionally restore the buffer contents to the state it
476 was in before BODY. Any changes are kept if the last form in BODY
477 returns non-nil. Otherwise it's undone using the undo facility, and
478 various other buffer state that might be affected by the changes is
479 restored. That includes the current buffer, point, mark, mark
480 activation \(similar to `save-excursion'), and the modified state.
481 The state is also restored if BODY exits nonlocally.
483 If BODY makes a change that unconditionally is undone then wrap this
484 macro inside `c-save-buffer-state'. That way the change can be done
485 even when the buffer is read-only, and without interference from
486 various buffer change hooks."
487 `(let (-tnt-chng-keep
488 -tnt-chng-state)
489 (unwind-protect
490 ;; Insert an undo boundary for use with `undo-more'. We
491 ;; don't use `undo-boundary' since it doesn't insert one
492 ;; unconditionally.
493 (setq buffer-undo-list (cons nil buffer-undo-list)
494 -tnt-chng-state (c-tnt-chng-record-state)
495 -tnt-chng-keep (progn ,@body))
496 (c-tnt-chng-cleanup -tnt-chng-keep -tnt-chng-state))))
497 (put 'c-tentative-buffer-changes 'lisp-indent-function 0)
499 (defun c-tnt-chng-record-state ()
500 ;; Used internally in `c-tentative-buffer-changes'.
501 (vector buffer-undo-list ; 0
502 (current-buffer) ; 1
503 ;; No need to use markers for the point and mark; if the
504 ;; undo got out of synch we're hosed anyway.
505 (point) ; 2
506 (mark t) ; 3
507 (c-region-is-active-p) ; 4
508 (buffer-modified-p))) ; 5
510 (defun c-tnt-chng-cleanup (keep saved-state)
511 ;; Used internally in `c-tentative-buffer-changes'.
513 (let ((saved-undo-list (elt saved-state 0)))
514 (if (eq buffer-undo-list saved-undo-list)
515 ;; No change was done after all.
516 (setq buffer-undo-list (cdr saved-undo-list))
518 (if keep
519 ;; Find and remove the undo boundary.
520 (let ((p buffer-undo-list))
521 (while (not (eq (cdr p) saved-undo-list))
522 (setq p (cdr p)))
523 (setcdr p (cdr saved-undo-list)))
525 ;; `primitive-undo' will remove the boundary.
526 (setq saved-undo-list (cdr saved-undo-list))
527 (let ((undo-in-progress t))
528 (while (not (eq (setq buffer-undo-list
529 (primitive-undo 1 buffer-undo-list))
530 saved-undo-list))))
532 (when (buffer-live-p (elt saved-state 1))
533 (set-buffer (elt saved-state 1))
534 (goto-char (elt saved-state 2))
535 (set-mark (elt saved-state 3))
536 (c-set-region-active (elt saved-state 4))
537 (and (not (elt saved-state 5))
538 (buffer-modified-p)
539 (set-buffer-modified-p nil)))))))
541 (defmacro c-forward-syntactic-ws (&optional limit)
542 "Forward skip over syntactic whitespace.
543 Syntactic whitespace is defined as whitespace characters, comments,
544 and preprocessor directives. However if point starts inside a comment
545 or preprocessor directive, the content of it is not treated as
546 whitespace.
548 LIMIT sets an upper limit of the forward movement, if specified. If
549 LIMIT or the end of the buffer is reached inside a comment or
550 preprocessor directive, the point will be left there.
552 Note that this function might do hidden buffer changes. See the
553 comment at the start of cc-engine.el for more info."
554 (if limit
555 `(save-restriction
556 (narrow-to-region (point-min) (or ,limit (point-max)))
557 (c-forward-sws))
558 '(c-forward-sws)))
560 (defmacro c-backward-syntactic-ws (&optional limit)
561 "Backward skip over syntactic whitespace.
562 Syntactic whitespace is defined as whitespace characters, comments,
563 and preprocessor directives. However if point starts inside a comment
564 or preprocessor directive, the content of it is not treated as
565 whitespace.
567 LIMIT sets a lower limit of the backward movement, if specified. If
568 LIMIT is reached inside a line comment or preprocessor directive then
569 the point is moved into it past the whitespace at the end.
571 Note that this function might do hidden buffer changes. See the
572 comment at the start of cc-engine.el for more info."
573 (if limit
574 `(save-restriction
575 (narrow-to-region (or ,limit (point-min)) (point-max))
576 (c-backward-sws))
577 '(c-backward-sws)))
579 (defmacro c-forward-sexp (&optional count)
580 "Move forward across COUNT balanced expressions.
581 A negative COUNT means move backward. Signal an error if the move
582 fails for any reason.
584 This is like `forward-sexp' except that it isn't interactive and does
585 not do any user friendly adjustments of the point and that it isn't
586 susceptible to user configurations such as disabling of signals in
587 certain situations."
588 (or count (setq count 1))
589 `(goto-char (scan-sexps (point) ,count)))
591 (defmacro c-backward-sexp (&optional count)
592 "See `c-forward-sexp' and reverse directions."
593 (or count (setq count 1))
594 `(c-forward-sexp ,(if (numberp count) (- count) `(- ,count))))
596 (defmacro c-safe-scan-lists (from count depth &optional limit)
597 "Like `scan-lists' but returns nil instead of signaling errors
598 for unbalanced parens.
600 A limit for the search may be given. FROM is assumed to be on the
601 right side of it."
602 (let ((res (if (featurep 'xemacs)
603 `(scan-lists ,from ,count ,depth nil t)
604 `(c-safe (scan-lists ,from ,count ,depth)))))
605 (if limit
606 `(save-restriction
607 ,(if (numberp count)
608 (if (< count 0)
609 `(narrow-to-region ,limit (point-max))
610 `(narrow-to-region (point-min) ,limit))
611 `(if (< ,count 0)
612 (narrow-to-region ,limit (point-max))
613 (narrow-to-region (point-min) ,limit)))
614 ,res)
615 res)))
618 ;; Wrappers for common scan-lists cases, mainly because it's almost
619 ;; impossible to get a feel for how that function works.
621 (defmacro c-go-list-forward ()
622 "Move backward across one balanced group of parentheses.
624 Return POINT when we succeed, NIL when we fail. In the latter case, leave
625 point unmoved."
626 `(c-safe (let ((endpos (scan-lists (point) 1 0)))
627 (goto-char endpos)
628 endpos)))
630 (defmacro c-go-list-backward ()
631 "Move backward across one balanced group of parentheses.
633 Return POINT when we succeed, NIL when we fail. In the latter case, leave
634 point unmoved."
635 `(c-safe (let ((endpos (scan-lists (point) -1 0)))
636 (goto-char endpos)
637 endpos)))
639 (defmacro c-up-list-forward (&optional pos limit)
640 "Return the first position after the list sexp containing POS,
641 or nil if no such position exists. The point is used if POS is left out.
643 A limit for the search may be given. The start position is assumed to
644 be before it."
645 `(c-safe-scan-lists ,(or pos `(point)) 1 1 ,limit))
647 (defmacro c-up-list-backward (&optional pos limit)
648 "Return the position of the start of the list sexp containing POS,
649 or nil if no such position exists. The point is used if POS is left out.
651 A limit for the search may be given. The start position is assumed to
652 be after it."
653 `(c-safe-scan-lists ,(or pos `(point)) -1 1 ,limit))
655 (defmacro c-down-list-forward (&optional pos limit)
656 "Return the first position inside the first list sexp after POS,
657 or nil if no such position exists. The point is used if POS is left out.
659 A limit for the search may be given. The start position is assumed to
660 be before it."
661 `(c-safe-scan-lists ,(or pos `(point)) 1 -1 ,limit))
663 (defmacro c-down-list-backward (&optional pos limit)
664 "Return the last position inside the last list sexp before POS,
665 or nil if no such position exists. The point is used if POS is left out.
667 A limit for the search may be given. The start position is assumed to
668 be after it."
669 `(c-safe-scan-lists ,(or pos `(point)) -1 -1 ,limit))
671 (defmacro c-go-up-list-forward (&optional pos limit)
672 "Move the point to the first position after the list sexp containing POS,
673 or containing the point if POS is left out. Return t if such a
674 position exists, otherwise nil is returned and the point isn't moved.
676 A limit for the search may be given. The start position is assumed to
677 be before it."
678 (let ((res `(c-safe (goto-char (scan-lists ,(or pos `(point)) 1 1)) t)))
679 (if limit
680 `(save-restriction
681 (narrow-to-region (point-min) ,limit)
682 ,res)
683 res)))
685 (defmacro c-go-up-list-backward (&optional pos limit)
686 "Move the point to the position of the start of the list sexp containing POS,
687 or containing the point if POS is left out. Return t if such a
688 position exists, otherwise nil is returned and the point isn't moved.
690 A limit for the search may be given. The start position is assumed to
691 be after it."
692 (let ((res `(c-safe (goto-char (scan-lists ,(or pos `(point)) -1 1)) t)))
693 (if limit
694 `(save-restriction
695 (narrow-to-region ,limit (point-max))
696 ,res)
697 res)))
699 (defmacro c-go-down-list-forward (&optional pos limit)
700 "Move the point to the first position inside the first list sexp after POS,
701 or before the point if POS is left out. Return t if such a position
702 exists, otherwise nil is returned and the point isn't moved.
704 A limit for the search may be given. The start position is assumed to
705 be before it."
706 (let ((res `(c-safe (goto-char (scan-lists ,(or pos `(point)) 1 -1)) t)))
707 (if limit
708 `(save-restriction
709 (narrow-to-region (point-min) ,limit)
710 ,res)
711 res)))
713 (defmacro c-go-down-list-backward (&optional pos limit)
714 "Move the point to the last position inside the last list sexp before POS,
715 or before the point if POS is left out. Return t if such a position
716 exists, otherwise nil is returned and the point isn't moved.
718 A limit for the search may be given. The start position is assumed to
719 be after it."
720 (let ((res `(c-safe (goto-char (scan-lists ,(or pos `(point)) -1 -1)) t)))
721 (if limit
722 `(save-restriction
723 (narrow-to-region ,limit (point-max))
724 ,res)
725 res)))
728 (defmacro c-beginning-of-defun-1 ()
729 ;; Wrapper around beginning-of-defun.
731 ;; NOTE: This function should contain the only explicit use of
732 ;; beginning-of-defun in CC Mode. Eventually something better than
733 ;; b-o-d will be available and this should be the only place the
734 ;; code needs to change. Everything else should use
735 ;; (c-beginning-of-defun-1)
737 ;; This is really a bit too large to be a macro but that isn't a
738 ;; problem as long as it only is used in one place in
739 ;; `c-parse-state'.
741 `(progn
742 (if (and ,(fboundp 'buffer-syntactic-context-depth)
743 c-enable-xemacs-performance-kludge-p)
744 ,(when (fboundp 'buffer-syntactic-context-depth)
745 ;; XEmacs only. This can improve the performance of
746 ;; c-parse-state to between 3 and 60 times faster when
747 ;; braces are hung. It can also degrade performance by
748 ;; about as much when braces are not hung.
749 '(let (beginning-of-defun-function end-of-defun-function
750 pos)
751 (while (not pos)
752 (save-restriction
753 (widen)
754 (setq pos (c-safe-scan-lists
755 (point) -1 (buffer-syntactic-context-depth))))
756 (cond
757 ((bobp) (setq pos (point-min)))
758 ((not pos)
759 (let ((distance (skip-chars-backward "^{")))
760 ;; unbalanced parenthesis, while invalid C code,
761 ;; shouldn't cause an infloop! See unbal.c
762 (when (zerop distance)
763 ;; Punt!
764 (beginning-of-defun)
765 (setq pos (point)))))
766 ((= pos 0))
767 ((not (eq (char-after pos) ?{))
768 (goto-char pos)
769 (setq pos nil))
771 (goto-char pos)))
772 ;; Emacs, which doesn't have buffer-syntactic-context-depth
773 (let (beginning-of-defun-function end-of-defun-function)
774 (beginning-of-defun)))
775 ;; if defun-prompt-regexp is non-nil, b-o-d won't leave us at the
776 ;; open brace.
777 (and defun-prompt-regexp
778 (looking-at defun-prompt-regexp)
779 (goto-char (match-end 0)))))
782 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
783 ;; V i r t u a l S e m i c o l o n s
785 ;; In most CC Mode languages, statements are terminated explicitly by
786 ;; semicolons or closing braces. In some of the CC modes (currently AWK Mode
787 ;; and certain user-specified #define macros in C, C++, etc. (November 2008)),
788 ;; statements are (or can be) terminated by EOLs. Such a statement is said to
789 ;; be terminated by a "virtual semicolon" (VS). A statement terminated by an
790 ;; actual semicolon or brace is never considered to have a VS.
792 ;; The indentation engine (or whatever) tests for a VS at a specific position
793 ;; by invoking the macro `c-at-vsemi-p', which in its turn calls the mode
794 ;; specific function (if any) which is the value of the language variable
795 ;; `c-at-vsemi-p-fn'. This function should only use "low-level" features of
796 ;; CC Mode, i.e. features which won't trigger infinite recursion. ;-) The
797 ;; actual details of what constitutes a VS in a language are thus encapsulated
798 ;; in code specific to that language (e.g. cc-awk.el). `c-at-vsemi-p' returns
799 ;; non-nil if point (or the optional parameter POS) is at a VS, nil otherwise.
801 ;; The language specific function might well do extensive analysis of the
802 ;; source text, and may use a caching scheme to speed up repeated calls.
804 ;; The "virtual semicolon" lies just after the last non-ws token on the line.
805 ;; Like POINT, it is considered to lie between two characters. For example,
806 ;; at the place shown in the following AWK source line:
808 ;; kbyte = 1024 # 1000 if you're not picky
809 ;; ^
810 ;; |
811 ;; Virtual Semicolon
813 ;; In addition to `c-at-vsemi-p-fn', a mode may need to supply a function for
814 ;; `c-vsemi-status-unknown-p-fn'. The macro `c-vsemi-status-unknown-p' is a
815 ;; rather recondite kludge. It exists because the function
816 ;; `c-beginning-of-statement-1' sometimes tests for VSs as an optimization,
817 ;; but `c-at-vsemi-p' might well need to call `c-beginning-of-statement-1' in
818 ;; its calculations, thus potentially leading to infinite recursion.
820 ;; The macro `c-vsemi-status-unknown-p' resolves this problem; it may return
821 ;; non-nil at any time; returning nil is a guarantee that an immediate
822 ;; invocation of `c-at-vsemi-p' at point will NOT call
823 ;; `c-beginning-of-statement-1'. `c-vsemi-status-unknown-p' may not itself
824 ;; call `c-beginning-of-statement-1'.
826 ;; The macro `c-vsemi-status-unknown-p' will typically check the caching
827 ;; scheme used by the `c-at-vsemi-p-fn', hence the name - the status is
828 ;; "unknown" if there is no cache entry current for the line.
829 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
831 (defmacro c-at-vsemi-p (&optional pos)
832 ;; Is there a virtual semicolon (not a real one or a }) at POS (defaults to
833 ;; point)? Always returns nil for languages which don't have Virtual
834 ;; semicolons.
835 ;; This macro might do hidden buffer changes.
836 `(if c-at-vsemi-p-fn
837 (funcall c-at-vsemi-p-fn ,@(if pos `(,pos)))))
839 (defmacro c-vsemi-status-unknown-p ()
840 ;; Return NIL only if it can be guaranteed that an immediate
841 ;; (c-at-vsemi-p) will NOT call c-beginning-of-statement-1. Otherwise,
842 ;; return non-nil. (See comments above). The function invoked by this
843 ;; macro MUST NOT UNDER ANY CIRCUMSTANCES itself call
844 ;; c-beginning-of-statement-1.
845 ;; Languages which don't have EOL terminated statements always return NIL
846 ;; (they _know_ there's no vsemi ;-).
847 `(if c-vsemi-status-unknown-p-fn (funcall c-vsemi-status-unknown-p-fn)))
850 (defmacro c-benign-error (format &rest args)
851 ;; Formats an error message for the echo area and dings, i.e. like
852 ;; `error' but doesn't abort.
853 `(progn
854 (message ,format ,@args)
855 (ding)))
857 (defmacro c-with-syntax-table (table &rest code)
858 ;; Temporarily switches to the specified syntax table in a failsafe
859 ;; way to execute code.
860 ;; Maintainers' note: If TABLE is `c++-template-syntax-table', DON'T call
861 ;; any forms inside this that call `c-parse-state'. !!!!
862 `(let ((c-with-syntax-table-orig-table (syntax-table)))
863 (unwind-protect
864 (progn
865 (set-syntax-table ,table)
866 ,@code)
867 (set-syntax-table c-with-syntax-table-orig-table))))
868 (put 'c-with-syntax-table 'lisp-indent-function 1)
870 (defmacro c-skip-ws-forward (&optional limit)
871 "Skip over any whitespace following point.
872 This function skips over horizontal and vertical whitespace and line
873 continuations."
874 (if limit
875 `(let ((limit (or ,limit (point-max))))
876 (while (progn
877 ;; skip-syntax-* doesn't count \n as whitespace..
878 (skip-chars-forward " \t\n\r\f\v" limit)
879 (when (and (eq (char-after) ?\\)
880 (< (point) limit))
881 (forward-char)
882 (or (eolp)
883 (progn (backward-char) nil))))))
884 '(while (progn
885 (skip-chars-forward " \t\n\r\f\v")
886 (when (eq (char-after) ?\\)
887 (forward-char)
888 (or (eolp)
889 (progn (backward-char) nil)))))))
891 (defmacro c-skip-ws-backward (&optional limit)
892 "Skip over any whitespace preceding point.
893 This function skips over horizontal and vertical whitespace and line
894 continuations."
895 (if limit
896 `(let ((limit (or ,limit (point-min))))
897 (while (progn
898 ;; skip-syntax-* doesn't count \n as whitespace..
899 (skip-chars-backward " \t\n\r\f\v" limit)
900 (and (eolp)
901 (eq (char-before) ?\\)
902 (> (point) limit)))
903 (backward-char)))
904 '(while (progn
905 (skip-chars-backward " \t\n\r\f\v")
906 (and (eolp)
907 (eq (char-before) ?\\)))
908 (backward-char))))
910 (eval-and-compile
911 (defvar c-langs-are-parametric nil))
913 (defmacro c-major-mode-is (mode)
914 "Return non-nil if the current CC Mode major mode is MODE.
915 MODE is either a mode symbol or a list of mode symbols."
917 (if c-langs-are-parametric
918 ;; Inside a `c-lang-defconst'.
919 `(c-lang-major-mode-is ,mode)
921 (if (eq (car-safe mode) 'quote)
922 (let ((mode (eval mode)))
923 (if (listp mode)
924 `(memq c-buffer-is-cc-mode ',mode)
925 `(eq c-buffer-is-cc-mode ',mode)))
927 `(let ((mode ,mode))
928 (if (listp mode)
929 (memq c-buffer-is-cc-mode mode)
930 (eq c-buffer-is-cc-mode mode))))))
933 ;; Macros/functions to handle so-called "char properties", which are
934 ;; properties set on a single character and that never spread to any
935 ;; other characters.
937 (eval-and-compile
938 ;; Constant used at compile time to decide whether or not to use
939 ;; XEmacs extents. Check all the extent functions we'll use since
940 ;; some packages might add compatibility aliases for some of them in
941 ;; Emacs.
942 (defconst c-use-extents (and (cc-bytecomp-fboundp 'extent-at)
943 (cc-bytecomp-fboundp 'set-extent-property)
944 (cc-bytecomp-fboundp 'set-extent-properties)
945 (cc-bytecomp-fboundp 'make-extent)
946 (cc-bytecomp-fboundp 'extent-property)
947 (cc-bytecomp-fboundp 'delete-extent)
948 (cc-bytecomp-fboundp 'map-extents))))
950 (defconst c-<-as-paren-syntax '(4 . ?>))
951 (put 'c-<-as-paren-syntax 'syntax-table c-<-as-paren-syntax)
953 (defconst c->-as-paren-syntax '(5 . ?<))
954 (put 'c->-as-paren-syntax 'syntax-table c->-as-paren-syntax)
956 ;; `c-put-char-property' is complex enough in XEmacs and Emacs < 21 to
957 ;; make it a function.
958 (defalias 'c-put-char-property-fun
959 (cc-eval-when-compile
960 (cond (c-use-extents
961 ;; XEmacs.
962 (byte-compile
963 (lambda (pos property value)
964 (let ((ext (extent-at pos nil property)))
965 (if ext
966 (set-extent-property ext property value)
967 (set-extent-properties (make-extent pos (1+ pos))
968 (cons property
969 (cons value
970 '(start-open t
971 end-open t)))))))))
973 ((not (cc-bytecomp-boundp 'text-property-default-nonsticky))
974 ;; In Emacs < 21 we have to mess with the `rear-nonsticky' property.
975 (byte-compile
976 (lambda (pos property value)
977 (put-text-property pos (1+ pos) property value)
978 (let ((prop (get-text-property pos 'rear-nonsticky)))
979 (or (memq property prop)
980 (put-text-property pos (1+ pos)
981 'rear-nonsticky
982 (cons property prop)))))))
983 ;; This won't be used for anything.
984 (t 'ignore))))
985 (cc-bytecomp-defun c-put-char-property-fun) ; Make it known below.
987 (defmacro c-put-char-property (pos property value)
988 ;; Put the given property with the given value on the character at
989 ;; POS and make it front and rear nonsticky, or start and end open
990 ;; in XEmacs vocabulary. If the character already has the given
991 ;; property then the value is replaced, and the behavior is
992 ;; undefined if that property has been put by some other function.
993 ;; PROPERTY is assumed to be constant.
995 ;; If there's a `text-property-default-nonsticky' variable (Emacs
996 ;; 21) then it's assumed that the property is present on it.
998 ;; This macro does a hidden buffer change.
999 (setq property (eval property))
1000 (if (or c-use-extents
1001 (not (cc-bytecomp-boundp 'text-property-default-nonsticky)))
1002 ;; XEmacs and Emacs < 21.
1003 `(c-put-char-property-fun ,pos ',property ,value)
1004 ;; In Emacs 21 we got the `rear-nonsticky' property covered
1005 ;; by `text-property-default-nonsticky'.
1006 `(let ((-pos- ,pos))
1007 (put-text-property -pos- (1+ -pos-) ',property ,value))))
1009 (defmacro c-get-char-property (pos property)
1010 ;; Get the value of the given property on the character at POS if
1011 ;; it's been put there by `c-put-char-property'. PROPERTY is
1012 ;; assumed to be constant.
1013 (setq property (eval property))
1014 (if c-use-extents
1015 ;; XEmacs.
1016 `(let ((ext (extent-at ,pos nil ',property)))
1017 (if ext (extent-property ext ',property)))
1018 ;; Emacs.
1019 `(get-text-property ,pos ',property)))
1021 ;; `c-clear-char-property' is complex enough in Emacs < 21 to make it
1022 ;; a function, since we have to mess with the `rear-nonsticky' property.
1023 (defalias 'c-clear-char-property-fun
1024 (cc-eval-when-compile
1025 (unless (or c-use-extents
1026 (cc-bytecomp-boundp 'text-property-default-nonsticky))
1027 (byte-compile
1028 (lambda (pos property)
1029 (when (get-text-property pos property)
1030 (remove-text-properties pos (1+ pos) (list property nil))
1031 (put-text-property pos (1+ pos)
1032 'rear-nonsticky
1033 (delq property (get-text-property
1034 pos 'rear-nonsticky)))))))))
1035 (cc-bytecomp-defun c-clear-char-property-fun) ; Make it known below.
1037 (defmacro c-clear-char-property (pos property)
1038 ;; Remove the given property on the character at POS if it's been put
1039 ;; there by `c-put-char-property'. PROPERTY is assumed to be
1040 ;; constant.
1042 ;; This macro does a hidden buffer change.
1043 (setq property (eval property))
1044 (cond (c-use-extents
1045 ;; XEmacs.
1046 `(let ((ext (extent-at ,pos nil ',property)))
1047 (if ext (delete-extent ext))))
1048 ((cc-bytecomp-boundp 'text-property-default-nonsticky)
1049 ;; In Emacs 21 we got the `rear-nonsticky' property covered
1050 ;; by `text-property-default-nonsticky'.
1051 `(let ((pos ,pos))
1052 (remove-text-properties pos (1+ pos)
1053 '(,property nil))))
1055 ;; Emacs < 21.
1056 `(c-clear-char-property-fun ,pos ',property))))
1058 (defmacro c-clear-char-properties (from to property)
1059 ;; Remove all the occurrences of the given property in the given
1060 ;; region that has been put with `c-put-char-property'. PROPERTY is
1061 ;; assumed to be constant.
1063 ;; Note that this function does not clean up the property from the
1064 ;; lists of the `rear-nonsticky' properties in the region, if such
1065 ;; are used. Thus it should not be used for common properties like
1066 ;; `syntax-table'.
1068 ;; This macro does hidden buffer changes.
1069 (setq property (eval property))
1070 (if c-use-extents
1071 ;; XEmacs.
1072 `(map-extents (lambda (ext ignored)
1073 (delete-extent ext))
1074 nil ,from ,to nil nil ',property)
1075 ;; Emacs.
1076 `(remove-text-properties ,from ,to '(,property nil))))
1078 (defmacro c-search-forward-char-property (property value &optional limit)
1079 "Search forward for a text-property PROPERTY having value VALUE.
1080 LIMIT bounds the search. The comparison is done with `equal'.
1082 Leave point just after the character, and set the match data on
1083 this character, and return point. If VALUE isn't found, Return
1084 nil; point is then left undefined."
1085 `(let ((place (point)))
1086 (while
1087 (and
1088 (< place ,(or limit '(point-max)))
1089 (not (equal (c-get-char-property place ,property) ,value)))
1090 (setq place (c-next-single-property-change
1091 place ,property nil ,(or limit '(point-max)))))
1092 (when (< place ,(or limit '(point-max)))
1093 (goto-char place)
1094 (search-forward-regexp ".") ; to set the match-data.
1095 (point))))
1097 (defmacro c-search-backward-char-property (property value &optional limit)
1098 "Search backward for a text-property PROPERTY having value VALUE.
1099 LIMIT bounds the search. The comparison is done with `equal'.
1101 Leave point just before the character, set the match data on this
1102 character, and return point. If VALUE isn't found, Return nil;
1103 point is then left undefined."
1104 `(let ((place (point)))
1105 (while
1106 (and
1107 (> place ,(or limit '(point-min)))
1108 (not (equal (c-get-char-property (1- place) ,property) ,value)))
1109 (setq place (,(if (and c-use-extents
1110 (fboundp 'previous-single-char-property-change))
1111 ;; XEmacs > 2005-01-25.
1112 'previous-single-char-property-change
1113 ;; Emacs and earlier XEmacs.
1114 'previous-single-property-change)
1115 place ,property nil ,(or limit '(point-min)))))
1116 (when (> place ,(or limit '(point-min)))
1117 (goto-char place)
1118 (search-backward-regexp ".") ; to set the match-data.
1119 (point))))
1121 (defun c-clear-char-property-with-value-function (from to property value)
1122 "Remove all text-properties PROPERTY from the region (FROM, TO)
1123 which have the value VALUE, as tested by `equal'. These
1124 properties are assumed to be over individual characters, having
1125 been put there by c-put-char-property. POINT remains unchanged."
1126 (let ((place from) end-place)
1127 (while ; loop round occurrences of (PROPERTY VALUE)
1128 (progn
1129 (while ; loop round changes in PROPERTY till we find VALUE
1130 (and
1131 (< place to)
1132 (not (equal (get-text-property place property) value)))
1133 (setq place (c-next-single-property-change place property nil to)))
1134 (< place to))
1135 (setq end-place (c-next-single-property-change place property nil to))
1136 (remove-text-properties place end-place (cons property nil))
1137 ;; Do we have to do anything with stickiness here?
1138 (setq place end-place))))
1140 (defmacro c-clear-char-property-with-value (from to property value)
1141 "Remove all text-properties PROPERTY from the region [FROM, TO)
1142 which have the value VALUE, as tested by `equal'. These
1143 properties are assumed to be over individual characters, having
1144 been put there by c-put-char-property. POINT remains unchanged."
1145 (if c-use-extents
1146 ;; XEmacs
1147 `(let ((-property- ,property))
1148 (map-extents (lambda (ext val)
1149 (if (equal (extent-property ext -property-) val)
1150 (delete-extent ext)))
1151 nil ,from ,to ,value nil -property-))
1152 ;; GNU Emacs
1153 `(c-clear-char-property-with-value-function ,from ,to ,property ,value)))
1155 ;; Macros to put overlays (Emacs) or extents (XEmacs) on buffer text.
1156 ;; For our purposes, these are characterized by being possible to
1157 ;; remove again without affecting the other text properties in the
1158 ;; buffer that got overridden when they were put.
1160 (defmacro c-put-overlay (from to property value)
1161 ;; Put an overlay/extent covering the given range in the current
1162 ;; buffer. It's currently undefined whether it's front/end sticky
1163 ;; or not. The overlay/extent object is returned.
1164 (if (cc-bytecomp-fboundp 'make-overlay)
1165 ;; Emacs.
1166 `(let ((ol (make-overlay ,from ,to)))
1167 (overlay-put ol ,property ,value)
1169 ;; XEmacs.
1170 `(let ((ext (make-extent ,from ,to)))
1171 (set-extent-property ext ,property ,value)
1172 ext)))
1174 (defmacro c-delete-overlay (overlay)
1175 ;; Deletes an overlay/extent object previously retrieved using
1176 ;; `c-put-overlay'.
1177 (if (cc-bytecomp-fboundp 'make-overlay)
1178 ;; Emacs.
1179 `(delete-overlay ,overlay)
1180 ;; XEmacs.
1181 `(delete-extent ,overlay)))
1184 ;; Make edebug understand the macros.
1185 ;(eval-after-load "edebug" ; 2006-07-09: def-edebug-spec is now in subr.el.
1186 ; '(progn
1187 (def-edebug-spec cc-eval-when-compile (&rest def-form))
1188 (def-edebug-spec c-point t)
1189 (def-edebug-spec c-set-region-active t)
1190 (def-edebug-spec c-safe t)
1191 (def-edebug-spec c-save-buffer-state let*)
1192 (def-edebug-spec c-tentative-buffer-changes t)
1193 (def-edebug-spec c-forward-syntactic-ws t)
1194 (def-edebug-spec c-backward-syntactic-ws t)
1195 (def-edebug-spec c-forward-sexp t)
1196 (def-edebug-spec c-backward-sexp t)
1197 (def-edebug-spec c-up-list-forward t)
1198 (def-edebug-spec c-up-list-backward t)
1199 (def-edebug-spec c-down-list-forward t)
1200 (def-edebug-spec c-down-list-backward t)
1201 (def-edebug-spec c-add-syntax t)
1202 (def-edebug-spec c-add-class-syntax t)
1203 (def-edebug-spec c-benign-error t)
1204 (def-edebug-spec c-with-syntax-table t)
1205 (def-edebug-spec c-skip-ws-forward t)
1206 (def-edebug-spec c-skip-ws-backward t)
1207 (def-edebug-spec c-major-mode-is t)
1208 (def-edebug-spec c-put-char-property t)
1209 (def-edebug-spec c-get-char-property t)
1210 (def-edebug-spec c-clear-char-property t)
1211 (def-edebug-spec c-clear-char-properties t)
1212 (def-edebug-spec c-put-overlay t)
1213 (def-edebug-spec c-delete-overlay t) ;))
1216 ;;; Functions.
1218 ;; Note: All these after the macros, to be on safe side in avoiding
1219 ;; bugs where macros are defined too late. These bugs often only show
1220 ;; when the files are compiled in a certain order within the same
1221 ;; session.
1223 (defsubst c-end-of-defun-1 ()
1224 ;; Replacement for end-of-defun that use c-beginning-of-defun-1.
1225 (let ((start (point)))
1226 ;; Skip forward into the next defun block. Don't bother to avoid
1227 ;; comments, literals etc, since beginning-of-defun doesn't do that
1228 ;; anyway.
1229 (skip-chars-forward "^}")
1230 (c-beginning-of-defun-1)
1231 (if (eq (char-after) ?{)
1232 (c-forward-sexp))
1233 (if (< (point) start)
1234 (goto-char (point-max)))))
1236 (defmacro c-mark-<-as-paren (pos)
1237 ;; Mark the "<" character at POS as a template opener using the
1238 ;; `syntax-table' property either directly (XEmacs) or via a `category'
1239 ;; property (GNU Emacs).
1241 ;; This function does a hidden buffer change. Note that we use
1242 ;; indirection through the `category' text property. This allows us to
1243 ;; toggle the property in all template brackets simultaneously and
1244 ;; cheaply. We use this, for instance, in `c-parse-state'.
1245 (if c-use-category
1246 `(c-put-char-property ,pos 'category 'c-<-as-paren-syntax)
1247 `(c-put-char-property ,pos 'syntax-table c-<-as-paren-syntax)))
1250 (defmacro c-mark->-as-paren (pos)
1251 ;; Mark the ">" character at POS as an sexp list closer using the
1252 ;; `syntax-table' property either directly (XEmacs) or via a `category'
1253 ;; property (GNU Emacs).
1255 ;; This function does a hidden buffer change. Note that we use
1256 ;; indirection through the `category' text property. This allows us to
1257 ;; toggle the property in all template brackets simultaneously and
1258 ;; cheaply. We use this, for instance, in `c-parse-state'.
1259 (if c-use-category
1260 `(c-put-char-property ,pos 'category 'c->-as-paren-syntax)
1261 `(c-put-char-property ,pos 'syntax-table c->-as-paren-syntax)))
1263 (defmacro c-unmark-<->-as-paren (pos)
1264 ;; Unmark the "<" or "<" character at POS as an sexp list opener using the
1265 ;; `syntax-table' property either directly or indirectly through a
1266 ;; `category' text property.
1268 ;; This function does a hidden buffer change. Note that we try to use
1269 ;; indirection through the `category' text property. This allows us to
1270 ;; toggle the property in all template brackets simultaneously and
1271 ;; cheaply. We use this, for instance, in `c-parse-state'.
1272 `(c-clear-char-property ,pos ,(if c-use-category ''category ''syntax-table)))
1274 (defsubst c-suppress-<->-as-parens ()
1275 ;; Suppress the syntactic effect of all marked < and > as parens. Note
1276 ;; that this effect is NOT buffer local. You should probably not use
1277 ;; this directly, but only through the macro
1278 ;; `c-with-<->-as-parens-suppressed'
1279 (put 'c-<-as-paren-syntax 'syntax-table nil)
1280 (put 'c->-as-paren-syntax 'syntax-table nil))
1282 (defsubst c-restore-<->-as-parens ()
1283 ;; Restore the syntactic effect of all marked <s and >s as parens. This
1284 ;; has no effect on unmarked <s and >s
1285 (put 'c-<-as-paren-syntax 'syntax-table c-<-as-paren-syntax)
1286 (put 'c->-as-paren-syntax 'syntax-table c->-as-paren-syntax))
1288 (defmacro c-with-<->-as-parens-suppressed (&rest forms)
1289 ;; Like progn, except that the paren property is suppressed on all
1290 ;; template brackets whilst they are running. This macro does a hidden
1291 ;; buffer change.
1292 `(unwind-protect
1293 (progn
1294 (c-suppress-<->-as-parens)
1295 ,@forms)
1296 (c-restore-<->-as-parens)))
1298 ;;;;;;;;;;;;;;;
1300 (defconst c-cpp-delimiter '(14)) ; generic comment syntax
1301 ;; This is the value of the `category' text property placed on every #
1302 ;; which introduces a CPP construct and every EOL (or EOB, or character
1303 ;; preceding //, etc.) which terminates it. We can instantly "comment
1304 ;; out" all CPP constructs by giving `c-cpp-delimiter' a syntax-table
1305 ;; property '(14) (generic comment delimiter).
1306 (defmacro c-set-cpp-delimiters (beg end)
1307 ;; This macro does a hidden buffer change.
1308 `(progn
1309 (c-put-char-property ,beg 'category 'c-cpp-delimiter)
1310 (if (< ,end (point-max))
1311 (c-put-char-property ,end 'category 'c-cpp-delimiter))))
1312 (defmacro c-clear-cpp-delimiters (beg end)
1313 ;; This macro does a hidden buffer change.
1314 `(progn
1315 (c-clear-char-property ,beg 'category)
1316 (if (< ,end (point-max))
1317 (c-clear-char-property ,end 'category))))
1319 (defsubst c-comment-out-cpps ()
1320 ;; Render all preprocessor constructs syntactically commented out.
1321 (put 'c-cpp-delimiter 'syntax-table c-cpp-delimiter))
1322 (defsubst c-uncomment-out-cpps ()
1323 ;; Restore the syntactic visibility of preprocessor constructs.
1324 (put 'c-cpp-delimiter 'syntax-table nil))
1326 (defmacro c-with-cpps-commented-out (&rest forms)
1327 ;; Execute FORMS... whilst the syntactic effect of all characters in
1328 ;; all CPP regions is suppressed. In particular, this is to suppress
1329 ;; the syntactic significance of parens/braces/brackets to functions
1330 ;; such as `scan-lists' and `parse-partial-sexp'.
1331 `(unwind-protect
1332 (c-save-buffer-state ()
1333 (c-comment-out-cpps)
1334 ,@forms)
1335 (c-save-buffer-state ()
1336 (c-uncomment-out-cpps))))
1338 (defmacro c-with-all-but-one-cpps-commented-out (beg end &rest forms)
1339 ;; Execute FORMS... whilst the syntactic effect of all characters in
1340 ;; every CPP region APART FROM THE ONE BETWEEN BEG and END is
1341 ;; suppressed.
1342 `(unwind-protect
1343 (c-save-buffer-state ()
1344 (save-restriction
1345 (widen)
1346 (c-clear-cpp-delimiters ,beg ,end))
1347 ,`(c-with-cpps-commented-out ,@forms))
1348 (c-save-buffer-state ()
1349 (save-restriction
1350 (widen)
1351 (c-set-cpp-delimiters ,beg ,end)))))
1353 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1354 ;; The following macros are to be used only in `c-parse-state' and its
1355 ;; subroutines. Their main purpose is to simplify the handling of C++/Java
1356 ;; template delimiters and CPP macros. In GNU Emacs, this is done slickly by
1357 ;; the judicious use of 'category properties. These don't exist in XEmacs.
1359 ;; Note: in the following macros, there is no special handling for parentheses
1360 ;; inside CPP constructs. That is because CPPs are always syntactically
1361 ;; balanced, thanks to `c-neutralize-CPP-line' in cc-mode.el.
1362 (defmacro c-sc-scan-lists-no-category+1+1 (from)
1363 ;; Do a (scan-lists FROM 1 1). Any finishing position which either (i) is
1364 ;; determined by and angle bracket; or (ii) is inside a macro whose start
1365 ;; isn't POINT-MACRO-START doesn't count as a finishing position.
1366 `(let ((here (point))
1367 (pos (scan-lists ,from 1 1)))
1368 (while (eq (char-before pos) ?>)
1369 (setq pos (scan-lists pos 1 1)))
1370 pos))
1372 (defmacro c-sc-scan-lists-no-category+1-1 (from)
1373 ;; Do a (scan-lists FROM 1 -1). Any finishing position which either (i) is
1374 ;; determined by an angle bracket; or (ii) is inside a macro whose start
1375 ;; isn't POINT-MACRO-START doesn't count as a finishing position.
1376 `(let ((here (point))
1377 (pos (scan-lists ,from 1 -1)))
1378 (while (eq (char-before pos) ?<)
1379 (setq pos (scan-lists pos 1 1))
1380 (setq pos (scan-lists pos 1 -1)))
1381 pos))
1383 (defmacro c-sc-scan-lists-no-category-1+1 (from)
1384 ;; Do a (scan-lists FROM -1 1). Any finishing position which either (i) is
1385 ;; determined by and angle bracket; or (ii) is inside a macro whose start
1386 ;; isn't POINT-MACRO-START doesn't count as a finishing position.
1387 `(let ((here (point))
1388 (pos (scan-lists ,from -1 1)))
1389 (while (eq (char-after pos) ?<)
1390 (setq pos (scan-lists pos -1 1)))
1391 pos))
1393 (defmacro c-sc-scan-lists-no-category-1-1 (from)
1394 ;; Do a (scan-lists FROM -1 -1). Any finishing position which either (i) is
1395 ;; determined by and angle bracket; or (ii) is inside a macro whose start
1396 ;; isn't POINT-MACRO-START doesn't count as a finishing position.
1397 `(let ((here (point))
1398 (pos (scan-lists ,from -1 -1)))
1399 (while (eq (char-after pos) ?>)
1400 (setq pos (scan-lists pos -1 1))
1401 (setq pos (scan-lists pos -1 -1)))
1402 pos))
1404 (defmacro c-sc-scan-lists (from count depth)
1405 (if c-use-category
1406 `(scan-lists ,from ,count ,depth)
1407 (cond
1408 ((and (eq count 1) (eq depth 1))
1409 `(c-sc-scan-lists-no-category+1+1 ,from))
1410 ((and (eq count 1) (eq depth -1))
1411 `(c-sc-scan-lists-no-category+1-1 ,from))
1412 ((and (eq count -1) (eq depth 1))
1413 `(c-sc-scan-lists-no-category-1+1 ,from))
1414 ((and (eq count -1) (eq depth -1))
1415 `(c-sc-scan-lists-no-category-1-1 ,from))
1416 (t (error "Invalid parameter(s) to c-sc-scan-lists")))))
1419 (defun c-sc-parse-partial-sexp-no-category (from to targetdepth stopbefore
1420 oldstate)
1421 ;; Do a parse-partial-sexp using the supplied arguments, disregarding
1422 ;; template/generic delimiters < > and disregarding macros other than the
1423 ;; one at POINT-MACRO-START.
1425 ;; NOTE that STOPBEFORE must be nil. TARGETDEPTH should be one less than
1426 ;; the depth in OLDSTATE. This function is thus a SPECIAL PURPOSE variation
1427 ;; on parse-partial-sexp, designed for calling from
1428 ;; `c-remove-stale-state-cache'.
1430 ;; Any finishing position which is determined by an angle bracket delimiter
1431 ;; doesn't count as a finishing position.
1433 ;; Note there is no special handling of CPP constructs here, since these are
1434 ;; always syntactically balanced (thanks to `c-neutralize-CPP-line').
1435 (let ((state
1436 (parse-partial-sexp from to targetdepth stopbefore oldstate)))
1437 (while
1438 (and (< (point) to)
1439 ;; We must have hit targetdepth.
1440 (or (eq (char-before) ?<)
1441 (eq (char-before) ?>)))
1442 (setcar state
1443 (if (memq (char-before) '(?> ?\) ?\} ?\]))
1444 (1+ (car state))
1445 (1- (car state))))
1446 (setq state
1447 (parse-partial-sexp (point) to targetdepth stopbefore oldstate)))
1448 state))
1450 (defmacro c-sc-parse-partial-sexp (from to &optional targetdepth stopbefore
1451 oldstate)
1452 (if c-use-category
1453 `(parse-partial-sexp ,from ,to ,targetdepth ,stopbefore ,oldstate)
1454 `(c-sc-parse-partial-sexp-no-category ,from ,to ,targetdepth ,stopbefore
1455 ,oldstate)))
1458 (defvar c-emacs-features)
1460 (defmacro c-looking-at-non-alphnumspace ()
1461 "Are we looking at a character which isn't alphanumeric or space?"
1462 (if (memq 'gen-comment-delim c-emacs-features)
1463 `(looking-at
1464 "\\([;#]\\|\\'\\|\\s(\\|\\s)\\|\\s\"\\|\\s\\\\|\\s$\\|\\s<\\|\\s>\\|\\s!\\)")
1465 `(or (looking-at
1466 "\\([;#]\\|\\'\\|\\s(\\|\\s)\\|\\s\"\\|\\s\\\\|\\s$\\|\\s<\\|\\s>\\)"
1467 (let ((prop (c-get-char-property (point) 'syntax-table)))
1468 (eq prop '(14))))))) ; '(14) is generic comment delimiter.
1471 (defsubst c-intersect-lists (list alist)
1472 ;; return the element of ALIST that matches the first element found
1473 ;; in LIST. Uses assq.
1474 (let (match)
1475 (while (and list
1476 (not (setq match (assq (car list) alist))))
1477 (setq list (cdr list)))
1478 match))
1480 (defsubst c-lookup-lists (list alist1 alist2)
1481 ;; first, find the first entry from LIST that is present in ALIST1,
1482 ;; then find the entry in ALIST2 for that entry.
1483 (assq (car (c-intersect-lists list alist1)) alist2))
1485 (defsubst c-langelem-sym (langelem)
1486 "Return the syntactic symbol in LANGELEM.
1488 LANGELEM is either a cons cell on the \"old\" form given as the first
1489 argument to lineup functions or a syntactic element on the \"new\"
1490 form as used in `c-syntactic-element'."
1491 (car langelem))
1493 (defsubst c-langelem-pos (langelem)
1494 "Return the anchor position in LANGELEM, or nil if there is none.
1496 LANGELEM is either a cons cell on the \"old\" form given as the first
1497 argument to lineup functions or a syntactic element on the \"new\"
1498 form as used in `c-syntactic-element'."
1499 (if (consp (cdr langelem))
1500 (car-safe (cdr langelem))
1501 (cdr langelem)))
1503 (defun c-langelem-col (langelem &optional preserve-point)
1504 "Return the column of the anchor position in LANGELEM.
1505 Also move the point to that position unless PRESERVE-POINT is non-nil.
1507 LANGELEM is either a cons cell on the \"old\" form given as the first
1508 argument to lineup functions or a syntactic element on the \"new\"
1509 form as used in `c-syntactic-element'."
1510 (let ((pos (c-langelem-pos langelem))
1511 (here (point)))
1512 (if pos
1513 (progn
1514 (goto-char pos)
1515 (prog1 (current-column)
1516 (if preserve-point
1517 (goto-char here))))
1518 0)))
1520 (defsubst c-langelem-2nd-pos (langelem)
1521 "Return the secondary position in LANGELEM, or nil if there is none.
1523 LANGELEM is typically a syntactic element on the \"new\" form as used
1524 in `c-syntactic-element'. It may also be a cons cell as passed in the
1525 first argument to lineup functions, but then the returned value always
1526 will be nil."
1527 (car-safe (cdr-safe (cdr-safe langelem))))
1529 (defsubst c-keep-region-active ()
1530 ;; Do whatever is necessary to keep the region active in XEmacs.
1531 ;; This is not needed for Emacs.
1532 (and (boundp 'zmacs-region-stays)
1533 (setq zmacs-region-stays t)))
1535 (put 'c-mode 'c-mode-prefix "c-")
1536 (put 'c++-mode 'c-mode-prefix "c++-")
1537 (put 'objc-mode 'c-mode-prefix "objc-")
1538 (put 'java-mode 'c-mode-prefix "java-")
1539 (put 'idl-mode 'c-mode-prefix "idl-")
1540 (put 'pike-mode 'c-mode-prefix "pike-")
1541 (put 'awk-mode 'c-mode-prefix "awk-")
1543 (defsubst c-mode-symbol (suffix)
1544 "Prefix the current mode prefix (e.g. \"c-\") to SUFFIX and return
1545 the corresponding symbol."
1546 (or c-buffer-is-cc-mode
1547 (error "Not inside a CC Mode based mode"))
1548 (let ((mode-prefix (get c-buffer-is-cc-mode 'c-mode-prefix)))
1549 (or mode-prefix
1550 (error "%S has no mode prefix known to `c-mode-symbol'"
1551 c-buffer-is-cc-mode))
1552 (intern (concat mode-prefix suffix))))
1554 (defsubst c-mode-var (suffix)
1555 "Prefix the current mode prefix (e.g. \"c-\") to SUFFIX and return
1556 the value of the variable with that name."
1557 (symbol-value (c-mode-symbol suffix)))
1559 (defsubst c-got-face-at (pos faces)
1560 "Return non-nil if position POS in the current buffer has any of the
1561 faces in the list FACES."
1562 (let ((pos-faces (get-text-property pos 'face)))
1563 (if (consp pos-faces)
1564 (progn
1565 (while (and pos-faces
1566 (not (memq (car pos-faces) faces)))
1567 (setq pos-faces (cdr pos-faces)))
1568 pos-faces)
1569 (memq pos-faces faces))))
1571 (defsubst c-face-name-p (facename)
1572 ;; Return t if FACENAME is the name of a face. This method is
1573 ;; necessary since facep in XEmacs only returns t for the actual
1574 ;; face objects (while it's only their names that are used just
1575 ;; about anywhere else) without providing a predicate that tests
1576 ;; face names.
1577 (memq facename (face-list)))
1579 (defun c-concat-separated (list separator)
1580 "Like `concat' on LIST, but separate each element with SEPARATOR.
1581 Notably, null elements in LIST are ignored."
1582 (mapconcat 'identity (delete nil (append list nil)) separator))
1584 (defun c-make-keywords-re (adorn list &optional mode)
1585 "Make a regexp that matches all the strings the list.
1586 Duplicates and nil elements in the list are removed. The
1587 resulting regexp may contain zero or more submatch expressions.
1589 If ADORN is t there will be at least one submatch and the first
1590 surrounds the matched alternative, and the regexp will also not match
1591 a prefix of any identifier. Adorned regexps cannot be appended. The
1592 language variable `c-nonsymbol-key' is used to make the adornment.
1594 A value 'appendable for ADORN is like above, but all alternatives in
1595 the list that end with a word constituent char will have \\> appended
1596 instead, so that the regexp remains appendable. Note that this
1597 variant doesn't always guarantee that an identifier prefix isn't
1598 matched since the symbol constituent '_' is normally considered a
1599 nonword token by \\>.
1601 The optional MODE specifies the language to get `c-nonsymbol-key' from
1602 when it's needed. The default is the current language taken from
1603 `c-buffer-is-cc-mode'."
1605 (setq list (delete nil (delete-dups list)))
1606 (if list
1607 (let (re)
1609 (if (eq adorn 'appendable)
1610 ;; This is kludgy but it works: Search for a string that
1611 ;; doesn't occur in any word in LIST. Append it to all
1612 ;; the alternatives where we want to add \>. Run through
1613 ;; `regexp-opt' and then replace it with \>.
1614 (let ((unique "") pos)
1615 (while (let (found)
1616 (setq unique (concat unique "@")
1617 pos list)
1618 (while (and pos
1619 (if (string-match unique (car pos))
1620 (progn (setq found t)
1621 nil)
1623 (setq pos (cdr pos)))
1624 found))
1625 (setq pos list)
1626 (while pos
1627 (if (string-match "\\w\\'" (car pos))
1628 (setcar pos (concat (car pos) unique)))
1629 (setq pos (cdr pos)))
1630 (setq re (regexp-opt list))
1631 (setq pos 0)
1632 (while (string-match unique re pos)
1633 (setq pos (+ (match-beginning 0) 2)
1634 re (replace-match "\\>" t t re))))
1636 (setq re (regexp-opt list)))
1638 ;; Emacs 20 and XEmacs (all versions so far) has a buggy
1639 ;; regexp-opt that doesn't always cope with strings containing
1640 ;; newlines. This kludge doesn't handle shy parens correctly
1641 ;; so we can't advice regexp-opt directly with it.
1642 (let (fail-list)
1643 (while list
1644 (and (string-match "\n" (car list)) ; To speed it up a little.
1645 (not (string-match (concat "\\`\\(" re "\\)\\'")
1646 (car list)))
1647 (setq fail-list (cons (car list) fail-list)))
1648 (setq list (cdr list)))
1649 (when fail-list
1650 (setq re (concat re
1651 "\\|"
1652 (mapconcat
1653 (if (eq adorn 'appendable)
1654 (lambda (str)
1655 (if (string-match "\\w\\'" str)
1656 (concat (regexp-quote str)
1657 "\\>")
1658 (regexp-quote str)))
1659 'regexp-quote)
1660 (sort fail-list
1661 (lambda (a b)
1662 (> (length a) (length b))))
1663 "\\|")))))
1665 ;; Add our own grouping parenthesis around re instead of
1666 ;; passing adorn to `regexp-opt', since in XEmacs it makes the
1667 ;; top level grouping "shy".
1668 (cond ((eq adorn 'appendable)
1669 (concat "\\(" re "\\)"))
1670 (adorn
1671 (concat "\\(" re "\\)"
1672 "\\("
1673 (c-get-lang-constant 'c-nonsymbol-key nil mode)
1674 "\\|$\\)"))
1676 re)))
1678 ;; Produce a regexp that matches nothing.
1679 (if adorn
1680 "\\(\\<\\>\\)"
1681 "\\<\\>")))
1683 (put 'c-make-keywords-re 'lisp-indent-function 1)
1685 (defun c-make-bare-char-alt (chars &optional inverted)
1686 "Make a character alternative string from the list of characters CHARS.
1687 The returned string is of the type that can be used with
1688 `skip-chars-forward' and `skip-chars-backward'. If INVERTED is
1689 non-nil, a caret is prepended to invert the set."
1690 ;; This function ought to be in the elisp core somewhere.
1691 (let ((str (if inverted "^" "")) char char2)
1692 (setq chars (sort (append chars nil) `<))
1693 (while chars
1694 (setq char (pop chars))
1695 (if (memq char '(?\\ ?^ ?-))
1696 ;; Quoting necessary (this method only works in the skip
1697 ;; functions).
1698 (setq str (format "%s\\%c" str char))
1699 (setq str (format "%s%c" str char)))
1700 ;; Check for range.
1701 (setq char2 char)
1702 (while (and chars (>= (1+ char2) (car chars)))
1703 (setq char2 (pop chars)))
1704 (unless (= char char2)
1705 (if (< (1+ char) char2)
1706 (setq str (format "%s-%c" str char2))
1707 (push char2 chars))))
1708 str))
1710 ;; Leftovers from (X)Emacs 19 compatibility.
1711 (defalias 'c-regexp-opt 'regexp-opt)
1712 (defalias 'c-regexp-opt-depth 'regexp-opt-depth)
1715 ;; Figure out what features this Emacs has
1717 (cc-bytecomp-defvar open-paren-in-column-0-is-defun-start)
1719 (defvar lookup-syntax-properties) ;XEmacs.
1721 (defconst c-emacs-features
1722 (let (list)
1724 (if (boundp 'infodock-version)
1725 ;; I've no idea what this actually is, but it's legacy. /mast
1726 (setq list (cons 'infodock list)))
1728 ;; XEmacs uses 8-bit modify-syntax-entry flags.
1729 ;; Emacs uses a 1-bit flag. We will have to set up our
1730 ;; syntax tables differently to handle this.
1731 (let ((table (copy-syntax-table))
1732 entry)
1733 (modify-syntax-entry ?a ". 12345678" table)
1734 (cond
1735 ;; Emacs
1736 ((arrayp table)
1737 (setq entry (aref table ?a))
1738 ;; In Emacs, table entries are cons cells
1739 (if (consp entry) (setq entry (car entry))))
1740 ;; XEmacs
1741 ((fboundp 'get-char-table)
1742 (setq entry (get-char-table ?a table)))
1743 ;; incompatible
1744 (t (error "CC Mode is incompatible with this version of Emacs")))
1745 (setq list (cons (if (= (logand (lsh entry -16) 255) 255)
1746 '8-bit
1747 '1-bit)
1748 list)))
1750 ;; Check whether beginning/end-of-defun call
1751 ;; beginning/end-of-defun-function nicely, passing through the
1752 ;; argument and respecting the return code.
1753 (let* (mark-ring
1754 (bod-param 'foo) (eod-param 'foo)
1755 (beginning-of-defun-function
1756 (lambda (&optional arg)
1757 (or (eq bod-param 'foo) (setq bod-param 'bar))
1758 (and (eq bod-param 'foo)
1759 (setq bod-param arg)
1760 (eq arg 3))))
1761 (end-of-defun-function
1762 (lambda (&optional arg)
1763 (and (eq eod-param 'foo)
1764 (setq eod-param arg)
1765 (eq arg 3)))))
1766 (if (save-excursion (and (beginning-of-defun 3) (eq bod-param 3)
1767 (not (beginning-of-defun))
1768 (end-of-defun 3) (eq eod-param 3)
1769 (not (end-of-defun))))
1770 (setq list (cons 'argumentative-bod-function list))))
1772 ;; Record whether the `category' text property works.
1773 (if c-use-category (setq list (cons 'category-properties list)))
1775 (let ((buf (generate-new-buffer " test"))
1776 parse-sexp-lookup-properties
1777 parse-sexp-ignore-comments
1778 lookup-syntax-properties) ; XEmacs
1779 (with-current-buffer buf
1780 (set-syntax-table (make-syntax-table))
1782 ;; For some reason we have to set some of these after the
1783 ;; buffer has been made current. (Specifically,
1784 ;; `parse-sexp-ignore-comments' in Emacs 21.)
1785 (setq parse-sexp-lookup-properties t
1786 parse-sexp-ignore-comments t
1787 lookup-syntax-properties t)
1789 ;; Find out if the `syntax-table' text property works.
1790 (modify-syntax-entry ?< ".")
1791 (modify-syntax-entry ?> ".")
1792 (insert "<()>")
1793 (c-mark-<-as-paren (point-min))
1794 (c-mark->-as-paren (+ 3 (point-min)))
1795 (goto-char (point-min))
1796 (c-forward-sexp)
1797 (if (= (point) (+ 4 (point-min)))
1798 (setq list (cons 'syntax-properties list))
1799 (error (concat
1800 "CC Mode is incompatible with this version of Emacs - "
1801 "support for the `syntax-table' text property "
1802 "is required.")))
1804 ;; Find out if "\\s!" (generic comment delimiters) work.
1805 (c-safe
1806 (modify-syntax-entry ?x "!")
1807 (if (string-match "\\s!" "x")
1808 (setq list (cons 'gen-comment-delim list))))
1810 ;; Find out if "\\s|" (generic string delimiters) work.
1811 (c-safe
1812 (modify-syntax-entry ?x "|")
1813 (if (string-match "\\s|" "x")
1814 (setq list (cons 'gen-string-delim list))))
1816 ;; See if POSIX char classes work.
1817 (when (and (string-match "[[:alpha:]]" "a")
1818 ;; All versions of Emacs 21 so far haven't fixed
1819 ;; char classes in `skip-chars-forward' and
1820 ;; `skip-chars-backward'.
1821 (progn
1822 (delete-region (point-min) (point-max))
1823 (insert "foo123")
1824 (skip-chars-backward "[:alnum:]")
1825 (bobp))
1826 (= (skip-chars-forward "[:alpha:]") 3))
1827 (setq list (cons 'posix-char-classes list)))
1829 ;; See if `open-paren-in-column-0-is-defun-start' exists and
1830 ;; isn't buggy (Emacs >= 21.4).
1831 (when (boundp 'open-paren-in-column-0-is-defun-start)
1832 (let ((open-paren-in-column-0-is-defun-start nil)
1833 (parse-sexp-ignore-comments t))
1834 (delete-region (point-min) (point-max))
1835 (set-syntax-table (make-syntax-table))
1836 (modify-syntax-entry ?\' "\"")
1837 (cond
1838 ;; XEmacs. Afaik this is currently an Emacs-only
1839 ;; feature, but it's good to be prepared.
1840 ((memq '8-bit list)
1841 (modify-syntax-entry ?/ ". 1456")
1842 (modify-syntax-entry ?* ". 23"))
1843 ;; Emacs
1844 ((memq '1-bit list)
1845 (modify-syntax-entry ?/ ". 124b")
1846 (modify-syntax-entry ?* ". 23")))
1847 (modify-syntax-entry ?\n "> b")
1848 (insert "/* '\n () */")
1849 (backward-sexp)
1850 (if (bobp)
1851 (setq list (cons 'col-0-paren list)))))
1853 (set-buffer-modified-p nil))
1854 (kill-buffer buf))
1856 ;; See if `parse-partial-sexp' returns the eighth element.
1857 (if (c-safe (>= (length (save-excursion
1858 (parse-partial-sexp (point) (point))))
1859 10))
1860 (setq list (cons 'pps-extended-state list))
1861 (error (concat
1862 "CC Mode is incompatible with this version of Emacs - "
1863 "`parse-partial-sexp' has to return at least 10 elements.")))
1865 ;;(message "c-emacs-features: %S" list)
1866 list)
1867 "A list of certain features in the (X)Emacs you are using.
1868 There are many flavors of Emacs out there, each with different
1869 features supporting those needed by CC Mode. The following values
1870 might be present:
1872 '8-bit 8 bit syntax entry flags (XEmacs style).
1873 '1-bit 1 bit syntax entry flags (Emacs style).
1874 'argumentative-bod-function beginning-of-defun and end-of-defun pass
1875 ARG through to beginning/end-of-defun-function.
1876 'syntax-properties It works to override the syntax for specific characters
1877 in the buffer with the 'syntax-table property. It's
1878 always set - CC Mode no longer works in emacsen without
1879 this feature.
1880 'category-properties Syntax routines can add a level of indirection to text
1881 properties using the 'category property.
1882 'gen-comment-delim Generic comment delimiters work
1883 (i.e. the syntax class `!').
1884 'gen-string-delim Generic string delimiters work
1885 (i.e. the syntax class `|').
1886 'pps-extended-state `parse-partial-sexp' returns a list with at least 10
1887 elements, i.e. it contains the position of the start of
1888 the last comment or string. It's always set - CC Mode
1889 no longer works in emacsen without this feature.
1890 'posix-char-classes The regexp engine understands POSIX character classes.
1891 'col-0-paren It's possible to turn off the ad-hoc rule that a paren
1892 in column zero is the start of a defun.
1893 'infodock This is Infodock (based on XEmacs).
1895 '8-bit and '1-bit are mutually exclusive.")
1898 ;;; Some helper constants.
1900 ;; If the regexp engine supports POSIX char classes then we can use
1901 ;; them to handle extended charsets correctly.
1902 (if (memq 'posix-char-classes c-emacs-features)
1903 (progn
1904 (defconst c-alpha "[:alpha:]")
1905 (defconst c-alnum "[:alnum:]")
1906 (defconst c-digit "[:digit:]")
1907 (defconst c-upper "[:upper:]")
1908 (defconst c-lower "[:lower:]"))
1909 (defconst c-alpha "a-zA-Z")
1910 (defconst c-alnum "a-zA-Z0-9")
1911 (defconst c-digit "0-9")
1912 (defconst c-upper "A-Z")
1913 (defconst c-lower "a-z"))
1916 ;;; System for handling language dependent constants.
1918 ;; This is used to set various language dependent data in a flexible
1919 ;; way: Language constants can be built from the values of other
1920 ;; language constants, also those for other languages. They can also
1921 ;; process the values of other language constants uniformly across all
1922 ;; the languages. E.g. one language constant can list all the type
1923 ;; keywords in each language, and another can build a regexp for each
1924 ;; language from those lists without code duplication.
1926 ;; Language constants are defined with `c-lang-defconst', and their
1927 ;; value forms (referred to as source definitions) are evaluated only
1928 ;; on demand when requested for a particular language with
1929 ;; `c-lang-const'. It's therefore possible to refer to the values of
1930 ;; constants defined later in the file, or in another file, just as
1931 ;; long as all the relevant `c-lang-defconst' have been loaded when
1932 ;; `c-lang-const' is actually evaluated from somewhere else.
1934 ;; `c-lang-const' forms are also evaluated at compile time and
1935 ;; replaced with the values they produce. Thus there's no overhead
1936 ;; for this system when compiled code is used - only the values
1937 ;; actually used in the code are present, and the file(s) containing
1938 ;; the `c-lang-defconst' forms don't need to be loaded at all then.
1939 ;; There are however safeguards to make sure that they can be loaded
1940 ;; to get the source definitions for the values if there's a mismatch
1941 ;; in compiled versions, or if `c-lang-const' is used uncompiled.
1943 ;; Note that the source definitions in a `c-lang-defconst' form are
1944 ;; compiled into the .elc file where it stands; there's no need to
1945 ;; load the source file to get it.
1947 ;; See cc-langs.el for more details about how this system is deployed
1948 ;; in CC Mode, and how the associated language variable system
1949 ;; (`c-lang-defvar') works. That file also contains a lot of
1950 ;; examples.
1952 (defun c-add-language (mode base-mode)
1953 "Declare a new language in the language dependent variable system.
1954 This is intended to be used by modes that inherit CC Mode to add new
1955 languages. It should be used at the top level before any calls to
1956 `c-lang-defconst'. MODE is the mode name symbol for the new language,
1957 and BASE-MODE is the mode name symbol for the language in CC Mode that
1958 is to be the template for the new mode.
1960 The exact effect of BASE-MODE is to make all language constants that
1961 haven't got a setting in the new language fall back to their values in
1962 BASE-MODE. It does not have any effect outside the language constant
1963 system."
1964 (unless (string-match "\\`\\(.*-\\)mode\\'" (symbol-name mode))
1965 (error "The mode name symbol `%s' must end with \"-mode\"" mode))
1966 (put mode 'c-mode-prefix (match-string 1 (symbol-name mode)))
1967 (unless (get base-mode 'c-mode-prefix)
1968 (error "Unknown base mode `%s'" base-mode))
1969 (put mode 'c-fallback-mode base-mode))
1971 (defvar c-lang-constants (make-vector 151 0))
1972 ;; Obarray used as a cache to keep track of the language constants.
1973 ;; The constants stored are those defined by `c-lang-defconst' and the values
1974 ;; computed by `c-lang-const'. It's mostly used at compile time but it's not
1975 ;; stored in compiled files.
1977 ;; The obarray contains all the language constants as symbols. The
1978 ;; value cells hold the evaluated values as alists where each car is
1979 ;; the mode name symbol and the corresponding cdr is the evaluated
1980 ;; value in that mode. The property lists hold the source definitions
1981 ;; and other miscellaneous data. The obarray might also contain
1982 ;; various other symbols, but those don't have any variable bindings.
1984 (defvar c-lang-const-expansion nil)
1986 (defsubst c-get-current-file ()
1987 ;; Return the base name of the current file.
1988 (let ((file (cond
1989 (load-in-progress
1990 ;; Being loaded.
1991 load-file-name)
1992 ((and (boundp 'byte-compile-dest-file)
1993 (stringp byte-compile-dest-file))
1994 ;; Being compiled.
1995 byte-compile-dest-file)
1997 ;; Being evaluated interactively.
1998 (buffer-file-name)))))
1999 (and file
2000 (file-name-sans-extension
2001 (file-name-nondirectory file)))))
2003 (defmacro c-lang-defconst-eval-immediately (form)
2004 "Can be used inside a VAL in `c-lang-defconst' to evaluate FORM
2005 immediately, i.e. at the same time as the `c-lang-defconst' form
2006 itself is evaluated."
2007 ;; Evaluate at macro expansion time, i.e. in the
2008 ;; `c--macroexpand-all' inside `c-lang-defconst'.
2009 (eval form))
2011 (defmacro c-lang-defconst (name &rest args)
2012 "Set the language specific values of the language constant NAME.
2013 The second argument can optionally be a docstring. The rest of the
2014 arguments are one or more repetitions of LANG VAL where LANG specifies
2015 the language(s) that VAL applies to. LANG is the name of the
2016 language, i.e. the mode name without the \"-mode\" suffix, or a list
2017 of such language names, or `t' for all languages. VAL is a form to
2018 evaluate to get the value.
2020 If LANG isn't `t' or one of the core languages in CC Mode, it must
2021 have been declared with `c-add-language'.
2023 Neither NAME, LANG nor VAL are evaluated directly - they should not be
2024 quoted. `c-lang-defconst-eval-immediately' can however be used inside
2025 VAL to evaluate parts of it directly.
2027 When VAL is evaluated for some language, that language is temporarily
2028 made current so that `c-lang-const' without an explicit language can
2029 be used inside VAL to refer to the value of a language constant in the
2030 same language. That is particularly useful if LANG is `t'.
2032 VAL is not evaluated right away but rather when the value is requested
2033 with `c-lang-const'. Thus it's possible to use `c-lang-const' inside
2034 VAL to refer to language constants that haven't been defined yet.
2035 However, if the definition of a language constant is in another file
2036 then that file must be loaded \(at compile time) before it's safe to
2037 reference the constant.
2039 The assignments in ARGS are processed in sequence like `setq', so
2040 \(c-lang-const NAME) may be used inside a VAL to refer to the last
2041 assigned value to this language constant, or a value that it has
2042 gotten in another earlier loaded file.
2044 To work well with repeated loads and interactive reevaluation, only
2045 one `c-lang-defconst' for each NAME is permitted per file. If there
2046 already is one it will be completely replaced; the value in the
2047 earlier definition will not affect `c-lang-const' on the same
2048 constant. A file is identified by its base name."
2050 (let* ((sym (intern (symbol-name name) c-lang-constants))
2051 ;; Make `c-lang-const' expand to a straightforward call to
2052 ;; `c-get-lang-constant' in `c--macroexpand-all' below.
2054 ;; (The default behavior, i.e. to expand to a call inside
2055 ;; `eval-when-compile' should be equivalent, since that macro
2056 ;; should only expand to its content if it's used inside a
2057 ;; form that's already evaluated at compile time. It's
2058 ;; however necessary to use our cover macro
2059 ;; `cc-eval-when-compile' due to bugs in `eval-when-compile',
2060 ;; and it expands to a bulkier form that in this case only is
2061 ;; unnecessary garbage that we don't want to store in the
2062 ;; language constant source definitions.)
2063 (c-lang-const-expansion 'call)
2064 (c-langs-are-parametric t)
2065 bindings
2066 pre-files)
2068 (or (symbolp name)
2069 (error "Not a symbol: %S" name))
2071 (when (stringp (car-safe args))
2072 ;; The docstring is hardly used anywhere since there's no normal
2073 ;; symbol to attach it to. It's primarily for getting the right
2074 ;; format in the source.
2075 (put sym 'variable-documentation (car args))
2076 (setq args (cdr args)))
2078 (or args
2079 (error "No assignments in `c-lang-defconst' for %S" name))
2081 ;; Rework ARGS to an association list to make it easier to handle.
2082 ;; It's reversed at the same time to make it easier to implement
2083 ;; the demand-driven (i.e. reversed) evaluation in `c-lang-const'.
2084 (while args
2085 (let ((assigned-mode
2086 (cond ((eq (car args) t) t)
2087 ((symbolp (car args))
2088 (list (intern (concat (symbol-name (car args))
2089 "-mode"))))
2090 ((listp (car args))
2091 (mapcar (lambda (lang)
2092 (or (symbolp lang)
2093 (error "Not a list of symbols: %S"
2094 (car args)))
2095 (intern (concat (symbol-name lang)
2096 "-mode")))
2097 (car args)))
2098 (t (error "Not a symbol or a list of symbols: %S"
2099 (car args)))))
2100 val)
2102 (or (cdr args)
2103 (error "No value for %S" (car args)))
2104 (setq args (cdr args)
2105 val (car args))
2107 ;; Emacs has a weird bug where it seems to fail to read
2108 ;; backquote lists from byte compiled files correctly (,@
2109 ;; forms, to be specific), so make sure the bindings in the
2110 ;; expansion below don't contain any backquote stuff.
2111 ;; (XEmacs handles it correctly and doesn't need this for that
2112 ;; reason, but we also use this expansion handle
2113 ;; `c-lang-defconst-eval-immediately' and to register
2114 ;; dependencies on the `c-lang-const's in VAL.)
2115 (setq val (c--macroexpand-all val))
2117 (setq bindings `(cons (cons ',assigned-mode (lambda () ,val)) ,bindings)
2118 args (cdr args))))
2120 ;; Compile in the other files that have provided source
2121 ;; definitions for this symbol, to make sure the order in the
2122 ;; `source' property is correct even when files are loaded out of
2123 ;; order.
2124 (setq pre-files (nreverse
2125 ;; Reverse to get the right load order.
2126 (mapcar 'car (get sym 'source))))
2128 `(eval-and-compile
2129 (c-define-lang-constant ',name ,bindings
2130 ,@(and pre-files `(',pre-files))))))
2132 (put 'c-lang-defconst 'lisp-indent-function 1)
2133 ;(eval-after-load "edebug" ; 2006-07-09: def-edebug-spec is now in subr.el.
2135 (def-edebug-spec c-lang-defconst
2136 (&define name [&optional stringp] [&rest sexp def-form]))
2138 (defun c-define-lang-constant (name bindings &optional pre-files)
2139 ;; Used by `c-lang-defconst'.
2141 (let* ((sym (intern (symbol-name name) c-lang-constants))
2142 (source (get sym 'source))
2143 (file (intern
2144 (or (c-get-current-file)
2145 (error "`c-lang-defconst' must be used in a file"))))
2146 (elem (assq file source)))
2148 ;;(when (cdr-safe elem)
2149 ;; (message "Language constant %s redefined in %S" name file))
2151 ;; Note that the order in the source alist is relevant. Like how
2152 ;; `c-lang-defconst' reverses the bindings, this reverses the
2153 ;; order between files so that the last to evaluate comes first.
2154 (unless elem
2155 (while pre-files
2156 (unless (assq (car pre-files) source)
2157 (setq source (cons (list (car pre-files)) source)))
2158 (setq pre-files (cdr pre-files)))
2159 (put sym 'source (cons (setq elem (list file)) source)))
2161 (setcdr elem bindings)
2163 ;; Bind the symbol as a variable, or clear any earlier evaluated
2164 ;; value it has.
2165 (set sym nil)
2167 ;; Clear the evaluated values that depend on this source.
2168 (let ((agenda (get sym 'dependents))
2169 (visited (make-vector 101 0))
2170 ptr)
2171 (while agenda
2172 (setq sym (car agenda)
2173 agenda (cdr agenda))
2174 (intern (symbol-name sym) visited)
2175 (set sym nil)
2176 (setq ptr (get sym 'dependents))
2177 (while ptr
2178 (setq sym (car ptr)
2179 ptr (cdr ptr))
2180 (unless (intern-soft (symbol-name sym) visited)
2181 (setq agenda (cons sym agenda))))))
2183 name))
2185 (defmacro c-lang-const (name &optional lang)
2186 "Get the mode specific value of the language constant NAME in language LANG.
2187 LANG is the name of the language, i.e. the mode name without the
2188 \"-mode\" suffix. If used inside `c-lang-defconst' or
2189 `c-lang-defvar', LANG may be left out to refer to the current
2190 language. NAME and LANG are not evaluated so they should not be
2191 quoted."
2193 (or (symbolp name)
2194 (error "Not a symbol: %S" name))
2195 (or (symbolp lang)
2196 (error "Not a symbol: %S" lang))
2198 (let ((sym (intern (symbol-name name) c-lang-constants))
2199 (mode (when lang (intern (concat (symbol-name lang) "-mode")))))
2201 (or (get mode 'c-mode-prefix) (null mode)
2202 (error "Unknown language %S: no `c-mode-prefix' property"
2203 lang))
2205 (if (eq c-lang-const-expansion 'immediate)
2206 ;; No need to find out the source file(s) when we evaluate
2207 ;; immediately since all the info is already there in the
2208 ;; `source' property.
2209 `',(c-get-lang-constant name nil mode)
2211 (let ((source-files
2212 (let ((file (c-get-current-file)))
2213 (if file (setq file (intern file)))
2214 ;; Get the source file(s) that must be loaded to get the value
2215 ;; of the constant. If the symbol isn't defined yet we assume
2216 ;; that its definition will come later in this file, and thus
2217 ;; are no file dependencies needed.
2218 (nreverse
2219 ;; Reverse to get the right load order.
2220 (apply 'nconc
2221 (mapcar (lambda (elem)
2222 (if (eq file (car elem))
2223 nil ; Exclude our own file.
2224 (list (car elem))))
2225 (get sym 'source))))))
2226 ;; Make some effort to do a compact call to
2227 ;; `c-get-lang-constant' since it will be compiled in.
2228 (args (and mode `(',mode))))
2230 (if (or source-files args)
2231 (push (and source-files `',source-files) args))
2233 (if (or (eq c-lang-const-expansion 'call)
2234 (and (not c-lang-const-expansion)
2235 (not mode))
2236 load-in-progress
2237 (not (boundp 'byte-compile-dest-file))
2238 (not (stringp byte-compile-dest-file)))
2239 ;; Either a straight call is requested in the context, or
2240 ;; we're in an "uncontrolled" context and got no language,
2241 ;; or we're not being byte compiled so the compile time
2242 ;; stuff below is unnecessary.
2243 `(c-get-lang-constant ',name ,@args)
2245 ;; Being compiled. If the loading and compiling version is
2246 ;; the same we use a value that is evaluated at compile time,
2247 ;; otherwise it's evaluated at runtime.
2248 `(if (eq c-version-sym ',c-version-sym)
2249 (cc-eval-when-compile
2250 (c-get-lang-constant ',name ,@args))
2251 (c-get-lang-constant ',name ,@args)))))))
2253 (defvar c-lang-constants-under-evaluation nil
2254 "Alist of constants in the process of being evaluated.
2255 The `cdr' of each entry indicates how far we've looked in the list
2256 of definitions, so that the def for var FOO in c-mode can be defined in
2257 terms of the def for that same var FOO (which will then rely on the
2258 fallback definition for all modes, to break the cycle).")
2260 (defconst c-lang--novalue "novalue")
2262 (defun c-get-lang-constant (name &optional source-files mode)
2263 ;; Used by `c-lang-const'.
2265 (or mode
2266 (setq mode c-buffer-is-cc-mode)
2267 (error "No current language"))
2269 (let* ((sym (intern (symbol-name name) c-lang-constants))
2270 (source (get sym 'source))
2271 elem
2272 (eval-in-sym (and c-lang-constants-under-evaluation
2273 (caar c-lang-constants-under-evaluation))))
2275 ;; Record the dependencies between this symbol and the one we're
2276 ;; being evaluated in.
2277 (when eval-in-sym
2278 (or (memq eval-in-sym (get sym 'dependents))
2279 (put sym 'dependents (cons eval-in-sym (get sym 'dependents)))))
2281 ;; Make sure the source files have entries on the `source'
2282 ;; property so that loading will take place when necessary.
2283 (while source-files
2284 (unless (assq (car source-files) source)
2285 (put sym 'source
2286 (setq source (cons (list (car source-files)) source)))
2287 ;; Might pull in more definitions which affect the value. The
2288 ;; clearing of dependent values etc is done when the
2289 ;; definition is encountered during the load; this is just to
2290 ;; jump past the check for a cached value below.
2291 (set sym nil))
2292 (setq source-files (cdr source-files)))
2294 (if (and (boundp sym)
2295 (setq elem (assq mode (symbol-value sym))))
2296 (cdr elem)
2298 ;; Check if an evaluation of this symbol is already underway.
2299 ;; In that case we just continue with the "assignment" before
2300 ;; the one currently being evaluated, thereby creating the
2301 ;; illusion if a `setq'-like sequence of assignments.
2302 (let* ((c-buffer-is-cc-mode mode)
2303 (source-pos
2304 (or (assq sym c-lang-constants-under-evaluation)
2305 (cons sym (vector source nil))))
2306 ;; Append `c-lang-constants-under-evaluation' even if an
2307 ;; earlier entry is found. It's only necessary to get
2308 ;; the recording of dependencies above correct.
2309 (c-lang-constants-under-evaluation
2310 (cons source-pos c-lang-constants-under-evaluation))
2311 (fallback (get mode 'c-fallback-mode))
2312 value
2313 ;; Make sure the recursion limits aren't very low
2314 ;; since the `c-lang-const' dependencies can go deep.
2315 (max-specpdl-size (max max-specpdl-size 3000))
2316 (max-lisp-eval-depth (max max-lisp-eval-depth 1000)))
2318 (if (if fallback
2319 (let ((backup-source-pos (copy-sequence (cdr source-pos))))
2320 (and
2321 ;; First try the original mode but don't accept an
2322 ;; entry matching all languages since the fallback
2323 ;; mode might have an explicit entry before that.
2324 (eq (setq value (c-find-assignment-for-mode
2325 (cdr source-pos) mode nil name))
2326 c-lang--novalue)
2327 ;; Try again with the fallback mode from the
2328 ;; original position. Note that
2329 ;; `c-buffer-is-cc-mode' still is the real mode if
2330 ;; language parameterization takes place.
2331 (eq (setq value (c-find-assignment-for-mode
2332 (setcdr source-pos backup-source-pos)
2333 fallback t name))
2334 c-lang--novalue)))
2335 ;; A simple lookup with no fallback mode.
2336 (eq (setq value (c-find-assignment-for-mode
2337 (cdr source-pos) mode t name))
2338 c-lang--novalue))
2339 (error
2340 "`%s' got no (prior) value in %S (might be a cyclic reference)"
2341 name mode))
2343 (condition-case err
2344 (setq value (funcall value))
2345 (error
2346 ;; Print a message to aid in locating the error. We don't
2347 ;; print the error itself since that will be done later by
2348 ;; some caller higher up.
2349 (message "Eval error in the `c-lang-defconst' for `%S' in %s:"
2350 sym mode)
2351 (makunbound sym)
2352 (signal (car err) (cdr err))))
2354 (set sym (cons (cons mode value) (symbol-value sym)))
2355 value))))
2357 (defun c-find-assignment-for-mode (source-pos mode match-any-lang _name)
2358 ;; Find the first assignment entry that applies to MODE at or after
2359 ;; SOURCE-POS. If MATCH-ANY-LANG is non-nil, entries with `t' as
2360 ;; the language list are considered to match, otherwise they don't.
2361 ;; On return SOURCE-POS is updated to point to the next assignment
2362 ;; after the returned one. If no assignment is found,
2363 ;; `c-lang--novalue' is returned as a magic value.
2365 ;; SOURCE-POS is a vector that points out a specific assignment in
2366 ;; the double alist that's used in the `source' property. The first
2367 ;; element is the position in the top alist which is indexed with
2368 ;; the source files, and the second element is the position in the
2369 ;; nested bindings alist.
2371 ;; NAME is only used for error messages.
2373 (catch 'found
2374 (let ((file-entry (elt source-pos 0))
2375 (assignment-entry (elt source-pos 1))
2376 assignment)
2378 (while (if assignment-entry
2380 ;; Handled the last assignment from one file, begin on the
2381 ;; next. Due to the check in `c-lang-defconst', we know
2382 ;; there's at least one.
2383 (when file-entry
2385 (unless (aset source-pos 1
2386 (setq assignment-entry (cdar file-entry)))
2387 ;; The file containing the source definitions has not
2388 ;; been loaded.
2389 (let ((file (symbol-name (caar file-entry)))
2390 (c-lang-constants-under-evaluation nil))
2391 ;;(message (concat "Loading %s to get the source "
2392 ;; "value for language constant %s")
2393 ;; file name)
2394 (load file nil t))
2396 (unless (setq assignment-entry (cdar file-entry))
2397 ;; The load didn't fill in the source for the
2398 ;; constant as expected. The situation is
2399 ;; probably that a derived mode was written for
2400 ;; and compiled with another version of CC Mode,
2401 ;; and the requested constant isn't in the
2402 ;; currently loaded one. Put in a dummy
2403 ;; assignment that matches no language.
2404 (setcdr (car file-entry)
2405 (setq assignment-entry (list (list nil))))))
2407 (aset source-pos 0 (setq file-entry (cdr file-entry)))
2410 (setq assignment (car assignment-entry))
2411 (aset source-pos 1
2412 (setq assignment-entry (cdr assignment-entry)))
2414 (when (if (listp (car assignment))
2415 (memq mode (car assignment))
2416 match-any-lang)
2417 (throw 'found (cdr assignment))))
2419 c-lang--novalue)))
2421 (defun c-lang-major-mode-is (mode)
2422 ;; `c-major-mode-is' expands to a call to this function inside
2423 ;; `c-lang-defconst'. Here we also match the mode(s) against any
2424 ;; fallback modes for the one in `c-buffer-is-cc-mode', so that
2425 ;; e.g. (c-major-mode-is 'c++-mode) is true in a derived language
2426 ;; that has c++-mode as base mode.
2427 (unless (listp mode)
2428 (setq mode (list mode)))
2429 (let (match (buf-mode c-buffer-is-cc-mode))
2430 (while (if (memq buf-mode mode)
2431 (progn
2432 (setq match t)
2433 nil)
2434 (setq buf-mode (get buf-mode 'c-fallback-mode))))
2435 match))
2438 (cc-provide 'cc-defs)
2440 ;;; Local Variables:
2441 ;;; indent-tabs-mode: t
2442 ;;; tab-width: 8
2443 ;;; End:
2444 ;;; cc-defs.el ends here