Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Emacs Lisp: How to add a folder and all its first level sub-folders to the load-path

Tags:

emacs

elisp

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:

  • ~/Projects/emacs
  • ~/Projects/emacs/package1
  • ~/Projects/emacs/package2
  • ~/Projects/emacs/package3

...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")
like image 763
Alexander Kojevnikov Avatar asked Oct 21 '08 10:10

Alexander Kojevnikov


People also ask

How do I load a lisp in emacs?

To load an Emacs Lisp file, type M-x load-file . This command reads a file name using the minibuffer, and executes the contents of that file as Emacs Lisp code. It is not necessary to visit the file first; this command reads the file directly from disk, not from an existing Emacs buffer.

How do I change directory in Emacs?

You can change this with M-x cd and type in whatever directory you would like to be the default instead (and by default I mean the one that will show up when you do C-x C-f ). If you start emacs without opening a file, you will end up with the *scratch* buffer open.

How do I navigate to a folder in Emacs?

In Emacs, type M-x dired. You will be prompted for the directory to open. Type in the directory to display, or press Return to open the default directory.


2 Answers

(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)))))
like image 193
Jouni K. Seppänen Avatar answered Oct 01 '22 15:10

Jouni K. Seppänen


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.

like image 45
Nicholas Riley Avatar answered Oct 01 '22 14:10

Nicholas Riley