blob: 0d7eaf2e89e0b66bb4d1ac2833173396f6187cb8 [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 Moolenaardace9f72020-12-28 15:07:45 +010035// suggested by Ariya Mizutani
36#if (_MSC_VER == 1200)
37# undef _WIN32_WINNT
Bram Moolenaar06469e92016-06-11 22:26:53 +020038#endif
39
Bram Moolenaarf9b5ef82010-09-29 13:02:53 +020040#ifdef DYNAMIC_RUBY
Bram Moolenaar071d4272004-06-13 20:20:40 +000041/*
42 * This is tricky. In ruby.h there is (inline) function rb_class_of()
43 * definition. This function use these variables. But we want function to
44 * use dll_* variables.
45 */
Bram Moolenaardace9f72020-12-28 15:07:45 +010046# if RUBY_VERSION >= 24
47# define USE_RUBY_INTEGER
48# endif
49
Bram Moolenaar071d4272004-06-13 20:20:40 +000050# define rb_cFalseClass (*dll_rb_cFalseClass)
51# define rb_cFixnum (*dll_rb_cFixnum)
Bram Moolenaar2016ae52016-06-13 20:08:43 +020052# if defined(USE_RUBY_INTEGER)
53# define rb_cInteger (*dll_rb_cInteger)
Bram Moolenaar06469e92016-06-11 22:26:53 +020054# endif
Bram Moolenaar41a41412020-01-07 21:32:19 +010055# if RUBY_VERSION >= 20
Bram Moolenaardb3fbe52013-03-07 15:16:21 +010056# define rb_cFloat (*dll_rb_cFloat)
57# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000058# define rb_cNilClass (*dll_rb_cNilClass)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010059# define rb_cString (*dll_rb_cString)
Bram Moolenaar071d4272004-06-13 20:20:40 +000060# define rb_cSymbol (*dll_rb_cSymbol)
61# define rb_cTrueClass (*dll_rb_cTrueClass)
Bram Moolenaardace9f72020-12-28 15:07:45 +010062
Bram Moolenaar41a41412020-01-07 21:32:19 +010063# if RUBY_VERSION >= 18
Bram Moolenaar071d4272004-06-13 20:20:40 +000064/*
Bram Moolenaar42d57f02010-03-10 12:47:00 +010065 * On ver 1.8, all Ruby functions are exported with "__declspec(dllimport)"
66 * in ruby.h. But it causes trouble for these variables, because it is
Bram Moolenaar071d4272004-06-13 20:20:40 +000067 * defined in this file. When defined this RUBY_EXPORT it modified to
68 * "extern" and be able to avoid this problem.
69 */
70# define RUBY_EXPORT
71# endif
Bram Moolenaarf9b5ef82010-09-29 13:02:53 +020072
Bram Moolenaardace9f72020-12-28 15:07:45 +010073# if RUBY_VERSION >= 19
Bram Moolenaar2ab2e862019-12-04 21:24:53 +010074// Ruby 1.9 defines a number of static functions which use rb_num2long and
75// rb_int2big
Bram Moolenaardace9f72020-12-28 15:07:45 +010076# define rb_num2long rb_num2long_stub
77# define rb_int2big rb_int2big_stub
Bram Moolenaar42d57f02010-03-10 12:47:00 +010078
Bram Moolenaardace9f72020-12-28 15:07:45 +010079# if RUBY_VERSION >= 30 || VIM_SIZEOF_INT < VIM_SIZEOF_LONG
Bram Moolenaar2ab2e862019-12-04 21:24:53 +010080// Ruby 1.9 defines a number of static functions which use rb_fix2int and
81// rb_num2int if VIM_SIZEOF_INT < VIM_SIZEOF_LONG (64bit)
Bram Moolenaardace9f72020-12-28 15:07:45 +010082# define rb_fix2int rb_fix2int_stub
83# define rb_num2int rb_num2int_stub
84# endif
85# endif
Bram Moolenaar0bcdd6e2013-04-14 16:19:03 +020086
Bram Moolenaardace9f72020-12-28 15:07:45 +010087# if RUBY_VERSION == 21
Bram Moolenaar2ab2e862019-12-04 21:24:53 +010088// Ruby 2.1 adds new GC called RGenGC and RARRAY_PTR uses
89// rb_gc_writebarrier_unprotect_promoted if USE_RGENGC
Bram Moolenaardace9f72020-12-28 15:07:45 +010090# define rb_gc_writebarrier_unprotect_promoted rb_gc_writebarrier_unprotect_promoted_stub
91# endif
Bram Moolenaara1a118b2014-02-05 22:41:15 +010092
Bram Moolenaardace9f72020-12-28 15:07:45 +010093# if RUBY_VERSION >= 22
94# define rb_gc_writebarrier_unprotect rb_gc_writebarrier_unprotect_stub
95# endif
96
97# if RUBY_VERSION >= 26
98# define rb_ary_detransient rb_ary_detransient_stub
99# endif
100
101# if RUBY_VERSION >= 30
102# define rb_check_type rb_check_type_stub
103# define rb_num2uint rb_num2uint_stub
104# define ruby_malloc_size_overflow ruby_malloc_size_overflow_stub
105# endif
106
107#endif // ifdef DYNAMIC_RUBY
Bram Moolenaarb09c6842018-12-27 22:11:01 +0100108
Bram Moolenaar81ea1df2020-04-11 18:01:41 +0200109// On macOS pre-installed Ruby defines "SIZEOF_TIME_T" as "SIZEOF_LONG" so it
Bram Moolenaar92c098d2020-05-26 20:09:11 +0200110// conflicts with the definition in config.h then causes a macro-redefined
111// warning.
Bram Moolenaar81ea1df2020-04-11 18:01:41 +0200112#ifdef SIZEOF_TIME_T
113# undef SIZEOF_TIME_T
114#endif
115
Bram Moolenaar071d4272004-06-13 20:20:40 +0000116#include <ruby.h>
Bram Moolenaar41a41412020-01-07 21:32:19 +0100117#if RUBY_VERSION >= 19
Bram Moolenaar165641d2010-02-17 16:23:09 +0100118# include <ruby/encoding.h>
119#endif
Bram Moolenaar41a41412020-01-07 21:32:19 +0100120#if RUBY_VERSION <= 18
Bram Moolenaar8dc4c722019-04-14 19:42:13 +0200121# include <st.h> // for ST_STOP and ST_CONTINUE
122#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000123
Bram Moolenaar92c098d2020-05-26 20:09:11 +0200124// See above.
125#ifdef SIZEOF_TIME_T
126# undef SIZEOF_TIME_T
127#endif
128
Bram Moolenaar8dc4c722019-04-14 19:42:13 +0200129#undef off_t // ruby defines off_t as _int64, Mingw uses long
Bram Moolenaar071d4272004-06-13 20:20:40 +0000130#undef EXTERN
131#undef _
132
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100133// T_DATA defined both by Ruby and Mac header files, hack around it...
Bram Moolenaard0573012017-10-28 21:11:06 +0200134#if defined(MACOS_X)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000135# define __OPENTRANSPORT__
136# define __OPENTRANSPORTPROTOCOL__
137# define __OPENTRANSPORTPROVIDERS__
138#endif
139
Bram Moolenaar165641d2010-02-17 16:23:09 +0100140/*
Bram Moolenaar0d27f642015-12-28 22:05:28 +0100141 * The TypedData_XXX macro family can be used since Ruby 1.9.2 but
142 * rb_data_type_t changed in 1.9.3, therefore require at least 2.0.
143 * The old Data_XXX macro family was deprecated on Ruby 2.2.
Bram Moolenaarf2f6d292015-12-28 20:57:10 +0100144 * Use TypedData_XXX if available.
145 */
Bram Moolenaar41a41412020-01-07 21:32:19 +0100146#if defined(TypedData_Wrap_Struct) && (RUBY_VERSION >= 20)
Bram Moolenaarf2f6d292015-12-28 20:57:10 +0100147# define USE_TYPEDDATA 1
148#endif
149
150/*
Bram Moolenaar84a05ac2013-05-06 04:24:17 +0200151 * Backward compatibility for Ruby 1.8 and earlier.
Bram Moolenaar165641d2010-02-17 16:23:09 +0100152 * Ruby 1.9 does not provide STR2CSTR, instead StringValuePtr is provided.
153 * Ruby 1.9 does not provide RXXX(s)->len and RXXX(s)->ptr, instead
154 * RXXX_LEN(s) and RXXX_PTR(s) are provided.
155 */
156#ifndef StringValuePtr
157# define StringValuePtr(s) STR2CSTR(s)
158#endif
159#ifndef RARRAY_LEN
160# define RARRAY_LEN(s) RARRAY(s)->len
161#endif
162#ifndef RARRAY_PTR
163# define RARRAY_PTR(s) RARRAY(s)->ptr
164#endif
165#ifndef RSTRING_LEN
166# define RSTRING_LEN(s) RSTRING(s)->len
167#endif
168#ifndef RSTRING_PTR
169# define RSTRING_PTR(s) RSTRING(s)->ptr
170#endif
171
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100172#ifdef HAVE_DUP
173# undef HAVE_DUP
174#endif
175
Bram Moolenaar071d4272004-06-13 20:20:40 +0000176#include "vim.h"
177#include "version.h"
178
Bram Moolenaar7dca2eb2019-02-18 20:42:50 +0100179#ifdef DYNAMIC_RUBY
180# if !defined(MSWIN) // must come after including vim.h, where it is defined
181# include <dlfcn.h>
182# define HINSTANCE void*
183# define RUBY_PROC void*
184# define load_dll(n) dlopen((n), RTLD_LAZY|RTLD_GLOBAL)
185# define symbol_from_dll dlsym
186# define close_dll dlclose
Martin Tournoij1a3e5742021-07-24 13:57:29 +0200187# define load_dll_error dlerror
Bram Moolenaar7dca2eb2019-02-18 20:42:50 +0100188# else
189# define RUBY_PROC FARPROC
190# define load_dll vimLoadLib
191# define symbol_from_dll GetProcAddress
192# define close_dll FreeLibrary
Martin Tournoij1a3e5742021-07-24 13:57:29 +0200193# define load_dll_error GetWin32Error
Bram Moolenaar7dca2eb2019-02-18 20:42:50 +0100194# endif
195#endif
196
Bram Moolenaar071d4272004-06-13 20:20:40 +0000197#if defined(PROTO) && !defined(FEAT_RUBY)
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100198// Define these to be able to generate the function prototypes.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000199# define VALUE int
200# define RUBY_DATA_FUNC int
201#endif
202
203static int ruby_initialized = 0;
Bram Moolenaar99685e62013-05-11 13:56:18 +0200204static void *ruby_stack_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000205static VALUE objtbl;
206
207static VALUE mVIM;
208static VALUE cBuffer;
209static VALUE cVimWindow;
210static VALUE eDeletedBufferError;
211static VALUE eDeletedWindowError;
212
213static int ensure_ruby_initialized(void);
214static void error_print(int);
215static void ruby_io_init(void);
216static void ruby_vim_init(void);
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100217static int ruby_convert_to_vim_value(VALUE val, typval_T *rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000218
Bram Moolenaar41a41412020-01-07 21:32:19 +0100219#if (RUBY_VERSION >= 19) || defined(RUBY_INIT_STACK)
Bram Moolenaar10f3a792013-05-20 12:52:29 +0200220# if defined(__ia64) && !defined(ruby_init_stack)
221# define ruby_init_stack(addr) ruby_init_stack((addr), rb_ia64_bsp())
222# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000223#endif
224
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200225#if defined(DYNAMIC_RUBY) || defined(PROTO)
Bram Moolenaaref269542016-01-19 13:22:12 +0100226# if defined(PROTO) && !defined(HINSTANCE)
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100227# define HINSTANCE int // for generating prototypes
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200228# endif
229
Bram Moolenaar071d4272004-06-13 20:20:40 +0000230/*
231 * Wrapper defines
232 */
Bram Moolenaar8b430b42020-02-22 15:01:00 +0100233// Ruby 2.7 actually expands the following symbols as macro.
234# if RUBY_VERSION >= 27
235# undef rb_define_global_function
236# undef rb_define_method
237# undef rb_define_module_function
238# undef rb_define_singleton_method
239# endif
240
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200241# define rb_assoc_new dll_rb_assoc_new
242# define rb_cObject (*dll_rb_cObject)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100243# define rb_class_new_instance dll_rb_class_new_instance
Bram Moolenaardace9f72020-12-28 15:07:45 +0100244# if RUBY_VERSION < 30
245# define rb_check_type dll_rb_check_type
246# endif
Bram Moolenaarf2f6d292015-12-28 20:57:10 +0100247# ifdef USE_TYPEDDATA
248# define rb_check_typeddata dll_rb_check_typeddata
Bram Moolenaar0c7485f2015-01-14 14:04:10 +0100249# endif
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200250# define rb_class_path dll_rb_class_path
Bram Moolenaarf2f6d292015-12-28 20:57:10 +0100251# ifdef USE_TYPEDDATA
Bram Moolenaar41a41412020-01-07 21:32:19 +0100252# if RUBY_VERSION >= 23
Bram Moolenaarf2f6d292015-12-28 20:57:10 +0100253# define rb_data_typed_object_wrap dll_rb_data_typed_object_wrap
254# else
255# define rb_data_typed_object_alloc dll_rb_data_typed_object_alloc
256# endif
257# else
258# define rb_data_object_alloc dll_rb_data_object_alloc
259# endif
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200260# define rb_define_class_under dll_rb_define_class_under
261# define rb_define_const dll_rb_define_const
262# define rb_define_global_function dll_rb_define_global_function
263# define rb_define_method dll_rb_define_method
264# define rb_define_module dll_rb_define_module
265# define rb_define_module_function dll_rb_define_module_function
266# define rb_define_singleton_method dll_rb_define_singleton_method
267# define rb_define_virtual_variable dll_rb_define_virtual_variable
268# define rb_stdout (*dll_rb_stdout)
Bram Moolenaarb6c8cd82018-07-24 05:41:30 +0200269# define rb_stderr (*dll_rb_stderr)
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200270# define rb_eArgError (*dll_rb_eArgError)
271# define rb_eIndexError (*dll_rb_eIndexError)
272# define rb_eRuntimeError (*dll_rb_eRuntimeError)
273# define rb_eStandardError (*dll_rb_eStandardError)
274# define rb_eval_string_protect dll_rb_eval_string_protect
Bram Moolenaar41a41412020-01-07 21:32:19 +0100275# if RUBY_VERSION >= 21
Bram Moolenaarf711cb22018-08-01 18:42:13 +0200276# define rb_funcallv dll_rb_funcallv
277# else
278# define rb_funcall2 dll_rb_funcall2
279# endif
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200280# define rb_global_variable dll_rb_global_variable
281# define rb_hash_aset dll_rb_hash_aset
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100282# define rb_hash_foreach dll_rb_hash_foreach
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200283# define rb_hash_new dll_rb_hash_new
284# define rb_inspect dll_rb_inspect
285# define rb_int2inum dll_rb_int2inum
Bram Moolenaar218beb32018-08-04 17:24:44 +0200286
287// ruby.h may redefine rb_intern to use RUBY_CONST_ID_CACHE(), but that won't
288// work. Not using the cache appears to be the best solution.
289# undef rb_intern
290# define rb_intern dll_rb_intern
291
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100292# if VIM_SIZEOF_INT < VIM_SIZEOF_LONG // 64 bits only
Bram Moolenaar41a41412020-01-07 21:32:19 +0100293# if RUBY_VERSION <= 18
Bram Moolenaar498af702014-03-28 21:58:21 +0100294# define rb_fix2int dll_rb_fix2int
295# define rb_num2int dll_rb_num2int
296# endif
Bram Moolenaardace9f72020-12-28 15:07:45 +0100297# if RUBY_VERSION < 30
298# define rb_num2uint dll_rb_num2uint
299# endif
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200300# endif
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100301# define rb_num2dbl dll_rb_num2dbl
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200302# define rb_lastline_get dll_rb_lastline_get
303# define rb_lastline_set dll_rb_lastline_set
Bram Moolenaard0573012017-10-28 21:11:06 +0200304# define rb_protect dll_rb_protect
305# define rb_load dll_rb_load
Bram Moolenaar41a41412020-01-07 21:32:19 +0100306# if RUBY_VERSION <= 18
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200307# define rb_num2long dll_rb_num2long
308# endif
Bram Moolenaar41a41412020-01-07 21:32:19 +0100309# if RUBY_VERSION <= 19
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200310# define rb_num2ulong dll_rb_num2ulong
311# endif
312# define rb_obj_alloc dll_rb_obj_alloc
313# define rb_obj_as_string dll_rb_obj_as_string
314# define rb_obj_id dll_rb_obj_id
315# define rb_raise dll_rb_raise
316# define rb_str_cat dll_rb_str_cat
317# define rb_str_concat dll_rb_str_concat
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100318# undef rb_str_new
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200319# define rb_str_new dll_rb_str_new
320# ifdef rb_str_new2
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100321// Ruby may #define rb_str_new2 to use rb_str_new_cstr.
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200322# define need_rb_str_new_cstr 1
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100323// Ruby's headers #define rb_str_new_cstr to make use of GCC's
324// __builtin_constant_p extension.
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200325# undef rb_str_new_cstr
326# define rb_str_new_cstr dll_rb_str_new_cstr
Bram Moolenaar76a86062013-05-11 17:45:48 +0200327# else
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200328# define rb_str_new2 dll_rb_str_new2
Bram Moolenaar76a86062013-05-11 17:45:48 +0200329# endif
Bram Moolenaar41a41412020-01-07 21:32:19 +0100330# if RUBY_VERSION >= 18
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200331# define rb_string_value dll_rb_string_value
332# define rb_string_value_ptr dll_rb_string_value_ptr
333# define rb_float_new dll_rb_float_new
334# define rb_ary_new dll_rb_ary_new
Bram Moolenaar51e9fbf2018-08-11 14:24:11 +0200335# ifdef rb_ary_new4
Bram Moolenaar49c99fc2020-02-11 23:01:39 +0100336# define RB_ARY_NEW4_MACRO 1
337# undef rb_ary_new4
Bram Moolenaar51e9fbf2018-08-11 14:24:11 +0200338# endif
339# define rb_ary_new4 dll_rb_ary_new4
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200340# define rb_ary_push dll_rb_ary_push
Bram Moolenaar41a41412020-01-07 21:32:19 +0100341# if (RUBY_VERSION >= 19) || defined(RUBY_INIT_STACK)
Bram Moolenaar10f3a792013-05-20 12:52:29 +0200342# ifdef __ia64
343# define rb_ia64_bsp dll_rb_ia64_bsp
344# undef ruby_init_stack
345# define ruby_init_stack(addr) dll_ruby_init_stack((addr), rb_ia64_bsp())
346# else
347# define ruby_init_stack dll_ruby_init_stack
348# endif
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200349# endif
350# else
351# define rb_str2cstr dll_rb_str2cstr
Bram Moolenaar3ca71f12010-10-27 16:49:47 +0200352# endif
Bram Moolenaar41a41412020-01-07 21:32:19 +0100353# if RUBY_VERSION >= 19
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200354# define rb_errinfo dll_rb_errinfo
355# else
356# define ruby_errinfo (*dll_ruby_errinfo)
357# endif
358# define ruby_init dll_ruby_init
359# define ruby_init_loadpath dll_ruby_init_loadpath
Bram Moolenaar4f974752019-02-17 17:44:42 +0100360# ifdef MSWIN
Bram Moolenaar41a41412020-01-07 21:32:19 +0100361# if RUBY_VERSION >= 19
Bram Moolenaar9d5c84a2018-12-19 20:48:46 +0100362# define ruby_sysinit dll_ruby_sysinit
363# else
364# define NtInitialize dll_NtInitialize
365# endif
Bram Moolenaar41a41412020-01-07 21:32:19 +0100366# if RUBY_VERSION >= 18
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200367# define rb_w32_snprintf dll_rb_w32_snprintf
368# endif
369# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000370
Bram Moolenaar41a41412020-01-07 21:32:19 +0100371# if RUBY_VERSION >= 19
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200372# define ruby_script dll_ruby_script
373# define rb_enc_find_index dll_rb_enc_find_index
374# define rb_enc_find dll_rb_enc_find
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100375# undef rb_enc_str_new
376# define rb_enc_str_new dll_rb_enc_str_new
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200377# define rb_sprintf dll_rb_sprintf
378# define rb_require dll_rb_require
Bram Moolenaar9b1067e2015-11-19 19:33:15 +0100379# define ruby_options dll_ruby_options
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200380# endif
Bram Moolenaar165641d2010-02-17 16:23:09 +0100381
Bram Moolenaar071d4272004-06-13 20:20:40 +0000382/*
383 * Pointers for dynamic link
384 */
385static VALUE (*dll_rb_assoc_new) (VALUE, VALUE);
Bram Moolenaarba52cde2010-06-25 04:29:11 +0200386VALUE *dll_rb_cFalseClass;
387VALUE *dll_rb_cFixnum;
Bram Moolenaar2016ae52016-06-13 20:08:43 +0200388# if defined(USE_RUBY_INTEGER)
Bram Moolenaar06469e92016-06-11 22:26:53 +0200389VALUE *dll_rb_cInteger;
390# endif
Bram Moolenaar41a41412020-01-07 21:32:19 +0100391# if RUBY_VERSION >= 20
Bram Moolenaardb3fbe52013-03-07 15:16:21 +0100392VALUE *dll_rb_cFloat;
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200393# endif
Bram Moolenaarba52cde2010-06-25 04:29:11 +0200394VALUE *dll_rb_cNilClass;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000395static VALUE *dll_rb_cObject;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100396VALUE *dll_rb_cString;
Bram Moolenaarba52cde2010-06-25 04:29:11 +0200397VALUE *dll_rb_cSymbol;
398VALUE *dll_rb_cTrueClass;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100399static VALUE (*dll_rb_class_new_instance) (int,VALUE*,VALUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000400static void (*dll_rb_check_type) (VALUE,int);
Bram Moolenaarf2f6d292015-12-28 20:57:10 +0100401# ifdef USE_TYPEDDATA
402static void *(*dll_rb_check_typeddata) (VALUE,const rb_data_type_t *);
403# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000404static VALUE (*dll_rb_class_path) (VALUE);
Bram Moolenaarf2f6d292015-12-28 20:57:10 +0100405# ifdef USE_TYPEDDATA
Bram Moolenaar41a41412020-01-07 21:32:19 +0100406# if RUBY_VERSION >= 23
Bram Moolenaarf2f6d292015-12-28 20:57:10 +0100407static VALUE (*dll_rb_data_typed_object_wrap) (VALUE, void*, const rb_data_type_t *);
408# else
409static VALUE (*dll_rb_data_typed_object_alloc) (VALUE, void*, const rb_data_type_t *);
410# endif
411# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000412static VALUE (*dll_rb_data_object_alloc) (VALUE, void*, RUBY_DATA_FUNC, RUBY_DATA_FUNC);
Bram Moolenaarf2f6d292015-12-28 20:57:10 +0100413# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000414static VALUE (*dll_rb_define_class_under) (VALUE, const char*, VALUE);
415static void (*dll_rb_define_const) (VALUE,const char*,VALUE);
416static void (*dll_rb_define_global_function) (const char*,VALUE(*)(),int);
417static void (*dll_rb_define_method) (VALUE,const char*,VALUE(*)(),int);
418static VALUE (*dll_rb_define_module) (const char*);
419static void (*dll_rb_define_module_function) (VALUE,const char*,VALUE(*)(),int);
420static void (*dll_rb_define_singleton_method) (VALUE,const char*,VALUE(*)(),int);
421static void (*dll_rb_define_virtual_variable) (const char*,VALUE(*)(),void(*)());
422static VALUE *dll_rb_stdout;
Bram Moolenaarb6c8cd82018-07-24 05:41:30 +0200423static VALUE *dll_rb_stderr;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000424static VALUE *dll_rb_eArgError;
425static VALUE *dll_rb_eIndexError;
426static VALUE *dll_rb_eRuntimeError;
427static VALUE *dll_rb_eStandardError;
428static VALUE (*dll_rb_eval_string_protect) (const char*, int*);
Bram Moolenaar41a41412020-01-07 21:32:19 +0100429# if RUBY_VERSION >= 21
Bram Moolenaarf711cb22018-08-01 18:42:13 +0200430static VALUE (*dll_rb_funcallv) (VALUE, ID, int, const VALUE*);
431# else
432static VALUE (*dll_rb_funcall2) (VALUE, ID, int, const VALUE*);
433# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000434static void (*dll_rb_global_variable) (VALUE*);
435static VALUE (*dll_rb_hash_aset) (VALUE, VALUE, VALUE);
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100436static VALUE (*dll_rb_hash_foreach) (VALUE, int (*)(VALUE, VALUE, VALUE), VALUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000437static VALUE (*dll_rb_hash_new) (void);
438static VALUE (*dll_rb_inspect) (VALUE);
439static VALUE (*dll_rb_int2inum) (long);
Bram Moolenaarf711cb22018-08-01 18:42:13 +0200440static ID (*dll_rb_intern) (const char*);
Bram Moolenaar9d20daf2021-02-01 19:31:47 +0100441# if RUBY_VERSION >= 30 || VIM_SIZEOF_INT < VIM_SIZEOF_LONG
Bram Moolenaar2623b4f2012-09-18 16:36:32 +0200442static long (*dll_rb_fix2int) (VALUE);
443static long (*dll_rb_num2int) (VALUE);
444static unsigned long (*dll_rb_num2uint) (VALUE);
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200445# endif
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100446static double (*dll_rb_num2dbl) (VALUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000447static VALUE (*dll_rb_lastline_get) (void);
448static void (*dll_rb_lastline_set) (VALUE);
Bram Moolenaar37badc82018-01-31 20:15:30 +0100449static VALUE (*dll_rb_protect) (VALUE (*)(VALUE), VALUE, int*);
Bram Moolenaard0573012017-10-28 21:11:06 +0200450static void (*dll_rb_load) (VALUE, int);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000451static long (*dll_rb_num2long) (VALUE);
452static unsigned long (*dll_rb_num2ulong) (VALUE);
453static VALUE (*dll_rb_obj_alloc) (VALUE);
454static VALUE (*dll_rb_obj_as_string) (VALUE);
455static VALUE (*dll_rb_obj_id) (VALUE);
456static void (*dll_rb_raise) (VALUE, const char*, ...);
Bram Moolenaar41a41412020-01-07 21:32:19 +0100457# if RUBY_VERSION >= 18
Bram Moolenaarf9b5ef82010-09-29 13:02:53 +0200458static VALUE (*dll_rb_string_value) (volatile VALUE*);
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200459# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000460static char *(*dll_rb_str2cstr) (VALUE,int*);
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200461# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000462static VALUE (*dll_rb_str_cat) (VALUE, const char*, long);
463static VALUE (*dll_rb_str_concat) (VALUE, VALUE);
464static VALUE (*dll_rb_str_new) (const char*, long);
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200465# ifdef need_rb_str_new_cstr
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100466// Ruby may #define rb_str_new2 to use rb_str_new_cstr.
Bram Moolenaar1d2beae2010-05-22 21:56:55 +0200467static VALUE (*dll_rb_str_new_cstr) (const char*);
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200468# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000469static VALUE (*dll_rb_str_new2) (const char*);
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200470# endif
Bram Moolenaar41a41412020-01-07 21:32:19 +0100471# if RUBY_VERSION >= 19
Bram Moolenaar165641d2010-02-17 16:23:09 +0100472static VALUE (*dll_rb_errinfo) (void);
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200473# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000474static VALUE *dll_ruby_errinfo;
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200475# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000476static void (*dll_ruby_init) (void);
477static void (*dll_ruby_init_loadpath) (void);
Bram Moolenaar4f974752019-02-17 17:44:42 +0100478# ifdef MSWIN
Bram Moolenaar41a41412020-01-07 21:32:19 +0100479# if RUBY_VERSION >= 19
Bram Moolenaar4f391792017-01-15 16:59:07 +0100480static void (*dll_ruby_sysinit) (int*, char***);
Bram Moolenaar9d5c84a2018-12-19 20:48:46 +0100481# else
482static void (*dll_NtInitialize) (int*, char***);
483# endif
Bram Moolenaar41a41412020-01-07 21:32:19 +0100484# if RUBY_VERSION >= 18
Bram Moolenaar3ca71f12010-10-27 16:49:47 +0200485static int (*dll_rb_w32_snprintf)(char*, size_t, const char*, ...);
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200486# endif
Bram Moolenaar3ca71f12010-10-27 16:49:47 +0200487# endif
Bram Moolenaar41a41412020-01-07 21:32:19 +0100488# if RUBY_VERSION >= 18
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100489static char * (*dll_rb_string_value_ptr) (volatile VALUE*);
490static VALUE (*dll_rb_float_new) (double);
491static VALUE (*dll_rb_ary_new) (void);
Bram Moolenaar51e9fbf2018-08-11 14:24:11 +0200492static VALUE (*dll_rb_ary_new4) (long n, const VALUE *elts);
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100493static VALUE (*dll_rb_ary_push) (VALUE, VALUE);
Bram Moolenaar41a41412020-01-07 21:32:19 +0100494# if RUBY_VERSION >= 26
Bram Moolenaarb09c6842018-12-27 22:11:01 +0100495static void (*dll_rb_ary_detransient) (VALUE);
496# endif
Bram Moolenaar41a41412020-01-07 21:32:19 +0100497# if (RUBY_VERSION >= 19) || defined(RUBY_INIT_STACK)
Bram Moolenaar10f3a792013-05-20 12:52:29 +0200498# ifdef __ia64
Bram Moolenaar76a86062013-05-11 17:45:48 +0200499static void * (*dll_rb_ia64_bsp) (void);
500static void (*dll_ruby_init_stack)(VALUE*, void*);
Bram Moolenaar10f3a792013-05-20 12:52:29 +0200501# else
Bram Moolenaar76a86062013-05-11 17:45:48 +0200502static void (*dll_ruby_init_stack)(VALUE*);
Bram Moolenaar10f3a792013-05-20 12:52:29 +0200503# endif
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200504# endif
Bram Moolenaar76a86062013-05-11 17:45:48 +0200505# endif
Bram Moolenaar41a41412020-01-07 21:32:19 +0100506# if RUBY_VERSION >= 19
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100507static VALUE (*dll_rb_int2big)(SIGNED_VALUE);
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200508# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000509
Bram Moolenaar41a41412020-01-07 21:32:19 +0100510# if RUBY_VERSION >= 19
Bram Moolenaar165641d2010-02-17 16:23:09 +0100511static void (*dll_ruby_script) (const char*);
512static int (*dll_rb_enc_find_index) (const char*);
513static rb_encoding* (*dll_rb_enc_find) (const char*);
514static VALUE (*dll_rb_enc_str_new) (const char*, long, rb_encoding*);
515static VALUE (*dll_rb_sprintf) (const char*, ...);
Bram Moolenaar7a8ef142010-12-24 13:39:35 +0100516static VALUE (*dll_rb_require) (const char*);
Bram Moolenaardace9f72020-12-28 15:07:45 +0100517static void* (*dll_ruby_options)(int, char**);
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200518# endif
Bram Moolenaar165641d2010-02-17 16:23:09 +0100519
Bram Moolenaara1a118b2014-02-05 22:41:15 +0100520# if defined(USE_RGENGC) && USE_RGENGC
Bram Moolenaar41a41412020-01-07 21:32:19 +0100521# if RUBY_VERSION == 21
Bram Moolenaara1a118b2014-02-05 22:41:15 +0100522static void (*dll_rb_gc_writebarrier_unprotect_promoted)(VALUE);
Bram Moolenaar0c7485f2015-01-14 14:04:10 +0100523# else
524static void (*dll_rb_gc_writebarrier_unprotect)(VALUE obj);
525# endif
Bram Moolenaara1a118b2014-02-05 22:41:15 +0100526# endif
527
Bram Moolenaardace9f72020-12-28 15:07:45 +0100528# if RUBY_VERSION >= 30
Bram Moolenaar9d20daf2021-02-01 19:31:47 +0100529# ifdef _MSC_VER
530static void (*dll_ruby_malloc_size_overflow)(size_t, size_t);
531# else
Bram Moolenaardace9f72020-12-28 15:07:45 +0100532NORETURN(static void (*dll_ruby_malloc_size_overflow)(size_t, size_t));
Bram Moolenaar9d20daf2021-02-01 19:31:47 +0100533# endif
Bram Moolenaardace9f72020-12-28 15:07:45 +0100534# endif
535
Bram Moolenaar0e121402020-12-10 20:50:34 +0100536# if RUBY_VERSION >= 26
537void rb_ary_detransient_stub(VALUE x);
538# endif
539
Bram Moolenaardace9f72020-12-28 15:07:45 +0100540// Do not generate a prototype here, VALUE isn't always defined.
541# ifndef PROTO
542# if RUBY_VERSION >= 19
543# if RUBY_VERSION >= 22
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100544 long
545rb_num2long_stub(VALUE x)
Bram Moolenaardace9f72020-12-28 15:07:45 +0100546# else
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100547 SIGNED_VALUE
548rb_num2long_stub(VALUE x)
Bram Moolenaardace9f72020-12-28 15:07:45 +0100549# endif
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100550{
551 return dll_rb_num2long(x);
552}
Bram Moolenaardace9f72020-12-28 15:07:45 +0100553# if RUBY_VERSION >= 26
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100554 VALUE
555rb_int2big_stub(intptr_t x)
Bram Moolenaardace9f72020-12-28 15:07:45 +0100556# else
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100557 VALUE
558rb_int2big_stub(SIGNED_VALUE x)
Bram Moolenaardace9f72020-12-28 15:07:45 +0100559# endif
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100560{
561 return dll_rb_int2big(x);
562}
Bram Moolenaar9d20daf2021-02-01 19:31:47 +0100563# if RUBY_VERSION >= 30 || VIM_SIZEOF_INT < VIM_SIZEOF_LONG
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100564 long
565rb_fix2int_stub(VALUE x)
Bram Moolenaar0bcdd6e2013-04-14 16:19:03 +0200566{
567 return dll_rb_fix2int(x);
568}
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100569 long
570rb_num2int_stub(VALUE x)
Bram Moolenaar0bcdd6e2013-04-14 16:19:03 +0200571{
572 return dll_rb_num2int(x);
573}
Bram Moolenaardace9f72020-12-28 15:07:45 +0100574# endif
575# if RUBY_VERSION >= 20
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100576 VALUE
Bram Moolenaar886ed692013-02-26 13:41:35 +0100577rb_float_new_in_heap(double d)
578{
579 return dll_rb_float_new(d);
580}
Bram Moolenaardace9f72020-12-28 15:07:45 +0100581# if RUBY_VERSION >= 22
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100582 unsigned long
583rb_num2ulong(VALUE x)
Bram Moolenaardace9f72020-12-28 15:07:45 +0100584# else
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100585 VALUE
586rb_num2ulong(VALUE x)
Bram Moolenaardace9f72020-12-28 15:07:45 +0100587# endif
Bram Moolenaar886ed692013-02-26 13:41:35 +0100588{
589 return (long)RSHIFT((SIGNED_VALUE)(x),1);
590}
Bram Moolenaardace9f72020-12-28 15:07:45 +0100591# endif
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200592# endif
Bram Moolenaardace9f72020-12-28 15:07:45 +0100593# if defined(USE_RGENGC) && USE_RGENGC
594# if RUBY_VERSION == 21
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100595 void
596rb_gc_writebarrier_unprotect_promoted_stub(VALUE obj)
Bram Moolenaara1a118b2014-02-05 22:41:15 +0100597{
Bram Moolenaar90140742014-11-27 17:44:08 +0100598 dll_rb_gc_writebarrier_unprotect_promoted(obj);
Bram Moolenaara1a118b2014-02-05 22:41:15 +0100599}
Bram Moolenaardace9f72020-12-28 15:07:45 +0100600# else
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100601 void
602rb_gc_writebarrier_unprotect_stub(VALUE obj)
Bram Moolenaar0c7485f2015-01-14 14:04:10 +0100603{
604 dll_rb_gc_writebarrier_unprotect(obj);
605}
Bram Moolenaardace9f72020-12-28 15:07:45 +0100606# endif
Bram Moolenaar0c7485f2015-01-14 14:04:10 +0100607# endif
Bram Moolenaardace9f72020-12-28 15:07:45 +0100608# if RUBY_VERSION >= 26
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100609 void
610rb_ary_detransient_stub(VALUE x)
Bram Moolenaarf62fc312019-01-08 20:29:32 +0100611{
612 dll_rb_ary_detransient(x);
613}
Bram Moolenaardace9f72020-12-28 15:07:45 +0100614# endif
615# if RUBY_VERSION >= 30
616 void
617rb_check_type_stub(VALUE obj, int t)
618{
619 dll_rb_check_type(obj, t);
620}
621 unsigned long
622rb_num2uint_stub(VALUE x)
623{
624 return dll_rb_num2uint(x);
625}
626 void
627ruby_malloc_size_overflow_stub(size_t x, size_t y)
628{
629 dll_ruby_malloc_size_overflow(x, y);
630}
631# endif
632# endif // ifndef PROTO
Bram Moolenaarf62fc312019-01-08 20:29:32 +0100633
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100634static HINSTANCE hinstRuby = NULL; // Instance of ruby.dll
Bram Moolenaar071d4272004-06-13 20:20:40 +0000635
636/*
Bram Moolenaarda2303d2005-08-30 21:55:26 +0000637 * Table of name to function pointer of ruby.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000638 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000639static struct
640{
641 char *name;
642 RUBY_PROC *ptr;
643} ruby_funcname_table[] =
644{
645 {"rb_assoc_new", (RUBY_PROC*)&dll_rb_assoc_new},
646 {"rb_cFalseClass", (RUBY_PROC*)&dll_rb_cFalseClass},
Bram Moolenaar2016ae52016-06-13 20:08:43 +0200647# if defined(USE_RUBY_INTEGER)
Bram Moolenaar06469e92016-06-11 22:26:53 +0200648 {"rb_cInteger", (RUBY_PROC*)&dll_rb_cInteger},
Bram Moolenaar6abda992017-01-09 21:10:31 +0100649# else
650 {"rb_cFixnum", (RUBY_PROC*)&dll_rb_cFixnum},
Bram Moolenaar06469e92016-06-11 22:26:53 +0200651# endif
Bram Moolenaar41a41412020-01-07 21:32:19 +0100652# if RUBY_VERSION >= 20
Bram Moolenaardb3fbe52013-03-07 15:16:21 +0100653 {"rb_cFloat", (RUBY_PROC*)&dll_rb_cFloat},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200654# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000655 {"rb_cNilClass", (RUBY_PROC*)&dll_rb_cNilClass},
656 {"rb_cObject", (RUBY_PROC*)&dll_rb_cObject},
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100657 {"rb_cString", (RUBY_PROC*)&dll_rb_cString},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000658 {"rb_cSymbol", (RUBY_PROC*)&dll_rb_cSymbol},
659 {"rb_cTrueClass", (RUBY_PROC*)&dll_rb_cTrueClass},
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100660 {"rb_class_new_instance", (RUBY_PROC*)&dll_rb_class_new_instance},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000661 {"rb_check_type", (RUBY_PROC*)&dll_rb_check_type},
Bram Moolenaarf2f6d292015-12-28 20:57:10 +0100662# ifdef USE_TYPEDDATA
663 {"rb_check_typeddata", (RUBY_PROC*)&dll_rb_check_typeddata},
664# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000665 {"rb_class_path", (RUBY_PROC*)&dll_rb_class_path},
Bram Moolenaarf2f6d292015-12-28 20:57:10 +0100666# ifdef USE_TYPEDDATA
Bram Moolenaar41a41412020-01-07 21:32:19 +0100667# if RUBY_VERSION >= 23
Bram Moolenaarf2f6d292015-12-28 20:57:10 +0100668 {"rb_data_typed_object_wrap", (RUBY_PROC*)&dll_rb_data_typed_object_wrap},
669# else
670 {"rb_data_typed_object_alloc", (RUBY_PROC*)&dll_rb_data_typed_object_alloc},
671# endif
672# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000673 {"rb_data_object_alloc", (RUBY_PROC*)&dll_rb_data_object_alloc},
Bram Moolenaarf2f6d292015-12-28 20:57:10 +0100674# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000675 {"rb_define_class_under", (RUBY_PROC*)&dll_rb_define_class_under},
676 {"rb_define_const", (RUBY_PROC*)&dll_rb_define_const},
677 {"rb_define_global_function", (RUBY_PROC*)&dll_rb_define_global_function},
678 {"rb_define_method", (RUBY_PROC*)&dll_rb_define_method},
679 {"rb_define_module", (RUBY_PROC*)&dll_rb_define_module},
680 {"rb_define_module_function", (RUBY_PROC*)&dll_rb_define_module_function},
681 {"rb_define_singleton_method", (RUBY_PROC*)&dll_rb_define_singleton_method},
682 {"rb_define_virtual_variable", (RUBY_PROC*)&dll_rb_define_virtual_variable},
683 {"rb_stdout", (RUBY_PROC*)&dll_rb_stdout},
Bram Moolenaarb6c8cd82018-07-24 05:41:30 +0200684 {"rb_stderr", (RUBY_PROC*)&dll_rb_stderr},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000685 {"rb_eArgError", (RUBY_PROC*)&dll_rb_eArgError},
686 {"rb_eIndexError", (RUBY_PROC*)&dll_rb_eIndexError},
687 {"rb_eRuntimeError", (RUBY_PROC*)&dll_rb_eRuntimeError},
688 {"rb_eStandardError", (RUBY_PROC*)&dll_rb_eStandardError},
689 {"rb_eval_string_protect", (RUBY_PROC*)&dll_rb_eval_string_protect},
Bram Moolenaar41a41412020-01-07 21:32:19 +0100690# if RUBY_VERSION >= 21
Bram Moolenaarf711cb22018-08-01 18:42:13 +0200691 {"rb_funcallv", (RUBY_PROC*)&dll_rb_funcallv},
692# else
693 {"rb_funcall2", (RUBY_PROC*)&dll_rb_funcall2},
694# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000695 {"rb_global_variable", (RUBY_PROC*)&dll_rb_global_variable},
696 {"rb_hash_aset", (RUBY_PROC*)&dll_rb_hash_aset},
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100697 {"rb_hash_foreach", (RUBY_PROC*)&dll_rb_hash_foreach},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000698 {"rb_hash_new", (RUBY_PROC*)&dll_rb_hash_new},
699 {"rb_inspect", (RUBY_PROC*)&dll_rb_inspect},
700 {"rb_int2inum", (RUBY_PROC*)&dll_rb_int2inum},
Bram Moolenaarf711cb22018-08-01 18:42:13 +0200701 {"rb_intern", (RUBY_PROC*)&dll_rb_intern},
Bram Moolenaar9d20daf2021-02-01 19:31:47 +0100702# if RUBY_VERSION >= 30 || VIM_SIZEOF_INT < VIM_SIZEOF_LONG
Bram Moolenaar2623b4f2012-09-18 16:36:32 +0200703 {"rb_fix2int", (RUBY_PROC*)&dll_rb_fix2int},
704 {"rb_num2int", (RUBY_PROC*)&dll_rb_num2int},
705 {"rb_num2uint", (RUBY_PROC*)&dll_rb_num2uint},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200706# endif
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100707 {"rb_num2dbl", (RUBY_PROC*)&dll_rb_num2dbl},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000708 {"rb_lastline_get", (RUBY_PROC*)&dll_rb_lastline_get},
709 {"rb_lastline_set", (RUBY_PROC*)&dll_rb_lastline_set},
Bram Moolenaard0573012017-10-28 21:11:06 +0200710 {"rb_protect", (RUBY_PROC*)&dll_rb_protect},
711 {"rb_load", (RUBY_PROC*)&dll_rb_load},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000712 {"rb_num2long", (RUBY_PROC*)&dll_rb_num2long},
713 {"rb_num2ulong", (RUBY_PROC*)&dll_rb_num2ulong},
714 {"rb_obj_alloc", (RUBY_PROC*)&dll_rb_obj_alloc},
715 {"rb_obj_as_string", (RUBY_PROC*)&dll_rb_obj_as_string},
716 {"rb_obj_id", (RUBY_PROC*)&dll_rb_obj_id},
717 {"rb_raise", (RUBY_PROC*)&dll_rb_raise},
Bram Moolenaar41a41412020-01-07 21:32:19 +0100718# if RUBY_VERSION >= 18
Bram Moolenaarf9b5ef82010-09-29 13:02:53 +0200719 {"rb_string_value", (RUBY_PROC*)&dll_rb_string_value},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200720# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000721 {"rb_str2cstr", (RUBY_PROC*)&dll_rb_str2cstr},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200722# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000723 {"rb_str_cat", (RUBY_PROC*)&dll_rb_str_cat},
724 {"rb_str_concat", (RUBY_PROC*)&dll_rb_str_concat},
725 {"rb_str_new", (RUBY_PROC*)&dll_rb_str_new},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200726# ifdef need_rb_str_new_cstr
Bram Moolenaar1d2beae2010-05-22 21:56:55 +0200727 {"rb_str_new_cstr", (RUBY_PROC*)&dll_rb_str_new_cstr},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200728# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000729 {"rb_str_new2", (RUBY_PROC*)&dll_rb_str_new2},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200730# endif
Bram Moolenaar41a41412020-01-07 21:32:19 +0100731# if RUBY_VERSION >= 19
Bram Moolenaar165641d2010-02-17 16:23:09 +0100732 {"rb_errinfo", (RUBY_PROC*)&dll_rb_errinfo},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200733# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000734 {"ruby_errinfo", (RUBY_PROC*)&dll_ruby_errinfo},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200735# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000736 {"ruby_init", (RUBY_PROC*)&dll_ruby_init},
737 {"ruby_init_loadpath", (RUBY_PROC*)&dll_ruby_init_loadpath},
Bram Moolenaar4f974752019-02-17 17:44:42 +0100738# ifdef MSWIN
Bram Moolenaar41a41412020-01-07 21:32:19 +0100739# if RUBY_VERSION >= 19
Bram Moolenaar4f391792017-01-15 16:59:07 +0100740 {"ruby_sysinit", (RUBY_PROC*)&dll_ruby_sysinit},
Bram Moolenaar9d5c84a2018-12-19 20:48:46 +0100741# else
742 {"NtInitialize", (RUBY_PROC*)&dll_NtInitialize},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200743# endif
Bram Moolenaar41a41412020-01-07 21:32:19 +0100744# if RUBY_VERSION >= 18
Bram Moolenaar071d4272004-06-13 20:20:40 +0000745 {"rb_w32_snprintf", (RUBY_PROC*)&dll_rb_w32_snprintf},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200746# endif
Bram Moolenaar3ca71f12010-10-27 16:49:47 +0200747# endif
Bram Moolenaar41a41412020-01-07 21:32:19 +0100748# if RUBY_VERSION >= 18
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100749 {"rb_string_value_ptr", (RUBY_PROC*)&dll_rb_string_value_ptr},
Bram Moolenaar41a41412020-01-07 21:32:19 +0100750# if RUBY_VERSION <= 19
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100751 {"rb_float_new", (RUBY_PROC*)&dll_rb_float_new},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200752# else
Bram Moolenaar886ed692013-02-26 13:41:35 +0100753 {"rb_float_new_in_heap", (RUBY_PROC*)&dll_rb_float_new},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200754# endif
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100755 {"rb_ary_new", (RUBY_PROC*)&dll_rb_ary_new},
Bram Moolenaar51e9fbf2018-08-11 14:24:11 +0200756# ifdef RB_ARY_NEW4_MACRO
757 {"rb_ary_new_from_values", (RUBY_PROC*)&dll_rb_ary_new4},
758# else
759 {"rb_ary_new4", (RUBY_PROC*)&dll_rb_ary_new4},
760# endif
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100761 {"rb_ary_push", (RUBY_PROC*)&dll_rb_ary_push},
Bram Moolenaar41a41412020-01-07 21:32:19 +0100762# if RUBY_VERSION >= 26
Bram Moolenaarb09c6842018-12-27 22:11:01 +0100763 {"rb_ary_detransient", (RUBY_PROC*)&dll_rb_ary_detransient},
764# endif
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200765# endif
Bram Moolenaar41a41412020-01-07 21:32:19 +0100766# if RUBY_VERSION >= 19
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100767 {"rb_int2big", (RUBY_PROC*)&dll_rb_int2big},
Bram Moolenaar165641d2010-02-17 16:23:09 +0100768 {"ruby_script", (RUBY_PROC*)&dll_ruby_script},
769 {"rb_enc_find_index", (RUBY_PROC*)&dll_rb_enc_find_index},
770 {"rb_enc_find", (RUBY_PROC*)&dll_rb_enc_find},
771 {"rb_enc_str_new", (RUBY_PROC*)&dll_rb_enc_str_new},
772 {"rb_sprintf", (RUBY_PROC*)&dll_rb_sprintf},
Bram Moolenaar7a8ef142010-12-24 13:39:35 +0100773 {"rb_require", (RUBY_PROC*)&dll_rb_require},
Bram Moolenaar9b1067e2015-11-19 19:33:15 +0100774 {"ruby_options", (RUBY_PROC*)&dll_ruby_options},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200775# endif
Bram Moolenaar41a41412020-01-07 21:32:19 +0100776# if (RUBY_VERSION >= 19) || defined(RUBY_INIT_STACK)
Bram Moolenaar10f3a792013-05-20 12:52:29 +0200777# ifdef __ia64
778 {"rb_ia64_bsp", (RUBY_PROC*)&dll_rb_ia64_bsp},
779# endif
780 {"ruby_init_stack", (RUBY_PROC*)&dll_ruby_init_stack},
781# endif
Bram Moolenaara1a118b2014-02-05 22:41:15 +0100782# if defined(USE_RGENGC) && USE_RGENGC
Bram Moolenaar41a41412020-01-07 21:32:19 +0100783# if RUBY_VERSION == 21
Bram Moolenaara1a118b2014-02-05 22:41:15 +0100784 {"rb_gc_writebarrier_unprotect_promoted", (RUBY_PROC*)&dll_rb_gc_writebarrier_unprotect_promoted},
Bram Moolenaar0c7485f2015-01-14 14:04:10 +0100785# else
786 {"rb_gc_writebarrier_unprotect", (RUBY_PROC*)&dll_rb_gc_writebarrier_unprotect},
787# endif
Bram Moolenaara1a118b2014-02-05 22:41:15 +0100788# endif
Bram Moolenaardace9f72020-12-28 15:07:45 +0100789# if RUBY_VERSION >= 30
790 {"ruby_malloc_size_overflow", (RUBY_PROC*)&dll_ruby_malloc_size_overflow},
791# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000792 {"", NULL},
793};
794
795/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000796 * Load library and get all pointers.
797 * Parameter 'libname' provides name of DLL.
798 * Return OK or FAIL.
799 */
800 static int
801ruby_runtime_link_init(char *libname, int verbose)
802{
803 int i;
804
805 if (hinstRuby)
806 return OK;
Bram Moolenaarf9b5ef82010-09-29 13:02:53 +0200807 hinstRuby = load_dll(libname);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000808 if (!hinstRuby)
809 {
810 if (verbose)
Martin Tournoij1a3e5742021-07-24 13:57:29 +0200811 semsg(_(e_loadlib), libname, load_dll_error());
Bram Moolenaar071d4272004-06-13 20:20:40 +0000812 return FAIL;
813 }
814
815 for (i = 0; ruby_funcname_table[i].ptr; ++i)
816 {
Bram Moolenaarf9b5ef82010-09-29 13:02:53 +0200817 if (!(*ruby_funcname_table[i].ptr = symbol_from_dll(hinstRuby,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000818 ruby_funcname_table[i].name)))
819 {
Bram Moolenaarf9b5ef82010-09-29 13:02:53 +0200820 close_dll(hinstRuby);
Bram Moolenaar3ca71f12010-10-27 16:49:47 +0200821 hinstRuby = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000822 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100823 semsg(_(e_loadfunc), ruby_funcname_table[i].name);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000824 return FAIL;
825 }
826 }
827 return OK;
828}
829
830/*
831 * If ruby is enabled (there is installed ruby on Windows system) return TRUE,
832 * else FALSE.
833 */
834 int
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100835ruby_enabled(int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000836{
Bram Moolenaar25e4fcd2016-01-09 14:57:47 +0100837 return ruby_runtime_link_init((char *)p_rubydll, verbose) == OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000838}
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100839#endif // defined(DYNAMIC_RUBY) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000840
841 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100842ruby_end(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000843{
Bram Moolenaar071d4272004-06-13 20:20:40 +0000844}
845
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100846 void
847ex_ruby(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000848{
849 int state;
850 char *script = NULL;
851
Bram Moolenaar35a2e192006-03-13 22:07:11 +0000852 script = (char *)script_get(eap, eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000853 if (!eap->skip && ensure_ruby_initialized())
854 {
855 if (script == NULL)
856 rb_eval_string_protect((char *)eap->arg, &state);
857 else
858 rb_eval_string_protect(script, &state);
859 if (state)
860 error_print(state);
861 }
862 vim_free(script);
863}
864
Bram Moolenaar165641d2010-02-17 16:23:09 +0100865/*
866 * In Ruby 1.9 or later, ruby String object has encoding.
867 * conversion buffer string of vim to ruby String object using
868 * VIM encoding option.
869 */
870 static VALUE
871vim_str2rb_enc_str(const char *s)
872{
Bram Moolenaar41a41412020-01-07 21:32:19 +0100873#if RUBY_VERSION >= 19
Bram Moolenaar165641d2010-02-17 16:23:09 +0100874 long lval;
875 char_u *sval;
876 rb_encoding *enc;
877
Yegappan Lakshmanan64095532021-12-06 11:03:55 +0000878 if (get_option_value((char_u *)"enc", &lval, &sval, NULL, 0) == gov_string)
Bram Moolenaar165641d2010-02-17 16:23:09 +0100879 {
880 enc = rb_enc_find((char *)sval);
881 vim_free(sval);
Bram Moolenaar758535a2016-03-30 22:06:16 +0200882 if (enc)
Bram Moolenaar9b0ac222016-06-01 20:31:43 +0200883 return rb_enc_str_new(s, (long)strlen(s), enc);
Bram Moolenaar165641d2010-02-17 16:23:09 +0100884 }
885#endif
886 return rb_str_new2(s);
887}
888
889 static VALUE
890eval_enc_string_protect(const char *str, int *state)
891{
Bram Moolenaar41a41412020-01-07 21:32:19 +0100892#if RUBY_VERSION >= 19
Bram Moolenaar165641d2010-02-17 16:23:09 +0100893 long lval;
894 char_u *sval;
895 rb_encoding *enc;
896 VALUE v;
897
Yegappan Lakshmanan64095532021-12-06 11:03:55 +0000898 if (get_option_value((char_u *)"enc", &lval, &sval, NULL, 0) == gov_string)
Bram Moolenaar165641d2010-02-17 16:23:09 +0100899 {
900 enc = rb_enc_find((char *)sval);
901 vim_free(sval);
902 if (enc)
903 {
904 v = rb_sprintf("#-*- coding:%s -*-\n%s", rb_enc_name(enc), str);
905 return rb_eval_string_protect(StringValuePtr(v), state);
906 }
907 }
908#endif
909 return rb_eval_string_protect(str, state);
910}
911
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100912 void
913ex_rubydo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000914{
915 int state;
916 linenr_T i;
Bram Moolenaarc593fee2017-01-29 23:11:25 +0100917 buf_T *was_curbuf = curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000918
919 if (ensure_ruby_initialized())
920 {
921 if (u_save(eap->line1 - 1, eap->line2 + 1) != OK)
922 return;
Bram Moolenaar758535a2016-03-30 22:06:16 +0200923 for (i = eap->line1; i <= eap->line2; i++)
924 {
Bram Moolenaare980d8a2010-12-08 13:11:21 +0100925 VALUE line;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000926
Bram Moolenaarc593fee2017-01-29 23:11:25 +0100927 if (i > curbuf->b_ml.ml_line_count)
928 break;
Bram Moolenaare980d8a2010-12-08 13:11:21 +0100929 line = vim_str2rb_enc_str((char *)ml_get(i));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000930 rb_lastline_set(line);
Bram Moolenaar165641d2010-02-17 16:23:09 +0100931 eval_enc_string_protect((char *) eap->arg, &state);
Bram Moolenaar758535a2016-03-30 22:06:16 +0200932 if (state)
933 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000934 error_print(state);
935 break;
936 }
Bram Moolenaarc593fee2017-01-29 23:11:25 +0100937 if (was_curbuf != curbuf)
938 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000939 line = rb_lastline_get();
Bram Moolenaar758535a2016-03-30 22:06:16 +0200940 if (!NIL_P(line))
941 {
942 if (TYPE(line) != T_STRING)
943 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100944 emsg(_("E265: $_ must be an instance of String"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000945 return;
946 }
Bram Moolenaar165641d2010-02-17 16:23:09 +0100947 ml_replace(i, (char_u *) StringValuePtr(line), 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000948 changed();
949#ifdef SYNTAX_HL
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100950 syn_changed(i); // recompute syntax hl. for this line
Bram Moolenaar071d4272004-06-13 20:20:40 +0000951#endif
952 }
953 }
954 check_cursor();
955 update_curbuf(NOT_VALID);
956 }
957}
958
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100959 static VALUE
960rb_load_wrap(VALUE file_to_load)
Bram Moolenaar37badc82018-01-31 20:15:30 +0100961{
962 rb_load(file_to_load, 0);
963 return Qnil;
964}
965
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100966 void
967ex_rubyfile(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000968{
969 int state;
970
971 if (ensure_ruby_initialized())
972 {
Bram Moolenaar37badc82018-01-31 20:15:30 +0100973 VALUE file_to_load = rb_str_new2((const char *)eap->arg);
974 rb_protect(rb_load_wrap, file_to_load, &state);
975 if (state)
976 error_print(state);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000977 }
978}
979
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100980 void
981ruby_buffer_free(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000982{
Bram Moolenaare344bea2005-09-01 20:46:49 +0000983 if (buf->b_ruby_ref)
984 {
985 rb_hash_aset(objtbl, rb_obj_id((VALUE) buf->b_ruby_ref), Qnil);
986 RDATA(buf->b_ruby_ref)->data = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000987 }
988}
989
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100990 void
991ruby_window_free(win_T *win)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000992{
Bram Moolenaare344bea2005-09-01 20:46:49 +0000993 if (win->w_ruby_ref)
994 {
995 rb_hash_aset(objtbl, rb_obj_id((VALUE) win->w_ruby_ref), Qnil);
996 RDATA(win->w_ruby_ref)->data = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000997 }
998}
999
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001000 static int
1001ensure_ruby_initialized(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001002{
1003 if (!ruby_initialized)
1004 {
1005#ifdef DYNAMIC_RUBY
1006 if (ruby_enabled(TRUE))
1007 {
1008#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01001009#ifdef MSWIN
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001010 // suggested by Ariya Mizutani
Bram Moolenaar0b69c732010-02-17 15:11:50 +01001011 int argc = 1;
1012 char *argv[] = {"gvim.exe"};
Bram Moolenaar90140742014-11-27 17:44:08 +01001013 char **argvp = argv;
Bram Moolenaar41a41412020-01-07 21:32:19 +01001014# if RUBY_VERSION >= 19
Bram Moolenaarfe6ce332017-01-14 20:12:01 +01001015 ruby_sysinit(&argc, &argvp);
1016# else
Bram Moolenaar90140742014-11-27 17:44:08 +01001017 NtInitialize(&argc, &argvp);
Bram Moolenaarfe6ce332017-01-14 20:12:01 +01001018# endif
Bram Moolenaar0b69c732010-02-17 15:11:50 +01001019#endif
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001020 {
Bram Moolenaar41a41412020-01-07 21:32:19 +01001021#if (RUBY_VERSION >= 19) || defined(RUBY_INIT_STACK)
Bram Moolenaar99685e62013-05-11 13:56:18 +02001022 ruby_init_stack(ruby_stack_start);
Bram Moolenaar165641d2010-02-17 16:23:09 +01001023#endif
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001024 ruby_init();
1025 }
Bram Moolenaar41a41412020-01-07 21:32:19 +01001026#if RUBY_VERSION >= 19
Bram Moolenaar7a8ef142010-12-24 13:39:35 +01001027 {
1028 int dummy_argc = 2;
Bram Moolenaard1bc96ce2017-09-26 21:21:44 +02001029 char *dummy_argv[] = {"vim-ruby", "-e_=0"};
Bram Moolenaar9b1067e2015-11-19 19:33:15 +01001030 ruby_options(dummy_argc, dummy_argv);
Bram Moolenaar7a8ef142010-12-24 13:39:35 +01001031 }
Bram Moolenaar165641d2010-02-17 16:23:09 +01001032 ruby_script("vim-ruby");
Bram Moolenaar7a8ef142010-12-24 13:39:35 +01001033#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001034 ruby_init_loadpath();
Bram Moolenaar165641d2010-02-17 16:23:09 +01001035#endif
Bram Moolenaar7a8ef142010-12-24 13:39:35 +01001036 ruby_io_init();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001037 ruby_vim_init();
1038 ruby_initialized = 1;
1039#ifdef DYNAMIC_RUBY
1040 }
1041 else
1042 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001043 emsg(_("E266: Sorry, this command is disabled, the Ruby library could not be loaded."));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001044 return 0;
1045 }
1046#endif
1047 }
1048 return ruby_initialized;
1049}
1050
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001051 static void
1052error_print(int state)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001053{
Bram Moolenaar41a41412020-01-07 21:32:19 +01001054#if !defined(DYNAMIC_RUBY) && (RUBY_VERSION <= 18)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001055 RUBYEXTERN VALUE ruby_errinfo;
1056#endif
Bram Moolenaarf711cb22018-08-01 18:42:13 +02001057 VALUE error;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001058 VALUE eclass;
1059 VALUE einfo;
Bram Moolenaarf711cb22018-08-01 18:42:13 +02001060 VALUE bt;
1061 int attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001062 char buff[BUFSIZ];
Bram Moolenaarf711cb22018-08-01 18:42:13 +02001063 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001064
1065#define TAG_RETURN 0x1
1066#define TAG_BREAK 0x2
1067#define TAG_NEXT 0x3
1068#define TAG_RETRY 0x4
1069#define TAG_REDO 0x5
1070#define TAG_RAISE 0x6
1071#define TAG_THROW 0x7
1072#define TAG_FATAL 0x8
1073#define TAG_MASK 0xf
1074
Bram Moolenaar758535a2016-03-30 22:06:16 +02001075 switch (state)
1076 {
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001077 case TAG_RETURN:
1078 emsg(_("E267: unexpected return"));
1079 break;
1080 case TAG_NEXT:
1081 emsg(_("E268: unexpected next"));
1082 break;
1083 case TAG_BREAK:
1084 emsg(_("E269: unexpected break"));
1085 break;
1086 case TAG_REDO:
1087 emsg(_("E270: unexpected redo"));
1088 break;
1089 case TAG_RETRY:
1090 emsg(_("E271: retry outside of rescue clause"));
1091 break;
1092 case TAG_RAISE:
1093 case TAG_FATAL:
Bram Moolenaar41a41412020-01-07 21:32:19 +01001094#if RUBY_VERSION >= 19
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001095 error = rb_errinfo();
Bram Moolenaar165641d2010-02-17 16:23:09 +01001096#else
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001097 error = ruby_errinfo;
Bram Moolenaar165641d2010-02-17 16:23:09 +01001098#endif
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001099 eclass = CLASS_OF(error);
1100 einfo = rb_obj_as_string(error);
1101 if (eclass == rb_eRuntimeError && RSTRING_LEN(einfo) == 0)
1102 {
1103 emsg(_("E272: unhandled exception"));
1104 }
1105 else
1106 {
1107 VALUE epath;
1108 char *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001109
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001110 epath = rb_class_path(eclass);
1111 vim_snprintf(buff, BUFSIZ, "%s: %s",
1112 RSTRING_PTR(epath), RSTRING_PTR(einfo));
1113 p = strchr(buff, '\n');
1114 if (p) *p = '\0';
1115 emsg(buff);
1116 }
Bram Moolenaarf711cb22018-08-01 18:42:13 +02001117
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001118 attr = syn_name2attr((char_u *)"Error");
Bram Moolenaar49c99fc2020-02-11 23:01:39 +01001119#if RUBY_VERSION >= 21
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001120 bt = rb_funcallv(error, rb_intern("backtrace"), 0, 0);
1121 for (i = 0; i < RARRAY_LEN(bt); i++)
1122 msg_attr(RSTRING_PTR(RARRAY_AREF(bt, i)), attr);
Bram Moolenaar49c99fc2020-02-11 23:01:39 +01001123#else
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001124 bt = rb_funcall2(error, rb_intern("backtrace"), 0, 0);
1125 for (i = 0; i < RARRAY_LEN(bt); i++)
1126 msg_attr(RSTRING_PTR(RARRAY_PTR(bt)[i]), attr);
Bram Moolenaar49c99fc2020-02-11 23:01:39 +01001127#endif
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001128 break;
1129 default:
1130 vim_snprintf(buff, BUFSIZ, _("E273: unknown longjmp status %d"), state);
1131 emsg(buff);
1132 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001133 }
1134}
1135
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001136 static VALUE
1137vim_message(VALUE self UNUSED, VALUE str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001138{
1139 char *buff, *p;
1140
1141 str = rb_obj_as_string(str);
Bram Moolenaar3f5f7952011-08-04 19:34:59 +02001142 if (RSTRING_LEN(str) > 0)
1143 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001144 // Only do this when the string isn't empty, alloc(0) causes trouble.
Bram Moolenaar00ccf542017-09-03 15:17:48 +02001145 buff = ALLOCA_N(char, RSTRING_LEN(str) + 1);
Bram Moolenaar3f5f7952011-08-04 19:34:59 +02001146 strcpy(buff, RSTRING_PTR(str));
1147 p = strchr(buff, '\n');
1148 if (p) *p = '\0';
Bram Moolenaar32526b32019-01-19 17:43:09 +01001149 msg(buff);
Bram Moolenaar3f5f7952011-08-04 19:34:59 +02001150 }
1151 else
1152 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01001153 msg("");
Bram Moolenaar3f5f7952011-08-04 19:34:59 +02001154 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001155 return Qnil;
1156}
1157
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001158 static VALUE
1159vim_set_option(VALUE self UNUSED, VALUE str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001160{
Bram Moolenaar165641d2010-02-17 16:23:09 +01001161 do_set((char_u *)StringValuePtr(str), 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001162 update_screen(NOT_VALID);
1163 return Qnil;
1164}
1165
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001166 static VALUE
1167vim_command(VALUE self UNUSED, VALUE str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001168{
Bram Moolenaar165641d2010-02-17 16:23:09 +01001169 do_cmdline_cmd((char_u *)StringValuePtr(str));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001170 return Qnil;
1171}
1172
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001173#ifdef FEAT_EVAL
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001174 static VALUE
1175vim_to_ruby(typval_T *tv)
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001176{
1177 VALUE result = Qnil;
1178
1179 if (tv->v_type == VAR_STRING)
1180 {
Bram Moolenaar94127e42010-03-19 23:08:48 +01001181 result = rb_str_new2(tv->vval.v_string == NULL
1182 ? "" : (char *)(tv->vval.v_string));
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001183 }
1184 else if (tv->v_type == VAR_NUMBER)
1185 {
Bram Moolenaar639a2552010-03-17 18:15:23 +01001186 result = INT2NUM(tv->vval.v_number);
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001187 }
1188# ifdef FEAT_FLOAT
1189 else if (tv->v_type == VAR_FLOAT)
1190 {
Bram Moolenaar639a2552010-03-17 18:15:23 +01001191 result = rb_float_new(tv->vval.v_float);
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001192 }
1193# endif
1194 else if (tv->v_type == VAR_LIST)
1195 {
Bram Moolenaar639a2552010-03-17 18:15:23 +01001196 list_T *list = tv->vval.v_list;
1197 listitem_T *curr;
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001198
Bram Moolenaar639a2552010-03-17 18:15:23 +01001199 result = rb_ary_new();
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001200
Bram Moolenaar639a2552010-03-17 18:15:23 +01001201 if (list != NULL)
1202 {
Bram Moolenaaraeea7212020-04-02 18:50:46 +02001203 FOR_ALL_LIST_ITEMS(list, curr)
Bram Moolenaar639a2552010-03-17 18:15:23 +01001204 rb_ary_push(result, vim_to_ruby(&curr->li_tv));
Bram Moolenaar639a2552010-03-17 18:15:23 +01001205 }
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001206 }
1207 else if (tv->v_type == VAR_DICT)
1208 {
Bram Moolenaar639a2552010-03-17 18:15:23 +01001209 result = rb_hash_new();
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001210
Bram Moolenaar639a2552010-03-17 18:15:23 +01001211 if (tv->vval.v_dict != NULL)
1212 {
1213 hashtab_T *ht = &tv->vval.v_dict->dv_hashtab;
1214 long_u todo = ht->ht_used;
1215 hashitem_T *hi;
1216 dictitem_T *di;
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001217
Bram Moolenaar639a2552010-03-17 18:15:23 +01001218 for (hi = ht->ht_array; todo > 0; ++hi)
1219 {
1220 if (!HASHITEM_EMPTY(hi))
1221 {
1222 --todo;
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001223
Bram Moolenaar639a2552010-03-17 18:15:23 +01001224 di = dict_lookup(hi);
1225 rb_hash_aset(result, rb_str_new2((char *)hi->hi_key),
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001226 vim_to_ruby(&di->di_tv));
Bram Moolenaar639a2552010-03-17 18:15:23 +01001227 }
1228 }
1229 }
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001230 }
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01001231 else if (tv->v_type == VAR_BOOL || tv->v_type == VAR_SPECIAL)
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001232 {
Bram Moolenaard84b26a2018-07-28 17:18:09 +02001233 if (tv->vval.v_number == VVAL_TRUE)
1234 result = Qtrue;
1235 else if (tv->vval.v_number == VVAL_FALSE)
1236 result = Qfalse;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001237 }
1238 else if (tv->v_type == VAR_BLOB)
1239 {
1240 result = rb_str_new(tv->vval.v_blob->bv_ga.ga_data,
1241 tv->vval.v_blob->bv_ga.ga_len);
1242 }
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001243 // else return Qnil;
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001244
1245 return result;
1246}
1247#endif
1248
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001249 static VALUE
1250vim_evaluate(VALUE self UNUSED, VALUE str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001251{
1252#ifdef FEAT_EVAL
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001253 typval_T *tv;
1254 VALUE result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001255
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001256 tv = eval_expr((char_u *)StringValuePtr(str), NULL);
1257 if (tv == NULL)
Bram Moolenaar639a2552010-03-17 18:15:23 +01001258 return Qnil;
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001259 result = vim_to_ruby(tv);
1260
1261 free_tv(tv);
1262
1263 return result;
1264#else
1265 return Qnil;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001266#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001267}
1268
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001269#ifdef USE_TYPEDDATA
1270static size_t buffer_dsize(const void *buf);
1271
1272static const rb_data_type_t buffer_type = {
1273 "vim_buffer",
Bram Moolenaar41a41412020-01-07 21:32:19 +01001274 {0, 0, buffer_dsize,
1275# if RUBY_VERSION >= 27
Bram Moolenaar8b430b42020-02-22 15:01:00 +01001276 0, {0}
Bram Moolenaar41a41412020-01-07 21:32:19 +01001277# else
1278 {0, 0}
1279# endif
1280 },
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001281 0, 0,
1282# ifdef RUBY_TYPED_FREE_IMMEDIATELY
1283 0,
1284# endif
1285};
1286
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001287 static size_t
1288buffer_dsize(const void *buf UNUSED)
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001289{
1290 return sizeof(buf_T);
1291}
1292#endif
1293
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001294 static VALUE
1295buffer_new(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001296{
Bram Moolenaare344bea2005-09-01 20:46:49 +00001297 if (buf->b_ruby_ref)
1298 {
1299 return (VALUE) buf->b_ruby_ref;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001300 }
Bram Moolenaare344bea2005-09-01 20:46:49 +00001301 else
1302 {
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001303#ifdef USE_TYPEDDATA
1304 VALUE obj = TypedData_Wrap_Struct(cBuffer, &buffer_type, buf);
1305#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001306 VALUE obj = Data_Wrap_Struct(cBuffer, 0, 0, buf);
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001307#endif
Bram Moolenaare344bea2005-09-01 20:46:49 +00001308 buf->b_ruby_ref = (void *) obj;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001309 rb_hash_aset(objtbl, rb_obj_id(obj), obj);
1310 return obj;
1311 }
1312}
1313
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001314 static buf_T *
1315get_buf(VALUE obj)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001316{
1317 buf_T *buf;
1318
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001319#ifdef USE_TYPEDDATA
1320 TypedData_Get_Struct(obj, buf_T, &buffer_type, buf);
1321#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001322 Data_Get_Struct(obj, buf_T, buf);
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001323#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001324 if (buf == NULL)
1325 rb_raise(eDeletedBufferError, "attempt to refer to deleted buffer");
1326 return buf;
1327}
1328
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001329 static VALUE
1330vim_blob(VALUE self UNUSED, VALUE str)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001331{
1332 VALUE result = rb_str_new("0z", 2);
1333 char buf[4];
1334 int i;
1335 for (i = 0; i < RSTRING_LEN(str); i++)
1336 {
Bram Moolenaar0d13cce2019-02-23 14:23:03 +01001337 sprintf(buf, "%02X", (unsigned char)(RSTRING_PTR(str)[i]));
Bram Moolenaarb191be22019-01-30 21:00:12 +01001338 rb_str_concat(result, rb_str_new2(buf));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001339 }
1340 return result;
1341}
1342
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001343 static VALUE
Bram Moolenaard5a986f2020-12-06 21:11:31 +01001344buffer_s_current(VALUE self UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001345{
1346 return buffer_new(curbuf);
1347}
1348
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001349 static VALUE
Bram Moolenaard5a986f2020-12-06 21:11:31 +01001350buffer_s_current_getter(ID id UNUSED, VALUE *x UNUSED)
1351{
1352 return buffer_new(curbuf);
1353}
1354
1355 static VALUE
1356buffer_s_count(VALUE self UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001357{
1358 buf_T *b;
1359 int n = 0;
1360
Bram Moolenaar29323592016-07-24 22:04:11 +02001361 FOR_ALL_BUFFERS(b)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001362 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001363 // Deleted buffers should not be counted
1364 // SegPhault - 01/07/05
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001365 if (b->b_p_bl)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001366 n++;
1367 }
1368
Bram Moolenaar071d4272004-06-13 20:20:40 +00001369 return INT2NUM(n);
1370}
1371
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001372 static VALUE
1373buffer_s_aref(VALUE self UNUSED, VALUE num)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001374{
1375 buf_T *b;
1376 int n = NUM2INT(num);
1377
Bram Moolenaar29323592016-07-24 22:04:11 +02001378 FOR_ALL_BUFFERS(b)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001379 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001380 // Deleted buffers should not be counted
1381 // SegPhault - 01/07/05
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001382 if (!b->b_p_bl)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001383 continue;
1384
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001385 if (n == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001386 return buffer_new(b);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001387
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001388 n--;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001389 }
1390 return Qnil;
1391}
1392
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001393 static VALUE
1394buffer_name(VALUE self)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001395{
1396 buf_T *buf = get_buf(self);
1397
Bram Moolenaar35a2e192006-03-13 22:07:11 +00001398 return buf->b_ffname ? rb_str_new2((char *)buf->b_ffname) : Qnil;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001399}
1400
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001401 static VALUE
1402buffer_number(VALUE self)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001403{
1404 buf_T *buf = get_buf(self);
1405
1406 return INT2NUM(buf->b_fnum);
1407}
1408
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001409 static VALUE
1410buffer_count(VALUE self)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001411{
1412 buf_T *buf = get_buf(self);
1413
1414 return INT2NUM(buf->b_ml.ml_line_count);
1415}
1416
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001417 static VALUE
1418get_buffer_line(buf_T *buf, linenr_T n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001419{
Bram Moolenaar3c531602010-11-16 14:46:19 +01001420 if (n <= 0 || n > buf->b_ml.ml_line_count)
1421 rb_raise(rb_eIndexError, "line number %ld out of range", (long)n);
1422 return vim_str2rb_enc_str((char *)ml_get_buf(buf, n, FALSE));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001423}
1424
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001425 static VALUE
1426buffer_aref(VALUE self, VALUE num)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001427{
1428 buf_T *buf = get_buf(self);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001429
1430 if (buf != NULL)
1431 return get_buffer_line(buf, (linenr_T)NUM2LONG(num));
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001432 return Qnil; // For stop warning
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001433}
1434
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001435 static VALUE
1436set_buffer_line(buf_T *buf, linenr_T n, VALUE str)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001437{
Bram Moolenaar165641d2010-02-17 16:23:09 +01001438 char *line = StringValuePtr(str);
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001439 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001440
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001441 if (n > 0 && n <= buf->b_ml.ml_line_count && line != NULL)
1442 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001443 // set curwin/curbuf for "buf" and save some things
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001444 aucmd_prepbuf(&aco, buf);
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001445
Bram Moolenaar758535a2016-03-30 22:06:16 +02001446 if (u_savesub(n) == OK)
1447 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001448 ml_replace(n, (char_u *)line, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001449 changed();
1450#ifdef SYNTAX_HL
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001451 syn_changed(n); // recompute syntax hl. for this line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001452#endif
1453 }
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001454
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001455 // restore curwin/curbuf and a few other things
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001456 aucmd_restbuf(&aco);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001457 // Careful: autocommands may have made "buf" invalid!
Bram Moolenaarf30e74c2006-08-16 17:35:00 +00001458
Bram Moolenaar071d4272004-06-13 20:20:40 +00001459 update_curbuf(NOT_VALID);
1460 }
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001461 else
1462 {
Bram Moolenaar165641d2010-02-17 16:23:09 +01001463 rb_raise(rb_eIndexError, "line number %ld out of range", (long)n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001464 }
1465 return str;
1466}
1467
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001468 static VALUE
1469buffer_aset(VALUE self, VALUE num, VALUE str)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001470{
1471 buf_T *buf = get_buf(self);
1472
1473 if (buf != NULL)
1474 return set_buffer_line(buf, (linenr_T)NUM2LONG(num), str);
1475 return str;
1476}
1477
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001478 static VALUE
1479buffer_delete(VALUE self, VALUE num)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001480{
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001481 buf_T *buf = get_buf(self);
1482 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 Moolenaar20ff7922006-06-20 19:10:43 +00001485 if (n > 0 && n <= buf->b_ml.ml_line_count)
1486 {
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_savedel(n, 1) == OK)
1491 {
Bram Moolenaarca70c072020-05-30 20:30:46 +02001492 ml_delete(n);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001493
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001494 // Changes to non-active buffers should properly refresh
1495 // SegPhault - 01/09/05
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001496 deleted_lines_mark(n, 1L);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001497
Bram Moolenaar071d4272004-06-13 20:20:40 +00001498 changed();
1499 }
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 Moolenaar20ff7922006-06-20 19:10:43 +00001507 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 Qnil;
1512}
1513
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001514 static VALUE
1515buffer_append(VALUE self, VALUE num, VALUE str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001516{
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001517 buf_T *buf = get_buf(self);
Bram Moolenaar165641d2010-02-17 16:23:09 +01001518 char *line = StringValuePtr(str);
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001519 long n = NUM2LONG(num);
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001520 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001521
Bram Moolenaar3c531602010-11-16 14:46:19 +01001522 if (line == NULL)
1523 {
Bram Moolenaar165641d2010-02-17 16:23:09 +01001524 rb_raise(rb_eIndexError, "NULL line");
1525 }
1526 else if (n >= 0 && n <= buf->b_ml.ml_line_count)
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001527 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001528 // set curwin/curbuf for "buf" and save some things
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001529 aucmd_prepbuf(&aco, buf);
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001530
Bram Moolenaar758535a2016-03-30 22:06:16 +02001531 if (u_inssub(n + 1) == OK)
1532 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001533 ml_append(n, (char_u *) line, (colnr_T) 0, FALSE);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001534
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001535 // Changes to non-active buffers should properly refresh screen
1536 // SegPhault - 12/20/04
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001537 appended_lines_mark(n, 1L);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001538
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001539 changed();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001540 }
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001541
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001542 // restore curwin/curbuf and a few other things
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001543 aucmd_restbuf(&aco);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001544 // Careful: autocommands may have made "buf" invalid!
Bram Moolenaarf30e74c2006-08-16 17:35:00 +00001545
Bram Moolenaar071d4272004-06-13 20:20:40 +00001546 update_curbuf(NOT_VALID);
1547 }
Bram Moolenaar3c531602010-11-16 14:46:19 +01001548 else
1549 {
Bram Moolenaar165641d2010-02-17 16:23:09 +01001550 rb_raise(rb_eIndexError, "line number %ld out of range", n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001551 }
1552 return str;
1553}
1554
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001555#ifdef USE_TYPEDDATA
1556static size_t window_dsize(const void *buf);
1557
1558static const rb_data_type_t window_type = {
1559 "vim_window",
Bram Moolenaar41a41412020-01-07 21:32:19 +01001560 {0, 0, window_dsize,
1561# if RUBY_VERSION >= 27
Bram Moolenaar8b430b42020-02-22 15:01:00 +01001562 0, {0}
Bram Moolenaar41a41412020-01-07 21:32:19 +01001563# else
1564 {0, 0}
1565# endif
1566 },
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001567 0, 0,
1568# ifdef RUBY_TYPED_FREE_IMMEDIATELY
1569 0,
1570# endif
1571};
1572
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001573 static size_t
1574window_dsize(const void *win UNUSED)
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001575{
1576 return sizeof(win_T);
1577}
1578#endif
1579
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001580 static VALUE
1581window_new(win_T *win)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001582{
Bram Moolenaare344bea2005-09-01 20:46:49 +00001583 if (win->w_ruby_ref)
1584 {
1585 return (VALUE) win->w_ruby_ref;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001586 }
Bram Moolenaare344bea2005-09-01 20:46:49 +00001587 else
1588 {
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001589#ifdef USE_TYPEDDATA
1590 VALUE obj = TypedData_Wrap_Struct(cVimWindow, &window_type, win);
1591#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001592 VALUE obj = Data_Wrap_Struct(cVimWindow, 0, 0, win);
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001593#endif
Bram Moolenaare344bea2005-09-01 20:46:49 +00001594 win->w_ruby_ref = (void *) obj;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001595 rb_hash_aset(objtbl, rb_obj_id(obj), obj);
1596 return obj;
1597 }
1598}
1599
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001600 static win_T *
1601get_win(VALUE obj)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001602{
1603 win_T *win;
1604
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001605#ifdef USE_TYPEDDATA
1606 TypedData_Get_Struct(obj, win_T, &window_type, win);
1607#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001608 Data_Get_Struct(obj, win_T, win);
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001609#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001610 if (win == NULL)
1611 rb_raise(eDeletedWindowError, "attempt to refer to deleted window");
1612 return win;
1613}
1614
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001615 static VALUE
Bram Moolenaard5a986f2020-12-06 21:11:31 +01001616window_s_current(VALUE self UNUSED)
1617{
1618 return window_new(curwin);
1619}
1620
1621 static VALUE
1622window_s_current_getter(ID id UNUSED, VALUE *x UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001623{
1624 return window_new(curwin);
1625}
1626
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001627/*
1628 * Added line manipulation functions
1629 * SegPhault - 03/07/05
1630 */
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001631 static VALUE
Bram Moolenaard5a986f2020-12-06 21:11:31 +01001632line_s_current(VALUE self UNUSED)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001633{
1634 return get_buffer_line(curbuf, curwin->w_cursor.lnum);
1635}
1636
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001637 static VALUE
1638set_current_line(VALUE self UNUSED, VALUE str)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001639{
1640 return set_buffer_line(curbuf, curwin->w_cursor.lnum, str);
1641}
1642
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001643 static VALUE
Bram Moolenaard5a986f2020-12-06 21:11:31 +01001644current_line_number(VALUE self UNUSED)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001645{
1646 return INT2FIX((int)curwin->w_cursor.lnum);
1647}
1648
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001649 static VALUE
Bram Moolenaard5a986f2020-12-06 21:11:31 +01001650window_s_count(VALUE self UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001651{
Bram Moolenaar071d4272004-06-13 20:20:40 +00001652 win_T *w;
1653 int n = 0;
1654
Bram Moolenaar29323592016-07-24 22:04:11 +02001655 FOR_ALL_WINDOWS(w)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001656 n++;
1657 return INT2NUM(n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001658}
1659
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001660 static VALUE
1661window_s_aref(VALUE self UNUSED, VALUE num)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001662{
1663 win_T *w;
1664 int n = NUM2INT(num);
1665
Bram Moolenaar071d4272004-06-13 20:20:40 +00001666 for (w = firstwin; w != NULL; w = w->w_next, --n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001667 if (n == 0)
1668 return window_new(w);
1669 return Qnil;
1670}
1671
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001672 static VALUE
1673window_buffer(VALUE self)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001674{
1675 win_T *win = get_win(self);
1676
1677 return buffer_new(win->w_buffer);
1678}
1679
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001680 static VALUE
1681window_height(VALUE self)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001682{
1683 win_T *win = get_win(self);
1684
1685 return INT2NUM(win->w_height);
1686}
1687
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001688 static VALUE
1689window_set_height(VALUE self, VALUE height)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001690{
1691 win_T *win = get_win(self);
1692 win_T *savewin = curwin;
1693
1694 curwin = win;
1695 win_setheight(NUM2INT(height));
1696 curwin = savewin;
1697 return height;
1698}
1699
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001700 static VALUE
1701window_width(VALUE self UNUSED)
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001702{
Bram Moolenaare745d752017-09-22 16:56:22 +02001703 return INT2NUM(get_win(self)->w_width);
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001704}
1705
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001706 static VALUE
1707window_set_width(VALUE self UNUSED, VALUE width)
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001708{
1709 win_T *win = get_win(self);
1710 win_T *savewin = curwin;
1711
1712 curwin = win;
1713 win_setwidth(NUM2INT(width));
1714 curwin = savewin;
1715 return width;
1716}
1717
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001718 static VALUE
1719window_cursor(VALUE self)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001720{
1721 win_T *win = get_win(self);
1722
1723 return rb_assoc_new(INT2NUM(win->w_cursor.lnum), INT2NUM(win->w_cursor.col));
1724}
1725
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001726 static VALUE
1727window_set_cursor(VALUE self, VALUE pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001728{
1729 VALUE lnum, col;
1730 win_T *win = get_win(self);
1731
1732 Check_Type(pos, T_ARRAY);
Bram Moolenaar165641d2010-02-17 16:23:09 +01001733 if (RARRAY_LEN(pos) != 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001734 rb_raise(rb_eArgError, "array length must be 2");
Bram Moolenaar165641d2010-02-17 16:23:09 +01001735 lnum = RARRAY_PTR(pos)[0];
1736 col = RARRAY_PTR(pos)[1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001737 win->w_cursor.lnum = NUM2LONG(lnum);
1738 win->w_cursor.col = NUM2UINT(col);
Bram Moolenaar53901442018-07-25 22:02:36 +02001739 win->w_set_curswant = TRUE;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001740 check_cursor(); // put cursor on an existing line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001741 update_screen(NOT_VALID);
1742 return Qnil;
1743}
1744
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001745 static VALUE
1746f_nop(VALUE self UNUSED)
Bram Moolenaar35df7d22012-04-20 18:05:47 +02001747{
1748 return Qnil;
1749}
1750
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001751 static VALUE
1752f_p(int argc, VALUE *argv, VALUE self UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001753{
1754 int i;
1755 VALUE str = rb_str_new("", 0);
Bram Moolenaar51e9fbf2018-08-11 14:24:11 +02001756 VALUE ret = Qnil;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001757
Bram Moolenaar758535a2016-03-30 22:06:16 +02001758 for (i = 0; i < argc; i++)
1759 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001760 if (i > 0) rb_str_cat(str, ", ", 2);
1761 rb_str_concat(str, rb_inspect(argv[i]));
1762 }
Bram Moolenaar32526b32019-01-19 17:43:09 +01001763 msg(RSTRING_PTR(str));
Bram Moolenaar51e9fbf2018-08-11 14:24:11 +02001764
1765 if (argc == 1)
1766 ret = argv[0];
1767 else if (argc > 1)
1768 ret = rb_ary_new4(argc, argv);
1769 return ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001770}
1771
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001772 static void
1773ruby_io_init(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001774{
1775#ifndef DYNAMIC_RUBY
1776 RUBYEXTERN VALUE rb_stdout;
Bram Moolenaarb6c8cd82018-07-24 05:41:30 +02001777 RUBYEXTERN VALUE rb_stderr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001778#endif
1779
1780 rb_stdout = rb_obj_alloc(rb_cObject);
Bram Moolenaarb6c8cd82018-07-24 05:41:30 +02001781 rb_stderr = rb_obj_alloc(rb_cObject);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001782 rb_define_singleton_method(rb_stdout, "write", vim_message, 1);
Bram Moolenaar35df7d22012-04-20 18:05:47 +02001783 rb_define_singleton_method(rb_stdout, "flush", f_nop, 0);
Bram Moolenaarb6c8cd82018-07-24 05:41:30 +02001784 rb_define_singleton_method(rb_stderr, "write", vim_message, 1);
1785 rb_define_singleton_method(rb_stderr, "flush", f_nop, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001786 rb_define_global_function("p", f_p, -1);
1787}
1788
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001789 static void
1790ruby_vim_init(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001791{
1792 objtbl = rb_hash_new();
1793 rb_global_variable(&objtbl);
1794
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001795 // The Vim module used to be called "VIM", but "Vim" is better. Make an
1796 // alias "VIM" for backwards compatibility.
Bram Moolenaarf711faf2007-05-10 16:48:19 +00001797 mVIM = rb_define_module("Vim");
1798 rb_define_const(rb_cObject, "VIM", mVIM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001799 rb_define_const(mVIM, "VERSION_MAJOR", INT2NUM(VIM_VERSION_MAJOR));
1800 rb_define_const(mVIM, "VERSION_MINOR", INT2NUM(VIM_VERSION_MINOR));
1801 rb_define_const(mVIM, "VERSION_BUILD", INT2NUM(VIM_VERSION_BUILD));
1802 rb_define_const(mVIM, "VERSION_PATCHLEVEL", INT2NUM(VIM_VERSION_PATCHLEVEL));
1803 rb_define_const(mVIM, "VERSION_SHORT", rb_str_new2(VIM_VERSION_SHORT));
1804 rb_define_const(mVIM, "VERSION_MEDIUM", rb_str_new2(VIM_VERSION_MEDIUM));
1805 rb_define_const(mVIM, "VERSION_LONG", rb_str_new2(VIM_VERSION_LONG));
1806 rb_define_const(mVIM, "VERSION_LONG_DATE", rb_str_new2(VIM_VERSION_LONG_DATE));
1807 rb_define_module_function(mVIM, "message", vim_message, 1);
1808 rb_define_module_function(mVIM, "set_option", vim_set_option, 1);
1809 rb_define_module_function(mVIM, "command", vim_command, 1);
1810 rb_define_module_function(mVIM, "evaluate", vim_evaluate, 1);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001811 rb_define_module_function(mVIM, "blob", vim_blob, 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001812
1813 eDeletedBufferError = rb_define_class_under(mVIM, "DeletedBufferError",
1814 rb_eStandardError);
1815 eDeletedWindowError = rb_define_class_under(mVIM, "DeletedWindowError",
1816 rb_eStandardError);
1817
1818 cBuffer = rb_define_class_under(mVIM, "Buffer", rb_cObject);
1819 rb_define_singleton_method(cBuffer, "current", buffer_s_current, 0);
1820 rb_define_singleton_method(cBuffer, "count", buffer_s_count, 0);
1821 rb_define_singleton_method(cBuffer, "[]", buffer_s_aref, 1);
1822 rb_define_method(cBuffer, "name", buffer_name, 0);
1823 rb_define_method(cBuffer, "number", buffer_number, 0);
1824 rb_define_method(cBuffer, "count", buffer_count, 0);
1825 rb_define_method(cBuffer, "length", buffer_count, 0);
1826 rb_define_method(cBuffer, "[]", buffer_aref, 1);
1827 rb_define_method(cBuffer, "[]=", buffer_aset, 2);
1828 rb_define_method(cBuffer, "delete", buffer_delete, 1);
1829 rb_define_method(cBuffer, "append", buffer_append, 2);
1830
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001831 // Added line manipulation functions
1832 // SegPhault - 03/07/05
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001833 rb_define_method(cBuffer, "line_number", current_line_number, 0);
1834 rb_define_method(cBuffer, "line", line_s_current, 0);
1835 rb_define_method(cBuffer, "line=", set_current_line, 1);
1836
1837
Bram Moolenaar071d4272004-06-13 20:20:40 +00001838 cVimWindow = rb_define_class_under(mVIM, "Window", rb_cObject);
1839 rb_define_singleton_method(cVimWindow, "current", window_s_current, 0);
1840 rb_define_singleton_method(cVimWindow, "count", window_s_count, 0);
1841 rb_define_singleton_method(cVimWindow, "[]", window_s_aref, 1);
1842 rb_define_method(cVimWindow, "buffer", window_buffer, 0);
1843 rb_define_method(cVimWindow, "height", window_height, 0);
1844 rb_define_method(cVimWindow, "height=", window_set_height, 1);
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001845 rb_define_method(cVimWindow, "width", window_width, 0);
1846 rb_define_method(cVimWindow, "width=", window_set_width, 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001847 rb_define_method(cVimWindow, "cursor", window_cursor, 0);
1848 rb_define_method(cVimWindow, "cursor=", window_set_cursor, 1);
1849
Bram Moolenaard5a986f2020-12-06 21:11:31 +01001850 rb_define_virtual_variable("$curbuf", buffer_s_current_getter, 0);
1851 rb_define_virtual_variable("$curwin", window_s_current_getter, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001852}
Bram Moolenaar99685e62013-05-11 13:56:18 +02001853
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001854 void
1855vim_ruby_init(void *stack_start)
Bram Moolenaar99685e62013-05-11 13:56:18 +02001856{
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001857 // should get machine stack start address early in main function
Bram Moolenaar99685e62013-05-11 13:56:18 +02001858 ruby_stack_start = stack_start;
1859}
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001860
1861 static int
1862convert_hash2dict(VALUE key, VALUE val, VALUE arg)
1863{
1864 dict_T *d = (dict_T *)arg;
1865 dictitem_T *di;
1866
Bram Moolenaardace9f72020-12-28 15:07:45 +01001867 di = dictitem_alloc((char_u *)RSTRING_PTR(rb_obj_as_string(key)));
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001868 if (di == NULL || ruby_convert_to_vim_value(val, &di->di_tv) != OK
1869 || dict_add(d, di) != OK)
1870 {
1871 d->dv_hashtab.ht_error = TRUE;
1872 return ST_STOP;
1873 }
1874 return ST_CONTINUE;
1875}
1876
1877 static int
1878ruby_convert_to_vim_value(VALUE val, typval_T *rettv)
1879{
1880 switch (TYPE(val))
1881 {
1882 case T_NIL:
1883 rettv->v_type = VAR_SPECIAL;
1884 rettv->vval.v_number = VVAL_NULL;
1885 break;
1886 case T_TRUE:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01001887 rettv->v_type = VAR_BOOL;
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001888 rettv->vval.v_number = VVAL_TRUE;
1889 break;
1890 case T_FALSE:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01001891 rettv->v_type = VAR_BOOL;
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001892 rettv->vval.v_number = VVAL_FALSE;
1893 break;
1894 case T_BIGNUM:
1895 case T_FIXNUM:
1896 rettv->v_type = VAR_NUMBER;
1897 rettv->vval.v_number = (varnumber_T)NUM2LONG(val);
1898 break;
1899#ifdef FEAT_FLOAT
1900 case T_FLOAT:
1901 rettv->v_type = VAR_FLOAT;
1902 rettv->vval.v_float = (float_T)NUM2DBL(val);
1903 break;
1904#endif
1905 default:
1906 val = rb_obj_as_string(val);
1907 // FALLTHROUGH
1908 case T_STRING:
1909 {
1910 VALUE str = (VALUE)RSTRING(val);
1911
1912 rettv->v_type = VAR_STRING;
1913 rettv->vval.v_string = vim_strnsave((char_u *)RSTRING_PTR(str),
Bram Moolenaar71ccd032020-06-12 22:59:11 +02001914 RSTRING_LEN(str));
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001915 }
1916 break;
1917 case T_ARRAY:
1918 {
1919 list_T *l;
1920 long i;
1921 typval_T v;
1922
1923 l = list_alloc();
1924 if (l == NULL)
1925 return FAIL;
1926
1927 for (i = 0; i < RARRAY_LEN(val); ++i)
1928 {
1929 if (ruby_convert_to_vim_value((VALUE)RARRAY_PTR(val)[i],
1930 &v) != OK)
1931 {
1932 list_unref(l);
1933 return FAIL;
1934 }
1935 list_append_tv(l, &v);
1936 clear_tv(&v);
1937 }
1938
1939 rettv->v_type = VAR_LIST;
1940 rettv->vval.v_list = l;
1941 ++l->lv_refcount;
1942 }
1943 break;
1944 case T_HASH:
1945 {
1946 dict_T *d;
1947
1948 d = dict_alloc();
1949 if (d == NULL)
1950 return FAIL;
1951
1952 rb_hash_foreach(val, convert_hash2dict, (VALUE)d);
1953 if (d->dv_hashtab.ht_error)
1954 {
1955 dict_unref(d);
1956 return FAIL;
1957 }
1958
1959 rettv->v_type = VAR_DICT;
1960 rettv->vval.v_dict = d;
1961 ++d->dv_refcount;
1962 }
1963 break;
1964 }
1965 return OK;
1966}
1967
1968 void
1969do_rubyeval(char_u *str, typval_T *rettv)
1970{
1971 int retval = FAIL;
1972
1973 if (ensure_ruby_initialized())
1974 {
1975 int state;
1976 VALUE obj;
1977
1978 obj = rb_eval_string_protect((const char *)str, &state);
1979 if (state)
1980 error_print(state);
1981 else
1982 retval = ruby_convert_to_vim_value(obj, rettv);
1983 }
1984 if (retval == FAIL)
1985 {
1986 rettv->v_type = VAR_NUMBER;
1987 rettv->vval.v_number = 0;
1988 }
1989}