diff options
| author | Stefan Monnier | 2000-06-20 16:24:04 +0000 |
|---|---|---|
| committer | Stefan Monnier | 2000-06-20 16:24:04 +0000 |
| commit | bcacade9c20f36e4c2b357dce890156f7c4f72aa (patch) | |
| tree | 8598ab6a01c3bd4b7d74107a5cd1e0c20dd48ec0 | |
| parent | 1bd03f6871b610ce91c3c37cd53cf470e0f2bcd1 (diff) | |
| download | emacs-bcacade9c20f36e4c2b357dce890156f7c4f72aa.tar.gz emacs-bcacade9c20f36e4c2b357dce890156f7c4f72aa.zip | |
(with-buffer-prepared-for-jit-lock):
Renamed from with-buffer-prepared-for-font-lock and use
inhibit-modification-hooks rather than setting *-change-functions.
Update all functions to use the new name.
(jit-lock-first-unfontify-pos): New semantics (and doc).
(jit-lock-mode): Make non-interactive.
Don't automatically turn on font-lock.
Set jit-lock-first-unfontify-pos to indicate deferred-contextual mode.
Always use jit-lock-after-change.
Remove and restore font-lock-after-change-function.
(turn-on-jit-lock, jit-lock-after-fontify-buffer)
(jit-lock-after-unfontify-buffer): Remove.
(jit-lock-stealth-fontify):
Reset jit-lock-first-unfontify-pos to point-max rather than to nil.
(jit-lock-after-change): Set the `fontified' text-prop to nil.
| -rw-r--r-- | lisp/jit-lock.el | 98 |
1 files changed, 40 insertions, 58 deletions
diff --git a/lisp/jit-lock.el b/lisp/jit-lock.el index 0e1c8de8d61..14c1fec9c25 100644 --- a/lisp/jit-lock.el +++ b/lisp/jit-lock.el | |||
| @@ -41,15 +41,14 @@ | |||
| 41 | (unless ,modified | 41 | (unless ,modified |
| 42 | (restore-buffer-modified-p nil))))) | 42 | (restore-buffer-modified-p nil))))) |
| 43 | 43 | ||
| 44 | (defmacro with-buffer-prepared-for-font-lock (&rest body) | 44 | (defmacro with-buffer-prepared-for-jit-lock (&rest body) |
| 45 | "Execute BODY in current buffer, overriding several variables. | 45 | "Execute BODY in current buffer, overriding several variables. |
| 46 | Preserves the `buffer-modified-p' state of the current buffer." | 46 | Preserves the `buffer-modified-p' state of the current buffer." |
| 47 | `(with-buffer-unmodified | 47 | `(with-buffer-unmodified |
| 48 | (let ((buffer-undo-list t) | 48 | (let ((buffer-undo-list t) |
| 49 | (inhibit-read-only t) | 49 | (inhibit-read-only t) |
| 50 | (inhibit-point-motion-hooks t) | 50 | (inhibit-point-motion-hooks t) |
| 51 | before-change-functions | 51 | (inhibit-modification-hooks t) |
| 52 | after-change-functions | ||
| 53 | deactivate-mark | 52 | deactivate-mark |
| 54 | buffer-file-name | 53 | buffer-file-name |
| 55 | buffer-file-truename) | 54 | buffer-file-truename) |
| @@ -60,7 +59,7 @@ Preserves the `buffer-modified-p' state of the current buffer." | |||
| 60 | ;;; Customization. | 59 | ;;; Customization. |
| 61 | 60 | ||
| 62 | (defcustom jit-lock-chunk-size 500 | 61 | (defcustom jit-lock-chunk-size 500 |
| 63 | "*Font-lock chunks of this many characters, or smaller." | 62 | "*Jit-lock chunks of this many characters, or smaller." |
| 64 | :type 'integer | 63 | :type 'integer |
| 65 | :group 'jit-lock) | 64 | :group 'jit-lock) |
| 66 | 65 | ||
| @@ -140,7 +139,8 @@ The value of this variable is used when JIT Lock mode is turned on." | |||
| 140 | 139 | ||
| 141 | 140 | ||
| 142 | (defvar jit-lock-first-unfontify-pos nil | 141 | (defvar jit-lock-first-unfontify-pos nil |
| 143 | "Consider text after this position as unfontified.") | 142 | "Consider text after this position as unfontified. |
| 143 | If nil, contextual fontification is disabled.") | ||
| 144 | (make-variable-buffer-local 'jit-lock-first-unfontify-pos) | 144 | (make-variable-buffer-local 'jit-lock-first-unfontify-pos) |
| 145 | 145 | ||
| 146 | 146 | ||
| @@ -156,7 +156,7 @@ The value of this variable is used when JIT Lock mode is turned on." | |||
| 156 | ;;;###autoload | 156 | ;;;###autoload |
| 157 | (defun jit-lock-mode (arg) | 157 | (defun jit-lock-mode (arg) |
| 158 | "Toggle Just-in-time Lock mode. | 158 | "Toggle Just-in-time Lock mode. |
| 159 | With arg, turn Just-in-time Lock mode on if and only if arg is positive. | 159 | Turn Just-in-time Lock mode on if and only if ARG is non-nil. |
| 160 | Enable it automatically by customizing group `font-lock'. | 160 | Enable it automatically by customizing group `font-lock'. |
| 161 | 161 | ||
| 162 | When Just-in-time Lock mode is enabled, fontification is different in the | 162 | When Just-in-time Lock mode is enabled, fontification is different in the |
| @@ -185,20 +185,10 @@ Stealth fontification only occurs while the system remains unloaded. | |||
| 185 | If the system load rises above `jit-lock-stealth-load' percent, stealth | 185 | If the system load rises above `jit-lock-stealth-load' percent, stealth |
| 186 | fontification is suspended. Stealth fontification intensity is controlled via | 186 | fontification is suspended. Stealth fontification intensity is controlled via |
| 187 | the variable `jit-lock-stealth-nice'." | 187 | the variable `jit-lock-stealth-nice'." |
| 188 | (interactive "P") | 188 | (setq jit-lock-mode arg) |
| 189 | (setq jit-lock-mode (if arg | 189 | (cond (;; Turn Just-in-time Lock mode on. |
| 190 | (> (prefix-numeric-value arg) 0) | 190 | jit-lock-mode |
| 191 | (not jit-lock-mode))) | 191 | |
| 192 | (cond ((and jit-lock-mode | ||
| 193 | (or (not (boundp 'font-lock-mode)) | ||
| 194 | (not font-lock-mode))) | ||
| 195 | ;; If font-lock is not on, turn it on, with Just-in-time | ||
| 196 | ;; Lock mode as support mode; font-lock will call us again. | ||
| 197 | (let ((font-lock-support-mode 'jit-lock-mode)) | ||
| 198 | (font-lock-mode t))) | ||
| 199 | |||
| 200 | ;; Turn Just-in-time Lock mode on. | ||
| 201 | (jit-lock-mode | ||
| 202 | ;; Mark the buffer for refontification | 192 | ;; Mark the buffer for refontification |
| 203 | ;; (in case spurious `fontified' text-props were left around). | 193 | ;; (in case spurious `fontified' text-props were left around). |
| 204 | (jit-lock-fontify-buffer) | 194 | (jit-lock-fontify-buffer) |
| @@ -215,8 +205,6 @@ the variable `jit-lock-stealth-nice'." | |||
| 215 | (set (make-local-variable 'font-lock-fontify-buffer-function) | 205 | (set (make-local-variable 'font-lock-fontify-buffer-function) |
| 216 | 'jit-lock-fontify-buffer)) | 206 | 'jit-lock-fontify-buffer)) |
| 217 | 207 | ||
| 218 | (setq jit-lock-first-unfontify-pos nil) | ||
| 219 | |||
| 220 | ;; Install an idle timer for stealth fontification. | 208 | ;; Install an idle timer for stealth fontification. |
| 221 | (when (and jit-lock-stealth-time | 209 | (when (and jit-lock-stealth-time |
| 222 | (null jit-lock-stealth-timer)) | 210 | (null jit-lock-stealth-timer)) |
| @@ -225,11 +213,16 @@ the variable `jit-lock-stealth-nice'." | |||
| 225 | jit-lock-stealth-time | 213 | jit-lock-stealth-time |
| 226 | 'jit-lock-stealth-fontify))) | 214 | 'jit-lock-stealth-fontify))) |
| 227 | 215 | ||
| 228 | ;; Add a hook for deferred contectual fontification. | 216 | ;; Initialize deferred contextual fontification if requested. |
| 229 | (when (or (eq jit-lock-defer-contextually 'always) | 217 | (when (or (eq jit-lock-defer-contextually 'always) |
| 230 | (and (not (eq jit-lock-defer-contextually 'never)) | 218 | (and (not (eq jit-lock-defer-contextually 'never)) |
| 231 | (null font-lock-keywords-only))) | 219 | (null font-lock-keywords-only))) |
| 232 | (add-hook 'after-change-functions 'jit-lock-after-change nil t)) | 220 | (setq jit-lock-first-unfontify-pos (point-max))) |
| 221 | |||
| 222 | ;; Setup our after-change-function | ||
| 223 | ;; and remove font-lock's (if any). | ||
| 224 | (remove-hook 'after-change-functions 'font-lock-after-change-function t) | ||
| 225 | (add-hook 'after-change-functions 'jit-lock-after-change nil t) | ||
| 233 | 226 | ||
| 234 | ;; Install the fontification hook. | 227 | ;; Install the fontification hook. |
| 235 | (add-hook 'fontification-functions 'jit-lock-function)) | 228 | (add-hook 'fontification-functions 'jit-lock-function)) |
| @@ -247,22 +240,20 @@ the variable `jit-lock-stealth-nice'." | |||
| 247 | jit-lock-saved-fontify-buffer-function) | 240 | jit-lock-saved-fontify-buffer-function) |
| 248 | (setq jit-lock-saved-fontify-buffer-function nil)) | 241 | (setq jit-lock-saved-fontify-buffer-function nil)) |
| 249 | 242 | ||
| 250 | ;; Remove hooks. | 243 | ;; Remove hooks (and restore font-lock's if necessary). |
| 251 | (remove-hook 'after-change-functions 'jit-lock-after-change t) | 244 | (remove-hook 'after-change-functions 'jit-lock-after-change t) |
| 245 | (when font-lock-mode | ||
| 246 | (add-hook 'after-change-functions | ||
| 247 | 'font-lock-after-change-function nil t)) | ||
| 252 | (remove-hook 'fontification-functions 'jit-lock-function)))) | 248 | (remove-hook 'fontification-functions 'jit-lock-function)))) |
| 253 | 249 | ||
| 254 | 250 | ||
| 255 | ;;;###autoload | ||
| 256 | (defun turn-on-jit-lock () | ||
| 257 | "Unconditionally turn on Just-in-time Lock mode." | ||
| 258 | (jit-lock-mode 1)) | ||
| 259 | |||
| 260 | ;; This function is used to prevent font-lock-fontify-buffer from | 251 | ;; This function is used to prevent font-lock-fontify-buffer from |
| 261 | ;; fontifying eagerly the whole buffer. This is important for | 252 | ;; fontifying eagerly the whole buffer. This is important for |
| 262 | ;; things like CWarn mode which adds/removes a few keywords and | 253 | ;; things like CWarn mode which adds/removes a few keywords and |
| 263 | ;; does a refontify (which takes ages on large files). | 254 | ;; does a refontify (which takes ages on large files). |
| 264 | (defun jit-lock-fontify-buffer () | 255 | (defun jit-lock-fontify-buffer () |
| 265 | (with-buffer-prepared-for-font-lock | 256 | (with-buffer-prepared-for-jit-lock |
| 266 | (save-restriction | 257 | (save-restriction |
| 267 | (widen) | 258 | (widen) |
| 268 | (add-text-properties (point-min) (point-max) '(fontified nil))))) | 259 | (add-text-properties (point-min) (point-max) '(fontified nil))))) |
| @@ -280,7 +271,7 @@ is active." | |||
| 280 | 271 | ||
| 281 | (defun jit-lock-function-1 (start) | 272 | (defun jit-lock-function-1 (start) |
| 282 | "Fontify current buffer starting at position START." | 273 | "Fontify current buffer starting at position START." |
| 283 | (with-buffer-prepared-for-font-lock | 274 | (with-buffer-prepared-for-jit-lock |
| 284 | (save-excursion | 275 | (save-excursion |
| 285 | (save-restriction | 276 | (save-restriction |
| 286 | (widen) | 277 | (widen) |
| @@ -325,21 +316,6 @@ is active." | |||
| 325 | ;; Restore previous buffer settings. | 316 | ;; Restore previous buffer settings. |
| 326 | (set-syntax-table old-syntax-table)))))) | 317 | (set-syntax-table old-syntax-table)))))) |
| 327 | 318 | ||
| 328 | |||
| 329 | (defun jit-lock-after-fontify-buffer () | ||
| 330 | "Mark the current buffer as fontified. | ||
| 331 | Called from `font-lock-after-fontify-buffer." | ||
| 332 | (with-buffer-prepared-for-font-lock | ||
| 333 | (add-text-properties (point-min) (point-max) '(fontified t)))) | ||
| 334 | |||
| 335 | |||
| 336 | (defun jit-lock-after-unfontify-buffer () | ||
| 337 | "Mark the current buffer as unfontified. | ||
| 338 | Called from `font-lock-after-fontify-buffer." | ||
| 339 | (with-buffer-prepared-for-font-lock | ||
| 340 | (remove-text-properties (point-min) (point-max) '(fontified nil)))) | ||
| 341 | |||
| 342 | |||
| 343 | 319 | ||
| 344 | ;;; Stealth fontification. | 320 | ;;; Stealth fontification. |
| 345 | 321 | ||
| @@ -366,7 +342,7 @@ Value is nil if there is nothing more to fontify." | |||
| 366 | (- around (/ jit-lock-chunk-size 2))))) | 342 | (- around (/ jit-lock-chunk-size 2))))) |
| 367 | (prop | 343 | (prop |
| 368 | ;; PREV is the start of a region of fontified | 344 | ;; PREV is the start of a region of fontified |
| 369 | ;; text containing AROUND. Start fontfifying a | 345 | ;; text containing AROUND. Start fontifying a |
| 370 | ;; chunk size before the end of the unfontified | 346 | ;; chunk size before the end of the unfontified |
| 371 | ;; region in front of that. | 347 | ;; region in front of that. |
| 372 | (max (or (previous-single-property-change prev 'fontified) | 348 | (max (or (previous-single-property-change prev 'fontified) |
| @@ -424,10 +400,10 @@ This functions is called after Emacs has been idle for | |||
| 424 | (widen) | 400 | (widen) |
| 425 | (when (and (>= jit-lock-first-unfontify-pos (point-min)) | 401 | (when (and (>= jit-lock-first-unfontify-pos (point-min)) |
| 426 | (< jit-lock-first-unfontify-pos (point-max))) | 402 | (< jit-lock-first-unfontify-pos (point-max))) |
| 427 | (with-buffer-prepared-for-font-lock | 403 | (with-buffer-prepared-for-jit-lock |
| 428 | (put-text-property jit-lock-first-unfontify-pos | 404 | (put-text-property jit-lock-first-unfontify-pos |
| 429 | (point-max) 'fontified nil)) | 405 | (point-max) 'fontified nil)) |
| 430 | (setq jit-lock-first-unfontify-pos nil)))) | 406 | (setq jit-lock-first-unfontify-pos (point-max))))) |
| 431 | 407 | ||
| 432 | ;; In the following code, the `sit-for' calls cause a | 408 | ;; In the following code, the `sit-for' calls cause a |
| 433 | ;; redisplay, so it's required that the | 409 | ;; redisplay, so it's required that the |
| @@ -462,16 +438,22 @@ is the pre-change length. | |||
| 462 | This function ensures that lines following the change will be refontified | 438 | This function ensures that lines following the change will be refontified |
| 463 | in case the syntax of those lines has changed. Refontification | 439 | in case the syntax of those lines has changed. Refontification |
| 464 | will take place when text is fontified stealthily." | 440 | will take place when text is fontified stealthily." |
| 465 | ;; Don't do much here---removing text properties is too slow for | ||
| 466 | ;; fast typers, giving them the impression of Emacs not being | ||
| 467 | ;; very responsive. | ||
| 468 | (when jit-lock-mode | 441 | (when jit-lock-mode |
| 469 | (setq jit-lock-first-unfontify-pos | 442 | ;; It's important that the `fontified' property be set from the |
| 470 | (if jit-lock-first-unfontify-pos | 443 | ;; beginning of the line, else font-lock will properly change the |
| 471 | (min jit-lock-first-unfontify-pos start) | 444 | ;; text's face, but the display will have been done already and will |
| 472 | start)))) | 445 | ;; be inconsistent with the buffer's content. |
| 446 | (setq start (line-beginning-position)) | ||
| 447 | ;; Make sure we change at least one char (in case of deletions). | ||
| 448 | (setq end (min (max end (1+ start)) (point-max))) | ||
| 449 | ;; Request refontification. | ||
| 450 | (with-buffer-prepared-for-jit-lock | ||
| 451 | (put-text-property start end 'fontified nil)) | ||
| 452 | ;; Mark the change for deferred contextual refontification. | ||
| 453 | (when jit-lock-first-unfontify-pos | ||
| 454 | (setq jit-lock-first-unfontify-pos | ||
| 455 | (min jit-lock-first-unfontify-pos start))))) | ||
| 473 | 456 | ||
| 474 | |||
| 475 | (provide 'jit-lock) | 457 | (provide 'jit-lock) |
| 476 | 458 | ||
| 477 | ;; jit-lock.el ends here | 459 | ;; jit-lock.el ends here |