unexist.dev

Changeset 94:ce5357c68ebb

Added calling of config methods
author unexist
date Mon, 30 May 2011 22:54:10 +0200
parents 81f54a9622c4
children e12b10237a77
files ruby/launcher.rb
diffstat 1 files changed, 32 insertions(+), 10 deletions(-) [+]
line wrap: on
line diff
--- a/ruby/launcher.rb	Fri Apr 29 16:35:46 2011 +0200
+++ b/ruby/launcher.rb	Mon May 30 22:54:10 2011 +0200
@@ -14,6 +14,7 @@
 #
 # Examples:
 #
+# :urxvt                - Call methods defined in the config
 # g subtle wm           - Change to browser view and search for 'subtle wm' via Google
 # urxvt @editor         - Open urxvt on view @editor with random tag
 # urxvt @editor #work   - Open urxvt on view @editor with tag #work
@@ -59,7 +60,8 @@
     # Precompile regexps
     RE_COMMAND  = Regexp.new(/^([+\^\*]*[A-Za-z0-9_\-\/'"\s]+)(\s[@#][A-Za-z0-9_-]+)*$/)
     RE_MODES    = Regexp.new(/^([+\^\*]*)([A-Za-z0-9_\-\/'"\s]+)/)
-    RE_SEARCH   = Regexp.new(/^[gs]\s.*/)
+    RE_SEARCH   = Regexp.new(/^[gs]\s*(.*)/)
+    RE_METHOD   = Regexp.new(/^[:]\s*(.*)/)
     RE_URI      = Regexp.new(/^(http|https):\/\/[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,5}(([0-9]{1,5})?\/.*)?$/ix)
     RE_CHROME   = Regexp.new(/chrom[e|ium]|iron/i)
     RE_FIREFOX  = Regexp.new(/navigator/i)
@@ -131,9 +133,15 @@
         @parsed_modes = ""
 
         # Cached data
-        @cached_tags  = Subtlext::Tag.all.map(&:name)
-        @cached_views = Subtlext::View.all.map(&:name)
-        @cached_apps  = {}
+        @cached_tags   = Subtlext::Tag.all.map(&:name)
+        @cached_views  = Subtlext::View.all.map(&:name)
+        @cached_apps   = {}
+
+        # FIXME: Find config instance
+        ObjectSpace.each_object(Subtle::Config) do |c|
+          @cached_sender  = c
+          @cached_methods = c.methods(false).map(&:to_s)
+        end
 
         # Something close to a skiplist
         @@paths.split(":").each do |path|
@@ -229,11 +237,15 @@
 
           info("Goto %s" % [ @candidate.to_s ])
         elsif(RE_SEARCH.match(string))
-          @candidate = URI.parse("http://www.google.com/#q=%s" % [
-            URI.escape(string.gsub(/^[gs]\s/, ''))
-          ])
+          @candidate = URI.parse(
+            "http://www.google.com/#q=%s" % [ URI.escape($1) ]
+          )
 
           info("Goto %s" % [ @candidate.to_s ])
+        elsif(RE_METHOD.match(string))
+          @candidate = $1.to_sym
+
+          info("Call :%s" % [ @candidate ])
         elsif(RE_COMMAND.match(string))
           @candidate    = string
           @parsed_tags  = []
@@ -308,6 +320,9 @@
             when "@"
               lookup = @cached_views
               prefix = "@"
+            when ":"
+              lookup = @cached_methods
+              prefix = ":"
             when "+", "^", "*"
               lookup = @cached_apps[last[@parsed_modes.size].to_sym]
               prefix = @parsed_modes
@@ -321,14 +336,19 @@
             lookup.each do |l|
               guesses << [
                 "%s%s" %[ prefix, l ],
-                Levenshtein::distance(last.gsub(/^[@#]/, ""),
+                Levenshtein::distance(last.gsub(/^[@#:]/, ""),
                   l, 1, 5, 5, @array1, @array2)
               ]
             end
 
             guesses.sort! { |a, b| a[1] <=> b[1] } # Sort by distance
 
-            @candidate = guesses[guess].first
+            @candidate = retval = guesses[guess].first
+
+            # Convert to symbol if methods are guessed
+            @candidate = @candidate.delete(":").to_sym if(":" == prefix)
+
+            retval
           end
         rescue => err
           puts err, err.backtrace
@@ -384,6 +404,8 @@
         # Check if input returns a value
         unless(ret.nil?)
           case @candidate
+            when Symbol #{{{
+              @cached_sender.send(@candidate) # }}}
             when String # {{{
               # Find or create tags
               @parsed_tags.map! do |t|
@@ -466,7 +488,7 @@
         begin
           if(@browser.nil?)
             Subtlext::Client.all.each do |c|
-              case c.instance
+              case c.klass
                 when RE_CHROME
                   @browser = :chrome
                   @view    = c.views.first