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