1 ;;; cc-defs.el --- compile time definitions for CC Mode
3 ;; Copyright (C) 1985, 1987, 1992-2014 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 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
63 (if (or (/= (regexp-opt-depth "\\(\\(\\)\\)") 2)
64 (not (fboundp 'push)))
67 ; (eval-after-load "font-lock" ; 2006-07-09. font-lock is now preloaded
69 (if (and (featurep 'xemacs) ; There is now (2005/12) code in GNU Emacs CVS
70 ; to make the call to f-l-c-k throw an error.
71 (not (featurep 'cc-fix)) ; only load the file once.
72 (let (font-lock-keywords)
73 (font-lock-compile-keywords '("\\<\\>"))
74 font-lock-keywords)) ; did the previous call foul this up?
77 ;; The above takes care of the delayed loading, but this is necessary
78 ;; to ensure correct byte compilation.
80 (if (and (featurep 'xemacs)
81 (not (featurep 'cc-fix))
84 (let (font-lock-keywords)
85 (font-lock-compile-keywords '("\\<\\>"))
89 ;; XEmacs 21.4 doesn't have `delete-dups'.
91 (if (and (not (fboundp 'delete-dups))
92 (not (featurep 'cc-fix)))
95 ;;; Variables also used at compile time.
97 (defconst c-version "5.32.5"
98 "CC Mode version number.")
100 (defconst c-version-sym (intern c-version))
101 ;; A little more compact and faster in comparisons.
103 (defvar c-buffer-is-cc-mode nil
104 "Non-nil for all buffers with a major mode derived from CC Mode.
105 Otherwise, this variable is nil. I.e. this variable is non-nil for
106 `c-mode', `c++-mode', `objc-mode', `java-mode', `idl-mode',
107 `pike-mode', `awk-mode', and any other non-CC Mode mode that calls
108 `c-initialize-cc-mode'. The value is the mode symbol itself
109 \(i.e. `c-mode' etc) of the original CC Mode mode, or just t if it's
111 (make-variable-buffer-local 'c-buffer-is-cc-mode)
113 ;; Have to make `c-buffer-is-cc-mode' permanently local so that it
114 ;; survives the initialization of the derived mode.
115 (put 'c-buffer-is-cc-mode 'permanent-local t)
118 ;; The following is used below during compilation.
120 (defvar c-inside-eval-when-compile nil)
122 (defmacro cc-eval-when-compile (&rest body)
123 "Like `progn', but evaluates the body at compile time.
124 The result of the body appears to the compiler as a quoted constant.
126 This variant works around bugs in `eval-when-compile' in various
127 \(X)Emacs versions. See cc-defs.el for details."
129 (if c-inside-eval-when-compile
130 ;; XEmacs 21.4.6 has a bug in `eval-when-compile' in that it
131 ;; evaluates its body at macro expansion time if it's nested
132 ;; inside another `eval-when-compile'. So we use a dynamically
133 ;; bound variable to avoid nesting them.
137 ;; In all (X)Emacsen so far, `eval-when-compile' byte compiles
138 ;; its contents before evaluating it. That can cause forms to
139 ;; be compiled in situations they aren't intended to be
142 ;; Example: It's not possible to defsubst a primitive, e.g. the
143 ;; following will produce an error (in any emacs flavor), since
144 ;; `nthcdr' is a primitive function that's handled specially by
145 ;; the byte compiler and thus can't be redefined:
147 ;; (defsubst nthcdr (val) val)
149 ;; `defsubst', like `defmacro', needs to be evaluated at
150 ;; compile time, so this will produce an error during byte
153 ;; CC Mode occasionally needs to do things like this for
154 ;; cross-emacs compatibility. It therefore uses the following
155 ;; to conditionally do a `defsubst':
157 ;; (eval-when-compile
158 ;; (if (not (fboundp 'foo))
159 ;; (defsubst foo ...)))
161 ;; But `eval-when-compile' byte compiles its contents and
162 ;; _then_ evaluates it (in all current emacs versions, up to
163 ;; and including Emacs 20.6 and XEmacs 21.1 as of this
164 ;; writing). So this will still produce an error, since the
165 ;; byte compiler will get to the defsubst anyway. That's
166 ;; arguably a bug because the point with `eval-when-compile' is
167 ;; that it should evaluate rather than compile its contents.
169 ;; We get around it by expanding the body to a quoted
170 ;; constant that we eval. That otoh introduce a problem in
171 ;; that a returned lambda expression doesn't get byte
172 ;; compiled (even if `function' is used).
173 (eval '(let ((c-inside-eval-when-compile t)) ,@body)))))
175 (put 'cc-eval-when-compile 'lisp-indent-hook 0))
178 (defalias 'c--macroexpand-all
179 (if (fboundp 'macroexpand-all)
180 'macroexpand-all 'cl-macroexpand-all)))
184 (defmacro c-point (position &optional point)
185 "Return the value of certain commonly referenced POSITIONs relative to POINT.
186 The current point is used if POINT isn't specified. POSITION can be
187 one of the following symbols:
189 `bol' -- beginning of line
191 `bod' -- beginning of defun
192 `eod' -- end of defun
193 `boi' -- beginning of indentation
194 `ionl' -- indentation of next line
195 `iopl' -- indentation of previous line
196 `bonl' -- beginning of next line
197 `eonl' -- end of next line
198 `bopl' -- beginning of previous line
199 `eopl' -- end of previous line
200 `bosws' -- beginning of syntactic whitespace
201 `eosws' -- end of syntactic whitespace
203 If the referenced position doesn't exist, the closest accessible point
204 to it is returned. This function does not modify the point or the mark."
206 (if (eq (car-safe position) 'quote)
207 (let ((position (eval position)))
211 (if (and (cc-bytecomp-fboundp 'line-beginning-position) (not point))
212 `(line-beginning-position)
214 ,@(if point `((goto-char ,point)))
219 (if (and (cc-bytecomp-fboundp 'line-end-position) (not point))
222 ,@(if point `((goto-char ,point)))
228 ,@(if point `((goto-char ,point)))
229 (back-to-indentation)
234 ,@(if point `((goto-char ,point)))
235 (c-beginning-of-defun-1)
240 ,@(if point `((goto-char ,point)))
245 (if (and (cc-bytecomp-fboundp 'line-beginning-position) (not point))
246 `(line-beginning-position 0)
248 ,@(if point `((goto-char ,point)))
253 (if (and (cc-bytecomp-fboundp 'line-beginning-position) (not point))
254 `(line-beginning-position 2)
256 ,@(if point `((goto-char ,point)))
261 (if (and (cc-bytecomp-fboundp 'line-end-position) (not point))
262 `(line-end-position 0)
264 ,@(if point `((goto-char ,point)))
266 (or (bobp) (backward-char))
270 (if (and (cc-bytecomp-fboundp 'line-end-position) (not point))
271 `(line-end-position 2)
273 ,@(if point `((goto-char ,point)))
280 ,@(if point `((goto-char ,point)))
282 (back-to-indentation)
287 ,@(if point `((goto-char ,point)))
289 (back-to-indentation)
292 ((eq position 'bosws)
294 ,@(if point `((goto-char ,point)))
295 (c-backward-syntactic-ws)
298 ((eq position 'eosws)
300 ,@(if point `((goto-char ,point)))
301 (c-forward-syntactic-ws)
304 (t (error "Unknown buffer position requested: %s" position))))
306 ;; The bulk of this should perhaps be in a function to avoid large
307 ;; expansions, but this case is not used anywhere in CC Mode (and
308 ;; probably not anywhere else either) so we only have it to be on
310 (message "Warning: c-point long expansion")
312 ,@(if point `((goto-char ,point)))
313 (let ((position ,position))
315 ((eq position 'bol) (beginning-of-line))
316 ((eq position 'eol) (end-of-line))
317 ((eq position 'boi) (back-to-indentation))
318 ((eq position 'bod) (c-beginning-of-defun-1))
319 ((eq position 'eod) (c-end-of-defun-1))
320 ((eq position 'bopl) (forward-line -1))
321 ((eq position 'bonl) (forward-line 1))
322 ((eq position 'eopl) (progn
324 (or (bobp) (backward-char))))
325 ((eq position 'eonl) (progn
328 ((eq position 'iopl) (progn
330 (back-to-indentation)))
331 ((eq position 'ionl) (progn
333 (back-to-indentation)))
334 ((eq position 'bosws) (c-backward-syntactic-ws))
335 ((eq position 'eosws) (c-forward-syntactic-ws))
336 (t (error "Unknown buffer position requested: %s" position))))
339 (defmacro c-region-is-active-p ()
340 ;; Return t when the region is active. The determination of region
341 ;; activeness is different in both Emacs and XEmacs.
342 ;; FIXME? Emacs has region-active-p since 23.1, so maybe this test
343 ;; should be updated.
344 (if (cc-bytecomp-boundp 'mark-active)
350 (defmacro c-set-region-active (activate)
351 ;; Activate the region if ACTIVE is non-nil, deactivate it
352 ;; otherwise. Covers the differences between Emacs and XEmacs.
353 (if (fboundp 'zmacs-activate-region)
356 (zmacs-activate-region)
357 (zmacs-deactivate-region))
359 `(setq mark-active ,activate)))
361 (defmacro c-delete-and-extract-region (start end)
362 "Delete the text between START and END and return it."
363 (if (cc-bytecomp-fboundp 'delete-and-extract-region)
364 ;; Emacs 21.1 and later
365 `(delete-and-extract-region ,start ,end)
366 ;; XEmacs and Emacs 20.x
368 (buffer-substring ,start ,end)
369 (delete-region ,start ,end))))
371 (defmacro c-safe (&rest body)
372 ;; safely execute BODY, return nil if an error occurred
376 (put 'c-safe 'lisp-indent-function 0)
378 (defmacro c-int-to-char (integer)
379 ;; In Emacs, a character is an integer. In XEmacs, a character is a
380 ;; type distinct from an integer. Sometimes we need to convert integers to
381 ;; characters. `c-int-to-char' makes this conversion, if necessary.
382 (if (fboundp 'int-to-char)
383 `(int-to-char ,integer)
386 (defmacro c-last-command-char ()
387 ;; The last character just typed. Note that `last-command-event' exists in
388 ;; both Emacs and XEmacs, but with confusingly different meanings.
389 (if (featurep 'xemacs)
391 'last-command-event))
393 (defmacro c-sentence-end ()
394 ;; Get the regular expression `sentence-end'.
395 (if (cc-bytecomp-fboundp 'sentence-end)
398 ;; Emacs <22 + XEmacs
401 (defmacro c-default-value-sentence-end ()
402 ;; Get the default value of the variable sentence end.
403 (if (cc-bytecomp-fboundp 'sentence-end)
405 `(let (sentence-end) (sentence-end))
406 ;; Emacs <22 + XEmacs
407 `(default-value 'sentence-end)))
409 ;; The following is essentially `save-buffer-state' from lazy-lock.el.
410 ;; It ought to be a standard macro.
411 (defmacro c-save-buffer-state (varlist &rest body)
412 "Bind variables according to VARLIST (in `let*' style) and eval BODY,
413 then restore the buffer state under the assumption that no significant
414 modification has been made in BODY. A change is considered
415 significant if it affects the buffer text in any way that isn't
416 completely restored again. Changes in text properties like `face' or
417 `syntax-table' are considered insignificant. This macro allows text
418 properties to be changed, even in a read-only buffer.
420 This macro should be placed around all calculations which set
421 \"insignificant\" text properties in a buffer, even when the buffer is
422 known to be writable. That way, these text properties remain set
423 even if the user undoes the command which set them.
425 This macro should ALWAYS be placed around \"temporary\" internal buffer
426 changes \(like adding a newline to calculate a text-property then
427 deleting it again\), so that the user never sees them on his
428 `buffer-undo-list'. See also `c-tentative-buffer-changes'.
430 However, any user-visible changes to the buffer \(like auto-newlines\)
431 must not be within a `c-save-buffer-state', since the user then
432 wouldn't be able to undo them.
434 The return value is the value of the last form in BODY."
435 `(let* ((modified (buffer-modified-p)) (buffer-undo-list t)
436 (inhibit-read-only t) (inhibit-point-motion-hooks t)
437 before-change-functions after-change-functions
439 buffer-file-name buffer-file-truename ; Prevent primitives checking
440 ; for file modification
446 (set-buffer-modified-p nil)))))
447 (put 'c-save-buffer-state 'lisp-indent-function 1)
449 (defmacro c-tentative-buffer-changes (&rest body)
450 "Eval BODY and optionally restore the buffer contents to the state it
451 was in before BODY. Any changes are kept if the last form in BODY
452 returns non-nil. Otherwise it's undone using the undo facility, and
453 various other buffer state that might be affected by the changes is
454 restored. That includes the current buffer, point, mark, mark
455 activation \(similar to `save-excursion'), and the modified state.
456 The state is also restored if BODY exits nonlocally.
458 If BODY makes a change that unconditionally is undone then wrap this
459 macro inside `c-save-buffer-state'. That way the change can be done
460 even when the buffer is read-only, and without interference from
461 various buffer change hooks."
462 `(let (-tnt-chng-keep
465 ;; Insert an undo boundary for use with `undo-more'. We
466 ;; don't use `undo-boundary' since it doesn't insert one
468 (setq buffer-undo-list (cons nil buffer-undo-list)
469 -tnt-chng-state (c-tnt-chng-record-state)
470 -tnt-chng-keep (progn ,@body))
471 (c-tnt-chng-cleanup -tnt-chng-keep -tnt-chng-state))))
472 (put 'c-tentative-buffer-changes 'lisp-indent-function 0)
474 (defun c-tnt-chng-record-state ()
475 ;; Used internally in `c-tentative-buffer-changes'.
476 (vector buffer-undo-list ; 0
478 ;; No need to use markers for the point and mark; if the
479 ;; undo got out of synch we're hosed anyway.
482 (c-region-is-active-p) ; 4
483 (buffer-modified-p))) ; 5
485 (defun c-tnt-chng-cleanup (keep saved-state)
486 ;; Used internally in `c-tentative-buffer-changes'.
488 (let ((saved-undo-list (elt saved-state 0)))
489 (if (eq buffer-undo-list saved-undo-list)
490 ;; No change was done after all.
491 (setq buffer-undo-list (cdr saved-undo-list))
494 ;; Find and remove the undo boundary.
495 (let ((p buffer-undo-list))
496 (while (not (eq (cdr p) saved-undo-list))
498 (setcdr p (cdr saved-undo-list)))
500 ;; `primitive-undo' will remove the boundary.
501 (setq saved-undo-list (cdr saved-undo-list))
502 (let ((undo-in-progress t))
503 (while (not (eq (setq buffer-undo-list
504 (primitive-undo 1 buffer-undo-list))
507 (when (buffer-live-p (elt saved-state 1))
508 (set-buffer (elt saved-state 1))
509 (goto-char (elt saved-state 2))
510 (set-mark (elt saved-state 3))
511 (c-set-region-active (elt saved-state 4))
512 (and (not (elt saved-state 5))
514 (set-buffer-modified-p nil)))))))
516 (defmacro c-forward-syntactic-ws (&optional limit)
517 "Forward skip over syntactic whitespace.
518 Syntactic whitespace is defined as whitespace characters, comments,
519 and preprocessor directives. However if point starts inside a comment
520 or preprocessor directive, the content of it is not treated as
523 LIMIT sets an upper limit of the forward movement, if specified. If
524 LIMIT or the end of the buffer is reached inside a comment or
525 preprocessor directive, the point will be left there.
527 Note that this function might do hidden buffer changes. See the
528 comment at the start of cc-engine.el for more info."
531 (narrow-to-region (point-min) (or ,limit (point-max)))
535 (defmacro c-backward-syntactic-ws (&optional limit)
536 "Backward skip over syntactic whitespace.
537 Syntactic whitespace is defined as whitespace characters, comments,
538 and preprocessor directives. However if point starts inside a comment
539 or preprocessor directive, the content of it is not treated as
542 LIMIT sets a lower limit of the backward movement, if specified. If
543 LIMIT is reached inside a line comment or preprocessor directive then
544 the point is moved into it past the whitespace at the end.
546 Note that this function might do hidden buffer changes. See the
547 comment at the start of cc-engine.el for more info."
550 (narrow-to-region (or ,limit (point-min)) (point-max))
554 (defmacro c-forward-sexp (&optional count)
555 "Move forward across COUNT balanced expressions.
556 A negative COUNT means move backward. Signal an error if the move
557 fails for any reason.
559 This is like `forward-sexp' except that it isn't interactive and does
560 not do any user friendly adjustments of the point and that it isn't
561 susceptible to user configurations such as disabling of signals in
563 (or count (setq count 1))
564 `(goto-char (scan-sexps (point) ,count)))
566 (defmacro c-backward-sexp (&optional count)
567 "See `c-forward-sexp' and reverse directions."
568 (or count (setq count 1))
569 `(c-forward-sexp ,(if (numberp count) (- count) `(- ,count))))
571 (defmacro c-safe-scan-lists (from count depth &optional limit)
572 "Like `scan-lists' but returns nil instead of signaling errors
573 for unbalanced parens.
575 A limit for the search may be given. FROM is assumed to be on the
577 (let ((res (if (featurep 'xemacs)
578 `(scan-lists ,from ,count ,depth nil t)
579 `(c-safe (scan-lists ,from ,count ,depth)))))
584 `(narrow-to-region ,limit (point-max))
585 `(narrow-to-region (point-min) ,limit))
587 (narrow-to-region ,limit (point-max))
588 (narrow-to-region (point-min) ,limit)))
593 ;; Wrappers for common scan-lists cases, mainly because it's almost
594 ;; impossible to get a feel for how that function works.
596 (defmacro c-go-list-forward ()
597 "Move backward across one balanced group of parentheses.
599 Return POINT when we succeed, NIL when we fail. In the latter case, leave
601 `(c-safe (let ((endpos (scan-lists (point) 1 0)))
605 (defmacro c-go-list-backward ()
606 "Move backward across one balanced group of parentheses.
608 Return POINT when we succeed, NIL when we fail. In the latter case, leave
610 `(c-safe (let ((endpos (scan-lists (point) -1 0)))
614 (defmacro c-up-list-forward (&optional pos limit)
615 "Return the first position after the list sexp containing POS,
616 or nil if no such position exists. The point is used if POS is left out.
618 A limit for the search may be given. The start position is assumed to
620 `(c-safe-scan-lists ,(or pos `(point)) 1 1 ,limit))
622 (defmacro c-up-list-backward (&optional pos limit)
623 "Return the position of the start of the list sexp containing POS,
624 or nil if no such position exists. The point is used if POS is left out.
626 A limit for the search may be given. The start position is assumed to
628 `(c-safe-scan-lists ,(or pos `(point)) -1 1 ,limit))
630 (defmacro c-down-list-forward (&optional pos limit)
631 "Return the first position inside the first list sexp after POS,
632 or nil if no such position exists. The point is used if POS is left out.
634 A limit for the search may be given. The start position is assumed to
636 `(c-safe-scan-lists ,(or pos `(point)) 1 -1 ,limit))
638 (defmacro c-down-list-backward (&optional pos limit)
639 "Return the last position inside the last list sexp before POS,
640 or nil if no such position exists. The point is used if POS is left out.
642 A limit for the search may be given. The start position is assumed to
644 `(c-safe-scan-lists ,(or pos `(point)) -1 -1 ,limit))
646 (defmacro c-go-up-list-forward (&optional pos limit)
647 "Move the point to the first position after the list sexp containing POS,
648 or containing the point if POS is left out. Return t if such a
649 position exists, otherwise nil is returned and the point isn't moved.
651 A limit for the search may be given. The start position is assumed to
653 (let ((res `(c-safe (goto-char (scan-lists ,(or pos `(point)) 1 1)) t)))
656 (narrow-to-region (point-min) ,limit)
660 (defmacro c-go-up-list-backward (&optional pos limit)
661 "Move the point to the position of the start of the list sexp containing POS,
662 or containing the point if POS is left out. Return t if such a
663 position exists, otherwise nil is returned and the point isn't moved.
665 A limit for the search may be given. The start position is assumed to
667 (let ((res `(c-safe (goto-char (scan-lists ,(or pos `(point)) -1 1)) t)))
670 (narrow-to-region ,limit (point-max))
674 (defmacro c-go-down-list-forward (&optional pos limit)
675 "Move the point to the first position inside the first list sexp after POS,
676 or before the point if POS is left out. Return t if such a position
677 exists, otherwise nil is returned and the point isn't moved.
679 A limit for the search may be given. The start position is assumed to
681 (let ((res `(c-safe (goto-char (scan-lists ,(or pos `(point)) 1 -1)) t)))
684 (narrow-to-region (point-min) ,limit)
688 (defmacro c-go-down-list-backward (&optional pos limit)
689 "Move the point to the last position inside the last list sexp before POS,
690 or before the point if POS is left out. Return t if such a position
691 exists, otherwise nil is returned and the point isn't moved.
693 A limit for the search may be given. The start position is assumed to
695 (let ((res `(c-safe (goto-char (scan-lists ,(or pos `(point)) -1 -1)) t)))
698 (narrow-to-region ,limit (point-max))
703 (defmacro c-beginning-of-defun-1 ()
704 ;; Wrapper around beginning-of-defun.
706 ;; NOTE: This function should contain the only explicit use of
707 ;; beginning-of-defun in CC Mode. Eventually something better than
708 ;; b-o-d will be available and this should be the only place the
709 ;; code needs to change. Everything else should use
710 ;; (c-beginning-of-defun-1)
712 ;; This is really a bit too large to be a macro but that isn't a
713 ;; problem as long as it only is used in one place in
717 (if (and ,(fboundp 'buffer-syntactic-context-depth)
718 c-enable-xemacs-performance-kludge-p)
719 ,(when (fboundp 'buffer-syntactic-context-depth)
720 ;; XEmacs only. This can improve the performance of
721 ;; c-parse-state to between 3 and 60 times faster when
722 ;; braces are hung. It can also degrade performance by
723 ;; about as much when braces are not hung.
724 '(let (beginning-of-defun-function end-of-defun-function
729 (setq pos (c-safe-scan-lists
730 (point) -1 (buffer-syntactic-context-depth))))
732 ((bobp) (setq pos (point-min)))
734 (let ((distance (skip-chars-backward "^{")))
735 ;; unbalanced parenthesis, while invalid C code,
736 ;; shouldn't cause an infloop! See unbal.c
737 (when (zerop distance)
740 (setq pos (point)))))
742 ((not (eq (char-after pos) ?{))
747 ;; Emacs, which doesn't have buffer-syntactic-context-depth
748 (let (beginning-of-defun-function end-of-defun-function)
749 (beginning-of-defun)))
750 ;; if defun-prompt-regexp is non-nil, b-o-d won't leave us at the
752 (and defun-prompt-regexp
753 (looking-at defun-prompt-regexp)
754 (goto-char (match-end 0)))))
757 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
758 ;; V i r t u a l S e m i c o l o n s
760 ;; In most CC Mode languages, statements are terminated explicitly by
761 ;; semicolons or closing braces. In some of the CC modes (currently AWK Mode
762 ;; and certain user-specified #define macros in C, C++, etc. (November 2008)),
763 ;; statements are (or can be) terminated by EOLs. Such a statement is said to
764 ;; be terminated by a "virtual semicolon" (VS). A statement terminated by an
765 ;; actual semicolon or brace is never considered to have a VS.
767 ;; The indentation engine (or whatever) tests for a VS at a specific position
768 ;; by invoking the macro `c-at-vsemi-p', which in its turn calls the mode
769 ;; specific function (if any) which is the value of the language variable
770 ;; `c-at-vsemi-p-fn'. This function should only use "low-level" features of
771 ;; CC Mode, i.e. features which won't trigger infinite recursion. ;-) The
772 ;; actual details of what constitutes a VS in a language are thus encapsulated
773 ;; in code specific to that language (e.g. cc-awk.el). `c-at-vsemi-p' returns
774 ;; non-nil if point (or the optional parameter POS) is at a VS, nil otherwise.
776 ;; The language specific function might well do extensive analysis of the
777 ;; source text, and may use a caching scheme to speed up repeated calls.
779 ;; The "virtual semicolon" lies just after the last non-ws token on the line.
780 ;; Like POINT, it is considered to lie between two characters. For example,
781 ;; at the place shown in the following AWK source line:
783 ;; kbyte = 1024 # 1000 if you're not picky
788 ;; In addition to `c-at-vsemi-p-fn', a mode may need to supply a function for
789 ;; `c-vsemi-status-unknown-p-fn'. The macro `c-vsemi-status-unknown-p' is a
790 ;; rather recondite kludge. It exists because the function
791 ;; `c-beginning-of-statement-1' sometimes tests for VSs as an optimization,
792 ;; but `c-at-vsemi-p' might well need to call `c-beginning-of-statement-1' in
793 ;; its calculations, thus potentially leading to infinite recursion.
795 ;; The macro `c-vsemi-status-unknown-p' resolves this problem; it may return
796 ;; non-nil at any time; returning nil is a guarantee that an immediate
797 ;; invocation of `c-at-vsemi-p' at point will NOT call
798 ;; `c-beginning-of-statement-1'. `c-vsemi-status-unknown-p' may not itself
799 ;; call `c-beginning-of-statement-1'.
801 ;; The macro `c-vsemi-status-unknown-p' will typically check the caching
802 ;; scheme used by the `c-at-vsemi-p-fn', hence the name - the status is
803 ;; "unknown" if there is no cache entry current for the line.
804 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
806 (defmacro c-at-vsemi-p (&optional pos)
807 ;; Is there a virtual semicolon (not a real one or a }) at POS (defaults to
808 ;; point)? Always returns nil for languages which don't have Virtual
810 ;; This macro might do hidden buffer changes.
812 (funcall c-at-vsemi-p-fn ,@(if pos `(,pos)))))
814 (defmacro c-vsemi-status-unknown-p ()
815 ;; Return NIL only if it can be guaranteed that an immediate
816 ;; (c-at-vsemi-p) will NOT call c-beginning-of-statement-1. Otherwise,
817 ;; return non-nil. (See comments above). The function invoked by this
818 ;; macro MUST NOT UNDER ANY CIRCUMSTANCES itself call
819 ;; c-beginning-of-statement-1.
820 ;; Languages which don't have EOL terminated statements always return NIL
821 ;; (they _know_ there's no vsemi ;-).
822 `(if c-vsemi-status-unknown-p-fn (funcall c-vsemi-status-unknown-p-fn)))
825 (defmacro c-benign-error (format &rest args)
826 ;; Formats an error message for the echo area and dings, i.e. like
827 ;; `error' but doesn't abort.
829 (message ,format ,@args)
832 (defmacro c-with-syntax-table (table &rest code)
833 ;; Temporarily switches to the specified syntax table in a failsafe
834 ;; way to execute code.
835 ;; Maintainers' note: If TABLE is `c++-template-syntax-table', DON'T call
836 ;; any forms inside this that call `c-parse-state'. !!!!
837 `(let ((c-with-syntax-table-orig-table (syntax-table)))
840 (set-syntax-table ,table)
842 (set-syntax-table c-with-syntax-table-orig-table))))
843 (put 'c-with-syntax-table 'lisp-indent-function 1)
845 (defmacro c-skip-ws-forward (&optional limit)
846 "Skip over any whitespace following point.
847 This function skips over horizontal and vertical whitespace and line
850 `(let ((limit (or ,limit (point-max))))
852 ;; skip-syntax-* doesn't count \n as whitespace..
853 (skip-chars-forward " \t\n\r\f\v" limit)
854 (when (and (eq (char-after) ?\\)
858 (progn (backward-char) nil))))))
860 (skip-chars-forward " \t\n\r\f\v")
861 (when (eq (char-after) ?\\)
864 (progn (backward-char) nil)))))))
866 (defmacro c-skip-ws-backward (&optional limit)
867 "Skip over any whitespace preceding point.
868 This function skips over horizontal and vertical whitespace and line
871 `(let ((limit (or ,limit (point-min))))
873 ;; skip-syntax-* doesn't count \n as whitespace..
874 (skip-chars-backward " \t\n\r\f\v" limit)
876 (eq (char-before) ?\\)
880 (skip-chars-backward " \t\n\r\f\v")
882 (eq (char-before) ?\\)))
886 (defvar c-langs-are-parametric nil))
888 (defmacro c-major-mode-is (mode)
889 "Return non-nil if the current CC Mode major mode is MODE.
890 MODE is either a mode symbol or a list of mode symbols."
892 (if c-langs-are-parametric
893 ;; Inside a `c-lang-defconst'.
894 `(c-lang-major-mode-is ,mode)
896 (if (eq (car-safe mode) 'quote)
897 (let ((mode (eval mode)))
899 `(memq c-buffer-is-cc-mode ',mode)
900 `(eq c-buffer-is-cc-mode ',mode)))
904 (memq c-buffer-is-cc-mode mode)
905 (eq c-buffer-is-cc-mode mode))))))
908 ;; Macros/functions to handle so-called "char properties", which are
909 ;; properties set on a single character and that never spread to any
913 ;; Constant used at compile time to decide whether or not to use
914 ;; XEmacs extents. Check all the extent functions we'll use since
915 ;; some packages might add compatibility aliases for some of them in
917 (defconst c-use-extents (and (cc-bytecomp-fboundp 'extent-at)
918 (cc-bytecomp-fboundp 'set-extent-property)
919 (cc-bytecomp-fboundp 'set-extent-properties)
920 (cc-bytecomp-fboundp 'make-extent)
921 (cc-bytecomp-fboundp 'extent-property)
922 (cc-bytecomp-fboundp 'delete-extent)
923 (cc-bytecomp-fboundp 'map-extents))))
925 (defconst c-<-as-paren-syntax '(4 . ?>))
926 (put 'c-<-as-paren-syntax 'syntax-table c-<-as-paren-syntax)
928 (defconst c->-as-paren-syntax '(5 . ?<))
929 (put 'c->-as-paren-syntax 'syntax-table c->-as-paren-syntax)
931 ;; `c-put-char-property' is complex enough in XEmacs and Emacs < 21 to
932 ;; make it a function.
933 (defalias 'c-put-char-property-fun
934 (cc-eval-when-compile
938 (lambda (pos property value)
939 (let ((ext (extent-at pos nil property)))
941 (set-extent-property ext property value)
942 (set-extent-properties (make-extent pos (1+ pos))
948 ((not (cc-bytecomp-boundp 'text-property-default-nonsticky))
949 ;; In Emacs < 21 we have to mess with the `rear-nonsticky' property.
951 (lambda (pos property value)
952 (put-text-property pos (1+ pos) property value)
953 (let ((prop (get-text-property pos 'rear-nonsticky)))
954 (or (memq property prop)
955 (put-text-property pos (1+ pos)
957 (cons property prop)))))))
958 ;; This won't be used for anything.
960 (cc-bytecomp-defun c-put-char-property-fun) ; Make it known below.
962 (defmacro c-put-char-property (pos property value)
963 ;; Put the given property with the given value on the character at
964 ;; POS and make it front and rear nonsticky, or start and end open
965 ;; in XEmacs vocabulary. If the character already has the given
966 ;; property then the value is replaced, and the behavior is
967 ;; undefined if that property has been put by some other function.
968 ;; PROPERTY is assumed to be constant.
970 ;; If there's a `text-property-default-nonsticky' variable (Emacs
971 ;; 21) then it's assumed that the property is present on it.
973 ;; This macro does a hidden buffer change.
974 (setq property (eval property))
975 (if (or c-use-extents
976 (not (cc-bytecomp-boundp 'text-property-default-nonsticky)))
977 ;; XEmacs and Emacs < 21.
978 `(c-put-char-property-fun ,pos ',property ,value)
979 ;; In Emacs 21 we got the `rear-nonsticky' property covered
980 ;; by `text-property-default-nonsticky'.
982 (put-text-property -pos- (1+ -pos-) ',property ,value))))
984 (defmacro c-get-char-property (pos property)
985 ;; Get the value of the given property on the character at POS if
986 ;; it's been put there by `c-put-char-property'. PROPERTY is
987 ;; assumed to be constant.
988 (setq property (eval property))
991 `(let ((ext (extent-at ,pos nil ',property)))
992 (if ext (extent-property ext ',property)))
994 `(get-text-property ,pos ',property)))
996 ;; `c-clear-char-property' is complex enough in Emacs < 21 to make it
997 ;; a function, since we have to mess with the `rear-nonsticky' property.
998 (defalias 'c-clear-char-property-fun
999 (cc-eval-when-compile
1000 (unless (or c-use-extents
1001 (cc-bytecomp-boundp 'text-property-default-nonsticky))
1003 (lambda (pos property)
1004 (when (get-text-property pos property)
1005 (remove-text-properties pos (1+ pos) (list property nil))
1006 (put-text-property pos (1+ pos)
1008 (delq property (get-text-property
1009 pos 'rear-nonsticky)))))))))
1010 (cc-bytecomp-defun c-clear-char-property-fun) ; Make it known below.
1012 (defmacro c-clear-char-property (pos property)
1013 ;; Remove the given property on the character at POS if it's been put
1014 ;; there by `c-put-char-property'. PROPERTY is assumed to be
1017 ;; This macro does a hidden buffer change.
1018 (setq property (eval property))
1019 (cond (c-use-extents
1021 `(let ((ext (extent-at ,pos nil ',property)))
1022 (if ext (delete-extent ext))))
1023 ((cc-bytecomp-boundp 'text-property-default-nonsticky)
1024 ;; In Emacs 21 we got the `rear-nonsticky' property covered
1025 ;; by `text-property-default-nonsticky'.
1027 (remove-text-properties pos (1+ pos)
1031 `(c-clear-char-property-fun ,pos ',property))))
1033 (defmacro c-clear-char-properties (from to property)
1034 ;; Remove all the occurrences of the given property in the given
1035 ;; region that has been put with `c-put-char-property'. PROPERTY is
1036 ;; assumed to be constant.
1038 ;; Note that this function does not clean up the property from the
1039 ;; lists of the `rear-nonsticky' properties in the region, if such
1040 ;; are used. Thus it should not be used for common properties like
1043 ;; This macro does hidden buffer changes.
1044 (setq property (eval property))
1047 `(map-extents (lambda (ext ignored)
1048 (delete-extent ext))
1049 nil ,from ,to nil nil ',property)
1051 `(remove-text-properties ,from ,to '(,property nil))))
1053 (defmacro c-search-forward-char-property (property value &optional limit)
1054 "Search forward for a text-property PROPERTY having value VALUE.
1055 LIMIT bounds the search. The comparison is done with `equal'.
1057 Leave point just after the character, and set the match data on
1058 this character, and return point. If VALUE isn't found, Return
1059 nil; point is then left undefined."
1060 `(let ((place (point)))
1063 (< place ,(or limit '(point-max)))
1064 (not (equal (get-text-property place ,property) ,value)))
1065 (setq place (next-single-property-change
1066 place ,property nil ,(or limit '(point-max)))))
1067 (when (< place ,(or limit '(point-max)))
1069 (search-forward-regexp ".") ; to set the match-data.
1072 (defmacro c-search-backward-char-property (property value &optional limit)
1073 "Search backward for a text-property PROPERTY having value VALUE.
1074 LIMIT bounds the search. The comparison is done with `equal'.
1076 Leave point just before the character, set the match data on this
1077 character, and return point. If VALUE isn't found, Return nil;
1078 point is then left undefined."
1079 `(let ((place (point)))
1082 (> place ,(or limit '(point-min)))
1083 (not (equal (get-text-property (1- place) ,property) ,value)))
1084 (setq place (previous-single-property-change
1085 place ,property nil ,(or limit '(point-min)))))
1086 (when (> place ,(or limit '(point-max)))
1088 (search-backward-regexp ".") ; to set the match-data.
1091 (defun c-clear-char-property-with-value-function (from to property value)
1092 "Remove all text-properties PROPERTY from the region (FROM, TO)
1093 which have the value VALUE, as tested by `equal'. These
1094 properties are assumed to be over individual characters, having
1095 been put there by c-put-char-property. POINT remains unchanged."
1096 (let ((place from) end-place)
1097 (while ; loop round occurrences of (PROPERTY VALUE)
1099 (while ; loop round changes in PROPERTY till we find VALUE
1102 (not (equal (get-text-property place property) value)))
1103 (setq place (next-single-property-change place property nil to)))
1105 (setq end-place (next-single-property-change place property nil to))
1106 (remove-text-properties place end-place (cons property nil))
1107 ;; Do we have to do anything with stickiness here?
1108 (setq place end-place))))
1110 (defmacro c-clear-char-property-with-value (from to property value)
1111 "Remove all text-properties PROPERTY from the region [FROM, TO)
1112 which have the value VALUE, as tested by `equal'. These
1113 properties are assumed to be over individual characters, having
1114 been put there by c-put-char-property. POINT remains unchanged."
1117 `(let ((-property- ,property))
1118 (map-extents (lambda (ext val)
1119 (if (equal (extent-property ext -property-) val)
1120 (delete-extent ext)))
1121 nil ,from ,to ,value nil -property-))
1123 `(c-clear-char-property-with-value-function ,from ,to ,property ,value)))
1125 ;; Macros to put overlays (Emacs) or extents (XEmacs) on buffer text.
1126 ;; For our purposes, these are characterized by being possible to
1127 ;; remove again without affecting the other text properties in the
1128 ;; buffer that got overridden when they were put.
1130 (defmacro c-put-overlay (from to property value)
1131 ;; Put an overlay/extent covering the given range in the current
1132 ;; buffer. It's currently undefined whether it's front/end sticky
1133 ;; or not. The overlay/extent object is returned.
1134 (if (cc-bytecomp-fboundp 'make-overlay)
1136 `(let ((ol (make-overlay ,from ,to)))
1137 (overlay-put ol ,property ,value)
1140 `(let ((ext (make-extent ,from ,to)))
1141 (set-extent-property ext ,property ,value)
1144 (defmacro c-delete-overlay (overlay)
1145 ;; Deletes an overlay/extent object previously retrieved using
1147 (if (cc-bytecomp-fboundp 'make-overlay)
1149 `(delete-overlay ,overlay)
1151 `(delete-extent ,overlay)))
1154 ;; Make edebug understand the macros.
1155 ;(eval-after-load "edebug" ; 2006-07-09: def-edebug-spec is now in subr.el.
1157 (def-edebug-spec cc-eval-when-compile (&rest def-form))
1158 (def-edebug-spec c-point t)
1159 (def-edebug-spec c-set-region-active t)
1160 (def-edebug-spec c-safe t)
1161 (def-edebug-spec c-save-buffer-state let*)
1162 (def-edebug-spec c-tentative-buffer-changes t)
1163 (def-edebug-spec c-forward-syntactic-ws t)
1164 (def-edebug-spec c-backward-syntactic-ws t)
1165 (def-edebug-spec c-forward-sexp t)
1166 (def-edebug-spec c-backward-sexp t)
1167 (def-edebug-spec c-up-list-forward t)
1168 (def-edebug-spec c-up-list-backward t)
1169 (def-edebug-spec c-down-list-forward t)
1170 (def-edebug-spec c-down-list-backward t)
1171 (def-edebug-spec c-add-syntax t)
1172 (def-edebug-spec c-add-class-syntax t)
1173 (def-edebug-spec c-benign-error t)
1174 (def-edebug-spec c-with-syntax-table t)
1175 (def-edebug-spec c-skip-ws-forward t)
1176 (def-edebug-spec c-skip-ws-backward t)
1177 (def-edebug-spec c-major-mode-is t)
1178 (def-edebug-spec c-put-char-property t)
1179 (def-edebug-spec c-get-char-property t)
1180 (def-edebug-spec c-clear-char-property t)
1181 (def-edebug-spec c-clear-char-properties t)
1182 (def-edebug-spec c-put-overlay t)
1183 (def-edebug-spec c-delete-overlay t) ;))
1188 ;; Note: All these after the macros, to be on safe side in avoiding
1189 ;; bugs where macros are defined too late. These bugs often only show
1190 ;; when the files are compiled in a certain order within the same
1193 (defsubst c-end-of-defun-1 ()
1194 ;; Replacement for end-of-defun that use c-beginning-of-defun-1.
1195 (let ((start (point)))
1196 ;; Skip forward into the next defun block. Don't bother to avoid
1197 ;; comments, literals etc, since beginning-of-defun doesn't do that
1199 (skip-chars-forward "^}")
1200 (c-beginning-of-defun-1)
1201 (if (eq (char-after) ?{)
1203 (if (< (point) start)
1204 (goto-char (point-max)))))
1206 (defsubst c-mark-<-as-paren (pos)
1207 ;; Mark the "<" character at POS as a template opener using the
1208 ;; `syntax-table' property via the `category' property.
1210 ;; This function does a hidden buffer change. Note that we use
1211 ;; indirection through the `category' text property. This allows us to
1212 ;; toggle the property in all template brackets simultaneously and
1213 ;; cheaply. We use this, for instance, in `c-parse-state'.
1214 (c-put-char-property pos 'category 'c-<-as-paren-syntax))
1216 (defsubst c-mark->-as-paren (pos)
1217 ;; Mark the ">" character at POS as an sexp list closer using the
1218 ;; syntax-table property.
1220 ;; This function does a hidden buffer change. Note that we use
1221 ;; indirection through the `category' text property. This allows us to
1222 ;; toggle the property in all template brackets simultaneously and
1223 ;; cheaply. We use this, for instance, in `c-parse-state'.
1224 (c-put-char-property pos 'category 'c->-as-paren-syntax))
1226 (defsubst c-unmark-<->-as-paren (pos)
1227 ;; Unmark the "<" or "<" character at POS as an sexp list opener using
1228 ;; the syntax-table property indirectly through the `category' text
1231 ;; This function does a hidden buffer change. Note that we use
1232 ;; indirection through the `category' text property. This allows us to
1233 ;; toggle the property in all template brackets simultaneously and
1234 ;; cheaply. We use this, for instance, in `c-parse-state'.
1235 (c-clear-char-property pos 'category))
1237 (defsubst c-suppress-<->-as-parens ()
1238 ;; Suppress the syntactic effect of all marked < and > as parens. Note
1239 ;; that this effect is NOT buffer local. You should probably not use
1240 ;; this directly, but only through the macro
1241 ;; `c-with-<->-as-parens-suppressed'
1242 (put 'c-<-as-paren-syntax 'syntax-table nil)
1243 (put 'c->-as-paren-syntax 'syntax-table nil))
1245 (defsubst c-restore-<->-as-parens ()
1246 ;; Restore the syntactic effect of all marked <s and >s as parens. This
1247 ;; has no effect on unmarked <s and >s
1248 (put 'c-<-as-paren-syntax 'syntax-table c-<-as-paren-syntax)
1249 (put 'c->-as-paren-syntax 'syntax-table c->-as-paren-syntax))
1251 (defmacro c-with-<->-as-parens-suppressed (&rest forms)
1252 ;; Like progn, except that the paren property is suppressed on all
1253 ;; template brackets whilst they are running. This macro does a hidden
1257 (c-suppress-<->-as-parens)
1259 (c-restore-<->-as-parens)))
1263 (defconst c-cpp-delimiter '(14)) ; generic comment syntax
1264 ;; This is the value of the `category' text property placed on every #
1265 ;; which introduces a CPP construct and every EOL (or EOB, or character
1266 ;; preceding //, etc.) which terminates it. We can instantly "comment
1267 ;; out" all CPP constructs by giving `c-cpp-delimiter' a syntax-table
1268 ;; property '(14) (generic comment delimiter).
1269 (defmacro c-set-cpp-delimiters (beg end)
1270 ;; This macro does a hidden buffer change.
1272 (c-put-char-property ,beg 'category 'c-cpp-delimiter)
1273 (if (< ,end (point-max))
1274 (c-put-char-property ,end 'category 'c-cpp-delimiter))))
1275 (defmacro c-clear-cpp-delimiters (beg end)
1276 ;; This macro does a hidden buffer change.
1278 (c-clear-char-property ,beg 'category)
1279 (if (< ,end (point-max))
1280 (c-clear-char-property ,end 'category))))
1282 (defsubst c-comment-out-cpps ()
1283 ;; Render all preprocessor constructs syntactically commented out.
1284 (put 'c-cpp-delimiter 'syntax-table c-cpp-delimiter))
1285 (defsubst c-uncomment-out-cpps ()
1286 ;; Restore the syntactic visibility of preprocessor constructs.
1287 (put 'c-cpp-delimiter 'syntax-table nil))
1289 (defmacro c-with-cpps-commented-out (&rest forms)
1290 ;; Execute FORMS... whilst the syntactic effect of all characters in
1291 ;; all CPP regions is suppressed. In particular, this is to suppress
1292 ;; the syntactic significance of parens/braces/brackets to functions
1293 ;; such as `scan-lists' and `parse-partial-sexp'.
1295 (c-save-buffer-state ()
1296 (c-comment-out-cpps)
1298 (c-save-buffer-state ()
1299 (c-uncomment-out-cpps))))
1301 (defmacro c-with-all-but-one-cpps-commented-out (beg end &rest forms)
1302 ;; Execute FORMS... whilst the syntactic effect of all characters in
1303 ;; every CPP region APART FROM THE ONE BETWEEN BEG and END is
1306 (c-save-buffer-state ()
1309 (c-clear-cpp-delimiters ,beg ,end))
1310 ,`(c-with-cpps-commented-out ,@forms))
1311 (c-save-buffer-state ()
1314 (c-set-cpp-delimiters ,beg ,end)))))
1316 (defsubst c-intersect-lists (list alist)
1317 ;; return the element of ALIST that matches the first element found
1318 ;; in LIST. Uses assq.
1321 (not (setq match (assq (car list) alist))))
1322 (setq list (cdr list)))
1325 (defsubst c-lookup-lists (list alist1 alist2)
1326 ;; first, find the first entry from LIST that is present in ALIST1,
1327 ;; then find the entry in ALIST2 for that entry.
1328 (assq (car (c-intersect-lists list alist1)) alist2))
1330 (defsubst c-langelem-sym (langelem)
1331 "Return the syntactic symbol in LANGELEM.
1333 LANGELEM is either a cons cell on the \"old\" form given as the first
1334 argument to lineup functions or a syntactic element on the \"new\"
1335 form as used in `c-syntactic-element'."
1338 (defsubst c-langelem-pos (langelem)
1339 "Return the anchor position in LANGELEM, or nil if there is none.
1341 LANGELEM is either a cons cell on the \"old\" form given as the first
1342 argument to lineup functions or a syntactic element on the \"new\"
1343 form as used in `c-syntactic-element'."
1344 (if (consp (cdr langelem))
1345 (car-safe (cdr langelem))
1348 (defun c-langelem-col (langelem &optional preserve-point)
1349 "Return the column of the anchor position in LANGELEM.
1350 Also move the point to that position unless PRESERVE-POINT is non-nil.
1352 LANGELEM is either a cons cell on the \"old\" form given as the first
1353 argument to lineup functions or a syntactic element on the \"new\"
1354 form as used in `c-syntactic-element'."
1355 (let ((pos (c-langelem-pos langelem))
1360 (prog1 (current-column)
1365 (defsubst c-langelem-2nd-pos (langelem)
1366 "Return the secondary position in LANGELEM, or nil if there is none.
1368 LANGELEM is typically a syntactic element on the \"new\" form as used
1369 in `c-syntactic-element'. It may also be a cons cell as passed in the
1370 first argument to lineup functions, but then the returned value always
1372 (car-safe (cdr-safe (cdr-safe langelem))))
1374 (defsubst c-keep-region-active ()
1375 ;; Do whatever is necessary to keep the region active in XEmacs.
1376 ;; This is not needed for Emacs.
1377 (and (boundp 'zmacs-region-stays)
1378 (setq zmacs-region-stays t)))
1380 (put 'c-mode 'c-mode-prefix "c-")
1381 (put 'c++-mode 'c-mode-prefix "c++-")
1382 (put 'objc-mode 'c-mode-prefix "objc-")
1383 (put 'java-mode 'c-mode-prefix "java-")
1384 (put 'idl-mode 'c-mode-prefix "idl-")
1385 (put 'pike-mode 'c-mode-prefix "pike-")
1386 (put 'awk-mode 'c-mode-prefix "awk-")
1388 (defsubst c-mode-symbol (suffix)
1389 "Prefix the current mode prefix (e.g. \"c-\") to SUFFIX and return
1390 the corresponding symbol."
1391 (or c-buffer-is-cc-mode
1392 (error "Not inside a CC Mode based mode"))
1393 (let ((mode-prefix (get c-buffer-is-cc-mode 'c-mode-prefix)))
1395 (error "%S has no mode prefix known to `c-mode-symbol'"
1396 c-buffer-is-cc-mode))
1397 (intern (concat mode-prefix suffix))))
1399 (defsubst c-mode-var (suffix)
1400 "Prefix the current mode prefix (e.g. \"c-\") to SUFFIX and return
1401 the value of the variable with that name."
1402 (symbol-value (c-mode-symbol suffix)))
1404 (defsubst c-got-face-at (pos faces)
1405 "Return non-nil if position POS in the current buffer has any of the
1406 faces in the list FACES."
1407 (let ((pos-faces (get-text-property pos 'face)))
1408 (if (consp pos-faces)
1410 (while (and pos-faces
1411 (not (memq (car pos-faces) faces)))
1412 (setq pos-faces (cdr pos-faces)))
1414 (memq pos-faces faces))))
1416 (defsubst c-face-name-p (facename)
1417 ;; Return t if FACENAME is the name of a face. This method is
1418 ;; necessary since facep in XEmacs only returns t for the actual
1419 ;; face objects (while it's only their names that are used just
1420 ;; about anywhere else) without providing a predicate that tests
1422 (memq facename (face-list)))
1424 (defun c-concat-separated (list separator)
1425 "Like `concat' on LIST, but separate each element with SEPARATOR.
1426 Notably, null elements in LIST are ignored."
1427 (mapconcat 'identity (delete nil (append list nil)) separator))
1429 (defun c-make-keywords-re (adorn list &optional mode)
1430 "Make a regexp that matches all the strings the list.
1431 Duplicates and nil elements in the list are removed. The
1432 resulting regexp may contain zero or more submatch expressions.
1434 If ADORN is t there will be at least one submatch and the first
1435 surrounds the matched alternative, and the regexp will also not match
1436 a prefix of any identifier. Adorned regexps cannot be appended. The
1437 language variable `c-nonsymbol-key' is used to make the adornment.
1439 A value 'appendable for ADORN is like above, but all alternatives in
1440 the list that end with a word constituent char will have \\> appended
1441 instead, so that the regexp remains appendable. Note that this
1442 variant doesn't always guarantee that an identifier prefix isn't
1443 matched since the symbol constituent '_' is normally considered a
1444 nonword token by \\>.
1446 The optional MODE specifies the language to get `c-nonsymbol-key' from
1447 when it's needed. The default is the current language taken from
1448 `c-buffer-is-cc-mode'."
1450 (setq list (delete nil (delete-dups list)))
1454 (if (eq adorn 'appendable)
1455 ;; This is kludgy but it works: Search for a string that
1456 ;; doesn't occur in any word in LIST. Append it to all
1457 ;; the alternatives where we want to add \>. Run through
1458 ;; `regexp-opt' and then replace it with \>.
1459 (let ((unique "") pos)
1461 (setq unique (concat unique "@")
1464 (if (string-match unique (car pos))
1465 (progn (setq found t)
1468 (setq pos (cdr pos)))
1472 (if (string-match "\\w\\'" (car pos))
1473 (setcar pos (concat (car pos) unique)))
1474 (setq pos (cdr pos)))
1475 (setq re (regexp-opt list))
1477 (while (string-match unique re pos)
1478 (setq pos (+ (match-beginning 0) 2)
1479 re (replace-match "\\>" t t re))))
1481 (setq re (regexp-opt list)))
1483 ;; Emacs 20 and XEmacs (all versions so far) has a buggy
1484 ;; regexp-opt that doesn't always cope with strings containing
1485 ;; newlines. This kludge doesn't handle shy parens correctly
1486 ;; so we can't advice regexp-opt directly with it.
1489 (and (string-match "\n" (car list)) ; To speed it up a little.
1490 (not (string-match (concat "\\`\\(" re "\\)\\'")
1492 (setq fail-list (cons (car list) fail-list)))
1493 (setq list (cdr list)))
1498 (if (eq adorn 'appendable)
1500 (if (string-match "\\w\\'" str)
1501 (concat (regexp-quote str)
1503 (regexp-quote str)))
1507 (> (length a) (length b))))
1510 ;; Add our own grouping parenthesis around re instead of
1511 ;; passing adorn to `regexp-opt', since in XEmacs it makes the
1512 ;; top level grouping "shy".
1513 (cond ((eq adorn 'appendable)
1514 (concat "\\(" re "\\)"))
1516 (concat "\\(" re "\\)"
1518 (c-get-lang-constant 'c-nonsymbol-key nil mode)
1523 ;; Produce a regexp that matches nothing.
1528 (put 'c-make-keywords-re 'lisp-indent-function 1)
1530 (defun c-make-bare-char-alt (chars &optional inverted)
1531 "Make a character alternative string from the list of characters CHARS.
1532 The returned string is of the type that can be used with
1533 `skip-chars-forward' and `skip-chars-backward'. If INVERTED is
1534 non-nil, a caret is prepended to invert the set."
1535 ;; This function ought to be in the elisp core somewhere.
1536 (let ((str (if inverted "^" "")) char char2)
1537 (setq chars (sort (append chars nil) `<))
1539 (setq char (pop chars))
1540 (if (memq char '(?\\ ?^ ?-))
1541 ;; Quoting necessary (this method only works in the skip
1543 (setq str (format "%s\\%c" str char))
1544 (setq str (format "%s%c" str char)))
1547 (while (and chars (>= (1+ char2) (car chars)))
1548 (setq char2 (pop chars)))
1549 (unless (= char char2)
1550 (if (< (1+ char) char2)
1551 (setq str (format "%s-%c" str char2))
1552 (push char2 chars))))
1555 ;; Leftovers from (X)Emacs 19 compatibility.
1556 (defalias 'c-regexp-opt 'regexp-opt)
1557 (defalias 'c-regexp-opt-depth 'regexp-opt-depth)
1560 ;; Figure out what features this Emacs has
1562 (cc-bytecomp-defvar open-paren-in-column-0-is-defun-start)
1564 (defvar lookup-syntax-properties) ;XEmacs.
1566 (defconst c-emacs-features
1569 (if (boundp 'infodock-version)
1570 ;; I've no idea what this actually is, but it's legacy. /mast
1571 (setq list (cons 'infodock list)))
1573 ;; XEmacs uses 8-bit modify-syntax-entry flags.
1574 ;; Emacs uses a 1-bit flag. We will have to set up our
1575 ;; syntax tables differently to handle this.
1576 (let ((table (copy-syntax-table))
1578 (modify-syntax-entry ?a ". 12345678" table)
1582 (setq entry (aref table ?a))
1583 ;; In Emacs, table entries are cons cells
1584 (if (consp entry) (setq entry (car entry))))
1586 ((fboundp 'get-char-table)
1587 (setq entry (get-char-table ?a table)))
1589 (t (error "CC Mode is incompatible with this version of Emacs")))
1590 (setq list (cons (if (= (logand (lsh entry -16) 255) 255)
1595 ;; Check whether beginning/end-of-defun call
1596 ;; beginning/end-of-defun-function nicely, passing through the
1597 ;; argument and respecting the return code.
1599 (bod-param 'foo) (eod-param 'foo)
1600 (beginning-of-defun-function
1601 (lambda (&optional arg)
1602 (or (eq bod-param 'foo) (setq bod-param 'bar))
1603 (and (eq bod-param 'foo)
1604 (setq bod-param arg)
1606 (end-of-defun-function
1607 (lambda (&optional arg)
1608 (and (eq eod-param 'foo)
1609 (setq eod-param arg)
1611 (if (save-excursion (and (beginning-of-defun 3) (eq bod-param 3)
1612 (not (beginning-of-defun))
1613 (end-of-defun 3) (eq eod-param 3)
1614 (not (end-of-defun))))
1615 (setq list (cons 'argumentative-bod-function list))))
1617 (let ((buf (generate-new-buffer " test"))
1618 parse-sexp-lookup-properties
1619 parse-sexp-ignore-comments
1620 lookup-syntax-properties) ; XEmacs
1621 (with-current-buffer buf
1622 (set-syntax-table (make-syntax-table))
1624 ;; For some reason we have to set some of these after the
1625 ;; buffer has been made current. (Specifically,
1626 ;; `parse-sexp-ignore-comments' in Emacs 21.)
1627 (setq parse-sexp-lookup-properties t
1628 parse-sexp-ignore-comments t
1629 lookup-syntax-properties t)
1631 ;; Find out if the `syntax-table' text property works.
1632 (modify-syntax-entry ?< ".")
1633 (modify-syntax-entry ?> ".")
1635 (c-mark-<-as-paren (point-min))
1636 (c-mark->-as-paren (+ 3 (point-min)))
1637 (goto-char (point-min))
1639 (if (= (point) (+ 4 (point-min)))
1640 (setq list (cons 'syntax-properties list))
1642 "CC Mode is incompatible with this version of Emacs - "
1643 "support for the `syntax-table' text property "
1646 ;; Find out if "\\s!" (generic comment delimiters) work.
1648 (modify-syntax-entry ?x "!")
1649 (if (string-match "\\s!" "x")
1650 (setq list (cons 'gen-comment-delim list))))
1652 ;; Find out if "\\s|" (generic string delimiters) work.
1654 (modify-syntax-entry ?x "|")
1655 (if (string-match "\\s|" "x")
1656 (setq list (cons 'gen-string-delim list))))
1658 ;; See if POSIX char classes work.
1659 (when (and (string-match "[[:alpha:]]" "a")
1660 ;; All versions of Emacs 21 so far haven't fixed
1661 ;; char classes in `skip-chars-forward' and
1662 ;; `skip-chars-backward'.
1664 (delete-region (point-min) (point-max))
1666 (skip-chars-backward "[:alnum:]")
1668 (= (skip-chars-forward "[:alpha:]") 3))
1669 (setq list (cons 'posix-char-classes list)))
1671 ;; See if `open-paren-in-column-0-is-defun-start' exists and
1672 ;; isn't buggy (Emacs >= 21.4).
1673 (when (boundp 'open-paren-in-column-0-is-defun-start)
1674 (let ((open-paren-in-column-0-is-defun-start nil)
1675 (parse-sexp-ignore-comments t))
1676 (delete-region (point-min) (point-max))
1677 (set-syntax-table (make-syntax-table))
1678 (modify-syntax-entry ?\' "\"")
1680 ;; XEmacs. Afaik this is currently an Emacs-only
1681 ;; feature, but it's good to be prepared.
1683 (modify-syntax-entry ?/ ". 1456")
1684 (modify-syntax-entry ?* ". 23"))
1687 (modify-syntax-entry ?/ ". 124b")
1688 (modify-syntax-entry ?* ". 23")))
1689 (modify-syntax-entry ?\n "> b")
1690 (insert "/* '\n () */")
1693 (setq list (cons 'col-0-paren list)))))
1695 (set-buffer-modified-p nil))
1698 ;; See if `parse-partial-sexp' returns the eighth element.
1699 (if (c-safe (>= (length (save-excursion
1700 (parse-partial-sexp (point) (point))))
1702 (setq list (cons 'pps-extended-state list))
1704 "CC Mode is incompatible with this version of Emacs - "
1705 "`parse-partial-sexp' has to return at least 10 elements.")))
1707 ;;(message "c-emacs-features: %S" list)
1709 "A list of certain features in the (X)Emacs you are using.
1710 There are many flavors of Emacs out there, each with different
1711 features supporting those needed by CC Mode. The following values
1714 '8-bit 8 bit syntax entry flags (XEmacs style).
1715 '1-bit 1 bit syntax entry flags (Emacs style).
1716 'argumentative-bod-function beginning-of-defun and end-of-defun pass
1717 ARG through to beginning/end-of-defun-function.
1718 'syntax-properties It works to override the syntax for specific characters
1719 in the buffer with the 'syntax-table property. It's
1720 always set - CC Mode no longer works in emacsen without
1722 'gen-comment-delim Generic comment delimiters work
1723 (i.e. the syntax class `!').
1724 'gen-string-delim Generic string delimiters work
1725 (i.e. the syntax class `|').
1726 'pps-extended-state `parse-partial-sexp' returns a list with at least 10
1727 elements, i.e. it contains the position of the start of
1728 the last comment or string. It's always set - CC Mode
1729 no longer works in emacsen without this feature.
1730 'posix-char-classes The regexp engine understands POSIX character classes.
1731 'col-0-paren It's possible to turn off the ad-hoc rule that a paren
1732 in column zero is the start of a defun.
1733 'infodock This is Infodock (based on XEmacs).
1735 '8-bit and '1-bit are mutually exclusive.")
1738 ;;; Some helper constants.
1740 ;; If the regexp engine supports POSIX char classes then we can use
1741 ;; them to handle extended charsets correctly.
1742 (if (memq 'posix-char-classes c-emacs-features)
1744 (defconst c-alpha "[:alpha:]")
1745 (defconst c-alnum "[:alnum:]")
1746 (defconst c-digit "[:digit:]")
1747 (defconst c-upper "[:upper:]")
1748 (defconst c-lower "[:lower:]"))
1749 (defconst c-alpha "a-zA-Z")
1750 (defconst c-alnum "a-zA-Z0-9")
1751 (defconst c-digit "0-9")
1752 (defconst c-upper "A-Z")
1753 (defconst c-lower "a-z"))
1756 ;;; System for handling language dependent constants.
1758 ;; This is used to set various language dependent data in a flexible
1759 ;; way: Language constants can be built from the values of other
1760 ;; language constants, also those for other languages. They can also
1761 ;; process the values of other language constants uniformly across all
1762 ;; the languages. E.g. one language constant can list all the type
1763 ;; keywords in each language, and another can build a regexp for each
1764 ;; language from those lists without code duplication.
1766 ;; Language constants are defined with `c-lang-defconst', and their
1767 ;; value forms (referred to as source definitions) are evaluated only
1768 ;; on demand when requested for a particular language with
1769 ;; `c-lang-const'. It's therefore possible to refer to the values of
1770 ;; constants defined later in the file, or in another file, just as
1771 ;; long as all the relevant `c-lang-defconst' have been loaded when
1772 ;; `c-lang-const' is actually evaluated from somewhere else.
1774 ;; `c-lang-const' forms are also evaluated at compile time and
1775 ;; replaced with the values they produce. Thus there's no overhead
1776 ;; for this system when compiled code is used - only the values
1777 ;; actually used in the code are present, and the file(s) containing
1778 ;; the `c-lang-defconst' forms don't need to be loaded at all then.
1779 ;; There are however safeguards to make sure that they can be loaded
1780 ;; to get the source definitions for the values if there's a mismatch
1781 ;; in compiled versions, or if `c-lang-const' is used uncompiled.
1783 ;; Note that the source definitions in a `c-lang-defconst' form are
1784 ;; compiled into the .elc file where it stands; there's no need to
1785 ;; load the source file to get it.
1787 ;; See cc-langs.el for more details about how this system is deployed
1788 ;; in CC Mode, and how the associated language variable system
1789 ;; (`c-lang-defvar') works. That file also contains a lot of
1792 (defun c-add-language (mode base-mode)
1793 "Declare a new language in the language dependent variable system.
1794 This is intended to be used by modes that inherit CC Mode to add new
1795 languages. It should be used at the top level before any calls to
1796 `c-lang-defconst'. MODE is the mode name symbol for the new language,
1797 and BASE-MODE is the mode name symbol for the language in CC Mode that
1798 is to be the template for the new mode.
1800 The exact effect of BASE-MODE is to make all language constants that
1801 haven't got a setting in the new language fall back to their values in
1802 BASE-MODE. It does not have any effect outside the language constant
1804 (unless (string-match "\\`\\(.*-\\)mode\\'" (symbol-name mode))
1805 (error "The mode name symbol `%s' must end with \"-mode\"" mode))
1806 (put mode 'c-mode-prefix (match-string 1 (symbol-name mode)))
1807 (unless (get base-mode 'c-mode-prefix)
1808 (error "Unknown base mode `%s'" base-mode))
1809 (put mode 'c-fallback-mode base-mode))
1811 (defvar c-lang-constants (make-vector 151 0))
1812 ;; Obarray used as a cache to keep track of the language constants.
1813 ;; The constants stored are those defined by `c-lang-defconst' and the values
1814 ;; computed by `c-lang-const'. It's mostly used at compile time but it's not
1815 ;; stored in compiled files.
1817 ;; The obarray contains all the language constants as symbols. The
1818 ;; value cells hold the evaluated values as alists where each car is
1819 ;; the mode name symbol and the corresponding cdr is the evaluated
1820 ;; value in that mode. The property lists hold the source definitions
1821 ;; and other miscellaneous data. The obarray might also contain
1822 ;; various other symbols, but those don't have any variable bindings.
1824 (defvar c-lang-const-expansion nil)
1826 (defsubst c-get-current-file ()
1827 ;; Return the base name of the current file.
1832 ((and (boundp 'byte-compile-dest-file)
1833 (stringp byte-compile-dest-file))
1835 byte-compile-dest-file)
1837 ;; Being evaluated interactively.
1838 (buffer-file-name)))))
1840 (file-name-sans-extension
1841 (file-name-nondirectory file)))))
1843 (defmacro c-lang-defconst-eval-immediately (form)
1844 "Can be used inside a VAL in `c-lang-defconst' to evaluate FORM
1845 immediately, i.e. at the same time as the `c-lang-defconst' form
1846 itself is evaluated."
1847 ;; Evaluate at macro expansion time, i.e. in the
1848 ;; `c--macroexpand-all' inside `c-lang-defconst'.
1851 (defmacro c-lang-defconst (name &rest args)
1852 "Set the language specific values of the language constant NAME.
1853 The second argument can optionally be a docstring. The rest of the
1854 arguments are one or more repetitions of LANG VAL where LANG specifies
1855 the language(s) that VAL applies to. LANG is the name of the
1856 language, i.e. the mode name without the \"-mode\" suffix, or a list
1857 of such language names, or `t' for all languages. VAL is a form to
1858 evaluate to get the value.
1860 If LANG isn't `t' or one of the core languages in CC Mode, it must
1861 have been declared with `c-add-language'.
1863 Neither NAME, LANG nor VAL are evaluated directly - they should not be
1864 quoted. `c-lang-defconst-eval-immediately' can however be used inside
1865 VAL to evaluate parts of it directly.
1867 When VAL is evaluated for some language, that language is temporarily
1868 made current so that `c-lang-const' without an explicit language can
1869 be used inside VAL to refer to the value of a language constant in the
1870 same language. That is particularly useful if LANG is `t'.
1872 VAL is not evaluated right away but rather when the value is requested
1873 with `c-lang-const'. Thus it's possible to use `c-lang-const' inside
1874 VAL to refer to language constants that haven't been defined yet.
1875 However, if the definition of a language constant is in another file
1876 then that file must be loaded \(at compile time) before it's safe to
1877 reference the constant.
1879 The assignments in ARGS are processed in sequence like `setq', so
1880 \(c-lang-const NAME) may be used inside a VAL to refer to the last
1881 assigned value to this language constant, or a value that it has
1882 gotten in another earlier loaded file.
1884 To work well with repeated loads and interactive reevaluation, only
1885 one `c-lang-defconst' for each NAME is permitted per file. If there
1886 already is one it will be completely replaced; the value in the
1887 earlier definition will not affect `c-lang-const' on the same
1888 constant. A file is identified by its base name."
1890 (let* ((sym (intern (symbol-name name) c-lang-constants))
1891 ;; Make `c-lang-const' expand to a straightforward call to
1892 ;; `c-get-lang-constant' in `c--macroexpand-all' below.
1894 ;; (The default behavior, i.e. to expand to a call inside
1895 ;; `eval-when-compile' should be equivalent, since that macro
1896 ;; should only expand to its content if it's used inside a
1897 ;; form that's already evaluated at compile time. It's
1898 ;; however necessary to use our cover macro
1899 ;; `cc-eval-when-compile' due to bugs in `eval-when-compile',
1900 ;; and it expands to a bulkier form that in this case only is
1901 ;; unnecessary garbage that we don't want to store in the
1902 ;; language constant source definitions.)
1903 (c-lang-const-expansion 'call)
1904 (c-langs-are-parametric t)
1909 (error "Not a symbol: %S" name))
1911 (when (stringp (car-safe args))
1912 ;; The docstring is hardly used anywhere since there's no normal
1913 ;; symbol to attach it to. It's primarily for getting the right
1914 ;; format in the source.
1915 (put sym 'variable-documentation (car args))
1916 (setq args (cdr args)))
1919 (error "No assignments in `c-lang-defconst' for %S" name))
1921 ;; Rework ARGS to an association list to make it easier to handle.
1922 ;; It's reversed at the same time to make it easier to implement
1923 ;; the demand-driven (i.e. reversed) evaluation in `c-lang-const'.
1925 (let ((assigned-mode
1926 (cond ((eq (car args) t) t)
1927 ((symbolp (car args))
1928 (list (intern (concat (symbol-name (car args))
1931 (mapcar (lambda (lang)
1933 (error "Not a list of symbols: %S"
1935 (intern (concat (symbol-name lang)
1938 (t (error "Not a symbol or a list of symbols: %S"
1943 (error "No value for %S" (car args)))
1944 (setq args (cdr args)
1947 ;; Emacs has a weird bug where it seems to fail to read
1948 ;; backquote lists from byte compiled files correctly (,@
1949 ;; forms, to be specific), so make sure the bindings in the
1950 ;; expansion below don't contain any backquote stuff.
1951 ;; (XEmacs handles it correctly and doesn't need this for that
1952 ;; reason, but we also use this expansion handle
1953 ;; `c-lang-defconst-eval-immediately' and to register
1954 ;; dependencies on the `c-lang-const's in VAL.)
1955 (setq val (c--macroexpand-all val))
1957 (setq bindings `(cons (cons ',assigned-mode (lambda () ,val)) ,bindings)
1960 ;; Compile in the other files that have provided source
1961 ;; definitions for this symbol, to make sure the order in the
1962 ;; `source' property is correct even when files are loaded out of
1964 (setq pre-files (nreverse
1965 ;; Reverse to get the right load order.
1966 (mapcar 'car (get sym 'source))))
1969 (c-define-lang-constant ',name ,bindings
1970 ,@(and pre-files `(',pre-files))))))
1972 (put 'c-lang-defconst 'lisp-indent-function 1)
1973 ;(eval-after-load "edebug" ; 2006-07-09: def-edebug-spec is now in subr.el.
1975 (def-edebug-spec c-lang-defconst
1976 (&define name [&optional stringp] [&rest sexp def-form]))
1978 (defun c-define-lang-constant (name bindings &optional pre-files)
1979 ;; Used by `c-lang-defconst'.
1981 (let* ((sym (intern (symbol-name name) c-lang-constants))
1982 (source (get sym 'source))
1984 (or (c-get-current-file)
1985 (error "`c-lang-defconst' must be used in a file"))))
1986 (elem (assq file source)))
1988 ;;(when (cdr-safe elem)
1989 ;; (message "Language constant %s redefined in %S" name file))
1991 ;; Note that the order in the source alist is relevant. Like how
1992 ;; `c-lang-defconst' reverses the bindings, this reverses the
1993 ;; order between files so that the last to evaluate comes first.
1996 (unless (assq (car pre-files) source)
1997 (setq source (cons (list (car pre-files)) source)))
1998 (setq pre-files (cdr pre-files)))
1999 (put sym 'source (cons (setq elem (list file)) source)))
2001 (setcdr elem bindings)
2003 ;; Bind the symbol as a variable, or clear any earlier evaluated
2007 ;; Clear the evaluated values that depend on this source.
2008 (let ((agenda (get sym 'dependents))
2009 (visited (make-vector 101 0))
2012 (setq sym (car agenda)
2013 agenda (cdr agenda))
2014 (intern (symbol-name sym) visited)
2016 (setq ptr (get sym 'dependents))
2020 (unless (intern-soft (symbol-name sym) visited)
2021 (setq agenda (cons sym agenda))))))
2025 (defmacro c-lang-const (name &optional lang)
2026 "Get the mode specific value of the language constant NAME in language LANG.
2027 LANG is the name of the language, i.e. the mode name without the
2028 \"-mode\" suffix. If used inside `c-lang-defconst' or
2029 `c-lang-defvar', LANG may be left out to refer to the current
2030 language. NAME and LANG are not evaluated so they should not be
2034 (error "Not a symbol: %S" name))
2036 (error "Not a symbol: %S" lang))
2038 (let ((sym (intern (symbol-name name) c-lang-constants))
2039 (mode (when lang (intern (concat (symbol-name lang) "-mode")))))
2041 (or (get mode 'c-mode-prefix) (null mode)
2042 (error "Unknown language %S: no `c-mode-prefix' property"
2045 (if (eq c-lang-const-expansion 'immediate)
2046 ;; No need to find out the source file(s) when we evaluate
2047 ;; immediately since all the info is already there in the
2048 ;; `source' property.
2049 `',(c-get-lang-constant name nil mode)
2052 (let ((file (c-get-current-file)))
2053 (if file (setq file (intern file)))
2054 ;; Get the source file(s) that must be loaded to get the value
2055 ;; of the constant. If the symbol isn't defined yet we assume
2056 ;; that its definition will come later in this file, and thus
2057 ;; are no file dependencies needed.
2059 ;; Reverse to get the right load order.
2061 (mapcar (lambda (elem)
2062 (if (eq file (car elem))
2063 nil ; Exclude our own file.
2065 (get sym 'source))))))
2066 ;; Make some effort to do a compact call to
2067 ;; `c-get-lang-constant' since it will be compiled in.
2068 (args (and mode `(',mode))))
2070 (if (or source-files args)
2071 (push (and source-files `',source-files) args))
2073 (if (or (eq c-lang-const-expansion 'call)
2074 (and (not c-lang-const-expansion)
2077 (not (boundp 'byte-compile-dest-file))
2078 (not (stringp byte-compile-dest-file)))
2079 ;; Either a straight call is requested in the context, or
2080 ;; we're in an "uncontrolled" context and got no language,
2081 ;; or we're not being byte compiled so the compile time
2082 ;; stuff below is unnecessary.
2083 `(c-get-lang-constant ',name ,@args)
2085 ;; Being compiled. If the loading and compiling version is
2086 ;; the same we use a value that is evaluated at compile time,
2087 ;; otherwise it's evaluated at runtime.
2088 `(if (eq c-version-sym ',c-version-sym)
2089 (cc-eval-when-compile
2090 (c-get-lang-constant ',name ,@args))
2091 (c-get-lang-constant ',name ,@args)))))))
2093 (defvar c-lang-constants-under-evaluation nil
2094 "Alist of constants in the process of being evaluated.
2095 The `cdr' of each entry indicates how far we've looked in the list
2096 of definitions, so that the def for var FOO in c-mode can be defined in
2097 terms of the def for that same var FOO (which will then rely on the
2098 fallback definition for all modes, to break the cycle).")
2100 (defconst c-lang--novalue "novalue")
2102 (defun c-get-lang-constant (name &optional source-files mode)
2103 ;; Used by `c-lang-const'.
2106 (setq mode c-buffer-is-cc-mode)
2107 (error "No current language"))
2109 (let* ((sym (intern (symbol-name name) c-lang-constants))
2110 (source (get sym 'source))
2112 (eval-in-sym (and c-lang-constants-under-evaluation
2113 (caar c-lang-constants-under-evaluation))))
2115 ;; Record the dependencies between this symbol and the one we're
2116 ;; being evaluated in.
2118 (or (memq eval-in-sym (get sym 'dependents))
2119 (put sym 'dependents (cons eval-in-sym (get sym 'dependents)))))
2121 ;; Make sure the source files have entries on the `source'
2122 ;; property so that loading will take place when necessary.
2124 (unless (assq (car source-files) source)
2126 (setq source (cons (list (car source-files)) source)))
2127 ;; Might pull in more definitions which affect the value. The
2128 ;; clearing of dependent values etc is done when the
2129 ;; definition is encountered during the load; this is just to
2130 ;; jump past the check for a cached value below.
2132 (setq source-files (cdr source-files)))
2134 (if (and (boundp sym)
2135 (setq elem (assq mode (symbol-value sym))))
2138 ;; Check if an evaluation of this symbol is already underway.
2139 ;; In that case we just continue with the "assignment" before
2140 ;; the one currently being evaluated, thereby creating the
2141 ;; illusion if a `setq'-like sequence of assignments.
2142 (let* ((c-buffer-is-cc-mode mode)
2144 (or (assq sym c-lang-constants-under-evaluation)
2145 (cons sym (vector source nil))))
2146 ;; Append `c-lang-constants-under-evaluation' even if an
2147 ;; earlier entry is found. It's only necessary to get
2148 ;; the recording of dependencies above correct.
2149 (c-lang-constants-under-evaluation
2150 (cons source-pos c-lang-constants-under-evaluation))
2151 (fallback (get mode 'c-fallback-mode))
2153 ;; Make sure the recursion limits aren't very low
2154 ;; since the `c-lang-const' dependencies can go deep.
2155 (max-specpdl-size (max max-specpdl-size 3000))
2156 (max-lisp-eval-depth (max max-lisp-eval-depth 1000)))
2159 (let ((backup-source-pos (copy-sequence (cdr source-pos))))
2161 ;; First try the original mode but don't accept an
2162 ;; entry matching all languages since the fallback
2163 ;; mode might have an explicit entry before that.
2164 (eq (setq value (c-find-assignment-for-mode
2165 (cdr source-pos) mode nil name))
2167 ;; Try again with the fallback mode from the
2168 ;; original position. Note that
2169 ;; `c-buffer-is-cc-mode' still is the real mode if
2170 ;; language parameterization takes place.
2171 (eq (setq value (c-find-assignment-for-mode
2172 (setcdr source-pos backup-source-pos)
2175 ;; A simple lookup with no fallback mode.
2176 (eq (setq value (c-find-assignment-for-mode
2177 (cdr source-pos) mode t name))
2180 "`%s' got no (prior) value in %S (might be a cyclic reference)"
2184 (setq value (funcall value))
2186 ;; Print a message to aid in locating the error. We don't
2187 ;; print the error itself since that will be done later by
2188 ;; some caller higher up.
2189 (message "Eval error in the `c-lang-defconst' for `%S' in %s:"
2192 (signal (car err) (cdr err))))
2194 (set sym (cons (cons mode value) (symbol-value sym)))
2197 (defun c-find-assignment-for-mode (source-pos mode match-any-lang _name)
2198 ;; Find the first assignment entry that applies to MODE at or after
2199 ;; SOURCE-POS. If MATCH-ANY-LANG is non-nil, entries with `t' as
2200 ;; the language list are considered to match, otherwise they don't.
2201 ;; On return SOURCE-POS is updated to point to the next assignment
2202 ;; after the returned one. If no assignment is found,
2203 ;; `c-lang--novalue' is returned as a magic value.
2205 ;; SOURCE-POS is a vector that points out a specific assignment in
2206 ;; the double alist that's used in the `source' property. The first
2207 ;; element is the position in the top alist which is indexed with
2208 ;; the source files, and the second element is the position in the
2209 ;; nested bindings alist.
2211 ;; NAME is only used for error messages.
2214 (let ((file-entry (elt source-pos 0))
2215 (assignment-entry (elt source-pos 1))
2218 (while (if assignment-entry
2220 ;; Handled the last assignment from one file, begin on the
2221 ;; next. Due to the check in `c-lang-defconst', we know
2222 ;; there's at least one.
2225 (unless (aset source-pos 1
2226 (setq assignment-entry (cdar file-entry)))
2227 ;; The file containing the source definitions has not
2229 (let ((file (symbol-name (caar file-entry)))
2230 (c-lang-constants-under-evaluation nil))
2231 ;;(message (concat "Loading %s to get the source "
2232 ;; "value for language constant %s")
2236 (unless (setq assignment-entry (cdar file-entry))
2237 ;; The load didn't fill in the source for the
2238 ;; constant as expected. The situation is
2239 ;; probably that a derived mode was written for
2240 ;; and compiled with another version of CC Mode,
2241 ;; and the requested constant isn't in the
2242 ;; currently loaded one. Put in a dummy
2243 ;; assignment that matches no language.
2244 (setcdr (car file-entry)
2245 (setq assignment-entry (list (list nil))))))
2247 (aset source-pos 0 (setq file-entry (cdr file-entry)))
2250 (setq assignment (car assignment-entry))
2252 (setq assignment-entry (cdr assignment-entry)))
2254 (when (if (listp (car assignment))
2255 (memq mode (car assignment))
2257 (throw 'found (cdr assignment))))
2261 (defun c-lang-major-mode-is (mode)
2262 ;; `c-major-mode-is' expands to a call to this function inside
2263 ;; `c-lang-defconst'. Here we also match the mode(s) against any
2264 ;; fallback modes for the one in `c-buffer-is-cc-mode', so that
2265 ;; e.g. (c-major-mode-is 'c++-mode) is true in a derived language
2266 ;; that has c++-mode as base mode.
2267 (unless (listp mode)
2268 (setq mode (list mode)))
2269 (let (match (buf-mode c-buffer-is-cc-mode))
2270 (while (if (memq buf-mode mode)
2274 (setq buf-mode (get buf-mode 'c-fallback-mode))))
2278 (cc-provide 'cc-defs)
2280 ;;; cc-defs.el ends here