(edt-default-emulation-setup): Share global-buffers-menu-map with the
[emacs.git] / lisp / progmodes / cc-defs.el
blob588e7c3428e9ce1eca52388a790a65ca1a7fb7b0
1 ;;; cc-defs.el --- compile time definitions for CC Mode
3 ;; Copyright (C) 1985, 1987, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
4 ;; 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008
5 ;; Free Software Foundation, Inc.
7 ;; Authors: 2003- Alan Mackenzie
8 ;; 1998- Martin Stjernholm
9 ;; 1992-1999 Barry A. Warsaw
10 ;; 1987 Dave Detlefs and Stewart Clamen
11 ;; 1985 Richard M. Stallman
12 ;; Maintainer: bug-cc-mode@gnu.org
13 ;; Created: 22-Apr-1997 (split from cc-mode.el)
14 ;; Version: See cc-mode.el
15 ;; Keywords: c languages oop
17 ;; This file is part of GNU Emacs.
19 ;; GNU Emacs is free software; you can redistribute it and/or modify
20 ;; it under the terms of the GNU General Public License as published by
21 ;; the Free Software Foundation; either version 3, or (at your option)
22 ;; any later version.
24 ;; GNU Emacs is distributed in the hope that it will be useful,
25 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
26 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27 ;; GNU General Public License for more details.
29 ;; You should have received a copy of the GNU General Public License
30 ;; along with this program; see the file COPYING. If not, write to
31 ;; the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
32 ;; Boston, MA 02110-1301, USA.
34 ;;; Commentary:
36 ;; This file contains macros, defsubsts, and various other things that
37 ;; must be loaded early both during compilation and at runtime.
39 ;;; Code:
41 (eval-when-compile
42 (let ((load-path
43 (if (and (boundp 'byte-compile-dest-file)
44 (stringp byte-compile-dest-file))
45 (cons (file-name-directory byte-compile-dest-file) load-path)
46 load-path)))
47 (load "cc-bytecomp" nil t)))
49 (eval-when-compile (require 'cl)) ; was (cc-external-require 'cl). ACM 2005/11/29.
50 (cc-external-require 'regexp-opt)
52 ;; Silence the compiler.
53 (cc-bytecomp-defvar c-enable-xemacs-performance-kludge-p) ; In cc-vars.el
54 (cc-bytecomp-defun buffer-syntactic-context-depth) ; XEmacs
55 (cc-bytecomp-defun region-active-p) ; XEmacs
56 (cc-bytecomp-defvar zmacs-region-stays) ; XEmacs
57 (cc-bytecomp-defvar zmacs-regions) ; XEmacs
58 (cc-bytecomp-defvar mark-active) ; Emacs
59 (cc-bytecomp-defvar deactivate-mark) ; Emacs
60 (cc-bytecomp-defvar inhibit-point-motion-hooks) ; Emacs
61 (cc-bytecomp-defvar parse-sexp-lookup-properties) ; Emacs
62 (cc-bytecomp-defvar text-property-default-nonsticky) ; Emacs 21
63 (cc-bytecomp-defvar lookup-syntax-properties) ; XEmacs
64 (cc-bytecomp-defun string-to-syntax) ; Emacs 21
67 ;; cc-fix.el contains compatibility macros that should be used if
68 ;; needed.
69 (eval-and-compile
70 (if (or (/= (regexp-opt-depth "\\(\\(\\)\\)") 2)
71 (not (fboundp 'push)))
72 (cc-load "cc-fix")))
74 ; (eval-after-load "font-lock" ; 2006-07-09. font-lock is now preloaded
75 ; '
76 (if (and (not (featurep 'cc-fix)) ; only load the file once.
77 (featurep 'xemacs) ; There is now (2005/12) code in GNU Emacs CVS
78 ; to make the call to f-l-c-k throw an error.
79 (let (font-lock-keywords)
80 (font-lock-compile-keywords '("\\<\\>"))
81 font-lock-keywords)) ; did the previous call foul this up?
82 (load "cc-fix")) ;)
84 ;; The above takes care of the delayed loading, but this is necessary
85 ;; to ensure correct byte compilation.
86 (eval-when-compile
87 (if (and (not (featurep 'cc-fix))
88 (featurep 'xemacs)
89 (progn
90 (require 'font-lock)
91 (let (font-lock-keywords)
92 (font-lock-compile-keywords '("\\<\\>"))
93 font-lock-keywords)))
94 (cc-load "cc-fix")))
97 ;;; Variables also used at compile time.
99 (defconst c-version "5.31.5"
100 "CC Mode version number.")
102 (defconst c-version-sym (intern c-version))
103 ;; A little more compact and faster in comparisons.
105 (defvar c-buffer-is-cc-mode nil
106 "Non-nil for all buffers with a major mode derived from CC Mode.
107 Otherwise, this variable is nil. I.e. this variable is non-nil for
108 `c-mode', `c++-mode', `objc-mode', `java-mode', `idl-mode',
109 `pike-mode', `awk-mode', and any other non-CC Mode mode that calls
110 `c-initialize-cc-mode'. The value is the mode symbol itself
111 \(i.e. `c-mode' etc) of the original CC Mode mode, or just t if it's
112 not known.")
113 (make-variable-buffer-local 'c-buffer-is-cc-mode)
115 ;; Have to make `c-buffer-is-cc-mode' permanently local so that it
116 ;; survives the initialization of the derived mode.
117 (put 'c-buffer-is-cc-mode 'permanent-local t)
120 ;; The following is used below during compilation.
121 (eval-and-compile
122 (defvar c-inside-eval-when-compile nil)
124 (defmacro cc-eval-when-compile (&rest body)
125 "Like `progn', but evaluates the body at compile time.
126 The result of the body appears to the compiler as a quoted constant.
128 This variant works around bugs in `eval-when-compile' in various
129 \(X)Emacs versions. See cc-defs.el for details."
131 (if c-inside-eval-when-compile
132 ;; XEmacs 21.4.6 has a bug in `eval-when-compile' in that it
133 ;; evaluates its body at macro expansion time if it's nested
134 ;; inside another `eval-when-compile'. So we use a dynamically
135 ;; bound variable to avoid nesting them.
136 `(progn ,@body)
138 `(eval-when-compile
139 ;; In all (X)Emacsen so far, `eval-when-compile' byte compiles
140 ;; its contents before evaluating it. That can cause forms to
141 ;; be compiled in situations they aren't intended to be
142 ;; compiled.
144 ;; Example: It's not possible to defsubst a primitive, e.g. the
145 ;; following will produce an error (in any emacs flavor), since
146 ;; `nthcdr' is a primitive function that's handled specially by
147 ;; the byte compiler and thus can't be redefined:
149 ;; (defsubst nthcdr (val) val)
151 ;; `defsubst', like `defmacro', needs to be evaluated at
152 ;; compile time, so this will produce an error during byte
153 ;; compilation.
155 ;; CC Mode occasionally needs to do things like this for
156 ;; cross-emacs compatibility. It therefore uses the following
157 ;; to conditionally do a `defsubst':
159 ;; (eval-when-compile
160 ;; (if (not (fboundp 'foo))
161 ;; (defsubst foo ...)))
163 ;; But `eval-when-compile' byte compiles its contents and
164 ;; _then_ evaluates it (in all current emacs versions, up to
165 ;; and including Emacs 20.6 and XEmacs 21.1 as of this
166 ;; writing). So this will still produce an error, since the
167 ;; byte compiler will get to the defsubst anyway. That's
168 ;; arguably a bug because the point with `eval-when-compile' is
169 ;; that it should evaluate rather than compile its contents.
171 ;; We get around it by expanding the body to a quoted
172 ;; constant that we eval. That otoh introduce a problem in
173 ;; that a returned lambda expression doesn't get byte
174 ;; compiled (even if `function' is used).
175 (eval '(let ((c-inside-eval-when-compile t)) ,@body)))))
177 (put 'cc-eval-when-compile 'lisp-indent-hook 0))
180 ;;; Macros.
182 (defmacro c-point (position &optional point)
183 "Return the value of certain commonly referenced POSITIONs relative to POINT.
184 The current point is used if POINT isn't specified. POSITION can be
185 one of the following symbols:
187 `bol' -- beginning of line
188 `eol' -- end of line
189 `bod' -- beginning of defun
190 `eod' -- end of defun
191 `boi' -- beginning of indentation
192 `ionl' -- indentation of next line
193 `iopl' -- indentation of previous line
194 `bonl' -- beginning of next line
195 `eonl' -- end of next line
196 `bopl' -- beginning of previous line
197 `eopl' -- end of previous line
198 `bosws' -- beginning of syntactic whitespace
199 `eosws' -- end of syntactic whitespace
201 If the referenced position doesn't exist, the closest accessible point
202 to it is returned. This function does not modify the point or the mark."
204 (if (eq (car-safe position) 'quote)
205 (let ((position (eval position)))
206 (cond
208 ((eq position 'bol)
209 (if (and (cc-bytecomp-fboundp 'line-beginning-position) (not point))
210 `(line-beginning-position)
211 `(save-excursion
212 ,@(if point `((goto-char ,point)))
213 (beginning-of-line)
214 (point))))
216 ((eq position 'eol)
217 (if (and (cc-bytecomp-fboundp 'line-end-position) (not point))
218 `(line-end-position)
219 `(save-excursion
220 ,@(if point `((goto-char ,point)))
221 (end-of-line)
222 (point))))
224 ((eq position 'boi)
225 `(save-excursion
226 ,@(if point `((goto-char ,point)))
227 (back-to-indentation)
228 (point)))
230 ((eq position 'bod)
231 `(save-excursion
232 ,@(if point `((goto-char ,point)))
233 (c-beginning-of-defun-1)
234 (point)))
236 ((eq position 'eod)
237 `(save-excursion
238 ,@(if point `((goto-char ,point)))
239 (c-end-of-defun-1)
240 (point)))
242 ((eq position 'bopl)
243 (if (and (cc-bytecomp-fboundp 'line-beginning-position) (not point))
244 `(line-beginning-position 0)
245 `(save-excursion
246 ,@(if point `((goto-char ,point)))
247 (forward-line -1)
248 (point))))
250 ((eq position 'bonl)
251 (if (and (cc-bytecomp-fboundp 'line-beginning-position) (not point))
252 `(line-beginning-position 2)
253 `(save-excursion
254 ,@(if point `((goto-char ,point)))
255 (forward-line 1)
256 (point))))
258 ((eq position 'eopl)
259 (if (and (cc-bytecomp-fboundp 'line-end-position) (not point))
260 `(line-end-position 0)
261 `(save-excursion
262 ,@(if point `((goto-char ,point)))
263 (beginning-of-line)
264 (or (bobp) (backward-char))
265 (point))))
267 ((eq position 'eonl)
268 (if (and (cc-bytecomp-fboundp 'line-end-position) (not point))
269 `(line-end-position 2)
270 `(save-excursion
271 ,@(if point `((goto-char ,point)))
272 (forward-line 1)
273 (end-of-line)
274 (point))))
276 ((eq position 'iopl)
277 `(save-excursion
278 ,@(if point `((goto-char ,point)))
279 (forward-line -1)
280 (back-to-indentation)
281 (point)))
283 ((eq position 'ionl)
284 `(save-excursion
285 ,@(if point `((goto-char ,point)))
286 (forward-line 1)
287 (back-to-indentation)
288 (point)))
290 ((eq position 'bosws)
291 `(save-excursion
292 ,@(if point `((goto-char ,point)))
293 (c-backward-syntactic-ws)
294 (point)))
296 ((eq position 'eosws)
297 `(save-excursion
298 ,@(if point `((goto-char ,point)))
299 (c-forward-syntactic-ws)
300 (point)))
302 (t (error "Unknown buffer position requested: %s" position))))
304 ;; The bulk of this should perhaps be in a function to avoid large
305 ;; expansions, but this case is not used anywhere in CC Mode (and
306 ;; probably not anywhere else either) so we only have it to be on
307 ;; the safe side.
308 (message "Warning: c-point long expansion")
309 `(save-excursion
310 ,@(if point `((goto-char ,point)))
311 (let ((position ,position))
312 (cond
313 ((eq position 'bol) (beginning-of-line))
314 ((eq position 'eol) (end-of-line))
315 ((eq position 'boi) (back-to-indentation))
316 ((eq position 'bod) (c-beginning-of-defun-1))
317 ((eq position 'eod) (c-end-of-defun-1))
318 ((eq position 'bopl) (forward-line -1))
319 ((eq position 'bonl) (forward-line 1))
320 ((eq position 'eopl) (progn
321 (beginning-of-line)
322 (or (bobp) (backward-char))))
323 ((eq position 'eonl) (progn
324 (forward-line 1)
325 (end-of-line)))
326 ((eq position 'iopl) (progn
327 (forward-line -1)
328 (back-to-indentation)))
329 ((eq position 'ionl) (progn
330 (forward-line 1)
331 (back-to-indentation)))
332 ((eq position 'bosws) (c-backward-syntactic-ws))
333 ((eq position 'eosws) (c-forward-syntactic-ws))
334 (t (error "Unknown buffer position requested: %s" position))))
335 (point))))
337 (defmacro c-region-is-active-p ()
338 ;; Return t when the region is active. The determination of region
339 ;; activeness is different in both Emacs and XEmacs.
340 (if (cc-bytecomp-fboundp 'region-active-p)
341 ;; XEmacs.
342 '(region-active-p)
343 ;; Emacs.
344 'mark-active))
346 (defmacro c-set-region-active (activate)
347 ;; Activate the region if ACTIVE is non-nil, deactivate it
348 ;; otherwise. Covers the differences between Emacs and XEmacs.
349 (if (cc-bytecomp-fboundp 'zmacs-activate-region)
350 ;; XEmacs.
351 `(if ,activate
352 (zmacs-activate-region)
353 (zmacs-deactivate-region))
354 ;; Emacs.
355 `(setq mark-active ,activate)))
357 (defmacro c-delete-and-extract-region (start end)
358 "Delete the text between START and END and return it."
359 (if (cc-bytecomp-fboundp 'delete-and-extract-region)
360 ;; Emacs 21.1 and later
361 `(delete-and-extract-region ,start ,end)
362 ;; XEmacs and Emacs 20.x
363 `(prog1
364 (buffer-substring ,start ,end)
365 (delete-region ,start ,end))))
367 (defmacro c-safe (&rest body)
368 ;; safely execute BODY, return nil if an error occurred
369 `(condition-case nil
370 (progn ,@body)
371 (error nil)))
372 (put 'c-safe 'lisp-indent-function 0)
374 (defmacro c-int-to-char (integer)
375 ;; In GNU Emacs, a character is an integer. In XEmacs, a character is a
376 ;; type distinct from an integer. Sometimes we need to convert integers to
377 ;; characters. `c-int-to-char' makes this conversion, if necessary.
378 (if (fboundp 'int-to-char)
379 `(int-to-char ,integer)
380 integer))
382 (defmacro c-sentence-end ()
383 ;; Get the regular expression `sentence-end'.
384 (if (cc-bytecomp-fboundp 'sentence-end)
385 ;; Emacs 22:
386 `(sentence-end)
387 ;; Emacs <22 + XEmacs
388 `sentence-end))
390 (defmacro c-default-value-sentence-end ()
391 ;; Get the default value of the variable sentence end.
392 (if (cc-bytecomp-fboundp 'sentence-end)
393 ;; Emacs 22:
394 `(let (sentence-end) (sentence-end))
395 ;; Emacs <22 + XEmacs
396 `(default-value 'sentence-end)))
398 ;; The following is essentially `save-buffer-state' from lazy-lock.el.
399 ;; It ought to be a standard macro.
400 (defmacro c-save-buffer-state (varlist &rest body)
401 "Bind variables according to VARLIST (in `let*' style) and eval BODY,
402 then restore the buffer state under the assumption that no significant
403 modification has been made in BODY. A change is considered
404 significant if it affects the buffer text in any way that isn't
405 completely restored again. Changes in text properties like `face' or
406 `syntax-table' are considered insignificant. This macro allows text
407 properties to be changed, even in a read-only buffer.
409 This macro should be placed around all calculations which set
410 \"insignificant\" text properties in a buffer, even when the buffer is
411 known to be writeable. That way, these text properties remain set
412 even if the user undoes the command which set them.
414 This macro should ALWAYS be placed around \"temporary\" internal buffer
415 changes \(like adding a newline to calculate a text-property then
416 deleting it again\), so that the user never sees them on his
417 `buffer-undo-list'. See also `c-tentative-buffer-changes'.
419 However, any user-visible changes to the buffer \(like auto-newlines\)
420 must not be within a `c-save-buffer-state', since the user then
421 wouldn't be able to undo them.
423 The return value is the value of the last form in BODY."
424 `(let* ((modified (buffer-modified-p)) (buffer-undo-list t)
425 (inhibit-read-only t) (inhibit-point-motion-hooks t)
426 before-change-functions after-change-functions
427 deactivate-mark
428 buffer-file-name buffer-file-truename ; Prevent primitives checking
429 ; for file modification
430 ,@varlist)
431 (unwind-protect
432 (progn ,@body)
433 (and (not modified)
434 (buffer-modified-p)
435 (set-buffer-modified-p nil)))))
436 (put 'c-save-buffer-state 'lisp-indent-function 1)
438 (defmacro c-tentative-buffer-changes (&rest body)
439 "Eval BODY and optionally restore the buffer contents to the state it
440 was in before BODY. Any changes are kept if the last form in BODY
441 returns non-nil. Otherwise it's undone using the undo facility, and
442 various other buffer state that might be affected by the changes is
443 restored. That includes the current buffer, point, mark, mark
444 activation \(similar to `save-excursion'), and the modified state.
445 The state is also restored if BODY exits nonlocally.
447 If BODY makes a change that unconditionally is undone then wrap this
448 macro inside `c-save-buffer-state'. That way the change can be done
449 even when the buffer is read-only, and without interference from
450 various buffer change hooks."
451 `(let (-tnt-chng-keep
452 -tnt-chng-state)
453 (unwind-protect
454 ;; Insert an undo boundary for use with `undo-more'. We
455 ;; don't use `undo-boundary' since it doesn't insert one
456 ;; unconditionally.
457 (setq buffer-undo-list (cons nil buffer-undo-list)
458 -tnt-chng-state (c-tnt-chng-record-state)
459 -tnt-chng-keep (progn ,@body))
460 (c-tnt-chng-cleanup -tnt-chng-keep -tnt-chng-state))))
461 (put 'c-tentative-buffer-changes 'lisp-indent-function 0)
463 (defun c-tnt-chng-record-state ()
464 ;; Used internally in `c-tentative-buffer-changes'.
465 (vector buffer-undo-list ; 0
466 (current-buffer) ; 1
467 ;; No need to use markers for the point and mark; if the
468 ;; undo got out of synch we're hosed anyway.
469 (point) ; 2
470 (mark t) ; 3
471 (c-region-is-active-p) ; 4
472 (buffer-modified-p))) ; 5
474 (defun c-tnt-chng-cleanup (keep saved-state)
475 ;; Used internally in `c-tentative-buffer-changes'.
477 (let ((saved-undo-list (elt saved-state 0)))
478 (if (eq buffer-undo-list saved-undo-list)
479 ;; No change was done afterall.
480 (setq buffer-undo-list (cdr saved-undo-list))
482 (if keep
483 ;; Find and remove the undo boundary.
484 (let ((p buffer-undo-list))
485 (while (not (eq (cdr p) saved-undo-list))
486 (setq p (cdr p)))
487 (setcdr p (cdr saved-undo-list)))
489 ;; `primitive-undo' will remove the boundary.
490 (setq saved-undo-list (cdr saved-undo-list))
491 (let ((undo-in-progress t))
492 (while (not (eq (setq buffer-undo-list
493 (primitive-undo 1 buffer-undo-list))
494 saved-undo-list))))
496 (when (buffer-live-p (elt saved-state 1))
497 (set-buffer (elt saved-state 1))
498 (goto-char (elt saved-state 2))
499 (set-mark (elt saved-state 3))
500 (c-set-region-active (elt saved-state 4))
501 (and (not (elt saved-state 5))
502 (buffer-modified-p)
503 (set-buffer-modified-p nil)))))))
505 (defmacro c-forward-syntactic-ws (&optional limit)
506 "Forward skip over syntactic whitespace.
507 Syntactic whitespace is defined as whitespace characters, comments,
508 and preprocessor directives. However if point starts inside a comment
509 or preprocessor directive, the content of it is not treated as
510 whitespace.
512 LIMIT sets an upper limit of the forward movement, if specified. If
513 LIMIT or the end of the buffer is reached inside a comment or
514 preprocessor directive, the point will be left there.
516 Note that this function might do hidden buffer changes. See the
517 comment at the start of cc-engine.el for more info."
518 (if limit
519 `(save-restriction
520 (narrow-to-region (point-min) (or ,limit (point-max)))
521 (c-forward-sws))
522 '(c-forward-sws)))
524 (defmacro c-backward-syntactic-ws (&optional limit)
525 "Backward skip over syntactic whitespace.
526 Syntactic whitespace is defined as whitespace characters, comments,
527 and preprocessor directives. However if point starts inside a comment
528 or preprocessor directive, the content of it is not treated as
529 whitespace.
531 LIMIT sets a lower limit of the backward movement, if specified. If
532 LIMIT is reached inside a line comment or preprocessor directive then
533 the point is moved into it past the whitespace at the end.
535 Note that this function might do hidden buffer changes. See the
536 comment at the start of cc-engine.el for more info."
537 (if limit
538 `(save-restriction
539 (narrow-to-region (or ,limit (point-min)) (point-max))
540 (c-backward-sws))
541 '(c-backward-sws)))
543 (defmacro c-forward-sexp (&optional count)
544 "Move forward across COUNT balanced expressions.
545 A negative COUNT means move backward. Signal an error if the move
546 fails for any reason.
548 This is like `forward-sexp' except that it isn't interactive and does
549 not do any user friendly adjustments of the point and that it isn't
550 susceptible to user configurations such as disabling of signals in
551 certain situations."
552 (or count (setq count 1))
553 `(goto-char (scan-sexps (point) ,count)))
555 (defmacro c-backward-sexp (&optional count)
556 "See `c-forward-sexp' and reverse directions."
557 (or count (setq count 1))
558 `(c-forward-sexp ,(if (numberp count) (- count) `(- ,count))))
560 (defmacro c-safe-scan-lists (from count depth &optional limit)
561 "Like `scan-lists' but returns nil instead of signalling errors
562 for unbalanced parens.
564 A limit for the search may be given. FROM is assumed to be on the
565 right side of it."
566 (let ((res (if (featurep 'xemacs)
567 `(scan-lists ,from ,count ,depth nil t)
568 `(c-safe (scan-lists ,from ,count ,depth)))))
569 (if limit
570 `(save-restriction
571 ,(if (numberp count)
572 (if (< count 0)
573 `(narrow-to-region ,limit (point-max))
574 `(narrow-to-region (point-min) ,limit))
575 `(if (< ,count 0)
576 (narrow-to-region ,limit (point-max))
577 (narrow-to-region (point-min) ,limit)))
578 ,res)
579 res)))
582 ;; Wrappers for common scan-lists cases, mainly because it's almost
583 ;; impossible to get a feel for how that function works.
585 (defmacro c-go-list-forward ()
586 "Move backward across one balanced group of parentheses.
588 Return POINT when we succeed, NIL when we fail. In the latter case, leave
589 point unmoved."
590 `(c-safe (let ((endpos (scan-lists (point) 1 0)))
591 (goto-char endpos)
592 endpos)))
594 (defmacro c-go-list-backward ()
595 "Move backward across one balanced group of parentheses.
597 Return POINT when we succeed, NIL when we fail. In the latter case, leave
598 point unmoved."
599 `(c-safe (let ((endpos (scan-lists (point) -1 0)))
600 (goto-char endpos)
601 endpos)))
603 (defmacro c-up-list-forward (&optional pos limit)
604 "Return the first position after the list sexp containing POS,
605 or nil if no such position exists. The point is used if POS is left out.
607 A limit for the search may be given. The start position is assumed to
608 be before it."
609 `(c-safe-scan-lists ,(or pos `(point)) 1 1 ,limit))
611 (defmacro c-up-list-backward (&optional pos limit)
612 "Return the position of the start of the list sexp containing POS,
613 or nil if no such position exists. The point is used if POS is left out.
615 A limit for the search may be given. The start position is assumed to
616 be after it."
617 `(c-safe-scan-lists ,(or pos `(point)) -1 1 ,limit))
619 (defmacro c-down-list-forward (&optional pos limit)
620 "Return the first position inside the first list sexp after POS,
621 or nil if no such position exists. The point is used if POS is left out.
623 A limit for the search may be given. The start position is assumed to
624 be before it."
625 `(c-safe-scan-lists ,(or pos `(point)) 1 -1 ,limit))
627 (defmacro c-down-list-backward (&optional pos limit)
628 "Return the last position inside the last list sexp before POS,
629 or nil if no such position exists. The point is used if POS is left out.
631 A limit for the search may be given. The start position is assumed to
632 be after it."
633 `(c-safe-scan-lists ,(or pos `(point)) -1 -1 ,limit))
635 (defmacro c-go-up-list-forward (&optional pos limit)
636 "Move the point to the first position after the list sexp containing POS,
637 or containing the point if POS is left out. Return t if such a
638 position exists, otherwise nil is returned and the point isn't moved.
640 A limit for the search may be given. The start position is assumed to
641 be before it."
642 (let ((res `(c-safe (goto-char (scan-lists ,(or pos `(point)) 1 1)) t)))
643 (if limit
644 `(save-restriction
645 (narrow-to-region (point-min) ,limit)
646 ,res)
647 res)))
649 (defmacro c-go-up-list-backward (&optional pos limit)
650 "Move the point to the position of the start of the list sexp containing POS,
651 or containing the point if POS is left out. Return t if such a
652 position exists, otherwise nil is returned and the point isn't moved.
654 A limit for the search may be given. The start position is assumed to
655 be after it."
656 (let ((res `(c-safe (goto-char (scan-lists ,(or pos `(point)) -1 1)) t)))
657 (if limit
658 `(save-restriction
659 (narrow-to-region ,limit (point-max))
660 ,res)
661 res)))
663 (defmacro c-go-down-list-forward (&optional pos limit)
664 "Move the point to the first position inside the first list sexp after POS,
665 or before the point if POS is left out. Return t if such a position
666 exists, otherwise nil is returned and the point isn't moved.
668 A limit for the search may be given. The start position is assumed to
669 be before it."
670 (let ((res `(c-safe (goto-char (scan-lists ,(or pos `(point)) 1 -1)) t)))
671 (if limit
672 `(save-restriction
673 (narrow-to-region (point-min) ,limit)
674 ,res)
675 res)))
677 (defmacro c-go-down-list-backward (&optional pos limit)
678 "Move the point to the last position inside the last list sexp before POS,
679 or before the point if POS is left out. Return t if such a position
680 exists, otherwise nil is returned and the point isn't moved.
682 A limit for the search may be given. The start position is assumed to
683 be after it."
684 (let ((res `(c-safe (goto-char (scan-lists ,(or pos `(point)) -1 -1)) t)))
685 (if limit
686 `(save-restriction
687 (narrow-to-region ,limit (point-max))
688 ,res)
689 res)))
692 (defmacro c-beginning-of-defun-1 ()
693 ;; Wrapper around beginning-of-defun.
695 ;; NOTE: This function should contain the only explicit use of
696 ;; beginning-of-defun in CC Mode. Eventually something better than
697 ;; b-o-d will be available and this should be the only place the
698 ;; code needs to change. Everything else should use
699 ;; (c-beginning-of-defun-1)
701 ;; This is really a bit too large to be a macro but that isn't a
702 ;; problem as long as it only is used in one place in
703 ;; `c-parse-state'.
705 `(progn
706 (if (and ,(cc-bytecomp-fboundp 'buffer-syntactic-context-depth)
707 c-enable-xemacs-performance-kludge-p)
708 ,(when (cc-bytecomp-fboundp 'buffer-syntactic-context-depth)
709 ;; XEmacs only. This can improve the performance of
710 ;; c-parse-state to between 3 and 60 times faster when
711 ;; braces are hung. It can also degrade performance by
712 ;; about as much when braces are not hung.
713 '(let (beginning-of-defun-function end-of-defun-function
714 pos)
715 (while (not pos)
716 (save-restriction
717 (widen)
718 (setq pos (c-safe-scan-lists
719 (point) -1 (buffer-syntactic-context-depth))))
720 (cond
721 ((bobp) (setq pos (point-min)))
722 ((not pos)
723 (let ((distance (skip-chars-backward "^{")))
724 ;; unbalanced parenthesis, while invalid C code,
725 ;; shouldn't cause an infloop! See unbal.c
726 (when (zerop distance)
727 ;; Punt!
728 (beginning-of-defun)
729 (setq pos (point)))))
730 ((= pos 0))
731 ((not (eq (char-after pos) ?{))
732 (goto-char pos)
733 (setq pos nil))
735 (goto-char pos)))
736 ;; Emacs, which doesn't have buffer-syntactic-context-depth
737 (let (beginning-of-defun-function end-of-defun-function)
738 (beginning-of-defun)))
739 ;; if defun-prompt-regexp is non-nil, b-o-d won't leave us at the
740 ;; open brace.
741 (and defun-prompt-regexp
742 (looking-at defun-prompt-regexp)
743 (goto-char (match-end 0)))))
746 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
747 ;; V i r t u a l S e m i c o l o n s
749 ;; In most CC Mode languages, statements are terminated explicitly by
750 ;; semicolons or closing braces. In some of the CC modes (currently only AWK
751 ;; Mode (April 2004)), statements are (or can be) terminated by EOLs. Such a
752 ;; statement is said to be terminated by a "virtual semicolon" (VS). A
753 ;; statement terminated by an actual semicolon or brace is never considered to
754 ;; have a VS.
756 ;; The indentation engine (or whatever) tests for a VS at a specific position
757 ;; by invoking the macro `c-at-vsemi-p', which in its turn calls the mode
758 ;; specific function (if any) which is the value of the language variable
759 ;; `c-at-vsemi-p-fn'. The actual details of what constitutes a VS in a
760 ;; language are thus encapsulated in code specific to that language
761 ;; (e.g. cc-awk.el). `c-at-vsemi-p' returns non-nil if point (or the optional
762 ;; parameter POS) is at a VS, nil otherwise.
764 ;; The language specific function might well do extensive analysis of the
765 ;; source text, and may use a cacheing scheme to speed up repeated calls.
767 ;; The "virtual semicolon" lies just after the last non-ws token on the line.
768 ;; Like POINT, it is considered to lie between two characters. For example,
769 ;; at the place shown in the following AWK source line:
771 ;; kbyte = 1024 # 1000 if you're not picky
772 ;; ^
773 ;; |
774 ;; Virtual Semicolon
776 ;; In addition to `c-at-vsemi-p-fn', a mode may need to supply a function for
777 ;; `c-vsemi-status-unknown-p-fn'. The macro `c-vsemi-status-unknown-p' is a
778 ;; rather recondite kludge. It exists because the function
779 ;; `c-beginning-of-statement-1' sometimes tests for VSs as an optimisation,
780 ;; but `c-at-vsemi-p' might well need to call `c-beginning-of-statement-1' in
781 ;; its calculations, thus potentially leading to infinite recursion.
783 ;; The macro `c-vsemi-status-unknown-p' resolves this problem; it may return
784 ;; non-nil at any time; returning nil is a guarantee that an immediate
785 ;; invocation of `c-at-vsemi-p' at point will NOT call
786 ;; `c-beginning-of-statement-1'. `c-vsemi-status-unknown-p' may not itself
787 ;; call `c-beginning-of-statement-1'.
789 ;; The macro `c-vsemi-status-unknown-p' will typically check the cacheing
790 ;; scheme used by the `c-at-vsemi-p-fn', hence the name - the status is
791 ;; "unknown" if there is no cache entry current for the line.
792 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
794 (defmacro c-at-vsemi-p (&optional pos)
795 ;; Is there a virtual semicolon (not a real one or a }) at POS (defaults to
796 ;; point)? Always returns nil for languages which don't have Virtual
797 ;; semicolons.
798 ;; This macro might do hidden buffer changes.
799 `(if c-at-vsemi-p-fn
800 (funcall c-at-vsemi-p-fn ,@(if pos `(,pos)))))
802 (defmacro c-vsemi-status-unknown-p ()
803 ;; Return NIL only if it can be guaranteed that an immediate
804 ;; (c-at-vsemi-p) will NOT call c-beginning-of-statement-1. Otherwise,
805 ;; return non-nil. (See comments above). The function invoked by this
806 ;; macro MUST NOT UNDER ANY CIRCUMSTANCES itself call
807 ;; c-beginning-of-statement-1.
808 ;; Languages which don't have EOL terminated statements always return NIL
809 ;; (they _know_ there's no vsemi ;-).
810 `(if c-vsemi-status-unknown-p-fn (funcall c-vsemi-status-unknown-p-fn)))
813 (defmacro c-benign-error (format &rest args)
814 ;; Formats an error message for the echo area and dings, i.e. like
815 ;; `error' but doesn't abort.
816 `(progn
817 (message ,format ,@args)
818 (ding)))
820 (defmacro c-with-syntax-table (table &rest code)
821 ;; Temporarily switches to the specified syntax table in a failsafe
822 ;; way to execute code.
823 `(let ((c-with-syntax-table-orig-table (syntax-table)))
824 (unwind-protect
825 (progn
826 (set-syntax-table ,table)
827 ,@code)
828 (set-syntax-table c-with-syntax-table-orig-table))))
829 (put 'c-with-syntax-table 'lisp-indent-function 1)
831 (defmacro c-skip-ws-forward (&optional limit)
832 "Skip over any whitespace following point.
833 This function skips over horizontal and vertical whitespace and line
834 continuations."
835 (if limit
836 `(let ((limit (or ,limit (point-max))))
837 (while (progn
838 ;; skip-syntax-* doesn't count \n as whitespace..
839 (skip-chars-forward " \t\n\r\f\v" limit)
840 (when (and (eq (char-after) ?\\)
841 (< (point) limit))
842 (forward-char)
843 (or (eolp)
844 (progn (backward-char) nil))))))
845 '(while (progn
846 (skip-chars-forward " \t\n\r\f\v")
847 (when (eq (char-after) ?\\)
848 (forward-char)
849 (or (eolp)
850 (progn (backward-char) nil)))))))
852 (defmacro c-skip-ws-backward (&optional limit)
853 "Skip over any whitespace preceding point.
854 This function skips over horizontal and vertical whitespace and line
855 continuations."
856 (if limit
857 `(let ((limit (or ,limit (point-min))))
858 (while (progn
859 ;; skip-syntax-* doesn't count \n as whitespace..
860 (skip-chars-backward " \t\n\r\f\v" limit)
861 (and (eolp)
862 (eq (char-before) ?\\)
863 (> (point) limit)))
864 (backward-char)))
865 '(while (progn
866 (skip-chars-backward " \t\n\r\f\v")
867 (and (eolp)
868 (eq (char-before) ?\\)))
869 (backward-char))))
871 (eval-and-compile
872 (defvar c-langs-are-parametric nil))
874 (defmacro c-major-mode-is (mode)
875 "Return non-nil if the current CC Mode major mode is MODE.
876 MODE is either a mode symbol or a list of mode symbols."
878 (if c-langs-are-parametric
879 ;; Inside a `c-lang-defconst'.
880 `(c-lang-major-mode-is ,mode)
882 (if (eq (car-safe mode) 'quote)
883 (let ((mode (eval mode)))
884 (if (listp mode)
885 `(memq c-buffer-is-cc-mode ',mode)
886 `(eq c-buffer-is-cc-mode ',mode)))
888 `(let ((mode ,mode))
889 (if (listp mode)
890 (memq c-buffer-is-cc-mode mode)
891 (eq c-buffer-is-cc-mode mode))))))
894 ;; Macros/functions to handle so-called "char properties", which are
895 ;; properties set on a single character and that never spread to any
896 ;; other characters.
898 (eval-and-compile
899 ;; Constant used at compile time to decide whether or not to use
900 ;; XEmacs extents. Check all the extent functions we'll use since
901 ;; some packages might add compatibility aliases for some of them in
902 ;; Emacs.
903 (defconst c-use-extents (and (cc-bytecomp-fboundp 'extent-at)
904 (cc-bytecomp-fboundp 'set-extent-property)
905 (cc-bytecomp-fboundp 'set-extent-properties)
906 (cc-bytecomp-fboundp 'make-extent)
907 (cc-bytecomp-fboundp 'extent-property)
908 (cc-bytecomp-fboundp 'delete-extent)
909 (cc-bytecomp-fboundp 'map-extents))))
911 ;; `c-put-char-property' is complex enough in XEmacs and Emacs < 21 to
912 ;; make it a function.
913 (defalias 'c-put-char-property-fun
914 (cc-eval-when-compile
915 (cond (c-use-extents
916 ;; XEmacs.
917 (byte-compile
918 (lambda (pos property value)
919 (let ((ext (extent-at pos nil property)))
920 (if ext
921 (set-extent-property ext property value)
922 (set-extent-properties (make-extent pos (1+ pos))
923 (cons property
924 (cons value
925 '(start-open t
926 end-open t)))))))))
928 ((not (cc-bytecomp-boundp 'text-property-default-nonsticky))
929 ;; In Emacs < 21 we have to mess with the `rear-nonsticky' property.
930 (byte-compile
931 (lambda (pos property value)
932 (put-text-property pos (1+ pos) property value)
933 (let ((prop (get-text-property pos 'rear-nonsticky)))
934 (or (memq property prop)
935 (put-text-property pos (1+ pos)
936 'rear-nonsticky
937 (cons property prop))))))))))
938 (cc-bytecomp-defun c-put-char-property-fun) ; Make it known below.
940 (defmacro c-put-char-property (pos property value)
941 ;; Put the given property with the given value on the character at
942 ;; POS and make it front and rear nonsticky, or start and end open
943 ;; in XEmacs vocabulary. If the character already has the given
944 ;; property then the value is replaced, and the behavior is
945 ;; undefined if that property has been put by some other function.
946 ;; PROPERTY is assumed to be constant.
948 ;; If there's a `text-property-default-nonsticky' variable (Emacs
949 ;; 21) then it's assumed that the property is present on it.
951 ;; This macro does a hidden buffer change.
952 (setq property (eval property))
953 (if (or c-use-extents
954 (not (cc-bytecomp-boundp 'text-property-default-nonsticky)))
955 ;; XEmacs and Emacs < 21.
956 `(c-put-char-property-fun ,pos ',property ,value)
957 ;; In Emacs 21 we got the `rear-nonsticky' property covered
958 ;; by `text-property-default-nonsticky'.
959 `(let ((-pos- ,pos))
960 (put-text-property -pos- (1+ -pos-) ',property ,value))))
962 (defmacro c-get-char-property (pos property)
963 ;; Get the value of the given property on the character at POS if
964 ;; it's been put there by `c-put-char-property'. PROPERTY is
965 ;; assumed to be constant.
966 (setq property (eval property))
967 (if c-use-extents
968 ;; XEmacs.
969 `(let ((ext (extent-at ,pos nil ',property)))
970 (if ext (extent-property ext ',property)))
971 ;; Emacs.
972 `(get-text-property ,pos ',property)))
974 ;; `c-clear-char-property' is complex enough in Emacs < 21 to make it
975 ;; a function, since we have to mess with the `rear-nonsticky' property.
976 (defalias 'c-clear-char-property-fun
977 (cc-eval-when-compile
978 (unless (or c-use-extents
979 (cc-bytecomp-boundp 'text-property-default-nonsticky))
980 (byte-compile
981 (lambda (pos property)
982 (when (get-text-property pos property)
983 (remove-text-properties pos (1+ pos) (list property nil))
984 (put-text-property pos (1+ pos)
985 'rear-nonsticky
986 (delq property (get-text-property
987 pos 'rear-nonsticky)))))))))
988 (cc-bytecomp-defun c-clear-char-property-fun) ; Make it known below.
990 (defmacro c-clear-char-property (pos property)
991 ;; Remove the given property on the character at POS if it's been put
992 ;; there by `c-put-char-property'. PROPERTY is assumed to be
993 ;; constant.
995 ;; This macro does a hidden buffer change.
996 (setq property (eval property))
997 (cond (c-use-extents
998 ;; XEmacs.
999 `(let ((ext (extent-at ,pos nil ',property)))
1000 (if ext (delete-extent ext))))
1001 ((cc-bytecomp-boundp 'text-property-default-nonsticky)
1002 ;; In Emacs 21 we got the `rear-nonsticky' property covered
1003 ;; by `text-property-default-nonsticky'.
1004 `(let ((pos ,pos))
1005 (remove-text-properties pos (1+ pos)
1006 '(,property nil))))
1008 ;; Emacs < 21.
1009 `(c-clear-char-property-fun ,pos ',property))))
1011 (defmacro c-clear-char-properties (from to property)
1012 ;; Remove all the occurences of the given property in the given
1013 ;; region that has been put with `c-put-char-property'. PROPERTY is
1014 ;; assumed to be constant.
1016 ;; Note that this function does not clean up the property from the
1017 ;; lists of the `rear-nonsticky' properties in the region, if such
1018 ;; are used. Thus it should not be used for common properties like
1019 ;; `syntax-table'.
1021 ;; This macro does hidden buffer changes.
1022 (setq property (eval property))
1023 (if c-use-extents
1024 ;; XEmacs.
1025 `(map-extents (lambda (ext ignored)
1026 (delete-extent ext))
1027 nil ,from ,to nil nil ',property)
1028 ;; Emacs.
1029 `(remove-text-properties ,from ,to '(,property nil))))
1031 (defun c-clear-char-property-with-value-function (from to property value)
1032 "Remove all text-properties PROPERTY from the region (FROM, TO)
1033 which have the value VALUE, as tested by `equal'. These
1034 properties are assumed to be over individual characters, having
1035 been put there by c-put-char-property. POINT remains unchanged."
1036 (let ((place from) end-place)
1037 (while ; loop round occurrances of (PROPERTY VALUE)
1038 (progn
1039 (while ; loop round changes in PROPERTY till we find VALUE
1040 (and
1041 (< place to)
1042 (not (equal (get-text-property place property) value)))
1043 (setq place (next-single-property-change place property nil to)))
1044 (< place to))
1045 (setq end-place (next-single-property-change place property nil to))
1046 (put-text-property place end-place property nil)
1047 ;; Do we have to do anything with stickiness here?
1048 (setq place end-place))))
1050 (defmacro c-clear-char-property-with-value (from to property value)
1051 "Remove all text-properties PROPERTY from the region [FROM, TO)
1052 which have the value VALUE, as tested by `equal'. These
1053 properties are assumed to be over individual characters, having
1054 been put there by c-put-char-property. POINT remains unchanged."
1055 (if c-use-extents
1056 ;; XEmacs
1057 `(let ((-property- ,property))
1058 (map-extents (lambda (ext val)
1059 (if (equal (extent-property ext -property-) val)
1060 (delete-extent ext)))
1061 nil ,from ,to ,value nil -property-))
1062 ;; Gnu Emacs
1063 `(c-clear-char-property-with-value-function ,from ,to ,property ,value)))
1065 ;; Macros to put overlays (Emacs) or extents (XEmacs) on buffer text.
1066 ;; For our purposes, these are characterized by being possible to
1067 ;; remove again without affecting the other text properties in the
1068 ;; buffer that got overridden when they were put.
1070 (defmacro c-put-overlay (from to property value)
1071 ;; Put an overlay/extent covering the given range in the current
1072 ;; buffer. It's currently undefined whether it's front/end sticky
1073 ;; or not. The overlay/extent object is returned.
1074 (if (cc-bytecomp-fboundp 'make-overlay)
1075 ;; Emacs.
1076 `(let ((ol (make-overlay ,from ,to)))
1077 (overlay-put ol ,property ,value)
1079 ;; XEmacs.
1080 `(let ((ext (make-extent ,from ,to)))
1081 (set-extent-property ext ,property ,value)
1082 ext)))
1084 (defmacro c-delete-overlay (overlay)
1085 ;; Deletes an overlay/extent object previously retrieved using
1086 ;; `c-put-overlay'.
1087 (if (cc-bytecomp-fboundp 'make-overlay)
1088 ;; Emacs.
1089 `(delete-overlay ,overlay)
1090 ;; XEmacs.
1091 `(delete-extent ,overlay)))
1094 ;; Make edebug understand the macros.
1095 ;(eval-after-load "edebug" ; 2006-07-09: def-edebug-spec is now in subr.el.
1096 ; '(progn
1097 (def-edebug-spec cc-eval-when-compile t)
1098 (def-edebug-spec c-point t)
1099 (def-edebug-spec c-set-region-active t)
1100 (def-edebug-spec c-safe t)
1101 (def-edebug-spec c-save-buffer-state let*)
1102 (def-edebug-spec c-tentative-buffer-changes t)
1103 (def-edebug-spec c-forward-syntactic-ws t)
1104 (def-edebug-spec c-backward-syntactic-ws t)
1105 (def-edebug-spec c-forward-sexp t)
1106 (def-edebug-spec c-backward-sexp t)
1107 (def-edebug-spec c-up-list-forward t)
1108 (def-edebug-spec c-up-list-backward t)
1109 (def-edebug-spec c-down-list-forward t)
1110 (def-edebug-spec c-down-list-backward t)
1111 (def-edebug-spec c-add-syntax t)
1112 (def-edebug-spec c-add-class-syntax t)
1113 (def-edebug-spec c-benign-error t)
1114 (def-edebug-spec c-with-syntax-table t)
1115 (def-edebug-spec c-skip-ws-forward t)
1116 (def-edebug-spec c-skip-ws-backward t)
1117 (def-edebug-spec c-major-mode-is t)
1118 (def-edebug-spec c-put-char-property t)
1119 (def-edebug-spec c-get-char-property t)
1120 (def-edebug-spec c-clear-char-property t)
1121 (def-edebug-spec c-clear-char-properties t)
1122 (def-edebug-spec c-put-overlay t)
1123 (def-edebug-spec c-delete-overlay t) ;))
1126 ;;; Functions.
1128 ;; Note: All these after the macros, to be on safe side in avoiding
1129 ;; bugs where macros are defined too late. These bugs often only show
1130 ;; when the files are compiled in a certain order within the same
1131 ;; session.
1133 (defsubst c-end-of-defun-1 ()
1134 ;; Replacement for end-of-defun that use c-beginning-of-defun-1.
1135 (let ((start (point)))
1136 ;; Skip forward into the next defun block. Don't bother to avoid
1137 ;; comments, literals etc, since beginning-of-defun doesn't do that
1138 ;; anyway.
1139 (skip-chars-forward "^}")
1140 (c-beginning-of-defun-1)
1141 (if (eq (char-after) ?{)
1142 (c-forward-sexp))
1143 (if (< (point) start)
1144 (goto-char (point-max)))))
1146 (defconst c-<-as-paren-syntax '(4 . ?>))
1148 (defsubst c-mark-<-as-paren (pos)
1149 ;; Mark the "<" character at POS as an sexp list opener using the
1150 ;; syntax-table property.
1152 ;; This function does a hidden buffer change.
1153 (c-put-char-property pos 'syntax-table c-<-as-paren-syntax))
1155 (defconst c->-as-paren-syntax '(5 . ?<))
1157 (defsubst c-mark->-as-paren (pos)
1158 ;; Mark the ">" character at POS as an sexp list closer using the
1159 ;; syntax-table property.
1161 ;; This function does a hidden buffer change.
1162 (c-put-char-property pos 'syntax-table c->-as-paren-syntax))
1164 (defsubst c-intersect-lists (list alist)
1165 ;; return the element of ALIST that matches the first element found
1166 ;; in LIST. Uses assq.
1167 (let (match)
1168 (while (and list
1169 (not (setq match (assq (car list) alist))))
1170 (setq list (cdr list)))
1171 match))
1173 (defsubst c-lookup-lists (list alist1 alist2)
1174 ;; first, find the first entry from LIST that is present in ALIST1,
1175 ;; then find the entry in ALIST2 for that entry.
1176 (assq (car (c-intersect-lists list alist1)) alist2))
1178 (defsubst c-langelem-sym (langelem)
1179 "Return the syntactic symbol in LANGELEM.
1181 LANGELEM is either a cons cell on the \"old\" form given as the first
1182 argument to lineup functions or a syntactic element on the \"new\"
1183 form as used in `c-syntactic-element'."
1184 (car langelem))
1186 (defsubst c-langelem-pos (langelem)
1187 "Return the anchor position in LANGELEM, or nil if there is none.
1189 LANGELEM is either a cons cell on the \"old\" form given as the first
1190 argument to lineup functions or a syntactic element on the \"new\"
1191 form as used in `c-syntactic-element'."
1192 (if (consp (cdr langelem))
1193 (car-safe (cdr langelem))
1194 (cdr langelem)))
1196 (defun c-langelem-col (langelem &optional preserve-point)
1197 "Return the column of the anchor position in LANGELEM.
1198 Also move the point to that position unless PRESERVE-POINT is non-nil.
1200 LANGELEM is either a cons cell on the \"old\" form given as the first
1201 argument to lineup functions or a syntactic element on the \"new\"
1202 form as used in `c-syntactic-element'."
1203 (let ((pos (c-langelem-pos langelem))
1204 (here (point)))
1205 (if pos
1206 (progn
1207 (goto-char pos)
1208 (prog1 (current-column)
1209 (if preserve-point
1210 (goto-char here))))
1211 0)))
1213 (defsubst c-langelem-2nd-pos (langelem)
1214 "Return the secondary position in LANGELEM, or nil if there is none.
1216 LANGELEM is typically a syntactic element on the \"new\" form as used
1217 in `c-syntactic-element'. It may also be a cons cell as passed in the
1218 first argument to lineup functions, but then the returned value always
1219 will be nil."
1220 (car-safe (cdr-safe (cdr-safe langelem))))
1222 (defsubst c-keep-region-active ()
1223 ;; Do whatever is necessary to keep the region active in XEmacs.
1224 ;; This is not needed for Emacs.
1225 (and (boundp 'zmacs-region-stays)
1226 (setq zmacs-region-stays t)))
1228 (put 'c-mode 'c-mode-prefix "c-")
1229 (put 'c++-mode 'c-mode-prefix "c++-")
1230 (put 'objc-mode 'c-mode-prefix "objc-")
1231 (put 'java-mode 'c-mode-prefix "java-")
1232 (put 'idl-mode 'c-mode-prefix "idl-")
1233 (put 'pike-mode 'c-mode-prefix "pike-")
1234 (put 'awk-mode 'c-mode-prefix "awk-")
1236 (defsubst c-mode-symbol (suffix)
1237 "Prefix the current mode prefix (e.g. \"c-\") to SUFFIX and return
1238 the corresponding symbol."
1239 (or c-buffer-is-cc-mode
1240 (error "Not inside a CC Mode based mode"))
1241 (let ((mode-prefix (get c-buffer-is-cc-mode 'c-mode-prefix)))
1242 (or mode-prefix
1243 (error "%S has no mode prefix known to `c-mode-symbol'"
1244 c-buffer-is-cc-mode))
1245 (intern (concat mode-prefix suffix))))
1247 (defsubst c-mode-var (suffix)
1248 "Prefix the current mode prefix (e.g. \"c-\") to SUFFIX and return
1249 the value of the variable with that name."
1250 (symbol-value (c-mode-symbol suffix)))
1252 (defsubst c-got-face-at (pos faces)
1253 "Return non-nil if position POS in the current buffer has any of the
1254 faces in the list FACES."
1255 (let ((pos-faces (get-text-property pos 'face)))
1256 (if (consp pos-faces)
1257 (progn
1258 (while (and pos-faces
1259 (not (memq (car pos-faces) faces)))
1260 (setq pos-faces (cdr pos-faces)))
1261 pos-faces)
1262 (memq pos-faces faces))))
1264 (defsubst c-face-name-p (facename)
1265 ;; Return t if FACENAME is the name of a face. This method is
1266 ;; necessary since facep in XEmacs only returns t for the actual
1267 ;; face objects (while it's only their names that are used just
1268 ;; about anywhere else) without providing a predicate that tests
1269 ;; face names.
1270 (memq facename (face-list)))
1272 (defun c-concat-separated (list separator)
1273 "Like `concat' on LIST, but separate each element with SEPARATOR.
1274 Notably, null elements in LIST are ignored."
1275 (mapconcat 'identity (delete nil (append list nil)) separator))
1277 (defun c-make-keywords-re (adorn list &optional mode)
1278 "Make a regexp that matches all the strings the list.
1279 Duplicates and nil elements in the list are removed. The resulting
1280 regexp may contain zero or more submatch expressions.
1282 If ADORN is t there will be at least one submatch and the first
1283 surrounds the matched alternative, and the regexp will also not match
1284 a prefix of any identifier. Adorned regexps cannot be appended. The
1285 language variable `c-nonsymbol-key' is used to make the adornment.
1287 A value 'appendable for ADORN is like above, but all alternatives in
1288 the list that end with a word constituent char will have \\> appended
1289 instead, so that the regexp remains appendable. Note that this
1290 variant doesn't always guarantee that an identifier prefix isn't
1291 matched since the symbol constituent '_' is normally considered a
1292 nonword token by \\>.
1294 The optional MODE specifies the language to get `c-nonsymbol-key' from
1295 when it's needed. The default is the current language taken from
1296 `c-buffer-is-cc-mode'."
1298 (let (unique)
1299 (dolist (elt list)
1300 (unless (member elt unique)
1301 (push elt unique)))
1302 (setq list (delete nil unique)))
1303 (if list
1304 (let (re)
1306 (if (eq adorn 'appendable)
1307 ;; This is kludgy but it works: Search for a string that
1308 ;; doesn't occur in any word in LIST. Append it to all
1309 ;; the alternatives where we want to add \>. Run through
1310 ;; `regexp-opt' and then replace it with \>.
1311 (let ((unique "") pos)
1312 (while (let (found)
1313 (setq unique (concat unique "@")
1314 pos list)
1315 (while (and pos
1316 (if (string-match unique (car pos))
1317 (progn (setq found t)
1318 nil)
1320 (setq pos (cdr pos)))
1321 found))
1322 (setq pos list)
1323 (while pos
1324 (if (string-match "\\w\\'" (car pos))
1325 (setcar pos (concat (car pos) unique)))
1326 (setq pos (cdr pos)))
1327 (setq re (regexp-opt list))
1328 (setq pos 0)
1329 (while (string-match unique re pos)
1330 (setq pos (+ (match-beginning 0) 2)
1331 re (replace-match "\\>" t t re))))
1333 (setq re (regexp-opt list)))
1335 ;; Emacs 20 and XEmacs (all versions so far) has a buggy
1336 ;; regexp-opt that doesn't always cope with strings containing
1337 ;; newlines. This kludge doesn't handle shy parens correctly
1338 ;; so we can't advice regexp-opt directly with it.
1339 (let (fail-list)
1340 (while list
1341 (and (string-match "\n" (car list)) ; To speed it up a little.
1342 (not (string-match (concat "\\`\\(" re "\\)\\'")
1343 (car list)))
1344 (setq fail-list (cons (car list) fail-list)))
1345 (setq list (cdr list)))
1346 (when fail-list
1347 (setq re (concat re
1348 "\\|"
1349 (mapconcat
1350 (if (eq adorn 'appendable)
1351 (lambda (str)
1352 (if (string-match "\\w\\'" str)
1353 (concat (regexp-quote str)
1354 "\\>")
1355 (regexp-quote str)))
1356 'regexp-quote)
1357 (sort fail-list
1358 (lambda (a b)
1359 (> (length a) (length b))))
1360 "\\|")))))
1362 ;; Add our own grouping parenthesis around re instead of
1363 ;; passing adorn to `regexp-opt', since in XEmacs it makes the
1364 ;; top level grouping "shy".
1365 (cond ((eq adorn 'appendable)
1366 (concat "\\(" re "\\)"))
1367 (adorn
1368 (concat "\\(" re "\\)"
1369 "\\("
1370 (c-get-lang-constant 'c-nonsymbol-key nil mode)
1371 "\\|$\\)"))
1373 re)))
1375 ;; Produce a regexp that matches nothing.
1376 (if adorn
1377 "\\(\\<\\>\\)"
1378 "\\<\\>")))
1380 (put 'c-make-keywords-re 'lisp-indent-function 1)
1382 (defun c-make-bare-char-alt (chars &optional inverted)
1383 "Make a character alternative string from the list of characters CHARS.
1384 The returned string is of the type that can be used with
1385 `skip-chars-forward' and `skip-chars-backward'. If INVERTED is
1386 non-nil, a caret is prepended to invert the set."
1387 ;; This function ought to be in the elisp core somewhere.
1388 (let ((str (if inverted "^" "")) char char2)
1389 (setq chars (sort (append chars nil) `<))
1390 (while chars
1391 (setq char (pop chars))
1392 (if (memq char '(?\\ ?^ ?-))
1393 ;; Quoting necessary (this method only works in the skip
1394 ;; functions).
1395 (setq str (format "%s\\%c" str char))
1396 (setq str (format "%s%c" str char)))
1397 ;; Check for range.
1398 (setq char2 char)
1399 (while (and chars (>= (1+ char2) (car chars)))
1400 (setq char2 (pop chars)))
1401 (unless (= char char2)
1402 (if (< (1+ char) char2)
1403 (setq str (format "%s-%c" str char2))
1404 (push char2 chars))))
1405 str))
1407 ;; Leftovers from (X)Emacs 19 compatibility.
1408 (defalias 'c-regexp-opt 'regexp-opt)
1409 (defalias 'c-regexp-opt-depth 'regexp-opt-depth)
1412 ;; Figure out what features this Emacs has
1414 (cc-bytecomp-defvar open-paren-in-column-0-is-defun-start)
1416 (defconst c-emacs-features
1417 (let (list)
1419 (if (boundp 'infodock-version)
1420 ;; I've no idea what this actually is, but it's legacy. /mast
1421 (setq list (cons 'infodock list)))
1423 ;; XEmacs uses 8-bit modify-syntax-entry flags.
1424 ;; Emacs uses a 1-bit flag. We will have to set up our
1425 ;; syntax tables differently to handle this.
1426 (let ((table (copy-syntax-table))
1427 entry)
1428 (modify-syntax-entry ?a ". 12345678" table)
1429 (cond
1430 ;; Emacs
1431 ((arrayp table)
1432 (setq entry (aref table ?a))
1433 ;; In Emacs, table entries are cons cells
1434 (if (consp entry) (setq entry (car entry))))
1435 ;; XEmacs
1436 ((fboundp 'get-char-table)
1437 (setq entry (get-char-table ?a table)))
1438 ;; incompatible
1439 (t (error "CC Mode is incompatible with this version of Emacs")))
1440 (setq list (cons (if (= (logand (lsh entry -16) 255) 255)
1441 '8-bit
1442 '1-bit)
1443 list)))
1445 (let ((buf (generate-new-buffer " test"))
1446 parse-sexp-lookup-properties
1447 parse-sexp-ignore-comments
1448 lookup-syntax-properties)
1449 (save-excursion
1450 (set-buffer buf)
1451 (set-syntax-table (make-syntax-table))
1453 ;; For some reason we have to set some of these after the
1454 ;; buffer has been made current. (Specifically,
1455 ;; `parse-sexp-ignore-comments' in Emacs 21.)
1456 (setq parse-sexp-lookup-properties t
1457 parse-sexp-ignore-comments t
1458 lookup-syntax-properties t)
1460 ;; Find out if the `syntax-table' text property works.
1461 (modify-syntax-entry ?< ".")
1462 (modify-syntax-entry ?> ".")
1463 (insert "<()>")
1464 (c-mark-<-as-paren (point-min))
1465 (c-mark->-as-paren (+ 3 (point-min)))
1466 (goto-char (point-min))
1467 (c-forward-sexp)
1468 (if (= (point) (+ 4 (point-min)))
1469 (setq list (cons 'syntax-properties list))
1470 (error (concat
1471 "CC Mode is incompatible with this version of Emacs - "
1472 "support for the `syntax-table' text property "
1473 "is required.")))
1475 ;; Find out if generic comment delimiters work.
1476 (c-safe
1477 (modify-syntax-entry ?x "!")
1478 (if (string-match "\\s!" "x")
1479 (setq list (cons 'gen-comment-delim list))))
1481 ;; Find out if generic string delimiters work.
1482 (c-safe
1483 (modify-syntax-entry ?x "|")
1484 (if (string-match "\\s|" "x")
1485 (setq list (cons 'gen-string-delim list))))
1487 ;; See if POSIX char classes work.
1488 (when (and (string-match "[[:alpha:]]" "a")
1489 ;; All versions of Emacs 21 so far haven't fixed
1490 ;; char classes in `skip-chars-forward' and
1491 ;; `skip-chars-backward'.
1492 (progn
1493 (delete-region (point-min) (point-max))
1494 (insert "foo123")
1495 (skip-chars-backward "[:alnum:]")
1496 (bobp))
1497 (= (skip-chars-forward "[:alpha:]") 3))
1498 (setq list (cons 'posix-char-classes list)))
1500 ;; See if `open-paren-in-column-0-is-defun-start' exists and
1501 ;; isn't buggy (Emacs >= 21.4).
1502 (when (boundp 'open-paren-in-column-0-is-defun-start)
1503 (let ((open-paren-in-column-0-is-defun-start nil)
1504 (parse-sexp-ignore-comments t))
1505 (delete-region (point-min) (point-max))
1506 (set-syntax-table (make-syntax-table))
1507 (modify-syntax-entry ?\' "\"")
1508 (cond
1509 ;; XEmacs. Afaik this is currently an Emacs-only
1510 ;; feature, but it's good to be prepared.
1511 ((memq '8-bit list)
1512 (modify-syntax-entry ?/ ". 1456")
1513 (modify-syntax-entry ?* ". 23"))
1514 ;; Emacs
1515 ((memq '1-bit list)
1516 (modify-syntax-entry ?/ ". 124b")
1517 (modify-syntax-entry ?* ". 23")))
1518 (modify-syntax-entry ?\n "> b")
1519 (insert "/* '\n () */")
1520 (backward-sexp)
1521 (if (bobp)
1522 (setq list (cons 'col-0-paren list)))))
1524 (set-buffer-modified-p nil))
1525 (kill-buffer buf))
1527 ;; See if `parse-partial-sexp' returns the eighth element.
1528 (if (c-safe (>= (length (save-excursion (parse-partial-sexp (point) (point))))
1529 10))
1530 (setq list (cons 'pps-extended-state list))
1531 (error (concat
1532 "CC Mode is incompatible with this version of Emacs - "
1533 "`parse-partial-sexp' has to return at least 10 elements.")))
1535 ;;(message "c-emacs-features: %S" list)
1536 list)
1537 "A list of certain features in the (X)Emacs you are using.
1538 There are many flavors of Emacs out there, each with different
1539 features supporting those needed by CC Mode. The following values
1540 might be present:
1542 '8-bit 8 bit syntax entry flags (XEmacs style).
1543 '1-bit 1 bit syntax entry flags (Emacs style).
1544 'syntax-properties It works to override the syntax for specific characters
1545 in the buffer with the 'syntax-table property. It's
1546 always set - CC Mode no longer works in emacsen without
1547 this feature.
1548 'gen-comment-delim Generic comment delimiters work
1549 (i.e. the syntax class `!').
1550 'gen-string-delim Generic string delimiters work
1551 (i.e. the syntax class `|').
1552 'pps-extended-state `parse-partial-sexp' returns a list with at least 10
1553 elements, i.e. it contains the position of the start of
1554 the last comment or string. It's always set - CC Mode
1555 no longer works in emacsen without this feature.
1556 'posix-char-classes The regexp engine understands POSIX character classes.
1557 'col-0-paren It's possible to turn off the ad-hoc rule that a paren
1558 in column zero is the start of a defun.
1559 'infodock This is Infodock (based on XEmacs).
1561 '8-bit and '1-bit are mutually exclusive.")
1564 ;;; Some helper constants.
1566 ;; If the regexp engine supports POSIX char classes then we can use
1567 ;; them to handle extended charsets correctly.
1568 (if (memq 'posix-char-classes c-emacs-features)
1569 (progn
1570 (defconst c-alpha "[:alpha:]")
1571 (defconst c-alnum "[:alnum:]")
1572 (defconst c-digit "[:digit:]")
1573 (defconst c-upper "[:upper:]")
1574 (defconst c-lower "[:lower:]"))
1575 (defconst c-alpha "a-zA-Z")
1576 (defconst c-alnum "a-zA-Z0-9")
1577 (defconst c-digit "0-9")
1578 (defconst c-upper "A-Z")
1579 (defconst c-lower "a-z"))
1582 ;;; System for handling language dependent constants.
1584 ;; This is used to set various language dependent data in a flexible
1585 ;; way: Language constants can be built from the values of other
1586 ;; language constants, also those for other languages. They can also
1587 ;; process the values of other language constants uniformly across all
1588 ;; the languages. E.g. one language constant can list all the type
1589 ;; keywords in each language, and another can build a regexp for each
1590 ;; language from those lists without code duplication.
1592 ;; Language constants are defined with `c-lang-defconst', and their
1593 ;; value forms (referred to as source definitions) are evaluated only
1594 ;; on demand when requested for a particular language with
1595 ;; `c-lang-const'. It's therefore possible to refer to the values of
1596 ;; constants defined later in the file, or in another file, just as
1597 ;; long as all the relevant `c-lang-defconst' have been loaded when
1598 ;; `c-lang-const' is actually evaluated from somewhere else.
1600 ;; `c-lang-const' forms are also evaluated at compile time and
1601 ;; replaced with the values they produce. Thus there's no overhead
1602 ;; for this system when compiled code is used - only the values
1603 ;; actually used in the code are present, and the file(s) containing
1604 ;; the `c-lang-defconst' forms don't need to be loaded at all then.
1605 ;; There are however safeguards to make sure that they can be loaded
1606 ;; to get the source definitions for the values if there's a mismatch
1607 ;; in compiled versions, or if `c-lang-const' is used uncompiled.
1609 ;; Note that the source definitions in a `c-lang-defconst' form are
1610 ;; compiled into the .elc file where it stands; there's no need to
1611 ;; load the source file to get it.
1613 ;; See cc-langs.el for more details about how this system is deployed
1614 ;; in CC Mode, and how the associated language variable system
1615 ;; (`c-lang-defvar') works. That file also contains a lot of
1616 ;; examples.
1618 (defun c-add-language (mode base-mode)
1619 "Declare a new language in the language dependent variable system.
1620 This is intended to be used by modes that inherit CC Mode to add new
1621 languages. It should be used at the top level before any calls to
1622 `c-lang-defconst'. MODE is the mode name symbol for the new language,
1623 and BASE-MODE is the mode name symbol for the language in CC Mode that
1624 is to be the template for the new mode.
1626 The exact effect of BASE-MODE is to make all language constants that
1627 haven't got a setting in the new language fall back to their values in
1628 BASE-MODE. It does not have any effect outside the language constant
1629 system."
1630 (unless (string-match "\\`\\(.*-\\)mode\\'" (symbol-name mode))
1631 (error "The mode name symbol `%s' must end with \"-mode\"" mode))
1632 (put mode 'c-mode-prefix (match-string 1 (symbol-name mode)))
1633 (unless (get base-mode 'c-mode-prefix)
1634 (error "Unknown base mode `%s'" base-mode))
1635 (put mode 'c-fallback-mode base-mode))
1637 (defvar c-lang-constants (make-vector 151 0))
1638 ;; This obarray is a cache to keep track of the language constants
1639 ;; defined by `c-lang-defconst' and the evaluated values returned by
1640 ;; `c-lang-const'. It's mostly used at compile time but it's not
1641 ;; stored in compiled files.
1643 ;; The obarray contains all the language constants as symbols. The
1644 ;; value cells hold the evaluated values as alists where each car is
1645 ;; the mode name symbol and the corresponding cdr is the evaluated
1646 ;; value in that mode. The property lists hold the source definitions
1647 ;; and other miscellaneous data. The obarray might also contain
1648 ;; various other symbols, but those don't have any variable bindings.
1650 (defvar c-lang-const-expansion nil)
1652 (defsubst c-get-current-file ()
1653 ;; Return the base name of the current file.
1654 (let ((file (cond
1655 (load-in-progress
1656 ;; Being loaded.
1657 load-file-name)
1658 ((and (boundp 'byte-compile-dest-file)
1659 (stringp byte-compile-dest-file))
1660 ;; Being compiled.
1661 byte-compile-dest-file)
1663 ;; Being evaluated interactively.
1664 (buffer-file-name)))))
1665 (and file
1666 (file-name-sans-extension
1667 (file-name-nondirectory file)))))
1669 (defmacro c-lang-defconst-eval-immediately (form)
1670 "Can be used inside a VAL in `c-lang-defconst' to evaluate FORM
1671 immediately, i.e. at the same time as the `c-lang-defconst' form
1672 itself is evaluated."
1673 ;; Evaluate at macro expansion time, i.e. in the
1674 ;; `cl-macroexpand-all' inside `c-lang-defconst'.
1675 (eval form))
1677 (defmacro c-lang-defconst (name &rest args)
1678 "Set the language specific values of the language constant NAME.
1679 The second argument can optionally be a docstring. The rest of the
1680 arguments are one or more repetitions of LANG VAL where LANG specifies
1681 the language(s) that VAL applies to. LANG is the name of the
1682 language, i.e. the mode name without the \"-mode\" suffix, or a list
1683 of such language names, or `t' for all languages. VAL is a form to
1684 evaluate to get the value.
1686 If LANG isn't `t' or one of the core languages in CC Mode, it must
1687 have been declared with `c-add-language'.
1689 Neither NAME, LANG nor VAL are evaluated directly - they should not be
1690 quoted. `c-lang-defconst-eval-immediately' can however be used inside
1691 VAL to evaluate parts of it directly.
1693 When VAL is evaluated for some language, that language is temporarily
1694 made current so that `c-lang-const' without an explicit language can
1695 be used inside VAL to refer to the value of a language constant in the
1696 same language. That is particularly useful if LANG is `t'.
1698 VAL is not evaluated right away but rather when the value is requested
1699 with `c-lang-const'. Thus it's possible to use `c-lang-const' inside
1700 VAL to refer to language constants that haven't been defined yet.
1701 However, if the definition of a language constant is in another file
1702 then that file must be loaded \(at compile time) before it's safe to
1703 reference the constant.
1705 The assignments in ARGS are processed in sequence like `setq', so
1706 \(c-lang-const NAME) may be used inside a VAL to refer to the last
1707 assigned value to this language constant, or a value that it has
1708 gotten in another earlier loaded file.
1710 To work well with repeated loads and interactive reevaluation, only
1711 one `c-lang-defconst' for each NAME is permitted per file. If there
1712 already is one it will be completely replaced; the value in the
1713 earlier definition will not affect `c-lang-const' on the same
1714 constant. A file is identified by its base name."
1716 (let* ((sym (intern (symbol-name name) c-lang-constants))
1717 ;; Make `c-lang-const' expand to a straightforward call to
1718 ;; `c-get-lang-constant' in `cl-macroexpand-all' below.
1720 ;; (The default behavior, i.e. to expand to a call inside
1721 ;; `eval-when-compile' should be equivalent, since that macro
1722 ;; should only expand to its content if it's used inside a
1723 ;; form that's already evaluated at compile time. It's
1724 ;; however necessary to use our cover macro
1725 ;; `cc-eval-when-compile' due to bugs in `eval-when-compile',
1726 ;; and it expands to a bulkier form that in this case only is
1727 ;; unnecessary garbage that we don't want to store in the
1728 ;; language constant source definitions.)
1729 (c-lang-const-expansion 'call)
1730 (c-langs-are-parametric t)
1731 bindings
1732 pre-files)
1734 (or (symbolp name)
1735 (error "Not a symbol: %s" name))
1737 (when (stringp (car-safe args))
1738 ;; The docstring is hardly used anywhere since there's no normal
1739 ;; symbol to attach it to. It's primarily for getting the right
1740 ;; format in the source.
1741 (put sym 'variable-documentation (car args))
1742 (setq args (cdr args)))
1744 (or args
1745 (error "No assignments in `c-lang-defconst' for %s" name))
1747 ;; Rework ARGS to an association list to make it easier to handle.
1748 ;; It's reversed at the same time to make it easier to implement
1749 ;; the demand-driven (i.e. reversed) evaluation in `c-lang-const'.
1750 (while args
1751 (let ((assigned-mode
1752 (cond ((eq (car args) t) t)
1753 ((symbolp (car args))
1754 (list (intern (concat (symbol-name (car args))
1755 "-mode"))))
1756 ((listp (car args))
1757 (mapcar (lambda (lang)
1758 (or (symbolp lang)
1759 (error "Not a list of symbols: %s"
1760 (car args)))
1761 (intern (concat (symbol-name lang)
1762 "-mode")))
1763 (car args)))
1764 (t (error "Not a symbol or a list of symbols: %s"
1765 (car args)))))
1766 val)
1768 (or (cdr args)
1769 (error "No value for %s" (car args)))
1770 (setq args (cdr args)
1771 val (car args))
1773 ;; Emacs has a weird bug where it seems to fail to read
1774 ;; backquote lists from byte compiled files correctly (,@
1775 ;; forms, to be specific), so make sure the bindings in the
1776 ;; expansion below don't contain any backquote stuff.
1777 ;; (XEmacs handles it correctly and doesn't need this for that
1778 ;; reason, but we also use this expansion handle
1779 ;; `c-lang-defconst-eval-immediately' and to register
1780 ;; dependencies on the `c-lang-const's in VAL.)
1781 (setq val (cl-macroexpand-all val))
1783 (setq bindings (cons (cons assigned-mode val) bindings)
1784 args (cdr args))))
1786 ;; Compile in the other files that have provided source
1787 ;; definitions for this symbol, to make sure the order in the
1788 ;; `source' property is correct even when files are loaded out of
1789 ;; order.
1790 (setq pre-files (nreverse
1791 ;; Reverse to get the right load order.
1792 (mapcar 'car (get sym 'source))))
1794 `(eval-and-compile
1795 (c-define-lang-constant ',name ',bindings
1796 ,@(and pre-files `(',pre-files))))))
1798 (put 'c-lang-defconst 'lisp-indent-function 1)
1799 ;(eval-after-load "edebug" ; 2006-07-09: def-edebug-spec is now in subr.el.
1801 (def-edebug-spec c-lang-defconst
1802 (&define name [&optional stringp] [&rest sexp def-form]))
1804 (defun c-define-lang-constant (name bindings &optional pre-files)
1805 ;; Used by `c-lang-defconst'.
1807 (let* ((sym (intern (symbol-name name) c-lang-constants))
1808 (source (get sym 'source))
1809 (file (intern
1810 (or (c-get-current-file)
1811 (error "`c-lang-defconst' must be used in a file"))))
1812 (elem (assq file source)))
1814 ;;(when (cdr-safe elem)
1815 ;; (message "Language constant %s redefined in %S" name file))
1817 ;; Note that the order in the source alist is relevant. Like how
1818 ;; `c-lang-defconst' reverses the bindings, this reverses the
1819 ;; order between files so that the last to evaluate comes first.
1820 (unless elem
1821 (while pre-files
1822 (unless (assq (car pre-files) source)
1823 (setq source (cons (list (car pre-files)) source)))
1824 (setq pre-files (cdr pre-files)))
1825 (put sym 'source (cons (setq elem (list file)) source)))
1827 (setcdr elem bindings)
1829 ;; Bind the symbol as a variable, or clear any earlier evaluated
1830 ;; value it has.
1831 (set sym nil)
1833 ;; Clear the evaluated values that depend on this source.
1834 (let ((agenda (get sym 'dependents))
1835 (visited (make-vector 101 0))
1836 ptr)
1837 (while agenda
1838 (setq sym (car agenda)
1839 agenda (cdr agenda))
1840 (intern (symbol-name sym) visited)
1841 (set sym nil)
1842 (setq ptr (get sym 'dependents))
1843 (while ptr
1844 (setq sym (car ptr)
1845 ptr (cdr ptr))
1846 (unless (intern-soft (symbol-name sym) visited)
1847 (setq agenda (cons sym agenda))))))
1849 name))
1851 (defmacro c-lang-const (name &optional lang)
1852 "Get the mode specific value of the language constant NAME in language LANG.
1853 LANG is the name of the language, i.e. the mode name without the
1854 \"-mode\" suffix. If used inside `c-lang-defconst' or
1855 `c-lang-defvar', LANG may be left out to refer to the current
1856 language. NAME and LANG are not evaluated so they should not be
1857 quoted."
1859 (or (symbolp name)
1860 (error "Not a symbol: %s" name))
1861 (or (symbolp lang)
1862 (error "Not a symbol: %s" lang))
1864 (let ((sym (intern (symbol-name name) c-lang-constants))
1865 mode source-files args)
1867 (when lang
1868 (setq mode (intern (concat (symbol-name lang) "-mode")))
1869 (unless (get mode 'c-mode-prefix)
1870 (error
1871 "Unknown language %S since it got no `c-mode-prefix' property"
1872 (symbol-name lang))))
1874 (if (eq c-lang-const-expansion 'immediate)
1875 ;; No need to find out the source file(s) when we evaluate
1876 ;; immediately since all the info is already there in the
1877 ;; `source' property.
1878 `',(c-get-lang-constant name nil mode)
1880 (let ((file (c-get-current-file)))
1881 (if file (setq file (intern file)))
1882 ;; Get the source file(s) that must be loaded to get the value
1883 ;; of the constant. If the symbol isn't defined yet we assume
1884 ;; that its definition will come later in this file, and thus
1885 ;; are no file dependencies needed.
1886 (setq source-files (nreverse
1887 ;; Reverse to get the right load order.
1888 (apply 'nconc
1889 (mapcar (lambda (elem)
1890 (if (eq file (car elem))
1891 nil ; Exclude our own file.
1892 (list (car elem))))
1893 (get sym 'source))))))
1895 ;; Make some effort to do a compact call to
1896 ;; `c-get-lang-constant' since it will be compiled in.
1897 (setq args (and mode `(',mode)))
1898 (if (or source-files args)
1899 (setq args (cons (and source-files `',source-files)
1900 args)))
1902 (if (or (eq c-lang-const-expansion 'call)
1903 (and (not c-lang-const-expansion)
1904 (not mode))
1905 load-in-progress
1906 (not (boundp 'byte-compile-dest-file))
1907 (not (stringp byte-compile-dest-file)))
1908 ;; Either a straight call is requested in the context, or
1909 ;; we're in an "uncontrolled" context and got no language,
1910 ;; or we're not being byte compiled so the compile time
1911 ;; stuff below is unnecessary.
1912 `(c-get-lang-constant ',name ,@args)
1914 ;; Being compiled. If the loading and compiling version is
1915 ;; the same we use a value that is evaluated at compile time,
1916 ;; otherwise it's evaluated at runtime.
1917 `(if (eq c-version-sym ',c-version-sym)
1918 (cc-eval-when-compile
1919 (c-get-lang-constant ',name ,@args))
1920 (c-get-lang-constant ',name ,@args))))))
1922 (defvar c-lang-constants-under-evaluation nil)
1924 (defun c-get-lang-constant (name &optional source-files mode)
1925 ;; Used by `c-lang-const'.
1927 (or mode
1928 (setq mode c-buffer-is-cc-mode)
1929 (error "No current language"))
1931 (let* ((sym (intern (symbol-name name) c-lang-constants))
1932 (source (get sym 'source))
1933 elem
1934 (eval-in-sym (and c-lang-constants-under-evaluation
1935 (caar c-lang-constants-under-evaluation))))
1937 ;; Record the dependencies between this symbol and the one we're
1938 ;; being evaluated in.
1939 (when eval-in-sym
1940 (or (memq eval-in-sym (get sym 'dependents))
1941 (put sym 'dependents (cons eval-in-sym (get sym 'dependents)))))
1943 ;; Make sure the source files have entries on the `source'
1944 ;; property so that loading will take place when necessary.
1945 (while source-files
1946 (unless (assq (car source-files) source)
1947 (put sym 'source
1948 (setq source (cons (list (car source-files)) source)))
1949 ;; Might pull in more definitions which affect the value. The
1950 ;; clearing of dependent values etc is done when the
1951 ;; definition is encountered during the load; this is just to
1952 ;; jump past the check for a cached value below.
1953 (set sym nil))
1954 (setq source-files (cdr source-files)))
1956 (if (and (boundp sym)
1957 (setq elem (assq mode (symbol-value sym))))
1958 (cdr elem)
1960 ;; Check if an evaluation of this symbol is already underway.
1961 ;; In that case we just continue with the "assignment" before
1962 ;; the one currently being evaluated, thereby creating the
1963 ;; illusion if a `setq'-like sequence of assignments.
1964 (let* ((c-buffer-is-cc-mode mode)
1965 (source-pos
1966 (or (assq sym c-lang-constants-under-evaluation)
1967 (cons sym (vector source nil))))
1968 ;; Append `c-lang-constants-under-evaluation' even if an
1969 ;; earlier entry is found. It's only necessary to get
1970 ;; the recording of dependencies above correct.
1971 (c-lang-constants-under-evaluation
1972 (cons source-pos c-lang-constants-under-evaluation))
1973 (fallback (get mode 'c-fallback-mode))
1974 value
1975 ;; Make sure the recursion limits aren't very low
1976 ;; since the `c-lang-const' dependencies can go deep.
1977 (max-specpdl-size (max max-specpdl-size 3000))
1978 (max-lisp-eval-depth (max max-lisp-eval-depth 1000)))
1980 (if (if fallback
1981 (let ((backup-source-pos (copy-sequence (cdr source-pos))))
1982 (and
1983 ;; First try the original mode but don't accept an
1984 ;; entry matching all languages since the fallback
1985 ;; mode might have an explicit entry before that.
1986 (eq (setq value (c-find-assignment-for-mode
1987 (cdr source-pos) mode nil name))
1988 c-lang-constants)
1989 ;; Try again with the fallback mode from the
1990 ;; original position. Note that
1991 ;; `c-buffer-is-cc-mode' still is the real mode if
1992 ;; language parameterization takes place.
1993 (eq (setq value (c-find-assignment-for-mode
1994 (setcdr source-pos backup-source-pos)
1995 fallback t name))
1996 c-lang-constants)))
1997 ;; A simple lookup with no fallback mode.
1998 (eq (setq value (c-find-assignment-for-mode
1999 (cdr source-pos) mode t name))
2000 c-lang-constants))
2001 (error
2002 "`%s' got no (prior) value in %s (might be a cyclic reference)"
2003 name mode))
2005 (condition-case err
2006 (setq value (eval value))
2007 (error
2008 ;; Print a message to aid in locating the error. We don't
2009 ;; print the error itself since that will be done later by
2010 ;; some caller higher up.
2011 (message "Eval error in the `c-lang-defconst' for `%s' in %s:"
2012 sym mode)
2013 (makunbound sym)
2014 (signal (car err) (cdr err))))
2016 (set sym (cons (cons mode value) (symbol-value sym)))
2017 value))))
2019 (defun c-find-assignment-for-mode (source-pos mode match-any-lang name)
2020 ;; Find the first assignment entry that applies to MODE at or after
2021 ;; SOURCE-POS. If MATCH-ANY-LANG is non-nil, entries with `t' as
2022 ;; the language list are considered to match, otherwise they don't.
2023 ;; On return SOURCE-POS is updated to point to the next assignment
2024 ;; after the returned one. If no assignment is found,
2025 ;; `c-lang-constants' is returned as a magic value.
2027 ;; SOURCE-POS is a vector that points out a specific assignment in
2028 ;; the double alist that's used in the `source' property. The first
2029 ;; element is the position in the top alist which is indexed with
2030 ;; the source files, and the second element is the position in the
2031 ;; nested bindings alist.
2033 ;; NAME is only used for error messages.
2035 (catch 'found
2036 (let ((file-entry (elt source-pos 0))
2037 (assignment-entry (elt source-pos 1))
2038 assignment)
2040 (while (if assignment-entry
2042 ;; Handled the last assignment from one file, begin on the
2043 ;; next. Due to the check in `c-lang-defconst', we know
2044 ;; there's at least one.
2045 (when file-entry
2047 (unless (aset source-pos 1
2048 (setq assignment-entry (cdar file-entry)))
2049 ;; The file containing the source definitions has not
2050 ;; been loaded.
2051 (let ((file (symbol-name (caar file-entry)))
2052 (c-lang-constants-under-evaluation nil))
2053 ;;(message (concat "Loading %s to get the source "
2054 ;; "value for language constant %s")
2055 ;; file name)
2056 (load file))
2058 (unless (setq assignment-entry (cdar file-entry))
2059 ;; The load didn't fill in the source for the
2060 ;; constant as expected. The situation is
2061 ;; probably that a derived mode was written for
2062 ;; and compiled with another version of CC Mode,
2063 ;; and the requested constant isn't in the
2064 ;; currently loaded one. Put in a dummy
2065 ;; assignment that matches no language.
2066 (setcdr (car file-entry)
2067 (setq assignment-entry (list (list nil))))))
2069 (aset source-pos 0 (setq file-entry (cdr file-entry)))
2072 (setq assignment (car assignment-entry))
2073 (aset source-pos 1
2074 (setq assignment-entry (cdr assignment-entry)))
2076 (when (if (listp (car assignment))
2077 (memq mode (car assignment))
2078 match-any-lang)
2079 (throw 'found (cdr assignment))))
2081 c-lang-constants)))
2083 (defun c-lang-major-mode-is (mode)
2084 ;; `c-major-mode-is' expands to a call to this function inside
2085 ;; `c-lang-defconst'. Here we also match the mode(s) against any
2086 ;; fallback modes for the one in `c-buffer-is-cc-mode', so that
2087 ;; e.g. (c-major-mode-is 'c++-mode) is true in a derived language
2088 ;; that has c++-mode as base mode.
2089 (unless (listp mode)
2090 (setq mode (list mode)))
2091 (let (match (buf-mode c-buffer-is-cc-mode))
2092 (while (if (memq buf-mode mode)
2093 (progn
2094 (setq match t)
2095 nil)
2096 (setq buf-mode (get buf-mode 'c-fallback-mode))))
2097 match))
2100 (cc-provide 'cc-defs)
2102 ;;; arch-tag: 3bb2629d-dd84-4ff0-ad39-584be0fe3cda
2103 ;;; cc-defs.el ends here