DYNAMIC_RUBY
[MacVim/KaoriYa.git] / src / if_ruby.c
blobf4c8f96b9eefe118799381fc64ebc607d3d5c569
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
91 # if defined(MACOS_X_UNIX)
92 typedef void * HINSTANCE;
93 typedef void * FARPROC;
94 # include <dlfcn.h>
95 # define LoadLibrary(a) dlopen(a,RTLD_NOW|RTLD_LOCAL)
96 # define FreeLibrary(a) dlclose(a)
97 # define GetProcAddress dlsym
98 # define DYNAMIC_RUBY_DLL "/System/Library/Frameworks/Ruby.framework/Ruby"
99 # endif
102 * Wrapper defines
104 #define rb_assoc_new dll_rb_assoc_new
105 #define rb_cObject (*dll_rb_cObject)
106 #define rb_check_type dll_rb_check_type
107 #define rb_class_path dll_rb_class_path
108 #define rb_data_object_alloc dll_rb_data_object_alloc
109 #define rb_define_class_under dll_rb_define_class_under
110 #define rb_define_const dll_rb_define_const
111 #define rb_define_global_function dll_rb_define_global_function
112 #define rb_define_method dll_rb_define_method
113 #define rb_define_module dll_rb_define_module
114 #define rb_define_module_function dll_rb_define_module_function
115 #define rb_define_singleton_method dll_rb_define_singleton_method
116 #define rb_define_virtual_variable dll_rb_define_virtual_variable
117 #define rb_stdout (*dll_rb_stdout)
118 #define rb_eArgError (*dll_rb_eArgError)
119 #define rb_eIndexError (*dll_rb_eIndexError)
120 #define rb_eRuntimeError (*dll_rb_eRuntimeError)
121 #define rb_eStandardError (*dll_rb_eStandardError)
122 #define rb_eval_string_protect dll_rb_eval_string_protect
123 #define rb_global_variable dll_rb_global_variable
124 #define rb_hash_aset dll_rb_hash_aset
125 #define rb_hash_new dll_rb_hash_new
126 #define rb_inspect dll_rb_inspect
127 #define rb_int2inum dll_rb_int2inum
128 #define rb_lastline_get dll_rb_lastline_get
129 #define rb_lastline_set dll_rb_lastline_set
130 #define rb_load_protect dll_rb_load_protect
131 #define rb_num2long dll_rb_num2long
132 #define rb_num2ulong dll_rb_num2ulong
133 #define rb_obj_alloc dll_rb_obj_alloc
134 #define rb_obj_as_string dll_rb_obj_as_string
135 #define rb_obj_id dll_rb_obj_id
136 #define rb_raise dll_rb_raise
137 #define rb_str2cstr dll_rb_str2cstr
138 #define rb_str_cat dll_rb_str_cat
139 #define rb_str_concat dll_rb_str_concat
140 #define rb_str_new dll_rb_str_new
141 #define rb_str_new2 dll_rb_str_new2
142 #define ruby_errinfo (*dll_ruby_errinfo)
143 #define ruby_init dll_ruby_init
144 #define ruby_init_loadpath dll_ruby_init_loadpath
145 #if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 18
146 # define rb_w32_snprintf dll_rb_w32_snprintf
147 #endif
150 * Pointers for dynamic link
152 static VALUE (*dll_rb_assoc_new) (VALUE, VALUE);
153 VALUE (*dll_rb_cFalseClass);
154 VALUE (*dll_rb_cFixnum);
155 VALUE (*dll_rb_cNilClass);
156 static VALUE (*dll_rb_cObject);
157 VALUE (*dll_rb_cSymbol);
158 VALUE (*dll_rb_cTrueClass);
159 static void (*dll_rb_check_type) (VALUE,int);
160 static VALUE (*dll_rb_class_path) (VALUE);
161 static VALUE (*dll_rb_data_object_alloc) (VALUE, void*, RUBY_DATA_FUNC, RUBY_DATA_FUNC);
162 static VALUE (*dll_rb_define_class_under) (VALUE, const char*, VALUE);
163 static void (*dll_rb_define_const) (VALUE,const char*,VALUE);
164 static void (*dll_rb_define_global_function) (const char*,VALUE(*)(),int);
165 static void (*dll_rb_define_method) (VALUE,const char*,VALUE(*)(),int);
166 static VALUE (*dll_rb_define_module) (const char*);
167 static void (*dll_rb_define_module_function) (VALUE,const char*,VALUE(*)(),int);
168 static void (*dll_rb_define_singleton_method) (VALUE,const char*,VALUE(*)(),int);
169 static void (*dll_rb_define_virtual_variable) (const char*,VALUE(*)(),void(*)());
170 static VALUE *dll_rb_stdout;
171 static VALUE *dll_rb_eArgError;
172 static VALUE *dll_rb_eIndexError;
173 static VALUE *dll_rb_eRuntimeError;
174 static VALUE *dll_rb_eStandardError;
175 static VALUE (*dll_rb_eval_string_protect) (const char*, int*);
176 static void (*dll_rb_global_variable) (VALUE*);
177 static VALUE (*dll_rb_hash_aset) (VALUE, VALUE, VALUE);
178 static VALUE (*dll_rb_hash_new) (void);
179 static VALUE (*dll_rb_inspect) (VALUE);
180 static VALUE (*dll_rb_int2inum) (long);
181 static VALUE (*dll_rb_int2inum) (long);
182 static VALUE (*dll_rb_lastline_get) (void);
183 static void (*dll_rb_lastline_set) (VALUE);
184 static void (*dll_rb_load_protect) (VALUE, int, int*);
185 static long (*dll_rb_num2long) (VALUE);
186 static unsigned long (*dll_rb_num2ulong) (VALUE);
187 static VALUE (*dll_rb_obj_alloc) (VALUE);
188 static VALUE (*dll_rb_obj_as_string) (VALUE);
189 static VALUE (*dll_rb_obj_id) (VALUE);
190 static void (*dll_rb_raise) (VALUE, const char*, ...);
191 static char *(*dll_rb_str2cstr) (VALUE,int*);
192 static VALUE (*dll_rb_str_cat) (VALUE, const char*, long);
193 static VALUE (*dll_rb_str_concat) (VALUE, VALUE);
194 static VALUE (*dll_rb_str_new) (const char*, long);
195 static VALUE (*dll_rb_str_new2) (const char*);
196 static VALUE *dll_ruby_errinfo;
197 static void (*dll_ruby_init) (void);
198 static void (*dll_ruby_init_loadpath) (void);
199 #if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 18
200 static int (*dll_rb_w32_snprintf)(char*, size_t, const char*, ...);
201 #endif
203 static HINSTANCE hinstRuby = 0; /* Instance of ruby.dll */
206 * Table of name to function pointer of ruby.
208 #define RUBY_PROC FARPROC
209 static struct
211 char *name;
212 RUBY_PROC *ptr;
213 } ruby_funcname_table[] =
215 {"rb_assoc_new", (RUBY_PROC*)&dll_rb_assoc_new},
216 {"rb_cFalseClass", (RUBY_PROC*)&dll_rb_cFalseClass},
217 {"rb_cFixnum", (RUBY_PROC*)&dll_rb_cFixnum},
218 {"rb_cNilClass", (RUBY_PROC*)&dll_rb_cNilClass},
219 {"rb_cObject", (RUBY_PROC*)&dll_rb_cObject},
220 {"rb_cSymbol", (RUBY_PROC*)&dll_rb_cSymbol},
221 {"rb_cTrueClass", (RUBY_PROC*)&dll_rb_cTrueClass},
222 {"rb_check_type", (RUBY_PROC*)&dll_rb_check_type},
223 {"rb_class_path", (RUBY_PROC*)&dll_rb_class_path},
224 {"rb_data_object_alloc", (RUBY_PROC*)&dll_rb_data_object_alloc},
225 {"rb_define_class_under", (RUBY_PROC*)&dll_rb_define_class_under},
226 {"rb_define_const", (RUBY_PROC*)&dll_rb_define_const},
227 {"rb_define_global_function", (RUBY_PROC*)&dll_rb_define_global_function},
228 {"rb_define_method", (RUBY_PROC*)&dll_rb_define_method},
229 {"rb_define_module", (RUBY_PROC*)&dll_rb_define_module},
230 {"rb_define_module_function", (RUBY_PROC*)&dll_rb_define_module_function},
231 {"rb_define_singleton_method", (RUBY_PROC*)&dll_rb_define_singleton_method},
232 {"rb_define_virtual_variable", (RUBY_PROC*)&dll_rb_define_virtual_variable},
233 {"rb_stdout", (RUBY_PROC*)&dll_rb_stdout},
234 {"rb_eArgError", (RUBY_PROC*)&dll_rb_eArgError},
235 {"rb_eIndexError", (RUBY_PROC*)&dll_rb_eIndexError},
236 {"rb_eRuntimeError", (RUBY_PROC*)&dll_rb_eRuntimeError},
237 {"rb_eStandardError", (RUBY_PROC*)&dll_rb_eStandardError},
238 {"rb_eval_string_protect", (RUBY_PROC*)&dll_rb_eval_string_protect},
239 {"rb_global_variable", (RUBY_PROC*)&dll_rb_global_variable},
240 {"rb_hash_aset", (RUBY_PROC*)&dll_rb_hash_aset},
241 {"rb_hash_new", (RUBY_PROC*)&dll_rb_hash_new},
242 {"rb_inspect", (RUBY_PROC*)&dll_rb_inspect},
243 {"rb_int2inum", (RUBY_PROC*)&dll_rb_int2inum},
244 {"rb_lastline_get", (RUBY_PROC*)&dll_rb_lastline_get},
245 {"rb_lastline_set", (RUBY_PROC*)&dll_rb_lastline_set},
246 {"rb_load_protect", (RUBY_PROC*)&dll_rb_load_protect},
247 {"rb_num2long", (RUBY_PROC*)&dll_rb_num2long},
248 {"rb_num2ulong", (RUBY_PROC*)&dll_rb_num2ulong},
249 {"rb_obj_alloc", (RUBY_PROC*)&dll_rb_obj_alloc},
250 {"rb_obj_as_string", (RUBY_PROC*)&dll_rb_obj_as_string},
251 {"rb_obj_id", (RUBY_PROC*)&dll_rb_obj_id},
252 {"rb_raise", (RUBY_PROC*)&dll_rb_raise},
253 {"rb_str2cstr", (RUBY_PROC*)&dll_rb_str2cstr},
254 {"rb_str_cat", (RUBY_PROC*)&dll_rb_str_cat},
255 {"rb_str_concat", (RUBY_PROC*)&dll_rb_str_concat},
256 {"rb_str_new", (RUBY_PROC*)&dll_rb_str_new},
257 {"rb_str_new2", (RUBY_PROC*)&dll_rb_str_new2},
258 {"ruby_errinfo", (RUBY_PROC*)&dll_ruby_errinfo},
259 {"ruby_init", (RUBY_PROC*)&dll_ruby_init},
260 {"ruby_init_loadpath", (RUBY_PROC*)&dll_ruby_init_loadpath},
261 #if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 18
262 {"rb_w32_snprintf", (RUBY_PROC*)&dll_rb_w32_snprintf},
263 #endif
264 {"", NULL},
268 * Free ruby.dll
270 static void
271 end_dynamic_ruby()
273 if (hinstRuby)
275 FreeLibrary(hinstRuby);
276 hinstRuby = 0;
281 * Load library and get all pointers.
282 * Parameter 'libname' provides name of DLL.
283 * Return OK or FAIL.
285 static int
286 ruby_runtime_link_init(char *libname, int verbose)
288 int i;
290 if (hinstRuby)
291 return OK;
292 hinstRuby = LoadLibrary(libname);
293 if (!hinstRuby)
295 if (verbose)
296 EMSG2(_(e_loadlib), libname);
297 return FAIL;
300 for (i = 0; ruby_funcname_table[i].ptr; ++i)
302 if (!(*ruby_funcname_table[i].ptr = GetProcAddress(hinstRuby,
303 ruby_funcname_table[i].name)))
305 FreeLibrary(hinstRuby);
306 hinstRuby = 0;
307 if (verbose)
308 EMSG2(_(e_loadfunc), ruby_funcname_table[i].name);
309 return FAIL;
312 return OK;
316 * If ruby is enabled (there is installed ruby on Windows system) return TRUE,
317 * else FALSE.
320 ruby_enabled(verbose)
321 int verbose;
323 return ruby_runtime_link_init(DYNAMIC_RUBY_DLL, verbose) == OK;
325 #endif /* defined(DYNAMIC_RUBY) || defined(PROTO) */
327 void
328 ruby_end()
330 #ifdef DYNAMIC_RUBY
331 end_dynamic_ruby();
332 #endif
335 void ex_ruby(exarg_T *eap)
337 int state;
338 char *script = NULL;
340 script = (char *)script_get(eap, eap->arg);
341 if (!eap->skip && ensure_ruby_initialized())
343 if (script == NULL)
344 rb_eval_string_protect((char *)eap->arg, &state);
345 else
346 rb_eval_string_protect(script, &state);
347 if (state)
348 error_print(state);
350 vim_free(script);
353 void ex_rubydo(exarg_T *eap)
355 int state;
356 linenr_T i;
358 if (ensure_ruby_initialized())
360 if (u_save(eap->line1 - 1, eap->line2 + 1) != OK)
361 return;
362 for (i = eap->line1; i <= eap->line2; i++) {
363 VALUE line, oldline;
365 line = oldline = rb_str_new2((char *)ml_get(i));
366 rb_lastline_set(line);
367 rb_eval_string_protect((char *) eap->arg, &state);
368 if (state) {
369 error_print(state);
370 break;
372 line = rb_lastline_get();
373 if (!NIL_P(line)) {
374 if (TYPE(line) != T_STRING) {
375 EMSG(_("E265: $_ must be an instance of String"));
376 return;
378 ml_replace(i, (char_u *) STR2CSTR(line), 1);
379 changed();
380 #ifdef SYNTAX_HL
381 syn_changed(i); /* recompute syntax hl. for this line */
382 #endif
385 check_cursor();
386 update_curbuf(NOT_VALID);
390 void ex_rubyfile(exarg_T *eap)
392 int state;
394 if (ensure_ruby_initialized())
396 rb_load_protect(rb_str_new2((char *) eap->arg), 0, &state);
397 if (state) error_print(state);
401 void ruby_buffer_free(buf_T *buf)
403 if (buf->b_ruby_ref)
405 rb_hash_aset(objtbl, rb_obj_id((VALUE) buf->b_ruby_ref), Qnil);
406 RDATA(buf->b_ruby_ref)->data = NULL;
410 void ruby_window_free(win_T *win)
412 if (win->w_ruby_ref)
414 rb_hash_aset(objtbl, rb_obj_id((VALUE) win->w_ruby_ref), Qnil);
415 RDATA(win->w_ruby_ref)->data = NULL;
419 static int ensure_ruby_initialized(void)
421 if (!ruby_initialized)
423 #ifdef DYNAMIC_RUBY
424 if (ruby_enabled(TRUE))
426 #endif
427 ruby_init();
428 ruby_init_loadpath();
429 ruby_io_init();
430 ruby_vim_init();
431 ruby_initialized = 1;
432 #ifdef DYNAMIC_RUBY
434 else
436 EMSG(_("E266: Sorry, this command is disabled, the Ruby library could not be loaded."));
437 return 0;
439 #endif
441 return ruby_initialized;
444 static void error_print(int state)
446 #ifndef DYNAMIC_RUBY
447 RUBYEXTERN VALUE ruby_errinfo;
448 #endif
449 VALUE eclass;
450 VALUE einfo;
451 char buff[BUFSIZ];
453 #define TAG_RETURN 0x1
454 #define TAG_BREAK 0x2
455 #define TAG_NEXT 0x3
456 #define TAG_RETRY 0x4
457 #define TAG_REDO 0x5
458 #define TAG_RAISE 0x6
459 #define TAG_THROW 0x7
460 #define TAG_FATAL 0x8
461 #define TAG_MASK 0xf
463 switch (state) {
464 case TAG_RETURN:
465 EMSG(_("E267: unexpected return"));
466 break;
467 case TAG_NEXT:
468 EMSG(_("E268: unexpected next"));
469 break;
470 case TAG_BREAK:
471 EMSG(_("E269: unexpected break"));
472 break;
473 case TAG_REDO:
474 EMSG(_("E270: unexpected redo"));
475 break;
476 case TAG_RETRY:
477 EMSG(_("E271: retry outside of rescue clause"));
478 break;
479 case TAG_RAISE:
480 case TAG_FATAL:
481 eclass = CLASS_OF(ruby_errinfo);
482 einfo = rb_obj_as_string(ruby_errinfo);
483 if (eclass == rb_eRuntimeError && RSTRING(einfo)->len == 0) {
484 EMSG(_("E272: unhandled exception"));
486 else {
487 VALUE epath;
488 char *p;
490 epath = rb_class_path(eclass);
491 vim_snprintf(buff, BUFSIZ, "%s: %s",
492 RSTRING(epath)->ptr, RSTRING(einfo)->ptr);
493 p = strchr(buff, '\n');
494 if (p) *p = '\0';
495 EMSG(buff);
497 break;
498 default:
499 vim_snprintf(buff, BUFSIZ, _("E273: unknown longjmp status %d"), state);
500 EMSG(buff);
501 break;
505 static VALUE vim_message(VALUE self, VALUE str)
507 char *buff, *p;
509 str = rb_obj_as_string(str);
510 buff = ALLOCA_N(char, RSTRING(str)->len);
511 strcpy(buff, RSTRING(str)->ptr);
512 p = strchr(buff, '\n');
513 if (p) *p = '\0';
514 MSG(buff);
515 return Qnil;
518 static VALUE vim_set_option(VALUE self, VALUE str)
520 do_set((char_u *)STR2CSTR(str), 0);
521 update_screen(NOT_VALID);
522 return Qnil;
525 static VALUE vim_command(VALUE self, VALUE str)
527 do_cmdline_cmd((char_u *)STR2CSTR(str));
528 return Qnil;
531 static VALUE vim_evaluate(VALUE self, VALUE str)
533 #ifdef FEAT_EVAL
534 char_u *value = eval_to_string((char_u *)STR2CSTR(str), NULL, TRUE);
536 if (value != NULL)
538 VALUE val = rb_str_new2((char *)value);
539 vim_free(value);
540 return val;
542 else
543 #endif
544 return Qnil;
547 static VALUE buffer_new(buf_T *buf)
549 if (buf->b_ruby_ref)
551 return (VALUE) buf->b_ruby_ref;
553 else
555 VALUE obj = Data_Wrap_Struct(cBuffer, 0, 0, buf);
556 buf->b_ruby_ref = (void *) obj;
557 rb_hash_aset(objtbl, rb_obj_id(obj), obj);
558 return obj;
562 static buf_T *get_buf(VALUE obj)
564 buf_T *buf;
566 Data_Get_Struct(obj, buf_T, buf);
567 if (buf == NULL)
568 rb_raise(eDeletedBufferError, "attempt to refer to deleted buffer");
569 return buf;
572 static VALUE buffer_s_current()
574 return buffer_new(curbuf);
577 static VALUE buffer_s_count()
579 buf_T *b;
580 int n = 0;
582 for (b = firstbuf; b != NULL; b = b->b_next)
584 /* Deleted buffers should not be counted
585 * SegPhault - 01/07/05 */
586 if (b->b_p_bl)
587 n++;
590 return INT2NUM(n);
593 static VALUE buffer_s_aref(VALUE self, VALUE num)
595 buf_T *b;
596 int n = NUM2INT(num);
598 for (b = firstbuf; b != NULL; b = b->b_next)
600 /* Deleted buffers should not be counted
601 * SegPhault - 01/07/05 */
602 if (!b->b_p_bl)
603 continue;
605 if (n == 0)
606 return buffer_new(b);
608 n--;
610 return Qnil;
613 static VALUE buffer_name(VALUE self)
615 buf_T *buf = get_buf(self);
617 return buf->b_ffname ? rb_str_new2((char *)buf->b_ffname) : Qnil;
620 static VALUE buffer_number(VALUE self)
622 buf_T *buf = get_buf(self);
624 return INT2NUM(buf->b_fnum);
627 static VALUE buffer_count(VALUE self)
629 buf_T *buf = get_buf(self);
631 return INT2NUM(buf->b_ml.ml_line_count);
634 static VALUE get_buffer_line(buf_T *buf, linenr_T n)
636 if (n > 0 && n <= buf->b_ml.ml_line_count)
638 char *line = (char *)ml_get_buf(buf, n, FALSE);
639 return line ? rb_str_new2(line) : Qnil;
641 rb_raise(rb_eIndexError, "index %d out of buffer", n);
642 return Qnil; /* For stop warning */
645 static VALUE buffer_aref(VALUE self, VALUE num)
647 buf_T *buf = get_buf(self);
649 if (buf != NULL)
650 return get_buffer_line(buf, (linenr_T)NUM2LONG(num));
651 return Qnil; /* For stop warning */
654 static VALUE set_buffer_line(buf_T *buf, linenr_T n, VALUE str)
656 char *line = STR2CSTR(str);
657 aco_save_T aco;
659 if (n > 0 && n <= buf->b_ml.ml_line_count && line != NULL)
661 /* set curwin/curbuf for "buf" and save some things */
662 aucmd_prepbuf(&aco, buf);
664 if (u_savesub(n) == OK) {
665 ml_replace(n, (char_u *)line, TRUE);
666 changed();
667 #ifdef SYNTAX_HL
668 syn_changed(n); /* recompute syntax hl. for this line */
669 #endif
672 /* restore curwin/curbuf and a few other things */
673 aucmd_restbuf(&aco);
674 /* Careful: autocommands may have made "buf" invalid! */
676 update_curbuf(NOT_VALID);
678 else
680 rb_raise(rb_eIndexError, "index %d out of buffer", n);
681 return Qnil; /* For stop warning */
683 return str;
686 static VALUE buffer_aset(VALUE self, VALUE num, VALUE str)
688 buf_T *buf = get_buf(self);
690 if (buf != NULL)
691 return set_buffer_line(buf, (linenr_T)NUM2LONG(num), str);
692 return str;
695 static VALUE buffer_delete(VALUE self, VALUE num)
697 buf_T *buf = get_buf(self);
698 long n = NUM2LONG(num);
699 aco_save_T aco;
701 if (n > 0 && n <= buf->b_ml.ml_line_count)
703 /* set curwin/curbuf for "buf" and save some things */
704 aucmd_prepbuf(&aco, buf);
706 if (u_savedel(n, 1) == OK) {
707 ml_delete(n, 0);
709 /* Changes to non-active buffers should properly refresh
710 * SegPhault - 01/09/05 */
711 deleted_lines_mark(n, 1L);
713 changed();
716 /* restore curwin/curbuf and a few other things */
717 aucmd_restbuf(&aco);
718 /* Careful: autocommands may have made "buf" invalid! */
720 update_curbuf(NOT_VALID);
722 else
724 rb_raise(rb_eIndexError, "index %d out of buffer", n);
726 return Qnil;
729 static VALUE buffer_append(VALUE self, VALUE num, VALUE str)
731 buf_T *buf = get_buf(self);
732 char *line = STR2CSTR(str);
733 long n = NUM2LONG(num);
734 aco_save_T aco;
736 if (n >= 0 && n <= buf->b_ml.ml_line_count && line != NULL)
738 /* set curwin/curbuf for "buf" and save some things */
739 aucmd_prepbuf(&aco, buf);
741 if (u_inssub(n + 1) == OK) {
742 ml_append(n, (char_u *) line, (colnr_T) 0, FALSE);
744 /* Changes to non-active buffers should properly refresh screen
745 * SegPhault - 12/20/04 */
746 appended_lines_mark(n, 1L);
748 changed();
751 /* restore curwin/curbuf and a few other things */
752 aucmd_restbuf(&aco);
753 /* Careful: autocommands may have made "buf" invalid! */
755 update_curbuf(NOT_VALID);
757 else {
758 rb_raise(rb_eIndexError, "index %d out of buffer", n);
760 return str;
763 static VALUE window_new(win_T *win)
765 if (win->w_ruby_ref)
767 return (VALUE) win->w_ruby_ref;
769 else
771 VALUE obj = Data_Wrap_Struct(cVimWindow, 0, 0, win);
772 win->w_ruby_ref = (void *) obj;
773 rb_hash_aset(objtbl, rb_obj_id(obj), obj);
774 return obj;
778 static win_T *get_win(VALUE obj)
780 win_T *win;
782 Data_Get_Struct(obj, win_T, win);
783 if (win == NULL)
784 rb_raise(eDeletedWindowError, "attempt to refer to deleted window");
785 return win;
788 static VALUE window_s_current()
790 return window_new(curwin);
794 * Added line manipulation functions
795 * SegPhault - 03/07/05
797 static VALUE line_s_current()
799 return get_buffer_line(curbuf, curwin->w_cursor.lnum);
802 static VALUE set_current_line(VALUE self, VALUE str)
804 return set_buffer_line(curbuf, curwin->w_cursor.lnum, str);
807 static VALUE current_line_number()
809 return INT2FIX((int)curwin->w_cursor.lnum);
814 static VALUE window_s_count()
816 #ifdef FEAT_WINDOWS
817 win_T *w;
818 int n = 0;
820 for (w = firstwin; w != NULL; w = w->w_next)
821 n++;
822 return INT2NUM(n);
823 #else
824 return INT2NUM(1);
825 #endif
828 static VALUE window_s_aref(VALUE self, VALUE num)
830 win_T *w;
831 int n = NUM2INT(num);
833 #ifndef FEAT_WINDOWS
834 w = curwin;
835 #else
836 for (w = firstwin; w != NULL; w = w->w_next, --n)
837 #endif
838 if (n == 0)
839 return window_new(w);
840 return Qnil;
843 static VALUE window_buffer(VALUE self)
845 win_T *win = get_win(self);
847 return buffer_new(win->w_buffer);
850 static VALUE window_height(VALUE self)
852 win_T *win = get_win(self);
854 return INT2NUM(win->w_height);
857 static VALUE window_set_height(VALUE self, VALUE height)
859 win_T *win = get_win(self);
860 win_T *savewin = curwin;
862 curwin = win;
863 win_setheight(NUM2INT(height));
864 curwin = savewin;
865 return height;
868 static VALUE window_width(VALUE self)
870 win_T *win = get_win(self);
872 return INT2NUM(win->w_width);
875 static VALUE window_set_width(VALUE self, VALUE width)
877 win_T *win = get_win(self);
878 win_T *savewin = curwin;
880 curwin = win;
881 win_setwidth(NUM2INT(width));
882 curwin = savewin;
883 return width;
886 static VALUE window_cursor(VALUE self)
888 win_T *win = get_win(self);
890 return rb_assoc_new(INT2NUM(win->w_cursor.lnum), INT2NUM(win->w_cursor.col));
893 static VALUE window_set_cursor(VALUE self, VALUE pos)
895 VALUE lnum, col;
896 win_T *win = get_win(self);
898 Check_Type(pos, T_ARRAY);
899 if (RARRAY(pos)->len != 2)
900 rb_raise(rb_eArgError, "array length must be 2");
901 lnum = RARRAY(pos)->ptr[0];
902 col = RARRAY(pos)->ptr[1];
903 win->w_cursor.lnum = NUM2LONG(lnum);
904 win->w_cursor.col = NUM2UINT(col);
905 check_cursor(); /* put cursor on an existing line */
906 update_screen(NOT_VALID);
907 return Qnil;
910 static VALUE f_p(int argc, VALUE *argv, VALUE self)
912 int i;
913 VALUE str = rb_str_new("", 0);
915 for (i = 0; i < argc; i++) {
916 if (i > 0) rb_str_cat(str, ", ", 2);
917 rb_str_concat(str, rb_inspect(argv[i]));
919 MSG(RSTRING(str)->ptr);
920 return Qnil;
923 static void ruby_io_init(void)
925 #ifndef DYNAMIC_RUBY
926 RUBYEXTERN VALUE rb_stdout;
927 #endif
929 rb_stdout = rb_obj_alloc(rb_cObject);
930 rb_define_singleton_method(rb_stdout, "write", vim_message, 1);
931 rb_define_global_function("p", f_p, -1);
934 static void ruby_vim_init(void)
936 objtbl = rb_hash_new();
937 rb_global_variable(&objtbl);
939 /* The Vim module used to be called "VIM", but "Vim" is better. Make an
940 * alias "VIM" for backwards compatiblity. */
941 mVIM = rb_define_module("Vim");
942 rb_define_const(rb_cObject, "VIM", mVIM);
943 rb_define_const(mVIM, "VERSION_MAJOR", INT2NUM(VIM_VERSION_MAJOR));
944 rb_define_const(mVIM, "VERSION_MINOR", INT2NUM(VIM_VERSION_MINOR));
945 rb_define_const(mVIM, "VERSION_BUILD", INT2NUM(VIM_VERSION_BUILD));
946 rb_define_const(mVIM, "VERSION_PATCHLEVEL", INT2NUM(VIM_VERSION_PATCHLEVEL));
947 rb_define_const(mVIM, "VERSION_SHORT", rb_str_new2(VIM_VERSION_SHORT));
948 rb_define_const(mVIM, "VERSION_MEDIUM", rb_str_new2(VIM_VERSION_MEDIUM));
949 rb_define_const(mVIM, "VERSION_LONG", rb_str_new2(VIM_VERSION_LONG));
950 rb_define_const(mVIM, "VERSION_LONG_DATE", rb_str_new2(VIM_VERSION_LONG_DATE));
951 rb_define_module_function(mVIM, "message", vim_message, 1);
952 rb_define_module_function(mVIM, "set_option", vim_set_option, 1);
953 rb_define_module_function(mVIM, "command", vim_command, 1);
954 rb_define_module_function(mVIM, "evaluate", vim_evaluate, 1);
956 eDeletedBufferError = rb_define_class_under(mVIM, "DeletedBufferError",
957 rb_eStandardError);
958 eDeletedWindowError = rb_define_class_under(mVIM, "DeletedWindowError",
959 rb_eStandardError);
961 cBuffer = rb_define_class_under(mVIM, "Buffer", rb_cObject);
962 rb_define_singleton_method(cBuffer, "current", buffer_s_current, 0);
963 rb_define_singleton_method(cBuffer, "count", buffer_s_count, 0);
964 rb_define_singleton_method(cBuffer, "[]", buffer_s_aref, 1);
965 rb_define_method(cBuffer, "name", buffer_name, 0);
966 rb_define_method(cBuffer, "number", buffer_number, 0);
967 rb_define_method(cBuffer, "count", buffer_count, 0);
968 rb_define_method(cBuffer, "length", buffer_count, 0);
969 rb_define_method(cBuffer, "[]", buffer_aref, 1);
970 rb_define_method(cBuffer, "[]=", buffer_aset, 2);
971 rb_define_method(cBuffer, "delete", buffer_delete, 1);
972 rb_define_method(cBuffer, "append", buffer_append, 2);
974 /* Added line manipulation functions
975 * SegPhault - 03/07/05 */
976 rb_define_method(cBuffer, "line_number", current_line_number, 0);
977 rb_define_method(cBuffer, "line", line_s_current, 0);
978 rb_define_method(cBuffer, "line=", set_current_line, 1);
981 cVimWindow = rb_define_class_under(mVIM, "Window", rb_cObject);
982 rb_define_singleton_method(cVimWindow, "current", window_s_current, 0);
983 rb_define_singleton_method(cVimWindow, "count", window_s_count, 0);
984 rb_define_singleton_method(cVimWindow, "[]", window_s_aref, 1);
985 rb_define_method(cVimWindow, "buffer", window_buffer, 0);
986 rb_define_method(cVimWindow, "height", window_height, 0);
987 rb_define_method(cVimWindow, "height=", window_set_height, 1);
988 rb_define_method(cVimWindow, "width", window_width, 0);
989 rb_define_method(cVimWindow, "width=", window_set_width, 1);
990 rb_define_method(cVimWindow, "cursor", window_cursor, 0);
991 rb_define_method(cVimWindow, "cursor=", window_set_cursor, 1);
993 rb_define_virtual_variable("$curbuf", buffer_s_current, 0);
994 rb_define_virtual_variable("$curwin", window_s_current, 0);