1 (defvar file-loadable-regexp
2 (replace-regexp-in-string
5 (suffix-list (get-load-suffixes)))
6 (concat (car suffix-list) "$"
7 (dolist (extension (cdr suffix-list) string)
8 (setq string (concat "\\|" extension "$" string))))))
9 "Regular expression that matches any file name with a file
10 extension returned by `get-load-suffixes'.")
12 (defun file-loadable-p (file)
13 "Return t if FILE is an Emacs lisp file.
14 More precisely, return t if the file name extension matches
15 `file-loadable-regexp'"
16 (string-match file-loadable-regexp file))
18 (defun load-dir (&optional directory recurse)
19 "Load all Emacs Lisp files in DIRECTORY.
21 Load files whose file name satisfies predicate `file-loadable-p'.
22 Non-interactively, DIRECTORY must be specified. If both compiled
23 and uncompiled versions of the same file exist, only load the
24 compiled file. If optional argument RECURSE is non-nil, (or,
25 interactively, with prefix argument) recursively load
28 ;; The idea here is to allow a prefix arg to specify recursion, but
29 ;; also to read from the minibuffer the directory name; yet in
30 ;; non-interactive use to only need the one directory-name argument,
31 ;; as in: (load-dir "~/foo")
32 (let* ((recurse (if recurse recurse (when current-prefix-arg t)))
33 (directory (if (stringp directory) directory
34 (when (called-interactively-p 'any)
36 (concat (if recurse "Recursively l" "L")
37 "oad all Emacs lisp files from directory: ")
38 default-directory default-directory t)))))
39 ;; For non-interactive use
40 (when (not (called-interactively-p 'any))
42 (error "Must specify a directory to when called non-interactively")))
43 (unless (file-directory-p directory)
44 (error "%s is not a directory" directory))
46 (directory-files (expand-file-name directory)
47 t directory-files-no-dot-files-regexp)))
48 (dolist (file file-list)
51 ;; This will include gzipped elisp files
52 (file-loadable-p file)
53 ;; Ignore symlinks to nonexistent targets.
55 ;; Don't try to load directies whose names end in ".el"
56 ;; etc., as if they were files. Note that we do not
57 ;; predicate on "permission denied" problems, instead
58 ;; letting things fail in that case so the user knows.
59 (not (file-directory-p file))
60 ;; If there exist both compiled and uncompiled versions of
61 ;; the same library, only load the compiled one. (This is
62 ;; why we let-bind the `file-list'.) This could perhaps be
63 ;; factored out, and currently still double-loads gzipped
65 (not (and (string= (file-name-extension file t) ".el")
67 (concat (file-name-sans-extension file) ".elc")
70 ((and (file-directory-p file)
72 (load-dir file t)))))))