Function: yank-media

yank-media is an autoloaded, interactive and byte-compiled function defined in yank-media.el.gz.

Signature

(yank-media &optional NOSELECT)

Documentation

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.

View in manual

Probably introduced at or before Emacs version 29.1.

Key Bindings

Source Code

;; Defined in /usr/src/emacs/lisp/yank-media.el.gz
;;;###autoload
(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 "P")
  (unless yank-media--registered-handlers
    (user-error "The `%s' mode hasn't registered any handlers" major-mode))
  (let ((all-types nil)
        pref-type)
    (pcase-dolist (`(,handled-type . ,handler)
                   yank-media--registered-handlers)
      (dolist (type (yank-media--find-matching-media handled-type))
        (push (cons type handler) all-types)))
    (unless all-types
      (user-error
       "No handler in the current buffer for anything on the clipboard"))
    (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)))))
     ;; No autoselection and multiple media types available.
     ((and noselect (length> all-types 1))
      (let ((type
             (intern
              (completing-read "Several types available, choose one: "
                               (or pref-type (mapcar #'car all-types))
                               nil t))))
        (funcall (alist-get type all-types)
                 type (yank-media--get-selection type)))))))