1 ;;; cc-defs.el --- compile time definitions for CC Mode
3 ;; Copyright (C) 1985, 1987, 1992-2013 Free Software Foundation, Inc.
5 ;; Authors: 2003- Alan Mackenzie
6 ;; 1998- Martin Stjernholm
7 ;; 1992-1999 Barry A. Warsaw
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
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/>.
33 ;; This file contains macros, defsubsts, and various other things that
34 ;; must be loaded early both during compilation and at runtime.
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)
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 zmacs-region-stays) ; XEmacs
53 (cc-bytecomp-defvar zmacs-regions) ; XEmacs
54 (cc-bytecomp-defvar mark-active) ; Emacs
55 (cc-bytecomp-defvar deactivate-mark) ; Emacs
56 (cc-bytecomp-defvar inhibit-point-motion-hooks) ; Emacs
57 (cc-bytecomp-defvar parse-sexp-lookup-properties) ; Emacs
58 (cc-bytecomp-defvar text-property-default-nonsticky) ; Emacs 21
59 (cc-bytecomp-defvar lookup-syntax-properties) ; XEmacs
60 (cc-bytecomp-defun string-to-syntax) ; Emacs 21
63 ;; cc-fix.el contains compatibility macros that should be used if
66 (if (or (/= (regexp-opt-depth "\\(\\(\\)\\)") 2)
67 (not (fboundp 'push)))
70 ; (eval-after-load "font-lock" ; 2006-07-09. font-lock is now preloaded
72 (if (and (featurep 'xemacs) ; There is now (2005/12) code in GNU Emacs CVS
73 ; to make the call to f-l-c-k throw an error.
74 (not (featurep 'cc-fix)) ; only load the file once.
75 (let (font-lock-keywords)
76 (font-lock-compile-keywords '("\\<\\>"))
77 font-lock-keywords)) ; did the previous call foul this up?
80 ;; The above takes care of the delayed loading, but this is necessary
81 ;; to ensure correct byte compilation.
83 (if (and (featurep 'xemacs)
84 (not (featurep 'cc-fix))
87 (let (font-lock-keywords)
88 (font-lock-compile-keywords '("\\<\\>"))
93 ;;; Variables also used at compile time.
95 (defconst c-version "5.32.5"
96 "CC Mode version number.")
98 (defconst c-version-sym (intern c-version))
99 ;; A little more compact and faster in comparisons.
101 (defvar c-buffer-is-cc-mode nil
102 "Non-nil for all buffers with a major mode derived from CC Mode.
103 Otherwise, this variable is nil. I.e. this variable is non-nil for
104 `c-mode', `c++-mode', `objc-mode', `java-mode', `idl-mode',
105 `pike-mode', `awk-mode', and any other non-CC Mode mode that calls
106 `c-initialize-cc-mode'. The value is the mode symbol itself
107 \(i.e. `c-mode' etc) of the original CC Mode mode, or just t if it's
109 (make-variable-buffer-local 'c-buffer-is-cc-mode)
111 ;; Have to make `c-buffer-is-cc-mode' permanently local so that it
112 ;; survives the initialization of the derived mode.
113 (put 'c-buffer-is-cc-mode 'permanent-local t)
116 ;; The following is used below during compilation.
118 (defvar c-inside-eval-when-compile nil)
120 (defmacro cc-eval-when-compile (&rest body)
121 "Like `progn', but evaluates the body at compile time.
122 The result of the body appears to the compiler as a quoted constant.
124 This variant works around bugs in `eval-when-compile' in various
125 \(X)Emacs versions. See cc-defs.el for details."
127 (if c-inside-eval-when-compile
128 ;; XEmacs 21.4.6 has a bug in `eval-when-compile' in that it
129 ;; evaluates its body at macro expansion time if it's nested
130 ;; inside another `eval-when-compile'. So we use a dynamically
131 ;; bound variable to avoid nesting them.
135 ;; In all (X)Emacsen so far, `eval-when-compile' byte compiles
136 ;; its contents before evaluating it. That can cause forms to
137 ;; be compiled in situations they aren't intended to be
140 ;; Example: It's not possible to defsubst a primitive, e.g. the
141 ;; following will produce an error (in any emacs flavor), since
142 ;; `nthcdr' is a primitive function that's handled specially by
143 ;; the byte compiler and thus can't be redefined:
145 ;; (defsubst nthcdr (val) val)
147 ;; `defsubst', like `defmacro', needs to be evaluated at
148 ;; compile time, so this will produce an error during byte
151 ;; CC Mode occasionally needs to do things like this for
152 ;; cross-emacs compatibility. It therefore uses the following
153 ;; to conditionally do a `defsubst':
155 ;; (eval-when-compile
156 ;; (if (not (fboundp 'foo))
157 ;; (defsubst foo ...)))
159 ;; But `eval-when-compile' byte compiles its contents and
160 ;; _then_ evaluates it (in all current emacs versions, up to
161 ;; and including Emacs 20.6 and XEmacs 21.1 as of this
162 ;; writing). So this will still produce an error, since the
163 ;; byte compiler will get to the defsubst anyway. That's
164 ;; arguably a bug because the point with `eval-when-compile' is
165 ;; that it should evaluate rather than compile its contents.
167 ;; We get around it by expanding the body to a quoted
168 ;; constant that we eval. That otoh introduce a problem in
169 ;; that a returned lambda expression doesn't get byte
170 ;; compiled (even if `function' is used).
171 (eval '(let ((c-inside-eval-when-compile t)) ,@body)))))
173 (put 'cc-eval-when-compile 'lisp-indent-hook 0))
178 (defmacro c-point (position &optional point)
179 "Return the value of certain commonly referenced POSITIONs relative to POINT.
180 The current point is used if POINT isn't specified. POSITION can be
181 one of the following symbols:
183 `bol' -- beginning of line
185 `bod' -- beginning of defun
186 `eod' -- end of defun
187 `boi' -- beginning of indentation
188 `ionl' -- indentation of next line
189 `iopl' -- indentation of previous line
190 `bonl' -- beginning of next line
191 `eonl' -- end of next line
192 `bopl' -- beginning of previous line
193 `eopl' -- end of previous line
194 `bosws' -- beginning of syntactic whitespace
195 `eosws' -- end of syntactic whitespace
197 If the referenced position doesn't exist, the closest accessible point
198 to it is returned. This function does not modify the point or the mark."
200 (if (eq (car-safe position) 'quote)
201 (let ((position (eval position)))
205 (if (and (cc-bytecomp-fboundp 'line-beginning-position) (not point))
206 `(line-beginning-position)
208 ,@(if point `((goto-char ,point)))
213 (if (and (cc-bytecomp-fboundp 'line-end-position) (not point))
216 ,@(if point `((goto-char ,point)))
222 ,@(if point `((goto-char ,point)))
223 (back-to-indentation)
228 ,@(if point `((goto-char ,point)))
229 (c-beginning-of-defun-1)
234 ,@(if point `((goto-char ,point)))
239 (if (and (cc-bytecomp-fboundp 'line-beginning-position) (not point))
240 `(line-beginning-position 0)
242 ,@(if point `((goto-char ,point)))
247 (if (and (cc-bytecomp-fboundp 'line-beginning-position) (not point))
248 `(line-beginning-position 2)
250 ,@(if point `((goto-char ,point)))
255 (if (and (cc-bytecomp-fboundp 'line-end-position) (not point))
256 `(line-end-position 0)
258 ,@(if point `((goto-char ,point)))
260 (or (bobp) (backward-char))
264 (if (and (cc-bytecomp-fboundp 'line-end-position) (not point))
265 `(line-end-position 2)
267 ,@(if point `((goto-char ,point)))
274 ,@(if point `((goto-char ,point)))
276 (back-to-indentation)
281 ,@(if point `((goto-char ,point)))
283 (back-to-indentation)
286 ((eq position 'bosws)
288 ,@(if point `((goto-char ,point)))
289 (c-backward-syntactic-ws)
292 ((eq position 'eosws)
294 ,@(if point `((goto-char ,point)))
295 (c-forward-syntactic-ws)
298 (t (error "Unknown buffer position requested: %s" position))))
300 ;; The bulk of this should perhaps be in a function to avoid large
301 ;; expansions, but this case is not used anywhere in CC Mode (and
302 ;; probably not anywhere else either) so we only have it to be on
304 (message "Warning: c-point long expansion")
306 ,@(if point `((goto-char ,point)))
307 (let ((position ,position))
309 ((eq position 'bol) (beginning-of-line))
310 ((eq position 'eol) (end-of-line))
311 ((eq position 'boi) (back-to-indentation))
312 ((eq position 'bod) (c-beginning-of-defun-1))
313 ((eq position 'eod) (c-end-of-defun-1))
314 ((eq position 'bopl) (forward-line -1))
315 ((eq position 'bonl) (forward-line 1))
316 ((eq position 'eopl) (progn
318 (or (bobp) (backward-char))))
319 ((eq position 'eonl) (progn
322 ((eq position 'iopl) (progn
324 (back-to-indentation)))
325 ((eq position 'ionl) (progn
327 (back-to-indentation)))
328 ((eq position 'bosws) (c-backward-syntactic-ws))
329 ((eq position 'eosws) (c-forward-syntactic-ws))
330 (t (error "Unknown buffer position requested: %s" position))))
333 (defmacro c-region-is-active-p ()
334 ;; Return t when the region is active. The determination of region
335 ;; activeness is different in both Emacs and XEmacs.
336 ;; FIXME? Emacs has region-active-p since 23.1, so maybe this test
337 ;; should be updated.
338 (if (cc-bytecomp-boundp 'mark-active)
344 (defmacro c-set-region-active (activate)
345 ;; Activate the region if ACTIVE is non-nil, deactivate it
346 ;; otherwise. Covers the differences between Emacs and XEmacs.
347 (if (fboundp 'zmacs-activate-region)
350 (zmacs-activate-region)
351 (zmacs-deactivate-region))
353 `(setq mark-active ,activate)))
355 (defmacro c-delete-and-extract-region (start end)
356 "Delete the text between START and END and return it."
357 (if (cc-bytecomp-fboundp 'delete-and-extract-region)
358 ;; Emacs 21.1 and later
359 `(delete-and-extract-region ,start ,end)
360 ;; XEmacs and Emacs 20.x
362 (buffer-substring ,start ,end)
363 (delete-region ,start ,end))))
365 (defmacro c-safe (&rest body)
366 ;; safely execute BODY, return nil if an error occurred
370 (put 'c-safe 'lisp-indent-function 0)
372 (defmacro c-int-to-char (integer)
373 ;; In Emacs, a character is an integer. In XEmacs, a character is a
374 ;; type distinct from an integer. Sometimes we need to convert integers to
375 ;; characters. `c-int-to-char' makes this conversion, if necessary.
376 (if (fboundp 'int-to-char)
377 `(int-to-char ,integer)
380 (defmacro c-last-command-char ()
381 ;; The last character just typed. Note that `last-command-event' exists in
382 ;; both Emacs and XEmacs, but with confusingly different meanings.
383 (if (featurep 'xemacs)
385 'last-command-event))
387 (defmacro c-sentence-end ()
388 ;; Get the regular expression `sentence-end'.
389 (if (cc-bytecomp-fboundp 'sentence-end)
392 ;; Emacs <22 + XEmacs
395 (defmacro c-default-value-sentence-end ()
396 ;; Get the default value of the variable sentence end.
397 (if (cc-bytecomp-fboundp 'sentence-end)
399 `(let (sentence-end) (sentence-end))
400 ;; Emacs <22 + XEmacs
401 `(default-value 'sentence-end)))
403 ;; The following is essentially `save-buffer-state' from lazy-lock.el.
404 ;; It ought to be a standard macro.
405 (defmacro c-save-buffer-state (varlist &rest body)
406 "Bind variables according to VARLIST (in `let*' style) and eval BODY,
407 then restore the buffer state under the assumption that no significant
408 modification has been made in BODY. A change is considered
409 significant if it affects the buffer text in any way that isn't
410 completely restored again. Changes in text properties like `face' or
411 `syntax-table' are considered insignificant. This macro allows text
412 properties to be changed, even in a read-only buffer.
414 This macro should be placed around all calculations which set
415 \"insignificant\" text properties in a buffer, even when the buffer is
416 known to be writable. That way, these text properties remain set
417 even if the user undoes the command which set them.
419 This macro should ALWAYS be placed around \"temporary\" internal buffer
420 changes \(like adding a newline to calculate a text-property then
421 deleting it again\), so that the user never sees them on his
422 `buffer-undo-list'. See also `c-tentative-buffer-changes'.
424 However, any user-visible changes to the buffer \(like auto-newlines\)
425 must not be within a `c-save-buffer-state', since the user then
426 wouldn't be able to undo them.
428 The return value is the value of the last form in BODY."
429 `(let* ((modified (buffer-modified-p)) (buffer-undo-list t)
430 (inhibit-read-only t) (inhibit-point-motion-hooks t)
431 before-change-functions after-change-functions
433 buffer-file-name buffer-file-truename ; Prevent primitives checking
434 ; for file modification
440 (set-buffer-modified-p nil)))))
441 (put 'c-save-buffer-state 'lisp-indent-function 1)
443 (defmacro c-tentative-buffer-changes (&rest body)
444 "Eval BODY and optionally restore the buffer contents to the state it
445 was in before BODY. Any changes are kept if the last form in BODY
446 returns non-nil. Otherwise it's undone using the undo facility, and
447 various other buffer state that might be affected by the changes is
448 restored. That includes the current buffer, point, mark, mark
449 activation \(similar to `save-excursion'), and the modified state.
450 The state is also restored if BODY exits nonlocally.
452 If BODY makes a change that unconditionally is undone then wrap this
453 macro inside `c-save-buffer-state'. That way the change can be done
454 even when the buffer is read-only, and without interference from
455 various buffer change hooks."
456 `(let (-tnt-chng-keep
459 ;; Insert an undo boundary for use with `undo-more'. We
460 ;; don't use `undo-boundary' since it doesn't insert one
462 (setq buffer-undo-list (cons nil buffer-undo-list)
463 -tnt-chng-state (c-tnt-chng-record-state)
464 -tnt-chng-keep (progn ,@body))
465 (c-tnt-chng-cleanup -tnt-chng-keep -tnt-chng-state))))
466 (put 'c-tentative-buffer-changes 'lisp-indent-function 0)
468 (defun c-tnt-chng-record-state ()
469 ;; Used internally in `c-tentative-buffer-changes'.
470 (vector buffer-undo-list ; 0
472 ;; No need to use markers for the point and mark; if the
473 ;; undo got out of synch we're hosed anyway.
476 (c-region-is-active-p) ; 4
477 (buffer-modified-p))) ; 5
479 (defun c-tnt-chng-cleanup (keep saved-state)
480 ;; Used internally in `c-tentative-buffer-changes'.
482 (let ((saved-undo-list (elt saved-state 0)))
483 (if (eq buffer-undo-list saved-undo-list)
484 ;; No change was done after all.
485 (setq buffer-undo-list (cdr saved-undo-list))
488 ;; Find and remove the undo boundary.
489 (let ((p buffer-undo-list))
490 (while (not (eq (cdr p) saved-undo-list))
492 (setcdr p (cdr saved-undo-list)))
494 ;; `primitive-undo' will remove the boundary.
495 (setq saved-undo-list (cdr saved-undo-list))
496 (let ((undo-in-progress t))
497 (while (not (eq (setq buffer-undo-list
498 (primitive-undo 1 buffer-undo-list))
501 (when (buffer-live-p (elt saved-state 1))
502 (set-buffer (elt saved-state 1))
503 (goto-char (elt saved-state 2))
504 (set-mark (elt saved-state 3))
505 (c-set-region-active (elt saved-state 4))
506 (and (not (elt saved-state 5))
508 (set-buffer-modified-p nil)))))))
510 (defmacro c-forward-syntactic-ws (&optional limit)
511 "Forward skip over syntactic whitespace.
512 Syntactic whitespace is defined as whitespace characters, comments,
513 and preprocessor directives. However if point starts inside a comment
514 or preprocessor directive, the content of it is not treated as
517 LIMIT sets an upper limit of the forward movement, if specified. If
518 LIMIT or the end of the buffer is reached inside a comment or
519 preprocessor directive, the point will be left there.
521 Note that this function might do hidden buffer changes. See the
522 comment at the start of cc-engine.el for more info."
525 (narrow-to-region (point-min) (or ,limit (point-max)))
529 (defmacro c-backward-syntactic-ws (&optional limit)
530 "Backward skip over syntactic whitespace.
531 Syntactic whitespace is defined as whitespace characters, comments,
532 and preprocessor directives. However if point starts inside a comment
533 or preprocessor directive, the content of it is not treated as
536 LIMIT sets a lower limit of the backward movement, if specified. If
537 LIMIT is reached inside a line comment or preprocessor directive then
538 the point is moved into it past the whitespace at the end.
540 Note that this function might do hidden buffer changes. See the
541 comment at the start of cc-engine.el for more info."
544 (narrow-to-region (or ,limit (point-min)) (point-max))
548 (defmacro c-forward-sexp (&optional count)
549 "Move forward across COUNT balanced expressions.
550 A negative COUNT means move backward. Signal an error if the move
551 fails for any reason.
553 This is like `forward-sexp' except that it isn't interactive and does
554 not do any user friendly adjustments of the point and that it isn't
555 susceptible to user configurations such as disabling of signals in
557 (or count (setq count 1))
558 `(goto-char (scan-sexps (point) ,count)))
560 (defmacro c-backward-sexp (&optional count)
561 "See `c-forward-sexp' and reverse directions."
562 (or count (setq count 1))
563 `(c-forward-sexp ,(if (numberp count) (- count) `(- ,count))))
565 (defmacro c-safe-scan-lists (from count depth &optional limit)
566 "Like `scan-lists' but returns nil instead of signaling errors
567 for unbalanced parens.
569 A limit for the search may be given. FROM is assumed to be on the
571 (let ((res (if (featurep 'xemacs)
572 `(scan-lists ,from ,count ,depth nil t)
573 `(c-safe (scan-lists ,from ,count ,depth)))))
578 `(narrow-to-region ,limit (point-max))
579 `(narrow-to-region (point-min) ,limit))
581 (narrow-to-region ,limit (point-max))
582 (narrow-to-region (point-min) ,limit)))
587 ;; Wrappers for common scan-lists cases, mainly because it's almost
588 ;; impossible to get a feel for how that function works.
590 (defmacro c-go-list-forward ()
591 "Move backward across one balanced group of parentheses.
593 Return POINT when we succeed, NIL when we fail. In the latter case, leave
595 `(c-safe (let ((endpos (scan-lists (point) 1 0)))
599 (defmacro c-go-list-backward ()
600 "Move backward across one balanced group of parentheses.
602 Return POINT when we succeed, NIL when we fail. In the latter case, leave
604 `(c-safe (let ((endpos (scan-lists (point) -1 0)))
608 (defmacro c-up-list-forward (&optional pos limit)
609 "Return the first position after the list sexp containing POS,
610 or nil if no such position exists. The point is used if POS is left out.
612 A limit for the search may be given. The start position is assumed to
614 `(c-safe-scan-lists ,(or pos `(point)) 1 1 ,limit))
616 (defmacro c-up-list-backward (&optional pos limit)
617 "Return the position of the start of the list sexp containing POS,
618 or nil if no such position exists. The point is used if POS is left out.
620 A limit for the search may be given. The start position is assumed to
622 `(c-safe-scan-lists ,(or pos `(point)) -1 1 ,limit))
624 (defmacro c-down-list-forward (&optional pos limit)
625 "Return the first position inside the first list sexp after POS,
626 or nil if no such position exists. The point is used if POS is left out.
628 A limit for the search may be given. The start position is assumed to
630 `(c-safe-scan-lists ,(or pos `(point)) 1 -1 ,limit))
632 (defmacro c-down-list-backward (&optional pos limit)
633 "Return the last position inside the last list sexp before POS,
634 or nil if no such position exists. The point is used if POS is left out.
636 A limit for the search may be given. The start position is assumed to
638 `(c-safe-scan-lists ,(or pos `(point)) -1 -1 ,limit))
640 (defmacro c-go-up-list-forward (&optional pos limit)
641 "Move the point to the first position after the list sexp containing POS,
642 or containing the point if POS is left out. Return t if such a
643 position exists, otherwise nil is returned and the point isn't moved.
645 A limit for the search may be given. The start position is assumed to
647 (let ((res `(c-safe (goto-char (scan-lists ,(or pos `(point)) 1 1)) t)))
650 (narrow-to-region (point-min) ,limit)
654 (defmacro c-go-up-list-backward (&optional pos limit)
655 "Move the point to the position of the start of the list sexp containing POS,
656 or containing the point if POS is left out. Return t if such a
657 position exists, otherwise nil is returned and the point isn't moved.
659 A limit for the search may be given. The start position is assumed to
661 (let ((res `(c-safe (goto-char (scan-lists ,(or pos `(point)) -1 1)) t)))
664 (narrow-to-region ,limit (point-max))
668 (defmacro c-go-down-list-forward (&optional pos limit)
669 "Move the point to the first position inside the first list sexp after POS,
670 or before the point if POS is left out. Return t if such a position
671 exists, otherwise nil is returned and the point isn't moved.
673 A limit for the search may be given. The start position is assumed to
675 (let ((res `(c-safe (goto-char (scan-lists ,(or pos `(point)) 1 -1)) t)))
678 (narrow-to-region (point-min) ,limit)
682 (defmacro c-go-down-list-backward (&optional pos limit)
683 "Move the point to the last position inside the last list sexp before POS,
684 or before the point if POS is left out. Return t if such a position
685 exists, otherwise nil is returned and the point isn't moved.
687 A limit for the search may be given. The start position is assumed to
689 (let ((res `(c-safe (goto-char (scan-lists ,(or pos `(point)) -1 -1)) t)))
692 (narrow-to-region ,limit (point-max))
697 (defmacro c-beginning-of-defun-1 ()
698 ;; Wrapper around beginning-of-defun.
700 ;; NOTE: This function should contain the only explicit use of
701 ;; beginning-of-defun in CC Mode. Eventually something better than
702 ;; b-o-d will be available and this should be the only place the
703 ;; code needs to change. Everything else should use
704 ;; (c-beginning-of-defun-1)
706 ;; This is really a bit too large to be a macro but that isn't a
707 ;; problem as long as it only is used in one place in
711 (if (and ,(fboundp 'buffer-syntactic-context-depth)
712 c-enable-xemacs-performance-kludge-p)
713 ,(when (fboundp 'buffer-syntactic-context-depth)
714 ;; XEmacs only. This can improve the performance of
715 ;; c-parse-state to between 3 and 60 times faster when
716 ;; braces are hung. It can also degrade performance by
717 ;; about as much when braces are not hung.
718 '(let (beginning-of-defun-function end-of-defun-function
723 (setq pos (c-safe-scan-lists
724 (point) -1 (buffer-syntactic-context-depth))))
726 ((bobp) (setq pos (point-min)))
728 (let ((distance (skip-chars-backward "^{")))
729 ;; unbalanced parenthesis, while invalid C code,
730 ;; shouldn't cause an infloop! See unbal.c
731 (when (zerop distance)
734 (setq pos (point)))))
736 ((not (eq (char-after pos) ?{))
741 ;; Emacs, which doesn't have buffer-syntactic-context-depth
742 (let (beginning-of-defun-function end-of-defun-function)
743 (beginning-of-defun)))
744 ;; if defun-prompt-regexp is non-nil, b-o-d won't leave us at the
746 (and defun-prompt-regexp
747 (looking-at defun-prompt-regexp)
748 (goto-char (match-end 0)))))
751 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
752 ;; V i r t u a l S e m i c o l o n s
754 ;; In most CC Mode languages, statements are terminated explicitly by
755 ;; semicolons or closing braces. In some of the CC modes (currently AWK Mode
756 ;; and certain user-specified #define macros in C, C++, etc. (November 2008)),
757 ;; statements are (or can be) terminated by EOLs. Such a statement is said to
758 ;; be terminated by a "virtual semicolon" (VS). A statement terminated by an
759 ;; actual semicolon or brace is never considered to have a VS.
761 ;; The indentation engine (or whatever) tests for a VS at a specific position
762 ;; by invoking the macro `c-at-vsemi-p', which in its turn calls the mode
763 ;; specific function (if any) which is the value of the language variable
764 ;; `c-at-vsemi-p-fn'. This function should only use "low-level" features of
765 ;; CC Mode, i.e. features which won't trigger infinite recursion. ;-) The
766 ;; actual details of what constitutes a VS in a language are thus encapsulated
767 ;; in code specific to that language (e.g. cc-awk.el). `c-at-vsemi-p' returns
768 ;; non-nil if point (or the optional parameter POS) is at a VS, nil otherwise.
770 ;; The language specific function might well do extensive analysis of the
771 ;; source text, and may use a caching scheme to speed up repeated calls.
773 ;; The "virtual semicolon" lies just after the last non-ws token on the line.
774 ;; Like POINT, it is considered to lie between two characters. For example,
775 ;; at the place shown in the following AWK source line:
777 ;; kbyte = 1024 # 1000 if you're not picky
782 ;; In addition to `c-at-vsemi-p-fn', a mode may need to supply a function for
783 ;; `c-vsemi-status-unknown-p-fn'. The macro `c-vsemi-status-unknown-p' is a
784 ;; rather recondite kludge. It exists because the function
785 ;; `c-beginning-of-statement-1' sometimes tests for VSs as an optimization,
786 ;; but `c-at-vsemi-p' might well need to call `c-beginning-of-statement-1' in
787 ;; its calculations, thus potentially leading to infinite recursion.
789 ;; The macro `c-vsemi-status-unknown-p' resolves this problem; it may return
790 ;; non-nil at any time; returning nil is a guarantee that an immediate
791 ;; invocation of `c-at-vsemi-p' at point will NOT call
792 ;; `c-beginning-of-statement-1'. `c-vsemi-status-unknown-p' may not itself
793 ;; call `c-beginning-of-statement-1'.
795 ;; The macro `c-vsemi-status-unknown-p' will typically check the caching
796 ;; scheme used by the `c-at-vsemi-p-fn', hence the name - the status is
797 ;; "unknown" if there is no cache entry current for the line.
798 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
800 (defmacro c-at-vsemi-p (&optional pos)
801 ;; Is there a virtual semicolon (not a real one or a }) at POS (defaults to
802 ;; point)? Always returns nil for languages which don't have Virtual
804 ;; This macro might do hidden buffer changes.
806 (funcall c-at-vsemi-p-fn ,@(if pos `(,pos)))))
808 (defmacro c-vsemi-status-unknown-p ()
809 ;; Return NIL only if it can be guaranteed that an immediate
810 ;; (c-at-vsemi-p) will NOT call c-beginning-of-statement-1. Otherwise,
811 ;; return non-nil. (See comments above). The function invoked by this
812 ;; macro MUST NOT UNDER ANY CIRCUMSTANCES itself call
813 ;; c-beginning-of-statement-1.
814 ;; Languages which don't have EOL terminated statements always return NIL
815 ;; (they _know_ there's no vsemi ;-).
816 `(if c-vsemi-status-unknown-p-fn (funcall c-vsemi-status-unknown-p-fn)))
819 (defmacro c-benign-error (format &rest args)
820 ;; Formats an error message for the echo area and dings, i.e. like
821 ;; `error' but doesn't abort.
823 (message ,format ,@args)
826 (defmacro c-with-syntax-table (table &rest code)
827 ;; Temporarily switches to the specified syntax table in a failsafe
828 ;; way to execute code.
829 `(let ((c-with-syntax-table-orig-table (syntax-table)))
832 (set-syntax-table ,table)
834 (set-syntax-table c-with-syntax-table-orig-table))))
835 (put 'c-with-syntax-table 'lisp-indent-function 1)
837 (defmacro c-skip-ws-forward (&optional limit)
838 "Skip over any whitespace following point.
839 This function skips over horizontal and vertical whitespace and line
842 `(let ((limit (or ,limit (point-max))))
844 ;; skip-syntax-* doesn't count \n as whitespace..
845 (skip-chars-forward " \t\n\r\f\v" limit)
846 (when (and (eq (char-after) ?\\)
850 (progn (backward-char) nil))))))
852 (skip-chars-forward " \t\n\r\f\v")
853 (when (eq (char-after) ?\\)
856 (progn (backward-char) nil)))))))
858 (defmacro c-skip-ws-backward (&optional limit)
859 "Skip over any whitespace preceding point.
860 This function skips over horizontal and vertical whitespace and line
863 `(let ((limit (or ,limit (point-min))))
865 ;; skip-syntax-* doesn't count \n as whitespace..
866 (skip-chars-backward " \t\n\r\f\v" limit)
868 (eq (char-before) ?\\)
872 (skip-chars-backward " \t\n\r\f\v")
874 (eq (char-before) ?\\)))
878 (defvar c-langs-are-parametric nil))
880 (defmacro c-major-mode-is (mode)
881 "Return non-nil if the current CC Mode major mode is MODE.
882 MODE is either a mode symbol or a list of mode symbols."
884 (if c-langs-are-parametric
885 ;; Inside a `c-lang-defconst'.
886 `(c-lang-major-mode-is ,mode)
888 (if (eq (car-safe mode) 'quote)
889 (let ((mode (eval mode)))
891 `(memq c-buffer-is-cc-mode ',mode)
892 `(eq c-buffer-is-cc-mode ',mode)))
896 (memq c-buffer-is-cc-mode mode)
897 (eq c-buffer-is-cc-mode mode))))))
900 ;; Macros/functions to handle so-called "char properties", which are
901 ;; properties set on a single character and that never spread to any
905 ;; Constant used at compile time to decide whether or not to use
906 ;; XEmacs extents. Check all the extent functions we'll use since
907 ;; some packages might add compatibility aliases for some of them in
909 (defconst c-use-extents (and (cc-bytecomp-fboundp 'extent-at)
910 (cc-bytecomp-fboundp 'set-extent-property)
911 (cc-bytecomp-fboundp 'set-extent-properties)
912 (cc-bytecomp-fboundp 'make-extent)
913 (cc-bytecomp-fboundp 'extent-property)
914 (cc-bytecomp-fboundp 'delete-extent)
915 (cc-bytecomp-fboundp 'map-extents))))
917 ;; `c-put-char-property' is complex enough in XEmacs and Emacs < 21 to
918 ;; make it a function.
919 (defalias 'c-put-char-property-fun
920 (cc-eval-when-compile
924 (lambda (pos property value)
925 (let ((ext (extent-at pos nil property)))
927 (set-extent-property ext property value)
928 (set-extent-properties (make-extent pos (1+ pos))
934 ((not (cc-bytecomp-boundp 'text-property-default-nonsticky))
935 ;; In Emacs < 21 we have to mess with the `rear-nonsticky' property.
937 (lambda (pos property value)
938 (put-text-property pos (1+ pos) property value)
939 (let ((prop (get-text-property pos 'rear-nonsticky)))
940 (or (memq property prop)
941 (put-text-property pos (1+ pos)
943 (cons property prop)))))))
944 ;; This won't be used for anything.
946 (cc-bytecomp-defun c-put-char-property-fun) ; Make it known below.
948 (defmacro c-put-char-property (pos property value)
949 ;; Put the given property with the given value on the character at
950 ;; POS and make it front and rear nonsticky, or start and end open
951 ;; in XEmacs vocabulary. If the character already has the given
952 ;; property then the value is replaced, and the behavior is
953 ;; undefined if that property has been put by some other function.
954 ;; PROPERTY is assumed to be constant.
956 ;; If there's a `text-property-default-nonsticky' variable (Emacs
957 ;; 21) then it's assumed that the property is present on it.
959 ;; This macro does a hidden buffer change.
960 (setq property (eval property))
961 (if (or c-use-extents
962 (not (cc-bytecomp-boundp 'text-property-default-nonsticky)))
963 ;; XEmacs and Emacs < 21.
964 `(c-put-char-property-fun ,pos ',property ,value)
965 ;; In Emacs 21 we got the `rear-nonsticky' property covered
966 ;; by `text-property-default-nonsticky'.
968 (put-text-property -pos- (1+ -pos-) ',property ,value))))
970 (defmacro c-get-char-property (pos property)
971 ;; Get the value of the given property on the character at POS if
972 ;; it's been put there by `c-put-char-property'. PROPERTY is
973 ;; assumed to be constant.
974 (setq property (eval property))
977 `(let ((ext (extent-at ,pos nil ',property)))
978 (if ext (extent-property ext ',property)))
980 `(get-text-property ,pos ',property)))
982 ;; `c-clear-char-property' is complex enough in Emacs < 21 to make it
983 ;; a function, since we have to mess with the `rear-nonsticky' property.
984 (defalias 'c-clear-char-property-fun
985 (cc-eval-when-compile
986 (unless (or c-use-extents
987 (cc-bytecomp-boundp 'text-property-default-nonsticky))
989 (lambda (pos property)
990 (when (get-text-property pos property)
991 (remove-text-properties pos (1+ pos) (list property nil))
992 (put-text-property pos (1+ pos)
994 (delq property (get-text-property
995 pos 'rear-nonsticky)))))))))
996 (cc-bytecomp-defun c-clear-char-property-fun) ; Make it known below.
998 (defmacro c-clear-char-property (pos property)
999 ;; Remove the given property on the character at POS if it's been put
1000 ;; there by `c-put-char-property'. PROPERTY is assumed to be
1003 ;; This macro does a hidden buffer change.
1004 (setq property (eval property))
1005 (cond (c-use-extents
1007 `(let ((ext (extent-at ,pos nil ',property)))
1008 (if ext (delete-extent ext))))
1009 ((cc-bytecomp-boundp 'text-property-default-nonsticky)
1010 ;; In Emacs 21 we got the `rear-nonsticky' property covered
1011 ;; by `text-property-default-nonsticky'.
1013 (remove-text-properties pos (1+ pos)
1017 `(c-clear-char-property-fun ,pos ',property))))
1019 (defmacro c-clear-char-properties (from to property)
1020 ;; Remove all the occurrences of the given property in the given
1021 ;; region that has been put with `c-put-char-property'. PROPERTY is
1022 ;; assumed to be constant.
1024 ;; Note that this function does not clean up the property from the
1025 ;; lists of the `rear-nonsticky' properties in the region, if such
1026 ;; are used. Thus it should not be used for common properties like
1029 ;; This macro does hidden buffer changes.
1030 (setq property (eval property))
1033 `(map-extents (lambda (ext ignored)
1034 (delete-extent ext))
1035 nil ,from ,to nil nil ',property)
1037 `(remove-text-properties ,from ,to '(,property nil))))
1039 (defmacro c-search-forward-char-property (property value &optional limit)
1040 "Search forward for a text-property PROPERTY having value VALUE.
1041 LIMIT bounds the search. The comparison is done with `equal'.
1043 Leave point just after the character, and set the match data on
1044 this character, and return point. If VALUE isn't found, Return
1045 nil; point is then left undefined."
1046 `(let ((place (point)))
1049 (< place ,(or limit '(point-max)))
1050 (not (equal (get-text-property place ,property) ,value)))
1051 (setq place (next-single-property-change
1052 place ,property nil ,(or limit '(point-max)))))
1053 (when (< place ,(or limit '(point-max)))
1055 (search-forward-regexp ".") ; to set the match-data.
1058 (defmacro c-search-backward-char-property (property value &optional limit)
1059 "Search backward for a text-property PROPERTY having value VALUE.
1060 LIMIT bounds the search. The comparison is done with `equal'.
1062 Leave point just before the character, set the match data on this
1063 character, and return point. If VALUE isn't found, Return nil;
1064 point is then left undefined."
1065 `(let ((place (point)))
1068 (> place ,(or limit '(point-min)))
1069 (not (equal (get-text-property (1- place) ,property) ,value)))
1070 (setq place (previous-single-property-change
1071 place ,property nil ,(or limit '(point-min)))))
1072 (when (> place ,(or limit '(point-max)))
1074 (search-backward-regexp ".") ; to set the match-data.
1077 (defun c-clear-char-property-with-value-function (from to property value)
1078 "Remove all text-properties PROPERTY from the region (FROM, TO)
1079 which have the value VALUE, as tested by `equal'. These
1080 properties are assumed to be over individual characters, having
1081 been put there by c-put-char-property. POINT remains unchanged."
1082 (let ((place from) end-place)
1083 (while ; loop round occurrences of (PROPERTY VALUE)
1085 (while ; loop round changes in PROPERTY till we find VALUE
1088 (not (equal (get-text-property place property) value)))
1089 (setq place (next-single-property-change place property nil to)))
1091 (setq end-place (next-single-property-change place property nil to))
1092 (remove-text-properties place end-place (cons property nil))
1093 ;; Do we have to do anything with stickiness here?
1094 (setq place end-place))))
1096 (defmacro c-clear-char-property-with-value (from to property value)
1097 "Remove all text-properties PROPERTY from the region [FROM, TO)
1098 which have the value VALUE, as tested by `equal'. These
1099 properties are assumed to be over individual characters, having
1100 been put there by c-put-char-property. POINT remains unchanged."
1103 `(let ((-property- ,property))
1104 (map-extents (lambda (ext val)
1105 (if (equal (extent-property ext -property-) val)
1106 (delete-extent ext)))
1107 nil ,from ,to ,value nil -property-))
1109 `(c-clear-char-property-with-value-function ,from ,to ,property ,value)))
1111 ;; Macros to put overlays (Emacs) or extents (XEmacs) on buffer text.
1112 ;; For our purposes, these are characterized by being possible to
1113 ;; remove again without affecting the other text properties in the
1114 ;; buffer that got overridden when they were put.
1116 (defmacro c-put-overlay (from to property value)
1117 ;; Put an overlay/extent covering the given range in the current
1118 ;; buffer. It's currently undefined whether it's front/end sticky
1119 ;; or not. The overlay/extent object is returned.
1120 (if (cc-bytecomp-fboundp 'make-overlay)
1122 `(let ((ol (make-overlay ,from ,to)))
1123 (overlay-put ol ,property ,value)
1126 `(let ((ext (make-extent ,from ,to)))
1127 (set-extent-property ext ,property ,value)
1130 (defmacro c-delete-overlay (overlay)
1131 ;; Deletes an overlay/extent object previously retrieved using
1133 (if (cc-bytecomp-fboundp 'make-overlay)
1135 `(delete-overlay ,overlay)
1137 `(delete-extent ,overlay)))
1140 ;; Make edebug understand the macros.
1141 ;(eval-after-load "edebug" ; 2006-07-09: def-edebug-spec is now in subr.el.
1143 (def-edebug-spec cc-eval-when-compile t)
1144 (def-edebug-spec c-point t)
1145 (def-edebug-spec c-set-region-active t)
1146 (def-edebug-spec c-safe t)
1147 (def-edebug-spec c-save-buffer-state let*)
1148 (def-edebug-spec c-tentative-buffer-changes t)
1149 (def-edebug-spec c-forward-syntactic-ws t)
1150 (def-edebug-spec c-backward-syntactic-ws t)
1151 (def-edebug-spec c-forward-sexp t)
1152 (def-edebug-spec c-backward-sexp t)
1153 (def-edebug-spec c-up-list-forward t)
1154 (def-edebug-spec c-up-list-backward t)
1155 (def-edebug-spec c-down-list-forward t)
1156 (def-edebug-spec c-down-list-backward t)
1157 (def-edebug-spec c-add-syntax t)
1158 (def-edebug-spec c-add-class-syntax t)
1159 (def-edebug-spec c-benign-error t)
1160 (def-edebug-spec c-with-syntax-table t)
1161 (def-edebug-spec c-skip-ws-forward t)
1162 (def-edebug-spec c-skip-ws-backward t)
1163 (def-edebug-spec c-major-mode-is t)
1164 (def-edebug-spec c-put-char-property t)
1165 (def-edebug-spec c-get-char-property t)
1166 (def-edebug-spec c-clear-char-property t)
1167 (def-edebug-spec c-clear-char-properties t)
1168 (def-edebug-spec c-put-overlay t)
1169 (def-edebug-spec c-delete-overlay t) ;))
1174 ;; Note: All these after the macros, to be on safe side in avoiding
1175 ;; bugs where macros are defined too late. These bugs often only show
1176 ;; when the files are compiled in a certain order within the same
1179 (defsubst c-end-of-defun-1 ()
1180 ;; Replacement for end-of-defun that use c-beginning-of-defun-1.
1181 (let ((start (point)))
1182 ;; Skip forward into the next defun block. Don't bother to avoid
1183 ;; comments, literals etc, since beginning-of-defun doesn't do that
1185 (skip-chars-forward "^}")
1186 (c-beginning-of-defun-1)
1187 (if (eq (char-after) ?{)
1189 (if (< (point) start)
1190 (goto-char (point-max)))))
1192 (defconst c-<-as-paren-syntax '(4 . ?>))
1193 (put 'c-<-as-paren-syntax 'syntax-table c-<-as-paren-syntax)
1195 (defsubst c-mark-<-as-paren (pos)
1196 ;; Mark the "<" character at POS as a template opener using the
1197 ;; `syntax-table' property via the `category' property.
1199 ;; This function does a hidden buffer change. Note that we use
1200 ;; indirection through the `category' text property. This allows us to
1201 ;; toggle the property in all template brackets simultaneously and
1202 ;; cheaply. We use this, for instance, in `c-parse-state'.
1203 (c-put-char-property pos 'category 'c-<-as-paren-syntax))
1205 (defconst c->-as-paren-syntax '(5 . ?<))
1206 (put 'c->-as-paren-syntax 'syntax-table c->-as-paren-syntax)
1208 (defsubst c-mark->-as-paren (pos)
1209 ;; Mark the ">" character at POS as an sexp list closer using the
1210 ;; syntax-table property.
1212 ;; This function does a hidden buffer change. Note that we use
1213 ;; indirection through the `category' text property. This allows us to
1214 ;; toggle the property in all template brackets simultaneously and
1215 ;; cheaply. We use this, for instance, in `c-parse-state'.
1216 (c-put-char-property pos 'category 'c->-as-paren-syntax))
1218 (defsubst c-unmark-<->-as-paren (pos)
1219 ;; Unmark the "<" or "<" character at POS as an sexp list opener using
1220 ;; the syntax-table property indirectly through the `category' text
1223 ;; This function does a hidden buffer change. Note that we use
1224 ;; indirection through the `category' text property. This allows us to
1225 ;; toggle the property in all template brackets simultaneously and
1226 ;; cheaply. We use this, for instance, in `c-parse-state'.
1227 (c-clear-char-property pos 'category))
1229 (defsubst c-suppress-<->-as-parens ()
1230 ;; Suppress the syntactic effect of all marked < and > as parens. Note
1231 ;; that this effect is NOT buffer local. You should probably not use
1232 ;; this directly, but only through the macro
1233 ;; `c-with-<->-as-parens-suppressed'
1234 (put 'c-<-as-paren-syntax 'syntax-table nil)
1235 (put 'c->-as-paren-syntax 'syntax-table nil))
1237 (defsubst c-restore-<->-as-parens ()
1238 ;; Restore the syntactic effect of all marked <s and >s as parens. This
1239 ;; has no effect on unmarked <s and >s
1240 (put 'c-<-as-paren-syntax 'syntax-table c-<-as-paren-syntax)
1241 (put 'c->-as-paren-syntax 'syntax-table c->-as-paren-syntax))
1243 (defmacro c-with-<->-as-parens-suppressed (&rest forms)
1244 ;; Like progn, except that the paren property is suppressed on all
1245 ;; template brackets whilst they are running. This macro does a hidden
1249 (c-suppress-<->-as-parens)
1251 (c-restore-<->-as-parens)))
1255 (defconst c-cpp-delimiter '(14)) ; generic comment syntax
1256 ;; This is the value of the `category' text property placed on every #
1257 ;; which introduces a CPP construct and every EOL (or EOB, or character
1258 ;; preceding //, etc.) which terminates it. We can instantly "comment
1259 ;; out" all CPP constructs by giving `c-cpp-delimiter' a syntax-table
1260 ;; property '(14) (generic comment delimiter).
1261 (defmacro c-set-cpp-delimiters (beg end)
1262 ;; This macro does a hidden buffer change.
1264 (c-put-char-property ,beg 'category 'c-cpp-delimiter)
1265 (if (< ,end (point-max))
1266 (c-put-char-property ,end 'category 'c-cpp-delimiter))))
1267 (defmacro c-clear-cpp-delimiters (beg end)
1268 ;; This macro does a hidden buffer change.
1270 (c-clear-char-property ,beg 'category)
1271 (if (< ,end (point-max))
1272 (c-clear-char-property ,end 'category))))
1274 (defsubst c-comment-out-cpps ()
1275 ;; Render all preprocessor constructs syntactically commented out.
1276 (put 'c-cpp-delimiter 'syntax-table c-cpp-delimiter))
1277 (defsubst c-uncomment-out-cpps ()
1278 ;; Restore the syntactic visibility of preprocessor constructs.
1279 (put 'c-cpp-delimiter 'syntax-table nil))
1281 (defmacro c-with-cpps-commented-out (&rest forms)
1282 ;; Execute FORMS... whilst the syntactic effect of all characters in
1283 ;; all CPP regions is suppressed. In particular, this is to suppress
1284 ;; the syntactic significance of parens/braces/brackets to functions
1285 ;; such as `scan-lists' and `parse-partial-sexp'.
1287 (c-save-buffer-state ()
1288 (c-comment-out-cpps)
1290 (c-save-buffer-state ()
1291 (c-uncomment-out-cpps))))
1293 (defmacro c-with-all-but-one-cpps-commented-out (beg end &rest forms)
1294 ;; Execute FORMS... whilst the syntactic effect of all characters in
1295 ;; every CPP region APART FROM THE ONE BETWEEN BEG and END is
1298 (c-save-buffer-state ()
1299 (c-clear-cpp-delimiters ,beg ,end)
1300 ,`(c-with-cpps-commented-out ,@forms))
1301 (c-save-buffer-state ()
1302 (c-set-cpp-delimiters ,beg ,end))))
1304 (defsubst c-intersect-lists (list alist)
1305 ;; return the element of ALIST that matches the first element found
1306 ;; in LIST. Uses assq.
1309 (not (setq match (assq (car list) alist))))
1310 (setq list (cdr list)))
1313 (defsubst c-lookup-lists (list alist1 alist2)
1314 ;; first, find the first entry from LIST that is present in ALIST1,
1315 ;; then find the entry in ALIST2 for that entry.
1316 (assq (car (c-intersect-lists list alist1)) alist2))
1318 (defsubst c-langelem-sym (langelem)
1319 "Return the syntactic symbol in LANGELEM.
1321 LANGELEM is either a cons cell on the \"old\" form given as the first
1322 argument to lineup functions or a syntactic element on the \"new\"
1323 form as used in `c-syntactic-element'."
1326 (defsubst c-langelem-pos (langelem)
1327 "Return the anchor position in LANGELEM, or nil if there is none.
1329 LANGELEM is either a cons cell on the \"old\" form given as the first
1330 argument to lineup functions or a syntactic element on the \"new\"
1331 form as used in `c-syntactic-element'."
1332 (if (consp (cdr langelem))
1333 (car-safe (cdr langelem))
1336 (defun c-langelem-col (langelem &optional preserve-point)
1337 "Return the column of the anchor position in LANGELEM.
1338 Also move the point to that position unless PRESERVE-POINT is non-nil.
1340 LANGELEM is either a cons cell on the \"old\" form given as the first
1341 argument to lineup functions or a syntactic element on the \"new\"
1342 form as used in `c-syntactic-element'."
1343 (let ((pos (c-langelem-pos langelem))
1348 (prog1 (current-column)
1353 (defsubst c-langelem-2nd-pos (langelem)
1354 "Return the secondary position in LANGELEM, or nil if there is none.
1356 LANGELEM is typically a syntactic element on the \"new\" form as used
1357 in `c-syntactic-element'. It may also be a cons cell as passed in the
1358 first argument to lineup functions, but then the returned value always
1360 (car-safe (cdr-safe (cdr-safe langelem))))
1362 (defsubst c-keep-region-active ()
1363 ;; Do whatever is necessary to keep the region active in XEmacs.
1364 ;; This is not needed for Emacs.
1365 (and (boundp 'zmacs-region-stays)
1366 (setq zmacs-region-stays t)))
1368 (put 'c-mode 'c-mode-prefix "c-")
1369 (put 'c++-mode 'c-mode-prefix "c++-")
1370 (put 'objc-mode 'c-mode-prefix "objc-")
1371 (put 'java-mode 'c-mode-prefix "java-")
1372 (put 'idl-mode 'c-mode-prefix "idl-")
1373 (put 'pike-mode 'c-mode-prefix "pike-")
1374 (put 'awk-mode 'c-mode-prefix "awk-")
1376 (defsubst c-mode-symbol (suffix)
1377 "Prefix the current mode prefix (e.g. \"c-\") to SUFFIX and return
1378 the corresponding symbol."
1379 (or c-buffer-is-cc-mode
1380 (error "Not inside a CC Mode based mode"))
1381 (let ((mode-prefix (get c-buffer-is-cc-mode 'c-mode-prefix)))
1383 (error "%S has no mode prefix known to `c-mode-symbol'"
1384 c-buffer-is-cc-mode))
1385 (intern (concat mode-prefix suffix))))
1387 (defsubst c-mode-var (suffix)
1388 "Prefix the current mode prefix (e.g. \"c-\") to SUFFIX and return
1389 the value of the variable with that name."
1390 (symbol-value (c-mode-symbol suffix)))
1392 (defsubst c-got-face-at (pos faces)
1393 "Return non-nil if position POS in the current buffer has any of the
1394 faces in the list FACES."
1395 (let ((pos-faces (get-text-property pos 'face)))
1396 (if (consp pos-faces)
1398 (while (and pos-faces
1399 (not (memq (car pos-faces) faces)))
1400 (setq pos-faces (cdr pos-faces)))
1402 (memq pos-faces faces))))
1404 (defsubst c-face-name-p (facename)
1405 ;; Return t if FACENAME is the name of a face. This method is
1406 ;; necessary since facep in XEmacs only returns t for the actual
1407 ;; face objects (while it's only their names that are used just
1408 ;; about anywhere else) without providing a predicate that tests
1410 (memq facename (face-list)))
1412 (defun c-concat-separated (list separator)
1413 "Like `concat' on LIST, but separate each element with SEPARATOR.
1414 Notably, null elements in LIST are ignored."
1415 (mapconcat 'identity (delete nil (append list nil)) separator))
1417 (defun c-make-keywords-re (adorn list &optional mode)
1418 "Make a regexp that matches all the strings the list.
1419 Duplicates and nil elements in the list are removed. The resulting
1420 regexp may contain zero or more submatch expressions.
1422 If ADORN is t there will be at least one submatch and the first
1423 surrounds the matched alternative, and the regexp will also not match
1424 a prefix of any identifier. Adorned regexps cannot be appended. The
1425 language variable `c-nonsymbol-key' is used to make the adornment.
1427 A value 'appendable for ADORN is like above, but all alternatives in
1428 the list that end with a word constituent char will have \\> appended
1429 instead, so that the regexp remains appendable. Note that this
1430 variant doesn't always guarantee that an identifier prefix isn't
1431 matched since the symbol constituent '_' is normally considered a
1432 nonword token by \\>.
1434 The optional MODE specifies the language to get `c-nonsymbol-key' from
1435 when it's needed. The default is the current language taken from
1436 `c-buffer-is-cc-mode'."
1440 (unless (member elt unique)
1442 (setq list (delete nil unique)))
1446 (if (eq adorn 'appendable)
1447 ;; This is kludgy but it works: Search for a string that
1448 ;; doesn't occur in any word in LIST. Append it to all
1449 ;; the alternatives where we want to add \>. Run through
1450 ;; `regexp-opt' and then replace it with \>.
1451 (let ((unique "") pos)
1453 (setq unique (concat unique "@")
1456 (if (string-match unique (car pos))
1457 (progn (setq found t)
1460 (setq pos (cdr pos)))
1464 (if (string-match "\\w\\'" (car pos))
1465 (setcar pos (concat (car pos) unique)))
1466 (setq pos (cdr pos)))
1467 (setq re (regexp-opt list))
1469 (while (string-match unique re pos)
1470 (setq pos (+ (match-beginning 0) 2)
1471 re (replace-match "\\>" t t re))))
1473 (setq re (regexp-opt list)))
1475 ;; Emacs 20 and XEmacs (all versions so far) has a buggy
1476 ;; regexp-opt that doesn't always cope with strings containing
1477 ;; newlines. This kludge doesn't handle shy parens correctly
1478 ;; so we can't advice regexp-opt directly with it.
1481 (and (string-match "\n" (car list)) ; To speed it up a little.
1482 (not (string-match (concat "\\`\\(" re "\\)\\'")
1484 (setq fail-list (cons (car list) fail-list)))
1485 (setq list (cdr list)))
1490 (if (eq adorn 'appendable)
1492 (if (string-match "\\w\\'" str)
1493 (concat (regexp-quote str)
1495 (regexp-quote str)))
1499 (> (length a) (length b))))
1502 ;; Add our own grouping parenthesis around re instead of
1503 ;; passing adorn to `regexp-opt', since in XEmacs it makes the
1504 ;; top level grouping "shy".
1505 (cond ((eq adorn 'appendable)
1506 (concat "\\(" re "\\)"))
1508 (concat "\\(" re "\\)"
1510 (c-get-lang-constant 'c-nonsymbol-key nil mode)
1515 ;; Produce a regexp that matches nothing.
1520 (put 'c-make-keywords-re 'lisp-indent-function 1)
1522 (defun c-make-bare-char-alt (chars &optional inverted)
1523 "Make a character alternative string from the list of characters CHARS.
1524 The returned string is of the type that can be used with
1525 `skip-chars-forward' and `skip-chars-backward'. If INVERTED is
1526 non-nil, a caret is prepended to invert the set."
1527 ;; This function ought to be in the elisp core somewhere.
1528 (let ((str (if inverted "^" "")) char char2)
1529 (setq chars (sort (append chars nil) `<))
1531 (setq char (pop chars))
1532 (if (memq char '(?\\ ?^ ?-))
1533 ;; Quoting necessary (this method only works in the skip
1535 (setq str (format "%s\\%c" str char))
1536 (setq str (format "%s%c" str char)))
1539 (while (and chars (>= (1+ char2) (car chars)))
1540 (setq char2 (pop chars)))
1541 (unless (= char char2)
1542 (if (< (1+ char) char2)
1543 (setq str (format "%s-%c" str char2))
1544 (push char2 chars))))
1547 ;; Leftovers from (X)Emacs 19 compatibility.
1548 (defalias 'c-regexp-opt 'regexp-opt)
1549 (defalias 'c-regexp-opt-depth 'regexp-opt-depth)
1552 ;; Figure out what features this Emacs has
1554 (cc-bytecomp-defvar open-paren-in-column-0-is-defun-start)
1556 (defconst c-emacs-features
1559 (if (boundp 'infodock-version)
1560 ;; I've no idea what this actually is, but it's legacy. /mast
1561 (setq list (cons 'infodock list)))
1563 ;; XEmacs uses 8-bit modify-syntax-entry flags.
1564 ;; Emacs uses a 1-bit flag. We will have to set up our
1565 ;; syntax tables differently to handle this.
1566 (let ((table (copy-syntax-table))
1568 (modify-syntax-entry ?a ". 12345678" table)
1572 (setq entry (aref table ?a))
1573 ;; In Emacs, table entries are cons cells
1574 (if (consp entry) (setq entry (car entry))))
1576 ((fboundp 'get-char-table)
1577 (setq entry (get-char-table ?a table)))
1579 (t (error "CC Mode is incompatible with this version of Emacs")))
1580 (setq list (cons (if (= (logand (lsh entry -16) 255) 255)
1585 ;; Check whether beginning/end-of-defun call
1586 ;; beginning/end-of-defun-function nicely, passing through the
1587 ;; argument and respecting the return code.
1589 (bod-param 'foo) (eod-param 'foo)
1590 (beginning-of-defun-function
1591 (lambda (&optional arg)
1592 (or (eq bod-param 'foo) (setq bod-param 'bar))
1593 (and (eq bod-param 'foo)
1594 (setq bod-param arg)
1596 (end-of-defun-function
1597 (lambda (&optional arg)
1598 (and (eq eod-param 'foo)
1599 (setq eod-param arg)
1601 (if (save-excursion (and (beginning-of-defun 3) (eq bod-param 3)
1602 (not (beginning-of-defun))
1603 (end-of-defun 3) (eq eod-param 3)
1604 (not (end-of-defun))))
1605 (setq list (cons 'argumentative-bod-function list))))
1607 (let ((buf (generate-new-buffer " test"))
1608 parse-sexp-lookup-properties
1609 parse-sexp-ignore-comments
1610 lookup-syntax-properties)
1611 (with-current-buffer buf
1612 (set-syntax-table (make-syntax-table))
1614 ;; For some reason we have to set some of these after the
1615 ;; buffer has been made current. (Specifically,
1616 ;; `parse-sexp-ignore-comments' in Emacs 21.)
1617 (setq parse-sexp-lookup-properties t
1618 parse-sexp-ignore-comments t
1619 lookup-syntax-properties t)
1621 ;; Find out if the `syntax-table' text property works.
1622 (modify-syntax-entry ?< ".")
1623 (modify-syntax-entry ?> ".")
1625 (c-mark-<-as-paren (point-min))
1626 (c-mark->-as-paren (+ 3 (point-min)))
1627 (goto-char (point-min))
1629 (if (= (point) (+ 4 (point-min)))
1630 (setq list (cons 'syntax-properties list))
1632 "CC Mode is incompatible with this version of Emacs - "
1633 "support for the `syntax-table' text property "
1636 ;; Find out if generic comment delimiters work.
1638 (modify-syntax-entry ?x "!")
1639 (if (string-match "\\s!" "x")
1640 (setq list (cons 'gen-comment-delim list))))
1642 ;; Find out if generic string delimiters work.
1644 (modify-syntax-entry ?x "|")
1645 (if (string-match "\\s|" "x")
1646 (setq list (cons 'gen-string-delim list))))
1648 ;; See if POSIX char classes work.
1649 (when (and (string-match "[[:alpha:]]" "a")
1650 ;; All versions of Emacs 21 so far haven't fixed
1651 ;; char classes in `skip-chars-forward' and
1652 ;; `skip-chars-backward'.
1654 (delete-region (point-min) (point-max))
1656 (skip-chars-backward "[:alnum:]")
1658 (= (skip-chars-forward "[:alpha:]") 3))
1659 (setq list (cons 'posix-char-classes list)))
1661 ;; See if `open-paren-in-column-0-is-defun-start' exists and
1662 ;; isn't buggy (Emacs >= 21.4).
1663 (when (boundp 'open-paren-in-column-0-is-defun-start)
1664 (let ((open-paren-in-column-0-is-defun-start nil)
1665 (parse-sexp-ignore-comments t))
1666 (delete-region (point-min) (point-max))
1667 (set-syntax-table (make-syntax-table))
1668 (modify-syntax-entry ?\' "\"")
1670 ;; XEmacs. Afaik this is currently an Emacs-only
1671 ;; feature, but it's good to be prepared.
1673 (modify-syntax-entry ?/ ". 1456")
1674 (modify-syntax-entry ?* ". 23"))
1677 (modify-syntax-entry ?/ ". 124b")
1678 (modify-syntax-entry ?* ". 23")))
1679 (modify-syntax-entry ?\n "> b")
1680 (insert "/* '\n () */")
1683 (setq list (cons 'col-0-paren list)))))
1685 (set-buffer-modified-p nil))
1688 ;; See if `parse-partial-sexp' returns the eighth element.
1689 (if (c-safe (>= (length (save-excursion (parse-partial-sexp (point) (point))))
1691 (setq list (cons 'pps-extended-state list))
1693 "CC Mode is incompatible with this version of Emacs - "
1694 "`parse-partial-sexp' has to return at least 10 elements.")))
1696 ;;(message "c-emacs-features: %S" list)
1698 "A list of certain features in the (X)Emacs you are using.
1699 There are many flavors of Emacs out there, each with different
1700 features supporting those needed by CC Mode. The following values
1703 '8-bit 8 bit syntax entry flags (XEmacs style).
1704 '1-bit 1 bit syntax entry flags (Emacs style).
1705 'argumentative-bod-function beginning-of-defun passes ARG through
1706 to a non-null beginning-of-defun-function. It is assumed
1707 the end-of-defun does the same thing.
1708 'syntax-properties It works to override the syntax for specific characters
1709 in the buffer with the 'syntax-table property. It's
1710 always set - CC Mode no longer works in emacsen without
1712 'gen-comment-delim Generic comment delimiters work
1713 (i.e. the syntax class `!').
1714 'gen-string-delim Generic string delimiters work
1715 (i.e. the syntax class `|').
1716 'pps-extended-state `parse-partial-sexp' returns a list with at least 10
1717 elements, i.e. it contains the position of the start of
1718 the last comment or string. It's always set - CC Mode
1719 no longer works in emacsen without this feature.
1720 'posix-char-classes The regexp engine understands POSIX character classes.
1721 'col-0-paren It's possible to turn off the ad-hoc rule that a paren
1722 in column zero is the start of a defun.
1723 'infodock This is Infodock (based on XEmacs).
1725 '8-bit and '1-bit are mutually exclusive.")
1728 ;;; Some helper constants.
1730 ;; If the regexp engine supports POSIX char classes then we can use
1731 ;; them to handle extended charsets correctly.
1732 (if (memq 'posix-char-classes c-emacs-features)
1734 (defconst c-alpha "[:alpha:]")
1735 (defconst c-alnum "[:alnum:]")
1736 (defconst c-digit "[:digit:]")
1737 (defconst c-upper "[:upper:]")
1738 (defconst c-lower "[:lower:]"))
1739 (defconst c-alpha "a-zA-Z")
1740 (defconst c-alnum "a-zA-Z0-9")
1741 (defconst c-digit "0-9")
1742 (defconst c-upper "A-Z")
1743 (defconst c-lower "a-z"))
1746 ;;; System for handling language dependent constants.
1748 ;; This is used to set various language dependent data in a flexible
1749 ;; way: Language constants can be built from the values of other
1750 ;; language constants, also those for other languages. They can also
1751 ;; process the values of other language constants uniformly across all
1752 ;; the languages. E.g. one language constant can list all the type
1753 ;; keywords in each language, and another can build a regexp for each
1754 ;; language from those lists without code duplication.
1756 ;; Language constants are defined with `c-lang-defconst', and their
1757 ;; value forms (referred to as source definitions) are evaluated only
1758 ;; on demand when requested for a particular language with
1759 ;; `c-lang-const'. It's therefore possible to refer to the values of
1760 ;; constants defined later in the file, or in another file, just as
1761 ;; long as all the relevant `c-lang-defconst' have been loaded when
1762 ;; `c-lang-const' is actually evaluated from somewhere else.
1764 ;; `c-lang-const' forms are also evaluated at compile time and
1765 ;; replaced with the values they produce. Thus there's no overhead
1766 ;; for this system when compiled code is used - only the values
1767 ;; actually used in the code are present, and the file(s) containing
1768 ;; the `c-lang-defconst' forms don't need to be loaded at all then.
1769 ;; There are however safeguards to make sure that they can be loaded
1770 ;; to get the source definitions for the values if there's a mismatch
1771 ;; in compiled versions, or if `c-lang-const' is used uncompiled.
1773 ;; Note that the source definitions in a `c-lang-defconst' form are
1774 ;; compiled into the .elc file where it stands; there's no need to
1775 ;; load the source file to get it.
1777 ;; See cc-langs.el for more details about how this system is deployed
1778 ;; in CC Mode, and how the associated language variable system
1779 ;; (`c-lang-defvar') works. That file also contains a lot of
1782 (defun c-add-language (mode base-mode)
1783 "Declare a new language in the language dependent variable system.
1784 This is intended to be used by modes that inherit CC Mode to add new
1785 languages. It should be used at the top level before any calls to
1786 `c-lang-defconst'. MODE is the mode name symbol for the new language,
1787 and BASE-MODE is the mode name symbol for the language in CC Mode that
1788 is to be the template for the new mode.
1790 The exact effect of BASE-MODE is to make all language constants that
1791 haven't got a setting in the new language fall back to their values in
1792 BASE-MODE. It does not have any effect outside the language constant
1794 (unless (string-match "\\`\\(.*-\\)mode\\'" (symbol-name mode))
1795 (error "The mode name symbol `%s' must end with \"-mode\"" mode))
1796 (put mode 'c-mode-prefix (match-string 1 (symbol-name mode)))
1797 (unless (get base-mode 'c-mode-prefix)
1798 (error "Unknown base mode `%s'" base-mode))
1799 (put mode 'c-fallback-mode base-mode))
1801 (defvar c-lang-constants (make-vector 151 0))
1802 ;; This obarray is a cache to keep track of the language constants
1803 ;; defined by `c-lang-defconst' and the evaluated values returned by
1804 ;; `c-lang-const'. It's mostly used at compile time but it's not
1805 ;; stored in compiled files.
1807 ;; The obarray contains all the language constants as symbols. The
1808 ;; value cells hold the evaluated values as alists where each car is
1809 ;; the mode name symbol and the corresponding cdr is the evaluated
1810 ;; value in that mode. The property lists hold the source definitions
1811 ;; and other miscellaneous data. The obarray might also contain
1812 ;; various other symbols, but those don't have any variable bindings.
1814 (defvar c-lang-const-expansion nil)
1816 (defsubst c-get-current-file ()
1817 ;; Return the base name of the current file.
1822 ((and (boundp 'byte-compile-dest-file)
1823 (stringp byte-compile-dest-file))
1825 byte-compile-dest-file)
1827 ;; Being evaluated interactively.
1828 (buffer-file-name)))))
1829 (and file (file-name-base file))))
1831 (defmacro c-lang-defconst-eval-immediately (form)
1832 "Can be used inside a VAL in `c-lang-defconst' to evaluate FORM
1833 immediately, i.e. at the same time as the `c-lang-defconst' form
1834 itself is evaluated."
1835 ;; Evaluate at macro expansion time, i.e. in the
1836 ;; `cl-macroexpand-all' inside `c-lang-defconst'.
1839 ;; Only used at compile time - suppress "might not be defined at runtime".
1840 (declare-function cl-macroexpand-all "cl" (form &optional env))
1842 (defmacro c-lang-defconst (name &rest args)
1843 "Set the language specific values of the language constant NAME.
1844 The second argument can optionally be a docstring. The rest of the
1845 arguments are one or more repetitions of LANG VAL where LANG specifies
1846 the language(s) that VAL applies to. LANG is the name of the
1847 language, i.e. the mode name without the \"-mode\" suffix, or a list
1848 of such language names, or `t' for all languages. VAL is a form to
1849 evaluate to get the value.
1851 If LANG isn't `t' or one of the core languages in CC Mode, it must
1852 have been declared with `c-add-language'.
1854 Neither NAME, LANG nor VAL are evaluated directly - they should not be
1855 quoted. `c-lang-defconst-eval-immediately' can however be used inside
1856 VAL to evaluate parts of it directly.
1858 When VAL is evaluated for some language, that language is temporarily
1859 made current so that `c-lang-const' without an explicit language can
1860 be used inside VAL to refer to the value of a language constant in the
1861 same language. That is particularly useful if LANG is `t'.
1863 VAL is not evaluated right away but rather when the value is requested
1864 with `c-lang-const'. Thus it's possible to use `c-lang-const' inside
1865 VAL to refer to language constants that haven't been defined yet.
1866 However, if the definition of a language constant is in another file
1867 then that file must be loaded \(at compile time) before it's safe to
1868 reference the constant.
1870 The assignments in ARGS are processed in sequence like `setq', so
1871 \(c-lang-const NAME) may be used inside a VAL to refer to the last
1872 assigned value to this language constant, or a value that it has
1873 gotten in another earlier loaded file.
1875 To work well with repeated loads and interactive reevaluation, only
1876 one `c-lang-defconst' for each NAME is permitted per file. If there
1877 already is one it will be completely replaced; the value in the
1878 earlier definition will not affect `c-lang-const' on the same
1879 constant. A file is identified by its base name."
1881 (let* ((sym (intern (symbol-name name) c-lang-constants))
1882 ;; Make `c-lang-const' expand to a straightforward call to
1883 ;; `c-get-lang-constant' in `cl-macroexpand-all' below.
1885 ;; (The default behavior, i.e. to expand to a call inside
1886 ;; `eval-when-compile' should be equivalent, since that macro
1887 ;; should only expand to its content if it's used inside a
1888 ;; form that's already evaluated at compile time. It's
1889 ;; however necessary to use our cover macro
1890 ;; `cc-eval-when-compile' due to bugs in `eval-when-compile',
1891 ;; and it expands to a bulkier form that in this case only is
1892 ;; unnecessary garbage that we don't want to store in the
1893 ;; language constant source definitions.)
1894 (c-lang-const-expansion 'call)
1895 (c-langs-are-parametric t)
1900 (error "Not a symbol: %s" name))
1902 (when (stringp (car-safe args))
1903 ;; The docstring is hardly used anywhere since there's no normal
1904 ;; symbol to attach it to. It's primarily for getting the right
1905 ;; format in the source.
1906 (put sym 'variable-documentation (car args))
1907 (setq args (cdr args)))
1910 (error "No assignments in `c-lang-defconst' for %s" name))
1912 ;; Rework ARGS to an association list to make it easier to handle.
1913 ;; It's reversed at the same time to make it easier to implement
1914 ;; the demand-driven (i.e. reversed) evaluation in `c-lang-const'.
1916 (let ((assigned-mode
1917 (cond ((eq (car args) t) t)
1918 ((symbolp (car args))
1919 (list (intern (concat (symbol-name (car args))
1922 (mapcar (lambda (lang)
1924 (error "Not a list of symbols: %s"
1926 (intern (concat (symbol-name lang)
1929 (t (error "Not a symbol or a list of symbols: %s"
1934 (error "No value for %s" (car args)))
1935 (setq args (cdr args)
1938 ;; Emacs has a weird bug where it seems to fail to read
1939 ;; backquote lists from byte compiled files correctly (,@
1940 ;; forms, to be specific), so make sure the bindings in the
1941 ;; expansion below don't contain any backquote stuff.
1942 ;; (XEmacs handles it correctly and doesn't need this for that
1943 ;; reason, but we also use this expansion handle
1944 ;; `c-lang-defconst-eval-immediately' and to register
1945 ;; dependencies on the `c-lang-const's in VAL.)
1946 (setq val (cl-macroexpand-all val))
1948 (setq bindings (cons (cons assigned-mode val) bindings)
1951 ;; Compile in the other files that have provided source
1952 ;; definitions for this symbol, to make sure the order in the
1953 ;; `source' property is correct even when files are loaded out of
1955 (setq pre-files (nreverse
1956 ;; Reverse to get the right load order.
1957 (mapcar 'car (get sym 'source))))
1960 (c-define-lang-constant ',name ',bindings
1961 ,@(and pre-files `(',pre-files))))))
1963 (put 'c-lang-defconst 'lisp-indent-function 1)
1964 ;(eval-after-load "edebug" ; 2006-07-09: def-edebug-spec is now in subr.el.
1966 (def-edebug-spec c-lang-defconst
1967 (&define name [&optional stringp] [&rest sexp def-form]))
1969 (defun c-define-lang-constant (name bindings &optional pre-files)
1970 ;; Used by `c-lang-defconst'.
1972 (let* ((sym (intern (symbol-name name) c-lang-constants))
1973 (source (get sym 'source))
1975 (or (c-get-current-file)
1976 (error "`c-lang-defconst' must be used in a file"))))
1977 (elem (assq file source)))
1979 ;;(when (cdr-safe elem)
1980 ;; (message "Language constant %s redefined in %S" name file))
1982 ;; Note that the order in the source alist is relevant. Like how
1983 ;; `c-lang-defconst' reverses the bindings, this reverses the
1984 ;; order between files so that the last to evaluate comes first.
1987 (unless (assq (car pre-files) source)
1988 (setq source (cons (list (car pre-files)) source)))
1989 (setq pre-files (cdr pre-files)))
1990 (put sym 'source (cons (setq elem (list file)) source)))
1992 (setcdr elem bindings)
1994 ;; Bind the symbol as a variable, or clear any earlier evaluated
1998 ;; Clear the evaluated values that depend on this source.
1999 (let ((agenda (get sym 'dependents))
2000 (visited (make-vector 101 0))
2003 (setq sym (car agenda)
2004 agenda (cdr agenda))
2005 (intern (symbol-name sym) visited)
2007 (setq ptr (get sym 'dependents))
2011 (unless (intern-soft (symbol-name sym) visited)
2012 (setq agenda (cons sym agenda))))))
2016 (defmacro c-lang-const (name &optional lang)
2017 "Get the mode specific value of the language constant NAME in language LANG.
2018 LANG is the name of the language, i.e. the mode name without the
2019 \"-mode\" suffix. If used inside `c-lang-defconst' or
2020 `c-lang-defvar', LANG may be left out to refer to the current
2021 language. NAME and LANG are not evaluated so they should not be
2025 (error "Not a symbol: %s" name))
2027 (error "Not a symbol: %s" lang))
2029 (let ((sym (intern (symbol-name name) c-lang-constants))
2030 mode source-files args)
2033 (setq mode (intern (concat (symbol-name lang) "-mode")))
2034 (unless (get mode 'c-mode-prefix)
2036 "Unknown language %S since it got no `c-mode-prefix' property"
2037 (symbol-name lang))))
2039 (if (eq c-lang-const-expansion 'immediate)
2040 ;; No need to find out the source file(s) when we evaluate
2041 ;; immediately since all the info is already there in the
2042 ;; `source' property.
2043 `',(c-get-lang-constant name nil mode)
2045 (let ((file (c-get-current-file)))
2046 (if file (setq file (intern file)))
2047 ;; Get the source file(s) that must be loaded to get the value
2048 ;; of the constant. If the symbol isn't defined yet we assume
2049 ;; that its definition will come later in this file, and thus
2050 ;; are no file dependencies needed.
2051 (setq source-files (nreverse
2052 ;; Reverse to get the right load order.
2054 (mapcar (lambda (elem)
2055 (if (eq file (car elem))
2056 nil ; Exclude our own file.
2058 (get sym 'source))))))
2060 ;; Make some effort to do a compact call to
2061 ;; `c-get-lang-constant' since it will be compiled in.
2062 (setq args (and mode `(',mode)))
2063 (if (or source-files args)
2064 (setq args (cons (and source-files `',source-files)
2067 (if (or (eq c-lang-const-expansion 'call)
2068 (and (not c-lang-const-expansion)
2071 (not (boundp 'byte-compile-dest-file))
2072 (not (stringp byte-compile-dest-file)))
2073 ;; Either a straight call is requested in the context, or
2074 ;; we're in an "uncontrolled" context and got no language,
2075 ;; or we're not being byte compiled so the compile time
2076 ;; stuff below is unnecessary.
2077 `(c-get-lang-constant ',name ,@args)
2079 ;; Being compiled. If the loading and compiling version is
2080 ;; the same we use a value that is evaluated at compile time,
2081 ;; otherwise it's evaluated at runtime.
2082 `(if (eq c-version-sym ',c-version-sym)
2083 (cc-eval-when-compile
2084 (c-get-lang-constant ',name ,@args))
2085 (c-get-lang-constant ',name ,@args))))))
2087 (defvar c-lang-constants-under-evaluation nil)
2089 (defun c-get-lang-constant (name &optional source-files mode)
2090 ;; Used by `c-lang-const'.
2093 (setq mode c-buffer-is-cc-mode)
2094 (error "No current language"))
2096 (let* ((sym (intern (symbol-name name) c-lang-constants))
2097 (source (get sym 'source))
2099 (eval-in-sym (and c-lang-constants-under-evaluation
2100 (caar c-lang-constants-under-evaluation))))
2102 ;; Record the dependencies between this symbol and the one we're
2103 ;; being evaluated in.
2105 (or (memq eval-in-sym (get sym 'dependents))
2106 (put sym 'dependents (cons eval-in-sym (get sym 'dependents)))))
2108 ;; Make sure the source files have entries on the `source'
2109 ;; property so that loading will take place when necessary.
2111 (unless (assq (car source-files) source)
2113 (setq source (cons (list (car source-files)) source)))
2114 ;; Might pull in more definitions which affect the value. The
2115 ;; clearing of dependent values etc is done when the
2116 ;; definition is encountered during the load; this is just to
2117 ;; jump past the check for a cached value below.
2119 (setq source-files (cdr source-files)))
2121 (if (and (boundp sym)
2122 (setq elem (assq mode (symbol-value sym))))
2125 ;; Check if an evaluation of this symbol is already underway.
2126 ;; In that case we just continue with the "assignment" before
2127 ;; the one currently being evaluated, thereby creating the
2128 ;; illusion if a `setq'-like sequence of assignments.
2129 (let* ((c-buffer-is-cc-mode mode)
2131 (or (assq sym c-lang-constants-under-evaluation)
2132 (cons sym (vector source nil))))
2133 ;; Append `c-lang-constants-under-evaluation' even if an
2134 ;; earlier entry is found. It's only necessary to get
2135 ;; the recording of dependencies above correct.
2136 (c-lang-constants-under-evaluation
2137 (cons source-pos c-lang-constants-under-evaluation))
2138 (fallback (get mode 'c-fallback-mode))
2140 ;; Make sure the recursion limits aren't very low
2141 ;; since the `c-lang-const' dependencies can go deep.
2142 (max-specpdl-size (max max-specpdl-size 3000))
2143 (max-lisp-eval-depth (max max-lisp-eval-depth 1000)))
2146 (let ((backup-source-pos (copy-sequence (cdr source-pos))))
2148 ;; First try the original mode but don't accept an
2149 ;; entry matching all languages since the fallback
2150 ;; mode might have an explicit entry before that.
2151 (eq (setq value (c-find-assignment-for-mode
2152 (cdr source-pos) mode nil name))
2154 ;; Try again with the fallback mode from the
2155 ;; original position. Note that
2156 ;; `c-buffer-is-cc-mode' still is the real mode if
2157 ;; language parameterization takes place.
2158 (eq (setq value (c-find-assignment-for-mode
2159 (setcdr source-pos backup-source-pos)
2162 ;; A simple lookup with no fallback mode.
2163 (eq (setq value (c-find-assignment-for-mode
2164 (cdr source-pos) mode t name))
2167 "`%s' got no (prior) value in %s (might be a cyclic reference)"
2171 (setq value (eval value))
2173 ;; Print a message to aid in locating the error. We don't
2174 ;; print the error itself since that will be done later by
2175 ;; some caller higher up.
2176 (message "Eval error in the `c-lang-defconst' for `%s' in %s:"
2179 (signal (car err) (cdr err))))
2181 (set sym (cons (cons mode value) (symbol-value sym)))
2184 (defun c-find-assignment-for-mode (source-pos mode match-any-lang name)
2185 ;; Find the first assignment entry that applies to MODE at or after
2186 ;; SOURCE-POS. If MATCH-ANY-LANG is non-nil, entries with `t' as
2187 ;; the language list are considered to match, otherwise they don't.
2188 ;; On return SOURCE-POS is updated to point to the next assignment
2189 ;; after the returned one. If no assignment is found,
2190 ;; `c-lang-constants' is returned as a magic value.
2192 ;; SOURCE-POS is a vector that points out a specific assignment in
2193 ;; the double alist that's used in the `source' property. The first
2194 ;; element is the position in the top alist which is indexed with
2195 ;; the source files, and the second element is the position in the
2196 ;; nested bindings alist.
2198 ;; NAME is only used for error messages.
2201 (let ((file-entry (elt source-pos 0))
2202 (assignment-entry (elt source-pos 1))
2205 (while (if assignment-entry
2207 ;; Handled the last assignment from one file, begin on the
2208 ;; next. Due to the check in `c-lang-defconst', we know
2209 ;; there's at least one.
2212 (unless (aset source-pos 1
2213 (setq assignment-entry (cdar file-entry)))
2214 ;; The file containing the source definitions has not
2216 (let ((file (symbol-name (caar file-entry)))
2217 (c-lang-constants-under-evaluation nil))
2218 ;;(message (concat "Loading %s to get the source "
2219 ;; "value for language constant %s")
2223 (unless (setq assignment-entry (cdar file-entry))
2224 ;; The load didn't fill in the source for the
2225 ;; constant as expected. The situation is
2226 ;; probably that a derived mode was written for
2227 ;; and compiled with another version of CC Mode,
2228 ;; and the requested constant isn't in the
2229 ;; currently loaded one. Put in a dummy
2230 ;; assignment that matches no language.
2231 (setcdr (car file-entry)
2232 (setq assignment-entry (list (list nil))))))
2234 (aset source-pos 0 (setq file-entry (cdr file-entry)))
2237 (setq assignment (car assignment-entry))
2239 (setq assignment-entry (cdr assignment-entry)))
2241 (when (if (listp (car assignment))
2242 (memq mode (car assignment))
2244 (throw 'found (cdr assignment))))
2248 (defun c-lang-major-mode-is (mode)
2249 ;; `c-major-mode-is' expands to a call to this function inside
2250 ;; `c-lang-defconst'. Here we also match the mode(s) against any
2251 ;; fallback modes for the one in `c-buffer-is-cc-mode', so that
2252 ;; e.g. (c-major-mode-is 'c++-mode) is true in a derived language
2253 ;; that has c++-mode as base mode.
2254 (unless (listp mode)
2255 (setq mode (list mode)))
2256 (let (match (buf-mode c-buffer-is-cc-mode))
2257 (while (if (memq buf-mode mode)
2261 (setq buf-mode (get buf-mode 'c-fallback-mode))))
2265 (cc-provide 'cc-defs)
2267 ;;; cc-defs.el ends here