[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Adding Additional Trees to the Load Path

From: Fabrice Niessen
Subject: Re: Adding Additional Trees to the Load Path
Date: Wed, 28 Jan 2009 09:57:15 +0100
User-agent: Gnus/5.110009 (No Gnus v0.9) Emacs/23.0.60 (gnu/linux)

Hello Tim,

> I have a directory `.emacs.d` in my home directory where I put lisp
> libraries for extensions to Emacs.  Currently, I have to explicitly
> include each of the extensions in my `.emacs` file in order to get
> them to work.  I tried adding `.emacs.d` to my load-path via the
> following:
> (add-to-list 'load-path (expand-file-name "~/.emacs.d"))
> But that had no effect.  I would like behaviour similar to site-lisp
> where all of the trees under site-lisp are automitacally added.  Any
> way I can do this?

My solution is to define my own custom function for adding
directories to the `load-path':

--8<---------------cut here---------------start------------->8---
;; load-path enhancement
(defun my-add-to-load-path (this-directory &optional with-subdirs recursive)
  "Add THIS-DIRECTORY at the beginning of the load-path, if it exists.
Add all its subdirectories not starting with a '.' if the
optional argument WITH-SUBDIRS is not nil.
Do it recursively if the third argument is not nil."
  (when this-directory
    (when (file-directory-p this-directory)
      (let* ((this-directory (expand-file-name this-directory))
             (files (directory-files this-directory t "^[^\\.]")))

        ;; completely canonicalize the directory name (*may not* begin with `~')
        (while (not (string= this-directory (expand-file-name this-directory)))
          (setq this-directory (expand-file-name this-directory)))

        (message "Adding `%s' to load-path..." this-directory)
        (add-to-list 'load-path this-directory)

        (if with-subdirs
              (while files
                (setq dir-or-file (car files))
                (when (file-directory-p dir-or-file)
                  (if recursive
                      (my-add-to-load-path dir-or-file 'with-subdirs)
                    (my-add-to-load-path dir-or-file)))
                (setq files (cdr files)))))))))
--8<---------------cut here---------------end--------------->8---

Then, depending on my needs, I simply write a call such as:

--8<---------------cut here---------------start------------->8---
(my-add-to-load-path "~/.emacs.d/" 'with-subdirs 'recursive)
--8<---------------cut here---------------end--------------->8---

As you can read above, all directories starting with a `.' will
be avoided. That allows me to have a `.TEMP' subdirectory where
I can put files I wanna test later, or versions I wanna keep but
not see loaded.

As well, on a case by case basis, I decide if I want subdirs to
be loaded, even recursively, by setting or not the two optional

Have a look at my .emacs file if you need more examples:


Fabrice Niessen
Search the Web with "My Google Search Tools" on

reply via email to

[Prev in Thread] Current Thread [Next in Thread]