2010-05-12 63 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.