aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSimon Marshall1996-01-25 10:38:39 +0000
committerSimon Marshall1996-01-25 10:38:39 +0000
commita2b8e66b8e36df71ab13f50d644bb53a819da8f7 (patch)
tree88e1af51dba123c8ac38fee12db8ceda4fc53cdc
parent70a302f866d03f4cae73c2442f57e5eed13c6402 (diff)
downloademacs-a2b8e66b8e36df71ab13f50d644bb53a819da8f7.tar.gz
emacs-a2b8e66b8e36df71ab13f50d644bb53a819da8f7.zip
Support for buffer local fontification functions.
Global Font Lock mode. Support for `eval' keywords.
-rw-r--r--lisp/font-lock.el373
1 files changed, 286 insertions, 87 deletions
diff --git a/lisp/font-lock.el b/lisp/font-lock.el
index ff0ede34e6e..e02ffb11d75 100644
--- a/lisp/font-lock.el
+++ b/lisp/font-lock.el
@@ -1,6 +1,6 @@
1;;; font-lock.el --- Electric font lock mode 1;;; font-lock.el --- Electric font lock mode
2 2
3;; Copyright (C) 1992, 1993, 1994, 1995 Free Software Foundation, Inc. 3;; Copyright (C) 1992, 1993, 1994, 1995, 1996 Free Software Foundation, Inc.
4 4
5;; Author: jwz, then rms, then sm <simon@gnu.ai.mit.edu> 5;; Author: jwz, then rms, then sm <simon@gnu.ai.mit.edu>
6;; Maintainer: FSF 6;; Maintainer: FSF
@@ -36,10 +36,14 @@
36;; When this minor mode is on, the faces of the current line are updated with 36;; When this minor mode is on, the faces of the current line are updated with
37;; every insertion or deletion. 37;; every insertion or deletion.
38;; 38;;
39;; To turn Font Lock mode on automatically, add this to your .emacs file: 39;; To turn Font Lock mode on automatically, add this to your ~/.emacs file:
40;; 40;;
41;; (add-hook 'emacs-lisp-mode-hook 'turn-on-font-lock) 41;; (add-hook 'emacs-lisp-mode-hook 'turn-on-font-lock)
42;; 42;;
43;; Or if you want to turn Font Lock mode on in many modes:
44;;
45;; (global-font-lock-mode t)
46;;
43;; Fontification for a particular mode may be available in a number of levels 47;; Fontification for a particular mode may be available in a number of levels
44;; of decoration. The higher the level, the more decoration, but the more time 48;; of decoration. The higher the level, the more decoration, but the more time
45;; it takes to fontify. See the variable `font-lock-maximum-decoration', and 49;; it takes to fontify. See the variable `font-lock-maximum-decoration', and
@@ -267,7 +271,8 @@ The value should be like the `cdr' of an item in `font-lock-defaults-alist'.")
267 "Alist of default major mode and Font Lock defaults. 271 "Alist of default major mode and Font Lock defaults.
268Each item should be a list of the form: 272Each item should be a list of the form:
269 273
270 (MAJOR-MODE . (KEYWORDS KEYWORDS-ONLY CASE-FOLD SYNTAX-ALIST SYNTAX-BEGIN)) 274 (MAJOR-MODE . (KEYWORDS KEYWORDS-ONLY CASE-FOLD SYNTAX-ALIST SYNTAX-BEGIN
275 LOCAL-FONTIFICATION))
271 276
272where MAJOR-MODE is a symbol. KEYWORDS may be a symbol (a variable or function 277where MAJOR-MODE is a symbol. KEYWORDS may be a symbol (a variable or function
273whose value is the keywords to use for fontification) or a list of symbols. 278whose value is the keywords to use for fontification) or a list of symbols.
@@ -288,7 +293,18 @@ is used as a position outside of a syntactic block, in the worst case.
288These item elements are used by Font Lock mode to set the variables 293These item elements are used by Font Lock mode to set the variables
289`font-lock-keywords', `font-lock-keywords-only', 294`font-lock-keywords', `font-lock-keywords-only',
290`font-lock-keywords-case-fold-search', `font-lock-syntax-table' and 295`font-lock-keywords-case-fold-search', `font-lock-syntax-table' and
291`font-lock-beginning-of-syntax-function', respectively.") 296`font-lock-beginning-of-syntax-function', respectively.
297
298LOCAL-FONTIFICATION should be of the form:
299
300 (FONTIFY-BUFFER-FUNCTION UNFONTIFY-BUFFER-FUNCTION FONTIFY-REGION-FUNCTION
301 UNFONTIFY-REGION-FUNCTION INHIBIT-THING-LOCK)
302
303where the first four elements are function names used to set the variables
304`font-lock-fontify-buffer-function', `font-lock-unfontify-buffer-function',
305`font-lock-fontify-region-function' and `font-lock-unfontify-region-function'.
306INHIBIT-THING-LOCK is a list of mode names whose modes should not be turned on.
307It is used to set the variable `font-lock-inhibit-thing-lock'.")
292 308
293(defvar font-lock-keywords-only nil 309(defvar font-lock-keywords-only nil
294 "*Non-nil means Font Lock should not fontify comments or strings. 310 "*Non-nil means Font Lock should not fontify comments or strings.
@@ -310,6 +326,31 @@ This is normally set via `font-lock-defaults'.")
310If this is nil, the beginning of the buffer is used (in the worst case). 326If this is nil, the beginning of the buffer is used (in the worst case).
311This is normally set via `font-lock-defaults'.") 327This is normally set via `font-lock-defaults'.")
312 328
329(defvar font-lock-fontify-buffer-function 'font-lock-default-fontify-buffer
330 "Function to use for fontifying the buffer.
331This is normally set via `font-lock-defaults'.")
332
333(defvar font-lock-unfontify-buffer-function 'font-lock-default-unfontify-buffer
334 "Function to use for unfontifying the buffer.
335This is used when turning off Font Lock mode.
336This is normally set via `font-lock-defaults'.")
337
338(defvar font-lock-fontify-region-function 'font-lock-default-fontify-region
339 "Function to use for fontifying a region.
340It should take two args, the beginning and end of the region, and an optional
341third arg VERBOSE. If non-nil, the function should print status messages.
342This is normally set via `font-lock-defaults'.")
343
344(defvar font-lock-unfontify-region-function 'font-lock-default-unfontify-region
345 "Function to use for unfontifying a region.
346It should take two args, the beginning and end of the region.
347This is normally set via `font-lock-defaults'.")
348
349(defvar font-lock-inhibit-thing-lock nil
350 "List of Font Lock mode related modes that should not be turned on.
351Currently, valid mode names as `fast-lock-mode' and `lazy-lock-mode'.
352This is normally set via `font-lock-defaults'.")
353
313;; These record the parse state at a particular position, always the start of a 354;; These record the parse state at a particular position, always the start of a
314;; line. Used to make `font-lock-fontify-syntactically-region' faster. 355;; line. Used to make `font-lock-fontify-syntactically-region' faster.
315(defvar font-lock-cache-position nil) 356(defvar font-lock-cache-position nil)
@@ -325,7 +366,7 @@ This is normally set via `font-lock-defaults'.")
325(defvar font-lock-mode-hook nil 366(defvar font-lock-mode-hook nil
326 "Function or functions to run on entry to Font Lock mode.") 367 "Function or functions to run on entry to Font Lock mode.")
327 368
328;; User functions. 369;; User commands.
329 370
330;;;###autoload 371;;;###autoload
331(defun font-lock-mode (&optional arg) 372(defun font-lock-mode (&optional arg)
@@ -348,6 +389,12 @@ Or for any visited file with the following in your ~/.emacs:
348 389
349 (add-hook 'find-file-hooks 'turn-on-font-lock) 390 (add-hook 'find-file-hooks 'turn-on-font-lock)
350 391
392Alternatively, you can use Global Font Lock mode to automagically turn on Font
393Lock mode in buffers whose major mode supports it, or in buffers whose major
394mode is one of `font-lock-global-modes'. For example, put in your ~/.emacs:
395
396 (global-font-lock-mode t)
397
351The default Font Lock mode faces and their attributes are defined in the 398The default Font Lock mode faces and their attributes are defined in the
352variable `font-lock-face-attributes', and Font Lock mode default settings in 399variable `font-lock-face-attributes', and Font Lock mode default settings in
353the variable `font-lock-defaults-alist'. You can set your own default settings 400the variable `font-lock-defaults-alist'. You can set your own default settings
@@ -364,15 +411,17 @@ size, you can use \\[font-lock-fontify-buffer].
364To fontify a window, perhaps because modification on the current line caused 411To fontify a window, perhaps because modification on the current line caused
365syntactic change on other lines, you can use \\[font-lock-fontify-window]." 412syntactic change on other lines, you can use \\[font-lock-fontify-window]."
366 (interactive "P") 413 (interactive "P")
367 (let ((on-p (if arg (> (prefix-numeric-value arg) 0) (not font-lock-mode))) 414 ;; Don't turn on Font Lock mode if we don't have a display (we're running a
415 ;; batch job) or if the buffer is invisible (the name starts with a space).
416 (let ((on-p (and (not noninteractive)
417 (not (eq (aref (buffer-name) 0) ?\ ))
418 (if arg
419 (> (prefix-numeric-value arg) 0)
420 (not font-lock-mode))))
368 (maximum-size (if (not (consp font-lock-maximum-size)) 421 (maximum-size (if (not (consp font-lock-maximum-size))
369 font-lock-maximum-size 422 font-lock-maximum-size
370 (cdr (or (assq major-mode font-lock-maximum-size) 423 (cdr (or (assq major-mode font-lock-maximum-size)
371 (assq t font-lock-maximum-size)))))) 424 (assq t font-lock-maximum-size))))))
372 ;; Don't turn on Font Lock mode if we don't have a display (we're running a
373 ;; batch job) or if the buffer is invisible (the name starts with a space).
374 (if (or noninteractive (eq (aref (buffer-name) 0) ?\ ))
375 (setq on-p nil))
376 (if (not on-p) 425 (if (not on-p)
377 (remove-hook 'after-change-functions 'font-lock-after-change-function 426 (remove-hook 'after-change-functions 'font-lock-after-change-function
378 t) 427 t)
@@ -382,46 +431,153 @@ syntactic change on other lines, you can use \\[font-lock-fontify-window]."
382 (set (make-local-variable 'font-lock-mode) on-p) 431 (set (make-local-variable 'font-lock-mode) on-p)
383 (cond (on-p 432 (cond (on-p
384 (font-lock-set-defaults) 433 (font-lock-set-defaults)
434 ;; If buffer is reverted, must clean up the state.
385 (make-local-hook 'before-revert-hook) 435 (make-local-hook 'before-revert-hook)
386 (make-local-hook 'after-revert-hook) 436 (make-local-hook 'after-revert-hook)
387 ;; If buffer is reverted, must clean up the state.
388 (add-hook 'before-revert-hook 'font-lock-revert-setup nil t) 437 (add-hook 'before-revert-hook 'font-lock-revert-setup nil t)
389 (add-hook 'after-revert-hook 'font-lock-revert-cleanup nil t) 438 (add-hook 'after-revert-hook 'font-lock-revert-cleanup nil t)
390 (run-hooks 'font-lock-mode-hook) 439 (run-hooks 'font-lock-mode-hook)
391 (cond (font-lock-fontified 440 (cond (font-lock-fontified
392 nil) 441 nil)
393 ((or (null maximum-size) (<= (buffer-size) maximum-size)) 442 ((or (null maximum-size) (<= (buffer-size) maximum-size)
443 (not (eq font-lock-fontify-buffer-function
444 (default-value
445 'font-lock-fontify-buffer-function))))
394 (font-lock-fontify-buffer)) 446 (font-lock-fontify-buffer))
395 (font-lock-verbose 447 (font-lock-verbose
396 (message "Fontifying %s... buffer too big." (buffer-name))))) 448 (message "Fontifying %s... buffer too big." (buffer-name)))))
397 (font-lock-fontified 449 (font-lock-fontified
398 (setq font-lock-fontified nil) 450 (font-lock-unfontify-buffer)
399 (remove-hook 'before-revert-hook 'font-lock-revert-setup t) 451 (remove-hook 'before-revert-hook 'font-lock-revert-setup t)
400 (remove-hook 'after-revert-hook 'font-lock-revert-cleanup t) 452 (remove-hook 'after-revert-hook 'font-lock-revert-cleanup t)
401 ;; Make sure we unfontify etc. in the whole buffer. 453 (font-lock-thing-lock-cleanup)
402 (save-restriction 454 (font-lock-unset-defaults))
403 (widen)
404 (font-lock-unfontify-region (point-min) (point-max))
405 (font-lock-thing-lock-cleanup)))
406 (t 455 (t
407 (remove-hook 'before-revert-hook 'font-lock-revert-setup t) 456 (remove-hook 'before-revert-hook 'font-lock-revert-setup t)
408 (remove-hook 'after-revert-hook 'font-lock-revert-cleanup t) 457 (remove-hook 'after-revert-hook 'font-lock-revert-cleanup t)
409 (font-lock-thing-lock-cleanup))) 458 (font-lock-thing-lock-cleanup)
459 (font-lock-unset-defaults)))
410 (force-mode-line-update))) 460 (force-mode-line-update)))
411 461
412;;;###autoload 462;;;###autoload
413(defun turn-on-font-lock () 463(defun turn-on-font-lock ()
414 "Turn on Font Lock mode, if the terminal can display it." 464 "Turn on Font Lock mode, if the terminal can display it."
415 (if window-system 465 (if window-system (font-lock-mode t)))
416 (font-lock-mode 1))) 466
467;; Code for Global Font Lock mode.
468
469;; A few people have hassled in the past for a way to make it easier to turn on
470;; Font Lock mode, perhaps the same way hilit19.el/hl319.el does. I've always
471;; balked at that way, as I see it as just re-moulding the same problem in
472;; another form. That is; some person would still have to keep track of which
473;; modes (which may not even be distributed with Emacs) support Font Lock mode.
474;; The list would always be out of date. And that person might have to be me.
475
476;; In the latest of these discussions the following hack came to mind. It is a
477;; gross hack, but it generally works. We use the convention that major modes
478;; start by calling the function `kill-all-local-variables', which in turn runs
479;; functions on the hook variable `change-major-mode-hook'. We attach our
480;; function `font-lock-change-major-mode' to that hook. Of course, when this
481;; hook is run, the major mode is in the process of being changed and we do not
482;; know what the final major mode will be. So, `font-lock-change-major-mode'
483;; only (a) notes the name of the current buffer, and (b) adds our function
484;; `turn-on-font-lock-if-supported' to the hook variable `post-command-hook'.
485;; By the time the functions on `post-command-hook' are run, the new major mode
486;; is assumed to be in place.
487
488;; Naturally this requires that (a) major modes run `kill-all-local-variables',
489;; as they are supposed to do, and (b) the major mode is in place after the
490;; command that ran `kill-all-local-variables' has finished. Arguably, any
491;; major mode that does not follow the convension (a) is broken, and I can't
492;; think of any reason why (b) would not be met. I don't know of any major
493;; modes that do not follow the convension (a), but I'm sure there are some
494;; obscure ones out there somewhere. Even if it works, it is still not clean.
495
496;; Probably the cleanest solution is to have each major mode function run some
497;; hook, e.g., `major-mode-hook', but maybe implementing that change is
498;; impractical. I am personally against making `setq' a macro or be advised
499;; (space'n'speed), or have a special function such as `set-major-mode' (a
500;; `major-mode-hook' is simpler), but maybe someone can come up with another
501;; solution? --sm.
502
503;;;###autoload
504(defvar font-lock-global-modes t
505 "*List of modes for which Font Lock mode is automatically turned on.
506Global Font Lock mode is controlled by the `global-font-lock-mode' command.
507If nil, means no modes have Font Lock mode automatically turned on.
508If t, all modes that support Font Lock mode have it automatically turned on.
509If a list, each element should be a major mode symbol name such as `c-mode'.
510Font Lock is automatically turned on if the buffer major mode supports it and
511is in this list. The sense of the list is negated if it begins with `not'.")
512
513;;;###autoload
514(defun global-font-lock-mode (&optional arg)
515 "Toggle Global Font Lock mode.
516With arg, turn Global Font Lock mode on if and only if arg is positive.
517
518When Global Font Lock mode is enabled, Font Lock mode is automagically
519turned on in a buffer if its major mode is one of `font-lock-global-modes'."
520 (interactive "P")
521 (if (if arg
522 (<= (prefix-numeric-value arg) 0)
523 (memq 'font-lock-change-major-mode change-major-mode-hook))
524 (remove-hook 'change-major-mode-hook 'font-lock-change-major-mode)
525 (add-hook 'change-major-mode-hook 'font-lock-change-major-mode)
526 (add-hook 'post-command-hook 'turn-on-font-lock-if-supported)
527 (setq font-lock-cache-buffers (buffer-list))))
528
529(defvar font-lock-cache-buffers nil) ; For remembering buffers.
530(defvar change-major-mode-hook nil) ; Make sure it's not void.
531
532(defun font-lock-change-major-mode ()
533 ;; Gross hack warning: Delicate readers should avert eyes now.
534 ;; Something is running `kill-all-local-variables', which generally means
535 ;; the major mode is being changed. Run `turn-on-font-lock-if-supported'
536 ;; after the current command has finished.
537 (add-hook 'post-command-hook 'turn-on-font-lock-if-supported)
538 (add-to-list 'font-lock-cache-buffers (current-buffer)))
539
540(defun turn-on-font-lock-if-supported ()
541 ;; Gross hack warning: Delicate readers should avert eyes now.
542 ;; Turn on Font Lock mode if (a) it's not already on, (b) the major mode
543 ;; supports Font Lock mode, and (c) it's one of `font-lock-global-modes'.
544 (remove-hook 'post-command-hook 'turn-on-font-lock-if-supported)
545 (while font-lock-cache-buffers
546 (if (buffer-name (car font-lock-cache-buffers))
547 (save-excursion
548 (set-buffer (car font-lock-cache-buffers))
549 (if (and (not font-lock-mode)
550 (or font-lock-defaults
551 (assq major-mode font-lock-defaults-alist))
552 (or (eq font-lock-global-modes t)
553 (if (eq (car-safe font-lock-global-modes) 'not)
554 (not (memq major-mode (cdr font-lock-global-modes)))
555 (memq major-mode font-lock-global-modes))))
556 (turn-on-font-lock))))
557 (setq font-lock-cache-buffers (cdr font-lock-cache-buffers))))
558
559;; End of Global Font Lock mode.
560
561;; Fontification functions.
417 562
418;;;###autoload 563;;;###autoload
419(defun font-lock-fontify-buffer () 564(defun font-lock-fontify-buffer ()
420 "Fontify the current buffer the way `font-lock-mode' would." 565 "Fontify the current buffer the way `font-lock-mode' would."
421 (interactive) 566 (interactive)
422 (let ((verbose (and (or font-lock-verbose (interactive-p)) 567 (let ((font-lock-verbose (or font-lock-verbose (interactive-p))))
423 (not (zerop (buffer-size)))))) 568 (funcall font-lock-fontify-buffer-function)))
424 (set (make-local-variable 'font-lock-fontified) nil) 569
570(defun font-lock-unfontify-buffer ()
571 (funcall font-lock-unfontify-buffer-function))
572
573(defun font-lock-fontify-region (beg end &optional loudly)
574 (funcall font-lock-fontify-region-function beg end loudly))
575
576(defun font-lock-unfontify-region (beg end)
577 (funcall font-lock-unfontify-region-function beg end))
578
579(defun font-lock-default-fontify-buffer ()
580 (let ((verbose (and font-lock-verbose (> (buffer-size) 0))))
425 (if verbose (message "Fontifying %s..." (buffer-name))) 581 (if verbose (message "Fontifying %s..." (buffer-name)))
426 ;; Make sure we have the right `font-lock-keywords' etc. 582 ;; Make sure we have the right `font-lock-keywords' etc.
427 (if (not font-lock-mode) (font-lock-set-defaults)) 583 (if (not font-lock-mode) (font-lock-set-defaults))
@@ -431,6 +587,7 @@ syntactic change on other lines, you can use \\[font-lock-fontify-window]."
431 (condition-case nil 587 (condition-case nil
432 (save-excursion 588 (save-excursion
433 (save-match-data 589 (save-match-data
590 (setq font-lock-fontified nil)
434 (font-lock-fontify-region (point-min) (point-max) verbose) 591 (font-lock-fontify-region (point-min) (point-max) verbose)
435 (setq font-lock-fontified t))) 592 (setq font-lock-fontified t)))
436 ;; We don't restore the old fontification, so it's best to unfontify. 593 ;; We don't restore the old fontification, so it's best to unfontify.
@@ -439,22 +596,16 @@ syntactic change on other lines, you can use \\[font-lock-fontify-window]."
439 (if font-lock-fontified "done" "aborted"))) 596 (if font-lock-fontified "done" "aborted")))
440 (font-lock-after-fontify-buffer)))) 597 (font-lock-after-fontify-buffer))))
441 598
442(defun font-lock-fontify-window () 599(defun font-lock-default-unfontify-buffer ()
443 "Fontify the current window the way `font-lock-mode' would." 600 (save-restriction
444 (interactive) 601 (widen)
445 (let ((font-lock-beginning-of-syntax-function nil)) 602 (font-lock-unfontify-region (point-min) (point-max))
446 (save-excursion 603 (setq font-lock-fontified nil)))
447 (save-match-data
448 (font-lock-fontify-region (window-start) (window-end))))))
449
450(define-key global-map [?\C-\S-l] 'font-lock-fontify-window)
451
452;; Fontification functions.
453 604
454;; We use this wrapper. However, `font-lock-fontify-region' used to be the 605;; We use this wrapper. However, `font-lock-fontify-region' used to be the
455;; name used for `font-lock-fontify-syntactically-region', so a change isn't 606;; name used for `font-lock-fontify-syntactically-region', so a change isn't
456;; back-compatible. But you shouldn't be calling these directly, should you? 607;; back-compatible. But you shouldn't be calling these directly, should you?
457(defun font-lock-fontify-region (beg end &optional loudly) 608(defun font-lock-default-fontify-region (beg end loudly)
458 (let ((modified (buffer-modified-p)) 609 (let ((modified (buffer-modified-p))
459 (buffer-undo-list t) (inhibit-read-only t) 610 (buffer-undo-list t) (inhibit-read-only t)
460 (old-syntax-table (syntax-table)) 611 (old-syntax-table (syntax-table))
@@ -481,7 +632,7 @@ syntactic change on other lines, you can use \\[font-lock-fontify-window]."
481; (or (nth 4 state) (nth 7 state)))) 632; (or (nth 4 state) (nth 7 state))))
482; (font-lock-fontify-keywords-region beg end)) 633; (font-lock-fontify-keywords-region beg end))
483 634
484(defun font-lock-unfontify-region (beg end) 635(defun font-lock-default-unfontify-region (beg end)
485 (let ((modified (buffer-modified-p)) 636 (let ((modified (buffer-modified-p))
486 (buffer-undo-list t) (inhibit-read-only t) 637 (buffer-undo-list t) (inhibit-read-only t)
487 before-change-functions after-change-functions 638 before-change-functions after-change-functions
@@ -497,6 +648,18 @@ syntactic change on other lines, you can use \\[font-lock-fontify-window]."
497 (font-lock-fontify-region 648 (font-lock-fontify-region
498 (progn (goto-char beg) (beginning-of-line) (point)) 649 (progn (goto-char beg) (beginning-of-line) (point))
499 (progn (goto-char end) (forward-line 1) (point)))))) 650 (progn (goto-char end) (forward-line 1) (point))))))
651
652(defun font-lock-fontify-window ()
653 "Fontify the current window the way `font-lock-mode' would."
654 (interactive)
655 (let ((font-lock-beginning-of-syntax-function nil))
656 (save-excursion
657 (save-match-data
658 (condition-case error-data
659 (font-lock-fontify-region (window-start) (window-end))
660 (error (message "Fontifying window... %s" error-data)))))))
661
662(define-key ctl-x-map "w" 'font-lock-fontify-window)
500 663
501;; Syntactic fontification functions. 664;; Syntactic fontification functions.
502 665
@@ -586,8 +749,7 @@ START should be at the beginning of a line."
586 ;; so go back to the real end of the comment. 749 ;; so go back to the real end of the comment.
587 (skip-chars-backward " \t")) 750 (skip-chars-backward " \t"))
588 (error (goto-char end)))) 751 (error (goto-char end))))
589 (put-text-property beg (point) 'face 752 (put-text-property beg (point) 'face font-lock-comment-face)
590 font-lock-comment-face)
591 (setq state (parse-partial-sexp here (point) nil nil state))) 753 (setq state (parse-partial-sexp here (point) nil nil state)))
592 (if (nth 3 state) 754 (if (nth 3 state)
593 ;; 755 ;;
@@ -800,22 +962,23 @@ START should be at the beginning of a line."
800 ;; is the (MATCHER HIGHLIGHT ...) shown in the variable's doc string. 962 ;; is the (MATCHER HIGHLIGHT ...) shown in the variable's doc string.
801 (let ((keywords (or keywords font-lock-keywords))) 963 (let ((keywords (or keywords font-lock-keywords)))
802 (setq font-lock-keywords 964 (setq font-lock-keywords
803 (if (eq (car-safe keywords) t) 965 (if (eq (car-safe keywords) t)
804 keywords 966 keywords
805 (cons t 967 (cons t (mapcar 'font-lock-compile-keyword keywords))))))
806 (mapcar 968
807 (function (lambda (item) 969(defun font-lock-compile-keyword (keyword)
808 (cond ((nlistp item) 970 (cond ((nlistp keyword) ; Just MATCHER
809 (list item '(0 font-lock-keyword-face))) 971 (list keyword '(0 font-lock-keyword-face)))
810 ((numberp (cdr item)) 972 ((eq (car keyword) 'eval) ; Specified (eval . FORM)
811 (list (car item) (list (cdr item) 'font-lock-keyword-face))) 973 (font-lock-compile-keyword (eval (cdr keyword))))
812 ((symbolp (cdr item)) 974 ((numberp (cdr keyword)) ; Specified (MATCHER . MATCH)
813 (list (car item) (list 0 (cdr item)))) 975 (list (car keyword) (list (cdr keyword) 'font-lock-keyword-face)))
814 ((nlistp (nth 1 item)) 976 ((symbolp (cdr keyword)) ; Specified (MATCHER . FACENAME)
815 (list (car item) (cdr item))) 977 (list (car keyword) (list 0 (cdr keyword))))
816 (t 978 ((nlistp (nth 1 keyword)) ; Specified (MATCHER . HIGHLIGHT)
817 item)))) 979 (list (car keyword) (cdr keyword)))
818 keywords)))))) 980 (t ; Hopefully (MATCHER HIGHLIGHT ...)
981 keyword)))
819 982
820(defun font-lock-choose-keywords (keywords level) 983(defun font-lock-choose-keywords (keywords level)
821 ;; Return LEVELth element of KEYWORDS. A LEVEL of nil is equal to a 984 ;; Return LEVELth element of KEYWORDS. A LEVEL of nil is equal to a
@@ -834,41 +997,77 @@ START should be at the beginning of a line."
834 997
835(defun font-lock-set-defaults () 998(defun font-lock-set-defaults ()
836 "Set fontification defaults appropriately for this mode. 999 "Set fontification defaults appropriately for this mode.
837Sets `font-lock-keywords', `font-lock-keywords-only', `font-lock-syntax-table', 1000Sets various variables using `font-lock-defaults' (or, if nil, using
838`font-lock-beginning-of-syntax-function' and 1001`font-lock-defaults-alist') and `font-lock-maximum-decoration'."
839`font-lock-keywords-case-fold-search' using `font-lock-defaults' (or, if nil,
840using `font-lock-defaults-alist') and `font-lock-maximum-decoration'."
841 ;; Set face defaults. 1002 ;; Set face defaults.
842 (font-lock-make-faces) 1003 (font-lock-make-faces)
843 ;; Set fontification defaults. 1004 ;; Set fontification defaults.
844 (or font-lock-keywords 1005 (make-local-variable 'font-lock-fontified)
845 (let* ((defaults (or font-lock-defaults 1006 (if font-lock-keywords
846 (cdr (assq major-mode font-lock-defaults-alist)))) 1007 nil
847 (keywords (font-lock-choose-keywords 1008 (let* ((defaults (or font-lock-defaults
848 (nth 0 defaults) font-lock-maximum-decoration))) 1009 (cdr (assq major-mode font-lock-defaults-alist))))
849 ;; Regexp fontification? 1010 (keywords (font-lock-choose-keywords
850 (setq font-lock-keywords (if (fboundp keywords) 1011 (nth 0 defaults) font-lock-maximum-decoration)))
851 (funcall keywords) 1012 ;; Regexp fontification?
852 (eval keywords))) 1013 (setq font-lock-keywords (if (fboundp keywords)
853 ;; Syntactic fontification? 1014 (funcall keywords)
854 (if (nth 1 defaults) 1015 (eval keywords)))
855 (set (make-local-variable 'font-lock-keywords-only) t)) 1016 ;; Syntactic fontification?
856 ;; Case fold during regexp fontification? 1017 (if (nth 1 defaults)
857 (if (nth 2 defaults) 1018 (set (make-local-variable 'font-lock-keywords-only) t))
858 (set (make-local-variable 'font-lock-keywords-case-fold-search) t)) 1019 ;; Case fold during regexp fontification?
859 ;; Syntax table for regexp and syntactic fontification? 1020 (if (nth 2 defaults)
860 (if (nth 3 defaults) 1021 (set (make-local-variable 'font-lock-keywords-case-fold-search) t))
861 (let ((slist (nth 3 defaults))) 1022 ;; Syntax table for regexp and syntactic fontification?
862 (set (make-local-variable 'font-lock-syntax-table) 1023 (if (nth 3 defaults)
863 (copy-syntax-table (syntax-table))) 1024 (let ((slist (nth 3 defaults)))
864 (while slist 1025 (set (make-local-variable 'font-lock-syntax-table)
865 (modify-syntax-entry (car (car slist)) (cdr (car slist)) 1026 (copy-syntax-table (syntax-table)))
866 font-lock-syntax-table) 1027 (while slist
867 (setq slist (cdr slist))))) 1028 (modify-syntax-entry (car (car slist)) (cdr (car slist))
868 ;; Syntax function for syntactic fontification? 1029 font-lock-syntax-table)
869 (if (nth 4 defaults) 1030 (setq slist (cdr slist)))))
870 (set (make-local-variable 'font-lock-beginning-of-syntax-function) 1031 ;; Syntax function for syntactic fontification?
871 (nth 4 defaults)))))) 1032 (if (nth 4 defaults)
1033 (set (make-local-variable 'font-lock-beginning-of-syntax-function)
1034 (nth 4 defaults)))
1035 ;; Local fontification?
1036 (if (nth 5 defaults)
1037 (let ((local (nth 5 defaults)))
1038 (if (nth 0 local)
1039 (set (make-local-variable 'font-lock-fontify-buffer-function)
1040 (nth 0 local)))
1041 (if (nth 1 local)
1042 (set (make-local-variable 'font-lock-unfontify-buffer-function)
1043 (nth 1 local)))
1044 (if (nth 2 local)
1045 (set (make-local-variable 'font-lock-fontify-region-function)
1046 (nth 2 local)))
1047 (if (nth 3 local)
1048 (set (make-local-variable 'font-lock-unfontify-region-function)
1049 (nth 3 local)))
1050 (if (nth 4 local)
1051 (set (make-local-variable 'font-lock-inhibit-thing-lock)
1052 (nth 4 local)))
1053 )))))
1054
1055(defun font-lock-unset-defaults ()
1056 "Unset fontification defaults. See `font-lock-set-defaults'."
1057 (setq font-lock-keywords nil
1058 font-lock-keywords-only nil
1059 font-lock-keywords-case-fold-search nil
1060 font-lock-syntax-table nil
1061 font-lock-beginning-of-syntax-function nil
1062 font-lock-fontify-buffer-function
1063 (default-value 'font-lock-fontify-buffer-function)
1064 font-lock-unfontify-buffer-function
1065 (default-value 'font-lock-unfontify-buffer-function)
1066 font-lock-fontify-region-function
1067 (default-value 'font-lock-fontify-region-function)
1068 font-lock-unfontify-region-function
1069 (default-value 'font-lock-unfontify-region-function)
1070 font-lock-inhibit-thing-lock nil))
872 1071
873;; Colour etc. support. 1072;; Colour etc. support.
874 1073