jit-lock.el 24 KB

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