The Mysteries of the Selection

After writing the package to edit WordPress articles in Emacs, I started thinking about doing something about non-text X selections in Emacs.

Emacs has all the required low-level support it needs to be able to yank HTML and images into Emacs, but nobody’s mainstreamed more high-level support into Emacs. Mostly because nobody really knew what it would look like, I think. (And because most Emacsens don’t really blog that much.)

Let’s have a look at what’s available here.

Let’s mark a word in an xterm, and then let’s see what Emacs gets:

(gui-get-selection 'PRIMARY 'TARGETS)
[TIMESTAMP TARGETS MULTIPLE UTF8_STRING
COMPOUND_TEXT TEXT STRING text/plain\;charset=utf-8
text/plain]

and compare when marking a word in Firefox:

(gui-get-selection 'PRIMARY 'TARGETS)
[TIMESTAMP TARGETS MULTIPLE text/html
text/_moz_htmlcontext text/_moz_htmlinfo
UTF8_STRING COMPOUND_TEXT TEXT STRING
text/x-moz-url-priv]

See? When marking words in an xterm, you get at text/plain thing you can yank in Emacs (and that’s what normally happens), but you mark something in Firefox, you get a text/html (among other things) in addition).

The same thing is the case in Chrome, although there you have to say “Copy” first to get the text/html part, and it lands on the CLIPBOARD and not in PRIMARY.

But that should be OK, anyway. Let’s look at the output from the text/html selection from Firefox.

Er, OK, that’s… little-endian utf-16. Er, OK… So that’s a thing?

Let’s look at the selection from Chrome, then:

So that’s a UTF-8 string? With… a nul at the end?

*sigh*

Well, OK, it’s easy enough to recognise one or the other forms, as long as there’s only two. *crosses fingers*

That means that the function for yanking HTML ends up being something like:

(defun ewp-yank-html ()
  "Yank the contents of the current X text/html selection, if any."
  (interactive)
  (let ((data (loop for type in '(PRIMARY CLIPBOARD)
                    for data = (x-get-selection-internal type 'text/html)
                    when data
                    return data)))
    (if (not data)
        (message "No text/html data in the current selection")
      (set-mark (point))
      (insert
       (if (and (> (length data) 2)
                (= (aref data 0) 255)
                (= (aref data 1) 254))
           ;; Somehow the selection is UTF-16 when selecting text in
           ;; Firefox.
           (decode-coding-string data 'utf-16-le)
         ;; But some sources add a nul to the end of the data.
         (decode-coding-string
          (replace-regexp-in-string (string 0) "" data)
          'utf-8))))))

Or should it look at the TIMESTAMP from the selections and then yank the newest one? By testing, it seems to be doing the right thing when not doing that…

Yanking images into buffers works along the same line, but there’s differences between where the images originate, of course. If doing a “Copy image” from Firefox, I can yank them as any format (image/png, image/jpeg), but from Chrome doesn’t support transcoding, so you have to loop a bit to find what the format really is. Or… I could look at the output from `gui-get-selection’? Yes, that seems like a better idea.

Let me try to yank a random image from Google…

Yup, that works. So you end up with something like:

(loop for type in '(PRIMARY CLIPBOARD)
      for st = (loop for st across
                     (gui-get-selection type 'TARGETS)
                     when (equal (car (split-string
                                       (symbol-name st) "/"))
                                 "image")
                     return st)
      when st
      return (x-get-selection-internal type st))

But just yanking an image into Emacs isn’t very useful: You have to be able to do something with it. In an HTML-like mode as this Postpress editing mode makes that kinda easy: Just represent it as an <img> element… and stash the entire image in the src=”data:image/jpeg;base64,FKJDSHJF…” bit. Since I’m showing the image as the `display’ bit on top, having a few megabytes of noise in the buffer doesn’t make any difference.

I was worried that this might somehow slow Emacs down, but nope. Displaying very long lines is something that Emacs doesn’t like, but as long as it’s hidden, it doesn’t seem to make any difference.

And it autosaves nicely, too.

OK, making it that easy to insert images into blog posts perhaps isn’t a good idea, but why should all ideas be good?

Anyway, I think something like this is something that Emacs should have, and I think I’ll pursue that further when I get time to do some more real Emacs work (which probably won’t be until like February).

I was originally pondering whether to offer an interface to a detailed view of the selection/clipboard, so that you can journey into all the different selection types, but after using this stuff for a couple of days, I think I’ve reached the conclusion that the only other two yanking types that makes sense (other than plain text, which we have now) are HTML and images. At least in a WYSIWYG-ish context…

Finally! Emacs is going to catch up with 90s editors! At least on this minor point! Amazing!

2 thoughts on “The Mysteries of the Selection”

Leave a Reply