diff --git a/lisp/yank-media.el b/lisp/yank-media.el index 563aae85419..30ede069769 100644 --- a/lisp/yank-media.el +++ b/lisp/yank-media.el @@ -29,6 +29,12 @@ (defvar yank-media--registered-handlers nil) +(defvar-local yank-media-preferred-type-function nil + "Function to select preferred mimetypes out of available ones. +If non-nil, this function is called with a list of mimetype that is +handled in the current buffer, and should return a list of preferred +types to use.") + ;;;###autoload (defun yank-media () "Yank media (images, HTML and the like) from the clipboard. @@ -49,18 +55,29 @@ 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. - (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)))))) + ;; Remove types that are unwanted. + (let ((pref-types (funcall (if yank-media-preferred-type-function + yank-media-preferred-type-function + #'identity) + (mapcar #'car all-types)))) + (unless pref-types + (user-error + ;; FIXME: Improve the error message. + "No handler in the current buffer for preferred data types")) + ;; We have a handler in the current buffer; if there's just + ;; one preferred type, just call the handler. + (if (length= pref-types 1) + (let ((data (assq (car pref-types) all-types))) + (funcall (cdr data) (car pref-types) + (yank-media--get-selection (car pref-types)))) + ;; More than one type preferred, ask the user for what type to + ;; insert. + (let ((type + (intern + (completing-read "Several types available, choose one: " + pref-types nil t)))) + (funcall (alist-get type all-types) + type (yank-media--get-selection type))))))) (defun yank-media--find-matching-media (handled-type) (seq-filter