4

It's a very simple task, really. But all my google results come up with complex solutions. I must be blind or going about this wrong. Anyway...

Here's what I want:

I want to split up my emacs configuration in the following structure:

.emacs.d/
  init.el
  custom1/
  custom2/

I want init.el to be as minimal as possible, just something that says "load all .el files in custom1/ and custom2/, recursively". Whenever I put a new file in any of those two directories I want it to automatically be loaded when emacs is started.

It seems simple and easy to me. But I fail at finding a simple solution.

  • 2
    It looks like there is a solution about half-way down the page on the wiki: http://www.emacswiki.org/emacs/DotEmacsModular I Googled **recursive load el emacs** – lawlist Nov 26 '15 at 16:35

1 Answers1

0

Here's an answer based on the comment from @lawlist:

The file ~/.emacs.d/load-directory.el:

(defun load-directory (directory)
  "Load recursively all `.el' files in DIRECTORY."
  (dolist (element (directory-files-and-attributes directory nil nil nil))
    (let* ((path (car element))
           (fullpath (concat directory "/" path))
           (isdir (car (cdr element)))
           (ignore-dir (or (string= path ".") (string= path ".."))))
      (cond
       ((and (eq isdir t) (not ignore-dir))
        (load-directory fullpath))
       ((and (eq isdir nil) (string= (substring path -3) ".el"))
        (load (file-name-sans-extension fullpath)))))))

Then in ~/.emacs.d/init.el:

(load "~/.emacs.d/load-directory")
(load-directory "~/.emacs.d/custom1")
(load-directory "~/.emacs.d/custom2")
  • I think `load-directory` could be simplified to `(require 'find-lisp) (mapcar (lambda (fn) (load (file-name-sans-extension fn))) (find-lisp-find-files directory "\\.el\\'"))` – YoungFrog Nov 27 '15 at 11:06