The Lumber Room

"Consign them to dust and damp by way of preserving them"

Archive for December 29th, 2006

Editing TEXTAREAs in an external editor

with 2 comments

For many people today, much more text editing happens in TEXTAREA forms than elsewhere, as observed here. But textareas have many problems — they don’t have all of the features your favourite editor has; it’s easy to lose text permanently (browser crash, or some confusion with undo operations, etc.); can’t save what you type for safekeeping, ….
The solution, of course, is to use your own external editor for entering text into textareas. Typing it somewhere and pasting it into the textarea is clumsy (and what happens when you decide to edit some more?). Fortunately, there are solutions which (promise to) work seamlessly:

  1. The ViewSourceWith firefox extension. This is what I use was using, and it works well. Despite its name (and all the things it can do), I suspect that this extension is primarily used for editing textareas :-)
  2. The It’s all text firefox extension. Haven’t used this myself.
  3. The MozEx firefox extension. Actually, this works only with Firefox 0.7–0.9; try the one at its homepage. I had tried this a while ago, and I vaguely remember there were some problems. It may have been fixed now, though.
  4. The Editus Externus firefox extension. Never heard of it before today. Doesn’t work on Firefox 2.
  5. The EmbeddedEditor extension. Experimental, works with Linux only.
  6. There’s also the Xinha here firefox extension, which starts a particular editor (“Xinha”) each time, but I think it’s pointless to learn one more entirely new editor, with probably its own set of limitations.

To use Emacs with any of these, simply set the external editor to use Emacs as a server, i.e. EmacsClient. (You could also set it to use emacs instead of emacsclient, but that would start a new instance of Emacs each time.)

There’s also an amazingly clumsy clever hack, which might work far better than anything else if you’re on X: a script called external-edit. (The Windows version external.exe is here.) This promises much, much more: being able to call an external editor from anywhere, not just Firefox. It works by getting the ID of the window with focus, simulating keypresses that correspond to those for Select All and Copy (so that the contents of the textarea get pasted into clipboard), creating a file with the contents of the clipboard, invoking your editor on the file and waiting for it to exit, moving file’s contents to clipboard, and generating the keystroke for Paste. Clever. Hack.
I haven’t tried the Windows version of course, but the Linux version is buggy (at least, I couldn’t get it to work directly). After much effort and a long time (that’s because I’m an idiot, not because there was much to change), I think I’ve fixed it. I’ve tested it on {GNOME,KDE}×{Firefox,Konqueror,Opera}×{emacs,emacsclient,gvim -f,gedit,kate}. You can get it here.
Simply set up a keystroke to invoke (say) “external-edit emacsclient”, and no matter where you’re typing something, if you suddenly decide you want to do it in Emacs, just hit the key. Nothing could be better :-)

Written by S

Fri, 2006-12-29 at 02:09:10

Posted in compknow