]> code.delx.au - gnu-emacs-elpa/blob - packages/load-dir-0.0.2.el
packages/load-dir-0.0.2.el: added.
[gnu-emacs-elpa] / packages / load-dir-0.0.2.el
1 ;;; load-dir.el --- load all Emacs Lisp files in given directories
2
3 ;; Copyright (C) 2011 Free Software Foundation, Inc
4
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>
8 ;; Version: 0.0.2
9 ;; Keywords: lisp, files, convenience
10
11 ;; This file is part of GNU Emacs.
12
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.
17
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.
22
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/>.
25
26 ;;; Commentary:
27
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')
31
32 ;; Normal usage in .emacs:
33
34 ;; (setq load-dirs '("~/mystuff")) ;; or Customize it
35 ;; (require 'load-dir) ;; this will add `load-dirs' to your `after-init-hook'
36
37 ;; Then after startup:
38
39 ;; Explicitly load new files only...
40 ;; M-x load-dirs
41
42 ;; Or reload all your files...
43 ;; M-x load-dirs-reload
44
45 ;;; Code:
46
47 (eval-when-compile (require 'cl))
48
49 (defgroup load-dir nil
50 "Automatically load all Emacs Lisp files in given directories."
51 :group 'initialization)
52
53 (defcustom load-dir-debug t
54 "Debugging messages toggle, default to t."
55 :group 'load-dir
56 :type 'boolean)
57
58 (defcustom load-dir-recursive nil
59 "Whether subdirectories should be loaded too."
60 :group 'load-dir
61 :type 'boolean)
62
63 (defcustom load-dir-ignore-errors nil
64 "Whether errors in the loaded files should be ignored."
65 :group 'load-dir
66 :type 'boolean)
67
68 (defcustom load-dirs nil
69 "This variable allows you to define which directories should be loaded.
70
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."
76 :group 'load-dir
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)
80 directory
81 (repeat :tag "Directories" directory)))
82
83 (defun load-dirs ()
84 "Load all Emacs Lisp files in `load-dirs'.
85 Will not load a file twice (use `load-dir-reload' for that).
86 Recurses into subdirectories if `load-dir-recursive' is t."
87 (interactive)
88 ;; avoid the case where users inadvertently set `load-dirs' to a string
89 (mapc 'load-dir-one (cond
90 ((eq load-dirs t)
91 (list (expand-file-name "~/.emacs.d/load.d")))
92 ((stringp load-dirs)
93 (list load-dirs))
94 (t load-dirs))))
95
96 (defvar load-dir-loaded nil
97 "List of already loaded files.")
98
99 ;;;###autoload
100 (defun load-dirs-reload ()
101 "Load all Emacs Lisp files in `load-dirs'.
102 Clears the list of loaded files and just calls `load-dir-load'."
103 (interactive)
104 (setq load-dir-loaded nil)
105 (load-dirs))
106
107 (defun load-dir-one (dir)
108 "Load all Emacs Lisp files in DIR.
109 Recurses into subdirectories if `load-dir-recursive' is t."
110 (load-dir-debug "Loading Emacs Lisp code from %s" dir)
111 (let ((suffixes (get-load-suffixes)))
112 (dolist (f (and (file-exists-p dir)
113 (file-directory-p dir)
114 (directory-files dir t)))
115 (when (and (not (file-directory-p f))
116 (member (file-name-extension f t) suffixes))
117 (setq f (file-name-sans-extension f))
118 (if (member f load-dir-loaded)
119 (load-dir-debug "Skipping %s, it's already loaded." f)
120 (if load-dir-ignore-errors
121 (with-demoted-errors (load f))
122 (load f))
123 (add-to-list 'load-dir-loaded f))))
124
125 (when load-dir-recursive
126 (dolist (f (directory-files dir t))
127 (when (file-directory-p f)
128 (load-dir-one f))))))
129
130 (defun load-dir-debug (&rest args)
131 "Print a debug message like `message' if `load-dir-debug' is set."
132 (when load-dir-debug
133 (apply 'message args)))
134
135 ;;;###autoload
136 (add-hook 'after-init-hook 'load-dirs)
137
138 (provide 'load-dir)
139 ;;; load-dir.el ends here