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

From: Phil Sainty <[email-masked]>
Date: Sun, 01 Jun 2025 15:19:02 +1200

A user in the matrix.org Emacs chat room was asking why they couldn't
open multiple directories in multiple dired buffers, mark various files
in each of those buffers, and then perform some action on ALL of those
marked files at once.

My first thought was that this would be chaotic due to the possibility
of dired buffers with marked files which the user wasn't aware of at
the time; but then I remembered that you can pass `dired' a file list
in order to create a new dired buffer with only those files, and I
thought *that* sounded like a nice feature which addressed the
requirement in a safe manner (as the user then gets to review the file
list in the new dired buffer before acting on them).

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

Thoughts?
From d024ac85ef6ce260196a47a85dc7f04a54fcf118 Mon Sep 17 00:00:00 2001
From: Phil Sainty <[email-masked]>
Date: Sun, 1 Jun 2025 14:59:38 +1200
Subject: [PATCH] * lisp/dired.el: (dired-on-marked-files-in-all-buffers): New
 command

(dired-get-marked-files-in-all-buffers): New function.
---
 lisp/dired.el | 25 +++++++++++++++++++++++++
 1 file changed, 25 insertions(+)

diff --git a/lisp/dired.el b/lisp/dired.el
index 63d373a63dc..e2563a1aea4 100644
--- a/lisp/dired.el
+++ b/lisp/dired.el
@@ -1070,6 +1070,31 @@ dired-get-marked-files
       (user-error (if (stringp error) error "No files specified")))
     result))
 
+(defun dired-get-marked-files-in-all-buffers ()
+  "The marked files in all dired buffers."
+  (cl-delete-duplicates
+   (apply #'nconc
+          (mapcar (lambda (buf)
+                    (with-current-buffer buf
+                      (when (derived-mode-p 'dired-mode)
+                        (let ((files (dired-get-marked-files nil nil nil t)))
+			  ;; Handle the DISTINGUISH-ONE-MARKED behaviour.
+                          (cond ((eql (length files) 1)
+                                 (setq files nil))
+                                ((and (eql (length files) 2)
+                                      (eq (car files) t))
+                                 (setq files (cdr files))))
+                          files))))
+                  (buffer-list)))
+   :test 'equal))
+
+(defun dired-on-marked-files-in-all-buffers ()
+  "Invoke `dired' on the marked files in all dired buffers."
+  (interactive)
+  (if-let ((files (dired-get-marked-files-in-all-buffers)))
+      (dired (cons "/" files))
+    (user-error "No marked files in any dired buffer")))
+
 (defun dired-mark--region-use-p ()
   "Whether Dired marking commands should act on region."
   (and dired-mark-region
-- 
2.34.1