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.
17 # if !defined(DYNAMIC_RUBY_VER) || (DYNAMIC_RUBY_VER < 18)
21 # define IMPORT /* For static dll usage __declspec(dllimport) */
22 # define RUBYEXTERN __declspec(dllimport)
26 # define RUBYEXTERN extern
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.
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.
51 /* suggested by Ariya Mizutani */
52 #if (_MSC_VER == 1200)
57 #if defined(RUBY_VERSION) && RUBY_VERSION >= 19
58 # include <ruby/encoding.h>
64 /* T_DATA defined both by Ruby and Mac header files, hack around it... */
65 #if defined(MACOS_X_UNIX) || defined(macintosh)
66 # define __OPENTRANSPORT__
67 # define __OPENTRANSPORTPROTOCOL__
68 # define __OPENTRANSPORTPROVIDERS__
72 * Backward compatiblity for Ruby 1.8 and earlier.
73 * Ruby 1.9 does not provide STR2CSTR, instead StringValuePtr is provided.
74 * Ruby 1.9 does not provide RXXX(s)->len and RXXX(s)->ptr, instead
75 * RXXX_LEN(s) and RXXX_PTR(s) are provided.
77 #ifndef StringValuePtr
78 # define StringValuePtr(s) STR2CSTR(s)
81 # define RARRAY_LEN(s) RARRAY(s)->len
84 # define RARRAY_PTR(s) RARRAY(s)->ptr
87 # define RSTRING_LEN(s) RSTRING(s)->len
90 # define RSTRING_PTR(s) RSTRING(s)->ptr
96 #if defined(PROTO) && !defined(FEAT_RUBY)
97 /* Define these to be able to generate the function prototypes. */
99 # define RUBY_DATA_FUNC int
102 static int ruby_initialized
= 0;
106 static VALUE cBuffer
;
107 static VALUE cVimWindow
;
108 static VALUE eDeletedBufferError
;
109 static VALUE eDeletedWindowError
;
111 static int ensure_ruby_initialized(void);
112 static void error_print(int);
113 static void ruby_io_init(void);
114 static void ruby_vim_init(void);
116 #if defined(DYNAMIC_RUBY) || defined(PROTO)
118 # define HINSTANCE int /* for generating prototypes */
124 #define rb_assoc_new dll_rb_assoc_new
125 #define rb_cObject (*dll_rb_cObject)
126 #define rb_check_type dll_rb_check_type
127 #define rb_class_path dll_rb_class_path
128 #define rb_data_object_alloc dll_rb_data_object_alloc
129 #define rb_define_class_under dll_rb_define_class_under
130 #define rb_define_const dll_rb_define_const
131 #define rb_define_global_function dll_rb_define_global_function
132 #define rb_define_method dll_rb_define_method
133 #define rb_define_module dll_rb_define_module
134 #define rb_define_module_function dll_rb_define_module_function
135 #define rb_define_singleton_method dll_rb_define_singleton_method
136 #define rb_define_virtual_variable dll_rb_define_virtual_variable
137 #define rb_stdout (*dll_rb_stdout)
138 #define rb_eArgError (*dll_rb_eArgError)
139 #define rb_eIndexError (*dll_rb_eIndexError)
140 #define rb_eRuntimeError (*dll_rb_eRuntimeError)
141 #define rb_eStandardError (*dll_rb_eStandardError)
142 #define rb_eval_string_protect dll_rb_eval_string_protect
143 #define rb_global_variable dll_rb_global_variable
144 #define rb_hash_aset dll_rb_hash_aset
145 #define rb_hash_new dll_rb_hash_new
146 #define rb_inspect dll_rb_inspect
147 #define rb_int2inum dll_rb_int2inum
148 #define rb_lastline_get dll_rb_lastline_get
149 #define rb_lastline_set dll_rb_lastline_set
150 #define rb_load_protect dll_rb_load_protect
151 #define rb_num2long dll_rb_num2long
152 #define rb_num2ulong dll_rb_num2ulong
153 #define rb_obj_alloc dll_rb_obj_alloc
154 #define rb_obj_as_string dll_rb_obj_as_string
155 #define rb_obj_id dll_rb_obj_id
156 #define rb_raise dll_rb_raise
157 #define rb_str2cstr dll_rb_str2cstr
158 #define rb_str_cat dll_rb_str_cat
159 #define rb_str_concat dll_rb_str_concat
160 #define rb_str_new dll_rb_str_new
161 #define rb_str_new2 dll_rb_str_new2
162 #if defined(RUBY_VERSION) && RUBY_VERSION >= 19
163 # define rb_errinfo dll_rb_errinfo
165 # define ruby_errinfo (*dll_ruby_errinfo)
167 #define ruby_init dll_ruby_init
168 #define ruby_init_loadpath dll_ruby_init_loadpath
169 #define NtInitialize dll_NtInitialize
170 #if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 18
171 # define rb_w32_snprintf dll_rb_w32_snprintf
174 #if defined(RUBY_VERSION) && RUBY_VERSION >= 19
175 # define ruby_script dll_ruby_script
176 # define rb_enc_find_index dll_rb_enc_find_index
177 # define rb_enc_find dll_rb_enc_find
178 # define rb_enc_str_new dll_rb_enc_str_new
179 # define rb_sprintf dll_rb_sprintf
183 * Pointers for dynamic link
185 static VALUE (*dll_rb_assoc_new
) (VALUE
, VALUE
);
186 static VALUE
*dll_rb_cFalseClass
;
187 static VALUE
*dll_rb_cFixnum
;
188 static VALUE
*dll_rb_cNilClass
;
189 static VALUE
*dll_rb_cObject
;
190 static VALUE
*dll_rb_cSymbol
;
191 static VALUE
*dll_rb_cTrueClass
;
192 static void (*dll_rb_check_type
) (VALUE
,int);
193 static VALUE (*dll_rb_class_path
) (VALUE
);
194 static VALUE (*dll_rb_data_object_alloc
) (VALUE
, void*, RUBY_DATA_FUNC
, RUBY_DATA_FUNC
);
195 static VALUE (*dll_rb_define_class_under
) (VALUE
, const char*, VALUE
);
196 static void (*dll_rb_define_const
) (VALUE
,const char*,VALUE
);
197 static void (*dll_rb_define_global_function
) (const char*,VALUE(*)(),int);
198 static void (*dll_rb_define_method
) (VALUE
,const char*,VALUE(*)(),int);
199 static VALUE (*dll_rb_define_module
) (const char*);
200 static void (*dll_rb_define_module_function
) (VALUE
,const char*,VALUE(*)(),int);
201 static void (*dll_rb_define_singleton_method
) (VALUE
,const char*,VALUE(*)(),int);
202 static void (*dll_rb_define_virtual_variable
) (const char*,VALUE(*)(),void(*)());
203 static VALUE
*dll_rb_stdout
;
204 static VALUE
*dll_rb_eArgError
;
205 static VALUE
*dll_rb_eIndexError
;
206 static VALUE
*dll_rb_eRuntimeError
;
207 static VALUE
*dll_rb_eStandardError
;
208 static VALUE (*dll_rb_eval_string_protect
) (const char*, int*);
209 static void (*dll_rb_global_variable
) (VALUE
*);
210 static VALUE (*dll_rb_hash_aset
) (VALUE
, VALUE
, VALUE
);
211 static VALUE (*dll_rb_hash_new
) (void);
212 static VALUE (*dll_rb_inspect
) (VALUE
);
213 static VALUE (*dll_rb_int2inum
) (long);
214 static VALUE (*dll_rb_int2inum
) (long);
215 static VALUE (*dll_rb_lastline_get
) (void);
216 static void (*dll_rb_lastline_set
) (VALUE
);
217 static void (*dll_rb_load_protect
) (VALUE
, int, int*);
218 static long (*dll_rb_num2long
) (VALUE
);
219 static unsigned long (*dll_rb_num2ulong
) (VALUE
);
220 static VALUE (*dll_rb_obj_alloc
) (VALUE
);
221 static VALUE (*dll_rb_obj_as_string
) (VALUE
);
222 static VALUE (*dll_rb_obj_id
) (VALUE
);
223 static void (*dll_rb_raise
) (VALUE
, const char*, ...);
224 static char *(*dll_rb_str2cstr
) (VALUE
,int*);
225 static VALUE (*dll_rb_str_cat
) (VALUE
, const char*, long);
226 static VALUE (*dll_rb_str_concat
) (VALUE
, VALUE
);
227 static VALUE (*dll_rb_str_new
) (const char*, long);
228 static VALUE (*dll_rb_str_new2
) (const char*);
229 #if defined(RUBY_VERSION) && RUBY_VERSION >= 19
230 static VALUE (*dll_rb_errinfo
) (void);
232 static VALUE
*dll_ruby_errinfo
;
234 static void (*dll_ruby_init
) (void);
235 static void (*dll_ruby_init_loadpath
) (void);
236 static void (*dll_NtInitialize
) (int*, char***);
237 #if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 18
238 static int (*dll_rb_w32_snprintf
)(char*, size_t, const char*, ...);
241 #if defined(RUBY_VERSION) && RUBY_VERSION >= 19
242 static void (*dll_ruby_script
) (const char*);
243 static int (*dll_rb_enc_find_index
) (const char*);
244 static rb_encoding
* (*dll_rb_enc_find
) (const char*);
245 static VALUE (*dll_rb_enc_str_new
) (const char*, long, rb_encoding
*);
246 static VALUE (*dll_rb_sprintf
) (const char*, ...);
249 static HINSTANCE hinstRuby
= 0; /* Instance of ruby.dll */
252 * Table of name to function pointer of ruby.
254 #define RUBY_PROC FARPROC
259 } ruby_funcname_table
[] =
261 {"rb_assoc_new", (RUBY_PROC
*)&dll_rb_assoc_new
},
262 {"rb_cFalseClass", (RUBY_PROC
*)&dll_rb_cFalseClass
},
263 {"rb_cFixnum", (RUBY_PROC
*)&dll_rb_cFixnum
},
264 {"rb_cNilClass", (RUBY_PROC
*)&dll_rb_cNilClass
},
265 {"rb_cObject", (RUBY_PROC
*)&dll_rb_cObject
},
266 {"rb_cSymbol", (RUBY_PROC
*)&dll_rb_cSymbol
},
267 {"rb_cTrueClass", (RUBY_PROC
*)&dll_rb_cTrueClass
},
268 {"rb_check_type", (RUBY_PROC
*)&dll_rb_check_type
},
269 {"rb_class_path", (RUBY_PROC
*)&dll_rb_class_path
},
270 {"rb_data_object_alloc", (RUBY_PROC
*)&dll_rb_data_object_alloc
},
271 {"rb_define_class_under", (RUBY_PROC
*)&dll_rb_define_class_under
},
272 {"rb_define_const", (RUBY_PROC
*)&dll_rb_define_const
},
273 {"rb_define_global_function", (RUBY_PROC
*)&dll_rb_define_global_function
},
274 {"rb_define_method", (RUBY_PROC
*)&dll_rb_define_method
},
275 {"rb_define_module", (RUBY_PROC
*)&dll_rb_define_module
},
276 {"rb_define_module_function", (RUBY_PROC
*)&dll_rb_define_module_function
},
277 {"rb_define_singleton_method", (RUBY_PROC
*)&dll_rb_define_singleton_method
},
278 {"rb_define_virtual_variable", (RUBY_PROC
*)&dll_rb_define_virtual_variable
},
279 {"rb_stdout", (RUBY_PROC
*)&dll_rb_stdout
},
280 {"rb_eArgError", (RUBY_PROC
*)&dll_rb_eArgError
},
281 {"rb_eIndexError", (RUBY_PROC
*)&dll_rb_eIndexError
},
282 {"rb_eRuntimeError", (RUBY_PROC
*)&dll_rb_eRuntimeError
},
283 {"rb_eStandardError", (RUBY_PROC
*)&dll_rb_eStandardError
},
284 {"rb_eval_string_protect", (RUBY_PROC
*)&dll_rb_eval_string_protect
},
285 {"rb_global_variable", (RUBY_PROC
*)&dll_rb_global_variable
},
286 {"rb_hash_aset", (RUBY_PROC
*)&dll_rb_hash_aset
},
287 {"rb_hash_new", (RUBY_PROC
*)&dll_rb_hash_new
},
288 {"rb_inspect", (RUBY_PROC
*)&dll_rb_inspect
},
289 {"rb_int2inum", (RUBY_PROC
*)&dll_rb_int2inum
},
290 {"rb_lastline_get", (RUBY_PROC
*)&dll_rb_lastline_get
},
291 {"rb_lastline_set", (RUBY_PROC
*)&dll_rb_lastline_set
},
292 {"rb_load_protect", (RUBY_PROC
*)&dll_rb_load_protect
},
293 {"rb_num2long", (RUBY_PROC
*)&dll_rb_num2long
},
294 {"rb_num2ulong", (RUBY_PROC
*)&dll_rb_num2ulong
},
295 {"rb_obj_alloc", (RUBY_PROC
*)&dll_rb_obj_alloc
},
296 {"rb_obj_as_string", (RUBY_PROC
*)&dll_rb_obj_as_string
},
297 {"rb_obj_id", (RUBY_PROC
*)&dll_rb_obj_id
},
298 {"rb_raise", (RUBY_PROC
*)&dll_rb_raise
},
299 {"rb_str2cstr", (RUBY_PROC
*)&dll_rb_str2cstr
},
300 {"rb_str_cat", (RUBY_PROC
*)&dll_rb_str_cat
},
301 {"rb_str_concat", (RUBY_PROC
*)&dll_rb_str_concat
},
302 {"rb_str_new", (RUBY_PROC
*)&dll_rb_str_new
},
303 {"rb_str_new2", (RUBY_PROC
*)&dll_rb_str_new2
},
304 #if defined(RUBY_VERSION) && RUBY_VERSION >= 19
305 {"rb_errinfo", (RUBY_PROC
*)&dll_rb_errinfo
},
307 {"ruby_errinfo", (RUBY_PROC
*)&dll_ruby_errinfo
},
309 {"ruby_init", (RUBY_PROC
*)&dll_ruby_init
},
310 {"ruby_init_loadpath", (RUBY_PROC
*)&dll_ruby_init_loadpath
},
311 {"NtInitialize", (RUBY_PROC
*)&dll_NtInitialize
},
312 #if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 18
313 {"rb_w32_snprintf", (RUBY_PROC
*)&dll_rb_w32_snprintf
},
315 #if defined(RUBY_VERSION) && RUBY_VERSION >= 19
316 {"ruby_script", (RUBY_PROC
*)&dll_ruby_script
},
317 {"rb_enc_find_index", (RUBY_PROC
*)&dll_rb_enc_find_index
},
318 {"rb_enc_find", (RUBY_PROC
*)&dll_rb_enc_find
},
319 {"rb_enc_str_new", (RUBY_PROC
*)&dll_rb_enc_str_new
},
320 {"rb_sprintf", (RUBY_PROC
*)&dll_rb_sprintf
},
333 FreeLibrary(hinstRuby
);
339 * Load library and get all pointers.
340 * Parameter 'libname' provides name of DLL.
344 ruby_runtime_link_init(char *libname
, int verbose
)
350 hinstRuby
= LoadLibrary(libname
);
354 EMSG2(_(e_loadlib
), libname
);
358 for (i
= 0; ruby_funcname_table
[i
].ptr
; ++i
)
360 if (!(*ruby_funcname_table
[i
].ptr
= GetProcAddress(hinstRuby
,
361 ruby_funcname_table
[i
].name
)))
363 FreeLibrary(hinstRuby
);
366 EMSG2(_(e_loadfunc
), ruby_funcname_table
[i
].name
);
374 * If ruby is enabled (there is installed ruby on Windows system) return TRUE,
378 ruby_enabled(verbose
)
381 return ruby_runtime_link_init(DYNAMIC_RUBY_DLL
, verbose
) == OK
;
383 #endif /* defined(DYNAMIC_RUBY) || defined(PROTO) */
393 void ex_ruby(exarg_T
*eap
)
398 script
= (char *)script_get(eap
, eap
->arg
);
399 if (!eap
->skip
&& ensure_ruby_initialized())
402 rb_eval_string_protect((char *)eap
->arg
, &state
);
404 rb_eval_string_protect(script
, &state
);
412 * In Ruby 1.9 or later, ruby String object has encoding.
413 * conversion buffer string of vim to ruby String object using
414 * VIM encoding option.
417 vim_str2rb_enc_str(const char *s
)
419 #if defined(RUBY_VERSION) && RUBY_VERSION >= 19
425 isnum
= get_option_value((char_u
*)"enc", &lval
, &sval
, 0);
428 enc
= rb_enc_find((char *)sval
);
431 return rb_enc_str_new(s
, strlen(s
), enc
);
435 return rb_str_new2(s
);
439 eval_enc_string_protect(const char *str
, int *state
)
441 #if defined(RUBY_VERSION) && RUBY_VERSION >= 19
448 isnum
= get_option_value((char_u
*)"enc", &lval
, &sval
, 0);
451 enc
= rb_enc_find((char *)sval
);
455 v
= rb_sprintf("#-*- coding:%s -*-\n%s", rb_enc_name(enc
), str
);
456 return rb_eval_string_protect(StringValuePtr(v
), state
);
460 return rb_eval_string_protect(str
, state
);
463 void ex_rubydo(exarg_T
*eap
)
468 if (ensure_ruby_initialized())
470 if (u_save(eap
->line1
- 1, eap
->line2
+ 1) != OK
)
472 for (i
= eap
->line1
; i
<= eap
->line2
; i
++) {
475 line
= oldline
= vim_str2rb_enc_str((char *)ml_get(i
));
476 rb_lastline_set(line
);
477 eval_enc_string_protect((char *) eap
->arg
, &state
);
482 line
= rb_lastline_get();
484 if (TYPE(line
) != T_STRING
) {
485 EMSG(_("E265: $_ must be an instance of String"));
488 ml_replace(i
, (char_u
*) StringValuePtr(line
), 1);
491 syn_changed(i
); /* recompute syntax hl. for this line */
496 update_curbuf(NOT_VALID
);
500 void ex_rubyfile(exarg_T
*eap
)
504 if (ensure_ruby_initialized())
506 rb_load_protect(rb_str_new2((char *) eap
->arg
), 0, &state
);
507 if (state
) error_print(state
);
511 void ruby_buffer_free(buf_T
*buf
)
515 rb_hash_aset(objtbl
, rb_obj_id((VALUE
) buf
->b_ruby_ref
), Qnil
);
516 RDATA(buf
->b_ruby_ref
)->data
= NULL
;
520 void ruby_window_free(win_T
*win
)
524 rb_hash_aset(objtbl
, rb_obj_id((VALUE
) win
->w_ruby_ref
), Qnil
);
525 RDATA(win
->w_ruby_ref
)->data
= NULL
;
529 static int ensure_ruby_initialized(void)
531 if (!ruby_initialized
)
534 if (ruby_enabled(TRUE
))
538 /* suggested by Ariya Mizutani */
540 char *argv
[] = {"gvim.exe"};
541 NtInitialize(&argc
, &argv
);
543 #if defined(RUBY_VERSION) && RUBY_VERSION >= 19
547 #if defined(RUBY_VERSION) && RUBY_VERSION >= 19
548 ruby_script("vim-ruby");
550 ruby_init_loadpath();
552 #if defined(RUBY_VERSION) && RUBY_VERSION >= 19
553 rb_enc_find_index("encdb");
556 ruby_initialized
= 1;
561 EMSG(_("E266: Sorry, this command is disabled, the Ruby library could not be loaded."));
566 return ruby_initialized
;
569 static void error_print(int state
)
572 #if !(defined(RUBY_VERSION) && RUBY_VERSION >= 19)
573 RUBYEXTERN VALUE ruby_errinfo
;
580 #define TAG_RETURN 0x1
581 #define TAG_BREAK 0x2
583 #define TAG_RETRY 0x4
585 #define TAG_RAISE 0x6
586 #define TAG_THROW 0x7
587 #define TAG_FATAL 0x8
592 EMSG(_("E267: unexpected return"));
595 EMSG(_("E268: unexpected next"));
598 EMSG(_("E269: unexpected break"));
601 EMSG(_("E270: unexpected redo"));
604 EMSG(_("E271: retry outside of rescue clause"));
608 #if defined(RUBY_VERSION) && RUBY_VERSION >= 19
609 eclass
= CLASS_OF(rb_errinfo());
610 einfo
= rb_obj_as_string(rb_errinfo());
612 eclass
= CLASS_OF(ruby_errinfo
);
613 einfo
= rb_obj_as_string(ruby_errinfo
);
615 if (eclass
== rb_eRuntimeError
&& RSTRING_LEN(einfo
) == 0) {
616 EMSG(_("E272: unhandled exception"));
622 epath
= rb_class_path(eclass
);
623 vim_snprintf(buff
, BUFSIZ
, "%s: %s",
624 RSTRING_PTR(epath
), RSTRING_PTR(einfo
));
625 p
= strchr(buff
, '\n');
631 vim_snprintf(buff
, BUFSIZ
, _("E273: unknown longjmp status %d"), state
);
637 static VALUE
vim_message(VALUE self UNUSED
, VALUE str
)
641 str
= rb_obj_as_string(str
);
642 buff
= ALLOCA_N(char, RSTRING_LEN(str
));
643 strcpy(buff
, RSTRING_PTR(str
));
644 p
= strchr(buff
, '\n');
650 static VALUE
vim_set_option(VALUE self UNUSED
, VALUE str
)
652 do_set((char_u
*)StringValuePtr(str
), 0);
653 update_screen(NOT_VALID
);
657 static VALUE
vim_command(VALUE self UNUSED
, VALUE str
)
659 do_cmdline_cmd((char_u
*)StringValuePtr(str
));
664 static VALUE
vim_to_ruby(typval_T
*tv
)
668 if (tv
->v_type
== VAR_STRING
)
670 result
= rb_str_new2((char *)tv
->vval
.v_string
);
672 else if (tv
->v_type
== VAR_NUMBER
)
674 result
= INT2NUM(tv
->vval
.v_number
);
677 else if (tv
->v_type
== VAR_FLOAT
)
679 result
= rb_float_new(tv
->vval
.v_float
);
682 else if (tv
->v_type
== VAR_LIST
)
684 list_T
*list
= tv
->vval
.v_list
;
687 result
= rb_ary_new();
691 for (curr
= list
->lv_first
; curr
!= NULL
; curr
= curr
->li_next
)
693 rb_ary_push(result
, vim_to_ruby(&curr
->li_tv
));
697 else if (tv
->v_type
== VAR_DICT
)
699 result
= rb_hash_new();
701 if (tv
->vval
.v_dict
!= NULL
)
703 hashtab_T
*ht
= &tv
->vval
.v_dict
->dv_hashtab
;
704 long_u todo
= ht
->ht_used
;
708 for (hi
= ht
->ht_array
; todo
> 0; ++hi
)
710 if (!HASHITEM_EMPTY(hi
))
714 di
= dict_lookup(hi
);
715 rb_hash_aset(result
, rb_str_new2((char *)hi
->hi_key
),
716 vim_to_ruby(&di
->di_tv
));
720 } /* else return Qnil; */
726 static VALUE
vim_evaluate(VALUE self UNUSED
, VALUE str
)
732 tv
= eval_expr((char_u
*)StringValuePtr(str
), NULL
);
737 result
= vim_to_ruby(tv
);
747 static VALUE
buffer_new(buf_T
*buf
)
751 return (VALUE
) buf
->b_ruby_ref
;
755 VALUE obj
= Data_Wrap_Struct(cBuffer
, 0, 0, buf
);
756 buf
->b_ruby_ref
= (void *) obj
;
757 rb_hash_aset(objtbl
, rb_obj_id(obj
), obj
);
762 static buf_T
*get_buf(VALUE obj
)
766 Data_Get_Struct(obj
, buf_T
, buf
);
768 rb_raise(eDeletedBufferError
, "attempt to refer to deleted buffer");
772 static VALUE
buffer_s_current()
774 return buffer_new(curbuf
);
777 static VALUE
buffer_s_count()
782 for (b
= firstbuf
; b
!= NULL
; b
= b
->b_next
)
784 /* Deleted buffers should not be counted
785 * SegPhault - 01/07/05 */
793 static VALUE
buffer_s_aref(VALUE self UNUSED
, VALUE num
)
796 int n
= NUM2INT(num
);
798 for (b
= firstbuf
; b
!= NULL
; b
= b
->b_next
)
800 /* Deleted buffers should not be counted
801 * SegPhault - 01/07/05 */
806 return buffer_new(b
);
813 static VALUE
buffer_name(VALUE self
)
815 buf_T
*buf
= get_buf(self
);
817 return buf
->b_ffname
? rb_str_new2((char *)buf
->b_ffname
) : Qnil
;
820 static VALUE
buffer_number(VALUE self
)
822 buf_T
*buf
= get_buf(self
);
824 return INT2NUM(buf
->b_fnum
);
827 static VALUE
buffer_count(VALUE self
)
829 buf_T
*buf
= get_buf(self
);
831 return INT2NUM(buf
->b_ml
.ml_line_count
);
834 static VALUE
get_buffer_line(buf_T
*buf
, linenr_T n
)
836 if (n
> 0 && n
<= buf
->b_ml
.ml_line_count
)
838 char *line
= (char *)ml_get_buf(buf
, n
, FALSE
);
839 return line
? vim_str2rb_enc_str(line
) : Qnil
;
841 rb_raise(rb_eIndexError
, "line number %ld out of range", (long)n
);
843 return Qnil
; /* For stop warning */
847 static VALUE
buffer_aref(VALUE self
, VALUE num
)
849 buf_T
*buf
= get_buf(self
);
852 return get_buffer_line(buf
, (linenr_T
)NUM2LONG(num
));
853 return Qnil
; /* For stop warning */
856 static VALUE
set_buffer_line(buf_T
*buf
, linenr_T n
, VALUE str
)
858 char *line
= StringValuePtr(str
);
861 if (n
> 0 && n
<= buf
->b_ml
.ml_line_count
&& line
!= NULL
)
863 /* set curwin/curbuf for "buf" and save some things */
864 aucmd_prepbuf(&aco
, buf
);
866 if (u_savesub(n
) == OK
) {
867 ml_replace(n
, (char_u
*)line
, TRUE
);
870 syn_changed(n
); /* recompute syntax hl. for this line */
874 /* restore curwin/curbuf and a few other things */
876 /* Careful: autocommands may have made "buf" invalid! */
878 update_curbuf(NOT_VALID
);
882 rb_raise(rb_eIndexError
, "line number %ld out of range", (long)n
);
884 return Qnil
; /* For stop warning */
890 static VALUE
buffer_aset(VALUE self
, VALUE num
, VALUE str
)
892 buf_T
*buf
= get_buf(self
);
895 return set_buffer_line(buf
, (linenr_T
)NUM2LONG(num
), str
);
899 static VALUE
buffer_delete(VALUE self
, VALUE num
)
901 buf_T
*buf
= get_buf(self
);
902 long n
= NUM2LONG(num
);
905 if (n
> 0 && n
<= buf
->b_ml
.ml_line_count
)
907 /* set curwin/curbuf for "buf" and save some things */
908 aucmd_prepbuf(&aco
, buf
);
910 if (u_savedel(n
, 1) == OK
) {
913 /* Changes to non-active buffers should properly refresh
914 * SegPhault - 01/09/05 */
915 deleted_lines_mark(n
, 1L);
920 /* restore curwin/curbuf and a few other things */
922 /* Careful: autocommands may have made "buf" invalid! */
924 update_curbuf(NOT_VALID
);
928 rb_raise(rb_eIndexError
, "line number %ld out of range", n
);
933 static VALUE
buffer_append(VALUE self
, VALUE num
, VALUE str
)
935 buf_T
*buf
= get_buf(self
);
936 char *line
= StringValuePtr(str
);
937 long n
= NUM2LONG(num
);
941 rb_raise(rb_eIndexError
, "NULL line");
943 else if (n
>= 0 && n
<= buf
->b_ml
.ml_line_count
)
945 /* set curwin/curbuf for "buf" and save some things */
946 aucmd_prepbuf(&aco
, buf
);
948 if (u_inssub(n
+ 1) == OK
) {
949 ml_append(n
, (char_u
*) line
, (colnr_T
) 0, FALSE
);
951 /* Changes to non-active buffers should properly refresh screen
952 * SegPhault - 12/20/04 */
953 appended_lines_mark(n
, 1L);
958 /* restore curwin/curbuf and a few other things */
960 /* Careful: autocommands may have made "buf" invalid! */
962 update_curbuf(NOT_VALID
);
965 rb_raise(rb_eIndexError
, "line number %ld out of range", n
);
970 static VALUE
window_new(win_T
*win
)
974 return (VALUE
) win
->w_ruby_ref
;
978 VALUE obj
= Data_Wrap_Struct(cVimWindow
, 0, 0, win
);
979 win
->w_ruby_ref
= (void *) obj
;
980 rb_hash_aset(objtbl
, rb_obj_id(obj
), obj
);
985 static win_T
*get_win(VALUE obj
)
989 Data_Get_Struct(obj
, win_T
, win
);
991 rb_raise(eDeletedWindowError
, "attempt to refer to deleted window");
995 static VALUE
window_s_current()
997 return window_new(curwin
);
1001 * Added line manipulation functions
1002 * SegPhault - 03/07/05
1004 static VALUE
line_s_current()
1006 return get_buffer_line(curbuf
, curwin
->w_cursor
.lnum
);
1009 static VALUE
set_current_line(VALUE self UNUSED
, VALUE str
)
1011 return set_buffer_line(curbuf
, curwin
->w_cursor
.lnum
, str
);
1014 static VALUE
current_line_number()
1016 return INT2FIX((int)curwin
->w_cursor
.lnum
);
1021 static VALUE
window_s_count()
1027 for (w
= firstwin
; w
!= NULL
; w
= w
->w_next
)
1035 static VALUE
window_s_aref(VALUE self UNUSED
, VALUE num
)
1038 int n
= NUM2INT(num
);
1040 #ifndef FEAT_WINDOWS
1043 for (w
= firstwin
; w
!= NULL
; w
= w
->w_next
, --n
)
1046 return window_new(w
);
1050 static VALUE
window_buffer(VALUE self
)
1052 win_T
*win
= get_win(self
);
1054 return buffer_new(win
->w_buffer
);
1057 static VALUE
window_height(VALUE self
)
1059 win_T
*win
= get_win(self
);
1061 return INT2NUM(win
->w_height
);
1064 static VALUE
window_set_height(VALUE self
, VALUE height
)
1066 win_T
*win
= get_win(self
);
1067 win_T
*savewin
= curwin
;
1070 win_setheight(NUM2INT(height
));
1075 static VALUE
window_width(VALUE self
)
1077 win_T
*win
= get_win(self
);
1079 return INT2NUM(win
->w_width
);
1082 static VALUE
window_set_width(VALUE self
, VALUE width
)
1084 win_T
*win
= get_win(self
);
1085 win_T
*savewin
= curwin
;
1088 win_setwidth(NUM2INT(width
));
1093 static VALUE
window_cursor(VALUE self
)
1095 win_T
*win
= get_win(self
);
1097 return rb_assoc_new(INT2NUM(win
->w_cursor
.lnum
), INT2NUM(win
->w_cursor
.col
));
1100 static VALUE
window_set_cursor(VALUE self
, VALUE pos
)
1103 win_T
*win
= get_win(self
);
1105 Check_Type(pos
, T_ARRAY
);
1106 if (RARRAY_LEN(pos
) != 2)
1107 rb_raise(rb_eArgError
, "array length must be 2");
1108 lnum
= RARRAY_PTR(pos
)[0];
1109 col
= RARRAY_PTR(pos
)[1];
1110 win
->w_cursor
.lnum
= NUM2LONG(lnum
);
1111 win
->w_cursor
.col
= NUM2UINT(col
);
1112 check_cursor(); /* put cursor on an existing line */
1113 update_screen(NOT_VALID
);
1117 static VALUE
f_p(int argc
, VALUE
*argv
, VALUE self UNUSED
)
1120 VALUE str
= rb_str_new("", 0);
1122 for (i
= 0; i
< argc
; i
++) {
1123 if (i
> 0) rb_str_cat(str
, ", ", 2);
1124 rb_str_concat(str
, rb_inspect(argv
[i
]));
1126 MSG(RSTRING_PTR(str
));
1130 static void ruby_io_init(void)
1132 #ifndef DYNAMIC_RUBY
1133 RUBYEXTERN VALUE rb_stdout
;
1136 rb_stdout
= rb_obj_alloc(rb_cObject
);
1137 rb_define_singleton_method(rb_stdout
, "write", vim_message
, 1);
1138 rb_define_global_function("p", f_p
, -1);
1141 static void ruby_vim_init(void)
1143 objtbl
= rb_hash_new();
1144 rb_global_variable(&objtbl
);
1146 /* The Vim module used to be called "VIM", but "Vim" is better. Make an
1147 * alias "VIM" for backwards compatiblity. */
1148 mVIM
= rb_define_module("Vim");
1149 rb_define_const(rb_cObject
, "VIM", mVIM
);
1150 rb_define_const(mVIM
, "VERSION_MAJOR", INT2NUM(VIM_VERSION_MAJOR
));
1151 rb_define_const(mVIM
, "VERSION_MINOR", INT2NUM(VIM_VERSION_MINOR
));
1152 rb_define_const(mVIM
, "VERSION_BUILD", INT2NUM(VIM_VERSION_BUILD
));
1153 rb_define_const(mVIM
, "VERSION_PATCHLEVEL", INT2NUM(VIM_VERSION_PATCHLEVEL
));
1154 rb_define_const(mVIM
, "VERSION_SHORT", rb_str_new2(VIM_VERSION_SHORT
));
1155 rb_define_const(mVIM
, "VERSION_MEDIUM", rb_str_new2(VIM_VERSION_MEDIUM
));
1156 rb_define_const(mVIM
, "VERSION_LONG", rb_str_new2(VIM_VERSION_LONG
));
1157 rb_define_const(mVIM
, "VERSION_LONG_DATE", rb_str_new2(VIM_VERSION_LONG_DATE
));
1158 rb_define_module_function(mVIM
, "message", vim_message
, 1);
1159 rb_define_module_function(mVIM
, "set_option", vim_set_option
, 1);
1160 rb_define_module_function(mVIM
, "command", vim_command
, 1);
1161 rb_define_module_function(mVIM
, "evaluate", vim_evaluate
, 1);
1163 eDeletedBufferError
= rb_define_class_under(mVIM
, "DeletedBufferError",
1165 eDeletedWindowError
= rb_define_class_under(mVIM
, "DeletedWindowError",
1168 cBuffer
= rb_define_class_under(mVIM
, "Buffer", rb_cObject
);
1169 rb_define_singleton_method(cBuffer
, "current", buffer_s_current
, 0);
1170 rb_define_singleton_method(cBuffer
, "count", buffer_s_count
, 0);
1171 rb_define_singleton_method(cBuffer
, "[]", buffer_s_aref
, 1);
1172 rb_define_method(cBuffer
, "name", buffer_name
, 0);
1173 rb_define_method(cBuffer
, "number", buffer_number
, 0);
1174 rb_define_method(cBuffer
, "count", buffer_count
, 0);
1175 rb_define_method(cBuffer
, "length", buffer_count
, 0);
1176 rb_define_method(cBuffer
, "[]", buffer_aref
, 1);
1177 rb_define_method(cBuffer
, "[]=", buffer_aset
, 2);
1178 rb_define_method(cBuffer
, "delete", buffer_delete
, 1);
1179 rb_define_method(cBuffer
, "append", buffer_append
, 2);
1181 /* Added line manipulation functions
1182 * SegPhault - 03/07/05 */
1183 rb_define_method(cBuffer
, "line_number", current_line_number
, 0);
1184 rb_define_method(cBuffer
, "line", line_s_current
, 0);
1185 rb_define_method(cBuffer
, "line=", set_current_line
, 1);
1188 cVimWindow
= rb_define_class_under(mVIM
, "Window", rb_cObject
);
1189 rb_define_singleton_method(cVimWindow
, "current", window_s_current
, 0);
1190 rb_define_singleton_method(cVimWindow
, "count", window_s_count
, 0);
1191 rb_define_singleton_method(cVimWindow
, "[]", window_s_aref
, 1);
1192 rb_define_method(cVimWindow
, "buffer", window_buffer
, 0);
1193 rb_define_method(cVimWindow
, "height", window_height
, 0);
1194 rb_define_method(cVimWindow
, "height=", window_set_height
, 1);
1195 rb_define_method(cVimWindow
, "width", window_width
, 0);
1196 rb_define_method(cVimWindow
, "width=", window_set_width
, 1);
1197 rb_define_method(cVimWindow
, "cursor", window_cursor
, 0);
1198 rb_define_method(cVimWindow
, "cursor=", window_set_cursor
, 1);
1200 rb_define_virtual_variable("$curbuf", buffer_s_current
, 0);
1201 rb_define_virtual_variable("$curwin", window_s_current
, 0);