Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Lisp, Avoid "Cannot open load file" when using require

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.

like image 656
Jesse Vogt Avatar asked May 12 '10 03:05

Jesse Vogt


1 Answers

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.
like image 146
Trey Jackson Avatar answered Oct 03 '22 05:10

Trey Jackson