Mercurial > vim
comparison src/if_ruby.c @ 16103:518f44125207 v8.1.1056
patch 8.1.1056: no eval function for Ruby
commit https://github.com/vim/vim/commit/e99be0e6d28fad96efd2b2be23fa38e7559e80e1
Author: Bram Moolenaar <Bram@vim.org>
Date: Tue Mar 26 22:51:09 2019 +0100
patch 8.1.1056: no eval function for Ruby
Problem: No eval function for Ruby.
Solution: Add rubyeval(). (Ozaki Kiichi, closes https://github.com/vim/vim/issues/4152)
author | Bram Moolenaar <Bram@vim.org> |
---|---|
date | Tue, 26 Mar 2019 23:00:07 +0100 |
parents | e5f82e8b3c06 |
children | cd5c83115ec6 |
comparison
equal
deleted
inserted
replaced
16102:6ce4723ed116 | 16103:518f44125207 |
---|---|
203 | 203 |
204 static int ensure_ruby_initialized(void); | 204 static int ensure_ruby_initialized(void); |
205 static void error_print(int); | 205 static void error_print(int); |
206 static void ruby_io_init(void); | 206 static void ruby_io_init(void); |
207 static void ruby_vim_init(void); | 207 static void ruby_vim_init(void); |
208 static int ruby_convert_to_vim_value(VALUE val, typval_T *rettv); | |
208 | 209 |
209 #if defined(RUBY19_OR_LATER) || defined(RUBY_INIT_STACK) | 210 #if defined(RUBY19_OR_LATER) || defined(RUBY_INIT_STACK) |
210 # if defined(__ia64) && !defined(ruby_init_stack) | 211 # if defined(__ia64) && !defined(ruby_init_stack) |
211 # define ruby_init_stack(addr) ruby_init_stack((addr), rb_ia64_bsp()) | 212 # define ruby_init_stack(addr) ruby_init_stack((addr), rb_ia64_bsp()) |
212 # endif | 213 # endif |
257 # else | 258 # else |
258 # define rb_funcall2 dll_rb_funcall2 | 259 # define rb_funcall2 dll_rb_funcall2 |
259 # endif | 260 # endif |
260 # define rb_global_variable dll_rb_global_variable | 261 # define rb_global_variable dll_rb_global_variable |
261 # define rb_hash_aset dll_rb_hash_aset | 262 # define rb_hash_aset dll_rb_hash_aset |
263 # define rb_hash_foreach dll_rb_hash_foreach | |
262 # define rb_hash_new dll_rb_hash_new | 264 # define rb_hash_new dll_rb_hash_new |
263 # define rb_inspect dll_rb_inspect | 265 # define rb_inspect dll_rb_inspect |
264 # define rb_int2inum dll_rb_int2inum | 266 # define rb_int2inum dll_rb_int2inum |
265 | 267 |
266 // ruby.h may redefine rb_intern to use RUBY_CONST_ID_CACHE(), but that won't | 268 // ruby.h may redefine rb_intern to use RUBY_CONST_ID_CACHE(), but that won't |
273 # define rb_fix2int dll_rb_fix2int | 275 # define rb_fix2int dll_rb_fix2int |
274 # define rb_num2int dll_rb_num2int | 276 # define rb_num2int dll_rb_num2int |
275 # endif | 277 # endif |
276 # define rb_num2uint dll_rb_num2uint | 278 # define rb_num2uint dll_rb_num2uint |
277 # endif | 279 # endif |
280 # define rb_num2dbl dll_rb_num2dbl | |
278 # define rb_lastline_get dll_rb_lastline_get | 281 # define rb_lastline_get dll_rb_lastline_get |
279 # define rb_lastline_set dll_rb_lastline_set | 282 # define rb_lastline_set dll_rb_lastline_set |
280 # define rb_protect dll_rb_protect | 283 # define rb_protect dll_rb_protect |
281 # define rb_load dll_rb_load | 284 # define rb_load dll_rb_load |
282 # ifndef RUBY19_OR_LATER | 285 # ifndef RUBY19_OR_LATER |
407 # else | 410 # else |
408 static VALUE (*dll_rb_funcall2) (VALUE, ID, int, const VALUE*); | 411 static VALUE (*dll_rb_funcall2) (VALUE, ID, int, const VALUE*); |
409 # endif | 412 # endif |
410 static void (*dll_rb_global_variable) (VALUE*); | 413 static void (*dll_rb_global_variable) (VALUE*); |
411 static VALUE (*dll_rb_hash_aset) (VALUE, VALUE, VALUE); | 414 static VALUE (*dll_rb_hash_aset) (VALUE, VALUE, VALUE); |
415 static VALUE (*dll_rb_hash_foreach) (VALUE, int (*)(VALUE, VALUE, VALUE), VALUE); | |
412 static VALUE (*dll_rb_hash_new) (void); | 416 static VALUE (*dll_rb_hash_new) (void); |
413 static VALUE (*dll_rb_inspect) (VALUE); | 417 static VALUE (*dll_rb_inspect) (VALUE); |
414 static VALUE (*dll_rb_int2inum) (long); | 418 static VALUE (*dll_rb_int2inum) (long); |
415 static ID (*dll_rb_intern) (const char*); | 419 static ID (*dll_rb_intern) (const char*); |
416 # if VIM_SIZEOF_INT < VIM_SIZEOF_LONG /* 64 bits only */ | 420 # if VIM_SIZEOF_INT < VIM_SIZEOF_LONG /* 64 bits only */ |
417 static long (*dll_rb_fix2int) (VALUE); | 421 static long (*dll_rb_fix2int) (VALUE); |
418 static long (*dll_rb_num2int) (VALUE); | 422 static long (*dll_rb_num2int) (VALUE); |
419 static unsigned long (*dll_rb_num2uint) (VALUE); | 423 static unsigned long (*dll_rb_num2uint) (VALUE); |
420 # endif | 424 # endif |
425 static double (*dll_rb_num2dbl) (VALUE); | |
421 static VALUE (*dll_rb_lastline_get) (void); | 426 static VALUE (*dll_rb_lastline_get) (void); |
422 static void (*dll_rb_lastline_set) (VALUE); | 427 static void (*dll_rb_lastline_set) (VALUE); |
423 static VALUE (*dll_rb_protect) (VALUE (*)(VALUE), VALUE, int*); | 428 static VALUE (*dll_rb_protect) (VALUE (*)(VALUE), VALUE, int*); |
424 static void (*dll_rb_load) (VALUE, int); | 429 static void (*dll_rb_load) (VALUE, int); |
425 static long (*dll_rb_num2long) (VALUE); | 430 static long (*dll_rb_num2long) (VALUE); |
499 # endif | 504 # endif |
500 # endif | 505 # endif |
501 | 506 |
502 # if defined(RUBY19_OR_LATER) && !defined(PROTO) | 507 # if defined(RUBY19_OR_LATER) && !defined(PROTO) |
503 # if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 22 | 508 # if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 22 |
504 long rb_num2long_stub(VALUE x) | 509 long |
510 rb_num2long_stub(VALUE x) | |
505 # else | 511 # else |
506 SIGNED_VALUE rb_num2long_stub(VALUE x) | 512 SIGNED_VALUE |
513 rb_num2long_stub(VALUE x) | |
507 # endif | 514 # endif |
508 { | 515 { |
509 return dll_rb_num2long(x); | 516 return dll_rb_num2long(x); |
510 } | 517 } |
511 # if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 26 | 518 # if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 26 |
512 VALUE rb_int2big_stub(intptr_t x) | 519 VALUE |
520 rb_int2big_stub(intptr_t x) | |
513 # else | 521 # else |
514 VALUE rb_int2big_stub(SIGNED_VALUE x) | 522 VALUE |
523 rb_int2big_stub(SIGNED_VALUE x) | |
515 # endif | 524 # endif |
516 { | 525 { |
517 return dll_rb_int2big(x); | 526 return dll_rb_int2big(x); |
518 } | 527 } |
519 # if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 19 \ | 528 # if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 19 \ |
520 && VIM_SIZEOF_INT < VIM_SIZEOF_LONG | 529 && VIM_SIZEOF_INT < VIM_SIZEOF_LONG |
521 long rb_fix2int_stub(VALUE x) | 530 long |
531 rb_fix2int_stub(VALUE x) | |
522 { | 532 { |
523 return dll_rb_fix2int(x); | 533 return dll_rb_fix2int(x); |
524 } | 534 } |
525 long rb_num2int_stub(VALUE x) | 535 long |
536 rb_num2int_stub(VALUE x) | |
526 { | 537 { |
527 return dll_rb_num2int(x); | 538 return dll_rb_num2int(x); |
528 } | 539 } |
529 # endif | 540 # endif |
530 # if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 20 | 541 # if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 20 |
531 VALUE | 542 VALUE |
532 rb_float_new_in_heap(double d) | 543 rb_float_new_in_heap(double d) |
533 { | 544 { |
534 return dll_rb_float_new(d); | 545 return dll_rb_float_new(d); |
535 } | 546 } |
536 # if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 22 | 547 # if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 22 |
537 unsigned long rb_num2ulong(VALUE x) | 548 unsigned long |
549 rb_num2ulong(VALUE x) | |
538 # else | 550 # else |
539 VALUE rb_num2ulong(VALUE x) | 551 VALUE |
552 rb_num2ulong(VALUE x) | |
540 # endif | 553 # endif |
541 { | 554 { |
542 return (long)RSHIFT((SIGNED_VALUE)(x),1); | 555 return (long)RSHIFT((SIGNED_VALUE)(x),1); |
543 } | 556 } |
544 # endif | 557 # endif |
545 # endif | 558 # endif |
546 | 559 |
547 /* Do not generate a prototype here, VALUE isn't always defined. */ | 560 /* Do not generate a prototype here, VALUE isn't always defined. */ |
548 # if defined(USE_RGENGC) && USE_RGENGC && !defined(PROTO) | 561 # if defined(USE_RGENGC) && USE_RGENGC && !defined(PROTO) |
549 # if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER == 21 | 562 # if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER == 21 |
550 void rb_gc_writebarrier_unprotect_promoted_stub(VALUE obj) | 563 void |
564 rb_gc_writebarrier_unprotect_promoted_stub(VALUE obj) | |
551 { | 565 { |
552 dll_rb_gc_writebarrier_unprotect_promoted(obj); | 566 dll_rb_gc_writebarrier_unprotect_promoted(obj); |
553 } | 567 } |
554 # else | 568 # else |
555 void rb_gc_writebarrier_unprotect_stub(VALUE obj) | 569 void |
570 rb_gc_writebarrier_unprotect_stub(VALUE obj) | |
556 { | 571 { |
557 dll_rb_gc_writebarrier_unprotect(obj); | 572 dll_rb_gc_writebarrier_unprotect(obj); |
558 } | 573 } |
559 # endif | 574 # endif |
560 # endif | 575 # endif |
561 | 576 |
562 # if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 26 | 577 # if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 26 |
563 void rb_ary_detransient_stub(VALUE x) | 578 void |
579 rb_ary_detransient_stub(VALUE x) | |
564 { | 580 { |
565 dll_rb_ary_detransient(x); | 581 dll_rb_ary_detransient(x); |
566 } | 582 } |
567 # endif | 583 # endif |
568 | 584 |
627 # else | 643 # else |
628 {"rb_funcall2", (RUBY_PROC*)&dll_rb_funcall2}, | 644 {"rb_funcall2", (RUBY_PROC*)&dll_rb_funcall2}, |
629 # endif | 645 # endif |
630 {"rb_global_variable", (RUBY_PROC*)&dll_rb_global_variable}, | 646 {"rb_global_variable", (RUBY_PROC*)&dll_rb_global_variable}, |
631 {"rb_hash_aset", (RUBY_PROC*)&dll_rb_hash_aset}, | 647 {"rb_hash_aset", (RUBY_PROC*)&dll_rb_hash_aset}, |
648 {"rb_hash_foreach", (RUBY_PROC*)&dll_rb_hash_foreach}, | |
632 {"rb_hash_new", (RUBY_PROC*)&dll_rb_hash_new}, | 649 {"rb_hash_new", (RUBY_PROC*)&dll_rb_hash_new}, |
633 {"rb_inspect", (RUBY_PROC*)&dll_rb_inspect}, | 650 {"rb_inspect", (RUBY_PROC*)&dll_rb_inspect}, |
634 {"rb_int2inum", (RUBY_PROC*)&dll_rb_int2inum}, | 651 {"rb_int2inum", (RUBY_PROC*)&dll_rb_int2inum}, |
635 {"rb_intern", (RUBY_PROC*)&dll_rb_intern}, | 652 {"rb_intern", (RUBY_PROC*)&dll_rb_intern}, |
636 # if VIM_SIZEOF_INT < VIM_SIZEOF_LONG /* 64 bits only */ | 653 # if VIM_SIZEOF_INT < VIM_SIZEOF_LONG /* 64 bits only */ |
637 {"rb_fix2int", (RUBY_PROC*)&dll_rb_fix2int}, | 654 {"rb_fix2int", (RUBY_PROC*)&dll_rb_fix2int}, |
638 {"rb_num2int", (RUBY_PROC*)&dll_rb_num2int}, | 655 {"rb_num2int", (RUBY_PROC*)&dll_rb_num2int}, |
639 {"rb_num2uint", (RUBY_PROC*)&dll_rb_num2uint}, | 656 {"rb_num2uint", (RUBY_PROC*)&dll_rb_num2uint}, |
640 # endif | 657 # endif |
658 {"rb_num2dbl", (RUBY_PROC*)&dll_rb_num2dbl}, | |
641 {"rb_lastline_get", (RUBY_PROC*)&dll_rb_lastline_get}, | 659 {"rb_lastline_get", (RUBY_PROC*)&dll_rb_lastline_get}, |
642 {"rb_lastline_set", (RUBY_PROC*)&dll_rb_lastline_set}, | 660 {"rb_lastline_set", (RUBY_PROC*)&dll_rb_lastline_set}, |
643 {"rb_protect", (RUBY_PROC*)&dll_rb_protect}, | 661 {"rb_protect", (RUBY_PROC*)&dll_rb_protect}, |
644 {"rb_load", (RUBY_PROC*)&dll_rb_load}, | 662 {"rb_load", (RUBY_PROC*)&dll_rb_load}, |
645 {"rb_num2long", (RUBY_PROC*)&dll_rb_num2long}, | 663 {"rb_num2long", (RUBY_PROC*)&dll_rb_num2long}, |
787 #ifdef DYNAMIC_RUBY | 805 #ifdef DYNAMIC_RUBY |
788 end_dynamic_ruby(); | 806 end_dynamic_ruby(); |
789 #endif | 807 #endif |
790 } | 808 } |
791 | 809 |
792 void ex_ruby(exarg_T *eap) | 810 void |
811 ex_ruby(exarg_T *eap) | |
793 { | 812 { |
794 int state; | 813 int state; |
795 char *script = NULL; | 814 char *script = NULL; |
796 | 815 |
797 script = (char *)script_get(eap, eap->arg); | 816 script = (char *)script_get(eap, eap->arg); |
858 } | 877 } |
859 #endif | 878 #endif |
860 return rb_eval_string_protect(str, state); | 879 return rb_eval_string_protect(str, state); |
861 } | 880 } |
862 | 881 |
863 void ex_rubydo(exarg_T *eap) | 882 void |
883 ex_rubydo(exarg_T *eap) | |
864 { | 884 { |
865 int state; | 885 int state; |
866 linenr_T i; | 886 linenr_T i; |
867 buf_T *was_curbuf = curbuf; | 887 buf_T *was_curbuf = curbuf; |
868 | 888 |
904 check_cursor(); | 924 check_cursor(); |
905 update_curbuf(NOT_VALID); | 925 update_curbuf(NOT_VALID); |
906 } | 926 } |
907 } | 927 } |
908 | 928 |
909 static VALUE rb_load_wrap(VALUE file_to_load) | 929 static VALUE |
930 rb_load_wrap(VALUE file_to_load) | |
910 { | 931 { |
911 rb_load(file_to_load, 0); | 932 rb_load(file_to_load, 0); |
912 return Qnil; | 933 return Qnil; |
913 } | 934 } |
914 | 935 |
915 void ex_rubyfile(exarg_T *eap) | 936 void |
937 ex_rubyfile(exarg_T *eap) | |
916 { | 938 { |
917 int state; | 939 int state; |
918 | 940 |
919 if (ensure_ruby_initialized()) | 941 if (ensure_ruby_initialized()) |
920 { | 942 { |
923 if (state) | 945 if (state) |
924 error_print(state); | 946 error_print(state); |
925 } | 947 } |
926 } | 948 } |
927 | 949 |
928 void ruby_buffer_free(buf_T *buf) | 950 void |
951 ruby_buffer_free(buf_T *buf) | |
929 { | 952 { |
930 if (buf->b_ruby_ref) | 953 if (buf->b_ruby_ref) |
931 { | 954 { |
932 rb_hash_aset(objtbl, rb_obj_id((VALUE) buf->b_ruby_ref), Qnil); | 955 rb_hash_aset(objtbl, rb_obj_id((VALUE) buf->b_ruby_ref), Qnil); |
933 RDATA(buf->b_ruby_ref)->data = NULL; | 956 RDATA(buf->b_ruby_ref)->data = NULL; |
934 } | 957 } |
935 } | 958 } |
936 | 959 |
937 void ruby_window_free(win_T *win) | 960 void |
961 ruby_window_free(win_T *win) | |
938 { | 962 { |
939 if (win->w_ruby_ref) | 963 if (win->w_ruby_ref) |
940 { | 964 { |
941 rb_hash_aset(objtbl, rb_obj_id((VALUE) win->w_ruby_ref), Qnil); | 965 rb_hash_aset(objtbl, rb_obj_id((VALUE) win->w_ruby_ref), Qnil); |
942 RDATA(win->w_ruby_ref)->data = NULL; | 966 RDATA(win->w_ruby_ref)->data = NULL; |
943 } | 967 } |
944 } | 968 } |
945 | 969 |
946 static int ensure_ruby_initialized(void) | 970 static int |
971 ensure_ruby_initialized(void) | |
947 { | 972 { |
948 if (!ruby_initialized) | 973 if (!ruby_initialized) |
949 { | 974 { |
950 #ifdef DYNAMIC_RUBY | 975 #ifdef DYNAMIC_RUBY |
951 if (ruby_enabled(TRUE)) | 976 if (ruby_enabled(TRUE)) |
991 #endif | 1016 #endif |
992 } | 1017 } |
993 return ruby_initialized; | 1018 return ruby_initialized; |
994 } | 1019 } |
995 | 1020 |
996 static void error_print(int state) | 1021 static void |
1022 error_print(int state) | |
997 { | 1023 { |
998 #if !defined(DYNAMIC_RUBY) && !defined(RUBY19_OR_LATER) | 1024 #if !defined(DYNAMIC_RUBY) && !defined(RUBY19_OR_LATER) |
999 RUBYEXTERN VALUE ruby_errinfo; | 1025 RUBYEXTERN VALUE ruby_errinfo; |
1000 #endif | 1026 #endif |
1001 VALUE error; | 1027 VALUE error; |
1016 #define TAG_FATAL 0x8 | 1042 #define TAG_FATAL 0x8 |
1017 #define TAG_MASK 0xf | 1043 #define TAG_MASK 0xf |
1018 | 1044 |
1019 switch (state) | 1045 switch (state) |
1020 { | 1046 { |
1021 case TAG_RETURN: | 1047 case TAG_RETURN: |
1022 emsg(_("E267: unexpected return")); | 1048 emsg(_("E267: unexpected return")); |
1023 break; | 1049 break; |
1024 case TAG_NEXT: | 1050 case TAG_NEXT: |
1025 emsg(_("E268: unexpected next")); | 1051 emsg(_("E268: unexpected next")); |
1026 break; | 1052 break; |
1027 case TAG_BREAK: | 1053 case TAG_BREAK: |
1028 emsg(_("E269: unexpected break")); | 1054 emsg(_("E269: unexpected break")); |
1029 break; | 1055 break; |
1030 case TAG_REDO: | 1056 case TAG_REDO: |
1031 emsg(_("E270: unexpected redo")); | 1057 emsg(_("E270: unexpected redo")); |
1032 break; | 1058 break; |
1033 case TAG_RETRY: | 1059 case TAG_RETRY: |
1034 emsg(_("E271: retry outside of rescue clause")); | 1060 emsg(_("E271: retry outside of rescue clause")); |
1035 break; | 1061 break; |
1036 case TAG_RAISE: | 1062 case TAG_RAISE: |
1037 case TAG_FATAL: | 1063 case TAG_FATAL: |
1038 #ifdef RUBY19_OR_LATER | 1064 #ifdef RUBY19_OR_LATER |
1039 error = rb_errinfo(); | 1065 error = rb_errinfo(); |
1040 #else | 1066 #else |
1041 error = ruby_errinfo; | 1067 error = ruby_errinfo; |
1042 #endif | 1068 #endif |
1043 eclass = CLASS_OF(error); | 1069 eclass = CLASS_OF(error); |
1044 einfo = rb_obj_as_string(error); | 1070 einfo = rb_obj_as_string(error); |
1045 if (eclass == rb_eRuntimeError && RSTRING_LEN(einfo) == 0) | 1071 if (eclass == rb_eRuntimeError && RSTRING_LEN(einfo) == 0) |
1046 { | 1072 { |
1047 emsg(_("E272: unhandled exception")); | 1073 emsg(_("E272: unhandled exception")); |
1048 } | 1074 } |
1049 else | 1075 else |
1050 { | 1076 { |
1051 VALUE epath; | 1077 VALUE epath; |
1052 char *p; | 1078 char *p; |
1053 | 1079 |
1054 epath = rb_class_path(eclass); | 1080 epath = rb_class_path(eclass); |
1055 vim_snprintf(buff, BUFSIZ, "%s: %s", | 1081 vim_snprintf(buff, BUFSIZ, "%s: %s", |
1056 RSTRING_PTR(epath), RSTRING_PTR(einfo)); | 1082 RSTRING_PTR(epath), RSTRING_PTR(einfo)); |
1057 p = strchr(buff, '\n'); | 1083 p = strchr(buff, '\n'); |
1058 if (p) *p = '\0'; | 1084 if (p) *p = '\0'; |
1085 emsg(buff); | |
1086 } | |
1087 | |
1088 attr = syn_name2attr((char_u *)"Error"); | |
1089 # ifdef RUBY21_OR_LATER | |
1090 bt = rb_funcallv(error, rb_intern("backtrace"), 0, 0); | |
1091 for (i = 0; i < RARRAY_LEN(bt); i++) | |
1092 msg_attr(RSTRING_PTR(RARRAY_AREF(bt, i)), attr); | |
1093 # else | |
1094 bt = rb_funcall2(error, rb_intern("backtrace"), 0, 0); | |
1095 for (i = 0; i < RARRAY_LEN(bt); i++) | |
1096 msg_attr(RSTRING_PTR(RARRAY_PTR(bt)[i]), attr); | |
1097 # endif | |
1098 break; | |
1099 default: | |
1100 vim_snprintf(buff, BUFSIZ, _("E273: unknown longjmp status %d"), state); | |
1059 emsg(buff); | 1101 emsg(buff); |
1060 } | 1102 break; |
1061 | 1103 } |
1062 attr = syn_name2attr((char_u *)"Error"); | 1104 } |
1063 # ifdef RUBY21_OR_LATER | 1105 |
1064 bt = rb_funcallv(error, rb_intern("backtrace"), 0, 0); | 1106 static VALUE |
1065 for (i = 0; i < RARRAY_LEN(bt); i++) | 1107 vim_message(VALUE self UNUSED, VALUE str) |
1066 msg_attr(RSTRING_PTR(RARRAY_AREF(bt, i)), attr); | |
1067 # else | |
1068 bt = rb_funcall2(error, rb_intern("backtrace"), 0, 0); | |
1069 for (i = 0; i < RARRAY_LEN(bt); i++) | |
1070 msg_attr(RSTRING_PTR(RARRAY_PTR(bt)[i]), attr); | |
1071 # endif | |
1072 break; | |
1073 default: | |
1074 vim_snprintf(buff, BUFSIZ, _("E273: unknown longjmp status %d"), state); | |
1075 emsg(buff); | |
1076 break; | |
1077 } | |
1078 } | |
1079 | |
1080 static VALUE vim_message(VALUE self UNUSED, VALUE str) | |
1081 { | 1108 { |
1082 char *buff, *p; | 1109 char *buff, *p; |
1083 | 1110 |
1084 str = rb_obj_as_string(str); | 1111 str = rb_obj_as_string(str); |
1085 if (RSTRING_LEN(str) > 0) | 1112 if (RSTRING_LEN(str) > 0) |
1096 msg(""); | 1123 msg(""); |
1097 } | 1124 } |
1098 return Qnil; | 1125 return Qnil; |
1099 } | 1126 } |
1100 | 1127 |
1101 static VALUE vim_set_option(VALUE self UNUSED, VALUE str) | 1128 static VALUE |
1129 vim_set_option(VALUE self UNUSED, VALUE str) | |
1102 { | 1130 { |
1103 do_set((char_u *)StringValuePtr(str), 0); | 1131 do_set((char_u *)StringValuePtr(str), 0); |
1104 update_screen(NOT_VALID); | 1132 update_screen(NOT_VALID); |
1105 return Qnil; | 1133 return Qnil; |
1106 } | 1134 } |
1107 | 1135 |
1108 static VALUE vim_command(VALUE self UNUSED, VALUE str) | 1136 static VALUE |
1137 vim_command(VALUE self UNUSED, VALUE str) | |
1109 { | 1138 { |
1110 do_cmdline_cmd((char_u *)StringValuePtr(str)); | 1139 do_cmdline_cmd((char_u *)StringValuePtr(str)); |
1111 return Qnil; | 1140 return Qnil; |
1112 } | 1141 } |
1113 | 1142 |
1114 #ifdef FEAT_EVAL | 1143 #ifdef FEAT_EVAL |
1115 static VALUE vim_to_ruby(typval_T *tv) | 1144 static VALUE |
1145 vim_to_ruby(typval_T *tv) | |
1116 { | 1146 { |
1117 VALUE result = Qnil; | 1147 VALUE result = Qnil; |
1118 | 1148 |
1119 if (tv->v_type == VAR_STRING) | 1149 if (tv->v_type == VAR_STRING) |
1120 { | 1150 { |
1186 | 1216 |
1187 return result; | 1217 return result; |
1188 } | 1218 } |
1189 #endif | 1219 #endif |
1190 | 1220 |
1191 static VALUE vim_evaluate(VALUE self UNUSED, VALUE str) | 1221 static VALUE |
1222 vim_evaluate(VALUE self UNUSED, VALUE str) | |
1192 { | 1223 { |
1193 #ifdef FEAT_EVAL | 1224 #ifdef FEAT_EVAL |
1194 typval_T *tv; | 1225 typval_T *tv; |
1195 VALUE result; | 1226 VALUE result; |
1196 | 1227 |
1219 # ifdef RUBY_TYPED_FREE_IMMEDIATELY | 1250 # ifdef RUBY_TYPED_FREE_IMMEDIATELY |
1220 0, | 1251 0, |
1221 # endif | 1252 # endif |
1222 }; | 1253 }; |
1223 | 1254 |
1224 static size_t buffer_dsize(const void *buf UNUSED) | 1255 static size_t |
1256 buffer_dsize(const void *buf UNUSED) | |
1225 { | 1257 { |
1226 return sizeof(buf_T); | 1258 return sizeof(buf_T); |
1227 } | 1259 } |
1228 #endif | 1260 #endif |
1229 | 1261 |
1230 static VALUE buffer_new(buf_T *buf) | 1262 static VALUE |
1263 buffer_new(buf_T *buf) | |
1231 { | 1264 { |
1232 if (buf->b_ruby_ref) | 1265 if (buf->b_ruby_ref) |
1233 { | 1266 { |
1234 return (VALUE) buf->b_ruby_ref; | 1267 return (VALUE) buf->b_ruby_ref; |
1235 } | 1268 } |
1244 rb_hash_aset(objtbl, rb_obj_id(obj), obj); | 1277 rb_hash_aset(objtbl, rb_obj_id(obj), obj); |
1245 return obj; | 1278 return obj; |
1246 } | 1279 } |
1247 } | 1280 } |
1248 | 1281 |
1249 static buf_T *get_buf(VALUE obj) | 1282 static buf_T * |
1283 get_buf(VALUE obj) | |
1250 { | 1284 { |
1251 buf_T *buf; | 1285 buf_T *buf; |
1252 | 1286 |
1253 #ifdef USE_TYPEDDATA | 1287 #ifdef USE_TYPEDDATA |
1254 TypedData_Get_Struct(obj, buf_T, &buffer_type, buf); | 1288 TypedData_Get_Struct(obj, buf_T, &buffer_type, buf); |
1258 if (buf == NULL) | 1292 if (buf == NULL) |
1259 rb_raise(eDeletedBufferError, "attempt to refer to deleted buffer"); | 1293 rb_raise(eDeletedBufferError, "attempt to refer to deleted buffer"); |
1260 return buf; | 1294 return buf; |
1261 } | 1295 } |
1262 | 1296 |
1263 static VALUE vim_blob(VALUE self UNUSED, VALUE str) | 1297 static VALUE |
1298 vim_blob(VALUE self UNUSED, VALUE str) | |
1264 { | 1299 { |
1265 VALUE result = rb_str_new("0z", 2); | 1300 VALUE result = rb_str_new("0z", 2); |
1266 char buf[4]; | 1301 char buf[4]; |
1267 int i; | 1302 int i; |
1268 for (i = 0; i < RSTRING_LEN(str); i++) | 1303 for (i = 0; i < RSTRING_LEN(str); i++) |
1271 rb_str_concat(result, rb_str_new2(buf)); | 1306 rb_str_concat(result, rb_str_new2(buf)); |
1272 } | 1307 } |
1273 return result; | 1308 return result; |
1274 } | 1309 } |
1275 | 1310 |
1276 static VALUE buffer_s_current(void) | 1311 static VALUE |
1312 buffer_s_current(void) | |
1277 { | 1313 { |
1278 return buffer_new(curbuf); | 1314 return buffer_new(curbuf); |
1279 } | 1315 } |
1280 | 1316 |
1281 static VALUE buffer_s_count(void) | 1317 static VALUE |
1318 buffer_s_count(void) | |
1282 { | 1319 { |
1283 buf_T *b; | 1320 buf_T *b; |
1284 int n = 0; | 1321 int n = 0; |
1285 | 1322 |
1286 FOR_ALL_BUFFERS(b) | 1323 FOR_ALL_BUFFERS(b) |
1292 } | 1329 } |
1293 | 1330 |
1294 return INT2NUM(n); | 1331 return INT2NUM(n); |
1295 } | 1332 } |
1296 | 1333 |
1297 static VALUE buffer_s_aref(VALUE self UNUSED, VALUE num) | 1334 static VALUE |
1335 buffer_s_aref(VALUE self UNUSED, VALUE num) | |
1298 { | 1336 { |
1299 buf_T *b; | 1337 buf_T *b; |
1300 int n = NUM2INT(num); | 1338 int n = NUM2INT(num); |
1301 | 1339 |
1302 FOR_ALL_BUFFERS(b) | 1340 FOR_ALL_BUFFERS(b) |
1312 n--; | 1350 n--; |
1313 } | 1351 } |
1314 return Qnil; | 1352 return Qnil; |
1315 } | 1353 } |
1316 | 1354 |
1317 static VALUE buffer_name(VALUE self) | 1355 static VALUE |
1356 buffer_name(VALUE self) | |
1318 { | 1357 { |
1319 buf_T *buf = get_buf(self); | 1358 buf_T *buf = get_buf(self); |
1320 | 1359 |
1321 return buf->b_ffname ? rb_str_new2((char *)buf->b_ffname) : Qnil; | 1360 return buf->b_ffname ? rb_str_new2((char *)buf->b_ffname) : Qnil; |
1322 } | 1361 } |
1323 | 1362 |
1324 static VALUE buffer_number(VALUE self) | 1363 static VALUE |
1364 buffer_number(VALUE self) | |
1325 { | 1365 { |
1326 buf_T *buf = get_buf(self); | 1366 buf_T *buf = get_buf(self); |
1327 | 1367 |
1328 return INT2NUM(buf->b_fnum); | 1368 return INT2NUM(buf->b_fnum); |
1329 } | 1369 } |
1330 | 1370 |
1331 static VALUE buffer_count(VALUE self) | 1371 static VALUE |
1372 buffer_count(VALUE self) | |
1332 { | 1373 { |
1333 buf_T *buf = get_buf(self); | 1374 buf_T *buf = get_buf(self); |
1334 | 1375 |
1335 return INT2NUM(buf->b_ml.ml_line_count); | 1376 return INT2NUM(buf->b_ml.ml_line_count); |
1336 } | 1377 } |
1337 | 1378 |
1338 static VALUE get_buffer_line(buf_T *buf, linenr_T n) | 1379 static VALUE |
1380 get_buffer_line(buf_T *buf, linenr_T n) | |
1339 { | 1381 { |
1340 if (n <= 0 || n > buf->b_ml.ml_line_count) | 1382 if (n <= 0 || n > buf->b_ml.ml_line_count) |
1341 rb_raise(rb_eIndexError, "line number %ld out of range", (long)n); | 1383 rb_raise(rb_eIndexError, "line number %ld out of range", (long)n); |
1342 return vim_str2rb_enc_str((char *)ml_get_buf(buf, n, FALSE)); | 1384 return vim_str2rb_enc_str((char *)ml_get_buf(buf, n, FALSE)); |
1343 } | 1385 } |
1344 | 1386 |
1345 static VALUE buffer_aref(VALUE self, VALUE num) | 1387 static VALUE |
1388 buffer_aref(VALUE self, VALUE num) | |
1346 { | 1389 { |
1347 buf_T *buf = get_buf(self); | 1390 buf_T *buf = get_buf(self); |
1348 | 1391 |
1349 if (buf != NULL) | 1392 if (buf != NULL) |
1350 return get_buffer_line(buf, (linenr_T)NUM2LONG(num)); | 1393 return get_buffer_line(buf, (linenr_T)NUM2LONG(num)); |
1351 return Qnil; /* For stop warning */ | 1394 return Qnil; /* For stop warning */ |
1352 } | 1395 } |
1353 | 1396 |
1354 static VALUE set_buffer_line(buf_T *buf, linenr_T n, VALUE str) | 1397 static VALUE |
1398 set_buffer_line(buf_T *buf, linenr_T n, VALUE str) | |
1355 { | 1399 { |
1356 char *line = StringValuePtr(str); | 1400 char *line = StringValuePtr(str); |
1357 aco_save_T aco; | 1401 aco_save_T aco; |
1358 | 1402 |
1359 if (n > 0 && n <= buf->b_ml.ml_line_count && line != NULL) | 1403 if (n > 0 && n <= buf->b_ml.ml_line_count && line != NULL) |
1381 rb_raise(rb_eIndexError, "line number %ld out of range", (long)n); | 1425 rb_raise(rb_eIndexError, "line number %ld out of range", (long)n); |
1382 } | 1426 } |
1383 return str; | 1427 return str; |
1384 } | 1428 } |
1385 | 1429 |
1386 static VALUE buffer_aset(VALUE self, VALUE num, VALUE str) | 1430 static VALUE |
1431 buffer_aset(VALUE self, VALUE num, VALUE str) | |
1387 { | 1432 { |
1388 buf_T *buf = get_buf(self); | 1433 buf_T *buf = get_buf(self); |
1389 | 1434 |
1390 if (buf != NULL) | 1435 if (buf != NULL) |
1391 return set_buffer_line(buf, (linenr_T)NUM2LONG(num), str); | 1436 return set_buffer_line(buf, (linenr_T)NUM2LONG(num), str); |
1392 return str; | 1437 return str; |
1393 } | 1438 } |
1394 | 1439 |
1395 static VALUE buffer_delete(VALUE self, VALUE num) | 1440 static VALUE |
1441 buffer_delete(VALUE self, VALUE num) | |
1396 { | 1442 { |
1397 buf_T *buf = get_buf(self); | 1443 buf_T *buf = get_buf(self); |
1398 long n = NUM2LONG(num); | 1444 long n = NUM2LONG(num); |
1399 aco_save_T aco; | 1445 aco_save_T aco; |
1400 | 1446 |
1425 rb_raise(rb_eIndexError, "line number %ld out of range", n); | 1471 rb_raise(rb_eIndexError, "line number %ld out of range", n); |
1426 } | 1472 } |
1427 return Qnil; | 1473 return Qnil; |
1428 } | 1474 } |
1429 | 1475 |
1430 static VALUE buffer_append(VALUE self, VALUE num, VALUE str) | 1476 static VALUE |
1477 buffer_append(VALUE self, VALUE num, VALUE str) | |
1431 { | 1478 { |
1432 buf_T *buf = get_buf(self); | 1479 buf_T *buf = get_buf(self); |
1433 char *line = StringValuePtr(str); | 1480 char *line = StringValuePtr(str); |
1434 long n = NUM2LONG(num); | 1481 long n = NUM2LONG(num); |
1435 aco_save_T aco; | 1482 aco_save_T aco; |
1477 # ifdef RUBY_TYPED_FREE_IMMEDIATELY | 1524 # ifdef RUBY_TYPED_FREE_IMMEDIATELY |
1478 0, | 1525 0, |
1479 # endif | 1526 # endif |
1480 }; | 1527 }; |
1481 | 1528 |
1482 static size_t window_dsize(const void *win UNUSED) | 1529 static size_t |
1530 window_dsize(const void *win UNUSED) | |
1483 { | 1531 { |
1484 return sizeof(win_T); | 1532 return sizeof(win_T); |
1485 } | 1533 } |
1486 #endif | 1534 #endif |
1487 | 1535 |
1488 static VALUE window_new(win_T *win) | 1536 static VALUE |
1537 window_new(win_T *win) | |
1489 { | 1538 { |
1490 if (win->w_ruby_ref) | 1539 if (win->w_ruby_ref) |
1491 { | 1540 { |
1492 return (VALUE) win->w_ruby_ref; | 1541 return (VALUE) win->w_ruby_ref; |
1493 } | 1542 } |
1502 rb_hash_aset(objtbl, rb_obj_id(obj), obj); | 1551 rb_hash_aset(objtbl, rb_obj_id(obj), obj); |
1503 return obj; | 1552 return obj; |
1504 } | 1553 } |
1505 } | 1554 } |
1506 | 1555 |
1507 static win_T *get_win(VALUE obj) | 1556 static win_T * |
1557 get_win(VALUE obj) | |
1508 { | 1558 { |
1509 win_T *win; | 1559 win_T *win; |
1510 | 1560 |
1511 #ifdef USE_TYPEDDATA | 1561 #ifdef USE_TYPEDDATA |
1512 TypedData_Get_Struct(obj, win_T, &window_type, win); | 1562 TypedData_Get_Struct(obj, win_T, &window_type, win); |
1516 if (win == NULL) | 1566 if (win == NULL) |
1517 rb_raise(eDeletedWindowError, "attempt to refer to deleted window"); | 1567 rb_raise(eDeletedWindowError, "attempt to refer to deleted window"); |
1518 return win; | 1568 return win; |
1519 } | 1569 } |
1520 | 1570 |
1521 static VALUE window_s_current(void) | 1571 static VALUE |
1572 window_s_current(void) | |
1522 { | 1573 { |
1523 return window_new(curwin); | 1574 return window_new(curwin); |
1524 } | 1575 } |
1525 | 1576 |
1526 /* | 1577 /* |
1527 * Added line manipulation functions | 1578 * Added line manipulation functions |
1528 * SegPhault - 03/07/05 | 1579 * SegPhault - 03/07/05 |
1529 */ | 1580 */ |
1530 static VALUE line_s_current(void) | 1581 static VALUE |
1582 line_s_current(void) | |
1531 { | 1583 { |
1532 return get_buffer_line(curbuf, curwin->w_cursor.lnum); | 1584 return get_buffer_line(curbuf, curwin->w_cursor.lnum); |
1533 } | 1585 } |
1534 | 1586 |
1535 static VALUE set_current_line(VALUE self UNUSED, VALUE str) | 1587 static VALUE |
1588 set_current_line(VALUE self UNUSED, VALUE str) | |
1536 { | 1589 { |
1537 return set_buffer_line(curbuf, curwin->w_cursor.lnum, str); | 1590 return set_buffer_line(curbuf, curwin->w_cursor.lnum, str); |
1538 } | 1591 } |
1539 | 1592 |
1540 static VALUE current_line_number(void) | 1593 static VALUE |
1594 current_line_number(void) | |
1541 { | 1595 { |
1542 return INT2FIX((int)curwin->w_cursor.lnum); | 1596 return INT2FIX((int)curwin->w_cursor.lnum); |
1543 } | 1597 } |
1544 | 1598 |
1545 | 1599 static VALUE |
1546 | 1600 window_s_count(void) |
1547 static VALUE window_s_count(void) | |
1548 { | 1601 { |
1549 win_T *w; | 1602 win_T *w; |
1550 int n = 0; | 1603 int n = 0; |
1551 | 1604 |
1552 FOR_ALL_WINDOWS(w) | 1605 FOR_ALL_WINDOWS(w) |
1553 n++; | 1606 n++; |
1554 return INT2NUM(n); | 1607 return INT2NUM(n); |
1555 } | 1608 } |
1556 | 1609 |
1557 static VALUE window_s_aref(VALUE self UNUSED, VALUE num) | 1610 static VALUE |
1611 window_s_aref(VALUE self UNUSED, VALUE num) | |
1558 { | 1612 { |
1559 win_T *w; | 1613 win_T *w; |
1560 int n = NUM2INT(num); | 1614 int n = NUM2INT(num); |
1561 | 1615 |
1562 for (w = firstwin; w != NULL; w = w->w_next, --n) | 1616 for (w = firstwin; w != NULL; w = w->w_next, --n) |
1563 if (n == 0) | 1617 if (n == 0) |
1564 return window_new(w); | 1618 return window_new(w); |
1565 return Qnil; | 1619 return Qnil; |
1566 } | 1620 } |
1567 | 1621 |
1568 static VALUE window_buffer(VALUE self) | 1622 static VALUE |
1623 window_buffer(VALUE self) | |
1569 { | 1624 { |
1570 win_T *win = get_win(self); | 1625 win_T *win = get_win(self); |
1571 | 1626 |
1572 return buffer_new(win->w_buffer); | 1627 return buffer_new(win->w_buffer); |
1573 } | 1628 } |
1574 | 1629 |
1575 static VALUE window_height(VALUE self) | 1630 static VALUE |
1631 window_height(VALUE self) | |
1576 { | 1632 { |
1577 win_T *win = get_win(self); | 1633 win_T *win = get_win(self); |
1578 | 1634 |
1579 return INT2NUM(win->w_height); | 1635 return INT2NUM(win->w_height); |
1580 } | 1636 } |
1581 | 1637 |
1582 static VALUE window_set_height(VALUE self, VALUE height) | 1638 static VALUE |
1639 window_set_height(VALUE self, VALUE height) | |
1583 { | 1640 { |
1584 win_T *win = get_win(self); | 1641 win_T *win = get_win(self); |
1585 win_T *savewin = curwin; | 1642 win_T *savewin = curwin; |
1586 | 1643 |
1587 curwin = win; | 1644 curwin = win; |
1588 win_setheight(NUM2INT(height)); | 1645 win_setheight(NUM2INT(height)); |
1589 curwin = savewin; | 1646 curwin = savewin; |
1590 return height; | 1647 return height; |
1591 } | 1648 } |
1592 | 1649 |
1593 static VALUE window_width(VALUE self UNUSED) | 1650 static VALUE |
1651 window_width(VALUE self UNUSED) | |
1594 { | 1652 { |
1595 return INT2NUM(get_win(self)->w_width); | 1653 return INT2NUM(get_win(self)->w_width); |
1596 } | 1654 } |
1597 | 1655 |
1598 static VALUE window_set_width(VALUE self UNUSED, VALUE width) | 1656 static VALUE |
1657 window_set_width(VALUE self UNUSED, VALUE width) | |
1599 { | 1658 { |
1600 win_T *win = get_win(self); | 1659 win_T *win = get_win(self); |
1601 win_T *savewin = curwin; | 1660 win_T *savewin = curwin; |
1602 | 1661 |
1603 curwin = win; | 1662 curwin = win; |
1604 win_setwidth(NUM2INT(width)); | 1663 win_setwidth(NUM2INT(width)); |
1605 curwin = savewin; | 1664 curwin = savewin; |
1606 return width; | 1665 return width; |
1607 } | 1666 } |
1608 | 1667 |
1609 static VALUE window_cursor(VALUE self) | 1668 static VALUE |
1669 window_cursor(VALUE self) | |
1610 { | 1670 { |
1611 win_T *win = get_win(self); | 1671 win_T *win = get_win(self); |
1612 | 1672 |
1613 return rb_assoc_new(INT2NUM(win->w_cursor.lnum), INT2NUM(win->w_cursor.col)); | 1673 return rb_assoc_new(INT2NUM(win->w_cursor.lnum), INT2NUM(win->w_cursor.col)); |
1614 } | 1674 } |
1615 | 1675 |
1616 static VALUE window_set_cursor(VALUE self, VALUE pos) | 1676 static VALUE |
1677 window_set_cursor(VALUE self, VALUE pos) | |
1617 { | 1678 { |
1618 VALUE lnum, col; | 1679 VALUE lnum, col; |
1619 win_T *win = get_win(self); | 1680 win_T *win = get_win(self); |
1620 | 1681 |
1621 Check_Type(pos, T_ARRAY); | 1682 Check_Type(pos, T_ARRAY); |
1629 check_cursor(); /* put cursor on an existing line */ | 1690 check_cursor(); /* put cursor on an existing line */ |
1630 update_screen(NOT_VALID); | 1691 update_screen(NOT_VALID); |
1631 return Qnil; | 1692 return Qnil; |
1632 } | 1693 } |
1633 | 1694 |
1634 static VALUE f_nop(VALUE self UNUSED) | 1695 static VALUE |
1696 f_nop(VALUE self UNUSED) | |
1635 { | 1697 { |
1636 return Qnil; | 1698 return Qnil; |
1637 } | 1699 } |
1638 | 1700 |
1639 static VALUE f_p(int argc, VALUE *argv, VALUE self UNUSED) | 1701 static VALUE |
1702 f_p(int argc, VALUE *argv, VALUE self UNUSED) | |
1640 { | 1703 { |
1641 int i; | 1704 int i; |
1642 VALUE str = rb_str_new("", 0); | 1705 VALUE str = rb_str_new("", 0); |
1643 VALUE ret = Qnil; | 1706 VALUE ret = Qnil; |
1644 | 1707 |
1654 else if (argc > 1) | 1717 else if (argc > 1) |
1655 ret = rb_ary_new4(argc, argv); | 1718 ret = rb_ary_new4(argc, argv); |
1656 return ret; | 1719 return ret; |
1657 } | 1720 } |
1658 | 1721 |
1659 static void ruby_io_init(void) | 1722 static void |
1723 ruby_io_init(void) | |
1660 { | 1724 { |
1661 #ifndef DYNAMIC_RUBY | 1725 #ifndef DYNAMIC_RUBY |
1662 RUBYEXTERN VALUE rb_stdout; | 1726 RUBYEXTERN VALUE rb_stdout; |
1663 RUBYEXTERN VALUE rb_stderr; | 1727 RUBYEXTERN VALUE rb_stderr; |
1664 #endif | 1728 #endif |
1670 rb_define_singleton_method(rb_stderr, "write", vim_message, 1); | 1734 rb_define_singleton_method(rb_stderr, "write", vim_message, 1); |
1671 rb_define_singleton_method(rb_stderr, "flush", f_nop, 0); | 1735 rb_define_singleton_method(rb_stderr, "flush", f_nop, 0); |
1672 rb_define_global_function("p", f_p, -1); | 1736 rb_define_global_function("p", f_p, -1); |
1673 } | 1737 } |
1674 | 1738 |
1675 static void ruby_vim_init(void) | 1739 static void |
1740 ruby_vim_init(void) | |
1676 { | 1741 { |
1677 objtbl = rb_hash_new(); | 1742 objtbl = rb_hash_new(); |
1678 rb_global_variable(&objtbl); | 1743 rb_global_variable(&objtbl); |
1679 | 1744 |
1680 /* The Vim module used to be called "VIM", but "Vim" is better. Make an | 1745 /* The Vim module used to be called "VIM", but "Vim" is better. Make an |
1734 | 1799 |
1735 rb_define_virtual_variable("$curbuf", buffer_s_current, 0); | 1800 rb_define_virtual_variable("$curbuf", buffer_s_current, 0); |
1736 rb_define_virtual_variable("$curwin", window_s_current, 0); | 1801 rb_define_virtual_variable("$curwin", window_s_current, 0); |
1737 } | 1802 } |
1738 | 1803 |
1739 void vim_ruby_init(void *stack_start) | 1804 void |
1805 vim_ruby_init(void *stack_start) | |
1740 { | 1806 { |
1741 /* should get machine stack start address early in main function */ | 1807 /* should get machine stack start address early in main function */ |
1742 ruby_stack_start = stack_start; | 1808 ruby_stack_start = stack_start; |
1743 } | 1809 } |
1810 | |
1811 static int | |
1812 convert_hash2dict(VALUE key, VALUE val, VALUE arg) | |
1813 { | |
1814 dict_T *d = (dict_T *)arg; | |
1815 dictitem_T *di; | |
1816 | |
1817 di = dictitem_alloc((char_u *)RSTRING_PTR(RSTRING(rb_obj_as_string(key)))); | |
1818 if (di == NULL || ruby_convert_to_vim_value(val, &di->di_tv) != OK | |
1819 || dict_add(d, di) != OK) | |
1820 { | |
1821 d->dv_hashtab.ht_error = TRUE; | |
1822 return ST_STOP; | |
1823 } | |
1824 return ST_CONTINUE; | |
1825 } | |
1826 | |
1827 static int | |
1828 ruby_convert_to_vim_value(VALUE val, typval_T *rettv) | |
1829 { | |
1830 switch (TYPE(val)) | |
1831 { | |
1832 case T_NIL: | |
1833 rettv->v_type = VAR_SPECIAL; | |
1834 rettv->vval.v_number = VVAL_NULL; | |
1835 break; | |
1836 case T_TRUE: | |
1837 rettv->v_type = VAR_SPECIAL; | |
1838 rettv->vval.v_number = VVAL_TRUE; | |
1839 break; | |
1840 case T_FALSE: | |
1841 rettv->v_type = VAR_SPECIAL; | |
1842 rettv->vval.v_number = VVAL_FALSE; | |
1843 break; | |
1844 case T_BIGNUM: | |
1845 case T_FIXNUM: | |
1846 rettv->v_type = VAR_NUMBER; | |
1847 rettv->vval.v_number = (varnumber_T)NUM2LONG(val); | |
1848 break; | |
1849 #ifdef FEAT_FLOAT | |
1850 case T_FLOAT: | |
1851 rettv->v_type = VAR_FLOAT; | |
1852 rettv->vval.v_float = (float_T)NUM2DBL(val); | |
1853 break; | |
1854 #endif | |
1855 default: | |
1856 val = rb_obj_as_string(val); | |
1857 // FALLTHROUGH | |
1858 case T_STRING: | |
1859 { | |
1860 VALUE str = (VALUE)RSTRING(val); | |
1861 | |
1862 rettv->v_type = VAR_STRING; | |
1863 rettv->vval.v_string = vim_strnsave((char_u *)RSTRING_PTR(str), | |
1864 (int)RSTRING_LEN(str)); | |
1865 } | |
1866 break; | |
1867 case T_ARRAY: | |
1868 { | |
1869 list_T *l; | |
1870 long i; | |
1871 typval_T v; | |
1872 | |
1873 l = list_alloc(); | |
1874 if (l == NULL) | |
1875 return FAIL; | |
1876 | |
1877 for (i = 0; i < RARRAY_LEN(val); ++i) | |
1878 { | |
1879 if (ruby_convert_to_vim_value((VALUE)RARRAY_PTR(val)[i], | |
1880 &v) != OK) | |
1881 { | |
1882 list_unref(l); | |
1883 return FAIL; | |
1884 } | |
1885 list_append_tv(l, &v); | |
1886 clear_tv(&v); | |
1887 } | |
1888 | |
1889 rettv->v_type = VAR_LIST; | |
1890 rettv->vval.v_list = l; | |
1891 ++l->lv_refcount; | |
1892 } | |
1893 break; | |
1894 case T_HASH: | |
1895 { | |
1896 dict_T *d; | |
1897 | |
1898 d = dict_alloc(); | |
1899 if (d == NULL) | |
1900 return FAIL; | |
1901 | |
1902 rb_hash_foreach(val, convert_hash2dict, (VALUE)d); | |
1903 if (d->dv_hashtab.ht_error) | |
1904 { | |
1905 dict_unref(d); | |
1906 return FAIL; | |
1907 } | |
1908 | |
1909 rettv->v_type = VAR_DICT; | |
1910 rettv->vval.v_dict = d; | |
1911 ++d->dv_refcount; | |
1912 } | |
1913 break; | |
1914 } | |
1915 return OK; | |
1916 } | |
1917 | |
1918 void | |
1919 do_rubyeval(char_u *str, typval_T *rettv) | |
1920 { | |
1921 int retval = FAIL; | |
1922 | |
1923 if (ensure_ruby_initialized()) | |
1924 { | |
1925 int state; | |
1926 VALUE obj; | |
1927 | |
1928 obj = rb_eval_string_protect((const char *)str, &state); | |
1929 if (state) | |
1930 error_print(state); | |
1931 else | |
1932 retval = ruby_convert_to_vim_value(obj, rettv); | |
1933 } | |
1934 if (retval == FAIL) | |
1935 { | |
1936 rettv->v_type = VAR_NUMBER; | |
1937 rettv->vval.v_number = 0; | |
1938 } | |
1939 } |