Copying Media to Emacs

Emacs isn’t really a tool for creating WYSIWYG documents, so the editing support for anything other than plain text (and code) is pretty basic.

As I noted a few years back, Emacs has the primitives needed to interact with non-text clipboard types, so adding code to (say) allow choosing “Copy Image” in a browser and then pasting it into a buffer would be trivial.

But then what? Now you have an image in your buffer — but what do you do with it? For yanking media to be useful, the Emacs mode has to be able to do something meaningful with it.

For instance, a mail mode could put it into an attachment (or inline it) — so that’s meaningful. The HTML mode could allow pasting both HTML and images into the mode — it could use the HTML from the browser as is, and it could offer to save the image to a local file and then insert an <img> pointing to it.

Org mode could probably also do something with the data?

And so on. So I’ve now added a general infrastructure to Emacs 29 to allow this: Modes that feel that they can do something useful with non-plain-text selections can register themselves as such (and say which types they can handle), and then there’s a new ‘M-x yank-media’ command the user can use.

(This doesn’t work on all systems yet — basically it works the ones that traditionally use X. Emacs on Windows hasn’t implemented all the necessary plumbing yet (but it seems like it shouldn’t take much), and it’s probably more work to be done on Macos. But Linux, the BSDs and similar should work fine now, and the rest show support it eventually.)

Watch! With amazement as I copy an image from Firefox and yank it into a mail buffer:

Behold! With astonishment as I copy some text from Firefox into ‘html-mode’ and I get HTML there:

So… this is something that Emacs should have grown about three decades ago, but 🇺🇲! 🇺🇲! 🇺🇲! 🎆

4 thoughts on “Copying Media to Emacs”

  1. This is great! Came over from https://www.reddit.com/r/emacs/comments/tby5cs/orgmacimagepaste_paste_images_and_cropped_pdf/, a small mode I wrote for doing just this on emacs-mac in org in v27. We really need a “clipboard-info” command or similar which can report the existence of and information about image data on the kill ring. Then you could simply overlay yank to do the job of yank-media. On Mac the clipboard holds various optional formats for the same data, and apps have to choose which they prefer. E.g. for your example of an image copied from a browser window, this gets presented as multiple image formats (TIFF, PNG, JPEG, etc.) as well as a few text formats like URL/UTF/etc.

Leave a Reply to larsmagne23Cancel reply