1 ;;; cc-defs.el --- compile time definitions for CC Mode
3 ;; Copyright (C) 1985, 1987, 1992-2015 Free Software Foundation, Inc.
5 ;; Authors: 2003- Alan Mackenzie
6 ;; 1998- Martin Stjernholm
7 ;; 1992-1999 Barry A. Warsaw
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 (when (featurep 'xemacs
) ; There is now (2005/12) code in GNU Emacs CVS
68 ; to make the call to f-l-c-k throw an error.
69 (eval-after-load "font-lock"
70 '(if (and (not (featurep 'cc-fix
)) ; only load the file once.
71 (let (font-lock-keywords)
72 (font-lock-compile-keywords '("\\<\\>"))
73 font-lock-keywords
)) ; did the previous call foul this up?
76 ;; The above takes care of the delayed loading, but this is necessary
77 ;; to ensure correct byte compilation.
79 (if (and (featurep 'xemacs
)
80 (not (featurep 'cc-fix
))
83 (let (font-lock-keywords)
84 (font-lock-compile-keywords '("\\<\\>"))
88 ;; XEmacs 21.4 doesn't have `delete-dups'.
90 (if (and (not (fboundp 'delete-dups
))
91 (not (featurep 'cc-fix
)))
94 ;;; Variables also used at compile time.
96 (defconst c-version
"5.33"
97 "CC Mode version number.")
99 (defconst c-version-sym
(intern c-version
))
100 ;; A little more compact and faster in comparisons.
102 (defvar c-buffer-is-cc-mode nil
103 "Non-nil for all buffers with a major mode derived from CC Mode.
104 Otherwise, this variable is nil. I.e. this variable is non-nil for
105 `c-mode', `c++-mode', `objc-mode', `java-mode', `idl-mode',
106 `pike-mode', `awk-mode', and any other non-CC Mode mode that calls
107 `c-initialize-cc-mode'. The value is the mode symbol itself
108 \(i.e. `c-mode' etc) of the original CC Mode mode, or just t if it's
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))
177 (defalias 'c--macroexpand-all
178 (if (fboundp 'macroexpand-all
)
179 'macroexpand-all
'cl-macroexpand-all
)))
183 (defmacro c-point
(position &optional point
)
184 "Return the value of certain commonly referenced POSITIONs relative to POINT.
185 The current point is used if POINT isn't specified. POSITION can be
186 one of the following symbols:
188 `bol' -- beginning of line
190 `bod' -- beginning of defun
191 `eod' -- end of defun
192 `boi' -- beginning of indentation
193 `ionl' -- indentation of next line
194 `iopl' -- indentation of previous line
195 `bonl' -- beginning of next line
196 `eonl' -- end of next line
197 `bopl' -- beginning of previous line
198 `eopl' -- end of previous line
199 `bosws' -- beginning of syntactic whitespace
200 `eosws' -- end of syntactic whitespace
202 If the referenced position doesn't exist, the closest accessible point
203 to it is returned. This function does not modify the point or the mark."
205 (if (eq (car-safe position
) 'quote
)
206 (let ((position (eval position
)))
210 (if (and (cc-bytecomp-fboundp 'line-beginning-position
) (not point
))
211 `(line-beginning-position)
213 ,@(if point
`((goto-char ,point
)))
218 (if (and (cc-bytecomp-fboundp 'line-end-position
) (not point
))
221 ,@(if point
`((goto-char ,point
)))
227 ,@(if point
`((goto-char ,point
)))
228 (back-to-indentation)
233 ,@(if point
`((goto-char ,point
)))
234 (c-beginning-of-defun-1)
239 ,@(if point
`((goto-char ,point
)))
244 (if (and (cc-bytecomp-fboundp 'line-beginning-position
) (not point
))
245 `(line-beginning-position 0)
247 ,@(if point
`((goto-char ,point
)))
252 (if (and (cc-bytecomp-fboundp 'line-beginning-position
) (not point
))
253 `(line-beginning-position 2)
255 ,@(if point
`((goto-char ,point
)))
260 (if (and (cc-bytecomp-fboundp 'line-end-position
) (not point
))
261 `(line-end-position 0)
263 ,@(if point
`((goto-char ,point
)))
265 (or (bobp) (backward-char))
269 (if (and (cc-bytecomp-fboundp 'line-end-position
) (not point
))
270 `(line-end-position 2)
272 ,@(if point
`((goto-char ,point
)))
279 ,@(if point
`((goto-char ,point
)))
281 (back-to-indentation)
286 ,@(if point
`((goto-char ,point
)))
288 (back-to-indentation)
291 ((eq position
'bosws
)
293 ,@(if point
`((goto-char ,point
)))
294 (c-backward-syntactic-ws)
297 ((eq position
'eosws
)
299 ,@(if point
`((goto-char ,point
)))
300 (c-forward-syntactic-ws)
303 (t (error "Unknown buffer position requested: %s" position
))))
305 ;; The bulk of this should perhaps be in a function to avoid large
306 ;; expansions, but this case is not used anywhere in CC Mode (and
307 ;; probably not anywhere else either) so we only have it to be on
309 (message "Warning: c-point long expansion")
311 ,@(if point
`((goto-char ,point
)))
312 (let ((position ,position
))
314 ((eq position
'bol
) (beginning-of-line))
315 ((eq position
'eol
) (end-of-line))
316 ((eq position
'boi
) (back-to-indentation))
317 ((eq position
'bod
) (c-beginning-of-defun-1))
318 ((eq position
'eod
) (c-end-of-defun-1))
319 ((eq position
'bopl
) (forward-line -
1))
320 ((eq position
'bonl
) (forward-line 1))
321 ((eq position
'eopl
) (progn
323 (or (bobp) (backward-char))))
324 ((eq position
'eonl
) (progn
327 ((eq position
'iopl
) (progn
329 (back-to-indentation)))
330 ((eq position
'ionl
) (progn
332 (back-to-indentation)))
333 ((eq position
'bosws
) (c-backward-syntactic-ws))
334 ((eq position
'eosws
) (c-forward-syntactic-ws))
335 (t (error "Unknown buffer position requested: %s" position
))))
339 ;; Constant to decide at compilation time whether to use category
340 ;; properties. Currently (2010-03) they're available only on GNU Emacs.
341 (defconst c-use-category
343 (let ((parse-sexp-lookup-properties t
)
344 (lookup-syntax-properties t
))
345 (set-syntax-table (make-syntax-table))
347 (put-text-property (point-min) (1+ (point-min))
348 'category
'c-
<-as-paren-syntax
)
349 (put-text-property (+ 3 (point-min)) (+ 4 (point-min))
350 'category
'c-
>-as-paren-syntax
)
351 (goto-char (point-min))
353 (= (point) (+ 4 (point-min)))))))
355 (defvar c-use-extents
)
357 (defmacro c-next-single-property-change
(position prop
&optional object limit
)
358 ;; See the doc string for either of the defuns expanded to.
359 (if (and c-use-extents
360 (fboundp 'next-single-char-property-change
))
361 ;; XEmacs >= 2005-01-25
362 `(next-single-char-property-change ,position
,prop
,object
,limit
)
363 ;; Emacs and earlier XEmacs
364 `(next-single-property-change ,position
,prop
,object
,limit
)))
366 (defmacro c-region-is-active-p
()
367 ;; Return t when the region is active. The determination of region
368 ;; activeness is different in both Emacs and XEmacs.
369 (if (cc-bytecomp-fboundp 'region-active-p
)
375 (defmacro c-set-region-active
(activate)
376 ;; Activate the region if ACTIVE is non-nil, deactivate it
377 ;; otherwise. Covers the differences between Emacs and XEmacs.
378 (if (fboundp 'zmacs-activate-region
)
381 (zmacs-activate-region)
382 (zmacs-deactivate-region))
384 `(setq mark-active
,activate
)))
386 (defmacro c-delete-and-extract-region
(start end
)
387 "Delete the text between START and END and return it."
388 (if (cc-bytecomp-fboundp 'delete-and-extract-region
)
389 ;; Emacs 21.1 and later
390 `(delete-and-extract-region ,start
,end
)
391 ;; XEmacs and Emacs 20.x
393 (buffer-substring ,start
,end
)
394 (delete-region ,start
,end
))))
396 (defmacro c-safe
(&rest body
)
397 ;; safely execute BODY, return nil if an error occurred
401 (put 'c-safe
'lisp-indent-function
0)
403 (defmacro c-int-to-char
(integer)
404 ;; In Emacs, a character is an integer. In XEmacs, a character is a
405 ;; type distinct from an integer. Sometimes we need to convert integers to
406 ;; characters. `c-int-to-char' makes this conversion, if necessary.
407 (if (fboundp 'int-to-char
)
408 `(int-to-char ,integer
)
411 (defmacro c-last-command-char
()
412 ;; The last character just typed. Note that `last-command-event' exists in
413 ;; both Emacs and XEmacs, but with confusingly different meanings.
414 (if (featurep 'xemacs
)
416 'last-command-event
))
418 (defmacro c-sentence-end
()
419 ;; Get the regular expression `sentence-end'.
420 (if (cc-bytecomp-fboundp 'sentence-end
)
423 ;; Emacs <22 + XEmacs
426 (defmacro c-default-value-sentence-end
()
427 ;; Get the default value of the variable sentence end.
428 (if (cc-bytecomp-fboundp 'sentence-end
)
430 `(let (sentence-end) (sentence-end))
431 ;; Emacs <22 + XEmacs
432 `(default-value 'sentence-end
)))
434 ;; The following is essentially `save-buffer-state' from lazy-lock.el.
435 ;; It ought to be a standard macro.
436 (defmacro c-save-buffer-state
(varlist &rest body
)
437 "Bind variables according to VARLIST (in `let*' style) and eval BODY,
438 then restore the buffer state under the assumption that no significant
439 modification has been made in BODY. A change is considered
440 significant if it affects the buffer text in any way that isn't
441 completely restored again. Changes in text properties like `face' or
442 `syntax-table' are considered insignificant. This macro allows text
443 properties to be changed, even in a read-only buffer.
445 This macro should be placed around all calculations which set
446 \"insignificant\" text properties in a buffer, even when the buffer is
447 known to be writable. That way, these text properties remain set
448 even if the user undoes the command which set them.
450 This macro should ALWAYS be placed around \"temporary\" internal buffer
451 changes \(like adding a newline to calculate a text-property then
452 deleting it again\), so that the user never sees them on his
453 `buffer-undo-list'. See also `c-tentative-buffer-changes'.
455 However, any user-visible changes to the buffer \(like auto-newlines\)
456 must not be within a `c-save-buffer-state', since the user then
457 wouldn't be able to undo them.
459 The return value is the value of the last form in BODY."
460 `(let* ((modified (buffer-modified-p)) (buffer-undo-list t
)
461 (inhibit-read-only t
) (inhibit-point-motion-hooks t
)
462 before-change-functions after-change-functions
464 buffer-file-name buffer-file-truename
; Prevent primitives checking
465 ; for file modification
471 (set-buffer-modified-p nil
)))))
472 (put 'c-save-buffer-state
'lisp-indent-function
1)
474 (defmacro c-tentative-buffer-changes
(&rest body
)
475 "Eval BODY and optionally restore the buffer contents to the state it
476 was in before BODY. Any changes are kept if the last form in BODY
477 returns non-nil. Otherwise it's undone using the undo facility, and
478 various other buffer state that might be affected by the changes is
479 restored. That includes the current buffer, point, mark, mark
480 activation \(similar to `save-excursion'), and the modified state.
481 The state is also restored if BODY exits nonlocally.
483 If BODY makes a change that unconditionally is undone then wrap this
484 macro inside `c-save-buffer-state'. That way the change can be done
485 even when the buffer is read-only, and without interference from
486 various buffer change hooks."
487 `(let (-tnt-chng-keep
490 ;; Insert an undo boundary for use with `undo-more'. We
491 ;; don't use `undo-boundary' since it doesn't insert one
493 (setq buffer-undo-list
(cons nil buffer-undo-list
)
494 -tnt-chng-state
(c-tnt-chng-record-state)
495 -tnt-chng-keep
(progn ,@body
))
496 (c-tnt-chng-cleanup -tnt-chng-keep -tnt-chng-state
))))
497 (put 'c-tentative-buffer-changes
'lisp-indent-function
0)
499 (defun c-tnt-chng-record-state ()
500 ;; Used internally in `c-tentative-buffer-changes'.
501 (vector buffer-undo-list
; 0
503 ;; No need to use markers for the point and mark; if the
504 ;; undo got out of synch we're hosed anyway.
507 (c-region-is-active-p) ; 4
508 (buffer-modified-p))) ; 5
510 (defun c-tnt-chng-cleanup (keep saved-state
)
511 ;; Used internally in `c-tentative-buffer-changes'.
513 (let ((saved-undo-list (elt saved-state
0)))
514 (if (eq buffer-undo-list saved-undo-list
)
515 ;; No change was done after all.
516 (setq buffer-undo-list
(cdr saved-undo-list
))
519 ;; Find and remove the undo boundary.
520 (let ((p buffer-undo-list
))
521 (while (not (eq (cdr p
) saved-undo-list
))
523 (setcdr p
(cdr saved-undo-list
)))
525 ;; `primitive-undo' will remove the boundary.
526 (setq saved-undo-list
(cdr saved-undo-list
))
527 (let ((undo-in-progress t
))
528 (while (not (eq (setq buffer-undo-list
529 (primitive-undo 1 buffer-undo-list
))
532 (when (buffer-live-p (elt saved-state
1))
533 (set-buffer (elt saved-state
1))
534 (goto-char (elt saved-state
2))
535 (set-mark (elt saved-state
3))
536 (c-set-region-active (elt saved-state
4))
537 (and (not (elt saved-state
5))
539 (set-buffer-modified-p nil
)))))))
541 (defmacro c-forward-syntactic-ws
(&optional limit
)
542 "Forward skip over syntactic whitespace.
543 Syntactic whitespace is defined as whitespace characters, comments,
544 and preprocessor directives. However if point starts inside a comment
545 or preprocessor directive, the content of it is not treated as
548 LIMIT sets an upper limit of the forward movement, if specified. If
549 LIMIT or the end of the buffer is reached inside a comment or
550 preprocessor directive, the point will be left there.
552 Note that this function might do hidden buffer changes. See the
553 comment at the start of cc-engine.el for more info."
556 (narrow-to-region (point-min) (or ,limit
(point-max)))
560 (defmacro c-backward-syntactic-ws
(&optional limit
)
561 "Backward skip over syntactic whitespace.
562 Syntactic whitespace is defined as whitespace characters, comments,
563 and preprocessor directives. However if point starts inside a comment
564 or preprocessor directive, the content of it is not treated as
567 LIMIT sets a lower limit of the backward movement, if specified. If
568 LIMIT is reached inside a line comment or preprocessor directive then
569 the point is moved into it past the whitespace at the end.
571 Note that this function might do hidden buffer changes. See the
572 comment at the start of cc-engine.el for more info."
575 (narrow-to-region (or ,limit
(point-min)) (point-max))
579 (defmacro c-forward-sexp
(&optional count
)
580 "Move forward across COUNT balanced expressions.
581 A negative COUNT means move backward. Signal an error if the move
582 fails for any reason.
584 This is like `forward-sexp' except that it isn't interactive and does
585 not do any user friendly adjustments of the point and that it isn't
586 susceptible to user configurations such as disabling of signals in
588 (or count
(setq count
1))
589 `(goto-char (scan-sexps (point) ,count
)))
591 (defmacro c-backward-sexp
(&optional count
)
592 "See `c-forward-sexp' and reverse directions."
593 (or count
(setq count
1))
594 `(c-forward-sexp ,(if (numberp count
) (- count
) `(- ,count
))))
596 (defmacro c-safe-scan-lists
(from count depth
&optional limit
)
597 "Like `scan-lists' but returns nil instead of signaling errors
598 for unbalanced parens.
600 A limit for the search may be given. FROM is assumed to be on the
602 (let ((res (if (featurep 'xemacs
)
603 `(scan-lists ,from
,count
,depth nil t
)
604 `(c-safe (scan-lists ,from
,count
,depth
)))))
609 `(narrow-to-region ,limit
(point-max))
610 `(narrow-to-region (point-min) ,limit
))
612 (narrow-to-region ,limit
(point-max))
613 (narrow-to-region (point-min) ,limit
)))
618 ;; Wrappers for common scan-lists cases, mainly because it's almost
619 ;; impossible to get a feel for how that function works.
621 (defmacro c-go-list-forward
()
622 "Move backward across one balanced group of parentheses.
624 Return POINT when we succeed, NIL when we fail. In the latter case, leave
626 `(c-safe (let ((endpos (scan-lists (point) 1 0)))
630 (defmacro c-go-list-backward
()
631 "Move backward across one balanced group of parentheses.
633 Return POINT when we succeed, NIL when we fail. In the latter case, leave
635 `(c-safe (let ((endpos (scan-lists (point) -
1 0)))
639 (defmacro c-up-list-forward
(&optional pos limit
)
640 "Return the first position after the list sexp containing POS,
641 or nil if no such position exists. The point is used if POS is left out.
643 A limit for the search may be given. The start position is assumed to
645 `(c-safe-scan-lists ,(or pos
`(point)) 1 1 ,limit
))
647 (defmacro c-up-list-backward
(&optional pos limit
)
648 "Return the position of the start of the list sexp containing POS,
649 or nil if no such position exists. The point is used if POS is left out.
651 A limit for the search may be given. The start position is assumed to
653 `(c-safe-scan-lists ,(or pos
`(point)) -
1 1 ,limit
))
655 (defmacro c-down-list-forward
(&optional pos limit
)
656 "Return the first position inside the first list sexp after POS,
657 or nil if no such position exists. The point is used if POS is left out.
659 A limit for the search may be given. The start position is assumed to
661 `(c-safe-scan-lists ,(or pos
`(point)) 1 -
1 ,limit
))
663 (defmacro c-down-list-backward
(&optional pos limit
)
664 "Return the last position inside the last list sexp before POS,
665 or nil if no such position exists. The point is used if POS is left out.
667 A limit for the search may be given. The start position is assumed to
669 `(c-safe-scan-lists ,(or pos
`(point)) -
1 -
1 ,limit
))
671 (defmacro c-go-up-list-forward
(&optional pos limit
)
672 "Move the point to the first position after the list sexp containing POS,
673 or containing the point if POS is left out. Return t if such a
674 position exists, otherwise nil is returned and the point isn't moved.
676 A limit for the search may be given. The start position is assumed to
678 (let ((res `(c-safe (goto-char (scan-lists ,(or pos
`(point)) 1 1)) t
)))
681 (narrow-to-region (point-min) ,limit
)
685 (defmacro c-go-up-list-backward
(&optional pos limit
)
686 "Move the point to the position of the start of the list sexp containing POS,
687 or containing the point if POS is left out. Return t if such a
688 position exists, otherwise nil is returned and the point isn't moved.
690 A limit for the search may be given. The start position is assumed to
692 (let ((res `(c-safe (goto-char (scan-lists ,(or pos
`(point)) -
1 1)) t
)))
695 (narrow-to-region ,limit
(point-max))
699 (defmacro c-go-down-list-forward
(&optional pos limit
)
700 "Move the point to the first position inside the first list sexp after POS,
701 or before the point if POS is left out. Return t if such a position
702 exists, otherwise nil is returned and the point isn't moved.
704 A limit for the search may be given. The start position is assumed to
706 (let ((res `(c-safe (goto-char (scan-lists ,(or pos
`(point)) 1 -
1)) t
)))
709 (narrow-to-region (point-min) ,limit
)
713 (defmacro c-go-down-list-backward
(&optional pos limit
)
714 "Move the point to the last position inside the last list sexp before POS,
715 or before the point if POS is left out. Return t if such a position
716 exists, otherwise nil is returned and the point isn't moved.
718 A limit for the search may be given. The start position is assumed to
720 (let ((res `(c-safe (goto-char (scan-lists ,(or pos
`(point)) -
1 -
1)) t
)))
723 (narrow-to-region ,limit
(point-max))
728 (defmacro c-beginning-of-defun-1
()
729 ;; Wrapper around beginning-of-defun.
731 ;; NOTE: This function should contain the only explicit use of
732 ;; beginning-of-defun in CC Mode. Eventually something better than
733 ;; b-o-d will be available and this should be the only place the
734 ;; code needs to change. Everything else should use
735 ;; (c-beginning-of-defun-1)
737 ;; This is really a bit too large to be a macro but that isn't a
738 ;; problem as long as it only is used in one place in
742 (if (and ,(fboundp 'buffer-syntactic-context-depth
)
743 c-enable-xemacs-performance-kludge-p
)
744 ,(when (fboundp 'buffer-syntactic-context-depth
)
745 ;; XEmacs only. This can improve the performance of
746 ;; c-parse-state to between 3 and 60 times faster when
747 ;; braces are hung. It can also degrade performance by
748 ;; about as much when braces are not hung.
749 '(let (beginning-of-defun-function end-of-defun-function
754 (setq pos
(c-safe-scan-lists
755 (point) -
1 (buffer-syntactic-context-depth))))
757 ((bobp) (setq pos
(point-min)))
759 (let ((distance (skip-chars-backward "^{")))
760 ;; unbalanced parenthesis, while invalid C code,
761 ;; shouldn't cause an infloop! See unbal.c
762 (when (zerop distance
)
765 (setq pos
(point)))))
767 ((not (eq (char-after pos
) ?
{))
772 ;; Emacs, which doesn't have buffer-syntactic-context-depth
773 (let (beginning-of-defun-function end-of-defun-function
)
774 (beginning-of-defun)))
775 ;; if defun-prompt-regexp is non-nil, b-o-d won't leave us at the
777 (and defun-prompt-regexp
778 (looking-at defun-prompt-regexp
)
779 (goto-char (match-end 0)))))
782 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
783 ;; V i r t u a l S e m i c o l o n s
785 ;; In most CC Mode languages, statements are terminated explicitly by
786 ;; semicolons or closing braces. In some of the CC modes (currently AWK Mode
787 ;; and certain user-specified #define macros in C, C++, etc. (November 2008)),
788 ;; statements are (or can be) terminated by EOLs. Such a statement is said to
789 ;; be terminated by a "virtual semicolon" (VS). A statement terminated by an
790 ;; actual semicolon or brace is never considered to have a VS.
792 ;; The indentation engine (or whatever) tests for a VS at a specific position
793 ;; by invoking the macro `c-at-vsemi-p', which in its turn calls the mode
794 ;; specific function (if any) which is the value of the language variable
795 ;; `c-at-vsemi-p-fn'. This function should only use "low-level" features of
796 ;; CC Mode, i.e. features which won't trigger infinite recursion. ;-) The
797 ;; actual details of what constitutes a VS in a language are thus encapsulated
798 ;; in code specific to that language (e.g. cc-awk.el). `c-at-vsemi-p' returns
799 ;; non-nil if point (or the optional parameter POS) is at a VS, nil otherwise.
801 ;; The language specific function might well do extensive analysis of the
802 ;; source text, and may use a caching scheme to speed up repeated calls.
804 ;; The "virtual semicolon" lies just after the last non-ws token on the line.
805 ;; Like POINT, it is considered to lie between two characters. For example,
806 ;; at the place shown in the following AWK source line:
808 ;; kbyte = 1024 # 1000 if you're not picky
813 ;; In addition to `c-at-vsemi-p-fn', a mode may need to supply a function for
814 ;; `c-vsemi-status-unknown-p-fn'. The macro `c-vsemi-status-unknown-p' is a
815 ;; rather recondite kludge. It exists because the function
816 ;; `c-beginning-of-statement-1' sometimes tests for VSs as an optimization,
817 ;; but `c-at-vsemi-p' might well need to call `c-beginning-of-statement-1' in
818 ;; its calculations, thus potentially leading to infinite recursion.
820 ;; The macro `c-vsemi-status-unknown-p' resolves this problem; it may return
821 ;; non-nil at any time; returning nil is a guarantee that an immediate
822 ;; invocation of `c-at-vsemi-p' at point will NOT call
823 ;; `c-beginning-of-statement-1'. `c-vsemi-status-unknown-p' may not itself
824 ;; call `c-beginning-of-statement-1'.
826 ;; The macro `c-vsemi-status-unknown-p' will typically check the caching
827 ;; scheme used by the `c-at-vsemi-p-fn', hence the name - the status is
828 ;; "unknown" if there is no cache entry current for the line.
829 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
831 (defmacro c-at-vsemi-p
(&optional pos
)
832 ;; Is there a virtual semicolon (not a real one or a }) at POS (defaults to
833 ;; point)? Always returns nil for languages which don't have Virtual
835 ;; This macro might do hidden buffer changes.
837 (funcall c-at-vsemi-p-fn
,@(if pos
`(,pos
)))))
839 (defmacro c-vsemi-status-unknown-p
()
840 ;; Return NIL only if it can be guaranteed that an immediate
841 ;; (c-at-vsemi-p) will NOT call c-beginning-of-statement-1. Otherwise,
842 ;; return non-nil. (See comments above). The function invoked by this
843 ;; macro MUST NOT UNDER ANY CIRCUMSTANCES itself call
844 ;; c-beginning-of-statement-1.
845 ;; Languages which don't have EOL terminated statements always return NIL
846 ;; (they _know_ there's no vsemi ;-).
847 `(if c-vsemi-status-unknown-p-fn
(funcall c-vsemi-status-unknown-p-fn
)))
850 (defmacro c-benign-error
(format &rest args
)
851 ;; Formats an error message for the echo area and dings, i.e. like
852 ;; `error' but doesn't abort.
854 (message ,format
,@args
)
857 (defmacro c-with-syntax-table
(table &rest code
)
858 ;; Temporarily switches to the specified syntax table in a failsafe
859 ;; way to execute code.
860 ;; Maintainers' note: If TABLE is `c++-template-syntax-table', DON'T call
861 ;; any forms inside this that call `c-parse-state'. !!!!
862 `(let ((c-with-syntax-table-orig-table (syntax-table)))
865 (set-syntax-table ,table
)
867 (set-syntax-table c-with-syntax-table-orig-table
))))
868 (put 'c-with-syntax-table
'lisp-indent-function
1)
870 (defmacro c-skip-ws-forward
(&optional limit
)
871 "Skip over any whitespace following point.
872 This function skips over horizontal and vertical whitespace and line
875 `(let ((limit (or ,limit
(point-max))))
877 ;; skip-syntax-* doesn't count \n as whitespace..
878 (skip-chars-forward " \t\n\r\f\v" limit
)
879 (when (and (eq (char-after) ?
\\)
883 (progn (backward-char) nil
))))))
885 (skip-chars-forward " \t\n\r\f\v")
886 (when (eq (char-after) ?
\\)
889 (progn (backward-char) nil
)))))))
891 (defmacro c-skip-ws-backward
(&optional limit
)
892 "Skip over any whitespace preceding point.
893 This function skips over horizontal and vertical whitespace and line
896 `(let ((limit (or ,limit
(point-min))))
898 ;; skip-syntax-* doesn't count \n as whitespace..
899 (skip-chars-backward " \t\n\r\f\v" limit
)
901 (eq (char-before) ?
\\)
905 (skip-chars-backward " \t\n\r\f\v")
907 (eq (char-before) ?
\\)))
911 (defvar c-langs-are-parametric nil
))
913 (defmacro c-major-mode-is
(mode)
914 "Return non-nil if the current CC Mode major mode is MODE.
915 MODE is either a mode symbol or a list of mode symbols."
917 (if c-langs-are-parametric
918 ;; Inside a `c-lang-defconst'.
919 `(c-lang-major-mode-is ,mode
)
921 (if (eq (car-safe mode
) 'quote
)
922 (let ((mode (eval mode
)))
924 `(memq c-buffer-is-cc-mode
',mode
)
925 `(eq c-buffer-is-cc-mode
',mode
)))
929 (memq c-buffer-is-cc-mode mode
)
930 (eq c-buffer-is-cc-mode mode
))))))
933 ;; Macros/functions to handle so-called "char properties", which are
934 ;; properties set on a single character and that never spread to any
938 ;; Constant used at compile time to decide whether or not to use
939 ;; XEmacs extents. Check all the extent functions we'll use since
940 ;; some packages might add compatibility aliases for some of them in
942 (defconst c-use-extents
(and (cc-bytecomp-fboundp 'extent-at
)
943 (cc-bytecomp-fboundp 'set-extent-property
)
944 (cc-bytecomp-fboundp 'set-extent-properties
)
945 (cc-bytecomp-fboundp 'make-extent
)
946 (cc-bytecomp-fboundp 'extent-property
)
947 (cc-bytecomp-fboundp 'delete-extent
)
948 (cc-bytecomp-fboundp 'map-extents
))))
950 (defconst c-
<-as-paren-syntax
'(4 . ?
>))
951 (put 'c-
<-as-paren-syntax
'syntax-table c-
<-as-paren-syntax
)
953 (defconst c-
>-as-paren-syntax
'(5 . ?
<))
954 (put 'c-
>-as-paren-syntax
'syntax-table c-
>-as-paren-syntax
)
956 ;; `c-put-char-property' is complex enough in XEmacs and Emacs < 21 to
957 ;; make it a function.
958 (defalias 'c-put-char-property-fun
959 (cc-eval-when-compile
963 (lambda (pos property value
)
964 (let ((ext (extent-at pos nil property
)))
966 (set-extent-property ext property value
)
967 (set-extent-properties (make-extent pos
(1+ pos
))
973 ((not (cc-bytecomp-boundp 'text-property-default-nonsticky
))
974 ;; In Emacs < 21 we have to mess with the `rear-nonsticky' property.
976 (lambda (pos property value
)
977 (put-text-property pos
(1+ pos
) property value
)
978 (let ((prop (get-text-property pos
'rear-nonsticky
)))
979 (or (memq property prop
)
980 (put-text-property pos
(1+ pos
)
982 (cons property prop
)))))))
983 ;; This won't be used for anything.
985 (cc-bytecomp-defun c-put-char-property-fun) ; Make it known below.
987 (defmacro c-put-char-property
(pos property value
)
988 ;; Put the given property with the given value on the character at
989 ;; POS and make it front and rear nonsticky, or start and end open
990 ;; in XEmacs vocabulary. If the character already has the given
991 ;; property then the value is replaced, and the behavior is
992 ;; undefined if that property has been put by some other function.
993 ;; PROPERTY is assumed to be constant.
995 ;; If there's a `text-property-default-nonsticky' variable (Emacs
996 ;; 21) then it's assumed that the property is present on it.
998 ;; This macro does a hidden buffer change.
999 (setq property
(eval property
))
1000 (if (or c-use-extents
1001 (not (cc-bytecomp-boundp 'text-property-default-nonsticky
)))
1002 ;; XEmacs and Emacs < 21.
1003 `(c-put-char-property-fun ,pos
',property
,value
)
1004 ;; In Emacs 21 we got the `rear-nonsticky' property covered
1005 ;; by `text-property-default-nonsticky'.
1006 `(let ((-pos- ,pos
))
1007 (put-text-property -pos-
(1+ -pos-
) ',property
,value
))))
1009 (defmacro c-get-char-property
(pos property
)
1010 ;; Get the value of the given property on the character at POS if
1011 ;; it's been put there by `c-put-char-property'. PROPERTY is
1012 ;; assumed to be constant.
1013 (setq property
(eval property
))
1016 `(let ((ext (extent-at ,pos nil
',property
)))
1017 (if ext
(extent-property ext
',property
)))
1019 `(get-text-property ,pos
',property
)))
1021 ;; `c-clear-char-property' is complex enough in Emacs < 21 to make it
1022 ;; a function, since we have to mess with the `rear-nonsticky' property.
1023 (defalias 'c-clear-char-property-fun
1024 (cc-eval-when-compile
1025 (unless (or c-use-extents
1026 (cc-bytecomp-boundp 'text-property-default-nonsticky
))
1028 (lambda (pos property
)
1029 (when (get-text-property pos property
)
1030 (remove-text-properties pos
(1+ pos
) (list property nil
))
1031 (put-text-property pos
(1+ pos
)
1033 (delq property
(get-text-property
1034 pos
'rear-nonsticky
)))))))))
1035 (cc-bytecomp-defun c-clear-char-property-fun) ; Make it known below.
1037 (defmacro c-clear-char-property
(pos property
)
1038 ;; Remove the given property on the character at POS if it's been put
1039 ;; there by `c-put-char-property'. PROPERTY is assumed to be
1042 ;; This macro does a hidden buffer change.
1043 (setq property
(eval property
))
1044 (cond (c-use-extents
1046 `(let ((ext (extent-at ,pos nil
',property
)))
1047 (if ext
(delete-extent ext
))))
1048 ((cc-bytecomp-boundp 'text-property-default-nonsticky
)
1049 ;; In Emacs 21 we got the `rear-nonsticky' property covered
1050 ;; by `text-property-default-nonsticky'.
1052 (remove-text-properties pos
(1+ pos
)
1056 `(c-clear-char-property-fun ,pos
',property
))))
1058 (defmacro c-clear-char-properties
(from to property
)
1059 ;; Remove all the occurrences of the given property in the given
1060 ;; region that has been put with `c-put-char-property'. PROPERTY is
1061 ;; assumed to be constant.
1063 ;; Note that this function does not clean up the property from the
1064 ;; lists of the `rear-nonsticky' properties in the region, if such
1065 ;; are used. Thus it should not be used for common properties like
1068 ;; This macro does hidden buffer changes.
1069 (setq property
(eval property
))
1072 `(map-extents (lambda (ext ignored
)
1073 (delete-extent ext
))
1074 nil
,from
,to nil nil
',property
)
1076 `(remove-text-properties ,from
,to
'(,property nil
))))
1078 (defmacro c-search-forward-char-property
(property value
&optional limit
)
1079 "Search forward for a text-property PROPERTY having value VALUE.
1080 LIMIT bounds the search. The comparison is done with `equal'.
1082 Leave point just after the character, and set the match data on
1083 this character, and return point. If VALUE isn't found, Return
1084 nil; point is then left undefined."
1085 `(let ((place (point)))
1088 (< place
,(or limit
'(point-max)))
1089 (not (equal (c-get-char-property place
,property
) ,value
)))
1090 (setq place
(c-next-single-property-change
1091 place
,property nil
,(or limit
'(point-max)))))
1092 (when (< place
,(or limit
'(point-max)))
1094 (search-forward-regexp ".") ; to set the match-data.
1097 (defmacro c-search-backward-char-property
(property value
&optional limit
)
1098 "Search backward for a text-property PROPERTY having value VALUE.
1099 LIMIT bounds the search. The comparison is done with `equal'.
1101 Leave point just before the character, set the match data on this
1102 character, and return point. If VALUE isn't found, Return nil;
1103 point is then left undefined."
1104 `(let ((place (point)))
1107 (> place
,(or limit
'(point-min)))
1108 (not (equal (c-get-char-property (1- place
) ,property
) ,value
)))
1109 (setq place
(,(if (and c-use-extents
1110 (fboundp 'previous-single-char-property-change
))
1111 ;; XEmacs > 2005-01-25.
1112 'previous-single-char-property-change
1113 ;; Emacs and earlier XEmacs.
1114 'previous-single-property-change
)
1115 place
,property nil
,(or limit
'(point-min)))))
1116 (when (> place
,(or limit
'(point-min)))
1118 (search-backward-regexp ".") ; to set the match-data.
1121 (defun c-clear-char-property-with-value-function (from to property value
)
1122 "Remove all text-properties PROPERTY from the region (FROM, TO)
1123 which have the value VALUE, as tested by `equal'. These
1124 properties are assumed to be over individual characters, having
1125 been put there by c-put-char-property. POINT remains unchanged."
1126 (let ((place from
) end-place
)
1127 (while ; loop round occurrences of (PROPERTY VALUE)
1129 (while ; loop round changes in PROPERTY till we find VALUE
1132 (not (equal (get-text-property place property
) value
)))
1133 (setq place
(c-next-single-property-change place property nil to
)))
1135 (setq end-place
(c-next-single-property-change place property nil to
))
1136 (remove-text-properties place end-place
(cons property nil
))
1137 ;; Do we have to do anything with stickiness here?
1138 (setq place end-place
))))
1140 (defmacro c-clear-char-property-with-value
(from to property value
)
1141 "Remove all text-properties PROPERTY from the region [FROM, TO)
1142 which have the value VALUE, as tested by `equal'. These
1143 properties are assumed to be over individual characters, having
1144 been put there by c-put-char-property. POINT remains unchanged."
1147 `(let ((-property- ,property
))
1148 (map-extents (lambda (ext val
)
1149 (if (equal (extent-property ext -property-
) val
)
1150 (delete-extent ext
)))
1151 nil
,from
,to
,value nil -property-
))
1153 `(c-clear-char-property-with-value-function ,from
,to
,property
,value
)))
1155 ;; Macros to put overlays (Emacs) or extents (XEmacs) on buffer text.
1156 ;; For our purposes, these are characterized by being possible to
1157 ;; remove again without affecting the other text properties in the
1158 ;; buffer that got overridden when they were put.
1160 (defmacro c-put-overlay
(from to property value
)
1161 ;; Put an overlay/extent covering the given range in the current
1162 ;; buffer. It's currently undefined whether it's front/end sticky
1163 ;; or not. The overlay/extent object is returned.
1164 (if (cc-bytecomp-fboundp 'make-overlay
)
1166 `(let ((ol (make-overlay ,from
,to
)))
1167 (overlay-put ol
,property
,value
)
1170 `(let ((ext (make-extent ,from
,to
)))
1171 (set-extent-property ext
,property
,value
)
1174 (defmacro c-delete-overlay
(overlay)
1175 ;; Deletes an overlay/extent object previously retrieved using
1177 (if (cc-bytecomp-fboundp 'make-overlay
)
1179 `(delete-overlay ,overlay
)
1181 `(delete-extent ,overlay
)))
1184 ;; Make edebug understand the macros.
1185 ;(eval-after-load "edebug" ; 2006-07-09: def-edebug-spec is now in subr.el.
1187 (def-edebug-spec cc-eval-when-compile
(&rest def-form
))
1188 (def-edebug-spec c-point t
)
1189 (def-edebug-spec c-set-region-active t
)
1190 (def-edebug-spec c-safe t
)
1191 (def-edebug-spec c-save-buffer-state let
*)
1192 (def-edebug-spec c-tentative-buffer-changes t
)
1193 (def-edebug-spec c-forward-syntactic-ws t
)
1194 (def-edebug-spec c-backward-syntactic-ws t
)
1195 (def-edebug-spec c-forward-sexp t
)
1196 (def-edebug-spec c-backward-sexp t
)
1197 (def-edebug-spec c-up-list-forward t
)
1198 (def-edebug-spec c-up-list-backward t
)
1199 (def-edebug-spec c-down-list-forward t
)
1200 (def-edebug-spec c-down-list-backward t
)
1201 (def-edebug-spec c-add-syntax t
)
1202 (def-edebug-spec c-add-class-syntax t
)
1203 (def-edebug-spec c-benign-error t
)
1204 (def-edebug-spec c-with-syntax-table t
)
1205 (def-edebug-spec c-skip-ws-forward t
)
1206 (def-edebug-spec c-skip-ws-backward t
)
1207 (def-edebug-spec c-major-mode-is t
)
1208 (def-edebug-spec c-put-char-property t
)
1209 (def-edebug-spec c-get-char-property t
)
1210 (def-edebug-spec c-clear-char-property t
)
1211 (def-edebug-spec c-clear-char-properties t
)
1212 (def-edebug-spec c-put-overlay t
)
1213 (def-edebug-spec c-delete-overlay t
) ;))
1218 ;; Note: All these after the macros, to be on safe side in avoiding
1219 ;; bugs where macros are defined too late. These bugs often only show
1220 ;; when the files are compiled in a certain order within the same
1223 (defsubst c-end-of-defun-1
()
1224 ;; Replacement for end-of-defun that use c-beginning-of-defun-1.
1225 (let ((start (point)))
1226 ;; Skip forward into the next defun block. Don't bother to avoid
1227 ;; comments, literals etc, since beginning-of-defun doesn't do that
1229 (skip-chars-forward "^}")
1230 (c-beginning-of-defun-1)
1231 (if (eq (char-after) ?
{)
1233 (if (< (point) start
)
1234 (goto-char (point-max)))))
1236 (defmacro c-mark-
<-as-paren
(pos)
1237 ;; Mark the "<" character at POS as a template opener using the
1238 ;; `syntax-table' property either directly (XEmacs) or via a `category'
1239 ;; property (GNU Emacs).
1241 ;; This function does a hidden buffer change. Note that we use
1242 ;; indirection through the `category' text property. This allows us to
1243 ;; toggle the property in all template brackets simultaneously and
1244 ;; cheaply. We use this, for instance, in `c-parse-state'.
1246 `(c-put-char-property ,pos
'category
'c-
<-as-paren-syntax
)
1247 `(c-put-char-property ,pos
'syntax-table c-
<-as-paren-syntax
)))
1250 (defmacro c-mark-
>-as-paren
(pos)
1251 ;; Mark the ">" character at POS as an sexp list closer using the
1252 ;; `syntax-table' property either directly (XEmacs) or via a `category'
1253 ;; property (GNU Emacs).
1255 ;; This function does a hidden buffer change. Note that we use
1256 ;; indirection through the `category' text property. This allows us to
1257 ;; toggle the property in all template brackets simultaneously and
1258 ;; cheaply. We use this, for instance, in `c-parse-state'.
1260 `(c-put-char-property ,pos
'category
'c-
>-as-paren-syntax
)
1261 `(c-put-char-property ,pos
'syntax-table c-
>-as-paren-syntax
)))
1263 (defmacro c-unmark-
<-
>-as-paren
(pos)
1264 ;; Unmark the "<" or "<" character at POS as an sexp list opener using the
1265 ;; `syntax-table' property either directly or indirectly through a
1266 ;; `category' text property.
1268 ;; This function does a hidden buffer change. Note that we try to use
1269 ;; indirection through the `category' text property. This allows us to
1270 ;; toggle the property in all template brackets simultaneously and
1271 ;; cheaply. We use this, for instance, in `c-parse-state'.
1272 `(c-clear-char-property ,pos
,(if c-use-category
''category
''syntax-table
)))
1274 (defsubst c-suppress-
<-
>-as-parens
()
1275 ;; Suppress the syntactic effect of all marked < and > as parens. Note
1276 ;; that this effect is NOT buffer local. You should probably not use
1277 ;; this directly, but only through the macro
1278 ;; `c-with-<->-as-parens-suppressed'
1279 (put 'c-
<-as-paren-syntax
'syntax-table nil
)
1280 (put 'c-
>-as-paren-syntax
'syntax-table nil
))
1282 (defsubst c-restore-
<-
>-as-parens
()
1283 ;; Restore the syntactic effect of all marked <s and >s as parens. This
1284 ;; has no effect on unmarked <s and >s
1285 (put 'c-
<-as-paren-syntax
'syntax-table c-
<-as-paren-syntax
)
1286 (put 'c-
>-as-paren-syntax
'syntax-table c-
>-as-paren-syntax
))
1288 (defmacro c-with-
<-
>-as-parens-suppressed
(&rest forms
)
1289 ;; Like progn, except that the paren property is suppressed on all
1290 ;; template brackets whilst they are running. This macro does a hidden
1294 (c-suppress-<-
>-as-parens
)
1296 (c-restore-<-
>-as-parens
)))
1300 (defconst c-cpp-delimiter
'(14)) ; generic comment syntax
1301 ;; This is the value of the `category' text property placed on every #
1302 ;; which introduces a CPP construct and every EOL (or EOB, or character
1303 ;; preceding //, etc.) which terminates it. We can instantly "comment
1304 ;; out" all CPP constructs by giving `c-cpp-delimiter' a syntax-table
1305 ;; property '(14) (generic comment delimiter).
1306 (defmacro c-set-cpp-delimiters
(beg end
)
1307 ;; This macro does a hidden buffer change.
1309 (c-put-char-property ,beg
'category
'c-cpp-delimiter
)
1310 (if (< ,end
(point-max))
1311 (c-put-char-property ,end
'category
'c-cpp-delimiter
))))
1312 (defmacro c-clear-cpp-delimiters
(beg end
)
1313 ;; This macro does a hidden buffer change.
1315 (c-clear-char-property ,beg
'category
)
1316 (if (< ,end
(point-max))
1317 (c-clear-char-property ,end
'category
))))
1319 (defsubst c-comment-out-cpps
()
1320 ;; Render all preprocessor constructs syntactically commented out.
1321 (put 'c-cpp-delimiter
'syntax-table c-cpp-delimiter
))
1322 (defsubst c-uncomment-out-cpps
()
1323 ;; Restore the syntactic visibility of preprocessor constructs.
1324 (put 'c-cpp-delimiter
'syntax-table nil
))
1326 (defmacro c-with-cpps-commented-out
(&rest forms
)
1327 ;; Execute FORMS... whilst the syntactic effect of all characters in
1328 ;; all CPP regions is suppressed. In particular, this is to suppress
1329 ;; the syntactic significance of parens/braces/brackets to functions
1330 ;; such as `scan-lists' and `parse-partial-sexp'.
1332 (c-save-buffer-state ()
1333 (c-comment-out-cpps)
1335 (c-save-buffer-state ()
1336 (c-uncomment-out-cpps))))
1338 (defmacro c-with-all-but-one-cpps-commented-out
(beg end
&rest forms
)
1339 ;; Execute FORMS... whilst the syntactic effect of all characters in
1340 ;; every CPP region APART FROM THE ONE BETWEEN BEG and END is
1343 (c-save-buffer-state ()
1346 (c-clear-cpp-delimiters ,beg
,end
))
1347 ,`(c-with-cpps-commented-out ,@forms
))
1348 (c-save-buffer-state ()
1351 (c-set-cpp-delimiters ,beg
,end
)))))
1353 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1354 ;; The following macros are to be used only in `c-parse-state' and its
1355 ;; subroutines. Their main purpose is to simplify the handling of C++/Java
1356 ;; template delimiters and CPP macros. In GNU Emacs, this is done slickly by
1357 ;; the judicious use of 'category properties. These don't exist in XEmacs.
1359 ;; Note: in the following macros, there is no special handling for parentheses
1360 ;; inside CPP constructs. That is because CPPs are always syntactically
1361 ;; balanced, thanks to `c-neutralize-CPP-line' in cc-mode.el.
1362 (defmacro c-sc-scan-lists-no-category
+1+1 (from)
1363 ;; Do a (scan-lists FROM 1 1). Any finishing position which either (i) is
1364 ;; determined by and angle bracket; or (ii) is inside a macro whose start
1365 ;; isn't POINT-MACRO-START doesn't count as a finishing position.
1366 `(let ((here (point))
1367 (pos (scan-lists ,from
1 1)))
1368 (while (eq (char-before pos
) ?
>)
1369 (setq pos
(scan-lists pos
1 1)))
1372 (defmacro c-sc-scan-lists-no-category
+1-
1 (from)
1373 ;; Do a (scan-lists FROM 1 -1). Any finishing position which either (i) is
1374 ;; determined by an angle bracket; or (ii) is inside a macro whose start
1375 ;; isn't POINT-MACRO-START doesn't count as a finishing position.
1376 `(let ((here (point))
1377 (pos (scan-lists ,from
1 -
1)))
1378 (while (eq (char-before pos
) ?
<)
1379 (setq pos
(scan-lists pos
1 1))
1380 (setq pos
(scan-lists pos
1 -
1)))
1383 (defmacro c-sc-scan-lists-no-category-1
+1 (from)
1384 ;; Do a (scan-lists FROM -1 1). Any finishing position which either (i) is
1385 ;; determined by and angle bracket; or (ii) is inside a macro whose start
1386 ;; isn't POINT-MACRO-START doesn't count as a finishing position.
1387 `(let ((here (point))
1388 (pos (scan-lists ,from -
1 1)))
1389 (while (eq (char-after pos
) ?
<)
1390 (setq pos
(scan-lists pos -
1 1)))
1393 (defmacro c-sc-scan-lists-no-category-1-1
(from)
1394 ;; Do a (scan-lists FROM -1 -1). Any finishing position which either (i) is
1395 ;; determined by and angle bracket; or (ii) is inside a macro whose start
1396 ;; isn't POINT-MACRO-START doesn't count as a finishing position.
1397 `(let ((here (point))
1398 (pos (scan-lists ,from -
1 -
1)))
1399 (while (eq (char-after pos
) ?
>)
1400 (setq pos
(scan-lists pos -
1 1))
1401 (setq pos
(scan-lists pos -
1 -
1)))
1404 (defmacro c-sc-scan-lists
(from count depth
)
1406 `(scan-lists ,from
,count
,depth
)
1408 ((and (eq count
1) (eq depth
1))
1409 `(c-sc-scan-lists-no-category+1+1 ,from
))
1410 ((and (eq count
1) (eq depth -
1))
1411 `(c-sc-scan-lists-no-category+1-
1 ,from
))
1412 ((and (eq count -
1) (eq depth
1))
1413 `(c-sc-scan-lists-no-category-1+1 ,from
))
1414 ((and (eq count -
1) (eq depth -
1))
1415 `(c-sc-scan-lists-no-category-1-1 ,from
))
1416 (t (error "Invalid parameter(s) to c-sc-scan-lists")))))
1419 (defun c-sc-parse-partial-sexp-no-category (from to targetdepth stopbefore
1421 ;; Do a parse-partial-sexp using the supplied arguments, disregarding
1422 ;; template/generic delimiters < > and disregarding macros other than the
1423 ;; one at POINT-MACRO-START.
1425 ;; NOTE that STOPBEFORE must be nil. TARGETDEPTH should be one less than
1426 ;; the depth in OLDSTATE. This function is thus a SPECIAL PURPOSE variation
1427 ;; on parse-partial-sexp, designed for calling from
1428 ;; `c-remove-stale-state-cache'.
1430 ;; Any finishing position which is determined by an angle bracket delimiter
1431 ;; doesn't count as a finishing position.
1433 ;; Note there is no special handling of CPP constructs here, since these are
1434 ;; always syntactically balanced (thanks to `c-neutralize-CPP-line').
1436 (parse-partial-sexp from to targetdepth stopbefore oldstate
)))
1439 ;; We must have hit targetdepth.
1440 (or (eq (char-before) ?
<)
1441 (eq (char-before) ?
>)))
1443 (if (memq (char-before) '(?
> ?\
) ?\
} ?\
]))
1447 (parse-partial-sexp (point) to targetdepth stopbefore oldstate
)))
1450 (defmacro c-sc-parse-partial-sexp
(from to
&optional targetdepth stopbefore
1453 `(parse-partial-sexp ,from
,to
,targetdepth
,stopbefore
,oldstate
)
1454 `(c-sc-parse-partial-sexp-no-category ,from
,to
,targetdepth
,stopbefore
1458 (defvar c-emacs-features
)
1460 (defmacro c-looking-at-non-alphnumspace
()
1461 "Are we looking at a character which isn't alphanumeric or space?"
1462 (if (memq 'gen-comment-delim c-emacs-features
)
1464 "\\([;#]\\|\\'\\|\\s(\\|\\s)\\|\\s\"\\|\\s\\\\|\\s$\\|\\s<\\|\\s>\\|\\s!\\)")
1466 "\\([;#]\\|\\'\\|\\s(\\|\\s)\\|\\s\"\\|\\s\\\\|\\s$\\|\\s<\\|\\s>\\)"
1467 (let ((prop (c-get-char-property (point) 'syntax-table
)))
1468 (eq prop
'(14))))))) ; '(14) is generic comment delimiter.
1471 (defsubst c-intersect-lists
(list alist
)
1472 ;; return the element of ALIST that matches the first element found
1473 ;; in LIST. Uses assq.
1476 (not (setq match
(assq (car list
) alist
))))
1477 (setq list
(cdr list
)))
1480 (defsubst c-lookup-lists
(list alist1 alist2
)
1481 ;; first, find the first entry from LIST that is present in ALIST1,
1482 ;; then find the entry in ALIST2 for that entry.
1483 (assq (car (c-intersect-lists list alist1
)) alist2
))
1485 (defsubst c-langelem-sym
(langelem)
1486 "Return the syntactic symbol in LANGELEM.
1488 LANGELEM is either a cons cell on the \"old\" form given as the first
1489 argument to lineup functions or a syntactic element on the \"new\"
1490 form as used in `c-syntactic-element'."
1493 (defsubst c-langelem-pos
(langelem)
1494 "Return the anchor position in LANGELEM, or nil if there is none.
1496 LANGELEM is either a cons cell on the \"old\" form given as the first
1497 argument to lineup functions or a syntactic element on the \"new\"
1498 form as used in `c-syntactic-element'."
1499 (if (consp (cdr langelem
))
1500 (car-safe (cdr langelem
))
1503 (defun c-langelem-col (langelem &optional preserve-point
)
1504 "Return the column of the anchor position in LANGELEM.
1505 Also move the point to that position unless PRESERVE-POINT is non-nil.
1507 LANGELEM is either a cons cell on the \"old\" form given as the first
1508 argument to lineup functions or a syntactic element on the \"new\"
1509 form as used in `c-syntactic-element'."
1510 (let ((pos (c-langelem-pos langelem
))
1515 (prog1 (current-column)
1520 (defsubst c-langelem-2nd-pos
(langelem)
1521 "Return the secondary position in LANGELEM, or nil if there is none.
1523 LANGELEM is typically a syntactic element on the \"new\" form as used
1524 in `c-syntactic-element'. It may also be a cons cell as passed in the
1525 first argument to lineup functions, but then the returned value always
1527 (car-safe (cdr-safe (cdr-safe langelem
))))
1529 (defsubst c-keep-region-active
()
1530 ;; Do whatever is necessary to keep the region active in XEmacs.
1531 ;; This is not needed for Emacs.
1532 (and (boundp 'zmacs-region-stays
)
1533 (setq zmacs-region-stays t
)))
1535 (put 'c-mode
'c-mode-prefix
"c-")
1536 (put 'c
++-mode
'c-mode-prefix
"c++-")
1537 (put 'objc-mode
'c-mode-prefix
"objc-")
1538 (put 'java-mode
'c-mode-prefix
"java-")
1539 (put 'idl-mode
'c-mode-prefix
"idl-")
1540 (put 'pike-mode
'c-mode-prefix
"pike-")
1541 (put 'awk-mode
'c-mode-prefix
"awk-")
1543 (defsubst c-mode-symbol
(suffix)
1544 "Prefix the current mode prefix (e.g. \"c-\") to SUFFIX and return
1545 the corresponding symbol."
1546 (or c-buffer-is-cc-mode
1547 (error "Not inside a CC Mode based mode"))
1548 (let ((mode-prefix (get c-buffer-is-cc-mode
'c-mode-prefix
)))
1550 (error "%S has no mode prefix known to `c-mode-symbol'"
1551 c-buffer-is-cc-mode
))
1552 (intern (concat mode-prefix suffix
))))
1554 (defsubst c-mode-var
(suffix)
1555 "Prefix the current mode prefix (e.g. \"c-\") to SUFFIX and return
1556 the value of the variable with that name."
1557 (symbol-value (c-mode-symbol suffix
)))
1559 (defsubst c-got-face-at
(pos faces
)
1560 "Return non-nil if position POS in the current buffer has any of the
1561 faces in the list FACES."
1562 (let ((pos-faces (get-text-property pos
'face
)))
1563 (if (consp pos-faces
)
1565 (while (and pos-faces
1566 (not (memq (car pos-faces
) faces
)))
1567 (setq pos-faces
(cdr pos-faces
)))
1569 (memq pos-faces faces
))))
1571 (defsubst c-face-name-p
(facename)
1572 ;; Return t if FACENAME is the name of a face. This method is
1573 ;; necessary since facep in XEmacs only returns t for the actual
1574 ;; face objects (while it's only their names that are used just
1575 ;; about anywhere else) without providing a predicate that tests
1577 (memq facename
(face-list)))
1579 (defun c-concat-separated (list separator
)
1580 "Like `concat' on LIST, but separate each element with SEPARATOR.
1581 Notably, null elements in LIST are ignored."
1582 (mapconcat 'identity
(delete nil
(append list nil
)) separator
))
1584 (defun c-make-keywords-re (adorn list
&optional mode
)
1585 "Make a regexp that matches all the strings the list.
1586 Duplicates and nil elements in the list are removed. The
1587 resulting regexp may contain zero or more submatch expressions.
1589 If ADORN is t there will be at least one submatch and the first
1590 surrounds the matched alternative, and the regexp will also not match
1591 a prefix of any identifier. Adorned regexps cannot be appended. The
1592 language variable `c-nonsymbol-key' is used to make the adornment.
1594 A value 'appendable for ADORN is like above, but all alternatives in
1595 the list that end with a word constituent char will have \\> appended
1596 instead, so that the regexp remains appendable. Note that this
1597 variant doesn't always guarantee that an identifier prefix isn't
1598 matched since the symbol constituent '_' is normally considered a
1599 nonword token by \\>.
1601 The optional MODE specifies the language to get `c-nonsymbol-key' from
1602 when it's needed. The default is the current language taken from
1603 `c-buffer-is-cc-mode'."
1605 (setq list
(delete nil
(delete-dups list
)))
1609 (if (eq adorn
'appendable
)
1610 ;; This is kludgy but it works: Search for a string that
1611 ;; doesn't occur in any word in LIST. Append it to all
1612 ;; the alternatives where we want to add \>. Run through
1613 ;; `regexp-opt' and then replace it with \>.
1614 (let ((unique "") pos
)
1616 (setq unique
(concat unique
"@")
1619 (if (string-match unique
(car pos
))
1620 (progn (setq found t
)
1623 (setq pos
(cdr pos
)))
1627 (if (string-match "\\w\\'" (car pos
))
1628 (setcar pos
(concat (car pos
) unique
)))
1629 (setq pos
(cdr pos
)))
1630 (setq re
(regexp-opt list
))
1632 (while (string-match unique re pos
)
1633 (setq pos
(+ (match-beginning 0) 2)
1634 re
(replace-match "\\>" t t re
))))
1636 (setq re
(regexp-opt list
)))
1638 ;; Emacs 20 and XEmacs (all versions so far) has a buggy
1639 ;; regexp-opt that doesn't always cope with strings containing
1640 ;; newlines. This kludge doesn't handle shy parens correctly
1641 ;; so we can't advice regexp-opt directly with it.
1644 (and (string-match "\n" (car list
)) ; To speed it up a little.
1645 (not (string-match (concat "\\`\\(" re
"\\)\\'")
1647 (setq fail-list
(cons (car list
) fail-list
)))
1648 (setq list
(cdr list
)))
1653 (if (eq adorn
'appendable
)
1655 (if (string-match "\\w\\'" str
)
1656 (concat (regexp-quote str
)
1658 (regexp-quote str
)))
1662 (> (length a
) (length b
))))
1665 ;; Add our own grouping parenthesis around re instead of
1666 ;; passing adorn to `regexp-opt', since in XEmacs it makes the
1667 ;; top level grouping "shy".
1668 (cond ((eq adorn
'appendable
)
1669 (concat "\\(" re
"\\)"))
1671 (concat "\\(" re
"\\)"
1673 (c-get-lang-constant 'c-nonsymbol-key nil mode
)
1678 ;; Produce a regexp that matches nothing.
1683 (put 'c-make-keywords-re
'lisp-indent-function
1)
1685 (defun c-make-bare-char-alt (chars &optional inverted
)
1686 "Make a character alternative string from the list of characters CHARS.
1687 The returned string is of the type that can be used with
1688 `skip-chars-forward' and `skip-chars-backward'. If INVERTED is
1689 non-nil, a caret is prepended to invert the set."
1690 ;; This function ought to be in the elisp core somewhere.
1691 (let ((str (if inverted
"^" "")) char char2
)
1692 (setq chars
(sort (append chars nil
) `<))
1694 (setq char
(pop chars
))
1695 (if (memq char
'(?
\\ ?^ ?-
))
1696 ;; Quoting necessary (this method only works in the skip
1698 (setq str
(format "%s\\%c" str char
))
1699 (setq str
(format "%s%c" str char
)))
1702 (while (and chars
(>= (1+ char2
) (car chars
)))
1703 (setq char2
(pop chars
)))
1704 (unless (= char char2
)
1705 (if (< (1+ char
) char2
)
1706 (setq str
(format "%s-%c" str char2
))
1707 (push char2 chars
))))
1710 ;; Leftovers from (X)Emacs 19 compatibility.
1711 (defalias 'c-regexp-opt
'regexp-opt
)
1712 (defalias 'c-regexp-opt-depth
'regexp-opt-depth
)
1715 ;; Figure out what features this Emacs has
1717 (cc-bytecomp-defvar open-paren-in-column-0-is-defun-start
)
1719 (defvar lookup-syntax-properties
) ;XEmacs.
1721 (defconst c-emacs-features
1724 (if (boundp 'infodock-version
)
1725 ;; I've no idea what this actually is, but it's legacy. /mast
1726 (setq list
(cons 'infodock list
)))
1728 ;; XEmacs uses 8-bit modify-syntax-entry flags.
1729 ;; Emacs uses a 1-bit flag. We will have to set up our
1730 ;; syntax tables differently to handle this.
1731 (let ((table (copy-syntax-table))
1733 (modify-syntax-entry ?a
". 12345678" table
)
1737 (setq entry
(aref table ?a
))
1738 ;; In Emacs, table entries are cons cells
1739 (if (consp entry
) (setq entry
(car entry
))))
1741 ((fboundp 'get-char-table
)
1742 (setq entry
(get-char-table ?a table
)))
1744 (t (error "CC Mode is incompatible with this version of Emacs")))
1745 (setq list
(cons (if (= (logand (lsh entry -
16) 255) 255)
1750 ;; Check whether beginning/end-of-defun call
1751 ;; beginning/end-of-defun-function nicely, passing through the
1752 ;; argument and respecting the return code.
1754 (bod-param 'foo
) (eod-param 'foo
)
1755 (beginning-of-defun-function
1756 (lambda (&optional arg
)
1757 (or (eq bod-param
'foo
) (setq bod-param
'bar
))
1758 (and (eq bod-param
'foo
)
1759 (setq bod-param arg
)
1761 (end-of-defun-function
1762 (lambda (&optional arg
)
1763 (and (eq eod-param
'foo
)
1764 (setq eod-param arg
)
1766 (if (save-excursion (and (beginning-of-defun 3) (eq bod-param
3)
1767 (not (beginning-of-defun))
1768 (end-of-defun 3) (eq eod-param
3)
1769 (not (end-of-defun))))
1770 (setq list
(cons 'argumentative-bod-function list
))))
1772 ;; Record whether the `category' text property works.
1773 (if c-use-category
(setq list
(cons 'category-properties list
)))
1775 (let ((buf (generate-new-buffer " test"))
1776 parse-sexp-lookup-properties
1777 parse-sexp-ignore-comments
1778 lookup-syntax-properties
) ; XEmacs
1779 (with-current-buffer buf
1780 (set-syntax-table (make-syntax-table))
1782 ;; For some reason we have to set some of these after the
1783 ;; buffer has been made current. (Specifically,
1784 ;; `parse-sexp-ignore-comments' in Emacs 21.)
1785 (setq parse-sexp-lookup-properties t
1786 parse-sexp-ignore-comments t
1787 lookup-syntax-properties t
)
1789 ;; Find out if the `syntax-table' text property works.
1790 (modify-syntax-entry ?
< ".")
1791 (modify-syntax-entry ?
> ".")
1793 (c-mark-<-as-paren
(point-min))
1794 (c-mark->-as-paren
(+ 3 (point-min)))
1795 (goto-char (point-min))
1797 (if (= (point) (+ 4 (point-min)))
1798 (setq list
(cons 'syntax-properties list
))
1800 "CC Mode is incompatible with this version of Emacs - "
1801 "support for the `syntax-table' text property "
1804 ;; Find out if "\\s!" (generic comment delimiters) work.
1806 (modify-syntax-entry ?x
"!")
1807 (if (string-match "\\s!" "x")
1808 (setq list
(cons 'gen-comment-delim list
))))
1810 ;; Find out if "\\s|" (generic string delimiters) work.
1812 (modify-syntax-entry ?x
"|")
1813 (if (string-match "\\s|" "x")
1814 (setq list
(cons 'gen-string-delim list
))))
1816 ;; See if POSIX char classes work.
1817 (when (and (string-match "[[:alpha:]]" "a")
1818 ;; All versions of Emacs 21 so far haven't fixed
1819 ;; char classes in `skip-chars-forward' and
1820 ;; `skip-chars-backward'.
1822 (delete-region (point-min) (point-max))
1824 (skip-chars-backward "[:alnum:]")
1826 (= (skip-chars-forward "[:alpha:]") 3))
1827 (setq list
(cons 'posix-char-classes list
)))
1829 ;; See if `open-paren-in-column-0-is-defun-start' exists and
1830 ;; isn't buggy (Emacs >= 21.4).
1831 (when (boundp 'open-paren-in-column-0-is-defun-start
)
1832 (let ((open-paren-in-column-0-is-defun-start nil
)
1833 (parse-sexp-ignore-comments t
))
1834 (delete-region (point-min) (point-max))
1835 (set-syntax-table (make-syntax-table))
1836 (modify-syntax-entry ?
\' "\"")
1838 ;; XEmacs. Afaik this is currently an Emacs-only
1839 ;; feature, but it's good to be prepared.
1841 (modify-syntax-entry ?
/ ". 1456")
1842 (modify-syntax-entry ?
* ". 23"))
1845 (modify-syntax-entry ?
/ ". 124b")
1846 (modify-syntax-entry ?
* ". 23")))
1847 (modify-syntax-entry ?
\n "> b")
1848 (insert "/* '\n () */")
1851 (setq list
(cons 'col-0-paren list
)))))
1853 (set-buffer-modified-p nil
))
1856 ;; See if `parse-partial-sexp' returns the eighth element.
1857 (if (c-safe (>= (length (save-excursion
1858 (parse-partial-sexp (point) (point))))
1860 (setq list
(cons 'pps-extended-state list
))
1862 "CC Mode is incompatible with this version of Emacs - "
1863 "`parse-partial-sexp' has to return at least 10 elements.")))
1865 ;;(message "c-emacs-features: %S" list)
1867 "A list of certain features in the (X)Emacs you are using.
1868 There are many flavors of Emacs out there, each with different
1869 features supporting those needed by CC Mode. The following values
1872 '8-bit 8 bit syntax entry flags (XEmacs style).
1873 '1-bit 1 bit syntax entry flags (Emacs style).
1874 'argumentative-bod-function beginning-of-defun and end-of-defun pass
1875 ARG through to beginning/end-of-defun-function.
1876 'syntax-properties It works to override the syntax for specific characters
1877 in the buffer with the 'syntax-table property. It's
1878 always set - CC Mode no longer works in emacsen without
1880 'category-properties Syntax routines can add a level of indirection to text
1881 properties using the 'category property.
1882 'gen-comment-delim Generic comment delimiters work
1883 (i.e. the syntax class `!').
1884 'gen-string-delim Generic string delimiters work
1885 (i.e. the syntax class `|').
1886 'pps-extended-state `parse-partial-sexp' returns a list with at least 10
1887 elements, i.e. it contains the position of the start of
1888 the last comment or string. It's always set - CC Mode
1889 no longer works in emacsen without this feature.
1890 'posix-char-classes The regexp engine understands POSIX character classes.
1891 'col-0-paren It's possible to turn off the ad-hoc rule that a paren
1892 in column zero is the start of a defun.
1893 'infodock This is Infodock (based on XEmacs).
1895 '8-bit and '1-bit are mutually exclusive.")
1898 ;;; Some helper constants.
1900 ;; If the regexp engine supports POSIX char classes then we can use
1901 ;; them to handle extended charsets correctly.
1902 (if (memq 'posix-char-classes c-emacs-features
)
1904 (defconst c-alpha
"[:alpha:]")
1905 (defconst c-alnum
"[:alnum:]")
1906 (defconst c-digit
"[:digit:]")
1907 (defconst c-upper
"[:upper:]")
1908 (defconst c-lower
"[:lower:]"))
1909 (defconst c-alpha
"a-zA-Z")
1910 (defconst c-alnum
"a-zA-Z0-9")
1911 (defconst c-digit
"0-9")
1912 (defconst c-upper
"A-Z")
1913 (defconst c-lower
"a-z"))
1916 ;;; System for handling language dependent constants.
1918 ;; This is used to set various language dependent data in a flexible
1919 ;; way: Language constants can be built from the values of other
1920 ;; language constants, also those for other languages. They can also
1921 ;; process the values of other language constants uniformly across all
1922 ;; the languages. E.g. one language constant can list all the type
1923 ;; keywords in each language, and another can build a regexp for each
1924 ;; language from those lists without code duplication.
1926 ;; Language constants are defined with `c-lang-defconst', and their
1927 ;; value forms (referred to as source definitions) are evaluated only
1928 ;; on demand when requested for a particular language with
1929 ;; `c-lang-const'. It's therefore possible to refer to the values of
1930 ;; constants defined later in the file, or in another file, just as
1931 ;; long as all the relevant `c-lang-defconst' have been loaded when
1932 ;; `c-lang-const' is actually evaluated from somewhere else.
1934 ;; `c-lang-const' forms are also evaluated at compile time and
1935 ;; replaced with the values they produce. Thus there's no overhead
1936 ;; for this system when compiled code is used - only the values
1937 ;; actually used in the code are present, and the file(s) containing
1938 ;; the `c-lang-defconst' forms don't need to be loaded at all then.
1939 ;; There are however safeguards to make sure that they can be loaded
1940 ;; to get the source definitions for the values if there's a mismatch
1941 ;; in compiled versions, or if `c-lang-const' is used uncompiled.
1943 ;; Note that the source definitions in a `c-lang-defconst' form are
1944 ;; compiled into the .elc file where it stands; there's no need to
1945 ;; load the source file to get it.
1947 ;; See cc-langs.el for more details about how this system is deployed
1948 ;; in CC Mode, and how the associated language variable system
1949 ;; (`c-lang-defvar') works. That file also contains a lot of
1952 (defun c-add-language (mode base-mode
)
1953 "Declare a new language in the language dependent variable system.
1954 This is intended to be used by modes that inherit CC Mode to add new
1955 languages. It should be used at the top level before any calls to
1956 `c-lang-defconst'. MODE is the mode name symbol for the new language,
1957 and BASE-MODE is the mode name symbol for the language in CC Mode that
1958 is to be the template for the new mode.
1960 The exact effect of BASE-MODE is to make all language constants that
1961 haven't got a setting in the new language fall back to their values in
1962 BASE-MODE. It does not have any effect outside the language constant
1964 (unless (string-match "\\`\\(.*-\\)mode\\'" (symbol-name mode
))
1965 (error "The mode name symbol `%s' must end with \"-mode\"" mode
))
1966 (put mode
'c-mode-prefix
(match-string 1 (symbol-name mode
)))
1967 (unless (get base-mode
'c-mode-prefix
)
1968 (error "Unknown base mode `%s'" base-mode
))
1969 (put mode
'c-fallback-mode base-mode
))
1971 (defvar c-lang-constants
(make-vector 151 0))
1972 ;; Obarray used as a cache to keep track of the language constants.
1973 ;; The constants stored are those defined by `c-lang-defconst' and the values
1974 ;; computed by `c-lang-const'. It's mostly used at compile time but it's not
1975 ;; stored in compiled files.
1977 ;; The obarray contains all the language constants as symbols. The
1978 ;; value cells hold the evaluated values as alists where each car is
1979 ;; the mode name symbol and the corresponding cdr is the evaluated
1980 ;; value in that mode. The property lists hold the source definitions
1981 ;; and other miscellaneous data. The obarray might also contain
1982 ;; various other symbols, but those don't have any variable bindings.
1984 (defvar c-lang-const-expansion nil
)
1986 ;; Ugly hack to pull in the definition of `cc-bytecomp-compiling-or-loading`
1987 ;; from cc-bytecomp to make it available at loadtime. This is the same
1988 ;; mechanism used in cc-mode.el for `c-populate-syntax-table'.
1989 (defalias 'cc-bytecomp-compiling-or-loading
1990 (cc-eval-when-compile
1991 (let ((f (symbol-function 'cc-bytecomp-compiling-or-loading
)))
1992 (if (byte-code-function-p f
) f
(byte-compile f
)))))
1994 (defsubst c-get-current-file
()
1995 ;; Return the base name of the current file.
1996 (let* ((c-or-l (cc-bytecomp-compiling-or-loading))
1999 ((eq c-or-l
'loading
) load-file-name
)
2000 ((eq c-or-l
'compiling
) byte-compile-dest-file
)
2001 ((null c-or-l
) (buffer-file-name)))))
2003 (file-name-sans-extension
2004 (file-name-nondirectory file
)))))
2006 (defmacro c-lang-defconst-eval-immediately
(form)
2007 "Can be used inside a VAL in `c-lang-defconst' to evaluate FORM
2008 immediately, i.e. at the same time as the `c-lang-defconst' form
2009 itself is evaluated."
2010 ;; Evaluate at macro expansion time, i.e. in the
2011 ;; `c--macroexpand-all' inside `c-lang-defconst'.
2014 (defmacro c-lang-defconst
(name &rest args
)
2015 "Set the language specific values of the language constant NAME.
2016 The second argument can optionally be a docstring. The rest of the
2017 arguments are one or more repetitions of LANG VAL where LANG specifies
2018 the language(s) that VAL applies to. LANG is the name of the
2019 language, i.e. the mode name without the \"-mode\" suffix, or a list
2020 of such language names, or `t' for all languages. VAL is a form to
2021 evaluate to get the value.
2023 If LANG isn't `t' or one of the core languages in CC Mode, it must
2024 have been declared with `c-add-language'.
2026 Neither NAME, LANG nor VAL are evaluated directly - they should not be
2027 quoted. `c-lang-defconst-eval-immediately' can however be used inside
2028 VAL to evaluate parts of it directly.
2030 When VAL is evaluated for some language, that language is temporarily
2031 made current so that `c-lang-const' without an explicit language can
2032 be used inside VAL to refer to the value of a language constant in the
2033 same language. That is particularly useful if LANG is `t'.
2035 VAL is not evaluated right away but rather when the value is requested
2036 with `c-lang-const'. Thus it's possible to use `c-lang-const' inside
2037 VAL to refer to language constants that haven't been defined yet.
2038 However, if the definition of a language constant is in another file
2039 then that file must be loaded \(at compile time) before it's safe to
2040 reference the constant.
2042 The assignments in ARGS are processed in sequence like `setq', so
2043 \(c-lang-const NAME) may be used inside a VAL to refer to the last
2044 assigned value to this language constant, or a value that it has
2045 gotten in another earlier loaded file.
2047 To work well with repeated loads and interactive reevaluation, only
2048 one `c-lang-defconst' for each NAME is permitted per file. If there
2049 already is one it will be completely replaced; the value in the
2050 earlier definition will not affect `c-lang-const' on the same
2051 constant. A file is identified by its base name."
2053 (let* ((sym (intern (symbol-name name
) c-lang-constants
))
2054 ;; Make `c-lang-const' expand to a straightforward call to
2055 ;; `c-get-lang-constant' in `c--macroexpand-all' below.
2057 ;; (The default behavior, i.e. to expand to a call inside
2058 ;; `eval-when-compile' should be equivalent, since that macro
2059 ;; should only expand to its content if it's used inside a
2060 ;; form that's already evaluated at compile time. It's
2061 ;; however necessary to use our cover macro
2062 ;; `cc-eval-when-compile' due to bugs in `eval-when-compile',
2063 ;; and it expands to a bulkier form that in this case only is
2064 ;; unnecessary garbage that we don't want to store in the
2065 ;; language constant source definitions.)
2066 (c-lang-const-expansion 'call
)
2067 (c-langs-are-parametric t
)
2069 (or (c-get-current-file)
2070 (error "`c-lang-defconst' can only be used in a file"))))
2075 (error "Not a symbol: %S" name
))
2077 (when (stringp (car-safe args
))
2078 ;; The docstring is hardly used anywhere since there's no normal
2079 ;; symbol to attach it to. It's primarily for getting the right
2080 ;; format in the source.
2081 (put sym
'variable-documentation
(car args
))
2082 (setq args
(cdr args
)))
2085 (error "No assignments in `c-lang-defconst' for %S" name
))
2087 ;; Rework ARGS to an association list to make it easier to handle.
2088 ;; It's reversed at the same time to make it easier to implement
2089 ;; the demand-driven (i.e. reversed) evaluation in `c-lang-const'.
2091 (let ((assigned-mode
2092 (cond ((eq (car args
) t
) t
)
2093 ((symbolp (car args
))
2094 (list (intern (concat (symbol-name (car args
))
2097 (mapcar (lambda (lang)
2099 (error "Not a list of symbols: %S"
2101 (intern (concat (symbol-name lang
)
2104 (t (error "Not a symbol or a list of symbols: %S"
2109 (error "No value for %S" (car args
)))
2110 (setq args
(cdr args
)
2113 ;; Emacs has a weird bug where it seems to fail to read
2114 ;; backquote lists from byte compiled files correctly (,@
2115 ;; forms, to be specific), so make sure the bindings in the
2116 ;; expansion below don't contain any backquote stuff.
2117 ;; (XEmacs handles it correctly and doesn't need this for that
2118 ;; reason, but we also use this expansion handle
2119 ;; `c-lang-defconst-eval-immediately' and to register
2120 ;; dependencies on the `c-lang-const's in VAL.)
2121 (setq val
(c--macroexpand-all val
))
2123 (setq bindings
`(cons (cons ',assigned-mode
(lambda () ,val
)) ,bindings
)
2126 ;; Compile in the other files that have provided source
2127 ;; definitions for this symbol, to make sure the order in the
2128 ;; `source' property is correct even when files are loaded out of
2130 (setq pre-files
(mapcar 'car
(get sym
'source
)))
2131 (if (memq file pre-files
)
2132 ;; This can happen when the source file (e.g. cc-langs.el) is first
2133 ;; loaded as source, setting a 'source property entry, and then itself
2135 (setq pre-files
(cdr (memq file pre-files
))))
2136 ;; Reverse to get the right load order.
2137 (setq pre-files
(nreverse pre-files
))
2140 (c-define-lang-constant ',name
,bindings
2141 ,@(and pre-files
`(',pre-files
))))))
2143 (put 'c-lang-defconst
'lisp-indent-function
1)
2144 ;(eval-after-load "edebug" ; 2006-07-09: def-edebug-spec is now in subr.el.
2146 (def-edebug-spec c-lang-defconst
2147 (&define name
[&optional stringp
] [&rest sexp def-form
]))
2149 (defun c-define-lang-constant (name bindings
&optional pre-files
)
2150 ;; Used by `c-lang-defconst'.
2152 (let* ((sym (intern (symbol-name name
) c-lang-constants
))
2153 (source (get sym
'source
))
2155 (or (c-get-current-file)
2156 (error "`c-lang-defconst' must be used in a file"))))
2157 (elem (assq file source
)))
2159 ;;(when (cdr-safe elem)
2160 ;; (message "Language constant %s redefined in %S" name file))
2162 ;; Note that the order in the source alist is relevant. Like how
2163 ;; `c-lang-defconst' reverses the bindings, this reverses the
2164 ;; order between files so that the last to evaluate comes first.
2167 (unless (assq (car pre-files
) source
)
2168 (setq source
(cons (list (car pre-files
)) source
)))
2169 (setq pre-files
(cdr pre-files
)))
2170 (put sym
'source
(cons (setq elem
(list file
)) source
)))
2172 (setcdr elem bindings
)
2174 ;; Bind the symbol as a variable, or clear any earlier evaluated
2178 ;; Clear the evaluated values that depend on this source.
2179 (let ((agenda (get sym
'dependents
))
2180 (visited (make-vector 101 0))
2183 (setq sym
(car agenda
)
2184 agenda
(cdr agenda
))
2185 (intern (symbol-name sym
) visited
)
2187 (setq ptr
(get sym
'dependents
))
2191 (unless (intern-soft (symbol-name sym
) visited
)
2192 (setq agenda
(cons sym agenda
))))))
2196 (defmacro c-lang-const
(name &optional lang
)
2197 "Get the mode specific value of the language constant NAME in language LANG.
2198 LANG is the name of the language, i.e. the mode name without the
2199 \"-mode\" suffix. If used inside `c-lang-defconst' or
2200 `c-lang-defvar', LANG may be left out to refer to the current
2201 language. NAME and LANG are not evaluated so they should not be
2205 (error "Not a symbol: %S" name
))
2207 (error "Not a symbol: %S" lang
))
2209 (let ((sym (intern (symbol-name name
) c-lang-constants
))
2210 (mode (when lang
(intern (concat (symbol-name lang
) "-mode")))))
2212 (or (get mode
'c-mode-prefix
) (null mode
)
2213 (error "Unknown language %S: no `c-mode-prefix' property"
2216 (if (eq c-lang-const-expansion
'immediate
)
2217 ;; No need to find out the source file(s) when we evaluate
2218 ;; immediately since all the info is already there in the
2219 ;; `source' property.
2220 `',(c-get-lang-constant name nil mode
)
2223 (let ((file (c-get-current-file)))
2224 (if file
(setq file
(intern file
)))
2225 ;; Get the source file(s) that must be loaded to get the value
2226 ;; of the constant. If the symbol isn't defined yet we assume
2227 ;; that its definition will come later in this file, and thus
2228 ;; are no file dependencies needed.
2230 ;; Reverse to get the right load order.
2232 (mapcar (lambda (elem)
2233 (if (eq file
(car elem
))
2234 nil
; Exclude our own file.
2236 (get sym
'source
))))))
2237 ;; Make some effort to do a compact call to
2238 ;; `c-get-lang-constant' since it will be compiled in.
2239 (args (and mode
`(',mode
))))
2241 (if (or source-files args
)
2242 (push (and source-files
`',source-files
) args
))
2244 (if (or (eq c-lang-const-expansion
'call
)
2245 (and (not c-lang-const-expansion
)
2247 (not (cc-bytecomp-is-compiling)))
2248 ;; Either a straight call is requested in the context, or
2249 ;; we're in an "uncontrolled" context and got no language,
2250 ;; or we're not being byte compiled so the compile time
2251 ;; stuff below is unnecessary.
2252 `(c-get-lang-constant ',name
,@args
)
2254 ;; Being compiled. If the loading and compiling version is
2255 ;; the same we use a value that is evaluated at compile time,
2256 ;; otherwise it's evaluated at runtime.
2257 `(if (eq c-version-sym
',c-version-sym
)
2258 (cc-eval-when-compile
2259 (c-get-lang-constant ',name
,@args
))
2260 (c-get-lang-constant ',name
,@args
)))))))
2262 (defvar c-lang-constants-under-evaluation nil
2263 "Alist of constants in the process of being evaluated.
2264 The `cdr' of each entry indicates how far we've looked in the list
2265 of definitions, so that the def for var FOO in c-mode can be defined in
2266 terms of the def for that same var FOO (which will then rely on the
2267 fallback definition for all modes, to break the cycle).")
2269 (defconst c-lang--novalue
"novalue")
2271 (defun c-get-lang-constant (name &optional source-files mode
)
2272 ;; Used by `c-lang-const'.
2275 (setq mode c-buffer-is-cc-mode
)
2276 (error "No current language"))
2278 (let* ((sym (intern (symbol-name name
) c-lang-constants
))
2279 (source (get sym
'source
))
2281 (eval-in-sym (and c-lang-constants-under-evaluation
2282 (caar c-lang-constants-under-evaluation
))))
2284 ;; Record the dependencies between this symbol and the one we're
2285 ;; being evaluated in.
2287 (or (memq eval-in-sym
(get sym
'dependents
))
2288 (put sym
'dependents
(cons eval-in-sym
(get sym
'dependents
)))))
2290 ;; Make sure the source files have entries on the `source'
2291 ;; property so that loading will take place when necessary.
2293 (unless (assq (car source-files
) source
)
2295 (setq source
(cons (list (car source-files
)) source
)))
2296 ;; Might pull in more definitions which affect the value. The
2297 ;; clearing of dependent values etc is done when the
2298 ;; definition is encountered during the load; this is just to
2299 ;; jump past the check for a cached value below.
2301 (setq source-files
(cdr source-files
)))
2303 (if (and (boundp sym
)
2304 (setq elem
(assq mode
(symbol-value sym
))))
2307 ;; Check if an evaluation of this symbol is already underway.
2308 ;; In that case we just continue with the "assignment" before
2309 ;; the one currently being evaluated, thereby creating the
2310 ;; illusion if a `setq'-like sequence of assignments.
2311 (let* ((c-buffer-is-cc-mode mode
)
2313 (or (assq sym c-lang-constants-under-evaluation
)
2314 (cons sym
(vector source nil
))))
2315 ;; Append `c-lang-constants-under-evaluation' even if an
2316 ;; earlier entry is found. It's only necessary to get
2317 ;; the recording of dependencies above correct.
2318 (c-lang-constants-under-evaluation
2319 (cons source-pos c-lang-constants-under-evaluation
))
2320 (fallback (get mode
'c-fallback-mode
))
2322 ;; Make sure the recursion limits aren't very low
2323 ;; since the `c-lang-const' dependencies can go deep.
2324 (max-specpdl-size (max max-specpdl-size
3000))
2325 (max-lisp-eval-depth (max max-lisp-eval-depth
1000)))
2328 (let ((backup-source-pos (copy-sequence (cdr source-pos
))))
2330 ;; First try the original mode but don't accept an
2331 ;; entry matching all languages since the fallback
2332 ;; mode might have an explicit entry before that.
2333 (eq (setq value
(c-find-assignment-for-mode
2334 (cdr source-pos
) mode nil name
))
2336 ;; Try again with the fallback mode from the
2337 ;; original position. Note that
2338 ;; `c-buffer-is-cc-mode' still is the real mode if
2339 ;; language parameterization takes place.
2340 (eq (setq value
(c-find-assignment-for-mode
2341 (setcdr source-pos backup-source-pos
)
2344 ;; A simple lookup with no fallback mode.
2345 (eq (setq value
(c-find-assignment-for-mode
2346 (cdr source-pos
) mode t name
))
2349 "`%s' got no (prior) value in %S (might be a cyclic reference)"
2353 (setq value
(funcall value
))
2355 ;; Print a message to aid in locating the error. We don't
2356 ;; print the error itself since that will be done later by
2357 ;; some caller higher up.
2358 (message "Eval error in the `c-lang-defconst' for `%S' in %s:"
2361 (signal (car err
) (cdr err
))))
2363 (set sym
(cons (cons mode value
) (symbol-value sym
)))
2366 (defun c-find-assignment-for-mode (source-pos mode match-any-lang _name
)
2367 ;; Find the first assignment entry that applies to MODE at or after
2368 ;; SOURCE-POS. If MATCH-ANY-LANG is non-nil, entries with `t' as
2369 ;; the language list are considered to match, otherwise they don't.
2370 ;; On return SOURCE-POS is updated to point to the next assignment
2371 ;; after the returned one. If no assignment is found,
2372 ;; `c-lang--novalue' is returned as a magic value.
2374 ;; SOURCE-POS is a vector that points out a specific assignment in
2375 ;; the double alist that's used in the `source' property. The first
2376 ;; element is the position in the top alist which is indexed with
2377 ;; the source files, and the second element is the position in the
2378 ;; nested bindings alist.
2380 ;; NAME is only used for error messages.
2383 (let ((file-entry (elt source-pos
0))
2384 (assignment-entry (elt source-pos
1))
2387 (while (if assignment-entry
2389 ;; Handled the last assignment from one file, begin on the
2390 ;; next. Due to the check in `c-lang-defconst', we know
2391 ;; there's at least one.
2394 (unless (aset source-pos
1
2395 (setq assignment-entry
(cdar file-entry
)))
2396 ;; The file containing the source definitions has not
2398 (let ((file (symbol-name (caar file-entry
)))
2399 (c-lang-constants-under-evaluation nil
))
2400 ;;(message (concat "Loading %s to get the source "
2401 ;; "value for language constant %s")
2405 (unless (setq assignment-entry
(cdar file-entry
))
2406 ;; The load didn't fill in the source for the
2407 ;; constant as expected. The situation is
2408 ;; probably that a derived mode was written for
2409 ;; and compiled with another version of CC Mode,
2410 ;; and the requested constant isn't in the
2411 ;; currently loaded one. Put in a dummy
2412 ;; assignment that matches no language.
2413 (setcdr (car file-entry
)
2414 (setq assignment-entry
(list (list nil
))))))
2416 (aset source-pos
0 (setq file-entry
(cdr file-entry
)))
2419 (setq assignment
(car assignment-entry
))
2421 (setq assignment-entry
(cdr assignment-entry
)))
2423 (when (if (listp (car assignment
))
2424 (memq mode
(car assignment
))
2426 (throw 'found
(cdr assignment
))))
2430 (defun c-lang-major-mode-is (mode)
2431 ;; `c-major-mode-is' expands to a call to this function inside
2432 ;; `c-lang-defconst'. Here we also match the mode(s) against any
2433 ;; fallback modes for the one in `c-buffer-is-cc-mode', so that
2434 ;; e.g. (c-major-mode-is 'c++-mode) is true in a derived language
2435 ;; that has c++-mode as base mode.
2436 (unless (listp mode
)
2437 (setq mode
(list mode
)))
2438 (let (match (buf-mode c-buffer-is-cc-mode
))
2439 (while (if (memq buf-mode mode
)
2443 (setq buf-mode
(get buf-mode
'c-fallback-mode
))))
2447 (cc-provide 'cc-defs
)
2449 ;;; Local Variables:
2450 ;;; indent-tabs-mode: t
2453 ;;; cc-defs.el ends here