unofficial mirror of emacs-devel@gnu.org 
 help / color / mirror / code / Atom feed
From: Alan Third <alan@idiocy.org>
To: Lars Ingebrigtsen <larsi@gnus.org>
Cc: Eli Zaretskii <eliz@gnu.org>, emacs-devel@gnu.org
Subject: Re: `M-x yank-media'
Date: Tue, 23 Nov 2021 23:42:19 +0000	[thread overview]
Message-ID: <YZ18W18vybG7Cb/H@idiocy.org> (raw)
In-Reply-To: <87sfw8jdl1.fsf@gnus.org>

[-- Attachment #1: Type: text/plain, Size: 1019 bytes --]

On Sun, Nov 07, 2021 at 02:32:42PM +0100, Lars Ingebrigtsen wrote:
> Eli Zaretskii <eliz@gnu.org> writes:
> 
> > What does this require from the clipboard to work?  What are the
> > assumptions?
> 
> It's basically this on GNU/Linux:
> 
> (gui-get-selection 'CLIPBOARD 'TARGETS)
> =>
> [TIMESTAMP TARGETS MULTIPLE SAVE_TARGETS text/html text/_moz_htmlinfo text/_moz_htmlcontext image/png image/jpeg image/bmp image/x-bmp image/x-MS-bmp image/x-icon image/x-ico image/x-win-bitmap image/vnd\.microsoft\.icon application/ico image/ico image/icon text/ico image/tiff]
> 
> This lists the available media types.
> 
> I've just had a quick peek at what happens in Macos, and it's very
> different there.
> 
> (gui-get-selection 'CLIPBOARD 'TARGETS)
> => "http"
> 
> So gui-get-selection (or rather, ns-get-selection) has to grow support
> for this?

I think the attached should sort this.

It doesn't seem to do much on GNUstep, I suspect GNUstep itself
doesn't support copying of much more than plain text.

-- 
Alan Third

[-- Attachment #2: 0001-Enable-yank-media-on-NS.patch --]
[-- Type: text/x-diff, Size: 4125 bytes --]

From 2606ecbed8df134370aed4c40215cf6928cb3215 Mon Sep 17 00:00:00 2001
From: Alan Third <alan@idiocy.org>
Date: Tue, 23 Nov 2021 20:56:44 +0000
Subject: [PATCH] Enable yank-media on NS

* src/nsselect.m (ns_get_foreign_selection): Handle non-plain text
clipboard entries.
(ns_string_from_pasteboard): Remove EOL conversion.
(syms_of_nsselect): Define QTARGETS.
---
 src/nsselect.m | 80 ++++++++++++++++++++++++++++++++++++++++++++++++--
 1 file changed, 77 insertions(+), 3 deletions(-)

diff --git a/src/nsselect.m b/src/nsselect.m
index 5ab3ef77fe..e999835014 100644
--- a/src/nsselect.m
+++ b/src/nsselect.m
@@ -215,9 +215,74 @@ Updated by Christian Limpach (chris@nice.ch)
 static Lisp_Object
 ns_get_foreign_selection (Lisp_Object symbol, Lisp_Object target)
 {
+  NSDictionary<NSString *, NSString *> *typeLookup;
   id pb;
   pb = ns_symbol_to_pb (symbol);
-  return pb != nil ? ns_string_from_pasteboard (pb) : Qnil;
+
+  /* Dictionary for looking up NS types from MIME types, and vice versa.  */
+  typeLookup
+    = [NSDictionary
+           dictionaryWithObjectsAndKeys:
+             @"text/plain",        NSPasteboardTypeURL,
+#if NS_USE_NSPasteboardTypeFileURL
+             @"text/plain",        NSPasteboardTypeFileURL,
+#else
+             @"text/plain",        NSFilenamesPboardType,
+#endif
+             @"text/html",         NSPasteboardTypeHTML,
+             @"text/plain",        NSPasteboardTypeMultipleTextSelection,
+             @"application/pdf",   NSPasteboardTypePDF,
+             @"image/png",         NSPasteboardTypePNG,
+             @"application/rtf",   NSPasteboardTypeRTF,
+             @"application/rtfd",  NSPasteboardTypeRTFD,
+             @"STRING",            NSPasteboardTypeString,
+             @"text/plain",        NSPasteboardTypeTabularText,
+             @"image/tiff",        NSPasteboardTypeTIFF,
+             nil];
+
+  if (EQ (target, QTARGETS))
+    {
+      NSMutableArray *types = [NSMutableArray arrayWithCapacity:3];
+
+      NSString *type;
+      NSEnumerator *e = [[pb types] objectEnumerator];
+      while (type = [e nextObject])
+        {
+          NSString *val = [typeLookup valueForKey:type];
+          if (val && ! [types containsObject:val])
+            [types addObject:val];
+        }
+
+      Lisp_Object v = Fmake_vector (make_fixnum ([types count]+1), Qnil);
+      ASET (v, 0, QTARGETS);
+
+      for (int i = 0 ; i < [types count] ; i++)
+        ASET (v, i+1, intern ([[types objectAtIndex:i] UTF8String]));
+
+      return v;
+    }
+  else
+    {
+      NSData *d;
+      NSArray *availableTypes;
+      NSString *result, *t;
+
+      if (!NILP (target))
+        availableTypes
+          = [typeLookup allKeysForObject:
+                          [NSString stringWithLispString:SYMBOL_NAME (target)]];
+      else
+        availableTypes = @[NSPasteboardTypeString];
+
+      t = [pb availableTypeFromArray:availableTypes];
+
+      result = [pb stringForType:t];
+      if (result)
+        return [result lispString];
+
+      d = [pb dataForType:t];
+      return make_string ([d bytes], [d length]);
+    }
 }
 
 
@@ -234,8 +299,6 @@ Updated by Christian Limpach (chris@nice.ch)
 ns_string_from_pasteboard (id pb)
 {
   NSString *type, *str;
-  const char *utfStr;
-  int length;
 
   type = [pb availableTypeFromArray: ns_return_types];
   if (type == nil)
@@ -260,6 +323,14 @@ Updated by Christian Limpach (chris@nice.ch)
         }
     }
 
+  /* FIXME: Is the below EOL conversion even needed?  I've removed it
+     for now so we can see if it causes problems.  */
+  return [str lispString];
+
+#if 0
+  const char *utfStr;
+  int length;
+
   /* assume UTF8 */
   NS_DURING
     {
@@ -294,6 +365,7 @@ Updated by Christian Limpach (chris@nice.ch)
   NS_ENDHANDLER
 
     return make_string (utfStr, length);
+#endif
 }
 
 
@@ -491,6 +563,8 @@ Updated by Christian Limpach (chris@nice.ch)
   DEFSYM (QTEXT, "TEXT");
   DEFSYM (QFILE_NAME, "FILE_NAME");
 
+  DEFSYM (QTARGETS, "TARGETS");
+
   defsubr (&Sns_disown_selection_internal);
   defsubr (&Sns_get_selection);
   defsubr (&Sns_own_selection_internal);
-- 
2.33.0


  parent reply	other threads:[~2021-11-23 23:42 UTC|newest]

Thread overview: 33+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-11-06 21:05 `M-x yank-media' Lars Ingebrigtsen
2021-11-06 23:09 ` H. Dieter Wilhelm
2021-11-07  3:05   ` [External] : " Drew Adams
2021-11-07  1:23 ` T.V Raman
2021-11-07 10:02 ` Eli Zaretskii
2021-11-07 13:12   ` Lars Ingebrigtsen
2021-11-07 13:21     ` Eli Zaretskii
2021-11-07 13:32       ` Lars Ingebrigtsen
2021-11-07 13:38         ` Po Lu
2021-11-07 13:45           ` Lars Ingebrigtsen
2021-11-08  0:56             ` Po Lu
2021-11-08  1:10               ` Lars Ingebrigtsen
2021-11-08  2:12                 ` Po Lu
2021-11-07 13:42         ` Eli Zaretskii
2021-11-07 13:48           ` Lars Ingebrigtsen
2021-11-07 13:57             ` Eli Zaretskii
2021-11-07 14:04               ` Lars Ingebrigtsen
2021-11-07 14:08                 ` Stefan Monnier
2021-11-07 14:11                   ` Lars Ingebrigtsen
2021-11-07 14:09                 ` Eli Zaretskii
2021-11-07 14:13                   ` Lars Ingebrigtsen
2021-11-23 23:42         ` Alan Third [this message]
2021-11-24  7:14           ` Lars Ingebrigtsen
2021-11-24 10:59             ` Alan Third
2021-11-24 16:45               ` Lars Ingebrigtsen
2021-11-25  0:53                 ` Po Lu
2021-11-25 13:37                   ` Lars Ingebrigtsen
2021-11-08  3:07 ` Richard Stallman
2021-11-08  4:55   ` Lars Ingebrigtsen
2021-11-08  5:09 ` Lars Ingebrigtsen
2021-11-08 14:29   ` Eli Zaretskii
2021-11-09  3:39     ` Lars Ingebrigtsen
2021-11-08 16:56 ` Uwe Brauer

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/emacs/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=YZ18W18vybG7Cb/H@idiocy.org \
    --to=alan@idiocy.org \
    --cc=eliz@gnu.org \
    --cc=emacs-devel@gnu.org \
    --cc=larsi@gnus.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).