6d28de37e54809831a459a68b2c3493d94118d3f
[MacVim.git] / src / if_ruby.c
blob6d28de37e54809831a459a68b2c3493d94118d3f
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 "__declspce(dllimport)"
43 * in ruby.h. But it cause 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 #include <ruby.h>
57 #if defined(RUBY_VERSION) && RUBY_VERSION >= 19
58 # include <ruby/encoding.h>
59 #endif
61 #undef EXTERN
62 #undef _
64 /* T_DATA defined both by Ruby and Mac header files, hack around it... */
65 #if defined(MACOS_X_UNIX) || defined(macintosh)
66 # define __OPENTRANSPORT__
67 # define __OPENTRANSPORTPROTOCOL__
68 # define __OPENTRANSPORTPROVIDERS__
69 #endif
72 * Backward compatiblity for Ruby 1.8 and earlier.
73 * Ruby 1.9 does not provide STR2CSTR, instead StringValuePtr is provided.
74 * Ruby 1.9 does not provide RXXX(s)->len and RXXX(s)->ptr, instead
75 * RXXX_LEN(s) and RXXX_PTR(s) are provided.
77 #ifndef StringValuePtr
78 # define StringValuePtr(s) STR2CSTR(s)
79 #endif
80 #ifndef RARRAY_LEN
81 # define RARRAY_LEN(s) RARRAY(s)->len
82 #endif
83 #ifndef RARRAY_PTR
84 # define RARRAY_PTR(s) RARRAY(s)->ptr
85 #endif
86 #ifndef RSTRING_LEN
87 # define RSTRING_LEN(s) RSTRING(s)->len
88 #endif
89 #ifndef RSTRING_PTR
90 # define RSTRING_PTR(s) RSTRING(s)->ptr
91 #endif
93 #include "vim.h"
94 #include "version.h"
96 #if defined(PROTO) && !defined(FEAT_RUBY)
97 /* Define these to be able to generate the function prototypes. */
98 # define VALUE int
99 # define RUBY_DATA_FUNC int
100 #endif
102 static int ruby_initialized = 0;
103 static VALUE objtbl;
105 static VALUE mVIM;
106 static VALUE cBuffer;
107 static VALUE cVimWindow;
108 static VALUE eDeletedBufferError;
109 static VALUE eDeletedWindowError;
111 static int ensure_ruby_initialized(void);
112 static void error_print(int);
113 static void ruby_io_init(void);
114 static void ruby_vim_init(void);
116 #if defined(DYNAMIC_RUBY) || defined(PROTO)
117 #ifdef PROTO
118 # define HINSTANCE int /* for generating prototypes */
119 #endif
122 * Wrapper defines
124 #define rb_assoc_new dll_rb_assoc_new
125 #define rb_cObject (*dll_rb_cObject)
126 #define rb_check_type dll_rb_check_type
127 #define rb_class_path dll_rb_class_path
128 #define rb_data_object_alloc dll_rb_data_object_alloc
129 #define rb_define_class_under dll_rb_define_class_under
130 #define rb_define_const dll_rb_define_const
131 #define rb_define_global_function dll_rb_define_global_function
132 #define rb_define_method dll_rb_define_method
133 #define rb_define_module dll_rb_define_module
134 #define rb_define_module_function dll_rb_define_module_function
135 #define rb_define_singleton_method dll_rb_define_singleton_method
136 #define rb_define_virtual_variable dll_rb_define_virtual_variable
137 #define rb_stdout (*dll_rb_stdout)
138 #define rb_eArgError (*dll_rb_eArgError)
139 #define rb_eIndexError (*dll_rb_eIndexError)
140 #define rb_eRuntimeError (*dll_rb_eRuntimeError)
141 #define rb_eStandardError (*dll_rb_eStandardError)
142 #define rb_eval_string_protect dll_rb_eval_string_protect
143 #define rb_global_variable dll_rb_global_variable
144 #define rb_hash_aset dll_rb_hash_aset
145 #define rb_hash_new dll_rb_hash_new
146 #define rb_inspect dll_rb_inspect
147 #define rb_int2inum dll_rb_int2inum
148 #define rb_lastline_get dll_rb_lastline_get
149 #define rb_lastline_set dll_rb_lastline_set
150 #define rb_load_protect dll_rb_load_protect
151 #define rb_num2long dll_rb_num2long
152 #define rb_num2ulong dll_rb_num2ulong
153 #define rb_obj_alloc dll_rb_obj_alloc
154 #define rb_obj_as_string dll_rb_obj_as_string
155 #define rb_obj_id dll_rb_obj_id
156 #define rb_raise dll_rb_raise
157 #define rb_str2cstr dll_rb_str2cstr
158 #define rb_str_cat dll_rb_str_cat
159 #define rb_str_concat dll_rb_str_concat
160 #define rb_str_new dll_rb_str_new
161 #define rb_str_new2 dll_rb_str_new2
162 #if defined(RUBY_VERSION) && RUBY_VERSION >= 19
163 # define rb_errinfo dll_rb_errinfo
164 #else
165 # define ruby_errinfo (*dll_ruby_errinfo)
166 #endif
167 #define ruby_init dll_ruby_init
168 #define ruby_init_loadpath dll_ruby_init_loadpath
169 #define NtInitialize dll_NtInitialize
170 #if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 18
171 # define rb_w32_snprintf dll_rb_w32_snprintf
172 #endif
174 #if defined(RUBY_VERSION) && RUBY_VERSION >= 19
175 # define ruby_script dll_ruby_script
176 # define rb_enc_find_index dll_rb_enc_find_index
177 # define rb_enc_find dll_rb_enc_find
178 # define rb_enc_str_new dll_rb_enc_str_new
179 # define rb_sprintf dll_rb_sprintf
180 #endif
183 * Pointers for dynamic link
185 static VALUE (*dll_rb_assoc_new) (VALUE, VALUE);
186 static VALUE *dll_rb_cFalseClass;
187 static VALUE *dll_rb_cFixnum;
188 static VALUE *dll_rb_cNilClass;
189 static VALUE *dll_rb_cObject;
190 static VALUE *dll_rb_cSymbol;
191 static VALUE *dll_rb_cTrueClass;
192 static void (*dll_rb_check_type) (VALUE,int);
193 static VALUE (*dll_rb_class_path) (VALUE);
194 static VALUE (*dll_rb_data_object_alloc) (VALUE, void*, RUBY_DATA_FUNC, RUBY_DATA_FUNC);
195 static VALUE (*dll_rb_define_class_under) (VALUE, const char*, VALUE);
196 static void (*dll_rb_define_const) (VALUE,const char*,VALUE);
197 static void (*dll_rb_define_global_function) (const char*,VALUE(*)(),int);
198 static void (*dll_rb_define_method) (VALUE,const char*,VALUE(*)(),int);
199 static VALUE (*dll_rb_define_module) (const char*);
200 static void (*dll_rb_define_module_function) (VALUE,const char*,VALUE(*)(),int);
201 static void (*dll_rb_define_singleton_method) (VALUE,const char*,VALUE(*)(),int);
202 static void (*dll_rb_define_virtual_variable) (const char*,VALUE(*)(),void(*)());
203 static VALUE *dll_rb_stdout;
204 static VALUE *dll_rb_eArgError;
205 static VALUE *dll_rb_eIndexError;
206 static VALUE *dll_rb_eRuntimeError;
207 static VALUE *dll_rb_eStandardError;
208 static VALUE (*dll_rb_eval_string_protect) (const char*, int*);
209 static void (*dll_rb_global_variable) (VALUE*);
210 static VALUE (*dll_rb_hash_aset) (VALUE, VALUE, VALUE);
211 static VALUE (*dll_rb_hash_new) (void);
212 static VALUE (*dll_rb_inspect) (VALUE);
213 static VALUE (*dll_rb_int2inum) (long);
214 static VALUE (*dll_rb_int2inum) (long);
215 static VALUE (*dll_rb_lastline_get) (void);
216 static void (*dll_rb_lastline_set) (VALUE);
217 static void (*dll_rb_load_protect) (VALUE, int, int*);
218 static long (*dll_rb_num2long) (VALUE);
219 static unsigned long (*dll_rb_num2ulong) (VALUE);
220 static VALUE (*dll_rb_obj_alloc) (VALUE);
221 static VALUE (*dll_rb_obj_as_string) (VALUE);
222 static VALUE (*dll_rb_obj_id) (VALUE);
223 static void (*dll_rb_raise) (VALUE, const char*, ...);
224 static char *(*dll_rb_str2cstr) (VALUE,int*);
225 static VALUE (*dll_rb_str_cat) (VALUE, const char*, long);
226 static VALUE (*dll_rb_str_concat) (VALUE, VALUE);
227 static VALUE (*dll_rb_str_new) (const char*, long);
228 static VALUE (*dll_rb_str_new2) (const char*);
229 #if defined(RUBY_VERSION) && RUBY_VERSION >= 19
230 static VALUE (*dll_rb_errinfo) (void);
231 #else
232 static VALUE *dll_ruby_errinfo;
233 #endif
234 static void (*dll_ruby_init) (void);
235 static void (*dll_ruby_init_loadpath) (void);
236 static void (*dll_NtInitialize) (int*, char***);
237 #if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 18
238 static int (*dll_rb_w32_snprintf)(char*, size_t, const char*, ...);
239 #endif
241 #if defined(RUBY_VERSION) && RUBY_VERSION >= 19
242 static void (*dll_ruby_script) (const char*);
243 static int (*dll_rb_enc_find_index) (const char*);
244 static rb_encoding* (*dll_rb_enc_find) (const char*);
245 static VALUE (*dll_rb_enc_str_new) (const char*, long, rb_encoding*);
246 static VALUE (*dll_rb_sprintf) (const char*, ...);
247 #endif
249 static HINSTANCE hinstRuby = 0; /* Instance of ruby.dll */
252 * Table of name to function pointer of ruby.
254 #define RUBY_PROC FARPROC
255 static struct
257 char *name;
258 RUBY_PROC *ptr;
259 } ruby_funcname_table[] =
261 {"rb_assoc_new", (RUBY_PROC*)&dll_rb_assoc_new},
262 {"rb_cFalseClass", (RUBY_PROC*)&dll_rb_cFalseClass},
263 {"rb_cFixnum", (RUBY_PROC*)&dll_rb_cFixnum},
264 {"rb_cNilClass", (RUBY_PROC*)&dll_rb_cNilClass},
265 {"rb_cObject", (RUBY_PROC*)&dll_rb_cObject},
266 {"rb_cSymbol", (RUBY_PROC*)&dll_rb_cSymbol},
267 {"rb_cTrueClass", (RUBY_PROC*)&dll_rb_cTrueClass},
268 {"rb_check_type", (RUBY_PROC*)&dll_rb_check_type},
269 {"rb_class_path", (RUBY_PROC*)&dll_rb_class_path},
270 {"rb_data_object_alloc", (RUBY_PROC*)&dll_rb_data_object_alloc},
271 {"rb_define_class_under", (RUBY_PROC*)&dll_rb_define_class_under},
272 {"rb_define_const", (RUBY_PROC*)&dll_rb_define_const},
273 {"rb_define_global_function", (RUBY_PROC*)&dll_rb_define_global_function},
274 {"rb_define_method", (RUBY_PROC*)&dll_rb_define_method},
275 {"rb_define_module", (RUBY_PROC*)&dll_rb_define_module},
276 {"rb_define_module_function", (RUBY_PROC*)&dll_rb_define_module_function},
277 {"rb_define_singleton_method", (RUBY_PROC*)&dll_rb_define_singleton_method},
278 {"rb_define_virtual_variable", (RUBY_PROC*)&dll_rb_define_virtual_variable},
279 {"rb_stdout", (RUBY_PROC*)&dll_rb_stdout},
280 {"rb_eArgError", (RUBY_PROC*)&dll_rb_eArgError},
281 {"rb_eIndexError", (RUBY_PROC*)&dll_rb_eIndexError},
282 {"rb_eRuntimeError", (RUBY_PROC*)&dll_rb_eRuntimeError},
283 {"rb_eStandardError", (RUBY_PROC*)&dll_rb_eStandardError},
284 {"rb_eval_string_protect", (RUBY_PROC*)&dll_rb_eval_string_protect},
285 {"rb_global_variable", (RUBY_PROC*)&dll_rb_global_variable},
286 {"rb_hash_aset", (RUBY_PROC*)&dll_rb_hash_aset},
287 {"rb_hash_new", (RUBY_PROC*)&dll_rb_hash_new},
288 {"rb_inspect", (RUBY_PROC*)&dll_rb_inspect},
289 {"rb_int2inum", (RUBY_PROC*)&dll_rb_int2inum},
290 {"rb_lastline_get", (RUBY_PROC*)&dll_rb_lastline_get},
291 {"rb_lastline_set", (RUBY_PROC*)&dll_rb_lastline_set},
292 {"rb_load_protect", (RUBY_PROC*)&dll_rb_load_protect},
293 {"rb_num2long", (RUBY_PROC*)&dll_rb_num2long},
294 {"rb_num2ulong", (RUBY_PROC*)&dll_rb_num2ulong},
295 {"rb_obj_alloc", (RUBY_PROC*)&dll_rb_obj_alloc},
296 {"rb_obj_as_string", (RUBY_PROC*)&dll_rb_obj_as_string},
297 {"rb_obj_id", (RUBY_PROC*)&dll_rb_obj_id},
298 {"rb_raise", (RUBY_PROC*)&dll_rb_raise},
299 {"rb_str2cstr", (RUBY_PROC*)&dll_rb_str2cstr},
300 {"rb_str_cat", (RUBY_PROC*)&dll_rb_str_cat},
301 {"rb_str_concat", (RUBY_PROC*)&dll_rb_str_concat},
302 {"rb_str_new", (RUBY_PROC*)&dll_rb_str_new},
303 {"rb_str_new2", (RUBY_PROC*)&dll_rb_str_new2},
304 #if defined(RUBY_VERSION) && RUBY_VERSION >= 19
305 {"rb_errinfo", (RUBY_PROC*)&dll_rb_errinfo},
306 #else
307 {"ruby_errinfo", (RUBY_PROC*)&dll_ruby_errinfo},
308 #endif
309 {"ruby_init", (RUBY_PROC*)&dll_ruby_init},
310 {"ruby_init_loadpath", (RUBY_PROC*)&dll_ruby_init_loadpath},
311 {"NtInitialize", (RUBY_PROC*)&dll_NtInitialize},
312 #if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 18
313 {"rb_w32_snprintf", (RUBY_PROC*)&dll_rb_w32_snprintf},
314 #endif
315 #if defined(RUBY_VERSION) && RUBY_VERSION >= 19
316 {"ruby_script", (RUBY_PROC*)&dll_ruby_script},
317 {"rb_enc_find_index", (RUBY_PROC*)&dll_rb_enc_find_index},
318 {"rb_enc_find", (RUBY_PROC*)&dll_rb_enc_find},
319 {"rb_enc_str_new", (RUBY_PROC*)&dll_rb_enc_str_new},
320 {"rb_sprintf", (RUBY_PROC*)&dll_rb_sprintf},
321 #endif
322 {"", NULL},
326 * Free ruby.dll
328 static void
329 end_dynamic_ruby()
331 if (hinstRuby)
333 FreeLibrary(hinstRuby);
334 hinstRuby = 0;
339 * Load library and get all pointers.
340 * Parameter 'libname' provides name of DLL.
341 * Return OK or FAIL.
343 static int
344 ruby_runtime_link_init(char *libname, int verbose)
346 int i;
348 if (hinstRuby)
349 return OK;
350 hinstRuby = LoadLibrary(libname);
351 if (!hinstRuby)
353 if (verbose)
354 EMSG2(_(e_loadlib), libname);
355 return FAIL;
358 for (i = 0; ruby_funcname_table[i].ptr; ++i)
360 if (!(*ruby_funcname_table[i].ptr = GetProcAddress(hinstRuby,
361 ruby_funcname_table[i].name)))
363 FreeLibrary(hinstRuby);
364 hinstRuby = 0;
365 if (verbose)
366 EMSG2(_(e_loadfunc), ruby_funcname_table[i].name);
367 return FAIL;
370 return OK;
374 * If ruby is enabled (there is installed ruby on Windows system) return TRUE,
375 * else FALSE.
378 ruby_enabled(verbose)
379 int verbose;
381 return ruby_runtime_link_init(DYNAMIC_RUBY_DLL, verbose) == OK;
383 #endif /* defined(DYNAMIC_RUBY) || defined(PROTO) */
385 void
386 ruby_end()
388 #ifdef DYNAMIC_RUBY
389 end_dynamic_ruby();
390 #endif
393 void ex_ruby(exarg_T *eap)
395 int state;
396 char *script = NULL;
398 script = (char *)script_get(eap, eap->arg);
399 if (!eap->skip && ensure_ruby_initialized())
401 if (script == NULL)
402 rb_eval_string_protect((char *)eap->arg, &state);
403 else
404 rb_eval_string_protect(script, &state);
405 if (state)
406 error_print(state);
408 vim_free(script);
412 * In Ruby 1.9 or later, ruby String object has encoding.
413 * conversion buffer string of vim to ruby String object using
414 * VIM encoding option.
416 static VALUE
417 vim_str2rb_enc_str(const char *s)
419 #if defined(RUBY_VERSION) && RUBY_VERSION >= 19
420 int isnum;
421 long lval;
422 char_u *sval;
423 rb_encoding *enc;
425 isnum = get_option_value((char_u *)"enc", &lval, &sval, 0);
426 if (isnum == 0)
428 enc = rb_enc_find((char *)sval);
429 vim_free(sval);
430 if (enc) {
431 return rb_enc_str_new(s, strlen(s), enc);
434 #endif
435 return rb_str_new2(s);
438 static VALUE
439 eval_enc_string_protect(const char *str, int *state)
441 #if defined(RUBY_VERSION) && RUBY_VERSION >= 19
442 int isnum;
443 long lval;
444 char_u *sval;
445 rb_encoding *enc;
446 VALUE v;
448 isnum = get_option_value((char_u *)"enc", &lval, &sval, 0);
449 if (isnum == 0)
451 enc = rb_enc_find((char *)sval);
452 vim_free(sval);
453 if (enc)
455 v = rb_sprintf("#-*- coding:%s -*-\n%s", rb_enc_name(enc), str);
456 return rb_eval_string_protect(StringValuePtr(v), state);
459 #endif
460 return rb_eval_string_protect(str, state);
463 void ex_rubydo(exarg_T *eap)
465 int state;
466 linenr_T i;
468 if (ensure_ruby_initialized())
470 if (u_save(eap->line1 - 1, eap->line2 + 1) != OK)
471 return;
472 for (i = eap->line1; i <= eap->line2; i++) {
473 VALUE line, oldline;
475 line = oldline = vim_str2rb_enc_str((char *)ml_get(i));
476 rb_lastline_set(line);
477 eval_enc_string_protect((char *) eap->arg, &state);
478 if (state) {
479 error_print(state);
480 break;
482 line = rb_lastline_get();
483 if (!NIL_P(line)) {
484 if (TYPE(line) != T_STRING) {
485 EMSG(_("E265: $_ must be an instance of String"));
486 return;
488 ml_replace(i, (char_u *) StringValuePtr(line), 1);
489 changed();
490 #ifdef SYNTAX_HL
491 syn_changed(i); /* recompute syntax hl. for this line */
492 #endif
495 check_cursor();
496 update_curbuf(NOT_VALID);
500 void ex_rubyfile(exarg_T *eap)
502 int state;
504 if (ensure_ruby_initialized())
506 rb_load_protect(rb_str_new2((char *) eap->arg), 0, &state);
507 if (state) error_print(state);
511 void ruby_buffer_free(buf_T *buf)
513 if (buf->b_ruby_ref)
515 rb_hash_aset(objtbl, rb_obj_id((VALUE) buf->b_ruby_ref), Qnil);
516 RDATA(buf->b_ruby_ref)->data = NULL;
520 void ruby_window_free(win_T *win)
522 if (win->w_ruby_ref)
524 rb_hash_aset(objtbl, rb_obj_id((VALUE) win->w_ruby_ref), Qnil);
525 RDATA(win->w_ruby_ref)->data = NULL;
529 static int ensure_ruby_initialized(void)
531 if (!ruby_initialized)
533 #ifdef DYNAMIC_RUBY
534 if (ruby_enabled(TRUE))
536 #endif
537 #ifdef _WIN32
538 /* suggested by Ariya Mizutani */
539 int argc = 1;
540 char *argv[] = {"gvim.exe"};
541 NtInitialize(&argc, &argv);
542 #endif
543 #if defined(RUBY_VERSION) && RUBY_VERSION >= 19
544 RUBY_INIT_STACK;
545 #endif
546 ruby_init();
547 #if defined(RUBY_VERSION) && RUBY_VERSION >= 19
548 ruby_script("vim-ruby");
549 #endif
550 ruby_init_loadpath();
551 ruby_io_init();
552 #if defined(RUBY_VERSION) && RUBY_VERSION >= 19
553 rb_enc_find_index("encdb");
554 #endif
555 ruby_vim_init();
556 ruby_initialized = 1;
557 #ifdef DYNAMIC_RUBY
559 else
561 EMSG(_("E266: Sorry, this command is disabled, the Ruby library could not be loaded."));
562 return 0;
564 #endif
566 return ruby_initialized;
569 static void error_print(int state)
571 #ifndef DYNAMIC_RUBY
572 #if !(defined(RUBY_VERSION) && RUBY_VERSION >= 19)
573 RUBYEXTERN VALUE ruby_errinfo;
574 #endif
575 #endif
576 VALUE eclass;
577 VALUE einfo;
578 char buff[BUFSIZ];
580 #define TAG_RETURN 0x1
581 #define TAG_BREAK 0x2
582 #define TAG_NEXT 0x3
583 #define TAG_RETRY 0x4
584 #define TAG_REDO 0x5
585 #define TAG_RAISE 0x6
586 #define TAG_THROW 0x7
587 #define TAG_FATAL 0x8
588 #define TAG_MASK 0xf
590 switch (state) {
591 case TAG_RETURN:
592 EMSG(_("E267: unexpected return"));
593 break;
594 case TAG_NEXT:
595 EMSG(_("E268: unexpected next"));
596 break;
597 case TAG_BREAK:
598 EMSG(_("E269: unexpected break"));
599 break;
600 case TAG_REDO:
601 EMSG(_("E270: unexpected redo"));
602 break;
603 case TAG_RETRY:
604 EMSG(_("E271: retry outside of rescue clause"));
605 break;
606 case TAG_RAISE:
607 case TAG_FATAL:
608 #if defined(RUBY_VERSION) && RUBY_VERSION >= 19
609 eclass = CLASS_OF(rb_errinfo());
610 einfo = rb_obj_as_string(rb_errinfo());
611 #else
612 eclass = CLASS_OF(ruby_errinfo);
613 einfo = rb_obj_as_string(ruby_errinfo);
614 #endif
615 if (eclass == rb_eRuntimeError && RSTRING_LEN(einfo) == 0) {
616 EMSG(_("E272: unhandled exception"));
618 else {
619 VALUE epath;
620 char *p;
622 epath = rb_class_path(eclass);
623 vim_snprintf(buff, BUFSIZ, "%s: %s",
624 RSTRING_PTR(epath), RSTRING_PTR(einfo));
625 p = strchr(buff, '\n');
626 if (p) *p = '\0';
627 EMSG(buff);
629 break;
630 default:
631 vim_snprintf(buff, BUFSIZ, _("E273: unknown longjmp status %d"), state);
632 EMSG(buff);
633 break;
637 static VALUE vim_message(VALUE self UNUSED, VALUE str)
639 char *buff, *p;
641 str = rb_obj_as_string(str);
642 buff = ALLOCA_N(char, RSTRING_LEN(str));
643 strcpy(buff, RSTRING_PTR(str));
644 p = strchr(buff, '\n');
645 if (p) *p = '\0';
646 MSG(buff);
647 return Qnil;
650 static VALUE vim_set_option(VALUE self UNUSED, VALUE str)
652 do_set((char_u *)StringValuePtr(str), 0);
653 update_screen(NOT_VALID);
654 return Qnil;
657 static VALUE vim_command(VALUE self UNUSED, VALUE str)
659 do_cmdline_cmd((char_u *)StringValuePtr(str));
660 return Qnil;
663 #ifdef FEAT_EVAL
664 static VALUE vim_to_ruby(typval_T *tv)
666 VALUE result = Qnil;
668 if (tv->v_type == VAR_STRING)
670 result = rb_str_new2((char *)tv->vval.v_string);
672 else if (tv->v_type == VAR_NUMBER)
674 result = INT2NUM(tv->vval.v_number);
676 # ifdef FEAT_FLOAT
677 else if (tv->v_type == VAR_FLOAT)
679 result = rb_float_new(tv->vval.v_float);
681 # endif
682 else if (tv->v_type == VAR_LIST)
684 list_T *list = tv->vval.v_list;
685 listitem_T *curr;
687 result = rb_ary_new();
689 if (list != NULL)
691 for (curr = list->lv_first; curr != NULL; curr = curr->li_next)
693 rb_ary_push(result, vim_to_ruby(&curr->li_tv));
697 else if (tv->v_type == VAR_DICT)
699 result = rb_hash_new();
701 if (tv->vval.v_dict != NULL)
703 hashtab_T *ht = &tv->vval.v_dict->dv_hashtab;
704 long_u todo = ht->ht_used;
705 hashitem_T *hi;
706 dictitem_T *di;
708 for (hi = ht->ht_array; todo > 0; ++hi)
710 if (!HASHITEM_EMPTY(hi))
712 --todo;
714 di = dict_lookup(hi);
715 rb_hash_aset(result, rb_str_new2((char *)hi->hi_key),
716 vim_to_ruby(&di->di_tv));
720 } /* else return Qnil; */
722 return result;
724 #endif
726 static VALUE vim_evaluate(VALUE self UNUSED, VALUE str)
728 #ifdef FEAT_EVAL
729 typval_T *tv;
730 VALUE result;
732 tv = eval_expr((char_u *)StringValuePtr(str), NULL);
733 if (tv == NULL)
735 return Qnil;
737 result = vim_to_ruby(tv);
739 free_tv(tv);
741 return result;
742 #else
743 return Qnil;
744 #endif
747 static VALUE buffer_new(buf_T *buf)
749 if (buf->b_ruby_ref)
751 return (VALUE) buf->b_ruby_ref;
753 else
755 VALUE obj = Data_Wrap_Struct(cBuffer, 0, 0, buf);
756 buf->b_ruby_ref = (void *) obj;
757 rb_hash_aset(objtbl, rb_obj_id(obj), obj);
758 return obj;
762 static buf_T *get_buf(VALUE obj)
764 buf_T *buf;
766 Data_Get_Struct(obj, buf_T, buf);
767 if (buf == NULL)
768 rb_raise(eDeletedBufferError, "attempt to refer to deleted buffer");
769 return buf;
772 static VALUE buffer_s_current()
774 return buffer_new(curbuf);
777 static VALUE buffer_s_count()
779 buf_T *b;
780 int n = 0;
782 for (b = firstbuf; b != NULL; b = b->b_next)
784 /* Deleted buffers should not be counted
785 * SegPhault - 01/07/05 */
786 if (b->b_p_bl)
787 n++;
790 return INT2NUM(n);
793 static VALUE buffer_s_aref(VALUE self UNUSED, VALUE num)
795 buf_T *b;
796 int n = NUM2INT(num);
798 for (b = firstbuf; b != NULL; b = b->b_next)
800 /* Deleted buffers should not be counted
801 * SegPhault - 01/07/05 */
802 if (!b->b_p_bl)
803 continue;
805 if (n == 0)
806 return buffer_new(b);
808 n--;
810 return Qnil;
813 static VALUE buffer_name(VALUE self)
815 buf_T *buf = get_buf(self);
817 return buf->b_ffname ? rb_str_new2((char *)buf->b_ffname) : Qnil;
820 static VALUE buffer_number(VALUE self)
822 buf_T *buf = get_buf(self);
824 return INT2NUM(buf->b_fnum);
827 static VALUE buffer_count(VALUE self)
829 buf_T *buf = get_buf(self);
831 return INT2NUM(buf->b_ml.ml_line_count);
834 static VALUE get_buffer_line(buf_T *buf, linenr_T n)
836 if (n > 0 && n <= buf->b_ml.ml_line_count)
838 char *line = (char *)ml_get_buf(buf, n, FALSE);
839 return line ? vim_str2rb_enc_str(line) : Qnil;
841 rb_raise(rb_eIndexError, "line number %ld out of range", (long)n);
842 #ifndef __GNUC__
843 return Qnil; /* For stop warning */
844 #endif
847 static VALUE buffer_aref(VALUE self, VALUE num)
849 buf_T *buf = get_buf(self);
851 if (buf != NULL)
852 return get_buffer_line(buf, (linenr_T)NUM2LONG(num));
853 return Qnil; /* For stop warning */
856 static VALUE set_buffer_line(buf_T *buf, linenr_T n, VALUE str)
858 char *line = StringValuePtr(str);
859 aco_save_T aco;
861 if (n > 0 && n <= buf->b_ml.ml_line_count && line != NULL)
863 /* set curwin/curbuf for "buf" and save some things */
864 aucmd_prepbuf(&aco, buf);
866 if (u_savesub(n) == OK) {
867 ml_replace(n, (char_u *)line, TRUE);
868 changed();
869 #ifdef SYNTAX_HL
870 syn_changed(n); /* recompute syntax hl. for this line */
871 #endif
874 /* restore curwin/curbuf and a few other things */
875 aucmd_restbuf(&aco);
876 /* Careful: autocommands may have made "buf" invalid! */
878 update_curbuf(NOT_VALID);
880 else
882 rb_raise(rb_eIndexError, "line number %ld out of range", (long)n);
883 #ifndef __GNUC__
884 return Qnil; /* For stop warning */
885 #endif
887 return str;
890 static VALUE buffer_aset(VALUE self, VALUE num, VALUE str)
892 buf_T *buf = get_buf(self);
894 if (buf != NULL)
895 return set_buffer_line(buf, (linenr_T)NUM2LONG(num), str);
896 return str;
899 static VALUE buffer_delete(VALUE self, VALUE num)
901 buf_T *buf = get_buf(self);
902 long n = NUM2LONG(num);
903 aco_save_T aco;
905 if (n > 0 && n <= buf->b_ml.ml_line_count)
907 /* set curwin/curbuf for "buf" and save some things */
908 aucmd_prepbuf(&aco, buf);
910 if (u_savedel(n, 1) == OK) {
911 ml_delete(n, 0);
913 /* Changes to non-active buffers should properly refresh
914 * SegPhault - 01/09/05 */
915 deleted_lines_mark(n, 1L);
917 changed();
920 /* restore curwin/curbuf and a few other things */
921 aucmd_restbuf(&aco);
922 /* Careful: autocommands may have made "buf" invalid! */
924 update_curbuf(NOT_VALID);
926 else
928 rb_raise(rb_eIndexError, "line number %ld out of range", n);
930 return Qnil;
933 static VALUE buffer_append(VALUE self, VALUE num, VALUE str)
935 buf_T *buf = get_buf(self);
936 char *line = StringValuePtr(str);
937 long n = NUM2LONG(num);
938 aco_save_T aco;
940 if (line == NULL) {
941 rb_raise(rb_eIndexError, "NULL line");
943 else if (n >= 0 && n <= buf->b_ml.ml_line_count)
945 /* set curwin/curbuf for "buf" and save some things */
946 aucmd_prepbuf(&aco, buf);
948 if (u_inssub(n + 1) == OK) {
949 ml_append(n, (char_u *) line, (colnr_T) 0, FALSE);
951 /* Changes to non-active buffers should properly refresh screen
952 * SegPhault - 12/20/04 */
953 appended_lines_mark(n, 1L);
955 changed();
958 /* restore curwin/curbuf and a few other things */
959 aucmd_restbuf(&aco);
960 /* Careful: autocommands may have made "buf" invalid! */
962 update_curbuf(NOT_VALID);
964 else {
965 rb_raise(rb_eIndexError, "line number %ld out of range", n);
967 return str;
970 static VALUE window_new(win_T *win)
972 if (win->w_ruby_ref)
974 return (VALUE) win->w_ruby_ref;
976 else
978 VALUE obj = Data_Wrap_Struct(cVimWindow, 0, 0, win);
979 win->w_ruby_ref = (void *) obj;
980 rb_hash_aset(objtbl, rb_obj_id(obj), obj);
981 return obj;
985 static win_T *get_win(VALUE obj)
987 win_T *win;
989 Data_Get_Struct(obj, win_T, win);
990 if (win == NULL)
991 rb_raise(eDeletedWindowError, "attempt to refer to deleted window");
992 return win;
995 static VALUE window_s_current()
997 return window_new(curwin);
1001 * Added line manipulation functions
1002 * SegPhault - 03/07/05
1004 static VALUE line_s_current()
1006 return get_buffer_line(curbuf, curwin->w_cursor.lnum);
1009 static VALUE set_current_line(VALUE self UNUSED, VALUE str)
1011 return set_buffer_line(curbuf, curwin->w_cursor.lnum, str);
1014 static VALUE current_line_number()
1016 return INT2FIX((int)curwin->w_cursor.lnum);
1021 static VALUE window_s_count()
1023 #ifdef FEAT_WINDOWS
1024 win_T *w;
1025 int n = 0;
1027 for (w = firstwin; w != NULL; w = w->w_next)
1028 n++;
1029 return INT2NUM(n);
1030 #else
1031 return INT2NUM(1);
1032 #endif
1035 static VALUE window_s_aref(VALUE self UNUSED, VALUE num)
1037 win_T *w;
1038 int n = NUM2INT(num);
1040 #ifndef FEAT_WINDOWS
1041 w = curwin;
1042 #else
1043 for (w = firstwin; w != NULL; w = w->w_next, --n)
1044 #endif
1045 if (n == 0)
1046 return window_new(w);
1047 return Qnil;
1050 static VALUE window_buffer(VALUE self)
1052 win_T *win = get_win(self);
1054 return buffer_new(win->w_buffer);
1057 static VALUE window_height(VALUE self)
1059 win_T *win = get_win(self);
1061 return INT2NUM(win->w_height);
1064 static VALUE window_set_height(VALUE self, VALUE height)
1066 win_T *win = get_win(self);
1067 win_T *savewin = curwin;
1069 curwin = win;
1070 win_setheight(NUM2INT(height));
1071 curwin = savewin;
1072 return height;
1075 static VALUE window_width(VALUE self)
1077 win_T *win = get_win(self);
1079 return INT2NUM(win->w_width);
1082 static VALUE window_set_width(VALUE self, VALUE width)
1084 win_T *win = get_win(self);
1085 win_T *savewin = curwin;
1087 curwin = win;
1088 win_setwidth(NUM2INT(width));
1089 curwin = savewin;
1090 return width;
1093 static VALUE window_cursor(VALUE self)
1095 win_T *win = get_win(self);
1097 return rb_assoc_new(INT2NUM(win->w_cursor.lnum), INT2NUM(win->w_cursor.col));
1100 static VALUE window_set_cursor(VALUE self, VALUE pos)
1102 VALUE lnum, col;
1103 win_T *win = get_win(self);
1105 Check_Type(pos, T_ARRAY);
1106 if (RARRAY_LEN(pos) != 2)
1107 rb_raise(rb_eArgError, "array length must be 2");
1108 lnum = RARRAY_PTR(pos)[0];
1109 col = RARRAY_PTR(pos)[1];
1110 win->w_cursor.lnum = NUM2LONG(lnum);
1111 win->w_cursor.col = NUM2UINT(col);
1112 check_cursor(); /* put cursor on an existing line */
1113 update_screen(NOT_VALID);
1114 return Qnil;
1117 static VALUE f_p(int argc, VALUE *argv, VALUE self UNUSED)
1119 int i;
1120 VALUE str = rb_str_new("", 0);
1122 for (i = 0; i < argc; i++) {
1123 if (i > 0) rb_str_cat(str, ", ", 2);
1124 rb_str_concat(str, rb_inspect(argv[i]));
1126 MSG(RSTRING_PTR(str));
1127 return Qnil;
1130 static void ruby_io_init(void)
1132 #ifndef DYNAMIC_RUBY
1133 RUBYEXTERN VALUE rb_stdout;
1134 #endif
1136 rb_stdout = rb_obj_alloc(rb_cObject);
1137 rb_define_singleton_method(rb_stdout, "write", vim_message, 1);
1138 rb_define_global_function("p", f_p, -1);
1141 static void ruby_vim_init(void)
1143 objtbl = rb_hash_new();
1144 rb_global_variable(&objtbl);
1146 /* The Vim module used to be called "VIM", but "Vim" is better. Make an
1147 * alias "VIM" for backwards compatiblity. */
1148 mVIM = rb_define_module("Vim");
1149 rb_define_const(rb_cObject, "VIM", mVIM);
1150 rb_define_const(mVIM, "VERSION_MAJOR", INT2NUM(VIM_VERSION_MAJOR));
1151 rb_define_const(mVIM, "VERSION_MINOR", INT2NUM(VIM_VERSION_MINOR));
1152 rb_define_const(mVIM, "VERSION_BUILD", INT2NUM(VIM_VERSION_BUILD));
1153 rb_define_const(mVIM, "VERSION_PATCHLEVEL", INT2NUM(VIM_VERSION_PATCHLEVEL));
1154 rb_define_const(mVIM, "VERSION_SHORT", rb_str_new2(VIM_VERSION_SHORT));
1155 rb_define_const(mVIM, "VERSION_MEDIUM", rb_str_new2(VIM_VERSION_MEDIUM));
1156 rb_define_const(mVIM, "VERSION_LONG", rb_str_new2(VIM_VERSION_LONG));
1157 rb_define_const(mVIM, "VERSION_LONG_DATE", rb_str_new2(VIM_VERSION_LONG_DATE));
1158 rb_define_module_function(mVIM, "message", vim_message, 1);
1159 rb_define_module_function(mVIM, "set_option", vim_set_option, 1);
1160 rb_define_module_function(mVIM, "command", vim_command, 1);
1161 rb_define_module_function(mVIM, "evaluate", vim_evaluate, 1);
1163 eDeletedBufferError = rb_define_class_under(mVIM, "DeletedBufferError",
1164 rb_eStandardError);
1165 eDeletedWindowError = rb_define_class_under(mVIM, "DeletedWindowError",
1166 rb_eStandardError);
1168 cBuffer = rb_define_class_under(mVIM, "Buffer", rb_cObject);
1169 rb_define_singleton_method(cBuffer, "current", buffer_s_current, 0);
1170 rb_define_singleton_method(cBuffer, "count", buffer_s_count, 0);
1171 rb_define_singleton_method(cBuffer, "[]", buffer_s_aref, 1);
1172 rb_define_method(cBuffer, "name", buffer_name, 0);
1173 rb_define_method(cBuffer, "number", buffer_number, 0);
1174 rb_define_method(cBuffer, "count", buffer_count, 0);
1175 rb_define_method(cBuffer, "length", buffer_count, 0);
1176 rb_define_method(cBuffer, "[]", buffer_aref, 1);
1177 rb_define_method(cBuffer, "[]=", buffer_aset, 2);
1178 rb_define_method(cBuffer, "delete", buffer_delete, 1);
1179 rb_define_method(cBuffer, "append", buffer_append, 2);
1181 /* Added line manipulation functions
1182 * SegPhault - 03/07/05 */
1183 rb_define_method(cBuffer, "line_number", current_line_number, 0);
1184 rb_define_method(cBuffer, "line", line_s_current, 0);
1185 rb_define_method(cBuffer, "line=", set_current_line, 1);
1188 cVimWindow = rb_define_class_under(mVIM, "Window", rb_cObject);
1189 rb_define_singleton_method(cVimWindow, "current", window_s_current, 0);
1190 rb_define_singleton_method(cVimWindow, "count", window_s_count, 0);
1191 rb_define_singleton_method(cVimWindow, "[]", window_s_aref, 1);
1192 rb_define_method(cVimWindow, "buffer", window_buffer, 0);
1193 rb_define_method(cVimWindow, "height", window_height, 0);
1194 rb_define_method(cVimWindow, "height=", window_set_height, 1);
1195 rb_define_method(cVimWindow, "width", window_width, 0);
1196 rb_define_method(cVimWindow, "width=", window_set_width, 1);
1197 rb_define_method(cVimWindow, "cursor", window_cursor, 0);
1198 rb_define_method(cVimWindow, "cursor=", window_set_cursor, 1);
1200 rb_define_virtual_variable("$curbuf", buffer_s_current, 0);
1201 rb_define_virtual_variable("$curwin", window_s_current, 0);