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