如何在emacs宏中避免急切的宏扩展失败?

Joh*_*hin 3 emacs macros

我定义了这个宏:

(defmacro with-current-directory (directory &rest body)
  "Set the working directory temporarily set to DIRECTORY and run BODY.
DIRECTORY is expanded"
  `(let ((default-directory
          ,(file-name-as-directory
            (expand-file-name (eval directory)))))
     ,@body))
Run Code Online (Sandbox Code Playgroud)

我在emacs打开时加载的一些lisp函数中使用它.我总是得到这些警告:

Eager macro-expansion failure: (void-variable repo-dir)
Eager macro-expansion failure: (wrong-type-argument stringp nil)
Run Code Online (Sandbox Code Playgroud)

据我所知,因为这些变量没有在加载时定义,而emacs正试图评估它们.我的问题是,我如何避免收到这些警告.有没有办法定义宏,以便不会发生?我无法弄清楚如何使用变量的值,而不是变量本身的符号.

leg*_*cia 5

像这样:

`(let ((default-directory
        (file-name-as-directory (expand-file-name ,directory))))
Run Code Online (Sandbox Code Playgroud)

由于directory不是值,而是将评估为值的lisp表达式,因此需要将表达式(使用逗号运算符)插入到宏的扩展中.如果在调用之前放置逗号,则file-name-as-directory必须能够基于表达式的标记在宏扩展时计算目录,如果directory引用变量名称,则无法执行此操作.