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