1 /* vi:set ts=8 sts=4 sw=4 noet: 2 * 3 * VIM - Vi IMproved by Bram Moolenaar 4 * 5 * Ruby interface by Shugo Maeda 6 * with improvements by SegPhault (Ryan Paul) 7 * with improvements by Jon Maken 8 * 9 * Do ":help uganda" in Vim to read copying and usage conditions. 10 * Do ":help credits" in Vim to see a list of people who contributed. 11 * See README.txt for an overview of the Vim source code. 12 */ 13 14 #include "protodef.h" 15 #ifdef HAVE_CONFIG_H 16 # include "auto/config.h" 17 #endif 18 19 #include <stdio.h> 20 #include <string.h> 21 22 #ifdef _WIN32 23 # if !defined(DYNAMIC_RUBY) || (RUBY_VERSION < 18) 24 # define NT 25 # endif 26 # ifndef DYNAMIC_RUBY 27 # define IMPORT // For static dll usage __declspec(dllimport) 28 # define RUBYEXTERN __declspec(dllimport) 29 # endif 30 #endif 31 #ifndef RUBYEXTERN 32 # define RUBYEXTERN extern 33 #endif 34 35 #if defined(DYNAMIC_RUBY) && RUBY_VERSION >= 24 36 # define USE_RUBY_INTEGER 37 #endif 38 39 #ifdef DYNAMIC_RUBY 40 /* 41 * This is tricky. In ruby.h there is (inline) function rb_class_of() 42 * definition. This function use these variables. But we want function to 43 * use dll_* variables. 44 */ 45 # define rb_cFalseClass (*dll_rb_cFalseClass) 46 # define rb_cFixnum (*dll_rb_cFixnum) 47 # if defined(USE_RUBY_INTEGER) 48 # define rb_cInteger (*dll_rb_cInteger) 49 # endif 50 # if RUBY_VERSION >= 20 51 # define rb_cFloat (*dll_rb_cFloat) 52 # endif 53 # define rb_cNilClass (*dll_rb_cNilClass) 54 # define rb_cString (*dll_rb_cString) 55 # define rb_cSymbol (*dll_rb_cSymbol) 56 # define rb_cTrueClass (*dll_rb_cTrueClass) 57 # if RUBY_VERSION >= 18 58 /* 59 * On ver 1.8, all Ruby functions are exported with "__declspec(dllimport)" 60 * in ruby.h. But it causes trouble for these variables, because it is 61 * defined in this file. When defined this RUBY_EXPORT it modified to 62 * "extern" and be able to avoid this problem. 63 */ 64 # define RUBY_EXPORT 65 # endif 66 67 #endif // ifdef DYNAMIC_RUBY 68 69 // suggested by Ariya Mizutani 70 #if (_MSC_VER == 1200) 71 # undef _WIN32_WINNT 72 #endif 73 74 #if defined(DYNAMIC_RUBY) && RUBY_VERSION >= 19 75 // Ruby 1.9 defines a number of static functions which use rb_num2long and 76 // rb_int2big 77 # define rb_num2long rb_num2long_stub 78 # define rb_int2big rb_int2big_stub 79 #endif 80 81 #if defined(DYNAMIC_RUBY) && RUBY_VERSION >= 19 \ 82 && VIM_SIZEOF_INT < VIM_SIZEOF_LONG 83 // Ruby 1.9 defines a number of static functions which use rb_fix2int and 84 // rb_num2int if VIM_SIZEOF_INT < VIM_SIZEOF_LONG (64bit) 85 # define rb_fix2int rb_fix2int_stub 86 # define rb_num2int rb_num2int_stub 87 #endif 88 89 #if defined(DYNAMIC_RUBY) && RUBY_VERSION == 21 90 // Ruby 2.1 adds new GC called RGenGC and RARRAY_PTR uses 91 // rb_gc_writebarrier_unprotect_promoted if USE_RGENGC 92 # define rb_gc_writebarrier_unprotect_promoted rb_gc_writebarrier_unprotect_promoted_stub 93 #endif 94 #if defined(DYNAMIC_RUBY) && RUBY_VERSION >= 22 95 # define rb_gc_writebarrier_unprotect rb_gc_writebarrier_unprotect_stub 96 #endif 97 98 #if defined(DYNAMIC_RUBY) && RUBY_VERSION >= 26 99 # define rb_ary_detransient rb_ary_detransient_stub 100 #endif 101 102 #include <ruby.h> 103 #if RUBY_VERSION >= 19 104 # include <ruby/encoding.h> 105 #endif 106 #if RUBY_VERSION <= 18 107 # include <st.h> // for ST_STOP and ST_CONTINUE 108 #endif 109 110 #undef off_t // ruby defines off_t as _int64, Mingw uses long 111 #undef EXTERN 112 #undef _ 113 114 // T_DATA defined both by Ruby and Mac header files, hack around it... 115 #if defined(MACOS_X) 116 # define __OPENTRANSPORT__ 117 # define __OPENTRANSPORTPROTOCOL__ 118 # define __OPENTRANSPORTPROVIDERS__ 119 #endif 120 121 /* 122 * The TypedData_XXX macro family can be used since Ruby 1.9.2 but 123 * rb_data_type_t changed in 1.9.3, therefore require at least 2.0. 124 * The old Data_XXX macro family was deprecated on Ruby 2.2. 125 * Use TypedData_XXX if available. 126 */ 127 #if defined(TypedData_Wrap_Struct) && (RUBY_VERSION >= 20) 128 # define USE_TYPEDDATA 1 129 #endif 130 131 /* 132 * Backward compatibility for Ruby 1.8 and earlier. 133 * Ruby 1.9 does not provide STR2CSTR, instead StringValuePtr is provided. 134 * Ruby 1.9 does not provide RXXX(s)->len and RXXX(s)->ptr, instead 135 * RXXX_LEN(s) and RXXX_PTR(s) are provided. 136 */ 137 #ifndef StringValuePtr 138 # define StringValuePtr(s) STR2CSTR(s) 139 #endif 140 #ifndef RARRAY_LEN 141 # define RARRAY_LEN(s) RARRAY(s)->len 142 #endif 143 #ifndef RARRAY_PTR 144 # define RARRAY_PTR(s) RARRAY(s)->ptr 145 #endif 146 #ifndef RSTRING_LEN 147 # define RSTRING_LEN(s) RSTRING(s)->len 148 #endif 149 #ifndef RSTRING_PTR 150 # define RSTRING_PTR(s) RSTRING(s)->ptr 151 #endif 152 153 #ifdef HAVE_DUP 154 # undef HAVE_DUP 155 #endif 156 157 #include "vim.h" 158 #include "version.h" 159 160 #ifdef DYNAMIC_RUBY 161 # if !defined(MSWIN) // must come after including vim.h, where it is defined 162 # include <dlfcn.h> 163 # define HINSTANCE void* 164 # define RUBY_PROC void* 165 # define load_dll(n) dlopen((n), RTLD_LAZY|RTLD_GLOBAL) 166 # define symbol_from_dll dlsym 167 # define close_dll dlclose 168 # else 169 # define RUBY_PROC FARPROC 170 # define load_dll vimLoadLib 171 # define symbol_from_dll GetProcAddress 172 # define close_dll FreeLibrary 173 # endif 174 #endif 175 176 #if defined(PROTO) && !defined(FEAT_RUBY) 177 // Define these to be able to generate the function prototypes. 178 # define VALUE int 179 # define RUBY_DATA_FUNC int 180 #endif 181 182 static int ruby_initialized = 0; 183 static void *ruby_stack_start; 184 static VALUE objtbl; 185 186 static VALUE mVIM; 187 static VALUE cBuffer; 188 static VALUE cVimWindow; 189 static VALUE eDeletedBufferError; 190 static VALUE eDeletedWindowError; 191 192 static int ensure_ruby_initialized(void); 193 static void error_print(int); 194 static void ruby_io_init(void); 195 static void ruby_vim_init(void); 196 static int ruby_convert_to_vim_value(VALUE val, typval_T *rettv); 197 198 #if (RUBY_VERSION >= 19) || defined(RUBY_INIT_STACK) 199 # if defined(__ia64) && !defined(ruby_init_stack) 200 # define ruby_init_stack(addr) ruby_init_stack((addr), rb_ia64_bsp()) 201 # endif 202 #endif 203 204 #if defined(DYNAMIC_RUBY) || defined(PROTO) 205 # if defined(PROTO) && !defined(HINSTANCE) 206 # define HINSTANCE int // for generating prototypes 207 # endif 208 209 /* 210 * Wrapper defines 211 */ 212 // Ruby 2.7 actually expands the following symbols as macro. 213 # if RUBY_VERSION >= 27 214 # undef rb_define_global_function 215 # undef rb_define_method 216 # undef rb_define_module_function 217 # undef rb_define_singleton_method 218 # endif 219 220 # define rb_assoc_new dll_rb_assoc_new 221 # define rb_cObject (*dll_rb_cObject) 222 # define rb_class_new_instance dll_rb_class_new_instance 223 # define rb_check_type dll_rb_check_type 224 # ifdef USE_TYPEDDATA 225 # define rb_check_typeddata dll_rb_check_typeddata 226 # endif 227 # define rb_class_path dll_rb_class_path 228 # ifdef USE_TYPEDDATA 229 # if RUBY_VERSION >= 23 230 # define rb_data_typed_object_wrap dll_rb_data_typed_object_wrap 231 # else 232 # define rb_data_typed_object_alloc dll_rb_data_typed_object_alloc 233 # endif 234 # else 235 # define rb_data_object_alloc dll_rb_data_object_alloc 236 # endif 237 # define rb_define_class_under dll_rb_define_class_under 238 # define rb_define_const dll_rb_define_const 239 # define rb_define_global_function dll_rb_define_global_function 240 # define rb_define_method dll_rb_define_method 241 # define rb_define_module dll_rb_define_module 242 # define rb_define_module_function dll_rb_define_module_function 243 # define rb_define_singleton_method dll_rb_define_singleton_method 244 # define rb_define_virtual_variable dll_rb_define_virtual_variable 245 # define rb_stdout (*dll_rb_stdout) 246 # define rb_stderr (*dll_rb_stderr) 247 # define rb_eArgError (*dll_rb_eArgError) 248 # define rb_eIndexError (*dll_rb_eIndexError) 249 # define rb_eRuntimeError (*dll_rb_eRuntimeError) 250 # define rb_eStandardError (*dll_rb_eStandardError) 251 # define rb_eval_string_protect dll_rb_eval_string_protect 252 # if RUBY_VERSION >= 21 253 # define rb_funcallv dll_rb_funcallv 254 # else 255 # define rb_funcall2 dll_rb_funcall2 256 # endif 257 # define rb_global_variable dll_rb_global_variable 258 # define rb_hash_aset dll_rb_hash_aset 259 # define rb_hash_foreach dll_rb_hash_foreach 260 # define rb_hash_new dll_rb_hash_new 261 # define rb_inspect dll_rb_inspect 262 # define rb_int2inum dll_rb_int2inum 263 264 // ruby.h may redefine rb_intern to use RUBY_CONST_ID_CACHE(), but that won't 265 // work. Not using the cache appears to be the best solution. 266 # undef rb_intern 267 # define rb_intern dll_rb_intern 268 269 # if VIM_SIZEOF_INT < VIM_SIZEOF_LONG // 64 bits only 270 # if RUBY_VERSION <= 18 271 # define rb_fix2int dll_rb_fix2int 272 # define rb_num2int dll_rb_num2int 273 # endif 274 # define rb_num2uint dll_rb_num2uint 275 # endif 276 # define rb_num2dbl dll_rb_num2dbl 277 # define rb_lastline_get dll_rb_lastline_get 278 # define rb_lastline_set dll_rb_lastline_set 279 # define rb_protect dll_rb_protect 280 # define rb_load dll_rb_load 281 # if RUBY_VERSION <= 18 282 # define rb_num2long dll_rb_num2long 283 # endif 284 # if RUBY_VERSION <= 19 285 # define rb_num2ulong dll_rb_num2ulong 286 # endif 287 # define rb_obj_alloc dll_rb_obj_alloc 288 # define rb_obj_as_string dll_rb_obj_as_string 289 # define rb_obj_id dll_rb_obj_id 290 # define rb_raise dll_rb_raise 291 # define rb_str_cat dll_rb_str_cat 292 # define rb_str_concat dll_rb_str_concat 293 # undef rb_str_new 294 # define rb_str_new dll_rb_str_new 295 # ifdef rb_str_new2 296 // Ruby may #define rb_str_new2 to use rb_str_new_cstr. 297 # define need_rb_str_new_cstr 1 298 // Ruby's headers #define rb_str_new_cstr to make use of GCC's 299 // __builtin_constant_p extension. 300 # undef rb_str_new_cstr 301 # define rb_str_new_cstr dll_rb_str_new_cstr 302 # else 303 # define rb_str_new2 dll_rb_str_new2 304 # endif 305 # if RUBY_VERSION >= 18 306 # define rb_string_value dll_rb_string_value 307 # define rb_string_value_ptr dll_rb_string_value_ptr 308 # define rb_float_new dll_rb_float_new 309 # define rb_ary_new dll_rb_ary_new 310 # ifdef rb_ary_new4 311 # define RB_ARY_NEW4_MACRO 1 312 # undef rb_ary_new4 313 # endif 314 # define rb_ary_new4 dll_rb_ary_new4 315 # define rb_ary_push dll_rb_ary_push 316 # if (RUBY_VERSION >= 19) || defined(RUBY_INIT_STACK) 317 # ifdef __ia64 318 # define rb_ia64_bsp dll_rb_ia64_bsp 319 # undef ruby_init_stack 320 # define ruby_init_stack(addr) dll_ruby_init_stack((addr), rb_ia64_bsp()) 321 # else 322 # define ruby_init_stack dll_ruby_init_stack 323 # endif 324 # endif 325 # else 326 # define rb_str2cstr dll_rb_str2cstr 327 # endif 328 # if RUBY_VERSION >= 19 329 # define rb_errinfo dll_rb_errinfo 330 # else 331 # define ruby_errinfo (*dll_ruby_errinfo) 332 # endif 333 # define ruby_init dll_ruby_init 334 # define ruby_init_loadpath dll_ruby_init_loadpath 335 # ifdef MSWIN 336 # if RUBY_VERSION >= 19 337 # define ruby_sysinit dll_ruby_sysinit 338 # else 339 # define NtInitialize dll_NtInitialize 340 # endif 341 # if RUBY_VERSION >= 18 342 # define rb_w32_snprintf dll_rb_w32_snprintf 343 # endif 344 # endif 345 346 # if RUBY_VERSION >= 19 347 # define ruby_script dll_ruby_script 348 # define rb_enc_find_index dll_rb_enc_find_index 349 # define rb_enc_find dll_rb_enc_find 350 # undef rb_enc_str_new 351 # define rb_enc_str_new dll_rb_enc_str_new 352 # define rb_sprintf dll_rb_sprintf 353 # define rb_require dll_rb_require 354 # define ruby_options dll_ruby_options 355 # endif 356 357 /* 358 * Pointers for dynamic link 359 */ 360 static VALUE (*dll_rb_assoc_new) (VALUE, VALUE); 361 VALUE *dll_rb_cFalseClass; 362 VALUE *dll_rb_cFixnum; 363 # if defined(USE_RUBY_INTEGER) 364 VALUE *dll_rb_cInteger; 365 # endif 366 # if RUBY_VERSION >= 20 367 VALUE *dll_rb_cFloat; 368 # endif 369 VALUE *dll_rb_cNilClass; 370 static VALUE *dll_rb_cObject; 371 VALUE *dll_rb_cString; 372 VALUE *dll_rb_cSymbol; 373 VALUE *dll_rb_cTrueClass; 374 static VALUE (*dll_rb_class_new_instance) (int,VALUE*,VALUE); 375 static void (*dll_rb_check_type) (VALUE,int); 376 # ifdef USE_TYPEDDATA 377 static void *(*dll_rb_check_typeddata) (VALUE,const rb_data_type_t *); 378 # endif 379 static VALUE (*dll_rb_class_path) (VALUE); 380 # ifdef USE_TYPEDDATA 381 # if RUBY_VERSION >= 23 382 static VALUE (*dll_rb_data_typed_object_wrap) (VALUE, void*, const rb_data_type_t *); 383 # else 384 static VALUE (*dll_rb_data_typed_object_alloc) (VALUE, void*, const rb_data_type_t *); 385 # endif 386 # else 387 static VALUE (*dll_rb_data_object_alloc) (VALUE, void*, RUBY_DATA_FUNC, RUBY_DATA_FUNC); 388 # endif 389 static VALUE (*dll_rb_define_class_under) (VALUE, const char*, VALUE); 390 static void (*dll_rb_define_const) (VALUE,const char*,VALUE); 391 static void (*dll_rb_define_global_function) (const char*,VALUE(*)(),int); 392 static void (*dll_rb_define_method) (VALUE,const char*,VALUE(*)(),int); 393 static VALUE (*dll_rb_define_module) (const char*); 394 static void (*dll_rb_define_module_function) (VALUE,const char*,VALUE(*)(),int); 395 static void (*dll_rb_define_singleton_method) (VALUE,const char*,VALUE(*)(),int); 396 static void (*dll_rb_define_virtual_variable) (const char*,VALUE(*)(),void(*)()); 397 static VALUE *dll_rb_stdout; 398 static VALUE *dll_rb_stderr; 399 static VALUE *dll_rb_eArgError; 400 static VALUE *dll_rb_eIndexError; 401 static VALUE *dll_rb_eRuntimeError; 402 static VALUE *dll_rb_eStandardError; 403 static VALUE (*dll_rb_eval_string_protect) (const char*, int*); 404 # if RUBY_VERSION >= 21 405 static VALUE (*dll_rb_funcallv) (VALUE, ID, int, const VALUE*); 406 # else 407 static VALUE (*dll_rb_funcall2) (VALUE, ID, int, const VALUE*); 408 # endif 409 static void (*dll_rb_global_variable) (VALUE*); 410 static VALUE (*dll_rb_hash_aset) (VALUE, VALUE, VALUE); 411 static VALUE (*dll_rb_hash_foreach) (VALUE, int (*)(VALUE, VALUE, VALUE), VALUE); 412 static VALUE (*dll_rb_hash_new) (void); 413 static VALUE (*dll_rb_inspect) (VALUE); 414 static VALUE (*dll_rb_int2inum) (long); 415 static ID (*dll_rb_intern) (const char*); 416 # if VIM_SIZEOF_INT < VIM_SIZEOF_LONG // 64 bits only 417 static long (*dll_rb_fix2int) (VALUE); 418 static long (*dll_rb_num2int) (VALUE); 419 static unsigned long (*dll_rb_num2uint) (VALUE); 420 # endif 421 static double (*dll_rb_num2dbl) (VALUE); 422 static VALUE (*dll_rb_lastline_get) (void); 423 static void (*dll_rb_lastline_set) (VALUE); 424 static VALUE (*dll_rb_protect) (VALUE (*)(VALUE), VALUE, int*); 425 static void (*dll_rb_load) (VALUE, int); 426 static long (*dll_rb_num2long) (VALUE); 427 static unsigned long (*dll_rb_num2ulong) (VALUE); 428 static VALUE (*dll_rb_obj_alloc) (VALUE); 429 static VALUE (*dll_rb_obj_as_string) (VALUE); 430 static VALUE (*dll_rb_obj_id) (VALUE); 431 static void (*dll_rb_raise) (VALUE, const char*, ...); 432 # if RUBY_VERSION >= 18 433 static VALUE (*dll_rb_string_value) (volatile VALUE*); 434 # else 435 static char *(*dll_rb_str2cstr) (VALUE,int*); 436 # endif 437 static VALUE (*dll_rb_str_cat) (VALUE, const char*, long); 438 static VALUE (*dll_rb_str_concat) (VALUE, VALUE); 439 static VALUE (*dll_rb_str_new) (const char*, long); 440 # ifdef need_rb_str_new_cstr 441 // Ruby may #define rb_str_new2 to use rb_str_new_cstr. 442 static VALUE (*dll_rb_str_new_cstr) (const char*); 443 # else 444 static VALUE (*dll_rb_str_new2) (const char*); 445 # endif 446 # if RUBY_VERSION >= 19 447 static VALUE (*dll_rb_errinfo) (void); 448 # else 449 static VALUE *dll_ruby_errinfo; 450 # endif 451 static void (*dll_ruby_init) (void); 452 static void (*dll_ruby_init_loadpath) (void); 453 # ifdef MSWIN 454 # if RUBY_VERSION >= 19 455 static void (*dll_ruby_sysinit) (int*, char***); 456 # else 457 static void (*dll_NtInitialize) (int*, char***); 458 # endif 459 # if RUBY_VERSION >= 18 460 static int (*dll_rb_w32_snprintf)(char*, size_t, const char*, ...); 461 # endif 462 # endif 463 # if RUBY_VERSION >= 18 464 static char * (*dll_rb_string_value_ptr) (volatile VALUE*); 465 static VALUE (*dll_rb_float_new) (double); 466 static VALUE (*dll_rb_ary_new) (void); 467 static VALUE (*dll_rb_ary_new4) (long n, const VALUE *elts); 468 static VALUE (*dll_rb_ary_push) (VALUE, VALUE); 469 # if RUBY_VERSION >= 26 470 static void (*dll_rb_ary_detransient) (VALUE); 471 # endif 472 # if (RUBY_VERSION >= 19) || defined(RUBY_INIT_STACK) 473 # ifdef __ia64 474 static void * (*dll_rb_ia64_bsp) (void); 475 static void (*dll_ruby_init_stack)(VALUE*, void*); 476 # else 477 static void (*dll_ruby_init_stack)(VALUE*); 478 # endif 479 # endif 480 # endif 481 # if RUBY_VERSION >= 19 482 static VALUE (*dll_rb_int2big)(SIGNED_VALUE); 483 # endif 484 485 # if RUBY_VERSION >= 19 486 static void (*dll_ruby_script) (const char*); 487 static int (*dll_rb_enc_find_index) (const char*); 488 static rb_encoding* (*dll_rb_enc_find) (const char*); 489 static VALUE (*dll_rb_enc_str_new) (const char*, long, rb_encoding*); 490 static VALUE (*dll_rb_sprintf) (const char*, ...); 491 static VALUE (*dll_rb_require) (const char*); 492 static void* (*ruby_options)(int, char**); 493 # endif 494 495 # if defined(USE_RGENGC) && USE_RGENGC 496 # if RUBY_VERSION == 21 497 static void (*dll_rb_gc_writebarrier_unprotect_promoted)(VALUE); 498 # else 499 static void (*dll_rb_gc_writebarrier_unprotect)(VALUE obj); 500 # endif 501 # endif 502 503 # if (RUBY_VERSION >= 19) && !defined(PROTO) 504 # if RUBY_VERSION >= 22 505 long 506 rb_num2long_stub(VALUE x) 507 # else 508 SIGNED_VALUE 509 rb_num2long_stub(VALUE x) 510 # endif 511 { 512 return dll_rb_num2long(x); 513 } 514 # if RUBY_VERSION >= 26 515 VALUE 516 rb_int2big_stub(intptr_t x) 517 # else 518 VALUE 519 rb_int2big_stub(SIGNED_VALUE x) 520 # endif 521 { 522 return dll_rb_int2big(x); 523 } 524 # if (RUBY_VERSION >= 19) && (VIM_SIZEOF_INT < VIM_SIZEOF_LONG) 525 long 526 rb_fix2int_stub(VALUE x) 527 { 528 return dll_rb_fix2int(x); 529 } 530 long 531 rb_num2int_stub(VALUE x) 532 { 533 return dll_rb_num2int(x); 534 } 535 # endif 536 # if RUBY_VERSION >= 20 537 VALUE 538 rb_float_new_in_heap(double d) 539 { 540 return dll_rb_float_new(d); 541 } 542 # if RUBY_VERSION >= 22 543 unsigned long 544 rb_num2ulong(VALUE x) 545 # else 546 VALUE 547 rb_num2ulong(VALUE x) 548 # endif 549 { 550 return (long)RSHIFT((SIGNED_VALUE)(x),1); 551 } 552 # endif 553 # endif 554 555 // Do not generate a prototype here, VALUE isn't always defined. 556 # if defined(USE_RGENGC) && USE_RGENGC && !defined(PROTO) 557 # if RUBY_VERSION == 21 558 void 559 rb_gc_writebarrier_unprotect_promoted_stub(VALUE obj) 560 { 561 dll_rb_gc_writebarrier_unprotect_promoted(obj); 562 } 563 # else 564 void 565 rb_gc_writebarrier_unprotect_stub(VALUE obj) 566 { 567 dll_rb_gc_writebarrier_unprotect(obj); 568 } 569 # endif 570 # endif 571 572 # if RUBY_VERSION >= 26 573 void 574 rb_ary_detransient_stub(VALUE x) 575 { 576 dll_rb_ary_detransient(x); 577 } 578 # endif 579 580 static HINSTANCE hinstRuby = NULL; // Instance of ruby.dll 581 582 /* 583 * Table of name to function pointer of ruby. 584 */ 585 static struct 586 { 587 char *name; 588 RUBY_PROC *ptr; 589 } ruby_funcname_table[] = 590 { 591 {"rb_assoc_new", (RUBY_PROC*)&dll_rb_assoc_new}, 592 {"rb_cFalseClass", (RUBY_PROC*)&dll_rb_cFalseClass}, 593 # if defined(USE_RUBY_INTEGER) 594 {"rb_cInteger", (RUBY_PROC*)&dll_rb_cInteger}, 595 # else 596 {"rb_cFixnum", (RUBY_PROC*)&dll_rb_cFixnum}, 597 # endif 598 # if RUBY_VERSION >= 20 599 {"rb_cFloat", (RUBY_PROC*)&dll_rb_cFloat}, 600 # endif 601 {"rb_cNilClass", (RUBY_PROC*)&dll_rb_cNilClass}, 602 {"rb_cObject", (RUBY_PROC*)&dll_rb_cObject}, 603 {"rb_cString", (RUBY_PROC*)&dll_rb_cString}, 604 {"rb_cSymbol", (RUBY_PROC*)&dll_rb_cSymbol}, 605 {"rb_cTrueClass", (RUBY_PROC*)&dll_rb_cTrueClass}, 606 {"rb_class_new_instance", (RUBY_PROC*)&dll_rb_class_new_instance}, 607 {"rb_check_type", (RUBY_PROC*)&dll_rb_check_type}, 608 # ifdef USE_TYPEDDATA 609 {"rb_check_typeddata", (RUBY_PROC*)&dll_rb_check_typeddata}, 610 # endif 611 {"rb_class_path", (RUBY_PROC*)&dll_rb_class_path}, 612 # ifdef USE_TYPEDDATA 613 # if RUBY_VERSION >= 23 614 {"rb_data_typed_object_wrap", (RUBY_PROC*)&dll_rb_data_typed_object_wrap}, 615 # else 616 {"rb_data_typed_object_alloc", (RUBY_PROC*)&dll_rb_data_typed_object_alloc}, 617 # endif 618 # else 619 {"rb_data_object_alloc", (RUBY_PROC*)&dll_rb_data_object_alloc}, 620 # endif 621 {"rb_define_class_under", (RUBY_PROC*)&dll_rb_define_class_under}, 622 {"rb_define_const", (RUBY_PROC*)&dll_rb_define_const}, 623 {"rb_define_global_function", (RUBY_PROC*)&dll_rb_define_global_function}, 624 {"rb_define_method", (RUBY_PROC*)&dll_rb_define_method}, 625 {"rb_define_module", (RUBY_PROC*)&dll_rb_define_module}, 626 {"rb_define_module_function", (RUBY_PROC*)&dll_rb_define_module_function}, 627 {"rb_define_singleton_method", (RUBY_PROC*)&dll_rb_define_singleton_method}, 628 {"rb_define_virtual_variable", (RUBY_PROC*)&dll_rb_define_virtual_variable}, 629 {"rb_stdout", (RUBY_PROC*)&dll_rb_stdout}, 630 {"rb_stderr", (RUBY_PROC*)&dll_rb_stderr}, 631 {"rb_eArgError", (RUBY_PROC*)&dll_rb_eArgError}, 632 {"rb_eIndexError", (RUBY_PROC*)&dll_rb_eIndexError}, 633 {"rb_eRuntimeError", (RUBY_PROC*)&dll_rb_eRuntimeError}, 634 {"rb_eStandardError", (RUBY_PROC*)&dll_rb_eStandardError}, 635 {"rb_eval_string_protect", (RUBY_PROC*)&dll_rb_eval_string_protect}, 636 # if RUBY_VERSION >= 21 637 {"rb_funcallv", (RUBY_PROC*)&dll_rb_funcallv}, 638 # else 639 {"rb_funcall2", (RUBY_PROC*)&dll_rb_funcall2}, 640 # endif 641 {"rb_global_variable", (RUBY_PROC*)&dll_rb_global_variable}, 642 {"rb_hash_aset", (RUBY_PROC*)&dll_rb_hash_aset}, 643 {"rb_hash_foreach", (RUBY_PROC*)&dll_rb_hash_foreach}, 644 {"rb_hash_new", (RUBY_PROC*)&dll_rb_hash_new}, 645 {"rb_inspect", (RUBY_PROC*)&dll_rb_inspect}, 646 {"rb_int2inum", (RUBY_PROC*)&dll_rb_int2inum}, 647 {"rb_intern", (RUBY_PROC*)&dll_rb_intern}, 648 # if VIM_SIZEOF_INT < VIM_SIZEOF_LONG // 64 bits only 649 {"rb_fix2int", (RUBY_PROC*)&dll_rb_fix2int}, 650 {"rb_num2int", (RUBY_PROC*)&dll_rb_num2int}, 651 {"rb_num2uint", (RUBY_PROC*)&dll_rb_num2uint}, 652 # endif 653 {"rb_num2dbl", (RUBY_PROC*)&dll_rb_num2dbl}, 654 {"rb_lastline_get", (RUBY_PROC*)&dll_rb_lastline_get}, 655 {"rb_lastline_set", (RUBY_PROC*)&dll_rb_lastline_set}, 656 {"rb_protect", (RUBY_PROC*)&dll_rb_protect}, 657 {"rb_load", (RUBY_PROC*)&dll_rb_load}, 658 {"rb_num2long", (RUBY_PROC*)&dll_rb_num2long}, 659 {"rb_num2ulong", (RUBY_PROC*)&dll_rb_num2ulong}, 660 {"rb_obj_alloc", (RUBY_PROC*)&dll_rb_obj_alloc}, 661 {"rb_obj_as_string", (RUBY_PROC*)&dll_rb_obj_as_string}, 662 {"rb_obj_id", (RUBY_PROC*)&dll_rb_obj_id}, 663 {"rb_raise", (RUBY_PROC*)&dll_rb_raise}, 664 # if RUBY_VERSION >= 18 665 {"rb_string_value", (RUBY_PROC*)&dll_rb_string_value}, 666 # else 667 {"rb_str2cstr", (RUBY_PROC*)&dll_rb_str2cstr}, 668 # endif 669 {"rb_str_cat", (RUBY_PROC*)&dll_rb_str_cat}, 670 {"rb_str_concat", (RUBY_PROC*)&dll_rb_str_concat}, 671 {"rb_str_new", (RUBY_PROC*)&dll_rb_str_new}, 672 # ifdef need_rb_str_new_cstr 673 {"rb_str_new_cstr", (RUBY_PROC*)&dll_rb_str_new_cstr}, 674 # else 675 {"rb_str_new2", (RUBY_PROC*)&dll_rb_str_new2}, 676 # endif 677 # if RUBY_VERSION >= 19 678 {"rb_errinfo", (RUBY_PROC*)&dll_rb_errinfo}, 679 # else 680 {"ruby_errinfo", (RUBY_PROC*)&dll_ruby_errinfo}, 681 # endif 682 {"ruby_init", (RUBY_PROC*)&dll_ruby_init}, 683 {"ruby_init_loadpath", (RUBY_PROC*)&dll_ruby_init_loadpath}, 684 # ifdef MSWIN 685 # if RUBY_VERSION >= 19 686 {"ruby_sysinit", (RUBY_PROC*)&dll_ruby_sysinit}, 687 # else 688 {"NtInitialize", (RUBY_PROC*)&dll_NtInitialize}, 689 # endif 690 # if RUBY_VERSION >= 18 691 {"rb_w32_snprintf", (RUBY_PROC*)&dll_rb_w32_snprintf}, 692 # endif 693 # endif 694 # if RUBY_VERSION >= 18 695 {"rb_string_value_ptr", (RUBY_PROC*)&dll_rb_string_value_ptr}, 696 # if RUBY_VERSION <= 19 697 {"rb_float_new", (RUBY_PROC*)&dll_rb_float_new}, 698 # else 699 {"rb_float_new_in_heap", (RUBY_PROC*)&dll_rb_float_new}, 700 # endif 701 {"rb_ary_new", (RUBY_PROC*)&dll_rb_ary_new}, 702 # ifdef RB_ARY_NEW4_MACRO 703 {"rb_ary_new_from_values", (RUBY_PROC*)&dll_rb_ary_new4}, 704 # else 705 {"rb_ary_new4", (RUBY_PROC*)&dll_rb_ary_new4}, 706 # endif 707 {"rb_ary_push", (RUBY_PROC*)&dll_rb_ary_push}, 708 # if RUBY_VERSION >= 26 709 {"rb_ary_detransient", (RUBY_PROC*)&dll_rb_ary_detransient}, 710 # endif 711 # endif 712 # if RUBY_VERSION >= 19 713 {"rb_int2big", (RUBY_PROC*)&dll_rb_int2big}, 714 {"ruby_script", (RUBY_PROC*)&dll_ruby_script}, 715 {"rb_enc_find_index", (RUBY_PROC*)&dll_rb_enc_find_index}, 716 {"rb_enc_find", (RUBY_PROC*)&dll_rb_enc_find}, 717 {"rb_enc_str_new", (RUBY_PROC*)&dll_rb_enc_str_new}, 718 {"rb_sprintf", (RUBY_PROC*)&dll_rb_sprintf}, 719 {"rb_require", (RUBY_PROC*)&dll_rb_require}, 720 {"ruby_options", (RUBY_PROC*)&dll_ruby_options}, 721 # endif 722 # if (RUBY_VERSION >= 19) || defined(RUBY_INIT_STACK) 723 # ifdef __ia64 724 {"rb_ia64_bsp", (RUBY_PROC*)&dll_rb_ia64_bsp}, 725 # endif 726 {"ruby_init_stack", (RUBY_PROC*)&dll_ruby_init_stack}, 727 # endif 728 # if defined(USE_RGENGC) && USE_RGENGC 729 # if RUBY_VERSION == 21 730 {"rb_gc_writebarrier_unprotect_promoted", (RUBY_PROC*)&dll_rb_gc_writebarrier_unprotect_promoted}, 731 # else 732 {"rb_gc_writebarrier_unprotect", (RUBY_PROC*)&dll_rb_gc_writebarrier_unprotect}, 733 # endif 734 # endif 735 {"", NULL}, 736 }; 737 738 /* 739 * Load library and get all pointers. 740 * Parameter 'libname' provides name of DLL. 741 * Return OK or FAIL. 742 */ 743 static int 744 ruby_runtime_link_init(char *libname, int verbose) 745 { 746 int i; 747 748 if (hinstRuby) 749 return OK; 750 hinstRuby = load_dll(libname); 751 if (!hinstRuby) 752 { 753 if (verbose) 754 semsg(_(e_loadlib), libname); 755 return FAIL; 756 } 757 758 for (i = 0; ruby_funcname_table[i].ptr; ++i) 759 { 760 if (!(*ruby_funcname_table[i].ptr = symbol_from_dll(hinstRuby, 761 ruby_funcname_table[i].name))) 762 { 763 close_dll(hinstRuby); 764 hinstRuby = NULL; 765 if (verbose) 766 semsg(_(e_loadfunc), ruby_funcname_table[i].name); 767 return FAIL; 768 } 769 } 770 return OK; 771 } 772 773 /* 774 * If ruby is enabled (there is installed ruby on Windows system) return TRUE, 775 * else FALSE. 776 */ 777 int 778 ruby_enabled(int verbose) 779 { 780 return ruby_runtime_link_init((char *)p_rubydll, verbose) == OK; 781 } 782 #endif // defined(DYNAMIC_RUBY) || defined(PROTO) 783 784 void 785 ruby_end(void) 786 { 787 } 788 789 void 790 ex_ruby(exarg_T *eap) 791 { 792 int state; 793 char *script = NULL; 794 795 script = (char *)script_get(eap, eap->arg); 796 if (!eap->skip && ensure_ruby_initialized()) 797 { 798 if (script == NULL) 799 rb_eval_string_protect((char *)eap->arg, &state); 800 else 801 rb_eval_string_protect(script, &state); 802 if (state) 803 error_print(state); 804 } 805 vim_free(script); 806 } 807 808 /* 809 * In Ruby 1.9 or later, ruby String object has encoding. 810 * conversion buffer string of vim to ruby String object using 811 * VIM encoding option. 812 */ 813 static VALUE 814 vim_str2rb_enc_str(const char *s) 815 { 816 #if RUBY_VERSION >= 19 817 int isnum; 818 long lval; 819 char_u *sval; 820 rb_encoding *enc; 821 822 isnum = get_option_value((char_u *)"enc", &lval, &sval, 0); 823 if (isnum == 0) 824 { 825 enc = rb_enc_find((char *)sval); 826 vim_free(sval); 827 if (enc) 828 return rb_enc_str_new(s, (long)strlen(s), enc); 829 } 830 #endif 831 return rb_str_new2(s); 832 } 833 834 static VALUE 835 eval_enc_string_protect(const char *str, int *state) 836 { 837 #if RUBY_VERSION >= 19 838 int isnum; 839 long lval; 840 char_u *sval; 841 rb_encoding *enc; 842 VALUE v; 843 844 isnum = get_option_value((char_u *)"enc", &lval, &sval, 0); 845 if (isnum == 0) 846 { 847 enc = rb_enc_find((char *)sval); 848 vim_free(sval); 849 if (enc) 850 { 851 v = rb_sprintf("#-*- coding:%s -*-\n%s", rb_enc_name(enc), str); 852 return rb_eval_string_protect(StringValuePtr(v), state); 853 } 854 } 855 #endif 856 return rb_eval_string_protect(str, state); 857 } 858 859 void 860 ex_rubydo(exarg_T *eap) 861 { 862 int state; 863 linenr_T i; 864 buf_T *was_curbuf = curbuf; 865 866 if (ensure_ruby_initialized()) 867 { 868 if (u_save(eap->line1 - 1, eap->line2 + 1) != OK) 869 return; 870 for (i = eap->line1; i <= eap->line2; i++) 871 { 872 VALUE line; 873 874 if (i > curbuf->b_ml.ml_line_count) 875 break; 876 line = vim_str2rb_enc_str((char *)ml_get(i)); 877 rb_lastline_set(line); 878 eval_enc_string_protect((char *) eap->arg, &state); 879 if (state) 880 { 881 error_print(state); 882 break; 883 } 884 if (was_curbuf != curbuf) 885 break; 886 line = rb_lastline_get(); 887 if (!NIL_P(line)) 888 { 889 if (TYPE(line) != T_STRING) 890 { 891 emsg(_("E265: $_ must be an instance of String")); 892 return; 893 } 894 ml_replace(i, (char_u *) StringValuePtr(line), 1); 895 changed(); 896 #ifdef SYNTAX_HL 897 syn_changed(i); // recompute syntax hl. for this line 898 #endif 899 } 900 } 901 check_cursor(); 902 update_curbuf(NOT_VALID); 903 } 904 } 905 906 static VALUE 907 rb_load_wrap(VALUE file_to_load) 908 { 909 rb_load(file_to_load, 0); 910 return Qnil; 911 } 912 913 void 914 ex_rubyfile(exarg_T *eap) 915 { 916 int state; 917 918 if (ensure_ruby_initialized()) 919 { 920 VALUE file_to_load = rb_str_new2((const char *)eap->arg); 921 rb_protect(rb_load_wrap, file_to_load, &state); 922 if (state) 923 error_print(state); 924 } 925 } 926 927 void 928 ruby_buffer_free(buf_T *buf) 929 { 930 if (buf->b_ruby_ref) 931 { 932 rb_hash_aset(objtbl, rb_obj_id((VALUE) buf->b_ruby_ref), Qnil); 933 RDATA(buf->b_ruby_ref)->data = NULL; 934 } 935 } 936 937 void 938 ruby_window_free(win_T *win) 939 { 940 if (win->w_ruby_ref) 941 { 942 rb_hash_aset(objtbl, rb_obj_id((VALUE) win->w_ruby_ref), Qnil); 943 RDATA(win->w_ruby_ref)->data = NULL; 944 } 945 } 946 947 static int 948 ensure_ruby_initialized(void) 949 { 950 if (!ruby_initialized) 951 { 952 #ifdef DYNAMIC_RUBY 953 if (ruby_enabled(TRUE)) 954 { 955 #endif 956 #ifdef MSWIN 957 // suggested by Ariya Mizutani 958 int argc = 1; 959 char *argv[] = {"gvim.exe"}; 960 char **argvp = argv; 961 # if RUBY_VERSION >= 19 962 ruby_sysinit(&argc, &argvp); 963 # else 964 NtInitialize(&argc, &argvp); 965 # endif 966 #endif 967 { 968 #if (RUBY_VERSION >= 19) || defined(RUBY_INIT_STACK) 969 ruby_init_stack(ruby_stack_start); 970 #endif 971 ruby_init(); 972 } 973 #if RUBY_VERSION >= 19 974 { 975 int dummy_argc = 2; 976 char *dummy_argv[] = {"vim-ruby", "-e_=0"}; 977 ruby_options(dummy_argc, dummy_argv); 978 } 979 ruby_script("vim-ruby"); 980 #else 981 ruby_init_loadpath(); 982 #endif 983 ruby_io_init(); 984 ruby_vim_init(); 985 ruby_initialized = 1; 986 #ifdef DYNAMIC_RUBY 987 } 988 else 989 { 990 emsg(_("E266: Sorry, this command is disabled, the Ruby library could not be loaded.")); 991 return 0; 992 } 993 #endif 994 } 995 return ruby_initialized; 996 } 997 998 static void 999 error_print(int state) 1000 { 1001 #if !defined(DYNAMIC_RUBY) && (RUBY_VERSION <= 18) 1002 RUBYEXTERN VALUE ruby_errinfo; 1003 #endif 1004 VALUE error; 1005 VALUE eclass; 1006 VALUE einfo; 1007 VALUE bt; 1008 int attr; 1009 char buff[BUFSIZ]; 1010 long i; 1011 1012 #define TAG_RETURN 0x1 1013 #define TAG_BREAK 0x2 1014 #define TAG_NEXT 0x3 1015 #define TAG_RETRY 0x4 1016 #define TAG_REDO 0x5 1017 #define TAG_RAISE 0x6 1018 #define TAG_THROW 0x7 1019 #define TAG_FATAL 0x8 1020 #define TAG_MASK 0xf 1021 1022 switch (state) 1023 { 1024 case TAG_RETURN: 1025 emsg(_("E267: unexpected return")); 1026 break; 1027 case TAG_NEXT: 1028 emsg(_("E268: unexpected next")); 1029 break; 1030 case TAG_BREAK: 1031 emsg(_("E269: unexpected break")); 1032 break; 1033 case TAG_REDO: 1034 emsg(_("E270: unexpected redo")); 1035 break; 1036 case TAG_RETRY: 1037 emsg(_("E271: retry outside of rescue clause")); 1038 break; 1039 case TAG_RAISE: 1040 case TAG_FATAL: 1041 #if RUBY_VERSION >= 19 1042 error = rb_errinfo(); 1043 #else 1044 error = ruby_errinfo; 1045 #endif 1046 eclass = CLASS_OF(error); 1047 einfo = rb_obj_as_string(error); 1048 if (eclass == rb_eRuntimeError && RSTRING_LEN(einfo) == 0) 1049 { 1050 emsg(_("E272: unhandled exception")); 1051 } 1052 else 1053 { 1054 VALUE epath; 1055 char *p; 1056 1057 epath = rb_class_path(eclass); 1058 vim_snprintf(buff, BUFSIZ, "%s: %s", 1059 RSTRING_PTR(epath), RSTRING_PTR(einfo)); 1060 p = strchr(buff, '\n'); 1061 if (p) *p = '\0'; 1062 emsg(buff); 1063 } 1064 1065 attr = syn_name2attr((char_u *)"Error"); 1066 #if RUBY_VERSION >= 21 1067 bt = rb_funcallv(error, rb_intern("backtrace"), 0, 0); 1068 for (i = 0; i < RARRAY_LEN(bt); i++) 1069 msg_attr(RSTRING_PTR(RARRAY_AREF(bt, i)), attr); 1070 #else 1071 bt = rb_funcall2(error, rb_intern("backtrace"), 0, 0); 1072 for (i = 0; i < RARRAY_LEN(bt); i++) 1073 msg_attr(RSTRING_PTR(RARRAY_PTR(bt)[i]), attr); 1074 #endif 1075 break; 1076 default: 1077 vim_snprintf(buff, BUFSIZ, _("E273: unknown longjmp status %d"), state); 1078 emsg(buff); 1079 break; 1080 } 1081 } 1082 1083 static VALUE 1084 vim_message(VALUE self UNUSED, VALUE str) 1085 { 1086 char *buff, *p; 1087 1088 str = rb_obj_as_string(str); 1089 if (RSTRING_LEN(str) > 0) 1090 { 1091 // Only do this when the string isn't empty, alloc(0) causes trouble. 1092 buff = ALLOCA_N(char, RSTRING_LEN(str) + 1); 1093 strcpy(buff, RSTRING_PTR(str)); 1094 p = strchr(buff, '\n'); 1095 if (p) *p = '\0'; 1096 msg(buff); 1097 } 1098 else 1099 { 1100 msg(""); 1101 } 1102 return Qnil; 1103 } 1104 1105 static VALUE 1106 vim_set_option(VALUE self UNUSED, VALUE str) 1107 { 1108 do_set((char_u *)StringValuePtr(str), 0); 1109 update_screen(NOT_VALID); 1110 return Qnil; 1111 } 1112 1113 static VALUE 1114 vim_command(VALUE self UNUSED, VALUE str) 1115 { 1116 do_cmdline_cmd((char_u *)StringValuePtr(str)); 1117 return Qnil; 1118 } 1119 1120 #ifdef FEAT_EVAL 1121 static VALUE 1122 vim_to_ruby(typval_T *tv) 1123 { 1124 VALUE result = Qnil; 1125 1126 if (tv->v_type == VAR_STRING) 1127 { 1128 result = rb_str_new2(tv->vval.v_string == NULL 1129 ? "" : (char *)(tv->vval.v_string)); 1130 } 1131 else if (tv->v_type == VAR_NUMBER) 1132 { 1133 result = INT2NUM(tv->vval.v_number); 1134 } 1135 # ifdef FEAT_FLOAT 1136 else if (tv->v_type == VAR_FLOAT) 1137 { 1138 result = rb_float_new(tv->vval.v_float); 1139 } 1140 # endif 1141 else if (tv->v_type == VAR_LIST) 1142 { 1143 list_T *list = tv->vval.v_list; 1144 listitem_T *curr; 1145 1146 result = rb_ary_new(); 1147 1148 if (list != NULL) 1149 { 1150 FOR_ALL_LIST_ITEMS(list, curr) 1151 rb_ary_push(result, vim_to_ruby(&curr->li_tv)); 1152 } 1153 } 1154 else if (tv->v_type == VAR_DICT) 1155 { 1156 result = rb_hash_new(); 1157 1158 if (tv->vval.v_dict != NULL) 1159 { 1160 hashtab_T *ht = &tv->vval.v_dict->dv_hashtab; 1161 long_u todo = ht->ht_used; 1162 hashitem_T *hi; 1163 dictitem_T *di; 1164 1165 for (hi = ht->ht_array; todo > 0; ++hi) 1166 { 1167 if (!HASHITEM_EMPTY(hi)) 1168 { 1169 --todo; 1170 1171 di = dict_lookup(hi); 1172 rb_hash_aset(result, rb_str_new2((char *)hi->hi_key), 1173 vim_to_ruby(&di->di_tv)); 1174 } 1175 } 1176 } 1177 } 1178 else if (tv->v_type == VAR_BOOL || tv->v_type == VAR_SPECIAL) 1179 { 1180 if (tv->vval.v_number == VVAL_TRUE) 1181 result = Qtrue; 1182 else if (tv->vval.v_number == VVAL_FALSE) 1183 result = Qfalse; 1184 } 1185 else if (tv->v_type == VAR_BLOB) 1186 { 1187 result = rb_str_new(tv->vval.v_blob->bv_ga.ga_data, 1188 tv->vval.v_blob->bv_ga.ga_len); 1189 } 1190 // else return Qnil; 1191 1192 return result; 1193 } 1194 #endif 1195 1196 static VALUE 1197 vim_evaluate(VALUE self UNUSED, VALUE str) 1198 { 1199 #ifdef FEAT_EVAL 1200 typval_T *tv; 1201 VALUE result; 1202 1203 tv = eval_expr((char_u *)StringValuePtr(str), NULL); 1204 if (tv == NULL) 1205 return Qnil; 1206 result = vim_to_ruby(tv); 1207 1208 free_tv(tv); 1209 1210 return result; 1211 #else 1212 return Qnil; 1213 #endif 1214 } 1215 1216 #ifdef USE_TYPEDDATA 1217 static size_t buffer_dsize(const void *buf); 1218 1219 static const rb_data_type_t buffer_type = { 1220 "vim_buffer", 1221 {0, 0, buffer_dsize, 1222 # if RUBY_VERSION >= 27 1223 0, {0} 1224 # else 1225 {0, 0} 1226 # endif 1227 }, 1228 0, 0, 1229 # ifdef RUBY_TYPED_FREE_IMMEDIATELY 1230 0, 1231 # endif 1232 }; 1233 1234 static size_t 1235 buffer_dsize(const void *buf UNUSED) 1236 { 1237 return sizeof(buf_T); 1238 } 1239 #endif 1240 1241 static VALUE 1242 buffer_new(buf_T *buf) 1243 { 1244 if (buf->b_ruby_ref) 1245 { 1246 return (VALUE) buf->b_ruby_ref; 1247 } 1248 else 1249 { 1250 #ifdef USE_TYPEDDATA 1251 VALUE obj = TypedData_Wrap_Struct(cBuffer, &buffer_type, buf); 1252 #else 1253 VALUE obj = Data_Wrap_Struct(cBuffer, 0, 0, buf); 1254 #endif 1255 buf->b_ruby_ref = (void *) obj; 1256 rb_hash_aset(objtbl, rb_obj_id(obj), obj); 1257 return obj; 1258 } 1259 } 1260 1261 static buf_T * 1262 get_buf(VALUE obj) 1263 { 1264 buf_T *buf; 1265 1266 #ifdef USE_TYPEDDATA 1267 TypedData_Get_Struct(obj, buf_T, &buffer_type, buf); 1268 #else 1269 Data_Get_Struct(obj, buf_T, buf); 1270 #endif 1271 if (buf == NULL) 1272 rb_raise(eDeletedBufferError, "attempt to refer to deleted buffer"); 1273 return buf; 1274 } 1275 1276 static VALUE 1277 vim_blob(VALUE self UNUSED, VALUE str) 1278 { 1279 VALUE result = rb_str_new("0z", 2); 1280 char buf[4]; 1281 int i; 1282 for (i = 0; i < RSTRING_LEN(str); i++) 1283 { 1284 sprintf(buf, "%02X", (unsigned char)(RSTRING_PTR(str)[i])); 1285 rb_str_concat(result, rb_str_new2(buf)); 1286 } 1287 return result; 1288 } 1289 1290 static VALUE 1291 buffer_s_current(void) 1292 { 1293 return buffer_new(curbuf); 1294 } 1295 1296 static VALUE 1297 buffer_s_count(void) 1298 { 1299 buf_T *b; 1300 int n = 0; 1301 1302 FOR_ALL_BUFFERS(b) 1303 { 1304 // Deleted buffers should not be counted 1305 // SegPhault - 01/07/05 1306 if (b->b_p_bl) 1307 n++; 1308 } 1309 1310 return INT2NUM(n); 1311 } 1312 1313 static VALUE 1314 buffer_s_aref(VALUE self UNUSED, VALUE num) 1315 { 1316 buf_T *b; 1317 int n = NUM2INT(num); 1318 1319 FOR_ALL_BUFFERS(b) 1320 { 1321 // Deleted buffers should not be counted 1322 // SegPhault - 01/07/05 1323 if (!b->b_p_bl) 1324 continue; 1325 1326 if (n == 0) 1327 return buffer_new(b); 1328 1329 n--; 1330 } 1331 return Qnil; 1332 } 1333 1334 static VALUE 1335 buffer_name(VALUE self) 1336 { 1337 buf_T *buf = get_buf(self); 1338 1339 return buf->b_ffname ? rb_str_new2((char *)buf->b_ffname) : Qnil; 1340 } 1341 1342 static VALUE 1343 buffer_number(VALUE self) 1344 { 1345 buf_T *buf = get_buf(self); 1346 1347 return INT2NUM(buf->b_fnum); 1348 } 1349 1350 static VALUE 1351 buffer_count(VALUE self) 1352 { 1353 buf_T *buf = get_buf(self); 1354 1355 return INT2NUM(buf->b_ml.ml_line_count); 1356 } 1357 1358 static VALUE 1359 get_buffer_line(buf_T *buf, linenr_T n) 1360 { 1361 if (n <= 0 || n > buf->b_ml.ml_line_count) 1362 rb_raise(rb_eIndexError, "line number %ld out of range", (long)n); 1363 return vim_str2rb_enc_str((char *)ml_get_buf(buf, n, FALSE)); 1364 } 1365 1366 static VALUE 1367 buffer_aref(VALUE self, VALUE num) 1368 { 1369 buf_T *buf = get_buf(self); 1370 1371 if (buf != NULL) 1372 return get_buffer_line(buf, (linenr_T)NUM2LONG(num)); 1373 return Qnil; // For stop warning 1374 } 1375 1376 static VALUE 1377 set_buffer_line(buf_T *buf, linenr_T n, VALUE str) 1378 { 1379 char *line = StringValuePtr(str); 1380 aco_save_T aco; 1381 1382 if (n > 0 && n <= buf->b_ml.ml_line_count && line != NULL) 1383 { 1384 // set curwin/curbuf for "buf" and save some things 1385 aucmd_prepbuf(&aco, buf); 1386 1387 if (u_savesub(n) == OK) 1388 { 1389 ml_replace(n, (char_u *)line, TRUE); 1390 changed(); 1391 #ifdef SYNTAX_HL 1392 syn_changed(n); // recompute syntax hl. for this line 1393 #endif 1394 } 1395 1396 // restore curwin/curbuf and a few other things 1397 aucmd_restbuf(&aco); 1398 // Careful: autocommands may have made "buf" invalid! 1399 1400 update_curbuf(NOT_VALID); 1401 } 1402 else 1403 { 1404 rb_raise(rb_eIndexError, "line number %ld out of range", (long)n); 1405 } 1406 return str; 1407 } 1408 1409 static VALUE 1410 buffer_aset(VALUE self, VALUE num, VALUE str) 1411 { 1412 buf_T *buf = get_buf(self); 1413 1414 if (buf != NULL) 1415 return set_buffer_line(buf, (linenr_T)NUM2LONG(num), str); 1416 return str; 1417 } 1418 1419 static VALUE 1420 buffer_delete(VALUE self, VALUE num) 1421 { 1422 buf_T *buf = get_buf(self); 1423 long n = NUM2LONG(num); 1424 aco_save_T aco; 1425 1426 if (n > 0 && n <= buf->b_ml.ml_line_count) 1427 { 1428 // set curwin/curbuf for "buf" and save some things 1429 aucmd_prepbuf(&aco, buf); 1430 1431 if (u_savedel(n, 1) == OK) 1432 { 1433 ml_delete(n, 0); 1434 1435 // Changes to non-active buffers should properly refresh 1436 // SegPhault - 01/09/05 1437 deleted_lines_mark(n, 1L); 1438 1439 changed(); 1440 } 1441 1442 // restore curwin/curbuf and a few other things 1443 aucmd_restbuf(&aco); 1444 // Careful: autocommands may have made "buf" invalid! 1445 1446 update_curbuf(NOT_VALID); 1447 } 1448 else 1449 { 1450 rb_raise(rb_eIndexError, "line number %ld out of range", n); 1451 } 1452 return Qnil; 1453 } 1454 1455 static VALUE 1456 buffer_append(VALUE self, VALUE num, VALUE str) 1457 { 1458 buf_T *buf = get_buf(self); 1459 char *line = StringValuePtr(str); 1460 long n = NUM2LONG(num); 1461 aco_save_T aco; 1462 1463 if (line == NULL) 1464 { 1465 rb_raise(rb_eIndexError, "NULL line"); 1466 } 1467 else if (n >= 0 && n <= buf->b_ml.ml_line_count) 1468 { 1469 // set curwin/curbuf for "buf" and save some things 1470 aucmd_prepbuf(&aco, buf); 1471 1472 if (u_inssub(n + 1) == OK) 1473 { 1474 ml_append(n, (char_u *) line, (colnr_T) 0, FALSE); 1475 1476 // Changes to non-active buffers should properly refresh screen 1477 // SegPhault - 12/20/04 1478 appended_lines_mark(n, 1L); 1479 1480 changed(); 1481 } 1482 1483 // restore curwin/curbuf and a few other things 1484 aucmd_restbuf(&aco); 1485 // Careful: autocommands may have made "buf" invalid! 1486 1487 update_curbuf(NOT_VALID); 1488 } 1489 else 1490 { 1491 rb_raise(rb_eIndexError, "line number %ld out of range", n); 1492 } 1493 return str; 1494 } 1495 1496 #ifdef USE_TYPEDDATA 1497 static size_t window_dsize(const void *buf); 1498 1499 static const rb_data_type_t window_type = { 1500 "vim_window", 1501 {0, 0, window_dsize, 1502 # if RUBY_VERSION >= 27 1503 0, {0} 1504 # else 1505 {0, 0} 1506 # endif 1507 }, 1508 0, 0, 1509 # ifdef RUBY_TYPED_FREE_IMMEDIATELY 1510 0, 1511 # endif 1512 }; 1513 1514 static size_t 1515 window_dsize(const void *win UNUSED) 1516 { 1517 return sizeof(win_T); 1518 } 1519 #endif 1520 1521 static VALUE 1522 window_new(win_T *win) 1523 { 1524 if (win->w_ruby_ref) 1525 { 1526 return (VALUE) win->w_ruby_ref; 1527 } 1528 else 1529 { 1530 #ifdef USE_TYPEDDATA 1531 VALUE obj = TypedData_Wrap_Struct(cVimWindow, &window_type, win); 1532 #else 1533 VALUE obj = Data_Wrap_Struct(cVimWindow, 0, 0, win); 1534 #endif 1535 win->w_ruby_ref = (void *) obj; 1536 rb_hash_aset(objtbl, rb_obj_id(obj), obj); 1537 return obj; 1538 } 1539 } 1540 1541 static win_T * 1542 get_win(VALUE obj) 1543 { 1544 win_T *win; 1545 1546 #ifdef USE_TYPEDDATA 1547 TypedData_Get_Struct(obj, win_T, &window_type, win); 1548 #else 1549 Data_Get_Struct(obj, win_T, win); 1550 #endif 1551 if (win == NULL) 1552 rb_raise(eDeletedWindowError, "attempt to refer to deleted window"); 1553 return win; 1554 } 1555 1556 static VALUE 1557 window_s_current(void) 1558 { 1559 return window_new(curwin); 1560 } 1561 1562 /* 1563 * Added line manipulation functions 1564 * SegPhault - 03/07/05 1565 */ 1566 static VALUE 1567 line_s_current(void) 1568 { 1569 return get_buffer_line(curbuf, curwin->w_cursor.lnum); 1570 } 1571 1572 static VALUE 1573 set_current_line(VALUE self UNUSED, VALUE str) 1574 { 1575 return set_buffer_line(curbuf, curwin->w_cursor.lnum, str); 1576 } 1577 1578 static VALUE 1579 current_line_number(void) 1580 { 1581 return INT2FIX((int)curwin->w_cursor.lnum); 1582 } 1583 1584 static VALUE 1585 window_s_count(void) 1586 { 1587 win_T *w; 1588 int n = 0; 1589 1590 FOR_ALL_WINDOWS(w) 1591 n++; 1592 return INT2NUM(n); 1593 } 1594 1595 static VALUE 1596 window_s_aref(VALUE self UNUSED, VALUE num) 1597 { 1598 win_T *w; 1599 int n = NUM2INT(num); 1600 1601 for (w = firstwin; w != NULL; w = w->w_next, --n) 1602 if (n == 0) 1603 return window_new(w); 1604 return Qnil; 1605 } 1606 1607 static VALUE 1608 window_buffer(VALUE self) 1609 { 1610 win_T *win = get_win(self); 1611 1612 return buffer_new(win->w_buffer); 1613 } 1614 1615 static VALUE 1616 window_height(VALUE self) 1617 { 1618 win_T *win = get_win(self); 1619 1620 return INT2NUM(win->w_height); 1621 } 1622 1623 static VALUE 1624 window_set_height(VALUE self, VALUE height) 1625 { 1626 win_T *win = get_win(self); 1627 win_T *savewin = curwin; 1628 1629 curwin = win; 1630 win_setheight(NUM2INT(height)); 1631 curwin = savewin; 1632 return height; 1633 } 1634 1635 static VALUE 1636 window_width(VALUE self UNUSED) 1637 { 1638 return INT2NUM(get_win(self)->w_width); 1639 } 1640 1641 static VALUE 1642 window_set_width(VALUE self UNUSED, VALUE width) 1643 { 1644 win_T *win = get_win(self); 1645 win_T *savewin = curwin; 1646 1647 curwin = win; 1648 win_setwidth(NUM2INT(width)); 1649 curwin = savewin; 1650 return width; 1651 } 1652 1653 static VALUE 1654 window_cursor(VALUE self) 1655 { 1656 win_T *win = get_win(self); 1657 1658 return rb_assoc_new(INT2NUM(win->w_cursor.lnum), INT2NUM(win->w_cursor.col)); 1659 } 1660 1661 static VALUE 1662 window_set_cursor(VALUE self, VALUE pos) 1663 { 1664 VALUE lnum, col; 1665 win_T *win = get_win(self); 1666 1667 Check_Type(pos, T_ARRAY); 1668 if (RARRAY_LEN(pos) != 2) 1669 rb_raise(rb_eArgError, "array length must be 2"); 1670 lnum = RARRAY_PTR(pos)[0]; 1671 col = RARRAY_PTR(pos)[1]; 1672 win->w_cursor.lnum = NUM2LONG(lnum); 1673 win->w_cursor.col = NUM2UINT(col); 1674 win->w_set_curswant = TRUE; 1675 check_cursor(); // put cursor on an existing line 1676 update_screen(NOT_VALID); 1677 return Qnil; 1678 } 1679 1680 static VALUE 1681 f_nop(VALUE self UNUSED) 1682 { 1683 return Qnil; 1684 } 1685 1686 static VALUE 1687 f_p(int argc, VALUE *argv, VALUE self UNUSED) 1688 { 1689 int i; 1690 VALUE str = rb_str_new("", 0); 1691 VALUE ret = Qnil; 1692 1693 for (i = 0; i < argc; i++) 1694 { 1695 if (i > 0) rb_str_cat(str, ", ", 2); 1696 rb_str_concat(str, rb_inspect(argv[i])); 1697 } 1698 msg(RSTRING_PTR(str)); 1699 1700 if (argc == 1) 1701 ret = argv[0]; 1702 else if (argc > 1) 1703 ret = rb_ary_new4(argc, argv); 1704 return ret; 1705 } 1706 1707 static void 1708 ruby_io_init(void) 1709 { 1710 #ifndef DYNAMIC_RUBY 1711 RUBYEXTERN VALUE rb_stdout; 1712 RUBYEXTERN VALUE rb_stderr; 1713 #endif 1714 1715 rb_stdout = rb_obj_alloc(rb_cObject); 1716 rb_stderr = rb_obj_alloc(rb_cObject); 1717 rb_define_singleton_method(rb_stdout, "write", vim_message, 1); 1718 rb_define_singleton_method(rb_stdout, "flush", f_nop, 0); 1719 rb_define_singleton_method(rb_stderr, "write", vim_message, 1); 1720 rb_define_singleton_method(rb_stderr, "flush", f_nop, 0); 1721 rb_define_global_function("p", f_p, -1); 1722 } 1723 1724 static void 1725 ruby_vim_init(void) 1726 { 1727 objtbl = rb_hash_new(); 1728 rb_global_variable(&objtbl); 1729 1730 // The Vim module used to be called "VIM", but "Vim" is better. Make an 1731 // alias "VIM" for backwards compatibility. 1732 mVIM = rb_define_module("Vim"); 1733 rb_define_const(rb_cObject, "VIM", mVIM); 1734 rb_define_const(mVIM, "VERSION_MAJOR", INT2NUM(VIM_VERSION_MAJOR)); 1735 rb_define_const(mVIM, "VERSION_MINOR", INT2NUM(VIM_VERSION_MINOR)); 1736 rb_define_const(mVIM, "VERSION_BUILD", INT2NUM(VIM_VERSION_BUILD)); 1737 rb_define_const(mVIM, "VERSION_PATCHLEVEL", INT2NUM(VIM_VERSION_PATCHLEVEL)); 1738 rb_define_const(mVIM, "VERSION_SHORT", rb_str_new2(VIM_VERSION_SHORT)); 1739 rb_define_const(mVIM, "VERSION_MEDIUM", rb_str_new2(VIM_VERSION_MEDIUM)); 1740 rb_define_const(mVIM, "VERSION_LONG", rb_str_new2(VIM_VERSION_LONG)); 1741 rb_define_const(mVIM, "VERSION_LONG_DATE", rb_str_new2(VIM_VERSION_LONG_DATE)); 1742 rb_define_module_function(mVIM, "message", vim_message, 1); 1743 rb_define_module_function(mVIM, "set_option", vim_set_option, 1); 1744 rb_define_module_function(mVIM, "command", vim_command, 1); 1745 rb_define_module_function(mVIM, "evaluate", vim_evaluate, 1); 1746 rb_define_module_function(mVIM, "blob", vim_blob, 1); 1747 1748 eDeletedBufferError = rb_define_class_under(mVIM, "DeletedBufferError", 1749 rb_eStandardError); 1750 eDeletedWindowError = rb_define_class_under(mVIM, "DeletedWindowError", 1751 rb_eStandardError); 1752 1753 cBuffer = rb_define_class_under(mVIM, "Buffer", rb_cObject); 1754 rb_define_singleton_method(cBuffer, "current", buffer_s_current, 0); 1755 rb_define_singleton_method(cBuffer, "count", buffer_s_count, 0); 1756 rb_define_singleton_method(cBuffer, "[]", buffer_s_aref, 1); 1757 rb_define_method(cBuffer, "name", buffer_name, 0); 1758 rb_define_method(cBuffer, "number", buffer_number, 0); 1759 rb_define_method(cBuffer, "count", buffer_count, 0); 1760 rb_define_method(cBuffer, "length", buffer_count, 0); 1761 rb_define_method(cBuffer, "[]", buffer_aref, 1); 1762 rb_define_method(cBuffer, "[]=", buffer_aset, 2); 1763 rb_define_method(cBuffer, "delete", buffer_delete, 1); 1764 rb_define_method(cBuffer, "append", buffer_append, 2); 1765 1766 // Added line manipulation functions 1767 // SegPhault - 03/07/05 1768 rb_define_method(cBuffer, "line_number", current_line_number, 0); 1769 rb_define_method(cBuffer, "line", line_s_current, 0); 1770 rb_define_method(cBuffer, "line=", set_current_line, 1); 1771 1772 1773 cVimWindow = rb_define_class_under(mVIM, "Window", rb_cObject); 1774 rb_define_singleton_method(cVimWindow, "current", window_s_current, 0); 1775 rb_define_singleton_method(cVimWindow, "count", window_s_count, 0); 1776 rb_define_singleton_method(cVimWindow, "[]", window_s_aref, 1); 1777 rb_define_method(cVimWindow, "buffer", window_buffer, 0); 1778 rb_define_method(cVimWindow, "height", window_height, 0); 1779 rb_define_method(cVimWindow, "height=", window_set_height, 1); 1780 rb_define_method(cVimWindow, "width", window_width, 0); 1781 rb_define_method(cVimWindow, "width=", window_set_width, 1); 1782 rb_define_method(cVimWindow, "cursor", window_cursor, 0); 1783 rb_define_method(cVimWindow, "cursor=", window_set_cursor, 1); 1784 1785 rb_define_virtual_variable("$curbuf", buffer_s_current, 0); 1786 rb_define_virtual_variable("$curwin", window_s_current, 0); 1787 } 1788 1789 void 1790 vim_ruby_init(void *stack_start) 1791 { 1792 // should get machine stack start address early in main function 1793 ruby_stack_start = stack_start; 1794 } 1795 1796 static int 1797 convert_hash2dict(VALUE key, VALUE val, VALUE arg) 1798 { 1799 dict_T *d = (dict_T *)arg; 1800 dictitem_T *di; 1801 1802 di = dictitem_alloc((char_u *)RSTRING_PTR(RSTRING(rb_obj_as_string(key)))); 1803 if (di == NULL || ruby_convert_to_vim_value(val, &di->di_tv) != OK 1804 || dict_add(d, di) != OK) 1805 { 1806 d->dv_hashtab.ht_error = TRUE; 1807 return ST_STOP; 1808 } 1809 return ST_CONTINUE; 1810 } 1811 1812 static int 1813 ruby_convert_to_vim_value(VALUE val, typval_T *rettv) 1814 { 1815 switch (TYPE(val)) 1816 { 1817 case T_NIL: 1818 rettv->v_type = VAR_SPECIAL; 1819 rettv->vval.v_number = VVAL_NULL; 1820 break; 1821 case T_TRUE: 1822 rettv->v_type = VAR_BOOL; 1823 rettv->vval.v_number = VVAL_TRUE; 1824 break; 1825 case T_FALSE: 1826 rettv->v_type = VAR_BOOL; 1827 rettv->vval.v_number = VVAL_FALSE; 1828 break; 1829 case T_BIGNUM: 1830 case T_FIXNUM: 1831 rettv->v_type = VAR_NUMBER; 1832 rettv->vval.v_number = (varnumber_T)NUM2LONG(val); 1833 break; 1834 #ifdef FEAT_FLOAT 1835 case T_FLOAT: 1836 rettv->v_type = VAR_FLOAT; 1837 rettv->vval.v_float = (float_T)NUM2DBL(val); 1838 break; 1839 #endif 1840 default: 1841 val = rb_obj_as_string(val); 1842 // FALLTHROUGH 1843 case T_STRING: 1844 { 1845 VALUE str = (VALUE)RSTRING(val); 1846 1847 rettv->v_type = VAR_STRING; 1848 rettv->vval.v_string = vim_strnsave((char_u *)RSTRING_PTR(str), 1849 (int)RSTRING_LEN(str)); 1850 } 1851 break; 1852 case T_ARRAY: 1853 { 1854 list_T *l; 1855 long i; 1856 typval_T v; 1857 1858 l = list_alloc(); 1859 if (l == NULL) 1860 return FAIL; 1861 1862 for (i = 0; i < RARRAY_LEN(val); ++i) 1863 { 1864 if (ruby_convert_to_vim_value((VALUE)RARRAY_PTR(val)[i], 1865 &v) != OK) 1866 { 1867 list_unref(l); 1868 return FAIL; 1869 } 1870 list_append_tv(l, &v); 1871 clear_tv(&v); 1872 } 1873 1874 rettv->v_type = VAR_LIST; 1875 rettv->vval.v_list = l; 1876 ++l->lv_refcount; 1877 } 1878 break; 1879 case T_HASH: 1880 { 1881 dict_T *d; 1882 1883 d = dict_alloc(); 1884 if (d == NULL) 1885 return FAIL; 1886 1887 rb_hash_foreach(val, convert_hash2dict, (VALUE)d); 1888 if (d->dv_hashtab.ht_error) 1889 { 1890 dict_unref(d); 1891 return FAIL; 1892 } 1893 1894 rettv->v_type = VAR_DICT; 1895 rettv->vval.v_dict = d; 1896 ++d->dv_refcount; 1897 } 1898 break; 1899 } 1900 return OK; 1901 } 1902 1903 void 1904 do_rubyeval(char_u *str, typval_T *rettv) 1905 { 1906 int retval = FAIL; 1907 1908 if (ensure_ruby_initialized()) 1909 { 1910 int state; 1911 VALUE obj; 1912 1913 obj = rb_eval_string_protect((const char *)str, &state); 1914 if (state) 1915 error_print(state); 1916 else 1917 retval = ruby_convert_to_vim_value(obj, rettv); 1918 } 1919 if (retval == FAIL) 1920 { 1921 rettv->v_type = VAR_NUMBER; 1922 rettv->vval.v_number = 0; 1923 } 1924 } 1925