changeset 3207:d1ecee58305f

Updated #294: Added handler for #on_match
author unexist
date Wed, 23 May 2012 01:41:41 +0200
parents 77e0c21c1426
children fead825416ef
files src/subtle/ruby.c
diffstat 1 files changed, 46 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/src/subtle/ruby.c	Tue May 22 23:28:46 2012 +0200
+++ b/src/subtle/ruby.c	Wed May 23 01:41:41 2012 +0200
@@ -1805,6 +1805,40 @@
   return Qnil;
 } /* }}} */
 
+/* RubyOptionsOnMatch {{{ */
+/*
+ * call-seq: on_match(, blk) -> nil
+ *
+ * Add a tag on match proc
+ *
+ *  tag "test" do
+ *    match "foobar"
+ *
+ *    on_match do |c|
+ *      c.gravity = :foobar
+ *    end
+ *  end
+ */
+
+static VALUE
+RubyOptionsOnMatch(int argc,
+  VALUE *argv,
+  VALUE self)
+{
+  VALUE value = Qnil;
+
+  rb_scan_args(argc, argv, "01", &value);
+
+  if(subtle->flags & SUB_SUBTLE_CHECK) return Qnil; ///< Skip on check
+
+  if(rb_block_given_p()) value = rb_block_proc(); ///< Get proc
+
+  rb_hash_aset(rb_iv_get(self, "@params"),
+    CHAR2SYM("on_match"), value);
+
+  return Qnil;
+} /* }}} */
+
 /* Config */
 
 /* RubyConfigMissing {{{ */
@@ -2059,7 +2093,7 @@
   int flags = 0, screenid = -1;
   unsigned long gravityid = 0;
   XRectangle geom = { 0 };
-  VALUE name = Qnil, match = Qnil, params = Qnil, value = Qnil;
+  VALUE name = Qnil, match = Qnil, params = Qnil, value = Qnil, proc = Qnil;
 
   rb_scan_args(argc, argv, "11", &name, &match);
 
@@ -2153,6 +2187,15 @@
             }
           else if(Qtrue == value) flags |= SUB_CLIENT_MODE_STICK;
         }
+
+      /* Set match proc */
+      if(RTEST(value = rb_hash_lookup(params, CHAR2SYM("on_match"))))
+        {
+          proc   = value;
+          flags |= SUB_TAG_PROC;
+
+          rb_ary_push(shelter, proc); ///< Protect from GC
+        }
     }
 
   /* Check value type */
@@ -2176,6 +2219,7 @@
               t->gravityid = gravityid;
               t->screenid  = screenid;
               t->geom      = geom;
+              t->proc      = proc;
 
               /* Add matcher */
               rargs[0] = (VALUE)t;
@@ -3452,6 +3496,7 @@
   rb_define_method(options, "match",          RubyOptionsMatch,       1);
   rb_define_method(options, "gravity",        RubyOptionsGravity,     1);
   rb_define_method(options, "style",          RubyOptionsStyle,       1);
+  rb_define_method(options, "on_match",       RubyOptionsOnMatch,    -1);
   rb_define_method(options, "method_missing", RubyOptionsDispatcher, -1);
 
   /*