无法将目录及其所有子目录添加到Emacs中的加载路径

无法将目录及其所有子目录添加到Emacs中的加载路径,emacs,lisp,elisp,Emacs,Lisp,Elisp,问题类似于 但是,它的不同之处在于将所有可实现的子目录也放在文件夹中 Jouni的代码,该代码将一级文件夹放在可实现的位置上 (let ((base "~/Projects/emacs")) (add-to-list 'load-path base) (dolist (f (directory-files base)) (let ((name (concat base "/" f))) (when (and (file-directory-p name)

问题类似于

但是,它的不同之处在于将所有可实现的子目录也放在文件夹中

Jouni的代码,该代码将一级文件夹放在可实现的位置上

(let ((base "~/Projects/emacs"))
  (add-to-list 'load-path base)
  (dolist (f (directory-files base))
    (let ((name (concat base "/" f)))
      (when (and (file-directory-p name) 
                 (not (equal f ".."))
                 (not (equal f ".")))
        (add-to-list 'load-path name)))))
如何将目录及其所有子目录放入Emacs中的加载路径?

另一个问题中的My处理多个级别的子目录

参考代码

(let* ((my-lisp-dir "~/.elisp/")
       (default-directory my-lisp-dir)
       (orig-load-path load-path))
  (setq load-path (cons my-lisp-dir nil))
  (normal-top-level-add-subdirs-to-load-path)
  (nconc load-path orig-load-path))

这里是对Jouni答案的改编,它使用了一个可以定制的帮助函数

helper函数的一个优点是,当它执行意外操作时,您可以跟踪它,因为它是纯函数,所以不会对加载路径产生副作用。我尝试使用普通的顶级add subdirs来加载路径,但其中的所有内容都会产生副作用,并且依赖于不可预测的特殊变量,因此编写干净的新内容更容易。请注意,我的答案没有使用索引节点,因此可能效率较低

这种方法的第二个优点是,它允许您定制希望忽略的文件

(defun add-to-load-path-with-subdirs (directory &optional endp) (let ((newdirs (lp-subdir-list directory))) (if endp (setq load-path (append load-path newdirs)) (setq load-path (nconc newdirs load-path))))) (defconst +lp-ignore-list+ (list "CVS" ".git" ".svn" ".." ".")) (defun lp-subdir-list (base &optional ignore) (unless ignore (setq ignore +lp-ignore-list+)) (let ((pending (list base)) (retval nil)) (while pending (let ((dir (pop pending))) (push dir retval) (dolist (f (directory-files dir)) (let ((name (concat dir "/" f))) (when (and (not (member f ignore)) (file-directory-p name)) (push name pending) (push name retval)))))) (reverse retval))) (使用子目录(目录和可选endp)定义添加到加载路径 (let((newdirs(lp subdir列表目录))) (如果endp(setq加载路径(追加加载路径newdirs)) (setq加载路径(NCOC newdirs加载路径()(()))) (defconst+lp忽略列表+ (列出“CVS.git.svn…”) (定义lp子目录列表(基本和可选忽略) (除非忽略) (setq忽略+lp忽略列表+) (let((待定(列表基数)) (返回零) (待决期间) (let((dir(pop待定))) (推送方向返回) (目录文件目录) (let((名称(concat dir)/f))) (当(和(非)(成员f忽略)) (file-directory-p name)) (推送名称待定) (推式名称检索(()()))) (反向返回) 简单回答:

 (normal-top-level-add-subdirs-to-load-path)

@尼古拉斯:为什么要用星号?let*是许多嵌套let的简写,每个嵌套let绑定一个变量;正则old let一次绑定所有变量。因此,使用let而不是let*,我无法在默认目录的绑定中引用我的lisp dir,因为它在let的主体之前不可用。要查看排除了哪些目录,运行C-h f普通顶级添加子目录到加载路径。@Nicholas:您如何看到函数中的内容,例如您最后一条评论中的内容?如果您单击“帮助”窗口中的“startup.el”,您将转到函数的定义(假设它是用elisp编写的,而不是用C编写的)