5

I have several .el files within my "~/.emacs.d" directory and I added the following lines to my .emacs file to load them at startup:

(let ((base "~/.emacs.d/")
      (files '("user.el" "erlang.el" "sbcl-slime.el"))
      (bfload (lambda (file) (load (expand-file-name (concat base file))))))
   (mapcar bfload files))

It works, but is this proper Emacs Lisp style? How can this be improved, please?

Stefan
  • 27,908
  • 4
  • 53
  • 82
Boris Mühmer
  • 476
  • 3
  • 10
  • 3
    Normally you would add them to the `load-path` and then `require` (this implies that the file you are loading `provide`s something.) Some prefer not to require the files until the corresponding mode would be loaded: [More info](http://stackoverflow.com/questions/6935908/emacs-best-practice-for-lazy-loading-modes-in-emacs). –  Jun 22 '13 at 12:32
  • @wvxvw Actually those `*.el` files are just wrappers for _providers_ (only setting up correct pathes for them) and I just didn't want to clutter the `.emacs` file. – Boris Mühmer Jun 22 '13 at 12:56

3 Answers3

12

First, don't put your .el files directly into ~/.emacs.d (Emacs puts various files in there, and they're not expected to be Elisp packages). You can put them into ~/.emacs.d/pkgs for example, instead.

How 'bout:

(dolist (file '("user.el" "erlang.el" "sbcl-slime.el"))
  (load (expand-file-name file "~/.emacs.d/pkgs"))
Stefan
  • 27,908
  • 4
  • 53
  • 82
2

You can mix Stefan's excellent suggestions of moving those files to a separate directory with init-loader https://github.com/emacs-jp/init-loader

You will have a couple of extra perks (auto byte-compiling the files) and you won't need to maintain the file list (just move/create a file in that directory).

juanleon
  • 9,220
  • 30
  • 41
  • The `init-loader` is a very interesting project, but just a bit to bloated to what I had in mind. But thank You for the pointer! – Boris Mühmer Jun 24 '13 at 19:47
1

Based on Stefan's example, I only add a file-exists-p:

(dolist (file '("user.el" "erlang.el" "sbcl-slime.el"))
  (let ((f (expand-file-name file "~/.emacs.d/pkgs")))
    (if (file-exists-p f)
      (load f))))

I think, this is the version I will use.

Boris Mühmer
  • 476
  • 3
  • 10
  • 2
    `file-exists-p` is a fine idea. You could also use the `noerror` argument of `load` to get the same result. – Stefan Jun 25 '13 at 14:08