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