在 elisp 中编译变量

Compiling variables in elisp

如何在 elisp 中编译作为函数调用结果的变量?是否需要将 eval-when-compile 添加到所有变量的主体中,或者是否可以通过某种方式确保相同的结果而无需在所有变量定义中重写它?

用例是编译我在机器之间变化的局部变量。例如,

(defun setup-defaults (loc)
  (when (eq system-type 'windows-nt)
    (cond
     ((file-exists-p (expand-file-name loc "~"))
      (file-name-as-directory (expand-file-name loc "~")))
     ((file-exists-p (expand-file-name loc "d:/"))
      (file-name-as-directory (expand-file-name loc "d:/"))))))

(defconst my/org (setup-defaults "org"))

(defconst my/home
  (eval-when-compile
    (file-name-directory
     (file-chase-links (or load-file-name "~/.emacs.d/init.el")))))

变量 my/home 将被编译为“~/.emacs.d/”,但 my/org 不会在字节编译中被计算,除非我将其重写为

(defconst my/org (eval-when-compile (setup-defaults "org")))

那么,我是否需要对所有变量都这样做?

啊,没关系我看到这就是宏的用途,只需使用 defmacro 似乎就可以解决问题,

(defmacro setup-defaults (loc)
  (when (eq system-type 'windows-nt)
    (cond
     ((file-exists-p (expand-file-name loc "~"))
      (file-name-as-directory (expand-file-name loc "~")))
     ((file-exists-p (expand-file-name loc "d:/"))
      (file-name-as-directory (expand-file-name loc "d:/"))))))