blob: c58c3c5f1362afe92bc5aa266adeabb319ddf1f4 [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
ichizok8bb3fe42021-12-28 15:51:45 +000076# 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)
ichizok8bb3fe42021-12-28 15:51:45 +000082# define rb_fix2int rb_fix2int_stub
83# define rb_num2int rb_num2int_stub
Bram Moolenaardace9f72020-12-28 15:07:45 +010084# 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
ichizok8bb3fe42021-12-28 15:51:45 +000090# define rb_gc_writebarrier_unprotect_promoted rb_gc_writebarrier_unprotect_promoted_stub
Bram Moolenaardace9f72020-12-28 15:07:45 +010091# endif
Bram Moolenaara1a118b2014-02-05 22:41:15 +010092
Bram Moolenaardace9f72020-12-28 15:07:45 +010093# if RUBY_VERSION >= 22
ichizok8bb3fe42021-12-28 15:51:45 +000094# define rb_gc_writebarrier_unprotect rb_gc_writebarrier_unprotect_stub
Bram Moolenaardace9f72020-12-28 15:07:45 +010095# endif
96
97# if RUBY_VERSION >= 26
ichizok8bb3fe42021-12-28 15:51:45 +000098# define rb_ary_detransient rb_ary_detransient_stub
Bram Moolenaardace9f72020-12-28 15:07:45 +010099# endif
100
101# if RUBY_VERSION >= 30
ichizok8bb3fe42021-12-28 15:51:45 +0000102# 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# if RUBY_VERSION >= 31
108# define rb_debug_rstring_null_ptr rb_debug_rstring_null_ptr_stub
109# define rb_unexpected_type rb_unexpected_type_stub
Bram Moolenaardace9f72020-12-28 15:07:45 +0100110# endif
111
112#endif // ifdef DYNAMIC_RUBY
Bram Moolenaarb09c6842018-12-27 22:11:01 +0100113
Bram Moolenaar81ea1df2020-04-11 18:01:41 +0200114// On macOS pre-installed Ruby defines "SIZEOF_TIME_T" as "SIZEOF_LONG" so it
Bram Moolenaar92c098d2020-05-26 20:09:11 +0200115// conflicts with the definition in config.h then causes a macro-redefined
116// warning.
Bram Moolenaar81ea1df2020-04-11 18:01:41 +0200117#ifdef SIZEOF_TIME_T
118# undef SIZEOF_TIME_T
119#endif
120
Bram Moolenaar071d4272004-06-13 20:20:40 +0000121#include <ruby.h>
Bram Moolenaar41a41412020-01-07 21:32:19 +0100122#if RUBY_VERSION >= 19
Bram Moolenaar165641d2010-02-17 16:23:09 +0100123# include <ruby/encoding.h>
124#endif
Bram Moolenaar41a41412020-01-07 21:32:19 +0100125#if RUBY_VERSION <= 18
Bram Moolenaar8dc4c722019-04-14 19:42:13 +0200126# include <st.h> // for ST_STOP and ST_CONTINUE
127#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000128
Bram Moolenaar92c098d2020-05-26 20:09:11 +0200129// See above.
130#ifdef SIZEOF_TIME_T
131# undef SIZEOF_TIME_T
132#endif
133
Bram Moolenaar8dc4c722019-04-14 19:42:13 +0200134#undef off_t // ruby defines off_t as _int64, Mingw uses long
Bram Moolenaar071d4272004-06-13 20:20:40 +0000135#undef EXTERN
136#undef _
137
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100138// T_DATA defined both by Ruby and Mac header files, hack around it...
Bram Moolenaard0573012017-10-28 21:11:06 +0200139#if defined(MACOS_X)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000140# define __OPENTRANSPORT__
141# define __OPENTRANSPORTPROTOCOL__
142# define __OPENTRANSPORTPROVIDERS__
143#endif
144
Bram Moolenaar165641d2010-02-17 16:23:09 +0100145/*
Bram Moolenaar0d27f642015-12-28 22:05:28 +0100146 * The TypedData_XXX macro family can be used since Ruby 1.9.2 but
147 * rb_data_type_t changed in 1.9.3, therefore require at least 2.0.
148 * The old Data_XXX macro family was deprecated on Ruby 2.2.
Bram Moolenaarf2f6d292015-12-28 20:57:10 +0100149 * Use TypedData_XXX if available.
150 */
Bram Moolenaar41a41412020-01-07 21:32:19 +0100151#if defined(TypedData_Wrap_Struct) && (RUBY_VERSION >= 20)
Bram Moolenaarf2f6d292015-12-28 20:57:10 +0100152# define USE_TYPEDDATA 1
153#endif
154
155/*
Bram Moolenaar84a05ac2013-05-06 04:24:17 +0200156 * Backward compatibility for Ruby 1.8 and earlier.
Bram Moolenaar165641d2010-02-17 16:23:09 +0100157 * Ruby 1.9 does not provide STR2CSTR, instead StringValuePtr is provided.
158 * Ruby 1.9 does not provide RXXX(s)->len and RXXX(s)->ptr, instead
159 * RXXX_LEN(s) and RXXX_PTR(s) are provided.
160 */
161#ifndef StringValuePtr
162# define StringValuePtr(s) STR2CSTR(s)
163#endif
164#ifndef RARRAY_LEN
165# define RARRAY_LEN(s) RARRAY(s)->len
166#endif
167#ifndef RARRAY_PTR
168# define RARRAY_PTR(s) RARRAY(s)->ptr
169#endif
170#ifndef RSTRING_LEN
171# define RSTRING_LEN(s) RSTRING(s)->len
172#endif
173#ifndef RSTRING_PTR
174# define RSTRING_PTR(s) RSTRING(s)->ptr
175#endif
176
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100177#ifdef HAVE_DUP
178# undef HAVE_DUP
179#endif
180
ichizok8bb3fe42021-12-28 15:51:45 +0000181// Avoid redefining TRUE/FALSE in vterm.h.
182#ifdef TRUE
183# undef TRUE
184#endif
185#ifdef FALSE
186# undef FALSE
187#endif
188
Bram Moolenaar071d4272004-06-13 20:20:40 +0000189#include "vim.h"
190#include "version.h"
191
Bram Moolenaar7dca2eb2019-02-18 20:42:50 +0100192#ifdef DYNAMIC_RUBY
193# if !defined(MSWIN) // must come after including vim.h, where it is defined
194# include <dlfcn.h>
195# define HINSTANCE void*
196# define RUBY_PROC void*
197# define load_dll(n) dlopen((n), RTLD_LAZY|RTLD_GLOBAL)
198# define symbol_from_dll dlsym
199# define close_dll dlclose
Martin Tournoij1a3e5742021-07-24 13:57:29 +0200200# define load_dll_error dlerror
Bram Moolenaar7dca2eb2019-02-18 20:42:50 +0100201# else
202# define RUBY_PROC FARPROC
203# define load_dll vimLoadLib
204# define symbol_from_dll GetProcAddress
205# define close_dll FreeLibrary
Martin Tournoij1a3e5742021-07-24 13:57:29 +0200206# define load_dll_error GetWin32Error
Bram Moolenaar7dca2eb2019-02-18 20:42:50 +0100207# endif
208#endif
209
Bram Moolenaar071d4272004-06-13 20:20:40 +0000210#if defined(PROTO) && !defined(FEAT_RUBY)
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100211// Define these to be able to generate the function prototypes.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000212# define VALUE int
213# define RUBY_DATA_FUNC int
214#endif
215
216static int ruby_initialized = 0;
Bram Moolenaar99685e62013-05-11 13:56:18 +0200217static void *ruby_stack_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000218static VALUE objtbl;
219
220static VALUE mVIM;
221static VALUE cBuffer;
222static VALUE cVimWindow;
223static VALUE eDeletedBufferError;
224static VALUE eDeletedWindowError;
225
226static int ensure_ruby_initialized(void);
227static void error_print(int);
228static void ruby_io_init(void);
229static void ruby_vim_init(void);
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100230static int ruby_convert_to_vim_value(VALUE val, typval_T *rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000231
Bram Moolenaar41a41412020-01-07 21:32:19 +0100232#if (RUBY_VERSION >= 19) || defined(RUBY_INIT_STACK)
Bram Moolenaar10f3a792013-05-20 12:52:29 +0200233# if defined(__ia64) && !defined(ruby_init_stack)
234# define ruby_init_stack(addr) ruby_init_stack((addr), rb_ia64_bsp())
235# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000236#endif
237
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200238#if defined(DYNAMIC_RUBY) || defined(PROTO)
Bram Moolenaaref269542016-01-19 13:22:12 +0100239# if defined(PROTO) && !defined(HINSTANCE)
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100240# define HINSTANCE int // for generating prototypes
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200241# endif
242
Bram Moolenaar071d4272004-06-13 20:20:40 +0000243/*
244 * Wrapper defines
245 */
Bram Moolenaar8b430b42020-02-22 15:01:00 +0100246// Ruby 2.7 actually expands the following symbols as macro.
247# if RUBY_VERSION >= 27
248# undef rb_define_global_function
249# undef rb_define_method
250# undef rb_define_module_function
251# undef rb_define_singleton_method
252# endif
253
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200254# define rb_assoc_new dll_rb_assoc_new
255# define rb_cObject (*dll_rb_cObject)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100256# define rb_class_new_instance dll_rb_class_new_instance
Bram Moolenaardace9f72020-12-28 15:07:45 +0100257# if RUBY_VERSION < 30
258# define rb_check_type dll_rb_check_type
259# endif
Bram Moolenaarf2f6d292015-12-28 20:57:10 +0100260# ifdef USE_TYPEDDATA
261# define rb_check_typeddata dll_rb_check_typeddata
Bram Moolenaar0c7485f2015-01-14 14:04:10 +0100262# endif
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200263# define rb_class_path dll_rb_class_path
Bram Moolenaarf2f6d292015-12-28 20:57:10 +0100264# ifdef USE_TYPEDDATA
Bram Moolenaar41a41412020-01-07 21:32:19 +0100265# if RUBY_VERSION >= 23
Bram Moolenaarf2f6d292015-12-28 20:57:10 +0100266# define rb_data_typed_object_wrap dll_rb_data_typed_object_wrap
267# else
268# define rb_data_typed_object_alloc dll_rb_data_typed_object_alloc
269# endif
270# else
271# define rb_data_object_alloc dll_rb_data_object_alloc
272# endif
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200273# define rb_define_class_under dll_rb_define_class_under
274# define rb_define_const dll_rb_define_const
275# define rb_define_global_function dll_rb_define_global_function
276# define rb_define_method dll_rb_define_method
277# define rb_define_module dll_rb_define_module
278# define rb_define_module_function dll_rb_define_module_function
279# define rb_define_singleton_method dll_rb_define_singleton_method
280# define rb_define_virtual_variable dll_rb_define_virtual_variable
281# define rb_stdout (*dll_rb_stdout)
Bram Moolenaarb6c8cd82018-07-24 05:41:30 +0200282# define rb_stderr (*dll_rb_stderr)
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200283# define rb_eArgError (*dll_rb_eArgError)
284# define rb_eIndexError (*dll_rb_eIndexError)
285# define rb_eRuntimeError (*dll_rb_eRuntimeError)
286# define rb_eStandardError (*dll_rb_eStandardError)
287# define rb_eval_string_protect dll_rb_eval_string_protect
Bram Moolenaar41a41412020-01-07 21:32:19 +0100288# if RUBY_VERSION >= 21
Bram Moolenaarf711cb22018-08-01 18:42:13 +0200289# define rb_funcallv dll_rb_funcallv
290# else
291# define rb_funcall2 dll_rb_funcall2
292# endif
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200293# define rb_global_variable dll_rb_global_variable
294# define rb_hash_aset dll_rb_hash_aset
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100295# define rb_hash_foreach dll_rb_hash_foreach
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200296# define rb_hash_new dll_rb_hash_new
297# define rb_inspect dll_rb_inspect
298# define rb_int2inum dll_rb_int2inum
Bram Moolenaar218beb32018-08-04 17:24:44 +0200299
300// ruby.h may redefine rb_intern to use RUBY_CONST_ID_CACHE(), but that won't
301// work. Not using the cache appears to be the best solution.
302# undef rb_intern
303# define rb_intern dll_rb_intern
304
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100305# if VIM_SIZEOF_INT < VIM_SIZEOF_LONG // 64 bits only
Bram Moolenaar41a41412020-01-07 21:32:19 +0100306# if RUBY_VERSION <= 18
Bram Moolenaar498af702014-03-28 21:58:21 +0100307# define rb_fix2int dll_rb_fix2int
308# define rb_num2int dll_rb_num2int
309# endif
Bram Moolenaardace9f72020-12-28 15:07:45 +0100310# if RUBY_VERSION < 30
311# define rb_num2uint dll_rb_num2uint
312# endif
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200313# endif
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100314# define rb_num2dbl dll_rb_num2dbl
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200315# define rb_lastline_get dll_rb_lastline_get
316# define rb_lastline_set dll_rb_lastline_set
Bram Moolenaard0573012017-10-28 21:11:06 +0200317# define rb_protect dll_rb_protect
318# define rb_load dll_rb_load
Bram Moolenaar41a41412020-01-07 21:32:19 +0100319# if RUBY_VERSION <= 18
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200320# define rb_num2long dll_rb_num2long
321# endif
Bram Moolenaar41a41412020-01-07 21:32:19 +0100322# if RUBY_VERSION <= 19
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200323# define rb_num2ulong dll_rb_num2ulong
324# endif
325# define rb_obj_alloc dll_rb_obj_alloc
326# define rb_obj_as_string dll_rb_obj_as_string
327# define rb_obj_id dll_rb_obj_id
328# define rb_raise dll_rb_raise
329# define rb_str_cat dll_rb_str_cat
330# define rb_str_concat dll_rb_str_concat
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100331# undef rb_str_new
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200332# define rb_str_new dll_rb_str_new
333# ifdef rb_str_new2
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100334// Ruby may #define rb_str_new2 to use rb_str_new_cstr.
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200335# define need_rb_str_new_cstr 1
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100336// Ruby's headers #define rb_str_new_cstr to make use of GCC's
337// __builtin_constant_p extension.
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200338# undef rb_str_new_cstr
339# define rb_str_new_cstr dll_rb_str_new_cstr
Bram Moolenaar76a86062013-05-11 17:45:48 +0200340# else
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200341# define rb_str_new2 dll_rb_str_new2
Bram Moolenaar76a86062013-05-11 17:45:48 +0200342# endif
Bram Moolenaar41a41412020-01-07 21:32:19 +0100343# if RUBY_VERSION >= 18
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200344# define rb_string_value dll_rb_string_value
345# define rb_string_value_ptr dll_rb_string_value_ptr
346# define rb_float_new dll_rb_float_new
347# define rb_ary_new dll_rb_ary_new
Bram Moolenaar51e9fbf2018-08-11 14:24:11 +0200348# ifdef rb_ary_new4
Bram Moolenaar49c99fc2020-02-11 23:01:39 +0100349# define RB_ARY_NEW4_MACRO 1
350# undef rb_ary_new4
Bram Moolenaar51e9fbf2018-08-11 14:24:11 +0200351# endif
352# define rb_ary_new4 dll_rb_ary_new4
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200353# define rb_ary_push dll_rb_ary_push
Bram Moolenaar41a41412020-01-07 21:32:19 +0100354# if (RUBY_VERSION >= 19) || defined(RUBY_INIT_STACK)
Bram Moolenaar10f3a792013-05-20 12:52:29 +0200355# ifdef __ia64
356# define rb_ia64_bsp dll_rb_ia64_bsp
357# undef ruby_init_stack
358# define ruby_init_stack(addr) dll_ruby_init_stack((addr), rb_ia64_bsp())
359# else
360# define ruby_init_stack dll_ruby_init_stack
361# endif
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200362# endif
363# else
364# define rb_str2cstr dll_rb_str2cstr
Bram Moolenaar3ca71f12010-10-27 16:49:47 +0200365# endif
Bram Moolenaar41a41412020-01-07 21:32:19 +0100366# if RUBY_VERSION >= 19
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200367# define rb_errinfo dll_rb_errinfo
368# else
369# define ruby_errinfo (*dll_ruby_errinfo)
370# endif
371# define ruby_init dll_ruby_init
372# define ruby_init_loadpath dll_ruby_init_loadpath
Bram Moolenaar4f974752019-02-17 17:44:42 +0100373# ifdef MSWIN
Bram Moolenaar41a41412020-01-07 21:32:19 +0100374# if RUBY_VERSION >= 19
Bram Moolenaar9d5c84a2018-12-19 20:48:46 +0100375# define ruby_sysinit dll_ruby_sysinit
376# else
377# define NtInitialize dll_NtInitialize
378# endif
Bram Moolenaar41a41412020-01-07 21:32:19 +0100379# if RUBY_VERSION >= 18
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200380# define rb_w32_snprintf dll_rb_w32_snprintf
381# endif
382# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000383
Bram Moolenaar41a41412020-01-07 21:32:19 +0100384# if RUBY_VERSION >= 19
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200385# define ruby_script dll_ruby_script
386# define rb_enc_find_index dll_rb_enc_find_index
387# define rb_enc_find dll_rb_enc_find
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100388# undef rb_enc_str_new
389# define rb_enc_str_new dll_rb_enc_str_new
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200390# define rb_sprintf dll_rb_sprintf
391# define rb_require dll_rb_require
Bram Moolenaar9b1067e2015-11-19 19:33:15 +0100392# define ruby_options dll_ruby_options
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200393# endif
Bram Moolenaar165641d2010-02-17 16:23:09 +0100394
Bram Moolenaar071d4272004-06-13 20:20:40 +0000395/*
396 * Pointers for dynamic link
397 */
398static VALUE (*dll_rb_assoc_new) (VALUE, VALUE);
Bram Moolenaarba52cde2010-06-25 04:29:11 +0200399VALUE *dll_rb_cFalseClass;
400VALUE *dll_rb_cFixnum;
Bram Moolenaar2016ae52016-06-13 20:08:43 +0200401# if defined(USE_RUBY_INTEGER)
Bram Moolenaar06469e92016-06-11 22:26:53 +0200402VALUE *dll_rb_cInteger;
403# endif
Bram Moolenaar41a41412020-01-07 21:32:19 +0100404# if RUBY_VERSION >= 20
Bram Moolenaardb3fbe52013-03-07 15:16:21 +0100405VALUE *dll_rb_cFloat;
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200406# endif
Bram Moolenaarba52cde2010-06-25 04:29:11 +0200407VALUE *dll_rb_cNilClass;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000408static VALUE *dll_rb_cObject;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100409VALUE *dll_rb_cString;
Bram Moolenaarba52cde2010-06-25 04:29:11 +0200410VALUE *dll_rb_cSymbol;
411VALUE *dll_rb_cTrueClass;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100412static VALUE (*dll_rb_class_new_instance) (int,VALUE*,VALUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000413static void (*dll_rb_check_type) (VALUE,int);
Bram Moolenaarf2f6d292015-12-28 20:57:10 +0100414# ifdef USE_TYPEDDATA
415static void *(*dll_rb_check_typeddata) (VALUE,const rb_data_type_t *);
416# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000417static VALUE (*dll_rb_class_path) (VALUE);
Bram Moolenaarf2f6d292015-12-28 20:57:10 +0100418# ifdef USE_TYPEDDATA
Bram Moolenaar41a41412020-01-07 21:32:19 +0100419# if RUBY_VERSION >= 23
Bram Moolenaarf2f6d292015-12-28 20:57:10 +0100420static VALUE (*dll_rb_data_typed_object_wrap) (VALUE, void*, const rb_data_type_t *);
421# else
422static VALUE (*dll_rb_data_typed_object_alloc) (VALUE, void*, const rb_data_type_t *);
423# endif
424# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000425static VALUE (*dll_rb_data_object_alloc) (VALUE, void*, RUBY_DATA_FUNC, RUBY_DATA_FUNC);
Bram Moolenaarf2f6d292015-12-28 20:57:10 +0100426# endif
ichizok8bb3fe42021-12-28 15:51:45 +0000427# if RUBY_VERSION >= 31
428static void (*dll_rb_debug_rstring_null_ptr) (const char*);
429# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000430static VALUE (*dll_rb_define_class_under) (VALUE, const char*, VALUE);
431static void (*dll_rb_define_const) (VALUE,const char*,VALUE);
432static void (*dll_rb_define_global_function) (const char*,VALUE(*)(),int);
433static void (*dll_rb_define_method) (VALUE,const char*,VALUE(*)(),int);
434static VALUE (*dll_rb_define_module) (const char*);
435static void (*dll_rb_define_module_function) (VALUE,const char*,VALUE(*)(),int);
436static void (*dll_rb_define_singleton_method) (VALUE,const char*,VALUE(*)(),int);
437static void (*dll_rb_define_virtual_variable) (const char*,VALUE(*)(),void(*)());
438static VALUE *dll_rb_stdout;
Bram Moolenaarb6c8cd82018-07-24 05:41:30 +0200439static VALUE *dll_rb_stderr;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000440static VALUE *dll_rb_eArgError;
441static VALUE *dll_rb_eIndexError;
442static VALUE *dll_rb_eRuntimeError;
443static VALUE *dll_rb_eStandardError;
444static VALUE (*dll_rb_eval_string_protect) (const char*, int*);
Bram Moolenaar41a41412020-01-07 21:32:19 +0100445# if RUBY_VERSION >= 21
Bram Moolenaarf711cb22018-08-01 18:42:13 +0200446static VALUE (*dll_rb_funcallv) (VALUE, ID, int, const VALUE*);
447# else
448static VALUE (*dll_rb_funcall2) (VALUE, ID, int, const VALUE*);
449# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000450static void (*dll_rb_global_variable) (VALUE*);
451static VALUE (*dll_rb_hash_aset) (VALUE, VALUE, VALUE);
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100452static VALUE (*dll_rb_hash_foreach) (VALUE, int (*)(VALUE, VALUE, VALUE), VALUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000453static VALUE (*dll_rb_hash_new) (void);
454static VALUE (*dll_rb_inspect) (VALUE);
455static VALUE (*dll_rb_int2inum) (long);
Bram Moolenaarf711cb22018-08-01 18:42:13 +0200456static ID (*dll_rb_intern) (const char*);
Bram Moolenaar9d20daf2021-02-01 19:31:47 +0100457# if RUBY_VERSION >= 30 || VIM_SIZEOF_INT < VIM_SIZEOF_LONG
Bram Moolenaar2623b4f2012-09-18 16:36:32 +0200458static long (*dll_rb_fix2int) (VALUE);
459static long (*dll_rb_num2int) (VALUE);
460static unsigned long (*dll_rb_num2uint) (VALUE);
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200461# endif
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100462static double (*dll_rb_num2dbl) (VALUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000463static VALUE (*dll_rb_lastline_get) (void);
464static void (*dll_rb_lastline_set) (VALUE);
Bram Moolenaar37badc82018-01-31 20:15:30 +0100465static VALUE (*dll_rb_protect) (VALUE (*)(VALUE), VALUE, int*);
Bram Moolenaard0573012017-10-28 21:11:06 +0200466static void (*dll_rb_load) (VALUE, int);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000467static long (*dll_rb_num2long) (VALUE);
468static unsigned long (*dll_rb_num2ulong) (VALUE);
469static VALUE (*dll_rb_obj_alloc) (VALUE);
470static VALUE (*dll_rb_obj_as_string) (VALUE);
471static VALUE (*dll_rb_obj_id) (VALUE);
472static void (*dll_rb_raise) (VALUE, const char*, ...);
Bram Moolenaar41a41412020-01-07 21:32:19 +0100473# if RUBY_VERSION >= 18
Bram Moolenaarf9b5ef82010-09-29 13:02:53 +0200474static VALUE (*dll_rb_string_value) (volatile VALUE*);
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200475# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000476static char *(*dll_rb_str2cstr) (VALUE,int*);
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200477# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000478static VALUE (*dll_rb_str_cat) (VALUE, const char*, long);
479static VALUE (*dll_rb_str_concat) (VALUE, VALUE);
480static VALUE (*dll_rb_str_new) (const char*, long);
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200481# ifdef need_rb_str_new_cstr
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100482// Ruby may #define rb_str_new2 to use rb_str_new_cstr.
Bram Moolenaar1d2beae2010-05-22 21:56:55 +0200483static VALUE (*dll_rb_str_new_cstr) (const char*);
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200484# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000485static VALUE (*dll_rb_str_new2) (const char*);
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200486# endif
Bram Moolenaar41a41412020-01-07 21:32:19 +0100487# if RUBY_VERSION >= 19
Bram Moolenaar165641d2010-02-17 16:23:09 +0100488static VALUE (*dll_rb_errinfo) (void);
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200489# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000490static VALUE *dll_ruby_errinfo;
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200491# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000492static void (*dll_ruby_init) (void);
493static void (*dll_ruby_init_loadpath) (void);
Bram Moolenaar4f974752019-02-17 17:44:42 +0100494# ifdef MSWIN
Bram Moolenaar41a41412020-01-07 21:32:19 +0100495# if RUBY_VERSION >= 19
Bram Moolenaar4f391792017-01-15 16:59:07 +0100496static void (*dll_ruby_sysinit) (int*, char***);
Bram Moolenaar9d5c84a2018-12-19 20:48:46 +0100497# else
498static void (*dll_NtInitialize) (int*, char***);
499# endif
Bram Moolenaar41a41412020-01-07 21:32:19 +0100500# if RUBY_VERSION >= 18
Bram Moolenaar3ca71f12010-10-27 16:49:47 +0200501static int (*dll_rb_w32_snprintf)(char*, size_t, const char*, ...);
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200502# endif
Bram Moolenaar3ca71f12010-10-27 16:49:47 +0200503# endif
ichizok8bb3fe42021-12-28 15:51:45 +0000504# if RUBY_VERSION >= 31
ichizoke1240652022-01-07 20:01:07 +0000505# ifdef _MSC_VER
506static void (*dll_rb_unexpected_type) (VALUE, int);
507# else
508NORETURN(static void (*dll_rb_unexpected_type) (VALUE, int));
509# endif
ichizok8bb3fe42021-12-28 15:51:45 +0000510# endif
Bram Moolenaar41a41412020-01-07 21:32:19 +0100511# if RUBY_VERSION >= 18
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100512static char * (*dll_rb_string_value_ptr) (volatile VALUE*);
513static VALUE (*dll_rb_float_new) (double);
514static VALUE (*dll_rb_ary_new) (void);
Bram Moolenaar51e9fbf2018-08-11 14:24:11 +0200515static VALUE (*dll_rb_ary_new4) (long n, const VALUE *elts);
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100516static VALUE (*dll_rb_ary_push) (VALUE, VALUE);
Bram Moolenaar41a41412020-01-07 21:32:19 +0100517# if RUBY_VERSION >= 26
Bram Moolenaarb09c6842018-12-27 22:11:01 +0100518static void (*dll_rb_ary_detransient) (VALUE);
519# endif
Bram Moolenaar41a41412020-01-07 21:32:19 +0100520# if (RUBY_VERSION >= 19) || defined(RUBY_INIT_STACK)
Bram Moolenaar10f3a792013-05-20 12:52:29 +0200521# ifdef __ia64
Bram Moolenaar76a86062013-05-11 17:45:48 +0200522static void * (*dll_rb_ia64_bsp) (void);
523static void (*dll_ruby_init_stack)(VALUE*, void*);
Bram Moolenaar10f3a792013-05-20 12:52:29 +0200524# else
Bram Moolenaar76a86062013-05-11 17:45:48 +0200525static void (*dll_ruby_init_stack)(VALUE*);
Bram Moolenaar10f3a792013-05-20 12:52:29 +0200526# endif
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200527# endif
Bram Moolenaar76a86062013-05-11 17:45:48 +0200528# endif
Bram Moolenaar41a41412020-01-07 21:32:19 +0100529# if RUBY_VERSION >= 19
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100530static VALUE (*dll_rb_int2big)(SIGNED_VALUE);
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200531# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000532
Bram Moolenaar41a41412020-01-07 21:32:19 +0100533# if RUBY_VERSION >= 19
Bram Moolenaar165641d2010-02-17 16:23:09 +0100534static void (*dll_ruby_script) (const char*);
535static int (*dll_rb_enc_find_index) (const char*);
536static rb_encoding* (*dll_rb_enc_find) (const char*);
537static VALUE (*dll_rb_enc_str_new) (const char*, long, rb_encoding*);
538static VALUE (*dll_rb_sprintf) (const char*, ...);
Bram Moolenaar7a8ef142010-12-24 13:39:35 +0100539static VALUE (*dll_rb_require) (const char*);
Bram Moolenaardace9f72020-12-28 15:07:45 +0100540static void* (*dll_ruby_options)(int, char**);
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200541# endif
Bram Moolenaar165641d2010-02-17 16:23:09 +0100542
Bram Moolenaara1a118b2014-02-05 22:41:15 +0100543# if defined(USE_RGENGC) && USE_RGENGC
Bram Moolenaar41a41412020-01-07 21:32:19 +0100544# if RUBY_VERSION == 21
Bram Moolenaara1a118b2014-02-05 22:41:15 +0100545static void (*dll_rb_gc_writebarrier_unprotect_promoted)(VALUE);
Bram Moolenaar0c7485f2015-01-14 14:04:10 +0100546# else
547static void (*dll_rb_gc_writebarrier_unprotect)(VALUE obj);
548# endif
Bram Moolenaara1a118b2014-02-05 22:41:15 +0100549# endif
550
Bram Moolenaardace9f72020-12-28 15:07:45 +0100551# if RUBY_VERSION >= 30
Bram Moolenaar9d20daf2021-02-01 19:31:47 +0100552# ifdef _MSC_VER
553static void (*dll_ruby_malloc_size_overflow)(size_t, size_t);
554# else
Bram Moolenaardace9f72020-12-28 15:07:45 +0100555NORETURN(static void (*dll_ruby_malloc_size_overflow)(size_t, size_t));
Bram Moolenaar9d20daf2021-02-01 19:31:47 +0100556# endif
Bram Moolenaardace9f72020-12-28 15:07:45 +0100557# endif
558
Bram Moolenaar0e121402020-12-10 20:50:34 +0100559# if RUBY_VERSION >= 26
560void rb_ary_detransient_stub(VALUE x);
561# endif
562
Bram Moolenaardace9f72020-12-28 15:07:45 +0100563// Do not generate a prototype here, VALUE isn't always defined.
564# ifndef PROTO
565# if RUBY_VERSION >= 19
566# if RUBY_VERSION >= 22
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100567 long
568rb_num2long_stub(VALUE x)
Bram Moolenaardace9f72020-12-28 15:07:45 +0100569# else
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100570 SIGNED_VALUE
571rb_num2long_stub(VALUE x)
Bram Moolenaardace9f72020-12-28 15:07:45 +0100572# endif
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100573{
574 return dll_rb_num2long(x);
575}
Bram Moolenaardace9f72020-12-28 15:07:45 +0100576# if RUBY_VERSION >= 26
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100577 VALUE
578rb_int2big_stub(intptr_t x)
Bram Moolenaardace9f72020-12-28 15:07:45 +0100579# else
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100580 VALUE
581rb_int2big_stub(SIGNED_VALUE x)
Bram Moolenaardace9f72020-12-28 15:07:45 +0100582# endif
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100583{
584 return dll_rb_int2big(x);
585}
Bram Moolenaar9d20daf2021-02-01 19:31:47 +0100586# if RUBY_VERSION >= 30 || VIM_SIZEOF_INT < VIM_SIZEOF_LONG
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100587 long
588rb_fix2int_stub(VALUE x)
Bram Moolenaar0bcdd6e2013-04-14 16:19:03 +0200589{
590 return dll_rb_fix2int(x);
591}
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100592 long
593rb_num2int_stub(VALUE x)
Bram Moolenaar0bcdd6e2013-04-14 16:19:03 +0200594{
595 return dll_rb_num2int(x);
596}
Bram Moolenaardace9f72020-12-28 15:07:45 +0100597# endif
598# if RUBY_VERSION >= 20
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100599 VALUE
Bram Moolenaar886ed692013-02-26 13:41:35 +0100600rb_float_new_in_heap(double d)
601{
602 return dll_rb_float_new(d);
603}
Bram Moolenaardace9f72020-12-28 15:07:45 +0100604# if RUBY_VERSION >= 22
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100605 unsigned long
606rb_num2ulong(VALUE x)
Bram Moolenaardace9f72020-12-28 15:07:45 +0100607# else
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100608 VALUE
609rb_num2ulong(VALUE x)
Bram Moolenaardace9f72020-12-28 15:07:45 +0100610# endif
Bram Moolenaar886ed692013-02-26 13:41:35 +0100611{
612 return (long)RSHIFT((SIGNED_VALUE)(x),1);
613}
Bram Moolenaardace9f72020-12-28 15:07:45 +0100614# endif
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200615# endif
Bram Moolenaardace9f72020-12-28 15:07:45 +0100616# if defined(USE_RGENGC) && USE_RGENGC
617# if RUBY_VERSION == 21
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100618 void
619rb_gc_writebarrier_unprotect_promoted_stub(VALUE obj)
Bram Moolenaara1a118b2014-02-05 22:41:15 +0100620{
Bram Moolenaar90140742014-11-27 17:44:08 +0100621 dll_rb_gc_writebarrier_unprotect_promoted(obj);
Bram Moolenaara1a118b2014-02-05 22:41:15 +0100622}
Bram Moolenaardace9f72020-12-28 15:07:45 +0100623# else
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100624 void
625rb_gc_writebarrier_unprotect_stub(VALUE obj)
Bram Moolenaar0c7485f2015-01-14 14:04:10 +0100626{
627 dll_rb_gc_writebarrier_unprotect(obj);
628}
Bram Moolenaardace9f72020-12-28 15:07:45 +0100629# endif
Bram Moolenaar0c7485f2015-01-14 14:04:10 +0100630# endif
Bram Moolenaardace9f72020-12-28 15:07:45 +0100631# if RUBY_VERSION >= 26
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100632 void
633rb_ary_detransient_stub(VALUE x)
Bram Moolenaarf62fc312019-01-08 20:29:32 +0100634{
635 dll_rb_ary_detransient(x);
636}
Bram Moolenaardace9f72020-12-28 15:07:45 +0100637# endif
638# if RUBY_VERSION >= 30
639 void
640rb_check_type_stub(VALUE obj, int t)
641{
642 dll_rb_check_type(obj, t);
643}
644 unsigned long
645rb_num2uint_stub(VALUE x)
646{
647 return dll_rb_num2uint(x);
648}
649 void
650ruby_malloc_size_overflow_stub(size_t x, size_t y)
651{
652 dll_ruby_malloc_size_overflow(x, y);
653}
654# endif
ichizok8bb3fe42021-12-28 15:51:45 +0000655# if RUBY_VERSION >= 31
656 void
657rb_debug_rstring_null_ptr_stub(const char *func)
658{
659 dll_rb_debug_rstring_null_ptr(func);
660}
661 void
662rb_unexpected_type_stub(VALUE self, int t)
663{
664 dll_rb_unexpected_type(self, t);
665}
666# endif
Bram Moolenaardace9f72020-12-28 15:07:45 +0100667# endif // ifndef PROTO
Bram Moolenaarf62fc312019-01-08 20:29:32 +0100668
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100669static HINSTANCE hinstRuby = NULL; // Instance of ruby.dll
Bram Moolenaar071d4272004-06-13 20:20:40 +0000670
671/*
Bram Moolenaarda2303d2005-08-30 21:55:26 +0000672 * Table of name to function pointer of ruby.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000673 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000674static struct
675{
676 char *name;
677 RUBY_PROC *ptr;
678} ruby_funcname_table[] =
679{
680 {"rb_assoc_new", (RUBY_PROC*)&dll_rb_assoc_new},
681 {"rb_cFalseClass", (RUBY_PROC*)&dll_rb_cFalseClass},
Bram Moolenaar2016ae52016-06-13 20:08:43 +0200682# if defined(USE_RUBY_INTEGER)
Bram Moolenaar06469e92016-06-11 22:26:53 +0200683 {"rb_cInteger", (RUBY_PROC*)&dll_rb_cInteger},
Bram Moolenaar6abda992017-01-09 21:10:31 +0100684# else
685 {"rb_cFixnum", (RUBY_PROC*)&dll_rb_cFixnum},
Bram Moolenaar06469e92016-06-11 22:26:53 +0200686# endif
Bram Moolenaar41a41412020-01-07 21:32:19 +0100687# if RUBY_VERSION >= 20
Bram Moolenaardb3fbe52013-03-07 15:16:21 +0100688 {"rb_cFloat", (RUBY_PROC*)&dll_rb_cFloat},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200689# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000690 {"rb_cNilClass", (RUBY_PROC*)&dll_rb_cNilClass},
691 {"rb_cObject", (RUBY_PROC*)&dll_rb_cObject},
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100692 {"rb_cString", (RUBY_PROC*)&dll_rb_cString},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000693 {"rb_cSymbol", (RUBY_PROC*)&dll_rb_cSymbol},
694 {"rb_cTrueClass", (RUBY_PROC*)&dll_rb_cTrueClass},
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100695 {"rb_class_new_instance", (RUBY_PROC*)&dll_rb_class_new_instance},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000696 {"rb_check_type", (RUBY_PROC*)&dll_rb_check_type},
Bram Moolenaarf2f6d292015-12-28 20:57:10 +0100697# ifdef USE_TYPEDDATA
698 {"rb_check_typeddata", (RUBY_PROC*)&dll_rb_check_typeddata},
699# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000700 {"rb_class_path", (RUBY_PROC*)&dll_rb_class_path},
Bram Moolenaarf2f6d292015-12-28 20:57:10 +0100701# ifdef USE_TYPEDDATA
Bram Moolenaar41a41412020-01-07 21:32:19 +0100702# if RUBY_VERSION >= 23
Bram Moolenaarf2f6d292015-12-28 20:57:10 +0100703 {"rb_data_typed_object_wrap", (RUBY_PROC*)&dll_rb_data_typed_object_wrap},
704# else
705 {"rb_data_typed_object_alloc", (RUBY_PROC*)&dll_rb_data_typed_object_alloc},
706# endif
707# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000708 {"rb_data_object_alloc", (RUBY_PROC*)&dll_rb_data_object_alloc},
Bram Moolenaarf2f6d292015-12-28 20:57:10 +0100709# endif
ichizok8bb3fe42021-12-28 15:51:45 +0000710# if RUBY_VERSION >= 31
711 {"rb_debug_rstring_null_ptr", (RUBY_PROC*)&dll_rb_debug_rstring_null_ptr},
712# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000713 {"rb_define_class_under", (RUBY_PROC*)&dll_rb_define_class_under},
714 {"rb_define_const", (RUBY_PROC*)&dll_rb_define_const},
715 {"rb_define_global_function", (RUBY_PROC*)&dll_rb_define_global_function},
716 {"rb_define_method", (RUBY_PROC*)&dll_rb_define_method},
717 {"rb_define_module", (RUBY_PROC*)&dll_rb_define_module},
718 {"rb_define_module_function", (RUBY_PROC*)&dll_rb_define_module_function},
719 {"rb_define_singleton_method", (RUBY_PROC*)&dll_rb_define_singleton_method},
720 {"rb_define_virtual_variable", (RUBY_PROC*)&dll_rb_define_virtual_variable},
721 {"rb_stdout", (RUBY_PROC*)&dll_rb_stdout},
Bram Moolenaarb6c8cd82018-07-24 05:41:30 +0200722 {"rb_stderr", (RUBY_PROC*)&dll_rb_stderr},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000723 {"rb_eArgError", (RUBY_PROC*)&dll_rb_eArgError},
724 {"rb_eIndexError", (RUBY_PROC*)&dll_rb_eIndexError},
725 {"rb_eRuntimeError", (RUBY_PROC*)&dll_rb_eRuntimeError},
726 {"rb_eStandardError", (RUBY_PROC*)&dll_rb_eStandardError},
727 {"rb_eval_string_protect", (RUBY_PROC*)&dll_rb_eval_string_protect},
Bram Moolenaar41a41412020-01-07 21:32:19 +0100728# if RUBY_VERSION >= 21
Bram Moolenaarf711cb22018-08-01 18:42:13 +0200729 {"rb_funcallv", (RUBY_PROC*)&dll_rb_funcallv},
730# else
731 {"rb_funcall2", (RUBY_PROC*)&dll_rb_funcall2},
732# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000733 {"rb_global_variable", (RUBY_PROC*)&dll_rb_global_variable},
734 {"rb_hash_aset", (RUBY_PROC*)&dll_rb_hash_aset},
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100735 {"rb_hash_foreach", (RUBY_PROC*)&dll_rb_hash_foreach},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000736 {"rb_hash_new", (RUBY_PROC*)&dll_rb_hash_new},
737 {"rb_inspect", (RUBY_PROC*)&dll_rb_inspect},
738 {"rb_int2inum", (RUBY_PROC*)&dll_rb_int2inum},
Bram Moolenaarf711cb22018-08-01 18:42:13 +0200739 {"rb_intern", (RUBY_PROC*)&dll_rb_intern},
Bram Moolenaar9d20daf2021-02-01 19:31:47 +0100740# if RUBY_VERSION >= 30 || VIM_SIZEOF_INT < VIM_SIZEOF_LONG
Bram Moolenaar2623b4f2012-09-18 16:36:32 +0200741 {"rb_fix2int", (RUBY_PROC*)&dll_rb_fix2int},
742 {"rb_num2int", (RUBY_PROC*)&dll_rb_num2int},
743 {"rb_num2uint", (RUBY_PROC*)&dll_rb_num2uint},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200744# endif
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100745 {"rb_num2dbl", (RUBY_PROC*)&dll_rb_num2dbl},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000746 {"rb_lastline_get", (RUBY_PROC*)&dll_rb_lastline_get},
747 {"rb_lastline_set", (RUBY_PROC*)&dll_rb_lastline_set},
Bram Moolenaard0573012017-10-28 21:11:06 +0200748 {"rb_protect", (RUBY_PROC*)&dll_rb_protect},
749 {"rb_load", (RUBY_PROC*)&dll_rb_load},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000750 {"rb_num2long", (RUBY_PROC*)&dll_rb_num2long},
751 {"rb_num2ulong", (RUBY_PROC*)&dll_rb_num2ulong},
752 {"rb_obj_alloc", (RUBY_PROC*)&dll_rb_obj_alloc},
753 {"rb_obj_as_string", (RUBY_PROC*)&dll_rb_obj_as_string},
754 {"rb_obj_id", (RUBY_PROC*)&dll_rb_obj_id},
755 {"rb_raise", (RUBY_PROC*)&dll_rb_raise},
Bram Moolenaar41a41412020-01-07 21:32:19 +0100756# if RUBY_VERSION >= 18
Bram Moolenaarf9b5ef82010-09-29 13:02:53 +0200757 {"rb_string_value", (RUBY_PROC*)&dll_rb_string_value},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200758# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000759 {"rb_str2cstr", (RUBY_PROC*)&dll_rb_str2cstr},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200760# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000761 {"rb_str_cat", (RUBY_PROC*)&dll_rb_str_cat},
762 {"rb_str_concat", (RUBY_PROC*)&dll_rb_str_concat},
763 {"rb_str_new", (RUBY_PROC*)&dll_rb_str_new},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200764# ifdef need_rb_str_new_cstr
Bram Moolenaar1d2beae2010-05-22 21:56:55 +0200765 {"rb_str_new_cstr", (RUBY_PROC*)&dll_rb_str_new_cstr},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200766# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000767 {"rb_str_new2", (RUBY_PROC*)&dll_rb_str_new2},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200768# endif
Bram Moolenaar41a41412020-01-07 21:32:19 +0100769# if RUBY_VERSION >= 19
Bram Moolenaar165641d2010-02-17 16:23:09 +0100770 {"rb_errinfo", (RUBY_PROC*)&dll_rb_errinfo},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200771# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000772 {"ruby_errinfo", (RUBY_PROC*)&dll_ruby_errinfo},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200773# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000774 {"ruby_init", (RUBY_PROC*)&dll_ruby_init},
775 {"ruby_init_loadpath", (RUBY_PROC*)&dll_ruby_init_loadpath},
Bram Moolenaar4f974752019-02-17 17:44:42 +0100776# ifdef MSWIN
Bram Moolenaar41a41412020-01-07 21:32:19 +0100777# if RUBY_VERSION >= 19
Bram Moolenaar4f391792017-01-15 16:59:07 +0100778 {"ruby_sysinit", (RUBY_PROC*)&dll_ruby_sysinit},
Bram Moolenaar9d5c84a2018-12-19 20:48:46 +0100779# else
780 {"NtInitialize", (RUBY_PROC*)&dll_NtInitialize},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200781# endif
Bram Moolenaar41a41412020-01-07 21:32:19 +0100782# if RUBY_VERSION >= 18
Bram Moolenaar071d4272004-06-13 20:20:40 +0000783 {"rb_w32_snprintf", (RUBY_PROC*)&dll_rb_w32_snprintf},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200784# endif
Bram Moolenaar3ca71f12010-10-27 16:49:47 +0200785# endif
ichizok8bb3fe42021-12-28 15:51:45 +0000786# if RUBY_VERSION >= 31
787 {"rb_unexpected_type", (RUBY_PROC*)&dll_rb_unexpected_type},
788# endif
Bram Moolenaar41a41412020-01-07 21:32:19 +0100789# if RUBY_VERSION >= 18
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100790 {"rb_string_value_ptr", (RUBY_PROC*)&dll_rb_string_value_ptr},
Bram Moolenaar41a41412020-01-07 21:32:19 +0100791# if RUBY_VERSION <= 19
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100792 {"rb_float_new", (RUBY_PROC*)&dll_rb_float_new},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200793# else
Bram Moolenaar886ed692013-02-26 13:41:35 +0100794 {"rb_float_new_in_heap", (RUBY_PROC*)&dll_rb_float_new},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200795# endif
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100796 {"rb_ary_new", (RUBY_PROC*)&dll_rb_ary_new},
Bram Moolenaar51e9fbf2018-08-11 14:24:11 +0200797# ifdef RB_ARY_NEW4_MACRO
798 {"rb_ary_new_from_values", (RUBY_PROC*)&dll_rb_ary_new4},
799# else
800 {"rb_ary_new4", (RUBY_PROC*)&dll_rb_ary_new4},
801# endif
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100802 {"rb_ary_push", (RUBY_PROC*)&dll_rb_ary_push},
Bram Moolenaar41a41412020-01-07 21:32:19 +0100803# if RUBY_VERSION >= 26
Bram Moolenaarb09c6842018-12-27 22:11:01 +0100804 {"rb_ary_detransient", (RUBY_PROC*)&dll_rb_ary_detransient},
805# endif
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200806# endif
Bram Moolenaar41a41412020-01-07 21:32:19 +0100807# if RUBY_VERSION >= 19
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100808 {"rb_int2big", (RUBY_PROC*)&dll_rb_int2big},
Bram Moolenaar165641d2010-02-17 16:23:09 +0100809 {"ruby_script", (RUBY_PROC*)&dll_ruby_script},
810 {"rb_enc_find_index", (RUBY_PROC*)&dll_rb_enc_find_index},
811 {"rb_enc_find", (RUBY_PROC*)&dll_rb_enc_find},
812 {"rb_enc_str_new", (RUBY_PROC*)&dll_rb_enc_str_new},
813 {"rb_sprintf", (RUBY_PROC*)&dll_rb_sprintf},
Bram Moolenaar7a8ef142010-12-24 13:39:35 +0100814 {"rb_require", (RUBY_PROC*)&dll_rb_require},
Bram Moolenaar9b1067e2015-11-19 19:33:15 +0100815 {"ruby_options", (RUBY_PROC*)&dll_ruby_options},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200816# endif
Bram Moolenaar41a41412020-01-07 21:32:19 +0100817# if (RUBY_VERSION >= 19) || defined(RUBY_INIT_STACK)
Bram Moolenaar10f3a792013-05-20 12:52:29 +0200818# ifdef __ia64
819 {"rb_ia64_bsp", (RUBY_PROC*)&dll_rb_ia64_bsp},
820# endif
821 {"ruby_init_stack", (RUBY_PROC*)&dll_ruby_init_stack},
822# endif
Bram Moolenaara1a118b2014-02-05 22:41:15 +0100823# if defined(USE_RGENGC) && USE_RGENGC
Bram Moolenaar41a41412020-01-07 21:32:19 +0100824# if RUBY_VERSION == 21
Bram Moolenaara1a118b2014-02-05 22:41:15 +0100825 {"rb_gc_writebarrier_unprotect_promoted", (RUBY_PROC*)&dll_rb_gc_writebarrier_unprotect_promoted},
Bram Moolenaar0c7485f2015-01-14 14:04:10 +0100826# else
827 {"rb_gc_writebarrier_unprotect", (RUBY_PROC*)&dll_rb_gc_writebarrier_unprotect},
828# endif
Bram Moolenaara1a118b2014-02-05 22:41:15 +0100829# endif
Bram Moolenaardace9f72020-12-28 15:07:45 +0100830# if RUBY_VERSION >= 30
831 {"ruby_malloc_size_overflow", (RUBY_PROC*)&dll_ruby_malloc_size_overflow},
832# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000833 {"", NULL},
834};
835
836/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000837 * Load library and get all pointers.
838 * Parameter 'libname' provides name of DLL.
839 * Return OK or FAIL.
840 */
841 static int
842ruby_runtime_link_init(char *libname, int verbose)
843{
844 int i;
845
846 if (hinstRuby)
847 return OK;
Bram Moolenaarf9b5ef82010-09-29 13:02:53 +0200848 hinstRuby = load_dll(libname);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000849 if (!hinstRuby)
850 {
851 if (verbose)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +0000852 semsg(_(e_could_not_load_library_str_str), libname, load_dll_error());
Bram Moolenaar071d4272004-06-13 20:20:40 +0000853 return FAIL;
854 }
855
856 for (i = 0; ruby_funcname_table[i].ptr; ++i)
857 {
Bram Moolenaarf9b5ef82010-09-29 13:02:53 +0200858 if (!(*ruby_funcname_table[i].ptr = symbol_from_dll(hinstRuby,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000859 ruby_funcname_table[i].name)))
860 {
Bram Moolenaarf9b5ef82010-09-29 13:02:53 +0200861 close_dll(hinstRuby);
Bram Moolenaar3ca71f12010-10-27 16:49:47 +0200862 hinstRuby = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000863 if (verbose)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +0000864 semsg(_(e_could_not_load_library_function_str), ruby_funcname_table[i].name);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000865 return FAIL;
866 }
867 }
868 return OK;
869}
870
871/*
872 * If ruby is enabled (there is installed ruby on Windows system) return TRUE,
873 * else FALSE.
874 */
875 int
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100876ruby_enabled(int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000877{
Bram Moolenaar25e4fcd2016-01-09 14:57:47 +0100878 return ruby_runtime_link_init((char *)p_rubydll, verbose) == OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000879}
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100880#endif // defined(DYNAMIC_RUBY) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000881
882 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100883ruby_end(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000884{
Bram Moolenaar071d4272004-06-13 20:20:40 +0000885}
886
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100887 void
888ex_ruby(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000889{
890 int state;
891 char *script = NULL;
892
Bram Moolenaar35a2e192006-03-13 22:07:11 +0000893 script = (char *)script_get(eap, eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000894 if (!eap->skip && ensure_ruby_initialized())
895 {
896 if (script == NULL)
897 rb_eval_string_protect((char *)eap->arg, &state);
898 else
899 rb_eval_string_protect(script, &state);
900 if (state)
901 error_print(state);
902 }
903 vim_free(script);
904}
905
Bram Moolenaar165641d2010-02-17 16:23:09 +0100906/*
907 * In Ruby 1.9 or later, ruby String object has encoding.
908 * conversion buffer string of vim to ruby String object using
909 * VIM encoding option.
910 */
911 static VALUE
912vim_str2rb_enc_str(const char *s)
913{
Bram Moolenaar41a41412020-01-07 21:32:19 +0100914#if RUBY_VERSION >= 19
Bram Moolenaar165641d2010-02-17 16:23:09 +0100915 long lval;
916 char_u *sval;
917 rb_encoding *enc;
918
Yegappan Lakshmanan64095532021-12-06 11:03:55 +0000919 if (get_option_value((char_u *)"enc", &lval, &sval, NULL, 0) == gov_string)
Bram Moolenaar165641d2010-02-17 16:23:09 +0100920 {
921 enc = rb_enc_find((char *)sval);
922 vim_free(sval);
Bram Moolenaar758535a2016-03-30 22:06:16 +0200923 if (enc)
Bram Moolenaar9b0ac222016-06-01 20:31:43 +0200924 return rb_enc_str_new(s, (long)strlen(s), enc);
Bram Moolenaar165641d2010-02-17 16:23:09 +0100925 }
926#endif
927 return rb_str_new2(s);
928}
929
930 static VALUE
931eval_enc_string_protect(const char *str, int *state)
932{
Bram Moolenaar41a41412020-01-07 21:32:19 +0100933#if RUBY_VERSION >= 19
Bram Moolenaar165641d2010-02-17 16:23:09 +0100934 long lval;
935 char_u *sval;
936 rb_encoding *enc;
937 VALUE v;
938
Yegappan Lakshmanan64095532021-12-06 11:03:55 +0000939 if (get_option_value((char_u *)"enc", &lval, &sval, NULL, 0) == gov_string)
Bram Moolenaar165641d2010-02-17 16:23:09 +0100940 {
941 enc = rb_enc_find((char *)sval);
942 vim_free(sval);
943 if (enc)
944 {
945 v = rb_sprintf("#-*- coding:%s -*-\n%s", rb_enc_name(enc), str);
946 return rb_eval_string_protect(StringValuePtr(v), state);
947 }
948 }
949#endif
950 return rb_eval_string_protect(str, state);
951}
952
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100953 void
954ex_rubydo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000955{
956 int state;
957 linenr_T i;
Bram Moolenaarc593fee2017-01-29 23:11:25 +0100958 buf_T *was_curbuf = curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000959
960 if (ensure_ruby_initialized())
961 {
962 if (u_save(eap->line1 - 1, eap->line2 + 1) != OK)
963 return;
Bram Moolenaar758535a2016-03-30 22:06:16 +0200964 for (i = eap->line1; i <= eap->line2; i++)
965 {
Bram Moolenaare980d8a2010-12-08 13:11:21 +0100966 VALUE line;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000967
Bram Moolenaarc593fee2017-01-29 23:11:25 +0100968 if (i > curbuf->b_ml.ml_line_count)
969 break;
Bram Moolenaare980d8a2010-12-08 13:11:21 +0100970 line = vim_str2rb_enc_str((char *)ml_get(i));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000971 rb_lastline_set(line);
Bram Moolenaar165641d2010-02-17 16:23:09 +0100972 eval_enc_string_protect((char *) eap->arg, &state);
Bram Moolenaar758535a2016-03-30 22:06:16 +0200973 if (state)
974 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000975 error_print(state);
976 break;
977 }
Bram Moolenaarc593fee2017-01-29 23:11:25 +0100978 if (was_curbuf != curbuf)
979 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000980 line = rb_lastline_get();
Bram Moolenaar758535a2016-03-30 22:06:16 +0200981 if (!NIL_P(line))
982 {
983 if (TYPE(line) != T_STRING)
984 {
Bram Moolenaar9a846fb2022-01-01 21:59:18 +0000985 emsg(_(e_dollar_must_be_an_instance_of_string));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000986 return;
987 }
Bram Moolenaar165641d2010-02-17 16:23:09 +0100988 ml_replace(i, (char_u *) StringValuePtr(line), 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000989 changed();
990#ifdef SYNTAX_HL
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100991 syn_changed(i); // recompute syntax hl. for this line
Bram Moolenaar071d4272004-06-13 20:20:40 +0000992#endif
993 }
994 }
995 check_cursor();
996 update_curbuf(NOT_VALID);
997 }
998}
999
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001000 static VALUE
1001rb_load_wrap(VALUE file_to_load)
Bram Moolenaar37badc82018-01-31 20:15:30 +01001002{
1003 rb_load(file_to_load, 0);
1004 return Qnil;
1005}
1006
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001007 void
1008ex_rubyfile(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001009{
1010 int state;
1011
1012 if (ensure_ruby_initialized())
1013 {
Bram Moolenaar37badc82018-01-31 20:15:30 +01001014 VALUE file_to_load = rb_str_new2((const char *)eap->arg);
1015 rb_protect(rb_load_wrap, file_to_load, &state);
1016 if (state)
1017 error_print(state);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001018 }
1019}
1020
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001021 void
1022ruby_buffer_free(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001023{
Bram Moolenaare344bea2005-09-01 20:46:49 +00001024 if (buf->b_ruby_ref)
1025 {
1026 rb_hash_aset(objtbl, rb_obj_id((VALUE) buf->b_ruby_ref), Qnil);
1027 RDATA(buf->b_ruby_ref)->data = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001028 }
1029}
1030
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001031 void
1032ruby_window_free(win_T *win)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001033{
Bram Moolenaare344bea2005-09-01 20:46:49 +00001034 if (win->w_ruby_ref)
1035 {
1036 rb_hash_aset(objtbl, rb_obj_id((VALUE) win->w_ruby_ref), Qnil);
1037 RDATA(win->w_ruby_ref)->data = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001038 }
1039}
1040
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001041 static int
1042ensure_ruby_initialized(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001043{
1044 if (!ruby_initialized)
1045 {
1046#ifdef DYNAMIC_RUBY
1047 if (ruby_enabled(TRUE))
1048 {
1049#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01001050#ifdef MSWIN
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001051 // suggested by Ariya Mizutani
Bram Moolenaar0b69c732010-02-17 15:11:50 +01001052 int argc = 1;
1053 char *argv[] = {"gvim.exe"};
Bram Moolenaar90140742014-11-27 17:44:08 +01001054 char **argvp = argv;
Bram Moolenaar41a41412020-01-07 21:32:19 +01001055# if RUBY_VERSION >= 19
Bram Moolenaarfe6ce332017-01-14 20:12:01 +01001056 ruby_sysinit(&argc, &argvp);
1057# else
Bram Moolenaar90140742014-11-27 17:44:08 +01001058 NtInitialize(&argc, &argvp);
Bram Moolenaarfe6ce332017-01-14 20:12:01 +01001059# endif
Bram Moolenaar0b69c732010-02-17 15:11:50 +01001060#endif
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001061 {
Bram Moolenaar41a41412020-01-07 21:32:19 +01001062#if (RUBY_VERSION >= 19) || defined(RUBY_INIT_STACK)
Bram Moolenaar99685e62013-05-11 13:56:18 +02001063 ruby_init_stack(ruby_stack_start);
Bram Moolenaar165641d2010-02-17 16:23:09 +01001064#endif
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001065 ruby_init();
1066 }
Bram Moolenaar41a41412020-01-07 21:32:19 +01001067#if RUBY_VERSION >= 19
Bram Moolenaar7a8ef142010-12-24 13:39:35 +01001068 {
1069 int dummy_argc = 2;
Bram Moolenaard1bc96ce2017-09-26 21:21:44 +02001070 char *dummy_argv[] = {"vim-ruby", "-e_=0"};
Bram Moolenaar9b1067e2015-11-19 19:33:15 +01001071 ruby_options(dummy_argc, dummy_argv);
Bram Moolenaar7a8ef142010-12-24 13:39:35 +01001072 }
Bram Moolenaar165641d2010-02-17 16:23:09 +01001073 ruby_script("vim-ruby");
Bram Moolenaar7a8ef142010-12-24 13:39:35 +01001074#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001075 ruby_init_loadpath();
Bram Moolenaar165641d2010-02-17 16:23:09 +01001076#endif
Bram Moolenaar7a8ef142010-12-24 13:39:35 +01001077 ruby_io_init();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001078 ruby_vim_init();
1079 ruby_initialized = 1;
1080#ifdef DYNAMIC_RUBY
1081 }
1082 else
1083 {
Bram Moolenaar9a846fb2022-01-01 21:59:18 +00001084 emsg(_(e_sorry_this_command_is_disabled_the_ruby_library_could_not_be_loaded));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001085 return 0;
1086 }
1087#endif
1088 }
1089 return ruby_initialized;
1090}
1091
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001092 static void
1093error_print(int state)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001094{
Bram Moolenaar41a41412020-01-07 21:32:19 +01001095#if !defined(DYNAMIC_RUBY) && (RUBY_VERSION <= 18)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001096 RUBYEXTERN VALUE ruby_errinfo;
1097#endif
Bram Moolenaarf711cb22018-08-01 18:42:13 +02001098 VALUE error;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001099 VALUE eclass;
1100 VALUE einfo;
Bram Moolenaarf711cb22018-08-01 18:42:13 +02001101 VALUE bt;
1102 int attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001103 char buff[BUFSIZ];
Bram Moolenaarf711cb22018-08-01 18:42:13 +02001104 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001105
1106#define TAG_RETURN 0x1
1107#define TAG_BREAK 0x2
1108#define TAG_NEXT 0x3
1109#define TAG_RETRY 0x4
1110#define TAG_REDO 0x5
1111#define TAG_RAISE 0x6
1112#define TAG_THROW 0x7
1113#define TAG_FATAL 0x8
1114#define TAG_MASK 0xf
1115
Bram Moolenaar758535a2016-03-30 22:06:16 +02001116 switch (state)
1117 {
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001118 case TAG_RETURN:
Bram Moolenaar9a846fb2022-01-01 21:59:18 +00001119 emsg(_(e_unexpected_return));
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001120 break;
1121 case TAG_NEXT:
Bram Moolenaar9a846fb2022-01-01 21:59:18 +00001122 emsg(_(e_unexpected_next));
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001123 break;
1124 case TAG_BREAK:
Bram Moolenaar9a846fb2022-01-01 21:59:18 +00001125 emsg(_(e_unexpected_break));
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001126 break;
1127 case TAG_REDO:
Bram Moolenaar9a846fb2022-01-01 21:59:18 +00001128 emsg(_(e_unexpected_redo));
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001129 break;
1130 case TAG_RETRY:
Bram Moolenaar9a846fb2022-01-01 21:59:18 +00001131 emsg(_(e_retry_outside_of_rescue_clause));
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001132 break;
1133 case TAG_RAISE:
1134 case TAG_FATAL:
Bram Moolenaar41a41412020-01-07 21:32:19 +01001135#if RUBY_VERSION >= 19
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001136 error = rb_errinfo();
Bram Moolenaar165641d2010-02-17 16:23:09 +01001137#else
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001138 error = ruby_errinfo;
Bram Moolenaar165641d2010-02-17 16:23:09 +01001139#endif
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001140 eclass = CLASS_OF(error);
1141 einfo = rb_obj_as_string(error);
1142 if (eclass == rb_eRuntimeError && RSTRING_LEN(einfo) == 0)
1143 {
Bram Moolenaar9a846fb2022-01-01 21:59:18 +00001144 emsg(_(e_unhandled_exception));
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001145 }
1146 else
1147 {
1148 VALUE epath;
1149 char *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001150
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001151 epath = rb_class_path(eclass);
1152 vim_snprintf(buff, BUFSIZ, "%s: %s",
1153 RSTRING_PTR(epath), RSTRING_PTR(einfo));
1154 p = strchr(buff, '\n');
1155 if (p) *p = '\0';
1156 emsg(buff);
1157 }
Bram Moolenaarf711cb22018-08-01 18:42:13 +02001158
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001159 attr = syn_name2attr((char_u *)"Error");
Bram Moolenaar49c99fc2020-02-11 23:01:39 +01001160#if RUBY_VERSION >= 21
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001161 bt = rb_funcallv(error, rb_intern("backtrace"), 0, 0);
1162 for (i = 0; i < RARRAY_LEN(bt); i++)
1163 msg_attr(RSTRING_PTR(RARRAY_AREF(bt, i)), attr);
Bram Moolenaar49c99fc2020-02-11 23:01:39 +01001164#else
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001165 bt = rb_funcall2(error, rb_intern("backtrace"), 0, 0);
1166 for (i = 0; i < RARRAY_LEN(bt); i++)
1167 msg_attr(RSTRING_PTR(RARRAY_PTR(bt)[i]), attr);
Bram Moolenaar49c99fc2020-02-11 23:01:39 +01001168#endif
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001169 break;
1170 default:
Bram Moolenaar9a846fb2022-01-01 21:59:18 +00001171 vim_snprintf(buff, BUFSIZ, _(e_unknown_longjmp_status_nr), state);
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001172 emsg(buff);
1173 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001174 }
1175}
1176
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001177 static VALUE
1178vim_message(VALUE self UNUSED, VALUE str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001179{
1180 char *buff, *p;
1181
1182 str = rb_obj_as_string(str);
Bram Moolenaar3f5f7952011-08-04 19:34:59 +02001183 if (RSTRING_LEN(str) > 0)
1184 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001185 // Only do this when the string isn't empty, alloc(0) causes trouble.
Bram Moolenaar00ccf542017-09-03 15:17:48 +02001186 buff = ALLOCA_N(char, RSTRING_LEN(str) + 1);
Bram Moolenaar3f5f7952011-08-04 19:34:59 +02001187 strcpy(buff, RSTRING_PTR(str));
1188 p = strchr(buff, '\n');
1189 if (p) *p = '\0';
Bram Moolenaar32526b32019-01-19 17:43:09 +01001190 msg(buff);
Bram Moolenaar3f5f7952011-08-04 19:34:59 +02001191 }
1192 else
1193 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01001194 msg("");
Bram Moolenaar3f5f7952011-08-04 19:34:59 +02001195 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001196 return Qnil;
1197}
1198
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001199 static VALUE
1200vim_set_option(VALUE self UNUSED, VALUE str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001201{
Bram Moolenaar165641d2010-02-17 16:23:09 +01001202 do_set((char_u *)StringValuePtr(str), 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001203 update_screen(NOT_VALID);
1204 return Qnil;
1205}
1206
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001207 static VALUE
1208vim_command(VALUE self UNUSED, VALUE str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001209{
Bram Moolenaar165641d2010-02-17 16:23:09 +01001210 do_cmdline_cmd((char_u *)StringValuePtr(str));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001211 return Qnil;
1212}
1213
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001214#ifdef FEAT_EVAL
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001215 static VALUE
1216vim_to_ruby(typval_T *tv)
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001217{
1218 VALUE result = Qnil;
1219
1220 if (tv->v_type == VAR_STRING)
1221 {
Bram Moolenaar94127e42010-03-19 23:08:48 +01001222 result = rb_str_new2(tv->vval.v_string == NULL
1223 ? "" : (char *)(tv->vval.v_string));
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001224 }
1225 else if (tv->v_type == VAR_NUMBER)
1226 {
Bram Moolenaar639a2552010-03-17 18:15:23 +01001227 result = INT2NUM(tv->vval.v_number);
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001228 }
1229# ifdef FEAT_FLOAT
1230 else if (tv->v_type == VAR_FLOAT)
1231 {
Bram Moolenaar639a2552010-03-17 18:15:23 +01001232 result = rb_float_new(tv->vval.v_float);
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001233 }
1234# endif
1235 else if (tv->v_type == VAR_LIST)
1236 {
Bram Moolenaar639a2552010-03-17 18:15:23 +01001237 list_T *list = tv->vval.v_list;
1238 listitem_T *curr;
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001239
Bram Moolenaar639a2552010-03-17 18:15:23 +01001240 result = rb_ary_new();
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001241
Bram Moolenaar639a2552010-03-17 18:15:23 +01001242 if (list != NULL)
1243 {
Bram Moolenaaraeea7212020-04-02 18:50:46 +02001244 FOR_ALL_LIST_ITEMS(list, curr)
Bram Moolenaar639a2552010-03-17 18:15:23 +01001245 rb_ary_push(result, vim_to_ruby(&curr->li_tv));
Bram Moolenaar639a2552010-03-17 18:15:23 +01001246 }
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001247 }
1248 else if (tv->v_type == VAR_DICT)
1249 {
Bram Moolenaar639a2552010-03-17 18:15:23 +01001250 result = rb_hash_new();
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001251
Bram Moolenaar639a2552010-03-17 18:15:23 +01001252 if (tv->vval.v_dict != NULL)
1253 {
1254 hashtab_T *ht = &tv->vval.v_dict->dv_hashtab;
1255 long_u todo = ht->ht_used;
1256 hashitem_T *hi;
1257 dictitem_T *di;
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001258
Bram Moolenaar639a2552010-03-17 18:15:23 +01001259 for (hi = ht->ht_array; todo > 0; ++hi)
1260 {
1261 if (!HASHITEM_EMPTY(hi))
1262 {
1263 --todo;
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001264
Bram Moolenaar639a2552010-03-17 18:15:23 +01001265 di = dict_lookup(hi);
1266 rb_hash_aset(result, rb_str_new2((char *)hi->hi_key),
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001267 vim_to_ruby(&di->di_tv));
Bram Moolenaar639a2552010-03-17 18:15:23 +01001268 }
1269 }
1270 }
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001271 }
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01001272 else if (tv->v_type == VAR_BOOL || tv->v_type == VAR_SPECIAL)
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001273 {
Bram Moolenaard84b26a2018-07-28 17:18:09 +02001274 if (tv->vval.v_number == VVAL_TRUE)
1275 result = Qtrue;
1276 else if (tv->vval.v_number == VVAL_FALSE)
1277 result = Qfalse;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001278 }
1279 else if (tv->v_type == VAR_BLOB)
1280 {
1281 result = rb_str_new(tv->vval.v_blob->bv_ga.ga_data,
1282 tv->vval.v_blob->bv_ga.ga_len);
1283 }
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001284 // else return Qnil;
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001285
1286 return result;
1287}
1288#endif
1289
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001290 static VALUE
1291vim_evaluate(VALUE self UNUSED, VALUE str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001292{
1293#ifdef FEAT_EVAL
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001294 typval_T *tv;
1295 VALUE result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001296
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001297 tv = eval_expr((char_u *)StringValuePtr(str), NULL);
1298 if (tv == NULL)
Bram Moolenaar639a2552010-03-17 18:15:23 +01001299 return Qnil;
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001300 result = vim_to_ruby(tv);
1301
1302 free_tv(tv);
1303
1304 return result;
1305#else
1306 return Qnil;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001307#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001308}
1309
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001310#ifdef USE_TYPEDDATA
1311static size_t buffer_dsize(const void *buf);
1312
1313static const rb_data_type_t buffer_type = {
1314 "vim_buffer",
Bram Moolenaar41a41412020-01-07 21:32:19 +01001315 {0, 0, buffer_dsize,
1316# if RUBY_VERSION >= 27
Bram Moolenaar8b430b42020-02-22 15:01:00 +01001317 0, {0}
Bram Moolenaar41a41412020-01-07 21:32:19 +01001318# else
1319 {0, 0}
1320# endif
1321 },
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001322 0, 0,
1323# ifdef RUBY_TYPED_FREE_IMMEDIATELY
1324 0,
1325# endif
1326};
1327
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001328 static size_t
1329buffer_dsize(const void *buf UNUSED)
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001330{
1331 return sizeof(buf_T);
1332}
1333#endif
1334
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001335 static VALUE
1336buffer_new(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001337{
Bram Moolenaare344bea2005-09-01 20:46:49 +00001338 if (buf->b_ruby_ref)
1339 {
1340 return (VALUE) buf->b_ruby_ref;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001341 }
Bram Moolenaare344bea2005-09-01 20:46:49 +00001342 else
1343 {
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001344#ifdef USE_TYPEDDATA
1345 VALUE obj = TypedData_Wrap_Struct(cBuffer, &buffer_type, buf);
1346#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001347 VALUE obj = Data_Wrap_Struct(cBuffer, 0, 0, buf);
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001348#endif
Bram Moolenaare344bea2005-09-01 20:46:49 +00001349 buf->b_ruby_ref = (void *) obj;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001350 rb_hash_aset(objtbl, rb_obj_id(obj), obj);
1351 return obj;
1352 }
1353}
1354
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001355 static buf_T *
1356get_buf(VALUE obj)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001357{
1358 buf_T *buf;
1359
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001360#ifdef USE_TYPEDDATA
1361 TypedData_Get_Struct(obj, buf_T, &buffer_type, buf);
1362#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001363 Data_Get_Struct(obj, buf_T, buf);
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001364#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001365 if (buf == NULL)
1366 rb_raise(eDeletedBufferError, "attempt to refer to deleted buffer");
1367 return buf;
1368}
1369
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001370 static VALUE
1371vim_blob(VALUE self UNUSED, VALUE str)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001372{
1373 VALUE result = rb_str_new("0z", 2);
1374 char buf[4];
1375 int i;
1376 for (i = 0; i < RSTRING_LEN(str); i++)
1377 {
Bram Moolenaar0d13cce2019-02-23 14:23:03 +01001378 sprintf(buf, "%02X", (unsigned char)(RSTRING_PTR(str)[i]));
Bram Moolenaarb191be22019-01-30 21:00:12 +01001379 rb_str_concat(result, rb_str_new2(buf));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001380 }
1381 return result;
1382}
1383
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001384 static VALUE
Bram Moolenaard5a986f2020-12-06 21:11:31 +01001385buffer_s_current(VALUE self UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001386{
1387 return buffer_new(curbuf);
1388}
1389
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001390 static VALUE
Bram Moolenaard5a986f2020-12-06 21:11:31 +01001391buffer_s_current_getter(ID id UNUSED, VALUE *x UNUSED)
1392{
1393 return buffer_new(curbuf);
1394}
1395
1396 static VALUE
1397buffer_s_count(VALUE self UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001398{
1399 buf_T *b;
1400 int n = 0;
1401
Bram Moolenaar29323592016-07-24 22:04:11 +02001402 FOR_ALL_BUFFERS(b)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001403 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001404 // Deleted buffers should not be counted
1405 // SegPhault - 01/07/05
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001406 if (b->b_p_bl)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001407 n++;
1408 }
1409
Bram Moolenaar071d4272004-06-13 20:20:40 +00001410 return INT2NUM(n);
1411}
1412
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001413 static VALUE
1414buffer_s_aref(VALUE self UNUSED, VALUE num)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001415{
1416 buf_T *b;
1417 int n = NUM2INT(num);
1418
Bram Moolenaar29323592016-07-24 22:04:11 +02001419 FOR_ALL_BUFFERS(b)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001420 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001421 // Deleted buffers should not be counted
1422 // SegPhault - 01/07/05
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001423 if (!b->b_p_bl)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001424 continue;
1425
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001426 if (n == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001427 return buffer_new(b);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001428
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001429 n--;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001430 }
1431 return Qnil;
1432}
1433
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001434 static VALUE
1435buffer_name(VALUE self)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001436{
1437 buf_T *buf = get_buf(self);
1438
Bram Moolenaar35a2e192006-03-13 22:07:11 +00001439 return buf->b_ffname ? rb_str_new2((char *)buf->b_ffname) : Qnil;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001440}
1441
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001442 static VALUE
1443buffer_number(VALUE self)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001444{
1445 buf_T *buf = get_buf(self);
1446
1447 return INT2NUM(buf->b_fnum);
1448}
1449
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001450 static VALUE
1451buffer_count(VALUE self)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001452{
1453 buf_T *buf = get_buf(self);
1454
1455 return INT2NUM(buf->b_ml.ml_line_count);
1456}
1457
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001458 static VALUE
1459get_buffer_line(buf_T *buf, linenr_T n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001460{
Bram Moolenaar3c531602010-11-16 14:46:19 +01001461 if (n <= 0 || n > buf->b_ml.ml_line_count)
1462 rb_raise(rb_eIndexError, "line number %ld out of range", (long)n);
1463 return vim_str2rb_enc_str((char *)ml_get_buf(buf, n, FALSE));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001464}
1465
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001466 static VALUE
1467buffer_aref(VALUE self, VALUE num)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001468{
1469 buf_T *buf = get_buf(self);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001470
1471 if (buf != NULL)
1472 return get_buffer_line(buf, (linenr_T)NUM2LONG(num));
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001473 return Qnil; // For stop warning
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001474}
1475
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001476 static VALUE
1477set_buffer_line(buf_T *buf, linenr_T n, VALUE str)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001478{
Bram Moolenaar165641d2010-02-17 16:23:09 +01001479 char *line = StringValuePtr(str);
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001480 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001481
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001482 if (n > 0 && n <= buf->b_ml.ml_line_count && line != NULL)
1483 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001484 // set curwin/curbuf for "buf" and save some things
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001485 aucmd_prepbuf(&aco, buf);
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001486
Bram Moolenaar758535a2016-03-30 22:06:16 +02001487 if (u_savesub(n) == OK)
1488 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001489 ml_replace(n, (char_u *)line, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001490 changed();
1491#ifdef SYNTAX_HL
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001492 syn_changed(n); // recompute syntax hl. for this line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001493#endif
1494 }
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001495
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001496 // restore curwin/curbuf and a few other things
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001497 aucmd_restbuf(&aco);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001498 // Careful: autocommands may have made "buf" invalid!
Bram Moolenaarf30e74c2006-08-16 17:35:00 +00001499
Bram Moolenaar071d4272004-06-13 20:20:40 +00001500 update_curbuf(NOT_VALID);
1501 }
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001502 else
1503 {
Bram Moolenaar165641d2010-02-17 16:23:09 +01001504 rb_raise(rb_eIndexError, "line number %ld out of range", (long)n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001505 }
1506 return str;
1507}
1508
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001509 static VALUE
1510buffer_aset(VALUE self, VALUE num, VALUE str)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001511{
1512 buf_T *buf = get_buf(self);
1513
1514 if (buf != NULL)
1515 return set_buffer_line(buf, (linenr_T)NUM2LONG(num), str);
1516 return str;
1517}
1518
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001519 static VALUE
1520buffer_delete(VALUE self, VALUE num)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001521{
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001522 buf_T *buf = get_buf(self);
1523 long n = NUM2LONG(num);
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001524 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001525
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001526 if (n > 0 && n <= buf->b_ml.ml_line_count)
1527 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001528 // set curwin/curbuf for "buf" and save some things
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001529 aucmd_prepbuf(&aco, buf);
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001530
Bram Moolenaar758535a2016-03-30 22:06:16 +02001531 if (u_savedel(n, 1) == OK)
1532 {
Bram Moolenaarca70c072020-05-30 20:30:46 +02001533 ml_delete(n);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001534
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001535 // Changes to non-active buffers should properly refresh
1536 // SegPhault - 01/09/05
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001537 deleted_lines_mark(n, 1L);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001538
Bram Moolenaar071d4272004-06-13 20:20:40 +00001539 changed();
1540 }
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001541
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001542 // restore curwin/curbuf and a few other things
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001543 aucmd_restbuf(&aco);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001544 // Careful: autocommands may have made "buf" invalid!
Bram Moolenaarf30e74c2006-08-16 17:35:00 +00001545
Bram Moolenaar071d4272004-06-13 20:20:40 +00001546 update_curbuf(NOT_VALID);
1547 }
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001548 else
1549 {
Bram Moolenaar165641d2010-02-17 16:23:09 +01001550 rb_raise(rb_eIndexError, "line number %ld out of range", n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001551 }
1552 return Qnil;
1553}
1554
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001555 static VALUE
1556buffer_append(VALUE self, VALUE num, VALUE str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001557{
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001558 buf_T *buf = get_buf(self);
Bram Moolenaar165641d2010-02-17 16:23:09 +01001559 char *line = StringValuePtr(str);
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001560 long n = NUM2LONG(num);
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001561 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001562
Bram Moolenaar3c531602010-11-16 14:46:19 +01001563 if (line == NULL)
1564 {
Bram Moolenaar165641d2010-02-17 16:23:09 +01001565 rb_raise(rb_eIndexError, "NULL line");
1566 }
1567 else if (n >= 0 && n <= buf->b_ml.ml_line_count)
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001568 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001569 // set curwin/curbuf for "buf" and save some things
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001570 aucmd_prepbuf(&aco, buf);
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001571
Bram Moolenaar758535a2016-03-30 22:06:16 +02001572 if (u_inssub(n + 1) == OK)
1573 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001574 ml_append(n, (char_u *) line, (colnr_T) 0, FALSE);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001575
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001576 // Changes to non-active buffers should properly refresh screen
1577 // SegPhault - 12/20/04
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001578 appended_lines_mark(n, 1L);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001579
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001580 changed();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001581 }
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001582
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001583 // restore curwin/curbuf and a few other things
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001584 aucmd_restbuf(&aco);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001585 // Careful: autocommands may have made "buf" invalid!
Bram Moolenaarf30e74c2006-08-16 17:35:00 +00001586
Bram Moolenaar071d4272004-06-13 20:20:40 +00001587 update_curbuf(NOT_VALID);
1588 }
Bram Moolenaar3c531602010-11-16 14:46:19 +01001589 else
1590 {
Bram Moolenaar165641d2010-02-17 16:23:09 +01001591 rb_raise(rb_eIndexError, "line number %ld out of range", n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001592 }
1593 return str;
1594}
1595
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001596#ifdef USE_TYPEDDATA
1597static size_t window_dsize(const void *buf);
1598
1599static const rb_data_type_t window_type = {
1600 "vim_window",
Bram Moolenaar41a41412020-01-07 21:32:19 +01001601 {0, 0, window_dsize,
1602# if RUBY_VERSION >= 27
Bram Moolenaar8b430b42020-02-22 15:01:00 +01001603 0, {0}
Bram Moolenaar41a41412020-01-07 21:32:19 +01001604# else
1605 {0, 0}
1606# endif
1607 },
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001608 0, 0,
1609# ifdef RUBY_TYPED_FREE_IMMEDIATELY
1610 0,
1611# endif
1612};
1613
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001614 static size_t
1615window_dsize(const void *win UNUSED)
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001616{
1617 return sizeof(win_T);
1618}
1619#endif
1620
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001621 static VALUE
1622window_new(win_T *win)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001623{
Bram Moolenaare344bea2005-09-01 20:46:49 +00001624 if (win->w_ruby_ref)
1625 {
1626 return (VALUE) win->w_ruby_ref;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001627 }
Bram Moolenaare344bea2005-09-01 20:46:49 +00001628 else
1629 {
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001630#ifdef USE_TYPEDDATA
1631 VALUE obj = TypedData_Wrap_Struct(cVimWindow, &window_type, win);
1632#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001633 VALUE obj = Data_Wrap_Struct(cVimWindow, 0, 0, win);
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001634#endif
Bram Moolenaare344bea2005-09-01 20:46:49 +00001635 win->w_ruby_ref = (void *) obj;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001636 rb_hash_aset(objtbl, rb_obj_id(obj), obj);
1637 return obj;
1638 }
1639}
1640
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001641 static win_T *
1642get_win(VALUE obj)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001643{
1644 win_T *win;
1645
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001646#ifdef USE_TYPEDDATA
1647 TypedData_Get_Struct(obj, win_T, &window_type, win);
1648#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001649 Data_Get_Struct(obj, win_T, win);
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001650#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001651 if (win == NULL)
1652 rb_raise(eDeletedWindowError, "attempt to refer to deleted window");
1653 return win;
1654}
1655
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001656 static VALUE
Bram Moolenaard5a986f2020-12-06 21:11:31 +01001657window_s_current(VALUE self UNUSED)
1658{
1659 return window_new(curwin);
1660}
1661
1662 static VALUE
1663window_s_current_getter(ID id UNUSED, VALUE *x UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001664{
1665 return window_new(curwin);
1666}
1667
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001668/*
1669 * Added line manipulation functions
1670 * SegPhault - 03/07/05
1671 */
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001672 static VALUE
Bram Moolenaard5a986f2020-12-06 21:11:31 +01001673line_s_current(VALUE self UNUSED)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001674{
1675 return get_buffer_line(curbuf, curwin->w_cursor.lnum);
1676}
1677
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001678 static VALUE
1679set_current_line(VALUE self UNUSED, VALUE str)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001680{
1681 return set_buffer_line(curbuf, curwin->w_cursor.lnum, str);
1682}
1683
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001684 static VALUE
Bram Moolenaard5a986f2020-12-06 21:11:31 +01001685current_line_number(VALUE self UNUSED)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001686{
1687 return INT2FIX((int)curwin->w_cursor.lnum);
1688}
1689
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001690 static VALUE
Bram Moolenaard5a986f2020-12-06 21:11:31 +01001691window_s_count(VALUE self UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001692{
Bram Moolenaar071d4272004-06-13 20:20:40 +00001693 win_T *w;
1694 int n = 0;
1695
Bram Moolenaar29323592016-07-24 22:04:11 +02001696 FOR_ALL_WINDOWS(w)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001697 n++;
1698 return INT2NUM(n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001699}
1700
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001701 static VALUE
1702window_s_aref(VALUE self UNUSED, VALUE num)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001703{
1704 win_T *w;
1705 int n = NUM2INT(num);
1706
Bram Moolenaar071d4272004-06-13 20:20:40 +00001707 for (w = firstwin; w != NULL; w = w->w_next, --n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001708 if (n == 0)
1709 return window_new(w);
1710 return Qnil;
1711}
1712
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001713 static VALUE
1714window_buffer(VALUE self)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001715{
1716 win_T *win = get_win(self);
1717
1718 return buffer_new(win->w_buffer);
1719}
1720
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001721 static VALUE
1722window_height(VALUE self)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001723{
1724 win_T *win = get_win(self);
1725
1726 return INT2NUM(win->w_height);
1727}
1728
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001729 static VALUE
1730window_set_height(VALUE self, VALUE height)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001731{
1732 win_T *win = get_win(self);
1733 win_T *savewin = curwin;
1734
1735 curwin = win;
1736 win_setheight(NUM2INT(height));
1737 curwin = savewin;
1738 return height;
1739}
1740
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001741 static VALUE
1742window_width(VALUE self UNUSED)
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001743{
Bram Moolenaare745d752017-09-22 16:56:22 +02001744 return INT2NUM(get_win(self)->w_width);
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001745}
1746
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001747 static VALUE
1748window_set_width(VALUE self UNUSED, VALUE width)
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001749{
1750 win_T *win = get_win(self);
1751 win_T *savewin = curwin;
1752
1753 curwin = win;
1754 win_setwidth(NUM2INT(width));
1755 curwin = savewin;
1756 return width;
1757}
1758
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001759 static VALUE
1760window_cursor(VALUE self)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001761{
1762 win_T *win = get_win(self);
1763
1764 return rb_assoc_new(INT2NUM(win->w_cursor.lnum), INT2NUM(win->w_cursor.col));
1765}
1766
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001767 static VALUE
1768window_set_cursor(VALUE self, VALUE pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001769{
1770 VALUE lnum, col;
1771 win_T *win = get_win(self);
1772
1773 Check_Type(pos, T_ARRAY);
Bram Moolenaar165641d2010-02-17 16:23:09 +01001774 if (RARRAY_LEN(pos) != 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001775 rb_raise(rb_eArgError, "array length must be 2");
Bram Moolenaar165641d2010-02-17 16:23:09 +01001776 lnum = RARRAY_PTR(pos)[0];
1777 col = RARRAY_PTR(pos)[1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001778 win->w_cursor.lnum = NUM2LONG(lnum);
1779 win->w_cursor.col = NUM2UINT(col);
Bram Moolenaar53901442018-07-25 22:02:36 +02001780 win->w_set_curswant = TRUE;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001781 check_cursor(); // put cursor on an existing line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001782 update_screen(NOT_VALID);
1783 return Qnil;
1784}
1785
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001786 static VALUE
1787f_nop(VALUE self UNUSED)
Bram Moolenaar35df7d22012-04-20 18:05:47 +02001788{
1789 return Qnil;
1790}
1791
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001792 static VALUE
1793f_p(int argc, VALUE *argv, VALUE self UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001794{
1795 int i;
1796 VALUE str = rb_str_new("", 0);
Bram Moolenaar51e9fbf2018-08-11 14:24:11 +02001797 VALUE ret = Qnil;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001798
Bram Moolenaar758535a2016-03-30 22:06:16 +02001799 for (i = 0; i < argc; i++)
1800 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001801 if (i > 0) rb_str_cat(str, ", ", 2);
1802 rb_str_concat(str, rb_inspect(argv[i]));
1803 }
Bram Moolenaar32526b32019-01-19 17:43:09 +01001804 msg(RSTRING_PTR(str));
Bram Moolenaar51e9fbf2018-08-11 14:24:11 +02001805
1806 if (argc == 1)
1807 ret = argv[0];
1808 else if (argc > 1)
1809 ret = rb_ary_new4(argc, argv);
1810 return ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001811}
1812
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001813 static void
1814ruby_io_init(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001815{
1816#ifndef DYNAMIC_RUBY
1817 RUBYEXTERN VALUE rb_stdout;
Bram Moolenaarb6c8cd82018-07-24 05:41:30 +02001818 RUBYEXTERN VALUE rb_stderr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001819#endif
1820
1821 rb_stdout = rb_obj_alloc(rb_cObject);
Bram Moolenaarb6c8cd82018-07-24 05:41:30 +02001822 rb_stderr = rb_obj_alloc(rb_cObject);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001823 rb_define_singleton_method(rb_stdout, "write", vim_message, 1);
Bram Moolenaar35df7d22012-04-20 18:05:47 +02001824 rb_define_singleton_method(rb_stdout, "flush", f_nop, 0);
Bram Moolenaarb6c8cd82018-07-24 05:41:30 +02001825 rb_define_singleton_method(rb_stderr, "write", vim_message, 1);
1826 rb_define_singleton_method(rb_stderr, "flush", f_nop, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001827 rb_define_global_function("p", f_p, -1);
1828}
1829
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001830 static void
1831ruby_vim_init(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001832{
1833 objtbl = rb_hash_new();
1834 rb_global_variable(&objtbl);
1835
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001836 // The Vim module used to be called "VIM", but "Vim" is better. Make an
1837 // alias "VIM" for backwards compatibility.
Bram Moolenaarf711faf2007-05-10 16:48:19 +00001838 mVIM = rb_define_module("Vim");
1839 rb_define_const(rb_cObject, "VIM", mVIM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001840 rb_define_const(mVIM, "VERSION_MAJOR", INT2NUM(VIM_VERSION_MAJOR));
1841 rb_define_const(mVIM, "VERSION_MINOR", INT2NUM(VIM_VERSION_MINOR));
1842 rb_define_const(mVIM, "VERSION_BUILD", INT2NUM(VIM_VERSION_BUILD));
1843 rb_define_const(mVIM, "VERSION_PATCHLEVEL", INT2NUM(VIM_VERSION_PATCHLEVEL));
1844 rb_define_const(mVIM, "VERSION_SHORT", rb_str_new2(VIM_VERSION_SHORT));
1845 rb_define_const(mVIM, "VERSION_MEDIUM", rb_str_new2(VIM_VERSION_MEDIUM));
1846 rb_define_const(mVIM, "VERSION_LONG", rb_str_new2(VIM_VERSION_LONG));
1847 rb_define_const(mVIM, "VERSION_LONG_DATE", rb_str_new2(VIM_VERSION_LONG_DATE));
1848 rb_define_module_function(mVIM, "message", vim_message, 1);
1849 rb_define_module_function(mVIM, "set_option", vim_set_option, 1);
1850 rb_define_module_function(mVIM, "command", vim_command, 1);
1851 rb_define_module_function(mVIM, "evaluate", vim_evaluate, 1);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001852 rb_define_module_function(mVIM, "blob", vim_blob, 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001853
1854 eDeletedBufferError = rb_define_class_under(mVIM, "DeletedBufferError",
1855 rb_eStandardError);
1856 eDeletedWindowError = rb_define_class_under(mVIM, "DeletedWindowError",
1857 rb_eStandardError);
1858
1859 cBuffer = rb_define_class_under(mVIM, "Buffer", rb_cObject);
1860 rb_define_singleton_method(cBuffer, "current", buffer_s_current, 0);
1861 rb_define_singleton_method(cBuffer, "count", buffer_s_count, 0);
1862 rb_define_singleton_method(cBuffer, "[]", buffer_s_aref, 1);
1863 rb_define_method(cBuffer, "name", buffer_name, 0);
1864 rb_define_method(cBuffer, "number", buffer_number, 0);
1865 rb_define_method(cBuffer, "count", buffer_count, 0);
1866 rb_define_method(cBuffer, "length", buffer_count, 0);
1867 rb_define_method(cBuffer, "[]", buffer_aref, 1);
1868 rb_define_method(cBuffer, "[]=", buffer_aset, 2);
1869 rb_define_method(cBuffer, "delete", buffer_delete, 1);
1870 rb_define_method(cBuffer, "append", buffer_append, 2);
1871
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001872 // Added line manipulation functions
1873 // SegPhault - 03/07/05
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001874 rb_define_method(cBuffer, "line_number", current_line_number, 0);
1875 rb_define_method(cBuffer, "line", line_s_current, 0);
1876 rb_define_method(cBuffer, "line=", set_current_line, 1);
1877
1878
Bram Moolenaar071d4272004-06-13 20:20:40 +00001879 cVimWindow = rb_define_class_under(mVIM, "Window", rb_cObject);
1880 rb_define_singleton_method(cVimWindow, "current", window_s_current, 0);
1881 rb_define_singleton_method(cVimWindow, "count", window_s_count, 0);
1882 rb_define_singleton_method(cVimWindow, "[]", window_s_aref, 1);
1883 rb_define_method(cVimWindow, "buffer", window_buffer, 0);
1884 rb_define_method(cVimWindow, "height", window_height, 0);
1885 rb_define_method(cVimWindow, "height=", window_set_height, 1);
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001886 rb_define_method(cVimWindow, "width", window_width, 0);
1887 rb_define_method(cVimWindow, "width=", window_set_width, 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001888 rb_define_method(cVimWindow, "cursor", window_cursor, 0);
1889 rb_define_method(cVimWindow, "cursor=", window_set_cursor, 1);
1890
Bram Moolenaard5a986f2020-12-06 21:11:31 +01001891 rb_define_virtual_variable("$curbuf", buffer_s_current_getter, 0);
1892 rb_define_virtual_variable("$curwin", window_s_current_getter, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001893}
Bram Moolenaar99685e62013-05-11 13:56:18 +02001894
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001895 void
1896vim_ruby_init(void *stack_start)
Bram Moolenaar99685e62013-05-11 13:56:18 +02001897{
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001898 // should get machine stack start address early in main function
Bram Moolenaar99685e62013-05-11 13:56:18 +02001899 ruby_stack_start = stack_start;
1900}
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001901
1902 static int
1903convert_hash2dict(VALUE key, VALUE val, VALUE arg)
1904{
1905 dict_T *d = (dict_T *)arg;
1906 dictitem_T *di;
1907
Bram Moolenaardace9f72020-12-28 15:07:45 +01001908 di = dictitem_alloc((char_u *)RSTRING_PTR(rb_obj_as_string(key)));
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001909 if (di == NULL || ruby_convert_to_vim_value(val, &di->di_tv) != OK
1910 || dict_add(d, di) != OK)
1911 {
1912 d->dv_hashtab.ht_error = TRUE;
1913 return ST_STOP;
1914 }
1915 return ST_CONTINUE;
1916}
1917
1918 static int
1919ruby_convert_to_vim_value(VALUE val, typval_T *rettv)
1920{
1921 switch (TYPE(val))
1922 {
1923 case T_NIL:
1924 rettv->v_type = VAR_SPECIAL;
1925 rettv->vval.v_number = VVAL_NULL;
1926 break;
1927 case T_TRUE:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01001928 rettv->v_type = VAR_BOOL;
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001929 rettv->vval.v_number = VVAL_TRUE;
1930 break;
1931 case T_FALSE:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01001932 rettv->v_type = VAR_BOOL;
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001933 rettv->vval.v_number = VVAL_FALSE;
1934 break;
1935 case T_BIGNUM:
1936 case T_FIXNUM:
1937 rettv->v_type = VAR_NUMBER;
1938 rettv->vval.v_number = (varnumber_T)NUM2LONG(val);
1939 break;
1940#ifdef FEAT_FLOAT
1941 case T_FLOAT:
1942 rettv->v_type = VAR_FLOAT;
1943 rettv->vval.v_float = (float_T)NUM2DBL(val);
1944 break;
1945#endif
1946 default:
1947 val = rb_obj_as_string(val);
1948 // FALLTHROUGH
1949 case T_STRING:
1950 {
1951 VALUE str = (VALUE)RSTRING(val);
1952
1953 rettv->v_type = VAR_STRING;
1954 rettv->vval.v_string = vim_strnsave((char_u *)RSTRING_PTR(str),
Bram Moolenaar71ccd032020-06-12 22:59:11 +02001955 RSTRING_LEN(str));
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001956 }
1957 break;
1958 case T_ARRAY:
1959 {
1960 list_T *l;
1961 long i;
1962 typval_T v;
1963
1964 l = list_alloc();
1965 if (l == NULL)
1966 return FAIL;
1967
1968 for (i = 0; i < RARRAY_LEN(val); ++i)
1969 {
1970 if (ruby_convert_to_vim_value((VALUE)RARRAY_PTR(val)[i],
1971 &v) != OK)
1972 {
1973 list_unref(l);
1974 return FAIL;
1975 }
1976 list_append_tv(l, &v);
1977 clear_tv(&v);
1978 }
1979
1980 rettv->v_type = VAR_LIST;
1981 rettv->vval.v_list = l;
1982 ++l->lv_refcount;
1983 }
1984 break;
1985 case T_HASH:
1986 {
1987 dict_T *d;
1988
1989 d = dict_alloc();
1990 if (d == NULL)
1991 return FAIL;
1992
1993 rb_hash_foreach(val, convert_hash2dict, (VALUE)d);
1994 if (d->dv_hashtab.ht_error)
1995 {
1996 dict_unref(d);
1997 return FAIL;
1998 }
1999
2000 rettv->v_type = VAR_DICT;
2001 rettv->vval.v_dict = d;
2002 ++d->dv_refcount;
2003 }
2004 break;
2005 }
2006 return OK;
2007}
2008
2009 void
2010do_rubyeval(char_u *str, typval_T *rettv)
2011{
2012 int retval = FAIL;
2013
2014 if (ensure_ruby_initialized())
2015 {
2016 int state;
2017 VALUE obj;
2018
2019 obj = rb_eval_string_protect((const char *)str, &state);
2020 if (state)
2021 error_print(state);
2022 else
2023 retval = ruby_convert_to_vim_value(obj, rettv);
2024 }
2025 if (retval == FAIL)
2026 {
2027 rettv->v_type = VAR_NUMBER;
2028 rettv->vval.v_number = 0;
2029 }
2030}