1 ;;; load-dir.el --- load all Emacs Lisp files in given directories
3 ;; Copyright (C) 2011 Free Software Foundation, Inc
5 ;; Authors: Teodor Zlatanov <tzz@lifelogs.com>,
6 ;; Ben Key <bkey76@gmail.com>
7 ;; With-Help-From: Evans Winner <ego111@gmail.com>, PJ Weisberg <pj@irregularexpressions.net>
9 ;; Keywords: lisp, files, convenience
11 ;; This file is part of GNU Emacs.
13 ;; GNU Emacs is free software: you can redistribute it and/or modify
14 ;; it under the terms of the GNU General Public License as published by
15 ;; the Free Software Foundation, either version 3 of the License, or
16 ;; (at your option) any later version.
18 ;; GNU Emacs is distributed in the hope that it will be useful,
19 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
20 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 ;; GNU General Public License for more details.
23 ;; You should have received a copy of the GNU General Public License
24 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
28 ;; This library will load all Lisp files found in the `load-dirs' variable.
29 ;; (you may also want to set `load-dir-debug', `load-dir-recursive',
30 ;; and `load-dir-ignore-errors')
32 ;; Normal usage in .emacs:
34 ;; (setq load-dirs '("~/mystuff")) ;; or Customize it
35 ;; (require 'load-dir) ;; this will add `load-dirs' to your `after-init-hook'
37 ;; Then after startup:
39 ;; Explicitly load new files only...
42 ;; Or reload all your files...
43 ;; M-x load-dirs-reload
47 (eval-when-compile (require 'cl))
49 (defgroup load-dir nil
50 "Automatically load all Emacs Lisp files in given directories."
51 :group 'initialization)
53 (defcustom load-dir-debug t
54 "Debugging messages toggle, default to t."
58 (defcustom load-dir-recursive nil
59 "Whether subdirectories should be loaded too."
63 (defcustom load-dir-ignore-errors nil
64 "Whether errors in the loaded files should be ignored."
68 (defcustom load-dirs nil
69 "This variable allows you to define which directories should be loaded.
71 If nil, no directories are loaded. This is the default behavior.
72 If t, only files in ~/.emacs.d/load.d will be loaded.
73 If a single directory name, only files in that directory will be loaded.
74 If a list of directory names, all files found in all the
75 directories will be loaded."
77 :tag "What directories to load"
78 :type '(choice (const :tag "Load all from ~/.emacs.d/load.d" t)
79 (const :tag "Don't load anything" nil)
81 (repeat :tag "Directories" directory)))
85 "Load all Emacs Lisp files in `load-dirs'.
86 Will not load a file twice (use `load-dir-reload' for that).
87 Recurses into subdirectories if `load-dir-recursive' is t."
89 ;; avoid the case where users inadvertently set `load-dirs' to a string
90 (mapc 'load-dir-one (cond
92 (list (expand-file-name "~/.emacs.d/load.d")))
97 (defvar load-dir-loaded nil
98 "List of already loaded files.")
101 (defun load-dirs-reload ()
102 "Load all Emacs Lisp files in `load-dirs'.
103 Clears the list of loaded files and just calls `load-dir-load'."
105 (setq load-dir-loaded nil)
108 (defun load-dir-one (dir)
109 "Load all Emacs Lisp files in DIR.
110 Recurses into subdirectories if `load-dir-recursive' is t."
111 (load-dir-debug "Loading Emacs Lisp code from %s" dir)
112 (let ((suffixes (get-load-suffixes)))
113 (dolist (f (and (file-exists-p dir)
114 (file-directory-p dir)
115 (directory-files dir t)))
116 (when (and (not (file-directory-p f))
117 (member (file-name-extension f t) suffixes))
118 (setq f (file-name-sans-extension f))
119 (if (member f load-dir-loaded)
120 (load-dir-debug "Skipping %s, it's already loaded." f)
121 (if load-dir-ignore-errors
122 (with-demoted-errors (load f))
124 (add-to-list 'load-dir-loaded f))))
126 (when load-dir-recursive
127 (dolist (f (directory-files dir t))
128 (when (file-directory-p f)
129 (load-dir-one f))))))
131 (defun load-dir-debug (&rest args)
132 "Print a debug message like `message' if `load-dir-debug' is set."
134 (apply 'message args)))
137 (add-hook 'after-init-hook 'load-dirs)
140 ;;; load-dir.el ends here