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