]> code.delx.au - gnu-emacs/blob - lisp/progmodes/cc-defs.el
Update copyright for release of 22.1 for progmodes directory.
[gnu-emacs] / lisp / progmodes / cc-defs.el
1 ;;; cc-defs.el --- compile time definitions for CC Mode
2
3 ;; Copyright (C) 1985,1987,1992-2003, 2004, 2005 Free Software Foundation, Inc.
4
5 ;; Authors: 1998- Martin Stjernholm
6 ;; 1992-1999 Barry A. Warsaw
7 ;; 1987 Dave Detlefs and Stewart Clamen
8 ;; 1985 Richard M. Stallman
9 ;; Maintainer: bug-cc-mode@gnu.org
10 ;; Created: 22-Apr-1997 (split from cc-mode.el)
11 ;; Version: See cc-mode.el
12 ;; Keywords: c languages oop
13
14 ;; This file is part of GNU Emacs.
15
16 ;; GNU Emacs is free software; you can redistribute it and/or modify
17 ;; it under the terms of the GNU General Public License as published by
18 ;; the Free Software Foundation; either version 2, or (at your option)
19 ;; any later version.
20
21 ;; GNU Emacs is distributed in the hope that it will be useful,
22 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
23 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 ;; GNU General Public License for more details.
25
26 ;; You should have received a copy of the GNU General Public License
27 ;; along with GNU Emacs; see the file COPYING. If not, write to
28 ;; the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
29 ;; Boston, MA 02110-1301, USA.
30
31 ;;; Commentary:
32
33 ;; This file contains macros, defsubsts, and various other things that
34 ;; must be loaded early both during compilation and at runtime.
35
36 ;;; Code:
37
38 (eval-when-compile
39 (let ((load-path
40 (if (and (boundp 'byte-compile-dest-file)
41 (stringp byte-compile-dest-file))
42 (cons (file-name-directory byte-compile-dest-file) load-path)
43 load-path)))
44 (load "cc-bytecomp" nil t)))
45
46 ;; `require' in XEmacs doesn't have the third NOERROR argument.
47 (condition-case nil (require 'regexp-opt) (file-error nil))
48
49 ;; Silence the compiler.
50 (cc-bytecomp-defvar c-enable-xemacs-performance-kludge-p) ; In cc-vars.el
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
58 (cc-bytecomp-defvar parse-sexp-lookup-properties) ; Emacs 20+
59 (cc-bytecomp-defvar text-property-default-nonsticky) ; Emacs 21
60 (cc-bytecomp-defvar lookup-syntax-properties) ; XEmacs 21
61 (cc-bytecomp-defun string-to-syntax) ; Emacs 21
62 (cc-bytecomp-defun regexp-opt-depth) ; (X)Emacs 20+
63
64 \f
65 ;; cc-fix.el contains compatibility macros that should be used if
66 ;; needed.
67 (eval-and-compile
68 (if (or (not (fboundp 'functionp))
69 (not (condition-case nil
70 (progn (eval '(char-before)) t)
71 (error nil)))
72 (not (condition-case nil
73 (progn (eval '(char-after)) t)
74 (error nil)))
75 (not (fboundp 'when))
76 (not (fboundp 'unless))
77 (not (fboundp 'regexp-opt))
78 (not (cc-bytecomp-fboundp 'regexp-opt-depth))
79 (/= (regexp-opt-depth "\\(\\(\\)\\)") 2))
80 (cc-load "cc-fix")
81 (defalias 'c-regexp-opt 'regexp-opt)
82 (defalias 'c-regexp-opt-depth 'regexp-opt-depth)))
83
84 (eval-after-load "font-lock"
85 '(if (and (not (featurep 'cc-fix)) ; only load the file once.
86 (let (font-lock-keywords)
87 (font-lock-compile-keywords '("\\<\\>"))
88 font-lock-keywords)) ; did the previous call foul this up?
89 (load "cc-fix")))
90
91 ;; The above takes care of the delayed loading, but this is necessary
92 ;; to ensure correct byte compilation.
93 (eval-when-compile
94 (if (and (not (featurep 'cc-fix))
95 (progn
96 (require 'font-lock)
97 (let (font-lock-keywords)
98 (font-lock-compile-keywords '("\\<\\>"))
99 font-lock-keywords)))
100 (cc-load "cc-fix")))
101
102 (eval-when-compile (require 'cl))
103
104 \f
105 ;;; Variables also used at compile time.
106
107 (defconst c-version "5.30.10"
108 "CC Mode version number.")
109
110 (defconst c-version-sym (intern c-version))
111 ;; A little more compact and faster in comparisons.
112
113 (defvar c-buffer-is-cc-mode nil
114 "Non-nil for all buffers with a major mode derived from CC Mode.
115 Otherwise, this variable is nil. I.e. this variable is non-nil for
116 `c-mode', `c++-mode', `objc-mode', `java-mode', `idl-mode',
117 `pike-mode', `awk-mode', and any other non-CC Mode mode that calls
118 `c-initialize-cc-mode'. The value is the mode symbol itself
119 \(i.e. `c-mode' etc) of the original CC Mode mode, or just t if it's
120 not known.")
121 (make-variable-buffer-local 'c-buffer-is-cc-mode)
122
123 ;; Have to make `c-buffer-is-cc-mode' permanently local so that it
124 ;; survives the initialization of the derived mode.
125 (put 'c-buffer-is-cc-mode 'permanent-local t)
126
127 \f
128 ;; The following is used below during compilation.
129 (eval-and-compile
130 (defvar c-inside-eval-when-compile nil)
131
132 (defmacro cc-eval-when-compile (&rest body)
133 "Like `progn', but evaluates the body at compile time.
134 The result of the body appears to the compiler as a quoted constant.
135
136 This variant works around bugs in `eval-when-compile' in various
137 \(X)Emacs versions. See cc-defs.el for details."
138
139 (if c-inside-eval-when-compile
140 ;; XEmacs 21.4.6 has a bug in `eval-when-compile' in that it
141 ;; evaluates its body at macro expansion time if it's nested
142 ;; inside another `eval-when-compile'. So we use a dynamically
143 ;; bound variable to avoid nesting them.
144 `(progn ,@body)
145
146 `(eval-when-compile
147 ;; In all (X)Emacsen so far, `eval-when-compile' byte compiles
148 ;; its contents before evaluating it. That can cause forms to
149 ;; be compiled in situations they aren't intended to be
150 ;; compiled.
151 ;;
152 ;; Example: It's not possible to defsubst a primitive, e.g. the
153 ;; following will produce an error (in any emacs flavor), since
154 ;; `nthcdr' is a primitive function that's handled specially by
155 ;; the byte compiler and thus can't be redefined:
156 ;;
157 ;; (defsubst nthcdr (val) val)
158 ;;
159 ;; `defsubst', like `defmacro', needs to be evaluated at
160 ;; compile time, so this will produce an error during byte
161 ;; compilation.
162 ;;
163 ;; CC Mode occasionally needs to do things like this for
164 ;; cross-emacs compatibility. It therefore uses the following
165 ;; to conditionally do a `defsubst':
166 ;;
167 ;; (eval-when-compile
168 ;; (if (not (fboundp 'foo))
169 ;; (defsubst foo ...)))
170 ;;
171 ;; But `eval-when-compile' byte compiles its contents and
172 ;; _then_ evaluates it (in all current emacs versions, up to
173 ;; and including Emacs 20.6 and XEmacs 21.1 as of this
174 ;; writing). So this will still produce an error, since the
175 ;; byte compiler will get to the defsubst anyway. That's
176 ;; arguably a bug because the point with `eval-when-compile' is
177 ;; that it should evaluate rather than compile its contents.
178 ;;
179 ;; We get around it by expanding the body to a quoted
180 ;; constant that we eval. That otoh introduce a problem in
181 ;; that a returned lambda expression doesn't get byte
182 ;; compiled (even if `function' is used).
183 (eval '(let ((c-inside-eval-when-compile t)) ,@body)))))
184
185 (put 'cc-eval-when-compile 'lisp-indent-hook 0))
186
187 \f
188 ;;; Macros.
189
190 (defmacro c-point (position &optional point)
191 "Return the value of certain commonly referenced POSITIONs relative to POINT.
192 The current point is used if POINT isn't specified. POSITION can be
193 one of the following symbols:
194
195 `bol' -- beginning of line
196 `eol' -- end of line
197 `bod' -- beginning of defun
198 `eod' -- end of defun
199 `boi' -- beginning of indentation
200 `ionl' -- indentation of next line
201 `iopl' -- indentation of previous line
202 `bonl' -- beginning of next line
203 `eonl' -- end of next line
204 `bopl' -- beginning of previous line
205 `eopl' -- end of previous line
206
207 If the referenced position doesn't exist, the closest accessible point
208 to it is returned. This function does not modify point or mark.
209
210 This function does not do any hidden buffer changes."
211
212 (if (eq (car-safe position) 'quote)
213 (let ((position (eval position)))
214 (cond
215
216 ((eq position 'bol)
217 (if (and (fboundp 'line-beginning-position) (not point))
218 `(line-beginning-position)
219 `(save-excursion
220 ,@(if point `((goto-char ,point)))
221 (beginning-of-line)
222 (point))))
223
224 ((eq position 'eol)
225 (if (and (fboundp 'line-end-position) (not point))
226 `(line-end-position)
227 `(save-excursion
228 ,@(if point `((goto-char ,point)))
229 (end-of-line)
230 (point))))
231
232 ((eq position 'boi)
233 `(save-excursion
234 ,@(if point `((goto-char ,point)))
235 (back-to-indentation)
236 (point)))
237
238 ((eq position 'bod)
239 `(save-excursion
240 ,@(if point `((goto-char ,point)))
241 (c-beginning-of-defun-1)
242 (point)))
243
244 ((eq position 'eod)
245 `(save-excursion
246 ,@(if point `((goto-char ,point)))
247 (c-end-of-defun-1)
248 (point)))
249
250 ((eq position 'bopl)
251 (if (and (fboundp 'line-beginning-position) (not point))
252 `(line-beginning-position 0)
253 `(save-excursion
254 ,@(if point `((goto-char ,point)))
255 (forward-line -1)
256 (point))))
257
258 ((eq position 'bonl)
259 (if (and (fboundp 'line-beginning-position) (not point))
260 `(line-beginning-position 2)
261 `(save-excursion
262 ,@(if point `((goto-char ,point)))
263 (forward-line 1)
264 (point))))
265
266 ((eq position 'eopl)
267 (if (and (fboundp 'line-end-position) (not point))
268 `(line-end-position 0)
269 `(save-excursion
270 ,@(if point `((goto-char ,point)))
271 (beginning-of-line)
272 (or (bobp) (backward-char))
273 (point))))
274
275 ((eq position 'eonl)
276 (if (and (fboundp 'line-end-position) (not point))
277 `(line-end-position 2)
278 `(save-excursion
279 ,@(if point `((goto-char ,point)))
280 (forward-line 1)
281 (end-of-line)
282 (point))))
283
284 ((eq position 'iopl)
285 `(save-excursion
286 ,@(if point `((goto-char ,point)))
287 (forward-line -1)
288 (back-to-indentation)
289 (point)))
290
291 ((eq position 'ionl)
292 `(save-excursion
293 ,@(if point `((goto-char ,point)))
294 (forward-line 1)
295 (back-to-indentation)
296 (point)))
297
298 (t (error "Unknown buffer position requested: %s" position))))
299
300 ;;(message "c-point long expansion")
301 `(save-excursion
302 ,@(if point `((goto-char ,point)))
303 (let ((position ,position))
304 (cond
305 ((eq position 'bol) (beginning-of-line))
306 ((eq position 'eol) (end-of-line))
307 ((eq position 'boi) (back-to-indentation))
308 ((eq position 'bod) (c-beginning-of-defun-1))
309 ((eq position 'eod) (c-end-of-defun-1))
310 ((eq position 'bopl) (forward-line -1))
311 ((eq position 'bonl) (forward-line 1))
312 ((eq position 'eopl) (progn
313 (beginning-of-line)
314 (or (bobp) (backward-char))))
315 ((eq position 'eonl) (progn
316 (forward-line 1)
317 (end-of-line)))
318 ((eq position 'iopl) (progn
319 (forward-line -1)
320 (back-to-indentation)))
321 ((eq position 'ionl) (progn
322 (forward-line 1)
323 (back-to-indentation)))
324 (t (error "Unknown buffer position requested: %s" position))))
325 (point))))
326
327 (defmacro c-safe (&rest body)
328 ;; safely execute BODY, return nil if an error occurred
329 ;;
330 ;; This function does not do any hidden buffer changes.
331 `(condition-case nil
332 (progn ,@body)
333 (error nil)))
334 (put 'c-safe 'lisp-indent-function 0)
335
336 ;; The following is essentially `save-buffer-state' from lazy-lock.el.
337 ;; It ought to be a standard macro.
338 (defmacro c-save-buffer-state (varlist &rest body)
339 "Bind variables according to VARLIST (in `let*' style) and eval BODY,
340 then restore the buffer state under the assumption that no significant
341 modification has been made. A change is considered significant if it
342 affects the buffer text in any way that isn't completely restored
343 again. Changes in text properties like `face' or `syntax-table' are
344 considered insignificant. This macro allows text properties to be
345 changed, even in a read-only buffer.
346
347 The return value is the value of the last form in BODY."
348 `(let* ((modified (buffer-modified-p)) (buffer-undo-list t)
349 (inhibit-read-only t) (inhibit-point-motion-hooks t)
350 before-change-functions after-change-functions
351 deactivate-mark
352 ,@varlist)
353 (prog1 (progn ,@body)
354 (and (not modified)
355 (buffer-modified-p)
356 (set-buffer-modified-p nil)))))
357 (put 'c-save-buffer-state 'lisp-indent-function 1)
358
359 (defmacro c-forward-syntactic-ws (&optional limit)
360 "Forward skip over syntactic whitespace.
361 Syntactic whitespace is defined as whitespace characters, comments,
362 and preprocessor directives. However if point starts inside a comment
363 or preprocessor directive, the content of it is not treated as
364 whitespace.
365
366 LIMIT sets an upper limit of the forward movement, if specified. If
367 LIMIT or the end of the buffer is reached inside a comment or
368 preprocessor directive, the point will be left there.
369
370 Note that this function might do hidden buffer changes. See the
371 comment at the start of cc-engine.el for more info."
372 (if limit
373 `(save-restriction
374 (narrow-to-region (point-min) (or ,limit (point-max)))
375 (c-forward-sws))
376 '(c-forward-sws)))
377
378 (defmacro c-backward-syntactic-ws (&optional limit)
379 "Backward skip over syntactic whitespace.
380 Syntactic whitespace is defined as whitespace characters, comments,
381 and preprocessor directives. However if point starts inside a comment
382 or preprocessor directive, the content of it is not treated as
383 whitespace.
384
385 LIMIT sets a lower limit of the backward movement, if specified. If
386 LIMIT is reached inside a line comment or preprocessor directive then
387 the point is moved into it past the whitespace at the end.
388
389 Note that this function might do hidden buffer changes. See the
390 comment at the start of cc-engine.el for more info."
391 (if limit
392 `(save-restriction
393 (narrow-to-region (or ,limit (point-min)) (point-max))
394 (c-backward-sws))
395 '(c-backward-sws)))
396
397 (defmacro c-forward-sexp (&optional count)
398 "Move forward across COUNT balanced expressions.
399 A negative COUNT means move backward. Signal an error if the move
400 fails for any reason.
401
402 This is like `forward-sexp' except that it isn't interactive and does
403 not do any user friendly adjustments of the point and that it isn't
404 susceptible to user configurations such as disabling of signals in
405 certain situations.
406
407 This function does not do any hidden buffer changes."
408 (or count (setq count 1))
409 `(goto-char (or (scan-sexps (point) ,count)
410 ,(if (numberp count)
411 (if (> count 0) `(point-max) `(point-min))
412 `(if (> ,count 0) (point-max) (point-min))))))
413
414 (defmacro c-backward-sexp (&optional count)
415 "See `c-forward-sexp' and reverse directions."
416 (or count (setq count 1))
417 `(c-forward-sexp ,(if (numberp count) (- count) `(- ,count))))
418
419 (defmacro c-safe-scan-lists (from count depth)
420 "Like `scan-lists' but returns nil instead of signaling errors.
421
422 This function does not do any hidden buffer changes."
423 (if (featurep 'xemacs)
424 `(scan-lists ,from ,count ,depth nil t)
425 `(c-safe (scan-lists ,from ,count ,depth))))
426
427 \f
428 ;; Wrappers for common scan-lists cases, mainly because it's almost
429 ;; impossible to get a feel for how that function works.
430
431 (defmacro c-up-list-forward (&optional pos)
432 "Return the first position after the list sexp containing POS,
433 or nil if no such position exists. The point is used if POS is left out.
434
435 This function does not do any hidden buffer changes."
436 `(c-safe-scan-lists ,(or pos `(point)) 1 1))
437
438 (defmacro c-up-list-backward (&optional pos)
439 "Return the position of the start of the list sexp containing POS,
440 or nil if no such position exists. The point is used if POS is left out.
441
442 This function does not do any hidden buffer changes."
443 `(c-safe-scan-lists ,(or pos `(point)) -1 1))
444
445 (defmacro c-down-list-forward (&optional pos)
446 "Return the first position inside the first list sexp after POS,
447 or nil if no such position exists. The point is used if POS is left out.
448
449 This function does not do any hidden buffer changes."
450 `(c-safe-scan-lists ,(or pos `(point)) 1 -1))
451
452 (defmacro c-down-list-backward (&optional pos)
453 "Return the last position inside the last list sexp before POS,
454 or nil if no such position exists. The point is used if POS is left out.
455
456 This function does not do any hidden buffer changes."
457 `(c-safe-scan-lists ,(or pos `(point)) -1 -1))
458
459 (defmacro c-go-up-list-forward (&optional pos)
460 "Move the point to the first position after the list sexp containing POS,
461 or the point if POS is left out. Return t if such a position exists,
462 otherwise nil is returned and the point isn't moved.
463
464 This function does not do any hidden buffer changes."
465 `(c-safe (goto-char (scan-lists ,(or pos `(point)) 1 1)) t))
466
467 (defmacro c-go-up-list-backward (&optional pos)
468 "Move the point to the position of the start of the list sexp containing POS,
469 or the point if POS is left out. Return t if such a position exists,
470 otherwise nil is returned and the point isn't moved.
471
472 This function does not do any hidden buffer changes."
473 `(c-safe (goto-char (scan-lists ,(or pos `(point)) -1 1)) t))
474
475 (defmacro c-go-down-list-forward (&optional pos)
476 "Move the point to the first position inside the first list sexp after POS,
477 or the point if POS is left out. Return t if such a position exists,
478 otherwise nil is returned and the point isn't moved.
479
480 This function does not do any hidden buffer changes."
481 `(c-safe (goto-char (scan-lists ,(or pos `(point)) 1 -1)) t))
482
483 (defmacro c-go-down-list-backward (&optional pos)
484 "Move the point to the last position inside the last list sexp before POS,
485 or the point if POS is left out. Return t if such a position exists,
486 otherwise nil is returned and the point isn't moved.
487
488 This function does not do any hidden buffer changes."
489 `(c-safe (goto-char (scan-lists ,(or pos `(point)) -1 -1)) t))
490
491 \f
492 (defmacro c-beginning-of-defun-1 ()
493 ;; Wrapper around beginning-of-defun.
494 ;;
495 ;; NOTE: This function should contain the only explicit use of
496 ;; beginning-of-defun in CC Mode. Eventually something better than
497 ;; b-o-d will be available and this should be the only place the
498 ;; code needs to change. Everything else should use
499 ;; (c-beginning-of-defun-1)
500 ;;
501 ;; This is really a bit too large to be a macro but that isn't a
502 ;; problem as long as it only is used in one place in
503 ;; `c-parse-state'.
504 ;;
505 ;; This function does not do any hidden buffer changes.
506
507 `(progn
508 (if (and ,(cc-bytecomp-fboundp 'buffer-syntactic-context-depth)
509 c-enable-xemacs-performance-kludge-p)
510 ,(when (cc-bytecomp-fboundp 'buffer-syntactic-context-depth)
511 ;; XEmacs only. This can improve the performance of
512 ;; c-parse-state to between 3 and 60 times faster when
513 ;; braces are hung. It can also degrade performance by
514 ;; about as much when braces are not hung.
515 '(let (pos)
516 (while (not pos)
517 (save-restriction
518 (widen)
519 (setq pos (c-safe-scan-lists
520 (point) -1 (buffer-syntactic-context-depth))))
521 (cond
522 ((bobp) (setq pos (point-min)))
523 ((not pos)
524 (let ((distance (skip-chars-backward "^{")))
525 ;; unbalanced parenthesis, while illegal C code,
526 ;; shouldn't cause an infloop! See unbal.c
527 (when (zerop distance)
528 ;; Punt!
529 (beginning-of-defun)
530 (setq pos (point)))))
531 ((= pos 0))
532 ((not (eq (char-after pos) ?{))
533 (goto-char pos)
534 (setq pos nil))
535 ))
536 (goto-char pos)))
537 ;; Emacs, which doesn't have buffer-syntactic-context-depth
538 (beginning-of-defun))
539 ;; if defun-prompt-regexp is non-nil, b-o-d won't leave us at the
540 ;; open brace.
541 (and defun-prompt-regexp
542 (looking-at defun-prompt-regexp)
543 (goto-char (match-end 0)))))
544
545 (defmacro c-benign-error (format &rest args)
546 ;; Formats an error message for the echo area and dings, i.e. like
547 ;; `error' but doesn't abort.
548 ;;
549 ;; This function does not do any hidden buffer changes.
550 `(progn
551 (message ,format ,@args)
552 (ding)))
553
554 (defmacro c-update-modeline ()
555 ;; set the c-auto-hungry-string for the correct designation on the modeline
556 ;;
557 ;; This function does not do any hidden buffer changes.
558 `(progn
559 (setq c-auto-hungry-string
560 (if c-auto-newline
561 (if c-hungry-delete-key "/ah" "/a")
562 (if c-hungry-delete-key "/h" nil)))
563 (force-mode-line-update)))
564
565 (defmacro c-with-syntax-table (table &rest code)
566 ;; Temporarily switches to the specified syntax table in a failsafe
567 ;; way to execute code.
568 ;;
569 ;; This function does not do any hidden buffer changes.
570 `(let ((c-with-syntax-table-orig-table (syntax-table)))
571 (unwind-protect
572 (progn
573 (set-syntax-table ,table)
574 ,@code)
575 (set-syntax-table c-with-syntax-table-orig-table))))
576 (put 'c-with-syntax-table 'lisp-indent-function 1)
577
578 (defmacro c-skip-ws-forward (&optional limit)
579 "Skip over any whitespace following point.
580 This function skips over horizontal and vertical whitespace and line
581 continuations.
582
583 This function does not do any hidden buffer changes."
584 (if limit
585 `(let ((limit (or ,limit (point-max))))
586 (while (progn
587 ;; skip-syntax-* doesn't count \n as whitespace..
588 (skip-chars-forward " \t\n\r\f\v" limit)
589 (when (and (eq (char-after) ?\\)
590 (< (point) limit))
591 (forward-char)
592 (or (eolp)
593 (progn (backward-char) nil))))))
594 '(while (progn
595 (skip-chars-forward " \t\n\r\f\v")
596 (when (eq (char-after) ?\\)
597 (forward-char)
598 (or (eolp)
599 (progn (backward-char) nil)))))))
600
601 (defmacro c-skip-ws-backward (&optional limit)
602 "Skip over any whitespace preceding point.
603 This function skips over horizontal and vertical whitespace and line
604 continuations.
605
606 This function does not do any hidden buffer changes."
607 (if limit
608 `(let ((limit (or ,limit (point-min))))
609 (while (progn
610 ;; skip-syntax-* doesn't count \n as whitespace..
611 (skip-chars-backward " \t\n\r\f\v" limit)
612 (and (eolp)
613 (eq (char-before) ?\\)
614 (> (point) limit)))
615 (backward-char)))
616 '(while (progn
617 (skip-chars-backward " \t\n\r\f\v")
618 (and (eolp)
619 (eq (char-before) ?\\)))
620 (backward-char))))
621
622 (eval-and-compile
623 (defvar c-langs-are-parametric nil))
624
625 (defmacro c-major-mode-is (mode)
626 "Return non-nil if the current CC Mode major mode is MODE.
627 MODE is either a mode symbol or a list of mode symbols.
628
629 This function does not do any hidden buffer changes."
630
631 (if c-langs-are-parametric
632 ;; Inside a `c-lang-defconst'.
633 `(c-lang-major-mode-is ,mode)
634
635 (if (eq (car-safe mode) 'quote)
636 (let ((mode (eval mode)))
637 (if (listp mode)
638 `(memq c-buffer-is-cc-mode ',mode)
639 `(eq c-buffer-is-cc-mode ',mode)))
640
641 `(let ((mode ,mode))
642 (if (listp mode)
643 (memq c-buffer-is-cc-mode mode)
644 (eq c-buffer-is-cc-mode mode))))))
645
646 (defmacro c-mode-is-new-awk-p ()
647 ;; Is the current mode the "new" awk mode? It is important for
648 ;; (e.g.) the cc-engine functions do distinguish between the old and
649 ;; new awk-modes.
650 '(and (c-major-mode-is 'awk-mode)
651 (memq 'syntax-properties c-emacs-features)))
652
653 (defmacro c-parse-sexp-lookup-properties ()
654 ;; Return the value of the variable that says whether the
655 ;; syntax-table property affects the sexp routines. Always return
656 ;; nil in (X)Emacsen without support for that.
657 ;;
658 ;; This function does not do any hidden buffer changes.
659 (cond ((cc-bytecomp-boundp 'parse-sexp-lookup-properties)
660 `parse-sexp-lookup-properties)
661 ((cc-bytecomp-boundp 'lookup-syntax-properties)
662 `lookup-syntax-properties)
663 (t nil)))
664
665 \f
666 ;; Macros/functions to handle so-called "char properties", which are
667 ;; properties set on a single character and that never spreads to any
668 ;; other characters.
669
670 (eval-and-compile
671 ;; Constant used at compile time to decide whether or not to use
672 ;; XEmacs extents. Check all the extent functions we'll use since
673 ;; some packages might add compatibility aliases for some of them in
674 ;; Emacs.
675 (defconst c-use-extents (and (cc-bytecomp-fboundp 'extent-at)
676 (cc-bytecomp-fboundp 'set-extent-property)
677 (cc-bytecomp-fboundp 'set-extent-properties)
678 (cc-bytecomp-fboundp 'make-extent)
679 (cc-bytecomp-fboundp 'extent-property)
680 (cc-bytecomp-fboundp 'delete-extent)
681 (cc-bytecomp-fboundp 'map-extents))))
682
683 ;; `c-put-char-property' is complex enough in XEmacs and Emacs < 21 to
684 ;; make it a function.
685 (defalias 'c-put-char-property-fun
686 (cc-eval-when-compile
687 (cond (c-use-extents
688 ;; XEmacs.
689 (byte-compile
690 (lambda (pos property value)
691 (let ((ext (extent-at pos nil property)))
692 (if ext
693 (set-extent-property ext property value)
694 (set-extent-properties (make-extent pos (1+ pos))
695 (cons property
696 (cons value
697 '(start-open t
698 end-open t)))))))))
699
700 ((not (cc-bytecomp-boundp 'text-property-default-nonsticky))
701 ;; In Emacs < 21 we have to mess with the `rear-nonsticky' property.
702 (byte-compile
703 (lambda (pos property value)
704 (put-text-property pos (1+ pos) property value)
705 (let ((prop (get-text-property pos 'rear-nonsticky)))
706 (or (memq property prop)
707 (put-text-property pos (1+ pos)
708 'rear-nonsticky
709 (cons property prop))))))))))
710 (cc-bytecomp-defun c-put-char-property-fun) ; Make it known below.
711
712 (defmacro c-put-char-property (pos property value)
713 ;; Put the given property with the given value on the character at
714 ;; POS and make it front and rear nonsticky, or start and end open
715 ;; in XEmacs vocabulary. If the character already has the given
716 ;; property then the value is replaced, and the behavior is
717 ;; undefined if that property has been put by some other function.
718 ;; PROPERTY is assumed to be constant.
719 ;;
720 ;; If there's a `text-property-default-nonsticky' variable (Emacs
721 ;; 21) then it's assumed that the property is present on it.
722 (setq property (eval property))
723 (if (or c-use-extents
724 (not (cc-bytecomp-boundp 'text-property-default-nonsticky)))
725 ;; XEmacs and Emacs < 21.
726 `(c-put-char-property-fun ,pos ',property ,value)
727 ;; In Emacs 21 we got the `rear-nonsticky' property covered
728 ;; by `text-property-default-nonsticky'.
729 `(let ((-pos- ,pos))
730 (put-text-property -pos- (1+ -pos-) ',property ,value))))
731
732 (defmacro c-get-char-property (pos property)
733 ;; Get the value of the given property on the character at POS if
734 ;; it's been put there by `c-put-char-property'. PROPERTY is
735 ;; assumed to be constant.
736 (setq property (eval property))
737 (if c-use-extents
738 ;; XEmacs.
739 `(let ((ext (extent-at ,pos nil ',property)))
740 (if ext (extent-property ext ',property)))
741 ;; Emacs.
742 `(get-text-property ,pos ',property)))
743
744 ;; `c-clear-char-property' is complex enough in Emacs < 21 to make it
745 ;; a function, since we have to mess with the `rear-nonsticky' property.
746 (defalias 'c-clear-char-property-fun
747 (cc-eval-when-compile
748 (unless (or c-use-extents
749 (cc-bytecomp-boundp 'text-property-default-nonsticky))
750 (byte-compile
751 (lambda (pos property)
752 (when (get-text-property pos property)
753 (remove-text-properties pos (1+ pos) (list property nil))
754 (put-text-property pos (1+ pos)
755 'rear-nonsticky
756 (delq property (get-text-property
757 pos 'rear-nonsticky)))))))))
758 (cc-bytecomp-defun c-clear-char-property-fun) ; Make it known below.
759
760 (defmacro c-clear-char-property (pos property)
761 ;; Remove the given property on the character at POS if it's been put
762 ;; there by `c-put-char-property'. PROPERTY is assumed to be
763 ;; constant.
764 (setq property (eval property))
765 (cond (c-use-extents
766 ;; XEmacs.
767 `(let ((ext (extent-at ,pos nil ',property)))
768 (if ext (delete-extent ext))))
769 ((cc-bytecomp-boundp 'text-property-default-nonsticky)
770 ;; In Emacs 21 we got the `rear-nonsticky' property covered
771 ;; by `text-property-default-nonsticky'.
772 `(let ((pos ,pos))
773 (remove-text-properties pos (1+ pos)
774 '(,property nil))))
775 (t
776 ;; Emacs < 21.
777 `(c-clear-char-property-fun ,pos ',property))))
778
779 (defmacro c-clear-char-properties (from to property)
780 ;; Remove all the occurences of the given property in the given
781 ;; region that has been put with `c-put-char-property'. PROPERTY is
782 ;; assumed to be constant.
783 ;;
784 ;; Note that this function does not clean up the property from the
785 ;; lists of the `rear-nonsticky' properties in the region, if such
786 ;; are used. Thus it should not be used for common properties like
787 ;; `syntax-table'.
788 (setq property (eval property))
789 (if c-use-extents
790 ;; XEmacs.
791 `(map-extents (lambda (ext ignored)
792 (delete-extent ext))
793 nil ,from ,to nil nil ',property)
794 ;; Emacs.
795 `(remove-text-properties ,from ,to '(,property nil))))
796
797 \f
798 ;; Make edebug understand the macros.
799 (eval-after-load "edebug"
800 '(progn
801 (def-edebug-spec c-point t)
802 (def-edebug-spec c-safe t)
803 (def-edebug-spec c-save-buffer-state let*)
804 (def-edebug-spec c-forward-syntactic-ws t)
805 (def-edebug-spec c-backward-syntactic-ws t)
806 (def-edebug-spec c-forward-sexp t)
807 (def-edebug-spec c-backward-sexp t)
808 (def-edebug-spec c-up-list-forward t)
809 (def-edebug-spec c-up-list-backward t)
810 (def-edebug-spec c-down-list-forward t)
811 (def-edebug-spec c-down-list-backward t)
812 (def-edebug-spec c-add-syntax t)
813 (def-edebug-spec c-add-class-syntax t)
814 (def-edebug-spec c-benign-error t)
815 (def-edebug-spec c-with-syntax-table t)
816 (def-edebug-spec c-skip-ws-forward t)
817 (def-edebug-spec c-skip-ws-backward t)
818 (def-edebug-spec c-major-mode-is t)
819 (def-edebug-spec c-put-char-property t)
820 (def-edebug-spec c-get-char-property t)
821 (def-edebug-spec c-clear-char-property t)
822 (def-edebug-spec c-clear-char-properties t)
823 (def-edebug-spec cc-eval-when-compile t)))
824
825 \f
826 ;;; Functions.
827
828 ;; Note: All these after the macros, to be on safe side in avoiding
829 ;; bugs where macros are defined too late. These bugs often only show
830 ;; when the files are compiled in a certain order within the same
831 ;; session.
832
833 (defsubst c-end-of-defun-1 ()
834 ;; Replacement for end-of-defun that use c-beginning-of-defun-1.
835 (let ((start (point)))
836 ;; Skip forward into the next defun block. Don't bother to avoid
837 ;; comments, literals etc, since beginning-of-defun doesn't do that
838 ;; anyway.
839 (skip-chars-forward "^}")
840 (c-beginning-of-defun-1)
841 (if (eq (char-after) ?{)
842 (c-forward-sexp))
843 (if (< (point) start)
844 (goto-char (point-max)))))
845
846 (defconst c-<-as-paren-syntax '(4 . ?>))
847
848 (defsubst c-mark-<-as-paren (pos)
849 ;; Mark the "<" character at POS as an sexp list opener using the
850 ;; syntax-table property. Note that Emacs 19 and XEmacs <= 20
851 ;; doesn't support syntax properties, so this function might not
852 ;; have any effect.
853 (c-put-char-property pos 'syntax-table c-<-as-paren-syntax))
854
855 (defconst c->-as-paren-syntax '(5 . ?<))
856
857 (defsubst c-mark->-as-paren (pos)
858 ;; Mark the ">" character at POS as an sexp list closer using the
859 ;; syntax-table property. Note that Emacs 19 and XEmacs <= 20
860 ;; doesn't support syntax properties, so this function might not
861 ;; have any effect.
862 (c-put-char-property pos 'syntax-table c->-as-paren-syntax))
863
864 (defsubst c-intersect-lists (list alist)
865 ;; return the element of ALIST that matches the first element found
866 ;; in LIST. Uses assq.
867 ;;
868 ;; This function does not do any hidden buffer changes.
869 (let (match)
870 (while (and list
871 (not (setq match (assq (car list) alist))))
872 (setq list (cdr list)))
873 match))
874
875 (defsubst c-lookup-lists (list alist1 alist2)
876 ;; first, find the first entry from LIST that is present in ALIST1,
877 ;; then find the entry in ALIST2 for that entry.
878 ;;
879 ;; This function does not do any hidden buffer changes.
880 (assq (car (c-intersect-lists list alist1)) alist2))
881
882 (defsubst c-langelem-sym (langelem)
883 "Return the syntactic symbol in LANGELEM.
884
885 LANGELEM is a syntactic element, i.e. either a cons cell on the
886 \"old\" form given as the first argument to lineup functions or a list
887 on the \"new\" form as used in `c-syntactic-element'.
888
889 This function does not do any hidden buffer changes."
890 (car langelem))
891
892 (defsubst c-langelem-pos (langelem)
893 "Return the (primary) anchor position in LANGELEM, or nil if there is none.
894
895 LANGELEM is a syntactic element, i.e. either a cons cell on the
896 \"old\" form given as the first argument to lineup functions or a list
897 on the \"new\" form as used in `c-syntactic-element'.
898
899 This function does not do any hidden buffer changes."
900 (if (consp (cdr langelem))
901 (car-safe (cdr langelem))
902 (cdr langelem)))
903
904 (defun c-langelem-col (langelem &optional preserve-point)
905 "Return the column of the (primary) anchor position in LANGELEM.
906 Leave point at that position unless PRESERVE-POINT is non-nil.
907
908 LANGELEM is a syntactic element, i.e. either a cons cell on the
909 \"old\" form given as the first argument to lineup functions or a list
910 on the \"new\" form as used in `c-syntactic-element'.
911
912 This function does not do any hidden buffer changes."
913 (let ((pos (c-langelem-pos langelem))
914 (here (point)))
915 (if pos
916 (progn
917 (goto-char pos)
918 (prog1 (current-column)
919 (if preserve-point
920 (goto-char here))))
921 0)))
922
923 (defsubst c-langelem-2nd-pos (langelem)
924 "Return the secondary position in LANGELEM, or nil if there is none.
925
926 LANGELEM is a syntactic element, typically on the \"new\" form as used
927 in `c-syntactic-element'. It may be on the \"old\" form that is used
928 as the first argument to lineup functions, but then the returned value
929 always will be nil.
930
931 This function does not do any hidden buffer changes."
932 (car-safe (cdr-safe (cdr-safe langelem))))
933
934 (defsubst c-keep-region-active ()
935 ;; Do whatever is necessary to keep the region active in XEmacs.
936 ;; This is not needed for Emacs.
937 ;;
938 ;; This function does not do any hidden buffer changes.
939 (and (boundp 'zmacs-region-stays)
940 (setq zmacs-region-stays t)))
941
942 (defsubst c-region-is-active-p ()
943 ;; Return t when the region is active. The determination of region
944 ;; activeness is different in both Emacs and XEmacs.
945 ;;
946 ;; This function does not do any hidden buffer changes.
947 (cond
948 ;; XEmacs
949 ((and (fboundp 'region-active-p)
950 (boundp 'zmacs-regions)
951 zmacs-regions)
952 (region-active-p))
953 ;; Emacs
954 ((boundp 'mark-active) mark-active)
955 ;; fallback; shouldn't get here
956 (t (mark t))))
957
958 (put 'c-mode 'c-mode-prefix "c-")
959 (put 'c++-mode 'c-mode-prefix "c++-")
960 (put 'objc-mode 'c-mode-prefix "objc-")
961 (put 'java-mode 'c-mode-prefix "java-")
962 (put 'idl-mode 'c-mode-prefix "idl-")
963 (put 'pike-mode 'c-mode-prefix "pike-")
964 (put 'awk-mode 'c-mode-prefix "awk-")
965
966 (defsubst c-mode-symbol (suffix)
967 "Prefix the current mode prefix (e.g. \"c-\") to SUFFIX and return
968 the corresponding symbol.
969
970 This function does not do any hidden buffer changes."
971 (or c-buffer-is-cc-mode
972 (error "Not inside a CC Mode based mode"))
973 (let ((mode-prefix (get c-buffer-is-cc-mode 'c-mode-prefix)))
974 (or mode-prefix
975 (error "%S has no mode prefix known to `c-mode-symbol'"
976 c-buffer-is-cc-mode))
977 (intern (concat mode-prefix suffix))))
978
979 (defsubst c-mode-var (suffix)
980 "Prefix the current mode prefix (e.g. \"c-\") to SUFFIX and return
981 the value of the variable with that name.
982
983 This function does not do any hidden buffer changes."
984 (symbol-value (c-mode-symbol suffix)))
985
986 (defsubst c-got-face-at (pos faces)
987 "Return non-nil if position POS in the current buffer has any of the
988 faces in the list FACES.
989
990 This function does not do any hidden buffer changes."
991 (let ((pos-faces (get-text-property pos 'face)))
992 (if (consp pos-faces)
993 (progn
994 (while (and pos-faces
995 (not (memq (car pos-faces) faces)))
996 (setq pos-faces (cdr pos-faces)))
997 pos-faces)
998 (memq pos-faces faces))))
999
1000 (defsubst c-face-name-p (facename)
1001 ;; Return t if FACENAME is the name of a face. This method is
1002 ;; necessary since facep in XEmacs only returns t for the actual
1003 ;; face objects (while it's only their names that are used just
1004 ;; about anywhere else) without providing a predicate that tests
1005 ;; face names.
1006 ;;
1007 ;; This function does not do any hidden buffer changes.
1008 (memq facename (face-list)))
1009
1010 (defun c-make-keywords-re (adorn list &optional mode)
1011 "Make a regexp that matches all the strings the list.
1012 Duplicates in the list are removed. The resulting regexp may contain
1013 zero or more submatch expressions.
1014
1015 If ADORN is non-nil there will be at least one submatch and the first
1016 matches the whole keyword, and the regexp will also not match a prefix
1017 of any identifier. Adorned regexps cannot be appended. The language
1018 variable `c-nonsymbol-key' is used to make the adornment. The
1019 optional MODE specifies the language to get it in. The default is the
1020 current language (taken from `c-buffer-is-cc-mode')."
1021
1022 (let (unique)
1023 (dolist (elt list)
1024 (unless (member elt unique)
1025 (push elt unique)))
1026 (setq list unique))
1027 (if list
1028 (let ((re (c-regexp-opt list)))
1029
1030 ;; Emacs < 21 and XEmacs (all versions so far) has a buggy
1031 ;; regexp-opt that doesn't always cope with strings containing
1032 ;; newlines. This kludge doesn't handle shy parens correctly
1033 ;; so we can't advice regexp-opt directly with it.
1034 (let (fail-list)
1035 (while list
1036 (and (string-match "\n" (car list)) ; To speed it up a little.
1037 (not (string-match (concat "\\`\\(" re "\\)\\'")
1038 (car list)))
1039 (setq fail-list (cons (car list) fail-list)))
1040 (setq list (cdr list)))
1041 (when fail-list
1042 (setq re (concat re
1043 "\\|"
1044 (mapconcat 'regexp-quote
1045 (sort fail-list
1046 (lambda (a b)
1047 (> (length a) (length b))))
1048 "\\|")))))
1049
1050 ;; Add our own grouping parenthesis around re instead of
1051 ;; passing adorn to `regexp-opt', since in XEmacs it makes the
1052 ;; top level grouping "shy".
1053 (if adorn
1054 (concat "\\(" re "\\)"
1055 "\\("
1056 (c-get-lang-constant 'c-nonsymbol-key nil mode)
1057 "\\|$\\)")
1058 re))
1059
1060 ;; Produce a regexp that matches nothing.
1061 (if adorn
1062 "\\(\\<\\>\\)"
1063 "\\<\\>")))
1064
1065 (put 'c-make-keywords-re 'lisp-indent-function 1)
1066
1067 \f
1068 ;; Figure out what features this Emacs has
1069
1070 (cc-bytecomp-defvar open-paren-in-column-0-is-defun-start)
1071
1072 (defconst c-emacs-features
1073 (let (list)
1074
1075 (if (boundp 'infodock-version)
1076 ;; I've no idea what this actually is, but it's legacy. /mast
1077 (setq list (cons 'infodock list)))
1078
1079 ;; XEmacs 19 and beyond use 8-bit modify-syntax-entry flags.
1080 ;; Emacs 19 uses a 1-bit flag. We will have to set up our
1081 ;; syntax tables differently to handle this.
1082 (let ((table (copy-syntax-table))
1083 entry)
1084 (modify-syntax-entry ?a ". 12345678" table)
1085 (cond
1086 ;; XEmacs 19, and beyond Emacs 19.34
1087 ((arrayp table)
1088 (setq entry (aref table ?a))
1089 ;; In Emacs, table entries are cons cells
1090 (if (consp entry) (setq entry (car entry))))
1091 ;; XEmacs 20
1092 ((fboundp 'get-char-table) (setq entry (get-char-table ?a table)))
1093 ;; before and including Emacs 19.34
1094 ((and (fboundp 'char-table-p)
1095 (char-table-p table))
1096 (setq entry (car (char-table-range table [?a]))))
1097 ;; incompatible
1098 (t (error "CC Mode is incompatible with this version of Emacs")))
1099 (setq list (cons (if (= (logand (lsh entry -16) 255) 255)
1100 '8-bit
1101 '1-bit)
1102 list)))
1103
1104 (let ((buf (generate-new-buffer " test"))
1105 parse-sexp-lookup-properties
1106 parse-sexp-ignore-comments
1107 lookup-syntax-properties)
1108 (save-excursion
1109 (set-buffer buf)
1110 (set-syntax-table (make-syntax-table))
1111
1112 ;; For some reason we have to set some of these after the
1113 ;; buffer has been made current. (Specifically,
1114 ;; `parse-sexp-ignore-comments' in Emacs 21.)
1115 (setq parse-sexp-lookup-properties t
1116 parse-sexp-ignore-comments t
1117 lookup-syntax-properties t)
1118
1119 ;; Find out if the `syntax-table' text property works.
1120 (modify-syntax-entry ?< ".")
1121 (modify-syntax-entry ?> ".")
1122 (insert "<()>")
1123 (c-mark-<-as-paren 1)
1124 (c-mark->-as-paren 4)
1125 (goto-char 1)
1126 (c-forward-sexp)
1127 (if (= (point) 5)
1128 (setq list (cons 'syntax-properties list)))
1129
1130 ;; Find out if generic comment delimiters work.
1131 (c-safe
1132 (modify-syntax-entry ?x "!")
1133 (if (string-match "\\s!" "x")
1134 (setq list (cons 'gen-comment-delim list))))
1135
1136 ;; Find out if generic string delimiters work.
1137 (c-safe
1138 (modify-syntax-entry ?x "|")
1139 (if (string-match "\\s|" "x")
1140 (setq list (cons 'gen-string-delim list))))
1141
1142 ;; See if POSIX char classes work.
1143 (when (and (string-match "[[:alpha:]]" "a")
1144 ;; All versions of Emacs 21 so far haven't fixed
1145 ;; char classes in `skip-chars-forward' and
1146 ;; `skip-chars-backward'.
1147 (progn
1148 (delete-region (point-min) (point-max))
1149 (insert "foo123")
1150 (skip-chars-backward "[:alnum:]")
1151 (bobp))
1152 (= (skip-chars-forward "[:alpha:]") 3))
1153 (setq list (cons 'posix-char-classes list)))
1154
1155 ;; See if `open-paren-in-column-0-is-defun-start' exists and
1156 ;; isn't buggy.
1157 (when (boundp 'open-paren-in-column-0-is-defun-start)
1158 (let ((open-paren-in-column-0-is-defun-start nil)
1159 (parse-sexp-ignore-comments t))
1160 (delete-region (point-min) (point-max))
1161 (set-syntax-table (make-syntax-table))
1162 (modify-syntax-entry ?\' "\"")
1163 (cond
1164 ;; XEmacs. Afaik this is currently an Emacs-only
1165 ;; feature, but it's good to be prepared.
1166 ((memq '8-bit list)
1167 (modify-syntax-entry ?/ ". 1456")
1168 (modify-syntax-entry ?* ". 23"))
1169 ;; Emacs
1170 ((memq '1-bit list)
1171 (modify-syntax-entry ?/ ". 124b")
1172 (modify-syntax-entry ?* ". 23")))
1173 (modify-syntax-entry ?\n "> b")
1174 (insert "/* '\n () */")
1175 (backward-sexp)
1176 (if (bobp)
1177 (setq list (cons 'col-0-paren list)))))
1178
1179 (set-buffer-modified-p nil))
1180 (kill-buffer buf))
1181
1182 ;; See if `parse-partial-sexp' returns the eighth element.
1183 (when (c-safe (>= (length (save-excursion (parse-partial-sexp 1 1))) 10))
1184 (setq list (cons 'pps-extended-state list)))
1185
1186 ;;(message "c-emacs-features: %S" list)
1187 list)
1188 "A list of certain features in the (X)Emacs you are using.
1189 There are many flavors of Emacs out there, each with different
1190 features supporting those needed by CC Mode. The following values
1191 might be present:
1192
1193 '8-bit 8 bit syntax entry flags (XEmacs style).
1194 '1-bit 1 bit syntax entry flags (Emacs style).
1195 'syntax-properties It works to override the syntax for specific characters
1196 in the buffer with the 'syntax-table property.
1197 'gen-comment-delim Generic comment delimiters work
1198 (i.e. the syntax class `!').
1199 'gen-string-delim Generic string delimiters work
1200 (i.e. the syntax class `|').
1201 'pps-extended-state `parse-partial-sexp' returns a list with at least 10
1202 elements, i.e. it contains the position of the
1203 start of the last comment or string.
1204 'posix-char-classes The regexp engine understands POSIX character classes.
1205 'col-0-paren It's possible to turn off the ad-hoc rule that a paren
1206 in column zero is the start of a defun.
1207 'infodock This is Infodock (based on XEmacs).
1208
1209 '8-bit and '1-bit are mutually exclusive.")
1210
1211 \f
1212 ;;; Some helper constants.
1213
1214 ;; If the regexp engine supports POSIX char classes then we can use
1215 ;; them to handle extended charsets correctly.
1216 (if (memq 'posix-char-classes c-emacs-features)
1217 (progn
1218 (defconst c-alpha "[:alpha:]")
1219 (defconst c-alnum "[:alnum:]")
1220 (defconst c-digit "[:digit:]")
1221 (defconst c-upper "[:upper:]")
1222 (defconst c-lower "[:lower:]"))
1223 (defconst c-alpha "a-zA-Z")
1224 (defconst c-alnum "a-zA-Z0-9")
1225 (defconst c-digit "0-9")
1226 (defconst c-upper "A-Z")
1227 (defconst c-lower "a-z"))
1228
1229 \f
1230 ;;; System for handling language dependent constants.
1231
1232 ;; This is used to set various language dependent data in a flexible
1233 ;; way: Language constants can be built from the values of other
1234 ;; language constants, also those for other languages. They can also
1235 ;; process the values of other language constants uniformly across all
1236 ;; the languages. E.g. one language constant can list all the type
1237 ;; keywords in each language, and another can build a regexp for each
1238 ;; language from those lists without code duplication.
1239 ;;
1240 ;; Language constants are defined with `c-lang-defconst', and their
1241 ;; value forms (referred to as source definitions) are evaluated only
1242 ;; on demand when requested for a particular language with
1243 ;; `c-lang-const'. It's therefore possible to refer to the values of
1244 ;; constants defined later in the file, or in another file, just as
1245 ;; long as all the relevant `c-lang-defconst' have been loaded when
1246 ;; `c-lang-const' is actually evaluated from somewhere else.
1247 ;;
1248 ;; `c-lang-const' forms are also evaluated at compile time and
1249 ;; replaced with the values they produce. Thus there's no overhead
1250 ;; for this system when compiled code is used - only the values
1251 ;; actually used in the code are present, and the file(s) containing
1252 ;; the `c-lang-defconst' forms don't need to be loaded at all then.
1253 ;; There are however safeguards to make sure that they can be loaded
1254 ;; to get the source definitions for the values if there's a mismatch
1255 ;; in compiled versions, or if `c-lang-const' is used uncompiled.
1256 ;;
1257 ;; Note that the source definitions in a `c-lang-defconst' form are
1258 ;; compiled into the .elc file where it stands; there's no need to
1259 ;; load the source file to get it.
1260 ;;
1261 ;; See cc-langs.el for more details about how this system is deployed
1262 ;; in CC Mode, and how the associated language variable system
1263 ;; (`c-lang-defvar') works. That file also contains a lot of
1264 ;; examples.
1265
1266 (defun c-add-language (mode base-mode)
1267 "Declare a new language in the language dependent variable system.
1268 This is intended to be used by modes that inherit CC Mode to add new
1269 languages. It should be used at the top level before any calls to
1270 `c-lang-defconst'. MODE is the mode name symbol for the new language,
1271 and BASE-MODE is the mode name symbol for the language in CC Mode that
1272 is to be the template for the new mode.
1273
1274 The exact effect of BASE-MODE is to make all language constants that
1275 haven't got a setting in the new language fall back to their values in
1276 BASE-MODE. It does not have any effect outside the language constant
1277 system."
1278 (unless (string-match "\\`\\(.*-\\)mode\\'" (symbol-name mode))
1279 (error "The mode name symbol `%s' must end with \"-mode\"" mode))
1280 (put mode 'c-mode-prefix (match-string 1 (symbol-name mode)))
1281 (unless (get base-mode 'c-mode-prefix)
1282 (error "Unknown base mode `%s'" base-mode))
1283 (put mode 'c-fallback-mode base-mode))
1284
1285 (defvar c-lang-constants (make-vector 151 0))
1286 ;; This obarray is a cache to keep track of the language constants
1287 ;; defined by `c-lang-defconst' and the evaluated values returned by
1288 ;; `c-lang-const'. It's mostly used at compile time but it's not
1289 ;; stored in compiled files.
1290 ;;
1291 ;; The obarray contains all the language constants as symbols. The
1292 ;; value cells hold the evaluated values as alists where each car is
1293 ;; the mode name symbol and the corresponding cdr is the evaluated
1294 ;; value in that mode. The property lists hold the source definitions
1295 ;; and other miscellaneous data. The obarray might also contain
1296 ;; various other symbols, but those don't have any variable bindings.
1297
1298 (defvar c-lang-const-expansion nil)
1299
1300 (defsubst c-get-current-file ()
1301 ;; Return the base name of the current file.
1302 (let ((file (cond
1303 (load-in-progress
1304 ;; Being loaded.
1305 load-file-name)
1306 ((and (boundp 'byte-compile-dest-file)
1307 (stringp byte-compile-dest-file))
1308 ;; Being compiled.
1309 byte-compile-dest-file)
1310 (t
1311 ;; Being evaluated interactively.
1312 (buffer-file-name)))))
1313 (and file
1314 (file-name-sans-extension
1315 (file-name-nondirectory file)))))
1316
1317 (defmacro c-lang-defconst-eval-immediately (form)
1318 "Can be used inside a VAL in `c-lang-defconst' to evaluate FORM
1319 immediately, i.e. at the same time as the `c-lang-defconst' form
1320 itself is evaluated."
1321 ;; Evaluate at macro expansion time, i.e. in the
1322 ;; `cl-macroexpand-all' inside `c-lang-defconst'.
1323 (eval form))
1324
1325 (defmacro c-lang-defconst (name &rest args)
1326 "Set the language specific values of the language constant NAME.
1327 The second argument can be an optional docstring. The rest of the
1328 arguments are one or more repetitions of LANG VAL where LANG specifies
1329 the language(s) that VAL applies to. LANG is the name of the
1330 language, i.e. the mode name without the \"-mode\" suffix, or a list
1331 of such language names, or `t' for all languages. VAL is a form to
1332 evaluate to get the value.
1333
1334 If LANG isn't `t' or one of the core languages in CC Mode, it must
1335 have been declared with `c-add-language'.
1336
1337 Neither NAME, LANG nor VAL are evaluated directly - they should not be
1338 quoted. `c-lang-defconst-eval-immediately' can however be used inside
1339 VAL to evaluate parts of it directly.
1340
1341 When VAL is evaluated for some language, that language is temporarily
1342 made current so that `c-lang-const' without an explicit language can
1343 be used inside VAL to refer to the value of a language constant in the
1344 same language. That is particularly useful if LANG is `t'.
1345
1346 VAL is not evaluated right away but rather when the value is requested
1347 with `c-lang-const'. Thus it's possible to use `c-lang-const' inside
1348 VAL to refer to language constants that haven't been defined yet.
1349 However, if the definition of a language constant is in another file
1350 then that file must be loaded \(at compile time) before it's safe to
1351 reference the constant.
1352
1353 The assignments in ARGS are processed in sequence like `setq', so
1354 \(c-lang-const NAME) may be used inside a VAL to refer to the last
1355 assigned value to this language constant, or a value that it has
1356 gotten in another earlier loaded file.
1357
1358 To work well with repeated loads and interactive reevaluation, only
1359 one `c-lang-defconst' for each NAME is permitted per file. If there
1360 already is one it will be completely replaced; the value in the
1361 earlier definition will not affect `c-lang-const' on the same
1362 constant. A file is identified by its base name.
1363
1364 This macro does not do any hidden buffer changes."
1365
1366 (let* ((sym (intern (symbol-name name) c-lang-constants))
1367 ;; Make `c-lang-const' expand to a straightforward call to
1368 ;; `c-get-lang-constant' in `cl-macroexpand-all' below.
1369 ;;
1370 ;; (The default behavior, i.e. to expand to a call inside
1371 ;; `eval-when-compile' should be equivalent, since that macro
1372 ;; should only expand to its content if it's used inside a
1373 ;; form that's already evaluated at compile time. It's
1374 ;; however necessary to use our cover macro
1375 ;; `cc-eval-when-compile' due to bugs in `eval-when-compile',
1376 ;; and it expands to a bulkier form that in this case only is
1377 ;; unnecessary garbage that we don't want to store in the
1378 ;; language constant source definitions.)
1379 (c-lang-const-expansion 'call)
1380 (c-langs-are-parametric t)
1381 bindings
1382 pre-files)
1383
1384 (or (symbolp name)
1385 (error "Not a symbol: %s" name))
1386
1387 (when (stringp (car-safe args))
1388 ;; The docstring is hardly used anywhere since there's no normal
1389 ;; symbol to attach it to. It's primarily for getting the right
1390 ;; format in the source.
1391 (put sym 'variable-documentation (car args))
1392 (setq args (cdr args)))
1393
1394 (or args
1395 (error "No assignments in `c-lang-defconst' for %s" name))
1396
1397 ;; Rework ARGS to an association list to make it easier to handle.
1398 ;; It's reversed at the same time to make it easier to implement
1399 ;; the demand-driven (i.e. reversed) evaluation in `c-lang-const'.
1400 (while args
1401 (let ((assigned-mode
1402 (cond ((eq (car args) t) t)
1403 ((symbolp (car args))
1404 (list (intern (concat (symbol-name (car args))
1405 "-mode"))))
1406 ((listp (car args))
1407 (mapcar (lambda (lang)
1408 (or (symbolp lang)
1409 (error "Not a list of symbols: %s"
1410 (car args)))
1411 (intern (concat (symbol-name lang)
1412 "-mode")))
1413 (car args)))
1414 (t (error "Not a symbol or a list of symbols: %s"
1415 (car args)))))
1416 val)
1417
1418 (or (cdr args)
1419 (error "No value for %s" (car args)))
1420 (setq args (cdr args)
1421 val (car args))
1422
1423 ;; Emacs has a weird bug where it seems to fail to read
1424 ;; backquote lists from byte compiled files correctly (,@
1425 ;; forms, to be specific), so make sure the bindings in the
1426 ;; expansion below doesn't contain any backquote stuff.
1427 ;; (XEmacs handles it correctly and doesn't need this for that
1428 ;; reason, but we also use this expansion handle
1429 ;; `c-lang-defconst-eval-immediately' and to register
1430 ;; dependencies on the `c-lang-const's in VAL.)
1431 (setq val (cl-macroexpand-all val))
1432
1433 (setq bindings (cons (cons assigned-mode val) bindings)
1434 args (cdr args))))
1435
1436 ;; Compile in the other files that have provided source
1437 ;; definitions for this symbol, to make sure the order in the
1438 ;; `source' property is correct even when files are loaded out of
1439 ;; order.
1440 (setq pre-files (nreverse
1441 ;; Reverse to get the right load order.
1442 (mapcar 'car (get sym 'source))))
1443
1444 `(eval-and-compile
1445 (c-define-lang-constant ',name ',bindings
1446 ,@(and pre-files `(',pre-files))))))
1447
1448 (put 'c-lang-defconst 'lisp-indent-function 1)
1449 (eval-after-load "edebug"
1450 '(def-edebug-spec c-lang-defconst
1451 (&define name [&optional stringp] [&rest sexp def-form])))
1452
1453 (defun c-define-lang-constant (name bindings &optional pre-files)
1454 ;; Used by `c-lang-defconst'. This function does not do any hidden
1455 ;; buffer changes.
1456
1457 (let* ((sym (intern (symbol-name name) c-lang-constants))
1458 (source (get sym 'source))
1459 (file (intern
1460 (or (c-get-current-file)
1461 (error "`c-lang-defconst' must be used in a file"))))
1462 (elem (assq file source)))
1463
1464 ;;(when (cdr-safe elem)
1465 ;; (message "Language constant %s redefined in %S" name file))
1466
1467 ;; Note that the order in the source alist is relevant. Like how
1468 ;; `c-lang-defconst' reverses the bindings, this reverses the
1469 ;; order between files so that the last to evaluate comes first.
1470 (unless elem
1471 (while pre-files
1472 (unless (assq (car pre-files) source)
1473 (setq source (cons (list (car pre-files)) source)))
1474 (setq pre-files (cdr pre-files)))
1475 (put sym 'source (cons (setq elem (list file)) source)))
1476
1477 (setcdr elem bindings)
1478
1479 ;; Bind the symbol as a variable, or clear any earlier evaluated
1480 ;; value it has.
1481 (set sym nil)
1482
1483 ;; Clear the evaluated values that depend on this source.
1484 (let ((agenda (get sym 'dependents))
1485 (visited (make-vector 101 0))
1486 ptr)
1487 (while agenda
1488 (setq sym (car agenda)
1489 agenda (cdr agenda))
1490 (intern (symbol-name sym) visited)
1491 (set sym nil)
1492 (setq ptr (get sym 'dependents))
1493 (while ptr
1494 (setq sym (car ptr)
1495 ptr (cdr ptr))
1496 (unless (intern-soft (symbol-name sym) visited)
1497 (setq agenda (cons sym agenda))))))
1498
1499 name))
1500
1501 (defmacro c-lang-const (name &optional lang)
1502 "Get the mode specific value of the language constant NAME in language LANG.
1503 LANG is the name of the language, i.e. the mode name without the
1504 \"-mode\" suffix. If used inside `c-lang-defconst' or
1505 `c-lang-defvar', LANG may be left out to refer to the current
1506 language. NAME and LANG are not evaluated so they should not be
1507 quoted.
1508
1509 This macro does not do any hidden buffer changes."
1510
1511 (or (symbolp name)
1512 (error "Not a symbol: %s" name))
1513 (or (symbolp lang)
1514 (error "Not a symbol: %s" lang))
1515
1516 (let ((sym (intern (symbol-name name) c-lang-constants))
1517 mode source-files args)
1518
1519 (if lang
1520 (progn
1521 (setq mode (intern (concat (symbol-name lang) "-mode")))
1522 (unless (get mode 'c-mode-prefix)
1523 (error
1524 "Unknown language %S since it got no `c-mode-prefix' property"
1525 (symbol-name lang))))
1526 (if c-buffer-is-cc-mode
1527 (setq lang c-buffer-is-cc-mode)
1528 (or c-langs-are-parametric
1529 (error
1530 "`c-lang-const' requires a literal language in this context"))))
1531
1532 (if (eq c-lang-const-expansion 'immediate)
1533 ;; No need to find out the source file(s) when we evaluate
1534 ;; immediately since all the info is already there in the
1535 ;; `source' property.
1536 `',(c-get-lang-constant name nil mode)
1537
1538 (let ((file (c-get-current-file)))
1539 (if file (setq file (intern file)))
1540 ;; Get the source file(s) that must be loaded to get the value
1541 ;; of the constant. If the symbol isn't defined yet we assume
1542 ;; that its definition will come later in this file, and thus
1543 ;; are no file dependencies needed.
1544 (setq source-files (nreverse
1545 ;; Reverse to get the right load order.
1546 (apply 'nconc
1547 (mapcar (lambda (elem)
1548 (if (eq file (car elem))
1549 nil ; Exclude our own file.
1550 (list (car elem))))
1551 (get sym 'source))))))
1552
1553 ;; Spend some effort to make a compact call to
1554 ;; `c-get-lang-constant' since it will be compiled in.
1555 (setq args (and mode `(',mode)))
1556 (if (or source-files args)
1557 (setq args (cons (and source-files `',source-files)
1558 args)))
1559
1560 (if (or (eq c-lang-const-expansion 'call)
1561 load-in-progress
1562 (not (boundp 'byte-compile-dest-file))
1563 (not (stringp byte-compile-dest-file)))
1564 ;; Either a straight call is requested in the context, or
1565 ;; we're not being byte compiled so the compile time stuff
1566 ;; below is unnecessary.
1567 `(c-get-lang-constant ',name ,@args)
1568
1569 ;; Being compiled. If the loading and compiling version is
1570 ;; the same we use a value that is evaluated at compile time,
1571 ;; otherwise it's evaluated at runtime.
1572 `(if (eq c-version-sym ',c-version-sym)
1573 (cc-eval-when-compile
1574 (c-get-lang-constant ',name ,@args))
1575 (c-get-lang-constant ',name ,@args))))))
1576
1577 (defvar c-lang-constants-under-evaluation nil)
1578
1579 (defun c-get-lang-constant (name &optional source-files mode)
1580 ;; Used by `c-lang-const'. This function does not do any hidden
1581 ;; buffer changes.
1582
1583 (or mode
1584 (setq mode c-buffer-is-cc-mode)
1585 (error "No current language"))
1586
1587 (let* ((sym (intern (symbol-name name) c-lang-constants))
1588 (source (get sym 'source))
1589 elem
1590 (eval-in-sym (and c-lang-constants-under-evaluation
1591 (caar c-lang-constants-under-evaluation))))
1592
1593 ;; Record the dependencies between this symbol and the one we're
1594 ;; being evaluated in.
1595 (when eval-in-sym
1596 (or (memq eval-in-sym (get sym 'dependents))
1597 (put sym 'dependents (cons eval-in-sym (get sym 'dependents)))))
1598
1599 ;; Make sure the source files have entries on the `source'
1600 ;; property so that loading will take place when necessary.
1601 (while source-files
1602 (unless (assq (car source-files) source)
1603 (put sym 'source
1604 (setq source (cons (list (car source-files)) source)))
1605 ;; Might pull in more definitions which affect the value. The
1606 ;; clearing of dependent values etc is done when the
1607 ;; definition is encountered during the load; this is just to
1608 ;; jump past the check for a cached value below.
1609 (set sym nil))
1610 (setq source-files (cdr source-files)))
1611
1612 (if (and (boundp sym)
1613 (setq elem (assq mode (symbol-value sym))))
1614 (cdr elem)
1615
1616 ;; Check if an evaluation of this symbol is already underway.
1617 ;; In that case we just continue with the "assignment" before
1618 ;; the one currently being evaluated, thereby creating the
1619 ;; illusion if a `setq'-like sequence of assignments.
1620 (let* ((c-buffer-is-cc-mode mode)
1621 (source-pos
1622 (or (assq sym c-lang-constants-under-evaluation)
1623 (cons sym (vector source nil))))
1624 ;; Append `c-lang-constants-under-evaluation' even if an
1625 ;; earlier entry is found. It's only necessary to get
1626 ;; the recording of dependencies above correct.
1627 (c-lang-constants-under-evaluation
1628 (cons source-pos c-lang-constants-under-evaluation))
1629 (fallback (get mode 'c-fallback-mode))
1630 value
1631 ;; Make sure the recursion limits aren't very low
1632 ;; since the `c-lang-const' dependencies can go deep.
1633 (max-specpdl-size (max max-specpdl-size 3000))
1634 (max-lisp-eval-depth (max max-lisp-eval-depth 1000)))
1635
1636 (if (if fallback
1637 (let ((backup-source-pos (copy-sequence (cdr source-pos))))
1638 (and
1639 ;; First try the original mode but don't accept an
1640 ;; entry matching all languages since the fallback
1641 ;; mode might have an explicit entry before that.
1642 (eq (setq value (c-find-assignment-for-mode
1643 (cdr source-pos) mode nil name))
1644 c-lang-constants)
1645 ;; Try again with the fallback mode from the
1646 ;; original position. Note that
1647 ;; `c-buffer-is-cc-mode' still is the real mode if
1648 ;; language parameterization takes place.
1649 (eq (setq value (c-find-assignment-for-mode
1650 (setcdr source-pos backup-source-pos)
1651 fallback t name))
1652 c-lang-constants)))
1653 ;; A simple lookup with no fallback mode.
1654 (eq (setq value (c-find-assignment-for-mode
1655 (cdr source-pos) mode t name))
1656 c-lang-constants))
1657 (error
1658 "`%s' got no (prior) value in %s (might be a cyclic reference)"
1659 name mode))
1660
1661 (condition-case err
1662 (setq value (eval value))
1663 (error
1664 ;; Print a message to aid in locating the error. We don't
1665 ;; print the error itself since that will be done later by
1666 ;; some caller higher up.
1667 (message "Eval error in the `c-lang-defconst' for `%s' in %s:"
1668 sym mode)
1669 (makunbound sym)
1670 (signal (car err) (cdr err))))
1671
1672 (set sym (cons (cons mode value) (symbol-value sym)))
1673 value))))
1674
1675 (defun c-find-assignment-for-mode (source-pos mode match-any-lang name)
1676 ;; Find the first assignment entry that applies to MODE at or after
1677 ;; SOURCE-POS. If MATCH-ANY-LANG is non-nil, entries with `t' as
1678 ;; the language list are considered to match, otherwise they don't.
1679 ;; On return SOURCE-POS is updated to point to the next assignment
1680 ;; after the returned one. If no assignment is found,
1681 ;; `c-lang-constants' is returned as a magic value.
1682 ;;
1683 ;; SOURCE-POS is a vector that points out a specific assignment in
1684 ;; the double alist that's used in the `source' property. The first
1685 ;; element is the position in the top alist which is indexed with
1686 ;; the source files, and the second element is the position in the
1687 ;; nested bindings alist.
1688 ;;
1689 ;; NAME is only used for error messages.
1690
1691 (catch 'found
1692 (let ((file-entry (elt source-pos 0))
1693 (assignment-entry (elt source-pos 1))
1694 assignment)
1695
1696 (while (if assignment-entry
1697 t
1698 ;; Handled the last assignment from one file, begin on the
1699 ;; next. Due to the check in `c-lang-defconst', we know
1700 ;; there's at least one.
1701 (when file-entry
1702
1703 (unless (aset source-pos 1
1704 (setq assignment-entry (cdar file-entry)))
1705 ;; The file containing the source definitions has not
1706 ;; been loaded.
1707 (let ((file (symbol-name (caar file-entry)))
1708 (c-lang-constants-under-evaluation nil))
1709 ;;(message (concat "Loading %s to get the source "
1710 ;; "value for language constant %s")
1711 ;; file name)
1712 (load file))
1713
1714 (unless (setq assignment-entry (cdar file-entry))
1715 ;; The load didn't fill in the source for the
1716 ;; constant as expected. The situation is
1717 ;; probably that a derived mode was written for
1718 ;; and compiled with another version of CC Mode,
1719 ;; and the requested constant isn't in the
1720 ;; currently loaded one. Put in a dummy
1721 ;; assignment that matches no language.
1722 (setcdr (car file-entry)
1723 (setq assignment-entry (list (list nil))))))
1724
1725 (aset source-pos 0 (setq file-entry (cdr file-entry)))
1726 t))
1727
1728 (setq assignment (car assignment-entry))
1729 (aset source-pos 1
1730 (setq assignment-entry (cdr assignment-entry)))
1731
1732 (when (if (listp (car assignment))
1733 (memq mode (car assignment))
1734 match-any-lang)
1735 (throw 'found (cdr assignment))))
1736
1737 c-lang-constants)))
1738
1739 (defun c-lang-major-mode-is (mode)
1740 ;; `c-major-mode-is' expands to a call to this function inside
1741 ;; `c-lang-defconst'. Here we also match the mode(s) against any
1742 ;; fallback modes for the one in `c-buffer-is-cc-mode', so that
1743 ;; e.g. (c-major-mode-is 'c++-mode) is true in a derived language
1744 ;; that has c++-mode as base mode.
1745 (unless (listp mode)
1746 (setq mode (list mode)))
1747 (let (match (buf-mode c-buffer-is-cc-mode))
1748 (while (if (memq buf-mode mode)
1749 (progn
1750 (setq match t)
1751 nil)
1752 (setq buf-mode (get buf-mode 'c-fallback-mode))))
1753 match))
1754
1755 \f
1756 (cc-provide 'cc-defs)
1757
1758 ;;; arch-tag: 3bb2629d-dd84-4ff0-ad39-584be0fe3cda
1759 ;;; cc-defs.el ends here