blob: 1619205e31a039b7b8dde7eded2f13615ab04327 [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 Moolenaar32d19c12018-09-13 17:26:54 +020014#include "protodef.h"
Bram Moolenaar2d73ff42010-10-27 17:40:59 +020015#ifdef HAVE_CONFIG_H
16# include "auto/config.h"
17#endif
Bram Moolenaar3ca71f12010-10-27 16:49:47 +020018
Bram Moolenaare2793352011-01-17 19:53:27 +010019#include <stdio.h>
20#include <string.h>
21
Bram Moolenaar071d4272004-06-13 20:20:40 +000022#ifdef _WIN32
Bram Moolenaar41a41412020-01-07 21:32:19 +010023# if !defined(DYNAMIC_RUBY) || (RUBY_VERSION < 18)
Bram Moolenaar49c99fc2020-02-11 23:01:39 +010024# define NT
Bram Moolenaar071d4272004-06-13 20:20:40 +000025# endif
26# ifndef DYNAMIC_RUBY
Bram Moolenaar2ab2e862019-12-04 21:24:53 +010027# define IMPORT // For static dll usage __declspec(dllimport)
Bram Moolenaar071d4272004-06-13 20:20:40 +000028# define RUBYEXTERN __declspec(dllimport)
29# endif
30#endif
31#ifndef RUBYEXTERN
32# define RUBYEXTERN extern
33#endif
34
Bram Moolenaar41a41412020-01-07 21:32:19 +010035#if defined(DYNAMIC_RUBY) && RUBY_VERSION >= 24
Bram Moolenaar2016ae52016-06-13 20:08:43 +020036# define USE_RUBY_INTEGER
Bram Moolenaar06469e92016-06-11 22:26:53 +020037#endif
38
Bram Moolenaarf9b5ef82010-09-29 13:02:53 +020039#ifdef DYNAMIC_RUBY
Bram Moolenaar071d4272004-06-13 20:20:40 +000040/*
41 * This is tricky. In ruby.h there is (inline) function rb_class_of()
42 * definition. This function use these variables. But we want function to
43 * use dll_* variables.
44 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000045# define rb_cFalseClass (*dll_rb_cFalseClass)
46# define rb_cFixnum (*dll_rb_cFixnum)
Bram Moolenaar2016ae52016-06-13 20:08:43 +020047# if defined(USE_RUBY_INTEGER)
48# define rb_cInteger (*dll_rb_cInteger)
Bram Moolenaar06469e92016-06-11 22:26:53 +020049# endif
Bram Moolenaar41a41412020-01-07 21:32:19 +010050# if RUBY_VERSION >= 20
Bram Moolenaardb3fbe52013-03-07 15:16:21 +010051# define rb_cFloat (*dll_rb_cFloat)
52# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000053# define rb_cNilClass (*dll_rb_cNilClass)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010054# define rb_cString (*dll_rb_cString)
Bram Moolenaar071d4272004-06-13 20:20:40 +000055# define rb_cSymbol (*dll_rb_cSymbol)
56# define rb_cTrueClass (*dll_rb_cTrueClass)
Bram Moolenaar41a41412020-01-07 21:32:19 +010057# if RUBY_VERSION >= 18
Bram Moolenaar071d4272004-06-13 20:20:40 +000058/*
Bram Moolenaar42d57f02010-03-10 12:47:00 +010059 * On ver 1.8, all Ruby functions are exported with "__declspec(dllimport)"
60 * in ruby.h. But it causes trouble for these variables, because it is
Bram Moolenaar071d4272004-06-13 20:20:40 +000061 * defined in this file. When defined this RUBY_EXPORT it modified to
62 * "extern" and be able to avoid this problem.
63 */
64# define RUBY_EXPORT
65# endif
Bram Moolenaarf9b5ef82010-09-29 13:02:53 +020066
Bram Moolenaar7dca2eb2019-02-18 20:42:50 +010067#endif // ifdef DYNAMIC_RUBY
Bram Moolenaar071d4272004-06-13 20:20:40 +000068
Bram Moolenaar7dca2eb2019-02-18 20:42:50 +010069// suggested by Ariya Mizutani
Bram Moolenaar0b69c732010-02-17 15:11:50 +010070#if (_MSC_VER == 1200)
71# undef _WIN32_WINNT
72#endif
73
Bram Moolenaar41a41412020-01-07 21:32:19 +010074#if defined(DYNAMIC_RUBY) && RUBY_VERSION >= 19
Bram Moolenaar2ab2e862019-12-04 21:24:53 +010075// Ruby 1.9 defines a number of static functions which use rb_num2long and
76// rb_int2big
Bram Moolenaar42d57f02010-03-10 12:47:00 +010077# define rb_num2long rb_num2long_stub
78# define rb_int2big rb_int2big_stub
79#endif
80
Bram Moolenaar41a41412020-01-07 21:32:19 +010081#if defined(DYNAMIC_RUBY) && RUBY_VERSION >= 19 \
Bram Moolenaar498af702014-03-28 21:58:21 +010082 && VIM_SIZEOF_INT < VIM_SIZEOF_LONG
Bram Moolenaar2ab2e862019-12-04 21:24:53 +010083// Ruby 1.9 defines a number of static functions which use rb_fix2int and
84// rb_num2int if VIM_SIZEOF_INT < VIM_SIZEOF_LONG (64bit)
Bram Moolenaar0bcdd6e2013-04-14 16:19:03 +020085# define rb_fix2int rb_fix2int_stub
86# define rb_num2int rb_num2int_stub
87#endif
88
Bram Moolenaar41a41412020-01-07 21:32:19 +010089#if defined(DYNAMIC_RUBY) && RUBY_VERSION == 21
Bram Moolenaar2ab2e862019-12-04 21:24:53 +010090// Ruby 2.1 adds new GC called RGenGC and RARRAY_PTR uses
91// rb_gc_writebarrier_unprotect_promoted if USE_RGENGC
Bram Moolenaar90140742014-11-27 17:44:08 +010092# define rb_gc_writebarrier_unprotect_promoted rb_gc_writebarrier_unprotect_promoted_stub
93#endif
Bram Moolenaar41a41412020-01-07 21:32:19 +010094#if defined(DYNAMIC_RUBY) && RUBY_VERSION >= 22
Bram Moolenaar0c7485f2015-01-14 14:04:10 +010095# define rb_gc_writebarrier_unprotect rb_gc_writebarrier_unprotect_stub
Bram Moolenaar0c7485f2015-01-14 14:04:10 +010096#endif
Bram Moolenaara1a118b2014-02-05 22:41:15 +010097
Bram Moolenaar41a41412020-01-07 21:32:19 +010098#if defined(DYNAMIC_RUBY) && RUBY_VERSION >= 26
Bram Moolenaarf62fc312019-01-08 20:29:32 +010099# define rb_ary_detransient rb_ary_detransient_stub
Bram Moolenaarb09c6842018-12-27 22:11:01 +0100100#endif
101
Bram Moolenaar81ea1df2020-04-11 18:01:41 +0200102// On macOS pre-installed Ruby defines "SIZEOF_TIME_T" as "SIZEOF_LONG" so it
Bram Moolenaar92c098d2020-05-26 20:09:11 +0200103// conflicts with the definition in config.h then causes a macro-redefined
104// warning.
Bram Moolenaar81ea1df2020-04-11 18:01:41 +0200105#ifdef SIZEOF_TIME_T
106# undef SIZEOF_TIME_T
107#endif
108
Bram Moolenaar071d4272004-06-13 20:20:40 +0000109#include <ruby.h>
Bram Moolenaar41a41412020-01-07 21:32:19 +0100110#if RUBY_VERSION >= 19
Bram Moolenaar165641d2010-02-17 16:23:09 +0100111# include <ruby/encoding.h>
112#endif
Bram Moolenaar41a41412020-01-07 21:32:19 +0100113#if RUBY_VERSION <= 18
Bram Moolenaar8dc4c722019-04-14 19:42:13 +0200114# include <st.h> // for ST_STOP and ST_CONTINUE
115#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000116
Bram Moolenaar92c098d2020-05-26 20:09:11 +0200117// See above.
118#ifdef SIZEOF_TIME_T
119# undef SIZEOF_TIME_T
120#endif
121
Bram Moolenaar8dc4c722019-04-14 19:42:13 +0200122#undef off_t // ruby defines off_t as _int64, Mingw uses long
Bram Moolenaar071d4272004-06-13 20:20:40 +0000123#undef EXTERN
124#undef _
125
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100126// T_DATA defined both by Ruby and Mac header files, hack around it...
Bram Moolenaard0573012017-10-28 21:11:06 +0200127#if defined(MACOS_X)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000128# define __OPENTRANSPORT__
129# define __OPENTRANSPORTPROTOCOL__
130# define __OPENTRANSPORTPROVIDERS__
131#endif
132
Bram Moolenaar165641d2010-02-17 16:23:09 +0100133/*
Bram Moolenaar0d27f642015-12-28 22:05:28 +0100134 * The TypedData_XXX macro family can be used since Ruby 1.9.2 but
135 * rb_data_type_t changed in 1.9.3, therefore require at least 2.0.
136 * The old Data_XXX macro family was deprecated on Ruby 2.2.
Bram Moolenaarf2f6d292015-12-28 20:57:10 +0100137 * Use TypedData_XXX if available.
138 */
Bram Moolenaar41a41412020-01-07 21:32:19 +0100139#if defined(TypedData_Wrap_Struct) && (RUBY_VERSION >= 20)
Bram Moolenaarf2f6d292015-12-28 20:57:10 +0100140# define USE_TYPEDDATA 1
141#endif
142
143/*
Bram Moolenaar84a05ac2013-05-06 04:24:17 +0200144 * Backward compatibility for Ruby 1.8 and earlier.
Bram Moolenaar165641d2010-02-17 16:23:09 +0100145 * Ruby 1.9 does not provide STR2CSTR, instead StringValuePtr is provided.
146 * Ruby 1.9 does not provide RXXX(s)->len and RXXX(s)->ptr, instead
147 * RXXX_LEN(s) and RXXX_PTR(s) are provided.
148 */
149#ifndef StringValuePtr
150# define StringValuePtr(s) STR2CSTR(s)
151#endif
152#ifndef RARRAY_LEN
153# define RARRAY_LEN(s) RARRAY(s)->len
154#endif
155#ifndef RARRAY_PTR
156# define RARRAY_PTR(s) RARRAY(s)->ptr
157#endif
158#ifndef RSTRING_LEN
159# define RSTRING_LEN(s) RSTRING(s)->len
160#endif
161#ifndef RSTRING_PTR
162# define RSTRING_PTR(s) RSTRING(s)->ptr
163#endif
164
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100165#ifdef HAVE_DUP
166# undef HAVE_DUP
167#endif
168
Bram Moolenaar071d4272004-06-13 20:20:40 +0000169#include "vim.h"
170#include "version.h"
171
Bram Moolenaar7dca2eb2019-02-18 20:42:50 +0100172#ifdef DYNAMIC_RUBY
173# if !defined(MSWIN) // must come after including vim.h, where it is defined
174# include <dlfcn.h>
175# define HINSTANCE void*
176# define RUBY_PROC void*
177# define load_dll(n) dlopen((n), RTLD_LAZY|RTLD_GLOBAL)
178# define symbol_from_dll dlsym
179# define close_dll dlclose
180# else
181# define RUBY_PROC FARPROC
182# define load_dll vimLoadLib
183# define symbol_from_dll GetProcAddress
184# define close_dll FreeLibrary
185# endif
186#endif
187
Bram Moolenaar071d4272004-06-13 20:20:40 +0000188#if defined(PROTO) && !defined(FEAT_RUBY)
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100189// Define these to be able to generate the function prototypes.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000190# define VALUE int
191# define RUBY_DATA_FUNC int
192#endif
193
194static int ruby_initialized = 0;
Bram Moolenaar99685e62013-05-11 13:56:18 +0200195static void *ruby_stack_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000196static VALUE objtbl;
197
198static VALUE mVIM;
199static VALUE cBuffer;
200static VALUE cVimWindow;
201static VALUE eDeletedBufferError;
202static VALUE eDeletedWindowError;
203
204static int ensure_ruby_initialized(void);
205static void error_print(int);
206static void ruby_io_init(void);
207static void ruby_vim_init(void);
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100208static int ruby_convert_to_vim_value(VALUE val, typval_T *rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000209
Bram Moolenaar41a41412020-01-07 21:32:19 +0100210#if (RUBY_VERSION >= 19) || defined(RUBY_INIT_STACK)
Bram Moolenaar10f3a792013-05-20 12:52:29 +0200211# if defined(__ia64) && !defined(ruby_init_stack)
212# define ruby_init_stack(addr) ruby_init_stack((addr), rb_ia64_bsp())
213# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000214#endif
215
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200216#if defined(DYNAMIC_RUBY) || defined(PROTO)
Bram Moolenaaref269542016-01-19 13:22:12 +0100217# if defined(PROTO) && !defined(HINSTANCE)
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100218# define HINSTANCE int // for generating prototypes
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200219# endif
220
Bram Moolenaar071d4272004-06-13 20:20:40 +0000221/*
222 * Wrapper defines
223 */
Bram Moolenaar8b430b42020-02-22 15:01:00 +0100224// Ruby 2.7 actually expands the following symbols as macro.
225# if RUBY_VERSION >= 27
226# undef rb_define_global_function
227# undef rb_define_method
228# undef rb_define_module_function
229# undef rb_define_singleton_method
230# endif
231
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200232# define rb_assoc_new dll_rb_assoc_new
233# define rb_cObject (*dll_rb_cObject)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100234# define rb_class_new_instance dll_rb_class_new_instance
Bram Moolenaarf2f6d292015-12-28 20:57:10 +0100235# define rb_check_type dll_rb_check_type
236# ifdef USE_TYPEDDATA
237# define rb_check_typeddata dll_rb_check_typeddata
Bram Moolenaar0c7485f2015-01-14 14:04:10 +0100238# endif
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200239# define rb_class_path dll_rb_class_path
Bram Moolenaarf2f6d292015-12-28 20:57:10 +0100240# ifdef USE_TYPEDDATA
Bram Moolenaar41a41412020-01-07 21:32:19 +0100241# if RUBY_VERSION >= 23
Bram Moolenaarf2f6d292015-12-28 20:57:10 +0100242# define rb_data_typed_object_wrap dll_rb_data_typed_object_wrap
243# else
244# define rb_data_typed_object_alloc dll_rb_data_typed_object_alloc
245# endif
246# else
247# define rb_data_object_alloc dll_rb_data_object_alloc
248# endif
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200249# define rb_define_class_under dll_rb_define_class_under
250# define rb_define_const dll_rb_define_const
251# define rb_define_global_function dll_rb_define_global_function
252# define rb_define_method dll_rb_define_method
253# define rb_define_module dll_rb_define_module
254# define rb_define_module_function dll_rb_define_module_function
255# define rb_define_singleton_method dll_rb_define_singleton_method
256# define rb_define_virtual_variable dll_rb_define_virtual_variable
257# define rb_stdout (*dll_rb_stdout)
Bram Moolenaarb6c8cd82018-07-24 05:41:30 +0200258# define rb_stderr (*dll_rb_stderr)
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200259# define rb_eArgError (*dll_rb_eArgError)
260# define rb_eIndexError (*dll_rb_eIndexError)
261# define rb_eRuntimeError (*dll_rb_eRuntimeError)
262# define rb_eStandardError (*dll_rb_eStandardError)
263# define rb_eval_string_protect dll_rb_eval_string_protect
Bram Moolenaar41a41412020-01-07 21:32:19 +0100264# if RUBY_VERSION >= 21
Bram Moolenaarf711cb22018-08-01 18:42:13 +0200265# define rb_funcallv dll_rb_funcallv
266# else
267# define rb_funcall2 dll_rb_funcall2
268# endif
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200269# define rb_global_variable dll_rb_global_variable
270# define rb_hash_aset dll_rb_hash_aset
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100271# define rb_hash_foreach dll_rb_hash_foreach
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200272# define rb_hash_new dll_rb_hash_new
273# define rb_inspect dll_rb_inspect
274# define rb_int2inum dll_rb_int2inum
Bram Moolenaar218beb32018-08-04 17:24:44 +0200275
276// ruby.h may redefine rb_intern to use RUBY_CONST_ID_CACHE(), but that won't
277// work. Not using the cache appears to be the best solution.
278# undef rb_intern
279# define rb_intern dll_rb_intern
280
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100281# if VIM_SIZEOF_INT < VIM_SIZEOF_LONG // 64 bits only
Bram Moolenaar41a41412020-01-07 21:32:19 +0100282# if RUBY_VERSION <= 18
Bram Moolenaar498af702014-03-28 21:58:21 +0100283# define rb_fix2int dll_rb_fix2int
284# define rb_num2int dll_rb_num2int
285# endif
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200286# define rb_num2uint dll_rb_num2uint
287# endif
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100288# define rb_num2dbl dll_rb_num2dbl
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200289# define rb_lastline_get dll_rb_lastline_get
290# define rb_lastline_set dll_rb_lastline_set
Bram Moolenaard0573012017-10-28 21:11:06 +0200291# define rb_protect dll_rb_protect
292# define rb_load dll_rb_load
Bram Moolenaar41a41412020-01-07 21:32:19 +0100293# if RUBY_VERSION <= 18
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200294# define rb_num2long dll_rb_num2long
295# endif
Bram Moolenaar41a41412020-01-07 21:32:19 +0100296# if RUBY_VERSION <= 19
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200297# define rb_num2ulong dll_rb_num2ulong
298# endif
299# define rb_obj_alloc dll_rb_obj_alloc
300# define rb_obj_as_string dll_rb_obj_as_string
301# define rb_obj_id dll_rb_obj_id
302# define rb_raise dll_rb_raise
303# define rb_str_cat dll_rb_str_cat
304# define rb_str_concat dll_rb_str_concat
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100305# undef rb_str_new
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200306# define rb_str_new dll_rb_str_new
307# ifdef rb_str_new2
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100308// Ruby may #define rb_str_new2 to use rb_str_new_cstr.
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200309# define need_rb_str_new_cstr 1
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100310// Ruby's headers #define rb_str_new_cstr to make use of GCC's
311// __builtin_constant_p extension.
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200312# undef rb_str_new_cstr
313# define rb_str_new_cstr dll_rb_str_new_cstr
Bram Moolenaar76a86062013-05-11 17:45:48 +0200314# else
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200315# define rb_str_new2 dll_rb_str_new2
Bram Moolenaar76a86062013-05-11 17:45:48 +0200316# endif
Bram Moolenaar41a41412020-01-07 21:32:19 +0100317# if RUBY_VERSION >= 18
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200318# define rb_string_value dll_rb_string_value
319# define rb_string_value_ptr dll_rb_string_value_ptr
320# define rb_float_new dll_rb_float_new
321# define rb_ary_new dll_rb_ary_new
Bram Moolenaar51e9fbf2018-08-11 14:24:11 +0200322# ifdef rb_ary_new4
Bram Moolenaar49c99fc2020-02-11 23:01:39 +0100323# define RB_ARY_NEW4_MACRO 1
324# undef rb_ary_new4
Bram Moolenaar51e9fbf2018-08-11 14:24:11 +0200325# endif
326# define rb_ary_new4 dll_rb_ary_new4
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200327# define rb_ary_push dll_rb_ary_push
Bram Moolenaar41a41412020-01-07 21:32:19 +0100328# if (RUBY_VERSION >= 19) || defined(RUBY_INIT_STACK)
Bram Moolenaar10f3a792013-05-20 12:52:29 +0200329# ifdef __ia64
330# define rb_ia64_bsp dll_rb_ia64_bsp
331# undef ruby_init_stack
332# define ruby_init_stack(addr) dll_ruby_init_stack((addr), rb_ia64_bsp())
333# else
334# define ruby_init_stack dll_ruby_init_stack
335# endif
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200336# endif
337# else
338# define rb_str2cstr dll_rb_str2cstr
Bram Moolenaar3ca71f12010-10-27 16:49:47 +0200339# endif
Bram Moolenaar41a41412020-01-07 21:32:19 +0100340# if RUBY_VERSION >= 19
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200341# define rb_errinfo dll_rb_errinfo
342# else
343# define ruby_errinfo (*dll_ruby_errinfo)
344# endif
345# define ruby_init dll_ruby_init
346# define ruby_init_loadpath dll_ruby_init_loadpath
Bram Moolenaar4f974752019-02-17 17:44:42 +0100347# ifdef MSWIN
Bram Moolenaar41a41412020-01-07 21:32:19 +0100348# if RUBY_VERSION >= 19
Bram Moolenaar9d5c84a2018-12-19 20:48:46 +0100349# define ruby_sysinit dll_ruby_sysinit
350# else
351# define NtInitialize dll_NtInitialize
352# endif
Bram Moolenaar41a41412020-01-07 21:32:19 +0100353# if RUBY_VERSION >= 18
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200354# define rb_w32_snprintf dll_rb_w32_snprintf
355# endif
356# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000357
Bram Moolenaar41a41412020-01-07 21:32:19 +0100358# if RUBY_VERSION >= 19
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200359# define ruby_script dll_ruby_script
360# define rb_enc_find_index dll_rb_enc_find_index
361# define rb_enc_find dll_rb_enc_find
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100362# undef rb_enc_str_new
363# define rb_enc_str_new dll_rb_enc_str_new
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200364# define rb_sprintf dll_rb_sprintf
365# define rb_require dll_rb_require
Bram Moolenaar9b1067e2015-11-19 19:33:15 +0100366# define ruby_options dll_ruby_options
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200367# endif
Bram Moolenaar165641d2010-02-17 16:23:09 +0100368
Bram Moolenaar071d4272004-06-13 20:20:40 +0000369/*
370 * Pointers for dynamic link
371 */
372static VALUE (*dll_rb_assoc_new) (VALUE, VALUE);
Bram Moolenaarba52cde2010-06-25 04:29:11 +0200373VALUE *dll_rb_cFalseClass;
374VALUE *dll_rb_cFixnum;
Bram Moolenaar2016ae52016-06-13 20:08:43 +0200375# if defined(USE_RUBY_INTEGER)
Bram Moolenaar06469e92016-06-11 22:26:53 +0200376VALUE *dll_rb_cInteger;
377# endif
Bram Moolenaar41a41412020-01-07 21:32:19 +0100378# if RUBY_VERSION >= 20
Bram Moolenaardb3fbe52013-03-07 15:16:21 +0100379VALUE *dll_rb_cFloat;
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200380# endif
Bram Moolenaarba52cde2010-06-25 04:29:11 +0200381VALUE *dll_rb_cNilClass;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000382static VALUE *dll_rb_cObject;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100383VALUE *dll_rb_cString;
Bram Moolenaarba52cde2010-06-25 04:29:11 +0200384VALUE *dll_rb_cSymbol;
385VALUE *dll_rb_cTrueClass;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100386static VALUE (*dll_rb_class_new_instance) (int,VALUE*,VALUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000387static void (*dll_rb_check_type) (VALUE,int);
Bram Moolenaarf2f6d292015-12-28 20:57:10 +0100388# ifdef USE_TYPEDDATA
389static void *(*dll_rb_check_typeddata) (VALUE,const rb_data_type_t *);
390# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000391static VALUE (*dll_rb_class_path) (VALUE);
Bram Moolenaarf2f6d292015-12-28 20:57:10 +0100392# ifdef USE_TYPEDDATA
Bram Moolenaar41a41412020-01-07 21:32:19 +0100393# if RUBY_VERSION >= 23
Bram Moolenaarf2f6d292015-12-28 20:57:10 +0100394static VALUE (*dll_rb_data_typed_object_wrap) (VALUE, void*, const rb_data_type_t *);
395# else
396static VALUE (*dll_rb_data_typed_object_alloc) (VALUE, void*, const rb_data_type_t *);
397# endif
398# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000399static VALUE (*dll_rb_data_object_alloc) (VALUE, void*, RUBY_DATA_FUNC, RUBY_DATA_FUNC);
Bram Moolenaarf2f6d292015-12-28 20:57:10 +0100400# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000401static VALUE (*dll_rb_define_class_under) (VALUE, const char*, VALUE);
402static void (*dll_rb_define_const) (VALUE,const char*,VALUE);
403static void (*dll_rb_define_global_function) (const char*,VALUE(*)(),int);
404static void (*dll_rb_define_method) (VALUE,const char*,VALUE(*)(),int);
405static VALUE (*dll_rb_define_module) (const char*);
406static void (*dll_rb_define_module_function) (VALUE,const char*,VALUE(*)(),int);
407static void (*dll_rb_define_singleton_method) (VALUE,const char*,VALUE(*)(),int);
408static void (*dll_rb_define_virtual_variable) (const char*,VALUE(*)(),void(*)());
409static VALUE *dll_rb_stdout;
Bram Moolenaarb6c8cd82018-07-24 05:41:30 +0200410static VALUE *dll_rb_stderr;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000411static VALUE *dll_rb_eArgError;
412static VALUE *dll_rb_eIndexError;
413static VALUE *dll_rb_eRuntimeError;
414static VALUE *dll_rb_eStandardError;
415static VALUE (*dll_rb_eval_string_protect) (const char*, int*);
Bram Moolenaar41a41412020-01-07 21:32:19 +0100416# if RUBY_VERSION >= 21
Bram Moolenaarf711cb22018-08-01 18:42:13 +0200417static VALUE (*dll_rb_funcallv) (VALUE, ID, int, const VALUE*);
418# else
419static VALUE (*dll_rb_funcall2) (VALUE, ID, int, const VALUE*);
420# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000421static void (*dll_rb_global_variable) (VALUE*);
422static VALUE (*dll_rb_hash_aset) (VALUE, VALUE, VALUE);
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100423static VALUE (*dll_rb_hash_foreach) (VALUE, int (*)(VALUE, VALUE, VALUE), VALUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000424static VALUE (*dll_rb_hash_new) (void);
425static VALUE (*dll_rb_inspect) (VALUE);
426static VALUE (*dll_rb_int2inum) (long);
Bram Moolenaarf711cb22018-08-01 18:42:13 +0200427static ID (*dll_rb_intern) (const char*);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100428# if VIM_SIZEOF_INT < VIM_SIZEOF_LONG // 64 bits only
Bram Moolenaar2623b4f2012-09-18 16:36:32 +0200429static long (*dll_rb_fix2int) (VALUE);
430static long (*dll_rb_num2int) (VALUE);
431static unsigned long (*dll_rb_num2uint) (VALUE);
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200432# endif
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100433static double (*dll_rb_num2dbl) (VALUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000434static VALUE (*dll_rb_lastline_get) (void);
435static void (*dll_rb_lastline_set) (VALUE);
Bram Moolenaar37badc82018-01-31 20:15:30 +0100436static VALUE (*dll_rb_protect) (VALUE (*)(VALUE), VALUE, int*);
Bram Moolenaard0573012017-10-28 21:11:06 +0200437static void (*dll_rb_load) (VALUE, int);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000438static long (*dll_rb_num2long) (VALUE);
439static unsigned long (*dll_rb_num2ulong) (VALUE);
440static VALUE (*dll_rb_obj_alloc) (VALUE);
441static VALUE (*dll_rb_obj_as_string) (VALUE);
442static VALUE (*dll_rb_obj_id) (VALUE);
443static void (*dll_rb_raise) (VALUE, const char*, ...);
Bram Moolenaar41a41412020-01-07 21:32:19 +0100444# if RUBY_VERSION >= 18
Bram Moolenaarf9b5ef82010-09-29 13:02:53 +0200445static VALUE (*dll_rb_string_value) (volatile VALUE*);
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200446# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000447static char *(*dll_rb_str2cstr) (VALUE,int*);
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200448# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000449static VALUE (*dll_rb_str_cat) (VALUE, const char*, long);
450static VALUE (*dll_rb_str_concat) (VALUE, VALUE);
451static VALUE (*dll_rb_str_new) (const char*, long);
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200452# ifdef need_rb_str_new_cstr
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100453// Ruby may #define rb_str_new2 to use rb_str_new_cstr.
Bram Moolenaar1d2beae2010-05-22 21:56:55 +0200454static VALUE (*dll_rb_str_new_cstr) (const char*);
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200455# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000456static VALUE (*dll_rb_str_new2) (const char*);
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200457# endif
Bram Moolenaar41a41412020-01-07 21:32:19 +0100458# if RUBY_VERSION >= 19
Bram Moolenaar165641d2010-02-17 16:23:09 +0100459static VALUE (*dll_rb_errinfo) (void);
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200460# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000461static VALUE *dll_ruby_errinfo;
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200462# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000463static void (*dll_ruby_init) (void);
464static void (*dll_ruby_init_loadpath) (void);
Bram Moolenaar4f974752019-02-17 17:44:42 +0100465# ifdef MSWIN
Bram Moolenaar41a41412020-01-07 21:32:19 +0100466# if RUBY_VERSION >= 19
Bram Moolenaar4f391792017-01-15 16:59:07 +0100467static void (*dll_ruby_sysinit) (int*, char***);
Bram Moolenaar9d5c84a2018-12-19 20:48:46 +0100468# else
469static void (*dll_NtInitialize) (int*, char***);
470# endif
Bram Moolenaar41a41412020-01-07 21:32:19 +0100471# if RUBY_VERSION >= 18
Bram Moolenaar3ca71f12010-10-27 16:49:47 +0200472static int (*dll_rb_w32_snprintf)(char*, size_t, const char*, ...);
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200473# endif
Bram Moolenaar3ca71f12010-10-27 16:49:47 +0200474# endif
Bram Moolenaar41a41412020-01-07 21:32:19 +0100475# if RUBY_VERSION >= 18
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100476static char * (*dll_rb_string_value_ptr) (volatile VALUE*);
477static VALUE (*dll_rb_float_new) (double);
478static VALUE (*dll_rb_ary_new) (void);
Bram Moolenaar51e9fbf2018-08-11 14:24:11 +0200479static VALUE (*dll_rb_ary_new4) (long n, const VALUE *elts);
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100480static VALUE (*dll_rb_ary_push) (VALUE, VALUE);
Bram Moolenaar41a41412020-01-07 21:32:19 +0100481# if RUBY_VERSION >= 26
Bram Moolenaarb09c6842018-12-27 22:11:01 +0100482static void (*dll_rb_ary_detransient) (VALUE);
483# endif
Bram Moolenaar41a41412020-01-07 21:32:19 +0100484# if (RUBY_VERSION >= 19) || defined(RUBY_INIT_STACK)
Bram Moolenaar10f3a792013-05-20 12:52:29 +0200485# ifdef __ia64
Bram Moolenaar76a86062013-05-11 17:45:48 +0200486static void * (*dll_rb_ia64_bsp) (void);
487static void (*dll_ruby_init_stack)(VALUE*, void*);
Bram Moolenaar10f3a792013-05-20 12:52:29 +0200488# else
Bram Moolenaar76a86062013-05-11 17:45:48 +0200489static void (*dll_ruby_init_stack)(VALUE*);
Bram Moolenaar10f3a792013-05-20 12:52:29 +0200490# endif
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200491# endif
Bram Moolenaar76a86062013-05-11 17:45:48 +0200492# endif
Bram Moolenaar41a41412020-01-07 21:32:19 +0100493# if RUBY_VERSION >= 19
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100494static VALUE (*dll_rb_int2big)(SIGNED_VALUE);
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200495# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000496
Bram Moolenaar41a41412020-01-07 21:32:19 +0100497# if RUBY_VERSION >= 19
Bram Moolenaar165641d2010-02-17 16:23:09 +0100498static void (*dll_ruby_script) (const char*);
499static int (*dll_rb_enc_find_index) (const char*);
500static rb_encoding* (*dll_rb_enc_find) (const char*);
501static VALUE (*dll_rb_enc_str_new) (const char*, long, rb_encoding*);
502static VALUE (*dll_rb_sprintf) (const char*, ...);
Bram Moolenaar7a8ef142010-12-24 13:39:35 +0100503static VALUE (*dll_rb_require) (const char*);
Bram Moolenaar9b1067e2015-11-19 19:33:15 +0100504static void* (*ruby_options)(int, char**);
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200505# endif
Bram Moolenaar165641d2010-02-17 16:23:09 +0100506
Bram Moolenaara1a118b2014-02-05 22:41:15 +0100507# if defined(USE_RGENGC) && USE_RGENGC
Bram Moolenaar41a41412020-01-07 21:32:19 +0100508# if RUBY_VERSION == 21
Bram Moolenaara1a118b2014-02-05 22:41:15 +0100509static void (*dll_rb_gc_writebarrier_unprotect_promoted)(VALUE);
Bram Moolenaar0c7485f2015-01-14 14:04:10 +0100510# else
511static void (*dll_rb_gc_writebarrier_unprotect)(VALUE obj);
512# endif
Bram Moolenaara1a118b2014-02-05 22:41:15 +0100513# endif
514
Bram Moolenaar41a41412020-01-07 21:32:19 +0100515# if (RUBY_VERSION >= 19) && !defined(PROTO)
516# if RUBY_VERSION >= 22
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100517 long
518rb_num2long_stub(VALUE x)
Bram Moolenaarbbc1a592015-04-21 15:25:31 +0200519# else
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100520 SIGNED_VALUE
521rb_num2long_stub(VALUE x)
Bram Moolenaarbbc1a592015-04-21 15:25:31 +0200522# endif
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100523{
524 return dll_rb_num2long(x);
525}
Bram Moolenaar41a41412020-01-07 21:32:19 +0100526# if RUBY_VERSION >= 26
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100527 VALUE
528rb_int2big_stub(intptr_t x)
Bram Moolenaar63d1fea2019-02-03 15:18:35 +0100529# else
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100530 VALUE
531rb_int2big_stub(SIGNED_VALUE x)
Bram Moolenaar63d1fea2019-02-03 15:18:35 +0100532# endif
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100533{
534 return dll_rb_int2big(x);
535}
Bram Moolenaar41a41412020-01-07 21:32:19 +0100536# if (RUBY_VERSION >= 19) && (VIM_SIZEOF_INT < VIM_SIZEOF_LONG)
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100537 long
538rb_fix2int_stub(VALUE x)
Bram Moolenaar0bcdd6e2013-04-14 16:19:03 +0200539{
540 return dll_rb_fix2int(x);
541}
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100542 long
543rb_num2int_stub(VALUE x)
Bram Moolenaar0bcdd6e2013-04-14 16:19:03 +0200544{
545 return dll_rb_num2int(x);
546}
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200547# endif
Bram Moolenaar41a41412020-01-07 21:32:19 +0100548# if RUBY_VERSION >= 20
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100549 VALUE
Bram Moolenaar886ed692013-02-26 13:41:35 +0100550rb_float_new_in_heap(double d)
551{
552 return dll_rb_float_new(d);
553}
Bram Moolenaar41a41412020-01-07 21:32:19 +0100554# if RUBY_VERSION >= 22
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100555 unsigned long
556rb_num2ulong(VALUE x)
Bram Moolenaarbbc1a592015-04-21 15:25:31 +0200557# else
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100558 VALUE
559rb_num2ulong(VALUE x)
Bram Moolenaarbbc1a592015-04-21 15:25:31 +0200560# endif
Bram Moolenaar886ed692013-02-26 13:41:35 +0100561{
562 return (long)RSHIFT((SIGNED_VALUE)(x),1);
563}
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200564# endif
565# endif
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100566
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100567 // Do not generate a prototype here, VALUE isn't always defined.
Bram Moolenaar3e9a1612014-11-12 16:05:04 +0100568# if defined(USE_RGENGC) && USE_RGENGC && !defined(PROTO)
Bram Moolenaar41a41412020-01-07 21:32:19 +0100569# if RUBY_VERSION == 21
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100570 void
571rb_gc_writebarrier_unprotect_promoted_stub(VALUE obj)
Bram Moolenaara1a118b2014-02-05 22:41:15 +0100572{
Bram Moolenaar90140742014-11-27 17:44:08 +0100573 dll_rb_gc_writebarrier_unprotect_promoted(obj);
Bram Moolenaara1a118b2014-02-05 22:41:15 +0100574}
Bram Moolenaar0c7485f2015-01-14 14:04:10 +0100575# else
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100576 void
577rb_gc_writebarrier_unprotect_stub(VALUE obj)
Bram Moolenaar0c7485f2015-01-14 14:04:10 +0100578{
579 dll_rb_gc_writebarrier_unprotect(obj);
580}
581# endif
582# endif
583
Bram Moolenaar41a41412020-01-07 21:32:19 +0100584# if RUBY_VERSION >= 26
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100585 void
586rb_ary_detransient_stub(VALUE x)
Bram Moolenaarf62fc312019-01-08 20:29:32 +0100587{
588 dll_rb_ary_detransient(x);
589}
590# endif
591
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100592static HINSTANCE hinstRuby = NULL; // Instance of ruby.dll
Bram Moolenaar071d4272004-06-13 20:20:40 +0000593
594/*
Bram Moolenaarda2303d2005-08-30 21:55:26 +0000595 * Table of name to function pointer of ruby.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000596 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000597static struct
598{
599 char *name;
600 RUBY_PROC *ptr;
601} ruby_funcname_table[] =
602{
603 {"rb_assoc_new", (RUBY_PROC*)&dll_rb_assoc_new},
604 {"rb_cFalseClass", (RUBY_PROC*)&dll_rb_cFalseClass},
Bram Moolenaar2016ae52016-06-13 20:08:43 +0200605# if defined(USE_RUBY_INTEGER)
Bram Moolenaar06469e92016-06-11 22:26:53 +0200606 {"rb_cInteger", (RUBY_PROC*)&dll_rb_cInteger},
Bram Moolenaar6abda992017-01-09 21:10:31 +0100607# else
608 {"rb_cFixnum", (RUBY_PROC*)&dll_rb_cFixnum},
Bram Moolenaar06469e92016-06-11 22:26:53 +0200609# endif
Bram Moolenaar41a41412020-01-07 21:32:19 +0100610# if RUBY_VERSION >= 20
Bram Moolenaardb3fbe52013-03-07 15:16:21 +0100611 {"rb_cFloat", (RUBY_PROC*)&dll_rb_cFloat},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200612# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000613 {"rb_cNilClass", (RUBY_PROC*)&dll_rb_cNilClass},
614 {"rb_cObject", (RUBY_PROC*)&dll_rb_cObject},
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100615 {"rb_cString", (RUBY_PROC*)&dll_rb_cString},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000616 {"rb_cSymbol", (RUBY_PROC*)&dll_rb_cSymbol},
617 {"rb_cTrueClass", (RUBY_PROC*)&dll_rb_cTrueClass},
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100618 {"rb_class_new_instance", (RUBY_PROC*)&dll_rb_class_new_instance},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000619 {"rb_check_type", (RUBY_PROC*)&dll_rb_check_type},
Bram Moolenaarf2f6d292015-12-28 20:57:10 +0100620# ifdef USE_TYPEDDATA
621 {"rb_check_typeddata", (RUBY_PROC*)&dll_rb_check_typeddata},
622# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000623 {"rb_class_path", (RUBY_PROC*)&dll_rb_class_path},
Bram Moolenaarf2f6d292015-12-28 20:57:10 +0100624# ifdef USE_TYPEDDATA
Bram Moolenaar41a41412020-01-07 21:32:19 +0100625# if RUBY_VERSION >= 23
Bram Moolenaarf2f6d292015-12-28 20:57:10 +0100626 {"rb_data_typed_object_wrap", (RUBY_PROC*)&dll_rb_data_typed_object_wrap},
627# else
628 {"rb_data_typed_object_alloc", (RUBY_PROC*)&dll_rb_data_typed_object_alloc},
629# endif
630# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000631 {"rb_data_object_alloc", (RUBY_PROC*)&dll_rb_data_object_alloc},
Bram Moolenaarf2f6d292015-12-28 20:57:10 +0100632# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000633 {"rb_define_class_under", (RUBY_PROC*)&dll_rb_define_class_under},
634 {"rb_define_const", (RUBY_PROC*)&dll_rb_define_const},
635 {"rb_define_global_function", (RUBY_PROC*)&dll_rb_define_global_function},
636 {"rb_define_method", (RUBY_PROC*)&dll_rb_define_method},
637 {"rb_define_module", (RUBY_PROC*)&dll_rb_define_module},
638 {"rb_define_module_function", (RUBY_PROC*)&dll_rb_define_module_function},
639 {"rb_define_singleton_method", (RUBY_PROC*)&dll_rb_define_singleton_method},
640 {"rb_define_virtual_variable", (RUBY_PROC*)&dll_rb_define_virtual_variable},
641 {"rb_stdout", (RUBY_PROC*)&dll_rb_stdout},
Bram Moolenaarb6c8cd82018-07-24 05:41:30 +0200642 {"rb_stderr", (RUBY_PROC*)&dll_rb_stderr},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000643 {"rb_eArgError", (RUBY_PROC*)&dll_rb_eArgError},
644 {"rb_eIndexError", (RUBY_PROC*)&dll_rb_eIndexError},
645 {"rb_eRuntimeError", (RUBY_PROC*)&dll_rb_eRuntimeError},
646 {"rb_eStandardError", (RUBY_PROC*)&dll_rb_eStandardError},
647 {"rb_eval_string_protect", (RUBY_PROC*)&dll_rb_eval_string_protect},
Bram Moolenaar41a41412020-01-07 21:32:19 +0100648# if RUBY_VERSION >= 21
Bram Moolenaarf711cb22018-08-01 18:42:13 +0200649 {"rb_funcallv", (RUBY_PROC*)&dll_rb_funcallv},
650# else
651 {"rb_funcall2", (RUBY_PROC*)&dll_rb_funcall2},
652# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000653 {"rb_global_variable", (RUBY_PROC*)&dll_rb_global_variable},
654 {"rb_hash_aset", (RUBY_PROC*)&dll_rb_hash_aset},
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100655 {"rb_hash_foreach", (RUBY_PROC*)&dll_rb_hash_foreach},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000656 {"rb_hash_new", (RUBY_PROC*)&dll_rb_hash_new},
657 {"rb_inspect", (RUBY_PROC*)&dll_rb_inspect},
658 {"rb_int2inum", (RUBY_PROC*)&dll_rb_int2inum},
Bram Moolenaarf711cb22018-08-01 18:42:13 +0200659 {"rb_intern", (RUBY_PROC*)&dll_rb_intern},
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100660# if VIM_SIZEOF_INT < VIM_SIZEOF_LONG // 64 bits only
Bram Moolenaar2623b4f2012-09-18 16:36:32 +0200661 {"rb_fix2int", (RUBY_PROC*)&dll_rb_fix2int},
662 {"rb_num2int", (RUBY_PROC*)&dll_rb_num2int},
663 {"rb_num2uint", (RUBY_PROC*)&dll_rb_num2uint},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200664# endif
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100665 {"rb_num2dbl", (RUBY_PROC*)&dll_rb_num2dbl},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000666 {"rb_lastline_get", (RUBY_PROC*)&dll_rb_lastline_get},
667 {"rb_lastline_set", (RUBY_PROC*)&dll_rb_lastline_set},
Bram Moolenaard0573012017-10-28 21:11:06 +0200668 {"rb_protect", (RUBY_PROC*)&dll_rb_protect},
669 {"rb_load", (RUBY_PROC*)&dll_rb_load},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000670 {"rb_num2long", (RUBY_PROC*)&dll_rb_num2long},
671 {"rb_num2ulong", (RUBY_PROC*)&dll_rb_num2ulong},
672 {"rb_obj_alloc", (RUBY_PROC*)&dll_rb_obj_alloc},
673 {"rb_obj_as_string", (RUBY_PROC*)&dll_rb_obj_as_string},
674 {"rb_obj_id", (RUBY_PROC*)&dll_rb_obj_id},
675 {"rb_raise", (RUBY_PROC*)&dll_rb_raise},
Bram Moolenaar41a41412020-01-07 21:32:19 +0100676# if RUBY_VERSION >= 18
Bram Moolenaarf9b5ef82010-09-29 13:02:53 +0200677 {"rb_string_value", (RUBY_PROC*)&dll_rb_string_value},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200678# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000679 {"rb_str2cstr", (RUBY_PROC*)&dll_rb_str2cstr},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200680# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000681 {"rb_str_cat", (RUBY_PROC*)&dll_rb_str_cat},
682 {"rb_str_concat", (RUBY_PROC*)&dll_rb_str_concat},
683 {"rb_str_new", (RUBY_PROC*)&dll_rb_str_new},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200684# ifdef need_rb_str_new_cstr
Bram Moolenaar1d2beae2010-05-22 21:56:55 +0200685 {"rb_str_new_cstr", (RUBY_PROC*)&dll_rb_str_new_cstr},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200686# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000687 {"rb_str_new2", (RUBY_PROC*)&dll_rb_str_new2},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200688# endif
Bram Moolenaar41a41412020-01-07 21:32:19 +0100689# if RUBY_VERSION >= 19
Bram Moolenaar165641d2010-02-17 16:23:09 +0100690 {"rb_errinfo", (RUBY_PROC*)&dll_rb_errinfo},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200691# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000692 {"ruby_errinfo", (RUBY_PROC*)&dll_ruby_errinfo},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200693# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000694 {"ruby_init", (RUBY_PROC*)&dll_ruby_init},
695 {"ruby_init_loadpath", (RUBY_PROC*)&dll_ruby_init_loadpath},
Bram Moolenaar4f974752019-02-17 17:44:42 +0100696# ifdef MSWIN
Bram Moolenaar41a41412020-01-07 21:32:19 +0100697# if RUBY_VERSION >= 19
Bram Moolenaar4f391792017-01-15 16:59:07 +0100698 {"ruby_sysinit", (RUBY_PROC*)&dll_ruby_sysinit},
Bram Moolenaar9d5c84a2018-12-19 20:48:46 +0100699# else
700 {"NtInitialize", (RUBY_PROC*)&dll_NtInitialize},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200701# endif
Bram Moolenaar41a41412020-01-07 21:32:19 +0100702# if RUBY_VERSION >= 18
Bram Moolenaar071d4272004-06-13 20:20:40 +0000703 {"rb_w32_snprintf", (RUBY_PROC*)&dll_rb_w32_snprintf},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200704# endif
Bram Moolenaar3ca71f12010-10-27 16:49:47 +0200705# endif
Bram Moolenaar41a41412020-01-07 21:32:19 +0100706# if RUBY_VERSION >= 18
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100707 {"rb_string_value_ptr", (RUBY_PROC*)&dll_rb_string_value_ptr},
Bram Moolenaar41a41412020-01-07 21:32:19 +0100708# if RUBY_VERSION <= 19
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100709 {"rb_float_new", (RUBY_PROC*)&dll_rb_float_new},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200710# else
Bram Moolenaar886ed692013-02-26 13:41:35 +0100711 {"rb_float_new_in_heap", (RUBY_PROC*)&dll_rb_float_new},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200712# endif
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100713 {"rb_ary_new", (RUBY_PROC*)&dll_rb_ary_new},
Bram Moolenaar51e9fbf2018-08-11 14:24:11 +0200714# ifdef RB_ARY_NEW4_MACRO
715 {"rb_ary_new_from_values", (RUBY_PROC*)&dll_rb_ary_new4},
716# else
717 {"rb_ary_new4", (RUBY_PROC*)&dll_rb_ary_new4},
718# endif
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100719 {"rb_ary_push", (RUBY_PROC*)&dll_rb_ary_push},
Bram Moolenaar41a41412020-01-07 21:32:19 +0100720# if RUBY_VERSION >= 26
Bram Moolenaarb09c6842018-12-27 22:11:01 +0100721 {"rb_ary_detransient", (RUBY_PROC*)&dll_rb_ary_detransient},
722# endif
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200723# endif
Bram Moolenaar41a41412020-01-07 21:32:19 +0100724# if RUBY_VERSION >= 19
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100725 {"rb_int2big", (RUBY_PROC*)&dll_rb_int2big},
Bram Moolenaar165641d2010-02-17 16:23:09 +0100726 {"ruby_script", (RUBY_PROC*)&dll_ruby_script},
727 {"rb_enc_find_index", (RUBY_PROC*)&dll_rb_enc_find_index},
728 {"rb_enc_find", (RUBY_PROC*)&dll_rb_enc_find},
729 {"rb_enc_str_new", (RUBY_PROC*)&dll_rb_enc_str_new},
730 {"rb_sprintf", (RUBY_PROC*)&dll_rb_sprintf},
Bram Moolenaar7a8ef142010-12-24 13:39:35 +0100731 {"rb_require", (RUBY_PROC*)&dll_rb_require},
Bram Moolenaar9b1067e2015-11-19 19:33:15 +0100732 {"ruby_options", (RUBY_PROC*)&dll_ruby_options},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200733# endif
Bram Moolenaar41a41412020-01-07 21:32:19 +0100734# if (RUBY_VERSION >= 19) || defined(RUBY_INIT_STACK)
Bram Moolenaar10f3a792013-05-20 12:52:29 +0200735# ifdef __ia64
736 {"rb_ia64_bsp", (RUBY_PROC*)&dll_rb_ia64_bsp},
737# endif
738 {"ruby_init_stack", (RUBY_PROC*)&dll_ruby_init_stack},
739# endif
Bram Moolenaara1a118b2014-02-05 22:41:15 +0100740# if defined(USE_RGENGC) && USE_RGENGC
Bram Moolenaar41a41412020-01-07 21:32:19 +0100741# if RUBY_VERSION == 21
Bram Moolenaara1a118b2014-02-05 22:41:15 +0100742 {"rb_gc_writebarrier_unprotect_promoted", (RUBY_PROC*)&dll_rb_gc_writebarrier_unprotect_promoted},
Bram Moolenaar0c7485f2015-01-14 14:04:10 +0100743# else
744 {"rb_gc_writebarrier_unprotect", (RUBY_PROC*)&dll_rb_gc_writebarrier_unprotect},
745# endif
Bram Moolenaara1a118b2014-02-05 22:41:15 +0100746# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000747 {"", NULL},
748};
749
750/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000751 * Load library and get all pointers.
752 * Parameter 'libname' provides name of DLL.
753 * Return OK or FAIL.
754 */
755 static int
756ruby_runtime_link_init(char *libname, int verbose)
757{
758 int i;
759
760 if (hinstRuby)
761 return OK;
Bram Moolenaarf9b5ef82010-09-29 13:02:53 +0200762 hinstRuby = load_dll(libname);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000763 if (!hinstRuby)
764 {
765 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100766 semsg(_(e_loadlib), libname);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000767 return FAIL;
768 }
769
770 for (i = 0; ruby_funcname_table[i].ptr; ++i)
771 {
Bram Moolenaarf9b5ef82010-09-29 13:02:53 +0200772 if (!(*ruby_funcname_table[i].ptr = symbol_from_dll(hinstRuby,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000773 ruby_funcname_table[i].name)))
774 {
Bram Moolenaarf9b5ef82010-09-29 13:02:53 +0200775 close_dll(hinstRuby);
Bram Moolenaar3ca71f12010-10-27 16:49:47 +0200776 hinstRuby = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000777 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100778 semsg(_(e_loadfunc), ruby_funcname_table[i].name);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000779 return FAIL;
780 }
781 }
782 return OK;
783}
784
785/*
786 * If ruby is enabled (there is installed ruby on Windows system) return TRUE,
787 * else FALSE.
788 */
789 int
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100790ruby_enabled(int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000791{
Bram Moolenaar25e4fcd2016-01-09 14:57:47 +0100792 return ruby_runtime_link_init((char *)p_rubydll, verbose) == OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000793}
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100794#endif // defined(DYNAMIC_RUBY) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000795
796 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100797ruby_end(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000798{
Bram Moolenaar071d4272004-06-13 20:20:40 +0000799}
800
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100801 void
802ex_ruby(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000803{
804 int state;
805 char *script = NULL;
806
Bram Moolenaar35a2e192006-03-13 22:07:11 +0000807 script = (char *)script_get(eap, eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000808 if (!eap->skip && ensure_ruby_initialized())
809 {
810 if (script == NULL)
811 rb_eval_string_protect((char *)eap->arg, &state);
812 else
813 rb_eval_string_protect(script, &state);
814 if (state)
815 error_print(state);
816 }
817 vim_free(script);
818}
819
Bram Moolenaar165641d2010-02-17 16:23:09 +0100820/*
821 * In Ruby 1.9 or later, ruby String object has encoding.
822 * conversion buffer string of vim to ruby String object using
823 * VIM encoding option.
824 */
825 static VALUE
826vim_str2rb_enc_str(const char *s)
827{
Bram Moolenaar41a41412020-01-07 21:32:19 +0100828#if RUBY_VERSION >= 19
Bram Moolenaar165641d2010-02-17 16:23:09 +0100829 int isnum;
830 long lval;
831 char_u *sval;
832 rb_encoding *enc;
833
834 isnum = get_option_value((char_u *)"enc", &lval, &sval, 0);
835 if (isnum == 0)
836 {
837 enc = rb_enc_find((char *)sval);
838 vim_free(sval);
Bram Moolenaar758535a2016-03-30 22:06:16 +0200839 if (enc)
Bram Moolenaar9b0ac222016-06-01 20:31:43 +0200840 return rb_enc_str_new(s, (long)strlen(s), enc);
Bram Moolenaar165641d2010-02-17 16:23:09 +0100841 }
842#endif
843 return rb_str_new2(s);
844}
845
846 static VALUE
847eval_enc_string_protect(const char *str, int *state)
848{
Bram Moolenaar41a41412020-01-07 21:32:19 +0100849#if RUBY_VERSION >= 19
Bram Moolenaar165641d2010-02-17 16:23:09 +0100850 int isnum;
851 long lval;
852 char_u *sval;
853 rb_encoding *enc;
854 VALUE v;
855
856 isnum = get_option_value((char_u *)"enc", &lval, &sval, 0);
857 if (isnum == 0)
858 {
859 enc = rb_enc_find((char *)sval);
860 vim_free(sval);
861 if (enc)
862 {
863 v = rb_sprintf("#-*- coding:%s -*-\n%s", rb_enc_name(enc), str);
864 return rb_eval_string_protect(StringValuePtr(v), state);
865 }
866 }
867#endif
868 return rb_eval_string_protect(str, state);
869}
870
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100871 void
872ex_rubydo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000873{
874 int state;
875 linenr_T i;
Bram Moolenaarc593fee2017-01-29 23:11:25 +0100876 buf_T *was_curbuf = curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000877
878 if (ensure_ruby_initialized())
879 {
880 if (u_save(eap->line1 - 1, eap->line2 + 1) != OK)
881 return;
Bram Moolenaar758535a2016-03-30 22:06:16 +0200882 for (i = eap->line1; i <= eap->line2; i++)
883 {
Bram Moolenaare980d8a2010-12-08 13:11:21 +0100884 VALUE line;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000885
Bram Moolenaarc593fee2017-01-29 23:11:25 +0100886 if (i > curbuf->b_ml.ml_line_count)
887 break;
Bram Moolenaare980d8a2010-12-08 13:11:21 +0100888 line = vim_str2rb_enc_str((char *)ml_get(i));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000889 rb_lastline_set(line);
Bram Moolenaar165641d2010-02-17 16:23:09 +0100890 eval_enc_string_protect((char *) eap->arg, &state);
Bram Moolenaar758535a2016-03-30 22:06:16 +0200891 if (state)
892 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000893 error_print(state);
894 break;
895 }
Bram Moolenaarc593fee2017-01-29 23:11:25 +0100896 if (was_curbuf != curbuf)
897 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000898 line = rb_lastline_get();
Bram Moolenaar758535a2016-03-30 22:06:16 +0200899 if (!NIL_P(line))
900 {
901 if (TYPE(line) != T_STRING)
902 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100903 emsg(_("E265: $_ must be an instance of String"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000904 return;
905 }
Bram Moolenaar165641d2010-02-17 16:23:09 +0100906 ml_replace(i, (char_u *) StringValuePtr(line), 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000907 changed();
908#ifdef SYNTAX_HL
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100909 syn_changed(i); // recompute syntax hl. for this line
Bram Moolenaar071d4272004-06-13 20:20:40 +0000910#endif
911 }
912 }
913 check_cursor();
914 update_curbuf(NOT_VALID);
915 }
916}
917
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100918 static VALUE
919rb_load_wrap(VALUE file_to_load)
Bram Moolenaar37badc82018-01-31 20:15:30 +0100920{
921 rb_load(file_to_load, 0);
922 return Qnil;
923}
924
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100925 void
926ex_rubyfile(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000927{
928 int state;
929
930 if (ensure_ruby_initialized())
931 {
Bram Moolenaar37badc82018-01-31 20:15:30 +0100932 VALUE file_to_load = rb_str_new2((const char *)eap->arg);
933 rb_protect(rb_load_wrap, file_to_load, &state);
934 if (state)
935 error_print(state);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000936 }
937}
938
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100939 void
940ruby_buffer_free(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000941{
Bram Moolenaare344bea2005-09-01 20:46:49 +0000942 if (buf->b_ruby_ref)
943 {
944 rb_hash_aset(objtbl, rb_obj_id((VALUE) buf->b_ruby_ref), Qnil);
945 RDATA(buf->b_ruby_ref)->data = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000946 }
947}
948
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100949 void
950ruby_window_free(win_T *win)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000951{
Bram Moolenaare344bea2005-09-01 20:46:49 +0000952 if (win->w_ruby_ref)
953 {
954 rb_hash_aset(objtbl, rb_obj_id((VALUE) win->w_ruby_ref), Qnil);
955 RDATA(win->w_ruby_ref)->data = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000956 }
957}
958
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100959 static int
960ensure_ruby_initialized(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000961{
962 if (!ruby_initialized)
963 {
964#ifdef DYNAMIC_RUBY
965 if (ruby_enabled(TRUE))
966 {
967#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +0100968#ifdef MSWIN
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100969 // suggested by Ariya Mizutani
Bram Moolenaar0b69c732010-02-17 15:11:50 +0100970 int argc = 1;
971 char *argv[] = {"gvim.exe"};
Bram Moolenaar90140742014-11-27 17:44:08 +0100972 char **argvp = argv;
Bram Moolenaar41a41412020-01-07 21:32:19 +0100973# if RUBY_VERSION >= 19
Bram Moolenaarfe6ce332017-01-14 20:12:01 +0100974 ruby_sysinit(&argc, &argvp);
975# else
Bram Moolenaar90140742014-11-27 17:44:08 +0100976 NtInitialize(&argc, &argvp);
Bram Moolenaarfe6ce332017-01-14 20:12:01 +0100977# endif
Bram Moolenaar0b69c732010-02-17 15:11:50 +0100978#endif
Bram Moolenaarb2c03502010-07-02 20:20:09 +0200979 {
Bram Moolenaar41a41412020-01-07 21:32:19 +0100980#if (RUBY_VERSION >= 19) || defined(RUBY_INIT_STACK)
Bram Moolenaar99685e62013-05-11 13:56:18 +0200981 ruby_init_stack(ruby_stack_start);
Bram Moolenaar165641d2010-02-17 16:23:09 +0100982#endif
Bram Moolenaarb2c03502010-07-02 20:20:09 +0200983 ruby_init();
984 }
Bram Moolenaar41a41412020-01-07 21:32:19 +0100985#if RUBY_VERSION >= 19
Bram Moolenaar7a8ef142010-12-24 13:39:35 +0100986 {
987 int dummy_argc = 2;
Bram Moolenaard1bc96ce2017-09-26 21:21:44 +0200988 char *dummy_argv[] = {"vim-ruby", "-e_=0"};
Bram Moolenaar9b1067e2015-11-19 19:33:15 +0100989 ruby_options(dummy_argc, dummy_argv);
Bram Moolenaar7a8ef142010-12-24 13:39:35 +0100990 }
Bram Moolenaar165641d2010-02-17 16:23:09 +0100991 ruby_script("vim-ruby");
Bram Moolenaar7a8ef142010-12-24 13:39:35 +0100992#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000993 ruby_init_loadpath();
Bram Moolenaar165641d2010-02-17 16:23:09 +0100994#endif
Bram Moolenaar7a8ef142010-12-24 13:39:35 +0100995 ruby_io_init();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000996 ruby_vim_init();
997 ruby_initialized = 1;
998#ifdef DYNAMIC_RUBY
999 }
1000 else
1001 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001002 emsg(_("E266: Sorry, this command is disabled, the Ruby library could not be loaded."));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001003 return 0;
1004 }
1005#endif
1006 }
1007 return ruby_initialized;
1008}
1009
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001010 static void
1011error_print(int state)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001012{
Bram Moolenaar41a41412020-01-07 21:32:19 +01001013#if !defined(DYNAMIC_RUBY) && (RUBY_VERSION <= 18)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001014 RUBYEXTERN VALUE ruby_errinfo;
1015#endif
Bram Moolenaarf711cb22018-08-01 18:42:13 +02001016 VALUE error;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001017 VALUE eclass;
1018 VALUE einfo;
Bram Moolenaarf711cb22018-08-01 18:42:13 +02001019 VALUE bt;
1020 int attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001021 char buff[BUFSIZ];
Bram Moolenaarf711cb22018-08-01 18:42:13 +02001022 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001023
1024#define TAG_RETURN 0x1
1025#define TAG_BREAK 0x2
1026#define TAG_NEXT 0x3
1027#define TAG_RETRY 0x4
1028#define TAG_REDO 0x5
1029#define TAG_RAISE 0x6
1030#define TAG_THROW 0x7
1031#define TAG_FATAL 0x8
1032#define TAG_MASK 0xf
1033
Bram Moolenaar758535a2016-03-30 22:06:16 +02001034 switch (state)
1035 {
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001036 case TAG_RETURN:
1037 emsg(_("E267: unexpected return"));
1038 break;
1039 case TAG_NEXT:
1040 emsg(_("E268: unexpected next"));
1041 break;
1042 case TAG_BREAK:
1043 emsg(_("E269: unexpected break"));
1044 break;
1045 case TAG_REDO:
1046 emsg(_("E270: unexpected redo"));
1047 break;
1048 case TAG_RETRY:
1049 emsg(_("E271: retry outside of rescue clause"));
1050 break;
1051 case TAG_RAISE:
1052 case TAG_FATAL:
Bram Moolenaar41a41412020-01-07 21:32:19 +01001053#if RUBY_VERSION >= 19
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001054 error = rb_errinfo();
Bram Moolenaar165641d2010-02-17 16:23:09 +01001055#else
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001056 error = ruby_errinfo;
Bram Moolenaar165641d2010-02-17 16:23:09 +01001057#endif
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001058 eclass = CLASS_OF(error);
1059 einfo = rb_obj_as_string(error);
1060 if (eclass == rb_eRuntimeError && RSTRING_LEN(einfo) == 0)
1061 {
1062 emsg(_("E272: unhandled exception"));
1063 }
1064 else
1065 {
1066 VALUE epath;
1067 char *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001068
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001069 epath = rb_class_path(eclass);
1070 vim_snprintf(buff, BUFSIZ, "%s: %s",
1071 RSTRING_PTR(epath), RSTRING_PTR(einfo));
1072 p = strchr(buff, '\n');
1073 if (p) *p = '\0';
1074 emsg(buff);
1075 }
Bram Moolenaarf711cb22018-08-01 18:42:13 +02001076
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001077 attr = syn_name2attr((char_u *)"Error");
Bram Moolenaar49c99fc2020-02-11 23:01:39 +01001078#if RUBY_VERSION >= 21
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001079 bt = rb_funcallv(error, rb_intern("backtrace"), 0, 0);
1080 for (i = 0; i < RARRAY_LEN(bt); i++)
1081 msg_attr(RSTRING_PTR(RARRAY_AREF(bt, i)), attr);
Bram Moolenaar49c99fc2020-02-11 23:01:39 +01001082#else
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001083 bt = rb_funcall2(error, rb_intern("backtrace"), 0, 0);
1084 for (i = 0; i < RARRAY_LEN(bt); i++)
1085 msg_attr(RSTRING_PTR(RARRAY_PTR(bt)[i]), attr);
Bram Moolenaar49c99fc2020-02-11 23:01:39 +01001086#endif
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001087 break;
1088 default:
1089 vim_snprintf(buff, BUFSIZ, _("E273: unknown longjmp status %d"), state);
1090 emsg(buff);
1091 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001092 }
1093}
1094
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001095 static VALUE
1096vim_message(VALUE self UNUSED, VALUE str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001097{
1098 char *buff, *p;
1099
1100 str = rb_obj_as_string(str);
Bram Moolenaar3f5f7952011-08-04 19:34:59 +02001101 if (RSTRING_LEN(str) > 0)
1102 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001103 // Only do this when the string isn't empty, alloc(0) causes trouble.
Bram Moolenaar00ccf542017-09-03 15:17:48 +02001104 buff = ALLOCA_N(char, RSTRING_LEN(str) + 1);
Bram Moolenaar3f5f7952011-08-04 19:34:59 +02001105 strcpy(buff, RSTRING_PTR(str));
1106 p = strchr(buff, '\n');
1107 if (p) *p = '\0';
Bram Moolenaar32526b32019-01-19 17:43:09 +01001108 msg(buff);
Bram Moolenaar3f5f7952011-08-04 19:34:59 +02001109 }
1110 else
1111 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01001112 msg("");
Bram Moolenaar3f5f7952011-08-04 19:34:59 +02001113 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001114 return Qnil;
1115}
1116
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001117 static VALUE
1118vim_set_option(VALUE self UNUSED, VALUE str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001119{
Bram Moolenaar165641d2010-02-17 16:23:09 +01001120 do_set((char_u *)StringValuePtr(str), 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001121 update_screen(NOT_VALID);
1122 return Qnil;
1123}
1124
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001125 static VALUE
1126vim_command(VALUE self UNUSED, VALUE str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001127{
Bram Moolenaar165641d2010-02-17 16:23:09 +01001128 do_cmdline_cmd((char_u *)StringValuePtr(str));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001129 return Qnil;
1130}
1131
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001132#ifdef FEAT_EVAL
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001133 static VALUE
1134vim_to_ruby(typval_T *tv)
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001135{
1136 VALUE result = Qnil;
1137
1138 if (tv->v_type == VAR_STRING)
1139 {
Bram Moolenaar94127e42010-03-19 23:08:48 +01001140 result = rb_str_new2(tv->vval.v_string == NULL
1141 ? "" : (char *)(tv->vval.v_string));
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001142 }
1143 else if (tv->v_type == VAR_NUMBER)
1144 {
Bram Moolenaar639a2552010-03-17 18:15:23 +01001145 result = INT2NUM(tv->vval.v_number);
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001146 }
1147# ifdef FEAT_FLOAT
1148 else if (tv->v_type == VAR_FLOAT)
1149 {
Bram Moolenaar639a2552010-03-17 18:15:23 +01001150 result = rb_float_new(tv->vval.v_float);
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001151 }
1152# endif
1153 else if (tv->v_type == VAR_LIST)
1154 {
Bram Moolenaar639a2552010-03-17 18:15:23 +01001155 list_T *list = tv->vval.v_list;
1156 listitem_T *curr;
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001157
Bram Moolenaar639a2552010-03-17 18:15:23 +01001158 result = rb_ary_new();
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001159
Bram Moolenaar639a2552010-03-17 18:15:23 +01001160 if (list != NULL)
1161 {
Bram Moolenaaraeea7212020-04-02 18:50:46 +02001162 FOR_ALL_LIST_ITEMS(list, curr)
Bram Moolenaar639a2552010-03-17 18:15:23 +01001163 rb_ary_push(result, vim_to_ruby(&curr->li_tv));
Bram Moolenaar639a2552010-03-17 18:15:23 +01001164 }
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001165 }
1166 else if (tv->v_type == VAR_DICT)
1167 {
Bram Moolenaar639a2552010-03-17 18:15:23 +01001168 result = rb_hash_new();
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001169
Bram Moolenaar639a2552010-03-17 18:15:23 +01001170 if (tv->vval.v_dict != NULL)
1171 {
1172 hashtab_T *ht = &tv->vval.v_dict->dv_hashtab;
1173 long_u todo = ht->ht_used;
1174 hashitem_T *hi;
1175 dictitem_T *di;
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001176
Bram Moolenaar639a2552010-03-17 18:15:23 +01001177 for (hi = ht->ht_array; todo > 0; ++hi)
1178 {
1179 if (!HASHITEM_EMPTY(hi))
1180 {
1181 --todo;
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001182
Bram Moolenaar639a2552010-03-17 18:15:23 +01001183 di = dict_lookup(hi);
1184 rb_hash_aset(result, rb_str_new2((char *)hi->hi_key),
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001185 vim_to_ruby(&di->di_tv));
Bram Moolenaar639a2552010-03-17 18:15:23 +01001186 }
1187 }
1188 }
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001189 }
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01001190 else if (tv->v_type == VAR_BOOL || tv->v_type == VAR_SPECIAL)
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001191 {
Bram Moolenaard84b26a2018-07-28 17:18:09 +02001192 if (tv->vval.v_number == VVAL_TRUE)
1193 result = Qtrue;
1194 else if (tv->vval.v_number == VVAL_FALSE)
1195 result = Qfalse;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001196 }
1197 else if (tv->v_type == VAR_BLOB)
1198 {
1199 result = rb_str_new(tv->vval.v_blob->bv_ga.ga_data,
1200 tv->vval.v_blob->bv_ga.ga_len);
1201 }
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001202 // else return Qnil;
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001203
1204 return result;
1205}
1206#endif
1207
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001208 static VALUE
1209vim_evaluate(VALUE self UNUSED, VALUE str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001210{
1211#ifdef FEAT_EVAL
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001212 typval_T *tv;
1213 VALUE result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001214
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001215 tv = eval_expr((char_u *)StringValuePtr(str), NULL);
1216 if (tv == NULL)
Bram Moolenaar639a2552010-03-17 18:15:23 +01001217 return Qnil;
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001218 result = vim_to_ruby(tv);
1219
1220 free_tv(tv);
1221
1222 return result;
1223#else
1224 return Qnil;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001225#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001226}
1227
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001228#ifdef USE_TYPEDDATA
1229static size_t buffer_dsize(const void *buf);
1230
1231static const rb_data_type_t buffer_type = {
1232 "vim_buffer",
Bram Moolenaar41a41412020-01-07 21:32:19 +01001233 {0, 0, buffer_dsize,
1234# if RUBY_VERSION >= 27
Bram Moolenaar8b430b42020-02-22 15:01:00 +01001235 0, {0}
Bram Moolenaar41a41412020-01-07 21:32:19 +01001236# else
1237 {0, 0}
1238# endif
1239 },
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001240 0, 0,
1241# ifdef RUBY_TYPED_FREE_IMMEDIATELY
1242 0,
1243# endif
1244};
1245
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001246 static size_t
1247buffer_dsize(const void *buf UNUSED)
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001248{
1249 return sizeof(buf_T);
1250}
1251#endif
1252
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001253 static VALUE
1254buffer_new(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001255{
Bram Moolenaare344bea2005-09-01 20:46:49 +00001256 if (buf->b_ruby_ref)
1257 {
1258 return (VALUE) buf->b_ruby_ref;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001259 }
Bram Moolenaare344bea2005-09-01 20:46:49 +00001260 else
1261 {
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001262#ifdef USE_TYPEDDATA
1263 VALUE obj = TypedData_Wrap_Struct(cBuffer, &buffer_type, buf);
1264#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001265 VALUE obj = Data_Wrap_Struct(cBuffer, 0, 0, buf);
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001266#endif
Bram Moolenaare344bea2005-09-01 20:46:49 +00001267 buf->b_ruby_ref = (void *) obj;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001268 rb_hash_aset(objtbl, rb_obj_id(obj), obj);
1269 return obj;
1270 }
1271}
1272
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001273 static buf_T *
1274get_buf(VALUE obj)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001275{
1276 buf_T *buf;
1277
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001278#ifdef USE_TYPEDDATA
1279 TypedData_Get_Struct(obj, buf_T, &buffer_type, buf);
1280#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001281 Data_Get_Struct(obj, buf_T, buf);
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001282#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001283 if (buf == NULL)
1284 rb_raise(eDeletedBufferError, "attempt to refer to deleted buffer");
1285 return buf;
1286}
1287
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001288 static VALUE
1289vim_blob(VALUE self UNUSED, VALUE str)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001290{
1291 VALUE result = rb_str_new("0z", 2);
1292 char buf[4];
1293 int i;
1294 for (i = 0; i < RSTRING_LEN(str); i++)
1295 {
Bram Moolenaar0d13cce2019-02-23 14:23:03 +01001296 sprintf(buf, "%02X", (unsigned char)(RSTRING_PTR(str)[i]));
Bram Moolenaarb191be22019-01-30 21:00:12 +01001297 rb_str_concat(result, rb_str_new2(buf));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001298 }
1299 return result;
1300}
1301
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001302 static VALUE
Bram Moolenaard5a986f2020-12-06 21:11:31 +01001303buffer_s_current(VALUE self UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001304{
1305 return buffer_new(curbuf);
1306}
1307
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001308 static VALUE
Bram Moolenaard5a986f2020-12-06 21:11:31 +01001309buffer_s_current_getter(ID id UNUSED, VALUE *x UNUSED)
1310{
1311 return buffer_new(curbuf);
1312}
1313
1314 static VALUE
1315buffer_s_count(VALUE self UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001316{
1317 buf_T *b;
1318 int n = 0;
1319
Bram Moolenaar29323592016-07-24 22:04:11 +02001320 FOR_ALL_BUFFERS(b)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001321 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001322 // Deleted buffers should not be counted
1323 // SegPhault - 01/07/05
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001324 if (b->b_p_bl)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001325 n++;
1326 }
1327
Bram Moolenaar071d4272004-06-13 20:20:40 +00001328 return INT2NUM(n);
1329}
1330
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001331 static VALUE
1332buffer_s_aref(VALUE self UNUSED, VALUE num)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001333{
1334 buf_T *b;
1335 int n = NUM2INT(num);
1336
Bram Moolenaar29323592016-07-24 22:04:11 +02001337 FOR_ALL_BUFFERS(b)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001338 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001339 // Deleted buffers should not be counted
1340 // SegPhault - 01/07/05
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001341 if (!b->b_p_bl)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001342 continue;
1343
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001344 if (n == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001345 return buffer_new(b);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001346
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001347 n--;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001348 }
1349 return Qnil;
1350}
1351
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001352 static VALUE
1353buffer_name(VALUE self)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001354{
1355 buf_T *buf = get_buf(self);
1356
Bram Moolenaar35a2e192006-03-13 22:07:11 +00001357 return buf->b_ffname ? rb_str_new2((char *)buf->b_ffname) : Qnil;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001358}
1359
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001360 static VALUE
1361buffer_number(VALUE self)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001362{
1363 buf_T *buf = get_buf(self);
1364
1365 return INT2NUM(buf->b_fnum);
1366}
1367
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001368 static VALUE
1369buffer_count(VALUE self)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001370{
1371 buf_T *buf = get_buf(self);
1372
1373 return INT2NUM(buf->b_ml.ml_line_count);
1374}
1375
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001376 static VALUE
1377get_buffer_line(buf_T *buf, linenr_T n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001378{
Bram Moolenaar3c531602010-11-16 14:46:19 +01001379 if (n <= 0 || n > buf->b_ml.ml_line_count)
1380 rb_raise(rb_eIndexError, "line number %ld out of range", (long)n);
1381 return vim_str2rb_enc_str((char *)ml_get_buf(buf, n, FALSE));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001382}
1383
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001384 static VALUE
1385buffer_aref(VALUE self, VALUE num)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001386{
1387 buf_T *buf = get_buf(self);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001388
1389 if (buf != NULL)
1390 return get_buffer_line(buf, (linenr_T)NUM2LONG(num));
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001391 return Qnil; // For stop warning
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001392}
1393
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001394 static VALUE
1395set_buffer_line(buf_T *buf, linenr_T n, VALUE str)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001396{
Bram Moolenaar165641d2010-02-17 16:23:09 +01001397 char *line = StringValuePtr(str);
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001398 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001399
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001400 if (n > 0 && n <= buf->b_ml.ml_line_count && line != NULL)
1401 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001402 // set curwin/curbuf for "buf" and save some things
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001403 aucmd_prepbuf(&aco, buf);
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001404
Bram Moolenaar758535a2016-03-30 22:06:16 +02001405 if (u_savesub(n) == OK)
1406 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001407 ml_replace(n, (char_u *)line, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001408 changed();
1409#ifdef SYNTAX_HL
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001410 syn_changed(n); // recompute syntax hl. for this line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001411#endif
1412 }
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001413
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001414 // restore curwin/curbuf and a few other things
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001415 aucmd_restbuf(&aco);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001416 // Careful: autocommands may have made "buf" invalid!
Bram Moolenaarf30e74c2006-08-16 17:35:00 +00001417
Bram Moolenaar071d4272004-06-13 20:20:40 +00001418 update_curbuf(NOT_VALID);
1419 }
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001420 else
1421 {
Bram Moolenaar165641d2010-02-17 16:23:09 +01001422 rb_raise(rb_eIndexError, "line number %ld out of range", (long)n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001423 }
1424 return str;
1425}
1426
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001427 static VALUE
1428buffer_aset(VALUE self, VALUE num, VALUE str)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001429{
1430 buf_T *buf = get_buf(self);
1431
1432 if (buf != NULL)
1433 return set_buffer_line(buf, (linenr_T)NUM2LONG(num), str);
1434 return str;
1435}
1436
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001437 static VALUE
1438buffer_delete(VALUE self, VALUE num)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001439{
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001440 buf_T *buf = get_buf(self);
1441 long n = NUM2LONG(num);
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001442 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001443
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001444 if (n > 0 && n <= buf->b_ml.ml_line_count)
1445 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001446 // set curwin/curbuf for "buf" and save some things
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001447 aucmd_prepbuf(&aco, buf);
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001448
Bram Moolenaar758535a2016-03-30 22:06:16 +02001449 if (u_savedel(n, 1) == OK)
1450 {
Bram Moolenaarca70c072020-05-30 20:30:46 +02001451 ml_delete(n);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001452
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001453 // Changes to non-active buffers should properly refresh
1454 // SegPhault - 01/09/05
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001455 deleted_lines_mark(n, 1L);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001456
Bram Moolenaar071d4272004-06-13 20:20:40 +00001457 changed();
1458 }
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001459
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001460 // restore curwin/curbuf and a few other things
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001461 aucmd_restbuf(&aco);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001462 // Careful: autocommands may have made "buf" invalid!
Bram Moolenaarf30e74c2006-08-16 17:35:00 +00001463
Bram Moolenaar071d4272004-06-13 20:20:40 +00001464 update_curbuf(NOT_VALID);
1465 }
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001466 else
1467 {
Bram Moolenaar165641d2010-02-17 16:23:09 +01001468 rb_raise(rb_eIndexError, "line number %ld out of range", n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001469 }
1470 return Qnil;
1471}
1472
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001473 static VALUE
1474buffer_append(VALUE self, VALUE num, VALUE str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001475{
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001476 buf_T *buf = get_buf(self);
Bram Moolenaar165641d2010-02-17 16:23:09 +01001477 char *line = StringValuePtr(str);
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001478 long n = NUM2LONG(num);
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001479 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001480
Bram Moolenaar3c531602010-11-16 14:46:19 +01001481 if (line == NULL)
1482 {
Bram Moolenaar165641d2010-02-17 16:23:09 +01001483 rb_raise(rb_eIndexError, "NULL line");
1484 }
1485 else if (n >= 0 && n <= buf->b_ml.ml_line_count)
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001486 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001487 // set curwin/curbuf for "buf" and save some things
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001488 aucmd_prepbuf(&aco, buf);
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001489
Bram Moolenaar758535a2016-03-30 22:06:16 +02001490 if (u_inssub(n + 1) == OK)
1491 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001492 ml_append(n, (char_u *) line, (colnr_T) 0, FALSE);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001493
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001494 // Changes to non-active buffers should properly refresh screen
1495 // SegPhault - 12/20/04
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001496 appended_lines_mark(n, 1L);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001497
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001498 changed();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001499 }
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001500
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001501 // restore curwin/curbuf and a few other things
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001502 aucmd_restbuf(&aco);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001503 // Careful: autocommands may have made "buf" invalid!
Bram Moolenaarf30e74c2006-08-16 17:35:00 +00001504
Bram Moolenaar071d4272004-06-13 20:20:40 +00001505 update_curbuf(NOT_VALID);
1506 }
Bram Moolenaar3c531602010-11-16 14:46:19 +01001507 else
1508 {
Bram Moolenaar165641d2010-02-17 16:23:09 +01001509 rb_raise(rb_eIndexError, "line number %ld out of range", n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001510 }
1511 return str;
1512}
1513
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001514#ifdef USE_TYPEDDATA
1515static size_t window_dsize(const void *buf);
1516
1517static const rb_data_type_t window_type = {
1518 "vim_window",
Bram Moolenaar41a41412020-01-07 21:32:19 +01001519 {0, 0, window_dsize,
1520# if RUBY_VERSION >= 27
Bram Moolenaar8b430b42020-02-22 15:01:00 +01001521 0, {0}
Bram Moolenaar41a41412020-01-07 21:32:19 +01001522# else
1523 {0, 0}
1524# endif
1525 },
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001526 0, 0,
1527# ifdef RUBY_TYPED_FREE_IMMEDIATELY
1528 0,
1529# endif
1530};
1531
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001532 static size_t
1533window_dsize(const void *win UNUSED)
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001534{
1535 return sizeof(win_T);
1536}
1537#endif
1538
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001539 static VALUE
1540window_new(win_T *win)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001541{
Bram Moolenaare344bea2005-09-01 20:46:49 +00001542 if (win->w_ruby_ref)
1543 {
1544 return (VALUE) win->w_ruby_ref;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001545 }
Bram Moolenaare344bea2005-09-01 20:46:49 +00001546 else
1547 {
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001548#ifdef USE_TYPEDDATA
1549 VALUE obj = TypedData_Wrap_Struct(cVimWindow, &window_type, win);
1550#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001551 VALUE obj = Data_Wrap_Struct(cVimWindow, 0, 0, win);
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001552#endif
Bram Moolenaare344bea2005-09-01 20:46:49 +00001553 win->w_ruby_ref = (void *) obj;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001554 rb_hash_aset(objtbl, rb_obj_id(obj), obj);
1555 return obj;
1556 }
1557}
1558
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001559 static win_T *
1560get_win(VALUE obj)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001561{
1562 win_T *win;
1563
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001564#ifdef USE_TYPEDDATA
1565 TypedData_Get_Struct(obj, win_T, &window_type, win);
1566#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001567 Data_Get_Struct(obj, win_T, win);
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001568#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001569 if (win == NULL)
1570 rb_raise(eDeletedWindowError, "attempt to refer to deleted window");
1571 return win;
1572}
1573
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001574 static VALUE
Bram Moolenaard5a986f2020-12-06 21:11:31 +01001575window_s_current(VALUE self UNUSED)
1576{
1577 return window_new(curwin);
1578}
1579
1580 static VALUE
1581window_s_current_getter(ID id UNUSED, VALUE *x UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001582{
1583 return window_new(curwin);
1584}
1585
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001586/*
1587 * Added line manipulation functions
1588 * SegPhault - 03/07/05
1589 */
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001590 static VALUE
Bram Moolenaard5a986f2020-12-06 21:11:31 +01001591line_s_current(VALUE self UNUSED)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001592{
1593 return get_buffer_line(curbuf, curwin->w_cursor.lnum);
1594}
1595
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001596 static VALUE
1597set_current_line(VALUE self UNUSED, VALUE str)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001598{
1599 return set_buffer_line(curbuf, curwin->w_cursor.lnum, str);
1600}
1601
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001602 static VALUE
Bram Moolenaard5a986f2020-12-06 21:11:31 +01001603current_line_number(VALUE self UNUSED)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001604{
1605 return INT2FIX((int)curwin->w_cursor.lnum);
1606}
1607
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001608 static VALUE
Bram Moolenaard5a986f2020-12-06 21:11:31 +01001609window_s_count(VALUE self UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001610{
Bram Moolenaar071d4272004-06-13 20:20:40 +00001611 win_T *w;
1612 int n = 0;
1613
Bram Moolenaar29323592016-07-24 22:04:11 +02001614 FOR_ALL_WINDOWS(w)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001615 n++;
1616 return INT2NUM(n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001617}
1618
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001619 static VALUE
1620window_s_aref(VALUE self UNUSED, VALUE num)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001621{
1622 win_T *w;
1623 int n = NUM2INT(num);
1624
Bram Moolenaar071d4272004-06-13 20:20:40 +00001625 for (w = firstwin; w != NULL; w = w->w_next, --n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001626 if (n == 0)
1627 return window_new(w);
1628 return Qnil;
1629}
1630
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001631 static VALUE
1632window_buffer(VALUE self)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001633{
1634 win_T *win = get_win(self);
1635
1636 return buffer_new(win->w_buffer);
1637}
1638
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001639 static VALUE
1640window_height(VALUE self)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001641{
1642 win_T *win = get_win(self);
1643
1644 return INT2NUM(win->w_height);
1645}
1646
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001647 static VALUE
1648window_set_height(VALUE self, VALUE height)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001649{
1650 win_T *win = get_win(self);
1651 win_T *savewin = curwin;
1652
1653 curwin = win;
1654 win_setheight(NUM2INT(height));
1655 curwin = savewin;
1656 return height;
1657}
1658
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001659 static VALUE
1660window_width(VALUE self UNUSED)
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001661{
Bram Moolenaare745d752017-09-22 16:56:22 +02001662 return INT2NUM(get_win(self)->w_width);
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001663}
1664
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001665 static VALUE
1666window_set_width(VALUE self UNUSED, VALUE width)
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001667{
1668 win_T *win = get_win(self);
1669 win_T *savewin = curwin;
1670
1671 curwin = win;
1672 win_setwidth(NUM2INT(width));
1673 curwin = savewin;
1674 return width;
1675}
1676
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001677 static VALUE
1678window_cursor(VALUE self)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001679{
1680 win_T *win = get_win(self);
1681
1682 return rb_assoc_new(INT2NUM(win->w_cursor.lnum), INT2NUM(win->w_cursor.col));
1683}
1684
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001685 static VALUE
1686window_set_cursor(VALUE self, VALUE pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001687{
1688 VALUE lnum, col;
1689 win_T *win = get_win(self);
1690
1691 Check_Type(pos, T_ARRAY);
Bram Moolenaar165641d2010-02-17 16:23:09 +01001692 if (RARRAY_LEN(pos) != 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001693 rb_raise(rb_eArgError, "array length must be 2");
Bram Moolenaar165641d2010-02-17 16:23:09 +01001694 lnum = RARRAY_PTR(pos)[0];
1695 col = RARRAY_PTR(pos)[1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001696 win->w_cursor.lnum = NUM2LONG(lnum);
1697 win->w_cursor.col = NUM2UINT(col);
Bram Moolenaar53901442018-07-25 22:02:36 +02001698 win->w_set_curswant = TRUE;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001699 check_cursor(); // put cursor on an existing line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001700 update_screen(NOT_VALID);
1701 return Qnil;
1702}
1703
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001704 static VALUE
1705f_nop(VALUE self UNUSED)
Bram Moolenaar35df7d22012-04-20 18:05:47 +02001706{
1707 return Qnil;
1708}
1709
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001710 static VALUE
1711f_p(int argc, VALUE *argv, VALUE self UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001712{
1713 int i;
1714 VALUE str = rb_str_new("", 0);
Bram Moolenaar51e9fbf2018-08-11 14:24:11 +02001715 VALUE ret = Qnil;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001716
Bram Moolenaar758535a2016-03-30 22:06:16 +02001717 for (i = 0; i < argc; i++)
1718 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001719 if (i > 0) rb_str_cat(str, ", ", 2);
1720 rb_str_concat(str, rb_inspect(argv[i]));
1721 }
Bram Moolenaar32526b32019-01-19 17:43:09 +01001722 msg(RSTRING_PTR(str));
Bram Moolenaar51e9fbf2018-08-11 14:24:11 +02001723
1724 if (argc == 1)
1725 ret = argv[0];
1726 else if (argc > 1)
1727 ret = rb_ary_new4(argc, argv);
1728 return ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001729}
1730
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001731 static void
1732ruby_io_init(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001733{
1734#ifndef DYNAMIC_RUBY
1735 RUBYEXTERN VALUE rb_stdout;
Bram Moolenaarb6c8cd82018-07-24 05:41:30 +02001736 RUBYEXTERN VALUE rb_stderr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001737#endif
1738
1739 rb_stdout = rb_obj_alloc(rb_cObject);
Bram Moolenaarb6c8cd82018-07-24 05:41:30 +02001740 rb_stderr = rb_obj_alloc(rb_cObject);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001741 rb_define_singleton_method(rb_stdout, "write", vim_message, 1);
Bram Moolenaar35df7d22012-04-20 18:05:47 +02001742 rb_define_singleton_method(rb_stdout, "flush", f_nop, 0);
Bram Moolenaarb6c8cd82018-07-24 05:41:30 +02001743 rb_define_singleton_method(rb_stderr, "write", vim_message, 1);
1744 rb_define_singleton_method(rb_stderr, "flush", f_nop, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001745 rb_define_global_function("p", f_p, -1);
1746}
1747
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001748 static void
1749ruby_vim_init(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001750{
1751 objtbl = rb_hash_new();
1752 rb_global_variable(&objtbl);
1753
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001754 // The Vim module used to be called "VIM", but "Vim" is better. Make an
1755 // alias "VIM" for backwards compatibility.
Bram Moolenaarf711faf2007-05-10 16:48:19 +00001756 mVIM = rb_define_module("Vim");
1757 rb_define_const(rb_cObject, "VIM", mVIM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001758 rb_define_const(mVIM, "VERSION_MAJOR", INT2NUM(VIM_VERSION_MAJOR));
1759 rb_define_const(mVIM, "VERSION_MINOR", INT2NUM(VIM_VERSION_MINOR));
1760 rb_define_const(mVIM, "VERSION_BUILD", INT2NUM(VIM_VERSION_BUILD));
1761 rb_define_const(mVIM, "VERSION_PATCHLEVEL", INT2NUM(VIM_VERSION_PATCHLEVEL));
1762 rb_define_const(mVIM, "VERSION_SHORT", rb_str_new2(VIM_VERSION_SHORT));
1763 rb_define_const(mVIM, "VERSION_MEDIUM", rb_str_new2(VIM_VERSION_MEDIUM));
1764 rb_define_const(mVIM, "VERSION_LONG", rb_str_new2(VIM_VERSION_LONG));
1765 rb_define_const(mVIM, "VERSION_LONG_DATE", rb_str_new2(VIM_VERSION_LONG_DATE));
1766 rb_define_module_function(mVIM, "message", vim_message, 1);
1767 rb_define_module_function(mVIM, "set_option", vim_set_option, 1);
1768 rb_define_module_function(mVIM, "command", vim_command, 1);
1769 rb_define_module_function(mVIM, "evaluate", vim_evaluate, 1);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001770 rb_define_module_function(mVIM, "blob", vim_blob, 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001771
1772 eDeletedBufferError = rb_define_class_under(mVIM, "DeletedBufferError",
1773 rb_eStandardError);
1774 eDeletedWindowError = rb_define_class_under(mVIM, "DeletedWindowError",
1775 rb_eStandardError);
1776
1777 cBuffer = rb_define_class_under(mVIM, "Buffer", rb_cObject);
1778 rb_define_singleton_method(cBuffer, "current", buffer_s_current, 0);
1779 rb_define_singleton_method(cBuffer, "count", buffer_s_count, 0);
1780 rb_define_singleton_method(cBuffer, "[]", buffer_s_aref, 1);
1781 rb_define_method(cBuffer, "name", buffer_name, 0);
1782 rb_define_method(cBuffer, "number", buffer_number, 0);
1783 rb_define_method(cBuffer, "count", buffer_count, 0);
1784 rb_define_method(cBuffer, "length", buffer_count, 0);
1785 rb_define_method(cBuffer, "[]", buffer_aref, 1);
1786 rb_define_method(cBuffer, "[]=", buffer_aset, 2);
1787 rb_define_method(cBuffer, "delete", buffer_delete, 1);
1788 rb_define_method(cBuffer, "append", buffer_append, 2);
1789
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001790 // Added line manipulation functions
1791 // SegPhault - 03/07/05
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001792 rb_define_method(cBuffer, "line_number", current_line_number, 0);
1793 rb_define_method(cBuffer, "line", line_s_current, 0);
1794 rb_define_method(cBuffer, "line=", set_current_line, 1);
1795
1796
Bram Moolenaar071d4272004-06-13 20:20:40 +00001797 cVimWindow = rb_define_class_under(mVIM, "Window", rb_cObject);
1798 rb_define_singleton_method(cVimWindow, "current", window_s_current, 0);
1799 rb_define_singleton_method(cVimWindow, "count", window_s_count, 0);
1800 rb_define_singleton_method(cVimWindow, "[]", window_s_aref, 1);
1801 rb_define_method(cVimWindow, "buffer", window_buffer, 0);
1802 rb_define_method(cVimWindow, "height", window_height, 0);
1803 rb_define_method(cVimWindow, "height=", window_set_height, 1);
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001804 rb_define_method(cVimWindow, "width", window_width, 0);
1805 rb_define_method(cVimWindow, "width=", window_set_width, 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001806 rb_define_method(cVimWindow, "cursor", window_cursor, 0);
1807 rb_define_method(cVimWindow, "cursor=", window_set_cursor, 1);
1808
Bram Moolenaard5a986f2020-12-06 21:11:31 +01001809 rb_define_virtual_variable("$curbuf", buffer_s_current_getter, 0);
1810 rb_define_virtual_variable("$curwin", window_s_current_getter, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001811}
Bram Moolenaar99685e62013-05-11 13:56:18 +02001812
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001813 void
1814vim_ruby_init(void *stack_start)
Bram Moolenaar99685e62013-05-11 13:56:18 +02001815{
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001816 // should get machine stack start address early in main function
Bram Moolenaar99685e62013-05-11 13:56:18 +02001817 ruby_stack_start = stack_start;
1818}
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001819
1820 static int
1821convert_hash2dict(VALUE key, VALUE val, VALUE arg)
1822{
1823 dict_T *d = (dict_T *)arg;
1824 dictitem_T *di;
1825
1826 di = dictitem_alloc((char_u *)RSTRING_PTR(RSTRING(rb_obj_as_string(key))));
1827 if (di == NULL || ruby_convert_to_vim_value(val, &di->di_tv) != OK
1828 || dict_add(d, di) != OK)
1829 {
1830 d->dv_hashtab.ht_error = TRUE;
1831 return ST_STOP;
1832 }
1833 return ST_CONTINUE;
1834}
1835
1836 static int
1837ruby_convert_to_vim_value(VALUE val, typval_T *rettv)
1838{
1839 switch (TYPE(val))
1840 {
1841 case T_NIL:
1842 rettv->v_type = VAR_SPECIAL;
1843 rettv->vval.v_number = VVAL_NULL;
1844 break;
1845 case T_TRUE:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01001846 rettv->v_type = VAR_BOOL;
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001847 rettv->vval.v_number = VVAL_TRUE;
1848 break;
1849 case T_FALSE:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01001850 rettv->v_type = VAR_BOOL;
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001851 rettv->vval.v_number = VVAL_FALSE;
1852 break;
1853 case T_BIGNUM:
1854 case T_FIXNUM:
1855 rettv->v_type = VAR_NUMBER;
1856 rettv->vval.v_number = (varnumber_T)NUM2LONG(val);
1857 break;
1858#ifdef FEAT_FLOAT
1859 case T_FLOAT:
1860 rettv->v_type = VAR_FLOAT;
1861 rettv->vval.v_float = (float_T)NUM2DBL(val);
1862 break;
1863#endif
1864 default:
1865 val = rb_obj_as_string(val);
1866 // FALLTHROUGH
1867 case T_STRING:
1868 {
1869 VALUE str = (VALUE)RSTRING(val);
1870
1871 rettv->v_type = VAR_STRING;
1872 rettv->vval.v_string = vim_strnsave((char_u *)RSTRING_PTR(str),
Bram Moolenaar71ccd032020-06-12 22:59:11 +02001873 RSTRING_LEN(str));
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001874 }
1875 break;
1876 case T_ARRAY:
1877 {
1878 list_T *l;
1879 long i;
1880 typval_T v;
1881
1882 l = list_alloc();
1883 if (l == NULL)
1884 return FAIL;
1885
1886 for (i = 0; i < RARRAY_LEN(val); ++i)
1887 {
1888 if (ruby_convert_to_vim_value((VALUE)RARRAY_PTR(val)[i],
1889 &v) != OK)
1890 {
1891 list_unref(l);
1892 return FAIL;
1893 }
1894 list_append_tv(l, &v);
1895 clear_tv(&v);
1896 }
1897
1898 rettv->v_type = VAR_LIST;
1899 rettv->vval.v_list = l;
1900 ++l->lv_refcount;
1901 }
1902 break;
1903 case T_HASH:
1904 {
1905 dict_T *d;
1906
1907 d = dict_alloc();
1908 if (d == NULL)
1909 return FAIL;
1910
1911 rb_hash_foreach(val, convert_hash2dict, (VALUE)d);
1912 if (d->dv_hashtab.ht_error)
1913 {
1914 dict_unref(d);
1915 return FAIL;
1916 }
1917
1918 rettv->v_type = VAR_DICT;
1919 rettv->vval.v_dict = d;
1920 ++d->dv_refcount;
1921 }
1922 break;
1923 }
1924 return OK;
1925}
1926
1927 void
1928do_rubyeval(char_u *str, typval_T *rettv)
1929{
1930 int retval = FAIL;
1931
1932 if (ensure_ruby_initialized())
1933 {
1934 int state;
1935 VALUE obj;
1936
1937 obj = rb_eval_string_protect((const char *)str, &state);
1938 if (state)
1939 error_print(state);
1940 else
1941 retval = ruby_convert_to_vim_value(obj, rettv);
1942 }
1943 if (retval == FAIL)
1944 {
1945 rettv->v_type = VAR_NUMBER;
1946 rettv->vval.v_number = 0;
1947 }
1948}