*** empty log message ***
[bpt/emacs.git] / lisp / jit-lock.el
CommitLineData
e8af40ee 1;;; jit-lock.el --- just-in-time fontification
7840ced1 2
cd9b540f 3;; Copyright (C) 1998, 2000, 2001 Free Software Foundation, Inc.
7840ced1
GM
4
5;; Author: Gerd Moellmann <gerd@gnu.org>
6;; Keywords: faces files
7840ced1
GM
7
8;; This file is part of GNU Emacs.
9
10;; GNU Emacs is free software; you can redistribute it and/or modify
11;; it 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;; GNU Emacs is distributed in the hope that it will be useful,
16;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18;; GNU General Public License for more details.
19
20;; You should have received a copy of the GNU General Public License
21;; along with GNU Emacs; 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
25;;; Commentary:
26
27;; Just-in-time fontification, triggered by C redisplay code.
28
29;;; Code:
30
31
7840ced1 32(eval-when-compile
60bffb78
GM
33 (defmacro with-buffer-unmodified (&rest body)
34 "Eval BODY, preserving the current buffer's modified state."
35 (let ((modified (make-symbol "modified")))
36 `(let ((,modified (buffer-modified-p)))
be390cb3
SM
37 (unwind-protect
38 (progn ,@body)
39 (unless ,modified
40 (restore-buffer-modified-p nil))))))
60bffb78 41
bcacade9 42 (defmacro with-buffer-prepared-for-jit-lock (&rest body)
7840ced1
GM
43 "Execute BODY in current buffer, overriding several variables.
44Preserves the `buffer-modified-p' state of the current buffer."
9f1a8fb4
GM
45 `(with-buffer-unmodified
46 (let ((buffer-undo-list t)
47 (inhibit-read-only t)
48 (inhibit-point-motion-hooks t)
bcacade9 49 (inhibit-modification-hooks t)
9f1a8fb4
GM
50 deactivate-mark
51 buffer-file-name
52 buffer-file-truename)
53 ,@body))))
7840ced1 54
60bffb78 55
7840ced1
GM
56\f
57;;; Customization.
58
59(defcustom jit-lock-chunk-size 500
bcacade9 60 "*Jit-lock chunks of this many characters, or smaller."
7840ced1
GM
61 :type 'integer
62 :group 'jit-lock)
63
64
65(defcustom jit-lock-stealth-time 3
66 "*Time in seconds to wait before beginning stealth fontification.
67Stealth fontification occurs if there is no input within this time.
f86292a9 68If nil, stealth fontification is never performed.
7840ced1
GM
69
70The value of this variable is used when JIT Lock mode is turned on."
71 :type '(choice (const :tag "never" nil)
72 (number :tag "seconds"))
73 :group 'jit-lock)
74
75
76(defcustom jit-lock-stealth-nice 0.125
77 "*Time in seconds to pause between chunks of stealth fontification.
78Each iteration of stealth fontification is separated by this amount of time,
79thus reducing the demand that stealth fontification makes on the system.
80If nil, means stealth fontification is never paused.
81To reduce machine load during stealth fontification, at the cost of stealth
82taking longer to fontify, you could increase the value of this variable.
83See also `jit-lock-stealth-load'."
84 :type '(choice (const :tag "never" nil)
85 (number :tag "seconds"))
86 :group 'jit-lock)
87
88
89(defcustom jit-lock-stealth-load
90 (if (condition-case nil (load-average) (error)) 200)
91 "*Load in percentage above which stealth fontification is suspended.
92Stealth fontification pauses when the system short-term load average (as
93returned by the function `load-average' if supported) goes above this level,
94thus reducing the demand that stealth fontification makes on the system.
95If nil, means stealth fontification is never suspended.
96To reduce machine load during stealth fontification, at the cost of stealth
97taking longer to fontify, you could reduce the value of this variable.
98See also `jit-lock-stealth-nice'."
99 :type (if (condition-case nil (load-average) (error))
100 '(choice (const :tag "never" nil)
101 (integer :tag "load"))
102 '(const :format "%t: unsupported\n" nil))
103 :group 'jit-lock)
104
105
106(defcustom jit-lock-stealth-verbose nil
107 "*If non-nil, means stealth fontification should show status messages."
108 :type 'boolean
109 :group 'jit-lock)
110
111
112(defcustom jit-lock-defer-contextually 'syntax-driven
113 "*If non-nil, means deferred fontification should be syntactically true.
114If nil, means deferred fontification occurs only on those lines modified. This
115means where modification on a line causes syntactic change on subsequent lines,
116those subsequent lines are not refontified to reflect their new context.
117If t, means deferred fontification occurs on those lines modified and all
118subsequent lines. This means those subsequent lines are refontified to reflect
119their new syntactic context, either immediately or when scrolling into them.
120If any other value, e.g., `syntax-driven', means deferred syntactically true
121fontification occurs only if syntactic fontification is performed using the
122buffer mode's syntax table, i.e., only if `font-lock-keywords-only' is nil.
123
124The value of this variable is used when JIT Lock mode is turned on."
125 :type '(choice (const :tag "never" nil)
126 (const :tag "always" t)
127 (other :tag "syntax-driven" syntax-driven))
128 :group 'jit-lock)
129
8e069ce2 130(defcustom jit-lock-defer-time nil ;; 0.25
b743187d
SM
131 "Idle time after which deferred fontification should take place.
132If nil, fontification is not deferred."
133 :group 'jit-lock
134 :type '(choice (const :tag "never" nil)
135 (number :tag "seconds")))
7840ced1
GM
136\f
137;;; Variables that are not customizable.
138
139(defvar jit-lock-mode nil
140 "Non-nil means Just-in-time Lock mode is active.")
141(make-variable-buffer-local 'jit-lock-mode)
142
be390cb3
SM
143(defvar jit-lock-functions nil
144 "Functions to do the actual fontification.
145They are called with two arguments: the START and END of the region to fontify.")
a62e3c6f 146(make-variable-buffer-local 'jit-lock-functions)
7840ced1
GM
147
148(defvar jit-lock-first-unfontify-pos nil
a62e3c6f 149 "Consider text after this position as contextually unfontified.
bcacade9 150If nil, contextual fontification is disabled.")
7840ced1
GM
151(make-variable-buffer-local 'jit-lock-first-unfontify-pos)
152
153
154(defvar jit-lock-stealth-timer nil
155 "Timer for stealth fontification in Just-in-time Lock mode.")
b743187d
SM
156
157(defvar jit-lock-defer-timer nil
158 "Timer for deferred fontification in Just-in-time Lock mode.")
159
160(defvar jit-lock-buffers nil
161 "List of buffers with pending deferred fontification.")
7840ced1
GM
162\f
163;;; JIT lock mode
164
7840ced1
GM
165(defun jit-lock-mode (arg)
166 "Toggle Just-in-time Lock mode.
bcacade9 167Turn Just-in-time Lock mode on if and only if ARG is non-nil.
7840ced1
GM
168Enable it automatically by customizing group `font-lock'.
169
170When Just-in-time Lock mode is enabled, fontification is different in the
171following ways:
172
173- Demand-driven buffer fontification triggered by Emacs C code.
174 This means initial fontification of the whole buffer does not occur.
175 Instead, fontification occurs when necessary, such as when scrolling
176 through the buffer would otherwise reveal unfontified areas. This is
177 useful if buffer fontification is too slow for large buffers.
178
179- Stealthy buffer fontification if `jit-lock-stealth-time' is non-nil.
180 This means remaining unfontified areas of buffers are fontified if Emacs has
181 been idle for `jit-lock-stealth-time' seconds, while Emacs remains idle.
182 This is useful if any buffer has any deferred fontification.
183
184- Deferred context fontification if `jit-lock-defer-contextually' is
185 non-nil. This means fontification updates the buffer corresponding to
186 true syntactic context, after `jit-lock-stealth-time' seconds of Emacs
187 idle time, while Emacs remains idle. Otherwise, fontification occurs
188 on modified lines only, and subsequent lines can remain fontified
189 corresponding to previous syntactic contexts. This is useful where
190 strings or comments span lines.
191
192Stealth fontification only occurs while the system remains unloaded.
193If the system load rises above `jit-lock-stealth-load' percent, stealth
194fontification is suspended. Stealth fontification intensity is controlled via
02b420eb 195the variable `jit-lock-stealth-nice'."
bcacade9
SM
196 (setq jit-lock-mode arg)
197 (cond (;; Turn Just-in-time Lock mode on.
198 jit-lock-mode
199
b743187d 200 ;; Mark the buffer for refontification.
a62e3c6f 201 (jit-lock-refontify)
02b420eb 202
7840ced1 203 ;; Install an idle timer for stealth fontification.
c94d5f40 204 (when (and jit-lock-stealth-time (null jit-lock-stealth-timer))
02b420eb 205 (setq jit-lock-stealth-timer
b743187d 206 (run-with-idle-timer jit-lock-stealth-time t
7840ced1
GM
207 'jit-lock-stealth-fontify)))
208
b743187d
SM
209 ;; Init deferred fontification timer.
210 (when (and jit-lock-defer-time (null jit-lock-defer-timer))
211 (setq jit-lock-defer-timer
212 (run-with-idle-timer jit-lock-defer-time t
213 'jit-lock-deferred-fontify)))
214
bcacade9 215 ;; Initialize deferred contextual fontification if requested.
a62e3c6f 216 (when (eq jit-lock-defer-contextually t)
c94d5f40
SM
217 (setq jit-lock-first-unfontify-pos
218 (or jit-lock-first-unfontify-pos (point-max))))
bcacade9 219
a62e3c6f 220 ;; Setup our hooks.
bcacade9 221 (add-hook 'after-change-functions 'jit-lock-after-change nil t)
7840ced1
GM
222 (add-hook 'fontification-functions 'jit-lock-function))
223
224 ;; Turn Just-in-time Lock mode off.
225 (t
b743187d
SM
226 ;; Cancel our idle timers.
227 (when (and (or jit-lock-stealth-timer jit-lock-defer-timer)
228 ;; Only if there's no other buffer using them.
229 (not (catch 'found
230 (dolist (buf (buffer-list))
231 (with-current-buffer buf
232 (when jit-lock-mode (throw 'found t)))))))
233 (when jit-lock-stealth-timer
234 (cancel-timer jit-lock-stealth-timer)
235 (setq jit-lock-stealth-timer nil))
236 (when jit-lock-defer-timer
237 (cancel-timer jit-lock-defer-timer)
238 (setq jit-lock-defer-timer nil)))
7840ced1 239
a62e3c6f 240 ;; Remove hooks.
02b420eb 241 (remove-hook 'after-change-functions 'jit-lock-after-change t)
7840ced1
GM
242 (remove-hook 'fontification-functions 'jit-lock-function))))
243
c94d5f40
SM
244;;;###autoload
245(defun jit-lock-register (fun &optional contextual)
8a677d4f
SM
246 "Register FUN as a fontification function to be called in this buffer.
247FUN will be called with two arguments START and END indicating the region
c94d5f40
SM
248that needs to be (re)fontified.
249If non-nil, CONTEXTUAL means that a contextual fontification would be useful."
f8bacc70 250 (add-hook 'jit-lock-functions fun nil t)
c94d5f40
SM
251 (when (and contextual jit-lock-defer-contextually)
252 (set (make-local-variable 'jit-lock-defer-contextually) t))
f8bacc70
SM
253 (jit-lock-mode t))
254
255(defun jit-lock-unregister (fun)
8a677d4f 256 "Unregister FUN as a fontification function.
f8bacc70
SM
257Only applies to the current buffer."
258 (remove-hook 'jit-lock-functions fun t)
a62e3c6f 259 (unless jit-lock-functions (jit-lock-mode nil)))
7840ced1 260
02b420eb
SM
261;; This function is used to prevent font-lock-fontify-buffer from
262;; fontifying eagerly the whole buffer. This is important for
263;; things like CWarn mode which adds/removes a few keywords and
264;; does a refontify (which takes ages on large files).
a62e3c6f
SM
265(defun jit-lock-refontify (&optional beg end)
266 "Force refontification of the region BEG..END (default whole buffer)."
bcacade9 267 (with-buffer-prepared-for-jit-lock
5a5987eb
SM
268 (save-restriction
269 (widen)
b743187d
SM
270 (put-text-property (or beg (point-min)) (or end (point-max))
271 'fontified nil))))
7840ced1
GM
272\f
273;;; On demand fontification.
274
275(defun jit-lock-function (start)
276 "Fontify current buffer starting at position START.
277This function is added to `fontification-functions' when `jit-lock-mode'
278is active."
279 (when jit-lock-mode
b743187d
SM
280 (if (null jit-lock-defer-time)
281 ;; No deferral.
282 (jit-lock-fontify-now start (+ start jit-lock-chunk-size))
283 ;; Record the buffer for later fontification.
284 (unless (memq (current-buffer) jit-lock-buffers)
285 (push (current-buffer) jit-lock-buffers))
286 ;; Mark the area as defer-fontified so that the redisplay engine
287 ;; is happy and so that the idle timer can find the places to fontify.
288 (with-buffer-prepared-for-jit-lock
289 (put-text-property start
290 (next-single-property-change
291 start 'fontified nil
292 (min (point-max) (+ start jit-lock-chunk-size)))
293 'fontified 'defer)))))
a62e3c6f
SM
294
295(defun jit-lock-fontify-now (&optional start end)
296 "Fontify current buffer from START to END.
297Defaults to the whole buffer. END can be out of bounds."
bcacade9 298 (with-buffer-prepared-for-jit-lock
60bffb78
GM
299 (save-excursion
300 (save-restriction
301 (widen)
a62e3c6f
SM
302 (unless start (setq start (point-min)))
303 (setq end (if end (min end (point-max)) (point-max)))
9724173b
GM
304 ;; This did bind `font-lock-beginning-of-syntax-function' to
305 ;; nil at some point, for an unknown reason. Don't do this; it
306 ;; can make highlighting slow due to expensive calls to
307 ;; `parse-partial-sexp' in function
308 ;; `font-lock-fontify-syntactically-region'. Example: paging
309 ;; from the end of a buffer to its start, can do repeated
310 ;; `parse-partial-sexp' starting from `point-min', which can
311 ;; take a long time in a large buffer.
312 (let (next)
60bffb78 313 (save-match-data
be390cb3
SM
314 ;; Fontify chunks beginning at START. The end of a
315 ;; chunk is either `end', or the start of a region
316 ;; before `end' that has already been fontified.
317 (while start
318 ;; Determine the end of this chunk.
319 (setq next (or (text-property-any start end 'fontified t)
320 end))
321
322 ;; Decide which range of text should be fontified.
323 ;; The problem is that START and NEXT may be in the
324 ;; middle of something matched by a font-lock regexp.
325 ;; Until someone has a better idea, let's start
326 ;; at the start of the line containing START and
327 ;; stop at the start of the line following NEXT.
a62e3c6f
SM
328 (goto-char next) (setq next (line-beginning-position 2))
329 (goto-char start) (setq start (line-beginning-position))
330
be390cb3
SM
331 ;; Fontify the chunk, and mark it as fontified.
332 ;; We mark it first, to make sure that we don't indefinitely
333 ;; re-execute this fontification if an error occurs.
b743187d 334 (put-text-property start next 'fontified t)
a62e3c6f 335 (run-hook-with-args 'jit-lock-functions start next)
b743187d 336
be390cb3
SM
337 ;; Find the start of the next chunk, if any.
338 (setq start (text-property-any next end 'fontified nil)))))))))
7840ced1 339
7840ced1
GM
340\f
341;;; Stealth fontification.
342
343(defsubst jit-lock-stealth-chunk-start (around)
344 "Return the start of the next chunk to fontify around position AROUND..
345Value is nil if there is nothing more to fontify."
8c887c51
GM
346 (if (zerop (buffer-size))
347 nil
348 (save-restriction
349 (widen)
b743187d 350 (let* ((next (text-property-not-all around (point-max) 'fontified t))
8c887c51
GM
351 (prev (previous-single-property-change around 'fontified))
352 (prop (get-text-property (max (point-min) (1- around))
353 'fontified))
354 (start (cond
355 ((null prev)
356 ;; There is no property change between AROUND
357 ;; and the start of the buffer. If PROP is
358 ;; non-nil, everything in front of AROUND is
359 ;; fontified, otherwise nothing is fontified.
b743187d 360 (if (eq prop t)
8c887c51
GM
361 nil
362 (max (point-min)
363 (- around (/ jit-lock-chunk-size 2)))))
b743187d 364 ((eq prop t)
8c887c51 365 ;; PREV is the start of a region of fontified
bcacade9 366 ;; text containing AROUND. Start fontifying a
8c887c51
GM
367 ;; chunk size before the end of the unfontified
368 ;; region in front of that.
369 (max (or (previous-single-property-change prev 'fontified)
370 (point-min))
371 (- prev jit-lock-chunk-size)))
372 (t
373 ;; PREV is the start of a region of unfontified
374 ;; text containing AROUND. Start at PREV or
375 ;; chunk size in front of AROUND, whichever is
376 ;; nearer.
377 (max prev (- around jit-lock-chunk-size)))))
378 (result (cond ((null start) next)
379 ((null next) start)
380 ((< (- around start) (- next around)) start)
381 (t next))))
382 result))))
383
7840ced1
GM
384
385(defun jit-lock-stealth-fontify ()
386 "Fontify buffers stealthily.
387This functions is called after Emacs has been idle for
388`jit-lock-stealth-time' seconds."
b743187d 389 ;; I used to check `inhibit-read-only' here, but I can't remember why. -stef
7840ced1
GM
390 (unless (or executing-kbd-macro
391 (window-minibuffer-p (selected-window)))
392 (let ((buffers (buffer-list))
393 minibuffer-auto-raise
394 message-log-max)
8c887c51 395 (while (and buffers (not (input-pending-p)))
7840ced1
GM
396 (let ((buffer (car buffers)))
397 (setq buffers (cdr buffers))
8c887c51 398
7840ced1
GM
399 (with-current-buffer buffer
400 (when jit-lock-mode
401 ;; This is funny. Calling sit-for with 3rd arg non-nil
402 ;; so that it doesn't redisplay, internally calls
403 ;; wait_reading_process_input also with a parameter
404 ;; saying "don't redisplay." Since this function here
405 ;; is called periodically, this effectively leads to
406 ;; process output not being redisplayed at all because
407 ;; redisplay_internal is never called. (That didn't
408 ;; work in the old redisplay either.) So, we learn that
409 ;; we mustn't call sit-for that way here. But then, we
410 ;; have to be cautious not to call sit-for in a widened
411 ;; buffer, since this could display hidden parts of that
412 ;; buffer. This explains the seemingly weird use of
413 ;; save-restriction/widen here.
414
415 (with-temp-message (if jit-lock-stealth-verbose
416 (concat "JIT stealth lock "
417 (buffer-name)))
8c887c51 418
9f1a8fb4
GM
419 ;; Perform deferred unfontification, if any.
420 (when jit-lock-first-unfontify-pos
421 (save-restriction
422 (widen)
423 (when (and (>= jit-lock-first-unfontify-pos (point-min))
424 (< jit-lock-first-unfontify-pos (point-max)))
b743187d
SM
425 ;; If we're in text that matches a complex multi-line
426 ;; font-lock pattern, make sure the whole text will be
427 ;; redisplayed eventually.
428 (when (get-text-property jit-lock-first-unfontify-pos
429 'jit-lock-defer-multiline)
430 (setq jit-lock-first-unfontify-pos
431 (or (previous-single-property-change
432 jit-lock-first-unfontify-pos
433 'jit-lock-defer-multiline)
434 (point-min))))
bcacade9 435 (with-buffer-prepared-for-jit-lock
b743187d
SM
436 (remove-text-properties
437 jit-lock-first-unfontify-pos (point-max)
438 '(fontified nil jit-lock-defer-multiline nil)))
bcacade9 439 (setq jit-lock-first-unfontify-pos (point-max)))))
9f1a8fb4
GM
440
441 ;; In the following code, the `sit-for' calls cause a
442 ;; redisplay, so it's required that the
443 ;; buffer-modified flag of a buffer that is displayed
444 ;; has the right value---otherwise the mode line of
445 ;; an unmodified buffer would show a `*'.
446 (let (start
447 (nice (or jit-lock-stealth-nice 0))
b743187d 448 (point (point-min)))
9f1a8fb4
GM
449 (while (and (setq start
450 (jit-lock-stealth-chunk-start point))
451 (sit-for nice))
7840ced1 452
b743187d
SM
453 ;; fontify a block.
454 (jit-lock-fontify-now start (+ start jit-lock-chunk-size))
455 ;; If stealth jit-locking is done backwards, this leads to
456 ;; excessive O(n^2) refontification. -stef
457 ;; (when (>= jit-lock-first-unfontify-pos start)
458 ;; (setq jit-lock-first-unfontify-pos end))
459
9f1a8fb4
GM
460 ;; Wait a little if load is too high.
461 (when (and jit-lock-stealth-load
462 (> (car (load-average)) jit-lock-stealth-load))
b743187d 463 (sit-for (or jit-lock-stealth-time 30)))))))))))))
7840ced1
GM
464
465
466\f
467;;; Deferred fontification.
468
b743187d
SM
469(defun jit-lock-deferred-fontify ()
470 "Fontify what was deferred."
471 (when jit-lock-buffers
472 ;; Mark the deferred regions back to `fontified = nil'
473 (dolist (buffer jit-lock-buffers)
474 (when (buffer-live-p buffer)
475 (with-current-buffer buffer
476 ;; (message "Jit-Defer %s" (buffer-name))
477 (with-buffer-prepared-for-jit-lock
478 (let ((pos (point-min)))
479 (while
480 (progn
481 (when (eq (get-text-property pos 'fontified) 'defer)
482 (put-text-property
483 pos (setq pos (next-single-property-change
484 pos 'fontified nil (point-max)))
485 'fontified nil))
486 (setq pos (next-single-property-change pos 'fontified)))))))))
487 (setq jit-lock-buffers nil)
488 ;; Force fontification of the visible parts.
489 (let ((jit-lock-defer-time nil))
490 ;; (message "Jit-Defer Now")
491 (sit-for 0)
492 ;; (message "Jit-Defer Done")
493 )))
494
495
7840ced1
GM
496(defun jit-lock-after-change (start end old-len)
497 "Mark the rest of the buffer as not fontified after a change.
498Installed on `after-change-functions'.
499START and END are the start and end of the changed text. OLD-LEN
500is the pre-change length.
501This function ensures that lines following the change will be refontified
502in case the syntax of those lines has changed. Refontification
503will take place when text is fontified stealthily."
7840ced1 504 (when jit-lock-mode
d9330f43
SM
505 (save-excursion
506 (with-buffer-prepared-for-jit-lock
507 ;; It's important that the `fontified' property be set from the
508 ;; beginning of the line, else font-lock will properly change the
509 ;; text's face, but the display will have been done already and will
510 ;; be inconsistent with the buffer's content.
511 (goto-char start)
512 (setq start (line-beginning-position))
7a1e3cec 513
df22166e
SM
514 ;; If we're in text that matches a multi-line font-lock pattern,
515 ;; make sure the whole text will be redisplayed.
b743187d 516 ;; I'm not sure this is ever necessary and/or sufficient. -stef
df22166e
SM
517 (when (get-text-property start 'font-lock-multiline)
518 (setq start (or (previous-single-property-change
519 start 'font-lock-multiline)
520 (point-min))))
521
d9330f43
SM
522 ;; Make sure we change at least one char (in case of deletions).
523 (setq end (min (max end (1+ start)) (point-max)))
524 ;; Request refontification.
525 (put-text-property start end 'fontified nil))
526 ;; Mark the change for deferred contextual refontification.
527 (when jit-lock-first-unfontify-pos
528 (setq jit-lock-first-unfontify-pos
529 (min jit-lock-first-unfontify-pos start))))))
7840ced1 530
7840ced1
GM
531(provide 'jit-lock)
532
e8af40ee 533;;; jit-lock.el ends here