comparison lisp/font-lock.el @ 15492:348f0896b1e7

(font-lock-verbose): Default to 0. Doc fix. (font-lock-default-fontify-buffer): Interpret numeric value. (font-lock-fontified): Don't make it permanent-local. (font-lock-mode): Remove before-revert-hook and after-revert-hook code. Call font-lock-turn-on-thing-lock before font-lock-mode-hook. Doc fix. (font-lock-revert-setup, font-lock-revert-cleanup): Deleted. (font-lock-support-mode): New variable. (font-lock-turn-on-thing-lock): New function. Use it. (font-lock-turn-off-thing-lock): Renamed font-lock-thing-lock-cleanup. (font-lock-change-major-mode): Turn off Font Lock mode if it is on. Add font-lock-change-major-mode to change-major-mode-hook at top level.
author Richard M. Stallman <rms@gnu.org>
date Sun, 23 Jun 1996 18:36:01 +0000
parents d3b17e7049bf
children 5c60db96bc32
comparison
equal deleted inserted replaced
15491:ea6697d31d1c 15492:348f0896b1e7
45 ;; (global-font-lock-mode t) 45 ;; (global-font-lock-mode t)
46 ;; 46 ;;
47 ;; 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
48 ;; 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
49 ;; 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
50 ;; also the variable `font-lock-maximum-size'. 50 ;; also the variable `font-lock-maximum-size'. Support modes for Font Lock
51 ;; mode can be used to speed up Font Lock mode. See `font-lock-support-mode'.
51 52
52 ;; If you add patterns for a new mode, say foo.el's `foo-mode', say in which 53 ;; If you add patterns for a new mode, say foo.el's `foo-mode', say in which
53 ;; you don't want syntactic fontification to occur, you can make Font Lock mode 54 ;; you don't want syntactic fontification to occur, you can make Font Lock mode
54 ;; use your regexps when turning on Font Lock by adding to `foo-mode-hook': 55 ;; use your regexps when turning on Font Lock by adding to `foo-mode-hook':
55 ;; 56 ;;
90 ;; Don't use it to, say, highlight keywords in commented out code or strings. 91 ;; Don't use it to, say, highlight keywords in commented out code or strings.
91 ;; - Err, that's it. 92 ;; - Err, that's it.
92 93
93 ;; User variables. 94 ;; User variables.
94 95
95 (defvar font-lock-verbose t 96 (defvar font-lock-verbose (* 0 1024)
96 "*If non-nil, means show status messages when fontifying.") 97 "*If non-nil, means show status messages for buffer fontification.
98 If a number, only buffers greater than this size have fontification messages.")
97 99
98 ;;;###autoload 100 ;;;###autoload
99 (defvar font-lock-maximum-decoration nil 101 (defvar font-lock-maximum-decoration nil
100 "*Maximum decoration level for fontification. 102 "*Maximum decoration level for fontification.
101 If nil, use the default decoration (typically the minimum available). 103 If nil, use the default decoration (typically the minimum available).
102 If t, use the maximum decoration available. 104 If t, use the maximum decoration available.
103 If a number, use that level of decoration (or if not available the maximum). 105 If a number, use that level of decoration (or if not available the maximum).
104 If a list, each element should be a cons pair of the form (MAJOR-MODE . LEVEL), 106 If a list, each element should be a cons pair of the form (MAJOR-MODE . LEVEL),
105 where MAJOR-MODE is a symbol or t (meaning the default). For example: 107 where MAJOR-MODE is a symbol or t (meaning the default). For example:
106 ((c++-mode . 2) (c-mode . t) (t . 1)) 108 ((c-mode . t) (c++-mode . 2) (t . 1))
107 means use level 2 decoration for buffers in `c++-mode', the maximum decoration 109 means use the maximum decoration available for buffers in C mode, level 2
108 available for buffers in `c-mode', and level 1 decoration otherwise.") 110 decoration for buffers in C++ mode, and level 1 decoration otherwise.")
109 111
110 ;;;###autoload 112 ;;;###autoload
111 (defvar font-lock-maximum-size (* 250 1024) 113 (defvar font-lock-maximum-size (* 250 1024)
112 "*Maximum size of a buffer for buffer fontification. 114 "*Maximum size of a buffer for buffer fontification.
113 Only buffers less than this can be fontified when Font Lock mode is turned on. 115 Only buffers less than this can be fontified when Font Lock mode is turned on.
114 If nil, means size is irrelevant. 116 If nil, means size is irrelevant.
115 If a list, each element should be a cons pair of the form (MAJOR-MODE . SIZE), 117 If a list, each element should be a cons pair of the form (MAJOR-MODE . SIZE),
116 where MAJOR-MODE is a symbol or t (meaning the default). For example: 118 where MAJOR-MODE is a symbol or t (meaning the default). For example:
117 ((c++-mode . 256000) (c-mode . 256000) (rmail-mode . 1048576)) 119 ((c-mode . 256000) (c++-mode . 256000) (rmail-mode . 1048576))
118 means that the maximum size is 250K for buffers in `c++-mode' or `c-mode', one 120 means that the maximum size is 250K for buffers in C or C++ modes, one megabyte
119 megabyte for buffers in `rmail-mode', and size is irrelevant otherwise.") 121 for buffers in Rmail mode, and size is irrelevant otherwise.")
120 122
121 ;; Fontification variables: 123 ;; Fontification variables:
122 124
123 (defvar font-lock-comment-face 'font-lock-comment-face 125 (defvar font-lock-comment-face 'font-lock-comment-face
124 "Face to use for comments.") 126 "Face to use for comments.")
243 nil nil ((?_ . "w") (?~ . "w")) beginning-of-defun 245 nil nil ((?_ . "w") (?~ . "w")) beginning-of-defun
244 (font-lock-mark-block-function . mark-defun))) 246 (font-lock-mark-block-function . mark-defun)))
245 (lisp-mode-defaults 247 (lisp-mode-defaults
246 '((lisp-font-lock-keywords 248 '((lisp-font-lock-keywords
247 lisp-font-lock-keywords-1 lisp-font-lock-keywords-2) 249 lisp-font-lock-keywords-1 lisp-font-lock-keywords-2)
248 nil nil 250 nil nil (("+-*/.<>=!?$%_&~^:" . "w")) beginning-of-defun
249 ((?: . "w") (?- . "w") (?* . "w") (?+ . "w") (?. . "w") (?< . "w") 251 (font-lock-mark-block-function . mark-defun)))
250 (?> . "w") (?= . "w") (?! . "w") (?? . "w") (?$ . "w") (?% . "w")
251 (?_ . "w") (?& . "w") (?~ . "w") (?^ . "w") (?/ . "w"))
252 beginning-of-defun (font-lock-mark-block-function . mark-defun)))
253 (scheme-mode-defaults 252 (scheme-mode-defaults
254 '(scheme-font-lock-keywords nil t 253 '(scheme-font-lock-keywords
255 ((?: . "w") (?- . "w") (?* . "w") (?+ . "w") (?. . "w") (?< . "w") 254 nil t (("+-*/.<>=!?$%_&~^:" . "w")) beginning-of-defun
256 (?> . "w") (?= . "w") (?! . "w") (?? . "w") (?$ . "w") (?% . "w") 255 (font-lock-mark-block-function . mark-defun)))
257 (?_ . "w") (?& . "w") (?~ . "w") (?^ . "w") (?/ . "w"))
258 beginning-of-defun (font-lock-mark-block-function . mark-defun)))
259 ;; For TeX modes we could use `backward-paragraph' for the same reason. 256 ;; For TeX modes we could use `backward-paragraph' for the same reason.
260 ;; But we don't, because paragraph breaks are arguably likely enough to 257 ;; But we don't, because paragraph breaks are arguably likely enough to
261 ;; occur within a genuine syntactic block to make it too risky. 258 ;; occur within a genuine syntactic block to make it too risky.
262 ;; However, we do specify a MARK-BLOCK function as that cannot result 259 ;; However, we do specify a MARK-BLOCK function as that cannot result
263 ;; in a mis-fontification even if it might not fontify enough. --sm. 260 ;; in a mis-fontification even if it might not fontify enough. --sm.
289 where MAJOR-MODE is a symbol. KEYWORDS may be a symbol (a variable or function 286 where MAJOR-MODE is a symbol. KEYWORDS may be a symbol (a variable or function
290 whose value is the keywords to use for fontification) or a list of symbols. 287 whose value is the keywords to use for fontification) or a list of symbols.
291 If KEYWORDS-ONLY is non-nil, syntactic fontification (strings and comments) is 288 If KEYWORDS-ONLY is non-nil, syntactic fontification (strings and comments) is
292 not performed. If CASE-FOLD is non-nil, the case of the keywords is ignored 289 not performed. If CASE-FOLD is non-nil, the case of the keywords is ignored
293 when fontifying. If SYNTAX-ALIST is non-nil, it should be a list of cons pairs 290 when fontifying. If SYNTAX-ALIST is non-nil, it should be a list of cons pairs
294 of the form (CHAR . STRING) used to set the local Font Lock syntax table, for 291 of the form (CHAR-OR-STRING . STRING) used to set the local Font Lock syntax
295 keyword and syntactic fontification (see `modify-syntax-entry'). 292 table, for keyword and syntactic fontification (see `modify-syntax-entry').
296 293
297 If SYNTAX-BEGIN is non-nil, it should be a function with no args used to move 294 If SYNTAX-BEGIN is non-nil, it should be a function with no args used to move
298 backwards outside any enclosing syntactic block, for syntactic fontification. 295 backwards outside any enclosing syntactic block, for syntactic fontification.
299 Typical values are `beginning-of-line' (i.e., the start of the line is known to 296 Typical values are `beginning-of-line' (i.e., the start of the line is known to
300 be outside a syntactic block), or `beginning-of-defun' for programming modes or 297 be outside a syntactic block), or `beginning-of-defun' for programming modes or
317 textual modes (i.e., the mode-dependent function is known to put point and mark 314 textual modes (i.e., the mode-dependent function is known to put point and mark
318 around a text block relevant to that mode). 315 around a text block relevant to that mode).
319 316
320 Other variables include those for buffer-specialised fontification functions, 317 Other variables include those for buffer-specialised fontification functions,
321 `font-lock-fontify-buffer-function', `font-lock-unfontify-buffer-function', 318 `font-lock-fontify-buffer-function', `font-lock-unfontify-buffer-function',
322 `font-lock-fontify-region-function', `font-lock-unfontify-region-function' and 319 `font-lock-fontify-region-function', `font-lock-unfontify-region-function',
323 `font-lock-inhibit-thing-lock'.") 320 `font-lock-maximum-size' and `font-lock-inhibit-thing-lock'.")
324 321
325 (defvar font-lock-keywords-only nil 322 (defvar font-lock-keywords-only nil
326 "*Non-nil means Font Lock should not fontify comments or strings. 323 "*Non-nil means Font Lock should not fontify comments or strings.
327 This is normally set via `font-lock-defaults'.") 324 This is normally set via `font-lock-defaults'.")
328 325
375 Currently, valid mode names as `fast-lock-mode' and `lazy-lock-mode'. 372 Currently, valid mode names as `fast-lock-mode' and `lazy-lock-mode'.
376 This is normally set via `font-lock-defaults'.") 373 This is normally set via `font-lock-defaults'.")
377 374
378 (defvar font-lock-mode nil) ; For the modeline. 375 (defvar font-lock-mode nil) ; For the modeline.
379 (defvar font-lock-fontified nil) ; Whether we have fontified the buffer. 376 (defvar font-lock-fontified nil) ; Whether we have fontified the buffer.
380 (put 'font-lock-fontified 'permanent-local t)
381 377
382 ;;;###autoload 378 ;;;###autoload
383 (defvar font-lock-mode-hook nil 379 (defvar font-lock-mode-hook nil
384 "Function or functions to run on entry to Font Lock mode.") 380 "Function or functions to run on entry to Font Lock mode.")
385 381
386 ;; User commands. 382 ;; Font Lock mode.
383
384 (eval-when-compile
385 ;; We don't do this at the top-level as we only use non-autoloaded macros.
386 (require 'cl))
387 387
388 ;;;###autoload 388 ;;;###autoload
389 (defun font-lock-mode (&optional arg) 389 (defun font-lock-mode (&optional arg)
390 "Toggle Font Lock mode. 390 "Toggle Font Lock mode.
391 With arg, turn Font Lock mode on if and only if arg is positive. 391 With arg, turn Font Lock mode on if and only if arg is positive.
411 The default Font Lock mode faces and their attributes are defined in the 411 The default Font Lock mode faces and their attributes are defined in the
412 variable `font-lock-face-attributes', and Font Lock mode default settings in 412 variable `font-lock-face-attributes', and Font Lock mode default settings in
413 the variable `font-lock-defaults-alist'. You can set your own default settings 413 the variable `font-lock-defaults-alist'. You can set your own default settings
414 for some mode, by setting a buffer local value for `font-lock-defaults', via 414 for some mode, by setting a buffer local value for `font-lock-defaults', via
415 its mode hook. 415 its mode hook.
416
417 Font Lock mode has a number of support modes that may be used to speed up Font
418 Lock mode in various ways. See the variable `font-lock-support-mode'.
416 419
417 Where modes support different levels of fontification, you can use the variable 420 Where modes support different levels of fontification, you can use the variable
418 `font-lock-maximum-decoration' to specify which level you generally prefer. 421 `font-lock-maximum-decoration' to specify which level you generally prefer.
419 When you turn Font Lock mode on/off the buffer is fontified/defontified, though 422 When you turn Font Lock mode on/off the buffer is fontified/defontified, though
420 fontification occurs only if the buffer is less than `font-lock-maximum-size'. 423 fontification occurs only if the buffer is less than `font-lock-maximum-size'.
426 lines around point), perhaps because modification on the current line caused 429 lines around point), perhaps because modification on the current line caused
427 syntactic change on other lines, you can use \\[font-lock-fontify-block]." 430 syntactic change on other lines, you can use \\[font-lock-fontify-block]."
428 (interactive "P") 431 (interactive "P")
429 ;; Don't turn on Font Lock mode if we don't have a display (we're running a 432 ;; Don't turn on Font Lock mode if we don't have a display (we're running a
430 ;; batch job) or if the buffer is invisible (the name starts with a space). 433 ;; batch job) or if the buffer is invisible (the name starts with a space).
431 (let ((maximum-size (font-lock-value-in-major-mode font-lock-maximum-size)) 434 (let ((on-p (and (not noninteractive)
432 (on-p (and (not noninteractive)
433 (not (eq (aref (buffer-name) 0) ?\ )) 435 (not (eq (aref (buffer-name) 0) ?\ ))
434 (if arg 436 (if arg
435 (> (prefix-numeric-value arg) 0) 437 (> (prefix-numeric-value arg) 0)
436 (not font-lock-mode))))) 438 (not font-lock-mode)))))
437 (if (not on-p) 439 (set (make-local-variable 'font-lock-mode) on-p)
438 (remove-hook 'after-change-functions 'font-lock-after-change-function 440 ;; Turn on Font Lock mode.
439 t) 441 (when on-p
440 (make-local-hook 'after-change-functions) 442 (make-local-hook 'after-change-functions)
441 (add-hook 'after-change-functions 'font-lock-after-change-function 443 (add-hook 'after-change-functions 'font-lock-after-change-function nil t)
442 nil t)) 444 (font-lock-set-defaults)
443 (set (make-local-variable 'font-lock-mode) on-p) 445 (font-lock-turn-on-thing-lock)
444 (cond (on-p 446 (run-hooks 'font-lock-mode-hook)
445 (font-lock-set-defaults) 447 ;; Fontify the buffer if we have to.
446 ;; If buffer is reverted, must clean up the state. 448 (let ((max-size (font-lock-value-in-major-mode font-lock-maximum-size)))
447 (make-local-hook 'before-revert-hook) 449 (cond (font-lock-fontified
448 (make-local-hook 'after-revert-hook) 450 nil)
449 (add-hook 'before-revert-hook 'font-lock-revert-setup nil t) 451 ((or (null max-size) (> max-size (buffer-size)))
450 (add-hook 'after-revert-hook 'font-lock-revert-cleanup nil t) 452 (font-lock-fontify-buffer))
451 (run-hooks 'font-lock-mode-hook) 453 (font-lock-verbose
452 (cond (font-lock-fontified 454 (message "Fontifying %s...buffer too big" (buffer-name))))))
453 nil) 455 ;; Turn off Font Lock mode.
454 ((or (null maximum-size) (<= (buffer-size) maximum-size) 456 (when (not on-p)
455 (not (eq font-lock-fontify-buffer-function 457 (remove-hook 'after-change-functions 'font-lock-after-change-function t)
456 (default-value 458 (font-lock-unfontify-buffer)
457 'font-lock-fontify-buffer-function)))) 459 (font-lock-turn-off-thing-lock)
458 (font-lock-fontify-buffer)) 460 (font-lock-unset-defaults))
459 (font-lock-verbose
460 (message "Fontifying %s...buffer too big" (buffer-name)))))
461 (font-lock-fontified
462 (font-lock-unfontify-buffer)
463 (remove-hook 'before-revert-hook 'font-lock-revert-setup t)
464 (remove-hook 'after-revert-hook 'font-lock-revert-cleanup t)
465 (font-lock-thing-lock-cleanup)
466 (font-lock-unset-defaults))
467 (t
468 (remove-hook 'before-revert-hook 'font-lock-revert-setup t)
469 (remove-hook 'after-revert-hook 'font-lock-revert-cleanup t)
470 (font-lock-thing-lock-cleanup)
471 (font-lock-unset-defaults)))
472 (force-mode-line-update))) 461 (force-mode-line-update)))
473 462
474 ;;;###autoload 463 ;;;###autoload
475 (defun turn-on-font-lock () 464 (defun turn-on-font-lock ()
476 "Turn on Font Lock mode conditionally. 465 "Turn on Font Lock mode conditionally.
478 (if (and window-system 467 (if (and window-system
479 (not font-lock-mode) 468 (not font-lock-mode)
480 (or font-lock-defaults (assq major-mode font-lock-defaults-alist))) 469 (or font-lock-defaults (assq major-mode font-lock-defaults-alist)))
481 (font-lock-mode t))) 470 (font-lock-mode t)))
482 471
483 ;; Code for Global Font Lock mode. 472 ;; Global Font Lock mode.
484 473 ;;
485 ;; A few people have hassled in the past for a way to make it easier to turn on 474 ;; A few people have hassled in the past for a way to make it easier to turn on
486 ;; Font Lock mode, perhaps the same way hilit19.el/hl319.el does. I've always 475 ;; Font Lock mode, without the user needing to know for which modes s/he has to
476 ;; turn it on, perhaps the same way hilit19.el/hl319.el does. I've always
487 ;; balked at that way, as I see it as just re-moulding the same problem in 477 ;; balked at that way, as I see it as just re-moulding the same problem in
488 ;; another form. That is; some person would still have to keep track of which 478 ;; another form. That is; some person would still have to keep track of which
489 ;; modes (which may not even be distributed with Emacs) support Font Lock mode. 479 ;; modes (which may not even be distributed with Emacs) support Font Lock mode.
490 ;; The list would always be out of date. And that person might have to be me. 480 ;; The list would always be out of date. And that person might have to be me.
491 481
492 ;; In the latest of these discussions the following hack came to mind. It is a 482 ;; Implementation.
493 ;; gross hack, but it generally works. We use the convention that major modes 483 ;;
494 ;; start by calling the function `kill-all-local-variables', which in turn runs 484 ;; In a previous discussion the following hack came to mind. It is a gross
485 ;; hack, but it generally works. We use the convention that major modes start
486 ;; by calling the function `kill-all-local-variables', which in turn runs
495 ;; functions on the hook variable `change-major-mode-hook'. We attach our 487 ;; functions on the hook variable `change-major-mode-hook'. We attach our
496 ;; function `font-lock-change-major-mode' to that hook. Of course, when this 488 ;; function `font-lock-change-major-mode' to that hook. Of course, when this
497 ;; hook is run, the major mode is in the process of being changed and we do not 489 ;; hook is run, the major mode is in the process of being changed and we do not
498 ;; know what the final major mode will be. So, `font-lock-change-major-mode' 490 ;; know what the final major mode will be. So, `font-lock-change-major-mode'
499 ;; only (a) notes the name of the current buffer, and (b) adds our function 491 ;; only (a) notes the name of the current buffer, and (b) adds our function
500 ;; `turn-on-font-lock-if-enabled' to the hook variable `post-command-hook'. 492 ;; `turn-on-font-lock-if-enabled' to the hook variables `find-file-hooks' and
501 ;; By the time the functions on `post-command-hook' are run, the new major mode 493 ;; `post-command-hook' (for buffers that are not visiting files). By the time
502 ;; is assumed to be in place. 494 ;; the functions on the first of these hooks to be run are run, the new major
503 495 ;; mode is assumed to be in place. This way we get a Font Lock function run
496 ;; when a major mode is turned on, without knowing major modes or their hooks.
497 ;;
504 ;; Naturally this requires that (a) major modes run `kill-all-local-variables', 498 ;; Naturally this requires that (a) major modes run `kill-all-local-variables',
505 ;; as they are supposed to do, and (b) the major mode is in place after the 499 ;; as they are supposed to do, and (b) the major mode is in place after the
506 ;; command that ran `kill-all-local-variables' has finished. Arguably, any 500 ;; file is visited or the command that ran `kill-all-local-variables' has
507 ;; major mode that does not follow the convension (a) is broken, and I can't 501 ;; finished, whichever the sooner. Arguably, any major mode that does not
508 ;; think of any reason why (b) would not be met. I don't know of any major 502 ;; follow the convension (a) is broken, and I can't think of any reason why (b)
509 ;; modes that do not follow the convension (a), but I'm sure there are some 503 ;; would not be met (except `gnudoit' on non-files). However, it is not clean.
510 ;; obscure ones out there somewhere. Even if it works, it is still not clean. 504 ;;
511
512 ;; Probably the cleanest solution is to have each major mode function run some 505 ;; Probably the cleanest solution is to have each major mode function run some
513 ;; hook, e.g., `major-mode-hook', but maybe implementing that change is 506 ;; hook, e.g., `major-mode-hook', but maybe implementing that change is
514 ;; impractical. I am personally against making `setq' a macro or be advised 507 ;; impractical. I am personally against making `setq' a macro or be advised,
515 ;; (space'n'speed), or have a special function such as `set-major-mode' (a 508 ;; or have a special function such as `set-major-mode', but maybe someone can
516 ;; `major-mode-hook' is simpler), but maybe someone can come up with another 509 ;; come up with another solution?
517 ;; solution? --sm. 510
511 ;; User interface.
512 ;;
513 ;; Although Global Font Lock mode is a pseudo-mode, I think that the user
514 ;; interface should conform to the usual Emacs convention for modes, i.e., a
515 ;; command to toggle the feature (`global-font-lock-mode') with a variable for
516 ;; finer control of the mode's behaviour (`font-lock-global-modes').
517 ;;
518 ;; I don't think it is better that the feature be enabled via a variable, since
519 ;; it does not conform to the usual convention. I don't think the feature
520 ;; should be enabled by loading font-lock.el, since other mechanisms such as
521 ;; M-x font-lock-mode RET or (add-hook 'c-mode-hook 'turn-on-font-lock) would
522 ;; cause Font Lock mode to be turned on everywhere, and it is not intuitive or
523 ;; informative because loading a file tells you nothing about the feature or
524 ;; how to control it. It would be contrary to the Principle of Least Surprise.
518 525
519 (defvar font-lock-buffers nil) ; For remembering buffers. 526 (defvar font-lock-buffers nil) ; For remembering buffers.
520 (defvar change-major-mode-hook nil) ; Make sure it's not void. 527 (defvar global-font-lock-mode nil)
521 528
522 ;;;###autoload 529 ;;;###autoload
523 (defvar font-lock-global-modes t 530 (defvar font-lock-global-modes t
524 "*Modes for which Font Lock mode is automatically turned on. 531 "*Modes for which Font Lock mode is automagically turned on.
525 Global Font Lock mode is controlled by the `global-font-lock-mode' command. 532 Global Font Lock mode is controlled by the `global-font-lock-mode' command.
526 If nil, means no modes have Font Lock mode automatically turned on. 533 If nil, means no modes have Font Lock mode automatically turned on.
527 If t, all modes that support Font Lock mode have it automatically turned on. 534 If t, all modes that support Font Lock mode have it automatically turned on.
528 If a list, each element should be a major mode symbol name such as `c-mode'. 535 If a list, it should be a list of `major-mode' symbol names for which Font Lock
529 Font Lock is automatically turned on if the buffer major mode supports it and 536 mode should be automatically turned on. The sense of the list is negated if it
530 is in this list. The sense of the list is negated if it begins with `not'.") 537 begins with `not'. For example:
538 (c-mode c++-mode)
539 means that Font Lock mode is turned on for buffers in C and C++ modes only.")
531 540
532 ;;;###autoload 541 ;;;###autoload
533 (defun global-font-lock-mode (&optional arg message) 542 (defun global-font-lock-mode (&optional arg message)
534 "Toggle Global Font Lock mode. 543 "Toggle Global Font Lock mode.
535 With prefix ARG, turn Global Font Lock mode on if and only if ARG is positive. 544 With prefix ARG, turn Global Font Lock mode on if and only if ARG is positive.
539 When Global Font Lock mode is enabled, Font Lock mode is automagically 548 When Global Font Lock mode is enabled, Font Lock mode is automagically
540 turned on in a buffer if its major mode is one of `font-lock-global-modes'." 549 turned on in a buffer if its major mode is one of `font-lock-global-modes'."
541 (interactive "P\np") 550 (interactive "P\np")
542 (let ((off-p (if arg 551 (let ((off-p (if arg
543 (<= (prefix-numeric-value arg) 0) 552 (<= (prefix-numeric-value arg) 0)
544 (memq 'font-lock-change-major-mode change-major-mode-hook)))) 553 global-font-lock-mode)))
545 (if off-p 554 (if off-p
546 (remove-hook 'change-major-mode-hook 'font-lock-change-major-mode) 555 (remove-hook 'find-file-hooks 'turn-on-font-lock-if-enabled)
547 (add-hook 'change-major-mode-hook 'font-lock-change-major-mode) 556 (add-hook 'find-file-hooks 'turn-on-font-lock-if-enabled)
548 (add-hook 'post-command-hook 'turn-on-font-lock-if-enabled) 557 (add-hook 'post-command-hook 'turn-on-font-lock-if-enabled)
549 ;; This is to make sure we fontify immediately
550 ;; when process filters (such as GUD) find files.
551 (add-hook 'find-file-hooks 'turn-on-font-lock-if-enabled)
552 (setq font-lock-buffers (buffer-list))) 558 (setq font-lock-buffers (buffer-list)))
553 (if message 559 (when message
554 (message "Global Font Lock mode is now %s." (if off-p "OFF" "ON"))) 560 (message "Global Font Lock mode is now %s." (if off-p "OFF" "ON")))
555 (not off-p))) 561 (setq global-font-lock-mode (not off-p))))
556 562
557 (defun font-lock-change-major-mode () 563 (defun font-lock-change-major-mode ()
564 ;; Turn off Font Lock mode if it's on.
565 (when font-lock-mode
566 (font-lock-mode nil))
558 ;; Gross hack warning: Delicate readers should avert eyes now. 567 ;; Gross hack warning: Delicate readers should avert eyes now.
559 ;; Something is running `kill-all-local-variables', which generally means the 568 ;; Something is running `kill-all-local-variables', which generally means the
560 ;; major mode is being changed. Run `turn-on-font-lock-if-enabled' after the 569 ;; major mode is being changed. Run `turn-on-font-lock-if-enabled' after the
561 ;; current command has finished. 570 ;; file is visited or the current command has finished.
562 (add-hook 'post-command-hook 'turn-on-font-lock-if-enabled) 571 (when global-font-lock-mode
563 (add-to-list 'font-lock-buffers (current-buffer))) 572 (add-hook 'post-command-hook 'turn-on-font-lock-if-enabled)
573 (add-to-list 'font-lock-buffers (current-buffer))))
564 574
565 (defun turn-on-font-lock-if-enabled () 575 (defun turn-on-font-lock-if-enabled ()
566 ;; Gross hack warning: Delicate readers should avert eyes now. 576 ;; Gross hack warning: Delicate readers should avert eyes now.
567 ;; Turn on Font Lock mode if it's one of `font-lock-global-modes'. 577 ;; Turn on Font Lock mode if it's one of `font-lock-global-modes'.
568 (remove-hook 'post-command-hook 'turn-on-font-lock-if-enabled) 578 (remove-hook 'post-command-hook 'turn-on-font-lock-if-enabled)
576 (memq major-mode font-lock-global-modes))) 586 (memq major-mode font-lock-global-modes)))
577 (let (inhibit-quit) 587 (let (inhibit-quit)
578 (turn-on-font-lock))))) 588 (turn-on-font-lock)))))
579 (setq font-lock-buffers (cdr font-lock-buffers)))) 589 (setq font-lock-buffers (cdr font-lock-buffers))))
580 590
591 (add-hook 'change-major-mode-hook 'font-lock-change-major-mode)
592
581 ;; End of Global Font Lock mode. 593 ;; End of Global Font Lock mode.
594
595 ;; Font Lock Support mode.
596 ;;
597 ;; This is the code used to interface font-lock.el with any of its add-on
598 ;; packages, and provide the user interface. Packages that have their own
599 ;; local buffer fontification functions (see below) may have to call
600 ;; `font-lock-after-fontify-buffer' and/or `font-lock-after-unfontify-buffer'
601 ;; themselves.
602
603 ;;;###autoload
604 (defvar font-lock-support-mode nil
605 "*Support mode for Font Lock mode.
606 Support modes speed up Font Lock mode by being choosy about when fontification
607 occurs. Known support modes are Fast Lock mode (symbol `fast-lock-mode') and
608 Lazy Lock mode (symbol `lazy-lock-mode'). See those modes for more info.
609 If nil, means support for Font Lock mode is never performed.
610 If a symbol, use that support mode.
611 If a list, each element should be of the form (MAJOR-MODE . SUPPORT-MODE),
612 where MAJOR-MODE is a symbol or t (meaning the default). For example:
613 ((c-mode . fast-lock-mode) (c++-mode . fast-lock-mode) (t . lazy-lock-mode))
614 means that Fast Lock mode is used to support Font Lock mode for buffers in C or
615 C++ modes, and Lazy Lock mode is used to support Font Lock mode otherwise.
616
617 The value of this variable is used when Font Lock mode is turned on.")
618
619 (defun font-lock-turn-on-thing-lock ()
620 (let ((thing-mode (font-lock-value-in-major-mode font-lock-support-mode)))
621 (cond ((eq thing-mode 'fast-lock-mode)
622 (fast-lock-mode t))
623 ((eq thing-mode 'lazy-lock-mode)
624 (lazy-lock-mode t)))))
625
626 (defvar fast-lock-mode nil)
627 (defvar lazy-lock-mode nil)
628
629 (defun font-lock-turn-off-thing-lock ()
630 (cond (fast-lock-mode
631 (fast-lock-mode nil))
632 (lazy-lock-mode
633 (lazy-lock-mode nil))))
634
635 (defun font-lock-after-fontify-buffer ()
636 (cond (fast-lock-mode
637 (fast-lock-after-fontify-buffer))
638 (lazy-lock-mode
639 (lazy-lock-after-fontify-buffer))))
640
641 (defun font-lock-after-unfontify-buffer ()
642 (cond (fast-lock-mode
643 (fast-lock-after-unfontify-buffer))
644 (lazy-lock-mode
645 (lazy-lock-after-unfontify-buffer))))
646
647 ;; End of Font Lock Support mode.
582 648
583 ;; Fontification functions. 649 ;; Fontification functions.
584 650
585 ;;;###autoload 651 ;;;###autoload
586 (defun font-lock-fontify-buffer () 652 (defun font-lock-fontify-buffer ()
597 663
598 (defun font-lock-unfontify-region (beg end) 664 (defun font-lock-unfontify-region (beg end)
599 (funcall font-lock-unfontify-region-function beg end)) 665 (funcall font-lock-unfontify-region-function beg end))
600 666
601 (defun font-lock-default-fontify-buffer () 667 (defun font-lock-default-fontify-buffer ()
602 (let ((verbose (and font-lock-verbose (> (buffer-size) 0)))) 668 (let ((verbose (if (numberp font-lock-verbose)
669 (> (buffer-size) font-lock-verbose)
670 font-lock-verbose)))
603 (if verbose (message "Fontifying %s..." (buffer-name))) 671 (if verbose (message "Fontifying %s..." (buffer-name)))
604 ;; Make sure we have the right `font-lock-keywords' etc. 672 ;; Make sure we have the right `font-lock-keywords' etc.
605 (if (not font-lock-mode) (font-lock-set-defaults)) 673 (if (not font-lock-mode) (font-lock-set-defaults))
606 ;; Make sure we fontify etc. in the whole buffer. 674 ;; Make sure we fontify etc. in the whole buffer.
607 (save-restriction 675 (save-restriction
969 (setq highlights (cdr highlights)))) 1037 (setq highlights (cdr highlights))))
970 (setq keywords (cdr keywords))))) 1038 (setq keywords (cdr keywords)))))
971 1039
972 ;; Various functions. 1040 ;; Various functions.
973 1041
974 ;; Turn off other related packages if they're on. I prefer a hook. --sm.
975 ;; These explicit calls are easier to understand
976 ;; because people know what they will do.
977 ;; A hook is a mystery because it might do anything whatever. --rms.
978 (defun font-lock-thing-lock-cleanup ()
979 (cond ((and (boundp 'fast-lock-mode) fast-lock-mode)
980 (fast-lock-mode -1))
981 ((and (boundp 'lazy-lock-mode) lazy-lock-mode)
982 (lazy-lock-mode -1))))
983
984 ;; Do something special for these packages after fontifying; I prefer a hook.
985 (defun font-lock-after-fontify-buffer ()
986 (cond ((and (boundp 'fast-lock-mode) fast-lock-mode)
987 (fast-lock-after-fontify-buffer))
988 ((and (boundp 'lazy-lock-mode) lazy-lock-mode)
989 (lazy-lock-after-fontify-buffer))))
990
991 ;; Do something special for these packages after unfontifying; I prefer a hook.
992 (defun font-lock-after-unfontify-buffer ()
993 (cond ((and (boundp 'fast-lock-mode) fast-lock-mode)
994 (fast-lock-after-unfontify-buffer))
995 ((and (boundp 'lazy-lock-mode) lazy-lock-mode)
996 (lazy-lock-after-unfontify-buffer))))
997
998 ;; If the buffer is about to be reverted, it won't be fontified afterward.
999 (defun font-lock-revert-setup ()
1000 (setq font-lock-fontified nil))
1001
1002 ;; If the buffer has just been reverted, normally that turns off
1003 ;; Font Lock mode. So turn the mode back on if necessary.
1004 (defalias 'font-lock-revert-cleanup
1005 'turn-on-font-lock)
1006
1007 (defun font-lock-compile-keywords (&optional keywords) 1042 (defun font-lock-compile-keywords (&optional keywords)
1008 ;; Compile `font-lock-keywords' into the form (t KEYWORD ...) where KEYWORD 1043 ;; Compile `font-lock-keywords' into the form (t KEYWORD ...) where KEYWORD
1009 ;; is the (MATCHER HIGHLIGHT ...) shown in the variable's doc string. 1044 ;; is the (MATCHER HIGHLIGHT ...) shown in the variable's doc string.
1010 (let ((keywords (or keywords font-lock-keywords))) 1045 (let ((keywords (or keywords font-lock-keywords)))
1011 (setq font-lock-keywords 1046 (setq font-lock-keywords
1074 (if (nth 3 defaults) 1109 (if (nth 3 defaults)
1075 (let ((slist (nth 3 defaults))) 1110 (let ((slist (nth 3 defaults)))
1076 (set (make-local-variable 'font-lock-syntax-table) 1111 (set (make-local-variable 'font-lock-syntax-table)
1077 (copy-syntax-table (syntax-table))) 1112 (copy-syntax-table (syntax-table)))
1078 (while slist 1113 (while slist
1079 (modify-syntax-entry (car (car slist)) (cdr (car slist)) 1114 ;; The character to modify may be a single CHAR or a STRING.
1080 font-lock-syntax-table) 1115 (let ((chars (if (numberp (car (car slist)))
1081 (setq slist (cdr slist))))) 1116 (list (car (car slist)))
1117 (mapcar 'identity (car (car slist)))))
1118 (syntax (cdr (car slist))))
1119 (while chars
1120 (modify-syntax-entry (car chars) syntax
1121 font-lock-syntax-table)
1122 (setq chars (cdr chars)))
1123 (setq slist (cdr slist))))))
1082 ;; Syntax function for syntactic fontification? 1124 ;; Syntax function for syntactic fontification?
1083 (if (nth 4 defaults) 1125 (if (nth 4 defaults)
1084 (set (make-local-variable 'font-lock-beginning-of-syntax-function) 1126 (set (make-local-variable 'font-lock-beginning-of-syntax-function)
1085 (nth 4 defaults))) 1127 (nth 4 defaults)))
1086 ;; Variable alist? 1128 ;; Variable alist?
1497 '("^#[ \t]*include[ \t]+\\(<[^>\"\n]+>\\)" 1 font-lock-string-face) 1539 '("^#[ \t]*include[ \t]+\\(<[^>\"\n]+>\\)" 1 font-lock-string-face)
1498 ;; 1540 ;;
1499 ;; Fontify function macro names. 1541 ;; Fontify function macro names.
1500 '("^#[ \t]*define[ \t]+\\(\\sw+\\)(" 1 font-lock-function-name-face) 1542 '("^#[ \t]*define[ \t]+\\(\\sw+\\)(" 1 font-lock-function-name-face)
1501 ;; 1543 ;;
1502 ;; Fontify symbol names in #if ... defined preprocessor directives. 1544 ;; Fontify symbol names in #elif or #if ... defined preprocessor directives.
1503 '("^#[ \t]*if\\>" 1545 '("^#[ \t]*\\(elif\\|if\\)\\>"
1504 ("\\<\\(defined\\)\\>[ \t]*(?\\(\\sw+\\)?" nil nil 1546 ("\\<\\(defined\\)\\>[ \t]*(?\\(\\sw+\\)?" nil nil
1505 (1 font-lock-reference-face) (2 font-lock-variable-name-face nil t))) 1547 (1 font-lock-reference-face) (2 font-lock-variable-name-face nil t)))
1506 ;; 1548 ;;
1507 ;; Fontify otherwise as symbol names, and the preprocessor directive names. 1549 ;; Fontify otherwise as symbol names, and the preprocessor directive names.
1508 '("^#[ \t]*\\(\\sw+\\)\\>[ \t]*\\(\\sw+\\)?" 1550 '("^#[ \t]*\\(\\sw+\\)\\>[ \t]*\\(\\sw+\\)?"
1576 (list 1618 (list
1577 '("^\\(\\sw+\\)\\(::\\(\\sw+\\)\\)?[ \t]*(" 1619 '("^\\(\\sw+\\)\\(::\\(\\sw+\\)\\)?[ \t]*("
1578 (1 (if (match-beginning 2) 1620 (1 (if (match-beginning 2)
1579 font-lock-type-face 1621 font-lock-type-face
1580 font-lock-function-name-face)) 1622 font-lock-function-name-face))
1581 (3 (if (match-beginning 2) font-lock-function-name-face) nil t)) 1623 (3 font-lock-function-name-face nil t))
1582 ))) 1624 )))
1583 1625
1584 (setq c++-font-lock-keywords-2 1626 (setq c++-font-lock-keywords-2
1585 (append c++-font-lock-keywords-1 1627 (append c++-font-lock-keywords-1
1586 (list 1628 (list
1684 3 (if (match-beginning 2) 'bold 'italic) keep)) 1726 3 (if (match-beginning 2) 'bold 'italic) keep))
1685 "Default expressions to highlight in TeX modes.") 1727 "Default expressions to highlight in TeX modes.")
1686 1728
1687 ;; Install ourselves: 1729 ;; Install ourselves:
1688 1730
1689 (or (assq 'font-lock-mode minor-mode-alist) 1731 (unless (assq 'font-lock-mode minor-mode-alist)
1690 (setq minor-mode-alist (cons '(font-lock-mode " Font") minor-mode-alist))) 1732 (setq minor-mode-alist (cons '(font-lock-mode " Font") minor-mode-alist)))
1691 1733
1692 ;; Provide ourselves: 1734 ;; Provide ourselves:
1693 1735
1694 (provide 'font-lock) 1736 (provide 'font-lock)
1695 1737