lazy-lock.el 48 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057
  1. ;;; lazy-lock.el --- lazy demand-driven fontification for fast Font Lock mode
  2. ;; Copyright (C) 1994-1998, 2001-2012 Free Software Foundation, Inc.
  3. ;; Author: Simon Marshall <simon@gnu.org>
  4. ;; Maintainer: FSF
  5. ;; Keywords: faces files
  6. ;; Version: 2.11
  7. ;; Obsolete-since: 22.1
  8. ;; This file is part of GNU Emacs.
  9. ;; GNU Emacs is free software: you can redistribute it and/or modify
  10. ;; it under the terms of the GNU General Public License as published by
  11. ;; the Free Software Foundation, either version 3 of the License, or
  12. ;; (at your option) any later version.
  13. ;; GNU Emacs is distributed in the hope that it will be useful,
  14. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. ;; GNU General Public License for more details.
  17. ;; You should have received a copy of the GNU General Public License
  18. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  19. ;;; Commentary:
  20. ;; Purpose:
  21. ;;
  22. ;; Lazy Lock mode is a Font Lock support mode.
  23. ;; It makes visiting buffers in Font Lock mode faster by making fontification
  24. ;; be demand-driven, deferred and stealthy, so that fontification only occurs
  25. ;; when, and where, necessary.
  26. ;;
  27. ;; See caveats and feedback below.
  28. ;; See also the fast-lock package. (But don't use them at the same time!)
  29. ;; Installation:
  30. ;;
  31. ;; Put in your ~/.emacs:
  32. ;;
  33. ;; (setq font-lock-support-mode 'lazy-lock-mode)
  34. ;;
  35. ;; Start up a new Emacs and use font-lock as usual (except that you can use the
  36. ;; so-called "gaudier" fontification regexps on big files without frustration).
  37. ;;
  38. ;; In a buffer (which has `font-lock-mode' enabled) which is at least
  39. ;; `lazy-lock-minimum-size' characters long, buffer fontification will not
  40. ;; occur and only the visible portion of the buffer will be fontified. Motion
  41. ;; around the buffer will fontify those visible portions not previously
  42. ;; fontified. If stealth fontification is enabled, buffer fontification will
  43. ;; occur in invisible parts of the buffer after `lazy-lock-stealth-time'
  44. ;; seconds of idle time. If on-the-fly fontification is deferred, on-the-fly
  45. ;; fontification will occur after `lazy-lock-defer-time' seconds of idle time.
  46. ;; User-visible differences with version 1:
  47. ;;
  48. ;; - Version 2 can defer on-the-fly fontification. Therefore you need not, and
  49. ;; should not, use defer-lock.el with this version of lazy-lock.el.
  50. ;;
  51. ;; A number of variables have changed meaning:
  52. ;;
  53. ;; - A value of nil for the variable `lazy-lock-minimum-size' means never turn
  54. ;; on demand-driven fontification. In version 1 this meant always turn on
  55. ;; demand-driven fontification. If you really want demand-driven fontification
  56. ;; regardless of buffer size, set this variable to 0.
  57. ;;
  58. ;; - The variable `lazy-lock-stealth-lines' cannot have a nil value. In
  59. ;; version 1 this meant use `window-height' as the maximum number of lines to
  60. ;; fontify as a stealth chunk. This makes no sense; stealth fontification is
  61. ;; of a buffer, not a window.
  62. ;; Implementation differences with version 1:
  63. ;;
  64. ;; - Version 1 of lazy-lock.el is a bit of a hack. Version 1 demand-driven
  65. ;; fontification, the core feature of lazy-lock.el, is implemented by placing a
  66. ;; function on `post-command-hook'. This function fontifies where necessary,
  67. ;; i.e., where a window scroll has occurred. However, there are a number of
  68. ;; problems with using `post-command-hook':
  69. ;;
  70. ;; (a) As the name suggests, `post-command-hook' is run after every command,
  71. ;; i.e., frequently and regardless of whether scrolling has occurred.
  72. ;; (b) Scrolling can occur during a command, when `post-command-hook' is not
  73. ;; run, i.e., it is not necessarily run after scrolling has occurred.
  74. ;; (c) When `post-command-hook' is run, there is nothing to suggest where
  75. ;; scrolling might have occurred, i.e., which windows have scrolled.
  76. ;;
  77. ;; Thus lazy-lock.el's function is called almost as often as possible, usually
  78. ;; when it need not be called, yet it is not always called when it is needed.
  79. ;; Also, lazy-lock.el's function must check each window to see if a scroll has
  80. ;; occurred there. Worse still, lazy-lock.el's function must fontify a region
  81. ;; twice as large as necessary to make sure the window is completely fontified.
  82. ;; Basically, `post-command-hook' is completely inappropriate for lazy-lock.el.
  83. ;;
  84. ;; Ideally, we want to attach lazy-lock.el's function to a hook that is run
  85. ;; only when scrolling occurs, e.g., `window-start' has changed, and tells us
  86. ;; as much information as we need, i.e., the window and its new buffer region.
  87. ;; Richard Stallman implemented a `window-scroll-functions' for Emacs 19.30.
  88. ;; Functions on it are run when `window-start' has changed, and are supplied
  89. ;; with the window and the window's new `window-start' position. (It would be
  90. ;; better if it also supplied the window's new `window-end' position, but that
  91. ;; is calculated as part of the redisplay process, and the functions on
  92. ;; `window-scroll-functions' are run before redisplay has finished.) Thus, the
  93. ;; hook deals with the above problems (a), (b) and (c).
  94. ;;
  95. ;; If only life was that easy. Version 2 demand-driven fontification is mostly
  96. ;; implemented by placing a function on `window-scroll-functions'. However,
  97. ;; not all scrolling occurs when `window-start' has changed. A change in
  98. ;; window size, e.g., via C-x 1, or a significant deletion, e.g., of a number
  99. ;; of lines, causes text previously invisible (i.e., after `window-end') to
  100. ;; become visible without changing `window-start'. Arguably, these events are
  101. ;; not scrolling events, but fontification must occur for lazy-lock.el to work.
  102. ;; Hooks `window-size-change-functions' and `redisplay-end-trigger-functions'
  103. ;; were added for these circumstances.
  104. ;;
  105. ;; (Ben Wing thinks these hooks are "horribly horribly kludgy", and implemented
  106. ;; a `pre-idle-hook', a `mother-of-all-post-command-hooks', for XEmacs 19.14.
  107. ;; He then hacked up a version 1 lazy-lock.el to use `pre-idle-hook' rather
  108. ;; than `post-command-hook'. Whereas functions on `post-command-hook' are
  109. ;; called almost as often as possible, functions on `pre-idle-hook' really are
  110. ;; called as often as possible, even when the mouse moves and, on some systems,
  111. ;; while XEmacs is idle. Thus, the hook deals with the above problem (b), but
  112. ;; unfortunately it makes (a) worse and does not address (c) at all.
  113. ;;
  114. ;; I freely admit that `redisplay-end-trigger-functions' and, to a much lesser
  115. ;; extent, `window-size-change-functions' are not pretty. However, I feel that
  116. ;; a `window-scroll-functions' feature is cleaner than a `pre-idle-hook', and
  117. ;; the result is faster and smaller, less intrusive and more targeted, code.
  118. ;; Since `pre-idle-hook' is pretty much like `post-command-hook', there is no
  119. ;; point in making this version of lazy-lock.el work with it. Anyway, that's
  120. ;; Lit 30 of my humble opinion.
  121. ;;
  122. ;; - Version 1 stealth fontification is also implemented by placing a function
  123. ;; on `post-command-hook'. This function waits for a given amount of time,
  124. ;; and, if Emacs remains idle, fontifies where necessary. Again, there are a
  125. ;; number of problems with using `post-command-hook':
  126. ;;
  127. ;; (a) Functions on `post-command-hook' are run sequentially, so this function
  128. ;; can interfere with other functions on the hook, and vice versa.
  129. ;; (b) This function waits for a given amount of time, so it can interfere with
  130. ;; various features that are dealt with by Emacs after a command, e.g.,
  131. ;; region highlighting, asynchronous updating and keystroke echoing.
  132. ;; (c) Fontification may be required during a command, when `post-command-hook'
  133. ;; is not run. (Version 2 deferred fontification only.)
  134. ;;
  135. ;; Again, `post-command-hook' is completely inappropriate for lazy-lock.el.
  136. ;; Richard Stallman and Morten Welinder implemented internal Timers and Idle
  137. ;; Timers for Emacs 19.31. Functions can be run independently at given times
  138. ;; or after given amounts of idle time. Thus, the feature deals with the above
  139. ;; problems (a), (b) and (c). Version 2 deferral and stealth are implemented
  140. ;; by functions on Idle Timers. (A function on XEmacs' `pre-idle-hook' is
  141. ;; similar to an Emacs Idle Timer function with a fixed zero second timeout.)
  142. ;; - Version 1 has the following problems (relative to version 2):
  143. ;;
  144. ;; (a) It is slow when it does its job.
  145. ;; (b) It does not always do its job when it should.
  146. ;; (c) It slows all interaction (when it doesn't need to do its job).
  147. ;; (d) It interferes with other package functions on `post-command-hook'.
  148. ;; (e) It interferes with Emacs things within the read-eval loop.
  149. ;;
  150. ;; Ben's hacked-up lazy-lock.el 1.14 almost solved (b) but made (c) worse.
  151. ;;
  152. ;; - Version 2 has the following additional features (relative to version 1):
  153. ;;
  154. ;; (a) It can defer fontification (both on-the-fly and on-scrolling).
  155. ;; (b) It can fontify contextually (syntactically true on-the-fly).
  156. ;; Caveats:
  157. ;;
  158. ;; Lazy Lock mode does not work efficiently with Outline mode.
  159. ;; This is because when in Outline mode, although text may be not visible to
  160. ;; you in the window, the text is visible to Emacs Lisp code (not surprisingly)
  161. ;; and Lazy Lock fontifies it mercilessly. Maybe it will be fixed one day.
  162. ;;
  163. ;; Because buffer text is not necessarily fontified, other packages that expect
  164. ;; buffer text to be fontified in Font Lock mode either might not work as
  165. ;; expected, or might not display buffer text as expected. An example of the
  166. ;; latter is `occur', which copies lines of buffer text into another buffer.
  167. ;;
  168. ;; In Emacs 19.30, Lazy Lock mode does not ensure that an existing buffer is
  169. ;; fontified if it is made visible via a minibuffer-less command that replaces
  170. ;; an existing window's buffer (e.g., via the Buffers menu). Upgrade!
  171. ;;
  172. ;; In Emacs 19.30, Lazy Lock mode does not work well with Transient Mark mode
  173. ;; or modes based on Comint mode (e.g., Shell mode), and also interferes with
  174. ;; the echoing of keystrokes in the minibuffer. This is because of the way
  175. ;; deferral and stealth have to be implemented for Emacs 19.30. Upgrade!
  176. ;;
  177. ;; Currently XEmacs does not have the features to support this version of
  178. ;; lazy-lock.el. Maybe it will one day.
  179. ;; History:
  180. ;;
  181. ;; 1.15--2.00:
  182. ;; - Rewrite for Emacs 19.30 and the features rms added to support lazy-lock.el
  183. ;; so that it could work correctly and efficiently.
  184. ;; - Many thanks to those who reported bugs, fixed bugs, made suggestions or
  185. ;; otherwise contributed in the version 1 cycle; Jari Aalto, Kevin Broadey,
  186. ;; Ulrik Dickow, Bill Dubuque, Bob Glickstein, Boris Goldowsky,
  187. ;; Jonas Jarnestrom, David Karr, Michael Kifer, Erik Naggum, Rick Sladkey,
  188. ;; Jim Thompson, Ben Wing, Ilya Zakharevich, and Richard Stallman.
  189. ;; 2.00--2.01:
  190. ;; - Made `lazy-lock-fontify-after-command' always `sit-for' and so redisplay
  191. ;; - Use `buffer-name' not `buffer-live-p' (Bill Dubuque hint)
  192. ;; - Made `lazy-lock-install' do `add-to-list' not `setq' of `current-buffer'
  193. ;; - Made `lazy-lock-fontify-after-install' loop over buffer list
  194. ;; - Made `lazy-lock-arrange-before-change' to arrange `window-end' triggering
  195. ;; - Made `lazy-lock-let-buffer-state' wrap both `befter-change-functions'
  196. ;; - Made `lazy-lock-fontify-region' do `condition-case' (Hyman Rosen report)
  197. ;; 2.01--2.02:
  198. ;; - Use `buffer-live-p' as `buffer-name' can barf (Richard Stanton report)
  199. ;; - Made `lazy-lock-install' set `font-lock-fontified' (Kevin Davidson report)
  200. ;; - Made `lazy-lock-install' add hooks only if needed
  201. ;; - Made `lazy-lock-unstall' add `font-lock-after-change-function' if needed
  202. ;; 2.02--2.03:
  203. ;; - Made `lazy-lock-fontify-region' do `condition-case' for `quit' too
  204. ;; - Made `lazy-lock-mode' respect the value of `font-lock-inhibit-thing-lock'
  205. ;; - Added `lazy-lock-after-unfontify-buffer'
  206. ;; - Removed `lazy-lock-fontify-after-install' hack
  207. ;; - Made `lazy-lock-fontify-after-scroll' not `set-buffer' to `window-buffer'
  208. ;; - Made `lazy-lock-fontify-after-trigger' not `set-buffer' to `window-buffer'
  209. ;; - Made `lazy-lock-fontify-after-idle' be interruptible (Scott Burson hint)
  210. ;; 2.03--2.04:
  211. ;; - Rewrite for Emacs 19.31 idle timers
  212. ;; - Renamed `buffer-windows' to `get-buffer-window-list'
  213. ;; - Removed `buffer-live-p'
  214. ;; - Made `lazy-lock-defer-after-change' always save `current-buffer'
  215. ;; - Made `lazy-lock-fontify-after-defer' just process buffers
  216. ;; - Made `lazy-lock-install-hooks' add hooks correctly (Kevin Broadey report)
  217. ;; - Made `lazy-lock-install' cope if `lazy-lock-defer-time' is a list
  218. ;; 2.04--2.05:
  219. ;; - Rewrite for Common Lisp macros
  220. ;; - Added `do-while' macro
  221. ;; - Renamed `lazy-lock-let-buffer-state' macro to `save-buffer-state'
  222. ;; - Returned `lazy-lock-fontify-after-install' hack (Darren Hall hint)
  223. ;; - Added `lazy-lock-defer-on-scrolling' functionality (Scott Byer hint)
  224. ;; - Made `lazy-lock-mode' wrap `font-lock-support-mode'
  225. ;; 2.05--2.06:
  226. ;; - Made `lazy-lock-fontify-after-defer' swap correctly (Scott Byer report)
  227. ;; 2.06--2.07:
  228. ;; - Added `lazy-lock-stealth-load' functionality (Rob Hooft hint)
  229. ;; - Made `lazy-lock-unstall' call `lazy-lock-fontify-region' if needed
  230. ;; - Made `lazy-lock-mode' call `lazy-lock-unstall' only if needed
  231. ;; - Made `lazy-lock-defer-after-scroll' do `set-window-redisplay-end-trigger'
  232. ;; - Added `lazy-lock-defer-contextually' functionality
  233. ;; - Added `lazy-lock-defer-on-the-fly' from `lazy-lock-defer-time'
  234. ;; - Renamed `lazy-lock-defer-driven' to `lazy-lock-defer-on-scrolling'
  235. ;; - Removed `lazy-lock-submit-bug-report' and bade farewell
  236. ;; 2.07--2.08:
  237. ;; - Made `lazy-lock-fontify-conservatively' fontify around `window-point'
  238. ;; - Made `save-buffer-state' wrap `inhibit-point-motion-hooks'
  239. ;; - Added Custom support
  240. ;; 2.08--2.09:
  241. ;; - Removed `byte-*' variables from `eval-when-compile' (Erik Naggum hint)
  242. ;; - Made various wrapping `inhibit-point-motion-hooks' (Vinicius Latorre hint)
  243. ;; - Made `lazy-lock-fontify-after-idle' wrap `minibuffer-auto-raise'
  244. ;; - Made `lazy-lock-fontify-after-defer' paranoid about deferred buffers
  245. ;; 2.09--2.10:
  246. ;; - Use `window-end' UPDATE arg for Emacs 20.4 and later.
  247. ;; - Made deferral `widen' before unfontifying (Dan Nicolaescu report)
  248. ;; - Use `lazy-lock-fontify-after-visage' for hideshow.el (Dan Nicolaescu hint)
  249. ;; - Use `other' widget where possible (Andreas Schwab fix)
  250. ;; 2.10--2.11:
  251. ;; - Used `with-temp-message' where possible to make messages temporary.
  252. ;;; Code:
  253. (require 'font-lock)
  254. (eval-when-compile
  255. ;; We don't do this at the top-level as we only use non-autoloaded macros.
  256. (require 'cl)
  257. ;;
  258. ;; We use this to preserve or protect things when modifying text properties.
  259. (defmacro save-buffer-state (varlist &rest body)
  260. "Bind variables according to VARLIST and eval BODY restoring buffer state."
  261. `(let* (,@(append varlist
  262. '((modified (buffer-modified-p))
  263. (buffer-undo-list t)
  264. (inhibit-read-only t)
  265. (inhibit-point-motion-hooks t)
  266. (inhibit-modification-hooks t)
  267. deactivate-mark
  268. buffer-file-name
  269. buffer-file-truename)))
  270. ,@body
  271. (when (and (not modified) (buffer-modified-p))
  272. (restore-buffer-modified-p nil))))
  273. (put 'save-buffer-state 'lisp-indent-function 1)
  274. ;;
  275. ;; We use this for clarity and speed. Naughty but nice.
  276. (defmacro do-while (test &rest body)
  277. "(do-while TEST BODY...): eval BODY... and repeat if TEST yields non-nil.
  278. The order of execution is thus BODY, TEST, BODY, TEST and so on
  279. until TEST returns nil."
  280. `(while (progn ,@body ,test)))
  281. (put 'do-while 'lisp-indent-function (get 'while 'lisp-indent-function)))
  282. (defgroup lazy-lock nil
  283. "Font Lock support mode to fontify lazily."
  284. :group 'font-lock)
  285. (defvar lazy-lock-mode nil) ; Whether we are turned on.
  286. (defvar lazy-lock-buffers nil) ; For deferral.
  287. (defvar lazy-lock-timers (cons nil nil)) ; For deferral and stealth.
  288. ;; User Variables:
  289. (defcustom lazy-lock-minimum-size 25600
  290. "Minimum size of a buffer for demand-driven fontification.
  291. On-demand fontification occurs if the buffer size is greater than this value.
  292. If nil, means demand-driven fontification is never performed.
  293. If a list, each element should be a cons pair of the form (MAJOR-MODE . SIZE),
  294. where MAJOR-MODE is a symbol or t (meaning the default). For example:
  295. ((c-mode . 25600) (c++-mode . 25600) (rmail-mode . 1048576))
  296. means that the minimum size is 25K for buffers in C or C++ modes, one megabyte
  297. for buffers in Rmail mode, and size is irrelevant otherwise.
  298. The value of this variable is used when Lazy Lock mode is turned on."
  299. :type '(choice (const :tag "none" nil)
  300. (integer :tag "size")
  301. (repeat :menu-tag "mode specific" :tag "mode specific"
  302. :value ((t . nil))
  303. (cons :tag "Instance"
  304. (radio :tag "Mode"
  305. (const :tag "all" t)
  306. (symbol :tag "name"))
  307. (radio :tag "Size"
  308. (const :tag "none" nil)
  309. (integer :tag "size")))))
  310. :group 'lazy-lock)
  311. (defcustom lazy-lock-defer-on-the-fly t
  312. "If non-nil, means fontification after a change should be deferred.
  313. If nil, means on-the-fly fontification is performed. This means when changes
  314. occur in the buffer, those areas are immediately fontified.
  315. If a list, it should be a list of `major-mode' symbol names for which deferred
  316. fontification should occur. The sense of the list is negated if it begins with
  317. `not'. For example:
  318. (c-mode c++-mode)
  319. means that on-the-fly fontification is deferred for buffers in C and C++ modes
  320. only, and deferral does not occur otherwise.
  321. The value of this variable is used when Lazy Lock mode is turned on."
  322. :type '(choice (const :tag "never" nil)
  323. (const :tag "always" t)
  324. (set :menu-tag "mode specific" :tag "modes"
  325. :value (not)
  326. (const :tag "Except" not)
  327. (repeat :inline t (symbol :tag "mode"))))
  328. :group 'lazy-lock)
  329. (defcustom lazy-lock-defer-on-scrolling nil
  330. "If non-nil, means fontification after a scroll should be deferred.
  331. If nil, means demand-driven fontification is performed. This means when
  332. scrolling into unfontified areas of the buffer, those areas are immediately
  333. fontified. Thus scrolling never presents unfontified areas. However, since
  334. fontification occurs during scrolling, scrolling may be slow.
  335. If t, means defer-driven fontification is performed. This means fontification
  336. of those areas is deferred. Thus scrolling may present momentarily unfontified
  337. areas. However, since fontification does not occur during scrolling, scrolling
  338. will be faster than demand-driven fontification.
  339. If any other value, e.g., `eventually', means demand-driven fontification is
  340. performed until the buffer is fontified, then buffer fontification becomes
  341. defer-driven. Thus scrolling never presents unfontified areas until the buffer
  342. is first fontified, after which subsequent scrolling may present future buffer
  343. insertions momentarily unfontified. However, since fontification does not
  344. occur during scrolling after the buffer is first fontified, scrolling will
  345. become faster. (But, since contextual changes continually occur, such a value
  346. makes little sense if `lazy-lock-defer-contextually' is non-nil.)
  347. The value of this variable is used when Lazy Lock mode is turned on."
  348. :type '(choice (const :tag "never" nil)
  349. (const :tag "always" t)
  350. (other :tag "eventually" eventually))
  351. :group 'lazy-lock)
  352. (defcustom lazy-lock-defer-contextually 'syntax-driven
  353. "If non-nil, means deferred fontification should be syntactically true.
  354. If nil, means deferred fontification occurs only on those lines modified. This
  355. means where modification on a line causes syntactic change on subsequent lines,
  356. those subsequent lines are not refontified to reflect their new context.
  357. If t, means deferred fontification occurs on those lines modified and all
  358. subsequent lines. This means those subsequent lines are refontified to reflect
  359. their new syntactic context, either immediately or when scrolling into them.
  360. If any other value, e.g., `syntax-driven', means deferred syntactically true
  361. fontification occurs only if syntactic fontification is performed using the
  362. buffer mode's syntax table, i.e., only if `font-lock-keywords-only' is nil.
  363. The value of this variable is used when Lazy Lock mode is turned on."
  364. :type '(choice (const :tag "never" nil)
  365. (const :tag "always" t)
  366. (other :tag "syntax-driven" syntax-driven))
  367. :group 'lazy-lock)
  368. (defcustom lazy-lock-defer-time 0.25
  369. "Time in seconds to delay before beginning deferred fontification.
  370. Deferred fontification occurs if there is no input within this time.
  371. If nil, means fontification is never deferred, regardless of the values of the
  372. variables `lazy-lock-defer-on-the-fly', `lazy-lock-defer-on-scrolling' and
  373. `lazy-lock-defer-contextually'.
  374. The value of this variable is used when Lazy Lock mode is turned on."
  375. :type '(choice (const :tag "never" nil)
  376. (number :tag "seconds"))
  377. :group 'lazy-lock)
  378. (defcustom lazy-lock-stealth-time 30
  379. "Time in seconds to delay before beginning stealth fontification.
  380. Stealth fontification occurs if there is no input within this time.
  381. If nil, means stealth fontification is never performed.
  382. The value of this variable is used when Lazy Lock mode is turned on."
  383. :type '(choice (const :tag "never" nil)
  384. (number :tag "seconds"))
  385. :group 'lazy-lock)
  386. (defcustom lazy-lock-stealth-lines (if font-lock-maximum-decoration 100 250)
  387. "Maximum size of a chunk of stealth fontification.
  388. Each iteration of stealth fontification can fontify this number of lines.
  389. To speed up input response during stealth fontification, at the cost of stealth
  390. taking longer to fontify, you could reduce the value of this variable."
  391. :type '(integer :tag "lines")
  392. :group 'lazy-lock)
  393. (defcustom lazy-lock-stealth-load
  394. (if (condition-case nil (load-average) (error)) 200)
  395. "Load in percentage above which stealth fontification is suspended.
  396. Stealth fontification pauses when the system short-term load average (as
  397. returned by the function `load-average' if supported) goes above this level,
  398. thus reducing the demand that stealth fontification makes on the system.
  399. If nil, means stealth fontification is never suspended.
  400. To reduce machine load during stealth fontification, at the cost of stealth
  401. taking longer to fontify, you could reduce the value of this variable.
  402. See also `lazy-lock-stealth-nice'."
  403. :type (if (condition-case nil (load-average) (error))
  404. '(choice (const :tag "never" nil)
  405. (integer :tag "load"))
  406. '(const :format "%t: unsupported\n" nil))
  407. :group 'lazy-lock)
  408. (defcustom lazy-lock-stealth-nice 0.125
  409. "Time in seconds to pause between chunks of stealth fontification.
  410. Each iteration of stealth fontification is separated by this amount of time,
  411. thus reducing the demand that stealth fontification makes on the system.
  412. If nil, means stealth fontification is never paused.
  413. To reduce machine load during stealth fontification, at the cost of stealth
  414. taking longer to fontify, you could increase the value of this variable.
  415. See also `lazy-lock-stealth-load'."
  416. :type '(choice (const :tag "never" nil)
  417. (number :tag "seconds"))
  418. :group 'lazy-lock)
  419. (defcustom lazy-lock-stealth-verbose
  420. (and (not lazy-lock-defer-contextually) (not (null font-lock-verbose)))
  421. "If non-nil, means stealth fontification should show status messages."
  422. :type 'boolean
  423. :group 'lazy-lock)
  424. ;; User Functions:
  425. ;;;###autoload
  426. (defun lazy-lock-mode (&optional arg)
  427. "Toggle Lazy Lock mode.
  428. With arg, turn Lazy Lock mode on if and only if arg is positive. Enable it
  429. automatically in your `~/.emacs' by:
  430. (setq font-lock-support-mode 'lazy-lock-mode)
  431. For a newer font-lock support mode with similar functionality, see
  432. `jit-lock-mode'. Eventually, Lazy Lock mode will be deprecated in
  433. JIT Lock's favor.
  434. When Lazy Lock mode is enabled, fontification can be lazy in a number of ways:
  435. - Demand-driven buffer fontification if `lazy-lock-minimum-size' is non-nil.
  436. This means initial fontification does not occur if the buffer is greater than
  437. `lazy-lock-minimum-size' characters in length. Instead, fontification occurs
  438. when necessary, such as when scrolling through the buffer would otherwise
  439. reveal unfontified areas. This is useful if buffer fontification is too slow
  440. for large buffers.
  441. - Deferred scroll fontification if `lazy-lock-defer-on-scrolling' is non-nil.
  442. This means demand-driven fontification does not occur as you scroll.
  443. Instead, fontification is deferred until after `lazy-lock-defer-time' seconds
  444. of Emacs idle time, while Emacs remains idle. This is useful if
  445. fontification is too slow to keep up with scrolling.
  446. - Deferred on-the-fly fontification if `lazy-lock-defer-on-the-fly' is non-nil.
  447. This means on-the-fly fontification does not occur as you type. Instead,
  448. fontification is deferred until after `lazy-lock-defer-time' seconds of Emacs
  449. idle time, while Emacs remains idle. This is useful if fontification is too
  450. slow to keep up with your typing.
  451. - Deferred context fontification if `lazy-lock-defer-contextually' is non-nil.
  452. This means fontification updates the buffer corresponding to true syntactic
  453. context, after `lazy-lock-defer-time' seconds of Emacs idle time, while Emacs
  454. remains idle. Otherwise, fontification occurs on modified lines only, and
  455. subsequent lines can remain fontified corresponding to previous syntactic
  456. contexts. This is useful where strings or comments span lines.
  457. - Stealthy buffer fontification if `lazy-lock-stealth-time' is non-nil.
  458. This means remaining unfontified areas of buffers are fontified if Emacs has
  459. been idle for `lazy-lock-stealth-time' seconds, while Emacs remains idle.
  460. This is useful if any buffer has any deferred fontification.
  461. Basic Font Lock mode on-the-fly fontification behavior fontifies modified
  462. lines only. Thus, if `lazy-lock-defer-contextually' is non-nil, Lazy Lock mode
  463. on-the-fly fontification may fontify differently, albeit correctly. In any
  464. event, to refontify some lines you can use \\[font-lock-fontify-block].
  465. Stealth fontification only occurs while the system remains unloaded.
  466. If the system load rises above `lazy-lock-stealth-load' percent, stealth
  467. fontification is suspended. Stealth fontification intensity is controlled via
  468. the variable `lazy-lock-stealth-nice' and `lazy-lock-stealth-lines', and
  469. verbosity is controlled via the variable `lazy-lock-stealth-verbose'."
  470. (interactive "P")
  471. (let* ((was-on lazy-lock-mode)
  472. (now-on (unless (memq 'lazy-lock-mode font-lock-inhibit-thing-lock)
  473. (if arg (> (prefix-numeric-value arg) 0) (not was-on)))))
  474. (cond ((and now-on (not font-lock-mode))
  475. ;; Turned on `lazy-lock-mode' rather than `font-lock-mode'.
  476. (message "Use font-lock-support-mode rather than calling lazy-lock-mode")
  477. (sit-for 2))
  478. (now-on
  479. ;; Turn ourselves on.
  480. (set (make-local-variable 'lazy-lock-mode) t)
  481. (lazy-lock-install))
  482. (was-on
  483. ;; Turn ourselves off.
  484. (set (make-local-variable 'lazy-lock-mode) nil)
  485. (lazy-lock-unstall)))))
  486. ;;;###autoload
  487. (defun turn-on-lazy-lock ()
  488. "Unconditionally turn on Lazy Lock mode."
  489. (lazy-lock-mode t))
  490. (defun lazy-lock-install ()
  491. (let ((min-size (font-lock-value-in-major-mode lazy-lock-minimum-size))
  492. (defer-change (and lazy-lock-defer-time lazy-lock-defer-on-the-fly))
  493. (defer-scroll (and lazy-lock-defer-time lazy-lock-defer-on-scrolling))
  494. (defer-context (and lazy-lock-defer-time lazy-lock-defer-contextually
  495. (or (eq lazy-lock-defer-contextually t)
  496. (null font-lock-keywords-only)))))
  497. ;;
  498. ;; Tell Font Lock whether Lazy Lock will do fontification.
  499. (make-local-variable 'font-lock-fontified)
  500. (setq font-lock-fontified (and min-size (>= (buffer-size) min-size)))
  501. ;;
  502. ;; Add the text properties and fontify.
  503. (if (not font-lock-fontified)
  504. (lazy-lock-after-fontify-buffer)
  505. ;; Make sure we fontify in any existing windows showing the buffer.
  506. (let ((windows (get-buffer-window-list (current-buffer) 'nomini t)))
  507. (lazy-lock-after-unfontify-buffer)
  508. (while windows
  509. (lazy-lock-fontify-conservatively (car windows))
  510. (setq windows (cdr windows)))))
  511. ;;
  512. ;; Add the fontification hooks.
  513. (lazy-lock-install-hooks
  514. font-lock-fontified
  515. (cond ((eq (car-safe defer-change) 'not)
  516. (not (memq major-mode (cdr defer-change))))
  517. ((listp defer-change)
  518. (memq major-mode defer-change))
  519. (t
  520. defer-change))
  521. (eq defer-scroll t)
  522. defer-context)
  523. ;;
  524. ;; Add the fontification timers.
  525. (lazy-lock-install-timers
  526. (if (or defer-change defer-scroll defer-context) lazy-lock-defer-time)
  527. lazy-lock-stealth-time)))
  528. (defun lazy-lock-install-hooks (fontifying
  529. defer-change defer-scroll defer-context)
  530. ;;
  531. ;; Add hook if lazy-lock.el is fontifying on scrolling or is deferring.
  532. (when (or fontifying defer-change defer-scroll defer-context)
  533. (add-hook 'window-scroll-functions (if defer-scroll
  534. 'lazy-lock-defer-after-scroll
  535. 'lazy-lock-fontify-after-scroll)
  536. nil t))
  537. ;;
  538. ;; Add hook if lazy-lock.el is fontifying and is not deferring changes.
  539. (when (and fontifying (not defer-change) (not defer-context))
  540. (add-hook 'before-change-functions 'lazy-lock-arrange-before-change nil t))
  541. ;;
  542. ;; Replace Font Lock mode hook.
  543. (remove-hook 'after-change-functions 'font-lock-after-change-function t)
  544. (add-hook 'after-change-functions
  545. (cond ((and defer-change defer-context)
  546. 'lazy-lock-defer-rest-after-change)
  547. (defer-change
  548. 'lazy-lock-defer-line-after-change)
  549. (defer-context
  550. 'lazy-lock-fontify-rest-after-change)
  551. (t
  552. 'lazy-lock-fontify-line-after-change))
  553. nil t)
  554. ;;
  555. ;; Add package-specific hook.
  556. (add-hook 'outline-view-change-hook 'lazy-lock-fontify-after-visage nil t)
  557. (add-hook 'hs-hide-hook 'lazy-lock-fontify-after-visage nil t))
  558. (defun lazy-lock-install-timers (dtime stime)
  559. ;; Schedule or re-schedule the deferral and stealth timers.
  560. ;; The layout of `lazy-lock-timers' is:
  561. ;; ((DEFER-TIME . DEFER-TIMER) (STEALTH-TIME . STEALTH-TIMER)
  562. ;; If an idle timeout has changed, cancel the existing idle timer (if there
  563. ;; is one) and schedule a new one (if the new idle timeout is non-nil).
  564. (unless (eq dtime (car (car lazy-lock-timers)))
  565. (let ((defer (car lazy-lock-timers)))
  566. (when (cdr defer)
  567. (cancel-timer (cdr defer)))
  568. (setcar lazy-lock-timers (cons dtime (and dtime
  569. (run-with-idle-timer dtime t 'lazy-lock-fontify-after-defer))))))
  570. (unless (eq stime (car (cdr lazy-lock-timers)))
  571. (let ((stealth (cdr lazy-lock-timers)))
  572. (when (cdr stealth)
  573. (cancel-timer (cdr stealth)))
  574. (setcdr lazy-lock-timers (cons stime (and stime
  575. (run-with-idle-timer stime t 'lazy-lock-fontify-after-idle)))))))
  576. (defun lazy-lock-unstall ()
  577. ;;
  578. ;; If Font Lock mode is still enabled, make sure that the buffer is
  579. ;; fontified, and reinstall its hook. We must do this first.
  580. (when font-lock-mode
  581. (when (lazy-lock-unfontified-p)
  582. (let ((verbose (if (numberp font-lock-verbose)
  583. (> (buffer-size) font-lock-verbose)
  584. font-lock-verbose)))
  585. (with-temp-message
  586. (when verbose
  587. (format "Fontifying %s..." (buffer-name)))
  588. ;; Make sure we fontify etc. in the whole buffer.
  589. (save-restriction
  590. (widen)
  591. (lazy-lock-fontify-region (point-min) (point-max))))))
  592. (add-hook 'after-change-functions 'font-lock-after-change-function nil t))
  593. ;;
  594. ;; Remove the text properties.
  595. (lazy-lock-after-unfontify-buffer)
  596. ;;
  597. ;; Remove the fontification hooks.
  598. (remove-hook 'window-scroll-functions 'lazy-lock-fontify-after-scroll t)
  599. (remove-hook 'window-scroll-functions 'lazy-lock-defer-after-scroll t)
  600. (remove-hook 'before-change-functions 'lazy-lock-arrange-before-change t)
  601. (remove-hook 'after-change-functions 'lazy-lock-fontify-line-after-change t)
  602. (remove-hook 'after-change-functions 'lazy-lock-fontify-rest-after-change t)
  603. (remove-hook 'after-change-functions 'lazy-lock-defer-line-after-change t)
  604. (remove-hook 'after-change-functions 'lazy-lock-defer-rest-after-change t)
  605. (remove-hook 'outline-view-change-hook 'lazy-lock-fontify-after-visage t)
  606. (remove-hook 'hs-hide-hook 'lazy-lock-fontify-after-visage t))
  607. ;; Hook functions.
  608. ;; Lazy Lock mode intervenes when (1) a previously invisible buffer region
  609. ;; becomes visible, i.e., for demand- or defer-driven on-the-scroll
  610. ;; fontification, (2) a buffer modification occurs, i.e., for defer-driven
  611. ;; on-the-fly fontification, (3) Emacs becomes idle, i.e., for fontification of
  612. ;; deferred fontification and stealth fontification, and (4) other special
  613. ;; occasions.
  614. ;; 1. There are three ways whereby this can happen.
  615. ;;
  616. ;; (a) Scrolling the window, either explicitly (e.g., `scroll-up') or
  617. ;; implicitly (e.g., `search-forward'). Here, `window-start' changes.
  618. ;; Fontification occurs by adding `lazy-lock-fontify-after-scroll' (for
  619. ;; demand-driven fontification) or `lazy-lock-defer-after-scroll' (for
  620. ;; defer-driven fontification) to the hook `window-scroll-functions'.
  621. (defun lazy-lock-fontify-after-scroll (window window-start)
  622. ;; Called from `window-scroll-functions'.
  623. ;; Fontify WINDOW from WINDOW-START following the scroll.
  624. (let ((inhibit-point-motion-hooks t))
  625. (lazy-lock-fontify-region window-start (window-end window t)))
  626. ;; A prior deletion that did not cause scrolling, followed by a scroll, would
  627. ;; result in an unnecessary trigger after this if we did not cancel it now.
  628. (set-window-redisplay-end-trigger window nil))
  629. (defun lazy-lock-defer-after-scroll (window window-start)
  630. ;; Called from `window-scroll-functions'.
  631. ;; Defer fontification following the scroll. Save the current buffer so that
  632. ;; we subsequently fontify in all windows showing the buffer.
  633. (unless (memq (current-buffer) lazy-lock-buffers)
  634. (push (current-buffer) lazy-lock-buffers))
  635. ;; A prior deletion that did not cause scrolling, followed by a scroll, would
  636. ;; result in an unnecessary trigger after this if we did not cancel it now.
  637. (set-window-redisplay-end-trigger window nil))
  638. ;; (b) Resizing the window, either explicitly (e.g., `enlarge-window') or
  639. ;; implicitly (e.g., `delete-other-windows'). Here, `window-end' changes.
  640. ;; Fontification occurs by adding `lazy-lock-fontify-after-resize' to the
  641. ;; hook `window-size-change-functions'.
  642. (defun lazy-lock-fontify-after-resize (frame)
  643. ;; Called from `window-size-change-functions'.
  644. ;; Fontify windows in FRAME following the resize. We cannot use
  645. ;; `window-start' or `window-end' so we fontify conservatively.
  646. (save-excursion
  647. (save-selected-window
  648. (select-frame frame)
  649. (walk-windows (function (lambda (window)
  650. (set-buffer (window-buffer window))
  651. (when lazy-lock-mode
  652. (lazy-lock-fontify-conservatively window))
  653. (set-window-redisplay-end-trigger window nil)))
  654. 'nomini frame))))
  655. ;; (c) Deletion in the buffer. Here, a `window-end' marker can become visible.
  656. ;; Fontification occurs by adding `lazy-lock-arrange-before-change' to
  657. ;; `before-change-functions' and `lazy-lock-fontify-after-trigger' to the
  658. ;; hook `redisplay-end-trigger-functions'. Before every deletion, the
  659. ;; marker `window-redisplay-end-trigger' position is set to the soon-to-be
  660. ;; changed `window-end' position. If the marker becomes visible,
  661. ;; `lazy-lock-fontify-after-trigger' gets called. Ouch. Note that we only
  662. ;; have to deal with this eventuality if there is no on-the-fly deferral.
  663. (defun lazy-lock-arrange-before-change (beg end)
  664. ;; Called from `before-change-functions'.
  665. ;; Arrange that if text becomes visible it will be fontified (if a deletion
  666. ;; is pending, text might become visible at the bottom).
  667. (unless (eq beg end)
  668. (let ((windows (get-buffer-window-list (current-buffer) 'nomini t)) window)
  669. (while windows
  670. (setq window (car windows))
  671. (unless (markerp (window-redisplay-end-trigger window))
  672. (set-window-redisplay-end-trigger window (make-marker)))
  673. (set-marker (window-redisplay-end-trigger window) (window-end window))
  674. (setq windows (cdr windows))))))
  675. (defun lazy-lock-fontify-after-trigger (window trigger-point)
  676. ;; Called from `redisplay-end-trigger-functions'.
  677. ;; Fontify WINDOW from TRIGGER-POINT following the redisplay.
  678. ;; We could probably just use `lazy-lock-fontify-after-scroll' without loss:
  679. ;; (inline (lazy-lock-fontify-after-scroll window (window-start window)))
  680. (let ((inhibit-point-motion-hooks t))
  681. (lazy-lock-fontify-region trigger-point (window-end window t))))
  682. ;; 2. Modified text must be marked as unfontified so it can be identified and
  683. ;; fontified later when Emacs is idle. Deferral occurs by adding one of
  684. ;; `lazy-lock-fontify-*-after-change' (for on-the-fly fontification) or
  685. ;; `lazy-lock-defer-*-after-change' (for deferred fontification) to the
  686. ;; hook `after-change-functions'.
  687. (defalias 'lazy-lock-fontify-line-after-change
  688. ;; Called from `after-change-functions'.
  689. ;; Fontify the current change.
  690. 'font-lock-after-change-function)
  691. (defun lazy-lock-fontify-rest-after-change (beg end old-len)
  692. ;; Called from `after-change-functions'.
  693. ;; Fontify the current change and defer fontification of the rest of the
  694. ;; buffer. Save the current buffer so that we subsequently fontify in all
  695. ;; windows showing the buffer.
  696. (lazy-lock-fontify-line-after-change beg end old-len)
  697. (save-buffer-state nil
  698. (unless (memq (current-buffer) lazy-lock-buffers)
  699. (push (current-buffer) lazy-lock-buffers))
  700. (save-restriction
  701. (widen)
  702. (remove-text-properties end (point-max) '(lazy-lock nil)))))
  703. (defun lazy-lock-defer-line-after-change (beg end old-len)
  704. ;; Called from `after-change-functions'.
  705. ;; Defer fontification of the current change. Save the current buffer so
  706. ;; that we subsequently fontify in all windows showing the buffer.
  707. (save-buffer-state nil
  708. (unless (memq (current-buffer) lazy-lock-buffers)
  709. (push (current-buffer) lazy-lock-buffers))
  710. (remove-text-properties (max (1- beg) (point-min))
  711. (min (1+ end) (point-max))
  712. '(lazy-lock nil))))
  713. (defun lazy-lock-defer-rest-after-change (beg end old-len)
  714. ;; Called from `after-change-functions'.
  715. ;; Defer fontification of the rest of the buffer. Save the current buffer so
  716. ;; that we subsequently fontify in all windows showing the buffer.
  717. (save-buffer-state nil
  718. (unless (memq (current-buffer) lazy-lock-buffers)
  719. (push (current-buffer) lazy-lock-buffers))
  720. (save-restriction
  721. (widen)
  722. (remove-text-properties (max (1- beg) (point-min))
  723. (point-max)
  724. '(lazy-lock nil)))))
  725. ;; 3. Deferred fontification and stealth fontification are done from these two
  726. ;; functions. They are set up as Idle Timers.
  727. (defun lazy-lock-fontify-after-defer ()
  728. ;; Called from `timer-idle-list'.
  729. ;; Fontify all windows where deferral has occurred for its buffer.
  730. (save-excursion
  731. (while (and lazy-lock-buffers (not (input-pending-p)))
  732. (let ((buffer (car lazy-lock-buffers)) windows)
  733. ;; Paranoia: check that the buffer is still live and Lazy Lock mode on.
  734. (when (buffer-live-p buffer)
  735. (set-buffer buffer)
  736. (when lazy-lock-mode
  737. (setq windows (get-buffer-window-list buffer 'nomini t))
  738. (while windows
  739. (lazy-lock-fontify-window (car windows))
  740. (setq windows (cdr windows)))))
  741. (setq lazy-lock-buffers (cdr lazy-lock-buffers)))))
  742. ;; Add hook if fontification should now be defer-driven in this buffer.
  743. (when (and lazy-lock-mode lazy-lock-defer-on-scrolling
  744. (memq 'lazy-lock-fontify-after-scroll window-scroll-functions)
  745. (not (or (input-pending-p) (lazy-lock-unfontified-p))))
  746. (remove-hook 'window-scroll-functions 'lazy-lock-fontify-after-scroll t)
  747. (add-hook 'window-scroll-functions 'lazy-lock-defer-after-scroll nil t)))
  748. (defun lazy-lock-fontify-after-idle ()
  749. ;; Called from `timer-idle-list'.
  750. ;; Fontify all buffers that need it, stealthily while idle.
  751. (unless (or executing-kbd-macro (window-minibuffer-p (selected-window)))
  752. ;; Loop over all buffers, fontify stealthily for each if necessary.
  753. (let ((buffers (buffer-list)) (continue t)
  754. message message-log-max minibuffer-auto-raise)
  755. (save-excursion
  756. (do-while (and buffers continue)
  757. (set-buffer (car buffers))
  758. (if (not (and lazy-lock-mode (lazy-lock-unfontified-p)))
  759. (setq continue (not (input-pending-p)))
  760. ;; Fontify regions in this buffer while there is no input.
  761. (with-temp-message
  762. (when lazy-lock-stealth-verbose
  763. "Fontifying stealthily...")
  764. (do-while (and (lazy-lock-unfontified-p) continue)
  765. (if (and lazy-lock-stealth-load
  766. (> (car (load-average)) lazy-lock-stealth-load))
  767. ;; Wait a while before continuing with the loop.
  768. (progn
  769. (when message
  770. (message "Fontifying stealthily...suspended")
  771. (setq message nil))
  772. (setq continue (sit-for (or lazy-lock-stealth-time 30))))
  773. ;; Fontify a chunk.
  774. (when lazy-lock-stealth-verbose
  775. (if message
  776. (message "Fontifying stealthily... %2d%% of %s"
  777. (lazy-lock-percent-fontified) (buffer-name))
  778. (message "Fontifying stealthily...")
  779. (setq message t)))
  780. ;; Current buffer may have changed during `sit-for'.
  781. (set-buffer (car buffers))
  782. (lazy-lock-fontify-chunk)
  783. (setq continue (sit-for (or lazy-lock-stealth-nice 0)))))))
  784. (setq buffers (cdr buffers)))))))
  785. ;; 4. Special circumstances.
  786. (defun lazy-lock-fontify-after-visage ()
  787. ;; Called from `outline-view-change-hook' and `hs-hide-hook'.
  788. ;; Fontify windows showing the current buffer, as its visibility has changed.
  789. ;; This is a conspiracy hack between lazy-lock.el, outline.el and
  790. ;; hideshow.el.
  791. (let ((windows (get-buffer-window-list (current-buffer) 'nomini t)))
  792. (while windows
  793. (lazy-lock-fontify-conservatively (car windows))
  794. (setq windows (cdr windows)))))
  795. (defun lazy-lock-after-fontify-buffer ()
  796. ;; Called from `font-lock-after-fontify-buffer'.
  797. ;; Mark the current buffer as fontified.
  798. ;; This is a conspiracy hack between lazy-lock.el and font-lock.el.
  799. (save-buffer-state nil
  800. (add-text-properties (point-min) (point-max) '(lazy-lock t))))
  801. (defun lazy-lock-after-unfontify-buffer ()
  802. ;; Called from `font-lock-after-unfontify-buffer'.
  803. ;; Mark the current buffer as unfontified.
  804. ;; This is a conspiracy hack between lazy-lock.el and font-lock.el.
  805. (save-buffer-state nil
  806. (remove-text-properties (point-min) (point-max) '(lazy-lock nil))))
  807. ;; Fontification functions.
  808. ;; If packages want to ensure that some region of the buffer is fontified, they
  809. ;; should use this function. For an example, see ps-print.el.
  810. (defun lazy-lock-fontify-region (beg end)
  811. ;; Fontify between BEG and END, where necessary, in the current buffer.
  812. (save-restriction
  813. (widen)
  814. (when (setq beg (text-property-any beg end 'lazy-lock nil))
  815. (save-excursion
  816. (save-match-data
  817. (save-buffer-state
  818. ;; Ensure syntactic fontification is always correct.
  819. (font-lock-beginning-of-syntax-function next)
  820. ;; Find successive unfontified regions between BEG and END.
  821. (condition-case data
  822. (do-while beg
  823. (setq next (or (text-property-any beg end 'lazy-lock t) end))
  824. ;; Make sure the region end points are at beginning of line.
  825. (goto-char beg)
  826. (unless (bolp)
  827. (beginning-of-line)
  828. (setq beg (point)))
  829. (goto-char next)
  830. (unless (bolp)
  831. (forward-line)
  832. (setq next (point)))
  833. ;; Fontify the region, then flag it as fontified.
  834. (font-lock-fontify-region beg next)
  835. (add-text-properties beg next '(lazy-lock t))
  836. (setq beg (text-property-any next end 'lazy-lock nil)))
  837. ((error quit) (message "Fontifying region...%s" data)))))))))
  838. (defun lazy-lock-fontify-chunk ()
  839. ;; Fontify the nearest chunk, for stealth, in the current buffer.
  840. (let ((inhibit-point-motion-hooks t))
  841. (save-excursion
  842. (save-restriction
  843. (widen)
  844. ;; Move to end of line in case the character at point is not fontified.
  845. (end-of-line)
  846. ;; Find where the previous (next) unfontified regions end (begin).
  847. (let ((prev (previous-single-property-change (point) 'lazy-lock))
  848. (next (text-property-any (point) (point-max) 'lazy-lock nil)))
  849. ;; Fontify from the nearest unfontified position.
  850. (if (or (null prev) (and next (< (- next (point)) (- (point) prev))))
  851. ;; The next, or neither, region is the nearest not fontified.
  852. (lazy-lock-fontify-region
  853. (progn (goto-char (or next (point-min)))
  854. (beginning-of-line)
  855. (point))
  856. (progn (goto-char (or next (point-min)))
  857. (forward-line lazy-lock-stealth-lines)
  858. (point)))
  859. ;; The previous region is the nearest not fontified.
  860. (lazy-lock-fontify-region
  861. (progn (goto-char prev)
  862. (forward-line (- lazy-lock-stealth-lines))
  863. (point))
  864. (progn (goto-char prev)
  865. (forward-line)
  866. (point)))))))))
  867. (defun lazy-lock-fontify-window (window)
  868. ;; Fontify in WINDOW between `window-start' and `window-end'.
  869. ;; We can only do this when we can use `window-start' and `window-end'.
  870. (with-current-buffer (window-buffer window)
  871. (lazy-lock-fontify-region (window-start window) (window-end window))))
  872. (defun lazy-lock-fontify-conservatively (window)
  873. ;; Fontify in WINDOW conservatively around point.
  874. ;; Where we cannot use `window-start' and `window-end' we do `window-height'
  875. ;; lines around point. That way we guarantee to have done enough.
  876. (with-current-buffer (window-buffer window)
  877. (let ((inhibit-point-motion-hooks t))
  878. (lazy-lock-fontify-region
  879. (save-excursion
  880. (goto-char (window-point window))
  881. (vertical-motion (- (window-height window)) window) (point))
  882. (save-excursion
  883. (goto-char (window-point window))
  884. (vertical-motion (window-height window) window) (point))))))
  885. (defun lazy-lock-unfontified-p ()
  886. ;; Return non-nil if there is anywhere still to be fontified.
  887. (save-restriction
  888. (widen)
  889. (text-property-any (point-min) (point-max) 'lazy-lock nil)))
  890. (defun lazy-lock-percent-fontified ()
  891. ;; Return the percentage (of characters) of the buffer that are fontified.
  892. (save-restriction
  893. (widen)
  894. (let ((beg (point-min)) (size 0) next)
  895. ;; Find where the next fontified region begins.
  896. (while (setq beg (text-property-any beg (point-max) 'lazy-lock t))
  897. (setq next (or (text-property-any beg (point-max) 'lazy-lock nil)
  898. (point-max)))
  899. (incf size (- next beg))
  900. (setq beg next))
  901. ;; Float because using integer multiplication will frequently overflow.
  902. (truncate (* (/ (float size) (point-max)) 100)))))
  903. ;; Version dependent workarounds and fixes.
  904. (when (consp lazy-lock-defer-time)
  905. ;;
  906. ;; In 2.06.04 and below, `lazy-lock-defer-time' could specify modes and time.
  907. (with-output-to-temp-buffer "*Help*"
  908. (princ "The value of the variable `lazy-lock-defer-time' was\n ")
  909. (princ lazy-lock-defer-time)
  910. (princ "\n")
  911. (princ "This variable cannot now be a list of modes and time,\n")
  912. (princ "so instead use ")
  913. (princ (substitute-command-keys "\\[customize-option]"))
  914. (princ " to modify the variables, or put the forms:\n")
  915. (princ " (setq lazy-lock-defer-time ")
  916. (princ (cdr lazy-lock-defer-time))
  917. (princ ")\n")
  918. (princ " (setq lazy-lock-defer-on-the-fly '")
  919. (princ (car lazy-lock-defer-time))
  920. (princ ")\n")
  921. (princ "in your ~/.emacs. ")
  922. (princ "The above forms have been evaluated for this editor session,\n")
  923. (princ "but you should use ")
  924. (princ (substitute-command-keys "\\[customize-option]"))
  925. (princ " or change your ~/.emacs now."))
  926. (setq lazy-lock-defer-on-the-fly (car lazy-lock-defer-time)
  927. lazy-lock-defer-time (cdr lazy-lock-defer-time)))
  928. (when (boundp 'lazy-lock-defer-driven)
  929. ;;
  930. ;; In 2.06.04 and below, `lazy-lock-defer-driven' was the variable name.
  931. (with-output-to-temp-buffer "*Help*"
  932. (princ "The value of the variable `lazy-lock-defer-driven' is set to ")
  933. (if (memq lazy-lock-defer-driven '(nil t))
  934. (princ lazy-lock-defer-driven)
  935. (princ "`")
  936. (princ lazy-lock-defer-driven)
  937. (princ "'"))
  938. (princ ".\n")
  939. (princ "This variable is now called `lazy-lock-defer-on-scrolling',\n")
  940. (princ "so instead use ")
  941. (princ (substitute-command-keys "\\[customize-option]"))
  942. (princ " to modify the variable, or put the form:\n")
  943. (princ " (setq lazy-lock-defer-on-scrolling ")
  944. (unless (memq lazy-lock-defer-driven '(nil t))
  945. (princ "'"))
  946. (princ lazy-lock-defer-driven)
  947. (princ ")\n")
  948. (princ "in your ~/.emacs. ")
  949. (princ "The above form has been evaluated for this editor session,\n")
  950. (princ "but you should use ")
  951. (princ (substitute-command-keys "\\[customize-option]"))
  952. (princ " or change your ~/.emacs now."))
  953. (setq lazy-lock-defer-on-scrolling lazy-lock-defer-driven))
  954. ;; Install ourselves:
  955. (add-hook 'window-size-change-functions 'lazy-lock-fontify-after-resize)
  956. (add-hook 'redisplay-end-trigger-functions 'lazy-lock-fontify-after-trigger)
  957. (unless (assq 'lazy-lock-mode minor-mode-alist)
  958. (setq minor-mode-alist (append minor-mode-alist '((lazy-lock-mode nil)))))
  959. ;; Provide ourselves:
  960. (provide 'lazy-lock)
  961. ;; Local Variables:
  962. ;; byte-compile-warnings: (not obsolete)
  963. ;; End:
  964. ;;; lazy-lock.el ends here