From 303481b4ecd7d422653263389f86c83b4a4c855b Mon Sep 17 00:00:00 2001 From: Tino Calancha Date: Fri, 25 Aug 2017 00:00:20 +0900 Subject: [PATCH] Keep face available if there are no matches If font-lock-mode is disabled in the current buffer, and there are no matches for REGEXP, then keep FACE available for a next search. * lisp/hi-lock.el (hi-lock-set-pattern): Add FACE into hi-lock--unused-faces if font-lock-mode is disabled and there are no matches. * test/lisp/hi-lock-tests.el (hi-lock-test-set-pattern): Add test. --- lisp/hi-lock.el | 8 ++++++-- test/lisp/hi-lock-tests.el | 12 ++++++++++++ 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/lisp/hi-lock.el b/lisp/hi-lock.el index 5139e01fa84..34300212f02 100644 --- a/lisp/hi-lock.el +++ b/lisp/hi-lock.el @@ -693,7 +693,8 @@ with completion and history." "Highlight REGEXP with face FACE." ;; Hashcons the regexp, so it can be passed to remove-overlays later. (setq regexp (hi-lock--hashcons regexp)) - (let ((pattern (list regexp (list 0 (list 'quote face) 'prepend)))) + (let ((pattern (list regexp (list 0 (list 'quote face) 'prepend))) + (no-matches t)) ;; Refuse to highlight a text that is already highlighted. (if (assoc regexp hi-lock-interactive-patterns) (add-to-list 'hi-lock--unused-faces (face-name face)) @@ -713,11 +714,14 @@ with completion and history." (save-excursion (goto-char search-start) (while (re-search-forward regexp search-end t) + (when no-matches (setq no-matches nil)) (let ((overlay (make-overlay (match-beginning 0) (match-end 0)))) (overlay-put overlay 'hi-lock-overlay t) (overlay-put overlay 'hi-lock-overlay-regexp regexp) (overlay-put overlay 'face face)) - (goto-char (match-end 0))))))))) + (goto-char (match-end 0))) + (when no-matches + (add-to-list 'hi-lock--unused-faces (face-name face))))))))) (defun hi-lock-set-file-patterns (patterns) "Replace file patterns list with PATTERNS and refontify." diff --git a/test/lisp/hi-lock-tests.el b/test/lisp/hi-lock-tests.el index 2cb662cfaca..9e2401979b0 100644 --- a/test/lisp/hi-lock-tests.el +++ b/test/lisp/hi-lock-tests.el @@ -36,5 +36,17 @@ (hi-lock-set-pattern "a" face)))) (should (equal hi-lock--unused-faces (cdr faces)))))) +(ert-deftest hi-lock-test-set-pattern () + (let ((faces hi-lock-face-defaults)) + (with-temp-buffer + (insert "foo bar") + (cl-letf (((symbol-function 'completing-read) + (lambda (prompt coll x y z hist defaults) + (car defaults)))) + (hi-lock-set-pattern "9999" (hi-lock-read-face-name)) ; No match + (hi-lock-set-pattern "foo" (hi-lock-read-face-name))) + ;; Only one match, then we have used just 1 face + (should (equal hi-lock--unused-faces (cdr faces)))))) + (provide 'hi-lock-tests) ;;; hi-lock-tests.el ends here -- 2.39.2