blob: 6300142b3cd9d7c3d82605d1abc2ce10a82bd187 [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 Moolenaar0e121402020-12-10 20:50:34 +0100515# if RUBY_VERSION >= 26
516void rb_ary_detransient_stub(VALUE x);
517# endif
518
Bram Moolenaar41a41412020-01-07 21:32:19 +0100519# if (RUBY_VERSION >= 19) && !defined(PROTO)
520# if RUBY_VERSION >= 22
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100521 long
522rb_num2long_stub(VALUE x)
Bram Moolenaarbbc1a592015-04-21 15:25:31 +0200523# else
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100524 SIGNED_VALUE
525rb_num2long_stub(VALUE x)
Bram Moolenaarbbc1a592015-04-21 15:25:31 +0200526# endif
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100527{
528 return dll_rb_num2long(x);
529}
Bram Moolenaar41a41412020-01-07 21:32:19 +0100530# if RUBY_VERSION >= 26
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100531 VALUE
532rb_int2big_stub(intptr_t x)
Bram Moolenaar63d1fea2019-02-03 15:18:35 +0100533# else
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100534 VALUE
535rb_int2big_stub(SIGNED_VALUE x)
Bram Moolenaar63d1fea2019-02-03 15:18:35 +0100536# endif
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100537{
538 return dll_rb_int2big(x);
539}
Bram Moolenaar41a41412020-01-07 21:32:19 +0100540# if (RUBY_VERSION >= 19) && (VIM_SIZEOF_INT < VIM_SIZEOF_LONG)
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100541 long
542rb_fix2int_stub(VALUE x)
Bram Moolenaar0bcdd6e2013-04-14 16:19:03 +0200543{
544 return dll_rb_fix2int(x);
545}
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100546 long
547rb_num2int_stub(VALUE x)
Bram Moolenaar0bcdd6e2013-04-14 16:19:03 +0200548{
549 return dll_rb_num2int(x);
550}
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200551# endif
Bram Moolenaar41a41412020-01-07 21:32:19 +0100552# if RUBY_VERSION >= 20
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100553 VALUE
Bram Moolenaar886ed692013-02-26 13:41:35 +0100554rb_float_new_in_heap(double d)
555{
556 return dll_rb_float_new(d);
557}
Bram Moolenaar41a41412020-01-07 21:32:19 +0100558# if RUBY_VERSION >= 22
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100559 unsigned long
560rb_num2ulong(VALUE x)
Bram Moolenaarbbc1a592015-04-21 15:25:31 +0200561# else
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100562 VALUE
563rb_num2ulong(VALUE x)
Bram Moolenaarbbc1a592015-04-21 15:25:31 +0200564# endif
Bram Moolenaar886ed692013-02-26 13:41:35 +0100565{
566 return (long)RSHIFT((SIGNED_VALUE)(x),1);
567}
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200568# endif
569# endif
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100570
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100571 // Do not generate a prototype here, VALUE isn't always defined.
Bram Moolenaar3e9a1612014-11-12 16:05:04 +0100572# if defined(USE_RGENGC) && USE_RGENGC && !defined(PROTO)
Bram Moolenaar41a41412020-01-07 21:32:19 +0100573# if RUBY_VERSION == 21
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100574 void
575rb_gc_writebarrier_unprotect_promoted_stub(VALUE obj)
Bram Moolenaara1a118b2014-02-05 22:41:15 +0100576{
Bram Moolenaar90140742014-11-27 17:44:08 +0100577 dll_rb_gc_writebarrier_unprotect_promoted(obj);
Bram Moolenaara1a118b2014-02-05 22:41:15 +0100578}
Bram Moolenaar0c7485f2015-01-14 14:04:10 +0100579# else
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100580 void
581rb_gc_writebarrier_unprotect_stub(VALUE obj)
Bram Moolenaar0c7485f2015-01-14 14:04:10 +0100582{
583 dll_rb_gc_writebarrier_unprotect(obj);
584}
585# endif
586# endif
587
Bram Moolenaar41a41412020-01-07 21:32:19 +0100588# if RUBY_VERSION >= 26
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100589 void
590rb_ary_detransient_stub(VALUE x)
Bram Moolenaarf62fc312019-01-08 20:29:32 +0100591{
592 dll_rb_ary_detransient(x);
593}
594# endif
595
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100596static HINSTANCE hinstRuby = NULL; // Instance of ruby.dll
Bram Moolenaar071d4272004-06-13 20:20:40 +0000597
598/*
Bram Moolenaarda2303d2005-08-30 21:55:26 +0000599 * Table of name to function pointer of ruby.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000600 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000601static struct
602{
603 char *name;
604 RUBY_PROC *ptr;
605} ruby_funcname_table[] =
606{
607 {"rb_assoc_new", (RUBY_PROC*)&dll_rb_assoc_new},
608 {"rb_cFalseClass", (RUBY_PROC*)&dll_rb_cFalseClass},
Bram Moolenaar2016ae52016-06-13 20:08:43 +0200609# if defined(USE_RUBY_INTEGER)
Bram Moolenaar06469e92016-06-11 22:26:53 +0200610 {"rb_cInteger", (RUBY_PROC*)&dll_rb_cInteger},
Bram Moolenaar6abda992017-01-09 21:10:31 +0100611# else
612 {"rb_cFixnum", (RUBY_PROC*)&dll_rb_cFixnum},
Bram Moolenaar06469e92016-06-11 22:26:53 +0200613# endif
Bram Moolenaar41a41412020-01-07 21:32:19 +0100614# if RUBY_VERSION >= 20
Bram Moolenaardb3fbe52013-03-07 15:16:21 +0100615 {"rb_cFloat", (RUBY_PROC*)&dll_rb_cFloat},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200616# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000617 {"rb_cNilClass", (RUBY_PROC*)&dll_rb_cNilClass},
618 {"rb_cObject", (RUBY_PROC*)&dll_rb_cObject},
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100619 {"rb_cString", (RUBY_PROC*)&dll_rb_cString},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000620 {"rb_cSymbol", (RUBY_PROC*)&dll_rb_cSymbol},
621 {"rb_cTrueClass", (RUBY_PROC*)&dll_rb_cTrueClass},
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100622 {"rb_class_new_instance", (RUBY_PROC*)&dll_rb_class_new_instance},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000623 {"rb_check_type", (RUBY_PROC*)&dll_rb_check_type},
Bram Moolenaarf2f6d292015-12-28 20:57:10 +0100624# ifdef USE_TYPEDDATA
625 {"rb_check_typeddata", (RUBY_PROC*)&dll_rb_check_typeddata},
626# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000627 {"rb_class_path", (RUBY_PROC*)&dll_rb_class_path},
Bram Moolenaarf2f6d292015-12-28 20:57:10 +0100628# ifdef USE_TYPEDDATA
Bram Moolenaar41a41412020-01-07 21:32:19 +0100629# if RUBY_VERSION >= 23
Bram Moolenaarf2f6d292015-12-28 20:57:10 +0100630 {"rb_data_typed_object_wrap", (RUBY_PROC*)&dll_rb_data_typed_object_wrap},
631# else
632 {"rb_data_typed_object_alloc", (RUBY_PROC*)&dll_rb_data_typed_object_alloc},
633# endif
634# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000635 {"rb_data_object_alloc", (RUBY_PROC*)&dll_rb_data_object_alloc},
Bram Moolenaarf2f6d292015-12-28 20:57:10 +0100636# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000637 {"rb_define_class_under", (RUBY_PROC*)&dll_rb_define_class_under},
638 {"rb_define_const", (RUBY_PROC*)&dll_rb_define_const},
639 {"rb_define_global_function", (RUBY_PROC*)&dll_rb_define_global_function},
640 {"rb_define_method", (RUBY_PROC*)&dll_rb_define_method},
641 {"rb_define_module", (RUBY_PROC*)&dll_rb_define_module},
642 {"rb_define_module_function", (RUBY_PROC*)&dll_rb_define_module_function},
643 {"rb_define_singleton_method", (RUBY_PROC*)&dll_rb_define_singleton_method},
644 {"rb_define_virtual_variable", (RUBY_PROC*)&dll_rb_define_virtual_variable},
645 {"rb_stdout", (RUBY_PROC*)&dll_rb_stdout},
Bram Moolenaarb6c8cd82018-07-24 05:41:30 +0200646 {"rb_stderr", (RUBY_PROC*)&dll_rb_stderr},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000647 {"rb_eArgError", (RUBY_PROC*)&dll_rb_eArgError},
648 {"rb_eIndexError", (RUBY_PROC*)&dll_rb_eIndexError},
649 {"rb_eRuntimeError", (RUBY_PROC*)&dll_rb_eRuntimeError},
650 {"rb_eStandardError", (RUBY_PROC*)&dll_rb_eStandardError},
651 {"rb_eval_string_protect", (RUBY_PROC*)&dll_rb_eval_string_protect},
Bram Moolenaar41a41412020-01-07 21:32:19 +0100652# if RUBY_VERSION >= 21
Bram Moolenaarf711cb22018-08-01 18:42:13 +0200653 {"rb_funcallv", (RUBY_PROC*)&dll_rb_funcallv},
654# else
655 {"rb_funcall2", (RUBY_PROC*)&dll_rb_funcall2},
656# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000657 {"rb_global_variable", (RUBY_PROC*)&dll_rb_global_variable},
658 {"rb_hash_aset", (RUBY_PROC*)&dll_rb_hash_aset},
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100659 {"rb_hash_foreach", (RUBY_PROC*)&dll_rb_hash_foreach},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000660 {"rb_hash_new", (RUBY_PROC*)&dll_rb_hash_new},
661 {"rb_inspect", (RUBY_PROC*)&dll_rb_inspect},
662 {"rb_int2inum", (RUBY_PROC*)&dll_rb_int2inum},
Bram Moolenaarf711cb22018-08-01 18:42:13 +0200663 {"rb_intern", (RUBY_PROC*)&dll_rb_intern},
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100664# if VIM_SIZEOF_INT < VIM_SIZEOF_LONG // 64 bits only
Bram Moolenaar2623b4f2012-09-18 16:36:32 +0200665 {"rb_fix2int", (RUBY_PROC*)&dll_rb_fix2int},
666 {"rb_num2int", (RUBY_PROC*)&dll_rb_num2int},
667 {"rb_num2uint", (RUBY_PROC*)&dll_rb_num2uint},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200668# endif
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100669 {"rb_num2dbl", (RUBY_PROC*)&dll_rb_num2dbl},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000670 {"rb_lastline_get", (RUBY_PROC*)&dll_rb_lastline_get},
671 {"rb_lastline_set", (RUBY_PROC*)&dll_rb_lastline_set},
Bram Moolenaard0573012017-10-28 21:11:06 +0200672 {"rb_protect", (RUBY_PROC*)&dll_rb_protect},
673 {"rb_load", (RUBY_PROC*)&dll_rb_load},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000674 {"rb_num2long", (RUBY_PROC*)&dll_rb_num2long},
675 {"rb_num2ulong", (RUBY_PROC*)&dll_rb_num2ulong},
676 {"rb_obj_alloc", (RUBY_PROC*)&dll_rb_obj_alloc},
677 {"rb_obj_as_string", (RUBY_PROC*)&dll_rb_obj_as_string},
678 {"rb_obj_id", (RUBY_PROC*)&dll_rb_obj_id},
679 {"rb_raise", (RUBY_PROC*)&dll_rb_raise},
Bram Moolenaar41a41412020-01-07 21:32:19 +0100680# if RUBY_VERSION >= 18
Bram Moolenaarf9b5ef82010-09-29 13:02:53 +0200681 {"rb_string_value", (RUBY_PROC*)&dll_rb_string_value},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200682# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000683 {"rb_str2cstr", (RUBY_PROC*)&dll_rb_str2cstr},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200684# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000685 {"rb_str_cat", (RUBY_PROC*)&dll_rb_str_cat},
686 {"rb_str_concat", (RUBY_PROC*)&dll_rb_str_concat},
687 {"rb_str_new", (RUBY_PROC*)&dll_rb_str_new},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200688# ifdef need_rb_str_new_cstr
Bram Moolenaar1d2beae2010-05-22 21:56:55 +0200689 {"rb_str_new_cstr", (RUBY_PROC*)&dll_rb_str_new_cstr},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200690# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000691 {"rb_str_new2", (RUBY_PROC*)&dll_rb_str_new2},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200692# endif
Bram Moolenaar41a41412020-01-07 21:32:19 +0100693# if RUBY_VERSION >= 19
Bram Moolenaar165641d2010-02-17 16:23:09 +0100694 {"rb_errinfo", (RUBY_PROC*)&dll_rb_errinfo},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200695# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000696 {"ruby_errinfo", (RUBY_PROC*)&dll_ruby_errinfo},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200697# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000698 {"ruby_init", (RUBY_PROC*)&dll_ruby_init},
699 {"ruby_init_loadpath", (RUBY_PROC*)&dll_ruby_init_loadpath},
Bram Moolenaar4f974752019-02-17 17:44:42 +0100700# ifdef MSWIN
Bram Moolenaar41a41412020-01-07 21:32:19 +0100701# if RUBY_VERSION >= 19
Bram Moolenaar4f391792017-01-15 16:59:07 +0100702 {"ruby_sysinit", (RUBY_PROC*)&dll_ruby_sysinit},
Bram Moolenaar9d5c84a2018-12-19 20:48:46 +0100703# else
704 {"NtInitialize", (RUBY_PROC*)&dll_NtInitialize},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200705# endif
Bram Moolenaar41a41412020-01-07 21:32:19 +0100706# if RUBY_VERSION >= 18
Bram Moolenaar071d4272004-06-13 20:20:40 +0000707 {"rb_w32_snprintf", (RUBY_PROC*)&dll_rb_w32_snprintf},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200708# endif
Bram Moolenaar3ca71f12010-10-27 16:49:47 +0200709# endif
Bram Moolenaar41a41412020-01-07 21:32:19 +0100710# if RUBY_VERSION >= 18
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100711 {"rb_string_value_ptr", (RUBY_PROC*)&dll_rb_string_value_ptr},
Bram Moolenaar41a41412020-01-07 21:32:19 +0100712# if RUBY_VERSION <= 19
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100713 {"rb_float_new", (RUBY_PROC*)&dll_rb_float_new},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200714# else
Bram Moolenaar886ed692013-02-26 13:41:35 +0100715 {"rb_float_new_in_heap", (RUBY_PROC*)&dll_rb_float_new},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200716# endif
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100717 {"rb_ary_new", (RUBY_PROC*)&dll_rb_ary_new},
Bram Moolenaar51e9fbf2018-08-11 14:24:11 +0200718# ifdef RB_ARY_NEW4_MACRO
719 {"rb_ary_new_from_values", (RUBY_PROC*)&dll_rb_ary_new4},
720# else
721 {"rb_ary_new4", (RUBY_PROC*)&dll_rb_ary_new4},
722# endif
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100723 {"rb_ary_push", (RUBY_PROC*)&dll_rb_ary_push},
Bram Moolenaar41a41412020-01-07 21:32:19 +0100724# if RUBY_VERSION >= 26
Bram Moolenaarb09c6842018-12-27 22:11:01 +0100725 {"rb_ary_detransient", (RUBY_PROC*)&dll_rb_ary_detransient},
726# endif
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200727# endif
Bram Moolenaar41a41412020-01-07 21:32:19 +0100728# if RUBY_VERSION >= 19
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100729 {"rb_int2big", (RUBY_PROC*)&dll_rb_int2big},
Bram Moolenaar165641d2010-02-17 16:23:09 +0100730 {"ruby_script", (RUBY_PROC*)&dll_ruby_script},
731 {"rb_enc_find_index", (RUBY_PROC*)&dll_rb_enc_find_index},
732 {"rb_enc_find", (RUBY_PROC*)&dll_rb_enc_find},
733 {"rb_enc_str_new", (RUBY_PROC*)&dll_rb_enc_str_new},
734 {"rb_sprintf", (RUBY_PROC*)&dll_rb_sprintf},
Bram Moolenaar7a8ef142010-12-24 13:39:35 +0100735 {"rb_require", (RUBY_PROC*)&dll_rb_require},
Bram Moolenaar9b1067e2015-11-19 19:33:15 +0100736 {"ruby_options", (RUBY_PROC*)&dll_ruby_options},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200737# endif
Bram Moolenaar41a41412020-01-07 21:32:19 +0100738# if (RUBY_VERSION >= 19) || defined(RUBY_INIT_STACK)
Bram Moolenaar10f3a792013-05-20 12:52:29 +0200739# ifdef __ia64
740 {"rb_ia64_bsp", (RUBY_PROC*)&dll_rb_ia64_bsp},
741# endif
742 {"ruby_init_stack", (RUBY_PROC*)&dll_ruby_init_stack},
743# endif
Bram Moolenaara1a118b2014-02-05 22:41:15 +0100744# if defined(USE_RGENGC) && USE_RGENGC
Bram Moolenaar41a41412020-01-07 21:32:19 +0100745# if RUBY_VERSION == 21
Bram Moolenaara1a118b2014-02-05 22:41:15 +0100746 {"rb_gc_writebarrier_unprotect_promoted", (RUBY_PROC*)&dll_rb_gc_writebarrier_unprotect_promoted},
Bram Moolenaar0c7485f2015-01-14 14:04:10 +0100747# else
748 {"rb_gc_writebarrier_unprotect", (RUBY_PROC*)&dll_rb_gc_writebarrier_unprotect},
749# endif
Bram Moolenaara1a118b2014-02-05 22:41:15 +0100750# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000751 {"", NULL},
752};
753
754/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000755 * Load library and get all pointers.
756 * Parameter 'libname' provides name of DLL.
757 * Return OK or FAIL.
758 */
759 static int
760ruby_runtime_link_init(char *libname, int verbose)
761{
762 int i;
763
764 if (hinstRuby)
765 return OK;
Bram Moolenaarf9b5ef82010-09-29 13:02:53 +0200766 hinstRuby = load_dll(libname);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000767 if (!hinstRuby)
768 {
769 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100770 semsg(_(e_loadlib), libname);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000771 return FAIL;
772 }
773
774 for (i = 0; ruby_funcname_table[i].ptr; ++i)
775 {
Bram Moolenaarf9b5ef82010-09-29 13:02:53 +0200776 if (!(*ruby_funcname_table[i].ptr = symbol_from_dll(hinstRuby,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000777 ruby_funcname_table[i].name)))
778 {
Bram Moolenaarf9b5ef82010-09-29 13:02:53 +0200779 close_dll(hinstRuby);
Bram Moolenaar3ca71f12010-10-27 16:49:47 +0200780 hinstRuby = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000781 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100782 semsg(_(e_loadfunc), ruby_funcname_table[i].name);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000783 return FAIL;
784 }
785 }
786 return OK;
787}
788
789/*
790 * If ruby is enabled (there is installed ruby on Windows system) return TRUE,
791 * else FALSE.
792 */
793 int
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100794ruby_enabled(int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000795{
Bram Moolenaar25e4fcd2016-01-09 14:57:47 +0100796 return ruby_runtime_link_init((char *)p_rubydll, verbose) == OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000797}
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100798#endif // defined(DYNAMIC_RUBY) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000799
800 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100801ruby_end(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000802{
Bram Moolenaar071d4272004-06-13 20:20:40 +0000803}
804
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100805 void
806ex_ruby(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000807{
808 int state;
809 char *script = NULL;
810
Bram Moolenaar35a2e192006-03-13 22:07:11 +0000811 script = (char *)script_get(eap, eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000812 if (!eap->skip && ensure_ruby_initialized())
813 {
814 if (script == NULL)
815 rb_eval_string_protect((char *)eap->arg, &state);
816 else
817 rb_eval_string_protect(script, &state);
818 if (state)
819 error_print(state);
820 }
821 vim_free(script);
822}
823
Bram Moolenaar165641d2010-02-17 16:23:09 +0100824/*
825 * In Ruby 1.9 or later, ruby String object has encoding.
826 * conversion buffer string of vim to ruby String object using
827 * VIM encoding option.
828 */
829 static VALUE
830vim_str2rb_enc_str(const char *s)
831{
Bram Moolenaar41a41412020-01-07 21:32:19 +0100832#if RUBY_VERSION >= 19
Bram Moolenaar165641d2010-02-17 16:23:09 +0100833 int isnum;
834 long lval;
835 char_u *sval;
836 rb_encoding *enc;
837
838 isnum = get_option_value((char_u *)"enc", &lval, &sval, 0);
839 if (isnum == 0)
840 {
841 enc = rb_enc_find((char *)sval);
842 vim_free(sval);
Bram Moolenaar758535a2016-03-30 22:06:16 +0200843 if (enc)
Bram Moolenaar9b0ac222016-06-01 20:31:43 +0200844 return rb_enc_str_new(s, (long)strlen(s), enc);
Bram Moolenaar165641d2010-02-17 16:23:09 +0100845 }
846#endif
847 return rb_str_new2(s);
848}
849
850 static VALUE
851eval_enc_string_protect(const char *str, int *state)
852{
Bram Moolenaar41a41412020-01-07 21:32:19 +0100853#if RUBY_VERSION >= 19
Bram Moolenaar165641d2010-02-17 16:23:09 +0100854 int isnum;
855 long lval;
856 char_u *sval;
857 rb_encoding *enc;
858 VALUE v;
859
860 isnum = get_option_value((char_u *)"enc", &lval, &sval, 0);
861 if (isnum == 0)
862 {
863 enc = rb_enc_find((char *)sval);
864 vim_free(sval);
865 if (enc)
866 {
867 v = rb_sprintf("#-*- coding:%s -*-\n%s", rb_enc_name(enc), str);
868 return rb_eval_string_protect(StringValuePtr(v), state);
869 }
870 }
871#endif
872 return rb_eval_string_protect(str, state);
873}
874
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100875 void
876ex_rubydo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000877{
878 int state;
879 linenr_T i;
Bram Moolenaarc593fee2017-01-29 23:11:25 +0100880 buf_T *was_curbuf = curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000881
882 if (ensure_ruby_initialized())
883 {
884 if (u_save(eap->line1 - 1, eap->line2 + 1) != OK)
885 return;
Bram Moolenaar758535a2016-03-30 22:06:16 +0200886 for (i = eap->line1; i <= eap->line2; i++)
887 {
Bram Moolenaare980d8a2010-12-08 13:11:21 +0100888 VALUE line;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000889
Bram Moolenaarc593fee2017-01-29 23:11:25 +0100890 if (i > curbuf->b_ml.ml_line_count)
891 break;
Bram Moolenaare980d8a2010-12-08 13:11:21 +0100892 line = vim_str2rb_enc_str((char *)ml_get(i));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000893 rb_lastline_set(line);
Bram Moolenaar165641d2010-02-17 16:23:09 +0100894 eval_enc_string_protect((char *) eap->arg, &state);
Bram Moolenaar758535a2016-03-30 22:06:16 +0200895 if (state)
896 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000897 error_print(state);
898 break;
899 }
Bram Moolenaarc593fee2017-01-29 23:11:25 +0100900 if (was_curbuf != curbuf)
901 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000902 line = rb_lastline_get();
Bram Moolenaar758535a2016-03-30 22:06:16 +0200903 if (!NIL_P(line))
904 {
905 if (TYPE(line) != T_STRING)
906 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100907 emsg(_("E265: $_ must be an instance of String"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000908 return;
909 }
Bram Moolenaar165641d2010-02-17 16:23:09 +0100910 ml_replace(i, (char_u *) StringValuePtr(line), 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000911 changed();
912#ifdef SYNTAX_HL
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100913 syn_changed(i); // recompute syntax hl. for this line
Bram Moolenaar071d4272004-06-13 20:20:40 +0000914#endif
915 }
916 }
917 check_cursor();
918 update_curbuf(NOT_VALID);
919 }
920}
921
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100922 static VALUE
923rb_load_wrap(VALUE file_to_load)
Bram Moolenaar37badc82018-01-31 20:15:30 +0100924{
925 rb_load(file_to_load, 0);
926 return Qnil;
927}
928
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100929 void
930ex_rubyfile(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000931{
932 int state;
933
934 if (ensure_ruby_initialized())
935 {
Bram Moolenaar37badc82018-01-31 20:15:30 +0100936 VALUE file_to_load = rb_str_new2((const char *)eap->arg);
937 rb_protect(rb_load_wrap, file_to_load, &state);
938 if (state)
939 error_print(state);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000940 }
941}
942
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100943 void
944ruby_buffer_free(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000945{
Bram Moolenaare344bea2005-09-01 20:46:49 +0000946 if (buf->b_ruby_ref)
947 {
948 rb_hash_aset(objtbl, rb_obj_id((VALUE) buf->b_ruby_ref), Qnil);
949 RDATA(buf->b_ruby_ref)->data = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000950 }
951}
952
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100953 void
954ruby_window_free(win_T *win)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000955{
Bram Moolenaare344bea2005-09-01 20:46:49 +0000956 if (win->w_ruby_ref)
957 {
958 rb_hash_aset(objtbl, rb_obj_id((VALUE) win->w_ruby_ref), Qnil);
959 RDATA(win->w_ruby_ref)->data = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000960 }
961}
962
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100963 static int
964ensure_ruby_initialized(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000965{
966 if (!ruby_initialized)
967 {
968#ifdef DYNAMIC_RUBY
969 if (ruby_enabled(TRUE))
970 {
971#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +0100972#ifdef MSWIN
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100973 // suggested by Ariya Mizutani
Bram Moolenaar0b69c732010-02-17 15:11:50 +0100974 int argc = 1;
975 char *argv[] = {"gvim.exe"};
Bram Moolenaar90140742014-11-27 17:44:08 +0100976 char **argvp = argv;
Bram Moolenaar41a41412020-01-07 21:32:19 +0100977# if RUBY_VERSION >= 19
Bram Moolenaarfe6ce332017-01-14 20:12:01 +0100978 ruby_sysinit(&argc, &argvp);
979# else
Bram Moolenaar90140742014-11-27 17:44:08 +0100980 NtInitialize(&argc, &argvp);
Bram Moolenaarfe6ce332017-01-14 20:12:01 +0100981# endif
Bram Moolenaar0b69c732010-02-17 15:11:50 +0100982#endif
Bram Moolenaarb2c03502010-07-02 20:20:09 +0200983 {
Bram Moolenaar41a41412020-01-07 21:32:19 +0100984#if (RUBY_VERSION >= 19) || defined(RUBY_INIT_STACK)
Bram Moolenaar99685e62013-05-11 13:56:18 +0200985 ruby_init_stack(ruby_stack_start);
Bram Moolenaar165641d2010-02-17 16:23:09 +0100986#endif
Bram Moolenaarb2c03502010-07-02 20:20:09 +0200987 ruby_init();
988 }
Bram Moolenaar41a41412020-01-07 21:32:19 +0100989#if RUBY_VERSION >= 19
Bram Moolenaar7a8ef142010-12-24 13:39:35 +0100990 {
991 int dummy_argc = 2;
Bram Moolenaard1bc96ce2017-09-26 21:21:44 +0200992 char *dummy_argv[] = {"vim-ruby", "-e_=0"};
Bram Moolenaar9b1067e2015-11-19 19:33:15 +0100993 ruby_options(dummy_argc, dummy_argv);
Bram Moolenaar7a8ef142010-12-24 13:39:35 +0100994 }
Bram Moolenaar165641d2010-02-17 16:23:09 +0100995 ruby_script("vim-ruby");
Bram Moolenaar7a8ef142010-12-24 13:39:35 +0100996#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000997 ruby_init_loadpath();
Bram Moolenaar165641d2010-02-17 16:23:09 +0100998#endif
Bram Moolenaar7a8ef142010-12-24 13:39:35 +0100999 ruby_io_init();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001000 ruby_vim_init();
1001 ruby_initialized = 1;
1002#ifdef DYNAMIC_RUBY
1003 }
1004 else
1005 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001006 emsg(_("E266: Sorry, this command is disabled, the Ruby library could not be loaded."));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001007 return 0;
1008 }
1009#endif
1010 }
1011 return ruby_initialized;
1012}
1013
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001014 static void
1015error_print(int state)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001016{
Bram Moolenaar41a41412020-01-07 21:32:19 +01001017#if !defined(DYNAMIC_RUBY) && (RUBY_VERSION <= 18)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001018 RUBYEXTERN VALUE ruby_errinfo;
1019#endif
Bram Moolenaarf711cb22018-08-01 18:42:13 +02001020 VALUE error;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001021 VALUE eclass;
1022 VALUE einfo;
Bram Moolenaarf711cb22018-08-01 18:42:13 +02001023 VALUE bt;
1024 int attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001025 char buff[BUFSIZ];
Bram Moolenaarf711cb22018-08-01 18:42:13 +02001026 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001027
1028#define TAG_RETURN 0x1
1029#define TAG_BREAK 0x2
1030#define TAG_NEXT 0x3
1031#define TAG_RETRY 0x4
1032#define TAG_REDO 0x5
1033#define TAG_RAISE 0x6
1034#define TAG_THROW 0x7
1035#define TAG_FATAL 0x8
1036#define TAG_MASK 0xf
1037
Bram Moolenaar758535a2016-03-30 22:06:16 +02001038 switch (state)
1039 {
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001040 case TAG_RETURN:
1041 emsg(_("E267: unexpected return"));
1042 break;
1043 case TAG_NEXT:
1044 emsg(_("E268: unexpected next"));
1045 break;
1046 case TAG_BREAK:
1047 emsg(_("E269: unexpected break"));
1048 break;
1049 case TAG_REDO:
1050 emsg(_("E270: unexpected redo"));
1051 break;
1052 case TAG_RETRY:
1053 emsg(_("E271: retry outside of rescue clause"));
1054 break;
1055 case TAG_RAISE:
1056 case TAG_FATAL:
Bram Moolenaar41a41412020-01-07 21:32:19 +01001057#if RUBY_VERSION >= 19
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001058 error = rb_errinfo();
Bram Moolenaar165641d2010-02-17 16:23:09 +01001059#else
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001060 error = ruby_errinfo;
Bram Moolenaar165641d2010-02-17 16:23:09 +01001061#endif
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001062 eclass = CLASS_OF(error);
1063 einfo = rb_obj_as_string(error);
1064 if (eclass == rb_eRuntimeError && RSTRING_LEN(einfo) == 0)
1065 {
1066 emsg(_("E272: unhandled exception"));
1067 }
1068 else
1069 {
1070 VALUE epath;
1071 char *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001072
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001073 epath = rb_class_path(eclass);
1074 vim_snprintf(buff, BUFSIZ, "%s: %s",
1075 RSTRING_PTR(epath), RSTRING_PTR(einfo));
1076 p = strchr(buff, '\n');
1077 if (p) *p = '\0';
1078 emsg(buff);
1079 }
Bram Moolenaarf711cb22018-08-01 18:42:13 +02001080
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001081 attr = syn_name2attr((char_u *)"Error");
Bram Moolenaar49c99fc2020-02-11 23:01:39 +01001082#if RUBY_VERSION >= 21
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001083 bt = rb_funcallv(error, rb_intern("backtrace"), 0, 0);
1084 for (i = 0; i < RARRAY_LEN(bt); i++)
1085 msg_attr(RSTRING_PTR(RARRAY_AREF(bt, i)), attr);
Bram Moolenaar49c99fc2020-02-11 23:01:39 +01001086#else
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001087 bt = rb_funcall2(error, rb_intern("backtrace"), 0, 0);
1088 for (i = 0; i < RARRAY_LEN(bt); i++)
1089 msg_attr(RSTRING_PTR(RARRAY_PTR(bt)[i]), attr);
Bram Moolenaar49c99fc2020-02-11 23:01:39 +01001090#endif
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001091 break;
1092 default:
1093 vim_snprintf(buff, BUFSIZ, _("E273: unknown longjmp status %d"), state);
1094 emsg(buff);
1095 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001096 }
1097}
1098
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001099 static VALUE
1100vim_message(VALUE self UNUSED, VALUE str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001101{
1102 char *buff, *p;
1103
1104 str = rb_obj_as_string(str);
Bram Moolenaar3f5f7952011-08-04 19:34:59 +02001105 if (RSTRING_LEN(str) > 0)
1106 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001107 // Only do this when the string isn't empty, alloc(0) causes trouble.
Bram Moolenaar00ccf542017-09-03 15:17:48 +02001108 buff = ALLOCA_N(char, RSTRING_LEN(str) + 1);
Bram Moolenaar3f5f7952011-08-04 19:34:59 +02001109 strcpy(buff, RSTRING_PTR(str));
1110 p = strchr(buff, '\n');
1111 if (p) *p = '\0';
Bram Moolenaar32526b32019-01-19 17:43:09 +01001112 msg(buff);
Bram Moolenaar3f5f7952011-08-04 19:34:59 +02001113 }
1114 else
1115 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01001116 msg("");
Bram Moolenaar3f5f7952011-08-04 19:34:59 +02001117 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001118 return Qnil;
1119}
1120
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001121 static VALUE
1122vim_set_option(VALUE self UNUSED, VALUE str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001123{
Bram Moolenaar165641d2010-02-17 16:23:09 +01001124 do_set((char_u *)StringValuePtr(str), 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001125 update_screen(NOT_VALID);
1126 return Qnil;
1127}
1128
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001129 static VALUE
1130vim_command(VALUE self UNUSED, VALUE str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001131{
Bram Moolenaar165641d2010-02-17 16:23:09 +01001132 do_cmdline_cmd((char_u *)StringValuePtr(str));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001133 return Qnil;
1134}
1135
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001136#ifdef FEAT_EVAL
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001137 static VALUE
1138vim_to_ruby(typval_T *tv)
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001139{
1140 VALUE result = Qnil;
1141
1142 if (tv->v_type == VAR_STRING)
1143 {
Bram Moolenaar94127e42010-03-19 23:08:48 +01001144 result = rb_str_new2(tv->vval.v_string == NULL
1145 ? "" : (char *)(tv->vval.v_string));
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001146 }
1147 else if (tv->v_type == VAR_NUMBER)
1148 {
Bram Moolenaar639a2552010-03-17 18:15:23 +01001149 result = INT2NUM(tv->vval.v_number);
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001150 }
1151# ifdef FEAT_FLOAT
1152 else if (tv->v_type == VAR_FLOAT)
1153 {
Bram Moolenaar639a2552010-03-17 18:15:23 +01001154 result = rb_float_new(tv->vval.v_float);
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001155 }
1156# endif
1157 else if (tv->v_type == VAR_LIST)
1158 {
Bram Moolenaar639a2552010-03-17 18:15:23 +01001159 list_T *list = tv->vval.v_list;
1160 listitem_T *curr;
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001161
Bram Moolenaar639a2552010-03-17 18:15:23 +01001162 result = rb_ary_new();
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001163
Bram Moolenaar639a2552010-03-17 18:15:23 +01001164 if (list != NULL)
1165 {
Bram Moolenaaraeea7212020-04-02 18:50:46 +02001166 FOR_ALL_LIST_ITEMS(list, curr)
Bram Moolenaar639a2552010-03-17 18:15:23 +01001167 rb_ary_push(result, vim_to_ruby(&curr->li_tv));
Bram Moolenaar639a2552010-03-17 18:15:23 +01001168 }
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001169 }
1170 else if (tv->v_type == VAR_DICT)
1171 {
Bram Moolenaar639a2552010-03-17 18:15:23 +01001172 result = rb_hash_new();
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001173
Bram Moolenaar639a2552010-03-17 18:15:23 +01001174 if (tv->vval.v_dict != NULL)
1175 {
1176 hashtab_T *ht = &tv->vval.v_dict->dv_hashtab;
1177 long_u todo = ht->ht_used;
1178 hashitem_T *hi;
1179 dictitem_T *di;
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001180
Bram Moolenaar639a2552010-03-17 18:15:23 +01001181 for (hi = ht->ht_array; todo > 0; ++hi)
1182 {
1183 if (!HASHITEM_EMPTY(hi))
1184 {
1185 --todo;
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001186
Bram Moolenaar639a2552010-03-17 18:15:23 +01001187 di = dict_lookup(hi);
1188 rb_hash_aset(result, rb_str_new2((char *)hi->hi_key),
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001189 vim_to_ruby(&di->di_tv));
Bram Moolenaar639a2552010-03-17 18:15:23 +01001190 }
1191 }
1192 }
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001193 }
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01001194 else if (tv->v_type == VAR_BOOL || tv->v_type == VAR_SPECIAL)
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001195 {
Bram Moolenaard84b26a2018-07-28 17:18:09 +02001196 if (tv->vval.v_number == VVAL_TRUE)
1197 result = Qtrue;
1198 else if (tv->vval.v_number == VVAL_FALSE)
1199 result = Qfalse;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001200 }
1201 else if (tv->v_type == VAR_BLOB)
1202 {
1203 result = rb_str_new(tv->vval.v_blob->bv_ga.ga_data,
1204 tv->vval.v_blob->bv_ga.ga_len);
1205 }
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001206 // else return Qnil;
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001207
1208 return result;
1209}
1210#endif
1211
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001212 static VALUE
1213vim_evaluate(VALUE self UNUSED, VALUE str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001214{
1215#ifdef FEAT_EVAL
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001216 typval_T *tv;
1217 VALUE result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001218
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001219 tv = eval_expr((char_u *)StringValuePtr(str), NULL);
1220 if (tv == NULL)
Bram Moolenaar639a2552010-03-17 18:15:23 +01001221 return Qnil;
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001222 result = vim_to_ruby(tv);
1223
1224 free_tv(tv);
1225
1226 return result;
1227#else
1228 return Qnil;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001229#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001230}
1231
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001232#ifdef USE_TYPEDDATA
1233static size_t buffer_dsize(const void *buf);
1234
1235static const rb_data_type_t buffer_type = {
1236 "vim_buffer",
Bram Moolenaar41a41412020-01-07 21:32:19 +01001237 {0, 0, buffer_dsize,
1238# if RUBY_VERSION >= 27
Bram Moolenaar8b430b42020-02-22 15:01:00 +01001239 0, {0}
Bram Moolenaar41a41412020-01-07 21:32:19 +01001240# else
1241 {0, 0}
1242# endif
1243 },
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001244 0, 0,
1245# ifdef RUBY_TYPED_FREE_IMMEDIATELY
1246 0,
1247# endif
1248};
1249
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001250 static size_t
1251buffer_dsize(const void *buf UNUSED)
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001252{
1253 return sizeof(buf_T);
1254}
1255#endif
1256
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001257 static VALUE
1258buffer_new(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001259{
Bram Moolenaare344bea2005-09-01 20:46:49 +00001260 if (buf->b_ruby_ref)
1261 {
1262 return (VALUE) buf->b_ruby_ref;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001263 }
Bram Moolenaare344bea2005-09-01 20:46:49 +00001264 else
1265 {
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001266#ifdef USE_TYPEDDATA
1267 VALUE obj = TypedData_Wrap_Struct(cBuffer, &buffer_type, buf);
1268#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001269 VALUE obj = Data_Wrap_Struct(cBuffer, 0, 0, buf);
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001270#endif
Bram Moolenaare344bea2005-09-01 20:46:49 +00001271 buf->b_ruby_ref = (void *) obj;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001272 rb_hash_aset(objtbl, rb_obj_id(obj), obj);
1273 return obj;
1274 }
1275}
1276
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001277 static buf_T *
1278get_buf(VALUE obj)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001279{
1280 buf_T *buf;
1281
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001282#ifdef USE_TYPEDDATA
1283 TypedData_Get_Struct(obj, buf_T, &buffer_type, buf);
1284#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001285 Data_Get_Struct(obj, buf_T, buf);
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001286#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001287 if (buf == NULL)
1288 rb_raise(eDeletedBufferError, "attempt to refer to deleted buffer");
1289 return buf;
1290}
1291
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001292 static VALUE
1293vim_blob(VALUE self UNUSED, VALUE str)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001294{
1295 VALUE result = rb_str_new("0z", 2);
1296 char buf[4];
1297 int i;
1298 for (i = 0; i < RSTRING_LEN(str); i++)
1299 {
Bram Moolenaar0d13cce2019-02-23 14:23:03 +01001300 sprintf(buf, "%02X", (unsigned char)(RSTRING_PTR(str)[i]));
Bram Moolenaarb191be22019-01-30 21:00:12 +01001301 rb_str_concat(result, rb_str_new2(buf));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001302 }
1303 return result;
1304}
1305
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001306 static VALUE
Bram Moolenaard5a986f2020-12-06 21:11:31 +01001307buffer_s_current(VALUE self UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001308{
1309 return buffer_new(curbuf);
1310}
1311
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001312 static VALUE
Bram Moolenaard5a986f2020-12-06 21:11:31 +01001313buffer_s_current_getter(ID id UNUSED, VALUE *x UNUSED)
1314{
1315 return buffer_new(curbuf);
1316}
1317
1318 static VALUE
1319buffer_s_count(VALUE self UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001320{
1321 buf_T *b;
1322 int n = 0;
1323
Bram Moolenaar29323592016-07-24 22:04:11 +02001324 FOR_ALL_BUFFERS(b)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001325 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001326 // Deleted buffers should not be counted
1327 // SegPhault - 01/07/05
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001328 if (b->b_p_bl)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001329 n++;
1330 }
1331
Bram Moolenaar071d4272004-06-13 20:20:40 +00001332 return INT2NUM(n);
1333}
1334
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001335 static VALUE
1336buffer_s_aref(VALUE self UNUSED, VALUE num)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001337{
1338 buf_T *b;
1339 int n = NUM2INT(num);
1340
Bram Moolenaar29323592016-07-24 22:04:11 +02001341 FOR_ALL_BUFFERS(b)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001342 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001343 // Deleted buffers should not be counted
1344 // SegPhault - 01/07/05
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001345 if (!b->b_p_bl)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001346 continue;
1347
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001348 if (n == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001349 return buffer_new(b);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001350
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001351 n--;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001352 }
1353 return Qnil;
1354}
1355
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001356 static VALUE
1357buffer_name(VALUE self)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001358{
1359 buf_T *buf = get_buf(self);
1360
Bram Moolenaar35a2e192006-03-13 22:07:11 +00001361 return buf->b_ffname ? rb_str_new2((char *)buf->b_ffname) : Qnil;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001362}
1363
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001364 static VALUE
1365buffer_number(VALUE self)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001366{
1367 buf_T *buf = get_buf(self);
1368
1369 return INT2NUM(buf->b_fnum);
1370}
1371
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001372 static VALUE
1373buffer_count(VALUE self)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001374{
1375 buf_T *buf = get_buf(self);
1376
1377 return INT2NUM(buf->b_ml.ml_line_count);
1378}
1379
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001380 static VALUE
1381get_buffer_line(buf_T *buf, linenr_T n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001382{
Bram Moolenaar3c531602010-11-16 14:46:19 +01001383 if (n <= 0 || n > buf->b_ml.ml_line_count)
1384 rb_raise(rb_eIndexError, "line number %ld out of range", (long)n);
1385 return vim_str2rb_enc_str((char *)ml_get_buf(buf, n, FALSE));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001386}
1387
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001388 static VALUE
1389buffer_aref(VALUE self, VALUE num)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001390{
1391 buf_T *buf = get_buf(self);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001392
1393 if (buf != NULL)
1394 return get_buffer_line(buf, (linenr_T)NUM2LONG(num));
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001395 return Qnil; // For stop warning
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001396}
1397
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001398 static VALUE
1399set_buffer_line(buf_T *buf, linenr_T n, VALUE str)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001400{
Bram Moolenaar165641d2010-02-17 16:23:09 +01001401 char *line = StringValuePtr(str);
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001402 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001403
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001404 if (n > 0 && n <= buf->b_ml.ml_line_count && line != NULL)
1405 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001406 // set curwin/curbuf for "buf" and save some things
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001407 aucmd_prepbuf(&aco, buf);
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001408
Bram Moolenaar758535a2016-03-30 22:06:16 +02001409 if (u_savesub(n) == OK)
1410 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001411 ml_replace(n, (char_u *)line, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001412 changed();
1413#ifdef SYNTAX_HL
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001414 syn_changed(n); // recompute syntax hl. for this line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001415#endif
1416 }
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001417
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001418 // restore curwin/curbuf and a few other things
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001419 aucmd_restbuf(&aco);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001420 // Careful: autocommands may have made "buf" invalid!
Bram Moolenaarf30e74c2006-08-16 17:35:00 +00001421
Bram Moolenaar071d4272004-06-13 20:20:40 +00001422 update_curbuf(NOT_VALID);
1423 }
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001424 else
1425 {
Bram Moolenaar165641d2010-02-17 16:23:09 +01001426 rb_raise(rb_eIndexError, "line number %ld out of range", (long)n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001427 }
1428 return str;
1429}
1430
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001431 static VALUE
1432buffer_aset(VALUE self, VALUE num, VALUE str)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001433{
1434 buf_T *buf = get_buf(self);
1435
1436 if (buf != NULL)
1437 return set_buffer_line(buf, (linenr_T)NUM2LONG(num), str);
1438 return str;
1439}
1440
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001441 static VALUE
1442buffer_delete(VALUE self, VALUE num)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001443{
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001444 buf_T *buf = get_buf(self);
1445 long n = NUM2LONG(num);
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001446 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001447
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001448 if (n > 0 && n <= buf->b_ml.ml_line_count)
1449 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001450 // set curwin/curbuf for "buf" and save some things
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001451 aucmd_prepbuf(&aco, buf);
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001452
Bram Moolenaar758535a2016-03-30 22:06:16 +02001453 if (u_savedel(n, 1) == OK)
1454 {
Bram Moolenaarca70c072020-05-30 20:30:46 +02001455 ml_delete(n);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001456
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001457 // Changes to non-active buffers should properly refresh
1458 // SegPhault - 01/09/05
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001459 deleted_lines_mark(n, 1L);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001460
Bram Moolenaar071d4272004-06-13 20:20:40 +00001461 changed();
1462 }
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001463
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001464 // restore curwin/curbuf and a few other things
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001465 aucmd_restbuf(&aco);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001466 // Careful: autocommands may have made "buf" invalid!
Bram Moolenaarf30e74c2006-08-16 17:35:00 +00001467
Bram Moolenaar071d4272004-06-13 20:20:40 +00001468 update_curbuf(NOT_VALID);
1469 }
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001470 else
1471 {
Bram Moolenaar165641d2010-02-17 16:23:09 +01001472 rb_raise(rb_eIndexError, "line number %ld out of range", n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001473 }
1474 return Qnil;
1475}
1476
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001477 static VALUE
1478buffer_append(VALUE self, VALUE num, VALUE str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001479{
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001480 buf_T *buf = get_buf(self);
Bram Moolenaar165641d2010-02-17 16:23:09 +01001481 char *line = StringValuePtr(str);
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001482 long n = NUM2LONG(num);
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001483 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001484
Bram Moolenaar3c531602010-11-16 14:46:19 +01001485 if (line == NULL)
1486 {
Bram Moolenaar165641d2010-02-17 16:23:09 +01001487 rb_raise(rb_eIndexError, "NULL line");
1488 }
1489 else if (n >= 0 && n <= buf->b_ml.ml_line_count)
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001490 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001491 // set curwin/curbuf for "buf" and save some things
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001492 aucmd_prepbuf(&aco, buf);
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001493
Bram Moolenaar758535a2016-03-30 22:06:16 +02001494 if (u_inssub(n + 1) == OK)
1495 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001496 ml_append(n, (char_u *) line, (colnr_T) 0, FALSE);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001497
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001498 // Changes to non-active buffers should properly refresh screen
1499 // SegPhault - 12/20/04
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001500 appended_lines_mark(n, 1L);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001501
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001502 changed();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001503 }
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001504
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001505 // restore curwin/curbuf and a few other things
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001506 aucmd_restbuf(&aco);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001507 // Careful: autocommands may have made "buf" invalid!
Bram Moolenaarf30e74c2006-08-16 17:35:00 +00001508
Bram Moolenaar071d4272004-06-13 20:20:40 +00001509 update_curbuf(NOT_VALID);
1510 }
Bram Moolenaar3c531602010-11-16 14:46:19 +01001511 else
1512 {
Bram Moolenaar165641d2010-02-17 16:23:09 +01001513 rb_raise(rb_eIndexError, "line number %ld out of range", n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001514 }
1515 return str;
1516}
1517
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001518#ifdef USE_TYPEDDATA
1519static size_t window_dsize(const void *buf);
1520
1521static const rb_data_type_t window_type = {
1522 "vim_window",
Bram Moolenaar41a41412020-01-07 21:32:19 +01001523 {0, 0, window_dsize,
1524# if RUBY_VERSION >= 27
Bram Moolenaar8b430b42020-02-22 15:01:00 +01001525 0, {0}
Bram Moolenaar41a41412020-01-07 21:32:19 +01001526# else
1527 {0, 0}
1528# endif
1529 },
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001530 0, 0,
1531# ifdef RUBY_TYPED_FREE_IMMEDIATELY
1532 0,
1533# endif
1534};
1535
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001536 static size_t
1537window_dsize(const void *win UNUSED)
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001538{
1539 return sizeof(win_T);
1540}
1541#endif
1542
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001543 static VALUE
1544window_new(win_T *win)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001545{
Bram Moolenaare344bea2005-09-01 20:46:49 +00001546 if (win->w_ruby_ref)
1547 {
1548 return (VALUE) win->w_ruby_ref;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001549 }
Bram Moolenaare344bea2005-09-01 20:46:49 +00001550 else
1551 {
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001552#ifdef USE_TYPEDDATA
1553 VALUE obj = TypedData_Wrap_Struct(cVimWindow, &window_type, win);
1554#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001555 VALUE obj = Data_Wrap_Struct(cVimWindow, 0, 0, win);
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001556#endif
Bram Moolenaare344bea2005-09-01 20:46:49 +00001557 win->w_ruby_ref = (void *) obj;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001558 rb_hash_aset(objtbl, rb_obj_id(obj), obj);
1559 return obj;
1560 }
1561}
1562
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001563 static win_T *
1564get_win(VALUE obj)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001565{
1566 win_T *win;
1567
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001568#ifdef USE_TYPEDDATA
1569 TypedData_Get_Struct(obj, win_T, &window_type, win);
1570#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001571 Data_Get_Struct(obj, win_T, win);
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001572#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001573 if (win == NULL)
1574 rb_raise(eDeletedWindowError, "attempt to refer to deleted window");
1575 return win;
1576}
1577
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001578 static VALUE
Bram Moolenaard5a986f2020-12-06 21:11:31 +01001579window_s_current(VALUE self UNUSED)
1580{
1581 return window_new(curwin);
1582}
1583
1584 static VALUE
1585window_s_current_getter(ID id UNUSED, VALUE *x UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001586{
1587 return window_new(curwin);
1588}
1589
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001590/*
1591 * Added line manipulation functions
1592 * SegPhault - 03/07/05
1593 */
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001594 static VALUE
Bram Moolenaard5a986f2020-12-06 21:11:31 +01001595line_s_current(VALUE self UNUSED)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001596{
1597 return get_buffer_line(curbuf, curwin->w_cursor.lnum);
1598}
1599
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001600 static VALUE
1601set_current_line(VALUE self UNUSED, VALUE str)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001602{
1603 return set_buffer_line(curbuf, curwin->w_cursor.lnum, str);
1604}
1605
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001606 static VALUE
Bram Moolenaard5a986f2020-12-06 21:11:31 +01001607current_line_number(VALUE self UNUSED)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001608{
1609 return INT2FIX((int)curwin->w_cursor.lnum);
1610}
1611
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001612 static VALUE
Bram Moolenaard5a986f2020-12-06 21:11:31 +01001613window_s_count(VALUE self UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001614{
Bram Moolenaar071d4272004-06-13 20:20:40 +00001615 win_T *w;
1616 int n = 0;
1617
Bram Moolenaar29323592016-07-24 22:04:11 +02001618 FOR_ALL_WINDOWS(w)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001619 n++;
1620 return INT2NUM(n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001621}
1622
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001623 static VALUE
1624window_s_aref(VALUE self UNUSED, VALUE num)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001625{
1626 win_T *w;
1627 int n = NUM2INT(num);
1628
Bram Moolenaar071d4272004-06-13 20:20:40 +00001629 for (w = firstwin; w != NULL; w = w->w_next, --n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001630 if (n == 0)
1631 return window_new(w);
1632 return Qnil;
1633}
1634
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001635 static VALUE
1636window_buffer(VALUE self)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001637{
1638 win_T *win = get_win(self);
1639
1640 return buffer_new(win->w_buffer);
1641}
1642
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001643 static VALUE
1644window_height(VALUE self)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001645{
1646 win_T *win = get_win(self);
1647
1648 return INT2NUM(win->w_height);
1649}
1650
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001651 static VALUE
1652window_set_height(VALUE self, VALUE height)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001653{
1654 win_T *win = get_win(self);
1655 win_T *savewin = curwin;
1656
1657 curwin = win;
1658 win_setheight(NUM2INT(height));
1659 curwin = savewin;
1660 return height;
1661}
1662
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001663 static VALUE
1664window_width(VALUE self UNUSED)
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001665{
Bram Moolenaare745d752017-09-22 16:56:22 +02001666 return INT2NUM(get_win(self)->w_width);
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001667}
1668
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001669 static VALUE
1670window_set_width(VALUE self UNUSED, VALUE width)
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001671{
1672 win_T *win = get_win(self);
1673 win_T *savewin = curwin;
1674
1675 curwin = win;
1676 win_setwidth(NUM2INT(width));
1677 curwin = savewin;
1678 return width;
1679}
1680
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001681 static VALUE
1682window_cursor(VALUE self)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001683{
1684 win_T *win = get_win(self);
1685
1686 return rb_assoc_new(INT2NUM(win->w_cursor.lnum), INT2NUM(win->w_cursor.col));
1687}
1688
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001689 static VALUE
1690window_set_cursor(VALUE self, VALUE pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001691{
1692 VALUE lnum, col;
1693 win_T *win = get_win(self);
1694
1695 Check_Type(pos, T_ARRAY);
Bram Moolenaar165641d2010-02-17 16:23:09 +01001696 if (RARRAY_LEN(pos) != 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001697 rb_raise(rb_eArgError, "array length must be 2");
Bram Moolenaar165641d2010-02-17 16:23:09 +01001698 lnum = RARRAY_PTR(pos)[0];
1699 col = RARRAY_PTR(pos)[1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001700 win->w_cursor.lnum = NUM2LONG(lnum);
1701 win->w_cursor.col = NUM2UINT(col);
Bram Moolenaar53901442018-07-25 22:02:36 +02001702 win->w_set_curswant = TRUE;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001703 check_cursor(); // put cursor on an existing line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001704 update_screen(NOT_VALID);
1705 return Qnil;
1706}
1707
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001708 static VALUE
1709f_nop(VALUE self UNUSED)
Bram Moolenaar35df7d22012-04-20 18:05:47 +02001710{
1711 return Qnil;
1712}
1713
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001714 static VALUE
1715f_p(int argc, VALUE *argv, VALUE self UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001716{
1717 int i;
1718 VALUE str = rb_str_new("", 0);
Bram Moolenaar51e9fbf2018-08-11 14:24:11 +02001719 VALUE ret = Qnil;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001720
Bram Moolenaar758535a2016-03-30 22:06:16 +02001721 for (i = 0; i < argc; i++)
1722 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001723 if (i > 0) rb_str_cat(str, ", ", 2);
1724 rb_str_concat(str, rb_inspect(argv[i]));
1725 }
Bram Moolenaar32526b32019-01-19 17:43:09 +01001726 msg(RSTRING_PTR(str));
Bram Moolenaar51e9fbf2018-08-11 14:24:11 +02001727
1728 if (argc == 1)
1729 ret = argv[0];
1730 else if (argc > 1)
1731 ret = rb_ary_new4(argc, argv);
1732 return ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001733}
1734
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001735 static void
1736ruby_io_init(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001737{
1738#ifndef DYNAMIC_RUBY
1739 RUBYEXTERN VALUE rb_stdout;
Bram Moolenaarb6c8cd82018-07-24 05:41:30 +02001740 RUBYEXTERN VALUE rb_stderr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001741#endif
1742
1743 rb_stdout = rb_obj_alloc(rb_cObject);
Bram Moolenaarb6c8cd82018-07-24 05:41:30 +02001744 rb_stderr = rb_obj_alloc(rb_cObject);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001745 rb_define_singleton_method(rb_stdout, "write", vim_message, 1);
Bram Moolenaar35df7d22012-04-20 18:05:47 +02001746 rb_define_singleton_method(rb_stdout, "flush", f_nop, 0);
Bram Moolenaarb6c8cd82018-07-24 05:41:30 +02001747 rb_define_singleton_method(rb_stderr, "write", vim_message, 1);
1748 rb_define_singleton_method(rb_stderr, "flush", f_nop, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001749 rb_define_global_function("p", f_p, -1);
1750}
1751
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001752 static void
1753ruby_vim_init(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001754{
1755 objtbl = rb_hash_new();
1756 rb_global_variable(&objtbl);
1757
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001758 // The Vim module used to be called "VIM", but "Vim" is better. Make an
1759 // alias "VIM" for backwards compatibility.
Bram Moolenaarf711faf2007-05-10 16:48:19 +00001760 mVIM = rb_define_module("Vim");
1761 rb_define_const(rb_cObject, "VIM", mVIM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001762 rb_define_const(mVIM, "VERSION_MAJOR", INT2NUM(VIM_VERSION_MAJOR));
1763 rb_define_const(mVIM, "VERSION_MINOR", INT2NUM(VIM_VERSION_MINOR));
1764 rb_define_const(mVIM, "VERSION_BUILD", INT2NUM(VIM_VERSION_BUILD));
1765 rb_define_const(mVIM, "VERSION_PATCHLEVEL", INT2NUM(VIM_VERSION_PATCHLEVEL));
1766 rb_define_const(mVIM, "VERSION_SHORT", rb_str_new2(VIM_VERSION_SHORT));
1767 rb_define_const(mVIM, "VERSION_MEDIUM", rb_str_new2(VIM_VERSION_MEDIUM));
1768 rb_define_const(mVIM, "VERSION_LONG", rb_str_new2(VIM_VERSION_LONG));
1769 rb_define_const(mVIM, "VERSION_LONG_DATE", rb_str_new2(VIM_VERSION_LONG_DATE));
1770 rb_define_module_function(mVIM, "message", vim_message, 1);
1771 rb_define_module_function(mVIM, "set_option", vim_set_option, 1);
1772 rb_define_module_function(mVIM, "command", vim_command, 1);
1773 rb_define_module_function(mVIM, "evaluate", vim_evaluate, 1);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001774 rb_define_module_function(mVIM, "blob", vim_blob, 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001775
1776 eDeletedBufferError = rb_define_class_under(mVIM, "DeletedBufferError",
1777 rb_eStandardError);
1778 eDeletedWindowError = rb_define_class_under(mVIM, "DeletedWindowError",
1779 rb_eStandardError);
1780
1781 cBuffer = rb_define_class_under(mVIM, "Buffer", rb_cObject);
1782 rb_define_singleton_method(cBuffer, "current", buffer_s_current, 0);
1783 rb_define_singleton_method(cBuffer, "count", buffer_s_count, 0);
1784 rb_define_singleton_method(cBuffer, "[]", buffer_s_aref, 1);
1785 rb_define_method(cBuffer, "name", buffer_name, 0);
1786 rb_define_method(cBuffer, "number", buffer_number, 0);
1787 rb_define_method(cBuffer, "count", buffer_count, 0);
1788 rb_define_method(cBuffer, "length", buffer_count, 0);
1789 rb_define_method(cBuffer, "[]", buffer_aref, 1);
1790 rb_define_method(cBuffer, "[]=", buffer_aset, 2);
1791 rb_define_method(cBuffer, "delete", buffer_delete, 1);
1792 rb_define_method(cBuffer, "append", buffer_append, 2);
1793
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001794 // Added line manipulation functions
1795 // SegPhault - 03/07/05
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001796 rb_define_method(cBuffer, "line_number", current_line_number, 0);
1797 rb_define_method(cBuffer, "line", line_s_current, 0);
1798 rb_define_method(cBuffer, "line=", set_current_line, 1);
1799
1800
Bram Moolenaar071d4272004-06-13 20:20:40 +00001801 cVimWindow = rb_define_class_under(mVIM, "Window", rb_cObject);
1802 rb_define_singleton_method(cVimWindow, "current", window_s_current, 0);
1803 rb_define_singleton_method(cVimWindow, "count", window_s_count, 0);
1804 rb_define_singleton_method(cVimWindow, "[]", window_s_aref, 1);
1805 rb_define_method(cVimWindow, "buffer", window_buffer, 0);
1806 rb_define_method(cVimWindow, "height", window_height, 0);
1807 rb_define_method(cVimWindow, "height=", window_set_height, 1);
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001808 rb_define_method(cVimWindow, "width", window_width, 0);
1809 rb_define_method(cVimWindow, "width=", window_set_width, 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001810 rb_define_method(cVimWindow, "cursor", window_cursor, 0);
1811 rb_define_method(cVimWindow, "cursor=", window_set_cursor, 1);
1812
Bram Moolenaard5a986f2020-12-06 21:11:31 +01001813 rb_define_virtual_variable("$curbuf", buffer_s_current_getter, 0);
1814 rb_define_virtual_variable("$curwin", window_s_current_getter, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001815}
Bram Moolenaar99685e62013-05-11 13:56:18 +02001816
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001817 void
1818vim_ruby_init(void *stack_start)
Bram Moolenaar99685e62013-05-11 13:56:18 +02001819{
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001820 // should get machine stack start address early in main function
Bram Moolenaar99685e62013-05-11 13:56:18 +02001821 ruby_stack_start = stack_start;
1822}
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001823
1824 static int
1825convert_hash2dict(VALUE key, VALUE val, VALUE arg)
1826{
1827 dict_T *d = (dict_T *)arg;
1828 dictitem_T *di;
1829
1830 di = dictitem_alloc((char_u *)RSTRING_PTR(RSTRING(rb_obj_as_string(key))));
1831 if (di == NULL || ruby_convert_to_vim_value(val, &di->di_tv) != OK
1832 || dict_add(d, di) != OK)
1833 {
1834 d->dv_hashtab.ht_error = TRUE;
1835 return ST_STOP;
1836 }
1837 return ST_CONTINUE;
1838}
1839
1840 static int
1841ruby_convert_to_vim_value(VALUE val, typval_T *rettv)
1842{
1843 switch (TYPE(val))
1844 {
1845 case T_NIL:
1846 rettv->v_type = VAR_SPECIAL;
1847 rettv->vval.v_number = VVAL_NULL;
1848 break;
1849 case T_TRUE:
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_TRUE;
1852 break;
1853 case T_FALSE:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01001854 rettv->v_type = VAR_BOOL;
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001855 rettv->vval.v_number = VVAL_FALSE;
1856 break;
1857 case T_BIGNUM:
1858 case T_FIXNUM:
1859 rettv->v_type = VAR_NUMBER;
1860 rettv->vval.v_number = (varnumber_T)NUM2LONG(val);
1861 break;
1862#ifdef FEAT_FLOAT
1863 case T_FLOAT:
1864 rettv->v_type = VAR_FLOAT;
1865 rettv->vval.v_float = (float_T)NUM2DBL(val);
1866 break;
1867#endif
1868 default:
1869 val = rb_obj_as_string(val);
1870 // FALLTHROUGH
1871 case T_STRING:
1872 {
1873 VALUE str = (VALUE)RSTRING(val);
1874
1875 rettv->v_type = VAR_STRING;
1876 rettv->vval.v_string = vim_strnsave((char_u *)RSTRING_PTR(str),
Bram Moolenaar71ccd032020-06-12 22:59:11 +02001877 RSTRING_LEN(str));
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001878 }
1879 break;
1880 case T_ARRAY:
1881 {
1882 list_T *l;
1883 long i;
1884 typval_T v;
1885
1886 l = list_alloc();
1887 if (l == NULL)
1888 return FAIL;
1889
1890 for (i = 0; i < RARRAY_LEN(val); ++i)
1891 {
1892 if (ruby_convert_to_vim_value((VALUE)RARRAY_PTR(val)[i],
1893 &v) != OK)
1894 {
1895 list_unref(l);
1896 return FAIL;
1897 }
1898 list_append_tv(l, &v);
1899 clear_tv(&v);
1900 }
1901
1902 rettv->v_type = VAR_LIST;
1903 rettv->vval.v_list = l;
1904 ++l->lv_refcount;
1905 }
1906 break;
1907 case T_HASH:
1908 {
1909 dict_T *d;
1910
1911 d = dict_alloc();
1912 if (d == NULL)
1913 return FAIL;
1914
1915 rb_hash_foreach(val, convert_hash2dict, (VALUE)d);
1916 if (d->dv_hashtab.ht_error)
1917 {
1918 dict_unref(d);
1919 return FAIL;
1920 }
1921
1922 rettv->v_type = VAR_DICT;
1923 rettv->vval.v_dict = d;
1924 ++d->dv_refcount;
1925 }
1926 break;
1927 }
1928 return OK;
1929}
1930
1931 void
1932do_rubyeval(char_u *str, typval_T *rettv)
1933{
1934 int retval = FAIL;
1935
1936 if (ensure_ruby_initialized())
1937 {
1938 int state;
1939 VALUE obj;
1940
1941 obj = rb_eval_string_protect((const char *)str, &state);
1942 if (state)
1943 error_print(state);
1944 else
1945 retval = ruby_convert_to_vim_value(obj, rettv);
1946 }
1947 if (retval == FAIL)
1948 {
1949 rettv->v_type = VAR_NUMBER;
1950 rettv->vval.v_number = 0;
1951 }
1952}