A while back I commented that I might switch to It's All Text for writing my blog posts. I really like having access to full GNU emacs features and its reliability for editing text. It's All Text (IAT) is an Iceweasel plugin that promised to give me that by actually allowing me to invoke emacs directly on a text box in my browser.
I'm idiosyncratic, though; I don't like to use the X interfaces to GNU emacs or even Lucid emacs. I want my emacs to run in a terminal window. And not just any terminal window—even though I run KDE, gnome-terminal has the best Unicode handling of any terminal I've tried, so I use it instead. Turns out that IAT can do the right thing with a little shell scripting help. Here's how I did it…
The workflow I want is clear. When I click on the IAT button below a text dialog in Iceweasel, I want a gnome-terminal running an emacs instance containing the current dialog text to pop up for me. When I'm done editing, I exit emacs. The terminal disappears, and the saved contents of the emacs buffer appear in the text dialog.
So the obvious thing to do is to install a shell script like this in my bin
#!/bin/sh /usr/bin/gnome-terminal -t "text box" \ -x emacs -nw "$1"
and tell IAT that this script is my editor.
Unfortunately, this script has a couple of issues. First and foremost, it doesn't work. Specifically, it doesn't work if I already have a gnome-terminal running. Turns out that, unlike its cousins xterm or konsole, if a gnome-terminal is already running on an X display, other invocations of gnome-terminal just pop open a window from the old instance rather than creating a new one. Which would be fine, except that the new invocations don't then block waiting for the newly-created window to close before they exit. Symptom? I don't get my editor window.
After about a half-hour of messing around, I grabbed the gnome-terminal source, but failed to find a solution there. I then called Keith Packard, who assured me that a solution does exist in the form of an argument to cause gnome-terminal to create a new instance rather than connecting. Armed with his assurances, I explored the source again, and found the --disable-factory flag. Sigh.
OK, now I get to try this thing out for serious. Next piece of bad news: paragraph formatting. Specifically, browser text boxes generally expect a paragraph to be formatted as essentially a single long line. So when IAT supplies the text to emacs, it's an ugly mess. If I reformat it with appropriate linebreaks, then it's an ugly mess when IAT pastes it back into the textbox.
Fortunately, I have had experience with this phenomenon before, and I have a Nickle program I wrote lying around that does roughly the right thing about the second problem. For the first problem, invoking the UNIX fmt utility is probably sufficient. Thus, I edited the shell script to make it invoke these programs.
The net result looks like this:
#!/bin/sh TMP="`echo \"$1\" | sed 's/\.txt$/-tmp.txt/'`" /usr/bin/fmt -s -w 60 "$1" >"$TMP" && /usr/bin/gnome-terminal --disable-factory \ -t "text box" -x /usr/bin/emacs -nw "$TMP" && /home/bart/bin/mi/tfmt "$TMP" >"$1" && /bin/mv "$TMP" /tmp/"`basename \"$1\"`".bak
Here, tfmt is the Nickle code I mentioned earlier.
Note that at the end I move the edit buffer into /tmp rather than deleting it. This is probably a privacy hole, since I'm not too careful with permissions, but it does let me get back the text quite easily if the browser eats it at some point. Turn it off or modify it if you don't like it.
This script is still problematic. Any formatting introduced in the text box is more-or-less destroyed by the time emacs gets it. Similarly, any formatting done on the emacs side is more-or-less eaten up by the time it gets to the text box. Both of these could be improved somewhat, but at the end of the day there's just a fundamental format mismatch that's unlikely ever to be seamless. That's OK—for the occasional post like this one with a lot of formatting I can still work in the text box directly. For most of my blog posts, though, the paragraph formatting isn't fancy and the round trip works fine.
The script above, as well as the Nickle text formatting code tfmt, are freely available at BartForge . Enjoy. (B)