Wednesday, Debtember 03, 2008
Why didn't I get a copy of this memo?
X11 has these things called “selections.” They have names. There are really only two you need to know about: the Primary selection and the Clipboard selection. An application is said to “own” a selection when it raises its hand and says, “I have the Primary selection now.” Only one application can own a selection at a time, so when one app asserts selection ownership, the previous owner loses it.
One of the really cool, yet rarely used, features of the selection mechanism is that it can negotiate what data formats to use. It's not just about text. When one application asks another for the selection, part of their communication involves the requester asking the owner for the list of types in which they are capable of delivering the selection data; then the requester picks the format they like best, and asks for it that way.
I've been struggling with writing blog entries for years, and yet, here I am, one day short of nine years still writing posts the old fashioned way—painfully (that link shows the steps I go through in quoting a page for this blog, and as you can see, it's several manual steps).
But on an unrelated project to this blog (and work) I had to dive into the inner workings of the X11 clipboard. In doing so, I came across Jamie Zawinski's page on X Selections, X Cut Buffers, and Emacs Kill Rings, which pretty much describes at a high level how the whole X11 clipboard thing works, but there was this bit about half-way down the page:
The content negotiation mechansim is very powerful, and I wish more applications would take advantage of it.
You can experiment with content negotiation with other apps from an XEmacs lisp-interaction buffer. To see what types an app will convert its selection to, make a selection in that app, and then type:(get-selection-internal 'PRIMARY 'TARGETS) ==> [TARGETS TIMESTAMP TEXT STRING LENGTH FILE_NAME OWNER_OS HOST_NAME USER CLASS NAME CLIENT_WINDOW PROCESS COMPOUND_TEXT] (get-selection-internal 'PRIMARY 'FILE_NAME) ==> "http://www.jwz.org/doc/x-cut-and-paste.html"
TARGETS? There might be more to the current primary (or
clipboard) selection than just plain text? I must play around with this.
And lo' I did.
And I'm glad I did, because what I found was amazing.
I highlighted some text in Firefox (running under Linux and
X11), and selected (through
some code I had to write—there appears to be no other way to do this other
than XEmacs, which I
don't have installed, nor do I wish to install) the
TARGETS, figure out what format the data is returned (an array of X11
atoms for what it's worth) and well … what do you know …
TIMESTAMP TARGETS text/html text/_moz_htmlcontext text/_moz_htmlinfo UTF8_STRING COMPOUND_TEXT TEXT STRING text/x-moz-url-priv
Hmm … So, instead of getting just the plain text (and let me pull some text from my own page here) …
Journals * Ceej's black book * Randomly Ever After * Orange is Holy * Wlofie's Online Journal * Azagthoth's Livejournal * Resilient's Livejournal * Ftrain
I can get the actual HTML?
Journals <ul> <li><a class="external" href="http://snippy.ceejbot.com/wiki/show/start" title="C. J.Silverio">Ceej's black book</a></li> <li><a class="external" href="http://www.asecular.com/ran/" title="TheGus">Randomly Ever After</a></li> <li><a class="external" href="http://www.springdew.com/" title="Spring Dew">Orange is Holy</a></li> <li><a class="external" href="http://wlofie.dyndns.org/diary/" title="Wlofie">Wlofie's Online Journal</a></li> <li><a class="external" href="http://www.livejournal.com/users/azagthoth/" title="Rob Summers">Azagthoth's Livejournal</a></li> <li><a class="external" href="http://www.livejournal.com/users/resilient/">Resilient's Livejournal</a></li> <li><a class="external" href="http://ftrain.com/" title="Paul Ford">Ftrain</a></li> </ul>
The answer appears to be yes (it's in UCS2 format for the record). And the URL?
Well … I'll be … (and this too, is in UCS2 format).
Some quick hacking, and now I have a program that will select the
URL and HTML from the Firefox primary
text selection, format it within a
<BLOCKQUOTE> tag, with
TITLE attributes filled in, and the
<P> tag with the citation information. And it's
easy enough to run said program inside the current editor I use.
- Ceej's black book
- Randomly Ever After
- Orange is Holy
- Wlofie's Online Journal
- Azagthoth's Livejournal
- Resilient's Livejournal
Hypertext editing and the Semantic Web - The Boston Diaries - Captain Napalm
Okay, enough nonsense … this has given me some ideas on an HTML editor …
Update on Tuesday, November 29th, 2011
Because someone asked, I made the source code available for download. There's not much in the way of documentation, but I figure that if you understand what this is doing, then you can probably compile it without issue.