If I have a folder structure set up like this:
~/Projects
emacs
package1
package1-helpers
package2
package2-helpers
package2-more-helpers
package3
package3-helpers
How do I add these folders:
...to the load-path
from my .emacs file?
I basically need a short automated version of this code:
(add-to-list 'load-path "~/Projects/emacs")
(add-to-list 'load-path "~/Projects/emacs/package1")
(add-to-list 'load-path "~/Projects/emacs/package2")
(add-to-list 'load-path "~/Projects/emacs/package3")
(let ((base "~/Projects/emacs"))
(add-to-list 'load-path base)
(dolist (f (directory-files base))
(let ((name (concat base "/" f)))
(when (and (file-directory-p name)
(not (equal f ".."))
(not (equal f ".")))
(add-to-list 'load-path name)))))
Here's something I use in my .emacs:
(let* ((my-lisp-dir "~/.elisp/")
(default-directory my-lisp-dir)
(orig-load-path load-path))
(setq load-path (cons my-lisp-dir nil))
(normal-top-level-add-subdirs-to-load-path)
(nconc load-path orig-load-path))
If you look at the description for normal-top-level-add-subdirs-to-load-path, it's somewhat smart about picking which directories to exclude.
我建议你使用subdirs.el
This is my hacked up version :P
(defun add-to-list-with-subdirs (base exclude-list include-list)
(dolist (f (directory-files base))
(let ((name (concat base "/" f)))
(when (and (file-directory-p name)
(not (member f exclude-list)))
(add-to-list 'load-path name)
(when (member f include-list)
(add-to-list-with-subdirs name exclude-list include-list)))))
(add-to-list 'load-path base))
This will add all first level dirs from base and exclude the ones in exclude-list, while for the dirs in include-list, it will add all the first level dirs of that dir too.
(add-to-list-with-subdirs "~/.emacs.d" '("." ".." "backup") '("vendor" "my-lisp"))
This function will map over first level sub-folders and files in BASE-PATH and add it to the LOAD-LIST if it's a directory (excluding directories "." and "..").
(defun add-subdirs-to-load-path (base-path)
"Adds first level subfolders to LOAD-PATH.
BASE-PATH must not end with a '/'"
(mapc (lambda (attr)
(let ((name (car attr))
(folder-p (cadr attr)))
(unless (or (not folder-p)
(equal name ".")
(equal name ".."))
(add-to-list 'load-path (concat base-path "/" name)))))
(directory-files-and-attributes base-path)))
Install dash
and f
third-party libraries. Your needed function is f-directories
:
(f-directories "~/YOURDIR") ; return only immediate directories
(f-directories "~/YOURDIR" nil t) ; all directories recursively
Then use --each
to add each found directory to load-path
. This whole operation works in O(n²), but as load-path
is usually super-small, who cares.
(add-to-list 'load-path "~/YOURDIR") ; your parent folder itself
(--each (f-directories "~/YOURDIR") (add-to-list 'load-path it))
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.