blob: 3751025a1e3c08de734687126c82ad3c6cbf0ff2 [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}
615 unsigned long
616rb_num2uint_stub(VALUE x)
617{
618 return dll_rb_num2uint(x);
619}
620 void
621ruby_malloc_size_overflow_stub(size_t x, size_t y)
622{
623 dll_ruby_malloc_size_overflow(x, y);
624}
625# endif
626# endif // ifndef PROTO
Bram Moolenaarf62fc312019-01-08 20:29:32 +0100627
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100628static HINSTANCE hinstRuby = NULL; // Instance of ruby.dll
Bram Moolenaar071d4272004-06-13 20:20:40 +0000629
630/*
Bram Moolenaarda2303d2005-08-30 21:55:26 +0000631 * Table of name to function pointer of ruby.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000632 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000633static struct
634{
635 char *name;
636 RUBY_PROC *ptr;
637} ruby_funcname_table[] =
638{
639 {"rb_assoc_new", (RUBY_PROC*)&dll_rb_assoc_new},
640 {"rb_cFalseClass", (RUBY_PROC*)&dll_rb_cFalseClass},
Bram Moolenaar2016ae52016-06-13 20:08:43 +0200641# if defined(USE_RUBY_INTEGER)
Bram Moolenaar06469e92016-06-11 22:26:53 +0200642 {"rb_cInteger", (RUBY_PROC*)&dll_rb_cInteger},
Bram Moolenaar6abda992017-01-09 21:10:31 +0100643# else
644 {"rb_cFixnum", (RUBY_PROC*)&dll_rb_cFixnum},
Bram Moolenaar06469e92016-06-11 22:26:53 +0200645# endif
Bram Moolenaar41a41412020-01-07 21:32:19 +0100646# if RUBY_VERSION >= 20
Bram Moolenaardb3fbe52013-03-07 15:16:21 +0100647 {"rb_cFloat", (RUBY_PROC*)&dll_rb_cFloat},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200648# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000649 {"rb_cNilClass", (RUBY_PROC*)&dll_rb_cNilClass},
650 {"rb_cObject", (RUBY_PROC*)&dll_rb_cObject},
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100651 {"rb_cString", (RUBY_PROC*)&dll_rb_cString},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000652 {"rb_cSymbol", (RUBY_PROC*)&dll_rb_cSymbol},
653 {"rb_cTrueClass", (RUBY_PROC*)&dll_rb_cTrueClass},
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100654 {"rb_class_new_instance", (RUBY_PROC*)&dll_rb_class_new_instance},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000655 {"rb_check_type", (RUBY_PROC*)&dll_rb_check_type},
Bram Moolenaarf2f6d292015-12-28 20:57:10 +0100656# ifdef USE_TYPEDDATA
657 {"rb_check_typeddata", (RUBY_PROC*)&dll_rb_check_typeddata},
658# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000659 {"rb_class_path", (RUBY_PROC*)&dll_rb_class_path},
Bram Moolenaarf2f6d292015-12-28 20:57:10 +0100660# ifdef USE_TYPEDDATA
Bram Moolenaar41a41412020-01-07 21:32:19 +0100661# if RUBY_VERSION >= 23
Bram Moolenaarf2f6d292015-12-28 20:57:10 +0100662 {"rb_data_typed_object_wrap", (RUBY_PROC*)&dll_rb_data_typed_object_wrap},
663# else
664 {"rb_data_typed_object_alloc", (RUBY_PROC*)&dll_rb_data_typed_object_alloc},
665# endif
666# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000667 {"rb_data_object_alloc", (RUBY_PROC*)&dll_rb_data_object_alloc},
Bram Moolenaarf2f6d292015-12-28 20:57:10 +0100668# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000669 {"rb_define_class_under", (RUBY_PROC*)&dll_rb_define_class_under},
670 {"rb_define_const", (RUBY_PROC*)&dll_rb_define_const},
671 {"rb_define_global_function", (RUBY_PROC*)&dll_rb_define_global_function},
672 {"rb_define_method", (RUBY_PROC*)&dll_rb_define_method},
673 {"rb_define_module", (RUBY_PROC*)&dll_rb_define_module},
674 {"rb_define_module_function", (RUBY_PROC*)&dll_rb_define_module_function},
675 {"rb_define_singleton_method", (RUBY_PROC*)&dll_rb_define_singleton_method},
676 {"rb_define_virtual_variable", (RUBY_PROC*)&dll_rb_define_virtual_variable},
677 {"rb_stdout", (RUBY_PROC*)&dll_rb_stdout},
Bram Moolenaarb6c8cd82018-07-24 05:41:30 +0200678 {"rb_stderr", (RUBY_PROC*)&dll_rb_stderr},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000679 {"rb_eArgError", (RUBY_PROC*)&dll_rb_eArgError},
680 {"rb_eIndexError", (RUBY_PROC*)&dll_rb_eIndexError},
681 {"rb_eRuntimeError", (RUBY_PROC*)&dll_rb_eRuntimeError},
682 {"rb_eStandardError", (RUBY_PROC*)&dll_rb_eStandardError},
683 {"rb_eval_string_protect", (RUBY_PROC*)&dll_rb_eval_string_protect},
Bram Moolenaar41a41412020-01-07 21:32:19 +0100684# if RUBY_VERSION >= 21
Bram Moolenaarf711cb22018-08-01 18:42:13 +0200685 {"rb_funcallv", (RUBY_PROC*)&dll_rb_funcallv},
686# else
687 {"rb_funcall2", (RUBY_PROC*)&dll_rb_funcall2},
688# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000689 {"rb_global_variable", (RUBY_PROC*)&dll_rb_global_variable},
690 {"rb_hash_aset", (RUBY_PROC*)&dll_rb_hash_aset},
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100691 {"rb_hash_foreach", (RUBY_PROC*)&dll_rb_hash_foreach},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000692 {"rb_hash_new", (RUBY_PROC*)&dll_rb_hash_new},
693 {"rb_inspect", (RUBY_PROC*)&dll_rb_inspect},
694 {"rb_int2inum", (RUBY_PROC*)&dll_rb_int2inum},
Bram Moolenaarf711cb22018-08-01 18:42:13 +0200695 {"rb_intern", (RUBY_PROC*)&dll_rb_intern},
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100696# if VIM_SIZEOF_INT < VIM_SIZEOF_LONG // 64 bits only
Bram Moolenaar2623b4f2012-09-18 16:36:32 +0200697 {"rb_fix2int", (RUBY_PROC*)&dll_rb_fix2int},
698 {"rb_num2int", (RUBY_PROC*)&dll_rb_num2int},
699 {"rb_num2uint", (RUBY_PROC*)&dll_rb_num2uint},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200700# endif
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100701 {"rb_num2dbl", (RUBY_PROC*)&dll_rb_num2dbl},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000702 {"rb_lastline_get", (RUBY_PROC*)&dll_rb_lastline_get},
703 {"rb_lastline_set", (RUBY_PROC*)&dll_rb_lastline_set},
Bram Moolenaard0573012017-10-28 21:11:06 +0200704 {"rb_protect", (RUBY_PROC*)&dll_rb_protect},
705 {"rb_load", (RUBY_PROC*)&dll_rb_load},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000706 {"rb_num2long", (RUBY_PROC*)&dll_rb_num2long},
707 {"rb_num2ulong", (RUBY_PROC*)&dll_rb_num2ulong},
708 {"rb_obj_alloc", (RUBY_PROC*)&dll_rb_obj_alloc},
709 {"rb_obj_as_string", (RUBY_PROC*)&dll_rb_obj_as_string},
710 {"rb_obj_id", (RUBY_PROC*)&dll_rb_obj_id},
711 {"rb_raise", (RUBY_PROC*)&dll_rb_raise},
Bram Moolenaar41a41412020-01-07 21:32:19 +0100712# if RUBY_VERSION >= 18
Bram Moolenaarf9b5ef82010-09-29 13:02:53 +0200713 {"rb_string_value", (RUBY_PROC*)&dll_rb_string_value},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200714# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000715 {"rb_str2cstr", (RUBY_PROC*)&dll_rb_str2cstr},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200716# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000717 {"rb_str_cat", (RUBY_PROC*)&dll_rb_str_cat},
718 {"rb_str_concat", (RUBY_PROC*)&dll_rb_str_concat},
719 {"rb_str_new", (RUBY_PROC*)&dll_rb_str_new},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200720# ifdef need_rb_str_new_cstr
Bram Moolenaar1d2beae2010-05-22 21:56:55 +0200721 {"rb_str_new_cstr", (RUBY_PROC*)&dll_rb_str_new_cstr},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200722# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000723 {"rb_str_new2", (RUBY_PROC*)&dll_rb_str_new2},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200724# endif
Bram Moolenaar41a41412020-01-07 21:32:19 +0100725# if RUBY_VERSION >= 19
Bram Moolenaar165641d2010-02-17 16:23:09 +0100726 {"rb_errinfo", (RUBY_PROC*)&dll_rb_errinfo},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200727# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000728 {"ruby_errinfo", (RUBY_PROC*)&dll_ruby_errinfo},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200729# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000730 {"ruby_init", (RUBY_PROC*)&dll_ruby_init},
731 {"ruby_init_loadpath", (RUBY_PROC*)&dll_ruby_init_loadpath},
Bram Moolenaar4f974752019-02-17 17:44:42 +0100732# ifdef MSWIN
Bram Moolenaar41a41412020-01-07 21:32:19 +0100733# if RUBY_VERSION >= 19
Bram Moolenaar4f391792017-01-15 16:59:07 +0100734 {"ruby_sysinit", (RUBY_PROC*)&dll_ruby_sysinit},
Bram Moolenaar9d5c84a2018-12-19 20:48:46 +0100735# else
736 {"NtInitialize", (RUBY_PROC*)&dll_NtInitialize},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200737# endif
Bram Moolenaar41a41412020-01-07 21:32:19 +0100738# if RUBY_VERSION >= 18
Bram Moolenaar071d4272004-06-13 20:20:40 +0000739 {"rb_w32_snprintf", (RUBY_PROC*)&dll_rb_w32_snprintf},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200740# endif
Bram Moolenaar3ca71f12010-10-27 16:49:47 +0200741# endif
Bram Moolenaar41a41412020-01-07 21:32:19 +0100742# if RUBY_VERSION >= 18
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100743 {"rb_string_value_ptr", (RUBY_PROC*)&dll_rb_string_value_ptr},
Bram Moolenaar41a41412020-01-07 21:32:19 +0100744# if RUBY_VERSION <= 19
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100745 {"rb_float_new", (RUBY_PROC*)&dll_rb_float_new},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200746# else
Bram Moolenaar886ed692013-02-26 13:41:35 +0100747 {"rb_float_new_in_heap", (RUBY_PROC*)&dll_rb_float_new},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200748# endif
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100749 {"rb_ary_new", (RUBY_PROC*)&dll_rb_ary_new},
Bram Moolenaar51e9fbf2018-08-11 14:24:11 +0200750# ifdef RB_ARY_NEW4_MACRO
751 {"rb_ary_new_from_values", (RUBY_PROC*)&dll_rb_ary_new4},
752# else
753 {"rb_ary_new4", (RUBY_PROC*)&dll_rb_ary_new4},
754# endif
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100755 {"rb_ary_push", (RUBY_PROC*)&dll_rb_ary_push},
Bram Moolenaar41a41412020-01-07 21:32:19 +0100756# if RUBY_VERSION >= 26
Bram Moolenaarb09c6842018-12-27 22:11:01 +0100757 {"rb_ary_detransient", (RUBY_PROC*)&dll_rb_ary_detransient},
758# endif
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200759# endif
Bram Moolenaar41a41412020-01-07 21:32:19 +0100760# if RUBY_VERSION >= 19
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100761 {"rb_int2big", (RUBY_PROC*)&dll_rb_int2big},
Bram Moolenaar165641d2010-02-17 16:23:09 +0100762 {"ruby_script", (RUBY_PROC*)&dll_ruby_script},
763 {"rb_enc_find_index", (RUBY_PROC*)&dll_rb_enc_find_index},
764 {"rb_enc_find", (RUBY_PROC*)&dll_rb_enc_find},
765 {"rb_enc_str_new", (RUBY_PROC*)&dll_rb_enc_str_new},
766 {"rb_sprintf", (RUBY_PROC*)&dll_rb_sprintf},
Bram Moolenaar7a8ef142010-12-24 13:39:35 +0100767 {"rb_require", (RUBY_PROC*)&dll_rb_require},
Bram Moolenaar9b1067e2015-11-19 19:33:15 +0100768 {"ruby_options", (RUBY_PROC*)&dll_ruby_options},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200769# endif
Bram Moolenaar41a41412020-01-07 21:32:19 +0100770# if (RUBY_VERSION >= 19) || defined(RUBY_INIT_STACK)
Bram Moolenaar10f3a792013-05-20 12:52:29 +0200771# ifdef __ia64
772 {"rb_ia64_bsp", (RUBY_PROC*)&dll_rb_ia64_bsp},
773# endif
774 {"ruby_init_stack", (RUBY_PROC*)&dll_ruby_init_stack},
775# endif
Bram Moolenaara1a118b2014-02-05 22:41:15 +0100776# if defined(USE_RGENGC) && USE_RGENGC
Bram Moolenaar41a41412020-01-07 21:32:19 +0100777# if RUBY_VERSION == 21
Bram Moolenaara1a118b2014-02-05 22:41:15 +0100778 {"rb_gc_writebarrier_unprotect_promoted", (RUBY_PROC*)&dll_rb_gc_writebarrier_unprotect_promoted},
Bram Moolenaar0c7485f2015-01-14 14:04:10 +0100779# else
780 {"rb_gc_writebarrier_unprotect", (RUBY_PROC*)&dll_rb_gc_writebarrier_unprotect},
781# endif
Bram Moolenaara1a118b2014-02-05 22:41:15 +0100782# endif
Bram Moolenaardace9f72020-12-28 15:07:45 +0100783# if RUBY_VERSION >= 30
784 {"ruby_malloc_size_overflow", (RUBY_PROC*)&dll_ruby_malloc_size_overflow},
785# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000786 {"", NULL},
787};
788
789/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000790 * Load library and get all pointers.
791 * Parameter 'libname' provides name of DLL.
792 * Return OK or FAIL.
793 */
794 static int
795ruby_runtime_link_init(char *libname, int verbose)
796{
797 int i;
798
799 if (hinstRuby)
800 return OK;
Bram Moolenaarf9b5ef82010-09-29 13:02:53 +0200801 hinstRuby = load_dll(libname);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000802 if (!hinstRuby)
803 {
804 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100805 semsg(_(e_loadlib), libname);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000806 return FAIL;
807 }
808
809 for (i = 0; ruby_funcname_table[i].ptr; ++i)
810 {
Bram Moolenaarf9b5ef82010-09-29 13:02:53 +0200811 if (!(*ruby_funcname_table[i].ptr = symbol_from_dll(hinstRuby,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000812 ruby_funcname_table[i].name)))
813 {
Bram Moolenaarf9b5ef82010-09-29 13:02:53 +0200814 close_dll(hinstRuby);
Bram Moolenaar3ca71f12010-10-27 16:49:47 +0200815 hinstRuby = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000816 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100817 semsg(_(e_loadfunc), ruby_funcname_table[i].name);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000818 return FAIL;
819 }
820 }
821 return OK;
822}
823
824/*
825 * If ruby is enabled (there is installed ruby on Windows system) return TRUE,
826 * else FALSE.
827 */
828 int
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100829ruby_enabled(int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000830{
Bram Moolenaar25e4fcd2016-01-09 14:57:47 +0100831 return ruby_runtime_link_init((char *)p_rubydll, verbose) == OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000832}
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100833#endif // defined(DYNAMIC_RUBY) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000834
835 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100836ruby_end(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000837{
Bram Moolenaar071d4272004-06-13 20:20:40 +0000838}
839
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100840 void
841ex_ruby(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000842{
843 int state;
844 char *script = NULL;
845
Bram Moolenaar35a2e192006-03-13 22:07:11 +0000846 script = (char *)script_get(eap, eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000847 if (!eap->skip && ensure_ruby_initialized())
848 {
849 if (script == NULL)
850 rb_eval_string_protect((char *)eap->arg, &state);
851 else
852 rb_eval_string_protect(script, &state);
853 if (state)
854 error_print(state);
855 }
856 vim_free(script);
857}
858
Bram Moolenaar165641d2010-02-17 16:23:09 +0100859/*
860 * In Ruby 1.9 or later, ruby String object has encoding.
861 * conversion buffer string of vim to ruby String object using
862 * VIM encoding option.
863 */
864 static VALUE
865vim_str2rb_enc_str(const char *s)
866{
Bram Moolenaar41a41412020-01-07 21:32:19 +0100867#if RUBY_VERSION >= 19
Bram Moolenaar165641d2010-02-17 16:23:09 +0100868 int isnum;
869 long lval;
870 char_u *sval;
871 rb_encoding *enc;
872
873 isnum = get_option_value((char_u *)"enc", &lval, &sval, 0);
874 if (isnum == 0)
875 {
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 int isnum;
890 long lval;
891 char_u *sval;
892 rb_encoding *enc;
893 VALUE v;
894
895 isnum = get_option_value((char_u *)"enc", &lval, &sval, 0);
896 if (isnum == 0)
897 {
898 enc = rb_enc_find((char *)sval);
899 vim_free(sval);
900 if (enc)
901 {
902 v = rb_sprintf("#-*- coding:%s -*-\n%s", rb_enc_name(enc), str);
903 return rb_eval_string_protect(StringValuePtr(v), state);
904 }
905 }
906#endif
907 return rb_eval_string_protect(str, state);
908}
909
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100910 void
911ex_rubydo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000912{
913 int state;
914 linenr_T i;
Bram Moolenaarc593fee2017-01-29 23:11:25 +0100915 buf_T *was_curbuf = curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000916
917 if (ensure_ruby_initialized())
918 {
919 if (u_save(eap->line1 - 1, eap->line2 + 1) != OK)
920 return;
Bram Moolenaar758535a2016-03-30 22:06:16 +0200921 for (i = eap->line1; i <= eap->line2; i++)
922 {
Bram Moolenaare980d8a2010-12-08 13:11:21 +0100923 VALUE line;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000924
Bram Moolenaarc593fee2017-01-29 23:11:25 +0100925 if (i > curbuf->b_ml.ml_line_count)
926 break;
Bram Moolenaare980d8a2010-12-08 13:11:21 +0100927 line = vim_str2rb_enc_str((char *)ml_get(i));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000928 rb_lastline_set(line);
Bram Moolenaar165641d2010-02-17 16:23:09 +0100929 eval_enc_string_protect((char *) eap->arg, &state);
Bram Moolenaar758535a2016-03-30 22:06:16 +0200930 if (state)
931 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000932 error_print(state);
933 break;
934 }
Bram Moolenaarc593fee2017-01-29 23:11:25 +0100935 if (was_curbuf != curbuf)
936 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000937 line = rb_lastline_get();
Bram Moolenaar758535a2016-03-30 22:06:16 +0200938 if (!NIL_P(line))
939 {
940 if (TYPE(line) != T_STRING)
941 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100942 emsg(_("E265: $_ must be an instance of String"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000943 return;
944 }
Bram Moolenaar165641d2010-02-17 16:23:09 +0100945 ml_replace(i, (char_u *) StringValuePtr(line), 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000946 changed();
947#ifdef SYNTAX_HL
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100948 syn_changed(i); // recompute syntax hl. for this line
Bram Moolenaar071d4272004-06-13 20:20:40 +0000949#endif
950 }
951 }
952 check_cursor();
953 update_curbuf(NOT_VALID);
954 }
955}
956
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100957 static VALUE
958rb_load_wrap(VALUE file_to_load)
Bram Moolenaar37badc82018-01-31 20:15:30 +0100959{
960 rb_load(file_to_load, 0);
961 return Qnil;
962}
963
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100964 void
965ex_rubyfile(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000966{
967 int state;
968
969 if (ensure_ruby_initialized())
970 {
Bram Moolenaar37badc82018-01-31 20:15:30 +0100971 VALUE file_to_load = rb_str_new2((const char *)eap->arg);
972 rb_protect(rb_load_wrap, file_to_load, &state);
973 if (state)
974 error_print(state);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000975 }
976}
977
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100978 void
979ruby_buffer_free(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000980{
Bram Moolenaare344bea2005-09-01 20:46:49 +0000981 if (buf->b_ruby_ref)
982 {
983 rb_hash_aset(objtbl, rb_obj_id((VALUE) buf->b_ruby_ref), Qnil);
984 RDATA(buf->b_ruby_ref)->data = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000985 }
986}
987
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100988 void
989ruby_window_free(win_T *win)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000990{
Bram Moolenaare344bea2005-09-01 20:46:49 +0000991 if (win->w_ruby_ref)
992 {
993 rb_hash_aset(objtbl, rb_obj_id((VALUE) win->w_ruby_ref), Qnil);
994 RDATA(win->w_ruby_ref)->data = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000995 }
996}
997
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100998 static int
999ensure_ruby_initialized(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001000{
1001 if (!ruby_initialized)
1002 {
1003#ifdef DYNAMIC_RUBY
1004 if (ruby_enabled(TRUE))
1005 {
1006#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01001007#ifdef MSWIN
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001008 // suggested by Ariya Mizutani
Bram Moolenaar0b69c732010-02-17 15:11:50 +01001009 int argc = 1;
1010 char *argv[] = {"gvim.exe"};
Bram Moolenaar90140742014-11-27 17:44:08 +01001011 char **argvp = argv;
Bram Moolenaar41a41412020-01-07 21:32:19 +01001012# if RUBY_VERSION >= 19
Bram Moolenaarfe6ce332017-01-14 20:12:01 +01001013 ruby_sysinit(&argc, &argvp);
1014# else
Bram Moolenaar90140742014-11-27 17:44:08 +01001015 NtInitialize(&argc, &argvp);
Bram Moolenaarfe6ce332017-01-14 20:12:01 +01001016# endif
Bram Moolenaar0b69c732010-02-17 15:11:50 +01001017#endif
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001018 {
Bram Moolenaar41a41412020-01-07 21:32:19 +01001019#if (RUBY_VERSION >= 19) || defined(RUBY_INIT_STACK)
Bram Moolenaar99685e62013-05-11 13:56:18 +02001020 ruby_init_stack(ruby_stack_start);
Bram Moolenaar165641d2010-02-17 16:23:09 +01001021#endif
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001022 ruby_init();
1023 }
Bram Moolenaar41a41412020-01-07 21:32:19 +01001024#if RUBY_VERSION >= 19
Bram Moolenaar7a8ef142010-12-24 13:39:35 +01001025 {
1026 int dummy_argc = 2;
Bram Moolenaard1bc96ce2017-09-26 21:21:44 +02001027 char *dummy_argv[] = {"vim-ruby", "-e_=0"};
Bram Moolenaar9b1067e2015-11-19 19:33:15 +01001028 ruby_options(dummy_argc, dummy_argv);
Bram Moolenaar7a8ef142010-12-24 13:39:35 +01001029 }
Bram Moolenaar165641d2010-02-17 16:23:09 +01001030 ruby_script("vim-ruby");
Bram Moolenaar7a8ef142010-12-24 13:39:35 +01001031#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001032 ruby_init_loadpath();
Bram Moolenaar165641d2010-02-17 16:23:09 +01001033#endif
Bram Moolenaar7a8ef142010-12-24 13:39:35 +01001034 ruby_io_init();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001035 ruby_vim_init();
1036 ruby_initialized = 1;
1037#ifdef DYNAMIC_RUBY
1038 }
1039 else
1040 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001041 emsg(_("E266: Sorry, this command is disabled, the Ruby library could not be loaded."));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001042 return 0;
1043 }
1044#endif
1045 }
1046 return ruby_initialized;
1047}
1048
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001049 static void
1050error_print(int state)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001051{
Bram Moolenaar41a41412020-01-07 21:32:19 +01001052#if !defined(DYNAMIC_RUBY) && (RUBY_VERSION <= 18)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001053 RUBYEXTERN VALUE ruby_errinfo;
1054#endif
Bram Moolenaarf711cb22018-08-01 18:42:13 +02001055 VALUE error;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001056 VALUE eclass;
1057 VALUE einfo;
Bram Moolenaarf711cb22018-08-01 18:42:13 +02001058 VALUE bt;
1059 int attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001060 char buff[BUFSIZ];
Bram Moolenaarf711cb22018-08-01 18:42:13 +02001061 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001062
1063#define TAG_RETURN 0x1
1064#define TAG_BREAK 0x2
1065#define TAG_NEXT 0x3
1066#define TAG_RETRY 0x4
1067#define TAG_REDO 0x5
1068#define TAG_RAISE 0x6
1069#define TAG_THROW 0x7
1070#define TAG_FATAL 0x8
1071#define TAG_MASK 0xf
1072
Bram Moolenaar758535a2016-03-30 22:06:16 +02001073 switch (state)
1074 {
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001075 case TAG_RETURN:
1076 emsg(_("E267: unexpected return"));
1077 break;
1078 case TAG_NEXT:
1079 emsg(_("E268: unexpected next"));
1080 break;
1081 case TAG_BREAK:
1082 emsg(_("E269: unexpected break"));
1083 break;
1084 case TAG_REDO:
1085 emsg(_("E270: unexpected redo"));
1086 break;
1087 case TAG_RETRY:
1088 emsg(_("E271: retry outside of rescue clause"));
1089 break;
1090 case TAG_RAISE:
1091 case TAG_FATAL:
Bram Moolenaar41a41412020-01-07 21:32:19 +01001092#if RUBY_VERSION >= 19
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001093 error = rb_errinfo();
Bram Moolenaar165641d2010-02-17 16:23:09 +01001094#else
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001095 error = ruby_errinfo;
Bram Moolenaar165641d2010-02-17 16:23:09 +01001096#endif
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001097 eclass = CLASS_OF(error);
1098 einfo = rb_obj_as_string(error);
1099 if (eclass == rb_eRuntimeError && RSTRING_LEN(einfo) == 0)
1100 {
1101 emsg(_("E272: unhandled exception"));
1102 }
1103 else
1104 {
1105 VALUE epath;
1106 char *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001107
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001108 epath = rb_class_path(eclass);
1109 vim_snprintf(buff, BUFSIZ, "%s: %s",
1110 RSTRING_PTR(epath), RSTRING_PTR(einfo));
1111 p = strchr(buff, '\n');
1112 if (p) *p = '\0';
1113 emsg(buff);
1114 }
Bram Moolenaarf711cb22018-08-01 18:42:13 +02001115
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001116 attr = syn_name2attr((char_u *)"Error");
Bram Moolenaar49c99fc2020-02-11 23:01:39 +01001117#if RUBY_VERSION >= 21
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001118 bt = rb_funcallv(error, rb_intern("backtrace"), 0, 0);
1119 for (i = 0; i < RARRAY_LEN(bt); i++)
1120 msg_attr(RSTRING_PTR(RARRAY_AREF(bt, i)), attr);
Bram Moolenaar49c99fc2020-02-11 23:01:39 +01001121#else
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001122 bt = rb_funcall2(error, rb_intern("backtrace"), 0, 0);
1123 for (i = 0; i < RARRAY_LEN(bt); i++)
1124 msg_attr(RSTRING_PTR(RARRAY_PTR(bt)[i]), attr);
Bram Moolenaar49c99fc2020-02-11 23:01:39 +01001125#endif
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001126 break;
1127 default:
1128 vim_snprintf(buff, BUFSIZ, _("E273: unknown longjmp status %d"), state);
1129 emsg(buff);
1130 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001131 }
1132}
1133
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001134 static VALUE
1135vim_message(VALUE self UNUSED, VALUE str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001136{
1137 char *buff, *p;
1138
1139 str = rb_obj_as_string(str);
Bram Moolenaar3f5f7952011-08-04 19:34:59 +02001140 if (RSTRING_LEN(str) > 0)
1141 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001142 // Only do this when the string isn't empty, alloc(0) causes trouble.
Bram Moolenaar00ccf542017-09-03 15:17:48 +02001143 buff = ALLOCA_N(char, RSTRING_LEN(str) + 1);
Bram Moolenaar3f5f7952011-08-04 19:34:59 +02001144 strcpy(buff, RSTRING_PTR(str));
1145 p = strchr(buff, '\n');
1146 if (p) *p = '\0';
Bram Moolenaar32526b32019-01-19 17:43:09 +01001147 msg(buff);
Bram Moolenaar3f5f7952011-08-04 19:34:59 +02001148 }
1149 else
1150 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01001151 msg("");
Bram Moolenaar3f5f7952011-08-04 19:34:59 +02001152 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001153 return Qnil;
1154}
1155
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001156 static VALUE
1157vim_set_option(VALUE self UNUSED, VALUE str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001158{
Bram Moolenaar165641d2010-02-17 16:23:09 +01001159 do_set((char_u *)StringValuePtr(str), 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001160 update_screen(NOT_VALID);
1161 return Qnil;
1162}
1163
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001164 static VALUE
1165vim_command(VALUE self UNUSED, VALUE str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001166{
Bram Moolenaar165641d2010-02-17 16:23:09 +01001167 do_cmdline_cmd((char_u *)StringValuePtr(str));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001168 return Qnil;
1169}
1170
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001171#ifdef FEAT_EVAL
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001172 static VALUE
1173vim_to_ruby(typval_T *tv)
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001174{
1175 VALUE result = Qnil;
1176
1177 if (tv->v_type == VAR_STRING)
1178 {
Bram Moolenaar94127e42010-03-19 23:08:48 +01001179 result = rb_str_new2(tv->vval.v_string == NULL
1180 ? "" : (char *)(tv->vval.v_string));
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001181 }
1182 else if (tv->v_type == VAR_NUMBER)
1183 {
Bram Moolenaar639a2552010-03-17 18:15:23 +01001184 result = INT2NUM(tv->vval.v_number);
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001185 }
1186# ifdef FEAT_FLOAT
1187 else if (tv->v_type == VAR_FLOAT)
1188 {
Bram Moolenaar639a2552010-03-17 18:15:23 +01001189 result = rb_float_new(tv->vval.v_float);
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001190 }
1191# endif
1192 else if (tv->v_type == VAR_LIST)
1193 {
Bram Moolenaar639a2552010-03-17 18:15:23 +01001194 list_T *list = tv->vval.v_list;
1195 listitem_T *curr;
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001196
Bram Moolenaar639a2552010-03-17 18:15:23 +01001197 result = rb_ary_new();
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001198
Bram Moolenaar639a2552010-03-17 18:15:23 +01001199 if (list != NULL)
1200 {
Bram Moolenaaraeea7212020-04-02 18:50:46 +02001201 FOR_ALL_LIST_ITEMS(list, curr)
Bram Moolenaar639a2552010-03-17 18:15:23 +01001202 rb_ary_push(result, vim_to_ruby(&curr->li_tv));
Bram Moolenaar639a2552010-03-17 18:15:23 +01001203 }
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001204 }
1205 else if (tv->v_type == VAR_DICT)
1206 {
Bram Moolenaar639a2552010-03-17 18:15:23 +01001207 result = rb_hash_new();
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001208
Bram Moolenaar639a2552010-03-17 18:15:23 +01001209 if (tv->vval.v_dict != NULL)
1210 {
1211 hashtab_T *ht = &tv->vval.v_dict->dv_hashtab;
1212 long_u todo = ht->ht_used;
1213 hashitem_T *hi;
1214 dictitem_T *di;
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001215
Bram Moolenaar639a2552010-03-17 18:15:23 +01001216 for (hi = ht->ht_array; todo > 0; ++hi)
1217 {
1218 if (!HASHITEM_EMPTY(hi))
1219 {
1220 --todo;
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001221
Bram Moolenaar639a2552010-03-17 18:15:23 +01001222 di = dict_lookup(hi);
1223 rb_hash_aset(result, rb_str_new2((char *)hi->hi_key),
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001224 vim_to_ruby(&di->di_tv));
Bram Moolenaar639a2552010-03-17 18:15:23 +01001225 }
1226 }
1227 }
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001228 }
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01001229 else if (tv->v_type == VAR_BOOL || tv->v_type == VAR_SPECIAL)
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001230 {
Bram Moolenaard84b26a2018-07-28 17:18:09 +02001231 if (tv->vval.v_number == VVAL_TRUE)
1232 result = Qtrue;
1233 else if (tv->vval.v_number == VVAL_FALSE)
1234 result = Qfalse;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001235 }
1236 else if (tv->v_type == VAR_BLOB)
1237 {
1238 result = rb_str_new(tv->vval.v_blob->bv_ga.ga_data,
1239 tv->vval.v_blob->bv_ga.ga_len);
1240 }
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001241 // else return Qnil;
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001242
1243 return result;
1244}
1245#endif
1246
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001247 static VALUE
1248vim_evaluate(VALUE self UNUSED, VALUE str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001249{
1250#ifdef FEAT_EVAL
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001251 typval_T *tv;
1252 VALUE result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001253
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001254 tv = eval_expr((char_u *)StringValuePtr(str), NULL);
1255 if (tv == NULL)
Bram Moolenaar639a2552010-03-17 18:15:23 +01001256 return Qnil;
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001257 result = vim_to_ruby(tv);
1258
1259 free_tv(tv);
1260
1261 return result;
1262#else
1263 return Qnil;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001264#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001265}
1266
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001267#ifdef USE_TYPEDDATA
1268static size_t buffer_dsize(const void *buf);
1269
1270static const rb_data_type_t buffer_type = {
1271 "vim_buffer",
Bram Moolenaar41a41412020-01-07 21:32:19 +01001272 {0, 0, buffer_dsize,
1273# if RUBY_VERSION >= 27
Bram Moolenaar8b430b42020-02-22 15:01:00 +01001274 0, {0}
Bram Moolenaar41a41412020-01-07 21:32:19 +01001275# else
1276 {0, 0}
1277# endif
1278 },
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001279 0, 0,
1280# ifdef RUBY_TYPED_FREE_IMMEDIATELY
1281 0,
1282# endif
1283};
1284
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001285 static size_t
1286buffer_dsize(const void *buf UNUSED)
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001287{
1288 return sizeof(buf_T);
1289}
1290#endif
1291
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001292 static VALUE
1293buffer_new(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001294{
Bram Moolenaare344bea2005-09-01 20:46:49 +00001295 if (buf->b_ruby_ref)
1296 {
1297 return (VALUE) buf->b_ruby_ref;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001298 }
Bram Moolenaare344bea2005-09-01 20:46:49 +00001299 else
1300 {
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001301#ifdef USE_TYPEDDATA
1302 VALUE obj = TypedData_Wrap_Struct(cBuffer, &buffer_type, buf);
1303#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001304 VALUE obj = Data_Wrap_Struct(cBuffer, 0, 0, buf);
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001305#endif
Bram Moolenaare344bea2005-09-01 20:46:49 +00001306 buf->b_ruby_ref = (void *) obj;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001307 rb_hash_aset(objtbl, rb_obj_id(obj), obj);
1308 return obj;
1309 }
1310}
1311
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001312 static buf_T *
1313get_buf(VALUE obj)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001314{
1315 buf_T *buf;
1316
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001317#ifdef USE_TYPEDDATA
1318 TypedData_Get_Struct(obj, buf_T, &buffer_type, buf);
1319#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001320 Data_Get_Struct(obj, buf_T, buf);
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001321#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001322 if (buf == NULL)
1323 rb_raise(eDeletedBufferError, "attempt to refer to deleted buffer");
1324 return buf;
1325}
1326
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001327 static VALUE
1328vim_blob(VALUE self UNUSED, VALUE str)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001329{
1330 VALUE result = rb_str_new("0z", 2);
1331 char buf[4];
1332 int i;
1333 for (i = 0; i < RSTRING_LEN(str); i++)
1334 {
Bram Moolenaar0d13cce2019-02-23 14:23:03 +01001335 sprintf(buf, "%02X", (unsigned char)(RSTRING_PTR(str)[i]));
Bram Moolenaarb191be22019-01-30 21:00:12 +01001336 rb_str_concat(result, rb_str_new2(buf));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001337 }
1338 return result;
1339}
1340
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001341 static VALUE
Bram Moolenaard5a986f2020-12-06 21:11:31 +01001342buffer_s_current(VALUE self UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001343{
1344 return buffer_new(curbuf);
1345}
1346
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001347 static VALUE
Bram Moolenaard5a986f2020-12-06 21:11:31 +01001348buffer_s_current_getter(ID id UNUSED, VALUE *x UNUSED)
1349{
1350 return buffer_new(curbuf);
1351}
1352
1353 static VALUE
1354buffer_s_count(VALUE self UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001355{
1356 buf_T *b;
1357 int n = 0;
1358
Bram Moolenaar29323592016-07-24 22:04:11 +02001359 FOR_ALL_BUFFERS(b)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001360 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001361 // Deleted buffers should not be counted
1362 // SegPhault - 01/07/05
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001363 if (b->b_p_bl)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001364 n++;
1365 }
1366
Bram Moolenaar071d4272004-06-13 20:20:40 +00001367 return INT2NUM(n);
1368}
1369
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001370 static VALUE
1371buffer_s_aref(VALUE self UNUSED, VALUE num)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001372{
1373 buf_T *b;
1374 int n = NUM2INT(num);
1375
Bram Moolenaar29323592016-07-24 22:04:11 +02001376 FOR_ALL_BUFFERS(b)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001377 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001378 // Deleted buffers should not be counted
1379 // SegPhault - 01/07/05
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001380 if (!b->b_p_bl)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001381 continue;
1382
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001383 if (n == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001384 return buffer_new(b);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001385
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001386 n--;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001387 }
1388 return Qnil;
1389}
1390
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001391 static VALUE
1392buffer_name(VALUE self)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001393{
1394 buf_T *buf = get_buf(self);
1395
Bram Moolenaar35a2e192006-03-13 22:07:11 +00001396 return buf->b_ffname ? rb_str_new2((char *)buf->b_ffname) : Qnil;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001397}
1398
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001399 static VALUE
1400buffer_number(VALUE self)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001401{
1402 buf_T *buf = get_buf(self);
1403
1404 return INT2NUM(buf->b_fnum);
1405}
1406
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001407 static VALUE
1408buffer_count(VALUE self)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001409{
1410 buf_T *buf = get_buf(self);
1411
1412 return INT2NUM(buf->b_ml.ml_line_count);
1413}
1414
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001415 static VALUE
1416get_buffer_line(buf_T *buf, linenr_T n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001417{
Bram Moolenaar3c531602010-11-16 14:46:19 +01001418 if (n <= 0 || n > buf->b_ml.ml_line_count)
1419 rb_raise(rb_eIndexError, "line number %ld out of range", (long)n);
1420 return vim_str2rb_enc_str((char *)ml_get_buf(buf, n, FALSE));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001421}
1422
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001423 static VALUE
1424buffer_aref(VALUE self, VALUE num)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001425{
1426 buf_T *buf = get_buf(self);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001427
1428 if (buf != NULL)
1429 return get_buffer_line(buf, (linenr_T)NUM2LONG(num));
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001430 return Qnil; // For stop warning
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001431}
1432
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001433 static VALUE
1434set_buffer_line(buf_T *buf, linenr_T n, VALUE str)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001435{
Bram Moolenaar165641d2010-02-17 16:23:09 +01001436 char *line = StringValuePtr(str);
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001437 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001438
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001439 if (n > 0 && n <= buf->b_ml.ml_line_count && line != NULL)
1440 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001441 // set curwin/curbuf for "buf" and save some things
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001442 aucmd_prepbuf(&aco, buf);
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001443
Bram Moolenaar758535a2016-03-30 22:06:16 +02001444 if (u_savesub(n) == OK)
1445 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001446 ml_replace(n, (char_u *)line, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001447 changed();
1448#ifdef SYNTAX_HL
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001449 syn_changed(n); // recompute syntax hl. for this line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001450#endif
1451 }
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001452
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001453 // restore curwin/curbuf and a few other things
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001454 aucmd_restbuf(&aco);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001455 // Careful: autocommands may have made "buf" invalid!
Bram Moolenaarf30e74c2006-08-16 17:35:00 +00001456
Bram Moolenaar071d4272004-06-13 20:20:40 +00001457 update_curbuf(NOT_VALID);
1458 }
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001459 else
1460 {
Bram Moolenaar165641d2010-02-17 16:23:09 +01001461 rb_raise(rb_eIndexError, "line number %ld out of range", (long)n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001462 }
1463 return str;
1464}
1465
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001466 static VALUE
1467buffer_aset(VALUE self, VALUE num, VALUE str)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001468{
1469 buf_T *buf = get_buf(self);
1470
1471 if (buf != NULL)
1472 return set_buffer_line(buf, (linenr_T)NUM2LONG(num), str);
1473 return str;
1474}
1475
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001476 static VALUE
1477buffer_delete(VALUE self, VALUE num)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001478{
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001479 buf_T *buf = get_buf(self);
1480 long n = NUM2LONG(num);
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001481 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001482
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001483 if (n > 0 && n <= buf->b_ml.ml_line_count)
1484 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001485 // set curwin/curbuf for "buf" and save some things
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001486 aucmd_prepbuf(&aco, buf);
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001487
Bram Moolenaar758535a2016-03-30 22:06:16 +02001488 if (u_savedel(n, 1) == OK)
1489 {
Bram Moolenaarca70c072020-05-30 20:30:46 +02001490 ml_delete(n);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001491
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001492 // Changes to non-active buffers should properly refresh
1493 // SegPhault - 01/09/05
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001494 deleted_lines_mark(n, 1L);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001495
Bram Moolenaar071d4272004-06-13 20:20:40 +00001496 changed();
1497 }
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001498
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001499 // restore curwin/curbuf and a few other things
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001500 aucmd_restbuf(&aco);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001501 // Careful: autocommands may have made "buf" invalid!
Bram Moolenaarf30e74c2006-08-16 17:35:00 +00001502
Bram Moolenaar071d4272004-06-13 20:20:40 +00001503 update_curbuf(NOT_VALID);
1504 }
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001505 else
1506 {
Bram Moolenaar165641d2010-02-17 16:23:09 +01001507 rb_raise(rb_eIndexError, "line number %ld out of range", n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001508 }
1509 return Qnil;
1510}
1511
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001512 static VALUE
1513buffer_append(VALUE self, VALUE num, VALUE str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001514{
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001515 buf_T *buf = get_buf(self);
Bram Moolenaar165641d2010-02-17 16:23:09 +01001516 char *line = StringValuePtr(str);
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001517 long n = NUM2LONG(num);
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001518 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001519
Bram Moolenaar3c531602010-11-16 14:46:19 +01001520 if (line == NULL)
1521 {
Bram Moolenaar165641d2010-02-17 16:23:09 +01001522 rb_raise(rb_eIndexError, "NULL line");
1523 }
1524 else if (n >= 0 && n <= buf->b_ml.ml_line_count)
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001525 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001526 // set curwin/curbuf for "buf" and save some things
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001527 aucmd_prepbuf(&aco, buf);
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001528
Bram Moolenaar758535a2016-03-30 22:06:16 +02001529 if (u_inssub(n + 1) == OK)
1530 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001531 ml_append(n, (char_u *) line, (colnr_T) 0, FALSE);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001532
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001533 // Changes to non-active buffers should properly refresh screen
1534 // SegPhault - 12/20/04
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001535 appended_lines_mark(n, 1L);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001536
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001537 changed();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001538 }
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001539
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001540 // restore curwin/curbuf and a few other things
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001541 aucmd_restbuf(&aco);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001542 // Careful: autocommands may have made "buf" invalid!
Bram Moolenaarf30e74c2006-08-16 17:35:00 +00001543
Bram Moolenaar071d4272004-06-13 20:20:40 +00001544 update_curbuf(NOT_VALID);
1545 }
Bram Moolenaar3c531602010-11-16 14:46:19 +01001546 else
1547 {
Bram Moolenaar165641d2010-02-17 16:23:09 +01001548 rb_raise(rb_eIndexError, "line number %ld out of range", n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001549 }
1550 return str;
1551}
1552
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001553#ifdef USE_TYPEDDATA
1554static size_t window_dsize(const void *buf);
1555
1556static const rb_data_type_t window_type = {
1557 "vim_window",
Bram Moolenaar41a41412020-01-07 21:32:19 +01001558 {0, 0, window_dsize,
1559# if RUBY_VERSION >= 27
Bram Moolenaar8b430b42020-02-22 15:01:00 +01001560 0, {0}
Bram Moolenaar41a41412020-01-07 21:32:19 +01001561# else
1562 {0, 0}
1563# endif
1564 },
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001565 0, 0,
1566# ifdef RUBY_TYPED_FREE_IMMEDIATELY
1567 0,
1568# endif
1569};
1570
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001571 static size_t
1572window_dsize(const void *win UNUSED)
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001573{
1574 return sizeof(win_T);
1575}
1576#endif
1577
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001578 static VALUE
1579window_new(win_T *win)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001580{
Bram Moolenaare344bea2005-09-01 20:46:49 +00001581 if (win->w_ruby_ref)
1582 {
1583 return (VALUE) win->w_ruby_ref;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001584 }
Bram Moolenaare344bea2005-09-01 20:46:49 +00001585 else
1586 {
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001587#ifdef USE_TYPEDDATA
1588 VALUE obj = TypedData_Wrap_Struct(cVimWindow, &window_type, win);
1589#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001590 VALUE obj = Data_Wrap_Struct(cVimWindow, 0, 0, win);
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001591#endif
Bram Moolenaare344bea2005-09-01 20:46:49 +00001592 win->w_ruby_ref = (void *) obj;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001593 rb_hash_aset(objtbl, rb_obj_id(obj), obj);
1594 return obj;
1595 }
1596}
1597
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001598 static win_T *
1599get_win(VALUE obj)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001600{
1601 win_T *win;
1602
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001603#ifdef USE_TYPEDDATA
1604 TypedData_Get_Struct(obj, win_T, &window_type, win);
1605#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001606 Data_Get_Struct(obj, win_T, win);
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001607#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001608 if (win == NULL)
1609 rb_raise(eDeletedWindowError, "attempt to refer to deleted window");
1610 return win;
1611}
1612
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001613 static VALUE
Bram Moolenaard5a986f2020-12-06 21:11:31 +01001614window_s_current(VALUE self UNUSED)
1615{
1616 return window_new(curwin);
1617}
1618
1619 static VALUE
1620window_s_current_getter(ID id UNUSED, VALUE *x UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001621{
1622 return window_new(curwin);
1623}
1624
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001625/*
1626 * Added line manipulation functions
1627 * SegPhault - 03/07/05
1628 */
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001629 static VALUE
Bram Moolenaard5a986f2020-12-06 21:11:31 +01001630line_s_current(VALUE self UNUSED)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001631{
1632 return get_buffer_line(curbuf, curwin->w_cursor.lnum);
1633}
1634
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001635 static VALUE
1636set_current_line(VALUE self UNUSED, VALUE str)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001637{
1638 return set_buffer_line(curbuf, curwin->w_cursor.lnum, str);
1639}
1640
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001641 static VALUE
Bram Moolenaard5a986f2020-12-06 21:11:31 +01001642current_line_number(VALUE self UNUSED)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001643{
1644 return INT2FIX((int)curwin->w_cursor.lnum);
1645}
1646
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001647 static VALUE
Bram Moolenaard5a986f2020-12-06 21:11:31 +01001648window_s_count(VALUE self UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001649{
Bram Moolenaar071d4272004-06-13 20:20:40 +00001650 win_T *w;
1651 int n = 0;
1652
Bram Moolenaar29323592016-07-24 22:04:11 +02001653 FOR_ALL_WINDOWS(w)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001654 n++;
1655 return INT2NUM(n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001656}
1657
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001658 static VALUE
1659window_s_aref(VALUE self UNUSED, VALUE num)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001660{
1661 win_T *w;
1662 int n = NUM2INT(num);
1663
Bram Moolenaar071d4272004-06-13 20:20:40 +00001664 for (w = firstwin; w != NULL; w = w->w_next, --n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001665 if (n == 0)
1666 return window_new(w);
1667 return Qnil;
1668}
1669
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001670 static VALUE
1671window_buffer(VALUE self)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001672{
1673 win_T *win = get_win(self);
1674
1675 return buffer_new(win->w_buffer);
1676}
1677
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001678 static VALUE
1679window_height(VALUE self)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001680{
1681 win_T *win = get_win(self);
1682
1683 return INT2NUM(win->w_height);
1684}
1685
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001686 static VALUE
1687window_set_height(VALUE self, VALUE height)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001688{
1689 win_T *win = get_win(self);
1690 win_T *savewin = curwin;
1691
1692 curwin = win;
1693 win_setheight(NUM2INT(height));
1694 curwin = savewin;
1695 return height;
1696}
1697
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001698 static VALUE
1699window_width(VALUE self UNUSED)
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001700{
Bram Moolenaare745d752017-09-22 16:56:22 +02001701 return INT2NUM(get_win(self)->w_width);
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001702}
1703
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001704 static VALUE
1705window_set_width(VALUE self UNUSED, VALUE width)
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001706{
1707 win_T *win = get_win(self);
1708 win_T *savewin = curwin;
1709
1710 curwin = win;
1711 win_setwidth(NUM2INT(width));
1712 curwin = savewin;
1713 return width;
1714}
1715
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001716 static VALUE
1717window_cursor(VALUE self)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001718{
1719 win_T *win = get_win(self);
1720
1721 return rb_assoc_new(INT2NUM(win->w_cursor.lnum), INT2NUM(win->w_cursor.col));
1722}
1723
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001724 static VALUE
1725window_set_cursor(VALUE self, VALUE pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001726{
1727 VALUE lnum, col;
1728 win_T *win = get_win(self);
1729
1730 Check_Type(pos, T_ARRAY);
Bram Moolenaar165641d2010-02-17 16:23:09 +01001731 if (RARRAY_LEN(pos) != 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001732 rb_raise(rb_eArgError, "array length must be 2");
Bram Moolenaar165641d2010-02-17 16:23:09 +01001733 lnum = RARRAY_PTR(pos)[0];
1734 col = RARRAY_PTR(pos)[1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001735 win->w_cursor.lnum = NUM2LONG(lnum);
1736 win->w_cursor.col = NUM2UINT(col);
Bram Moolenaar53901442018-07-25 22:02:36 +02001737 win->w_set_curswant = TRUE;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001738 check_cursor(); // put cursor on an existing line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001739 update_screen(NOT_VALID);
1740 return Qnil;
1741}
1742
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001743 static VALUE
1744f_nop(VALUE self UNUSED)
Bram Moolenaar35df7d22012-04-20 18:05:47 +02001745{
1746 return Qnil;
1747}
1748
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001749 static VALUE
1750f_p(int argc, VALUE *argv, VALUE self UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001751{
1752 int i;
1753 VALUE str = rb_str_new("", 0);
Bram Moolenaar51e9fbf2018-08-11 14:24:11 +02001754 VALUE ret = Qnil;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001755
Bram Moolenaar758535a2016-03-30 22:06:16 +02001756 for (i = 0; i < argc; i++)
1757 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001758 if (i > 0) rb_str_cat(str, ", ", 2);
1759 rb_str_concat(str, rb_inspect(argv[i]));
1760 }
Bram Moolenaar32526b32019-01-19 17:43:09 +01001761 msg(RSTRING_PTR(str));
Bram Moolenaar51e9fbf2018-08-11 14:24:11 +02001762
1763 if (argc == 1)
1764 ret = argv[0];
1765 else if (argc > 1)
1766 ret = rb_ary_new4(argc, argv);
1767 return ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001768}
1769
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001770 static void
1771ruby_io_init(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001772{
1773#ifndef DYNAMIC_RUBY
1774 RUBYEXTERN VALUE rb_stdout;
Bram Moolenaarb6c8cd82018-07-24 05:41:30 +02001775 RUBYEXTERN VALUE rb_stderr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001776#endif
1777
1778 rb_stdout = rb_obj_alloc(rb_cObject);
Bram Moolenaarb6c8cd82018-07-24 05:41:30 +02001779 rb_stderr = rb_obj_alloc(rb_cObject);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001780 rb_define_singleton_method(rb_stdout, "write", vim_message, 1);
Bram Moolenaar35df7d22012-04-20 18:05:47 +02001781 rb_define_singleton_method(rb_stdout, "flush", f_nop, 0);
Bram Moolenaarb6c8cd82018-07-24 05:41:30 +02001782 rb_define_singleton_method(rb_stderr, "write", vim_message, 1);
1783 rb_define_singleton_method(rb_stderr, "flush", f_nop, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001784 rb_define_global_function("p", f_p, -1);
1785}
1786
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001787 static void
1788ruby_vim_init(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001789{
1790 objtbl = rb_hash_new();
1791 rb_global_variable(&objtbl);
1792
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001793 // The Vim module used to be called "VIM", but "Vim" is better. Make an
1794 // alias "VIM" for backwards compatibility.
Bram Moolenaarf711faf2007-05-10 16:48:19 +00001795 mVIM = rb_define_module("Vim");
1796 rb_define_const(rb_cObject, "VIM", mVIM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001797 rb_define_const(mVIM, "VERSION_MAJOR", INT2NUM(VIM_VERSION_MAJOR));
1798 rb_define_const(mVIM, "VERSION_MINOR", INT2NUM(VIM_VERSION_MINOR));
1799 rb_define_const(mVIM, "VERSION_BUILD", INT2NUM(VIM_VERSION_BUILD));
1800 rb_define_const(mVIM, "VERSION_PATCHLEVEL", INT2NUM(VIM_VERSION_PATCHLEVEL));
1801 rb_define_const(mVIM, "VERSION_SHORT", rb_str_new2(VIM_VERSION_SHORT));
1802 rb_define_const(mVIM, "VERSION_MEDIUM", rb_str_new2(VIM_VERSION_MEDIUM));
1803 rb_define_const(mVIM, "VERSION_LONG", rb_str_new2(VIM_VERSION_LONG));
1804 rb_define_const(mVIM, "VERSION_LONG_DATE", rb_str_new2(VIM_VERSION_LONG_DATE));
1805 rb_define_module_function(mVIM, "message", vim_message, 1);
1806 rb_define_module_function(mVIM, "set_option", vim_set_option, 1);
1807 rb_define_module_function(mVIM, "command", vim_command, 1);
1808 rb_define_module_function(mVIM, "evaluate", vim_evaluate, 1);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001809 rb_define_module_function(mVIM, "blob", vim_blob, 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001810
1811 eDeletedBufferError = rb_define_class_under(mVIM, "DeletedBufferError",
1812 rb_eStandardError);
1813 eDeletedWindowError = rb_define_class_under(mVIM, "DeletedWindowError",
1814 rb_eStandardError);
1815
1816 cBuffer = rb_define_class_under(mVIM, "Buffer", rb_cObject);
1817 rb_define_singleton_method(cBuffer, "current", buffer_s_current, 0);
1818 rb_define_singleton_method(cBuffer, "count", buffer_s_count, 0);
1819 rb_define_singleton_method(cBuffer, "[]", buffer_s_aref, 1);
1820 rb_define_method(cBuffer, "name", buffer_name, 0);
1821 rb_define_method(cBuffer, "number", buffer_number, 0);
1822 rb_define_method(cBuffer, "count", buffer_count, 0);
1823 rb_define_method(cBuffer, "length", buffer_count, 0);
1824 rb_define_method(cBuffer, "[]", buffer_aref, 1);
1825 rb_define_method(cBuffer, "[]=", buffer_aset, 2);
1826 rb_define_method(cBuffer, "delete", buffer_delete, 1);
1827 rb_define_method(cBuffer, "append", buffer_append, 2);
1828
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001829 // Added line manipulation functions
1830 // SegPhault - 03/07/05
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001831 rb_define_method(cBuffer, "line_number", current_line_number, 0);
1832 rb_define_method(cBuffer, "line", line_s_current, 0);
1833 rb_define_method(cBuffer, "line=", set_current_line, 1);
1834
1835
Bram Moolenaar071d4272004-06-13 20:20:40 +00001836 cVimWindow = rb_define_class_under(mVIM, "Window", rb_cObject);
1837 rb_define_singleton_method(cVimWindow, "current", window_s_current, 0);
1838 rb_define_singleton_method(cVimWindow, "count", window_s_count, 0);
1839 rb_define_singleton_method(cVimWindow, "[]", window_s_aref, 1);
1840 rb_define_method(cVimWindow, "buffer", window_buffer, 0);
1841 rb_define_method(cVimWindow, "height", window_height, 0);
1842 rb_define_method(cVimWindow, "height=", window_set_height, 1);
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001843 rb_define_method(cVimWindow, "width", window_width, 0);
1844 rb_define_method(cVimWindow, "width=", window_set_width, 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001845 rb_define_method(cVimWindow, "cursor", window_cursor, 0);
1846 rb_define_method(cVimWindow, "cursor=", window_set_cursor, 1);
1847
Bram Moolenaard5a986f2020-12-06 21:11:31 +01001848 rb_define_virtual_variable("$curbuf", buffer_s_current_getter, 0);
1849 rb_define_virtual_variable("$curwin", window_s_current_getter, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001850}
Bram Moolenaar99685e62013-05-11 13:56:18 +02001851
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001852 void
1853vim_ruby_init(void *stack_start)
Bram Moolenaar99685e62013-05-11 13:56:18 +02001854{
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001855 // should get machine stack start address early in main function
Bram Moolenaar99685e62013-05-11 13:56:18 +02001856 ruby_stack_start = stack_start;
1857}
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001858
1859 static int
1860convert_hash2dict(VALUE key, VALUE val, VALUE arg)
1861{
1862 dict_T *d = (dict_T *)arg;
1863 dictitem_T *di;
1864
Bram Moolenaardace9f72020-12-28 15:07:45 +01001865 di = dictitem_alloc((char_u *)RSTRING_PTR(rb_obj_as_string(key)));
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001866 if (di == NULL || ruby_convert_to_vim_value(val, &di->di_tv) != OK
1867 || dict_add(d, di) != OK)
1868 {
1869 d->dv_hashtab.ht_error = TRUE;
1870 return ST_STOP;
1871 }
1872 return ST_CONTINUE;
1873}
1874
1875 static int
1876ruby_convert_to_vim_value(VALUE val, typval_T *rettv)
1877{
1878 switch (TYPE(val))
1879 {
1880 case T_NIL:
1881 rettv->v_type = VAR_SPECIAL;
1882 rettv->vval.v_number = VVAL_NULL;
1883 break;
1884 case T_TRUE:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01001885 rettv->v_type = VAR_BOOL;
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001886 rettv->vval.v_number = VVAL_TRUE;
1887 break;
1888 case T_FALSE:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01001889 rettv->v_type = VAR_BOOL;
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001890 rettv->vval.v_number = VVAL_FALSE;
1891 break;
1892 case T_BIGNUM:
1893 case T_FIXNUM:
1894 rettv->v_type = VAR_NUMBER;
1895 rettv->vval.v_number = (varnumber_T)NUM2LONG(val);
1896 break;
1897#ifdef FEAT_FLOAT
1898 case T_FLOAT:
1899 rettv->v_type = VAR_FLOAT;
1900 rettv->vval.v_float = (float_T)NUM2DBL(val);
1901 break;
1902#endif
1903 default:
1904 val = rb_obj_as_string(val);
1905 // FALLTHROUGH
1906 case T_STRING:
1907 {
1908 VALUE str = (VALUE)RSTRING(val);
1909
1910 rettv->v_type = VAR_STRING;
1911 rettv->vval.v_string = vim_strnsave((char_u *)RSTRING_PTR(str),
Bram Moolenaar71ccd032020-06-12 22:59:11 +02001912 RSTRING_LEN(str));
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001913 }
1914 break;
1915 case T_ARRAY:
1916 {
1917 list_T *l;
1918 long i;
1919 typval_T v;
1920
1921 l = list_alloc();
1922 if (l == NULL)
1923 return FAIL;
1924
1925 for (i = 0; i < RARRAY_LEN(val); ++i)
1926 {
1927 if (ruby_convert_to_vim_value((VALUE)RARRAY_PTR(val)[i],
1928 &v) != OK)
1929 {
1930 list_unref(l);
1931 return FAIL;
1932 }
1933 list_append_tv(l, &v);
1934 clear_tv(&v);
1935 }
1936
1937 rettv->v_type = VAR_LIST;
1938 rettv->vval.v_list = l;
1939 ++l->lv_refcount;
1940 }
1941 break;
1942 case T_HASH:
1943 {
1944 dict_T *d;
1945
1946 d = dict_alloc();
1947 if (d == NULL)
1948 return FAIL;
1949
1950 rb_hash_foreach(val, convert_hash2dict, (VALUE)d);
1951 if (d->dv_hashtab.ht_error)
1952 {
1953 dict_unref(d);
1954 return FAIL;
1955 }
1956
1957 rettv->v_type = VAR_DICT;
1958 rettv->vval.v_dict = d;
1959 ++d->dv_refcount;
1960 }
1961 break;
1962 }
1963 return OK;
1964}
1965
1966 void
1967do_rubyeval(char_u *str, typval_T *rettv)
1968{
1969 int retval = FAIL;
1970
1971 if (ensure_ruby_initialized())
1972 {
1973 int state;
1974 VALUE obj;
1975
1976 obj = rb_eval_string_protect((const char *)str, &state);
1977 if (state)
1978 error_print(state);
1979 else
1980 retval = ruby_convert_to_vim_value(obj, rettv);
1981 }
1982 if (retval == FAIL)
1983 {
1984 rettv->v_type = VAR_NUMBER;
1985 rettv->vval.v_number = 0;
1986 }
1987}