Subject: bug#30929: 26.0.91; Text drag and drop does not work

From: Nick Helm <[email-masked]>
Date: Wed, 28 Mar 2018 22:20:13 +1300

Hi Alan,

On Sun, 25 Mar 2018 at 12:57:32 +0100, Alan Third wrote:

> Looks like this is how the modifiers are set in performDragOperation
>
>   if (! (op & (NSDragOperationMove|NSDragOperationDelete)) &&
>       // URL drags contain all operations (0xf), don't allow all to be set.
>       (op & 0xf) != 0xf)
>     {
>       if (op & NSDragOperationLink)
>         modifiers |= NSEventModifierFlagControl;
>       if (op & NSDragOperationCopy)
>         modifiers |= NSEventModifierFlagOption;
>       if (op & NSDragOperationGeneric)
>         modifiers |= NSEventModifierFlagCommand;
>     }
>
>   modifiers = EV_MODIFIERS2 (modifiers);
>
> It’s setting the actual modifier keys, so when a user changes those
> keys’ settings this breaks.
>
> You can also set these flags by using the actual modifier keys.
>
> This looks like it matches up with what Apple expect you to do, but it
> doesn’t seem to match up with Emacs’s event handling very well. 

That looks about right to me too, it least it matches the general
approach in the docs.

I had a go at mapping the hardware modifiers to Emacs events (and
existing bindings) for each drag type and DragOperation mask. Patch
attached. This doesn't support the ns-right-*-modifiers yet, but they
should be pretty easy to add if you want to go this way.

>From 9a177de5bacde98ef5c513c73f7aa48b58909af4 Mon Sep 17 00:00:00 2001
From: Nick Helm <[email-masked]>
Date: Wed, 28 Mar 2018 21:06:59 +1300
Subject: [PATCH] Improve handling of drag-and-drop modifiers on NS.

---
 src/nsterm.m | 56 +++++++++++++++++++++++++++++++++++++++++---------------
 1 file changed, 41 insertions(+), 15 deletions(-)

diff --git a/src/nsterm.m b/src/nsterm.m
index c8ae31abc0..4ad60d1b6a 100644
--- a/src/nsterm.m
+++ b/src/nsterm.m
@@ -8115,19 +8115,6 @@ -(BOOL)performDragOperation: (id <NSDraggingInfo>) sender
   pb = [sender draggingPasteboard];
   type = [pb availableTypeFromArray: ns_drag_types];
 
-  if (! (op & (NSDragOperationMove|NSDragOperationDelete)) &&
-      // URL drags contain all operations (0xf), don't allow all to be set.
-      (op & 0xf) != 0xf)
-    {
-      if (op & NSDragOperationLink)
-        modifiers |= NSEventModifierFlagControl;
-      if (op & NSDragOperationCopy)
-        modifiers |= NSEventModifierFlagOption;
-      if (op & NSDragOperationGeneric)
-        modifiers |= NSEventModifierFlagCommand;
-    }
-
-  modifiers = EV_MODIFIERS2 (modifiers);
   if (type == 0)
     {
       return NO;
@@ -8141,13 +8128,24 @@ -(BOOL)performDragOperation: (id <NSDraggingInfo>) sender
       if (!(files = [pb propertyListForType: type]))
         return NO;
 
+      if (! (op & (NSDragOperationMove | NSDragOperationDelete)) &&
+          (op & 0xf) != 0xf)
+        {
+          if (op & NSDragOperationGeneric)
+            modifiers |= NSEventModifierFlagCommand;
+          else if (op & NSDragOperationLink)
+            modifiers |= NSEventModifierFlagControl;
+          else if (op & NSDragOperationCopy)
+            modifiers |= NSEventModifierFlagOption;
+        }
+
       fenum = [files objectEnumerator];
       while ( (file = [fenum nextObject]) )
         {
           emacs_event->kind = DRAG_N_DROP_EVENT;
           XSETINT (emacs_event->x, x);
           XSETINT (emacs_event->y, y);
-          emacs_event->modifiers = modifiers;
+          emacs_event->modifiers = EV_MODIFIERS2 (modifiers);
           emacs_event->arg =  list2 (Qfile, build_string ([file UTF8String]));
           EV_TRAILER (theEvent);
         }
@@ -8158,10 +8156,22 @@ -(BOOL)performDragOperation: (id <NSDraggingInfo>) sender
       NSURL *url = [NSURL URLFromPasteboard: pb];
       if (url == nil) return NO;
 
+      if (! (op & (NSDragOperationMove|NSDragOperationDelete)) &&
+          // URL drags contain all operations (0xf), don't allow all to be set.
+          (op & 0xf) != 0xf)
+        {
+          if (op & NSDragOperationGeneric)
+            modifiers |= NSEventModifierFlagCommand;
+          else if (op & NSDragOperationLink)
+            modifiers |= NSEventModifierFlagControl;
+          else if (op & NSDragOperationCopy)
+            modifiers |= NSEventModifierFlagOption;
+        }
+
       emacs_event->kind = DRAG_N_DROP_EVENT;
       XSETINT (emacs_event->x, x);
       XSETINT (emacs_event->y, y);
-      emacs_event->modifiers = modifiers;
+      emacs_event->modifiers = EV_MODIFIERS2 (modifiers);
       emacs_event->arg =  list2 (Qurl,
                                  build_string ([[url absoluteString]
                                                  UTF8String]));
@@ -8183,6 +8193,22 @@ -(BOOL)performDragOperation: (id <NSDraggingInfo>) sender
       if (! (data = [pb stringForType: type]))
         return NO;
 
+      if (! (op & (NSDragOperationMove | NSDragOperationDelete)) &&
+          (op & 0xf) != 0xf)
+        {
+          if ((op & NSDragOperationCopy) && (op & NSDragOperationGeneric))
+            modifiers |= meta_modifier;
+          else if (op & NSDragOperationGeneric)
+            modifiers |= (parse_solitary_modifier (ns_command_modifier)
+                          | meta_modifier);
+          else if (op & NSDragOperationCopy)
+            modifiers |= (parse_solitary_modifier (ns_alternate_modifier)
+                          | meta_modifier);
+          else
+            modifiers |= (parse_solitary_modifier (ns_control_modifier)
+                          | meta_modifier);
+        }
+
       emacs_event->kind = DRAG_N_DROP_EVENT;
       XSETINT (emacs_event->x, x);
       XSETINT (emacs_event->y, y);
-- 
2.14.3 (Apple Git-98)