elisp警告“对自由变量的引用”

Rom*_*kov 5 emacs elisp

我在徘徊如何摆脱过分警告。我的设置如下:

我有设置“ emacs-root”变量的init.el文件:

;; root of all emacs-related stuff
(defvar emacs-root
   (if (or (eq system-type 'cygwin)
      (eq system-type 'gnu/linux)
      (eq system-type 'linux)
      (eq system-type 'darwin))
        "~/.emacs.d/"    "z:/.emacs.d/"
     "Path to where EMACS configuration root is."))
Run Code Online (Sandbox Code Playgroud)

然后在我的init.el中

;; load plugins with el-get
(require 'el-get-settings)
Run Code Online (Sandbox Code Playgroud)

在el-get-settings.el中,我正在使用el-get加载软件包,并将“ el-get / el-get”文件夹附加到加载路径:

 ;; add el-get to the load path, and install it if it doesn't exist
 (add-to-list 'load-path (concat emacs-root "el-get/el-get"))
Run Code Online (Sandbox Code Playgroud)

问题是我对添加到列表的最后一个表达式中的'emacs-root'发出警告:“引用自由变量'emacs-root'”

我在这里做错了什么,有什么办法可以使编译器满意?

顺便说一句,这个设置可以正常工作-在加载期间,我没有任何问题,只是这个烦人的警告。

问候,罗马

sds*_*sds 5

在编译引用变量的文件时,emacs-root必须已经定义了变量。避免警告的最简单方法是添加

(eval-when-compile (defvar emacs-root)) ; defined in ~/.init.el
Run Code Online (Sandbox Code Playgroud)

el-get-settings.el违规表格之前。

或者,您可以defvar将从init.el移至el-get-settings.el

请注意,您可以使用eval-when-compilein defvar加快加载编译文件的速度(当然,如果这样做,则不应在平台之间复制编译文件):

(defvar emacs-root
  (eval-when-compile
    (if (or (eq system-type 'cygwin)
            (eq system-type 'gnu/linux)
            (eq system-type 'linux)
            (eq system-type 'darwin))
        "~/.emacs.d/"
        "z:/.emacs.d/"))
  "Path to where EMACS configuration root is.")
Run Code Online (Sandbox Code Playgroud)

还需要注意的是你原来defvar emacs-root的问题,如果坏了,它把变量emacs-root"Path to where EMACS configuration root is."Windows上。

  • 对`(defvar foo)`的评估是禁止操作的。所以(eval-when-compile(defvar foo))应该是空操作,因为顾名思义,它应该在编译期间简单地评估(defvar foo)。IOW不要将(defvar foo)包装在eval-when-compile中,因为它违背了它的目的。当然,实际上,它确实适用于歇斯底里的葡萄干,并且因为有太多人使用这种破碎形式,所以我们不得不使它起作用。 (2认同)