From 6edc252fcbb1cd5349046e5f40bf6aca27c47e43 Mon Sep 17 00:00:00 2001 From: Juri Linkov Date: Wed, 5 Jun 2024 09:38:39 +0300 Subject: [PATCH] Rehighlight hi-lock patterns after reverting the buffer. * 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 | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/lisp/hi-lock.el b/lisp/hi-lock.el index 516bee67dd0..3c26cc84509 100644 --- a/lisp/hi-lock.el +++ b/lisp/hi-lock.el @@ -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.") -- 2.39.5