cc-defs.el 83 KB
Newer Older
1
;;; cc-defs.el --- compile time definitions for CC Mode
Richard M. Stallman's avatar
Richard M. Stallman committed
2

3
;; Copyright (C) 1985, 1987, 1992-2014 Free Software Foundation, Inc.
Richard M. Stallman's avatar
Richard M. Stallman committed
4

Alan Mackenzie's avatar
Alan Mackenzie committed
5 6
;; Authors:    2003- Alan Mackenzie
;;             1998- Martin Stjernholm
7
;;             1992-1999 Barry A. Warsaw
8 9
;;             1987 Dave Detlefs
;;             1987 Stewart Clamen
Richard M. Stallman's avatar
Richard M. Stallman committed
10
;;             1985 Richard M. Stallman
Barry A. Warsaw's avatar
Barry A. Warsaw committed
11
;; Maintainer: bug-cc-mode@gnu.org
Richard M. Stallman's avatar
Richard M. Stallman committed
12
;; Created:    22-Apr-1997 (split from cc-mode.el)
13 14
;; Keywords:   c languages
;; Package:    cc-mode
Richard M. Stallman's avatar
Richard M. Stallman committed
15 16 17

;; This file is part of GNU Emacs.

18
;; GNU Emacs is free software: you can redistribute it and/or modify
Richard M. Stallman's avatar
Richard M. Stallman committed
19
;; it under the terms of the GNU General Public License as published by
20 21
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
Richard M. Stallman's avatar
Richard M. Stallman committed
22 23 24 25 26 27 28

;; GNU Emacs is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;; GNU General Public License for more details.

;; You should have received a copy of the GNU General Public License
29
;; along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.
Richard M. Stallman's avatar
Richard M. Stallman committed
30

31 32
;;; Commentary:

33 34 35
;; This file contains macros, defsubsts, and various other things that
;; must be loaded early both during compilation and at runtime.

36 37
;;; Code:

Gerd Moellmann's avatar
Gerd Moellmann committed
38 39 40 41 42 43
(eval-when-compile
  (let ((load-path
	 (if (and (boundp 'byte-compile-dest-file)
		  (stringp byte-compile-dest-file))
	     (cons (file-name-directory byte-compile-dest-file) load-path)
	   load-path)))
44 45
    (load "cc-bytecomp" nil t)))

46 47
(eval-when-compile (require 'cl)) ; was (cc-external-require 'cl).  ACM 2005/11/29.
(cc-external-require 'regexp-opt)
Barry A. Warsaw's avatar
Barry A. Warsaw committed
48

49 50 51 52 53 54
;; Silence the compiler.
(cc-bytecomp-defvar c-enable-xemacs-performance-kludge-p) ; In cc-vars.el
(cc-bytecomp-defun region-active-p)	; XEmacs
(cc-bytecomp-defvar mark-active)	; Emacs
(cc-bytecomp-defvar deactivate-mark)	; Emacs
(cc-bytecomp-defvar inhibit-point-motion-hooks) ; Emacs
55
(cc-bytecomp-defvar parse-sexp-lookup-properties) ; Emacs
56 57 58 59 60
(cc-bytecomp-defvar text-property-default-nonsticky) ; Emacs 21
(cc-bytecomp-defun string-to-syntax)	; Emacs 21


;; cc-fix.el contains compatibility macros that should be used if
Gerd Moellmann's avatar
Gerd Moellmann committed
61 62
;; needed.
(eval-and-compile
63 64 65
  (if (or (/= (regexp-opt-depth "\\(\\(\\)\\)") 2)
	  (not (fboundp 'push)))
      (cc-load "cc-fix")))
Gerd Moellmann's avatar
Gerd Moellmann committed
66

67 68
; (eval-after-load "font-lock"  ; 2006-07-09.  font-lock is now preloaded
;   '
69
(if (and (featurep 'xemacs)	; There is now (2005/12) code in GNU Emacs CVS
70
				; to make the call to f-l-c-k throw an error.
71
	 (not (featurep 'cc-fix)) ; only load the file once.
72 73 74 75
	 (let (font-lock-keywords)
	   (font-lock-compile-keywords '("\\<\\>"))
	   font-lock-keywords))     ; did the previous call foul this up?
    (load "cc-fix")) ;)
76 77 78 79

;; The above takes care of the delayed loading, but this is necessary
;; to ensure correct byte compilation.
(eval-when-compile
80 81
  (if (and (featurep 'xemacs)
	   (not (featurep 'cc-fix))
82 83 84
	   (progn
	     (require 'font-lock)
	     (let (font-lock-keywords)
85
	       (font-lock-compile-keywords '("\\<\\>"))
86 87 88
	       font-lock-keywords)))
      (cc-load "cc-fix")))

Richard M. Stallman's avatar
Richard M. Stallman committed
89

90 91
;;; Variables also used at compile time.

92
(defconst c-version "5.32.5"
93 94 95 96 97 98 99 100 101
  "CC Mode version number.")

(defconst c-version-sym (intern c-version))
;; A little more compact and faster in comparisons.

(defvar c-buffer-is-cc-mode nil
  "Non-nil for all buffers with a major mode derived from CC Mode.
Otherwise, this variable is nil.  I.e. this variable is non-nil for
`c-mode', `c++-mode', `objc-mode', `java-mode', `idl-mode',
102 103 104 105
`pike-mode', `awk-mode', and any other non-CC Mode mode that calls
`c-initialize-cc-mode'.  The value is the mode symbol itself
\(i.e. `c-mode' etc) of the original CC Mode mode, or just t if it's
not known.")
106 107 108 109 110 111 112 113 114 115
(make-variable-buffer-local 'c-buffer-is-cc-mode)

;; Have to make `c-buffer-is-cc-mode' permanently local so that it
;; survives the initialization of the derived mode.
(put 'c-buffer-is-cc-mode 'permanent-local t)


;; The following is used below during compilation.
(eval-and-compile
  (defvar c-inside-eval-when-compile nil)
Gerd Moellmann's avatar
Gerd Moellmann committed
116

117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171
  (defmacro cc-eval-when-compile (&rest body)
    "Like `progn', but evaluates the body at compile time.
The result of the body appears to the compiler as a quoted constant.

This variant works around bugs in `eval-when-compile' in various
\(X)Emacs versions.  See cc-defs.el for details."

    (if c-inside-eval-when-compile
	;; XEmacs 21.4.6 has a bug in `eval-when-compile' in that it
	;; evaluates its body at macro expansion time if it's nested
	;; inside another `eval-when-compile'.  So we use a dynamically
	;; bound variable to avoid nesting them.
	`(progn ,@body)

      `(eval-when-compile
	 ;; In all (X)Emacsen so far, `eval-when-compile' byte compiles
	 ;; its contents before evaluating it.  That can cause forms to
	 ;; be compiled in situations they aren't intended to be
	 ;; compiled.
	 ;;
	 ;; Example: It's not possible to defsubst a primitive, e.g. the
	 ;; following will produce an error (in any emacs flavor), since
	 ;; `nthcdr' is a primitive function that's handled specially by
	 ;; the byte compiler and thus can't be redefined:
	 ;;
	 ;;     (defsubst nthcdr (val) val)
	 ;;
	 ;; `defsubst', like `defmacro', needs to be evaluated at
	 ;; compile time, so this will produce an error during byte
	 ;; compilation.
	 ;;
	 ;; CC Mode occasionally needs to do things like this for
	 ;; cross-emacs compatibility.  It therefore uses the following
	 ;; to conditionally do a `defsubst':
	 ;;
	 ;;     (eval-when-compile
	 ;;       (if (not (fboundp 'foo))
	 ;;           (defsubst foo ...)))
	 ;;
	 ;; But `eval-when-compile' byte compiles its contents and
	 ;; _then_ evaluates it (in all current emacs versions, up to
	 ;; and including Emacs 20.6 and XEmacs 21.1 as of this
	 ;; writing).  So this will still produce an error, since the
	 ;; byte compiler will get to the defsubst anyway.  That's
	 ;; arguably a bug because the point with `eval-when-compile' is
	 ;; that it should evaluate rather than compile its contents.
	 ;;
	 ;; We get around it by expanding the body to a quoted
	 ;; constant that we eval.  That otoh introduce a problem in
	 ;; that a returned lambda expression doesn't get byte
	 ;; compiled (even if `function' is used).
	 (eval '(let ((c-inside-eval-when-compile t)) ,@body)))))

  (put 'cc-eval-when-compile 'lisp-indent-hook 0))

172 173 174 175
(eval-and-compile
  (defalias 'c--macroexpand-all
    (if (fboundp 'macroexpand-all)
        'macroexpand-all 'cl-macroexpand-all)))
176 177

;;; Macros.
Gerd Moellmann's avatar
Gerd Moellmann committed
178 179

(defmacro c-point (position &optional point)
180 181 182 183
  "Return the value of certain commonly referenced POSITIONs relative to POINT.
The current point is used if POINT isn't specified.  POSITION can be
one of the following symbols:

184 185 186 187 188 189 190 191 192 193 194 195 196
`bol'   -- beginning of line
`eol'   -- end of line
`bod'   -- beginning of defun
`eod'   -- end of defun
`boi'   -- beginning of indentation
`ionl'  -- indentation of next line
`iopl'  -- indentation of previous line
`bonl'  -- beginning of next line
`eonl'  -- end of next line
`bopl'  -- beginning of previous line
`eopl'  -- end of previous line
`bosws' -- beginning of syntactic whitespace
`eosws' -- end of syntactic whitespace
197 198

If the referenced position doesn't exist, the closest accessible point
199
to it is returned.  This function does not modify the point or the mark."
200 201 202 203 204 205

  (if (eq (car-safe position) 'quote)
      (let ((position (eval position)))
	(cond

	 ((eq position 'bol)
206
	  (if (and (cc-bytecomp-fboundp 'line-beginning-position) (not point))
207 208 209 210 211 212 213
	      `(line-beginning-position)
	    `(save-excursion
	       ,@(if point `((goto-char ,point)))
	       (beginning-of-line)
	       (point))))

	 ((eq position 'eol)
214
	  (if (and (cc-bytecomp-fboundp 'line-end-position) (not point))
215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239
	      `(line-end-position)
	    `(save-excursion
	       ,@(if point `((goto-char ,point)))
	       (end-of-line)
	       (point))))

	 ((eq position 'boi)
	  `(save-excursion
	     ,@(if point `((goto-char ,point)))
	     (back-to-indentation)
	     (point)))

	 ((eq position 'bod)
	  `(save-excursion
	     ,@(if point `((goto-char ,point)))
	     (c-beginning-of-defun-1)
	     (point)))

	 ((eq position 'eod)
	  `(save-excursion
	     ,@(if point `((goto-char ,point)))
	     (c-end-of-defun-1)
	     (point)))

	 ((eq position 'bopl)
240
	  (if (and (cc-bytecomp-fboundp 'line-beginning-position) (not point))
241 242 243 244 245 246 247
	      `(line-beginning-position 0)
	    `(save-excursion
	       ,@(if point `((goto-char ,point)))
	       (forward-line -1)
	       (point))))

	 ((eq position 'bonl)
248
	  (if (and (cc-bytecomp-fboundp 'line-beginning-position) (not point))
249 250 251 252 253 254 255
	      `(line-beginning-position 2)
	    `(save-excursion
	       ,@(if point `((goto-char ,point)))
	       (forward-line 1)
	       (point))))

	 ((eq position 'eopl)
256
	  (if (and (cc-bytecomp-fboundp 'line-end-position) (not point))
257 258 259 260 261 262 263 264
	      `(line-end-position 0)
	    `(save-excursion
	       ,@(if point `((goto-char ,point)))
	       (beginning-of-line)
	       (or (bobp) (backward-char))
	       (point))))

	 ((eq position 'eonl)
265
	  (if (and (cc-bytecomp-fboundp 'line-end-position) (not point))
266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286
	      `(line-end-position 2)
	    `(save-excursion
	       ,@(if point `((goto-char ,point)))
	       (forward-line 1)
	       (end-of-line)
	       (point))))

	 ((eq position 'iopl)
	  `(save-excursion
	     ,@(if point `((goto-char ,point)))
	     (forward-line -1)
	     (back-to-indentation)
	     (point)))

	 ((eq position 'ionl)
	  `(save-excursion
	     ,@(if point `((goto-char ,point)))
	     (forward-line 1)
	     (back-to-indentation)
	     (point)))

287 288 289 290 291 292 293 294 295 296 297 298
	 ((eq position 'bosws)
	  `(save-excursion
	     ,@(if point `((goto-char ,point)))
	     (c-backward-syntactic-ws)
	     (point)))

	 ((eq position 'eosws)
	  `(save-excursion
	     ,@(if point `((goto-char ,point)))
	     (c-forward-syntactic-ws)
	     (point)))

299 300
	 (t (error "Unknown buffer position requested: %s" position))))

301 302 303 304 305
    ;; The bulk of this should perhaps be in a function to avoid large
    ;; expansions, but this case is not used anywhere in CC Mode (and
    ;; probably not anywhere else either) so we only have it to be on
    ;; the safe side.
    (message "Warning: c-point long expansion")
306 307 308 309
    `(save-excursion
       ,@(if point `((goto-char ,point)))
       (let ((position ,position))
	 (cond
310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330
	  ((eq position 'bol)	(beginning-of-line))
	  ((eq position 'eol)	(end-of-line))
	  ((eq position 'boi)	(back-to-indentation))
	  ((eq position 'bod)	(c-beginning-of-defun-1))
	  ((eq position 'eod)	(c-end-of-defun-1))
	  ((eq position 'bopl)	(forward-line -1))
	  ((eq position 'bonl)	(forward-line 1))
	  ((eq position 'eopl)	(progn
				  (beginning-of-line)
				  (or (bobp) (backward-char))))
	  ((eq position 'eonl)	(progn
				  (forward-line 1)
				  (end-of-line)))
	  ((eq position 'iopl)	(progn
				  (forward-line -1)
				  (back-to-indentation)))
	  ((eq position 'ionl)	(progn
				  (forward-line 1)
				(back-to-indentation)))
	  ((eq position 'bosws)	(c-backward-syntactic-ws))
	  ((eq position 'eosws)	(c-forward-syntactic-ws))
331 332
	  (t (error "Unknown buffer position requested: %s" position))))
       (point))))
Richard M. Stallman's avatar
Richard M. Stallman committed
333

334 335 336
(defmacro c-region-is-active-p ()
  ;; Return t when the region is active.  The determination of region
  ;; activeness is different in both Emacs and XEmacs.
337 338
  ;; FIXME? Emacs has region-active-p since 23.1, so maybe this test
  ;; should be updated.
339 340 341 342 343
  (if (cc-bytecomp-boundp 'mark-active)
      ;; Emacs.
      'mark-active
    ;; XEmacs.
    '(region-active-p)))
344 345 346 347

(defmacro c-set-region-active (activate)
  ;; Activate the region if ACTIVE is non-nil, deactivate it
  ;; otherwise.  Covers the differences between Emacs and XEmacs.
348
  (if (fboundp 'zmacs-activate-region)
349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365
      ;; XEmacs.
      `(if ,activate
	   (zmacs-activate-region)
	 (zmacs-deactivate-region))
    ;; Emacs.
    `(setq mark-active ,activate)))

(defmacro c-delete-and-extract-region (start end)
  "Delete the text between START and END and return it."
  (if (cc-bytecomp-fboundp 'delete-and-extract-region)
      ;; Emacs 21.1 and later
      `(delete-and-extract-region ,start ,end)
    ;; XEmacs and Emacs 20.x
    `(prog1
       (buffer-substring ,start ,end)
       (delete-region ,start ,end))))

Richard M. Stallman's avatar
Richard M. Stallman committed
366 367
(defmacro c-safe (&rest body)
  ;; safely execute BODY, return nil if an error occurred
Gerd Moellmann's avatar
Gerd Moellmann committed
368 369 370
  `(condition-case nil
       (progn ,@body)
     (error nil)))
371
(put 'c-safe 'lisp-indent-function 0)
Gerd Moellmann's avatar
Gerd Moellmann committed
372

373
(defmacro c-int-to-char (integer)
374
  ;; In Emacs, a character is an integer.  In XEmacs, a character is a
375 376 377 378 379 380
  ;; type distinct from an integer.  Sometimes we need to convert integers to
  ;; characters.  `c-int-to-char' makes this conversion, if necessary.
  (if (fboundp 'int-to-char)
      `(int-to-char ,integer)
    integer))

381 382 383 384 385 386 387
(defmacro c-last-command-char ()
  ;; The last character just typed.  Note that `last-command-event' exists in
  ;; both Emacs and XEmacs, but with confusingly different meanings.
  (if (featurep 'xemacs)
      'last-command-char
    'last-command-event))

388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403
(defmacro c-sentence-end ()
  ;; Get the regular expression `sentence-end'.
  (if (cc-bytecomp-fboundp 'sentence-end)
      ;; Emacs 22:
      `(sentence-end)
    ;; Emacs <22 + XEmacs
    `sentence-end))

(defmacro c-default-value-sentence-end ()
  ;; Get the default value of the variable sentence end.
  (if (cc-bytecomp-fboundp 'sentence-end)
      ;; Emacs 22:
      `(let (sentence-end) (sentence-end))
    ;; Emacs <22 + XEmacs
    `(default-value 'sentence-end)))

404 405 406 407 408
;; The following is essentially `save-buffer-state' from lazy-lock.el.
;; It ought to be a standard macro.
(defmacro c-save-buffer-state (varlist &rest body)
  "Bind variables according to VARLIST (in `let*' style) and eval BODY,
then restore the buffer state under the assumption that no significant
409 410 411 412 413 414 415 416
modification has been made in BODY.  A change is considered
significant if it affects the buffer text in any way that isn't
completely restored again.  Changes in text properties like `face' or
`syntax-table' are considered insignificant.  This macro allows text
properties to be changed, even in a read-only buffer.

This macro should be placed around all calculations which set
\"insignificant\" text properties in a buffer, even when the buffer is
Chong Yidong's avatar
Chong Yidong committed
417
known to be writable.  That way, these text properties remain set
418 419 420 421 422 423 424 425 426 427
even if the user undoes the command which set them.

This macro should ALWAYS be placed around \"temporary\" internal buffer
changes \(like adding a newline to calculate a text-property then
deleting it again\), so that the user never sees them on his
`buffer-undo-list'.  See also `c-tentative-buffer-changes'.

However, any user-visible changes to the buffer \(like auto-newlines\)
must not be within a `c-save-buffer-state', since the user then
wouldn't be able to undo them.
428 429 430 431 432 433

The return value is the value of the last form in BODY."
  `(let* ((modified (buffer-modified-p)) (buffer-undo-list t)
	  (inhibit-read-only t) (inhibit-point-motion-hooks t)
	  before-change-functions after-change-functions
	  deactivate-mark
434 435
	  buffer-file-name buffer-file-truename ; Prevent primitives checking
						; for file modification
436
	  ,@varlist)
437 438
     (unwind-protect
	 (progn ,@body)
439 440 441 442
       (and (not modified)
	    (buffer-modified-p)
	    (set-buffer-modified-p nil)))))
(put 'c-save-buffer-state 'lisp-indent-function 1)
Gerd Moellmann's avatar
Gerd Moellmann committed
443

444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484
(defmacro c-tentative-buffer-changes (&rest body)
  "Eval BODY and optionally restore the buffer contents to the state it
was in before BODY.  Any changes are kept if the last form in BODY
returns non-nil.  Otherwise it's undone using the undo facility, and
various other buffer state that might be affected by the changes is
restored.  That includes the current buffer, point, mark, mark
activation \(similar to `save-excursion'), and the modified state.
The state is also restored if BODY exits nonlocally.

If BODY makes a change that unconditionally is undone then wrap this
macro inside `c-save-buffer-state'.  That way the change can be done
even when the buffer is read-only, and without interference from
various buffer change hooks."
  `(let (-tnt-chng-keep
	 -tnt-chng-state)
     (unwind-protect
	 ;; Insert an undo boundary for use with `undo-more'.  We
	 ;; don't use `undo-boundary' since it doesn't insert one
	 ;; unconditionally.
	 (setq buffer-undo-list (cons nil buffer-undo-list)
	       -tnt-chng-state (c-tnt-chng-record-state)
	       -tnt-chng-keep (progn ,@body))
       (c-tnt-chng-cleanup -tnt-chng-keep -tnt-chng-state))))
(put 'c-tentative-buffer-changes 'lisp-indent-function 0)

(defun c-tnt-chng-record-state ()
  ;; Used internally in `c-tentative-buffer-changes'.
  (vector buffer-undo-list		; 0
	  (current-buffer)		; 1
	  ;; No need to use markers for the point and mark; if the
	  ;; undo got out of synch we're hosed anyway.
	  (point)			; 2
	  (mark t)			; 3
	  (c-region-is-active-p)	; 4
	  (buffer-modified-p)))		; 5

(defun c-tnt-chng-cleanup (keep saved-state)
  ;; Used internally in `c-tentative-buffer-changes'.

  (let ((saved-undo-list (elt saved-state 0)))
    (if (eq buffer-undo-list saved-undo-list)
Paul Eggert's avatar
Paul Eggert committed
485
	;; No change was done after all.
486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510
	(setq buffer-undo-list (cdr saved-undo-list))

      (if keep
	  ;; Find and remove the undo boundary.
	  (let ((p buffer-undo-list))
	    (while (not (eq (cdr p) saved-undo-list))
	      (setq p (cdr p)))
	    (setcdr p (cdr saved-undo-list)))

	;; `primitive-undo' will remove the boundary.
	(setq saved-undo-list (cdr saved-undo-list))
	(let ((undo-in-progress t))
	  (while (not (eq (setq buffer-undo-list
				(primitive-undo 1 buffer-undo-list))
			  saved-undo-list))))

	(when (buffer-live-p (elt saved-state 1))
	  (set-buffer (elt saved-state 1))
	  (goto-char (elt saved-state 2))
	  (set-mark (elt saved-state 3))
	  (c-set-region-active (elt saved-state 4))
	  (and (not (elt saved-state 5))
	       (buffer-modified-p)
	       (set-buffer-modified-p nil)))))))

511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556
(defmacro c-forward-syntactic-ws (&optional limit)
  "Forward skip over syntactic whitespace.
Syntactic whitespace is defined as whitespace characters, comments,
and preprocessor directives.  However if point starts inside a comment
or preprocessor directive, the content of it is not treated as
whitespace.

LIMIT sets an upper limit of the forward movement, if specified.  If
LIMIT or the end of the buffer is reached inside a comment or
preprocessor directive, the point will be left there.

Note that this function might do hidden buffer changes.  See the
comment at the start of cc-engine.el for more info."
  (if limit
      `(save-restriction
	 (narrow-to-region (point-min) (or ,limit (point-max)))
	 (c-forward-sws))
    '(c-forward-sws)))

(defmacro c-backward-syntactic-ws (&optional limit)
  "Backward skip over syntactic whitespace.
Syntactic whitespace is defined as whitespace characters, comments,
and preprocessor directives.  However if point starts inside a comment
or preprocessor directive, the content of it is not treated as
whitespace.

LIMIT sets a lower limit of the backward movement, if specified.  If
LIMIT is reached inside a line comment or preprocessor directive then
the point is moved into it past the whitespace at the end.

Note that this function might do hidden buffer changes.  See the
comment at the start of cc-engine.el for more info."
  (if limit
      `(save-restriction
	 (narrow-to-region (or ,limit (point-min)) (point-max))
	 (c-backward-sws))
    '(c-backward-sws)))

(defmacro c-forward-sexp (&optional count)
  "Move forward across COUNT balanced expressions.
A negative COUNT means move backward.  Signal an error if the move
fails for any reason.

This is like `forward-sexp' except that it isn't interactive and does
not do any user friendly adjustments of the point and that it isn't
susceptible to user configurations such as disabling of signals in
557
certain situations."
558
  (or count (setq count 1))
559
  `(goto-char (scan-sexps (point) ,count)))
560 561 562 563 564 565

(defmacro c-backward-sexp (&optional count)
  "See `c-forward-sexp' and reverse directions."
  (or count (setq count 1))
  `(c-forward-sexp ,(if (numberp count) (- count) `(- ,count))))

566
(defmacro c-safe-scan-lists (from count depth &optional limit)
Paul Eggert's avatar
Paul Eggert committed
567
  "Like `scan-lists' but returns nil instead of signaling errors
568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585
for unbalanced parens.

A limit for the search may be given.  FROM is assumed to be on the
right side of it."
  (let ((res (if (featurep 'xemacs)
		 `(scan-lists ,from ,count ,depth nil t)
	       `(c-safe (scan-lists ,from ,count ,depth)))))
    (if limit
	`(save-restriction
	   ,(if (numberp count)
		(if (< count 0)
		    `(narrow-to-region ,limit (point-max))
		  `(narrow-to-region (point-min) ,limit))
	      `(if (< ,count 0)
		   (narrow-to-region ,limit (point-max))
		 (narrow-to-region (point-min) ,limit)))
	   ,res)
      res)))
586 587


588 589
;; Wrappers for common scan-lists cases, mainly because it's almost
;; impossible to get a feel for how that function works.
590

591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608
(defmacro c-go-list-forward ()
  "Move backward across one balanced group of parentheses.

Return POINT when we succeed, NIL when we fail.  In the latter case, leave
point unmoved."
  `(c-safe (let ((endpos (scan-lists (point) 1 0)))
	     (goto-char endpos)
	     endpos)))

(defmacro c-go-list-backward ()
  "Move backward across one balanced group of parentheses.

Return POINT when we succeed, NIL when we fail.  In the latter case, leave
point unmoved."
  `(c-safe (let ((endpos (scan-lists (point) -1 0)))
	     (goto-char endpos)
	     endpos)))

609
(defmacro c-up-list-forward (&optional pos limit)
610 611 612
  "Return the first position after the list sexp containing POS,
or nil if no such position exists.  The point is used if POS is left out.

613 614 615
A limit for the search may be given.  The start position is assumed to
be before it."
  `(c-safe-scan-lists ,(or pos `(point)) 1 1 ,limit))
616

617
(defmacro c-up-list-backward (&optional pos limit)
618 619 620
  "Return the position of the start of the list sexp containing POS,
or nil if no such position exists.  The point is used if POS is left out.

621 622 623
A limit for the search may be given.  The start position is assumed to
be after it."
  `(c-safe-scan-lists ,(or pos `(point)) -1 1 ,limit))
624

625
(defmacro c-down-list-forward (&optional pos limit)
626 627 628
  "Return the first position inside the first list sexp after POS,
or nil if no such position exists.  The point is used if POS is left out.

629 630 631
A limit for the search may be given.  The start position is assumed to
be before it."
  `(c-safe-scan-lists ,(or pos `(point)) 1 -1 ,limit))
632

633
(defmacro c-down-list-backward (&optional pos limit)
634 635 636
  "Return the last position inside the last list sexp before POS,
or nil if no such position exists.  The point is used if POS is left out.

637 638 639
A limit for the search may be given.  The start position is assumed to
be after it."
  `(c-safe-scan-lists ,(or pos `(point)) -1 -1 ,limit))
640

641
(defmacro c-go-up-list-forward (&optional pos limit)
642
  "Move the point to the first position after the list sexp containing POS,
643 644 645 646 647 648 649 650 651 652 653 654 655
or containing the point if POS is left out.  Return t if such a
position exists, otherwise nil is returned and the point isn't moved.

A limit for the search may be given.  The start position is assumed to
be before it."
  (let ((res `(c-safe (goto-char (scan-lists ,(or pos `(point)) 1 1)) t)))
    (if limit
	`(save-restriction
	   (narrow-to-region (point-min) ,limit)
	   ,res)
      res)))

(defmacro c-go-up-list-backward (&optional pos limit)
656
  "Move the point to the position of the start of the list sexp containing POS,
657 658 659 660 661 662 663 664 665 666 667 668 669
or containing the point if POS is left out.  Return t if such a
position exists, otherwise nil is returned and the point isn't moved.

A limit for the search may be given.  The start position is assumed to
be after it."
  (let ((res `(c-safe (goto-char (scan-lists ,(or pos `(point)) -1 1)) t)))
    (if limit
	`(save-restriction
	   (narrow-to-region ,limit (point-max))
	   ,res)
      res)))

(defmacro c-go-down-list-forward (&optional pos limit)
670
  "Move the point to the first position inside the first list sexp after POS,
671 672 673 674 675 676 677 678 679 680 681 682 683
or before the point if POS is left out.  Return t if such a position
exists, otherwise nil is returned and the point isn't moved.

A limit for the search may be given.  The start position is assumed to
be before it."
  (let ((res `(c-safe (goto-char (scan-lists ,(or pos `(point)) 1 -1)) t)))
    (if limit
	`(save-restriction
	   (narrow-to-region (point-min) ,limit)
	   ,res)
      res)))

(defmacro c-go-down-list-backward (&optional pos limit)
684
  "Move the point to the last position inside the last list sexp before POS,
685 686 687 688 689 690 691 692 693 694 695
or before the point if POS is left out.  Return t if such a position
exists, otherwise nil is returned and the point isn't moved.

A limit for the search may be given.  The start position is assumed to
be after it."
  (let ((res `(c-safe (goto-char (scan-lists ,(or pos `(point)) -1 -1)) t)))
    (if limit
	`(save-restriction
	   (narrow-to-region ,limit (point-max))
	   ,res)
      res)))
696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711


(defmacro c-beginning-of-defun-1 ()
  ;; Wrapper around beginning-of-defun.
  ;;
  ;; NOTE: This function should contain the only explicit use of
  ;; beginning-of-defun in CC Mode.  Eventually something better than
  ;; b-o-d will be available and this should be the only place the
  ;; code needs to change.  Everything else should use
  ;; (c-beginning-of-defun-1)
  ;;
  ;; This is really a bit too large to be a macro but that isn't a
  ;; problem as long as it only is used in one place in
  ;; `c-parse-state'.

  `(progn
712
     (if (and ,(fboundp 'buffer-syntactic-context-depth)
713
	      c-enable-xemacs-performance-kludge-p)
714
	 ,(when (fboundp 'buffer-syntactic-context-depth)
715 716 717 718
	    ;; XEmacs only.  This can improve the performance of
	    ;; c-parse-state to between 3 and 60 times faster when
	    ;; braces are hung.  It can also degrade performance by
	    ;; about as much when braces are not hung.
719 720
	    '(let (beginning-of-defun-function end-of-defun-function
					       pos)
721 722 723 724 725 726 727 728 729
	       (while (not pos)
		 (save-restriction
		   (widen)
		   (setq pos (c-safe-scan-lists
			      (point) -1 (buffer-syntactic-context-depth))))
		 (cond
		  ((bobp) (setq pos (point-min)))
		  ((not pos)
		   (let ((distance (skip-chars-backward "^{")))
Juanma Barranquero's avatar
Juanma Barranquero committed
730
		     ;; unbalanced parenthesis, while invalid C code,
731 732 733 734 735 736 737 738 739 740 741 742
		     ;; shouldn't cause an infloop!  See unbal.c
		     (when (zerop distance)
		       ;; Punt!
		       (beginning-of-defun)
		       (setq pos (point)))))
		  ((= pos 0))
		  ((not (eq (char-after pos) ?{))
		   (goto-char pos)
		   (setq pos nil))
		  ))
	       (goto-char pos)))
       ;; Emacs, which doesn't have buffer-syntactic-context-depth
743 744
       (let (beginning-of-defun-function end-of-defun-function)
	 (beginning-of-defun)))
745 746 747 748 749
     ;; if defun-prompt-regexp is non-nil, b-o-d won't leave us at the
     ;; open brace.
     (and defun-prompt-regexp
	  (looking-at defun-prompt-regexp)
	  (goto-char (match-end 0)))))
750

751 752

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
753
;; V i r t u a l   S e m i c o l o n s
754 755
;;
;; In most CC Mode languages, statements are terminated explicitly by
756 757 758 759 760
;; semicolons or closing braces.  In some of the CC modes (currently AWK Mode
;; and certain user-specified #define macros in C, C++, etc. (November 2008)),
;; statements are (or can be) terminated by EOLs.  Such a statement is said to
;; be terminated by a "virtual semicolon" (VS).  A statement terminated by an
;; actual semicolon or brace is never considered to have a VS.
761 762 763 764
;;
;; The indentation engine (or whatever) tests for a VS at a specific position
;; by invoking the macro `c-at-vsemi-p', which in its turn calls the mode
;; specific function (if any) which is the value of the language variable
765 766 767 768 769
;; `c-at-vsemi-p-fn'.  This function should only use "low-level" features of
;; CC Mode, i.e. features which won't trigger infinite recursion.  ;-) The
;; actual details of what constitutes a VS in a language are thus encapsulated
;; in code specific to that language (e.g. cc-awk.el).  `c-at-vsemi-p' returns
;; non-nil if point (or the optional parameter POS) is at a VS, nil otherwise.
770 771
;;
;; The language specific function might well do extensive analysis of the
Paul Eggert's avatar
Paul Eggert committed
772
;; source text, and may use a caching scheme to speed up repeated calls.
773 774 775 776 777 778 779 780 781 782 783 784 785
;;
;; The "virtual semicolon" lies just after the last non-ws token on the line.
;; Like POINT, it is considered to lie between two characters.  For example,
;; at the place shown in the following AWK source line:
;;
;;          kbyte = 1024             # 1000 if you're not picky
;;                      ^
;;                      |
;;              Virtual Semicolon
;;
;; In addition to `c-at-vsemi-p-fn', a mode may need to supply a function for
;; `c-vsemi-status-unknown-p-fn'.  The macro `c-vsemi-status-unknown-p' is a
;; rather recondite kludge.  It exists because the function
Paul Eggert's avatar
Paul Eggert committed
786
;; `c-beginning-of-statement-1' sometimes tests for VSs as an optimization,
787 788 789 790 791 792 793 794 795
;; but `c-at-vsemi-p' might well need to call `c-beginning-of-statement-1' in
;; its calculations, thus potentially leading to infinite recursion.
;;
;; The macro `c-vsemi-status-unknown-p' resolves this problem; it may return
;; non-nil at any time; returning nil is a guarantee that an immediate
;; invocation of `c-at-vsemi-p' at point will NOT call
;; `c-beginning-of-statement-1'.  `c-vsemi-status-unknown-p' may not itself
;; call `c-beginning-of-statement-1'.
;;
Paul Eggert's avatar
Paul Eggert committed
796
;; The macro `c-vsemi-status-unknown-p' will typically check the caching
797
;; scheme used by the `c-at-vsemi-p-fn', hence the name - the status is
798
;; "unknown" if there is no cache entry current for the line.
799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

(defmacro c-at-vsemi-p (&optional pos)
  ;; Is there a virtual semicolon (not a real one or a }) at POS (defaults to
  ;; point)?  Always returns nil for languages which don't have Virtual
  ;; semicolons.
  ;; This macro might do hidden buffer changes.
  `(if c-at-vsemi-p-fn
       (funcall c-at-vsemi-p-fn ,@(if pos `(,pos)))))

(defmacro c-vsemi-status-unknown-p ()
  ;; Return NIL only if it can be guaranteed that an immediate
  ;; (c-at-vsemi-p) will NOT call c-beginning-of-statement-1.  Otherwise,
  ;; return non-nil.  (See comments above).  The function invoked by this
  ;; macro MUST NOT UNDER ANY CIRCUMSTANCES itself call
  ;; c-beginning-of-statement-1.
  ;; Languages which don't have EOL terminated statements always return NIL
  ;; (they _know_ there's no vsemi ;-).
  `(if c-vsemi-status-unknown-p-fn (funcall c-vsemi-status-unknown-p-fn)))


820 821 822 823 824 825
(defmacro c-benign-error (format &rest args)
  ;; Formats an error message for the echo area and dings, i.e. like
  ;; `error' but doesn't abort.
  `(progn
     (message ,format ,@args)
     (ding)))
Gerd Moellmann's avatar
Gerd Moellmann committed
826 827 828 829 830 831 832 833 834 835 836 837

(defmacro c-with-syntax-table (table &rest code)
  ;; Temporarily switches to the specified syntax table in a failsafe
  ;; way to execute code.
  `(let ((c-with-syntax-table-orig-table (syntax-table)))
     (unwind-protect
	 (progn
	   (set-syntax-table ,table)
	   ,@code)
       (set-syntax-table c-with-syntax-table-orig-table))))
(put 'c-with-syntax-table 'lisp-indent-function 1)

838 839 840
(defmacro c-skip-ws-forward (&optional limit)
  "Skip over any whitespace following point.
This function skips over horizontal and vertical whitespace and line
841
continuations."
842
  (if limit
843
      `(let ((limit (or ,limit (point-max))))
844 845
	 (while (progn
		  ;; skip-syntax-* doesn't count \n as whitespace..
846
		  (skip-chars-forward " \t\n\r\f\v" limit)
847
		  (when (and (eq (char-after) ?\\)
848
			     (< (point) limit))
849 850 851 852
		    (forward-char)
		    (or (eolp)
			(progn (backward-char) nil))))))
    '(while (progn
853
	      (skip-chars-forward " \t\n\r\f\v")
854 855 856 857 858 859 860 861
	      (when (eq (char-after) ?\\)
		(forward-char)
		(or (eolp)
		    (progn (backward-char) nil)))))))

(defmacro c-skip-ws-backward (&optional limit)
  "Skip over any whitespace preceding point.
This function skips over horizontal and vertical whitespace and line
862
continuations."
863
  (if limit
864
      `(let ((limit (or ,limit (point-min))))
865 866
	 (while (progn
		  ;; skip-syntax-* doesn't count \n as whitespace..
867
		  (skip-chars-backward " \t\n\r\f\v" limit)
868 869
		  (and (eolp)
		       (eq (char-before) ?\\)
870
		       (> (point) limit)))
871 872
	   (backward-char)))
    '(while (progn
873
	      (skip-chars-backward " \t\n\r\f\v")
874 875 876 877
	      (and (eolp)
		   (eq (char-before) ?\\)))
       (backward-char))))

Martin Stjernholm's avatar
Martin Stjernholm committed
878 879 880
(eval-and-compile
  (defvar c-langs-are-parametric nil))

881 882
(defmacro c-major-mode-is (mode)
  "Return non-nil if the current CC Mode major mode is MODE.
883
MODE is either a mode symbol or a list of mode symbols."
Martin Stjernholm's avatar
Martin Stjernholm committed
884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899

  (if c-langs-are-parametric
      ;; Inside a `c-lang-defconst'.
      `(c-lang-major-mode-is ,mode)

    (if (eq (car-safe mode) 'quote)
	(let ((mode (eval mode)))
	  (if (listp mode)
	      `(memq c-buffer-is-cc-mode ',mode)
	    `(eq c-buffer-is-cc-mode ',mode)))

      `(let ((mode ,mode))
	 (if (listp mode)
	     (memq c-buffer-is-cc-mode mode)
	   (eq c-buffer-is-cc-mode mode))))))

900 901

;; Macros/functions to handle so-called "char properties", which are
902
;; properties set on a single character and that never spread to any
903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943
;; other characters.

(eval-and-compile
  ;; Constant used at compile time to decide whether or not to use
  ;; XEmacs extents.  Check all the extent functions we'll use since
  ;; some packages might add compatibility aliases for some of them in
  ;; Emacs.
  (defconst c-use-extents (and (cc-bytecomp-fboundp 'extent-at)
			       (cc-bytecomp-fboundp 'set-extent-property)
			       (cc-bytecomp-fboundp 'set-extent-properties)
			       (cc-bytecomp-fboundp 'make-extent)
			       (cc-bytecomp-fboundp 'extent-property)
			       (cc-bytecomp-fboundp 'delete-extent)
			       (cc-bytecomp-fboundp 'map-extents))))

;; `c-put-char-property' is complex enough in XEmacs and Emacs < 21 to
;; make it a function.
(defalias 'c-put-char-property-fun
  (cc-eval-when-compile
    (cond (c-use-extents
	   ;; XEmacs.
	   (byte-compile
	    (lambda (pos property value)
	      (let ((ext (extent-at pos nil property)))
		(if ext
		    (set-extent-property ext property value)
		  (set-extent-properties (make-extent pos (1+ pos))
					 (cons property
					       (cons value
						     '(start-open t
						       end-open t)))))))))

	  ((not (cc-bytecomp-boundp 'text-property-default-nonsticky))
	   ;; In Emacs < 21 we have to mess with the `rear-nonsticky' property.
	   (byte-compile
	    (lambda (pos property value)
	      (put-text-property pos (1+ pos) property value)
	      (let ((prop (get-text-property pos 'rear-nonsticky)))
		(or (memq property prop)
		    (put-text-property pos (1+ pos)
				       'rear-nonsticky
944 945 946
				       (cons property prop)))))))
	  ;; This won't be used for anything.
	  (t 'ignore))))
947 948 949 950 951 952 953 954 955 956 957 958
(cc-bytecomp-defun c-put-char-property-fun) ; Make it known below.

(defmacro c-put-char-property (pos property value)
  ;; Put the given property with the given value on the character at
  ;; POS and make it front and rear nonsticky, or start and end open
  ;; in XEmacs vocabulary.  If the character already has the given
  ;; property then the value is replaced, and the behavior is
  ;; undefined if that property has been put by some other function.
  ;; PROPERTY is assumed to be constant.
  ;;
  ;; If there's a `text-property-default-nonsticky' variable (Emacs
  ;; 21) then it's assumed that the property is present on it.
959 960
  ;;
  ;; This macro does a hidden buffer change.
961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002
  (setq property (eval property))
  (if (or c-use-extents
	  (not (cc-bytecomp-boundp 'text-property-default-nonsticky)))
      ;; XEmacs and Emacs < 21.
      `(c-put-char-property-fun ,pos ',property ,value)
    ;; In Emacs 21 we got the `rear-nonsticky' property covered
    ;; by `text-property-default-nonsticky'.
    `(let ((-pos- ,pos))
       (put-text-property -pos- (1+ -pos-) ',property ,value))))

(defmacro c-get-char-property (pos property)
  ;; Get the value of the given property on the character at POS if
  ;; it's been put there by `c-put-char-property'.  PROPERTY is
  ;; assumed to be constant.
  (setq property (eval property))
  (if c-use-extents
      ;; XEmacs.
      `(let ((ext (extent-at ,pos nil ',property)))
	 (if ext (extent-property ext ',property)))
    ;; Emacs.
    `(get-text-property ,pos ',property)))

;; `c-clear-char-property' is complex enough in Emacs < 21 to make it
;; a function, since we have to mess with the `rear-nonsticky' property.
(defalias 'c-clear-char-property-fun
  (cc-eval-when-compile
    (unless (or c-use-extents
		(cc-bytecomp-boundp 'text-property-default-nonsticky))
      (byte-compile
       (lambda (pos property)
	 (when (get-text-property pos property)
	   (remove-text-properties pos (1+ pos) (list property nil))
	   (put-text-property pos (1+ pos)
			      'rear-nonsticky
			      (delq property (get-text-property
					      pos 'rear-nonsticky)))))))))
(cc-bytecomp-defun c-clear-char-property-fun) ; Make it known below.

(defmacro c-clear-char-property (pos property)
  ;; Remove the given property on the character at POS if it's been put
  ;; there by `c-put-char-property'.  PROPERTY is assumed to be
  ;; constant.
1003 1004
  ;;
  ;; This macro does a hidden buffer change.
1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020
  (setq property (eval property))
  (cond (c-use-extents
	 ;; XEmacs.
	 `(let ((ext (extent-at ,pos nil ',property)))
	    (if ext (delete-extent ext))))
	((cc-bytecomp-boundp 'text-property-default-nonsticky)
	 ;; In Emacs 21 we got the `rear-nonsticky' property covered
	 ;; by `text-property-default-nonsticky'.
	 `(let ((pos ,pos))
	    (remove-text-properties pos (1+ pos)
				    '(,property nil))))
	(t
	 ;; Emacs < 21.
	 `(c-clear-char-property-fun ,pos ',property))))

(defmacro c-clear-char-properties (from to property)
Juanma Barranquero's avatar
Juanma Barranquero committed
1021
  ;; Remove all the occurrences of the given property in the given
1022 1023 1024 1025 1026 1027 1028
  ;; region that has been put with `c-put-char-property'.  PROPERTY is
  ;; assumed to be constant.
  ;;
  ;; Note that this function does not clean up the property from the
  ;; lists of the `rear-nonsticky' properties in the region, if such
  ;; are used.  Thus it should not be used for common properties like
  ;; `syntax-table'.
1029 1030
  ;;
  ;; This macro does hidden buffer changes.
1031 1032 1033 1034 1035 1036 1037 1038 1039
  (setq property (eval property))
  (if c-use-extents
      ;; XEmacs.
      `(map-extents (lambda (ext ignored)
		      (delete-extent ext))
		    nil ,from ,to nil nil ',property)
    ;; Emacs.
    `(remove-text-properties ,from ,to '(,property nil))))

1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077
(defmacro c-search-forward-char-property (property value &optional limit)
  "Search forward for a text-property PROPERTY having value VALUE.
LIMIT bounds the search.  The comparison is done with `equal'.

Leave point just after the character, and set the match data on
this character, and return point.  If VALUE isn't found, Return
nil; point is then left undefined."
  `(let ((place (point)))
     (while
	 (and
	  (< place ,(or limit '(point-max)))
	  (not (equal (get-text-property place ,property) ,value)))
       (setq place (next-single-property-change
		    place ,property nil ,(or limit '(point-max)))))
     (when (< place ,(or limit '(point-max)))
       (goto-char place)
       (search-forward-regexp ".")	; to set the match-data.
       (point))))

(defmacro c-search-backward-char-property (property value &optional limit)
  "Search backward for a text-property PROPERTY having value VALUE.
LIMIT bounds the search.  The comparison is done with `equal'.

Leave point just before the character, set the match data on this
character, and return point.  If VALUE isn't found, Return nil;
point is then left undefined."
  `(let ((place (point)))
     (while
	 (and
	  (> place ,(or limit '(point-min)))
	  (not (equal (get-text-property (1- place) ,property) ,value)))
       (setq place (previous-single-property-change
		    place ,property nil ,(or limit '(point-min)))))
     (when (> place ,(or limit '(point-max)))
       (goto-char place)
       (search-backward-regexp ".")	; to set the match-data.
       (point))))

1078 1079 1080 1081 1082 1083
(defun c-clear-char-property-with-value-function (from to property value)
  "Remove all text-properties PROPERTY from the region (FROM, TO)
which have the value VALUE, as tested by `equal'.  These
properties are assumed to be over individual characters, having
been put there by c-put-char-property.  POINT remains unchanged."
  (let ((place from) end-place)
Juanma Barranquero's avatar
Juanma Barranquero committed
1084
    (while			  ; loop round occurrences of (PROPERTY VALUE)
1085 1086 1087 1088 1089 1090 1091 1092
	(progn
	  (while	   ; loop round changes in PROPERTY till we find VALUE
	      (and
	       (< place to)
	       (not (equal (get-text-property place property) value)))
	    (setq place (next-single-property-change place property nil to)))
	  (< place to))
      (setq end-place (next-single-property-change place property nil to))
Alan Mackenzie's avatar
Alan Mackenzie committed
1093
      (remove-text-properties place end-place (cons property nil))
1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110
      ;; Do we have to do anything with stickiness here?
      (setq place end-place))))

(defmacro c-clear-char-property-with-value (from to property value)
  "Remove all text-properties PROPERTY from the region [FROM, TO)
which have the value VALUE, as tested by `equal'.  These
properties are assumed to be over individual characters, having
been put there by c-put-char-property.  POINT remains unchanged."
  (if c-use-extents
    ;; XEmacs
      `(let ((-property- ,property))
	 (map-extents (lambda (ext val)
			(if (equal (extent-property ext -property-) val)
			    (delete-extent ext)))
		      nil ,from ,to ,value nil -property-))
  ;; Gnu Emacs
    `(c-clear-char-property-with-value-function ,from ,to ,property ,value)))
1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139

;; Macros to put overlays (Emacs) or extents (XEmacs) on buffer text.
;; For our purposes, these are characterized by being possible to
;; remove again without affecting the other text properties in the
;; buffer that got overridden when they were put.

(defmacro c-put-overlay (from to property value)
  ;; Put an overlay/extent covering the given range in the current
  ;; buffer.  It's currently undefined whether it's front/end sticky
  ;; or not.  The overlay/extent object is returned.
  (if (cc-bytecomp-fboundp 'make-overlay)
      ;; Emacs.
      `(let ((ol (make-overlay ,from ,to)))
	 (overlay-put ol ,property ,value)
	 ol)
    ;; XEmacs.
    `(let ((ext (make-extent ,from ,to)))
       (set-extent-property ext ,property ,value)
       ext)))

(defmacro c-delete-overlay (overlay)
  ;; Deletes an overlay/extent object previously retrieved using
  ;; `c-put-overlay'.
  (if (cc-bytecomp-fboundp 'make-overlay)
      ;; Emacs.
      `(delete-overlay ,overlay)
    ;; XEmacs.
    `(delete-extent ,overlay)))

1140

1141
;; Make edebug understand the macros.
1142 1143
;(eval-after-load "edebug" ; 2006-07-09: def-edebug-spec is now in subr.el.
;  '(progn
1144
(def-edebug-spec cc-eval-when-compile (&rest def-form))
1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170
(def-edebug-spec c-point t)
(def-edebug-spec c-set-region-active t)
(def-edebug-spec c-safe t)
(def-edebug-spec c-save-buffer-state let*)
(def-edebug-spec c-tentative-buffer-changes t)
(def-edebug-spec c-forward-syntactic-ws t)
(def-edebug-spec c-backward-syntactic-ws t)
(def-edebug-spec c-forward-sexp t)
(def-edebug-spec c-backward-sexp t)
(def-edebug-spec c-up-list-forward t)
(def-edebug-spec c-up-list-backward t)
(def-edebug-spec c-down-list-forward t)
(def-edebug-spec c-down-list-backward t)
(def-edebug-spec c-add-syntax t)
(def-edebug-spec c-add-class-syntax t)
(def-edebug-spec c-benign-error t)
(def-edebug-spec c-with-syntax-table t)
(def-edebug-spec c-skip-ws-forward t)
(def-edebug-spec c-skip-ws-backward t)
(def-edebug-spec c-major-mode-is t)
(def-edebug-spec c-put-char-property t)
(def-edebug-spec c-get-char-property t)
(def-edebug-spec c-clear-char-property t)
(def-edebug-spec c-clear-char-properties t)
(def-edebug-spec c-put-overlay t)
(def-edebug-spec c-delete-overlay t) ;))
1171

1172 1173

;;; Functions.
Gerd Moellmann's avatar
Gerd Moellmann committed
1174 1175 1176 1177 1178 1179

;; Note: All these after the macros, to be on safe side in avoiding
;; bugs where macros are defined too late.  These bugs often only show
;; when the files are compiled in a certain order within the same
;; session.

Gerd Moellmann's avatar
Gerd Moellmann committed
1180 1181
(defsubst c-end-of-defun-1 ()
  ;; Replacement for end-of-defun that use c-beginning-of-defun-1.
1182 1183 1184 1185 1186 1187 1188 1189 1190 1191
  (let ((start (point)))
    ;; Skip forward into the next defun block. Don't bother to avoid
    ;; comments, literals etc, since beginning-of-defun doesn't do that
    ;; anyway.
    (skip-chars-forward "^}")
    (c-beginning-of-defun-1)
    (if (eq (char-after) ?{)
	(c-forward-sexp))
    (if (< (point) start)
	(goto-char (point-max)))))
Richard M. Stallman's avatar
Richard M. Stallman committed
1192

1193
(defconst c-<-as-paren-syntax '(4 . ?>))
1194
(put 'c-<-as-paren-syntax 'syntax-table c-<-as-paren-syntax)
1195 1196

(defsubst c-mark-<-as-paren (pos)
1197 1198
  ;; Mark the "<" character at POS as a template opener using the
  ;; `syntax-table' property via the `category' property.
1199
  ;;
1200 1201 1202 1203 1204
  ;; This function does a hidden buffer change.  Note that we use
  ;; indirection through the `category' text property.  This allows us to
  ;; toggle the property in all template brackets simultaneously and
  ;; cheaply.  We use this, for instance, in `c-parse-state'.
  (c-put-char-property pos 'category 'c-<-as-paren-syntax))
1205 1206

(defconst c->-as-paren-syntax '(5 . ?<))
1207
(put 'c->-as-paren-syntax 'syntax-table c->-as-paren-syntax)
1208 1209 1210

(defsubst c-mark->-as-paren (pos)
  ;; Mark the ">" character at POS as an sexp list closer using the
1211 1212
  ;; syntax-table property.
  ;;
1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260
  ;; This function does a hidden buffer change.  Note that we use
  ;; indirection through the `category' text property.  This allows us to
  ;; toggle the property in all template brackets simultaneously and
  ;; cheaply.  We use this, for instance, in `c-parse-state'.
  (c-put-char-property pos 'category 'c->-as-paren-syntax))

(defsubst c-unmark-<->-as-paren (pos)
  ;; Unmark the "<" or "<" character at POS as an sexp list opener using
  ;; the syntax-table property indirectly through the `category' text
  ;; property.
  ;;
  ;; This function does a hidden buffer change.  Note that we use
  ;; indirection through the `category' text property.  This allows us to
  ;; toggle the property in all template brackets simultaneously and
  ;; cheaply.  We use this, for instance, in `c-parse-state'.
  (c-clear-char-property pos 'category))

(defsubst c-suppress-<->-as-parens ()
  ;; Suppress the syntactic effect of all marked < and > as parens.  Note
  ;; that this effect is NOT buffer local.  You should probably not use
  ;; this directly, but only through the macro
  ;; `c-with-<->-as-parens-suppressed'
  (put 'c-<-as-paren-syntax 'syntax-table nil)
  (put 'c->-as-paren-syntax 'syntax-table nil))

(defsubst c-restore-<->-as-parens ()
  ;; Restore the syntactic effect of all marked <s and >s as parens.  This
  ;; has no effect on unmarked <s and >s
  (put 'c-<-as-paren-syntax 'syntax-table c-<-as-paren-syntax)
  (put 'c->-as-paren-syntax 'syntax-table c->-as-paren-syntax))

(defmacro c-with-<->-as-parens-suppressed (&rest forms)
  ;; Like progn, except that the paren property is suppressed on all
  ;; template brackets whilst they are running.  This macro does a hidden
  ;; buffer change.
  `(unwind-protect
       (progn
	 (c-suppress-<->-as-parens)
	 ,@forms)
     (c-restore-<->-as-parens)))

;;;;;;;;;;;;;;;

(defconst c-cpp-delimiter '(14)) ; generic comment syntax
;; This is the value of the `category' text property placed on every #
;; which introduces a CPP construct and every EOL (or EOB, or character
;; preceding //, etc.) which terminates it.  We can instantly "comment
;; out" all CPP constructs by giving `c-cpp-delimiter' a syntax-table
Paul Eggert's avatar
Paul Eggert committed
1261
;; property '(14) (generic comment delimiter).
1262 1263 1264 1265
(defmacro c-set-cpp-delimiters (beg end)
  ;; This macro does a hidden buffer change.
  `(progn
     (c-put-char-property ,beg 'category 'c-cpp-delimiter)
1266 1267
     (if (< ,end (point-max))
	 (c-put-char-property ,end 'category 'c-cpp-delimiter))))
1268 1269 1270 1271
(defmacro c-clear-cpp-delimiters (beg end)
  ;; This macro does a hidden buffer change.
  `(progn
     (c-clear-char-property ,beg 'category)
1272 1273
     (if (< ,end (point-max))
	 (c-clear-char-property ,end 'category))))
1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299

(defsubst c-comment-out-cpps ()
  ;; Render all preprocessor constructs syntactically commented out.
  (put 'c-cpp-delimiter 'syntax-table c-cpp-delimiter))
(defsubst c-uncomment-out-cpps ()
  ;; Restore the syntactic visibility of preprocessor constructs.
  (put 'c-cpp-delimiter 'syntax-table nil))

(defmacro c-with-cpps-commented-out (&rest forms)
  ;; Execute FORMS... whilst the syntactic effect of all characters in
  ;; all CPP regions is suppressed.  In particular, this is to suppress
  ;; the syntactic significance of parens/braces/brackets to functions
  ;; such as `scan-lists' and `parse-partial-sexp'.
  `(unwind-protect
       (c-save-buffer-state ()
	   (c-comment-out-cpps)
	   ,@forms)
     (c-save-buffer-state ()
       (c-uncomment-out-cpps))))

(defmacro c-with-all-but-one-cpps-commented-out (beg end &rest forms)
  ;; Execute FORMS... whilst the syntactic effect of all characters in
  ;; every CPP region APART FROM THE ONE BETWEEN BEG and END is
  ;; suppressed.
  `(unwind-protect
       (c-save-buffer-state ()
1300 1301 1302
	 (save-restriction
	   (widen)
	   (c-clear-cpp-delimiters ,beg ,end))
1303 1304
	 ,`(c-with-cpps-commented-out ,@forms))
     (c-save-buffer-state ()
1305 1306 1307
       (save-restriction
	 (widen)
	 (c-set-cpp-delimiters ,beg ,end)))))
1308

Richard M. Stallman's avatar
Richard M. Stallman committed
1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322
(defsubst c-intersect-lists (list alist)
  ;; return the element of ALIST that matches the first element found
  ;; in LIST.  Uses assq.
  (let (match)
    (while (and list
		(not (setq match (assq (car list) alist))))
      (setq list (cdr list)))
    match))

(defsubst c-lookup-lists (list alist1 alist2)
  ;; first, find the first entry from LIST that is present in ALIST1,
  ;; then find the entry in ALIST2 for that entry.
  (assq (car (c-intersect-lists list alist1)) alist2))

1323 1324 1325
(defsubst c-langelem-sym (langelem)
  "Return the syntactic symbol in LANGELEM.

1326 1327 1328
LANGELEM is either a cons cell on the \"old\" form given as the first
argument to lineup functions or a syntactic element on the \"new\"
form as used in `c-syntactic-element'."
1329 1330 1331
  (car langelem))

(defsubst c-langelem-pos (langelem)
1332
  "Return the anchor position in LANGELEM, or nil if there is none.