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

From: Jambunathan K <[email-masked]>
Date: Thu, 11 Oct 2012 03:38:51 +0530

Here is a patch for Part-1/Item-2.  This patch applies ON TOP OF
bug11095-part1.patch attached with
http://debbugs.gnu.org/cgi/bugreport.cgi?bug=11095#8)

=== modified file 'lisp/ChangeLog'
--- lisp/ChangeLog	2012-10-10 20:23:49 +0000
+++ lisp/ChangeLog	2012-10-10 21:59:39 +0000
@@ -1,5 +1,11 @@
 2012-10-10  Jambunathan K  <[email-masked]>
 
+	* hi-lock.el (hi-lock-auto-select-face): New user variable.
+	(hi-lock-auto-select-face-defaults): New buffer local variable.
+	(hi-lock-read-face-name): Honor `hi-lock-auto-select-face'.
+
+2012-10-10  Jambunathan K  <[email-masked]>
+
 	* hi-lock.el (hi-yellow, hi-pink, hi-green, hi-blue, hi-black-b)
         (hi-blue-b, hi-green-b, hi-red-b, hi-black-hb): Mark these faces
 	as obsolete.

=== modified file 'lisp/hi-lock.el'
--- lisp/hi-lock.el	2012-10-10 20:23:49 +0000
+++ lisp/hi-lock.el	2012-10-10 21:44:28 +0000
@@ -135,6 +135,14 @@ patterns."
 ;; It can have a function value.
 (put 'hi-lock-file-patterns-policy 'risky-local-variable t)
 
+(defcustom hi-lock-auto-select-face nil
+  "Non-nil if highlighting commands should not prompt for face names.
+When non-nil, each hi-lock command will cycle through faces in
+`hi-lock-face-defaults'."
+  :type 'boolean
+  :group 'hi-lock
+  :version "24.3")
+
 (defgroup hi-lock-faces nil
   "Faces for hi-lock."
   :group 'hi-lock
@@ -221,8 +229,15 @@ patterns."
     "hi-lock-6" "hi-lock-7" "hi-lock-8" "hi-lock-9")
   "Default faces for hi-lock interactive functions.")
 
-;;(dolist (f hi-lock-face-defaults)
-;;  (unless (facep f) (error "%s not a face" f)))
+(defvar hi-lock-auto-select-face-defaults
+  (let ((l (copy-sequence hi-lock-face-defaults)))
+    (setcdr (last l) l))
+  "Circular list of faces used for interactive highlighting.
+When `hi-lock-auto-select-face' is non-nil, use the face at the
+head of this list for next interactive highlighting.  See also
+`hi-lock-read-face-name'.")
+
+(make-variable-buffer-local 'hi-lock-auto-select-face-defaults)
 
 (define-obsolete-variable-alias 'hi-lock-regexp-history
                                 'regexp-history
@@ -577,7 +592,15 @@ not suitable."
     regexp))
 
 (defun hi-lock-read-face-name ()
-  "Read face name from minibuffer with completion and history."
+  "Return face name for interactive highlighting.
+When `hi-lock-auto-select-face' is non-nil, return head of
+`hi-lock-auto-select-face-defaults'.  Otherwise, read face name
+from minibuffer with completion and history."
+  (if hi-lock-auto-select-face
+      ;; Return current head and rotate the face list.
+      (prog1 (car hi-lock-auto-select-face-defaults)
+	(setq hi-lock-auto-select-face-defaults
+	      (cdr hi-lock-auto-select-face-defaults)))
   (intern (completing-read
            "Highlight using face: "
            obarray 'facep t
@@ -590,7 +613,7 @@ not suitable."
                             (not (equal prefix (car hi-lock-face-defaults))))
                        (length prefix) 0)))
            'face-name-history
-	   (cdr hi-lock-face-defaults))))
+	     (cdr hi-lock-face-defaults)))))
 
 (defun hi-lock-set-pattern (regexp face)
   "Highlight REGEXP with face FACE."

> Proposal is in two parts.  Part-I deals with `hi-lock-face-buffer'.
> Part-II deals with `unhighlight-regexp'.  Part-III has a dump of the
> current customization I have in my .emacs.
>
> I believe that my proposal is useful in general.  So I request that it
> be folded in to Emacs-24.1.
>
> Part-I: `hi-lock-face-buffer' & Co.
> ----------------------------------
>
> 1) Review the face names used in `hi-lock-face-defaults' and make the
>    faces customizable.  The defaults may not look good on a user's his
>    own font-lock configuration.
>
> 2) Make `hi-lock-face-buffer' use a different face on each invocation.  
>
>    Here is a real-world usecase for the above request.
>
>    As a programmer, I use highlighting to trace variable dependencies
>    within a function.  For example, in the example below, after
>    highlighting the variables in __different__ faces, I will come to the
>    conclusion that "a" depends on "d" and "tmp".
>
>      c = d;
>      b = c + tmp;
>      a = b;
>
>    And I use this very often to track variables and how they get their
>    values from.
>
>    If I were to use the default Emacs provided behaviour then I would
>    have to press M-n multiple times as I highlight more and more
>    symbols. (Typically I have 3-5 symbols highlighted before I turn off
>    highlighting.)
>
> See elisp snippet at the end of the mail.
>
>
> Part-II: `unhighlight-regexp'
> ------------------------------
>
> See usecase in Part-I/Item-2 
>
> 1) I want to selectively turn-off highlighting for certain regexps
>    (arguably) that _specific_ highlighted regexp cursor is stationed on.
>    This would happen when I decide that I don't want to factor a
>    particular variable for my current refactoring exercise.
>
>    I find the current behaviour of `unhighlight-regexp' slightly
>    tedious.  
>
>    1. There is no completion for which regexp I want to unhighlight and
>       I have to circle through `hi-lock-interactive-patterns'.
>
>    2. Browsing the `hi-lock-interactive-patterns' is tedious for the
>       following reasons:
>
>       - The order in which unhighlighting happens could totally be
>         unrelated to the order in which highlighting happens.  When I am
>         analyzing the variable flow, I don't want to do a "context
>         switch" trying to find out what item to choose from the
>         `unhighlight-regexp' menu.
>
> 2) I want to unhighlight all regexps.  This happens when I am done with
>    variable analysis.
>
>
> ps: I am not questioning the current defaults.  I am only saying that it
> the current behaviour is too generic to be immediately useful (atleast
> for my usecase) and so needs some sort of extra augmentation.
>
> Part-III: Elisp snippet
> -----------------------
>
> ;; Why should the color of the faces be encoded in the variable name?
> ;; Seems counter-intutitive to me.  I cannot relate to a hi-yellow
> ;; face customized to render in red.
>
> ;; (defvar hi-lock-face-defaults
> ;;   '("hi-yellow" "hi-pink" "hi-green" "hi-blue" "hi-black-b"
> ;;     "hi-blue-b" "hi-red-b" "hi-green-b" "hi-black-hb")
> ;;   "Default faces for hi-lock interactive functions.")
>
> ;; So roll out my own face for highlighting.  Make them
> ;; __customizable__.  Note that the name of the face doesn't say what
> ;; the color of the highlight will be.  Works for the color theme that
> ;; I have.
> (custom-set-faces
>  '(highlight1 ((t (:background "yellow" :foreground "black"))))
>  '(highlight2 ((t (:background "OrangeRed" :foreground "black"))))
>  '(highlight3 ((((class color) (background dark)) (:background "AntiqueWhite" :foreground "black"))))
>  '(highlight4 ((t (:background "SystemHotTrackingColor"))))
>  '(highlight5 ((t (:background "VioletRed" :foreground "black")))))
>
> ;; override the Emacs default
> (setq hi-lock-face-defaults
>       '("highlight1" "highlight2" "highlight3" "highlight4" "highlight5"))
>
> (defvar hi-lock-faces
>   (let ((l (copy-list hi-lock-face-defaults)))
>     (setcdr (last l) l))
>   "Circular list of faces in `hi-lock-face-defaults'.")
>
> ;; make `hi-lock-faces' buffer local
> (make-variable-buffer-local 'hi-lock-faces)
>
> (defun highlight-symbol ()
>   "Highlight symbol at point.  
> For illustartion only.  Note the use of `hi-lock-face-buffer'.
> Choose a new face from `hi-lock-faces' on each invocation.
> Overrides the default behaviour in vanilla Emacs which is to use
> the face at the head of the list."
>   (interactive)
>   (hi-lock-face-buffer
>    (concat "\\_<" (regexp-quote (thing-at-point 'symbol)) "\\_>")
>    ;; rotate the face list
>    (prog1 (car hi-lock-faces)
>      (setq hi-lock-faces (cdr hi-lock-faces)))))
>
> (defun my-unhighlight-regexp (arg)
>   "Wrapper around `unhighlight-regexp'.
> With a prefix argument, turn off all highlighting.
>
> TODO: Check if the symbol is right now on a highlighted regexp.
> If yes, unhighlight only that regexp."
>   (interactive "P")
>   (if arg
>       (mapc (lambda (p)
> 	      (unhighlight-regexp (car p)))
> 	    hi-lock-interactive-patterns)
>     (call-interactively 'unhighlight-regexp)))
>
>
>
>

--