comparison lisp/jit-lock.el @ 29413:ce16b083b459

(jit-lock-saved-fontify-buffer-function): New var. (jit-lock-fontify-buffer): New function for JIT refontification. (jit-lock-mode): Fix docstring. Use jit-lock-fontify-buffer for font-lock-fontify-buffer-function. Remove jit-lock-after-change from the _local_ hook. (jit-lock-function-1): Fix docstring.
author Stefan Monnier <monnier@iro.umontreal.ca>
date Mon, 05 Jun 2000 03:08:50 +0000
parents dbb0996702bd
children f37b857741b1
comparison
equal deleted inserted replaced
29412:ecd388a0937d 29413:ce16b083b459
145 145
146 146
147 (defvar jit-lock-stealth-timer nil 147 (defvar jit-lock-stealth-timer nil
148 "Timer for stealth fontification in Just-in-time Lock mode.") 148 "Timer for stealth fontification in Just-in-time Lock mode.")
149 149
150 (defvar jit-lock-saved-fontify-buffer-function nil
151 "Value of `font-lock-fontify-buffer-function' before jit-lock's activation.")
150 152
151 153
152 ;;; JIT lock mode 154 ;;; JIT lock mode
153 155
154 ;;;###autoload 156 ;;;###autoload
180 strings or comments span lines. 182 strings or comments span lines.
181 183
182 Stealth fontification only occurs while the system remains unloaded. 184 Stealth fontification only occurs while the system remains unloaded.
183 If the system load rises above `jit-lock-stealth-load' percent, stealth 185 If the system load rises above `jit-lock-stealth-load' percent, stealth
184 fontification is suspended. Stealth fontification intensity is controlled via 186 fontification is suspended. Stealth fontification intensity is controlled via
185 the variable `jit-lock-stealth-nice' and `jit-lock-stealth-lines'." 187 the variable `jit-lock-stealth-nice'."
186 (interactive "P") 188 (interactive "P")
187 (setq jit-lock-mode (if arg 189 (setq jit-lock-mode (if arg
188 (> (prefix-numeric-value arg) 0) 190 (> (prefix-numeric-value arg) 0)
189 (not jit-lock-mode))) 191 (not jit-lock-mode)))
190 (cond ((and jit-lock-mode 192 (cond ((and jit-lock-mode
197 199
198 ;; Turn Just-in-time Lock mode on. 200 ;; Turn Just-in-time Lock mode on.
199 (jit-lock-mode 201 (jit-lock-mode
200 ;; Setting `font-lock-fontified' makes font-lock believe the 202 ;; Setting `font-lock-fontified' makes font-lock believe the
201 ;; buffer is already fontified, so that it won't highlight 203 ;; buffer is already fontified, so that it won't highlight
202 ;; the whole buffer. 204 ;; the whole buffer or bail out on a large buffer.
203 (make-local-variable 'font-lock-fontified) 205 (make-local-variable 'font-lock-fontified)
204 (setq font-lock-fontified t) 206 (setq font-lock-fontified t)
207
208 ;; Setup JIT font-lock-fontify-buffer.
209 (unless jit-lock-saved-fontify-buffer-function
210 (set (make-local-variable 'jit-lock-saved-fontify-buffer-function)
211 font-lock-fontify-buffer-function)
212 (set (make-local-variable 'font-lock-fontify-buffer-function)
213 'jit-lock-fontify-buffer))
205 214
206 (setq jit-lock-first-unfontify-pos nil) 215 (setq jit-lock-first-unfontify-pos nil)
207 216
208 ;; Install an idle timer for stealth fontification. 217 ;; Install an idle timer for stealth fontification.
209 (when (and jit-lock-stealth-time 218 (when (and jit-lock-stealth-time
210 (null jit-lock-stealth-timer)) 219 (null jit-lock-stealth-timer))
211 (setq jit-lock-stealth-timer 220 (setq jit-lock-stealth-timer
212 (run-with-idle-timer jit-lock-stealth-time 221 (run-with-idle-timer jit-lock-stealth-time
213 jit-lock-stealth-time 222 jit-lock-stealth-time
214 'jit-lock-stealth-fontify))) 223 'jit-lock-stealth-fontify)))
215 224
216 ;; Add a hook for deferred contectual fontification. 225 ;; Add a hook for deferred contectual fontification.
227 ;; Cancel our idle timer. 236 ;; Cancel our idle timer.
228 (when jit-lock-stealth-timer 237 (when jit-lock-stealth-timer
229 (cancel-timer jit-lock-stealth-timer) 238 (cancel-timer jit-lock-stealth-timer)
230 (setq jit-lock-stealth-timer nil)) 239 (setq jit-lock-stealth-timer nil))
231 240
241 ;; Restore non-JIT font-lock-fontify-buffer.
242 (when jit-lock-saved-fontify-buffer-function
243 (set (make-local-variable 'font-lock-fontify-buffer-function)
244 jit-lock-saved-fontify-buffer-function)
245 (setq jit-lock-saved-fontify-buffer-function nil))
246
232 ;; Remove hooks. 247 ;; Remove hooks.
233 (remove-hook 'after-change-functions 'jit-lock-after-change) 248 (remove-hook 'after-change-functions 'jit-lock-after-change t)
234 (remove-hook 'fontification-functions 'jit-lock-function)))) 249 (remove-hook 'fontification-functions 'jit-lock-function))))
235 250
236 251
237 ;;;###autoload 252 ;;;###autoload
238 (defun turn-on-jit-lock () 253 (defun turn-on-jit-lock ()
239 "Unconditionally turn on Just-in-time Lock mode." 254 "Unconditionally turn on Just-in-time Lock mode."
240 (jit-lock-mode 1)) 255 (jit-lock-mode 1))
241 256
257 ;; This function is used to prevent font-lock-fontify-buffer from
258 ;; fontifying eagerly the whole buffer. This is important for
259 ;; things like CWarn mode which adds/removes a few keywords and
260 ;; does a refontify (which takes ages on large files).
261 (defun jit-lock-fontify-buffer ()
262 (if (not (and font-lock-mode jit-lock-mode))
263 (funcall jit-lock-saved-fontify-buffer-function)
264 (with-buffer-prepared-for-font-lock
265 (save-restriction
266 (widen)
267 (add-text-properties (point-min) (point-max) '(fontified nil))))))
242 268
243 269
244 ;;; On demand fontification. 270 ;;; On demand fontification.
245 271
246 (defun jit-lock-function (start) 272 (defun jit-lock-function (start)
250 (when jit-lock-mode 276 (when jit-lock-mode
251 (jit-lock-function-1 start))) 277 (jit-lock-function-1 start)))
252 278
253 279
254 (defun jit-lock-function-1 (start) 280 (defun jit-lock-function-1 (start)
255 "Fontify current buffer starting at position START. 281 "Fontify current buffer starting at position START."
256 This function is added to `fontification-functions' when `jit-lock-mode'
257 is active."
258 (with-buffer-prepared-for-font-lock 282 (with-buffer-prepared-for-font-lock
259 (save-excursion 283 (save-excursion
260 (save-restriction 284 (save-restriction
261 (widen) 285 (widen)
262 (let ((end (min (point-max) (+ start jit-lock-chunk-size))) 286 (let ((end (min (point-max) (+ start jit-lock-chunk-size)))