Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Load emacs elisp library only if it is present

I have a standard .emacs file that I want to use on multiple machines. One some of the machines I am not able to load all of my elisp libraries.

Right now, I get this error on machines where, for example, wc-mode-0.2.el is not present:

Cannot open load file: wc-mode-0.2.el

Is there a way that I can make the .emacs file not error out at this point? Either by:

  1. Checking to see if the library exists before I try to load it.
  2. Catching the error condition.
like image 918
vy32 Avatar asked Oct 21 '25 14:10

vy32


1 Answers

Both load and require have a way of no signaling an error if a file is not found.

(load FILE &optional NOERROR NOMESSAGE NOSUFFIX MUST-SUFFIX)
(require FEATURE &optional FILENAME NOERROR)

So you can do this:

;; using load
(when (load "myfile.el" t)
       (do-my-thing))
;;using require
(when (require "myfeature" nil t)
       (do-my-thing))
like image 92
yorodm Avatar answered Oct 24 '25 07:10

yorodm



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!