]> code.delx.au - gnu-emacs/blob - lisp/lazy-lock.el
(a) split lazy-lock-defer-time into lazy-lock-defer-time and lazy-lock-defer-on-the...
[gnu-emacs] / lisp / lazy-lock.el
1 ;;; lazy-lock.el --- Lazy demand-driven fontification for fast Font Lock mode.
2
3 ;; Copyright (C) 1994, 1995, 1996 Free Software Foundation, Inc.
4
5 ;; Author: Simon Marshall <simon@gnu.ai.mit.edu>
6 ;; Keywords: faces files
7 ;; Version: 2.07
8
9 ;;; This file is part of GNU Emacs.
10
11 ;; GNU Emacs is free software; you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation; either version 2, or (at your option)
14 ;; any later version.
15
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
20
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING. If not, write to the
23 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24 ;; Boston, MA 02111-1307, USA.
25
26 ;;; Commentary:
27
28 ;; Lazy Lock mode is a Font Lock support mode.
29 ;; It makes visiting buffers in Font Lock mode faster by making fontification
30 ;; be demand-driven, deferred and stealthy, so that fontification only occurs
31 ;; when, and where, necessary.
32 ;;
33 ;; See caveats and feedback below.
34 ;; See also the fast-lock package. (But don't use them at the same time!)
35
36 ;; Installation:
37 ;;
38 ;; Put in your ~/.emacs:
39 ;;
40 ;; (setq font-lock-support-mode 'lazy-lock-mode)
41 ;;
42 ;; Start up a new Emacs and use font-lock as usual (except that you can use the
43 ;; so-called "gaudier" fontification regexps on big files without frustration).
44 ;;
45 ;; In a buffer (which has `font-lock-mode' enabled) which is at least
46 ;; `lazy-lock-minimum-size' characters long, buffer fontification will not
47 ;; occur and only the visible portion of the buffer will be fontified. Motion
48 ;; around the buffer will fontify those visible portions not previously
49 ;; fontified. If stealth fontification is enabled, buffer fontification will
50 ;; occur in invisible parts of the buffer after `lazy-lock-stealth-time'
51 ;; seconds of idle time. If on-the-fly fontification is deferred, on-the-fly
52 ;; fontification will occur after `lazy-lock-defer-time' seconds of idle time.
53
54 ;; User-visible differences with version 1:
55 ;;
56 ;; - Version 2 can defer on-the-fly fontification. Therefore you need not, and
57 ;; should not, use defer-lock.el with this version of lazy-lock.el.
58 ;;
59 ;; A number of variables have changed meaning:
60 ;;
61 ;; - A value of nil for the variable `lazy-lock-minimum-size' means never turn
62 ;; on demand-driven fontification. In version 1 this meant always turn on
63 ;; demand-driven fontification. If you really want demand-driven fontification
64 ;; regardless of buffer size, set this variable to 0.
65 ;;
66 ;; - The variable `lazy-lock-stealth-lines' cannot have a nil value. In
67 ;; version 1 this meant use `window-height' as the maximum number of lines to
68 ;; fontify as a stealth chunk. This makes no sense; stealth fontification is
69 ;; of a buffer, not a window.
70
71 ;; Implementation differences with version 1:
72 ;;
73 ;; - Version 1 of lazy-lock.el is a bit of a hack. Version 1 demand-driven
74 ;; fontification, the core feature of lazy-lock.el, is implemented by placing a
75 ;; function on `post-command-hook'. This function fontifies where necessary,
76 ;; i.e., where a window scroll has occurred. However, there are a number of
77 ;; problems with using `post-command-hook':
78 ;;
79 ;; (a) As the name suggests, `post-command-hook' is run after every command,
80 ;; i.e., frequently and regardless of whether scrolling has occurred.
81 ;; (b) Scrolling can occur during a command, when `post-command-hook' is not
82 ;; run, i.e., it is not necessarily run after scrolling has occurred.
83 ;; (c) When `post-command-hook' is run, there is nothing to suggest where
84 ;; scrolling might have occurred, i.e., which windows have scrolled.
85 ;;
86 ;; Thus lazy-lock.el's function is called almost as often as possible, usually
87 ;; when it need not be called, yet it is not always called when it is needed.
88 ;; Also, lazy-lock.el's function must check each window to see if a scroll has
89 ;; occurred there. Worse still, lazy-lock.el's function must fontify a region
90 ;; twice as large as necessary to make sure the window is completely fontified.
91 ;; Basically, `post-command-hook' is completely inappropriate for lazy-lock.el.
92 ;;
93 ;; Ideally, we want to attach lazy-lock.el's function to a hook that is run
94 ;; only when scrolling occurs, e.g., `window-start' has changed, and tells us
95 ;; as much information as we need, i.e., the window and its new buffer region.
96 ;; Richard Stallman implemented a `window-scroll-functions' for Emacs 19.30.
97 ;; Functions on it are run when `window-start' has changed, and are supplied
98 ;; with the window and the window's new `window-start' position. (It would be
99 ;; better if it also supplied the window's new `window-end' position, but that
100 ;; is calculated as part of the redisplay process, and the functions on
101 ;; `window-scroll-functions' are run before redisplay has finished.) Thus, the
102 ;; hook deals with the above problems (a), (b) and (c).
103 ;;
104 ;; If only life was that easy. Version 2 demand-driven fontification is mostly
105 ;; implemented by placing a function on `window-scroll-functions'. However,
106 ;; not all scrolling occurs when `window-start' has changed. A change in
107 ;; window size, e.g., via C-x 1, or a significant deletion, e.g., of a number
108 ;; of lines, causes text previously invisible (i.e., after `window-end') to
109 ;; become visible without changing `window-start'. Arguably, these events are
110 ;; not scrolling events, but fontification must occur for lazy-lock.el to work.
111 ;; Hooks `window-size-change-functions' and `redisplay-end-trigger-functions'
112 ;; were added for these circumstances.
113 ;;
114 ;; Ben Wing thinks these hooks are "horribly horribly kludgy", and implemented
115 ;; a `pre-idle-hook', a `mother-of-all-post-command-hooks', for XEmacs 19.14.
116 ;; He then hacked up a version 1 lazy-lock.el to use `pre-idle-hook' rather
117 ;; than `post-command-hook'. Whereas functions on `post-command-hook' are
118 ;; called almost as often as possible, functions on `pre-idle-hook' really are
119 ;; called as often as possible, even when the mouse moves and, on some systems,
120 ;; while XEmacs is idle. Thus, the hook deals with the above problem (b), but
121 ;; unfortunately it makes (a) worse and does not address (c) at all.
122 ;;
123 ;; I freely admit that `redisplay-end-trigger-functions' and, to a much lesser
124 ;; extent, `window-size-change-functions' are not pretty. However, I feel that
125 ;; a `window-scroll-functions' feature is cleaner than a `pre-idle-hook', and
126 ;; the result is faster and smaller, less intrusive and more targeted, code.
127 ;; Since `pre-idle-hook' is pretty much like `post-command-hook', there is no
128 ;; point in making this version of lazy-lock.el work with it. Anyway, that's
129 ;; Lit 30 of my humble opinion.
130 ;;
131 ;; - Version 1 stealth fontification is also implemented by placing a function
132 ;; on `post-command-hook'. This function waits for a given amount of time,
133 ;; and, if Emacs remains idle, fontifies where necessary. Again, there are a
134 ;; number of problems with using `post-command-hook':
135 ;;
136 ;; (a) Functions on `post-command-hook' are run sequentially, so this function
137 ;; can interfere with other functions on the hook, and vice versa.
138 ;; (b) This function waits for a given amount of time, so it can interfere with
139 ;; various features that are dealt with by Emacs after a command, e.g.,
140 ;; region highlighting, asynchronous updating and keystroke echoing.
141 ;; (c) Fontification may be required during a command, when `post-command-hook'
142 ;; is not run. (Version 2 deferred fontification only.)
143 ;;
144 ;; Again, `post-command-hook' is completely inappropriate for lazy-lock.el.
145 ;; Richard Stallman and Morten Welinder implemented internal Timers and Idle
146 ;; Timers for Emacs 19.31. Functions can be run independently at given times
147 ;; or after given amounts of idle time. Thus, the feature deals with the above
148 ;; problems (a), (b) and (c). Version 2 deferral and stealth are implemented
149 ;; by functions on Idle Timers. (A function on XEmacs' `pre-idle-hook' is
150 ;; similar to an Emacs Idle Timer function with a fixed zero second timeout.)
151
152 ;; Caveats:
153 ;;
154 ;; Lazy Lock mode does not work efficiently with Outline mode.
155 ;; This is because when in Outline mode, although text may be not visible to
156 ;; you in the window, the text is visible to Emacs Lisp code (not surprisingly)
157 ;; and Lazy Lock fontifies it mercilessly. Maybe it will be fixed one day.
158 ;;
159 ;; Because buffer text is not necessarily fontified, other packages that expect
160 ;; buffer text to be fontified in Font Lock mode either might not work as
161 ;; expected, or might not display buffer text as expected. An example of the
162 ;; latter is `occur', which copies lines of buffer text into another buffer.
163 ;;
164 ;; In Emacs 19.30, Lazy Lock mode does not ensure that an existing buffer is
165 ;; fontified if it is made visible via a minibuffer-less command that replaces
166 ;; an existing window's buffer (e.g., via the Buffers menu). Upgrade!
167 ;;
168 ;; In Emacs 19.30, Lazy Lock mode does not work well with Transient Mark mode
169 ;; or modes based on Comint mode (e.g., Shell mode), and also interferes with
170 ;; the echoing of keystrokes in the minibuffer. This is because of the way
171 ;; deferral and stealth have to be implemented for Emacs 19.30. Upgrade!
172 ;;
173 ;; Currently XEmacs does not have the features to support this version of
174 ;; lazy-lock.el. Maybe it will one day.
175 \f
176 ;; History:
177 ;;
178 ;; 1.15--2.00:
179 ;; - Rewrite for Emacs 19.30 and the features rms added to support lazy-lock.el
180 ;; so that it could work correctly and efficiently.
181 ;; - Many thanks to those who reported bugs, fixed bugs, made suggestions or
182 ;; otherwise contributed in the version 1 cycle; Jari Aalto, Kevin Broadey,
183 ;; Ulrik Dickow, Bill Dubuque, Bob Glickstein, Boris Goldowsky,
184 ;; Jonas Jarnestrom, David Karr, Michael Kifer, Erik Naggum, Rick Sladkey,
185 ;; Jim Thompson, Ben Wing, Ilya Zakharevich, and Richard Stallman.
186 ;; 2.00--2.01:
187 ;; - Made `lazy-lock-fontify-after-command' always `sit-for' and so redisplay
188 ;; - Use `buffer-name' not `buffer-live-p' (Bill Dubuque hint)
189 ;; - Made `lazy-lock-install' do `add-to-list' not `setq' of `current-buffer'
190 ;; - Made `lazy-lock-fontify-after-install' loop over buffer list
191 ;; - Made `lazy-lock-arrange-before-change' to arrange `window-end' triggering
192 ;; - Made `lazy-lock-let-buffer-state' wrap both `befter-change-functions'
193 ;; - Made `lazy-lock-fontify-region' do `condition-case' (Hyman Rosen report)
194 ;; 2.01--2.02:
195 ;; - Use `buffer-live-p' as `buffer-name' can barf (Richard Stanton report)
196 ;; - Made `lazy-lock-install' set `font-lock-fontified' (Kevin Davidson report)
197 ;; - Made `lazy-lock-install' add hooks only if needed
198 ;; - Made `lazy-lock-unstall' add `font-lock-after-change-function' if needed
199 ;; 2.02--2.03:
200 ;; - Made `lazy-lock-fontify-region' do `condition-case' for `quit' too
201 ;; - Made `lazy-lock-mode' respect the value of `font-lock-inhibit-thing-lock'
202 ;; - Added `lazy-lock-after-unfontify-buffer'
203 ;; - Removed `lazy-lock-fontify-after-install' hack
204 ;; - Made `lazy-lock-fontify-after-scroll' not `set-buffer' to `window-buffer'
205 ;; - Made `lazy-lock-fontify-after-trigger' not `set-buffer' to `window-buffer'
206 ;; - Made `lazy-lock-fontify-after-idle' be interruptible (Scott Burson hint)
207 ;; 2.03--2.04:
208 ;; - Rewrite for Emacs 19.31 idle timers
209 ;; - Renamed `buffer-windows' to `get-buffer-window-list'
210 ;; - Removed `buffer-live-p'
211 ;; - Made `lazy-lock-defer-after-change' always save `current-buffer'
212 ;; - Made `lazy-lock-fontify-after-defer' just process buffers
213 ;; - Made `lazy-lock-install-hooks' add hooks correctly (Kevin Broadey report)
214 ;; - Made `lazy-lock-install' cope if `lazy-lock-defer-time' is a list
215 ;; 2.04--2.05:
216 ;; - Rewrite for Common Lisp macros
217 ;; - Added `do-while' macro
218 ;; - Renamed `lazy-lock-let-buffer-state' macro to `save-buffer-state'
219 ;; - Returned `lazy-lock-fontify-after-install' hack (Darren Hall hint)
220 ;; - Added `lazy-lock-defer-on-scrolling' functionality (Scott Byer hint)
221 ;; - Made `lazy-lock-mode' wrap `font-lock-support-mode'
222 ;; 2.05--2.06:
223 ;; - Made `lazy-lock-fontify-after-defer' swap correctly (Scott Byer report)
224 ;; 2.06--2.07:
225 ;; - Added `lazy-lock-stealth-load' functionality (Rob Hooft hint)
226 ;; - Made `lazy-lock-unstall' call `lazy-lock-fontify-region' if needed
227 ;; - Made `lazy-lock-mode' call `lazy-lock-unstall' only if needed
228 ;; - Made `lazy-lock-defer-after-scroll' do `set-window-redisplay-end-trigger'
229 ;; - Added `lazy-lock-defer-contextually' functionality
230 ;; - Added `lazy-lock-defer-on-the-fly' from `lazy-lock-defer-time'
231 ;; - Renamed `lazy-lock-defer-driven' to `lazy-lock-defer-on-scrolling'
232 ;; - Removed `lazy-lock-submit-bug-report' and bade farewell
233 \f
234 ;;; Code:
235
236 (require 'font-lock)
237
238 ;; Make sure lazy-lock.el is supported.
239 (if (if (save-match-data (string-match "Lucid\\|XEmacs" (emacs-version)))
240 t
241 (and (= emacs-major-version 19) (< emacs-minor-version 30)))
242 (error "`lazy-lock' was written for Emacs 19.30 or later"))
243
244 ;; Flush out those lusers who didn't read all of the Commentary.
245 (if (or (memq 'turn-on-defer-lock font-lock-mode-hook)
246 (memq 'defer-lock-mode font-lock-mode-hook))
247 (error "`lazy-lock' was written for use without `defer-lock'"))
248
249 (eval-when-compile
250 ;;
251 ;; We don't do this at the top-level as idle timers are not necessarily used.
252 (require 'timer)
253 ;; We don't do this at the top-level as we only use non-autoloaded macros.
254 (require 'cl)
255 ;;
256 ;; Well, shouldn't Lazy Lock mode be as lazy as possible?
257 (setq byte-compile-dynamic t byte-compile-dynamic-docstrings t)
258 ;; But, we make sure that the code is as zippy as can be.
259 (setq byte-optimize t)
260 ;;
261 ;; We use this to preserve or protect things when modifying text properties.
262 (defmacro save-buffer-state (varlist &rest body)
263 "Bind variables according to VARLIST and eval BODY restoring buffer state."
264 (` (let* ((,@ (append varlist
265 '((modified (buffer-modified-p))
266 (inhibit-read-only t) (buffer-undo-list t)
267 before-change-functions after-change-functions
268 deactivate-mark buffer-file-name buffer-file-truename))))
269 (,@ body)
270 (when (and (not modified) (buffer-modified-p))
271 (set-buffer-modified-p nil)))))
272 (put 'save-buffer-state 'lisp-indent-function 1)
273 ;;
274 ;; We use this for clarity and speed. Naughty but nice.
275 (defmacro do-while (test &rest body)
276 "(do-while TEST BODY...): eval BODY... and repeat if TEST yields non-nil.
277 The order of execution is thus BODY, TEST, BODY, TEST and so on
278 until TEST returns nil."
279 (` (while (progn (,@ body) (, test)))))
280 (put 'do-while 'lisp-indent-function (get 'while 'lisp-indent-function))
281 ;;
282 ;; We use this for clarity and speed. Borrowed from a future Emacs.
283 (or (fboundp 'with-current-buffer)
284 (defmacro with-current-buffer (buffer &rest body)
285 "Execute the forms in BODY with BUFFER as the current buffer.
286 The value returned is the value of the last form in BODY."
287 (` (save-excursion (set-buffer (, buffer)) (,@ body)))))
288 (put 'with-current-buffer 'lisp-indent-function 1))
289
290 ;(defun lazy-lock-submit-bug-report ()
291 ; "Submit via mail a bug report on lazy-lock.el."
292 ; (interactive)
293 ; (let ((reporter-prompt-for-summary-p t))
294 ; (reporter-submit-bug-report "simon@gnu.ai.mit.edu" "lazy-lock 2.07"
295 ; '(lazy-lock-minimum-size lazy-lock-defer-on-the-fly
296 ; lazy-lock-defer-on-scrolling lazy-lock-defer-contextually
297 ; lazy-lock-defer-time lazy-lock-stealth-time
298 ; lazy-lock-stealth-load lazy-lock-stealth-nice lazy-lock-stealth-lines
299 ; lazy-lock-stealth-verbose)
300 ; nil nil
301 ; (concat "Hi Si.,
302 ;
303 ;I want to report a bug. I've read the `Bugs' section of `Info' on Emacs, so I
304 ;know how to make a clear and unambiguous report. To reproduce the bug:
305 ;
306 ;Start a fresh editor via `" invocation-name " -no-init-file -no-site-file'.
307 ;In the `*scratch*' buffer, evaluate:"))))
308
309 (defvar lazy-lock-mode nil)
310 (defvar lazy-lock-buffers nil) ; for deferral
311 (defvar lazy-lock-timers (cons nil nil)) ; for deferral and stealth
312 \f
313 ;; User Variables:
314
315 (defvar lazy-lock-minimum-size (* 25 1024)
316 "*Minimum size of a buffer for demand-driven fontification.
317 On-demand fontification occurs if the buffer size is greater than this value.
318 If nil, means demand-driven fontification is never performed.
319 If a list, each element should be a cons pair of the form (MAJOR-MODE . SIZE),
320 where MAJOR-MODE is a symbol or t (meaning the default). For example:
321 ((c-mode . 25600) (c++-mode . 25600) (rmail-mode . 1048576))
322 means that the minimum size is 25K for buffers in C or C++ modes, one megabyte
323 for buffers in Rmail mode, and size is irrelevant otherwise.
324
325 The value of this variable is used when Lazy Lock mode is turned on.")
326
327 (defvar lazy-lock-defer-on-the-fly t
328 "*If non-nil, means fontification after a change should be deferred.
329 If nil, means on-the-fly fontification is performed. This means when changes
330 occur in the buffer, those areas are immediately fontified.
331 If a list, it should be a list of `major-mode' symbol names for which deferred
332 fontification should occur. The sense of the list is negated if it begins with
333 `not'. For example:
334 (c-mode c++-mode)
335 means that on-the-fly fontification is deferred for buffers in C and C++ modes
336 only, and deferral does not occur otherwise.
337
338 The value of this variable is used when Lazy Lock mode is turned on.")
339
340 (defvar lazy-lock-defer-on-scrolling nil
341 "*If non-nil, means fontification after a scroll should be deferred.
342 If nil, means demand-driven fontification is performed. This means when
343 scrolling into unfontified areas of the buffer, those areas are immediately
344 fontified. Thus scrolling never presents unfontified areas. However, since
345 fontification occurs during scrolling, scrolling may be slow.
346 If t, means defer-driven fontification is performed. This means fontification
347 of those areas is deferred. Thus scrolling may present momentarily unfontified
348 areas. However, since fontification does not occur during scrolling, scrolling
349 will be faster than demand-driven fontification.
350 If any other value, e.g., `eventually', means demand-driven fontification is
351 performed until the buffer is fontified, then buffer fontification becomes
352 defer-driven. Thus scrolling never presents unfontified areas until the buffer
353 is first fontified, after which subsequent scrolling may present future buffer
354 insertions momentarily unfontified. However, since fontification does not
355 occur during scrolling after the buffer is first fontified, scrolling will
356 become faster. (But, since contextual changes continually occur, such a value
357 makes little sense if `lazy-lock-defer-contextually' is non-nil.)
358
359 The value of this variable is used when Lazy Lock mode is turned on.")
360
361 (defvar lazy-lock-defer-contextually 'syntax-driven
362 "*If non-nil, means deferred fontification should be syntactically true.
363 If nil, means deferred fontification occurs only on those lines modified. This
364 means where modification on a line causes syntactic change on subsequent lines,
365 those subsequent lines are not refontified to reflect their new context.
366 If t, means deferred fontification occurs on those lines modified and all
367 subsequent lines. This means those subsequent lines are refontified to reflect
368 their new syntactic context, either immediately or when scrolling into them.
369 If any other value, e.g., `syntax-driven', means deferred syntactically true
370 fontification occurs only if syntactic fontification is performed using the
371 buffer mode's syntax table, i.e., only if `font-lock-keywords-only' is nil.
372
373 The value of this variable is used when Lazy Lock mode is turned on.")
374
375 (defvar lazy-lock-defer-time
376 (if (featurep 'lisp-float-type) (/ (float 1) (float 3)) 1)
377 "*Time in seconds to delay before beginning deferred fontification.
378 Deferred fontification occurs if there is no input within this time.
379 If nil, means fontification is never deferred, regardless of the values of the
380 variables `lazy-lock-defer-on-the-fly', `lazy-lock-defer-on-scrolling' and
381 `lazy-lock-defer-contextually'.
382
383 The value of this variable is used when Lazy Lock mode is turned on.")
384
385 (defvar lazy-lock-stealth-time 30
386 "*Time in seconds to delay before beginning stealth fontification.
387 Stealth fontification occurs if there is no input within this time.
388 If nil, means stealth fontification is never performed.
389
390 The value of this variable is used when Lazy Lock mode is turned on.")
391
392 (defvar lazy-lock-stealth-lines (if font-lock-maximum-decoration 100 250)
393 "*Maximum size of a chunk of stealth fontification.
394 Each iteration of stealth fontification can fontify this number of lines.
395 To speed up input response during stealth fontification, at the cost of stealth
396 taking longer to fontify, you could reduce the value of this variable.")
397
398 (defvar lazy-lock-stealth-load
399 (when (condition-case nil (load-average) (error)) 200)
400 "*Load in percentage above which stealth fontification is suspended.
401 Stealth fontification pauses when the system short-term load average (as
402 returned by the function `load-average' if supported) goes above this level,
403 thus reducing the demand that stealth fontification makes on the system.
404 If nil, means stealth fontification is never suspended.
405 To reduce machine load during stealth fontification, at the cost of stealth
406 taking longer to fontify, you could reduce the value of this variable.
407 See also `lazy-lock-stealth-nice'.")
408
409 (defvar lazy-lock-stealth-nice
410 (if (featurep 'lisp-float-type) (/ (float 1) (float 8)) 1)
411 "*Time in seconds to pause between chunks of stealth fontification.
412 Each iteration of stealth fontification is separated by this amount of time,
413 thus reducing the demand that stealth fontification makes on the system.
414 If nil, means stealth fontification is never paused.
415 To reduce machine load during stealth fontification, at the cost of stealth
416 taking longer to fontify, you could increase the value of this variable.
417 See also `lazy-lock-stealth-load'.")
418
419 (defvar lazy-lock-stealth-verbose
420 (when (featurep 'lisp-float-type)
421 (and font-lock-verbose (not lazy-lock-defer-contextually)))
422 "*If non-nil, means stealth fontification should show status messages.")
423 \f
424 ;; User Functions:
425
426 ;;;###autoload
427 (defun lazy-lock-mode (&optional arg)
428 "Toggle Lazy Lock mode.
429 With arg, turn Lazy Lock mode on if and only if arg is positive. Enable it
430 automatically in your `~/.emacs' by:
431
432 (setq font-lock-support-mode 'lazy-lock-mode)
433
434 When Lazy Lock mode is enabled, fontification can be lazy in a number of ways:
435
436 - Demand-driven buffer fontification if `lazy-lock-minimum-size' is non-nil.
437 This means initial fontification does not occur if the buffer is greater than
438 `lazy-lock-minimum-size' characters in length. Instead, fontification occurs
439 when necessary, such as when scrolling through the buffer would otherwise
440 reveal unfontified areas. This is useful if buffer fontification is too slow
441 for large buffers.
442
443 - Deferred scroll fontification if `lazy-lock-defer-on-scrolling' is non-nil.
444 This means demand-driven fontification does not occur as you scroll.
445 Instead, fontification is deferred until after `lazy-lock-defer-time' seconds
446 of Emacs idle time, while Emacs remains idle. This is useful if
447 fontification is too slow to keep up with scrolling.
448
449 - Deferred on-the-fly fontification if `lazy-lock-defer-on-the-fly' is non-nil.
450 This means on-the-fly fontification does not occur as you type. Instead,
451 fontification is deferred until after `lazy-lock-defer-time' seconds of Emacs
452 idle time, while Emacs remains idle. This is useful if fontification is too
453 slow to keep up with your typing.
454
455 - Deferred context fontification if `lazy-lock-defer-contextually' is non-nil.
456 This means fontification updates the buffer corresponding to true syntactic
457 context, after `lazy-lock-defer-time' seconds of Emacs idle time, while Emacs
458 remains idle. Otherwise, fontification occurs on modified lines only, and
459 subsequent lines can remain fontified corresponding to previous syntactic
460 contexts. This is useful where strings or comments span lines.
461
462 - Stealthy buffer fontification if `lazy-lock-stealth-time' is non-nil.
463 This means remaining unfontified areas of buffers are fontified if Emacs has
464 been idle for `lazy-lock-stealth-time' seconds, while Emacs remains idle.
465 This is useful if any buffer has any deferred fontification.
466
467 Basic Font Lock mode on-the-fly fontification behaviour fontifies modified
468 lines only. Thus, if `lazy-lock-defer-contextually' is non-nil, Lazy Lock mode
469 on-the-fly fontification may fontify differently, albeit correctly. In any
470 event, to refontify some lines you can use \\[font-lock-fontify-block].
471
472 Stealth fontification only occurs while the system remains unloaded.
473 If the system load rises above `lazy-lock-stealth-load' percent, stealth
474 fontification is suspended. Stealth fontification intensity is controlled via
475 the variable `lazy-lock-stealth-nice' and `lazy-lock-stealth-lines', and
476 verbosity is controlled via the variable `lazy-lock-stealth-verbose'."
477 (interactive "P")
478 (let* ((was-on lazy-lock-mode)
479 (now-on (unless (memq 'lazy-lock-mode font-lock-inhibit-thing-lock)
480 (if arg (> (prefix-numeric-value arg) 0) (not was-on)))))
481 (cond ((and now-on (not font-lock-mode))
482 ;; Turned on `lazy-lock-mode' rather than `font-lock-mode'.
483 (let ((font-lock-support-mode 'lazy-lock-mode))
484 (font-lock-mode t)))
485 (now-on
486 ;; Turn ourselves on.
487 (set (make-local-variable 'lazy-lock-mode) t)
488 (lazy-lock-install))
489 (was-on
490 ;; Turn ourselves off.
491 (set (make-local-variable 'lazy-lock-mode) nil)
492 (lazy-lock-unstall)))))
493
494 ;;;###autoload
495 (defun turn-on-lazy-lock ()
496 "Unconditionally turn on Lazy Lock mode."
497 (lazy-lock-mode t))
498
499 (defun lazy-lock-install ()
500 (let ((min-size (font-lock-value-in-major-mode lazy-lock-minimum-size))
501 (defer-change (and lazy-lock-defer-time lazy-lock-defer-on-the-fly))
502 (defer-scroll (and lazy-lock-defer-time lazy-lock-defer-on-scrolling))
503 (defer-context (and lazy-lock-defer-time lazy-lock-defer-contextually
504 (or (eq lazy-lock-defer-contextually t)
505 (null font-lock-keywords-only)))))
506 ;;
507 ;; Tell Font Lock whether Lazy Lock will do fontification.
508 (make-local-variable 'font-lock-fontified)
509 (setq font-lock-fontified (and min-size (>= (buffer-size) min-size)))
510 ;;
511 ;; Add the text properties and fontify.
512 (if (not font-lock-fontified)
513 (lazy-lock-after-fontify-buffer)
514 ;; Make sure we fontify in any existing windows showing the buffer.
515 (let ((windows (get-buffer-window-list (current-buffer) 'nomini t)))
516 (lazy-lock-after-unfontify-buffer)
517 (while windows
518 (lazy-lock-fontify-conservatively (car windows))
519 (setq windows (cdr windows)))))
520 ;;
521 ;; Add the fontification hooks.
522 (lazy-lock-install-hooks
523 font-lock-fontified
524 (cond ((eq (car-safe defer-change) 'not)
525 (not (memq major-mode (cdr defer-change))))
526 ((listp defer-change)
527 (memq major-mode defer-change))
528 (t
529 defer-change))
530 (eq defer-scroll t)
531 defer-context)
532 ;;
533 ;; Add the fontification timers.
534 (lazy-lock-install-timers
535 (if (or defer-change defer-scroll defer-context) lazy-lock-defer-time)
536 lazy-lock-stealth-time)))
537
538 (defun lazy-lock-install-hooks (fontifying
539 defer-change defer-scroll defer-context)
540 ;;
541 ;; Add hook if lazy-lock.el is fontifying on scrolling or is deferring.
542 (when (or fontifying defer-change defer-scroll defer-context)
543 (make-local-hook 'window-scroll-functions)
544 (add-hook 'window-scroll-functions (if defer-scroll
545 'lazy-lock-defer-after-scroll
546 'lazy-lock-fontify-after-scroll)
547 nil t))
548 ;;
549 ;; Add hook if lazy-lock.el is fontifying and is not deferring changes.
550 (when (and fontifying (not defer-change) (not defer-context))
551 (make-local-hook 'before-change-functions)
552 (add-hook 'before-change-functions 'lazy-lock-arrange-before-change nil t))
553 ;;
554 ;; Replace Font Lock mode hook.
555 (remove-hook 'after-change-functions 'font-lock-after-change-function t)
556 (add-hook 'after-change-functions
557 (cond ((and defer-change defer-context)
558 'lazy-lock-defer-rest-after-change)
559 (defer-change
560 'lazy-lock-defer-line-after-change)
561 (defer-context
562 'lazy-lock-fontify-rest-after-change)
563 (t
564 'lazy-lock-fontify-line-after-change))
565 nil t)
566 ;;
567 ;; Add package-specific hook.
568 (make-local-hook 'outline-view-change-hook)
569 (add-hook 'outline-view-change-hook 'lazy-lock-fontify-after-outline nil t))
570
571 (defun lazy-lock-install-timers (dtime stime)
572 ;; Schedule or re-schedule the deferral and stealth timers.
573 ;; The layout of `lazy-lock-timers' is:
574 ;; ((DEFER-TIME . DEFER-TIMER) (STEALTH-TIME . STEALTH-TIMER)
575 ;; If an idle timeout has changed, cancel the existing idle timer (if there
576 ;; is one) and schedule a new one (if the new idle timeout is non-nil).
577 (unless (eq dtime (car (car lazy-lock-timers)))
578 (let ((defer (car lazy-lock-timers)))
579 (when (cdr defer)
580 (cancel-timer (cdr defer)))
581 (setcar lazy-lock-timers (cons dtime (and dtime
582 (run-with-idle-timer dtime t 'lazy-lock-fontify-after-defer))))))
583 (unless (eq stime (car (cdr lazy-lock-timers)))
584 (let ((stealth (cdr lazy-lock-timers)))
585 (when (cdr stealth)
586 (cancel-timer (cdr stealth)))
587 (setcdr lazy-lock-timers (cons stime (and stime
588 (run-with-idle-timer stime t 'lazy-lock-fontify-after-idle)))))))
589
590 (defun lazy-lock-unstall ()
591 ;;
592 ;; If Font Lock mode is still enabled, make sure that the buffer is
593 ;; fontified, and reinstall its hook. We must do this first.
594 (when font-lock-mode
595 (when (lazy-lock-unfontified-p)
596 (let ((verbose (if (numberp font-lock-verbose)
597 (> (buffer-size) font-lock-verbose)
598 font-lock-verbose)))
599 (if verbose (message "Fontifying %s..." (buffer-name)))
600 ;; Make sure we fontify etc. in the whole buffer.
601 (save-restriction
602 (widen)
603 (lazy-lock-fontify-region (point-min) (point-max)))
604 (if verbose (message "Fontifying %s...%s" (buffer-name)
605 (if (lazy-lock-unfontified-p) "quit" "done")))))
606 (add-hook 'after-change-functions 'font-lock-after-change-function nil t))
607 ;;
608 ;; Remove the text properties.
609 (lazy-lock-after-unfontify-buffer)
610 ;;
611 ;; Remove the fontification hooks.
612 (remove-hook 'window-scroll-functions 'lazy-lock-fontify-after-scroll t)
613 (remove-hook 'window-scroll-functions 'lazy-lock-defer-after-scroll t)
614 (remove-hook 'before-change-functions 'lazy-lock-arrange-before-change t)
615 (remove-hook 'after-change-functions 'lazy-lock-fontify-line-after-change t)
616 (remove-hook 'after-change-functions 'lazy-lock-fontify-rest-after-change t)
617 (remove-hook 'after-change-functions 'lazy-lock-defer-line-after-change t)
618 (remove-hook 'after-change-functions 'lazy-lock-defer-rest-after-change t)
619 (remove-hook 'outline-view-change-hook 'lazy-lock-fontify-after-outline t))
620 \f
621 ;; Hook functions.
622
623 ;; Lazy Lock mode intervenes when (1) a previously invisible buffer region
624 ;; becomes visible, i.e., for demand- or defer-driven on-the-scroll
625 ;; fontification, (2) a buffer modification occurs, i.e., for defer-driven
626 ;; on-the-fly fontification, (3) Emacs becomes idle, i.e., for fontification of
627 ;; deferred fontification and stealth fontification, and (4) other special
628 ;; occasions.
629
630 ;; 1. There are three ways whereby this can happen.
631 ;;
632 ;; (a) Scrolling the window, either explicitly (e.g., `scroll-up') or
633 ;; implicitly (e.g., `search-forward'). Here, `window-start' changes.
634 ;; Fontification occurs by adding `lazy-lock-fontify-after-scroll' (for
635 ;; demand-driven fontification) or `lazy-lock-defer-after-scroll' (for
636 ;; defer-driven fontification) to the hook `window-scroll-functions'.
637
638 (defun lazy-lock-fontify-after-scroll (window window-start)
639 ;; Called from `window-scroll-functions'.
640 ;; Fontify WINDOW from WINDOW-START following the scroll. We cannot use
641 ;; `window-end' so we work out what it would be via `vertical-motion'.
642 (save-excursion
643 (goto-char window-start)
644 (vertical-motion (window-height window) window)
645 (lazy-lock-fontify-region window-start (point)))
646 ;; A prior deletion that did not cause scrolling, followed by a scroll, would
647 ;; result in an unnecessary trigger after this if we did not cancel it now.
648 (set-window-redisplay-end-trigger window nil))
649
650 (defun lazy-lock-defer-after-scroll (window window-start)
651 ;; Called from `window-scroll-functions'.
652 ;; Defer fontification following the scroll. Save the current buffer so that
653 ;; we subsequently fontify in all windows showing the buffer.
654 (unless (memq (current-buffer) lazy-lock-buffers)
655 (push (current-buffer) lazy-lock-buffers))
656 ;; A prior deletion that did not cause scrolling, followed by a scroll, would
657 ;; result in an unnecessary trigger after this if we did not cancel it now.
658 (set-window-redisplay-end-trigger window nil))
659
660 ;; (b) Resizing the window, either explicitly (e.g., `enlarge-window') or
661 ;; implicitly (e.g., `delete-other-windows'). Here, `window-end' changes.
662 ;; Fontification occurs by adding `lazy-lock-fontify-after-resize' to the
663 ;; hook `window-size-change-functions'.
664
665 (defun lazy-lock-fontify-after-resize (frame)
666 ;; Called from `window-size-change-functions'.
667 ;; Fontify windows in FRAME following the resize. We cannot use
668 ;; `window-start' or `window-end' so we fontify conservatively.
669 (save-excursion
670 (save-selected-window
671 (select-frame frame)
672 (walk-windows (function (lambda (window)
673 (set-buffer (window-buffer window))
674 (when lazy-lock-mode
675 (lazy-lock-fontify-conservatively window))
676 (set-window-redisplay-end-trigger window nil)))
677 'nomini frame))))
678
679 ;; (c) Deletion in the buffer. Here, a `window-end' marker can become visible.
680 ;; Fontification occurs by adding `lazy-lock-arrange-before-change' to
681 ;; `before-change-functions' and `lazy-lock-fontify-after-trigger' to the
682 ;; hook `redisplay-end-trigger-functions'. Before every deletion, the
683 ;; marker `window-redisplay-end-trigger' position is set to the soon-to-be
684 ;; changed `window-end' position. If the marker becomes visible,
685 ;; `lazy-lock-fontify-after-trigger' gets called. Ouch. Note that we only
686 ;; have to deal with this eventuality if there is no on-the-fly deferral.
687
688 (defun lazy-lock-arrange-before-change (beg end)
689 ;; Called from `before-change-functions'.
690 ;; Arrange that if text becomes visible it will be fontified (if a deletion
691 ;; is pending, text might become visible at the bottom).
692 (unless (eq beg end)
693 (let ((windows (get-buffer-window-list (current-buffer) 'nomini t)) window)
694 (while windows
695 (setq window (car windows))
696 (unless (markerp (window-redisplay-end-trigger window))
697 (set-window-redisplay-end-trigger window (make-marker)))
698 (set-marker (window-redisplay-end-trigger window) (window-end window))
699 (setq windows (cdr windows))))))
700
701 (defun lazy-lock-fontify-after-trigger (window trigger-point)
702 ;; Called from `redisplay-end-trigger-functions'.
703 ;; Fontify WINDOW from TRIGGER-POINT. We cannot use `window-end' so we work
704 ;; out what it would be via `vertical-motion'.
705 ;; We could probably just use `lazy-lock-fontify-after-scroll' without loss:
706 ;; (lazy-lock-fontify-after-scroll window (window-start window))
707 (save-excursion
708 (goto-char (window-start window))
709 (vertical-motion (window-height window) window)
710 (lazy-lock-fontify-region trigger-point (point))))
711
712 ;; 2. Modified text must be marked as unfontified so it can be identified and
713 ;; fontified later when Emacs is idle. Deferral occurs by adding one of
714 ;; `lazy-lock-fontify-*-after-change' (for on-the-fly fontification) or
715 ;; `lazy-lock-defer-*-after-change' (for deferred fontification) to the
716 ;; hook `after-change-functions'.
717
718 (defalias 'lazy-lock-fontify-line-after-change
719 ;; Called from `after-change-functions'.
720 ;; Fontify the current change.
721 'font-lock-after-change-function)
722
723 (defun lazy-lock-fontify-rest-after-change (beg end old-len)
724 ;; Called from `after-change-functions'.
725 ;; Fontify the current change and defer fontification of the rest of the
726 ;; buffer. Save the current buffer so that we subsequently fontify in all
727 ;; windows showing the buffer.
728 (lazy-lock-fontify-line-after-change beg end old-len)
729 (save-buffer-state nil
730 (unless (memq (current-buffer) lazy-lock-buffers)
731 (push (current-buffer) lazy-lock-buffers))
732 (remove-text-properties end (point-max) '(lazy-lock nil))))
733
734 (defun lazy-lock-defer-line-after-change (beg end old-len)
735 ;; Called from `after-change-functions'.
736 ;; Defer fontification of the current change. Save the current buffer so
737 ;; that we subsequently fontify in all windows showing the buffer.
738 (save-buffer-state nil
739 (unless (memq (current-buffer) lazy-lock-buffers)
740 (push (current-buffer) lazy-lock-buffers))
741 (remove-text-properties (max (1- beg) (point-min))
742 (min (1+ end) (point-max))
743 '(lazy-lock nil))))
744
745 (defun lazy-lock-defer-rest-after-change (beg end old-len)
746 ;; Called from `after-change-functions'.
747 ;; Defer fontification of the rest of the buffer. Save the current buffer so
748 ;; that we subsequently fontify in all windows showing the buffer.
749 (save-buffer-state nil
750 (unless (memq (current-buffer) lazy-lock-buffers)
751 (push (current-buffer) lazy-lock-buffers))
752 (remove-text-properties (max (1- beg) (point-min))
753 (point-max)
754 '(lazy-lock nil))))
755
756 ;; 3. Deferred fontification and stealth fontification are done from these two
757 ;; functions. They are set up as Idle Timers.
758
759 (defun lazy-lock-fontify-after-defer ()
760 ;; Called from `timer-idle-list'.
761 ;; Fontify all windows where deferral has occurred for its buffer.
762 (while (and lazy-lock-buffers (not (input-pending-p)))
763 (let ((windows (get-buffer-window-list (car lazy-lock-buffers) 'nomini t)))
764 (while windows
765 (lazy-lock-fontify-window (car windows))
766 (setq windows (cdr windows)))
767 (setq lazy-lock-buffers (cdr lazy-lock-buffers))))
768 ;; Add hook if fontification should now be defer-driven in this buffer.
769 (when (and lazy-lock-mode lazy-lock-defer-on-scrolling
770 (memq 'lazy-lock-fontify-after-scroll window-scroll-functions)
771 (not (or (input-pending-p) (lazy-lock-unfontified-p))))
772 (remove-hook 'window-scroll-functions 'lazy-lock-fontify-after-scroll t)
773 (add-hook 'window-scroll-functions 'lazy-lock-defer-after-scroll nil t)))
774
775 (defun lazy-lock-fontify-after-idle ()
776 ;; Called from `timer-idle-list'.
777 ;; Fontify all buffers that need it, stealthily while idle.
778 (unless (or executing-kbd-macro (window-minibuffer-p (selected-window)))
779 ;; Loop over all buffers, fontify stealthily for each if necessary.
780 (let ((buffers (buffer-list)) (continue t) message message-log-max)
781 (save-excursion
782 (do-while (and buffers continue)
783 (set-buffer (car buffers))
784 (if (not (and lazy-lock-mode (lazy-lock-unfontified-p)))
785 (setq continue (not (input-pending-p)))
786 ;; Fontify regions in this buffer while there is no input.
787 (do-while (and (lazy-lock-unfontified-p) continue)
788 (if (and lazy-lock-stealth-load
789 (> (car (load-average)) lazy-lock-stealth-load))
790 ;; Wait a while before continuing with the loop.
791 (progn
792 (when message
793 (message "Fontifying stealthily...suspended")
794 (setq message nil))
795 (setq continue (sit-for (or lazy-lock-stealth-time 30))))
796 ;; Fontify a chunk.
797 (when lazy-lock-stealth-verbose
798 (if message
799 (message "Fontifying stealthily... %2d%% of %s"
800 (lazy-lock-percent-fontified) (buffer-name))
801 (message "Fontifying stealthily...")
802 (setq message t)))
803 (lazy-lock-fontify-chunk)
804 (setq continue (sit-for (or lazy-lock-stealth-nice 0))))))
805 (setq buffers (cdr buffers))))
806 (when message
807 (message "Fontifying stealthily...%s" (if continue "done" "quit"))))))
808
809 ;; 4. Special circumstances.
810
811 (defun lazy-lock-fontify-after-outline ()
812 ;; Called from `outline-view-change-hook'.
813 ;; Fontify windows showing the current buffer, as its visibility has changed.
814 ;; This is a conspiracy hack between lazy-lock.el and noutline.el.
815 (let ((windows (get-buffer-window-list (current-buffer) 'nomini t)))
816 (while windows
817 (lazy-lock-fontify-conservatively (car windows))
818 (setq windows (cdr windows)))))
819
820 (defun lazy-lock-after-fontify-buffer ()
821 ;; Called from `font-lock-after-fontify-buffer'.
822 ;; Mark the current buffer as fontified.
823 ;; This is a conspiracy hack between lazy-lock.el and font-lock.el.
824 (save-buffer-state nil
825 (add-text-properties (point-min) (point-max) '(lazy-lock t))))
826
827 (defun lazy-lock-after-unfontify-buffer ()
828 ;; Called from `font-lock-after-unfontify-buffer'.
829 ;; Mark the current buffer as unfontified.
830 ;; This is a conspiracy hack between lazy-lock.el and font-lock.el.
831 (save-buffer-state nil
832 (remove-text-properties (point-min) (point-max) '(lazy-lock nil))))
833 \f
834 ;; Fontification functions.
835
836 ;; If packages want to ensure that some region of the buffer is fontified, they
837 ;; should use this function. For an example, see ps-print.el.
838 (defun lazy-lock-fontify-region (beg end)
839 ;; Fontify between BEG and END, where necessary, in the current buffer.
840 (when (setq beg (text-property-any beg end 'lazy-lock nil))
841 (save-excursion
842 (save-match-data
843 (save-buffer-state
844 ;; Ensure syntactic fontification is always correct.
845 (font-lock-beginning-of-syntax-function next)
846 ;; Find successive unfontified regions between BEG and END.
847 (condition-case data
848 (do-while beg
849 (setq next (or (text-property-any beg end 'lazy-lock t) end))
850 ;; Make sure the region end points are at beginning of line.
851 (goto-char beg)
852 (unless (bolp)
853 (beginning-of-line)
854 (setq beg (point)))
855 (goto-char next)
856 (unless (bolp)
857 (forward-line)
858 (setq next (point)))
859 ;; Fontify the region, then flag it as fontified.
860 (font-lock-fontify-region beg next)
861 (add-text-properties beg next '(lazy-lock t))
862 (setq beg (text-property-any next end 'lazy-lock nil)))
863 ((error quit) (message "Fontifying region...%s" data))))))))
864
865 (defun lazy-lock-fontify-chunk ()
866 ;; Fontify the nearest chunk, for stealth, in the current buffer.
867 (save-excursion
868 (save-restriction
869 (widen)
870 ;; Move to end of line in case the character at point is not fontified.
871 (end-of-line)
872 ;; Find where the previous, and next, unfontified regions end, and begin.
873 (let ((prev (previous-single-property-change (point) 'lazy-lock))
874 (next (text-property-any (point) (point-max) 'lazy-lock nil)))
875 ;; Fontify from the nearest unfontified position.
876 (if (or (null prev) (and next (< (- next (point)) (- (point) prev))))
877 ;; The next, or neither, region is the nearest not fontified.
878 (lazy-lock-fontify-region
879 (progn (goto-char (or next (point-min)))
880 (beginning-of-line)
881 (point))
882 (progn (goto-char (or next (point-min)))
883 (forward-line lazy-lock-stealth-lines)
884 (point)))
885 ;; The previous region is the nearest not fontified.
886 (lazy-lock-fontify-region
887 (progn (goto-char prev)
888 (forward-line (- lazy-lock-stealth-lines))
889 (point))
890 (progn (goto-char prev)
891 (forward-line)
892 (point))))))))
893
894 (defun lazy-lock-fontify-window (window)
895 ;; Fontify in WINDOW between `window-start' and `window-end'.
896 ;; We can only do this when we can use `window-start' and `window-end'.
897 (with-current-buffer (window-buffer window)
898 (lazy-lock-fontify-region (window-start window) (window-end window))))
899
900 (defun lazy-lock-fontify-conservatively (window)
901 ;; Fontify in WINDOW conservatively around point.
902 ;; Where we cannot use `window-start' and `window-end' we do `window-height'
903 ;; lines around point. That way we guarantee to have done enough.
904 (with-current-buffer (window-buffer window)
905 (lazy-lock-fontify-region
906 (save-excursion
907 (vertical-motion (- (window-height window)) window) (point))
908 (save-excursion
909 (vertical-motion (window-height window) window) (point)))))
910
911 (defun lazy-lock-unfontified-p ()
912 ;; Return non-nil if there is anywhere still to be fontified.
913 (save-restriction
914 (widen)
915 (text-property-any (point-min) (point-max) 'lazy-lock nil)))
916
917 (defun lazy-lock-percent-fontified ()
918 ;; Return the percentage (of characters) of the buffer that are fontified.
919 (save-restriction
920 (widen)
921 (let ((beg (point-min)) (size 0) next)
922 ;; Find where the next fontified region begins.
923 (while (setq beg (text-property-any beg (point-max) 'lazy-lock t))
924 (setq next (or (text-property-any beg (point-max) 'lazy-lock nil)
925 (point-max)))
926 (incf size (- next beg))
927 (setq beg next))
928 ;; Float because using integer multiplication will frequently overflow.
929 (truncate (* (/ (float size) (point-max)) 100)))))
930 \f
931 ;; Version dependent workarounds and fixes.
932
933 (when (if (save-match-data (string-match "Lucid\\|XEmacs" (emacs-version)))
934 nil
935 (and (= emacs-major-version 19) (= emacs-minor-version 30)))
936 ;;
937 ;; We use `post-command-idle-hook' for deferral and stealth. Oh Lordy.
938 (defun lazy-lock-install-timers (foo bar)
939 (add-hook 'post-command-idle-hook 'lazy-lock-fontify-post-command t)
940 (add-hook 'post-command-idle-hook 'lazy-lock-fontify-post-idle t)
941 (add-to-list 'lazy-lock-install (current-buffer))
942 (add-hook 'post-command-hook 'lazy-lock-fontify-after-install))
943 (defun lazy-lock-fontify-post-command ()
944 (and lazy-lock-buffers (not executing-kbd-macro)
945 (progn
946 (and deactivate-mark (deactivate-mark))
947 (sit-for
948 (or (cdr-safe lazy-lock-defer-time) lazy-lock-defer-time 0)))
949 (lazy-lock-fontify-after-defer)))
950 (defun lazy-lock-fontify-post-idle ()
951 (and lazy-lock-stealth-time (not executing-kbd-macro)
952 (not (window-minibuffer-p (selected-window)))
953 (progn
954 (and deactivate-mark (deactivate-mark))
955 (sit-for lazy-lock-stealth-time))
956 (lazy-lock-fontify-after-idle)))
957 ;;
958 ;; Simulate running of `window-scroll-functions' in `set-window-buffer'.
959 (defvar lazy-lock-install nil)
960 (defun lazy-lock-fontify-after-install ()
961 (remove-hook 'post-command-hook 'lazy-lock-fontify-after-install)
962 (while lazy-lock-install
963 (mapcar 'lazy-lock-fontify-conservatively
964 (get-buffer-window-list (pop lazy-lock-install) 'nomini t)))))
965
966 (when (consp lazy-lock-defer-time)
967 ;;
968 ;; In 2.06.04 and below, `lazy-lock-defer-time' could specify modes and time.
969 (with-output-to-temp-buffer "*Help*"
970 (princ "The value of the variable `lazy-lock-defer-time' was\n ")
971 (princ lazy-lock-defer-time)
972 (princ "\n")
973 (princ "This variable cannot now be a list of modes and time, ")
974 (princ "so instead use the forms:\n")
975 (princ " (setq lazy-lock-defer-time ")
976 (princ (cdr lazy-lock-defer-time))
977 (princ ")\n")
978 (princ " (setq lazy-lock-defer-on-the-fly '")
979 (princ (car lazy-lock-defer-time))
980 (princ ")\n")
981 (princ "in your ~/.emacs. ")
982 (princ "The above forms have been evaluated for this editor session,\n")
983 (princ "but you should change your ~/.emacs now."))
984 (setq lazy-lock-defer-on-the-fly (car lazy-lock-defer-time)
985 lazy-lock-defer-time (cdr lazy-lock-defer-time)))
986
987 (when (boundp 'lazy-lock-defer-driven)
988 ;;
989 ;; In 2.06.04 and below, `lazy-lock-defer-driven' was the variable name.
990 (with-output-to-temp-buffer "*Help*"
991 (princ "The value of the variable `lazy-lock-defer-driven' is set to ")
992 (if (memq lazy-lock-defer-driven '(nil t))
993 (princ lazy-lock-defer-driven)
994 (princ "`")
995 (princ lazy-lock-defer-driven)
996 (princ "'"))
997 (princ ".\n")
998 (princ "This variable is now called `lazy-lock-defer-on-scrolling',\n")
999 (princ "so instead use the form:\n")
1000 (princ " (setq lazy-lock-defer-on-scrolling ")
1001 (unless (memq lazy-lock-defer-driven '(nil t))
1002 (princ "'"))
1003 (princ lazy-lock-defer-driven)
1004 (princ ")\n")
1005 (princ "in your ~/.emacs. ")
1006 (princ "The above form has been evaluated for this editor session,\n")
1007 (princ "but you should change your ~/.emacs now."))
1008 (setq lazy-lock-defer-on-scrolling lazy-lock-defer-driven))
1009 \f
1010 ;; Possibly absent.
1011
1012 (unless (boundp 'font-lock-inhibit-thing-lock)
1013 ;; Font Lock mode uses this to direct Lazy and Fast Lock modes to stay off.
1014 (defvar font-lock-inhibit-thing-lock nil
1015 "List of Font Lock mode related modes that should not be turned on."))
1016
1017 (unless (fboundp 'font-lock-value-in-major-mode)
1018 (defun font-lock-value-in-major-mode (alist)
1019 ;; Return value in ALIST for `major-mode'.
1020 (if (consp alist)
1021 (cdr (or (assq major-mode alist) (assq t alist)))
1022 alist)))
1023
1024 (unless (fboundp 'get-buffer-window-list)
1025 ;; We use this to get all windows showing a buffer we have to fontify.
1026 (defun get-buffer-window-list (buffer &optional minibuf frame)
1027 "Return windows currently displaying BUFFER, or nil if none."
1028 (let ((buffer (if (bufferp buffer) buffer (get-buffer buffer))) windows)
1029 (walk-windows (function (lambda (window)
1030 (when (eq (window-buffer window) buffer)
1031 (push window windows))))
1032 minibuf frame)
1033 windows)))
1034 \f
1035 ;; Install ourselves:
1036
1037 (add-hook 'window-size-change-functions 'lazy-lock-fontify-after-resize)
1038 (add-hook 'redisplay-end-trigger-functions 'lazy-lock-fontify-after-trigger)
1039
1040 (unless (assq 'lazy-lock-mode minor-mode-alist)
1041 (setq minor-mode-alist (append minor-mode-alist '((lazy-lock-mode nil)))))
1042
1043 ;; Provide ourselves:
1044
1045 (provide 'lazy-lock)
1046
1047 ;;; lazy-lock.el ends here