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