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