blob: bc6edc7536f0f34a1b859b4e0ef7ec29ab37e1cb [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
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 Moolenaar2016ae52016-06-13 20:08:43 +020034#if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 24
35# define USE_RUBY_INTEGER
Bram Moolenaar06469e92016-06-11 22:26:53 +020036#endif
37
Bram Moolenaarf9b5ef82010-09-29 13:02:53 +020038#ifdef DYNAMIC_RUBY
Bram Moolenaar071d4272004-06-13 20:20:40 +000039/*
40 * This is tricky. In ruby.h there is (inline) function rb_class_of()
41 * definition. This function use these variables. But we want function to
42 * use dll_* variables.
43 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000044# define rb_cFalseClass (*dll_rb_cFalseClass)
45# define rb_cFixnum (*dll_rb_cFixnum)
Bram Moolenaar2016ae52016-06-13 20:08:43 +020046# if defined(USE_RUBY_INTEGER)
47# define rb_cInteger (*dll_rb_cInteger)
Bram Moolenaar06469e92016-06-11 22:26:53 +020048# endif
Bram Moolenaardb3fbe52013-03-07 15:16:21 +010049# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 20
50# define rb_cFloat (*dll_rb_cFloat)
51# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000052# define rb_cNilClass (*dll_rb_cNilClass)
53# define rb_cSymbol (*dll_rb_cSymbol)
54# define rb_cTrueClass (*dll_rb_cTrueClass)
55# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 18
56/*
Bram Moolenaar42d57f02010-03-10 12:47:00 +010057 * On ver 1.8, all Ruby functions are exported with "__declspec(dllimport)"
58 * in ruby.h. But it causes trouble for these variables, because it is
Bram Moolenaar071d4272004-06-13 20:20:40 +000059 * defined in this file. When defined this RUBY_EXPORT it modified to
60 * "extern" and be able to avoid this problem.
61 */
62# define RUBY_EXPORT
63# endif
Bram Moolenaarf9b5ef82010-09-29 13:02:53 +020064
Bram Moolenaar2d73ff42010-10-27 17:40:59 +020065#if !(defined(WIN32) || defined(_WIN64))
Bram Moolenaarf9b5ef82010-09-29 13:02:53 +020066# include <dlfcn.h>
Bram Moolenaar3ca71f12010-10-27 16:49:47 +020067# define HINSTANCE void*
68# define RUBY_PROC void*
Bram Moolenaarf9b5ef82010-09-29 13:02:53 +020069# define load_dll(n) dlopen((n), RTLD_LAZY|RTLD_GLOBAL)
70# define symbol_from_dll dlsym
71# define close_dll dlclose
72#else
Bram Moolenaar3ca71f12010-10-27 16:49:47 +020073# define RUBY_PROC FARPROC
Bram Moolenaarebbcb822010-10-23 14:02:54 +020074# define load_dll vimLoadLib
Bram Moolenaarf9b5ef82010-09-29 13:02:53 +020075# define symbol_from_dll GetProcAddress
76# define close_dll FreeLibrary
Bram Moolenaar071d4272004-06-13 20:20:40 +000077#endif
78
Bram Moolenaarf9b5ef82010-09-29 13:02:53 +020079#endif /* ifdef DYNAMIC_RUBY */
80
Bram Moolenaar0b69c732010-02-17 15:11:50 +010081/* suggested by Ariya Mizutani */
82#if (_MSC_VER == 1200)
83# undef _WIN32_WINNT
84#endif
85
Bram Moolenaar639a2552010-03-17 18:15:23 +010086#if (defined(RUBY_VERSION) && RUBY_VERSION >= 19) \
87 || (defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 19)
88# define RUBY19_OR_LATER 1
89#endif
90
Bram Moolenaar0d27f642015-12-28 22:05:28 +010091#if (defined(RUBY_VERSION) && RUBY_VERSION >= 20) \
92 || (defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 20)
93# define RUBY20_OR_LATER 1
94#endif
95
Bram Moolenaar42d57f02010-03-10 12:47:00 +010096#if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 19
97/* Ruby 1.9 defines a number of static functions which use rb_num2long and
98 * rb_int2big */
99# define rb_num2long rb_num2long_stub
100# define rb_int2big rb_int2big_stub
101#endif
102
Bram Moolenaar498af702014-03-28 21:58:21 +0100103#if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 19 \
104 && VIM_SIZEOF_INT < VIM_SIZEOF_LONG
105/* Ruby 1.9 defines a number of static functions which use rb_fix2int and
Bram Moolenaara2aa31a2014-02-23 22:52:40 +0100106 * rb_num2int if VIM_SIZEOF_INT < VIM_SIZEOF_LONG (64bit) */
Bram Moolenaar0bcdd6e2013-04-14 16:19:03 +0200107# define rb_fix2int rb_fix2int_stub
108# define rb_num2int rb_num2int_stub
109#endif
110
Bram Moolenaar0c7485f2015-01-14 14:04:10 +0100111#if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER == 21
Bram Moolenaara1a118b2014-02-05 22:41:15 +0100112/* Ruby 2.1 adds new GC called RGenGC and RARRAY_PTR uses
113 * rb_gc_writebarrier_unprotect_promoted if USE_RGENGC */
Bram Moolenaar90140742014-11-27 17:44:08 +0100114# define rb_gc_writebarrier_unprotect_promoted rb_gc_writebarrier_unprotect_promoted_stub
115#endif
Bram Moolenaar0c7485f2015-01-14 14:04:10 +0100116#if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 22
117# define rb_gc_writebarrier_unprotect rb_gc_writebarrier_unprotect_stub
Bram Moolenaar0c7485f2015-01-14 14:04:10 +0100118#endif
Bram Moolenaara1a118b2014-02-05 22:41:15 +0100119
Bram Moolenaar071d4272004-06-13 20:20:40 +0000120#include <ruby.h>
Bram Moolenaar639a2552010-03-17 18:15:23 +0100121#ifdef RUBY19_OR_LATER
Bram Moolenaar165641d2010-02-17 16:23:09 +0100122# include <ruby/encoding.h>
123#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000124
Bram Moolenaar7a8ef142010-12-24 13:39:35 +0100125#undef off_t /* ruby defines off_t as _int64, Mingw uses long */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000126#undef EXTERN
127#undef _
128
129/* T_DATA defined both by Ruby and Mac header files, hack around it... */
Bram Moolenaarcb635362007-05-12 13:02:42 +0000130#if defined(MACOS_X_UNIX) || defined(macintosh)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000131# define __OPENTRANSPORT__
132# define __OPENTRANSPORTPROTOCOL__
133# define __OPENTRANSPORTPROVIDERS__
134#endif
135
Bram Moolenaar165641d2010-02-17 16:23:09 +0100136/*
Bram Moolenaar0d27f642015-12-28 22:05:28 +0100137 * The TypedData_XXX macro family can be used since Ruby 1.9.2 but
138 * rb_data_type_t changed in 1.9.3, therefore require at least 2.0.
139 * The old Data_XXX macro family was deprecated on Ruby 2.2.
Bram Moolenaarf2f6d292015-12-28 20:57:10 +0100140 * Use TypedData_XXX if available.
141 */
Bram Moolenaar0d27f642015-12-28 22:05:28 +0100142#if defined(TypedData_Wrap_Struct) && defined(RUBY20_OR_LATER)
Bram Moolenaarf2f6d292015-12-28 20:57:10 +0100143# define USE_TYPEDDATA 1
144#endif
145
146/*
Bram Moolenaar84a05ac2013-05-06 04:24:17 +0200147 * Backward compatibility for Ruby 1.8 and earlier.
Bram Moolenaar165641d2010-02-17 16:23:09 +0100148 * Ruby 1.9 does not provide STR2CSTR, instead StringValuePtr is provided.
149 * Ruby 1.9 does not provide RXXX(s)->len and RXXX(s)->ptr, instead
150 * RXXX_LEN(s) and RXXX_PTR(s) are provided.
151 */
152#ifndef StringValuePtr
153# define StringValuePtr(s) STR2CSTR(s)
154#endif
155#ifndef RARRAY_LEN
156# define RARRAY_LEN(s) RARRAY(s)->len
157#endif
158#ifndef RARRAY_PTR
159# define RARRAY_PTR(s) RARRAY(s)->ptr
160#endif
161#ifndef RSTRING_LEN
162# define RSTRING_LEN(s) RSTRING(s)->len
163#endif
164#ifndef RSTRING_PTR
165# define RSTRING_PTR(s) RSTRING(s)->ptr
166#endif
167
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100168#ifdef HAVE_DUP
169# undef HAVE_DUP
170#endif
171
Bram Moolenaar071d4272004-06-13 20:20:40 +0000172#include "vim.h"
173#include "version.h"
174
175#if defined(PROTO) && !defined(FEAT_RUBY)
176/* Define these to be able to generate the function prototypes. */
177# define VALUE int
178# define RUBY_DATA_FUNC int
179#endif
180
181static int ruby_initialized = 0;
Bram Moolenaar99685e62013-05-11 13:56:18 +0200182static void *ruby_stack_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000183static VALUE objtbl;
184
185static VALUE mVIM;
186static VALUE cBuffer;
187static VALUE cVimWindow;
188static VALUE eDeletedBufferError;
189static VALUE eDeletedWindowError;
190
191static int ensure_ruby_initialized(void);
192static void error_print(int);
193static void ruby_io_init(void);
194static void ruby_vim_init(void);
195
Bram Moolenaar10f3a792013-05-20 12:52:29 +0200196#if defined(RUBY19_OR_LATER) || defined(RUBY_INIT_STACK)
197# if defined(__ia64) && !defined(ruby_init_stack)
198# define ruby_init_stack(addr) ruby_init_stack((addr), rb_ia64_bsp())
199# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000200#endif
201
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200202#if defined(DYNAMIC_RUBY) || defined(PROTO)
Bram Moolenaaref269542016-01-19 13:22:12 +0100203# if defined(PROTO) && !defined(HINSTANCE)
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200204# define HINSTANCE int /* for generating prototypes */
205# endif
206
Bram Moolenaar071d4272004-06-13 20:20:40 +0000207/*
208 * Wrapper defines
209 */
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200210# define rb_assoc_new dll_rb_assoc_new
211# define rb_cObject (*dll_rb_cObject)
Bram Moolenaarf2f6d292015-12-28 20:57:10 +0100212# define rb_check_type dll_rb_check_type
213# ifdef USE_TYPEDDATA
214# define rb_check_typeddata dll_rb_check_typeddata
Bram Moolenaar0c7485f2015-01-14 14:04:10 +0100215# endif
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200216# define rb_class_path dll_rb_class_path
Bram Moolenaarf2f6d292015-12-28 20:57:10 +0100217# ifdef USE_TYPEDDATA
218# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 23
219# define rb_data_typed_object_wrap dll_rb_data_typed_object_wrap
220# else
221# define rb_data_typed_object_alloc dll_rb_data_typed_object_alloc
222# endif
223# else
224# define rb_data_object_alloc dll_rb_data_object_alloc
225# endif
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200226# define rb_define_class_under dll_rb_define_class_under
227# define rb_define_const dll_rb_define_const
228# define rb_define_global_function dll_rb_define_global_function
229# define rb_define_method dll_rb_define_method
230# define rb_define_module dll_rb_define_module
231# define rb_define_module_function dll_rb_define_module_function
232# define rb_define_singleton_method dll_rb_define_singleton_method
233# define rb_define_virtual_variable dll_rb_define_virtual_variable
234# define rb_stdout (*dll_rb_stdout)
235# define rb_eArgError (*dll_rb_eArgError)
236# define rb_eIndexError (*dll_rb_eIndexError)
237# define rb_eRuntimeError (*dll_rb_eRuntimeError)
238# define rb_eStandardError (*dll_rb_eStandardError)
239# define rb_eval_string_protect dll_rb_eval_string_protect
240# define rb_global_variable dll_rb_global_variable
241# define rb_hash_aset dll_rb_hash_aset
242# define rb_hash_new dll_rb_hash_new
243# define rb_inspect dll_rb_inspect
244# define rb_int2inum dll_rb_int2inum
Bram Moolenaara2aa31a2014-02-23 22:52:40 +0100245# if VIM_SIZEOF_INT < VIM_SIZEOF_LONG /* 64 bits only */
Bram Moolenaar498af702014-03-28 21:58:21 +0100246# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER <= 18
247# define rb_fix2int dll_rb_fix2int
248# define rb_num2int dll_rb_num2int
249# endif
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200250# define rb_num2uint dll_rb_num2uint
251# endif
252# define rb_lastline_get dll_rb_lastline_get
253# define rb_lastline_set dll_rb_lastline_set
254# define rb_load_protect dll_rb_load_protect
255# ifndef RUBY19_OR_LATER
256# define rb_num2long dll_rb_num2long
257# endif
258# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER <= 19
259# define rb_num2ulong dll_rb_num2ulong
260# endif
261# define rb_obj_alloc dll_rb_obj_alloc
262# define rb_obj_as_string dll_rb_obj_as_string
263# define rb_obj_id dll_rb_obj_id
264# define rb_raise dll_rb_raise
265# define rb_str_cat dll_rb_str_cat
266# define rb_str_concat dll_rb_str_concat
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100267# undef rb_str_new
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200268# define rb_str_new dll_rb_str_new
269# ifdef rb_str_new2
Bram Moolenaar1d2beae2010-05-22 21:56:55 +0200270/* Ruby may #define rb_str_new2 to use rb_str_new_cstr. */
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200271# define need_rb_str_new_cstr 1
Bram Moolenaar3ca71f12010-10-27 16:49:47 +0200272/* Ruby's headers #define rb_str_new_cstr to make use of GCC's
273 * __builtin_constant_p extension. */
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200274# undef rb_str_new_cstr
275# define rb_str_new_cstr dll_rb_str_new_cstr
Bram Moolenaar76a86062013-05-11 17:45:48 +0200276# else
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200277# define rb_str_new2 dll_rb_str_new2
Bram Moolenaar76a86062013-05-11 17:45:48 +0200278# endif
Bram Moolenaar3ca71f12010-10-27 16:49:47 +0200279# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 18
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200280# define rb_string_value dll_rb_string_value
281# define rb_string_value_ptr dll_rb_string_value_ptr
282# define rb_float_new dll_rb_float_new
283# define rb_ary_new dll_rb_ary_new
284# define rb_ary_push dll_rb_ary_push
Bram Moolenaar10f3a792013-05-20 12:52:29 +0200285# if defined(RUBY19_OR_LATER) || defined(RUBY_INIT_STACK)
286# ifdef __ia64
287# define rb_ia64_bsp dll_rb_ia64_bsp
288# undef ruby_init_stack
289# define ruby_init_stack(addr) dll_ruby_init_stack((addr), rb_ia64_bsp())
290# else
291# define ruby_init_stack dll_ruby_init_stack
292# endif
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200293# endif
294# else
295# define rb_str2cstr dll_rb_str2cstr
Bram Moolenaar3ca71f12010-10-27 16:49:47 +0200296# endif
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200297# ifdef RUBY19_OR_LATER
298# define rb_errinfo dll_rb_errinfo
299# else
300# define ruby_errinfo (*dll_ruby_errinfo)
301# endif
302# define ruby_init dll_ruby_init
303# define ruby_init_loadpath dll_ruby_init_loadpath
304# ifdef WIN3264
305# define NtInitialize dll_NtInitialize
306# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 18
307# define rb_w32_snprintf dll_rb_w32_snprintf
308# endif
309# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000310
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200311# ifdef RUBY19_OR_LATER
312# define ruby_script dll_ruby_script
313# define rb_enc_find_index dll_rb_enc_find_index
314# define rb_enc_find dll_rb_enc_find
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100315# undef rb_enc_str_new
316# define rb_enc_str_new dll_rb_enc_str_new
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200317# define rb_sprintf dll_rb_sprintf
318# define rb_require dll_rb_require
Bram Moolenaar9b1067e2015-11-19 19:33:15 +0100319# define ruby_options dll_ruby_options
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200320# endif
Bram Moolenaar165641d2010-02-17 16:23:09 +0100321
Bram Moolenaar071d4272004-06-13 20:20:40 +0000322/*
323 * Pointers for dynamic link
324 */
325static VALUE (*dll_rb_assoc_new) (VALUE, VALUE);
Bram Moolenaarba52cde2010-06-25 04:29:11 +0200326VALUE *dll_rb_cFalseClass;
327VALUE *dll_rb_cFixnum;
Bram Moolenaar2016ae52016-06-13 20:08:43 +0200328# if defined(USE_RUBY_INTEGER)
Bram Moolenaar06469e92016-06-11 22:26:53 +0200329VALUE *dll_rb_cInteger;
330# endif
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200331# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 20
Bram Moolenaardb3fbe52013-03-07 15:16:21 +0100332VALUE *dll_rb_cFloat;
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200333# endif
Bram Moolenaarba52cde2010-06-25 04:29:11 +0200334VALUE *dll_rb_cNilClass;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000335static VALUE *dll_rb_cObject;
Bram Moolenaarba52cde2010-06-25 04:29:11 +0200336VALUE *dll_rb_cSymbol;
337VALUE *dll_rb_cTrueClass;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000338static void (*dll_rb_check_type) (VALUE,int);
Bram Moolenaarf2f6d292015-12-28 20:57:10 +0100339# ifdef USE_TYPEDDATA
340static void *(*dll_rb_check_typeddata) (VALUE,const rb_data_type_t *);
341# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000342static VALUE (*dll_rb_class_path) (VALUE);
Bram Moolenaarf2f6d292015-12-28 20:57:10 +0100343# ifdef USE_TYPEDDATA
344# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 23
345static VALUE (*dll_rb_data_typed_object_wrap) (VALUE, void*, const rb_data_type_t *);
346# else
347static VALUE (*dll_rb_data_typed_object_alloc) (VALUE, void*, const rb_data_type_t *);
348# endif
349# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000350static VALUE (*dll_rb_data_object_alloc) (VALUE, void*, RUBY_DATA_FUNC, RUBY_DATA_FUNC);
Bram Moolenaarf2f6d292015-12-28 20:57:10 +0100351# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000352static VALUE (*dll_rb_define_class_under) (VALUE, const char*, VALUE);
353static void (*dll_rb_define_const) (VALUE,const char*,VALUE);
354static void (*dll_rb_define_global_function) (const char*,VALUE(*)(),int);
355static void (*dll_rb_define_method) (VALUE,const char*,VALUE(*)(),int);
356static VALUE (*dll_rb_define_module) (const char*);
357static void (*dll_rb_define_module_function) (VALUE,const char*,VALUE(*)(),int);
358static void (*dll_rb_define_singleton_method) (VALUE,const char*,VALUE(*)(),int);
359static void (*dll_rb_define_virtual_variable) (const char*,VALUE(*)(),void(*)());
360static VALUE *dll_rb_stdout;
361static VALUE *dll_rb_eArgError;
362static VALUE *dll_rb_eIndexError;
363static VALUE *dll_rb_eRuntimeError;
364static VALUE *dll_rb_eStandardError;
365static VALUE (*dll_rb_eval_string_protect) (const char*, int*);
366static void (*dll_rb_global_variable) (VALUE*);
367static VALUE (*dll_rb_hash_aset) (VALUE, VALUE, VALUE);
368static VALUE (*dll_rb_hash_new) (void);
369static VALUE (*dll_rb_inspect) (VALUE);
370static VALUE (*dll_rb_int2inum) (long);
Bram Moolenaara2aa31a2014-02-23 22:52:40 +0100371# if VIM_SIZEOF_INT < VIM_SIZEOF_LONG /* 64 bits only */
Bram Moolenaar2623b4f2012-09-18 16:36:32 +0200372static long (*dll_rb_fix2int) (VALUE);
373static long (*dll_rb_num2int) (VALUE);
374static unsigned long (*dll_rb_num2uint) (VALUE);
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200375# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000376static VALUE (*dll_rb_lastline_get) (void);
377static void (*dll_rb_lastline_set) (VALUE);
378static void (*dll_rb_load_protect) (VALUE, int, int*);
379static long (*dll_rb_num2long) (VALUE);
380static unsigned long (*dll_rb_num2ulong) (VALUE);
381static VALUE (*dll_rb_obj_alloc) (VALUE);
382static VALUE (*dll_rb_obj_as_string) (VALUE);
383static VALUE (*dll_rb_obj_id) (VALUE);
384static void (*dll_rb_raise) (VALUE, const char*, ...);
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200385# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 18
Bram Moolenaarf9b5ef82010-09-29 13:02:53 +0200386static VALUE (*dll_rb_string_value) (volatile VALUE*);
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200387# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000388static char *(*dll_rb_str2cstr) (VALUE,int*);
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200389# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000390static VALUE (*dll_rb_str_cat) (VALUE, const char*, long);
391static VALUE (*dll_rb_str_concat) (VALUE, VALUE);
392static VALUE (*dll_rb_str_new) (const char*, long);
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200393# ifdef need_rb_str_new_cstr
Bram Moolenaar1d2beae2010-05-22 21:56:55 +0200394/* Ruby may #define rb_str_new2 to use rb_str_new_cstr. */
395static VALUE (*dll_rb_str_new_cstr) (const char*);
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200396# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000397static VALUE (*dll_rb_str_new2) (const char*);
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200398# endif
399# ifdef RUBY19_OR_LATER
Bram Moolenaar165641d2010-02-17 16:23:09 +0100400static VALUE (*dll_rb_errinfo) (void);
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200401# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000402static VALUE *dll_ruby_errinfo;
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200403# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000404static void (*dll_ruby_init) (void);
405static void (*dll_ruby_init_loadpath) (void);
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200406# ifdef WIN3264
Bram Moolenaar0b69c732010-02-17 15:11:50 +0100407static void (*dll_NtInitialize) (int*, char***);
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200408# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 18
Bram Moolenaar3ca71f12010-10-27 16:49:47 +0200409static int (*dll_rb_w32_snprintf)(char*, size_t, const char*, ...);
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200410# endif
Bram Moolenaar3ca71f12010-10-27 16:49:47 +0200411# endif
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200412# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 18
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100413static char * (*dll_rb_string_value_ptr) (volatile VALUE*);
414static VALUE (*dll_rb_float_new) (double);
415static VALUE (*dll_rb_ary_new) (void);
416static VALUE (*dll_rb_ary_push) (VALUE, VALUE);
Bram Moolenaar10f3a792013-05-20 12:52:29 +0200417# if defined(RUBY19_OR_LATER) || defined(RUBY_INIT_STACK)
418# ifdef __ia64
Bram Moolenaar76a86062013-05-11 17:45:48 +0200419static void * (*dll_rb_ia64_bsp) (void);
420static void (*dll_ruby_init_stack)(VALUE*, void*);
Bram Moolenaar10f3a792013-05-20 12:52:29 +0200421# else
Bram Moolenaar76a86062013-05-11 17:45:48 +0200422static void (*dll_ruby_init_stack)(VALUE*);
Bram Moolenaar10f3a792013-05-20 12:52:29 +0200423# endif
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200424# endif
Bram Moolenaar76a86062013-05-11 17:45:48 +0200425# endif
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200426# ifdef RUBY19_OR_LATER
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100427static VALUE (*dll_rb_int2big)(SIGNED_VALUE);
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200428# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000429
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200430# ifdef RUBY19_OR_LATER
Bram Moolenaar165641d2010-02-17 16:23:09 +0100431static void (*dll_ruby_script) (const char*);
432static int (*dll_rb_enc_find_index) (const char*);
433static rb_encoding* (*dll_rb_enc_find) (const char*);
434static VALUE (*dll_rb_enc_str_new) (const char*, long, rb_encoding*);
435static VALUE (*dll_rb_sprintf) (const char*, ...);
Bram Moolenaar7a8ef142010-12-24 13:39:35 +0100436static VALUE (*dll_rb_require) (const char*);
Bram Moolenaar9b1067e2015-11-19 19:33:15 +0100437static void* (*ruby_options)(int, char**);
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200438# endif
Bram Moolenaar165641d2010-02-17 16:23:09 +0100439
Bram Moolenaara1a118b2014-02-05 22:41:15 +0100440# if defined(USE_RGENGC) && USE_RGENGC
Bram Moolenaar0c7485f2015-01-14 14:04:10 +0100441# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER == 21
Bram Moolenaara1a118b2014-02-05 22:41:15 +0100442static void (*dll_rb_gc_writebarrier_unprotect_promoted)(VALUE);
Bram Moolenaar0c7485f2015-01-14 14:04:10 +0100443# else
444static void (*dll_rb_gc_writebarrier_unprotect)(VALUE obj);
445# endif
Bram Moolenaara1a118b2014-02-05 22:41:15 +0100446# endif
447
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200448# if defined(RUBY19_OR_LATER) && !defined(PROTO)
Bram Moolenaarbbc1a592015-04-21 15:25:31 +0200449# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 22
450long rb_num2long_stub(VALUE x)
451# else
Bram Moolenaarff8cf2b2012-11-24 13:39:00 +0100452SIGNED_VALUE rb_num2long_stub(VALUE x)
Bram Moolenaarbbc1a592015-04-21 15:25:31 +0200453# endif
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100454{
455 return dll_rb_num2long(x);
456}
Bram Moolenaarff8cf2b2012-11-24 13:39:00 +0100457VALUE rb_int2big_stub(SIGNED_VALUE x)
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100458{
459 return dll_rb_int2big(x);
460}
Bram Moolenaar498af702014-03-28 21:58:21 +0100461# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 19 \
462 && VIM_SIZEOF_INT < VIM_SIZEOF_LONG
Bram Moolenaar0bcdd6e2013-04-14 16:19:03 +0200463long rb_fix2int_stub(VALUE x)
464{
465 return dll_rb_fix2int(x);
466}
467long rb_num2int_stub(VALUE x)
468{
469 return dll_rb_num2int(x);
470}
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200471# endif
472# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 20
Bram Moolenaar886ed692013-02-26 13:41:35 +0100473VALUE
474rb_float_new_in_heap(double d)
475{
476 return dll_rb_float_new(d);
477}
Bram Moolenaarbbc1a592015-04-21 15:25:31 +0200478# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 22
479unsigned long rb_num2ulong(VALUE x)
480# else
Bram Moolenaardb3fbe52013-03-07 15:16:21 +0100481VALUE rb_num2ulong(VALUE x)
Bram Moolenaarbbc1a592015-04-21 15:25:31 +0200482# endif
Bram Moolenaar886ed692013-02-26 13:41:35 +0100483{
484 return (long)RSHIFT((SIGNED_VALUE)(x),1);
485}
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200486# endif
487# endif
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100488
Bram Moolenaar3e9a1612014-11-12 16:05:04 +0100489 /* Do not generate a prototype here, VALUE isn't always defined. */
490# if defined(USE_RGENGC) && USE_RGENGC && !defined(PROTO)
Bram Moolenaar0c7485f2015-01-14 14:04:10 +0100491# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER == 21
Bram Moolenaara1a118b2014-02-05 22:41:15 +0100492void rb_gc_writebarrier_unprotect_promoted_stub(VALUE obj)
493{
Bram Moolenaar90140742014-11-27 17:44:08 +0100494 dll_rb_gc_writebarrier_unprotect_promoted(obj);
Bram Moolenaara1a118b2014-02-05 22:41:15 +0100495}
Bram Moolenaar0c7485f2015-01-14 14:04:10 +0100496# else
497void rb_gc_writebarrier_unprotect_stub(VALUE obj)
498{
499 dll_rb_gc_writebarrier_unprotect(obj);
500}
501# endif
502# endif
503
Bram Moolenaar3ca71f12010-10-27 16:49:47 +0200504static HINSTANCE hinstRuby = NULL; /* Instance of ruby.dll */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000505
506/*
Bram Moolenaarda2303d2005-08-30 21:55:26 +0000507 * Table of name to function pointer of ruby.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000508 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000509static struct
510{
511 char *name;
512 RUBY_PROC *ptr;
513} ruby_funcname_table[] =
514{
515 {"rb_assoc_new", (RUBY_PROC*)&dll_rb_assoc_new},
516 {"rb_cFalseClass", (RUBY_PROC*)&dll_rb_cFalseClass},
Bram Moolenaar2016ae52016-06-13 20:08:43 +0200517# if defined(USE_RUBY_INTEGER)
Bram Moolenaar06469e92016-06-11 22:26:53 +0200518 {"rb_cInteger", (RUBY_PROC*)&dll_rb_cInteger},
Bram Moolenaar6abda992017-01-09 21:10:31 +0100519# else
520 {"rb_cFixnum", (RUBY_PROC*)&dll_rb_cFixnum},
Bram Moolenaar06469e92016-06-11 22:26:53 +0200521# endif
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200522# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 20
Bram Moolenaardb3fbe52013-03-07 15:16:21 +0100523 {"rb_cFloat", (RUBY_PROC*)&dll_rb_cFloat},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200524# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000525 {"rb_cNilClass", (RUBY_PROC*)&dll_rb_cNilClass},
526 {"rb_cObject", (RUBY_PROC*)&dll_rb_cObject},
527 {"rb_cSymbol", (RUBY_PROC*)&dll_rb_cSymbol},
528 {"rb_cTrueClass", (RUBY_PROC*)&dll_rb_cTrueClass},
529 {"rb_check_type", (RUBY_PROC*)&dll_rb_check_type},
Bram Moolenaarf2f6d292015-12-28 20:57:10 +0100530# ifdef USE_TYPEDDATA
531 {"rb_check_typeddata", (RUBY_PROC*)&dll_rb_check_typeddata},
532# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000533 {"rb_class_path", (RUBY_PROC*)&dll_rb_class_path},
Bram Moolenaarf2f6d292015-12-28 20:57:10 +0100534# ifdef USE_TYPEDDATA
535# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 23
536 {"rb_data_typed_object_wrap", (RUBY_PROC*)&dll_rb_data_typed_object_wrap},
537# else
538 {"rb_data_typed_object_alloc", (RUBY_PROC*)&dll_rb_data_typed_object_alloc},
539# endif
540# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000541 {"rb_data_object_alloc", (RUBY_PROC*)&dll_rb_data_object_alloc},
Bram Moolenaarf2f6d292015-12-28 20:57:10 +0100542# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000543 {"rb_define_class_under", (RUBY_PROC*)&dll_rb_define_class_under},
544 {"rb_define_const", (RUBY_PROC*)&dll_rb_define_const},
545 {"rb_define_global_function", (RUBY_PROC*)&dll_rb_define_global_function},
546 {"rb_define_method", (RUBY_PROC*)&dll_rb_define_method},
547 {"rb_define_module", (RUBY_PROC*)&dll_rb_define_module},
548 {"rb_define_module_function", (RUBY_PROC*)&dll_rb_define_module_function},
549 {"rb_define_singleton_method", (RUBY_PROC*)&dll_rb_define_singleton_method},
550 {"rb_define_virtual_variable", (RUBY_PROC*)&dll_rb_define_virtual_variable},
551 {"rb_stdout", (RUBY_PROC*)&dll_rb_stdout},
552 {"rb_eArgError", (RUBY_PROC*)&dll_rb_eArgError},
553 {"rb_eIndexError", (RUBY_PROC*)&dll_rb_eIndexError},
554 {"rb_eRuntimeError", (RUBY_PROC*)&dll_rb_eRuntimeError},
555 {"rb_eStandardError", (RUBY_PROC*)&dll_rb_eStandardError},
556 {"rb_eval_string_protect", (RUBY_PROC*)&dll_rb_eval_string_protect},
557 {"rb_global_variable", (RUBY_PROC*)&dll_rb_global_variable},
558 {"rb_hash_aset", (RUBY_PROC*)&dll_rb_hash_aset},
559 {"rb_hash_new", (RUBY_PROC*)&dll_rb_hash_new},
560 {"rb_inspect", (RUBY_PROC*)&dll_rb_inspect},
561 {"rb_int2inum", (RUBY_PROC*)&dll_rb_int2inum},
Bram Moolenaara2aa31a2014-02-23 22:52:40 +0100562# if VIM_SIZEOF_INT < VIM_SIZEOF_LONG /* 64 bits only */
Bram Moolenaar2623b4f2012-09-18 16:36:32 +0200563 {"rb_fix2int", (RUBY_PROC*)&dll_rb_fix2int},
564 {"rb_num2int", (RUBY_PROC*)&dll_rb_num2int},
565 {"rb_num2uint", (RUBY_PROC*)&dll_rb_num2uint},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200566# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000567 {"rb_lastline_get", (RUBY_PROC*)&dll_rb_lastline_get},
568 {"rb_lastline_set", (RUBY_PROC*)&dll_rb_lastline_set},
569 {"rb_load_protect", (RUBY_PROC*)&dll_rb_load_protect},
570 {"rb_num2long", (RUBY_PROC*)&dll_rb_num2long},
571 {"rb_num2ulong", (RUBY_PROC*)&dll_rb_num2ulong},
572 {"rb_obj_alloc", (RUBY_PROC*)&dll_rb_obj_alloc},
573 {"rb_obj_as_string", (RUBY_PROC*)&dll_rb_obj_as_string},
574 {"rb_obj_id", (RUBY_PROC*)&dll_rb_obj_id},
575 {"rb_raise", (RUBY_PROC*)&dll_rb_raise},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200576# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 18
Bram Moolenaarf9b5ef82010-09-29 13:02:53 +0200577 {"rb_string_value", (RUBY_PROC*)&dll_rb_string_value},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200578# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000579 {"rb_str2cstr", (RUBY_PROC*)&dll_rb_str2cstr},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200580# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000581 {"rb_str_cat", (RUBY_PROC*)&dll_rb_str_cat},
582 {"rb_str_concat", (RUBY_PROC*)&dll_rb_str_concat},
583 {"rb_str_new", (RUBY_PROC*)&dll_rb_str_new},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200584# ifdef need_rb_str_new_cstr
Bram Moolenaar1d2beae2010-05-22 21:56:55 +0200585 {"rb_str_new_cstr", (RUBY_PROC*)&dll_rb_str_new_cstr},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200586# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000587 {"rb_str_new2", (RUBY_PROC*)&dll_rb_str_new2},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200588# endif
589# ifdef RUBY19_OR_LATER
Bram Moolenaar165641d2010-02-17 16:23:09 +0100590 {"rb_errinfo", (RUBY_PROC*)&dll_rb_errinfo},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200591# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000592 {"ruby_errinfo", (RUBY_PROC*)&dll_ruby_errinfo},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200593# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000594 {"ruby_init", (RUBY_PROC*)&dll_ruby_init},
595 {"ruby_init_loadpath", (RUBY_PROC*)&dll_ruby_init_loadpath},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200596# ifdef WIN3264
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100597 {
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200598# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER < 19
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100599 "NtInitialize",
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200600# else
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100601 "ruby_sysinit",
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200602# endif
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100603 (RUBY_PROC*)&dll_NtInitialize},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200604# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 18
Bram Moolenaar071d4272004-06-13 20:20:40 +0000605 {"rb_w32_snprintf", (RUBY_PROC*)&dll_rb_w32_snprintf},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200606# endif
Bram Moolenaar3ca71f12010-10-27 16:49:47 +0200607# endif
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200608# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 18
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100609 {"rb_string_value_ptr", (RUBY_PROC*)&dll_rb_string_value_ptr},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200610# if DYNAMIC_RUBY_VER <= 19
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100611 {"rb_float_new", (RUBY_PROC*)&dll_rb_float_new},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200612# else
Bram Moolenaar886ed692013-02-26 13:41:35 +0100613 {"rb_float_new_in_heap", (RUBY_PROC*)&dll_rb_float_new},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200614# endif
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100615 {"rb_ary_new", (RUBY_PROC*)&dll_rb_ary_new},
616 {"rb_ary_push", (RUBY_PROC*)&dll_rb_ary_push},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200617# endif
618# ifdef RUBY19_OR_LATER
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100619 {"rb_int2big", (RUBY_PROC*)&dll_rb_int2big},
Bram Moolenaar165641d2010-02-17 16:23:09 +0100620 {"ruby_script", (RUBY_PROC*)&dll_ruby_script},
621 {"rb_enc_find_index", (RUBY_PROC*)&dll_rb_enc_find_index},
622 {"rb_enc_find", (RUBY_PROC*)&dll_rb_enc_find},
623 {"rb_enc_str_new", (RUBY_PROC*)&dll_rb_enc_str_new},
624 {"rb_sprintf", (RUBY_PROC*)&dll_rb_sprintf},
Bram Moolenaar7a8ef142010-12-24 13:39:35 +0100625 {"rb_require", (RUBY_PROC*)&dll_rb_require},
Bram Moolenaar9b1067e2015-11-19 19:33:15 +0100626 {"ruby_options", (RUBY_PROC*)&dll_ruby_options},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200627# endif
Bram Moolenaar10f3a792013-05-20 12:52:29 +0200628# if defined(RUBY19_OR_LATER) || defined(RUBY_INIT_STACK)
629# ifdef __ia64
630 {"rb_ia64_bsp", (RUBY_PROC*)&dll_rb_ia64_bsp},
631# endif
632 {"ruby_init_stack", (RUBY_PROC*)&dll_ruby_init_stack},
633# endif
Bram Moolenaara1a118b2014-02-05 22:41:15 +0100634# if defined(USE_RGENGC) && USE_RGENGC
Bram Moolenaar0c7485f2015-01-14 14:04:10 +0100635# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER == 21
Bram Moolenaara1a118b2014-02-05 22:41:15 +0100636 {"rb_gc_writebarrier_unprotect_promoted", (RUBY_PROC*)&dll_rb_gc_writebarrier_unprotect_promoted},
Bram Moolenaar0c7485f2015-01-14 14:04:10 +0100637# else
638 {"rb_gc_writebarrier_unprotect", (RUBY_PROC*)&dll_rb_gc_writebarrier_unprotect},
639# endif
Bram Moolenaara1a118b2014-02-05 22:41:15 +0100640# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000641 {"", NULL},
642};
643
644/*
645 * Free ruby.dll
646 */
647 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100648end_dynamic_ruby(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000649{
650 if (hinstRuby)
651 {
Bram Moolenaarf9b5ef82010-09-29 13:02:53 +0200652 close_dll(hinstRuby);
Bram Moolenaar3ca71f12010-10-27 16:49:47 +0200653 hinstRuby = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000654 }
655}
656
657/*
658 * Load library and get all pointers.
659 * Parameter 'libname' provides name of DLL.
660 * Return OK or FAIL.
661 */
662 static int
663ruby_runtime_link_init(char *libname, int verbose)
664{
665 int i;
666
667 if (hinstRuby)
668 return OK;
Bram Moolenaarf9b5ef82010-09-29 13:02:53 +0200669 hinstRuby = load_dll(libname);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000670 if (!hinstRuby)
671 {
672 if (verbose)
673 EMSG2(_(e_loadlib), libname);
674 return FAIL;
675 }
676
677 for (i = 0; ruby_funcname_table[i].ptr; ++i)
678 {
Bram Moolenaarf9b5ef82010-09-29 13:02:53 +0200679 if (!(*ruby_funcname_table[i].ptr = symbol_from_dll(hinstRuby,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000680 ruby_funcname_table[i].name)))
681 {
Bram Moolenaarf9b5ef82010-09-29 13:02:53 +0200682 close_dll(hinstRuby);
Bram Moolenaar3ca71f12010-10-27 16:49:47 +0200683 hinstRuby = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000684 if (verbose)
685 EMSG2(_(e_loadfunc), ruby_funcname_table[i].name);
686 return FAIL;
687 }
688 }
689 return OK;
690}
691
692/*
693 * If ruby is enabled (there is installed ruby on Windows system) return TRUE,
694 * else FALSE.
695 */
696 int
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100697ruby_enabled(int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000698{
Bram Moolenaar25e4fcd2016-01-09 14:57:47 +0100699 return ruby_runtime_link_init((char *)p_rubydll, verbose) == OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000700}
701#endif /* defined(DYNAMIC_RUBY) || defined(PROTO) */
702
703 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100704ruby_end(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000705{
706#ifdef DYNAMIC_RUBY
707 end_dynamic_ruby();
708#endif
709}
710
711void ex_ruby(exarg_T *eap)
712{
713 int state;
714 char *script = NULL;
715
Bram Moolenaar35a2e192006-03-13 22:07:11 +0000716 script = (char *)script_get(eap, eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000717 if (!eap->skip && ensure_ruby_initialized())
718 {
719 if (script == NULL)
720 rb_eval_string_protect((char *)eap->arg, &state);
721 else
722 rb_eval_string_protect(script, &state);
723 if (state)
724 error_print(state);
725 }
726 vim_free(script);
727}
728
Bram Moolenaar165641d2010-02-17 16:23:09 +0100729/*
730 * In Ruby 1.9 or later, ruby String object has encoding.
731 * conversion buffer string of vim to ruby String object using
732 * VIM encoding option.
733 */
734 static VALUE
735vim_str2rb_enc_str(const char *s)
736{
Bram Moolenaar639a2552010-03-17 18:15:23 +0100737#ifdef RUBY19_OR_LATER
Bram Moolenaar165641d2010-02-17 16:23:09 +0100738 int isnum;
739 long lval;
740 char_u *sval;
741 rb_encoding *enc;
742
743 isnum = get_option_value((char_u *)"enc", &lval, &sval, 0);
744 if (isnum == 0)
745 {
746 enc = rb_enc_find((char *)sval);
747 vim_free(sval);
Bram Moolenaar758535a2016-03-30 22:06:16 +0200748 if (enc)
749 {
Bram Moolenaar9b0ac222016-06-01 20:31:43 +0200750 return rb_enc_str_new(s, (long)strlen(s), enc);
Bram Moolenaar165641d2010-02-17 16:23:09 +0100751 }
752 }
753#endif
754 return rb_str_new2(s);
755}
756
757 static VALUE
758eval_enc_string_protect(const char *str, int *state)
759{
Bram Moolenaar639a2552010-03-17 18:15:23 +0100760#ifdef RUBY19_OR_LATER
Bram Moolenaar165641d2010-02-17 16:23:09 +0100761 int isnum;
762 long lval;
763 char_u *sval;
764 rb_encoding *enc;
765 VALUE v;
766
767 isnum = get_option_value((char_u *)"enc", &lval, &sval, 0);
768 if (isnum == 0)
769 {
770 enc = rb_enc_find((char *)sval);
771 vim_free(sval);
772 if (enc)
773 {
774 v = rb_sprintf("#-*- coding:%s -*-\n%s", rb_enc_name(enc), str);
775 return rb_eval_string_protect(StringValuePtr(v), state);
776 }
777 }
778#endif
779 return rb_eval_string_protect(str, state);
780}
781
Bram Moolenaar071d4272004-06-13 20:20:40 +0000782void ex_rubydo(exarg_T *eap)
783{
784 int state;
785 linenr_T i;
786
787 if (ensure_ruby_initialized())
788 {
789 if (u_save(eap->line1 - 1, eap->line2 + 1) != OK)
790 return;
Bram Moolenaar758535a2016-03-30 22:06:16 +0200791 for (i = eap->line1; i <= eap->line2; i++)
792 {
Bram Moolenaare980d8a2010-12-08 13:11:21 +0100793 VALUE line;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000794
Bram Moolenaare980d8a2010-12-08 13:11:21 +0100795 line = vim_str2rb_enc_str((char *)ml_get(i));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000796 rb_lastline_set(line);
Bram Moolenaar165641d2010-02-17 16:23:09 +0100797 eval_enc_string_protect((char *) eap->arg, &state);
Bram Moolenaar758535a2016-03-30 22:06:16 +0200798 if (state)
799 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000800 error_print(state);
801 break;
802 }
803 line = rb_lastline_get();
Bram Moolenaar758535a2016-03-30 22:06:16 +0200804 if (!NIL_P(line))
805 {
806 if (TYPE(line) != T_STRING)
807 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000808 EMSG(_("E265: $_ must be an instance of String"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000809 return;
810 }
Bram Moolenaar165641d2010-02-17 16:23:09 +0100811 ml_replace(i, (char_u *) StringValuePtr(line), 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000812 changed();
813#ifdef SYNTAX_HL
814 syn_changed(i); /* recompute syntax hl. for this line */
815#endif
816 }
817 }
818 check_cursor();
819 update_curbuf(NOT_VALID);
820 }
821}
822
823void ex_rubyfile(exarg_T *eap)
824{
825 int state;
826
827 if (ensure_ruby_initialized())
828 {
829 rb_load_protect(rb_str_new2((char *) eap->arg), 0, &state);
830 if (state) error_print(state);
831 }
832}
833
834void ruby_buffer_free(buf_T *buf)
835{
Bram Moolenaare344bea2005-09-01 20:46:49 +0000836 if (buf->b_ruby_ref)
837 {
838 rb_hash_aset(objtbl, rb_obj_id((VALUE) buf->b_ruby_ref), Qnil);
839 RDATA(buf->b_ruby_ref)->data = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000840 }
841}
842
843void ruby_window_free(win_T *win)
844{
Bram Moolenaare344bea2005-09-01 20:46:49 +0000845 if (win->w_ruby_ref)
846 {
847 rb_hash_aset(objtbl, rb_obj_id((VALUE) win->w_ruby_ref), Qnil);
848 RDATA(win->w_ruby_ref)->data = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000849 }
850}
851
852static int ensure_ruby_initialized(void)
853{
854 if (!ruby_initialized)
855 {
856#ifdef DYNAMIC_RUBY
857 if (ruby_enabled(TRUE))
858 {
859#endif
Bram Moolenaar0b69c732010-02-17 15:11:50 +0100860#ifdef _WIN32
861 /* suggested by Ariya Mizutani */
862 int argc = 1;
863 char *argv[] = {"gvim.exe"};
Bram Moolenaar90140742014-11-27 17:44:08 +0100864 char **argvp = argv;
865 NtInitialize(&argc, &argvp);
Bram Moolenaar0b69c732010-02-17 15:11:50 +0100866#endif
Bram Moolenaarb2c03502010-07-02 20:20:09 +0200867 {
Bram Moolenaar10f3a792013-05-20 12:52:29 +0200868#if defined(RUBY19_OR_LATER) || defined(RUBY_INIT_STACK)
Bram Moolenaar99685e62013-05-11 13:56:18 +0200869 ruby_init_stack(ruby_stack_start);
Bram Moolenaar165641d2010-02-17 16:23:09 +0100870#endif
Bram Moolenaarb2c03502010-07-02 20:20:09 +0200871 ruby_init();
872 }
Bram Moolenaar639a2552010-03-17 18:15:23 +0100873#ifdef RUBY19_OR_LATER
Bram Moolenaar7a8ef142010-12-24 13:39:35 +0100874 {
875 int dummy_argc = 2;
876 char *dummy_argv[] = {"vim-ruby", "-e0"};
Bram Moolenaar9b1067e2015-11-19 19:33:15 +0100877 ruby_options(dummy_argc, dummy_argv);
Bram Moolenaar7a8ef142010-12-24 13:39:35 +0100878 }
Bram Moolenaar165641d2010-02-17 16:23:09 +0100879 ruby_script("vim-ruby");
Bram Moolenaar7a8ef142010-12-24 13:39:35 +0100880#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000881 ruby_init_loadpath();
Bram Moolenaar165641d2010-02-17 16:23:09 +0100882#endif
Bram Moolenaar7a8ef142010-12-24 13:39:35 +0100883 ruby_io_init();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000884 ruby_vim_init();
885 ruby_initialized = 1;
886#ifdef DYNAMIC_RUBY
887 }
888 else
889 {
890 EMSG(_("E266: Sorry, this command is disabled, the Ruby library could not be loaded."));
891 return 0;
892 }
893#endif
894 }
895 return ruby_initialized;
896}
897
898static void error_print(int state)
899{
900#ifndef DYNAMIC_RUBY
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100901#if !(defined(RUBY_VERSION) && RUBY_VERSION >= 19) \
902 && !(defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 19)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000903 RUBYEXTERN VALUE ruby_errinfo;
904#endif
Bram Moolenaar165641d2010-02-17 16:23:09 +0100905#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000906 VALUE eclass;
907 VALUE einfo;
908 char buff[BUFSIZ];
909
910#define TAG_RETURN 0x1
911#define TAG_BREAK 0x2
912#define TAG_NEXT 0x3
913#define TAG_RETRY 0x4
914#define TAG_REDO 0x5
915#define TAG_RAISE 0x6
916#define TAG_THROW 0x7
917#define TAG_FATAL 0x8
918#define TAG_MASK 0xf
919
Bram Moolenaar758535a2016-03-30 22:06:16 +0200920 switch (state)
921 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000922 case TAG_RETURN:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000923 EMSG(_("E267: unexpected return"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000924 break;
925 case TAG_NEXT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000926 EMSG(_("E268: unexpected next"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000927 break;
928 case TAG_BREAK:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000929 EMSG(_("E269: unexpected break"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000930 break;
931 case TAG_REDO:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000932 EMSG(_("E270: unexpected redo"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000933 break;
934 case TAG_RETRY:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000935 EMSG(_("E271: retry outside of rescue clause"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000936 break;
937 case TAG_RAISE:
938 case TAG_FATAL:
Bram Moolenaar639a2552010-03-17 18:15:23 +0100939#ifdef RUBY19_OR_LATER
Bram Moolenaar165641d2010-02-17 16:23:09 +0100940 eclass = CLASS_OF(rb_errinfo());
941 einfo = rb_obj_as_string(rb_errinfo());
942#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000943 eclass = CLASS_OF(ruby_errinfo);
944 einfo = rb_obj_as_string(ruby_errinfo);
Bram Moolenaar165641d2010-02-17 16:23:09 +0100945#endif
Bram Moolenaar758535a2016-03-30 22:06:16 +0200946 if (eclass == rb_eRuntimeError && RSTRING_LEN(einfo) == 0)
947 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000948 EMSG(_("E272: unhandled exception"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000949 }
Bram Moolenaar758535a2016-03-30 22:06:16 +0200950 else
951 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000952 VALUE epath;
953 char *p;
954
955 epath = rb_class_path(eclass);
Bram Moolenaar9c13b352005-05-19 20:53:52 +0000956 vim_snprintf(buff, BUFSIZ, "%s: %s",
Bram Moolenaar165641d2010-02-17 16:23:09 +0100957 RSTRING_PTR(epath), RSTRING_PTR(einfo));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000958 p = strchr(buff, '\n');
959 if (p) *p = '\0';
960 EMSG(buff);
961 }
962 break;
963 default:
Bram Moolenaar9c13b352005-05-19 20:53:52 +0000964 vim_snprintf(buff, BUFSIZ, _("E273: unknown longjmp status %d"), state);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000965 EMSG(buff);
966 break;
967 }
968}
969
Bram Moolenaarcd8b20a2009-05-22 16:20:57 +0000970static VALUE vim_message(VALUE self UNUSED, VALUE str)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000971{
972 char *buff, *p;
973
974 str = rb_obj_as_string(str);
Bram Moolenaar3f5f7952011-08-04 19:34:59 +0200975 if (RSTRING_LEN(str) > 0)
976 {
977 /* Only do this when the string isn't empty, alloc(0) causes trouble. */
978 buff = ALLOCA_N(char, RSTRING_LEN(str));
979 strcpy(buff, RSTRING_PTR(str));
980 p = strchr(buff, '\n');
981 if (p) *p = '\0';
982 MSG(buff);
983 }
984 else
985 {
986 MSG("");
987 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000988 return Qnil;
989}
990
Bram Moolenaarcd8b20a2009-05-22 16:20:57 +0000991static VALUE vim_set_option(VALUE self UNUSED, VALUE str)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000992{
Bram Moolenaar165641d2010-02-17 16:23:09 +0100993 do_set((char_u *)StringValuePtr(str), 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000994 update_screen(NOT_VALID);
995 return Qnil;
996}
997
Bram Moolenaarcd8b20a2009-05-22 16:20:57 +0000998static VALUE vim_command(VALUE self UNUSED, VALUE str)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000999{
Bram Moolenaar165641d2010-02-17 16:23:09 +01001000 do_cmdline_cmd((char_u *)StringValuePtr(str));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001001 return Qnil;
1002}
1003
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001004#ifdef FEAT_EVAL
1005static VALUE vim_to_ruby(typval_T *tv)
1006{
1007 VALUE result = Qnil;
1008
1009 if (tv->v_type == VAR_STRING)
1010 {
Bram Moolenaar94127e42010-03-19 23:08:48 +01001011 result = rb_str_new2(tv->vval.v_string == NULL
1012 ? "" : (char *)(tv->vval.v_string));
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001013 }
1014 else if (tv->v_type == VAR_NUMBER)
1015 {
Bram Moolenaar639a2552010-03-17 18:15:23 +01001016 result = INT2NUM(tv->vval.v_number);
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001017 }
1018# ifdef FEAT_FLOAT
1019 else if (tv->v_type == VAR_FLOAT)
1020 {
Bram Moolenaar639a2552010-03-17 18:15:23 +01001021 result = rb_float_new(tv->vval.v_float);
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001022 }
1023# endif
1024 else if (tv->v_type == VAR_LIST)
1025 {
Bram Moolenaar639a2552010-03-17 18:15:23 +01001026 list_T *list = tv->vval.v_list;
1027 listitem_T *curr;
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001028
Bram Moolenaar639a2552010-03-17 18:15:23 +01001029 result = rb_ary_new();
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001030
Bram Moolenaar639a2552010-03-17 18:15:23 +01001031 if (list != NULL)
1032 {
1033 for (curr = list->lv_first; curr != NULL; curr = curr->li_next)
1034 {
1035 rb_ary_push(result, vim_to_ruby(&curr->li_tv));
1036 }
1037 }
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001038 }
1039 else if (tv->v_type == VAR_DICT)
1040 {
Bram Moolenaar639a2552010-03-17 18:15:23 +01001041 result = rb_hash_new();
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001042
Bram Moolenaar639a2552010-03-17 18:15:23 +01001043 if (tv->vval.v_dict != NULL)
1044 {
1045 hashtab_T *ht = &tv->vval.v_dict->dv_hashtab;
1046 long_u todo = ht->ht_used;
1047 hashitem_T *hi;
1048 dictitem_T *di;
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001049
Bram Moolenaar639a2552010-03-17 18:15:23 +01001050 for (hi = ht->ht_array; todo > 0; ++hi)
1051 {
1052 if (!HASHITEM_EMPTY(hi))
1053 {
1054 --todo;
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001055
Bram Moolenaar639a2552010-03-17 18:15:23 +01001056 di = dict_lookup(hi);
1057 rb_hash_aset(result, rb_str_new2((char *)hi->hi_key),
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001058 vim_to_ruby(&di->di_tv));
Bram Moolenaar639a2552010-03-17 18:15:23 +01001059 }
1060 }
1061 }
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001062 }
1063 else if (tv->v_type == VAR_SPECIAL)
1064 {
1065 if (tv->vval.v_number <= VVAL_TRUE)
1066 result = INT2NUM(tv->vval.v_number);
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001067 } /* else return Qnil; */
1068
1069 return result;
1070}
1071#endif
1072
Bram Moolenaarcd8b20a2009-05-22 16:20:57 +00001073static VALUE vim_evaluate(VALUE self UNUSED, VALUE str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001074{
1075#ifdef FEAT_EVAL
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001076 typval_T *tv;
1077 VALUE result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001078
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001079 tv = eval_expr((char_u *)StringValuePtr(str), NULL);
1080 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001081 {
Bram Moolenaar639a2552010-03-17 18:15:23 +01001082 return Qnil;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001083 }
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001084 result = vim_to_ruby(tv);
1085
1086 free_tv(tv);
1087
1088 return result;
1089#else
1090 return Qnil;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001091#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001092}
1093
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001094#ifdef USE_TYPEDDATA
1095static size_t buffer_dsize(const void *buf);
1096
1097static const rb_data_type_t buffer_type = {
1098 "vim_buffer",
1099 {0, 0, buffer_dsize, {0, 0}},
1100 0, 0,
1101# ifdef RUBY_TYPED_FREE_IMMEDIATELY
1102 0,
1103# endif
1104};
1105
1106static size_t buffer_dsize(const void *buf UNUSED)
1107{
1108 return sizeof(buf_T);
1109}
1110#endif
1111
Bram Moolenaar071d4272004-06-13 20:20:40 +00001112static VALUE buffer_new(buf_T *buf)
1113{
Bram Moolenaare344bea2005-09-01 20:46:49 +00001114 if (buf->b_ruby_ref)
1115 {
1116 return (VALUE) buf->b_ruby_ref;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001117 }
Bram Moolenaare344bea2005-09-01 20:46:49 +00001118 else
1119 {
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001120#ifdef USE_TYPEDDATA
1121 VALUE obj = TypedData_Wrap_Struct(cBuffer, &buffer_type, buf);
1122#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001123 VALUE obj = Data_Wrap_Struct(cBuffer, 0, 0, buf);
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001124#endif
Bram Moolenaare344bea2005-09-01 20:46:49 +00001125 buf->b_ruby_ref = (void *) obj;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001126 rb_hash_aset(objtbl, rb_obj_id(obj), obj);
1127 return obj;
1128 }
1129}
1130
1131static buf_T *get_buf(VALUE obj)
1132{
1133 buf_T *buf;
1134
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001135#ifdef USE_TYPEDDATA
1136 TypedData_Get_Struct(obj, buf_T, &buffer_type, buf);
1137#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001138 Data_Get_Struct(obj, buf_T, buf);
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001139#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001140 if (buf == NULL)
1141 rb_raise(eDeletedBufferError, "attempt to refer to deleted buffer");
1142 return buf;
1143}
1144
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001145static VALUE buffer_s_current(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001146{
1147 return buffer_new(curbuf);
1148}
1149
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001150static VALUE buffer_s_count(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001151{
1152 buf_T *b;
1153 int n = 0;
1154
Bram Moolenaar29323592016-07-24 22:04:11 +02001155 FOR_ALL_BUFFERS(b)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001156 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001157 /* Deleted buffers should not be counted
1158 * SegPhault - 01/07/05 */
1159 if (b->b_p_bl)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001160 n++;
1161 }
1162
Bram Moolenaar071d4272004-06-13 20:20:40 +00001163 return INT2NUM(n);
1164}
1165
Bram Moolenaarcd8b20a2009-05-22 16:20:57 +00001166static VALUE buffer_s_aref(VALUE self UNUSED, VALUE num)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001167{
1168 buf_T *b;
1169 int n = NUM2INT(num);
1170
Bram Moolenaar29323592016-07-24 22:04:11 +02001171 FOR_ALL_BUFFERS(b)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001172 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001173 /* Deleted buffers should not be counted
1174 * SegPhault - 01/07/05 */
1175 if (!b->b_p_bl)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001176 continue;
1177
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001178 if (n == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001179 return buffer_new(b);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001180
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001181 n--;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001182 }
1183 return Qnil;
1184}
1185
1186static VALUE buffer_name(VALUE self)
1187{
1188 buf_T *buf = get_buf(self);
1189
Bram Moolenaar35a2e192006-03-13 22:07:11 +00001190 return buf->b_ffname ? rb_str_new2((char *)buf->b_ffname) : Qnil;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001191}
1192
1193static VALUE buffer_number(VALUE self)
1194{
1195 buf_T *buf = get_buf(self);
1196
1197 return INT2NUM(buf->b_fnum);
1198}
1199
1200static VALUE buffer_count(VALUE self)
1201{
1202 buf_T *buf = get_buf(self);
1203
1204 return INT2NUM(buf->b_ml.ml_line_count);
1205}
1206
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001207static VALUE get_buffer_line(buf_T *buf, linenr_T n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001208{
Bram Moolenaar3c531602010-11-16 14:46:19 +01001209 if (n <= 0 || n > buf->b_ml.ml_line_count)
1210 rb_raise(rb_eIndexError, "line number %ld out of range", (long)n);
1211 return vim_str2rb_enc_str((char *)ml_get_buf(buf, n, FALSE));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001212}
1213
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001214static VALUE buffer_aref(VALUE self, VALUE num)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001215{
1216 buf_T *buf = get_buf(self);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001217
1218 if (buf != NULL)
1219 return get_buffer_line(buf, (linenr_T)NUM2LONG(num));
1220 return Qnil; /* For stop warning */
1221}
1222
1223static VALUE set_buffer_line(buf_T *buf, linenr_T n, VALUE str)
1224{
Bram Moolenaar165641d2010-02-17 16:23:09 +01001225 char *line = StringValuePtr(str);
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001226 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001227
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001228 if (n > 0 && n <= buf->b_ml.ml_line_count && line != NULL)
1229 {
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001230 /* set curwin/curbuf for "buf" and save some things */
1231 aucmd_prepbuf(&aco, buf);
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001232
Bram Moolenaar758535a2016-03-30 22:06:16 +02001233 if (u_savesub(n) == OK)
1234 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001235 ml_replace(n, (char_u *)line, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001236 changed();
1237#ifdef SYNTAX_HL
1238 syn_changed(n); /* recompute syntax hl. for this line */
1239#endif
1240 }
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001241
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001242 /* restore curwin/curbuf and a few other things */
1243 aucmd_restbuf(&aco);
1244 /* Careful: autocommands may have made "buf" invalid! */
Bram Moolenaarf30e74c2006-08-16 17:35:00 +00001245
Bram Moolenaar071d4272004-06-13 20:20:40 +00001246 update_curbuf(NOT_VALID);
1247 }
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001248 else
1249 {
Bram Moolenaar165641d2010-02-17 16:23:09 +01001250 rb_raise(rb_eIndexError, "line number %ld out of range", (long)n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001251 }
1252 return str;
1253}
1254
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001255static VALUE buffer_aset(VALUE self, VALUE num, VALUE str)
1256{
1257 buf_T *buf = get_buf(self);
1258
1259 if (buf != NULL)
1260 return set_buffer_line(buf, (linenr_T)NUM2LONG(num), str);
1261 return str;
1262}
1263
Bram Moolenaar071d4272004-06-13 20:20:40 +00001264static VALUE buffer_delete(VALUE self, VALUE num)
1265{
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001266 buf_T *buf = get_buf(self);
1267 long n = NUM2LONG(num);
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001268 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001269
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001270 if (n > 0 && n <= buf->b_ml.ml_line_count)
1271 {
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001272 /* set curwin/curbuf for "buf" and save some things */
1273 aucmd_prepbuf(&aco, buf);
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001274
Bram Moolenaar758535a2016-03-30 22:06:16 +02001275 if (u_savedel(n, 1) == OK)
1276 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001277 ml_delete(n, 0);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001278
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001279 /* Changes to non-active buffers should properly refresh
1280 * SegPhault - 01/09/05 */
1281 deleted_lines_mark(n, 1L);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001282
Bram Moolenaar071d4272004-06-13 20:20:40 +00001283 changed();
1284 }
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001285
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001286 /* restore curwin/curbuf and a few other things */
1287 aucmd_restbuf(&aco);
1288 /* Careful: autocommands may have made "buf" invalid! */
Bram Moolenaarf30e74c2006-08-16 17:35:00 +00001289
Bram Moolenaar071d4272004-06-13 20:20:40 +00001290 update_curbuf(NOT_VALID);
1291 }
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001292 else
1293 {
Bram Moolenaar165641d2010-02-17 16:23:09 +01001294 rb_raise(rb_eIndexError, "line number %ld out of range", n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001295 }
1296 return Qnil;
1297}
1298
1299static VALUE buffer_append(VALUE self, VALUE num, VALUE str)
1300{
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001301 buf_T *buf = get_buf(self);
Bram Moolenaar165641d2010-02-17 16:23:09 +01001302 char *line = StringValuePtr(str);
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001303 long n = NUM2LONG(num);
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001304 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001305
Bram Moolenaar3c531602010-11-16 14:46:19 +01001306 if (line == NULL)
1307 {
Bram Moolenaar165641d2010-02-17 16:23:09 +01001308 rb_raise(rb_eIndexError, "NULL line");
1309 }
1310 else if (n >= 0 && n <= buf->b_ml.ml_line_count)
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001311 {
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001312 /* set curwin/curbuf for "buf" and save some things */
1313 aucmd_prepbuf(&aco, buf);
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001314
Bram Moolenaar758535a2016-03-30 22:06:16 +02001315 if (u_inssub(n + 1) == OK)
1316 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001317 ml_append(n, (char_u *) line, (colnr_T) 0, FALSE);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001318
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001319 /* Changes to non-active buffers should properly refresh screen
1320 * SegPhault - 12/20/04 */
1321 appended_lines_mark(n, 1L);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001322
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001323 changed();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001324 }
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001325
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001326 /* restore curwin/curbuf and a few other things */
1327 aucmd_restbuf(&aco);
1328 /* Careful: autocommands may have made "buf" invalid! */
Bram Moolenaarf30e74c2006-08-16 17:35:00 +00001329
Bram Moolenaar071d4272004-06-13 20:20:40 +00001330 update_curbuf(NOT_VALID);
1331 }
Bram Moolenaar3c531602010-11-16 14:46:19 +01001332 else
1333 {
Bram Moolenaar165641d2010-02-17 16:23:09 +01001334 rb_raise(rb_eIndexError, "line number %ld out of range", n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001335 }
1336 return str;
1337}
1338
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001339#ifdef USE_TYPEDDATA
1340static size_t window_dsize(const void *buf);
1341
1342static const rb_data_type_t window_type = {
1343 "vim_window",
1344 {0, 0, window_dsize, {0, 0}},
1345 0, 0,
1346# ifdef RUBY_TYPED_FREE_IMMEDIATELY
1347 0,
1348# endif
1349};
1350
1351static size_t window_dsize(const void *win UNUSED)
1352{
1353 return sizeof(win_T);
1354}
1355#endif
1356
Bram Moolenaar071d4272004-06-13 20:20:40 +00001357static VALUE window_new(win_T *win)
1358{
Bram Moolenaare344bea2005-09-01 20:46:49 +00001359 if (win->w_ruby_ref)
1360 {
1361 return (VALUE) win->w_ruby_ref;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001362 }
Bram Moolenaare344bea2005-09-01 20:46:49 +00001363 else
1364 {
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001365#ifdef USE_TYPEDDATA
1366 VALUE obj = TypedData_Wrap_Struct(cVimWindow, &window_type, win);
1367#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001368 VALUE obj = Data_Wrap_Struct(cVimWindow, 0, 0, win);
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001369#endif
Bram Moolenaare344bea2005-09-01 20:46:49 +00001370 win->w_ruby_ref = (void *) obj;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001371 rb_hash_aset(objtbl, rb_obj_id(obj), obj);
1372 return obj;
1373 }
1374}
1375
1376static win_T *get_win(VALUE obj)
1377{
1378 win_T *win;
1379
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001380#ifdef USE_TYPEDDATA
1381 TypedData_Get_Struct(obj, win_T, &window_type, win);
1382#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001383 Data_Get_Struct(obj, win_T, win);
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001384#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001385 if (win == NULL)
1386 rb_raise(eDeletedWindowError, "attempt to refer to deleted window");
1387 return win;
1388}
1389
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001390static VALUE window_s_current(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001391{
1392 return window_new(curwin);
1393}
1394
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001395/*
1396 * Added line manipulation functions
1397 * SegPhault - 03/07/05
1398 */
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001399static VALUE line_s_current(void)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001400{
1401 return get_buffer_line(curbuf, curwin->w_cursor.lnum);
1402}
1403
Bram Moolenaarcd8b20a2009-05-22 16:20:57 +00001404static VALUE set_current_line(VALUE self UNUSED, VALUE str)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001405{
1406 return set_buffer_line(curbuf, curwin->w_cursor.lnum, str);
1407}
1408
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001409static VALUE current_line_number(void)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001410{
1411 return INT2FIX((int)curwin->w_cursor.lnum);
1412}
1413
1414
1415
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001416static VALUE window_s_count(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001417{
1418#ifdef FEAT_WINDOWS
1419 win_T *w;
1420 int n = 0;
1421
Bram Moolenaar29323592016-07-24 22:04:11 +02001422 FOR_ALL_WINDOWS(w)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001423 n++;
1424 return INT2NUM(n);
1425#else
1426 return INT2NUM(1);
1427#endif
1428}
1429
Bram Moolenaarcd8b20a2009-05-22 16:20:57 +00001430static VALUE window_s_aref(VALUE self UNUSED, VALUE num)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001431{
1432 win_T *w;
1433 int n = NUM2INT(num);
1434
1435#ifndef FEAT_WINDOWS
1436 w = curwin;
1437#else
1438 for (w = firstwin; w != NULL; w = w->w_next, --n)
1439#endif
1440 if (n == 0)
1441 return window_new(w);
1442 return Qnil;
1443}
1444
1445static VALUE window_buffer(VALUE self)
1446{
1447 win_T *win = get_win(self);
1448
1449 return buffer_new(win->w_buffer);
1450}
1451
1452static VALUE window_height(VALUE self)
1453{
1454 win_T *win = get_win(self);
1455
1456 return INT2NUM(win->w_height);
1457}
1458
1459static VALUE window_set_height(VALUE self, VALUE height)
1460{
1461 win_T *win = get_win(self);
1462 win_T *savewin = curwin;
1463
1464 curwin = win;
1465 win_setheight(NUM2INT(height));
1466 curwin = savewin;
1467 return height;
1468}
1469
Bram Moolenaarfeeaa682013-02-14 22:19:51 +01001470static VALUE window_width(VALUE self UNUSED)
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001471{
Bram Moolenaarfeeaa682013-02-14 22:19:51 +01001472 return INT2NUM(W_WIDTH(get_win(self)));
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001473}
1474
Bram Moolenaarfeeaa682013-02-14 22:19:51 +01001475static VALUE window_set_width(VALUE self UNUSED, VALUE width)
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001476{
Bram Moolenaar44a2f922016-03-19 22:11:51 +01001477#ifdef FEAT_WINDOWS
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001478 win_T *win = get_win(self);
1479 win_T *savewin = curwin;
1480
1481 curwin = win;
1482 win_setwidth(NUM2INT(width));
1483 curwin = savewin;
Bram Moolenaarfeeaa682013-02-14 22:19:51 +01001484#endif
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001485 return width;
1486}
1487
Bram Moolenaar071d4272004-06-13 20:20:40 +00001488static VALUE window_cursor(VALUE self)
1489{
1490 win_T *win = get_win(self);
1491
1492 return rb_assoc_new(INT2NUM(win->w_cursor.lnum), INT2NUM(win->w_cursor.col));
1493}
1494
1495static VALUE window_set_cursor(VALUE self, VALUE pos)
1496{
1497 VALUE lnum, col;
1498 win_T *win = get_win(self);
1499
1500 Check_Type(pos, T_ARRAY);
Bram Moolenaar165641d2010-02-17 16:23:09 +01001501 if (RARRAY_LEN(pos) != 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001502 rb_raise(rb_eArgError, "array length must be 2");
Bram Moolenaar165641d2010-02-17 16:23:09 +01001503 lnum = RARRAY_PTR(pos)[0];
1504 col = RARRAY_PTR(pos)[1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001505 win->w_cursor.lnum = NUM2LONG(lnum);
1506 win->w_cursor.col = NUM2UINT(col);
1507 check_cursor(); /* put cursor on an existing line */
1508 update_screen(NOT_VALID);
1509 return Qnil;
1510}
1511
Bram Moolenaar6217cdc2012-04-25 12:28:09 +02001512static VALUE f_nop(VALUE self UNUSED)
Bram Moolenaar35df7d22012-04-20 18:05:47 +02001513{
1514 return Qnil;
1515}
1516
Bram Moolenaarcd8b20a2009-05-22 16:20:57 +00001517static VALUE f_p(int argc, VALUE *argv, VALUE self UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001518{
1519 int i;
1520 VALUE str = rb_str_new("", 0);
1521
Bram Moolenaar758535a2016-03-30 22:06:16 +02001522 for (i = 0; i < argc; i++)
1523 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001524 if (i > 0) rb_str_cat(str, ", ", 2);
1525 rb_str_concat(str, rb_inspect(argv[i]));
1526 }
Bram Moolenaar165641d2010-02-17 16:23:09 +01001527 MSG(RSTRING_PTR(str));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001528 return Qnil;
1529}
1530
1531static void ruby_io_init(void)
1532{
1533#ifndef DYNAMIC_RUBY
1534 RUBYEXTERN VALUE rb_stdout;
1535#endif
1536
1537 rb_stdout = rb_obj_alloc(rb_cObject);
1538 rb_define_singleton_method(rb_stdout, "write", vim_message, 1);
Bram Moolenaar35df7d22012-04-20 18:05:47 +02001539 rb_define_singleton_method(rb_stdout, "flush", f_nop, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001540 rb_define_global_function("p", f_p, -1);
1541}
1542
1543static void ruby_vim_init(void)
1544{
1545 objtbl = rb_hash_new();
1546 rb_global_variable(&objtbl);
1547
Bram Moolenaarf711faf2007-05-10 16:48:19 +00001548 /* The Vim module used to be called "VIM", but "Vim" is better. Make an
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02001549 * alias "VIM" for backwards compatibility. */
Bram Moolenaarf711faf2007-05-10 16:48:19 +00001550 mVIM = rb_define_module("Vim");
1551 rb_define_const(rb_cObject, "VIM", mVIM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001552 rb_define_const(mVIM, "VERSION_MAJOR", INT2NUM(VIM_VERSION_MAJOR));
1553 rb_define_const(mVIM, "VERSION_MINOR", INT2NUM(VIM_VERSION_MINOR));
1554 rb_define_const(mVIM, "VERSION_BUILD", INT2NUM(VIM_VERSION_BUILD));
1555 rb_define_const(mVIM, "VERSION_PATCHLEVEL", INT2NUM(VIM_VERSION_PATCHLEVEL));
1556 rb_define_const(mVIM, "VERSION_SHORT", rb_str_new2(VIM_VERSION_SHORT));
1557 rb_define_const(mVIM, "VERSION_MEDIUM", rb_str_new2(VIM_VERSION_MEDIUM));
1558 rb_define_const(mVIM, "VERSION_LONG", rb_str_new2(VIM_VERSION_LONG));
1559 rb_define_const(mVIM, "VERSION_LONG_DATE", rb_str_new2(VIM_VERSION_LONG_DATE));
1560 rb_define_module_function(mVIM, "message", vim_message, 1);
1561 rb_define_module_function(mVIM, "set_option", vim_set_option, 1);
1562 rb_define_module_function(mVIM, "command", vim_command, 1);
1563 rb_define_module_function(mVIM, "evaluate", vim_evaluate, 1);
1564
1565 eDeletedBufferError = rb_define_class_under(mVIM, "DeletedBufferError",
1566 rb_eStandardError);
1567 eDeletedWindowError = rb_define_class_under(mVIM, "DeletedWindowError",
1568 rb_eStandardError);
1569
1570 cBuffer = rb_define_class_under(mVIM, "Buffer", rb_cObject);
1571 rb_define_singleton_method(cBuffer, "current", buffer_s_current, 0);
1572 rb_define_singleton_method(cBuffer, "count", buffer_s_count, 0);
1573 rb_define_singleton_method(cBuffer, "[]", buffer_s_aref, 1);
1574 rb_define_method(cBuffer, "name", buffer_name, 0);
1575 rb_define_method(cBuffer, "number", buffer_number, 0);
1576 rb_define_method(cBuffer, "count", buffer_count, 0);
1577 rb_define_method(cBuffer, "length", buffer_count, 0);
1578 rb_define_method(cBuffer, "[]", buffer_aref, 1);
1579 rb_define_method(cBuffer, "[]=", buffer_aset, 2);
1580 rb_define_method(cBuffer, "delete", buffer_delete, 1);
1581 rb_define_method(cBuffer, "append", buffer_append, 2);
1582
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001583 /* Added line manipulation functions
1584 * SegPhault - 03/07/05 */
1585 rb_define_method(cBuffer, "line_number", current_line_number, 0);
1586 rb_define_method(cBuffer, "line", line_s_current, 0);
1587 rb_define_method(cBuffer, "line=", set_current_line, 1);
1588
1589
Bram Moolenaar071d4272004-06-13 20:20:40 +00001590 cVimWindow = rb_define_class_under(mVIM, "Window", rb_cObject);
1591 rb_define_singleton_method(cVimWindow, "current", window_s_current, 0);
1592 rb_define_singleton_method(cVimWindow, "count", window_s_count, 0);
1593 rb_define_singleton_method(cVimWindow, "[]", window_s_aref, 1);
1594 rb_define_method(cVimWindow, "buffer", window_buffer, 0);
1595 rb_define_method(cVimWindow, "height", window_height, 0);
1596 rb_define_method(cVimWindow, "height=", window_set_height, 1);
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001597 rb_define_method(cVimWindow, "width", window_width, 0);
1598 rb_define_method(cVimWindow, "width=", window_set_width, 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001599 rb_define_method(cVimWindow, "cursor", window_cursor, 0);
1600 rb_define_method(cVimWindow, "cursor=", window_set_cursor, 1);
1601
1602 rb_define_virtual_variable("$curbuf", buffer_s_current, 0);
1603 rb_define_virtual_variable("$curwin", window_s_current, 0);
1604}
Bram Moolenaar99685e62013-05-11 13:56:18 +02001605
1606void vim_ruby_init(void *stack_start)
1607{
1608 /* should get machine stack start address early in main function */
1609 ruby_stack_start = stack_start;
1610}