blob: d86f6be4b2bfc20c8fb10a76987e11cab8584f74 [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
Bram Moolenaar3c531602010-11-16 14:46:19 +0100232# define rb_intern2 dll_rb_intern2
233# define rb_const_remove dll_rb_const_remove
234# define Init_prelude dll_Init_prelude
Bram Moolenaar165641d2010-02-17 16:23:09 +0100235# define rb_sprintf dll_rb_sprintf
Bram Moolenaar639a2552010-03-17 18:15:23 +0100236# define ruby_init_stack dll_ruby_init_stack
Bram Moolenaar165641d2010-02-17 16:23:09 +0100237#endif
238
Bram Moolenaar071d4272004-06-13 20:20:40 +0000239/*
240 * Pointers for dynamic link
241 */
242static VALUE (*dll_rb_assoc_new) (VALUE, VALUE);
Bram Moolenaarba52cde2010-06-25 04:29:11 +0200243VALUE *dll_rb_cFalseClass;
244VALUE *dll_rb_cFixnum;
245VALUE *dll_rb_cNilClass;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000246static VALUE *dll_rb_cObject;
Bram Moolenaarba52cde2010-06-25 04:29:11 +0200247VALUE *dll_rb_cSymbol;
248VALUE *dll_rb_cTrueClass;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000249static void (*dll_rb_check_type) (VALUE,int);
250static VALUE (*dll_rb_class_path) (VALUE);
251static VALUE (*dll_rb_data_object_alloc) (VALUE, void*, RUBY_DATA_FUNC, RUBY_DATA_FUNC);
252static VALUE (*dll_rb_define_class_under) (VALUE, const char*, VALUE);
253static void (*dll_rb_define_const) (VALUE,const char*,VALUE);
254static void (*dll_rb_define_global_function) (const char*,VALUE(*)(),int);
255static void (*dll_rb_define_method) (VALUE,const char*,VALUE(*)(),int);
256static VALUE (*dll_rb_define_module) (const char*);
257static void (*dll_rb_define_module_function) (VALUE,const char*,VALUE(*)(),int);
258static void (*dll_rb_define_singleton_method) (VALUE,const char*,VALUE(*)(),int);
259static void (*dll_rb_define_virtual_variable) (const char*,VALUE(*)(),void(*)());
260static VALUE *dll_rb_stdout;
261static VALUE *dll_rb_eArgError;
262static VALUE *dll_rb_eIndexError;
263static VALUE *dll_rb_eRuntimeError;
264static VALUE *dll_rb_eStandardError;
265static VALUE (*dll_rb_eval_string_protect) (const char*, int*);
266static void (*dll_rb_global_variable) (VALUE*);
267static VALUE (*dll_rb_hash_aset) (VALUE, VALUE, VALUE);
268static VALUE (*dll_rb_hash_new) (void);
269static VALUE (*dll_rb_inspect) (VALUE);
270static VALUE (*dll_rb_int2inum) (long);
271static VALUE (*dll_rb_int2inum) (long);
272static VALUE (*dll_rb_lastline_get) (void);
273static void (*dll_rb_lastline_set) (VALUE);
274static void (*dll_rb_load_protect) (VALUE, int, int*);
275static long (*dll_rb_num2long) (VALUE);
276static unsigned long (*dll_rb_num2ulong) (VALUE);
277static VALUE (*dll_rb_obj_alloc) (VALUE);
278static VALUE (*dll_rb_obj_as_string) (VALUE);
279static VALUE (*dll_rb_obj_id) (VALUE);
280static void (*dll_rb_raise) (VALUE, const char*, ...);
Bram Moolenaarf9b5ef82010-09-29 13:02:53 +0200281#if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 18
282static VALUE (*dll_rb_string_value) (volatile VALUE*);
283#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000284static char *(*dll_rb_str2cstr) (VALUE,int*);
Bram Moolenaarf9b5ef82010-09-29 13:02:53 +0200285#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000286static VALUE (*dll_rb_str_cat) (VALUE, const char*, long);
287static VALUE (*dll_rb_str_concat) (VALUE, VALUE);
288static VALUE (*dll_rb_str_new) (const char*, long);
Bram Moolenaar1d2beae2010-05-22 21:56:55 +0200289#ifdef need_rb_str_new_cstr
290/* Ruby may #define rb_str_new2 to use rb_str_new_cstr. */
291static VALUE (*dll_rb_str_new_cstr) (const char*);
292#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000293static VALUE (*dll_rb_str_new2) (const char*);
Bram Moolenaar1d2beae2010-05-22 21:56:55 +0200294#endif
Bram Moolenaar639a2552010-03-17 18:15:23 +0100295#ifdef RUBY19_OR_LATER
Bram Moolenaar165641d2010-02-17 16:23:09 +0100296static VALUE (*dll_rb_errinfo) (void);
297#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000298static VALUE *dll_ruby_errinfo;
Bram Moolenaar165641d2010-02-17 16:23:09 +0100299#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000300static void (*dll_ruby_init) (void);
301static void (*dll_ruby_init_loadpath) (void);
Bram Moolenaar3ca71f12010-10-27 16:49:47 +0200302#ifdef WIN3264
Bram Moolenaar0b69c732010-02-17 15:11:50 +0100303static void (*dll_NtInitialize) (int*, char***);
Bram Moolenaar3ca71f12010-10-27 16:49:47 +0200304# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 18
305static int (*dll_rb_w32_snprintf)(char*, size_t, const char*, ...);
306# endif
307#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000308#if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 18
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100309static char * (*dll_rb_string_value_ptr) (volatile VALUE*);
310static VALUE (*dll_rb_float_new) (double);
311static VALUE (*dll_rb_ary_new) (void);
312static VALUE (*dll_rb_ary_push) (VALUE, VALUE);
313#endif
Bram Moolenaar639a2552010-03-17 18:15:23 +0100314#ifdef RUBY19_OR_LATER
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100315static VALUE (*dll_rb_int2big)(SIGNED_VALUE);
316#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000317
Bram Moolenaar639a2552010-03-17 18:15:23 +0100318#ifdef RUBY19_OR_LATER
Bram Moolenaar165641d2010-02-17 16:23:09 +0100319static void (*dll_ruby_script) (const char*);
320static int (*dll_rb_enc_find_index) (const char*);
321static rb_encoding* (*dll_rb_enc_find) (const char*);
322static VALUE (*dll_rb_enc_str_new) (const char*, long, rb_encoding*);
Bram Moolenaar3c531602010-11-16 14:46:19 +0100323static ID (*dll_rb_intern2) (const char*, long);
324static void (*dll_Init_prelude) (void);
325static VALUE (*dll_rb_const_remove) (VALUE, ID);
Bram Moolenaar165641d2010-02-17 16:23:09 +0100326static VALUE (*dll_rb_sprintf) (const char*, ...);
Bram Moolenaar639a2552010-03-17 18:15:23 +0100327static void (*ruby_init_stack)(VALUE*);
Bram Moolenaar165641d2010-02-17 16:23:09 +0100328#endif
329
Bram Moolenaar639a2552010-03-17 18:15:23 +0100330#ifdef RUBY19_OR_LATER
Bram Moolenaarba52cde2010-06-25 04:29:11 +0200331SIGNED_VALUE rb_num2long_stub(VALUE x)
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100332{
333 return dll_rb_num2long(x);
334}
Bram Moolenaarba52cde2010-06-25 04:29:11 +0200335VALUE rb_int2big_stub(SIGNED_VALUE x)
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100336{
337 return dll_rb_int2big(x);
338}
339#endif
340
Bram Moolenaar3ca71f12010-10-27 16:49:47 +0200341static HINSTANCE hinstRuby = NULL; /* Instance of ruby.dll */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000342
343/*
Bram Moolenaarda2303d2005-08-30 21:55:26 +0000344 * Table of name to function pointer of ruby.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000345 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000346static struct
347{
348 char *name;
349 RUBY_PROC *ptr;
350} ruby_funcname_table[] =
351{
352 {"rb_assoc_new", (RUBY_PROC*)&dll_rb_assoc_new},
353 {"rb_cFalseClass", (RUBY_PROC*)&dll_rb_cFalseClass},
354 {"rb_cFixnum", (RUBY_PROC*)&dll_rb_cFixnum},
355 {"rb_cNilClass", (RUBY_PROC*)&dll_rb_cNilClass},
356 {"rb_cObject", (RUBY_PROC*)&dll_rb_cObject},
357 {"rb_cSymbol", (RUBY_PROC*)&dll_rb_cSymbol},
358 {"rb_cTrueClass", (RUBY_PROC*)&dll_rb_cTrueClass},
359 {"rb_check_type", (RUBY_PROC*)&dll_rb_check_type},
360 {"rb_class_path", (RUBY_PROC*)&dll_rb_class_path},
361 {"rb_data_object_alloc", (RUBY_PROC*)&dll_rb_data_object_alloc},
362 {"rb_define_class_under", (RUBY_PROC*)&dll_rb_define_class_under},
363 {"rb_define_const", (RUBY_PROC*)&dll_rb_define_const},
364 {"rb_define_global_function", (RUBY_PROC*)&dll_rb_define_global_function},
365 {"rb_define_method", (RUBY_PROC*)&dll_rb_define_method},
366 {"rb_define_module", (RUBY_PROC*)&dll_rb_define_module},
367 {"rb_define_module_function", (RUBY_PROC*)&dll_rb_define_module_function},
368 {"rb_define_singleton_method", (RUBY_PROC*)&dll_rb_define_singleton_method},
369 {"rb_define_virtual_variable", (RUBY_PROC*)&dll_rb_define_virtual_variable},
370 {"rb_stdout", (RUBY_PROC*)&dll_rb_stdout},
371 {"rb_eArgError", (RUBY_PROC*)&dll_rb_eArgError},
372 {"rb_eIndexError", (RUBY_PROC*)&dll_rb_eIndexError},
373 {"rb_eRuntimeError", (RUBY_PROC*)&dll_rb_eRuntimeError},
374 {"rb_eStandardError", (RUBY_PROC*)&dll_rb_eStandardError},
375 {"rb_eval_string_protect", (RUBY_PROC*)&dll_rb_eval_string_protect},
376 {"rb_global_variable", (RUBY_PROC*)&dll_rb_global_variable},
377 {"rb_hash_aset", (RUBY_PROC*)&dll_rb_hash_aset},
378 {"rb_hash_new", (RUBY_PROC*)&dll_rb_hash_new},
379 {"rb_inspect", (RUBY_PROC*)&dll_rb_inspect},
380 {"rb_int2inum", (RUBY_PROC*)&dll_rb_int2inum},
381 {"rb_lastline_get", (RUBY_PROC*)&dll_rb_lastline_get},
382 {"rb_lastline_set", (RUBY_PROC*)&dll_rb_lastline_set},
383 {"rb_load_protect", (RUBY_PROC*)&dll_rb_load_protect},
384 {"rb_num2long", (RUBY_PROC*)&dll_rb_num2long},
385 {"rb_num2ulong", (RUBY_PROC*)&dll_rb_num2ulong},
386 {"rb_obj_alloc", (RUBY_PROC*)&dll_rb_obj_alloc},
387 {"rb_obj_as_string", (RUBY_PROC*)&dll_rb_obj_as_string},
388 {"rb_obj_id", (RUBY_PROC*)&dll_rb_obj_id},
389 {"rb_raise", (RUBY_PROC*)&dll_rb_raise},
Bram Moolenaarf9b5ef82010-09-29 13:02:53 +0200390#if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 18
391 {"rb_string_value", (RUBY_PROC*)&dll_rb_string_value},
392#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000393 {"rb_str2cstr", (RUBY_PROC*)&dll_rb_str2cstr},
Bram Moolenaarf9b5ef82010-09-29 13:02:53 +0200394#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000395 {"rb_str_cat", (RUBY_PROC*)&dll_rb_str_cat},
396 {"rb_str_concat", (RUBY_PROC*)&dll_rb_str_concat},
397 {"rb_str_new", (RUBY_PROC*)&dll_rb_str_new},
Bram Moolenaar1d2beae2010-05-22 21:56:55 +0200398#ifdef need_rb_str_new_cstr
399 {"rb_str_new_cstr", (RUBY_PROC*)&dll_rb_str_new_cstr},
400#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000401 {"rb_str_new2", (RUBY_PROC*)&dll_rb_str_new2},
Bram Moolenaar1d2beae2010-05-22 21:56:55 +0200402#endif
Bram Moolenaar639a2552010-03-17 18:15:23 +0100403#ifdef RUBY19_OR_LATER
Bram Moolenaar165641d2010-02-17 16:23:09 +0100404 {"rb_errinfo", (RUBY_PROC*)&dll_rb_errinfo},
405#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000406 {"ruby_errinfo", (RUBY_PROC*)&dll_ruby_errinfo},
Bram Moolenaar165641d2010-02-17 16:23:09 +0100407#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000408 {"ruby_init", (RUBY_PROC*)&dll_ruby_init},
409 {"ruby_init_loadpath", (RUBY_PROC*)&dll_ruby_init_loadpath},
Bram Moolenaar3ca71f12010-10-27 16:49:47 +0200410#ifdef WIN3264
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100411 {
Bram Moolenaar3ca71f12010-10-27 16:49:47 +0200412# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER < 19
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100413 "NtInitialize",
Bram Moolenaar3ca71f12010-10-27 16:49:47 +0200414# else
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100415 "ruby_sysinit",
Bram Moolenaar3ca71f12010-10-27 16:49:47 +0200416# endif
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100417 (RUBY_PROC*)&dll_NtInitialize},
Bram Moolenaar3ca71f12010-10-27 16:49:47 +0200418# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 18
Bram Moolenaar071d4272004-06-13 20:20:40 +0000419 {"rb_w32_snprintf", (RUBY_PROC*)&dll_rb_w32_snprintf},
Bram Moolenaar3ca71f12010-10-27 16:49:47 +0200420# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000421#endif
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100422#if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 18
423 {"rb_string_value_ptr", (RUBY_PROC*)&dll_rb_string_value_ptr},
424 {"rb_float_new", (RUBY_PROC*)&dll_rb_float_new},
425 {"rb_ary_new", (RUBY_PROC*)&dll_rb_ary_new},
426 {"rb_ary_push", (RUBY_PROC*)&dll_rb_ary_push},
427#endif
Bram Moolenaar639a2552010-03-17 18:15:23 +0100428#ifdef RUBY19_OR_LATER
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100429 {"rb_int2big", (RUBY_PROC*)&dll_rb_int2big},
Bram Moolenaar165641d2010-02-17 16:23:09 +0100430 {"ruby_script", (RUBY_PROC*)&dll_ruby_script},
431 {"rb_enc_find_index", (RUBY_PROC*)&dll_rb_enc_find_index},
432 {"rb_enc_find", (RUBY_PROC*)&dll_rb_enc_find},
433 {"rb_enc_str_new", (RUBY_PROC*)&dll_rb_enc_str_new},
Bram Moolenaar3c531602010-11-16 14:46:19 +0100434 {"rb_intern2", (RUBY_PROC*)&dll_rb_intern2},
435 {"rb_const_remove", (RUBY_PROC*)&dll_rb_const_remove},
436 {"Init_prelude", (RUBY_PROC*)&dll_Init_prelude},
Bram Moolenaar165641d2010-02-17 16:23:09 +0100437 {"rb_sprintf", (RUBY_PROC*)&dll_rb_sprintf},
Bram Moolenaar639a2552010-03-17 18:15:23 +0100438 {"ruby_init_stack", (RUBY_PROC*)&dll_ruby_init_stack},
Bram Moolenaar165641d2010-02-17 16:23:09 +0100439#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000440 {"", NULL},
441};
442
443/*
444 * Free ruby.dll
445 */
446 static void
447end_dynamic_ruby()
448{
449 if (hinstRuby)
450 {
Bram Moolenaarf9b5ef82010-09-29 13:02:53 +0200451 close_dll(hinstRuby);
Bram Moolenaar3ca71f12010-10-27 16:49:47 +0200452 hinstRuby = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000453 }
454}
455
456/*
457 * Load library and get all pointers.
458 * Parameter 'libname' provides name of DLL.
459 * Return OK or FAIL.
460 */
461 static int
462ruby_runtime_link_init(char *libname, int verbose)
463{
464 int i;
465
466 if (hinstRuby)
467 return OK;
Bram Moolenaarf9b5ef82010-09-29 13:02:53 +0200468 hinstRuby = load_dll(libname);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000469 if (!hinstRuby)
470 {
471 if (verbose)
472 EMSG2(_(e_loadlib), libname);
473 return FAIL;
474 }
475
476 for (i = 0; ruby_funcname_table[i].ptr; ++i)
477 {
Bram Moolenaarf9b5ef82010-09-29 13:02:53 +0200478 if (!(*ruby_funcname_table[i].ptr = symbol_from_dll(hinstRuby,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000479 ruby_funcname_table[i].name)))
480 {
Bram Moolenaarf9b5ef82010-09-29 13:02:53 +0200481 close_dll(hinstRuby);
Bram Moolenaar3ca71f12010-10-27 16:49:47 +0200482 hinstRuby = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000483 if (verbose)
484 EMSG2(_(e_loadfunc), ruby_funcname_table[i].name);
485 return FAIL;
486 }
487 }
488 return OK;
489}
490
491/*
492 * If ruby is enabled (there is installed ruby on Windows system) return TRUE,
493 * else FALSE.
494 */
495 int
496ruby_enabled(verbose)
497 int verbose;
498{
499 return ruby_runtime_link_init(DYNAMIC_RUBY_DLL, verbose) == OK;
500}
501#endif /* defined(DYNAMIC_RUBY) || defined(PROTO) */
502
503 void
504ruby_end()
505{
506#ifdef DYNAMIC_RUBY
507 end_dynamic_ruby();
508#endif
509}
510
511void ex_ruby(exarg_T *eap)
512{
513 int state;
514 char *script = NULL;
515
Bram Moolenaar35a2e192006-03-13 22:07:11 +0000516 script = (char *)script_get(eap, eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000517 if (!eap->skip && ensure_ruby_initialized())
518 {
519 if (script == NULL)
520 rb_eval_string_protect((char *)eap->arg, &state);
521 else
522 rb_eval_string_protect(script, &state);
523 if (state)
524 error_print(state);
525 }
526 vim_free(script);
527}
528
Bram Moolenaar165641d2010-02-17 16:23:09 +0100529/*
530 * In Ruby 1.9 or later, ruby String object has encoding.
531 * conversion buffer string of vim to ruby String object using
532 * VIM encoding option.
533 */
534 static VALUE
535vim_str2rb_enc_str(const char *s)
536{
Bram Moolenaar639a2552010-03-17 18:15:23 +0100537#ifdef RUBY19_OR_LATER
Bram Moolenaar165641d2010-02-17 16:23:09 +0100538 int isnum;
539 long lval;
540 char_u *sval;
541 rb_encoding *enc;
542
543 isnum = get_option_value((char_u *)"enc", &lval, &sval, 0);
544 if (isnum == 0)
545 {
546 enc = rb_enc_find((char *)sval);
547 vim_free(sval);
548 if (enc) {
549 return rb_enc_str_new(s, strlen(s), enc);
550 }
551 }
552#endif
553 return rb_str_new2(s);
554}
555
556 static VALUE
557eval_enc_string_protect(const char *str, int *state)
558{
Bram Moolenaar639a2552010-03-17 18:15:23 +0100559#ifdef RUBY19_OR_LATER
Bram Moolenaar165641d2010-02-17 16:23:09 +0100560 int isnum;
561 long lval;
562 char_u *sval;
563 rb_encoding *enc;
564 VALUE v;
565
566 isnum = get_option_value((char_u *)"enc", &lval, &sval, 0);
567 if (isnum == 0)
568 {
569 enc = rb_enc_find((char *)sval);
570 vim_free(sval);
571 if (enc)
572 {
573 v = rb_sprintf("#-*- coding:%s -*-\n%s", rb_enc_name(enc), str);
574 return rb_eval_string_protect(StringValuePtr(v), state);
575 }
576 }
577#endif
578 return rb_eval_string_protect(str, state);
579}
580
Bram Moolenaar071d4272004-06-13 20:20:40 +0000581void ex_rubydo(exarg_T *eap)
582{
583 int state;
584 linenr_T i;
585
586 if (ensure_ruby_initialized())
587 {
588 if (u_save(eap->line1 - 1, eap->line2 + 1) != OK)
589 return;
590 for (i = eap->line1; i <= eap->line2; i++) {
591 VALUE line, oldline;
592
Bram Moolenaar165641d2010-02-17 16:23:09 +0100593 line = oldline = vim_str2rb_enc_str((char *)ml_get(i));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000594 rb_lastline_set(line);
Bram Moolenaar165641d2010-02-17 16:23:09 +0100595 eval_enc_string_protect((char *) eap->arg, &state);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000596 if (state) {
597 error_print(state);
598 break;
599 }
600 line = rb_lastline_get();
601 if (!NIL_P(line)) {
602 if (TYPE(line) != T_STRING) {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000603 EMSG(_("E265: $_ must be an instance of String"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000604 return;
605 }
Bram Moolenaar165641d2010-02-17 16:23:09 +0100606 ml_replace(i, (char_u *) StringValuePtr(line), 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000607 changed();
608#ifdef SYNTAX_HL
609 syn_changed(i); /* recompute syntax hl. for this line */
610#endif
611 }
612 }
613 check_cursor();
614 update_curbuf(NOT_VALID);
615 }
616}
617
618void ex_rubyfile(exarg_T *eap)
619{
620 int state;
621
622 if (ensure_ruby_initialized())
623 {
624 rb_load_protect(rb_str_new2((char *) eap->arg), 0, &state);
625 if (state) error_print(state);
626 }
627}
628
629void ruby_buffer_free(buf_T *buf)
630{
Bram Moolenaare344bea2005-09-01 20:46:49 +0000631 if (buf->b_ruby_ref)
632 {
633 rb_hash_aset(objtbl, rb_obj_id((VALUE) buf->b_ruby_ref), Qnil);
634 RDATA(buf->b_ruby_ref)->data = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000635 }
636}
637
638void ruby_window_free(win_T *win)
639{
Bram Moolenaare344bea2005-09-01 20:46:49 +0000640 if (win->w_ruby_ref)
641 {
642 rb_hash_aset(objtbl, rb_obj_id((VALUE) win->w_ruby_ref), Qnil);
643 RDATA(win->w_ruby_ref)->data = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000644 }
645}
646
647static int ensure_ruby_initialized(void)
648{
649 if (!ruby_initialized)
650 {
651#ifdef DYNAMIC_RUBY
652 if (ruby_enabled(TRUE))
653 {
654#endif
Bram Moolenaar0b69c732010-02-17 15:11:50 +0100655#ifdef _WIN32
656 /* suggested by Ariya Mizutani */
657 int argc = 1;
658 char *argv[] = {"gvim.exe"};
659 NtInitialize(&argc, &argv);
660#endif
Bram Moolenaarb2c03502010-07-02 20:20:09 +0200661 {
Bram Moolenaar639a2552010-03-17 18:15:23 +0100662#ifdef RUBY19_OR_LATER
Bram Moolenaarb2c03502010-07-02 20:20:09 +0200663 RUBY_INIT_STACK;
Bram Moolenaar165641d2010-02-17 16:23:09 +0100664#endif
Bram Moolenaarb2c03502010-07-02 20:20:09 +0200665 ruby_init();
666 }
Bram Moolenaar639a2552010-03-17 18:15:23 +0100667#ifdef RUBY19_OR_LATER
Bram Moolenaar165641d2010-02-17 16:23:09 +0100668 ruby_script("vim-ruby");
669#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000670 ruby_init_loadpath();
671 ruby_io_init();
Bram Moolenaar639a2552010-03-17 18:15:23 +0100672#ifdef RUBY19_OR_LATER
Bram Moolenaar165641d2010-02-17 16:23:09 +0100673 rb_enc_find_index("encdb");
Bram Moolenaar3c531602010-11-16 14:46:19 +0100674
675 /* This avoids the error "Encoding::ConverterNotFoundError: code
676 * converter not found (UTF-16LE to ASCII-8BIT)". */
677 rb_define_module("Gem");
678 Init_prelude();
679 rb_const_remove(rb_cObject, rb_intern2("TMP_RUBY_PREFIX", 15));
Bram Moolenaar165641d2010-02-17 16:23:09 +0100680#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000681 ruby_vim_init();
682 ruby_initialized = 1;
683#ifdef DYNAMIC_RUBY
684 }
685 else
686 {
687 EMSG(_("E266: Sorry, this command is disabled, the Ruby library could not be loaded."));
688 return 0;
689 }
690#endif
691 }
692 return ruby_initialized;
693}
694
695static void error_print(int state)
696{
697#ifndef DYNAMIC_RUBY
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100698#if !(defined(RUBY_VERSION) && RUBY_VERSION >= 19) \
699 && !(defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 19)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000700 RUBYEXTERN VALUE ruby_errinfo;
701#endif
Bram Moolenaar165641d2010-02-17 16:23:09 +0100702#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000703 VALUE eclass;
704 VALUE einfo;
705 char buff[BUFSIZ];
706
707#define TAG_RETURN 0x1
708#define TAG_BREAK 0x2
709#define TAG_NEXT 0x3
710#define TAG_RETRY 0x4
711#define TAG_REDO 0x5
712#define TAG_RAISE 0x6
713#define TAG_THROW 0x7
714#define TAG_FATAL 0x8
715#define TAG_MASK 0xf
716
717 switch (state) {
718 case TAG_RETURN:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000719 EMSG(_("E267: unexpected return"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000720 break;
721 case TAG_NEXT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000722 EMSG(_("E268: unexpected next"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000723 break;
724 case TAG_BREAK:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000725 EMSG(_("E269: unexpected break"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000726 break;
727 case TAG_REDO:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000728 EMSG(_("E270: unexpected redo"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000729 break;
730 case TAG_RETRY:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000731 EMSG(_("E271: retry outside of rescue clause"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000732 break;
733 case TAG_RAISE:
734 case TAG_FATAL:
Bram Moolenaar639a2552010-03-17 18:15:23 +0100735#ifdef RUBY19_OR_LATER
Bram Moolenaar165641d2010-02-17 16:23:09 +0100736 eclass = CLASS_OF(rb_errinfo());
737 einfo = rb_obj_as_string(rb_errinfo());
738#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000739 eclass = CLASS_OF(ruby_errinfo);
740 einfo = rb_obj_as_string(ruby_errinfo);
Bram Moolenaar165641d2010-02-17 16:23:09 +0100741#endif
742 if (eclass == rb_eRuntimeError && RSTRING_LEN(einfo) == 0) {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000743 EMSG(_("E272: unhandled exception"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000744 }
745 else {
746 VALUE epath;
747 char *p;
748
749 epath = rb_class_path(eclass);
Bram Moolenaar9c13b352005-05-19 20:53:52 +0000750 vim_snprintf(buff, BUFSIZ, "%s: %s",
Bram Moolenaar165641d2010-02-17 16:23:09 +0100751 RSTRING_PTR(epath), RSTRING_PTR(einfo));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000752 p = strchr(buff, '\n');
753 if (p) *p = '\0';
754 EMSG(buff);
755 }
756 break;
757 default:
Bram Moolenaar9c13b352005-05-19 20:53:52 +0000758 vim_snprintf(buff, BUFSIZ, _("E273: unknown longjmp status %d"), state);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000759 EMSG(buff);
760 break;
761 }
762}
763
Bram Moolenaarcd8b20a2009-05-22 16:20:57 +0000764static VALUE vim_message(VALUE self UNUSED, VALUE str)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000765{
766 char *buff, *p;
767
768 str = rb_obj_as_string(str);
Bram Moolenaar165641d2010-02-17 16:23:09 +0100769 buff = ALLOCA_N(char, RSTRING_LEN(str));
770 strcpy(buff, RSTRING_PTR(str));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000771 p = strchr(buff, '\n');
772 if (p) *p = '\0';
773 MSG(buff);
774 return Qnil;
775}
776
Bram Moolenaarcd8b20a2009-05-22 16:20:57 +0000777static VALUE vim_set_option(VALUE self UNUSED, VALUE str)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000778{
Bram Moolenaar165641d2010-02-17 16:23:09 +0100779 do_set((char_u *)StringValuePtr(str), 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000780 update_screen(NOT_VALID);
781 return Qnil;
782}
783
Bram Moolenaarcd8b20a2009-05-22 16:20:57 +0000784static VALUE vim_command(VALUE self UNUSED, VALUE str)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000785{
Bram Moolenaar165641d2010-02-17 16:23:09 +0100786 do_cmdline_cmd((char_u *)StringValuePtr(str));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000787 return Qnil;
788}
789
Bram Moolenaar3fac56e2010-02-24 15:48:04 +0100790#ifdef FEAT_EVAL
791static VALUE vim_to_ruby(typval_T *tv)
792{
793 VALUE result = Qnil;
794
795 if (tv->v_type == VAR_STRING)
796 {
Bram Moolenaar94127e42010-03-19 23:08:48 +0100797 result = rb_str_new2(tv->vval.v_string == NULL
798 ? "" : (char *)(tv->vval.v_string));
Bram Moolenaar3fac56e2010-02-24 15:48:04 +0100799 }
800 else if (tv->v_type == VAR_NUMBER)
801 {
Bram Moolenaar639a2552010-03-17 18:15:23 +0100802 result = INT2NUM(tv->vval.v_number);
Bram Moolenaar3fac56e2010-02-24 15:48:04 +0100803 }
804# ifdef FEAT_FLOAT
805 else if (tv->v_type == VAR_FLOAT)
806 {
Bram Moolenaar639a2552010-03-17 18:15:23 +0100807 result = rb_float_new(tv->vval.v_float);
Bram Moolenaar3fac56e2010-02-24 15:48:04 +0100808 }
809# endif
810 else if (tv->v_type == VAR_LIST)
811 {
Bram Moolenaar639a2552010-03-17 18:15:23 +0100812 list_T *list = tv->vval.v_list;
813 listitem_T *curr;
Bram Moolenaar3fac56e2010-02-24 15:48:04 +0100814
Bram Moolenaar639a2552010-03-17 18:15:23 +0100815 result = rb_ary_new();
Bram Moolenaar3fac56e2010-02-24 15:48:04 +0100816
Bram Moolenaar639a2552010-03-17 18:15:23 +0100817 if (list != NULL)
818 {
819 for (curr = list->lv_first; curr != NULL; curr = curr->li_next)
820 {
821 rb_ary_push(result, vim_to_ruby(&curr->li_tv));
822 }
823 }
Bram Moolenaar3fac56e2010-02-24 15:48:04 +0100824 }
825 else if (tv->v_type == VAR_DICT)
826 {
Bram Moolenaar639a2552010-03-17 18:15:23 +0100827 result = rb_hash_new();
Bram Moolenaar3fac56e2010-02-24 15:48:04 +0100828
Bram Moolenaar639a2552010-03-17 18:15:23 +0100829 if (tv->vval.v_dict != NULL)
830 {
831 hashtab_T *ht = &tv->vval.v_dict->dv_hashtab;
832 long_u todo = ht->ht_used;
833 hashitem_T *hi;
834 dictitem_T *di;
Bram Moolenaar3fac56e2010-02-24 15:48:04 +0100835
Bram Moolenaar639a2552010-03-17 18:15:23 +0100836 for (hi = ht->ht_array; todo > 0; ++hi)
837 {
838 if (!HASHITEM_EMPTY(hi))
839 {
840 --todo;
Bram Moolenaar3fac56e2010-02-24 15:48:04 +0100841
Bram Moolenaar639a2552010-03-17 18:15:23 +0100842 di = dict_lookup(hi);
843 rb_hash_aset(result, rb_str_new2((char *)hi->hi_key),
Bram Moolenaar3fac56e2010-02-24 15:48:04 +0100844 vim_to_ruby(&di->di_tv));
Bram Moolenaar639a2552010-03-17 18:15:23 +0100845 }
846 }
847 }
Bram Moolenaar3fac56e2010-02-24 15:48:04 +0100848 } /* else return Qnil; */
849
850 return result;
851}
852#endif
853
Bram Moolenaarcd8b20a2009-05-22 16:20:57 +0000854static VALUE vim_evaluate(VALUE self UNUSED, VALUE str)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000855{
856#ifdef FEAT_EVAL
Bram Moolenaar3fac56e2010-02-24 15:48:04 +0100857 typval_T *tv;
858 VALUE result;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000859
Bram Moolenaar3fac56e2010-02-24 15:48:04 +0100860 tv = eval_expr((char_u *)StringValuePtr(str), NULL);
861 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000862 {
Bram Moolenaar639a2552010-03-17 18:15:23 +0100863 return Qnil;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000864 }
Bram Moolenaar3fac56e2010-02-24 15:48:04 +0100865 result = vim_to_ruby(tv);
866
867 free_tv(tv);
868
869 return result;
870#else
871 return Qnil;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000872#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000873}
874
875static VALUE buffer_new(buf_T *buf)
876{
Bram Moolenaare344bea2005-09-01 20:46:49 +0000877 if (buf->b_ruby_ref)
878 {
879 return (VALUE) buf->b_ruby_ref;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000880 }
Bram Moolenaare344bea2005-09-01 20:46:49 +0000881 else
882 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000883 VALUE obj = Data_Wrap_Struct(cBuffer, 0, 0, buf);
Bram Moolenaare344bea2005-09-01 20:46:49 +0000884 buf->b_ruby_ref = (void *) obj;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000885 rb_hash_aset(objtbl, rb_obj_id(obj), obj);
886 return obj;
887 }
888}
889
890static buf_T *get_buf(VALUE obj)
891{
892 buf_T *buf;
893
894 Data_Get_Struct(obj, buf_T, buf);
895 if (buf == NULL)
896 rb_raise(eDeletedBufferError, "attempt to refer to deleted buffer");
897 return buf;
898}
899
900static VALUE buffer_s_current()
901{
902 return buffer_new(curbuf);
903}
904
905static VALUE buffer_s_count()
906{
907 buf_T *b;
908 int n = 0;
909
Bram Moolenaarbe4d5062006-03-18 21:30:13 +0000910 for (b = firstbuf; b != NULL; b = b->b_next)
911 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000912 /* Deleted buffers should not be counted
913 * SegPhault - 01/07/05 */
914 if (b->b_p_bl)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +0000915 n++;
916 }
917
Bram Moolenaar071d4272004-06-13 20:20:40 +0000918 return INT2NUM(n);
919}
920
Bram Moolenaarcd8b20a2009-05-22 16:20:57 +0000921static VALUE buffer_s_aref(VALUE self UNUSED, VALUE num)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000922{
923 buf_T *b;
924 int n = NUM2INT(num);
925
Bram Moolenaarbe4d5062006-03-18 21:30:13 +0000926 for (b = firstbuf; b != NULL; b = b->b_next)
927 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000928 /* Deleted buffers should not be counted
929 * SegPhault - 01/07/05 */
930 if (!b->b_p_bl)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +0000931 continue;
932
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000933 if (n == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000934 return buffer_new(b);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +0000935
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000936 n--;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000937 }
938 return Qnil;
939}
940
941static VALUE buffer_name(VALUE self)
942{
943 buf_T *buf = get_buf(self);
944
Bram Moolenaar35a2e192006-03-13 22:07:11 +0000945 return buf->b_ffname ? rb_str_new2((char *)buf->b_ffname) : Qnil;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000946}
947
948static VALUE buffer_number(VALUE self)
949{
950 buf_T *buf = get_buf(self);
951
952 return INT2NUM(buf->b_fnum);
953}
954
955static VALUE buffer_count(VALUE self)
956{
957 buf_T *buf = get_buf(self);
958
959 return INT2NUM(buf->b_ml.ml_line_count);
960}
961
Bram Moolenaarbe4d5062006-03-18 21:30:13 +0000962static VALUE get_buffer_line(buf_T *buf, linenr_T n)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000963{
Bram Moolenaar3c531602010-11-16 14:46:19 +0100964 if (n <= 0 || n > buf->b_ml.ml_line_count)
965 rb_raise(rb_eIndexError, "line number %ld out of range", (long)n);
966 return vim_str2rb_enc_str((char *)ml_get_buf(buf, n, FALSE));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000967}
968
Bram Moolenaarbe4d5062006-03-18 21:30:13 +0000969static VALUE buffer_aref(VALUE self, VALUE num)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000970{
971 buf_T *buf = get_buf(self);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +0000972
973 if (buf != NULL)
974 return get_buffer_line(buf, (linenr_T)NUM2LONG(num));
975 return Qnil; /* For stop warning */
976}
977
978static VALUE set_buffer_line(buf_T *buf, linenr_T n, VALUE str)
979{
Bram Moolenaar165641d2010-02-17 16:23:09 +0100980 char *line = StringValuePtr(str);
Bram Moolenaar20ff7922006-06-20 19:10:43 +0000981 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000982
Bram Moolenaar20ff7922006-06-20 19:10:43 +0000983 if (n > 0 && n <= buf->b_ml.ml_line_count && line != NULL)
984 {
Bram Moolenaar20ff7922006-06-20 19:10:43 +0000985 /* set curwin/curbuf for "buf" and save some things */
986 aucmd_prepbuf(&aco, buf);
Bram Moolenaar20ff7922006-06-20 19:10:43 +0000987
Bram Moolenaar071d4272004-06-13 20:20:40 +0000988 if (u_savesub(n) == OK) {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +0000989 ml_replace(n, (char_u *)line, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000990 changed();
991#ifdef SYNTAX_HL
992 syn_changed(n); /* recompute syntax hl. for this line */
993#endif
994 }
Bram Moolenaar20ff7922006-06-20 19:10:43 +0000995
Bram Moolenaar20ff7922006-06-20 19:10:43 +0000996 /* restore curwin/curbuf and a few other things */
997 aucmd_restbuf(&aco);
998 /* Careful: autocommands may have made "buf" invalid! */
Bram Moolenaarf30e74c2006-08-16 17:35:00 +0000999
Bram Moolenaar071d4272004-06-13 20:20:40 +00001000 update_curbuf(NOT_VALID);
1001 }
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001002 else
1003 {
Bram Moolenaar165641d2010-02-17 16:23:09 +01001004 rb_raise(rb_eIndexError, "line number %ld out of range", (long)n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001005 }
1006 return str;
1007}
1008
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001009static VALUE buffer_aset(VALUE self, VALUE num, VALUE str)
1010{
1011 buf_T *buf = get_buf(self);
1012
1013 if (buf != NULL)
1014 return set_buffer_line(buf, (linenr_T)NUM2LONG(num), str);
1015 return str;
1016}
1017
Bram Moolenaar071d4272004-06-13 20:20:40 +00001018static VALUE buffer_delete(VALUE self, VALUE num)
1019{
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001020 buf_T *buf = get_buf(self);
1021 long n = NUM2LONG(num);
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001022 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001023
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001024 if (n > 0 && n <= buf->b_ml.ml_line_count)
1025 {
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001026 /* set curwin/curbuf for "buf" and save some things */
1027 aucmd_prepbuf(&aco, buf);
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001028
Bram Moolenaar071d4272004-06-13 20:20:40 +00001029 if (u_savedel(n, 1) == OK) {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001030 ml_delete(n, 0);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001031
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001032 /* Changes to non-active buffers should properly refresh
1033 * SegPhault - 01/09/05 */
1034 deleted_lines_mark(n, 1L);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001035
Bram Moolenaar071d4272004-06-13 20:20:40 +00001036 changed();
1037 }
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001038
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001039 /* restore curwin/curbuf and a few other things */
1040 aucmd_restbuf(&aco);
1041 /* Careful: autocommands may have made "buf" invalid! */
Bram Moolenaarf30e74c2006-08-16 17:35:00 +00001042
Bram Moolenaar071d4272004-06-13 20:20:40 +00001043 update_curbuf(NOT_VALID);
1044 }
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001045 else
1046 {
Bram Moolenaar165641d2010-02-17 16:23:09 +01001047 rb_raise(rb_eIndexError, "line number %ld out of range", n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001048 }
1049 return Qnil;
1050}
1051
1052static VALUE buffer_append(VALUE self, VALUE num, VALUE str)
1053{
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001054 buf_T *buf = get_buf(self);
Bram Moolenaar165641d2010-02-17 16:23:09 +01001055 char *line = StringValuePtr(str);
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001056 long n = NUM2LONG(num);
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001057 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001058
Bram Moolenaar3c531602010-11-16 14:46:19 +01001059 if (line == NULL)
1060 {
Bram Moolenaar165641d2010-02-17 16:23:09 +01001061 rb_raise(rb_eIndexError, "NULL line");
1062 }
1063 else if (n >= 0 && n <= buf->b_ml.ml_line_count)
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001064 {
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001065 /* set curwin/curbuf for "buf" and save some things */
1066 aucmd_prepbuf(&aco, buf);
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001067
Bram Moolenaar071d4272004-06-13 20:20:40 +00001068 if (u_inssub(n + 1) == OK) {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001069 ml_append(n, (char_u *) line, (colnr_T) 0, FALSE);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001070
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001071 /* Changes to non-active buffers should properly refresh screen
1072 * SegPhault - 12/20/04 */
1073 appended_lines_mark(n, 1L);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001074
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001075 changed();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001076 }
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001077
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001078 /* restore curwin/curbuf and a few other things */
1079 aucmd_restbuf(&aco);
1080 /* Careful: autocommands may have made "buf" invalid! */
Bram Moolenaarf30e74c2006-08-16 17:35:00 +00001081
Bram Moolenaar071d4272004-06-13 20:20:40 +00001082 update_curbuf(NOT_VALID);
1083 }
Bram Moolenaar3c531602010-11-16 14:46:19 +01001084 else
1085 {
Bram Moolenaar165641d2010-02-17 16:23:09 +01001086 rb_raise(rb_eIndexError, "line number %ld out of range", n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001087 }
1088 return str;
1089}
1090
1091static VALUE window_new(win_T *win)
1092{
Bram Moolenaare344bea2005-09-01 20:46:49 +00001093 if (win->w_ruby_ref)
1094 {
1095 return (VALUE) win->w_ruby_ref;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001096 }
Bram Moolenaare344bea2005-09-01 20:46:49 +00001097 else
1098 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001099 VALUE obj = Data_Wrap_Struct(cVimWindow, 0, 0, win);
Bram Moolenaare344bea2005-09-01 20:46:49 +00001100 win->w_ruby_ref = (void *) obj;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001101 rb_hash_aset(objtbl, rb_obj_id(obj), obj);
1102 return obj;
1103 }
1104}
1105
1106static win_T *get_win(VALUE obj)
1107{
1108 win_T *win;
1109
1110 Data_Get_Struct(obj, win_T, win);
1111 if (win == NULL)
1112 rb_raise(eDeletedWindowError, "attempt to refer to deleted window");
1113 return win;
1114}
1115
1116static VALUE window_s_current()
1117{
1118 return window_new(curwin);
1119}
1120
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001121/*
1122 * Added line manipulation functions
1123 * SegPhault - 03/07/05
1124 */
1125static VALUE line_s_current()
1126{
1127 return get_buffer_line(curbuf, curwin->w_cursor.lnum);
1128}
1129
Bram Moolenaarcd8b20a2009-05-22 16:20:57 +00001130static VALUE set_current_line(VALUE self UNUSED, VALUE str)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001131{
1132 return set_buffer_line(curbuf, curwin->w_cursor.lnum, str);
1133}
1134
1135static VALUE current_line_number()
1136{
1137 return INT2FIX((int)curwin->w_cursor.lnum);
1138}
1139
1140
1141
Bram Moolenaar071d4272004-06-13 20:20:40 +00001142static VALUE window_s_count()
1143{
1144#ifdef FEAT_WINDOWS
1145 win_T *w;
1146 int n = 0;
1147
Bram Moolenaarf740b292006-02-16 22:11:02 +00001148 for (w = firstwin; w != NULL; w = w->w_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001149 n++;
1150 return INT2NUM(n);
1151#else
1152 return INT2NUM(1);
1153#endif
1154}
1155
Bram Moolenaarcd8b20a2009-05-22 16:20:57 +00001156static VALUE window_s_aref(VALUE self UNUSED, VALUE num)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001157{
1158 win_T *w;
1159 int n = NUM2INT(num);
1160
1161#ifndef FEAT_WINDOWS
1162 w = curwin;
1163#else
1164 for (w = firstwin; w != NULL; w = w->w_next, --n)
1165#endif
1166 if (n == 0)
1167 return window_new(w);
1168 return Qnil;
1169}
1170
1171static VALUE window_buffer(VALUE self)
1172{
1173 win_T *win = get_win(self);
1174
1175 return buffer_new(win->w_buffer);
1176}
1177
1178static VALUE window_height(VALUE self)
1179{
1180 win_T *win = get_win(self);
1181
1182 return INT2NUM(win->w_height);
1183}
1184
1185static VALUE window_set_height(VALUE self, VALUE height)
1186{
1187 win_T *win = get_win(self);
1188 win_T *savewin = curwin;
1189
1190 curwin = win;
1191 win_setheight(NUM2INT(height));
1192 curwin = savewin;
1193 return height;
1194}
1195
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001196static VALUE window_width(VALUE self)
1197{
1198 win_T *win = get_win(self);
1199
1200 return INT2NUM(win->w_width);
1201}
1202
1203static VALUE window_set_width(VALUE self, VALUE width)
1204{
1205 win_T *win = get_win(self);
1206 win_T *savewin = curwin;
1207
1208 curwin = win;
1209 win_setwidth(NUM2INT(width));
1210 curwin = savewin;
1211 return width;
1212}
1213
Bram Moolenaar071d4272004-06-13 20:20:40 +00001214static VALUE window_cursor(VALUE self)
1215{
1216 win_T *win = get_win(self);
1217
1218 return rb_assoc_new(INT2NUM(win->w_cursor.lnum), INT2NUM(win->w_cursor.col));
1219}
1220
1221static VALUE window_set_cursor(VALUE self, VALUE pos)
1222{
1223 VALUE lnum, col;
1224 win_T *win = get_win(self);
1225
1226 Check_Type(pos, T_ARRAY);
Bram Moolenaar165641d2010-02-17 16:23:09 +01001227 if (RARRAY_LEN(pos) != 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001228 rb_raise(rb_eArgError, "array length must be 2");
Bram Moolenaar165641d2010-02-17 16:23:09 +01001229 lnum = RARRAY_PTR(pos)[0];
1230 col = RARRAY_PTR(pos)[1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001231 win->w_cursor.lnum = NUM2LONG(lnum);
1232 win->w_cursor.col = NUM2UINT(col);
1233 check_cursor(); /* put cursor on an existing line */
1234 update_screen(NOT_VALID);
1235 return Qnil;
1236}
1237
Bram Moolenaarcd8b20a2009-05-22 16:20:57 +00001238static VALUE f_p(int argc, VALUE *argv, VALUE self UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001239{
1240 int i;
1241 VALUE str = rb_str_new("", 0);
1242
1243 for (i = 0; i < argc; i++) {
1244 if (i > 0) rb_str_cat(str, ", ", 2);
1245 rb_str_concat(str, rb_inspect(argv[i]));
1246 }
Bram Moolenaar165641d2010-02-17 16:23:09 +01001247 MSG(RSTRING_PTR(str));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001248 return Qnil;
1249}
1250
1251static void ruby_io_init(void)
1252{
1253#ifndef DYNAMIC_RUBY
1254 RUBYEXTERN VALUE rb_stdout;
1255#endif
1256
1257 rb_stdout = rb_obj_alloc(rb_cObject);
1258 rb_define_singleton_method(rb_stdout, "write", vim_message, 1);
1259 rb_define_global_function("p", f_p, -1);
1260}
1261
1262static void ruby_vim_init(void)
1263{
1264 objtbl = rb_hash_new();
1265 rb_global_variable(&objtbl);
1266
Bram Moolenaarf711faf2007-05-10 16:48:19 +00001267 /* The Vim module used to be called "VIM", but "Vim" is better. Make an
1268 * alias "VIM" for backwards compatiblity. */
1269 mVIM = rb_define_module("Vim");
1270 rb_define_const(rb_cObject, "VIM", mVIM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001271 rb_define_const(mVIM, "VERSION_MAJOR", INT2NUM(VIM_VERSION_MAJOR));
1272 rb_define_const(mVIM, "VERSION_MINOR", INT2NUM(VIM_VERSION_MINOR));
1273 rb_define_const(mVIM, "VERSION_BUILD", INT2NUM(VIM_VERSION_BUILD));
1274 rb_define_const(mVIM, "VERSION_PATCHLEVEL", INT2NUM(VIM_VERSION_PATCHLEVEL));
1275 rb_define_const(mVIM, "VERSION_SHORT", rb_str_new2(VIM_VERSION_SHORT));
1276 rb_define_const(mVIM, "VERSION_MEDIUM", rb_str_new2(VIM_VERSION_MEDIUM));
1277 rb_define_const(mVIM, "VERSION_LONG", rb_str_new2(VIM_VERSION_LONG));
1278 rb_define_const(mVIM, "VERSION_LONG_DATE", rb_str_new2(VIM_VERSION_LONG_DATE));
1279 rb_define_module_function(mVIM, "message", vim_message, 1);
1280 rb_define_module_function(mVIM, "set_option", vim_set_option, 1);
1281 rb_define_module_function(mVIM, "command", vim_command, 1);
1282 rb_define_module_function(mVIM, "evaluate", vim_evaluate, 1);
1283
1284 eDeletedBufferError = rb_define_class_under(mVIM, "DeletedBufferError",
1285 rb_eStandardError);
1286 eDeletedWindowError = rb_define_class_under(mVIM, "DeletedWindowError",
1287 rb_eStandardError);
1288
1289 cBuffer = rb_define_class_under(mVIM, "Buffer", rb_cObject);
1290 rb_define_singleton_method(cBuffer, "current", buffer_s_current, 0);
1291 rb_define_singleton_method(cBuffer, "count", buffer_s_count, 0);
1292 rb_define_singleton_method(cBuffer, "[]", buffer_s_aref, 1);
1293 rb_define_method(cBuffer, "name", buffer_name, 0);
1294 rb_define_method(cBuffer, "number", buffer_number, 0);
1295 rb_define_method(cBuffer, "count", buffer_count, 0);
1296 rb_define_method(cBuffer, "length", buffer_count, 0);
1297 rb_define_method(cBuffer, "[]", buffer_aref, 1);
1298 rb_define_method(cBuffer, "[]=", buffer_aset, 2);
1299 rb_define_method(cBuffer, "delete", buffer_delete, 1);
1300 rb_define_method(cBuffer, "append", buffer_append, 2);
1301
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001302 /* Added line manipulation functions
1303 * SegPhault - 03/07/05 */
1304 rb_define_method(cBuffer, "line_number", current_line_number, 0);
1305 rb_define_method(cBuffer, "line", line_s_current, 0);
1306 rb_define_method(cBuffer, "line=", set_current_line, 1);
1307
1308
Bram Moolenaar071d4272004-06-13 20:20:40 +00001309 cVimWindow = rb_define_class_under(mVIM, "Window", rb_cObject);
1310 rb_define_singleton_method(cVimWindow, "current", window_s_current, 0);
1311 rb_define_singleton_method(cVimWindow, "count", window_s_count, 0);
1312 rb_define_singleton_method(cVimWindow, "[]", window_s_aref, 1);
1313 rb_define_method(cVimWindow, "buffer", window_buffer, 0);
1314 rb_define_method(cVimWindow, "height", window_height, 0);
1315 rb_define_method(cVimWindow, "height=", window_set_height, 1);
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001316 rb_define_method(cVimWindow, "width", window_width, 0);
1317 rb_define_method(cVimWindow, "width=", window_set_width, 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001318 rb_define_method(cVimWindow, "cursor", window_cursor, 0);
1319 rb_define_method(cVimWindow, "cursor=", window_set_cursor, 1);
1320
1321 rb_define_virtual_variable("$curbuf", buffer_s_current, 0);
1322 rb_define_virtual_variable("$curwin", window_s_current, 0);
1323}