我熟悉load-path
and require
,但我想知道是否可以将init.elrequire
中的多个语句合并到某种循环中,最终执行类似-ing 给定目录中的所有文件的操作。require
有没有办法做到这一点?或者我应该保持原样,使用多个require
语句?
我同意 Drew 的观点,你想load
在这种情况下使用。此函数基于 Drew 的代码,进行了一些调整以避免在同时存在 .el 和 .elc 版本时重新加载库。
(defun my-load-all-in-directory (dir)
"`load' all elisp libraries in directory DIR which are not already loaded."
(interactive "D")
(let ((libraries-loaded (mapcar #'file-name-sans-extension
(delq nil (mapcar #'car load-history)))))
(dolist (file (directory-files dir t ".+\\.elc?$"))
(let ((library (file-name-sans-extension file)))
(unless (member library libraries-loaded)
(load library nil t)
(push library libraries-loaded))))))
(let ((loaded (mapcar #'car load-history)))
(dolist (file (directory-files "~/.emacs.d" t ".+\\.elc?$"))
(unless (catch 'foo
(dolist (done loaded)
(when (equal file done) (throw 'foo t)))
nil)
(load (file-name-sans-extension file))
(push file loaded))))
load
,而不是require
,因为功能名称(来自provide
)不一定与文件的基本名称相同。或者该文件甚至可能不是provide
功能。你可以尝试这样的事情:
(mapc (lambda (name)
(require (intern (file-name-sans-extension name))))
(directory-files ".emacs.d" nil "\\.el$"))
解释:
directory-files
file-name-sans-extension
intern
,require
然后