"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))
(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."
(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