Search code examples
emacsconfiguration-files

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.

Solution

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