仅在存在的情况下加载emacs elisp库

时间:2017-06-06 14:01:01

标签: emacs configuration-files

我有一个标准的.emacs文件,我想在多台机器上使用。其中一些机器我无法加载我的所有elisp库。

现在,我在机器上遇到此错误,例如,wc-mode-0.2.el不存在:

Cannot open load file: wc-mode-0.2.el

有没有办法可以让.emacs文件在这一点上没有错误?通过:

  1. 在我尝试加载库之前检查库是否存在。
  2. 捕获错误情况。

2 个答案:

答案 0 :(得分:4)

如果找不到文件,loadrequire都会发出错误信号。

(load FILE &optional NOERROR NOMESSAGE NOSUFFIX MUST-SUFFIX)
(require FEATURE &optional FILENAME NOERROR)

所以你可以这样做:

;; using load
(when (load "myfile.el" t)
       (do-my-thing))
;;using require
(when (require "myfeature" nil t)
       (do-my-thing))

答案 1 :(得分:0)

虽然@ yorodm的答案可能更正确(赞成!),但我多年来一直使用的表格是:

;; load & configure myfeature if it's available
(cond ((locate-library "myfeature")
       (require 'myfeature)
       (setq myfeature-variable "stuff")
       (do-my-thing))