MitchN
MitchN

Reputation: 63

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

I have a long list of files (full path names, one each on its own line, in a text file), and I want to open all of these files into emacs buffers, so that I can then use multi-occur-in-matching-buffers to navigate around in these files.

How can I open the list of files from within emacs ? The files listed are in arbitrary folders and have arbitrary file names. Ie., there is no regular pattern to the paths and names, so I am not looking for a particular regular expression to match my example below.

I don't want to do this at the emacs command line invocation, as I am running emacs on windows by clicking on an icon, and also, I want to keep open the other buffers (files) I have already open.

I am able to create a custom elisp function (hard-coding of the list of file names in the function), as follows (short example).

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

I can place that elisp in a buffer, then select it all, then eval-region, then execute the function with M-x open-my-files.

However, it would be much more productive for me if the elisp would read the list of files from the text file which contains the list.

Upvotes: 2

Views: 934

Answers (2)

MitchN
MitchN

Reputation: 63

I have come to the following solution. Similar to the answer proposed by James Anderson, but replacing the re-search-forward with thing-at-point, and a few other changes based on some other references.

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

References:

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

Grab current line in buffer as a string in elisp

How do I delete the newline from a process output?

Upvotes: 2

James Anderson
James Anderson

Reputation: 589

This seems to work on my machine:

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

you might need to futz around with the regex though (tested on a unix file system). And its emacs, so there is probably a better way that someone will probably point out. The buffers loaded will not be set to current buffer on purpose.

Upvotes: 1

Related Questions