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