2010-05-12 65 views
16

我正在处理一个自定义的.emacs文件,我可以在多个不同的计算机上使用它。我希望能够加载一个模式,如果它存在于系统上。如果它不存在,我希望Emacs停止显示错误:File error: Cannot open load file, X在Lisp中,避免在使用时需要“无法打开加载文件”

例如:

(require 'darkroom-mode) 

结果:

File error: Cannot open load file, darkroom-mode 

我使用file-exists-p来测试是否存在其他一些文件,但这个测试我会承担我需要寻找我的负荷路径。我是Lisp的新手,所以这是困扰我的。

+3

或者,只是'(忽略错误(需要'不管))'。 – jrockway 2010-05-12 10:53:28

回答

25

如果你只是想继续require自发出一个错误,你可以这样做:

; third arg non-nil means to not signal an error if file not found 
; a symbol provides documentation at the invocation (and is non-nil) 
(require 'darkroom-mode nil 'noerror) 

从文档(章˚F需要RET):

require is a built-in function in `C source code'. 

(require feature &optional filename noerror) 

If feature feature is not loaded, load it from filename. 
If feature is not a member of the list `features', then the feature 
is not loaded; so load the file filename. 
If filename is omitted, the printname of feature is used as the file name, 
and `load' will try to load this name appended with the suffix `.elc' or 
`.el', in that order. The name without appended suffix will not be used. 
If the optional third argument noerror is non-nil, 
then return nil if the file is not found instead of signaling an error.