Subject: Re: bug#40337: 28.0.50; Enable case-fold-search in hi-lock

From: Juri Linkov <[email-masked]>
Date: Mon, 06 Apr 2020 02:12:13 +0300

> I think it's a good interpretation of that docstring.  If needed
> we could additionally tweak the docstring to clarify the behavior.

While testing I found a problem: using 'unhighlight-regexp' ('M-s h u')
displays too long prompt:

  Regexp to unhighlight (default (closure ((case-fold . t) (subexp . 0) (face . hi-yellow) (regexp . foo) t) (limit) (let ((case-fold-search case-fold)) (re-search-forward regexp limit t)))): 

Then I tried to construct a closure *after* adding a plain regexp
to hi-lock-interactive-patterns, i.e. immediately in font-lock-add-keywords.

But this poses another problem: it's not easy to find a closure by regexp
in font-lock-keywords for removing a keyword by font-lock-remove-keywords
in 'unhighlight-regexp'.

I tried the patch below, and sometimes it works, but I know
it's horribly ugly, and it's a wrong direction to search the regexp
in the lexical environment of a closure.

Maybe then better to add an intermediate mapping to hi-lock
like there is in isearch: isearch-message vs isearch-string,
where isearch-message is user-facing representaion,
and isearch-string contains internal data.

This could help to solve another existing problem of using
hi-lock from isearch in char-fold mode, where unhighlight-regexp
displays unreadable prompt too:
  Regexp to unhighlight (default \(?:ḟ\|[fᶠḟⓕｆ𝐟𝑓𝒇𝒻𝓯𝔣𝕗𝖋𝖿𝗳𝘧𝙛𝚏]\)): 

diff --git a/lisp/hi-lock.el b/lisp/hi-lock.el
index de258935e1..9173b66b7f 100644
--- a/lisp/hi-lock.el
+++ b/lisp/hi-lock.el
@@ -625,7 +645,12 @@ hi-lock-unface-buffer
       ;; calls font-lock-set-defaults).  This is yet-another bug in
       ;; font-lock-add/remove-keywords, which we circumvent here by
       ;; testing `font-lock-fontified' (bug#19796).
-      (if font-lock-fontified (font-lock-remove-keywords nil (list keyword)))
+      (when font-lock-fontified
+        (font-lock-remove-keywords nil (list keyword))
+        (dolist (k font-lock-keywords)
+          (when (and (consp k) (consp (car k)) (eq (caar k) 'closure)
+                     (equal (car keyword) (cdr (assq 'regexp (cadr (car k))))))
+            (font-lock-remove-keywords nil (list k)))))
       (setq hi-lock-interactive-patterns
             (delq keyword hi-lock-interactive-patterns))
       (remove-overlays
@@ -728,7 +753,13 @@ hi-lock-set-pattern
       (push pattern hi-lock-interactive-patterns)
       (if (and font-lock-mode (font-lock-specified-p major-mode))
 	  (progn
-	    (font-lock-add-keywords nil (list pattern) t)
+	    (font-lock-add-keywords
+             nil (list (cons
+                        (lambda (limit)
+                          (let ((case-fold-search case-fold))
+                            (re-search-forward (car pattern) limit t)))
+                        (cdr pattern)))
+             t)
 	    (font-lock-flush))
         (let* ((range-min (- (point) (/ hi-lock-highlight-range 2)))
                (range-max (+ (point) (/ hi-lock-highlight-range 2)))