blob: a8c8379b62d8d362bab5657a9949cf597751fa2c [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
187# else
188# define RUBY_PROC FARPROC
189# define load_dll vimLoadLib
190# define symbol_from_dll GetProcAddress
191# define close_dll FreeLibrary
192# endif
193#endif
194
Bram Moolenaar071d4272004-06-13 20:20:40 +0000195#if defined(PROTO) && !defined(FEAT_RUBY)
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100196// Define these to be able to generate the function prototypes.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000197# define VALUE int
198# define RUBY_DATA_FUNC int
199#endif
200
201static int ruby_initialized = 0;
Bram Moolenaar99685e62013-05-11 13:56:18 +0200202static void *ruby_stack_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000203static VALUE objtbl;
204
205static VALUE mVIM;
206static VALUE cBuffer;
207static VALUE cVimWindow;
208static VALUE eDeletedBufferError;
209static VALUE eDeletedWindowError;
210
211static int ensure_ruby_initialized(void);
212static void error_print(int);
213static void ruby_io_init(void);
214static void ruby_vim_init(void);
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100215static int ruby_convert_to_vim_value(VALUE val, typval_T *rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000216
Bram Moolenaar41a41412020-01-07 21:32:19 +0100217#if (RUBY_VERSION >= 19) || defined(RUBY_INIT_STACK)
Bram Moolenaar10f3a792013-05-20 12:52:29 +0200218# if defined(__ia64) && !defined(ruby_init_stack)
219# define ruby_init_stack(addr) ruby_init_stack((addr), rb_ia64_bsp())
220# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000221#endif
222
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200223#if defined(DYNAMIC_RUBY) || defined(PROTO)
Bram Moolenaaref269542016-01-19 13:22:12 +0100224# if defined(PROTO) && !defined(HINSTANCE)
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100225# define HINSTANCE int // for generating prototypes
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200226# endif
227
Bram Moolenaar071d4272004-06-13 20:20:40 +0000228/*
229 * Wrapper defines
230 */
Bram Moolenaar8b430b42020-02-22 15:01:00 +0100231// Ruby 2.7 actually expands the following symbols as macro.
232# if RUBY_VERSION >= 27
233# undef rb_define_global_function
234# undef rb_define_method
235# undef rb_define_module_function
236# undef rb_define_singleton_method
237# endif
238
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200239# define rb_assoc_new dll_rb_assoc_new
240# define rb_cObject (*dll_rb_cObject)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100241# define rb_class_new_instance dll_rb_class_new_instance
Bram Moolenaardace9f72020-12-28 15:07:45 +0100242# if RUBY_VERSION < 30
243# define rb_check_type dll_rb_check_type
244# endif
Bram Moolenaarf2f6d292015-12-28 20:57:10 +0100245# ifdef USE_TYPEDDATA
246# define rb_check_typeddata dll_rb_check_typeddata
Bram Moolenaar0c7485f2015-01-14 14:04:10 +0100247# endif
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200248# define rb_class_path dll_rb_class_path
Bram Moolenaarf2f6d292015-12-28 20:57:10 +0100249# ifdef USE_TYPEDDATA
Bram Moolenaar41a41412020-01-07 21:32:19 +0100250# if RUBY_VERSION >= 23
Bram Moolenaarf2f6d292015-12-28 20:57:10 +0100251# define rb_data_typed_object_wrap dll_rb_data_typed_object_wrap
252# else
253# define rb_data_typed_object_alloc dll_rb_data_typed_object_alloc
254# endif
255# else
256# define rb_data_object_alloc dll_rb_data_object_alloc
257# endif
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200258# define rb_define_class_under dll_rb_define_class_under
259# define rb_define_const dll_rb_define_const
260# define rb_define_global_function dll_rb_define_global_function
261# define rb_define_method dll_rb_define_method
262# define rb_define_module dll_rb_define_module
263# define rb_define_module_function dll_rb_define_module_function
264# define rb_define_singleton_method dll_rb_define_singleton_method
265# define rb_define_virtual_variable dll_rb_define_virtual_variable
266# define rb_stdout (*dll_rb_stdout)
Bram Moolenaarb6c8cd82018-07-24 05:41:30 +0200267# define rb_stderr (*dll_rb_stderr)
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200268# define rb_eArgError (*dll_rb_eArgError)
269# define rb_eIndexError (*dll_rb_eIndexError)
270# define rb_eRuntimeError (*dll_rb_eRuntimeError)
271# define rb_eStandardError (*dll_rb_eStandardError)
272# define rb_eval_string_protect dll_rb_eval_string_protect
Bram Moolenaar41a41412020-01-07 21:32:19 +0100273# if RUBY_VERSION >= 21
Bram Moolenaarf711cb22018-08-01 18:42:13 +0200274# define rb_funcallv dll_rb_funcallv
275# else
276# define rb_funcall2 dll_rb_funcall2
277# endif
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200278# define rb_global_variable dll_rb_global_variable
279# define rb_hash_aset dll_rb_hash_aset
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100280# define rb_hash_foreach dll_rb_hash_foreach
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200281# define rb_hash_new dll_rb_hash_new
282# define rb_inspect dll_rb_inspect
283# define rb_int2inum dll_rb_int2inum
Bram Moolenaar218beb32018-08-04 17:24:44 +0200284
285// ruby.h may redefine rb_intern to use RUBY_CONST_ID_CACHE(), but that won't
286// work. Not using the cache appears to be the best solution.
287# undef rb_intern
288# define rb_intern dll_rb_intern
289
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100290# if VIM_SIZEOF_INT < VIM_SIZEOF_LONG // 64 bits only
Bram Moolenaar41a41412020-01-07 21:32:19 +0100291# if RUBY_VERSION <= 18
Bram Moolenaar498af702014-03-28 21:58:21 +0100292# define rb_fix2int dll_rb_fix2int
293# define rb_num2int dll_rb_num2int
294# endif
Bram Moolenaardace9f72020-12-28 15:07:45 +0100295# if RUBY_VERSION < 30
296# define rb_num2uint dll_rb_num2uint
297# endif
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200298# endif
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100299# define rb_num2dbl dll_rb_num2dbl
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200300# define rb_lastline_get dll_rb_lastline_get
301# define rb_lastline_set dll_rb_lastline_set
Bram Moolenaard0573012017-10-28 21:11:06 +0200302# define rb_protect dll_rb_protect
303# define rb_load dll_rb_load
Bram Moolenaar41a41412020-01-07 21:32:19 +0100304# if RUBY_VERSION <= 18
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200305# define rb_num2long dll_rb_num2long
306# endif
Bram Moolenaar41a41412020-01-07 21:32:19 +0100307# if RUBY_VERSION <= 19
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200308# define rb_num2ulong dll_rb_num2ulong
309# endif
310# define rb_obj_alloc dll_rb_obj_alloc
311# define rb_obj_as_string dll_rb_obj_as_string
312# define rb_obj_id dll_rb_obj_id
313# define rb_raise dll_rb_raise
314# define rb_str_cat dll_rb_str_cat
315# define rb_str_concat dll_rb_str_concat
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100316# undef rb_str_new
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200317# define rb_str_new dll_rb_str_new
318# ifdef rb_str_new2
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100319// Ruby may #define rb_str_new2 to use rb_str_new_cstr.
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200320# define need_rb_str_new_cstr 1
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100321// Ruby's headers #define rb_str_new_cstr to make use of GCC's
322// __builtin_constant_p extension.
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200323# undef rb_str_new_cstr
324# define rb_str_new_cstr dll_rb_str_new_cstr
Bram Moolenaar76a86062013-05-11 17:45:48 +0200325# else
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200326# define rb_str_new2 dll_rb_str_new2
Bram Moolenaar76a86062013-05-11 17:45:48 +0200327# endif
Bram Moolenaar41a41412020-01-07 21:32:19 +0100328# if RUBY_VERSION >= 18
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200329# define rb_string_value dll_rb_string_value
330# define rb_string_value_ptr dll_rb_string_value_ptr
331# define rb_float_new dll_rb_float_new
332# define rb_ary_new dll_rb_ary_new
Bram Moolenaar51e9fbf2018-08-11 14:24:11 +0200333# ifdef rb_ary_new4
Bram Moolenaar49c99fc2020-02-11 23:01:39 +0100334# define RB_ARY_NEW4_MACRO 1
335# undef rb_ary_new4
Bram Moolenaar51e9fbf2018-08-11 14:24:11 +0200336# endif
337# define rb_ary_new4 dll_rb_ary_new4
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200338# define rb_ary_push dll_rb_ary_push
Bram Moolenaar41a41412020-01-07 21:32:19 +0100339# if (RUBY_VERSION >= 19) || defined(RUBY_INIT_STACK)
Bram Moolenaar10f3a792013-05-20 12:52:29 +0200340# ifdef __ia64
341# define rb_ia64_bsp dll_rb_ia64_bsp
342# undef ruby_init_stack
343# define ruby_init_stack(addr) dll_ruby_init_stack((addr), rb_ia64_bsp())
344# else
345# define ruby_init_stack dll_ruby_init_stack
346# endif
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200347# endif
348# else
349# define rb_str2cstr dll_rb_str2cstr
Bram Moolenaar3ca71f12010-10-27 16:49:47 +0200350# endif
Bram Moolenaar41a41412020-01-07 21:32:19 +0100351# if RUBY_VERSION >= 19
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200352# define rb_errinfo dll_rb_errinfo
353# else
354# define ruby_errinfo (*dll_ruby_errinfo)
355# endif
356# define ruby_init dll_ruby_init
357# define ruby_init_loadpath dll_ruby_init_loadpath
Bram Moolenaar4f974752019-02-17 17:44:42 +0100358# ifdef MSWIN
Bram Moolenaar41a41412020-01-07 21:32:19 +0100359# if RUBY_VERSION >= 19
Bram Moolenaar9d5c84a2018-12-19 20:48:46 +0100360# define ruby_sysinit dll_ruby_sysinit
361# else
362# define NtInitialize dll_NtInitialize
363# endif
Bram Moolenaar41a41412020-01-07 21:32:19 +0100364# if RUBY_VERSION >= 18
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200365# define rb_w32_snprintf dll_rb_w32_snprintf
366# endif
367# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000368
Bram Moolenaar41a41412020-01-07 21:32:19 +0100369# if RUBY_VERSION >= 19
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200370# define ruby_script dll_ruby_script
371# define rb_enc_find_index dll_rb_enc_find_index
372# define rb_enc_find dll_rb_enc_find
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100373# undef rb_enc_str_new
374# define rb_enc_str_new dll_rb_enc_str_new
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200375# define rb_sprintf dll_rb_sprintf
376# define rb_require dll_rb_require
Bram Moolenaar9b1067e2015-11-19 19:33:15 +0100377# define ruby_options dll_ruby_options
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200378# endif
Bram Moolenaar165641d2010-02-17 16:23:09 +0100379
Bram Moolenaar071d4272004-06-13 20:20:40 +0000380/*
381 * Pointers for dynamic link
382 */
383static VALUE (*dll_rb_assoc_new) (VALUE, VALUE);
Bram Moolenaarba52cde2010-06-25 04:29:11 +0200384VALUE *dll_rb_cFalseClass;
385VALUE *dll_rb_cFixnum;
Bram Moolenaar2016ae52016-06-13 20:08:43 +0200386# if defined(USE_RUBY_INTEGER)
Bram Moolenaar06469e92016-06-11 22:26:53 +0200387VALUE *dll_rb_cInteger;
388# endif
Bram Moolenaar41a41412020-01-07 21:32:19 +0100389# if RUBY_VERSION >= 20
Bram Moolenaardb3fbe52013-03-07 15:16:21 +0100390VALUE *dll_rb_cFloat;
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200391# endif
Bram Moolenaarba52cde2010-06-25 04:29:11 +0200392VALUE *dll_rb_cNilClass;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000393static VALUE *dll_rb_cObject;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100394VALUE *dll_rb_cString;
Bram Moolenaarba52cde2010-06-25 04:29:11 +0200395VALUE *dll_rb_cSymbol;
396VALUE *dll_rb_cTrueClass;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100397static VALUE (*dll_rb_class_new_instance) (int,VALUE*,VALUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000398static void (*dll_rb_check_type) (VALUE,int);
Bram Moolenaarf2f6d292015-12-28 20:57:10 +0100399# ifdef USE_TYPEDDATA
400static void *(*dll_rb_check_typeddata) (VALUE,const rb_data_type_t *);
401# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000402static VALUE (*dll_rb_class_path) (VALUE);
Bram Moolenaarf2f6d292015-12-28 20:57:10 +0100403# ifdef USE_TYPEDDATA
Bram Moolenaar41a41412020-01-07 21:32:19 +0100404# if RUBY_VERSION >= 23
Bram Moolenaarf2f6d292015-12-28 20:57:10 +0100405static VALUE (*dll_rb_data_typed_object_wrap) (VALUE, void*, const rb_data_type_t *);
406# else
407static VALUE (*dll_rb_data_typed_object_alloc) (VALUE, void*, const rb_data_type_t *);
408# endif
409# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000410static VALUE (*dll_rb_data_object_alloc) (VALUE, void*, RUBY_DATA_FUNC, RUBY_DATA_FUNC);
Bram Moolenaarf2f6d292015-12-28 20:57:10 +0100411# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000412static VALUE (*dll_rb_define_class_under) (VALUE, const char*, VALUE);
413static void (*dll_rb_define_const) (VALUE,const char*,VALUE);
414static void (*dll_rb_define_global_function) (const char*,VALUE(*)(),int);
415static void (*dll_rb_define_method) (VALUE,const char*,VALUE(*)(),int);
416static VALUE (*dll_rb_define_module) (const char*);
417static void (*dll_rb_define_module_function) (VALUE,const char*,VALUE(*)(),int);
418static void (*dll_rb_define_singleton_method) (VALUE,const char*,VALUE(*)(),int);
419static void (*dll_rb_define_virtual_variable) (const char*,VALUE(*)(),void(*)());
420static VALUE *dll_rb_stdout;
Bram Moolenaarb6c8cd82018-07-24 05:41:30 +0200421static VALUE *dll_rb_stderr;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000422static VALUE *dll_rb_eArgError;
423static VALUE *dll_rb_eIndexError;
424static VALUE *dll_rb_eRuntimeError;
425static VALUE *dll_rb_eStandardError;
426static VALUE (*dll_rb_eval_string_protect) (const char*, int*);
Bram Moolenaar41a41412020-01-07 21:32:19 +0100427# if RUBY_VERSION >= 21
Bram Moolenaarf711cb22018-08-01 18:42:13 +0200428static VALUE (*dll_rb_funcallv) (VALUE, ID, int, const VALUE*);
429# else
430static VALUE (*dll_rb_funcall2) (VALUE, ID, int, const VALUE*);
431# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000432static void (*dll_rb_global_variable) (VALUE*);
433static VALUE (*dll_rb_hash_aset) (VALUE, VALUE, VALUE);
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100434static VALUE (*dll_rb_hash_foreach) (VALUE, int (*)(VALUE, VALUE, VALUE), VALUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000435static VALUE (*dll_rb_hash_new) (void);
436static VALUE (*dll_rb_inspect) (VALUE);
437static VALUE (*dll_rb_int2inum) (long);
Bram Moolenaarf711cb22018-08-01 18:42:13 +0200438static ID (*dll_rb_intern) (const char*);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100439# if VIM_SIZEOF_INT < VIM_SIZEOF_LONG // 64 bits only
Bram Moolenaar2623b4f2012-09-18 16:36:32 +0200440static long (*dll_rb_fix2int) (VALUE);
441static long (*dll_rb_num2int) (VALUE);
442static unsigned long (*dll_rb_num2uint) (VALUE);
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200443# endif
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100444static double (*dll_rb_num2dbl) (VALUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000445static VALUE (*dll_rb_lastline_get) (void);
446static void (*dll_rb_lastline_set) (VALUE);
Bram Moolenaar37badc82018-01-31 20:15:30 +0100447static VALUE (*dll_rb_protect) (VALUE (*)(VALUE), VALUE, int*);
Bram Moolenaard0573012017-10-28 21:11:06 +0200448static void (*dll_rb_load) (VALUE, int);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000449static long (*dll_rb_num2long) (VALUE);
450static unsigned long (*dll_rb_num2ulong) (VALUE);
451static VALUE (*dll_rb_obj_alloc) (VALUE);
452static VALUE (*dll_rb_obj_as_string) (VALUE);
453static VALUE (*dll_rb_obj_id) (VALUE);
454static void (*dll_rb_raise) (VALUE, const char*, ...);
Bram Moolenaar41a41412020-01-07 21:32:19 +0100455# if RUBY_VERSION >= 18
Bram Moolenaarf9b5ef82010-09-29 13:02:53 +0200456static VALUE (*dll_rb_string_value) (volatile VALUE*);
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200457# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000458static char *(*dll_rb_str2cstr) (VALUE,int*);
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200459# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000460static VALUE (*dll_rb_str_cat) (VALUE, const char*, long);
461static VALUE (*dll_rb_str_concat) (VALUE, VALUE);
462static VALUE (*dll_rb_str_new) (const char*, long);
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200463# ifdef need_rb_str_new_cstr
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100464// Ruby may #define rb_str_new2 to use rb_str_new_cstr.
Bram Moolenaar1d2beae2010-05-22 21:56:55 +0200465static VALUE (*dll_rb_str_new_cstr) (const char*);
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200466# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000467static VALUE (*dll_rb_str_new2) (const char*);
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200468# endif
Bram Moolenaar41a41412020-01-07 21:32:19 +0100469# if RUBY_VERSION >= 19
Bram Moolenaar165641d2010-02-17 16:23:09 +0100470static VALUE (*dll_rb_errinfo) (void);
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200471# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000472static VALUE *dll_ruby_errinfo;
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200473# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000474static void (*dll_ruby_init) (void);
475static void (*dll_ruby_init_loadpath) (void);
Bram Moolenaar4f974752019-02-17 17:44:42 +0100476# ifdef MSWIN
Bram Moolenaar41a41412020-01-07 21:32:19 +0100477# if RUBY_VERSION >= 19
Bram Moolenaar4f391792017-01-15 16:59:07 +0100478static void (*dll_ruby_sysinit) (int*, char***);
Bram Moolenaar9d5c84a2018-12-19 20:48:46 +0100479# else
480static void (*dll_NtInitialize) (int*, char***);
481# endif
Bram Moolenaar41a41412020-01-07 21:32:19 +0100482# if RUBY_VERSION >= 18
Bram Moolenaar3ca71f12010-10-27 16:49:47 +0200483static int (*dll_rb_w32_snprintf)(char*, size_t, const char*, ...);
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200484# endif
Bram Moolenaar3ca71f12010-10-27 16:49:47 +0200485# endif
Bram Moolenaar41a41412020-01-07 21:32:19 +0100486# if RUBY_VERSION >= 18
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100487static char * (*dll_rb_string_value_ptr) (volatile VALUE*);
488static VALUE (*dll_rb_float_new) (double);
489static VALUE (*dll_rb_ary_new) (void);
Bram Moolenaar51e9fbf2018-08-11 14:24:11 +0200490static VALUE (*dll_rb_ary_new4) (long n, const VALUE *elts);
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100491static VALUE (*dll_rb_ary_push) (VALUE, VALUE);
Bram Moolenaar41a41412020-01-07 21:32:19 +0100492# if RUBY_VERSION >= 26
Bram Moolenaarb09c6842018-12-27 22:11:01 +0100493static void (*dll_rb_ary_detransient) (VALUE);
494# endif
Bram Moolenaar41a41412020-01-07 21:32:19 +0100495# if (RUBY_VERSION >= 19) || defined(RUBY_INIT_STACK)
Bram Moolenaar10f3a792013-05-20 12:52:29 +0200496# ifdef __ia64
Bram Moolenaar76a86062013-05-11 17:45:48 +0200497static void * (*dll_rb_ia64_bsp) (void);
498static void (*dll_ruby_init_stack)(VALUE*, void*);
Bram Moolenaar10f3a792013-05-20 12:52:29 +0200499# else
Bram Moolenaar76a86062013-05-11 17:45:48 +0200500static void (*dll_ruby_init_stack)(VALUE*);
Bram Moolenaar10f3a792013-05-20 12:52:29 +0200501# endif
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200502# endif
Bram Moolenaar76a86062013-05-11 17:45:48 +0200503# endif
Bram Moolenaar41a41412020-01-07 21:32:19 +0100504# if RUBY_VERSION >= 19
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100505static VALUE (*dll_rb_int2big)(SIGNED_VALUE);
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200506# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000507
Bram Moolenaar41a41412020-01-07 21:32:19 +0100508# if RUBY_VERSION >= 19
Bram Moolenaar165641d2010-02-17 16:23:09 +0100509static void (*dll_ruby_script) (const char*);
510static int (*dll_rb_enc_find_index) (const char*);
511static rb_encoding* (*dll_rb_enc_find) (const char*);
512static VALUE (*dll_rb_enc_str_new) (const char*, long, rb_encoding*);
513static VALUE (*dll_rb_sprintf) (const char*, ...);
Bram Moolenaar7a8ef142010-12-24 13:39:35 +0100514static VALUE (*dll_rb_require) (const char*);
Bram Moolenaardace9f72020-12-28 15:07:45 +0100515static void* (*dll_ruby_options)(int, char**);
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200516# endif
Bram Moolenaar165641d2010-02-17 16:23:09 +0100517
Bram Moolenaara1a118b2014-02-05 22:41:15 +0100518# if defined(USE_RGENGC) && USE_RGENGC
Bram Moolenaar41a41412020-01-07 21:32:19 +0100519# if RUBY_VERSION == 21
Bram Moolenaara1a118b2014-02-05 22:41:15 +0100520static void (*dll_rb_gc_writebarrier_unprotect_promoted)(VALUE);
Bram Moolenaar0c7485f2015-01-14 14:04:10 +0100521# else
522static void (*dll_rb_gc_writebarrier_unprotect)(VALUE obj);
523# endif
Bram Moolenaara1a118b2014-02-05 22:41:15 +0100524# endif
525
Bram Moolenaardace9f72020-12-28 15:07:45 +0100526# if RUBY_VERSION >= 30
527NORETURN(static void (*dll_ruby_malloc_size_overflow)(size_t, size_t));
528# endif
529
Bram Moolenaar0e121402020-12-10 20:50:34 +0100530# if RUBY_VERSION >= 26
531void rb_ary_detransient_stub(VALUE x);
532# endif
533
Bram Moolenaardace9f72020-12-28 15:07:45 +0100534// Do not generate a prototype here, VALUE isn't always defined.
535# ifndef PROTO
536# if RUBY_VERSION >= 19
537# if RUBY_VERSION >= 22
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100538 long
539rb_num2long_stub(VALUE x)
Bram Moolenaardace9f72020-12-28 15:07:45 +0100540# else
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100541 SIGNED_VALUE
542rb_num2long_stub(VALUE x)
Bram Moolenaardace9f72020-12-28 15:07:45 +0100543# endif
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100544{
545 return dll_rb_num2long(x);
546}
Bram Moolenaardace9f72020-12-28 15:07:45 +0100547# if RUBY_VERSION >= 26
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100548 VALUE
549rb_int2big_stub(intptr_t x)
Bram Moolenaardace9f72020-12-28 15:07:45 +0100550# else
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100551 VALUE
552rb_int2big_stub(SIGNED_VALUE x)
Bram Moolenaardace9f72020-12-28 15:07:45 +0100553# endif
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100554{
555 return dll_rb_int2big(x);
556}
Bram Moolenaardace9f72020-12-28 15:07:45 +0100557# if VIM_SIZEOF_INT < VIM_SIZEOF_LONG
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100558 long
559rb_fix2int_stub(VALUE x)
Bram Moolenaar0bcdd6e2013-04-14 16:19:03 +0200560{
561 return dll_rb_fix2int(x);
562}
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100563 long
564rb_num2int_stub(VALUE x)
Bram Moolenaar0bcdd6e2013-04-14 16:19:03 +0200565{
566 return dll_rb_num2int(x);
567}
Bram Moolenaardace9f72020-12-28 15:07:45 +0100568# endif
569# if RUBY_VERSION >= 20
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100570 VALUE
Bram Moolenaar886ed692013-02-26 13:41:35 +0100571rb_float_new_in_heap(double d)
572{
573 return dll_rb_float_new(d);
574}
Bram Moolenaardace9f72020-12-28 15:07:45 +0100575# if RUBY_VERSION >= 22
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100576 unsigned long
577rb_num2ulong(VALUE x)
Bram Moolenaardace9f72020-12-28 15:07:45 +0100578# else
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100579 VALUE
580rb_num2ulong(VALUE x)
Bram Moolenaardace9f72020-12-28 15:07:45 +0100581# endif
Bram Moolenaar886ed692013-02-26 13:41:35 +0100582{
583 return (long)RSHIFT((SIGNED_VALUE)(x),1);
584}
Bram Moolenaardace9f72020-12-28 15:07:45 +0100585# endif
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200586# endif
Bram Moolenaardace9f72020-12-28 15:07:45 +0100587# if defined(USE_RGENGC) && USE_RGENGC
588# if RUBY_VERSION == 21
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100589 void
590rb_gc_writebarrier_unprotect_promoted_stub(VALUE obj)
Bram Moolenaara1a118b2014-02-05 22:41:15 +0100591{
Bram Moolenaar90140742014-11-27 17:44:08 +0100592 dll_rb_gc_writebarrier_unprotect_promoted(obj);
Bram Moolenaara1a118b2014-02-05 22:41:15 +0100593}
Bram Moolenaardace9f72020-12-28 15:07:45 +0100594# else
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100595 void
596rb_gc_writebarrier_unprotect_stub(VALUE obj)
Bram Moolenaar0c7485f2015-01-14 14:04:10 +0100597{
598 dll_rb_gc_writebarrier_unprotect(obj);
599}
Bram Moolenaardace9f72020-12-28 15:07:45 +0100600# endif
Bram Moolenaar0c7485f2015-01-14 14:04:10 +0100601# endif
Bram Moolenaardace9f72020-12-28 15:07:45 +0100602# if RUBY_VERSION >= 26
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100603 void
604rb_ary_detransient_stub(VALUE x)
Bram Moolenaarf62fc312019-01-08 20:29:32 +0100605{
606 dll_rb_ary_detransient(x);
607}
Bram Moolenaardace9f72020-12-28 15:07:45 +0100608# endif
609# if RUBY_VERSION >= 30
610 void
611rb_check_type_stub(VALUE obj, int t)
612{
613 dll_rb_check_type(obj, t);
614}
Bram Moolenaar467b59c2021-01-08 19:31:39 +0100615# if VIM_SIZEOF_INT < VIM_SIZEOF_LONG // 64 bits only
Bram Moolenaardace9f72020-12-28 15:07:45 +0100616 unsigned long
617rb_num2uint_stub(VALUE x)
618{
619 return dll_rb_num2uint(x);
620}
Bram Moolenaar467b59c2021-01-08 19:31:39 +0100621# endif
Bram Moolenaardace9f72020-12-28 15:07:45 +0100622 void
623ruby_malloc_size_overflow_stub(size_t x, size_t y)
624{
625 dll_ruby_malloc_size_overflow(x, y);
626}
627# endif
628# endif // ifndef PROTO
Bram Moolenaarf62fc312019-01-08 20:29:32 +0100629
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100630static HINSTANCE hinstRuby = NULL; // Instance of ruby.dll
Bram Moolenaar071d4272004-06-13 20:20:40 +0000631
632/*
Bram Moolenaarda2303d2005-08-30 21:55:26 +0000633 * Table of name to function pointer of ruby.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000634 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000635static struct
636{
637 char *name;
638 RUBY_PROC *ptr;
639} ruby_funcname_table[] =
640{
641 {"rb_assoc_new", (RUBY_PROC*)&dll_rb_assoc_new},
642 {"rb_cFalseClass", (RUBY_PROC*)&dll_rb_cFalseClass},
Bram Moolenaar2016ae52016-06-13 20:08:43 +0200643# if defined(USE_RUBY_INTEGER)
Bram Moolenaar06469e92016-06-11 22:26:53 +0200644 {"rb_cInteger", (RUBY_PROC*)&dll_rb_cInteger},
Bram Moolenaar6abda992017-01-09 21:10:31 +0100645# else
646 {"rb_cFixnum", (RUBY_PROC*)&dll_rb_cFixnum},
Bram Moolenaar06469e92016-06-11 22:26:53 +0200647# endif
Bram Moolenaar41a41412020-01-07 21:32:19 +0100648# if RUBY_VERSION >= 20
Bram Moolenaardb3fbe52013-03-07 15:16:21 +0100649 {"rb_cFloat", (RUBY_PROC*)&dll_rb_cFloat},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200650# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000651 {"rb_cNilClass", (RUBY_PROC*)&dll_rb_cNilClass},
652 {"rb_cObject", (RUBY_PROC*)&dll_rb_cObject},
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100653 {"rb_cString", (RUBY_PROC*)&dll_rb_cString},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000654 {"rb_cSymbol", (RUBY_PROC*)&dll_rb_cSymbol},
655 {"rb_cTrueClass", (RUBY_PROC*)&dll_rb_cTrueClass},
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100656 {"rb_class_new_instance", (RUBY_PROC*)&dll_rb_class_new_instance},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000657 {"rb_check_type", (RUBY_PROC*)&dll_rb_check_type},
Bram Moolenaarf2f6d292015-12-28 20:57:10 +0100658# ifdef USE_TYPEDDATA
659 {"rb_check_typeddata", (RUBY_PROC*)&dll_rb_check_typeddata},
660# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000661 {"rb_class_path", (RUBY_PROC*)&dll_rb_class_path},
Bram Moolenaarf2f6d292015-12-28 20:57:10 +0100662# ifdef USE_TYPEDDATA
Bram Moolenaar41a41412020-01-07 21:32:19 +0100663# if RUBY_VERSION >= 23
Bram Moolenaarf2f6d292015-12-28 20:57:10 +0100664 {"rb_data_typed_object_wrap", (RUBY_PROC*)&dll_rb_data_typed_object_wrap},
665# else
666 {"rb_data_typed_object_alloc", (RUBY_PROC*)&dll_rb_data_typed_object_alloc},
667# endif
668# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000669 {"rb_data_object_alloc", (RUBY_PROC*)&dll_rb_data_object_alloc},
Bram Moolenaarf2f6d292015-12-28 20:57:10 +0100670# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000671 {"rb_define_class_under", (RUBY_PROC*)&dll_rb_define_class_under},
672 {"rb_define_const", (RUBY_PROC*)&dll_rb_define_const},
673 {"rb_define_global_function", (RUBY_PROC*)&dll_rb_define_global_function},
674 {"rb_define_method", (RUBY_PROC*)&dll_rb_define_method},
675 {"rb_define_module", (RUBY_PROC*)&dll_rb_define_module},
676 {"rb_define_module_function", (RUBY_PROC*)&dll_rb_define_module_function},
677 {"rb_define_singleton_method", (RUBY_PROC*)&dll_rb_define_singleton_method},
678 {"rb_define_virtual_variable", (RUBY_PROC*)&dll_rb_define_virtual_variable},
679 {"rb_stdout", (RUBY_PROC*)&dll_rb_stdout},
Bram Moolenaarb6c8cd82018-07-24 05:41:30 +0200680 {"rb_stderr", (RUBY_PROC*)&dll_rb_stderr},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000681 {"rb_eArgError", (RUBY_PROC*)&dll_rb_eArgError},
682 {"rb_eIndexError", (RUBY_PROC*)&dll_rb_eIndexError},
683 {"rb_eRuntimeError", (RUBY_PROC*)&dll_rb_eRuntimeError},
684 {"rb_eStandardError", (RUBY_PROC*)&dll_rb_eStandardError},
685 {"rb_eval_string_protect", (RUBY_PROC*)&dll_rb_eval_string_protect},
Bram Moolenaar41a41412020-01-07 21:32:19 +0100686# if RUBY_VERSION >= 21
Bram Moolenaarf711cb22018-08-01 18:42:13 +0200687 {"rb_funcallv", (RUBY_PROC*)&dll_rb_funcallv},
688# else
689 {"rb_funcall2", (RUBY_PROC*)&dll_rb_funcall2},
690# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000691 {"rb_global_variable", (RUBY_PROC*)&dll_rb_global_variable},
692 {"rb_hash_aset", (RUBY_PROC*)&dll_rb_hash_aset},
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100693 {"rb_hash_foreach", (RUBY_PROC*)&dll_rb_hash_foreach},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000694 {"rb_hash_new", (RUBY_PROC*)&dll_rb_hash_new},
695 {"rb_inspect", (RUBY_PROC*)&dll_rb_inspect},
696 {"rb_int2inum", (RUBY_PROC*)&dll_rb_int2inum},
Bram Moolenaarf711cb22018-08-01 18:42:13 +0200697 {"rb_intern", (RUBY_PROC*)&dll_rb_intern},
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100698# if VIM_SIZEOF_INT < VIM_SIZEOF_LONG // 64 bits only
Bram Moolenaar2623b4f2012-09-18 16:36:32 +0200699 {"rb_fix2int", (RUBY_PROC*)&dll_rb_fix2int},
700 {"rb_num2int", (RUBY_PROC*)&dll_rb_num2int},
701 {"rb_num2uint", (RUBY_PROC*)&dll_rb_num2uint},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200702# endif
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100703 {"rb_num2dbl", (RUBY_PROC*)&dll_rb_num2dbl},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000704 {"rb_lastline_get", (RUBY_PROC*)&dll_rb_lastline_get},
705 {"rb_lastline_set", (RUBY_PROC*)&dll_rb_lastline_set},
Bram Moolenaard0573012017-10-28 21:11:06 +0200706 {"rb_protect", (RUBY_PROC*)&dll_rb_protect},
707 {"rb_load", (RUBY_PROC*)&dll_rb_load},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000708 {"rb_num2long", (RUBY_PROC*)&dll_rb_num2long},
709 {"rb_num2ulong", (RUBY_PROC*)&dll_rb_num2ulong},
710 {"rb_obj_alloc", (RUBY_PROC*)&dll_rb_obj_alloc},
711 {"rb_obj_as_string", (RUBY_PROC*)&dll_rb_obj_as_string},
712 {"rb_obj_id", (RUBY_PROC*)&dll_rb_obj_id},
713 {"rb_raise", (RUBY_PROC*)&dll_rb_raise},
Bram Moolenaar41a41412020-01-07 21:32:19 +0100714# if RUBY_VERSION >= 18
Bram Moolenaarf9b5ef82010-09-29 13:02:53 +0200715 {"rb_string_value", (RUBY_PROC*)&dll_rb_string_value},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200716# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000717 {"rb_str2cstr", (RUBY_PROC*)&dll_rb_str2cstr},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200718# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000719 {"rb_str_cat", (RUBY_PROC*)&dll_rb_str_cat},
720 {"rb_str_concat", (RUBY_PROC*)&dll_rb_str_concat},
721 {"rb_str_new", (RUBY_PROC*)&dll_rb_str_new},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200722# ifdef need_rb_str_new_cstr
Bram Moolenaar1d2beae2010-05-22 21:56:55 +0200723 {"rb_str_new_cstr", (RUBY_PROC*)&dll_rb_str_new_cstr},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200724# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000725 {"rb_str_new2", (RUBY_PROC*)&dll_rb_str_new2},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200726# endif
Bram Moolenaar41a41412020-01-07 21:32:19 +0100727# if RUBY_VERSION >= 19
Bram Moolenaar165641d2010-02-17 16:23:09 +0100728 {"rb_errinfo", (RUBY_PROC*)&dll_rb_errinfo},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200729# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000730 {"ruby_errinfo", (RUBY_PROC*)&dll_ruby_errinfo},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200731# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000732 {"ruby_init", (RUBY_PROC*)&dll_ruby_init},
733 {"ruby_init_loadpath", (RUBY_PROC*)&dll_ruby_init_loadpath},
Bram Moolenaar4f974752019-02-17 17:44:42 +0100734# ifdef MSWIN
Bram Moolenaar41a41412020-01-07 21:32:19 +0100735# if RUBY_VERSION >= 19
Bram Moolenaar4f391792017-01-15 16:59:07 +0100736 {"ruby_sysinit", (RUBY_PROC*)&dll_ruby_sysinit},
Bram Moolenaar9d5c84a2018-12-19 20:48:46 +0100737# else
738 {"NtInitialize", (RUBY_PROC*)&dll_NtInitialize},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200739# endif
Bram Moolenaar41a41412020-01-07 21:32:19 +0100740# if RUBY_VERSION >= 18
Bram Moolenaar071d4272004-06-13 20:20:40 +0000741 {"rb_w32_snprintf", (RUBY_PROC*)&dll_rb_w32_snprintf},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200742# endif
Bram Moolenaar3ca71f12010-10-27 16:49:47 +0200743# endif
Bram Moolenaar41a41412020-01-07 21:32:19 +0100744# if RUBY_VERSION >= 18
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100745 {"rb_string_value_ptr", (RUBY_PROC*)&dll_rb_string_value_ptr},
Bram Moolenaar41a41412020-01-07 21:32:19 +0100746# if RUBY_VERSION <= 19
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100747 {"rb_float_new", (RUBY_PROC*)&dll_rb_float_new},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200748# else
Bram Moolenaar886ed692013-02-26 13:41:35 +0100749 {"rb_float_new_in_heap", (RUBY_PROC*)&dll_rb_float_new},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200750# endif
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100751 {"rb_ary_new", (RUBY_PROC*)&dll_rb_ary_new},
Bram Moolenaar51e9fbf2018-08-11 14:24:11 +0200752# ifdef RB_ARY_NEW4_MACRO
753 {"rb_ary_new_from_values", (RUBY_PROC*)&dll_rb_ary_new4},
754# else
755 {"rb_ary_new4", (RUBY_PROC*)&dll_rb_ary_new4},
756# endif
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100757 {"rb_ary_push", (RUBY_PROC*)&dll_rb_ary_push},
Bram Moolenaar41a41412020-01-07 21:32:19 +0100758# if RUBY_VERSION >= 26
Bram Moolenaarb09c6842018-12-27 22:11:01 +0100759 {"rb_ary_detransient", (RUBY_PROC*)&dll_rb_ary_detransient},
760# endif
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200761# endif
Bram Moolenaar41a41412020-01-07 21:32:19 +0100762# if RUBY_VERSION >= 19
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100763 {"rb_int2big", (RUBY_PROC*)&dll_rb_int2big},
Bram Moolenaar165641d2010-02-17 16:23:09 +0100764 {"ruby_script", (RUBY_PROC*)&dll_ruby_script},
765 {"rb_enc_find_index", (RUBY_PROC*)&dll_rb_enc_find_index},
766 {"rb_enc_find", (RUBY_PROC*)&dll_rb_enc_find},
767 {"rb_enc_str_new", (RUBY_PROC*)&dll_rb_enc_str_new},
768 {"rb_sprintf", (RUBY_PROC*)&dll_rb_sprintf},
Bram Moolenaar7a8ef142010-12-24 13:39:35 +0100769 {"rb_require", (RUBY_PROC*)&dll_rb_require},
Bram Moolenaar9b1067e2015-11-19 19:33:15 +0100770 {"ruby_options", (RUBY_PROC*)&dll_ruby_options},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200771# endif
Bram Moolenaar41a41412020-01-07 21:32:19 +0100772# if (RUBY_VERSION >= 19) || defined(RUBY_INIT_STACK)
Bram Moolenaar10f3a792013-05-20 12:52:29 +0200773# ifdef __ia64
774 {"rb_ia64_bsp", (RUBY_PROC*)&dll_rb_ia64_bsp},
775# endif
776 {"ruby_init_stack", (RUBY_PROC*)&dll_ruby_init_stack},
777# endif
Bram Moolenaara1a118b2014-02-05 22:41:15 +0100778# if defined(USE_RGENGC) && USE_RGENGC
Bram Moolenaar41a41412020-01-07 21:32:19 +0100779# if RUBY_VERSION == 21
Bram Moolenaara1a118b2014-02-05 22:41:15 +0100780 {"rb_gc_writebarrier_unprotect_promoted", (RUBY_PROC*)&dll_rb_gc_writebarrier_unprotect_promoted},
Bram Moolenaar0c7485f2015-01-14 14:04:10 +0100781# else
782 {"rb_gc_writebarrier_unprotect", (RUBY_PROC*)&dll_rb_gc_writebarrier_unprotect},
783# endif
Bram Moolenaara1a118b2014-02-05 22:41:15 +0100784# endif
Bram Moolenaardace9f72020-12-28 15:07:45 +0100785# if RUBY_VERSION >= 30
786 {"ruby_malloc_size_overflow", (RUBY_PROC*)&dll_ruby_malloc_size_overflow},
787# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000788 {"", NULL},
789};
790
791/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000792 * Load library and get all pointers.
793 * Parameter 'libname' provides name of DLL.
794 * Return OK or FAIL.
795 */
796 static int
797ruby_runtime_link_init(char *libname, int verbose)
798{
799 int i;
800
801 if (hinstRuby)
802 return OK;
Bram Moolenaarf9b5ef82010-09-29 13:02:53 +0200803 hinstRuby = load_dll(libname);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000804 if (!hinstRuby)
805 {
806 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100807 semsg(_(e_loadlib), libname);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000808 return FAIL;
809 }
810
811 for (i = 0; ruby_funcname_table[i].ptr; ++i)
812 {
Bram Moolenaarf9b5ef82010-09-29 13:02:53 +0200813 if (!(*ruby_funcname_table[i].ptr = symbol_from_dll(hinstRuby,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000814 ruby_funcname_table[i].name)))
815 {
Bram Moolenaarf9b5ef82010-09-29 13:02:53 +0200816 close_dll(hinstRuby);
Bram Moolenaar3ca71f12010-10-27 16:49:47 +0200817 hinstRuby = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000818 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100819 semsg(_(e_loadfunc), ruby_funcname_table[i].name);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000820 return FAIL;
821 }
822 }
823 return OK;
824}
825
826/*
827 * If ruby is enabled (there is installed ruby on Windows system) return TRUE,
828 * else FALSE.
829 */
830 int
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100831ruby_enabled(int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000832{
Bram Moolenaar25e4fcd2016-01-09 14:57:47 +0100833 return ruby_runtime_link_init((char *)p_rubydll, verbose) == OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000834}
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100835#endif // defined(DYNAMIC_RUBY) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000836
837 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100838ruby_end(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000839{
Bram Moolenaar071d4272004-06-13 20:20:40 +0000840}
841
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100842 void
843ex_ruby(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000844{
845 int state;
846 char *script = NULL;
847
Bram Moolenaar35a2e192006-03-13 22:07:11 +0000848 script = (char *)script_get(eap, eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000849 if (!eap->skip && ensure_ruby_initialized())
850 {
851 if (script == NULL)
852 rb_eval_string_protect((char *)eap->arg, &state);
853 else
854 rb_eval_string_protect(script, &state);
855 if (state)
856 error_print(state);
857 }
858 vim_free(script);
859}
860
Bram Moolenaar165641d2010-02-17 16:23:09 +0100861/*
862 * In Ruby 1.9 or later, ruby String object has encoding.
863 * conversion buffer string of vim to ruby String object using
864 * VIM encoding option.
865 */
866 static VALUE
867vim_str2rb_enc_str(const char *s)
868{
Bram Moolenaar41a41412020-01-07 21:32:19 +0100869#if RUBY_VERSION >= 19
Bram Moolenaar165641d2010-02-17 16:23:09 +0100870 long lval;
871 char_u *sval;
872 rb_encoding *enc;
873
Bram Moolenaardd1f4262020-12-31 17:41:01 +0100874 if (get_option_value((char_u *)"enc", &lval, &sval, 0) == gov_string)
Bram Moolenaar165641d2010-02-17 16:23:09 +0100875 {
876 enc = rb_enc_find((char *)sval);
877 vim_free(sval);
Bram Moolenaar758535a2016-03-30 22:06:16 +0200878 if (enc)
Bram Moolenaar9b0ac222016-06-01 20:31:43 +0200879 return rb_enc_str_new(s, (long)strlen(s), enc);
Bram Moolenaar165641d2010-02-17 16:23:09 +0100880 }
881#endif
882 return rb_str_new2(s);
883}
884
885 static VALUE
886eval_enc_string_protect(const char *str, int *state)
887{
Bram Moolenaar41a41412020-01-07 21:32:19 +0100888#if RUBY_VERSION >= 19
Bram Moolenaar165641d2010-02-17 16:23:09 +0100889 long lval;
890 char_u *sval;
891 rb_encoding *enc;
892 VALUE v;
893
Bram Moolenaardd1f4262020-12-31 17:41:01 +0100894 if (get_option_value((char_u *)"enc", &lval, &sval, 0) == gov_string)
Bram Moolenaar165641d2010-02-17 16:23:09 +0100895 {
896 enc = rb_enc_find((char *)sval);
897 vim_free(sval);
898 if (enc)
899 {
900 v = rb_sprintf("#-*- coding:%s -*-\n%s", rb_enc_name(enc), str);
901 return rb_eval_string_protect(StringValuePtr(v), state);
902 }
903 }
904#endif
905 return rb_eval_string_protect(str, state);
906}
907
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100908 void
909ex_rubydo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000910{
911 int state;
912 linenr_T i;
Bram Moolenaarc593fee2017-01-29 23:11:25 +0100913 buf_T *was_curbuf = curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000914
915 if (ensure_ruby_initialized())
916 {
917 if (u_save(eap->line1 - 1, eap->line2 + 1) != OK)
918 return;
Bram Moolenaar758535a2016-03-30 22:06:16 +0200919 for (i = eap->line1; i <= eap->line2; i++)
920 {
Bram Moolenaare980d8a2010-12-08 13:11:21 +0100921 VALUE line;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000922
Bram Moolenaarc593fee2017-01-29 23:11:25 +0100923 if (i > curbuf->b_ml.ml_line_count)
924 break;
Bram Moolenaare980d8a2010-12-08 13:11:21 +0100925 line = vim_str2rb_enc_str((char *)ml_get(i));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000926 rb_lastline_set(line);
Bram Moolenaar165641d2010-02-17 16:23:09 +0100927 eval_enc_string_protect((char *) eap->arg, &state);
Bram Moolenaar758535a2016-03-30 22:06:16 +0200928 if (state)
929 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000930 error_print(state);
931 break;
932 }
Bram Moolenaarc593fee2017-01-29 23:11:25 +0100933 if (was_curbuf != curbuf)
934 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000935 line = rb_lastline_get();
Bram Moolenaar758535a2016-03-30 22:06:16 +0200936 if (!NIL_P(line))
937 {
938 if (TYPE(line) != T_STRING)
939 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100940 emsg(_("E265: $_ must be an instance of String"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000941 return;
942 }
Bram Moolenaar165641d2010-02-17 16:23:09 +0100943 ml_replace(i, (char_u *) StringValuePtr(line), 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000944 changed();
945#ifdef SYNTAX_HL
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100946 syn_changed(i); // recompute syntax hl. for this line
Bram Moolenaar071d4272004-06-13 20:20:40 +0000947#endif
948 }
949 }
950 check_cursor();
951 update_curbuf(NOT_VALID);
952 }
953}
954
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100955 static VALUE
956rb_load_wrap(VALUE file_to_load)
Bram Moolenaar37badc82018-01-31 20:15:30 +0100957{
958 rb_load(file_to_load, 0);
959 return Qnil;
960}
961
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100962 void
963ex_rubyfile(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000964{
965 int state;
966
967 if (ensure_ruby_initialized())
968 {
Bram Moolenaar37badc82018-01-31 20:15:30 +0100969 VALUE file_to_load = rb_str_new2((const char *)eap->arg);
970 rb_protect(rb_load_wrap, file_to_load, &state);
971 if (state)
972 error_print(state);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000973 }
974}
975
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100976 void
977ruby_buffer_free(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000978{
Bram Moolenaare344bea2005-09-01 20:46:49 +0000979 if (buf->b_ruby_ref)
980 {
981 rb_hash_aset(objtbl, rb_obj_id((VALUE) buf->b_ruby_ref), Qnil);
982 RDATA(buf->b_ruby_ref)->data = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000983 }
984}
985
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100986 void
987ruby_window_free(win_T *win)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000988{
Bram Moolenaare344bea2005-09-01 20:46:49 +0000989 if (win->w_ruby_ref)
990 {
991 rb_hash_aset(objtbl, rb_obj_id((VALUE) win->w_ruby_ref), Qnil);
992 RDATA(win->w_ruby_ref)->data = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000993 }
994}
995
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100996 static int
997ensure_ruby_initialized(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000998{
999 if (!ruby_initialized)
1000 {
1001#ifdef DYNAMIC_RUBY
1002 if (ruby_enabled(TRUE))
1003 {
1004#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01001005#ifdef MSWIN
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001006 // suggested by Ariya Mizutani
Bram Moolenaar0b69c732010-02-17 15:11:50 +01001007 int argc = 1;
1008 char *argv[] = {"gvim.exe"};
Bram Moolenaar90140742014-11-27 17:44:08 +01001009 char **argvp = argv;
Bram Moolenaar41a41412020-01-07 21:32:19 +01001010# if RUBY_VERSION >= 19
Bram Moolenaarfe6ce332017-01-14 20:12:01 +01001011 ruby_sysinit(&argc, &argvp);
1012# else
Bram Moolenaar90140742014-11-27 17:44:08 +01001013 NtInitialize(&argc, &argvp);
Bram Moolenaarfe6ce332017-01-14 20:12:01 +01001014# endif
Bram Moolenaar0b69c732010-02-17 15:11:50 +01001015#endif
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001016 {
Bram Moolenaar41a41412020-01-07 21:32:19 +01001017#if (RUBY_VERSION >= 19) || defined(RUBY_INIT_STACK)
Bram Moolenaar99685e62013-05-11 13:56:18 +02001018 ruby_init_stack(ruby_stack_start);
Bram Moolenaar165641d2010-02-17 16:23:09 +01001019#endif
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001020 ruby_init();
1021 }
Bram Moolenaar41a41412020-01-07 21:32:19 +01001022#if RUBY_VERSION >= 19
Bram Moolenaar7a8ef142010-12-24 13:39:35 +01001023 {
1024 int dummy_argc = 2;
Bram Moolenaard1bc96ce2017-09-26 21:21:44 +02001025 char *dummy_argv[] = {"vim-ruby", "-e_=0"};
Bram Moolenaar9b1067e2015-11-19 19:33:15 +01001026 ruby_options(dummy_argc, dummy_argv);
Bram Moolenaar7a8ef142010-12-24 13:39:35 +01001027 }
Bram Moolenaar165641d2010-02-17 16:23:09 +01001028 ruby_script("vim-ruby");
Bram Moolenaar7a8ef142010-12-24 13:39:35 +01001029#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001030 ruby_init_loadpath();
Bram Moolenaar165641d2010-02-17 16:23:09 +01001031#endif
Bram Moolenaar7a8ef142010-12-24 13:39:35 +01001032 ruby_io_init();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001033 ruby_vim_init();
1034 ruby_initialized = 1;
1035#ifdef DYNAMIC_RUBY
1036 }
1037 else
1038 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001039 emsg(_("E266: Sorry, this command is disabled, the Ruby library could not be loaded."));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001040 return 0;
1041 }
1042#endif
1043 }
1044 return ruby_initialized;
1045}
1046
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001047 static void
1048error_print(int state)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001049{
Bram Moolenaar41a41412020-01-07 21:32:19 +01001050#if !defined(DYNAMIC_RUBY) && (RUBY_VERSION <= 18)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001051 RUBYEXTERN VALUE ruby_errinfo;
1052#endif
Bram Moolenaarf711cb22018-08-01 18:42:13 +02001053 VALUE error;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001054 VALUE eclass;
1055 VALUE einfo;
Bram Moolenaarf711cb22018-08-01 18:42:13 +02001056 VALUE bt;
1057 int attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001058 char buff[BUFSIZ];
Bram Moolenaarf711cb22018-08-01 18:42:13 +02001059 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001060
1061#define TAG_RETURN 0x1
1062#define TAG_BREAK 0x2
1063#define TAG_NEXT 0x3
1064#define TAG_RETRY 0x4
1065#define TAG_REDO 0x5
1066#define TAG_RAISE 0x6
1067#define TAG_THROW 0x7
1068#define TAG_FATAL 0x8
1069#define TAG_MASK 0xf
1070
Bram Moolenaar758535a2016-03-30 22:06:16 +02001071 switch (state)
1072 {
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001073 case TAG_RETURN:
1074 emsg(_("E267: unexpected return"));
1075 break;
1076 case TAG_NEXT:
1077 emsg(_("E268: unexpected next"));
1078 break;
1079 case TAG_BREAK:
1080 emsg(_("E269: unexpected break"));
1081 break;
1082 case TAG_REDO:
1083 emsg(_("E270: unexpected redo"));
1084 break;
1085 case TAG_RETRY:
1086 emsg(_("E271: retry outside of rescue clause"));
1087 break;
1088 case TAG_RAISE:
1089 case TAG_FATAL:
Bram Moolenaar41a41412020-01-07 21:32:19 +01001090#if RUBY_VERSION >= 19
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001091 error = rb_errinfo();
Bram Moolenaar165641d2010-02-17 16:23:09 +01001092#else
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001093 error = ruby_errinfo;
Bram Moolenaar165641d2010-02-17 16:23:09 +01001094#endif
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001095 eclass = CLASS_OF(error);
1096 einfo = rb_obj_as_string(error);
1097 if (eclass == rb_eRuntimeError && RSTRING_LEN(einfo) == 0)
1098 {
1099 emsg(_("E272: unhandled exception"));
1100 }
1101 else
1102 {
1103 VALUE epath;
1104 char *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001105
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001106 epath = rb_class_path(eclass);
1107 vim_snprintf(buff, BUFSIZ, "%s: %s",
1108 RSTRING_PTR(epath), RSTRING_PTR(einfo));
1109 p = strchr(buff, '\n');
1110 if (p) *p = '\0';
1111 emsg(buff);
1112 }
Bram Moolenaarf711cb22018-08-01 18:42:13 +02001113
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001114 attr = syn_name2attr((char_u *)"Error");
Bram Moolenaar49c99fc2020-02-11 23:01:39 +01001115#if RUBY_VERSION >= 21
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001116 bt = rb_funcallv(error, rb_intern("backtrace"), 0, 0);
1117 for (i = 0; i < RARRAY_LEN(bt); i++)
1118 msg_attr(RSTRING_PTR(RARRAY_AREF(bt, i)), attr);
Bram Moolenaar49c99fc2020-02-11 23:01:39 +01001119#else
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001120 bt = rb_funcall2(error, rb_intern("backtrace"), 0, 0);
1121 for (i = 0; i < RARRAY_LEN(bt); i++)
1122 msg_attr(RSTRING_PTR(RARRAY_PTR(bt)[i]), attr);
Bram Moolenaar49c99fc2020-02-11 23:01:39 +01001123#endif
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001124 break;
1125 default:
1126 vim_snprintf(buff, BUFSIZ, _("E273: unknown longjmp status %d"), state);
1127 emsg(buff);
1128 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001129 }
1130}
1131
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001132 static VALUE
1133vim_message(VALUE self UNUSED, VALUE str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001134{
1135 char *buff, *p;
1136
1137 str = rb_obj_as_string(str);
Bram Moolenaar3f5f7952011-08-04 19:34:59 +02001138 if (RSTRING_LEN(str) > 0)
1139 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001140 // Only do this when the string isn't empty, alloc(0) causes trouble.
Bram Moolenaar00ccf542017-09-03 15:17:48 +02001141 buff = ALLOCA_N(char, RSTRING_LEN(str) + 1);
Bram Moolenaar3f5f7952011-08-04 19:34:59 +02001142 strcpy(buff, RSTRING_PTR(str));
1143 p = strchr(buff, '\n');
1144 if (p) *p = '\0';
Bram Moolenaar32526b32019-01-19 17:43:09 +01001145 msg(buff);
Bram Moolenaar3f5f7952011-08-04 19:34:59 +02001146 }
1147 else
1148 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01001149 msg("");
Bram Moolenaar3f5f7952011-08-04 19:34:59 +02001150 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001151 return Qnil;
1152}
1153
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001154 static VALUE
1155vim_set_option(VALUE self UNUSED, VALUE str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001156{
Bram Moolenaar165641d2010-02-17 16:23:09 +01001157 do_set((char_u *)StringValuePtr(str), 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001158 update_screen(NOT_VALID);
1159 return Qnil;
1160}
1161
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001162 static VALUE
1163vim_command(VALUE self UNUSED, VALUE str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001164{
Bram Moolenaar165641d2010-02-17 16:23:09 +01001165 do_cmdline_cmd((char_u *)StringValuePtr(str));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001166 return Qnil;
1167}
1168
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001169#ifdef FEAT_EVAL
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001170 static VALUE
1171vim_to_ruby(typval_T *tv)
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001172{
1173 VALUE result = Qnil;
1174
1175 if (tv->v_type == VAR_STRING)
1176 {
Bram Moolenaar94127e42010-03-19 23:08:48 +01001177 result = rb_str_new2(tv->vval.v_string == NULL
1178 ? "" : (char *)(tv->vval.v_string));
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001179 }
1180 else if (tv->v_type == VAR_NUMBER)
1181 {
Bram Moolenaar639a2552010-03-17 18:15:23 +01001182 result = INT2NUM(tv->vval.v_number);
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001183 }
1184# ifdef FEAT_FLOAT
1185 else if (tv->v_type == VAR_FLOAT)
1186 {
Bram Moolenaar639a2552010-03-17 18:15:23 +01001187 result = rb_float_new(tv->vval.v_float);
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001188 }
1189# endif
1190 else if (tv->v_type == VAR_LIST)
1191 {
Bram Moolenaar639a2552010-03-17 18:15:23 +01001192 list_T *list = tv->vval.v_list;
1193 listitem_T *curr;
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001194
Bram Moolenaar639a2552010-03-17 18:15:23 +01001195 result = rb_ary_new();
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001196
Bram Moolenaar639a2552010-03-17 18:15:23 +01001197 if (list != NULL)
1198 {
Bram Moolenaaraeea7212020-04-02 18:50:46 +02001199 FOR_ALL_LIST_ITEMS(list, curr)
Bram Moolenaar639a2552010-03-17 18:15:23 +01001200 rb_ary_push(result, vim_to_ruby(&curr->li_tv));
Bram Moolenaar639a2552010-03-17 18:15:23 +01001201 }
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001202 }
1203 else if (tv->v_type == VAR_DICT)
1204 {
Bram Moolenaar639a2552010-03-17 18:15:23 +01001205 result = rb_hash_new();
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001206
Bram Moolenaar639a2552010-03-17 18:15:23 +01001207 if (tv->vval.v_dict != NULL)
1208 {
1209 hashtab_T *ht = &tv->vval.v_dict->dv_hashtab;
1210 long_u todo = ht->ht_used;
1211 hashitem_T *hi;
1212 dictitem_T *di;
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001213
Bram Moolenaar639a2552010-03-17 18:15:23 +01001214 for (hi = ht->ht_array; todo > 0; ++hi)
1215 {
1216 if (!HASHITEM_EMPTY(hi))
1217 {
1218 --todo;
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001219
Bram Moolenaar639a2552010-03-17 18:15:23 +01001220 di = dict_lookup(hi);
1221 rb_hash_aset(result, rb_str_new2((char *)hi->hi_key),
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001222 vim_to_ruby(&di->di_tv));
Bram Moolenaar639a2552010-03-17 18:15:23 +01001223 }
1224 }
1225 }
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001226 }
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01001227 else if (tv->v_type == VAR_BOOL || tv->v_type == VAR_SPECIAL)
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001228 {
Bram Moolenaard84b26a2018-07-28 17:18:09 +02001229 if (tv->vval.v_number == VVAL_TRUE)
1230 result = Qtrue;
1231 else if (tv->vval.v_number == VVAL_FALSE)
1232 result = Qfalse;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001233 }
1234 else if (tv->v_type == VAR_BLOB)
1235 {
1236 result = rb_str_new(tv->vval.v_blob->bv_ga.ga_data,
1237 tv->vval.v_blob->bv_ga.ga_len);
1238 }
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001239 // else return Qnil;
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001240
1241 return result;
1242}
1243#endif
1244
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001245 static VALUE
1246vim_evaluate(VALUE self UNUSED, VALUE str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001247{
1248#ifdef FEAT_EVAL
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001249 typval_T *tv;
1250 VALUE result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001251
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001252 tv = eval_expr((char_u *)StringValuePtr(str), NULL);
1253 if (tv == NULL)
Bram Moolenaar639a2552010-03-17 18:15:23 +01001254 return Qnil;
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001255 result = vim_to_ruby(tv);
1256
1257 free_tv(tv);
1258
1259 return result;
1260#else
1261 return Qnil;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001262#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001263}
1264
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001265#ifdef USE_TYPEDDATA
1266static size_t buffer_dsize(const void *buf);
1267
1268static const rb_data_type_t buffer_type = {
1269 "vim_buffer",
Bram Moolenaar41a41412020-01-07 21:32:19 +01001270 {0, 0, buffer_dsize,
1271# if RUBY_VERSION >= 27
Bram Moolenaar8b430b42020-02-22 15:01:00 +01001272 0, {0}
Bram Moolenaar41a41412020-01-07 21:32:19 +01001273# else
1274 {0, 0}
1275# endif
1276 },
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001277 0, 0,
1278# ifdef RUBY_TYPED_FREE_IMMEDIATELY
1279 0,
1280# endif
1281};
1282
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001283 static size_t
1284buffer_dsize(const void *buf UNUSED)
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001285{
1286 return sizeof(buf_T);
1287}
1288#endif
1289
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001290 static VALUE
1291buffer_new(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001292{
Bram Moolenaare344bea2005-09-01 20:46:49 +00001293 if (buf->b_ruby_ref)
1294 {
1295 return (VALUE) buf->b_ruby_ref;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001296 }
Bram Moolenaare344bea2005-09-01 20:46:49 +00001297 else
1298 {
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001299#ifdef USE_TYPEDDATA
1300 VALUE obj = TypedData_Wrap_Struct(cBuffer, &buffer_type, buf);
1301#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001302 VALUE obj = Data_Wrap_Struct(cBuffer, 0, 0, buf);
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001303#endif
Bram Moolenaare344bea2005-09-01 20:46:49 +00001304 buf->b_ruby_ref = (void *) obj;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001305 rb_hash_aset(objtbl, rb_obj_id(obj), obj);
1306 return obj;
1307 }
1308}
1309
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001310 static buf_T *
1311get_buf(VALUE obj)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001312{
1313 buf_T *buf;
1314
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001315#ifdef USE_TYPEDDATA
1316 TypedData_Get_Struct(obj, buf_T, &buffer_type, buf);
1317#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001318 Data_Get_Struct(obj, buf_T, buf);
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001319#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001320 if (buf == NULL)
1321 rb_raise(eDeletedBufferError, "attempt to refer to deleted buffer");
1322 return buf;
1323}
1324
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001325 static VALUE
1326vim_blob(VALUE self UNUSED, VALUE str)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001327{
1328 VALUE result = rb_str_new("0z", 2);
1329 char buf[4];
1330 int i;
1331 for (i = 0; i < RSTRING_LEN(str); i++)
1332 {
Bram Moolenaar0d13cce2019-02-23 14:23:03 +01001333 sprintf(buf, "%02X", (unsigned char)(RSTRING_PTR(str)[i]));
Bram Moolenaarb191be22019-01-30 21:00:12 +01001334 rb_str_concat(result, rb_str_new2(buf));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001335 }
1336 return result;
1337}
1338
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001339 static VALUE
Bram Moolenaard5a986f2020-12-06 21:11:31 +01001340buffer_s_current(VALUE self UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001341{
1342 return buffer_new(curbuf);
1343}
1344
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001345 static VALUE
Bram Moolenaard5a986f2020-12-06 21:11:31 +01001346buffer_s_current_getter(ID id UNUSED, VALUE *x UNUSED)
1347{
1348 return buffer_new(curbuf);
1349}
1350
1351 static VALUE
1352buffer_s_count(VALUE self UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001353{
1354 buf_T *b;
1355 int n = 0;
1356
Bram Moolenaar29323592016-07-24 22:04:11 +02001357 FOR_ALL_BUFFERS(b)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001358 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001359 // Deleted buffers should not be counted
1360 // SegPhault - 01/07/05
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001361 if (b->b_p_bl)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001362 n++;
1363 }
1364
Bram Moolenaar071d4272004-06-13 20:20:40 +00001365 return INT2NUM(n);
1366}
1367
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001368 static VALUE
1369buffer_s_aref(VALUE self UNUSED, VALUE num)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001370{
1371 buf_T *b;
1372 int n = NUM2INT(num);
1373
Bram Moolenaar29323592016-07-24 22:04:11 +02001374 FOR_ALL_BUFFERS(b)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001375 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001376 // Deleted buffers should not be counted
1377 // SegPhault - 01/07/05
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001378 if (!b->b_p_bl)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001379 continue;
1380
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001381 if (n == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001382 return buffer_new(b);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001383
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001384 n--;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001385 }
1386 return Qnil;
1387}
1388
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001389 static VALUE
1390buffer_name(VALUE self)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001391{
1392 buf_T *buf = get_buf(self);
1393
Bram Moolenaar35a2e192006-03-13 22:07:11 +00001394 return buf->b_ffname ? rb_str_new2((char *)buf->b_ffname) : Qnil;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001395}
1396
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001397 static VALUE
1398buffer_number(VALUE self)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001399{
1400 buf_T *buf = get_buf(self);
1401
1402 return INT2NUM(buf->b_fnum);
1403}
1404
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001405 static VALUE
1406buffer_count(VALUE self)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001407{
1408 buf_T *buf = get_buf(self);
1409
1410 return INT2NUM(buf->b_ml.ml_line_count);
1411}
1412
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001413 static VALUE
1414get_buffer_line(buf_T *buf, linenr_T n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001415{
Bram Moolenaar3c531602010-11-16 14:46:19 +01001416 if (n <= 0 || n > buf->b_ml.ml_line_count)
1417 rb_raise(rb_eIndexError, "line number %ld out of range", (long)n);
1418 return vim_str2rb_enc_str((char *)ml_get_buf(buf, n, FALSE));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001419}
1420
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001421 static VALUE
1422buffer_aref(VALUE self, VALUE num)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001423{
1424 buf_T *buf = get_buf(self);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001425
1426 if (buf != NULL)
1427 return get_buffer_line(buf, (linenr_T)NUM2LONG(num));
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001428 return Qnil; // For stop warning
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001429}
1430
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001431 static VALUE
1432set_buffer_line(buf_T *buf, linenr_T n, VALUE str)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001433{
Bram Moolenaar165641d2010-02-17 16:23:09 +01001434 char *line = StringValuePtr(str);
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001435 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001436
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001437 if (n > 0 && n <= buf->b_ml.ml_line_count && line != NULL)
1438 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001439 // set curwin/curbuf for "buf" and save some things
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001440 aucmd_prepbuf(&aco, buf);
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001441
Bram Moolenaar758535a2016-03-30 22:06:16 +02001442 if (u_savesub(n) == OK)
1443 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001444 ml_replace(n, (char_u *)line, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001445 changed();
1446#ifdef SYNTAX_HL
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001447 syn_changed(n); // recompute syntax hl. for this line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001448#endif
1449 }
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001450
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001451 // restore curwin/curbuf and a few other things
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001452 aucmd_restbuf(&aco);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001453 // Careful: autocommands may have made "buf" invalid!
Bram Moolenaarf30e74c2006-08-16 17:35:00 +00001454
Bram Moolenaar071d4272004-06-13 20:20:40 +00001455 update_curbuf(NOT_VALID);
1456 }
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001457 else
1458 {
Bram Moolenaar165641d2010-02-17 16:23:09 +01001459 rb_raise(rb_eIndexError, "line number %ld out of range", (long)n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001460 }
1461 return str;
1462}
1463
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001464 static VALUE
1465buffer_aset(VALUE self, VALUE num, VALUE str)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001466{
1467 buf_T *buf = get_buf(self);
1468
1469 if (buf != NULL)
1470 return set_buffer_line(buf, (linenr_T)NUM2LONG(num), str);
1471 return str;
1472}
1473
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001474 static VALUE
1475buffer_delete(VALUE self, VALUE num)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001476{
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001477 buf_T *buf = get_buf(self);
1478 long n = NUM2LONG(num);
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001479 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001480
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001481 if (n > 0 && n <= buf->b_ml.ml_line_count)
1482 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001483 // set curwin/curbuf for "buf" and save some things
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001484 aucmd_prepbuf(&aco, buf);
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001485
Bram Moolenaar758535a2016-03-30 22:06:16 +02001486 if (u_savedel(n, 1) == OK)
1487 {
Bram Moolenaarca70c072020-05-30 20:30:46 +02001488 ml_delete(n);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001489
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001490 // Changes to non-active buffers should properly refresh
1491 // SegPhault - 01/09/05
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001492 deleted_lines_mark(n, 1L);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001493
Bram Moolenaar071d4272004-06-13 20:20:40 +00001494 changed();
1495 }
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001496
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001497 // restore curwin/curbuf and a few other things
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001498 aucmd_restbuf(&aco);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001499 // Careful: autocommands may have made "buf" invalid!
Bram Moolenaarf30e74c2006-08-16 17:35:00 +00001500
Bram Moolenaar071d4272004-06-13 20:20:40 +00001501 update_curbuf(NOT_VALID);
1502 }
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001503 else
1504 {
Bram Moolenaar165641d2010-02-17 16:23:09 +01001505 rb_raise(rb_eIndexError, "line number %ld out of range", n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001506 }
1507 return Qnil;
1508}
1509
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001510 static VALUE
1511buffer_append(VALUE self, VALUE num, VALUE str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001512{
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001513 buf_T *buf = get_buf(self);
Bram Moolenaar165641d2010-02-17 16:23:09 +01001514 char *line = StringValuePtr(str);
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001515 long n = NUM2LONG(num);
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001516 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001517
Bram Moolenaar3c531602010-11-16 14:46:19 +01001518 if (line == NULL)
1519 {
Bram Moolenaar165641d2010-02-17 16:23:09 +01001520 rb_raise(rb_eIndexError, "NULL line");
1521 }
1522 else if (n >= 0 && n <= buf->b_ml.ml_line_count)
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001523 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001524 // set curwin/curbuf for "buf" and save some things
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001525 aucmd_prepbuf(&aco, buf);
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001526
Bram Moolenaar758535a2016-03-30 22:06:16 +02001527 if (u_inssub(n + 1) == OK)
1528 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001529 ml_append(n, (char_u *) line, (colnr_T) 0, FALSE);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001530
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001531 // Changes to non-active buffers should properly refresh screen
1532 // SegPhault - 12/20/04
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001533 appended_lines_mark(n, 1L);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001534
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001535 changed();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001536 }
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001537
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001538 // restore curwin/curbuf and a few other things
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001539 aucmd_restbuf(&aco);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001540 // Careful: autocommands may have made "buf" invalid!
Bram Moolenaarf30e74c2006-08-16 17:35:00 +00001541
Bram Moolenaar071d4272004-06-13 20:20:40 +00001542 update_curbuf(NOT_VALID);
1543 }
Bram Moolenaar3c531602010-11-16 14:46:19 +01001544 else
1545 {
Bram Moolenaar165641d2010-02-17 16:23:09 +01001546 rb_raise(rb_eIndexError, "line number %ld out of range", n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001547 }
1548 return str;
1549}
1550
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001551#ifdef USE_TYPEDDATA
1552static size_t window_dsize(const void *buf);
1553
1554static const rb_data_type_t window_type = {
1555 "vim_window",
Bram Moolenaar41a41412020-01-07 21:32:19 +01001556 {0, 0, window_dsize,
1557# if RUBY_VERSION >= 27
Bram Moolenaar8b430b42020-02-22 15:01:00 +01001558 0, {0}
Bram Moolenaar41a41412020-01-07 21:32:19 +01001559# else
1560 {0, 0}
1561# endif
1562 },
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001563 0, 0,
1564# ifdef RUBY_TYPED_FREE_IMMEDIATELY
1565 0,
1566# endif
1567};
1568
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001569 static size_t
1570window_dsize(const void *win UNUSED)
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001571{
1572 return sizeof(win_T);
1573}
1574#endif
1575
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001576 static VALUE
1577window_new(win_T *win)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001578{
Bram Moolenaare344bea2005-09-01 20:46:49 +00001579 if (win->w_ruby_ref)
1580 {
1581 return (VALUE) win->w_ruby_ref;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001582 }
Bram Moolenaare344bea2005-09-01 20:46:49 +00001583 else
1584 {
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001585#ifdef USE_TYPEDDATA
1586 VALUE obj = TypedData_Wrap_Struct(cVimWindow, &window_type, win);
1587#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001588 VALUE obj = Data_Wrap_Struct(cVimWindow, 0, 0, win);
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001589#endif
Bram Moolenaare344bea2005-09-01 20:46:49 +00001590 win->w_ruby_ref = (void *) obj;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001591 rb_hash_aset(objtbl, rb_obj_id(obj), obj);
1592 return obj;
1593 }
1594}
1595
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001596 static win_T *
1597get_win(VALUE obj)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001598{
1599 win_T *win;
1600
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001601#ifdef USE_TYPEDDATA
1602 TypedData_Get_Struct(obj, win_T, &window_type, win);
1603#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001604 Data_Get_Struct(obj, win_T, win);
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001605#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001606 if (win == NULL)
1607 rb_raise(eDeletedWindowError, "attempt to refer to deleted window");
1608 return win;
1609}
1610
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001611 static VALUE
Bram Moolenaard5a986f2020-12-06 21:11:31 +01001612window_s_current(VALUE self UNUSED)
1613{
1614 return window_new(curwin);
1615}
1616
1617 static VALUE
1618window_s_current_getter(ID id UNUSED, VALUE *x UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001619{
1620 return window_new(curwin);
1621}
1622
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001623/*
1624 * Added line manipulation functions
1625 * SegPhault - 03/07/05
1626 */
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001627 static VALUE
Bram Moolenaard5a986f2020-12-06 21:11:31 +01001628line_s_current(VALUE self UNUSED)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001629{
1630 return get_buffer_line(curbuf, curwin->w_cursor.lnum);
1631}
1632
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001633 static VALUE
1634set_current_line(VALUE self UNUSED, VALUE str)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001635{
1636 return set_buffer_line(curbuf, curwin->w_cursor.lnum, str);
1637}
1638
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001639 static VALUE
Bram Moolenaard5a986f2020-12-06 21:11:31 +01001640current_line_number(VALUE self UNUSED)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001641{
1642 return INT2FIX((int)curwin->w_cursor.lnum);
1643}
1644
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001645 static VALUE
Bram Moolenaard5a986f2020-12-06 21:11:31 +01001646window_s_count(VALUE self UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001647{
Bram Moolenaar071d4272004-06-13 20:20:40 +00001648 win_T *w;
1649 int n = 0;
1650
Bram Moolenaar29323592016-07-24 22:04:11 +02001651 FOR_ALL_WINDOWS(w)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001652 n++;
1653 return INT2NUM(n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001654}
1655
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001656 static VALUE
1657window_s_aref(VALUE self UNUSED, VALUE num)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001658{
1659 win_T *w;
1660 int n = NUM2INT(num);
1661
Bram Moolenaar071d4272004-06-13 20:20:40 +00001662 for (w = firstwin; w != NULL; w = w->w_next, --n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001663 if (n == 0)
1664 return window_new(w);
1665 return Qnil;
1666}
1667
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001668 static VALUE
1669window_buffer(VALUE self)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001670{
1671 win_T *win = get_win(self);
1672
1673 return buffer_new(win->w_buffer);
1674}
1675
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001676 static VALUE
1677window_height(VALUE self)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001678{
1679 win_T *win = get_win(self);
1680
1681 return INT2NUM(win->w_height);
1682}
1683
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001684 static VALUE
1685window_set_height(VALUE self, VALUE height)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001686{
1687 win_T *win = get_win(self);
1688 win_T *savewin = curwin;
1689
1690 curwin = win;
1691 win_setheight(NUM2INT(height));
1692 curwin = savewin;
1693 return height;
1694}
1695
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001696 static VALUE
1697window_width(VALUE self UNUSED)
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001698{
Bram Moolenaare745d752017-09-22 16:56:22 +02001699 return INT2NUM(get_win(self)->w_width);
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001700}
1701
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001702 static VALUE
1703window_set_width(VALUE self UNUSED, VALUE width)
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001704{
1705 win_T *win = get_win(self);
1706 win_T *savewin = curwin;
1707
1708 curwin = win;
1709 win_setwidth(NUM2INT(width));
1710 curwin = savewin;
1711 return width;
1712}
1713
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001714 static VALUE
1715window_cursor(VALUE self)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001716{
1717 win_T *win = get_win(self);
1718
1719 return rb_assoc_new(INT2NUM(win->w_cursor.lnum), INT2NUM(win->w_cursor.col));
1720}
1721
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001722 static VALUE
1723window_set_cursor(VALUE self, VALUE pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001724{
1725 VALUE lnum, col;
1726 win_T *win = get_win(self);
1727
1728 Check_Type(pos, T_ARRAY);
Bram Moolenaar165641d2010-02-17 16:23:09 +01001729 if (RARRAY_LEN(pos) != 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001730 rb_raise(rb_eArgError, "array length must be 2");
Bram Moolenaar165641d2010-02-17 16:23:09 +01001731 lnum = RARRAY_PTR(pos)[0];
1732 col = RARRAY_PTR(pos)[1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001733 win->w_cursor.lnum = NUM2LONG(lnum);
1734 win->w_cursor.col = NUM2UINT(col);
Bram Moolenaar53901442018-07-25 22:02:36 +02001735 win->w_set_curswant = TRUE;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001736 check_cursor(); // put cursor on an existing line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001737 update_screen(NOT_VALID);
1738 return Qnil;
1739}
1740
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001741 static VALUE
1742f_nop(VALUE self UNUSED)
Bram Moolenaar35df7d22012-04-20 18:05:47 +02001743{
1744 return Qnil;
1745}
1746
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001747 static VALUE
1748f_p(int argc, VALUE *argv, VALUE self UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001749{
1750 int i;
1751 VALUE str = rb_str_new("", 0);
Bram Moolenaar51e9fbf2018-08-11 14:24:11 +02001752 VALUE ret = Qnil;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001753
Bram Moolenaar758535a2016-03-30 22:06:16 +02001754 for (i = 0; i < argc; i++)
1755 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001756 if (i > 0) rb_str_cat(str, ", ", 2);
1757 rb_str_concat(str, rb_inspect(argv[i]));
1758 }
Bram Moolenaar32526b32019-01-19 17:43:09 +01001759 msg(RSTRING_PTR(str));
Bram Moolenaar51e9fbf2018-08-11 14:24:11 +02001760
1761 if (argc == 1)
1762 ret = argv[0];
1763 else if (argc > 1)
1764 ret = rb_ary_new4(argc, argv);
1765 return ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001766}
1767
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001768 static void
1769ruby_io_init(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001770{
1771#ifndef DYNAMIC_RUBY
1772 RUBYEXTERN VALUE rb_stdout;
Bram Moolenaarb6c8cd82018-07-24 05:41:30 +02001773 RUBYEXTERN VALUE rb_stderr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001774#endif
1775
1776 rb_stdout = rb_obj_alloc(rb_cObject);
Bram Moolenaarb6c8cd82018-07-24 05:41:30 +02001777 rb_stderr = rb_obj_alloc(rb_cObject);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001778 rb_define_singleton_method(rb_stdout, "write", vim_message, 1);
Bram Moolenaar35df7d22012-04-20 18:05:47 +02001779 rb_define_singleton_method(rb_stdout, "flush", f_nop, 0);
Bram Moolenaarb6c8cd82018-07-24 05:41:30 +02001780 rb_define_singleton_method(rb_stderr, "write", vim_message, 1);
1781 rb_define_singleton_method(rb_stderr, "flush", f_nop, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001782 rb_define_global_function("p", f_p, -1);
1783}
1784
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001785 static void
1786ruby_vim_init(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001787{
1788 objtbl = rb_hash_new();
1789 rb_global_variable(&objtbl);
1790
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001791 // The Vim module used to be called "VIM", but "Vim" is better. Make an
1792 // alias "VIM" for backwards compatibility.
Bram Moolenaarf711faf2007-05-10 16:48:19 +00001793 mVIM = rb_define_module("Vim");
1794 rb_define_const(rb_cObject, "VIM", mVIM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001795 rb_define_const(mVIM, "VERSION_MAJOR", INT2NUM(VIM_VERSION_MAJOR));
1796 rb_define_const(mVIM, "VERSION_MINOR", INT2NUM(VIM_VERSION_MINOR));
1797 rb_define_const(mVIM, "VERSION_BUILD", INT2NUM(VIM_VERSION_BUILD));
1798 rb_define_const(mVIM, "VERSION_PATCHLEVEL", INT2NUM(VIM_VERSION_PATCHLEVEL));
1799 rb_define_const(mVIM, "VERSION_SHORT", rb_str_new2(VIM_VERSION_SHORT));
1800 rb_define_const(mVIM, "VERSION_MEDIUM", rb_str_new2(VIM_VERSION_MEDIUM));
1801 rb_define_const(mVIM, "VERSION_LONG", rb_str_new2(VIM_VERSION_LONG));
1802 rb_define_const(mVIM, "VERSION_LONG_DATE", rb_str_new2(VIM_VERSION_LONG_DATE));
1803 rb_define_module_function(mVIM, "message", vim_message, 1);
1804 rb_define_module_function(mVIM, "set_option", vim_set_option, 1);
1805 rb_define_module_function(mVIM, "command", vim_command, 1);
1806 rb_define_module_function(mVIM, "evaluate", vim_evaluate, 1);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001807 rb_define_module_function(mVIM, "blob", vim_blob, 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001808
1809 eDeletedBufferError = rb_define_class_under(mVIM, "DeletedBufferError",
1810 rb_eStandardError);
1811 eDeletedWindowError = rb_define_class_under(mVIM, "DeletedWindowError",
1812 rb_eStandardError);
1813
1814 cBuffer = rb_define_class_under(mVIM, "Buffer", rb_cObject);
1815 rb_define_singleton_method(cBuffer, "current", buffer_s_current, 0);
1816 rb_define_singleton_method(cBuffer, "count", buffer_s_count, 0);
1817 rb_define_singleton_method(cBuffer, "[]", buffer_s_aref, 1);
1818 rb_define_method(cBuffer, "name", buffer_name, 0);
1819 rb_define_method(cBuffer, "number", buffer_number, 0);
1820 rb_define_method(cBuffer, "count", buffer_count, 0);
1821 rb_define_method(cBuffer, "length", buffer_count, 0);
1822 rb_define_method(cBuffer, "[]", buffer_aref, 1);
1823 rb_define_method(cBuffer, "[]=", buffer_aset, 2);
1824 rb_define_method(cBuffer, "delete", buffer_delete, 1);
1825 rb_define_method(cBuffer, "append", buffer_append, 2);
1826
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001827 // Added line manipulation functions
1828 // SegPhault - 03/07/05
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001829 rb_define_method(cBuffer, "line_number", current_line_number, 0);
1830 rb_define_method(cBuffer, "line", line_s_current, 0);
1831 rb_define_method(cBuffer, "line=", set_current_line, 1);
1832
1833
Bram Moolenaar071d4272004-06-13 20:20:40 +00001834 cVimWindow = rb_define_class_under(mVIM, "Window", rb_cObject);
1835 rb_define_singleton_method(cVimWindow, "current", window_s_current, 0);
1836 rb_define_singleton_method(cVimWindow, "count", window_s_count, 0);
1837 rb_define_singleton_method(cVimWindow, "[]", window_s_aref, 1);
1838 rb_define_method(cVimWindow, "buffer", window_buffer, 0);
1839 rb_define_method(cVimWindow, "height", window_height, 0);
1840 rb_define_method(cVimWindow, "height=", window_set_height, 1);
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001841 rb_define_method(cVimWindow, "width", window_width, 0);
1842 rb_define_method(cVimWindow, "width=", window_set_width, 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001843 rb_define_method(cVimWindow, "cursor", window_cursor, 0);
1844 rb_define_method(cVimWindow, "cursor=", window_set_cursor, 1);
1845
Bram Moolenaard5a986f2020-12-06 21:11:31 +01001846 rb_define_virtual_variable("$curbuf", buffer_s_current_getter, 0);
1847 rb_define_virtual_variable("$curwin", window_s_current_getter, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001848}
Bram Moolenaar99685e62013-05-11 13:56:18 +02001849
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001850 void
1851vim_ruby_init(void *stack_start)
Bram Moolenaar99685e62013-05-11 13:56:18 +02001852{
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001853 // should get machine stack start address early in main function
Bram Moolenaar99685e62013-05-11 13:56:18 +02001854 ruby_stack_start = stack_start;
1855}
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001856
1857 static int
1858convert_hash2dict(VALUE key, VALUE val, VALUE arg)
1859{
1860 dict_T *d = (dict_T *)arg;
1861 dictitem_T *di;
1862
Bram Moolenaardace9f72020-12-28 15:07:45 +01001863 di = dictitem_alloc((char_u *)RSTRING_PTR(rb_obj_as_string(key)));
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001864 if (di == NULL || ruby_convert_to_vim_value(val, &di->di_tv) != OK
1865 || dict_add(d, di) != OK)
1866 {
1867 d->dv_hashtab.ht_error = TRUE;
1868 return ST_STOP;
1869 }
1870 return ST_CONTINUE;
1871}
1872
1873 static int
1874ruby_convert_to_vim_value(VALUE val, typval_T *rettv)
1875{
1876 switch (TYPE(val))
1877 {
1878 case T_NIL:
1879 rettv->v_type = VAR_SPECIAL;
1880 rettv->vval.v_number = VVAL_NULL;
1881 break;
1882 case T_TRUE:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01001883 rettv->v_type = VAR_BOOL;
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001884 rettv->vval.v_number = VVAL_TRUE;
1885 break;
1886 case T_FALSE:
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_FALSE;
1889 break;
1890 case T_BIGNUM:
1891 case T_FIXNUM:
1892 rettv->v_type = VAR_NUMBER;
1893 rettv->vval.v_number = (varnumber_T)NUM2LONG(val);
1894 break;
1895#ifdef FEAT_FLOAT
1896 case T_FLOAT:
1897 rettv->v_type = VAR_FLOAT;
1898 rettv->vval.v_float = (float_T)NUM2DBL(val);
1899 break;
1900#endif
1901 default:
1902 val = rb_obj_as_string(val);
1903 // FALLTHROUGH
1904 case T_STRING:
1905 {
1906 VALUE str = (VALUE)RSTRING(val);
1907
1908 rettv->v_type = VAR_STRING;
1909 rettv->vval.v_string = vim_strnsave((char_u *)RSTRING_PTR(str),
Bram Moolenaar71ccd032020-06-12 22:59:11 +02001910 RSTRING_LEN(str));
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001911 }
1912 break;
1913 case T_ARRAY:
1914 {
1915 list_T *l;
1916 long i;
1917 typval_T v;
1918
1919 l = list_alloc();
1920 if (l == NULL)
1921 return FAIL;
1922
1923 for (i = 0; i < RARRAY_LEN(val); ++i)
1924 {
1925 if (ruby_convert_to_vim_value((VALUE)RARRAY_PTR(val)[i],
1926 &v) != OK)
1927 {
1928 list_unref(l);
1929 return FAIL;
1930 }
1931 list_append_tv(l, &v);
1932 clear_tv(&v);
1933 }
1934
1935 rettv->v_type = VAR_LIST;
1936 rettv->vval.v_list = l;
1937 ++l->lv_refcount;
1938 }
1939 break;
1940 case T_HASH:
1941 {
1942 dict_T *d;
1943
1944 d = dict_alloc();
1945 if (d == NULL)
1946 return FAIL;
1947
1948 rb_hash_foreach(val, convert_hash2dict, (VALUE)d);
1949 if (d->dv_hashtab.ht_error)
1950 {
1951 dict_unref(d);
1952 return FAIL;
1953 }
1954
1955 rettv->v_type = VAR_DICT;
1956 rettv->vval.v_dict = d;
1957 ++d->dv_refcount;
1958 }
1959 break;
1960 }
1961 return OK;
1962}
1963
1964 void
1965do_rubyeval(char_u *str, typval_T *rettv)
1966{
1967 int retval = FAIL;
1968
1969 if (ensure_ruby_initialized())
1970 {
1971 int state;
1972 VALUE obj;
1973
1974 obj = rb_eval_string_protect((const char *)str, &state);
1975 if (state)
1976 error_print(state);
1977 else
1978 retval = ruby_convert_to_vim_value(obj, rettv);
1979 }
1980 if (retval == FAIL)
1981 {
1982 rettv->v_type = VAR_NUMBER;
1983 rettv->vval.v_number = 0;
1984 }
1985}