Commit | Line | Data |
---|---|---|
e8af40ee | 1 | ;;; jit-lock.el --- just-in-time fontification |
7840ced1 | 2 | |
0d30b337 | 3 | ;; Copyright (C) 1998, 2000, 2001, 2002, 2003, 2004, |
5df4f04c | 4 | ;; 2005, 2006, 2007, 2008, 2009, 2010, 2011 Free Software Foundation, Inc. |
7840ced1 GM |
5 | |
6 | ;; Author: Gerd Moellmann <gerd@gnu.org> | |
7 | ;; Keywords: faces files | |
bd78fa1d | 8 | ;; Package: emacs |
7840ced1 GM |
9 | |
10 | ;; This file is part of GNU Emacs. | |
11 | ||
eb3fa2cf | 12 | ;; GNU Emacs is free software: you can redistribute it and/or modify |
7840ced1 | 13 | ;; it under the terms of the GNU General Public License as published by |
eb3fa2cf GM |
14 | ;; the Free Software Foundation, either version 3 of the License, or |
15 | ;; (at your option) any later version. | |
7840ced1 GM |
16 | |
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. | |
21 | ||
22 | ;; You should have received a copy of the GNU General Public License | |
eb3fa2cf | 23 | ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. |
7840ced1 GM |
24 | |
25 | ;;; Commentary: | |
26 | ||
27 | ;; Just-in-time fontification, triggered by C redisplay code. | |
28 | ||
29 | ;;; Code: | |
30 | ||
31 | ||
7840ced1 | 32 | (eval-when-compile |
6b2fcbb5 SM |
33 | (require 'cl) |
34 | ||
bcacade9 | 35 | (defmacro with-buffer-prepared-for-jit-lock (&rest body) |
7840ced1 GM |
36 | "Execute BODY in current buffer, overriding several variables. |
37 | Preserves the `buffer-modified-p' state of the current buffer." | |
7b4d9d3b | 38 | (declare (debug t)) |
d36b74ca SM |
39 | `(let ((inhibit-point-motion-hooks t)) |
40 | (with-silent-modifications | |
41 | ,@body)))) | |
7840ced1 GM |
42 | \f |
43 | ;;; Customization. | |
44 | ||
623a1226 SM |
45 | (defgroup jit-lock nil |
46 | "Font Lock support mode to fontify just-in-time." | |
623a1226 SM |
47 | :version "21.1" |
48 | :group 'font-lock) | |
49 | ||
7840ced1 | 50 | (defcustom jit-lock-chunk-size 500 |
9201cc28 | 51 | "Jit-lock fontifies chunks of at most this many characters at a time. |
45cb4c96 EZ |
52 | |
53 | This variable controls both display-time and stealth fontification." | |
7840ced1 GM |
54 | :type 'integer |
55 | :group 'jit-lock) | |
56 | ||
57 | ||
d0483d25 | 58 | (defcustom jit-lock-stealth-time nil |
9201cc28 | 59 | "Time in seconds to wait before beginning stealth fontification. |
7840ced1 | 60 | Stealth fontification occurs if there is no input within this time. |
f86292a9 | 61 | If nil, stealth fontification is never performed. |
7840ced1 GM |
62 | |
63 | The value of this variable is used when JIT Lock mode is turned on." | |
64 | :type '(choice (const :tag "never" nil) | |
d0483d25 | 65 | (number :tag "seconds" :value 16)) |
7840ced1 GM |
66 | :group 'jit-lock) |
67 | ||
68 | ||
4739237d | 69 | (defcustom jit-lock-stealth-nice 0.5 |
9201cc28 | 70 | "Time in seconds to pause between chunks of stealth fontification. |
7840ced1 GM |
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) | |
f1180544 | 78 | (number :tag "seconds")) |
7840ced1 | 79 | :group 'jit-lock) |
f1180544 | 80 | |
7840ced1 GM |
81 | |
82 | (defcustom jit-lock-stealth-load | |
83 | (if (condition-case nil (load-average) (error)) 200) | |
9201cc28 | 84 | "Load in percentage above which stealth fontification is suspended. |
7840ced1 GM |
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)) | |
96 | :group 'jit-lock) | |
97 | ||
98 | ||
99 | (defcustom jit-lock-stealth-verbose nil | |
9201cc28 | 100 | "If non-nil, means stealth fontification should show status messages." |
7840ced1 GM |
101 | :type 'boolean |
102 | :group 'jit-lock) | |
103 | ||
104 | ||
f415f2d7 SM |
105 | (defvaralias 'jit-lock-defer-contextually 'jit-lock-contextually) |
106 | (defcustom jit-lock-contextually 'syntax-driven | |
9201cc28 | 107 | "If non-nil, means fontification should be syntactically true. |
f415f2d7 | 108 | If nil, means fontification occurs only on those lines modified. This |
7840ced1 GM |
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. | |
f415f2d7 | 111 | If t, means fontification occurs on those lines modified and all |
7840ced1 | 112 | subsequent lines. This means those subsequent lines are refontified to reflect |
7b4d9d3b | 113 | their new syntactic context, after `jit-lock-context-time' seconds. |
f415f2d7 | 114 | If any other value, e.g., `syntax-driven', means syntactically true |
7840ced1 GM |
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. | |
117 | ||
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)) | |
122 | :group 'jit-lock) | |
123 | ||
7b4d9d3b SM |
124 | (defcustom jit-lock-context-time 0.5 |
125 | "Idle time after which text is contextually refontified, if applicable." | |
f5307782 JB |
126 | :type '(number :tag "seconds") |
127 | :group 'jit-lock) | |
128 | ||
8e069ce2 | 129 | (defcustom jit-lock-defer-time nil ;; 0.25 |
b743187d SM |
130 | "Idle time after which deferred fontification should take place. |
131 | If nil, fontification is not deferred." | |
132 | :group 'jit-lock | |
133 | :type '(choice (const :tag "never" nil) | |
134 | (number :tag "seconds"))) | |
7840ced1 GM |
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 | ||
be390cb3 SM |
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.") | |
a62e3c6f | 145 | (make-variable-buffer-local 'jit-lock-functions) |
7840ced1 | 146 | |
623a1226 | 147 | (defvar jit-lock-context-unfontify-pos nil |
a62e3c6f | 148 | "Consider text after this position as contextually unfontified. |
bcacade9 | 149 | If nil, contextual fontification is disabled.") |
623a1226 | 150 | (make-variable-buffer-local 'jit-lock-context-unfontify-pos) |
7840ced1 GM |
151 | |
152 | ||
153 | (defvar jit-lock-stealth-timer nil | |
154 | "Timer for stealth fontification in Just-in-time Lock mode.") | |
1063efe8 CY |
155 | (defvar jit-lock-stealth-repeat-timer nil |
156 | "Timer for repeated stealth fontification in Just-in-time Lock mode.") | |
7b4d9d3b SM |
157 | (defvar jit-lock-context-timer nil |
158 | "Timer for context fontification in Just-in-time Lock mode.") | |
b743187d SM |
159 | (defvar jit-lock-defer-timer nil |
160 | "Timer for deferred fontification in Just-in-time Lock mode.") | |
161 | ||
623a1226 | 162 | (defvar jit-lock-defer-buffers nil |
b743187d | 163 | "List of buffers with pending deferred fontification.") |
1063efe8 CY |
164 | (defvar jit-lock-stealth-buffers nil |
165 | "List of buffers that are being fontified stealthily.") | |
7840ced1 GM |
166 | \f |
167 | ;;; JIT lock mode | |
168 | ||
7840ced1 GM |
169 | (defun jit-lock-mode (arg) |
170 | "Toggle Just-in-time Lock mode. | |
bcacade9 | 171 | Turn Just-in-time Lock mode on if and only if ARG is non-nil. |
7840ced1 GM |
172 | Enable it automatically by customizing group `font-lock'. |
173 | ||
174 | When Just-in-time Lock mode is enabled, fontification is different in the | |
175 | following ways: | |
176 | ||
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. | |
182 | ||
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. | |
187 | ||
f415f2d7 | 188 | - Deferred context fontification if `jit-lock-contextually' is |
7840ced1 | 189 | non-nil. This means fontification updates the buffer corresponding to |
7b4d9d3b | 190 | true syntactic context, after `jit-lock-context-time' seconds of Emacs |
7840ced1 GM |
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. | |
195 | ||
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 | |
02b420eb | 199 | the variable `jit-lock-stealth-nice'." |
bcacade9 SM |
200 | (setq jit-lock-mode arg) |
201 | (cond (;; Turn Just-in-time Lock mode on. | |
202 | jit-lock-mode | |
203 | ||
b743187d | 204 | ;; Mark the buffer for refontification. |
a62e3c6f | 205 | (jit-lock-refontify) |
02b420eb | 206 | |
7840ced1 | 207 | ;; Install an idle timer for stealth fontification. |
c94d5f40 | 208 | (when (and jit-lock-stealth-time (null jit-lock-stealth-timer)) |
02b420eb | 209 | (setq jit-lock-stealth-timer |
b743187d | 210 | (run-with-idle-timer jit-lock-stealth-time t |
7840ced1 GM |
211 | 'jit-lock-stealth-fontify))) |
212 | ||
1063efe8 CY |
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))) | |
219 | ||
b743187d SM |
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))) | |
225 | ||
623a1226 | 226 | ;; Initialize contextual fontification if requested. |
f415f2d7 | 227 | (when (eq jit-lock-contextually t) |
7b4d9d3b SM |
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))) | |
623a1226 SM |
232 | (setq jit-lock-context-unfontify-pos |
233 | (or jit-lock-context-unfontify-pos (point-max)))) | |
bcacade9 | 234 | |
a62e3c6f | 235 | ;; Setup our hooks. |
bcacade9 | 236 | (add-hook 'after-change-functions 'jit-lock-after-change nil t) |
7840ced1 GM |
237 | (add-hook 'fontification-functions 'jit-lock-function)) |
238 | ||
239 | ;; Turn Just-in-time Lock mode off. | |
240 | (t | |
b743187d | 241 | ;; Cancel our idle timers. |
7b4d9d3b SM |
242 | (when (and (or jit-lock-stealth-timer jit-lock-defer-timer |
243 | jit-lock-context-timer) | |
b743187d SM |
244 | ;; Only if there's no other buffer using them. |
245 | (not (catch 'found | |
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)) | |
7b4d9d3b SM |
252 | (when jit-lock-context-timer |
253 | (cancel-timer jit-lock-context-timer) | |
254 | (setq jit-lock-context-timer nil)) | |
b743187d SM |
255 | (when jit-lock-defer-timer |
256 | (cancel-timer jit-lock-defer-timer) | |
257 | (setq jit-lock-defer-timer nil))) | |
7840ced1 | 258 | |
a62e3c6f | 259 | ;; Remove hooks. |
02b420eb | 260 | (remove-hook 'after-change-functions 'jit-lock-after-change t) |
7840ced1 GM |
261 | (remove-hook 'fontification-functions 'jit-lock-function)))) |
262 | ||
c94d5f40 | 263 | (defun jit-lock-register (fun &optional contextual) |
8a677d4f SM |
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 | |
c94d5f40 SM |
266 | that needs to be (re)fontified. |
267 | If non-nil, CONTEXTUAL means that a contextual fontification would be useful." | |
f8bacc70 | 268 | (add-hook 'jit-lock-functions fun nil t) |
f415f2d7 SM |
269 | (when (and contextual jit-lock-contextually) |
270 | (set (make-local-variable 'jit-lock-contextually) t)) | |
f8bacc70 SM |
271 | (jit-lock-mode t)) |
272 | ||
273 | (defun jit-lock-unregister (fun) | |
8a677d4f | 274 | "Unregister FUN as a fontification function. |
f8bacc70 SM |
275 | Only applies to the current buffer." |
276 | (remove-hook 'jit-lock-functions fun t) | |
a62e3c6f | 277 | (unless jit-lock-functions (jit-lock-mode nil))) |
7840ced1 | 278 | |
02b420eb SM |
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). | |
a62e3c6f SM |
283 | (defun jit-lock-refontify (&optional beg end) |
284 | "Force refontification of the region BEG..END (default whole buffer)." | |
bcacade9 | 285 | (with-buffer-prepared-for-jit-lock |
5a5987eb SM |
286 | (save-restriction |
287 | (widen) | |
b743187d SM |
288 | (put-text-property (or beg (point-min)) (or end (point-max)) |
289 | 'fontified nil)))) | |
7840ced1 GM |
290 | \f |
291 | ;;; On demand fontification. | |
292 | ||
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' | |
296 | is active." | |
74614ac6 | 297 | (when (and jit-lock-mode (not memory-full)) |
0902822d | 298 | (if (null jit-lock-defer-timer) |
b743187d SM |
299 | ;; No deferral. |
300 | (jit-lock-fontify-now start (+ start jit-lock-chunk-size)) | |
301 | ;; Record the buffer for later fontification. | |
623a1226 SM |
302 | (unless (memq (current-buffer) jit-lock-defer-buffers) |
303 | (push (current-buffer) jit-lock-defer-buffers)) | |
b743187d SM |
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 | |
309 | start 'fontified nil | |
310 | (min (point-max) (+ start jit-lock-chunk-size))) | |
311 | 'fontified 'defer))))) | |
a62e3c6f SM |
312 | |
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." | |
bcacade9 | 316 | (with-buffer-prepared-for-jit-lock |
60bffb78 | 317 | (save-excursion |
c9cf2e67 SM |
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. | |
663e1660 | 328 | (let ((orig-start start) next) |
c9cf2e67 SM |
329 | (save-match-data |
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. | |
c1860747 | 333 | (while (and start (< start end)) |
c9cf2e67 SM |
334 | ;; Determine the end of this chunk. |
335 | (setq next (or (text-property-any start end 'fontified t) | |
336 | end)) | |
337 | ||
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)) | |
f1180544 | 346 | |
c4ac63d0 SM |
347 | ;; Make sure the contextual refontification doesn't re-refontify |
348 | ;; what's already been refontified. | |
c4ac63d0 SM |
349 | (when (and jit-lock-context-unfontify-pos |
350 | (< jit-lock-context-unfontify-pos next) | |
7aaf6f17 SM |
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))) | |
c4ac63d0 SM |
357 | (setq jit-lock-context-unfontify-pos next)) |
358 | ||
c9cf2e67 SM |
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) | |
f415f2d7 SM |
363 | (condition-case err |
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)))) | |
c9cf2e67 | 370 | |
663e1660 SM |
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) | |
be956324 | 381 | (run-with-timer 0 nil 'jit-lock-force-redisplay |
2a8edf31 | 382 | (current-buffer) start orig-start)) |
663e1660 | 383 | |
c9cf2e67 SM |
384 | ;; Find the start of the next chunk, if any. |
385 | (setq start (text-property-any next end 'fontified nil)))))))) | |
7840ced1 | 386 | |
be956324 SM |
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. | |
32937629 | 393 | (put-text-property start end 'fontified t)))) |
2a8edf31 KS |
394 | |
395 | ||
7840ced1 GM |
396 | \f |
397 | ;;; Stealth fontification. | |
398 | ||
399 | (defsubst jit-lock-stealth-chunk-start (around) | |
6dea7173 | 400 | "Return the start of the next chunk to fontify around position AROUND. |
7840ced1 | 401 | Value is nil if there is nothing more to fontify." |
8c887c51 GM |
402 | (if (zerop (buffer-size)) |
403 | nil | |
404 | (save-restriction | |
405 | (widen) | |
b743187d | 406 | (let* ((next (text-property-not-all around (point-max) 'fontified t)) |
8c887c51 GM |
407 | (prev (previous-single-property-change around 'fontified)) |
408 | (prop (get-text-property (max (point-min) (1- around)) | |
409 | 'fontified)) | |
410 | (start (cond | |
411 | ((null prev) | |
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. | |
b743187d | 416 | (if (eq prop t) |
8c887c51 GM |
417 | nil |
418 | (max (point-min) | |
419 | (- around (/ jit-lock-chunk-size 2))))) | |
b743187d | 420 | ((eq prop t) |
8c887c51 | 421 | ;; PREV is the start of a region of fontified |
bcacade9 | 422 | ;; text containing AROUND. Start fontifying a |
8c887c51 GM |
423 | ;; chunk size before the end of the unfontified |
424 | ;; region in front of that. | |
425 | (max (or (previous-single-property-change prev 'fontified) | |
426 | (point-min)) | |
427 | (- prev jit-lock-chunk-size))) | |
428 | (t | |
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 | |
432 | ;; nearer. | |
433 | (max prev (- around jit-lock-chunk-size))))) | |
434 | (result (cond ((null start) next) | |
435 | ((null next) start) | |
436 | ((< (- around start) (- next around)) start) | |
437 | (t next)))) | |
438 | result)))) | |
f1180544 | 439 | |
1063efe8 | 440 | (defun jit-lock-stealth-fontify (&optional repeat) |
7840ced1 | 441 | "Fontify buffers stealthily. |
1063efe8 CY |
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. | |
446 | (unless repeat | |
447 | (cancel-timer jit-lock-stealth-repeat-timer)) | |
7840ced1 | 448 | (unless (or executing-kbd-macro |
74614ac6 | 449 | memory-full |
1063efe8 CY |
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. | |
453 | (null (if repeat | |
454 | jit-lock-stealth-buffers | |
455 | (setq jit-lock-stealth-buffers (buffer-list))))) | |
456 | (let ((buffer (car jit-lock-stealth-buffers)) | |
457 | (delay 0) | |
7840ced1 | 458 | minibuffer-auto-raise |
1063efe8 CY |
459 | message-log-max |
460 | start) | |
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' | |
470 | ;; characters. | |
471 | (with-temp-message (if jit-lock-stealth-verbose | |
472 | (concat "JIT stealth lock " | |
473 | (buffer-name))) | |
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)))) | |
484 | ;; Call us again. | |
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))))) | |
7840ced1 GM |
489 | |
490 | \f | |
491 | ;;; Deferred fontification. | |
492 | ||
b743187d SM |
493 | (defun jit-lock-deferred-fontify () |
494 | "Fontify what was deferred." | |
74614ac6 | 495 | (when (and jit-lock-defer-buffers (not memory-full)) |
b743187d | 496 | ;; Mark the deferred regions back to `fontified = nil' |
623a1226 | 497 | (dolist (buffer jit-lock-defer-buffers) |
b743187d SM |
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))) | |
503 | (while | |
504 | (progn | |
505 | (when (eq (get-text-property pos 'fontified) 'defer) | |
506 | (put-text-property | |
507 | pos (setq pos (next-single-property-change | |
508 | pos 'fontified nil (point-max))) | |
509 | 'fontified nil)) | |
510 | (setq pos (next-single-property-change pos 'fontified))))))))) | |
623a1226 | 511 | (setq jit-lock-defer-buffers nil) |
b743187d | 512 | ;; Force fontification of the visible parts. |
0902822d | 513 | (let ((jit-lock-defer-timer nil)) |
b743187d SM |
514 | ;; (message "Jit-Defer Now") |
515 | (sit-for 0) | |
516 | ;; (message "Jit-Defer Done") | |
517 | ))) | |
f1180544 | 518 | |
b743187d | 519 | |
7b4d9d3b SM |
520 | (defun jit-lock-context-fontify () |
521 | "Refresh fontification to take new context into account." | |
74614ac6 RS |
522 | (unless memory-full |
523 | (dolist (buffer (buffer-list)) | |
524 | (with-current-buffer buffer | |
525 | (when jit-lock-context-unfontify-pos | |
526 | ;; (message "Jit-Context %s" (buffer-name)) | |
527 | (save-restriction | |
528 | (widen) | |
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) | |
543 | (point-min)))) | |
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))))))))) | |
7b4d9d3b | 550 | |
663e1660 SM |
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'. | |
558 | ||
9fd762b0 SM |
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').") | |
663e1660 | 563 | |
7840ced1 GM |
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." | |
74614ac6 | 572 | (when (and jit-lock-mode (not memory-full)) |
663e1660 SM |
573 | (let ((jit-lock-start start) |
574 | (jit-lock-end end)) | |
575 | (with-buffer-prepared-for-jit-lock | |
576 | (run-hook-with-args 'jit-lock-after-change-extend-region-functions | |
577 | start end old-len) | |
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)))))) | |
f1180544 | 591 | |
7840ced1 GM |
592 | (provide 'jit-lock) |
593 | ||
e8af40ee | 594 | ;;; jit-lock.el ends here |