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