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