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