Wednesday, May 07, 2014

Improving Terminal IDE: Adding emacs and command line copy & paste support

Terminal IDE rocks. For those who have yet to install it, it provides a bash command prompt from within Android. This isn't just an academic exercise, bash often provides the most efficient way to manage files (oh, the joy of mv, cp and tar), work with networks (curl and netcat baby!) and generally hack away with your device.

As good as Terminal IDE is, here's two ways to enhance it:

Add emacs

Sure, emacs runs on Android, but it has a nasty habit of segfaulting. I found that if I cranked down my font size it would run. Great, I could either run emacs and not see it, or see it and not have it run. David Meggison provides an brilliant fix: run emacs under Terminal IDE. He provides instructions for doing so. Though, I think they can be abbreviated to:

  • Install the emacs 'app' from Google Play
  • Open up Terminal IDE
  • Copy the emacs executable from the sdcard/emacs directory into your home directory in Terminal IDE (bonus points if you copy it into a local bin directory)
  • Invoke emacs
  • Sit back and be amazed

I've noticed a number of redraw issues, but nothing like the crashes I was seeing using the emacs app itself. It's definitely usable.

Command line copy and paste

Terminal IDE is great and all, but it often feels a bit disconnected from the rest of the system. If I've got some output from curl I want to e-mail, it takes jumping through hoops to get this done. What I really wanted was a quick and easy way to get content into and out of Terminal IDE. The Android clipboard seemed the ideal path. But how to do this?

My solution was to use Tasker (well, duh, right?). I developed two new Tasker profiles:

These are both pretty dang simple. Clipboard: Get watches the magic variable %CLIP (which contains the clipboard contents). When it changes, it automatically pushes the changes to a file living at: Tasker/clipboard/get.txt. Similarly, Clipboard: Set watches for changes to Tasker/clipboard/set.txt, as soon as this file updates, the contents of are stored on the clipboard.

With these Tasker profiles running, I can now set and get the clipboard contents via plain old text files. Which of course, bash loves. From the Terminal IDE side of things, I wrote the following shell script:

#!/data/data/com.spartacusrex.spartacuside/files/system/bin/bash

##
## Work with the system clipboard. Or, at least pretend to.
## Really, this is all powered by Tasker.
##

CLIP_DIR=$HOME/sdcard/Tasker/clipboard

if [ "$1" = "-s" ] ; then
    shift
    if [ -z "$1" ] ; then
 cat > $CLIP_DIR/set.txt
    else
 echo "$@" > $CLIP_DIR/set.txt
    fi
    exit
elif [ "$1" = "-g" ] ; then
    cat $CLIP_DIR/get.txt
    exit
else
    echo "Usage: `basename $0` {-s|-g} [text to copy]"
    exit
fi

With this in place, I can now do:

 # setting the clipboard
 curl -i 'http://www.google.com/' | clip -s   

 # get the clipboard
 clip -g | wc -l

Now, sending off the output of curl is as simple as capturing the output, switching over to the Gmail app, and pasting the results.

Next up: mixing these two solutions. I'm sure it's possible to convince emacs to use these files (get.txt and set.txt) in its kill ring operations.

No comments:

Post a Comment