Sunday, June 28, 2009

Sprucing up gedit: Part 2

Hi, welcome back... Yesterday I described what you can do with some gedit plugins. Today, we look at one of the least used, but most powerful features of Gedit: "External Tools". It can be found in the "Tools -> External Tools..." menu.

The external tools option allows you to write a script and execute that script from within gedit, using a few shortcut keys. This script is a shell script, with a few extra variables exposed by gedit.

Without going into the gory details, I will describe how you can make the most of this feature with the least effort... (of course, if you are a little industrious, you can do really cool things with this feature)

So, gedit provides a few default scripts:
1. Build (default Ctrl+F9)
2. Remove Trailing Spaces
3. Run Command

These three should really be quite useful already.

Here is the one most useful builtin variable that you can read: $GEDIT_CURRENT_DOCUMENT_DIR.
This tells you the path to the currently open document in gedit.

If you are used to writing shell scripts, you don't need to read any further, really. Just get creative :) But if you are relatively new/uncomfortable with scripts, here goes...

The shell script that is part of the "Build" command does this:
1. Go to the $GEDIT_CURRENT_DOCUMENT_DIR, and check if there is a makefile there.
2. If yes, execute it.
3. If no, go one level down and try again (until root is reached).

We will now go ahead and modify the build command so it executes the target of the makefile as well...
Assume your executable has the extension ".out". Then, assuming we know which directory it is in, we can write a simple script to execute this file:

cd ${DIR}
OUTFILE=`ls -1 *.out | head -n 1`
exec `echo ./$OUTFILE`


Apologies for the crappy script. With (a lot) more effort, we can figure out what the exact name of the executable created by make is... but that's a topic for more intellectual characters and best not broached by lesser beings such as myself...

For those who want to know what the script does, it goes to the directory where the executable file is present, lists the .out files in the directory, and picks the first one. This first .out file is executed by the 'exec' command.

Note that we no longer need to explicitly exit from this script because the entire instance of the shell has been replaced by the program that we 'exec-ed'. Basically, when we call this External Tool by pressing Ctrl+F9, gedit starts a shell, and runs this script in the shell. When we call 'exec', the shell process is replaced in memory by the program we executed.

Now, if we combine this with the original Build script given in gedit, we get this:


#!/bin/sh

EHOME=`echo $HOME | sed "s/#/\#/"`
DIR=$GEDIT_CURRENT_DOCUMENT_DIR
while test "$DIR" != "/"; do
 for m in GNUmakefile makefile Makefile; do
  if [ -f "${DIR}/${m}" ]; then
   echo "Using ${m} from ${DIR}" | sed "s#$EHOME#~#" > /dev/stderr
   make -C "${DIR}"

    cd ${DIR}
    OUTFILE=`ls -1 *.out | head -n 1`
    exec `echo ./$OUTFILE`


   fi
  done
  DIR=`dirname "${DIR}"`
done
echo "No Makefile found!" > /dev/stderr


The changes we made are highlighted in bold.

Now, whenever you want to run your program, assuming you have a proper makefile ready, just hit Ctrl+F9 and voila! your program (compiles if required and) starts up. You may later want to add cosmetic changes like remapping this to Ctrl+F5 or whatever. To learn more about the External Tools plugin, just go Here

1 comment: