0

I need to write a macro, with-temp-highlight-regexps, that evaluates BODY and than restores the previous highlighted regexps (created with the highlight-regexp function). I thought I could do it by let-binding the variable in which this kind of highlights are stored. I read the hi-lock.el file but I'm not so skilled to find the info I need.

Any suggestion?

EDIT. I tried this approach following phils' suggestion:

(defun my-temp-highlights () 
   (interactive)

   (highlight-regexp "STRING 1")

   (let ((hi-lock-interactive-patterns hi-lock-interactive-patterns))
     (highlight-regexp "STRING 2")

     (read-string "\"RETURN\" TO QUIT"))
   )

Running this function should keep STRING 1 highlighted but the STRING 2 highlighting should turn off, keeping the highlight only until RETURN. The strange behaviour (for me) of this function is that the STRING 2 highlight is non turned off when I run hi-lock-mode.

(I know I coud use unhilight-regexp but it is not what I'm searching for.)

EDIT 2. I also the following code to understand what happens:

(defun my-temp-highlights () 
   (interactive)

   (highlight-regexp "STRING 1")
   (princ hi-lock-interactive-patterns)
   (sit-for 5)

   (let ((BACKUP_VAR hi-lock-interactive-patterns))
     (highlight-regexp "STRING 2")
     (princ hi-lock-interactive-patterns)
     (sit-for 5)
     (read-string "ENTER TO QUIT")
     (setq hi-lock-interactive-patterns BACKUP_VAR))

   (princ hi-lock-interactive-patterns)
   (sit-for 5)

     )

But I'm not able to figure out why the second regexp (STRING 2) keeps to be highlighted when the hi-lock-interactive-patterns value is restored. (And why hi-lock-mode turn off does NOT effect it's higlighting).

1

I think you're looking for C-hv hi-lock-interactive-patterns


Following the question edit...

The strange behaviour (for me) of this function is that the STRING 2 highlight is non turned off

Ultimately hi-lock is using the font-lock machinery, and so the patterns get added to (but not removed from) font-lock-keywords with the way your code was working. Perhaps in your use-case you want to be using that more directly.

I know I coud use unhilight-regexp but it is not what I'm searching for.

Given that you are calling read-string -- at which point other code (e.g. process filters) may be running -- it may be preferable to use that approach.

You could adapt your function like so:

(defun my-temp-highlights () 
  (interactive)

  (highlight-regexp "STRING 1")

  (let ((pattern "STRING 2"))
    (unwind-protect
        (progn
          (highlight-regexp pattern)
          (read-string "\"RETURN\" TO QUIT"))
      (unhighlight-regexp pattern))))
  • Please take a look at the editing of my question, maybe you could help me. – Gabriele Nicolardi Dec 15 '18 at 17:41
  • Ok, the font-lock-keywords hint helped to realize my purpose. Thanks. (Take a look at my answer.) – Gabriele Nicolardi Dec 16 '18 at 0:56
2

A way to think about figuring that out is to wonder what code in hi-lock needs to know what has been highlighted. An obvious place is in the code for unhighlighting, since it prompts for a choice of regexps that have already been highlighted. If you look at the function, you can probably chase it down from there.

0

Following phils' hints I wrote this macro that fits my purpose:

(defmacro with-temp-highlights (&rest body)      
  `(unwind-protect
       (let ((font-lock-keywords font-lock-keywords)
             (hi-lock-interactive-patterns hi-lock-interactive-patterns))
     ,@body
     )

     ;; *DUMMY HIGHLIGHT* 
     ;; I need it to reapply the highlights stored in the
     ;; `font-lock-keywords` and `hi-lock-interactive-patterns`
     ;; variables
     (highlight-regexp "MY DUMMY REGEXP")
     (unhighlight-regexp "MY DUMMY REGEXP")
     ))

It makes temporary the regexp highlighs in BODY keeping all the preexisting highlights.

You can test it evaluating:

(highlight-regexp "Chicchirichì")

(with-temp-highlights
 (highlight-regexp "Coccodè")
 (sit-for 3))

My only perplexity is about the DUMMY HIGHLIGHT stuff, but I didn't found and alternative way to reset/reload the 2 let-binded variables.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.