Delete Emacs 25 test in mode-local.el
[emacs.git] / lisp / progmodes / cc-defs.el
blob0422ca0b02896be72ec3721fc2c4931d419d3562
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
8 ;; 1987 Dave Detlefs
9 ;; 1987 Stewart Clamen
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
14 ;; Package: cc-mode
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/>.
31 ;;; Commentary:
33 ;; This file contains macros, defsubsts, and various other things that
34 ;; must be loaded early both during compilation and at runtime.
36 ;;; Code:
38 (eval-when-compile
39 (let ((load-path
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)
43 load-path)))
44 (load "cc-bytecomp" nil t)))
46 (eval-and-compile
47 (defvar c--mapcan-status
48 (cond ((and (fboundp 'mapcan)
49 (subrp (symbol-function 'mapcan)))
50 ;; XEmacs
51 'mapcan)
52 ((locate-file "cl-lib.elc" load-path)
53 ;; Emacs >= 24.3
54 'cl-mapcan)
56 ;; Emacs <= 24.2
57 nil))))
59 (cc-external-require (if (eq c--mapcan-status 'cl-mapcan) 'cl-lib 'cl))
60 ; was (cc-external-require 'cl). ACM 2005/11/29.
61 ; Changed from (eval-when-compile (require 'cl)) back to
62 ; cc-external-require, 2015-08-12.
63 (cc-external-require 'regexp-opt)
65 ;; Silence the compiler.
66 (cc-bytecomp-defvar c-enable-xemacs-performance-kludge-p) ; In cc-vars.el
67 (cc-bytecomp-defun region-active-p) ; XEmacs
68 (cc-bytecomp-defvar mark-active) ; Emacs
69 (cc-bytecomp-defvar deactivate-mark) ; Emacs
70 (cc-bytecomp-defvar inhibit-point-motion-hooks) ; Emacs
71 (cc-bytecomp-defvar parse-sexp-lookup-properties) ; Emacs
72 (cc-bytecomp-defvar text-property-default-nonsticky) ; Emacs 21
73 (cc-bytecomp-defun string-to-syntax) ; Emacs 21
76 ;; cc-fix.el contains compatibility macros that should be used if
77 ;; needed.
78 (eval-and-compile
79 (if (or (/= (regexp-opt-depth "\\(\\(\\)\\)") 2)
80 (not (fboundp 'push)))
81 (cc-load "cc-fix")))
83 (when (featurep 'xemacs) ; There is now (2005/12) code in GNU Emacs CVS
84 ; to make the call to f-l-c-k throw an error.
85 (eval-after-load "font-lock"
86 '(if (and (not (featurep 'cc-fix)) ; only load the file once.
87 (let (font-lock-keywords)
88 (font-lock-compile-keywords '("\\<\\>"))
89 font-lock-keywords)) ; did the previous call foul this up?
90 (load "cc-fix"))))
92 ;; The above takes care of the delayed loading, but this is necessary
93 ;; to ensure correct byte compilation.
94 (eval-when-compile
95 (if (and (featurep 'xemacs)
96 (not (featurep 'cc-fix))
97 (progn
98 (require 'font-lock)
99 (let (font-lock-keywords)
100 (font-lock-compile-keywords '("\\<\\>"))
101 font-lock-keywords)))
102 (cc-load "cc-fix")))
104 ;; XEmacs 21.4 doesn't have `delete-dups'.
105 (eval-and-compile
106 (if (and (not (fboundp 'delete-dups))
107 (not (featurep 'cc-fix)))
108 (cc-load "cc-fix")))
110 ;;; Variables also used at compile time.
112 (defconst c-version "5.33"
113 "CC Mode version number.")
115 (defconst c-version-sym (intern c-version))
116 ;; A little more compact and faster in comparisons.
118 (defvar c-buffer-is-cc-mode nil
119 "Non-nil for all buffers with a major mode derived from CC Mode.
120 Otherwise, this variable is nil. I.e. this variable is non-nil for
121 `c-mode', `c++-mode', `objc-mode', `java-mode', `idl-mode',
122 `pike-mode', `awk-mode', and any other non-CC Mode mode that calls
123 `c-initialize-cc-mode'. The value is the mode symbol itself
124 \(i.e. `c-mode' etc) of the original CC Mode mode, or just t if it's
125 not known.")
126 (make-variable-buffer-local 'c-buffer-is-cc-mode)
128 ;; Have to make `c-buffer-is-cc-mode' permanently local so that it
129 ;; survives the initialization of the derived mode.
130 (put 'c-buffer-is-cc-mode 'permanent-local t)
133 ;; The following is used below during compilation.
134 (eval-and-compile
135 (defvar c-inside-eval-when-compile nil)
137 (defmacro cc-eval-when-compile (&rest body)
138 "Like `progn', but evaluates the body at compile time.
139 The result of the body appears to the compiler as a quoted constant.
141 This variant works around bugs in `eval-when-compile' in various
142 \(X)Emacs versions. See cc-defs.el for details."
144 (if c-inside-eval-when-compile
145 ;; XEmacs 21.4.6 has a bug in `eval-when-compile' in that it
146 ;; evaluates its body at macro expansion time if it's nested
147 ;; inside another `eval-when-compile'. So we use a dynamically
148 ;; bound variable to avoid nesting them.
149 `(progn ,@body)
151 `(eval-when-compile
152 ;; In all (X)Emacsen so far, `eval-when-compile' byte compiles
153 ;; its contents before evaluating it. That can cause forms to
154 ;; be compiled in situations they aren't intended to be
155 ;; compiled.
157 ;; Example: It's not possible to defsubst a primitive, e.g. the
158 ;; following will produce an error (in any emacs flavor), since
159 ;; `nthcdr' is a primitive function that's handled specially by
160 ;; the byte compiler and thus can't be redefined:
162 ;; (defsubst nthcdr (val) val)
164 ;; `defsubst', like `defmacro', needs to be evaluated at
165 ;; compile time, so this will produce an error during byte
166 ;; compilation.
168 ;; CC Mode occasionally needs to do things like this for
169 ;; cross-emacs compatibility. It therefore uses the following
170 ;; to conditionally do a `defsubst':
172 ;; (eval-when-compile
173 ;; (if (not (fboundp 'foo))
174 ;; (defsubst foo ...)))
176 ;; But `eval-when-compile' byte compiles its contents and
177 ;; _then_ evaluates it (in all current emacs versions, up to
178 ;; and including Emacs 20.6 and XEmacs 21.1 as of this
179 ;; writing). So this will still produce an error, since the
180 ;; byte compiler will get to the defsubst anyway. That's
181 ;; arguably a bug because the point with `eval-when-compile' is
182 ;; that it should evaluate rather than compile its contents.
184 ;; We get around it by expanding the body to a quoted
185 ;; constant that we eval. That otoh introduce a problem in
186 ;; that a returned lambda expression doesn't get byte
187 ;; compiled (even if `function' is used).
188 (eval '(let ((c-inside-eval-when-compile t)) ,@body)))))
190 (put 'cc-eval-when-compile 'lisp-indent-hook 0))
193 ;;; Macros.
194 (defmacro c--mapcan (fun liszt)
195 ;; CC Mode equivalent of `mapcan' which bridges the difference
196 ;; between the host [X]Emacsen."
197 ;; The motivation for this macro is to avoid the irritating message
198 ;; "function `mapcan' from cl package called at runtime" produced by Emacs.
199 (cond
200 ((eq c--mapcan-status 'mapcan)
201 `(mapcan ,fun ,liszt))
202 ((eq c--mapcan-status 'cl-mapcan)
203 `(cl-mapcan ,fun ,liszt))
205 ;; Emacs <= 24.2. It would be nice to be able to distinguish between
206 ;; compile-time and run-time use here.
207 `(apply 'nconc (mapcar ,fun ,liszt)))))
209 (defmacro c--set-difference (liszt1 liszt2 &rest other-args)
210 ;; Macro to smooth out the renaming of `set-difference' in Emacs 24.3.
211 (if (eq c--mapcan-status 'cl-mapcan)
212 `(cl-set-difference ,liszt1 ,liszt2 ,@other-args)
213 `(set-difference ,liszt1 ,liszt2 ,@other-args)))
215 (defmacro c--intersection (liszt1 liszt2 &rest other-args)
216 ;; Macro to smooth out the renaming of `intersection' in Emacs 24.3.
217 (if (eq c--mapcan-status 'cl-mapcan)
218 `(cl-intersection ,liszt1 ,liszt2 ,@other-args)
219 `(intersection ,liszt1 ,liszt2 ,@other-args)))
221 (eval-and-compile
222 (defmacro c--macroexpand-all (form &optional environment)
223 ;; Macro to smooth out the renaming of `cl-macroexpand-all' in Emacs 24.3.
224 (if (eq c--mapcan-status 'cl-mapcan)
225 `(macroexpand-all ,form ,environment)
226 `(cl-macroexpand-all ,form ,environment)))
228 (defmacro c--delete-duplicates (cl-seq &rest cl-keys)
229 ;; Macro to smooth out the renaming of `delete-duplicates' in Emacs 24.3.
230 (if (eq c--mapcan-status 'cl-mapcan)
231 `(cl-delete-duplicates ,cl-seq ,@cl-keys)
232 `(delete-duplicates ,cl-seq ,@cl-keys))))
234 (defmacro c-point (position &optional point)
235 "Return the value of certain commonly referenced POSITIONs relative to POINT.
236 The current point is used if POINT isn't specified. POSITION can be
237 one of the following symbols:
239 `bol' -- beginning of line
240 `eol' -- end of line
241 `bod' -- beginning of defun
242 `eod' -- end of defun
243 `boi' -- beginning of indentation
244 `ionl' -- indentation of next line
245 `iopl' -- indentation of previous line
246 `bonl' -- beginning of next line
247 `eonl' -- end of next line
248 `bopl' -- beginning of previous line
249 `eopl' -- end of previous line
250 `bosws' -- beginning of syntactic whitespace
251 `eosws' -- end of syntactic whitespace
253 If the referenced position doesn't exist, the closest accessible point
254 to it is returned. This function does not modify the point or the mark."
256 (if (eq (car-safe position) 'quote)
257 (let ((position (eval position)))
258 (cond
260 ((eq position 'bol)
261 (if (and (cc-bytecomp-fboundp 'line-beginning-position) (not point))
262 `(line-beginning-position)
263 `(save-excursion
264 ,@(if point `((goto-char ,point)))
265 (beginning-of-line)
266 (point))))
268 ((eq position 'eol)
269 (if (and (cc-bytecomp-fboundp 'line-end-position) (not point))
270 `(line-end-position)
271 `(save-excursion
272 ,@(if point `((goto-char ,point)))
273 (end-of-line)
274 (point))))
276 ((eq position 'boi)
277 `(save-excursion
278 ,@(if point `((goto-char ,point)))
279 (back-to-indentation)
280 (point)))
282 ((eq position 'bod)
283 `(save-excursion
284 ,@(if point `((goto-char ,point)))
285 (c-beginning-of-defun-1)
286 (point)))
288 ((eq position 'eod)
289 `(save-excursion
290 ,@(if point `((goto-char ,point)))
291 (c-end-of-defun-1)
292 (point)))
294 ((eq position 'bopl)
295 (if (and (cc-bytecomp-fboundp 'line-beginning-position) (not point))
296 `(line-beginning-position 0)
297 `(save-excursion
298 ,@(if point `((goto-char ,point)))
299 (forward-line -1)
300 (point))))
302 ((eq position 'bonl)
303 (if (and (cc-bytecomp-fboundp 'line-beginning-position) (not point))
304 `(line-beginning-position 2)
305 `(save-excursion
306 ,@(if point `((goto-char ,point)))
307 (forward-line 1)
308 (point))))
310 ((eq position 'eopl)
311 (if (and (cc-bytecomp-fboundp 'line-end-position) (not point))
312 `(line-end-position 0)
313 `(save-excursion
314 ,@(if point `((goto-char ,point)))
315 (beginning-of-line)
316 (or (bobp) (backward-char))
317 (point))))
319 ((eq position 'eonl)
320 (if (and (cc-bytecomp-fboundp 'line-end-position) (not point))
321 `(line-end-position 2)
322 `(save-excursion
323 ,@(if point `((goto-char ,point)))
324 (forward-line 1)
325 (end-of-line)
326 (point))))
328 ((eq position 'iopl)
329 `(save-excursion
330 ,@(if point `((goto-char ,point)))
331 (forward-line -1)
332 (back-to-indentation)
333 (point)))
335 ((eq position 'ionl)
336 `(save-excursion
337 ,@(if point `((goto-char ,point)))
338 (forward-line 1)
339 (back-to-indentation)
340 (point)))
342 ((eq position 'bosws)
343 `(save-excursion
344 ,@(if point `((goto-char ,point)))
345 (c-backward-syntactic-ws)
346 (point)))
348 ((eq position 'eosws)
349 `(save-excursion
350 ,@(if point `((goto-char ,point)))
351 (c-forward-syntactic-ws)
352 (point)))
354 (t (error "Unknown buffer position requested: %s" position))))
356 ;; The bulk of this should perhaps be in a function to avoid large
357 ;; expansions, but this case is not used anywhere in CC Mode (and
358 ;; probably not anywhere else either) so we only have it to be on
359 ;; the safe side.
360 (message "Warning: c-point long expansion")
361 `(save-excursion
362 ,@(if point `((goto-char ,point)))
363 (let ((position ,position))
364 (cond
365 ((eq position 'bol) (beginning-of-line))
366 ((eq position 'eol) (end-of-line))
367 ((eq position 'boi) (back-to-indentation))
368 ((eq position 'bod) (c-beginning-of-defun-1))
369 ((eq position 'eod) (c-end-of-defun-1))
370 ((eq position 'bopl) (forward-line -1))
371 ((eq position 'bonl) (forward-line 1))
372 ((eq position 'eopl) (progn
373 (beginning-of-line)
374 (or (bobp) (backward-char))))
375 ((eq position 'eonl) (progn
376 (forward-line 1)
377 (end-of-line)))
378 ((eq position 'iopl) (progn
379 (forward-line -1)
380 (back-to-indentation)))
381 ((eq position 'ionl) (progn
382 (forward-line 1)
383 (back-to-indentation)))
384 ((eq position 'bosws) (c-backward-syntactic-ws))
385 ((eq position 'eosws) (c-forward-syntactic-ws))
386 (t (error "Unknown buffer position requested: %s" position))))
387 (point))))
389 (eval-and-compile
390 ;; Constant to decide at compilation time whether to use category
391 ;; properties. Currently (2010-03) they're available only on GNU Emacs.
392 (defconst c-use-category
393 (with-temp-buffer
394 (let ((parse-sexp-lookup-properties t)
395 (lookup-syntax-properties t))
396 (set-syntax-table (make-syntax-table))
397 (insert "<()>")
398 (put-text-property (point-min) (1+ (point-min))
399 'category 'c-<-as-paren-syntax)
400 (put-text-property (+ 3 (point-min)) (+ 4 (point-min))
401 'category 'c->-as-paren-syntax)
402 (goto-char (point-min))
403 (forward-sexp)
404 (= (point) (+ 4 (point-min)))))))
406 (defvar c-use-extents)
408 (defmacro c-next-single-property-change (position prop &optional object limit)
409 ;; See the doc string for either of the defuns expanded to.
410 (if (and c-use-extents
411 (fboundp 'next-single-char-property-change))
412 ;; XEmacs >= 2005-01-25
413 `(next-single-char-property-change ,position ,prop ,object ,limit)
414 ;; Emacs and earlier XEmacs
415 `(next-single-property-change ,position ,prop ,object ,limit)))
417 (defmacro c-region-is-active-p ()
418 ;; Return t when the region is active. The determination of region
419 ;; activeness is different in both Emacs and XEmacs.
420 (if (cc-bytecomp-fboundp 'region-active-p)
421 ;; XEmacs.
422 '(region-active-p)
423 ;; Old Emacs.
424 'mark-active))
426 (defmacro c-set-region-active (activate)
427 ;; Activate the region if ACTIVE is non-nil, deactivate it
428 ;; otherwise. Covers the differences between Emacs and XEmacs.
429 (if (fboundp 'zmacs-activate-region)
430 ;; XEmacs.
431 `(if ,activate
432 (zmacs-activate-region)
433 (zmacs-deactivate-region))
434 ;; Emacs.
435 `(setq mark-active ,activate)))
437 (defmacro c-delete-and-extract-region (start end)
438 "Delete the text between START and END and return it."
439 (if (cc-bytecomp-fboundp 'delete-and-extract-region)
440 ;; Emacs 21.1 and later
441 `(delete-and-extract-region ,start ,end)
442 ;; XEmacs and Emacs 20.x
443 `(prog1
444 (buffer-substring ,start ,end)
445 (delete-region ,start ,end))))
447 (defmacro c-safe (&rest body)
448 ;; safely execute BODY, return nil if an error occurred
449 `(condition-case nil
450 (progn ,@body)
451 (error nil)))
452 (put 'c-safe 'lisp-indent-function 0)
454 (defmacro c-int-to-char (integer)
455 ;; In Emacs, a character is an integer. In XEmacs, a character is a
456 ;; type distinct from an integer. Sometimes we need to convert integers to
457 ;; characters. `c-int-to-char' makes this conversion, if necessary.
458 (if (fboundp 'int-to-char)
459 `(int-to-char ,integer)
460 integer))
462 (defmacro c-last-command-char ()
463 ;; The last character just typed. Note that `last-command-event' exists in
464 ;; both Emacs and XEmacs, but with confusingly different meanings.
465 (if (featurep 'xemacs)
466 'last-command-char
467 'last-command-event))
469 (defmacro c-sentence-end ()
470 ;; Get the regular expression `sentence-end'.
471 (if (cc-bytecomp-fboundp 'sentence-end)
472 ;; Emacs 22:
473 `(sentence-end)
474 ;; Emacs <22 + XEmacs
475 `sentence-end))
477 (defmacro c-default-value-sentence-end ()
478 ;; Get the default value of the variable sentence end.
479 (if (cc-bytecomp-fboundp 'sentence-end)
480 ;; Emacs 22:
481 `(let (sentence-end) (sentence-end))
482 ;; Emacs <22 + XEmacs
483 `(default-value 'sentence-end)))
485 ;; The following is essentially `save-buffer-state' from lazy-lock.el.
486 ;; It ought to be a standard macro.
487 (defmacro c-save-buffer-state (varlist &rest body)
488 "Bind variables according to VARLIST (in `let*' style) and eval BODY,
489 then restore the buffer state under the assumption that no significant
490 modification has been made in BODY. A change is considered
491 significant if it affects the buffer text in any way that isn't
492 completely restored again. Changes in text properties like `face' or
493 `syntax-table' are considered insignificant. This macro allows text
494 properties to be changed, even in a read-only buffer.
496 This macro should be placed around all calculations which set
497 \"insignificant\" text properties in a buffer, even when the buffer is
498 known to be writable. That way, these text properties remain set
499 even if the user undoes the command which set them.
501 This macro should ALWAYS be placed around \"temporary\" internal buffer
502 changes \(like adding a newline to calculate a text-property then
503 deleting it again\), so that the user never sees them on his
504 `buffer-undo-list'. See also `c-tentative-buffer-changes'.
506 However, any user-visible changes to the buffer \(like auto-newlines\)
507 must not be within a `c-save-buffer-state', since the user then
508 wouldn't be able to undo them.
510 The return value is the value of the last form in BODY."
511 `(let* ((modified (buffer-modified-p)) (buffer-undo-list t)
512 (inhibit-read-only t) (inhibit-point-motion-hooks t)
513 before-change-functions after-change-functions
514 deactivate-mark
515 buffer-file-name buffer-file-truename ; Prevent primitives checking
516 ; for file modification
517 ,@varlist)
518 (unwind-protect
519 (progn ,@body)
520 (and (not modified)
521 (buffer-modified-p)
522 (set-buffer-modified-p nil)))))
523 (put 'c-save-buffer-state 'lisp-indent-function 1)
525 (defmacro c-tentative-buffer-changes (&rest body)
526 "Eval BODY and optionally restore the buffer contents to the state it
527 was in before BODY. Any changes are kept if the last form in BODY
528 returns non-nil. Otherwise it's undone using the undo facility, and
529 various other buffer state that might be affected by the changes is
530 restored. That includes the current buffer, point, mark, mark
531 activation \(similar to `save-excursion'), and the modified state.
532 The state is also restored if BODY exits nonlocally.
534 If BODY makes a change that unconditionally is undone then wrap this
535 macro inside `c-save-buffer-state'. That way the change can be done
536 even when the buffer is read-only, and without interference from
537 various buffer change hooks."
538 `(let (-tnt-chng-keep
539 -tnt-chng-state)
540 (unwind-protect
541 ;; Insert an undo boundary for use with `undo-more'. We
542 ;; don't use `undo-boundary' since it doesn't insert one
543 ;; unconditionally.
544 (setq buffer-undo-list (cons nil buffer-undo-list)
545 -tnt-chng-state (c-tnt-chng-record-state)
546 -tnt-chng-keep (progn ,@body))
547 (c-tnt-chng-cleanup -tnt-chng-keep -tnt-chng-state))))
548 (put 'c-tentative-buffer-changes 'lisp-indent-function 0)
550 (defun c-tnt-chng-record-state ()
551 ;; Used internally in `c-tentative-buffer-changes'.
552 (vector buffer-undo-list ; 0
553 (current-buffer) ; 1
554 ;; No need to use markers for the point and mark; if the
555 ;; undo got out of synch we're hosed anyway.
556 (point) ; 2
557 (mark t) ; 3
558 (c-region-is-active-p) ; 4
559 (buffer-modified-p))) ; 5
561 (defun c-tnt-chng-cleanup (keep saved-state)
562 ;; Used internally in `c-tentative-buffer-changes'.
564 (let ((saved-undo-list (elt saved-state 0)))
565 (if (eq buffer-undo-list saved-undo-list)
566 ;; No change was done after all.
567 (setq buffer-undo-list (cdr saved-undo-list))
569 (if keep
570 ;; Find and remove the undo boundary.
571 (let ((p buffer-undo-list))
572 (while (not (eq (cdr p) saved-undo-list))
573 (setq p (cdr p)))
574 (setcdr p (cdr saved-undo-list)))
576 ;; `primitive-undo' will remove the boundary.
577 (setq saved-undo-list (cdr saved-undo-list))
578 (let ((undo-in-progress t))
579 (while (not (eq (setq buffer-undo-list
580 (primitive-undo 1 buffer-undo-list))
581 saved-undo-list))))
583 (when (buffer-live-p (elt saved-state 1))
584 (set-buffer (elt saved-state 1))
585 (goto-char (elt saved-state 2))
586 (set-mark (elt saved-state 3))
587 (c-set-region-active (elt saved-state 4))
588 (and (not (elt saved-state 5))
589 (buffer-modified-p)
590 (set-buffer-modified-p nil)))))))
592 (defmacro c-forward-syntactic-ws (&optional limit)
593 "Forward skip over syntactic whitespace.
594 Syntactic whitespace is defined as whitespace characters, comments,
595 and preprocessor directives. However if point starts inside a comment
596 or preprocessor directive, the content of it is not treated as
597 whitespace.
599 LIMIT sets an upper limit of the forward movement, if specified. If
600 LIMIT or the end of the buffer is reached inside a comment or
601 preprocessor directive, the point will be left there.
603 Note that this function might do hidden buffer changes. See the
604 comment at the start of cc-engine.el for more info."
605 (if limit
606 `(save-restriction
607 (narrow-to-region (point-min) (or ,limit (point-max)))
608 (c-forward-sws))
609 '(c-forward-sws)))
611 (defmacro c-backward-syntactic-ws (&optional limit)
612 "Backward skip over syntactic whitespace.
613 Syntactic whitespace is defined as whitespace characters, comments,
614 and preprocessor directives. However if point starts inside a comment
615 or preprocessor directive, the content of it is not treated as
616 whitespace.
618 LIMIT sets a lower limit of the backward movement, if specified. If
619 LIMIT is reached inside a line comment or preprocessor directive then
620 the point is moved into it past the whitespace at the end.
622 Note that this function might do hidden buffer changes. See the
623 comment at the start of cc-engine.el for more info."
624 (if limit
625 `(save-restriction
626 (narrow-to-region (or ,limit (point-min)) (point-max))
627 (c-backward-sws))
628 '(c-backward-sws)))
630 (defmacro c-forward-sexp (&optional count)
631 "Move forward across COUNT balanced expressions.
632 A negative COUNT means move backward. Signal an error if the move
633 fails for any reason.
635 This is like `forward-sexp' except that it isn't interactive and does
636 not do any user friendly adjustments of the point and that it isn't
637 susceptible to user configurations such as disabling of signals in
638 certain situations."
639 (or count (setq count 1))
640 `(goto-char (scan-sexps (point) ,count)))
642 (defmacro c-backward-sexp (&optional count)
643 "See `c-forward-sexp' and reverse directions."
644 (or count (setq count 1))
645 `(c-forward-sexp ,(if (numberp count) (- count) `(- ,count))))
647 (defmacro c-safe-scan-lists (from count depth &optional limit)
648 "Like `scan-lists' but returns nil instead of signaling errors
649 for unbalanced parens.
651 A limit for the search may be given. FROM is assumed to be on the
652 right side of it."
653 (let ((res (if (featurep 'xemacs)
654 `(scan-lists ,from ,count ,depth nil t)
655 `(c-safe (scan-lists ,from ,count ,depth)))))
656 (if limit
657 `(save-restriction
658 ,(if (numberp count)
659 (if (< count 0)
660 `(narrow-to-region ,limit (point-max))
661 `(narrow-to-region (point-min) ,limit))
662 `(if (< ,count 0)
663 (narrow-to-region ,limit (point-max))
664 (narrow-to-region (point-min) ,limit)))
665 ,res)
666 res)))
669 ;; Wrappers for common scan-lists cases, mainly because it's almost
670 ;; impossible to get a feel for how that function works.
672 (defmacro c-go-list-forward ()
673 "Move backward across one balanced group of parentheses.
675 Return POINT when we succeed, NIL when we fail. In the latter case, leave
676 point unmoved."
677 `(c-safe (let ((endpos (scan-lists (point) 1 0)))
678 (goto-char endpos)
679 endpos)))
681 (defmacro c-go-list-backward ()
682 "Move backward across one balanced group of parentheses.
684 Return POINT when we succeed, NIL when we fail. In the latter case, leave
685 point unmoved."
686 `(c-safe (let ((endpos (scan-lists (point) -1 0)))
687 (goto-char endpos)
688 endpos)))
690 (defmacro c-up-list-forward (&optional pos limit)
691 "Return the first position after the list sexp containing POS,
692 or nil if no such position exists. The point is used if POS is left out.
694 A limit for the search may be given. The start position is assumed to
695 be before it."
696 `(c-safe-scan-lists ,(or pos `(point)) 1 1 ,limit))
698 (defmacro c-up-list-backward (&optional pos limit)
699 "Return the position of the start of the list sexp containing POS,
700 or nil if no such position exists. The point is used if POS is left out.
702 A limit for the search may be given. The start position is assumed to
703 be after it."
704 `(c-safe-scan-lists ,(or pos `(point)) -1 1 ,limit))
706 (defmacro c-down-list-forward (&optional pos limit)
707 "Return the first position inside the first list sexp after POS,
708 or nil if no such position exists. The point is used if POS is left out.
710 A limit for the search may be given. The start position is assumed to
711 be before it."
712 `(c-safe-scan-lists ,(or pos `(point)) 1 -1 ,limit))
714 (defmacro c-down-list-backward (&optional pos limit)
715 "Return the last position inside the last list sexp before POS,
716 or nil if no such position exists. The point is used if POS is left out.
718 A limit for the search may be given. The start position is assumed to
719 be after it."
720 `(c-safe-scan-lists ,(or pos `(point)) -1 -1 ,limit))
722 (defmacro c-go-up-list-forward (&optional pos limit)
723 "Move the point to the first position after the list sexp containing POS,
724 or containing the point if POS is left out. Return t if such a
725 position exists, otherwise nil is returned and the point isn't moved.
727 A limit for the search may be given. The start position is assumed to
728 be before it."
729 (let ((res `(c-safe (goto-char (scan-lists ,(or pos `(point)) 1 1)) t)))
730 (if limit
731 `(save-restriction
732 (narrow-to-region (point-min) ,limit)
733 ,res)
734 res)))
736 (defmacro c-go-up-list-backward (&optional pos limit)
737 "Move the point to the position of the start of the list sexp containing POS,
738 or containing the point if POS is left out. Return t if such a
739 position exists, otherwise nil is returned and the point isn't moved.
741 A limit for the search may be given. The start position is assumed to
742 be after it."
743 (let ((res `(c-safe (goto-char (scan-lists ,(or pos `(point)) -1 1)) t)))
744 (if limit
745 `(save-restriction
746 (narrow-to-region ,limit (point-max))
747 ,res)
748 res)))
750 (defmacro c-go-down-list-forward (&optional pos limit)
751 "Move the point to the first position inside the first list sexp after POS,
752 or before the point if POS is left out. Return t if such a position
753 exists, otherwise nil is returned and the point isn't moved.
755 A limit for the search may be given. The start position is assumed to
756 be before it."
757 (let ((res `(c-safe (goto-char (scan-lists ,(or pos `(point)) 1 -1)) t)))
758 (if limit
759 `(save-restriction
760 (narrow-to-region (point-min) ,limit)
761 ,res)
762 res)))
764 (defmacro c-go-down-list-backward (&optional pos limit)
765 "Move the point to the last position inside the last list sexp before POS,
766 or before the point if POS is left out. Return t if such a position
767 exists, otherwise nil is returned and the point isn't moved.
769 A limit for the search may be given. The start position is assumed to
770 be after it."
771 (let ((res `(c-safe (goto-char (scan-lists ,(or pos `(point)) -1 -1)) t)))
772 (if limit
773 `(save-restriction
774 (narrow-to-region ,limit (point-max))
775 ,res)
776 res)))
779 (defmacro c-beginning-of-defun-1 ()
780 ;; Wrapper around beginning-of-defun.
782 ;; NOTE: This function should contain the only explicit use of
783 ;; beginning-of-defun in CC Mode. Eventually something better than
784 ;; b-o-d will be available and this should be the only place the
785 ;; code needs to change. Everything else should use
786 ;; (c-beginning-of-defun-1)
788 ;; This is really a bit too large to be a macro but that isn't a
789 ;; problem as long as it only is used in one place in
790 ;; `c-parse-state'.
792 `(progn
793 (if (and ,(fboundp 'buffer-syntactic-context-depth)
794 c-enable-xemacs-performance-kludge-p)
795 ,(when (fboundp 'buffer-syntactic-context-depth)
796 ;; XEmacs only. This can improve the performance of
797 ;; c-parse-state to between 3 and 60 times faster when
798 ;; braces are hung. It can also degrade performance by
799 ;; about as much when braces are not hung.
800 '(let (beginning-of-defun-function end-of-defun-function
801 pos)
802 (while (not pos)
803 (save-restriction
804 (widen)
805 (setq pos (c-safe-scan-lists
806 (point) -1 (buffer-syntactic-context-depth))))
807 (cond
808 ((bobp) (setq pos (point-min)))
809 ((not pos)
810 (let ((distance (skip-chars-backward "^{")))
811 ;; unbalanced parenthesis, while invalid C code,
812 ;; shouldn't cause an infloop! See unbal.c
813 (when (zerop distance)
814 ;; Punt!
815 (beginning-of-defun)
816 (setq pos (point)))))
817 ((= pos 0))
818 ((not (eq (char-after pos) ?{))
819 (goto-char pos)
820 (setq pos nil))
822 (goto-char pos)))
823 ;; Emacs, which doesn't have buffer-syntactic-context-depth
824 (let (beginning-of-defun-function end-of-defun-function)
825 (beginning-of-defun)))
826 ;; if defun-prompt-regexp is non-nil, b-o-d won't leave us at the
827 ;; open brace.
828 (and defun-prompt-regexp
829 (looking-at defun-prompt-regexp)
830 (goto-char (match-end 0)))))
833 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
834 ;; V i r t u a l S e m i c o l o n s
836 ;; In most CC Mode languages, statements are terminated explicitly by
837 ;; semicolons or closing braces. In some of the CC modes (currently AWK Mode
838 ;; and certain user-specified #define macros in C, C++, etc. (November 2008)),
839 ;; statements are (or can be) terminated by EOLs. Such a statement is said to
840 ;; be terminated by a "virtual semicolon" (VS). A statement terminated by an
841 ;; actual semicolon or brace is never considered to have a VS.
843 ;; The indentation engine (or whatever) tests for a VS at a specific position
844 ;; by invoking the macro `c-at-vsemi-p', which in its turn calls the mode
845 ;; specific function (if any) which is the value of the language variable
846 ;; `c-at-vsemi-p-fn'. This function should only use "low-level" features of
847 ;; CC Mode, i.e. features which won't trigger infinite recursion. ;-) The
848 ;; actual details of what constitutes a VS in a language are thus encapsulated
849 ;; in code specific to that language (e.g. cc-awk.el). `c-at-vsemi-p' returns
850 ;; non-nil if point (or the optional parameter POS) is at a VS, nil otherwise.
852 ;; The language specific function might well do extensive analysis of the
853 ;; source text, and may use a caching scheme to speed up repeated calls.
855 ;; The "virtual semicolon" lies just after the last non-ws token on the line.
856 ;; Like POINT, it is considered to lie between two characters. For example,
857 ;; at the place shown in the following AWK source line:
859 ;; kbyte = 1024 # 1000 if you're not picky
860 ;; ^
861 ;; |
862 ;; Virtual Semicolon
864 ;; In addition to `c-at-vsemi-p-fn', a mode may need to supply a function for
865 ;; `c-vsemi-status-unknown-p-fn'. The macro `c-vsemi-status-unknown-p' is a
866 ;; rather recondite kludge. It exists because the function
867 ;; `c-beginning-of-statement-1' sometimes tests for VSs as an optimization,
868 ;; but `c-at-vsemi-p' might well need to call `c-beginning-of-statement-1' in
869 ;; its calculations, thus potentially leading to infinite recursion.
871 ;; The macro `c-vsemi-status-unknown-p' resolves this problem; it may return
872 ;; non-nil at any time; returning nil is a guarantee that an immediate
873 ;; invocation of `c-at-vsemi-p' at point will NOT call
874 ;; `c-beginning-of-statement-1'. `c-vsemi-status-unknown-p' may not itself
875 ;; call `c-beginning-of-statement-1'.
877 ;; The macro `c-vsemi-status-unknown-p' will typically check the caching
878 ;; scheme used by the `c-at-vsemi-p-fn', hence the name - the status is
879 ;; "unknown" if there is no cache entry current for the line.
880 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
882 (defmacro c-at-vsemi-p (&optional pos)
883 ;; Is there a virtual semicolon (not a real one or a }) at POS (defaults to
884 ;; point)? Always returns nil for languages which don't have Virtual
885 ;; semicolons.
886 ;; This macro might do hidden buffer changes.
887 `(if c-at-vsemi-p-fn
888 (funcall c-at-vsemi-p-fn ,@(if pos `(,pos)))))
890 (defmacro c-vsemi-status-unknown-p ()
891 ;; Return NIL only if it can be guaranteed that an immediate
892 ;; (c-at-vsemi-p) will NOT call c-beginning-of-statement-1. Otherwise,
893 ;; return non-nil. (See comments above). The function invoked by this
894 ;; macro MUST NOT UNDER ANY CIRCUMSTANCES itself call
895 ;; c-beginning-of-statement-1.
896 ;; Languages which don't have EOL terminated statements always return NIL
897 ;; (they _know_ there's no vsemi ;-).
898 `(if c-vsemi-status-unknown-p-fn (funcall c-vsemi-status-unknown-p-fn)))
901 (defmacro c-benign-error (format &rest args)
902 ;; Formats an error message for the echo area and dings, i.e. like
903 ;; `error' but doesn't abort.
904 `(progn
905 (message ,format ,@args)
906 (ding)))
908 (defmacro c-with-syntax-table (table &rest code)
909 ;; Temporarily switches to the specified syntax table in a failsafe
910 ;; way to execute code.
911 ;; Maintainers' note: If TABLE is `c++-template-syntax-table', DON'T call
912 ;; any forms inside this that call `c-parse-state'. !!!!
913 `(let ((c-with-syntax-table-orig-table (syntax-table)))
914 (unwind-protect
915 (progn
916 (set-syntax-table ,table)
917 ,@code)
918 (set-syntax-table c-with-syntax-table-orig-table))))
919 (put 'c-with-syntax-table 'lisp-indent-function 1)
921 (defmacro c-skip-ws-forward (&optional limit)
922 "Skip over any whitespace following point.
923 This function skips over horizontal and vertical whitespace and line
924 continuations."
925 (if limit
926 `(let ((limit (or ,limit (point-max))))
927 (while (progn
928 ;; skip-syntax-* doesn't count \n as whitespace..
929 (skip-chars-forward " \t\n\r\f\v" limit)
930 (when (and (eq (char-after) ?\\)
931 (< (point) limit))
932 (forward-char)
933 (or (eolp)
934 (progn (backward-char) nil))))))
935 '(while (progn
936 (skip-chars-forward " \t\n\r\f\v")
937 (when (eq (char-after) ?\\)
938 (forward-char)
939 (or (eolp)
940 (progn (backward-char) nil)))))))
942 (defmacro c-skip-ws-backward (&optional limit)
943 "Skip over any whitespace preceding point.
944 This function skips over horizontal and vertical whitespace and line
945 continuations."
946 (if limit
947 `(let ((limit (or ,limit (point-min))))
948 (while (progn
949 ;; skip-syntax-* doesn't count \n as whitespace..
950 (skip-chars-backward " \t\n\r\f\v" limit)
951 (and (eolp)
952 (eq (char-before) ?\\)
953 (> (point) limit)))
954 (backward-char)))
955 '(while (progn
956 (skip-chars-backward " \t\n\r\f\v")
957 (and (eolp)
958 (eq (char-before) ?\\)))
959 (backward-char))))
961 (eval-and-compile
962 (defvar c-langs-are-parametric nil))
964 (defmacro c-major-mode-is (mode)
965 "Return non-nil if the current CC Mode major mode is MODE.
966 MODE is either a mode symbol or a list of mode symbols."
968 (if c-langs-are-parametric
969 ;; Inside a `c-lang-defconst'.
970 `(c-lang-major-mode-is ,mode)
972 (if (eq (car-safe mode) 'quote)
973 (let ((mode (eval mode)))
974 (if (listp mode)
975 `(memq c-buffer-is-cc-mode ',mode)
976 `(eq c-buffer-is-cc-mode ',mode)))
978 `(let ((mode ,mode))
979 (if (listp mode)
980 (memq c-buffer-is-cc-mode mode)
981 (eq c-buffer-is-cc-mode mode))))))
984 ;; Macros/functions to handle so-called "char properties", which are
985 ;; properties set on a single character and that never spread to any
986 ;; other characters.
988 (eval-and-compile
989 ;; Constant used at compile time to decide whether or not to use
990 ;; XEmacs extents. Check all the extent functions we'll use since
991 ;; some packages might add compatibility aliases for some of them in
992 ;; Emacs.
993 (defconst c-use-extents (and (cc-bytecomp-fboundp 'extent-at)
994 (cc-bytecomp-fboundp 'set-extent-property)
995 (cc-bytecomp-fboundp 'set-extent-properties)
996 (cc-bytecomp-fboundp 'make-extent)
997 (cc-bytecomp-fboundp 'extent-property)
998 (cc-bytecomp-fboundp 'delete-extent)
999 (cc-bytecomp-fboundp 'map-extents))))
1001 (defconst c-<-as-paren-syntax '(4 . ?>))
1002 (put 'c-<-as-paren-syntax 'syntax-table c-<-as-paren-syntax)
1004 (defconst c->-as-paren-syntax '(5 . ?<))
1005 (put 'c->-as-paren-syntax 'syntax-table c->-as-paren-syntax)
1007 ;; `c-put-char-property' is complex enough in XEmacs and Emacs < 21 to
1008 ;; make it a function.
1009 (defalias 'c-put-char-property-fun
1010 (cc-eval-when-compile
1011 (cond (c-use-extents
1012 ;; XEmacs.
1013 (byte-compile
1014 (lambda (pos property value)
1015 (let ((ext (extent-at pos nil property)))
1016 (if ext
1017 (set-extent-property ext property value)
1018 (set-extent-properties (make-extent pos (1+ pos))
1019 (cons property
1020 (cons value
1021 '(start-open t
1022 end-open t)))))))))
1024 ((not (cc-bytecomp-boundp 'text-property-default-nonsticky))
1025 ;; In Emacs < 21 we have to mess with the `rear-nonsticky' property.
1026 (byte-compile
1027 (lambda (pos property value)
1028 (put-text-property pos (1+ pos) property value)
1029 (let ((prop (get-text-property pos 'rear-nonsticky)))
1030 (or (memq property prop)
1031 (put-text-property pos (1+ pos)
1032 'rear-nonsticky
1033 (cons property prop)))))))
1034 ;; This won't be used for anything.
1035 (t 'ignore))))
1036 (cc-bytecomp-defun c-put-char-property-fun) ; Make it known below.
1038 (defmacro c-put-char-property (pos property value)
1039 ;; Put the given property with the given value on the character at
1040 ;; POS and make it front and rear nonsticky, or start and end open
1041 ;; in XEmacs vocabulary. If the character already has the given
1042 ;; property then the value is replaced, and the behavior is
1043 ;; undefined if that property has been put by some other function.
1044 ;; PROPERTY is assumed to be constant.
1046 ;; If there's a `text-property-default-nonsticky' variable (Emacs
1047 ;; 21) then it's assumed that the property is present on it.
1049 ;; This macro does a hidden buffer change.
1050 (setq property (eval property))
1051 (if (or c-use-extents
1052 (not (cc-bytecomp-boundp 'text-property-default-nonsticky)))
1053 ;; XEmacs and Emacs < 21.
1054 `(c-put-char-property-fun ,pos ',property ,value)
1055 ;; In Emacs 21 we got the `rear-nonsticky' property covered
1056 ;; by `text-property-default-nonsticky'.
1057 `(let ((-pos- ,pos))
1058 (put-text-property -pos- (1+ -pos-) ',property ,value))))
1060 (defmacro c-get-char-property (pos property)
1061 ;; Get the value of the given property on the character at POS if
1062 ;; it's been put there by `c-put-char-property'. PROPERTY is
1063 ;; assumed to be constant.
1064 (setq property (eval property))
1065 (if c-use-extents
1066 ;; XEmacs.
1067 `(let ((ext (extent-at ,pos nil ',property)))
1068 (if ext (extent-property ext ',property)))
1069 ;; Emacs.
1070 `(get-text-property ,pos ',property)))
1072 ;; `c-clear-char-property' is complex enough in Emacs < 21 to make it
1073 ;; a function, since we have to mess with the `rear-nonsticky' property.
1074 (defalias 'c-clear-char-property-fun
1075 (cc-eval-when-compile
1076 (unless (or c-use-extents
1077 (cc-bytecomp-boundp 'text-property-default-nonsticky))
1078 (byte-compile
1079 (lambda (pos property)
1080 (when (get-text-property pos property)
1081 (remove-text-properties pos (1+ pos) (list property nil))
1082 (put-text-property pos (1+ pos)
1083 'rear-nonsticky
1084 (delq property (get-text-property
1085 pos 'rear-nonsticky)))))))))
1086 (cc-bytecomp-defun c-clear-char-property-fun) ; Make it known below.
1088 (defmacro c-clear-char-property (pos property)
1089 ;; Remove the given property on the character at POS if it's been put
1090 ;; there by `c-put-char-property'. PROPERTY is assumed to be
1091 ;; constant.
1093 ;; This macro does a hidden buffer change.
1094 (setq property (eval property))
1095 (cond (c-use-extents
1096 ;; XEmacs.
1097 `(let ((ext (extent-at ,pos nil ',property)))
1098 (if ext (delete-extent ext))))
1099 ((cc-bytecomp-boundp 'text-property-default-nonsticky)
1100 ;; In Emacs 21 we got the `rear-nonsticky' property covered
1101 ;; by `text-property-default-nonsticky'.
1102 `(let ((pos ,pos))
1103 (remove-text-properties pos (1+ pos)
1104 '(,property nil))))
1106 ;; Emacs < 21.
1107 `(c-clear-char-property-fun ,pos ',property))))
1109 (defmacro c-clear-char-properties (from to property)
1110 ;; Remove all the occurrences of the given property in the given
1111 ;; region that has been put with `c-put-char-property'. PROPERTY is
1112 ;; assumed to be constant.
1114 ;; Note that this function does not clean up the property from the
1115 ;; lists of the `rear-nonsticky' properties in the region, if such
1116 ;; are used. Thus it should not be used for common properties like
1117 ;; `syntax-table'.
1119 ;; This macro does hidden buffer changes.
1120 (setq property (eval property))
1121 (if c-use-extents
1122 ;; XEmacs.
1123 `(map-extents (lambda (ext ignored)
1124 (delete-extent ext))
1125 nil ,from ,to nil nil ',property)
1126 ;; Emacs.
1127 `(remove-text-properties ,from ,to '(,property nil))))
1129 (defmacro c-search-forward-char-property (property value &optional limit)
1130 "Search forward for a text-property PROPERTY having value VALUE.
1131 LIMIT bounds the search. The comparison is done with `equal'.
1133 Leave point just after the character, and set the match data on
1134 this character, and return point. If VALUE isn't found, Return
1135 nil; point is then left undefined."
1136 `(let ((place (point)))
1137 (while
1138 (and
1139 (< place ,(or limit '(point-max)))
1140 (not (equal (c-get-char-property place ,property) ,value)))
1141 (setq place (c-next-single-property-change
1142 place ,property nil ,(or limit '(point-max)))))
1143 (when (< place ,(or limit '(point-max)))
1144 (goto-char place)
1145 (search-forward-regexp ".") ; to set the match-data.
1146 (point))))
1148 (defmacro c-search-backward-char-property (property value &optional limit)
1149 "Search backward for a text-property PROPERTY having value VALUE.
1150 LIMIT bounds the search. The comparison is done with `equal'.
1152 Leave point just before the character, set the match data on this
1153 character, and return point. If VALUE isn't found, Return nil;
1154 point is then left undefined."
1155 `(let ((place (point)))
1156 (while
1157 (and
1158 (> place ,(or limit '(point-min)))
1159 (not (equal (c-get-char-property (1- place) ,property) ,value)))
1160 (setq place (,(if (and c-use-extents
1161 (fboundp 'previous-single-char-property-change))
1162 ;; XEmacs > 2005-01-25.
1163 'previous-single-char-property-change
1164 ;; Emacs and earlier XEmacs.
1165 'previous-single-property-change)
1166 place ,property nil ,(or limit '(point-min)))))
1167 (when (> place ,(or limit '(point-min)))
1168 (goto-char place)
1169 (search-backward-regexp ".") ; to set the match-data.
1170 (point))))
1172 (defun c-clear-char-property-with-value-function (from to property value)
1173 "Remove all text-properties PROPERTY from the region (FROM, TO)
1174 which have the value VALUE, as tested by `equal'. These
1175 properties are assumed to be over individual characters, having
1176 been put there by c-put-char-property. POINT remains unchanged."
1177 (let ((place from) end-place)
1178 (while ; loop round occurrences of (PROPERTY VALUE)
1179 (progn
1180 (while ; loop round changes in PROPERTY till we find VALUE
1181 (and
1182 (< place to)
1183 (not (equal (get-text-property place property) value)))
1184 (setq place (c-next-single-property-change place property nil to)))
1185 (< place to))
1186 (setq end-place (c-next-single-property-change place property nil to))
1187 (remove-text-properties place end-place (cons property nil))
1188 ;; Do we have to do anything with stickiness here?
1189 (setq place end-place))))
1191 (defmacro c-clear-char-property-with-value (from to property value)
1192 "Remove all text-properties PROPERTY from the region [FROM, TO)
1193 which have the value VALUE, as tested by `equal'. These
1194 properties are assumed to be over individual characters, having
1195 been put there by c-put-char-property. POINT remains unchanged."
1196 (if c-use-extents
1197 ;; XEmacs
1198 `(let ((-property- ,property))
1199 (map-extents (lambda (ext val)
1200 (if (equal (extent-property ext -property-) val)
1201 (delete-extent ext)))
1202 nil ,from ,to ,value nil -property-))
1203 ;; GNU Emacs
1204 `(c-clear-char-property-with-value-function ,from ,to ,property ,value)))
1206 ;; Macros to put overlays (Emacs) or extents (XEmacs) on buffer text.
1207 ;; For our purposes, these are characterized by being possible to
1208 ;; remove again without affecting the other text properties in the
1209 ;; buffer that got overridden when they were put.
1211 (defmacro c-put-overlay (from to property value)
1212 ;; Put an overlay/extent covering the given range in the current
1213 ;; buffer. It's currently undefined whether it's front/end sticky
1214 ;; or not. The overlay/extent object is returned.
1215 (if (cc-bytecomp-fboundp 'make-overlay)
1216 ;; Emacs.
1217 `(let ((ol (make-overlay ,from ,to)))
1218 (overlay-put ol ,property ,value)
1220 ;; XEmacs.
1221 `(let ((ext (make-extent ,from ,to)))
1222 (set-extent-property ext ,property ,value)
1223 ext)))
1225 (defmacro c-delete-overlay (overlay)
1226 ;; Deletes an overlay/extent object previously retrieved using
1227 ;; `c-put-overlay'.
1228 (if (cc-bytecomp-fboundp 'make-overlay)
1229 ;; Emacs.
1230 `(delete-overlay ,overlay)
1231 ;; XEmacs.
1232 `(delete-extent ,overlay)))
1235 ;; Make edebug understand the macros.
1236 ;(eval-after-load "edebug" ; 2006-07-09: def-edebug-spec is now in subr.el.
1237 ; '(progn
1238 (def-edebug-spec cc-eval-when-compile (&rest def-form))
1239 (def-edebug-spec c-point t)
1240 (def-edebug-spec c-set-region-active t)
1241 (def-edebug-spec c-safe t)
1242 (def-edebug-spec c-save-buffer-state let*)
1243 (def-edebug-spec c-tentative-buffer-changes t)
1244 (def-edebug-spec c-forward-syntactic-ws t)
1245 (def-edebug-spec c-backward-syntactic-ws t)
1246 (def-edebug-spec c-forward-sexp t)
1247 (def-edebug-spec c-backward-sexp t)
1248 (def-edebug-spec c-up-list-forward t)
1249 (def-edebug-spec c-up-list-backward t)
1250 (def-edebug-spec c-down-list-forward t)
1251 (def-edebug-spec c-down-list-backward t)
1252 (def-edebug-spec c-add-syntax t)
1253 (def-edebug-spec c-add-class-syntax t)
1254 (def-edebug-spec c-benign-error t)
1255 (def-edebug-spec c-with-syntax-table t)
1256 (def-edebug-spec c-skip-ws-forward t)
1257 (def-edebug-spec c-skip-ws-backward t)
1258 (def-edebug-spec c-major-mode-is t)
1259 (def-edebug-spec c-put-char-property t)
1260 (def-edebug-spec c-get-char-property t)
1261 (def-edebug-spec c-clear-char-property t)
1262 (def-edebug-spec c-clear-char-properties t)
1263 (def-edebug-spec c-put-overlay t)
1264 (def-edebug-spec c-delete-overlay t) ;))
1267 ;;; Functions.
1269 ;; Note: All these after the macros, to be on safe side in avoiding
1270 ;; bugs where macros are defined too late. These bugs often only show
1271 ;; when the files are compiled in a certain order within the same
1272 ;; session.
1274 (defsubst c-end-of-defun-1 ()
1275 ;; Replacement for end-of-defun that use c-beginning-of-defun-1.
1276 (let ((start (point)))
1277 ;; Skip forward into the next defun block. Don't bother to avoid
1278 ;; comments, literals etc, since beginning-of-defun doesn't do that
1279 ;; anyway.
1280 (skip-chars-forward "^}")
1281 (c-beginning-of-defun-1)
1282 (if (eq (char-after) ?{)
1283 (c-forward-sexp))
1284 (if (< (point) start)
1285 (goto-char (point-max)))))
1287 (defmacro c-mark-<-as-paren (pos)
1288 ;; Mark the "<" character at POS as a template opener using the
1289 ;; `syntax-table' property either directly (XEmacs) or via a `category'
1290 ;; property (GNU Emacs).
1292 ;; This function does a hidden buffer change. Note that we use
1293 ;; indirection through the `category' text property. This allows us to
1294 ;; toggle the property in all template brackets simultaneously and
1295 ;; cheaply. We use this, for instance, in `c-parse-state'.
1296 (if c-use-category
1297 `(c-put-char-property ,pos 'category 'c-<-as-paren-syntax)
1298 `(c-put-char-property ,pos 'syntax-table c-<-as-paren-syntax)))
1301 (defmacro c-mark->-as-paren (pos)
1302 ;; Mark the ">" character at POS as an sexp list closer using the
1303 ;; `syntax-table' property either directly (XEmacs) or via a `category'
1304 ;; property (GNU Emacs).
1306 ;; This function does a hidden buffer change. Note that we use
1307 ;; indirection through the `category' text property. This allows us to
1308 ;; toggle the property in all template brackets simultaneously and
1309 ;; cheaply. We use this, for instance, in `c-parse-state'.
1310 (if c-use-category
1311 `(c-put-char-property ,pos 'category 'c->-as-paren-syntax)
1312 `(c-put-char-property ,pos 'syntax-table c->-as-paren-syntax)))
1314 (defmacro c-unmark-<->-as-paren (pos)
1315 ;; Unmark the "<" or "<" character at POS as an sexp list opener using the
1316 ;; `syntax-table' property either directly or indirectly through a
1317 ;; `category' text property.
1319 ;; This function does a hidden buffer change. Note that we try to use
1320 ;; indirection through the `category' text property. This allows us to
1321 ;; toggle the property in all template brackets simultaneously and
1322 ;; cheaply. We use this, for instance, in `c-parse-state'.
1323 `(c-clear-char-property ,pos ,(if c-use-category ''category ''syntax-table)))
1325 (defsubst c-suppress-<->-as-parens ()
1326 ;; Suppress the syntactic effect of all marked < and > as parens. Note
1327 ;; that this effect is NOT buffer local. You should probably not use
1328 ;; this directly, but only through the macro
1329 ;; `c-with-<->-as-parens-suppressed'
1330 (put 'c-<-as-paren-syntax 'syntax-table nil)
1331 (put 'c->-as-paren-syntax 'syntax-table nil))
1333 (defsubst c-restore-<->-as-parens ()
1334 ;; Restore the syntactic effect of all marked <s and >s as parens. This
1335 ;; has no effect on unmarked <s and >s
1336 (put 'c-<-as-paren-syntax 'syntax-table c-<-as-paren-syntax)
1337 (put 'c->-as-paren-syntax 'syntax-table c->-as-paren-syntax))
1339 (defmacro c-with-<->-as-parens-suppressed (&rest forms)
1340 ;; Like progn, except that the paren property is suppressed on all
1341 ;; template brackets whilst they are running. This macro does a hidden
1342 ;; buffer change.
1343 `(unwind-protect
1344 (progn
1345 (c-suppress-<->-as-parens)
1346 ,@forms)
1347 (c-restore-<->-as-parens)))
1349 ;;;;;;;;;;;;;;;
1351 (defconst c-cpp-delimiter '(14)) ; generic comment syntax
1352 ;; This is the value of the `category' text property placed on every #
1353 ;; which introduces a CPP construct and every EOL (or EOB, or character
1354 ;; preceding //, etc.) which terminates it. We can instantly "comment
1355 ;; out" all CPP constructs by giving `c-cpp-delimiter' a syntax-table
1356 ;; property '(14) (generic comment delimiter).
1357 (defmacro c-set-cpp-delimiters (beg end)
1358 ;; This macro does a hidden buffer change.
1359 `(progn
1360 (c-put-char-property ,beg 'category 'c-cpp-delimiter)
1361 (if (< ,end (point-max))
1362 (c-put-char-property ,end 'category 'c-cpp-delimiter))))
1363 (defmacro c-clear-cpp-delimiters (beg end)
1364 ;; This macro does a hidden buffer change.
1365 `(progn
1366 (c-clear-char-property ,beg 'category)
1367 (if (< ,end (point-max))
1368 (c-clear-char-property ,end 'category))))
1370 (defsubst c-comment-out-cpps ()
1371 ;; Render all preprocessor constructs syntactically commented out.
1372 (put 'c-cpp-delimiter 'syntax-table c-cpp-delimiter))
1373 (defsubst c-uncomment-out-cpps ()
1374 ;; Restore the syntactic visibility of preprocessor constructs.
1375 (put 'c-cpp-delimiter 'syntax-table nil))
1377 (defmacro c-with-cpps-commented-out (&rest forms)
1378 ;; Execute FORMS... whilst the syntactic effect of all characters in
1379 ;; all CPP regions is suppressed. In particular, this is to suppress
1380 ;; the syntactic significance of parens/braces/brackets to functions
1381 ;; such as `scan-lists' and `parse-partial-sexp'.
1382 `(unwind-protect
1383 (c-save-buffer-state ()
1384 (c-comment-out-cpps)
1385 ,@forms)
1386 (c-save-buffer-state ()
1387 (c-uncomment-out-cpps))))
1389 (defmacro c-with-all-but-one-cpps-commented-out (beg end &rest forms)
1390 ;; Execute FORMS... whilst the syntactic effect of all characters in
1391 ;; every CPP region APART FROM THE ONE BETWEEN BEG and END is
1392 ;; suppressed.
1393 `(unwind-protect
1394 (c-save-buffer-state ()
1395 (save-restriction
1396 (widen)
1397 (c-clear-cpp-delimiters ,beg ,end))
1398 ,`(c-with-cpps-commented-out ,@forms))
1399 (c-save-buffer-state ()
1400 (save-restriction
1401 (widen)
1402 (c-set-cpp-delimiters ,beg ,end)))))
1404 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1405 ;; The following macros are to be used only in `c-parse-state' and its
1406 ;; subroutines. Their main purpose is to simplify the handling of C++/Java
1407 ;; template delimiters and CPP macros. In GNU Emacs, this is done slickly by
1408 ;; the judicious use of 'category properties. These don't exist in XEmacs.
1410 ;; Note: in the following macros, there is no special handling for parentheses
1411 ;; inside CPP constructs. That is because CPPs are always syntactically
1412 ;; balanced, thanks to `c-neutralize-CPP-line' in cc-mode.el.
1413 (defmacro c-sc-scan-lists-no-category+1+1 (from)
1414 ;; Do a (scan-lists FROM 1 1). Any finishing position which either (i) is
1415 ;; determined by and angle bracket; or (ii) is inside a macro whose start
1416 ;; isn't POINT-MACRO-START doesn't count as a finishing position.
1417 `(let ((here (point))
1418 (pos (scan-lists ,from 1 1)))
1419 (while (eq (char-before pos) ?>)
1420 (setq pos (scan-lists pos 1 1)))
1421 pos))
1423 (defmacro c-sc-scan-lists-no-category+1-1 (from)
1424 ;; Do a (scan-lists FROM 1 -1). Any finishing position which either (i) is
1425 ;; determined by an angle bracket; or (ii) is inside a macro whose start
1426 ;; isn't POINT-MACRO-START doesn't count as a finishing position.
1427 `(let ((here (point))
1428 (pos (scan-lists ,from 1 -1)))
1429 (while (eq (char-before pos) ?<)
1430 (setq pos (scan-lists pos 1 1))
1431 (setq pos (scan-lists pos 1 -1)))
1432 pos))
1434 (defmacro c-sc-scan-lists-no-category-1+1 (from)
1435 ;; Do a (scan-lists FROM -1 1). Any finishing position which either (i) is
1436 ;; determined by and angle bracket; or (ii) is inside a macro whose start
1437 ;; isn't POINT-MACRO-START doesn't count as a finishing position.
1438 `(let ((here (point))
1439 (pos (scan-lists ,from -1 1)))
1440 (while (eq (char-after pos) ?<)
1441 (setq pos (scan-lists pos -1 1)))
1442 pos))
1444 (defmacro c-sc-scan-lists-no-category-1-1 (from)
1445 ;; Do a (scan-lists FROM -1 -1). Any finishing position which either (i) is
1446 ;; determined by and angle bracket; or (ii) is inside a macro whose start
1447 ;; isn't POINT-MACRO-START doesn't count as a finishing position.
1448 `(let ((here (point))
1449 (pos (scan-lists ,from -1 -1)))
1450 (while (eq (char-after pos) ?>)
1451 (setq pos (scan-lists pos -1 1))
1452 (setq pos (scan-lists pos -1 -1)))
1453 pos))
1455 (defmacro c-sc-scan-lists (from count depth)
1456 (if c-use-category
1457 `(scan-lists ,from ,count ,depth)
1458 (cond
1459 ((and (eq count 1) (eq depth 1))
1460 `(c-sc-scan-lists-no-category+1+1 ,from))
1461 ((and (eq count 1) (eq depth -1))
1462 `(c-sc-scan-lists-no-category+1-1 ,from))
1463 ((and (eq count -1) (eq depth 1))
1464 `(c-sc-scan-lists-no-category-1+1 ,from))
1465 ((and (eq count -1) (eq depth -1))
1466 `(c-sc-scan-lists-no-category-1-1 ,from))
1467 (t (error "Invalid parameter(s) to c-sc-scan-lists")))))
1470 (defun c-sc-parse-partial-sexp-no-category (from to targetdepth stopbefore
1471 oldstate)
1472 ;; Do a parse-partial-sexp using the supplied arguments, disregarding
1473 ;; template/generic delimiters < > and disregarding macros other than the
1474 ;; one at POINT-MACRO-START.
1476 ;; NOTE that STOPBEFORE must be nil. TARGETDEPTH should be one less than
1477 ;; the depth in OLDSTATE. This function is thus a SPECIAL PURPOSE variation
1478 ;; on parse-partial-sexp, designed for calling from
1479 ;; `c-remove-stale-state-cache'.
1481 ;; Any finishing position which is determined by an angle bracket delimiter
1482 ;; doesn't count as a finishing position.
1484 ;; Note there is no special handling of CPP constructs here, since these are
1485 ;; always syntactically balanced (thanks to `c-neutralize-CPP-line').
1486 (let ((state
1487 (parse-partial-sexp from to targetdepth stopbefore oldstate)))
1488 (while
1489 (and (< (point) to)
1490 ;; We must have hit targetdepth.
1491 (or (eq (char-before) ?<)
1492 (eq (char-before) ?>)))
1493 (setcar state
1494 (if (memq (char-before) '(?> ?\) ?\} ?\]))
1495 (1+ (car state))
1496 (1- (car state))))
1497 (setq state
1498 (parse-partial-sexp (point) to targetdepth stopbefore oldstate)))
1499 state))
1501 (defmacro c-sc-parse-partial-sexp (from to &optional targetdepth stopbefore
1502 oldstate)
1503 (if c-use-category
1504 `(parse-partial-sexp ,from ,to ,targetdepth ,stopbefore ,oldstate)
1505 `(c-sc-parse-partial-sexp-no-category ,from ,to ,targetdepth ,stopbefore
1506 ,oldstate)))
1509 (defvar c-emacs-features)
1511 (defmacro c-looking-at-non-alphnumspace ()
1512 "Are we looking at a character which isn't alphanumeric or space?"
1513 (if (memq 'gen-comment-delim c-emacs-features)
1514 `(looking-at
1515 "\\([;#]\\|\\'\\|\\s(\\|\\s)\\|\\s\"\\|\\s\\\\|\\s$\\|\\s<\\|\\s>\\|\\s!\\)")
1516 `(or (looking-at
1517 "\\([;#]\\|\\'\\|\\s(\\|\\s)\\|\\s\"\\|\\s\\\\|\\s$\\|\\s<\\|\\s>\\)"
1518 (let ((prop (c-get-char-property (point) 'syntax-table)))
1519 (eq prop '(14))))))) ; '(14) is generic comment delimiter.
1522 (defsubst c-intersect-lists (list alist)
1523 ;; return the element of ALIST that matches the first element found
1524 ;; in LIST. Uses assq.
1525 (let (match)
1526 (while (and list
1527 (not (setq match (assq (car list) alist))))
1528 (setq list (cdr list)))
1529 match))
1531 (defsubst c-lookup-lists (list alist1 alist2)
1532 ;; first, find the first entry from LIST that is present in ALIST1,
1533 ;; then find the entry in ALIST2 for that entry.
1534 (assq (car (c-intersect-lists list alist1)) alist2))
1536 (defsubst c-langelem-sym (langelem)
1537 "Return the syntactic symbol in LANGELEM.
1539 LANGELEM is either a cons cell on the \"old\" form given as the first
1540 argument to lineup functions or a syntactic element on the \"new\"
1541 form as used in `c-syntactic-element'."
1542 (car langelem))
1544 (defsubst c-langelem-pos (langelem)
1545 "Return the anchor position in LANGELEM, or nil if there is none.
1547 LANGELEM is either a cons cell on the \"old\" form given as the first
1548 argument to lineup functions or a syntactic element on the \"new\"
1549 form as used in `c-syntactic-element'."
1550 (if (consp (cdr langelem))
1551 (car-safe (cdr langelem))
1552 (cdr langelem)))
1554 (defun c-langelem-col (langelem &optional preserve-point)
1555 "Return the column of the anchor position in LANGELEM.
1556 Also move the point to that position unless PRESERVE-POINT is non-nil.
1558 LANGELEM is either a cons cell on the \"old\" form given as the first
1559 argument to lineup functions or a syntactic element on the \"new\"
1560 form as used in `c-syntactic-element'."
1561 (let ((pos (c-langelem-pos langelem))
1562 (here (point)))
1563 (if pos
1564 (progn
1565 (goto-char pos)
1566 (prog1 (current-column)
1567 (if preserve-point
1568 (goto-char here))))
1569 0)))
1571 (defsubst c-langelem-2nd-pos (langelem)
1572 "Return the secondary position in LANGELEM, or nil if there is none.
1574 LANGELEM is typically a syntactic element on the \"new\" form as used
1575 in `c-syntactic-element'. It may also be a cons cell as passed in the
1576 first argument to lineup functions, but then the returned value always
1577 will be nil."
1578 (car-safe (cdr-safe (cdr-safe langelem))))
1580 (defsubst c-keep-region-active ()
1581 ;; Do whatever is necessary to keep the region active in XEmacs.
1582 ;; This is not needed for Emacs.
1583 (and (boundp 'zmacs-region-stays)
1584 (setq zmacs-region-stays t)))
1586 (put 'c-mode 'c-mode-prefix "c-")
1587 (put 'c++-mode 'c-mode-prefix "c++-")
1588 (put 'objc-mode 'c-mode-prefix "objc-")
1589 (put 'java-mode 'c-mode-prefix "java-")
1590 (put 'idl-mode 'c-mode-prefix "idl-")
1591 (put 'pike-mode 'c-mode-prefix "pike-")
1592 (put 'awk-mode 'c-mode-prefix "awk-")
1594 (defsubst c-mode-symbol (suffix)
1595 "Prefix the current mode prefix (e.g. \"c-\") to SUFFIX and return
1596 the corresponding symbol."
1597 (or c-buffer-is-cc-mode
1598 (error "Not inside a CC Mode based mode"))
1599 (let ((mode-prefix (get c-buffer-is-cc-mode 'c-mode-prefix)))
1600 (or mode-prefix
1601 (error "%S has no mode prefix known to `c-mode-symbol'"
1602 c-buffer-is-cc-mode))
1603 (intern (concat mode-prefix suffix))))
1605 (defsubst c-mode-var (suffix)
1606 "Prefix the current mode prefix (e.g. \"c-\") to SUFFIX and return
1607 the value of the variable with that name."
1608 (symbol-value (c-mode-symbol suffix)))
1610 (defsubst c-got-face-at (pos faces)
1611 "Return non-nil if position POS in the current buffer has any of the
1612 faces in the list FACES."
1613 (let ((pos-faces (get-text-property pos 'face)))
1614 (if (consp pos-faces)
1615 (progn
1616 (while (and pos-faces
1617 (not (memq (car pos-faces) faces)))
1618 (setq pos-faces (cdr pos-faces)))
1619 pos-faces)
1620 (memq pos-faces faces))))
1622 (defsubst c-face-name-p (facename)
1623 ;; Return t if FACENAME is the name of a face. This method is
1624 ;; necessary since facep in XEmacs only returns t for the actual
1625 ;; face objects (while it's only their names that are used just
1626 ;; about anywhere else) without providing a predicate that tests
1627 ;; face names.
1628 (memq facename (face-list)))
1630 (defun c-concat-separated (list separator)
1631 "Like `concat' on LIST, but separate each element with SEPARATOR.
1632 Notably, null elements in LIST are ignored."
1633 (mapconcat 'identity (delete nil (append list nil)) separator))
1635 (defun c-make-keywords-re (adorn list &optional mode)
1636 "Make a regexp that matches all the strings the list.
1637 Duplicates and nil elements in the list are removed. The
1638 resulting regexp may contain zero or more submatch expressions.
1640 If ADORN is t there will be at least one submatch and the first
1641 surrounds the matched alternative, and the regexp will also not match
1642 a prefix of any identifier. Adorned regexps cannot be appended. The
1643 language variable `c-nonsymbol-key' is used to make the adornment.
1645 A value `appendable' for ADORN is like above, but all alternatives in
1646 the list that end with a word constituent char will have \\> appended
1647 instead, so that the regexp remains appendable. Note that this
1648 variant doesn't always guarantee that an identifier prefix isn't
1649 matched since the symbol constituent `_' is normally considered a
1650 nonword token by \\>.
1652 The optional MODE specifies the language to get `c-nonsymbol-key' from
1653 when it's needed. The default is the current language taken from
1654 `c-buffer-is-cc-mode'."
1656 (setq list (delete nil (delete-dups list)))
1657 (if list
1658 (let (re)
1660 (if (eq adorn 'appendable)
1661 ;; This is kludgy but it works: Search for a string that
1662 ;; doesn't occur in any word in LIST. Append it to all
1663 ;; the alternatives where we want to add \>. Run through
1664 ;; `regexp-opt' and then replace it with \>.
1665 (let ((unique "") pos)
1666 (while (let (found)
1667 (setq unique (concat unique "@")
1668 pos list)
1669 (while (and pos
1670 (if (string-match unique (car pos))
1671 (progn (setq found t)
1672 nil)
1674 (setq pos (cdr pos)))
1675 found))
1676 (setq pos list)
1677 (while pos
1678 (if (string-match "\\w\\'" (car pos))
1679 (setcar pos (concat (car pos) unique)))
1680 (setq pos (cdr pos)))
1681 (setq re (regexp-opt list))
1682 (setq pos 0)
1683 (while (string-match unique re pos)
1684 (setq pos (+ (match-beginning 0) 2)
1685 re (replace-match "\\>" t t re))))
1687 (setq re (regexp-opt list)))
1689 ;; Emacs 20 and XEmacs (all versions so far) has a buggy
1690 ;; regexp-opt that doesn't always cope with strings containing
1691 ;; newlines. This kludge doesn't handle shy parens correctly
1692 ;; so we can't advice regexp-opt directly with it.
1693 (let (fail-list)
1694 (while list
1695 (and (string-match "\n" (car list)) ; To speed it up a little.
1696 (not (string-match (concat "\\`\\(" re "\\)\\'")
1697 (car list)))
1698 (setq fail-list (cons (car list) fail-list)))
1699 (setq list (cdr list)))
1700 (when fail-list
1701 (setq re (concat re
1702 "\\|"
1703 (mapconcat
1704 (if (eq adorn 'appendable)
1705 (lambda (str)
1706 (if (string-match "\\w\\'" str)
1707 (concat (regexp-quote str)
1708 "\\>")
1709 (regexp-quote str)))
1710 'regexp-quote)
1711 (sort fail-list
1712 (lambda (a b)
1713 (> (length a) (length b))))
1714 "\\|")))))
1716 ;; Add our own grouping parenthesis around re instead of
1717 ;; passing adorn to `regexp-opt', since in XEmacs it makes the
1718 ;; top level grouping "shy".
1719 (cond ((eq adorn 'appendable)
1720 (concat "\\(" re "\\)"))
1721 (adorn
1722 (concat "\\(" re "\\)"
1723 "\\("
1724 (c-get-lang-constant 'c-nonsymbol-key nil mode)
1725 "\\|$\\)"))
1727 re)))
1729 ;; Produce a regexp that matches nothing.
1730 (if adorn
1731 "\\(\\<\\>\\)"
1732 "\\<\\>")))
1734 (put 'c-make-keywords-re 'lisp-indent-function 1)
1736 (defun c-make-bare-char-alt (chars &optional inverted)
1737 "Make a character alternative string from the list of characters CHARS.
1738 The returned string is of the type that can be used with
1739 `skip-chars-forward' and `skip-chars-backward'. If INVERTED is
1740 non-nil, a caret is prepended to invert the set."
1741 ;; This function ought to be in the elisp core somewhere.
1742 (let ((str (if inverted "^" "")) char char2)
1743 (setq chars (sort (append chars nil) `<))
1744 (while chars
1745 (setq char (pop chars))
1746 (if (memq char '(?\\ ?^ ?-))
1747 ;; Quoting necessary (this method only works in the skip
1748 ;; functions).
1749 (setq str (format "%s\\%c" str char))
1750 (setq str (format "%s%c" str char)))
1751 ;; Check for range.
1752 (setq char2 char)
1753 (while (and chars (>= (1+ char2) (car chars)))
1754 (setq char2 (pop chars)))
1755 (unless (= char char2)
1756 (if (< (1+ char) char2)
1757 (setq str (format "%s-%c" str char2))
1758 (push char2 chars))))
1759 str))
1761 ;; Leftovers from (X)Emacs 19 compatibility.
1762 (defalias 'c-regexp-opt 'regexp-opt)
1763 (defalias 'c-regexp-opt-depth 'regexp-opt-depth)
1766 ;; Figure out what features this Emacs has
1768 (cc-bytecomp-defvar open-paren-in-column-0-is-defun-start)
1770 (defvar lookup-syntax-properties) ;XEmacs.
1772 (defconst c-emacs-features
1773 (let (list)
1775 (if (boundp 'infodock-version)
1776 ;; I've no idea what this actually is, but it's legacy. /mast
1777 (setq list (cons 'infodock list)))
1779 ;; XEmacs uses 8-bit modify-syntax-entry flags.
1780 ;; Emacs uses a 1-bit flag. We will have to set up our
1781 ;; syntax tables differently to handle this.
1782 (let ((table (copy-syntax-table))
1783 entry)
1784 (modify-syntax-entry ?a ". 12345678" table)
1785 (cond
1786 ;; Emacs
1787 ((arrayp table)
1788 (setq entry (aref table ?a))
1789 ;; In Emacs, table entries are cons cells
1790 (if (consp entry) (setq entry (car entry))))
1791 ;; XEmacs
1792 ((fboundp 'get-char-table)
1793 (setq entry (get-char-table ?a table)))
1794 ;; incompatible
1795 (t (error "CC Mode is incompatible with this version of Emacs")))
1796 (setq list (cons (if (= (logand (lsh entry -16) 255) 255)
1797 '8-bit
1798 '1-bit)
1799 list)))
1801 ;; Check whether beginning/end-of-defun call
1802 ;; beginning/end-of-defun-function nicely, passing through the
1803 ;; argument and respecting the return code.
1804 (let* (mark-ring
1805 (bod-param 'foo) (eod-param 'foo)
1806 (beginning-of-defun-function
1807 (lambda (&optional arg)
1808 (or (eq bod-param 'foo) (setq bod-param 'bar))
1809 (and (eq bod-param 'foo)
1810 (setq bod-param arg)
1811 (eq arg 3))))
1812 (end-of-defun-function
1813 (lambda (&optional arg)
1814 (and (eq eod-param 'foo)
1815 (setq eod-param arg)
1816 (eq arg 3)))))
1817 (if (save-excursion (and (beginning-of-defun 3) (eq bod-param 3)
1818 (not (beginning-of-defun))
1819 (end-of-defun 3) (eq eod-param 3)
1820 (not (end-of-defun))))
1821 (setq list (cons 'argumentative-bod-function list))))
1823 ;; Record whether the `category' text property works.
1824 (if c-use-category (setq list (cons 'category-properties list)))
1826 (let ((buf (generate-new-buffer " test"))
1827 parse-sexp-lookup-properties
1828 parse-sexp-ignore-comments
1829 lookup-syntax-properties) ; XEmacs
1830 (with-current-buffer buf
1831 (set-syntax-table (make-syntax-table))
1833 ;; For some reason we have to set some of these after the
1834 ;; buffer has been made current. (Specifically,
1835 ;; `parse-sexp-ignore-comments' in Emacs 21.)
1836 (setq parse-sexp-lookup-properties t
1837 parse-sexp-ignore-comments t
1838 lookup-syntax-properties t)
1840 ;; Find out if the `syntax-table' text property works.
1841 (modify-syntax-entry ?< ".")
1842 (modify-syntax-entry ?> ".")
1843 (insert "<()>")
1844 (c-mark-<-as-paren (point-min))
1845 (c-mark->-as-paren (+ 3 (point-min)))
1846 (goto-char (point-min))
1847 (c-forward-sexp)
1848 (if (= (point) (+ 4 (point-min)))
1849 (setq list (cons 'syntax-properties list))
1850 (error (concat
1851 "CC Mode is incompatible with this version of Emacs - "
1852 "support for the `syntax-table' text property "
1853 "is required.")))
1855 ;; Find out if "\\s!" (generic comment delimiters) work.
1856 (c-safe
1857 (modify-syntax-entry ?x "!")
1858 (if (string-match "\\s!" "x")
1859 (setq list (cons 'gen-comment-delim list))))
1861 ;; Find out if "\\s|" (generic string delimiters) work.
1862 (c-safe
1863 (modify-syntax-entry ?x "|")
1864 (if (string-match "\\s|" "x")
1865 (setq list (cons 'gen-string-delim list))))
1867 ;; See if POSIX char classes work.
1868 (when (and (string-match "[[:alpha:]]" "a")
1869 ;; All versions of Emacs 21 so far haven't fixed
1870 ;; char classes in `skip-chars-forward' and
1871 ;; `skip-chars-backward'.
1872 (progn
1873 (delete-region (point-min) (point-max))
1874 (insert "foo123")
1875 (skip-chars-backward "[:alnum:]")
1876 (bobp))
1877 (= (skip-chars-forward "[:alpha:]") 3))
1878 (setq list (cons 'posix-char-classes list)))
1880 ;; See if `open-paren-in-column-0-is-defun-start' exists and
1881 ;; isn't buggy (Emacs >= 21.4).
1882 (when (boundp 'open-paren-in-column-0-is-defun-start)
1883 (let ((open-paren-in-column-0-is-defun-start nil)
1884 (parse-sexp-ignore-comments t))
1885 (delete-region (point-min) (point-max))
1886 (set-syntax-table (make-syntax-table))
1887 (modify-syntax-entry ?\' "\"")
1888 (cond
1889 ;; XEmacs. Afaik this is currently an Emacs-only
1890 ;; feature, but it's good to be prepared.
1891 ((memq '8-bit list)
1892 (modify-syntax-entry ?/ ". 1456")
1893 (modify-syntax-entry ?* ". 23"))
1894 ;; Emacs
1895 ((memq '1-bit list)
1896 (modify-syntax-entry ?/ ". 124b")
1897 (modify-syntax-entry ?* ". 23")))
1898 (modify-syntax-entry ?\n "> b")
1899 (insert "/* '\n () */")
1900 (backward-sexp)
1901 (if (bobp)
1902 (setq list (cons 'col-0-paren list)))))
1904 (set-buffer-modified-p nil))
1905 (kill-buffer buf))
1907 ;; See if `parse-partial-sexp' returns the eighth element.
1908 (if (c-safe (>= (length (save-excursion
1909 (parse-partial-sexp (point) (point))))
1910 10))
1911 (setq list (cons 'pps-extended-state list))
1912 (error (concat
1913 "CC Mode is incompatible with this version of Emacs - "
1914 "`parse-partial-sexp' has to return at least 10 elements.")))
1916 ;;(message "c-emacs-features: %S" list)
1917 list)
1918 "A list of certain features in the (X)Emacs you are using.
1919 There are many flavors of Emacs out there, each with different
1920 features supporting those needed by CC Mode. The following values
1921 might be present:
1923 `8-bit' 8 bit syntax entry flags (XEmacs style).
1924 `1-bit' 1 bit syntax entry flags (Emacs style).
1925 `argumentative-bod-function' beginning-of-defun and end-of-defun pass
1926 ARG through to beginning/end-of-defun-function.
1927 `syntax-properties' It works to override the syntax for specific characters
1928 in the buffer with the `syntax-table' property. It's
1929 always set - CC Mode no longer works in emacsen without
1930 this feature.
1931 `category-properties' Syntax routines can add a level of indirection to text
1932 properties using the `category' property.
1933 `gen-comment-delim' Generic comment delimiters work
1934 (i.e. the syntax class `!').
1935 `gen-string-delim' Generic string delimiters work
1936 (i.e. the syntax class `|').
1937 `pps-extended-state' `parse-partial-sexp' returns a list with at least 10
1938 elements, i.e. it contains the position of the start of
1939 the last comment or string. It's always set - CC Mode
1940 no longer works in emacsen without this feature.
1941 `posix-char-classes' The regexp engine understands POSIX character classes.
1942 `col-0-paren' It's possible to turn off the ad-hoc rule that a paren
1943 in column zero is the start of a defun.
1944 `infodock' This is Infodock (based on XEmacs).
1946 `8-bit' and `1-bit' are mutually exclusive.")
1949 ;;; Some helper constants.
1951 ;; If the regexp engine supports POSIX char classes then we can use
1952 ;; them to handle extended charsets correctly.
1953 (if (memq 'posix-char-classes c-emacs-features)
1954 (progn
1955 (defconst c-alpha "[:alpha:]")
1956 (defconst c-alnum "[:alnum:]")
1957 (defconst c-digit "[:digit:]")
1958 (defconst c-upper "[:upper:]")
1959 (defconst c-lower "[:lower:]"))
1960 (defconst c-alpha "a-zA-Z")
1961 (defconst c-alnum "a-zA-Z0-9")
1962 (defconst c-digit "0-9")
1963 (defconst c-upper "A-Z")
1964 (defconst c-lower "a-z"))
1967 ;;; System for handling language dependent constants.
1969 ;; This is used to set various language dependent data in a flexible
1970 ;; way: Language constants can be built from the values of other
1971 ;; language constants, also those for other languages. They can also
1972 ;; process the values of other language constants uniformly across all
1973 ;; the languages. E.g. one language constant can list all the type
1974 ;; keywords in each language, and another can build a regexp for each
1975 ;; language from those lists without code duplication.
1977 ;; Language constants are defined with `c-lang-defconst', and their
1978 ;; value forms (referred to as source definitions) are evaluated only
1979 ;; on demand when requested for a particular language with
1980 ;; `c-lang-const'. It's therefore possible to refer to the values of
1981 ;; constants defined later in the file, or in another file, just as
1982 ;; long as all the relevant `c-lang-defconst' have been loaded when
1983 ;; `c-lang-const' is actually evaluated from somewhere else.
1985 ;; `c-lang-const' forms are also evaluated at compile time and
1986 ;; replaced with the values they produce. Thus there's no overhead
1987 ;; for this system when compiled code is used - only the values
1988 ;; actually used in the code are present, and the file(s) containing
1989 ;; the `c-lang-defconst' forms don't need to be loaded at all then.
1990 ;; There are however safeguards to make sure that they can be loaded
1991 ;; to get the source definitions for the values if there's a mismatch
1992 ;; in compiled versions, or if `c-lang-const' is used uncompiled.
1994 ;; Note that the source definitions in a `c-lang-defconst' form are
1995 ;; compiled into the .elc file where it stands; there's no need to
1996 ;; load the source file to get it.
1998 ;; See cc-langs.el for more details about how this system is deployed
1999 ;; in CC Mode, and how the associated language variable system
2000 ;; (`c-lang-defvar') works. That file also contains a lot of
2001 ;; examples.
2003 (defun c-add-language (mode base-mode)
2004 "Declare a new language in the language dependent variable system.
2005 This is intended to be used by modes that inherit CC Mode to add new
2006 languages. It should be used at the top level before any calls to
2007 `c-lang-defconst'. MODE is the mode name symbol for the new language,
2008 and BASE-MODE is the mode name symbol for the language in CC Mode that
2009 is to be the template for the new mode.
2011 The exact effect of BASE-MODE is to make all language constants that
2012 haven't got a setting in the new language fall back to their values in
2013 BASE-MODE. It does not have any effect outside the language constant
2014 system."
2015 (unless (string-match "\\`\\(.*-\\)mode\\'" (symbol-name mode))
2016 (error "The mode name symbol `%s' must end with \"-mode\"" mode))
2017 (put mode 'c-mode-prefix (match-string 1 (symbol-name mode)))
2018 (unless (get base-mode 'c-mode-prefix)
2019 (error "Unknown base mode `%s'" base-mode))
2020 (put mode 'c-fallback-mode base-mode))
2022 (defvar c-lang-constants (make-vector 151 0))
2023 ;; Obarray used as a cache to keep track of the language constants.
2024 ;; The constants stored are those defined by `c-lang-defconst' and the values
2025 ;; computed by `c-lang-const'. It's mostly used at compile time but it's not
2026 ;; stored in compiled files.
2028 ;; The obarray contains all the language constants as symbols. The
2029 ;; value cells hold the evaluated values as alists where each car is
2030 ;; the mode name symbol and the corresponding cdr is the evaluated
2031 ;; value in that mode. The property lists hold the source definitions
2032 ;; and other miscellaneous data. The obarray might also contain
2033 ;; various other symbols, but those don't have any variable bindings.
2035 (defvar c-lang-const-expansion nil)
2037 ;; Ugly hack to pull in the definition of `cc-bytecomp-compiling-or-loading'
2038 ;; from cc-bytecomp to make it available at loadtime. This is the same
2039 ;; mechanism used in cc-mode.el for `c-populate-syntax-table'.
2040 (defalias 'cc-bytecomp-compiling-or-loading
2041 (cc-eval-when-compile
2042 (let ((f (symbol-function 'cc-bytecomp-compiling-or-loading)))
2043 (if (byte-code-function-p f) f (byte-compile f)))))
2045 (defsubst c-get-current-file ()
2046 ;; Return the base name of the current file.
2047 (let* ((c-or-l (cc-bytecomp-compiling-or-loading))
2048 (file
2049 (cond
2050 ((eq c-or-l 'loading) load-file-name)
2051 ((eq c-or-l 'compiling) byte-compile-dest-file)
2052 ((null c-or-l) (buffer-file-name)))))
2053 (and file
2054 (file-name-sans-extension
2055 (file-name-nondirectory file)))))
2057 (defmacro c-lang-defconst-eval-immediately (form)
2058 "Can be used inside a VAL in `c-lang-defconst' to evaluate FORM
2059 immediately, i.e. at the same time as the `c-lang-defconst' form
2060 itself is evaluated."
2061 ;; Evaluate at macro expansion time, i.e. in the
2062 ;; `c--macroexpand-all' inside `c-lang-defconst'.
2063 (eval form))
2065 (defmacro c-lang-defconst (name &rest args)
2066 "Set the language specific values of the language constant NAME.
2067 The second argument can optionally be a docstring. The rest of the
2068 arguments are one or more repetitions of LANG VAL where LANG specifies
2069 the language(s) that VAL applies to. LANG is the name of the
2070 language, i.e. the mode name without the \"-mode\" suffix, or a list
2071 of such language names, or t for all languages. VAL is a form to
2072 evaluate to get the value.
2074 If LANG isn't t or one of the core languages in CC Mode, it must
2075 have been declared with `c-add-language'.
2077 Neither NAME, LANG nor VAL are evaluated directly - they should not be
2078 quoted. `c-lang-defconst-eval-immediately' can however be used inside
2079 VAL to evaluate parts of it directly.
2081 When VAL is evaluated for some language, that language is temporarily
2082 made current so that `c-lang-const' without an explicit language can
2083 be used inside VAL to refer to the value of a language constant in the
2084 same language. That is particularly useful if LANG is t.
2086 VAL is not evaluated right away but rather when the value is requested
2087 with `c-lang-const'. Thus it's possible to use `c-lang-const' inside
2088 VAL to refer to language constants that haven't been defined yet.
2089 However, if the definition of a language constant is in another file
2090 then that file must be loaded \(at compile time) before it's safe to
2091 reference the constant.
2093 The assignments in ARGS are processed in sequence like `setq', so
2094 \(c-lang-const NAME) may be used inside a VAL to refer to the last
2095 assigned value to this language constant, or a value that it has
2096 gotten in another earlier loaded file.
2098 To work well with repeated loads and interactive reevaluation, only
2099 one `c-lang-defconst' for each NAME is permitted per file. If there
2100 already is one it will be completely replaced; the value in the
2101 earlier definition will not affect `c-lang-const' on the same
2102 constant. A file is identified by its base name."
2104 (let* ((sym (intern (symbol-name name) c-lang-constants))
2105 ;; Make `c-lang-const' expand to a straightforward call to
2106 ;; `c-get-lang-constant' in `c--macroexpand-all' below.
2108 ;; (The default behavior, i.e. to expand to a call inside
2109 ;; `eval-when-compile' should be equivalent, since that macro
2110 ;; should only expand to its content if it's used inside a
2111 ;; form that's already evaluated at compile time. It's
2112 ;; however necessary to use our cover macro
2113 ;; `cc-eval-when-compile' due to bugs in `eval-when-compile',
2114 ;; and it expands to a bulkier form that in this case only is
2115 ;; unnecessary garbage that we don't want to store in the
2116 ;; language constant source definitions.)
2117 (c-lang-const-expansion 'call)
2118 (c-langs-are-parametric t)
2119 (file (intern
2120 (or (c-get-current-file)
2121 (error "`c-lang-defconst' can only be used in a file"))))
2122 bindings
2123 pre-files)
2125 (or (symbolp name)
2126 (error "Not a symbol: %S" name))
2128 (when (stringp (car-safe args))
2129 ;; The docstring is hardly used anywhere since there's no normal
2130 ;; symbol to attach it to. It's primarily for getting the right
2131 ;; format in the source.
2132 (put sym 'variable-documentation (car args))
2133 (setq args (cdr args)))
2135 (or args
2136 (error "No assignments in `c-lang-defconst' for %S" name))
2138 ;; Rework ARGS to an association list to make it easier to handle.
2139 ;; It's reversed at the same time to make it easier to implement
2140 ;; the demand-driven (i.e. reversed) evaluation in `c-lang-const'.
2141 (while args
2142 (let ((assigned-mode
2143 (cond ((eq (car args) t) t)
2144 ((symbolp (car args))
2145 (list (intern (concat (symbol-name (car args))
2146 "-mode"))))
2147 ((listp (car args))
2148 (mapcar (lambda (lang)
2149 (or (symbolp lang)
2150 (error "Not a list of symbols: %S"
2151 (car args)))
2152 (intern (concat (symbol-name lang)
2153 "-mode")))
2154 (car args)))
2155 (t (error "Not a symbol or a list of symbols: %S"
2156 (car args)))))
2157 val)
2159 (or (cdr args)
2160 (error "No value for %S" (car args)))
2161 (setq args (cdr args)
2162 val (car args))
2164 ;; Emacs has a weird bug where it seems to fail to read
2165 ;; backquote lists from byte compiled files correctly (,@
2166 ;; forms, to be specific), so make sure the bindings in the
2167 ;; expansion below don't contain any backquote stuff.
2168 ;; (XEmacs handles it correctly and doesn't need this for that
2169 ;; reason, but we also use this expansion handle
2170 ;; `c-lang-defconst-eval-immediately' and to register
2171 ;; dependencies on the `c-lang-const's in VAL.)
2172 (setq val (c--macroexpand-all val))
2174 (setq bindings `(cons (cons ',assigned-mode (lambda () ,val)) ,bindings)
2175 args (cdr args))))
2177 ;; Compile in the other files that have provided source
2178 ;; definitions for this symbol, to make sure the order in the
2179 ;; `source' property is correct even when files are loaded out of
2180 ;; order.
2181 (setq pre-files (mapcar 'car (get sym 'source)))
2182 (if (memq file pre-files)
2183 ;; This can happen when the source file (e.g. cc-langs.el) is first
2184 ;; loaded as source, setting a 'source property entry, and then itself
2185 ;; being compiled.
2186 (setq pre-files (cdr (memq file pre-files))))
2187 ;; Reverse to get the right load order.
2188 (setq pre-files (nreverse pre-files))
2190 `(eval-and-compile
2191 (c-define-lang-constant ',name ,bindings
2192 ,@(and pre-files `(',pre-files))))))
2194 (put 'c-lang-defconst 'lisp-indent-function 1)
2195 ;(eval-after-load "edebug" ; 2006-07-09: def-edebug-spec is now in subr.el.
2197 (def-edebug-spec c-lang-defconst
2198 (&define name [&optional stringp] [&rest sexp def-form]))
2200 (defun c-define-lang-constant (name bindings &optional pre-files)
2201 ;; Used by `c-lang-defconst'.
2203 (let* ((sym (intern (symbol-name name) c-lang-constants))
2204 (source (get sym 'source))
2205 (file (intern
2206 (or (c-get-current-file)
2207 (error "`c-lang-defconst' must be used in a file"))))
2208 (elem (assq file source)))
2210 ;;(when (cdr-safe elem)
2211 ;; (message "Language constant %s redefined in %S" name file))
2213 ;; Note that the order in the source alist is relevant. Like how
2214 ;; `c-lang-defconst' reverses the bindings, this reverses the
2215 ;; order between files so that the last to evaluate comes first.
2216 (unless elem
2217 (while pre-files
2218 (unless (assq (car pre-files) source)
2219 (setq source (cons (list (car pre-files)) source)))
2220 (setq pre-files (cdr pre-files)))
2221 (put sym 'source (cons (setq elem (list file)) source)))
2223 (setcdr elem bindings)
2225 ;; Bind the symbol as a variable, or clear any earlier evaluated
2226 ;; value it has.
2227 (set sym nil)
2229 ;; Clear the evaluated values that depend on this source.
2230 (let ((agenda (get sym 'dependents))
2231 (visited (make-vector 101 0))
2232 ptr)
2233 (while agenda
2234 (setq sym (car agenda)
2235 agenda (cdr agenda))
2236 (intern (symbol-name sym) visited)
2237 (set sym nil)
2238 (setq ptr (get sym 'dependents))
2239 (while ptr
2240 (setq sym (car ptr)
2241 ptr (cdr ptr))
2242 (unless (intern-soft (symbol-name sym) visited)
2243 (setq agenda (cons sym agenda))))))
2245 name))
2247 (defmacro c-lang-const (name &optional lang)
2248 "Get the mode specific value of the language constant NAME in language LANG.
2249 LANG is the name of the language, i.e. the mode name without the
2250 \"-mode\" suffix. If used inside `c-lang-defconst' or
2251 `c-lang-defvar', LANG may be left out to refer to the current
2252 language. NAME and LANG are not evaluated so they should not be
2253 quoted."
2255 (or (symbolp name)
2256 (error "Not a symbol: %S" name))
2257 (or (symbolp lang)
2258 (error "Not a symbol: %S" lang))
2260 (let ((sym (intern (symbol-name name) c-lang-constants))
2261 (mode (when lang (intern (concat (symbol-name lang) "-mode")))))
2263 (or (get mode 'c-mode-prefix) (null mode)
2264 (error "Unknown language %S: no `c-mode-prefix' property"
2265 lang))
2267 (if (eq c-lang-const-expansion 'immediate)
2268 ;; No need to find out the source file(s) when we evaluate
2269 ;; immediately since all the info is already there in the
2270 ;; `source' property.
2271 `',(c-get-lang-constant name nil mode)
2273 (let ((source-files
2274 (let ((file (c-get-current-file)))
2275 (if file (setq file (intern file)))
2276 ;; Get the source file(s) that must be loaded to get the value
2277 ;; of the constant. If the symbol isn't defined yet we assume
2278 ;; that its definition will come later in this file, and thus
2279 ;; are no file dependencies needed.
2280 (nreverse
2281 ;; Reverse to get the right load order.
2282 (c--mapcan (lambda (elem)
2283 (if (eq file (car elem))
2284 nil ; Exclude our own file.
2285 (list (car elem))))
2286 (get sym 'source)))))
2288 ;; Make some effort to do a compact call to
2289 ;; `c-get-lang-constant' since it will be compiled in.
2290 (args (and mode `(',mode))))
2292 (if (or source-files args)
2293 (push (and source-files `',source-files) args))
2295 (if (or (eq c-lang-const-expansion 'call)
2296 (and (not c-lang-const-expansion)
2297 (not mode))
2298 (not (cc-bytecomp-is-compiling)))
2299 ;; Either a straight call is requested in the context, or
2300 ;; we're in an "uncontrolled" context and got no language,
2301 ;; or we're not being byte compiled so the compile time
2302 ;; stuff below is unnecessary.
2303 `(c-get-lang-constant ',name ,@args)
2305 ;; Being compiled. If the loading and compiling version is
2306 ;; the same we use a value that is evaluated at compile time,
2307 ;; otherwise it's evaluated at runtime.
2308 `(if (eq c-version-sym ',c-version-sym)
2309 (cc-eval-when-compile
2310 (c-get-lang-constant ',name ,@args))
2311 (c-get-lang-constant ',name ,@args)))))))
2313 (defvar c-lang-constants-under-evaluation nil
2314 "Alist of constants in the process of being evaluated.
2315 The `cdr' of each entry indicates how far we've looked in the list
2316 of definitions, so that the def for var FOO in c-mode can be defined in
2317 terms of the def for that same var FOO (which will then rely on the
2318 fallback definition for all modes, to break the cycle).")
2320 (defconst c-lang--novalue "novalue")
2322 (defun c-get-lang-constant (name &optional source-files mode)
2323 ;; Used by `c-lang-const'.
2325 (or mode
2326 (setq mode c-buffer-is-cc-mode)
2327 (error "No current language"))
2329 (let* ((sym (intern (symbol-name name) c-lang-constants))
2330 (source (get sym 'source))
2331 elem
2332 (eval-in-sym (and c-lang-constants-under-evaluation
2333 (caar c-lang-constants-under-evaluation))))
2335 ;; Record the dependencies between this symbol and the one we're
2336 ;; being evaluated in.
2337 (when eval-in-sym
2338 (or (memq eval-in-sym (get sym 'dependents))
2339 (put sym 'dependents (cons eval-in-sym (get sym 'dependents)))))
2341 ;; Make sure the source files have entries on the `source'
2342 ;; property so that loading will take place when necessary.
2343 (while source-files
2344 (unless (assq (car source-files) source)
2345 (put sym 'source
2346 (setq source (cons (list (car source-files)) source)))
2347 ;; Might pull in more definitions which affect the value. The
2348 ;; clearing of dependent values etc is done when the
2349 ;; definition is encountered during the load; this is just to
2350 ;; jump past the check for a cached value below.
2351 (set sym nil))
2352 (setq source-files (cdr source-files)))
2354 (if (and (boundp sym)
2355 (setq elem (assq mode (symbol-value sym))))
2356 (cdr elem)
2358 ;; Check if an evaluation of this symbol is already underway.
2359 ;; In that case we just continue with the "assignment" before
2360 ;; the one currently being evaluated, thereby creating the
2361 ;; illusion if a `setq'-like sequence of assignments.
2362 (let* ((c-buffer-is-cc-mode mode)
2363 (source-pos
2364 (or (assq sym c-lang-constants-under-evaluation)
2365 (cons sym (vector source nil))))
2366 ;; Append `c-lang-constants-under-evaluation' even if an
2367 ;; earlier entry is found. It's only necessary to get
2368 ;; the recording of dependencies above correct.
2369 (c-lang-constants-under-evaluation
2370 (cons source-pos c-lang-constants-under-evaluation))
2371 (fallback (get mode 'c-fallback-mode))
2372 value
2373 ;; Make sure the recursion limits aren't very low
2374 ;; since the `c-lang-const' dependencies can go deep.
2375 (max-specpdl-size (max max-specpdl-size 3000))
2376 (max-lisp-eval-depth (max max-lisp-eval-depth 1000)))
2378 (if (if fallback
2379 (let ((backup-source-pos (copy-sequence (cdr source-pos))))
2380 (and
2381 ;; First try the original mode but don't accept an
2382 ;; entry matching all languages since the fallback
2383 ;; mode might have an explicit entry before that.
2384 (eq (setq value (c-find-assignment-for-mode
2385 (cdr source-pos) mode nil name))
2386 c-lang--novalue)
2387 ;; Try again with the fallback mode from the
2388 ;; original position. Note that
2389 ;; `c-buffer-is-cc-mode' still is the real mode if
2390 ;; language parameterization takes place.
2391 (eq (setq value (c-find-assignment-for-mode
2392 (setcdr source-pos backup-source-pos)
2393 fallback t name))
2394 c-lang--novalue)))
2395 ;; A simple lookup with no fallback mode.
2396 (eq (setq value (c-find-assignment-for-mode
2397 (cdr source-pos) mode t name))
2398 c-lang--novalue))
2399 (error
2400 "`%s' got no (prior) value in %S (might be a cyclic reference)"
2401 name mode))
2403 (condition-case err
2404 (setq value (funcall value))
2405 (error
2406 ;; Print a message to aid in locating the error. We don't
2407 ;; print the error itself since that will be done later by
2408 ;; some caller higher up.
2409 (message "Eval error in the `c-lang-defconst' for `%S' in %s:"
2410 sym mode)
2411 (makunbound sym)
2412 (signal (car err) (cdr err))))
2414 (set sym (cons (cons mode value) (symbol-value sym)))
2415 value))))
2417 (defun c-find-assignment-for-mode (source-pos mode match-any-lang _name)
2418 ;; Find the first assignment entry that applies to MODE at or after
2419 ;; SOURCE-POS. If MATCH-ANY-LANG is non-nil, entries with t as
2420 ;; the language list are considered to match, otherwise they don't.
2421 ;; On return SOURCE-POS is updated to point to the next assignment
2422 ;; after the returned one. If no assignment is found,
2423 ;; `c-lang--novalue' is returned as a magic value.
2425 ;; SOURCE-POS is a vector that points out a specific assignment in
2426 ;; the double alist that's used in the `source' property. The first
2427 ;; element is the position in the top alist which is indexed with
2428 ;; the source files, and the second element is the position in the
2429 ;; nested bindings alist.
2431 ;; NAME is only used for error messages.
2433 (catch 'found
2434 (let ((file-entry (elt source-pos 0))
2435 (assignment-entry (elt source-pos 1))
2436 assignment)
2438 (while (if assignment-entry
2440 ;; Handled the last assignment from one file, begin on the
2441 ;; next. Due to the check in `c-lang-defconst', we know
2442 ;; there's at least one.
2443 (when file-entry
2445 (unless (aset source-pos 1
2446 (setq assignment-entry (cdar file-entry)))
2447 ;; The file containing the source definitions has not
2448 ;; been loaded.
2449 (let ((file (symbol-name (caar file-entry)))
2450 (c-lang-constants-under-evaluation nil))
2451 ;;(message (concat "Loading %s to get the source "
2452 ;; "value for language constant %s")
2453 ;; file name)
2454 (load file nil t))
2456 (unless (setq assignment-entry (cdar file-entry))
2457 ;; The load didn't fill in the source for the
2458 ;; constant as expected. The situation is
2459 ;; probably that a derived mode was written for
2460 ;; and compiled with another version of CC Mode,
2461 ;; and the requested constant isn't in the
2462 ;; currently loaded one. Put in a dummy
2463 ;; assignment that matches no language.
2464 (setcdr (car file-entry)
2465 (setq assignment-entry (list (list nil))))))
2467 (aset source-pos 0 (setq file-entry (cdr file-entry)))
2470 (setq assignment (car assignment-entry))
2471 (aset source-pos 1
2472 (setq assignment-entry (cdr assignment-entry)))
2474 (when (if (listp (car assignment))
2475 (memq mode (car assignment))
2476 match-any-lang)
2477 (throw 'found (cdr assignment))))
2479 c-lang--novalue)))
2481 (defun c-lang-major-mode-is (mode)
2482 ;; `c-major-mode-is' expands to a call to this function inside
2483 ;; `c-lang-defconst'. Here we also match the mode(s) against any
2484 ;; fallback modes for the one in `c-buffer-is-cc-mode', so that
2485 ;; e.g. (c-major-mode-is 'c++-mode) is true in a derived language
2486 ;; that has c++-mode as base mode.
2487 (unless (listp mode)
2488 (setq mode (list mode)))
2489 (let (match (buf-mode c-buffer-is-cc-mode))
2490 (while (if (memq buf-mode mode)
2491 (progn
2492 (setq match t)
2493 nil)
2494 (setq buf-mode (get buf-mode 'c-fallback-mode))))
2495 match))
2498 (cc-provide 'cc-defs)
2500 ;; Local Variables:
2501 ;; indent-tabs-mode: t
2502 ;; tab-width: 8
2503 ;; End:
2504 ;;; cc-defs.el ends here