Jeb Wilkins

Coding and climbing in Cumbria.

Hacking in Lua

I use an alternative window manager called Awesome - it works a bit differently to a normal Gnome desktop. One feature I miss is the run command in Gnome which takes whatever you throw at it and tries to act accordingly - give it a url and it opens a web browser, give it a path and it opens the file manager, give it a program and in runs the program.

Awesome has a simple run command but it only launches programs. It turns out Awesome is really really configurable with all behavior coded in Lua. A quick hunt through the config file (which is just lua code) revealed

key({ modkey }, "F1",
        function ()
  { prompt = "Run: " },
            awful.util.spawn, awful.completion.bash,
            awful.util.getdir("cache") .. "/history")

awful.util.spawn is the importent part, its used to spawn the chosen program, so I just needed to supply my own spawning program. A half hour of learning at Lua later and I’d got the following function, which i now use instead of awful.util.spawn

function gnomeopen(cmd, sn, screen)
  if os.execute("which " .. cmd) == 0 then
    return awful.util.spawn(cmd, sn, screen)
    return awful.util.spawn("gnome-open " .. cmd, sn, screen)

This uses unixes which to check for whether theres a matching program, then either runs it as before, or passes it through the gnome-open program.