jit-lock.el 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668
  1. ;;; jit-lock.el --- just-in-time fontification -*- lexical-binding: t -*-
  2. ;; Copyright (C) 1998, 2000-2015 Free Software Foundation, Inc.
  3. ;; Author: Gerd Moellmann <gerd@gnu.org>
  4. ;; Keywords: faces files
  5. ;; Package: emacs
  6. ;; This file is part of GNU Emacs.
  7. ;; GNU Emacs is free software: you can redistribute it and/or modify
  8. ;; it under the terms of the GNU General Public License as published by
  9. ;; the Free Software Foundation, either version 3 of the License, or
  10. ;; (at your option) any later version.
  11. ;; GNU Emacs is distributed in the hope that it will be useful,
  12. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. ;; GNU General Public License for more details.
  15. ;; You should have received a copy of the GNU General Public License
  16. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  17. ;;; Commentary:
  18. ;; Just-in-time fontification, triggered by C redisplay code.
  19. ;;; Code:
  20. (eval-when-compile
  21. (defmacro with-buffer-prepared-for-jit-lock (&rest body)
  22. "Execute BODY in current buffer, overriding several variables.
  23. Preserves the `buffer-modified-p' state of the current buffer."
  24. (declare (debug t))
  25. `(let ((inhibit-point-motion-hooks t))
  26. (with-silent-modifications
  27. ,@body))))
  28. ;;; Customization.
  29. (defgroup jit-lock nil
  30. "Font Lock support mode to fontify just-in-time."
  31. :version "21.1"
  32. :group 'font-lock)
  33. (defcustom jit-lock-chunk-size 500
  34. "Jit-lock fontifies chunks of at most this many characters at a time.
  35. This variable controls both display-time and stealth fontification."
  36. :type 'integer
  37. :group 'jit-lock)
  38. (defcustom jit-lock-stealth-time nil
  39. "Time in seconds to wait before beginning stealth fontification.
  40. Stealth fontification occurs if there is no input within this time.
  41. If nil, stealth fontification is never performed.
  42. The value of this variable is used when JIT Lock mode is turned on."
  43. :type '(choice (const :tag "never" nil)
  44. (number :tag "seconds" :value 16))
  45. :group 'jit-lock)
  46. (defcustom jit-lock-stealth-nice 0.5
  47. "Time in seconds to pause between chunks of stealth fontification.
  48. Each iteration of stealth fontification is separated by this amount of time,
  49. thus reducing the demand that stealth fontification makes on the system.
  50. If nil, means stealth fontification is never paused.
  51. To reduce machine load during stealth fontification, at the cost of stealth
  52. taking longer to fontify, you could increase the value of this variable.
  53. See also `jit-lock-stealth-load'."
  54. :type '(choice (const :tag "never" nil)
  55. (number :tag "seconds"))
  56. :group 'jit-lock)
  57. (defcustom jit-lock-stealth-load
  58. (if (condition-case nil (load-average) (error)) 200)
  59. "Load in percentage above which stealth fontification is suspended.
  60. Stealth fontification pauses when the system short-term load average (as
  61. returned by the function `load-average' if supported) goes above this level,
  62. thus reducing the demand that stealth fontification makes on the system.
  63. If nil, means stealth fontification is never suspended.
  64. To reduce machine load during stealth fontification, at the cost of stealth
  65. taking longer to fontify, you could reduce the value of this variable.
  66. See also `jit-lock-stealth-nice'."
  67. :type (if (condition-case nil (load-average) (error))
  68. '(choice (const :tag "never" nil)
  69. (integer :tag "load"))
  70. '(const :format "%t: unsupported\n" nil))
  71. :group 'jit-lock)
  72. (defcustom jit-lock-stealth-verbose nil
  73. "If non-nil, means stealth fontification should show status messages."
  74. :type 'boolean
  75. :group 'jit-lock)
  76. (defvaralias 'jit-lock-defer-contextually 'jit-lock-contextually)
  77. (defcustom jit-lock-contextually 'syntax-driven
  78. "If non-nil, means fontification should be syntactically true.
  79. If nil, means fontification occurs only on those lines modified. This
  80. means where modification on a line causes syntactic change on subsequent lines,
  81. those subsequent lines are not refontified to reflect their new context.
  82. If t, means fontification occurs on those lines modified and all
  83. subsequent lines. This means those subsequent lines are refontified to reflect
  84. their new syntactic context, after `jit-lock-context-time' seconds.
  85. If any other value, e.g., `syntax-driven', means syntactically true
  86. fontification occurs only if syntactic fontification is performed using the
  87. buffer mode's syntax table, i.e., only if `font-lock-keywords-only' is nil.
  88. The value of this variable is used when JIT Lock mode is turned on."
  89. :type '(choice (const :tag "never" nil)
  90. (const :tag "always" t)
  91. (other :tag "syntax-driven" syntax-driven))
  92. :group 'jit-lock)
  93. (defcustom jit-lock-context-time 0.5
  94. "Idle time after which text is contextually refontified, if applicable."
  95. :type '(number :tag "seconds")
  96. :group 'jit-lock)
  97. (defcustom jit-lock-defer-time nil ;; 0.25
  98. "Idle time after which deferred fontification should take place.
  99. If nil, fontification is not deferred.
  100. If 0, then fontification is only deferred while there is input pending."
  101. :group 'jit-lock
  102. :type '(choice (const :tag "never" nil)
  103. (number :tag "seconds")))
  104. ;;; Variables that are not customizable.
  105. (defvar-local jit-lock-mode nil
  106. "Non-nil means Just-in-time Lock mode is active.")
  107. (defvar-local jit-lock-functions nil
  108. "Functions to do the actual fontification.
  109. They are called with two arguments: the START and END of the region to fontify.")
  110. (defvar-local jit-lock-context-unfontify-pos nil
  111. "Consider text after this position as contextually unfontified.
  112. If nil, contextual fontification is disabled.")
  113. (defvar jit-lock-stealth-timer nil
  114. "Timer for stealth fontification in Just-in-time Lock mode.")
  115. (defvar jit-lock-stealth-repeat-timer nil
  116. "Timer for repeated stealth fontification in Just-in-time Lock mode.")
  117. (defvar jit-lock-context-timer nil
  118. "Timer for context fontification in Just-in-time Lock mode.")
  119. (defvar jit-lock-defer-timer nil
  120. "Timer for deferred fontification in Just-in-time Lock mode.")
  121. (defvar jit-lock-defer-buffers nil
  122. "List of buffers with pending deferred fontification.")
  123. (defvar jit-lock-stealth-buffers nil
  124. "List of buffers that are being fontified stealthily.")
  125. ;;; JIT lock mode
  126. (defun jit-lock-mode (arg)
  127. "Toggle Just-in-time Lock mode.
  128. Turn Just-in-time Lock mode on if and only if ARG is non-nil.
  129. Enable it automatically by customizing group `font-lock'.
  130. When Just-in-time Lock mode is enabled, fontification is different in the
  131. following ways:
  132. - Demand-driven buffer fontification triggered by Emacs C code.
  133. This means initial fontification of the whole buffer does not occur.
  134. Instead, fontification occurs when necessary, such as when scrolling
  135. through the buffer would otherwise reveal unfontified areas. This is
  136. useful if buffer fontification is too slow for large buffers.
  137. - Stealthy buffer fontification if `jit-lock-stealth-time' is non-nil.
  138. This means remaining unfontified areas of buffers are fontified if Emacs has
  139. been idle for `jit-lock-stealth-time' seconds, while Emacs remains idle.
  140. This is useful if any buffer has any deferred fontification.
  141. - Deferred context fontification if `jit-lock-contextually' is
  142. non-nil. This means fontification updates the buffer corresponding to
  143. true syntactic context, after `jit-lock-context-time' seconds of Emacs
  144. idle time, while Emacs remains idle. Otherwise, fontification occurs
  145. on modified lines only, and subsequent lines can remain fontified
  146. corresponding to previous syntactic contexts. This is useful where
  147. strings or comments span lines.
  148. Stealth fontification only occurs while the system remains unloaded.
  149. If the system load rises above `jit-lock-stealth-load' percent, stealth
  150. fontification is suspended. Stealth fontification intensity is controlled via
  151. the variable `jit-lock-stealth-nice'.
  152. If you need to debug code run from jit-lock, see `jit-lock-debug-mode'."
  153. (setq jit-lock-mode arg)
  154. (cond
  155. ((buffer-base-buffer)
  156. ;; We're in an indirect buffer. This doesn't work because jit-lock relies
  157. ;; on the `fontified' text-property which is shared with the base buffer.
  158. (setq jit-lock-mode nil)
  159. (message "Not enabling jit-lock: it does not work in indirect buffer"))
  160. (jit-lock-mode ;; Turn Just-in-time Lock mode on.
  161. ;; Mark the buffer for refontification.
  162. (jit-lock-refontify)
  163. ;; Install an idle timer for stealth fontification.
  164. (when (and jit-lock-stealth-time (null jit-lock-stealth-timer))
  165. (setq jit-lock-stealth-timer
  166. (run-with-idle-timer jit-lock-stealth-time t
  167. 'jit-lock-stealth-fontify)))
  168. ;; Create, but do not activate, the idle timer for repeated
  169. ;; stealth fontification.
  170. (when (and jit-lock-stealth-time (null jit-lock-stealth-repeat-timer))
  171. (setq jit-lock-stealth-repeat-timer (timer-create))
  172. (timer-set-function jit-lock-stealth-repeat-timer
  173. 'jit-lock-stealth-fontify '(t)))
  174. ;; Init deferred fontification timer.
  175. (when (and jit-lock-defer-time (null jit-lock-defer-timer))
  176. (setq jit-lock-defer-timer
  177. (run-with-idle-timer jit-lock-defer-time t
  178. 'jit-lock-deferred-fontify)))
  179. ;; Initialize contextual fontification if requested.
  180. (when (eq jit-lock-contextually t)
  181. (unless jit-lock-context-timer
  182. (setq jit-lock-context-timer
  183. (run-with-idle-timer jit-lock-context-time t
  184. 'jit-lock-context-fontify)))
  185. (setq jit-lock-context-unfontify-pos
  186. (or jit-lock-context-unfontify-pos (point-max))))
  187. ;; Setup our hooks.
  188. (add-hook 'after-change-functions 'jit-lock-after-change nil t)
  189. (add-hook 'fontification-functions 'jit-lock-function))
  190. ;; Turn Just-in-time Lock mode off.
  191. (t
  192. ;; Cancel our idle timers.
  193. (when (and (or jit-lock-stealth-timer jit-lock-defer-timer
  194. jit-lock-context-timer)
  195. ;; Only if there's no other buffer using them.
  196. (not (catch 'found
  197. (dolist (buf (buffer-list))
  198. (with-current-buffer buf
  199. (when jit-lock-mode (throw 'found t)))))))
  200. (when jit-lock-stealth-timer
  201. (cancel-timer jit-lock-stealth-timer)
  202. (setq jit-lock-stealth-timer nil))
  203. (when jit-lock-context-timer
  204. (cancel-timer jit-lock-context-timer)
  205. (setq jit-lock-context-timer nil))
  206. (when jit-lock-defer-timer
  207. (cancel-timer jit-lock-defer-timer)
  208. (setq jit-lock-defer-timer nil)))
  209. ;; Remove hooks.
  210. (remove-hook 'after-change-functions 'jit-lock-after-change t)
  211. (remove-hook 'fontification-functions 'jit-lock-function))))
  212. (define-minor-mode jit-lock-debug-mode
  213. "Minor mode to help debug code run from jit-lock.
  214. When this minor mode is enabled, jit-lock runs as little code as possible
  215. during redisplay and moves the rest to a timer, where things
  216. like `debug-on-error' and Edebug can be used."
  217. :global t :group 'jit-lock
  218. (when jit-lock-defer-timer
  219. (cancel-timer jit-lock-defer-timer)
  220. (setq jit-lock-defer-timer nil))
  221. (when jit-lock-debug-mode
  222. (setq jit-lock-defer-timer
  223. (run-with-idle-timer 0 t #'jit-lock--debug-fontify))))
  224. (defvar jit-lock--debug-fontifying nil)
  225. (defun jit-lock--debug-fontify ()
  226. "Fontify what was deferred for debugging."
  227. (when (and (not jit-lock--debug-fontifying)
  228. jit-lock-defer-buffers (not memory-full))
  229. (let ((jit-lock--debug-fontifying t)
  230. (inhibit-debugger nil)) ;FIXME: Not sufficient!
  231. ;; Mark the deferred regions back to `fontified = nil'
  232. (dolist (buffer jit-lock-defer-buffers)
  233. (when (buffer-live-p buffer)
  234. (with-current-buffer buffer
  235. ;; (message "Jit-Debug %s" (buffer-name))
  236. (with-buffer-prepared-for-jit-lock
  237. (let ((pos (point-min)))
  238. (while
  239. (progn
  240. (when (eq (get-text-property pos 'fontified) 'defer)
  241. (let ((beg pos)
  242. (end (setq pos (next-single-property-change
  243. pos 'fontified
  244. nil (point-max)))))
  245. (put-text-property beg end 'fontified nil)
  246. (jit-lock-fontify-now beg end)))
  247. (setq pos (next-single-property-change
  248. pos 'fontified)))))))))
  249. (setq jit-lock-defer-buffers nil))))
  250. (defun jit-lock-register (fun &optional contextual)
  251. "Register FUN as a fontification function to be called in this buffer.
  252. FUN will be called with two arguments START and END indicating the region
  253. that needs to be (re)fontified.
  254. If non-nil, CONTEXTUAL means that a contextual fontification would be useful."
  255. (add-hook 'jit-lock-functions fun nil t)
  256. (when (and contextual jit-lock-contextually)
  257. (setq-local jit-lock-contextually t))
  258. (jit-lock-mode t))
  259. (defun jit-lock-unregister (fun)
  260. "Unregister FUN as a fontification function.
  261. Only applies to the current buffer."
  262. (remove-hook 'jit-lock-functions fun t)
  263. (unless jit-lock-functions (jit-lock-mode nil)))
  264. (defun jit-lock-refontify (&optional beg end)
  265. "Force refontification of the region BEG..END (default whole buffer)."
  266. (with-buffer-prepared-for-jit-lock
  267. (save-restriction
  268. (widen)
  269. (put-text-property (or beg (point-min)) (or end (point-max))
  270. 'fontified nil))))
  271. ;;; On demand fontification.
  272. (defun jit-lock-function (start)
  273. "Fontify current buffer starting at position START.
  274. This function is added to `fontification-functions' when `jit-lock-mode'
  275. is active."
  276. (when (and jit-lock-mode (not memory-full))
  277. (if (not (and jit-lock-defer-timer
  278. (or (not (eq jit-lock-defer-time 0))
  279. (input-pending-p))))
  280. ;; No deferral.
  281. (jit-lock-fontify-now start (+ start jit-lock-chunk-size))
  282. ;; Record the buffer for later fontification.
  283. (unless (memq (current-buffer) jit-lock-defer-buffers)
  284. (push (current-buffer) jit-lock-defer-buffers))
  285. ;; Mark the area as defer-fontified so that the redisplay engine
  286. ;; is happy and so that the idle timer can find the places to fontify.
  287. (with-buffer-prepared-for-jit-lock
  288. (put-text-property start
  289. (next-single-property-change
  290. start 'fontified nil
  291. (min (point-max) (+ start jit-lock-chunk-size)))
  292. 'fontified 'defer)))))
  293. (defun jit-lock--run-functions (beg end)
  294. (let ((tight-beg nil) (tight-end nil)
  295. (loose-beg beg) (loose-end end))
  296. (run-hook-wrapped
  297. 'jit-lock-functions
  298. (lambda (fun)
  299. (pcase-let*
  300. ((res (funcall fun beg end))
  301. (`(,this-beg . ,this-end)
  302. (if (eq (car-safe res) 'jit-lock-bounds)
  303. (cdr res) (cons beg end))))
  304. ;; If all functions don't fontify the same region, we currently
  305. ;; just try to "still be correct". But we could go further and for
  306. ;; the chunks of text that was fontified by some functions but not
  307. ;; all, we could add text-properties indicating which functions were
  308. ;; already run to avoid running them redundantly when we get to
  309. ;; those chunks.
  310. (setq tight-beg (max (or tight-beg (point-min)) this-beg))
  311. (setq tight-end (min (or tight-end (point-max)) this-end))
  312. (setq loose-beg (min loose-beg this-beg))
  313. (setq loose-end (max loose-end this-end))
  314. nil)))
  315. `(,(min tight-beg beg) ,(max tight-end end) ,loose-beg ,loose-end)))
  316. (defun jit-lock-fontify-now (&optional start end)
  317. "Fontify current buffer from START to END.
  318. Defaults to the whole buffer. END can be out of bounds."
  319. (with-buffer-prepared-for-jit-lock
  320. (save-excursion
  321. (unless start (setq start (point-min)))
  322. (setq end (if end (min end (point-max)) (point-max)))
  323. (let ((orig-start start) next)
  324. (save-match-data
  325. ;; Fontify chunks beginning at START. The end of a
  326. ;; chunk is either `end', or the start of a region
  327. ;; before `end' that has already been fontified.
  328. (while (and start (< start end))
  329. ;; Determine the end of this chunk.
  330. (setq next (or (text-property-any start end 'fontified t)
  331. end))
  332. ;; Fontify the chunk, and mark it as fontified.
  333. ;; We mark it first, to make sure that we don't indefinitely
  334. ;; re-execute this fontification if an error occurs.
  335. (put-text-property start next 'fontified t)
  336. (pcase-let
  337. ;; `tight' is the part we've fully refontified, and `loose'
  338. ;; is the part we've partly refontified (some of the
  339. ;; functions have refontified it but maybe not all).
  340. ((`(,tight-beg ,tight-end ,loose-beg ,_loose-end)
  341. (condition-case err
  342. (jit-lock--run-functions start next)
  343. ;; If the user quits (which shouldn't happen in normal
  344. ;; on-the-fly jit-locking), make sure the fontification
  345. ;; will be performed before displaying the block again.
  346. (quit (put-text-property start next 'fontified nil)
  347. (signal (car err) (cdr err))))))
  348. ;; In case we fontified more than requested, take advantage of the
  349. ;; good news.
  350. (when (or (< tight-beg start) (> tight-end next))
  351. (put-text-property tight-beg tight-end 'fontified t))
  352. ;; Make sure the contextual refontification doesn't re-refontify
  353. ;; what's already been refontified.
  354. (when (and jit-lock-context-unfontify-pos
  355. (< jit-lock-context-unfontify-pos tight-end)
  356. (>= jit-lock-context-unfontify-pos tight-beg)
  357. ;; Don't move boundary forward if we have to
  358. ;; refontify previous text. Otherwise, we risk moving
  359. ;; it past the end of the multiline property and thus
  360. ;; forget about this multiline region altogether.
  361. (not (get-text-property tight-beg
  362. 'jit-lock-defer-multiline)))
  363. (setq jit-lock-context-unfontify-pos tight-end))
  364. ;; The redisplay engine has already rendered the buffer up-to
  365. ;; `orig-start' and won't notice if the above jit-lock-functions
  366. ;; changed the appearance of any part of the buffer prior
  367. ;; to that. So if `loose-beg' is before `orig-start', we need to
  368. ;; cause a new redisplay cycle after this one so that the changes
  369. ;; are properly reflected on screen.
  370. ;; To make such repeated redisplay happen less often, we can
  371. ;; eagerly extend the refontified region with
  372. ;; jit-lock-after-change-extend-region-functions.
  373. (when (< loose-beg orig-start)
  374. (run-with-timer 0 nil #'jit-lock-force-redisplay
  375. (copy-marker loose-beg)
  376. (copy-marker orig-start)))
  377. ;; Find the start of the next chunk, if any.
  378. (setq start
  379. (text-property-any tight-end end 'fontified nil)))))))))
  380. (defun jit-lock-force-redisplay (start end)
  381. "Force the display engine to re-render START's buffer from START to END.
  382. This applies to the buffer associated with marker START."
  383. (when (marker-buffer start)
  384. (with-current-buffer (marker-buffer start)
  385. (with-buffer-prepared-for-jit-lock
  386. (when (> end (point-max))
  387. (setq end (point-max) start (min start end)))
  388. (when (< start (point-min))
  389. (setq start (point-min) end (max start end)))
  390. ;; Don't cause refontification (it's already been done), but just do
  391. ;; some random buffer change, so as to force redisplay.
  392. (put-text-property start end 'fontified t)))))
  393. ;;; Stealth fontification.
  394. (defsubst jit-lock-stealth-chunk-start (around)
  395. "Return the start of the next chunk to fontify around position AROUND.
  396. Value is nil if there is nothing more to fontify."
  397. (if (zerop (buffer-size))
  398. nil
  399. (let* ((next (text-property-not-all around (point-max) 'fontified t))
  400. (prev (previous-single-property-change around 'fontified))
  401. (prop (get-text-property (max (point-min) (1- around))
  402. 'fontified))
  403. (start (cond
  404. ((null prev)
  405. ;; There is no property change between AROUND
  406. ;; and the start of the buffer. If PROP is
  407. ;; non-nil, everything in front of AROUND is
  408. ;; fontified, otherwise nothing is fontified.
  409. (if (eq prop t)
  410. nil
  411. (max (point-min)
  412. (- around (/ jit-lock-chunk-size 2)))))
  413. ((eq prop t)
  414. ;; PREV is the start of a region of fontified
  415. ;; text containing AROUND. Start fontifying a
  416. ;; chunk size before the end of the unfontified
  417. ;; region in front of that.
  418. (max (or (previous-single-property-change prev 'fontified)
  419. (point-min))
  420. (- prev jit-lock-chunk-size)))
  421. (t
  422. ;; PREV is the start of a region of unfontified
  423. ;; text containing AROUND. Start at PREV or
  424. ;; chunk size in front of AROUND, whichever is
  425. ;; nearer.
  426. (max prev (- around jit-lock-chunk-size)))))
  427. (result (cond ((null start) next)
  428. ((null next) start)
  429. ((< (- around start) (- next around)) start)
  430. (t next))))
  431. result)))
  432. (defun jit-lock-stealth-fontify (&optional repeat)
  433. "Fontify buffers stealthily.
  434. This function is called repeatedly after Emacs has become idle for
  435. `jit-lock-stealth-time' seconds. Optional argument REPEAT is expected
  436. non-nil in a repeated invocation of this function."
  437. ;; Cancel timer for repeated invocations.
  438. (unless repeat
  439. (cancel-timer jit-lock-stealth-repeat-timer))
  440. (unless (or executing-kbd-macro
  441. memory-full
  442. (window-minibuffer-p)
  443. ;; For first invocation set up `jit-lock-stealth-buffers'.
  444. ;; In repeated invocations it's already been set up.
  445. (null (if repeat
  446. jit-lock-stealth-buffers
  447. (setq jit-lock-stealth-buffers (buffer-list)))))
  448. (let ((buffer (car jit-lock-stealth-buffers))
  449. (delay 0)
  450. minibuffer-auto-raise
  451. message-log-max
  452. start)
  453. (if (and jit-lock-stealth-load
  454. ;; load-average can return nil. The w32 emulation does
  455. ;; that during the first few dozens of seconds after
  456. ;; startup.
  457. (> (or (car (load-average)) 0) jit-lock-stealth-load))
  458. ;; Wait a little if load is too high.
  459. (setq delay jit-lock-stealth-time)
  460. (if (buffer-live-p buffer)
  461. (with-current-buffer buffer
  462. (if (and jit-lock-mode
  463. (setq start (jit-lock-stealth-chunk-start (point))))
  464. ;; Fontify one block of at most `jit-lock-chunk-size'
  465. ;; characters.
  466. (with-temp-message (if jit-lock-stealth-verbose
  467. (concat "JIT stealth lock "
  468. (buffer-name)))
  469. (jit-lock-fontify-now start
  470. (+ start jit-lock-chunk-size))
  471. ;; Run again after `jit-lock-stealth-nice' seconds.
  472. (setq delay (or jit-lock-stealth-nice 0)))
  473. ;; Nothing to fontify here. Remove this buffer from
  474. ;; `jit-lock-stealth-buffers' and run again immediately.
  475. (setq jit-lock-stealth-buffers (cdr jit-lock-stealth-buffers))))
  476. ;; Buffer is no longer live. Remove it from
  477. ;; `jit-lock-stealth-buffers' and run again immediately.
  478. (setq jit-lock-stealth-buffers (cdr jit-lock-stealth-buffers))))
  479. ;; Call us again.
  480. (when jit-lock-stealth-buffers
  481. (timer-set-idle-time jit-lock-stealth-repeat-timer (current-idle-time))
  482. (timer-inc-time jit-lock-stealth-repeat-timer delay)
  483. (timer-activate-when-idle jit-lock-stealth-repeat-timer t)))))
  484. ;;; Deferred fontification.
  485. (defun jit-lock-deferred-fontify ()
  486. "Fontify what was deferred."
  487. (when (and jit-lock-defer-buffers (not memory-full))
  488. ;; Mark the deferred regions back to `fontified = nil'
  489. (dolist (buffer jit-lock-defer-buffers)
  490. (when (buffer-live-p buffer)
  491. (with-current-buffer buffer
  492. ;; (message "Jit-Defer %s" (buffer-name))
  493. (with-buffer-prepared-for-jit-lock
  494. (let ((pos (point-min)))
  495. (while
  496. (progn
  497. (when (eq (get-text-property pos 'fontified) 'defer)
  498. (put-text-property
  499. pos (setq pos (next-single-property-change
  500. pos 'fontified nil (point-max)))
  501. 'fontified nil))
  502. (setq pos (next-single-property-change
  503. pos 'fontified)))))))))
  504. ;; Force fontification of the visible parts.
  505. (let ((buffers jit-lock-defer-buffers)
  506. (jit-lock-defer-timer nil))
  507. (setq jit-lock-defer-buffers nil)
  508. ;; (message "Jit-Defer Now")
  509. (unless (redisplay) ;FIXME: Should we `force'?
  510. (setq jit-lock-defer-buffers buffers))
  511. ;; (message "Jit-Defer Done")
  512. )))
  513. (defun jit-lock-context-fontify ()
  514. "Refresh fontification to take new context into account."
  515. (unless memory-full
  516. (dolist (buffer (buffer-list))
  517. (with-current-buffer buffer
  518. (when jit-lock-context-unfontify-pos
  519. ;; (message "Jit-Context %s" (buffer-name))
  520. (save-restriction
  521. ;; Don't be blindsided by narrowing that starts in the middle
  522. ;; of a jit-lock-defer-multiline.
  523. (widen)
  524. (when (and (>= jit-lock-context-unfontify-pos (point-min))
  525. (< jit-lock-context-unfontify-pos (point-max)))
  526. ;; If we're in text that matches a complex multi-line
  527. ;; font-lock pattern, make sure the whole text will be
  528. ;; redisplayed eventually.
  529. ;; Despite its name, we treat jit-lock-defer-multiline here
  530. ;; rather than in jit-lock-defer since it has to do with multiple
  531. ;; lines, i.e. with context.
  532. (when (get-text-property jit-lock-context-unfontify-pos
  533. 'jit-lock-defer-multiline)
  534. (setq jit-lock-context-unfontify-pos
  535. (or (previous-single-property-change
  536. jit-lock-context-unfontify-pos
  537. 'jit-lock-defer-multiline)
  538. (point-min))))
  539. (with-buffer-prepared-for-jit-lock
  540. ;; Force contextual refontification.
  541. (remove-text-properties
  542. jit-lock-context-unfontify-pos (point-max)
  543. '(fontified nil jit-lock-defer-multiline nil)))
  544. (setq jit-lock-context-unfontify-pos (point-max)))))))))
  545. (defvar jit-lock-start) (defvar jit-lock-end) ; Dynamically scoped variables.
  546. (defvar jit-lock-after-change-extend-region-functions nil
  547. "Hook that can extend the text to refontify after a change.
  548. This is run after every buffer change. The functions are called with
  549. the three arguments of `after-change-functions': START END OLD-LEN.
  550. The extended region to refontify is returned indirectly by modifying
  551. the variables `jit-lock-start' and `jit-lock-end'.
  552. Note that extending the region this way is not strictly necessary, except
  553. that the nature of the redisplay code tends to otherwise leave some of
  554. the rehighlighted text displayed with the old highlight until the next
  555. redisplay (see comment about repeated redisplay in `jit-lock-fontify-now').")
  556. (defun jit-lock-after-change (start end old-len)
  557. "Mark the rest of the buffer as not fontified after a change.
  558. Installed on `after-change-functions'.
  559. START and END are the start and end of the changed text. OLD-LEN
  560. is the pre-change length.
  561. This function ensures that lines following the change will be refontified
  562. in case the syntax of those lines has changed. Refontification
  563. will take place when text is fontified stealthily."
  564. (when (and jit-lock-mode (not memory-full))
  565. (let ((jit-lock-start start)
  566. (jit-lock-end end))
  567. (with-buffer-prepared-for-jit-lock
  568. (run-hook-with-args 'jit-lock-after-change-extend-region-functions
  569. start end old-len)
  570. ;; Make sure we change at least one char (in case of deletions).
  571. (setq jit-lock-end (min (max jit-lock-end (1+ start)) (point-max)))
  572. ;; Request refontification.
  573. (save-restriction
  574. (widen)
  575. (put-text-property jit-lock-start jit-lock-end 'fontified nil)))
  576. ;; Mark the change for deferred contextual refontification.
  577. (when jit-lock-context-unfontify-pos
  578. (setq jit-lock-context-unfontify-pos
  579. ;; Here we use `start' because nothing guarantees that the
  580. ;; text between start and end will be otherwise refontified:
  581. ;; usually it will be refontified by virtue of being
  582. ;; displayed, but if it's outside of any displayed area in the
  583. ;; buffer, only jit-lock-context-* will re-fontify it.
  584. (min jit-lock-context-unfontify-pos jit-lock-start))))))
  585. (provide 'jit-lock)
  586. ;;; jit-lock.el ends here