1 ;;; jit-lock.el --- just-in-time fontification
3 ;; Copyright (C) 1998, 2000, 2001, 2002, 2003, 2004,
4 ;; 2005, 2006, 2007, 2008, 2009, 2010 Free Software Foundation, Inc.
6 ;; Author: Gerd Moellmann <gerd@gnu.org>
7 ;; Keywords: faces files
10 ;; This file is part of GNU Emacs.
12 ;; GNU Emacs is free software: you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation, either version 3 of the License, or
15 ;; (at your option) any later version.
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
27 ;; Just-in-time fontification, triggered by C redisplay code.
35 (defmacro with-buffer-prepared-for-jit-lock
(&rest body
)
36 "Execute BODY in current buffer, overriding several variables.
37 Preserves the `buffer-modified-p' state of the current buffer."
39 `(let ((inhibit-point-motion-hooks t
))
40 (with-silent-modifications
45 (defgroup jit-lock nil
46 "Font Lock support mode to fontify just-in-time."
50 (defcustom jit-lock-chunk-size
500
51 "Jit-lock fontifies chunks of at most this many characters at a time.
53 This variable controls both display-time and stealth fontification."
58 (defcustom jit-lock-stealth-time nil
59 "Time in seconds to wait before beginning stealth fontification.
60 Stealth fontification occurs if there is no input within this time.
61 If nil, stealth fontification is never performed.
63 The value of this variable is used when JIT Lock mode is turned on."
64 :type
'(choice (const :tag
"never" nil
)
65 (number :tag
"seconds" :value
16))
69 (defcustom jit-lock-stealth-nice
0.5
70 "Time in seconds to pause between chunks of stealth fontification.
71 Each iteration of stealth fontification is separated by this amount of time,
72 thus reducing the demand that stealth fontification makes on the system.
73 If nil, means stealth fontification is never paused.
74 To reduce machine load during stealth fontification, at the cost of stealth
75 taking longer to fontify, you could increase the value of this variable.
76 See also `jit-lock-stealth-load'."
77 :type
'(choice (const :tag
"never" nil
)
78 (number :tag
"seconds"))
82 (defcustom jit-lock-stealth-load
83 (if (condition-case nil
(load-average) (error)) 200)
84 "Load in percentage above which stealth fontification is suspended.
85 Stealth fontification pauses when the system short-term load average (as
86 returned by the function `load-average' if supported) goes above this level,
87 thus reducing the demand that stealth fontification makes on the system.
88 If nil, means stealth fontification is never suspended.
89 To reduce machine load during stealth fontification, at the cost of stealth
90 taking longer to fontify, you could reduce the value of this variable.
91 See also `jit-lock-stealth-nice'."
92 :type
(if (condition-case nil
(load-average) (error))
93 '(choice (const :tag
"never" nil
)
94 (integer :tag
"load"))
95 '(const :format
"%t: unsupported\n" nil
))
99 (defcustom jit-lock-stealth-verbose nil
100 "If non-nil, means stealth fontification should show status messages."
105 (defvaralias 'jit-lock-defer-contextually
'jit-lock-contextually
)
106 (defcustom jit-lock-contextually
'syntax-driven
107 "If non-nil, means fontification should be syntactically true.
108 If nil, means fontification occurs only on those lines modified. This
109 means where modification on a line causes syntactic change on subsequent lines,
110 those subsequent lines are not refontified to reflect their new context.
111 If t, means fontification occurs on those lines modified and all
112 subsequent lines. This means those subsequent lines are refontified to reflect
113 their new syntactic context, after `jit-lock-context-time' seconds.
114 If any other value, e.g., `syntax-driven', means syntactically true
115 fontification occurs only if syntactic fontification is performed using the
116 buffer mode's syntax table, i.e., only if `font-lock-keywords-only' is nil.
118 The value of this variable is used when JIT Lock mode is turned on."
119 :type
'(choice (const :tag
"never" nil
)
120 (const :tag
"always" t
)
121 (other :tag
"syntax-driven" syntax-driven
))
124 (defcustom jit-lock-context-time
0.5
125 "Idle time after which text is contextually refontified, if applicable."
126 :type
'(number :tag
"seconds")
129 (defcustom jit-lock-defer-time nil
;; 0.25
130 "Idle time after which deferred fontification should take place.
131 If nil, fontification is not deferred."
133 :type
'(choice (const :tag
"never" nil
)
134 (number :tag
"seconds")))
136 ;;; Variables that are not customizable.
138 (defvar jit-lock-mode nil
139 "Non-nil means Just-in-time Lock mode is active.")
140 (make-variable-buffer-local 'jit-lock-mode
)
142 (defvar jit-lock-functions nil
143 "Functions to do the actual fontification.
144 They are called with two arguments: the START and END of the region to fontify.")
145 (make-variable-buffer-local 'jit-lock-functions
)
147 (defvar jit-lock-context-unfontify-pos nil
148 "Consider text after this position as contextually unfontified.
149 If nil, contextual fontification is disabled.")
150 (make-variable-buffer-local 'jit-lock-context-unfontify-pos
)
153 (defvar jit-lock-stealth-timer nil
154 "Timer for stealth fontification in Just-in-time Lock mode.")
155 (defvar jit-lock-stealth-repeat-timer nil
156 "Timer for repeated stealth fontification in Just-in-time Lock mode.")
157 (defvar jit-lock-context-timer nil
158 "Timer for context fontification in Just-in-time Lock mode.")
159 (defvar jit-lock-defer-timer nil
160 "Timer for deferred fontification in Just-in-time Lock mode.")
162 (defvar jit-lock-defer-buffers nil
163 "List of buffers with pending deferred fontification.")
164 (defvar jit-lock-stealth-buffers nil
165 "List of buffers that are being fontified stealthily.")
169 (defun jit-lock-mode (arg)
170 "Toggle Just-in-time Lock mode.
171 Turn Just-in-time Lock mode on if and only if ARG is non-nil.
172 Enable it automatically by customizing group `font-lock'.
174 When Just-in-time Lock mode is enabled, fontification is different in the
177 - Demand-driven buffer fontification triggered by Emacs C code.
178 This means initial fontification of the whole buffer does not occur.
179 Instead, fontification occurs when necessary, such as when scrolling
180 through the buffer would otherwise reveal unfontified areas. This is
181 useful if buffer fontification is too slow for large buffers.
183 - Stealthy buffer fontification if `jit-lock-stealth-time' is non-nil.
184 This means remaining unfontified areas of buffers are fontified if Emacs has
185 been idle for `jit-lock-stealth-time' seconds, while Emacs remains idle.
186 This is useful if any buffer has any deferred fontification.
188 - Deferred context fontification if `jit-lock-contextually' is
189 non-nil. This means fontification updates the buffer corresponding to
190 true syntactic context, after `jit-lock-context-time' seconds of Emacs
191 idle time, while Emacs remains idle. Otherwise, fontification occurs
192 on modified lines only, and subsequent lines can remain fontified
193 corresponding to previous syntactic contexts. This is useful where
194 strings or comments span lines.
196 Stealth fontification only occurs while the system remains unloaded.
197 If the system load rises above `jit-lock-stealth-load' percent, stealth
198 fontification is suspended. Stealth fontification intensity is controlled via
199 the variable `jit-lock-stealth-nice'."
200 (setq jit-lock-mode arg
)
201 (cond (;; Turn Just-in-time Lock mode on.
204 ;; Mark the buffer for refontification.
207 ;; Install an idle timer for stealth fontification.
208 (when (and jit-lock-stealth-time
(null jit-lock-stealth-timer
))
209 (setq jit-lock-stealth-timer
210 (run-with-idle-timer jit-lock-stealth-time t
211 'jit-lock-stealth-fontify
)))
213 ;; Create, but do not activate, the idle timer for repeated
214 ;; stealth fontification.
215 (when (and jit-lock-stealth-time
(null jit-lock-stealth-repeat-timer
))
216 (setq jit-lock-stealth-repeat-timer
(timer-create))
217 (timer-set-function jit-lock-stealth-repeat-timer
218 'jit-lock-stealth-fontify
'(t)))
220 ;; Init deferred fontification timer.
221 (when (and jit-lock-defer-time
(null jit-lock-defer-timer
))
222 (setq jit-lock-defer-timer
223 (run-with-idle-timer jit-lock-defer-time t
224 'jit-lock-deferred-fontify
)))
226 ;; Initialize contextual fontification if requested.
227 (when (eq jit-lock-contextually t
)
228 (unless jit-lock-context-timer
229 (setq jit-lock-context-timer
230 (run-with-idle-timer jit-lock-context-time t
231 'jit-lock-context-fontify
)))
232 (setq jit-lock-context-unfontify-pos
233 (or jit-lock-context-unfontify-pos
(point-max))))
236 (add-hook 'after-change-functions
'jit-lock-after-change nil t
)
237 (add-hook 'fontification-functions
'jit-lock-function
))
239 ;; Turn Just-in-time Lock mode off.
241 ;; Cancel our idle timers.
242 (when (and (or jit-lock-stealth-timer jit-lock-defer-timer
243 jit-lock-context-timer
)
244 ;; Only if there's no other buffer using them.
246 (dolist (buf (buffer-list))
247 (with-current-buffer buf
248 (when jit-lock-mode
(throw 'found t
)))))))
249 (when jit-lock-stealth-timer
250 (cancel-timer jit-lock-stealth-timer
)
251 (setq jit-lock-stealth-timer nil
))
252 (when jit-lock-context-timer
253 (cancel-timer jit-lock-context-timer
)
254 (setq jit-lock-context-timer nil
))
255 (when jit-lock-defer-timer
256 (cancel-timer jit-lock-defer-timer
)
257 (setq jit-lock-defer-timer nil
)))
260 (remove-hook 'after-change-functions
'jit-lock-after-change t
)
261 (remove-hook 'fontification-functions
'jit-lock-function
))))
263 (defun jit-lock-register (fun &optional contextual
)
264 "Register FUN as a fontification function to be called in this buffer.
265 FUN will be called with two arguments START and END indicating the region
266 that needs to be (re)fontified.
267 If non-nil, CONTEXTUAL means that a contextual fontification would be useful."
268 (add-hook 'jit-lock-functions fun nil t
)
269 (when (and contextual jit-lock-contextually
)
270 (set (make-local-variable 'jit-lock-contextually
) t
))
273 (defun jit-lock-unregister (fun)
274 "Unregister FUN as a fontification function.
275 Only applies to the current buffer."
276 (remove-hook 'jit-lock-functions fun t
)
277 (unless jit-lock-functions
(jit-lock-mode nil
)))
279 ;; This function is used to prevent font-lock-fontify-buffer from
280 ;; fontifying eagerly the whole buffer. This is important for
281 ;; things like CWarn mode which adds/removes a few keywords and
282 ;; does a refontify (which takes ages on large files).
283 (defun jit-lock-refontify (&optional beg end
)
284 "Force refontification of the region BEG..END (default whole buffer)."
285 (with-buffer-prepared-for-jit-lock
288 (put-text-property (or beg
(point-min)) (or end
(point-max))
291 ;;; On demand fontification.
293 (defun jit-lock-function (start)
294 "Fontify current buffer starting at position START.
295 This function is added to `fontification-functions' when `jit-lock-mode'
297 (when (and jit-lock-mode
(not memory-full
))
298 (if (null jit-lock-defer-timer
)
300 (jit-lock-fontify-now start
(+ start jit-lock-chunk-size
))
301 ;; Record the buffer for later fontification.
302 (unless (memq (current-buffer) jit-lock-defer-buffers
)
303 (push (current-buffer) jit-lock-defer-buffers
))
304 ;; Mark the area as defer-fontified so that the redisplay engine
305 ;; is happy and so that the idle timer can find the places to fontify.
306 (with-buffer-prepared-for-jit-lock
307 (put-text-property start
308 (next-single-property-change
310 (min (point-max) (+ start jit-lock-chunk-size
)))
311 'fontified
'defer
)))))
313 (defun jit-lock-fontify-now (&optional start end
)
314 "Fontify current buffer from START to END.
315 Defaults to the whole buffer. END can be out of bounds."
316 (with-buffer-prepared-for-jit-lock
318 (unless start
(setq start
(point-min)))
319 (setq end
(if end
(min end
(point-max)) (point-max)))
320 ;; This did bind `font-lock-beginning-of-syntax-function' to
321 ;; nil at some point, for an unknown reason. Don't do this; it
322 ;; can make highlighting slow due to expensive calls to
323 ;; `parse-partial-sexp' in function
324 ;; `font-lock-fontify-syntactically-region'. Example: paging
325 ;; from the end of a buffer to its start, can do repeated
326 ;; `parse-partial-sexp' starting from `point-min', which can
327 ;; take a long time in a large buffer.
328 (let ((orig-start start
) next
)
330 ;; Fontify chunks beginning at START. The end of a
331 ;; chunk is either `end', or the start of a region
332 ;; before `end' that has already been fontified.
333 (while (and start
(< start end
))
334 ;; Determine the end of this chunk.
335 (setq next
(or (text-property-any start end
'fontified t
)
338 ;; Decide which range of text should be fontified.
339 ;; The problem is that START and NEXT may be in the
340 ;; middle of something matched by a font-lock regexp.
341 ;; Until someone has a better idea, let's start
342 ;; at the start of the line containing START and
343 ;; stop at the start of the line following NEXT.
344 (goto-char next
) (setq next
(line-beginning-position 2))
345 (goto-char start
) (setq start
(line-beginning-position))
347 ;; Make sure the contextual refontification doesn't re-refontify
348 ;; what's already been refontified.
349 (when (and jit-lock-context-unfontify-pos
350 (< jit-lock-context-unfontify-pos next
)
351 (>= jit-lock-context-unfontify-pos start
)
352 ;; Don't move boundary forward if we have to
353 ;; refontify previous text. Otherwise, we risk moving
354 ;; it past the end of the multiline property and thus
355 ;; forget about this multiline region altogether.
356 (not (get-text-property start
'jit-lock-defer-multiline
)))
357 (setq jit-lock-context-unfontify-pos next
))
359 ;; Fontify the chunk, and mark it as fontified.
360 ;; We mark it first, to make sure that we don't indefinitely
361 ;; re-execute this fontification if an error occurs.
362 (put-text-property start next
'fontified t
)
364 (run-hook-with-args 'jit-lock-functions start next
)
365 ;; If the user quits (which shouldn't happen in normal on-the-fly
366 ;; jit-locking), make sure the fontification will be performed
367 ;; before displaying the block again.
368 (quit (put-text-property start next
'fontified nil
)
369 (funcall 'signal
(car err
) (cdr err
))))
371 ;; The redisplay engine has already rendered the buffer up-to
372 ;; `orig-start' and won't notice if the above jit-lock-functions
373 ;; changed the appearance of any part of the buffer prior
374 ;; to that. So if `start' is before `orig-start', we need to
375 ;; cause a new redisplay cycle after this one so that any changes
376 ;; are properly reflected on screen.
377 ;; To make such repeated redisplay happen less often, we can
378 ;; eagerly extend the refontified region with
379 ;; jit-lock-after-change-extend-region-functions.
380 (when (< start orig-start
)
381 (run-with-timer 0 nil
'jit-lock-force-redisplay
382 (current-buffer) start orig-start
))
384 ;; Find the start of the next chunk, if any.
385 (setq start
(text-property-any next end
'fontified nil
))))))))
387 (defun jit-lock-force-redisplay (buf start end
)
388 "Force the display engine to re-render buffer BUF from START to END."
389 (with-current-buffer buf
390 (with-buffer-prepared-for-jit-lock
391 ;; Don't cause refontification (it's already been done), but just do
392 ;; some random buffer change, so as to force redisplay.
393 (put-text-property start end
'fontified t
))))
397 ;;; Stealth fontification.
399 (defsubst jit-lock-stealth-chunk-start
(around)
400 "Return the start of the next chunk to fontify around position AROUND.
401 Value is nil if there is nothing more to fontify."
402 (if (zerop (buffer-size))
406 (let* ((next (text-property-not-all around
(point-max) 'fontified t
))
407 (prev (previous-single-property-change around
'fontified
))
408 (prop (get-text-property (max (point-min) (1- around
))
412 ;; There is no property change between AROUND
413 ;; and the start of the buffer. If PROP is
414 ;; non-nil, everything in front of AROUND is
415 ;; fontified, otherwise nothing is fontified.
419 (- around
(/ jit-lock-chunk-size
2)))))
421 ;; PREV is the start of a region of fontified
422 ;; text containing AROUND. Start fontifying a
423 ;; chunk size before the end of the unfontified
424 ;; region in front of that.
425 (max (or (previous-single-property-change prev
'fontified
)
427 (- prev jit-lock-chunk-size
)))
429 ;; PREV is the start of a region of unfontified
430 ;; text containing AROUND. Start at PREV or
431 ;; chunk size in front of AROUND, whichever is
433 (max prev
(- around jit-lock-chunk-size
)))))
434 (result (cond ((null start
) next
)
436 ((< (- around start
) (- next around
)) start
)
440 (defun jit-lock-stealth-fontify (&optional repeat
)
441 "Fontify buffers stealthily.
442 This function is called repeatedly after Emacs has become idle for
443 `jit-lock-stealth-time' seconds. Optional argument REPEAT is expected
444 non-nil in a repeated invocation of this function."
445 ;; Cancel timer for repeated invocations.
447 (cancel-timer jit-lock-stealth-repeat-timer
))
448 (unless (or executing-kbd-macro
450 (window-minibuffer-p (selected-window))
451 ;; For first invocation set up `jit-lock-stealth-buffers'.
452 ;; In repeated invocations it's already been set up.
454 jit-lock-stealth-buffers
455 (setq jit-lock-stealth-buffers
(buffer-list)))))
456 (let ((buffer (car jit-lock-stealth-buffers
))
458 minibuffer-auto-raise
461 (if (and jit-lock-stealth-load
462 (> (car (load-average)) jit-lock-stealth-load
))
463 ;; Wait a little if load is too high.
464 (setq delay jit-lock-stealth-time
)
465 (if (buffer-live-p buffer
)
466 (with-current-buffer buffer
467 (if (and jit-lock-mode
468 (setq start
(jit-lock-stealth-chunk-start (point))))
469 ;; Fontify one block of at most `jit-lock-chunk-size'
471 (with-temp-message (if jit-lock-stealth-verbose
472 (concat "JIT stealth lock "
474 (jit-lock-fontify-now start
475 (+ start jit-lock-chunk-size
))
476 ;; Run again after `jit-lock-stealth-nice' seconds.
477 (setq delay
(or jit-lock-stealth-nice
0)))
478 ;; Nothing to fontify here. Remove this buffer from
479 ;; `jit-lock-stealth-buffers' and run again immediately.
480 (setq jit-lock-stealth-buffers
(cdr jit-lock-stealth-buffers
))))
481 ;; Buffer is no longer live. Remove it from
482 ;; `jit-lock-stealth-buffers' and run again immediately.
483 (setq jit-lock-stealth-buffers
(cdr jit-lock-stealth-buffers
))))
485 (when jit-lock-stealth-buffers
486 (timer-set-idle-time jit-lock-stealth-repeat-timer
(current-idle-time))
487 (timer-inc-time jit-lock-stealth-repeat-timer delay
)
488 (timer-activate-when-idle jit-lock-stealth-repeat-timer t
)))))
491 ;;; Deferred fontification.
493 (defun jit-lock-deferred-fontify ()
494 "Fontify what was deferred."
495 (when (and jit-lock-defer-buffers
(not memory-full
))
496 ;; Mark the deferred regions back to `fontified = nil'
497 (dolist (buffer jit-lock-defer-buffers
)
498 (when (buffer-live-p buffer
)
499 (with-current-buffer buffer
500 ;; (message "Jit-Defer %s" (buffer-name))
501 (with-buffer-prepared-for-jit-lock
502 (let ((pos (point-min)))
505 (when (eq (get-text-property pos
'fontified
) 'defer
)
507 pos
(setq pos
(next-single-property-change
508 pos
'fontified nil
(point-max)))
510 (setq pos
(next-single-property-change pos
'fontified
)))))))))
511 (setq jit-lock-defer-buffers nil
)
512 ;; Force fontification of the visible parts.
513 (let ((jit-lock-defer-timer nil
))
514 ;; (message "Jit-Defer Now")
516 ;; (message "Jit-Defer Done")
520 (defun jit-lock-context-fontify ()
521 "Refresh fontification to take new context into account."
523 (dolist (buffer (buffer-list))
524 (with-current-buffer buffer
525 (when jit-lock-context-unfontify-pos
526 ;; (message "Jit-Context %s" (buffer-name))
529 (when (and (>= jit-lock-context-unfontify-pos
(point-min))
530 (< jit-lock-context-unfontify-pos
(point-max)))
531 ;; If we're in text that matches a complex multi-line
532 ;; font-lock pattern, make sure the whole text will be
533 ;; redisplayed eventually.
534 ;; Despite its name, we treat jit-lock-defer-multiline here
535 ;; rather than in jit-lock-defer since it has to do with multiple
536 ;; lines, i.e. with context.
537 (when (get-text-property jit-lock-context-unfontify-pos
538 'jit-lock-defer-multiline
)
539 (setq jit-lock-context-unfontify-pos
540 (or (previous-single-property-change
541 jit-lock-context-unfontify-pos
542 'jit-lock-defer-multiline
)
544 (with-buffer-prepared-for-jit-lock
545 ;; Force contextual refontification.
546 (remove-text-properties
547 jit-lock-context-unfontify-pos
(point-max)
548 '(fontified nil jit-lock-defer-multiline nil
)))
549 (setq jit-lock-context-unfontify-pos
(point-max)))))))))
551 (defvar jit-lock-start
) (defvar jit-lock-end
) ; Dynamically scoped variables.
552 (defvar jit-lock-after-change-extend-region-functions nil
553 "Hook that can extend the text to refontify after a change.
554 This is run after every buffer change. The functions are called with
555 the three arguments of `after-change-functions': START END OLD-LEN.
556 The extended region to refontify is returned indirectly by modifying
557 the variables `jit-lock-start' and `jit-lock-end'.
559 Note that extending the region this way is not strictly necessary, except
560 that the nature of the redisplay code tends to otherwise leave some of
561 the rehighlighted text displayed with the old highlight until the next
562 redisplay (see comment about repeated redisplay in `jit-lock-fontify-now').")
564 (defun jit-lock-after-change (start end old-len
)
565 "Mark the rest of the buffer as not fontified after a change.
566 Installed on `after-change-functions'.
567 START and END are the start and end of the changed text. OLD-LEN
568 is the pre-change length.
569 This function ensures that lines following the change will be refontified
570 in case the syntax of those lines has changed. Refontification
571 will take place when text is fontified stealthily."
572 (when (and jit-lock-mode
(not memory-full
))
573 (let ((jit-lock-start start
)
575 (with-buffer-prepared-for-jit-lock
576 (run-hook-with-args 'jit-lock-after-change-extend-region-functions
578 ;; Make sure we change at least one char (in case of deletions).
579 (setq jit-lock-end
(min (max jit-lock-end
(1+ start
)) (point-max)))
580 ;; Request refontification.
581 (put-text-property jit-lock-start jit-lock-end
'fontified nil
))
582 ;; Mark the change for deferred contextual refontification.
583 (when jit-lock-context-unfontify-pos
584 (setq jit-lock-context-unfontify-pos
585 ;; Here we use `start' because nothing guarantees that the
586 ;; text between start and end will be otherwise refontified:
587 ;; usually it will be refontified by virtue of being
588 ;; displayed, but if it's outside of any displayed area in the
589 ;; buffer, only jit-lock-context-* will re-fontify it.
590 (min jit-lock-context-unfontify-pos jit-lock-start
))))))
594 ;; arch-tag: 56b5de6e-f581-453b-bb97-49c39372ff9e
595 ;;; jit-lock.el ends here