Further Fun With the Clipboard

In the previous episode of this blog, I mused about how Emacs should perhaps handle non-text yanking (i.e., HTML and images).

I didn’t really want to write a mode to examine the clipboard and the selections, but I did anyway just now, and I discovered something kinda interesting.

Well I never!

Anyway, this is basically what the mode looks like:

It just lists all the types of selection that exist under the primary/secondary selections, as well as the clipboard. In this instance, I’ve done a “Copy image” in Firefox. So I can inspect the text/html “version” the image…

Which turns out to be the <img;> bit, logically enough.

And the image/png is the image itself, even more logically enough.

But here’s the slightly interesting bit: If you mark some text in Firefox (i.e., put the text into the primary selection in nerd speak), you get these selection types:

And the TEXT bit is as you’d expect…

But what’s that text/x-moz-url-priv?

It’s the URL of the page you marked the text on! This can obviously be used for UX purposes, like providing a function that’d simultaneously quote something and do the link at the same time. Like this:

So something like:

  (when-let ((url (x-get-selection-internal 'PRIMARY 'text/x-moz-url-priv)))
    (insert (format "On <a href="%S">somebody</a> wrote:\n\n"
                    (ewp-decode-text-selection url))))
  (insert "<blockquote>\n")
  (insert (substring-no-properties (current-kill 0)))
  (insert "</blockquote>\n\n")

You can find this stuff on Microsoft Github.

Chrome isn’t as helpful as Firefox here, you just get a very minimal selection list when marking some text there:

(Oh, yeah, I added a command to do screenshots directly from the ewp composition buffer. Otherwise this blog would have taken me HOURS and HOURS to make.)

Of course, how useful this stuff is outside of a writing-for-the-web context is debatable…

PERSON A: It’s not useful!

PERSON B: Oh, OK.

Well, that was a short debate!

March 1942: Reap the Wild Wind




















When I saw the start of the title sequence, with the American eagle and everything, I thought that we’d finally arrived at a honest-to-goodness American war movie.

But no: It’s a Cecil B. DeMille extravaganza set in 1840.

It’s a romantic/comedic/epic kind of thing, and I had no idea that the plot would get this complicated. It’s very nice watching a movie and not really knowing where it’s going, in a general sense. And it doesn’t happen that often. I wasn’t even sure what genre this was going to turn out to be.

Paulette Goddard is great as the leading character here, and it’s fun seeing John Wayne not on a horse. And looking younger than most films I’ve seen him in.

If you want to be picky, I thing you could say that this film just goes through too many phases: Naval adventure, romantic intrigue, courtroom drama and finally undersea horror. But I like it. It’s huge and unwieldy, but it’s good.

Reap the Wild Wind. Cecil B. DeMille. 1942.

Popular movies in March 1942 according to IMDB:

PosterVotesRatingMovie
3966.8Star Spangled Rhythm
8776.8The Male Animal
22856.8Reap the Wild Wind
3696.7The Courtship of Andy Hardy
3976.7Rings on Her Fingers
3056.5Always in My Heart
5886.2To the Shores of Tripoli
41206.1The Ghost of Frankenstein
8084.2Black Dragons

This blog post is part of the Decade series.

February 1942: To Be Or Not To Be














To Be Or Not To Be. Ernst Lubitsch. 1942.

Oh yeah! This one! I’ve seen it several times before. And the Mel Brooks remake, of course. I’ve been attempting to avoid movies I’ve seen before in this blog series, but my research sucks!
“Heil myself.”

It’s a very funny film, of course, and as a comedy/thriller everything fits together so neatly. Masterful. But I wonder what on earth they were thinking when making this. I mean, Germany had invaded Poland in 1939, and the horrors in Poland were ongoing when they made it. So… they responded by making a very funny movie making fun of Germans.
Ah, yeah, not everybody were amused:

To Be or Not To Be, now regarded as one of the best films of Lubitsch’s, Benny’s and Lombard’s careers, was initially not well received by the public, many of whom could not understand the notion of making fun out of such a real threat as the Nazis. According to Jack Benny’s unfinished memoir, published in 1991, his own father walked out of the theater early in the film, disgusted that his son was in a Nazi uniform, and vowed not to set foot in the theater again. Benny convinced him otherwise and his father ended up loving the film, and saw it forty-six times.

(According to a wikipedia.)

Popular movies in February 1942 according to IMDB:

PosterVotesRatingMovie
199808.2To Be or Not to Be
31827.7Kings Row
9747.6Castle in the Desert
70597.3Woman of the Year
11577.1Ride ‘Em Cowboy
4297.1Mr. Wise Guy
12647.0Roxie Hart
4576.7The Great Man’s Lady
10396.5Captains of the Clouds
4636.3The Lady Is Willing

This blog post is part of the Decade series.

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!

My New Concert Review Blog

I went to a metal festival. In a church!

It’s been desecrated I mean des-consecrated, but it’s still a great room. Thank you, religious people, for making impractical spaces like this that can be used for fun stuff now.

The downstairs crypt is more intimate.

Anyway, as usual I thought that the traditional metal bands sounded kinda thin (Lonely Camel was very swampy, but I’m all about Skynyrd, y’all), but Domkraft were awesome in the basement, and Toner Low shook the church to its foundations as the headliner in the main space.

Isn’t that the most evil metal band name ever? It is.