blob: 397b77284aac165dd99bb69e1dd1bda9e7b461a1 [file] [log] [blame]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001/* vi:set ts=8 sts=4 sw=4:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004 *
5 * Ruby interface by Shugo Maeda
6 * with improvements by SegPhault (Ryan Paul)
Bram Moolenaarf9b5ef82010-09-29 13:02:53 +02007 * with improvements by Jon Maken
Bram Moolenaar071d4272004-06-13 20:20:40 +00008 *
9 * Do ":help uganda" in Vim to read copying and usage conditions.
10 * Do ":help credits" in Vim to see a list of people who contributed.
11 * See README.txt for an overview of the Vim source code.
12 */
13
14#include <stdio.h>
15#include <string.h>
16
Bram Moolenaar2d73ff42010-10-27 17:40:59 +020017#ifdef HAVE_CONFIG_H
18# include "auto/config.h"
19#endif
Bram Moolenaar3ca71f12010-10-27 16:49:47 +020020
Bram Moolenaar071d4272004-06-13 20:20:40 +000021#ifdef _WIN32
22# if !defined(DYNAMIC_RUBY_VER) || (DYNAMIC_RUBY_VER < 18)
23# define NT
24# endif
25# ifndef DYNAMIC_RUBY
26# define IMPORT /* For static dll usage __declspec(dllimport) */
27# define RUBYEXTERN __declspec(dllimport)
28# endif
29#endif
30#ifndef RUBYEXTERN
31# define RUBYEXTERN extern
32#endif
33
Bram Moolenaarf9b5ef82010-09-29 13:02:53 +020034#ifdef DYNAMIC_RUBY
Bram Moolenaar071d4272004-06-13 20:20:40 +000035/*
36 * This is tricky. In ruby.h there is (inline) function rb_class_of()
37 * definition. This function use these variables. But we want function to
38 * use dll_* variables.
39 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000040# define rb_cFalseClass (*dll_rb_cFalseClass)
41# define rb_cFixnum (*dll_rb_cFixnum)
42# define rb_cNilClass (*dll_rb_cNilClass)
43# define rb_cSymbol (*dll_rb_cSymbol)
44# define rb_cTrueClass (*dll_rb_cTrueClass)
45# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 18
46/*
Bram Moolenaar42d57f02010-03-10 12:47:00 +010047 * On ver 1.8, all Ruby functions are exported with "__declspec(dllimport)"
48 * in ruby.h. But it causes trouble for these variables, because it is
Bram Moolenaar071d4272004-06-13 20:20:40 +000049 * defined in this file. When defined this RUBY_EXPORT it modified to
50 * "extern" and be able to avoid this problem.
51 */
52# define RUBY_EXPORT
53# endif
Bram Moolenaarf9b5ef82010-09-29 13:02:53 +020054
Bram Moolenaar2d73ff42010-10-27 17:40:59 +020055#if !(defined(WIN32) || defined(_WIN64))
Bram Moolenaarf9b5ef82010-09-29 13:02:53 +020056# include <dlfcn.h>
Bram Moolenaar3ca71f12010-10-27 16:49:47 +020057# define HINSTANCE void*
58# define RUBY_PROC void*
Bram Moolenaarf9b5ef82010-09-29 13:02:53 +020059# define load_dll(n) dlopen((n), RTLD_LAZY|RTLD_GLOBAL)
60# define symbol_from_dll dlsym
61# define close_dll dlclose
62#else
Bram Moolenaar3ca71f12010-10-27 16:49:47 +020063# define RUBY_PROC FARPROC
Bram Moolenaarebbcb822010-10-23 14:02:54 +020064# define load_dll vimLoadLib
Bram Moolenaarf9b5ef82010-09-29 13:02:53 +020065# define symbol_from_dll GetProcAddress
66# define close_dll FreeLibrary
Bram Moolenaar071d4272004-06-13 20:20:40 +000067#endif
68
Bram Moolenaarf9b5ef82010-09-29 13:02:53 +020069#endif /* ifdef DYNAMIC_RUBY */
70
Bram Moolenaar0b69c732010-02-17 15:11:50 +010071/* suggested by Ariya Mizutani */
72#if (_MSC_VER == 1200)
73# undef _WIN32_WINNT
74#endif
75
Bram Moolenaar639a2552010-03-17 18:15:23 +010076#if (defined(RUBY_VERSION) && RUBY_VERSION >= 19) \
77 || (defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 19)
78# define RUBY19_OR_LATER 1
79#endif
80
Bram Moolenaar42d57f02010-03-10 12:47:00 +010081#if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 19
82/* Ruby 1.9 defines a number of static functions which use rb_num2long and
83 * rb_int2big */
84# define rb_num2long rb_num2long_stub
85# define rb_int2big rb_int2big_stub
86#endif
87
Bram Moolenaar071d4272004-06-13 20:20:40 +000088#include <ruby.h>
Bram Moolenaar639a2552010-03-17 18:15:23 +010089#ifdef RUBY19_OR_LATER
Bram Moolenaar165641d2010-02-17 16:23:09 +010090# include <ruby/encoding.h>
91#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000092
93#undef EXTERN
94#undef _
95
96/* T_DATA defined both by Ruby and Mac header files, hack around it... */
Bram Moolenaarcb635362007-05-12 13:02:42 +000097#if defined(MACOS_X_UNIX) || defined(macintosh)
Bram Moolenaar071d4272004-06-13 20:20:40 +000098# define __OPENTRANSPORT__
99# define __OPENTRANSPORTPROTOCOL__
100# define __OPENTRANSPORTPROVIDERS__
101#endif
102
Bram Moolenaar165641d2010-02-17 16:23:09 +0100103/*
104 * Backward compatiblity for Ruby 1.8 and earlier.
105 * Ruby 1.9 does not provide STR2CSTR, instead StringValuePtr is provided.
106 * Ruby 1.9 does not provide RXXX(s)->len and RXXX(s)->ptr, instead
107 * RXXX_LEN(s) and RXXX_PTR(s) are provided.
108 */
109#ifndef StringValuePtr
110# define StringValuePtr(s) STR2CSTR(s)
111#endif
112#ifndef RARRAY_LEN
113# define RARRAY_LEN(s) RARRAY(s)->len
114#endif
115#ifndef RARRAY_PTR
116# define RARRAY_PTR(s) RARRAY(s)->ptr
117#endif
118#ifndef RSTRING_LEN
119# define RSTRING_LEN(s) RSTRING(s)->len
120#endif
121#ifndef RSTRING_PTR
122# define RSTRING_PTR(s) RSTRING(s)->ptr
123#endif
124
Bram Moolenaar071d4272004-06-13 20:20:40 +0000125#include "vim.h"
126#include "version.h"
127
128#if defined(PROTO) && !defined(FEAT_RUBY)
129/* Define these to be able to generate the function prototypes. */
130# define VALUE int
131# define RUBY_DATA_FUNC int
132#endif
133
134static int ruby_initialized = 0;
135static VALUE objtbl;
136
137static VALUE mVIM;
138static VALUE cBuffer;
139static VALUE cVimWindow;
140static VALUE eDeletedBufferError;
141static VALUE eDeletedWindowError;
142
143static int ensure_ruby_initialized(void);
144static void error_print(int);
145static void ruby_io_init(void);
146static void ruby_vim_init(void);
147
148#if defined(DYNAMIC_RUBY) || defined(PROTO)
149#ifdef PROTO
150# define HINSTANCE int /* for generating prototypes */
151#endif
152
153/*
154 * Wrapper defines
155 */
156#define rb_assoc_new dll_rb_assoc_new
157#define rb_cObject (*dll_rb_cObject)
158#define rb_check_type dll_rb_check_type
159#define rb_class_path dll_rb_class_path
160#define rb_data_object_alloc dll_rb_data_object_alloc
161#define rb_define_class_under dll_rb_define_class_under
162#define rb_define_const dll_rb_define_const
163#define rb_define_global_function dll_rb_define_global_function
164#define rb_define_method dll_rb_define_method
165#define rb_define_module dll_rb_define_module
166#define rb_define_module_function dll_rb_define_module_function
167#define rb_define_singleton_method dll_rb_define_singleton_method
168#define rb_define_virtual_variable dll_rb_define_virtual_variable
169#define rb_stdout (*dll_rb_stdout)
170#define rb_eArgError (*dll_rb_eArgError)
171#define rb_eIndexError (*dll_rb_eIndexError)
172#define rb_eRuntimeError (*dll_rb_eRuntimeError)
173#define rb_eStandardError (*dll_rb_eStandardError)
174#define rb_eval_string_protect dll_rb_eval_string_protect
175#define rb_global_variable dll_rb_global_variable
176#define rb_hash_aset dll_rb_hash_aset
177#define rb_hash_new dll_rb_hash_new
178#define rb_inspect dll_rb_inspect
179#define rb_int2inum dll_rb_int2inum
180#define rb_lastline_get dll_rb_lastline_get
181#define rb_lastline_set dll_rb_lastline_set
182#define rb_load_protect dll_rb_load_protect
Bram Moolenaar3ca71f12010-10-27 16:49:47 +0200183#ifndef RUBY19_OR_LATER
Bram Moolenaar071d4272004-06-13 20:20:40 +0000184#define rb_num2long dll_rb_num2long
Bram Moolenaar3ca71f12010-10-27 16:49:47 +0200185#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000186#define rb_num2ulong dll_rb_num2ulong
187#define rb_obj_alloc dll_rb_obj_alloc
188#define rb_obj_as_string dll_rb_obj_as_string
189#define rb_obj_id dll_rb_obj_id
190#define rb_raise dll_rb_raise
Bram Moolenaar071d4272004-06-13 20:20:40 +0000191#define rb_str_cat dll_rb_str_cat
192#define rb_str_concat dll_rb_str_concat
193#define rb_str_new dll_rb_str_new
Bram Moolenaar1d2beae2010-05-22 21:56:55 +0200194#ifdef rb_str_new2
195/* Ruby may #define rb_str_new2 to use rb_str_new_cstr. */
196# define need_rb_str_new_cstr 1
Bram Moolenaar3ca71f12010-10-27 16:49:47 +0200197/* Ruby's headers #define rb_str_new_cstr to make use of GCC's
198 * __builtin_constant_p extension. */
199# undef rb_str_new_cstr
Bram Moolenaar1d2beae2010-05-22 21:56:55 +0200200# define rb_str_new_cstr dll_rb_str_new_cstr
201#else
Bram Moolenaar218116c2010-05-20 21:46:00 +0200202# define rb_str_new2 dll_rb_str_new2
203#endif
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100204#if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 18
Bram Moolenaarf9b5ef82010-09-29 13:02:53 +0200205# define rb_string_value dll_rb_string_value
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100206# define rb_string_value_ptr dll_rb_string_value_ptr
207# define rb_float_new dll_rb_float_new
208# define rb_ary_new dll_rb_ary_new
209# define rb_ary_push dll_rb_ary_push
Bram Moolenaarf9b5ef82010-09-29 13:02:53 +0200210#else
211# define rb_str2cstr dll_rb_str2cstr
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100212#endif
Bram Moolenaar639a2552010-03-17 18:15:23 +0100213#ifdef RUBY19_OR_LATER
Bram Moolenaar165641d2010-02-17 16:23:09 +0100214# define rb_errinfo dll_rb_errinfo
215#else
216# define ruby_errinfo (*dll_ruby_errinfo)
217#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000218#define ruby_init dll_ruby_init
219#define ruby_init_loadpath dll_ruby_init_loadpath
Bram Moolenaar3ca71f12010-10-27 16:49:47 +0200220#ifdef WIN3264
221# define NtInitialize dll_NtInitialize
222# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 18
223# define rb_w32_snprintf dll_rb_w32_snprintf
224# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000225#endif
226
Bram Moolenaar639a2552010-03-17 18:15:23 +0100227#ifdef RUBY19_OR_LATER
Bram Moolenaar165641d2010-02-17 16:23:09 +0100228# define ruby_script dll_ruby_script
229# define rb_enc_find_index dll_rb_enc_find_index
230# define rb_enc_find dll_rb_enc_find
231# define rb_enc_str_new dll_rb_enc_str_new
232# define rb_sprintf dll_rb_sprintf
Bram Moolenaar639a2552010-03-17 18:15:23 +0100233# define ruby_init_stack dll_ruby_init_stack
Bram Moolenaar165641d2010-02-17 16:23:09 +0100234#endif
235
Bram Moolenaar071d4272004-06-13 20:20:40 +0000236/*
237 * Pointers for dynamic link
238 */
239static VALUE (*dll_rb_assoc_new) (VALUE, VALUE);
Bram Moolenaarba52cde2010-06-25 04:29:11 +0200240VALUE *dll_rb_cFalseClass;
241VALUE *dll_rb_cFixnum;
242VALUE *dll_rb_cNilClass;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000243static VALUE *dll_rb_cObject;
Bram Moolenaarba52cde2010-06-25 04:29:11 +0200244VALUE *dll_rb_cSymbol;
245VALUE *dll_rb_cTrueClass;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000246static void (*dll_rb_check_type) (VALUE,int);
247static VALUE (*dll_rb_class_path) (VALUE);
248static VALUE (*dll_rb_data_object_alloc) (VALUE, void*, RUBY_DATA_FUNC, RUBY_DATA_FUNC);
249static VALUE (*dll_rb_define_class_under) (VALUE, const char*, VALUE);
250static void (*dll_rb_define_const) (VALUE,const char*,VALUE);
251static void (*dll_rb_define_global_function) (const char*,VALUE(*)(),int);
252static void (*dll_rb_define_method) (VALUE,const char*,VALUE(*)(),int);
253static VALUE (*dll_rb_define_module) (const char*);
254static void (*dll_rb_define_module_function) (VALUE,const char*,VALUE(*)(),int);
255static void (*dll_rb_define_singleton_method) (VALUE,const char*,VALUE(*)(),int);
256static void (*dll_rb_define_virtual_variable) (const char*,VALUE(*)(),void(*)());
257static VALUE *dll_rb_stdout;
258static VALUE *dll_rb_eArgError;
259static VALUE *dll_rb_eIndexError;
260static VALUE *dll_rb_eRuntimeError;
261static VALUE *dll_rb_eStandardError;
262static VALUE (*dll_rb_eval_string_protect) (const char*, int*);
263static void (*dll_rb_global_variable) (VALUE*);
264static VALUE (*dll_rb_hash_aset) (VALUE, VALUE, VALUE);
265static VALUE (*dll_rb_hash_new) (void);
266static VALUE (*dll_rb_inspect) (VALUE);
267static VALUE (*dll_rb_int2inum) (long);
268static VALUE (*dll_rb_int2inum) (long);
269static VALUE (*dll_rb_lastline_get) (void);
270static void (*dll_rb_lastline_set) (VALUE);
271static void (*dll_rb_load_protect) (VALUE, int, int*);
272static long (*dll_rb_num2long) (VALUE);
273static unsigned long (*dll_rb_num2ulong) (VALUE);
274static VALUE (*dll_rb_obj_alloc) (VALUE);
275static VALUE (*dll_rb_obj_as_string) (VALUE);
276static VALUE (*dll_rb_obj_id) (VALUE);
277static void (*dll_rb_raise) (VALUE, const char*, ...);
Bram Moolenaarf9b5ef82010-09-29 13:02:53 +0200278#if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 18
279static VALUE (*dll_rb_string_value) (volatile VALUE*);
280#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000281static char *(*dll_rb_str2cstr) (VALUE,int*);
Bram Moolenaarf9b5ef82010-09-29 13:02:53 +0200282#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000283static VALUE (*dll_rb_str_cat) (VALUE, const char*, long);
284static VALUE (*dll_rb_str_concat) (VALUE, VALUE);
285static VALUE (*dll_rb_str_new) (const char*, long);
Bram Moolenaar1d2beae2010-05-22 21:56:55 +0200286#ifdef need_rb_str_new_cstr
287/* Ruby may #define rb_str_new2 to use rb_str_new_cstr. */
288static VALUE (*dll_rb_str_new_cstr) (const char*);
289#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000290static VALUE (*dll_rb_str_new2) (const char*);
Bram Moolenaar1d2beae2010-05-22 21:56:55 +0200291#endif
Bram Moolenaar639a2552010-03-17 18:15:23 +0100292#ifdef RUBY19_OR_LATER
Bram Moolenaar165641d2010-02-17 16:23:09 +0100293static VALUE (*dll_rb_errinfo) (void);
294#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000295static VALUE *dll_ruby_errinfo;
Bram Moolenaar165641d2010-02-17 16:23:09 +0100296#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000297static void (*dll_ruby_init) (void);
298static void (*dll_ruby_init_loadpath) (void);
Bram Moolenaar3ca71f12010-10-27 16:49:47 +0200299#ifdef WIN3264
Bram Moolenaar0b69c732010-02-17 15:11:50 +0100300static void (*dll_NtInitialize) (int*, char***);
Bram Moolenaar3ca71f12010-10-27 16:49:47 +0200301# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 18
302static int (*dll_rb_w32_snprintf)(char*, size_t, const char*, ...);
303# endif
304#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000305#if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 18
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100306static char * (*dll_rb_string_value_ptr) (volatile VALUE*);
307static VALUE (*dll_rb_float_new) (double);
308static VALUE (*dll_rb_ary_new) (void);
309static VALUE (*dll_rb_ary_push) (VALUE, VALUE);
310#endif
Bram Moolenaar639a2552010-03-17 18:15:23 +0100311#ifdef RUBY19_OR_LATER
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100312static VALUE (*dll_rb_int2big)(SIGNED_VALUE);
313#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000314
Bram Moolenaar639a2552010-03-17 18:15:23 +0100315#ifdef RUBY19_OR_LATER
Bram Moolenaar165641d2010-02-17 16:23:09 +0100316static void (*dll_ruby_script) (const char*);
317static int (*dll_rb_enc_find_index) (const char*);
318static rb_encoding* (*dll_rb_enc_find) (const char*);
319static VALUE (*dll_rb_enc_str_new) (const char*, long, rb_encoding*);
320static VALUE (*dll_rb_sprintf) (const char*, ...);
Bram Moolenaar639a2552010-03-17 18:15:23 +0100321static void (*ruby_init_stack)(VALUE*);
Bram Moolenaar165641d2010-02-17 16:23:09 +0100322#endif
323
Bram Moolenaar639a2552010-03-17 18:15:23 +0100324#ifdef RUBY19_OR_LATER
Bram Moolenaarba52cde2010-06-25 04:29:11 +0200325SIGNED_VALUE rb_num2long_stub(VALUE x)
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100326{
327 return dll_rb_num2long(x);
328}
Bram Moolenaarba52cde2010-06-25 04:29:11 +0200329VALUE rb_int2big_stub(SIGNED_VALUE x)
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100330{
331 return dll_rb_int2big(x);
332}
333#endif
334
Bram Moolenaar3ca71f12010-10-27 16:49:47 +0200335static HINSTANCE hinstRuby = NULL; /* Instance of ruby.dll */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000336
337/*
Bram Moolenaarda2303d2005-08-30 21:55:26 +0000338 * Table of name to function pointer of ruby.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000339 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000340static struct
341{
342 char *name;
343 RUBY_PROC *ptr;
344} ruby_funcname_table[] =
345{
346 {"rb_assoc_new", (RUBY_PROC*)&dll_rb_assoc_new},
347 {"rb_cFalseClass", (RUBY_PROC*)&dll_rb_cFalseClass},
348 {"rb_cFixnum", (RUBY_PROC*)&dll_rb_cFixnum},
349 {"rb_cNilClass", (RUBY_PROC*)&dll_rb_cNilClass},
350 {"rb_cObject", (RUBY_PROC*)&dll_rb_cObject},
351 {"rb_cSymbol", (RUBY_PROC*)&dll_rb_cSymbol},
352 {"rb_cTrueClass", (RUBY_PROC*)&dll_rb_cTrueClass},
353 {"rb_check_type", (RUBY_PROC*)&dll_rb_check_type},
354 {"rb_class_path", (RUBY_PROC*)&dll_rb_class_path},
355 {"rb_data_object_alloc", (RUBY_PROC*)&dll_rb_data_object_alloc},
356 {"rb_define_class_under", (RUBY_PROC*)&dll_rb_define_class_under},
357 {"rb_define_const", (RUBY_PROC*)&dll_rb_define_const},
358 {"rb_define_global_function", (RUBY_PROC*)&dll_rb_define_global_function},
359 {"rb_define_method", (RUBY_PROC*)&dll_rb_define_method},
360 {"rb_define_module", (RUBY_PROC*)&dll_rb_define_module},
361 {"rb_define_module_function", (RUBY_PROC*)&dll_rb_define_module_function},
362 {"rb_define_singleton_method", (RUBY_PROC*)&dll_rb_define_singleton_method},
363 {"rb_define_virtual_variable", (RUBY_PROC*)&dll_rb_define_virtual_variable},
364 {"rb_stdout", (RUBY_PROC*)&dll_rb_stdout},
365 {"rb_eArgError", (RUBY_PROC*)&dll_rb_eArgError},
366 {"rb_eIndexError", (RUBY_PROC*)&dll_rb_eIndexError},
367 {"rb_eRuntimeError", (RUBY_PROC*)&dll_rb_eRuntimeError},
368 {"rb_eStandardError", (RUBY_PROC*)&dll_rb_eStandardError},
369 {"rb_eval_string_protect", (RUBY_PROC*)&dll_rb_eval_string_protect},
370 {"rb_global_variable", (RUBY_PROC*)&dll_rb_global_variable},
371 {"rb_hash_aset", (RUBY_PROC*)&dll_rb_hash_aset},
372 {"rb_hash_new", (RUBY_PROC*)&dll_rb_hash_new},
373 {"rb_inspect", (RUBY_PROC*)&dll_rb_inspect},
374 {"rb_int2inum", (RUBY_PROC*)&dll_rb_int2inum},
375 {"rb_lastline_get", (RUBY_PROC*)&dll_rb_lastline_get},
376 {"rb_lastline_set", (RUBY_PROC*)&dll_rb_lastline_set},
377 {"rb_load_protect", (RUBY_PROC*)&dll_rb_load_protect},
378 {"rb_num2long", (RUBY_PROC*)&dll_rb_num2long},
379 {"rb_num2ulong", (RUBY_PROC*)&dll_rb_num2ulong},
380 {"rb_obj_alloc", (RUBY_PROC*)&dll_rb_obj_alloc},
381 {"rb_obj_as_string", (RUBY_PROC*)&dll_rb_obj_as_string},
382 {"rb_obj_id", (RUBY_PROC*)&dll_rb_obj_id},
383 {"rb_raise", (RUBY_PROC*)&dll_rb_raise},
Bram Moolenaarf9b5ef82010-09-29 13:02:53 +0200384#if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 18
385 {"rb_string_value", (RUBY_PROC*)&dll_rb_string_value},
386#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000387 {"rb_str2cstr", (RUBY_PROC*)&dll_rb_str2cstr},
Bram Moolenaarf9b5ef82010-09-29 13:02:53 +0200388#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000389 {"rb_str_cat", (RUBY_PROC*)&dll_rb_str_cat},
390 {"rb_str_concat", (RUBY_PROC*)&dll_rb_str_concat},
391 {"rb_str_new", (RUBY_PROC*)&dll_rb_str_new},
Bram Moolenaar1d2beae2010-05-22 21:56:55 +0200392#ifdef need_rb_str_new_cstr
393 {"rb_str_new_cstr", (RUBY_PROC*)&dll_rb_str_new_cstr},
394#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000395 {"rb_str_new2", (RUBY_PROC*)&dll_rb_str_new2},
Bram Moolenaar1d2beae2010-05-22 21:56:55 +0200396#endif
Bram Moolenaar639a2552010-03-17 18:15:23 +0100397#ifdef RUBY19_OR_LATER
Bram Moolenaar165641d2010-02-17 16:23:09 +0100398 {"rb_errinfo", (RUBY_PROC*)&dll_rb_errinfo},
399#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000400 {"ruby_errinfo", (RUBY_PROC*)&dll_ruby_errinfo},
Bram Moolenaar165641d2010-02-17 16:23:09 +0100401#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000402 {"ruby_init", (RUBY_PROC*)&dll_ruby_init},
403 {"ruby_init_loadpath", (RUBY_PROC*)&dll_ruby_init_loadpath},
Bram Moolenaar3ca71f12010-10-27 16:49:47 +0200404#ifdef WIN3264
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100405 {
Bram Moolenaar3ca71f12010-10-27 16:49:47 +0200406# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER < 19
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100407 "NtInitialize",
Bram Moolenaar3ca71f12010-10-27 16:49:47 +0200408# else
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100409 "ruby_sysinit",
Bram Moolenaar3ca71f12010-10-27 16:49:47 +0200410# endif
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100411 (RUBY_PROC*)&dll_NtInitialize},
Bram Moolenaar3ca71f12010-10-27 16:49:47 +0200412# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 18
Bram Moolenaar071d4272004-06-13 20:20:40 +0000413 {"rb_w32_snprintf", (RUBY_PROC*)&dll_rb_w32_snprintf},
Bram Moolenaar3ca71f12010-10-27 16:49:47 +0200414# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000415#endif
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100416#if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 18
417 {"rb_string_value_ptr", (RUBY_PROC*)&dll_rb_string_value_ptr},
418 {"rb_float_new", (RUBY_PROC*)&dll_rb_float_new},
419 {"rb_ary_new", (RUBY_PROC*)&dll_rb_ary_new},
420 {"rb_ary_push", (RUBY_PROC*)&dll_rb_ary_push},
421#endif
Bram Moolenaar639a2552010-03-17 18:15:23 +0100422#ifdef RUBY19_OR_LATER
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100423 {"rb_int2big", (RUBY_PROC*)&dll_rb_int2big},
Bram Moolenaar165641d2010-02-17 16:23:09 +0100424 {"ruby_script", (RUBY_PROC*)&dll_ruby_script},
425 {"rb_enc_find_index", (RUBY_PROC*)&dll_rb_enc_find_index},
426 {"rb_enc_find", (RUBY_PROC*)&dll_rb_enc_find},
427 {"rb_enc_str_new", (RUBY_PROC*)&dll_rb_enc_str_new},
428 {"rb_sprintf", (RUBY_PROC*)&dll_rb_sprintf},
Bram Moolenaar639a2552010-03-17 18:15:23 +0100429 {"ruby_init_stack", (RUBY_PROC*)&dll_ruby_init_stack},
Bram Moolenaar165641d2010-02-17 16:23:09 +0100430#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000431 {"", NULL},
432};
433
434/*
435 * Free ruby.dll
436 */
437 static void
438end_dynamic_ruby()
439{
440 if (hinstRuby)
441 {
Bram Moolenaarf9b5ef82010-09-29 13:02:53 +0200442 close_dll(hinstRuby);
Bram Moolenaar3ca71f12010-10-27 16:49:47 +0200443 hinstRuby = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000444 }
445}
446
447/*
448 * Load library and get all pointers.
449 * Parameter 'libname' provides name of DLL.
450 * Return OK or FAIL.
451 */
452 static int
453ruby_runtime_link_init(char *libname, int verbose)
454{
455 int i;
456
457 if (hinstRuby)
458 return OK;
Bram Moolenaarf9b5ef82010-09-29 13:02:53 +0200459 hinstRuby = load_dll(libname);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000460 if (!hinstRuby)
461 {
462 if (verbose)
463 EMSG2(_(e_loadlib), libname);
464 return FAIL;
465 }
466
467 for (i = 0; ruby_funcname_table[i].ptr; ++i)
468 {
Bram Moolenaarf9b5ef82010-09-29 13:02:53 +0200469 if (!(*ruby_funcname_table[i].ptr = symbol_from_dll(hinstRuby,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000470 ruby_funcname_table[i].name)))
471 {
Bram Moolenaarf9b5ef82010-09-29 13:02:53 +0200472 close_dll(hinstRuby);
Bram Moolenaar3ca71f12010-10-27 16:49:47 +0200473 hinstRuby = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000474 if (verbose)
475 EMSG2(_(e_loadfunc), ruby_funcname_table[i].name);
476 return FAIL;
477 }
478 }
479 return OK;
480}
481
482/*
483 * If ruby is enabled (there is installed ruby on Windows system) return TRUE,
484 * else FALSE.
485 */
486 int
487ruby_enabled(verbose)
488 int verbose;
489{
490 return ruby_runtime_link_init(DYNAMIC_RUBY_DLL, verbose) == OK;
491}
492#endif /* defined(DYNAMIC_RUBY) || defined(PROTO) */
493
494 void
495ruby_end()
496{
497#ifdef DYNAMIC_RUBY
498 end_dynamic_ruby();
499#endif
500}
501
502void ex_ruby(exarg_T *eap)
503{
504 int state;
505 char *script = NULL;
506
Bram Moolenaar35a2e192006-03-13 22:07:11 +0000507 script = (char *)script_get(eap, eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000508 if (!eap->skip && ensure_ruby_initialized())
509 {
510 if (script == NULL)
511 rb_eval_string_protect((char *)eap->arg, &state);
512 else
513 rb_eval_string_protect(script, &state);
514 if (state)
515 error_print(state);
516 }
517 vim_free(script);
518}
519
Bram Moolenaar165641d2010-02-17 16:23:09 +0100520/*
521 * In Ruby 1.9 or later, ruby String object has encoding.
522 * conversion buffer string of vim to ruby String object using
523 * VIM encoding option.
524 */
525 static VALUE
526vim_str2rb_enc_str(const char *s)
527{
Bram Moolenaar639a2552010-03-17 18:15:23 +0100528#ifdef RUBY19_OR_LATER
Bram Moolenaar165641d2010-02-17 16:23:09 +0100529 int isnum;
530 long lval;
531 char_u *sval;
532 rb_encoding *enc;
533
534 isnum = get_option_value((char_u *)"enc", &lval, &sval, 0);
535 if (isnum == 0)
536 {
537 enc = rb_enc_find((char *)sval);
538 vim_free(sval);
539 if (enc) {
540 return rb_enc_str_new(s, strlen(s), enc);
541 }
542 }
543#endif
544 return rb_str_new2(s);
545}
546
547 static VALUE
548eval_enc_string_protect(const char *str, int *state)
549{
Bram Moolenaar639a2552010-03-17 18:15:23 +0100550#ifdef RUBY19_OR_LATER
Bram Moolenaar165641d2010-02-17 16:23:09 +0100551 int isnum;
552 long lval;
553 char_u *sval;
554 rb_encoding *enc;
555 VALUE v;
556
557 isnum = get_option_value((char_u *)"enc", &lval, &sval, 0);
558 if (isnum == 0)
559 {
560 enc = rb_enc_find((char *)sval);
561 vim_free(sval);
562 if (enc)
563 {
564 v = rb_sprintf("#-*- coding:%s -*-\n%s", rb_enc_name(enc), str);
565 return rb_eval_string_protect(StringValuePtr(v), state);
566 }
567 }
568#endif
569 return rb_eval_string_protect(str, state);
570}
571
Bram Moolenaar071d4272004-06-13 20:20:40 +0000572void ex_rubydo(exarg_T *eap)
573{
574 int state;
575 linenr_T i;
576
577 if (ensure_ruby_initialized())
578 {
579 if (u_save(eap->line1 - 1, eap->line2 + 1) != OK)
580 return;
581 for (i = eap->line1; i <= eap->line2; i++) {
582 VALUE line, oldline;
583
Bram Moolenaar165641d2010-02-17 16:23:09 +0100584 line = oldline = vim_str2rb_enc_str((char *)ml_get(i));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000585 rb_lastline_set(line);
Bram Moolenaar165641d2010-02-17 16:23:09 +0100586 eval_enc_string_protect((char *) eap->arg, &state);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000587 if (state) {
588 error_print(state);
589 break;
590 }
591 line = rb_lastline_get();
592 if (!NIL_P(line)) {
593 if (TYPE(line) != T_STRING) {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000594 EMSG(_("E265: $_ must be an instance of String"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000595 return;
596 }
Bram Moolenaar165641d2010-02-17 16:23:09 +0100597 ml_replace(i, (char_u *) StringValuePtr(line), 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000598 changed();
599#ifdef SYNTAX_HL
600 syn_changed(i); /* recompute syntax hl. for this line */
601#endif
602 }
603 }
604 check_cursor();
605 update_curbuf(NOT_VALID);
606 }
607}
608
609void ex_rubyfile(exarg_T *eap)
610{
611 int state;
612
613 if (ensure_ruby_initialized())
614 {
615 rb_load_protect(rb_str_new2((char *) eap->arg), 0, &state);
616 if (state) error_print(state);
617 }
618}
619
620void ruby_buffer_free(buf_T *buf)
621{
Bram Moolenaare344bea2005-09-01 20:46:49 +0000622 if (buf->b_ruby_ref)
623 {
624 rb_hash_aset(objtbl, rb_obj_id((VALUE) buf->b_ruby_ref), Qnil);
625 RDATA(buf->b_ruby_ref)->data = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000626 }
627}
628
629void ruby_window_free(win_T *win)
630{
Bram Moolenaare344bea2005-09-01 20:46:49 +0000631 if (win->w_ruby_ref)
632 {
633 rb_hash_aset(objtbl, rb_obj_id((VALUE) win->w_ruby_ref), Qnil);
634 RDATA(win->w_ruby_ref)->data = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000635 }
636}
637
638static int ensure_ruby_initialized(void)
639{
640 if (!ruby_initialized)
641 {
642#ifdef DYNAMIC_RUBY
643 if (ruby_enabled(TRUE))
644 {
645#endif
Bram Moolenaar0b69c732010-02-17 15:11:50 +0100646#ifdef _WIN32
647 /* suggested by Ariya Mizutani */
648 int argc = 1;
649 char *argv[] = {"gvim.exe"};
650 NtInitialize(&argc, &argv);
651#endif
Bram Moolenaarb2c03502010-07-02 20:20:09 +0200652 {
Bram Moolenaar639a2552010-03-17 18:15:23 +0100653#ifdef RUBY19_OR_LATER
Bram Moolenaarb2c03502010-07-02 20:20:09 +0200654 RUBY_INIT_STACK;
Bram Moolenaar165641d2010-02-17 16:23:09 +0100655#endif
Bram Moolenaarb2c03502010-07-02 20:20:09 +0200656 ruby_init();
657 }
Bram Moolenaar639a2552010-03-17 18:15:23 +0100658#ifdef RUBY19_OR_LATER
Bram Moolenaar165641d2010-02-17 16:23:09 +0100659 ruby_script("vim-ruby");
660#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000661 ruby_init_loadpath();
662 ruby_io_init();
Bram Moolenaar639a2552010-03-17 18:15:23 +0100663#ifdef RUBY19_OR_LATER
Bram Moolenaar165641d2010-02-17 16:23:09 +0100664 rb_enc_find_index("encdb");
665#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000666 ruby_vim_init();
667 ruby_initialized = 1;
668#ifdef DYNAMIC_RUBY
669 }
670 else
671 {
672 EMSG(_("E266: Sorry, this command is disabled, the Ruby library could not be loaded."));
673 return 0;
674 }
675#endif
676 }
677 return ruby_initialized;
678}
679
680static void error_print(int state)
681{
682#ifndef DYNAMIC_RUBY
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100683#if !(defined(RUBY_VERSION) && RUBY_VERSION >= 19) \
684 && !(defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 19)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000685 RUBYEXTERN VALUE ruby_errinfo;
686#endif
Bram Moolenaar165641d2010-02-17 16:23:09 +0100687#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000688 VALUE eclass;
689 VALUE einfo;
690 char buff[BUFSIZ];
691
692#define TAG_RETURN 0x1
693#define TAG_BREAK 0x2
694#define TAG_NEXT 0x3
695#define TAG_RETRY 0x4
696#define TAG_REDO 0x5
697#define TAG_RAISE 0x6
698#define TAG_THROW 0x7
699#define TAG_FATAL 0x8
700#define TAG_MASK 0xf
701
702 switch (state) {
703 case TAG_RETURN:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000704 EMSG(_("E267: unexpected return"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000705 break;
706 case TAG_NEXT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000707 EMSG(_("E268: unexpected next"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000708 break;
709 case TAG_BREAK:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000710 EMSG(_("E269: unexpected break"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000711 break;
712 case TAG_REDO:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000713 EMSG(_("E270: unexpected redo"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000714 break;
715 case TAG_RETRY:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000716 EMSG(_("E271: retry outside of rescue clause"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000717 break;
718 case TAG_RAISE:
719 case TAG_FATAL:
Bram Moolenaar639a2552010-03-17 18:15:23 +0100720#ifdef RUBY19_OR_LATER
Bram Moolenaar165641d2010-02-17 16:23:09 +0100721 eclass = CLASS_OF(rb_errinfo());
722 einfo = rb_obj_as_string(rb_errinfo());
723#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000724 eclass = CLASS_OF(ruby_errinfo);
725 einfo = rb_obj_as_string(ruby_errinfo);
Bram Moolenaar165641d2010-02-17 16:23:09 +0100726#endif
727 if (eclass == rb_eRuntimeError && RSTRING_LEN(einfo) == 0) {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000728 EMSG(_("E272: unhandled exception"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000729 }
730 else {
731 VALUE epath;
732 char *p;
733
734 epath = rb_class_path(eclass);
Bram Moolenaar9c13b352005-05-19 20:53:52 +0000735 vim_snprintf(buff, BUFSIZ, "%s: %s",
Bram Moolenaar165641d2010-02-17 16:23:09 +0100736 RSTRING_PTR(epath), RSTRING_PTR(einfo));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000737 p = strchr(buff, '\n');
738 if (p) *p = '\0';
739 EMSG(buff);
740 }
741 break;
742 default:
Bram Moolenaar9c13b352005-05-19 20:53:52 +0000743 vim_snprintf(buff, BUFSIZ, _("E273: unknown longjmp status %d"), state);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000744 EMSG(buff);
745 break;
746 }
747}
748
Bram Moolenaarcd8b20a2009-05-22 16:20:57 +0000749static VALUE vim_message(VALUE self UNUSED, VALUE str)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000750{
751 char *buff, *p;
752
753 str = rb_obj_as_string(str);
Bram Moolenaar165641d2010-02-17 16:23:09 +0100754 buff = ALLOCA_N(char, RSTRING_LEN(str));
755 strcpy(buff, RSTRING_PTR(str));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000756 p = strchr(buff, '\n');
757 if (p) *p = '\0';
758 MSG(buff);
759 return Qnil;
760}
761
Bram Moolenaarcd8b20a2009-05-22 16:20:57 +0000762static VALUE vim_set_option(VALUE self UNUSED, VALUE str)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000763{
Bram Moolenaar165641d2010-02-17 16:23:09 +0100764 do_set((char_u *)StringValuePtr(str), 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000765 update_screen(NOT_VALID);
766 return Qnil;
767}
768
Bram Moolenaarcd8b20a2009-05-22 16:20:57 +0000769static VALUE vim_command(VALUE self UNUSED, VALUE str)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000770{
Bram Moolenaar165641d2010-02-17 16:23:09 +0100771 do_cmdline_cmd((char_u *)StringValuePtr(str));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000772 return Qnil;
773}
774
Bram Moolenaar3fac56e2010-02-24 15:48:04 +0100775#ifdef FEAT_EVAL
776static VALUE vim_to_ruby(typval_T *tv)
777{
778 VALUE result = Qnil;
779
780 if (tv->v_type == VAR_STRING)
781 {
Bram Moolenaar94127e42010-03-19 23:08:48 +0100782 result = rb_str_new2(tv->vval.v_string == NULL
783 ? "" : (char *)(tv->vval.v_string));
Bram Moolenaar3fac56e2010-02-24 15:48:04 +0100784 }
785 else if (tv->v_type == VAR_NUMBER)
786 {
Bram Moolenaar639a2552010-03-17 18:15:23 +0100787 result = INT2NUM(tv->vval.v_number);
Bram Moolenaar3fac56e2010-02-24 15:48:04 +0100788 }
789# ifdef FEAT_FLOAT
790 else if (tv->v_type == VAR_FLOAT)
791 {
Bram Moolenaar639a2552010-03-17 18:15:23 +0100792 result = rb_float_new(tv->vval.v_float);
Bram Moolenaar3fac56e2010-02-24 15:48:04 +0100793 }
794# endif
795 else if (tv->v_type == VAR_LIST)
796 {
Bram Moolenaar639a2552010-03-17 18:15:23 +0100797 list_T *list = tv->vval.v_list;
798 listitem_T *curr;
Bram Moolenaar3fac56e2010-02-24 15:48:04 +0100799
Bram Moolenaar639a2552010-03-17 18:15:23 +0100800 result = rb_ary_new();
Bram Moolenaar3fac56e2010-02-24 15:48:04 +0100801
Bram Moolenaar639a2552010-03-17 18:15:23 +0100802 if (list != NULL)
803 {
804 for (curr = list->lv_first; curr != NULL; curr = curr->li_next)
805 {
806 rb_ary_push(result, vim_to_ruby(&curr->li_tv));
807 }
808 }
Bram Moolenaar3fac56e2010-02-24 15:48:04 +0100809 }
810 else if (tv->v_type == VAR_DICT)
811 {
Bram Moolenaar639a2552010-03-17 18:15:23 +0100812 result = rb_hash_new();
Bram Moolenaar3fac56e2010-02-24 15:48:04 +0100813
Bram Moolenaar639a2552010-03-17 18:15:23 +0100814 if (tv->vval.v_dict != NULL)
815 {
816 hashtab_T *ht = &tv->vval.v_dict->dv_hashtab;
817 long_u todo = ht->ht_used;
818 hashitem_T *hi;
819 dictitem_T *di;
Bram Moolenaar3fac56e2010-02-24 15:48:04 +0100820
Bram Moolenaar639a2552010-03-17 18:15:23 +0100821 for (hi = ht->ht_array; todo > 0; ++hi)
822 {
823 if (!HASHITEM_EMPTY(hi))
824 {
825 --todo;
Bram Moolenaar3fac56e2010-02-24 15:48:04 +0100826
Bram Moolenaar639a2552010-03-17 18:15:23 +0100827 di = dict_lookup(hi);
828 rb_hash_aset(result, rb_str_new2((char *)hi->hi_key),
Bram Moolenaar3fac56e2010-02-24 15:48:04 +0100829 vim_to_ruby(&di->di_tv));
Bram Moolenaar639a2552010-03-17 18:15:23 +0100830 }
831 }
832 }
Bram Moolenaar3fac56e2010-02-24 15:48:04 +0100833 } /* else return Qnil; */
834
835 return result;
836}
837#endif
838
Bram Moolenaarcd8b20a2009-05-22 16:20:57 +0000839static VALUE vim_evaluate(VALUE self UNUSED, VALUE str)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000840{
841#ifdef FEAT_EVAL
Bram Moolenaar3fac56e2010-02-24 15:48:04 +0100842 typval_T *tv;
843 VALUE result;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000844
Bram Moolenaar3fac56e2010-02-24 15:48:04 +0100845 tv = eval_expr((char_u *)StringValuePtr(str), NULL);
846 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000847 {
Bram Moolenaar639a2552010-03-17 18:15:23 +0100848 return Qnil;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000849 }
Bram Moolenaar3fac56e2010-02-24 15:48:04 +0100850 result = vim_to_ruby(tv);
851
852 free_tv(tv);
853
854 return result;
855#else
856 return Qnil;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000857#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000858}
859
860static VALUE buffer_new(buf_T *buf)
861{
Bram Moolenaare344bea2005-09-01 20:46:49 +0000862 if (buf->b_ruby_ref)
863 {
864 return (VALUE) buf->b_ruby_ref;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000865 }
Bram Moolenaare344bea2005-09-01 20:46:49 +0000866 else
867 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000868 VALUE obj = Data_Wrap_Struct(cBuffer, 0, 0, buf);
Bram Moolenaare344bea2005-09-01 20:46:49 +0000869 buf->b_ruby_ref = (void *) obj;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000870 rb_hash_aset(objtbl, rb_obj_id(obj), obj);
871 return obj;
872 }
873}
874
875static buf_T *get_buf(VALUE obj)
876{
877 buf_T *buf;
878
879 Data_Get_Struct(obj, buf_T, buf);
880 if (buf == NULL)
881 rb_raise(eDeletedBufferError, "attempt to refer to deleted buffer");
882 return buf;
883}
884
885static VALUE buffer_s_current()
886{
887 return buffer_new(curbuf);
888}
889
890static VALUE buffer_s_count()
891{
892 buf_T *b;
893 int n = 0;
894
Bram Moolenaarbe4d5062006-03-18 21:30:13 +0000895 for (b = firstbuf; b != NULL; b = b->b_next)
896 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000897 /* Deleted buffers should not be counted
898 * SegPhault - 01/07/05 */
899 if (b->b_p_bl)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +0000900 n++;
901 }
902
Bram Moolenaar071d4272004-06-13 20:20:40 +0000903 return INT2NUM(n);
904}
905
Bram Moolenaarcd8b20a2009-05-22 16:20:57 +0000906static VALUE buffer_s_aref(VALUE self UNUSED, VALUE num)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000907{
908 buf_T *b;
909 int n = NUM2INT(num);
910
Bram Moolenaarbe4d5062006-03-18 21:30:13 +0000911 for (b = firstbuf; b != NULL; b = b->b_next)
912 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000913 /* Deleted buffers should not be counted
914 * SegPhault - 01/07/05 */
915 if (!b->b_p_bl)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +0000916 continue;
917
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000918 if (n == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000919 return buffer_new(b);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +0000920
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000921 n--;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000922 }
923 return Qnil;
924}
925
926static VALUE buffer_name(VALUE self)
927{
928 buf_T *buf = get_buf(self);
929
Bram Moolenaar35a2e192006-03-13 22:07:11 +0000930 return buf->b_ffname ? rb_str_new2((char *)buf->b_ffname) : Qnil;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000931}
932
933static VALUE buffer_number(VALUE self)
934{
935 buf_T *buf = get_buf(self);
936
937 return INT2NUM(buf->b_fnum);
938}
939
940static VALUE buffer_count(VALUE self)
941{
942 buf_T *buf = get_buf(self);
943
944 return INT2NUM(buf->b_ml.ml_line_count);
945}
946
Bram Moolenaarbe4d5062006-03-18 21:30:13 +0000947static VALUE get_buffer_line(buf_T *buf, linenr_T n)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000948{
Bram Moolenaarbe4d5062006-03-18 21:30:13 +0000949 if (n > 0 && n <= buf->b_ml.ml_line_count)
950 {
Bram Moolenaar35a2e192006-03-13 22:07:11 +0000951 char *line = (char *)ml_get_buf(buf, n, FALSE);
Bram Moolenaar165641d2010-02-17 16:23:09 +0100952 return line ? vim_str2rb_enc_str(line) : Qnil;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000953 }
Bram Moolenaar165641d2010-02-17 16:23:09 +0100954 rb_raise(rb_eIndexError, "line number %ld out of range", (long)n);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +0000955 return Qnil; /* For stop warning */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000956}
957
Bram Moolenaarbe4d5062006-03-18 21:30:13 +0000958static VALUE buffer_aref(VALUE self, VALUE num)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000959{
960 buf_T *buf = get_buf(self);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +0000961
962 if (buf != NULL)
963 return get_buffer_line(buf, (linenr_T)NUM2LONG(num));
964 return Qnil; /* For stop warning */
965}
966
967static VALUE set_buffer_line(buf_T *buf, linenr_T n, VALUE str)
968{
Bram Moolenaar165641d2010-02-17 16:23:09 +0100969 char *line = StringValuePtr(str);
Bram Moolenaar20ff7922006-06-20 19:10:43 +0000970 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000971
Bram Moolenaar20ff7922006-06-20 19:10:43 +0000972 if (n > 0 && n <= buf->b_ml.ml_line_count && line != NULL)
973 {
Bram Moolenaar20ff7922006-06-20 19:10:43 +0000974 /* set curwin/curbuf for "buf" and save some things */
975 aucmd_prepbuf(&aco, buf);
Bram Moolenaar20ff7922006-06-20 19:10:43 +0000976
Bram Moolenaar071d4272004-06-13 20:20:40 +0000977 if (u_savesub(n) == OK) {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +0000978 ml_replace(n, (char_u *)line, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000979 changed();
980#ifdef SYNTAX_HL
981 syn_changed(n); /* recompute syntax hl. for this line */
982#endif
983 }
Bram Moolenaar20ff7922006-06-20 19:10:43 +0000984
Bram Moolenaar20ff7922006-06-20 19:10:43 +0000985 /* restore curwin/curbuf and a few other things */
986 aucmd_restbuf(&aco);
987 /* Careful: autocommands may have made "buf" invalid! */
Bram Moolenaarf30e74c2006-08-16 17:35:00 +0000988
Bram Moolenaar071d4272004-06-13 20:20:40 +0000989 update_curbuf(NOT_VALID);
990 }
Bram Moolenaar20ff7922006-06-20 19:10:43 +0000991 else
992 {
Bram Moolenaar165641d2010-02-17 16:23:09 +0100993 rb_raise(rb_eIndexError, "line number %ld out of range", (long)n);
Bram Moolenaarcd8b20a2009-05-22 16:20:57 +0000994#ifndef __GNUC__
Bram Moolenaar071d4272004-06-13 20:20:40 +0000995 return Qnil; /* For stop warning */
Bram Moolenaarcd8b20a2009-05-22 16:20:57 +0000996#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000997 }
998 return str;
999}
1000
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001001static VALUE buffer_aset(VALUE self, VALUE num, VALUE str)
1002{
1003 buf_T *buf = get_buf(self);
1004
1005 if (buf != NULL)
1006 return set_buffer_line(buf, (linenr_T)NUM2LONG(num), str);
1007 return str;
1008}
1009
Bram Moolenaar071d4272004-06-13 20:20:40 +00001010static VALUE buffer_delete(VALUE self, VALUE num)
1011{
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001012 buf_T *buf = get_buf(self);
1013 long n = NUM2LONG(num);
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001014 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001015
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001016 if (n > 0 && n <= buf->b_ml.ml_line_count)
1017 {
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001018 /* set curwin/curbuf for "buf" and save some things */
1019 aucmd_prepbuf(&aco, buf);
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001020
Bram Moolenaar071d4272004-06-13 20:20:40 +00001021 if (u_savedel(n, 1) == OK) {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001022 ml_delete(n, 0);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001023
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001024 /* Changes to non-active buffers should properly refresh
1025 * SegPhault - 01/09/05 */
1026 deleted_lines_mark(n, 1L);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001027
Bram Moolenaar071d4272004-06-13 20:20:40 +00001028 changed();
1029 }
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001030
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001031 /* restore curwin/curbuf and a few other things */
1032 aucmd_restbuf(&aco);
1033 /* Careful: autocommands may have made "buf" invalid! */
Bram Moolenaarf30e74c2006-08-16 17:35:00 +00001034
Bram Moolenaar071d4272004-06-13 20:20:40 +00001035 update_curbuf(NOT_VALID);
1036 }
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001037 else
1038 {
Bram Moolenaar165641d2010-02-17 16:23:09 +01001039 rb_raise(rb_eIndexError, "line number %ld out of range", n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001040 }
1041 return Qnil;
1042}
1043
1044static VALUE buffer_append(VALUE self, VALUE num, VALUE str)
1045{
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001046 buf_T *buf = get_buf(self);
Bram Moolenaar165641d2010-02-17 16:23:09 +01001047 char *line = StringValuePtr(str);
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001048 long n = NUM2LONG(num);
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001049 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001050
Bram Moolenaar83bac8b2010-02-18 15:53:29 +01001051 if (line == NULL) {
Bram Moolenaar165641d2010-02-17 16:23:09 +01001052 rb_raise(rb_eIndexError, "NULL line");
1053 }
1054 else if (n >= 0 && n <= buf->b_ml.ml_line_count)
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001055 {
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001056 /* set curwin/curbuf for "buf" and save some things */
1057 aucmd_prepbuf(&aco, buf);
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001058
Bram Moolenaar071d4272004-06-13 20:20:40 +00001059 if (u_inssub(n + 1) == OK) {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001060 ml_append(n, (char_u *) line, (colnr_T) 0, FALSE);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001061
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001062 /* Changes to non-active buffers should properly refresh screen
1063 * SegPhault - 12/20/04 */
1064 appended_lines_mark(n, 1L);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001065
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001066 changed();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001067 }
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001068
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001069 /* restore curwin/curbuf and a few other things */
1070 aucmd_restbuf(&aco);
1071 /* Careful: autocommands may have made "buf" invalid! */
Bram Moolenaarf30e74c2006-08-16 17:35:00 +00001072
Bram Moolenaar071d4272004-06-13 20:20:40 +00001073 update_curbuf(NOT_VALID);
1074 }
1075 else {
Bram Moolenaar165641d2010-02-17 16:23:09 +01001076 rb_raise(rb_eIndexError, "line number %ld out of range", n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001077 }
1078 return str;
1079}
1080
1081static VALUE window_new(win_T *win)
1082{
Bram Moolenaare344bea2005-09-01 20:46:49 +00001083 if (win->w_ruby_ref)
1084 {
1085 return (VALUE) win->w_ruby_ref;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001086 }
Bram Moolenaare344bea2005-09-01 20:46:49 +00001087 else
1088 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001089 VALUE obj = Data_Wrap_Struct(cVimWindow, 0, 0, win);
Bram Moolenaare344bea2005-09-01 20:46:49 +00001090 win->w_ruby_ref = (void *) obj;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001091 rb_hash_aset(objtbl, rb_obj_id(obj), obj);
1092 return obj;
1093 }
1094}
1095
1096static win_T *get_win(VALUE obj)
1097{
1098 win_T *win;
1099
1100 Data_Get_Struct(obj, win_T, win);
1101 if (win == NULL)
1102 rb_raise(eDeletedWindowError, "attempt to refer to deleted window");
1103 return win;
1104}
1105
1106static VALUE window_s_current()
1107{
1108 return window_new(curwin);
1109}
1110
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001111/*
1112 * Added line manipulation functions
1113 * SegPhault - 03/07/05
1114 */
1115static VALUE line_s_current()
1116{
1117 return get_buffer_line(curbuf, curwin->w_cursor.lnum);
1118}
1119
Bram Moolenaarcd8b20a2009-05-22 16:20:57 +00001120static VALUE set_current_line(VALUE self UNUSED, VALUE str)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001121{
1122 return set_buffer_line(curbuf, curwin->w_cursor.lnum, str);
1123}
1124
1125static VALUE current_line_number()
1126{
1127 return INT2FIX((int)curwin->w_cursor.lnum);
1128}
1129
1130
1131
Bram Moolenaar071d4272004-06-13 20:20:40 +00001132static VALUE window_s_count()
1133{
1134#ifdef FEAT_WINDOWS
1135 win_T *w;
1136 int n = 0;
1137
Bram Moolenaarf740b292006-02-16 22:11:02 +00001138 for (w = firstwin; w != NULL; w = w->w_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001139 n++;
1140 return INT2NUM(n);
1141#else
1142 return INT2NUM(1);
1143#endif
1144}
1145
Bram Moolenaarcd8b20a2009-05-22 16:20:57 +00001146static VALUE window_s_aref(VALUE self UNUSED, VALUE num)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001147{
1148 win_T *w;
1149 int n = NUM2INT(num);
1150
1151#ifndef FEAT_WINDOWS
1152 w = curwin;
1153#else
1154 for (w = firstwin; w != NULL; w = w->w_next, --n)
1155#endif
1156 if (n == 0)
1157 return window_new(w);
1158 return Qnil;
1159}
1160
1161static VALUE window_buffer(VALUE self)
1162{
1163 win_T *win = get_win(self);
1164
1165 return buffer_new(win->w_buffer);
1166}
1167
1168static VALUE window_height(VALUE self)
1169{
1170 win_T *win = get_win(self);
1171
1172 return INT2NUM(win->w_height);
1173}
1174
1175static VALUE window_set_height(VALUE self, VALUE height)
1176{
1177 win_T *win = get_win(self);
1178 win_T *savewin = curwin;
1179
1180 curwin = win;
1181 win_setheight(NUM2INT(height));
1182 curwin = savewin;
1183 return height;
1184}
1185
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001186static VALUE window_width(VALUE self)
1187{
1188 win_T *win = get_win(self);
1189
1190 return INT2NUM(win->w_width);
1191}
1192
1193static VALUE window_set_width(VALUE self, VALUE width)
1194{
1195 win_T *win = get_win(self);
1196 win_T *savewin = curwin;
1197
1198 curwin = win;
1199 win_setwidth(NUM2INT(width));
1200 curwin = savewin;
1201 return width;
1202}
1203
Bram Moolenaar071d4272004-06-13 20:20:40 +00001204static VALUE window_cursor(VALUE self)
1205{
1206 win_T *win = get_win(self);
1207
1208 return rb_assoc_new(INT2NUM(win->w_cursor.lnum), INT2NUM(win->w_cursor.col));
1209}
1210
1211static VALUE window_set_cursor(VALUE self, VALUE pos)
1212{
1213 VALUE lnum, col;
1214 win_T *win = get_win(self);
1215
1216 Check_Type(pos, T_ARRAY);
Bram Moolenaar165641d2010-02-17 16:23:09 +01001217 if (RARRAY_LEN(pos) != 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001218 rb_raise(rb_eArgError, "array length must be 2");
Bram Moolenaar165641d2010-02-17 16:23:09 +01001219 lnum = RARRAY_PTR(pos)[0];
1220 col = RARRAY_PTR(pos)[1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001221 win->w_cursor.lnum = NUM2LONG(lnum);
1222 win->w_cursor.col = NUM2UINT(col);
1223 check_cursor(); /* put cursor on an existing line */
1224 update_screen(NOT_VALID);
1225 return Qnil;
1226}
1227
Bram Moolenaarcd8b20a2009-05-22 16:20:57 +00001228static VALUE f_p(int argc, VALUE *argv, VALUE self UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001229{
1230 int i;
1231 VALUE str = rb_str_new("", 0);
1232
1233 for (i = 0; i < argc; i++) {
1234 if (i > 0) rb_str_cat(str, ", ", 2);
1235 rb_str_concat(str, rb_inspect(argv[i]));
1236 }
Bram Moolenaar165641d2010-02-17 16:23:09 +01001237 MSG(RSTRING_PTR(str));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001238 return Qnil;
1239}
1240
1241static void ruby_io_init(void)
1242{
1243#ifndef DYNAMIC_RUBY
1244 RUBYEXTERN VALUE rb_stdout;
1245#endif
1246
1247 rb_stdout = rb_obj_alloc(rb_cObject);
1248 rb_define_singleton_method(rb_stdout, "write", vim_message, 1);
1249 rb_define_global_function("p", f_p, -1);
1250}
1251
1252static void ruby_vim_init(void)
1253{
1254 objtbl = rb_hash_new();
1255 rb_global_variable(&objtbl);
1256
Bram Moolenaarf711faf2007-05-10 16:48:19 +00001257 /* The Vim module used to be called "VIM", but "Vim" is better. Make an
1258 * alias "VIM" for backwards compatiblity. */
1259 mVIM = rb_define_module("Vim");
1260 rb_define_const(rb_cObject, "VIM", mVIM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001261 rb_define_const(mVIM, "VERSION_MAJOR", INT2NUM(VIM_VERSION_MAJOR));
1262 rb_define_const(mVIM, "VERSION_MINOR", INT2NUM(VIM_VERSION_MINOR));
1263 rb_define_const(mVIM, "VERSION_BUILD", INT2NUM(VIM_VERSION_BUILD));
1264 rb_define_const(mVIM, "VERSION_PATCHLEVEL", INT2NUM(VIM_VERSION_PATCHLEVEL));
1265 rb_define_const(mVIM, "VERSION_SHORT", rb_str_new2(VIM_VERSION_SHORT));
1266 rb_define_const(mVIM, "VERSION_MEDIUM", rb_str_new2(VIM_VERSION_MEDIUM));
1267 rb_define_const(mVIM, "VERSION_LONG", rb_str_new2(VIM_VERSION_LONG));
1268 rb_define_const(mVIM, "VERSION_LONG_DATE", rb_str_new2(VIM_VERSION_LONG_DATE));
1269 rb_define_module_function(mVIM, "message", vim_message, 1);
1270 rb_define_module_function(mVIM, "set_option", vim_set_option, 1);
1271 rb_define_module_function(mVIM, "command", vim_command, 1);
1272 rb_define_module_function(mVIM, "evaluate", vim_evaluate, 1);
1273
1274 eDeletedBufferError = rb_define_class_under(mVIM, "DeletedBufferError",
1275 rb_eStandardError);
1276 eDeletedWindowError = rb_define_class_under(mVIM, "DeletedWindowError",
1277 rb_eStandardError);
1278
1279 cBuffer = rb_define_class_under(mVIM, "Buffer", rb_cObject);
1280 rb_define_singleton_method(cBuffer, "current", buffer_s_current, 0);
1281 rb_define_singleton_method(cBuffer, "count", buffer_s_count, 0);
1282 rb_define_singleton_method(cBuffer, "[]", buffer_s_aref, 1);
1283 rb_define_method(cBuffer, "name", buffer_name, 0);
1284 rb_define_method(cBuffer, "number", buffer_number, 0);
1285 rb_define_method(cBuffer, "count", buffer_count, 0);
1286 rb_define_method(cBuffer, "length", buffer_count, 0);
1287 rb_define_method(cBuffer, "[]", buffer_aref, 1);
1288 rb_define_method(cBuffer, "[]=", buffer_aset, 2);
1289 rb_define_method(cBuffer, "delete", buffer_delete, 1);
1290 rb_define_method(cBuffer, "append", buffer_append, 2);
1291
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001292 /* Added line manipulation functions
1293 * SegPhault - 03/07/05 */
1294 rb_define_method(cBuffer, "line_number", current_line_number, 0);
1295 rb_define_method(cBuffer, "line", line_s_current, 0);
1296 rb_define_method(cBuffer, "line=", set_current_line, 1);
1297
1298
Bram Moolenaar071d4272004-06-13 20:20:40 +00001299 cVimWindow = rb_define_class_under(mVIM, "Window", rb_cObject);
1300 rb_define_singleton_method(cVimWindow, "current", window_s_current, 0);
1301 rb_define_singleton_method(cVimWindow, "count", window_s_count, 0);
1302 rb_define_singleton_method(cVimWindow, "[]", window_s_aref, 1);
1303 rb_define_method(cVimWindow, "buffer", window_buffer, 0);
1304 rb_define_method(cVimWindow, "height", window_height, 0);
1305 rb_define_method(cVimWindow, "height=", window_set_height, 1);
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001306 rb_define_method(cVimWindow, "width", window_width, 0);
1307 rb_define_method(cVimWindow, "width=", window_set_width, 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001308 rb_define_method(cVimWindow, "cursor", window_cursor, 0);
1309 rb_define_method(cVimWindow, "cursor=", window_set_cursor, 1);
1310
1311 rb_define_virtual_variable("$curbuf", buffer_s_current, 0);
1312 rb_define_virtual_variable("$curwin", window_s_current, 0);
1313}