【问题标题】:In Lisp, Avoid "Cannot open load file" when using require在 Lisp 中,使用 require 时避免“无法打开加载文件”
【发布时间】:2011-02-18 10:48:25
【问题描述】:

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

例如:

(require 'darkroom-mode)

结果:

File error: Cannot open load file, darkroom-mode

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

【问题讨论】:

  • 或者,只是(ignore-errors (require 'whatever))

标签: emacs lisp elisp dot-emacs


【解决方案1】:

如果您只是想防止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 需要 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.

【讨论】:

    猜你喜欢
    • 2013-02-17
    • 2011-09-14
    • 2017-05-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多