all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: Visuwesh <visuweshm@gmail.com>
To: Ihor Radchenko <yantar92@posteo.net>
Cc: Eli Zaretskii <eliz@gnu.org>,
	 pinmacs@cas.cat,  rpluim@gmail.com, emacs-devel@gnu.org
Subject: Re: yank-media: allow users to limit image types that can be inserted
Date: Thu, 12 Dec 2024 21:48:30 +0530	[thread overview]
Message-ID: <87r06cj2nd.fsf@gmail.com> (raw)
In-Reply-To: <87cyjbwsoh.fsf@gmail.com> (Visuwesh's message of "Mon, 04 Nov 2024 09:34:30 +0530")

[திங்கள் நவம்பர் 04, 2024] Visuwesh wrote:

To recap,

The OP asked for a way to choose a specific image type out of the
multiple types available in their clipboard.  We decided that Emacs
should auto-select the best type itself and have a way for major-modes
to customise this selection process.  Auto-selection should also be
skipped with a prefix arg.

> [ஞாயிறு நவம்பர் 03, 2024] Ihor Radchenko wrote:
>
>> Visuwesh <visuweshm@gmail.com> writes:
>>
>>>> And I'm saying that I don't understand why users would need such
>>>> control, in addition to being able to select a format in each case.
>>>
>>> OK, I think I finally understand your stance: we let yank-media pick the
>>> best format out of all in the clipboard based on rules that a major-mode
>>> can tweak.  Apart from this, we also have C-u or somesuch that asks the
>>> user to select the data type instead of auto-selecting it.  Since the
>>> C-u is good enough to override a case-by-case basis, you do not see
>>> value in having a defcustom to let the user customise the selection
>>> rules for the best data type, correct?
>>
>> So, what will be the plan here? A patch?
>
> I just want to inform that I probably won't be able to get to writing a
> patch until mid December since I hardly have time with my semester end
> nearing.

Here's the promised patch (I can create a bug report if that is
preferred):

diff --git a/lisp/yank-media.el b/lisp/yank-media.el
index 17981c37c0e..a5ad913c24a 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 type 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 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,27 @@ 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 a preferred mime type so use it unconditionally.
+     ((and (null noselect) pref-type)
+      (funcall (cdr (assq pref-type all-types)) pref-type
+               (yank-media--get-selection pref-type)))
+     ;; The user chose to not autoselet and there's just a single type,
+     ;; just call the handler.
+     ((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.
+     (t
       (let ((type
              (intern
               (completing-read "Several types available, choose one: "
                                (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

As planned, yank-media auto-selects the best media type out of the
available types by default.  This auto-selection can be skipped by
giving a prefix arg.  Some details about the selection process itself:

  1. The autoselect function can return only one preferred type.
  2. There is no user option to control this selection process.  I
     cannot envision such an option myself.  Major mode authors can
     control the selection rules by using add-function (like they
     already do with e.g., filter-buffer-substring-function):

        (add-function :before/:after/:around/...
                      (local 'yank-media-autoselect-function)
                      #'major-mode-ymaf)

     An informed user could do the same if she desires.
  3. The autoselect function _can_ return nil.  If it does, then we
     proceed as if the prefix arg was given.  This makes sense to me
     since we cannot hope to be exhaustive since the mime type can be
     _anything_.  For e.g., Avogadro puts chemical/x-mdl-molfile when
     you select and copy atoms but I doubt many Emacs user would ever
     come across such a type in their clipboard.  So it is best we fall
     over to the NOSELECT=t when the autoselect function returns nil
     instead of signalling an error.
  4. We select application/x-libreoffice-tsvc first since LibreOffice
     also puts image/png when you copy table cell(s).
  5. We prefer PNG images over other types.
  6. We prefer text/html but I believe the plan is to eventually prefer
     text/rtf?

Now some questions:

  1. Should we allow the autoselect function to return _multiple_
     preferred types?
  2. Should we prefer image/svg over image/png and image/jpeg?  Should
     we prefer it over image/jpeg at least?  Or do we leave it to the
     major-mode to take care of this (e.g., as in Robert's example)?
  3. We prefer images over files cut/copied to the clipboard.  I don't
     know if any software puts both image/png and
     x-special/gnome-copied-files to the clipboard.  If it does, which
     do we prefer?  The list of file:// links or raw image/png data?
  4. 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.



  parent reply	other threads:[~2024-12-12 16:18 UTC|newest]

Thread overview: 73+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-09-22 16:53 yank-media: allow users to limit image types that can be inserted pinmacs
2024-09-23 11:20 ` Eli Zaretskii
2024-09-23 13:46   ` Visuwesh
2024-09-23 14:30     ` Eli Zaretskii
2024-09-23 15:06       ` Visuwesh
2024-09-23 15:48         ` Eli Zaretskii
2024-09-23 15:09       ` Robert Pluim
2024-09-23 15:14         ` Visuwesh
2024-09-23 15:20           ` Robert Pluim
2024-09-23 15:58           ` Eli Zaretskii
2024-09-24  5:00             ` Visuwesh
2024-09-24  5:10               ` Visuwesh
2024-09-24 11:57               ` Eli Zaretskii
2024-09-24 12:42                 ` Visuwesh
2024-09-23 15:54         ` Eli Zaretskii
2024-09-23 16:10           ` Robert Pluim
2024-09-23 16:34             ` Eli Zaretskii
2024-09-23 18:00               ` pinmacs
2024-09-23 18:35                 ` Eli Zaretskii
2024-09-23 20:45                   ` Pedro
2024-09-23 21:08                   ` pinmacs
2024-09-24  8:15                     ` Robert Pluim
2024-09-24 11:30                     ` Eli Zaretskii
2024-09-24 12:18                       ` Robert Pluim
2024-09-24 13:08                         ` Eli Zaretskii
2024-09-24 13:38                           ` Visuwesh
2024-09-24 13:50                             ` Eli Zaretskii
2024-09-24  5:08                   ` Visuwesh
2024-09-24 12:00                     ` Eli Zaretskii
2024-09-24 12:50                       ` Visuwesh
2024-09-24 13:23                         ` Eli Zaretskii
2024-09-24 13:37                           ` Visuwesh
2024-10-26 17:27                       ` Ihor Radchenko
2024-10-26 19:09                         ` Eli Zaretskii
2024-10-27  8:17                           ` Ihor Radchenko
2024-10-27  9:14                             ` Eli Zaretskii
2024-10-27  9:36                               ` Visuwesh
2024-10-27 10:09                                 ` Eli Zaretskii
2024-10-27 15:02                                   ` Visuwesh
2024-10-27 17:11                                     ` Eli Zaretskii
2024-10-28 13:37                                       ` Visuwesh
2024-10-29 11:29                                       ` Visuwesh
2024-10-30 23:22                                         ` Pedro
2024-10-31  8:29                                           ` Eli Zaretskii
2024-10-31 10:47                                             ` pinmacs
2024-10-31 11:16                                               ` Eli Zaretskii
2024-10-31 11:51                                                 ` pinmacs
2024-10-31 14:31                                                   ` Eli Zaretskii
     [not found]                                                 ` <c67bb616-710b-4272-919d-bf4ece8e7c99@imayhem.com>
2024-10-31 14:20                                                   ` Eli Zaretskii
2024-10-31 18:21                                                     ` Ihor Radchenko
2024-10-31 19:03                                                       ` Eli Zaretskii
2024-10-31 19:08                                                         ` Ihor Radchenko
2024-10-31 19:29                                                           ` Eli Zaretskii
2024-10-31 19:42                                                             ` Ihor Radchenko
2024-11-01  7:01                                                               ` Eli Zaretskii
2024-10-31  8:48                                           ` Visuwesh
2024-10-31  8:24                                         ` Eli Zaretskii
2024-10-31  8:46                                           ` Visuwesh
2024-10-31  9:56                                             ` Eli Zaretskii
2024-11-01  5:20                                               ` Visuwesh
2024-11-01  7:38                                                 ` Eli Zaretskii
2024-11-03 17:19                                                 ` Ihor Radchenko
2024-11-03 18:47                                                   ` Eli Zaretskii
2024-11-04  4:04                                                   ` Visuwesh
2024-11-04 20:03                                                     ` Ihor Radchenko
2024-11-04 20:19                                                       ` Eli Zaretskii
2024-12-12 16:18                                                     ` Visuwesh [this message]
2024-12-21  9:40                                                       ` Eli Zaretskii
2024-12-21  9:47                                                         ` Ihor Radchenko
2024-10-28 18:39                               ` Ihor Radchenko
2024-10-28 18:50                                 ` Eli Zaretskii
2024-09-23 18:11               ` Eli Zaretskii
2024-09-24  8:38                 ` Robert Pluim

Reply instructions:

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

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

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

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

  git send-email \
    --in-reply-to=87r06cj2nd.fsf@gmail.com \
    --to=visuweshm@gmail.com \
    --cc=eliz@gnu.org \
    --cc=emacs-devel@gnu.org \
    --cc=pinmacs@cas.cat \
    --cc=rpluim@gmail.com \
    --cc=yantar92@posteo.net \
    /path/to/YOUR_REPLY

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

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

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

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.