17

我正在处理一个自定义 .emacs 文件,我将能够在多台不同的计算机上使用该文件。如果系统上存在模式,我希望能够加载它。如果它不存在,我希望 Emacs 停止显示错误:File error: Cannot open load file, X.

例如:

(require 'darkroom-mode)

结果是:

File error: Cannot open load file, darkroom-mode

file-exists-p用来测试是否存在某些其他文件,但对于这个测试,我假设我需要搜索我的加载路径。我是 Lisp 的新手,所以这让我很困惑。

4

1 回答 1

26

如果您只想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) 

从文档(C-h f require 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.
于 2010-05-12T04:14:48.807 回答