I'm working on a small function to recursively load my Emacs customizations. In writing it, I noticed the argument of load, MUST-SUFFIX, requires the FILE argument to not include a directory name to capitalize on the suffix "autodetection" behavior (I only want it to load based on load-suffixes).

I thought I'd try temporarily setting load-path, since the description of load also mentions:

This function searches the directories in `load-path'.

When I tried setting load-path in a let to the current directory, it wouldn't load any of the files. However, when I temporarily appended it to the beginning of load-path, it seemed to work.

I'm wondering what the difference is. Since I am making use of functions from dash.el and cl-lib, do these need to be in load-path?

Here is what I wrote (sorry it's a bit jumbled, I threw it together rather quickly, since my current solution for the same task much slower):

(defun my-load-recursive (directory)
  (let ((load-path (cons directory load-path))
         (->> (directory-files directory nil nil 'nosort)
               (lambda (element)
                 (or (string-equal element ".")
                     (string-equal element ".."))))
               (lambda (file)
                  (expand-file-name file directory)))))))
    (destructuring-bind (files dirs) dir-contents
         (lambda (file)
           (when (load file 'noerror 'nomessage nil 'mustsuffix)
             (expand-file-name file directory)))
         (delete-dups (mapcar 'file-name-base files))))
         (lambda (dir)
           (expand-file-name dir directory))
  • 5
    I'm having trouble understanding what you want. Can you give us, for example, a directory structure, a sample load-path, an elisp command you expect to load one of your files, what happens when you try to load it, and what you expect to happen? – zck Sep 8 '15 at 20:06
  • 1
    Re: "When I tried setting load-path in a let to the current directory, it wouldn't load any of the files. ", did you let-bind load-path to a list ? Or just to a directory path string? (And if you're going to clobber it completely, be very careful that nothing you're loading will itself try to load/require something not in your directory.) – phils Sep 8 '15 at 23:40
  • @phils ...What you recommended was what I needed. I replaced (let ((load-path (cons directory load-path)))) with (let ((load-path (list directory)))), and it gave me the output I was looking for. If you create an answer from your comment, I can accept it. – bitflips Sep 9 '15 at 3:33


Make sure that load-path is still a list, and not just a directory path string.

e.g. this:

(let ((load-path (list "/path/to/directory")))

and never this:

(let ((load-path "/path/to/directory"))

In addition, remember that libraries very commonly require (or otherwise load, potentially indirectly) other libraries, and as such, clobbering the load-path completely is not necessarily a good idea.

Directories in load-path are looked at in the order of the list, so consing your directory to the front of the list (exactly as you ended up doing) will give that directory priority when Emacs looks for libraries, and is therefore going to be a more robust approach in general:

(let ((load-path (cons "/path/to/directory" load-path)))
| improve this answer | |

Your Answer

