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