如何实現這個宏?(原題目和问題实无关系)

我有一个语句想包装成宏。语句是这样的:

(add-hook 'evil-normal-state-entry-hook
          (lambda ()
            (set-face-attribute 'cursor nil :background moon-normal-state-cursor-color)
            ))

就是进入normal state的时候光标颜色换成指定的颜色

我写成宏是这样的

(defmacro change-cursor-on-hook| (hook color)
  `(add-hook 'hook (lambda ()
                     (set-face-attribute 'cursor nil :background color)
                     ))
  )

然而

(change-cursor-on-hook| evil-insert-state-entry-hook doom-blue)

以后什么都没有发生(语句是有效果的)

Message buffer里把宏打印出来了

((lambda nil (set-face-attribute (quote cursor) nil :background color)) (closure ((color . doom-blue) (hook quote evil-insert-state-entry-hook) t) nil (set-face-attribute (quote cursor) nil :background color)) (closure (t) nil (set-face-attribute (quote cursor) nil :background color)))

如果我把反引号删了,执行宏就会报错“

Debugger entered--Lisp error: (void-function closure)
  (closure ((color . doom-blue) (hook quote evil-insert-state-entry-hook) t) nil (set-face-attribute 'cursor nil :background color))
  ((closure ((color . doom-blue) (hook . evil-insert-state-entry-hook) t) nil (set-face-attribute 'cursor nil :background color)) (function (lambda nil (set-face-attribute 'cursor nil :background color))) (closure ((color . doom-blue) (hook quote evil-insert-state-entry-hook) t) nil (set-face-attribute 'cursor nil :background color)) (closure (t) nil (set-face-attribute 'cursor nil :background color)))
  eval(((closure ((color . doom-blue) (hook . evil-insert-state-entry-hook) t) nil (set-face-attribute 'cursor nil :background color)) (function (lambda nil (set-face-attribute 'cursor nil :background color))) (closure ((color . doom-blue) (hook quote evil-insert-state-entry-hook) t) nil (set-face-attribute 'cursor nil :background color)) (closure (t) nil (set-face-attribute 'cursor nil :background color))) t)

我没明白是哪出了问题。谁能指点一下我吗。

(defmacro change-cursor-on-hook| (hook color)
  `(add-hook (quote ,hook)
             (lambda ()
               (set-face-attribute 'cursor nil :background ,color))))

or (without backquote)

(defmacro change-cursor-on-hook| (hook color)
  (list 'add-hook (list 'quote hook)
             (list 'lambda ()
               (list 'set-face-attribute ''cursor nil :background color))))
1 个赞

懂了懂了,感谢。

',hook

(macroexpand `',(+ 1 2))
     => '3
1 个赞

主要为了针对答案保证可读。read macro 可以叠加我是知道的。