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

我有设置“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."))

然后在我的init.el中
;; load plugins with el-get
(require 'el-get-settings)

在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"))

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

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

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

问候,罗马

最佳答案

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

(eval-when-compile (defvar emacs-root)) ; defined in ~/.init.el

在冒犯表格之前的el-get-settings.el中。

或者,您可以将defvarinit.el移到el-get-settings.el

请注意,您可以在eval-when-compile中使用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.")

还要注意,问题中的原始defvar emacs-root如果损坏,则会在Windows上将变量emacs-root设置为"Path to where EMACS configuration root is."

关于emacs - elisp警告 "reference to free variable",我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/22898244/

10-11 23:02
查看更多