Friday, March 13, 2009

Reinventing a Wheel: Launching Applications

Hitting ALT-F2 in GNOME brings up an application launcher dialog. You start typing a command, and the list of matching applications is displayed, narrowed down as characters are typed. The arrow keys or mouse can also be used to select one of the applications from the list. Most of the time it beats opening a terminal and typing the command, or searching for the application through the menus.

A similar functionality is provided by default in awesome (version 2.3), with MOD4-F2 (MOD4 is bound to the Windows key on PC keyboards) - awesome-menu is launched, replacing the statusbar at the top of the screen with an "Execute:" prompt, together with a list of available executable files. This list is narrowed down as you type, and the arrow keys may also be used to navigate the list.

So far so good, but, on my oh-so-slow laptop, it takes quite a while for the menu to come up. I found the following entry for MOD4-F2 in the awesome configuration file ~/.awesomerc:

key
{
modkey = {"Mod4"}
key = "F2"
command = "spawn"
arg = "find /usr/bin -type f -executable ! -empty | awesome-menu -e 'exec ' Execute:"
}

The problem here is that /usr/bin is searched for executable files every time MOD4-F2 is pressed. This takes precious time.

I recalled that wmii had a similar functionality which seemed to work much faster somehow. After a short search I found that the trick was pretty simple: during startup wmiirc caches the list of executable files in ~/.proglist. So I added the following lines to my ~/.xprofile:

ls -lL $(echo $PATH | sed 'y/:/ /') 2>/dev/null \
| awk '$1 ~ /^[^d].*x/ && NF > 2 { print $NF }' \
| sort -u > ~/.proglist

and modified ~/.awesomerc:

key
{
modkey = {"Mod4"}
key = "F2"
command = "spawn"
arg = "cat ~/.proglist | awesome-menu -e 'exec ' Execute:"
}

Apart for speed this has the advantage of finding all accessible executables in the path, and not just those in /usr/bin.

It has the disadvantage of not being dynamic - e.g. if you install a new package, the new executables will not be shown with MOD4-F2, until ~/.proglist is updated (either manually or by restarting X).

No comments:

Post a Comment