2c0ca19
To: vim-dev@vim.org
2c0ca19
Subject: Patch 7.2.374
2c0ca19
Fcc: outbox
2c0ca19
From: Bram Moolenaar <Bram@moolenaar.net>
2c0ca19
Mime-Version: 1.0
2c0ca19
Content-Type: text/plain; charset=UTF-8
2c0ca19
Content-Transfer-Encoding: 8bit
2c0ca19
------------
2c0ca19
2c0ca19
Patch 7.2.374
2c0ca19
Problem:    Ruby eval() doesn't understand Vim types.
2c0ca19
Solution:   Add the vim_to_ruby() function.  (George Gensure)
2c0ca19
Files:	    src/eval.c, src/if_ruby.c
2c0ca19
2c0ca19
2c0ca19
*** ../vim-7.2.373/src/eval.c	2010-01-19 15:51:29.000000000 +0100
2c0ca19
--- src/eval.c	2010-02-24 15:36:40.000000000 +0100
2c0ca19
***************
2c0ca19
*** 5872,5878 ****
2c0ca19
      return item1 == NULL && item2 == NULL;
2c0ca19
  }
2c0ca19
  
2c0ca19
! #if defined(FEAT_PYTHON) || defined(FEAT_MZSCHEME) || defined(PROTO)
2c0ca19
  /*
2c0ca19
   * Return the dictitem that an entry in a hashtable points to.
2c0ca19
   */
2c0ca19
--- 5872,5879 ----
2c0ca19
      return item1 == NULL && item2 == NULL;
2c0ca19
  }
2c0ca19
  
2c0ca19
! #if defined(FEAT_RUBY) || defined(FEAT_PYTHON) || defined(FEAT_MZSCHEME) \
2c0ca19
! 	|| defined(PROTO)
2c0ca19
  /*
2c0ca19
   * Return the dictitem that an entry in a hashtable points to.
2c0ca19
   */
2c0ca19
*** ../vim-7.2.373/src/if_ruby.c	2010-02-18 15:51:25.000000000 +0100
2c0ca19
--- src/if_ruby.c	2010-02-24 15:45:15.000000000 +0100
2c0ca19
***************
2c0ca19
*** 660,679 ****
2c0ca19
      return Qnil;
2c0ca19
  }
2c0ca19
  
2c0ca19
  static VALUE vim_evaluate(VALUE self UNUSED, VALUE str)
2c0ca19
  {
2c0ca19
  #ifdef FEAT_EVAL
2c0ca19
!     char_u *value = eval_to_string((char_u *)StringValuePtr(str), NULL, TRUE);
2c0ca19
  
2c0ca19
!     if (value != NULL)
2c0ca19
      {
2c0ca19
! 	VALUE val = rb_str_new2((char *)value);
2c0ca19
! 	vim_free(value);
2c0ca19
! 	return val;
2c0ca19
      }
2c0ca19
!     else
2c0ca19
  #endif
2c0ca19
- 	return Qnil;
2c0ca19
  }
2c0ca19
  
2c0ca19
  static VALUE buffer_new(buf_T *buf)
2c0ca19
--- 660,747 ----
2c0ca19
      return Qnil;
2c0ca19
  }
2c0ca19
  
2c0ca19
+ #ifdef FEAT_EVAL
2c0ca19
+ static VALUE vim_to_ruby(typval_T *tv)
2c0ca19
+ {
2c0ca19
+     VALUE result = Qnil;
2c0ca19
+ 
2c0ca19
+     if (tv->v_type == VAR_STRING)
2c0ca19
+     {
2c0ca19
+         result = rb_str_new2((char *)tv->vval.v_string);
2c0ca19
+     }
2c0ca19
+     else if (tv->v_type == VAR_NUMBER)
2c0ca19
+     {
2c0ca19
+         result = INT2NUM(tv->vval.v_number);
2c0ca19
+     }
2c0ca19
+ # ifdef FEAT_FLOAT
2c0ca19
+     else if (tv->v_type == VAR_FLOAT)
2c0ca19
+     {
2c0ca19
+         result = rb_float_new(tv->vval.v_float);
2c0ca19
+     }
2c0ca19
+ # endif
2c0ca19
+     else if (tv->v_type == VAR_LIST)
2c0ca19
+     {
2c0ca19
+         list_T      *list = tv->vval.v_list;
2c0ca19
+         listitem_T  *curr;
2c0ca19
+ 
2c0ca19
+         result = rb_ary_new();
2c0ca19
+ 
2c0ca19
+         if (list != NULL)
2c0ca19
+         {
2c0ca19
+             for (curr = list->lv_first; curr != NULL; curr = curr->li_next)
2c0ca19
+             {
2c0ca19
+                 rb_ary_push(result, vim_to_ruby(&curr->li_tv));
2c0ca19
+             }
2c0ca19
+         }
2c0ca19
+     }
2c0ca19
+     else if (tv->v_type == VAR_DICT)
2c0ca19
+     {
2c0ca19
+         result = rb_hash_new();
2c0ca19
+ 
2c0ca19
+         if (tv->vval.v_dict != NULL)
2c0ca19
+         {
2c0ca19
+             hashtab_T   *ht = &tv->vval.v_dict->dv_hashtab;
2c0ca19
+             long_u      todo = ht->ht_used;
2c0ca19
+             hashitem_T  *hi;
2c0ca19
+             dictitem_T  *di;
2c0ca19
+ 
2c0ca19
+             for (hi = ht->ht_array; todo > 0; ++hi)
2c0ca19
+             {
2c0ca19
+                 if (!HASHITEM_EMPTY(hi))
2c0ca19
+                 {
2c0ca19
+                     --todo;
2c0ca19
+ 
2c0ca19
+                     di = dict_lookup(hi);
2c0ca19
+                     rb_hash_aset(result, rb_str_new2((char *)hi->hi_key),
2c0ca19
+ 						     vim_to_ruby(&di->di_tv));
2c0ca19
+                 }
2c0ca19
+             }
2c0ca19
+         }
2c0ca19
+     } /* else return Qnil; */
2c0ca19
+ 
2c0ca19
+     return result;
2c0ca19
+ }
2c0ca19
+ #endif
2c0ca19
+ 
2c0ca19
  static VALUE vim_evaluate(VALUE self UNUSED, VALUE str)
2c0ca19
  {
2c0ca19
  #ifdef FEAT_EVAL
2c0ca19
!     typval_T    *tv;
2c0ca19
!     VALUE       result;
2c0ca19
  
2c0ca19
!     tv = eval_expr((char_u *)StringValuePtr(str), NULL);
2c0ca19
!     if (tv == NULL)
2c0ca19
      {
2c0ca19
!         return Qnil;
2c0ca19
      }
2c0ca19
!     result = vim_to_ruby(tv);
2c0ca19
! 
2c0ca19
!     free_tv(tv);
2c0ca19
! 
2c0ca19
!     return result;
2c0ca19
! #else
2c0ca19
!     return Qnil;
2c0ca19
  #endif
2c0ca19
  }
2c0ca19
  
2c0ca19
  static VALUE buffer_new(buf_T *buf)
2c0ca19
*** ../vim-7.2.373/src/version.c	2010-02-24 15:25:13.000000000 +0100
2c0ca19
--- src/version.c	2010-02-24 15:46:57.000000000 +0100
2c0ca19
***************
2c0ca19
*** 683,684 ****
2c0ca19
--- 683,686 ----
2c0ca19
  {   /* Add new patch number below this line */
2c0ca19
+ /**/
2c0ca19
+     374,
2c0ca19
  /**/
2c0ca19
2c0ca19
-- 
2c0ca19
ARTHUR: (as the MAN next to him is squashed by a sheep) Knights!  Run away!
2c0ca19
   Midst echoing shouts of "run away" the KNIGHTS retreat to cover with the odd
2c0ca19
   cow or goose hitting them still.  The KNIGHTS crouch down under cover.
2c0ca19
                 "Monty Python and the Holy Grail" PYTHON (MONTY) PICTURES LTD
2c0ca19
2c0ca19
 /// Bram Moolenaar -- Bram@Moolenaar.net -- http://www.Moolenaar.net   \\\
2c0ca19
///        sponsor Vim, vote for features -- http://www.Vim.org/sponsor/ \\\
2c0ca19
\\\        download, build and distribute -- http://www.A-A-P.org        ///
2c0ca19
 \\\            help me help AIDS victims -- http://ICCF-Holland.org    ///