]> code.delx.au - gnu-emacs-elpa/blob - packages/load-dir-0.0.2.el
* packages/all-1.0.el: Change version. Address byte-compiler warnings.
[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 package provides a way to load all Emacs Lisp snippets (they
29 ;; don't have to be libraries) in a directory on startup or when Emacs is
30 ;; already running. It won't reload snippets unless the user requests
31 ;; it, so for instance adding a lambda to a hook is usually safe.
32 ;;
33 ;; You can specify ~/.emacs.d/load.d, a single directory, or a list of
34 ;; directories. The file search can be recursive.
35 ;;
36 ;; The intent with ~/.emacs.d/load.d is to give package installers like
37 ;; el-get.el (see https://github.com/dimitri/el-get) and other tools a
38 ;; way to easily bootstrap themselves without necessarily modifying your
39 ;; .emacs or custom files directly.
40
41 ;;; Code:
42
43 (eval-when-compile (require 'cl))
44
45 (defgroup load-dir nil
46 "Automatically load all Emacs Lisp files in given directories."
47 :group 'initialization)
48
49 (defcustom load-dir-debug t
50 "Debugging messages toggle, default to t."
51 :group 'load-dir
52 :type 'boolean)
53
54 (defcustom load-dir-recursive nil
55 "Whether subdirectories should be loaded too."
56 :group 'load-dir
57 :type 'boolean)
58
59 (defcustom load-dir-ignore-errors nil
60 "Whether errors in the loaded files should be ignored."
61 :group 'load-dir
62 :type 'boolean)
63
64 (defcustom load-dirs nil
65 "This variable allows you to define which directories should be loaded.
66
67 If nil, no directories are loaded. This is the default behavior.
68 If t, only files in ~/.emacs.d/load.d will be loaded.
69 If a single directory name, only files in that directory will be loaded.
70 If a list of directory names, all files found in all the
71 directories will be loaded."
72 :group 'load-dir
73 :tag "What directories to load"
74 :type '(choice (const :tag "Load all from ~/.emacs.d/load.d" t)
75 (const :tag "Don't load anything" nil)
76 directory
77 (repeat :tag "Directories" directory)))
78
79 ;;;###autoload
80 (defun load-dirs ()
81 "Load all Emacs Lisp files in `load-dirs'.
82 Will not load a file twice (use `load-dir-reload' for that).
83 Recurses into subdirectories if `load-dir-recursive' is t."
84 (interactive)
85 ;; avoid the case where users inadvertently set `load-dirs' to a string
86 (mapc 'load-dir-one (cond
87 ((eq load-dirs t)
88 (list (expand-file-name "~/.emacs.d/load.d")))
89 ((stringp load-dirs)
90 (list load-dirs))
91 (t load-dirs))))
92
93 (defvar load-dir-loaded nil
94 "List of already loaded files.")
95
96 ;;;###autoload
97 (defun load-dirs-reload ()
98 "Load all Emacs Lisp files in `load-dirs'.
99 Clears the list of loaded files and just calls `load-dir-load'."
100 (interactive)
101 (setq load-dir-loaded nil)
102 (load-dirs))
103
104 (defun load-dir-one (dir)
105 "Load all Emacs Lisp files in DIR.
106 Recurses into subdirectories if `load-dir-recursive' is t."
107 (load-dir-debug "Loading Emacs Lisp code from %s" dir)
108 (let ((suffixes (get-load-suffixes)))
109 (dolist (f (and (file-exists-p dir)
110 (file-directory-p dir)
111 (directory-files dir t)))
112 (when (and (not (file-directory-p f))
113 (member (file-name-extension f t) suffixes))
114 (setq f (file-name-sans-extension f))
115 (if (member f load-dir-loaded)
116 (load-dir-debug "Skipping %s, it's already loaded." f)
117 (if load-dir-ignore-errors
118 (with-demoted-errors (load f))
119 (load f))
120 (add-to-list 'load-dir-loaded f))))
121
122 (when load-dir-recursive
123 (dolist (f (directory-files dir t))
124 (when (file-directory-p f)
125 (load-dir-one f))))))
126
127 (defun load-dir-debug (&rest args)
128 "Print a debug message like `message' if `load-dir-debug' is set."
129 (when load-dir-debug
130 (apply 'message args)))
131
132 ;;;###autoload
133 (add-hook 'after-init-hook 'load-dirs)
134
135 (provide 'load-dir)
136 ;;; load-dir.el ends here