blob: eb79077bdaf7c434998905bcf7a2d16d0a130322 [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 Moolenaard0573012017-10-28 21:11:06 +0200130#if defined(MACOS_X)
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
Bram Moolenaard0573012017-10-28 21:11:06 +0200254# define rb_protect dll_rb_protect
255# define rb_load dll_rb_load
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200256# ifndef RUBY19_OR_LATER
257# define rb_num2long dll_rb_num2long
258# endif
259# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER <= 19
260# define rb_num2ulong dll_rb_num2ulong
261# endif
262# define rb_obj_alloc dll_rb_obj_alloc
263# define rb_obj_as_string dll_rb_obj_as_string
264# define rb_obj_id dll_rb_obj_id
265# define rb_raise dll_rb_raise
266# define rb_str_cat dll_rb_str_cat
267# define rb_str_concat dll_rb_str_concat
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100268# undef rb_str_new
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200269# define rb_str_new dll_rb_str_new
270# ifdef rb_str_new2
Bram Moolenaar1d2beae2010-05-22 21:56:55 +0200271/* Ruby may #define rb_str_new2 to use rb_str_new_cstr. */
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200272# define need_rb_str_new_cstr 1
Bram Moolenaar3ca71f12010-10-27 16:49:47 +0200273/* Ruby's headers #define rb_str_new_cstr to make use of GCC's
274 * __builtin_constant_p extension. */
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200275# undef rb_str_new_cstr
276# define rb_str_new_cstr dll_rb_str_new_cstr
Bram Moolenaar76a86062013-05-11 17:45:48 +0200277# else
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200278# define rb_str_new2 dll_rb_str_new2
Bram Moolenaar76a86062013-05-11 17:45:48 +0200279# endif
Bram Moolenaar3ca71f12010-10-27 16:49:47 +0200280# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 18
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200281# define rb_string_value dll_rb_string_value
282# define rb_string_value_ptr dll_rb_string_value_ptr
283# define rb_float_new dll_rb_float_new
284# define rb_ary_new dll_rb_ary_new
285# define rb_ary_push dll_rb_ary_push
Bram Moolenaar10f3a792013-05-20 12:52:29 +0200286# if defined(RUBY19_OR_LATER) || defined(RUBY_INIT_STACK)
287# ifdef __ia64
288# define rb_ia64_bsp dll_rb_ia64_bsp
289# undef ruby_init_stack
290# define ruby_init_stack(addr) dll_ruby_init_stack((addr), rb_ia64_bsp())
291# else
292# define ruby_init_stack dll_ruby_init_stack
293# endif
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200294# endif
295# else
296# define rb_str2cstr dll_rb_str2cstr
Bram Moolenaar3ca71f12010-10-27 16:49:47 +0200297# endif
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200298# ifdef RUBY19_OR_LATER
299# define rb_errinfo dll_rb_errinfo
300# else
301# define ruby_errinfo (*dll_ruby_errinfo)
302# endif
303# define ruby_init dll_ruby_init
304# define ruby_init_loadpath dll_ruby_init_loadpath
305# ifdef WIN3264
306# define NtInitialize dll_NtInitialize
Bram Moolenaar4f391792017-01-15 16:59:07 +0100307# define ruby_sysinit dll_ruby_sysinit
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200308# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 18
309# define rb_w32_snprintf dll_rb_w32_snprintf
310# endif
311# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000312
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200313# ifdef RUBY19_OR_LATER
314# define ruby_script dll_ruby_script
315# define rb_enc_find_index dll_rb_enc_find_index
316# define rb_enc_find dll_rb_enc_find
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100317# undef rb_enc_str_new
318# define rb_enc_str_new dll_rb_enc_str_new
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200319# define rb_sprintf dll_rb_sprintf
320# define rb_require dll_rb_require
Bram Moolenaar9b1067e2015-11-19 19:33:15 +0100321# define ruby_options dll_ruby_options
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200322# endif
Bram Moolenaar165641d2010-02-17 16:23:09 +0100323
Bram Moolenaar071d4272004-06-13 20:20:40 +0000324/*
325 * Pointers for dynamic link
326 */
327static VALUE (*dll_rb_assoc_new) (VALUE, VALUE);
Bram Moolenaarba52cde2010-06-25 04:29:11 +0200328VALUE *dll_rb_cFalseClass;
329VALUE *dll_rb_cFixnum;
Bram Moolenaar2016ae52016-06-13 20:08:43 +0200330# if defined(USE_RUBY_INTEGER)
Bram Moolenaar06469e92016-06-11 22:26:53 +0200331VALUE *dll_rb_cInteger;
332# endif
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200333# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 20
Bram Moolenaardb3fbe52013-03-07 15:16:21 +0100334VALUE *dll_rb_cFloat;
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200335# endif
Bram Moolenaarba52cde2010-06-25 04:29:11 +0200336VALUE *dll_rb_cNilClass;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000337static VALUE *dll_rb_cObject;
Bram Moolenaarba52cde2010-06-25 04:29:11 +0200338VALUE *dll_rb_cSymbol;
339VALUE *dll_rb_cTrueClass;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000340static void (*dll_rb_check_type) (VALUE,int);
Bram Moolenaarf2f6d292015-12-28 20:57:10 +0100341# ifdef USE_TYPEDDATA
342static void *(*dll_rb_check_typeddata) (VALUE,const rb_data_type_t *);
343# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000344static VALUE (*dll_rb_class_path) (VALUE);
Bram Moolenaarf2f6d292015-12-28 20:57:10 +0100345# ifdef USE_TYPEDDATA
346# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 23
347static VALUE (*dll_rb_data_typed_object_wrap) (VALUE, void*, const rb_data_type_t *);
348# else
349static VALUE (*dll_rb_data_typed_object_alloc) (VALUE, void*, const rb_data_type_t *);
350# endif
351# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000352static VALUE (*dll_rb_data_object_alloc) (VALUE, void*, RUBY_DATA_FUNC, RUBY_DATA_FUNC);
Bram Moolenaarf2f6d292015-12-28 20:57:10 +0100353# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000354static VALUE (*dll_rb_define_class_under) (VALUE, const char*, VALUE);
355static void (*dll_rb_define_const) (VALUE,const char*,VALUE);
356static void (*dll_rb_define_global_function) (const char*,VALUE(*)(),int);
357static void (*dll_rb_define_method) (VALUE,const char*,VALUE(*)(),int);
358static VALUE (*dll_rb_define_module) (const char*);
359static void (*dll_rb_define_module_function) (VALUE,const char*,VALUE(*)(),int);
360static void (*dll_rb_define_singleton_method) (VALUE,const char*,VALUE(*)(),int);
361static void (*dll_rb_define_virtual_variable) (const char*,VALUE(*)(),void(*)());
362static VALUE *dll_rb_stdout;
363static VALUE *dll_rb_eArgError;
364static VALUE *dll_rb_eIndexError;
365static VALUE *dll_rb_eRuntimeError;
366static VALUE *dll_rb_eStandardError;
367static VALUE (*dll_rb_eval_string_protect) (const char*, int*);
368static void (*dll_rb_global_variable) (VALUE*);
369static VALUE (*dll_rb_hash_aset) (VALUE, VALUE, VALUE);
370static VALUE (*dll_rb_hash_new) (void);
371static VALUE (*dll_rb_inspect) (VALUE);
372static VALUE (*dll_rb_int2inum) (long);
Bram Moolenaara2aa31a2014-02-23 22:52:40 +0100373# if VIM_SIZEOF_INT < VIM_SIZEOF_LONG /* 64 bits only */
Bram Moolenaar2623b4f2012-09-18 16:36:32 +0200374static long (*dll_rb_fix2int) (VALUE);
375static long (*dll_rb_num2int) (VALUE);
376static unsigned long (*dll_rb_num2uint) (VALUE);
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200377# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000378static VALUE (*dll_rb_lastline_get) (void);
379static void (*dll_rb_lastline_set) (VALUE);
Bram Moolenaar37badc82018-01-31 20:15:30 +0100380static VALUE (*dll_rb_protect) (VALUE (*)(VALUE), VALUE, int*);
Bram Moolenaard0573012017-10-28 21:11:06 +0200381static void (*dll_rb_load) (VALUE, int);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000382static long (*dll_rb_num2long) (VALUE);
383static unsigned long (*dll_rb_num2ulong) (VALUE);
384static VALUE (*dll_rb_obj_alloc) (VALUE);
385static VALUE (*dll_rb_obj_as_string) (VALUE);
386static VALUE (*dll_rb_obj_id) (VALUE);
387static void (*dll_rb_raise) (VALUE, const char*, ...);
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200388# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 18
Bram Moolenaarf9b5ef82010-09-29 13:02:53 +0200389static VALUE (*dll_rb_string_value) (volatile VALUE*);
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200390# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000391static char *(*dll_rb_str2cstr) (VALUE,int*);
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200392# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000393static VALUE (*dll_rb_str_cat) (VALUE, const char*, long);
394static VALUE (*dll_rb_str_concat) (VALUE, VALUE);
395static VALUE (*dll_rb_str_new) (const char*, long);
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200396# ifdef need_rb_str_new_cstr
Bram Moolenaar1d2beae2010-05-22 21:56:55 +0200397/* Ruby may #define rb_str_new2 to use rb_str_new_cstr. */
398static VALUE (*dll_rb_str_new_cstr) (const char*);
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200399# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000400static VALUE (*dll_rb_str_new2) (const char*);
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200401# endif
402# ifdef RUBY19_OR_LATER
Bram Moolenaar165641d2010-02-17 16:23:09 +0100403static VALUE (*dll_rb_errinfo) (void);
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200404# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000405static VALUE *dll_ruby_errinfo;
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200406# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000407static void (*dll_ruby_init) (void);
408static void (*dll_ruby_init_loadpath) (void);
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200409# ifdef WIN3264
Bram Moolenaar0b69c732010-02-17 15:11:50 +0100410static void (*dll_NtInitialize) (int*, char***);
Bram Moolenaar4f391792017-01-15 16:59:07 +0100411static void (*dll_ruby_sysinit) (int*, char***);
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200412# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 18
Bram Moolenaar3ca71f12010-10-27 16:49:47 +0200413static int (*dll_rb_w32_snprintf)(char*, size_t, const char*, ...);
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200414# endif
Bram Moolenaar3ca71f12010-10-27 16:49:47 +0200415# endif
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200416# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 18
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100417static char * (*dll_rb_string_value_ptr) (volatile VALUE*);
418static VALUE (*dll_rb_float_new) (double);
419static VALUE (*dll_rb_ary_new) (void);
420static VALUE (*dll_rb_ary_push) (VALUE, VALUE);
Bram Moolenaar10f3a792013-05-20 12:52:29 +0200421# if defined(RUBY19_OR_LATER) || defined(RUBY_INIT_STACK)
422# ifdef __ia64
Bram Moolenaar76a86062013-05-11 17:45:48 +0200423static void * (*dll_rb_ia64_bsp) (void);
424static void (*dll_ruby_init_stack)(VALUE*, void*);
Bram Moolenaar10f3a792013-05-20 12:52:29 +0200425# else
Bram Moolenaar76a86062013-05-11 17:45:48 +0200426static void (*dll_ruby_init_stack)(VALUE*);
Bram Moolenaar10f3a792013-05-20 12:52:29 +0200427# endif
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200428# endif
Bram Moolenaar76a86062013-05-11 17:45:48 +0200429# endif
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200430# ifdef RUBY19_OR_LATER
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100431static VALUE (*dll_rb_int2big)(SIGNED_VALUE);
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200432# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000433
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200434# ifdef RUBY19_OR_LATER
Bram Moolenaar165641d2010-02-17 16:23:09 +0100435static void (*dll_ruby_script) (const char*);
436static int (*dll_rb_enc_find_index) (const char*);
437static rb_encoding* (*dll_rb_enc_find) (const char*);
438static VALUE (*dll_rb_enc_str_new) (const char*, long, rb_encoding*);
439static VALUE (*dll_rb_sprintf) (const char*, ...);
Bram Moolenaar7a8ef142010-12-24 13:39:35 +0100440static VALUE (*dll_rb_require) (const char*);
Bram Moolenaar9b1067e2015-11-19 19:33:15 +0100441static void* (*ruby_options)(int, char**);
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200442# endif
Bram Moolenaar165641d2010-02-17 16:23:09 +0100443
Bram Moolenaara1a118b2014-02-05 22:41:15 +0100444# if defined(USE_RGENGC) && USE_RGENGC
Bram Moolenaar0c7485f2015-01-14 14:04:10 +0100445# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER == 21
Bram Moolenaara1a118b2014-02-05 22:41:15 +0100446static void (*dll_rb_gc_writebarrier_unprotect_promoted)(VALUE);
Bram Moolenaar0c7485f2015-01-14 14:04:10 +0100447# else
448static void (*dll_rb_gc_writebarrier_unprotect)(VALUE obj);
449# endif
Bram Moolenaara1a118b2014-02-05 22:41:15 +0100450# endif
451
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200452# if defined(RUBY19_OR_LATER) && !defined(PROTO)
Bram Moolenaarbbc1a592015-04-21 15:25:31 +0200453# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 22
454long rb_num2long_stub(VALUE x)
455# else
Bram Moolenaarff8cf2b2012-11-24 13:39:00 +0100456SIGNED_VALUE rb_num2long_stub(VALUE x)
Bram Moolenaarbbc1a592015-04-21 15:25:31 +0200457# endif
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100458{
459 return dll_rb_num2long(x);
460}
Bram Moolenaarff8cf2b2012-11-24 13:39:00 +0100461VALUE rb_int2big_stub(SIGNED_VALUE x)
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100462{
463 return dll_rb_int2big(x);
464}
Bram Moolenaar498af702014-03-28 21:58:21 +0100465# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 19 \
466 && VIM_SIZEOF_INT < VIM_SIZEOF_LONG
Bram Moolenaar0bcdd6e2013-04-14 16:19:03 +0200467long rb_fix2int_stub(VALUE x)
468{
469 return dll_rb_fix2int(x);
470}
471long rb_num2int_stub(VALUE x)
472{
473 return dll_rb_num2int(x);
474}
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200475# endif
476# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 20
Bram Moolenaar886ed692013-02-26 13:41:35 +0100477VALUE
478rb_float_new_in_heap(double d)
479{
480 return dll_rb_float_new(d);
481}
Bram Moolenaarbbc1a592015-04-21 15:25:31 +0200482# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 22
483unsigned long rb_num2ulong(VALUE x)
484# else
Bram Moolenaardb3fbe52013-03-07 15:16:21 +0100485VALUE rb_num2ulong(VALUE x)
Bram Moolenaarbbc1a592015-04-21 15:25:31 +0200486# endif
Bram Moolenaar886ed692013-02-26 13:41:35 +0100487{
488 return (long)RSHIFT((SIGNED_VALUE)(x),1);
489}
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200490# endif
491# endif
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100492
Bram Moolenaar3e9a1612014-11-12 16:05:04 +0100493 /* Do not generate a prototype here, VALUE isn't always defined. */
494# if defined(USE_RGENGC) && USE_RGENGC && !defined(PROTO)
Bram Moolenaar0c7485f2015-01-14 14:04:10 +0100495# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER == 21
Bram Moolenaara1a118b2014-02-05 22:41:15 +0100496void rb_gc_writebarrier_unprotect_promoted_stub(VALUE obj)
497{
Bram Moolenaar90140742014-11-27 17:44:08 +0100498 dll_rb_gc_writebarrier_unprotect_promoted(obj);
Bram Moolenaara1a118b2014-02-05 22:41:15 +0100499}
Bram Moolenaar0c7485f2015-01-14 14:04:10 +0100500# else
501void rb_gc_writebarrier_unprotect_stub(VALUE obj)
502{
503 dll_rb_gc_writebarrier_unprotect(obj);
504}
505# endif
506# endif
507
Bram Moolenaar3ca71f12010-10-27 16:49:47 +0200508static HINSTANCE hinstRuby = NULL; /* Instance of ruby.dll */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000509
510/*
Bram Moolenaarda2303d2005-08-30 21:55:26 +0000511 * Table of name to function pointer of ruby.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000512 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000513static struct
514{
515 char *name;
516 RUBY_PROC *ptr;
517} ruby_funcname_table[] =
518{
519 {"rb_assoc_new", (RUBY_PROC*)&dll_rb_assoc_new},
520 {"rb_cFalseClass", (RUBY_PROC*)&dll_rb_cFalseClass},
Bram Moolenaar2016ae52016-06-13 20:08:43 +0200521# if defined(USE_RUBY_INTEGER)
Bram Moolenaar06469e92016-06-11 22:26:53 +0200522 {"rb_cInteger", (RUBY_PROC*)&dll_rb_cInteger},
Bram Moolenaar6abda992017-01-09 21:10:31 +0100523# else
524 {"rb_cFixnum", (RUBY_PROC*)&dll_rb_cFixnum},
Bram Moolenaar06469e92016-06-11 22:26:53 +0200525# endif
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200526# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 20
Bram Moolenaardb3fbe52013-03-07 15:16:21 +0100527 {"rb_cFloat", (RUBY_PROC*)&dll_rb_cFloat},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200528# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000529 {"rb_cNilClass", (RUBY_PROC*)&dll_rb_cNilClass},
530 {"rb_cObject", (RUBY_PROC*)&dll_rb_cObject},
531 {"rb_cSymbol", (RUBY_PROC*)&dll_rb_cSymbol},
532 {"rb_cTrueClass", (RUBY_PROC*)&dll_rb_cTrueClass},
533 {"rb_check_type", (RUBY_PROC*)&dll_rb_check_type},
Bram Moolenaarf2f6d292015-12-28 20:57:10 +0100534# ifdef USE_TYPEDDATA
535 {"rb_check_typeddata", (RUBY_PROC*)&dll_rb_check_typeddata},
536# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000537 {"rb_class_path", (RUBY_PROC*)&dll_rb_class_path},
Bram Moolenaarf2f6d292015-12-28 20:57:10 +0100538# ifdef USE_TYPEDDATA
539# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 23
540 {"rb_data_typed_object_wrap", (RUBY_PROC*)&dll_rb_data_typed_object_wrap},
541# else
542 {"rb_data_typed_object_alloc", (RUBY_PROC*)&dll_rb_data_typed_object_alloc},
543# endif
544# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000545 {"rb_data_object_alloc", (RUBY_PROC*)&dll_rb_data_object_alloc},
Bram Moolenaarf2f6d292015-12-28 20:57:10 +0100546# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000547 {"rb_define_class_under", (RUBY_PROC*)&dll_rb_define_class_under},
548 {"rb_define_const", (RUBY_PROC*)&dll_rb_define_const},
549 {"rb_define_global_function", (RUBY_PROC*)&dll_rb_define_global_function},
550 {"rb_define_method", (RUBY_PROC*)&dll_rb_define_method},
551 {"rb_define_module", (RUBY_PROC*)&dll_rb_define_module},
552 {"rb_define_module_function", (RUBY_PROC*)&dll_rb_define_module_function},
553 {"rb_define_singleton_method", (RUBY_PROC*)&dll_rb_define_singleton_method},
554 {"rb_define_virtual_variable", (RUBY_PROC*)&dll_rb_define_virtual_variable},
555 {"rb_stdout", (RUBY_PROC*)&dll_rb_stdout},
556 {"rb_eArgError", (RUBY_PROC*)&dll_rb_eArgError},
557 {"rb_eIndexError", (RUBY_PROC*)&dll_rb_eIndexError},
558 {"rb_eRuntimeError", (RUBY_PROC*)&dll_rb_eRuntimeError},
559 {"rb_eStandardError", (RUBY_PROC*)&dll_rb_eStandardError},
560 {"rb_eval_string_protect", (RUBY_PROC*)&dll_rb_eval_string_protect},
561 {"rb_global_variable", (RUBY_PROC*)&dll_rb_global_variable},
562 {"rb_hash_aset", (RUBY_PROC*)&dll_rb_hash_aset},
563 {"rb_hash_new", (RUBY_PROC*)&dll_rb_hash_new},
564 {"rb_inspect", (RUBY_PROC*)&dll_rb_inspect},
565 {"rb_int2inum", (RUBY_PROC*)&dll_rb_int2inum},
Bram Moolenaara2aa31a2014-02-23 22:52:40 +0100566# if VIM_SIZEOF_INT < VIM_SIZEOF_LONG /* 64 bits only */
Bram Moolenaar2623b4f2012-09-18 16:36:32 +0200567 {"rb_fix2int", (RUBY_PROC*)&dll_rb_fix2int},
568 {"rb_num2int", (RUBY_PROC*)&dll_rb_num2int},
569 {"rb_num2uint", (RUBY_PROC*)&dll_rb_num2uint},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200570# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000571 {"rb_lastline_get", (RUBY_PROC*)&dll_rb_lastline_get},
572 {"rb_lastline_set", (RUBY_PROC*)&dll_rb_lastline_set},
Bram Moolenaard0573012017-10-28 21:11:06 +0200573 {"rb_protect", (RUBY_PROC*)&dll_rb_protect},
574 {"rb_load", (RUBY_PROC*)&dll_rb_load},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000575 {"rb_num2long", (RUBY_PROC*)&dll_rb_num2long},
576 {"rb_num2ulong", (RUBY_PROC*)&dll_rb_num2ulong},
577 {"rb_obj_alloc", (RUBY_PROC*)&dll_rb_obj_alloc},
578 {"rb_obj_as_string", (RUBY_PROC*)&dll_rb_obj_as_string},
579 {"rb_obj_id", (RUBY_PROC*)&dll_rb_obj_id},
580 {"rb_raise", (RUBY_PROC*)&dll_rb_raise},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200581# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 18
Bram Moolenaarf9b5ef82010-09-29 13:02:53 +0200582 {"rb_string_value", (RUBY_PROC*)&dll_rb_string_value},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200583# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000584 {"rb_str2cstr", (RUBY_PROC*)&dll_rb_str2cstr},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200585# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000586 {"rb_str_cat", (RUBY_PROC*)&dll_rb_str_cat},
587 {"rb_str_concat", (RUBY_PROC*)&dll_rb_str_concat},
588 {"rb_str_new", (RUBY_PROC*)&dll_rb_str_new},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200589# ifdef need_rb_str_new_cstr
Bram Moolenaar1d2beae2010-05-22 21:56:55 +0200590 {"rb_str_new_cstr", (RUBY_PROC*)&dll_rb_str_new_cstr},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200591# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000592 {"rb_str_new2", (RUBY_PROC*)&dll_rb_str_new2},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200593# endif
594# ifdef RUBY19_OR_LATER
Bram Moolenaar165641d2010-02-17 16:23:09 +0100595 {"rb_errinfo", (RUBY_PROC*)&dll_rb_errinfo},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200596# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000597 {"ruby_errinfo", (RUBY_PROC*)&dll_ruby_errinfo},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200598# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000599 {"ruby_init", (RUBY_PROC*)&dll_ruby_init},
600 {"ruby_init_loadpath", (RUBY_PROC*)&dll_ruby_init_loadpath},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200601# ifdef WIN3264
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200602# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER < 19
Bram Moolenaar4f391792017-01-15 16:59:07 +0100603 {"NtInitialize", (RUBY_PROC*)&dll_NtInitialize},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200604# else
Bram Moolenaar4f391792017-01-15 16:59:07 +0100605 {"ruby_sysinit", (RUBY_PROC*)&dll_ruby_sysinit},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200606# endif
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200607# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 18
Bram Moolenaar071d4272004-06-13 20:20:40 +0000608 {"rb_w32_snprintf", (RUBY_PROC*)&dll_rb_w32_snprintf},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200609# endif
Bram Moolenaar3ca71f12010-10-27 16:49:47 +0200610# endif
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200611# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 18
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100612 {"rb_string_value_ptr", (RUBY_PROC*)&dll_rb_string_value_ptr},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200613# if DYNAMIC_RUBY_VER <= 19
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100614 {"rb_float_new", (RUBY_PROC*)&dll_rb_float_new},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200615# else
Bram Moolenaar886ed692013-02-26 13:41:35 +0100616 {"rb_float_new_in_heap", (RUBY_PROC*)&dll_rb_float_new},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200617# endif
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100618 {"rb_ary_new", (RUBY_PROC*)&dll_rb_ary_new},
619 {"rb_ary_push", (RUBY_PROC*)&dll_rb_ary_push},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200620# endif
621# ifdef RUBY19_OR_LATER
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100622 {"rb_int2big", (RUBY_PROC*)&dll_rb_int2big},
Bram Moolenaar165641d2010-02-17 16:23:09 +0100623 {"ruby_script", (RUBY_PROC*)&dll_ruby_script},
624 {"rb_enc_find_index", (RUBY_PROC*)&dll_rb_enc_find_index},
625 {"rb_enc_find", (RUBY_PROC*)&dll_rb_enc_find},
626 {"rb_enc_str_new", (RUBY_PROC*)&dll_rb_enc_str_new},
627 {"rb_sprintf", (RUBY_PROC*)&dll_rb_sprintf},
Bram Moolenaar7a8ef142010-12-24 13:39:35 +0100628 {"rb_require", (RUBY_PROC*)&dll_rb_require},
Bram Moolenaar9b1067e2015-11-19 19:33:15 +0100629 {"ruby_options", (RUBY_PROC*)&dll_ruby_options},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200630# endif
Bram Moolenaar10f3a792013-05-20 12:52:29 +0200631# if defined(RUBY19_OR_LATER) || defined(RUBY_INIT_STACK)
632# ifdef __ia64
633 {"rb_ia64_bsp", (RUBY_PROC*)&dll_rb_ia64_bsp},
634# endif
635 {"ruby_init_stack", (RUBY_PROC*)&dll_ruby_init_stack},
636# endif
Bram Moolenaara1a118b2014-02-05 22:41:15 +0100637# if defined(USE_RGENGC) && USE_RGENGC
Bram Moolenaar0c7485f2015-01-14 14:04:10 +0100638# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER == 21
Bram Moolenaara1a118b2014-02-05 22:41:15 +0100639 {"rb_gc_writebarrier_unprotect_promoted", (RUBY_PROC*)&dll_rb_gc_writebarrier_unprotect_promoted},
Bram Moolenaar0c7485f2015-01-14 14:04:10 +0100640# else
641 {"rb_gc_writebarrier_unprotect", (RUBY_PROC*)&dll_rb_gc_writebarrier_unprotect},
642# endif
Bram Moolenaara1a118b2014-02-05 22:41:15 +0100643# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000644 {"", NULL},
645};
646
647/*
648 * Free ruby.dll
649 */
650 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100651end_dynamic_ruby(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000652{
653 if (hinstRuby)
654 {
Bram Moolenaarf9b5ef82010-09-29 13:02:53 +0200655 close_dll(hinstRuby);
Bram Moolenaar3ca71f12010-10-27 16:49:47 +0200656 hinstRuby = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000657 }
658}
659
660/*
661 * Load library and get all pointers.
662 * Parameter 'libname' provides name of DLL.
663 * Return OK or FAIL.
664 */
665 static int
666ruby_runtime_link_init(char *libname, int verbose)
667{
668 int i;
669
670 if (hinstRuby)
671 return OK;
Bram Moolenaarf9b5ef82010-09-29 13:02:53 +0200672 hinstRuby = load_dll(libname);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000673 if (!hinstRuby)
674 {
675 if (verbose)
676 EMSG2(_(e_loadlib), libname);
677 return FAIL;
678 }
679
680 for (i = 0; ruby_funcname_table[i].ptr; ++i)
681 {
Bram Moolenaarf9b5ef82010-09-29 13:02:53 +0200682 if (!(*ruby_funcname_table[i].ptr = symbol_from_dll(hinstRuby,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000683 ruby_funcname_table[i].name)))
684 {
Bram Moolenaarf9b5ef82010-09-29 13:02:53 +0200685 close_dll(hinstRuby);
Bram Moolenaar3ca71f12010-10-27 16:49:47 +0200686 hinstRuby = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000687 if (verbose)
688 EMSG2(_(e_loadfunc), ruby_funcname_table[i].name);
689 return FAIL;
690 }
691 }
692 return OK;
693}
694
695/*
696 * If ruby is enabled (there is installed ruby on Windows system) return TRUE,
697 * else FALSE.
698 */
699 int
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100700ruby_enabled(int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000701{
Bram Moolenaar25e4fcd2016-01-09 14:57:47 +0100702 return ruby_runtime_link_init((char *)p_rubydll, verbose) == OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000703}
704#endif /* defined(DYNAMIC_RUBY) || defined(PROTO) */
705
706 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100707ruby_end(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000708{
709#ifdef DYNAMIC_RUBY
710 end_dynamic_ruby();
711#endif
712}
713
714void ex_ruby(exarg_T *eap)
715{
716 int state;
717 char *script = NULL;
718
Bram Moolenaar35a2e192006-03-13 22:07:11 +0000719 script = (char *)script_get(eap, eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000720 if (!eap->skip && ensure_ruby_initialized())
721 {
722 if (script == NULL)
723 rb_eval_string_protect((char *)eap->arg, &state);
724 else
725 rb_eval_string_protect(script, &state);
726 if (state)
727 error_print(state);
728 }
729 vim_free(script);
730}
731
Bram Moolenaar165641d2010-02-17 16:23:09 +0100732/*
733 * In Ruby 1.9 or later, ruby String object has encoding.
734 * conversion buffer string of vim to ruby String object using
735 * VIM encoding option.
736 */
737 static VALUE
738vim_str2rb_enc_str(const char *s)
739{
Bram Moolenaar639a2552010-03-17 18:15:23 +0100740#ifdef RUBY19_OR_LATER
Bram Moolenaar165641d2010-02-17 16:23:09 +0100741 int isnum;
742 long lval;
743 char_u *sval;
744 rb_encoding *enc;
745
746 isnum = get_option_value((char_u *)"enc", &lval, &sval, 0);
747 if (isnum == 0)
748 {
749 enc = rb_enc_find((char *)sval);
750 vim_free(sval);
Bram Moolenaar758535a2016-03-30 22:06:16 +0200751 if (enc)
752 {
Bram Moolenaar9b0ac222016-06-01 20:31:43 +0200753 return rb_enc_str_new(s, (long)strlen(s), enc);
Bram Moolenaar165641d2010-02-17 16:23:09 +0100754 }
755 }
756#endif
757 return rb_str_new2(s);
758}
759
760 static VALUE
761eval_enc_string_protect(const char *str, int *state)
762{
Bram Moolenaar639a2552010-03-17 18:15:23 +0100763#ifdef RUBY19_OR_LATER
Bram Moolenaar165641d2010-02-17 16:23:09 +0100764 int isnum;
765 long lval;
766 char_u *sval;
767 rb_encoding *enc;
768 VALUE v;
769
770 isnum = get_option_value((char_u *)"enc", &lval, &sval, 0);
771 if (isnum == 0)
772 {
773 enc = rb_enc_find((char *)sval);
774 vim_free(sval);
775 if (enc)
776 {
777 v = rb_sprintf("#-*- coding:%s -*-\n%s", rb_enc_name(enc), str);
778 return rb_eval_string_protect(StringValuePtr(v), state);
779 }
780 }
781#endif
782 return rb_eval_string_protect(str, state);
783}
784
Bram Moolenaar071d4272004-06-13 20:20:40 +0000785void ex_rubydo(exarg_T *eap)
786{
787 int state;
788 linenr_T i;
Bram Moolenaarc593fee2017-01-29 23:11:25 +0100789 buf_T *was_curbuf = curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000790
791 if (ensure_ruby_initialized())
792 {
793 if (u_save(eap->line1 - 1, eap->line2 + 1) != OK)
794 return;
Bram Moolenaar758535a2016-03-30 22:06:16 +0200795 for (i = eap->line1; i <= eap->line2; i++)
796 {
Bram Moolenaare980d8a2010-12-08 13:11:21 +0100797 VALUE line;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000798
Bram Moolenaarc593fee2017-01-29 23:11:25 +0100799 if (i > curbuf->b_ml.ml_line_count)
800 break;
Bram Moolenaare980d8a2010-12-08 13:11:21 +0100801 line = vim_str2rb_enc_str((char *)ml_get(i));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000802 rb_lastline_set(line);
Bram Moolenaar165641d2010-02-17 16:23:09 +0100803 eval_enc_string_protect((char *) eap->arg, &state);
Bram Moolenaar758535a2016-03-30 22:06:16 +0200804 if (state)
805 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000806 error_print(state);
807 break;
808 }
Bram Moolenaarc593fee2017-01-29 23:11:25 +0100809 if (was_curbuf != curbuf)
810 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000811 line = rb_lastline_get();
Bram Moolenaar758535a2016-03-30 22:06:16 +0200812 if (!NIL_P(line))
813 {
814 if (TYPE(line) != T_STRING)
815 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000816 EMSG(_("E265: $_ must be an instance of String"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000817 return;
818 }
Bram Moolenaar165641d2010-02-17 16:23:09 +0100819 ml_replace(i, (char_u *) StringValuePtr(line), 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000820 changed();
821#ifdef SYNTAX_HL
822 syn_changed(i); /* recompute syntax hl. for this line */
823#endif
824 }
825 }
826 check_cursor();
827 update_curbuf(NOT_VALID);
828 }
829}
830
Bram Moolenaar0b394642018-05-17 13:11:46 +0200831static VALUE rb_load_wrap(VALUE file_to_load)
Bram Moolenaar37badc82018-01-31 20:15:30 +0100832{
833 rb_load(file_to_load, 0);
834 return Qnil;
835}
836
Bram Moolenaar071d4272004-06-13 20:20:40 +0000837void ex_rubyfile(exarg_T *eap)
838{
839 int state;
840
841 if (ensure_ruby_initialized())
842 {
Bram Moolenaar37badc82018-01-31 20:15:30 +0100843 VALUE file_to_load = rb_str_new2((const char *)eap->arg);
844 rb_protect(rb_load_wrap, file_to_load, &state);
845 if (state)
846 error_print(state);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000847 }
848}
849
850void ruby_buffer_free(buf_T *buf)
851{
Bram Moolenaare344bea2005-09-01 20:46:49 +0000852 if (buf->b_ruby_ref)
853 {
854 rb_hash_aset(objtbl, rb_obj_id((VALUE) buf->b_ruby_ref), Qnil);
855 RDATA(buf->b_ruby_ref)->data = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000856 }
857}
858
859void ruby_window_free(win_T *win)
860{
Bram Moolenaare344bea2005-09-01 20:46:49 +0000861 if (win->w_ruby_ref)
862 {
863 rb_hash_aset(objtbl, rb_obj_id((VALUE) win->w_ruby_ref), Qnil);
864 RDATA(win->w_ruby_ref)->data = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000865 }
866}
867
868static int ensure_ruby_initialized(void)
869{
870 if (!ruby_initialized)
871 {
872#ifdef DYNAMIC_RUBY
873 if (ruby_enabled(TRUE))
874 {
875#endif
Bram Moolenaar0b69c732010-02-17 15:11:50 +0100876#ifdef _WIN32
877 /* suggested by Ariya Mizutani */
878 int argc = 1;
879 char *argv[] = {"gvim.exe"};
Bram Moolenaar90140742014-11-27 17:44:08 +0100880 char **argvp = argv;
Bram Moolenaarfe6ce332017-01-14 20:12:01 +0100881# ifdef RUBY19_OR_LATER
882 ruby_sysinit(&argc, &argvp);
883# else
Bram Moolenaar90140742014-11-27 17:44:08 +0100884 NtInitialize(&argc, &argvp);
Bram Moolenaarfe6ce332017-01-14 20:12:01 +0100885# endif
Bram Moolenaar0b69c732010-02-17 15:11:50 +0100886#endif
Bram Moolenaarb2c03502010-07-02 20:20:09 +0200887 {
Bram Moolenaar10f3a792013-05-20 12:52:29 +0200888#if defined(RUBY19_OR_LATER) || defined(RUBY_INIT_STACK)
Bram Moolenaar99685e62013-05-11 13:56:18 +0200889 ruby_init_stack(ruby_stack_start);
Bram Moolenaar165641d2010-02-17 16:23:09 +0100890#endif
Bram Moolenaarb2c03502010-07-02 20:20:09 +0200891 ruby_init();
892 }
Bram Moolenaar639a2552010-03-17 18:15:23 +0100893#ifdef RUBY19_OR_LATER
Bram Moolenaar7a8ef142010-12-24 13:39:35 +0100894 {
895 int dummy_argc = 2;
Bram Moolenaard1bc96ce2017-09-26 21:21:44 +0200896 char *dummy_argv[] = {"vim-ruby", "-e_=0"};
Bram Moolenaar9b1067e2015-11-19 19:33:15 +0100897 ruby_options(dummy_argc, dummy_argv);
Bram Moolenaar7a8ef142010-12-24 13:39:35 +0100898 }
Bram Moolenaar165641d2010-02-17 16:23:09 +0100899 ruby_script("vim-ruby");
Bram Moolenaar7a8ef142010-12-24 13:39:35 +0100900#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000901 ruby_init_loadpath();
Bram Moolenaar165641d2010-02-17 16:23:09 +0100902#endif
Bram Moolenaar7a8ef142010-12-24 13:39:35 +0100903 ruby_io_init();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000904 ruby_vim_init();
905 ruby_initialized = 1;
906#ifdef DYNAMIC_RUBY
907 }
908 else
909 {
910 EMSG(_("E266: Sorry, this command is disabled, the Ruby library could not be loaded."));
911 return 0;
912 }
913#endif
914 }
915 return ruby_initialized;
916}
917
918static void error_print(int state)
919{
920#ifndef DYNAMIC_RUBY
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100921#if !(defined(RUBY_VERSION) && RUBY_VERSION >= 19) \
922 && !(defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 19)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000923 RUBYEXTERN VALUE ruby_errinfo;
924#endif
Bram Moolenaar165641d2010-02-17 16:23:09 +0100925#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000926 VALUE eclass;
927 VALUE einfo;
928 char buff[BUFSIZ];
929
930#define TAG_RETURN 0x1
931#define TAG_BREAK 0x2
932#define TAG_NEXT 0x3
933#define TAG_RETRY 0x4
934#define TAG_REDO 0x5
935#define TAG_RAISE 0x6
936#define TAG_THROW 0x7
937#define TAG_FATAL 0x8
938#define TAG_MASK 0xf
939
Bram Moolenaar758535a2016-03-30 22:06:16 +0200940 switch (state)
941 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000942 case TAG_RETURN:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000943 EMSG(_("E267: unexpected return"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000944 break;
945 case TAG_NEXT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000946 EMSG(_("E268: unexpected next"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000947 break;
948 case TAG_BREAK:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000949 EMSG(_("E269: unexpected break"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000950 break;
951 case TAG_REDO:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000952 EMSG(_("E270: unexpected redo"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000953 break;
954 case TAG_RETRY:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000955 EMSG(_("E271: retry outside of rescue clause"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000956 break;
957 case TAG_RAISE:
958 case TAG_FATAL:
Bram Moolenaar639a2552010-03-17 18:15:23 +0100959#ifdef RUBY19_OR_LATER
Bram Moolenaar165641d2010-02-17 16:23:09 +0100960 eclass = CLASS_OF(rb_errinfo());
961 einfo = rb_obj_as_string(rb_errinfo());
962#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000963 eclass = CLASS_OF(ruby_errinfo);
964 einfo = rb_obj_as_string(ruby_errinfo);
Bram Moolenaar165641d2010-02-17 16:23:09 +0100965#endif
Bram Moolenaar758535a2016-03-30 22:06:16 +0200966 if (eclass == rb_eRuntimeError && RSTRING_LEN(einfo) == 0)
967 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000968 EMSG(_("E272: unhandled exception"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000969 }
Bram Moolenaar758535a2016-03-30 22:06:16 +0200970 else
971 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000972 VALUE epath;
973 char *p;
974
975 epath = rb_class_path(eclass);
Bram Moolenaar9c13b352005-05-19 20:53:52 +0000976 vim_snprintf(buff, BUFSIZ, "%s: %s",
Bram Moolenaar165641d2010-02-17 16:23:09 +0100977 RSTRING_PTR(epath), RSTRING_PTR(einfo));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000978 p = strchr(buff, '\n');
979 if (p) *p = '\0';
980 EMSG(buff);
981 }
982 break;
983 default:
Bram Moolenaar9c13b352005-05-19 20:53:52 +0000984 vim_snprintf(buff, BUFSIZ, _("E273: unknown longjmp status %d"), state);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000985 EMSG(buff);
986 break;
987 }
988}
989
Bram Moolenaarcd8b20a2009-05-22 16:20:57 +0000990static VALUE vim_message(VALUE self UNUSED, VALUE str)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000991{
992 char *buff, *p;
993
994 str = rb_obj_as_string(str);
Bram Moolenaar3f5f7952011-08-04 19:34:59 +0200995 if (RSTRING_LEN(str) > 0)
996 {
997 /* Only do this when the string isn't empty, alloc(0) causes trouble. */
Bram Moolenaar00ccf542017-09-03 15:17:48 +0200998 buff = ALLOCA_N(char, RSTRING_LEN(str) + 1);
Bram Moolenaar3f5f7952011-08-04 19:34:59 +0200999 strcpy(buff, RSTRING_PTR(str));
1000 p = strchr(buff, '\n');
1001 if (p) *p = '\0';
1002 MSG(buff);
1003 }
1004 else
1005 {
1006 MSG("");
1007 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001008 return Qnil;
1009}
1010
Bram Moolenaarcd8b20a2009-05-22 16:20:57 +00001011static VALUE vim_set_option(VALUE self UNUSED, VALUE str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001012{
Bram Moolenaar165641d2010-02-17 16:23:09 +01001013 do_set((char_u *)StringValuePtr(str), 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001014 update_screen(NOT_VALID);
1015 return Qnil;
1016}
1017
Bram Moolenaarcd8b20a2009-05-22 16:20:57 +00001018static VALUE vim_command(VALUE self UNUSED, VALUE str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001019{
Bram Moolenaar165641d2010-02-17 16:23:09 +01001020 do_cmdline_cmd((char_u *)StringValuePtr(str));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001021 return Qnil;
1022}
1023
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001024#ifdef FEAT_EVAL
1025static VALUE vim_to_ruby(typval_T *tv)
1026{
1027 VALUE result = Qnil;
1028
1029 if (tv->v_type == VAR_STRING)
1030 {
Bram Moolenaar94127e42010-03-19 23:08:48 +01001031 result = rb_str_new2(tv->vval.v_string == NULL
1032 ? "" : (char *)(tv->vval.v_string));
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001033 }
1034 else if (tv->v_type == VAR_NUMBER)
1035 {
Bram Moolenaar639a2552010-03-17 18:15:23 +01001036 result = INT2NUM(tv->vval.v_number);
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001037 }
1038# ifdef FEAT_FLOAT
1039 else if (tv->v_type == VAR_FLOAT)
1040 {
Bram Moolenaar639a2552010-03-17 18:15:23 +01001041 result = rb_float_new(tv->vval.v_float);
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001042 }
1043# endif
1044 else if (tv->v_type == VAR_LIST)
1045 {
Bram Moolenaar639a2552010-03-17 18:15:23 +01001046 list_T *list = tv->vval.v_list;
1047 listitem_T *curr;
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001048
Bram Moolenaar639a2552010-03-17 18:15:23 +01001049 result = rb_ary_new();
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001050
Bram Moolenaar639a2552010-03-17 18:15:23 +01001051 if (list != NULL)
1052 {
1053 for (curr = list->lv_first; curr != NULL; curr = curr->li_next)
1054 {
1055 rb_ary_push(result, vim_to_ruby(&curr->li_tv));
1056 }
1057 }
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001058 }
1059 else if (tv->v_type == VAR_DICT)
1060 {
Bram Moolenaar639a2552010-03-17 18:15:23 +01001061 result = rb_hash_new();
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001062
Bram Moolenaar639a2552010-03-17 18:15:23 +01001063 if (tv->vval.v_dict != NULL)
1064 {
1065 hashtab_T *ht = &tv->vval.v_dict->dv_hashtab;
1066 long_u todo = ht->ht_used;
1067 hashitem_T *hi;
1068 dictitem_T *di;
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001069
Bram Moolenaar639a2552010-03-17 18:15:23 +01001070 for (hi = ht->ht_array; todo > 0; ++hi)
1071 {
1072 if (!HASHITEM_EMPTY(hi))
1073 {
1074 --todo;
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001075
Bram Moolenaar639a2552010-03-17 18:15:23 +01001076 di = dict_lookup(hi);
1077 rb_hash_aset(result, rb_str_new2((char *)hi->hi_key),
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001078 vim_to_ruby(&di->di_tv));
Bram Moolenaar639a2552010-03-17 18:15:23 +01001079 }
1080 }
1081 }
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001082 }
1083 else if (tv->v_type == VAR_SPECIAL)
1084 {
1085 if (tv->vval.v_number <= VVAL_TRUE)
1086 result = INT2NUM(tv->vval.v_number);
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001087 } /* else return Qnil; */
1088
1089 return result;
1090}
1091#endif
1092
Bram Moolenaarcd8b20a2009-05-22 16:20:57 +00001093static VALUE vim_evaluate(VALUE self UNUSED, VALUE str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001094{
1095#ifdef FEAT_EVAL
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001096 typval_T *tv;
1097 VALUE result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001098
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001099 tv = eval_expr((char_u *)StringValuePtr(str), NULL);
1100 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001101 {
Bram Moolenaar639a2552010-03-17 18:15:23 +01001102 return Qnil;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001103 }
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001104 result = vim_to_ruby(tv);
1105
1106 free_tv(tv);
1107
1108 return result;
1109#else
1110 return Qnil;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001111#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001112}
1113
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001114#ifdef USE_TYPEDDATA
1115static size_t buffer_dsize(const void *buf);
1116
1117static const rb_data_type_t buffer_type = {
1118 "vim_buffer",
1119 {0, 0, buffer_dsize, {0, 0}},
1120 0, 0,
1121# ifdef RUBY_TYPED_FREE_IMMEDIATELY
1122 0,
1123# endif
1124};
1125
1126static size_t buffer_dsize(const void *buf UNUSED)
1127{
1128 return sizeof(buf_T);
1129}
1130#endif
1131
Bram Moolenaar071d4272004-06-13 20:20:40 +00001132static VALUE buffer_new(buf_T *buf)
1133{
Bram Moolenaare344bea2005-09-01 20:46:49 +00001134 if (buf->b_ruby_ref)
1135 {
1136 return (VALUE) buf->b_ruby_ref;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001137 }
Bram Moolenaare344bea2005-09-01 20:46:49 +00001138 else
1139 {
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001140#ifdef USE_TYPEDDATA
1141 VALUE obj = TypedData_Wrap_Struct(cBuffer, &buffer_type, buf);
1142#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001143 VALUE obj = Data_Wrap_Struct(cBuffer, 0, 0, buf);
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001144#endif
Bram Moolenaare344bea2005-09-01 20:46:49 +00001145 buf->b_ruby_ref = (void *) obj;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001146 rb_hash_aset(objtbl, rb_obj_id(obj), obj);
1147 return obj;
1148 }
1149}
1150
1151static buf_T *get_buf(VALUE obj)
1152{
1153 buf_T *buf;
1154
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001155#ifdef USE_TYPEDDATA
1156 TypedData_Get_Struct(obj, buf_T, &buffer_type, buf);
1157#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001158 Data_Get_Struct(obj, buf_T, buf);
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001159#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001160 if (buf == NULL)
1161 rb_raise(eDeletedBufferError, "attempt to refer to deleted buffer");
1162 return buf;
1163}
1164
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001165static VALUE buffer_s_current(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001166{
1167 return buffer_new(curbuf);
1168}
1169
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001170static VALUE buffer_s_count(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001171{
1172 buf_T *b;
1173 int n = 0;
1174
Bram Moolenaar29323592016-07-24 22:04:11 +02001175 FOR_ALL_BUFFERS(b)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001176 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001177 /* Deleted buffers should not be counted
1178 * SegPhault - 01/07/05 */
1179 if (b->b_p_bl)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001180 n++;
1181 }
1182
Bram Moolenaar071d4272004-06-13 20:20:40 +00001183 return INT2NUM(n);
1184}
1185
Bram Moolenaarcd8b20a2009-05-22 16:20:57 +00001186static VALUE buffer_s_aref(VALUE self UNUSED, VALUE num)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001187{
1188 buf_T *b;
1189 int n = NUM2INT(num);
1190
Bram Moolenaar29323592016-07-24 22:04:11 +02001191 FOR_ALL_BUFFERS(b)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001192 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001193 /* Deleted buffers should not be counted
1194 * SegPhault - 01/07/05 */
1195 if (!b->b_p_bl)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001196 continue;
1197
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001198 if (n == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001199 return buffer_new(b);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001200
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001201 n--;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001202 }
1203 return Qnil;
1204}
1205
1206static VALUE buffer_name(VALUE self)
1207{
1208 buf_T *buf = get_buf(self);
1209
Bram Moolenaar35a2e192006-03-13 22:07:11 +00001210 return buf->b_ffname ? rb_str_new2((char *)buf->b_ffname) : Qnil;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001211}
1212
1213static VALUE buffer_number(VALUE self)
1214{
1215 buf_T *buf = get_buf(self);
1216
1217 return INT2NUM(buf->b_fnum);
1218}
1219
1220static VALUE buffer_count(VALUE self)
1221{
1222 buf_T *buf = get_buf(self);
1223
1224 return INT2NUM(buf->b_ml.ml_line_count);
1225}
1226
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001227static VALUE get_buffer_line(buf_T *buf, linenr_T n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001228{
Bram Moolenaar3c531602010-11-16 14:46:19 +01001229 if (n <= 0 || n > buf->b_ml.ml_line_count)
1230 rb_raise(rb_eIndexError, "line number %ld out of range", (long)n);
1231 return vim_str2rb_enc_str((char *)ml_get_buf(buf, n, FALSE));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001232}
1233
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001234static VALUE buffer_aref(VALUE self, VALUE num)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001235{
1236 buf_T *buf = get_buf(self);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001237
1238 if (buf != NULL)
1239 return get_buffer_line(buf, (linenr_T)NUM2LONG(num));
1240 return Qnil; /* For stop warning */
1241}
1242
1243static VALUE set_buffer_line(buf_T *buf, linenr_T n, VALUE str)
1244{
Bram Moolenaar165641d2010-02-17 16:23:09 +01001245 char *line = StringValuePtr(str);
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001246 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001247
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001248 if (n > 0 && n <= buf->b_ml.ml_line_count && line != NULL)
1249 {
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001250 /* set curwin/curbuf for "buf" and save some things */
1251 aucmd_prepbuf(&aco, buf);
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001252
Bram Moolenaar758535a2016-03-30 22:06:16 +02001253 if (u_savesub(n) == OK)
1254 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001255 ml_replace(n, (char_u *)line, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001256 changed();
1257#ifdef SYNTAX_HL
1258 syn_changed(n); /* recompute syntax hl. for this line */
1259#endif
1260 }
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001261
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001262 /* restore curwin/curbuf and a few other things */
1263 aucmd_restbuf(&aco);
1264 /* Careful: autocommands may have made "buf" invalid! */
Bram Moolenaarf30e74c2006-08-16 17:35:00 +00001265
Bram Moolenaar071d4272004-06-13 20:20:40 +00001266 update_curbuf(NOT_VALID);
1267 }
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001268 else
1269 {
Bram Moolenaar165641d2010-02-17 16:23:09 +01001270 rb_raise(rb_eIndexError, "line number %ld out of range", (long)n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001271 }
1272 return str;
1273}
1274
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001275static VALUE buffer_aset(VALUE self, VALUE num, VALUE str)
1276{
1277 buf_T *buf = get_buf(self);
1278
1279 if (buf != NULL)
1280 return set_buffer_line(buf, (linenr_T)NUM2LONG(num), str);
1281 return str;
1282}
1283
Bram Moolenaar071d4272004-06-13 20:20:40 +00001284static VALUE buffer_delete(VALUE self, VALUE num)
1285{
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001286 buf_T *buf = get_buf(self);
1287 long n = NUM2LONG(num);
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001288 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001289
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001290 if (n > 0 && n <= buf->b_ml.ml_line_count)
1291 {
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001292 /* set curwin/curbuf for "buf" and save some things */
1293 aucmd_prepbuf(&aco, buf);
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001294
Bram Moolenaar758535a2016-03-30 22:06:16 +02001295 if (u_savedel(n, 1) == OK)
1296 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001297 ml_delete(n, 0);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001298
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001299 /* Changes to non-active buffers should properly refresh
1300 * SegPhault - 01/09/05 */
1301 deleted_lines_mark(n, 1L);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001302
Bram Moolenaar071d4272004-06-13 20:20:40 +00001303 changed();
1304 }
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001305
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001306 /* restore curwin/curbuf and a few other things */
1307 aucmd_restbuf(&aco);
1308 /* Careful: autocommands may have made "buf" invalid! */
Bram Moolenaarf30e74c2006-08-16 17:35:00 +00001309
Bram Moolenaar071d4272004-06-13 20:20:40 +00001310 update_curbuf(NOT_VALID);
1311 }
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001312 else
1313 {
Bram Moolenaar165641d2010-02-17 16:23:09 +01001314 rb_raise(rb_eIndexError, "line number %ld out of range", n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001315 }
1316 return Qnil;
1317}
1318
1319static VALUE buffer_append(VALUE self, VALUE num, VALUE str)
1320{
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001321 buf_T *buf = get_buf(self);
Bram Moolenaar165641d2010-02-17 16:23:09 +01001322 char *line = StringValuePtr(str);
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001323 long n = NUM2LONG(num);
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001324 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001325
Bram Moolenaar3c531602010-11-16 14:46:19 +01001326 if (line == NULL)
1327 {
Bram Moolenaar165641d2010-02-17 16:23:09 +01001328 rb_raise(rb_eIndexError, "NULL line");
1329 }
1330 else if (n >= 0 && n <= buf->b_ml.ml_line_count)
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001331 {
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001332 /* set curwin/curbuf for "buf" and save some things */
1333 aucmd_prepbuf(&aco, buf);
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001334
Bram Moolenaar758535a2016-03-30 22:06:16 +02001335 if (u_inssub(n + 1) == OK)
1336 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001337 ml_append(n, (char_u *) line, (colnr_T) 0, FALSE);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001338
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001339 /* Changes to non-active buffers should properly refresh screen
1340 * SegPhault - 12/20/04 */
1341 appended_lines_mark(n, 1L);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001342
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001343 changed();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001344 }
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001345
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001346 /* restore curwin/curbuf and a few other things */
1347 aucmd_restbuf(&aco);
1348 /* Careful: autocommands may have made "buf" invalid! */
Bram Moolenaarf30e74c2006-08-16 17:35:00 +00001349
Bram Moolenaar071d4272004-06-13 20:20:40 +00001350 update_curbuf(NOT_VALID);
1351 }
Bram Moolenaar3c531602010-11-16 14:46:19 +01001352 else
1353 {
Bram Moolenaar165641d2010-02-17 16:23:09 +01001354 rb_raise(rb_eIndexError, "line number %ld out of range", n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001355 }
1356 return str;
1357}
1358
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001359#ifdef USE_TYPEDDATA
1360static size_t window_dsize(const void *buf);
1361
1362static const rb_data_type_t window_type = {
1363 "vim_window",
1364 {0, 0, window_dsize, {0, 0}},
1365 0, 0,
1366# ifdef RUBY_TYPED_FREE_IMMEDIATELY
1367 0,
1368# endif
1369};
1370
1371static size_t window_dsize(const void *win UNUSED)
1372{
1373 return sizeof(win_T);
1374}
1375#endif
1376
Bram Moolenaar071d4272004-06-13 20:20:40 +00001377static VALUE window_new(win_T *win)
1378{
Bram Moolenaare344bea2005-09-01 20:46:49 +00001379 if (win->w_ruby_ref)
1380 {
1381 return (VALUE) win->w_ruby_ref;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001382 }
Bram Moolenaare344bea2005-09-01 20:46:49 +00001383 else
1384 {
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001385#ifdef USE_TYPEDDATA
1386 VALUE obj = TypedData_Wrap_Struct(cVimWindow, &window_type, win);
1387#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001388 VALUE obj = Data_Wrap_Struct(cVimWindow, 0, 0, win);
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001389#endif
Bram Moolenaare344bea2005-09-01 20:46:49 +00001390 win->w_ruby_ref = (void *) obj;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001391 rb_hash_aset(objtbl, rb_obj_id(obj), obj);
1392 return obj;
1393 }
1394}
1395
1396static win_T *get_win(VALUE obj)
1397{
1398 win_T *win;
1399
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001400#ifdef USE_TYPEDDATA
1401 TypedData_Get_Struct(obj, win_T, &window_type, win);
1402#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001403 Data_Get_Struct(obj, win_T, win);
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001404#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001405 if (win == NULL)
1406 rb_raise(eDeletedWindowError, "attempt to refer to deleted window");
1407 return win;
1408}
1409
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001410static VALUE window_s_current(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001411{
1412 return window_new(curwin);
1413}
1414
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001415/*
1416 * Added line manipulation functions
1417 * SegPhault - 03/07/05
1418 */
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001419static VALUE line_s_current(void)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001420{
1421 return get_buffer_line(curbuf, curwin->w_cursor.lnum);
1422}
1423
Bram Moolenaarcd8b20a2009-05-22 16:20:57 +00001424static VALUE set_current_line(VALUE self UNUSED, VALUE str)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001425{
1426 return set_buffer_line(curbuf, curwin->w_cursor.lnum, str);
1427}
1428
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001429static VALUE current_line_number(void)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001430{
1431 return INT2FIX((int)curwin->w_cursor.lnum);
1432}
1433
1434
1435
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001436static VALUE window_s_count(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001437{
Bram Moolenaar071d4272004-06-13 20:20:40 +00001438 win_T *w;
1439 int n = 0;
1440
Bram Moolenaar29323592016-07-24 22:04:11 +02001441 FOR_ALL_WINDOWS(w)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001442 n++;
1443 return INT2NUM(n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001444}
1445
Bram Moolenaarcd8b20a2009-05-22 16:20:57 +00001446static VALUE window_s_aref(VALUE self UNUSED, VALUE num)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001447{
1448 win_T *w;
1449 int n = NUM2INT(num);
1450
Bram Moolenaar071d4272004-06-13 20:20:40 +00001451 for (w = firstwin; w != NULL; w = w->w_next, --n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001452 if (n == 0)
1453 return window_new(w);
1454 return Qnil;
1455}
1456
1457static VALUE window_buffer(VALUE self)
1458{
1459 win_T *win = get_win(self);
1460
1461 return buffer_new(win->w_buffer);
1462}
1463
1464static VALUE window_height(VALUE self)
1465{
1466 win_T *win = get_win(self);
1467
1468 return INT2NUM(win->w_height);
1469}
1470
1471static VALUE window_set_height(VALUE self, VALUE height)
1472{
1473 win_T *win = get_win(self);
1474 win_T *savewin = curwin;
1475
1476 curwin = win;
1477 win_setheight(NUM2INT(height));
1478 curwin = savewin;
1479 return height;
1480}
1481
Bram Moolenaarfeeaa682013-02-14 22:19:51 +01001482static VALUE window_width(VALUE self UNUSED)
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001483{
Bram Moolenaare745d752017-09-22 16:56:22 +02001484 return INT2NUM(get_win(self)->w_width);
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001485}
1486
Bram Moolenaarfeeaa682013-02-14 22:19:51 +01001487static VALUE window_set_width(VALUE self UNUSED, VALUE width)
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001488{
1489 win_T *win = get_win(self);
1490 win_T *savewin = curwin;
1491
1492 curwin = win;
1493 win_setwidth(NUM2INT(width));
1494 curwin = savewin;
1495 return width;
1496}
1497
Bram Moolenaar071d4272004-06-13 20:20:40 +00001498static VALUE window_cursor(VALUE self)
1499{
1500 win_T *win = get_win(self);
1501
1502 return rb_assoc_new(INT2NUM(win->w_cursor.lnum), INT2NUM(win->w_cursor.col));
1503}
1504
1505static VALUE window_set_cursor(VALUE self, VALUE pos)
1506{
1507 VALUE lnum, col;
1508 win_T *win = get_win(self);
1509
1510 Check_Type(pos, T_ARRAY);
Bram Moolenaar165641d2010-02-17 16:23:09 +01001511 if (RARRAY_LEN(pos) != 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001512 rb_raise(rb_eArgError, "array length must be 2");
Bram Moolenaar165641d2010-02-17 16:23:09 +01001513 lnum = RARRAY_PTR(pos)[0];
1514 col = RARRAY_PTR(pos)[1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001515 win->w_cursor.lnum = NUM2LONG(lnum);
1516 win->w_cursor.col = NUM2UINT(col);
1517 check_cursor(); /* put cursor on an existing line */
1518 update_screen(NOT_VALID);
1519 return Qnil;
1520}
1521
Bram Moolenaar6217cdc2012-04-25 12:28:09 +02001522static VALUE f_nop(VALUE self UNUSED)
Bram Moolenaar35df7d22012-04-20 18:05:47 +02001523{
1524 return Qnil;
1525}
1526
Bram Moolenaarcd8b20a2009-05-22 16:20:57 +00001527static VALUE f_p(int argc, VALUE *argv, VALUE self UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001528{
1529 int i;
1530 VALUE str = rb_str_new("", 0);
1531
Bram Moolenaar758535a2016-03-30 22:06:16 +02001532 for (i = 0; i < argc; i++)
1533 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001534 if (i > 0) rb_str_cat(str, ", ", 2);
1535 rb_str_concat(str, rb_inspect(argv[i]));
1536 }
Bram Moolenaar165641d2010-02-17 16:23:09 +01001537 MSG(RSTRING_PTR(str));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001538 return Qnil;
1539}
1540
1541static void ruby_io_init(void)
1542{
1543#ifndef DYNAMIC_RUBY
1544 RUBYEXTERN VALUE rb_stdout;
1545#endif
1546
1547 rb_stdout = rb_obj_alloc(rb_cObject);
1548 rb_define_singleton_method(rb_stdout, "write", vim_message, 1);
Bram Moolenaar35df7d22012-04-20 18:05:47 +02001549 rb_define_singleton_method(rb_stdout, "flush", f_nop, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001550 rb_define_global_function("p", f_p, -1);
1551}
1552
1553static void ruby_vim_init(void)
1554{
1555 objtbl = rb_hash_new();
1556 rb_global_variable(&objtbl);
1557
Bram Moolenaarf711faf2007-05-10 16:48:19 +00001558 /* The Vim module used to be called "VIM", but "Vim" is better. Make an
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02001559 * alias "VIM" for backwards compatibility. */
Bram Moolenaarf711faf2007-05-10 16:48:19 +00001560 mVIM = rb_define_module("Vim");
1561 rb_define_const(rb_cObject, "VIM", mVIM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001562 rb_define_const(mVIM, "VERSION_MAJOR", INT2NUM(VIM_VERSION_MAJOR));
1563 rb_define_const(mVIM, "VERSION_MINOR", INT2NUM(VIM_VERSION_MINOR));
1564 rb_define_const(mVIM, "VERSION_BUILD", INT2NUM(VIM_VERSION_BUILD));
1565 rb_define_const(mVIM, "VERSION_PATCHLEVEL", INT2NUM(VIM_VERSION_PATCHLEVEL));
1566 rb_define_const(mVIM, "VERSION_SHORT", rb_str_new2(VIM_VERSION_SHORT));
1567 rb_define_const(mVIM, "VERSION_MEDIUM", rb_str_new2(VIM_VERSION_MEDIUM));
1568 rb_define_const(mVIM, "VERSION_LONG", rb_str_new2(VIM_VERSION_LONG));
1569 rb_define_const(mVIM, "VERSION_LONG_DATE", rb_str_new2(VIM_VERSION_LONG_DATE));
1570 rb_define_module_function(mVIM, "message", vim_message, 1);
1571 rb_define_module_function(mVIM, "set_option", vim_set_option, 1);
1572 rb_define_module_function(mVIM, "command", vim_command, 1);
1573 rb_define_module_function(mVIM, "evaluate", vim_evaluate, 1);
1574
1575 eDeletedBufferError = rb_define_class_under(mVIM, "DeletedBufferError",
1576 rb_eStandardError);
1577 eDeletedWindowError = rb_define_class_under(mVIM, "DeletedWindowError",
1578 rb_eStandardError);
1579
1580 cBuffer = rb_define_class_under(mVIM, "Buffer", rb_cObject);
1581 rb_define_singleton_method(cBuffer, "current", buffer_s_current, 0);
1582 rb_define_singleton_method(cBuffer, "count", buffer_s_count, 0);
1583 rb_define_singleton_method(cBuffer, "[]", buffer_s_aref, 1);
1584 rb_define_method(cBuffer, "name", buffer_name, 0);
1585 rb_define_method(cBuffer, "number", buffer_number, 0);
1586 rb_define_method(cBuffer, "count", buffer_count, 0);
1587 rb_define_method(cBuffer, "length", buffer_count, 0);
1588 rb_define_method(cBuffer, "[]", buffer_aref, 1);
1589 rb_define_method(cBuffer, "[]=", buffer_aset, 2);
1590 rb_define_method(cBuffer, "delete", buffer_delete, 1);
1591 rb_define_method(cBuffer, "append", buffer_append, 2);
1592
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001593 /* Added line manipulation functions
1594 * SegPhault - 03/07/05 */
1595 rb_define_method(cBuffer, "line_number", current_line_number, 0);
1596 rb_define_method(cBuffer, "line", line_s_current, 0);
1597 rb_define_method(cBuffer, "line=", set_current_line, 1);
1598
1599
Bram Moolenaar071d4272004-06-13 20:20:40 +00001600 cVimWindow = rb_define_class_under(mVIM, "Window", rb_cObject);
1601 rb_define_singleton_method(cVimWindow, "current", window_s_current, 0);
1602 rb_define_singleton_method(cVimWindow, "count", window_s_count, 0);
1603 rb_define_singleton_method(cVimWindow, "[]", window_s_aref, 1);
1604 rb_define_method(cVimWindow, "buffer", window_buffer, 0);
1605 rb_define_method(cVimWindow, "height", window_height, 0);
1606 rb_define_method(cVimWindow, "height=", window_set_height, 1);
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001607 rb_define_method(cVimWindow, "width", window_width, 0);
1608 rb_define_method(cVimWindow, "width=", window_set_width, 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001609 rb_define_method(cVimWindow, "cursor", window_cursor, 0);
1610 rb_define_method(cVimWindow, "cursor=", window_set_cursor, 1);
1611
1612 rb_define_virtual_variable("$curbuf", buffer_s_current, 0);
1613 rb_define_virtual_variable("$curwin", window_s_current, 0);
1614}
Bram Moolenaar99685e62013-05-11 13:56:18 +02001615
1616void vim_ruby_init(void *stack_start)
1617{
1618 /* should get machine stack start address early in main function */
1619 ruby_stack_start = stack_start;
1620}