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 πŸ‡ΊπŸ‡²! πŸ‡ΊπŸ‡²! πŸ‡ΊπŸ‡²! πŸŽ†

One thought on “Copying Media to Emacs”

Leave a Reply