blob: 0e95e17b708196e58426e0e788ca4bb72e3311b5 [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 Moolenaar3ca71f12010-10-27 16:49:47 +020017#include "auto/config.h"
18
Bram Moolenaar071d4272004-06-13 20:20:40 +000019#ifdef _WIN32
20# if !defined(DYNAMIC_RUBY_VER) || (DYNAMIC_RUBY_VER < 18)
21# define NT
22# endif
23# ifndef DYNAMIC_RUBY
24# define IMPORT /* For static dll usage __declspec(dllimport) */
25# define RUBYEXTERN __declspec(dllimport)
26# endif
27#endif
28#ifndef RUBYEXTERN
29# define RUBYEXTERN extern
30#endif
31
Bram Moolenaarf9b5ef82010-09-29 13:02:53 +020032#ifdef DYNAMIC_RUBY
Bram Moolenaar071d4272004-06-13 20:20:40 +000033/*
34 * This is tricky. In ruby.h there is (inline) function rb_class_of()
35 * definition. This function use these variables. But we want function to
36 * use dll_* variables.
37 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000038# define rb_cFalseClass (*dll_rb_cFalseClass)
39# define rb_cFixnum (*dll_rb_cFixnum)
40# define rb_cNilClass (*dll_rb_cNilClass)
41# define rb_cSymbol (*dll_rb_cSymbol)
42# define rb_cTrueClass (*dll_rb_cTrueClass)
43# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 18
44/*
Bram Moolenaar42d57f02010-03-10 12:47:00 +010045 * On ver 1.8, all Ruby functions are exported with "__declspec(dllimport)"
46 * in ruby.h. But it causes trouble for these variables, because it is
Bram Moolenaar071d4272004-06-13 20:20:40 +000047 * defined in this file. When defined this RUBY_EXPORT it modified to
48 * "extern" and be able to avoid this problem.
49 */
50# define RUBY_EXPORT
51# endif
Bram Moolenaarf9b5ef82010-09-29 13:02:53 +020052
Bram Moolenaar3ca71f12010-10-27 16:49:47 +020053#ifndef WIN3264
Bram Moolenaarf9b5ef82010-09-29 13:02:53 +020054# include <dlfcn.h>
Bram Moolenaar3ca71f12010-10-27 16:49:47 +020055# define HINSTANCE void*
56# define RUBY_PROC void*
Bram Moolenaarf9b5ef82010-09-29 13:02:53 +020057# define load_dll(n) dlopen((n), RTLD_LAZY|RTLD_GLOBAL)
58# define symbol_from_dll dlsym
59# define close_dll dlclose
60#else
Bram Moolenaar3ca71f12010-10-27 16:49:47 +020061# define RUBY_PROC FARPROC
Bram Moolenaarebbcb822010-10-23 14:02:54 +020062# define load_dll vimLoadLib
Bram Moolenaarf9b5ef82010-09-29 13:02:53 +020063# define symbol_from_dll GetProcAddress
64# define close_dll FreeLibrary
Bram Moolenaar071d4272004-06-13 20:20:40 +000065#endif
66
Bram Moolenaarf9b5ef82010-09-29 13:02:53 +020067#endif /* ifdef DYNAMIC_RUBY */
68
Bram Moolenaar0b69c732010-02-17 15:11:50 +010069/* suggested by Ariya Mizutani */
70#if (_MSC_VER == 1200)
71# undef _WIN32_WINNT
72#endif
73
Bram Moolenaar639a2552010-03-17 18:15:23 +010074#if (defined(RUBY_VERSION) && RUBY_VERSION >= 19) \
75 || (defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 19)
76# define RUBY19_OR_LATER 1
77#endif
78
Bram Moolenaar42d57f02010-03-10 12:47:00 +010079#if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 19
80/* Ruby 1.9 defines a number of static functions which use rb_num2long and
81 * rb_int2big */
82# define rb_num2long rb_num2long_stub
83# define rb_int2big rb_int2big_stub
84#endif
85
Bram Moolenaar071d4272004-06-13 20:20:40 +000086#include <ruby.h>
Bram Moolenaar639a2552010-03-17 18:15:23 +010087#ifdef RUBY19_OR_LATER
Bram Moolenaar165641d2010-02-17 16:23:09 +010088# include <ruby/encoding.h>
89#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000090
91#undef EXTERN
92#undef _
93
94/* T_DATA defined both by Ruby and Mac header files, hack around it... */
Bram Moolenaarcb635362007-05-12 13:02:42 +000095#if defined(MACOS_X_UNIX) || defined(macintosh)
Bram Moolenaar071d4272004-06-13 20:20:40 +000096# define __OPENTRANSPORT__
97# define __OPENTRANSPORTPROTOCOL__
98# define __OPENTRANSPORTPROVIDERS__
99#endif
100
Bram Moolenaar165641d2010-02-17 16:23:09 +0100101/*
102 * Backward compatiblity for Ruby 1.8 and earlier.
103 * Ruby 1.9 does not provide STR2CSTR, instead StringValuePtr is provided.
104 * Ruby 1.9 does not provide RXXX(s)->len and RXXX(s)->ptr, instead
105 * RXXX_LEN(s) and RXXX_PTR(s) are provided.
106 */
107#ifndef StringValuePtr
108# define StringValuePtr(s) STR2CSTR(s)
109#endif
110#ifndef RARRAY_LEN
111# define RARRAY_LEN(s) RARRAY(s)->len
112#endif
113#ifndef RARRAY_PTR
114# define RARRAY_PTR(s) RARRAY(s)->ptr
115#endif
116#ifndef RSTRING_LEN
117# define RSTRING_LEN(s) RSTRING(s)->len
118#endif
119#ifndef RSTRING_PTR
120# define RSTRING_PTR(s) RSTRING(s)->ptr
121#endif
122
Bram Moolenaar071d4272004-06-13 20:20:40 +0000123#include "vim.h"
124#include "version.h"
125
126#if defined(PROTO) && !defined(FEAT_RUBY)
127/* Define these to be able to generate the function prototypes. */
128# define VALUE int
129# define RUBY_DATA_FUNC int
130#endif
131
132static int ruby_initialized = 0;
133static VALUE objtbl;
134
135static VALUE mVIM;
136static VALUE cBuffer;
137static VALUE cVimWindow;
138static VALUE eDeletedBufferError;
139static VALUE eDeletedWindowError;
140
141static int ensure_ruby_initialized(void);
142static void error_print(int);
143static void ruby_io_init(void);
144static void ruby_vim_init(void);
145
146#if defined(DYNAMIC_RUBY) || defined(PROTO)
147#ifdef PROTO
148# define HINSTANCE int /* for generating prototypes */
149#endif
150
151/*
152 * Wrapper defines
153 */
154#define rb_assoc_new dll_rb_assoc_new
155#define rb_cObject (*dll_rb_cObject)
156#define rb_check_type dll_rb_check_type
157#define rb_class_path dll_rb_class_path
158#define rb_data_object_alloc dll_rb_data_object_alloc
159#define rb_define_class_under dll_rb_define_class_under
160#define rb_define_const dll_rb_define_const
161#define rb_define_global_function dll_rb_define_global_function
162#define rb_define_method dll_rb_define_method
163#define rb_define_module dll_rb_define_module
164#define rb_define_module_function dll_rb_define_module_function
165#define rb_define_singleton_method dll_rb_define_singleton_method
166#define rb_define_virtual_variable dll_rb_define_virtual_variable
167#define rb_stdout (*dll_rb_stdout)
168#define rb_eArgError (*dll_rb_eArgError)
169#define rb_eIndexError (*dll_rb_eIndexError)
170#define rb_eRuntimeError (*dll_rb_eRuntimeError)
171#define rb_eStandardError (*dll_rb_eStandardError)
172#define rb_eval_string_protect dll_rb_eval_string_protect
173#define rb_global_variable dll_rb_global_variable
174#define rb_hash_aset dll_rb_hash_aset
175#define rb_hash_new dll_rb_hash_new
176#define rb_inspect dll_rb_inspect
177#define rb_int2inum dll_rb_int2inum
178#define rb_lastline_get dll_rb_lastline_get
179#define rb_lastline_set dll_rb_lastline_set
180#define rb_load_protect dll_rb_load_protect
Bram Moolenaar3ca71f12010-10-27 16:49:47 +0200181#ifndef RUBY19_OR_LATER
Bram Moolenaar071d4272004-06-13 20:20:40 +0000182#define rb_num2long dll_rb_num2long
Bram Moolenaar3ca71f12010-10-27 16:49:47 +0200183#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000184#define rb_num2ulong dll_rb_num2ulong
185#define rb_obj_alloc dll_rb_obj_alloc
186#define rb_obj_as_string dll_rb_obj_as_string
187#define rb_obj_id dll_rb_obj_id
188#define rb_raise dll_rb_raise
Bram Moolenaar071d4272004-06-13 20:20:40 +0000189#define rb_str_cat dll_rb_str_cat
190#define rb_str_concat dll_rb_str_concat
191#define rb_str_new dll_rb_str_new
Bram Moolenaar1d2beae2010-05-22 21:56:55 +0200192#ifdef rb_str_new2
193/* Ruby may #define rb_str_new2 to use rb_str_new_cstr. */
194# define need_rb_str_new_cstr 1
Bram Moolenaar3ca71f12010-10-27 16:49:47 +0200195/* Ruby's headers #define rb_str_new_cstr to make use of GCC's
196 * __builtin_constant_p extension. */
197# undef rb_str_new_cstr
Bram Moolenaar1d2beae2010-05-22 21:56:55 +0200198# define rb_str_new_cstr dll_rb_str_new_cstr
199#else
Bram Moolenaar218116c2010-05-20 21:46:00 +0200200# define rb_str_new2 dll_rb_str_new2
201#endif
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100202#if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 18
Bram Moolenaarf9b5ef82010-09-29 13:02:53 +0200203# define rb_string_value dll_rb_string_value
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100204# define rb_string_value_ptr dll_rb_string_value_ptr
205# define rb_float_new dll_rb_float_new
206# define rb_ary_new dll_rb_ary_new
207# define rb_ary_push dll_rb_ary_push
Bram Moolenaarf9b5ef82010-09-29 13:02:53 +0200208#else
209# define rb_str2cstr dll_rb_str2cstr
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100210#endif
Bram Moolenaar639a2552010-03-17 18:15:23 +0100211#ifdef RUBY19_OR_LATER
Bram Moolenaar165641d2010-02-17 16:23:09 +0100212# define rb_errinfo dll_rb_errinfo
213#else
214# define ruby_errinfo (*dll_ruby_errinfo)
215#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000216#define ruby_init dll_ruby_init
217#define ruby_init_loadpath dll_ruby_init_loadpath
Bram Moolenaar3ca71f12010-10-27 16:49:47 +0200218#ifdef WIN3264
219# define NtInitialize dll_NtInitialize
220# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 18
221# define rb_w32_snprintf dll_rb_w32_snprintf
222# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000223#endif
224
Bram Moolenaar639a2552010-03-17 18:15:23 +0100225#ifdef RUBY19_OR_LATER
Bram Moolenaar165641d2010-02-17 16:23:09 +0100226# define ruby_script dll_ruby_script
227# define rb_enc_find_index dll_rb_enc_find_index
228# define rb_enc_find dll_rb_enc_find
229# define rb_enc_str_new dll_rb_enc_str_new
230# define rb_sprintf dll_rb_sprintf
Bram Moolenaar639a2552010-03-17 18:15:23 +0100231# define ruby_init_stack dll_ruby_init_stack
Bram Moolenaar165641d2010-02-17 16:23:09 +0100232#endif
233
Bram Moolenaar071d4272004-06-13 20:20:40 +0000234/*
235 * Pointers for dynamic link
236 */
237static VALUE (*dll_rb_assoc_new) (VALUE, VALUE);
Bram Moolenaarba52cde2010-06-25 04:29:11 +0200238VALUE *dll_rb_cFalseClass;
239VALUE *dll_rb_cFixnum;
240VALUE *dll_rb_cNilClass;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000241static VALUE *dll_rb_cObject;
Bram Moolenaarba52cde2010-06-25 04:29:11 +0200242VALUE *dll_rb_cSymbol;
243VALUE *dll_rb_cTrueClass;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000244static void (*dll_rb_check_type) (VALUE,int);
245static VALUE (*dll_rb_class_path) (VALUE);
246static VALUE (*dll_rb_data_object_alloc) (VALUE, void*, RUBY_DATA_FUNC, RUBY_DATA_FUNC);
247static VALUE (*dll_rb_define_class_under) (VALUE, const char*, VALUE);
248static void (*dll_rb_define_const) (VALUE,const char*,VALUE);
249static void (*dll_rb_define_global_function) (const char*,VALUE(*)(),int);
250static void (*dll_rb_define_method) (VALUE,const char*,VALUE(*)(),int);
251static VALUE (*dll_rb_define_module) (const char*);
252static void (*dll_rb_define_module_function) (VALUE,const char*,VALUE(*)(),int);
253static void (*dll_rb_define_singleton_method) (VALUE,const char*,VALUE(*)(),int);
254static void (*dll_rb_define_virtual_variable) (const char*,VALUE(*)(),void(*)());
255static VALUE *dll_rb_stdout;
256static VALUE *dll_rb_eArgError;
257static VALUE *dll_rb_eIndexError;
258static VALUE *dll_rb_eRuntimeError;
259static VALUE *dll_rb_eStandardError;
260static VALUE (*dll_rb_eval_string_protect) (const char*, int*);
261static void (*dll_rb_global_variable) (VALUE*);
262static VALUE (*dll_rb_hash_aset) (VALUE, VALUE, VALUE);
263static VALUE (*dll_rb_hash_new) (void);
264static VALUE (*dll_rb_inspect) (VALUE);
265static VALUE (*dll_rb_int2inum) (long);
266static VALUE (*dll_rb_int2inum) (long);
267static VALUE (*dll_rb_lastline_get) (void);
268static void (*dll_rb_lastline_set) (VALUE);
269static void (*dll_rb_load_protect) (VALUE, int, int*);
270static long (*dll_rb_num2long) (VALUE);
271static unsigned long (*dll_rb_num2ulong) (VALUE);
272static VALUE (*dll_rb_obj_alloc) (VALUE);
273static VALUE (*dll_rb_obj_as_string) (VALUE);
274static VALUE (*dll_rb_obj_id) (VALUE);
275static void (*dll_rb_raise) (VALUE, const char*, ...);
Bram Moolenaarf9b5ef82010-09-29 13:02:53 +0200276#if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 18
277static VALUE (*dll_rb_string_value) (volatile VALUE*);
278#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000279static char *(*dll_rb_str2cstr) (VALUE,int*);
Bram Moolenaarf9b5ef82010-09-29 13:02:53 +0200280#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000281static VALUE (*dll_rb_str_cat) (VALUE, const char*, long);
282static VALUE (*dll_rb_str_concat) (VALUE, VALUE);
283static VALUE (*dll_rb_str_new) (const char*, long);
Bram Moolenaar1d2beae2010-05-22 21:56:55 +0200284#ifdef need_rb_str_new_cstr
285/* Ruby may #define rb_str_new2 to use rb_str_new_cstr. */
286static VALUE (*dll_rb_str_new_cstr) (const char*);
287#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000288static VALUE (*dll_rb_str_new2) (const char*);
Bram Moolenaar1d2beae2010-05-22 21:56:55 +0200289#endif
Bram Moolenaar639a2552010-03-17 18:15:23 +0100290#ifdef RUBY19_OR_LATER
Bram Moolenaar165641d2010-02-17 16:23:09 +0100291static VALUE (*dll_rb_errinfo) (void);
292#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000293static VALUE *dll_ruby_errinfo;
Bram Moolenaar165641d2010-02-17 16:23:09 +0100294#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000295static void (*dll_ruby_init) (void);
296static void (*dll_ruby_init_loadpath) (void);
Bram Moolenaar3ca71f12010-10-27 16:49:47 +0200297#ifdef WIN3264
Bram Moolenaar0b69c732010-02-17 15:11:50 +0100298static void (*dll_NtInitialize) (int*, char***);
Bram Moolenaar3ca71f12010-10-27 16:49:47 +0200299# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 18
300static int (*dll_rb_w32_snprintf)(char*, size_t, const char*, ...);
301# endif
302#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000303#if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 18
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100304static char * (*dll_rb_string_value_ptr) (volatile VALUE*);
305static VALUE (*dll_rb_float_new) (double);
306static VALUE (*dll_rb_ary_new) (void);
307static VALUE (*dll_rb_ary_push) (VALUE, VALUE);
308#endif
Bram Moolenaar639a2552010-03-17 18:15:23 +0100309#ifdef RUBY19_OR_LATER
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100310static VALUE (*dll_rb_int2big)(SIGNED_VALUE);
311#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000312
Bram Moolenaar639a2552010-03-17 18:15:23 +0100313#ifdef RUBY19_OR_LATER
Bram Moolenaar165641d2010-02-17 16:23:09 +0100314static void (*dll_ruby_script) (const char*);
315static int (*dll_rb_enc_find_index) (const char*);
316static rb_encoding* (*dll_rb_enc_find) (const char*);
317static VALUE (*dll_rb_enc_str_new) (const char*, long, rb_encoding*);
318static VALUE (*dll_rb_sprintf) (const char*, ...);
Bram Moolenaar639a2552010-03-17 18:15:23 +0100319static void (*ruby_init_stack)(VALUE*);
Bram Moolenaar165641d2010-02-17 16:23:09 +0100320#endif
321
Bram Moolenaar639a2552010-03-17 18:15:23 +0100322#ifdef RUBY19_OR_LATER
Bram Moolenaarba52cde2010-06-25 04:29:11 +0200323SIGNED_VALUE rb_num2long_stub(VALUE x)
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100324{
325 return dll_rb_num2long(x);
326}
Bram Moolenaarba52cde2010-06-25 04:29:11 +0200327VALUE rb_int2big_stub(SIGNED_VALUE x)
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100328{
329 return dll_rb_int2big(x);
330}
331#endif
332
Bram Moolenaar3ca71f12010-10-27 16:49:47 +0200333static HINSTANCE hinstRuby = NULL; /* Instance of ruby.dll */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000334
335/*
Bram Moolenaarda2303d2005-08-30 21:55:26 +0000336 * Table of name to function pointer of ruby.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000337 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000338static struct
339{
340 char *name;
341 RUBY_PROC *ptr;
342} ruby_funcname_table[] =
343{
344 {"rb_assoc_new", (RUBY_PROC*)&dll_rb_assoc_new},
345 {"rb_cFalseClass", (RUBY_PROC*)&dll_rb_cFalseClass},
346 {"rb_cFixnum", (RUBY_PROC*)&dll_rb_cFixnum},
347 {"rb_cNilClass", (RUBY_PROC*)&dll_rb_cNilClass},
348 {"rb_cObject", (RUBY_PROC*)&dll_rb_cObject},
349 {"rb_cSymbol", (RUBY_PROC*)&dll_rb_cSymbol},
350 {"rb_cTrueClass", (RUBY_PROC*)&dll_rb_cTrueClass},
351 {"rb_check_type", (RUBY_PROC*)&dll_rb_check_type},
352 {"rb_class_path", (RUBY_PROC*)&dll_rb_class_path},
353 {"rb_data_object_alloc", (RUBY_PROC*)&dll_rb_data_object_alloc},
354 {"rb_define_class_under", (RUBY_PROC*)&dll_rb_define_class_under},
355 {"rb_define_const", (RUBY_PROC*)&dll_rb_define_const},
356 {"rb_define_global_function", (RUBY_PROC*)&dll_rb_define_global_function},
357 {"rb_define_method", (RUBY_PROC*)&dll_rb_define_method},
358 {"rb_define_module", (RUBY_PROC*)&dll_rb_define_module},
359 {"rb_define_module_function", (RUBY_PROC*)&dll_rb_define_module_function},
360 {"rb_define_singleton_method", (RUBY_PROC*)&dll_rb_define_singleton_method},
361 {"rb_define_virtual_variable", (RUBY_PROC*)&dll_rb_define_virtual_variable},
362 {"rb_stdout", (RUBY_PROC*)&dll_rb_stdout},
363 {"rb_eArgError", (RUBY_PROC*)&dll_rb_eArgError},
364 {"rb_eIndexError", (RUBY_PROC*)&dll_rb_eIndexError},
365 {"rb_eRuntimeError", (RUBY_PROC*)&dll_rb_eRuntimeError},
366 {"rb_eStandardError", (RUBY_PROC*)&dll_rb_eStandardError},
367 {"rb_eval_string_protect", (RUBY_PROC*)&dll_rb_eval_string_protect},
368 {"rb_global_variable", (RUBY_PROC*)&dll_rb_global_variable},
369 {"rb_hash_aset", (RUBY_PROC*)&dll_rb_hash_aset},
370 {"rb_hash_new", (RUBY_PROC*)&dll_rb_hash_new},
371 {"rb_inspect", (RUBY_PROC*)&dll_rb_inspect},
372 {"rb_int2inum", (RUBY_PROC*)&dll_rb_int2inum},
373 {"rb_lastline_get", (RUBY_PROC*)&dll_rb_lastline_get},
374 {"rb_lastline_set", (RUBY_PROC*)&dll_rb_lastline_set},
375 {"rb_load_protect", (RUBY_PROC*)&dll_rb_load_protect},
376 {"rb_num2long", (RUBY_PROC*)&dll_rb_num2long},
377 {"rb_num2ulong", (RUBY_PROC*)&dll_rb_num2ulong},
378 {"rb_obj_alloc", (RUBY_PROC*)&dll_rb_obj_alloc},
379 {"rb_obj_as_string", (RUBY_PROC*)&dll_rb_obj_as_string},
380 {"rb_obj_id", (RUBY_PROC*)&dll_rb_obj_id},
381 {"rb_raise", (RUBY_PROC*)&dll_rb_raise},
Bram Moolenaarf9b5ef82010-09-29 13:02:53 +0200382#if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 18
383 {"rb_string_value", (RUBY_PROC*)&dll_rb_string_value},
384#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000385 {"rb_str2cstr", (RUBY_PROC*)&dll_rb_str2cstr},
Bram Moolenaarf9b5ef82010-09-29 13:02:53 +0200386#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000387 {"rb_str_cat", (RUBY_PROC*)&dll_rb_str_cat},
388 {"rb_str_concat", (RUBY_PROC*)&dll_rb_str_concat},
389 {"rb_str_new", (RUBY_PROC*)&dll_rb_str_new},
Bram Moolenaar1d2beae2010-05-22 21:56:55 +0200390#ifdef need_rb_str_new_cstr
391 {"rb_str_new_cstr", (RUBY_PROC*)&dll_rb_str_new_cstr},
392#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000393 {"rb_str_new2", (RUBY_PROC*)&dll_rb_str_new2},
Bram Moolenaar1d2beae2010-05-22 21:56:55 +0200394#endif
Bram Moolenaar639a2552010-03-17 18:15:23 +0100395#ifdef RUBY19_OR_LATER
Bram Moolenaar165641d2010-02-17 16:23:09 +0100396 {"rb_errinfo", (RUBY_PROC*)&dll_rb_errinfo},
397#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000398 {"ruby_errinfo", (RUBY_PROC*)&dll_ruby_errinfo},
Bram Moolenaar165641d2010-02-17 16:23:09 +0100399#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000400 {"ruby_init", (RUBY_PROC*)&dll_ruby_init},
401 {"ruby_init_loadpath", (RUBY_PROC*)&dll_ruby_init_loadpath},
Bram Moolenaar3ca71f12010-10-27 16:49:47 +0200402#ifdef WIN3264
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100403 {
Bram Moolenaar3ca71f12010-10-27 16:49:47 +0200404# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER < 19
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100405 "NtInitialize",
Bram Moolenaar3ca71f12010-10-27 16:49:47 +0200406# else
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100407 "ruby_sysinit",
Bram Moolenaar3ca71f12010-10-27 16:49:47 +0200408# endif
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100409 (RUBY_PROC*)&dll_NtInitialize},
Bram Moolenaar3ca71f12010-10-27 16:49:47 +0200410# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 18
Bram Moolenaar071d4272004-06-13 20:20:40 +0000411 {"rb_w32_snprintf", (RUBY_PROC*)&dll_rb_w32_snprintf},
Bram Moolenaar3ca71f12010-10-27 16:49:47 +0200412# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000413#endif
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100414#if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 18
415 {"rb_string_value_ptr", (RUBY_PROC*)&dll_rb_string_value_ptr},
416 {"rb_float_new", (RUBY_PROC*)&dll_rb_float_new},
417 {"rb_ary_new", (RUBY_PROC*)&dll_rb_ary_new},
418 {"rb_ary_push", (RUBY_PROC*)&dll_rb_ary_push},
419#endif
Bram Moolenaar639a2552010-03-17 18:15:23 +0100420#ifdef RUBY19_OR_LATER
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100421 {"rb_int2big", (RUBY_PROC*)&dll_rb_int2big},
Bram Moolenaar165641d2010-02-17 16:23:09 +0100422 {"ruby_script", (RUBY_PROC*)&dll_ruby_script},
423 {"rb_enc_find_index", (RUBY_PROC*)&dll_rb_enc_find_index},
424 {"rb_enc_find", (RUBY_PROC*)&dll_rb_enc_find},
425 {"rb_enc_str_new", (RUBY_PROC*)&dll_rb_enc_str_new},
426 {"rb_sprintf", (RUBY_PROC*)&dll_rb_sprintf},
Bram Moolenaar639a2552010-03-17 18:15:23 +0100427 {"ruby_init_stack", (RUBY_PROC*)&dll_ruby_init_stack},
Bram Moolenaar165641d2010-02-17 16:23:09 +0100428#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000429 {"", NULL},
430};
431
432/*
433 * Free ruby.dll
434 */
435 static void
436end_dynamic_ruby()
437{
438 if (hinstRuby)
439 {
Bram Moolenaarf9b5ef82010-09-29 13:02:53 +0200440 close_dll(hinstRuby);
Bram Moolenaar3ca71f12010-10-27 16:49:47 +0200441 hinstRuby = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000442 }
443}
444
445/*
446 * Load library and get all pointers.
447 * Parameter 'libname' provides name of DLL.
448 * Return OK or FAIL.
449 */
450 static int
451ruby_runtime_link_init(char *libname, int verbose)
452{
453 int i;
454
455 if (hinstRuby)
456 return OK;
Bram Moolenaarf9b5ef82010-09-29 13:02:53 +0200457 hinstRuby = load_dll(libname);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000458 if (!hinstRuby)
459 {
460 if (verbose)
461 EMSG2(_(e_loadlib), libname);
462 return FAIL;
463 }
464
465 for (i = 0; ruby_funcname_table[i].ptr; ++i)
466 {
Bram Moolenaarf9b5ef82010-09-29 13:02:53 +0200467 if (!(*ruby_funcname_table[i].ptr = symbol_from_dll(hinstRuby,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000468 ruby_funcname_table[i].name)))
469 {
Bram Moolenaarf9b5ef82010-09-29 13:02:53 +0200470 close_dll(hinstRuby);
Bram Moolenaar3ca71f12010-10-27 16:49:47 +0200471 hinstRuby = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000472 if (verbose)
473 EMSG2(_(e_loadfunc), ruby_funcname_table[i].name);
474 return FAIL;
475 }
476 }
477 return OK;
478}
479
480/*
481 * If ruby is enabled (there is installed ruby on Windows system) return TRUE,
482 * else FALSE.
483 */
484 int
485ruby_enabled(verbose)
486 int verbose;
487{
488 return ruby_runtime_link_init(DYNAMIC_RUBY_DLL, verbose) == OK;
489}
490#endif /* defined(DYNAMIC_RUBY) || defined(PROTO) */
491
492 void
493ruby_end()
494{
495#ifdef DYNAMIC_RUBY
496 end_dynamic_ruby();
497#endif
498}
499
500void ex_ruby(exarg_T *eap)
501{
502 int state;
503 char *script = NULL;
504
Bram Moolenaar35a2e192006-03-13 22:07:11 +0000505 script = (char *)script_get(eap, eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000506 if (!eap->skip && ensure_ruby_initialized())
507 {
508 if (script == NULL)
509 rb_eval_string_protect((char *)eap->arg, &state);
510 else
511 rb_eval_string_protect(script, &state);
512 if (state)
513 error_print(state);
514 }
515 vim_free(script);
516}
517
Bram Moolenaar165641d2010-02-17 16:23:09 +0100518/*
519 * In Ruby 1.9 or later, ruby String object has encoding.
520 * conversion buffer string of vim to ruby String object using
521 * VIM encoding option.
522 */
523 static VALUE
524vim_str2rb_enc_str(const char *s)
525{
Bram Moolenaar639a2552010-03-17 18:15:23 +0100526#ifdef RUBY19_OR_LATER
Bram Moolenaar165641d2010-02-17 16:23:09 +0100527 int isnum;
528 long lval;
529 char_u *sval;
530 rb_encoding *enc;
531
532 isnum = get_option_value((char_u *)"enc", &lval, &sval, 0);
533 if (isnum == 0)
534 {
535 enc = rb_enc_find((char *)sval);
536 vim_free(sval);
537 if (enc) {
538 return rb_enc_str_new(s, strlen(s), enc);
539 }
540 }
541#endif
542 return rb_str_new2(s);
543}
544
545 static VALUE
546eval_enc_string_protect(const char *str, int *state)
547{
Bram Moolenaar639a2552010-03-17 18:15:23 +0100548#ifdef RUBY19_OR_LATER
Bram Moolenaar165641d2010-02-17 16:23:09 +0100549 int isnum;
550 long lval;
551 char_u *sval;
552 rb_encoding *enc;
553 VALUE v;
554
555 isnum = get_option_value((char_u *)"enc", &lval, &sval, 0);
556 if (isnum == 0)
557 {
558 enc = rb_enc_find((char *)sval);
559 vim_free(sval);
560 if (enc)
561 {
562 v = rb_sprintf("#-*- coding:%s -*-\n%s", rb_enc_name(enc), str);
563 return rb_eval_string_protect(StringValuePtr(v), state);
564 }
565 }
566#endif
567 return rb_eval_string_protect(str, state);
568}
569
Bram Moolenaar071d4272004-06-13 20:20:40 +0000570void ex_rubydo(exarg_T *eap)
571{
572 int state;
573 linenr_T i;
574
575 if (ensure_ruby_initialized())
576 {
577 if (u_save(eap->line1 - 1, eap->line2 + 1) != OK)
578 return;
579 for (i = eap->line1; i <= eap->line2; i++) {
580 VALUE line, oldline;
581
Bram Moolenaar165641d2010-02-17 16:23:09 +0100582 line = oldline = vim_str2rb_enc_str((char *)ml_get(i));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000583 rb_lastline_set(line);
Bram Moolenaar165641d2010-02-17 16:23:09 +0100584 eval_enc_string_protect((char *) eap->arg, &state);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000585 if (state) {
586 error_print(state);
587 break;
588 }
589 line = rb_lastline_get();
590 if (!NIL_P(line)) {
591 if (TYPE(line) != T_STRING) {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000592 EMSG(_("E265: $_ must be an instance of String"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000593 return;
594 }
Bram Moolenaar165641d2010-02-17 16:23:09 +0100595 ml_replace(i, (char_u *) StringValuePtr(line), 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000596 changed();
597#ifdef SYNTAX_HL
598 syn_changed(i); /* recompute syntax hl. for this line */
599#endif
600 }
601 }
602 check_cursor();
603 update_curbuf(NOT_VALID);
604 }
605}
606
607void ex_rubyfile(exarg_T *eap)
608{
609 int state;
610
611 if (ensure_ruby_initialized())
612 {
613 rb_load_protect(rb_str_new2((char *) eap->arg), 0, &state);
614 if (state) error_print(state);
615 }
616}
617
618void ruby_buffer_free(buf_T *buf)
619{
Bram Moolenaare344bea2005-09-01 20:46:49 +0000620 if (buf->b_ruby_ref)
621 {
622 rb_hash_aset(objtbl, rb_obj_id((VALUE) buf->b_ruby_ref), Qnil);
623 RDATA(buf->b_ruby_ref)->data = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000624 }
625}
626
627void ruby_window_free(win_T *win)
628{
Bram Moolenaare344bea2005-09-01 20:46:49 +0000629 if (win->w_ruby_ref)
630 {
631 rb_hash_aset(objtbl, rb_obj_id((VALUE) win->w_ruby_ref), Qnil);
632 RDATA(win->w_ruby_ref)->data = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000633 }
634}
635
636static int ensure_ruby_initialized(void)
637{
638 if (!ruby_initialized)
639 {
640#ifdef DYNAMIC_RUBY
641 if (ruby_enabled(TRUE))
642 {
643#endif
Bram Moolenaar0b69c732010-02-17 15:11:50 +0100644#ifdef _WIN32
645 /* suggested by Ariya Mizutani */
646 int argc = 1;
647 char *argv[] = {"gvim.exe"};
648 NtInitialize(&argc, &argv);
649#endif
Bram Moolenaarb2c03502010-07-02 20:20:09 +0200650 {
Bram Moolenaar639a2552010-03-17 18:15:23 +0100651#ifdef RUBY19_OR_LATER
Bram Moolenaarb2c03502010-07-02 20:20:09 +0200652 RUBY_INIT_STACK;
Bram Moolenaar165641d2010-02-17 16:23:09 +0100653#endif
Bram Moolenaarb2c03502010-07-02 20:20:09 +0200654 ruby_init();
655 }
Bram Moolenaar639a2552010-03-17 18:15:23 +0100656#ifdef RUBY19_OR_LATER
Bram Moolenaar165641d2010-02-17 16:23:09 +0100657 ruby_script("vim-ruby");
658#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000659 ruby_init_loadpath();
660 ruby_io_init();
Bram Moolenaar639a2552010-03-17 18:15:23 +0100661#ifdef RUBY19_OR_LATER
Bram Moolenaar165641d2010-02-17 16:23:09 +0100662 rb_enc_find_index("encdb");
663#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000664 ruby_vim_init();
665 ruby_initialized = 1;
666#ifdef DYNAMIC_RUBY
667 }
668 else
669 {
670 EMSG(_("E266: Sorry, this command is disabled, the Ruby library could not be loaded."));
671 return 0;
672 }
673#endif
674 }
675 return ruby_initialized;
676}
677
678static void error_print(int state)
679{
680#ifndef DYNAMIC_RUBY
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100681#if !(defined(RUBY_VERSION) && RUBY_VERSION >= 19) \
682 && !(defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 19)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000683 RUBYEXTERN VALUE ruby_errinfo;
684#endif
Bram Moolenaar165641d2010-02-17 16:23:09 +0100685#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000686 VALUE eclass;
687 VALUE einfo;
688 char buff[BUFSIZ];
689
690#define TAG_RETURN 0x1
691#define TAG_BREAK 0x2
692#define TAG_NEXT 0x3
693#define TAG_RETRY 0x4
694#define TAG_REDO 0x5
695#define TAG_RAISE 0x6
696#define TAG_THROW 0x7
697#define TAG_FATAL 0x8
698#define TAG_MASK 0xf
699
700 switch (state) {
701 case TAG_RETURN:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000702 EMSG(_("E267: unexpected return"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000703 break;
704 case TAG_NEXT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000705 EMSG(_("E268: unexpected next"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000706 break;
707 case TAG_BREAK:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000708 EMSG(_("E269: unexpected break"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000709 break;
710 case TAG_REDO:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000711 EMSG(_("E270: unexpected redo"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000712 break;
713 case TAG_RETRY:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000714 EMSG(_("E271: retry outside of rescue clause"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000715 break;
716 case TAG_RAISE:
717 case TAG_FATAL:
Bram Moolenaar639a2552010-03-17 18:15:23 +0100718#ifdef RUBY19_OR_LATER
Bram Moolenaar165641d2010-02-17 16:23:09 +0100719 eclass = CLASS_OF(rb_errinfo());
720 einfo = rb_obj_as_string(rb_errinfo());
721#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000722 eclass = CLASS_OF(ruby_errinfo);
723 einfo = rb_obj_as_string(ruby_errinfo);
Bram Moolenaar165641d2010-02-17 16:23:09 +0100724#endif
725 if (eclass == rb_eRuntimeError && RSTRING_LEN(einfo) == 0) {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000726 EMSG(_("E272: unhandled exception"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000727 }
728 else {
729 VALUE epath;
730 char *p;
731
732 epath = rb_class_path(eclass);
Bram Moolenaar9c13b352005-05-19 20:53:52 +0000733 vim_snprintf(buff, BUFSIZ, "%s: %s",
Bram Moolenaar165641d2010-02-17 16:23:09 +0100734 RSTRING_PTR(epath), RSTRING_PTR(einfo));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000735 p = strchr(buff, '\n');
736 if (p) *p = '\0';
737 EMSG(buff);
738 }
739 break;
740 default:
Bram Moolenaar9c13b352005-05-19 20:53:52 +0000741 vim_snprintf(buff, BUFSIZ, _("E273: unknown longjmp status %d"), state);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000742 EMSG(buff);
743 break;
744 }
745}
746
Bram Moolenaarcd8b20a2009-05-22 16:20:57 +0000747static VALUE vim_message(VALUE self UNUSED, VALUE str)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000748{
749 char *buff, *p;
750
751 str = rb_obj_as_string(str);
Bram Moolenaar165641d2010-02-17 16:23:09 +0100752 buff = ALLOCA_N(char, RSTRING_LEN(str));
753 strcpy(buff, RSTRING_PTR(str));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000754 p = strchr(buff, '\n');
755 if (p) *p = '\0';
756 MSG(buff);
757 return Qnil;
758}
759
Bram Moolenaarcd8b20a2009-05-22 16:20:57 +0000760static VALUE vim_set_option(VALUE self UNUSED, VALUE str)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000761{
Bram Moolenaar165641d2010-02-17 16:23:09 +0100762 do_set((char_u *)StringValuePtr(str), 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000763 update_screen(NOT_VALID);
764 return Qnil;
765}
766
Bram Moolenaarcd8b20a2009-05-22 16:20:57 +0000767static VALUE vim_command(VALUE self UNUSED, VALUE str)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000768{
Bram Moolenaar165641d2010-02-17 16:23:09 +0100769 do_cmdline_cmd((char_u *)StringValuePtr(str));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000770 return Qnil;
771}
772
Bram Moolenaar3fac56e2010-02-24 15:48:04 +0100773#ifdef FEAT_EVAL
774static VALUE vim_to_ruby(typval_T *tv)
775{
776 VALUE result = Qnil;
777
778 if (tv->v_type == VAR_STRING)
779 {
Bram Moolenaar94127e42010-03-19 23:08:48 +0100780 result = rb_str_new2(tv->vval.v_string == NULL
781 ? "" : (char *)(tv->vval.v_string));
Bram Moolenaar3fac56e2010-02-24 15:48:04 +0100782 }
783 else if (tv->v_type == VAR_NUMBER)
784 {
Bram Moolenaar639a2552010-03-17 18:15:23 +0100785 result = INT2NUM(tv->vval.v_number);
Bram Moolenaar3fac56e2010-02-24 15:48:04 +0100786 }
787# ifdef FEAT_FLOAT
788 else if (tv->v_type == VAR_FLOAT)
789 {
Bram Moolenaar639a2552010-03-17 18:15:23 +0100790 result = rb_float_new(tv->vval.v_float);
Bram Moolenaar3fac56e2010-02-24 15:48:04 +0100791 }
792# endif
793 else if (tv->v_type == VAR_LIST)
794 {
Bram Moolenaar639a2552010-03-17 18:15:23 +0100795 list_T *list = tv->vval.v_list;
796 listitem_T *curr;
Bram Moolenaar3fac56e2010-02-24 15:48:04 +0100797
Bram Moolenaar639a2552010-03-17 18:15:23 +0100798 result = rb_ary_new();
Bram Moolenaar3fac56e2010-02-24 15:48:04 +0100799
Bram Moolenaar639a2552010-03-17 18:15:23 +0100800 if (list != NULL)
801 {
802 for (curr = list->lv_first; curr != NULL; curr = curr->li_next)
803 {
804 rb_ary_push(result, vim_to_ruby(&curr->li_tv));
805 }
806 }
Bram Moolenaar3fac56e2010-02-24 15:48:04 +0100807 }
808 else if (tv->v_type == VAR_DICT)
809 {
Bram Moolenaar639a2552010-03-17 18:15:23 +0100810 result = rb_hash_new();
Bram Moolenaar3fac56e2010-02-24 15:48:04 +0100811
Bram Moolenaar639a2552010-03-17 18:15:23 +0100812 if (tv->vval.v_dict != NULL)
813 {
814 hashtab_T *ht = &tv->vval.v_dict->dv_hashtab;
815 long_u todo = ht->ht_used;
816 hashitem_T *hi;
817 dictitem_T *di;
Bram Moolenaar3fac56e2010-02-24 15:48:04 +0100818
Bram Moolenaar639a2552010-03-17 18:15:23 +0100819 for (hi = ht->ht_array; todo > 0; ++hi)
820 {
821 if (!HASHITEM_EMPTY(hi))
822 {
823 --todo;
Bram Moolenaar3fac56e2010-02-24 15:48:04 +0100824
Bram Moolenaar639a2552010-03-17 18:15:23 +0100825 di = dict_lookup(hi);
826 rb_hash_aset(result, rb_str_new2((char *)hi->hi_key),
Bram Moolenaar3fac56e2010-02-24 15:48:04 +0100827 vim_to_ruby(&di->di_tv));
Bram Moolenaar639a2552010-03-17 18:15:23 +0100828 }
829 }
830 }
Bram Moolenaar3fac56e2010-02-24 15:48:04 +0100831 } /* else return Qnil; */
832
833 return result;
834}
835#endif
836
Bram Moolenaarcd8b20a2009-05-22 16:20:57 +0000837static VALUE vim_evaluate(VALUE self UNUSED, VALUE str)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000838{
839#ifdef FEAT_EVAL
Bram Moolenaar3fac56e2010-02-24 15:48:04 +0100840 typval_T *tv;
841 VALUE result;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000842
Bram Moolenaar3fac56e2010-02-24 15:48:04 +0100843 tv = eval_expr((char_u *)StringValuePtr(str), NULL);
844 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000845 {
Bram Moolenaar639a2552010-03-17 18:15:23 +0100846 return Qnil;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000847 }
Bram Moolenaar3fac56e2010-02-24 15:48:04 +0100848 result = vim_to_ruby(tv);
849
850 free_tv(tv);
851
852 return result;
853#else
854 return Qnil;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000855#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000856}
857
858static VALUE buffer_new(buf_T *buf)
859{
Bram Moolenaare344bea2005-09-01 20:46:49 +0000860 if (buf->b_ruby_ref)
861 {
862 return (VALUE) buf->b_ruby_ref;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000863 }
Bram Moolenaare344bea2005-09-01 20:46:49 +0000864 else
865 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000866 VALUE obj = Data_Wrap_Struct(cBuffer, 0, 0, buf);
Bram Moolenaare344bea2005-09-01 20:46:49 +0000867 buf->b_ruby_ref = (void *) obj;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000868 rb_hash_aset(objtbl, rb_obj_id(obj), obj);
869 return obj;
870 }
871}
872
873static buf_T *get_buf(VALUE obj)
874{
875 buf_T *buf;
876
877 Data_Get_Struct(obj, buf_T, buf);
878 if (buf == NULL)
879 rb_raise(eDeletedBufferError, "attempt to refer to deleted buffer");
880 return buf;
881}
882
883static VALUE buffer_s_current()
884{
885 return buffer_new(curbuf);
886}
887
888static VALUE buffer_s_count()
889{
890 buf_T *b;
891 int n = 0;
892
Bram Moolenaarbe4d5062006-03-18 21:30:13 +0000893 for (b = firstbuf; b != NULL; b = b->b_next)
894 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000895 /* Deleted buffers should not be counted
896 * SegPhault - 01/07/05 */
897 if (b->b_p_bl)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +0000898 n++;
899 }
900
Bram Moolenaar071d4272004-06-13 20:20:40 +0000901 return INT2NUM(n);
902}
903
Bram Moolenaarcd8b20a2009-05-22 16:20:57 +0000904static VALUE buffer_s_aref(VALUE self UNUSED, VALUE num)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000905{
906 buf_T *b;
907 int n = NUM2INT(num);
908
Bram Moolenaarbe4d5062006-03-18 21:30:13 +0000909 for (b = firstbuf; b != NULL; b = b->b_next)
910 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000911 /* Deleted buffers should not be counted
912 * SegPhault - 01/07/05 */
913 if (!b->b_p_bl)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +0000914 continue;
915
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000916 if (n == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000917 return buffer_new(b);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +0000918
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000919 n--;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000920 }
921 return Qnil;
922}
923
924static VALUE buffer_name(VALUE self)
925{
926 buf_T *buf = get_buf(self);
927
Bram Moolenaar35a2e192006-03-13 22:07:11 +0000928 return buf->b_ffname ? rb_str_new2((char *)buf->b_ffname) : Qnil;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000929}
930
931static VALUE buffer_number(VALUE self)
932{
933 buf_T *buf = get_buf(self);
934
935 return INT2NUM(buf->b_fnum);
936}
937
938static VALUE buffer_count(VALUE self)
939{
940 buf_T *buf = get_buf(self);
941
942 return INT2NUM(buf->b_ml.ml_line_count);
943}
944
Bram Moolenaarbe4d5062006-03-18 21:30:13 +0000945static VALUE get_buffer_line(buf_T *buf, linenr_T n)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000946{
Bram Moolenaarbe4d5062006-03-18 21:30:13 +0000947 if (n > 0 && n <= buf->b_ml.ml_line_count)
948 {
Bram Moolenaar35a2e192006-03-13 22:07:11 +0000949 char *line = (char *)ml_get_buf(buf, n, FALSE);
Bram Moolenaar165641d2010-02-17 16:23:09 +0100950 return line ? vim_str2rb_enc_str(line) : Qnil;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000951 }
Bram Moolenaar165641d2010-02-17 16:23:09 +0100952 rb_raise(rb_eIndexError, "line number %ld out of range", (long)n);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +0000953 return Qnil; /* For stop warning */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000954}
955
Bram Moolenaarbe4d5062006-03-18 21:30:13 +0000956static VALUE buffer_aref(VALUE self, VALUE num)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000957{
958 buf_T *buf = get_buf(self);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +0000959
960 if (buf != NULL)
961 return get_buffer_line(buf, (linenr_T)NUM2LONG(num));
962 return Qnil; /* For stop warning */
963}
964
965static VALUE set_buffer_line(buf_T *buf, linenr_T n, VALUE str)
966{
Bram Moolenaar165641d2010-02-17 16:23:09 +0100967 char *line = StringValuePtr(str);
Bram Moolenaar20ff7922006-06-20 19:10:43 +0000968 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000969
Bram Moolenaar20ff7922006-06-20 19:10:43 +0000970 if (n > 0 && n <= buf->b_ml.ml_line_count && line != NULL)
971 {
Bram Moolenaar20ff7922006-06-20 19:10:43 +0000972 /* set curwin/curbuf for "buf" and save some things */
973 aucmd_prepbuf(&aco, buf);
Bram Moolenaar20ff7922006-06-20 19:10:43 +0000974
Bram Moolenaar071d4272004-06-13 20:20:40 +0000975 if (u_savesub(n) == OK) {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +0000976 ml_replace(n, (char_u *)line, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000977 changed();
978#ifdef SYNTAX_HL
979 syn_changed(n); /* recompute syntax hl. for this line */
980#endif
981 }
Bram Moolenaar20ff7922006-06-20 19:10:43 +0000982
Bram Moolenaar20ff7922006-06-20 19:10:43 +0000983 /* restore curwin/curbuf and a few other things */
984 aucmd_restbuf(&aco);
985 /* Careful: autocommands may have made "buf" invalid! */
Bram Moolenaarf30e74c2006-08-16 17:35:00 +0000986
Bram Moolenaar071d4272004-06-13 20:20:40 +0000987 update_curbuf(NOT_VALID);
988 }
Bram Moolenaar20ff7922006-06-20 19:10:43 +0000989 else
990 {
Bram Moolenaar165641d2010-02-17 16:23:09 +0100991 rb_raise(rb_eIndexError, "line number %ld out of range", (long)n);
Bram Moolenaarcd8b20a2009-05-22 16:20:57 +0000992#ifndef __GNUC__
Bram Moolenaar071d4272004-06-13 20:20:40 +0000993 return Qnil; /* For stop warning */
Bram Moolenaarcd8b20a2009-05-22 16:20:57 +0000994#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000995 }
996 return str;
997}
998
Bram Moolenaarbe4d5062006-03-18 21:30:13 +0000999static VALUE buffer_aset(VALUE self, VALUE num, VALUE str)
1000{
1001 buf_T *buf = get_buf(self);
1002
1003 if (buf != NULL)
1004 return set_buffer_line(buf, (linenr_T)NUM2LONG(num), str);
1005 return str;
1006}
1007
Bram Moolenaar071d4272004-06-13 20:20:40 +00001008static VALUE buffer_delete(VALUE self, VALUE num)
1009{
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001010 buf_T *buf = get_buf(self);
1011 long n = NUM2LONG(num);
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001012 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001013
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001014 if (n > 0 && n <= buf->b_ml.ml_line_count)
1015 {
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001016 /* set curwin/curbuf for "buf" and save some things */
1017 aucmd_prepbuf(&aco, buf);
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001018
Bram Moolenaar071d4272004-06-13 20:20:40 +00001019 if (u_savedel(n, 1) == OK) {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001020 ml_delete(n, 0);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001021
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001022 /* Changes to non-active buffers should properly refresh
1023 * SegPhault - 01/09/05 */
1024 deleted_lines_mark(n, 1L);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001025
Bram Moolenaar071d4272004-06-13 20:20:40 +00001026 changed();
1027 }
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001028
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001029 /* restore curwin/curbuf and a few other things */
1030 aucmd_restbuf(&aco);
1031 /* Careful: autocommands may have made "buf" invalid! */
Bram Moolenaarf30e74c2006-08-16 17:35:00 +00001032
Bram Moolenaar071d4272004-06-13 20:20:40 +00001033 update_curbuf(NOT_VALID);
1034 }
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001035 else
1036 {
Bram Moolenaar165641d2010-02-17 16:23:09 +01001037 rb_raise(rb_eIndexError, "line number %ld out of range", n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001038 }
1039 return Qnil;
1040}
1041
1042static VALUE buffer_append(VALUE self, VALUE num, VALUE str)
1043{
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001044 buf_T *buf = get_buf(self);
Bram Moolenaar165641d2010-02-17 16:23:09 +01001045 char *line = StringValuePtr(str);
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001046 long n = NUM2LONG(num);
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001047 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001048
Bram Moolenaar83bac8b2010-02-18 15:53:29 +01001049 if (line == NULL) {
Bram Moolenaar165641d2010-02-17 16:23:09 +01001050 rb_raise(rb_eIndexError, "NULL line");
1051 }
1052 else if (n >= 0 && n <= buf->b_ml.ml_line_count)
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001053 {
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001054 /* set curwin/curbuf for "buf" and save some things */
1055 aucmd_prepbuf(&aco, buf);
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001056
Bram Moolenaar071d4272004-06-13 20:20:40 +00001057 if (u_inssub(n + 1) == OK) {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001058 ml_append(n, (char_u *) line, (colnr_T) 0, FALSE);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001059
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001060 /* Changes to non-active buffers should properly refresh screen
1061 * SegPhault - 12/20/04 */
1062 appended_lines_mark(n, 1L);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001063
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001064 changed();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001065 }
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001066
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001067 /* restore curwin/curbuf and a few other things */
1068 aucmd_restbuf(&aco);
1069 /* Careful: autocommands may have made "buf" invalid! */
Bram Moolenaarf30e74c2006-08-16 17:35:00 +00001070
Bram Moolenaar071d4272004-06-13 20:20:40 +00001071 update_curbuf(NOT_VALID);
1072 }
1073 else {
Bram Moolenaar165641d2010-02-17 16:23:09 +01001074 rb_raise(rb_eIndexError, "line number %ld out of range", n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001075 }
1076 return str;
1077}
1078
1079static VALUE window_new(win_T *win)
1080{
Bram Moolenaare344bea2005-09-01 20:46:49 +00001081 if (win->w_ruby_ref)
1082 {
1083 return (VALUE) win->w_ruby_ref;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001084 }
Bram Moolenaare344bea2005-09-01 20:46:49 +00001085 else
1086 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001087 VALUE obj = Data_Wrap_Struct(cVimWindow, 0, 0, win);
Bram Moolenaare344bea2005-09-01 20:46:49 +00001088 win->w_ruby_ref = (void *) obj;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001089 rb_hash_aset(objtbl, rb_obj_id(obj), obj);
1090 return obj;
1091 }
1092}
1093
1094static win_T *get_win(VALUE obj)
1095{
1096 win_T *win;
1097
1098 Data_Get_Struct(obj, win_T, win);
1099 if (win == NULL)
1100 rb_raise(eDeletedWindowError, "attempt to refer to deleted window");
1101 return win;
1102}
1103
1104static VALUE window_s_current()
1105{
1106 return window_new(curwin);
1107}
1108
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001109/*
1110 * Added line manipulation functions
1111 * SegPhault - 03/07/05
1112 */
1113static VALUE line_s_current()
1114{
1115 return get_buffer_line(curbuf, curwin->w_cursor.lnum);
1116}
1117
Bram Moolenaarcd8b20a2009-05-22 16:20:57 +00001118static VALUE set_current_line(VALUE self UNUSED, VALUE str)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001119{
1120 return set_buffer_line(curbuf, curwin->w_cursor.lnum, str);
1121}
1122
1123static VALUE current_line_number()
1124{
1125 return INT2FIX((int)curwin->w_cursor.lnum);
1126}
1127
1128
1129
Bram Moolenaar071d4272004-06-13 20:20:40 +00001130static VALUE window_s_count()
1131{
1132#ifdef FEAT_WINDOWS
1133 win_T *w;
1134 int n = 0;
1135
Bram Moolenaarf740b292006-02-16 22:11:02 +00001136 for (w = firstwin; w != NULL; w = w->w_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001137 n++;
1138 return INT2NUM(n);
1139#else
1140 return INT2NUM(1);
1141#endif
1142}
1143
Bram Moolenaarcd8b20a2009-05-22 16:20:57 +00001144static VALUE window_s_aref(VALUE self UNUSED, VALUE num)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001145{
1146 win_T *w;
1147 int n = NUM2INT(num);
1148
1149#ifndef FEAT_WINDOWS
1150 w = curwin;
1151#else
1152 for (w = firstwin; w != NULL; w = w->w_next, --n)
1153#endif
1154 if (n == 0)
1155 return window_new(w);
1156 return Qnil;
1157}
1158
1159static VALUE window_buffer(VALUE self)
1160{
1161 win_T *win = get_win(self);
1162
1163 return buffer_new(win->w_buffer);
1164}
1165
1166static VALUE window_height(VALUE self)
1167{
1168 win_T *win = get_win(self);
1169
1170 return INT2NUM(win->w_height);
1171}
1172
1173static VALUE window_set_height(VALUE self, VALUE height)
1174{
1175 win_T *win = get_win(self);
1176 win_T *savewin = curwin;
1177
1178 curwin = win;
1179 win_setheight(NUM2INT(height));
1180 curwin = savewin;
1181 return height;
1182}
1183
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001184static VALUE window_width(VALUE self)
1185{
1186 win_T *win = get_win(self);
1187
1188 return INT2NUM(win->w_width);
1189}
1190
1191static VALUE window_set_width(VALUE self, VALUE width)
1192{
1193 win_T *win = get_win(self);
1194 win_T *savewin = curwin;
1195
1196 curwin = win;
1197 win_setwidth(NUM2INT(width));
1198 curwin = savewin;
1199 return width;
1200}
1201
Bram Moolenaar071d4272004-06-13 20:20:40 +00001202static VALUE window_cursor(VALUE self)
1203{
1204 win_T *win = get_win(self);
1205
1206 return rb_assoc_new(INT2NUM(win->w_cursor.lnum), INT2NUM(win->w_cursor.col));
1207}
1208
1209static VALUE window_set_cursor(VALUE self, VALUE pos)
1210{
1211 VALUE lnum, col;
1212 win_T *win = get_win(self);
1213
1214 Check_Type(pos, T_ARRAY);
Bram Moolenaar165641d2010-02-17 16:23:09 +01001215 if (RARRAY_LEN(pos) != 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001216 rb_raise(rb_eArgError, "array length must be 2");
Bram Moolenaar165641d2010-02-17 16:23:09 +01001217 lnum = RARRAY_PTR(pos)[0];
1218 col = RARRAY_PTR(pos)[1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001219 win->w_cursor.lnum = NUM2LONG(lnum);
1220 win->w_cursor.col = NUM2UINT(col);
1221 check_cursor(); /* put cursor on an existing line */
1222 update_screen(NOT_VALID);
1223 return Qnil;
1224}
1225
Bram Moolenaarcd8b20a2009-05-22 16:20:57 +00001226static VALUE f_p(int argc, VALUE *argv, VALUE self UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001227{
1228 int i;
1229 VALUE str = rb_str_new("", 0);
1230
1231 for (i = 0; i < argc; i++) {
1232 if (i > 0) rb_str_cat(str, ", ", 2);
1233 rb_str_concat(str, rb_inspect(argv[i]));
1234 }
Bram Moolenaar165641d2010-02-17 16:23:09 +01001235 MSG(RSTRING_PTR(str));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001236 return Qnil;
1237}
1238
1239static void ruby_io_init(void)
1240{
1241#ifndef DYNAMIC_RUBY
1242 RUBYEXTERN VALUE rb_stdout;
1243#endif
1244
1245 rb_stdout = rb_obj_alloc(rb_cObject);
1246 rb_define_singleton_method(rb_stdout, "write", vim_message, 1);
1247 rb_define_global_function("p", f_p, -1);
1248}
1249
1250static void ruby_vim_init(void)
1251{
1252 objtbl = rb_hash_new();
1253 rb_global_variable(&objtbl);
1254
Bram Moolenaarf711faf2007-05-10 16:48:19 +00001255 /* The Vim module used to be called "VIM", but "Vim" is better. Make an
1256 * alias "VIM" for backwards compatiblity. */
1257 mVIM = rb_define_module("Vim");
1258 rb_define_const(rb_cObject, "VIM", mVIM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001259 rb_define_const(mVIM, "VERSION_MAJOR", INT2NUM(VIM_VERSION_MAJOR));
1260 rb_define_const(mVIM, "VERSION_MINOR", INT2NUM(VIM_VERSION_MINOR));
1261 rb_define_const(mVIM, "VERSION_BUILD", INT2NUM(VIM_VERSION_BUILD));
1262 rb_define_const(mVIM, "VERSION_PATCHLEVEL", INT2NUM(VIM_VERSION_PATCHLEVEL));
1263 rb_define_const(mVIM, "VERSION_SHORT", rb_str_new2(VIM_VERSION_SHORT));
1264 rb_define_const(mVIM, "VERSION_MEDIUM", rb_str_new2(VIM_VERSION_MEDIUM));
1265 rb_define_const(mVIM, "VERSION_LONG", rb_str_new2(VIM_VERSION_LONG));
1266 rb_define_const(mVIM, "VERSION_LONG_DATE", rb_str_new2(VIM_VERSION_LONG_DATE));
1267 rb_define_module_function(mVIM, "message", vim_message, 1);
1268 rb_define_module_function(mVIM, "set_option", vim_set_option, 1);
1269 rb_define_module_function(mVIM, "command", vim_command, 1);
1270 rb_define_module_function(mVIM, "evaluate", vim_evaluate, 1);
1271
1272 eDeletedBufferError = rb_define_class_under(mVIM, "DeletedBufferError",
1273 rb_eStandardError);
1274 eDeletedWindowError = rb_define_class_under(mVIM, "DeletedWindowError",
1275 rb_eStandardError);
1276
1277 cBuffer = rb_define_class_under(mVIM, "Buffer", rb_cObject);
1278 rb_define_singleton_method(cBuffer, "current", buffer_s_current, 0);
1279 rb_define_singleton_method(cBuffer, "count", buffer_s_count, 0);
1280 rb_define_singleton_method(cBuffer, "[]", buffer_s_aref, 1);
1281 rb_define_method(cBuffer, "name", buffer_name, 0);
1282 rb_define_method(cBuffer, "number", buffer_number, 0);
1283 rb_define_method(cBuffer, "count", buffer_count, 0);
1284 rb_define_method(cBuffer, "length", buffer_count, 0);
1285 rb_define_method(cBuffer, "[]", buffer_aref, 1);
1286 rb_define_method(cBuffer, "[]=", buffer_aset, 2);
1287 rb_define_method(cBuffer, "delete", buffer_delete, 1);
1288 rb_define_method(cBuffer, "append", buffer_append, 2);
1289
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001290 /* Added line manipulation functions
1291 * SegPhault - 03/07/05 */
1292 rb_define_method(cBuffer, "line_number", current_line_number, 0);
1293 rb_define_method(cBuffer, "line", line_s_current, 0);
1294 rb_define_method(cBuffer, "line=", set_current_line, 1);
1295
1296
Bram Moolenaar071d4272004-06-13 20:20:40 +00001297 cVimWindow = rb_define_class_under(mVIM, "Window", rb_cObject);
1298 rb_define_singleton_method(cVimWindow, "current", window_s_current, 0);
1299 rb_define_singleton_method(cVimWindow, "count", window_s_count, 0);
1300 rb_define_singleton_method(cVimWindow, "[]", window_s_aref, 1);
1301 rb_define_method(cVimWindow, "buffer", window_buffer, 0);
1302 rb_define_method(cVimWindow, "height", window_height, 0);
1303 rb_define_method(cVimWindow, "height=", window_set_height, 1);
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001304 rb_define_method(cVimWindow, "width", window_width, 0);
1305 rb_define_method(cVimWindow, "width=", window_set_width, 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001306 rb_define_method(cVimWindow, "cursor", window_cursor, 0);
1307 rb_define_method(cVimWindow, "cursor=", window_set_cursor, 1);
1308
1309 rb_define_virtual_variable("$curbuf", buffer_s_current, 0);
1310 rb_define_virtual_variable("$curwin", window_s_current, 0);
1311}