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
|
207
|
25 ;;; Synched up with: Not in FSF (mostly equivalent to lazy-lock 2.06
|
|
26 ;;; in FSF 19.34).
|
193
|
27
|
|
28 ;;; Commentary:
|
|
29
|
207
|
30 ;;; This is an experimental demand based font-lock implemenation. It
|
|
31 ;;; is almost equal in functionality and interface to lazy-lock 2.06
|
|
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")
|
|
119 (set (make-local-variable 'lazy-shot-mode)
|
|
120 (and (if arg (> (prefix-numeric-value arg) 0) (not lazy-shot-mode))))
|
|
121 (cond ((and lazy-shot-mode (not font-lock-mode))
|
|
122 ;; Turned on `lazy-shot-mode' rather than `font-lock-mode'.
|
|
123 (let ((font-lock-support-mode 'lazy-shot-mode))
|
|
124 (font-lock-mode t)))
|
|
125 (lazy-shot-mode
|
|
126 ;; Turn ourselves on.
|
|
127 (lazy-shot-install))
|
|
128 (t
|
|
129 ;; Turn ourselves off.
|
|
130 (lazy-shot-unstall))))
|
|
131
|
207
|
132 (custom-add-option 'font-lock-mode-hook 'turn-on-lazy-lock)
|
|
133
|
193
|
134 ;;;###autoload
|
|
135 (defun turn-on-lazy-shot ()
|
|
136 "Unconditionally turn on Lazy Lock mode."
|
|
137 (lazy-shot-mode t))
|
|
138
|
207
|
139 ;; Can we do something intelligent here?
|
|
140 ;; I would want to set-extent-end-position start on extents that
|
|
141 ;; only partially overlap!
|
|
142 (defun lazy-shot-clean-up-extents (start end)
|
|
143 "Make sure there are no lazy-shot-extens betweeen START and END.
|
|
144 This improves efficiency and C-g behavior."
|
|
145 ;; Be carefull this function is typically called with inhibit-quit!
|
|
146 (map-extents (lambda (e b) (delete-extent e))
|
|
147 nil start end nil 'start-and-end-in-region 'initial-redisplay-function
|
|
148 'lazy-shot-redisplay-function))
|
|
149
|
|
150 (defun lazy-shot-redisplay-function (extent)
|
|
151 "Lazy lock the EXTENT when it has become visisble."
|
|
152 (lazy-shot-lock-extent extent nil))
|
193
|
153
|
207
|
154 (defun lazy-shot-lock-extent (extent stealth)
|
|
155 "Font-lock the EXTENT. Called from redisplay-trigger functions and
|
|
156 stealth locking functions"
|
|
157 (when (extent-live-p extent)
|
|
158 (let ((start (extent-start-position extent))
|
|
159 (end (extent-end-position extent))
|
|
160 (buffer (extent-object extent)))
|
|
161 (delete-extent extent)
|
|
162 (save-excursion
|
|
163 ;; Should inhibit quit here
|
|
164 (set-buffer buffer) ;; with-current-buffer is silly here
|
|
165 ;; This magic should really go into font-lock-fonity-region
|
|
166 (goto-char start)
|
|
167 (setq start (point-at-bol))
|
|
168 (goto-char end)
|
|
169 (setq end (point-at-bol 2))
|
|
170 (lazy-shot-clean-up-extents start end)
|
|
171 (if (or lazy-shot-verbose (and stealth lazy-shot-stealth-verbose))
|
|
172 (display-message 'progress
|
|
173 (format "Lazy-shot fontifying %sfrom %s to %s in %s"
|
|
174 (if stealth "stealthy " "") start end buffer)))
|
|
175 ;; and a allow quit here
|
|
176 (save-match-data
|
|
177 (font-lock-fontify-region start end))))))
|
193
|
178
|
207
|
179 (defun lazy-shot-stealth-lock (buffer)
|
|
180 "Find an extent to lazy lock in buffer."
|
|
181 (if (buffer-live-p buffer)
|
|
182 (with-current-buffer buffer
|
|
183 (let ((extent t))
|
|
184 (while (and extent (sit-for lazy-shot-stealth-nice))
|
|
185 (setq extent
|
|
186 (or ;; First after point
|
|
187 (map-extents (lambda (e n) e) nil (point) nil nil nil
|
|
188 'initial-redisplay-function
|
|
189 'lazy-shot-redisplay-function)
|
|
190 ;; Then before it
|
|
191 (map-extents (lambda (e n) e) nil nil (point) nil nil
|
|
192 'initial-redisplay-function
|
|
193 'lazy-shot-redisplay-function)))
|
|
194 (if extent
|
|
195 (lazy-shot-lock-extent extent t)
|
|
196 (delete-itimer current-itimer)
|
|
197 (setq lazy-shot-stealth-timer nil)))))
|
|
198 (delete-itimer current-itimer)))
|
|
199
|
193
|
200 (defun lazy-shot-install-extent (spos epos &optional buffer)
|
207
|
201 "Make an extent that will lazy-shot if it is displayed."
|
193
|
202 (let ((extent (make-extent spos epos buffer)))
|
|
203 (when extent
|
207
|
204 (set-extent-initial-redisplay-function extent
|
|
205 'lazy-shot-redisplay-function))
|
193
|
206 extent))
|
|
207
|
203
|
208
|
193
|
209 (defun lazy-shot-install-extents (fontifying)
|
|
210 ;;
|
|
211 ;; Add hook if lazy-shot.el is deferring or is fontifying on scrolling.
|
203
|
212 (when fontifying
|
207
|
213 (let ((max (point-max))
|
|
214 start)
|
|
215 (save-excursion
|
|
216 (goto-char (point-min))
|
|
217 (while (not (eobp))
|
|
218 (setq start (point))
|
|
219 (goto-char (min max (+ start lazy-shot-step-size)))
|
|
220 (forward-line 1)
|
|
221 (lazy-shot-install-extent start (point)))))))
|
|
222
|
|
223 (defun lazy-shot-install-timer (fontifying)
|
|
224 (when (and lazy-shot-stealth-time fontifying)
|
|
225 (make-variable-buffer-local 'lazy-shot-stealth-timer)
|
|
226 (setq lazy-shot-stealth-timer
|
|
227 (start-itimer (format "lazy shot for %s" (current-buffer))
|
|
228 'lazy-shot-stealth-lock lazy-shot-stealth-time
|
|
229 lazy-shot-stealth-time
|
|
230 t t (current-buffer)))))
|
|
231
|
193
|
232
|
|
233 (defun lazy-shot-install ()
|
|
234 (make-local-variable 'font-lock-fontified)
|
207
|
235 (setq font-lock-fontified (and lazy-shot-minimum-size
|
|
236 (>= (buffer-size) lazy-shot-minimum-size)))
|
|
237 (lazy-shot-install-extents font-lock-fontified)
|
|
238 (lazy-shot-install-timer font-lock-fontified))
|
193
|
239
|
|
240 (defun lazy-shot-unstall ()
|
207
|
241 ;; Stop the timer
|
|
242 (when lazy-shot-stealth-timer
|
|
243 (delete-itimer lazy-shot-stealth-timer)
|
|
244 (setq lazy-shot-stealth-timer nil))
|
193
|
245 ;; Remove the extents.
|
|
246 (map-extents
|
|
247 (lambda (e arg) (delete-extent e) nil)
|
207
|
248 nil nil nil nil nil 'initial-redisplay-function 'lazy-shot-redisplay-function)
|
193
|
249 ;;
|
|
250 ;; Remove the fontification hooks.
|
|
251 (remove-hook 'after-change-functions 'lazy-shot-defer-after-change t)
|
|
252 ;;
|
|
253 ;; If Font Lock mode is still enabled, reinstall its hook.
|
|
254 (when font-lock-mode
|
|
255 (add-hook 'after-change-functions 'font-lock-after-change-function nil t)))
|
|
256
|
|
257
|
|
258 (provide 'lazy-shot)
|
|
259
|
|
260 ;;; lazy-shot.el ends here
|