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