comparison lisp/jit-lock.el @ 88155:d7ddb3e565de

sync with trunk
author Henrik Enberg <henrik.enberg@telia.com>
date Mon, 16 Jan 2006 00:03:54 +0000
parents e88404e8f2cf
children
comparison
equal deleted inserted replaced
88154:8ce476d3ba36 88155:d7ddb3e565de
1 ;;; jit-lock.el --- just-in-time fontification 1 ;;; jit-lock.el --- just-in-time fontification
2 2
3 ;; Copyright (C) 1998, 2000, 2001 Free Software Foundation, Inc. 3 ;; Copyright (C) 1998, 2000, 2001, 2002, 2003, 2004,
4 ;; 2005 Free Software Foundation, Inc.
4 5
5 ;; Author: Gerd Moellmann <gerd@gnu.org> 6 ;; Author: Gerd Moellmann <gerd@gnu.org>
6 ;; Keywords: faces files 7 ;; Keywords: faces files
7 8
8 ;; This file is part of GNU Emacs. 9 ;; This file is part of GNU Emacs.
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details. 19 ;; GNU General Public License for more details.
19 20
20 ;; You should have received a copy of the GNU General Public License 21 ;; 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 ;; along with GNU Emacs; see the file COPYING. If not, write to the
22 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330, 23 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
23 ;; Boston, MA 02111-1307, USA. 24 ;; Boston, MA 02110-1301, USA.
24 25
25 ;;; Commentary: 26 ;;; Commentary:
26 27
27 ;; Just-in-time fontification, triggered by C redisplay code. 28 ;; Just-in-time fontification, triggered by C redisplay code.
28 29
30 31
31 32
32 (eval-when-compile 33 (eval-when-compile
33 (defmacro with-buffer-unmodified (&rest body) 34 (defmacro with-buffer-unmodified (&rest body)
34 "Eval BODY, preserving the current buffer's modified state." 35 "Eval BODY, preserving the current buffer's modified state."
36 (declare (debug t))
35 (let ((modified (make-symbol "modified"))) 37 (let ((modified (make-symbol "modified")))
36 `(let ((,modified (buffer-modified-p))) 38 `(let ((,modified (buffer-modified-p)))
37 (unwind-protect 39 (unwind-protect
38 (progn ,@body) 40 (progn ,@body)
39 (unless ,modified 41 (unless ,modified
40 (restore-buffer-modified-p nil)))))) 42 (restore-buffer-modified-p nil))))))
41 43
42 (defmacro with-buffer-prepared-for-jit-lock (&rest body) 44 (defmacro with-buffer-prepared-for-jit-lock (&rest body)
43 "Execute BODY in current buffer, overriding several variables. 45 "Execute BODY in current buffer, overriding several variables.
44 Preserves the `buffer-modified-p' state of the current buffer." 46 Preserves the `buffer-modified-p' state of the current buffer."
47 (declare (debug t))
45 `(with-buffer-unmodified 48 `(with-buffer-unmodified
46 (let ((buffer-undo-list t) 49 (let ((buffer-undo-list t)
47 (inhibit-read-only t) 50 (inhibit-read-only t)
48 (inhibit-point-motion-hooks t) 51 (inhibit-point-motion-hooks t)
49 (inhibit-modification-hooks t) 52 (inhibit-modification-hooks t)
54 57
55 58
56 59
57 ;;; Customization. 60 ;;; Customization.
58 61
62 (defgroup jit-lock nil
63 "Font Lock support mode to fontify just-in-time."
64 :version "21.1"
65 :group 'font-lock)
66
59 (defcustom jit-lock-chunk-size 500 67 (defcustom jit-lock-chunk-size 500
60 "*Jit-lock chunks of this many characters, or smaller." 68 "*Jit-lock fontifies chunks of at most this many characters at a time.
69
70 This variable controls both display-time and stealth fontification."
61 :type 'integer 71 :type 'integer
62 :group 'jit-lock) 72 :group 'jit-lock)
63 73
64 74
65 (defcustom jit-lock-stealth-time 3 75 (defcustom jit-lock-stealth-time 16
66 "*Time in seconds to wait before beginning stealth fontification. 76 "*Time in seconds to wait before beginning stealth fontification.
67 Stealth fontification occurs if there is no input within this time. 77 Stealth fontification occurs if there is no input within this time.
68 If nil, stealth fontification is never performed. 78 If nil, stealth fontification is never performed.
69 79
70 The value of this variable is used when JIT Lock mode is turned on." 80 The value of this variable is used when JIT Lock mode is turned on."
71 :type '(choice (const :tag "never" nil) 81 :type '(choice (const :tag "never" nil)
72 (number :tag "seconds")) 82 (number :tag "seconds"))
73 :group 'jit-lock) 83 :group 'jit-lock)
74 84
75 85
76 (defcustom jit-lock-stealth-nice 0.125 86 (defcustom jit-lock-stealth-nice 0.5
77 "*Time in seconds to pause between chunks of stealth fontification. 87 "*Time in seconds to pause between chunks of stealth fontification.
78 Each iteration of stealth fontification is separated by this amount of time, 88 Each iteration of stealth fontification is separated by this amount of time,
79 thus reducing the demand that stealth fontification makes on the system. 89 thus reducing the demand that stealth fontification makes on the system.
80 If nil, means stealth fontification is never paused. 90 If nil, means stealth fontification is never paused.
81 To reduce machine load during stealth fontification, at the cost of stealth 91 To reduce machine load during stealth fontification, at the cost of stealth
107 "*If non-nil, means stealth fontification should show status messages." 117 "*If non-nil, means stealth fontification should show status messages."
108 :type 'boolean 118 :type 'boolean
109 :group 'jit-lock) 119 :group 'jit-lock)
110 120
111 121
112 (defcustom jit-lock-defer-contextually 'syntax-driven 122 (defvaralias 'jit-lock-defer-contextually 'jit-lock-contextually)
113 "*If non-nil, means deferred fontification should be syntactically true. 123 (defcustom jit-lock-contextually 'syntax-driven
114 If nil, means deferred fontification occurs only on those lines modified. This 124 "*If non-nil, means fontification should be syntactically true.
125 If nil, means fontification occurs only on those lines modified. This
115 means where modification on a line causes syntactic change on subsequent lines, 126 means where modification on a line causes syntactic change on subsequent lines,
116 those subsequent lines are not refontified to reflect their new context. 127 those subsequent lines are not refontified to reflect their new context.
117 If t, means deferred fontification occurs on those lines modified and all 128 If t, means fontification occurs on those lines modified and all
118 subsequent lines. This means those subsequent lines are refontified to reflect 129 subsequent lines. This means those subsequent lines are refontified to reflect
119 their new syntactic context, either immediately or when scrolling into them. 130 their new syntactic context, after `jit-lock-context-time' seconds.
120 If any other value, e.g., `syntax-driven', means deferred syntactically true 131 If any other value, e.g., `syntax-driven', means syntactically true
121 fontification occurs only if syntactic fontification is performed using the 132 fontification occurs only if syntactic fontification is performed using the
122 buffer mode's syntax table, i.e., only if `font-lock-keywords-only' is nil. 133 buffer mode's syntax table, i.e., only if `font-lock-keywords-only' is nil.
123 134
124 The value of this variable is used when JIT Lock mode is turned on." 135 The value of this variable is used when JIT Lock mode is turned on."
125 :type '(choice (const :tag "never" nil) 136 :type '(choice (const :tag "never" nil)
126 (const :tag "always" t) 137 (const :tag "always" t)
127 (other :tag "syntax-driven" syntax-driven)) 138 (other :tag "syntax-driven" syntax-driven))
139 :group 'jit-lock)
140
141 (defcustom jit-lock-context-time 0.5
142 "Idle time after which text is contextually refontified, if applicable."
143 :type '(number :tag "seconds")
128 :group 'jit-lock) 144 :group 'jit-lock)
129 145
130 (defcustom jit-lock-defer-time nil ;; 0.25 146 (defcustom jit-lock-defer-time nil ;; 0.25
131 "Idle time after which deferred fontification should take place. 147 "Idle time after which deferred fontification should take place.
132 If nil, fontification is not deferred." 148 If nil, fontification is not deferred."
143 (defvar jit-lock-functions nil 159 (defvar jit-lock-functions nil
144 "Functions to do the actual fontification. 160 "Functions to do the actual fontification.
145 They are called with two arguments: the START and END of the region to fontify.") 161 They are called with two arguments: the START and END of the region to fontify.")
146 (make-variable-buffer-local 'jit-lock-functions) 162 (make-variable-buffer-local 'jit-lock-functions)
147 163
148 (defvar jit-lock-first-unfontify-pos nil 164 (defvar jit-lock-context-unfontify-pos nil
149 "Consider text after this position as contextually unfontified. 165 "Consider text after this position as contextually unfontified.
150 If nil, contextual fontification is disabled.") 166 If nil, contextual fontification is disabled.")
151 (make-variable-buffer-local 'jit-lock-first-unfontify-pos) 167 (make-variable-buffer-local 'jit-lock-context-unfontify-pos)
152 168
153 169
154 (defvar jit-lock-stealth-timer nil 170 (defvar jit-lock-stealth-timer nil
155 "Timer for stealth fontification in Just-in-time Lock mode.") 171 "Timer for stealth fontification in Just-in-time Lock mode.")
156 172 (defvar jit-lock-context-timer nil
173 "Timer for context fontification in Just-in-time Lock mode.")
157 (defvar jit-lock-defer-timer nil 174 (defvar jit-lock-defer-timer nil
158 "Timer for deferred fontification in Just-in-time Lock mode.") 175 "Timer for deferred fontification in Just-in-time Lock mode.")
159 176
160 (defvar jit-lock-buffers nil 177 (defvar jit-lock-defer-buffers nil
161 "List of buffers with pending deferred fontification.") 178 "List of buffers with pending deferred fontification.")
162 179
163 ;;; JIT lock mode 180 ;;; JIT lock mode
164 181
165 (defun jit-lock-mode (arg) 182 (defun jit-lock-mode (arg)
179 - Stealthy buffer fontification if `jit-lock-stealth-time' is non-nil. 196 - Stealthy buffer fontification if `jit-lock-stealth-time' is non-nil.
180 This means remaining unfontified areas of buffers are fontified if Emacs has 197 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. 198 been idle for `jit-lock-stealth-time' seconds, while Emacs remains idle.
182 This is useful if any buffer has any deferred fontification. 199 This is useful if any buffer has any deferred fontification.
183 200
184 - Deferred context fontification if `jit-lock-defer-contextually' is 201 - Deferred context fontification if `jit-lock-contextually' is
185 non-nil. This means fontification updates the buffer corresponding to 202 non-nil. This means fontification updates the buffer corresponding to
186 true syntactic context, after `jit-lock-stealth-time' seconds of Emacs 203 true syntactic context, after `jit-lock-context-time' seconds of Emacs
187 idle time, while Emacs remains idle. Otherwise, fontification occurs 204 idle time, while Emacs remains idle. Otherwise, fontification occurs
188 on modified lines only, and subsequent lines can remain fontified 205 on modified lines only, and subsequent lines can remain fontified
189 corresponding to previous syntactic contexts. This is useful where 206 corresponding to previous syntactic contexts. This is useful where
190 strings or comments span lines. 207 strings or comments span lines.
191 208
210 (when (and jit-lock-defer-time (null jit-lock-defer-timer)) 227 (when (and jit-lock-defer-time (null jit-lock-defer-timer))
211 (setq jit-lock-defer-timer 228 (setq jit-lock-defer-timer
212 (run-with-idle-timer jit-lock-defer-time t 229 (run-with-idle-timer jit-lock-defer-time t
213 'jit-lock-deferred-fontify))) 230 'jit-lock-deferred-fontify)))
214 231
215 ;; Initialize deferred contextual fontification if requested. 232 ;; Initialize contextual fontification if requested.
216 (when (eq jit-lock-defer-contextually t) 233 (when (eq jit-lock-contextually t)
217 (setq jit-lock-first-unfontify-pos 234 (unless jit-lock-context-timer
218 (or jit-lock-first-unfontify-pos (point-max)))) 235 (setq jit-lock-context-timer
236 (run-with-idle-timer jit-lock-context-time t
237 'jit-lock-context-fontify)))
238 (setq jit-lock-context-unfontify-pos
239 (or jit-lock-context-unfontify-pos (point-max))))
219 240
220 ;; Setup our hooks. 241 ;; Setup our hooks.
221 (add-hook 'after-change-functions 'jit-lock-after-change nil t) 242 (add-hook 'after-change-functions 'jit-lock-after-change nil t)
222 (add-hook 'fontification-functions 'jit-lock-function)) 243 (add-hook 'fontification-functions 'jit-lock-function))
223 244
224 ;; Turn Just-in-time Lock mode off. 245 ;; Turn Just-in-time Lock mode off.
225 (t 246 (t
226 ;; Cancel our idle timers. 247 ;; Cancel our idle timers.
227 (when (and (or jit-lock-stealth-timer jit-lock-defer-timer) 248 (when (and (or jit-lock-stealth-timer jit-lock-defer-timer
249 jit-lock-context-timer)
228 ;; Only if there's no other buffer using them. 250 ;; Only if there's no other buffer using them.
229 (not (catch 'found 251 (not (catch 'found
230 (dolist (buf (buffer-list)) 252 (dolist (buf (buffer-list))
231 (with-current-buffer buf 253 (with-current-buffer buf
232 (when jit-lock-mode (throw 'found t))))))) 254 (when jit-lock-mode (throw 'found t)))))))
233 (when jit-lock-stealth-timer 255 (when jit-lock-stealth-timer
234 (cancel-timer jit-lock-stealth-timer) 256 (cancel-timer jit-lock-stealth-timer)
235 (setq jit-lock-stealth-timer nil)) 257 (setq jit-lock-stealth-timer nil))
258 (when jit-lock-context-timer
259 (cancel-timer jit-lock-context-timer)
260 (setq jit-lock-context-timer nil))
236 (when jit-lock-defer-timer 261 (when jit-lock-defer-timer
237 (cancel-timer jit-lock-defer-timer) 262 (cancel-timer jit-lock-defer-timer)
238 (setq jit-lock-defer-timer nil))) 263 (setq jit-lock-defer-timer nil)))
239 264
240 ;; Remove hooks. 265 ;; Remove hooks.
241 (remove-hook 'after-change-functions 'jit-lock-after-change t) 266 (remove-hook 'after-change-functions 'jit-lock-after-change t)
242 (remove-hook 'fontification-functions 'jit-lock-function)))) 267 (remove-hook 'fontification-functions 'jit-lock-function))))
243 268
244 ;;;###autoload
245 (defun jit-lock-register (fun &optional contextual) 269 (defun jit-lock-register (fun &optional contextual)
246 "Register FUN as a fontification function to be called in this buffer. 270 "Register FUN as a fontification function to be called in this buffer.
247 FUN will be called with two arguments START and END indicating the region 271 FUN will be called with two arguments START and END indicating the region
248 that needs to be (re)fontified. 272 that needs to be (re)fontified.
249 If non-nil, CONTEXTUAL means that a contextual fontification would be useful." 273 If non-nil, CONTEXTUAL means that a contextual fontification would be useful."
250 (add-hook 'jit-lock-functions fun nil t) 274 (add-hook 'jit-lock-functions fun nil t)
251 (when (and contextual jit-lock-defer-contextually) 275 (when (and contextual jit-lock-contextually)
252 (set (make-local-variable 'jit-lock-defer-contextually) t)) 276 (set (make-local-variable 'jit-lock-contextually) t))
253 (jit-lock-mode t)) 277 (jit-lock-mode t))
254 278
255 (defun jit-lock-unregister (fun) 279 (defun jit-lock-unregister (fun)
256 "Unregister FUN as a fontification function. 280 "Unregister FUN as a fontification function.
257 Only applies to the current buffer." 281 Only applies to the current buffer."
274 298
275 (defun jit-lock-function (start) 299 (defun jit-lock-function (start)
276 "Fontify current buffer starting at position START. 300 "Fontify current buffer starting at position START.
277 This function is added to `fontification-functions' when `jit-lock-mode' 301 This function is added to `fontification-functions' when `jit-lock-mode'
278 is active." 302 is active."
279 (when jit-lock-mode 303 (when (and jit-lock-mode (not memory-full))
280 (if (null jit-lock-defer-time) 304 (if (null jit-lock-defer-time)
281 ;; No deferral. 305 ;; No deferral.
282 (jit-lock-fontify-now start (+ start jit-lock-chunk-size)) 306 (jit-lock-fontify-now start (+ start jit-lock-chunk-size))
283 ;; Record the buffer for later fontification. 307 ;; Record the buffer for later fontification.
284 (unless (memq (current-buffer) jit-lock-buffers) 308 (unless (memq (current-buffer) jit-lock-defer-buffers)
285 (push (current-buffer) jit-lock-buffers)) 309 (push (current-buffer) jit-lock-defer-buffers))
286 ;; Mark the area as defer-fontified so that the redisplay engine 310 ;; 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. 311 ;; is happy and so that the idle timer can find the places to fontify.
288 (with-buffer-prepared-for-jit-lock 312 (with-buffer-prepared-for-jit-lock
289 (put-text-property start 313 (put-text-property start
290 (next-single-property-change 314 (next-single-property-change
324 ;; at the start of the line containing START and 348 ;; at the start of the line containing START and
325 ;; stop at the start of the line following NEXT. 349 ;; stop at the start of the line following NEXT.
326 (goto-char next) (setq next (line-beginning-position 2)) 350 (goto-char next) (setq next (line-beginning-position 2))
327 (goto-char start) (setq start (line-beginning-position)) 351 (goto-char start) (setq start (line-beginning-position))
328 352
353 ;; Make sure the contextual refontification doesn't re-refontify
354 ;; what's already been refontified.
355 (when (and jit-lock-context-unfontify-pos
356 (< jit-lock-context-unfontify-pos next)
357 (>= jit-lock-context-unfontify-pos start)
358 ;; Don't move boundary forward if we have to
359 ;; refontify previous text. Otherwise, we risk moving
360 ;; it past the end of the multiline property and thus
361 ;; forget about this multiline region altogether.
362 (not (get-text-property start 'jit-lock-defer-multiline)))
363 (setq jit-lock-context-unfontify-pos next))
364
329 ;; Fontify the chunk, and mark it as fontified. 365 ;; Fontify the chunk, and mark it as fontified.
330 ;; We mark it first, to make sure that we don't indefinitely 366 ;; We mark it first, to make sure that we don't indefinitely
331 ;; re-execute this fontification if an error occurs. 367 ;; re-execute this fontification if an error occurs.
332 (put-text-property start next 'fontified t) 368 (put-text-property start next 'fontified t)
333 (run-hook-with-args 'jit-lock-functions start next) 369 (condition-case err
370 (run-hook-with-args 'jit-lock-functions start next)
371 ;; If the user quits (which shouldn't happen in normal on-the-fly
372 ;; jit-locking), make sure the fontification will be performed
373 ;; before displaying the block again.
374 (quit (put-text-property start next 'fontified nil)
375 (funcall 'signal (car err) (cdr err))))
334 376
335 ;; Find the start of the next chunk, if any. 377 ;; Find the start of the next chunk, if any.
336 (setq start (text-property-any next end 'fontified nil)))))))) 378 (setq start (text-property-any next end 'fontified nil))))))))
337 379
338 380
384 "Fontify buffers stealthily. 426 "Fontify buffers stealthily.
385 This functions is called after Emacs has been idle for 427 This functions is called after Emacs has been idle for
386 `jit-lock-stealth-time' seconds." 428 `jit-lock-stealth-time' seconds."
387 ;; I used to check `inhibit-read-only' here, but I can't remember why. -stef 429 ;; I used to check `inhibit-read-only' here, but I can't remember why. -stef
388 (unless (or executing-kbd-macro 430 (unless (or executing-kbd-macro
431 memory-full
389 (window-minibuffer-p (selected-window))) 432 (window-minibuffer-p (selected-window)))
390 (let ((buffers (buffer-list)) 433 (let ((buffers (buffer-list))
434 (outer-buffer (current-buffer))
391 minibuffer-auto-raise 435 minibuffer-auto-raise
392 message-log-max) 436 message-log-max)
393 (while (and buffers (not (input-pending-p))) 437 (with-local-quit
394 (let ((buffer (car buffers))) 438 (while (and buffers (not (input-pending-p)))
395 (setq buffers (cdr buffers)) 439 (with-current-buffer (pop buffers)
396
397 (with-current-buffer buffer
398 (when jit-lock-mode 440 (when jit-lock-mode
399 ;; This is funny. Calling sit-for with 3rd arg non-nil 441 ;; This is funny. Calling sit-for with 3rd arg non-nil
400 ;; so that it doesn't redisplay, internally calls 442 ;; so that it doesn't redisplay, internally calls
401 ;; wait_reading_process_input also with a parameter 443 ;; wait_reading_process_input also with a parameter
402 ;; saying "don't redisplay." Since this function here 444 ;; saying "don't redisplay." Since this function here
412 454
413 (with-temp-message (if jit-lock-stealth-verbose 455 (with-temp-message (if jit-lock-stealth-verbose
414 (concat "JIT stealth lock " 456 (concat "JIT stealth lock "
415 (buffer-name))) 457 (buffer-name)))
416 458
417 ;; Perform deferred unfontification, if any.
418 (when jit-lock-first-unfontify-pos
419 (save-restriction
420 (widen)
421 (when (and (>= jit-lock-first-unfontify-pos (point-min))
422 (< jit-lock-first-unfontify-pos (point-max)))
423 ;; If we're in text that matches a complex multi-line
424 ;; font-lock pattern, make sure the whole text will be
425 ;; redisplayed eventually.
426 (when (get-text-property jit-lock-first-unfontify-pos
427 'jit-lock-defer-multiline)
428 (setq jit-lock-first-unfontify-pos
429 (or (previous-single-property-change
430 jit-lock-first-unfontify-pos
431 'jit-lock-defer-multiline)
432 (point-min))))
433 (with-buffer-prepared-for-jit-lock
434 (remove-text-properties
435 jit-lock-first-unfontify-pos (point-max)
436 '(fontified nil jit-lock-defer-multiline nil)))
437 (setq jit-lock-first-unfontify-pos (point-max)))))
438
439 ;; In the following code, the `sit-for' calls cause a 459 ;; In the following code, the `sit-for' calls cause a
440 ;; redisplay, so it's required that the 460 ;; redisplay, so it's required that the
441 ;; buffer-modified flag of a buffer that is displayed 461 ;; buffer-modified flag of a buffer that is displayed
442 ;; has the right value---otherwise the mode line of 462 ;; has the right value---otherwise the mode line of
443 ;; an unmodified buffer would show a `*'. 463 ;; an unmodified buffer would show a `*'.
444 (let (start 464 (let (start
445 (nice (or jit-lock-stealth-nice 0)) 465 (nice (or jit-lock-stealth-nice 0))
446 (point (point-min))) 466 (point (point-min)))
447 (while (and (setq start 467 (while (and (setq start
448 (jit-lock-stealth-chunk-start point)) 468 (jit-lock-stealth-chunk-start point))
449 (sit-for nice)) 469 ;; In case sit-for runs any timers,
470 ;; give them the expected current buffer.
471 (with-current-buffer outer-buffer
472 (sit-for nice)))
450 473
451 ;; fontify a block. 474 ;; fontify a block.
452 (jit-lock-fontify-now start (+ start jit-lock-chunk-size)) 475 (jit-lock-fontify-now start (+ start jit-lock-chunk-size))
453 ;; If stealth jit-locking is done backwards, this leads to 476 ;; If stealth jit-locking is done backwards, this leads to
454 ;; excessive O(n^2) refontification. -stef 477 ;; excessive O(n^2) refontification. -stef
455 ;; (when (>= jit-lock-first-unfontify-pos start) 478 ;; (when (>= jit-lock-context-unfontify-pos start)
456 ;; (setq jit-lock-first-unfontify-pos end)) 479 ;; (setq jit-lock-context-unfontify-pos end))
457 480
458 ;; Wait a little if load is too high. 481 ;; Wait a little if load is too high.
459 (when (and jit-lock-stealth-load 482 (when (and jit-lock-stealth-load
460 (> (car (load-average)) jit-lock-stealth-load)) 483 (> (car (load-average)) jit-lock-stealth-load))
461 (sit-for (or jit-lock-stealth-time 30))))))))))))) 484 ;; In case sit-for runs any timers,
485 ;; give them the expected current buffer.
486 (with-current-buffer outer-buffer
487 (sit-for (or jit-lock-stealth-time 30))))))))))))))
462 488
463 489
464 490
465 ;;; Deferred fontification. 491 ;;; Deferred fontification.
466 492
467 (defun jit-lock-deferred-fontify () 493 (defun jit-lock-deferred-fontify ()
468 "Fontify what was deferred." 494 "Fontify what was deferred."
469 (when jit-lock-buffers 495 (when (and jit-lock-defer-buffers (not memory-full))
470 ;; Mark the deferred regions back to `fontified = nil' 496 ;; Mark the deferred regions back to `fontified = nil'
471 (dolist (buffer jit-lock-buffers) 497 (dolist (buffer jit-lock-defer-buffers)
472 (when (buffer-live-p buffer) 498 (when (buffer-live-p buffer)
473 (with-current-buffer buffer 499 (with-current-buffer buffer
474 ;; (message "Jit-Defer %s" (buffer-name)) 500 ;; (message "Jit-Defer %s" (buffer-name))
475 (with-buffer-prepared-for-jit-lock 501 (with-buffer-prepared-for-jit-lock
476 (let ((pos (point-min))) 502 (let ((pos (point-min)))
480 (put-text-property 506 (put-text-property
481 pos (setq pos (next-single-property-change 507 pos (setq pos (next-single-property-change
482 pos 'fontified nil (point-max))) 508 pos 'fontified nil (point-max)))
483 'fontified nil)) 509 'fontified nil))
484 (setq pos (next-single-property-change pos 'fontified))))))))) 510 (setq pos (next-single-property-change pos 'fontified)))))))))
485 (setq jit-lock-buffers nil) 511 (setq jit-lock-defer-buffers nil)
486 ;; Force fontification of the visible parts. 512 ;; Force fontification of the visible parts.
487 (let ((jit-lock-defer-time nil)) 513 (let ((jit-lock-defer-time nil))
488 ;; (message "Jit-Defer Now") 514 ;; (message "Jit-Defer Now")
489 (sit-for 0) 515 (sit-for 0)
490 ;; (message "Jit-Defer Done") 516 ;; (message "Jit-Defer Done")
491 ))) 517 )))
492 518
519
520 (defun jit-lock-context-fontify ()
521 "Refresh fontification to take new context into account."
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)))))))))
493 550
494 (defun jit-lock-after-change (start end old-len) 551 (defun jit-lock-after-change (start end old-len)
495 "Mark the rest of the buffer as not fontified after a change. 552 "Mark the rest of the buffer as not fontified after a change.
496 Installed on `after-change-functions'. 553 Installed on `after-change-functions'.
497 START and END are the start and end of the changed text. OLD-LEN 554 START and END are the start and end of the changed text. OLD-LEN
498 is the pre-change length. 555 is the pre-change length.
499 This function ensures that lines following the change will be refontified 556 This function ensures that lines following the change will be refontified
500 in case the syntax of those lines has changed. Refontification 557 in case the syntax of those lines has changed. Refontification
501 will take place when text is fontified stealthily." 558 will take place when text is fontified stealthily."
502 (when jit-lock-mode 559 (when (and jit-lock-mode (not memory-full))
503 (save-excursion 560 (save-excursion
504 (with-buffer-prepared-for-jit-lock 561 (with-buffer-prepared-for-jit-lock
505 ;; It's important that the `fontified' property be set from the 562 ;; It's important that the `fontified' property be set from the
506 ;; beginning of the line, else font-lock will properly change the 563 ;; beginning of the line, else font-lock will properly change the
507 ;; text's face, but the display will have been done already and will 564 ;; text's face, but the display will have been done already and will
520 ;; Make sure we change at least one char (in case of deletions). 577 ;; Make sure we change at least one char (in case of deletions).
521 (setq end (min (max end (1+ start)) (point-max))) 578 (setq end (min (max end (1+ start)) (point-max)))
522 ;; Request refontification. 579 ;; Request refontification.
523 (put-text-property start end 'fontified nil)) 580 (put-text-property start end 'fontified nil))
524 ;; Mark the change for deferred contextual refontification. 581 ;; Mark the change for deferred contextual refontification.
525 (when jit-lock-first-unfontify-pos 582 (when jit-lock-context-unfontify-pos
526 (setq jit-lock-first-unfontify-pos 583 (setq jit-lock-context-unfontify-pos
527 (min jit-lock-first-unfontify-pos start)))))) 584 ;; Here we use `start' because nothing guarantees that the
585 ;; text between start and end will be otherwise refontified:
586 ;; usually it will be refontified by virtue of being
587 ;; displayed, but if it's outside of any displayed area in the
588 ;; buffer, only jit-lock-context-* will re-fontify it.
589 (min jit-lock-context-unfontify-pos start))))))
528 590
529 (provide 'jit-lock) 591 (provide 'jit-lock)
530 592
593 ;; arch-tag: 56b5de6e-f581-453b-bb97-49c39372ff9e
531 ;;; jit-lock.el ends here 594 ;;; jit-lock.el ends here