blob: fcbbf336e77a7a80c59a94f5f66a352b03462dd9 [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
505static void (*dll_rb_unexpected_type) (VALUE, int) ATTRIBUTE_NORETURN;
506# endif
Bram Moolenaar41a41412020-01-07 21:32:19 +0100507# if RUBY_VERSION >= 18
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100508static char * (*dll_rb_string_value_ptr) (volatile VALUE*);
509static VALUE (*dll_rb_float_new) (double);
510static VALUE (*dll_rb_ary_new) (void);
Bram Moolenaar51e9fbf2018-08-11 14:24:11 +0200511static VALUE (*dll_rb_ary_new4) (long n, const VALUE *elts);
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100512static VALUE (*dll_rb_ary_push) (VALUE, VALUE);
Bram Moolenaar41a41412020-01-07 21:32:19 +0100513# if RUBY_VERSION >= 26
Bram Moolenaarb09c6842018-12-27 22:11:01 +0100514static void (*dll_rb_ary_detransient) (VALUE);
515# endif
Bram Moolenaar41a41412020-01-07 21:32:19 +0100516# if (RUBY_VERSION >= 19) || defined(RUBY_INIT_STACK)
Bram Moolenaar10f3a792013-05-20 12:52:29 +0200517# ifdef __ia64
Bram Moolenaar76a86062013-05-11 17:45:48 +0200518static void * (*dll_rb_ia64_bsp) (void);
519static void (*dll_ruby_init_stack)(VALUE*, void*);
Bram Moolenaar10f3a792013-05-20 12:52:29 +0200520# else
Bram Moolenaar76a86062013-05-11 17:45:48 +0200521static void (*dll_ruby_init_stack)(VALUE*);
Bram Moolenaar10f3a792013-05-20 12:52:29 +0200522# endif
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200523# endif
Bram Moolenaar76a86062013-05-11 17:45:48 +0200524# endif
Bram Moolenaar41a41412020-01-07 21:32:19 +0100525# if RUBY_VERSION >= 19
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100526static VALUE (*dll_rb_int2big)(SIGNED_VALUE);
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200527# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000528
Bram Moolenaar41a41412020-01-07 21:32:19 +0100529# if RUBY_VERSION >= 19
Bram Moolenaar165641d2010-02-17 16:23:09 +0100530static void (*dll_ruby_script) (const char*);
531static int (*dll_rb_enc_find_index) (const char*);
532static rb_encoding* (*dll_rb_enc_find) (const char*);
533static VALUE (*dll_rb_enc_str_new) (const char*, long, rb_encoding*);
534static VALUE (*dll_rb_sprintf) (const char*, ...);
Bram Moolenaar7a8ef142010-12-24 13:39:35 +0100535static VALUE (*dll_rb_require) (const char*);
Bram Moolenaardace9f72020-12-28 15:07:45 +0100536static void* (*dll_ruby_options)(int, char**);
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200537# endif
Bram Moolenaar165641d2010-02-17 16:23:09 +0100538
Bram Moolenaara1a118b2014-02-05 22:41:15 +0100539# if defined(USE_RGENGC) && USE_RGENGC
Bram Moolenaar41a41412020-01-07 21:32:19 +0100540# if RUBY_VERSION == 21
Bram Moolenaara1a118b2014-02-05 22:41:15 +0100541static void (*dll_rb_gc_writebarrier_unprotect_promoted)(VALUE);
Bram Moolenaar0c7485f2015-01-14 14:04:10 +0100542# else
543static void (*dll_rb_gc_writebarrier_unprotect)(VALUE obj);
544# endif
Bram Moolenaara1a118b2014-02-05 22:41:15 +0100545# endif
546
Bram Moolenaardace9f72020-12-28 15:07:45 +0100547# if RUBY_VERSION >= 30
Bram Moolenaar9d20daf2021-02-01 19:31:47 +0100548# ifdef _MSC_VER
549static void (*dll_ruby_malloc_size_overflow)(size_t, size_t);
550# else
Bram Moolenaardace9f72020-12-28 15:07:45 +0100551NORETURN(static void (*dll_ruby_malloc_size_overflow)(size_t, size_t));
Bram Moolenaar9d20daf2021-02-01 19:31:47 +0100552# endif
Bram Moolenaardace9f72020-12-28 15:07:45 +0100553# endif
554
Bram Moolenaar0e121402020-12-10 20:50:34 +0100555# if RUBY_VERSION >= 26
556void rb_ary_detransient_stub(VALUE x);
557# endif
558
Bram Moolenaardace9f72020-12-28 15:07:45 +0100559// Do not generate a prototype here, VALUE isn't always defined.
560# ifndef PROTO
561# if RUBY_VERSION >= 19
562# if RUBY_VERSION >= 22
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100563 long
564rb_num2long_stub(VALUE x)
Bram Moolenaardace9f72020-12-28 15:07:45 +0100565# else
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100566 SIGNED_VALUE
567rb_num2long_stub(VALUE x)
Bram Moolenaardace9f72020-12-28 15:07:45 +0100568# endif
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100569{
570 return dll_rb_num2long(x);
571}
Bram Moolenaardace9f72020-12-28 15:07:45 +0100572# if RUBY_VERSION >= 26
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100573 VALUE
574rb_int2big_stub(intptr_t x)
Bram Moolenaardace9f72020-12-28 15:07:45 +0100575# else
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100576 VALUE
577rb_int2big_stub(SIGNED_VALUE x)
Bram Moolenaardace9f72020-12-28 15:07:45 +0100578# endif
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100579{
580 return dll_rb_int2big(x);
581}
Bram Moolenaar9d20daf2021-02-01 19:31:47 +0100582# if RUBY_VERSION >= 30 || VIM_SIZEOF_INT < VIM_SIZEOF_LONG
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100583 long
584rb_fix2int_stub(VALUE x)
Bram Moolenaar0bcdd6e2013-04-14 16:19:03 +0200585{
586 return dll_rb_fix2int(x);
587}
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100588 long
589rb_num2int_stub(VALUE x)
Bram Moolenaar0bcdd6e2013-04-14 16:19:03 +0200590{
591 return dll_rb_num2int(x);
592}
Bram Moolenaardace9f72020-12-28 15:07:45 +0100593# endif
594# if RUBY_VERSION >= 20
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100595 VALUE
Bram Moolenaar886ed692013-02-26 13:41:35 +0100596rb_float_new_in_heap(double d)
597{
598 return dll_rb_float_new(d);
599}
Bram Moolenaardace9f72020-12-28 15:07:45 +0100600# if RUBY_VERSION >= 22
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100601 unsigned long
602rb_num2ulong(VALUE x)
Bram Moolenaardace9f72020-12-28 15:07:45 +0100603# else
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100604 VALUE
605rb_num2ulong(VALUE x)
Bram Moolenaardace9f72020-12-28 15:07:45 +0100606# endif
Bram Moolenaar886ed692013-02-26 13:41:35 +0100607{
608 return (long)RSHIFT((SIGNED_VALUE)(x),1);
609}
Bram Moolenaardace9f72020-12-28 15:07:45 +0100610# endif
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200611# endif
Bram Moolenaardace9f72020-12-28 15:07:45 +0100612# if defined(USE_RGENGC) && USE_RGENGC
613# if RUBY_VERSION == 21
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100614 void
615rb_gc_writebarrier_unprotect_promoted_stub(VALUE obj)
Bram Moolenaara1a118b2014-02-05 22:41:15 +0100616{
Bram Moolenaar90140742014-11-27 17:44:08 +0100617 dll_rb_gc_writebarrier_unprotect_promoted(obj);
Bram Moolenaara1a118b2014-02-05 22:41:15 +0100618}
Bram Moolenaardace9f72020-12-28 15:07:45 +0100619# else
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100620 void
621rb_gc_writebarrier_unprotect_stub(VALUE obj)
Bram Moolenaar0c7485f2015-01-14 14:04:10 +0100622{
623 dll_rb_gc_writebarrier_unprotect(obj);
624}
Bram Moolenaardace9f72020-12-28 15:07:45 +0100625# endif
Bram Moolenaar0c7485f2015-01-14 14:04:10 +0100626# endif
Bram Moolenaardace9f72020-12-28 15:07:45 +0100627# if RUBY_VERSION >= 26
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100628 void
629rb_ary_detransient_stub(VALUE x)
Bram Moolenaarf62fc312019-01-08 20:29:32 +0100630{
631 dll_rb_ary_detransient(x);
632}
Bram Moolenaardace9f72020-12-28 15:07:45 +0100633# endif
634# if RUBY_VERSION >= 30
635 void
636rb_check_type_stub(VALUE obj, int t)
637{
638 dll_rb_check_type(obj, t);
639}
640 unsigned long
641rb_num2uint_stub(VALUE x)
642{
643 return dll_rb_num2uint(x);
644}
645 void
646ruby_malloc_size_overflow_stub(size_t x, size_t y)
647{
648 dll_ruby_malloc_size_overflow(x, y);
649}
650# endif
ichizok8bb3fe42021-12-28 15:51:45 +0000651# if RUBY_VERSION >= 31
652 void
653rb_debug_rstring_null_ptr_stub(const char *func)
654{
655 dll_rb_debug_rstring_null_ptr(func);
656}
657 void
658rb_unexpected_type_stub(VALUE self, int t)
659{
660 dll_rb_unexpected_type(self, t);
661}
662# endif
Bram Moolenaardace9f72020-12-28 15:07:45 +0100663# endif // ifndef PROTO
Bram Moolenaarf62fc312019-01-08 20:29:32 +0100664
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100665static HINSTANCE hinstRuby = NULL; // Instance of ruby.dll
Bram Moolenaar071d4272004-06-13 20:20:40 +0000666
667/*
Bram Moolenaarda2303d2005-08-30 21:55:26 +0000668 * Table of name to function pointer of ruby.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000669 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000670static struct
671{
672 char *name;
673 RUBY_PROC *ptr;
674} ruby_funcname_table[] =
675{
676 {"rb_assoc_new", (RUBY_PROC*)&dll_rb_assoc_new},
677 {"rb_cFalseClass", (RUBY_PROC*)&dll_rb_cFalseClass},
Bram Moolenaar2016ae52016-06-13 20:08:43 +0200678# if defined(USE_RUBY_INTEGER)
Bram Moolenaar06469e92016-06-11 22:26:53 +0200679 {"rb_cInteger", (RUBY_PROC*)&dll_rb_cInteger},
Bram Moolenaar6abda992017-01-09 21:10:31 +0100680# else
681 {"rb_cFixnum", (RUBY_PROC*)&dll_rb_cFixnum},
Bram Moolenaar06469e92016-06-11 22:26:53 +0200682# endif
Bram Moolenaar41a41412020-01-07 21:32:19 +0100683# if RUBY_VERSION >= 20
Bram Moolenaardb3fbe52013-03-07 15:16:21 +0100684 {"rb_cFloat", (RUBY_PROC*)&dll_rb_cFloat},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200685# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000686 {"rb_cNilClass", (RUBY_PROC*)&dll_rb_cNilClass},
687 {"rb_cObject", (RUBY_PROC*)&dll_rb_cObject},
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100688 {"rb_cString", (RUBY_PROC*)&dll_rb_cString},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000689 {"rb_cSymbol", (RUBY_PROC*)&dll_rb_cSymbol},
690 {"rb_cTrueClass", (RUBY_PROC*)&dll_rb_cTrueClass},
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100691 {"rb_class_new_instance", (RUBY_PROC*)&dll_rb_class_new_instance},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000692 {"rb_check_type", (RUBY_PROC*)&dll_rb_check_type},
Bram Moolenaarf2f6d292015-12-28 20:57:10 +0100693# ifdef USE_TYPEDDATA
694 {"rb_check_typeddata", (RUBY_PROC*)&dll_rb_check_typeddata},
695# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000696 {"rb_class_path", (RUBY_PROC*)&dll_rb_class_path},
Bram Moolenaarf2f6d292015-12-28 20:57:10 +0100697# ifdef USE_TYPEDDATA
Bram Moolenaar41a41412020-01-07 21:32:19 +0100698# if RUBY_VERSION >= 23
Bram Moolenaarf2f6d292015-12-28 20:57:10 +0100699 {"rb_data_typed_object_wrap", (RUBY_PROC*)&dll_rb_data_typed_object_wrap},
700# else
701 {"rb_data_typed_object_alloc", (RUBY_PROC*)&dll_rb_data_typed_object_alloc},
702# endif
703# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000704 {"rb_data_object_alloc", (RUBY_PROC*)&dll_rb_data_object_alloc},
Bram Moolenaarf2f6d292015-12-28 20:57:10 +0100705# endif
ichizok8bb3fe42021-12-28 15:51:45 +0000706# if RUBY_VERSION >= 31
707 {"rb_debug_rstring_null_ptr", (RUBY_PROC*)&dll_rb_debug_rstring_null_ptr},
708# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000709 {"rb_define_class_under", (RUBY_PROC*)&dll_rb_define_class_under},
710 {"rb_define_const", (RUBY_PROC*)&dll_rb_define_const},
711 {"rb_define_global_function", (RUBY_PROC*)&dll_rb_define_global_function},
712 {"rb_define_method", (RUBY_PROC*)&dll_rb_define_method},
713 {"rb_define_module", (RUBY_PROC*)&dll_rb_define_module},
714 {"rb_define_module_function", (RUBY_PROC*)&dll_rb_define_module_function},
715 {"rb_define_singleton_method", (RUBY_PROC*)&dll_rb_define_singleton_method},
716 {"rb_define_virtual_variable", (RUBY_PROC*)&dll_rb_define_virtual_variable},
717 {"rb_stdout", (RUBY_PROC*)&dll_rb_stdout},
Bram Moolenaarb6c8cd82018-07-24 05:41:30 +0200718 {"rb_stderr", (RUBY_PROC*)&dll_rb_stderr},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000719 {"rb_eArgError", (RUBY_PROC*)&dll_rb_eArgError},
720 {"rb_eIndexError", (RUBY_PROC*)&dll_rb_eIndexError},
721 {"rb_eRuntimeError", (RUBY_PROC*)&dll_rb_eRuntimeError},
722 {"rb_eStandardError", (RUBY_PROC*)&dll_rb_eStandardError},
723 {"rb_eval_string_protect", (RUBY_PROC*)&dll_rb_eval_string_protect},
Bram Moolenaar41a41412020-01-07 21:32:19 +0100724# if RUBY_VERSION >= 21
Bram Moolenaarf711cb22018-08-01 18:42:13 +0200725 {"rb_funcallv", (RUBY_PROC*)&dll_rb_funcallv},
726# else
727 {"rb_funcall2", (RUBY_PROC*)&dll_rb_funcall2},
728# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000729 {"rb_global_variable", (RUBY_PROC*)&dll_rb_global_variable},
730 {"rb_hash_aset", (RUBY_PROC*)&dll_rb_hash_aset},
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100731 {"rb_hash_foreach", (RUBY_PROC*)&dll_rb_hash_foreach},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000732 {"rb_hash_new", (RUBY_PROC*)&dll_rb_hash_new},
733 {"rb_inspect", (RUBY_PROC*)&dll_rb_inspect},
734 {"rb_int2inum", (RUBY_PROC*)&dll_rb_int2inum},
Bram Moolenaarf711cb22018-08-01 18:42:13 +0200735 {"rb_intern", (RUBY_PROC*)&dll_rb_intern},
Bram Moolenaar9d20daf2021-02-01 19:31:47 +0100736# if RUBY_VERSION >= 30 || VIM_SIZEOF_INT < VIM_SIZEOF_LONG
Bram Moolenaar2623b4f2012-09-18 16:36:32 +0200737 {"rb_fix2int", (RUBY_PROC*)&dll_rb_fix2int},
738 {"rb_num2int", (RUBY_PROC*)&dll_rb_num2int},
739 {"rb_num2uint", (RUBY_PROC*)&dll_rb_num2uint},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200740# endif
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100741 {"rb_num2dbl", (RUBY_PROC*)&dll_rb_num2dbl},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000742 {"rb_lastline_get", (RUBY_PROC*)&dll_rb_lastline_get},
743 {"rb_lastline_set", (RUBY_PROC*)&dll_rb_lastline_set},
Bram Moolenaard0573012017-10-28 21:11:06 +0200744 {"rb_protect", (RUBY_PROC*)&dll_rb_protect},
745 {"rb_load", (RUBY_PROC*)&dll_rb_load},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000746 {"rb_num2long", (RUBY_PROC*)&dll_rb_num2long},
747 {"rb_num2ulong", (RUBY_PROC*)&dll_rb_num2ulong},
748 {"rb_obj_alloc", (RUBY_PROC*)&dll_rb_obj_alloc},
749 {"rb_obj_as_string", (RUBY_PROC*)&dll_rb_obj_as_string},
750 {"rb_obj_id", (RUBY_PROC*)&dll_rb_obj_id},
751 {"rb_raise", (RUBY_PROC*)&dll_rb_raise},
Bram Moolenaar41a41412020-01-07 21:32:19 +0100752# if RUBY_VERSION >= 18
Bram Moolenaarf9b5ef82010-09-29 13:02:53 +0200753 {"rb_string_value", (RUBY_PROC*)&dll_rb_string_value},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200754# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000755 {"rb_str2cstr", (RUBY_PROC*)&dll_rb_str2cstr},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200756# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000757 {"rb_str_cat", (RUBY_PROC*)&dll_rb_str_cat},
758 {"rb_str_concat", (RUBY_PROC*)&dll_rb_str_concat},
759 {"rb_str_new", (RUBY_PROC*)&dll_rb_str_new},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200760# ifdef need_rb_str_new_cstr
Bram Moolenaar1d2beae2010-05-22 21:56:55 +0200761 {"rb_str_new_cstr", (RUBY_PROC*)&dll_rb_str_new_cstr},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200762# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000763 {"rb_str_new2", (RUBY_PROC*)&dll_rb_str_new2},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200764# endif
Bram Moolenaar41a41412020-01-07 21:32:19 +0100765# if RUBY_VERSION >= 19
Bram Moolenaar165641d2010-02-17 16:23:09 +0100766 {"rb_errinfo", (RUBY_PROC*)&dll_rb_errinfo},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200767# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000768 {"ruby_errinfo", (RUBY_PROC*)&dll_ruby_errinfo},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200769# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000770 {"ruby_init", (RUBY_PROC*)&dll_ruby_init},
771 {"ruby_init_loadpath", (RUBY_PROC*)&dll_ruby_init_loadpath},
Bram Moolenaar4f974752019-02-17 17:44:42 +0100772# ifdef MSWIN
Bram Moolenaar41a41412020-01-07 21:32:19 +0100773# if RUBY_VERSION >= 19
Bram Moolenaar4f391792017-01-15 16:59:07 +0100774 {"ruby_sysinit", (RUBY_PROC*)&dll_ruby_sysinit},
Bram Moolenaar9d5c84a2018-12-19 20:48:46 +0100775# else
776 {"NtInitialize", (RUBY_PROC*)&dll_NtInitialize},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200777# endif
Bram Moolenaar41a41412020-01-07 21:32:19 +0100778# if RUBY_VERSION >= 18
Bram Moolenaar071d4272004-06-13 20:20:40 +0000779 {"rb_w32_snprintf", (RUBY_PROC*)&dll_rb_w32_snprintf},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200780# endif
Bram Moolenaar3ca71f12010-10-27 16:49:47 +0200781# endif
ichizok8bb3fe42021-12-28 15:51:45 +0000782# if RUBY_VERSION >= 31
783 {"rb_unexpected_type", (RUBY_PROC*)&dll_rb_unexpected_type},
784# endif
Bram Moolenaar41a41412020-01-07 21:32:19 +0100785# if RUBY_VERSION >= 18
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100786 {"rb_string_value_ptr", (RUBY_PROC*)&dll_rb_string_value_ptr},
Bram Moolenaar41a41412020-01-07 21:32:19 +0100787# if RUBY_VERSION <= 19
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100788 {"rb_float_new", (RUBY_PROC*)&dll_rb_float_new},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200789# else
Bram Moolenaar886ed692013-02-26 13:41:35 +0100790 {"rb_float_new_in_heap", (RUBY_PROC*)&dll_rb_float_new},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200791# endif
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100792 {"rb_ary_new", (RUBY_PROC*)&dll_rb_ary_new},
Bram Moolenaar51e9fbf2018-08-11 14:24:11 +0200793# ifdef RB_ARY_NEW4_MACRO
794 {"rb_ary_new_from_values", (RUBY_PROC*)&dll_rb_ary_new4},
795# else
796 {"rb_ary_new4", (RUBY_PROC*)&dll_rb_ary_new4},
797# endif
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100798 {"rb_ary_push", (RUBY_PROC*)&dll_rb_ary_push},
Bram Moolenaar41a41412020-01-07 21:32:19 +0100799# if RUBY_VERSION >= 26
Bram Moolenaarb09c6842018-12-27 22:11:01 +0100800 {"rb_ary_detransient", (RUBY_PROC*)&dll_rb_ary_detransient},
801# endif
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200802# endif
Bram Moolenaar41a41412020-01-07 21:32:19 +0100803# if RUBY_VERSION >= 19
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100804 {"rb_int2big", (RUBY_PROC*)&dll_rb_int2big},
Bram Moolenaar165641d2010-02-17 16:23:09 +0100805 {"ruby_script", (RUBY_PROC*)&dll_ruby_script},
806 {"rb_enc_find_index", (RUBY_PROC*)&dll_rb_enc_find_index},
807 {"rb_enc_find", (RUBY_PROC*)&dll_rb_enc_find},
808 {"rb_enc_str_new", (RUBY_PROC*)&dll_rb_enc_str_new},
809 {"rb_sprintf", (RUBY_PROC*)&dll_rb_sprintf},
Bram Moolenaar7a8ef142010-12-24 13:39:35 +0100810 {"rb_require", (RUBY_PROC*)&dll_rb_require},
Bram Moolenaar9b1067e2015-11-19 19:33:15 +0100811 {"ruby_options", (RUBY_PROC*)&dll_ruby_options},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200812# endif
Bram Moolenaar41a41412020-01-07 21:32:19 +0100813# if (RUBY_VERSION >= 19) || defined(RUBY_INIT_STACK)
Bram Moolenaar10f3a792013-05-20 12:52:29 +0200814# ifdef __ia64
815 {"rb_ia64_bsp", (RUBY_PROC*)&dll_rb_ia64_bsp},
816# endif
817 {"ruby_init_stack", (RUBY_PROC*)&dll_ruby_init_stack},
818# endif
Bram Moolenaara1a118b2014-02-05 22:41:15 +0100819# if defined(USE_RGENGC) && USE_RGENGC
Bram Moolenaar41a41412020-01-07 21:32:19 +0100820# if RUBY_VERSION == 21
Bram Moolenaara1a118b2014-02-05 22:41:15 +0100821 {"rb_gc_writebarrier_unprotect_promoted", (RUBY_PROC*)&dll_rb_gc_writebarrier_unprotect_promoted},
Bram Moolenaar0c7485f2015-01-14 14:04:10 +0100822# else
823 {"rb_gc_writebarrier_unprotect", (RUBY_PROC*)&dll_rb_gc_writebarrier_unprotect},
824# endif
Bram Moolenaara1a118b2014-02-05 22:41:15 +0100825# endif
Bram Moolenaardace9f72020-12-28 15:07:45 +0100826# if RUBY_VERSION >= 30
827 {"ruby_malloc_size_overflow", (RUBY_PROC*)&dll_ruby_malloc_size_overflow},
828# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000829 {"", NULL},
830};
831
832/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000833 * Load library and get all pointers.
834 * Parameter 'libname' provides name of DLL.
835 * Return OK or FAIL.
836 */
837 static int
838ruby_runtime_link_init(char *libname, int verbose)
839{
840 int i;
841
842 if (hinstRuby)
843 return OK;
Bram Moolenaarf9b5ef82010-09-29 13:02:53 +0200844 hinstRuby = load_dll(libname);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000845 if (!hinstRuby)
846 {
847 if (verbose)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +0000848 semsg(_(e_could_not_load_library_str_str), libname, load_dll_error());
Bram Moolenaar071d4272004-06-13 20:20:40 +0000849 return FAIL;
850 }
851
852 for (i = 0; ruby_funcname_table[i].ptr; ++i)
853 {
Bram Moolenaarf9b5ef82010-09-29 13:02:53 +0200854 if (!(*ruby_funcname_table[i].ptr = symbol_from_dll(hinstRuby,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000855 ruby_funcname_table[i].name)))
856 {
Bram Moolenaarf9b5ef82010-09-29 13:02:53 +0200857 close_dll(hinstRuby);
Bram Moolenaar3ca71f12010-10-27 16:49:47 +0200858 hinstRuby = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000859 if (verbose)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +0000860 semsg(_(e_could_not_load_library_function_str), ruby_funcname_table[i].name);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000861 return FAIL;
862 }
863 }
864 return OK;
865}
866
867/*
868 * If ruby is enabled (there is installed ruby on Windows system) return TRUE,
869 * else FALSE.
870 */
871 int
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100872ruby_enabled(int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000873{
Bram Moolenaar25e4fcd2016-01-09 14:57:47 +0100874 return ruby_runtime_link_init((char *)p_rubydll, verbose) == OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000875}
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100876#endif // defined(DYNAMIC_RUBY) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000877
878 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100879ruby_end(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000880{
Bram Moolenaar071d4272004-06-13 20:20:40 +0000881}
882
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100883 void
884ex_ruby(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000885{
886 int state;
887 char *script = NULL;
888
Bram Moolenaar35a2e192006-03-13 22:07:11 +0000889 script = (char *)script_get(eap, eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000890 if (!eap->skip && ensure_ruby_initialized())
891 {
892 if (script == NULL)
893 rb_eval_string_protect((char *)eap->arg, &state);
894 else
895 rb_eval_string_protect(script, &state);
896 if (state)
897 error_print(state);
898 }
899 vim_free(script);
900}
901
Bram Moolenaar165641d2010-02-17 16:23:09 +0100902/*
903 * In Ruby 1.9 or later, ruby String object has encoding.
904 * conversion buffer string of vim to ruby String object using
905 * VIM encoding option.
906 */
907 static VALUE
908vim_str2rb_enc_str(const char *s)
909{
Bram Moolenaar41a41412020-01-07 21:32:19 +0100910#if RUBY_VERSION >= 19
Bram Moolenaar165641d2010-02-17 16:23:09 +0100911 long lval;
912 char_u *sval;
913 rb_encoding *enc;
914
Yegappan Lakshmanan64095532021-12-06 11:03:55 +0000915 if (get_option_value((char_u *)"enc", &lval, &sval, NULL, 0) == gov_string)
Bram Moolenaar165641d2010-02-17 16:23:09 +0100916 {
917 enc = rb_enc_find((char *)sval);
918 vim_free(sval);
Bram Moolenaar758535a2016-03-30 22:06:16 +0200919 if (enc)
Bram Moolenaar9b0ac222016-06-01 20:31:43 +0200920 return rb_enc_str_new(s, (long)strlen(s), enc);
Bram Moolenaar165641d2010-02-17 16:23:09 +0100921 }
922#endif
923 return rb_str_new2(s);
924}
925
926 static VALUE
927eval_enc_string_protect(const char *str, int *state)
928{
Bram Moolenaar41a41412020-01-07 21:32:19 +0100929#if RUBY_VERSION >= 19
Bram Moolenaar165641d2010-02-17 16:23:09 +0100930 long lval;
931 char_u *sval;
932 rb_encoding *enc;
933 VALUE v;
934
Yegappan Lakshmanan64095532021-12-06 11:03:55 +0000935 if (get_option_value((char_u *)"enc", &lval, &sval, NULL, 0) == gov_string)
Bram Moolenaar165641d2010-02-17 16:23:09 +0100936 {
937 enc = rb_enc_find((char *)sval);
938 vim_free(sval);
939 if (enc)
940 {
941 v = rb_sprintf("#-*- coding:%s -*-\n%s", rb_enc_name(enc), str);
942 return rb_eval_string_protect(StringValuePtr(v), state);
943 }
944 }
945#endif
946 return rb_eval_string_protect(str, state);
947}
948
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100949 void
950ex_rubydo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000951{
952 int state;
953 linenr_T i;
Bram Moolenaarc593fee2017-01-29 23:11:25 +0100954 buf_T *was_curbuf = curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000955
956 if (ensure_ruby_initialized())
957 {
958 if (u_save(eap->line1 - 1, eap->line2 + 1) != OK)
959 return;
Bram Moolenaar758535a2016-03-30 22:06:16 +0200960 for (i = eap->line1; i <= eap->line2; i++)
961 {
Bram Moolenaare980d8a2010-12-08 13:11:21 +0100962 VALUE line;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000963
Bram Moolenaarc593fee2017-01-29 23:11:25 +0100964 if (i > curbuf->b_ml.ml_line_count)
965 break;
Bram Moolenaare980d8a2010-12-08 13:11:21 +0100966 line = vim_str2rb_enc_str((char *)ml_get(i));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000967 rb_lastline_set(line);
Bram Moolenaar165641d2010-02-17 16:23:09 +0100968 eval_enc_string_protect((char *) eap->arg, &state);
Bram Moolenaar758535a2016-03-30 22:06:16 +0200969 if (state)
970 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000971 error_print(state);
972 break;
973 }
Bram Moolenaarc593fee2017-01-29 23:11:25 +0100974 if (was_curbuf != curbuf)
975 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000976 line = rb_lastline_get();
Bram Moolenaar758535a2016-03-30 22:06:16 +0200977 if (!NIL_P(line))
978 {
979 if (TYPE(line) != T_STRING)
980 {
Bram Moolenaar9a846fb2022-01-01 21:59:18 +0000981 emsg(_(e_dollar_must_be_an_instance_of_string));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000982 return;
983 }
Bram Moolenaar165641d2010-02-17 16:23:09 +0100984 ml_replace(i, (char_u *) StringValuePtr(line), 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000985 changed();
986#ifdef SYNTAX_HL
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100987 syn_changed(i); // recompute syntax hl. for this line
Bram Moolenaar071d4272004-06-13 20:20:40 +0000988#endif
989 }
990 }
991 check_cursor();
992 update_curbuf(NOT_VALID);
993 }
994}
995
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100996 static VALUE
997rb_load_wrap(VALUE file_to_load)
Bram Moolenaar37badc82018-01-31 20:15:30 +0100998{
999 rb_load(file_to_load, 0);
1000 return Qnil;
1001}
1002
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001003 void
1004ex_rubyfile(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001005{
1006 int state;
1007
1008 if (ensure_ruby_initialized())
1009 {
Bram Moolenaar37badc82018-01-31 20:15:30 +01001010 VALUE file_to_load = rb_str_new2((const char *)eap->arg);
1011 rb_protect(rb_load_wrap, file_to_load, &state);
1012 if (state)
1013 error_print(state);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001014 }
1015}
1016
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001017 void
1018ruby_buffer_free(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001019{
Bram Moolenaare344bea2005-09-01 20:46:49 +00001020 if (buf->b_ruby_ref)
1021 {
1022 rb_hash_aset(objtbl, rb_obj_id((VALUE) buf->b_ruby_ref), Qnil);
1023 RDATA(buf->b_ruby_ref)->data = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001024 }
1025}
1026
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001027 void
1028ruby_window_free(win_T *win)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001029{
Bram Moolenaare344bea2005-09-01 20:46:49 +00001030 if (win->w_ruby_ref)
1031 {
1032 rb_hash_aset(objtbl, rb_obj_id((VALUE) win->w_ruby_ref), Qnil);
1033 RDATA(win->w_ruby_ref)->data = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001034 }
1035}
1036
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001037 static int
1038ensure_ruby_initialized(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001039{
1040 if (!ruby_initialized)
1041 {
1042#ifdef DYNAMIC_RUBY
1043 if (ruby_enabled(TRUE))
1044 {
1045#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01001046#ifdef MSWIN
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001047 // suggested by Ariya Mizutani
Bram Moolenaar0b69c732010-02-17 15:11:50 +01001048 int argc = 1;
1049 char *argv[] = {"gvim.exe"};
Bram Moolenaar90140742014-11-27 17:44:08 +01001050 char **argvp = argv;
Bram Moolenaar41a41412020-01-07 21:32:19 +01001051# if RUBY_VERSION >= 19
Bram Moolenaarfe6ce332017-01-14 20:12:01 +01001052 ruby_sysinit(&argc, &argvp);
1053# else
Bram Moolenaar90140742014-11-27 17:44:08 +01001054 NtInitialize(&argc, &argvp);
Bram Moolenaarfe6ce332017-01-14 20:12:01 +01001055# endif
Bram Moolenaar0b69c732010-02-17 15:11:50 +01001056#endif
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001057 {
Bram Moolenaar41a41412020-01-07 21:32:19 +01001058#if (RUBY_VERSION >= 19) || defined(RUBY_INIT_STACK)
Bram Moolenaar99685e62013-05-11 13:56:18 +02001059 ruby_init_stack(ruby_stack_start);
Bram Moolenaar165641d2010-02-17 16:23:09 +01001060#endif
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001061 ruby_init();
1062 }
Bram Moolenaar41a41412020-01-07 21:32:19 +01001063#if RUBY_VERSION >= 19
Bram Moolenaar7a8ef142010-12-24 13:39:35 +01001064 {
1065 int dummy_argc = 2;
Bram Moolenaard1bc96ce2017-09-26 21:21:44 +02001066 char *dummy_argv[] = {"vim-ruby", "-e_=0"};
Bram Moolenaar9b1067e2015-11-19 19:33:15 +01001067 ruby_options(dummy_argc, dummy_argv);
Bram Moolenaar7a8ef142010-12-24 13:39:35 +01001068 }
Bram Moolenaar165641d2010-02-17 16:23:09 +01001069 ruby_script("vim-ruby");
Bram Moolenaar7a8ef142010-12-24 13:39:35 +01001070#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001071 ruby_init_loadpath();
Bram Moolenaar165641d2010-02-17 16:23:09 +01001072#endif
Bram Moolenaar7a8ef142010-12-24 13:39:35 +01001073 ruby_io_init();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001074 ruby_vim_init();
1075 ruby_initialized = 1;
1076#ifdef DYNAMIC_RUBY
1077 }
1078 else
1079 {
Bram Moolenaar9a846fb2022-01-01 21:59:18 +00001080 emsg(_(e_sorry_this_command_is_disabled_the_ruby_library_could_not_be_loaded));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001081 return 0;
1082 }
1083#endif
1084 }
1085 return ruby_initialized;
1086}
1087
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001088 static void
1089error_print(int state)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001090{
Bram Moolenaar41a41412020-01-07 21:32:19 +01001091#if !defined(DYNAMIC_RUBY) && (RUBY_VERSION <= 18)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001092 RUBYEXTERN VALUE ruby_errinfo;
1093#endif
Bram Moolenaarf711cb22018-08-01 18:42:13 +02001094 VALUE error;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001095 VALUE eclass;
1096 VALUE einfo;
Bram Moolenaarf711cb22018-08-01 18:42:13 +02001097 VALUE bt;
1098 int attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001099 char buff[BUFSIZ];
Bram Moolenaarf711cb22018-08-01 18:42:13 +02001100 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001101
1102#define TAG_RETURN 0x1
1103#define TAG_BREAK 0x2
1104#define TAG_NEXT 0x3
1105#define TAG_RETRY 0x4
1106#define TAG_REDO 0x5
1107#define TAG_RAISE 0x6
1108#define TAG_THROW 0x7
1109#define TAG_FATAL 0x8
1110#define TAG_MASK 0xf
1111
Bram Moolenaar758535a2016-03-30 22:06:16 +02001112 switch (state)
1113 {
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001114 case TAG_RETURN:
Bram Moolenaar9a846fb2022-01-01 21:59:18 +00001115 emsg(_(e_unexpected_return));
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001116 break;
1117 case TAG_NEXT:
Bram Moolenaar9a846fb2022-01-01 21:59:18 +00001118 emsg(_(e_unexpected_next));
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001119 break;
1120 case TAG_BREAK:
Bram Moolenaar9a846fb2022-01-01 21:59:18 +00001121 emsg(_(e_unexpected_break));
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001122 break;
1123 case TAG_REDO:
Bram Moolenaar9a846fb2022-01-01 21:59:18 +00001124 emsg(_(e_unexpected_redo));
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001125 break;
1126 case TAG_RETRY:
Bram Moolenaar9a846fb2022-01-01 21:59:18 +00001127 emsg(_(e_retry_outside_of_rescue_clause));
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001128 break;
1129 case TAG_RAISE:
1130 case TAG_FATAL:
Bram Moolenaar41a41412020-01-07 21:32:19 +01001131#if RUBY_VERSION >= 19
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001132 error = rb_errinfo();
Bram Moolenaar165641d2010-02-17 16:23:09 +01001133#else
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001134 error = ruby_errinfo;
Bram Moolenaar165641d2010-02-17 16:23:09 +01001135#endif
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001136 eclass = CLASS_OF(error);
1137 einfo = rb_obj_as_string(error);
1138 if (eclass == rb_eRuntimeError && RSTRING_LEN(einfo) == 0)
1139 {
Bram Moolenaar9a846fb2022-01-01 21:59:18 +00001140 emsg(_(e_unhandled_exception));
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001141 }
1142 else
1143 {
1144 VALUE epath;
1145 char *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001146
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001147 epath = rb_class_path(eclass);
1148 vim_snprintf(buff, BUFSIZ, "%s: %s",
1149 RSTRING_PTR(epath), RSTRING_PTR(einfo));
1150 p = strchr(buff, '\n');
1151 if (p) *p = '\0';
1152 emsg(buff);
1153 }
Bram Moolenaarf711cb22018-08-01 18:42:13 +02001154
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001155 attr = syn_name2attr((char_u *)"Error");
Bram Moolenaar49c99fc2020-02-11 23:01:39 +01001156#if RUBY_VERSION >= 21
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001157 bt = rb_funcallv(error, rb_intern("backtrace"), 0, 0);
1158 for (i = 0; i < RARRAY_LEN(bt); i++)
1159 msg_attr(RSTRING_PTR(RARRAY_AREF(bt, i)), attr);
Bram Moolenaar49c99fc2020-02-11 23:01:39 +01001160#else
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001161 bt = rb_funcall2(error, rb_intern("backtrace"), 0, 0);
1162 for (i = 0; i < RARRAY_LEN(bt); i++)
1163 msg_attr(RSTRING_PTR(RARRAY_PTR(bt)[i]), attr);
Bram Moolenaar49c99fc2020-02-11 23:01:39 +01001164#endif
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001165 break;
1166 default:
Bram Moolenaar9a846fb2022-01-01 21:59:18 +00001167 vim_snprintf(buff, BUFSIZ, _(e_unknown_longjmp_status_nr), state);
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001168 emsg(buff);
1169 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001170 }
1171}
1172
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001173 static VALUE
1174vim_message(VALUE self UNUSED, VALUE str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001175{
1176 char *buff, *p;
1177
1178 str = rb_obj_as_string(str);
Bram Moolenaar3f5f7952011-08-04 19:34:59 +02001179 if (RSTRING_LEN(str) > 0)
1180 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001181 // Only do this when the string isn't empty, alloc(0) causes trouble.
Bram Moolenaar00ccf542017-09-03 15:17:48 +02001182 buff = ALLOCA_N(char, RSTRING_LEN(str) + 1);
Bram Moolenaar3f5f7952011-08-04 19:34:59 +02001183 strcpy(buff, RSTRING_PTR(str));
1184 p = strchr(buff, '\n');
1185 if (p) *p = '\0';
Bram Moolenaar32526b32019-01-19 17:43:09 +01001186 msg(buff);
Bram Moolenaar3f5f7952011-08-04 19:34:59 +02001187 }
1188 else
1189 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01001190 msg("");
Bram Moolenaar3f5f7952011-08-04 19:34:59 +02001191 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001192 return Qnil;
1193}
1194
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001195 static VALUE
1196vim_set_option(VALUE self UNUSED, VALUE str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001197{
Bram Moolenaar165641d2010-02-17 16:23:09 +01001198 do_set((char_u *)StringValuePtr(str), 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001199 update_screen(NOT_VALID);
1200 return Qnil;
1201}
1202
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001203 static VALUE
1204vim_command(VALUE self UNUSED, VALUE str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001205{
Bram Moolenaar165641d2010-02-17 16:23:09 +01001206 do_cmdline_cmd((char_u *)StringValuePtr(str));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001207 return Qnil;
1208}
1209
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001210#ifdef FEAT_EVAL
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001211 static VALUE
1212vim_to_ruby(typval_T *tv)
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001213{
1214 VALUE result = Qnil;
1215
1216 if (tv->v_type == VAR_STRING)
1217 {
Bram Moolenaar94127e42010-03-19 23:08:48 +01001218 result = rb_str_new2(tv->vval.v_string == NULL
1219 ? "" : (char *)(tv->vval.v_string));
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001220 }
1221 else if (tv->v_type == VAR_NUMBER)
1222 {
Bram Moolenaar639a2552010-03-17 18:15:23 +01001223 result = INT2NUM(tv->vval.v_number);
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001224 }
1225# ifdef FEAT_FLOAT
1226 else if (tv->v_type == VAR_FLOAT)
1227 {
Bram Moolenaar639a2552010-03-17 18:15:23 +01001228 result = rb_float_new(tv->vval.v_float);
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001229 }
1230# endif
1231 else if (tv->v_type == VAR_LIST)
1232 {
Bram Moolenaar639a2552010-03-17 18:15:23 +01001233 list_T *list = tv->vval.v_list;
1234 listitem_T *curr;
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001235
Bram Moolenaar639a2552010-03-17 18:15:23 +01001236 result = rb_ary_new();
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001237
Bram Moolenaar639a2552010-03-17 18:15:23 +01001238 if (list != NULL)
1239 {
Bram Moolenaaraeea7212020-04-02 18:50:46 +02001240 FOR_ALL_LIST_ITEMS(list, curr)
Bram Moolenaar639a2552010-03-17 18:15:23 +01001241 rb_ary_push(result, vim_to_ruby(&curr->li_tv));
Bram Moolenaar639a2552010-03-17 18:15:23 +01001242 }
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001243 }
1244 else if (tv->v_type == VAR_DICT)
1245 {
Bram Moolenaar639a2552010-03-17 18:15:23 +01001246 result = rb_hash_new();
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001247
Bram Moolenaar639a2552010-03-17 18:15:23 +01001248 if (tv->vval.v_dict != NULL)
1249 {
1250 hashtab_T *ht = &tv->vval.v_dict->dv_hashtab;
1251 long_u todo = ht->ht_used;
1252 hashitem_T *hi;
1253 dictitem_T *di;
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001254
Bram Moolenaar639a2552010-03-17 18:15:23 +01001255 for (hi = ht->ht_array; todo > 0; ++hi)
1256 {
1257 if (!HASHITEM_EMPTY(hi))
1258 {
1259 --todo;
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001260
Bram Moolenaar639a2552010-03-17 18:15:23 +01001261 di = dict_lookup(hi);
1262 rb_hash_aset(result, rb_str_new2((char *)hi->hi_key),
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001263 vim_to_ruby(&di->di_tv));
Bram Moolenaar639a2552010-03-17 18:15:23 +01001264 }
1265 }
1266 }
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001267 }
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01001268 else if (tv->v_type == VAR_BOOL || tv->v_type == VAR_SPECIAL)
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001269 {
Bram Moolenaard84b26a2018-07-28 17:18:09 +02001270 if (tv->vval.v_number == VVAL_TRUE)
1271 result = Qtrue;
1272 else if (tv->vval.v_number == VVAL_FALSE)
1273 result = Qfalse;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001274 }
1275 else if (tv->v_type == VAR_BLOB)
1276 {
1277 result = rb_str_new(tv->vval.v_blob->bv_ga.ga_data,
1278 tv->vval.v_blob->bv_ga.ga_len);
1279 }
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001280 // else return Qnil;
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001281
1282 return result;
1283}
1284#endif
1285
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001286 static VALUE
1287vim_evaluate(VALUE self UNUSED, VALUE str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001288{
1289#ifdef FEAT_EVAL
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001290 typval_T *tv;
1291 VALUE result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001292
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001293 tv = eval_expr((char_u *)StringValuePtr(str), NULL);
1294 if (tv == NULL)
Bram Moolenaar639a2552010-03-17 18:15:23 +01001295 return Qnil;
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001296 result = vim_to_ruby(tv);
1297
1298 free_tv(tv);
1299
1300 return result;
1301#else
1302 return Qnil;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001303#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001304}
1305
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001306#ifdef USE_TYPEDDATA
1307static size_t buffer_dsize(const void *buf);
1308
1309static const rb_data_type_t buffer_type = {
1310 "vim_buffer",
Bram Moolenaar41a41412020-01-07 21:32:19 +01001311 {0, 0, buffer_dsize,
1312# if RUBY_VERSION >= 27
Bram Moolenaar8b430b42020-02-22 15:01:00 +01001313 0, {0}
Bram Moolenaar41a41412020-01-07 21:32:19 +01001314# else
1315 {0, 0}
1316# endif
1317 },
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001318 0, 0,
1319# ifdef RUBY_TYPED_FREE_IMMEDIATELY
1320 0,
1321# endif
1322};
1323
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001324 static size_t
1325buffer_dsize(const void *buf UNUSED)
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001326{
1327 return sizeof(buf_T);
1328}
1329#endif
1330
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001331 static VALUE
1332buffer_new(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001333{
Bram Moolenaare344bea2005-09-01 20:46:49 +00001334 if (buf->b_ruby_ref)
1335 {
1336 return (VALUE) buf->b_ruby_ref;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001337 }
Bram Moolenaare344bea2005-09-01 20:46:49 +00001338 else
1339 {
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001340#ifdef USE_TYPEDDATA
1341 VALUE obj = TypedData_Wrap_Struct(cBuffer, &buffer_type, buf);
1342#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001343 VALUE obj = Data_Wrap_Struct(cBuffer, 0, 0, buf);
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001344#endif
Bram Moolenaare344bea2005-09-01 20:46:49 +00001345 buf->b_ruby_ref = (void *) obj;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001346 rb_hash_aset(objtbl, rb_obj_id(obj), obj);
1347 return obj;
1348 }
1349}
1350
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001351 static buf_T *
1352get_buf(VALUE obj)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001353{
1354 buf_T *buf;
1355
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001356#ifdef USE_TYPEDDATA
1357 TypedData_Get_Struct(obj, buf_T, &buffer_type, buf);
1358#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001359 Data_Get_Struct(obj, buf_T, buf);
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001360#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001361 if (buf == NULL)
1362 rb_raise(eDeletedBufferError, "attempt to refer to deleted buffer");
1363 return buf;
1364}
1365
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001366 static VALUE
1367vim_blob(VALUE self UNUSED, VALUE str)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001368{
1369 VALUE result = rb_str_new("0z", 2);
1370 char buf[4];
1371 int i;
1372 for (i = 0; i < RSTRING_LEN(str); i++)
1373 {
Bram Moolenaar0d13cce2019-02-23 14:23:03 +01001374 sprintf(buf, "%02X", (unsigned char)(RSTRING_PTR(str)[i]));
Bram Moolenaarb191be22019-01-30 21:00:12 +01001375 rb_str_concat(result, rb_str_new2(buf));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001376 }
1377 return result;
1378}
1379
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001380 static VALUE
Bram Moolenaard5a986f2020-12-06 21:11:31 +01001381buffer_s_current(VALUE self UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001382{
1383 return buffer_new(curbuf);
1384}
1385
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001386 static VALUE
Bram Moolenaard5a986f2020-12-06 21:11:31 +01001387buffer_s_current_getter(ID id UNUSED, VALUE *x UNUSED)
1388{
1389 return buffer_new(curbuf);
1390}
1391
1392 static VALUE
1393buffer_s_count(VALUE self UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001394{
1395 buf_T *b;
1396 int n = 0;
1397
Bram Moolenaar29323592016-07-24 22:04:11 +02001398 FOR_ALL_BUFFERS(b)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001399 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001400 // Deleted buffers should not be counted
1401 // SegPhault - 01/07/05
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001402 if (b->b_p_bl)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001403 n++;
1404 }
1405
Bram Moolenaar071d4272004-06-13 20:20:40 +00001406 return INT2NUM(n);
1407}
1408
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001409 static VALUE
1410buffer_s_aref(VALUE self UNUSED, VALUE num)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001411{
1412 buf_T *b;
1413 int n = NUM2INT(num);
1414
Bram Moolenaar29323592016-07-24 22:04:11 +02001415 FOR_ALL_BUFFERS(b)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001416 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001417 // Deleted buffers should not be counted
1418 // SegPhault - 01/07/05
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001419 if (!b->b_p_bl)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001420 continue;
1421
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001422 if (n == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001423 return buffer_new(b);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001424
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001425 n--;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001426 }
1427 return Qnil;
1428}
1429
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001430 static VALUE
1431buffer_name(VALUE self)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001432{
1433 buf_T *buf = get_buf(self);
1434
Bram Moolenaar35a2e192006-03-13 22:07:11 +00001435 return buf->b_ffname ? rb_str_new2((char *)buf->b_ffname) : Qnil;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001436}
1437
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001438 static VALUE
1439buffer_number(VALUE self)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001440{
1441 buf_T *buf = get_buf(self);
1442
1443 return INT2NUM(buf->b_fnum);
1444}
1445
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001446 static VALUE
1447buffer_count(VALUE self)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001448{
1449 buf_T *buf = get_buf(self);
1450
1451 return INT2NUM(buf->b_ml.ml_line_count);
1452}
1453
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001454 static VALUE
1455get_buffer_line(buf_T *buf, linenr_T n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001456{
Bram Moolenaar3c531602010-11-16 14:46:19 +01001457 if (n <= 0 || n > buf->b_ml.ml_line_count)
1458 rb_raise(rb_eIndexError, "line number %ld out of range", (long)n);
1459 return vim_str2rb_enc_str((char *)ml_get_buf(buf, n, FALSE));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001460}
1461
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001462 static VALUE
1463buffer_aref(VALUE self, VALUE num)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001464{
1465 buf_T *buf = get_buf(self);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001466
1467 if (buf != NULL)
1468 return get_buffer_line(buf, (linenr_T)NUM2LONG(num));
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001469 return Qnil; // For stop warning
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001470}
1471
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001472 static VALUE
1473set_buffer_line(buf_T *buf, linenr_T n, VALUE str)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001474{
Bram Moolenaar165641d2010-02-17 16:23:09 +01001475 char *line = StringValuePtr(str);
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001476 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001477
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001478 if (n > 0 && n <= buf->b_ml.ml_line_count && line != NULL)
1479 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001480 // set curwin/curbuf for "buf" and save some things
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001481 aucmd_prepbuf(&aco, buf);
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001482
Bram Moolenaar758535a2016-03-30 22:06:16 +02001483 if (u_savesub(n) == OK)
1484 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001485 ml_replace(n, (char_u *)line, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001486 changed();
1487#ifdef SYNTAX_HL
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001488 syn_changed(n); // recompute syntax hl. for this line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001489#endif
1490 }
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001491
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001492 // restore curwin/curbuf and a few other things
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001493 aucmd_restbuf(&aco);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001494 // Careful: autocommands may have made "buf" invalid!
Bram Moolenaarf30e74c2006-08-16 17:35:00 +00001495
Bram Moolenaar071d4272004-06-13 20:20:40 +00001496 update_curbuf(NOT_VALID);
1497 }
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001498 else
1499 {
Bram Moolenaar165641d2010-02-17 16:23:09 +01001500 rb_raise(rb_eIndexError, "line number %ld out of range", (long)n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001501 }
1502 return str;
1503}
1504
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001505 static VALUE
1506buffer_aset(VALUE self, VALUE num, VALUE str)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001507{
1508 buf_T *buf = get_buf(self);
1509
1510 if (buf != NULL)
1511 return set_buffer_line(buf, (linenr_T)NUM2LONG(num), str);
1512 return str;
1513}
1514
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001515 static VALUE
1516buffer_delete(VALUE self, VALUE num)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001517{
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001518 buf_T *buf = get_buf(self);
1519 long n = NUM2LONG(num);
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001520 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001521
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001522 if (n > 0 && n <= buf->b_ml.ml_line_count)
1523 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001524 // set curwin/curbuf for "buf" and save some things
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001525 aucmd_prepbuf(&aco, buf);
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001526
Bram Moolenaar758535a2016-03-30 22:06:16 +02001527 if (u_savedel(n, 1) == OK)
1528 {
Bram Moolenaarca70c072020-05-30 20:30:46 +02001529 ml_delete(n);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001530
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001531 // Changes to non-active buffers should properly refresh
1532 // SegPhault - 01/09/05
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001533 deleted_lines_mark(n, 1L);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001534
Bram Moolenaar071d4272004-06-13 20:20:40 +00001535 changed();
1536 }
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001537
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001538 // restore curwin/curbuf and a few other things
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001539 aucmd_restbuf(&aco);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001540 // Careful: autocommands may have made "buf" invalid!
Bram Moolenaarf30e74c2006-08-16 17:35:00 +00001541
Bram Moolenaar071d4272004-06-13 20:20:40 +00001542 update_curbuf(NOT_VALID);
1543 }
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001544 else
1545 {
Bram Moolenaar165641d2010-02-17 16:23:09 +01001546 rb_raise(rb_eIndexError, "line number %ld out of range", n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001547 }
1548 return Qnil;
1549}
1550
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001551 static VALUE
1552buffer_append(VALUE self, VALUE num, VALUE str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001553{
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001554 buf_T *buf = get_buf(self);
Bram Moolenaar165641d2010-02-17 16:23:09 +01001555 char *line = StringValuePtr(str);
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001556 long n = NUM2LONG(num);
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001557 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001558
Bram Moolenaar3c531602010-11-16 14:46:19 +01001559 if (line == NULL)
1560 {
Bram Moolenaar165641d2010-02-17 16:23:09 +01001561 rb_raise(rb_eIndexError, "NULL line");
1562 }
1563 else if (n >= 0 && n <= buf->b_ml.ml_line_count)
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001564 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001565 // set curwin/curbuf for "buf" and save some things
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001566 aucmd_prepbuf(&aco, buf);
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001567
Bram Moolenaar758535a2016-03-30 22:06:16 +02001568 if (u_inssub(n + 1) == OK)
1569 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001570 ml_append(n, (char_u *) line, (colnr_T) 0, FALSE);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001571
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001572 // Changes to non-active buffers should properly refresh screen
1573 // SegPhault - 12/20/04
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001574 appended_lines_mark(n, 1L);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001575
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001576 changed();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001577 }
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001578
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001579 // restore curwin/curbuf and a few other things
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001580 aucmd_restbuf(&aco);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001581 // Careful: autocommands may have made "buf" invalid!
Bram Moolenaarf30e74c2006-08-16 17:35:00 +00001582
Bram Moolenaar071d4272004-06-13 20:20:40 +00001583 update_curbuf(NOT_VALID);
1584 }
Bram Moolenaar3c531602010-11-16 14:46:19 +01001585 else
1586 {
Bram Moolenaar165641d2010-02-17 16:23:09 +01001587 rb_raise(rb_eIndexError, "line number %ld out of range", n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001588 }
1589 return str;
1590}
1591
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001592#ifdef USE_TYPEDDATA
1593static size_t window_dsize(const void *buf);
1594
1595static const rb_data_type_t window_type = {
1596 "vim_window",
Bram Moolenaar41a41412020-01-07 21:32:19 +01001597 {0, 0, window_dsize,
1598# if RUBY_VERSION >= 27
Bram Moolenaar8b430b42020-02-22 15:01:00 +01001599 0, {0}
Bram Moolenaar41a41412020-01-07 21:32:19 +01001600# else
1601 {0, 0}
1602# endif
1603 },
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001604 0, 0,
1605# ifdef RUBY_TYPED_FREE_IMMEDIATELY
1606 0,
1607# endif
1608};
1609
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001610 static size_t
1611window_dsize(const void *win UNUSED)
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001612{
1613 return sizeof(win_T);
1614}
1615#endif
1616
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001617 static VALUE
1618window_new(win_T *win)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001619{
Bram Moolenaare344bea2005-09-01 20:46:49 +00001620 if (win->w_ruby_ref)
1621 {
1622 return (VALUE) win->w_ruby_ref;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001623 }
Bram Moolenaare344bea2005-09-01 20:46:49 +00001624 else
1625 {
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001626#ifdef USE_TYPEDDATA
1627 VALUE obj = TypedData_Wrap_Struct(cVimWindow, &window_type, win);
1628#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001629 VALUE obj = Data_Wrap_Struct(cVimWindow, 0, 0, win);
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001630#endif
Bram Moolenaare344bea2005-09-01 20:46:49 +00001631 win->w_ruby_ref = (void *) obj;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001632 rb_hash_aset(objtbl, rb_obj_id(obj), obj);
1633 return obj;
1634 }
1635}
1636
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001637 static win_T *
1638get_win(VALUE obj)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001639{
1640 win_T *win;
1641
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001642#ifdef USE_TYPEDDATA
1643 TypedData_Get_Struct(obj, win_T, &window_type, win);
1644#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001645 Data_Get_Struct(obj, win_T, win);
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001646#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001647 if (win == NULL)
1648 rb_raise(eDeletedWindowError, "attempt to refer to deleted window");
1649 return win;
1650}
1651
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001652 static VALUE
Bram Moolenaard5a986f2020-12-06 21:11:31 +01001653window_s_current(VALUE self UNUSED)
1654{
1655 return window_new(curwin);
1656}
1657
1658 static VALUE
1659window_s_current_getter(ID id UNUSED, VALUE *x UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001660{
1661 return window_new(curwin);
1662}
1663
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001664/*
1665 * Added line manipulation functions
1666 * SegPhault - 03/07/05
1667 */
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001668 static VALUE
Bram Moolenaard5a986f2020-12-06 21:11:31 +01001669line_s_current(VALUE self UNUSED)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001670{
1671 return get_buffer_line(curbuf, curwin->w_cursor.lnum);
1672}
1673
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001674 static VALUE
1675set_current_line(VALUE self UNUSED, VALUE str)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001676{
1677 return set_buffer_line(curbuf, curwin->w_cursor.lnum, str);
1678}
1679
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001680 static VALUE
Bram Moolenaard5a986f2020-12-06 21:11:31 +01001681current_line_number(VALUE self UNUSED)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001682{
1683 return INT2FIX((int)curwin->w_cursor.lnum);
1684}
1685
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001686 static VALUE
Bram Moolenaard5a986f2020-12-06 21:11:31 +01001687window_s_count(VALUE self UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001688{
Bram Moolenaar071d4272004-06-13 20:20:40 +00001689 win_T *w;
1690 int n = 0;
1691
Bram Moolenaar29323592016-07-24 22:04:11 +02001692 FOR_ALL_WINDOWS(w)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001693 n++;
1694 return INT2NUM(n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001695}
1696
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001697 static VALUE
1698window_s_aref(VALUE self UNUSED, VALUE num)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001699{
1700 win_T *w;
1701 int n = NUM2INT(num);
1702
Bram Moolenaar071d4272004-06-13 20:20:40 +00001703 for (w = firstwin; w != NULL; w = w->w_next, --n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001704 if (n == 0)
1705 return window_new(w);
1706 return Qnil;
1707}
1708
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001709 static VALUE
1710window_buffer(VALUE self)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001711{
1712 win_T *win = get_win(self);
1713
1714 return buffer_new(win->w_buffer);
1715}
1716
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001717 static VALUE
1718window_height(VALUE self)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001719{
1720 win_T *win = get_win(self);
1721
1722 return INT2NUM(win->w_height);
1723}
1724
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001725 static VALUE
1726window_set_height(VALUE self, VALUE height)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001727{
1728 win_T *win = get_win(self);
1729 win_T *savewin = curwin;
1730
1731 curwin = win;
1732 win_setheight(NUM2INT(height));
1733 curwin = savewin;
1734 return height;
1735}
1736
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001737 static VALUE
1738window_width(VALUE self UNUSED)
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001739{
Bram Moolenaare745d752017-09-22 16:56:22 +02001740 return INT2NUM(get_win(self)->w_width);
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001741}
1742
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001743 static VALUE
1744window_set_width(VALUE self UNUSED, VALUE width)
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001745{
1746 win_T *win = get_win(self);
1747 win_T *savewin = curwin;
1748
1749 curwin = win;
1750 win_setwidth(NUM2INT(width));
1751 curwin = savewin;
1752 return width;
1753}
1754
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001755 static VALUE
1756window_cursor(VALUE self)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001757{
1758 win_T *win = get_win(self);
1759
1760 return rb_assoc_new(INT2NUM(win->w_cursor.lnum), INT2NUM(win->w_cursor.col));
1761}
1762
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001763 static VALUE
1764window_set_cursor(VALUE self, VALUE pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001765{
1766 VALUE lnum, col;
1767 win_T *win = get_win(self);
1768
1769 Check_Type(pos, T_ARRAY);
Bram Moolenaar165641d2010-02-17 16:23:09 +01001770 if (RARRAY_LEN(pos) != 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001771 rb_raise(rb_eArgError, "array length must be 2");
Bram Moolenaar165641d2010-02-17 16:23:09 +01001772 lnum = RARRAY_PTR(pos)[0];
1773 col = RARRAY_PTR(pos)[1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001774 win->w_cursor.lnum = NUM2LONG(lnum);
1775 win->w_cursor.col = NUM2UINT(col);
Bram Moolenaar53901442018-07-25 22:02:36 +02001776 win->w_set_curswant = TRUE;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001777 check_cursor(); // put cursor on an existing line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001778 update_screen(NOT_VALID);
1779 return Qnil;
1780}
1781
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001782 static VALUE
1783f_nop(VALUE self UNUSED)
Bram Moolenaar35df7d22012-04-20 18:05:47 +02001784{
1785 return Qnil;
1786}
1787
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001788 static VALUE
1789f_p(int argc, VALUE *argv, VALUE self UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001790{
1791 int i;
1792 VALUE str = rb_str_new("", 0);
Bram Moolenaar51e9fbf2018-08-11 14:24:11 +02001793 VALUE ret = Qnil;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001794
Bram Moolenaar758535a2016-03-30 22:06:16 +02001795 for (i = 0; i < argc; i++)
1796 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001797 if (i > 0) rb_str_cat(str, ", ", 2);
1798 rb_str_concat(str, rb_inspect(argv[i]));
1799 }
Bram Moolenaar32526b32019-01-19 17:43:09 +01001800 msg(RSTRING_PTR(str));
Bram Moolenaar51e9fbf2018-08-11 14:24:11 +02001801
1802 if (argc == 1)
1803 ret = argv[0];
1804 else if (argc > 1)
1805 ret = rb_ary_new4(argc, argv);
1806 return ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001807}
1808
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001809 static void
1810ruby_io_init(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001811{
1812#ifndef DYNAMIC_RUBY
1813 RUBYEXTERN VALUE rb_stdout;
Bram Moolenaarb6c8cd82018-07-24 05:41:30 +02001814 RUBYEXTERN VALUE rb_stderr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001815#endif
1816
1817 rb_stdout = rb_obj_alloc(rb_cObject);
Bram Moolenaarb6c8cd82018-07-24 05:41:30 +02001818 rb_stderr = rb_obj_alloc(rb_cObject);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001819 rb_define_singleton_method(rb_stdout, "write", vim_message, 1);
Bram Moolenaar35df7d22012-04-20 18:05:47 +02001820 rb_define_singleton_method(rb_stdout, "flush", f_nop, 0);
Bram Moolenaarb6c8cd82018-07-24 05:41:30 +02001821 rb_define_singleton_method(rb_stderr, "write", vim_message, 1);
1822 rb_define_singleton_method(rb_stderr, "flush", f_nop, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001823 rb_define_global_function("p", f_p, -1);
1824}
1825
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001826 static void
1827ruby_vim_init(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001828{
1829 objtbl = rb_hash_new();
1830 rb_global_variable(&objtbl);
1831
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001832 // The Vim module used to be called "VIM", but "Vim" is better. Make an
1833 // alias "VIM" for backwards compatibility.
Bram Moolenaarf711faf2007-05-10 16:48:19 +00001834 mVIM = rb_define_module("Vim");
1835 rb_define_const(rb_cObject, "VIM", mVIM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001836 rb_define_const(mVIM, "VERSION_MAJOR", INT2NUM(VIM_VERSION_MAJOR));
1837 rb_define_const(mVIM, "VERSION_MINOR", INT2NUM(VIM_VERSION_MINOR));
1838 rb_define_const(mVIM, "VERSION_BUILD", INT2NUM(VIM_VERSION_BUILD));
1839 rb_define_const(mVIM, "VERSION_PATCHLEVEL", INT2NUM(VIM_VERSION_PATCHLEVEL));
1840 rb_define_const(mVIM, "VERSION_SHORT", rb_str_new2(VIM_VERSION_SHORT));
1841 rb_define_const(mVIM, "VERSION_MEDIUM", rb_str_new2(VIM_VERSION_MEDIUM));
1842 rb_define_const(mVIM, "VERSION_LONG", rb_str_new2(VIM_VERSION_LONG));
1843 rb_define_const(mVIM, "VERSION_LONG_DATE", rb_str_new2(VIM_VERSION_LONG_DATE));
1844 rb_define_module_function(mVIM, "message", vim_message, 1);
1845 rb_define_module_function(mVIM, "set_option", vim_set_option, 1);
1846 rb_define_module_function(mVIM, "command", vim_command, 1);
1847 rb_define_module_function(mVIM, "evaluate", vim_evaluate, 1);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001848 rb_define_module_function(mVIM, "blob", vim_blob, 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001849
1850 eDeletedBufferError = rb_define_class_under(mVIM, "DeletedBufferError",
1851 rb_eStandardError);
1852 eDeletedWindowError = rb_define_class_under(mVIM, "DeletedWindowError",
1853 rb_eStandardError);
1854
1855 cBuffer = rb_define_class_under(mVIM, "Buffer", rb_cObject);
1856 rb_define_singleton_method(cBuffer, "current", buffer_s_current, 0);
1857 rb_define_singleton_method(cBuffer, "count", buffer_s_count, 0);
1858 rb_define_singleton_method(cBuffer, "[]", buffer_s_aref, 1);
1859 rb_define_method(cBuffer, "name", buffer_name, 0);
1860 rb_define_method(cBuffer, "number", buffer_number, 0);
1861 rb_define_method(cBuffer, "count", buffer_count, 0);
1862 rb_define_method(cBuffer, "length", buffer_count, 0);
1863 rb_define_method(cBuffer, "[]", buffer_aref, 1);
1864 rb_define_method(cBuffer, "[]=", buffer_aset, 2);
1865 rb_define_method(cBuffer, "delete", buffer_delete, 1);
1866 rb_define_method(cBuffer, "append", buffer_append, 2);
1867
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001868 // Added line manipulation functions
1869 // SegPhault - 03/07/05
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001870 rb_define_method(cBuffer, "line_number", current_line_number, 0);
1871 rb_define_method(cBuffer, "line", line_s_current, 0);
1872 rb_define_method(cBuffer, "line=", set_current_line, 1);
1873
1874
Bram Moolenaar071d4272004-06-13 20:20:40 +00001875 cVimWindow = rb_define_class_under(mVIM, "Window", rb_cObject);
1876 rb_define_singleton_method(cVimWindow, "current", window_s_current, 0);
1877 rb_define_singleton_method(cVimWindow, "count", window_s_count, 0);
1878 rb_define_singleton_method(cVimWindow, "[]", window_s_aref, 1);
1879 rb_define_method(cVimWindow, "buffer", window_buffer, 0);
1880 rb_define_method(cVimWindow, "height", window_height, 0);
1881 rb_define_method(cVimWindow, "height=", window_set_height, 1);
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001882 rb_define_method(cVimWindow, "width", window_width, 0);
1883 rb_define_method(cVimWindow, "width=", window_set_width, 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001884 rb_define_method(cVimWindow, "cursor", window_cursor, 0);
1885 rb_define_method(cVimWindow, "cursor=", window_set_cursor, 1);
1886
Bram Moolenaard5a986f2020-12-06 21:11:31 +01001887 rb_define_virtual_variable("$curbuf", buffer_s_current_getter, 0);
1888 rb_define_virtual_variable("$curwin", window_s_current_getter, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001889}
Bram Moolenaar99685e62013-05-11 13:56:18 +02001890
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001891 void
1892vim_ruby_init(void *stack_start)
Bram Moolenaar99685e62013-05-11 13:56:18 +02001893{
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001894 // should get machine stack start address early in main function
Bram Moolenaar99685e62013-05-11 13:56:18 +02001895 ruby_stack_start = stack_start;
1896}
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001897
1898 static int
1899convert_hash2dict(VALUE key, VALUE val, VALUE arg)
1900{
1901 dict_T *d = (dict_T *)arg;
1902 dictitem_T *di;
1903
Bram Moolenaardace9f72020-12-28 15:07:45 +01001904 di = dictitem_alloc((char_u *)RSTRING_PTR(rb_obj_as_string(key)));
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001905 if (di == NULL || ruby_convert_to_vim_value(val, &di->di_tv) != OK
1906 || dict_add(d, di) != OK)
1907 {
1908 d->dv_hashtab.ht_error = TRUE;
1909 return ST_STOP;
1910 }
1911 return ST_CONTINUE;
1912}
1913
1914 static int
1915ruby_convert_to_vim_value(VALUE val, typval_T *rettv)
1916{
1917 switch (TYPE(val))
1918 {
1919 case T_NIL:
1920 rettv->v_type = VAR_SPECIAL;
1921 rettv->vval.v_number = VVAL_NULL;
1922 break;
1923 case T_TRUE:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01001924 rettv->v_type = VAR_BOOL;
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001925 rettv->vval.v_number = VVAL_TRUE;
1926 break;
1927 case T_FALSE:
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_FALSE;
1930 break;
1931 case T_BIGNUM:
1932 case T_FIXNUM:
1933 rettv->v_type = VAR_NUMBER;
1934 rettv->vval.v_number = (varnumber_T)NUM2LONG(val);
1935 break;
1936#ifdef FEAT_FLOAT
1937 case T_FLOAT:
1938 rettv->v_type = VAR_FLOAT;
1939 rettv->vval.v_float = (float_T)NUM2DBL(val);
1940 break;
1941#endif
1942 default:
1943 val = rb_obj_as_string(val);
1944 // FALLTHROUGH
1945 case T_STRING:
1946 {
1947 VALUE str = (VALUE)RSTRING(val);
1948
1949 rettv->v_type = VAR_STRING;
1950 rettv->vval.v_string = vim_strnsave((char_u *)RSTRING_PTR(str),
Bram Moolenaar71ccd032020-06-12 22:59:11 +02001951 RSTRING_LEN(str));
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001952 }
1953 break;
1954 case T_ARRAY:
1955 {
1956 list_T *l;
1957 long i;
1958 typval_T v;
1959
1960 l = list_alloc();
1961 if (l == NULL)
1962 return FAIL;
1963
1964 for (i = 0; i < RARRAY_LEN(val); ++i)
1965 {
1966 if (ruby_convert_to_vim_value((VALUE)RARRAY_PTR(val)[i],
1967 &v) != OK)
1968 {
1969 list_unref(l);
1970 return FAIL;
1971 }
1972 list_append_tv(l, &v);
1973 clear_tv(&v);
1974 }
1975
1976 rettv->v_type = VAR_LIST;
1977 rettv->vval.v_list = l;
1978 ++l->lv_refcount;
1979 }
1980 break;
1981 case T_HASH:
1982 {
1983 dict_T *d;
1984
1985 d = dict_alloc();
1986 if (d == NULL)
1987 return FAIL;
1988
1989 rb_hash_foreach(val, convert_hash2dict, (VALUE)d);
1990 if (d->dv_hashtab.ht_error)
1991 {
1992 dict_unref(d);
1993 return FAIL;
1994 }
1995
1996 rettv->v_type = VAR_DICT;
1997 rettv->vval.v_dict = d;
1998 ++d->dv_refcount;
1999 }
2000 break;
2001 }
2002 return OK;
2003}
2004
2005 void
2006do_rubyeval(char_u *str, typval_T *rettv)
2007{
2008 int retval = FAIL;
2009
2010 if (ensure_ruby_initialized())
2011 {
2012 int state;
2013 VALUE obj;
2014
2015 obj = rb_eval_string_protect((const char *)str, &state);
2016 if (state)
2017 error_print(state);
2018 else
2019 retval = ruby_convert_to_vim_value(obj, rettv);
2020 }
2021 if (retval == FAIL)
2022 {
2023 rettv->v_type = VAR_NUMBER;
2024 rettv->vval.v_number = 0;
2025 }
2026}