9

I have a mechanism for burning items to disc from a dired buffer. Unfortunately, the file's I've patched are strewn accross the file system. How can I create a dired buffer from all open files?

Sean Allred
  • 6,861
  • 16
  • 85
  • possible duplicate of [Programmatically insert files into Dired buffer](http://emacs.stackexchange.com/questions/2567/programmatically-insert-files-into-dired-buffer) – Drew Nov 05 '14 at 05:07
  • @Drew I would argue not a duplicate, at least not strictly. This question would simply become 'How do I get a list of all open buffers bound to files?'. – Sean Allred Nov 05 '14 at 05:08
  • @Drew Specifically: `(sort (remove nil (mapcar #'buffer-file-name (buffer-list))) #'string<)` – Sean Allred Nov 05 '14 at 05:09
  • The information added is that `buffer-list` exists (which I did know) and that `buffer-file-name` returns `nil` when there is no file (which I did not know). Your proposed duplicate doesn't contain this information or have any need to. – Sean Allred Nov 05 '14 at 05:11
  • The only difference is how you come up with the list of files. In that case, this question should be closed and replaced with a question "*How do I get a list of all open files?*" – Drew Nov 05 '14 at 05:11
  • 1
    @Drew In my experience, trying to make every question as modular as possible is a disadvantage rather than an advantage. It decreases the usefulness with persons who stumble upon this from Google and will never register simply because they Google'd their *use case*. If I want through every question on TeX.SE and dupe'd questions that had the same conceptual answers, moderation would become a nightmare and the site would plunge into darkness. – Sean Allred Nov 05 '14 at 05:13

2 Answers2

12

The dired command accepts a list of file-names as argument. Just create one and pass to it:

(dired
 (cons
  "Open Files"
  (sort (remove nil (mapcar #'buffer-file-name (buffer-list)))
       #'string<))) 
Malabarba
  • 22,878
  • 6
  • 78
  • 163
  • I think this misses one file: when you pass a list to dired the first element in that list is taken as the directory name, the rest as the files. How about this: `(dired (cons "*Open Files*" (sort (remove nil (mapcar #'buffer-file-name (buffer-list))) #'string<)))` – glucas Nov 04 '14 at 19:10
  • Just a note: this will err if there is a file-associated buffer that has not been saved to disk yet. – Sean Allred Nov 04 '14 at 21:28
1

Just provide a list of strings, to the dired function, example:

(dired '("custom dired" ".emacs.d/init.el" "mydotfiles/.bashrc"))

EDIT

@Malabarba answer is the best way to go.

Nsukami _
  • 6,341
  • 2
  • 22
  • 35