Subject: Re: bug#11095: [PATCH] Re: bug#11095: 24.0.94; hi-lock-face-buffer/unhighlight-regexp': Augment?

From: Stefan Monnier <[email-masked]>
Date: Mon, 10 Dec 2012 16:27:07 -0500

>  	 (let* ((hi-text
>  		 (buffer-substring-no-properties
> -		  (previous-single-property-change (point) 'face)
> -		  (next-single-property-change (point) 'face))))
> +		  (previous-single-char-property-change (point) 'face)
> +		  (next-single-char-property-change (point) 'face))))
>  	   ;; Compute hi-lock patterns that match the
>  	   ;; highlighted text at point.  Use this later in
>  	   ;; during completing-read.

But when overlays are used, the previous code should have found the
overlay already.  IIUC the above patch works only because of an
inconsistency between previous-single-property-change and
previous-single-char-property-change where the first may return nil
whereas the other always returns an integer.

But now that I look at this code I see that it doesn't work right for
its intended use case (i.e. when font-lock-mode is on) when point is at the
very start/end of the matched.

So I've installed a completely different patch instead (see below).
There should be an easier way to do it :-(

> -  (unless hi-lock-interactive-patterns
> -    (setq hi-lock--unused-faces hi-lock-face-defaults))
> +  (unless (or hi-lock-interactive-patterns hi-lock--unused-faces)
> +    ;; This is the very first request for interactive highlighting.
> +    ;; Initialize unused faces list.
> +    (setq hi-lock--unused-faces (copy-sequence hi-lock-face-defaults)))
[...]
> -      (setq hi-lock--unused-faces (remove face hi-lock--unused-faces))
> +      (setq hi-lock--unused-faces (delete face hi-lock--unused-faces))

Why?

        Stefan

=== modified file 'lisp/hi-lock.el'
--- lisp/hi-lock.el	2012-12-10 18:33:59 +0000
+++ lisp/hi-lock.el	2012-12-10 21:16:31 +0000
@@ -474,19 +474,33 @@
     (let ((regexp (get-char-property (point) 'hi-lock-overlay-regexp)))
       (when regexp (push regexp regexps)))
     ;; With font-locking on, check if the cursor is on a highlighted text.
-    (and (memq (face-at-point)
-               (mapcar #'hi-lock-keyword->face hi-lock-interactive-patterns))
+    (let ((face-after (get-text-property (point) 'face))
+          (face-before
+           (unless (bobp) (get-text-property (1- (point)) 'face)))
+          (faces (mapcar #'hi-lock-keyword->face
+                         hi-lock-interactive-patterns)))
+      (unless (memq face-before faces) (setq face-before nil))
+      (unless (memq face-after faces) (setq face-after nil))
+      (when (and face-before face-after (not (eq face-before face-after)))
+        (setq face-before nil))
+      (when (or face-after face-before)
 	 (let* ((hi-text
 		 (buffer-substring-no-properties
-		  (previous-single-property-change (point) 'face)
-		  (next-single-property-change (point) 'face))))
+                 (if face-before
+                     (or (previous-single-property-change (point) 'face)
+                         (point-min))
+                   (point))
+                 (if face-after
+                     (or (next-single-property-change (point) 'face)
+                         (point-max))
+                   (point)))))
 	   ;; Compute hi-lock patterns that match the
 	   ;; highlighted text at point.  Use this later in
 	   ;; during completing-read.
 	   (dolist (hi-lock-pattern hi-lock-interactive-patterns)
 	     (let ((regexp (car hi-lock-pattern)))
 	       (if (string-match regexp hi-text)
-		   (push regexp regexps))))))
+                  (push regexp regexps)))))))
     regexps))
 
 (defvar-local hi-lock--unused-faces nil