0

I'm trying to create my first macro. I often change the background color of the query-replace matched string in my script. I usually do:

(let* ((face 'query-replace)
       (bg (face-background face))) 
  (unwind-protect
      (progn
    (set-face-background face "gold")
    (perform-replace "^%+\n" ""  t t nil 1 nil (point-min) (point-max)))
    (set-face-background face bg)))

Now I'd like to have a macro to do that. Something like:

(with-query-replace-color "gold"
  (perform-replace "^%+\n" ""  t t nil 1 nil (point-min) (point-max)))

I tried with:

(defmacro with-query-replace-color (COLOR &rest body)
  "Change bg color in `perform-replace'..."
  (declare (indent 1))
  (let* ((face 'query-replace)
     (bg (face-background face)))
    `(unwind-protect
     (progn
       (set-face-background ,face ,COLOR)
       ,@body) 
       (set-face-background ,face ,bg))))

I get:

Symbol’s value as variable is void: query-replace.
  1. What am I doing wrong?

  2. Should I use a function instead?

  • Why do you need a macro for that? Why won't a regular function do? – wasamasa Oct 23 '18 at 7:09
  • @wasamasa Because I don't know how to write functions that accept (&rest body) as argument. I thought it was non possible. Isn't so? – Gabriele Nicolardi Oct 23 '18 at 7:18
  • @wasamasa I edited the title of my question and added a second one to take in account what you pointed. – Gabriele Nicolardi Oct 23 '18 at 7:43
  • You can, but it wouldn't have the same effect (unless you were using a thunk). – wasamasa Oct 23 '18 at 8:01
  • Your error: not enough quoting (you actually don't need face variable, just write 'query-replace in the macro body). – wvxvw Oct 23 '18 at 9:26
0

I solved with:

(defmacro with-query-replace-color (COLOR &rest body)
  "Changes bg color in `perform-replace'..."
  (declare (indent 1))
  `(let* ((face `query-replace)
     (bg (face-background face))) 
      (unwind-protect
      (progn
        (set-face-background face ,COLOR)
        ,@body)
     (set-face-background face bg))))

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.