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