blob: 90e814ec125bcf052ed913a0eabef1c8c8e11be3 [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
Bram Moolenaar2d73ff42010-10-27 17:40:59 +020014#ifdef HAVE_CONFIG_H
15# include "auto/config.h"
16#endif
Bram Moolenaar3ca71f12010-10-27 16:49:47 +020017
Bram Moolenaare2793352011-01-17 19:53:27 +010018#include <stdio.h>
19#include <string.h>
20
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)
Bram Moolenaardb3fbe52013-03-07 15:16:21 +010042# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 20
43# define rb_cFloat (*dll_rb_cFloat)
44# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000045# define rb_cNilClass (*dll_rb_cNilClass)
46# define rb_cSymbol (*dll_rb_cSymbol)
47# define rb_cTrueClass (*dll_rb_cTrueClass)
48# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 18
49/*
Bram Moolenaar42d57f02010-03-10 12:47:00 +010050 * On ver 1.8, all Ruby functions are exported with "__declspec(dllimport)"
51 * in ruby.h. But it causes trouble for these variables, because it is
Bram Moolenaar071d4272004-06-13 20:20:40 +000052 * defined in this file. When defined this RUBY_EXPORT it modified to
53 * "extern" and be able to avoid this problem.
54 */
55# define RUBY_EXPORT
56# endif
Bram Moolenaarf9b5ef82010-09-29 13:02:53 +020057
Bram Moolenaar2d73ff42010-10-27 17:40:59 +020058#if !(defined(WIN32) || defined(_WIN64))
Bram Moolenaarf9b5ef82010-09-29 13:02:53 +020059# include <dlfcn.h>
Bram Moolenaar3ca71f12010-10-27 16:49:47 +020060# define HINSTANCE void*
61# define RUBY_PROC void*
Bram Moolenaarf9b5ef82010-09-29 13:02:53 +020062# define load_dll(n) dlopen((n), RTLD_LAZY|RTLD_GLOBAL)
63# define symbol_from_dll dlsym
64# define close_dll dlclose
65#else
Bram Moolenaar3ca71f12010-10-27 16:49:47 +020066# define RUBY_PROC FARPROC
Bram Moolenaarebbcb822010-10-23 14:02:54 +020067# define load_dll vimLoadLib
Bram Moolenaarf9b5ef82010-09-29 13:02:53 +020068# define symbol_from_dll GetProcAddress
69# define close_dll FreeLibrary
Bram Moolenaar071d4272004-06-13 20:20:40 +000070#endif
71
Bram Moolenaarf9b5ef82010-09-29 13:02:53 +020072#endif /* ifdef DYNAMIC_RUBY */
73
Bram Moolenaar0b69c732010-02-17 15:11:50 +010074/* suggested by Ariya Mizutani */
75#if (_MSC_VER == 1200)
76# undef _WIN32_WINNT
77#endif
78
Bram Moolenaar639a2552010-03-17 18:15:23 +010079#if (defined(RUBY_VERSION) && RUBY_VERSION >= 19) \
80 || (defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 19)
81# define RUBY19_OR_LATER 1
82#endif
83
Bram Moolenaar0d27f642015-12-28 22:05:28 +010084#if (defined(RUBY_VERSION) && RUBY_VERSION >= 20) \
85 || (defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 20)
86# define RUBY20_OR_LATER 1
87#endif
88
Bram Moolenaar42d57f02010-03-10 12:47:00 +010089#if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 19
90/* Ruby 1.9 defines a number of static functions which use rb_num2long and
91 * rb_int2big */
92# define rb_num2long rb_num2long_stub
93# define rb_int2big rb_int2big_stub
94#endif
95
Bram Moolenaar498af702014-03-28 21:58:21 +010096#if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 19 \
97 && VIM_SIZEOF_INT < VIM_SIZEOF_LONG
98/* Ruby 1.9 defines a number of static functions which use rb_fix2int and
Bram Moolenaara2aa31a2014-02-23 22:52:40 +010099 * rb_num2int if VIM_SIZEOF_INT < VIM_SIZEOF_LONG (64bit) */
Bram Moolenaar0bcdd6e2013-04-14 16:19:03 +0200100# define rb_fix2int rb_fix2int_stub
101# define rb_num2int rb_num2int_stub
102#endif
103
Bram Moolenaar0c7485f2015-01-14 14:04:10 +0100104#if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER == 21
Bram Moolenaara1a118b2014-02-05 22:41:15 +0100105/* Ruby 2.1 adds new GC called RGenGC and RARRAY_PTR uses
106 * rb_gc_writebarrier_unprotect_promoted if USE_RGENGC */
Bram Moolenaar90140742014-11-27 17:44:08 +0100107# define rb_gc_writebarrier_unprotect_promoted rb_gc_writebarrier_unprotect_promoted_stub
108#endif
Bram Moolenaar0c7485f2015-01-14 14:04:10 +0100109#if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 22
110# define rb_gc_writebarrier_unprotect rb_gc_writebarrier_unprotect_stub
Bram Moolenaar0c7485f2015-01-14 14:04:10 +0100111#endif
Bram Moolenaara1a118b2014-02-05 22:41:15 +0100112
Bram Moolenaar071d4272004-06-13 20:20:40 +0000113#include <ruby.h>
Bram Moolenaar639a2552010-03-17 18:15:23 +0100114#ifdef RUBY19_OR_LATER
Bram Moolenaar165641d2010-02-17 16:23:09 +0100115# include <ruby/encoding.h>
116#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000117
Bram Moolenaar7a8ef142010-12-24 13:39:35 +0100118#undef off_t /* ruby defines off_t as _int64, Mingw uses long */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000119#undef EXTERN
120#undef _
121
122/* T_DATA defined both by Ruby and Mac header files, hack around it... */
Bram Moolenaarcb635362007-05-12 13:02:42 +0000123#if defined(MACOS_X_UNIX) || defined(macintosh)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000124# define __OPENTRANSPORT__
125# define __OPENTRANSPORTPROTOCOL__
126# define __OPENTRANSPORTPROVIDERS__
127#endif
128
Bram Moolenaar165641d2010-02-17 16:23:09 +0100129/*
Bram Moolenaar0d27f642015-12-28 22:05:28 +0100130 * The TypedData_XXX macro family can be used since Ruby 1.9.2 but
131 * rb_data_type_t changed in 1.9.3, therefore require at least 2.0.
132 * The old Data_XXX macro family was deprecated on Ruby 2.2.
Bram Moolenaarf2f6d292015-12-28 20:57:10 +0100133 * Use TypedData_XXX if available.
134 */
Bram Moolenaar0d27f642015-12-28 22:05:28 +0100135#if defined(TypedData_Wrap_Struct) && defined(RUBY20_OR_LATER)
Bram Moolenaarf2f6d292015-12-28 20:57:10 +0100136# define USE_TYPEDDATA 1
137#endif
138
139/*
Bram Moolenaar84a05ac2013-05-06 04:24:17 +0200140 * Backward compatibility for Ruby 1.8 and earlier.
Bram Moolenaar165641d2010-02-17 16:23:09 +0100141 * Ruby 1.9 does not provide STR2CSTR, instead StringValuePtr is provided.
142 * Ruby 1.9 does not provide RXXX(s)->len and RXXX(s)->ptr, instead
143 * RXXX_LEN(s) and RXXX_PTR(s) are provided.
144 */
145#ifndef StringValuePtr
146# define StringValuePtr(s) STR2CSTR(s)
147#endif
148#ifndef RARRAY_LEN
149# define RARRAY_LEN(s) RARRAY(s)->len
150#endif
151#ifndef RARRAY_PTR
152# define RARRAY_PTR(s) RARRAY(s)->ptr
153#endif
154#ifndef RSTRING_LEN
155# define RSTRING_LEN(s) RSTRING(s)->len
156#endif
157#ifndef RSTRING_PTR
158# define RSTRING_PTR(s) RSTRING(s)->ptr
159#endif
160
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100161#ifdef HAVE_DUP
162# undef HAVE_DUP
163#endif
164
Bram Moolenaar071d4272004-06-13 20:20:40 +0000165#include "vim.h"
166#include "version.h"
167
168#if defined(PROTO) && !defined(FEAT_RUBY)
169/* Define these to be able to generate the function prototypes. */
170# define VALUE int
171# define RUBY_DATA_FUNC int
172#endif
173
174static int ruby_initialized = 0;
Bram Moolenaar99685e62013-05-11 13:56:18 +0200175static void *ruby_stack_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000176static VALUE objtbl;
177
178static VALUE mVIM;
179static VALUE cBuffer;
180static VALUE cVimWindow;
181static VALUE eDeletedBufferError;
182static VALUE eDeletedWindowError;
183
184static int ensure_ruby_initialized(void);
185static void error_print(int);
186static void ruby_io_init(void);
187static void ruby_vim_init(void);
188
Bram Moolenaar10f3a792013-05-20 12:52:29 +0200189#if defined(RUBY19_OR_LATER) || defined(RUBY_INIT_STACK)
190# if defined(__ia64) && !defined(ruby_init_stack)
191# define ruby_init_stack(addr) ruby_init_stack((addr), rb_ia64_bsp())
192# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000193#endif
194
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200195#if defined(DYNAMIC_RUBY) || defined(PROTO)
Bram Moolenaaref269542016-01-19 13:22:12 +0100196# if defined(PROTO) && !defined(HINSTANCE)
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200197# define HINSTANCE int /* for generating prototypes */
198# endif
199
Bram Moolenaar071d4272004-06-13 20:20:40 +0000200/*
201 * Wrapper defines
202 */
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200203# define rb_assoc_new dll_rb_assoc_new
204# define rb_cObject (*dll_rb_cObject)
Bram Moolenaarf2f6d292015-12-28 20:57:10 +0100205# define rb_check_type dll_rb_check_type
206# ifdef USE_TYPEDDATA
207# define rb_check_typeddata dll_rb_check_typeddata
Bram Moolenaar0c7485f2015-01-14 14:04:10 +0100208# endif
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200209# define rb_class_path dll_rb_class_path
Bram Moolenaarf2f6d292015-12-28 20:57:10 +0100210# ifdef USE_TYPEDDATA
211# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 23
212# define rb_data_typed_object_wrap dll_rb_data_typed_object_wrap
213# else
214# define rb_data_typed_object_alloc dll_rb_data_typed_object_alloc
215# endif
216# else
217# define rb_data_object_alloc dll_rb_data_object_alloc
218# endif
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200219# define rb_define_class_under dll_rb_define_class_under
220# define rb_define_const dll_rb_define_const
221# define rb_define_global_function dll_rb_define_global_function
222# define rb_define_method dll_rb_define_method
223# define rb_define_module dll_rb_define_module
224# define rb_define_module_function dll_rb_define_module_function
225# define rb_define_singleton_method dll_rb_define_singleton_method
226# define rb_define_virtual_variable dll_rb_define_virtual_variable
227# define rb_stdout (*dll_rb_stdout)
228# define rb_eArgError (*dll_rb_eArgError)
229# define rb_eIndexError (*dll_rb_eIndexError)
230# define rb_eRuntimeError (*dll_rb_eRuntimeError)
231# define rb_eStandardError (*dll_rb_eStandardError)
232# define rb_eval_string_protect dll_rb_eval_string_protect
233# define rb_global_variable dll_rb_global_variable
234# define rb_hash_aset dll_rb_hash_aset
235# define rb_hash_new dll_rb_hash_new
236# define rb_inspect dll_rb_inspect
237# define rb_int2inum dll_rb_int2inum
Bram Moolenaara2aa31a2014-02-23 22:52:40 +0100238# if VIM_SIZEOF_INT < VIM_SIZEOF_LONG /* 64 bits only */
Bram Moolenaar498af702014-03-28 21:58:21 +0100239# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER <= 18
240# define rb_fix2int dll_rb_fix2int
241# define rb_num2int dll_rb_num2int
242# endif
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200243# define rb_num2uint dll_rb_num2uint
244# endif
245# define rb_lastline_get dll_rb_lastline_get
246# define rb_lastline_set dll_rb_lastline_set
247# define rb_load_protect dll_rb_load_protect
248# ifndef RUBY19_OR_LATER
249# define rb_num2long dll_rb_num2long
250# endif
251# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER <= 19
252# define rb_num2ulong dll_rb_num2ulong
253# endif
254# define rb_obj_alloc dll_rb_obj_alloc
255# define rb_obj_as_string dll_rb_obj_as_string
256# define rb_obj_id dll_rb_obj_id
257# define rb_raise dll_rb_raise
258# define rb_str_cat dll_rb_str_cat
259# define rb_str_concat dll_rb_str_concat
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100260# undef rb_str_new
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200261# define rb_str_new dll_rb_str_new
262# ifdef rb_str_new2
Bram Moolenaar1d2beae2010-05-22 21:56:55 +0200263/* Ruby may #define rb_str_new2 to use rb_str_new_cstr. */
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200264# define need_rb_str_new_cstr 1
Bram Moolenaar3ca71f12010-10-27 16:49:47 +0200265/* Ruby's headers #define rb_str_new_cstr to make use of GCC's
266 * __builtin_constant_p extension. */
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200267# undef rb_str_new_cstr
268# define rb_str_new_cstr dll_rb_str_new_cstr
Bram Moolenaar76a86062013-05-11 17:45:48 +0200269# else
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200270# define rb_str_new2 dll_rb_str_new2
Bram Moolenaar76a86062013-05-11 17:45:48 +0200271# endif
Bram Moolenaar3ca71f12010-10-27 16:49:47 +0200272# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 18
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200273# define rb_string_value dll_rb_string_value
274# define rb_string_value_ptr dll_rb_string_value_ptr
275# define rb_float_new dll_rb_float_new
276# define rb_ary_new dll_rb_ary_new
277# define rb_ary_push dll_rb_ary_push
Bram Moolenaar10f3a792013-05-20 12:52:29 +0200278# if defined(RUBY19_OR_LATER) || defined(RUBY_INIT_STACK)
279# ifdef __ia64
280# define rb_ia64_bsp dll_rb_ia64_bsp
281# undef ruby_init_stack
282# define ruby_init_stack(addr) dll_ruby_init_stack((addr), rb_ia64_bsp())
283# else
284# define ruby_init_stack dll_ruby_init_stack
285# endif
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200286# endif
287# else
288# define rb_str2cstr dll_rb_str2cstr
Bram Moolenaar3ca71f12010-10-27 16:49:47 +0200289# endif
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200290# ifdef RUBY19_OR_LATER
291# define rb_errinfo dll_rb_errinfo
292# else
293# define ruby_errinfo (*dll_ruby_errinfo)
294# endif
295# define ruby_init dll_ruby_init
296# define ruby_init_loadpath dll_ruby_init_loadpath
297# ifdef WIN3264
298# define NtInitialize dll_NtInitialize
299# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 18
300# define rb_w32_snprintf dll_rb_w32_snprintf
301# endif
302# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000303
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200304# ifdef RUBY19_OR_LATER
305# define ruby_script dll_ruby_script
306# define rb_enc_find_index dll_rb_enc_find_index
307# define rb_enc_find dll_rb_enc_find
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100308# undef rb_enc_str_new
309# define rb_enc_str_new dll_rb_enc_str_new
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200310# define rb_sprintf dll_rb_sprintf
311# define rb_require dll_rb_require
Bram Moolenaar9b1067e2015-11-19 19:33:15 +0100312# define ruby_options dll_ruby_options
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200313# endif
Bram Moolenaar165641d2010-02-17 16:23:09 +0100314
Bram Moolenaar071d4272004-06-13 20:20:40 +0000315/*
316 * Pointers for dynamic link
317 */
318static VALUE (*dll_rb_assoc_new) (VALUE, VALUE);
Bram Moolenaarba52cde2010-06-25 04:29:11 +0200319VALUE *dll_rb_cFalseClass;
320VALUE *dll_rb_cFixnum;
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200321# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 20
Bram Moolenaardb3fbe52013-03-07 15:16:21 +0100322VALUE *dll_rb_cFloat;
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200323# endif
Bram Moolenaarba52cde2010-06-25 04:29:11 +0200324VALUE *dll_rb_cNilClass;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000325static VALUE *dll_rb_cObject;
Bram Moolenaarba52cde2010-06-25 04:29:11 +0200326VALUE *dll_rb_cSymbol;
327VALUE *dll_rb_cTrueClass;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000328static void (*dll_rb_check_type) (VALUE,int);
Bram Moolenaarf2f6d292015-12-28 20:57:10 +0100329# ifdef USE_TYPEDDATA
330static void *(*dll_rb_check_typeddata) (VALUE,const rb_data_type_t *);
331# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000332static VALUE (*dll_rb_class_path) (VALUE);
Bram Moolenaarf2f6d292015-12-28 20:57:10 +0100333# ifdef USE_TYPEDDATA
334# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 23
335static VALUE (*dll_rb_data_typed_object_wrap) (VALUE, void*, const rb_data_type_t *);
336# else
337static VALUE (*dll_rb_data_typed_object_alloc) (VALUE, void*, const rb_data_type_t *);
338# endif
339# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000340static VALUE (*dll_rb_data_object_alloc) (VALUE, void*, RUBY_DATA_FUNC, RUBY_DATA_FUNC);
Bram Moolenaarf2f6d292015-12-28 20:57:10 +0100341# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000342static VALUE (*dll_rb_define_class_under) (VALUE, const char*, VALUE);
343static void (*dll_rb_define_const) (VALUE,const char*,VALUE);
344static void (*dll_rb_define_global_function) (const char*,VALUE(*)(),int);
345static void (*dll_rb_define_method) (VALUE,const char*,VALUE(*)(),int);
346static VALUE (*dll_rb_define_module) (const char*);
347static void (*dll_rb_define_module_function) (VALUE,const char*,VALUE(*)(),int);
348static void (*dll_rb_define_singleton_method) (VALUE,const char*,VALUE(*)(),int);
349static void (*dll_rb_define_virtual_variable) (const char*,VALUE(*)(),void(*)());
350static VALUE *dll_rb_stdout;
351static VALUE *dll_rb_eArgError;
352static VALUE *dll_rb_eIndexError;
353static VALUE *dll_rb_eRuntimeError;
354static VALUE *dll_rb_eStandardError;
355static VALUE (*dll_rb_eval_string_protect) (const char*, int*);
356static void (*dll_rb_global_variable) (VALUE*);
357static VALUE (*dll_rb_hash_aset) (VALUE, VALUE, VALUE);
358static VALUE (*dll_rb_hash_new) (void);
359static VALUE (*dll_rb_inspect) (VALUE);
360static VALUE (*dll_rb_int2inum) (long);
Bram Moolenaara2aa31a2014-02-23 22:52:40 +0100361# if VIM_SIZEOF_INT < VIM_SIZEOF_LONG /* 64 bits only */
Bram Moolenaar2623b4f2012-09-18 16:36:32 +0200362static long (*dll_rb_fix2int) (VALUE);
363static long (*dll_rb_num2int) (VALUE);
364static unsigned long (*dll_rb_num2uint) (VALUE);
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200365# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000366static VALUE (*dll_rb_lastline_get) (void);
367static void (*dll_rb_lastline_set) (VALUE);
368static void (*dll_rb_load_protect) (VALUE, int, int*);
369static long (*dll_rb_num2long) (VALUE);
370static unsigned long (*dll_rb_num2ulong) (VALUE);
371static VALUE (*dll_rb_obj_alloc) (VALUE);
372static VALUE (*dll_rb_obj_as_string) (VALUE);
373static VALUE (*dll_rb_obj_id) (VALUE);
374static void (*dll_rb_raise) (VALUE, const char*, ...);
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200375# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 18
Bram Moolenaarf9b5ef82010-09-29 13:02:53 +0200376static VALUE (*dll_rb_string_value) (volatile VALUE*);
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200377# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000378static char *(*dll_rb_str2cstr) (VALUE,int*);
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200379# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000380static VALUE (*dll_rb_str_cat) (VALUE, const char*, long);
381static VALUE (*dll_rb_str_concat) (VALUE, VALUE);
382static VALUE (*dll_rb_str_new) (const char*, long);
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200383# ifdef need_rb_str_new_cstr
Bram Moolenaar1d2beae2010-05-22 21:56:55 +0200384/* Ruby may #define rb_str_new2 to use rb_str_new_cstr. */
385static VALUE (*dll_rb_str_new_cstr) (const char*);
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200386# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000387static VALUE (*dll_rb_str_new2) (const char*);
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200388# endif
389# ifdef RUBY19_OR_LATER
Bram Moolenaar165641d2010-02-17 16:23:09 +0100390static VALUE (*dll_rb_errinfo) (void);
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200391# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000392static VALUE *dll_ruby_errinfo;
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200393# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000394static void (*dll_ruby_init) (void);
395static void (*dll_ruby_init_loadpath) (void);
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200396# ifdef WIN3264
Bram Moolenaar0b69c732010-02-17 15:11:50 +0100397static void (*dll_NtInitialize) (int*, char***);
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200398# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 18
Bram Moolenaar3ca71f12010-10-27 16:49:47 +0200399static int (*dll_rb_w32_snprintf)(char*, size_t, const char*, ...);
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200400# endif
Bram Moolenaar3ca71f12010-10-27 16:49:47 +0200401# endif
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200402# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 18
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100403static char * (*dll_rb_string_value_ptr) (volatile VALUE*);
404static VALUE (*dll_rb_float_new) (double);
405static VALUE (*dll_rb_ary_new) (void);
406static VALUE (*dll_rb_ary_push) (VALUE, VALUE);
Bram Moolenaar10f3a792013-05-20 12:52:29 +0200407# if defined(RUBY19_OR_LATER) || defined(RUBY_INIT_STACK)
408# ifdef __ia64
Bram Moolenaar76a86062013-05-11 17:45:48 +0200409static void * (*dll_rb_ia64_bsp) (void);
410static void (*dll_ruby_init_stack)(VALUE*, void*);
Bram Moolenaar10f3a792013-05-20 12:52:29 +0200411# else
Bram Moolenaar76a86062013-05-11 17:45:48 +0200412static void (*dll_ruby_init_stack)(VALUE*);
Bram Moolenaar10f3a792013-05-20 12:52:29 +0200413# endif
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200414# endif
Bram Moolenaar76a86062013-05-11 17:45:48 +0200415# endif
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200416# ifdef RUBY19_OR_LATER
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100417static VALUE (*dll_rb_int2big)(SIGNED_VALUE);
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200418# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000419
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200420# ifdef RUBY19_OR_LATER
Bram Moolenaar165641d2010-02-17 16:23:09 +0100421static void (*dll_ruby_script) (const char*);
422static int (*dll_rb_enc_find_index) (const char*);
423static rb_encoding* (*dll_rb_enc_find) (const char*);
424static VALUE (*dll_rb_enc_str_new) (const char*, long, rb_encoding*);
425static VALUE (*dll_rb_sprintf) (const char*, ...);
Bram Moolenaar7a8ef142010-12-24 13:39:35 +0100426static VALUE (*dll_rb_require) (const char*);
Bram Moolenaar9b1067e2015-11-19 19:33:15 +0100427static void* (*ruby_options)(int, char**);
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200428# endif
Bram Moolenaar165641d2010-02-17 16:23:09 +0100429
Bram Moolenaara1a118b2014-02-05 22:41:15 +0100430# if defined(USE_RGENGC) && USE_RGENGC
Bram Moolenaar0c7485f2015-01-14 14:04:10 +0100431# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER == 21
Bram Moolenaara1a118b2014-02-05 22:41:15 +0100432static void (*dll_rb_gc_writebarrier_unprotect_promoted)(VALUE);
Bram Moolenaar0c7485f2015-01-14 14:04:10 +0100433# else
434static void (*dll_rb_gc_writebarrier_unprotect)(VALUE obj);
435# endif
Bram Moolenaara1a118b2014-02-05 22:41:15 +0100436# endif
437
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200438# if defined(RUBY19_OR_LATER) && !defined(PROTO)
Bram Moolenaarbbc1a592015-04-21 15:25:31 +0200439# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 22
440long rb_num2long_stub(VALUE x)
441# else
Bram Moolenaarff8cf2b2012-11-24 13:39:00 +0100442SIGNED_VALUE rb_num2long_stub(VALUE x)
Bram Moolenaarbbc1a592015-04-21 15:25:31 +0200443# endif
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100444{
445 return dll_rb_num2long(x);
446}
Bram Moolenaarff8cf2b2012-11-24 13:39:00 +0100447VALUE rb_int2big_stub(SIGNED_VALUE x)
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100448{
449 return dll_rb_int2big(x);
450}
Bram Moolenaar498af702014-03-28 21:58:21 +0100451# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 19 \
452 && VIM_SIZEOF_INT < VIM_SIZEOF_LONG
Bram Moolenaar0bcdd6e2013-04-14 16:19:03 +0200453long rb_fix2int_stub(VALUE x)
454{
455 return dll_rb_fix2int(x);
456}
457long rb_num2int_stub(VALUE x)
458{
459 return dll_rb_num2int(x);
460}
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200461# endif
462# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 20
Bram Moolenaar886ed692013-02-26 13:41:35 +0100463VALUE
464rb_float_new_in_heap(double d)
465{
466 return dll_rb_float_new(d);
467}
Bram Moolenaarbbc1a592015-04-21 15:25:31 +0200468# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 22
469unsigned long rb_num2ulong(VALUE x)
470# else
Bram Moolenaardb3fbe52013-03-07 15:16:21 +0100471VALUE rb_num2ulong(VALUE x)
Bram Moolenaarbbc1a592015-04-21 15:25:31 +0200472# endif
Bram Moolenaar886ed692013-02-26 13:41:35 +0100473{
474 return (long)RSHIFT((SIGNED_VALUE)(x),1);
475}
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200476# endif
477# endif
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100478
Bram Moolenaar3e9a1612014-11-12 16:05:04 +0100479 /* Do not generate a prototype here, VALUE isn't always defined. */
480# if defined(USE_RGENGC) && USE_RGENGC && !defined(PROTO)
Bram Moolenaar0c7485f2015-01-14 14:04:10 +0100481# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER == 21
Bram Moolenaara1a118b2014-02-05 22:41:15 +0100482void rb_gc_writebarrier_unprotect_promoted_stub(VALUE obj)
483{
Bram Moolenaar90140742014-11-27 17:44:08 +0100484 dll_rb_gc_writebarrier_unprotect_promoted(obj);
Bram Moolenaara1a118b2014-02-05 22:41:15 +0100485}
Bram Moolenaar0c7485f2015-01-14 14:04:10 +0100486# else
487void rb_gc_writebarrier_unprotect_stub(VALUE obj)
488{
489 dll_rb_gc_writebarrier_unprotect(obj);
490}
491# endif
492# endif
493
Bram Moolenaar3ca71f12010-10-27 16:49:47 +0200494static HINSTANCE hinstRuby = NULL; /* Instance of ruby.dll */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000495
496/*
Bram Moolenaarda2303d2005-08-30 21:55:26 +0000497 * Table of name to function pointer of ruby.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000498 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000499static struct
500{
501 char *name;
502 RUBY_PROC *ptr;
503} ruby_funcname_table[] =
504{
505 {"rb_assoc_new", (RUBY_PROC*)&dll_rb_assoc_new},
506 {"rb_cFalseClass", (RUBY_PROC*)&dll_rb_cFalseClass},
507 {"rb_cFixnum", (RUBY_PROC*)&dll_rb_cFixnum},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200508# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 20
Bram Moolenaardb3fbe52013-03-07 15:16:21 +0100509 {"rb_cFloat", (RUBY_PROC*)&dll_rb_cFloat},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200510# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000511 {"rb_cNilClass", (RUBY_PROC*)&dll_rb_cNilClass},
512 {"rb_cObject", (RUBY_PROC*)&dll_rb_cObject},
513 {"rb_cSymbol", (RUBY_PROC*)&dll_rb_cSymbol},
514 {"rb_cTrueClass", (RUBY_PROC*)&dll_rb_cTrueClass},
515 {"rb_check_type", (RUBY_PROC*)&dll_rb_check_type},
Bram Moolenaarf2f6d292015-12-28 20:57:10 +0100516# ifdef USE_TYPEDDATA
517 {"rb_check_typeddata", (RUBY_PROC*)&dll_rb_check_typeddata},
518# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000519 {"rb_class_path", (RUBY_PROC*)&dll_rb_class_path},
Bram Moolenaarf2f6d292015-12-28 20:57:10 +0100520# ifdef USE_TYPEDDATA
521# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 23
522 {"rb_data_typed_object_wrap", (RUBY_PROC*)&dll_rb_data_typed_object_wrap},
523# else
524 {"rb_data_typed_object_alloc", (RUBY_PROC*)&dll_rb_data_typed_object_alloc},
525# endif
526# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000527 {"rb_data_object_alloc", (RUBY_PROC*)&dll_rb_data_object_alloc},
Bram Moolenaarf2f6d292015-12-28 20:57:10 +0100528# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000529 {"rb_define_class_under", (RUBY_PROC*)&dll_rb_define_class_under},
530 {"rb_define_const", (RUBY_PROC*)&dll_rb_define_const},
531 {"rb_define_global_function", (RUBY_PROC*)&dll_rb_define_global_function},
532 {"rb_define_method", (RUBY_PROC*)&dll_rb_define_method},
533 {"rb_define_module", (RUBY_PROC*)&dll_rb_define_module},
534 {"rb_define_module_function", (RUBY_PROC*)&dll_rb_define_module_function},
535 {"rb_define_singleton_method", (RUBY_PROC*)&dll_rb_define_singleton_method},
536 {"rb_define_virtual_variable", (RUBY_PROC*)&dll_rb_define_virtual_variable},
537 {"rb_stdout", (RUBY_PROC*)&dll_rb_stdout},
538 {"rb_eArgError", (RUBY_PROC*)&dll_rb_eArgError},
539 {"rb_eIndexError", (RUBY_PROC*)&dll_rb_eIndexError},
540 {"rb_eRuntimeError", (RUBY_PROC*)&dll_rb_eRuntimeError},
541 {"rb_eStandardError", (RUBY_PROC*)&dll_rb_eStandardError},
542 {"rb_eval_string_protect", (RUBY_PROC*)&dll_rb_eval_string_protect},
543 {"rb_global_variable", (RUBY_PROC*)&dll_rb_global_variable},
544 {"rb_hash_aset", (RUBY_PROC*)&dll_rb_hash_aset},
545 {"rb_hash_new", (RUBY_PROC*)&dll_rb_hash_new},
546 {"rb_inspect", (RUBY_PROC*)&dll_rb_inspect},
547 {"rb_int2inum", (RUBY_PROC*)&dll_rb_int2inum},
Bram Moolenaara2aa31a2014-02-23 22:52:40 +0100548# if VIM_SIZEOF_INT < VIM_SIZEOF_LONG /* 64 bits only */
Bram Moolenaar2623b4f2012-09-18 16:36:32 +0200549 {"rb_fix2int", (RUBY_PROC*)&dll_rb_fix2int},
550 {"rb_num2int", (RUBY_PROC*)&dll_rb_num2int},
551 {"rb_num2uint", (RUBY_PROC*)&dll_rb_num2uint},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200552# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000553 {"rb_lastline_get", (RUBY_PROC*)&dll_rb_lastline_get},
554 {"rb_lastline_set", (RUBY_PROC*)&dll_rb_lastline_set},
555 {"rb_load_protect", (RUBY_PROC*)&dll_rb_load_protect},
556 {"rb_num2long", (RUBY_PROC*)&dll_rb_num2long},
557 {"rb_num2ulong", (RUBY_PROC*)&dll_rb_num2ulong},
558 {"rb_obj_alloc", (RUBY_PROC*)&dll_rb_obj_alloc},
559 {"rb_obj_as_string", (RUBY_PROC*)&dll_rb_obj_as_string},
560 {"rb_obj_id", (RUBY_PROC*)&dll_rb_obj_id},
561 {"rb_raise", (RUBY_PROC*)&dll_rb_raise},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200562# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 18
Bram Moolenaarf9b5ef82010-09-29 13:02:53 +0200563 {"rb_string_value", (RUBY_PROC*)&dll_rb_string_value},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200564# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000565 {"rb_str2cstr", (RUBY_PROC*)&dll_rb_str2cstr},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200566# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000567 {"rb_str_cat", (RUBY_PROC*)&dll_rb_str_cat},
568 {"rb_str_concat", (RUBY_PROC*)&dll_rb_str_concat},
569 {"rb_str_new", (RUBY_PROC*)&dll_rb_str_new},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200570# ifdef need_rb_str_new_cstr
Bram Moolenaar1d2beae2010-05-22 21:56:55 +0200571 {"rb_str_new_cstr", (RUBY_PROC*)&dll_rb_str_new_cstr},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200572# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000573 {"rb_str_new2", (RUBY_PROC*)&dll_rb_str_new2},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200574# endif
575# ifdef RUBY19_OR_LATER
Bram Moolenaar165641d2010-02-17 16:23:09 +0100576 {"rb_errinfo", (RUBY_PROC*)&dll_rb_errinfo},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200577# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000578 {"ruby_errinfo", (RUBY_PROC*)&dll_ruby_errinfo},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200579# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000580 {"ruby_init", (RUBY_PROC*)&dll_ruby_init},
581 {"ruby_init_loadpath", (RUBY_PROC*)&dll_ruby_init_loadpath},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200582# ifdef WIN3264
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100583 {
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200584# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER < 19
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100585 "NtInitialize",
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200586# else
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100587 "ruby_sysinit",
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200588# endif
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100589 (RUBY_PROC*)&dll_NtInitialize},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200590# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 18
Bram Moolenaar071d4272004-06-13 20:20:40 +0000591 {"rb_w32_snprintf", (RUBY_PROC*)&dll_rb_w32_snprintf},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200592# endif
Bram Moolenaar3ca71f12010-10-27 16:49:47 +0200593# endif
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200594# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 18
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100595 {"rb_string_value_ptr", (RUBY_PROC*)&dll_rb_string_value_ptr},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200596# if DYNAMIC_RUBY_VER <= 19
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100597 {"rb_float_new", (RUBY_PROC*)&dll_rb_float_new},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200598# else
Bram Moolenaar886ed692013-02-26 13:41:35 +0100599 {"rb_float_new_in_heap", (RUBY_PROC*)&dll_rb_float_new},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200600# endif
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100601 {"rb_ary_new", (RUBY_PROC*)&dll_rb_ary_new},
602 {"rb_ary_push", (RUBY_PROC*)&dll_rb_ary_push},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200603# endif
604# ifdef RUBY19_OR_LATER
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100605 {"rb_int2big", (RUBY_PROC*)&dll_rb_int2big},
Bram Moolenaar165641d2010-02-17 16:23:09 +0100606 {"ruby_script", (RUBY_PROC*)&dll_ruby_script},
607 {"rb_enc_find_index", (RUBY_PROC*)&dll_rb_enc_find_index},
608 {"rb_enc_find", (RUBY_PROC*)&dll_rb_enc_find},
609 {"rb_enc_str_new", (RUBY_PROC*)&dll_rb_enc_str_new},
610 {"rb_sprintf", (RUBY_PROC*)&dll_rb_sprintf},
Bram Moolenaar7a8ef142010-12-24 13:39:35 +0100611 {"rb_require", (RUBY_PROC*)&dll_rb_require},
Bram Moolenaar9b1067e2015-11-19 19:33:15 +0100612 {"ruby_options", (RUBY_PROC*)&dll_ruby_options},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200613# endif
Bram Moolenaar10f3a792013-05-20 12:52:29 +0200614# if defined(RUBY19_OR_LATER) || defined(RUBY_INIT_STACK)
615# ifdef __ia64
616 {"rb_ia64_bsp", (RUBY_PROC*)&dll_rb_ia64_bsp},
617# endif
618 {"ruby_init_stack", (RUBY_PROC*)&dll_ruby_init_stack},
619# endif
Bram Moolenaara1a118b2014-02-05 22:41:15 +0100620# if defined(USE_RGENGC) && USE_RGENGC
Bram Moolenaar0c7485f2015-01-14 14:04:10 +0100621# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER == 21
Bram Moolenaara1a118b2014-02-05 22:41:15 +0100622 {"rb_gc_writebarrier_unprotect_promoted", (RUBY_PROC*)&dll_rb_gc_writebarrier_unprotect_promoted},
Bram Moolenaar0c7485f2015-01-14 14:04:10 +0100623# else
624 {"rb_gc_writebarrier_unprotect", (RUBY_PROC*)&dll_rb_gc_writebarrier_unprotect},
625# endif
Bram Moolenaara1a118b2014-02-05 22:41:15 +0100626# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000627 {"", NULL},
628};
629
630/*
631 * Free ruby.dll
632 */
633 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100634end_dynamic_ruby(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000635{
636 if (hinstRuby)
637 {
Bram Moolenaarf9b5ef82010-09-29 13:02:53 +0200638 close_dll(hinstRuby);
Bram Moolenaar3ca71f12010-10-27 16:49:47 +0200639 hinstRuby = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000640 }
641}
642
643/*
644 * Load library and get all pointers.
645 * Parameter 'libname' provides name of DLL.
646 * Return OK or FAIL.
647 */
648 static int
649ruby_runtime_link_init(char *libname, int verbose)
650{
651 int i;
652
653 if (hinstRuby)
654 return OK;
Bram Moolenaarf9b5ef82010-09-29 13:02:53 +0200655 hinstRuby = load_dll(libname);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000656 if (!hinstRuby)
657 {
658 if (verbose)
659 EMSG2(_(e_loadlib), libname);
660 return FAIL;
661 }
662
663 for (i = 0; ruby_funcname_table[i].ptr; ++i)
664 {
Bram Moolenaarf9b5ef82010-09-29 13:02:53 +0200665 if (!(*ruby_funcname_table[i].ptr = symbol_from_dll(hinstRuby,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000666 ruby_funcname_table[i].name)))
667 {
Bram Moolenaarf9b5ef82010-09-29 13:02:53 +0200668 close_dll(hinstRuby);
Bram Moolenaar3ca71f12010-10-27 16:49:47 +0200669 hinstRuby = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000670 if (verbose)
671 EMSG2(_(e_loadfunc), ruby_funcname_table[i].name);
672 return FAIL;
673 }
674 }
675 return OK;
676}
677
678/*
679 * If ruby is enabled (there is installed ruby on Windows system) return TRUE,
680 * else FALSE.
681 */
682 int
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100683ruby_enabled(int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000684{
Bram Moolenaar25e4fcd2016-01-09 14:57:47 +0100685 return ruby_runtime_link_init((char *)p_rubydll, verbose) == OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000686}
687#endif /* defined(DYNAMIC_RUBY) || defined(PROTO) */
688
689 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100690ruby_end(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000691{
692#ifdef DYNAMIC_RUBY
693 end_dynamic_ruby();
694#endif
695}
696
697void ex_ruby(exarg_T *eap)
698{
699 int state;
700 char *script = NULL;
701
Bram Moolenaar35a2e192006-03-13 22:07:11 +0000702 script = (char *)script_get(eap, eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000703 if (!eap->skip && ensure_ruby_initialized())
704 {
705 if (script == NULL)
706 rb_eval_string_protect((char *)eap->arg, &state);
707 else
708 rb_eval_string_protect(script, &state);
709 if (state)
710 error_print(state);
711 }
712 vim_free(script);
713}
714
Bram Moolenaar165641d2010-02-17 16:23:09 +0100715/*
716 * In Ruby 1.9 or later, ruby String object has encoding.
717 * conversion buffer string of vim to ruby String object using
718 * VIM encoding option.
719 */
720 static VALUE
721vim_str2rb_enc_str(const char *s)
722{
Bram Moolenaar639a2552010-03-17 18:15:23 +0100723#ifdef RUBY19_OR_LATER
Bram Moolenaar165641d2010-02-17 16:23:09 +0100724 int isnum;
725 long lval;
726 char_u *sval;
727 rb_encoding *enc;
728
729 isnum = get_option_value((char_u *)"enc", &lval, &sval, 0);
730 if (isnum == 0)
731 {
732 enc = rb_enc_find((char *)sval);
733 vim_free(sval);
Bram Moolenaar758535a2016-03-30 22:06:16 +0200734 if (enc)
735 {
Bram Moolenaar9b0ac222016-06-01 20:31:43 +0200736 return rb_enc_str_new(s, (long)strlen(s), enc);
Bram Moolenaar165641d2010-02-17 16:23:09 +0100737 }
738 }
739#endif
740 return rb_str_new2(s);
741}
742
743 static VALUE
744eval_enc_string_protect(const char *str, int *state)
745{
Bram Moolenaar639a2552010-03-17 18:15:23 +0100746#ifdef RUBY19_OR_LATER
Bram Moolenaar165641d2010-02-17 16:23:09 +0100747 int isnum;
748 long lval;
749 char_u *sval;
750 rb_encoding *enc;
751 VALUE v;
752
753 isnum = get_option_value((char_u *)"enc", &lval, &sval, 0);
754 if (isnum == 0)
755 {
756 enc = rb_enc_find((char *)sval);
757 vim_free(sval);
758 if (enc)
759 {
760 v = rb_sprintf("#-*- coding:%s -*-\n%s", rb_enc_name(enc), str);
761 return rb_eval_string_protect(StringValuePtr(v), state);
762 }
763 }
764#endif
765 return rb_eval_string_protect(str, state);
766}
767
Bram Moolenaar071d4272004-06-13 20:20:40 +0000768void ex_rubydo(exarg_T *eap)
769{
770 int state;
771 linenr_T i;
772
773 if (ensure_ruby_initialized())
774 {
775 if (u_save(eap->line1 - 1, eap->line2 + 1) != OK)
776 return;
Bram Moolenaar758535a2016-03-30 22:06:16 +0200777 for (i = eap->line1; i <= eap->line2; i++)
778 {
Bram Moolenaare980d8a2010-12-08 13:11:21 +0100779 VALUE line;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000780
Bram Moolenaare980d8a2010-12-08 13:11:21 +0100781 line = vim_str2rb_enc_str((char *)ml_get(i));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000782 rb_lastline_set(line);
Bram Moolenaar165641d2010-02-17 16:23:09 +0100783 eval_enc_string_protect((char *) eap->arg, &state);
Bram Moolenaar758535a2016-03-30 22:06:16 +0200784 if (state)
785 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000786 error_print(state);
787 break;
788 }
789 line = rb_lastline_get();
Bram Moolenaar758535a2016-03-30 22:06:16 +0200790 if (!NIL_P(line))
791 {
792 if (TYPE(line) != T_STRING)
793 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000794 EMSG(_("E265: $_ must be an instance of String"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000795 return;
796 }
Bram Moolenaar165641d2010-02-17 16:23:09 +0100797 ml_replace(i, (char_u *) StringValuePtr(line), 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000798 changed();
799#ifdef SYNTAX_HL
800 syn_changed(i); /* recompute syntax hl. for this line */
801#endif
802 }
803 }
804 check_cursor();
805 update_curbuf(NOT_VALID);
806 }
807}
808
809void ex_rubyfile(exarg_T *eap)
810{
811 int state;
812
813 if (ensure_ruby_initialized())
814 {
815 rb_load_protect(rb_str_new2((char *) eap->arg), 0, &state);
816 if (state) error_print(state);
817 }
818}
819
820void ruby_buffer_free(buf_T *buf)
821{
Bram Moolenaare344bea2005-09-01 20:46:49 +0000822 if (buf->b_ruby_ref)
823 {
824 rb_hash_aset(objtbl, rb_obj_id((VALUE) buf->b_ruby_ref), Qnil);
825 RDATA(buf->b_ruby_ref)->data = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000826 }
827}
828
829void ruby_window_free(win_T *win)
830{
Bram Moolenaare344bea2005-09-01 20:46:49 +0000831 if (win->w_ruby_ref)
832 {
833 rb_hash_aset(objtbl, rb_obj_id((VALUE) win->w_ruby_ref), Qnil);
834 RDATA(win->w_ruby_ref)->data = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000835 }
836}
837
838static int ensure_ruby_initialized(void)
839{
840 if (!ruby_initialized)
841 {
842#ifdef DYNAMIC_RUBY
843 if (ruby_enabled(TRUE))
844 {
845#endif
Bram Moolenaar0b69c732010-02-17 15:11:50 +0100846#ifdef _WIN32
847 /* suggested by Ariya Mizutani */
848 int argc = 1;
849 char *argv[] = {"gvim.exe"};
Bram Moolenaar90140742014-11-27 17:44:08 +0100850 char **argvp = argv;
851 NtInitialize(&argc, &argvp);
Bram Moolenaar0b69c732010-02-17 15:11:50 +0100852#endif
Bram Moolenaarb2c03502010-07-02 20:20:09 +0200853 {
Bram Moolenaar10f3a792013-05-20 12:52:29 +0200854#if defined(RUBY19_OR_LATER) || defined(RUBY_INIT_STACK)
Bram Moolenaar99685e62013-05-11 13:56:18 +0200855 ruby_init_stack(ruby_stack_start);
Bram Moolenaar165641d2010-02-17 16:23:09 +0100856#endif
Bram Moolenaarb2c03502010-07-02 20:20:09 +0200857 ruby_init();
858 }
Bram Moolenaar639a2552010-03-17 18:15:23 +0100859#ifdef RUBY19_OR_LATER
Bram Moolenaar7a8ef142010-12-24 13:39:35 +0100860 {
861 int dummy_argc = 2;
862 char *dummy_argv[] = {"vim-ruby", "-e0"};
Bram Moolenaar9b1067e2015-11-19 19:33:15 +0100863 ruby_options(dummy_argc, dummy_argv);
Bram Moolenaar7a8ef142010-12-24 13:39:35 +0100864 }
Bram Moolenaar165641d2010-02-17 16:23:09 +0100865 ruby_script("vim-ruby");
Bram Moolenaar7a8ef142010-12-24 13:39:35 +0100866#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000867 ruby_init_loadpath();
Bram Moolenaar165641d2010-02-17 16:23:09 +0100868#endif
Bram Moolenaar7a8ef142010-12-24 13:39:35 +0100869 ruby_io_init();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000870 ruby_vim_init();
871 ruby_initialized = 1;
872#ifdef DYNAMIC_RUBY
873 }
874 else
875 {
876 EMSG(_("E266: Sorry, this command is disabled, the Ruby library could not be loaded."));
877 return 0;
878 }
879#endif
880 }
881 return ruby_initialized;
882}
883
884static void error_print(int state)
885{
886#ifndef DYNAMIC_RUBY
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100887#if !(defined(RUBY_VERSION) && RUBY_VERSION >= 19) \
888 && !(defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 19)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000889 RUBYEXTERN VALUE ruby_errinfo;
890#endif
Bram Moolenaar165641d2010-02-17 16:23:09 +0100891#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000892 VALUE eclass;
893 VALUE einfo;
894 char buff[BUFSIZ];
895
896#define TAG_RETURN 0x1
897#define TAG_BREAK 0x2
898#define TAG_NEXT 0x3
899#define TAG_RETRY 0x4
900#define TAG_REDO 0x5
901#define TAG_RAISE 0x6
902#define TAG_THROW 0x7
903#define TAG_FATAL 0x8
904#define TAG_MASK 0xf
905
Bram Moolenaar758535a2016-03-30 22:06:16 +0200906 switch (state)
907 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000908 case TAG_RETURN:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000909 EMSG(_("E267: unexpected return"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000910 break;
911 case TAG_NEXT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000912 EMSG(_("E268: unexpected next"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000913 break;
914 case TAG_BREAK:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000915 EMSG(_("E269: unexpected break"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000916 break;
917 case TAG_REDO:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000918 EMSG(_("E270: unexpected redo"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000919 break;
920 case TAG_RETRY:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000921 EMSG(_("E271: retry outside of rescue clause"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000922 break;
923 case TAG_RAISE:
924 case TAG_FATAL:
Bram Moolenaar639a2552010-03-17 18:15:23 +0100925#ifdef RUBY19_OR_LATER
Bram Moolenaar165641d2010-02-17 16:23:09 +0100926 eclass = CLASS_OF(rb_errinfo());
927 einfo = rb_obj_as_string(rb_errinfo());
928#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000929 eclass = CLASS_OF(ruby_errinfo);
930 einfo = rb_obj_as_string(ruby_errinfo);
Bram Moolenaar165641d2010-02-17 16:23:09 +0100931#endif
Bram Moolenaar758535a2016-03-30 22:06:16 +0200932 if (eclass == rb_eRuntimeError && RSTRING_LEN(einfo) == 0)
933 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000934 EMSG(_("E272: unhandled exception"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000935 }
Bram Moolenaar758535a2016-03-30 22:06:16 +0200936 else
937 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000938 VALUE epath;
939 char *p;
940
941 epath = rb_class_path(eclass);
Bram Moolenaar9c13b352005-05-19 20:53:52 +0000942 vim_snprintf(buff, BUFSIZ, "%s: %s",
Bram Moolenaar165641d2010-02-17 16:23:09 +0100943 RSTRING_PTR(epath), RSTRING_PTR(einfo));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000944 p = strchr(buff, '\n');
945 if (p) *p = '\0';
946 EMSG(buff);
947 }
948 break;
949 default:
Bram Moolenaar9c13b352005-05-19 20:53:52 +0000950 vim_snprintf(buff, BUFSIZ, _("E273: unknown longjmp status %d"), state);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000951 EMSG(buff);
952 break;
953 }
954}
955
Bram Moolenaarcd8b20a2009-05-22 16:20:57 +0000956static VALUE vim_message(VALUE self UNUSED, VALUE str)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000957{
958 char *buff, *p;
959
960 str = rb_obj_as_string(str);
Bram Moolenaar3f5f7952011-08-04 19:34:59 +0200961 if (RSTRING_LEN(str) > 0)
962 {
963 /* Only do this when the string isn't empty, alloc(0) causes trouble. */
964 buff = ALLOCA_N(char, RSTRING_LEN(str));
965 strcpy(buff, RSTRING_PTR(str));
966 p = strchr(buff, '\n');
967 if (p) *p = '\0';
968 MSG(buff);
969 }
970 else
971 {
972 MSG("");
973 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000974 return Qnil;
975}
976
Bram Moolenaarcd8b20a2009-05-22 16:20:57 +0000977static VALUE vim_set_option(VALUE self UNUSED, VALUE str)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000978{
Bram Moolenaar165641d2010-02-17 16:23:09 +0100979 do_set((char_u *)StringValuePtr(str), 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000980 update_screen(NOT_VALID);
981 return Qnil;
982}
983
Bram Moolenaarcd8b20a2009-05-22 16:20:57 +0000984static VALUE vim_command(VALUE self UNUSED, VALUE str)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000985{
Bram Moolenaar165641d2010-02-17 16:23:09 +0100986 do_cmdline_cmd((char_u *)StringValuePtr(str));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000987 return Qnil;
988}
989
Bram Moolenaar3fac56e2010-02-24 15:48:04 +0100990#ifdef FEAT_EVAL
991static VALUE vim_to_ruby(typval_T *tv)
992{
993 VALUE result = Qnil;
994
995 if (tv->v_type == VAR_STRING)
996 {
Bram Moolenaar94127e42010-03-19 23:08:48 +0100997 result = rb_str_new2(tv->vval.v_string == NULL
998 ? "" : (char *)(tv->vval.v_string));
Bram Moolenaar3fac56e2010-02-24 15:48:04 +0100999 }
1000 else if (tv->v_type == VAR_NUMBER)
1001 {
Bram Moolenaar639a2552010-03-17 18:15:23 +01001002 result = INT2NUM(tv->vval.v_number);
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001003 }
1004# ifdef FEAT_FLOAT
1005 else if (tv->v_type == VAR_FLOAT)
1006 {
Bram Moolenaar639a2552010-03-17 18:15:23 +01001007 result = rb_float_new(tv->vval.v_float);
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001008 }
1009# endif
1010 else if (tv->v_type == VAR_LIST)
1011 {
Bram Moolenaar639a2552010-03-17 18:15:23 +01001012 list_T *list = tv->vval.v_list;
1013 listitem_T *curr;
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001014
Bram Moolenaar639a2552010-03-17 18:15:23 +01001015 result = rb_ary_new();
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001016
Bram Moolenaar639a2552010-03-17 18:15:23 +01001017 if (list != NULL)
1018 {
1019 for (curr = list->lv_first; curr != NULL; curr = curr->li_next)
1020 {
1021 rb_ary_push(result, vim_to_ruby(&curr->li_tv));
1022 }
1023 }
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001024 }
1025 else if (tv->v_type == VAR_DICT)
1026 {
Bram Moolenaar639a2552010-03-17 18:15:23 +01001027 result = rb_hash_new();
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001028
Bram Moolenaar639a2552010-03-17 18:15:23 +01001029 if (tv->vval.v_dict != NULL)
1030 {
1031 hashtab_T *ht = &tv->vval.v_dict->dv_hashtab;
1032 long_u todo = ht->ht_used;
1033 hashitem_T *hi;
1034 dictitem_T *di;
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001035
Bram Moolenaar639a2552010-03-17 18:15:23 +01001036 for (hi = ht->ht_array; todo > 0; ++hi)
1037 {
1038 if (!HASHITEM_EMPTY(hi))
1039 {
1040 --todo;
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001041
Bram Moolenaar639a2552010-03-17 18:15:23 +01001042 di = dict_lookup(hi);
1043 rb_hash_aset(result, rb_str_new2((char *)hi->hi_key),
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001044 vim_to_ruby(&di->di_tv));
Bram Moolenaar639a2552010-03-17 18:15:23 +01001045 }
1046 }
1047 }
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001048 }
1049 else if (tv->v_type == VAR_SPECIAL)
1050 {
1051 if (tv->vval.v_number <= VVAL_TRUE)
1052 result = INT2NUM(tv->vval.v_number);
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001053 } /* else return Qnil; */
1054
1055 return result;
1056}
1057#endif
1058
Bram Moolenaarcd8b20a2009-05-22 16:20:57 +00001059static VALUE vim_evaluate(VALUE self UNUSED, VALUE str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001060{
1061#ifdef FEAT_EVAL
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001062 typval_T *tv;
1063 VALUE result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001064
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001065 tv = eval_expr((char_u *)StringValuePtr(str), NULL);
1066 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001067 {
Bram Moolenaar639a2552010-03-17 18:15:23 +01001068 return Qnil;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001069 }
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001070 result = vim_to_ruby(tv);
1071
1072 free_tv(tv);
1073
1074 return result;
1075#else
1076 return Qnil;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001077#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001078}
1079
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001080#ifdef USE_TYPEDDATA
1081static size_t buffer_dsize(const void *buf);
1082
1083static const rb_data_type_t buffer_type = {
1084 "vim_buffer",
1085 {0, 0, buffer_dsize, {0, 0}},
1086 0, 0,
1087# ifdef RUBY_TYPED_FREE_IMMEDIATELY
1088 0,
1089# endif
1090};
1091
1092static size_t buffer_dsize(const void *buf UNUSED)
1093{
1094 return sizeof(buf_T);
1095}
1096#endif
1097
Bram Moolenaar071d4272004-06-13 20:20:40 +00001098static VALUE buffer_new(buf_T *buf)
1099{
Bram Moolenaare344bea2005-09-01 20:46:49 +00001100 if (buf->b_ruby_ref)
1101 {
1102 return (VALUE) buf->b_ruby_ref;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001103 }
Bram Moolenaare344bea2005-09-01 20:46:49 +00001104 else
1105 {
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001106#ifdef USE_TYPEDDATA
1107 VALUE obj = TypedData_Wrap_Struct(cBuffer, &buffer_type, buf);
1108#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001109 VALUE obj = Data_Wrap_Struct(cBuffer, 0, 0, buf);
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001110#endif
Bram Moolenaare344bea2005-09-01 20:46:49 +00001111 buf->b_ruby_ref = (void *) obj;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001112 rb_hash_aset(objtbl, rb_obj_id(obj), obj);
1113 return obj;
1114 }
1115}
1116
1117static buf_T *get_buf(VALUE obj)
1118{
1119 buf_T *buf;
1120
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001121#ifdef USE_TYPEDDATA
1122 TypedData_Get_Struct(obj, buf_T, &buffer_type, buf);
1123#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001124 Data_Get_Struct(obj, buf_T, buf);
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001125#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001126 if (buf == NULL)
1127 rb_raise(eDeletedBufferError, "attempt to refer to deleted buffer");
1128 return buf;
1129}
1130
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001131static VALUE buffer_s_current(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001132{
1133 return buffer_new(curbuf);
1134}
1135
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001136static VALUE buffer_s_count(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001137{
1138 buf_T *b;
1139 int n = 0;
1140
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001141 for (b = firstbuf; b != NULL; b = b->b_next)
1142 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001143 /* Deleted buffers should not be counted
1144 * SegPhault - 01/07/05 */
1145 if (b->b_p_bl)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001146 n++;
1147 }
1148
Bram Moolenaar071d4272004-06-13 20:20:40 +00001149 return INT2NUM(n);
1150}
1151
Bram Moolenaarcd8b20a2009-05-22 16:20:57 +00001152static VALUE buffer_s_aref(VALUE self UNUSED, VALUE num)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001153{
1154 buf_T *b;
1155 int n = NUM2INT(num);
1156
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001157 for (b = firstbuf; b != NULL; b = b->b_next)
1158 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001159 /* Deleted buffers should not be counted
1160 * SegPhault - 01/07/05 */
1161 if (!b->b_p_bl)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001162 continue;
1163
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001164 if (n == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001165 return buffer_new(b);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001166
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001167 n--;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001168 }
1169 return Qnil;
1170}
1171
1172static VALUE buffer_name(VALUE self)
1173{
1174 buf_T *buf = get_buf(self);
1175
Bram Moolenaar35a2e192006-03-13 22:07:11 +00001176 return buf->b_ffname ? rb_str_new2((char *)buf->b_ffname) : Qnil;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001177}
1178
1179static VALUE buffer_number(VALUE self)
1180{
1181 buf_T *buf = get_buf(self);
1182
1183 return INT2NUM(buf->b_fnum);
1184}
1185
1186static VALUE buffer_count(VALUE self)
1187{
1188 buf_T *buf = get_buf(self);
1189
1190 return INT2NUM(buf->b_ml.ml_line_count);
1191}
1192
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001193static VALUE get_buffer_line(buf_T *buf, linenr_T n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001194{
Bram Moolenaar3c531602010-11-16 14:46:19 +01001195 if (n <= 0 || n > buf->b_ml.ml_line_count)
1196 rb_raise(rb_eIndexError, "line number %ld out of range", (long)n);
1197 return vim_str2rb_enc_str((char *)ml_get_buf(buf, n, FALSE));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001198}
1199
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001200static VALUE buffer_aref(VALUE self, VALUE num)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001201{
1202 buf_T *buf = get_buf(self);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001203
1204 if (buf != NULL)
1205 return get_buffer_line(buf, (linenr_T)NUM2LONG(num));
1206 return Qnil; /* For stop warning */
1207}
1208
1209static VALUE set_buffer_line(buf_T *buf, linenr_T n, VALUE str)
1210{
Bram Moolenaar165641d2010-02-17 16:23:09 +01001211 char *line = StringValuePtr(str);
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001212 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001213
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001214 if (n > 0 && n <= buf->b_ml.ml_line_count && line != NULL)
1215 {
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001216 /* set curwin/curbuf for "buf" and save some things */
1217 aucmd_prepbuf(&aco, buf);
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001218
Bram Moolenaar758535a2016-03-30 22:06:16 +02001219 if (u_savesub(n) == OK)
1220 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001221 ml_replace(n, (char_u *)line, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001222 changed();
1223#ifdef SYNTAX_HL
1224 syn_changed(n); /* recompute syntax hl. for this line */
1225#endif
1226 }
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001227
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001228 /* restore curwin/curbuf and a few other things */
1229 aucmd_restbuf(&aco);
1230 /* Careful: autocommands may have made "buf" invalid! */
Bram Moolenaarf30e74c2006-08-16 17:35:00 +00001231
Bram Moolenaar071d4272004-06-13 20:20:40 +00001232 update_curbuf(NOT_VALID);
1233 }
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001234 else
1235 {
Bram Moolenaar165641d2010-02-17 16:23:09 +01001236 rb_raise(rb_eIndexError, "line number %ld out of range", (long)n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001237 }
1238 return str;
1239}
1240
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001241static VALUE buffer_aset(VALUE self, VALUE num, VALUE str)
1242{
1243 buf_T *buf = get_buf(self);
1244
1245 if (buf != NULL)
1246 return set_buffer_line(buf, (linenr_T)NUM2LONG(num), str);
1247 return str;
1248}
1249
Bram Moolenaar071d4272004-06-13 20:20:40 +00001250static VALUE buffer_delete(VALUE self, VALUE num)
1251{
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001252 buf_T *buf = get_buf(self);
1253 long n = NUM2LONG(num);
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001254 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001255
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001256 if (n > 0 && n <= buf->b_ml.ml_line_count)
1257 {
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001258 /* set curwin/curbuf for "buf" and save some things */
1259 aucmd_prepbuf(&aco, buf);
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001260
Bram Moolenaar758535a2016-03-30 22:06:16 +02001261 if (u_savedel(n, 1) == OK)
1262 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001263 ml_delete(n, 0);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001264
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001265 /* Changes to non-active buffers should properly refresh
1266 * SegPhault - 01/09/05 */
1267 deleted_lines_mark(n, 1L);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001268
Bram Moolenaar071d4272004-06-13 20:20:40 +00001269 changed();
1270 }
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001271
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001272 /* restore curwin/curbuf and a few other things */
1273 aucmd_restbuf(&aco);
1274 /* Careful: autocommands may have made "buf" invalid! */
Bram Moolenaarf30e74c2006-08-16 17:35:00 +00001275
Bram Moolenaar071d4272004-06-13 20:20:40 +00001276 update_curbuf(NOT_VALID);
1277 }
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001278 else
1279 {
Bram Moolenaar165641d2010-02-17 16:23:09 +01001280 rb_raise(rb_eIndexError, "line number %ld out of range", n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001281 }
1282 return Qnil;
1283}
1284
1285static VALUE buffer_append(VALUE self, VALUE num, VALUE str)
1286{
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001287 buf_T *buf = get_buf(self);
Bram Moolenaar165641d2010-02-17 16:23:09 +01001288 char *line = StringValuePtr(str);
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001289 long n = NUM2LONG(num);
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001290 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001291
Bram Moolenaar3c531602010-11-16 14:46:19 +01001292 if (line == NULL)
1293 {
Bram Moolenaar165641d2010-02-17 16:23:09 +01001294 rb_raise(rb_eIndexError, "NULL line");
1295 }
1296 else if (n >= 0 && n <= buf->b_ml.ml_line_count)
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001297 {
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001298 /* set curwin/curbuf for "buf" and save some things */
1299 aucmd_prepbuf(&aco, buf);
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001300
Bram Moolenaar758535a2016-03-30 22:06:16 +02001301 if (u_inssub(n + 1) == OK)
1302 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001303 ml_append(n, (char_u *) line, (colnr_T) 0, FALSE);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001304
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001305 /* Changes to non-active buffers should properly refresh screen
1306 * SegPhault - 12/20/04 */
1307 appended_lines_mark(n, 1L);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001308
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001309 changed();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001310 }
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001311
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001312 /* restore curwin/curbuf and a few other things */
1313 aucmd_restbuf(&aco);
1314 /* Careful: autocommands may have made "buf" invalid! */
Bram Moolenaarf30e74c2006-08-16 17:35:00 +00001315
Bram Moolenaar071d4272004-06-13 20:20:40 +00001316 update_curbuf(NOT_VALID);
1317 }
Bram Moolenaar3c531602010-11-16 14:46:19 +01001318 else
1319 {
Bram Moolenaar165641d2010-02-17 16:23:09 +01001320 rb_raise(rb_eIndexError, "line number %ld out of range", n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001321 }
1322 return str;
1323}
1324
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001325#ifdef USE_TYPEDDATA
1326static size_t window_dsize(const void *buf);
1327
1328static const rb_data_type_t window_type = {
1329 "vim_window",
1330 {0, 0, window_dsize, {0, 0}},
1331 0, 0,
1332# ifdef RUBY_TYPED_FREE_IMMEDIATELY
1333 0,
1334# endif
1335};
1336
1337static size_t window_dsize(const void *win UNUSED)
1338{
1339 return sizeof(win_T);
1340}
1341#endif
1342
Bram Moolenaar071d4272004-06-13 20:20:40 +00001343static VALUE window_new(win_T *win)
1344{
Bram Moolenaare344bea2005-09-01 20:46:49 +00001345 if (win->w_ruby_ref)
1346 {
1347 return (VALUE) win->w_ruby_ref;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001348 }
Bram Moolenaare344bea2005-09-01 20:46:49 +00001349 else
1350 {
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001351#ifdef USE_TYPEDDATA
1352 VALUE obj = TypedData_Wrap_Struct(cVimWindow, &window_type, win);
1353#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001354 VALUE obj = Data_Wrap_Struct(cVimWindow, 0, 0, win);
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001355#endif
Bram Moolenaare344bea2005-09-01 20:46:49 +00001356 win->w_ruby_ref = (void *) obj;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001357 rb_hash_aset(objtbl, rb_obj_id(obj), obj);
1358 return obj;
1359 }
1360}
1361
1362static win_T *get_win(VALUE obj)
1363{
1364 win_T *win;
1365
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001366#ifdef USE_TYPEDDATA
1367 TypedData_Get_Struct(obj, win_T, &window_type, win);
1368#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001369 Data_Get_Struct(obj, win_T, win);
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001370#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001371 if (win == NULL)
1372 rb_raise(eDeletedWindowError, "attempt to refer to deleted window");
1373 return win;
1374}
1375
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001376static VALUE window_s_current(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001377{
1378 return window_new(curwin);
1379}
1380
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001381/*
1382 * Added line manipulation functions
1383 * SegPhault - 03/07/05
1384 */
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001385static VALUE line_s_current(void)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001386{
1387 return get_buffer_line(curbuf, curwin->w_cursor.lnum);
1388}
1389
Bram Moolenaarcd8b20a2009-05-22 16:20:57 +00001390static VALUE set_current_line(VALUE self UNUSED, VALUE str)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001391{
1392 return set_buffer_line(curbuf, curwin->w_cursor.lnum, str);
1393}
1394
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001395static VALUE current_line_number(void)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001396{
1397 return INT2FIX((int)curwin->w_cursor.lnum);
1398}
1399
1400
1401
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001402static VALUE window_s_count(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001403{
1404#ifdef FEAT_WINDOWS
1405 win_T *w;
1406 int n = 0;
1407
Bram Moolenaarf740b292006-02-16 22:11:02 +00001408 for (w = firstwin; w != NULL; w = w->w_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001409 n++;
1410 return INT2NUM(n);
1411#else
1412 return INT2NUM(1);
1413#endif
1414}
1415
Bram Moolenaarcd8b20a2009-05-22 16:20:57 +00001416static VALUE window_s_aref(VALUE self UNUSED, VALUE num)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001417{
1418 win_T *w;
1419 int n = NUM2INT(num);
1420
1421#ifndef FEAT_WINDOWS
1422 w = curwin;
1423#else
1424 for (w = firstwin; w != NULL; w = w->w_next, --n)
1425#endif
1426 if (n == 0)
1427 return window_new(w);
1428 return Qnil;
1429}
1430
1431static VALUE window_buffer(VALUE self)
1432{
1433 win_T *win = get_win(self);
1434
1435 return buffer_new(win->w_buffer);
1436}
1437
1438static VALUE window_height(VALUE self)
1439{
1440 win_T *win = get_win(self);
1441
1442 return INT2NUM(win->w_height);
1443}
1444
1445static VALUE window_set_height(VALUE self, VALUE height)
1446{
1447 win_T *win = get_win(self);
1448 win_T *savewin = curwin;
1449
1450 curwin = win;
1451 win_setheight(NUM2INT(height));
1452 curwin = savewin;
1453 return height;
1454}
1455
Bram Moolenaarfeeaa682013-02-14 22:19:51 +01001456static VALUE window_width(VALUE self UNUSED)
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001457{
Bram Moolenaarfeeaa682013-02-14 22:19:51 +01001458 return INT2NUM(W_WIDTH(get_win(self)));
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001459}
1460
Bram Moolenaarfeeaa682013-02-14 22:19:51 +01001461static VALUE window_set_width(VALUE self UNUSED, VALUE width)
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001462{
Bram Moolenaar44a2f922016-03-19 22:11:51 +01001463#ifdef FEAT_WINDOWS
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001464 win_T *win = get_win(self);
1465 win_T *savewin = curwin;
1466
1467 curwin = win;
1468 win_setwidth(NUM2INT(width));
1469 curwin = savewin;
Bram Moolenaarfeeaa682013-02-14 22:19:51 +01001470#endif
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001471 return width;
1472}
1473
Bram Moolenaar071d4272004-06-13 20:20:40 +00001474static VALUE window_cursor(VALUE self)
1475{
1476 win_T *win = get_win(self);
1477
1478 return rb_assoc_new(INT2NUM(win->w_cursor.lnum), INT2NUM(win->w_cursor.col));
1479}
1480
1481static VALUE window_set_cursor(VALUE self, VALUE pos)
1482{
1483 VALUE lnum, col;
1484 win_T *win = get_win(self);
1485
1486 Check_Type(pos, T_ARRAY);
Bram Moolenaar165641d2010-02-17 16:23:09 +01001487 if (RARRAY_LEN(pos) != 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001488 rb_raise(rb_eArgError, "array length must be 2");
Bram Moolenaar165641d2010-02-17 16:23:09 +01001489 lnum = RARRAY_PTR(pos)[0];
1490 col = RARRAY_PTR(pos)[1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001491 win->w_cursor.lnum = NUM2LONG(lnum);
1492 win->w_cursor.col = NUM2UINT(col);
1493 check_cursor(); /* put cursor on an existing line */
1494 update_screen(NOT_VALID);
1495 return Qnil;
1496}
1497
Bram Moolenaar6217cdc2012-04-25 12:28:09 +02001498static VALUE f_nop(VALUE self UNUSED)
Bram Moolenaar35df7d22012-04-20 18:05:47 +02001499{
1500 return Qnil;
1501}
1502
Bram Moolenaarcd8b20a2009-05-22 16:20:57 +00001503static VALUE f_p(int argc, VALUE *argv, VALUE self UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001504{
1505 int i;
1506 VALUE str = rb_str_new("", 0);
1507
Bram Moolenaar758535a2016-03-30 22:06:16 +02001508 for (i = 0; i < argc; i++)
1509 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001510 if (i > 0) rb_str_cat(str, ", ", 2);
1511 rb_str_concat(str, rb_inspect(argv[i]));
1512 }
Bram Moolenaar165641d2010-02-17 16:23:09 +01001513 MSG(RSTRING_PTR(str));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001514 return Qnil;
1515}
1516
1517static void ruby_io_init(void)
1518{
1519#ifndef DYNAMIC_RUBY
1520 RUBYEXTERN VALUE rb_stdout;
1521#endif
1522
1523 rb_stdout = rb_obj_alloc(rb_cObject);
1524 rb_define_singleton_method(rb_stdout, "write", vim_message, 1);
Bram Moolenaar35df7d22012-04-20 18:05:47 +02001525 rb_define_singleton_method(rb_stdout, "flush", f_nop, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001526 rb_define_global_function("p", f_p, -1);
1527}
1528
1529static void ruby_vim_init(void)
1530{
1531 objtbl = rb_hash_new();
1532 rb_global_variable(&objtbl);
1533
Bram Moolenaarf711faf2007-05-10 16:48:19 +00001534 /* The Vim module used to be called "VIM", but "Vim" is better. Make an
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02001535 * alias "VIM" for backwards compatibility. */
Bram Moolenaarf711faf2007-05-10 16:48:19 +00001536 mVIM = rb_define_module("Vim");
1537 rb_define_const(rb_cObject, "VIM", mVIM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001538 rb_define_const(mVIM, "VERSION_MAJOR", INT2NUM(VIM_VERSION_MAJOR));
1539 rb_define_const(mVIM, "VERSION_MINOR", INT2NUM(VIM_VERSION_MINOR));
1540 rb_define_const(mVIM, "VERSION_BUILD", INT2NUM(VIM_VERSION_BUILD));
1541 rb_define_const(mVIM, "VERSION_PATCHLEVEL", INT2NUM(VIM_VERSION_PATCHLEVEL));
1542 rb_define_const(mVIM, "VERSION_SHORT", rb_str_new2(VIM_VERSION_SHORT));
1543 rb_define_const(mVIM, "VERSION_MEDIUM", rb_str_new2(VIM_VERSION_MEDIUM));
1544 rb_define_const(mVIM, "VERSION_LONG", rb_str_new2(VIM_VERSION_LONG));
1545 rb_define_const(mVIM, "VERSION_LONG_DATE", rb_str_new2(VIM_VERSION_LONG_DATE));
1546 rb_define_module_function(mVIM, "message", vim_message, 1);
1547 rb_define_module_function(mVIM, "set_option", vim_set_option, 1);
1548 rb_define_module_function(mVIM, "command", vim_command, 1);
1549 rb_define_module_function(mVIM, "evaluate", vim_evaluate, 1);
1550
1551 eDeletedBufferError = rb_define_class_under(mVIM, "DeletedBufferError",
1552 rb_eStandardError);
1553 eDeletedWindowError = rb_define_class_under(mVIM, "DeletedWindowError",
1554 rb_eStandardError);
1555
1556 cBuffer = rb_define_class_under(mVIM, "Buffer", rb_cObject);
1557 rb_define_singleton_method(cBuffer, "current", buffer_s_current, 0);
1558 rb_define_singleton_method(cBuffer, "count", buffer_s_count, 0);
1559 rb_define_singleton_method(cBuffer, "[]", buffer_s_aref, 1);
1560 rb_define_method(cBuffer, "name", buffer_name, 0);
1561 rb_define_method(cBuffer, "number", buffer_number, 0);
1562 rb_define_method(cBuffer, "count", buffer_count, 0);
1563 rb_define_method(cBuffer, "length", buffer_count, 0);
1564 rb_define_method(cBuffer, "[]", buffer_aref, 1);
1565 rb_define_method(cBuffer, "[]=", buffer_aset, 2);
1566 rb_define_method(cBuffer, "delete", buffer_delete, 1);
1567 rb_define_method(cBuffer, "append", buffer_append, 2);
1568
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001569 /* Added line manipulation functions
1570 * SegPhault - 03/07/05 */
1571 rb_define_method(cBuffer, "line_number", current_line_number, 0);
1572 rb_define_method(cBuffer, "line", line_s_current, 0);
1573 rb_define_method(cBuffer, "line=", set_current_line, 1);
1574
1575
Bram Moolenaar071d4272004-06-13 20:20:40 +00001576 cVimWindow = rb_define_class_under(mVIM, "Window", rb_cObject);
1577 rb_define_singleton_method(cVimWindow, "current", window_s_current, 0);
1578 rb_define_singleton_method(cVimWindow, "count", window_s_count, 0);
1579 rb_define_singleton_method(cVimWindow, "[]", window_s_aref, 1);
1580 rb_define_method(cVimWindow, "buffer", window_buffer, 0);
1581 rb_define_method(cVimWindow, "height", window_height, 0);
1582 rb_define_method(cVimWindow, "height=", window_set_height, 1);
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001583 rb_define_method(cVimWindow, "width", window_width, 0);
1584 rb_define_method(cVimWindow, "width=", window_set_width, 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001585 rb_define_method(cVimWindow, "cursor", window_cursor, 0);
1586 rb_define_method(cVimWindow, "cursor=", window_set_cursor, 1);
1587
1588 rb_define_virtual_variable("$curbuf", buffer_s_current, 0);
1589 rb_define_virtual_variable("$curwin", window_s_current, 0);
1590}
Bram Moolenaar99685e62013-05-11 13:56:18 +02001591
1592void vim_ruby_init(void *stack_start)
1593{
1594 /* should get machine stack start address early in main function */
1595 ruby_stack_start = stack_start;
1596}