--- /dev/null
+#!/usr/bin/env ruby
+
+#-------------------------------------------------------------------------------
+# Function Definitions
+#-------------------------------------------------------------------------------
+def expand(str)
+ str
+end
+
+def rule(&block)
+ begin
+ block.call()
+ exit 0
+ rescue
+ end
+end
+
+def matches(var, regex)
+ if not Regexp.new(expand(regex)) =~ ENV[var] then
+ raise "match failed"
+ end
+end
+
+def is(var, val)
+ if not ENV[var] == expand(val) then
+ raise "comparison failed"
+ end
+end
+
+def is_set(var)
+ if not ENV[var] then
+ raise "variable not set"
+ end
+end
+
+def is_dir(path)
+ if not File.directory?(expand(path)) then
+ raise "not a directory"
+ end
+end
+
+def is_file(path)
+ if not File.file?(expand(path)) then
+ raise "not a file"
+ end
+end
+
+def set(var, val)
+ ENV[var] = val
+end
+
+def unset(var)
+ ENV[var] = nil
+end
+
+def exec(cmd)
+ if not system(cmd) then
+ raise "command failed"
+ end
+end
+
+def launch(cmd)
+ spawn(expand(cmd))
+end
+
+def import(path)
+ if File.file?(expand(path)) then
+ load(path)
+ end
+end
+
+#-------------------------------------------------------------------------------
+# Builtin Rule Definitions
+#-------------------------------------------------------------------------------
+ENV["data"] = ARGV[0] # store the data
+
+import("./plumbing")
+import("$HOME/lib/plumbing")
+
+# Look up .c or .h files in Code/
+rule do
+ is_set "EDITOR"
+ matches "data", "\\.[ch]$"
+ is_dir "Code"
+ exec "[[ $$(find Code -type f -name '*$data') ]]"
+ launch "find Code -type f -name '*$data' | xargs -r $EDITOR"
+end
+
+# Match URLS and open them with the browser
+rule do
+ is_set "BROWSER"
+ matches "data", "^(https?|ftp)://.*"
+ launch "$BROWSER $0"
+end
+
+# Open files with addresses in the editor
+rule do
+ is_set "EDITOR"
+ matches "data", "^([^:]+):([0-9]+)"
+ is_file "$1"
+ launch "tctl $0"
+end
+
+# If it's an existing text file, open it with editor
+rule do
+ is_set "EDITOR"
+ is_file "$data"
+ exec "file --mime '$file' | grep -q 'text/'"
+ launch "$EDITOR $file"
+end
+
+# Look it up in ctags database
+rule do
+ is_set "EDITOR"
+ is_file "tags"
+ exec "grep -q '^$data\\s\\+' tags"
+ launch "picktag fetch tags $data | xargs -r tide"
+end
+
+# If it's an existing directory, open it with system default
+rule do
+ is_dir "$data"
+ launch "open $data"
+end
+
+exit 1 #if we made it here then no rules matched
+
+