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