[7.2.360] Ruby on MS-Windows: can't use sockets.
[vim_mainline.git] / src / if_ruby.c
blob4aac79a6b6db9ffe55eb6378de8a47908a4b0f75
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>
58 #undef EXTERN
59 #undef _
61 /* T_DATA defined both by Ruby and Mac header files, hack around it... */
62 #if defined(MACOS_X_UNIX) || defined(macintosh)
63 # define __OPENTRANSPORT__
64 # define __OPENTRANSPORTPROTOCOL__
65 # define __OPENTRANSPORTPROVIDERS__
66 #endif
68 #include "vim.h"
69 #include "version.h"
71 #if defined(PROTO) && !defined(FEAT_RUBY)
72 /* Define these to be able to generate the function prototypes. */
73 # define VALUE int
74 # define RUBY_DATA_FUNC int
75 #endif
77 static int ruby_initialized = 0;
78 static VALUE objtbl;
80 static VALUE mVIM;
81 static VALUE cBuffer;
82 static VALUE cVimWindow;
83 static VALUE eDeletedBufferError;
84 static VALUE eDeletedWindowError;
86 static int ensure_ruby_initialized(void);
87 static void error_print(int);
88 static void ruby_io_init(void);
89 static void ruby_vim_init(void);
91 #if defined(DYNAMIC_RUBY) || defined(PROTO)
92 #ifdef PROTO
93 # define HINSTANCE int /* for generating prototypes */
94 #endif
97 * Wrapper defines
99 #define rb_assoc_new dll_rb_assoc_new
100 #define rb_cObject (*dll_rb_cObject)
101 #define rb_check_type dll_rb_check_type
102 #define rb_class_path dll_rb_class_path
103 #define rb_data_object_alloc dll_rb_data_object_alloc
104 #define rb_define_class_under dll_rb_define_class_under
105 #define rb_define_const dll_rb_define_const
106 #define rb_define_global_function dll_rb_define_global_function
107 #define rb_define_method dll_rb_define_method
108 #define rb_define_module dll_rb_define_module
109 #define rb_define_module_function dll_rb_define_module_function
110 #define rb_define_singleton_method dll_rb_define_singleton_method
111 #define rb_define_virtual_variable dll_rb_define_virtual_variable
112 #define rb_stdout (*dll_rb_stdout)
113 #define rb_eArgError (*dll_rb_eArgError)
114 #define rb_eIndexError (*dll_rb_eIndexError)
115 #define rb_eRuntimeError (*dll_rb_eRuntimeError)
116 #define rb_eStandardError (*dll_rb_eStandardError)
117 #define rb_eval_string_protect dll_rb_eval_string_protect
118 #define rb_global_variable dll_rb_global_variable
119 #define rb_hash_aset dll_rb_hash_aset
120 #define rb_hash_new dll_rb_hash_new
121 #define rb_inspect dll_rb_inspect
122 #define rb_int2inum dll_rb_int2inum
123 #define rb_lastline_get dll_rb_lastline_get
124 #define rb_lastline_set dll_rb_lastline_set
125 #define rb_load_protect dll_rb_load_protect
126 #define rb_num2long dll_rb_num2long
127 #define rb_num2ulong dll_rb_num2ulong
128 #define rb_obj_alloc dll_rb_obj_alloc
129 #define rb_obj_as_string dll_rb_obj_as_string
130 #define rb_obj_id dll_rb_obj_id
131 #define rb_raise dll_rb_raise
132 #define rb_str2cstr dll_rb_str2cstr
133 #define rb_str_cat dll_rb_str_cat
134 #define rb_str_concat dll_rb_str_concat
135 #define rb_str_new dll_rb_str_new
136 #define rb_str_new2 dll_rb_str_new2
137 #define ruby_errinfo (*dll_ruby_errinfo)
138 #define ruby_init dll_ruby_init
139 #define ruby_init_loadpath dll_ruby_init_loadpath
140 #define NtInitialize dll_NtInitialize
141 #if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 18
142 # define rb_w32_snprintf dll_rb_w32_snprintf
143 #endif
146 * Pointers for dynamic link
148 static VALUE (*dll_rb_assoc_new) (VALUE, VALUE);
149 static VALUE *dll_rb_cFalseClass;
150 static VALUE *dll_rb_cFixnum;
151 static VALUE *dll_rb_cNilClass;
152 static VALUE *dll_rb_cObject;
153 static VALUE *dll_rb_cSymbol;
154 static VALUE *dll_rb_cTrueClass;
155 static void (*dll_rb_check_type) (VALUE,int);
156 static VALUE (*dll_rb_class_path) (VALUE);
157 static VALUE (*dll_rb_data_object_alloc) (VALUE, void*, RUBY_DATA_FUNC, RUBY_DATA_FUNC);
158 static VALUE (*dll_rb_define_class_under) (VALUE, const char*, VALUE);
159 static void (*dll_rb_define_const) (VALUE,const char*,VALUE);
160 static void (*dll_rb_define_global_function) (const char*,VALUE(*)(),int);
161 static void (*dll_rb_define_method) (VALUE,const char*,VALUE(*)(),int);
162 static VALUE (*dll_rb_define_module) (const char*);
163 static void (*dll_rb_define_module_function) (VALUE,const char*,VALUE(*)(),int);
164 static void (*dll_rb_define_singleton_method) (VALUE,const char*,VALUE(*)(),int);
165 static void (*dll_rb_define_virtual_variable) (const char*,VALUE(*)(),void(*)());
166 static VALUE *dll_rb_stdout;
167 static VALUE *dll_rb_eArgError;
168 static VALUE *dll_rb_eIndexError;
169 static VALUE *dll_rb_eRuntimeError;
170 static VALUE *dll_rb_eStandardError;
171 static VALUE (*dll_rb_eval_string_protect) (const char*, int*);
172 static void (*dll_rb_global_variable) (VALUE*);
173 static VALUE (*dll_rb_hash_aset) (VALUE, VALUE, VALUE);
174 static VALUE (*dll_rb_hash_new) (void);
175 static VALUE (*dll_rb_inspect) (VALUE);
176 static VALUE (*dll_rb_int2inum) (long);
177 static VALUE (*dll_rb_int2inum) (long);
178 static VALUE (*dll_rb_lastline_get) (void);
179 static void (*dll_rb_lastline_set) (VALUE);
180 static void (*dll_rb_load_protect) (VALUE, int, int*);
181 static long (*dll_rb_num2long) (VALUE);
182 static unsigned long (*dll_rb_num2ulong) (VALUE);
183 static VALUE (*dll_rb_obj_alloc) (VALUE);
184 static VALUE (*dll_rb_obj_as_string) (VALUE);
185 static VALUE (*dll_rb_obj_id) (VALUE);
186 static void (*dll_rb_raise) (VALUE, const char*, ...);
187 static char *(*dll_rb_str2cstr) (VALUE,int*);
188 static VALUE (*dll_rb_str_cat) (VALUE, const char*, long);
189 static VALUE (*dll_rb_str_concat) (VALUE, VALUE);
190 static VALUE (*dll_rb_str_new) (const char*, long);
191 static VALUE (*dll_rb_str_new2) (const char*);
192 static VALUE *dll_ruby_errinfo;
193 static void (*dll_ruby_init) (void);
194 static void (*dll_ruby_init_loadpath) (void);
195 static void (*dll_NtInitialize) (int*, char***);
196 #if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 18
197 static int (*dll_rb_w32_snprintf)(char*, size_t, const char*, ...);
198 #endif
200 static HINSTANCE hinstRuby = 0; /* Instance of ruby.dll */
203 * Table of name to function pointer of ruby.
205 #define RUBY_PROC FARPROC
206 static struct
208 char *name;
209 RUBY_PROC *ptr;
210 } ruby_funcname_table[] =
212 {"rb_assoc_new", (RUBY_PROC*)&dll_rb_assoc_new},
213 {"rb_cFalseClass", (RUBY_PROC*)&dll_rb_cFalseClass},
214 {"rb_cFixnum", (RUBY_PROC*)&dll_rb_cFixnum},
215 {"rb_cNilClass", (RUBY_PROC*)&dll_rb_cNilClass},
216 {"rb_cObject", (RUBY_PROC*)&dll_rb_cObject},
217 {"rb_cSymbol", (RUBY_PROC*)&dll_rb_cSymbol},
218 {"rb_cTrueClass", (RUBY_PROC*)&dll_rb_cTrueClass},
219 {"rb_check_type", (RUBY_PROC*)&dll_rb_check_type},
220 {"rb_class_path", (RUBY_PROC*)&dll_rb_class_path},
221 {"rb_data_object_alloc", (RUBY_PROC*)&dll_rb_data_object_alloc},
222 {"rb_define_class_under", (RUBY_PROC*)&dll_rb_define_class_under},
223 {"rb_define_const", (RUBY_PROC*)&dll_rb_define_const},
224 {"rb_define_global_function", (RUBY_PROC*)&dll_rb_define_global_function},
225 {"rb_define_method", (RUBY_PROC*)&dll_rb_define_method},
226 {"rb_define_module", (RUBY_PROC*)&dll_rb_define_module},
227 {"rb_define_module_function", (RUBY_PROC*)&dll_rb_define_module_function},
228 {"rb_define_singleton_method", (RUBY_PROC*)&dll_rb_define_singleton_method},
229 {"rb_define_virtual_variable", (RUBY_PROC*)&dll_rb_define_virtual_variable},
230 {"rb_stdout", (RUBY_PROC*)&dll_rb_stdout},
231 {"rb_eArgError", (RUBY_PROC*)&dll_rb_eArgError},
232 {"rb_eIndexError", (RUBY_PROC*)&dll_rb_eIndexError},
233 {"rb_eRuntimeError", (RUBY_PROC*)&dll_rb_eRuntimeError},
234 {"rb_eStandardError", (RUBY_PROC*)&dll_rb_eStandardError},
235 {"rb_eval_string_protect", (RUBY_PROC*)&dll_rb_eval_string_protect},
236 {"rb_global_variable", (RUBY_PROC*)&dll_rb_global_variable},
237 {"rb_hash_aset", (RUBY_PROC*)&dll_rb_hash_aset},
238 {"rb_hash_new", (RUBY_PROC*)&dll_rb_hash_new},
239 {"rb_inspect", (RUBY_PROC*)&dll_rb_inspect},
240 {"rb_int2inum", (RUBY_PROC*)&dll_rb_int2inum},
241 {"rb_lastline_get", (RUBY_PROC*)&dll_rb_lastline_get},
242 {"rb_lastline_set", (RUBY_PROC*)&dll_rb_lastline_set},
243 {"rb_load_protect", (RUBY_PROC*)&dll_rb_load_protect},
244 {"rb_num2long", (RUBY_PROC*)&dll_rb_num2long},
245 {"rb_num2ulong", (RUBY_PROC*)&dll_rb_num2ulong},
246 {"rb_obj_alloc", (RUBY_PROC*)&dll_rb_obj_alloc},
247 {"rb_obj_as_string", (RUBY_PROC*)&dll_rb_obj_as_string},
248 {"rb_obj_id", (RUBY_PROC*)&dll_rb_obj_id},
249 {"rb_raise", (RUBY_PROC*)&dll_rb_raise},
250 {"rb_str2cstr", (RUBY_PROC*)&dll_rb_str2cstr},
251 {"rb_str_cat", (RUBY_PROC*)&dll_rb_str_cat},
252 {"rb_str_concat", (RUBY_PROC*)&dll_rb_str_concat},
253 {"rb_str_new", (RUBY_PROC*)&dll_rb_str_new},
254 {"rb_str_new2", (RUBY_PROC*)&dll_rb_str_new2},
255 {"ruby_errinfo", (RUBY_PROC*)&dll_ruby_errinfo},
256 {"ruby_init", (RUBY_PROC*)&dll_ruby_init},
257 {"ruby_init_loadpath", (RUBY_PROC*)&dll_ruby_init_loadpath},
258 {"NtInitialize", (RUBY_PROC*)&dll_NtInitialize},
259 #if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 18
260 {"rb_w32_snprintf", (RUBY_PROC*)&dll_rb_w32_snprintf},
261 #endif
262 {"", NULL},
266 * Free ruby.dll
268 static void
269 end_dynamic_ruby()
271 if (hinstRuby)
273 FreeLibrary(hinstRuby);
274 hinstRuby = 0;
279 * Load library and get all pointers.
280 * Parameter 'libname' provides name of DLL.
281 * Return OK or FAIL.
283 static int
284 ruby_runtime_link_init(char *libname, int verbose)
286 int i;
288 if (hinstRuby)
289 return OK;
290 hinstRuby = LoadLibrary(libname);
291 if (!hinstRuby)
293 if (verbose)
294 EMSG2(_(e_loadlib), libname);
295 return FAIL;
298 for (i = 0; ruby_funcname_table[i].ptr; ++i)
300 if (!(*ruby_funcname_table[i].ptr = GetProcAddress(hinstRuby,
301 ruby_funcname_table[i].name)))
303 FreeLibrary(hinstRuby);
304 hinstRuby = 0;
305 if (verbose)
306 EMSG2(_(e_loadfunc), ruby_funcname_table[i].name);
307 return FAIL;
310 return OK;
314 * If ruby is enabled (there is installed ruby on Windows system) return TRUE,
315 * else FALSE.
318 ruby_enabled(verbose)
319 int verbose;
321 return ruby_runtime_link_init(DYNAMIC_RUBY_DLL, verbose) == OK;
323 #endif /* defined(DYNAMIC_RUBY) || defined(PROTO) */
325 void
326 ruby_end()
328 #ifdef DYNAMIC_RUBY
329 end_dynamic_ruby();
330 #endif
333 void ex_ruby(exarg_T *eap)
335 int state;
336 char *script = NULL;
338 script = (char *)script_get(eap, eap->arg);
339 if (!eap->skip && ensure_ruby_initialized())
341 if (script == NULL)
342 rb_eval_string_protect((char *)eap->arg, &state);
343 else
344 rb_eval_string_protect(script, &state);
345 if (state)
346 error_print(state);
348 vim_free(script);
351 void ex_rubydo(exarg_T *eap)
353 int state;
354 linenr_T i;
356 if (ensure_ruby_initialized())
358 if (u_save(eap->line1 - 1, eap->line2 + 1) != OK)
359 return;
360 for (i = eap->line1; i <= eap->line2; i++) {
361 VALUE line, oldline;
363 line = oldline = rb_str_new2((char *)ml_get(i));
364 rb_lastline_set(line);
365 rb_eval_string_protect((char *) eap->arg, &state);
366 if (state) {
367 error_print(state);
368 break;
370 line = rb_lastline_get();
371 if (!NIL_P(line)) {
372 if (TYPE(line) != T_STRING) {
373 EMSG(_("E265: $_ must be an instance of String"));
374 return;
376 ml_replace(i, (char_u *) STR2CSTR(line), 1);
377 changed();
378 #ifdef SYNTAX_HL
379 syn_changed(i); /* recompute syntax hl. for this line */
380 #endif
383 check_cursor();
384 update_curbuf(NOT_VALID);
388 void ex_rubyfile(exarg_T *eap)
390 int state;
392 if (ensure_ruby_initialized())
394 rb_load_protect(rb_str_new2((char *) eap->arg), 0, &state);
395 if (state) error_print(state);
399 void ruby_buffer_free(buf_T *buf)
401 if (buf->b_ruby_ref)
403 rb_hash_aset(objtbl, rb_obj_id((VALUE) buf->b_ruby_ref), Qnil);
404 RDATA(buf->b_ruby_ref)->data = NULL;
408 void ruby_window_free(win_T *win)
410 if (win->w_ruby_ref)
412 rb_hash_aset(objtbl, rb_obj_id((VALUE) win->w_ruby_ref), Qnil);
413 RDATA(win->w_ruby_ref)->data = NULL;
417 static int ensure_ruby_initialized(void)
419 if (!ruby_initialized)
421 #ifdef DYNAMIC_RUBY
422 if (ruby_enabled(TRUE))
424 #endif
425 #ifdef _WIN32
426 /* suggested by Ariya Mizutani */
427 int argc = 1;
428 char *argv[] = {"gvim.exe"};
429 NtInitialize(&argc, &argv);
430 #endif
431 ruby_init();
432 ruby_init_loadpath();
433 ruby_io_init();
434 ruby_vim_init();
435 ruby_initialized = 1;
436 #ifdef DYNAMIC_RUBY
438 else
440 EMSG(_("E266: Sorry, this command is disabled, the Ruby library could not be loaded."));
441 return 0;
443 #endif
445 return ruby_initialized;
448 static void error_print(int state)
450 #ifndef DYNAMIC_RUBY
451 RUBYEXTERN VALUE ruby_errinfo;
452 #endif
453 VALUE eclass;
454 VALUE einfo;
455 char buff[BUFSIZ];
457 #define TAG_RETURN 0x1
458 #define TAG_BREAK 0x2
459 #define TAG_NEXT 0x3
460 #define TAG_RETRY 0x4
461 #define TAG_REDO 0x5
462 #define TAG_RAISE 0x6
463 #define TAG_THROW 0x7
464 #define TAG_FATAL 0x8
465 #define TAG_MASK 0xf
467 switch (state) {
468 case TAG_RETURN:
469 EMSG(_("E267: unexpected return"));
470 break;
471 case TAG_NEXT:
472 EMSG(_("E268: unexpected next"));
473 break;
474 case TAG_BREAK:
475 EMSG(_("E269: unexpected break"));
476 break;
477 case TAG_REDO:
478 EMSG(_("E270: unexpected redo"));
479 break;
480 case TAG_RETRY:
481 EMSG(_("E271: retry outside of rescue clause"));
482 break;
483 case TAG_RAISE:
484 case TAG_FATAL:
485 eclass = CLASS_OF(ruby_errinfo);
486 einfo = rb_obj_as_string(ruby_errinfo);
487 if (eclass == rb_eRuntimeError && RSTRING(einfo)->len == 0) {
488 EMSG(_("E272: unhandled exception"));
490 else {
491 VALUE epath;
492 char *p;
494 epath = rb_class_path(eclass);
495 vim_snprintf(buff, BUFSIZ, "%s: %s",
496 RSTRING(epath)->ptr, RSTRING(einfo)->ptr);
497 p = strchr(buff, '\n');
498 if (p) *p = '\0';
499 EMSG(buff);
501 break;
502 default:
503 vim_snprintf(buff, BUFSIZ, _("E273: unknown longjmp status %d"), state);
504 EMSG(buff);
505 break;
509 static VALUE vim_message(VALUE self UNUSED, VALUE str)
511 char *buff, *p;
513 str = rb_obj_as_string(str);
514 buff = ALLOCA_N(char, RSTRING(str)->len);
515 strcpy(buff, RSTRING(str)->ptr);
516 p = strchr(buff, '\n');
517 if (p) *p = '\0';
518 MSG(buff);
519 return Qnil;
522 static VALUE vim_set_option(VALUE self UNUSED, VALUE str)
524 do_set((char_u *)STR2CSTR(str), 0);
525 update_screen(NOT_VALID);
526 return Qnil;
529 static VALUE vim_command(VALUE self UNUSED, VALUE str)
531 do_cmdline_cmd((char_u *)STR2CSTR(str));
532 return Qnil;
535 static VALUE vim_evaluate(VALUE self UNUSED, VALUE str)
537 #ifdef FEAT_EVAL
538 char_u *value = eval_to_string((char_u *)STR2CSTR(str), NULL, TRUE);
540 if (value != NULL)
542 VALUE val = rb_str_new2((char *)value);
543 vim_free(value);
544 return val;
546 else
547 #endif
548 return Qnil;
551 static VALUE buffer_new(buf_T *buf)
553 if (buf->b_ruby_ref)
555 return (VALUE) buf->b_ruby_ref;
557 else
559 VALUE obj = Data_Wrap_Struct(cBuffer, 0, 0, buf);
560 buf->b_ruby_ref = (void *) obj;
561 rb_hash_aset(objtbl, rb_obj_id(obj), obj);
562 return obj;
566 static buf_T *get_buf(VALUE obj)
568 buf_T *buf;
570 Data_Get_Struct(obj, buf_T, buf);
571 if (buf == NULL)
572 rb_raise(eDeletedBufferError, "attempt to refer to deleted buffer");
573 return buf;
576 static VALUE buffer_s_current()
578 return buffer_new(curbuf);
581 static VALUE buffer_s_count()
583 buf_T *b;
584 int n = 0;
586 for (b = firstbuf; b != NULL; b = b->b_next)
588 /* Deleted buffers should not be counted
589 * SegPhault - 01/07/05 */
590 if (b->b_p_bl)
591 n++;
594 return INT2NUM(n);
597 static VALUE buffer_s_aref(VALUE self UNUSED, VALUE num)
599 buf_T *b;
600 int n = NUM2INT(num);
602 for (b = firstbuf; b != NULL; b = b->b_next)
604 /* Deleted buffers should not be counted
605 * SegPhault - 01/07/05 */
606 if (!b->b_p_bl)
607 continue;
609 if (n == 0)
610 return buffer_new(b);
612 n--;
614 return Qnil;
617 static VALUE buffer_name(VALUE self)
619 buf_T *buf = get_buf(self);
621 return buf->b_ffname ? rb_str_new2((char *)buf->b_ffname) : Qnil;
624 static VALUE buffer_number(VALUE self)
626 buf_T *buf = get_buf(self);
628 return INT2NUM(buf->b_fnum);
631 static VALUE buffer_count(VALUE self)
633 buf_T *buf = get_buf(self);
635 return INT2NUM(buf->b_ml.ml_line_count);
638 static VALUE get_buffer_line(buf_T *buf, linenr_T n)
640 if (n > 0 && n <= buf->b_ml.ml_line_count)
642 char *line = (char *)ml_get_buf(buf, n, FALSE);
643 return line ? rb_str_new2(line) : Qnil;
645 rb_raise(rb_eIndexError, "index %d out of buffer", n);
646 #ifndef __GNUC__
647 return Qnil; /* For stop warning */
648 #endif
651 static VALUE buffer_aref(VALUE self, VALUE num)
653 buf_T *buf = get_buf(self);
655 if (buf != NULL)
656 return get_buffer_line(buf, (linenr_T)NUM2LONG(num));
657 return Qnil; /* For stop warning */
660 static VALUE set_buffer_line(buf_T *buf, linenr_T n, VALUE str)
662 char *line = STR2CSTR(str);
663 aco_save_T aco;
665 if (n > 0 && n <= buf->b_ml.ml_line_count && line != NULL)
667 /* set curwin/curbuf for "buf" and save some things */
668 aucmd_prepbuf(&aco, buf);
670 if (u_savesub(n) == OK) {
671 ml_replace(n, (char_u *)line, TRUE);
672 changed();
673 #ifdef SYNTAX_HL
674 syn_changed(n); /* recompute syntax hl. for this line */
675 #endif
678 /* restore curwin/curbuf and a few other things */
679 aucmd_restbuf(&aco);
680 /* Careful: autocommands may have made "buf" invalid! */
682 update_curbuf(NOT_VALID);
684 else
686 rb_raise(rb_eIndexError, "index %d out of buffer", n);
687 #ifndef __GNUC__
688 return Qnil; /* For stop warning */
689 #endif
691 return str;
694 static VALUE buffer_aset(VALUE self, VALUE num, VALUE str)
696 buf_T *buf = get_buf(self);
698 if (buf != NULL)
699 return set_buffer_line(buf, (linenr_T)NUM2LONG(num), str);
700 return str;
703 static VALUE buffer_delete(VALUE self, VALUE num)
705 buf_T *buf = get_buf(self);
706 long n = NUM2LONG(num);
707 aco_save_T aco;
709 if (n > 0 && n <= buf->b_ml.ml_line_count)
711 /* set curwin/curbuf for "buf" and save some things */
712 aucmd_prepbuf(&aco, buf);
714 if (u_savedel(n, 1) == OK) {
715 ml_delete(n, 0);
717 /* Changes to non-active buffers should properly refresh
718 * SegPhault - 01/09/05 */
719 deleted_lines_mark(n, 1L);
721 changed();
724 /* restore curwin/curbuf and a few other things */
725 aucmd_restbuf(&aco);
726 /* Careful: autocommands may have made "buf" invalid! */
728 update_curbuf(NOT_VALID);
730 else
732 rb_raise(rb_eIndexError, "index %d out of buffer", n);
734 return Qnil;
737 static VALUE buffer_append(VALUE self, VALUE num, VALUE str)
739 buf_T *buf = get_buf(self);
740 char *line = STR2CSTR(str);
741 long n = NUM2LONG(num);
742 aco_save_T aco;
744 if (n >= 0 && n <= buf->b_ml.ml_line_count && line != NULL)
746 /* set curwin/curbuf for "buf" and save some things */
747 aucmd_prepbuf(&aco, buf);
749 if (u_inssub(n + 1) == OK) {
750 ml_append(n, (char_u *) line, (colnr_T) 0, FALSE);
752 /* Changes to non-active buffers should properly refresh screen
753 * SegPhault - 12/20/04 */
754 appended_lines_mark(n, 1L);
756 changed();
759 /* restore curwin/curbuf and a few other things */
760 aucmd_restbuf(&aco);
761 /* Careful: autocommands may have made "buf" invalid! */
763 update_curbuf(NOT_VALID);
765 else {
766 rb_raise(rb_eIndexError, "index %d out of buffer", n);
768 return str;
771 static VALUE window_new(win_T *win)
773 if (win->w_ruby_ref)
775 return (VALUE) win->w_ruby_ref;
777 else
779 VALUE obj = Data_Wrap_Struct(cVimWindow, 0, 0, win);
780 win->w_ruby_ref = (void *) obj;
781 rb_hash_aset(objtbl, rb_obj_id(obj), obj);
782 return obj;
786 static win_T *get_win(VALUE obj)
788 win_T *win;
790 Data_Get_Struct(obj, win_T, win);
791 if (win == NULL)
792 rb_raise(eDeletedWindowError, "attempt to refer to deleted window");
793 return win;
796 static VALUE window_s_current()
798 return window_new(curwin);
802 * Added line manipulation functions
803 * SegPhault - 03/07/05
805 static VALUE line_s_current()
807 return get_buffer_line(curbuf, curwin->w_cursor.lnum);
810 static VALUE set_current_line(VALUE self UNUSED, VALUE str)
812 return set_buffer_line(curbuf, curwin->w_cursor.lnum, str);
815 static VALUE current_line_number()
817 return INT2FIX((int)curwin->w_cursor.lnum);
822 static VALUE window_s_count()
824 #ifdef FEAT_WINDOWS
825 win_T *w;
826 int n = 0;
828 for (w = firstwin; w != NULL; w = w->w_next)
829 n++;
830 return INT2NUM(n);
831 #else
832 return INT2NUM(1);
833 #endif
836 static VALUE window_s_aref(VALUE self UNUSED, VALUE num)
838 win_T *w;
839 int n = NUM2INT(num);
841 #ifndef FEAT_WINDOWS
842 w = curwin;
843 #else
844 for (w = firstwin; w != NULL; w = w->w_next, --n)
845 #endif
846 if (n == 0)
847 return window_new(w);
848 return Qnil;
851 static VALUE window_buffer(VALUE self)
853 win_T *win = get_win(self);
855 return buffer_new(win->w_buffer);
858 static VALUE window_height(VALUE self)
860 win_T *win = get_win(self);
862 return INT2NUM(win->w_height);
865 static VALUE window_set_height(VALUE self, VALUE height)
867 win_T *win = get_win(self);
868 win_T *savewin = curwin;
870 curwin = win;
871 win_setheight(NUM2INT(height));
872 curwin = savewin;
873 return height;
876 static VALUE window_width(VALUE self)
878 win_T *win = get_win(self);
880 return INT2NUM(win->w_width);
883 static VALUE window_set_width(VALUE self, VALUE width)
885 win_T *win = get_win(self);
886 win_T *savewin = curwin;
888 curwin = win;
889 win_setwidth(NUM2INT(width));
890 curwin = savewin;
891 return width;
894 static VALUE window_cursor(VALUE self)
896 win_T *win = get_win(self);
898 return rb_assoc_new(INT2NUM(win->w_cursor.lnum), INT2NUM(win->w_cursor.col));
901 static VALUE window_set_cursor(VALUE self, VALUE pos)
903 VALUE lnum, col;
904 win_T *win = get_win(self);
906 Check_Type(pos, T_ARRAY);
907 if (RARRAY(pos)->len != 2)
908 rb_raise(rb_eArgError, "array length must be 2");
909 lnum = RARRAY(pos)->ptr[0];
910 col = RARRAY(pos)->ptr[1];
911 win->w_cursor.lnum = NUM2LONG(lnum);
912 win->w_cursor.col = NUM2UINT(col);
913 check_cursor(); /* put cursor on an existing line */
914 update_screen(NOT_VALID);
915 return Qnil;
918 static VALUE f_p(int argc, VALUE *argv, VALUE self UNUSED)
920 int i;
921 VALUE str = rb_str_new("", 0);
923 for (i = 0; i < argc; i++) {
924 if (i > 0) rb_str_cat(str, ", ", 2);
925 rb_str_concat(str, rb_inspect(argv[i]));
927 MSG(RSTRING(str)->ptr);
928 return Qnil;
931 static void ruby_io_init(void)
933 #ifndef DYNAMIC_RUBY
934 RUBYEXTERN VALUE rb_stdout;
935 #endif
937 rb_stdout = rb_obj_alloc(rb_cObject);
938 rb_define_singleton_method(rb_stdout, "write", vim_message, 1);
939 rb_define_global_function("p", f_p, -1);
942 static void ruby_vim_init(void)
944 objtbl = rb_hash_new();
945 rb_global_variable(&objtbl);
947 /* The Vim module used to be called "VIM", but "Vim" is better. Make an
948 * alias "VIM" for backwards compatiblity. */
949 mVIM = rb_define_module("Vim");
950 rb_define_const(rb_cObject, "VIM", mVIM);
951 rb_define_const(mVIM, "VERSION_MAJOR", INT2NUM(VIM_VERSION_MAJOR));
952 rb_define_const(mVIM, "VERSION_MINOR", INT2NUM(VIM_VERSION_MINOR));
953 rb_define_const(mVIM, "VERSION_BUILD", INT2NUM(VIM_VERSION_BUILD));
954 rb_define_const(mVIM, "VERSION_PATCHLEVEL", INT2NUM(VIM_VERSION_PATCHLEVEL));
955 rb_define_const(mVIM, "VERSION_SHORT", rb_str_new2(VIM_VERSION_SHORT));
956 rb_define_const(mVIM, "VERSION_MEDIUM", rb_str_new2(VIM_VERSION_MEDIUM));
957 rb_define_const(mVIM, "VERSION_LONG", rb_str_new2(VIM_VERSION_LONG));
958 rb_define_const(mVIM, "VERSION_LONG_DATE", rb_str_new2(VIM_VERSION_LONG_DATE));
959 rb_define_module_function(mVIM, "message", vim_message, 1);
960 rb_define_module_function(mVIM, "set_option", vim_set_option, 1);
961 rb_define_module_function(mVIM, "command", vim_command, 1);
962 rb_define_module_function(mVIM, "evaluate", vim_evaluate, 1);
964 eDeletedBufferError = rb_define_class_under(mVIM, "DeletedBufferError",
965 rb_eStandardError);
966 eDeletedWindowError = rb_define_class_under(mVIM, "DeletedWindowError",
967 rb_eStandardError);
969 cBuffer = rb_define_class_under(mVIM, "Buffer", rb_cObject);
970 rb_define_singleton_method(cBuffer, "current", buffer_s_current, 0);
971 rb_define_singleton_method(cBuffer, "count", buffer_s_count, 0);
972 rb_define_singleton_method(cBuffer, "[]", buffer_s_aref, 1);
973 rb_define_method(cBuffer, "name", buffer_name, 0);
974 rb_define_method(cBuffer, "number", buffer_number, 0);
975 rb_define_method(cBuffer, "count", buffer_count, 0);
976 rb_define_method(cBuffer, "length", buffer_count, 0);
977 rb_define_method(cBuffer, "[]", buffer_aref, 1);
978 rb_define_method(cBuffer, "[]=", buffer_aset, 2);
979 rb_define_method(cBuffer, "delete", buffer_delete, 1);
980 rb_define_method(cBuffer, "append", buffer_append, 2);
982 /* Added line manipulation functions
983 * SegPhault - 03/07/05 */
984 rb_define_method(cBuffer, "line_number", current_line_number, 0);
985 rb_define_method(cBuffer, "line", line_s_current, 0);
986 rb_define_method(cBuffer, "line=", set_current_line, 1);
989 cVimWindow = rb_define_class_under(mVIM, "Window", rb_cObject);
990 rb_define_singleton_method(cVimWindow, "current", window_s_current, 0);
991 rb_define_singleton_method(cVimWindow, "count", window_s_count, 0);
992 rb_define_singleton_method(cVimWindow, "[]", window_s_aref, 1);
993 rb_define_method(cVimWindow, "buffer", window_buffer, 0);
994 rb_define_method(cVimWindow, "height", window_height, 0);
995 rb_define_method(cVimWindow, "height=", window_set_height, 1);
996 rb_define_method(cVimWindow, "width", window_width, 0);
997 rb_define_method(cVimWindow, "width=", window_set_width, 1);
998 rb_define_method(cVimWindow, "cursor", window_cursor, 0);
999 rb_define_method(cVimWindow, "cursor=", window_set_cursor, 1);
1001 rb_define_virtual_variable("$curbuf", buffer_s_current, 0);
1002 rb_define_virtual_variable("$curwin", window_s_current, 0);