(font-lock-mode): Use the new hooks to get proper behavior on a revert.
authorKarl Heuer <kwzh@gnu.org>
Tue, 1 Feb 1994 01:32:48 +0000 (01:32 +0000)
committerKarl Heuer <kwzh@gnu.org>
Tue, 1 Feb 1994 01:32:48 +0000 (01:32 +0000)
lisp/font-lock.el

index 9ce1ed1..d0c042d 100644 (file)
@@ -365,17 +365,32 @@ can use \\[font-lock-fontify-buffer]."
     (set (make-local-variable 'font-lock-mode) on-p)
     (cond (on-p
           (font-lock-set-defaults)
+          (make-local-variable 'before-revert-hook)
           (make-local-variable 'after-revert-hook)
-          ;;if buffer is reverted, must repeat fontification. 
-          (setq after-revert-hook 'font-lock-fontify-buffer)
+          ;; If buffer is reverted, must clean up the state.
+          (add-hook 'before-revert-hook 'font-lock-revert-setup)
+          (add-hook 'after-revert-hook 'font-lock-revert-cleanup)
           (run-hooks 'font-lock-mode-hook)
           (or font-lock-fontified (font-lock-fontify-buffer)))
          (font-lock-fontified
           (setq font-lock-fontified nil)
-          (setq after-revert-hook nil)
+          (remove-hook 'before-revert-hook 'font-lock-revert-setup)
+          (remove-hook 'after-revert-hook 'font-lock-revert-cleanup)
           (font-lock-unfontify-region (point-min) (point-max))))
     (force-mode-line-update)))
 
+;; If the buffer is about to be reverted, it won't be fontified.
+(defun font-lock-revert-setup ()
+  (setq font-lock-fontified nil))
+
+;; If the buffer has just been reverted, we might not even be in font-lock
+;; mode anymore, and if we are, the buffer may or may not have already been
+;; refontified.  Refontify here if it looks like we need to.
+(defun font-lock-revert-cleanup ()
+  (and font-lock-mode
+       (not font-lock-fontified)
+       (font-lock-mode 1)))
+
 (defun font-lock-fontify-buffer ()
   "Fontify the current buffer the way `font-lock-mode' would: