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