blob: 9e58902141cdaf639ed26ad401b9086fbb7b145f [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
3 * VIM - Vi IMproved by Bram Moolenaar
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004 *
5 * Ruby interface by Shugo Maeda
6 * with improvements by SegPhault (Ryan Paul)
Bram Moolenaarf9b5ef82010-09-29 13:02:53 +02007 * with improvements by Jon Maken
Bram Moolenaar071d4272004-06-13 20:20:40 +00008 *
9 * Do ":help uganda" in Vim to read copying and usage conditions.
10 * Do ":help credits" in Vim to see a list of people who contributed.
11 * See README.txt for an overview of the Vim source code.
12 */
13
Bram Moolenaar32d19c12018-09-13 17:26:54 +020014#include "protodef.h"
Bram Moolenaar2d73ff42010-10-27 17:40:59 +020015#ifdef HAVE_CONFIG_H
16# include "auto/config.h"
17#endif
Bram Moolenaar3ca71f12010-10-27 16:49:47 +020018
Bram Moolenaare2793352011-01-17 19:53:27 +010019#include <stdio.h>
20#include <string.h>
21
Bram Moolenaar071d4272004-06-13 20:20:40 +000022#ifdef _WIN32
Bram Moolenaar41a41412020-01-07 21:32:19 +010023# if !defined(DYNAMIC_RUBY) || (RUBY_VERSION < 18)
Bram Moolenaar49c99fc2020-02-11 23:01:39 +010024# define NT
Bram Moolenaar071d4272004-06-13 20:20:40 +000025# endif
26# ifndef DYNAMIC_RUBY
Bram Moolenaar2ab2e862019-12-04 21:24:53 +010027# define IMPORT // For static dll usage __declspec(dllimport)
Bram Moolenaar071d4272004-06-13 20:20:40 +000028# define RUBYEXTERN __declspec(dllimport)
29# endif
30#endif
31#ifndef RUBYEXTERN
32# define RUBYEXTERN extern
33#endif
34
Bram Moolenaardace9f72020-12-28 15:07:45 +010035// suggested by Ariya Mizutani
36#if (_MSC_VER == 1200)
37# undef _WIN32_WINNT
Bram Moolenaar06469e92016-06-11 22:26:53 +020038#endif
39
Bram Moolenaarf9b5ef82010-09-29 13:02:53 +020040#ifdef DYNAMIC_RUBY
Bram Moolenaar071d4272004-06-13 20:20:40 +000041/*
42 * This is tricky. In ruby.h there is (inline) function rb_class_of()
43 * definition. This function use these variables. But we want function to
44 * use dll_* variables.
45 */
Bram Moolenaardace9f72020-12-28 15:07:45 +010046# if RUBY_VERSION >= 24
47# define USE_RUBY_INTEGER
48# endif
49
Bram Moolenaar071d4272004-06-13 20:20:40 +000050# define rb_cFalseClass (*dll_rb_cFalseClass)
51# define rb_cFixnum (*dll_rb_cFixnum)
Bram Moolenaar2016ae52016-06-13 20:08:43 +020052# if defined(USE_RUBY_INTEGER)
53# define rb_cInteger (*dll_rb_cInteger)
Bram Moolenaar06469e92016-06-11 22:26:53 +020054# endif
Bram Moolenaar41a41412020-01-07 21:32:19 +010055# if RUBY_VERSION >= 20
Bram Moolenaardb3fbe52013-03-07 15:16:21 +010056# define rb_cFloat (*dll_rb_cFloat)
57# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000058# define rb_cNilClass (*dll_rb_cNilClass)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010059# define rb_cString (*dll_rb_cString)
Bram Moolenaar071d4272004-06-13 20:20:40 +000060# define rb_cSymbol (*dll_rb_cSymbol)
61# define rb_cTrueClass (*dll_rb_cTrueClass)
Bram Moolenaardace9f72020-12-28 15:07:45 +010062
Bram Moolenaar41a41412020-01-07 21:32:19 +010063# if RUBY_VERSION >= 18
Bram Moolenaar071d4272004-06-13 20:20:40 +000064/*
Bram Moolenaar42d57f02010-03-10 12:47:00 +010065 * On ver 1.8, all Ruby functions are exported with "__declspec(dllimport)"
66 * in ruby.h. But it causes trouble for these variables, because it is
Bram Moolenaar071d4272004-06-13 20:20:40 +000067 * defined in this file. When defined this RUBY_EXPORT it modified to
68 * "extern" and be able to avoid this problem.
69 */
70# define RUBY_EXPORT
71# endif
Bram Moolenaarf9b5ef82010-09-29 13:02:53 +020072
Bram Moolenaardace9f72020-12-28 15:07:45 +010073# if RUBY_VERSION >= 19
Bram Moolenaar2ab2e862019-12-04 21:24:53 +010074// Ruby 1.9 defines a number of static functions which use rb_num2long and
75// rb_int2big
Bram Moolenaardace9f72020-12-28 15:07:45 +010076# define rb_num2long rb_num2long_stub
77# define rb_int2big rb_int2big_stub
Bram Moolenaar42d57f02010-03-10 12:47:00 +010078
Bram Moolenaardace9f72020-12-28 15:07:45 +010079# if RUBY_VERSION >= 30 || VIM_SIZEOF_INT < VIM_SIZEOF_LONG
Bram Moolenaar2ab2e862019-12-04 21:24:53 +010080// Ruby 1.9 defines a number of static functions which use rb_fix2int and
81// rb_num2int if VIM_SIZEOF_INT < VIM_SIZEOF_LONG (64bit)
Bram Moolenaardace9f72020-12-28 15:07:45 +010082# define rb_fix2int rb_fix2int_stub
83# define rb_num2int rb_num2int_stub
84# endif
85# endif
Bram Moolenaar0bcdd6e2013-04-14 16:19:03 +020086
Bram Moolenaardace9f72020-12-28 15:07:45 +010087# if RUBY_VERSION == 21
Bram Moolenaar2ab2e862019-12-04 21:24:53 +010088// Ruby 2.1 adds new GC called RGenGC and RARRAY_PTR uses
89// rb_gc_writebarrier_unprotect_promoted if USE_RGENGC
Bram Moolenaardace9f72020-12-28 15:07:45 +010090# define rb_gc_writebarrier_unprotect_promoted rb_gc_writebarrier_unprotect_promoted_stub
91# endif
Bram Moolenaara1a118b2014-02-05 22:41:15 +010092
Bram Moolenaardace9f72020-12-28 15:07:45 +010093# if RUBY_VERSION >= 22
94# define rb_gc_writebarrier_unprotect rb_gc_writebarrier_unprotect_stub
95# endif
96
97# if RUBY_VERSION >= 26
98# define rb_ary_detransient rb_ary_detransient_stub
99# endif
100
101# if RUBY_VERSION >= 30
102# define rb_check_type rb_check_type_stub
103# define rb_num2uint rb_num2uint_stub
104# define ruby_malloc_size_overflow ruby_malloc_size_overflow_stub
105# endif
106
107#endif // ifdef DYNAMIC_RUBY
Bram Moolenaarb09c6842018-12-27 22:11:01 +0100108
Bram Moolenaar81ea1df2020-04-11 18:01:41 +0200109// On macOS pre-installed Ruby defines "SIZEOF_TIME_T" as "SIZEOF_LONG" so it
Bram Moolenaar92c098d2020-05-26 20:09:11 +0200110// conflicts with the definition in config.h then causes a macro-redefined
111// warning.
Bram Moolenaar81ea1df2020-04-11 18:01:41 +0200112#ifdef SIZEOF_TIME_T
113# undef SIZEOF_TIME_T
114#endif
115
Bram Moolenaar071d4272004-06-13 20:20:40 +0000116#include <ruby.h>
Bram Moolenaar41a41412020-01-07 21:32:19 +0100117#if RUBY_VERSION >= 19
Bram Moolenaar165641d2010-02-17 16:23:09 +0100118# include <ruby/encoding.h>
119#endif
Bram Moolenaar41a41412020-01-07 21:32:19 +0100120#if RUBY_VERSION <= 18
Bram Moolenaar8dc4c722019-04-14 19:42:13 +0200121# include <st.h> // for ST_STOP and ST_CONTINUE
122#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000123
Bram Moolenaar92c098d2020-05-26 20:09:11 +0200124// See above.
125#ifdef SIZEOF_TIME_T
126# undef SIZEOF_TIME_T
127#endif
128
Bram Moolenaar8dc4c722019-04-14 19:42:13 +0200129#undef off_t // ruby defines off_t as _int64, Mingw uses long
Bram Moolenaar071d4272004-06-13 20:20:40 +0000130#undef EXTERN
131#undef _
132
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100133// T_DATA defined both by Ruby and Mac header files, hack around it...
Bram Moolenaard0573012017-10-28 21:11:06 +0200134#if defined(MACOS_X)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000135# define __OPENTRANSPORT__
136# define __OPENTRANSPORTPROTOCOL__
137# define __OPENTRANSPORTPROVIDERS__
138#endif
139
Bram Moolenaar165641d2010-02-17 16:23:09 +0100140/*
Bram Moolenaar0d27f642015-12-28 22:05:28 +0100141 * The TypedData_XXX macro family can be used since Ruby 1.9.2 but
142 * rb_data_type_t changed in 1.9.3, therefore require at least 2.0.
143 * The old Data_XXX macro family was deprecated on Ruby 2.2.
Bram Moolenaarf2f6d292015-12-28 20:57:10 +0100144 * Use TypedData_XXX if available.
145 */
Bram Moolenaar41a41412020-01-07 21:32:19 +0100146#if defined(TypedData_Wrap_Struct) && (RUBY_VERSION >= 20)
Bram Moolenaarf2f6d292015-12-28 20:57:10 +0100147# define USE_TYPEDDATA 1
148#endif
149
150/*
Bram Moolenaar84a05ac2013-05-06 04:24:17 +0200151 * Backward compatibility for Ruby 1.8 and earlier.
Bram Moolenaar165641d2010-02-17 16:23:09 +0100152 * Ruby 1.9 does not provide STR2CSTR, instead StringValuePtr is provided.
153 * Ruby 1.9 does not provide RXXX(s)->len and RXXX(s)->ptr, instead
154 * RXXX_LEN(s) and RXXX_PTR(s) are provided.
155 */
156#ifndef StringValuePtr
157# define StringValuePtr(s) STR2CSTR(s)
158#endif
159#ifndef RARRAY_LEN
160# define RARRAY_LEN(s) RARRAY(s)->len
161#endif
162#ifndef RARRAY_PTR
163# define RARRAY_PTR(s) RARRAY(s)->ptr
164#endif
165#ifndef RSTRING_LEN
166# define RSTRING_LEN(s) RSTRING(s)->len
167#endif
168#ifndef RSTRING_PTR
169# define RSTRING_PTR(s) RSTRING(s)->ptr
170#endif
171
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100172#ifdef HAVE_DUP
173# undef HAVE_DUP
174#endif
175
Bram Moolenaar071d4272004-06-13 20:20:40 +0000176#include "vim.h"
177#include "version.h"
178
Bram Moolenaar7dca2eb2019-02-18 20:42:50 +0100179#ifdef DYNAMIC_RUBY
180# if !defined(MSWIN) // must come after including vim.h, where it is defined
181# include <dlfcn.h>
182# define HINSTANCE void*
183# define RUBY_PROC void*
184# define load_dll(n) dlopen((n), RTLD_LAZY|RTLD_GLOBAL)
185# define symbol_from_dll dlsym
186# define close_dll dlclose
187# else
188# define RUBY_PROC FARPROC
189# define load_dll vimLoadLib
190# define symbol_from_dll GetProcAddress
191# define close_dll FreeLibrary
192# endif
193#endif
194
Bram Moolenaar071d4272004-06-13 20:20:40 +0000195#if defined(PROTO) && !defined(FEAT_RUBY)
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100196// Define these to be able to generate the function prototypes.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000197# define VALUE int
198# define RUBY_DATA_FUNC int
199#endif
200
201static int ruby_initialized = 0;
Bram Moolenaar99685e62013-05-11 13:56:18 +0200202static void *ruby_stack_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000203static VALUE objtbl;
204
205static VALUE mVIM;
206static VALUE cBuffer;
207static VALUE cVimWindow;
208static VALUE eDeletedBufferError;
209static VALUE eDeletedWindowError;
210
211static int ensure_ruby_initialized(void);
212static void error_print(int);
213static void ruby_io_init(void);
214static void ruby_vim_init(void);
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100215static int ruby_convert_to_vim_value(VALUE val, typval_T *rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000216
Bram Moolenaar41a41412020-01-07 21:32:19 +0100217#if (RUBY_VERSION >= 19) || defined(RUBY_INIT_STACK)
Bram Moolenaar10f3a792013-05-20 12:52:29 +0200218# if defined(__ia64) && !defined(ruby_init_stack)
219# define ruby_init_stack(addr) ruby_init_stack((addr), rb_ia64_bsp())
220# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000221#endif
222
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200223#if defined(DYNAMIC_RUBY) || defined(PROTO)
Bram Moolenaaref269542016-01-19 13:22:12 +0100224# if defined(PROTO) && !defined(HINSTANCE)
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100225# define HINSTANCE int // for generating prototypes
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200226# endif
227
Bram Moolenaar071d4272004-06-13 20:20:40 +0000228/*
229 * Wrapper defines
230 */
Bram Moolenaar8b430b42020-02-22 15:01:00 +0100231// Ruby 2.7 actually expands the following symbols as macro.
232# if RUBY_VERSION >= 27
233# undef rb_define_global_function
234# undef rb_define_method
235# undef rb_define_module_function
236# undef rb_define_singleton_method
237# endif
238
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200239# define rb_assoc_new dll_rb_assoc_new
240# define rb_cObject (*dll_rb_cObject)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100241# define rb_class_new_instance dll_rb_class_new_instance
Bram Moolenaardace9f72020-12-28 15:07:45 +0100242# if RUBY_VERSION < 30
243# define rb_check_type dll_rb_check_type
244# endif
Bram Moolenaarf2f6d292015-12-28 20:57:10 +0100245# ifdef USE_TYPEDDATA
246# define rb_check_typeddata dll_rb_check_typeddata
Bram Moolenaar0c7485f2015-01-14 14:04:10 +0100247# endif
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200248# define rb_class_path dll_rb_class_path
Bram Moolenaarf2f6d292015-12-28 20:57:10 +0100249# ifdef USE_TYPEDDATA
Bram Moolenaar41a41412020-01-07 21:32:19 +0100250# if RUBY_VERSION >= 23
Bram Moolenaarf2f6d292015-12-28 20:57:10 +0100251# define rb_data_typed_object_wrap dll_rb_data_typed_object_wrap
252# else
253# define rb_data_typed_object_alloc dll_rb_data_typed_object_alloc
254# endif
255# else
256# define rb_data_object_alloc dll_rb_data_object_alloc
257# endif
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200258# define rb_define_class_under dll_rb_define_class_under
259# define rb_define_const dll_rb_define_const
260# define rb_define_global_function dll_rb_define_global_function
261# define rb_define_method dll_rb_define_method
262# define rb_define_module dll_rb_define_module
263# define rb_define_module_function dll_rb_define_module_function
264# define rb_define_singleton_method dll_rb_define_singleton_method
265# define rb_define_virtual_variable dll_rb_define_virtual_variable
266# define rb_stdout (*dll_rb_stdout)
Bram Moolenaarb6c8cd82018-07-24 05:41:30 +0200267# define rb_stderr (*dll_rb_stderr)
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200268# define rb_eArgError (*dll_rb_eArgError)
269# define rb_eIndexError (*dll_rb_eIndexError)
270# define rb_eRuntimeError (*dll_rb_eRuntimeError)
271# define rb_eStandardError (*dll_rb_eStandardError)
272# define rb_eval_string_protect dll_rb_eval_string_protect
Bram Moolenaar41a41412020-01-07 21:32:19 +0100273# if RUBY_VERSION >= 21
Bram Moolenaarf711cb22018-08-01 18:42:13 +0200274# define rb_funcallv dll_rb_funcallv
275# else
276# define rb_funcall2 dll_rb_funcall2
277# endif
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200278# define rb_global_variable dll_rb_global_variable
279# define rb_hash_aset dll_rb_hash_aset
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100280# define rb_hash_foreach dll_rb_hash_foreach
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200281# define rb_hash_new dll_rb_hash_new
282# define rb_inspect dll_rb_inspect
283# define rb_int2inum dll_rb_int2inum
Bram Moolenaar218beb32018-08-04 17:24:44 +0200284
285// ruby.h may redefine rb_intern to use RUBY_CONST_ID_CACHE(), but that won't
286// work. Not using the cache appears to be the best solution.
287# undef rb_intern
288# define rb_intern dll_rb_intern
289
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100290# if VIM_SIZEOF_INT < VIM_SIZEOF_LONG // 64 bits only
Bram Moolenaar41a41412020-01-07 21:32:19 +0100291# if RUBY_VERSION <= 18
Bram Moolenaar498af702014-03-28 21:58:21 +0100292# define rb_fix2int dll_rb_fix2int
293# define rb_num2int dll_rb_num2int
294# endif
Bram Moolenaardace9f72020-12-28 15:07:45 +0100295# if RUBY_VERSION < 30
296# define rb_num2uint dll_rb_num2uint
297# endif
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200298# endif
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100299# define rb_num2dbl dll_rb_num2dbl
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200300# define rb_lastline_get dll_rb_lastline_get
301# define rb_lastline_set dll_rb_lastline_set
Bram Moolenaard0573012017-10-28 21:11:06 +0200302# define rb_protect dll_rb_protect
303# define rb_load dll_rb_load
Bram Moolenaar41a41412020-01-07 21:32:19 +0100304# if RUBY_VERSION <= 18
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200305# define rb_num2long dll_rb_num2long
306# endif
Bram Moolenaar41a41412020-01-07 21:32:19 +0100307# if RUBY_VERSION <= 19
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200308# define rb_num2ulong dll_rb_num2ulong
309# endif
310# define rb_obj_alloc dll_rb_obj_alloc
311# define rb_obj_as_string dll_rb_obj_as_string
312# define rb_obj_id dll_rb_obj_id
313# define rb_raise dll_rb_raise
314# define rb_str_cat dll_rb_str_cat
315# define rb_str_concat dll_rb_str_concat
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100316# undef rb_str_new
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200317# define rb_str_new dll_rb_str_new
318# ifdef rb_str_new2
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100319// Ruby may #define rb_str_new2 to use rb_str_new_cstr.
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200320# define need_rb_str_new_cstr 1
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100321// Ruby's headers #define rb_str_new_cstr to make use of GCC's
322// __builtin_constant_p extension.
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200323# undef rb_str_new_cstr
324# define rb_str_new_cstr dll_rb_str_new_cstr
Bram Moolenaar76a86062013-05-11 17:45:48 +0200325# else
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200326# define rb_str_new2 dll_rb_str_new2
Bram Moolenaar76a86062013-05-11 17:45:48 +0200327# endif
Bram Moolenaar41a41412020-01-07 21:32:19 +0100328# if RUBY_VERSION >= 18
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200329# define rb_string_value dll_rb_string_value
330# define rb_string_value_ptr dll_rb_string_value_ptr
331# define rb_float_new dll_rb_float_new
332# define rb_ary_new dll_rb_ary_new
Bram Moolenaar51e9fbf2018-08-11 14:24:11 +0200333# ifdef rb_ary_new4
Bram Moolenaar49c99fc2020-02-11 23:01:39 +0100334# define RB_ARY_NEW4_MACRO 1
335# undef rb_ary_new4
Bram Moolenaar51e9fbf2018-08-11 14:24:11 +0200336# endif
337# define rb_ary_new4 dll_rb_ary_new4
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200338# define rb_ary_push dll_rb_ary_push
Bram Moolenaar41a41412020-01-07 21:32:19 +0100339# if (RUBY_VERSION >= 19) || defined(RUBY_INIT_STACK)
Bram Moolenaar10f3a792013-05-20 12:52:29 +0200340# ifdef __ia64
341# define rb_ia64_bsp dll_rb_ia64_bsp
342# undef ruby_init_stack
343# define ruby_init_stack(addr) dll_ruby_init_stack((addr), rb_ia64_bsp())
344# else
345# define ruby_init_stack dll_ruby_init_stack
346# endif
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200347# endif
348# else
349# define rb_str2cstr dll_rb_str2cstr
Bram Moolenaar3ca71f12010-10-27 16:49:47 +0200350# endif
Bram Moolenaar41a41412020-01-07 21:32:19 +0100351# if RUBY_VERSION >= 19
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200352# define rb_errinfo dll_rb_errinfo
353# else
354# define ruby_errinfo (*dll_ruby_errinfo)
355# endif
356# define ruby_init dll_ruby_init
357# define ruby_init_loadpath dll_ruby_init_loadpath
Bram Moolenaar4f974752019-02-17 17:44:42 +0100358# ifdef MSWIN
Bram Moolenaar41a41412020-01-07 21:32:19 +0100359# if RUBY_VERSION >= 19
Bram Moolenaar9d5c84a2018-12-19 20:48:46 +0100360# define ruby_sysinit dll_ruby_sysinit
361# else
362# define NtInitialize dll_NtInitialize
363# endif
Bram Moolenaar41a41412020-01-07 21:32:19 +0100364# if RUBY_VERSION >= 18
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200365# define rb_w32_snprintf dll_rb_w32_snprintf
366# endif
367# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000368
Bram Moolenaar41a41412020-01-07 21:32:19 +0100369# if RUBY_VERSION >= 19
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200370# define ruby_script dll_ruby_script
371# define rb_enc_find_index dll_rb_enc_find_index
372# define rb_enc_find dll_rb_enc_find
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100373# undef rb_enc_str_new
374# define rb_enc_str_new dll_rb_enc_str_new
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200375# define rb_sprintf dll_rb_sprintf
376# define rb_require dll_rb_require
Bram Moolenaar9b1067e2015-11-19 19:33:15 +0100377# define ruby_options dll_ruby_options
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200378# endif
Bram Moolenaar165641d2010-02-17 16:23:09 +0100379
Bram Moolenaar071d4272004-06-13 20:20:40 +0000380/*
381 * Pointers for dynamic link
382 */
383static VALUE (*dll_rb_assoc_new) (VALUE, VALUE);
Bram Moolenaarba52cde2010-06-25 04:29:11 +0200384VALUE *dll_rb_cFalseClass;
385VALUE *dll_rb_cFixnum;
Bram Moolenaar2016ae52016-06-13 20:08:43 +0200386# if defined(USE_RUBY_INTEGER)
Bram Moolenaar06469e92016-06-11 22:26:53 +0200387VALUE *dll_rb_cInteger;
388# endif
Bram Moolenaar41a41412020-01-07 21:32:19 +0100389# if RUBY_VERSION >= 20
Bram Moolenaardb3fbe52013-03-07 15:16:21 +0100390VALUE *dll_rb_cFloat;
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200391# endif
Bram Moolenaarba52cde2010-06-25 04:29:11 +0200392VALUE *dll_rb_cNilClass;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000393static VALUE *dll_rb_cObject;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100394VALUE *dll_rb_cString;
Bram Moolenaarba52cde2010-06-25 04:29:11 +0200395VALUE *dll_rb_cSymbol;
396VALUE *dll_rb_cTrueClass;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100397static VALUE (*dll_rb_class_new_instance) (int,VALUE*,VALUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000398static void (*dll_rb_check_type) (VALUE,int);
Bram Moolenaarf2f6d292015-12-28 20:57:10 +0100399# ifdef USE_TYPEDDATA
400static void *(*dll_rb_check_typeddata) (VALUE,const rb_data_type_t *);
401# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000402static VALUE (*dll_rb_class_path) (VALUE);
Bram Moolenaarf2f6d292015-12-28 20:57:10 +0100403# ifdef USE_TYPEDDATA
Bram Moolenaar41a41412020-01-07 21:32:19 +0100404# if RUBY_VERSION >= 23
Bram Moolenaarf2f6d292015-12-28 20:57:10 +0100405static VALUE (*dll_rb_data_typed_object_wrap) (VALUE, void*, const rb_data_type_t *);
406# else
407static VALUE (*dll_rb_data_typed_object_alloc) (VALUE, void*, const rb_data_type_t *);
408# endif
409# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000410static VALUE (*dll_rb_data_object_alloc) (VALUE, void*, RUBY_DATA_FUNC, RUBY_DATA_FUNC);
Bram Moolenaarf2f6d292015-12-28 20:57:10 +0100411# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000412static VALUE (*dll_rb_define_class_under) (VALUE, const char*, VALUE);
413static void (*dll_rb_define_const) (VALUE,const char*,VALUE);
414static void (*dll_rb_define_global_function) (const char*,VALUE(*)(),int);
415static void (*dll_rb_define_method) (VALUE,const char*,VALUE(*)(),int);
416static VALUE (*dll_rb_define_module) (const char*);
417static void (*dll_rb_define_module_function) (VALUE,const char*,VALUE(*)(),int);
418static void (*dll_rb_define_singleton_method) (VALUE,const char*,VALUE(*)(),int);
419static void (*dll_rb_define_virtual_variable) (const char*,VALUE(*)(),void(*)());
420static VALUE *dll_rb_stdout;
Bram Moolenaarb6c8cd82018-07-24 05:41:30 +0200421static VALUE *dll_rb_stderr;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000422static VALUE *dll_rb_eArgError;
423static VALUE *dll_rb_eIndexError;
424static VALUE *dll_rb_eRuntimeError;
425static VALUE *dll_rb_eStandardError;
426static VALUE (*dll_rb_eval_string_protect) (const char*, int*);
Bram Moolenaar41a41412020-01-07 21:32:19 +0100427# if RUBY_VERSION >= 21
Bram Moolenaarf711cb22018-08-01 18:42:13 +0200428static VALUE (*dll_rb_funcallv) (VALUE, ID, int, const VALUE*);
429# else
430static VALUE (*dll_rb_funcall2) (VALUE, ID, int, const VALUE*);
431# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000432static void (*dll_rb_global_variable) (VALUE*);
433static VALUE (*dll_rb_hash_aset) (VALUE, VALUE, VALUE);
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100434static VALUE (*dll_rb_hash_foreach) (VALUE, int (*)(VALUE, VALUE, VALUE), VALUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000435static VALUE (*dll_rb_hash_new) (void);
436static VALUE (*dll_rb_inspect) (VALUE);
437static VALUE (*dll_rb_int2inum) (long);
Bram Moolenaarf711cb22018-08-01 18:42:13 +0200438static ID (*dll_rb_intern) (const char*);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100439# if VIM_SIZEOF_INT < VIM_SIZEOF_LONG // 64 bits only
Bram Moolenaar2623b4f2012-09-18 16:36:32 +0200440static long (*dll_rb_fix2int) (VALUE);
441static long (*dll_rb_num2int) (VALUE);
442static unsigned long (*dll_rb_num2uint) (VALUE);
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200443# endif
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100444static double (*dll_rb_num2dbl) (VALUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000445static VALUE (*dll_rb_lastline_get) (void);
446static void (*dll_rb_lastline_set) (VALUE);
Bram Moolenaar37badc82018-01-31 20:15:30 +0100447static VALUE (*dll_rb_protect) (VALUE (*)(VALUE), VALUE, int*);
Bram Moolenaard0573012017-10-28 21:11:06 +0200448static void (*dll_rb_load) (VALUE, int);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000449static long (*dll_rb_num2long) (VALUE);
450static unsigned long (*dll_rb_num2ulong) (VALUE);
451static VALUE (*dll_rb_obj_alloc) (VALUE);
452static VALUE (*dll_rb_obj_as_string) (VALUE);
453static VALUE (*dll_rb_obj_id) (VALUE);
454static void (*dll_rb_raise) (VALUE, const char*, ...);
Bram Moolenaar41a41412020-01-07 21:32:19 +0100455# if RUBY_VERSION >= 18
Bram Moolenaarf9b5ef82010-09-29 13:02:53 +0200456static VALUE (*dll_rb_string_value) (volatile VALUE*);
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200457# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000458static char *(*dll_rb_str2cstr) (VALUE,int*);
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200459# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000460static VALUE (*dll_rb_str_cat) (VALUE, const char*, long);
461static VALUE (*dll_rb_str_concat) (VALUE, VALUE);
462static VALUE (*dll_rb_str_new) (const char*, long);
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200463# ifdef need_rb_str_new_cstr
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100464// Ruby may #define rb_str_new2 to use rb_str_new_cstr.
Bram Moolenaar1d2beae2010-05-22 21:56:55 +0200465static VALUE (*dll_rb_str_new_cstr) (const char*);
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200466# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000467static VALUE (*dll_rb_str_new2) (const char*);
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200468# endif
Bram Moolenaar41a41412020-01-07 21:32:19 +0100469# if RUBY_VERSION >= 19
Bram Moolenaar165641d2010-02-17 16:23:09 +0100470static VALUE (*dll_rb_errinfo) (void);
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200471# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000472static VALUE *dll_ruby_errinfo;
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200473# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000474static void (*dll_ruby_init) (void);
475static void (*dll_ruby_init_loadpath) (void);
Bram Moolenaar4f974752019-02-17 17:44:42 +0100476# ifdef MSWIN
Bram Moolenaar41a41412020-01-07 21:32:19 +0100477# if RUBY_VERSION >= 19
Bram Moolenaar4f391792017-01-15 16:59:07 +0100478static void (*dll_ruby_sysinit) (int*, char***);
Bram Moolenaar9d5c84a2018-12-19 20:48:46 +0100479# else
480static void (*dll_NtInitialize) (int*, char***);
481# endif
Bram Moolenaar41a41412020-01-07 21:32:19 +0100482# if RUBY_VERSION >= 18
Bram Moolenaar3ca71f12010-10-27 16:49:47 +0200483static int (*dll_rb_w32_snprintf)(char*, size_t, const char*, ...);
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200484# endif
Bram Moolenaar3ca71f12010-10-27 16:49:47 +0200485# endif
Bram Moolenaar41a41412020-01-07 21:32:19 +0100486# if RUBY_VERSION >= 18
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100487static char * (*dll_rb_string_value_ptr) (volatile VALUE*);
488static VALUE (*dll_rb_float_new) (double);
489static VALUE (*dll_rb_ary_new) (void);
Bram Moolenaar51e9fbf2018-08-11 14:24:11 +0200490static VALUE (*dll_rb_ary_new4) (long n, const VALUE *elts);
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100491static VALUE (*dll_rb_ary_push) (VALUE, VALUE);
Bram Moolenaar41a41412020-01-07 21:32:19 +0100492# if RUBY_VERSION >= 26
Bram Moolenaarb09c6842018-12-27 22:11:01 +0100493static void (*dll_rb_ary_detransient) (VALUE);
494# endif
Bram Moolenaar41a41412020-01-07 21:32:19 +0100495# if (RUBY_VERSION >= 19) || defined(RUBY_INIT_STACK)
Bram Moolenaar10f3a792013-05-20 12:52:29 +0200496# ifdef __ia64
Bram Moolenaar76a86062013-05-11 17:45:48 +0200497static void * (*dll_rb_ia64_bsp) (void);
498static void (*dll_ruby_init_stack)(VALUE*, void*);
Bram Moolenaar10f3a792013-05-20 12:52:29 +0200499# else
Bram Moolenaar76a86062013-05-11 17:45:48 +0200500static void (*dll_ruby_init_stack)(VALUE*);
Bram Moolenaar10f3a792013-05-20 12:52:29 +0200501# endif
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200502# endif
Bram Moolenaar76a86062013-05-11 17:45:48 +0200503# endif
Bram Moolenaar41a41412020-01-07 21:32:19 +0100504# if RUBY_VERSION >= 19
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100505static VALUE (*dll_rb_int2big)(SIGNED_VALUE);
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200506# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000507
Bram Moolenaar41a41412020-01-07 21:32:19 +0100508# if RUBY_VERSION >= 19
Bram Moolenaar165641d2010-02-17 16:23:09 +0100509static void (*dll_ruby_script) (const char*);
510static int (*dll_rb_enc_find_index) (const char*);
511static rb_encoding* (*dll_rb_enc_find) (const char*);
512static VALUE (*dll_rb_enc_str_new) (const char*, long, rb_encoding*);
513static VALUE (*dll_rb_sprintf) (const char*, ...);
Bram Moolenaar7a8ef142010-12-24 13:39:35 +0100514static VALUE (*dll_rb_require) (const char*);
Bram Moolenaardace9f72020-12-28 15:07:45 +0100515static void* (*dll_ruby_options)(int, char**);
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200516# endif
Bram Moolenaar165641d2010-02-17 16:23:09 +0100517
Bram Moolenaara1a118b2014-02-05 22:41:15 +0100518# if defined(USE_RGENGC) && USE_RGENGC
Bram Moolenaar41a41412020-01-07 21:32:19 +0100519# if RUBY_VERSION == 21
Bram Moolenaara1a118b2014-02-05 22:41:15 +0100520static void (*dll_rb_gc_writebarrier_unprotect_promoted)(VALUE);
Bram Moolenaar0c7485f2015-01-14 14:04:10 +0100521# else
522static void (*dll_rb_gc_writebarrier_unprotect)(VALUE obj);
523# endif
Bram Moolenaara1a118b2014-02-05 22:41:15 +0100524# endif
525
Bram Moolenaardace9f72020-12-28 15:07:45 +0100526# if RUBY_VERSION >= 30
527NORETURN(static void (*dll_ruby_malloc_size_overflow)(size_t, size_t));
528# endif
529
Bram Moolenaar0e121402020-12-10 20:50:34 +0100530# if RUBY_VERSION >= 26
531void rb_ary_detransient_stub(VALUE x);
532# endif
533
Bram Moolenaardace9f72020-12-28 15:07:45 +0100534// Do not generate a prototype here, VALUE isn't always defined.
535# ifndef PROTO
536# if RUBY_VERSION >= 19
537# if RUBY_VERSION >= 22
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100538 long
539rb_num2long_stub(VALUE x)
Bram Moolenaardace9f72020-12-28 15:07:45 +0100540# else
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100541 SIGNED_VALUE
542rb_num2long_stub(VALUE x)
Bram Moolenaardace9f72020-12-28 15:07:45 +0100543# endif
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100544{
545 return dll_rb_num2long(x);
546}
Bram Moolenaardace9f72020-12-28 15:07:45 +0100547# if RUBY_VERSION >= 26
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100548 VALUE
549rb_int2big_stub(intptr_t x)
Bram Moolenaardace9f72020-12-28 15:07:45 +0100550# else
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100551 VALUE
552rb_int2big_stub(SIGNED_VALUE x)
Bram Moolenaardace9f72020-12-28 15:07:45 +0100553# endif
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100554{
555 return dll_rb_int2big(x);
556}
Bram Moolenaardace9f72020-12-28 15:07:45 +0100557# if VIM_SIZEOF_INT < VIM_SIZEOF_LONG
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100558 long
559rb_fix2int_stub(VALUE x)
Bram Moolenaar0bcdd6e2013-04-14 16:19:03 +0200560{
561 return dll_rb_fix2int(x);
562}
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100563 long
564rb_num2int_stub(VALUE x)
Bram Moolenaar0bcdd6e2013-04-14 16:19:03 +0200565{
566 return dll_rb_num2int(x);
567}
Bram Moolenaardace9f72020-12-28 15:07:45 +0100568# endif
569# if RUBY_VERSION >= 20
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100570 VALUE
Bram Moolenaar886ed692013-02-26 13:41:35 +0100571rb_float_new_in_heap(double d)
572{
573 return dll_rb_float_new(d);
574}
Bram Moolenaardace9f72020-12-28 15:07:45 +0100575# if RUBY_VERSION >= 22
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100576 unsigned long
577rb_num2ulong(VALUE x)
Bram Moolenaardace9f72020-12-28 15:07:45 +0100578# else
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100579 VALUE
580rb_num2ulong(VALUE x)
Bram Moolenaardace9f72020-12-28 15:07:45 +0100581# endif
Bram Moolenaar886ed692013-02-26 13:41:35 +0100582{
583 return (long)RSHIFT((SIGNED_VALUE)(x),1);
584}
Bram Moolenaardace9f72020-12-28 15:07:45 +0100585# endif
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200586# endif
Bram Moolenaardace9f72020-12-28 15:07:45 +0100587# if defined(USE_RGENGC) && USE_RGENGC
588# if RUBY_VERSION == 21
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100589 void
590rb_gc_writebarrier_unprotect_promoted_stub(VALUE obj)
Bram Moolenaara1a118b2014-02-05 22:41:15 +0100591{
Bram Moolenaar90140742014-11-27 17:44:08 +0100592 dll_rb_gc_writebarrier_unprotect_promoted(obj);
Bram Moolenaara1a118b2014-02-05 22:41:15 +0100593}
Bram Moolenaardace9f72020-12-28 15:07:45 +0100594# else
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100595 void
596rb_gc_writebarrier_unprotect_stub(VALUE obj)
Bram Moolenaar0c7485f2015-01-14 14:04:10 +0100597{
598 dll_rb_gc_writebarrier_unprotect(obj);
599}
Bram Moolenaardace9f72020-12-28 15:07:45 +0100600# endif
Bram Moolenaar0c7485f2015-01-14 14:04:10 +0100601# endif
Bram Moolenaardace9f72020-12-28 15:07:45 +0100602# if RUBY_VERSION >= 26
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100603 void
604rb_ary_detransient_stub(VALUE x)
Bram Moolenaarf62fc312019-01-08 20:29:32 +0100605{
606 dll_rb_ary_detransient(x);
607}
Bram Moolenaardace9f72020-12-28 15:07:45 +0100608# endif
609# if RUBY_VERSION >= 30
610 void
611rb_check_type_stub(VALUE obj, int t)
612{
613 dll_rb_check_type(obj, t);
614}
615 unsigned long
616rb_num2uint_stub(VALUE x)
617{
618 return dll_rb_num2uint(x);
619}
620 void
621ruby_malloc_size_overflow_stub(size_t x, size_t y)
622{
623 dll_ruby_malloc_size_overflow(x, y);
624}
625# endif
626# endif // ifndef PROTO
Bram Moolenaarf62fc312019-01-08 20:29:32 +0100627
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100628static HINSTANCE hinstRuby = NULL; // Instance of ruby.dll
Bram Moolenaar071d4272004-06-13 20:20:40 +0000629
630/*
Bram Moolenaarda2303d2005-08-30 21:55:26 +0000631 * Table of name to function pointer of ruby.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000632 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000633static struct
634{
635 char *name;
636 RUBY_PROC *ptr;
637} ruby_funcname_table[] =
638{
639 {"rb_assoc_new", (RUBY_PROC*)&dll_rb_assoc_new},
640 {"rb_cFalseClass", (RUBY_PROC*)&dll_rb_cFalseClass},
Bram Moolenaar2016ae52016-06-13 20:08:43 +0200641# if defined(USE_RUBY_INTEGER)
Bram Moolenaar06469e92016-06-11 22:26:53 +0200642 {"rb_cInteger", (RUBY_PROC*)&dll_rb_cInteger},
Bram Moolenaar6abda992017-01-09 21:10:31 +0100643# else
644 {"rb_cFixnum", (RUBY_PROC*)&dll_rb_cFixnum},
Bram Moolenaar06469e92016-06-11 22:26:53 +0200645# endif
Bram Moolenaar41a41412020-01-07 21:32:19 +0100646# if RUBY_VERSION >= 20
Bram Moolenaardb3fbe52013-03-07 15:16:21 +0100647 {"rb_cFloat", (RUBY_PROC*)&dll_rb_cFloat},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200648# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000649 {"rb_cNilClass", (RUBY_PROC*)&dll_rb_cNilClass},
650 {"rb_cObject", (RUBY_PROC*)&dll_rb_cObject},
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100651 {"rb_cString", (RUBY_PROC*)&dll_rb_cString},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000652 {"rb_cSymbol", (RUBY_PROC*)&dll_rb_cSymbol},
653 {"rb_cTrueClass", (RUBY_PROC*)&dll_rb_cTrueClass},
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100654 {"rb_class_new_instance", (RUBY_PROC*)&dll_rb_class_new_instance},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000655 {"rb_check_type", (RUBY_PROC*)&dll_rb_check_type},
Bram Moolenaarf2f6d292015-12-28 20:57:10 +0100656# ifdef USE_TYPEDDATA
657 {"rb_check_typeddata", (RUBY_PROC*)&dll_rb_check_typeddata},
658# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000659 {"rb_class_path", (RUBY_PROC*)&dll_rb_class_path},
Bram Moolenaarf2f6d292015-12-28 20:57:10 +0100660# ifdef USE_TYPEDDATA
Bram Moolenaar41a41412020-01-07 21:32:19 +0100661# if RUBY_VERSION >= 23
Bram Moolenaarf2f6d292015-12-28 20:57:10 +0100662 {"rb_data_typed_object_wrap", (RUBY_PROC*)&dll_rb_data_typed_object_wrap},
663# else
664 {"rb_data_typed_object_alloc", (RUBY_PROC*)&dll_rb_data_typed_object_alloc},
665# endif
666# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000667 {"rb_data_object_alloc", (RUBY_PROC*)&dll_rb_data_object_alloc},
Bram Moolenaarf2f6d292015-12-28 20:57:10 +0100668# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000669 {"rb_define_class_under", (RUBY_PROC*)&dll_rb_define_class_under},
670 {"rb_define_const", (RUBY_PROC*)&dll_rb_define_const},
671 {"rb_define_global_function", (RUBY_PROC*)&dll_rb_define_global_function},
672 {"rb_define_method", (RUBY_PROC*)&dll_rb_define_method},
673 {"rb_define_module", (RUBY_PROC*)&dll_rb_define_module},
674 {"rb_define_module_function", (RUBY_PROC*)&dll_rb_define_module_function},
675 {"rb_define_singleton_method", (RUBY_PROC*)&dll_rb_define_singleton_method},
676 {"rb_define_virtual_variable", (RUBY_PROC*)&dll_rb_define_virtual_variable},
677 {"rb_stdout", (RUBY_PROC*)&dll_rb_stdout},
Bram Moolenaarb6c8cd82018-07-24 05:41:30 +0200678 {"rb_stderr", (RUBY_PROC*)&dll_rb_stderr},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000679 {"rb_eArgError", (RUBY_PROC*)&dll_rb_eArgError},
680 {"rb_eIndexError", (RUBY_PROC*)&dll_rb_eIndexError},
681 {"rb_eRuntimeError", (RUBY_PROC*)&dll_rb_eRuntimeError},
682 {"rb_eStandardError", (RUBY_PROC*)&dll_rb_eStandardError},
683 {"rb_eval_string_protect", (RUBY_PROC*)&dll_rb_eval_string_protect},
Bram Moolenaar41a41412020-01-07 21:32:19 +0100684# if RUBY_VERSION >= 21
Bram Moolenaarf711cb22018-08-01 18:42:13 +0200685 {"rb_funcallv", (RUBY_PROC*)&dll_rb_funcallv},
686# else
687 {"rb_funcall2", (RUBY_PROC*)&dll_rb_funcall2},
688# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000689 {"rb_global_variable", (RUBY_PROC*)&dll_rb_global_variable},
690 {"rb_hash_aset", (RUBY_PROC*)&dll_rb_hash_aset},
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100691 {"rb_hash_foreach", (RUBY_PROC*)&dll_rb_hash_foreach},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000692 {"rb_hash_new", (RUBY_PROC*)&dll_rb_hash_new},
693 {"rb_inspect", (RUBY_PROC*)&dll_rb_inspect},
694 {"rb_int2inum", (RUBY_PROC*)&dll_rb_int2inum},
Bram Moolenaarf711cb22018-08-01 18:42:13 +0200695 {"rb_intern", (RUBY_PROC*)&dll_rb_intern},
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100696# if VIM_SIZEOF_INT < VIM_SIZEOF_LONG // 64 bits only
Bram Moolenaar2623b4f2012-09-18 16:36:32 +0200697 {"rb_fix2int", (RUBY_PROC*)&dll_rb_fix2int},
698 {"rb_num2int", (RUBY_PROC*)&dll_rb_num2int},
699 {"rb_num2uint", (RUBY_PROC*)&dll_rb_num2uint},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200700# endif
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100701 {"rb_num2dbl", (RUBY_PROC*)&dll_rb_num2dbl},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000702 {"rb_lastline_get", (RUBY_PROC*)&dll_rb_lastline_get},
703 {"rb_lastline_set", (RUBY_PROC*)&dll_rb_lastline_set},
Bram Moolenaard0573012017-10-28 21:11:06 +0200704 {"rb_protect", (RUBY_PROC*)&dll_rb_protect},
705 {"rb_load", (RUBY_PROC*)&dll_rb_load},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000706 {"rb_num2long", (RUBY_PROC*)&dll_rb_num2long},
707 {"rb_num2ulong", (RUBY_PROC*)&dll_rb_num2ulong},
708 {"rb_obj_alloc", (RUBY_PROC*)&dll_rb_obj_alloc},
709 {"rb_obj_as_string", (RUBY_PROC*)&dll_rb_obj_as_string},
710 {"rb_obj_id", (RUBY_PROC*)&dll_rb_obj_id},
711 {"rb_raise", (RUBY_PROC*)&dll_rb_raise},
Bram Moolenaar41a41412020-01-07 21:32:19 +0100712# if RUBY_VERSION >= 18
Bram Moolenaarf9b5ef82010-09-29 13:02:53 +0200713 {"rb_string_value", (RUBY_PROC*)&dll_rb_string_value},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200714# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000715 {"rb_str2cstr", (RUBY_PROC*)&dll_rb_str2cstr},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200716# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000717 {"rb_str_cat", (RUBY_PROC*)&dll_rb_str_cat},
718 {"rb_str_concat", (RUBY_PROC*)&dll_rb_str_concat},
719 {"rb_str_new", (RUBY_PROC*)&dll_rb_str_new},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200720# ifdef need_rb_str_new_cstr
Bram Moolenaar1d2beae2010-05-22 21:56:55 +0200721 {"rb_str_new_cstr", (RUBY_PROC*)&dll_rb_str_new_cstr},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200722# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000723 {"rb_str_new2", (RUBY_PROC*)&dll_rb_str_new2},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200724# endif
Bram Moolenaar41a41412020-01-07 21:32:19 +0100725# if RUBY_VERSION >= 19
Bram Moolenaar165641d2010-02-17 16:23:09 +0100726 {"rb_errinfo", (RUBY_PROC*)&dll_rb_errinfo},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200727# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000728 {"ruby_errinfo", (RUBY_PROC*)&dll_ruby_errinfo},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200729# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000730 {"ruby_init", (RUBY_PROC*)&dll_ruby_init},
731 {"ruby_init_loadpath", (RUBY_PROC*)&dll_ruby_init_loadpath},
Bram Moolenaar4f974752019-02-17 17:44:42 +0100732# ifdef MSWIN
Bram Moolenaar41a41412020-01-07 21:32:19 +0100733# if RUBY_VERSION >= 19
Bram Moolenaar4f391792017-01-15 16:59:07 +0100734 {"ruby_sysinit", (RUBY_PROC*)&dll_ruby_sysinit},
Bram Moolenaar9d5c84a2018-12-19 20:48:46 +0100735# else
736 {"NtInitialize", (RUBY_PROC*)&dll_NtInitialize},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200737# endif
Bram Moolenaar41a41412020-01-07 21:32:19 +0100738# if RUBY_VERSION >= 18
Bram Moolenaar071d4272004-06-13 20:20:40 +0000739 {"rb_w32_snprintf", (RUBY_PROC*)&dll_rb_w32_snprintf},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200740# endif
Bram Moolenaar3ca71f12010-10-27 16:49:47 +0200741# endif
Bram Moolenaar41a41412020-01-07 21:32:19 +0100742# if RUBY_VERSION >= 18
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100743 {"rb_string_value_ptr", (RUBY_PROC*)&dll_rb_string_value_ptr},
Bram Moolenaar41a41412020-01-07 21:32:19 +0100744# if RUBY_VERSION <= 19
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100745 {"rb_float_new", (RUBY_PROC*)&dll_rb_float_new},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200746# else
Bram Moolenaar886ed692013-02-26 13:41:35 +0100747 {"rb_float_new_in_heap", (RUBY_PROC*)&dll_rb_float_new},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200748# endif
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100749 {"rb_ary_new", (RUBY_PROC*)&dll_rb_ary_new},
Bram Moolenaar51e9fbf2018-08-11 14:24:11 +0200750# ifdef RB_ARY_NEW4_MACRO
751 {"rb_ary_new_from_values", (RUBY_PROC*)&dll_rb_ary_new4},
752# else
753 {"rb_ary_new4", (RUBY_PROC*)&dll_rb_ary_new4},
754# endif
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100755 {"rb_ary_push", (RUBY_PROC*)&dll_rb_ary_push},
Bram Moolenaar41a41412020-01-07 21:32:19 +0100756# if RUBY_VERSION >= 26
Bram Moolenaarb09c6842018-12-27 22:11:01 +0100757 {"rb_ary_detransient", (RUBY_PROC*)&dll_rb_ary_detransient},
758# endif
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200759# endif
Bram Moolenaar41a41412020-01-07 21:32:19 +0100760# if RUBY_VERSION >= 19
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100761 {"rb_int2big", (RUBY_PROC*)&dll_rb_int2big},
Bram Moolenaar165641d2010-02-17 16:23:09 +0100762 {"ruby_script", (RUBY_PROC*)&dll_ruby_script},
763 {"rb_enc_find_index", (RUBY_PROC*)&dll_rb_enc_find_index},
764 {"rb_enc_find", (RUBY_PROC*)&dll_rb_enc_find},
765 {"rb_enc_str_new", (RUBY_PROC*)&dll_rb_enc_str_new},
766 {"rb_sprintf", (RUBY_PROC*)&dll_rb_sprintf},
Bram Moolenaar7a8ef142010-12-24 13:39:35 +0100767 {"rb_require", (RUBY_PROC*)&dll_rb_require},
Bram Moolenaar9b1067e2015-11-19 19:33:15 +0100768 {"ruby_options", (RUBY_PROC*)&dll_ruby_options},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200769# endif
Bram Moolenaar41a41412020-01-07 21:32:19 +0100770# if (RUBY_VERSION >= 19) || defined(RUBY_INIT_STACK)
Bram Moolenaar10f3a792013-05-20 12:52:29 +0200771# ifdef __ia64
772 {"rb_ia64_bsp", (RUBY_PROC*)&dll_rb_ia64_bsp},
773# endif
774 {"ruby_init_stack", (RUBY_PROC*)&dll_ruby_init_stack},
775# endif
Bram Moolenaara1a118b2014-02-05 22:41:15 +0100776# if defined(USE_RGENGC) && USE_RGENGC
Bram Moolenaar41a41412020-01-07 21:32:19 +0100777# if RUBY_VERSION == 21
Bram Moolenaara1a118b2014-02-05 22:41:15 +0100778 {"rb_gc_writebarrier_unprotect_promoted", (RUBY_PROC*)&dll_rb_gc_writebarrier_unprotect_promoted},
Bram Moolenaar0c7485f2015-01-14 14:04:10 +0100779# else
780 {"rb_gc_writebarrier_unprotect", (RUBY_PROC*)&dll_rb_gc_writebarrier_unprotect},
781# endif
Bram Moolenaara1a118b2014-02-05 22:41:15 +0100782# endif
Bram Moolenaardace9f72020-12-28 15:07:45 +0100783# if RUBY_VERSION >= 30
784 {"ruby_malloc_size_overflow", (RUBY_PROC*)&dll_ruby_malloc_size_overflow},
785# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000786 {"", NULL},
787};
788
789/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000790 * Load library and get all pointers.
791 * Parameter 'libname' provides name of DLL.
792 * Return OK or FAIL.
793 */
794 static int
795ruby_runtime_link_init(char *libname, int verbose)
796{
797 int i;
798
799 if (hinstRuby)
800 return OK;
Bram Moolenaarf9b5ef82010-09-29 13:02:53 +0200801 hinstRuby = load_dll(libname);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000802 if (!hinstRuby)
803 {
804 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100805 semsg(_(e_loadlib), libname);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000806 return FAIL;
807 }
808
809 for (i = 0; ruby_funcname_table[i].ptr; ++i)
810 {
Bram Moolenaarf9b5ef82010-09-29 13:02:53 +0200811 if (!(*ruby_funcname_table[i].ptr = symbol_from_dll(hinstRuby,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000812 ruby_funcname_table[i].name)))
813 {
Bram Moolenaarf9b5ef82010-09-29 13:02:53 +0200814 close_dll(hinstRuby);
Bram Moolenaar3ca71f12010-10-27 16:49:47 +0200815 hinstRuby = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000816 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100817 semsg(_(e_loadfunc), ruby_funcname_table[i].name);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000818 return FAIL;
819 }
820 }
821 return OK;
822}
823
824/*
825 * If ruby is enabled (there is installed ruby on Windows system) return TRUE,
826 * else FALSE.
827 */
828 int
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100829ruby_enabled(int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000830{
Bram Moolenaar25e4fcd2016-01-09 14:57:47 +0100831 return ruby_runtime_link_init((char *)p_rubydll, verbose) == OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000832}
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100833#endif // defined(DYNAMIC_RUBY) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000834
835 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100836ruby_end(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000837{
Bram Moolenaar071d4272004-06-13 20:20:40 +0000838}
839
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100840 void
841ex_ruby(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000842{
843 int state;
844 char *script = NULL;
845
Bram Moolenaar35a2e192006-03-13 22:07:11 +0000846 script = (char *)script_get(eap, eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000847 if (!eap->skip && ensure_ruby_initialized())
848 {
849 if (script == NULL)
850 rb_eval_string_protect((char *)eap->arg, &state);
851 else
852 rb_eval_string_protect(script, &state);
853 if (state)
854 error_print(state);
855 }
856 vim_free(script);
857}
858
Bram Moolenaar165641d2010-02-17 16:23:09 +0100859/*
860 * In Ruby 1.9 or later, ruby String object has encoding.
861 * conversion buffer string of vim to ruby String object using
862 * VIM encoding option.
863 */
864 static VALUE
865vim_str2rb_enc_str(const char *s)
866{
Bram Moolenaar41a41412020-01-07 21:32:19 +0100867#if RUBY_VERSION >= 19
Bram Moolenaar165641d2010-02-17 16:23:09 +0100868 long lval;
869 char_u *sval;
870 rb_encoding *enc;
871
Bram Moolenaardd1f4262020-12-31 17:41:01 +0100872 if (get_option_value((char_u *)"enc", &lval, &sval, 0) == gov_string)
Bram Moolenaar165641d2010-02-17 16:23:09 +0100873 {
874 enc = rb_enc_find((char *)sval);
875 vim_free(sval);
Bram Moolenaar758535a2016-03-30 22:06:16 +0200876 if (enc)
Bram Moolenaar9b0ac222016-06-01 20:31:43 +0200877 return rb_enc_str_new(s, (long)strlen(s), enc);
Bram Moolenaar165641d2010-02-17 16:23:09 +0100878 }
879#endif
880 return rb_str_new2(s);
881}
882
883 static VALUE
884eval_enc_string_protect(const char *str, int *state)
885{
Bram Moolenaar41a41412020-01-07 21:32:19 +0100886#if RUBY_VERSION >= 19
Bram Moolenaar165641d2010-02-17 16:23:09 +0100887 long lval;
888 char_u *sval;
889 rb_encoding *enc;
890 VALUE v;
891
Bram Moolenaardd1f4262020-12-31 17:41:01 +0100892 if (get_option_value((char_u *)"enc", &lval, &sval, 0) == gov_string)
Bram Moolenaar165641d2010-02-17 16:23:09 +0100893 {
894 enc = rb_enc_find((char *)sval);
895 vim_free(sval);
896 if (enc)
897 {
898 v = rb_sprintf("#-*- coding:%s -*-\n%s", rb_enc_name(enc), str);
899 return rb_eval_string_protect(StringValuePtr(v), state);
900 }
901 }
902#endif
903 return rb_eval_string_protect(str, state);
904}
905
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100906 void
907ex_rubydo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000908{
909 int state;
910 linenr_T i;
Bram Moolenaarc593fee2017-01-29 23:11:25 +0100911 buf_T *was_curbuf = curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000912
913 if (ensure_ruby_initialized())
914 {
915 if (u_save(eap->line1 - 1, eap->line2 + 1) != OK)
916 return;
Bram Moolenaar758535a2016-03-30 22:06:16 +0200917 for (i = eap->line1; i <= eap->line2; i++)
918 {
Bram Moolenaare980d8a2010-12-08 13:11:21 +0100919 VALUE line;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000920
Bram Moolenaarc593fee2017-01-29 23:11:25 +0100921 if (i > curbuf->b_ml.ml_line_count)
922 break;
Bram Moolenaare980d8a2010-12-08 13:11:21 +0100923 line = vim_str2rb_enc_str((char *)ml_get(i));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000924 rb_lastline_set(line);
Bram Moolenaar165641d2010-02-17 16:23:09 +0100925 eval_enc_string_protect((char *) eap->arg, &state);
Bram Moolenaar758535a2016-03-30 22:06:16 +0200926 if (state)
927 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000928 error_print(state);
929 break;
930 }
Bram Moolenaarc593fee2017-01-29 23:11:25 +0100931 if (was_curbuf != curbuf)
932 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000933 line = rb_lastline_get();
Bram Moolenaar758535a2016-03-30 22:06:16 +0200934 if (!NIL_P(line))
935 {
936 if (TYPE(line) != T_STRING)
937 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100938 emsg(_("E265: $_ must be an instance of String"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000939 return;
940 }
Bram Moolenaar165641d2010-02-17 16:23:09 +0100941 ml_replace(i, (char_u *) StringValuePtr(line), 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000942 changed();
943#ifdef SYNTAX_HL
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100944 syn_changed(i); // recompute syntax hl. for this line
Bram Moolenaar071d4272004-06-13 20:20:40 +0000945#endif
946 }
947 }
948 check_cursor();
949 update_curbuf(NOT_VALID);
950 }
951}
952
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100953 static VALUE
954rb_load_wrap(VALUE file_to_load)
Bram Moolenaar37badc82018-01-31 20:15:30 +0100955{
956 rb_load(file_to_load, 0);
957 return Qnil;
958}
959
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100960 void
961ex_rubyfile(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000962{
963 int state;
964
965 if (ensure_ruby_initialized())
966 {
Bram Moolenaar37badc82018-01-31 20:15:30 +0100967 VALUE file_to_load = rb_str_new2((const char *)eap->arg);
968 rb_protect(rb_load_wrap, file_to_load, &state);
969 if (state)
970 error_print(state);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000971 }
972}
973
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100974 void
975ruby_buffer_free(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000976{
Bram Moolenaare344bea2005-09-01 20:46:49 +0000977 if (buf->b_ruby_ref)
978 {
979 rb_hash_aset(objtbl, rb_obj_id((VALUE) buf->b_ruby_ref), Qnil);
980 RDATA(buf->b_ruby_ref)->data = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000981 }
982}
983
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100984 void
985ruby_window_free(win_T *win)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000986{
Bram Moolenaare344bea2005-09-01 20:46:49 +0000987 if (win->w_ruby_ref)
988 {
989 rb_hash_aset(objtbl, rb_obj_id((VALUE) win->w_ruby_ref), Qnil);
990 RDATA(win->w_ruby_ref)->data = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000991 }
992}
993
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100994 static int
995ensure_ruby_initialized(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000996{
997 if (!ruby_initialized)
998 {
999#ifdef DYNAMIC_RUBY
1000 if (ruby_enabled(TRUE))
1001 {
1002#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01001003#ifdef MSWIN
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001004 // suggested by Ariya Mizutani
Bram Moolenaar0b69c732010-02-17 15:11:50 +01001005 int argc = 1;
1006 char *argv[] = {"gvim.exe"};
Bram Moolenaar90140742014-11-27 17:44:08 +01001007 char **argvp = argv;
Bram Moolenaar41a41412020-01-07 21:32:19 +01001008# if RUBY_VERSION >= 19
Bram Moolenaarfe6ce332017-01-14 20:12:01 +01001009 ruby_sysinit(&argc, &argvp);
1010# else
Bram Moolenaar90140742014-11-27 17:44:08 +01001011 NtInitialize(&argc, &argvp);
Bram Moolenaarfe6ce332017-01-14 20:12:01 +01001012# endif
Bram Moolenaar0b69c732010-02-17 15:11:50 +01001013#endif
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001014 {
Bram Moolenaar41a41412020-01-07 21:32:19 +01001015#if (RUBY_VERSION >= 19) || defined(RUBY_INIT_STACK)
Bram Moolenaar99685e62013-05-11 13:56:18 +02001016 ruby_init_stack(ruby_stack_start);
Bram Moolenaar165641d2010-02-17 16:23:09 +01001017#endif
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001018 ruby_init();
1019 }
Bram Moolenaar41a41412020-01-07 21:32:19 +01001020#if RUBY_VERSION >= 19
Bram Moolenaar7a8ef142010-12-24 13:39:35 +01001021 {
1022 int dummy_argc = 2;
Bram Moolenaard1bc96ce2017-09-26 21:21:44 +02001023 char *dummy_argv[] = {"vim-ruby", "-e_=0"};
Bram Moolenaar9b1067e2015-11-19 19:33:15 +01001024 ruby_options(dummy_argc, dummy_argv);
Bram Moolenaar7a8ef142010-12-24 13:39:35 +01001025 }
Bram Moolenaar165641d2010-02-17 16:23:09 +01001026 ruby_script("vim-ruby");
Bram Moolenaar7a8ef142010-12-24 13:39:35 +01001027#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001028 ruby_init_loadpath();
Bram Moolenaar165641d2010-02-17 16:23:09 +01001029#endif
Bram Moolenaar7a8ef142010-12-24 13:39:35 +01001030 ruby_io_init();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001031 ruby_vim_init();
1032 ruby_initialized = 1;
1033#ifdef DYNAMIC_RUBY
1034 }
1035 else
1036 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001037 emsg(_("E266: Sorry, this command is disabled, the Ruby library could not be loaded."));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001038 return 0;
1039 }
1040#endif
1041 }
1042 return ruby_initialized;
1043}
1044
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001045 static void
1046error_print(int state)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001047{
Bram Moolenaar41a41412020-01-07 21:32:19 +01001048#if !defined(DYNAMIC_RUBY) && (RUBY_VERSION <= 18)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001049 RUBYEXTERN VALUE ruby_errinfo;
1050#endif
Bram Moolenaarf711cb22018-08-01 18:42:13 +02001051 VALUE error;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001052 VALUE eclass;
1053 VALUE einfo;
Bram Moolenaarf711cb22018-08-01 18:42:13 +02001054 VALUE bt;
1055 int attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001056 char buff[BUFSIZ];
Bram Moolenaarf711cb22018-08-01 18:42:13 +02001057 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001058
1059#define TAG_RETURN 0x1
1060#define TAG_BREAK 0x2
1061#define TAG_NEXT 0x3
1062#define TAG_RETRY 0x4
1063#define TAG_REDO 0x5
1064#define TAG_RAISE 0x6
1065#define TAG_THROW 0x7
1066#define TAG_FATAL 0x8
1067#define TAG_MASK 0xf
1068
Bram Moolenaar758535a2016-03-30 22:06:16 +02001069 switch (state)
1070 {
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001071 case TAG_RETURN:
1072 emsg(_("E267: unexpected return"));
1073 break;
1074 case TAG_NEXT:
1075 emsg(_("E268: unexpected next"));
1076 break;
1077 case TAG_BREAK:
1078 emsg(_("E269: unexpected break"));
1079 break;
1080 case TAG_REDO:
1081 emsg(_("E270: unexpected redo"));
1082 break;
1083 case TAG_RETRY:
1084 emsg(_("E271: retry outside of rescue clause"));
1085 break;
1086 case TAG_RAISE:
1087 case TAG_FATAL:
Bram Moolenaar41a41412020-01-07 21:32:19 +01001088#if RUBY_VERSION >= 19
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001089 error = rb_errinfo();
Bram Moolenaar165641d2010-02-17 16:23:09 +01001090#else
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001091 error = ruby_errinfo;
Bram Moolenaar165641d2010-02-17 16:23:09 +01001092#endif
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001093 eclass = CLASS_OF(error);
1094 einfo = rb_obj_as_string(error);
1095 if (eclass == rb_eRuntimeError && RSTRING_LEN(einfo) == 0)
1096 {
1097 emsg(_("E272: unhandled exception"));
1098 }
1099 else
1100 {
1101 VALUE epath;
1102 char *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001103
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001104 epath = rb_class_path(eclass);
1105 vim_snprintf(buff, BUFSIZ, "%s: %s",
1106 RSTRING_PTR(epath), RSTRING_PTR(einfo));
1107 p = strchr(buff, '\n');
1108 if (p) *p = '\0';
1109 emsg(buff);
1110 }
Bram Moolenaarf711cb22018-08-01 18:42:13 +02001111
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001112 attr = syn_name2attr((char_u *)"Error");
Bram Moolenaar49c99fc2020-02-11 23:01:39 +01001113#if RUBY_VERSION >= 21
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001114 bt = rb_funcallv(error, rb_intern("backtrace"), 0, 0);
1115 for (i = 0; i < RARRAY_LEN(bt); i++)
1116 msg_attr(RSTRING_PTR(RARRAY_AREF(bt, i)), attr);
Bram Moolenaar49c99fc2020-02-11 23:01:39 +01001117#else
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001118 bt = rb_funcall2(error, rb_intern("backtrace"), 0, 0);
1119 for (i = 0; i < RARRAY_LEN(bt); i++)
1120 msg_attr(RSTRING_PTR(RARRAY_PTR(bt)[i]), attr);
Bram Moolenaar49c99fc2020-02-11 23:01:39 +01001121#endif
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001122 break;
1123 default:
1124 vim_snprintf(buff, BUFSIZ, _("E273: unknown longjmp status %d"), state);
1125 emsg(buff);
1126 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001127 }
1128}
1129
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001130 static VALUE
1131vim_message(VALUE self UNUSED, VALUE str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001132{
1133 char *buff, *p;
1134
1135 str = rb_obj_as_string(str);
Bram Moolenaar3f5f7952011-08-04 19:34:59 +02001136 if (RSTRING_LEN(str) > 0)
1137 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001138 // Only do this when the string isn't empty, alloc(0) causes trouble.
Bram Moolenaar00ccf542017-09-03 15:17:48 +02001139 buff = ALLOCA_N(char, RSTRING_LEN(str) + 1);
Bram Moolenaar3f5f7952011-08-04 19:34:59 +02001140 strcpy(buff, RSTRING_PTR(str));
1141 p = strchr(buff, '\n');
1142 if (p) *p = '\0';
Bram Moolenaar32526b32019-01-19 17:43:09 +01001143 msg(buff);
Bram Moolenaar3f5f7952011-08-04 19:34:59 +02001144 }
1145 else
1146 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01001147 msg("");
Bram Moolenaar3f5f7952011-08-04 19:34:59 +02001148 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001149 return Qnil;
1150}
1151
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001152 static VALUE
1153vim_set_option(VALUE self UNUSED, VALUE str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001154{
Bram Moolenaar165641d2010-02-17 16:23:09 +01001155 do_set((char_u *)StringValuePtr(str), 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001156 update_screen(NOT_VALID);
1157 return Qnil;
1158}
1159
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001160 static VALUE
1161vim_command(VALUE self UNUSED, VALUE str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001162{
Bram Moolenaar165641d2010-02-17 16:23:09 +01001163 do_cmdline_cmd((char_u *)StringValuePtr(str));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001164 return Qnil;
1165}
1166
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001167#ifdef FEAT_EVAL
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001168 static VALUE
1169vim_to_ruby(typval_T *tv)
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001170{
1171 VALUE result = Qnil;
1172
1173 if (tv->v_type == VAR_STRING)
1174 {
Bram Moolenaar94127e42010-03-19 23:08:48 +01001175 result = rb_str_new2(tv->vval.v_string == NULL
1176 ? "" : (char *)(tv->vval.v_string));
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001177 }
1178 else if (tv->v_type == VAR_NUMBER)
1179 {
Bram Moolenaar639a2552010-03-17 18:15:23 +01001180 result = INT2NUM(tv->vval.v_number);
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001181 }
1182# ifdef FEAT_FLOAT
1183 else if (tv->v_type == VAR_FLOAT)
1184 {
Bram Moolenaar639a2552010-03-17 18:15:23 +01001185 result = rb_float_new(tv->vval.v_float);
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001186 }
1187# endif
1188 else if (tv->v_type == VAR_LIST)
1189 {
Bram Moolenaar639a2552010-03-17 18:15:23 +01001190 list_T *list = tv->vval.v_list;
1191 listitem_T *curr;
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001192
Bram Moolenaar639a2552010-03-17 18:15:23 +01001193 result = rb_ary_new();
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001194
Bram Moolenaar639a2552010-03-17 18:15:23 +01001195 if (list != NULL)
1196 {
Bram Moolenaaraeea7212020-04-02 18:50:46 +02001197 FOR_ALL_LIST_ITEMS(list, curr)
Bram Moolenaar639a2552010-03-17 18:15:23 +01001198 rb_ary_push(result, vim_to_ruby(&curr->li_tv));
Bram Moolenaar639a2552010-03-17 18:15:23 +01001199 }
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001200 }
1201 else if (tv->v_type == VAR_DICT)
1202 {
Bram Moolenaar639a2552010-03-17 18:15:23 +01001203 result = rb_hash_new();
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001204
Bram Moolenaar639a2552010-03-17 18:15:23 +01001205 if (tv->vval.v_dict != NULL)
1206 {
1207 hashtab_T *ht = &tv->vval.v_dict->dv_hashtab;
1208 long_u todo = ht->ht_used;
1209 hashitem_T *hi;
1210 dictitem_T *di;
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001211
Bram Moolenaar639a2552010-03-17 18:15:23 +01001212 for (hi = ht->ht_array; todo > 0; ++hi)
1213 {
1214 if (!HASHITEM_EMPTY(hi))
1215 {
1216 --todo;
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001217
Bram Moolenaar639a2552010-03-17 18:15:23 +01001218 di = dict_lookup(hi);
1219 rb_hash_aset(result, rb_str_new2((char *)hi->hi_key),
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001220 vim_to_ruby(&di->di_tv));
Bram Moolenaar639a2552010-03-17 18:15:23 +01001221 }
1222 }
1223 }
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001224 }
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01001225 else if (tv->v_type == VAR_BOOL || tv->v_type == VAR_SPECIAL)
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001226 {
Bram Moolenaard84b26a2018-07-28 17:18:09 +02001227 if (tv->vval.v_number == VVAL_TRUE)
1228 result = Qtrue;
1229 else if (tv->vval.v_number == VVAL_FALSE)
1230 result = Qfalse;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001231 }
1232 else if (tv->v_type == VAR_BLOB)
1233 {
1234 result = rb_str_new(tv->vval.v_blob->bv_ga.ga_data,
1235 tv->vval.v_blob->bv_ga.ga_len);
1236 }
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001237 // else return Qnil;
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001238
1239 return result;
1240}
1241#endif
1242
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001243 static VALUE
1244vim_evaluate(VALUE self UNUSED, VALUE str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001245{
1246#ifdef FEAT_EVAL
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001247 typval_T *tv;
1248 VALUE result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001249
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001250 tv = eval_expr((char_u *)StringValuePtr(str), NULL);
1251 if (tv == NULL)
Bram Moolenaar639a2552010-03-17 18:15:23 +01001252 return Qnil;
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001253 result = vim_to_ruby(tv);
1254
1255 free_tv(tv);
1256
1257 return result;
1258#else
1259 return Qnil;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001260#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001261}
1262
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001263#ifdef USE_TYPEDDATA
1264static size_t buffer_dsize(const void *buf);
1265
1266static const rb_data_type_t buffer_type = {
1267 "vim_buffer",
Bram Moolenaar41a41412020-01-07 21:32:19 +01001268 {0, 0, buffer_dsize,
1269# if RUBY_VERSION >= 27
Bram Moolenaar8b430b42020-02-22 15:01:00 +01001270 0, {0}
Bram Moolenaar41a41412020-01-07 21:32:19 +01001271# else
1272 {0, 0}
1273# endif
1274 },
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001275 0, 0,
1276# ifdef RUBY_TYPED_FREE_IMMEDIATELY
1277 0,
1278# endif
1279};
1280
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001281 static size_t
1282buffer_dsize(const void *buf UNUSED)
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001283{
1284 return sizeof(buf_T);
1285}
1286#endif
1287
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001288 static VALUE
1289buffer_new(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001290{
Bram Moolenaare344bea2005-09-01 20:46:49 +00001291 if (buf->b_ruby_ref)
1292 {
1293 return (VALUE) buf->b_ruby_ref;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001294 }
Bram Moolenaare344bea2005-09-01 20:46:49 +00001295 else
1296 {
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001297#ifdef USE_TYPEDDATA
1298 VALUE obj = TypedData_Wrap_Struct(cBuffer, &buffer_type, buf);
1299#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001300 VALUE obj = Data_Wrap_Struct(cBuffer, 0, 0, buf);
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001301#endif
Bram Moolenaare344bea2005-09-01 20:46:49 +00001302 buf->b_ruby_ref = (void *) obj;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001303 rb_hash_aset(objtbl, rb_obj_id(obj), obj);
1304 return obj;
1305 }
1306}
1307
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001308 static buf_T *
1309get_buf(VALUE obj)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001310{
1311 buf_T *buf;
1312
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001313#ifdef USE_TYPEDDATA
1314 TypedData_Get_Struct(obj, buf_T, &buffer_type, buf);
1315#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001316 Data_Get_Struct(obj, buf_T, buf);
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001317#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001318 if (buf == NULL)
1319 rb_raise(eDeletedBufferError, "attempt to refer to deleted buffer");
1320 return buf;
1321}
1322
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001323 static VALUE
1324vim_blob(VALUE self UNUSED, VALUE str)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001325{
1326 VALUE result = rb_str_new("0z", 2);
1327 char buf[4];
1328 int i;
1329 for (i = 0; i < RSTRING_LEN(str); i++)
1330 {
Bram Moolenaar0d13cce2019-02-23 14:23:03 +01001331 sprintf(buf, "%02X", (unsigned char)(RSTRING_PTR(str)[i]));
Bram Moolenaarb191be22019-01-30 21:00:12 +01001332 rb_str_concat(result, rb_str_new2(buf));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001333 }
1334 return result;
1335}
1336
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001337 static VALUE
Bram Moolenaard5a986f2020-12-06 21:11:31 +01001338buffer_s_current(VALUE self UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001339{
1340 return buffer_new(curbuf);
1341}
1342
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001343 static VALUE
Bram Moolenaard5a986f2020-12-06 21:11:31 +01001344buffer_s_current_getter(ID id UNUSED, VALUE *x UNUSED)
1345{
1346 return buffer_new(curbuf);
1347}
1348
1349 static VALUE
1350buffer_s_count(VALUE self UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001351{
1352 buf_T *b;
1353 int n = 0;
1354
Bram Moolenaar29323592016-07-24 22:04:11 +02001355 FOR_ALL_BUFFERS(b)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001356 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001357 // Deleted buffers should not be counted
1358 // SegPhault - 01/07/05
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001359 if (b->b_p_bl)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001360 n++;
1361 }
1362
Bram Moolenaar071d4272004-06-13 20:20:40 +00001363 return INT2NUM(n);
1364}
1365
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001366 static VALUE
1367buffer_s_aref(VALUE self UNUSED, VALUE num)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001368{
1369 buf_T *b;
1370 int n = NUM2INT(num);
1371
Bram Moolenaar29323592016-07-24 22:04:11 +02001372 FOR_ALL_BUFFERS(b)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001373 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001374 // Deleted buffers should not be counted
1375 // SegPhault - 01/07/05
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001376 if (!b->b_p_bl)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001377 continue;
1378
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001379 if (n == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001380 return buffer_new(b);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001381
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001382 n--;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001383 }
1384 return Qnil;
1385}
1386
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001387 static VALUE
1388buffer_name(VALUE self)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001389{
1390 buf_T *buf = get_buf(self);
1391
Bram Moolenaar35a2e192006-03-13 22:07:11 +00001392 return buf->b_ffname ? rb_str_new2((char *)buf->b_ffname) : Qnil;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001393}
1394
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001395 static VALUE
1396buffer_number(VALUE self)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001397{
1398 buf_T *buf = get_buf(self);
1399
1400 return INT2NUM(buf->b_fnum);
1401}
1402
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001403 static VALUE
1404buffer_count(VALUE self)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001405{
1406 buf_T *buf = get_buf(self);
1407
1408 return INT2NUM(buf->b_ml.ml_line_count);
1409}
1410
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001411 static VALUE
1412get_buffer_line(buf_T *buf, linenr_T n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001413{
Bram Moolenaar3c531602010-11-16 14:46:19 +01001414 if (n <= 0 || n > buf->b_ml.ml_line_count)
1415 rb_raise(rb_eIndexError, "line number %ld out of range", (long)n);
1416 return vim_str2rb_enc_str((char *)ml_get_buf(buf, n, FALSE));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001417}
1418
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001419 static VALUE
1420buffer_aref(VALUE self, VALUE num)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001421{
1422 buf_T *buf = get_buf(self);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001423
1424 if (buf != NULL)
1425 return get_buffer_line(buf, (linenr_T)NUM2LONG(num));
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001426 return Qnil; // For stop warning
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001427}
1428
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001429 static VALUE
1430set_buffer_line(buf_T *buf, linenr_T n, VALUE str)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001431{
Bram Moolenaar165641d2010-02-17 16:23:09 +01001432 char *line = StringValuePtr(str);
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001433 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001434
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001435 if (n > 0 && n <= buf->b_ml.ml_line_count && line != NULL)
1436 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001437 // set curwin/curbuf for "buf" and save some things
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001438 aucmd_prepbuf(&aco, buf);
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001439
Bram Moolenaar758535a2016-03-30 22:06:16 +02001440 if (u_savesub(n) == OK)
1441 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001442 ml_replace(n, (char_u *)line, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001443 changed();
1444#ifdef SYNTAX_HL
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001445 syn_changed(n); // recompute syntax hl. for this line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001446#endif
1447 }
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001448
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001449 // restore curwin/curbuf and a few other things
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001450 aucmd_restbuf(&aco);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001451 // Careful: autocommands may have made "buf" invalid!
Bram Moolenaarf30e74c2006-08-16 17:35:00 +00001452
Bram Moolenaar071d4272004-06-13 20:20:40 +00001453 update_curbuf(NOT_VALID);
1454 }
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001455 else
1456 {
Bram Moolenaar165641d2010-02-17 16:23:09 +01001457 rb_raise(rb_eIndexError, "line number %ld out of range", (long)n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001458 }
1459 return str;
1460}
1461
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001462 static VALUE
1463buffer_aset(VALUE self, VALUE num, VALUE str)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001464{
1465 buf_T *buf = get_buf(self);
1466
1467 if (buf != NULL)
1468 return set_buffer_line(buf, (linenr_T)NUM2LONG(num), str);
1469 return str;
1470}
1471
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001472 static VALUE
1473buffer_delete(VALUE self, VALUE num)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001474{
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001475 buf_T *buf = get_buf(self);
1476 long n = NUM2LONG(num);
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001477 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001478
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001479 if (n > 0 && n <= buf->b_ml.ml_line_count)
1480 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001481 // set curwin/curbuf for "buf" and save some things
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001482 aucmd_prepbuf(&aco, buf);
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001483
Bram Moolenaar758535a2016-03-30 22:06:16 +02001484 if (u_savedel(n, 1) == OK)
1485 {
Bram Moolenaarca70c072020-05-30 20:30:46 +02001486 ml_delete(n);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001487
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001488 // Changes to non-active buffers should properly refresh
1489 // SegPhault - 01/09/05
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001490 deleted_lines_mark(n, 1L);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001491
Bram Moolenaar071d4272004-06-13 20:20:40 +00001492 changed();
1493 }
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001494
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001495 // restore curwin/curbuf and a few other things
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001496 aucmd_restbuf(&aco);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001497 // Careful: autocommands may have made "buf" invalid!
Bram Moolenaarf30e74c2006-08-16 17:35:00 +00001498
Bram Moolenaar071d4272004-06-13 20:20:40 +00001499 update_curbuf(NOT_VALID);
1500 }
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001501 else
1502 {
Bram Moolenaar165641d2010-02-17 16:23:09 +01001503 rb_raise(rb_eIndexError, "line number %ld out of range", n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001504 }
1505 return Qnil;
1506}
1507
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001508 static VALUE
1509buffer_append(VALUE self, VALUE num, VALUE str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001510{
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001511 buf_T *buf = get_buf(self);
Bram Moolenaar165641d2010-02-17 16:23:09 +01001512 char *line = StringValuePtr(str);
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001513 long n = NUM2LONG(num);
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001514 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001515
Bram Moolenaar3c531602010-11-16 14:46:19 +01001516 if (line == NULL)
1517 {
Bram Moolenaar165641d2010-02-17 16:23:09 +01001518 rb_raise(rb_eIndexError, "NULL line");
1519 }
1520 else if (n >= 0 && n <= buf->b_ml.ml_line_count)
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001521 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001522 // set curwin/curbuf for "buf" and save some things
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001523 aucmd_prepbuf(&aco, buf);
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001524
Bram Moolenaar758535a2016-03-30 22:06:16 +02001525 if (u_inssub(n + 1) == OK)
1526 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001527 ml_append(n, (char_u *) line, (colnr_T) 0, FALSE);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001528
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001529 // Changes to non-active buffers should properly refresh screen
1530 // SegPhault - 12/20/04
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001531 appended_lines_mark(n, 1L);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001532
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001533 changed();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001534 }
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001535
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001536 // restore curwin/curbuf and a few other things
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001537 aucmd_restbuf(&aco);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001538 // Careful: autocommands may have made "buf" invalid!
Bram Moolenaarf30e74c2006-08-16 17:35:00 +00001539
Bram Moolenaar071d4272004-06-13 20:20:40 +00001540 update_curbuf(NOT_VALID);
1541 }
Bram Moolenaar3c531602010-11-16 14:46:19 +01001542 else
1543 {
Bram Moolenaar165641d2010-02-17 16:23:09 +01001544 rb_raise(rb_eIndexError, "line number %ld out of range", n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001545 }
1546 return str;
1547}
1548
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001549#ifdef USE_TYPEDDATA
1550static size_t window_dsize(const void *buf);
1551
1552static const rb_data_type_t window_type = {
1553 "vim_window",
Bram Moolenaar41a41412020-01-07 21:32:19 +01001554 {0, 0, window_dsize,
1555# if RUBY_VERSION >= 27
Bram Moolenaar8b430b42020-02-22 15:01:00 +01001556 0, {0}
Bram Moolenaar41a41412020-01-07 21:32:19 +01001557# else
1558 {0, 0}
1559# endif
1560 },
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001561 0, 0,
1562# ifdef RUBY_TYPED_FREE_IMMEDIATELY
1563 0,
1564# endif
1565};
1566
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001567 static size_t
1568window_dsize(const void *win UNUSED)
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001569{
1570 return sizeof(win_T);
1571}
1572#endif
1573
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001574 static VALUE
1575window_new(win_T *win)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001576{
Bram Moolenaare344bea2005-09-01 20:46:49 +00001577 if (win->w_ruby_ref)
1578 {
1579 return (VALUE) win->w_ruby_ref;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001580 }
Bram Moolenaare344bea2005-09-01 20:46:49 +00001581 else
1582 {
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001583#ifdef USE_TYPEDDATA
1584 VALUE obj = TypedData_Wrap_Struct(cVimWindow, &window_type, win);
1585#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001586 VALUE obj = Data_Wrap_Struct(cVimWindow, 0, 0, win);
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001587#endif
Bram Moolenaare344bea2005-09-01 20:46:49 +00001588 win->w_ruby_ref = (void *) obj;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001589 rb_hash_aset(objtbl, rb_obj_id(obj), obj);
1590 return obj;
1591 }
1592}
1593
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001594 static win_T *
1595get_win(VALUE obj)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001596{
1597 win_T *win;
1598
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001599#ifdef USE_TYPEDDATA
1600 TypedData_Get_Struct(obj, win_T, &window_type, win);
1601#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001602 Data_Get_Struct(obj, win_T, win);
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001603#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001604 if (win == NULL)
1605 rb_raise(eDeletedWindowError, "attempt to refer to deleted window");
1606 return win;
1607}
1608
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001609 static VALUE
Bram Moolenaard5a986f2020-12-06 21:11:31 +01001610window_s_current(VALUE self UNUSED)
1611{
1612 return window_new(curwin);
1613}
1614
1615 static VALUE
1616window_s_current_getter(ID id UNUSED, VALUE *x UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001617{
1618 return window_new(curwin);
1619}
1620
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001621/*
1622 * Added line manipulation functions
1623 * SegPhault - 03/07/05
1624 */
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001625 static VALUE
Bram Moolenaard5a986f2020-12-06 21:11:31 +01001626line_s_current(VALUE self UNUSED)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001627{
1628 return get_buffer_line(curbuf, curwin->w_cursor.lnum);
1629}
1630
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001631 static VALUE
1632set_current_line(VALUE self UNUSED, VALUE str)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001633{
1634 return set_buffer_line(curbuf, curwin->w_cursor.lnum, str);
1635}
1636
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001637 static VALUE
Bram Moolenaard5a986f2020-12-06 21:11:31 +01001638current_line_number(VALUE self UNUSED)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001639{
1640 return INT2FIX((int)curwin->w_cursor.lnum);
1641}
1642
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001643 static VALUE
Bram Moolenaard5a986f2020-12-06 21:11:31 +01001644window_s_count(VALUE self UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001645{
Bram Moolenaar071d4272004-06-13 20:20:40 +00001646 win_T *w;
1647 int n = 0;
1648
Bram Moolenaar29323592016-07-24 22:04:11 +02001649 FOR_ALL_WINDOWS(w)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001650 n++;
1651 return INT2NUM(n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001652}
1653
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001654 static VALUE
1655window_s_aref(VALUE self UNUSED, VALUE num)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001656{
1657 win_T *w;
1658 int n = NUM2INT(num);
1659
Bram Moolenaar071d4272004-06-13 20:20:40 +00001660 for (w = firstwin; w != NULL; w = w->w_next, --n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001661 if (n == 0)
1662 return window_new(w);
1663 return Qnil;
1664}
1665
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001666 static VALUE
1667window_buffer(VALUE self)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001668{
1669 win_T *win = get_win(self);
1670
1671 return buffer_new(win->w_buffer);
1672}
1673
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001674 static VALUE
1675window_height(VALUE self)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001676{
1677 win_T *win = get_win(self);
1678
1679 return INT2NUM(win->w_height);
1680}
1681
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001682 static VALUE
1683window_set_height(VALUE self, VALUE height)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001684{
1685 win_T *win = get_win(self);
1686 win_T *savewin = curwin;
1687
1688 curwin = win;
1689 win_setheight(NUM2INT(height));
1690 curwin = savewin;
1691 return height;
1692}
1693
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001694 static VALUE
1695window_width(VALUE self UNUSED)
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001696{
Bram Moolenaare745d752017-09-22 16:56:22 +02001697 return INT2NUM(get_win(self)->w_width);
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001698}
1699
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001700 static VALUE
1701window_set_width(VALUE self UNUSED, VALUE width)
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001702{
1703 win_T *win = get_win(self);
1704 win_T *savewin = curwin;
1705
1706 curwin = win;
1707 win_setwidth(NUM2INT(width));
1708 curwin = savewin;
1709 return width;
1710}
1711
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001712 static VALUE
1713window_cursor(VALUE self)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001714{
1715 win_T *win = get_win(self);
1716
1717 return rb_assoc_new(INT2NUM(win->w_cursor.lnum), INT2NUM(win->w_cursor.col));
1718}
1719
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001720 static VALUE
1721window_set_cursor(VALUE self, VALUE pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001722{
1723 VALUE lnum, col;
1724 win_T *win = get_win(self);
1725
1726 Check_Type(pos, T_ARRAY);
Bram Moolenaar165641d2010-02-17 16:23:09 +01001727 if (RARRAY_LEN(pos) != 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001728 rb_raise(rb_eArgError, "array length must be 2");
Bram Moolenaar165641d2010-02-17 16:23:09 +01001729 lnum = RARRAY_PTR(pos)[0];
1730 col = RARRAY_PTR(pos)[1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001731 win->w_cursor.lnum = NUM2LONG(lnum);
1732 win->w_cursor.col = NUM2UINT(col);
Bram Moolenaar53901442018-07-25 22:02:36 +02001733 win->w_set_curswant = TRUE;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001734 check_cursor(); // put cursor on an existing line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001735 update_screen(NOT_VALID);
1736 return Qnil;
1737}
1738
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001739 static VALUE
1740f_nop(VALUE self UNUSED)
Bram Moolenaar35df7d22012-04-20 18:05:47 +02001741{
1742 return Qnil;
1743}
1744
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001745 static VALUE
1746f_p(int argc, VALUE *argv, VALUE self UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001747{
1748 int i;
1749 VALUE str = rb_str_new("", 0);
Bram Moolenaar51e9fbf2018-08-11 14:24:11 +02001750 VALUE ret = Qnil;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001751
Bram Moolenaar758535a2016-03-30 22:06:16 +02001752 for (i = 0; i < argc; i++)
1753 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001754 if (i > 0) rb_str_cat(str, ", ", 2);
1755 rb_str_concat(str, rb_inspect(argv[i]));
1756 }
Bram Moolenaar32526b32019-01-19 17:43:09 +01001757 msg(RSTRING_PTR(str));
Bram Moolenaar51e9fbf2018-08-11 14:24:11 +02001758
1759 if (argc == 1)
1760 ret = argv[0];
1761 else if (argc > 1)
1762 ret = rb_ary_new4(argc, argv);
1763 return ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001764}
1765
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001766 static void
1767ruby_io_init(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001768{
1769#ifndef DYNAMIC_RUBY
1770 RUBYEXTERN VALUE rb_stdout;
Bram Moolenaarb6c8cd82018-07-24 05:41:30 +02001771 RUBYEXTERN VALUE rb_stderr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001772#endif
1773
1774 rb_stdout = rb_obj_alloc(rb_cObject);
Bram Moolenaarb6c8cd82018-07-24 05:41:30 +02001775 rb_stderr = rb_obj_alloc(rb_cObject);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001776 rb_define_singleton_method(rb_stdout, "write", vim_message, 1);
Bram Moolenaar35df7d22012-04-20 18:05:47 +02001777 rb_define_singleton_method(rb_stdout, "flush", f_nop, 0);
Bram Moolenaarb6c8cd82018-07-24 05:41:30 +02001778 rb_define_singleton_method(rb_stderr, "write", vim_message, 1);
1779 rb_define_singleton_method(rb_stderr, "flush", f_nop, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001780 rb_define_global_function("p", f_p, -1);
1781}
1782
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001783 static void
1784ruby_vim_init(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001785{
1786 objtbl = rb_hash_new();
1787 rb_global_variable(&objtbl);
1788
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001789 // The Vim module used to be called "VIM", but "Vim" is better. Make an
1790 // alias "VIM" for backwards compatibility.
Bram Moolenaarf711faf2007-05-10 16:48:19 +00001791 mVIM = rb_define_module("Vim");
1792 rb_define_const(rb_cObject, "VIM", mVIM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001793 rb_define_const(mVIM, "VERSION_MAJOR", INT2NUM(VIM_VERSION_MAJOR));
1794 rb_define_const(mVIM, "VERSION_MINOR", INT2NUM(VIM_VERSION_MINOR));
1795 rb_define_const(mVIM, "VERSION_BUILD", INT2NUM(VIM_VERSION_BUILD));
1796 rb_define_const(mVIM, "VERSION_PATCHLEVEL", INT2NUM(VIM_VERSION_PATCHLEVEL));
1797 rb_define_const(mVIM, "VERSION_SHORT", rb_str_new2(VIM_VERSION_SHORT));
1798 rb_define_const(mVIM, "VERSION_MEDIUM", rb_str_new2(VIM_VERSION_MEDIUM));
1799 rb_define_const(mVIM, "VERSION_LONG", rb_str_new2(VIM_VERSION_LONG));
1800 rb_define_const(mVIM, "VERSION_LONG_DATE", rb_str_new2(VIM_VERSION_LONG_DATE));
1801 rb_define_module_function(mVIM, "message", vim_message, 1);
1802 rb_define_module_function(mVIM, "set_option", vim_set_option, 1);
1803 rb_define_module_function(mVIM, "command", vim_command, 1);
1804 rb_define_module_function(mVIM, "evaluate", vim_evaluate, 1);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001805 rb_define_module_function(mVIM, "blob", vim_blob, 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001806
1807 eDeletedBufferError = rb_define_class_under(mVIM, "DeletedBufferError",
1808 rb_eStandardError);
1809 eDeletedWindowError = rb_define_class_under(mVIM, "DeletedWindowError",
1810 rb_eStandardError);
1811
1812 cBuffer = rb_define_class_under(mVIM, "Buffer", rb_cObject);
1813 rb_define_singleton_method(cBuffer, "current", buffer_s_current, 0);
1814 rb_define_singleton_method(cBuffer, "count", buffer_s_count, 0);
1815 rb_define_singleton_method(cBuffer, "[]", buffer_s_aref, 1);
1816 rb_define_method(cBuffer, "name", buffer_name, 0);
1817 rb_define_method(cBuffer, "number", buffer_number, 0);
1818 rb_define_method(cBuffer, "count", buffer_count, 0);
1819 rb_define_method(cBuffer, "length", buffer_count, 0);
1820 rb_define_method(cBuffer, "[]", buffer_aref, 1);
1821 rb_define_method(cBuffer, "[]=", buffer_aset, 2);
1822 rb_define_method(cBuffer, "delete", buffer_delete, 1);
1823 rb_define_method(cBuffer, "append", buffer_append, 2);
1824
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001825 // Added line manipulation functions
1826 // SegPhault - 03/07/05
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001827 rb_define_method(cBuffer, "line_number", current_line_number, 0);
1828 rb_define_method(cBuffer, "line", line_s_current, 0);
1829 rb_define_method(cBuffer, "line=", set_current_line, 1);
1830
1831
Bram Moolenaar071d4272004-06-13 20:20:40 +00001832 cVimWindow = rb_define_class_under(mVIM, "Window", rb_cObject);
1833 rb_define_singleton_method(cVimWindow, "current", window_s_current, 0);
1834 rb_define_singleton_method(cVimWindow, "count", window_s_count, 0);
1835 rb_define_singleton_method(cVimWindow, "[]", window_s_aref, 1);
1836 rb_define_method(cVimWindow, "buffer", window_buffer, 0);
1837 rb_define_method(cVimWindow, "height", window_height, 0);
1838 rb_define_method(cVimWindow, "height=", window_set_height, 1);
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001839 rb_define_method(cVimWindow, "width", window_width, 0);
1840 rb_define_method(cVimWindow, "width=", window_set_width, 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001841 rb_define_method(cVimWindow, "cursor", window_cursor, 0);
1842 rb_define_method(cVimWindow, "cursor=", window_set_cursor, 1);
1843
Bram Moolenaard5a986f2020-12-06 21:11:31 +01001844 rb_define_virtual_variable("$curbuf", buffer_s_current_getter, 0);
1845 rb_define_virtual_variable("$curwin", window_s_current_getter, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001846}
Bram Moolenaar99685e62013-05-11 13:56:18 +02001847
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001848 void
1849vim_ruby_init(void *stack_start)
Bram Moolenaar99685e62013-05-11 13:56:18 +02001850{
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001851 // should get machine stack start address early in main function
Bram Moolenaar99685e62013-05-11 13:56:18 +02001852 ruby_stack_start = stack_start;
1853}
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001854
1855 static int
1856convert_hash2dict(VALUE key, VALUE val, VALUE arg)
1857{
1858 dict_T *d = (dict_T *)arg;
1859 dictitem_T *di;
1860
Bram Moolenaardace9f72020-12-28 15:07:45 +01001861 di = dictitem_alloc((char_u *)RSTRING_PTR(rb_obj_as_string(key)));
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001862 if (di == NULL || ruby_convert_to_vim_value(val, &di->di_tv) != OK
1863 || dict_add(d, di) != OK)
1864 {
1865 d->dv_hashtab.ht_error = TRUE;
1866 return ST_STOP;
1867 }
1868 return ST_CONTINUE;
1869}
1870
1871 static int
1872ruby_convert_to_vim_value(VALUE val, typval_T *rettv)
1873{
1874 switch (TYPE(val))
1875 {
1876 case T_NIL:
1877 rettv->v_type = VAR_SPECIAL;
1878 rettv->vval.v_number = VVAL_NULL;
1879 break;
1880 case T_TRUE:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01001881 rettv->v_type = VAR_BOOL;
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001882 rettv->vval.v_number = VVAL_TRUE;
1883 break;
1884 case T_FALSE:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01001885 rettv->v_type = VAR_BOOL;
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001886 rettv->vval.v_number = VVAL_FALSE;
1887 break;
1888 case T_BIGNUM:
1889 case T_FIXNUM:
1890 rettv->v_type = VAR_NUMBER;
1891 rettv->vval.v_number = (varnumber_T)NUM2LONG(val);
1892 break;
1893#ifdef FEAT_FLOAT
1894 case T_FLOAT:
1895 rettv->v_type = VAR_FLOAT;
1896 rettv->vval.v_float = (float_T)NUM2DBL(val);
1897 break;
1898#endif
1899 default:
1900 val = rb_obj_as_string(val);
1901 // FALLTHROUGH
1902 case T_STRING:
1903 {
1904 VALUE str = (VALUE)RSTRING(val);
1905
1906 rettv->v_type = VAR_STRING;
1907 rettv->vval.v_string = vim_strnsave((char_u *)RSTRING_PTR(str),
Bram Moolenaar71ccd032020-06-12 22:59:11 +02001908 RSTRING_LEN(str));
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001909 }
1910 break;
1911 case T_ARRAY:
1912 {
1913 list_T *l;
1914 long i;
1915 typval_T v;
1916
1917 l = list_alloc();
1918 if (l == NULL)
1919 return FAIL;
1920
1921 for (i = 0; i < RARRAY_LEN(val); ++i)
1922 {
1923 if (ruby_convert_to_vim_value((VALUE)RARRAY_PTR(val)[i],
1924 &v) != OK)
1925 {
1926 list_unref(l);
1927 return FAIL;
1928 }
1929 list_append_tv(l, &v);
1930 clear_tv(&v);
1931 }
1932
1933 rettv->v_type = VAR_LIST;
1934 rettv->vval.v_list = l;
1935 ++l->lv_refcount;
1936 }
1937 break;
1938 case T_HASH:
1939 {
1940 dict_T *d;
1941
1942 d = dict_alloc();
1943 if (d == NULL)
1944 return FAIL;
1945
1946 rb_hash_foreach(val, convert_hash2dict, (VALUE)d);
1947 if (d->dv_hashtab.ht_error)
1948 {
1949 dict_unref(d);
1950 return FAIL;
1951 }
1952
1953 rettv->v_type = VAR_DICT;
1954 rettv->vval.v_dict = d;
1955 ++d->dv_refcount;
1956 }
1957 break;
1958 }
1959 return OK;
1960}
1961
1962 void
1963do_rubyeval(char_u *str, typval_T *rettv)
1964{
1965 int retval = FAIL;
1966
1967 if (ensure_ruby_initialized())
1968 {
1969 int state;
1970 VALUE obj;
1971
1972 obj = rb_eval_string_protect((const char *)str, &state);
1973 if (state)
1974 error_print(state);
1975 else
1976 retval = ruby_convert_to_vim_value(obj, rettv);
1977 }
1978 if (retval == FAIL)
1979 {
1980 rettv->v_type = VAR_NUMBER;
1981 rettv->vval.v_number = 0;
1982 }
1983}