GNU bug report logs - #75116
[PATCH] Make 'yank-media' autoselect the best media type

Please note: This is a static page, with minimal formatting, updated once a day.
Click here to see this page with the latest information and nicer formatting.

Package: emacs; Severity: wishlist; Reported by: Visuwesh <visuweshm@HIDDEN>; Keywords: patch; dated Thu, 26 Dec 2024 12:29:01 UTC; Maintainer for emacs is bug-gnu-emacs@HIDDEN.

Message received at 75116 <at> debbugs.gnu.org:


Received: (at 75116) by debbugs.gnu.org; 13 Feb 2025 13:34:06 +0000
From debbugs-submit-bounces <at> debbugs.gnu.org Thu Feb 13 08:34:06 2025
Received: from localhost ([127.0.0.1]:41392 helo=debbugs.gnu.org)
	by debbugs.gnu.org with esmtp (Exim 4.84_2)
	(envelope-from <debbugs-submit-bounces <at> debbugs.gnu.org>)
	id 1tiZLx-00083c-Mk
	for submit <at> debbugs.gnu.org; Thu, 13 Feb 2025 08:34:06 -0500
Received: from mail-pj1-x1042.google.com ([2607:f8b0:4864:20::1042]:56606)
 by debbugs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128)
 (Exim 4.84_2) (envelope-from <visuweshm@HIDDEN>)
 id 1tiZLu-000832-Tm
 for 75116 <at> debbugs.gnu.org; Thu, 13 Feb 2025 08:34:03 -0500
Received: by mail-pj1-x1042.google.com with SMTP id
 98e67ed59e1d1-2f9d3d0f55dso1337188a91.1
 for <75116 <at> debbugs.gnu.org>; Thu, 13 Feb 2025 05:34:02 -0800 (PST)
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
 d=gmail.com; s=20230601; t=1739453636; x=1740058436; darn=debbugs.gnu.org;
 h=mime-version:user-agent:message-id:date:references:in-reply-to
 :subject:cc:to:from:from:to:cc:subject:date:message-id:reply-to;
 bh=NlGc5XMWXE9WOW57YEdELvE86fVjvmmwHrLgGn47yts=;
 b=PNrYsWgYlMRNkwg3X/h9YlybBkc7cjHC3+5TgSyi5x3tq0jYBL5fmOyHsP8V8G+XUp
 GlL6HRsyUjt5VC0SZ+CiB3pYwSnClY9JaAv6En0xl6/OXOIdj9C/eOkTRBjaLSMisIEv
 tR7Z2mhSeANQIswarU4KV0DhBOzRS01eYm+XZWlzK+UVkvi7MRRRroo0Sh5XCQWThzhl
 e07y6sDnwF4Fv++WJ5BGydT/B6JG0bglV/Y0ihOi7bsIwLChKC1V1TIJbqN1p4wTWdCh
 inSLPalRpNuENzB2arHQVjSFP6DvFqjuhvj3iDjOleZEeGBIznL8ld1mn9oYn+J+XGR6
 sFPA==
X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
 d=1e100.net; s=20230601; t=1739453636; x=1740058436;
 h=mime-version:user-agent:message-id:date:references:in-reply-to
 :subject:cc:to:from:x-gm-message-state:from:to:cc:subject:date
 :message-id:reply-to;
 bh=NlGc5XMWXE9WOW57YEdELvE86fVjvmmwHrLgGn47yts=;
 b=m1XcaLGIPPk4yyRUbRiFIo28i0/kSNAQOJtwGQMLbWoCW090O6TIyamk+WCV6EQ+AU
 A522BYsdr7QYbAToxa1zsyCZv8Sxs1rl8cRJEOqorN/Fcq446TQlmXcjVCh9t6Zr7rIw
 8QOgfrAeojzEzhC1vVOUBHDCQ3eOZF92Z5fgxVgrxXwOAOS4KzJXfWyUxDarE5lvl+jv
 vN+yXhdQVdHQWkpw0734HMiDdDsysfyvZE6NJDUBsGvZrPiiWfj546AO3CVSbCsffArG
 Z1k2mVehIxPDOXdE/wCr8eylXWsI41sQ0zvGggH5cNMUGXQl/FpQwlKQtM5/B6NeOjbY
 NoRw==
X-Forwarded-Encrypted: i=1;
 AJvYcCVHMybnwlBqw0hQgQzV53cXKaNO1ASbdU6mR5Q5wbcPfz1bDaZ2zlRGv2TAbF/us7VLZojsUQ==@debbugs.gnu.org
X-Gm-Message-State: AOJu0YwFBN92KUyosGnG9/Tc7VGh5nIhjJPIXvbrnib/lX8y4OtLC8Qh
 l4059U3UShG12UeBmvm0UcEmo4uJ8hYRM4gpHOAZWLHveSU+bPyu
X-Gm-Gg: ASbGnctkiAn4Y6OvEWHcOVoRFVK67+gGCbEPHGPLs8g2atz2rL41sefe6SIHyPSYj11
 Ue2xUN8PPWWl5B6WnHmcx74rUBYSvwRJgliHGUJcQZkAFpWJEU/ZvW+e24cE4YGMpJoiIfh9sjM
 TceWlOyOf+bJqSsyaTbqlNgQbsTAjYEEMjjB25ntz1wdRU2AXJ2Ip8KRmp/svl5OOrPuQcNJty4
 YcIBrRrsblyI6gAevp1yF+pOP7gz4zevMscnmc5jC6VAaD43xUsMs4vvWevABqVpZWIxAma8WpZ
 qVx8SYWeoQzA
X-Google-Smtp-Source: AGHT+IFZLcZ91bXnjQoxSTxwgIbFhNEcBYQ0vahnLPeKEJIZGXytjCR2Mm1C1kqc3da9ESiO0wvOVw==
X-Received: by 2002:a17:90b:5243:b0:2ef:31a9:95c6 with SMTP id
 98e67ed59e1d1-2fbf5be7625mr12398205a91.14.1739453635647; 
 Thu, 13 Feb 2025 05:33:55 -0800 (PST)
Received: from localhost ([115.240.90.130])
 by smtp.gmail.com with UTF8SMTPSA id
 98e67ed59e1d1-2fc13ba61f1sm1222555a91.42.2025.02.13.05.33.53
 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256);
 Thu, 13 Feb 2025 05:33:54 -0800 (PST)
From: Visuwesh <visuweshm@HIDDEN>
To: Eli Zaretskii <eliz@HIDDEN>
Subject: Re: bug#75116: [PATCH] Make 'yank-media' autoselect the best media
 type
In-Reply-To: <864j0yhyeu.fsf@HIDDEN> (Eli Zaretskii's message of "Thu, 13 Feb
 2025 13:33:29 +0200")
References: <87o70yeiih.fsf@HIDDEN> <86r05uxx4i.fsf@HIDDEN>
 <871pxtcxiq.fsf@HIDDEN> <86r05sro63.fsf@HIDDEN>
 <87bjwkbls7.fsf@HIDDEN> <87cygzhjw9.fsf@localhost>
 <868qrn6alg.fsf@HIDDEN> <8734hvhifj.fsf@localhost>
 <867c7769f0.fsf@HIDDEN> <871pw2jdsp.fsf@HIDDEN>
 <864j0yhyeu.fsf@HIDDEN>
Date: Thu, 13 Feb 2025 19:03:50 +0530
Message-ID: <87tt8yhsu9.fsf@HIDDEN>
User-Agent: Gnus/5.13 (Gnus v5.13)
MIME-Version: 1.0
Content-Type: multipart/mixed; boundary="=-=-="
X-Spam-Score: 1.0 (+)
X-Debbugs-Envelope-To: 75116
Cc: yantar92@HIDDEN, pinmacs@HIDDEN, rpluim@HIDDEN,
 75116 <at> debbugs.gnu.org
X-BeenThere: debbugs-submit <at> debbugs.gnu.org
X-Mailman-Version: 2.1.18
Precedence: list
List-Id: <debbugs-submit.debbugs.gnu.org>
List-Unsubscribe: <https://debbugs.gnu.org/cgi-bin/mailman/options/debbugs-submit>, 
 <mailto:debbugs-submit-request <at> debbugs.gnu.org?subject=unsubscribe>
List-Archive: <https://debbugs.gnu.org/cgi-bin/mailman/private/debbugs-submit/>
List-Post: <mailto:debbugs-submit <at> debbugs.gnu.org>
List-Help: <mailto:debbugs-submit-request <at> debbugs.gnu.org?subject=help>
List-Subscribe: <https://debbugs.gnu.org/cgi-bin/mailman/listinfo/debbugs-submit>, 
 <mailto:debbugs-submit-request <at> debbugs.gnu.org?subject=subscribe>
Errors-To: debbugs-submit-bounces <at> debbugs.gnu.org
Sender: "Debbugs-submit" <debbugs-submit-bounces <at> debbugs.gnu.org>
X-Spam-Score: -1.0 (-)

--=-=-=
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: quoted-printable

[=E0=AE=B5=E0=AE=BF=E0=AE=AF=E0=AE=BE=E0=AE=B4=E0=AE=A9=E0=AF=8D =E0=AE=AA=
=E0=AE=BF=E0=AE=AA=E0=AF=8D=E0=AE=B0=E0=AE=B5=E0=AE=B0=E0=AE=BF 13, 2025] E=
li Zaretskii wrote:

>> From: Visuwesh <visuweshm@HIDDEN>
>> Cc: Ihor Radchenko <yantar92@HIDDEN>,  75116 <at> debbugs.gnu.org,
>>   pinmacs@HIDDEN,  rpluim@HIDDEN
>> Date: Thu, 13 Feb 2025 16:45:50 +0530
>>=20
>> Sorry for the >month long delay yet again.  I got caught up with $LIFE.
>> The attached patch has the required code and documentation changes.
>
> Thanks.
>
> Please use @sc{mime} in Texinfo and upper-case MIME everywhere else.

Thanks for your patience.  Please see attached.


--=-=-=
Content-Type: text/x-diff
Content-Disposition: attachment;
 filename=0001-Make-yank-media-auto-select-the-best-mime-type.patch

From de7339191cfbec0bf81459aa1c879c5243346445 Mon Sep 17 00:00:00 2001
From: Visuwesh <visuweshm@HIDDEN>
Date: Thu, 13 Feb 2025 16:43:06 +0530
Subject: [PATCH] Make yank-media auto select the best mime type

* lisp/yank-media.el (yank-media-preferred-types): Add new
variable that holds the list of mime types in order of their
preference.
(yank-media-autoselect-function): Add new variable and function
to choose the most preferred media type.
(yank-media): Make 'yank-media' choose the most preferred mime
type by default.
* doc/emacs/killing.texi (Clipboard):
* doc/lispref/frames.texi (Yanking Media): Document the new
behaviour, and the new variables.
* etc/NEWS: Announce the change.  (Bug#75116)
---
 doc/emacs/killing.texi  |  6 ++-
 doc/lispref/frames.texi | 24 ++++++++++++
 etc/NEWS                |  8 ++++
 lisp/yank-media.el      | 85 ++++++++++++++++++++++++++++++++++++-----
 4 files changed, 111 insertions(+), 12 deletions(-)

diff --git a/doc/emacs/killing.texi b/doc/emacs/killing.texi
index 070e15dee76..aee68608bbf 100644
--- a/doc/emacs/killing.texi
+++ b/doc/emacs/killing.texi
@@ -587,8 +587,10 @@ Clipboard
 instance, a web browser will usually let you choose ``Copy Image'' on
 images, and this image will be put on the clipboard.  On capable
 platforms, Emacs can yank these objects with the @code{yank-media}
-command---but only in modes that have support for it (@pxref{Yanking
-Media,,, elisp, The Emacs Lisp Reference Manual}).
+command---but only in modes that have support for it.  By default, it
+auto-selects the preferred media type available in the clipboard but
+this can be overriden by giving the prefix argument to the command
+(@pxref{Yanking Media,,, elisp, The Emacs Lisp Reference Manual}).
 
 @cindex clipboard manager
 @vindex x-select-enable-clipboard-manager
diff --git a/doc/lispref/frames.texi b/doc/lispref/frames.texi
index bc2d6b07ae8..984f9bb597d 100644
--- a/doc/lispref/frames.texi
+++ b/doc/lispref/frames.texi
@@ -4812,6 +4812,30 @@ Yanking Media
 @code{gui-get-selection}.
 @end defun
 
+The @code{yank-media} command auto selects the preferred @sc{mime} type
+by default.  The rules used for the selection can be controlled through
+the variables @code{yank-media-autoselect-function} and
+@code{yank-media-preferred-types}.
+
+@defvar yank-media-autoselect-function
+This variable should specify a function that will be called with the
+list of @sc{mime} types available for the current major mode, and should
+return a list of preferred @sc{mime} types to use.  The first @sc{mime}
+type in the list will always be used by the @code{yank-media} command
+when auto selection is requested.
+@end defvar
+
+@defvar yank-media-preferred-types
+This variable changes the default selection process of
+@code{yank-media-autoselect-function}.  It is a list that should contain
+the sole @sc{mime} type to choose in the order of their preference.  It
+can also contain a function in which case it is called with the list of
+available @sc{mime} types and must return a list of preferred @sc{mime}
+types in order of their preference.  This list is passed onto the
+@code{yank-media} command so the first element of the returned list is
+chosen when auto selection is requested.
+@end defvar
+
 The @code{yank-media-types} command presents a list of selection data
 types that are currently available, which is useful when implementing
 yank-media handlers; for programs generally offer an eclectic and
diff --git a/etc/NEWS b/etc/NEWS
index 419c9bd8d20..24cea20c782 100644
--- a/etc/NEWS
+++ b/etc/NEWS
@@ -1176,6 +1176,14 @@ This macro adds the current buffer to 'inhibit-auto-revert-buffers',
 runs its body, and removes the current buffer from
 'inhibit-auto-revert-buffers' afterwards.
 
+** Yank media
+
++++
+*** 'yank-media' now auto-selects the most preferred MIME type.
+Major-mode authors can customise the variables
+'yank-media-autoselect-function' and/or 'yank-media-preferred-types' to
+change the selection rules.
+
 
 * New Modes and Packages in Emacs 31.1
 
diff --git a/lisp/yank-media.el b/lisp/yank-media.el
index 600cc0ffaf5..62d613e7f1f 100644
--- a/lisp/yank-media.el
+++ b/lisp/yank-media.el
@@ -29,19 +29,67 @@
 
 (defvar yank-media--registered-handlers nil)
 
+(defvar yank-media-autoselect-function #'yank-media-autoselect-function
+  "Function to auto select the best MIME types when many are available.
+The function is called with a list of MIME types that have handler in
+the current buffer, and should return the list of MIME types to use in
+order of their priority.  When `yank-media' auto-selects the MIME type,
+it will always choose the first one of the returned list.
+Major-mode authors can change this variable to influence the selection
+process.")
+
+(defvar yank-media-preferred-types
+  `(;; Check first since LibreOffice also puts a PNG image in the
+    ;; clipboard when a table cell is copied.
+    application/x-libreoffice-tsvc
+    ;; Give PNG more priority.
+    image/png
+    image/jpeg
+    ;; These are files copied/cut to the clipboard from a file manager
+    ;; in a GNU/Linux and/or BSD environment.
+    ,@(when (memq window-system '(x pgtk))
+        (list (lambda (mimetypes)
+                (ensure-list
+                 (seq-find (lambda (type)
+                             (string-match-p "x-special/\\(gnome\\|KDE\\|mate\\)-copied-files"
+                                             (symbol-name type)))
+                           mimetypes)))))
+    ;; FIXME: We should have a way to handle text/rtf.
+    text/html)
+  "List of MIME types in the order of preference.
+Each element in the list should be a symbol to choose that MIME type
+exclusively, or a function of one argument and should return the list of
+MIME types to use in order of their priority or nil if no preferred type
+is found.
+Major-mode authors can change this variable to influence the selection
+process, or by directly changing the variable
+`yank-media-autoselect-function'.")
+
+(defun yank-media-autoselect-function (mimetypes)
+  (catch 'preferred
+    (dolist (typ yank-media-preferred-types)
+      (let ((ret (if (functionp typ)
+                     (funcall typ mimetypes)
+                   (and (memq typ mimetypes) (list typ)))))
+        (when ret (throw 'preferred ret))))))
+
 ;;;###autoload
-(defun yank-media ()
+(defun yank-media (&optional noselect)
   "Yank media (images, HTML and the like) from the clipboard.
 This command depends on the current major mode having support for
 accepting the media type.  The mode has to register itself using
 the `yank-media-handler' mechanism.
+Optional argument NOSELECT non-nil (interactively, with a prefix
+argument) means to skip auto-selecting the best MIME type and ask for
+the MIME type to use.
 
 Also see `yank-media-types' for a command that lets you explore
 all the different selection types."
-  (interactive)
+  (interactive "P")
   (unless yank-media--registered-handlers
     (user-error "The `%s' mode hasn't registered any handlers" major-mode))
-  (let ((all-types nil))
+  (let ((all-types nil)
+        pref-type)
     (pcase-dolist (`(,handled-type . ,handler)
                    yank-media--registered-handlers)
       (dolist (type (yank-media--find-matching-media handled-type))
@@ -49,18 +97,35 @@ yank-media
     (unless all-types
       (user-error
        "No handler in the current buffer for anything on the clipboard"))
-    ;; We have a handler in the current buffer; if there's just
-    ;; matching type, just call the handler.
-    (if (length= all-types 1)
+    (setq pref-type (and (null noselect)
+                         (funcall yank-media-autoselect-function
+                                  (mapcar #'car all-types))))
+    (cond
+     ;; We are asked to autoselect and have a preferred MIME type.
+     ((and (null noselect) pref-type)
+      (funcall (cdr (assq (car pref-type) all-types))
+               (car pref-type)
+               (yank-media--get-selection (car pref-type))))
+     ;; We are asked to autoselect and no preferred MIME type.
+     ((and (null noselect) (null pref-type))
+      (message
+       (substitute-command-keys
+        "No preferred MIME type to yank, try \\[universal-argument] \\[yank-media]")))
+     ;; No autoselection and there's only one media type available.
+     ((and noselect (length= all-types 1))
+      (when (y-or-n-p (format "Yank the `%s' clipboard item?"
+                              (caar all-types)))
         (funcall (cdar all-types) (caar all-types)
-                 (yank-media--get-selection (caar all-types)))
-      ;; More than one type the user for what type to insert.
+                 (yank-media--get-selection (caar all-types)))))
+     ;; No autoselection and multiple media types available.
+     ((and noselect (length> all-types 1))
       (let ((type
              (intern
               (completing-read "Several types available, choose one: "
-                               (mapcar #'car all-types) nil t))))
+                               (or pref-type (mapcar #'car all-types))
+                               nil t))))
         (funcall (alist-get type all-types)
-                 type (yank-media--get-selection type))))))
+                 type (yank-media--get-selection type)))))))
 
 (defun yank-media--find-matching-media (handled-type)
   (seq-filter
-- 
2.47.2


--=-=-=--




Information forwarded to bug-gnu-emacs@HIDDEN:
bug#75116; Package emacs. Full text available.

Message received at 75116 <at> debbugs.gnu.org:


Received: (at 75116) by debbugs.gnu.org; 13 Feb 2025 11:33:47 +0000
From debbugs-submit-bounces <at> debbugs.gnu.org Thu Feb 13 06:33:47 2025
Received: from localhost ([127.0.0.1]:40986 helo=debbugs.gnu.org)
	by debbugs.gnu.org with esmtp (Exim 4.84_2)
	(envelope-from <debbugs-submit-bounces <at> debbugs.gnu.org>)
	id 1tiXTX-00079B-Cn
	for submit <at> debbugs.gnu.org; Thu, 13 Feb 2025 06:33:47 -0500
Received: from eggs.gnu.org ([2001:470:142:3::10]:38060)
 by debbugs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256)
 (Exim 4.84_2) (envelope-from <eliz@HIDDEN>) id 1tiXTT-00078r-Bd
 for 75116 <at> debbugs.gnu.org; Thu, 13 Feb 2025 06:33:44 -0500
Received: from fencepost.gnu.org ([2001:470:142:3::e])
 by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256)
 (Exim 4.90_1) (envelope-from <eliz@HIDDEN>)
 id 1tiXTM-0004Lf-Ty; Thu, 13 Feb 2025 06:33:37 -0500
DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=gnu.org;
 s=fencepost-gnu-org; h=References:Subject:In-Reply-To:To:From:Date:
 mime-version; bh=nBdy+9YR3OT9rij7G2jx2L9Esm266OmBiXLrE+eSvSw=; b=ibpuBMXvHzQ0
 VjMdGnmwyZglhZTxdxwBAsKwfNLkLiYkb557poIe62Qzq9bCUKKi+z6/PWWAy0ux41NihmSTPzX4g
 VrtZonRfwGvfZwlpmjflFCum+9RJ9nkLjtzgq7RT/6Ez952eq7sduGZUIOJM8QIWl7x5v4NDgZoXf
 Swhekrt0U+++KeO2MnVqC+RycBZJB6yABvEKtzsr//AH/ISKFxtFtVwoV8wmKxWDa0QFQQYfwPQLL
 F2xoLLr8bO5C1t6t5+74FC5DYJRHJ8daMlnoOedjIDm7+NiZCy0HCnG0KKZ05lPNWJsnBZA6x8Sr3
 awC/5AgufZBJhAUizjzMKA==;
Date: Thu, 13 Feb 2025 13:33:29 +0200
Message-Id: <864j0yhyeu.fsf@HIDDEN>
From: Eli Zaretskii <eliz@HIDDEN>
To: Visuwesh <visuweshm@HIDDEN>
In-Reply-To: <871pw2jdsp.fsf@HIDDEN> (message from Visuwesh on Thu, 13 Feb
 2025 16:45:50 +0530)
Subject: Re: bug#75116: [PATCH] Make 'yank-media' autoselect the best media
 type
References: <87o70yeiih.fsf@HIDDEN> <86r05uxx4i.fsf@HIDDEN>
 <871pxtcxiq.fsf@HIDDEN> <86r05sro63.fsf@HIDDEN>
 <87bjwkbls7.fsf@HIDDEN> <87cygzhjw9.fsf@localhost>
 <868qrn6alg.fsf@HIDDEN> <8734hvhifj.fsf@localhost>
 <867c7769f0.fsf@HIDDEN> <871pw2jdsp.fsf@HIDDEN>
X-Spam-Score: -2.3 (--)
X-Debbugs-Envelope-To: 75116
Cc: yantar92@HIDDEN, pinmacs@HIDDEN, rpluim@HIDDEN,
 75116 <at> debbugs.gnu.org
X-BeenThere: debbugs-submit <at> debbugs.gnu.org
X-Mailman-Version: 2.1.18
Precedence: list
List-Id: <debbugs-submit.debbugs.gnu.org>
List-Unsubscribe: <https://debbugs.gnu.org/cgi-bin/mailman/options/debbugs-submit>, 
 <mailto:debbugs-submit-request <at> debbugs.gnu.org?subject=unsubscribe>
List-Archive: <https://debbugs.gnu.org/cgi-bin/mailman/private/debbugs-submit/>
List-Post: <mailto:debbugs-submit <at> debbugs.gnu.org>
List-Help: <mailto:debbugs-submit-request <at> debbugs.gnu.org?subject=help>
List-Subscribe: <https://debbugs.gnu.org/cgi-bin/mailman/listinfo/debbugs-submit>, 
 <mailto:debbugs-submit-request <at> debbugs.gnu.org?subject=subscribe>
Errors-To: debbugs-submit-bounces <at> debbugs.gnu.org
Sender: "Debbugs-submit" <debbugs-submit-bounces <at> debbugs.gnu.org>
X-Spam-Score: -3.3 (---)

> From: Visuwesh <visuweshm@HIDDEN>
> Cc: Ihor Radchenko <yantar92@HIDDEN>,  75116 <at> debbugs.gnu.org,
>   pinmacs@HIDDEN,  rpluim@HIDDEN
> Date: Thu, 13 Feb 2025 16:45:50 +0530
> 
> Sorry for the >month long delay yet again.  I got caught up with $LIFE.
> The attached patch has the required code and documentation changes.

Thanks.

Please use @sc{mime} in Texinfo and upper-case MIME everywhere else.




Information forwarded to bug-gnu-emacs@HIDDEN:
bug#75116; Package emacs. Full text available.

Message received at 75116 <at> debbugs.gnu.org:


Received: (at 75116) by debbugs.gnu.org; 13 Feb 2025 11:16:05 +0000
From debbugs-submit-bounces <at> debbugs.gnu.org Thu Feb 13 06:16:05 2025
Received: from localhost ([127.0.0.1]:40927 helo=debbugs.gnu.org)
	by debbugs.gnu.org with esmtp (Exim 4.84_2)
	(envelope-from <debbugs-submit-bounces <at> debbugs.gnu.org>)
	id 1tiXCO-0003GB-2h
	for submit <at> debbugs.gnu.org; Thu, 13 Feb 2025 06:16:04 -0500
Received: from mail-pl1-x641.google.com ([2607:f8b0:4864:20::641]:42218)
 by debbugs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128)
 (Exim 4.84_2) (envelope-from <visuweshm@HIDDEN>)
 id 1tiXCK-0003FY-2c
 for 75116 <at> debbugs.gnu.org; Thu, 13 Feb 2025 06:16:01 -0500
Received: by mail-pl1-x641.google.com with SMTP id
 d9443c01a7336-220c92c857aso11834135ad.0
 for <75116 <at> debbugs.gnu.org>; Thu, 13 Feb 2025 03:16:00 -0800 (PST)
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
 d=gmail.com; s=20230601; t=1739445354; x=1740050154; darn=debbugs.gnu.org;
 h=mime-version:user-agent:message-id:date:references:in-reply-to
 :subject:cc:to:from:from:to:cc:subject:date:message-id:reply-to;
 bh=dZsXkBvQAE95ghKhUIR3LLhQ3PSP/1uvibGH/Hw7XNw=;
 b=T660YQk/ckK/Q95VbOdqj4xj+dDupITOF/gJ7RVLzAgFBLRu6WmaxdvU2lpzX9eva2
 G386JPl8UC9cWfFfvB/un+US8PO6UtUmX3TwARWmcv7vrvchWI+f7tAULRRWCBj8MQHc
 ZpR4I4JFYHY840AyqRBPvEA/jZOgVBckM7hqsfgp4qiJl8Nil3Atd6EGEMD1rIqI0L5E
 iKIn8lvKQ04W1g0JH+e+Xc5T7J/6ugF8ITJWzipQqGklgf4SEDmr+rbf7pCGcXOVAoUz
 tPFfDj2Cc3i3DPQQkOyHztBvLQbK8DcnWF7egUYvP7SEJIZtsmM2wS91n40cLlO/Ryub
 ipDQ==
X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
 d=1e100.net; s=20230601; t=1739445354; x=1740050154;
 h=mime-version:user-agent:message-id:date:references:in-reply-to
 :subject:cc:to:from:x-gm-message-state:from:to:cc:subject:date
 :message-id:reply-to;
 bh=dZsXkBvQAE95ghKhUIR3LLhQ3PSP/1uvibGH/Hw7XNw=;
 b=P9dwb8T4ibvBR5WlePfmrRJHkTsffCgCTO8kPFe6V1x1gV4FAMcLTUH4sjqUMV0JSX
 dO6pfuKIBmfcMvMsYyW2IetG8aFApYw6P/yvQhNzflKjtfIxa35ZFwwXNEUgDSHau9Zw
 V6xpOPpNLGsOKwg2iITIVPVNJgFnnYPI8aAmWlasBueuEzEC76PzkioW+KW+WE15gVLq
 g/VeyS40Z5crPq9qN0WGHQbe4EgW7vAzsRC5ZgFa35fNbRalvd/3tkWhrinitWw+oxDS
 bwQUo7D879/xpYCFG72zb0CPfMBTMGn+ZS/I/qqEROpHvhstxZ67iWzOHG7vFrKid4JZ
 XYow==
X-Forwarded-Encrypted: i=1;
 AJvYcCWSXNX5xQ8YjeREl6r0mM7JuxlBM6eyGFMuqoQQ0/b16QpH2OJayRF/tNNAdbXCsVkYci7c8g==@debbugs.gnu.org
X-Gm-Message-State: AOJu0YyDuScKjKrir1PfXqL2I3JgHQvp0yJUX+JmdnRBrESpYYw9bTcr
 +2Hu4/oJ7NEopVJFS0zL6UHHxe0rQszoR5ZQvlldHich078SYCjW
X-Gm-Gg: ASbGncvAXOTRyUB1jf3FAokCcsuqNhLSnCvdk0QH0dpMiC0Jr+m8kJ7iuLDKz5+b3Ka
 3eRaPWfpyWGKQ2PEw0Z1zT5yNX9zrPwCwY5Br0awRjMQUIS2akBroYpLNZ9nDIoSA2qJEpQe6qs
 V48xqi0RbpRRlJig3Gjymak6U/7eN/loiLRiheSC+mjdiDGPL+Gij7+zSeuo3Iu2hFcEcGKVF5I
 QQn3eroqo284gysvSBGkFj78CO7a8XiDBmCmrR2mCfui1WSnAArKHt+yGWbiuOphMebniiICVCD
 tr02jo6g//s1
X-Google-Smtp-Source: AGHT+IFLurDUHZ/IeVmAFp1kgNWvfSxDGJcVAxOTNCQAcp4gWmrj7fERJLVPOZSSBKBKCXlQRks41g==
X-Received: by 2002:a17:902:d48b:b0:21f:58fd:d215 with SMTP id
 d9443c01a7336-220d3509bc4mr35139005ad.11.1739445353987; 
 Thu, 13 Feb 2025 03:15:53 -0800 (PST)
Received: from localhost ([115.240.90.130])
 by smtp.gmail.com with UTF8SMTPSA id
 41be03b00d2f7-adb5813a4c2sm970532a12.24.2025.02.13.03.15.52
 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256);
 Thu, 13 Feb 2025 03:15:53 -0800 (PST)
From: Visuwesh <visuweshm@HIDDEN>
To: Eli Zaretskii <eliz@HIDDEN>
Subject: Re: bug#75116: [PATCH] Make 'yank-media' autoselect the best media
 type
In-Reply-To: <867c7769f0.fsf@HIDDEN> (Eli Zaretskii's message of "Mon, 06 Jan
 2025 21:15:31 +0200")
References: <87o70yeiih.fsf@HIDDEN> <86r05uxx4i.fsf@HIDDEN>
 <871pxtcxiq.fsf@HIDDEN> <86r05sro63.fsf@HIDDEN>
 <87bjwkbls7.fsf@HIDDEN> <87cygzhjw9.fsf@localhost>
 <868qrn6alg.fsf@HIDDEN> <8734hvhifj.fsf@localhost>
 <867c7769f0.fsf@HIDDEN>
Date: Thu, 13 Feb 2025 16:45:50 +0530
Message-ID: <871pw2jdsp.fsf@HIDDEN>
User-Agent: Gnus/5.13 (Gnus v5.13)
MIME-Version: 1.0
Content-Type: multipart/mixed; boundary="=-=-="
X-Spam-Score: 1.0 (+)
X-Debbugs-Envelope-To: 75116
Cc: Ihor Radchenko <yantar92@HIDDEN>, pinmacs@HIDDEN, rpluim@HIDDEN,
 75116 <at> debbugs.gnu.org
X-BeenThere: debbugs-submit <at> debbugs.gnu.org
X-Mailman-Version: 2.1.18
Precedence: list
List-Id: <debbugs-submit.debbugs.gnu.org>
List-Unsubscribe: <https://debbugs.gnu.org/cgi-bin/mailman/options/debbugs-submit>, 
 <mailto:debbugs-submit-request <at> debbugs.gnu.org?subject=unsubscribe>
List-Archive: <https://debbugs.gnu.org/cgi-bin/mailman/private/debbugs-submit/>
List-Post: <mailto:debbugs-submit <at> debbugs.gnu.org>
List-Help: <mailto:debbugs-submit-request <at> debbugs.gnu.org?subject=help>
List-Subscribe: <https://debbugs.gnu.org/cgi-bin/mailman/listinfo/debbugs-submit>, 
 <mailto:debbugs-submit-request <at> debbugs.gnu.org?subject=subscribe>
Errors-To: debbugs-submit-bounces <at> debbugs.gnu.org
Sender: "Debbugs-submit" <debbugs-submit-bounces <at> debbugs.gnu.org>
X-Spam-Score: -1.0 (-)

--=-=-=
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: quoted-printable

[=E0=AE=A4=E0=AE=BF=E0=AE=99=E0=AF=8D=E0=AE=95=E0=AE=B3=E0=AF=8D =E0=AE=9C=
=E0=AE=A9=E0=AE=B5=E0=AE=B0=E0=AE=BF 06, 2025] Eli Zaretskii wrote:

>> From: Ihor Radchenko <yantar92@HIDDEN>
>> Cc: visuweshm@HIDDEN, 75116 <at> debbugs.gnu.org, pinmacs@HIDDEN,
>>  rpluim@HIDDEN
>> Date: Mon, 06 Jan 2025 19:05:20 +0000
>>=20
>> Eli Zaretskii <eliz@HIDDEN> writes:
>>=20
>> >> >>>     User asks to autoselect:
>> >> >>>       1. autoselect-function (a-s-f) returns one media type: we y=
ank it.
>> >> >>
>> >> >> Yes.
>> >> >>
>> >> >>>       2. a-s-f returns multiple media types: we ask the user whic=
h one
>> >> >>>          to yank.
>> >> >>
>> >> >> No, we use the first one.
>> > ...
>> > That's what I mean by "ask": use the default behavior of the command.
>> >
>> > Conceptually, it's the same as the paste operation in other
>> > applications: by default, the application decides which of the formats
>> > to use, and if the user wants to choose, he/she needs to pop up a menu
>> > of available formats in some way and select from there.
>>=20
>> I see the logic.
>>=20
>> I still find returning a list of mimetypes useful though - if user wants
>> to implement an additional filter on top of a-s-f provided by major
>> mode, having a list of mimetypes as input is more flexible.
>
> We could leave the selection to the command level, and have the
> function return a list in all the cases.
>
>> But we need to document that the final choice will be taken as the first
>> element from what a-s-f (possibly adviced) returns.
>
> Under the default behavior of auto-selecting one type, yes.  The
> function should therefore arrange for the first type to be the most
> reasonable/useful one.

Sorry for the >month long delay yet again.  I got caught up with $LIFE.
The attached patch has the required code and documentation changes.


--=-=-=
Content-Type: text/x-diff
Content-Disposition: attachment;
 filename=0001-Make-yank-media-auto-select-the-best-mime-type.patch

From a9199639242c1016fef504278e79b983acd1f59a Mon Sep 17 00:00:00 2001
From: Visuwesh <visuweshm@HIDDEN>
Date: Thu, 13 Feb 2025 16:43:06 +0530
Subject: [PATCH] Make yank-media auto select the best mime type

* lisp/yank-media.el (yank-media-preferred-types): Add new
variable that holds the list of mime types in order of their
preference.
(yank-media-autoselect-function): Add new variable and function
to choose the most preferred media type.
(yank-media): Make 'yank-media' choose the most preferred mime
type by default.
* doc/emacs/killing.texi (Clipboard):
* doc/lispref/frames.texi (Yanking Media): Document the new
behaviour, and the new variables.
* etc/NEWS: Announce the change.  (Bug#75116)
---
 doc/emacs/killing.texi  |  6 ++-
 doc/lispref/frames.texi | 24 ++++++++++++
 etc/NEWS                |  8 ++++
 lisp/yank-media.el      | 85 ++++++++++++++++++++++++++++++++++++-----
 4 files changed, 111 insertions(+), 12 deletions(-)

diff --git a/doc/emacs/killing.texi b/doc/emacs/killing.texi
index 070e15dee76..aee68608bbf 100644
--- a/doc/emacs/killing.texi
+++ b/doc/emacs/killing.texi
@@ -587,8 +587,10 @@ Clipboard
 instance, a web browser will usually let you choose ``Copy Image'' on
 images, and this image will be put on the clipboard.  On capable
 platforms, Emacs can yank these objects with the @code{yank-media}
-command---but only in modes that have support for it (@pxref{Yanking
-Media,,, elisp, The Emacs Lisp Reference Manual}).
+command---but only in modes that have support for it.  By default, it
+auto-selects the preferred media type available in the clipboard but
+this can be overriden by giving the prefix argument to the command
+(@pxref{Yanking Media,,, elisp, The Emacs Lisp Reference Manual}).
 
 @cindex clipboard manager
 @vindex x-select-enable-clipboard-manager
diff --git a/doc/lispref/frames.texi b/doc/lispref/frames.texi
index bc2d6b07ae8..218cb5e0016 100644
--- a/doc/lispref/frames.texi
+++ b/doc/lispref/frames.texi
@@ -4812,6 +4812,30 @@ Yanking Media
 @code{gui-get-selection}.
 @end defun
 
+The @code{yank-media} command auto selects the preferred mime type by
+default.  The rules used for the selection can be controlled through the
+variables @code{yank-media-autoselect-function} and
+@code{yank-media-preferred-types}.
+
+@defvar yank-media-autoselect-function
+This variable should specify a function that will be called with the
+list of mime types available for the current major mode, and should
+return a list of preferred mime types to use.  The first mime type in
+the list will always be used by the @code{yank-media} command when auto
+selection is requested.
+@end defvar
+
+@defvar yank-media-preferred-types
+This variable changes the default selection process of
+@code{yank-media-autoselect-function}.  It is a list that should contain
+the sole mime type to choose in the order of their preference.  It can
+also contain a function in which case it is called with the list of
+available mime types and must return a list of preferred mime types in
+order of their preference.  This list is passed onto the
+@code{yank-media} command so the first element of the returned list is
+chosen when auto selection is requested.
+@end defvar
+
 The @code{yank-media-types} command presents a list of selection data
 types that are currently available, which is useful when implementing
 yank-media handlers; for programs generally offer an eclectic and
diff --git a/etc/NEWS b/etc/NEWS
index 419c9bd8d20..b60d813f85f 100644
--- a/etc/NEWS
+++ b/etc/NEWS
@@ -1176,6 +1176,14 @@ This macro adds the current buffer to 'inhibit-auto-revert-buffers',
 runs its body, and removes the current buffer from
 'inhibit-auto-revert-buffers' afterwards.
 
+** Yank media
+
++++
+*** 'yank-media' now auto-selects the most preferred mime type.
+Major-mode authors can customise the variables
+'yank-media-autoselect-function' and/or 'yank-media-preferred-types' to
+change the selection rules.
+
 
 * New Modes and Packages in Emacs 31.1
 
diff --git a/lisp/yank-media.el b/lisp/yank-media.el
index 600cc0ffaf5..46427711652 100644
--- a/lisp/yank-media.el
+++ b/lisp/yank-media.el
@@ -29,19 +29,67 @@
 
 (defvar yank-media--registered-handlers nil)
 
+(defvar yank-media-autoselect-function #'yank-media-autoselect-function
+  "Function to auto select the best mime types when many are available.
+The function is called with a list of mime types that have handler in
+the current buffer, and should return the list of mime types to use in
+order of their priority.  When `yank-media' auto-selects the mime type,
+it will always choose the first one of the returned list.
+Major-mode authors can change this variable to influence the selection
+process.")
+
+(defvar yank-media-preferred-types
+  `(;; Check first since LibreOffice also puts a PNG image in the
+    ;; clipboard when a table cell is copied.
+    application/x-libreoffice-tsvc
+    ;; Give PNG more priority.
+    image/png
+    image/jpeg
+    ;; These are files copied/cut to the clipboard from a file manager
+    ;; in a GNU/Linux and/or BSD environment.
+    ,@(when (memq window-system '(x pgtk))
+        (list (lambda (mimetypes)
+                (ensure-list
+                 (seq-find (lambda (type)
+                             (string-match-p "x-special/\\(gnome\\|KDE\\|mate\\)-copied-files"
+                                             (symbol-name type)))
+                           mimetypes)))))
+    ;; FIXME: We should have a way to handle text/rtf.
+    text/html)
+  "List of mime types in the order of preference.
+Each element in the list should be a symbol to choose that mime type
+exclusively, or a function of one argument and should return the list of
+mime types to use in order of their priority or nil if no preferred type
+is found.
+Major-mode authors can change this variable to influence the selection
+process, or by directly changing the variable
+`yank-media-autoselect-function'.")
+
+(defun yank-media-autoselect-function (mimetypes)
+  (catch 'preferred
+    (dolist (typ yank-media-preferred-types)
+      (let ((ret (if (functionp typ)
+                     (funcall typ mimetypes)
+                   (and (memq typ mimetypes) (list typ)))))
+        (when ret (throw 'preferred ret))))))
+
 ;;;###autoload
-(defun yank-media ()
+(defun yank-media (&optional noselect)
   "Yank media (images, HTML and the like) from the clipboard.
 This command depends on the current major mode having support for
 accepting the media type.  The mode has to register itself using
 the `yank-media-handler' mechanism.
+Optional argument NOSELECT non-nil (interactively, with a prefix
+argument) means to skip auto-selecting the best mimetype and ask
+for the media type to use.
 
 Also see `yank-media-types' for a command that lets you explore
 all the different selection types."
-  (interactive)
+  (interactive "P")
   (unless yank-media--registered-handlers
     (user-error "The `%s' mode hasn't registered any handlers" major-mode))
-  (let ((all-types nil))
+  (let ((all-types nil)
+        pref-type)
     (pcase-dolist (`(,handled-type . ,handler)
                    yank-media--registered-handlers)
       (dolist (type (yank-media--find-matching-media handled-type))
@@ -49,18 +97,35 @@ yank-media
     (unless all-types
       (user-error
        "No handler in the current buffer for anything on the clipboard"))
-    ;; We have a handler in the current buffer; if there's just
-    ;; matching type, just call the handler.
-    (if (length= all-types 1)
+    (setq pref-type (and (null noselect)
+                         (funcall yank-media-autoselect-function
+                                  (mapcar #'car all-types))))
+    (cond
+     ;; We are asked to autoselect and have a preferred mime type.
+     ((and (null noselect) pref-type)
+      (funcall (cdr (assq (car pref-type) all-types))
+               (car pref-type)
+               (yank-media--get-selection (car pref-type))))
+     ;; We are asked to autoselect and no preferred mime type.
+     ((and (null noselect) (null pref-type))
+      (message
+       (substitute-command-keys
+        "No preferred mime type to yank, try \\[universal-argument] \\[yank-media]")))
+     ;; No autoselection and there's only one media type available.
+     ((and noselect (length= all-types 1))
+      (when (y-or-n-p (format "Yank the `%s' clipboard item?"
+                              (caar all-types)))
         (funcall (cdar all-types) (caar all-types)
-                 (yank-media--get-selection (caar all-types)))
-      ;; More than one type the user for what type to insert.
+                 (yank-media--get-selection (caar all-types)))))
+     ;; No autoselection and multiple media types available.
+     ((and noselect (length> all-types 1))
       (let ((type
              (intern
               (completing-read "Several types available, choose one: "
-                               (mapcar #'car all-types) nil t))))
+                               (or pref-type (mapcar #'car all-types))
+                               nil t))))
         (funcall (alist-get type all-types)
-                 type (yank-media--get-selection type))))))
+                 type (yank-media--get-selection type)))))))
 
 (defun yank-media--find-matching-media (handled-type)
   (seq-filter
-- 
2.47.2


--=-=-=--




Information forwarded to bug-gnu-emacs@HIDDEN:
bug#75116; Package emacs. Full text available.

Message received at 75116 <at> debbugs.gnu.org:


Received: (at 75116) by debbugs.gnu.org; 6 Jan 2025 19:15:46 +0000
From debbugs-submit-bounces <at> debbugs.gnu.org Mon Jan 06 14:15:46 2025
Received: from localhost ([127.0.0.1]:40112 helo=debbugs.gnu.org)
	by debbugs.gnu.org with esmtp (Exim 4.84_2)
	(envelope-from <debbugs-submit-bounces <at> debbugs.gnu.org>)
	id 1tUsZm-0005eN-0H
	for submit <at> debbugs.gnu.org; Mon, 06 Jan 2025 14:15:46 -0500
Received: from eggs.gnu.org ([2001:470:142:3::10]:40318)
 by debbugs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256)
 (Exim 4.84_2) (envelope-from <eliz@HIDDEN>) id 1tUsZj-0005e5-LQ
 for 75116 <at> debbugs.gnu.org; Mon, 06 Jan 2025 14:15:44 -0500
Received: from fencepost.gnu.org ([2001:470:142:3::e])
 by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256)
 (Exim 4.90_1) (envelope-from <eliz@HIDDEN>)
 id 1tUsZc-0007HC-Bn; Mon, 06 Jan 2025 14:15:36 -0500
DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=gnu.org;
 s=fencepost-gnu-org; h=References:Subject:In-Reply-To:To:From:Date:
 mime-version; bh=0wmgBumCXiMfpfycK59wnwp/+m3OLLPVg5avya9F0jE=; b=DiHJfIg9o4Jy
 aPPDdNslkpjMeqjWxW9W8UzGCAk6fipBRB75HtaCvGvgTBXI/wkn95lgus+RyCewkbANJxifMWjge
 YGAFaCterG0WTe4BzOvF37rhdIRVe0eULmWROj8f8W9F0jzoYI4UdfIVOazITO82sB/qckNYJVUSl
 3aD20dCiyYG7dO1WWUnJES6uNCLjMEONpQgXHkgW4w07qsTsRCjTWUkTSPOKucX7jQuJQuT1sXIde
 oHaY5Q18t6aH1wLCZVtZ45F7ekf3DDQiocDXaTfgUZmSO33Q7PFI50p7HlHBQ2tyOYa2Rm9WXMfpj
 5eZPbfcsUmsHR8zgGB85cQ==;
Date: Mon, 06 Jan 2025 21:15:31 +0200
Message-Id: <867c7769f0.fsf@HIDDEN>
From: Eli Zaretskii <eliz@HIDDEN>
To: Ihor Radchenko <yantar92@HIDDEN>
In-Reply-To: <8734hvhifj.fsf@localhost> (message from Ihor Radchenko on Mon,
 06 Jan 2025 19:05:20 +0000)
Subject: Re: bug#75116: [PATCH] Make 'yank-media' autoselect the best media
 type
References: <87o70yeiih.fsf@HIDDEN> <86r05uxx4i.fsf@HIDDEN>
 <871pxtcxiq.fsf@HIDDEN> <86r05sro63.fsf@HIDDEN>
 <87bjwkbls7.fsf@HIDDEN> <87cygzhjw9.fsf@localhost>
 <868qrn6alg.fsf@HIDDEN> <8734hvhifj.fsf@localhost>
X-Spam-Score: -2.3 (--)
X-Debbugs-Envelope-To: 75116
Cc: pinmacs@HIDDEN, rpluim@HIDDEN, 75116 <at> debbugs.gnu.org,
 visuweshm@HIDDEN
X-BeenThere: debbugs-submit <at> debbugs.gnu.org
X-Mailman-Version: 2.1.18
Precedence: list
List-Id: <debbugs-submit.debbugs.gnu.org>
List-Unsubscribe: <https://debbugs.gnu.org/cgi-bin/mailman/options/debbugs-submit>, 
 <mailto:debbugs-submit-request <at> debbugs.gnu.org?subject=unsubscribe>
List-Archive: <https://debbugs.gnu.org/cgi-bin/mailman/private/debbugs-submit/>
List-Post: <mailto:debbugs-submit <at> debbugs.gnu.org>
List-Help: <mailto:debbugs-submit-request <at> debbugs.gnu.org?subject=help>
List-Subscribe: <https://debbugs.gnu.org/cgi-bin/mailman/listinfo/debbugs-submit>, 
 <mailto:debbugs-submit-request <at> debbugs.gnu.org?subject=subscribe>
Errors-To: debbugs-submit-bounces <at> debbugs.gnu.org
Sender: "Debbugs-submit" <debbugs-submit-bounces <at> debbugs.gnu.org>
X-Spam-Score: -3.3 (---)

> From: Ihor Radchenko <yantar92@HIDDEN>
> Cc: visuweshm@HIDDEN, 75116 <at> debbugs.gnu.org, pinmacs@HIDDEN,
>  rpluim@HIDDEN
> Date: Mon, 06 Jan 2025 19:05:20 +0000
> 
> Eli Zaretskii <eliz@HIDDEN> writes:
> 
> >> >>>     User asks to autoselect:
> >> >>>       1. autoselect-function (a-s-f) returns one media type: we yank it.
> >> >>
> >> >> Yes.
> >> >>
> >> >>>       2. a-s-f returns multiple media types: we ask the user which one
> >> >>>          to yank.
> >> >>
> >> >> No, we use the first one.
> > ...
> > That's what I mean by "ask": use the default behavior of the command.
> >
> > Conceptually, it's the same as the paste operation in other
> > applications: by default, the application decides which of the formats
> > to use, and if the user wants to choose, he/she needs to pop up a menu
> > of available formats in some way and select from there.
> 
> I see the logic.
> 
> I still find returning a list of mimetypes useful though - if user wants
> to implement an additional filter on top of a-s-f provided by major
> mode, having a list of mimetypes as input is more flexible.

We could leave the selection to the command level, and have the
function return a list in all the cases.

> But we need to document that the final choice will be taken as the first
> element from what a-s-f (possibly adviced) returns.

Under the default behavior of auto-selecting one type, yes.  The
function should therefore arrange for the first type to be the most
reasonable/useful one.




Information forwarded to bug-gnu-emacs@HIDDEN:
bug#75116; Package emacs. Full text available.

Message received at 75116 <at> debbugs.gnu.org:


Received: (at 75116) by debbugs.gnu.org; 6 Jan 2025 19:03:19 +0000
From debbugs-submit-bounces <at> debbugs.gnu.org Mon Jan 06 14:03:18 2025
Received: from localhost ([127.0.0.1]:40096 helo=debbugs.gnu.org)
	by debbugs.gnu.org with esmtp (Exim 4.84_2)
	(envelope-from <debbugs-submit-bounces <at> debbugs.gnu.org>)
	id 1tUsNf-00050L-29
	for submit <at> debbugs.gnu.org; Mon, 06 Jan 2025 14:03:18 -0500
Received: from mout02.posteo.de ([185.67.36.66]:36439)
 by debbugs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256)
 (Exim 4.84_2) (envelope-from <yantar92@HIDDEN>)
 id 1tUsNb-000500-Oa
 for 75116 <at> debbugs.gnu.org; Mon, 06 Jan 2025 14:03:13 -0500
Received: from submission (posteo.de [185.67.36.169]) 
 by mout02.posteo.de (Postfix) with ESMTPS id 1FF47240101
 for <75116 <at> debbugs.gnu.org>; Mon,  6 Jan 2025 20:03:02 +0100 (CET)
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=posteo.net; s=2017;
 t=1736190184; bh=3xmSIh/11RuliO/93XUS1I1DoMPJvPV3Pqk9ZGI9u80=;
 h=From:To:Cc:Subject:Date:Message-ID:MIME-Version:Content-Type:
 From;
 b=lu+8X8c+e8tm2/9bOA8b06PxnsyyYXB4OoJiIV/25VYe2oEDdR8JZzmfhCEEjmRbp
 x50cJ07pGg9SmuVqK0plkCf2OJpHLU2KuioxazohSwz1cMcFFiV4xvN2DhU2xAEqMP
 FehT8AK3aghck6gA06o7y6GKS8tnDsxPgzx+drbOhkEVtciM2esEHRd10nz1fz3LT2
 CWfr+4/Ey/OelLIUR+HGTlS0hVtXRHkc1drWpK4m/V8j81vFCOhnVEf9JB5eeETxL1
 GJ3kYBHBhRXvSMpAsqXnik0A6p64PsNnSDzSDDTZPTOUophj3/rmERCOjNrBlFA9OF
 PWXLPcGHht3ZA==
Received: from customer (localhost [127.0.0.1])
 by submission (posteo.de) with ESMTPSA id 4YRk8j5kf4z9rxF;
 Mon,  6 Jan 2025 20:03:01 +0100 (CET)
From: Ihor Radchenko <yantar92@HIDDEN>
To: Eli Zaretskii <eliz@HIDDEN>
Subject: Re: bug#75116: [PATCH] Make 'yank-media' autoselect the best media
 type
In-Reply-To: <868qrn6alg.fsf@HIDDEN>
References: <87o70yeiih.fsf@HIDDEN> <86r05uxx4i.fsf@HIDDEN>
 <871pxtcxiq.fsf@HIDDEN> <86r05sro63.fsf@HIDDEN>
 <87bjwkbls7.fsf@HIDDEN> <87cygzhjw9.fsf@localhost>
 <868qrn6alg.fsf@HIDDEN>
Date: Mon, 06 Jan 2025 19:05:20 +0000
Message-ID: <8734hvhifj.fsf@localhost>
MIME-Version: 1.0
Content-Type: text/plain
X-Spam-Score: -2.3 (--)
X-Debbugs-Envelope-To: 75116
Cc: pinmacs@HIDDEN, rpluim@HIDDEN, 75116 <at> debbugs.gnu.org,
 visuweshm@HIDDEN
X-BeenThere: debbugs-submit <at> debbugs.gnu.org
X-Mailman-Version: 2.1.18
Precedence: list
List-Id: <debbugs-submit.debbugs.gnu.org>
List-Unsubscribe: <https://debbugs.gnu.org/cgi-bin/mailman/options/debbugs-submit>, 
 <mailto:debbugs-submit-request <at> debbugs.gnu.org?subject=unsubscribe>
List-Archive: <https://debbugs.gnu.org/cgi-bin/mailman/private/debbugs-submit/>
List-Post: <mailto:debbugs-submit <at> debbugs.gnu.org>
List-Help: <mailto:debbugs-submit-request <at> debbugs.gnu.org?subject=help>
List-Subscribe: <https://debbugs.gnu.org/cgi-bin/mailman/listinfo/debbugs-submit>, 
 <mailto:debbugs-submit-request <at> debbugs.gnu.org?subject=subscribe>
Errors-To: debbugs-submit-bounces <at> debbugs.gnu.org
Sender: "Debbugs-submit" <debbugs-submit-bounces <at> debbugs.gnu.org>
X-Spam-Score: -3.3 (---)

Eli Zaretskii <eliz@HIDDEN> writes:

>> >>>     User asks to autoselect:
>> >>>       1. autoselect-function (a-s-f) returns one media type: we yank it.
>> >>
>> >> Yes.
>> >>
>> >>>       2. a-s-f returns multiple media types: we ask the user which one
>> >>>          to yank.
>> >>
>> >> No, we use the first one.
> ...
> That's what I mean by "ask": use the default behavior of the command.
>
> Conceptually, it's the same as the paste operation in other
> applications: by default, the application decides which of the formats
> to use, and if the user wants to choose, he/she needs to pop up a menu
> of available formats in some way and select from there.

I see the logic.

I still find returning a list of mimetypes useful though - if user wants
to implement an additional filter on top of a-s-f provided by major
mode, having a list of mimetypes as input is more flexible.

But we need to document that the final choice will be taken as the first
element from what a-s-f (possibly adviced) returns.

-- 
Ihor Radchenko // yantar92,
Org mode maintainer,
Learn more about Org mode at <https://orgmode.org/>.
Support Org development at <https://liberapay.com/org-mode>,
or support my work at <https://liberapay.com/yantar92>




Information forwarded to bug-gnu-emacs@HIDDEN:
bug#75116; Package emacs. Full text available.

Message received at 75116 <at> debbugs.gnu.org:


Received: (at 75116) by debbugs.gnu.org; 6 Jan 2025 18:50:32 +0000
From debbugs-submit-bounces <at> debbugs.gnu.org Mon Jan 06 13:50:32 2025
Received: from localhost ([127.0.0.1]:40083 helo=debbugs.gnu.org)
	by debbugs.gnu.org with esmtp (Exim 4.84_2)
	(envelope-from <debbugs-submit-bounces <at> debbugs.gnu.org>)
	id 1tUsBL-0004NR-Ga
	for submit <at> debbugs.gnu.org; Mon, 06 Jan 2025 13:50:31 -0500
Received: from eggs.gnu.org ([2001:470:142:3::10]:59676)
 by debbugs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256)
 (Exim 4.84_2) (envelope-from <eliz@HIDDEN>) id 1tUsBJ-0004N7-Ji
 for 75116 <at> debbugs.gnu.org; Mon, 06 Jan 2025 13:50:30 -0500
Received: from fencepost.gnu.org ([2001:470:142:3::e])
 by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256)
 (Exim 4.90_1) (envelope-from <eliz@HIDDEN>)
 id 1tUsBD-0004UF-D3; Mon, 06 Jan 2025 13:50:23 -0500
DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=gnu.org;
 s=fencepost-gnu-org; h=References:Subject:In-Reply-To:To:From:Date:
 mime-version; bh=UFMAWmhsfJ43A9wyOegWoeTdd9b+UN8N7bmRhDthR9g=; b=fXkdkRn6qhsp
 oyrltblR3mdak+6lFdp6h1yIVDwb8AOLL35vfgfk78QMzNeE/Xz4h46RJb8p6iqwps9d9OtjkNXcp
 mmdR36IYmiIAAcefj6mYUNlzYcDCn+9QEr0wqiCJu8hiev8pf0LksZwhcDrKR/kZ+9W+hHTbwAnQo
 7LWe06rrcLvnOaC0sI8zTUnYLKJnH+YM8BFinIvpd07d51PQAMssh/ieplfm2DabtDjI6ZyXMiny+
 wtcXoHxMJnmTEUK08osuuRKYZ2rSIjpeLadPvgKe5X0xnUsZ6fpiZFbLBfULP5pSQNFkRKYCp6pAv
 pOF6Ctktxs40DV5XN4BjEQ==;
Date: Mon, 06 Jan 2025 20:50:03 +0200
Message-Id: <868qrn6alg.fsf@HIDDEN>
From: Eli Zaretskii <eliz@HIDDEN>
To: Ihor Radchenko <yantar92@HIDDEN>
In-Reply-To: <87cygzhjw9.fsf@localhost> (message from Ihor Radchenko on Mon,
 06 Jan 2025 18:33:42 +0000)
Subject: Re: bug#75116: [PATCH] Make 'yank-media' autoselect the best media
 type
References: <87o70yeiih.fsf@HIDDEN> <86r05uxx4i.fsf@HIDDEN>
 <871pxtcxiq.fsf@HIDDEN> <86r05sro63.fsf@HIDDEN>
 <87bjwkbls7.fsf@HIDDEN> <87cygzhjw9.fsf@localhost>
X-Spam-Score: -2.3 (--)
X-Debbugs-Envelope-To: 75116
Cc: pinmacs@HIDDEN, rpluim@HIDDEN, 75116 <at> debbugs.gnu.org,
 visuweshm@HIDDEN
X-BeenThere: debbugs-submit <at> debbugs.gnu.org
X-Mailman-Version: 2.1.18
Precedence: list
List-Id: <debbugs-submit.debbugs.gnu.org>
List-Unsubscribe: <https://debbugs.gnu.org/cgi-bin/mailman/options/debbugs-submit>, 
 <mailto:debbugs-submit-request <at> debbugs.gnu.org?subject=unsubscribe>
List-Archive: <https://debbugs.gnu.org/cgi-bin/mailman/private/debbugs-submit/>
List-Post: <mailto:debbugs-submit <at> debbugs.gnu.org>
List-Help: <mailto:debbugs-submit-request <at> debbugs.gnu.org?subject=help>
List-Subscribe: <https://debbugs.gnu.org/cgi-bin/mailman/listinfo/debbugs-submit>, 
 <mailto:debbugs-submit-request <at> debbugs.gnu.org?subject=subscribe>
Errors-To: debbugs-submit-bounces <at> debbugs.gnu.org
Sender: "Debbugs-submit" <debbugs-submit-bounces <at> debbugs.gnu.org>
X-Spam-Score: -3.3 (---)

> From: Ihor Radchenko <yantar92@HIDDEN>
> Cc: Eli Zaretskii <eliz@HIDDEN>, 75116 <at> debbugs.gnu.org, pinmacs@HIDDEN,
>  rpluim@HIDDEN
> Date: Mon, 06 Jan 2025 18:33:42 +0000
> 
> Visuwesh <visuweshm@HIDDEN> writes:
> 
> >>> >> +    (setq pref-type (and (null noselect)
> >>> >> +                         (funcall yank-media-autoselect-function
> >>> >> +                                  (mapcar #'car all-types))))
> >>> >> +    (cond
> >>> >> +     ;; We have one preferred mime type so use it unconditionally.
> >>> >> +     ((and pref-type (symbolp pref-type))
> >>> >> +      (funcall (cdr (assq pref-type all-types)) pref-type
> >>> >> +               (yank-media--get-selection pref-type)))
> >>> >> +     ;; The user chose to not autoselect and there's just a single type,
> >>> >> +     ;; just call the handler.
> >>> >> +     ((and (null pref-type) (length= all-types 1))
> >>> >> +      (funcall (cdar all-types) (caar all-types)
> >>> >> +               (yank-media--get-selection (caar all-types))))
> >>> >
> >>> > This goes against what the doc string says.  And I think the doc
> >>> > string describes a better behavior: if the user asked not to
> >>> > auto-select, we shouldn't, even if there's just one type available.
> >>> > We should instead ask the user whether to yank that type, because the
> >>> > user could decide she doesn't want that type, even it it's the only
> >>> > one.
> >>> ...
> >>> I want to ensure we are on the same page wrt UI here:
> >>> 
> >>>     User asks to autoselect:
> >>>       1. autoselect-function (a-s-f) returns one media type: we yank it.
> >>
> >> Yes.
> >>
> >>>       2. a-s-f returns multiple media types: we ask the user which one
> >>>          to yank.
> >>
> >> No, we use the first one.
> >
> > Doesn't this mean there is pratically no difference between (1) and (2)?
> > Ihor, is this okay?  Or did you have something else in mind when you
> > asked for the possibility to return multiple types?
> 
> Eli, what exactly do you mean by "user asks to autoselect"?
> AFAIU, autoselect is done by default. User cannot ask for it. User can
> only supply a prefix argument to explicitly disable autoselection.

That's what I mean by "ask": use the default behavior of the command.

Conceptually, it's the same as the paste operation in other
applications: by default, the application decides which of the formats
to use, and if the user wants to choose, he/she needs to pop up a menu
of available formats in some way and select from there.




Information forwarded to bug-gnu-emacs@HIDDEN:
bug#75116; Package emacs. Full text available.

Message received at 75116 <at> debbugs.gnu.org:


Received: (at 75116) by debbugs.gnu.org; 6 Jan 2025 18:31:35 +0000
From debbugs-submit-bounces <at> debbugs.gnu.org Mon Jan 06 13:31:35 2025
Received: from localhost ([127.0.0.1]:40039 helo=debbugs.gnu.org)
	by debbugs.gnu.org with esmtp (Exim 4.84_2)
	(envelope-from <debbugs-submit-bounces <at> debbugs.gnu.org>)
	id 1tUrt0-0003Lo-MU
	for submit <at> debbugs.gnu.org; Mon, 06 Jan 2025 13:31:35 -0500
Received: from mout01.posteo.de ([185.67.36.65]:49385)
 by debbugs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256)
 (Exim 4.84_2) (envelope-from <yantar92@HIDDEN>)
 id 1tUrsx-0003LW-Ns
 for 75116 <at> debbugs.gnu.org; Mon, 06 Jan 2025 13:31:33 -0500
Received: from submission (posteo.de [185.67.36.169]) 
 by mout01.posteo.de (Postfix) with ESMTPS id 6D20A240027
 for <75116 <at> debbugs.gnu.org>; Mon,  6 Jan 2025 19:31:24 +0100 (CET)
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=posteo.net; s=2017;
 t=1736188285; bh=tdRABS5tNDiiG7rr/IIHvUqAe3gE3XOapvrmdJxo5XM=;
 h=From:To:Cc:Subject:Date:Message-ID:MIME-Version:Content-Type:
 From;
 b=HGYXB/DgqIZwGPoIn+mEndVVpq1Xd0UCbqK7Je08Gy4R1frReeVYuie1lIT5J4f/E
 Pbkiflnz+BhRxK71gee3P1GN5GnaAv5eeR1Ci3XT50ZOjPIR5fKSJuUOKZ1Sgi21+3
 VY05S0Ohma4R4y4BEHZgh+dcXRSF4bLcHGOSuRPR+SBNXqKyNrmKG+aHPTg55xGa0q
 cr+1PRw2F+/qYjvVoI5Xn3p632Jgi6V9qW4B3lf7eH3rb8Y2r3bh74WMZZgX84zgHk
 bKb2M0r52RVwZFaiOEU5ZjfkESj4vQfJei0Gf+hqBIDf1E0pvoua5HyY1/qrv9GYzc
 x9liy8zP49wSA==
Received: from customer (localhost [127.0.0.1])
 by submission (posteo.de) with ESMTPSA id 4YRjSD0N82z9rxR;
 Mon,  6 Jan 2025 19:31:24 +0100 (CET)
From: Ihor Radchenko <yantar92@HIDDEN>
To: Visuwesh <visuweshm@HIDDEN>
Subject: Re: bug#75116: [PATCH] Make 'yank-media' autoselect the best media
 type
In-Reply-To: <87bjwkbls7.fsf@HIDDEN>
References: <87o70yeiih.fsf@HIDDEN> <86r05uxx4i.fsf@HIDDEN>
 <871pxtcxiq.fsf@HIDDEN> <86r05sro63.fsf@HIDDEN>
 <87bjwkbls7.fsf@HIDDEN>
Date: Mon, 06 Jan 2025 18:33:42 +0000
Message-ID: <87cygzhjw9.fsf@localhost>
MIME-Version: 1.0
Content-Type: text/plain
X-Spam-Score: -2.3 (--)
X-Debbugs-Envelope-To: 75116
Cc: Eli Zaretskii <eliz@HIDDEN>, pinmacs@HIDDEN, rpluim@HIDDEN,
 75116 <at> debbugs.gnu.org
X-BeenThere: debbugs-submit <at> debbugs.gnu.org
X-Mailman-Version: 2.1.18
Precedence: list
List-Id: <debbugs-submit.debbugs.gnu.org>
List-Unsubscribe: <https://debbugs.gnu.org/cgi-bin/mailman/options/debbugs-submit>, 
 <mailto:debbugs-submit-request <at> debbugs.gnu.org?subject=unsubscribe>
List-Archive: <https://debbugs.gnu.org/cgi-bin/mailman/private/debbugs-submit/>
List-Post: <mailto:debbugs-submit <at> debbugs.gnu.org>
List-Help: <mailto:debbugs-submit-request <at> debbugs.gnu.org?subject=help>
List-Subscribe: <https://debbugs.gnu.org/cgi-bin/mailman/listinfo/debbugs-submit>, 
 <mailto:debbugs-submit-request <at> debbugs.gnu.org?subject=subscribe>
Errors-To: debbugs-submit-bounces <at> debbugs.gnu.org
Sender: "Debbugs-submit" <debbugs-submit-bounces <at> debbugs.gnu.org>
X-Spam-Score: -3.3 (---)

Visuwesh <visuweshm@HIDDEN> writes:

>>> >> +    (setq pref-type (and (null noselect)
>>> >> +                         (funcall yank-media-autoselect-function
>>> >> +                                  (mapcar #'car all-types))))
>>> >> +    (cond
>>> >> +     ;; We have one preferred mime type so use it unconditionally.
>>> >> +     ((and pref-type (symbolp pref-type))
>>> >> +      (funcall (cdr (assq pref-type all-types)) pref-type
>>> >> +               (yank-media--get-selection pref-type)))
>>> >> +     ;; The user chose to not autoselect and there's just a single type,
>>> >> +     ;; just call the handler.
>>> >> +     ((and (null pref-type) (length= all-types 1))
>>> >> +      (funcall (cdar all-types) (caar all-types)
>>> >> +               (yank-media--get-selection (caar all-types))))
>>> >
>>> > This goes against what the doc string says.  And I think the doc
>>> > string describes a better behavior: if the user asked not to
>>> > auto-select, we shouldn't, even if there's just one type available.
>>> > We should instead ask the user whether to yank that type, because the
>>> > user could decide she doesn't want that type, even it it's the only
>>> > one.
>>> ...
>>> I want to ensure we are on the same page wrt UI here:
>>> 
>>>     User asks to autoselect:
>>>       1. autoselect-function (a-s-f) returns one media type: we yank it.
>>
>> Yes.
>>
>>>       2. a-s-f returns multiple media types: we ask the user which one
>>>          to yank.
>>
>> No, we use the first one.
>
> Doesn't this mean there is pratically no difference between (1) and (2)?
> Ihor, is this okay?  Or did you have something else in mind when you
> asked for the possibility to return multiple types?

Eli, what exactly do you mean by "user asks to autoselect"?
AFAIU, autoselect is done by default. User cannot ask for it. User can
only supply a prefix argument to explicitly disable autoselection.

-- 
Ihor Radchenko // yantar92,
Org mode maintainer,
Learn more about Org mode at <https://orgmode.org/>.
Support Org development at <https://liberapay.com/org-mode>,
or support my work at <https://liberapay.com/yantar92>




Information forwarded to bug-gnu-emacs@HIDDEN:
bug#75116; Package emacs. Full text available.

Message received at 75116 <at> debbugs.gnu.org:


Received: (at 75116) by debbugs.gnu.org; 6 Jan 2025 14:10:39 +0000
From debbugs-submit-bounces <at> debbugs.gnu.org Mon Jan 06 09:10:39 2025
Received: from localhost ([127.0.0.1]:37379 helo=debbugs.gnu.org)
	by debbugs.gnu.org with esmtp (Exim 4.84_2)
	(envelope-from <debbugs-submit-bounces <at> debbugs.gnu.org>)
	id 1tUnoV-0006Ib-Cr
	for submit <at> debbugs.gnu.org; Mon, 06 Jan 2025 09:10:39 -0500
Received: from [2001:470:142:3::10] (port=53654 helo=eggs.gnu.org)
 by debbugs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256)
 (Exim 4.84_2) (envelope-from <eliz@HIDDEN>) id 1tUnoT-0006IB-9Q
 for 75116 <at> debbugs.gnu.org; Mon, 06 Jan 2025 09:10:38 -0500
Received: from fencepost.gnu.org ([2001:470:142:3::e])
 by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256)
 (Exim 4.90_1) (envelope-from <eliz@HIDDEN>)
 id 1tUnoE-0006yy-TL; Mon, 06 Jan 2025 09:10:24 -0500
DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=gnu.org;
 s=fencepost-gnu-org; h=MIME-version:References:Subject:In-Reply-To:To:From:
 Date; bh=qWgyj3p/G/3UjKfm0X3vPP1b8Df7OIwXYeG8vHRBuM8=; b=O5CzDsyaoDbM7ypFwyxQ
 3L8gwjzQoPuYja6odLziZvasDHfqNxaOqDQg29GPgrvH80DTAzS5lo1KNxHzJdKzvV3BqycxaINCJ
 qLLHJjQ+6xYxZpfc5uRcSw6jFgBbfzceMMVlM0G9Y7XaXM80abzjqCbzrKApc0/bdD9lW1epnjoY/
 BEN0JDSIRdEn+0M/ZfjeD8VXOtFF1KHOzv2Bl791CADTi3k37lYfxjbWH7YUawQ+TVR302POw6HQX
 NB58Jq8h3LYlrlE1lG6EYq/GxaOOojEr7U5GYVYZuzxPU/wwfKeKzsys25mJQlX0nMMO6cBE+a40a
 480h+7Xdsd627g==;
Date: Mon, 06 Jan 2025 16:10:06 +0200
Message-Id: <86o70k58zl.fsf@HIDDEN>
From: Eli Zaretskii <eliz@HIDDEN>
To: Visuwesh <visuweshm@HIDDEN>
In-Reply-To: <87bjwkbls7.fsf@HIDDEN> (message from Visuwesh on Mon, 06 Jan
 2025 10:07:12 +0530)
Subject: Re: bug#75116: [PATCH] Make 'yank-media' autoselect the best media
 type
References: <87o70yeiih.fsf@HIDDEN> <86r05uxx4i.fsf@HIDDEN>
 <871pxtcxiq.fsf@HIDDEN> <86r05sro63.fsf@HIDDEN> <87bjwkbls7.fsf@HIDDEN>
MIME-version: 1.0
Content-type: text/plain; charset=utf-8
Content-Transfer-Encoding: 8bit
X-Spam-Score: -1.0 (-)
X-Debbugs-Envelope-To: 75116
Cc: yantar92@HIDDEN, pinmacs@HIDDEN, rpluim@HIDDEN,
 75116 <at> debbugs.gnu.org
X-BeenThere: debbugs-submit <at> debbugs.gnu.org
X-Mailman-Version: 2.1.18
Precedence: list
List-Id: <debbugs-submit.debbugs.gnu.org>
List-Unsubscribe: <https://debbugs.gnu.org/cgi-bin/mailman/options/debbugs-submit>, 
 <mailto:debbugs-submit-request <at> debbugs.gnu.org?subject=unsubscribe>
List-Archive: <https://debbugs.gnu.org/cgi-bin/mailman/private/debbugs-submit/>
List-Post: <mailto:debbugs-submit <at> debbugs.gnu.org>
List-Help: <mailto:debbugs-submit-request <at> debbugs.gnu.org?subject=help>
List-Subscribe: <https://debbugs.gnu.org/cgi-bin/mailman/listinfo/debbugs-submit>, 
 <mailto:debbugs-submit-request <at> debbugs.gnu.org?subject=subscribe>
Errors-To: debbugs-submit-bounces <at> debbugs.gnu.org
Sender: "Debbugs-submit" <debbugs-submit-bounces <at> debbugs.gnu.org>
X-Spam-Score: -2.0 (--)

> From: Visuwesh <visuweshm@HIDDEN>
> Cc: 75116 <at> debbugs.gnu.org,  yantar92@HIDDEN,  pinmacs@HIDDEN,
>   rpluim@HIDDEN
> Date: Mon, 06 Jan 2025 10:07:12 +0530
> 
> [சனி டிசம்பர் 28, 2024] Eli Zaretskii wrote:
> 
> >>     User asks to autoselect:
> >>       1. autoselect-function (a-s-f) returns one media type: we yank it.
> >
> > Yes.
> >
> >>       2. a-s-f returns multiple media types: we ask the user which one
> >>          to yank.
> >
> > No, we use the first one.
> 
> Doesn't this mean there is pratically no difference between (1) and (2)?

When the user asks to autoselect, yes.  I think this is reasonable:
auto-selecting from 1 or from N is the same.





Information forwarded to bug-gnu-emacs@HIDDEN:
bug#75116; Package emacs. Full text available.

Message received at 75116 <at> debbugs.gnu.org:


Received: (at 75116) by debbugs.gnu.org; 6 Jan 2025 04:39:10 +0000
From debbugs-submit-bounces <at> debbugs.gnu.org Sun Jan 05 23:39:10 2025
Received: from localhost ([127.0.0.1]:36347 helo=debbugs.gnu.org)
	by debbugs.gnu.org with esmtp (Exim 4.84_2)
	(envelope-from <debbugs-submit-bounces <at> debbugs.gnu.org>)
	id 1tUetQ-00039A-8T
	for submit <at> debbugs.gnu.org; Sun, 05 Jan 2025 23:39:10 -0500
Received: from mail-pj1-x1041.google.com ([2607:f8b0:4864:20::1041]:53366)
 by debbugs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128)
 (Exim 4.84_2) (envelope-from <visuweshm@HIDDEN>)
 id 1tUes0-0002qN-Vr
 for 75116 <at> debbugs.gnu.org; Sun, 05 Jan 2025 23:39:04 -0500
Received: by mail-pj1-x1041.google.com with SMTP id
 98e67ed59e1d1-2ef28f07dbaso15694666a91.2
 for <75116 <at> debbugs.gnu.org>; Sun, 05 Jan 2025 20:37:40 -0800 (PST)
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
 d=gmail.com; s=20230601; t=1736138258; x=1736743058; darn=debbugs.gnu.org;
 h=content-transfer-encoding:mime-version:user-agent:message-id:date
 :references:in-reply-to:subject:cc:to:from:from:to:cc:subject:date
 :message-id:reply-to;
 bh=tTHFTRC+TzVIOzrDkrMMGwvsD0Z/2iYHmKKTiGRG8VU=;
 b=P/JIz6234YtBVX5R4mpDE4yqoQKvh4Esfrwp9mt3ohu4pbBbgtsE9y/DB2GsGz8E16
 J5HdQHH/nQsmj+K4GElGStJ5tQSv5obR3ygwH8QVOiXObFxsDKj1qVzb5VcmsEMMzCHK
 UbGdZhGBgL9KQlbxCxO4Ig0Ssd3ls62u7M8Ug/Y1Lrhboga6YZj2VkTqhJkO3AqosaYm
 5Z//4RR6j3oWqKlpDUFZlHc2cOk7W0OeEcTVPekbv68RxyrMAZyo6PtGF52js98mZh6O
 iY4MeUG9iqGB1N3bVKIk+AqdbHgRo4m1m20RpmnbcmdbHTwb4hDHIhwygr3pdjnnpMpl
 tTPw==
X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
 d=1e100.net; s=20230601; t=1736138258; x=1736743058;
 h=content-transfer-encoding:mime-version:user-agent:message-id:date
 :references:in-reply-to:subject:cc:to:from:x-gm-message-state:from
 :to:cc:subject:date:message-id:reply-to;
 bh=tTHFTRC+TzVIOzrDkrMMGwvsD0Z/2iYHmKKTiGRG8VU=;
 b=Uvc1jP42rpvyjTekJYvFturAsSLC6N4XkR6UddRthKkJfgDw3FdTrgmWAIIM0rcfx1
 m9S0OGV2wdMr00s72K5X3w2zSBsMKcyu8BmiZ0+Cub5a0VAtzkGIUfdG0iNXzJJlU2sd
 3qNl2JNUmLA4g6CLMvtWcvyCZC07EQDpAQi7+PgIVHDDijA1/gG8ZMSLXwkdprGSPJ0q
 EWeHILxH/3EWQMgJ+Eea7pa3Ixz1rDQEBYQbYzI+7leyr0TuOZS4yQ/74OX+wOOPhe3C
 RHmdQhqijlbrneiWI2fEGUSREsyHDfupg0GcxuqDvrFfOoXooGGQv6/zqlbIeraA3GMU
 VPPQ==
X-Gm-Message-State: AOJu0Yw5b2s/yOaBq2WMOkw5+B4nY25tvVHOMo8eauDbAURxtgHaMUrw
 6Elq8ApeArxsZ9/nL9xfnPklqe14knsTDLRiVQ5e9j/9Cj/GBbzK
X-Gm-Gg: ASbGncuJZ9djfb7GZcCvOmgYqxzsOTxKbeA1lHtRQzdq7SCeXYTiWAe1yJw9EpnwK+0
 nuQfqY2gku3xqHHYslYCUUM6gj/kZXlwwcYf9tSi2+FR8k05HTTs/zLgAfBcvKONtIIog5Lyp3u
 xkgjYSvZPurR96OfAjTADgCCgqiFow7MDi0iHOgMITXnzCGxQcN3RR5cBUtqVJ/LhJP5ZoNsaaE
 g0ClPOu8qZN7NRuv+F2a5N1eMjFNRP4cg0aaPhexw+SDrZ5W0ITWQ==
X-Google-Smtp-Source: AGHT+IHOPrLJxNs1EayEDMk4imH7bu5GpAAgkZHgw1w33et3kXuzuRJ8bWpF8LKc/FWTMJ/S/0ynOg==
X-Received: by 2002:a17:90b:2d44:b0:2ee:df57:b194 with SMTP id
 98e67ed59e1d1-2f452e3c70amr76553358a91.21.1736138258421; 
 Sun, 05 Jan 2025 20:37:38 -0800 (PST)
Received: from localhost ([49.204.136.228]) by smtp.gmail.com with ESMTPSA id
 d9443c01a7336-219dc9f7d83sm284422595ad.226.2025.01.05.20.37.37
 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256);
 Sun, 05 Jan 2025 20:37:38 -0800 (PST)
From: Visuwesh <visuweshm@HIDDEN>
To: Eli Zaretskii <eliz@HIDDEN>
Subject: Re: bug#75116: [PATCH] Make 'yank-media' autoselect the best media
 type
In-Reply-To: <86r05sro63.fsf@HIDDEN> (Eli Zaretskii's message of "Sat, 28 Dec
 2024 14:24:04 +0200")
References: <87o70yeiih.fsf@HIDDEN> <86r05uxx4i.fsf@HIDDEN>
 <871pxtcxiq.fsf@HIDDEN> <86r05sro63.fsf@HIDDEN>
Date: Mon, 06 Jan 2025 10:07:12 +0530
Message-ID: <87bjwkbls7.fsf@HIDDEN>
User-Agent: Gnus/5.13 (Gnus v5.13)
MIME-Version: 1.0
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: quoted-printable
X-Spam-Score: 0.0 (/)
X-Debbugs-Envelope-To: 75116
Cc: yantar92@HIDDEN, pinmacs@HIDDEN, rpluim@HIDDEN,
 75116 <at> debbugs.gnu.org
X-BeenThere: debbugs-submit <at> debbugs.gnu.org
X-Mailman-Version: 2.1.18
Precedence: list
List-Id: <debbugs-submit.debbugs.gnu.org>
List-Unsubscribe: <https://debbugs.gnu.org/cgi-bin/mailman/options/debbugs-submit>, 
 <mailto:debbugs-submit-request <at> debbugs.gnu.org?subject=unsubscribe>
List-Archive: <https://debbugs.gnu.org/cgi-bin/mailman/private/debbugs-submit/>
List-Post: <mailto:debbugs-submit <at> debbugs.gnu.org>
List-Help: <mailto:debbugs-submit-request <at> debbugs.gnu.org?subject=help>
List-Subscribe: <https://debbugs.gnu.org/cgi-bin/mailman/listinfo/debbugs-submit>, 
 <mailto:debbugs-submit-request <at> debbugs.gnu.org?subject=subscribe>
Errors-To: debbugs-submit-bounces <at> debbugs.gnu.org
Sender: "Debbugs-submit" <debbugs-submit-bounces <at> debbugs.gnu.org>
X-Spam-Score: -1.0 (-)

[=E0=AE=9A=E0=AE=A9=E0=AE=BF =E0=AE=9F=E0=AE=BF=E0=AE=9A=E0=AE=AE=E0=AF=8D=
=E0=AE=AA=E0=AE=B0=E0=AF=8D 28, 2024] Eli Zaretskii wrote:

>> >> +(defvar yank-media-preferred-types
>> >> +  `(;; Check first since LibreOffice also puts a PNG image in the
>> >> +    ;; clipboard when a table cell is copied.
>> >> +    application/x-libreoffice-tsvc
>> >> +    ;; Give PNG more priority.
>> >> +    image/png
>> >> +    image/jpeg
>> >> +    ;; These are files copied/cut to the clipboard from a file manag=
er.
>> >> +    ,(lambda (mimetypes)
>> >> +       (seq-find (lambda (type)
>> >> +                (string-match-p "x-special/\\(gnome\\|KDE\\|mate\\)-=
files"
>> >> +                                (symbol-name type)))
>> >> +              mimetypes))
>> >> +    ;; FIXME: We should have a way to handle text/rtf.
>> >> +    text/html)
>> >
>> > Not sure I understand the value you suggest.  It seems to lack many
>> > important types.=20=20
>>=20
>> These are media types for which support for yank-media already exists:
>
> I thought we should have here as many media types as possible,
> regardless of their support, because this variable expresses the
> relative priorities of the types.  Major modes which add handlers
> should not need to also modify the value of this variable
> unnecessarily, no?

I agree with you but I am limited by my awareness of these potential
media types.  I would be happy to add more if someone points me to them.

>> > Also, aren't at least some of the types system-dependent?
>>=20
>> Yes, definitely.  x/special-gnome-files and
>> application/x-libreoffice-tsvc are system- and software-dependent resp.
>> This was one of my comments addressed in the message I posted to
>> emacs-devel:
>>=20
>>      The mimetype used for cut/copied files only works in Linux
>>      environments.  If other platforms can present such file:// links in
>>      the clipboard and Emacs supports it, we would need to add it to the
>>      list too.
>>=20
>> If we want platform-agnostic types, I assume we need an abstraction
>> layer on top that would present the clipboard data in a uniform manner.
>> I do not have the means to work on this since I only use Linux systems.
>
> I just thought about having the value depend on the platform.

Ah, understood.

>> >> +    (setq pref-type (and (null noselect)
>> >> +                         (funcall yank-media-autoselect-function
>> >> +                                  (mapcar #'car all-types))))
>> >> +    (cond
>> >> +     ;; We have one preferred mime type so use it unconditionally.
>> >> +     ((and pref-type (symbolp pref-type))
>> >> +      (funcall (cdr (assq pref-type all-types)) pref-type
>> >> +               (yank-media--get-selection pref-type)))
>> >> +     ;; The user chose to not autoselect and there's just a single t=
ype,
>> >> +     ;; just call the handler.
>> >> +     ((and (null pref-type) (length=3D all-types 1))
>> >> +      (funcall (cdar all-types) (caar all-types)
>> >> +               (yank-media--get-selection (caar all-types))))
>> >
>> > This goes against what the doc string says.  And I think the doc
>> > string describes a better behavior: if the user asked not to
>> > auto-select, we shouldn't, even if there's just one type available.
>> > We should instead ask the user whether to yank that type, because the
>> > user could decide she doesn't want that type, even it it's the only
>> > one.
>> >
>> > Also, I think we should show some message if
>> > yank-media-autoselect-function returns nil.  AFAIU, the code you
>> > posted silently does nothing, which IMO is not the best UI.
>>=20
>> I want to ensure we are on the same page wrt UI here:
>>=20
>>     User asks to autoselect:
>>       1. autoselect-function (a-s-f) returns one media type: we yank it.
>
> Yes.
>
>>       2. a-s-f returns multiple media types: we ask the user which one
>>          to yank.
>
> No, we use the first one.

Doesn't this mean there is pratically no difference between (1) and (2)?
Ihor, is this okay?  Or did you have something else in mind when you
asked for the possibility to return multiple types?

>>       3. a-s-f returns nil.  We show a message and do what?
>
> We show a message and do nothing.
>
>>     User asks not to autoselect:
>>       4. (length all-types) =3D 1: We show the media type and ask if she
>>          wants to yank it.
>
> Yes.
>
>>       5. (length all-types) > 1: We ask for the media type to yank.
>
> Yes.
>
>> Excepting case (3), does the other cases sound good?
>
> See above regarding my opinions.

Thank you, they are clear now.




Information forwarded to bug-gnu-emacs@HIDDEN:
bug#75116; Package emacs. Full text available.
Severity set to 'wishlist' from 'normal' Request was from Stefan Kangas <stefankangas@HIDDEN> to control <at> debbugs.gnu.org. Full text available.

Message received at 75116 <at> debbugs.gnu.org:


Received: (at 75116) by debbugs.gnu.org; 28 Dec 2024 12:24:15 +0000
From debbugs-submit-bounces <at> debbugs.gnu.org Sat Dec 28 07:24:15 2024
Received: from localhost ([127.0.0.1]:49656 helo=debbugs.gnu.org)
	by debbugs.gnu.org with esmtp (Exim 4.84_2)
	(envelope-from <debbugs-submit-bounces <at> debbugs.gnu.org>)
	id 1tRVra-00024P-Lf
	for submit <at> debbugs.gnu.org; Sat, 28 Dec 2024 07:24:15 -0500
Received: from eggs.gnu.org ([209.51.188.92]:53286)
 by debbugs.gnu.org with esmtp (Exim 4.84_2)
 (envelope-from <eliz@HIDDEN>) id 1tRVrY-000248-Fj
 for 75116 <at> debbugs.gnu.org; Sat, 28 Dec 2024 07:24:13 -0500
Received: from fencepost.gnu.org ([2001:470:142:3::e])
 by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256)
 (Exim 4.90_1) (envelope-from <eliz@HIDDEN>)
 id 1tRVrS-0005FS-UC; Sat, 28 Dec 2024 07:24:07 -0500
DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=gnu.org;
 s=fencepost-gnu-org; h=MIME-version:References:Subject:In-Reply-To:To:From:
 Date; bh=iuXIs9PWnUwOZSH89pwanEZ5cAeFRLaG0Rn204glKhg=; b=GDbgVB5nvyOlZZpqGfxr
 ze6cyADoIevYWvEV4baX8UkzKGTaevWsz/xJjaadIYZ3Q3yG1ZJ+p3wceVMvcIJv5slwfIk6tYuPw
 nahjmRmzp7Mxu9b3U6xR/cVz1Gc4SLVd4coaqBnPcsVLhggvU60Nkdm1/BJX28bbsV+l57Z7Uwtov
 seo69B6mUxDIafrcF0qVnBoBerEJpneQeqkmTcU+f1ddky2Nm9Sok3JE8u3wKhjPfeWjaizgvyV15
 ia8mHywNaRyT+tW3nx2S5tybWYdWBOGoVC//s2MxCi/WRxsD0fWwyEoOP7qqC5Ll3wrvMlr3/B8Ko
 iR+UV/qy0SKKAw==;
Date: Sat, 28 Dec 2024 14:24:04 +0200
Message-Id: <86r05sro63.fsf@HIDDEN>
From: Eli Zaretskii <eliz@HIDDEN>
To: Visuwesh <visuweshm@HIDDEN>
In-Reply-To: <871pxtcxiq.fsf@HIDDEN> (message from Visuwesh on Fri, 27 Dec
 2024 14:28:53 +0530)
Subject: Re: bug#75116: [PATCH] Make 'yank-media' autoselect the best media
 type
References: <87o70yeiih.fsf@HIDDEN> <86r05uxx4i.fsf@HIDDEN>
 <871pxtcxiq.fsf@HIDDEN>
MIME-version: 1.0
Content-type: text/plain; charset=utf-8
Content-Transfer-Encoding: 8bit
X-Spam-Score: -2.3 (--)
X-Debbugs-Envelope-To: 75116
Cc: yantar92@HIDDEN, pinmacs@HIDDEN, rpluim@HIDDEN,
 75116 <at> debbugs.gnu.org
X-BeenThere: debbugs-submit <at> debbugs.gnu.org
X-Mailman-Version: 2.1.18
Precedence: list
List-Id: <debbugs-submit.debbugs.gnu.org>
List-Unsubscribe: <https://debbugs.gnu.org/cgi-bin/mailman/options/debbugs-submit>, 
 <mailto:debbugs-submit-request <at> debbugs.gnu.org?subject=unsubscribe>
List-Archive: <https://debbugs.gnu.org/cgi-bin/mailman/private/debbugs-submit/>
List-Post: <mailto:debbugs-submit <at> debbugs.gnu.org>
List-Help: <mailto:debbugs-submit-request <at> debbugs.gnu.org?subject=help>
List-Subscribe: <https://debbugs.gnu.org/cgi-bin/mailman/listinfo/debbugs-submit>, 
 <mailto:debbugs-submit-request <at> debbugs.gnu.org?subject=subscribe>
Errors-To: debbugs-submit-bounces <at> debbugs.gnu.org
Sender: "Debbugs-submit" <debbugs-submit-bounces <at> debbugs.gnu.org>
X-Spam-Score: -3.3 (---)

> From: Visuwesh <visuweshm@HIDDEN>
> Cc: 75116 <at> debbugs.gnu.org,  yantar92@HIDDEN,  pinmacs@HIDDEN,
>   rpluim@HIDDEN
> Date: Fri, 27 Dec 2024 14:28:53 +0530
> 
> [வியாழன் டிசம்பர் 26, 2024] Eli Zaretskii wrote:
> >> I think the variable
> >> yank-media-preferred-types gives a more granular control for major-mode
> >> authors than (add-function (local 'yank-media-autoselect-function) ...)
> >
> > Maybe.  But one of my comments is exactly about that: I don't quite
> > understand your intent for how major modes should use this variable
> > (since neither the doc string nor the comments spell that out).  Would
> > you please explain your thoughts on that?
> 
> I was thinking using a variable like yank-media-preferred-types would be
> easier to ensure that image/svg is tried _before_ image/png but not
> _before_ application/x-libreoffice-tsvc, etc.  Maybe this is
> overengineering things.  I do not hold a strong opinion here so if you
> think `yank-media-autoselect-function' is enough control for major-mode
> authors, that is enough.

If we intend for major modes to bind this variable, we should say so
in the doc string, IMO.

> >> +(defvar yank-media-preferred-types
> >> +  `(;; Check first since LibreOffice also puts a PNG image in the
> >> +    ;; clipboard when a table cell is copied.
> >> +    application/x-libreoffice-tsvc
> >> +    ;; Give PNG more priority.
> >> +    image/png
> >> +    image/jpeg
> >> +    ;; These are files copied/cut to the clipboard from a file manager.
> >> +    ,(lambda (mimetypes)
> >> +       (seq-find (lambda (type)
> >> +                (string-match-p "x-special/\\(gnome\\|KDE\\|mate\\)-files"
> >> +                                (symbol-name type)))
> >> +              mimetypes))
> >> +    ;; FIXME: We should have a way to handle text/rtf.
> >> +    text/html)
> >
> > Not sure I understand the value you suggest.  It seems to lack many
> > important types.  
> 
> These are media types for which support for yank-media already exists:

I thought we should have here as many media types as possible,
regardless of their support, because this variable expresses the
relative priorities of the types.  Major modes which add handlers
should not need to also modify the value of this variable
unnecessarily, no?

> > Also, aren't at least some of the types system-dependent?
> 
> Yes, definitely.  x/special-gnome-files and
> application/x-libreoffice-tsvc are system- and software-dependent resp.
> This was one of my comments addressed in the message I posted to
> emacs-devel:
> 
>      The mimetype used for cut/copied files only works in Linux
>      environments.  If other platforms can present such file:// links in
>      the clipboard and Emacs supports it, we would need to add it to the
>      list too.
> 
> If we want platform-agnostic types, I assume we need an abstraction
> layer on top that would present the clipboard data in a uniform manner.
> I do not have the means to work on this since I only use Linux systems.

I just thought about having the value depend on the platform.

> >> +    (setq pref-type (and (null noselect)
> >> +                         (funcall yank-media-autoselect-function
> >> +                                  (mapcar #'car all-types))))
> >> +    (cond
> >> +     ;; We have one preferred mime type so use it unconditionally.
> >> +     ((and pref-type (symbolp pref-type))
> >> +      (funcall (cdr (assq pref-type all-types)) pref-type
> >> +               (yank-media--get-selection pref-type)))
> >> +     ;; The user chose to not autoselect and there's just a single type,
> >> +     ;; just call the handler.
> >> +     ((and (null pref-type) (length= all-types 1))
> >> +      (funcall (cdar all-types) (caar all-types)
> >> +               (yank-media--get-selection (caar all-types))))
> >
> > This goes against what the doc string says.  And I think the doc
> > string describes a better behavior: if the user asked not to
> > auto-select, we shouldn't, even if there's just one type available.
> > We should instead ask the user whether to yank that type, because the
> > user could decide she doesn't want that type, even it it's the only
> > one.
> >
> > Also, I think we should show some message if
> > yank-media-autoselect-function returns nil.  AFAIU, the code you
> > posted silently does nothing, which IMO is not the best UI.
> 
> I want to ensure we are on the same page wrt UI here:
> 
>     User asks to autoselect:
>       1. autoselect-function (a-s-f) returns one media type: we yank it.

Yes.

>       2. a-s-f returns multiple media types: we ask the user which one
>          to yank.

No, we use the first one.

>       3. a-s-f returns nil.  We show a message and do what?

We show a message and do nothing.

>     User asks not to autoselect:
>       4. (length all-types) = 1: We show the media type and ask if she
>          wants to yank it.

Yes.

>       5. (length all-types) > 1: We ask for the media type to yank.

Yes.

> Excepting case (3), does the other cases sound good?

See above regarding my opinions.




Information forwarded to bug-gnu-emacs@HIDDEN:
bug#75116; Package emacs. Full text available.

Message received at 75116 <at> debbugs.gnu.org:


Received: (at 75116) by debbugs.gnu.org; 27 Dec 2024 09:00:00 +0000
From debbugs-submit-bounces <at> debbugs.gnu.org Fri Dec 27 04:00:00 2024
Received: from localhost ([127.0.0.1]:44658 helo=debbugs.gnu.org)
	by debbugs.gnu.org with esmtp (Exim 4.84_2)
	(envelope-from <debbugs-submit-bounces <at> debbugs.gnu.org>)
	id 1tR6CO-0004We-1X
	for submit <at> debbugs.gnu.org; Fri, 27 Dec 2024 04:00:00 -0500
Received: from mail-pl1-f193.google.com ([209.85.214.193]:48189)
 by debbugs.gnu.org with esmtp (Exim 4.84_2)
 (envelope-from <visuweshm@HIDDEN>) id 1tR6CL-0004WU-FX
 for 75116 <at> debbugs.gnu.org; Fri, 27 Dec 2024 03:59:58 -0500
Received: by mail-pl1-f193.google.com with SMTP id
 d9443c01a7336-216281bc30fso98904065ad.0
 for <75116 <at> debbugs.gnu.org>; Fri, 27 Dec 2024 00:59:57 -0800 (PST)
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
 d=gmail.com; s=20230601; t=1735289936; x=1735894736; darn=debbugs.gnu.org;
 h=content-transfer-encoding:mime-version:user-agent:message-id:date
 :references:in-reply-to:subject:cc:to:from:from:to:cc:subject:date
 :message-id:reply-to;
 bh=qLiJ0YNcoKETMt9BFb8drLgXofm/QQUHeqi/c00AiRY=;
 b=OoW2jQ9EJxEXAzTJg+HBnQmz4LepoxWNBzqat77gOKFGWqc7cYfO2aVY0wA5RcXgiv
 eWcv8IhbbdXUcMB8QUkmQ2XnHxgq1KL2558rZYcB80QhnetOGMXYGvNse2dIzK6uzuJk
 kB0mPIQ7yiG6pMaKu42/AivHHacgw9OLdLzCu4sTmhysXL2ohx5TGLfSRHv8kIFcYPX/
 j+lr6CxzaqpUCbLfd/tlogw/VtRHvFQAB2SOzwRLFM0jxyWRzrbHy3NBHwm3yhPPfVzv
 goG2UofwGm5ybYjk1XXD3jA+aE59aV7Gbb6IbaxOfnOggJZ+UDMEiBzwMaSpYelmw77e
 NdXg==
X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
 d=1e100.net; s=20230601; t=1735289936; x=1735894736;
 h=content-transfer-encoding:mime-version:user-agent:message-id:date
 :references:in-reply-to:subject:cc:to:from:x-gm-message-state:from
 :to:cc:subject:date:message-id:reply-to;
 bh=qLiJ0YNcoKETMt9BFb8drLgXofm/QQUHeqi/c00AiRY=;
 b=NIhxdsKTevYL+1DYYeCBEJ5YfovsZ5EMlLYP3wbUKIv03LliRZQT36KYqFzdXM6EHC
 cUsOvtejReWCpfM8sl/jKgN7r57nuqmgV1gcXDgX9HhYrif9mUSBJX8N706INcw3PXZ7
 ZU5YHlrZezt2lYxwXvYms0m/yXDOKCCK2xN9nM99px1BU/lq9O9aGAaO9sQlFR4HSj6F
 388eyKDF0mEuAeEhAOCNHV6hH3OHxSpDfhvR+ujo0DYWzSvrPjcR3HYmZUPDK17Pnc04
 koEigqziwT0YBXUSLJuSazeF5IiZgX087TI92wq2UB44nD1uuILg9arjexRa4Ki2hplG
 qdlg==
X-Gm-Message-State: AOJu0YzXirL4BkSqA2Wr5/60ylsn6Wy1Fd8lClVdOs6ZUnn6HRszVQH4
 aNk+oR3G87EGfY7cqjv4gU+2+7YVtt68jc6U0b7Nej9gn2cIRP16
X-Gm-Gg: ASbGncvenmDdCq82anv6kbp+TneI6GM1O+4MNVDOc+kVZ3ghxrE2bPqLdc6gsSpMZUI
 URfOWXuZ4ZgTb3S3nTWJR4BBuGEvlzRncMLywuWbJrA+ghfN3VklYkhHtzigLo9v9KN5xYOKTbz
 6TaW+KVp9UEcpVQk9jKSBmdqJClW2GDfxzm8ZW0YbrjbkaNT7IQji8X92yTelLxcrPsE+vaBMIx
 UGCoQu/Tu/LyhCv0zOcjM/IqW6pS/G/K2AGPbW+I0n43J5I5ylFSQ==
X-Google-Smtp-Source: AGHT+IFINvOXrrOWDuxK5goa8ldB7TGff0V4BvHZqodIS8ucCmyNuU/0cCi14daWdWput2ts5qxEQw==
X-Received: by 2002:a17:902:dace:b0:216:3732:ade3 with SMTP id
 d9443c01a7336-219e6f25fd1mr341116295ad.35.1735289936570; 
 Fri, 27 Dec 2024 00:58:56 -0800 (PST)
Received: from localhost ([49.204.136.169]) by smtp.gmail.com with ESMTPSA id
 d9443c01a7336-219dc962d5fsm131410355ad.38.2024.12.27.00.58.55
 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256);
 Fri, 27 Dec 2024 00:58:56 -0800 (PST)
From: Visuwesh <visuweshm@HIDDEN>
To: Eli Zaretskii <eliz@HIDDEN>
Subject: Re: bug#75116: [PATCH] Make 'yank-media' autoselect the best media
 type
In-Reply-To: <86r05uxx4i.fsf@HIDDEN> (Eli Zaretskii's message of "Thu, 26 Dec
 2024 17:49:33 +0200")
References: <87o70yeiih.fsf@HIDDEN> <86r05uxx4i.fsf@HIDDEN>
Date: Fri, 27 Dec 2024 14:28:53 +0530
Message-ID: <871pxtcxiq.fsf@HIDDEN>
User-Agent: Gnus/5.13 (Gnus v5.13)
MIME-Version: 1.0
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: quoted-printable
X-Spam-Score: 1.0 (+)
X-Debbugs-Envelope-To: 75116
Cc: yantar92@HIDDEN, pinmacs@HIDDEN, rpluim@HIDDEN,
 75116 <at> debbugs.gnu.org
X-BeenThere: debbugs-submit <at> debbugs.gnu.org
X-Mailman-Version: 2.1.18
Precedence: list
List-Id: <debbugs-submit.debbugs.gnu.org>
List-Unsubscribe: <https://debbugs.gnu.org/cgi-bin/mailman/options/debbugs-submit>, 
 <mailto:debbugs-submit-request <at> debbugs.gnu.org?subject=unsubscribe>
List-Archive: <https://debbugs.gnu.org/cgi-bin/mailman/private/debbugs-submit/>
List-Post: <mailto:debbugs-submit <at> debbugs.gnu.org>
List-Help: <mailto:debbugs-submit-request <at> debbugs.gnu.org?subject=help>
List-Subscribe: <https://debbugs.gnu.org/cgi-bin/mailman/listinfo/debbugs-submit>, 
 <mailto:debbugs-submit-request <at> debbugs.gnu.org?subject=subscribe>
Errors-To: debbugs-submit-bounces <at> debbugs.gnu.org
Sender: "Debbugs-submit" <debbugs-submit-bounces <at> debbugs.gnu.org>
X-Spam-Score: -1.0 (-)

[=E0=AE=B5=E0=AE=BF=E0=AE=AF=E0=AE=BE=E0=AE=B4=E0=AE=A9=E0=AF=8D =E0=AE=9F=
=E0=AE=BF=E0=AE=9A=E0=AE=AE=E0=AF=8D=E0=AE=AA=E0=AE=B0=E0=AF=8D 26, 2024] E=
li Zaretskii wrote:

>> Cc: Ihor Radchenko <yantar92@HIDDEN>, pinmacs@HIDDEN, rpluim@gmail.=
com, Eli Zaretskii <eliz@HIDDEN>
>> From: Visuwesh <visuweshm@HIDDEN>
>> Date: Thu, 26 Dec 2024 17:57:50 +0530
>>=20
>> This is a continuation of the long thread in emacs-devel:
>> https://yhetil.org/emacs-devel/79fc91f3-c2c3-44db-9817-595808917f26@cas.=
cat/
>>=20
>> This message provides a summary:
>> https://yhetil.org/87r06cj2nd.fsf@HIDDEN
>>=20
>> Ihor wrote:
>>=20
>> > The only comment is that leaving an option to return a list of types
>> > rather than only a single type will make things more flexible.
>>=20
>> And this is now done in the attached patch.
>
> Thanks.
>
>> Before I go about writing NEWS and updating the manual, what do you
>> think about the attached instead?
>
> Some comments below.
>
>> I think the variable
>> yank-media-preferred-types gives a more granular control for major-mode
>> authors than (add-function (local 'yank-media-autoselect-function) ...)
>
> Maybe.  But one of my comments is exactly about that: I don't quite
> understand your intent for how major modes should use this variable
> (since neither the doc string nor the comments spell that out).  Would
> you please explain your thoughts on that?

I was thinking using a variable like yank-media-preferred-types would be
easier to ensure that image/svg is tried _before_ image/png but not
_before_ application/x-libreoffice-tsvc, etc.  Maybe this is
overengineering things.  I do not hold a strong opinion here so if you
think `yank-media-autoselect-function' is enough control for major-mode
authors, that is enough.

As for (add-function (local 'yank-media-autoselect-function) ...)
scenario, taking Robert's example of preferring image/svg in some HTML
documents, one could say

    (add-function :around (local 'yank-media-autoselect-function)
      (lambda (oldfun types)
        (if (memq 'image/svg types)=20
            'image/svg
          (funcall oldfun types))))

>> +(defvar yank-media-preferred-types
>> +  `(;; Check first since LibreOffice also puts a PNG image in the
>> +    ;; clipboard when a table cell is copied.
>> +    application/x-libreoffice-tsvc
>> +    ;; Give PNG more priority.
>> +    image/png
>> +    image/jpeg
>> +    ;; These are files copied/cut to the clipboard from a file manager.
>> +    ,(lambda (mimetypes)
>> +       (seq-find (lambda (type)
>> +                (string-match-p "x-special/\\(gnome\\|KDE\\|mate\\)-fil=
es"
>> +                                (symbol-name type)))
>> +              mimetypes))
>> +    ;; FIXME: We should have a way to handle text/rtf.
>> +    text/html)
>
> Not sure I understand the value you suggest.  It seems to lack many
> important types.=20=20

These are media types for which support for yank-media already exists:

    ./lisp/gnus/message.el:3180:  (yank-media-handler "image/.*" #'message-=
-yank-media-image-handler)
    ./lisp/org/org.el:20757:    (yank-media-handler "image/.*" #'org--image=
-yank-media-handler)
    ./lisp/org/org.el:20760:    (yank-media-handler "x/special-\\(?:gnome\\=
|KDE\\|mate\\)-files"
    ./lisp/textmodes/sgml-mode.el:2419:  (yank-media-handler 'text/html #'h=
tml-mode--html-yank-handler)
    ./lisp/textmodes/sgml-mode.el:2420:  (yank-media-handler "image/.*" #'h=
tml-mode--image-yank-handler)

and org-mode's main branch recently gained support for
application/x-libreoffice-tsvc.

Personally, these are the only media types which I use/come across
daily.  Someone=E2=84=A2 needs to comment on other media types that are
potentially useful.
[ I have a patch for message.el to add support for x/special-gnome-files
  which I need to bring in sync with master and send soon=E2=84=A2.  ]

> Also, aren't at least some of the types system-dependent?

Yes, definitely.  x/special-gnome-files and
application/x-libreoffice-tsvc are system- and software-dependent resp.
This was one of my comments addressed in the message I posted to
emacs-devel:

     The mimetype used for cut/copied files only works in Linux
     environments.  If other platforms can present such file:// links in
     the clipboard and Emacs supports it, we would need to add it to the
     list too.

If we want platform-agnostic types, I assume we need an abstraction
layer on top that would present the clipboard data in a uniform manner.
I do not have the means to work on this since I only use Linux systems.

>> +  "List of mime types in the order of preference.
>> +Each element in the list should be a symbol to choose the mime type
>> +denoted by it, or a function of one argument, the mime types available,
>> +and should return the mime types to use.")
>
> If this is intended for major modes to override, we should say so in
> the doc string.
>
>> +(defvar yank-media-autoselect-function #'yank-media-autoselect-function
>> +  "Function to auto select the best mime types when many are available.
>                                                        ^^^^
> I suggest "more than one" there.  "Many" could be misinterpreted to
> exclude the case of just two possible types.

Thanks, I was stuck on the phrasing here.

>> +    (setq pref-type (and (null noselect)
>> +                         (funcall yank-media-autoselect-function
>> +                                  (mapcar #'car all-types))))
>> +    (cond
>> +     ;; We have one preferred mime type so use it unconditionally.
>> +     ((and pref-type (symbolp pref-type))
>> +      (funcall (cdr (assq pref-type all-types)) pref-type
>> +               (yank-media--get-selection pref-type)))
>> +     ;; The user chose to not autoselect and there's just a single type,
>> +     ;; just call the handler.
>> +     ((and (null pref-type) (length=3D all-types 1))
>> +      (funcall (cdar all-types) (caar all-types)
>> +               (yank-media--get-selection (caar all-types))))
>
> This goes against what the doc string says.  And I think the doc
> string describes a better behavior: if the user asked not to
> auto-select, we shouldn't, even if there's just one type available.
> We should instead ask the user whether to yank that type, because the
> user could decide she doesn't want that type, even it it's the only
> one.
>
> Also, I think we should show some message if
> yank-media-autoselect-function returns nil.  AFAIU, the code you
> posted silently does nothing, which IMO is not the best UI.

I want to ensure we are on the same page wrt UI here:

    User asks to autoselect:
      1. autoselect-function (a-s-f) returns one media type: we yank it.
      2. a-s-f returns multiple media types: we ask the user which one
         to yank.
      3. a-s-f returns nil.  We show a message and do what?

         If (length all-types) =3D 1, should we insert it no questions aske=
d?
         If (length all-types) > 1, we ask as usual.
=20=20=20=20=20=20=20=20=20
         Or, should we proceed as if the user did not ask for
         autoselection?

    User asks not to autoselect:
      4. (length all-types) =3D 1: We show the media type and ask if she
         wants to yank it.
      5. (length all-types) > 1: We ask for the media type to yank.

Excepting case (3), does the other cases sound good?

>> I know that I have to update the Info node (info "(elisp) Yanking
>> Media").  Does (info "(emacs) Clipboard") need any update too?
>
> IMO, yes.  In fact, I think the user-facing part of the description of
> yank-media should be moved to the Emacs user manual, the "Clipboard"
> node.

OK, yank-media is already documented in the Emacs user manual.  We would
need to talk about C-u and provide a description of auto-selection.




Information forwarded to bug-gnu-emacs@HIDDEN:
bug#75116; Package emacs. Full text available.

Message received at 75116 <at> debbugs.gnu.org:


Received: (at 75116) by debbugs.gnu.org; 26 Dec 2024 15:49:46 +0000
From debbugs-submit-bounces <at> debbugs.gnu.org Thu Dec 26 10:49:46 2024
Received: from localhost ([127.0.0.1]:41927 helo=debbugs.gnu.org)
	by debbugs.gnu.org with esmtp (Exim 4.84_2)
	(envelope-from <debbugs-submit-bounces <at> debbugs.gnu.org>)
	id 1tQq7O-0004hz-0Z
	for submit <at> debbugs.gnu.org; Thu, 26 Dec 2024 10:49:46 -0500
Received: from eggs.gnu.org ([209.51.188.92]:40630)
 by debbugs.gnu.org with esmtp (Exim 4.84_2)
 (envelope-from <eliz@HIDDEN>) id 1tQq7K-0004hl-Bo
 for 75116 <at> debbugs.gnu.org; Thu, 26 Dec 2024 10:49:44 -0500
Received: from fencepost.gnu.org ([2001:470:142:3::e])
 by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256)
 (Exim 4.90_1) (envelope-from <eliz@HIDDEN>)
 id 1tQq7D-0001eP-6v; Thu, 26 Dec 2024 10:49:35 -0500
DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=gnu.org;
 s=fencepost-gnu-org; h=References:Subject:In-Reply-To:To:From:Date:
 mime-version; bh=aicOYiONo00StYYTvonTjNyrkYhTCL3SH4x2D+NFFrM=; b=F2i1NgDrzWoi
 jMrCrpPtsJnBYrwLWafvy7lfxjprGE/3vmIeRvDBtcHzNy0GJ0rinxvHnjcbYdNmIncSJhPGGrxjV
 z2o4ROOBF/w0BTTLOUPCEfz80NaMLyqWyD+TodnfLRTHxZNa2VjqVNuVzdLAH5l69FB19ynkfMJE3
 7zHqATVuobxncahG0Yel7+lzD2VZc/Cr+vvx9C+OfILaG6UV2nppYYHjmZA81bUPm/4YYSo+bYz4H
 AIb54G2pC4wWIL2UrK/LlsbQlmGGcSDAGJAfxPLRF+Zs6+Jw58q/UjbAZNXiSBOqkIwjnstZ/Mmzq
 y8LxZoRXT4cfhG4TNciATw==;
Date: Thu, 26 Dec 2024 17:49:33 +0200
Message-Id: <86r05uxx4i.fsf@HIDDEN>
From: Eli Zaretskii <eliz@HIDDEN>
To: Visuwesh <visuweshm@HIDDEN>
In-Reply-To: <87o70yeiih.fsf@HIDDEN> (message from Visuwesh on Thu, 26 Dec
 2024 17:57:50 +0530)
Subject: Re: bug#75116: [PATCH] Make 'yank-media' autoselect the best media
 type
References: <87o70yeiih.fsf@HIDDEN>
X-Spam-Score: 0.0 (/)
X-Debbugs-Envelope-To: 75116
Cc: yantar92@HIDDEN, pinmacs@HIDDEN, rpluim@HIDDEN,
 75116 <at> debbugs.gnu.org
X-BeenThere: debbugs-submit <at> debbugs.gnu.org
X-Mailman-Version: 2.1.18
Precedence: list
List-Id: <debbugs-submit.debbugs.gnu.org>
List-Unsubscribe: <https://debbugs.gnu.org/cgi-bin/mailman/options/debbugs-submit>, 
 <mailto:debbugs-submit-request <at> debbugs.gnu.org?subject=unsubscribe>
List-Archive: <https://debbugs.gnu.org/cgi-bin/mailman/private/debbugs-submit/>
List-Post: <mailto:debbugs-submit <at> debbugs.gnu.org>
List-Help: <mailto:debbugs-submit-request <at> debbugs.gnu.org?subject=help>
List-Subscribe: <https://debbugs.gnu.org/cgi-bin/mailman/listinfo/debbugs-submit>, 
 <mailto:debbugs-submit-request <at> debbugs.gnu.org?subject=subscribe>
Errors-To: debbugs-submit-bounces <at> debbugs.gnu.org
Sender: "Debbugs-submit" <debbugs-submit-bounces <at> debbugs.gnu.org>
X-Spam-Score: -3.3 (---)

> Cc: Ihor Radchenko <yantar92@HIDDEN>, pinmacs@HIDDEN, rpluim@HIDDEN, Eli Zaretskii <eliz@HIDDEN>
> From: Visuwesh <visuweshm@HIDDEN>
> Date: Thu, 26 Dec 2024 17:57:50 +0530
> 
> This is a continuation of the long thread in emacs-devel:
> https://yhetil.org/emacs-devel/79fc91f3-c2c3-44db-9817-595808917f26@HIDDEN/
> 
> This message provides a summary:
> https://yhetil.org/87r06cj2nd.fsf@HIDDEN
> 
> Ihor wrote:
> 
> > The only comment is that leaving an option to return a list of types
> > rather than only a single type will make things more flexible.
> 
> And this is now done in the attached patch.

Thanks.

> Before I go about writing NEWS and updating the manual, what do you
> think about the attached instead?

Some comments below.

> I think the variable
> yank-media-preferred-types gives a more granular control for major-mode
> authors than (add-function (local 'yank-media-autoselect-function) ...)

Maybe.  But one of my comments is exactly about that: I don't quite
understand your intent for how major modes should use this variable
(since neither the doc string nor the comments spell that out).  Would
you please explain your thoughts on that?

> +(defvar yank-media-preferred-types
> +  `(;; Check first since LibreOffice also puts a PNG image in the
> +    ;; clipboard when a table cell is copied.
> +    application/x-libreoffice-tsvc
> +    ;; Give PNG more priority.
> +    image/png
> +    image/jpeg
> +    ;; These are files copied/cut to the clipboard from a file manager.
> +    ,(lambda (mimetypes)
> +       (seq-find (lambda (type)
> +                (string-match-p "x-special/\\(gnome\\|KDE\\|mate\\)-files"
> +                                (symbol-name type)))
> +              mimetypes))
> +    ;; FIXME: We should have a way to handle text/rtf.
> +    text/html)

Not sure I understand the value you suggest.  It seems to lack many
important types.  Also, aren't at least some of the types
system-dependent?

> +  "List of mime types in the order of preference.
> +Each element in the list should be a symbol to choose the mime type
> +denoted by it, or a function of one argument, the mime types available,
> +and should return the mime types to use.")

If this is intended for major modes to override, we should say so in
the doc string.

> +(defvar yank-media-autoselect-function #'yank-media-autoselect-function
> +  "Function to auto select the best mime types when many are available.
                                                       ^^^^
I suggest "more than one" there.  "Many" could be misinterpreted to
exclude the case of just two possible types.

> +    (setq pref-type (and (null noselect)
> +                         (funcall yank-media-autoselect-function
> +                                  (mapcar #'car all-types))))
> +    (cond
> +     ;; We have one preferred mime type so use it unconditionally.
> +     ((and pref-type (symbolp pref-type))
> +      (funcall (cdr (assq pref-type all-types)) pref-type
> +               (yank-media--get-selection pref-type)))
> +     ;; The user chose to not autoselect and there's just a single type,
> +     ;; just call the handler.
> +     ((and (null pref-type) (length= all-types 1))
> +      (funcall (cdar all-types) (caar all-types)
> +               (yank-media--get-selection (caar all-types))))

This goes against what the doc string says.  And I think the doc
string describes a better behavior: if the user asked not to
auto-select, we shouldn't, even if there's just one type available.
We should instead ask the user whether to yank that type, because the
user could decide she doesn't want that type, even it it's the only
one.

Also, I think we should show some message if
yank-media-autoselect-function returns nil.  AFAIU, the code you
posted silently does nothing, which IMO is not the best UI.

> I know that I have to update the Info node (info "(elisp) Yanking
> Media").  Does (info "(emacs) Clipboard") need any update too?

IMO, yes.  In fact, I think the user-facing part of the description of
yank-media should be moved to the Emacs user manual, the "Clipboard"
node.




Information forwarded to bug-gnu-emacs@HIDDEN:
bug#75116; Package emacs. Full text available.

Message received at submit <at> debbugs.gnu.org:


Received: (at submit) by debbugs.gnu.org; 26 Dec 2024 12:28:04 +0000
From debbugs-submit-bounces <at> debbugs.gnu.org Thu Dec 26 07:28:04 2024
Received: from localhost ([127.0.0.1]:40391 helo=debbugs.gnu.org)
	by debbugs.gnu.org with esmtp (Exim 4.84_2)
	(envelope-from <debbugs-submit-bounces <at> debbugs.gnu.org>)
	id 1tQmyB-0003Tb-OF
	for submit <at> debbugs.gnu.org; Thu, 26 Dec 2024 07:28:04 -0500
Received: from lists.gnu.org ([209.51.188.17]:37602)
 by debbugs.gnu.org with esmtp (Exim 4.84_2)
 (envelope-from <visuweshm@HIDDEN>) id 1tQmy7-0003T8-Qf
 for submit <at> debbugs.gnu.org; Thu, 26 Dec 2024 07:28:02 -0500
Received: from eggs.gnu.org ([2001:470:142:3::10])
 by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256)
 (Exim 4.90_1) (envelope-from <visuweshm@HIDDEN>)
 id 1tQmy6-0000xb-Fa
 for bug-gnu-emacs@HIDDEN; Thu, 26 Dec 2024 07:27:58 -0500
Received: from mail-pl1-x644.google.com ([2607:f8b0:4864:20::644])
 by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128)
 (Exim 4.90_1) (envelope-from <visuweshm@HIDDEN>)
 id 1tQmy3-0006JN-Km
 for bug-gnu-emacs@HIDDEN; Thu, 26 Dec 2024 07:27:58 -0500
Received: by mail-pl1-x644.google.com with SMTP id
 d9443c01a7336-21636268e43so2386705ad.2
 for <bug-gnu-emacs@HIDDEN>; Thu, 26 Dec 2024 04:27:55 -0800 (PST)
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
 d=gmail.com; s=20230601; t=1735216074; x=1735820874; darn=gnu.org;
 h=mime-version:user-agent:message-id:date:subject:to:from:from:to:cc
 :subject:date:message-id:reply-to;
 bh=JGaRogkqW8qru0U7Ye+xb6fQP8EYxcvTp6WHzq7Hfvw=;
 b=GT2UUGPI/lT6InCtyYBTNpZ++SxRL6YRG+fIZdugm2/b3GC4dNYzi/KLjz9fPm24fQ
 oBpRJpKqF/uTatjWSGRcQmgfAscTbd3t/7FD1xpEi24xgUpLyYL/WqL9vW9PFNCPlI3M
 qTH6ycB3iZg9tuiqMtFZWi3ca1hAyFAyDJGQtsmiNXwBC9ppITaAv+DZGk7vl3payBzF
 DUwyd11x10RUafjLZJDz3GIpO7oEdCgtRT5G+GtHVQLA4ucjJtqTro647yf9gsxyKDAI
 /EwXILlIKqpbi1oOsivhex3YR/cLuBiBeIo682v+bvNr9p7mfJDS1JRpbD/f4yNKvYSK
 3xFA==
X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
 d=1e100.net; s=20230601; t=1735216074; x=1735820874;
 h=mime-version:user-agent:message-id:date:subject:to:from
 :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to;
 bh=JGaRogkqW8qru0U7Ye+xb6fQP8EYxcvTp6WHzq7Hfvw=;
 b=oWHDUkwf/ez3+KqbFeuKnRf9VwgEnHA0E1TSAjQkAE2bhBzciTlQwNLV90I+vLVk7Z
 uN0xQBiIAmttOzLfhbnIeQVZYc1RvanrXxzw2cvR5lCmCTdlZdxrzWRnXG9FsZFtP82d
 scBnT+suYfYxuJr8m3wmBiUI1L9bcsxvo7wXXC8x3HMyupKp6afSn/5hH/tI4QrMhYOO
 psSocI/kL0gX0uwe8a0SlSo7DJ8SjKSiQTj+EggIuW4iyJ1Q2sihdLJXQsEa4T1Rxf6y
 KrccEu+EWkx1SuxiS/qB4fJAR2l/SjE6QPBcBit7FUR9A7eAbZF7cz05mEoLbw9cPWa1
 v1aw==
X-Gm-Message-State: AOJu0Yw78HGr9aVk9jQSmavJUt66NIgUGuLaySzQ6NRhh/AsURYq+Kj5
 SaCSF8k6NWK7RXYpxhft+U/5BiNSv12LV8VWLmHUdwYrVWQbZ0Z4DgpXv0C9
X-Gm-Gg: ASbGncuOjC6OsiTT/hggH6QVK8/HyS8wxcARCI6zRU0pVjJiTKYYxkhTf3LyS6EvwAh
 vrf1IHic9Ohmt0lzrKdIK3RNIy7GhYsFGVNi/b3Z9n50kehBR3D12gkECaEYHYOazp6KRGYlv5q
 STOWQ6LkwZ9kOj7j96eV5D+RuCDMVgQxk6ITmPfVxhJCn3v00UuE3ddcwWP5BPVgQn2lUWXejLg
 WTJFONydlxc7XZISQan9m3aermP/j7RKPxLq2/BCm1hKxdAaAo=
X-Google-Smtp-Source: AGHT+IFOL7RmEm8HeLN6lhBQWbLVzxOwQJhTJrCpKHwSmSozcN6AkBzFQ0LqvopHWIlslV2uaZXWyw==
X-Received: by 2002:a05:6a20:12d2:b0:1e1:3970:d75a with SMTP id
 adf61e73a8af0-1e5e0458eadmr40019268637.9.1735216073762; 
 Thu, 26 Dec 2024 04:27:53 -0800 (PST)
Received: from localhost ([49.205.85.12]) by smtp.gmail.com with ESMTPSA id
 d2e1a72fcca58-72aad81575fsm12737330b3a.36.2024.12.26.04.27.52
 for <bug-gnu-emacs@HIDDEN>
 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256);
 Thu, 26 Dec 2024 04:27:52 -0800 (PST)
From: Visuwesh <visuweshm@HIDDEN>
To: bug-gnu-emacs@HIDDEN
Subject: [PATCH] Make 'yank-media' autoselect the best media type
X-Debbugs-Cc: Ihor Radchenko <yantar92@HIDDEN>, pinmacs@HIDDEN,
 rpluim@HIDDEN, Eli Zaretskii <eliz@HIDDEN>
Date: Thu, 26 Dec 2024 17:57:50 +0530
Message-ID: <87o70yeiih.fsf@HIDDEN>
User-Agent: Gnus/5.13 (Gnus v5.13)
MIME-Version: 1.0
Content-Type: multipart/mixed; boundary="=-=-="
Received-SPF: pass client-ip=2607:f8b0:4864:20::644;
 envelope-from=visuweshm@HIDDEN; helo=mail-pl1-x644.google.com
X-Spam_score_int: -20
X-Spam_score: -2.1
X-Spam_bar: --
X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1,
 DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, FREEMAIL_FROM=0.001,
 RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001,
 SPF_PASS=-0.001 autolearn=ham autolearn_force=no
X-Spam_action: no action
X-Spam-Score: -1.3 (-)
X-Debbugs-Envelope-To: submit
X-BeenThere: debbugs-submit <at> debbugs.gnu.org
X-Mailman-Version: 2.1.18
Precedence: list
List-Id: <debbugs-submit.debbugs.gnu.org>
List-Unsubscribe: <https://debbugs.gnu.org/cgi-bin/mailman/options/debbugs-submit>, 
 <mailto:debbugs-submit-request <at> debbugs.gnu.org?subject=unsubscribe>
List-Archive: <https://debbugs.gnu.org/cgi-bin/mailman/private/debbugs-submit/>
List-Post: <mailto:debbugs-submit <at> debbugs.gnu.org>
List-Help: <mailto:debbugs-submit-request <at> debbugs.gnu.org?subject=help>
List-Subscribe: <https://debbugs.gnu.org/cgi-bin/mailman/listinfo/debbugs-submit>, 
 <mailto:debbugs-submit-request <at> debbugs.gnu.org?subject=subscribe>
Errors-To: debbugs-submit-bounces <at> debbugs.gnu.org
Sender: "Debbugs-submit" <debbugs-submit-bounces <at> debbugs.gnu.org>
X-Spam-Score: -2.3 (--)

--=-=-=
Content-Type: text/plain

Tags: patch

This is a continuation of the long thread in emacs-devel:
https://yhetil.org/emacs-devel/79fc91f3-c2c3-44db-9817-595808917f26@HIDDEN/

This message provides a summary:
https://yhetil.org/87r06cj2nd.fsf@HIDDEN

Ihor wrote:

> The only comment is that leaving an option to return a list of types
> rather than only a single type will make things more flexible.

And this is now done in the attached patch.

Before I go about writing NEWS and updating the manual, what do you
think about the attached instead?  I think the variable
yank-media-preferred-types gives a more granular control for major-mode
authors than (add-function (local 'yank-media-autoselect-function) ...)


--=-=-=
Content-Type: text/x-diff
Content-Disposition: attachment; filename=yank-media-autoselect-other.diff

diff --git a/lisp/yank-media.el b/lisp/yank-media.el
index 17981c37c0e..010e942680d 100644
--- a/lisp/yank-media.el
+++ b/lisp/yank-media.el
@@ -29,19 +29,57 @@

 (defvar yank-media--registered-handlers nil)

+(defvar yank-media-preferred-types
+  `(;; Check first since LibreOffice also puts a PNG image in the
+    ;; clipboard when a table cell is copied.
+    application/x-libreoffice-tsvc
+    ;; Give PNG more priority.
+    image/png
+    image/jpeg
+    ;; These are files copied/cut to the clipboard from a file manager.
+    ,(lambda (mimetypes)
+       (seq-find (lambda (type)
+                (string-match-p "x-special/\\(gnome\\|KDE\\|mate\\)-files"
+                                (symbol-name type)))
+              mimetypes))
+    ;; FIXME: We should have a way to handle text/rtf.
+    text/html)
+  "List of mime types in the order of preference.
+Each element in the list should be a symbol to choose the mime type
+denoted by it, or a function of one argument, the mime types available,
+and should return the mime types to use.")
+
+(defvar yank-media-autoselect-function #'yank-media-autoselect-function
+  "Function to auto select the best mime types when many are available.
+The function is called with a list of mime types that have handler in
+the current buffer, and should return the type or a list of types to
+use, or nil if no preferred type is found.")
+
+(defun yank-media-autoselect-function (mimetypes)
+  (catch 'preferred
+    (dolist (typ yank-media-preferred-types)
+      (let ((ret (if (functionp typ)
+                     (funcall typ mimetypes)
+                   (and (memq typ mimetypes) typ))))
+        (when ret (throw 'preferred typ))))))
+
 ;;;###autoload
-(defun yank-media ()
+(defun yank-media (&optional noselect)
   "Yank media (images, HTML and the like) from the clipboard.
 This command depends on the current major mode having support for
 accepting the media type.  The mode has to register itself using
 the `yank-media-handler' mechanism.
+Optional argument NOSELECT non-nil (interactively, with a prefix
+argument) means to skip auto-selecting the best mimetype and ask
+for the media type to use when multiple are available.

 Also see `yank-media-types' for a command that lets you explore
 all the different selection types."
-  (interactive)
+  (interactive "P")
   (unless yank-media--registered-handlers
     (user-error "The `%s' mode hasn't registered any handlers" major-mode))
-  (let ((all-types nil))
+  (let ((all-types nil)
+        pref-type)
     (pcase-dolist (`(,handled-type . ,handler)
                    yank-media--registered-handlers)
       (dolist (type (yank-media--find-matching-media handled-type))
@@ -49,18 +87,28 @@ yank-media
     (unless all-types
       (user-error
        "No handler in the current buffer for anything on the clipboard"))
-    ;; We have a handler in the current buffer; if there's just
-    ;; matching type, just call the handler.
-    (if (length= all-types 1)
-        (funcall (cdar all-types) (caar all-types)
-                 (yank-media--get-selection (caar all-types)))
-      ;; More than one type the user for what type to insert.
+    (setq pref-type (and (null noselect)
+                         (funcall yank-media-autoselect-function
+                                  (mapcar #'car all-types))))
+    (cond
+     ;; We have one preferred mime type so use it unconditionally.
+     ((and pref-type (symbolp pref-type))
+      (funcall (cdr (assq pref-type all-types)) pref-type
+               (yank-media--get-selection pref-type)))
+     ;; The user chose to not autoselect and there's just a single type,
+     ;; just call the handler.
+     ((and (null pref-type) (length= all-types 1))
+      (funcall (cdar all-types) (caar all-types)
+               (yank-media--get-selection (caar all-types))))
+     ;; More than one type, ask the user for what type to insert.
+     (t
       (let ((type
              (intern
               (completing-read "Several types available, choose one: "
-                               (mapcar #'car all-types) nil t))))
+                               (or pref-type (mapcar #'car all-types))
+                               nil t))))
         (funcall (alist-get type all-types)
-                 type (yank-media--get-selection type))))))
+                 type (yank-media--get-selection type)))))))

 (defun yank-media--find-matching-media (handled-type)
   (seq-filter

--=-=-=
Content-Type: text/plain


I know that I have to update the Info node (info "(elisp) Yanking
Media").  Does (info "(emacs) Clipboard") need any update too?

In GNU Emacs 31.0.50 (build 27, x86_64-pc-linux-gnu, X toolkit, cairo
 version 1.18.2, Xaw scroll bars) of 2024-12-07 built on astatine
Repository revision: 9ddec89e422d0dd6e9069731b8f2dd2c90aa5607
Repository branch: master
Windowing system distributor 'The X.Org Foundation', version 11.0.12101014
System Description: Debian GNU/Linux trixie/sid

Configured using:
 'configure --with-sound=alsa --with-x-toolkit=lucid --without-xaw3d
 --without-gconf --without-libsystemd --with-cairo CFLAGS=-g3'

--=-=-=
Content-Type: text/patch
Content-Disposition: attachment;
 filename=0001-Make-yank-media-autoselect-the-best-media-type.patch

From b12ea40822eb0cacf67de514f273cba6f283e580 Mon Sep 17 00:00:00 2001
From: Visuwesh <visuweshm@HIDDEN>
Date: Thu, 26 Dec 2024 17:50:13 +0530
Subject: [PATCH] Make 'yank-media' autoselect the best media type

* lisp/yank-media.el (yank-media-autoselect-function)
(yank-media-autoselect-function): Add new variable and function
to make 'yank-media' choose the best/preferred media type out of
the available ones.
(yank-media): Change to account for above.
---
 lisp/yank-media.el | 62 ++++++++++++++++++++++++++++++++++++++--------
 1 file changed, 51 insertions(+), 11 deletions(-)

diff --git a/lisp/yank-media.el b/lisp/yank-media.el
index 17981c37c0e..86dc05cd4f0 100644
--- a/lisp/yank-media.el
+++ b/lisp/yank-media.el
@@ -29,19 +29,49 @@
 
 (defvar yank-media--registered-handlers nil)
 
+(defvar yank-media-autoselect-function #'yank-media-autoselect-function
+  "Function to auto select the best mime types when many are available.
+The function is called with a list of mime types that have handler in
+the current buffer, and should return the type or a list of types to
+use, or nil if no preferred type is found.")
+
+(defun yank-media-autoselect-function (mimetypes)
+  (cond
+   ;; Check first since LibreOffice also puts a PNG image in the
+   ;; clipboard when a table cell is copied.
+   ((memq 'application/x-libreoffice-tsvc mimetypes)
+    'application/x-libreoffice-tsvc)
+   ;; Give PNG more priority.
+   ((memq 'image/png mimetypes)
+    'image/png)
+   ((memq 'image/jpeg mimetypes)
+    'image/jpeg)
+   ;; These are files copied/cut to the clipboard from a file manager.
+   ((seq-find (lambda (type)
+                (string-match-p "x-special/\\(gnome\\|KDE\\|mate\\)-files"
+                                (symbol-name type)))
+              mimetypes))
+   ;; FIXME: We should have a way to handle text/rtf.
+   ((memq 'text/html mimetypes)
+    'text/html)))
+
 ;;;###autoload
-(defun yank-media ()
+(defun yank-media (&optional noselect)
   "Yank media (images, HTML and the like) from the clipboard.
 This command depends on the current major mode having support for
 accepting the media type.  The mode has to register itself using
 the `yank-media-handler' mechanism.
+Optional argument NOSELECT non-nil (interactively, with a prefix
+argument) means to skip auto-selecting the best mimetype and ask
+for the media type to use when multiple are available.
 
 Also see `yank-media-types' for a command that lets you explore
 all the different selection types."
-  (interactive)
+  (interactive "P")
   (unless yank-media--registered-handlers
     (user-error "The `%s' mode hasn't registered any handlers" major-mode))
-  (let ((all-types nil))
+  (let ((all-types nil)
+        pref-type)
     (pcase-dolist (`(,handled-type . ,handler)
                    yank-media--registered-handlers)
       (dolist (type (yank-media--find-matching-media handled-type))
@@ -49,18 +79,28 @@ yank-media
     (unless all-types
       (user-error
        "No handler in the current buffer for anything on the clipboard"))
-    ;; We have a handler in the current buffer; if there's just
-    ;; matching type, just call the handler.
-    (if (length= all-types 1)
-        (funcall (cdar all-types) (caar all-types)
-                 (yank-media--get-selection (caar all-types)))
-      ;; More than one type the user for what type to insert.
+    (setq pref-type (and (null noselect)
+                         (funcall yank-media-autoselect-function
+                                  (mapcar #'car all-types))))
+    (cond
+     ;; We have one preferred mime type so use it unconditionally.
+     ((and pref-type (symbolp pref-type))
+      (funcall (cdr (assq pref-type all-types)) pref-type
+               (yank-media--get-selection pref-type)))
+     ;; The user chose to not autoselect and there's just a single type,
+     ;; just call the handler.
+     ((and (null pref-type) (length= all-types 1))
+      (funcall (cdar all-types) (caar all-types)
+               (yank-media--get-selection (caar all-types))))
+     ;; More than one type, ask the user for what type to insert.
+     (t
       (let ((type
              (intern
               (completing-read "Several types available, choose one: "
-                               (mapcar #'car all-types) nil t))))
+                               (or pref-type (mapcar #'car all-types))
+                               nil t))))
         (funcall (alist-get type all-types)
-                 type (yank-media--get-selection type))))))
+                 type (yank-media--get-selection type)))))))
 
 (defun yank-media--find-matching-media (handled-type)
   (seq-filter
-- 
2.45.2


--=-=-=--




Acknowledgement sent to Visuwesh <visuweshm@HIDDEN>:
New bug report received and forwarded. Copy sent to yantar92@HIDDEN, pinmacs@HIDDEN, rpluim@HIDDEN, eliz@HIDDEN, bug-gnu-emacs@HIDDEN. Full text available.
Report forwarded to yantar92@HIDDEN, pinmacs@HIDDEN, rpluim@HIDDEN, eliz@HIDDEN, bug-gnu-emacs@HIDDEN:
bug#75116; Package emacs. Full text available.
Please note: This is a static page, with minimal formatting, updated once a day.
Click here to see this page with the latest information and nicer formatting.
Last modified: Thu, 13 Feb 2025 13:45:02 UTC

GNU bug tracking system
Copyright (C) 1999 Darren O. Benham, 1997 nCipher Corporation Ltd, 1994-97 Ian Jackson.