aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJuri Linkov2020-04-13 02:40:56 +0300
committerJuri Linkov2020-04-13 02:40:56 +0300
commit91e4acf7c736dfdb2673dc33c9303b5284e925df (patch)
treee72ce8d84512ee75701424fdac26560c94ee88f7
parent68ffe4a3c9a001db528b057109d11de71471e4ff (diff)
downloademacs-91e4acf7c736dfdb2673dc33c9303b5284e925df.tar.gz
emacs-91e4acf7c736dfdb2673dc33c9303b5284e925df.zip
Fix hi-lock test and add new test for case-fold (bug#40337)
* lisp/hi-lock.el (hi-lock--regexps-at-point): Handle font-lock faces. (hi-lock-unface-buffer): Simplify default value handling. (hi-lock-set-pattern): Add either lighter or regexp to hi-lock-interactive-lighters. (hi-lock-set-pattern): Put overlay prop hi-lock-overlay-regexp to either lighter or regexp. * test/lisp/hi-lock-tests.el (hi-lock-bug26666): Use "b" instead of "a". (hi-lock-case-fold): New test.
-rw-r--r--etc/NEWS2
-rw-r--r--lisp/hi-lock.el37
-rw-r--r--test/lisp/hi-lock-tests.el102
3 files changed, 120 insertions, 21 deletions
diff --git a/etc/NEWS b/etc/NEWS
index 28c01d71f18..7a7f11f5071 100644
--- a/etc/NEWS
+++ b/etc/NEWS
@@ -267,7 +267,7 @@ to substitute spaces in regexp search.
267 267
268--- 268---
269*** The default value of 'hi-lock-highlight-range' was enlarged. 269*** The default value of 'hi-lock-highlight-range' was enlarged.
270The new default value is 2000000 (2 million). 270The new default value is 2000000 (2 megabytes).
271 271
272** Texinfo 272** Texinfo
273 273
diff --git a/lisp/hi-lock.el b/lisp/hi-lock.el
index d5e46651a50..1d8dc0624ba 100644
--- a/lisp/hi-lock.el
+++ b/lisp/hi-lock.el
@@ -564,13 +564,15 @@ in which case the highlighting will not update as you type."
564 (let ((regexp (get-char-property (point) 'hi-lock-overlay-regexp))) 564 (let ((regexp (get-char-property (point) 'hi-lock-overlay-regexp)))
565 (when regexp (push regexp regexps))) 565 (when regexp (push regexp regexps)))
566 ;; With font-locking on, check if the cursor is on a highlighted text. 566 ;; With font-locking on, check if the cursor is on a highlighted text.
567 (let ((face-after (get-text-property (point) 'face)) 567 (let* ((faces-after (get-text-property (point) 'face))
568 (face-before 568 (faces-before
569 (unless (bobp) (get-text-property (1- (point)) 'face))) 569 (unless (bobp) (get-text-property (1- (point)) 'face)))
570 (faces (mapcar #'hi-lock-keyword->face 570 (faces-after (if (consp faces-after) faces-after (list faces-after)))
571 hi-lock-interactive-patterns))) 571 (faces-before (if (consp faces-before) faces-before (list faces-before)))
572 (unless (memq face-before faces) (setq face-before nil)) 572 (faces (mapcar #'hi-lock-keyword->face
573 (unless (memq face-after faces) (setq face-after nil)) 573 hi-lock-interactive-patterns))
574 (face-after (seq-some (lambda (face) (car (memq face faces))) faces-after))
575 (face-before (seq-some (lambda (face) (car (memq face faces))) faces-before)))
574 (when (and face-before face-after (not (eq face-before face-after))) 576 (when (and face-before face-after (not (eq face-before face-after)))
575 (setq face-before nil)) 577 (setq face-before nil))
576 (when (or face-after face-before) 578 (when (or face-after face-before)
@@ -588,7 +590,8 @@ in which case the highlighting will not update as you type."
588 ;; highlighted text at point. Use this later in 590 ;; highlighted text at point. Use this later in
589 ;; during completing-read. 591 ;; during completing-read.
590 (dolist (hi-lock-pattern hi-lock-interactive-patterns) 592 (dolist (hi-lock-pattern hi-lock-interactive-patterns)
591 (let ((regexp (car hi-lock-pattern))) 593 (let ((regexp (or (car (rassq hi-lock-pattern hi-lock-interactive-lighters))
594 (car hi-lock-pattern))))
592 (if (string-match regexp hi-text) 595 (if (string-match regexp hi-text)
593 (push regexp regexps))))))) 596 (push regexp regexps)))))))
594 regexps)) 597 regexps))
@@ -642,15 +645,10 @@ then remove all hi-lock highlighting."
642 (user-error "No highlighting to remove")) 645 (user-error "No highlighting to remove"))
643 ;; Infer the regexp to un-highlight based on cursor position. 646 ;; Infer the regexp to un-highlight based on cursor position.
644 (let* ((defaults (or (hi-lock--regexps-at-point) 647 (let* ((defaults (or (hi-lock--regexps-at-point)
645 (mapcar #'car hi-lock-interactive-patterns)))) 648 (mapcar (lambda (pattern)
646 (setq defaults 649 (or (car (rassq pattern hi-lock-interactive-lighters))
647 (mapcar (lambda (default) 650 (car pattern)))
648 (or (car (rassq default 651 hi-lock-interactive-patterns))))
649 (mapcar (lambda (a)
650 (cons (car a) (cadr a)))
651 hi-lock-interactive-lighters)))
652 default))
653 defaults))
654 (list 652 (list
655 (completing-read (if (null defaults) 653 (completing-read (if (null defaults)
656 "Regexp to unhighlight: " 654 "Regexp to unhighlight: "
@@ -767,7 +765,8 @@ SPACES-REGEXP is a regexp to substitute spaces in font-lock search."
767 (list subexp (list 'quote face) 'prepend))) 765 (list subexp (list 'quote face) 'prepend)))
768 (no-matches t)) 766 (no-matches t))
769 ;; Refuse to highlight a text that is already highlighted. 767 ;; Refuse to highlight a text that is already highlighted.
770 (if (assoc regexp hi-lock-interactive-patterns) 768 (if (or (assoc regexp hi-lock-interactive-patterns)
769 (assoc (or lighter regexp) hi-lock-interactive-lighters))
771 (add-to-list 'hi-lock--unused-faces (face-name face)) 770 (add-to-list 'hi-lock--unused-faces (face-name face))
772 (push pattern hi-lock-interactive-patterns) 771 (push pattern hi-lock-interactive-patterns)
773 (push (cons (or lighter regexp) pattern) hi-lock-interactive-lighters) 772 (push (cons (or lighter regexp) pattern) hi-lock-interactive-lighters)
@@ -792,7 +791,7 @@ SPACES-REGEXP is a regexp to substitute spaces in font-lock search."
792 (let ((overlay (make-overlay (match-beginning subexp) 791 (let ((overlay (make-overlay (match-beginning subexp)
793 (match-end subexp)))) 792 (match-end subexp))))
794 (overlay-put overlay 'hi-lock-overlay t) 793 (overlay-put overlay 'hi-lock-overlay t)
795 (overlay-put overlay 'hi-lock-overlay-regexp regexp) 794 (overlay-put overlay 'hi-lock-overlay-regexp (or lighter regexp))
796 (overlay-put overlay 'face face)) 795 (overlay-put overlay 'face face))
797 (goto-char (match-end 0))) 796 (goto-char (match-end 0)))
798 (when no-matches 797 (when no-matches
diff --git a/test/lisp/hi-lock-tests.el b/test/lisp/hi-lock-tests.el
index dd2c28053a0..252caaa2650 100644
--- a/test/lisp/hi-lock-tests.el
+++ b/test/lisp/hi-lock-tests.el
@@ -33,7 +33,9 @@
33 (car defaults)))) 33 (car defaults))))
34 (dotimes (_ 2) 34 (dotimes (_ 2)
35 (let ((face (hi-lock-read-face-name))) 35 (let ((face (hi-lock-read-face-name)))
36 (hi-lock-set-pattern "a" face)))) 36 ;; This test should use regexp "b" different from "a"
37 ;; used in another test because hi-lock--hashcons is global.
38 (hi-lock-set-pattern "b" face))))
37 (should (equal hi-lock--unused-faces (cdr faces)))))) 39 (should (equal hi-lock--unused-faces (cdr faces))))))
38 40
39(ert-deftest hi-lock-test-set-pattern () 41(ert-deftest hi-lock-test-set-pattern ()
@@ -48,5 +50,103 @@
48 ;; Only one match, then we have used just 1 face 50 ;; Only one match, then we have used just 1 face
49 (should (equal hi-lock--unused-faces (cdr faces)))))) 51 (should (equal hi-lock--unused-faces (cdr faces))))))
50 52
53(ert-deftest hi-lock-case-fold ()
54 "Test for case-sensitivity."
55 (let ((hi-lock-auto-select-face t))
56 (with-temp-buffer
57 (insert "a A b B\n")
58
59 (dotimes (_ 2) (highlight-regexp "[a]"))
60 (should (= (length (overlays-in (point-min) (point-max))) 2))
61 (unhighlight-regexp "[a]")
62 (should (= (length (overlays-in (point-min) (point-max))) 0))
63
64 (dotimes (_ 2) (highlight-regexp "[a]" nil nil "a"))
65 (should (= (length (overlays-in (point-min) (point-max))) 2))
66 (unhighlight-regexp "a")
67 (should (= (length (overlays-in (point-min) (point-max))) 0))
68
69 (dotimes (_ 2) (highlight-regexp "[A]" ))
70 (should (= (length (overlays-in (point-min) (point-max))) 1))
71 (unhighlight-regexp "[A]")
72 (should (= (length (overlays-in (point-min) (point-max))) 0))
73
74 (dotimes (_ 2) (highlight-regexp "[A]" nil nil "A"))
75 (should (= (length (overlays-in (point-min) (point-max))) 1))
76 (unhighlight-regexp "A")
77 (should (= (length (overlays-in (point-min) (point-max))) 0))
78
79 (let ((case-fold-search nil)) (dotimes (_ 2) (highlight-regexp "[a]")))
80 (should (= (length (overlays-in (point-min) (point-max))) 1))
81 (unhighlight-regexp "[a]")
82 (should (= (length (overlays-in (point-min) (point-max))) 0))
83
84 (dotimes (_ 2) (highlight-phrase "a a"))
85 (should (= (length (overlays-in (point-min) (point-max))) 1))
86 (unhighlight-regexp "a a")
87 (should (= (length (overlays-in (point-min) (point-max))) 0))
88
89 (let ((search-spaces-regexp search-whitespace-regexp)) (highlight-regexp "a a"))
90 (should (= (length (overlays-in (point-min) (point-max))) 1))
91 (cl-letf (((symbol-function 'completing-read)
92 (lambda (_prompt _coll _x _y _z _hist defaults)
93 (car defaults))))
94 (call-interactively 'unhighlight-regexp))
95 (should (= (length (overlays-in (point-min) (point-max))) 0))
96
97 (emacs-lisp-mode)
98 (setq font-lock-mode t)
99
100 (dotimes (_ 2) (highlight-regexp "[a]"))
101 (font-lock-ensure)
102 (should (memq 'hi-yellow (get-text-property 1 'face)))
103 (should (memq 'hi-yellow (get-text-property 3 'face)))
104 (let ((font-lock-fontified t)) (unhighlight-regexp "[a]"))
105 (should (null (get-text-property 3 'face)))
106
107 (dotimes (_ 2) (highlight-regexp "[a]" nil nil "a"))
108 (font-lock-ensure)
109 (should (memq 'hi-yellow (get-text-property 1 'face)))
110 (should (memq 'hi-yellow (get-text-property 3 'face)))
111 (let ((font-lock-fontified t)) (unhighlight-regexp "a"))
112 (should (null (get-text-property 3 'face)))
113
114 (dotimes (_ 2) (highlight-regexp "[A]" ))
115 (font-lock-ensure)
116 (should (null (get-text-property 1 'face)))
117 (should (memq 'hi-yellow (get-text-property 3 'face)))
118 (let ((font-lock-fontified t)) (unhighlight-regexp "[A]"))
119 (should (null (get-text-property 3 'face)))
120
121 (dotimes (_ 2) (highlight-regexp "[A]" nil nil "A"))
122 (font-lock-ensure)
123 (should (null (get-text-property 1 'face)))
124 (should (memq 'hi-yellow (get-text-property 3 'face)))
125 (let ((font-lock-fontified t)) (unhighlight-regexp "A"))
126 (should (null (get-text-property 3 'face)))
127
128 (let ((case-fold-search nil)) (dotimes (_ 2) (highlight-regexp "[a]")))
129 (font-lock-ensure)
130 (should (memq 'hi-yellow (get-text-property 1 'face)))
131 (should (null (get-text-property 3 'face)))
132 (let ((font-lock-fontified t)) (unhighlight-regexp "[a]"))
133 (should (null (get-text-property 1 'face)))
134
135 (dotimes (_ 2) (highlight-phrase "a a"))
136 (font-lock-ensure)
137 (should (memq 'hi-yellow (get-text-property 1 'face)))
138 (let ((font-lock-fontified t)) (unhighlight-regexp "a a"))
139 (should (null (get-text-property 1 'face)))
140
141 (let ((search-spaces-regexp search-whitespace-regexp)) (highlight-regexp "a a"))
142 (font-lock-ensure)
143 (should (memq 'hi-yellow (get-text-property 1 'face)))
144 (cl-letf (((symbol-function 'completing-read)
145 (lambda (_prompt _coll _x _y _z _hist defaults)
146 (car defaults)))
147 (font-lock-fontified t))
148 (call-interactively 'unhighlight-regexp))
149 (should (null (get-text-property 1 'face))))))
150
51(provide 'hi-lock-tests) 151(provide 'hi-lock-tests)
52;;; hi-lock-tests.el ends here 152;;; hi-lock-tests.el ends here