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