0

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))
        (dir-contents
         (->> (directory-files directory nil nil 'nosort)
              (-remove
               (lambda (element)
                 (or (string-equal element ".")
                     (string-equal element ".."))))
              (-separate
               (lambda (file)
                 (file-regular-p
                  (expand-file-name file directory)))))))
    (destructuring-bind (files dirs) dir-contents
      (nconc
       (delq
        nil
        (mapcar
         (lambda (file)
           (when (load file 'noerror 'nomessage nil 'mustsuffix)
             (expand-file-name file directory)))
         (delete-dups (mapcar 'file-name-base files))))
       (car
        (mapcar
        #'my-load-recursive
        (mapcar
         (lambda (dir)
           (expand-file-name dir directory))
         dirs)))))))
3
  • 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, 2015 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, 2015 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.
    – user2267
    Sep 9, 2015 at 3:33

1 Answer 1

2

When I tried setting load-path in a let to the current directory, it wouldn't load any of the files.

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.