Fix maintainer address.
[emacs.git] / lisp / lazy-lock.el
blob7d28d7177e28922a8000480061455615074c27dc
1 ;;; lazy-lock.el --- Lazy demand-driven fontification for fast Font Lock mode.
3 ;; Copyright (C) 1994, 1995, 1996, 1997, 1998 Free Software Foundation, Inc.
5 ;; Author: Simon Marshall <simon@gnu.org>
6 ;; Keywords: faces files
7 ;; Version: 2.11
9 ;;; This file is part of GNU Emacs.
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.
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.
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.
26 ;;; Commentary:
28 ;; Purpose:
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.
35 ;; See caveats and feedback below.
36 ;; See also the fast-lock package. (But don't use them at the same time!)
38 ;; Installation:
39 ;;
40 ;; Put in your ~/.emacs:
42 ;; (setq font-lock-support-mode 'lazy-lock-mode)
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).
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.
56 ;; User-visible differences with version 1:
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.
61 ;; A number of variables have changed meaning:
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.
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.
73 ;; Implementation differences with version 1:
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':
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.
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.
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).
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.
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.
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.
133 ;; - Version 1 stealth fontification is also implemented by placing a function
134 ;; on `post-command-hook'. This function waits for a given amount of time,
135 ;; and, if Emacs remains idle, fontifies where necessary. Again, there are a
136 ;; number of problems with using `post-command-hook':
138 ;; (a) Functions on `post-command-hook' are run sequentially, so this function
139 ;; can interfere with other functions on the hook, and vice versa.
140 ;; (b) This function waits for a given amount of time, so it can interfere with
141 ;; various features that are dealt with by Emacs after a command, e.g.,
142 ;; region highlighting, asynchronous updating and keystroke echoing.
143 ;; (c) Fontification may be required during a command, when `post-command-hook'
144 ;; is not run. (Version 2 deferred fontification only.)
146 ;; Again, `post-command-hook' is completely inappropriate for lazy-lock.el.
147 ;; Richard Stallman and Morten Welinder implemented internal Timers and Idle
148 ;; Timers for Emacs 19.31. Functions can be run independently at given times
149 ;; or after given amounts of idle time. Thus, the feature deals with the above
150 ;; problems (a), (b) and (c). Version 2 deferral and stealth are implemented
151 ;; by functions on Idle Timers. (A function on XEmacs' `pre-idle-hook' is
152 ;; similar to an Emacs Idle Timer function with a fixed zero second timeout.)
154 ;; - Version 1 has the following problems (relative to version 2):
156 ;; (a) It is slow when it does its job.
157 ;; (b) It does not always do its job when it should.
158 ;; (c) It slows all interaction (when it doesn't need to do its job).
159 ;; (d) It interferes with other package functions on `post-command-hook'.
160 ;; (e) It interferes with Emacs things within the read-eval loop.
162 ;; Ben's hacked-up lazy-lock.el 1.14 almost solved (b) but made (c) worse.
164 ;; - Version 2 has the following additional features (relative to version 1):
166 ;; (a) It can defer fontification (both on-the-fly and on-scrolling).
167 ;; (b) It can fontify contextually (syntactically true on-the-fly).
169 ;; Caveats:
171 ;; Lazy Lock mode does not work efficiently with Outline mode.
172 ;; This is because when in Outline mode, although text may be not visible to
173 ;; you in the window, the text is visible to Emacs Lisp code (not surprisingly)
174 ;; and Lazy Lock fontifies it mercilessly. Maybe it will be fixed one day.
176 ;; Because buffer text is not necessarily fontified, other packages that expect
177 ;; buffer text to be fontified in Font Lock mode either might not work as
178 ;; expected, or might not display buffer text as expected. An example of the
179 ;; latter is `occur', which copies lines of buffer text into another buffer.
181 ;; In Emacs 19.30, Lazy Lock mode does not ensure that an existing buffer is
182 ;; fontified if it is made visible via a minibuffer-less command that replaces
183 ;; an existing window's buffer (e.g., via the Buffers menu). Upgrade!
185 ;; In Emacs 19.30, Lazy Lock mode does not work well with Transient Mark mode
186 ;; or modes based on Comint mode (e.g., Shell mode), and also interferes with
187 ;; the echoing of keystrokes in the minibuffer. This is because of the way
188 ;; deferral and stealth have to be implemented for Emacs 19.30. Upgrade!
190 ;; Currently XEmacs does not have the features to support this version of
191 ;; lazy-lock.el. Maybe it will one day.
193 ;; History:
195 ;; 1.15--2.00:
196 ;; - Rewrite for Emacs 19.30 and the features rms added to support lazy-lock.el
197 ;; so that it could work correctly and efficiently.
198 ;; - Many thanks to those who reported bugs, fixed bugs, made suggestions or
199 ;; otherwise contributed in the version 1 cycle; Jari Aalto, Kevin Broadey,
200 ;; Ulrik Dickow, Bill Dubuque, Bob Glickstein, Boris Goldowsky,
201 ;; Jonas Jarnestrom, David Karr, Michael Kifer, Erik Naggum, Rick Sladkey,
202 ;; Jim Thompson, Ben Wing, Ilya Zakharevich, and Richard Stallman.
203 ;; 2.00--2.01:
204 ;; - Made `lazy-lock-fontify-after-command' always `sit-for' and so redisplay
205 ;; - Use `buffer-name' not `buffer-live-p' (Bill Dubuque hint)
206 ;; - Made `lazy-lock-install' do `add-to-list' not `setq' of `current-buffer'
207 ;; - Made `lazy-lock-fontify-after-install' loop over buffer list
208 ;; - Made `lazy-lock-arrange-before-change' to arrange `window-end' triggering
209 ;; - Made `lazy-lock-let-buffer-state' wrap both `befter-change-functions'
210 ;; - Made `lazy-lock-fontify-region' do `condition-case' (Hyman Rosen report)
211 ;; 2.01--2.02:
212 ;; - Use `buffer-live-p' as `buffer-name' can barf (Richard Stanton report)
213 ;; - Made `lazy-lock-install' set `font-lock-fontified' (Kevin Davidson report)
214 ;; - Made `lazy-lock-install' add hooks only if needed
215 ;; - Made `lazy-lock-unstall' add `font-lock-after-change-function' if needed
216 ;; 2.02--2.03:
217 ;; - Made `lazy-lock-fontify-region' do `condition-case' for `quit' too
218 ;; - Made `lazy-lock-mode' respect the value of `font-lock-inhibit-thing-lock'
219 ;; - Added `lazy-lock-after-unfontify-buffer'
220 ;; - Removed `lazy-lock-fontify-after-install' hack
221 ;; - Made `lazy-lock-fontify-after-scroll' not `set-buffer' to `window-buffer'
222 ;; - Made `lazy-lock-fontify-after-trigger' not `set-buffer' to `window-buffer'
223 ;; - Made `lazy-lock-fontify-after-idle' be interruptible (Scott Burson hint)
224 ;; 2.03--2.04:
225 ;; - Rewrite for Emacs 19.31 idle timers
226 ;; - Renamed `buffer-windows' to `get-buffer-window-list'
227 ;; - Removed `buffer-live-p'
228 ;; - Made `lazy-lock-defer-after-change' always save `current-buffer'
229 ;; - Made `lazy-lock-fontify-after-defer' just process buffers
230 ;; - Made `lazy-lock-install-hooks' add hooks correctly (Kevin Broadey report)
231 ;; - Made `lazy-lock-install' cope if `lazy-lock-defer-time' is a list
232 ;; 2.04--2.05:
233 ;; - Rewrite for Common Lisp macros
234 ;; - Added `do-while' macro
235 ;; - Renamed `lazy-lock-let-buffer-state' macro to `save-buffer-state'
236 ;; - Returned `lazy-lock-fontify-after-install' hack (Darren Hall hint)
237 ;; - Added `lazy-lock-defer-on-scrolling' functionality (Scott Byer hint)
238 ;; - Made `lazy-lock-mode' wrap `font-lock-support-mode'
239 ;; 2.05--2.06:
240 ;; - Made `lazy-lock-fontify-after-defer' swap correctly (Scott Byer report)
241 ;; 2.06--2.07:
242 ;; - Added `lazy-lock-stealth-load' functionality (Rob Hooft hint)
243 ;; - Made `lazy-lock-unstall' call `lazy-lock-fontify-region' if needed
244 ;; - Made `lazy-lock-mode' call `lazy-lock-unstall' only if needed
245 ;; - Made `lazy-lock-defer-after-scroll' do `set-window-redisplay-end-trigger'
246 ;; - Added `lazy-lock-defer-contextually' functionality
247 ;; - Added `lazy-lock-defer-on-the-fly' from `lazy-lock-defer-time'
248 ;; - Renamed `lazy-lock-defer-driven' to `lazy-lock-defer-on-scrolling'
249 ;; - Removed `lazy-lock-submit-bug-report' and bade farewell
250 ;; 2.07--2.08:
251 ;; - Made `lazy-lock-fontify-conservatively' fontify around `window-point'
252 ;; - Made `save-buffer-state' wrap `inhibit-point-motion-hooks'
253 ;; - Added Custom support
254 ;; 2.08--2.09:
255 ;; - Removed `byte-*' variables from `eval-when-compile' (Erik Naggum hint)
256 ;; - Made various wrapping `inhibit-point-motion-hooks' (Vinicius Latorre hint)
257 ;; - Made `lazy-lock-fontify-after-idle' wrap `minibuffer-auto-raise'
258 ;; - Made `lazy-lock-fontify-after-defer' paranoid about deferred buffers
259 ;; 2.09--2.10:
260 ;; - Use `window-end' UPDATE arg for Emacs 20.4 and later.
261 ;; - Made deferral `widen' before unfontifying (Dan Nicolaescu report)
262 ;; - Use `lazy-lock-fontify-after-visage' for hideshow.el (Dan Nicolaescu hint)
263 ;; - Use `other' widget where possible (Andreas Schwab fix)
264 ;; 2.10--2.11:
265 ;; - Used `with-temp-message' where possible to make messages temporary.
267 ;;; Code:
269 (require 'font-lock)
271 ;; Make sure lazy-lock.el is supported.
272 (if (if (save-match-data (string-match "Lucid\\|XEmacs" (emacs-version)))
274 (and (= emacs-major-version 19) (< emacs-minor-version 30)))
275 (error "`lazy-lock' was written for Emacs 19.30 or later"))
277 (eval-when-compile
279 ;; We don't do this at the top-level as idle timers are not necessarily used.
280 (require 'timer)
281 ;; We don't do this at the top-level as we only use non-autoloaded macros.
282 (require 'cl)
284 ;; We use this to preserve or protect things when modifying text properties.
285 (defmacro save-buffer-state (varlist &rest body)
286 "Bind variables according to VARLIST and eval BODY restoring buffer state."
287 (` (let* ((,@ (append varlist
288 '((modified (buffer-modified-p)) (buffer-undo-list t)
289 (inhibit-read-only t) (inhibit-point-motion-hooks t)
290 before-change-functions after-change-functions
291 deactivate-mark buffer-file-name buffer-file-truename))))
292 (,@ body)
293 (when (and (not modified) (buffer-modified-p))
294 (set-buffer-modified-p nil)))))
295 (put 'save-buffer-state 'lisp-indent-function 1)
297 ;; We use this for clarity and speed. Naughty but nice.
298 (defmacro do-while (test &rest body)
299 "(do-while TEST BODY...): eval BODY... and repeat if TEST yields non-nil.
300 The order of execution is thus BODY, TEST, BODY, TEST and so on
301 until TEST returns nil."
302 (` (while (progn (,@ body) (, test)))))
303 (put 'do-while 'lisp-indent-function (get 'while 'lisp-indent-function))
305 ;; We use this for clarity and speed. Borrowed from a future Emacs.
306 (or (fboundp 'with-current-buffer)
307 (defmacro with-current-buffer (buffer &rest body)
308 "Execute the forms in BODY with BUFFER as the current buffer.
309 The value returned is the value of the last form in BODY."
310 (` (save-excursion (set-buffer (, buffer)) (,@ body)))))
311 (put 'with-current-buffer 'lisp-indent-function 1)
313 ;; We use this for compatibility with a future Emacs.
314 (or (fboundp 'with-temp-message)
315 (defmacro with-temp-message (message &rest body)
316 (` (let ((temp-message (, message)) current-message)
317 (unwind-protect
318 (progn
319 (when temp-message
320 (setq current-message (current-message))
321 (message temp-message))
322 (,@ body))
323 (when temp-message
324 (message current-message)))))))
326 ;; We use this for compatibility with a future Emacs.
327 (or (fboundp 'defcustom)
328 (defmacro defcustom (symbol value doc &rest args)
329 (` (defvar (, symbol) (, value) (, doc))))))
331 ;(defun lazy-lock-submit-bug-report ()
332 ; "Submit via mail a bug report on lazy-lock.el."
333 ; (interactive)
334 ; (let ((reporter-prompt-for-summary-p t))
335 ; (reporter-submit-bug-report "simon@gnu.org" "lazy-lock 2.11"
336 ; '(lazy-lock-minimum-size lazy-lock-defer-on-the-fly
337 ; lazy-lock-defer-on-scrolling lazy-lock-defer-contextually
338 ; lazy-lock-defer-time lazy-lock-stealth-time
339 ; lazy-lock-stealth-load lazy-lock-stealth-nice lazy-lock-stealth-lines
340 ; lazy-lock-stealth-verbose)
341 ; nil nil
342 ; (concat "Hi Si.,
344 ;I want to report a bug. I've read the `Bugs' section of `Info' on Emacs, so I
345 ;know how to make a clear and unambiguous report. To reproduce the bug:
347 ;Start a fresh editor via `" invocation-name " -no-init-file -no-site-file'.
348 ;In the `*scratch*' buffer, evaluate:"))))
350 (defvar lazy-lock-mode nil) ; Whether we are turned on.
351 (defvar lazy-lock-buffers nil) ; For deferral.
352 (defvar lazy-lock-timers (cons nil nil)) ; For deferral and stealth.
354 ;; User Variables:
356 (defcustom lazy-lock-minimum-size 25600
357 "*Minimum size of a buffer for demand-driven fontification.
358 On-demand fontification occurs if the buffer size is greater than this value.
359 If nil, means demand-driven fontification is never performed.
360 If a list, each element should be a cons pair of the form (MAJOR-MODE . SIZE),
361 where MAJOR-MODE is a symbol or t (meaning the default). For example:
362 ((c-mode . 25600) (c++-mode . 25600) (rmail-mode . 1048576))
363 means that the minimum size is 25K for buffers in C or C++ modes, one megabyte
364 for buffers in Rmail mode, and size is irrelevant otherwise.
366 The value of this variable is used when Lazy Lock mode is turned on."
367 :type '(choice (const :tag "none" nil)
368 (integer :tag "size")
369 (repeat :menu-tag "mode specific" :tag "mode specific"
370 :value ((t . nil))
371 (cons :tag "Instance"
372 (radio :tag "Mode"
373 (const :tag "all" t)
374 (symbol :tag "name"))
375 (radio :tag "Size"
376 (const :tag "none" nil)
377 (integer :tag "size")))))
378 :group 'lazy-lock)
380 (defcustom lazy-lock-defer-on-the-fly t
381 "*If non-nil, means fontification after a change should be deferred.
382 If nil, means on-the-fly fontification is performed. This means when changes
383 occur in the buffer, those areas are immediately fontified.
384 If a list, it should be a list of `major-mode' symbol names for which deferred
385 fontification should occur. The sense of the list is negated if it begins with
386 `not'. For example:
387 (c-mode c++-mode)
388 means that on-the-fly fontification is deferred for buffers in C and C++ modes
389 only, and deferral does not occur otherwise.
391 The value of this variable is used when Lazy Lock mode is turned on."
392 :type '(choice (const :tag "never" nil)
393 (const :tag "always" t)
394 (set :menu-tag "mode specific" :tag "modes"
395 :value (not)
396 (const :tag "Except" not)
397 (repeat :inline t (symbol :tag "mode"))))
398 :group 'lazy-lock)
400 (defcustom lazy-lock-defer-on-scrolling nil
401 "*If non-nil, means fontification after a scroll should be deferred.
402 If nil, means demand-driven fontification is performed. This means when
403 scrolling into unfontified areas of the buffer, those areas are immediately
404 fontified. Thus scrolling never presents unfontified areas. However, since
405 fontification occurs during scrolling, scrolling may be slow.
406 If t, means defer-driven fontification is performed. This means fontification
407 of those areas is deferred. Thus scrolling may present momentarily unfontified
408 areas. However, since fontification does not occur during scrolling, scrolling
409 will be faster than demand-driven fontification.
410 If any other value, e.g., `eventually', means demand-driven fontification is
411 performed until the buffer is fontified, then buffer fontification becomes
412 defer-driven. Thus scrolling never presents unfontified areas until the buffer
413 is first fontified, after which subsequent scrolling may present future buffer
414 insertions momentarily unfontified. However, since fontification does not
415 occur during scrolling after the buffer is first fontified, scrolling will
416 become faster. (But, since contextual changes continually occur, such a value
417 makes little sense if `lazy-lock-defer-contextually' is non-nil.)
419 The value of this variable is used when Lazy Lock mode is turned on."
420 :type '(choice (const :tag "never" nil)
421 (const :tag "always" t)
422 (other :tag "eventually" eventually))
423 :group 'lazy-lock)
425 (defcustom lazy-lock-defer-contextually 'syntax-driven
426 "*If non-nil, means deferred fontification should be syntactically true.
427 If nil, means deferred fontification occurs only on those lines modified. This
428 means where modification on a line causes syntactic change on subsequent lines,
429 those subsequent lines are not refontified to reflect their new context.
430 If t, means deferred fontification occurs on those lines modified and all
431 subsequent lines. This means those subsequent lines are refontified to reflect
432 their new syntactic context, either immediately or when scrolling into them.
433 If any other value, e.g., `syntax-driven', means deferred syntactically true
434 fontification occurs only if syntactic fontification is performed using the
435 buffer mode's syntax table, i.e., only if `font-lock-keywords-only' is nil.
437 The value of this variable is used when Lazy Lock mode is turned on."
438 :type '(choice (const :tag "never" nil)
439 (const :tag "always" t)
440 (other :tag "syntax-driven" syntax-driven))
441 :group 'lazy-lock)
443 (defcustom lazy-lock-defer-time
444 (if (featurep 'lisp-float-type) (/ (float 1) (float 4)) 1)
445 "*Time in seconds to delay before beginning deferred fontification.
446 Deferred fontification occurs if there is no input within this time.
447 If nil, means fontification is never deferred, regardless of the values of the
448 variables `lazy-lock-defer-on-the-fly', `lazy-lock-defer-on-scrolling' and
449 `lazy-lock-defer-contextually'.
451 The value of this variable is used when Lazy Lock mode is turned on."
452 :type '(choice (const :tag "never" nil)
453 (number :tag "seconds"))
454 :group 'lazy-lock)
456 (defcustom lazy-lock-stealth-time 30
457 "*Time in seconds to delay before beginning stealth fontification.
458 Stealth fontification occurs if there is no input within this time.
459 If nil, means stealth fontification is never performed.
461 The value of this variable is used when Lazy Lock mode is turned on."
462 :type '(choice (const :tag "never" nil)
463 (number :tag "seconds"))
464 :group 'lazy-lock)
466 (defcustom lazy-lock-stealth-lines (if font-lock-maximum-decoration 100 250)
467 "*Maximum size of a chunk of stealth fontification.
468 Each iteration of stealth fontification can fontify this number of lines.
469 To speed up input response during stealth fontification, at the cost of stealth
470 taking longer to fontify, you could reduce the value of this variable."
471 :type '(integer :tag "lines")
472 :group 'lazy-lock)
474 (defcustom lazy-lock-stealth-load
475 (if (condition-case nil (load-average) (error)) 200)
476 "*Load in percentage above which stealth fontification is suspended.
477 Stealth fontification pauses when the system short-term load average (as
478 returned by the function `load-average' if supported) goes above this level,
479 thus reducing the demand that stealth fontification makes on the system.
480 If nil, means stealth fontification is never suspended.
481 To reduce machine load during stealth fontification, at the cost of stealth
482 taking longer to fontify, you could reduce the value of this variable.
483 See also `lazy-lock-stealth-nice'."
484 :type (if (condition-case nil (load-average) (error))
485 '(choice (const :tag "never" nil)
486 (integer :tag "load"))
487 '(const :format "%t: unsupported\n" nil))
488 :group 'lazy-lock)
490 (defcustom lazy-lock-stealth-nice
491 (if (featurep 'lisp-float-type) (/ (float 1) (float 8)) 1)
492 "*Time in seconds to pause between chunks of stealth fontification.
493 Each iteration of stealth fontification is separated by this amount of time,
494 thus reducing the demand that stealth fontification makes on the system.
495 If nil, means stealth fontification is never paused.
496 To reduce machine load during stealth fontification, at the cost of stealth
497 taking longer to fontify, you could increase the value of this variable.
498 See also `lazy-lock-stealth-load'."
499 :type '(choice (const :tag "never" nil)
500 (number :tag "seconds"))
501 :group 'lazy-lock)
503 (defcustom lazy-lock-stealth-verbose
504 (if (featurep 'lisp-float-type)
505 (and (not lazy-lock-defer-contextually) (not (null font-lock-verbose))))
506 "*If non-nil, means stealth fontification should show status messages."
507 :type 'boolean
508 :group 'lazy-lock)
510 ;; User Functions:
512 ;;;###autoload
513 (defun lazy-lock-mode (&optional arg)
514 "Toggle Lazy Lock mode.
515 With arg, turn Lazy Lock mode on if and only if arg is positive. Enable it
516 automatically in your `~/.emacs' by:
518 (setq font-lock-support-mode 'lazy-lock-mode)
520 When Lazy Lock mode is enabled, fontification can be lazy in a number of ways:
522 - Demand-driven buffer fontification if `lazy-lock-minimum-size' is non-nil.
523 This means initial fontification does not occur if the buffer is greater than
524 `lazy-lock-minimum-size' characters in length. Instead, fontification occurs
525 when necessary, such as when scrolling through the buffer would otherwise
526 reveal unfontified areas. This is useful if buffer fontification is too slow
527 for large buffers.
529 - Deferred scroll fontification if `lazy-lock-defer-on-scrolling' is non-nil.
530 This means demand-driven fontification does not occur as you scroll.
531 Instead, fontification is deferred until after `lazy-lock-defer-time' seconds
532 of Emacs idle time, while Emacs remains idle. This is useful if
533 fontification is too slow to keep up with scrolling.
535 - Deferred on-the-fly fontification if `lazy-lock-defer-on-the-fly' is non-nil.
536 This means on-the-fly fontification does not occur as you type. Instead,
537 fontification is deferred until after `lazy-lock-defer-time' seconds of Emacs
538 idle time, while Emacs remains idle. This is useful if fontification is too
539 slow to keep up with your typing.
541 - Deferred context fontification if `lazy-lock-defer-contextually' is non-nil.
542 This means fontification updates the buffer corresponding to true syntactic
543 context, after `lazy-lock-defer-time' seconds of Emacs idle time, while Emacs
544 remains idle. Otherwise, fontification occurs on modified lines only, and
545 subsequent lines can remain fontified corresponding to previous syntactic
546 contexts. This is useful where strings or comments span lines.
548 - Stealthy buffer fontification if `lazy-lock-stealth-time' is non-nil.
549 This means remaining unfontified areas of buffers are fontified if Emacs has
550 been idle for `lazy-lock-stealth-time' seconds, while Emacs remains idle.
551 This is useful if any buffer has any deferred fontification.
553 Basic Font Lock mode on-the-fly fontification behaviour fontifies modified
554 lines only. Thus, if `lazy-lock-defer-contextually' is non-nil, Lazy Lock mode
555 on-the-fly fontification may fontify differently, albeit correctly. In any
556 event, to refontify some lines you can use \\[font-lock-fontify-block].
558 Stealth fontification only occurs while the system remains unloaded.
559 If the system load rises above `lazy-lock-stealth-load' percent, stealth
560 fontification is suspended. Stealth fontification intensity is controlled via
561 the variable `lazy-lock-stealth-nice' and `lazy-lock-stealth-lines', and
562 verbosity is controlled via the variable `lazy-lock-stealth-verbose'."
563 (interactive "P")
564 (let* ((was-on lazy-lock-mode)
565 (now-on (unless (memq 'lazy-lock-mode font-lock-inhibit-thing-lock)
566 (if arg (> (prefix-numeric-value arg) 0) (not was-on)))))
567 (cond ((and now-on (not font-lock-mode))
568 ;; Turned on `lazy-lock-mode' rather than `font-lock-mode'.
569 (let ((font-lock-support-mode 'lazy-lock-mode))
570 (font-lock-mode t)))
571 (now-on
572 ;; Turn ourselves on.
573 (set (make-local-variable 'lazy-lock-mode) t)
574 (lazy-lock-install))
575 (was-on
576 ;; Turn ourselves off.
577 (set (make-local-variable 'lazy-lock-mode) nil)
578 (lazy-lock-unstall)))))
580 ;;;###autoload
581 (defun turn-on-lazy-lock ()
582 "Unconditionally turn on Lazy Lock mode."
583 (lazy-lock-mode t))
585 (defun lazy-lock-install ()
586 (let ((min-size (font-lock-value-in-major-mode lazy-lock-minimum-size))
587 (defer-change (and lazy-lock-defer-time lazy-lock-defer-on-the-fly))
588 (defer-scroll (and lazy-lock-defer-time lazy-lock-defer-on-scrolling))
589 (defer-context (and lazy-lock-defer-time lazy-lock-defer-contextually
590 (or (eq lazy-lock-defer-contextually t)
591 (null font-lock-keywords-only)))))
593 ;; Tell Font Lock whether Lazy Lock will do fontification.
594 (make-local-variable 'font-lock-fontified)
595 (setq font-lock-fontified (and min-size (>= (buffer-size) min-size)))
597 ;; Add the text properties and fontify.
598 (if (not font-lock-fontified)
599 (lazy-lock-after-fontify-buffer)
600 ;; Make sure we fontify in any existing windows showing the buffer.
601 (let ((windows (get-buffer-window-list (current-buffer) 'nomini t)))
602 (lazy-lock-after-unfontify-buffer)
603 (while windows
604 (lazy-lock-fontify-conservatively (car windows))
605 (setq windows (cdr windows)))))
607 ;; Add the fontification hooks.
608 (lazy-lock-install-hooks
609 font-lock-fontified
610 (cond ((eq (car-safe defer-change) 'not)
611 (not (memq major-mode (cdr defer-change))))
612 ((listp defer-change)
613 (memq major-mode defer-change))
615 defer-change))
616 (eq defer-scroll t)
617 defer-context)
619 ;; Add the fontification timers.
620 (lazy-lock-install-timers
621 (if (or defer-change defer-scroll defer-context) lazy-lock-defer-time)
622 lazy-lock-stealth-time)))
624 (defun lazy-lock-install-hooks (fontifying
625 defer-change defer-scroll defer-context)
627 ;; Add hook if lazy-lock.el is fontifying on scrolling or is deferring.
628 (when (or fontifying defer-change defer-scroll defer-context)
629 (make-local-hook 'window-scroll-functions)
630 (add-hook 'window-scroll-functions (if defer-scroll
631 'lazy-lock-defer-after-scroll
632 'lazy-lock-fontify-after-scroll)
633 nil t))
635 ;; Add hook if lazy-lock.el is fontifying and is not deferring changes.
636 (when (and fontifying (not defer-change) (not defer-context))
637 (make-local-hook 'before-change-functions)
638 (add-hook 'before-change-functions 'lazy-lock-arrange-before-change nil t))
640 ;; Replace Font Lock mode hook.
641 (remove-hook 'after-change-functions 'font-lock-after-change-function t)
642 (add-hook 'after-change-functions
643 (cond ((and defer-change defer-context)
644 'lazy-lock-defer-rest-after-change)
645 (defer-change
646 'lazy-lock-defer-line-after-change)
647 (defer-context
648 'lazy-lock-fontify-rest-after-change)
650 'lazy-lock-fontify-line-after-change))
651 nil t)
653 ;; Add package-specific hook.
654 (make-local-hook 'outline-view-change-hook)
655 (add-hook 'outline-view-change-hook 'lazy-lock-fontify-after-visage nil t)
656 (make-local-hook 'hs-hide-hook)
657 (add-hook 'hs-hide-hook 'lazy-lock-fontify-after-visage nil t))
659 (defun lazy-lock-install-timers (dtime stime)
660 ;; Schedule or re-schedule the deferral and stealth timers.
661 ;; The layout of `lazy-lock-timers' is:
662 ;; ((DEFER-TIME . DEFER-TIMER) (STEALTH-TIME . STEALTH-TIMER)
663 ;; If an idle timeout has changed, cancel the existing idle timer (if there
664 ;; is one) and schedule a new one (if the new idle timeout is non-nil).
665 (unless (eq dtime (car (car lazy-lock-timers)))
666 (let ((defer (car lazy-lock-timers)))
667 (when (cdr defer)
668 (cancel-timer (cdr defer)))
669 (setcar lazy-lock-timers (cons dtime (and dtime
670 (run-with-idle-timer dtime t 'lazy-lock-fontify-after-defer))))))
671 (unless (eq stime (car (cdr lazy-lock-timers)))
672 (let ((stealth (cdr lazy-lock-timers)))
673 (when (cdr stealth)
674 (cancel-timer (cdr stealth)))
675 (setcdr lazy-lock-timers (cons stime (and stime
676 (run-with-idle-timer stime t 'lazy-lock-fontify-after-idle)))))))
678 (defun lazy-lock-unstall ()
680 ;; If Font Lock mode is still enabled, make sure that the buffer is
681 ;; fontified, and reinstall its hook. We must do this first.
682 (when font-lock-mode
683 (when (lazy-lock-unfontified-p)
684 (let ((verbose (if (numberp font-lock-verbose)
685 (> (buffer-size) font-lock-verbose)
686 font-lock-verbose)))
687 (with-temp-message
688 (when verbose
689 (format "Fontifying %s..." (buffer-name)))
690 ;; Make sure we fontify etc. in the whole buffer.
691 (save-restriction
692 (widen)
693 (lazy-lock-fontify-region (point-min) (point-max))))))
694 (add-hook 'after-change-functions 'font-lock-after-change-function nil t))
696 ;; Remove the text properties.
697 (lazy-lock-after-unfontify-buffer)
699 ;; Remove the fontification hooks.
700 (remove-hook 'window-scroll-functions 'lazy-lock-fontify-after-scroll t)
701 (remove-hook 'window-scroll-functions 'lazy-lock-defer-after-scroll t)
702 (remove-hook 'before-change-functions 'lazy-lock-arrange-before-change t)
703 (remove-hook 'after-change-functions 'lazy-lock-fontify-line-after-change t)
704 (remove-hook 'after-change-functions 'lazy-lock-fontify-rest-after-change t)
705 (remove-hook 'after-change-functions 'lazy-lock-defer-line-after-change t)
706 (remove-hook 'after-change-functions 'lazy-lock-defer-rest-after-change t)
707 (remove-hook 'outline-view-change-hook 'lazy-lock-fontify-after-visage t)
708 (remove-hook 'hs-hide-hook 'lazy-lock-fontify-after-visage t))
710 ;; Hook functions.
712 ;; Lazy Lock mode intervenes when (1) a previously invisible buffer region
713 ;; becomes visible, i.e., for demand- or defer-driven on-the-scroll
714 ;; fontification, (2) a buffer modification occurs, i.e., for defer-driven
715 ;; on-the-fly fontification, (3) Emacs becomes idle, i.e., for fontification of
716 ;; deferred fontification and stealth fontification, and (4) other special
717 ;; occasions.
719 ;; 1. There are three ways whereby this can happen.
721 ;; (a) Scrolling the window, either explicitly (e.g., `scroll-up') or
722 ;; implicitly (e.g., `search-forward'). Here, `window-start' changes.
723 ;; Fontification occurs by adding `lazy-lock-fontify-after-scroll' (for
724 ;; demand-driven fontification) or `lazy-lock-defer-after-scroll' (for
725 ;; defer-driven fontification) to the hook `window-scroll-functions'.
727 (defun lazy-lock-fontify-after-scroll (window window-start)
728 ;; Called from `window-scroll-functions'.
729 ;; Fontify WINDOW from WINDOW-START following the scroll.
730 (let ((inhibit-point-motion-hooks t))
731 (lazy-lock-fontify-region window-start (window-end window t)))
732 ;; A prior deletion that did not cause scrolling, followed by a scroll, would
733 ;; result in an unnecessary trigger after this if we did not cancel it now.
734 (set-window-redisplay-end-trigger window nil))
736 (defun lazy-lock-defer-after-scroll (window window-start)
737 ;; Called from `window-scroll-functions'.
738 ;; Defer fontification following the scroll. Save the current buffer so that
739 ;; we subsequently fontify in all windows showing the buffer.
740 (unless (memq (current-buffer) lazy-lock-buffers)
741 (push (current-buffer) lazy-lock-buffers))
742 ;; A prior deletion that did not cause scrolling, followed by a scroll, would
743 ;; result in an unnecessary trigger after this if we did not cancel it now.
744 (set-window-redisplay-end-trigger window nil))
746 ;; (b) Resizing the window, either explicitly (e.g., `enlarge-window') or
747 ;; implicitly (e.g., `delete-other-windows'). Here, `window-end' changes.
748 ;; Fontification occurs by adding `lazy-lock-fontify-after-resize' to the
749 ;; hook `window-size-change-functions'.
751 (defun lazy-lock-fontify-after-resize (frame)
752 ;; Called from `window-size-change-functions'.
753 ;; Fontify windows in FRAME following the resize. We cannot use
754 ;; `window-start' or `window-end' so we fontify conservatively.
755 (save-excursion
756 (save-selected-window
757 (select-frame frame)
758 (walk-windows (function (lambda (window)
759 (set-buffer (window-buffer window))
760 (when lazy-lock-mode
761 (lazy-lock-fontify-conservatively window))
762 (set-window-redisplay-end-trigger window nil)))
763 'nomini frame))))
765 ;; (c) Deletion in the buffer. Here, a `window-end' marker can become visible.
766 ;; Fontification occurs by adding `lazy-lock-arrange-before-change' to
767 ;; `before-change-functions' and `lazy-lock-fontify-after-trigger' to the
768 ;; hook `redisplay-end-trigger-functions'. Before every deletion, the
769 ;; marker `window-redisplay-end-trigger' position is set to the soon-to-be
770 ;; changed `window-end' position. If the marker becomes visible,
771 ;; `lazy-lock-fontify-after-trigger' gets called. Ouch. Note that we only
772 ;; have to deal with this eventuality if there is no on-the-fly deferral.
774 (defun lazy-lock-arrange-before-change (beg end)
775 ;; Called from `before-change-functions'.
776 ;; Arrange that if text becomes visible it will be fontified (if a deletion
777 ;; is pending, text might become visible at the bottom).
778 (unless (eq beg end)
779 (let ((windows (get-buffer-window-list (current-buffer) 'nomini t)) window)
780 (while windows
781 (setq window (car windows))
782 (unless (markerp (window-redisplay-end-trigger window))
783 (set-window-redisplay-end-trigger window (make-marker)))
784 (set-marker (window-redisplay-end-trigger window) (window-end window))
785 (setq windows (cdr windows))))))
787 (defun lazy-lock-fontify-after-trigger (window trigger-point)
788 ;; Called from `redisplay-end-trigger-functions'.
789 ;; Fontify WINDOW from TRIGGER-POINT following the redisplay.
790 ;; We could probably just use `lazy-lock-fontify-after-scroll' without loss:
791 ;; (inline (lazy-lock-fontify-after-scroll window (window-start window)))
792 (let ((inhibit-point-motion-hooks t))
793 (lazy-lock-fontify-region trigger-point (window-end window t))))
795 ;; 2. Modified text must be marked as unfontified so it can be identified and
796 ;; fontified later when Emacs is idle. Deferral occurs by adding one of
797 ;; `lazy-lock-fontify-*-after-change' (for on-the-fly fontification) or
798 ;; `lazy-lock-defer-*-after-change' (for deferred fontification) to the
799 ;; hook `after-change-functions'.
801 (defalias 'lazy-lock-fontify-line-after-change
802 ;; Called from `after-change-functions'.
803 ;; Fontify the current change.
804 'font-lock-after-change-function)
806 (defun lazy-lock-fontify-rest-after-change (beg end old-len)
807 ;; Called from `after-change-functions'.
808 ;; Fontify the current change and defer fontification of the rest of the
809 ;; buffer. Save the current buffer so that we subsequently fontify in all
810 ;; windows showing the buffer.
811 (lazy-lock-fontify-line-after-change beg end old-len)
812 (save-buffer-state nil
813 (unless (memq (current-buffer) lazy-lock-buffers)
814 (push (current-buffer) lazy-lock-buffers))
815 (save-restriction
816 (widen)
817 (remove-text-properties end (point-max) '(lazy-lock nil)))))
819 (defun lazy-lock-defer-line-after-change (beg end old-len)
820 ;; Called from `after-change-functions'.
821 ;; Defer fontification of the current change. Save the current buffer so
822 ;; that we subsequently fontify in all windows showing the buffer.
823 (save-buffer-state nil
824 (unless (memq (current-buffer) lazy-lock-buffers)
825 (push (current-buffer) lazy-lock-buffers))
826 (remove-text-properties (max (1- beg) (point-min))
827 (min (1+ end) (point-max))
828 '(lazy-lock nil))))
830 (defun lazy-lock-defer-rest-after-change (beg end old-len)
831 ;; Called from `after-change-functions'.
832 ;; Defer fontification of the rest of the buffer. Save the current buffer so
833 ;; that we subsequently fontify in all windows showing the buffer.
834 (save-buffer-state nil
835 (unless (memq (current-buffer) lazy-lock-buffers)
836 (push (current-buffer) lazy-lock-buffers))
837 (save-restriction
838 (widen)
839 (remove-text-properties (max (1- beg) (point-min))
840 (point-max)
841 '(lazy-lock nil)))))
843 ;; 3. Deferred fontification and stealth fontification are done from these two
844 ;; functions. They are set up as Idle Timers.
846 (defun lazy-lock-fontify-after-defer ()
847 ;; Called from `timer-idle-list'.
848 ;; Fontify all windows where deferral has occurred for its buffer.
849 (save-excursion
850 (while (and lazy-lock-buffers (not (input-pending-p)))
851 (let ((buffer (car lazy-lock-buffers)) windows)
852 ;; Paranoia: check that the buffer is still live and Lazy Lock mode on.
853 (when (buffer-live-p buffer)
854 (set-buffer buffer)
855 (when lazy-lock-mode
856 (setq windows (get-buffer-window-list buffer 'nomini t))
857 (while windows
858 (lazy-lock-fontify-window (car windows))
859 (setq windows (cdr windows)))))
860 (setq lazy-lock-buffers (cdr lazy-lock-buffers)))))
861 ;; Add hook if fontification should now be defer-driven in this buffer.
862 (when (and lazy-lock-mode lazy-lock-defer-on-scrolling
863 (memq 'lazy-lock-fontify-after-scroll window-scroll-functions)
864 (not (or (input-pending-p) (lazy-lock-unfontified-p))))
865 (remove-hook 'window-scroll-functions 'lazy-lock-fontify-after-scroll t)
866 (add-hook 'window-scroll-functions 'lazy-lock-defer-after-scroll nil t)))
868 (defun lazy-lock-fontify-after-idle ()
869 ;; Called from `timer-idle-list'.
870 ;; Fontify all buffers that need it, stealthily while idle.
871 (unless (or executing-kbd-macro (window-minibuffer-p (selected-window)))
872 ;; Loop over all buffers, fontify stealthily for each if necessary.
873 (let ((buffers (buffer-list)) (continue t)
874 message message-log-max minibuffer-auto-raise)
875 (save-excursion
876 (do-while (and buffers continue)
877 (set-buffer (car buffers))
878 (if (not (and lazy-lock-mode (lazy-lock-unfontified-p)))
879 (setq continue (not (input-pending-p)))
880 ;; Fontify regions in this buffer while there is no input.
881 (with-temp-message
882 (when lazy-lock-stealth-verbose
883 "Fontifying stealthily...")
884 (do-while (and (lazy-lock-unfontified-p) continue)
885 (if (and lazy-lock-stealth-load
886 (> (car (load-average)) lazy-lock-stealth-load))
887 ;; Wait a while before continuing with the loop.
888 (progn
889 (when message
890 (message "Fontifying stealthily...suspended")
891 (setq message nil))
892 (setq continue (sit-for (or lazy-lock-stealth-time 30))))
893 ;; Fontify a chunk.
894 (when lazy-lock-stealth-verbose
895 (if message
896 (message "Fontifying stealthily... %2d%% of %s"
897 (lazy-lock-percent-fontified) (buffer-name))
898 (message "Fontifying stealthily...")
899 (setq message t)))
900 (lazy-lock-fontify-chunk)
901 (setq continue (sit-for (or lazy-lock-stealth-nice 0)))))))
902 (setq buffers (cdr buffers)))))))
904 ;; 4. Special circumstances.
906 (defun lazy-lock-fontify-after-visage ()
907 ;; Called from `outline-view-change-hook' and `hs-hide-hook'.
908 ;; Fontify windows showing the current buffer, as its visibility has changed.
909 ;; This is a conspiracy hack between lazy-lock.el, outline.el and
910 ;; hideshow.el.
911 (let ((windows (get-buffer-window-list (current-buffer) 'nomini t)))
912 (while windows
913 (lazy-lock-fontify-conservatively (car windows))
914 (setq windows (cdr windows)))))
916 (defun lazy-lock-after-fontify-buffer ()
917 ;; Called from `font-lock-after-fontify-buffer'.
918 ;; Mark the current buffer as fontified.
919 ;; This is a conspiracy hack between lazy-lock.el and font-lock.el.
920 (save-buffer-state nil
921 (add-text-properties (point-min) (point-max) '(lazy-lock t))))
923 (defun lazy-lock-after-unfontify-buffer ()
924 ;; Called from `font-lock-after-unfontify-buffer'.
925 ;; Mark the current buffer as unfontified.
926 ;; This is a conspiracy hack between lazy-lock.el and font-lock.el.
927 (save-buffer-state nil
928 (remove-text-properties (point-min) (point-max) '(lazy-lock nil))))
930 ;; Fontification functions.
932 ;; If packages want to ensure that some region of the buffer is fontified, they
933 ;; should use this function. For an example, see ps-print.el.
934 (defun lazy-lock-fontify-region (beg end)
935 ;; Fontify between BEG and END, where necessary, in the current buffer.
936 (when (setq beg (text-property-any beg end 'lazy-lock nil))
937 (save-excursion
938 (save-match-data
939 (save-buffer-state
940 ;; Ensure syntactic fontification is always correct.
941 (font-lock-beginning-of-syntax-function next)
942 ;; Find successive unfontified regions between BEG and END.
943 (condition-case data
944 (do-while beg
945 (setq next (or (text-property-any beg end 'lazy-lock t) end))
946 ;; Make sure the region end points are at beginning of line.
947 (goto-char beg)
948 (unless (bolp)
949 (beginning-of-line)
950 (setq beg (point)))
951 (goto-char next)
952 (unless (bolp)
953 (forward-line)
954 (setq next (point)))
955 ;; Fontify the region, then flag it as fontified.
956 (font-lock-fontify-region beg next)
957 (add-text-properties beg next '(lazy-lock t))
958 (setq beg (text-property-any next end 'lazy-lock nil)))
959 ((error quit) (message "Fontifying region...%s" data))))))))
961 (defun lazy-lock-fontify-chunk ()
962 ;; Fontify the nearest chunk, for stealth, in the current buffer.
963 (let ((inhibit-point-motion-hooks t))
964 (save-excursion
965 (save-restriction
966 (widen)
967 ;; Move to end of line in case the character at point is not fontified.
968 (end-of-line)
969 ;; Find where the previous (next) unfontified regions end (begin).
970 (let ((prev (previous-single-property-change (point) 'lazy-lock))
971 (next (text-property-any (point) (point-max) 'lazy-lock nil)))
972 ;; Fontify from the nearest unfontified position.
973 (if (or (null prev) (and next (< (- next (point)) (- (point) prev))))
974 ;; The next, or neither, region is the nearest not fontified.
975 (lazy-lock-fontify-region
976 (progn (goto-char (or next (point-min)))
977 (beginning-of-line)
978 (point))
979 (progn (goto-char (or next (point-min)))
980 (forward-line lazy-lock-stealth-lines)
981 (point)))
982 ;; The previous region is the nearest not fontified.
983 (lazy-lock-fontify-region
984 (progn (goto-char prev)
985 (forward-line (- lazy-lock-stealth-lines))
986 (point))
987 (progn (goto-char prev)
988 (forward-line)
989 (point)))))))))
991 (defun lazy-lock-fontify-window (window)
992 ;; Fontify in WINDOW between `window-start' and `window-end'.
993 ;; We can only do this when we can use `window-start' and `window-end'.
994 (with-current-buffer (window-buffer window)
995 (lazy-lock-fontify-region (window-start window) (window-end window))))
997 (defun lazy-lock-fontify-conservatively (window)
998 ;; Fontify in WINDOW conservatively around point.
999 ;; Where we cannot use `window-start' and `window-end' we do `window-height'
1000 ;; lines around point. That way we guarantee to have done enough.
1001 (with-current-buffer (window-buffer window)
1002 (let ((inhibit-point-motion-hooks t))
1003 (lazy-lock-fontify-region
1004 (save-excursion
1005 (goto-char (window-point window))
1006 (vertical-motion (- (window-height window)) window) (point))
1007 (save-excursion
1008 (goto-char (window-point window))
1009 (vertical-motion (window-height window) window) (point))))))
1011 (defun lazy-lock-unfontified-p ()
1012 ;; Return non-nil if there is anywhere still to be fontified.
1013 (save-restriction
1014 (widen)
1015 (text-property-any (point-min) (point-max) 'lazy-lock nil)))
1017 (defun lazy-lock-percent-fontified ()
1018 ;; Return the percentage (of characters) of the buffer that are fontified.
1019 (save-restriction
1020 (widen)
1021 (let ((beg (point-min)) (size 0) next)
1022 ;; Find where the next fontified region begins.
1023 (while (setq beg (text-property-any beg (point-max) 'lazy-lock t))
1024 (setq next (or (text-property-any beg (point-max) 'lazy-lock nil)
1025 (point-max)))
1026 (incf size (- next beg))
1027 (setq beg next))
1028 ;; Float because using integer multiplication will frequently overflow.
1029 (truncate (* (/ (float size) (point-max)) 100)))))
1031 ;; Version dependent workarounds and fixes.
1033 (when (if (save-match-data (string-match "Lucid\\|XEmacs" (emacs-version)))
1035 (and (= emacs-major-version 19) (= emacs-minor-version 30)))
1037 ;; We use `post-command-idle-hook' for deferral and stealth. Oh Lordy.
1038 (defun lazy-lock-install-timers (foo bar)
1039 (add-hook 'post-command-idle-hook 'lazy-lock-fontify-post-command t)
1040 (add-hook 'post-command-idle-hook 'lazy-lock-fontify-post-idle t)
1041 (add-to-list 'lazy-lock-install (current-buffer))
1042 (add-hook 'post-command-hook 'lazy-lock-fontify-after-install))
1043 (defun lazy-lock-fontify-post-command ()
1044 (and lazy-lock-buffers (not executing-kbd-macro)
1045 (progn
1046 (and deactivate-mark (deactivate-mark))
1047 (sit-for
1048 (or (cdr-safe lazy-lock-defer-time) lazy-lock-defer-time 0)))
1049 (lazy-lock-fontify-after-defer)))
1050 (defun lazy-lock-fontify-post-idle ()
1051 (and lazy-lock-stealth-time (not executing-kbd-macro)
1052 (not (window-minibuffer-p (selected-window)))
1053 (progn
1054 (and deactivate-mark (deactivate-mark))
1055 (sit-for lazy-lock-stealth-time))
1056 (lazy-lock-fontify-after-idle)))
1058 ;; Simulate running of `window-scroll-functions' in `set-window-buffer'.
1059 (defvar lazy-lock-install nil)
1060 (defun lazy-lock-fontify-after-install ()
1061 (remove-hook 'post-command-hook 'lazy-lock-fontify-after-install)
1062 (while lazy-lock-install
1063 (mapcar 'lazy-lock-fontify-conservatively
1064 (get-buffer-window-list (pop lazy-lock-install) 'nomini t)))))
1066 (when (if (save-match-data (string-match "Lucid\\|XEmacs" (emacs-version)))
1068 (or (and (= emacs-major-version 20) (< emacs-minor-version 4))
1069 (= emacs-major-version 19)))
1071 ;; We use `vertical-motion' rather than `window-end' UPDATE arg.
1072 (defun lazy-lock-fontify-after-scroll (window window-start)
1073 ;; Called from `window-scroll-functions'.
1074 ;; Fontify WINDOW from WINDOW-START following the scroll. We cannot use
1075 ;; `window-end' so we work out what it would be via `vertical-motion'.
1076 (let ((inhibit-point-motion-hooks t))
1077 (save-excursion
1078 (goto-char window-start)
1079 (vertical-motion (window-height window) window)
1080 (lazy-lock-fontify-region window-start (point))))
1081 (set-window-redisplay-end-trigger window nil))
1082 (defun lazy-lock-fontify-after-trigger (window trigger-point)
1083 ;; Called from `redisplay-end-trigger-functions'.
1084 ;; Fontify WINDOW from TRIGGER-POINT following the redisplay. We cannot
1085 ;; use `window-end' so we work out what it would be via `vertical-motion'.
1086 (let ((inhibit-point-motion-hooks t))
1087 (save-excursion
1088 (goto-char (window-start window))
1089 (vertical-motion (window-height window) window)
1090 (lazy-lock-fontify-region trigger-point (point))))))
1092 (when (consp lazy-lock-defer-time)
1094 ;; In 2.06.04 and below, `lazy-lock-defer-time' could specify modes and time.
1095 (with-output-to-temp-buffer "*Help*"
1096 (princ "The value of the variable `lazy-lock-defer-time' was\n ")
1097 (princ lazy-lock-defer-time)
1098 (princ "\n")
1099 (princ "This variable cannot now be a list of modes and time,\n")
1100 (princ "so instead use ")
1101 (princ (substitute-command-keys "\\[customize-option]"))
1102 (princ " to modify the variables, or put the forms:\n")
1103 (princ " (setq lazy-lock-defer-time ")
1104 (princ (cdr lazy-lock-defer-time))
1105 (princ ")\n")
1106 (princ " (setq lazy-lock-defer-on-the-fly '")
1107 (princ (car lazy-lock-defer-time))
1108 (princ ")\n")
1109 (princ "in your ~/.emacs. ")
1110 (princ "The above forms have been evaluated for this editor session,\n")
1111 (princ "but you should use ")
1112 (princ (substitute-command-keys "\\[customize-option]"))
1113 (princ " or change your ~/.emacs now."))
1114 (setq lazy-lock-defer-on-the-fly (car lazy-lock-defer-time)
1115 lazy-lock-defer-time (cdr lazy-lock-defer-time)))
1117 (when (boundp 'lazy-lock-defer-driven)
1119 ;; In 2.06.04 and below, `lazy-lock-defer-driven' was the variable name.
1120 (with-output-to-temp-buffer "*Help*"
1121 (princ "The value of the variable `lazy-lock-defer-driven' is set to ")
1122 (if (memq lazy-lock-defer-driven '(nil t))
1123 (princ lazy-lock-defer-driven)
1124 (princ "`")
1125 (princ lazy-lock-defer-driven)
1126 (princ "'"))
1127 (princ ".\n")
1128 (princ "This variable is now called `lazy-lock-defer-on-scrolling',\n")
1129 (princ "so instead use ")
1130 (princ (substitute-command-keys "\\[customize-option]"))
1131 (princ " to modify the variable, or put the form:\n")
1132 (princ " (setq lazy-lock-defer-on-scrolling ")
1133 (unless (memq lazy-lock-defer-driven '(nil t))
1134 (princ "'"))
1135 (princ lazy-lock-defer-driven)
1136 (princ ")\n")
1137 (princ "in your ~/.emacs. ")
1138 (princ "The above form has been evaluated for this editor session,\n")
1139 (princ "but you should use ")
1140 (princ (substitute-command-keys "\\[customize-option]"))
1141 (princ " or change your ~/.emacs now."))
1142 (setq lazy-lock-defer-on-scrolling lazy-lock-defer-driven))
1144 ;; Possibly absent.
1146 (unless (boundp 'font-lock-inhibit-thing-lock)
1147 ;; Font Lock mode uses this to direct Lazy and Fast Lock modes to stay off.
1148 (defvar font-lock-inhibit-thing-lock nil
1149 "List of Font Lock mode related modes that should not be turned on."))
1151 (unless (fboundp 'font-lock-value-in-major-mode)
1152 (defun font-lock-value-in-major-mode (alist)
1153 ;; Return value in ALIST for `major-mode'.
1154 (if (consp alist)
1155 (cdr (or (assq major-mode alist) (assq t alist)))
1156 alist)))
1158 (unless (fboundp 'buffer-live-p)
1159 ;; We use this to check that a buffer we have to fontify still exists.
1160 (defun buffer-live-p (object)
1161 "Return non-nil if OBJECT is an editor buffer that has not been deleted."
1162 (and (bufferp object) (buffer-name object))))
1164 (unless (fboundp 'get-buffer-window-list)
1165 ;; We use this to get all windows showing a buffer we have to fontify.
1166 (defun get-buffer-window-list (buffer &optional minibuf frame)
1167 "Return windows currently displaying BUFFER, or nil if none."
1168 (let ((buffer (if (bufferp buffer) buffer (get-buffer buffer))) windows)
1169 (walk-windows (function (lambda (window)
1170 (when (eq (window-buffer window) buffer)
1171 (push window windows))))
1172 minibuf frame)
1173 windows)))
1175 (unless (fboundp 'current-message)
1176 (defun current-message ()
1177 ""))
1179 ;; Install ourselves:
1181 (add-hook 'window-size-change-functions 'lazy-lock-fontify-after-resize)
1182 (add-hook 'redisplay-end-trigger-functions 'lazy-lock-fontify-after-trigger)
1184 (unless (assq 'lazy-lock-mode minor-mode-alist)
1185 (setq minor-mode-alist (append minor-mode-alist '((lazy-lock-mode nil)))))
1187 ;; Provide ourselves:
1189 (provide 'lazy-lock)
1191 ;;; lazy-lock.el ends here