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