【问题标题】:Emacs Lisp: How to add a folder and all its first level sub-folders to the load-pathEmacs Lisp:如何将文件夹及其所有第一级子文件夹添加到加载路径
【发布时间】:2010-09-18 07:02:49
【问题描述】:

如果我有这样的文件夹结构:

~/Projects
    emacs
        package1
            package1-helpers
        package2
            package2-helpers
            package2-more-helpers
        package3
            package3-helpers

如何添加这些文件夹:

  • ~/Projects/emacs
  • ~/Projects/emacs/package1
  • ~/Projects/emacs/package2
  • ~/Projects/emacs/package3

...到我的 .emacs 文件中的load-path

我基本上需要这段代码的简短自动化版本:

(add-to-list 'load-path "~/Projects/emacs")
(add-to-list 'load-path "~/Projects/emacs/package1")
(add-to-list 'load-path "~/Projects/emacs/package2")
(add-to-list 'load-path "~/Projects/emacs/package3")

【问题讨论】:

    标签: emacs elisp


    【解决方案1】:
    (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)))))
    

    【讨论】:

    • 谢谢 Jouni!不幸的是,我收到一条错误消息:“加载 `c:/Users/Alex/AppData/Roaming/.emacs' 时发生错误:Symbol 的函数定义为 void:do”。我有在 Vista 下运行的 Emacs 22.2.1。
    • 糟糕——“do”在 cl.el 中,因此您可以通过在开头添加“(require cl)”来使原始建议生效。但是我修改了使用dolist的答案,既使它更简单,又避免了对cl.el的需要。
    【解决方案2】:

    我建议你使用subdirs.el

    【讨论】:

      【解决方案3】:

      这是我在 .emacs 中使用的内容:

      (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))
      

      如果您查看 normal-top-level-add-subdirs-to-load-path 的描述,选择要排除的目录有点聪明。

      【讨论】:

        【解决方案4】:

        这是我的破解版:P

        (defun add-to-list-with-subdirs (base exclude-list include-list)
          (dolist (f (directory-files base))
         (let ((name (concat base "/" f)))
           (when (and (file-directory-p name)
             (not (member f exclude-list)))
          (add-to-list 'load-path name)
          (when (member f include-list)
           (add-to-list-with-subdirs name exclude-list include-list)))))
          (add-to-list 'load-path base))
        

        这将从基础添加所有一级目录并排除排除列表中的目录,而对于包含列表中的目录,它也会添加该目录的所有一级目录。

        (add-to-list-with-subdirs "~/.emacs.d" '("." ".." "backup") '("vendor" "my-lisp"))
        

        【讨论】:

          【解决方案5】:

          此函数将映射 BASE-PATH 中的第一级子文件夹和文件,如果是目录(不包括目录“.”和“..”),则将其添加到 LOAD-LIST。

          (defun add-subdirs-to-load-path (base-path)
            "Adds first level subfolders to LOAD-PATH.
          BASE-PATH must not end with a '/'"
            (mapc (lambda (attr)
                    (let ((name (car attr))
                          (folder-p (cadr attr)))
                      (unless (or (not folder-p)
                                  (equal name ".")
                                  (equal name ".."))
                        (add-to-list 'load-path (concat base-path "/" name)))))
                  (directory-files-and-attributes base-path)))
          

          【讨论】:

          • 请对你的回答做一些解释。仅代码答案不受欢迎
          【解决方案6】:

          安装 dashf 第三方库。你需要的函数是f-directories:

          (f-directories "~/YOURDIR") ; return only immediate directories
          (f-directories "~/YOURDIR" nil t) ; all directories recursively
          

          然后使用--each 将找到的每个目录添加到load-path。整个操作在 O(n²) 中运行,但 load-path 通常非常小,谁在乎呢。

          (add-to-list 'load-path "~/YOURDIR") ; your parent folder itself
          (--each (f-directories "~/YOURDIR") (add-to-list 'load-path it))
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2021-07-24
            • 2019-05-13
            • 2019-12-22
            • 1970-01-01
            • 1970-01-01
            • 2020-03-17
            • 1970-01-01
            • 2017-12-16
            相关资源
            最近更新 更多