Merge branch 'vim'
[MacVim.git] / src / if_ruby.c
blob656393908be5a0e8a373d96af94e4b4127686e0b
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 #ifdef FEAT_GUI_MACVIM
52 # include <Ruby/Ruby.h>
53 #else
54 # include <ruby.h>
55 #endif
57 #undef EXTERN
58 #undef _
60 /* T_DATA defined both by Ruby and Mac header files, hack around it... */
61 #if defined(MACOS_X_UNIX) || defined(macintosh)
62 # define __OPENTRANSPORT__
63 # define __OPENTRANSPORTPROTOCOL__
64 # define __OPENTRANSPORTPROVIDERS__
65 #endif
67 #include "vim.h"
68 #include "version.h"
70 #if defined(PROTO) && !defined(FEAT_RUBY)
71 /* Define these to be able to generate the function prototypes. */
72 # define VALUE int
73 # define RUBY_DATA_FUNC int
74 #endif
76 static int ruby_initialized = 0;
77 static VALUE objtbl;
79 static VALUE mVIM;
80 static VALUE cBuffer;
81 static VALUE cVimWindow;
82 static VALUE eDeletedBufferError;
83 static VALUE eDeletedWindowError;
85 static int ensure_ruby_initialized(void);
86 static void error_print(int);
87 static void ruby_io_init(void);
88 static void ruby_vim_init(void);
90 #if defined(DYNAMIC_RUBY) || defined(PROTO)
91 #ifdef PROTO
92 # define HINSTANCE int /* for generating prototypes */
93 #endif
96 * Wrapper defines
98 #define rb_assoc_new dll_rb_assoc_new
99 #define rb_cObject (*dll_rb_cObject)
100 #define rb_check_type dll_rb_check_type
101 #define rb_class_path dll_rb_class_path
102 #define rb_data_object_alloc dll_rb_data_object_alloc
103 #define rb_define_class_under dll_rb_define_class_under
104 #define rb_define_const dll_rb_define_const
105 #define rb_define_global_function dll_rb_define_global_function
106 #define rb_define_method dll_rb_define_method
107 #define rb_define_module dll_rb_define_module
108 #define rb_define_module_function dll_rb_define_module_function
109 #define rb_define_singleton_method dll_rb_define_singleton_method
110 #define rb_define_virtual_variable dll_rb_define_virtual_variable
111 #define rb_stdout (*dll_rb_stdout)
112 #define rb_eArgError (*dll_rb_eArgError)
113 #define rb_eIndexError (*dll_rb_eIndexError)
114 #define rb_eRuntimeError (*dll_rb_eRuntimeError)
115 #define rb_eStandardError (*dll_rb_eStandardError)
116 #define rb_eval_string_protect dll_rb_eval_string_protect
117 #define rb_global_variable dll_rb_global_variable
118 #define rb_hash_aset dll_rb_hash_aset
119 #define rb_hash_new dll_rb_hash_new
120 #define rb_inspect dll_rb_inspect
121 #define rb_int2inum dll_rb_int2inum
122 #define rb_lastline_get dll_rb_lastline_get
123 #define rb_lastline_set dll_rb_lastline_set
124 #define rb_load_protect dll_rb_load_protect
125 #define rb_num2long dll_rb_num2long
126 #define rb_num2ulong dll_rb_num2ulong
127 #define rb_obj_alloc dll_rb_obj_alloc
128 #define rb_obj_as_string dll_rb_obj_as_string
129 #define rb_obj_id dll_rb_obj_id
130 #define rb_raise dll_rb_raise
131 #define rb_str2cstr dll_rb_str2cstr
132 #define rb_str_cat dll_rb_str_cat
133 #define rb_str_concat dll_rb_str_concat
134 #define rb_str_new dll_rb_str_new
135 #define rb_str_new2 dll_rb_str_new2
136 #define ruby_errinfo (*dll_ruby_errinfo)
137 #define ruby_init dll_ruby_init
138 #define ruby_init_loadpath dll_ruby_init_loadpath
139 #if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 18
140 # define rb_w32_snprintf dll_rb_w32_snprintf
141 #endif
144 * Pointers for dynamic link
146 static VALUE (*dll_rb_assoc_new) (VALUE, VALUE);
147 static VALUE *dll_rb_cFalseClass;
148 static VALUE *dll_rb_cFixnum;
149 static VALUE *dll_rb_cNilClass;
150 static VALUE *dll_rb_cObject;
151 static VALUE *dll_rb_cSymbol;
152 static VALUE *dll_rb_cTrueClass;
153 static void (*dll_rb_check_type) (VALUE,int);
154 static VALUE (*dll_rb_class_path) (VALUE);
155 static VALUE (*dll_rb_data_object_alloc) (VALUE, void*, RUBY_DATA_FUNC, RUBY_DATA_FUNC);
156 static VALUE (*dll_rb_define_class_under) (VALUE, const char*, VALUE);
157 static void (*dll_rb_define_const) (VALUE,const char*,VALUE);
158 static void (*dll_rb_define_global_function) (const char*,VALUE(*)(),int);
159 static void (*dll_rb_define_method) (VALUE,const char*,VALUE(*)(),int);
160 static VALUE (*dll_rb_define_module) (const char*);
161 static void (*dll_rb_define_module_function) (VALUE,const char*,VALUE(*)(),int);
162 static void (*dll_rb_define_singleton_method) (VALUE,const char*,VALUE(*)(),int);
163 static void (*dll_rb_define_virtual_variable) (const char*,VALUE(*)(),void(*)());
164 static VALUE *dll_rb_stdout;
165 static VALUE *dll_rb_eArgError;
166 static VALUE *dll_rb_eIndexError;
167 static VALUE *dll_rb_eRuntimeError;
168 static VALUE *dll_rb_eStandardError;
169 static VALUE (*dll_rb_eval_string_protect) (const char*, int*);
170 static void (*dll_rb_global_variable) (VALUE*);
171 static VALUE (*dll_rb_hash_aset) (VALUE, VALUE, VALUE);
172 static VALUE (*dll_rb_hash_new) (void);
173 static VALUE (*dll_rb_inspect) (VALUE);
174 static VALUE (*dll_rb_int2inum) (long);
175 static VALUE (*dll_rb_int2inum) (long);
176 static VALUE (*dll_rb_lastline_get) (void);
177 static void (*dll_rb_lastline_set) (VALUE);
178 static void (*dll_rb_load_protect) (VALUE, int, int*);
179 static long (*dll_rb_num2long) (VALUE);
180 static unsigned long (*dll_rb_num2ulong) (VALUE);
181 static VALUE (*dll_rb_obj_alloc) (VALUE);
182 static VALUE (*dll_rb_obj_as_string) (VALUE);
183 static VALUE (*dll_rb_obj_id) (VALUE);
184 static void (*dll_rb_raise) (VALUE, const char*, ...);
185 static char *(*dll_rb_str2cstr) (VALUE,int*);
186 static VALUE (*dll_rb_str_cat) (VALUE, const char*, long);
187 static VALUE (*dll_rb_str_concat) (VALUE, VALUE);
188 static VALUE (*dll_rb_str_new) (const char*, long);
189 static VALUE (*dll_rb_str_new2) (const char*);
190 static VALUE *dll_ruby_errinfo;
191 static void (*dll_ruby_init) (void);
192 static void (*dll_ruby_init_loadpath) (void);
193 #if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 18
194 static int (*dll_rb_w32_snprintf)(char*, size_t, const char*, ...);
195 #endif
197 static HINSTANCE hinstRuby = 0; /* Instance of ruby.dll */
200 * Table of name to function pointer of ruby.
202 #define RUBY_PROC FARPROC
203 static struct
205 char *name;
206 RUBY_PROC *ptr;
207 } ruby_funcname_table[] =
209 {"rb_assoc_new", (RUBY_PROC*)&dll_rb_assoc_new},
210 {"rb_cFalseClass", (RUBY_PROC*)&dll_rb_cFalseClass},
211 {"rb_cFixnum", (RUBY_PROC*)&dll_rb_cFixnum},
212 {"rb_cNilClass", (RUBY_PROC*)&dll_rb_cNilClass},
213 {"rb_cObject", (RUBY_PROC*)&dll_rb_cObject},
214 {"rb_cSymbol", (RUBY_PROC*)&dll_rb_cSymbol},
215 {"rb_cTrueClass", (RUBY_PROC*)&dll_rb_cTrueClass},
216 {"rb_check_type", (RUBY_PROC*)&dll_rb_check_type},
217 {"rb_class_path", (RUBY_PROC*)&dll_rb_class_path},
218 {"rb_data_object_alloc", (RUBY_PROC*)&dll_rb_data_object_alloc},
219 {"rb_define_class_under", (RUBY_PROC*)&dll_rb_define_class_under},
220 {"rb_define_const", (RUBY_PROC*)&dll_rb_define_const},
221 {"rb_define_global_function", (RUBY_PROC*)&dll_rb_define_global_function},
222 {"rb_define_method", (RUBY_PROC*)&dll_rb_define_method},
223 {"rb_define_module", (RUBY_PROC*)&dll_rb_define_module},
224 {"rb_define_module_function", (RUBY_PROC*)&dll_rb_define_module_function},
225 {"rb_define_singleton_method", (RUBY_PROC*)&dll_rb_define_singleton_method},
226 {"rb_define_virtual_variable", (RUBY_PROC*)&dll_rb_define_virtual_variable},
227 {"rb_stdout", (RUBY_PROC*)&dll_rb_stdout},
228 {"rb_eArgError", (RUBY_PROC*)&dll_rb_eArgError},
229 {"rb_eIndexError", (RUBY_PROC*)&dll_rb_eIndexError},
230 {"rb_eRuntimeError", (RUBY_PROC*)&dll_rb_eRuntimeError},
231 {"rb_eStandardError", (RUBY_PROC*)&dll_rb_eStandardError},
232 {"rb_eval_string_protect", (RUBY_PROC*)&dll_rb_eval_string_protect},
233 {"rb_global_variable", (RUBY_PROC*)&dll_rb_global_variable},
234 {"rb_hash_aset", (RUBY_PROC*)&dll_rb_hash_aset},
235 {"rb_hash_new", (RUBY_PROC*)&dll_rb_hash_new},
236 {"rb_inspect", (RUBY_PROC*)&dll_rb_inspect},
237 {"rb_int2inum", (RUBY_PROC*)&dll_rb_int2inum},
238 {"rb_lastline_get", (RUBY_PROC*)&dll_rb_lastline_get},
239 {"rb_lastline_set", (RUBY_PROC*)&dll_rb_lastline_set},
240 {"rb_load_protect", (RUBY_PROC*)&dll_rb_load_protect},
241 {"rb_num2long", (RUBY_PROC*)&dll_rb_num2long},
242 {"rb_num2ulong", (RUBY_PROC*)&dll_rb_num2ulong},
243 {"rb_obj_alloc", (RUBY_PROC*)&dll_rb_obj_alloc},
244 {"rb_obj_as_string", (RUBY_PROC*)&dll_rb_obj_as_string},
245 {"rb_obj_id", (RUBY_PROC*)&dll_rb_obj_id},
246 {"rb_raise", (RUBY_PROC*)&dll_rb_raise},
247 {"rb_str2cstr", (RUBY_PROC*)&dll_rb_str2cstr},
248 {"rb_str_cat", (RUBY_PROC*)&dll_rb_str_cat},
249 {"rb_str_concat", (RUBY_PROC*)&dll_rb_str_concat},
250 {"rb_str_new", (RUBY_PROC*)&dll_rb_str_new},
251 {"rb_str_new2", (RUBY_PROC*)&dll_rb_str_new2},
252 {"ruby_errinfo", (RUBY_PROC*)&dll_ruby_errinfo},
253 {"ruby_init", (RUBY_PROC*)&dll_ruby_init},
254 {"ruby_init_loadpath", (RUBY_PROC*)&dll_ruby_init_loadpath},
255 #if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 18
256 {"rb_w32_snprintf", (RUBY_PROC*)&dll_rb_w32_snprintf},
257 #endif
258 {"", NULL},
262 * Free ruby.dll
264 static void
265 end_dynamic_ruby()
267 if (hinstRuby)
269 FreeLibrary(hinstRuby);
270 hinstRuby = 0;
275 * Load library and get all pointers.
276 * Parameter 'libname' provides name of DLL.
277 * Return OK or FAIL.
279 static int
280 ruby_runtime_link_init(char *libname, int verbose)
282 int i;
284 if (hinstRuby)
285 return OK;
286 hinstRuby = LoadLibrary(libname);
287 if (!hinstRuby)
289 if (verbose)
290 EMSG2(_(e_loadlib), libname);
291 return FAIL;
294 for (i = 0; ruby_funcname_table[i].ptr; ++i)
296 if (!(*ruby_funcname_table[i].ptr = GetProcAddress(hinstRuby,
297 ruby_funcname_table[i].name)))
299 FreeLibrary(hinstRuby);
300 hinstRuby = 0;
301 if (verbose)
302 EMSG2(_(e_loadfunc), ruby_funcname_table[i].name);
303 return FAIL;
306 return OK;
310 * If ruby is enabled (there is installed ruby on Windows system) return TRUE,
311 * else FALSE.
314 ruby_enabled(verbose)
315 int verbose;
317 return ruby_runtime_link_init(DYNAMIC_RUBY_DLL, verbose) == OK;
319 #endif /* defined(DYNAMIC_RUBY) || defined(PROTO) */
321 void
322 ruby_end()
324 #ifdef DYNAMIC_RUBY
325 end_dynamic_ruby();
326 #endif
329 void ex_ruby(exarg_T *eap)
331 int state;
332 char *script = NULL;
334 script = (char *)script_get(eap, eap->arg);
335 if (!eap->skip && ensure_ruby_initialized())
337 if (script == NULL)
338 rb_eval_string_protect((char *)eap->arg, &state);
339 else
340 rb_eval_string_protect(script, &state);
341 if (state)
342 error_print(state);
344 vim_free(script);
347 void ex_rubydo(exarg_T *eap)
349 int state;
350 linenr_T i;
352 if (ensure_ruby_initialized())
354 if (u_save(eap->line1 - 1, eap->line2 + 1) != OK)
355 return;
356 for (i = eap->line1; i <= eap->line2; i++) {
357 VALUE line, oldline;
359 line = oldline = rb_str_new2((char *)ml_get(i));
360 rb_lastline_set(line);
361 rb_eval_string_protect((char *) eap->arg, &state);
362 if (state) {
363 error_print(state);
364 break;
366 line = rb_lastline_get();
367 if (!NIL_P(line)) {
368 if (TYPE(line) != T_STRING) {
369 EMSG(_("E265: $_ must be an instance of String"));
370 return;
372 ml_replace(i, (char_u *) STR2CSTR(line), 1);
373 changed();
374 #ifdef SYNTAX_HL
375 syn_changed(i); /* recompute syntax hl. for this line */
376 #endif
379 check_cursor();
380 update_curbuf(NOT_VALID);
384 void ex_rubyfile(exarg_T *eap)
386 int state;
388 if (ensure_ruby_initialized())
390 rb_load_protect(rb_str_new2((char *) eap->arg), 0, &state);
391 if (state) error_print(state);
395 void ruby_buffer_free(buf_T *buf)
397 if (buf->b_ruby_ref)
399 rb_hash_aset(objtbl, rb_obj_id((VALUE) buf->b_ruby_ref), Qnil);
400 RDATA(buf->b_ruby_ref)->data = NULL;
404 void ruby_window_free(win_T *win)
406 if (win->w_ruby_ref)
408 rb_hash_aset(objtbl, rb_obj_id((VALUE) win->w_ruby_ref), Qnil);
409 RDATA(win->w_ruby_ref)->data = NULL;
413 static int ensure_ruby_initialized(void)
415 if (!ruby_initialized)
417 #ifdef DYNAMIC_RUBY
418 if (ruby_enabled(TRUE))
420 #endif
421 ruby_init();
422 ruby_init_loadpath();
423 ruby_io_init();
424 ruby_vim_init();
425 ruby_initialized = 1;
426 #ifdef DYNAMIC_RUBY
428 else
430 EMSG(_("E266: Sorry, this command is disabled, the Ruby library could not be loaded."));
431 return 0;
433 #endif
435 return ruby_initialized;
438 static void error_print(int state)
440 #ifndef DYNAMIC_RUBY
441 RUBYEXTERN VALUE ruby_errinfo;
442 #endif
443 VALUE eclass;
444 VALUE einfo;
445 char buff[BUFSIZ];
447 #define TAG_RETURN 0x1
448 #define TAG_BREAK 0x2
449 #define TAG_NEXT 0x3
450 #define TAG_RETRY 0x4
451 #define TAG_REDO 0x5
452 #define TAG_RAISE 0x6
453 #define TAG_THROW 0x7
454 #define TAG_FATAL 0x8
455 #define TAG_MASK 0xf
457 switch (state) {
458 case TAG_RETURN:
459 EMSG(_("E267: unexpected return"));
460 break;
461 case TAG_NEXT:
462 EMSG(_("E268: unexpected next"));
463 break;
464 case TAG_BREAK:
465 EMSG(_("E269: unexpected break"));
466 break;
467 case TAG_REDO:
468 EMSG(_("E270: unexpected redo"));
469 break;
470 case TAG_RETRY:
471 EMSG(_("E271: retry outside of rescue clause"));
472 break;
473 case TAG_RAISE:
474 case TAG_FATAL:
475 eclass = CLASS_OF(ruby_errinfo);
476 einfo = rb_obj_as_string(ruby_errinfo);
477 if (eclass == rb_eRuntimeError && RSTRING(einfo)->len == 0) {
478 EMSG(_("E272: unhandled exception"));
480 else {
481 VALUE epath;
482 char *p;
484 epath = rb_class_path(eclass);
485 vim_snprintf(buff, BUFSIZ, "%s: %s",
486 RSTRING(epath)->ptr, RSTRING(einfo)->ptr);
487 p = strchr(buff, '\n');
488 if (p) *p = '\0';
489 EMSG(buff);
491 break;
492 default:
493 vim_snprintf(buff, BUFSIZ, _("E273: unknown longjmp status %d"), state);
494 EMSG(buff);
495 break;
499 static VALUE vim_message(VALUE self UNUSED, VALUE str)
501 char *buff, *p;
503 str = rb_obj_as_string(str);
504 buff = ALLOCA_N(char, RSTRING(str)->len);
505 strcpy(buff, RSTRING(str)->ptr);
506 p = strchr(buff, '\n');
507 if (p) *p = '\0';
508 MSG(buff);
509 return Qnil;
512 static VALUE vim_set_option(VALUE self UNUSED, VALUE str)
514 do_set((char_u *)STR2CSTR(str), 0);
515 update_screen(NOT_VALID);
516 return Qnil;
519 static VALUE vim_command(VALUE self UNUSED, VALUE str)
521 do_cmdline_cmd((char_u *)STR2CSTR(str));
522 return Qnil;
525 static VALUE vim_evaluate(VALUE self UNUSED, VALUE str)
527 #ifdef FEAT_EVAL
528 char_u *value = eval_to_string((char_u *)STR2CSTR(str), NULL, TRUE);
530 if (value != NULL)
532 VALUE val = rb_str_new2((char *)value);
533 vim_free(value);
534 return val;
536 else
537 #endif
538 return Qnil;
541 static VALUE buffer_new(buf_T *buf)
543 if (buf->b_ruby_ref)
545 return (VALUE) buf->b_ruby_ref;
547 else
549 VALUE obj = Data_Wrap_Struct(cBuffer, 0, 0, buf);
550 buf->b_ruby_ref = (void *) obj;
551 rb_hash_aset(objtbl, rb_obj_id(obj), obj);
552 return obj;
556 static buf_T *get_buf(VALUE obj)
558 buf_T *buf;
560 Data_Get_Struct(obj, buf_T, buf);
561 if (buf == NULL)
562 rb_raise(eDeletedBufferError, "attempt to refer to deleted buffer");
563 return buf;
566 static VALUE buffer_s_current()
568 return buffer_new(curbuf);
571 static VALUE buffer_s_count()
573 buf_T *b;
574 int n = 0;
576 for (b = firstbuf; b != NULL; b = b->b_next)
578 /* Deleted buffers should not be counted
579 * SegPhault - 01/07/05 */
580 if (b->b_p_bl)
581 n++;
584 return INT2NUM(n);
587 static VALUE buffer_s_aref(VALUE self UNUSED, VALUE num)
589 buf_T *b;
590 int n = NUM2INT(num);
592 for (b = firstbuf; b != NULL; b = b->b_next)
594 /* Deleted buffers should not be counted
595 * SegPhault - 01/07/05 */
596 if (!b->b_p_bl)
597 continue;
599 if (n == 0)
600 return buffer_new(b);
602 n--;
604 return Qnil;
607 static VALUE buffer_name(VALUE self)
609 buf_T *buf = get_buf(self);
611 return buf->b_ffname ? rb_str_new2((char *)buf->b_ffname) : Qnil;
614 static VALUE buffer_number(VALUE self)
616 buf_T *buf = get_buf(self);
618 return INT2NUM(buf->b_fnum);
621 static VALUE buffer_count(VALUE self)
623 buf_T *buf = get_buf(self);
625 return INT2NUM(buf->b_ml.ml_line_count);
628 static VALUE get_buffer_line(buf_T *buf, linenr_T n)
630 if (n > 0 && n <= buf->b_ml.ml_line_count)
632 char *line = (char *)ml_get_buf(buf, n, FALSE);
633 return line ? rb_str_new2(line) : Qnil;
635 rb_raise(rb_eIndexError, "index %d out of buffer", n);
636 #ifndef __GNUC__
637 return Qnil; /* For stop warning */
638 #endif
641 static VALUE buffer_aref(VALUE self, VALUE num)
643 buf_T *buf = get_buf(self);
645 if (buf != NULL)
646 return get_buffer_line(buf, (linenr_T)NUM2LONG(num));
647 return Qnil; /* For stop warning */
650 static VALUE set_buffer_line(buf_T *buf, linenr_T n, VALUE str)
652 char *line = STR2CSTR(str);
653 aco_save_T aco;
655 if (n > 0 && n <= buf->b_ml.ml_line_count && line != NULL)
657 /* set curwin/curbuf for "buf" and save some things */
658 aucmd_prepbuf(&aco, buf);
660 if (u_savesub(n) == OK) {
661 ml_replace(n, (char_u *)line, TRUE);
662 changed();
663 #ifdef SYNTAX_HL
664 syn_changed(n); /* recompute syntax hl. for this line */
665 #endif
668 /* restore curwin/curbuf and a few other things */
669 aucmd_restbuf(&aco);
670 /* Careful: autocommands may have made "buf" invalid! */
672 update_curbuf(NOT_VALID);
674 else
676 rb_raise(rb_eIndexError, "index %d out of buffer", n);
677 #ifndef __GNUC__
678 return Qnil; /* For stop warning */
679 #endif
681 return str;
684 static VALUE buffer_aset(VALUE self, VALUE num, VALUE str)
686 buf_T *buf = get_buf(self);
688 if (buf != NULL)
689 return set_buffer_line(buf, (linenr_T)NUM2LONG(num), str);
690 return str;
693 static VALUE buffer_delete(VALUE self, VALUE num)
695 buf_T *buf = get_buf(self);
696 long n = NUM2LONG(num);
697 aco_save_T aco;
699 if (n > 0 && n <= buf->b_ml.ml_line_count)
701 /* set curwin/curbuf for "buf" and save some things */
702 aucmd_prepbuf(&aco, buf);
704 if (u_savedel(n, 1) == OK) {
705 ml_delete(n, 0);
707 /* Changes to non-active buffers should properly refresh
708 * SegPhault - 01/09/05 */
709 deleted_lines_mark(n, 1L);
711 changed();
714 /* restore curwin/curbuf and a few other things */
715 aucmd_restbuf(&aco);
716 /* Careful: autocommands may have made "buf" invalid! */
718 update_curbuf(NOT_VALID);
720 else
722 rb_raise(rb_eIndexError, "index %d out of buffer", n);
724 return Qnil;
727 static VALUE buffer_append(VALUE self, VALUE num, VALUE str)
729 buf_T *buf = get_buf(self);
730 char *line = STR2CSTR(str);
731 long n = NUM2LONG(num);
732 aco_save_T aco;
734 if (n >= 0 && n <= buf->b_ml.ml_line_count && line != NULL)
736 /* set curwin/curbuf for "buf" and save some things */
737 aucmd_prepbuf(&aco, buf);
739 if (u_inssub(n + 1) == OK) {
740 ml_append(n, (char_u *) line, (colnr_T) 0, FALSE);
742 /* Changes to non-active buffers should properly refresh screen
743 * SegPhault - 12/20/04 */
744 appended_lines_mark(n, 1L);
746 changed();
749 /* restore curwin/curbuf and a few other things */
750 aucmd_restbuf(&aco);
751 /* Careful: autocommands may have made "buf" invalid! */
753 update_curbuf(NOT_VALID);
755 else {
756 rb_raise(rb_eIndexError, "index %d out of buffer", n);
758 return str;
761 static VALUE window_new(win_T *win)
763 if (win->w_ruby_ref)
765 return (VALUE) win->w_ruby_ref;
767 else
769 VALUE obj = Data_Wrap_Struct(cVimWindow, 0, 0, win);
770 win->w_ruby_ref = (void *) obj;
771 rb_hash_aset(objtbl, rb_obj_id(obj), obj);
772 return obj;
776 static win_T *get_win(VALUE obj)
778 win_T *win;
780 Data_Get_Struct(obj, win_T, win);
781 if (win == NULL)
782 rb_raise(eDeletedWindowError, "attempt to refer to deleted window");
783 return win;
786 static VALUE window_s_current()
788 return window_new(curwin);
792 * Added line manipulation functions
793 * SegPhault - 03/07/05
795 static VALUE line_s_current()
797 return get_buffer_line(curbuf, curwin->w_cursor.lnum);
800 static VALUE set_current_line(VALUE self UNUSED, VALUE str)
802 return set_buffer_line(curbuf, curwin->w_cursor.lnum, str);
805 static VALUE current_line_number()
807 return INT2FIX((int)curwin->w_cursor.lnum);
812 static VALUE window_s_count()
814 #ifdef FEAT_WINDOWS
815 win_T *w;
816 int n = 0;
818 for (w = firstwin; w != NULL; w = w->w_next)
819 n++;
820 return INT2NUM(n);
821 #else
822 return INT2NUM(1);
823 #endif
826 static VALUE window_s_aref(VALUE self UNUSED, VALUE num)
828 win_T *w;
829 int n = NUM2INT(num);
831 #ifndef FEAT_WINDOWS
832 w = curwin;
833 #else
834 for (w = firstwin; w != NULL; w = w->w_next, --n)
835 #endif
836 if (n == 0)
837 return window_new(w);
838 return Qnil;
841 static VALUE window_buffer(VALUE self)
843 win_T *win = get_win(self);
845 return buffer_new(win->w_buffer);
848 static VALUE window_height(VALUE self)
850 win_T *win = get_win(self);
852 return INT2NUM(win->w_height);
855 static VALUE window_set_height(VALUE self, VALUE height)
857 win_T *win = get_win(self);
858 win_T *savewin = curwin;
860 curwin = win;
861 win_setheight(NUM2INT(height));
862 curwin = savewin;
863 return height;
866 static VALUE window_width(VALUE self)
868 win_T *win = get_win(self);
870 return INT2NUM(win->w_width);
873 static VALUE window_set_width(VALUE self, VALUE width)
875 win_T *win = get_win(self);
876 win_T *savewin = curwin;
878 curwin = win;
879 win_setwidth(NUM2INT(width));
880 curwin = savewin;
881 return width;
884 static VALUE window_cursor(VALUE self)
886 win_T *win = get_win(self);
888 return rb_assoc_new(INT2NUM(win->w_cursor.lnum), INT2NUM(win->w_cursor.col));
891 static VALUE window_set_cursor(VALUE self, VALUE pos)
893 VALUE lnum, col;
894 win_T *win = get_win(self);
896 Check_Type(pos, T_ARRAY);
897 if (RARRAY(pos)->len != 2)
898 rb_raise(rb_eArgError, "array length must be 2");
899 lnum = RARRAY(pos)->ptr[0];
900 col = RARRAY(pos)->ptr[1];
901 win->w_cursor.lnum = NUM2LONG(lnum);
902 win->w_cursor.col = NUM2UINT(col);
903 check_cursor(); /* put cursor on an existing line */
904 update_screen(NOT_VALID);
905 return Qnil;
908 static VALUE f_p(int argc, VALUE *argv, VALUE self UNUSED)
910 int i;
911 VALUE str = rb_str_new("", 0);
913 for (i = 0; i < argc; i++) {
914 if (i > 0) rb_str_cat(str, ", ", 2);
915 rb_str_concat(str, rb_inspect(argv[i]));
917 MSG(RSTRING(str)->ptr);
918 return Qnil;
921 static void ruby_io_init(void)
923 #ifndef DYNAMIC_RUBY
924 RUBYEXTERN VALUE rb_stdout;
925 #endif
927 rb_stdout = rb_obj_alloc(rb_cObject);
928 rb_define_singleton_method(rb_stdout, "write", vim_message, 1);
929 rb_define_global_function("p", f_p, -1);
932 static void ruby_vim_init(void)
934 objtbl = rb_hash_new();
935 rb_global_variable(&objtbl);
937 /* The Vim module used to be called "VIM", but "Vim" is better. Make an
938 * alias "VIM" for backwards compatiblity. */
939 mVIM = rb_define_module("Vim");
940 rb_define_const(rb_cObject, "VIM", mVIM);
941 rb_define_const(mVIM, "VERSION_MAJOR", INT2NUM(VIM_VERSION_MAJOR));
942 rb_define_const(mVIM, "VERSION_MINOR", INT2NUM(VIM_VERSION_MINOR));
943 rb_define_const(mVIM, "VERSION_BUILD", INT2NUM(VIM_VERSION_BUILD));
944 rb_define_const(mVIM, "VERSION_PATCHLEVEL", INT2NUM(VIM_VERSION_PATCHLEVEL));
945 rb_define_const(mVIM, "VERSION_SHORT", rb_str_new2(VIM_VERSION_SHORT));
946 rb_define_const(mVIM, "VERSION_MEDIUM", rb_str_new2(VIM_VERSION_MEDIUM));
947 rb_define_const(mVIM, "VERSION_LONG", rb_str_new2(VIM_VERSION_LONG));
948 rb_define_const(mVIM, "VERSION_LONG_DATE", rb_str_new2(VIM_VERSION_LONG_DATE));
949 rb_define_module_function(mVIM, "message", vim_message, 1);
950 rb_define_module_function(mVIM, "set_option", vim_set_option, 1);
951 rb_define_module_function(mVIM, "command", vim_command, 1);
952 rb_define_module_function(mVIM, "evaluate", vim_evaluate, 1);
954 eDeletedBufferError = rb_define_class_under(mVIM, "DeletedBufferError",
955 rb_eStandardError);
956 eDeletedWindowError = rb_define_class_under(mVIM, "DeletedWindowError",
957 rb_eStandardError);
959 cBuffer = rb_define_class_under(mVIM, "Buffer", rb_cObject);
960 rb_define_singleton_method(cBuffer, "current", buffer_s_current, 0);
961 rb_define_singleton_method(cBuffer, "count", buffer_s_count, 0);
962 rb_define_singleton_method(cBuffer, "[]", buffer_s_aref, 1);
963 rb_define_method(cBuffer, "name", buffer_name, 0);
964 rb_define_method(cBuffer, "number", buffer_number, 0);
965 rb_define_method(cBuffer, "count", buffer_count, 0);
966 rb_define_method(cBuffer, "length", buffer_count, 0);
967 rb_define_method(cBuffer, "[]", buffer_aref, 1);
968 rb_define_method(cBuffer, "[]=", buffer_aset, 2);
969 rb_define_method(cBuffer, "delete", buffer_delete, 1);
970 rb_define_method(cBuffer, "append", buffer_append, 2);
972 /* Added line manipulation functions
973 * SegPhault - 03/07/05 */
974 rb_define_method(cBuffer, "line_number", current_line_number, 0);
975 rb_define_method(cBuffer, "line", line_s_current, 0);
976 rb_define_method(cBuffer, "line=", set_current_line, 1);
979 cVimWindow = rb_define_class_under(mVIM, "Window", rb_cObject);
980 rb_define_singleton_method(cVimWindow, "current", window_s_current, 0);
981 rb_define_singleton_method(cVimWindow, "count", window_s_count, 0);
982 rb_define_singleton_method(cVimWindow, "[]", window_s_aref, 1);
983 rb_define_method(cVimWindow, "buffer", window_buffer, 0);
984 rb_define_method(cVimWindow, "height", window_height, 0);
985 rb_define_method(cVimWindow, "height=", window_set_height, 1);
986 rb_define_method(cVimWindow, "width", window_width, 0);
987 rb_define_method(cVimWindow, "width=", window_set_width, 1);
988 rb_define_method(cVimWindow, "cursor", window_cursor, 0);
989 rb_define_method(cVimWindow, "cursor=", window_set_cursor, 1);
991 rb_define_virtual_variable("$curbuf", buffer_s_current, 0);
992 rb_define_virtual_variable("$curwin", window_s_current, 0);