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