1 ;;; lazy-lock.el --- Lazy demand-driven fontification for fast Font Lock mode.
3 ;; Copyright (C) 1994, 1995 Free Software Foundation, Inc.
4 ;; Copyright (C) 2000, 2003 Ben Wing.
6 ;; Author: Ben Wing <ben@xemacs.org>
7 ;; Original Author: Simon Marshall <simon@gnu.org>
8 ;; Maintainer: XEmacs Development Team
9 ;; Keywords: faces files
11 ;;; This file is part of XEmacs.
13 ;; XEmacs is free software; you can redistribute it and/or modify
14 ;; it under the terms of the GNU General Public License as published by
15 ;; the Free Software Foundation; either version 2, or (at your option)
18 ;; XEmacs is distributed in the hope that it will be useful,
19 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
20 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 ;; GNU General Public License for more details.
23 ;; You should have received a copy of the GNU General Public License
24 ;; along with XEmacs; see the file COPYING. If not, write to the
25 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
26 ;; Boston, MA 02111-1307, USA.
28 ;;; Synched up with: FSF 21.2. Heavily, heavily modified.
32 ;; This version of Lazy Lock has been rewritten for XEmacs by Ben Wing.
33 ;; The FSF version (2.11, as of FSF 21.2) supports GNU Emacs only, and
34 ;; relies on C support that is extremely kludgy (three different hooks:
35 ;; `window-scroll-functions', `window-size-change-functions', and
36 ;; `redisplay-end-trigger-functions', and an additional need to hook onto
37 ;; `before-change-functions') and not supported by XEmacs. This version
38 ;; uses `pre-idle-hook' instead.
40 ;; Simon Marshall has a long diatribe about `pre-idle-hook', noting the
41 ;; fact that it will be called before nearly every redisplay and therefore
42 ;; assuming (wrongly) that this will make a lazy-lock implemented that way
43 ;; extremely slow, as well as complaining about the need to fontify twice
44 ;; as much as necessary, since we don't know the area that will be
45 ;; displayed. However, (a) under XEmacs, we have an argument `GUARANTEE'
46 ;; to `window-end' that computes the proper value as of the next redisplay,
47 ;; so we don't run into the need to fontify more than necessary (in fact
48 ;; lazy-lock version 2 still has this problem), and (b) given proper
49 ;; optimization checks in the pre-idle-hook, the speed of running the hook
50 ;; does not turn out to be a major factor at all. (It's easy to profile
51 ;; this given the built-in profiling support in XEmacs.) We DO have a
52 ;; problem under some circumstances with scrolling -- in particular,
53 ;; scrolling using the arrow keys, but not the page-up/page-down keys.
54 ;; This is because point moves outside the displayed area, and redisplay
55 ;; will then change the displayed area. In such a case we do indeed need
56 ;; to kludgily fontify an area twice the size of the window (and that's
57 ;; only a guess). However, this is at least mitigated by the fact that we
58 ;; can check to determine whether this will happen and only need to take
59 ;; evasive action in those circumstances.
61 ;; Gerd Marshall in FSF 21 finally went and implemented more-or-less
62 ;; non-kludgy C support for fontification. This involves a hook
63 ;; `fontification-functions', which is triggered by the absence of the
64 ;; `fontified' property on text that redisplay is displaying. This way,
65 ;; redisplay tells us exactly what needs to be fontified, and there is no
66 ;; need for guessing by trapping all sorts of hooks. The one thing that
67 ;; seems somewhat kludgy about this interface is that it passes only a
68 ;; single position, not a range, and asks that the function arbitarily
69 ;; fontify a chunk of 400-600 characters, after which it will check again.
70 ;; However, I trust Gerd's sense of design so I assume there was a reason
71 ;; for this. Perhaps at some point we will implement this support in
74 ;; Finally, for reasons that are not at all clear to me, someone went ahead
75 ;; and created another lazy fontification package for XEmacs (lazy-shot).
76 ;; That package relies on the extent property `initial-redisplay-function',
77 ;; which would not be so bad except that the implementation of this
78 ;; function is broken in that the function is called through an eval event,
79 ;; which is executed *after* redisplay. Thus, horrible redisplay flashing.
80 ;; To fix this, let the function be called at pre-idle-hook time, or just
81 ;; scrap this stuff entirely and implement `fontification-functions'.
83 ;; (NB Steve claimed that lazy-lock is too slow or something. However,
84 ;; I used to use it regularly on a Pentium 90 with no problems.)
86 ;; Note: This version is extensively modified from FSF lazy-lock v2.11.
87 ;; Everything related to window-scroll-functions and redisplay end triggers
88 ;; has been removed, as well as the variable `lazy-lock-defer-on-scrolling'
89 ;; and the following functions:
91 ;; -- `lazy-lock-fontify-after-scroll'
92 ;; -- `lazy-lock-defer-after-scroll'
93 ;; -- `lazy-lock-fontify-after-resize'
94 ;; -- `lazy-lock-arrange-before-change'
95 ;; -- `lazy-lock-fontify-after-trigger'
96 ;; -- `lazy-lock-fontify-line-after-change'
97 ;; -- `lazy-lock-fontify-rest-after-change'
98 ;; -- `lazy-lock-defer-line-after-change'
99 ;; -- `lazy-lock-defer-rest-after-change'
100 ;; -- `lazy-lock-fontify-after-visage'
101 ;; -- `lazy-lock-fontify-conservatively'
103 ;; The `*-after-change' functions have been combined into a single
104 ;; after-change function, based on jit-lock (our model, based on
105 ;; pre-idle-hook, is much closer to jit-lock's than FSF's lazy-lock, in
106 ;; that both we and jit-lock have redisplay support of some sort that
107 ;; guarantees that visible regions will get fontified, without the need for
108 ;; numerous hooks and the collusion of other packages). The rest of the
109 ;; functions are simply unnecessary. Some code from lazy-lock v1 is
110 ;; carried over, in particular the code to walk the frames and windows
111 ;; (since pre-idle-hook is called just once and we're not told which
112 ;; windows need updating).
114 ;; The prime operation of this package can be see in the functions
115 ;; `lazy-lock-fontify-window' and `lazy-lock-after-change'.
116 ;; `lazy-lock-pre-fontify-windows' is also interesting, but mainly just
117 ;; walks through frames and windows, finds windows to fontify and passes
118 ;; them to `lazy-lock-fontify-window'.
120 ;; "Deferring" in this context is different from the deferring that
121 ;; font-lock itself does.
123 ;; Font-lock defers fontification of changes made to a buffer until right
124 ;; before display of that buffer. This has lots of advantages -- most
125 ;; noteworthy, it in one fell swoop eliminates almost all the problems with
126 ;; excess fontification. Temporary buffers will never be displayed, so
127 ;; they never will have any fontification done on them. Multiple changes
128 ;; to a buffer can be batched up -- this is important because there is a
129 ;; lot of overhead to doing even a one-character fontification. When a
130 ;; function makes a character-at-a-time change, font-lock used to go crazy,
133 ;; Lazy-lock (and jit-lock, etc.) deferral (perhaps we should call it
134 ;; "support-mode deferral" refers to deferring fontification not until the
135 ;; next redisplay, but some time later (1/4 of a second, 3 seconds, 30
136 ;; seconds. etc). This usually happens in the context of the after-change
137 ;; function, where the actual region changed (perhaps enlarged a bit) is
138 ;; fontified and the following text is marked (in some sense) as
139 ;; unfontified and will get fontified later, for example on an idle-timer
140 ;; set to go off 1/4 of a second after idle. That way, there will be no
141 ;; interference in typing that would happen when you try to immediately
142 ;; fontify the whole rest of the window every single change.
148 ;; To make visiting buffers in `font-lock-mode' faster by making fontification
149 ;; be demand-driven and stealthy.
150 ;; Fontification only occurs when, and where, necessary.
152 ;; See caveats and feedback below. See also the fast-lock and lazy-shot
153 ;; packages. (But don't use them at the same time as lazy-lock!)
157 ;; As of 21.5, put in your ~/.emacs:
159 ;; (setq font-lock-support-mode 'lazy-lock-mode)
161 ;; For 21.4, do this:
163 ;; (add-hook 'font-lock-mode-hook 'turn-on-lazy-lock)
165 ;; Start up a new XEmacs and use font-lock as usual (except that you can
166 ;; use the so-called "gaudier" fontification regexps on big files without
169 ;; In a buffer (which has `font-lock-mode' enabled) which is at least
170 ;; `lazy-lock-minimum-size' characters long, only the visible portion of the
171 ;; buffer will be fontified. Motion around the buffer will fontify those
172 ;; visible portions that were not previous fontified.
174 ;; If stealth fontification is enabled, fontification will occur in invisible
175 ;; parts of the buffer after `lazy-lock-stealth-time' seconds of idle time.
178 ;; Lazy Lock mode does not work efficiently with Outline mode. This is because
179 ;; when in Outline mode, although text may be hidden (not visible in the
180 ;; window), the text is visible to Emacs Lisp code (not surprisingly) and Lazy
181 ;; Lock fontifies it mercilessly. Hopefully this will be fixed one day.
185 ;; Feedback is welcome.
186 ;; To submit a bug report (or make comments) please send to ben@xemacs.org.
191 ;; Well, shouldn't Lazy Lock be as lazy as possible?
192 ;(setq byte-compile-dynamic t byte-compile-dynamic-docstrings t)
194 ;; We use this to preserve or protect things when modifying text properties.
195 (defmacro save-buffer-state (varlist &rest body)
196 "Bind variables according to VARLIST and eval BODY restoring buffer state.
197 Under FSF, it appears that setting text properties respects the read-only-ness
198 of a buffer and marks the buffer as modified, as well as maybe even calling
199 before-change and after-change hooks! We don't do any of these things under
200 XEmacs, at least currently, so this whole macro is basically just a `let'."
201 (` (let* ((,@ (append varlist
202 '(;(modified (buffer-modified-p)) (buffer-undo-list t)
203 ;(inhibit-read-only t)
204 ;;; FSF (inhibit-point-motion-hooks t)
205 ;before-change-functions after-change-functions
206 ;;; FSF deactivate-mark
207 ;buffer-file-name buffer-file-truename
210 ;(when (and (not modified) (buffer-modified-p))
211 ; (set-buffer-modified-p nil))
213 (put 'save-buffer-state 'lisp-indent-function 1)
215 ;; We use this for clarity and speed. Naughty but nice.
216 (defmacro do-while (test &rest body)
217 "(do-while TEST BODY...): eval BODY... and repeat if TEST yields non-nil.
218 The order of execution is thus BODY, TEST, BODY, TEST and so on
219 until TEST returns nil."
220 (` (while (progn (,@ body) (, test)))))
221 (put 'do-while 'lisp-indent-function (get 'while 'lisp-indent-function))
223 ;; We use this for compatibility with a future Emacs.
224 (or (fboundp 'with-temp-message)
225 (defmacro with-temp-message (message &rest body)
226 (let ((current-message (make-symbol "current-message"))
227 (temp-message (make-symbol "with-temp-message")))
228 `(let ((,temp-message ,message)
233 (setq ,current-message (current-message))
234 (message "%s" ,temp-message))
236 (and ,temp-message ,current-message
237 (message "%s" ,current-message))))))))
239 (defvar lazy-lock-window-start-cache (make-hash-table :weakness 'key))
240 (defvar lazy-lock-window-end-cache (make-hash-table :weakness 'key))
241 (defvar lazy-lock-window-buffer-cache (make-hash-table :weakness 'key))
242 (defvar lazy-lock-window-buffer-modiff-cache (make-hash-table :weakness 'key))
243 (defvar lazy-lock-frame-modiff-cache (make-hash-table :weakness 'key))
244 (defvar lazy-lock-text-props-changed-cache (make-hash-table :weakness 'key)
245 "Table of if non-nil, `lazy-lock' text prop changed and we need to wake up.")
247 (defvar lazy-lock-mode nil) ; Whether we are turned on.
248 (defvar lazy-lock-buffers nil) ; For deferral.
249 (defvar lazy-lock-timers (cons nil nil)) ; For deferral and stealth.
251 (defvar lazy-lock-first-unfontify-pos nil
252 "Consider text after this position as contextually unfontified.
253 If nil, contextual fontification is disabled.")
254 (make-variable-buffer-local 'lazy-lock-first-unfontify-pos)
256 (defgroup lazy-lock nil
257 "Lazy-lock customizations"
259 :prefix "lazy-lock-")
262 (defcustom lazy-lock-mode nil
263 "Non nil means `lazy-lock-mode' is on."
265 :require 'lazy-lock ;; which in turn requires font-lock.
267 :initialize 'custom-initialize-default
268 :set '(lambda (var val)
272 (add-hook 'font-lock-mode-hook 'turn-on-lazy-lock))
274 (remove-hook 'font-lock-mode-hook 'turn-on-lazy-lock)))
280 (defcustom lazy-lock-minimum-size 25600
281 "*Minimum size of a buffer for demand-driven fontification.
282 On-demand fontification occurs if the buffer size is greater than this value.
283 If nil, means demand-driven fontification is never performed.
284 If a list, each element should be a cons pair of the form (MAJOR-MODE . SIZE),
285 where MAJOR-MODE is a symbol or t (meaning the default). For example:
286 ((c-mode . 25600) (c++-mode . 25600) (rmail-mode . 1048576))
287 means that the minimum size is 25K for buffers in C or C++ modes, one megabyte
288 for buffers in Rmail mode, and size is irrelevant otherwise.
290 The value of this variable is used when Lazy Lock mode is turned on."
291 :type '(choice (const :tag "none" nil)
292 (integer :tag "size")
293 (repeat :menu-tag "mode specific" :tag "mode specific"
295 (cons :tag "Instance"
298 (symbol :tag "name"))
300 (const :tag "none" nil)
301 (integer :tag "size")))))
304 ;; We don't currently support this in our version of lazy-lock.
305 ;; It's tricky to implement given the way our redisplay support works, and
306 ;; it's not clear it's useful. (jit-lock likewise deletes it entirely)
307 (defcustom lazy-lock-defer-on-the-fly nil
308 "*If non-nil, means fontification after a change should be deferred.
309 If nil, means on-the-fly fontification is performed. This means when changes
310 occur in the buffer, those areas are immediately fontified.
311 If a list, it should be a list of `major-mode' symbol names for which deferred
312 fontification should occur. The sense of the list is negated if it begins with
315 means that on-the-fly fontification is deferred for buffers in C and C++ modes
316 only, and deferral does not occur otherwise.
318 NOTE: Not currently implemented in this version of lazy-lock.
320 The value of this variable is used when Lazy Lock mode is turned on."
321 :type '(choice (const :tag "never" nil)
322 (const :tag "always" t)
323 (set :menu-tag "mode specific" :tag "modes"
325 (const :tag "Except" not)
326 (repeat :inline t (symbol :tag "mode"))))
329 (defcustom lazy-lock-defer-contextually 'syntax-driven
330 "*If non-nil, means deferred fontification should be syntactically true.
331 If nil, means deferred fontification occurs only on those lines modified. This
332 means where modification on a line causes syntactic change on subsequent lines,
333 those subsequent lines are not refontified to reflect their new context.
334 If t, means deferred fontification occurs on those lines modified and all
335 subsequent lines. This means those subsequent lines are refontified to reflect
336 their new syntactic context, either immediately or when scrolling into them.
337 If any other value, e.g., `syntax-driven', means deferred syntactically true
338 fontification occurs only if syntactic fontification is performed using the
339 buffer mode's syntax table, i.e., only if `font-lock-keywords-only' is nil.
341 The value of this variable is used when Lazy Lock mode is turned on."
342 :type '(choice (const :tag "never" nil)
343 (const :tag "always" t)
344 (other :tag "syntax-driven" syntax-driven))
347 (defcustom lazy-lock-defer-time
348 (if (featurep 'lisp-float-type) (/ (float 1) (float 4)) 1)
349 "*Time in seconds to delay before beginning deferred fontification.
350 Deferred fontification occurs if there is no input within this time.
351 If nil, means fontification is never deferred, regardless of the values of the
352 variables `lazy-lock-defer-on-the-fly' and `lazy-lock-defer-contextually'.
354 The value of this variable is used when Lazy Lock mode is turned on."
355 :type '(choice (const :tag "never" nil)
356 (number :tag "seconds"))
359 ;; not by default because it's not stealthy enough -- it can cause
360 ;; annoying and unpredictable delays when it's running and you try to
362 (defcustom lazy-lock-stealth-time nil ;30
363 "*Time in seconds to delay before beginning stealth fontification.
364 Stealth fontification occurs if there is no input within this time.
365 If nil, means stealth fontification is never performed.
367 The value of this variable is used when Lazy Lock mode is turned on."
368 :type '(choice (const :tag "never" nil)
369 (number :tag "seconds"))
372 (defcustom lazy-lock-stealth-lines (if font-lock-maximum-decoration 100 250)
373 "*Maximum size of a chunk of stealth fontification.
374 Each iteration of stealth fontification can fontify this number of lines.
375 To speed up input response during stealth fontification, at the cost of stealth
376 taking longer to fontify, you could reduce the value of this variable."
377 :type '(integer :tag "lines")
380 (defcustom lazy-lock-stealth-load
381 (if (condition-case nil (load-average) (error)) 200)
382 "*Load in percentage above which stealth fontification is suspended.
383 Stealth fontification pauses when the system short-term load average (as
384 returned by the function `load-average' if supported) goes above this level,
385 thus reducing the demand that stealth fontification makes on the system.
386 If nil, means stealth fontification is never suspended.
387 To reduce machine load during stealth fontification, at the cost of stealth
388 taking longer to fontify, you could reduce the value of this variable.
389 See also `lazy-lock-stealth-nice'."
390 :type (if (condition-case nil (load-average) (error))
391 '(choice (const :tag "never" nil)
392 (integer :tag "load"))
393 '(const :format "%t: unsupported\n" nil))
396 (defcustom lazy-lock-stealth-nice
397 (if (featurep 'lisp-float-type) (/ (float 1) (float 8)) 1)
398 "*Time in seconds to pause between chunks of stealth fontification.
399 Each iteration of stealth fontification is separated by this amount of time,
400 thus reducing the demand that stealth fontification makes on the system.
401 If nil, means stealth fontification is never paused.
402 To reduce machine load during stealth fontification, at the cost of stealth
403 taking longer to fontify, you could increase the value of this variable.
404 See also `lazy-lock-stealth-load'."
405 :type '(choice (const :tag "never" nil)
406 (number :tag "seconds"))
409 (defcustom lazy-lock-stealth-verbose
410 (if (featurep 'lisp-float-type)
411 (and (not lazy-lock-defer-contextually) (not (null font-lock-verbose))))
412 "*If non-nil, means stealth fontification should show status messages."
416 (defcustom lazy-lock-walk-windows 'all-frames
417 "*If non-nil, fontify windows other than the selected window.
418 If `all-frames', fontify windows even on other frames.
419 A non-nil value slows down redisplay."
423 (defcustom lazy-lock-mode-line-string nil ; " Lazy"
424 "*String to display in the modeline when `lazy-lock-mode' is active.
425 Set this to nil if you don't want a modeline indicator."
426 :type '(choice string
427 (const :tag "none" nil))
430 ; (defvar lazy-lock-rounding-size 500
431 ; "Round end points of fontified chunks to the nearest multiple of this value.
432 ; Fontifying any amount of text involves some overhead; by increasing the
433 ; size, we minimize this, and by rounding to particular points we help to
434 ; minimize constant refontification in some circumstances when the displayed
435 ; area moves little by little. Setting this too big can cause unnecessary
442 (defun lazy-lock-mode (&optional arg)
443 "Toggle Lazy Lock mode.
444 With arg, turn Lazy Lock mode on if and only if arg is positive. Enable it
445 automatically in your `~/.emacs' by:
447 (setq font-lock-support-mode 'lazy-lock-mode)
449 When Lazy Lock mode is enabled, fontification can be lazy in a number of ways:
451 - Demand-driven buffer fontification if `lazy-lock-minimum-size' is non-nil.
452 This means initial fontification does not occur if the buffer is greater than
453 `lazy-lock-minimum-size' characters in length. Instead, fontification occurs
454 when necessary, such as when scrolling through the buffer would otherwise
455 reveal unfontified areas. This is useful if buffer fontification is too slow
458 - Deferred on-the-fly fontification if `lazy-lock-defer-on-the-fly' is non-nil.
459 This means on-the-fly fontification does not occur as you type. Instead,
460 fontification is deferred until after `lazy-lock-defer-time' seconds of Emacs
461 idle time, while Emacs remains idle. This is useful if fontification is too
462 slow to keep up with your typing.
464 - Deferred context fontification if `lazy-lock-defer-contextually' is non-nil.
465 This means fontification updates the buffer corresponding to true syntactic
466 context, after `lazy-lock-defer-time' seconds of Emacs idle time, while Emacs
467 remains idle. Otherwise, fontification occurs on modified lines only, and
468 subsequent lines can remain fontified corresponding to previous syntactic
469 contexts. This is useful where strings or comments span lines.
471 - Stealthy buffer fontification if `lazy-lock-stealth-time' is non-nil.
472 This means remaining unfontified areas of buffers are fontified if Emacs has
473 been idle for `lazy-lock-stealth-time' seconds, while Emacs remains idle.
474 This is useful if any buffer has any deferred fontification.
476 Basic Font Lock mode on-the-fly fontification behaviour fontifies modified
477 lines only. Thus, if `lazy-lock-defer-contextually' is non-nil, Lazy Lock mode
478 on-the-fly fontification may fontify differently, albeit correctly. In any
479 event, to refontify some lines you can use \\[font-lock-fontify-block].
481 Stealth fontification only occurs while the system remains unloaded.
482 If the system load rises above `lazy-lock-stealth-load' percent, stealth
483 fontification is suspended. Stealth fontification intensity is controlled via
484 the variable `lazy-lock-stealth-nice' and `lazy-lock-stealth-lines', and
485 verbosity is controlled via the variable `lazy-lock-stealth-verbose'."
487 (let* ((was-on lazy-lock-mode)
488 (now-on (unless (memq 'lazy-lock-mode font-lock-inhibit-thing-lock)
489 (if arg (> (prefix-numeric-value arg) 0) (not was-on)))))
490 (cond ((and now-on (not font-lock-mode))
491 ;; Turned on `lazy-lock-mode' rather than `font-lock-mode'.
492 (let ((font-lock-support-mode 'lazy-lock-mode))
495 ;; Turn ourselves on.
496 (set (make-local-variable 'lazy-lock-mode) t)
499 ;; Turn ourselves off.
500 (set (make-local-variable 'lazy-lock-mode) nil)
501 (lazy-lock-unstall)))))
504 (defun turn-on-lazy-lock ()
505 "Unconditionally turn on Lazy Lock mode."
508 ;; Copied from font-lock-value-in-major-mode (not available for 21.4 users)
509 (defun lazy-lock-value-in-major-mode (alist)
510 "Return value in ALIST for `major-mode', or ALIST if it is not an alist.
511 Structure is ((MAJOR-MODE . VALUE) ...) where MAJOR-MODE may be t."
513 (cdr (or (assq major-mode alist) (assq t alist)))
516 (defun lazy-lock-install ()
517 (let ((min-size (lazy-lock-value-in-major-mode lazy-lock-minimum-size))
518 (defer-change (and lazy-lock-defer-time lazy-lock-defer-on-the-fly))
519 (defer-context (and lazy-lock-defer-time lazy-lock-defer-contextually
520 (or (eq lazy-lock-defer-contextually t)
521 (null font-lock-keywords-only)))))
523 ;; Tell Font Lock whether Lazy Lock will do fontification.
524 (make-local-variable 'font-lock-fontified)
525 (setq font-lock-fontified (and min-size (>= (buffer-size) min-size)))
527 ;; Add the text properties and fontify.
528 (if (not font-lock-fontified)
529 (lazy-lock-after-fontify-buffer)
530 ;; FSF 21.2 here explicitly fontifies the visible window. Not
531 ;; necessary in XEmacs. You could say this is yet another hokey
532 ;; hook-in required to get things working.
535 ;; Initialize deferred contextual fontification if requested.
537 (setq lazy-lock-first-unfontify-pos
538 (or lazy-lock-first-unfontify-pos (point-max))))
541 ;; Add the fontification hooks.
542 (lazy-lock-install-hooks
544 (cond ((eq (car-safe defer-change) 'not)
545 (not (memq major-mode (cdr defer-change))))
546 ((listp defer-change)
547 (memq major-mode defer-change))
552 ;; Add the fontification timers.
553 (lazy-lock-install-timers
554 (if (or defer-change defer-context) lazy-lock-defer-time)
555 lazy-lock-stealth-time)))
557 ;; XEmacs DEFER-SCROLL argument deleted and all related code.
558 (defun lazy-lock-install-hooks (fontifying defer-change defer-context)
559 ;; Make sure our hooks are correct.
560 (remove-hook 'pre-idle-hook 'lazy-lock-pre-idle-fontify-windows)
561 ;; Make sure our hooks are at the end. Font-lock in XEmacs installs its
562 ;; own pre-idle-hook to implement deferral (#### something that should
563 ;; really be merged with this file; or more likely, lazy-lock in its
564 ;; entirety should be merged into font-lock). We *DO NOT* make
565 ;; pre-idle-hook be local. It needs to be able to update the unfontified
566 ;; regions of *all* frames. If you turn off lazy lock in your particular
567 ;; buffer and set a local value to nil, none of the other windows will
569 (add-hook 'pre-idle-hook 'lazy-lock-pre-idle-fontify-windows t)
571 ;; Replace Font Lock mode hook.
572 (make-local-hook 'after-change-functions)
573 (remove-hook 'after-change-functions 'font-lock-after-change-function t)
574 (add-hook 'after-change-functions 'lazy-lock-after-change t t)
576 ;; FSF 21.2: Lots and lots of hooks here. Hooks for `outline', hooks for
577 ;; `hideshow', hooks for redisplay-end-triggers, window-size-changed, and
578 ;; window-scroll, before-change-functions needed to set up the end
579 ;; triggers, multiple different versions of the after-change hook. All
580 ;; GONE! VANQUISHED! DEAD! DESTROYED! Thank God. We have one
581 ;; after-change hook and the rest are unnecessary in our model.
584 (defun lazy-lock-install-timers (dtime stime)
585 ;; Schedule or re-schedule the deferral and stealth timers.
586 ;; The layout of `lazy-lock-timers' is:
587 ;; ((DEFER-TIME . DEFER-TIMER) (STEALTH-TIME . STEALTH-TIMER)
588 ;; If an idle timeout has changed, cancel the existing idle timer (if there
589 ;; is one) and schedule a new one (if the new idle timeout is non-nil).
590 (unless (eq dtime (car (car lazy-lock-timers)))
591 (let ((defer (car lazy-lock-timers)))
593 (delete-itimer (cdr defer)))
594 (setcar lazy-lock-timers (cons dtime (and dtime
595 (run-with-idle-timer dtime t 'lazy-lock-fontify-after-defer))))))
596 (unless (eq stime (car (cdr lazy-lock-timers)))
597 (let ((stealth (cdr lazy-lock-timers)))
599 (delete-itimer (cdr stealth)))
600 (setcdr lazy-lock-timers (cons stime (and stime
601 (run-with-idle-timer stime t 'lazy-lock-fontify-after-idle)))))))
603 (defun lazy-lock-unstall ()
605 ;; If Font Lock mode is still enabled, make sure that the buffer is
606 ;; fontified, and reinstall its hook. We must do this first.
608 (when (lazy-lock-unfontified-p)
609 (let ((verbose (if (numberp font-lock-verbose)
610 (> (buffer-size) font-lock-verbose)
614 (format "Fontifying %s..." (buffer-name)))
615 ;; Make sure we fontify etc. in the whole buffer.
618 (lazy-lock-fontify-region (point-min) (point-max))))))
619 (add-hook 'after-change-functions 'font-lock-after-change-function t t))
621 ;; Remove the text properties.
622 (lazy-lock-after-unfontify-buffer)
624 ;; Remove the fontification hooks.
625 (remove-hook 'after-change-functions 'lazy-lock-after-change t)
628 ;; use put-nonduplicable-text-property to avoid unfriendly behavior
629 ;; when doing undo, etc. We really don't want syntax-highlighting text
630 ;; properties copied into strings or tracked by undo.
633 ;; [[ #### If start-open and end-open really behaved like they are supposed to,
634 ;; we wouldn't really need this. I kind of fixed them up, but there's still
635 ;; a bug -- inserting text into the middle of a region of
636 ;; (start-open t end-open t) text should cause it not to inherit, but it
639 ;; They do behave correctly now. #### What should we be doing? --ben
641 (defalias 'lazy-lock-put-text-property 'put-nonduplicable-text-property)
646 ;; 1. Text that needs to be fontified is done through `pre-idle-hook',
647 ;; which runs directly before redisplay happens. It is a single hook,
648 ;; so the hook must look through all the windows/frames/etc to see what
649 ;; needs to be done. This is not that elegant of a solution, but it
650 ;; works and avoids the horrible hacked-up multi-hooks in FSF's
651 ;; lazy-lock. Gerd's jit-lock does it right, and #### we should
652 ;; implement the same support.
654 ;; lazy-lock optimization:
656 ;; pre-idle-hook is called an awful lot -- pretty much every time the
657 ;; mouse moves or a timeout expires, for example. On Linux (sometimes),
658 ;; IRIX 5.x, and Solaris 2.something, it happens every 1/4 of a second
659 ;; due to the 1/4-second timers installed to compensate for various
660 ;; operating system deficiencies in the handling of SIGIO and SIGCHLD.
661 ;; (Those timers cause a cycle of the event loop. They don't necessarily
662 ;; have to, but rewriting to avoid this is fairly tricky and requires
663 ;; having significant amounts of code called from signal handlers, which
664 ;; (despite that fact that FSF Emacs reads its X input during a signal
665 ;; handler ?!), is almost always a bad idea -- it's extremely easy to
666 ;; introduce race conditions, which are very hard to track down.
668 ;; So to improve things, I added `frame-modified-tick'. This is an
669 ;; internal counter that gets ticked any time that any internal
670 ;; redisplay variable gets ticked. If `frame-modified-tick' is
671 ;; the same as the last time we checked, it means that redisplay will
672 ;; do absolutely nothing when encountering this frame, and thus we
673 ;; can skip out immediately. This happens when the 1/4-second timer
674 ;; fires while we're idle, or if we just move the mouse. (Moving
675 ;; around in a buffer changes `frame-modified-tick' because the
676 ;; internal redisplay variable "point_changed" gets ticked. We could
677 ;; easily improve things further by adding more tick counters, mirroring
678 ;; more closely the internal redisplay counters -- e.g. if we had
679 ;; another counter that didn't get ticked when point moved, we could
680 ;; tell if anything was going to happen by seeing if point is within
681 ;; window-start and window-end, since we know that redisplay will
682 ;; only do a window-scroll if it's not. (If window-start or window-end
683 ;; or window-buffer or anything else changed, windows_changed or
684 ;; some other variable will get ticked.))
686 ;; Also, it's wise to try and avoid things that cons. Avoiding
687 ;; `save-window-excursion', as we do, is definitely a major win because
688 ;; that's a heavy-duty consing function. In fact, we do no consing at all
689 ;; until the frame-modified tick goes off, and even then the only potential
690 ;; consing we do is save-excursion; but in fact, that is consless too.
692 (defun lazy-lock-pre-idle-fontify-windows ()
693 ; (debug-print (gethash (selected-frame) lazy-lock-frame-modiff-cache))
694 ; (debug-print (frame-modified-tick (selected-frame)))
695 (unless nil ;(memq this-command lazy-lock-ignore-commands)
696 ;; Do the visible parts of the buffer(s), i.e., the window(s).
697 ;(dp "pre-idle-called")
698 (if (or (not lazy-lock-walk-windows)
699 (and (eq lazy-lock-walk-windows t) (one-window-p t)))
700 (or (window-minibuffer-p (selected-window))
701 (lazy-lock-fontify-window (selected-window)))
702 (if (eq lazy-lock-walk-windows t)
703 (lazy-lock-maybe-fontify-frame (selected-frame))
704 ;; Visit all visible non-minibuffer-only frames on the selected device.
705 ;; This is harder than it looks, since the `next-frame'
706 ;; interface is error-prone - finding the starting frame is hard.
707 (catch 'lazy-lock-frame-loop-done
708 (let* ((starting-frame (selected-frame))
709 (frame starting-frame))
710 (when (or (not (frame-visible-p frame))
711 (frame-minibuffer-only-p frame))
712 ;; starting-frame not suitable.
713 (setq starting-frame (next-frame starting-frame 'visible-nomini))
714 (when (eq starting-frame frame)
715 ;; No suitable frames.
716 (throw 'lazy-lock-frame-loop-done t))
717 (setq frame starting-frame))
719 (lazy-lock-maybe-fontify-frame frame)
720 (setq frame (next-frame frame 'visible-nomini))
721 (when (eq frame starting-frame)
722 (throw 'lazy-lock-frame-loop-done t)))))))))
724 (defun lazy-lock-maybe-fontify-frame (frame)
725 ;; Fontify the given frame if we need to. We first check the
726 ;; appropriate frame-modified-tick to avoid changing global state.
727 ;(dp "fontify-frame %s" frame)
728 (let ((tick (frame-modified-tick frame)))
730 (unless (eq tick (gethash frame lazy-lock-frame-modiff-cache))
731 (puthash frame tick lazy-lock-frame-modiff-cache)
732 ;; We have to select the frame due to a bug in walk-windows in XEmacs
734 (with-selected-frame frame
735 (walk-windows #'lazy-lock-fontify-window 'no-minibuf frame)))))
737 ;; 2. Modified text must be marked as unfontified so it can be identified and
738 ;; fontified later when Emacs is idle. Deferral occurs by adding one of
739 ;; `lazy-lock-fontify-*-after-change' (for on-the-fly fontification) or
740 ;; `lazy-lock-defer-*-after-change' (for deferred fontification) to the
741 ;; hook `after-change-functions'.
743 ;; Modeled after jit-lock-after-change (21.2).
744 (defun lazy-lock-after-change (beg end old-len)
745 "Mark the rest of the buffer as not fontified after a change.
746 Installed on `after-change-functions'.
747 BEG and END are the start and end of the changed text. OLD-LEN
748 is the pre-change length.
749 This function ensures that lines following the change will be refontified
750 in case the syntax of those lines has changed. Refontification
751 will take place when text is fontified stealthily."
754 (save-buffer-state nil
755 ;; It's important that the `fontified' property be set from the
756 ;; beginning of the line, else font-lock will properly change the
757 ;; text's face, but the display will have been done already and will
758 ;; be inconsistent with the buffer's content.
760 (setq beg (point-at-bol))
762 ;; If we're in text that matches a multi-line font-lock pattern,
763 ;; make sure the whole text will be redisplayed.
764 (when (get-text-property beg 'font-lock-multiline)
765 (setq beg (or (previous-single-property-change
766 beg 'font-lock-multiline)
769 ;; Make sure we change at least one char (in case of deletions).
770 (setq end (min (max end (1+ beg)) (point-max)))
771 ;; Request refontification of changed region right away.
772 ;;(when (not lazy-lock-defer-on-the-fly) #### Doesn't work.
773 ;;We have to arrange a way for the inserted chunk to definitely be
774 ;;fontified in the defer function. There may be a whole bunch of
775 ;;chunks inserted before the defer function is run. We'd probably
776 ;;need text properties with a different name from `lazy-lock' to
777 ;;note all such regions, and maybe keep a minimum and maximum, and
778 ;;it would make the logic all clogged up. --ben
780 (font-lock-after-change-function beg end old-len)
781 (lazy-lock-put-text-property beg end 'lazy-lock nil)))
782 (unless (memq (current-buffer) lazy-lock-buffers)
783 (push (current-buffer) lazy-lock-buffers))
784 ;; Mark the change for deferred contextual refontification.
785 (when lazy-lock-first-unfontify-pos
786 (setq lazy-lock-first-unfontify-pos
787 (min lazy-lock-first-unfontify-pos beg)))
790 ;; 3. Deferred fontification and stealth fontification are done from these two
791 ;; functions. They are set up as Idle Timers.
793 (defun lazy-lock-fontify-after-defer ()
794 ;; Called from `timer-idle-list'.
795 ;; Fontify all windows where deferral has occurred for its buffer.
797 ;(dp "fontify-after-defer")
799 (while (and lazy-lock-buffers (not (input-pending-p)))
800 (let ((buffer (car lazy-lock-buffers)) windows)
801 ;; Paranoia: check that the buffer is still live and Lazy Lock mode on.
802 (when (buffer-live-p buffer)
805 ;; Perform deferred unfontification, if any.
806 (when lazy-lock-first-unfontify-pos
809 (when (and (>= lazy-lock-first-unfontify-pos (point-min))
810 (< lazy-lock-first-unfontify-pos (point-max)))
811 (save-buffer-state nil
812 (lazy-lock-put-text-property lazy-lock-first-unfontify-pos
813 (point-max) 'lazy-lock nil))
814 (setq lazy-lock-first-unfontify-pos (point-max))
817 (setq windows (get-buffer-window-list buffer 'nomini t))
819 (puthash (car windows) t lazy-lock-text-props-changed-cache)
820 ;; #### The following isn't necessary. Does it speed up the
821 ;; response time? Or slow down the overall performance?
822 (lazy-lock-fontify-window (car windows))
823 (setq windows (cdr windows)))))
824 (setq lazy-lock-buffers (cdr lazy-lock-buffers))))))
826 (defun lazy-lock-fontify-after-idle ()
827 ;; Called from `timer-idle-list'.
828 ;; Fontify all buffers that need it, stealthily while idle.
829 (unless (or executing-kbd-macro (window-minibuffer-p (selected-window)))
830 ;; Loop over all buffers, fontify stealthily for each if necessary.
831 (let ((buffers (buffer-list)) (continue t)
832 message ;; FSF 21.2 message-log-max minibuffer-auto-raise
835 (do-while (and buffers continue)
836 (set-buffer (car buffers))
837 (if (not (and lazy-lock-mode (lazy-lock-unfontified-p)))
838 (setq continue (not (input-pending-p)))
839 ;; Fontify regions in this buffer while there is no input.
841 (when lazy-lock-stealth-verbose
842 "Fontifying stealthily...")
843 (do-while (and (lazy-lock-unfontified-p) continue)
844 (if (and lazy-lock-stealth-load
845 (> (car (load-average)) lazy-lock-stealth-load))
846 ;; Wait a while before continuing with the loop.
849 (message "Fontifying stealthily...suspended")
851 (setq continue (sit-for (or lazy-lock-stealth-time 30))))
853 (when lazy-lock-stealth-verbose
855 (message "Fontifying stealthily... %2d%% of %s"
856 (lazy-lock-percent-fontified) (buffer-name))
857 (message "Fontifying stealthily...")
859 ;; Current buffer may have changed during `sit-for'.
860 (set-buffer (car buffers))
861 (lazy-lock-fontify-chunk)
862 (setq continue (sit-for (or lazy-lock-stealth-nice 0)))))))
863 (setq buffers (cdr buffers)))))))
865 ;; 4. Special circumstances.
867 (defun lazy-lock-after-fontify-buffer ()
868 ;; Called from `font-lock-after-fontify-buffer'.
869 ;; Mark the current buffer as fontified.
870 ;; FSF: [[This is a conspiracy hack between lazy-lock.el and font-lock.el.]]
871 (save-buffer-state nil
872 (lazy-lock-put-text-property (point-min) (point-max)
875 (defun lazy-lock-after-unfontify-buffer ()
876 ;; Called from `font-lock-after-unfontify-buffer'.
877 ;; Mark the current buffer as unfontified.
878 ;; FSF: [[This is a conspiracy hack between lazy-lock.el and font-lock.el.]]
879 (save-buffer-state nil
880 (remove-text-properties (point-min) (point-max) '(lazy-lock nil))))
884 ;; Functions for fontification:
886 ;; If packages want to ensure that some region of the buffer is fontified, they
887 ;; should use this function. For an example, see ps-print.el.
889 (defun lazy-lock-fontify-region (beg end)
890 ;; Fontify between BEG and END, where necessary, in the current buffer.
893 (when (setq beg (text-property-any beg end 'lazy-lock nil))
897 ;; Ensure syntactic fontification is always correct.
898 ;; Do NOT bind font-lock-beginning-of-syntax-function because
899 ;; that leads to catastrophic behavior when scrolling backwards
900 ;; from the end of a large buffer -- parse-partial-sexp will start
901 ;; at (point-min) each time!
902 (;; FSF 21.2 font-lock-beginning-of-syntax-function
904 ;; Find successive unfontified regions between BEG and END.
906 ;; FSF has just BEG as the condition. We have a bug in
907 ;; text-property-any in 21.4 when BEG > END so we need the
909 (do-while (and beg (< beg end))
910 (setq next (or (text-property-any beg end 'lazy-lock t) end))
911 ;; Make sure the region end points are at beginning of
921 ;; Fontify the region, then flag it as fontified.
922 (font-lock-fontify-region beg next)
923 (lazy-lock-put-text-property beg next 'lazy-lock t)
924 (setq beg (text-property-any next end 'lazy-lock
926 ((error quit) (message "Fontifying region...%s" data))
929 (defun lazy-lock-fontify-chunk ()
930 ;; Fontify the nearest chunk, for stealth, in the current buffer.
931 (let (;(inhibit-point-motion-hooks t)
936 ;; Move to end of line in case the character at point is not fontified.
938 ;; Find where the previous (next) unfontified regions end (begin).
939 (let ((prev (previous-single-property-change (point) 'lazy-lock))
940 (next (text-property-any (point) (point-max) 'lazy-lock nil)))
941 ;; Fontify from the nearest unfontified position.
942 (if (or (null prev) (and next (< (- next (point)) (- (point) prev))))
943 ;; The next, or neither, region is the nearest not fontified.
944 (lazy-lock-fontify-region
945 (progn (goto-char (or next (point-min)))
948 (progn (goto-char (or next (point-min)))
949 (forward-line lazy-lock-stealth-lines)
951 ;; The previous region is the nearest not fontified.
952 (lazy-lock-fontify-region
953 (progn (goto-char prev)
954 (forward-line (- lazy-lock-stealth-lines))
956 (progn (goto-char prev)
960 (defun lazy-lock-vertical-motion-value (window pos lines)
961 "Move LINES lines down from POS in the WINDOW's buffer and return pos."
962 (let ((buffer (window-buffer window)))
963 (if (eq window (selected-window))
964 (with-current-buffer buffer
967 (vertical-motion lines window)
969 ;; In this case, vertical-motion sets window's point, not window's
971 (let ((winp (window-point window)))
974 (set-window-point window pos)
975 (vertical-motion lines window)
976 (window-point window))
977 (set-window-point window winp)))))
979 (defun lazy-lock-fontify-window (window)
980 ;; Fontify the given window if we need to. We first check the
981 ;; buffer-local value of lazy-lock-mode to make sure we should do
982 ;; the more accurate (but semi-expensive) checks below.
983 ;(dp "fontifying %s" window)
984 (let ((buffer (window-buffer window))
986 (check-text-props (gethash window lazy-lock-text-props-changed-cache)))
987 (when (symbol-value-in-buffer 'lazy-lock-mode buffer)
988 (with-current-buffer buffer
989 (let ((ws (window-start window))
990 ;; use the GUARANTEE option on window-end to be accurate. this
991 ;; also avoids the need to fontify an over-large area to avoid
992 ;; leaving unfontified areas visible. Unfortunately, it seems
993 ;; that by just using the return value from window-end, the
994 ;; clipped line at the bottom of the window doesn't get
995 ;; fontified. So we have to go down from there. It turns out
996 ;; that an arg of 2 is the minimum that will work -- and 0
997 ;; actually goes up a line! #### Another bug in vertical-motion?
998 (we ;(window-end window t))
999 (lazy-lock-vertical-motion-value window
1000 (window-end window t) 2))
1001 (point (point buffer))
1002 (modiff (buffer-modified-tick buffer)))
1003 (cond ((or (< point ws) (> point we))
1004 (setq we-are-screwed t check-text-props t))
1005 ((or check-text-props
1009 (gethash window lazy-lock-window-buffer-cache))
1012 lazy-lock-window-buffer-modiff-cache))
1013 (eq ws (gethash window lazy-lock-window-start-cache))
1014 (eq we (gethash window lazy-lock-window-end-cache)))))
1015 (setq check-text-props t)))
1016 (when we-are-screwed
1018 (lazy-lock-vertical-motion-value window (window-point window)
1019 (- (window-height window))))
1021 (lazy-lock-vertical-motion-value window (window-point window)
1022 (window-height window)))
1023 ;; #### Not currently implemented. Perhaps not necessary.
1024 ; (setq ws (* lazy-lock-rounding-size
1025 ; (/ ws lazy-lock-rounding-size))
1026 ; we (* lazy-lock-rounding-size
1027 ; (/ (+ we (1- lazy-lock-rounding-size))
1028 ; lazy-lock-rounding-size)))
1029 ; (setq ws (max (point-min buffer) ws)
1030 ; we (min (point-max buffer) we)))
1032 (when check-text-props
1033 (puthash window buffer lazy-lock-window-buffer-cache)
1034 (puthash window modiff lazy-lock-window-buffer-modiff-cache)
1035 (puthash window ws lazy-lock-window-start-cache)
1036 (puthash window we lazy-lock-window-end-cache)
1037 (lazy-lock-fontify-region ws we)
1040 (defun lazy-lock-unfontified-p ()
1041 ;; Return non-nil if there is anywhere still to be fontified.
1044 (text-property-any (point-min) (point-max) 'lazy-lock nil)))
1046 (defun lazy-lock-percent-fontified ()
1047 ;; Return the percentage (of characters) of the buffer that are fontified.
1050 (let ((beg (point-min)) (size 0) next)
1051 ;; Find where the next fontified region begins.
1052 (while (setq beg (text-property-any beg (point-max) 'lazy-lock t))
1053 (setq next (or (text-property-any beg (point-max) 'lazy-lock nil)
1055 (incf size (- next beg))
1057 ;; Float because using integer multiplication will frequently overflow.
1058 (truncate (* (/ (float size) (point-max)) 100)))))
1060 ;; Install ourselves:
1062 (add-hook 'font-lock-after-fontify-buffer-hook
1063 'lazy-lock-after-fontify-buffer)
1065 (add-hook 'font-lock-after-unfontify-buffer-hook
1066 'lazy-lock-after-unfontify-buffer)
1068 ;; XEmacs change: do it the right way. This works with modeline mousing.
1070 (add-minor-mode 'lazy-lock-mode 'lazy-lock-mode-line-string)
1072 ;; Provide ourselves:
1074 (provide 'lazy-lock)
1076 ;;; lazy-lock.el ends here