我想在正确安装特定包的同时自定义环境.如何检查elisp中是否安装了某个软件包?像这样的东西?:
(if (require 'ecb)
(progn (setq ....))
(message "ECB not installed!"))
Run Code Online (Sandbox Code Playgroud)
phi*_*ils 37
tripleee的答案是错误处理的一个方便示例,但在这种情况下是不必要的.
(when (require 'some-library nil 'noerror)
do-things)
Run Code Online (Sandbox Code Playgroud)
这'noerror可以是任何非零值,但当然这种方式更具描述性.我经常看到也:noerror用过,但我不知道在符号上使用关键字参数是否有任何特别的优势(评论,任何人?我很有兴趣知道).
require是一个内置函数
C source code.(需要FEATURE和可选的FILENAME NOERROR)
如果未加载功能FEATURE,请从FILENAME加载它.
如果FEATURE不是列表的成员, 则不加载features该功能
; 所以加载文件FILENAME.
如果省略文件名,特征的printname作为文件名,
并load会尝试加载这个名字后缀追加.elc或者
.el,按照这个顺序.不使用没有附加后缀的名称.
有关get-load-suffixes后缀的完整列表,请参阅参考资料.
如果可选的第三个参数NOERROR是非nil,
那么如果找不到该文件则返回nil,而不是发出错误信号.
通常返回值为FEATURE.
加载FILENAME的开始和结束时的正常消息被抑制.
(require)如果失败,将抛出错误.这应该就是你所需要的.
如果您只想在可用的情况下配置ECB的行为,请主要查看添加内容ecb-hook- 这是有条件地配置Emacs包的常规方法.
如果没有可用的挂钩,或者您想由于某种原因手动滚动它,请尝试类似的方法
(eval-after-load 'ecb '(setq ecb-be-more-like-better-yes-p t))
Run Code Online (Sandbox Code Playgroud)
如果你真的想手动滚动它,你可以捕获失败的错误,require如下所示:
(condition-case nil
(progn
(require 'ecb)
(setq ecb-be-more-like-better-yes-p t) )
(file-error (message "ECB not available; not configuring") ))
Run Code Online (Sandbox Code Playgroud)
请注意,将从内部condition-case捕获任何file-error内容,progn因此您要确保不要在其中执行任何其他文件操作.最终你可能只想把它放在require里面condition-case并用它作为原始if形式的条件,但这已经失控了......
(if (condition-case nil (require 'ecb) (error nil))
(setq ecb-be-more-like-better-yes-p t)
(message "ECB not available; not configuring") )
Run Code Online (Sandbox Code Playgroud)
小智 7
为什么不使用"featurep"?
(featurep FEATURE &optional SUBFEATURE)
Run Code Online (Sandbox Code Playgroud)
如果此Emacs中存在FEATURE,则返回t.
(if (featurep 'ecb)
(message "ECB is there!"))
Run Code Online (Sandbox Code Playgroud)