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