1 ;;; cc-defs.el --- compile time definitions for CC Mode
3 ;; Copyright (C) 1985, 1987, 1992-2011 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 buffer-syntactic-context-depth) ; XEmacs
52 (cc-bytecomp-defun region-active-p) ; XEmacs
53 (cc-bytecomp-defvar zmacs-region-stays
) ; XEmacs
54 (cc-bytecomp-defvar zmacs-regions
) ; XEmacs
55 (cc-bytecomp-defvar mark-active
) ; Emacs
56 (cc-bytecomp-defvar deactivate-mark
) ; Emacs
57 (cc-bytecomp-defvar inhibit-point-motion-hooks
) ; Emacs
58 (cc-bytecomp-defvar parse-sexp-lookup-properties
) ; Emacs
59 (cc-bytecomp-defvar text-property-default-nonsticky
) ; Emacs 21
60 (cc-bytecomp-defvar lookup-syntax-properties
) ; XEmacs
61 (cc-bytecomp-defun string-to-syntax) ; Emacs 21
64 ;; cc-fix.el contains compatibility macros that should be used if
67 (if (or (/= (regexp-opt-depth "\\(\\(\\)\\)") 2)
68 (not (fboundp 'push
)))
71 ; (eval-after-load "font-lock" ; 2006-07-09. font-lock is now preloaded
73 (if (and (featurep 'xemacs
) ; There is now (2005/12) code in GNU Emacs CVS
74 ; to make the call to f-l-c-k throw an error.
75 (not (featurep 'cc-fix
)) ; only load the file once.
76 (let (font-lock-keywords)
77 (font-lock-compile-keywords '("\\<\\>"))
78 font-lock-keywords
)) ; did the previous call foul this up?
81 ;; The above takes care of the delayed loading, but this is necessary
82 ;; to ensure correct byte compilation.
84 (if (and (featurep 'xemacs
)
85 (not (featurep 'cc-fix
))
88 (let (font-lock-keywords)
89 (font-lock-compile-keywords '("\\<\\>"))
94 ;;; Variables also used at compile time.
96 (defconst c-version
"5.31.8"
97 "CC Mode version number.")
99 (defconst c-version-sym
(intern c-version
))
100 ;; A little more compact and faster in comparisons.
102 (defvar c-buffer-is-cc-mode nil
103 "Non-nil for all buffers with a major mode derived from CC Mode.
104 Otherwise, this variable is nil. I.e. this variable is non-nil for
105 `c-mode', `c++-mode', `objc-mode', `java-mode', `idl-mode',
106 `pike-mode', `awk-mode', and any other non-CC Mode mode that calls
107 `c-initialize-cc-mode'. The value is the mode symbol itself
108 \(i.e. `c-mode' etc) of the original CC Mode mode, or just t if it's
110 (make-variable-buffer-local 'c-buffer-is-cc-mode
)
112 ;; Have to make `c-buffer-is-cc-mode' permanently local so that it
113 ;; survives the initialization of the derived mode.
114 (put 'c-buffer-is-cc-mode
'permanent-local t
)
117 ;; The following is used below during compilation.
119 (defvar c-inside-eval-when-compile nil
)
121 (defmacro cc-eval-when-compile
(&rest body
)
122 "Like `progn', but evaluates the body at compile time.
123 The result of the body appears to the compiler as a quoted constant.
125 This variant works around bugs in `eval-when-compile' in various
126 \(X)Emacs versions. See cc-defs.el for details."
128 (if c-inside-eval-when-compile
129 ;; XEmacs 21.4.6 has a bug in `eval-when-compile' in that it
130 ;; evaluates its body at macro expansion time if it's nested
131 ;; inside another `eval-when-compile'. So we use a dynamically
132 ;; bound variable to avoid nesting them.
136 ;; In all (X)Emacsen so far, `eval-when-compile' byte compiles
137 ;; its contents before evaluating it. That can cause forms to
138 ;; be compiled in situations they aren't intended to be
141 ;; Example: It's not possible to defsubst a primitive, e.g. the
142 ;; following will produce an error (in any emacs flavor), since
143 ;; `nthcdr' is a primitive function that's handled specially by
144 ;; the byte compiler and thus can't be redefined:
146 ;; (defsubst nthcdr (val) val)
148 ;; `defsubst', like `defmacro', needs to be evaluated at
149 ;; compile time, so this will produce an error during byte
152 ;; CC Mode occasionally needs to do things like this for
153 ;; cross-emacs compatibility. It therefore uses the following
154 ;; to conditionally do a `defsubst':
156 ;; (eval-when-compile
157 ;; (if (not (fboundp 'foo))
158 ;; (defsubst foo ...)))
160 ;; But `eval-when-compile' byte compiles its contents and
161 ;; _then_ evaluates it (in all current emacs versions, up to
162 ;; and including Emacs 20.6 and XEmacs 21.1 as of this
163 ;; writing). So this will still produce an error, since the
164 ;; byte compiler will get to the defsubst anyway. That's
165 ;; arguably a bug because the point with `eval-when-compile' is
166 ;; that it should evaluate rather than compile its contents.
168 ;; We get around it by expanding the body to a quoted
169 ;; constant that we eval. That otoh introduce a problem in
170 ;; that a returned lambda expression doesn't get byte
171 ;; compiled (even if `function' is used).
172 (eval '(let ((c-inside-eval-when-compile t
)) ,@body
)))))
174 (put 'cc-eval-when-compile
'lisp-indent-hook
0))
179 (defmacro c-point
(position &optional point
)
180 "Return the value of certain commonly referenced POSITIONs relative to POINT.
181 The current point is used if POINT isn't specified. POSITION can be
182 one of the following symbols:
184 `bol' -- beginning of line
186 `bod' -- beginning of defun
187 `eod' -- end of defun
188 `boi' -- beginning of indentation
189 `ionl' -- indentation of next line
190 `iopl' -- indentation of previous line
191 `bonl' -- beginning of next line
192 `eonl' -- end of next line
193 `bopl' -- beginning of previous line
194 `eopl' -- end of previous line
195 `bosws' -- beginning of syntactic whitespace
196 `eosws' -- end of syntactic whitespace
198 If the referenced position doesn't exist, the closest accessible point
199 to it is returned. This function does not modify the point or the mark."
201 (if (eq (car-safe position
) 'quote
)
202 (let ((position (eval position
)))
206 (if (and (cc-bytecomp-fboundp 'line-beginning-position
) (not point
))
207 `(line-beginning-position)
209 ,@(if point
`((goto-char ,point
)))
214 (if (and (cc-bytecomp-fboundp 'line-end-position
) (not point
))
217 ,@(if point
`((goto-char ,point
)))
223 ,@(if point
`((goto-char ,point
)))
224 (back-to-indentation)
229 ,@(if point
`((goto-char ,point
)))
230 (c-beginning-of-defun-1)
235 ,@(if point
`((goto-char ,point
)))
240 (if (and (cc-bytecomp-fboundp 'line-beginning-position
) (not point
))
241 `(line-beginning-position 0)
243 ,@(if point
`((goto-char ,point
)))
248 (if (and (cc-bytecomp-fboundp 'line-beginning-position
) (not point
))
249 `(line-beginning-position 2)
251 ,@(if point
`((goto-char ,point
)))
256 (if (and (cc-bytecomp-fboundp 'line-end-position
) (not point
))
257 `(line-end-position 0)
259 ,@(if point
`((goto-char ,point
)))
261 (or (bobp) (backward-char))
265 (if (and (cc-bytecomp-fboundp 'line-end-position
) (not point
))
266 `(line-end-position 2)
268 ,@(if point
`((goto-char ,point
)))
275 ,@(if point
`((goto-char ,point
)))
277 (back-to-indentation)
282 ,@(if point
`((goto-char ,point
)))
284 (back-to-indentation)
287 ((eq position
'bosws
)
289 ,@(if point
`((goto-char ,point
)))
290 (c-backward-syntactic-ws)
293 ((eq position
'eosws
)
295 ,@(if point
`((goto-char ,point
)))
296 (c-forward-syntactic-ws)
299 (t (error "Unknown buffer position requested: %s" position
))))
301 ;; The bulk of this should perhaps be in a function to avoid large
302 ;; expansions, but this case is not used anywhere in CC Mode (and
303 ;; probably not anywhere else either) so we only have it to be on
305 (message "Warning: c-point long expansion")
307 ,@(if point
`((goto-char ,point
)))
308 (let ((position ,position
))
310 ((eq position
'bol
) (beginning-of-line))
311 ((eq position
'eol
) (end-of-line))
312 ((eq position
'boi
) (back-to-indentation))
313 ((eq position
'bod
) (c-beginning-of-defun-1))
314 ((eq position
'eod
) (c-end-of-defun-1))
315 ((eq position
'bopl
) (forward-line -
1))
316 ((eq position
'bonl
) (forward-line 1))
317 ((eq position
'eopl
) (progn
319 (or (bobp) (backward-char))))
320 ((eq position
'eonl
) (progn
323 ((eq position
'iopl
) (progn
325 (back-to-indentation)))
326 ((eq position
'ionl
) (progn
328 (back-to-indentation)))
329 ((eq position
'bosws
) (c-backward-syntactic-ws))
330 ((eq position
'eosws
) (c-forward-syntactic-ws))
331 (t (error "Unknown buffer position requested: %s" position
))))
334 (defmacro c-region-is-active-p
()
335 ;; Return t when the region is active. The determination of region
336 ;; activeness is different in both Emacs and XEmacs.
337 (if (cc-bytecomp-boundp 'mark-active
)
343 (defmacro c-set-region-active
(activate)
344 ;; Activate the region if ACTIVE is non-nil, deactivate it
345 ;; otherwise. Covers the differences between Emacs and XEmacs.
346 (if (cc-bytecomp-fboundp 'zmacs-activate-region
)
349 (zmacs-activate-region)
350 (zmacs-deactivate-region))
352 `(setq mark-active
,activate
)))
354 (defmacro c-delete-and-extract-region
(start end
)
355 "Delete the text between START and END and return it."
356 (if (cc-bytecomp-fboundp 'delete-and-extract-region
)
357 ;; Emacs 21.1 and later
358 `(delete-and-extract-region ,start
,end
)
359 ;; XEmacs and Emacs 20.x
361 (buffer-substring ,start
,end
)
362 (delete-region ,start
,end
))))
364 (defmacro c-safe
(&rest body
)
365 ;; safely execute BODY, return nil if an error occurred
369 (put 'c-safe
'lisp-indent-function
0)
371 (defmacro c-int-to-char
(integer)
372 ;; In GNU Emacs, a character is an integer. In XEmacs, a character is a
373 ;; type distinct from an integer. Sometimes we need to convert integers to
374 ;; characters. `c-int-to-char' makes this conversion, if necessary.
375 (if (fboundp 'int-to-char
)
376 `(int-to-char ,integer
)
379 (defmacro c-sentence-end
()
380 ;; Get the regular expression `sentence-end'.
381 (if (cc-bytecomp-fboundp 'sentence-end
)
384 ;; Emacs <22 + XEmacs
387 (defmacro c-default-value-sentence-end
()
388 ;; Get the default value of the variable sentence end.
389 (if (cc-bytecomp-fboundp 'sentence-end
)
391 `(let (sentence-end) (sentence-end))
392 ;; Emacs <22 + XEmacs
393 `(default-value 'sentence-end
)))
395 ;; The following is essentially `save-buffer-state' from lazy-lock.el.
396 ;; It ought to be a standard macro.
397 (defmacro c-save-buffer-state
(varlist &rest body
)
398 "Bind variables according to VARLIST (in `let*' style) and eval BODY,
399 then restore the buffer state under the assumption that no significant
400 modification has been made in BODY. A change is considered
401 significant if it affects the buffer text in any way that isn't
402 completely restored again. Changes in text properties like `face' or
403 `syntax-table' are considered insignificant. This macro allows text
404 properties to be changed, even in a read-only buffer.
406 This macro should be placed around all calculations which set
407 \"insignificant\" text properties in a buffer, even when the buffer is
408 known to be writable. That way, these text properties remain set
409 even if the user undoes the command which set them.
411 This macro should ALWAYS be placed around \"temporary\" internal buffer
412 changes \(like adding a newline to calculate a text-property then
413 deleting it again\), so that the user never sees them on his
414 `buffer-undo-list'. See also `c-tentative-buffer-changes'.
416 However, any user-visible changes to the buffer \(like auto-newlines\)
417 must not be within a `c-save-buffer-state', since the user then
418 wouldn't be able to undo them.
420 The return value is the value of the last form in BODY."
421 `(let* ((modified (buffer-modified-p)) (buffer-undo-list t
)
422 (inhibit-read-only t
) (inhibit-point-motion-hooks t
)
423 before-change-functions after-change-functions
425 buffer-file-name buffer-file-truename
; Prevent primitives checking
426 ; for file modification
432 (set-buffer-modified-p nil
)))))
433 (put 'c-save-buffer-state
'lisp-indent-function
1)
435 (defmacro c-tentative-buffer-changes
(&rest body
)
436 "Eval BODY and optionally restore the buffer contents to the state it
437 was in before BODY. Any changes are kept if the last form in BODY
438 returns non-nil. Otherwise it's undone using the undo facility, and
439 various other buffer state that might be affected by the changes is
440 restored. That includes the current buffer, point, mark, mark
441 activation \(similar to `save-excursion'), and the modified state.
442 The state is also restored if BODY exits nonlocally.
444 If BODY makes a change that unconditionally is undone then wrap this
445 macro inside `c-save-buffer-state'. That way the change can be done
446 even when the buffer is read-only, and without interference from
447 various buffer change hooks."
448 `(let (-tnt-chng-keep
451 ;; Insert an undo boundary for use with `undo-more'. We
452 ;; don't use `undo-boundary' since it doesn't insert one
454 (setq buffer-undo-list
(cons nil buffer-undo-list
)
455 -tnt-chng-state
(c-tnt-chng-record-state)
456 -tnt-chng-keep
(progn ,@body
))
457 (c-tnt-chng-cleanup -tnt-chng-keep -tnt-chng-state
))))
458 (put 'c-tentative-buffer-changes
'lisp-indent-function
0)
460 (defun c-tnt-chng-record-state ()
461 ;; Used internally in `c-tentative-buffer-changes'.
462 (vector buffer-undo-list
; 0
464 ;; No need to use markers for the point and mark; if the
465 ;; undo got out of synch we're hosed anyway.
468 (c-region-is-active-p) ; 4
469 (buffer-modified-p))) ; 5
471 (defun c-tnt-chng-cleanup (keep saved-state
)
472 ;; Used internally in `c-tentative-buffer-changes'.
474 (let ((saved-undo-list (elt saved-state
0)))
475 (if (eq buffer-undo-list saved-undo-list
)
476 ;; No change was done afterall.
477 (setq buffer-undo-list
(cdr saved-undo-list
))
480 ;; Find and remove the undo boundary.
481 (let ((p buffer-undo-list
))
482 (while (not (eq (cdr p
) saved-undo-list
))
484 (setcdr p
(cdr saved-undo-list
)))
486 ;; `primitive-undo' will remove the boundary.
487 (setq saved-undo-list
(cdr saved-undo-list
))
488 (let ((undo-in-progress t
))
489 (while (not (eq (setq buffer-undo-list
490 (primitive-undo 1 buffer-undo-list
))
493 (when (buffer-live-p (elt saved-state
1))
494 (set-buffer (elt saved-state
1))
495 (goto-char (elt saved-state
2))
496 (set-mark (elt saved-state
3))
497 (c-set-region-active (elt saved-state
4))
498 (and (not (elt saved-state
5))
500 (set-buffer-modified-p nil
)))))))
502 (defmacro c-forward-syntactic-ws
(&optional limit
)
503 "Forward skip over syntactic whitespace.
504 Syntactic whitespace is defined as whitespace characters, comments,
505 and preprocessor directives. However if point starts inside a comment
506 or preprocessor directive, the content of it is not treated as
509 LIMIT sets an upper limit of the forward movement, if specified. If
510 LIMIT or the end of the buffer is reached inside a comment or
511 preprocessor directive, the point will be left there.
513 Note that this function might do hidden buffer changes. See the
514 comment at the start of cc-engine.el for more info."
517 (narrow-to-region (point-min) (or ,limit
(point-max)))
521 (defmacro c-backward-syntactic-ws
(&optional limit
)
522 "Backward skip over syntactic whitespace.
523 Syntactic whitespace is defined as whitespace characters, comments,
524 and preprocessor directives. However if point starts inside a comment
525 or preprocessor directive, the content of it is not treated as
528 LIMIT sets a lower limit of the backward movement, if specified. If
529 LIMIT is reached inside a line comment or preprocessor directive then
530 the point is moved into it past the whitespace at the end.
532 Note that this function might do hidden buffer changes. See the
533 comment at the start of cc-engine.el for more info."
536 (narrow-to-region (or ,limit
(point-min)) (point-max))
540 (defmacro c-forward-sexp
(&optional count
)
541 "Move forward across COUNT balanced expressions.
542 A negative COUNT means move backward. Signal an error if the move
543 fails for any reason.
545 This is like `forward-sexp' except that it isn't interactive and does
546 not do any user friendly adjustments of the point and that it isn't
547 susceptible to user configurations such as disabling of signals in
549 (or count
(setq count
1))
550 `(goto-char (scan-sexps (point) ,count
)))
552 (defmacro c-backward-sexp
(&optional count
)
553 "See `c-forward-sexp' and reverse directions."
554 (or count
(setq count
1))
555 `(c-forward-sexp ,(if (numberp count
) (- count
) `(- ,count
))))
557 (defmacro c-safe-scan-lists
(from count depth
&optional limit
)
558 "Like `scan-lists' but returns nil instead of signalling errors
559 for unbalanced parens.
561 A limit for the search may be given. FROM is assumed to be on the
563 (let ((res (if (featurep 'xemacs
)
564 `(scan-lists ,from
,count
,depth nil t
)
565 `(c-safe (scan-lists ,from
,count
,depth
)))))
570 `(narrow-to-region ,limit
(point-max))
571 `(narrow-to-region (point-min) ,limit
))
573 (narrow-to-region ,limit
(point-max))
574 (narrow-to-region (point-min) ,limit
)))
579 ;; Wrappers for common scan-lists cases, mainly because it's almost
580 ;; impossible to get a feel for how that function works.
582 (defmacro c-go-list-forward
()
583 "Move backward across one balanced group of parentheses.
585 Return POINT when we succeed, NIL when we fail. In the latter case, leave
587 `(c-safe (let ((endpos (scan-lists (point) 1 0)))
591 (defmacro c-go-list-backward
()
592 "Move backward across one balanced group of parentheses.
594 Return POINT when we succeed, NIL when we fail. In the latter case, leave
596 `(c-safe (let ((endpos (scan-lists (point) -
1 0)))
600 (defmacro c-up-list-forward
(&optional pos limit
)
601 "Return the first position after the list sexp containing POS,
602 or nil if no such position exists. The point is used if POS is left out.
604 A limit for the search may be given. The start position is assumed to
606 `(c-safe-scan-lists ,(or pos
`(point)) 1 1 ,limit
))
608 (defmacro c-up-list-backward
(&optional pos limit
)
609 "Return the position of the start of 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-down-list-forward
(&optional pos limit
)
617 "Return the first position inside the first list sexp after 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-backward
(&optional pos limit
)
625 "Return the last position inside the last list sexp before 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-go-up-list-forward
(&optional pos limit
)
633 "Move the point to the first position after the list sexp containing POS,
634 or containing the point if POS is left out. Return t if such a
635 position exists, otherwise nil is returned and the point isn't moved.
637 A limit for the search may be given. The start position is assumed to
639 (let ((res `(c-safe (goto-char (scan-lists ,(or pos
`(point)) 1 1)) t
)))
642 (narrow-to-region (point-min) ,limit
)
646 (defmacro c-go-up-list-backward
(&optional pos limit
)
647 "Move the point to the position of the start of 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 ,limit
(point-max))
660 (defmacro c-go-down-list-forward
(&optional pos limit
)
661 "Move the point to the first position inside the first list sexp after POS,
662 or before the point if POS is left out. Return t if such a position
663 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 (point-min) ,limit
)
674 (defmacro c-go-down-list-backward
(&optional pos limit
)
675 "Move the point to the last position inside the last list sexp before 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 ,limit
(point-max))
689 (defmacro c-beginning-of-defun-1
()
690 ;; Wrapper around beginning-of-defun.
692 ;; NOTE: This function should contain the only explicit use of
693 ;; beginning-of-defun in CC Mode. Eventually something better than
694 ;; b-o-d will be available and this should be the only place the
695 ;; code needs to change. Everything else should use
696 ;; (c-beginning-of-defun-1)
698 ;; This is really a bit too large to be a macro but that isn't a
699 ;; problem as long as it only is used in one place in
703 (if (and ,(cc-bytecomp-fboundp 'buffer-syntactic-context-depth
)
704 c-enable-xemacs-performance-kludge-p
)
705 ,(when (cc-bytecomp-fboundp 'buffer-syntactic-context-depth
)
706 ;; XEmacs only. This can improve the performance of
707 ;; c-parse-state to between 3 and 60 times faster when
708 ;; braces are hung. It can also degrade performance by
709 ;; about as much when braces are not hung.
710 '(let (beginning-of-defun-function end-of-defun-function
715 (setq pos
(c-safe-scan-lists
716 (point) -
1 (buffer-syntactic-context-depth))))
718 ((bobp) (setq pos
(point-min)))
720 (let ((distance (skip-chars-backward "^{")))
721 ;; unbalanced parenthesis, while invalid C code,
722 ;; shouldn't cause an infloop! See unbal.c
723 (when (zerop distance
)
726 (setq pos
(point)))))
728 ((not (eq (char-after pos
) ?
{))
733 ;; Emacs, which doesn't have buffer-syntactic-context-depth
734 (let (beginning-of-defun-function end-of-defun-function
)
735 (beginning-of-defun)))
736 ;; if defun-prompt-regexp is non-nil, b-o-d won't leave us at the
738 (and defun-prompt-regexp
739 (looking-at defun-prompt-regexp
)
740 (goto-char (match-end 0)))))
743 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
744 ;; V i r t u a l S e m i c o l o n s
746 ;; In most CC Mode languages, statements are terminated explicitly by
747 ;; semicolons or closing braces. In some of the CC modes (currently only AWK
748 ;; Mode (April 2004)), statements are (or can be) terminated by EOLs. Such a
749 ;; statement is said to be terminated by a "virtual semicolon" (VS). A
750 ;; statement terminated by an actual semicolon or brace is never considered to
753 ;; The indentation engine (or whatever) tests for a VS at a specific position
754 ;; by invoking the macro `c-at-vsemi-p', which in its turn calls the mode
755 ;; specific function (if any) which is the value of the language variable
756 ;; `c-at-vsemi-p-fn'. The actual details of what constitutes a VS in a
757 ;; language are thus encapsulated in code specific to that language
758 ;; (e.g. cc-awk.el). `c-at-vsemi-p' returns non-nil if point (or the optional
759 ;; parameter POS) is at a VS, nil otherwise.
761 ;; The language specific function might well do extensive analysis of the
762 ;; source text, and may use a cacheing scheme to speed up repeated calls.
764 ;; The "virtual semicolon" lies just after the last non-ws token on the line.
765 ;; Like POINT, it is considered to lie between two characters. For example,
766 ;; at the place shown in the following AWK source line:
768 ;; kbyte = 1024 # 1000 if you're not picky
773 ;; In addition to `c-at-vsemi-p-fn', a mode may need to supply a function for
774 ;; `c-vsemi-status-unknown-p-fn'. The macro `c-vsemi-status-unknown-p' is a
775 ;; rather recondite kludge. It exists because the function
776 ;; `c-beginning-of-statement-1' sometimes tests for VSs as an optimisation,
777 ;; but `c-at-vsemi-p' might well need to call `c-beginning-of-statement-1' in
778 ;; its calculations, thus potentially leading to infinite recursion.
780 ;; The macro `c-vsemi-status-unknown-p' resolves this problem; it may return
781 ;; non-nil at any time; returning nil is a guarantee that an immediate
782 ;; invocation of `c-at-vsemi-p' at point will NOT call
783 ;; `c-beginning-of-statement-1'. `c-vsemi-status-unknown-p' may not itself
784 ;; call `c-beginning-of-statement-1'.
786 ;; The macro `c-vsemi-status-unknown-p' will typically check the cacheing
787 ;; scheme used by the `c-at-vsemi-p-fn', hence the name - the status is
788 ;; "unknown" if there is no cache entry current for the line.
789 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
791 (defmacro c-at-vsemi-p
(&optional pos
)
792 ;; Is there a virtual semicolon (not a real one or a }) at POS (defaults to
793 ;; point)? Always returns nil for languages which don't have Virtual
795 ;; This macro might do hidden buffer changes.
797 (funcall c-at-vsemi-p-fn
,@(if pos
`(,pos
)))))
799 (defmacro c-vsemi-status-unknown-p
()
800 ;; Return NIL only if it can be guaranteed that an immediate
801 ;; (c-at-vsemi-p) will NOT call c-beginning-of-statement-1. Otherwise,
802 ;; return non-nil. (See comments above). The function invoked by this
803 ;; macro MUST NOT UNDER ANY CIRCUMSTANCES itself call
804 ;; c-beginning-of-statement-1.
805 ;; Languages which don't have EOL terminated statements always return NIL
806 ;; (they _know_ there's no vsemi ;-).
807 `(if c-vsemi-status-unknown-p-fn
(funcall c-vsemi-status-unknown-p-fn
)))
810 (defmacro c-benign-error
(format &rest args
)
811 ;; Formats an error message for the echo area and dings, i.e. like
812 ;; `error' but doesn't abort.
814 (message ,format
,@args
)
817 (defmacro c-with-syntax-table
(table &rest code
)
818 ;; Temporarily switches to the specified syntax table in a failsafe
819 ;; way to execute code.
820 `(let ((c-with-syntax-table-orig-table (syntax-table)))
823 (set-syntax-table ,table
)
825 (set-syntax-table c-with-syntax-table-orig-table
))))
826 (put 'c-with-syntax-table
'lisp-indent-function
1)
828 (defmacro c-skip-ws-forward
(&optional limit
)
829 "Skip over any whitespace following point.
830 This function skips over horizontal and vertical whitespace and line
833 `(let ((limit (or ,limit
(point-max))))
835 ;; skip-syntax-* doesn't count \n as whitespace..
836 (skip-chars-forward " \t\n\r\f\v" limit
)
837 (when (and (eq (char-after) ?
\\)
841 (progn (backward-char) nil
))))))
843 (skip-chars-forward " \t\n\r\f\v")
844 (when (eq (char-after) ?
\\)
847 (progn (backward-char) nil
)))))))
849 (defmacro c-skip-ws-backward
(&optional limit
)
850 "Skip over any whitespace preceding point.
851 This function skips over horizontal and vertical whitespace and line
854 `(let ((limit (or ,limit
(point-min))))
856 ;; skip-syntax-* doesn't count \n as whitespace..
857 (skip-chars-backward " \t\n\r\f\v" limit
)
859 (eq (char-before) ?
\\)
863 (skip-chars-backward " \t\n\r\f\v")
865 (eq (char-before) ?
\\)))
869 (defvar c-langs-are-parametric nil
))
871 (defmacro c-major-mode-is
(mode)
872 "Return non-nil if the current CC Mode major mode is MODE.
873 MODE is either a mode symbol or a list of mode symbols."
875 (if c-langs-are-parametric
876 ;; Inside a `c-lang-defconst'.
877 `(c-lang-major-mode-is ,mode
)
879 (if (eq (car-safe mode
) 'quote
)
880 (let ((mode (eval mode
)))
882 `(memq c-buffer-is-cc-mode
',mode
)
883 `(eq c-buffer-is-cc-mode
',mode
)))
887 (memq c-buffer-is-cc-mode mode
)
888 (eq c-buffer-is-cc-mode mode
))))))
891 ;; Macros/functions to handle so-called "char properties", which are
892 ;; properties set on a single character and that never spread to any
896 ;; Constant used at compile time to decide whether or not to use
897 ;; XEmacs extents. Check all the extent functions we'll use since
898 ;; some packages might add compatibility aliases for some of them in
900 (defconst c-use-extents
(and (cc-bytecomp-fboundp 'extent-at
)
901 (cc-bytecomp-fboundp 'set-extent-property
)
902 (cc-bytecomp-fboundp 'set-extent-properties
)
903 (cc-bytecomp-fboundp 'make-extent
)
904 (cc-bytecomp-fboundp 'extent-property
)
905 (cc-bytecomp-fboundp 'delete-extent
)
906 (cc-bytecomp-fboundp 'map-extents
))))
908 ;; `c-put-char-property' is complex enough in XEmacs and Emacs < 21 to
909 ;; make it a function.
910 (defalias 'c-put-char-property-fun
911 (cc-eval-when-compile
915 (lambda (pos property value
)
916 (let ((ext (extent-at pos nil property
)))
918 (set-extent-property ext property value
)
919 (set-extent-properties (make-extent pos
(1+ pos
))
925 ((not (cc-bytecomp-boundp 'text-property-default-nonsticky
))
926 ;; In Emacs < 21 we have to mess with the `rear-nonsticky' property.
928 (lambda (pos property value
)
929 (put-text-property pos
(1+ pos
) property value
)
930 (let ((prop (get-text-property pos
'rear-nonsticky
)))
931 (or (memq property prop
)
932 (put-text-property pos
(1+ pos
)
934 (cons property prop
)))))))
935 ;; This won't be used for anything.
937 (cc-bytecomp-defun c-put-char-property-fun) ; Make it known below.
939 (defmacro c-put-char-property
(pos property value
)
940 ;; Put the given property with the given value on the character at
941 ;; POS and make it front and rear nonsticky, or start and end open
942 ;; in XEmacs vocabulary. If the character already has the given
943 ;; property then the value is replaced, and the behavior is
944 ;; undefined if that property has been put by some other function.
945 ;; PROPERTY is assumed to be constant.
947 ;; If there's a `text-property-default-nonsticky' variable (Emacs
948 ;; 21) then it's assumed that the property is present on it.
950 ;; This macro does a hidden buffer change.
951 (setq property
(eval property
))
952 (if (or c-use-extents
953 (not (cc-bytecomp-boundp 'text-property-default-nonsticky
)))
954 ;; XEmacs and Emacs < 21.
955 `(c-put-char-property-fun ,pos
',property
,value
)
956 ;; In Emacs 21 we got the `rear-nonsticky' property covered
957 ;; by `text-property-default-nonsticky'.
959 (put-text-property -pos-
(1+ -pos-
) ',property
,value
))))
961 (defmacro c-get-char-property
(pos property
)
962 ;; Get the value of the given property on the character at POS if
963 ;; it's been put there by `c-put-char-property'. PROPERTY is
964 ;; assumed to be constant.
965 (setq property
(eval property
))
968 `(let ((ext (extent-at ,pos nil
',property
)))
969 (if ext
(extent-property ext
',property
)))
971 `(get-text-property ,pos
',property
)))
973 ;; `c-clear-char-property' is complex enough in Emacs < 21 to make it
974 ;; a function, since we have to mess with the `rear-nonsticky' property.
975 (defalias 'c-clear-char-property-fun
976 (cc-eval-when-compile
977 (unless (or c-use-extents
978 (cc-bytecomp-boundp 'text-property-default-nonsticky
))
980 (lambda (pos property
)
981 (when (get-text-property pos property
)
982 (remove-text-properties pos
(1+ pos
) (list property nil
))
983 (put-text-property pos
(1+ pos
)
985 (delq property
(get-text-property
986 pos
'rear-nonsticky
)))))))))
987 (cc-bytecomp-defun c-clear-char-property-fun) ; Make it known below.
989 (defmacro c-clear-char-property
(pos property
)
990 ;; Remove the given property on the character at POS if it's been put
991 ;; there by `c-put-char-property'. PROPERTY is assumed to be
994 ;; This macro does a hidden buffer change.
995 (setq property
(eval property
))
998 `(let ((ext (extent-at ,pos nil
',property
)))
999 (if ext
(delete-extent ext
))))
1000 ((cc-bytecomp-boundp 'text-property-default-nonsticky
)
1001 ;; In Emacs 21 we got the `rear-nonsticky' property covered
1002 ;; by `text-property-default-nonsticky'.
1004 (remove-text-properties pos
(1+ pos
)
1008 `(c-clear-char-property-fun ,pos
',property
))))
1010 (defmacro c-clear-char-properties
(from to property
)
1011 ;; Remove all the occurrences of the given property in the given
1012 ;; region that has been put with `c-put-char-property'. PROPERTY is
1013 ;; assumed to be constant.
1015 ;; Note that this function does not clean up the property from the
1016 ;; lists of the `rear-nonsticky' properties in the region, if such
1017 ;; are used. Thus it should not be used for common properties like
1020 ;; This macro does hidden buffer changes.
1021 (setq property
(eval property
))
1024 `(map-extents (lambda (ext ignored
)
1025 (delete-extent ext
))
1026 nil
,from
,to nil nil
',property
)
1028 `(remove-text-properties ,from
,to
'(,property nil
))))
1030 (defmacro c-search-forward-char-property
(property value
&optional limit
)
1031 "Search forward for a text-property PROPERTY having value VALUE.
1032 LIMIT bounds the search. The comparison is done with `equal'.
1034 Leave point just after the character, and set the match data on
1035 this character, and return point. If VALUE isn't found, Return
1036 nil; point is then left undefined."
1037 `(let ((place (point)))
1040 (< place
,(or limit
'(point-max)))
1041 (not (equal (get-text-property place
,property
) ,value
)))
1042 (setq place
(next-single-property-change
1043 place
,property nil
,(or limit
'(point-max)))))
1044 (when (< place
,(or limit
'(point-max)))
1046 (search-forward-regexp ".") ; to set the match-data.
1049 (defmacro c-search-backward-char-property
(property value
&optional limit
)
1050 "Search backward for a text-property PROPERTY having value VALUE.
1051 LIMIT bounds the search. The comparison is done with `equal'.
1053 Leave point just before the character, set the match data on this
1054 character, and return point. If VALUE isn't found, Return nil;
1055 point is then left undefined."
1056 `(let ((place (point)))
1059 (> place
,(or limit
'(point-min)))
1060 (not (equal (get-text-property (1- place
) ,property
) ,value
)))
1061 (setq place
(previous-single-property-change
1062 place
,property nil
,(or limit
'(point-min)))))
1063 (when (> place
,(or limit
'(point-max)))
1065 (search-backward-regexp ".") ; to set the match-data.
1068 (defun c-clear-char-property-with-value-function (from to property value
)
1069 "Remove all text-properties PROPERTY from the region (FROM, TO)
1070 which have the value VALUE, as tested by `equal'. These
1071 properties are assumed to be over individual characters, having
1072 been put there by c-put-char-property. POINT remains unchanged."
1073 (let ((place from
) end-place
)
1074 (while ; loop round occurrences of (PROPERTY VALUE)
1076 (while ; loop round changes in PROPERTY till we find VALUE
1079 (not (equal (get-text-property place property
) value
)))
1080 (setq place
(next-single-property-change place property nil to
)))
1082 (setq end-place
(next-single-property-change place property nil to
))
1083 (remove-text-properties place end-place
(cons property nil
))
1084 ;; Do we have to do anything with stickiness here?
1085 (setq place end-place
))))
1087 (defmacro c-clear-char-property-with-value
(from to property value
)
1088 "Remove all text-properties PROPERTY from the region [FROM, TO)
1089 which have the value VALUE, as tested by `equal'. These
1090 properties are assumed to be over individual characters, having
1091 been put there by c-put-char-property. POINT remains unchanged."
1094 `(let ((-property- ,property
))
1095 (map-extents (lambda (ext val
)
1096 (if (equal (extent-property ext -property-
) val
)
1097 (delete-extent ext
)))
1098 nil
,from
,to
,value nil -property-
))
1100 `(c-clear-char-property-with-value-function ,from
,to
,property
,value
)))
1102 ;; Macros to put overlays (Emacs) or extents (XEmacs) on buffer text.
1103 ;; For our purposes, these are characterized by being possible to
1104 ;; remove again without affecting the other text properties in the
1105 ;; buffer that got overridden when they were put.
1107 (defmacro c-put-overlay
(from to property value
)
1108 ;; Put an overlay/extent covering the given range in the current
1109 ;; buffer. It's currently undefined whether it's front/end sticky
1110 ;; or not. The overlay/extent object is returned.
1111 (if (cc-bytecomp-fboundp 'make-overlay
)
1113 `(let ((ol (make-overlay ,from
,to
)))
1114 (overlay-put ol
,property
,value
)
1117 `(let ((ext (make-extent ,from
,to
)))
1118 (set-extent-property ext
,property
,value
)
1121 (defmacro c-delete-overlay
(overlay)
1122 ;; Deletes an overlay/extent object previously retrieved using
1124 (if (cc-bytecomp-fboundp 'make-overlay
)
1126 `(delete-overlay ,overlay
)
1128 `(delete-extent ,overlay
)))
1131 ;; Make edebug understand the macros.
1132 ;(eval-after-load "edebug" ; 2006-07-09: def-edebug-spec is now in subr.el.
1134 (def-edebug-spec cc-eval-when-compile t
)
1135 (def-edebug-spec c-point t
)
1136 (def-edebug-spec c-set-region-active t
)
1137 (def-edebug-spec c-safe t
)
1138 (def-edebug-spec c-save-buffer-state let
*)
1139 (def-edebug-spec c-tentative-buffer-changes t
)
1140 (def-edebug-spec c-forward-syntactic-ws t
)
1141 (def-edebug-spec c-backward-syntactic-ws t
)
1142 (def-edebug-spec c-forward-sexp t
)
1143 (def-edebug-spec c-backward-sexp t
)
1144 (def-edebug-spec c-up-list-forward t
)
1145 (def-edebug-spec c-up-list-backward t
)
1146 (def-edebug-spec c-down-list-forward t
)
1147 (def-edebug-spec c-down-list-backward t
)
1148 (def-edebug-spec c-add-syntax t
)
1149 (def-edebug-spec c-add-class-syntax t
)
1150 (def-edebug-spec c-benign-error t
)
1151 (def-edebug-spec c-with-syntax-table t
)
1152 (def-edebug-spec c-skip-ws-forward t
)
1153 (def-edebug-spec c-skip-ws-backward t
)
1154 (def-edebug-spec c-major-mode-is t
)
1155 (def-edebug-spec c-put-char-property t
)
1156 (def-edebug-spec c-get-char-property t
)
1157 (def-edebug-spec c-clear-char-property t
)
1158 (def-edebug-spec c-clear-char-properties t
)
1159 (def-edebug-spec c-put-overlay t
)
1160 (def-edebug-spec c-delete-overlay t
) ;))
1165 ;; Note: All these after the macros, to be on safe side in avoiding
1166 ;; bugs where macros are defined too late. These bugs often only show
1167 ;; when the files are compiled in a certain order within the same
1170 (defsubst c-end-of-defun-1
()
1171 ;; Replacement for end-of-defun that use c-beginning-of-defun-1.
1172 (let ((start (point)))
1173 ;; Skip forward into the next defun block. Don't bother to avoid
1174 ;; comments, literals etc, since beginning-of-defun doesn't do that
1176 (skip-chars-forward "^}")
1177 (c-beginning-of-defun-1)
1178 (if (eq (char-after) ?
{)
1180 (if (< (point) start
)
1181 (goto-char (point-max)))))
1183 (defconst c-
<-as-paren-syntax
'(4 . ?
>))
1184 (put 'c-
<-as-paren-syntax
'syntax-table c-
<-as-paren-syntax
)
1186 (defsubst c-mark-
<-as-paren
(pos)
1187 ;; Mark the "<" character at POS as a template opener using the
1188 ;; `syntax-table' property via the `category' property.
1190 ;; This function does a hidden buffer change. Note that we use
1191 ;; indirection through the `category' text property. This allows us to
1192 ;; toggle the property in all template brackets simultaneously and
1193 ;; cheaply. We use this, for instance, in `c-parse-state'.
1194 (c-put-char-property pos
'category
'c-
<-as-paren-syntax
))
1196 (defconst c-
>-as-paren-syntax
'(5 . ?
<))
1197 (put 'c-
>-as-paren-syntax
'syntax-table c-
>-as-paren-syntax
)
1199 (defsubst c-mark-
>-as-paren
(pos)
1200 ;; Mark the ">" character at POS as an sexp list closer using the
1201 ;; syntax-table property.
1203 ;; This function does a hidden buffer change. Note that we use
1204 ;; indirection through the `category' text property. This allows us to
1205 ;; toggle the property in all template brackets simultaneously and
1206 ;; cheaply. We use this, for instance, in `c-parse-state'.
1207 (c-put-char-property pos
'category
'c-
>-as-paren-syntax
))
1209 (defsubst c-unmark-
<-
>-as-paren
(pos)
1210 ;; Unmark the "<" or "<" character at POS as an sexp list opener using
1211 ;; the syntax-table property indirectly through the `category' text
1214 ;; This function does a hidden buffer change. Note that we use
1215 ;; indirection through the `category' text property. This allows us to
1216 ;; toggle the property in all template brackets simultaneously and
1217 ;; cheaply. We use this, for instance, in `c-parse-state'.
1218 (c-clear-char-property pos
'category
))
1220 (defsubst c-suppress-
<-
>-as-parens
()
1221 ;; Suppress the syntactic effect of all marked < and > as parens. Note
1222 ;; that this effect is NOT buffer local. You should probably not use
1223 ;; this directly, but only through the macro
1224 ;; `c-with-<->-as-parens-suppressed'
1225 (put 'c-
<-as-paren-syntax
'syntax-table nil
)
1226 (put 'c-
>-as-paren-syntax
'syntax-table nil
))
1228 (defsubst c-restore-
<-
>-as-parens
()
1229 ;; Restore the syntactic effect of all marked <s and >s as parens. This
1230 ;; has no effect on unmarked <s and >s
1231 (put 'c-
<-as-paren-syntax
'syntax-table c-
<-as-paren-syntax
)
1232 (put 'c-
>-as-paren-syntax
'syntax-table c-
>-as-paren-syntax
))
1234 (defmacro c-with-
<-
>-as-parens-suppressed
(&rest forms
)
1235 ;; Like progn, except that the paren property is suppressed on all
1236 ;; template brackets whilst they are running. This macro does a hidden
1240 (c-suppress-<-
>-as-parens
)
1242 (c-restore-<-
>-as-parens
)))
1246 (defconst c-cpp-delimiter
'(14)) ; generic comment syntax
1247 ;; This is the value of the `category' text property placed on every #
1248 ;; which introduces a CPP construct and every EOL (or EOB, or character
1249 ;; preceding //, etc.) which terminates it. We can instantly "comment
1250 ;; out" all CPP constructs by giving `c-cpp-delimiter' a syntax-table
1251 ;; propery '(14) (generic comment delimiter).
1252 (defmacro c-set-cpp-delimiters
(beg end
)
1253 ;; This macro does a hidden buffer change.
1255 (c-put-char-property ,beg
'category
'c-cpp-delimiter
)
1256 (if (< ,end
(point-max))
1257 (c-put-char-property ,end
'category
'c-cpp-delimiter
))))
1258 (defmacro c-clear-cpp-delimiters
(beg end
)
1259 ;; This macro does a hidden buffer change.
1261 (c-clear-char-property ,beg
'category
)
1262 (if (< ,end
(point-max))
1263 (c-clear-char-property ,end
'category
))))
1265 (defsubst c-comment-out-cpps
()
1266 ;; Render all preprocessor constructs syntactically commented out.
1267 (put 'c-cpp-delimiter
'syntax-table c-cpp-delimiter
))
1268 (defsubst c-uncomment-out-cpps
()
1269 ;; Restore the syntactic visibility of preprocessor constructs.
1270 (put 'c-cpp-delimiter
'syntax-table nil
))
1272 (defmacro c-with-cpps-commented-out
(&rest forms
)
1273 ;; Execute FORMS... whilst the syntactic effect of all characters in
1274 ;; all CPP regions is suppressed. In particular, this is to suppress
1275 ;; the syntactic significance of parens/braces/brackets to functions
1276 ;; such as `scan-lists' and `parse-partial-sexp'.
1278 (c-save-buffer-state ()
1279 (c-comment-out-cpps)
1281 (c-save-buffer-state ()
1282 (c-uncomment-out-cpps))))
1284 (defmacro c-with-all-but-one-cpps-commented-out
(beg end
&rest forms
)
1285 ;; Execute FORMS... whilst the syntactic effect of all characters in
1286 ;; every CPP region APART FROM THE ONE BETWEEN BEG and END is
1289 (c-save-buffer-state ()
1290 (c-clear-cpp-delimiters ,beg
,end
)
1291 ,`(c-with-cpps-commented-out ,@forms
))
1292 (c-save-buffer-state ()
1293 (c-set-cpp-delimiters ,beg
,end
))))
1295 (defsubst c-intersect-lists
(list alist
)
1296 ;; return the element of ALIST that matches the first element found
1297 ;; in LIST. Uses assq.
1300 (not (setq match
(assq (car list
) alist
))))
1301 (setq list
(cdr list
)))
1304 (defsubst c-lookup-lists
(list alist1 alist2
)
1305 ;; first, find the first entry from LIST that is present in ALIST1,
1306 ;; then find the entry in ALIST2 for that entry.
1307 (assq (car (c-intersect-lists list alist1
)) alist2
))
1309 (defsubst c-langelem-sym
(langelem)
1310 "Return the syntactic symbol in LANGELEM.
1312 LANGELEM is either a cons cell on the \"old\" form given as the first
1313 argument to lineup functions or a syntactic element on the \"new\"
1314 form as used in `c-syntactic-element'."
1317 (defsubst c-langelem-pos
(langelem)
1318 "Return the anchor position in LANGELEM, or nil if there is none.
1320 LANGELEM is either a cons cell on the \"old\" form given as the first
1321 argument to lineup functions or a syntactic element on the \"new\"
1322 form as used in `c-syntactic-element'."
1323 (if (consp (cdr langelem
))
1324 (car-safe (cdr langelem
))
1327 (defun c-langelem-col (langelem &optional preserve-point
)
1328 "Return the column of the anchor position in LANGELEM.
1329 Also move the point to that position unless PRESERVE-POINT is non-nil.
1331 LANGELEM is either a cons cell on the \"old\" form given as the first
1332 argument to lineup functions or a syntactic element on the \"new\"
1333 form as used in `c-syntactic-element'."
1334 (let ((pos (c-langelem-pos langelem
))
1339 (prog1 (current-column)
1344 (defsubst c-langelem-2nd-pos
(langelem)
1345 "Return the secondary position in LANGELEM, or nil if there is none.
1347 LANGELEM is typically a syntactic element on the \"new\" form as used
1348 in `c-syntactic-element'. It may also be a cons cell as passed in the
1349 first argument to lineup functions, but then the returned value always
1351 (car-safe (cdr-safe (cdr-safe langelem
))))
1353 (defsubst c-keep-region-active
()
1354 ;; Do whatever is necessary to keep the region active in XEmacs.
1355 ;; This is not needed for Emacs.
1356 (and (boundp 'zmacs-region-stays
)
1357 (setq zmacs-region-stays t
)))
1359 (put 'c-mode
'c-mode-prefix
"c-")
1360 (put 'c
++-mode
'c-mode-prefix
"c++-")
1361 (put 'objc-mode
'c-mode-prefix
"objc-")
1362 (put 'java-mode
'c-mode-prefix
"java-")
1363 (put 'idl-mode
'c-mode-prefix
"idl-")
1364 (put 'pike-mode
'c-mode-prefix
"pike-")
1365 (put 'awk-mode
'c-mode-prefix
"awk-")
1367 (defsubst c-mode-symbol
(suffix)
1368 "Prefix the current mode prefix (e.g. \"c-\") to SUFFIX and return
1369 the corresponding symbol."
1370 (or c-buffer-is-cc-mode
1371 (error "Not inside a CC Mode based mode"))
1372 (let ((mode-prefix (get c-buffer-is-cc-mode
'c-mode-prefix
)))
1374 (error "%S has no mode prefix known to `c-mode-symbol'"
1375 c-buffer-is-cc-mode
))
1376 (intern (concat mode-prefix suffix
))))
1378 (defsubst c-mode-var
(suffix)
1379 "Prefix the current mode prefix (e.g. \"c-\") to SUFFIX and return
1380 the value of the variable with that name."
1381 (symbol-value (c-mode-symbol suffix
)))
1383 (defsubst c-got-face-at
(pos faces
)
1384 "Return non-nil if position POS in the current buffer has any of the
1385 faces in the list FACES."
1386 (let ((pos-faces (get-text-property pos
'face
)))
1387 (if (consp pos-faces
)
1389 (while (and pos-faces
1390 (not (memq (car pos-faces
) faces
)))
1391 (setq pos-faces
(cdr pos-faces
)))
1393 (memq pos-faces faces
))))
1395 (defsubst c-face-name-p
(facename)
1396 ;; Return t if FACENAME is the name of a face. This method is
1397 ;; necessary since facep in XEmacs only returns t for the actual
1398 ;; face objects (while it's only their names that are used just
1399 ;; about anywhere else) without providing a predicate that tests
1401 (memq facename
(face-list)))
1403 (defun c-concat-separated (list separator
)
1404 "Like `concat' on LIST, but separate each element with SEPARATOR.
1405 Notably, null elements in LIST are ignored."
1406 (mapconcat 'identity
(delete nil
(append list nil
)) separator
))
1408 (defun c-make-keywords-re (adorn list
&optional mode
)
1409 "Make a regexp that matches all the strings the list.
1410 Duplicates and nil elements in the list are removed. The resulting
1411 regexp may contain zero or more submatch expressions.
1413 If ADORN is t there will be at least one submatch and the first
1414 surrounds the matched alternative, and the regexp will also not match
1415 a prefix of any identifier. Adorned regexps cannot be appended. The
1416 language variable `c-nonsymbol-key' is used to make the adornment.
1418 A value 'appendable for ADORN is like above, but all alternatives in
1419 the list that end with a word constituent char will have \\> appended
1420 instead, so that the regexp remains appendable. Note that this
1421 variant doesn't always guarantee that an identifier prefix isn't
1422 matched since the symbol constituent '_' is normally considered a
1423 nonword token by \\>.
1425 The optional MODE specifies the language to get `c-nonsymbol-key' from
1426 when it's needed. The default is the current language taken from
1427 `c-buffer-is-cc-mode'."
1431 (unless (member elt unique
)
1433 (setq list
(delete nil unique
)))
1437 (if (eq adorn
'appendable
)
1438 ;; This is kludgy but it works: Search for a string that
1439 ;; doesn't occur in any word in LIST. Append it to all
1440 ;; the alternatives where we want to add \>. Run through
1441 ;; `regexp-opt' and then replace it with \>.
1442 (let ((unique "") pos
)
1444 (setq unique
(concat unique
"@")
1447 (if (string-match unique
(car pos
))
1448 (progn (setq found t
)
1451 (setq pos
(cdr pos
)))
1455 (if (string-match "\\w\\'" (car pos
))
1456 (setcar pos
(concat (car pos
) unique
)))
1457 (setq pos
(cdr pos
)))
1458 (setq re
(regexp-opt list
))
1460 (while (string-match unique re pos
)
1461 (setq pos
(+ (match-beginning 0) 2)
1462 re
(replace-match "\\>" t t re
))))
1464 (setq re
(regexp-opt list
)))
1466 ;; Emacs 20 and XEmacs (all versions so far) has a buggy
1467 ;; regexp-opt that doesn't always cope with strings containing
1468 ;; newlines. This kludge doesn't handle shy parens correctly
1469 ;; so we can't advice regexp-opt directly with it.
1472 (and (string-match "\n" (car list
)) ; To speed it up a little.
1473 (not (string-match (concat "\\`\\(" re
"\\)\\'")
1475 (setq fail-list
(cons (car list
) fail-list
)))
1476 (setq list
(cdr list
)))
1481 (if (eq adorn
'appendable
)
1483 (if (string-match "\\w\\'" str
)
1484 (concat (regexp-quote str
)
1486 (regexp-quote str
)))
1490 (> (length a
) (length b
))))
1493 ;; Add our own grouping parenthesis around re instead of
1494 ;; passing adorn to `regexp-opt', since in XEmacs it makes the
1495 ;; top level grouping "shy".
1496 (cond ((eq adorn
'appendable
)
1497 (concat "\\(" re
"\\)"))
1499 (concat "\\(" re
"\\)"
1501 (c-get-lang-constant 'c-nonsymbol-key nil mode
)
1506 ;; Produce a regexp that matches nothing.
1511 (put 'c-make-keywords-re
'lisp-indent-function
1)
1513 (defun c-make-bare-char-alt (chars &optional inverted
)
1514 "Make a character alternative string from the list of characters CHARS.
1515 The returned string is of the type that can be used with
1516 `skip-chars-forward' and `skip-chars-backward'. If INVERTED is
1517 non-nil, a caret is prepended to invert the set."
1518 ;; This function ought to be in the elisp core somewhere.
1519 (let ((str (if inverted
"^" "")) char char2
)
1520 (setq chars
(sort (append chars nil
) `<))
1522 (setq char
(pop chars
))
1523 (if (memq char
'(?
\\ ?^ ?-
))
1524 ;; Quoting necessary (this method only works in the skip
1526 (setq str
(format "%s\\%c" str char
))
1527 (setq str
(format "%s%c" str char
)))
1530 (while (and chars
(>= (1+ char2
) (car chars
)))
1531 (setq char2
(pop chars
)))
1532 (unless (= char char2
)
1533 (if (< (1+ char
) char2
)
1534 (setq str
(format "%s-%c" str char2
))
1535 (push char2 chars
))))
1538 ;; Leftovers from (X)Emacs 19 compatibility.
1539 (defalias 'c-regexp-opt
'regexp-opt
)
1540 (defalias 'c-regexp-opt-depth
'regexp-opt-depth
)
1543 ;; Figure out what features this Emacs has
1545 (cc-bytecomp-defvar open-paren-in-column-0-is-defun-start
)
1547 (defconst c-emacs-features
1550 (if (boundp 'infodock-version
)
1551 ;; I've no idea what this actually is, but it's legacy. /mast
1552 (setq list
(cons 'infodock list
)))
1554 ;; XEmacs uses 8-bit modify-syntax-entry flags.
1555 ;; Emacs uses a 1-bit flag. We will have to set up our
1556 ;; syntax tables differently to handle this.
1557 (let ((table (copy-syntax-table))
1559 (modify-syntax-entry ?a
". 12345678" table
)
1563 (setq entry
(aref table ?a
))
1564 ;; In Emacs, table entries are cons cells
1565 (if (consp entry
) (setq entry
(car entry
))))
1567 ((fboundp 'get-char-table
)
1568 (setq entry
(get-char-table ?a table
)))
1570 (t (error "CC Mode is incompatible with this version of Emacs")))
1571 (setq list
(cons (if (= (logand (lsh entry -
16) 255) 255)
1576 ;; Check whether beginning/end-of-defun call
1577 ;; beginning/end-of-defun-function nicely, passing through the
1578 ;; argument and respecting the return code.
1580 (bod-param 'foo
) (eod-param 'foo
)
1581 (beginning-of-defun-function
1582 (lambda (&optional arg
)
1583 (or (eq bod-param
'foo
) (setq bod-param
'bar
))
1584 (and (eq bod-param
'foo
)
1585 (setq bod-param arg
)
1587 (end-of-defun-function
1588 (lambda (&optional arg
)
1589 (and (eq eod-param
'foo
)
1590 (setq eod-param arg
)
1592 (if (save-excursion (and (beginning-of-defun 3) (eq bod-param
3)
1593 (not (beginning-of-defun))
1594 (end-of-defun 3) (eq eod-param
3)
1595 (not (end-of-defun))))
1596 (setq list
(cons 'argumentative-bod-function list
))))
1598 (let ((buf (generate-new-buffer " test"))
1599 parse-sexp-lookup-properties
1600 parse-sexp-ignore-comments
1601 lookup-syntax-properties
)
1602 (with-current-buffer buf
1603 (set-syntax-table (make-syntax-table))
1605 ;; For some reason we have to set some of these after the
1606 ;; buffer has been made current. (Specifically,
1607 ;; `parse-sexp-ignore-comments' in Emacs 21.)
1608 (setq parse-sexp-lookup-properties t
1609 parse-sexp-ignore-comments t
1610 lookup-syntax-properties t
)
1612 ;; Find out if the `syntax-table' text property works.
1613 (modify-syntax-entry ?
< ".")
1614 (modify-syntax-entry ?
> ".")
1616 (c-mark-<-as-paren
(point-min))
1617 (c-mark->-as-paren
(+ 3 (point-min)))
1618 (goto-char (point-min))
1620 (if (= (point) (+ 4 (point-min)))
1621 (setq list
(cons 'syntax-properties list
))
1623 "CC Mode is incompatible with this version of Emacs - "
1624 "support for the `syntax-table' text property "
1627 ;; Find out if generic comment delimiters work.
1629 (modify-syntax-entry ?x
"!")
1630 (if (string-match "\\s!" "x")
1631 (setq list
(cons 'gen-comment-delim list
))))
1633 ;; Find out if generic string delimiters work.
1635 (modify-syntax-entry ?x
"|")
1636 (if (string-match "\\s|" "x")
1637 (setq list
(cons 'gen-string-delim list
))))
1639 ;; See if POSIX char classes work.
1640 (when (and (string-match "[[:alpha:]]" "a")
1641 ;; All versions of Emacs 21 so far haven't fixed
1642 ;; char classes in `skip-chars-forward' and
1643 ;; `skip-chars-backward'.
1645 (delete-region (point-min) (point-max))
1647 (skip-chars-backward "[:alnum:]")
1649 (= (skip-chars-forward "[:alpha:]") 3))
1650 (setq list
(cons 'posix-char-classes list
)))
1652 ;; See if `open-paren-in-column-0-is-defun-start' exists and
1653 ;; isn't buggy (Emacs >= 21.4).
1654 (when (boundp 'open-paren-in-column-0-is-defun-start
)
1655 (let ((open-paren-in-column-0-is-defun-start nil
)
1656 (parse-sexp-ignore-comments t
))
1657 (delete-region (point-min) (point-max))
1658 (set-syntax-table (make-syntax-table))
1659 (modify-syntax-entry ?
\' "\"")
1661 ;; XEmacs. Afaik this is currently an Emacs-only
1662 ;; feature, but it's good to be prepared.
1664 (modify-syntax-entry ?
/ ". 1456")
1665 (modify-syntax-entry ?
* ". 23"))
1668 (modify-syntax-entry ?
/ ". 124b")
1669 (modify-syntax-entry ?
* ". 23")))
1670 (modify-syntax-entry ?
\n "> b")
1671 (insert "/* '\n () */")
1674 (setq list
(cons 'col-0-paren list
)))))
1676 (set-buffer-modified-p nil
))
1679 ;; See if `parse-partial-sexp' returns the eighth element.
1680 (if (c-safe (>= (length (save-excursion (parse-partial-sexp (point) (point))))
1682 (setq list
(cons 'pps-extended-state list
))
1684 "CC Mode is incompatible with this version of Emacs - "
1685 "`parse-partial-sexp' has to return at least 10 elements.")))
1687 ;;(message "c-emacs-features: %S" list)
1689 "A list of certain features in the (X)Emacs you are using.
1690 There are many flavors of Emacs out there, each with different
1691 features supporting those needed by CC Mode. The following values
1694 '8-bit 8 bit syntax entry flags (XEmacs style).
1695 '1-bit 1 bit syntax entry flags (Emacs style).
1696 'argumentative-bod-function beginning-of-defun passes ARG through
1697 to a non-null beginning-of-defun-function. It is assumed
1698 the end-of-defun does the same thing.
1699 'syntax-properties It works to override the syntax for specific characters
1700 in the buffer with the 'syntax-table property. It's
1701 always set - CC Mode no longer works in emacsen without
1703 'gen-comment-delim Generic comment delimiters work
1704 (i.e. the syntax class `!').
1705 'gen-string-delim Generic string delimiters work
1706 (i.e. the syntax class `|').
1707 'pps-extended-state `parse-partial-sexp' returns a list with at least 10
1708 elements, i.e. it contains the position of the start of
1709 the last comment or string. It's always set - CC Mode
1710 no longer works in emacsen without this feature.
1711 'posix-char-classes The regexp engine understands POSIX character classes.
1712 'col-0-paren It's possible to turn off the ad-hoc rule that a paren
1713 in column zero is the start of a defun.
1714 'infodock This is Infodock (based on XEmacs).
1716 '8-bit and '1-bit are mutually exclusive.")
1719 ;;; Some helper constants.
1721 ;; If the regexp engine supports POSIX char classes then we can use
1722 ;; them to handle extended charsets correctly.
1723 (if (memq 'posix-char-classes c-emacs-features
)
1725 (defconst c-alpha
"[:alpha:]")
1726 (defconst c-alnum
"[:alnum:]")
1727 (defconst c-digit
"[:digit:]")
1728 (defconst c-upper
"[:upper:]")
1729 (defconst c-lower
"[:lower:]"))
1730 (defconst c-alpha
"a-zA-Z")
1731 (defconst c-alnum
"a-zA-Z0-9")
1732 (defconst c-digit
"0-9")
1733 (defconst c-upper
"A-Z")
1734 (defconst c-lower
"a-z"))
1737 ;;; System for handling language dependent constants.
1739 ;; This is used to set various language dependent data in a flexible
1740 ;; way: Language constants can be built from the values of other
1741 ;; language constants, also those for other languages. They can also
1742 ;; process the values of other language constants uniformly across all
1743 ;; the languages. E.g. one language constant can list all the type
1744 ;; keywords in each language, and another can build a regexp for each
1745 ;; language from those lists without code duplication.
1747 ;; Language constants are defined with `c-lang-defconst', and their
1748 ;; value forms (referred to as source definitions) are evaluated only
1749 ;; on demand when requested for a particular language with
1750 ;; `c-lang-const'. It's therefore possible to refer to the values of
1751 ;; constants defined later in the file, or in another file, just as
1752 ;; long as all the relevant `c-lang-defconst' have been loaded when
1753 ;; `c-lang-const' is actually evaluated from somewhere else.
1755 ;; `c-lang-const' forms are also evaluated at compile time and
1756 ;; replaced with the values they produce. Thus there's no overhead
1757 ;; for this system when compiled code is used - only the values
1758 ;; actually used in the code are present, and the file(s) containing
1759 ;; the `c-lang-defconst' forms don't need to be loaded at all then.
1760 ;; There are however safeguards to make sure that they can be loaded
1761 ;; to get the source definitions for the values if there's a mismatch
1762 ;; in compiled versions, or if `c-lang-const' is used uncompiled.
1764 ;; Note that the source definitions in a `c-lang-defconst' form are
1765 ;; compiled into the .elc file where it stands; there's no need to
1766 ;; load the source file to get it.
1768 ;; See cc-langs.el for more details about how this system is deployed
1769 ;; in CC Mode, and how the associated language variable system
1770 ;; (`c-lang-defvar') works. That file also contains a lot of
1773 (defun c-add-language (mode base-mode
)
1774 "Declare a new language in the language dependent variable system.
1775 This is intended to be used by modes that inherit CC Mode to add new
1776 languages. It should be used at the top level before any calls to
1777 `c-lang-defconst'. MODE is the mode name symbol for the new language,
1778 and BASE-MODE is the mode name symbol for the language in CC Mode that
1779 is to be the template for the new mode.
1781 The exact effect of BASE-MODE is to make all language constants that
1782 haven't got a setting in the new language fall back to their values in
1783 BASE-MODE. It does not have any effect outside the language constant
1785 (unless (string-match "\\`\\(.*-\\)mode\\'" (symbol-name mode
))
1786 (error "The mode name symbol `%s' must end with \"-mode\"" mode
))
1787 (put mode
'c-mode-prefix
(match-string 1 (symbol-name mode
)))
1788 (unless (get base-mode
'c-mode-prefix
)
1789 (error "Unknown base mode `%s'" base-mode
))
1790 (put mode
'c-fallback-mode base-mode
))
1792 (defvar c-lang-constants
(make-vector 151 0))
1793 ;; This obarray is a cache to keep track of the language constants
1794 ;; defined by `c-lang-defconst' and the evaluated values returned by
1795 ;; `c-lang-const'. It's mostly used at compile time but it's not
1796 ;; stored in compiled files.
1798 ;; The obarray contains all the language constants as symbols. The
1799 ;; value cells hold the evaluated values as alists where each car is
1800 ;; the mode name symbol and the corresponding cdr is the evaluated
1801 ;; value in that mode. The property lists hold the source definitions
1802 ;; and other miscellaneous data. The obarray might also contain
1803 ;; various other symbols, but those don't have any variable bindings.
1805 (defvar c-lang-const-expansion nil
)
1807 (defsubst c-get-current-file
()
1808 ;; Return the base name of the current file.
1813 ((and (boundp 'byte-compile-dest-file
)
1814 (stringp byte-compile-dest-file
))
1816 byte-compile-dest-file
)
1818 ;; Being evaluated interactively.
1819 (buffer-file-name)))))
1821 (file-name-sans-extension
1822 (file-name-nondirectory file
)))))
1824 (defmacro c-lang-defconst-eval-immediately
(form)
1825 "Can be used inside a VAL in `c-lang-defconst' to evaluate FORM
1826 immediately, i.e. at the same time as the `c-lang-defconst' form
1827 itself is evaluated."
1828 ;; Evaluate at macro expansion time, i.e. in the
1829 ;; `cl-macroexpand-all' inside `c-lang-defconst'.
1832 ;; Only used at compile time - suppress "might not be defined at runtime".
1833 (declare-function cl-macroexpand-all
"cl-extra" (form &optional env
))
1835 (defmacro c-lang-defconst
(name &rest args
)
1836 "Set the language specific values of the language constant NAME.
1837 The second argument can optionally be a docstring. The rest of the
1838 arguments are one or more repetitions of LANG VAL where LANG specifies
1839 the language(s) that VAL applies to. LANG is the name of the
1840 language, i.e. the mode name without the \"-mode\" suffix, or a list
1841 of such language names, or `t' for all languages. VAL is a form to
1842 evaluate to get the value.
1844 If LANG isn't `t' or one of the core languages in CC Mode, it must
1845 have been declared with `c-add-language'.
1847 Neither NAME, LANG nor VAL are evaluated directly - they should not be
1848 quoted. `c-lang-defconst-eval-immediately' can however be used inside
1849 VAL to evaluate parts of it directly.
1851 When VAL is evaluated for some language, that language is temporarily
1852 made current so that `c-lang-const' without an explicit language can
1853 be used inside VAL to refer to the value of a language constant in the
1854 same language. That is particularly useful if LANG is `t'.
1856 VAL is not evaluated right away but rather when the value is requested
1857 with `c-lang-const'. Thus it's possible to use `c-lang-const' inside
1858 VAL to refer to language constants that haven't been defined yet.
1859 However, if the definition of a language constant is in another file
1860 then that file must be loaded \(at compile time) before it's safe to
1861 reference the constant.
1863 The assignments in ARGS are processed in sequence like `setq', so
1864 \(c-lang-const NAME) may be used inside a VAL to refer to the last
1865 assigned value to this language constant, or a value that it has
1866 gotten in another earlier loaded file.
1868 To work well with repeated loads and interactive reevaluation, only
1869 one `c-lang-defconst' for each NAME is permitted per file. If there
1870 already is one it will be completely replaced; the value in the
1871 earlier definition will not affect `c-lang-const' on the same
1872 constant. A file is identified by its base name."
1874 (let* ((sym (intern (symbol-name name
) c-lang-constants
))
1875 ;; Make `c-lang-const' expand to a straightforward call to
1876 ;; `c-get-lang-constant' in `cl-macroexpand-all' below.
1878 ;; (The default behavior, i.e. to expand to a call inside
1879 ;; `eval-when-compile' should be equivalent, since that macro
1880 ;; should only expand to its content if it's used inside a
1881 ;; form that's already evaluated at compile time. It's
1882 ;; however necessary to use our cover macro
1883 ;; `cc-eval-when-compile' due to bugs in `eval-when-compile',
1884 ;; and it expands to a bulkier form that in this case only is
1885 ;; unnecessary garbage that we don't want to store in the
1886 ;; language constant source definitions.)
1887 (c-lang-const-expansion 'call
)
1888 (c-langs-are-parametric t
)
1893 (error "Not a symbol: %s" name
))
1895 (when (stringp (car-safe args
))
1896 ;; The docstring is hardly used anywhere since there's no normal
1897 ;; symbol to attach it to. It's primarily for getting the right
1898 ;; format in the source.
1899 (put sym
'variable-documentation
(car args
))
1900 (setq args
(cdr args
)))
1903 (error "No assignments in `c-lang-defconst' for %s" name
))
1905 ;; Rework ARGS to an association list to make it easier to handle.
1906 ;; It's reversed at the same time to make it easier to implement
1907 ;; the demand-driven (i.e. reversed) evaluation in `c-lang-const'.
1909 (let ((assigned-mode
1910 (cond ((eq (car args
) t
) t
)
1911 ((symbolp (car args
))
1912 (list (intern (concat (symbol-name (car args
))
1915 (mapcar (lambda (lang)
1917 (error "Not a list of symbols: %s"
1919 (intern (concat (symbol-name lang
)
1922 (t (error "Not a symbol or a list of symbols: %s"
1927 (error "No value for %s" (car args
)))
1928 (setq args
(cdr args
)
1931 ;; Emacs has a weird bug where it seems to fail to read
1932 ;; backquote lists from byte compiled files correctly (,@
1933 ;; forms, to be specific), so make sure the bindings in the
1934 ;; expansion below don't contain any backquote stuff.
1935 ;; (XEmacs handles it correctly and doesn't need this for that
1936 ;; reason, but we also use this expansion handle
1937 ;; `c-lang-defconst-eval-immediately' and to register
1938 ;; dependencies on the `c-lang-const's in VAL.)
1939 (setq val
(cl-macroexpand-all val
))
1941 (setq bindings
(cons (cons assigned-mode val
) bindings
)
1944 ;; Compile in the other files that have provided source
1945 ;; definitions for this symbol, to make sure the order in the
1946 ;; `source' property is correct even when files are loaded out of
1948 (setq pre-files
(nreverse
1949 ;; Reverse to get the right load order.
1950 (mapcar 'car
(get sym
'source
))))
1953 (c-define-lang-constant ',name
',bindings
1954 ,@(and pre-files
`(',pre-files
))))))
1956 (put 'c-lang-defconst
'lisp-indent-function
1)
1957 ;(eval-after-load "edebug" ; 2006-07-09: def-edebug-spec is now in subr.el.
1959 (def-edebug-spec c-lang-defconst
1960 (&define name
[&optional stringp
] [&rest sexp def-form
]))
1962 (defun c-define-lang-constant (name bindings
&optional pre-files
)
1963 ;; Used by `c-lang-defconst'.
1965 (let* ((sym (intern (symbol-name name
) c-lang-constants
))
1966 (source (get sym
'source
))
1968 (or (c-get-current-file)
1969 (error "`c-lang-defconst' must be used in a file"))))
1970 (elem (assq file source
)))
1972 ;;(when (cdr-safe elem)
1973 ;; (message "Language constant %s redefined in %S" name file))
1975 ;; Note that the order in the source alist is relevant. Like how
1976 ;; `c-lang-defconst' reverses the bindings, this reverses the
1977 ;; order between files so that the last to evaluate comes first.
1980 (unless (assq (car pre-files
) source
)
1981 (setq source
(cons (list (car pre-files
)) source
)))
1982 (setq pre-files
(cdr pre-files
)))
1983 (put sym
'source
(cons (setq elem
(list file
)) source
)))
1985 (setcdr elem bindings
)
1987 ;; Bind the symbol as a variable, or clear any earlier evaluated
1991 ;; Clear the evaluated values that depend on this source.
1992 (let ((agenda (get sym
'dependents
))
1993 (visited (make-vector 101 0))
1996 (setq sym
(car agenda
)
1997 agenda
(cdr agenda
))
1998 (intern (symbol-name sym
) visited
)
2000 (setq ptr
(get sym
'dependents
))
2004 (unless (intern-soft (symbol-name sym
) visited
)
2005 (setq agenda
(cons sym agenda
))))))
2009 (defmacro c-lang-const
(name &optional lang
)
2010 "Get the mode specific value of the language constant NAME in language LANG.
2011 LANG is the name of the language, i.e. the mode name without the
2012 \"-mode\" suffix. If used inside `c-lang-defconst' or
2013 `c-lang-defvar', LANG may be left out to refer to the current
2014 language. NAME and LANG are not evaluated so they should not be
2018 (error "Not a symbol: %s" name
))
2020 (error "Not a symbol: %s" lang
))
2022 (let ((sym (intern (symbol-name name
) c-lang-constants
))
2023 mode source-files args
)
2026 (setq mode
(intern (concat (symbol-name lang
) "-mode")))
2027 (unless (get mode
'c-mode-prefix
)
2029 "Unknown language %S since it got no `c-mode-prefix' property"
2030 (symbol-name lang
))))
2032 (if (eq c-lang-const-expansion
'immediate
)
2033 ;; No need to find out the source file(s) when we evaluate
2034 ;; immediately since all the info is already there in the
2035 ;; `source' property.
2036 `',(c-get-lang-constant name nil mode
)
2038 (let ((file (c-get-current-file)))
2039 (if file
(setq file
(intern file
)))
2040 ;; Get the source file(s) that must be loaded to get the value
2041 ;; of the constant. If the symbol isn't defined yet we assume
2042 ;; that its definition will come later in this file, and thus
2043 ;; are no file dependencies needed.
2044 (setq source-files
(nreverse
2045 ;; Reverse to get the right load order.
2047 (mapcar (lambda (elem)
2048 (if (eq file
(car elem
))
2049 nil
; Exclude our own file.
2051 (get sym
'source
))))))
2053 ;; Make some effort to do a compact call to
2054 ;; `c-get-lang-constant' since it will be compiled in.
2055 (setq args
(and mode
`(',mode
)))
2056 (if (or source-files args
)
2057 (setq args
(cons (and source-files
`',source-files
)
2060 (if (or (eq c-lang-const-expansion
'call
)
2061 (and (not c-lang-const-expansion
)
2064 (not (boundp 'byte-compile-dest-file
))
2065 (not (stringp byte-compile-dest-file
)))
2066 ;; Either a straight call is requested in the context, or
2067 ;; we're in an "uncontrolled" context and got no language,
2068 ;; or we're not being byte compiled so the compile time
2069 ;; stuff below is unnecessary.
2070 `(c-get-lang-constant ',name
,@args
)
2072 ;; Being compiled. If the loading and compiling version is
2073 ;; the same we use a value that is evaluated at compile time,
2074 ;; otherwise it's evaluated at runtime.
2075 `(if (eq c-version-sym
',c-version-sym
)
2076 (cc-eval-when-compile
2077 (c-get-lang-constant ',name
,@args
))
2078 (c-get-lang-constant ',name
,@args
))))))
2080 (defvar c-lang-constants-under-evaluation nil
)
2082 (defun c-get-lang-constant (name &optional source-files mode
)
2083 ;; Used by `c-lang-const'.
2086 (setq mode c-buffer-is-cc-mode
)
2087 (error "No current language"))
2089 (let* ((sym (intern (symbol-name name
) c-lang-constants
))
2090 (source (get sym
'source
))
2092 (eval-in-sym (and c-lang-constants-under-evaluation
2093 (caar c-lang-constants-under-evaluation
))))
2095 ;; Record the dependencies between this symbol and the one we're
2096 ;; being evaluated in.
2098 (or (memq eval-in-sym
(get sym
'dependents
))
2099 (put sym
'dependents
(cons eval-in-sym
(get sym
'dependents
)))))
2101 ;; Make sure the source files have entries on the `source'
2102 ;; property so that loading will take place when necessary.
2104 (unless (assq (car source-files
) source
)
2106 (setq source
(cons (list (car source-files
)) source
)))
2107 ;; Might pull in more definitions which affect the value. The
2108 ;; clearing of dependent values etc is done when the
2109 ;; definition is encountered during the load; this is just to
2110 ;; jump past the check for a cached value below.
2112 (setq source-files
(cdr source-files
)))
2114 (if (and (boundp sym
)
2115 (setq elem
(assq mode
(symbol-value sym
))))
2118 ;; Check if an evaluation of this symbol is already underway.
2119 ;; In that case we just continue with the "assignment" before
2120 ;; the one currently being evaluated, thereby creating the
2121 ;; illusion if a `setq'-like sequence of assignments.
2122 (let* ((c-buffer-is-cc-mode mode
)
2124 (or (assq sym c-lang-constants-under-evaluation
)
2125 (cons sym
(vector source nil
))))
2126 ;; Append `c-lang-constants-under-evaluation' even if an
2127 ;; earlier entry is found. It's only necessary to get
2128 ;; the recording of dependencies above correct.
2129 (c-lang-constants-under-evaluation
2130 (cons source-pos c-lang-constants-under-evaluation
))
2131 (fallback (get mode
'c-fallback-mode
))
2133 ;; Make sure the recursion limits aren't very low
2134 ;; since the `c-lang-const' dependencies can go deep.
2135 (max-specpdl-size (max max-specpdl-size
3000))
2136 (max-lisp-eval-depth (max max-lisp-eval-depth
1000)))
2139 (let ((backup-source-pos (copy-sequence (cdr source-pos
))))
2141 ;; First try the original mode but don't accept an
2142 ;; entry matching all languages since the fallback
2143 ;; mode might have an explicit entry before that.
2144 (eq (setq value
(c-find-assignment-for-mode
2145 (cdr source-pos
) mode nil name
))
2147 ;; Try again with the fallback mode from the
2148 ;; original position. Note that
2149 ;; `c-buffer-is-cc-mode' still is the real mode if
2150 ;; language parameterization takes place.
2151 (eq (setq value
(c-find-assignment-for-mode
2152 (setcdr source-pos backup-source-pos
)
2155 ;; A simple lookup with no fallback mode.
2156 (eq (setq value
(c-find-assignment-for-mode
2157 (cdr source-pos
) mode t name
))
2160 "`%s' got no (prior) value in %s (might be a cyclic reference)"
2164 (setq value
(eval value
))
2166 ;; Print a message to aid in locating the error. We don't
2167 ;; print the error itself since that will be done later by
2168 ;; some caller higher up.
2169 (message "Eval error in the `c-lang-defconst' for `%s' in %s:"
2172 (signal (car err
) (cdr err
))))
2174 (set sym
(cons (cons mode value
) (symbol-value sym
)))
2177 (defun c-find-assignment-for-mode (source-pos mode match-any-lang name
)
2178 ;; Find the first assignment entry that applies to MODE at or after
2179 ;; SOURCE-POS. If MATCH-ANY-LANG is non-nil, entries with `t' as
2180 ;; the language list are considered to match, otherwise they don't.
2181 ;; On return SOURCE-POS is updated to point to the next assignment
2182 ;; after the returned one. If no assignment is found,
2183 ;; `c-lang-constants' is returned as a magic value.
2185 ;; SOURCE-POS is a vector that points out a specific assignment in
2186 ;; the double alist that's used in the `source' property. The first
2187 ;; element is the position in the top alist which is indexed with
2188 ;; the source files, and the second element is the position in the
2189 ;; nested bindings alist.
2191 ;; NAME is only used for error messages.
2194 (let ((file-entry (elt source-pos
0))
2195 (assignment-entry (elt source-pos
1))
2198 (while (if assignment-entry
2200 ;; Handled the last assignment from one file, begin on the
2201 ;; next. Due to the check in `c-lang-defconst', we know
2202 ;; there's at least one.
2205 (unless (aset source-pos
1
2206 (setq assignment-entry
(cdar file-entry
)))
2207 ;; The file containing the source definitions has not
2209 (let ((file (symbol-name (caar file-entry
)))
2210 (c-lang-constants-under-evaluation nil
))
2211 ;;(message (concat "Loading %s to get the source "
2212 ;; "value for language constant %s")
2216 (unless (setq assignment-entry
(cdar file-entry
))
2217 ;; The load didn't fill in the source for the
2218 ;; constant as expected. The situation is
2219 ;; probably that a derived mode was written for
2220 ;; and compiled with another version of CC Mode,
2221 ;; and the requested constant isn't in the
2222 ;; currently loaded one. Put in a dummy
2223 ;; assignment that matches no language.
2224 (setcdr (car file-entry
)
2225 (setq assignment-entry
(list (list nil
))))))
2227 (aset source-pos
0 (setq file-entry
(cdr file-entry
)))
2230 (setq assignment
(car assignment-entry
))
2232 (setq assignment-entry
(cdr assignment-entry
)))
2234 (when (if (listp (car assignment
))
2235 (memq mode
(car assignment
))
2237 (throw 'found
(cdr assignment
))))
2241 (defun c-lang-major-mode-is (mode)
2242 ;; `c-major-mode-is' expands to a call to this function inside
2243 ;; `c-lang-defconst'. Here we also match the mode(s) against any
2244 ;; fallback modes for the one in `c-buffer-is-cc-mode', so that
2245 ;; e.g. (c-major-mode-is 'c++-mode) is true in a derived language
2246 ;; that has c++-mode as base mode.
2247 (unless (listp mode
)
2248 (setq mode
(list mode
)))
2249 (let (match (buf-mode c-buffer-is-cc-mode
))
2250 (while (if (memq buf-mode mode
)
2254 (setq buf-mode
(get buf-mode
'c-fallback-mode
))))
2258 (cc-provide 'cc-defs
)
2260 ;;; cc-defs.el ends here