В Lisp Избегайте, “Не может открыть файл загрузки”, когда использование требует

Я работаю над пользовательским .emacs файлом, который я смогу использовать на нескольких различных компьютерах. Я хотел бы смочь загрузить режим, если он существует в системе. Если бы это не существует, я хотел бы, чтобы Emacs прекратил показывать ошибку: File error: Cannot open load file, X.

Например:

(require 'darkroom-mode)

Результаты в:

File error: Cannot open load file, darkroom-mode

Я использую file-exists-p протестировать, если бы определенные другие файлы существуют, но для этого теста я предположил бы, что должен искать свой путь загрузки. Я плохо знаком с Lisp, таким образом, это озадачивает меня.

17
задан Paul Nathan 12 May 2010 в 17:19
поделиться

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.
26
ответ дан 30 November 2019 в 13:04
поделиться
Другие вопросы по тегам:

Похожие вопросы: