如何从 emacs 中打开一长串文件

How can I open a long list of files from within emacs

我有一长串文件(完整路径名,一个单独的行,在一个文本文件中),我想将所有这些文件打开到 emacs 缓冲区中,这样我就可以使用 multi -occur-in-matching-buffers 在这些文件中导航。

如何从 emacs 中打开文件列表?列出的文件位于任意文件夹中并具有任意文件名。也就是说,路径和名称没有规则模式,所以我不是在寻找特定的正则表达式来匹配我下面的示例。

我不想在 emacs 命令行调用时执行此操作,因为我是 运行 emacs on windows 通过单击图标,而且,我想保持打开我已经打开的其他缓冲区(文件)。

我能够创建自定义 elisp 函数(函数中文件名列表的硬编码),如下(简短示例)。

(defun open-my-files ()
  "Open my files"
  (interactive)
  (find-file "c:/files/file1.txt")
  (find-file "c:/more_files/deeper/log.log")
  (find-file "c:/one_last_note.ini")
)

我可以将 elisp 放在缓冲区中,然后 select 全部,然后是 eval-region,然后使用 M-x open-my-files 执行函数。

但是,如果 elisp 从包含列表的文本文件中读取文件列表,对我来说会更有效率。

这似乎适用于我的机器:

(defun open-a-bunch-of-files (filelist)
  (with-temp-buffer
    (insert-file-contents filelist)
    (goto-char (point-min))
    (let ((done nil))
      (while (not done)
        (if (re-search-forward "^\([a-z_A-Z:\/.0-9]+\)$" nil t nil)
            (find-file-noselect (match-string 1))
          (setf done t))))))

(open-a-bunch-of-files "./filelist.txt")

不过您可能需要使用正则表达式(在 unix 文件系统上测试过)。还有它的 emacs,所以可能有人会指出更好的方法。加载的缓冲区不会故意设置为当前缓冲区。

我得出以下解决方案。类似于 James Anderson 提出的答案,但将 re-search-forward 替换为 thing-at-point,以及基于其他参考资料的一些其他更改。

  (defun open-a-bunch-of-files ()
    "Open a a bunch of files, given a text file containing a list of file names"
    (interactive)
    (setq my_filelist (completing-read "my_filelist: " nil nil nil))
    (with-temp-buffer
      (insert-file-contents my_filelist)
      (goto-char (point-min))
      (while (not (eobp))
        (find-file-noselect (replace-regexp-in-string "\n$" "" (thing-at-point 'line t)))
        (forward-line)
      )
    )
  )

参考文献:

https://emacs.stackexchange.com/questions/19518/is-there-an-idiomatic-way-of-reading-each-line-in-a-buffer-to-process-it-line-by

How do I delete the newline from a process output?