0
|
1 ;;; lazy-lock.el --- Lazy demand-driven fontification for fast Font Lock mode.
|
|
2
|
24
|
3 ;; Copyright (C) 1994, 1995, 1997 Free Software Foundation, Inc.
|
0
|
4
|
|
5 ;; Author: Simon Marshall <simon@gnu.ai.mit.edu>
|
|
6 ;; Keywords: faces files
|
24
|
7 ;; Version: 1.16
|
0
|
8
|
|
9 ;;; This file is part of GNU Emacs.
|
|
10
|
|
11 ;; GNU Emacs is free software; you can redistribute it and/or modify
|
|
12 ;; it under the terms of the GNU General Public License as published by
|
|
13 ;; the Free Software Foundation; either version 2, or (at your option)
|
|
14 ;; any later version.
|
|
15
|
|
16 ;; GNU Emacs is distributed in the hope that it will be useful,
|
|
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
19 ;; GNU General Public License for more details.
|
|
20
|
|
21 ;; You should have received a copy of the GNU General Public License
|
24
|
22 ;; along with GNU Emacs; see the file COPYING. If not, write to the
|
|
23 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
|
|
24 ;; Boston, MA 02111-1307, USA.
|
0
|
25
|
|
26 ;;; Commentary:
|
|
27
|
|
28 ;; Purpose:
|
|
29 ;;
|
|
30 ;; To make visiting buffers in `font-lock-mode' faster by making fontification
|
|
31 ;; be demand-driven and stealthy.
|
|
32 ;; Fontification only occurs when, and where, necessary.
|
|
33 ;;
|
|
34 ;; See caveats and feedback below. See also the defer-lock and fast-lock
|
|
35 ;; packages. (But don't use lazy-lock.el and fast-lock.el at the same time!)
|
|
36
|
|
37 ;; Installation:
|
|
38 ;;
|
|
39 ;; Put this file somewhere where Emacs can find it (i.e., in one of the paths
|
|
40 ;; in your `load-path'), `byte-compile-file' it, and put in your ~/.emacs:
|
|
41 ;;
|
|
42 ;; (autoload 'turn-on-lazy-lock "lazy-lock"
|
|
43 ;; "Unconditionally turn on Lazy Lock mode.")
|
|
44 ;;
|
|
45 ;; (add-hook 'font-lock-mode-hook 'turn-on-lazy-lock)
|
|
46 ;;
|
|
47 ;; Start up a new Emacs and use font-lock as usual (except that you can use the
|
|
48 ;; so-called "gaudier" fontification regexps on big files without frustration).
|
|
49 ;;
|
|
50 ;; In a buffer (which has `font-lock-mode' enabled) which is at least
|
|
51 ;; `lazy-lock-minimum-size' characters long, only the visible portion of the
|
|
52 ;; buffer will be fontified. Motion around the buffer will fontify those
|
|
53 ;; visible portions that were not previous fontified. If the variable
|
|
54 ;; `lazy-lock-hide-invisible' is non-nil, redisplay will be delayed until after
|
|
55 ;; fontification. Otherwise, text that has not yet been fontified is displayed
|
|
56 ;; in `lazy-lock-invisible-foreground'.
|
|
57 ;;
|
|
58 ;; If stealth fontification is enabled, fontification will occur in invisible
|
|
59 ;; parts of the buffer after `lazy-lock-stealth-time' seconds of idle time.
|
|
60
|
|
61 ;; Advanced Use:
|
|
62 ;;
|
|
63 ;; You can also do fancy things with `advice'. For example, to fontify when
|
|
64 ;; dragging the scroll-bar in Emacs, you could put in your ~/.emacs:
|
|
65 ;;
|
|
66 ;; (autoload 'lazy-lock-post-command-fontify-windows "lazy-lock")
|
|
67 ;;
|
|
68 ;; (defadvice scroll-bar-drag-1 (after fontify-window activate compile)
|
|
69 ;; (let ((lazy-lock-walk-windows nil) (lazy-lock-hide-invisible nil))
|
|
70 ;; (lazy-lock-post-command-fontify-windows)))
|
|
71 ;;
|
|
72 ;; Or to fontify when the Debugger pops up a source code window:
|
|
73 ;;
|
|
74 ;; (autoload 'lazy-lock-fontify-walk-windows "lazy-lock")
|
|
75 ;;
|
|
76 ;; (defadvice gud-display-line (after fontify-window activate compile)
|
|
77 ;; (let ((lazy-lock-walk-windows t) (lazy-lock-hide-invisible t))
|
|
78 ;; (lazy-lock-fontify-walk-windows)))
|
|
79 ;;
|
|
80 ;; Scott Byer <byer@mv.us.adobe.com> suggested this to fontify the visible part
|
|
81 ;; of an insertion only:
|
|
82 ;;
|
|
83 ;; (defvar lazy-lock-insert-commands
|
|
84 ;; '(yank yank-pop clipboard-yank hilit-yank hilit-yank-pop
|
|
85 ;; mail-yank-original mouse-yank-at-click mouse-yank-secondary
|
|
86 ;; yank-rectangle)
|
|
87 ;; "A list of insertion commands.")
|
|
88 ;;
|
|
89 ;; (defadvice font-lock-after-change-function (around fontify-insertion
|
|
90 ;; activate compile)
|
|
91 ;; (if (or (not (memq this-command lazy-lock-insert-commands))
|
|
92 ;; (and (pos-visible-in-window-p beg) (pos-visible-in-window-p end)))
|
|
93 ;; ad-do-it
|
|
94 ;; (let ((this-command 'ignore))
|
|
95 ;; (put-text-property beg end 'fontified nil)
|
|
96 ;; (lazy-lock-fontify-window))))
|
|
97 ;;
|
|
98 ;; Let me know if you use any other `advice' and I'll put it here. Thanks.
|
|
99 ;;
|
|
100 ;; These kinds of things with `advice' aren't done automatically because they
|
|
101 ;; cause large packages (advice.el plus bytecomp.el and friends) to be loaded.
|
|
102
|
24
|
103 ;; Implementation differences with version 2:
|
|
104 ;;
|
|
105 ;; - Version 1 of lazy-lock.el is a bit of a hack. Version 1 demand-driven
|
|
106 ;; fontification, the core feature of lazy-lock.el, is implemented by placing a
|
|
107 ;; function on `post-command-hook'. This function fontifies where necessary,
|
|
108 ;; i.e., where a window scroll has occurred. However, there are a number of
|
|
109 ;; problems with using `post-command-hook':
|
|
110 ;;
|
|
111 ;; (a) As the name suggests, `post-command-hook' is run after every command,
|
|
112 ;; i.e., frequently and regardless of whether scrolling has occurred.
|
|
113 ;; (b) Scrolling can occur during a command, when `post-command-hook' is not
|
|
114 ;; run, i.e., it is not necessarily run after scrolling has occurred.
|
|
115 ;; (c) When `post-command-hook' is run, there is nothing to suggest where
|
|
116 ;; scrolling might have occurred, i.e., which windows have scrolled.
|
|
117 ;;
|
|
118 ;; Thus lazy-lock.el's function is called almost as often as possible, usually
|
|
119 ;; when it need not be called, yet it is not always called when it is needed.
|
|
120 ;; Also, lazy-lock.el's function must check each window to see if a scroll has
|
|
121 ;; occurred there. Worse still, lazy-lock.el's function must fontify a region
|
|
122 ;; twice as large as necessary to make sure the window is completely fontified.
|
|
123 ;; Basically, `post-command-hook' is completely inappropriate for lazy-lock.el.
|
|
124 ;;
|
|
125 ;; Ideally, we want to attach lazy-lock.el's function to a hook that is run
|
|
126 ;; only when scrolling occurs, e.g., `window-start' has changed, and tells us
|
|
127 ;; as much information as we need, i.e., the window and its new buffer region.
|
|
128 ;; Richard Stallman implemented a `window-scroll-functions' for Emacs 19.30.
|
|
129 ;; Functions on it are run when `window-start' has changed, and are supplied
|
|
130 ;; with the window and the window's new `window-start' position. (It would be
|
|
131 ;; better if it also supplied the window's new `window-end' position, but that
|
|
132 ;; is calculated as part of the redisplay process, and the functions on
|
|
133 ;; `window-scroll-functions' are run before redisplay has finished.) Thus, the
|
|
134 ;; hook deals with the above problems (a), (b) and (c).
|
|
135 ;;
|
|
136 ;; If only life was that easy. Version 2 demand-driven fontification is mostly
|
|
137 ;; implemented by placing a function on `window-scroll-functions'. However,
|
|
138 ;; not all scrolling occurs when `window-start' has changed. A change in
|
|
139 ;; window size, e.g., via C-x 1, or a significant deletion, e.g., of a number
|
|
140 ;; of lines, causes text previously invisible (i.e., after `window-end') to
|
|
141 ;; become visible without changing `window-start'. Arguably, these events are
|
|
142 ;; not scrolling events, but fontification must occur for lazy-lock.el to work.
|
|
143 ;; Hooks `window-size-change-functions' and `redisplay-end-trigger-functions'
|
|
144 ;; were added for these circumstances.
|
|
145 ;;
|
|
146 ;; (Ben Wing thinks these hooks are "horribly horribly kludgy", and implemented
|
|
147 ;; a `pre-idle-hook', a `mother-of-all-post-command-hooks', for XEmacs 19.14.
|
|
148 ;; He then hacked up a version 1 lazy-lock.el to use `pre-idle-hook' rather
|
|
149 ;; than `post-command-hook'. Whereas functions on `post-command-hook' are
|
|
150 ;; called almost as often as possible, functions on `pre-idle-hook' really are
|
|
151 ;; called as often as possible, even when the mouse moves and, on some systems,
|
|
152 ;; while XEmacs is idle. Thus, the hook deals with the above problem (b), but
|
|
153 ;; unfortunately it makes (a) worse and does not address (c) at all.
|
|
154 ;;
|
|
155 ;; I freely admit that `redisplay-end-trigger-functions' and, to a much lesser
|
|
156 ;; extent, `window-size-change-functions' are not pretty. However, I feel that
|
|
157 ;; a `window-scroll-functions' feature is cleaner than a `pre-idle-hook', and
|
|
158 ;; the result is faster and smaller, less intrusive and more targeted, code.
|
|
159 ;; Since `pre-idle-hook' is pretty much like `post-command-hook', there is no
|
|
160 ;; point in making this version of lazy-lock.el work with it. Anyway, that's
|
|
161 ;; Lit 30 of my humble opinion.
|
|
162 ;;
|
|
163 ;; Steve Baur reverted to a non-hacked version 1 lazy-lock.el for XEmacs 19.15
|
|
164 ;; and 20.0. Obviously, the above `post-command-hook' problems still apply.)
|
|
165 ;;
|
|
166 ;; - Version 1 stealth fontification is also implemented by placing a function
|
|
167 ;; on `post-command-hook'. This function waits for a given amount of time,
|
|
168 ;; and, if Emacs remains idle, fontifies where necessary. Again, there are a
|
|
169 ;; number of problems with using `post-command-hook':
|
|
170 ;;
|
|
171 ;; (a) Functions on `post-command-hook' are run sequentially, so this function
|
|
172 ;; can interfere with other functions on the hook, and vice versa.
|
|
173 ;; (b) This function waits for a given amount of time, so it can interfere with
|
|
174 ;; various features that are dealt with by Emacs after a command, e.g.,
|
|
175 ;; region highlighting, asynchronous updating and keystroke echoing.
|
|
176 ;; (c) Fontification may be required during a command, when `post-command-hook'
|
|
177 ;; is not run. (Version 2 deferred fontification only.)
|
|
178 ;;
|
|
179 ;; Again, `post-command-hook' is completely inappropriate for lazy-lock.el.
|
|
180 ;; Richard Stallman and Morten Welinder implemented internal Timers and Idle
|
|
181 ;; Timers for Emacs 19.31. Functions can be run independently at given times
|
|
182 ;; or after given amounts of idle time. Thus, the feature deals with the above
|
|
183 ;; problems (a), (b) and (c). Version 2 deferral and stealth are implemented
|
|
184 ;; by functions on Idle Timers. (A function on XEmacs' `pre-idle-hook' is
|
|
185 ;; similar to an Emacs Idle Timer function with a fixed zero second timeout.)
|
|
186 ;;
|
|
187 ;; - Version 1 has the following problems (relative to version 2):
|
|
188 ;;
|
|
189 ;; (a) It is slow when it does its job.
|
|
190 ;; (b) It does not always do its job when it should.
|
|
191 ;; (c) It slows all interaction (when it doesn't need to do its job).
|
|
192 ;; (d) It interferes with other package functions on `post-command-hook'.
|
|
193 ;; (e) It interferes with Emacs things within the read-eval loop.
|
|
194 ;;
|
|
195 ;; Ben's hacked-up lazy-lock.el 1.14 almost solved (b) but made (c) worse.
|
|
196 ;;
|
|
197 ;; - Version 2 has the following additional features (relative to version 1):
|
|
198 ;;
|
|
199 ;; (a) It can defer fontification (both on-the-fly and on-scrolling).
|
|
200 ;; (b) It can fontify contextually (syntactically true on-the-fly).
|
|
201
|
0
|
202 ;; Caveats:
|
24
|
203
|
0
|
204 ;; Lazy Lock mode does not work efficiently with Outline mode. This is because
|
|
205 ;; when in Outline mode, although text may be hidden (not visible in the
|
|
206 ;; window), the text is visible to Emacs Lisp code (not surprisingly) and Lazy
|
|
207 ;; Lock fontifies it mercilessly. Hopefully this will be fixed one day.
|
|
208 ;;
|
|
209 ;; Lazy Lock mode does not fontify windows as they appear:
|
|
210 ;;
|
|
211 ;; 1. With `query-replace' or `ispell-*', as Lazy Lock only knows about point
|
|
212 ;; motion after the command exits.
|
|
213 ;;
|
|
214 ;; 2. When displayed by gud.el (the Grand Unified Debugger), as they are
|
|
215 ;; displayed via a process sentinel. See `Advanced Use' above.
|
|
216 ;;
|
|
217 ;; 3. In XEmacs 19.12, when the last command was invoked via a mouse event,
|
|
218 ;; because of a bug/feature in/of `sit-for'.
|
|
219 ;;
|
|
220 ;; 4. In other random situations that I don't know about (yet).
|
|
221 ;;
|
|
222 ;; If you have `lazy-lock-hide-invisible' you may notice that redisplay occurs
|
|
223 ;; before fontification regardlessly. This is due to other packages sitting on
|
|
224 ;; `post-command-hook' and provoking redisplay. If you use these packages, you
|
|
225 ;; can't use `lazy-lock-hide-invisible'.
|
|
226 ;;
|
|
227 ;; If you have `lazy-lock-hide-invisible' and use scrollbar scrolling using
|
|
228 ;; Emacs 19, hidden text will not be fontified as it becomes visible. It is
|
|
229 ;; expected that Emacs 19 will provide the necessary hooks in future, to solve
|
|
230 ;; this problem and the problem above.
|
|
231 ;;
|
|
232 ;; Unless otherwise stated, "Emacs 19.X" means versions up to and including X.
|
|
233 ;;
|
|
234 ;; In Emacs 19.25, one `window-start'/`window-end' bug means that if you open a
|
|
235 ;; file in another frame (such as via `find-tag-other-frame'), the whole buffer
|
|
236 ;; is fontified regardless. Upgrade!
|
|
237 ;;
|
|
238 ;; In Emacs 19.25, fontification by stealth is turned off because of a fatal
|
|
239 ;; bug in `previous-single-property-change'. Upgrade!
|
|
240 ;;
|
|
241 ;; In Emacs 19.28, if you see a message in the minibuffer of the form
|
|
242 ;; "Fontifying window... done. (Restarted in foo.c)"
|
|
243 ;; it means the Garbage Collector has marked some (subsequently used) text
|
|
244 ;; properties. Lazy Lock attempts to recover the situation by restarting in
|
|
245 ;; that buffer. Unfortunately, that buffer will be left in a writable and
|
|
246 ;; modified state. Also, other windows may not be fontified when this happens.
|
|
247 ;; To reduce the frequency of this bug occuring, increase in your ~/.emacs the
|
|
248 ;; value of `gc-cons-threshold' to, say, 1Meg, e.g.:
|
|
249 ;;
|
|
250 ;; (setq gc-cons-threshold (* 1024 1024))
|
|
251 ;;
|
|
252 ;; The solution is to upgrade! (With thanks to Kevin Broadey for help here.)
|
|
253 ;;
|
|
254 ;; For XEmacs 19.11 and Lucid Emacs 19.10 users, lazy-lock sort-of works.
|
|
255 ;; There are bugs in text property and point/window primatives. Upgrade!
|
|
256 ;;
|
24
|
257 ;; Currently XEmacs does not have the features to support version 2 of
|
|
258 ;; lazy-lock.el. Maybe it will one day.
|
0
|
259
|
|
260 ;; History:
|
|
261 ;;
|
|
262 ;; 0.01--1.00:
|
|
263 ;; - Changed name from fore-lock to lazy-lock. Shame though.
|
|
264 ;; - Dropped `advice'-wrapping completely. Ask me if you're interested in it.
|
24
|
265 ;; - Made `lazy-lock-mode' ignore `post-command-hook' and `buffer-file-name'
|
|
266 ;; - Made `lazy-lock-fontify-window' check `lazy-lock-mode' and `this-command'
|
|
267 ;; - Made `lazy-lock-fontify-window' redisplay via `sit-for'
|
|
268 ;; - Added `lazy-lock-minimum-size' to control `lazy-lock-mode'
|
0
|
269 ;; 1.00--1.01:
|
24
|
270 ;; - Added `lazy-lock-fontify-buffer'
|
|
271 ;; - Made `lazy-lock-fontify-window' ignore `lazy-lock-mode'
|
|
272 ;; - Made `lazy-lock-fontify-window' suspicious of `window-' favourites again
|
|
273 ;; - Added `lazy-lock-delay-commands' (idea from William G. Dubuque)
|
|
274 ;; - Added `lazy-lock-ignore-commands' for completeness
|
|
275 ;; - Added `lazy-lock-continuity-time' for normal input delay
|
0
|
276 ;; 1.01--1.02:
|
24
|
277 ;; - Made `lazy-lock-fontify-window' cope with multiple unfontified regions
|
|
278 ;; - Made `lazy-lock-mode' remove `fontified' properties if turned off
|
|
279 ;; - Made `lazy-lock-fontify-window' fontify by lines
|
|
280 ;; - Added `lazy-lock-cache-position' buffer local to detect visibility change
|
|
281 ;; - Added `lazy-lock-post-command-hook' to do the waiting
|
|
282 ;; - Made `lazy-lock-fontify-window' just do the fontification
|
|
283 ;; - Made `lazy-lock-mode' append `lazy-lock-post-command-hook'
|
|
284 ;; - Added `lazy-lock-walk-windows' to hack multi-window motion
|
|
285 ;; - Made `lazy-lock-post-command-hook' `walk-windows' if variable is non-nil
|
|
286 ;; - Removed `lazy-lock-ignore-commands' since insertion may change window
|
|
287 ;; - Added `lazy-lock-fontify-stealthily' and `lazy-lock-stealth-time'
|
|
288 ;; - Made `lazy-lock-post-command-hook' use them
|
0
|
289 ;; 1.02--1.03:
|
24
|
290 ;; - Made `lazy-lock-fontify-stealthily' do `forward-line' not `previous-line'
|
|
291 ;; - Made `lazy-lock-fontify-stealthily' `move-to-window-line' first
|
|
292 ;; - Made `lazy-lock-fontify-stealthily' use `text-property-any' for region
|
|
293 ;; - Made `lazy-lock-post-command-hook' loop on `lazy-lock-fontify-stealthily'
|
0
|
294 ;; 1.03--1.04:
|
24
|
295 ;; - Made `lazy-lock-mode' reset `lazy-lock-cache-position'
|
|
296 ;; - Made `lazy-lock-post-command-hook' `widen' for `if' `text-property-any'
|
|
297 ;; - Made `lazy-lock-fontify-stealthily' return `text-property-any'
|
|
298 ;; - Added `lazy-lock-percent-fontified' for a/be-musement
|
|
299 ;; - Made `lazy-lock-post-command-hook' use it
|
|
300 ;; - Made `lazy-lock-mode' use `make-local-hook' etc. if available
|
|
301 ;; - Made `lazy-lock-mode' use `before-revert-hook' and `after-revert-hook'
|
|
302 ;; - Made `lazy-lock-post-command-hook' protect `deactivate-mark'
|
|
303 ;; - Adds `lazy-lock-post-command-hook' globally to `post-command-hook'
|
0
|
304 ;; 1.04--1.05:
|
24
|
305 ;; - Made `lazy-lock-mode' test `make-local-hook' not `emacs-minor-version'
|
0
|
306 ;; 1.05--1.06:
|
24
|
307 ;; - Added `lazy-lock-ignore-commands' for commands that leave no event but do
|
|
308 ;; - Made `lazy-lock-post-command-hook' check `lazy-lock-ignore-commands'
|
0
|
309 ;; 1.06--1.07:
|
24
|
310 ;; - Removed `before-revert-hook' and `after-revert-hook' use
|
0
|
311 ;; 1.07--1.08:
|
24
|
312 ;; - Added `lazy-lock-submit-bug-report'
|
|
313 ;; - Made `lazy-lock-post-command-hook' check `executing-macro'
|
|
314 ;; - Made it sort-of/almost work for XEmacs (help from Jonas Jarnestrom)
|
|
315 ;; - XEmacs: Fix `text-property-not-all' (fix based on fast-lock.el 3.05 fix)
|
|
316 ;; - XEmacs: Set `font-lock-no-comments' and alias `frame-parameters'
|
|
317 ;; - Made `byte-compile-warnings' omit `unresolved' on compilation
|
|
318 ;; - Made `lazy-lock-post-command-hook' protect `buffer-undo-list'
|
|
319 ;; - Moved `deactivate-mark' and `buffer-undo-list' protection to functions
|
|
320 ;; - Added `lazy-lock-invisible-foreground' (idea from Boris Goldowsky)
|
|
321 ;; - XEmacs: Fix to use `text-property-not-all' t, not `text-property-any' nil
|
|
322 ;; - Made `lazy-lock-percent-fontified' return `round' to an integer
|
|
323 ;; - XEmacs: Fix `text-property-any' (fix and work around for a bug elsewhere)
|
|
324 ;; - XEmacs: Fix `lazy-lock-submit-bug-report' for reporter.el & vm-window.el
|
|
325 ;; - XEmacs: Made `lazy-lock-fontify-window' loop `while' `<' not `/='
|
|
326 ;; - Use `font-lock-after-change-function' to do the fontification
|
0
|
327 ;; 1.08--1.09:
|
24
|
328 ;; - Made `lazy-lock-post-command-hook' protect with `condition-case'
|
|
329 ;; - Made `lazy-lock-cache-start' to cache `window-start'
|
|
330 ;; - Made `lazy-lock-fontify-window' check and cache `lazy-lock-cache-start'
|
|
331 ;; - Renamed `lazy-lock-cache-position' to `lazy-lock-cache-end'
|
|
332 ;; - XEmacs: Fix for `font-lock-after-change-function'
|
|
333 ;; - Adds `lazy-lock-post-command-hook' globally to `window-setup-hook'
|
0
|
334 ;; 1.09--1.10:
|
24
|
335 ;; - Made `buffer-file-name' be `let' to prevent supersession (Kevin Broadey)
|
|
336 ;; - Made `lazy-lock-submit-bug-report' `require' reporter (Ilya Zakharevich)
|
|
337 ;; - Made `lazy-lock-mode' and `turn-on-lazy-lock' succeed `autoload' cookies
|
|
338 ;; - Added `lazy-lock-fontify-walk-windows' for walking window fontification
|
|
339 ;; - Added `lazy-lock-fontify-walk-stealthily' for walking stealth
|
|
340 ;; - Removed `move-to-window-line' from `lazy-lock-fontify-stealthily'
|
|
341 ;; - Made `lazy-lock-percent-fontified' use `truncate' rather than `round'
|
|
342 ;; - Added other `*-argument' to `lazy-lock-ignore-commands' (Kevin Broadey)
|
|
343 ;; - Made `lazy-lock-fontify-stealthily' not assume buffer is part `fontified'
|
|
344 ;; - Emacs: Fix for `font-lock-fontify-region'
|
|
345 ;; - Made `lazy-lock-post-command-hook' check for minibuffer (Kevin Broadey)
|
|
346 ;; - Added `lazy-lock-stealth-nice' for niceness during stealth fontification
|
|
347 ;; - Added `lazy-lock-stealth-lines' for chunks of stealth fontification
|
0
|
348 ;; 1.10--1.11: incorporated hack by Ben Wing from William Dubuque's fontifly.el
|
24
|
349 ;; - Made `lazy-lock-fontify-stealthily' see a non `fontified' preceding line
|
|
350 ;; - XEmacs: Fix `text-property-any' and `text-property-not-all' (Ben Wing)
|
|
351 ;; - XEmacs: Fix `lazy-lock-continuity-time' (Ben Wing)
|
|
352 ;; - Added awful `lazy-lock-running-xemacs-p' (Ben Wing)
|
|
353 ;; - Made loading set `emacs-minor-version' if it's not bound
|
|
354 ;; - Added `lazy-lock-hide-invisible' to control redisplay
|
|
355 ;; - Made `lazy-lock-post-command-hook' use it in `sit-for' (Ben Wing)
|
|
356 ;; - Made `lazy-lock-fontify-window' move relative to `end-of-line' if non-nil
|
|
357 ;; - Added `lazy-lock-fontify-region' so packages can ensure fontification
|
|
358 ;; - Made `lazy-lock-fontify-walk-stealthily' do stealth widening
|
|
359 ;; - Made `lazy-lock-fontify-stealthily' always do adjacent preceding regions
|
|
360 ;; - Added `lazy-lock-after-fontify-buffer'
|
|
361 ;; - XEmacs: Removed `font-lock-no-comments' incompatibility code
|
|
362 ;; - Removed `lazy-lock-delay-time' and `lazy-lock-delay-commands'
|
|
363 ;; - Removed `lazy-lock-post-command' and split the functionality
|
|
364 ;; - Adds `lazy-lock-post-command-fontify-windows' on first
|
|
365 ;; - Adds `lazy-lock-post-command-fontify-stealthily' on last
|
|
366 ;; - Made `lazy-lock-mode' ensure both first and last on `post-command-hook'
|
|
367 ;; - Made `lazy-lock-mode' ensure `font-lock-mode' is on
|
|
368 ;; - Wrap `lazy-lock-post-command-fontify-stealthily' for errors (David Karr)
|
|
369 ;; - Added `calcDigit-key' to `lazy-lock-ignore-commands' (Bob Glickstein)
|
|
370 ;; - Wrap `lazy-lock-running-xemacs-p' with `eval-and-compile' (Erik Naggum)
|
|
371 ;; - XEmacs: Fix use of `previous-single-property-change' (Jim Thompson)
|
|
372 ;; - XEmacs: Fix `next-single-property-change' fix for 19.11 (Jim Thompson)
|
|
373 ;; - Added `lazy-lock-post-resize-fontify-windows' to fontify on resizing
|
|
374 ;; - Adds globally to `window-size-change-functions'
|
|
375 ;; - Added `lazy-lock-post-setup-fontify-windows' to fontify after start up
|
|
376 ;; - Adds globally to `window-setup-hook'
|
|
377 ;; - Made `lazy-lock-post-command-fontify-windows' check for `input-pending-p'
|
|
378 ;; - Made `save-selected-window' to restore the `selected-window'
|
|
379 ;; - Use `save-selected-window' rather than `save-window-excursion'
|
0
|
380 ;; 1.11--1.12:
|
24
|
381 ;; - Made `lazy-lock-post-command-fontify-windows' do `set-buffer' first
|
|
382 ;; - Made `lazy-lock-fontify-stealthily' respect narrowing before point
|
|
383 ;; - Added `lazy-lock-post-setup-ediff-control-frame' for Ediff control frame
|
|
384 ;; - Adds globally to `ediff-after-setup-control-frame-hooks'
|
|
385 ;; - Wrap `save-selected-window' with `save-excursion' for `current-buffer'
|
0
|
386 ;; 1.12--1.13:
|
24
|
387 ;; - XEmacs: Add `lazy-lock-after-fontify-buffer' to the Font Lock hook
|
|
388 ;; - Made `buffer-file-truename' also wrapped for supersession (Rick Sladkey)
|
|
389 ;; - Made `font-lock-beginning-of-syntax-function' wrapped for fontification
|
|
390 ;; - Added `lazy-lock-stealth-verbose' (after harassment from Ben Wing)
|
|
391 ;; - XEmacs: Made `font-lock-verbose' wrapped for stealth fontification
|
0
|
392 ;; 1.13--1.14:
|
24
|
393 ;; - Wrap `lazy-lock-colour-invisible' for `set-face-foreground' (Jari Aalto)
|
12
|
394 ;; 1.14--1.15:
|
24
|
395 ;; - Made `lazy-lock-post-command-setup'; may add to `post-command-idle-hook'
|
|
396 ;; 1.15--1.16:
|
|
397 ;; - Test `emacs-major-version' as well as `emacs-minor-version'
|
|
398 ;; - Barf if Emacs 19.30 or up is running
|
|
399 ;; - Adds globally to `ediff-after-setup-control-frame-hook' too
|
|
400 ;; - Renamed `lazy-lock-running-xemacs-p' to `lazy-lock-running-xemacs'
|
|
401 ;; - Removed `lazy-lock-submit-bug-report' and bade farewell
|
0
|
402
|
24
|
403 ;;; Code:
|
|
404
|
0
|
405 (require 'font-lock)
|
|
406
|
24
|
407 ;; Make sure lazy-lock.el isn't depreciated.
|
|
408 (if (if (save-match-data (string-match "Lucid\\|XEmacs" (emacs-version)))
|
|
409 nil
|
|
410 (or (> emacs-major-version 19) (> emacs-minor-version 29)))
|
|
411 (error "`lazy-lock' version 2 should be used for Emacs 19.30 or later"))
|
0
|
412
|
24
|
413 (eval-when-compile
|
|
414 ;;
|
|
415 ;; We don't do this at the top-level as we only use non-autoloaded macros.
|
|
416 (require 'cl)
|
|
417 ;;
|
|
418 ;; Well, shouldn't Lazy Lock be as lazy as possible?
|
|
419 (setq byte-compile-dynamic t byte-compile-dynamic-docstrings t))
|
0
|
420
|
|
421 (eval-and-compile
|
24
|
422 ;; Yuck, but we make so much use of this variable it's probably worth it.
|
|
423 (defconst lazy-lock-running-xemacs
|
0
|
424 (not (null (save-match-data (string-match "Lucid" emacs-version))))))
|
|
425
|
|
426 (defvar lazy-lock-cache-start nil) ; for window fontifiction
|
|
427 (defvar lazy-lock-cache-end nil) ; for window fontifiction
|
|
428 (defvar lazy-lock-cache-continue nil) ; for stealth fontifiction
|
|
429 (defvar lazy-lock-mode nil) ; for modeline
|
|
430
|
|
431 ;; User Variables:
|
|
432
|
|
433 (defvar lazy-lock-minimum-size (* 25 1024)
|
|
434 "*If non-nil, the minimum size for buffers.
|
|
435 Only buffers more than this can have demand-driven fontification.
|
|
436 If nil, means size is irrelevant.")
|
|
437
|
|
438 (defvar lazy-lock-walk-windows t
|
|
439 "*If non-nil, fontify windows other than the selected window.
|
|
440 If `all-frames', fontify windows even on other frames.
|
|
441 A non-nil value slows down redisplay.")
|
|
442
|
|
443 ;; XEmacs 19.11 and below exercise a bug in the Xt event loop.
|
|
444 (defvar lazy-lock-continuity-time
|
24
|
445 (cond ((not lazy-lock-running-xemacs)
|
|
446 0)
|
|
447 ((and (= emacs-major-version 19) (< emacs-minor-version 12))
|
|
448 (if (featurep 'lisp-float-type) (/ (float 1) (float 1000)) 1))
|
|
449 (t
|
|
450 0))
|
0
|
451 "*Time in seconds to delay before normal window fontification.
|
|
452 Window fontification occurs if there is no input within this time.")
|
|
453
|
|
454 ;; `previous-single-property-change' at `point-min' up to Emacs 19.25 is fatal.
|
|
455 ;; `text-property-any', `text-property-not-all' and
|
|
456 ;; `next-single-property-change' up to XEmacs 19.11 are too broke.
|
|
457 (defvar lazy-lock-stealth-time
|
24
|
458 (when (or (> emacs-major-version 19)
|
|
459 (and (not lazy-lock-running-xemacs) (> emacs-minor-version 25))
|
|
460 (and lazy-lock-running-xemacs (> emacs-minor-version 11)))
|
|
461 30)
|
0
|
462 "*Time in seconds to delay before beginning stealth fontification.
|
|
463 Stealth fontification occurs if there is no input within this time.
|
|
464 If nil, means no fontification by stealth.")
|
|
465
|
|
466 (defvar lazy-lock-stealth-lines
|
24
|
467 (if lazy-lock-running-xemacs
|
|
468 (if font-lock-maximum-decoration 50 100)
|
|
469 (if font-lock-maximum-decoration 100 250))
|
0
|
470 "*If non-nil, the maximum size of a chunk of stealth fontification.
|
|
471 Each iteration of stealth fontification can fontify this number of lines.
|
|
472 To speed up input response during stealth fontification, at the cost of stealth
|
|
473 taking longer to fontify, you could reduce the value of this variable.
|
|
474 If nil, means use `window-height' for the maximum chunk size.")
|
|
475
|
24
|
476 (defvar lazy-lock-stealth-nice
|
|
477 (if lazy-lock-running-xemacs
|
|
478 (if (featurep 'lisp-float-type) (/ (float 1) (float 4)) 1)
|
|
479 (if (featurep 'lisp-float-type) (/ (float 1) (float 8)) 1))
|
0
|
480 "*Time in seconds to pause during chunks of stealth fontification.
|
|
481 To reduce machine load during stealth fontification, at the cost of stealth
|
|
482 taking longer to fontify, you could increase the value of this variable.")
|
|
483
|
|
484 (defvar lazy-lock-stealth-verbose font-lock-verbose
|
|
485 "*If non-nil, means stealth fontification should show status messages.")
|
|
486
|
|
487 (defvar lazy-lock-ignore-commands
|
|
488 (append
|
|
489 ;; Standard commands...
|
|
490 '(universal-argument digit-argument negative-argument
|
|
491 isearch-other-control-char isearch-other-meta-char)
|
|
492 ;; And some resulting from non-standard packages...
|
|
493 (if (fboundp 'calc) '(calcDigit-key)))
|
|
494 "A list of commands after which fontification should not occur.
|
|
495 To speed up typing response, at the cost of Lazy Lock not fontifying when
|
|
496 insertion causes scrolling, you could add `self-insert-command' to this list.")
|
|
497
|
24
|
498 (defvar lazy-lock-hide-invisible lazy-lock-running-xemacs
|
0
|
499 "*If non-nil, hide invisible text while it is fontified.
|
|
500 If non-nil, redisplay is delayed until after fontification occurs. If nil,
|
|
501 text is shown (in `lazy-lock-invisible-foreground') while it is fontified.
|
|
502 A non-nil value slows down redisplay and can slow down cursor motion.")
|
|
503
|
|
504 (defvar lazy-lock-invisible-foreground "gray50"
|
|
505 "The foreground colour to use to display invisible text.
|
|
506 If nil, the default foreground is used. If t, the default background is used.
|
|
507 If a string, it should be a colour to use (either its name or its RGB value).
|
|
508 Invisible text is momentarily seen (if `lazy-lock-hide-invisible' is nil) when
|
|
509 scrolling into unfontified regions.")
|
|
510
|
|
511 ;; User Functions:
|
|
512
|
|
513 ;;;###autoload
|
|
514 (defun lazy-lock-mode (&optional arg)
|
|
515 "Toggle Lazy Lock mode.
|
|
516 With arg, turn Lazy Lock mode on if and only if arg is positive and the buffer
|
|
517 is at least `lazy-lock-minimum-size' characters long.
|
|
518
|
|
519 When Lazy Lock mode is enabled, fontification is demand-driven and stealthy:
|
|
520
|
|
521 - Fontification occurs in visible parts of buffers when necessary.
|
|
522 Occurs if there is no input after pausing for `lazy-lock-continuity-time'.
|
|
523
|
|
524 - Fontification occurs in invisible parts when Emacs has been idle.
|
|
525 Occurs if there is no input after pausing for `lazy-lock-stealth-time'.
|
|
526
|
|
527 If `lazy-lock-hide-invisible' is non-nil, text is not displayed until it is
|
|
528 fontified, otherwise it is displayed in `lazy-lock-invisible-foreground'.
|
|
529
|
|
530 See also variables `lazy-lock-walk-windows' and `lazy-lock-ignore-commands' for
|
|
531 window (scroll) fontification, and `lazy-lock-stealth-lines',
|
|
532 `lazy-lock-stealth-nice' and `lazy-lock-stealth-verbose' for stealth
|
|
533 fontification.
|
|
534
|
|
535 Use \\[lazy-lock-submit-bug-report] to send bug reports or feedback."
|
|
536 (interactive "P")
|
|
537 (set (make-local-variable 'lazy-lock-mode)
|
|
538 (and (<= (or lazy-lock-minimum-size 0) (buffer-size))
|
|
539 (if arg (> (prefix-numeric-value arg) 0) (not lazy-lock-mode))))
|
|
540 (if (and lazy-lock-mode (not font-lock-mode))
|
|
541 ;; Turned on `lazy-lock-mode' rather than using `font-lock-mode-hook'.
|
|
542 (progn
|
|
543 (add-hook 'font-lock-mode-hook 'turn-on-lazy-lock)
|
|
544 (font-lock-mode 1))
|
|
545 ;; Let's get down to business.
|
12
|
546 (lazy-lock-post-command-setup)
|
0
|
547 (if (not lazy-lock-mode)
|
|
548 (let ((modified (buffer-modified-p)) (inhibit-read-only t)
|
|
549 (buffer-undo-list t)
|
|
550 deactivate-mark buffer-file-name buffer-file-truename)
|
|
551 (remove-text-properties (point-min) (point-max) '(fontified nil))
|
|
552 (or modified (set-buffer-modified-p nil)))
|
|
553 (if (and (not lazy-lock-hide-invisible) lazy-lock-invisible-foreground)
|
|
554 (lazy-lock-colour-invisible))
|
|
555 (set (make-local-variable 'lazy-lock-cache-start) 0)
|
|
556 (set (make-local-variable 'lazy-lock-cache-end) 0)
|
|
557 (set (make-local-variable 'font-lock-fontified) t))))
|
|
558
|
|
559 ;;;###autoload
|
|
560 (defun turn-on-lazy-lock ()
|
|
561 "Unconditionally turn on Lazy Lock mode."
|
|
562 (lazy-lock-mode 1))
|
|
563
|
24
|
564 (when (and (= emacs-major-version 19)
|
|
565 (< emacs-minor-version (if lazy-lock-running-xemacs 12 29)))
|
|
566 ;; We don't need this in Emacs 19.29 or XEmacs 19.12.
|
|
567 (defun lazy-lock-fontify-buffer ()
|
|
568 "Fontify the current buffer where necessary."
|
|
569 (interactive)
|
|
570 (lazy-lock-fontify-region (point-min) (point-max))))
|
0
|
571
|
|
572 ;; API Functions:
|
|
573
|
|
574 (defun lazy-lock-fontify-region (start end &optional buffer)
|
|
575 "Fontify between START and END in BUFFER where necessary."
|
|
576 (save-excursion
|
|
577 (and buffer (set-buffer buffer))
|
|
578 (save-restriction
|
|
579 (narrow-to-region start end)
|
|
580 (let ((lazy-lock-stealth-lines (count-lines start end)))
|
|
581 (while (text-property-not-all start end 'fontified t)
|
|
582 (lazy-lock-fontify-stealthily))))))
|
|
583
|
|
584 (defun lazy-lock-after-fontify-buffer ()
|
|
585 ;; Mark the buffer as `fontified'.
|
|
586 (let ((modified (buffer-modified-p)) (inhibit-read-only t)
|
|
587 (buffer-undo-list t)
|
|
588 deactivate-mark buffer-file-name buffer-file-truename)
|
12
|
589 (put-text-property (point-min) (point-max) 'fontified t)
|
0
|
590 (or modified (set-buffer-modified-p nil))))
|
|
591
|
|
592 ;; Just a cleaner-looking way of coping with Emacs' and XEmacs' `sit-for'.
|
|
593 (defmacro lazy-lock-sit-for (seconds &optional nodisp)
|
24
|
594 (if lazy-lock-running-xemacs
|
0
|
595 (` (sit-for (, seconds) (, nodisp)))
|
|
596 (` (sit-for (, seconds) 0 (, nodisp)))))
|
|
597
|
|
598 ;; Using `save-window-excursion' provokes `window-size-change-functions'.
|
|
599 ;; I prefer `save-walking-excursion', of course, because I have a warped mind.
|
24
|
600 (unless (fboundp 'save-selected-window)
|
0
|
601 (eval-and-compile
|
|
602 (defmacro save-selected-window (&rest body)
|
|
603 "Execute the BODY forms, restoring the selected window.
|
|
604 Does not restore the value of point in the selected window, or anything else."
|
|
605 (` (let ((original-window (selected-window)))
|
|
606 (unwind-protect
|
|
607 (progn (,@ body))
|
|
608 (select-window original-window))))))
|
|
609 (put 'save-selected-window 'lisp-indent-function 0))
|
|
610
|
|
611 ;; Functions for hooks:
|
|
612
|
12
|
613 (defun lazy-lock-post-command-fontify-windows ()
|
|
614 ;; We might not be where we think we are, since `post-command-hook' is run
|
|
615 ;; before `command_loop_1' makes sure we have the correct buffer selected.
|
24
|
616 ; (set-buffer (window-buffer))
|
12
|
617 ;; Do groovy things if (a) not in a macro, (b) no input pending, (c) got a
|
|
618 ;; real command, (d) not in the minibuffer, and (e) no input after waiting
|
|
619 ;; for `lazy-lock-continuity-time'.
|
|
620 (if (or executing-kbd-macro
|
|
621 (input-pending-p)
|
|
622 (memq this-command lazy-lock-ignore-commands)
|
|
623 (window-minibuffer-p (selected-window)))
|
0
|
624 (setq lazy-lock-cache-continue nil)
|
|
625 (setq lazy-lock-cache-continue t)
|
12
|
626 (if (lazy-lock-sit-for lazy-lock-continuity-time lazy-lock-hide-invisible)
|
|
627 ;; Do the visible parts of the buffer(s), i.e., the window(s).
|
|
628 (if (or (not lazy-lock-walk-windows)
|
|
629 (and (eq lazy-lock-walk-windows t) (one-window-p t)))
|
|
630 (if lazy-lock-mode (condition-case nil (lazy-lock-fontify-window)))
|
|
631 (lazy-lock-fontify-walk-windows)))))
|
0
|
632
|
|
633 (defun lazy-lock-post-command-fontify-stealthily ()
|
|
634 ;; Do groovy things if (a-d) above, (e) not moving the mouse, and (f) no
|
12
|
635 ;; input after after waiting for `lazy-lock-stealth-time'.
|
0
|
636 (if (and lazy-lock-cache-continue lazy-lock-stealth-time)
|
|
637 (condition-case data
|
|
638 (if (lazy-lock-sit-for lazy-lock-stealth-time)
|
|
639 ;; Do the invisible parts of buffers.
|
|
640 (lazy-lock-fontify-walk-stealthily))
|
|
641 (error (message "Fontifying stealthily... %s" data)))))
|
|
642
|
|
643 (defun lazy-lock-post-resize-fontify-windows (frame)
|
|
644 ;; Fontify all windows in FRAME.
|
|
645 (let ((lazy-lock-walk-windows t) executing-kbd-macro this-command)
|
|
646 (save-excursion
|
|
647 (save-selected-window
|
|
648 (select-frame frame)
|
12
|
649 (lazy-lock-post-command-fontify-windows)))))
|
0
|
650
|
|
651 (defun lazy-lock-post-setup-emacs-fontify-windows ()
|
|
652 ;; Fontify all windows in all frames.
|
|
653 (let ((lazy-lock-walk-windows 'all-frames) executing-kbd-macro this-command)
|
12
|
654 (lazy-lock-post-command-fontify-windows)))
|
0
|
655
|
|
656 (defun lazy-lock-post-setup-ediff-control-frame ()
|
|
657 ;; Fontify all windows in all frames when using the Ediff control frame.
|
|
658 (make-local-variable 'lazy-lock-walk-windows)
|
|
659 (setq lazy-lock-walk-windows (if (ediff-multiframe-setup-p) 'all-frames t))
|
12
|
660 (lazy-lock-post-command-setup))
|
|
661
|
|
662 (defun lazy-lock-post-command-setup ()
|
|
663 ;; Make sure that we're in the correct positions to avoid hassle.
|
|
664 (remove-hook 'post-command-hook 'lazy-lock-post-command-fontify-windows)
|
|
665 (remove-hook 'post-command-hook 'lazy-lock-post-command-fontify-stealthily)
|
|
666 (add-hook 'post-command-hook 'lazy-lock-post-command-fontify-windows)
|
|
667 (add-hook (if (boundp 'post-command-idle-hook)
|
|
668 'post-command-idle-hook
|
|
669 'post-command-hook)
|
|
670 'lazy-lock-post-command-fontify-stealthily t))
|
0
|
671
|
|
672 ;; Functions for fontification:
|
|
673
|
|
674 (defun lazy-lock-fontify-window ()
|
|
675 ;; Fontify the visible part of the buffer where necessary.
|
24
|
676 (let (ws we wh)
|
|
677 ;; Find the bounds of the visible part exactly or conservatively.
|
|
678 (if (not lazy-lock-hide-invisible)
|
|
679 (setq ws (min (max (window-start) (point-min)) (point-max))
|
|
680 we (min (max (1- (window-end)) (point-min)) (point-max)))
|
|
681 (setq wh (window-height) ; Buggy: (window-displayed-height)
|
|
682 ws (save-excursion (forward-line (- wh)) (point))
|
|
683 we (save-excursion (forward-line wh) (point))))
|
|
684 ;; Find whether bounds have changed since previous fontification.
|
0
|
685 (if (or (/= ws lazy-lock-cache-start) (/= we lazy-lock-cache-end))
|
|
686 ;; Find where we haven't `fontified' before.
|
|
687 (let* ((start (or (text-property-not-all ws we 'fontified t) ws))
|
|
688 (end (or (text-property-any start we 'fontified t) we))
|
|
689 (modified (buffer-modified-p)) (inhibit-read-only t)
|
|
690 ;; We do the following to prevent: undo list addition; region
|
|
691 ;; highlight disappearance; supersession/locking checks.
|
|
692 (buffer-undo-list t)
|
|
693 deactivate-mark buffer-file-name buffer-file-truename
|
|
694 ;; Ensure Emacs 19.30 syntactic fontification is always correct.
|
|
695 font-lock-beginning-of-syntax-function
|
|
696 ;; Prevent XEmacs 19.13 during fontification from messages.
|
|
697 font-lock-verbose)
|
|
698 (while (< start end)
|
|
699 ;; Fontify and flag the region as `fontified'.
|
12
|
700 (font-lock-after-change-function start end 0)
|
|
701 (put-text-property start end 'fontified t)
|
0
|
702 ;; Find the next region.
|
|
703 (setq start (or (text-property-not-all ws we 'fontified t) ws)
|
|
704 end (or (text-property-any start we 'fontified t) we)))
|
|
705 (setq lazy-lock-cache-start ws lazy-lock-cache-end we)
|
|
706 (or modified (set-buffer-modified-p nil))))))
|
|
707
|
|
708 (defun lazy-lock-fontify-walk-windows ()
|
|
709 ;; Fontify windows in all required by walking through them.
|
|
710 (save-excursion
|
|
711 (save-selected-window
|
|
712 (condition-case nil
|
|
713 (walk-windows
|
|
714 (function (lambda (window)
|
|
715 (select-window window)
|
|
716 (if lazy-lock-mode (lazy-lock-fontify-window))))
|
|
717 'no-minibuf (eq lazy-lock-walk-windows 'all-frames))
|
|
718 (wrong-type-argument
|
|
719 ;; Looks like the Emacs 19.28 Garbage Collection bug has hit town.
|
|
720 ;; Completely remove all text properties and restart.
|
|
721 (set-text-properties (point-min) (point-max) nil)
|
|
722 (turn-on-lazy-lock)
|
|
723 (lazy-lock-fontify-window)
|
|
724 (message "Fontifying window... done. (Restarted in %s)"
|
|
725 (buffer-name)))))))
|
|
726
|
|
727 (defun lazy-lock-fontify-stealthily ()
|
|
728 ;; Fontify an invisible part of the buffer where necessary.
|
|
729 (save-excursion
|
|
730 ;; Move to the end in case the character to the left is not `fontified'.
|
|
731 (end-of-line)
|
|
732 ;; Find where the next and previous regions not `fontified' begin and end.
|
|
733 (let ((next (text-property-not-all (point) (point-max) 'fontified t))
|
|
734 (prev (let ((p (previous-single-property-change (point) 'fontified)))
|
|
735 (and p (> p (point-min)) p)))
|
|
736 (modified (buffer-modified-p)) (inhibit-read-only t) start end
|
|
737 ;; We do the following to prevent: undo list addition; region
|
|
738 ;; highlight disappearance; supersession/locking checks.
|
|
739 (buffer-undo-list t)
|
|
740 deactivate-mark buffer-file-name buffer-file-truename
|
|
741 ;; Ensure Emacs 19.30 syntactic fontification is always correct.
|
|
742 font-lock-beginning-of-syntax-function
|
|
743 ;; Prevent XEmacs 19.13 during fontification from spewing messages.
|
|
744 font-lock-verbose)
|
|
745 (cond ((and (null next) (null prev))
|
|
746 ;; Nothing has been `fontified' yet.
|
|
747 (beginning-of-line 1) (setq start (point))
|
|
748 (forward-line (or lazy-lock-stealth-lines (window-height)))
|
|
749 (setq end (point)))
|
|
750 ((or (null prev)
|
|
751 (and next (> (- (point) prev) (- next (point)))))
|
|
752 ;; The next region is the nearest not `fontified'.
|
|
753 (goto-char next) (beginning-of-line 1) (setq start (point))
|
|
754 (forward-line (or lazy-lock-stealth-lines (window-height)))
|
|
755 ;; Maybe the region is already partially `fontified'.
|
|
756 (setq end (or (text-property-any next (point) 'fontified t)
|
|
757 (point))))
|
|
758 (t
|
|
759 ;; The previous region is the nearest not `fontified'.
|
|
760 (goto-char prev) (forward-line 1) (setq end (point))
|
|
761 (forward-line (- (or lazy-lock-stealth-lines (window-height))))
|
|
762 ;; Maybe the region is already partially `fontified'.
|
|
763 (setq start
|
|
764 (or (previous-single-property-change prev 'fontified nil (point))
|
|
765 (point)))))
|
|
766 ;; Fontify and flag the region as `fontified'.
|
12
|
767 (font-lock-after-change-function start end 0)
|
|
768 (put-text-property start end 'fontified t)
|
0
|
769 (or modified (set-buffer-modified-p nil)))))
|
|
770
|
|
771 (defun lazy-lock-fontify-walk-stealthily ()
|
|
772 ;; Fontify regions in all required buffers while there is no input.
|
|
773 (let ((buffers (buffer-list)) (continue t) fontified message-log-max)
|
|
774 (save-excursion
|
|
775 (while (and buffers continue)
|
|
776 (set-buffer (car buffers))
|
|
777 (if (and lazy-lock-mode (lazy-lock-unfontified-p))
|
|
778 ;; Fontify regions in this buffer while there is no input.
|
|
779 (let ((bufname (buffer-name)))
|
|
780 (if (and lazy-lock-stealth-verbose (not fontified))
|
|
781 (message "Fontifying stealthily..."))
|
|
782 ;; We `save-restriction' and `widen' around everything as
|
|
783 ;; `lazy-lock-fontify-stealthily' doesn't and we `sit-for'.
|
|
784 (save-restriction (widen) (lazy-lock-fontify-stealthily))
|
|
785 (while (and (lazy-lock-unfontified-p)
|
|
786 (setq continue (lazy-lock-sit-for
|
|
787 lazy-lock-stealth-nice)))
|
|
788 (if lazy-lock-stealth-verbose
|
|
789 (message "Fontifying stealthily... %2d%% of %s"
|
|
790 (lazy-lock-percent-fontified) bufname))
|
|
791 (save-restriction (widen) (lazy-lock-fontify-stealthily)))
|
|
792 ;; Note that fontification occurred.
|
|
793 (setq fontified t)))
|
|
794 (setq buffers (cdr buffers))))
|
|
795 (if (and lazy-lock-stealth-verbose fontified)
|
|
796 (message "Fontifying stealthily... %s." (if continue "done" "quit")))))
|
|
797
|
|
798 (defun lazy-lock-unfontified-p ()
|
|
799 ;; Return non-nil if there is anywhere still to be `fontified'.
|
|
800 (save-restriction
|
|
801 (widen)
|
|
802 (text-property-not-all (point-min) (point-max) 'fontified t)))
|
|
803
|
|
804 (defun lazy-lock-percent-fontified ()
|
|
805 ;; Return the percentage (of characters) of the buffer that are `fontified'.
|
|
806 (save-restriction
|
|
807 (widen)
|
|
808 (let ((size 0) (start (point-min)) (max (point-max)) end)
|
|
809 (while (setq start (text-property-any start max 'fontified t))
|
|
810 (setq end (or (text-property-not-all start max 'fontified t) max)
|
|
811 size (+ size (- end start))
|
|
812 start end))
|
24
|
813 ;; Float because using integer multiplication will frequently overflow.
|
|
814 (truncate (* (/ (float size) (point-max)) 100)))))
|
0
|
815
|
|
816 (defun lazy-lock-colour-invisible ()
|
|
817 ;; Fontify the current buffer in `lazy-lock-invisible-face'.
|
|
818 (save-restriction
|
|
819 (widen)
|
|
820 (let ((face 'lazy-lock-invisible-face)
|
|
821 (fore (if (stringp lazy-lock-invisible-foreground)
|
|
822 lazy-lock-invisible-foreground
|
|
823 (cdr (assq 'background-color (frame-parameters)))))
|
|
824 (modified (buffer-modified-p)) (inhibit-read-only t)
|
|
825 (buffer-undo-list t)
|
|
826 deactivate-mark buffer-file-name buffer-file-truename)
|
|
827 (make-face face)
|
|
828 (if (not (equal (face-foreground face) fore))
|
|
829 (condition-case nil
|
|
830 (set-face-foreground face fore)
|
|
831 (error (message "Unable to use foreground \"%s\"" fore))))
|
12
|
832 (put-text-property (point-min) (point-max) 'face face)
|
|
833 (put-text-property (point-min) (point-max) 'fontified nil)
|
0
|
834 (or modified (set-buffer-modified-p nil)))))
|
|
835
|
|
836 ;; Functions for Emacs:
|
|
837
|
|
838 ;; This fix is for a number of bugs in the function in Emacs 19.28.
|
24
|
839 (when (and (not lazy-lock-running-xemacs)
|
|
840 (= emacs-major-version 19) (< emacs-minor-version 28))
|
|
841 (defun font-lock-fontify-region (start end &optional loudly)
|
|
842 "Put proper face on each string and comment between START and END."
|
|
843 (save-excursion
|
|
844 (save-restriction
|
|
845 (widen)
|
|
846 (goto-char start)
|
|
847 (beginning-of-line)
|
|
848 (if loudly (message "Fontifying %s... (syntactically...)" (buffer-name)))
|
|
849 (let ((inhibit-read-only t) (buffer-undo-list t)
|
|
850 buffer-file-name buffer-file-truename
|
|
851 (modified (buffer-modified-p))
|
|
852 (old-syntax (syntax-table))
|
|
853 (synstart (if comment-start-skip
|
|
854 (concat "\\s\"\\|" comment-start-skip)
|
|
855 "\\s\""))
|
|
856 (comstart (if comment-start-skip
|
|
857 (concat "\\s<\\|" comment-start-skip)
|
|
858 "\\s<"))
|
|
859 (startline (point))
|
|
860 state prev prevstate)
|
|
861 (unwind-protect
|
|
862 (progn
|
|
863 (if font-lock-syntax-table
|
|
864 (set-syntax-table font-lock-syntax-table))
|
|
865 ;; Find the state at the line-beginning before START.
|
|
866 (if (eq startline font-lock-cache-position)
|
|
867 (setq state font-lock-cache-state)
|
|
868 ;; Find outermost containing sexp.
|
|
869 (beginning-of-defun)
|
|
870 ;; Find the state at STARTLINE.
|
|
871 (while (< (point) startline)
|
|
872 (setq state (parse-partial-sexp (point) startline 0)))
|
|
873 (setq font-lock-cache-state state
|
|
874 font-lock-cache-position (point)))
|
|
875 ;; Now find the state precisely at START.
|
|
876 (setq state (parse-partial-sexp (point) start nil nil state))
|
|
877 ;; If the region starts inside a string, show the extent of it.
|
|
878 (if (nth 3 state)
|
|
879 (let ((beg (point)))
|
|
880 (while (and (re-search-forward "\\s\"" end 'move)
|
|
881 (nth 3 (parse-partial-sexp beg (point) nil nil
|
|
882 state))))
|
|
883 (put-text-property beg (point) 'face font-lock-string-face)
|
|
884 (setq state (parse-partial-sexp beg (point)
|
|
885 nil nil state))))
|
|
886 ;; Likewise for a comment.
|
|
887 (if (or (nth 4 state) (nth 7 state))
|
|
888 (let ((beg (point)))
|
|
889 (save-restriction
|
|
890 (narrow-to-region (point-min) end)
|
|
891 (condition-case nil
|
|
892 (progn
|
|
893 (re-search-backward comstart (point-min) 'move)
|
|
894 (forward-comment 1)
|
|
895 ;; forward-comment skips all whitespace,
|
|
896 ;; so go back to the real end of the comment.
|
|
897 (skip-chars-backward " \t"))
|
|
898 (error (goto-char end))))
|
|
899 (put-text-property beg (point) 'face
|
|
900 font-lock-comment-face)
|
|
901 (setq state (parse-partial-sexp beg (point)
|
|
902 nil nil state))))
|
|
903 ;; Find each interesting place between here and END.
|
|
904 (while (and (< (point) end)
|
|
905 (setq prev (point) prevstate state)
|
|
906 (re-search-forward synstart end t)
|
|
907 (progn
|
|
908 ;; Clear out the fonts of what we skip over.
|
|
909 (remove-text-properties prev (point) '(face nil))
|
|
910 ;; Verify the state at that place
|
|
911 ;; so we don't get fooled by \" or \;.
|
|
912 (setq state (parse-partial-sexp prev (point)
|
|
913 nil nil state))))
|
|
914 (let ((here (point)))
|
|
915 (if (or (nth 4 state) (nth 7 state))
|
|
916 ;; We found a real comment start.
|
|
917 (let ((beg (match-beginning 0)))
|
|
918 (goto-char beg)
|
|
919 (save-restriction
|
|
920 (narrow-to-region (point-min) end)
|
|
921 (condition-case nil
|
|
922 (progn
|
|
923 (forward-comment 1)
|
|
924 ;; forward-comment skips all whitespace,
|
|
925 ;; so go back to the real end of the comment.
|
|
926 (skip-chars-backward " \t"))
|
|
927 (error (goto-char end))))
|
|
928 (put-text-property beg (point) 'face
|
|
929 font-lock-comment-face)
|
|
930 (setq state (parse-partial-sexp here (point)
|
|
931 nil nil state)))
|
|
932 (if (nth 3 state)
|
0
|
933 (let ((beg (match-beginning 0)))
|
24
|
934 (while (and (re-search-forward "\\s\"" end 'move)
|
|
935 (nth 3 (parse-partial-sexp
|
|
936 here (point) nil nil state))))
|
12
|
937 (put-text-property beg (point) 'face
|
24
|
938 font-lock-string-face)
|
0
|
939 (setq state (parse-partial-sexp here (point)
|
24
|
940 nil nil state))))))
|
|
941 ;; Make sure PREV is non-nil after the loop
|
|
942 ;; only if it was set on the very last iteration.
|
|
943 (setq prev nil)))
|
|
944 (set-syntax-table old-syntax)
|
|
945 (and prev
|
|
946 (remove-text-properties prev end '(face nil)))
|
|
947 (and (buffer-modified-p)
|
|
948 (not modified)
|
|
949 (set-buffer-modified-p nil))))))))
|
0
|
950
|
|
951 ;; Functions for XEmacs:
|
|
952
|
|
953 ;; These fix bugs in `text-property-any' and `text-property-not-all'. They may
|
|
954 ;; not work perfectly in 19.11 and below because `next-single-property-change'
|
|
955 ;; is also broke and not easily fixable in Lisp.
|
24
|
956 (when (and lazy-lock-running-xemacs
|
|
957 (= emacs-major-version 19) (< emacs-minor-version 12))
|
|
958 ;; Loop through property changes until found. This fix includes a work
|
|
959 ;; around which prevents a bug in `window-start' causing a barf here.
|
|
960 (defun text-property-any (start end prop value &optional buffer)
|
|
961 "Check text from START to END to see if PROP is ever `eq' to VALUE.
|
0
|
962 If so, return the position of the first character whose PROP is `eq'
|
|
963 to VALUE. Otherwise return nil."
|
24
|
964 (let ((start (min start end)) (end (max start end)))
|
|
965 (while (and start (not (eq (get-text-property start prop buffer) value)))
|
|
966 (setq start (next-single-property-change start prop buffer end)))
|
|
967 start))
|
|
968 ;; No need to loop here; if it's not at START it's at the next change.
|
|
969 ;; However, `next-single-property-change' sometimes returns LIMIT, or
|
|
970 ;; `point-max', if no change is found and sometimes returns nil.
|
|
971 (defun text-property-not-all (start end prop value &optional buffer)
|
|
972 "Check text from START to END to see if PROP is ever not `eq' to VALUE.
|
0
|
973 If so, return the position of the first character whose PROP is not
|
|
974 `eq' to VALUE. Otherwise, return nil."
|
24
|
975 (if (not (eq value (get-text-property start prop buffer)))
|
|
976 start
|
|
977 (let ((next (next-single-property-change start prop buffer end))
|
|
978 (end (or end (save-excursion (and buffer (set-buffer buffer))
|
|
979 (point-max)))))
|
|
980 (and next (< next end) next)))))
|
0
|
981
|
|
982 ;; XEmacs 19.11 function `font-lock-any-extents-p' looks for `text-prop' rather
|
|
983 ;; than `face'. Since `font-lock-unfontify-region' only removes `face', and we
|
|
984 ;; have non-font-lock properties hanging about, `text-prop' never gets removed.
|
|
985 ;; Unfortunately `font-lock-any-extents-p' is inlined so we can't redefine it.
|
24
|
986 (when (and lazy-lock-running-xemacs
|
|
987 (= emacs-major-version 19) (< emacs-minor-version 12))
|
|
988 (add-hook 'font-lock-mode-hook
|
|
989 (function (lambda ()
|
|
990 (remove-hook 'after-change-functions 'font-lock-after-change-function)
|
|
991 (add-hook 'after-change-functions
|
|
992 (function (lambda (beg end old-len)
|
|
993 (let ((a-c-beg beg) (a-c-end end))
|
|
994 (save-excursion
|
|
995 ;; First set `text-prop' to nil for `font-lock-any-extents-p'.
|
|
996 (goto-char end) (forward-line 1) (setq end (point))
|
|
997 (goto-char beg) (beginning-of-line) (setq beg (point))
|
|
998 (put-text-property beg end 'text-prop nil)
|
|
999 ;; Then do the real `font-lock-after-change-function'.
|
|
1000 (font-lock-after-change-function a-c-beg a-c-end old-len)
|
|
1001 ;; Now set `fontified' to t to stop `lazy-lock-fontify-window'.
|
|
1002 (put-text-property beg end 'fontified t))))))))))
|
0
|
1003
|
24
|
1004 ;; XEmacs 19.12 font-lock.el's `font-lock-fontify-buffer' runs a hook.
|
|
1005 (when lazy-lock-running-xemacs
|
|
1006 (add-hook 'font-lock-after-fontify-buffer-hook
|
|
1007 'lazy-lock-after-fontify-buffer))
|
0
|
1008
|
24
|
1009 ;; Cope with the differences between Emacs and earlier [LX]Emacs.
|
|
1010 (unless (fboundp 'frame-parameters)
|
|
1011 (defalias 'frame-parameters 'screen-parameters))
|
|
1012
|
|
1013 ;; Cope with the differences between Emacs and earlier [LX]Emacs. Buggy.
|
|
1014 ;(unless (fboundp 'window-displayed-height)
|
|
1015 ; (defalias 'window-displayed-height 'window-height))
|
0
|
1016
|
|
1017 ;; Install ourselves:
|
|
1018
|
|
1019 ;; We don't install ourselves on `font-lock-mode-hook' as other packages can be
|
|
1020 ;; used with font-lock.el, and lazy-lock.el should be dumpable without forcing
|
|
1021 ;; people to get lazy or making it difficult for people to use alternatives.
|
12
|
1022
|
|
1023 ;; After a command is run.
|
|
1024 (lazy-lock-post-command-setup)
|
|
1025
|
|
1026 ;; After some relevant event.
|
0
|
1027 (add-hook 'window-setup-hook 'lazy-lock-post-setup-emacs-fontify-windows)
|
24
|
1028 (add-hook 'window-size-change-functions 'lazy-lock-post-resize-fontify-windows)
|
0
|
1029
|
|
1030 ;; Package-specific.
|
24
|
1031 (add-hook 'ediff-after-setup-control-frame-hooks ; Emacs 19.29, Ediff 2.26.
|
|
1032 'lazy-lock-post-setup-ediff-control-frame)
|
|
1033 (add-hook 'ediff-after-setup-control-frame-hook ; Emacs 19.30, Ediff 2.47.
|
0
|
1034 'lazy-lock-post-setup-ediff-control-frame)
|
|
1035
|
|
1036 ;; Might as well uninstall too. Package-local symbols would be nice...
|
24
|
1037 (when (fboundp 'unintern)
|
|
1038 (unintern 'lazy-lock-running-xemacs)
|
|
1039 (unintern 'lazy-lock-sit-for))
|
0
|
1040
|
|
1041 ;;;###autoload
|
24
|
1042 (when (fboundp 'add-minor-mode)
|
|
1043 (defvar lazy-lock-mode nil)
|
|
1044 (add-minor-mode 'lazy-lock-mode nil))
|
|
1045 ;;;###dont-autoload
|
|
1046 (unless (assq 'lazy-lock-mode minor-mode-alist)
|
|
1047 (setq minor-mode-alist (append minor-mode-alist '((lazy-lock-mode nil)))))
|
0
|
1048
|
|
1049 ;; Provide ourselves:
|
|
1050
|
|
1051 (provide 'lazy-lock)
|
|
1052
|
|
1053 ;;; lazy-lock.el ends here
|