Update from author.
[bpt/emacs.git] / lisp / lazy-lock.el
CommitLineData
2eb9703d
RS
1;;; lazy-lock.el --- Lazy demand-driven fontification for fast Font Lock mode.
2
58523e00 3;; Copyright (C) 1994, 1995, 1996, 1997, 1998 Free Software Foundation, Inc.
2eb9703d 4
7a31427e 5;; Author: Simon Marshall <simon@gnu.org>
26543889 6;; Maintainer: FSF
2eb9703d 7;; Keywords: faces files
58523e00 8;; Version: 2.11
2eb9703d
RS
9
10;;; This file is part of GNU Emacs.
11
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 2, or (at your option)
15;; any later version.
16
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.
21
22;; You should have received a copy of the GNU General Public License
23;; along with GNU Emacs; see the file COPYING. If not, write to the
24;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
25;; Boston, MA 02111-1307, USA.
26
27;;; Commentary:
28
caf1765c
SM
29;; Purpose:
30;;
d7606d13
SM
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.
2eb9703d
RS
35;;
36;; See caveats and feedback below.
37;; See also the fast-lock package. (But don't use them at the same time!)
38
39;; Installation:
40;;
41;; Put in your ~/.emacs:
42;;
6a02d88b 43;; (setq font-lock-support-mode 'lazy-lock-mode)
2eb9703d
RS
44;;
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).
47;;
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.
56
57;; User-visible differences with version 1:
58;;
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.
61;;
62;; A number of variables have changed meaning:
63;;
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.
68;;
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.
73
74;; Implementation differences with version 1:
75;;
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':
81;;
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.
88;;
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.
95;;
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).
106;;
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
d7606d13
SM
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.
2eb9703d 116;;
caf1765c 117;; (Ben Wing thinks these hooks are "horribly horribly kludgy", and implemented
2eb9703d
RS
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.
125;;
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.
133;;
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':
138;;
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.)
146;;
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
d7606d13 153;; similar to an Emacs Idle Timer function with a fixed zero second timeout.)
2eb9703d 154
caf1765c
SM
155;; - Version 1 has the following problems (relative to version 2):
156;;
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.
162;;
163;; Ben's hacked-up lazy-lock.el 1.14 almost solved (b) but made (c) worse.
164;;
165;; - Version 2 has the following additional features (relative to version 1):
166;;
167;; (a) It can defer fontification (both on-the-fly and on-scrolling).
168;; (b) It can fontify contextually (syntactically true on-the-fly).
169
2eb9703d
RS
170;; Caveats:
171;;
d7606d13
SM
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.
2eb9703d
RS
176;;
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.
181;;
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!
185;;
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!
190;;
6a02d88b
SM
191;; Currently XEmacs does not have the features to support this version of
192;; lazy-lock.el. Maybe it will one day.
2eb9703d 193\f
54d893e3
SM
194;; History:
195;;
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)
b14fbaa0 257;; - Made various wrapping `inhibit-point-motion-hooks' (Vinicius Latorre hint)
86c87ebc 258;; - Made `lazy-lock-fontify-after-idle' wrap `minibuffer-auto-raise'
7a31427e 259;; - Made `lazy-lock-fontify-after-defer' paranoid about deferred buffers
ce2cc837 260;; 2.09--2.10:
58523e00 261;; - Use `window-end' UPDATE arg for Emacs 20.4 and later.
12837971
SM
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)
58523e00
SM
265;; 2.10--2.11:
266;; - Used `with-temp-message' where possible to make messages temporary.
54d893e3 267\f
d7606d13
SM
268;;; Code:
269
2eb9703d
RS
270(require 'font-lock)
271
2eb9703d 272(eval-when-compile
2eb9703d 273 ;; We don't do this at the top-level as we only use non-autoloaded macros.
54d893e3
SM
274 (require 'cl)
275 ;;
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)) (buffer-undo-list t)
281 (inhibit-read-only t) (inhibit-point-motion-hooks t)
282 before-change-functions after-change-functions
283 deactivate-mark buffer-file-name buffer-file-truename))))
284 (,@ body)
285 (when (and (not modified) (buffer-modified-p))
286 (set-buffer-modified-p nil)))))
287 (put 'save-buffer-state 'lisp-indent-function 1)
288 ;;
289 ;; We use this for clarity and speed. Naughty but nice.
290 (defmacro do-while (test &rest body)
291 "(do-while TEST BODY...): eval BODY... and repeat if TEST yields non-nil.
2eb9703d
RS
292The order of execution is thus BODY, TEST, BODY, TEST and so on
293until TEST returns nil."
54d893e3 294 (` (while (progn (,@ body) (, test)))))
8242a6dc 295 (put 'do-while 'lisp-indent-function (get 'while 'lisp-indent-function)))
d7606d13 296
caf1765c
SM
297(defvar lazy-lock-mode nil) ; Whether we are turned on.
298(defvar lazy-lock-buffers nil) ; For deferral.
299(defvar lazy-lock-timers (cons nil nil)) ; For deferral and stealth.
2eb9703d
RS
300\f
301;; User Variables:
302
58523e00 303(defcustom lazy-lock-minimum-size 25600
2eb9703d
RS
304 "*Minimum size of a buffer for demand-driven fontification.
305On-demand fontification occurs if the buffer size is greater than this value.
306If nil, means demand-driven fontification is never performed.
307If a list, each element should be a cons pair of the form (MAJOR-MODE . SIZE),
308where MAJOR-MODE is a symbol or t (meaning the default). For example:
309 ((c-mode . 25600) (c++-mode . 25600) (rmail-mode . 1048576))
310means that the minimum size is 25K for buffers in C or C++ modes, one megabyte
311for buffers in Rmail mode, and size is irrelevant otherwise.
312
caf1765c 313The value of this variable is used when Lazy Lock mode is turned on."
b34aa0a3
SM
314 :type '(choice (const :tag "none" nil)
315 (integer :tag "size")
316 (repeat :menu-tag "mode specific" :tag "mode specific"
317 :value ((t . nil))
318 (cons :tag "Instance"
319 (radio :tag "Mode"
320 (const :tag "all" t)
321 (symbol :tag "name"))
322 (radio :tag "Size"
323 (const :tag "none" nil)
324 (integer :tag "size")))))
caf1765c 325 :group 'lazy-lock)
2eb9703d 326
caf1765c 327(defcustom lazy-lock-defer-on-the-fly t
d7606d13
SM
328 "*If non-nil, means fontification after a change should be deferred.
329If nil, means on-the-fly fontification is performed. This means when changes
330occur in the buffer, those areas are immediately fontified.
331If a list, it should be a list of `major-mode' symbol names for which deferred
332fontification should occur. The sense of the list is negated if it begins with
333`not'. For example:
334 (c-mode c++-mode)
335means that on-the-fly fontification is deferred for buffers in C and C++ modes
336only, and deferral does not occur otherwise.
337
caf1765c 338The value of this variable is used when Lazy Lock mode is turned on."
b34aa0a3
SM
339 :type '(choice (const :tag "never" nil)
340 (const :tag "always" t)
341 (set :menu-tag "mode specific" :tag "modes"
342 :value (not)
343 (const :tag "Except" not)
344 (repeat :inline t (symbol :tag "mode"))))
caf1765c 345 :group 'lazy-lock)
d7606d13 346
caf1765c 347(defcustom lazy-lock-defer-on-scrolling nil
d7606d13 348 "*If non-nil, means fontification after a scroll should be deferred.
6a02d88b
SM
349If nil, means demand-driven fontification is performed. This means when
350scrolling into unfontified areas of the buffer, those areas are immediately
351fontified. Thus scrolling never presents unfontified areas. However, since
352fontification occurs during scrolling, scrolling may be slow.
2eb9703d
RS
353If t, means defer-driven fontification is performed. This means fontification
354of those areas is deferred. Thus scrolling may present momentarily unfontified
355areas. However, since fontification does not occur during scrolling, scrolling
356will be faster than demand-driven fontification.
6a02d88b
SM
357If any other value, e.g., `eventually', means demand-driven fontification is
358performed until the buffer is fontified, then buffer fontification becomes
359defer-driven. Thus scrolling never presents unfontified areas until the buffer
360is first fontified, after which subsequent scrolling may present future buffer
361insertions momentarily unfontified. However, since fontification does not
362occur during scrolling after the buffer is first fontified, scrolling will
d7606d13
SM
363become faster. (But, since contextual changes continually occur, such a value
364makes little sense if `lazy-lock-defer-contextually' is non-nil.)
365
caf1765c 366The value of this variable is used when Lazy Lock mode is turned on."
b34aa0a3
SM
367 :type '(choice (const :tag "never" nil)
368 (const :tag "always" t)
13eacf88 369 (other :tag "eventually" eventually))
caf1765c 370 :group 'lazy-lock)
d7606d13 371
caf1765c 372(defcustom lazy-lock-defer-contextually 'syntax-driven
d7606d13
SM
373 "*If non-nil, means deferred fontification should be syntactically true.
374If nil, means deferred fontification occurs only on those lines modified. This
375means where modification on a line causes syntactic change on subsequent lines,
376those subsequent lines are not refontified to reflect their new context.
377If t, means deferred fontification occurs on those lines modified and all
378subsequent lines. This means those subsequent lines are refontified to reflect
379their new syntactic context, either immediately or when scrolling into them.
380If any other value, e.g., `syntax-driven', means deferred syntactically true
381fontification occurs only if syntactic fontification is performed using the
382buffer mode's syntax table, i.e., only if `font-lock-keywords-only' is nil.
2eb9703d 383
caf1765c 384The value of this variable is used when Lazy Lock mode is turned on."
b34aa0a3
SM
385 :type '(choice (const :tag "never" nil)
386 (const :tag "always" t)
13eacf88 387 (other :tag "syntax-driven" syntax-driven))
caf1765c 388 :group 'lazy-lock)
2eb9703d 389
caf1765c 390(defcustom lazy-lock-defer-time
b14fbaa0 391 (if (featurep 'lisp-float-type) (/ (float 1) (float 4)) 1)
2eb9703d
RS
392 "*Time in seconds to delay before beginning deferred fontification.
393Deferred fontification occurs if there is no input within this time.
d7606d13
SM
394If nil, means fontification is never deferred, regardless of the values of the
395variables `lazy-lock-defer-on-the-fly', `lazy-lock-defer-on-scrolling' and
396`lazy-lock-defer-contextually'.
2eb9703d 397
caf1765c 398The value of this variable is used when Lazy Lock mode is turned on."
b34aa0a3
SM
399 :type '(choice (const :tag "never" nil)
400 (number :tag "seconds"))
caf1765c 401 :group 'lazy-lock)
2eb9703d 402
caf1765c 403(defcustom lazy-lock-stealth-time 30
2eb9703d
RS
404 "*Time in seconds to delay before beginning stealth fontification.
405Stealth fontification occurs if there is no input within this time.
406If nil, means stealth fontification is never performed.
407
caf1765c 408The value of this variable is used when Lazy Lock mode is turned on."
b34aa0a3
SM
409 :type '(choice (const :tag "never" nil)
410 (number :tag "seconds"))
caf1765c 411 :group 'lazy-lock)
2eb9703d 412
caf1765c 413(defcustom lazy-lock-stealth-lines (if font-lock-maximum-decoration 100 250)
2eb9703d
RS
414 "*Maximum size of a chunk of stealth fontification.
415Each iteration of stealth fontification can fontify this number of lines.
416To speed up input response during stealth fontification, at the cost of stealth
caf1765c 417taking longer to fontify, you could reduce the value of this variable."
b34aa0a3 418 :type '(integer :tag "lines")
caf1765c 419 :group 'lazy-lock)
2eb9703d 420
caf1765c 421(defcustom lazy-lock-stealth-load
fc24e7fb 422 (if (condition-case nil (load-average) (error)) 200)
d7606d13
SM
423 "*Load in percentage above which stealth fontification is suspended.
424Stealth fontification pauses when the system short-term load average (as
425returned by the function `load-average' if supported) goes above this level,
426thus reducing the demand that stealth fontification makes on the system.
427If nil, means stealth fontification is never suspended.
428To reduce machine load during stealth fontification, at the cost of stealth
429taking longer to fontify, you could reduce the value of this variable.
caf1765c 430See also `lazy-lock-stealth-nice'."
b34aa0a3
SM
431 :type (if (condition-case nil (load-average) (error))
432 '(choice (const :tag "never" nil)
433 (integer :tag "load"))
54d893e3 434 '(const :format "%t: unsupported\n" nil))
caf1765c 435 :group 'lazy-lock)
d7606d13 436
caf1765c 437(defcustom lazy-lock-stealth-nice
2eb9703d
RS
438 (if (featurep 'lisp-float-type) (/ (float 1) (float 8)) 1)
439 "*Time in seconds to pause between chunks of stealth fontification.
d7606d13
SM
440Each iteration of stealth fontification is separated by this amount of time,
441thus reducing the demand that stealth fontification makes on the system.
442If nil, means stealth fontification is never paused.
2eb9703d 443To reduce machine load during stealth fontification, at the cost of stealth
d7606d13 444taking longer to fontify, you could increase the value of this variable.
caf1765c 445See also `lazy-lock-stealth-load'."
b34aa0a3
SM
446 :type '(choice (const :tag "never" nil)
447 (number :tag "seconds"))
caf1765c 448 :group 'lazy-lock)
2eb9703d 449
caf1765c 450(defcustom lazy-lock-stealth-verbose
fc24e7fb
SM
451 (if (featurep 'lisp-float-type)
452 (and (not lazy-lock-defer-contextually) (not (null font-lock-verbose))))
caf1765c
SM
453 "*If non-nil, means stealth fontification should show status messages."
454 :type 'boolean
455 :group 'lazy-lock)
2eb9703d
RS
456\f
457;; User Functions:
458
459;;;###autoload
460(defun lazy-lock-mode (&optional arg)
461 "Toggle Lazy Lock mode.
462With arg, turn Lazy Lock mode on if and only if arg is positive. Enable it
463automatically in your `~/.emacs' by:
464
465 (setq font-lock-support-mode 'lazy-lock-mode)
466
467When Lazy Lock mode is enabled, fontification can be lazy in a number of ways:
468
d7606d13
SM
469- Demand-driven buffer fontification if `lazy-lock-minimum-size' is non-nil.
470 This means initial fontification does not occur if the buffer is greater than
471 `lazy-lock-minimum-size' characters in length. Instead, fontification occurs
472 when necessary, such as when scrolling through the buffer would otherwise
473 reveal unfontified areas. This is useful if buffer fontification is too slow
474 for large buffers.
475
476- Deferred scroll fontification if `lazy-lock-defer-on-scrolling' is non-nil.
477 This means demand-driven fontification does not occur as you scroll.
478 Instead, fontification is deferred until after `lazy-lock-defer-time' seconds
479 of Emacs idle time, while Emacs remains idle. This is useful if
480 fontification is too slow to keep up with scrolling.
481
482- Deferred on-the-fly fontification if `lazy-lock-defer-on-the-fly' is non-nil.
483 This means on-the-fly fontification does not occur as you type. Instead,
484 fontification is deferred until after `lazy-lock-defer-time' seconds of Emacs
485 idle time, while Emacs remains idle. This is useful if fontification is too
486 slow to keep up with your typing.
487
488- Deferred context fontification if `lazy-lock-defer-contextually' is non-nil.
489 This means fontification updates the buffer corresponding to true syntactic
490 context, after `lazy-lock-defer-time' seconds of Emacs idle time, while Emacs
491 remains idle. Otherwise, fontification occurs on modified lines only, and
492 subsequent lines can remain fontified corresponding to previous syntactic
493 contexts. This is useful where strings or comments span lines.
494
495- Stealthy buffer fontification if `lazy-lock-stealth-time' is non-nil.
496 This means remaining unfontified areas of buffers are fontified if Emacs has
497 been idle for `lazy-lock-stealth-time' seconds, while Emacs remains idle.
498 This is useful if any buffer has any deferred fontification.
499
500Basic Font Lock mode on-the-fly fontification behaviour fontifies modified
501lines only. Thus, if `lazy-lock-defer-contextually' is non-nil, Lazy Lock mode
502on-the-fly fontification may fontify differently, albeit correctly. In any
503event, to refontify some lines you can use \\[font-lock-fontify-block].
504
505Stealth fontification only occurs while the system remains unloaded.
506If the system load rises above `lazy-lock-stealth-load' percent, stealth
507fontification is suspended. Stealth fontification intensity is controlled via
508the variable `lazy-lock-stealth-nice' and `lazy-lock-stealth-lines', and
509verbosity is controlled via the variable `lazy-lock-stealth-verbose'."
2eb9703d 510 (interactive "P")
d7606d13
SM
511 (let* ((was-on lazy-lock-mode)
512 (now-on (unless (memq 'lazy-lock-mode font-lock-inhibit-thing-lock)
513 (if arg (> (prefix-numeric-value arg) 0) (not was-on)))))
514 (cond ((and now-on (not font-lock-mode))
515 ;; Turned on `lazy-lock-mode' rather than `font-lock-mode'.
516 (let ((font-lock-support-mode 'lazy-lock-mode))
517 (font-lock-mode t)))
518 (now-on
519 ;; Turn ourselves on.
520 (set (make-local-variable 'lazy-lock-mode) t)
521 (lazy-lock-install))
522 (was-on
523 ;; Turn ourselves off.
524 (set (make-local-variable 'lazy-lock-mode) nil)
525 (lazy-lock-unstall)))))
2eb9703d
RS
526
527;;;###autoload
528(defun turn-on-lazy-lock ()
529 "Unconditionally turn on Lazy Lock mode."
530 (lazy-lock-mode t))
531
532(defun lazy-lock-install ()
d7606d13
SM
533 (let ((min-size (font-lock-value-in-major-mode lazy-lock-minimum-size))
534 (defer-change (and lazy-lock-defer-time lazy-lock-defer-on-the-fly))
535 (defer-scroll (and lazy-lock-defer-time lazy-lock-defer-on-scrolling))
536 (defer-context (and lazy-lock-defer-time lazy-lock-defer-contextually
537 (or (eq lazy-lock-defer-contextually t)
538 (null font-lock-keywords-only)))))
2eb9703d
RS
539 ;;
540 ;; Tell Font Lock whether Lazy Lock will do fontification.
541 (make-local-variable 'font-lock-fontified)
542 (setq font-lock-fontified (and min-size (>= (buffer-size) min-size)))
543 ;;
544 ;; Add the text properties and fontify.
545 (if (not font-lock-fontified)
546 (lazy-lock-after-fontify-buffer)
547 ;; Make sure we fontify in any existing windows showing the buffer.
548 (let ((windows (get-buffer-window-list (current-buffer) 'nomini t)))
549 (lazy-lock-after-unfontify-buffer)
550 (while windows
551 (lazy-lock-fontify-conservatively (car windows))
552 (setq windows (cdr windows)))))
553 ;;
554 ;; Add the fontification hooks.
555 (lazy-lock-install-hooks
2eb9703d 556 font-lock-fontified
d7606d13
SM
557 (cond ((eq (car-safe defer-change) 'not)
558 (not (memq major-mode (cdr defer-change))))
559 ((listp defer-change)
560 (memq major-mode defer-change))
561 (t
562 defer-change))
563 (eq defer-scroll t)
564 defer-context)
2eb9703d
RS
565 ;;
566 ;; Add the fontification timers.
567 (lazy-lock-install-timers
d7606d13 568 (if (or defer-change defer-scroll defer-context) lazy-lock-defer-time)
2eb9703d
RS
569 lazy-lock-stealth-time)))
570
d7606d13
SM
571(defun lazy-lock-install-hooks (fontifying
572 defer-change defer-scroll defer-context)
2eb9703d 573 ;;
d7606d13
SM
574 ;; Add hook if lazy-lock.el is fontifying on scrolling or is deferring.
575 (when (or fontifying defer-change defer-scroll defer-context)
2eb9703d 576 (make-local-hook 'window-scroll-functions)
d7606d13 577 (add-hook 'window-scroll-functions (if defer-scroll
2eb9703d
RS
578 'lazy-lock-defer-after-scroll
579 'lazy-lock-fontify-after-scroll)
580 nil t))
581 ;;
d7606d13
SM
582 ;; Add hook if lazy-lock.el is fontifying and is not deferring changes.
583 (when (and fontifying (not defer-change) (not defer-context))
2eb9703d
RS
584 (make-local-hook 'before-change-functions)
585 (add-hook 'before-change-functions 'lazy-lock-arrange-before-change nil t))
586 ;;
d7606d13
SM
587 ;; Replace Font Lock mode hook.
588 (remove-hook 'after-change-functions 'font-lock-after-change-function t)
589 (add-hook 'after-change-functions
590 (cond ((and defer-change defer-context)
591 'lazy-lock-defer-rest-after-change)
592 (defer-change
593 'lazy-lock-defer-line-after-change)
594 (defer-context
595 'lazy-lock-fontify-rest-after-change)
596 (t
597 'lazy-lock-fontify-line-after-change))
598 nil t)
2eb9703d 599 ;;
d7606d13 600 ;; Add package-specific hook.
2eb9703d 601 (make-local-hook 'outline-view-change-hook)
12837971
SM
602 (add-hook 'outline-view-change-hook 'lazy-lock-fontify-after-visage nil t)
603 (make-local-hook 'hs-hide-hook)
604 (add-hook 'hs-hide-hook 'lazy-lock-fontify-after-visage nil t))
2eb9703d
RS
605
606(defun lazy-lock-install-timers (dtime stime)
607 ;; Schedule or re-schedule the deferral and stealth timers.
608 ;; The layout of `lazy-lock-timers' is:
609 ;; ((DEFER-TIME . DEFER-TIMER) (STEALTH-TIME . STEALTH-TIMER)
610 ;; If an idle timeout has changed, cancel the existing idle timer (if there
611 ;; is one) and schedule a new one (if the new idle timeout is non-nil).
612 (unless (eq dtime (car (car lazy-lock-timers)))
613 (let ((defer (car lazy-lock-timers)))
614 (when (cdr defer)
615 (cancel-timer (cdr defer)))
616 (setcar lazy-lock-timers (cons dtime (and dtime
617 (run-with-idle-timer dtime t 'lazy-lock-fontify-after-defer))))))
618 (unless (eq stime (car (cdr lazy-lock-timers)))
619 (let ((stealth (cdr lazy-lock-timers)))
620 (when (cdr stealth)
621 (cancel-timer (cdr stealth)))
622 (setcdr lazy-lock-timers (cons stime (and stime
623 (run-with-idle-timer stime t 'lazy-lock-fontify-after-idle)))))))
624
625(defun lazy-lock-unstall ()
626 ;;
d7606d13
SM
627 ;; If Font Lock mode is still enabled, make sure that the buffer is
628 ;; fontified, and reinstall its hook. We must do this first.
629 (when font-lock-mode
630 (when (lazy-lock-unfontified-p)
631 (let ((verbose (if (numberp font-lock-verbose)
632 (> (buffer-size) font-lock-verbose)
633 font-lock-verbose)))
58523e00 634 (with-temp-message
20af777c
SM
635 (when verbose
636 (format "Fontifying %s..." (buffer-name)))
58523e00
SM
637 ;; Make sure we fontify etc. in the whole buffer.
638 (save-restriction
639 (widen)
640 (lazy-lock-fontify-region (point-min) (point-max))))))
d7606d13
SM
641 (add-hook 'after-change-functions 'font-lock-after-change-function nil t))
642 ;;
2eb9703d
RS
643 ;; Remove the text properties.
644 (lazy-lock-after-unfontify-buffer)
645 ;;
646 ;; Remove the fontification hooks.
647 (remove-hook 'window-scroll-functions 'lazy-lock-fontify-after-scroll t)
648 (remove-hook 'window-scroll-functions 'lazy-lock-defer-after-scroll t)
649 (remove-hook 'before-change-functions 'lazy-lock-arrange-before-change t)
d7606d13
SM
650 (remove-hook 'after-change-functions 'lazy-lock-fontify-line-after-change t)
651 (remove-hook 'after-change-functions 'lazy-lock-fontify-rest-after-change t)
652 (remove-hook 'after-change-functions 'lazy-lock-defer-line-after-change t)
653 (remove-hook 'after-change-functions 'lazy-lock-defer-rest-after-change t)
12837971
SM
654 (remove-hook 'outline-view-change-hook 'lazy-lock-fontify-after-visage t)
655 (remove-hook 'hs-hide-hook 'lazy-lock-fontify-after-visage t))
2eb9703d
RS
656\f
657;; Hook functions.
658
d7606d13
SM
659;; Lazy Lock mode intervenes when (1) a previously invisible buffer region
660;; becomes visible, i.e., for demand- or defer-driven on-the-scroll
661;; fontification, (2) a buffer modification occurs, i.e., for defer-driven
662;; on-the-fly fontification, (3) Emacs becomes idle, i.e., for fontification of
663;; deferred fontification and stealth fontification, and (4) other special
664;; occasions.
665
666;; 1. There are three ways whereby this can happen.
667;;
668;; (a) Scrolling the window, either explicitly (e.g., `scroll-up') or
669;; implicitly (e.g., `search-forward'). Here, `window-start' changes.
670;; Fontification occurs by adding `lazy-lock-fontify-after-scroll' (for
671;; demand-driven fontification) or `lazy-lock-defer-after-scroll' (for
672;; defer-driven fontification) to the hook `window-scroll-functions'.
673
2eb9703d
RS
674(defun lazy-lock-fontify-after-scroll (window window-start)
675 ;; Called from `window-scroll-functions'.
ce2cc837 676 ;; Fontify WINDOW from WINDOW-START following the scroll.
b14fbaa0 677 (let ((inhibit-point-motion-hooks t))
ce2cc837 678 (lazy-lock-fontify-region window-start (window-end window t)))
2eb9703d
RS
679 ;; A prior deletion that did not cause scrolling, followed by a scroll, would
680 ;; result in an unnecessary trigger after this if we did not cancel it now.
681 (set-window-redisplay-end-trigger window nil))
682
d7606d13
SM
683(defun lazy-lock-defer-after-scroll (window window-start)
684 ;; Called from `window-scroll-functions'.
685 ;; Defer fontification following the scroll. Save the current buffer so that
686 ;; we subsequently fontify in all windows showing the buffer.
687 (unless (memq (current-buffer) lazy-lock-buffers)
688 (push (current-buffer) lazy-lock-buffers))
689 ;; A prior deletion that did not cause scrolling, followed by a scroll, would
690 ;; result in an unnecessary trigger after this if we did not cancel it now.
691 (set-window-redisplay-end-trigger window nil))
692
693;; (b) Resizing the window, either explicitly (e.g., `enlarge-window') or
694;; implicitly (e.g., `delete-other-windows'). Here, `window-end' changes.
695;; Fontification occurs by adding `lazy-lock-fontify-after-resize' to the
696;; hook `window-size-change-functions'.
2eb9703d
RS
697
698(defun lazy-lock-fontify-after-resize (frame)
699 ;; Called from `window-size-change-functions'.
d7606d13
SM
700 ;; Fontify windows in FRAME following the resize. We cannot use
701 ;; `window-start' or `window-end' so we fontify conservatively.
2eb9703d
RS
702 (save-excursion
703 (save-selected-window
704 (select-frame frame)
705 (walk-windows (function (lambda (window)
706 (set-buffer (window-buffer window))
707 (when lazy-lock-mode
708 (lazy-lock-fontify-conservatively window))
709 (set-window-redisplay-end-trigger window nil)))
710 'nomini frame))))
711
d7606d13
SM
712;; (c) Deletion in the buffer. Here, a `window-end' marker can become visible.
713;; Fontification occurs by adding `lazy-lock-arrange-before-change' to
714;; `before-change-functions' and `lazy-lock-fontify-after-trigger' to the
715;; hook `redisplay-end-trigger-functions'. Before every deletion, the
716;; marker `window-redisplay-end-trigger' position is set to the soon-to-be
717;; changed `window-end' position. If the marker becomes visible,
718;; `lazy-lock-fontify-after-trigger' gets called. Ouch. Note that we only
719;; have to deal with this eventuality if there is no on-the-fly deferral.
720
2eb9703d
RS
721(defun lazy-lock-arrange-before-change (beg end)
722 ;; Called from `before-change-functions'.
723 ;; Arrange that if text becomes visible it will be fontified (if a deletion
724 ;; is pending, text might become visible at the bottom).
725 (unless (eq beg end)
726 (let ((windows (get-buffer-window-list (current-buffer) 'nomini t)) window)
727 (while windows
728 (setq window (car windows))
729 (unless (markerp (window-redisplay-end-trigger window))
730 (set-window-redisplay-end-trigger window (make-marker)))
731 (set-marker (window-redisplay-end-trigger window) (window-end window))
732 (setq windows (cdr windows))))))
733
d7606d13
SM
734(defun lazy-lock-fontify-after-trigger (window trigger-point)
735 ;; Called from `redisplay-end-trigger-functions'.
ce2cc837 736 ;; Fontify WINDOW from TRIGGER-POINT following the redisplay.
d7606d13 737 ;; We could probably just use `lazy-lock-fontify-after-scroll' without loss:
ce2cc837 738 ;; (inline (lazy-lock-fontify-after-scroll window (window-start window)))
b14fbaa0 739 (let ((inhibit-point-motion-hooks t))
ce2cc837 740 (lazy-lock-fontify-region trigger-point (window-end window t))))
2eb9703d 741
d7606d13
SM
742;; 2. Modified text must be marked as unfontified so it can be identified and
743;; fontified later when Emacs is idle. Deferral occurs by adding one of
744;; `lazy-lock-fontify-*-after-change' (for on-the-fly fontification) or
745;; `lazy-lock-defer-*-after-change' (for deferred fontification) to the
746;; hook `after-change-functions'.
747
748(defalias 'lazy-lock-fontify-line-after-change
2eb9703d 749 ;; Called from `after-change-functions'.
d7606d13
SM
750 ;; Fontify the current change.
751 'font-lock-after-change-function)
752
753(defun lazy-lock-fontify-rest-after-change (beg end old-len)
754 ;; Called from `after-change-functions'.
755 ;; Fontify the current change and defer fontification of the rest of the
756 ;; buffer. Save the current buffer so that we subsequently fontify in all
757 ;; windows showing the buffer.
758 (lazy-lock-fontify-line-after-change beg end old-len)
2eb9703d
RS
759 (save-buffer-state nil
760 (unless (memq (current-buffer) lazy-lock-buffers)
761 (push (current-buffer) lazy-lock-buffers))
12837971
SM
762 (save-restriction
763 (widen)
764 (remove-text-properties end (point-max) '(lazy-lock nil)))))
d7606d13
SM
765
766(defun lazy-lock-defer-line-after-change (beg end old-len)
767 ;; Called from `after-change-functions'.
768 ;; Defer fontification of the current change. Save the current buffer so
769 ;; that we subsequently fontify in all windows showing the buffer.
770 (save-buffer-state nil
771 (unless (memq (current-buffer) lazy-lock-buffers)
772 (push (current-buffer) lazy-lock-buffers))
773 (remove-text-properties (max (1- beg) (point-min))
774 (min (1+ end) (point-max))
775 '(lazy-lock nil))))
776
777(defun lazy-lock-defer-rest-after-change (beg end old-len)
778 ;; Called from `after-change-functions'.
779 ;; Defer fontification of the rest of the buffer. Save the current buffer so
780 ;; that we subsequently fontify in all windows showing the buffer.
781 (save-buffer-state nil
782 (unless (memq (current-buffer) lazy-lock-buffers)
783 (push (current-buffer) lazy-lock-buffers))
12837971
SM
784 (save-restriction
785 (widen)
786 (remove-text-properties (max (1- beg) (point-min))
787 (point-max)
788 '(lazy-lock nil)))))
d7606d13
SM
789
790;; 3. Deferred fontification and stealth fontification are done from these two
791;; functions. They are set up as Idle Timers.
2eb9703d
RS
792
793(defun lazy-lock-fontify-after-defer ()
794 ;; Called from `timer-idle-list'.
795 ;; Fontify all windows where deferral has occurred for its buffer.
7a31427e
SM
796 (save-excursion
797 (while (and lazy-lock-buffers (not (input-pending-p)))
798 (let ((buffer (car lazy-lock-buffers)) windows)
799 ;; Paranoia: check that the buffer is still live and Lazy Lock mode on.
800 (when (buffer-live-p buffer)
801 (set-buffer buffer)
802 (when lazy-lock-mode
803 (setq windows (get-buffer-window-list buffer 'nomini t))
804 (while windows
805 (lazy-lock-fontify-window (car windows))
806 (setq windows (cdr windows)))))
807 (setq lazy-lock-buffers (cdr lazy-lock-buffers)))))
6a02d88b 808 ;; Add hook if fontification should now be defer-driven in this buffer.
d7606d13 809 (when (and lazy-lock-mode lazy-lock-defer-on-scrolling
6a02d88b
SM
810 (memq 'lazy-lock-fontify-after-scroll window-scroll-functions)
811 (not (or (input-pending-p) (lazy-lock-unfontified-p))))
2eb9703d
RS
812 (remove-hook 'window-scroll-functions 'lazy-lock-fontify-after-scroll t)
813 (add-hook 'window-scroll-functions 'lazy-lock-defer-after-scroll nil t)))
814
815(defun lazy-lock-fontify-after-idle ()
816 ;; Called from `timer-idle-list'.
817 ;; Fontify all buffers that need it, stealthily while idle.
818 (unless (or executing-kbd-macro (window-minibuffer-p (selected-window)))
819 ;; Loop over all buffers, fontify stealthily for each if necessary.
86c87ebc
SM
820 (let ((buffers (buffer-list)) (continue t)
821 message message-log-max minibuffer-auto-raise)
2eb9703d
RS
822 (save-excursion
823 (do-while (and buffers continue)
824 (set-buffer (car buffers))
825 (if (not (and lazy-lock-mode (lazy-lock-unfontified-p)))
826 (setq continue (not (input-pending-p)))
827 ;; Fontify regions in this buffer while there is no input.
58523e00 828 (with-temp-message
20af777c
SM
829 (when lazy-lock-stealth-verbose
830 "Fontifying stealthily...")
58523e00
SM
831 (do-while (and (lazy-lock-unfontified-p) continue)
832 (if (and lazy-lock-stealth-load
833 (> (car (load-average)) lazy-lock-stealth-load))
834 ;; Wait a while before continuing with the loop.
835 (progn
836 (when message
837 (message "Fontifying stealthily...suspended")
838 (setq message nil))
839 (setq continue (sit-for (or lazy-lock-stealth-time 30))))
840 ;; Fontify a chunk.
841 (when lazy-lock-stealth-verbose
842 (if message
843 (message "Fontifying stealthily... %2d%% of %s"
844 (lazy-lock-percent-fontified) (buffer-name))
845 (message "Fontifying stealthily...")
846 (setq message t)))
847 (lazy-lock-fontify-chunk)
848 (setq continue (sit-for (or lazy-lock-stealth-nice 0)))))))
849 (setq buffers (cdr buffers)))))))
2eb9703d 850
d7606d13
SM
851;; 4. Special circumstances.
852
12837971
SM
853(defun lazy-lock-fontify-after-visage ()
854 ;; Called from `outline-view-change-hook' and `hs-hide-hook'.
2eb9703d 855 ;; Fontify windows showing the current buffer, as its visibility has changed.
12837971
SM
856 ;; This is a conspiracy hack between lazy-lock.el, outline.el and
857 ;; hideshow.el.
2eb9703d
RS
858 (let ((windows (get-buffer-window-list (current-buffer) 'nomini t)))
859 (while windows
860 (lazy-lock-fontify-conservatively (car windows))
861 (setq windows (cdr windows)))))
862
863(defun lazy-lock-after-fontify-buffer ()
864 ;; Called from `font-lock-after-fontify-buffer'.
865 ;; Mark the current buffer as fontified.
866 ;; This is a conspiracy hack between lazy-lock.el and font-lock.el.
867 (save-buffer-state nil
868 (add-text-properties (point-min) (point-max) '(lazy-lock t))))
869
870(defun lazy-lock-after-unfontify-buffer ()
871 ;; Called from `font-lock-after-unfontify-buffer'.
872 ;; Mark the current buffer as unfontified.
873 ;; This is a conspiracy hack between lazy-lock.el and font-lock.el.
874 (save-buffer-state nil
875 (remove-text-properties (point-min) (point-max) '(lazy-lock nil))))
876\f
877;; Fontification functions.
878
879;; If packages want to ensure that some region of the buffer is fontified, they
880;; should use this function. For an example, see ps-print.el.
881(defun lazy-lock-fontify-region (beg end)
882 ;; Fontify between BEG and END, where necessary, in the current buffer.
883 (when (setq beg (text-property-any beg end 'lazy-lock nil))
884 (save-excursion
885 (save-match-data
886 (save-buffer-state
887 ;; Ensure syntactic fontification is always correct.
888 (font-lock-beginning-of-syntax-function next)
889 ;; Find successive unfontified regions between BEG and END.
890 (condition-case data
891 (do-while beg
892 (setq next (or (text-property-any beg end 'lazy-lock t) end))
893 ;; Make sure the region end points are at beginning of line.
894 (goto-char beg)
895 (unless (bolp)
896 (beginning-of-line)
897 (setq beg (point)))
898 (goto-char next)
899 (unless (bolp)
900 (forward-line)
901 (setq next (point)))
902 ;; Fontify the region, then flag it as fontified.
903 (font-lock-fontify-region beg next)
904 (add-text-properties beg next '(lazy-lock t))
905 (setq beg (text-property-any next end 'lazy-lock nil)))
906 ((error quit) (message "Fontifying region...%s" data))))))))
907
908(defun lazy-lock-fontify-chunk ()
909 ;; Fontify the nearest chunk, for stealth, in the current buffer.
b14fbaa0
SM
910 (let ((inhibit-point-motion-hooks t))
911 (save-excursion
912 (save-restriction
913 (widen)
914 ;; Move to end of line in case the character at point is not fontified.
915 (end-of-line)
916 ;; Find where the previous (next) unfontified regions end (begin).
917 (let ((prev (previous-single-property-change (point) 'lazy-lock))
918 (next (text-property-any (point) (point-max) 'lazy-lock nil)))
919 ;; Fontify from the nearest unfontified position.
920 (if (or (null prev) (and next (< (- next (point)) (- (point) prev))))
921 ;; The next, or neither, region is the nearest not fontified.
922 (lazy-lock-fontify-region
923 (progn (goto-char (or next (point-min)))
924 (beginning-of-line)
925 (point))
926 (progn (goto-char (or next (point-min)))
927 (forward-line lazy-lock-stealth-lines)
928 (point)))
929 ;; The previous region is the nearest not fontified.
2eb9703d 930 (lazy-lock-fontify-region
b14fbaa0
SM
931 (progn (goto-char prev)
932 (forward-line (- lazy-lock-stealth-lines))
2eb9703d 933 (point))
b14fbaa0
SM
934 (progn (goto-char prev)
935 (forward-line)
936 (point)))))))))
2eb9703d
RS
937
938(defun lazy-lock-fontify-window (window)
939 ;; Fontify in WINDOW between `window-start' and `window-end'.
940 ;; We can only do this when we can use `window-start' and `window-end'.
d7606d13 941 (with-current-buffer (window-buffer window)
2eb9703d
RS
942 (lazy-lock-fontify-region (window-start window) (window-end window))))
943
944(defun lazy-lock-fontify-conservatively (window)
945 ;; Fontify in WINDOW conservatively around point.
946 ;; Where we cannot use `window-start' and `window-end' we do `window-height'
947 ;; lines around point. That way we guarantee to have done enough.
d7606d13 948 (with-current-buffer (window-buffer window)
b14fbaa0
SM
949 (let ((inhibit-point-motion-hooks t))
950 (lazy-lock-fontify-region
951 (save-excursion
952 (goto-char (window-point window))
953 (vertical-motion (- (window-height window)) window) (point))
954 (save-excursion
955 (goto-char (window-point window))
956 (vertical-motion (window-height window) window) (point))))))
2eb9703d
RS
957
958(defun lazy-lock-unfontified-p ()
959 ;; Return non-nil if there is anywhere still to be fontified.
960 (save-restriction
961 (widen)
962 (text-property-any (point-min) (point-max) 'lazy-lock nil)))
963
964(defun lazy-lock-percent-fontified ()
965 ;; Return the percentage (of characters) of the buffer that are fontified.
966 (save-restriction
967 (widen)
d7606d13 968 (let ((beg (point-min)) (size 0) next)
2eb9703d 969 ;; Find where the next fontified region begins.
d7606d13
SM
970 (while (setq beg (text-property-any beg (point-max) 'lazy-lock t))
971 (setq next (or (text-property-any beg (point-max) 'lazy-lock nil)
972 (point-max)))
973 (incf size (- next beg))
974 (setq beg next))
975 ;; Float because using integer multiplication will frequently overflow.
976 (truncate (* (/ (float size) (point-max)) 100)))))
2eb9703d
RS
977\f
978;; Version dependent workarounds and fixes.
979
d7606d13
SM
980(when (consp lazy-lock-defer-time)
981 ;;
982 ;; In 2.06.04 and below, `lazy-lock-defer-time' could specify modes and time.
983 (with-output-to-temp-buffer "*Help*"
984 (princ "The value of the variable `lazy-lock-defer-time' was\n ")
985 (princ lazy-lock-defer-time)
986 (princ "\n")
58523e00
SM
987 (princ "This variable cannot now be a list of modes and time,\n")
988 (princ "so instead use ")
989 (princ (substitute-command-keys "\\[customize-option]"))
990 (princ " to modify the variables, or put the forms:\n")
d7606d13
SM
991 (princ " (setq lazy-lock-defer-time ")
992 (princ (cdr lazy-lock-defer-time))
993 (princ ")\n")
994 (princ " (setq lazy-lock-defer-on-the-fly '")
995 (princ (car lazy-lock-defer-time))
996 (princ ")\n")
997 (princ "in your ~/.emacs. ")
998 (princ "The above forms have been evaluated for this editor session,\n")
58523e00
SM
999 (princ "but you should use ")
1000 (princ (substitute-command-keys "\\[customize-option]"))
1001 (princ " or change your ~/.emacs now."))
d7606d13
SM
1002 (setq lazy-lock-defer-on-the-fly (car lazy-lock-defer-time)
1003 lazy-lock-defer-time (cdr lazy-lock-defer-time)))
1004
1005(when (boundp 'lazy-lock-defer-driven)
1006 ;;
1007 ;; In 2.06.04 and below, `lazy-lock-defer-driven' was the variable name.
1008 (with-output-to-temp-buffer "*Help*"
1009 (princ "The value of the variable `lazy-lock-defer-driven' is set to ")
1010 (if (memq lazy-lock-defer-driven '(nil t))
1011 (princ lazy-lock-defer-driven)
1012 (princ "`")
1013 (princ lazy-lock-defer-driven)
1014 (princ "'"))
1015 (princ ".\n")
1016 (princ "This variable is now called `lazy-lock-defer-on-scrolling',\n")
58523e00
SM
1017 (princ "so instead use ")
1018 (princ (substitute-command-keys "\\[customize-option]"))
1019 (princ " to modify the variable, or put the form:\n")
d7606d13
SM
1020 (princ " (setq lazy-lock-defer-on-scrolling ")
1021 (unless (memq lazy-lock-defer-driven '(nil t))
1022 (princ "'"))
1023 (princ lazy-lock-defer-driven)
1024 (princ ")\n")
1025 (princ "in your ~/.emacs. ")
1026 (princ "The above form has been evaluated for this editor session,\n")
58523e00
SM
1027 (princ "but you should use ")
1028 (princ (substitute-command-keys "\\[customize-option]"))
1029 (princ " or change your ~/.emacs now."))
d7606d13 1030 (setq lazy-lock-defer-on-scrolling lazy-lock-defer-driven))
2eb9703d 1031\f
2eb9703d
RS
1032;; Install ourselves:
1033
2eb9703d
RS
1034(add-hook 'window-size-change-functions 'lazy-lock-fontify-after-resize)
1035(add-hook 'redisplay-end-trigger-functions 'lazy-lock-fontify-after-trigger)
1036
1037(unless (assq 'lazy-lock-mode minor-mode-alist)
1038 (setq minor-mode-alist (append minor-mode-alist '((lazy-lock-mode nil)))))
1039
1040;; Provide ourselves:
1041
1042(provide 'lazy-lock)
1043
1044;;; lazy-lock.el ends here