在 Lisp 中,避免“无法打开加载文件”当使用要求时

发布于 2024-09-01 01:08:03 字数 365 浏览 6 评论 0原文

我正在开发一个自定义 .emacs 文件,我将能够在几台不同的计算机上使用该文件。我希望能够加载系统上存在的模式。如果它不存在,我希望 Emacs 停止显示错误:文件错误:无法打开加载文件,X

例如:

(require 'darkroom-mode)

结果:

File error: Cannot open load file, darkroom-mode

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

I am working on a custom .emacs file that I will be able to use on several different computers. I would like to be able to load a mode if it exists on the system. If it does not exist I would like Emacs to stop showing the error: File error: Cannot open load file, X.

For example:

(require 'darkroom-mode)

Results in:

File error: Cannot open load file, darkroom-mode

I am using file-exists-p to test if certain other files exist but for this test I would assume I need to search my load-path. I am new to Lisp so this is stumping me.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

无人问我粥可暖 2024-09-08 01:08:03

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

从文档 (Ch 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.

If you just want to keep require from issuing an error, you can do:

; 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) 

From the documentation (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.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文