Subject: bug#73258: 31.0.50; w32 drag-n-dropping multiple files is broken

From: Cecilio Pardo <[email-masked]>
Date: Sat, 21 Sep 2024 15:41:03 +0200

On 21/09/2024 12:11, Eli Zaretskii wrote:
> This causes the following warning while byte-compiling:
>
>    In w32-dropped-file-to-url:
>    term/w32-win.el:129:29: Warning: reference to free variable `window'
>
> And indeed, 'window' is not bound to any value here, and
> dnd-handle-multiple-urls does need it to be a valid window.  If I try
> drag-and-drop with your patch installed, Emacs signals an error about
> 'window' being void.  I don't understand how it worked for you.
>
> Also, what about this comment:
>
>    ;; FIXME: is the W32 build capable only of receiving a single file
>    ;; from each drop?
>
> I guess it is no longer pertinent and should be deleted?

I messed up when preparing the patch file, I'm sorry.

Here is the correct one.
From 3c5af14b5c0d956153d83fe9a5041ae897e5b9eb Mon Sep 17 00:00:00 2001
From: Cecilio Pardo <[email-masked]>
Date: Sat, 21 Sep 2024 15:30:27 +0200
Subject: [PATCH] Fix multifile drag-n-drop on win32

Pass all dropped files to dnd-handle-multiple-urls
on one call instead on doing multiple calls (Bug#73258).
* lisp/term/w32-win.el (w32-dropped-file-to-url): New function to
convert file name to a url for dnd
(w32-handle-dropped-file): Changed to use w32-dropped-file-to-url
(w32-drag-n-drop): Changed to pass all files to
dnd-handle-multiple-urls
---
 lisp/term/w32-win.el | 27 +++++++++++++++++----------
 1 file changed, 17 insertions(+), 10 deletions(-)

diff --git a/lisp/term/w32-win.el b/lisp/term/w32-win.el
index 3c0acf368f4..b57b3dd3bef 100644
--- a/lisp/term/w32-win.el
+++ b/lisp/term/w32-win.el
@@ -101,6 +101,13 @@ w32-color-map
 ;;   (princ event))
 
 (defun w32-handle-dropped-file (window file-name)
+  (dnd-handle-multiple-urls
+   window
+   (list
+    (w32-dropped-file-to-url file-name))
+   'private))
+
+(defun w32-dropped-file-to-url (file-name)
   (let ((f (if (eq system-type 'cygwin)
                (cygwin-convert-file-name-from-windows file-name t)
              (subst-char-in-string ?\\ ?/ file-name)))
@@ -117,14 +124,11 @@ w32-handle-dropped-file
                      (split-string (encode-coding-string f coding)
                                    "/")
                      "/")))
-  ;; FIXME: is the W32 build capable only of receiving a single file
-  ;; from each drop?
-  (dnd-handle-multiple-urls window (list (concat
-			                  (if (eq system-type 'cygwin)
-				              "file://"
-			                    "file:")
-			                  file-name))
-                            'private))
+  (concat
+   (if (eq system-type 'cygwin)
+       "file://"
+     "file:")
+   file-name))
 
 (defun w32-drag-n-drop (event &optional new-frame)
   "Edit the files listed in the drag-n-drop EVENT.
@@ -146,8 +150,11 @@ w32-drag-n-drop
       (raise-frame)
       (setq window (selected-window))
 
-      (mapc (apply-partially #'w32-handle-dropped-file window)
-            (car (cdr (cdr event)))))))
+      (dnd-handle-multiple-urls
+       window
+       (mapcar #'w32-dropped-file-to-url
+               (car (cdr (cdr event))))
+       'private))))
 
 (defun w32-drag-n-drop-other-frame (event)
   "Edit the files listed in the drag-n-drop EVENT, in other frames.
-- 
2.35.1.windows.2