vy32
vy32

Reputation: 29655

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.

Upvotes: 4

Views: 1279

Answers (2)

nega
nega

Reputation: 2747

While @yorodm's answer is probably more correct (upvoted!), a form that I've been using for years is:

;; load & configure myfeature if it's available
(cond ((locate-library "myfeature")
       (require 'myfeature)
       (setq myfeature-variable "stuff")
       (do-my-thing))

Upvotes: 0

yorodm
yorodm

Reputation: 4461

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

Upvotes: 6

Related Questions