Subject: bug#72254: 29.4; Cannot yank media when image come from Gnome-shell screenshot

From: Po Lu <[email-masked]>
Date: Wed, 24 Jul 2024 17:29:54 +0800

Robert Pluim <[email-masked]> writes:

>>>>>> On Tue, 23 Jul 2024 19:16:23 +0200, Sébastien Gendre <[email-masked]> said:
>
>     Sébastien> If I evaluate this `cl-defmethod', and then evaluate (gui-get-selection
>     Sébastien> 'CLIPBOARD 'TARGETS) from an Org-mode buffer after taking an screenshot,
>     Sébastien> I get this error:
>
> My bad. Try this instead:
>
> diff --git a/lisp/term/pgtk-win.el b/lisp/term/pgtk-win.el
> index 505174048f6..06f962ff714 100644
> --- a/lisp/term/pgtk-win.el
> +++ b/lisp/term/pgtk-win.el
> @@ -140,6 +140,14 @@ gui-backend-get-selection
>                                           &context (window-system pgtk))
>    (pgtk-get-selection-internal selection-symbol target-type))
>  
> +(cl-defmethod gui-backend-get-selection ((selection-symbol (eql 'CLIPBOARD))
> +                                         (target-type (eql 'TARGETS))
> +                                         &context (window-system pgtk))
> +  (let ((sel (pgtk-get-selection-internal selection-symbol target-type)))
> +    (if (vectorp sel)
> +        sel
> +      (vector sel))))
> +
>  
>  (defvar pgtk-preedit-overlay nil)
>  
>
> Po Lu, is this the right level to fix this? Or should we handle
> 'CLIPBOARD + 'TARGETS specially in `pgtk-get-selection-internal'? (And
> maybe we should add "&optional time_stamp terminal" to the
> `cl-defmethods' in pgtk-win.el)

I would prefer to treat this as a bug in GTK by installing a workaround,
perhaps in the spirit of:

diff --git a/src/pgtkselect.c b/src/pgtkselect.c
index c9f117126b2..0535482b8de 100644
--- a/src/pgtkselect.c
+++ b/src/pgtkselect.c
@@ -1644,10 +1644,24 @@ DEFUN ("pgtk-get-selection-internal", Fpgtk_get_selection_internal,
 
   if (NILP (val) && FRAME_LIVE_P (f))
     {
-      Lisp_Object frame;
+      Lisp_Object frame, val;
       XSETFRAME (frame, f);
-      return pgtk_get_foreign_selection (selection_symbol, target_type,
-					 time_stamp, frame);
+
+      val = pgtk_get_foreign_selection (selection_symbol, target_type,
+					time_stamp, frame);
+
+      /* A window property holding just one item is indistinguishable
+	 from an array of one element, and is always decoded as the
+	 former, producing issues with programs that expect the TARGETS
+	 property always to return vectors, even when the toolkit
+	 reports just one data type.  Though X sidesteps this ambiguity
+	 by defining TARGETS as returning at least two properties
+	 TARGETS and MULTIPLE, GTK knows no such scruples, and therefore
+	 symbol values (or nil) should be enclosed in vectors when
+	 TARGETS is being requested.  (bug#72254) */
+      if (EQ (target_type, QTARGETS) && (NILP (val) || SYMBOLP (val)))
+	val = make_vector (NILP (val) ? 0 : 1, &val);
+      return val;
     }
 
   if (CONSP (val) && SYMBOLP (XCAR (val)))