在elisp中编译变量

时间:2016-06-29 02:02:27

标签: variables emacs initialization startup

如何在函数调用的结果中编译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")))

那么,我是否需要为所有变量做到这一点?

1 个答案:

答案 0 :(得分:1)

啊,没关系,我发现这就是宏的用途,只需使用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:/"))))))