1 /* vi:set ts=8 sts=4 sw=4:
3 * VIM - Vi IMproved by Bram Moolenaar
5 * Ruby interface by Shugo Maeda
6 * with improvements by SegPhault (Ryan Paul)
8 * Do ":help uganda" in Vim to read copying and usage conditions.
9 * Do ":help credits" in Vim to see a list of people who contributed.
10 * See README.txt for an overview of the Vim source code.
17 # if !defined(DYNAMIC_RUBY_VER) || (DYNAMIC_RUBY_VER < 18)
21 # define IMPORT /* For static dll usage __declspec(dllimport) */
22 # define RUBYEXTERN __declspec(dllimport)
26 # define RUBYEXTERN extern
30 * This is tricky. In ruby.h there is (inline) function rb_class_of()
31 * definition. This function use these variables. But we want function to
32 * use dll_* variables.
35 # define rb_cFalseClass (*dll_rb_cFalseClass)
36 # define rb_cFixnum (*dll_rb_cFixnum)
37 # define rb_cNilClass (*dll_rb_cNilClass)
38 # define rb_cSymbol (*dll_rb_cSymbol)
39 # define rb_cTrueClass (*dll_rb_cTrueClass)
40 # if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 18
42 * On ver 1.8, all Ruby functions are exported with "__declspec(dllimport)"
43 * in ruby.h. But it causes trouble for these variables, because it is
44 * defined in this file. When defined this RUBY_EXPORT it modified to
45 * "extern" and be able to avoid this problem.
51 /* suggested by Ariya Mizutani */
52 #if (_MSC_VER == 1200)
56 #if (defined(RUBY_VERSION) && RUBY_VERSION >= 19) \
57 || (defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 19)
58 # define RUBY19_OR_LATER 1
61 #if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 19
62 /* Ruby 1.9 defines a number of static functions which use rb_num2long and
64 # define rb_num2long rb_num2long_stub
65 # define rb_int2big rb_int2big_stub
69 #ifdef RUBY19_OR_LATER
70 # include <ruby/encoding.h>
76 /* T_DATA defined both by Ruby and Mac header files, hack around it... */
77 #if defined(MACOS_X_UNIX) || defined(macintosh)
78 # define __OPENTRANSPORT__
79 # define __OPENTRANSPORTPROTOCOL__
80 # define __OPENTRANSPORTPROVIDERS__
84 * Backward compatiblity for Ruby 1.8 and earlier.
85 * Ruby 1.9 does not provide STR2CSTR, instead StringValuePtr is provided.
86 * Ruby 1.9 does not provide RXXX(s)->len and RXXX(s)->ptr, instead
87 * RXXX_LEN(s) and RXXX_PTR(s) are provided.
89 #ifndef StringValuePtr
90 # define StringValuePtr(s) STR2CSTR(s)
93 # define RARRAY_LEN(s) RARRAY(s)->len
96 # define RARRAY_PTR(s) RARRAY(s)->ptr
99 # define RSTRING_LEN(s) RSTRING(s)->len
102 # define RSTRING_PTR(s) RSTRING(s)->ptr
108 #if defined(PROTO) && !defined(FEAT_RUBY)
109 /* Define these to be able to generate the function prototypes. */
111 # define RUBY_DATA_FUNC int
114 static int ruby_initialized
= 0;
118 static VALUE cBuffer
;
119 static VALUE cVimWindow
;
120 static VALUE eDeletedBufferError
;
121 static VALUE eDeletedWindowError
;
123 static int ensure_ruby_initialized(void);
124 static void error_print(int);
125 static void ruby_io_init(void);
126 static void ruby_vim_init(void);
128 #if defined(DYNAMIC_RUBY) || defined(PROTO)
130 # define HINSTANCE int /* for generating prototypes */
136 #define rb_assoc_new dll_rb_assoc_new
137 #define rb_cObject (*dll_rb_cObject)
138 #define rb_check_type dll_rb_check_type
139 #define rb_class_path dll_rb_class_path
140 #define rb_data_object_alloc dll_rb_data_object_alloc
141 #define rb_define_class_under dll_rb_define_class_under
142 #define rb_define_const dll_rb_define_const
143 #define rb_define_global_function dll_rb_define_global_function
144 #define rb_define_method dll_rb_define_method
145 #define rb_define_module dll_rb_define_module
146 #define rb_define_module_function dll_rb_define_module_function
147 #define rb_define_singleton_method dll_rb_define_singleton_method
148 #define rb_define_virtual_variable dll_rb_define_virtual_variable
149 #define rb_stdout (*dll_rb_stdout)
150 #define rb_eArgError (*dll_rb_eArgError)
151 #define rb_eIndexError (*dll_rb_eIndexError)
152 #define rb_eRuntimeError (*dll_rb_eRuntimeError)
153 #define rb_eStandardError (*dll_rb_eStandardError)
154 #define rb_eval_string_protect dll_rb_eval_string_protect
155 #define rb_global_variable dll_rb_global_variable
156 #define rb_hash_aset dll_rb_hash_aset
157 #define rb_hash_new dll_rb_hash_new
158 #define rb_inspect dll_rb_inspect
159 #define rb_int2inum dll_rb_int2inum
160 #define rb_lastline_get dll_rb_lastline_get
161 #define rb_lastline_set dll_rb_lastline_set
162 #define rb_load_protect dll_rb_load_protect
163 #define rb_num2long dll_rb_num2long
164 #define rb_num2ulong dll_rb_num2ulong
165 #define rb_obj_alloc dll_rb_obj_alloc
166 #define rb_obj_as_string dll_rb_obj_as_string
167 #define rb_obj_id dll_rb_obj_id
168 #define rb_raise dll_rb_raise
169 #define rb_str2cstr dll_rb_str2cstr
170 #define rb_str_cat dll_rb_str_cat
171 #define rb_str_concat dll_rb_str_concat
172 #define rb_str_new dll_rb_str_new
173 #define rb_str_new2 dll_rb_str_new2
174 #if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 18
175 # define rb_string_value_ptr dll_rb_string_value_ptr
176 # define rb_float_new dll_rb_float_new
177 # define rb_ary_new dll_rb_ary_new
178 # define rb_ary_push dll_rb_ary_push
180 #ifdef RUBY19_OR_LATER
181 # define rb_errinfo dll_rb_errinfo
183 # define ruby_errinfo (*dll_ruby_errinfo)
185 #define ruby_init dll_ruby_init
186 #define ruby_init_loadpath dll_ruby_init_loadpath
187 #define NtInitialize dll_NtInitialize
188 #if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 18
189 # define rb_w32_snprintf dll_rb_w32_snprintf
192 #ifdef RUBY19_OR_LATER
193 # define ruby_script dll_ruby_script
194 # define rb_enc_find_index dll_rb_enc_find_index
195 # define rb_enc_find dll_rb_enc_find
196 # define rb_enc_str_new dll_rb_enc_str_new
197 # define rb_sprintf dll_rb_sprintf
198 # define ruby_init_stack dll_ruby_init_stack
202 * Pointers for dynamic link
204 static VALUE (*dll_rb_assoc_new
) (VALUE
, VALUE
);
205 static VALUE
*dll_rb_cFalseClass
;
206 static VALUE
*dll_rb_cFixnum
;
207 static VALUE
*dll_rb_cNilClass
;
208 static VALUE
*dll_rb_cObject
;
209 static VALUE
*dll_rb_cSymbol
;
210 static VALUE
*dll_rb_cTrueClass
;
211 static void (*dll_rb_check_type
) (VALUE
,int);
212 static VALUE (*dll_rb_class_path
) (VALUE
);
213 static VALUE (*dll_rb_data_object_alloc
) (VALUE
, void*, RUBY_DATA_FUNC
, RUBY_DATA_FUNC
);
214 static VALUE (*dll_rb_define_class_under
) (VALUE
, const char*, VALUE
);
215 static void (*dll_rb_define_const
) (VALUE
,const char*,VALUE
);
216 static void (*dll_rb_define_global_function
) (const char*,VALUE(*)(),int);
217 static void (*dll_rb_define_method
) (VALUE
,const char*,VALUE(*)(),int);
218 static VALUE (*dll_rb_define_module
) (const char*);
219 static void (*dll_rb_define_module_function
) (VALUE
,const char*,VALUE(*)(),int);
220 static void (*dll_rb_define_singleton_method
) (VALUE
,const char*,VALUE(*)(),int);
221 static void (*dll_rb_define_virtual_variable
) (const char*,VALUE(*)(),void(*)());
222 static VALUE
*dll_rb_stdout
;
223 static VALUE
*dll_rb_eArgError
;
224 static VALUE
*dll_rb_eIndexError
;
225 static VALUE
*dll_rb_eRuntimeError
;
226 static VALUE
*dll_rb_eStandardError
;
227 static VALUE (*dll_rb_eval_string_protect
) (const char*, int*);
228 static void (*dll_rb_global_variable
) (VALUE
*);
229 static VALUE (*dll_rb_hash_aset
) (VALUE
, VALUE
, VALUE
);
230 static VALUE (*dll_rb_hash_new
) (void);
231 static VALUE (*dll_rb_inspect
) (VALUE
);
232 static VALUE (*dll_rb_int2inum
) (long);
233 static VALUE (*dll_rb_int2inum
) (long);
234 static VALUE (*dll_rb_lastline_get
) (void);
235 static void (*dll_rb_lastline_set
) (VALUE
);
236 static void (*dll_rb_load_protect
) (VALUE
, int, int*);
237 static long (*dll_rb_num2long
) (VALUE
);
238 static unsigned long (*dll_rb_num2ulong
) (VALUE
);
239 static VALUE (*dll_rb_obj_alloc
) (VALUE
);
240 static VALUE (*dll_rb_obj_as_string
) (VALUE
);
241 static VALUE (*dll_rb_obj_id
) (VALUE
);
242 static void (*dll_rb_raise
) (VALUE
, const char*, ...);
243 static char *(*dll_rb_str2cstr
) (VALUE
,int*);
244 static VALUE (*dll_rb_str_cat
) (VALUE
, const char*, long);
245 static VALUE (*dll_rb_str_concat
) (VALUE
, VALUE
);
246 static VALUE (*dll_rb_str_new
) (const char*, long);
247 static VALUE (*dll_rb_str_new2
) (const char*);
248 #ifdef RUBY19_OR_LATER
249 static VALUE (*dll_rb_errinfo
) (void);
251 static VALUE
*dll_ruby_errinfo
;
253 static void (*dll_ruby_init
) (void);
254 static void (*dll_ruby_init_loadpath
) (void);
255 static void (*dll_NtInitialize
) (int*, char***);
256 #if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 18
257 static char * (*dll_rb_string_value_ptr
) (volatile VALUE
*);
258 static VALUE (*dll_rb_float_new
) (double);
259 static VALUE (*dll_rb_ary_new
) (void);
260 static VALUE (*dll_rb_ary_push
) (VALUE
, VALUE
);
262 #ifdef RUBY19_OR_LATER
263 static VALUE (*dll_rb_int2big
)(SIGNED_VALUE
);
265 #if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 18
266 static int (*dll_rb_w32_snprintf
)(char*, size_t, const char*, ...);
269 #ifdef RUBY19_OR_LATER
270 static void (*dll_ruby_script
) (const char*);
271 static int (*dll_rb_enc_find_index
) (const char*);
272 static rb_encoding
* (*dll_rb_enc_find
) (const char*);
273 static VALUE (*dll_rb_enc_str_new
) (const char*, long, rb_encoding
*);
274 static VALUE (*dll_rb_sprintf
) (const char*, ...);
275 static void (*ruby_init_stack
)(VALUE
*);
278 #ifdef RUBY19_OR_LATER
279 static SIGNED_VALUE
rb_num2long_stub(VALUE x
)
281 return dll_rb_num2long(x
);
283 static VALUE
rb_int2big_stub(SIGNED_VALUE x
)
285 return dll_rb_int2big(x
);
289 static HINSTANCE hinstRuby
= 0; /* Instance of ruby.dll */
292 * Table of name to function pointer of ruby.
294 #define RUBY_PROC FARPROC
299 } ruby_funcname_table
[] =
301 {"rb_assoc_new", (RUBY_PROC
*)&dll_rb_assoc_new
},
302 {"rb_cFalseClass", (RUBY_PROC
*)&dll_rb_cFalseClass
},
303 {"rb_cFixnum", (RUBY_PROC
*)&dll_rb_cFixnum
},
304 {"rb_cNilClass", (RUBY_PROC
*)&dll_rb_cNilClass
},
305 {"rb_cObject", (RUBY_PROC
*)&dll_rb_cObject
},
306 {"rb_cSymbol", (RUBY_PROC
*)&dll_rb_cSymbol
},
307 {"rb_cTrueClass", (RUBY_PROC
*)&dll_rb_cTrueClass
},
308 {"rb_check_type", (RUBY_PROC
*)&dll_rb_check_type
},
309 {"rb_class_path", (RUBY_PROC
*)&dll_rb_class_path
},
310 {"rb_data_object_alloc", (RUBY_PROC
*)&dll_rb_data_object_alloc
},
311 {"rb_define_class_under", (RUBY_PROC
*)&dll_rb_define_class_under
},
312 {"rb_define_const", (RUBY_PROC
*)&dll_rb_define_const
},
313 {"rb_define_global_function", (RUBY_PROC
*)&dll_rb_define_global_function
},
314 {"rb_define_method", (RUBY_PROC
*)&dll_rb_define_method
},
315 {"rb_define_module", (RUBY_PROC
*)&dll_rb_define_module
},
316 {"rb_define_module_function", (RUBY_PROC
*)&dll_rb_define_module_function
},
317 {"rb_define_singleton_method", (RUBY_PROC
*)&dll_rb_define_singleton_method
},
318 {"rb_define_virtual_variable", (RUBY_PROC
*)&dll_rb_define_virtual_variable
},
319 {"rb_stdout", (RUBY_PROC
*)&dll_rb_stdout
},
320 {"rb_eArgError", (RUBY_PROC
*)&dll_rb_eArgError
},
321 {"rb_eIndexError", (RUBY_PROC
*)&dll_rb_eIndexError
},
322 {"rb_eRuntimeError", (RUBY_PROC
*)&dll_rb_eRuntimeError
},
323 {"rb_eStandardError", (RUBY_PROC
*)&dll_rb_eStandardError
},
324 {"rb_eval_string_protect", (RUBY_PROC
*)&dll_rb_eval_string_protect
},
325 {"rb_global_variable", (RUBY_PROC
*)&dll_rb_global_variable
},
326 {"rb_hash_aset", (RUBY_PROC
*)&dll_rb_hash_aset
},
327 {"rb_hash_new", (RUBY_PROC
*)&dll_rb_hash_new
},
328 {"rb_inspect", (RUBY_PROC
*)&dll_rb_inspect
},
329 {"rb_int2inum", (RUBY_PROC
*)&dll_rb_int2inum
},
330 {"rb_lastline_get", (RUBY_PROC
*)&dll_rb_lastline_get
},
331 {"rb_lastline_set", (RUBY_PROC
*)&dll_rb_lastline_set
},
332 {"rb_load_protect", (RUBY_PROC
*)&dll_rb_load_protect
},
333 {"rb_num2long", (RUBY_PROC
*)&dll_rb_num2long
},
334 {"rb_num2ulong", (RUBY_PROC
*)&dll_rb_num2ulong
},
335 {"rb_obj_alloc", (RUBY_PROC
*)&dll_rb_obj_alloc
},
336 {"rb_obj_as_string", (RUBY_PROC
*)&dll_rb_obj_as_string
},
337 {"rb_obj_id", (RUBY_PROC
*)&dll_rb_obj_id
},
338 {"rb_raise", (RUBY_PROC
*)&dll_rb_raise
},
339 {"rb_str2cstr", (RUBY_PROC
*)&dll_rb_str2cstr
},
340 {"rb_str_cat", (RUBY_PROC
*)&dll_rb_str_cat
},
341 {"rb_str_concat", (RUBY_PROC
*)&dll_rb_str_concat
},
342 {"rb_str_new", (RUBY_PROC
*)&dll_rb_str_new
},
343 {"rb_str_new2", (RUBY_PROC
*)&dll_rb_str_new2
},
344 #ifdef RUBY19_OR_LATER
345 {"rb_errinfo", (RUBY_PROC
*)&dll_rb_errinfo
},
347 {"ruby_errinfo", (RUBY_PROC
*)&dll_ruby_errinfo
},
349 {"ruby_init", (RUBY_PROC
*)&dll_ruby_init
},
350 {"ruby_init_loadpath", (RUBY_PROC
*)&dll_ruby_init_loadpath
},
352 #if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER < 19
357 (RUBY_PROC
*)&dll_NtInitialize
},
358 #if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 18
359 {"rb_w32_snprintf", (RUBY_PROC
*)&dll_rb_w32_snprintf
},
361 #if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 18
362 {"rb_string_value_ptr", (RUBY_PROC
*)&dll_rb_string_value_ptr
},
363 {"rb_float_new", (RUBY_PROC
*)&dll_rb_float_new
},
364 {"rb_ary_new", (RUBY_PROC
*)&dll_rb_ary_new
},
365 {"rb_ary_push", (RUBY_PROC
*)&dll_rb_ary_push
},
367 #ifdef RUBY19_OR_LATER
368 {"rb_int2big", (RUBY_PROC
*)&dll_rb_int2big
},
369 {"ruby_script", (RUBY_PROC
*)&dll_ruby_script
},
370 {"rb_enc_find_index", (RUBY_PROC
*)&dll_rb_enc_find_index
},
371 {"rb_enc_find", (RUBY_PROC
*)&dll_rb_enc_find
},
372 {"rb_enc_str_new", (RUBY_PROC
*)&dll_rb_enc_str_new
},
373 {"rb_sprintf", (RUBY_PROC
*)&dll_rb_sprintf
},
374 {"ruby_init_stack", (RUBY_PROC
*)&dll_ruby_init_stack
},
387 FreeLibrary(hinstRuby
);
393 * Load library and get all pointers.
394 * Parameter 'libname' provides name of DLL.
398 ruby_runtime_link_init(char *libname
, int verbose
)
404 hinstRuby
= LoadLibrary(libname
);
408 EMSG2(_(e_loadlib
), libname
);
412 for (i
= 0; ruby_funcname_table
[i
].ptr
; ++i
)
414 if (!(*ruby_funcname_table
[i
].ptr
= GetProcAddress(hinstRuby
,
415 ruby_funcname_table
[i
].name
)))
417 FreeLibrary(hinstRuby
);
420 EMSG2(_(e_loadfunc
), ruby_funcname_table
[i
].name
);
428 * If ruby is enabled (there is installed ruby on Windows system) return TRUE,
432 ruby_enabled(verbose
)
435 return ruby_runtime_link_init(DYNAMIC_RUBY_DLL
, verbose
) == OK
;
437 #endif /* defined(DYNAMIC_RUBY) || defined(PROTO) */
447 void ex_ruby(exarg_T
*eap
)
452 script
= (char *)script_get(eap
, eap
->arg
);
453 if (!eap
->skip
&& ensure_ruby_initialized())
456 rb_eval_string_protect((char *)eap
->arg
, &state
);
458 rb_eval_string_protect(script
, &state
);
466 * In Ruby 1.9 or later, ruby String object has encoding.
467 * conversion buffer string of vim to ruby String object using
468 * VIM encoding option.
471 vim_str2rb_enc_str(const char *s
)
473 #ifdef RUBY19_OR_LATER
479 isnum
= get_option_value((char_u
*)"enc", &lval
, &sval
, 0);
482 enc
= rb_enc_find((char *)sval
);
485 return rb_enc_str_new(s
, strlen(s
), enc
);
489 return rb_str_new2(s
);
493 eval_enc_string_protect(const char *str
, int *state
)
495 #ifdef RUBY19_OR_LATER
502 isnum
= get_option_value((char_u
*)"enc", &lval
, &sval
, 0);
505 enc
= rb_enc_find((char *)sval
);
509 v
= rb_sprintf("#-*- coding:%s -*-\n%s", rb_enc_name(enc
), str
);
510 return rb_eval_string_protect(StringValuePtr(v
), state
);
514 return rb_eval_string_protect(str
, state
);
517 void ex_rubydo(exarg_T
*eap
)
522 if (ensure_ruby_initialized())
524 if (u_save(eap
->line1
- 1, eap
->line2
+ 1) != OK
)
526 for (i
= eap
->line1
; i
<= eap
->line2
; i
++) {
529 line
= oldline
= vim_str2rb_enc_str((char *)ml_get(i
));
530 rb_lastline_set(line
);
531 eval_enc_string_protect((char *) eap
->arg
, &state
);
536 line
= rb_lastline_get();
538 if (TYPE(line
) != T_STRING
) {
539 EMSG(_("E265: $_ must be an instance of String"));
542 ml_replace(i
, (char_u
*) StringValuePtr(line
), 1);
545 syn_changed(i
); /* recompute syntax hl. for this line */
550 update_curbuf(NOT_VALID
);
554 void ex_rubyfile(exarg_T
*eap
)
558 if (ensure_ruby_initialized())
560 rb_load_protect(rb_str_new2((char *) eap
->arg
), 0, &state
);
561 if (state
) error_print(state
);
565 void ruby_buffer_free(buf_T
*buf
)
569 rb_hash_aset(objtbl
, rb_obj_id((VALUE
) buf
->b_ruby_ref
), Qnil
);
570 RDATA(buf
->b_ruby_ref
)->data
= NULL
;
574 void ruby_window_free(win_T
*win
)
578 rb_hash_aset(objtbl
, rb_obj_id((VALUE
) win
->w_ruby_ref
), Qnil
);
579 RDATA(win
->w_ruby_ref
)->data
= NULL
;
583 static int ensure_ruby_initialized(void)
585 if (!ruby_initialized
)
588 if (ruby_enabled(TRUE
))
592 /* suggested by Ariya Mizutani */
594 char *argv
[] = {"gvim.exe"};
595 NtInitialize(&argc
, &argv
);
597 #ifdef RUBY19_OR_LATER
601 #ifdef RUBY19_OR_LATER
602 ruby_script("vim-ruby");
604 ruby_init_loadpath();
606 #ifdef RUBY19_OR_LATER
607 rb_enc_find_index("encdb");
610 ruby_initialized
= 1;
615 EMSG(_("E266: Sorry, this command is disabled, the Ruby library could not be loaded."));
620 return ruby_initialized
;
623 static void error_print(int state
)
626 #if !(defined(RUBY_VERSION) && RUBY_VERSION >= 19) \
627 && !(defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 19)
628 RUBYEXTERN VALUE ruby_errinfo
;
635 #define TAG_RETURN 0x1
636 #define TAG_BREAK 0x2
638 #define TAG_RETRY 0x4
640 #define TAG_RAISE 0x6
641 #define TAG_THROW 0x7
642 #define TAG_FATAL 0x8
647 EMSG(_("E267: unexpected return"));
650 EMSG(_("E268: unexpected next"));
653 EMSG(_("E269: unexpected break"));
656 EMSG(_("E270: unexpected redo"));
659 EMSG(_("E271: retry outside of rescue clause"));
663 #ifdef RUBY19_OR_LATER
664 eclass
= CLASS_OF(rb_errinfo());
665 einfo
= rb_obj_as_string(rb_errinfo());
667 eclass
= CLASS_OF(ruby_errinfo
);
668 einfo
= rb_obj_as_string(ruby_errinfo
);
670 if (eclass
== rb_eRuntimeError
&& RSTRING_LEN(einfo
) == 0) {
671 EMSG(_("E272: unhandled exception"));
677 epath
= rb_class_path(eclass
);
678 vim_snprintf(buff
, BUFSIZ
, "%s: %s",
679 RSTRING_PTR(epath
), RSTRING_PTR(einfo
));
680 p
= strchr(buff
, '\n');
686 vim_snprintf(buff
, BUFSIZ
, _("E273: unknown longjmp status %d"), state
);
692 static VALUE
vim_message(VALUE self UNUSED
, VALUE str
)
696 str
= rb_obj_as_string(str
);
697 buff
= ALLOCA_N(char, RSTRING_LEN(str
));
698 strcpy(buff
, RSTRING_PTR(str
));
699 p
= strchr(buff
, '\n');
705 static VALUE
vim_set_option(VALUE self UNUSED
, VALUE str
)
707 do_set((char_u
*)StringValuePtr(str
), 0);
708 update_screen(NOT_VALID
);
712 static VALUE
vim_command(VALUE self UNUSED
, VALUE str
)
714 do_cmdline_cmd((char_u
*)StringValuePtr(str
));
719 static VALUE
vim_to_ruby(typval_T
*tv
)
723 if (tv
->v_type
== VAR_STRING
)
725 result
= rb_str_new2(tv
->vval
.v_string
== NULL
726 ? "" : (char *)(tv
->vval
.v_string
));
728 else if (tv
->v_type
== VAR_NUMBER
)
730 result
= INT2NUM(tv
->vval
.v_number
);
733 else if (tv
->v_type
== VAR_FLOAT
)
735 result
= rb_float_new(tv
->vval
.v_float
);
738 else if (tv
->v_type
== VAR_LIST
)
740 list_T
*list
= tv
->vval
.v_list
;
743 result
= rb_ary_new();
747 for (curr
= list
->lv_first
; curr
!= NULL
; curr
= curr
->li_next
)
749 rb_ary_push(result
, vim_to_ruby(&curr
->li_tv
));
753 else if (tv
->v_type
== VAR_DICT
)
755 result
= rb_hash_new();
757 if (tv
->vval
.v_dict
!= NULL
)
759 hashtab_T
*ht
= &tv
->vval
.v_dict
->dv_hashtab
;
760 long_u todo
= ht
->ht_used
;
764 for (hi
= ht
->ht_array
; todo
> 0; ++hi
)
766 if (!HASHITEM_EMPTY(hi
))
770 di
= dict_lookup(hi
);
771 rb_hash_aset(result
, rb_str_new2((char *)hi
->hi_key
),
772 vim_to_ruby(&di
->di_tv
));
776 } /* else return Qnil; */
782 static VALUE
vim_evaluate(VALUE self UNUSED
, VALUE str
)
788 tv
= eval_expr((char_u
*)StringValuePtr(str
), NULL
);
793 result
= vim_to_ruby(tv
);
803 static VALUE
buffer_new(buf_T
*buf
)
807 return (VALUE
) buf
->b_ruby_ref
;
811 VALUE obj
= Data_Wrap_Struct(cBuffer
, 0, 0, buf
);
812 buf
->b_ruby_ref
= (void *) obj
;
813 rb_hash_aset(objtbl
, rb_obj_id(obj
), obj
);
818 static buf_T
*get_buf(VALUE obj
)
822 Data_Get_Struct(obj
, buf_T
, buf
);
824 rb_raise(eDeletedBufferError
, "attempt to refer to deleted buffer");
828 static VALUE
buffer_s_current()
830 return buffer_new(curbuf
);
833 static VALUE
buffer_s_count()
838 for (b
= firstbuf
; b
!= NULL
; b
= b
->b_next
)
840 /* Deleted buffers should not be counted
841 * SegPhault - 01/07/05 */
849 static VALUE
buffer_s_aref(VALUE self UNUSED
, VALUE num
)
852 int n
= NUM2INT(num
);
854 for (b
= firstbuf
; b
!= NULL
; b
= b
->b_next
)
856 /* Deleted buffers should not be counted
857 * SegPhault - 01/07/05 */
862 return buffer_new(b
);
869 static VALUE
buffer_name(VALUE self
)
871 buf_T
*buf
= get_buf(self
);
873 return buf
->b_ffname
? rb_str_new2((char *)buf
->b_ffname
) : Qnil
;
876 static VALUE
buffer_number(VALUE self
)
878 buf_T
*buf
= get_buf(self
);
880 return INT2NUM(buf
->b_fnum
);
883 static VALUE
buffer_count(VALUE self
)
885 buf_T
*buf
= get_buf(self
);
887 return INT2NUM(buf
->b_ml
.ml_line_count
);
890 static VALUE
get_buffer_line(buf_T
*buf
, linenr_T n
)
892 if (n
> 0 && n
<= buf
->b_ml
.ml_line_count
)
894 char *line
= (char *)ml_get_buf(buf
, n
, FALSE
);
895 return line
? vim_str2rb_enc_str(line
) : Qnil
;
897 rb_raise(rb_eIndexError
, "line number %ld out of range", (long)n
);
899 return Qnil
; /* For stop warning */
903 static VALUE
buffer_aref(VALUE self
, VALUE num
)
905 buf_T
*buf
= get_buf(self
);
908 return get_buffer_line(buf
, (linenr_T
)NUM2LONG(num
));
909 return Qnil
; /* For stop warning */
912 static VALUE
set_buffer_line(buf_T
*buf
, linenr_T n
, VALUE str
)
914 char *line
= StringValuePtr(str
);
917 if (n
> 0 && n
<= buf
->b_ml
.ml_line_count
&& line
!= NULL
)
919 /* set curwin/curbuf for "buf" and save some things */
920 aucmd_prepbuf(&aco
, buf
);
922 if (u_savesub(n
) == OK
) {
923 ml_replace(n
, (char_u
*)line
, TRUE
);
926 syn_changed(n
); /* recompute syntax hl. for this line */
930 /* restore curwin/curbuf and a few other things */
932 /* Careful: autocommands may have made "buf" invalid! */
934 update_curbuf(NOT_VALID
);
938 rb_raise(rb_eIndexError
, "line number %ld out of range", (long)n
);
940 return Qnil
; /* For stop warning */
946 static VALUE
buffer_aset(VALUE self
, VALUE num
, VALUE str
)
948 buf_T
*buf
= get_buf(self
);
951 return set_buffer_line(buf
, (linenr_T
)NUM2LONG(num
), str
);
955 static VALUE
buffer_delete(VALUE self
, VALUE num
)
957 buf_T
*buf
= get_buf(self
);
958 long n
= NUM2LONG(num
);
961 if (n
> 0 && n
<= buf
->b_ml
.ml_line_count
)
963 /* set curwin/curbuf for "buf" and save some things */
964 aucmd_prepbuf(&aco
, buf
);
966 if (u_savedel(n
, 1) == OK
) {
969 /* Changes to non-active buffers should properly refresh
970 * SegPhault - 01/09/05 */
971 deleted_lines_mark(n
, 1L);
976 /* restore curwin/curbuf and a few other things */
978 /* Careful: autocommands may have made "buf" invalid! */
980 update_curbuf(NOT_VALID
);
984 rb_raise(rb_eIndexError
, "line number %ld out of range", n
);
989 static VALUE
buffer_append(VALUE self
, VALUE num
, VALUE str
)
991 buf_T
*buf
= get_buf(self
);
992 char *line
= StringValuePtr(str
);
993 long n
= NUM2LONG(num
);
997 rb_raise(rb_eIndexError
, "NULL line");
999 else if (n
>= 0 && n
<= buf
->b_ml
.ml_line_count
)
1001 /* set curwin/curbuf for "buf" and save some things */
1002 aucmd_prepbuf(&aco
, buf
);
1004 if (u_inssub(n
+ 1) == OK
) {
1005 ml_append(n
, (char_u
*) line
, (colnr_T
) 0, FALSE
);
1007 /* Changes to non-active buffers should properly refresh screen
1008 * SegPhault - 12/20/04 */
1009 appended_lines_mark(n
, 1L);
1014 /* restore curwin/curbuf and a few other things */
1015 aucmd_restbuf(&aco
);
1016 /* Careful: autocommands may have made "buf" invalid! */
1018 update_curbuf(NOT_VALID
);
1021 rb_raise(rb_eIndexError
, "line number %ld out of range", n
);
1026 static VALUE
window_new(win_T
*win
)
1028 if (win
->w_ruby_ref
)
1030 return (VALUE
) win
->w_ruby_ref
;
1034 VALUE obj
= Data_Wrap_Struct(cVimWindow
, 0, 0, win
);
1035 win
->w_ruby_ref
= (void *) obj
;
1036 rb_hash_aset(objtbl
, rb_obj_id(obj
), obj
);
1041 static win_T
*get_win(VALUE obj
)
1045 Data_Get_Struct(obj
, win_T
, win
);
1047 rb_raise(eDeletedWindowError
, "attempt to refer to deleted window");
1051 static VALUE
window_s_current()
1053 return window_new(curwin
);
1057 * Added line manipulation functions
1058 * SegPhault - 03/07/05
1060 static VALUE
line_s_current()
1062 return get_buffer_line(curbuf
, curwin
->w_cursor
.lnum
);
1065 static VALUE
set_current_line(VALUE self UNUSED
, VALUE str
)
1067 return set_buffer_line(curbuf
, curwin
->w_cursor
.lnum
, str
);
1070 static VALUE
current_line_number()
1072 return INT2FIX((int)curwin
->w_cursor
.lnum
);
1077 static VALUE
window_s_count()
1083 for (w
= firstwin
; w
!= NULL
; w
= w
->w_next
)
1091 static VALUE
window_s_aref(VALUE self UNUSED
, VALUE num
)
1094 int n
= NUM2INT(num
);
1096 #ifndef FEAT_WINDOWS
1099 for (w
= firstwin
; w
!= NULL
; w
= w
->w_next
, --n
)
1102 return window_new(w
);
1106 static VALUE
window_buffer(VALUE self
)
1108 win_T
*win
= get_win(self
);
1110 return buffer_new(win
->w_buffer
);
1113 static VALUE
window_height(VALUE self
)
1115 win_T
*win
= get_win(self
);
1117 return INT2NUM(win
->w_height
);
1120 static VALUE
window_set_height(VALUE self
, VALUE height
)
1122 win_T
*win
= get_win(self
);
1123 win_T
*savewin
= curwin
;
1126 win_setheight(NUM2INT(height
));
1131 static VALUE
window_width(VALUE self
)
1133 win_T
*win
= get_win(self
);
1135 return INT2NUM(win
->w_width
);
1138 static VALUE
window_set_width(VALUE self
, VALUE width
)
1140 win_T
*win
= get_win(self
);
1141 win_T
*savewin
= curwin
;
1144 win_setwidth(NUM2INT(width
));
1149 static VALUE
window_cursor(VALUE self
)
1151 win_T
*win
= get_win(self
);
1153 return rb_assoc_new(INT2NUM(win
->w_cursor
.lnum
), INT2NUM(win
->w_cursor
.col
));
1156 static VALUE
window_set_cursor(VALUE self
, VALUE pos
)
1159 win_T
*win
= get_win(self
);
1161 Check_Type(pos
, T_ARRAY
);
1162 if (RARRAY_LEN(pos
) != 2)
1163 rb_raise(rb_eArgError
, "array length must be 2");
1164 lnum
= RARRAY_PTR(pos
)[0];
1165 col
= RARRAY_PTR(pos
)[1];
1166 win
->w_cursor
.lnum
= NUM2LONG(lnum
);
1167 win
->w_cursor
.col
= NUM2UINT(col
);
1168 check_cursor(); /* put cursor on an existing line */
1169 update_screen(NOT_VALID
);
1173 static VALUE
f_p(int argc
, VALUE
*argv
, VALUE self UNUSED
)
1176 VALUE str
= rb_str_new("", 0);
1178 for (i
= 0; i
< argc
; i
++) {
1179 if (i
> 0) rb_str_cat(str
, ", ", 2);
1180 rb_str_concat(str
, rb_inspect(argv
[i
]));
1182 MSG(RSTRING_PTR(str
));
1186 static void ruby_io_init(void)
1188 #ifndef DYNAMIC_RUBY
1189 RUBYEXTERN VALUE rb_stdout
;
1192 rb_stdout
= rb_obj_alloc(rb_cObject
);
1193 rb_define_singleton_method(rb_stdout
, "write", vim_message
, 1);
1194 rb_define_global_function("p", f_p
, -1);
1197 static void ruby_vim_init(void)
1199 objtbl
= rb_hash_new();
1200 rb_global_variable(&objtbl
);
1202 /* The Vim module used to be called "VIM", but "Vim" is better. Make an
1203 * alias "VIM" for backwards compatiblity. */
1204 mVIM
= rb_define_module("Vim");
1205 rb_define_const(rb_cObject
, "VIM", mVIM
);
1206 rb_define_const(mVIM
, "VERSION_MAJOR", INT2NUM(VIM_VERSION_MAJOR
));
1207 rb_define_const(mVIM
, "VERSION_MINOR", INT2NUM(VIM_VERSION_MINOR
));
1208 rb_define_const(mVIM
, "VERSION_BUILD", INT2NUM(VIM_VERSION_BUILD
));
1209 rb_define_const(mVIM
, "VERSION_PATCHLEVEL", INT2NUM(VIM_VERSION_PATCHLEVEL
));
1210 rb_define_const(mVIM
, "VERSION_SHORT", rb_str_new2(VIM_VERSION_SHORT
));
1211 rb_define_const(mVIM
, "VERSION_MEDIUM", rb_str_new2(VIM_VERSION_MEDIUM
));
1212 rb_define_const(mVIM
, "VERSION_LONG", rb_str_new2(VIM_VERSION_LONG
));
1213 rb_define_const(mVIM
, "VERSION_LONG_DATE", rb_str_new2(VIM_VERSION_LONG_DATE
));
1214 rb_define_module_function(mVIM
, "message", vim_message
, 1);
1215 rb_define_module_function(mVIM
, "set_option", vim_set_option
, 1);
1216 rb_define_module_function(mVIM
, "command", vim_command
, 1);
1217 rb_define_module_function(mVIM
, "evaluate", vim_evaluate
, 1);
1219 eDeletedBufferError
= rb_define_class_under(mVIM
, "DeletedBufferError",
1221 eDeletedWindowError
= rb_define_class_under(mVIM
, "DeletedWindowError",
1224 cBuffer
= rb_define_class_under(mVIM
, "Buffer", rb_cObject
);
1225 rb_define_singleton_method(cBuffer
, "current", buffer_s_current
, 0);
1226 rb_define_singleton_method(cBuffer
, "count", buffer_s_count
, 0);
1227 rb_define_singleton_method(cBuffer
, "[]", buffer_s_aref
, 1);
1228 rb_define_method(cBuffer
, "name", buffer_name
, 0);
1229 rb_define_method(cBuffer
, "number", buffer_number
, 0);
1230 rb_define_method(cBuffer
, "count", buffer_count
, 0);
1231 rb_define_method(cBuffer
, "length", buffer_count
, 0);
1232 rb_define_method(cBuffer
, "[]", buffer_aref
, 1);
1233 rb_define_method(cBuffer
, "[]=", buffer_aset
, 2);
1234 rb_define_method(cBuffer
, "delete", buffer_delete
, 1);
1235 rb_define_method(cBuffer
, "append", buffer_append
, 2);
1237 /* Added line manipulation functions
1238 * SegPhault - 03/07/05 */
1239 rb_define_method(cBuffer
, "line_number", current_line_number
, 0);
1240 rb_define_method(cBuffer
, "line", line_s_current
, 0);
1241 rb_define_method(cBuffer
, "line=", set_current_line
, 1);
1244 cVimWindow
= rb_define_class_under(mVIM
, "Window", rb_cObject
);
1245 rb_define_singleton_method(cVimWindow
, "current", window_s_current
, 0);
1246 rb_define_singleton_method(cVimWindow
, "count", window_s_count
, 0);
1247 rb_define_singleton_method(cVimWindow
, "[]", window_s_aref
, 1);
1248 rb_define_method(cVimWindow
, "buffer", window_buffer
, 0);
1249 rb_define_method(cVimWindow
, "height", window_height
, 0);
1250 rb_define_method(cVimWindow
, "height=", window_set_height
, 1);
1251 rb_define_method(cVimWindow
, "width", window_width
, 0);
1252 rb_define_method(cVimWindow
, "width=", window_set_width
, 1);
1253 rb_define_method(cVimWindow
, "cursor", window_cursor
, 0);
1254 rb_define_method(cVimWindow
, "cursor=", window_set_cursor
, 1);
1256 rb_define_virtual_variable("$curbuf", buffer_s_current
, 0);
1257 rb_define_virtual_variable("$curwin", window_s_current
, 0);