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