]> git.eshelyaron.com Git - emacs.git/commitdiff
Rehighlight hi-lock patterns after reverting the buffer.
authorJuri Linkov <juri@linkov.net>
Wed, 5 Jun 2024 06:38:39 +0000 (09:38 +0300)
committerEshel Yaron <me@eshelyaron.com>
Wed, 5 Jun 2024 10:15:45 +0000 (12:15 +0200)
* lisp/hi-lock.el (hi-lock-revert-buffer-rehighlight): New function.
(hi-lock-mode): Add hi-lock-revert-buffer-rehighlight to
revert-buffer-restore-functions, and remove after mode is disabled.
https://lists.gnu.org/archive/html/emacs-devel/2024-06/msg00044.html

(cherry picked from commit 870100a8b7172a5845369ec6a2c28c2b06b7eaee)

lisp/hi-lock.el

index 516bee67dd04cc512ffe4cd2a368949cb3a7936e..3c26cc845098174987f10a6040ae51a19c4c99b1 100644 (file)
@@ -376,7 +376,9 @@ use (hi-lock-mode 1) for individual buffers.")))
        (hi-lock-find-patterns)
         (add-hook 'font-lock-mode-hook 'hi-lock-font-lock-hook nil t)
         ;; Remove regexps from font-lock-keywords (bug#13891).
-       (add-hook 'change-major-mode-hook (lambda () (hi-lock-mode -1)) nil t))
+       (add-hook 'change-major-mode-hook (lambda () (hi-lock-mode -1)) nil t)
+       (add-hook 'revert-buffer-restore-functions
+                 #'hi-lock-revert-buffer-rehighlight nil t))
     ;; Turned off.
     (when (or hi-lock-interactive-patterns
              hi-lock-file-patterns)
@@ -390,7 +392,9 @@ use (hi-lock-mode 1) for individual buffers.")))
       (remove-overlays nil nil 'hi-lock-overlay t)
       (font-lock-flush))
     (define-key-after menu-bar-edit-menu [hi-lock] nil)
-    (remove-hook 'font-lock-mode-hook 'hi-lock-font-lock-hook t)))
+    (remove-hook 'font-lock-mode-hook 'hi-lock-font-lock-hook t)
+    (remove-hook 'revert-buffer-restore-functions
+                #'hi-lock-revert-buffer-rehighlight t)))
 
 ;;;###autoload
 (define-globalized-minor-mode global-hi-lock-mode
@@ -859,6 +863,17 @@ SPACES-REGEXP is a regexp to substitute spaces in font-lock search."
     (font-lock-add-keywords nil hi-lock-file-patterns t)
     (font-lock-add-keywords nil hi-lock-interactive-patterns t)))
 
+(defun hi-lock-revert-buffer-rehighlight ()
+  "Rehighlight hi-lock patterns after `revert-buffer'.
+Apply the previous patterns after reverting the buffer."
+  (when-let ((patterns hi-lock-interactive-lighters))
+    (lambda ()
+      (setq hi-lock-interactive-lighters nil
+            hi-lock-interactive-patterns nil)
+      (let ((hi-lock-auto-select-face t))
+        (dolist (pattern (reverse patterns))
+          (highlight-regexp (car pattern) (hi-lock-read-face-name)))))))
+
 (defvar hi-lock--hashcons-hash
   (make-hash-table :test 'equal :weakness t)
   "Hash table used to hash cons regexps.")