Subject: RE: [External] : bug#78658: 30.1; [PATCH] Dired feature suggestion: dired-on-marked-files-in-all-buffers

From: Drew Adams <[email-masked]>
Date: Mon, 2 Jun 2025 00:13:50 +0000

> > I've attached a patch with a first pass implementation.

It's a very good idea.  I plan to add it to Dired+ (with
attribution), for use with older Emacs, even if it doesn't
get added to Emacs. 

> Ideally, it could find the common prefix, then set
> default-directory to it and list only relative file names,
> like in the output of e.g. 'find-dired-with-command'.

Another good idea.
___

Here's the code I have for this now.  If you add the feature
to Emacs then the Dired+ functions will just become aliases
for the vanilla Emacs functions (in releases where they exist).

(defun diredp-get-marked-files-in-all-buffers ()
  "Return names of files and directories marked in any Dired buffers.
Like `dired-get-marked-files', but for all Dired buffers."
  (diredp-delete-dups
   (let ((dired-bufs  (delq nil (mapcar (lambda (d.b)
                                          (and (buffer-live-p (cdr d.b))
                                               (cdr d.b)))
                                        dired-buffers))))
     (apply #'nconc
            (mapcar (lambda (buf)
                      (with-current-buffer buf
                        (let ((files  (dired-get-marked-files nil nil nil t)))
                          (setq files  (and (cdr files)  (if (eq (car files) t)
                                                             (cadr files)
                                                           files))))))
                    dired-bufs)))))

(defun diredp-marked-files-in-all-buffers ()
  "Dired the files and directories marked in any Dired buffers.
Like `diredp-marked-files', but for all Dired buffers."
  (interactive)
  (let ((files  (diredp-get-marked-files-in-all-buffers)))
    (unless (directory-name-p common) (setq common  (diredp-parent-dir common)))
    (let ((default-directory  (or common  default-directory)))
      (dired (cons "MARKED-ANYWHERE"
                   (if common
                       (mapcar (lambda (file) (file-relative-name file common))
                               files)
                     files)))))))

Differences from Phil's patch (besides the function names):

1. Use Juri's suggestion of using a common ancestor directory
   and relative file names.  IIRC, file systems can sometimes
   have no common prefix - in which case use absolute names if
   there's no common ancestor directory.

2. The Dired buffer is named `MARKED ANYWHERE', so as not to
   interfere with (i.e., replace) another (e.g., normal)
   listing of the same directory.

3. Use variable `dired-buffers', to check only Dired buffers
   instead of all buffers.

4. Quick check of marked-files list (without `length'). 
___

FWIW -

Dired+ has long had commands that act on all marked files & dirs
in a Dired buffer, and all marked files & dirs in Dired buffers
for marked subdirs,... defined recursively.

IOW, instead of (as in Phil's feature) checking all Dired buffers
for markings, such functions check only the current Dired buffer,
Dired buffers for its marked subdirs, and so on recursively.

Applying `dired' to that collection of marked files & dirs is
only one possible action.  In general, all of the usual actions
for marked files are provided (by separate commands).

Phil's feature is a good addition to such functionality.