As the title suggests, how do I get all the file entries in a Dired buffer using Elisp?
Manipulating marks seems unnecessary, and perhaps problematic.
Obviously there is functionality within dired to do this sort of thing (albeit not well documented). Here's a simple approach:
;; Silence compile-time warning: (declare-function dired-map-dired-file-lines "dired-aux") (defun my-dired-files () "Return a list of files (only) in the current dired buffer." (eval-when-compile (require 'cl-lib)) (require 'dired-aux) (let (flist) (cl-flet ((fpush (fname) (push fname flist))) (dired-map-dired-file-lines #'fpush)) (nreverse flist)))
In the comments you say you'd like the resulting list to include the directory paths as well as the file paths.
dired-map-dired-file-lines explicitly excludes directories, and there's no option to include them; but it's easy to define a function which does, simply by copying the original and omitting the unwanted test:
(defun my-dired-map-over-files-and-dirs (fun) "Perform FUN with point at the end of each file or directory line. FUN takes one argument, the absolute filename." (save-excursion (let (file buffer-read-only) (goto-char (point-min)) (while (not (eobp)) (save-excursion (and (not (eolp)) (setq file (dired-get-filename nil t)) ; nil on non-file (progn (end-of-line) (funcall fun file)))) (forward-line 1)))))
Just use this in place of
dired-map-dired-file-lines in the initial function. You can then also exclude the
declare-function call as well.
You could, of course, further modify this new function to build the desired list directly, instead of calling a function to do that. I feel the more general approach is fine in practice, but you might prefer the idea of wrapping it all into a single function.
In addition to @abo-abo's answer, which tells you how to get a list of the file (& dir) names:
Depending on what you want, you can alternatively use
dired-copy-filename-as-kill (bound to
w) instead of
It puts the file names into a single, space-separated string, and pushes that string to the
(Space separation means, of course, that it is not so helpful when some of the file names themselves contain space characters. ;-))