Merge branch 'vim' into feat/indent-wrap-lines
[vim_extended.git] / src / if_ruby.c
blob0fed2c01829bc9bfa8aea4cd27f7d06df475a802
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 #include <ruby.h>
53 #undef EXTERN
54 #undef _
56 /* T_DATA defined both by Ruby and Mac header files, hack around it... */
57 #if defined(MACOS_X_UNIX) || defined(macintosh)
58 # define __OPENTRANSPORT__
59 # define __OPENTRANSPORTPROTOCOL__
60 # define __OPENTRANSPORTPROVIDERS__
61 #endif
63 #include "vim.h"
64 #include "version.h"
66 #if defined(PROTO) && !defined(FEAT_RUBY)
67 /* Define these to be able to generate the function prototypes. */
68 # define VALUE int
69 # define RUBY_DATA_FUNC int
70 #endif
72 static int ruby_initialized = 0;
73 static VALUE objtbl;
75 static VALUE mVIM;
76 static VALUE cBuffer;
77 static VALUE cVimWindow;
78 static VALUE eDeletedBufferError;
79 static VALUE eDeletedWindowError;
81 static int ensure_ruby_initialized(void);
82 static void error_print(int);
83 static void ruby_io_init(void);
84 static void ruby_vim_init(void);
86 #if defined(DYNAMIC_RUBY) || defined(PROTO)
87 #ifdef PROTO
88 # define HINSTANCE int /* for generating prototypes */
89 #endif
92 * Wrapper defines
94 #define rb_assoc_new dll_rb_assoc_new
95 #define rb_cObject (*dll_rb_cObject)
96 #define rb_check_type dll_rb_check_type
97 #define rb_class_path dll_rb_class_path
98 #define rb_data_object_alloc dll_rb_data_object_alloc
99 #define rb_define_class_under dll_rb_define_class_under
100 #define rb_define_const dll_rb_define_const
101 #define rb_define_global_function dll_rb_define_global_function
102 #define rb_define_method dll_rb_define_method
103 #define rb_define_module dll_rb_define_module
104 #define rb_define_module_function dll_rb_define_module_function
105 #define rb_define_singleton_method dll_rb_define_singleton_method
106 #define rb_define_virtual_variable dll_rb_define_virtual_variable
107 #define rb_stdout (*dll_rb_stdout)
108 #define rb_eArgError (*dll_rb_eArgError)
109 #define rb_eIndexError (*dll_rb_eIndexError)
110 #define rb_eRuntimeError (*dll_rb_eRuntimeError)
111 #define rb_eStandardError (*dll_rb_eStandardError)
112 #define rb_eval_string_protect dll_rb_eval_string_protect
113 #define rb_global_variable dll_rb_global_variable
114 #define rb_hash_aset dll_rb_hash_aset
115 #define rb_hash_new dll_rb_hash_new
116 #define rb_inspect dll_rb_inspect
117 #define rb_int2inum dll_rb_int2inum
118 #define rb_lastline_get dll_rb_lastline_get
119 #define rb_lastline_set dll_rb_lastline_set
120 #define rb_load_protect dll_rb_load_protect
121 #define rb_num2long dll_rb_num2long
122 #define rb_num2ulong dll_rb_num2ulong
123 #define rb_obj_alloc dll_rb_obj_alloc
124 #define rb_obj_as_string dll_rb_obj_as_string
125 #define rb_obj_id dll_rb_obj_id
126 #define rb_raise dll_rb_raise
127 #define rb_str2cstr dll_rb_str2cstr
128 #define rb_str_cat dll_rb_str_cat
129 #define rb_str_concat dll_rb_str_concat
130 #define rb_str_new dll_rb_str_new
131 #define rb_str_new2 dll_rb_str_new2
132 #define ruby_errinfo (*dll_ruby_errinfo)
133 #define ruby_init dll_ruby_init
134 #define ruby_init_loadpath dll_ruby_init_loadpath
135 #if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 18
136 # define rb_w32_snprintf dll_rb_w32_snprintf
137 #endif
140 * Pointers for dynamic link
142 static VALUE (*dll_rb_assoc_new) (VALUE, VALUE);
143 static VALUE *dll_rb_cFalseClass;
144 static VALUE *dll_rb_cFixnum;
145 static VALUE *dll_rb_cNilClass;
146 static VALUE *dll_rb_cObject;
147 static VALUE *dll_rb_cSymbol;
148 static VALUE *dll_rb_cTrueClass;
149 static void (*dll_rb_check_type) (VALUE,int);
150 static VALUE (*dll_rb_class_path) (VALUE);
151 static VALUE (*dll_rb_data_object_alloc) (VALUE, void*, RUBY_DATA_FUNC, RUBY_DATA_FUNC);
152 static VALUE (*dll_rb_define_class_under) (VALUE, const char*, VALUE);
153 static void (*dll_rb_define_const) (VALUE,const char*,VALUE);
154 static void (*dll_rb_define_global_function) (const char*,VALUE(*)(),int);
155 static void (*dll_rb_define_method) (VALUE,const char*,VALUE(*)(),int);
156 static VALUE (*dll_rb_define_module) (const char*);
157 static void (*dll_rb_define_module_function) (VALUE,const char*,VALUE(*)(),int);
158 static void (*dll_rb_define_singleton_method) (VALUE,const char*,VALUE(*)(),int);
159 static void (*dll_rb_define_virtual_variable) (const char*,VALUE(*)(),void(*)());
160 static VALUE *dll_rb_stdout;
161 static VALUE *dll_rb_eArgError;
162 static VALUE *dll_rb_eIndexError;
163 static VALUE *dll_rb_eRuntimeError;
164 static VALUE *dll_rb_eStandardError;
165 static VALUE (*dll_rb_eval_string_protect) (const char*, int*);
166 static void (*dll_rb_global_variable) (VALUE*);
167 static VALUE (*dll_rb_hash_aset) (VALUE, VALUE, VALUE);
168 static VALUE (*dll_rb_hash_new) (void);
169 static VALUE (*dll_rb_inspect) (VALUE);
170 static VALUE (*dll_rb_int2inum) (long);
171 static VALUE (*dll_rb_int2inum) (long);
172 static VALUE (*dll_rb_lastline_get) (void);
173 static void (*dll_rb_lastline_set) (VALUE);
174 static void (*dll_rb_load_protect) (VALUE, int, int*);
175 static long (*dll_rb_num2long) (VALUE);
176 static unsigned long (*dll_rb_num2ulong) (VALUE);
177 static VALUE (*dll_rb_obj_alloc) (VALUE);
178 static VALUE (*dll_rb_obj_as_string) (VALUE);
179 static VALUE (*dll_rb_obj_id) (VALUE);
180 static void (*dll_rb_raise) (VALUE, const char*, ...);
181 static char *(*dll_rb_str2cstr) (VALUE,int*);
182 static VALUE (*dll_rb_str_cat) (VALUE, const char*, long);
183 static VALUE (*dll_rb_str_concat) (VALUE, VALUE);
184 static VALUE (*dll_rb_str_new) (const char*, long);
185 static VALUE (*dll_rb_str_new2) (const char*);
186 static VALUE *dll_ruby_errinfo;
187 static void (*dll_ruby_init) (void);
188 static void (*dll_ruby_init_loadpath) (void);
189 #if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 18
190 static int (*dll_rb_w32_snprintf)(char*, size_t, const char*, ...);
191 #endif
193 static HINSTANCE hinstRuby = 0; /* Instance of ruby.dll */
196 * Table of name to function pointer of ruby.
198 #define RUBY_PROC FARPROC
199 static struct
201 char *name;
202 RUBY_PROC *ptr;
203 } ruby_funcname_table[] =
205 {"rb_assoc_new", (RUBY_PROC*)&dll_rb_assoc_new},
206 {"rb_cFalseClass", (RUBY_PROC*)&dll_rb_cFalseClass},
207 {"rb_cFixnum", (RUBY_PROC*)&dll_rb_cFixnum},
208 {"rb_cNilClass", (RUBY_PROC*)&dll_rb_cNilClass},
209 {"rb_cObject", (RUBY_PROC*)&dll_rb_cObject},
210 {"rb_cSymbol", (RUBY_PROC*)&dll_rb_cSymbol},
211 {"rb_cTrueClass", (RUBY_PROC*)&dll_rb_cTrueClass},
212 {"rb_check_type", (RUBY_PROC*)&dll_rb_check_type},
213 {"rb_class_path", (RUBY_PROC*)&dll_rb_class_path},
214 {"rb_data_object_alloc", (RUBY_PROC*)&dll_rb_data_object_alloc},
215 {"rb_define_class_under", (RUBY_PROC*)&dll_rb_define_class_under},
216 {"rb_define_const", (RUBY_PROC*)&dll_rb_define_const},
217 {"rb_define_global_function", (RUBY_PROC*)&dll_rb_define_global_function},
218 {"rb_define_method", (RUBY_PROC*)&dll_rb_define_method},
219 {"rb_define_module", (RUBY_PROC*)&dll_rb_define_module},
220 {"rb_define_module_function", (RUBY_PROC*)&dll_rb_define_module_function},
221 {"rb_define_singleton_method", (RUBY_PROC*)&dll_rb_define_singleton_method},
222 {"rb_define_virtual_variable", (RUBY_PROC*)&dll_rb_define_virtual_variable},
223 {"rb_stdout", (RUBY_PROC*)&dll_rb_stdout},
224 {"rb_eArgError", (RUBY_PROC*)&dll_rb_eArgError},
225 {"rb_eIndexError", (RUBY_PROC*)&dll_rb_eIndexError},
226 {"rb_eRuntimeError", (RUBY_PROC*)&dll_rb_eRuntimeError},
227 {"rb_eStandardError", (RUBY_PROC*)&dll_rb_eStandardError},
228 {"rb_eval_string_protect", (RUBY_PROC*)&dll_rb_eval_string_protect},
229 {"rb_global_variable", (RUBY_PROC*)&dll_rb_global_variable},
230 {"rb_hash_aset", (RUBY_PROC*)&dll_rb_hash_aset},
231 {"rb_hash_new", (RUBY_PROC*)&dll_rb_hash_new},
232 {"rb_inspect", (RUBY_PROC*)&dll_rb_inspect},
233 {"rb_int2inum", (RUBY_PROC*)&dll_rb_int2inum},
234 {"rb_lastline_get", (RUBY_PROC*)&dll_rb_lastline_get},
235 {"rb_lastline_set", (RUBY_PROC*)&dll_rb_lastline_set},
236 {"rb_load_protect", (RUBY_PROC*)&dll_rb_load_protect},
237 {"rb_num2long", (RUBY_PROC*)&dll_rb_num2long},
238 {"rb_num2ulong", (RUBY_PROC*)&dll_rb_num2ulong},
239 {"rb_obj_alloc", (RUBY_PROC*)&dll_rb_obj_alloc},
240 {"rb_obj_as_string", (RUBY_PROC*)&dll_rb_obj_as_string},
241 {"rb_obj_id", (RUBY_PROC*)&dll_rb_obj_id},
242 {"rb_raise", (RUBY_PROC*)&dll_rb_raise},
243 {"rb_str2cstr", (RUBY_PROC*)&dll_rb_str2cstr},
244 {"rb_str_cat", (RUBY_PROC*)&dll_rb_str_cat},
245 {"rb_str_concat", (RUBY_PROC*)&dll_rb_str_concat},
246 {"rb_str_new", (RUBY_PROC*)&dll_rb_str_new},
247 {"rb_str_new2", (RUBY_PROC*)&dll_rb_str_new2},
248 {"ruby_errinfo", (RUBY_PROC*)&dll_ruby_errinfo},
249 {"ruby_init", (RUBY_PROC*)&dll_ruby_init},
250 {"ruby_init_loadpath", (RUBY_PROC*)&dll_ruby_init_loadpath},
251 #if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 18
252 {"rb_w32_snprintf", (RUBY_PROC*)&dll_rb_w32_snprintf},
253 #endif
254 {"", NULL},
258 * Free ruby.dll
260 static void
261 end_dynamic_ruby()
263 if (hinstRuby)
265 FreeLibrary(hinstRuby);
266 hinstRuby = 0;
271 * Load library and get all pointers.
272 * Parameter 'libname' provides name of DLL.
273 * Return OK or FAIL.
275 static int
276 ruby_runtime_link_init(char *libname, int verbose)
278 int i;
280 if (hinstRuby)
281 return OK;
282 hinstRuby = LoadLibrary(libname);
283 if (!hinstRuby)
285 if (verbose)
286 EMSG2(_(e_loadlib), libname);
287 return FAIL;
290 for (i = 0; ruby_funcname_table[i].ptr; ++i)
292 if (!(*ruby_funcname_table[i].ptr = GetProcAddress(hinstRuby,
293 ruby_funcname_table[i].name)))
295 FreeLibrary(hinstRuby);
296 hinstRuby = 0;
297 if (verbose)
298 EMSG2(_(e_loadfunc), ruby_funcname_table[i].name);
299 return FAIL;
302 return OK;
306 * If ruby is enabled (there is installed ruby on Windows system) return TRUE,
307 * else FALSE.
310 ruby_enabled(verbose)
311 int verbose;
313 return ruby_runtime_link_init(DYNAMIC_RUBY_DLL, verbose) == OK;
315 #endif /* defined(DYNAMIC_RUBY) || defined(PROTO) */
317 void
318 ruby_end()
320 #ifdef DYNAMIC_RUBY
321 end_dynamic_ruby();
322 #endif
325 void ex_ruby(exarg_T *eap)
327 int state;
328 char *script = NULL;
330 script = (char *)script_get(eap, eap->arg);
331 if (!eap->skip && ensure_ruby_initialized())
333 if (script == NULL)
334 rb_eval_string_protect((char *)eap->arg, &state);
335 else
336 rb_eval_string_protect(script, &state);
337 if (state)
338 error_print(state);
340 vim_free(script);
343 void ex_rubydo(exarg_T *eap)
345 int state;
346 linenr_T i;
348 if (ensure_ruby_initialized())
350 if (u_save(eap->line1 - 1, eap->line2 + 1) != OK)
351 return;
352 for (i = eap->line1; i <= eap->line2; i++) {
353 VALUE line, oldline;
355 line = oldline = rb_str_new2((char *)ml_get(i));
356 rb_lastline_set(line);
357 rb_eval_string_protect((char *) eap->arg, &state);
358 if (state) {
359 error_print(state);
360 break;
362 line = rb_lastline_get();
363 if (!NIL_P(line)) {
364 if (TYPE(line) != T_STRING) {
365 EMSG(_("E265: $_ must be an instance of String"));
366 return;
368 ml_replace(i, (char_u *) STR2CSTR(line), 1);
369 changed();
370 #ifdef SYNTAX_HL
371 syn_changed(i); /* recompute syntax hl. for this line */
372 #endif
375 check_cursor();
376 update_curbuf(NOT_VALID);
380 void ex_rubyfile(exarg_T *eap)
382 int state;
384 if (ensure_ruby_initialized())
386 rb_load_protect(rb_str_new2((char *) eap->arg), 0, &state);
387 if (state) error_print(state);
391 void ruby_buffer_free(buf_T *buf)
393 if (buf->b_ruby_ref)
395 rb_hash_aset(objtbl, rb_obj_id((VALUE) buf->b_ruby_ref), Qnil);
396 RDATA(buf->b_ruby_ref)->data = NULL;
400 void ruby_window_free(win_T *win)
402 if (win->w_ruby_ref)
404 rb_hash_aset(objtbl, rb_obj_id((VALUE) win->w_ruby_ref), Qnil);
405 RDATA(win->w_ruby_ref)->data = NULL;
409 static int ensure_ruby_initialized(void)
411 if (!ruby_initialized)
413 #ifdef DYNAMIC_RUBY
414 if (ruby_enabled(TRUE))
416 #endif
417 ruby_init();
418 ruby_init_loadpath();
419 ruby_io_init();
420 ruby_vim_init();
421 ruby_initialized = 1;
422 #ifdef DYNAMIC_RUBY
424 else
426 EMSG(_("E266: Sorry, this command is disabled, the Ruby library could not be loaded."));
427 return 0;
429 #endif
431 return ruby_initialized;
434 static void error_print(int state)
436 #ifndef DYNAMIC_RUBY
437 RUBYEXTERN VALUE ruby_errinfo;
438 #endif
439 VALUE eclass;
440 VALUE einfo;
441 char buff[BUFSIZ];
443 #define TAG_RETURN 0x1
444 #define TAG_BREAK 0x2
445 #define TAG_NEXT 0x3
446 #define TAG_RETRY 0x4
447 #define TAG_REDO 0x5
448 #define TAG_RAISE 0x6
449 #define TAG_THROW 0x7
450 #define TAG_FATAL 0x8
451 #define TAG_MASK 0xf
453 switch (state) {
454 case TAG_RETURN:
455 EMSG(_("E267: unexpected return"));
456 break;
457 case TAG_NEXT:
458 EMSG(_("E268: unexpected next"));
459 break;
460 case TAG_BREAK:
461 EMSG(_("E269: unexpected break"));
462 break;
463 case TAG_REDO:
464 EMSG(_("E270: unexpected redo"));
465 break;
466 case TAG_RETRY:
467 EMSG(_("E271: retry outside of rescue clause"));
468 break;
469 case TAG_RAISE:
470 case TAG_FATAL:
471 eclass = CLASS_OF(ruby_errinfo);
472 einfo = rb_obj_as_string(ruby_errinfo);
473 if (eclass == rb_eRuntimeError && RSTRING(einfo)->len == 0) {
474 EMSG(_("E272: unhandled exception"));
476 else {
477 VALUE epath;
478 char *p;
480 epath = rb_class_path(eclass);
481 vim_snprintf(buff, BUFSIZ, "%s: %s",
482 RSTRING(epath)->ptr, RSTRING(einfo)->ptr);
483 p = strchr(buff, '\n');
484 if (p) *p = '\0';
485 EMSG(buff);
487 break;
488 default:
489 vim_snprintf(buff, BUFSIZ, _("E273: unknown longjmp status %d"), state);
490 EMSG(buff);
491 break;
495 static VALUE vim_message(VALUE self UNUSED, VALUE str)
497 char *buff, *p;
499 str = rb_obj_as_string(str);
500 buff = ALLOCA_N(char, RSTRING(str)->len);
501 strcpy(buff, RSTRING(str)->ptr);
502 p = strchr(buff, '\n');
503 if (p) *p = '\0';
504 MSG(buff);
505 return Qnil;
508 static VALUE vim_set_option(VALUE self UNUSED, VALUE str)
510 do_set((char_u *)STR2CSTR(str), 0);
511 update_screen(NOT_VALID);
512 return Qnil;
515 static VALUE vim_command(VALUE self UNUSED, VALUE str)
517 do_cmdline_cmd((char_u *)STR2CSTR(str));
518 return Qnil;
521 static VALUE vim_evaluate(VALUE self UNUSED, VALUE str)
523 #ifdef FEAT_EVAL
524 char_u *value = eval_to_string((char_u *)STR2CSTR(str), NULL, TRUE);
526 if (value != NULL)
528 VALUE val = rb_str_new2((char *)value);
529 vim_free(value);
530 return val;
532 else
533 #endif
534 return Qnil;
537 static VALUE buffer_new(buf_T *buf)
539 if (buf->b_ruby_ref)
541 return (VALUE) buf->b_ruby_ref;
543 else
545 VALUE obj = Data_Wrap_Struct(cBuffer, 0, 0, buf);
546 buf->b_ruby_ref = (void *) obj;
547 rb_hash_aset(objtbl, rb_obj_id(obj), obj);
548 return obj;
552 static buf_T *get_buf(VALUE obj)
554 buf_T *buf;
556 Data_Get_Struct(obj, buf_T, buf);
557 if (buf == NULL)
558 rb_raise(eDeletedBufferError, "attempt to refer to deleted buffer");
559 return buf;
562 static VALUE buffer_s_current()
564 return buffer_new(curbuf);
567 static VALUE buffer_s_count()
569 buf_T *b;
570 int n = 0;
572 for (b = firstbuf; b != NULL; b = b->b_next)
574 /* Deleted buffers should not be counted
575 * SegPhault - 01/07/05 */
576 if (b->b_p_bl)
577 n++;
580 return INT2NUM(n);
583 static VALUE buffer_s_aref(VALUE self UNUSED, VALUE num)
585 buf_T *b;
586 int n = NUM2INT(num);
588 for (b = firstbuf; b != NULL; b = b->b_next)
590 /* Deleted buffers should not be counted
591 * SegPhault - 01/07/05 */
592 if (!b->b_p_bl)
593 continue;
595 if (n == 0)
596 return buffer_new(b);
598 n--;
600 return Qnil;
603 static VALUE buffer_name(VALUE self)
605 buf_T *buf = get_buf(self);
607 return buf->b_ffname ? rb_str_new2((char *)buf->b_ffname) : Qnil;
610 static VALUE buffer_number(VALUE self)
612 buf_T *buf = get_buf(self);
614 return INT2NUM(buf->b_fnum);
617 static VALUE buffer_count(VALUE self)
619 buf_T *buf = get_buf(self);
621 return INT2NUM(buf->b_ml.ml_line_count);
624 static VALUE get_buffer_line(buf_T *buf, linenr_T n)
626 if (n > 0 && n <= buf->b_ml.ml_line_count)
628 char *line = (char *)ml_get_buf(buf, n, FALSE);
629 return line ? rb_str_new2(line) : Qnil;
631 rb_raise(rb_eIndexError, "index %d out of buffer", n);
632 #ifndef __GNUC__
633 return Qnil; /* For stop warning */
634 #endif
637 static VALUE buffer_aref(VALUE self, VALUE num)
639 buf_T *buf = get_buf(self);
641 if (buf != NULL)
642 return get_buffer_line(buf, (linenr_T)NUM2LONG(num));
643 return Qnil; /* For stop warning */
646 static VALUE set_buffer_line(buf_T *buf, linenr_T n, VALUE str)
648 char *line = STR2CSTR(str);
649 aco_save_T aco;
651 if (n > 0 && n <= buf->b_ml.ml_line_count && line != NULL)
653 /* set curwin/curbuf for "buf" and save some things */
654 aucmd_prepbuf(&aco, buf);
656 if (u_savesub(n) == OK) {
657 ml_replace(n, (char_u *)line, TRUE);
658 changed();
659 #ifdef SYNTAX_HL
660 syn_changed(n); /* recompute syntax hl. for this line */
661 #endif
664 /* restore curwin/curbuf and a few other things */
665 aucmd_restbuf(&aco);
666 /* Careful: autocommands may have made "buf" invalid! */
668 update_curbuf(NOT_VALID);
670 else
672 rb_raise(rb_eIndexError, "index %d out of buffer", n);
673 #ifndef __GNUC__
674 return Qnil; /* For stop warning */
675 #endif
677 return str;
680 static VALUE buffer_aset(VALUE self, VALUE num, VALUE str)
682 buf_T *buf = get_buf(self);
684 if (buf != NULL)
685 return set_buffer_line(buf, (linenr_T)NUM2LONG(num), str);
686 return str;
689 static VALUE buffer_delete(VALUE self, VALUE num)
691 buf_T *buf = get_buf(self);
692 long n = NUM2LONG(num);
693 aco_save_T aco;
695 if (n > 0 && n <= buf->b_ml.ml_line_count)
697 /* set curwin/curbuf for "buf" and save some things */
698 aucmd_prepbuf(&aco, buf);
700 if (u_savedel(n, 1) == OK) {
701 ml_delete(n, 0);
703 /* Changes to non-active buffers should properly refresh
704 * SegPhault - 01/09/05 */
705 deleted_lines_mark(n, 1L);
707 changed();
710 /* restore curwin/curbuf and a few other things */
711 aucmd_restbuf(&aco);
712 /* Careful: autocommands may have made "buf" invalid! */
714 update_curbuf(NOT_VALID);
716 else
718 rb_raise(rb_eIndexError, "index %d out of buffer", n);
720 return Qnil;
723 static VALUE buffer_append(VALUE self, VALUE num, VALUE str)
725 buf_T *buf = get_buf(self);
726 char *line = STR2CSTR(str);
727 long n = NUM2LONG(num);
728 aco_save_T aco;
730 if (n >= 0 && n <= buf->b_ml.ml_line_count && line != NULL)
732 /* set curwin/curbuf for "buf" and save some things */
733 aucmd_prepbuf(&aco, buf);
735 if (u_inssub(n + 1) == OK) {
736 ml_append(n, (char_u *) line, (colnr_T) 0, FALSE);
738 /* Changes to non-active buffers should properly refresh screen
739 * SegPhault - 12/20/04 */
740 appended_lines_mark(n, 1L);
742 changed();
745 /* restore curwin/curbuf and a few other things */
746 aucmd_restbuf(&aco);
747 /* Careful: autocommands may have made "buf" invalid! */
749 update_curbuf(NOT_VALID);
751 else {
752 rb_raise(rb_eIndexError, "index %d out of buffer", n);
754 return str;
757 static VALUE window_new(win_T *win)
759 if (win->w_ruby_ref)
761 return (VALUE) win->w_ruby_ref;
763 else
765 VALUE obj = Data_Wrap_Struct(cVimWindow, 0, 0, win);
766 win->w_ruby_ref = (void *) obj;
767 rb_hash_aset(objtbl, rb_obj_id(obj), obj);
768 return obj;
772 static win_T *get_win(VALUE obj)
774 win_T *win;
776 Data_Get_Struct(obj, win_T, win);
777 if (win == NULL)
778 rb_raise(eDeletedWindowError, "attempt to refer to deleted window");
779 return win;
782 static VALUE window_s_current()
784 return window_new(curwin);
788 * Added line manipulation functions
789 * SegPhault - 03/07/05
791 static VALUE line_s_current()
793 return get_buffer_line(curbuf, curwin->w_cursor.lnum);
796 static VALUE set_current_line(VALUE self UNUSED, VALUE str)
798 return set_buffer_line(curbuf, curwin->w_cursor.lnum, str);
801 static VALUE current_line_number()
803 return INT2FIX((int)curwin->w_cursor.lnum);
808 static VALUE window_s_count()
810 #ifdef FEAT_WINDOWS
811 win_T *w;
812 int n = 0;
814 for (w = firstwin; w != NULL; w = w->w_next)
815 n++;
816 return INT2NUM(n);
817 #else
818 return INT2NUM(1);
819 #endif
822 static VALUE window_s_aref(VALUE self UNUSED, VALUE num)
824 win_T *w;
825 int n = NUM2INT(num);
827 #ifndef FEAT_WINDOWS
828 w = curwin;
829 #else
830 for (w = firstwin; w != NULL; w = w->w_next, --n)
831 #endif
832 if (n == 0)
833 return window_new(w);
834 return Qnil;
837 static VALUE window_buffer(VALUE self)
839 win_T *win = get_win(self);
841 return buffer_new(win->w_buffer);
844 static VALUE window_height(VALUE self)
846 win_T *win = get_win(self);
848 return INT2NUM(win->w_height);
851 static VALUE window_set_height(VALUE self, VALUE height)
853 win_T *win = get_win(self);
854 win_T *savewin = curwin;
856 curwin = win;
857 win_setheight(NUM2INT(height));
858 curwin = savewin;
859 return height;
862 static VALUE window_width(VALUE self)
864 win_T *win = get_win(self);
866 return INT2NUM(win->w_width);
869 static VALUE window_set_width(VALUE self, VALUE width)
871 win_T *win = get_win(self);
872 win_T *savewin = curwin;
874 curwin = win;
875 win_setwidth(NUM2INT(width));
876 curwin = savewin;
877 return width;
880 static VALUE window_cursor(VALUE self)
882 win_T *win = get_win(self);
884 return rb_assoc_new(INT2NUM(win->w_cursor.lnum), INT2NUM(win->w_cursor.col));
887 static VALUE window_set_cursor(VALUE self, VALUE pos)
889 VALUE lnum, col;
890 win_T *win = get_win(self);
892 Check_Type(pos, T_ARRAY);
893 if (RARRAY(pos)->len != 2)
894 rb_raise(rb_eArgError, "array length must be 2");
895 lnum = RARRAY(pos)->ptr[0];
896 col = RARRAY(pos)->ptr[1];
897 win->w_cursor.lnum = NUM2LONG(lnum);
898 win->w_cursor.col = NUM2UINT(col);
899 check_cursor(); /* put cursor on an existing line */
900 update_screen(NOT_VALID);
901 return Qnil;
904 static VALUE f_p(int argc, VALUE *argv, VALUE self UNUSED)
906 int i;
907 VALUE str = rb_str_new("", 0);
909 for (i = 0; i < argc; i++) {
910 if (i > 0) rb_str_cat(str, ", ", 2);
911 rb_str_concat(str, rb_inspect(argv[i]));
913 MSG(RSTRING(str)->ptr);
914 return Qnil;
917 static void ruby_io_init(void)
919 #ifndef DYNAMIC_RUBY
920 RUBYEXTERN VALUE rb_stdout;
921 #endif
923 rb_stdout = rb_obj_alloc(rb_cObject);
924 rb_define_singleton_method(rb_stdout, "write", vim_message, 1);
925 rb_define_global_function("p", f_p, -1);
928 static void ruby_vim_init(void)
930 objtbl = rb_hash_new();
931 rb_global_variable(&objtbl);
933 /* The Vim module used to be called "VIM", but "Vim" is better. Make an
934 * alias "VIM" for backwards compatiblity. */
935 mVIM = rb_define_module("Vim");
936 rb_define_const(rb_cObject, "VIM", mVIM);
937 rb_define_const(mVIM, "VERSION_MAJOR", INT2NUM(VIM_VERSION_MAJOR));
938 rb_define_const(mVIM, "VERSION_MINOR", INT2NUM(VIM_VERSION_MINOR));
939 rb_define_const(mVIM, "VERSION_BUILD", INT2NUM(VIM_VERSION_BUILD));
940 rb_define_const(mVIM, "VERSION_PATCHLEVEL", INT2NUM(VIM_VERSION_PATCHLEVEL));
941 rb_define_const(mVIM, "VERSION_SHORT", rb_str_new2(VIM_VERSION_SHORT));
942 rb_define_const(mVIM, "VERSION_MEDIUM", rb_str_new2(VIM_VERSION_MEDIUM));
943 rb_define_const(mVIM, "VERSION_LONG", rb_str_new2(VIM_VERSION_LONG));
944 rb_define_const(mVIM, "VERSION_LONG_DATE", rb_str_new2(VIM_VERSION_LONG_DATE));
945 rb_define_module_function(mVIM, "message", vim_message, 1);
946 rb_define_module_function(mVIM, "set_option", vim_set_option, 1);
947 rb_define_module_function(mVIM, "command", vim_command, 1);
948 rb_define_module_function(mVIM, "evaluate", vim_evaluate, 1);
950 eDeletedBufferError = rb_define_class_under(mVIM, "DeletedBufferError",
951 rb_eStandardError);
952 eDeletedWindowError = rb_define_class_under(mVIM, "DeletedWindowError",
953 rb_eStandardError);
955 cBuffer = rb_define_class_under(mVIM, "Buffer", rb_cObject);
956 rb_define_singleton_method(cBuffer, "current", buffer_s_current, 0);
957 rb_define_singleton_method(cBuffer, "count", buffer_s_count, 0);
958 rb_define_singleton_method(cBuffer, "[]", buffer_s_aref, 1);
959 rb_define_method(cBuffer, "name", buffer_name, 0);
960 rb_define_method(cBuffer, "number", buffer_number, 0);
961 rb_define_method(cBuffer, "count", buffer_count, 0);
962 rb_define_method(cBuffer, "length", buffer_count, 0);
963 rb_define_method(cBuffer, "[]", buffer_aref, 1);
964 rb_define_method(cBuffer, "[]=", buffer_aset, 2);
965 rb_define_method(cBuffer, "delete", buffer_delete, 1);
966 rb_define_method(cBuffer, "append", buffer_append, 2);
968 /* Added line manipulation functions
969 * SegPhault - 03/07/05 */
970 rb_define_method(cBuffer, "line_number", current_line_number, 0);
971 rb_define_method(cBuffer, "line", line_s_current, 0);
972 rb_define_method(cBuffer, "line=", set_current_line, 1);
975 cVimWindow = rb_define_class_under(mVIM, "Window", rb_cObject);
976 rb_define_singleton_method(cVimWindow, "current", window_s_current, 0);
977 rb_define_singleton_method(cVimWindow, "count", window_s_count, 0);
978 rb_define_singleton_method(cVimWindow, "[]", window_s_aref, 1);
979 rb_define_method(cVimWindow, "buffer", window_buffer, 0);
980 rb_define_method(cVimWindow, "height", window_height, 0);
981 rb_define_method(cVimWindow, "height=", window_set_height, 1);
982 rb_define_method(cVimWindow, "width", window_width, 0);
983 rb_define_method(cVimWindow, "width=", window_set_width, 1);
984 rb_define_method(cVimWindow, "cursor", window_cursor, 0);
985 rb_define_method(cVimWindow, "cursor=", window_set_cursor, 1);
987 rb_define_virtual_variable("$curbuf", buffer_s_current, 0);
988 rb_define_virtual_variable("$curwin", window_s_current, 0);