]> code.delx.au - gnu-emacs-elpa/blob - packages/load-dir-0.0.2.el
cec7ce5d583e31b90ca00ac24613e96d506b42e4
[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 ;;;###autoload
84 (defun load-dirs ()
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."
88 (interactive)
89 ;; avoid the case where users inadvertently set `load-dirs' to a string
90 (mapc 'load-dir-one (cond
91 ((eq load-dirs t)
92 (list (expand-file-name "~/.emacs.d/load.d")))
93 ((stringp load-dirs)
94 (list load-dirs))
95 (t load-dirs))))
96
97 (defvar load-dir-loaded nil
98 "List of already loaded files.")
99
100 ;;;###autoload
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'."
104 (interactive)
105 (setq load-dir-loaded nil)
106 (load-dirs))
107
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))
123 (load f))
124 (add-to-list 'load-dir-loaded f))))
125
126 (when load-dir-recursive
127 (dolist (f (directory-files dir t))
128 (when (file-directory-p f)
129 (load-dir-one f))))))
130
131 (defun load-dir-debug (&rest args)
132 "Print a debug message like `message' if `load-dir-debug' is set."
133 (when load-dir-debug
134 (apply 'message args)))
135
136 ;;;###autoload
137 (add-hook 'after-init-hook 'load-dirs)
138
139 (provide 'load-dir)
140 ;;; load-dir.el ends here