Commit | Line | Data |
---|---|---|
7840ced1 GM |
1 | ;;; jit-lock.el --- just-in-time fontification. |
2 | ||
3 | ;; Copyright (C) 1998 Free Software Foundation, Inc. | |
4 | ||
5 | ;; Author: Gerd Moellmann <gerd@gnu.org> | |
6 | ;; Keywords: faces files | |
7 | ;; Version: 1.0 | |
8 | ||
9 | ;; This file is part of GNU Emacs. | |
10 | ||
11 | ;; GNU Emacs is free software; you can redistribute it and/or modify | |
12 | ;; it under the terms of the GNU General Public License as published by | |
13 | ;; the Free Software Foundation; either version 2, or (at your option) | |
14 | ;; any later version. | |
15 | ||
16 | ;; GNU Emacs is distributed in the hope that it will be useful, | |
17 | ;; but WITHOUT ANY WARRANTY; without even the implied warranty of | |
18 | ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
19 | ;; GNU General Public License for more details. | |
20 | ||
21 | ;; You should have received a copy of the GNU General Public License | |
22 | ;; along with GNU Emacs; see the file COPYING. If not, write to the | |
23 | ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330, | |
24 | ;; Boston, MA 02111-1307, USA. | |
25 | ||
26 | ;;; Commentary: | |
27 | ||
28 | ;; Just-in-time fontification, triggered by C redisplay code. | |
29 | ||
30 | ;;; Code: | |
31 | ||
32 | ||
33 | (require 'font-lock) | |
34 | ||
35 | (eval-when-compile | |
60bffb78 GM |
36 | (defmacro with-buffer-unmodified (&rest body) |
37 | "Eval BODY, preserving the current buffer's modified state." | |
38 | (let ((modified (make-symbol "modified"))) | |
39 | `(let ((,modified (buffer-modified-p))) | |
40 | ,@body | |
41 | (unless ,modified) | |
42 | ;; Calling set-buffer-modified causes redisplay to consider | |
43 | ;; all windows because that function sets update_mode_lines. | |
44 | (set-buffer-modified-p nil)))) | |
45 | ||
7840ced1 GM |
46 | (defmacro with-buffer-prepared-for-font-lock (&rest body) |
47 | "Execute BODY in current buffer, overriding several variables. | |
48 | Preserves the `buffer-modified-p' state of the current buffer." | |
60bffb78 | 49 | `(let ((buffer-undo-list t) |
7840ced1 GM |
50 | (inhibit-read-only t) |
51 | (inhibit-point-motion-hooks t) | |
52 | before-change-functions | |
53 | after-change-functions | |
54 | deactivate-mark | |
55 | buffer-file-name | |
56 | buffer-file-truename) | |
60bffb78 | 57 | ,@body))) |
7840ced1 | 58 | |
60bffb78 | 59 | |
7840ced1 GM |
60 | \f |
61 | ;;; Customization. | |
62 | ||
63 | (defcustom jit-lock-chunk-size 500 | |
64 | "*Font-lock chunks of this many characters, or smaller." | |
65 | :type 'integer | |
66 | :group 'jit-lock) | |
67 | ||
68 | ||
69 | (defcustom jit-lock-stealth-time 3 | |
70 | "*Time in seconds to wait before beginning stealth fontification. | |
71 | Stealth fontification occurs if there is no input within this time. | |
72 | If nil, means stealth fontification is never performed. | |
73 | ||
74 | The value of this variable is used when JIT Lock mode is turned on." | |
75 | :type '(choice (const :tag "never" nil) | |
76 | (number :tag "seconds")) | |
77 | :group 'jit-lock) | |
78 | ||
79 | ||
80 | (defcustom jit-lock-stealth-nice 0.125 | |
81 | "*Time in seconds to pause between chunks of stealth fontification. | |
82 | Each iteration of stealth fontification is separated by this amount of time, | |
83 | thus reducing the demand that stealth fontification makes on the system. | |
84 | If nil, means stealth fontification is never paused. | |
85 | To reduce machine load during stealth fontification, at the cost of stealth | |
86 | taking longer to fontify, you could increase the value of this variable. | |
87 | See also `jit-lock-stealth-load'." | |
88 | :type '(choice (const :tag "never" nil) | |
89 | (number :tag "seconds")) | |
90 | :group 'jit-lock) | |
91 | ||
92 | ||
93 | (defcustom jit-lock-stealth-load | |
94 | (if (condition-case nil (load-average) (error)) 200) | |
95 | "*Load in percentage above which stealth fontification is suspended. | |
96 | Stealth fontification pauses when the system short-term load average (as | |
97 | returned by the function `load-average' if supported) goes above this level, | |
98 | thus reducing the demand that stealth fontification makes on the system. | |
99 | If nil, means stealth fontification is never suspended. | |
100 | To reduce machine load during stealth fontification, at the cost of stealth | |
101 | taking longer to fontify, you could reduce the value of this variable. | |
102 | See also `jit-lock-stealth-nice'." | |
103 | :type (if (condition-case nil (load-average) (error)) | |
104 | '(choice (const :tag "never" nil) | |
105 | (integer :tag "load")) | |
106 | '(const :format "%t: unsupported\n" nil)) | |
107 | :group 'jit-lock) | |
108 | ||
109 | ||
110 | (defcustom jit-lock-stealth-verbose nil | |
111 | "*If non-nil, means stealth fontification should show status messages." | |
112 | :type 'boolean | |
113 | :group 'jit-lock) | |
114 | ||
115 | ||
116 | (defcustom jit-lock-defer-contextually 'syntax-driven | |
117 | "*If non-nil, means deferred fontification should be syntactically true. | |
118 | If nil, means deferred fontification occurs only on those lines modified. This | |
119 | means where modification on a line causes syntactic change on subsequent lines, | |
120 | those subsequent lines are not refontified to reflect their new context. | |
121 | If t, means deferred fontification occurs on those lines modified and all | |
122 | subsequent lines. This means those subsequent lines are refontified to reflect | |
123 | their new syntactic context, either immediately or when scrolling into them. | |
124 | If any other value, e.g., `syntax-driven', means deferred syntactically true | |
125 | fontification occurs only if syntactic fontification is performed using the | |
126 | buffer mode's syntax table, i.e., only if `font-lock-keywords-only' is nil. | |
127 | ||
128 | The value of this variable is used when JIT Lock mode is turned on." | |
129 | :type '(choice (const :tag "never" nil) | |
130 | (const :tag "always" t) | |
131 | (other :tag "syntax-driven" syntax-driven)) | |
132 | :group 'jit-lock) | |
133 | ||
134 | ||
135 | \f | |
136 | ;;; Variables that are not customizable. | |
137 | ||
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) | |
141 | ||
142 | ||
143 | (defvar jit-lock-first-unfontify-pos nil | |
144 | "Consider text after this position as unfontified.") | |
145 | (make-variable-buffer-local 'jit-lock-first-unfontify-pos) | |
146 | ||
147 | ||
148 | (defvar jit-lock-stealth-timer nil | |
149 | "Timer for stealth fontification in Just-in-time Lock mode.") | |
150 | ||
151 | ||
152 | \f | |
153 | ;;; JIT lock mode | |
154 | ||
155 | ;;;###autoload | |
156 | (defun jit-lock-mode (arg) | |
157 | "Toggle Just-in-time Lock mode. | |
158 | With arg, turn Just-in-time Lock mode on if and only if arg is positive. | |
159 | Enable it automatically by customizing group `font-lock'. | |
160 | ||
161 | When Just-in-time Lock mode is enabled, fontification is different in the | |
162 | following ways: | |
163 | ||
164 | - Demand-driven buffer fontification triggered by Emacs C code. | |
165 | This means initial fontification of the whole buffer does not occur. | |
166 | Instead, fontification occurs when necessary, such as when scrolling | |
167 | through the buffer would otherwise reveal unfontified areas. This is | |
168 | useful if buffer fontification is too slow for large buffers. | |
169 | ||
170 | - Stealthy buffer fontification if `jit-lock-stealth-time' is non-nil. | |
171 | This means remaining unfontified areas of buffers are fontified if Emacs has | |
172 | been idle for `jit-lock-stealth-time' seconds, while Emacs remains idle. | |
173 | This is useful if any buffer has any deferred fontification. | |
174 | ||
175 | - Deferred context fontification if `jit-lock-defer-contextually' is | |
176 | non-nil. This means fontification updates the buffer corresponding to | |
177 | true syntactic context, after `jit-lock-stealth-time' seconds of Emacs | |
178 | idle time, while Emacs remains idle. Otherwise, fontification occurs | |
179 | on modified lines only, and subsequent lines can remain fontified | |
180 | corresponding to previous syntactic contexts. This is useful where | |
181 | strings or comments span lines. | |
182 | ||
183 | Stealth fontification only occurs while the system remains unloaded. | |
184 | If the system load rises above `jit-lock-stealth-load' percent, stealth | |
185 | fontification is suspended. Stealth fontification intensity is controlled via | |
186 | the variable `jit-lock-stealth-nice' and `jit-lock-stealth-lines'." | |
187 | (interactive "P") | |
188 | (setq jit-lock-mode (if arg | |
189 | (> (prefix-numeric-value arg) 0) | |
190 | (not jit-lock-mode))) | |
191 | (cond ((and jit-lock-mode | |
192 | (or (not (boundp 'font-lock-mode)) | |
193 | (not font-lock-mode))) | |
194 | ;; If font-lock is not on, turn it on, with Just-in-time | |
195 | ;; Lock mode as support mode; font-lock will call us again. | |
196 | (let ((font-lock-support-mode 'jit-lock-mode)) | |
197 | (font-lock-mode t))) | |
198 | ||
199 | ;; Turn Just-in-time Lock mode on. | |
200 | (jit-lock-mode | |
201 | ;; Setting `font-lock-fontified' makes font-lock believe the | |
202 | ;; buffer is already fontified, so that it won't highlight | |
203 | ;; the whole buffer. | |
204 | (make-local-variable 'font-lock-fontified) | |
205 | (setq font-lock-fontified t) | |
206 | ||
207 | (setq jit-lock-first-unfontify-pos nil) | |
208 | ||
209 | ;; Install an idle timer for stealth fontification. | |
210 | (when (and jit-lock-stealth-time | |
211 | (null jit-lock-stealth-timer)) | |
212 | (setq jit-lock-stealth-timer | |
213 | (run-with-idle-timer jit-lock-stealth-time | |
214 | jit-lock-stealth-time | |
215 | 'jit-lock-stealth-fontify))) | |
216 | ||
217 | ;; Add a hook for deferred contectual fontification. | |
218 | (when (or (eq jit-lock-defer-contextually 'always) | |
219 | (and (not (eq jit-lock-defer-contextually 'never)) | |
220 | (null font-lock-keywords-only))) | |
221 | (add-hook 'after-change-functions 'jit-lock-after-change)) | |
222 | ||
223 | ;; Install the fontification hook. | |
224 | (add-hook 'fontification-functions 'jit-lock-function)) | |
225 | ||
226 | ;; Turn Just-in-time Lock mode off. | |
227 | (t | |
228 | ;; Cancel our idle timer. | |
229 | (when jit-lock-stealth-timer | |
230 | (cancel-timer jit-lock-stealth-timer) | |
231 | (setq jit-lock-stealth-timer nil)) | |
232 | ||
233 | ;; Remove hooks. | |
234 | (remove-hook 'after-change-functions 'jit-lock-after-change) | |
235 | (remove-hook 'fontification-functions 'jit-lock-function)))) | |
236 | ||
237 | ||
238 | ;;;###autoload | |
239 | (defun turn-on-jit-lock () | |
240 | "Unconditionally turn on Just-in-time Lock mode." | |
241 | (jit-lock-mode 1)) | |
242 | ||
243 | ||
244 | \f | |
245 | ;;; On demand fontification. | |
246 | ||
247 | (defun jit-lock-function (start) | |
248 | "Fontify current buffer starting at position START. | |
249 | This function is added to `fontification-functions' when `jit-lock-mode' | |
250 | is active." | |
251 | (when jit-lock-mode | |
60bffb78 GM |
252 | (with-buffer-unmodified (jit-lock-function-1 start)))) |
253 | ||
254 | ||
255 | (defun jit-lock-function-1 (start) | |
256 | "Fontify current buffer starting at position START. | |
257 | This function is added to `fontification-functions' when `jit-lock-mode' | |
258 | is active." | |
259 | (with-buffer-prepared-for-font-lock | |
260 | (save-excursion | |
261 | (save-restriction | |
262 | (widen) | |
263 | (let ((end (min (point-max) (+ start jit-lock-chunk-size))) | |
264 | (parse-sexp-lookup-properties font-lock-syntactic-keywords) | |
265 | (font-lock-beginning-of-syntax-function nil) | |
266 | (old-syntax-table (syntax-table)) | |
267 | next font-lock-start font-lock-end) | |
268 | (when font-lock-syntax-table | |
269 | (set-syntax-table font-lock-syntax-table)) | |
270 | (save-match-data | |
271 | (condition-case error | |
272 | ;; Fontify chunks beginning at START. The end of a | |
273 | ;; chunk is either `end', or the start of a region | |
274 | ;; before `end' that has already been fontified. | |
275 | (while start | |
276 | ;; Determine the end of this chunk. | |
277 | (setq next (or (text-property-any start end 'fontified t) | |
278 | end)) | |
279 | ||
280 | ;; Decide which range of text should be fontified. | |
281 | ;; The problem is that START and NEXT may be in the | |
282 | ;; middle of something matched by a font-lock regexp. | |
283 | ;; Until someone has a better idea, let's start | |
284 | ;; at the start of the line containing START and | |
285 | ;; stop at the start of the line following NEXT. | |
286 | (goto-char next) | |
287 | (setq font-lock-end (line-beginning-position 2)) | |
288 | (goto-char start) | |
289 | (setq font-lock-start (line-beginning-position)) | |
7840ced1 | 290 | |
60bffb78 GM |
291 | ;; Fontify the chunk, and mark it as fontified. |
292 | (font-lock-fontify-region font-lock-start font-lock-end nil) | |
293 | (add-text-properties start next '(fontified t)) | |
7840ced1 | 294 | |
60bffb78 GM |
295 | ;; Find the start of the next chunk, if any. |
296 | (setq start (text-property-any next end 'fontified nil))) | |
7840ced1 | 297 | |
60bffb78 GM |
298 | ((error quit) |
299 | (message "Fontifying region...%s" error)))) | |
7840ced1 | 300 | |
60bffb78 GM |
301 | ;; Restore previous buffer settings. |
302 | (set-syntax-table old-syntax-table)))))) | |
7840ced1 GM |
303 | |
304 | ||
305 | (defun jit-lock-after-fontify-buffer () | |
306 | "Mark the current buffer as fontified. | |
307 | Called from `font-lock-after-fontify-buffer." | |
308 | (with-buffer-prepared-for-font-lock | |
309 | (add-text-properties (point-min) (point-max) '(fontified t)))) | |
310 | ||
311 | ||
312 | (defun jit-lock-after-unfontify-buffer () | |
313 | "Mark the current buffer as unfontified. | |
314 | Called from `font-lock-after-fontify-buffer." | |
315 | (with-buffer-prepared-for-font-lock | |
316 | (remove-text-properties (point-min) (point-max) '(fontified nil)))) | |
317 | ||
318 | ||
319 | \f | |
320 | ;;; Stealth fontification. | |
321 | ||
322 | (defsubst jit-lock-stealth-chunk-start (around) | |
323 | "Return the start of the next chunk to fontify around position AROUND.. | |
324 | Value is nil if there is nothing more to fontify." | |
8c887c51 GM |
325 | (if (zerop (buffer-size)) |
326 | nil | |
327 | (save-restriction | |
328 | (widen) | |
329 | (let* ((next (text-property-any around (point-max) 'fontified nil)) | |
330 | (prev (previous-single-property-change around 'fontified)) | |
331 | (prop (get-text-property (max (point-min) (1- around)) | |
332 | 'fontified)) | |
333 | (start (cond | |
334 | ((null prev) | |
335 | ;; There is no property change between AROUND | |
336 | ;; and the start of the buffer. If PROP is | |
337 | ;; non-nil, everything in front of AROUND is | |
338 | ;; fontified, otherwise nothing is fontified. | |
339 | (if prop | |
340 | nil | |
341 | (max (point-min) | |
342 | (- around (/ jit-lock-chunk-size 2))))) | |
343 | (prop | |
344 | ;; PREV is the start of a region of fontified | |
345 | ;; text containing AROUND. Start fontfifying a | |
346 | ;; chunk size before the end of the unfontified | |
347 | ;; region in front of that. | |
348 | (max (or (previous-single-property-change prev 'fontified) | |
349 | (point-min)) | |
350 | (- prev jit-lock-chunk-size))) | |
351 | (t | |
352 | ;; PREV is the start of a region of unfontified | |
353 | ;; text containing AROUND. Start at PREV or | |
354 | ;; chunk size in front of AROUND, whichever is | |
355 | ;; nearer. | |
356 | (max prev (- around jit-lock-chunk-size))))) | |
357 | (result (cond ((null start) next) | |
358 | ((null next) start) | |
359 | ((< (- around start) (- next around)) start) | |
360 | (t next)))) | |
361 | result)))) | |
362 | ||
7840ced1 GM |
363 | |
364 | (defun jit-lock-stealth-fontify () | |
365 | "Fontify buffers stealthily. | |
366 | This functions is called after Emacs has been idle for | |
367 | `jit-lock-stealth-time' seconds." | |
368 | (unless (or executing-kbd-macro | |
369 | (window-minibuffer-p (selected-window))) | |
370 | (let ((buffers (buffer-list)) | |
371 | minibuffer-auto-raise | |
372 | message-log-max) | |
8c887c51 | 373 | (while (and buffers (not (input-pending-p))) |
7840ced1 GM |
374 | (let ((buffer (car buffers))) |
375 | (setq buffers (cdr buffers)) | |
8c887c51 | 376 | |
7840ced1 GM |
377 | (with-current-buffer buffer |
378 | (when jit-lock-mode | |
379 | ;; This is funny. Calling sit-for with 3rd arg non-nil | |
380 | ;; so that it doesn't redisplay, internally calls | |
381 | ;; wait_reading_process_input also with a parameter | |
382 | ;; saying "don't redisplay." Since this function here | |
383 | ;; is called periodically, this effectively leads to | |
384 | ;; process output not being redisplayed at all because | |
385 | ;; redisplay_internal is never called. (That didn't | |
386 | ;; work in the old redisplay either.) So, we learn that | |
387 | ;; we mustn't call sit-for that way here. But then, we | |
388 | ;; have to be cautious not to call sit-for in a widened | |
389 | ;; buffer, since this could display hidden parts of that | |
390 | ;; buffer. This explains the seemingly weird use of | |
391 | ;; save-restriction/widen here. | |
392 | ||
393 | (with-temp-message (if jit-lock-stealth-verbose | |
394 | (concat "JIT stealth lock " | |
395 | (buffer-name))) | |
8c887c51 | 396 | |
60bffb78 GM |
397 | (with-buffer-unmodified |
398 | ||
399 | ;; Perform deferred unfontification, if any. | |
400 | (when jit-lock-first-unfontify-pos | |
401 | (save-restriction | |
402 | (widen) | |
403 | (when (and (>= jit-lock-first-unfontify-pos (point-min)) | |
404 | (< jit-lock-first-unfontify-pos (point-max))) | |
405 | (with-buffer-prepared-for-font-lock | |
406 | (put-text-property jit-lock-first-unfontify-pos | |
407 | (point-max) 'fontified nil)) | |
408 | (setq jit-lock-first-unfontify-pos nil)))) | |
7840ced1 | 409 | |
60bffb78 GM |
410 | (let (start |
411 | (nice (or jit-lock-stealth-nice 0)) | |
412 | (point (point))) | |
413 | (while (and (setq start (jit-lock-stealth-chunk-start point)) | |
414 | (sit-for nice)) | |
7840ced1 | 415 | |
60bffb78 GM |
416 | ;; Wait a little if load is too high. |
417 | (when (and jit-lock-stealth-load | |
418 | (> (car (load-average)) jit-lock-stealth-load)) | |
419 | (sit-for (or jit-lock-stealth-time 30))) | |
7840ced1 | 420 | |
60bffb78 GM |
421 | ;; Unless there's input pending now, fontify. |
422 | (unless (input-pending-p) | |
423 | (jit-lock-function-1 start))))))))))))) | |
7840ced1 GM |
424 | |
425 | ||
426 | \f | |
427 | ;;; Deferred fontification. | |
428 | ||
429 | (defun jit-lock-after-change (start end old-len) | |
430 | "Mark the rest of the buffer as not fontified after a change. | |
431 | Installed on `after-change-functions'. | |
432 | START and END are the start and end of the changed text. OLD-LEN | |
433 | is the pre-change length. | |
434 | This function ensures that lines following the change will be refontified | |
435 | in case the syntax of those lines has changed. Refontification | |
436 | will take place when text is fontified stealthily." | |
437 | ;; Don't do much here---removing text properties is too slow for | |
438 | ;; fast typers, giving them the impression of Emacs not being | |
439 | ;; very responsive. | |
440 | (when jit-lock-mode | |
441 | (setq jit-lock-first-unfontify-pos | |
442 | (if jit-lock-first-unfontify-pos | |
443 | (min jit-lock-first-unfontify-pos start) | |
444 | start)))) | |
445 | ||
446 | ||
447 | (provide 'jit-lock) | |
448 | ||
449 | ;; jit-lock.el ends here |