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(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 19
57 /* Ruby 1.9 defines a number of static functions which use rb_num2long and
59 # define rb_num2long rb_num2long_stub
60 # define rb_int2big rb_int2big_stub
64 #if defined(RUBY_VERSION) && RUBY_VERSION >= 19
65 # include <ruby/encoding.h>
71 /* T_DATA defined both by Ruby and Mac header files, hack around it... */
72 #if defined(MACOS_X_UNIX) || defined(macintosh)
73 # define __OPENTRANSPORT__
74 # define __OPENTRANSPORTPROTOCOL__
75 # define __OPENTRANSPORTPROVIDERS__
79 * Backward compatiblity for Ruby 1.8 and earlier.
80 * Ruby 1.9 does not provide STR2CSTR, instead StringValuePtr is provided.
81 * Ruby 1.9 does not provide RXXX(s)->len and RXXX(s)->ptr, instead
82 * RXXX_LEN(s) and RXXX_PTR(s) are provided.
84 #ifndef StringValuePtr
85 # define StringValuePtr(s) STR2CSTR(s)
88 # define RARRAY_LEN(s) RARRAY(s)->len
91 # define RARRAY_PTR(s) RARRAY(s)->ptr
94 # define RSTRING_LEN(s) RSTRING(s)->len
97 # define RSTRING_PTR(s) RSTRING(s)->ptr
103 #if defined(PROTO) && !defined(FEAT_RUBY)
104 /* Define these to be able to generate the function prototypes. */
106 # define RUBY_DATA_FUNC int
109 static int ruby_initialized
= 0;
113 static VALUE cBuffer
;
114 static VALUE cVimWindow
;
115 static VALUE eDeletedBufferError
;
116 static VALUE eDeletedWindowError
;
118 static int ensure_ruby_initialized(void);
119 static void error_print(int);
120 static void ruby_io_init(void);
121 static void ruby_vim_init(void);
123 #if defined(DYNAMIC_RUBY) || defined(PROTO)
125 # define HINSTANCE int /* for generating prototypes */
131 #define rb_assoc_new dll_rb_assoc_new
132 #define rb_cObject (*dll_rb_cObject)
133 #define rb_check_type dll_rb_check_type
134 #define rb_class_path dll_rb_class_path
135 #define rb_data_object_alloc dll_rb_data_object_alloc
136 #define rb_define_class_under dll_rb_define_class_under
137 #define rb_define_const dll_rb_define_const
138 #define rb_define_global_function dll_rb_define_global_function
139 #define rb_define_method dll_rb_define_method
140 #define rb_define_module dll_rb_define_module
141 #define rb_define_module_function dll_rb_define_module_function
142 #define rb_define_singleton_method dll_rb_define_singleton_method
143 #define rb_define_virtual_variable dll_rb_define_virtual_variable
144 #define rb_stdout (*dll_rb_stdout)
145 #define rb_eArgError (*dll_rb_eArgError)
146 #define rb_eIndexError (*dll_rb_eIndexError)
147 #define rb_eRuntimeError (*dll_rb_eRuntimeError)
148 #define rb_eStandardError (*dll_rb_eStandardError)
149 #define rb_eval_string_protect dll_rb_eval_string_protect
150 #define rb_global_variable dll_rb_global_variable
151 #define rb_hash_aset dll_rb_hash_aset
152 #define rb_hash_new dll_rb_hash_new
153 #define rb_inspect dll_rb_inspect
154 #define rb_int2inum dll_rb_int2inum
155 #define rb_lastline_get dll_rb_lastline_get
156 #define rb_lastline_set dll_rb_lastline_set
157 #define rb_load_protect dll_rb_load_protect
158 #define rb_num2long dll_rb_num2long
159 #define rb_num2ulong dll_rb_num2ulong
160 #define rb_obj_alloc dll_rb_obj_alloc
161 #define rb_obj_as_string dll_rb_obj_as_string
162 #define rb_obj_id dll_rb_obj_id
163 #define rb_raise dll_rb_raise
164 #define rb_str2cstr dll_rb_str2cstr
165 #define rb_str_cat dll_rb_str_cat
166 #define rb_str_concat dll_rb_str_concat
167 #define rb_str_new dll_rb_str_new
168 #define rb_str_new2 dll_rb_str_new2
169 #if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 18
170 # define rb_string_value_ptr dll_rb_string_value_ptr
171 # define rb_float_new dll_rb_float_new
172 # define rb_ary_new dll_rb_ary_new
173 # define rb_ary_push dll_rb_ary_push
175 #if defined(RUBY_VERSION) && RUBY_VERSION >= 19 \
176 || defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 19
177 # define rb_errinfo dll_rb_errinfo
179 # define ruby_errinfo (*dll_ruby_errinfo)
181 #define ruby_init dll_ruby_init
182 #define ruby_init_loadpath dll_ruby_init_loadpath
183 #define NtInitialize dll_NtInitialize
184 #if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 18
185 # define rb_w32_snprintf dll_rb_w32_snprintf
188 #if defined(RUBY_VERSION) && RUBY_VERSION >= 19
189 # define ruby_script dll_ruby_script
190 # define rb_enc_find_index dll_rb_enc_find_index
191 # define rb_enc_find dll_rb_enc_find
192 # define rb_enc_str_new dll_rb_enc_str_new
193 # define rb_sprintf dll_rb_sprintf
197 * Pointers for dynamic link
199 static VALUE (*dll_rb_assoc_new
) (VALUE
, VALUE
);
200 static VALUE
*dll_rb_cFalseClass
;
201 static VALUE
*dll_rb_cFixnum
;
202 static VALUE
*dll_rb_cNilClass
;
203 static VALUE
*dll_rb_cObject
;
204 static VALUE
*dll_rb_cSymbol
;
205 static VALUE
*dll_rb_cTrueClass
;
206 static void (*dll_rb_check_type
) (VALUE
,int);
207 static VALUE (*dll_rb_class_path
) (VALUE
);
208 static VALUE (*dll_rb_data_object_alloc
) (VALUE
, void*, RUBY_DATA_FUNC
, RUBY_DATA_FUNC
);
209 static VALUE (*dll_rb_define_class_under
) (VALUE
, const char*, VALUE
);
210 static void (*dll_rb_define_const
) (VALUE
,const char*,VALUE
);
211 static void (*dll_rb_define_global_function
) (const char*,VALUE(*)(),int);
212 static void (*dll_rb_define_method
) (VALUE
,const char*,VALUE(*)(),int);
213 static VALUE (*dll_rb_define_module
) (const char*);
214 static void (*dll_rb_define_module_function
) (VALUE
,const char*,VALUE(*)(),int);
215 static void (*dll_rb_define_singleton_method
) (VALUE
,const char*,VALUE(*)(),int);
216 static void (*dll_rb_define_virtual_variable
) (const char*,VALUE(*)(),void(*)());
217 static VALUE
*dll_rb_stdout
;
218 static VALUE
*dll_rb_eArgError
;
219 static VALUE
*dll_rb_eIndexError
;
220 static VALUE
*dll_rb_eRuntimeError
;
221 static VALUE
*dll_rb_eStandardError
;
222 static VALUE (*dll_rb_eval_string_protect
) (const char*, int*);
223 static void (*dll_rb_global_variable
) (VALUE
*);
224 static VALUE (*dll_rb_hash_aset
) (VALUE
, VALUE
, VALUE
);
225 static VALUE (*dll_rb_hash_new
) (void);
226 static VALUE (*dll_rb_inspect
) (VALUE
);
227 static VALUE (*dll_rb_int2inum
) (long);
228 static VALUE (*dll_rb_int2inum
) (long);
229 static VALUE (*dll_rb_lastline_get
) (void);
230 static void (*dll_rb_lastline_set
) (VALUE
);
231 static void (*dll_rb_load_protect
) (VALUE
, int, int*);
232 static long (*dll_rb_num2long
) (VALUE
);
233 static unsigned long (*dll_rb_num2ulong
) (VALUE
);
234 static VALUE (*dll_rb_obj_alloc
) (VALUE
);
235 static VALUE (*dll_rb_obj_as_string
) (VALUE
);
236 static VALUE (*dll_rb_obj_id
) (VALUE
);
237 static void (*dll_rb_raise
) (VALUE
, const char*, ...);
238 static char *(*dll_rb_str2cstr
) (VALUE
,int*);
239 static VALUE (*dll_rb_str_cat
) (VALUE
, const char*, long);
240 static VALUE (*dll_rb_str_concat
) (VALUE
, VALUE
);
241 static VALUE (*dll_rb_str_new
) (const char*, long);
242 static VALUE (*dll_rb_str_new2
) (const char*);
243 #if defined(RUBY_VERSION) && RUBY_VERSION >= 19 \
244 || defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 19
245 static VALUE (*dll_rb_errinfo
) (void);
247 static VALUE
*dll_ruby_errinfo
;
249 static void (*dll_ruby_init
) (void);
250 static void (*dll_ruby_init_loadpath
) (void);
251 static void (*dll_NtInitialize
) (int*, char***);
252 #if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 18
253 static char * (*dll_rb_string_value_ptr
) (volatile VALUE
*);
254 static VALUE (*dll_rb_float_new
) (double);
255 static VALUE (*dll_rb_ary_new
) (void);
256 static VALUE (*dll_rb_ary_push
) (VALUE
, VALUE
);
258 #if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 19
259 static VALUE (*dll_rb_int2big
)(SIGNED_VALUE
);
261 #if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 18
262 static int (*dll_rb_w32_snprintf
)(char*, size_t, const char*, ...);
265 #if defined(RUBY_VERSION) && RUBY_VERSION >= 19
266 static void (*dll_ruby_script
) (const char*);
267 static int (*dll_rb_enc_find_index
) (const char*);
268 static rb_encoding
* (*dll_rb_enc_find
) (const char*);
269 static VALUE (*dll_rb_enc_str_new
) (const char*, long, rb_encoding
*);
270 static VALUE (*dll_rb_sprintf
) (const char*, ...);
273 #if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 19
274 static SIGNED_VALUE
rb_num2long_stub(VALUE x
)
276 return dll_rb_num2long(x
);
278 static VALUE
rb_int2big_stub(SIGNED_VALUE x
)
280 return dll_rb_int2big(x
);
284 static HINSTANCE hinstRuby
= 0; /* Instance of ruby.dll */
287 * Table of name to function pointer of ruby.
289 #define RUBY_PROC FARPROC
294 } ruby_funcname_table
[] =
296 {"rb_assoc_new", (RUBY_PROC
*)&dll_rb_assoc_new
},
297 {"rb_cFalseClass", (RUBY_PROC
*)&dll_rb_cFalseClass
},
298 {"rb_cFixnum", (RUBY_PROC
*)&dll_rb_cFixnum
},
299 {"rb_cNilClass", (RUBY_PROC
*)&dll_rb_cNilClass
},
300 {"rb_cObject", (RUBY_PROC
*)&dll_rb_cObject
},
301 {"rb_cSymbol", (RUBY_PROC
*)&dll_rb_cSymbol
},
302 {"rb_cTrueClass", (RUBY_PROC
*)&dll_rb_cTrueClass
},
303 {"rb_check_type", (RUBY_PROC
*)&dll_rb_check_type
},
304 {"rb_class_path", (RUBY_PROC
*)&dll_rb_class_path
},
305 {"rb_data_object_alloc", (RUBY_PROC
*)&dll_rb_data_object_alloc
},
306 {"rb_define_class_under", (RUBY_PROC
*)&dll_rb_define_class_under
},
307 {"rb_define_const", (RUBY_PROC
*)&dll_rb_define_const
},
308 {"rb_define_global_function", (RUBY_PROC
*)&dll_rb_define_global_function
},
309 {"rb_define_method", (RUBY_PROC
*)&dll_rb_define_method
},
310 {"rb_define_module", (RUBY_PROC
*)&dll_rb_define_module
},
311 {"rb_define_module_function", (RUBY_PROC
*)&dll_rb_define_module_function
},
312 {"rb_define_singleton_method", (RUBY_PROC
*)&dll_rb_define_singleton_method
},
313 {"rb_define_virtual_variable", (RUBY_PROC
*)&dll_rb_define_virtual_variable
},
314 {"rb_stdout", (RUBY_PROC
*)&dll_rb_stdout
},
315 {"rb_eArgError", (RUBY_PROC
*)&dll_rb_eArgError
},
316 {"rb_eIndexError", (RUBY_PROC
*)&dll_rb_eIndexError
},
317 {"rb_eRuntimeError", (RUBY_PROC
*)&dll_rb_eRuntimeError
},
318 {"rb_eStandardError", (RUBY_PROC
*)&dll_rb_eStandardError
},
319 {"rb_eval_string_protect", (RUBY_PROC
*)&dll_rb_eval_string_protect
},
320 {"rb_global_variable", (RUBY_PROC
*)&dll_rb_global_variable
},
321 {"rb_hash_aset", (RUBY_PROC
*)&dll_rb_hash_aset
},
322 {"rb_hash_new", (RUBY_PROC
*)&dll_rb_hash_new
},
323 {"rb_inspect", (RUBY_PROC
*)&dll_rb_inspect
},
324 {"rb_int2inum", (RUBY_PROC
*)&dll_rb_int2inum
},
325 {"rb_lastline_get", (RUBY_PROC
*)&dll_rb_lastline_get
},
326 {"rb_lastline_set", (RUBY_PROC
*)&dll_rb_lastline_set
},
327 {"rb_load_protect", (RUBY_PROC
*)&dll_rb_load_protect
},
328 {"rb_num2long", (RUBY_PROC
*)&dll_rb_num2long
},
329 {"rb_num2ulong", (RUBY_PROC
*)&dll_rb_num2ulong
},
330 {"rb_obj_alloc", (RUBY_PROC
*)&dll_rb_obj_alloc
},
331 {"rb_obj_as_string", (RUBY_PROC
*)&dll_rb_obj_as_string
},
332 {"rb_obj_id", (RUBY_PROC
*)&dll_rb_obj_id
},
333 {"rb_raise", (RUBY_PROC
*)&dll_rb_raise
},
334 {"rb_str2cstr", (RUBY_PROC
*)&dll_rb_str2cstr
},
335 {"rb_str_cat", (RUBY_PROC
*)&dll_rb_str_cat
},
336 {"rb_str_concat", (RUBY_PROC
*)&dll_rb_str_concat
},
337 {"rb_str_new", (RUBY_PROC
*)&dll_rb_str_new
},
338 {"rb_str_new2", (RUBY_PROC
*)&dll_rb_str_new2
},
339 #if defined(RUBY_VERSION) && RUBY_VERSION >= 19 \
340 || defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 19
341 {"rb_errinfo", (RUBY_PROC
*)&dll_rb_errinfo
},
343 {"ruby_errinfo", (RUBY_PROC
*)&dll_ruby_errinfo
},
345 {"ruby_init", (RUBY_PROC
*)&dll_ruby_init
},
346 {"ruby_init_loadpath", (RUBY_PROC
*)&dll_ruby_init_loadpath
},
348 #if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER < 19
353 (RUBY_PROC
*)&dll_NtInitialize
},
354 #if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 18
355 {"rb_w32_snprintf", (RUBY_PROC
*)&dll_rb_w32_snprintf
},
357 #if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 18
358 {"rb_string_value_ptr", (RUBY_PROC
*)&dll_rb_string_value_ptr
},
359 {"rb_float_new", (RUBY_PROC
*)&dll_rb_float_new
},
360 {"rb_ary_new", (RUBY_PROC
*)&dll_rb_ary_new
},
361 {"rb_ary_push", (RUBY_PROC
*)&dll_rb_ary_push
},
363 #if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 19
364 {"rb_int2big", (RUBY_PROC
*)&dll_rb_int2big
},
366 #if defined(RUBY_VERSION) && RUBY_VERSION >= 19
367 {"ruby_script", (RUBY_PROC
*)&dll_ruby_script
},
368 {"rb_enc_find_index", (RUBY_PROC
*)&dll_rb_enc_find_index
},
369 {"rb_enc_find", (RUBY_PROC
*)&dll_rb_enc_find
},
370 {"rb_enc_str_new", (RUBY_PROC
*)&dll_rb_enc_str_new
},
371 {"rb_sprintf", (RUBY_PROC
*)&dll_rb_sprintf
},
384 FreeLibrary(hinstRuby
);
390 * Load library and get all pointers.
391 * Parameter 'libname' provides name of DLL.
395 ruby_runtime_link_init(char *libname
, int verbose
)
401 hinstRuby
= LoadLibrary(libname
);
405 EMSG2(_(e_loadlib
), libname
);
409 for (i
= 0; ruby_funcname_table
[i
].ptr
; ++i
)
411 if (!(*ruby_funcname_table
[i
].ptr
= GetProcAddress(hinstRuby
,
412 ruby_funcname_table
[i
].name
)))
414 FreeLibrary(hinstRuby
);
417 EMSG2(_(e_loadfunc
), ruby_funcname_table
[i
].name
);
425 * If ruby is enabled (there is installed ruby on Windows system) return TRUE,
429 ruby_enabled(verbose
)
432 return ruby_runtime_link_init(DYNAMIC_RUBY_DLL
, verbose
) == OK
;
434 #endif /* defined(DYNAMIC_RUBY) || defined(PROTO) */
444 void ex_ruby(exarg_T
*eap
)
449 script
= (char *)script_get(eap
, eap
->arg
);
450 if (!eap
->skip
&& ensure_ruby_initialized())
453 rb_eval_string_protect((char *)eap
->arg
, &state
);
455 rb_eval_string_protect(script
, &state
);
463 * In Ruby 1.9 or later, ruby String object has encoding.
464 * conversion buffer string of vim to ruby String object using
465 * VIM encoding option.
468 vim_str2rb_enc_str(const char *s
)
470 #if defined(RUBY_VERSION) && RUBY_VERSION >= 19
476 isnum
= get_option_value((char_u
*)"enc", &lval
, &sval
, 0);
479 enc
= rb_enc_find((char *)sval
);
482 return rb_enc_str_new(s
, strlen(s
), enc
);
486 return rb_str_new2(s
);
490 eval_enc_string_protect(const char *str
, int *state
)
492 #if defined(RUBY_VERSION) && RUBY_VERSION >= 19
499 isnum
= get_option_value((char_u
*)"enc", &lval
, &sval
, 0);
502 enc
= rb_enc_find((char *)sval
);
506 v
= rb_sprintf("#-*- coding:%s -*-\n%s", rb_enc_name(enc
), str
);
507 return rb_eval_string_protect(StringValuePtr(v
), state
);
511 return rb_eval_string_protect(str
, state
);
514 void ex_rubydo(exarg_T
*eap
)
519 if (ensure_ruby_initialized())
521 if (u_save(eap
->line1
- 1, eap
->line2
+ 1) != OK
)
523 for (i
= eap
->line1
; i
<= eap
->line2
; i
++) {
526 line
= oldline
= vim_str2rb_enc_str((char *)ml_get(i
));
527 rb_lastline_set(line
);
528 eval_enc_string_protect((char *) eap
->arg
, &state
);
533 line
= rb_lastline_get();
535 if (TYPE(line
) != T_STRING
) {
536 EMSG(_("E265: $_ must be an instance of String"));
539 ml_replace(i
, (char_u
*) StringValuePtr(line
), 1);
542 syn_changed(i
); /* recompute syntax hl. for this line */
547 update_curbuf(NOT_VALID
);
551 void ex_rubyfile(exarg_T
*eap
)
555 if (ensure_ruby_initialized())
557 rb_load_protect(rb_str_new2((char *) eap
->arg
), 0, &state
);
558 if (state
) error_print(state
);
562 void ruby_buffer_free(buf_T
*buf
)
566 rb_hash_aset(objtbl
, rb_obj_id((VALUE
) buf
->b_ruby_ref
), Qnil
);
567 RDATA(buf
->b_ruby_ref
)->data
= NULL
;
571 void ruby_window_free(win_T
*win
)
575 rb_hash_aset(objtbl
, rb_obj_id((VALUE
) win
->w_ruby_ref
), Qnil
);
576 RDATA(win
->w_ruby_ref
)->data
= NULL
;
580 static int ensure_ruby_initialized(void)
582 if (!ruby_initialized
)
585 if (ruby_enabled(TRUE
))
589 /* suggested by Ariya Mizutani */
591 char *argv
[] = {"gvim.exe"};
592 NtInitialize(&argc
, &argv
);
594 #if defined(RUBY_VERSION) && RUBY_VERSION >= 19
598 #if defined(RUBY_VERSION) && RUBY_VERSION >= 19
599 ruby_script("vim-ruby");
601 ruby_init_loadpath();
603 #if defined(RUBY_VERSION) && RUBY_VERSION >= 19
604 rb_enc_find_index("encdb");
607 ruby_initialized
= 1;
612 EMSG(_("E266: Sorry, this command is disabled, the Ruby library could not be loaded."));
617 return ruby_initialized
;
620 static void error_print(int state
)
623 #if !(defined(RUBY_VERSION) && RUBY_VERSION >= 19) \
624 && !(defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 19)
625 RUBYEXTERN VALUE ruby_errinfo
;
632 #define TAG_RETURN 0x1
633 #define TAG_BREAK 0x2
635 #define TAG_RETRY 0x4
637 #define TAG_RAISE 0x6
638 #define TAG_THROW 0x7
639 #define TAG_FATAL 0x8
644 EMSG(_("E267: unexpected return"));
647 EMSG(_("E268: unexpected next"));
650 EMSG(_("E269: unexpected break"));
653 EMSG(_("E270: unexpected redo"));
656 EMSG(_("E271: retry outside of rescue clause"));
660 #if defined(RUBY_VERSION) && RUBY_VERSION >= 19 \
661 || defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 19
662 eclass
= CLASS_OF(rb_errinfo());
663 einfo
= rb_obj_as_string(rb_errinfo());
665 eclass
= CLASS_OF(ruby_errinfo
);
666 einfo
= rb_obj_as_string(ruby_errinfo
);
668 if (eclass
== rb_eRuntimeError
&& RSTRING_LEN(einfo
) == 0) {
669 EMSG(_("E272: unhandled exception"));
675 epath
= rb_class_path(eclass
);
676 vim_snprintf(buff
, BUFSIZ
, "%s: %s",
677 RSTRING_PTR(epath
), RSTRING_PTR(einfo
));
678 p
= strchr(buff
, '\n');
684 vim_snprintf(buff
, BUFSIZ
, _("E273: unknown longjmp status %d"), state
);
690 static VALUE
vim_message(VALUE self UNUSED
, VALUE str
)
694 str
= rb_obj_as_string(str
);
695 buff
= ALLOCA_N(char, RSTRING_LEN(str
));
696 strcpy(buff
, RSTRING_PTR(str
));
697 p
= strchr(buff
, '\n');
703 static VALUE
vim_set_option(VALUE self UNUSED
, VALUE str
)
705 do_set((char_u
*)StringValuePtr(str
), 0);
706 update_screen(NOT_VALID
);
710 static VALUE
vim_command(VALUE self UNUSED
, VALUE str
)
712 do_cmdline_cmd((char_u
*)StringValuePtr(str
));
717 static VALUE
vim_to_ruby(typval_T
*tv
)
721 if (tv
->v_type
== VAR_STRING
)
723 result
= rb_str_new2((char *)tv
->vval
.v_string
);
725 else if (tv
->v_type
== VAR_NUMBER
)
727 result
= INT2NUM(tv
->vval
.v_number
);
730 else if (tv
->v_type
== VAR_FLOAT
)
732 result
= rb_float_new(tv
->vval
.v_float
);
735 else if (tv
->v_type
== VAR_LIST
)
737 list_T
*list
= tv
->vval
.v_list
;
740 result
= rb_ary_new();
744 for (curr
= list
->lv_first
; curr
!= NULL
; curr
= curr
->li_next
)
746 rb_ary_push(result
, vim_to_ruby(&curr
->li_tv
));
750 else if (tv
->v_type
== VAR_DICT
)
752 result
= rb_hash_new();
754 if (tv
->vval
.v_dict
!= NULL
)
756 hashtab_T
*ht
= &tv
->vval
.v_dict
->dv_hashtab
;
757 long_u todo
= ht
->ht_used
;
761 for (hi
= ht
->ht_array
; todo
> 0; ++hi
)
763 if (!HASHITEM_EMPTY(hi
))
767 di
= dict_lookup(hi
);
768 rb_hash_aset(result
, rb_str_new2((char *)hi
->hi_key
),
769 vim_to_ruby(&di
->di_tv
));
773 } /* else return Qnil; */
779 static VALUE
vim_evaluate(VALUE self UNUSED
, VALUE str
)
785 tv
= eval_expr((char_u
*)StringValuePtr(str
), NULL
);
790 result
= vim_to_ruby(tv
);
800 static VALUE
buffer_new(buf_T
*buf
)
804 return (VALUE
) buf
->b_ruby_ref
;
808 VALUE obj
= Data_Wrap_Struct(cBuffer
, 0, 0, buf
);
809 buf
->b_ruby_ref
= (void *) obj
;
810 rb_hash_aset(objtbl
, rb_obj_id(obj
), obj
);
815 static buf_T
*get_buf(VALUE obj
)
819 Data_Get_Struct(obj
, buf_T
, buf
);
821 rb_raise(eDeletedBufferError
, "attempt to refer to deleted buffer");
825 static VALUE
buffer_s_current()
827 return buffer_new(curbuf
);
830 static VALUE
buffer_s_count()
835 for (b
= firstbuf
; b
!= NULL
; b
= b
->b_next
)
837 /* Deleted buffers should not be counted
838 * SegPhault - 01/07/05 */
846 static VALUE
buffer_s_aref(VALUE self UNUSED
, VALUE num
)
849 int n
= NUM2INT(num
);
851 for (b
= firstbuf
; b
!= NULL
; b
= b
->b_next
)
853 /* Deleted buffers should not be counted
854 * SegPhault - 01/07/05 */
859 return buffer_new(b
);
866 static VALUE
buffer_name(VALUE self
)
868 buf_T
*buf
= get_buf(self
);
870 return buf
->b_ffname
? rb_str_new2((char *)buf
->b_ffname
) : Qnil
;
873 static VALUE
buffer_number(VALUE self
)
875 buf_T
*buf
= get_buf(self
);
877 return INT2NUM(buf
->b_fnum
);
880 static VALUE
buffer_count(VALUE self
)
882 buf_T
*buf
= get_buf(self
);
884 return INT2NUM(buf
->b_ml
.ml_line_count
);
887 static VALUE
get_buffer_line(buf_T
*buf
, linenr_T n
)
889 if (n
> 0 && n
<= buf
->b_ml
.ml_line_count
)
891 char *line
= (char *)ml_get_buf(buf
, n
, FALSE
);
892 return line
? vim_str2rb_enc_str(line
) : Qnil
;
894 rb_raise(rb_eIndexError
, "line number %ld out of range", (long)n
);
896 return Qnil
; /* For stop warning */
900 static VALUE
buffer_aref(VALUE self
, VALUE num
)
902 buf_T
*buf
= get_buf(self
);
905 return get_buffer_line(buf
, (linenr_T
)NUM2LONG(num
));
906 return Qnil
; /* For stop warning */
909 static VALUE
set_buffer_line(buf_T
*buf
, linenr_T n
, VALUE str
)
911 char *line
= StringValuePtr(str
);
914 if (n
> 0 && n
<= buf
->b_ml
.ml_line_count
&& line
!= NULL
)
916 /* set curwin/curbuf for "buf" and save some things */
917 aucmd_prepbuf(&aco
, buf
);
919 if (u_savesub(n
) == OK
) {
920 ml_replace(n
, (char_u
*)line
, TRUE
);
923 syn_changed(n
); /* recompute syntax hl. for this line */
927 /* restore curwin/curbuf and a few other things */
929 /* Careful: autocommands may have made "buf" invalid! */
931 update_curbuf(NOT_VALID
);
935 rb_raise(rb_eIndexError
, "line number %ld out of range", (long)n
);
937 return Qnil
; /* For stop warning */
943 static VALUE
buffer_aset(VALUE self
, VALUE num
, VALUE str
)
945 buf_T
*buf
= get_buf(self
);
948 return set_buffer_line(buf
, (linenr_T
)NUM2LONG(num
), str
);
952 static VALUE
buffer_delete(VALUE self
, VALUE num
)
954 buf_T
*buf
= get_buf(self
);
955 long n
= NUM2LONG(num
);
958 if (n
> 0 && n
<= buf
->b_ml
.ml_line_count
)
960 /* set curwin/curbuf for "buf" and save some things */
961 aucmd_prepbuf(&aco
, buf
);
963 if (u_savedel(n
, 1) == OK
) {
966 /* Changes to non-active buffers should properly refresh
967 * SegPhault - 01/09/05 */
968 deleted_lines_mark(n
, 1L);
973 /* restore curwin/curbuf and a few other things */
975 /* Careful: autocommands may have made "buf" invalid! */
977 update_curbuf(NOT_VALID
);
981 rb_raise(rb_eIndexError
, "line number %ld out of range", n
);
986 static VALUE
buffer_append(VALUE self
, VALUE num
, VALUE str
)
988 buf_T
*buf
= get_buf(self
);
989 char *line
= StringValuePtr(str
);
990 long n
= NUM2LONG(num
);
994 rb_raise(rb_eIndexError
, "NULL line");
996 else if (n
>= 0 && n
<= buf
->b_ml
.ml_line_count
)
998 /* set curwin/curbuf for "buf" and save some things */
999 aucmd_prepbuf(&aco
, buf
);
1001 if (u_inssub(n
+ 1) == OK
) {
1002 ml_append(n
, (char_u
*) line
, (colnr_T
) 0, FALSE
);
1004 /* Changes to non-active buffers should properly refresh screen
1005 * SegPhault - 12/20/04 */
1006 appended_lines_mark(n
, 1L);
1011 /* restore curwin/curbuf and a few other things */
1012 aucmd_restbuf(&aco
);
1013 /* Careful: autocommands may have made "buf" invalid! */
1015 update_curbuf(NOT_VALID
);
1018 rb_raise(rb_eIndexError
, "line number %ld out of range", n
);
1023 static VALUE
window_new(win_T
*win
)
1025 if (win
->w_ruby_ref
)
1027 return (VALUE
) win
->w_ruby_ref
;
1031 VALUE obj
= Data_Wrap_Struct(cVimWindow
, 0, 0, win
);
1032 win
->w_ruby_ref
= (void *) obj
;
1033 rb_hash_aset(objtbl
, rb_obj_id(obj
), obj
);
1038 static win_T
*get_win(VALUE obj
)
1042 Data_Get_Struct(obj
, win_T
, win
);
1044 rb_raise(eDeletedWindowError
, "attempt to refer to deleted window");
1048 static VALUE
window_s_current()
1050 return window_new(curwin
);
1054 * Added line manipulation functions
1055 * SegPhault - 03/07/05
1057 static VALUE
line_s_current()
1059 return get_buffer_line(curbuf
, curwin
->w_cursor
.lnum
);
1062 static VALUE
set_current_line(VALUE self UNUSED
, VALUE str
)
1064 return set_buffer_line(curbuf
, curwin
->w_cursor
.lnum
, str
);
1067 static VALUE
current_line_number()
1069 return INT2FIX((int)curwin
->w_cursor
.lnum
);
1074 static VALUE
window_s_count()
1080 for (w
= firstwin
; w
!= NULL
; w
= w
->w_next
)
1088 static VALUE
window_s_aref(VALUE self UNUSED
, VALUE num
)
1091 int n
= NUM2INT(num
);
1093 #ifndef FEAT_WINDOWS
1096 for (w
= firstwin
; w
!= NULL
; w
= w
->w_next
, --n
)
1099 return window_new(w
);
1103 static VALUE
window_buffer(VALUE self
)
1105 win_T
*win
= get_win(self
);
1107 return buffer_new(win
->w_buffer
);
1110 static VALUE
window_height(VALUE self
)
1112 win_T
*win
= get_win(self
);
1114 return INT2NUM(win
->w_height
);
1117 static VALUE
window_set_height(VALUE self
, VALUE height
)
1119 win_T
*win
= get_win(self
);
1120 win_T
*savewin
= curwin
;
1123 win_setheight(NUM2INT(height
));
1128 static VALUE
window_width(VALUE self
)
1130 win_T
*win
= get_win(self
);
1132 return INT2NUM(win
->w_width
);
1135 static VALUE
window_set_width(VALUE self
, VALUE width
)
1137 win_T
*win
= get_win(self
);
1138 win_T
*savewin
= curwin
;
1141 win_setwidth(NUM2INT(width
));
1146 static VALUE
window_cursor(VALUE self
)
1148 win_T
*win
= get_win(self
);
1150 return rb_assoc_new(INT2NUM(win
->w_cursor
.lnum
), INT2NUM(win
->w_cursor
.col
));
1153 static VALUE
window_set_cursor(VALUE self
, VALUE pos
)
1156 win_T
*win
= get_win(self
);
1158 Check_Type(pos
, T_ARRAY
);
1159 if (RARRAY_LEN(pos
) != 2)
1160 rb_raise(rb_eArgError
, "array length must be 2");
1161 lnum
= RARRAY_PTR(pos
)[0];
1162 col
= RARRAY_PTR(pos
)[1];
1163 win
->w_cursor
.lnum
= NUM2LONG(lnum
);
1164 win
->w_cursor
.col
= NUM2UINT(col
);
1165 check_cursor(); /* put cursor on an existing line */
1166 update_screen(NOT_VALID
);
1170 static VALUE
f_p(int argc
, VALUE
*argv
, VALUE self UNUSED
)
1173 VALUE str
= rb_str_new("", 0);
1175 for (i
= 0; i
< argc
; i
++) {
1176 if (i
> 0) rb_str_cat(str
, ", ", 2);
1177 rb_str_concat(str
, rb_inspect(argv
[i
]));
1179 MSG(RSTRING_PTR(str
));
1183 static void ruby_io_init(void)
1185 #ifndef DYNAMIC_RUBY
1186 RUBYEXTERN VALUE rb_stdout
;
1189 rb_stdout
= rb_obj_alloc(rb_cObject
);
1190 rb_define_singleton_method(rb_stdout
, "write", vim_message
, 1);
1191 rb_define_global_function("p", f_p
, -1);
1194 static void ruby_vim_init(void)
1196 objtbl
= rb_hash_new();
1197 rb_global_variable(&objtbl
);
1199 /* The Vim module used to be called "VIM", but "Vim" is better. Make an
1200 * alias "VIM" for backwards compatiblity. */
1201 mVIM
= rb_define_module("Vim");
1202 rb_define_const(rb_cObject
, "VIM", mVIM
);
1203 rb_define_const(mVIM
, "VERSION_MAJOR", INT2NUM(VIM_VERSION_MAJOR
));
1204 rb_define_const(mVIM
, "VERSION_MINOR", INT2NUM(VIM_VERSION_MINOR
));
1205 rb_define_const(mVIM
, "VERSION_BUILD", INT2NUM(VIM_VERSION_BUILD
));
1206 rb_define_const(mVIM
, "VERSION_PATCHLEVEL", INT2NUM(VIM_VERSION_PATCHLEVEL
));
1207 rb_define_const(mVIM
, "VERSION_SHORT", rb_str_new2(VIM_VERSION_SHORT
));
1208 rb_define_const(mVIM
, "VERSION_MEDIUM", rb_str_new2(VIM_VERSION_MEDIUM
));
1209 rb_define_const(mVIM
, "VERSION_LONG", rb_str_new2(VIM_VERSION_LONG
));
1210 rb_define_const(mVIM
, "VERSION_LONG_DATE", rb_str_new2(VIM_VERSION_LONG_DATE
));
1211 rb_define_module_function(mVIM
, "message", vim_message
, 1);
1212 rb_define_module_function(mVIM
, "set_option", vim_set_option
, 1);
1213 rb_define_module_function(mVIM
, "command", vim_command
, 1);
1214 rb_define_module_function(mVIM
, "evaluate", vim_evaluate
, 1);
1216 eDeletedBufferError
= rb_define_class_under(mVIM
, "DeletedBufferError",
1218 eDeletedWindowError
= rb_define_class_under(mVIM
, "DeletedWindowError",
1221 cBuffer
= rb_define_class_under(mVIM
, "Buffer", rb_cObject
);
1222 rb_define_singleton_method(cBuffer
, "current", buffer_s_current
, 0);
1223 rb_define_singleton_method(cBuffer
, "count", buffer_s_count
, 0);
1224 rb_define_singleton_method(cBuffer
, "[]", buffer_s_aref
, 1);
1225 rb_define_method(cBuffer
, "name", buffer_name
, 0);
1226 rb_define_method(cBuffer
, "number", buffer_number
, 0);
1227 rb_define_method(cBuffer
, "count", buffer_count
, 0);
1228 rb_define_method(cBuffer
, "length", buffer_count
, 0);
1229 rb_define_method(cBuffer
, "[]", buffer_aref
, 1);
1230 rb_define_method(cBuffer
, "[]=", buffer_aset
, 2);
1231 rb_define_method(cBuffer
, "delete", buffer_delete
, 1);
1232 rb_define_method(cBuffer
, "append", buffer_append
, 2);
1234 /* Added line manipulation functions
1235 * SegPhault - 03/07/05 */
1236 rb_define_method(cBuffer
, "line_number", current_line_number
, 0);
1237 rb_define_method(cBuffer
, "line", line_s_current
, 0);
1238 rb_define_method(cBuffer
, "line=", set_current_line
, 1);
1241 cVimWindow
= rb_define_class_under(mVIM
, "Window", rb_cObject
);
1242 rb_define_singleton_method(cVimWindow
, "current", window_s_current
, 0);
1243 rb_define_singleton_method(cVimWindow
, "count", window_s_count
, 0);
1244 rb_define_singleton_method(cVimWindow
, "[]", window_s_aref
, 1);
1245 rb_define_method(cVimWindow
, "buffer", window_buffer
, 0);
1246 rb_define_method(cVimWindow
, "height", window_height
, 0);
1247 rb_define_method(cVimWindow
, "height=", window_set_height
, 1);
1248 rb_define_method(cVimWindow
, "width", window_width
, 0);
1249 rb_define_method(cVimWindow
, "width=", window_set_width
, 1);
1250 rb_define_method(cVimWindow
, "cursor", window_cursor
, 0);
1251 rb_define_method(cVimWindow
, "cursor=", window_set_cursor
, 1);
1253 rb_define_virtual_variable("$curbuf", buffer_s_current
, 0);
1254 rb_define_virtual_variable("$curwin", window_s_current
, 0);