802e1c1c7c07b0d81554d06ccf6c8119f1dca076
[MacVim.git] / src / if_ruby.c
blob802e1c1c7c07b0d81554d06ccf6c8119f1dca076
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.
13 #include <stdio.h>
14 #include <string.h>
16 #ifdef _WIN32
17 # if !defined(DYNAMIC_RUBY_VER) || (DYNAMIC_RUBY_VER < 18)
18 # define NT
19 # endif
20 # ifndef DYNAMIC_RUBY
21 # define IMPORT /* For static dll usage __declspec(dllimport) */
22 # define RUBYEXTERN __declspec(dllimport)
23 # endif
24 #endif
25 #ifndef RUBYEXTERN
26 # define RUBYEXTERN extern
27 #endif
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.
34 #ifdef DYNAMIC_RUBY
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.
47 # define RUBY_EXPORT
48 # endif
49 #endif
51 /* suggested by Ariya Mizutani */
52 #if (_MSC_VER == 1200)
53 # undef _WIN32_WINNT
54 #endif
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
58 * rb_int2big */
59 # define rb_num2long rb_num2long_stub
60 # define rb_int2big rb_int2big_stub
61 #endif
63 #include <ruby.h>
64 #if defined(RUBY_VERSION) && RUBY_VERSION >= 19
65 # include <ruby/encoding.h>
66 #endif
68 #undef EXTERN
69 #undef _
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__
76 #endif
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)
86 #endif
87 #ifndef RARRAY_LEN
88 # define RARRAY_LEN(s) RARRAY(s)->len
89 #endif
90 #ifndef RARRAY_PTR
91 # define RARRAY_PTR(s) RARRAY(s)->ptr
92 #endif
93 #ifndef RSTRING_LEN
94 # define RSTRING_LEN(s) RSTRING(s)->len
95 #endif
96 #ifndef RSTRING_PTR
97 # define RSTRING_PTR(s) RSTRING(s)->ptr
98 #endif
100 #include "vim.h"
101 #include "version.h"
103 #if defined(PROTO) && !defined(FEAT_RUBY)
104 /* Define these to be able to generate the function prototypes. */
105 # define VALUE int
106 # define RUBY_DATA_FUNC int
107 #endif
109 static int ruby_initialized = 0;
110 static VALUE objtbl;
112 static VALUE mVIM;
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)
124 #ifdef PROTO
125 # define HINSTANCE int /* for generating prototypes */
126 #endif
129 * Wrapper defines
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
174 #endif
175 #if defined(RUBY_VERSION) && RUBY_VERSION >= 19 \
176 || defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 19
177 # define rb_errinfo dll_rb_errinfo
178 #else
179 # define ruby_errinfo (*dll_ruby_errinfo)
180 #endif
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
186 #endif
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
194 #endif
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);
246 #else
247 static VALUE *dll_ruby_errinfo;
248 #endif
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);
257 #endif
258 #if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 19
259 static VALUE (*dll_rb_int2big)(SIGNED_VALUE);
260 #endif
261 #if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 18
262 static int (*dll_rb_w32_snprintf)(char*, size_t, const char*, ...);
263 #endif
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*, ...);
271 #endif
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);
282 #endif
284 static HINSTANCE hinstRuby = 0; /* Instance of ruby.dll */
287 * Table of name to function pointer of ruby.
289 #define RUBY_PROC FARPROC
290 static struct
292 char *name;
293 RUBY_PROC *ptr;
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},
342 #else
343 {"ruby_errinfo", (RUBY_PROC*)&dll_ruby_errinfo},
344 #endif
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
349 "NtInitialize",
350 #else
351 "ruby_sysinit",
352 #endif
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},
356 #endif
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},
362 #endif
363 #if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 19
364 {"rb_int2big", (RUBY_PROC*)&dll_rb_int2big},
365 #endif
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},
372 #endif
373 {"", NULL},
377 * Free ruby.dll
379 static void
380 end_dynamic_ruby()
382 if (hinstRuby)
384 FreeLibrary(hinstRuby);
385 hinstRuby = 0;
390 * Load library and get all pointers.
391 * Parameter 'libname' provides name of DLL.
392 * Return OK or FAIL.
394 static int
395 ruby_runtime_link_init(char *libname, int verbose)
397 int i;
399 if (hinstRuby)
400 return OK;
401 hinstRuby = LoadLibrary(libname);
402 if (!hinstRuby)
404 if (verbose)
405 EMSG2(_(e_loadlib), libname);
406 return FAIL;
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);
415 hinstRuby = 0;
416 if (verbose)
417 EMSG2(_(e_loadfunc), ruby_funcname_table[i].name);
418 return FAIL;
421 return OK;
425 * If ruby is enabled (there is installed ruby on Windows system) return TRUE,
426 * else FALSE.
429 ruby_enabled(verbose)
430 int verbose;
432 return ruby_runtime_link_init(DYNAMIC_RUBY_DLL, verbose) == OK;
434 #endif /* defined(DYNAMIC_RUBY) || defined(PROTO) */
436 void
437 ruby_end()
439 #ifdef DYNAMIC_RUBY
440 end_dynamic_ruby();
441 #endif
444 void ex_ruby(exarg_T *eap)
446 int state;
447 char *script = NULL;
449 script = (char *)script_get(eap, eap->arg);
450 if (!eap->skip && ensure_ruby_initialized())
452 if (script == NULL)
453 rb_eval_string_protect((char *)eap->arg, &state);
454 else
455 rb_eval_string_protect(script, &state);
456 if (state)
457 error_print(state);
459 vim_free(script);
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.
467 static VALUE
468 vim_str2rb_enc_str(const char *s)
470 #if defined(RUBY_VERSION) && RUBY_VERSION >= 19
471 int isnum;
472 long lval;
473 char_u *sval;
474 rb_encoding *enc;
476 isnum = get_option_value((char_u *)"enc", &lval, &sval, 0);
477 if (isnum == 0)
479 enc = rb_enc_find((char *)sval);
480 vim_free(sval);
481 if (enc) {
482 return rb_enc_str_new(s, strlen(s), enc);
485 #endif
486 return rb_str_new2(s);
489 static VALUE
490 eval_enc_string_protect(const char *str, int *state)
492 #if defined(RUBY_VERSION) && RUBY_VERSION >= 19
493 int isnum;
494 long lval;
495 char_u *sval;
496 rb_encoding *enc;
497 VALUE v;
499 isnum = get_option_value((char_u *)"enc", &lval, &sval, 0);
500 if (isnum == 0)
502 enc = rb_enc_find((char *)sval);
503 vim_free(sval);
504 if (enc)
506 v = rb_sprintf("#-*- coding:%s -*-\n%s", rb_enc_name(enc), str);
507 return rb_eval_string_protect(StringValuePtr(v), state);
510 #endif
511 return rb_eval_string_protect(str, state);
514 void ex_rubydo(exarg_T *eap)
516 int state;
517 linenr_T i;
519 if (ensure_ruby_initialized())
521 if (u_save(eap->line1 - 1, eap->line2 + 1) != OK)
522 return;
523 for (i = eap->line1; i <= eap->line2; i++) {
524 VALUE line, oldline;
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);
529 if (state) {
530 error_print(state);
531 break;
533 line = rb_lastline_get();
534 if (!NIL_P(line)) {
535 if (TYPE(line) != T_STRING) {
536 EMSG(_("E265: $_ must be an instance of String"));
537 return;
539 ml_replace(i, (char_u *) StringValuePtr(line), 1);
540 changed();
541 #ifdef SYNTAX_HL
542 syn_changed(i); /* recompute syntax hl. for this line */
543 #endif
546 check_cursor();
547 update_curbuf(NOT_VALID);
551 void ex_rubyfile(exarg_T *eap)
553 int state;
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)
564 if (buf->b_ruby_ref)
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)
573 if (win->w_ruby_ref)
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)
584 #ifdef DYNAMIC_RUBY
585 if (ruby_enabled(TRUE))
587 #endif
588 #ifdef _WIN32
589 /* suggested by Ariya Mizutani */
590 int argc = 1;
591 char *argv[] = {"gvim.exe"};
592 NtInitialize(&argc, &argv);
593 #endif
594 #if defined(RUBY_VERSION) && RUBY_VERSION >= 19
595 RUBY_INIT_STACK;
596 #endif
597 ruby_init();
598 #if defined(RUBY_VERSION) && RUBY_VERSION >= 19
599 ruby_script("vim-ruby");
600 #endif
601 ruby_init_loadpath();
602 ruby_io_init();
603 #if defined(RUBY_VERSION) && RUBY_VERSION >= 19
604 rb_enc_find_index("encdb");
605 #endif
606 ruby_vim_init();
607 ruby_initialized = 1;
608 #ifdef DYNAMIC_RUBY
610 else
612 EMSG(_("E266: Sorry, this command is disabled, the Ruby library could not be loaded."));
613 return 0;
615 #endif
617 return ruby_initialized;
620 static void error_print(int state)
622 #ifndef DYNAMIC_RUBY
623 #if !(defined(RUBY_VERSION) && RUBY_VERSION >= 19) \
624 && !(defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 19)
625 RUBYEXTERN VALUE ruby_errinfo;
626 #endif
627 #endif
628 VALUE eclass;
629 VALUE einfo;
630 char buff[BUFSIZ];
632 #define TAG_RETURN 0x1
633 #define TAG_BREAK 0x2
634 #define TAG_NEXT 0x3
635 #define TAG_RETRY 0x4
636 #define TAG_REDO 0x5
637 #define TAG_RAISE 0x6
638 #define TAG_THROW 0x7
639 #define TAG_FATAL 0x8
640 #define TAG_MASK 0xf
642 switch (state) {
643 case TAG_RETURN:
644 EMSG(_("E267: unexpected return"));
645 break;
646 case TAG_NEXT:
647 EMSG(_("E268: unexpected next"));
648 break;
649 case TAG_BREAK:
650 EMSG(_("E269: unexpected break"));
651 break;
652 case TAG_REDO:
653 EMSG(_("E270: unexpected redo"));
654 break;
655 case TAG_RETRY:
656 EMSG(_("E271: retry outside of rescue clause"));
657 break;
658 case TAG_RAISE:
659 case TAG_FATAL:
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());
664 #else
665 eclass = CLASS_OF(ruby_errinfo);
666 einfo = rb_obj_as_string(ruby_errinfo);
667 #endif
668 if (eclass == rb_eRuntimeError && RSTRING_LEN(einfo) == 0) {
669 EMSG(_("E272: unhandled exception"));
671 else {
672 VALUE epath;
673 char *p;
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');
679 if (p) *p = '\0';
680 EMSG(buff);
682 break;
683 default:
684 vim_snprintf(buff, BUFSIZ, _("E273: unknown longjmp status %d"), state);
685 EMSG(buff);
686 break;
690 static VALUE vim_message(VALUE self UNUSED, VALUE str)
692 char *buff, *p;
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');
698 if (p) *p = '\0';
699 MSG(buff);
700 return Qnil;
703 static VALUE vim_set_option(VALUE self UNUSED, VALUE str)
705 do_set((char_u *)StringValuePtr(str), 0);
706 update_screen(NOT_VALID);
707 return Qnil;
710 static VALUE vim_command(VALUE self UNUSED, VALUE str)
712 do_cmdline_cmd((char_u *)StringValuePtr(str));
713 return Qnil;
716 #ifdef FEAT_EVAL
717 static VALUE vim_to_ruby(typval_T *tv)
719 VALUE result = Qnil;
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);
729 # ifdef FEAT_FLOAT
730 else if (tv->v_type == VAR_FLOAT)
732 result = rb_float_new(tv->vval.v_float);
734 # endif
735 else if (tv->v_type == VAR_LIST)
737 list_T *list = tv->vval.v_list;
738 listitem_T *curr;
740 result = rb_ary_new();
742 if (list != NULL)
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;
758 hashitem_T *hi;
759 dictitem_T *di;
761 for (hi = ht->ht_array; todo > 0; ++hi)
763 if (!HASHITEM_EMPTY(hi))
765 --todo;
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; */
775 return result;
777 #endif
779 static VALUE vim_evaluate(VALUE self UNUSED, VALUE str)
781 #ifdef FEAT_EVAL
782 typval_T *tv;
783 VALUE result;
785 tv = eval_expr((char_u *)StringValuePtr(str), NULL);
786 if (tv == NULL)
788 return Qnil;
790 result = vim_to_ruby(tv);
792 free_tv(tv);
794 return result;
795 #else
796 return Qnil;
797 #endif
800 static VALUE buffer_new(buf_T *buf)
802 if (buf->b_ruby_ref)
804 return (VALUE) buf->b_ruby_ref;
806 else
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);
811 return obj;
815 static buf_T *get_buf(VALUE obj)
817 buf_T *buf;
819 Data_Get_Struct(obj, buf_T, buf);
820 if (buf == NULL)
821 rb_raise(eDeletedBufferError, "attempt to refer to deleted buffer");
822 return buf;
825 static VALUE buffer_s_current()
827 return buffer_new(curbuf);
830 static VALUE buffer_s_count()
832 buf_T *b;
833 int n = 0;
835 for (b = firstbuf; b != NULL; b = b->b_next)
837 /* Deleted buffers should not be counted
838 * SegPhault - 01/07/05 */
839 if (b->b_p_bl)
840 n++;
843 return INT2NUM(n);
846 static VALUE buffer_s_aref(VALUE self UNUSED, VALUE num)
848 buf_T *b;
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 */
855 if (!b->b_p_bl)
856 continue;
858 if (n == 0)
859 return buffer_new(b);
861 n--;
863 return Qnil;
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);
895 #ifndef __GNUC__
896 return Qnil; /* For stop warning */
897 #endif
900 static VALUE buffer_aref(VALUE self, VALUE num)
902 buf_T *buf = get_buf(self);
904 if (buf != NULL)
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);
912 aco_save_T aco;
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);
921 changed();
922 #ifdef SYNTAX_HL
923 syn_changed(n); /* recompute syntax hl. for this line */
924 #endif
927 /* restore curwin/curbuf and a few other things */
928 aucmd_restbuf(&aco);
929 /* Careful: autocommands may have made "buf" invalid! */
931 update_curbuf(NOT_VALID);
933 else
935 rb_raise(rb_eIndexError, "line number %ld out of range", (long)n);
936 #ifndef __GNUC__
937 return Qnil; /* For stop warning */
938 #endif
940 return str;
943 static VALUE buffer_aset(VALUE self, VALUE num, VALUE str)
945 buf_T *buf = get_buf(self);
947 if (buf != NULL)
948 return set_buffer_line(buf, (linenr_T)NUM2LONG(num), str);
949 return str;
952 static VALUE buffer_delete(VALUE self, VALUE num)
954 buf_T *buf = get_buf(self);
955 long n = NUM2LONG(num);
956 aco_save_T aco;
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) {
964 ml_delete(n, 0);
966 /* Changes to non-active buffers should properly refresh
967 * SegPhault - 01/09/05 */
968 deleted_lines_mark(n, 1L);
970 changed();
973 /* restore curwin/curbuf and a few other things */
974 aucmd_restbuf(&aco);
975 /* Careful: autocommands may have made "buf" invalid! */
977 update_curbuf(NOT_VALID);
979 else
981 rb_raise(rb_eIndexError, "line number %ld out of range", n);
983 return Qnil;
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);
991 aco_save_T aco;
993 if (line == NULL) {
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);
1008 changed();
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);
1017 else {
1018 rb_raise(rb_eIndexError, "line number %ld out of range", n);
1020 return str;
1023 static VALUE window_new(win_T *win)
1025 if (win->w_ruby_ref)
1027 return (VALUE) win->w_ruby_ref;
1029 else
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);
1034 return obj;
1038 static win_T *get_win(VALUE obj)
1040 win_T *win;
1042 Data_Get_Struct(obj, win_T, win);
1043 if (win == NULL)
1044 rb_raise(eDeletedWindowError, "attempt to refer to deleted window");
1045 return win;
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()
1076 #ifdef FEAT_WINDOWS
1077 win_T *w;
1078 int n = 0;
1080 for (w = firstwin; w != NULL; w = w->w_next)
1081 n++;
1082 return INT2NUM(n);
1083 #else
1084 return INT2NUM(1);
1085 #endif
1088 static VALUE window_s_aref(VALUE self UNUSED, VALUE num)
1090 win_T *w;
1091 int n = NUM2INT(num);
1093 #ifndef FEAT_WINDOWS
1094 w = curwin;
1095 #else
1096 for (w = firstwin; w != NULL; w = w->w_next, --n)
1097 #endif
1098 if (n == 0)
1099 return window_new(w);
1100 return Qnil;
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;
1122 curwin = win;
1123 win_setheight(NUM2INT(height));
1124 curwin = savewin;
1125 return 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;
1140 curwin = win;
1141 win_setwidth(NUM2INT(width));
1142 curwin = savewin;
1143 return 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)
1155 VALUE lnum, col;
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);
1167 return Qnil;
1170 static VALUE f_p(int argc, VALUE *argv, VALUE self UNUSED)
1172 int i;
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));
1180 return Qnil;
1183 static void ruby_io_init(void)
1185 #ifndef DYNAMIC_RUBY
1186 RUBYEXTERN VALUE rb_stdout;
1187 #endif
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",
1217 rb_eStandardError);
1218 eDeletedWindowError = rb_define_class_under(mVIM, "DeletedWindowError",
1219 rb_eStandardError);
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);