193
|
1 ;;; lazy-shot.el --- Lazy font locking for XEmacs
|
|
2
|
|
3 ;; Copyright (C) 1997 Jan Vroonhof
|
|
4
|
|
5 ;; Author: Jan Vroonhof <vroonhof@math.ethz.ch>
|
|
6 ;; Keywords: languages, faces
|
|
7
|
|
8 ;; This file is part of XEmacs
|
|
9
|
|
10 ;; XEmacs is free software; you can redistribute it and/or modify it
|
|
11 ;; under the terms of the GNU General Public License as published by
|
|
12 ;; the Free Software Foundation; either version 2, or (at your option)
|
|
13 ;; any later version.
|
|
14
|
|
15 ;; XEmacs is distributed in the hope that it will be useful, but
|
|
16 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
18 ;; General Public License for more details.
|
|
19
|
|
20 ;; You should have received a copy of the GNU General Public License
|
|
21 ;; along with XEmacs; see the file COPYING. If not, write to the
|
|
22 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
|
|
23 ;; Boston, MA 02111-1307, USA.
|
|
24
|
209
|
25 ;;; Synched up with: Not in FSF (mostly equivalent to lazy-lock 2.09
|
|
26 ;;; in FSF 20.2).
|
193
|
27
|
|
28 ;;; Commentary:
|
|
29
|
207
|
30 ;;; This is an experimental demand based font-lock implemenation. It
|
209
|
31 ;;; is almost equal in functionality and interface to lazy-lock 2.09
|
207
|
32 ;;; Does somebody really need defer-locking?
|
|
33 ;;;
|
|
34 ;;; To use: put
|
|
35 ;;; (add-hook 'font-lock-mode-hook 'turn-on-lazy-shot)
|
|
36 ;;; in .emacs (.xemacs/init.el). Do not use in combination with
|
|
37 ;;; lazy-lock.
|
|
38
|
|
39 ;;; It is exprimental in the sense that it relies on C support from
|
|
40 ;;; the redisplay engine, that is experimental. The code in this file
|
|
41 ;;; is more or less finished. The C code support experimental because
|
|
42 ;;; the current design is rumoured to be ugly. Secondly because
|
|
43 ;;; XEmacs does actually display the "un-font-locked" parts of the
|
|
44 ;;; buffer first, the user notices flashing as the buffer is repainted
|
|
45 ;;; with color/fonts.
|
193
|
46
|
|
47 ;;; Code:
|
|
48
|
|
49 (require 'font-lock)
|
207
|
50 (require 'itimer)
|
193
|
51
|
|
52 (defvar lazy-shot-mode nil)
|
|
53
|
|
54
|
203
|
55 (defgroup lazy-shot nil
|
|
56 "Lazy-shot customizations"
|
|
57 :group 'tools
|
207
|
58 :group 'faces
|
203
|
59 :prefix "lazy-shot-")
|
|
60
|
207
|
61 (defcustom lazy-shot-minimum-size 0
|
|
62 "*Minimum size of a buffer for demand-driven fontification.
|
|
63 On-demand fontification occurs if the buffer size is greater than this value.
|
|
64 If nil, means demand-driven fontification is never performed."
|
|
65 :type '(choice (const :tag "Off" nil)
|
|
66 (integer :tag "Size"))
|
|
67 :group 'lazy-shot)
|
|
68
|
|
69
|
203
|
70 (defcustom lazy-shot-step-size 1024 ; Please test diffent sizes
|
|
71 "Minimum size of each fontification shot."
|
|
72 :type 'integer
|
|
73 :group 'lazy-shot)
|
193
|
74
|
207
|
75 (defcustom lazy-shot-stealth-time 30
|
|
76 "*Time in seconds to delay before beginning stealth fontification.
|
|
77 Stealth fontification occurs if there is no input within this time.
|
|
78 If nil, means stealth fontification is never performed.
|
|
79
|
|
80 The value of this variable is used when Lazy Shot mode is turned on."
|
|
81 :type '(choice (const :tag "Off" nil)
|
|
82 (number :tag "Time"))
|
|
83 :group 'lazy-shot)
|
|
84
|
|
85 (defcustom lazy-shot-stealth-lines (if font-lock-maximum-decoration 100 250)
|
|
86 "*Maximum size of a chunk of stealth fontification.
|
|
87 Each iteration of stealth fontification can fontify this number of lines.
|
|
88 To speed up input response during stealth fontification, at the cost of stealth
|
|
89 taking longer to fontify, you could reduce the value of this variable."
|
|
90 :type 'integer
|
|
91 :group 'lazy-shot)
|
|
92
|
|
93 (defcustom lazy-shot-stealth-nice
|
|
94 (/ (float 1) (float 8))
|
|
95 "*Time in seconds to pause between chunks of stealth fontification.
|
|
96 Each iteration of stealth fontification is separated by this amount of time.
|
|
97 To reduce machine load during stealth fontification, at the cost of stealth
|
|
98 taking longer to fontify, you could increase the value of this variable."
|
|
99 :type 'number
|
|
100 :group 'lazy-shot)
|
|
101
|
|
102 (defcustom lazy-shot-verbose (not (null font-lock-verbose))
|
|
103 "*If non-nil, means demand fontification should show status messages."
|
|
104 :type 'boolean
|
|
105 :group 'lazy-shot)
|
|
106
|
|
107 (defcustom lazy-shot-stealth-verbose (not (null lazy-shot-verbose))
|
|
108 "*If non-nil, means stealth fontification should show status messages."
|
|
109 :type 'boolean
|
|
110 :group 'lazy-shot)
|
|
111
|
|
112
|
|
113
|
193
|
114 ;;;###autoload
|
|
115 (defun lazy-shot-mode (&optional arg)
|
|
116 "Toggle Lazy Lock mode.
|
|
117 With arg, turn Lazy Lock mode on if and only if arg is positive."
|
|
118 (interactive "P")
|
209
|
119 (let ((was-on lazy-shot-mode))
|
|
120 (set (make-local-variable 'lazy-shot-mode)
|
|
121 (and (if arg (> (prefix-numeric-value arg) 0) (not lazy-shot-mode))))
|
|
122 (cond ((and lazy-shot-mode (not font-lock-mode))
|
|
123 ;; Turned on `lazy-shot-mode' rather than `font-lock-mode'.
|
|
124 (let ((font-lock-support-mode 'lazy-shot-mode))
|
|
125 (font-lock-mode t)))
|
|
126 (lazy-shot-mode
|
|
127 ;; Turn ourselves on.
|
|
128 (lazy-shot-install))
|
|
129 (was-on
|
|
130 ;; Turn ourselves off.
|
|
131 (lazy-shot-unstall)))))
|
193
|
132
|
207
|
133 (custom-add-option 'font-lock-mode-hook 'turn-on-lazy-lock)
|
|
134
|
193
|
135 ;;;###autoload
|
|
136 (defun turn-on-lazy-shot ()
|
|
137 "Unconditionally turn on Lazy Lock mode."
|
|
138 (lazy-shot-mode t))
|
|
139
|
207
|
140 ;; Can we do something intelligent here?
|
|
141 ;; I would want to set-extent-end-position start on extents that
|
|
142 ;; only partially overlap!
|
|
143 (defun lazy-shot-clean-up-extents (start end)
|
|
144 "Make sure there are no lazy-shot-extens betweeen START and END.
|
|
145 This improves efficiency and C-g behavior."
|
|
146 ;; Be carefull this function is typically called with inhibit-quit!
|
|
147 (map-extents (lambda (e b) (delete-extent e))
|
|
148 nil start end nil 'start-and-end-in-region 'initial-redisplay-function
|
|
149 'lazy-shot-redisplay-function))
|
|
150
|
|
151 (defun lazy-shot-redisplay-function (extent)
|
|
152 "Lazy lock the EXTENT when it has become visisble."
|
|
153 (lazy-shot-lock-extent extent nil))
|
193
|
154
|
209
|
155
|
207
|
156 (defun lazy-shot-lock-extent (extent stealth)
|
|
157 "Font-lock the EXTENT. Called from redisplay-trigger functions and
|
|
158 stealth locking functions"
|
|
159 (when (extent-live-p extent)
|
|
160 (let ((start (extent-start-position extent))
|
|
161 (end (extent-end-position extent))
|
|
162 (buffer (extent-object extent)))
|
|
163 (delete-extent extent)
|
209
|
164 (lazy-shot-fontify-internal buffer start end
|
|
165 (or lazy-shot-verbose
|
|
166 (and stealth
|
|
167 lazy-shot-stealth-verbose))
|
|
168 (if stealth "stealthy " "")))))
|
|
169
|
|
170 (defun lazy-shot-fontify-internal (buffer start end verbose message)
|
|
171 (save-excursion
|
|
172 ;; Should inhibit quit here
|
|
173 (set-buffer buffer) ;; with-current-buffer is silly here
|
|
174 ;; This magic should really go into font-lock-fonity-region
|
|
175 (goto-char start)
|
|
176 (setq start (point-at-bol))
|
|
177 (goto-char end)
|
|
178 (setq end (point-at-bol 2))
|
|
179 (lazy-shot-clean-up-extents start end)
|
|
180 ;; and a allow quit here
|
|
181 (if verbose
|
|
182 (display-message 'progress
|
|
183 (format "Lazy-shot fontifying %sfrom %s to %s in %s"
|
|
184 message start end buffer)))
|
|
185 (save-match-data
|
|
186 (font-lock-fontify-region start end))))
|
|
187
|
|
188 ;; Note this is suboptimal but works for now. It is not called that often.
|
|
189 (defun lazy-shot-fontify-region (start end &optional buffer)
|
|
190 (lazy-shot-fontify-internal (or buffer (current-buffer))
|
|
191 start end lazy-shot-verbose
|
|
192 "on request "))
|
193
|
193
|
207
|
194 (defun lazy-shot-stealth-lock (buffer)
|
|
195 "Find an extent to lazy lock in buffer."
|
|
196 (if (buffer-live-p buffer)
|
|
197 (with-current-buffer buffer
|
|
198 (let ((extent t))
|
|
199 (while (and extent (sit-for lazy-shot-stealth-nice))
|
|
200 (setq extent
|
|
201 (or ;; First after point
|
|
202 (map-extents (lambda (e n) e) nil (point) nil nil nil
|
|
203 'initial-redisplay-function
|
|
204 'lazy-shot-redisplay-function)
|
|
205 ;; Then before it
|
|
206 (map-extents (lambda (e n) e) nil nil (point) nil nil
|
|
207 'initial-redisplay-function
|
|
208 'lazy-shot-redisplay-function)))
|
|
209 (if extent
|
|
210 (lazy-shot-lock-extent extent t)
|
|
211 (delete-itimer current-itimer)
|
|
212 (setq lazy-shot-stealth-timer nil)))))
|
|
213 (delete-itimer current-itimer)))
|
|
214
|
193
|
215 (defun lazy-shot-install-extent (spos epos &optional buffer)
|
207
|
216 "Make an extent that will lazy-shot if it is displayed."
|
193
|
217 (let ((extent (make-extent spos epos buffer)))
|
|
218 (when extent
|
207
|
219 (set-extent-initial-redisplay-function extent
|
|
220 'lazy-shot-redisplay-function))
|
193
|
221 extent))
|
|
222
|
203
|
223
|
193
|
224 (defun lazy-shot-install-extents (fontifying)
|
|
225 ;;
|
|
226 ;; Add hook if lazy-shot.el is deferring or is fontifying on scrolling.
|
203
|
227 (when fontifying
|
207
|
228 (let ((max (point-max))
|
|
229 start)
|
|
230 (save-excursion
|
|
231 (goto-char (point-min))
|
|
232 (while (not (eobp))
|
|
233 (setq start (point))
|
|
234 (goto-char (min max (+ start lazy-shot-step-size)))
|
|
235 (forward-line 1)
|
|
236 (lazy-shot-install-extent start (point)))))))
|
|
237
|
|
238 (defun lazy-shot-install-timer (fontifying)
|
|
239 (when (and lazy-shot-stealth-time fontifying)
|
|
240 (make-variable-buffer-local 'lazy-shot-stealth-timer)
|
|
241 (setq lazy-shot-stealth-timer
|
|
242 (start-itimer (format "lazy shot for %s" (current-buffer))
|
|
243 'lazy-shot-stealth-lock lazy-shot-stealth-time
|
|
244 lazy-shot-stealth-time
|
|
245 t t (current-buffer)))))
|
|
246
|
193
|
247
|
|
248 (defun lazy-shot-install ()
|
|
249 (make-local-variable 'font-lock-fontified)
|
207
|
250 (setq font-lock-fontified (and lazy-shot-minimum-size
|
|
251 (>= (buffer-size) lazy-shot-minimum-size)))
|
|
252 (lazy-shot-install-extents font-lock-fontified)
|
209
|
253 (lazy-shot-install-timer font-lock-fontified)
|
|
254 (add-hook 'font-lock-after-fontify-buffer-hook
|
|
255 'lazy-shot-unstall-after-fontify))
|
193
|
256
|
209
|
257 ;; Kludge needed untill lazy-lock-fontify-region is more intelligent
|
|
258 (defun lazy-shot-unstall-after-fontify ()
|
|
259 (lazy-shot-unstall 1))
|
|
260
|
|
261 (defun lazy-shot-unstall (&optional no-fontify)
|
207
|
262 ;; Stop the timer
|
|
263 (when lazy-shot-stealth-timer
|
|
264 (delete-itimer lazy-shot-stealth-timer)
|
|
265 (setq lazy-shot-stealth-timer nil))
|
193
|
266 ;; Remove the extents.
|
|
267 (map-extents
|
|
268 (lambda (e arg) (delete-extent e) nil)
|
207
|
269 nil nil nil nil nil 'initial-redisplay-function 'lazy-shot-redisplay-function)
|
209
|
270 (when (and font-lock-mode (not no-fontify))
|
|
271 (save-restriction
|
|
272 (widen)
|
|
273 (lazy-shot-fontify-region (point-min) (point-max)))))
|
193
|
274
|
|
275 (provide 'lazy-shot)
|
|
276
|
|
277 ;;; lazy-shot.el ends here
|