blob: 7632cbf05496a8df8449579413d22f4512504b96 [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 Moolenaarf9b5ef82010-09-29 13:02:53 +020035#ifdef DYNAMIC_RUBY
Bram Moolenaar071d4272004-06-13 20:20:40 +000036/*
37 * This is tricky. In ruby.h there is (inline) function rb_class_of()
38 * definition. This function use these variables. But we want function to
39 * use dll_* variables.
40 */
Bram Moolenaardace9f72020-12-28 15:07:45 +010041# if RUBY_VERSION >= 24
42# define USE_RUBY_INTEGER
43# endif
44
Bram Moolenaar071d4272004-06-13 20:20:40 +000045# define rb_cFalseClass (*dll_rb_cFalseClass)
46# define rb_cFixnum (*dll_rb_cFixnum)
Bram Moolenaar2016ae52016-06-13 20:08:43 +020047# if defined(USE_RUBY_INTEGER)
48# define rb_cInteger (*dll_rb_cInteger)
Bram Moolenaar06469e92016-06-11 22:26:53 +020049# endif
Bram Moolenaar41a41412020-01-07 21:32:19 +010050# if RUBY_VERSION >= 20
Bram Moolenaardb3fbe52013-03-07 15:16:21 +010051# define rb_cFloat (*dll_rb_cFloat)
52# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000053# define rb_cNilClass (*dll_rb_cNilClass)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010054# define rb_cString (*dll_rb_cString)
Bram Moolenaar071d4272004-06-13 20:20:40 +000055# define rb_cSymbol (*dll_rb_cSymbol)
56# define rb_cTrueClass (*dll_rb_cTrueClass)
Bram Moolenaardace9f72020-12-28 15:07:45 +010057
Bram Moolenaar41a41412020-01-07 21:32:19 +010058# if RUBY_VERSION >= 18
Bram Moolenaar071d4272004-06-13 20:20:40 +000059/*
Bram Moolenaar42d57f02010-03-10 12:47:00 +010060 * On ver 1.8, all Ruby functions are exported with "__declspec(dllimport)"
61 * in ruby.h. But it causes trouble for these variables, because it is
Bram Moolenaar071d4272004-06-13 20:20:40 +000062 * defined in this file. When defined this RUBY_EXPORT it modified to
63 * "extern" and be able to avoid this problem.
64 */
65# define RUBY_EXPORT
66# endif
Bram Moolenaarf9b5ef82010-09-29 13:02:53 +020067
Bram Moolenaardace9f72020-12-28 15:07:45 +010068# if RUBY_VERSION >= 19
Bram Moolenaar2ab2e862019-12-04 21:24:53 +010069// Ruby 1.9 defines a number of static functions which use rb_num2long and
70// rb_int2big
ichizok8bb3fe42021-12-28 15:51:45 +000071# define rb_num2long rb_num2long_stub
72# define rb_int2big rb_int2big_stub
Bram Moolenaar42d57f02010-03-10 12:47:00 +010073
Bram Moolenaardace9f72020-12-28 15:07:45 +010074# if RUBY_VERSION >= 30 || VIM_SIZEOF_INT < VIM_SIZEOF_LONG
Bram Moolenaar2ab2e862019-12-04 21:24:53 +010075// Ruby 1.9 defines a number of static functions which use rb_fix2int and
76// rb_num2int if VIM_SIZEOF_INT < VIM_SIZEOF_LONG (64bit)
ichizok8bb3fe42021-12-28 15:51:45 +000077# define rb_fix2int rb_fix2int_stub
78# define rb_num2int rb_num2int_stub
Bram Moolenaardace9f72020-12-28 15:07:45 +010079# endif
80# endif
Bram Moolenaar0bcdd6e2013-04-14 16:19:03 +020081
Bram Moolenaardace9f72020-12-28 15:07:45 +010082# if RUBY_VERSION == 21
Bram Moolenaar2ab2e862019-12-04 21:24:53 +010083// Ruby 2.1 adds new GC called RGenGC and RARRAY_PTR uses
84// rb_gc_writebarrier_unprotect_promoted if USE_RGENGC
ichizok8bb3fe42021-12-28 15:51:45 +000085# define rb_gc_writebarrier_unprotect_promoted rb_gc_writebarrier_unprotect_promoted_stub
Bram Moolenaardace9f72020-12-28 15:07:45 +010086# endif
Bram Moolenaara1a118b2014-02-05 22:41:15 +010087
Bram Moolenaardace9f72020-12-28 15:07:45 +010088# if RUBY_VERSION >= 22
ichizok8bb3fe42021-12-28 15:51:45 +000089# define rb_gc_writebarrier_unprotect rb_gc_writebarrier_unprotect_stub
Bram Moolenaardace9f72020-12-28 15:07:45 +010090# endif
91
92# if RUBY_VERSION >= 26
ichizok8bb3fe42021-12-28 15:51:45 +000093# define rb_ary_detransient rb_ary_detransient_stub
Bram Moolenaardace9f72020-12-28 15:07:45 +010094# endif
95
96# if RUBY_VERSION >= 30
ichizok8bb3fe42021-12-28 15:51:45 +000097# define rb_check_type rb_check_type_stub
98# define rb_num2uint rb_num2uint_stub
99# define ruby_malloc_size_overflow ruby_malloc_size_overflow_stub
100# endif
101
102# if RUBY_VERSION >= 31
103# define rb_debug_rstring_null_ptr rb_debug_rstring_null_ptr_stub
104# define rb_unexpected_type rb_unexpected_type_stub
Bram Moolenaardace9f72020-12-28 15:07:45 +0100105# 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
ichizok8bb3fe42021-12-28 15:51:45 +0000176// Avoid redefining TRUE/FALSE in vterm.h.
177#ifdef TRUE
178# undef TRUE
179#endif
180#ifdef FALSE
181# undef FALSE
182#endif
183
Bram Moolenaar071d4272004-06-13 20:20:40 +0000184#include "vim.h"
185#include "version.h"
186
Bram Moolenaar7dca2eb2019-02-18 20:42:50 +0100187#ifdef DYNAMIC_RUBY
188# if !defined(MSWIN) // must come after including vim.h, where it is defined
189# include <dlfcn.h>
190# define HINSTANCE void*
191# define RUBY_PROC void*
192# define load_dll(n) dlopen((n), RTLD_LAZY|RTLD_GLOBAL)
193# define symbol_from_dll dlsym
194# define close_dll dlclose
Martin Tournoij1a3e5742021-07-24 13:57:29 +0200195# define load_dll_error dlerror
Bram Moolenaar7dca2eb2019-02-18 20:42:50 +0100196# else
197# define RUBY_PROC FARPROC
198# define load_dll vimLoadLib
199# define symbol_from_dll GetProcAddress
200# define close_dll FreeLibrary
Martin Tournoij1a3e5742021-07-24 13:57:29 +0200201# define load_dll_error GetWin32Error
Bram Moolenaar7dca2eb2019-02-18 20:42:50 +0100202# endif
203#endif
204
Bram Moolenaar071d4272004-06-13 20:20:40 +0000205#if defined(PROTO) && !defined(FEAT_RUBY)
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100206// Define these to be able to generate the function prototypes.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000207# define VALUE int
208# define RUBY_DATA_FUNC int
209#endif
210
211static int ruby_initialized = 0;
Bram Moolenaar99685e62013-05-11 13:56:18 +0200212static void *ruby_stack_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000213static VALUE objtbl;
214
215static VALUE mVIM;
216static VALUE cBuffer;
217static VALUE cVimWindow;
218static VALUE eDeletedBufferError;
219static VALUE eDeletedWindowError;
220
221static int ensure_ruby_initialized(void);
222static void error_print(int);
223static void ruby_io_init(void);
224static void ruby_vim_init(void);
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100225static int ruby_convert_to_vim_value(VALUE val, typval_T *rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000226
Bram Moolenaar41a41412020-01-07 21:32:19 +0100227#if (RUBY_VERSION >= 19) || defined(RUBY_INIT_STACK)
Bram Moolenaar10f3a792013-05-20 12:52:29 +0200228# if defined(__ia64) && !defined(ruby_init_stack)
229# define ruby_init_stack(addr) ruby_init_stack((addr), rb_ia64_bsp())
230# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000231#endif
232
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200233#if defined(DYNAMIC_RUBY) || defined(PROTO)
Bram Moolenaaref269542016-01-19 13:22:12 +0100234# if defined(PROTO) && !defined(HINSTANCE)
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100235# define HINSTANCE int // for generating prototypes
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200236# endif
237
Bram Moolenaar071d4272004-06-13 20:20:40 +0000238/*
239 * Wrapper defines
240 */
Bram Moolenaar8b430b42020-02-22 15:01:00 +0100241// Ruby 2.7 actually expands the following symbols as macro.
242# if RUBY_VERSION >= 27
243# undef rb_define_global_function
244# undef rb_define_method
245# undef rb_define_module_function
246# undef rb_define_singleton_method
247# endif
248
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200249# define rb_assoc_new dll_rb_assoc_new
250# define rb_cObject (*dll_rb_cObject)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100251# define rb_class_new_instance dll_rb_class_new_instance
Bram Moolenaardace9f72020-12-28 15:07:45 +0100252# if RUBY_VERSION < 30
253# define rb_check_type dll_rb_check_type
254# endif
Bram Moolenaarf2f6d292015-12-28 20:57:10 +0100255# ifdef USE_TYPEDDATA
256# define rb_check_typeddata dll_rb_check_typeddata
Bram Moolenaar0c7485f2015-01-14 14:04:10 +0100257# endif
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200258# define rb_class_path dll_rb_class_path
Bram Moolenaarf2f6d292015-12-28 20:57:10 +0100259# ifdef USE_TYPEDDATA
Bram Moolenaar41a41412020-01-07 21:32:19 +0100260# if RUBY_VERSION >= 23
Bram Moolenaarf2f6d292015-12-28 20:57:10 +0100261# define rb_data_typed_object_wrap dll_rb_data_typed_object_wrap
262# else
263# define rb_data_typed_object_alloc dll_rb_data_typed_object_alloc
264# endif
265# else
266# define rb_data_object_alloc dll_rb_data_object_alloc
267# endif
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200268# define rb_define_class_under dll_rb_define_class_under
269# define rb_define_const dll_rb_define_const
270# define rb_define_global_function dll_rb_define_global_function
271# define rb_define_method dll_rb_define_method
272# define rb_define_module dll_rb_define_module
273# define rb_define_module_function dll_rb_define_module_function
274# define rb_define_singleton_method dll_rb_define_singleton_method
275# define rb_define_virtual_variable dll_rb_define_virtual_variable
276# define rb_stdout (*dll_rb_stdout)
Bram Moolenaarb6c8cd82018-07-24 05:41:30 +0200277# define rb_stderr (*dll_rb_stderr)
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200278# define rb_eArgError (*dll_rb_eArgError)
279# define rb_eIndexError (*dll_rb_eIndexError)
280# define rb_eRuntimeError (*dll_rb_eRuntimeError)
281# define rb_eStandardError (*dll_rb_eStandardError)
282# define rb_eval_string_protect dll_rb_eval_string_protect
Bram Moolenaar41a41412020-01-07 21:32:19 +0100283# if RUBY_VERSION >= 21
Bram Moolenaarf711cb22018-08-01 18:42:13 +0200284# define rb_funcallv dll_rb_funcallv
285# else
286# define rb_funcall2 dll_rb_funcall2
287# endif
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200288# define rb_global_variable dll_rb_global_variable
289# define rb_hash_aset dll_rb_hash_aset
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100290# define rb_hash_foreach dll_rb_hash_foreach
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200291# define rb_hash_new dll_rb_hash_new
292# define rb_inspect dll_rb_inspect
293# define rb_int2inum dll_rb_int2inum
Bram Moolenaar218beb32018-08-04 17:24:44 +0200294
295// ruby.h may redefine rb_intern to use RUBY_CONST_ID_CACHE(), but that won't
296// work. Not using the cache appears to be the best solution.
297# undef rb_intern
298# define rb_intern dll_rb_intern
299
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100300# if VIM_SIZEOF_INT < VIM_SIZEOF_LONG // 64 bits only
Bram Moolenaar41a41412020-01-07 21:32:19 +0100301# if RUBY_VERSION <= 18
Bram Moolenaar498af702014-03-28 21:58:21 +0100302# define rb_fix2int dll_rb_fix2int
303# define rb_num2int dll_rb_num2int
304# endif
Bram Moolenaardace9f72020-12-28 15:07:45 +0100305# if RUBY_VERSION < 30
306# define rb_num2uint dll_rb_num2uint
307# endif
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200308# endif
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100309# define rb_num2dbl dll_rb_num2dbl
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200310# define rb_lastline_get dll_rb_lastline_get
311# define rb_lastline_set dll_rb_lastline_set
Bram Moolenaard0573012017-10-28 21:11:06 +0200312# define rb_protect dll_rb_protect
313# define rb_load dll_rb_load
Bram Moolenaar41a41412020-01-07 21:32:19 +0100314# if RUBY_VERSION <= 18
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200315# define rb_num2long dll_rb_num2long
316# endif
Bram Moolenaar41a41412020-01-07 21:32:19 +0100317# if RUBY_VERSION <= 19
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200318# define rb_num2ulong dll_rb_num2ulong
319# endif
320# define rb_obj_alloc dll_rb_obj_alloc
321# define rb_obj_as_string dll_rb_obj_as_string
322# define rb_obj_id dll_rb_obj_id
323# define rb_raise dll_rb_raise
324# define rb_str_cat dll_rb_str_cat
325# define rb_str_concat dll_rb_str_concat
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100326# undef rb_str_new
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200327# define rb_str_new dll_rb_str_new
328# ifdef rb_str_new2
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100329// Ruby may #define rb_str_new2 to use rb_str_new_cstr.
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200330# define need_rb_str_new_cstr 1
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100331// Ruby's headers #define rb_str_new_cstr to make use of GCC's
332// __builtin_constant_p extension.
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200333# undef rb_str_new_cstr
334# define rb_str_new_cstr dll_rb_str_new_cstr
Bram Moolenaar76a86062013-05-11 17:45:48 +0200335# else
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200336# define rb_str_new2 dll_rb_str_new2
Bram Moolenaar76a86062013-05-11 17:45:48 +0200337# endif
Bram Moolenaar41a41412020-01-07 21:32:19 +0100338# if RUBY_VERSION >= 18
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200339# define rb_string_value dll_rb_string_value
340# define rb_string_value_ptr dll_rb_string_value_ptr
341# define rb_float_new dll_rb_float_new
342# define rb_ary_new dll_rb_ary_new
Bram Moolenaar51e9fbf2018-08-11 14:24:11 +0200343# ifdef rb_ary_new4
Bram Moolenaar49c99fc2020-02-11 23:01:39 +0100344# define RB_ARY_NEW4_MACRO 1
345# undef rb_ary_new4
Bram Moolenaar51e9fbf2018-08-11 14:24:11 +0200346# endif
347# define rb_ary_new4 dll_rb_ary_new4
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200348# define rb_ary_push dll_rb_ary_push
Bram Moolenaar41a41412020-01-07 21:32:19 +0100349# if (RUBY_VERSION >= 19) || defined(RUBY_INIT_STACK)
Bram Moolenaar10f3a792013-05-20 12:52:29 +0200350# ifdef __ia64
351# define rb_ia64_bsp dll_rb_ia64_bsp
352# undef ruby_init_stack
353# define ruby_init_stack(addr) dll_ruby_init_stack((addr), rb_ia64_bsp())
354# else
355# define ruby_init_stack dll_ruby_init_stack
356# endif
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200357# endif
358# else
359# define rb_str2cstr dll_rb_str2cstr
Bram Moolenaar3ca71f12010-10-27 16:49:47 +0200360# endif
Bram Moolenaar41a41412020-01-07 21:32:19 +0100361# if RUBY_VERSION >= 19
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200362# define rb_errinfo dll_rb_errinfo
363# else
364# define ruby_errinfo (*dll_ruby_errinfo)
365# endif
366# define ruby_init dll_ruby_init
367# define ruby_init_loadpath dll_ruby_init_loadpath
Bram Moolenaar4f974752019-02-17 17:44:42 +0100368# ifdef MSWIN
Bram Moolenaar41a41412020-01-07 21:32:19 +0100369# if RUBY_VERSION >= 19
Bram Moolenaar9d5c84a2018-12-19 20:48:46 +0100370# define ruby_sysinit dll_ruby_sysinit
371# else
372# define NtInitialize dll_NtInitialize
373# endif
Bram Moolenaar41a41412020-01-07 21:32:19 +0100374# if RUBY_VERSION >= 18
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200375# define rb_w32_snprintf dll_rb_w32_snprintf
376# endif
377# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000378
Bram Moolenaar41a41412020-01-07 21:32:19 +0100379# if RUBY_VERSION >= 19
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200380# define ruby_script dll_ruby_script
381# define rb_enc_find_index dll_rb_enc_find_index
382# define rb_enc_find dll_rb_enc_find
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100383# undef rb_enc_str_new
384# define rb_enc_str_new dll_rb_enc_str_new
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200385# define rb_sprintf dll_rb_sprintf
386# define rb_require dll_rb_require
Bram Moolenaar9b1067e2015-11-19 19:33:15 +0100387# define ruby_options dll_ruby_options
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200388# endif
Bram Moolenaar165641d2010-02-17 16:23:09 +0100389
Bram Moolenaar071d4272004-06-13 20:20:40 +0000390/*
391 * Pointers for dynamic link
392 */
393static VALUE (*dll_rb_assoc_new) (VALUE, VALUE);
Bram Moolenaarba52cde2010-06-25 04:29:11 +0200394VALUE *dll_rb_cFalseClass;
395VALUE *dll_rb_cFixnum;
Bram Moolenaar2016ae52016-06-13 20:08:43 +0200396# if defined(USE_RUBY_INTEGER)
Bram Moolenaar06469e92016-06-11 22:26:53 +0200397VALUE *dll_rb_cInteger;
398# endif
Bram Moolenaar41a41412020-01-07 21:32:19 +0100399# if RUBY_VERSION >= 20
Bram Moolenaardb3fbe52013-03-07 15:16:21 +0100400VALUE *dll_rb_cFloat;
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200401# endif
Bram Moolenaarba52cde2010-06-25 04:29:11 +0200402VALUE *dll_rb_cNilClass;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000403static VALUE *dll_rb_cObject;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100404VALUE *dll_rb_cString;
Bram Moolenaarba52cde2010-06-25 04:29:11 +0200405VALUE *dll_rb_cSymbol;
406VALUE *dll_rb_cTrueClass;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100407static VALUE (*dll_rb_class_new_instance) (int,VALUE*,VALUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000408static void (*dll_rb_check_type) (VALUE,int);
Bram Moolenaarf2f6d292015-12-28 20:57:10 +0100409# ifdef USE_TYPEDDATA
410static void *(*dll_rb_check_typeddata) (VALUE,const rb_data_type_t *);
411# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000412static VALUE (*dll_rb_class_path) (VALUE);
Bram Moolenaarf2f6d292015-12-28 20:57:10 +0100413# ifdef USE_TYPEDDATA
Bram Moolenaar41a41412020-01-07 21:32:19 +0100414# if RUBY_VERSION >= 23
Bram Moolenaarf2f6d292015-12-28 20:57:10 +0100415static VALUE (*dll_rb_data_typed_object_wrap) (VALUE, void*, const rb_data_type_t *);
416# else
417static VALUE (*dll_rb_data_typed_object_alloc) (VALUE, void*, const rb_data_type_t *);
418# endif
419# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000420static VALUE (*dll_rb_data_object_alloc) (VALUE, void*, RUBY_DATA_FUNC, RUBY_DATA_FUNC);
Bram Moolenaarf2f6d292015-12-28 20:57:10 +0100421# endif
ichizok8bb3fe42021-12-28 15:51:45 +0000422# if RUBY_VERSION >= 31
423static void (*dll_rb_debug_rstring_null_ptr) (const char*);
424# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000425static VALUE (*dll_rb_define_class_under) (VALUE, const char*, VALUE);
426static void (*dll_rb_define_const) (VALUE,const char*,VALUE);
427static void (*dll_rb_define_global_function) (const char*,VALUE(*)(),int);
428static void (*dll_rb_define_method) (VALUE,const char*,VALUE(*)(),int);
429static VALUE (*dll_rb_define_module) (const char*);
430static void (*dll_rb_define_module_function) (VALUE,const char*,VALUE(*)(),int);
431static void (*dll_rb_define_singleton_method) (VALUE,const char*,VALUE(*)(),int);
432static void (*dll_rb_define_virtual_variable) (const char*,VALUE(*)(),void(*)());
433static VALUE *dll_rb_stdout;
Bram Moolenaarb6c8cd82018-07-24 05:41:30 +0200434static VALUE *dll_rb_stderr;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000435static VALUE *dll_rb_eArgError;
436static VALUE *dll_rb_eIndexError;
437static VALUE *dll_rb_eRuntimeError;
438static VALUE *dll_rb_eStandardError;
439static VALUE (*dll_rb_eval_string_protect) (const char*, int*);
Bram Moolenaar41a41412020-01-07 21:32:19 +0100440# if RUBY_VERSION >= 21
Bram Moolenaarf711cb22018-08-01 18:42:13 +0200441static VALUE (*dll_rb_funcallv) (VALUE, ID, int, const VALUE*);
442# else
443static VALUE (*dll_rb_funcall2) (VALUE, ID, int, const VALUE*);
444# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000445static void (*dll_rb_global_variable) (VALUE*);
446static VALUE (*dll_rb_hash_aset) (VALUE, VALUE, VALUE);
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100447static VALUE (*dll_rb_hash_foreach) (VALUE, int (*)(VALUE, VALUE, VALUE), VALUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000448static VALUE (*dll_rb_hash_new) (void);
449static VALUE (*dll_rb_inspect) (VALUE);
450static VALUE (*dll_rb_int2inum) (long);
Bram Moolenaarf711cb22018-08-01 18:42:13 +0200451static ID (*dll_rb_intern) (const char*);
Bram Moolenaar9d20daf2021-02-01 19:31:47 +0100452# if RUBY_VERSION >= 30 || VIM_SIZEOF_INT < VIM_SIZEOF_LONG
Bram Moolenaar2623b4f2012-09-18 16:36:32 +0200453static long (*dll_rb_fix2int) (VALUE);
454static long (*dll_rb_num2int) (VALUE);
455static unsigned long (*dll_rb_num2uint) (VALUE);
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200456# endif
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100457static double (*dll_rb_num2dbl) (VALUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000458static VALUE (*dll_rb_lastline_get) (void);
459static void (*dll_rb_lastline_set) (VALUE);
Bram Moolenaar37badc82018-01-31 20:15:30 +0100460static VALUE (*dll_rb_protect) (VALUE (*)(VALUE), VALUE, int*);
Bram Moolenaard0573012017-10-28 21:11:06 +0200461static void (*dll_rb_load) (VALUE, int);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000462static long (*dll_rb_num2long) (VALUE);
463static unsigned long (*dll_rb_num2ulong) (VALUE);
464static VALUE (*dll_rb_obj_alloc) (VALUE);
465static VALUE (*dll_rb_obj_as_string) (VALUE);
466static VALUE (*dll_rb_obj_id) (VALUE);
467static void (*dll_rb_raise) (VALUE, const char*, ...);
Bram Moolenaar41a41412020-01-07 21:32:19 +0100468# if RUBY_VERSION >= 18
Bram Moolenaarf9b5ef82010-09-29 13:02:53 +0200469static VALUE (*dll_rb_string_value) (volatile VALUE*);
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200470# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000471static char *(*dll_rb_str2cstr) (VALUE,int*);
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200472# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000473static VALUE (*dll_rb_str_cat) (VALUE, const char*, long);
474static VALUE (*dll_rb_str_concat) (VALUE, VALUE);
475static VALUE (*dll_rb_str_new) (const char*, long);
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200476# ifdef need_rb_str_new_cstr
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100477// Ruby may #define rb_str_new2 to use rb_str_new_cstr.
Bram Moolenaar1d2beae2010-05-22 21:56:55 +0200478static VALUE (*dll_rb_str_new_cstr) (const char*);
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200479# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000480static VALUE (*dll_rb_str_new2) (const char*);
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200481# endif
Bram Moolenaar41a41412020-01-07 21:32:19 +0100482# if RUBY_VERSION >= 19
Bram Moolenaar165641d2010-02-17 16:23:09 +0100483static VALUE (*dll_rb_errinfo) (void);
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200484# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000485static VALUE *dll_ruby_errinfo;
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200486# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000487static void (*dll_ruby_init) (void);
488static void (*dll_ruby_init_loadpath) (void);
Bram Moolenaar4f974752019-02-17 17:44:42 +0100489# ifdef MSWIN
Bram Moolenaar41a41412020-01-07 21:32:19 +0100490# if RUBY_VERSION >= 19
Bram Moolenaar4f391792017-01-15 16:59:07 +0100491static void (*dll_ruby_sysinit) (int*, char***);
Bram Moolenaar9d5c84a2018-12-19 20:48:46 +0100492# else
493static void (*dll_NtInitialize) (int*, char***);
494# endif
Bram Moolenaar41a41412020-01-07 21:32:19 +0100495# if RUBY_VERSION >= 18
Bram Moolenaar3ca71f12010-10-27 16:49:47 +0200496static int (*dll_rb_w32_snprintf)(char*, size_t, const char*, ...);
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200497# endif
Bram Moolenaar3ca71f12010-10-27 16:49:47 +0200498# endif
ichizok8bb3fe42021-12-28 15:51:45 +0000499# if RUBY_VERSION >= 31
ichizoke1240652022-01-07 20:01:07 +0000500# ifdef _MSC_VER
501static void (*dll_rb_unexpected_type) (VALUE, int);
502# else
503NORETURN(static void (*dll_rb_unexpected_type) (VALUE, int));
504# endif
ichizok8bb3fe42021-12-28 15:51:45 +0000505# endif
Bram Moolenaar41a41412020-01-07 21:32:19 +0100506# if RUBY_VERSION >= 18
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100507static char * (*dll_rb_string_value_ptr) (volatile VALUE*);
508static VALUE (*dll_rb_float_new) (double);
509static VALUE (*dll_rb_ary_new) (void);
Bram Moolenaar51e9fbf2018-08-11 14:24:11 +0200510static VALUE (*dll_rb_ary_new4) (long n, const VALUE *elts);
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100511static VALUE (*dll_rb_ary_push) (VALUE, VALUE);
Bram Moolenaar41a41412020-01-07 21:32:19 +0100512# if RUBY_VERSION >= 26
Bram Moolenaarb09c6842018-12-27 22:11:01 +0100513static void (*dll_rb_ary_detransient) (VALUE);
514# endif
Bram Moolenaar41a41412020-01-07 21:32:19 +0100515# if (RUBY_VERSION >= 19) || defined(RUBY_INIT_STACK)
Bram Moolenaar10f3a792013-05-20 12:52:29 +0200516# ifdef __ia64
Bram Moolenaar76a86062013-05-11 17:45:48 +0200517static void * (*dll_rb_ia64_bsp) (void);
518static void (*dll_ruby_init_stack)(VALUE*, void*);
Bram Moolenaar10f3a792013-05-20 12:52:29 +0200519# else
Bram Moolenaar76a86062013-05-11 17:45:48 +0200520static void (*dll_ruby_init_stack)(VALUE*);
Bram Moolenaar10f3a792013-05-20 12:52:29 +0200521# endif
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200522# endif
Bram Moolenaar76a86062013-05-11 17:45:48 +0200523# endif
Bram Moolenaar41a41412020-01-07 21:32:19 +0100524# if RUBY_VERSION >= 19
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100525static VALUE (*dll_rb_int2big)(SIGNED_VALUE);
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200526# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000527
Bram Moolenaar41a41412020-01-07 21:32:19 +0100528# if RUBY_VERSION >= 19
Bram Moolenaar165641d2010-02-17 16:23:09 +0100529static void (*dll_ruby_script) (const char*);
530static int (*dll_rb_enc_find_index) (const char*);
531static rb_encoding* (*dll_rb_enc_find) (const char*);
532static VALUE (*dll_rb_enc_str_new) (const char*, long, rb_encoding*);
533static VALUE (*dll_rb_sprintf) (const char*, ...);
Bram Moolenaar7a8ef142010-12-24 13:39:35 +0100534static VALUE (*dll_rb_require) (const char*);
Bram Moolenaardace9f72020-12-28 15:07:45 +0100535static void* (*dll_ruby_options)(int, char**);
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200536# endif
Bram Moolenaar165641d2010-02-17 16:23:09 +0100537
Bram Moolenaara1a118b2014-02-05 22:41:15 +0100538# if defined(USE_RGENGC) && USE_RGENGC
Bram Moolenaar41a41412020-01-07 21:32:19 +0100539# if RUBY_VERSION == 21
Bram Moolenaara1a118b2014-02-05 22:41:15 +0100540static void (*dll_rb_gc_writebarrier_unprotect_promoted)(VALUE);
Bram Moolenaar0c7485f2015-01-14 14:04:10 +0100541# else
542static void (*dll_rb_gc_writebarrier_unprotect)(VALUE obj);
543# endif
Bram Moolenaara1a118b2014-02-05 22:41:15 +0100544# endif
545
Bram Moolenaardace9f72020-12-28 15:07:45 +0100546# if RUBY_VERSION >= 30
Bram Moolenaar9d20daf2021-02-01 19:31:47 +0100547# ifdef _MSC_VER
548static void (*dll_ruby_malloc_size_overflow)(size_t, size_t);
549# else
Bram Moolenaardace9f72020-12-28 15:07:45 +0100550NORETURN(static void (*dll_ruby_malloc_size_overflow)(size_t, size_t));
Bram Moolenaar9d20daf2021-02-01 19:31:47 +0100551# endif
Bram Moolenaardace9f72020-12-28 15:07:45 +0100552# endif
553
Bram Moolenaar0e121402020-12-10 20:50:34 +0100554# if RUBY_VERSION >= 26
555void rb_ary_detransient_stub(VALUE x);
556# endif
557
Bram Moolenaardace9f72020-12-28 15:07:45 +0100558// Do not generate a prototype here, VALUE isn't always defined.
559# ifndef PROTO
560# if RUBY_VERSION >= 19
561# if RUBY_VERSION >= 22
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100562 long
563rb_num2long_stub(VALUE x)
Bram Moolenaardace9f72020-12-28 15:07:45 +0100564# else
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100565 SIGNED_VALUE
566rb_num2long_stub(VALUE x)
Bram Moolenaardace9f72020-12-28 15:07:45 +0100567# endif
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100568{
569 return dll_rb_num2long(x);
570}
Bram Moolenaardace9f72020-12-28 15:07:45 +0100571# if RUBY_VERSION >= 26
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100572 VALUE
573rb_int2big_stub(intptr_t x)
Bram Moolenaardace9f72020-12-28 15:07:45 +0100574# else
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100575 VALUE
576rb_int2big_stub(SIGNED_VALUE x)
Bram Moolenaardace9f72020-12-28 15:07:45 +0100577# endif
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100578{
579 return dll_rb_int2big(x);
580}
Bram Moolenaar9d20daf2021-02-01 19:31:47 +0100581# if RUBY_VERSION >= 30 || VIM_SIZEOF_INT < VIM_SIZEOF_LONG
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100582 long
583rb_fix2int_stub(VALUE x)
Bram Moolenaar0bcdd6e2013-04-14 16:19:03 +0200584{
585 return dll_rb_fix2int(x);
586}
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100587 long
588rb_num2int_stub(VALUE x)
Bram Moolenaar0bcdd6e2013-04-14 16:19:03 +0200589{
590 return dll_rb_num2int(x);
591}
Bram Moolenaardace9f72020-12-28 15:07:45 +0100592# endif
593# if RUBY_VERSION >= 20
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100594 VALUE
Bram Moolenaar886ed692013-02-26 13:41:35 +0100595rb_float_new_in_heap(double d)
596{
597 return dll_rb_float_new(d);
598}
Bram Moolenaardace9f72020-12-28 15:07:45 +0100599# if RUBY_VERSION >= 22
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100600 unsigned long
601rb_num2ulong(VALUE x)
Bram Moolenaardace9f72020-12-28 15:07:45 +0100602# else
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100603 VALUE
604rb_num2ulong(VALUE x)
Bram Moolenaardace9f72020-12-28 15:07:45 +0100605# endif
Bram Moolenaar886ed692013-02-26 13:41:35 +0100606{
607 return (long)RSHIFT((SIGNED_VALUE)(x),1);
608}
Bram Moolenaardace9f72020-12-28 15:07:45 +0100609# endif
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200610# endif
Bram Moolenaardace9f72020-12-28 15:07:45 +0100611# if defined(USE_RGENGC) && USE_RGENGC
612# if RUBY_VERSION == 21
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100613 void
614rb_gc_writebarrier_unprotect_promoted_stub(VALUE obj)
Bram Moolenaara1a118b2014-02-05 22:41:15 +0100615{
Bram Moolenaar90140742014-11-27 17:44:08 +0100616 dll_rb_gc_writebarrier_unprotect_promoted(obj);
Bram Moolenaara1a118b2014-02-05 22:41:15 +0100617}
Bram Moolenaardace9f72020-12-28 15:07:45 +0100618# else
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100619 void
620rb_gc_writebarrier_unprotect_stub(VALUE obj)
Bram Moolenaar0c7485f2015-01-14 14:04:10 +0100621{
622 dll_rb_gc_writebarrier_unprotect(obj);
623}
Bram Moolenaardace9f72020-12-28 15:07:45 +0100624# endif
Bram Moolenaar0c7485f2015-01-14 14:04:10 +0100625# endif
Bram Moolenaardace9f72020-12-28 15:07:45 +0100626# if RUBY_VERSION >= 26
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100627 void
628rb_ary_detransient_stub(VALUE x)
Bram Moolenaarf62fc312019-01-08 20:29:32 +0100629{
630 dll_rb_ary_detransient(x);
631}
Bram Moolenaardace9f72020-12-28 15:07:45 +0100632# endif
633# if RUBY_VERSION >= 30
634 void
635rb_check_type_stub(VALUE obj, int t)
636{
637 dll_rb_check_type(obj, t);
638}
639 unsigned long
640rb_num2uint_stub(VALUE x)
641{
642 return dll_rb_num2uint(x);
643}
644 void
645ruby_malloc_size_overflow_stub(size_t x, size_t y)
646{
647 dll_ruby_malloc_size_overflow(x, y);
648}
649# endif
ichizok8bb3fe42021-12-28 15:51:45 +0000650# if RUBY_VERSION >= 31
651 void
652rb_debug_rstring_null_ptr_stub(const char *func)
653{
654 dll_rb_debug_rstring_null_ptr(func);
655}
656 void
657rb_unexpected_type_stub(VALUE self, int t)
658{
659 dll_rb_unexpected_type(self, t);
660}
661# endif
Bram Moolenaardace9f72020-12-28 15:07:45 +0100662# endif // ifndef PROTO
Bram Moolenaarf62fc312019-01-08 20:29:32 +0100663
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100664static HINSTANCE hinstRuby = NULL; // Instance of ruby.dll
Bram Moolenaar071d4272004-06-13 20:20:40 +0000665
666/*
Bram Moolenaarda2303d2005-08-30 21:55:26 +0000667 * Table of name to function pointer of ruby.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000668 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000669static struct
670{
671 char *name;
672 RUBY_PROC *ptr;
673} ruby_funcname_table[] =
674{
675 {"rb_assoc_new", (RUBY_PROC*)&dll_rb_assoc_new},
676 {"rb_cFalseClass", (RUBY_PROC*)&dll_rb_cFalseClass},
Bram Moolenaar2016ae52016-06-13 20:08:43 +0200677# if defined(USE_RUBY_INTEGER)
Bram Moolenaar06469e92016-06-11 22:26:53 +0200678 {"rb_cInteger", (RUBY_PROC*)&dll_rb_cInteger},
Bram Moolenaar6abda992017-01-09 21:10:31 +0100679# else
680 {"rb_cFixnum", (RUBY_PROC*)&dll_rb_cFixnum},
Bram Moolenaar06469e92016-06-11 22:26:53 +0200681# endif
Bram Moolenaar41a41412020-01-07 21:32:19 +0100682# if RUBY_VERSION >= 20
Bram Moolenaardb3fbe52013-03-07 15:16:21 +0100683 {"rb_cFloat", (RUBY_PROC*)&dll_rb_cFloat},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200684# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000685 {"rb_cNilClass", (RUBY_PROC*)&dll_rb_cNilClass},
686 {"rb_cObject", (RUBY_PROC*)&dll_rb_cObject},
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100687 {"rb_cString", (RUBY_PROC*)&dll_rb_cString},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000688 {"rb_cSymbol", (RUBY_PROC*)&dll_rb_cSymbol},
689 {"rb_cTrueClass", (RUBY_PROC*)&dll_rb_cTrueClass},
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100690 {"rb_class_new_instance", (RUBY_PROC*)&dll_rb_class_new_instance},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000691 {"rb_check_type", (RUBY_PROC*)&dll_rb_check_type},
Bram Moolenaarf2f6d292015-12-28 20:57:10 +0100692# ifdef USE_TYPEDDATA
693 {"rb_check_typeddata", (RUBY_PROC*)&dll_rb_check_typeddata},
694# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000695 {"rb_class_path", (RUBY_PROC*)&dll_rb_class_path},
Bram Moolenaarf2f6d292015-12-28 20:57:10 +0100696# ifdef USE_TYPEDDATA
Bram Moolenaar41a41412020-01-07 21:32:19 +0100697# if RUBY_VERSION >= 23
Bram Moolenaarf2f6d292015-12-28 20:57:10 +0100698 {"rb_data_typed_object_wrap", (RUBY_PROC*)&dll_rb_data_typed_object_wrap},
699# else
700 {"rb_data_typed_object_alloc", (RUBY_PROC*)&dll_rb_data_typed_object_alloc},
701# endif
702# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000703 {"rb_data_object_alloc", (RUBY_PROC*)&dll_rb_data_object_alloc},
Bram Moolenaarf2f6d292015-12-28 20:57:10 +0100704# endif
ichizok8bb3fe42021-12-28 15:51:45 +0000705# if RUBY_VERSION >= 31
706 {"rb_debug_rstring_null_ptr", (RUBY_PROC*)&dll_rb_debug_rstring_null_ptr},
707# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000708 {"rb_define_class_under", (RUBY_PROC*)&dll_rb_define_class_under},
709 {"rb_define_const", (RUBY_PROC*)&dll_rb_define_const},
710 {"rb_define_global_function", (RUBY_PROC*)&dll_rb_define_global_function},
711 {"rb_define_method", (RUBY_PROC*)&dll_rb_define_method},
712 {"rb_define_module", (RUBY_PROC*)&dll_rb_define_module},
713 {"rb_define_module_function", (RUBY_PROC*)&dll_rb_define_module_function},
714 {"rb_define_singleton_method", (RUBY_PROC*)&dll_rb_define_singleton_method},
715 {"rb_define_virtual_variable", (RUBY_PROC*)&dll_rb_define_virtual_variable},
716 {"rb_stdout", (RUBY_PROC*)&dll_rb_stdout},
Bram Moolenaarb6c8cd82018-07-24 05:41:30 +0200717 {"rb_stderr", (RUBY_PROC*)&dll_rb_stderr},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000718 {"rb_eArgError", (RUBY_PROC*)&dll_rb_eArgError},
719 {"rb_eIndexError", (RUBY_PROC*)&dll_rb_eIndexError},
720 {"rb_eRuntimeError", (RUBY_PROC*)&dll_rb_eRuntimeError},
721 {"rb_eStandardError", (RUBY_PROC*)&dll_rb_eStandardError},
722 {"rb_eval_string_protect", (RUBY_PROC*)&dll_rb_eval_string_protect},
Bram Moolenaar41a41412020-01-07 21:32:19 +0100723# if RUBY_VERSION >= 21
Bram Moolenaarf711cb22018-08-01 18:42:13 +0200724 {"rb_funcallv", (RUBY_PROC*)&dll_rb_funcallv},
725# else
726 {"rb_funcall2", (RUBY_PROC*)&dll_rb_funcall2},
727# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000728 {"rb_global_variable", (RUBY_PROC*)&dll_rb_global_variable},
729 {"rb_hash_aset", (RUBY_PROC*)&dll_rb_hash_aset},
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100730 {"rb_hash_foreach", (RUBY_PROC*)&dll_rb_hash_foreach},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000731 {"rb_hash_new", (RUBY_PROC*)&dll_rb_hash_new},
732 {"rb_inspect", (RUBY_PROC*)&dll_rb_inspect},
733 {"rb_int2inum", (RUBY_PROC*)&dll_rb_int2inum},
Bram Moolenaarf711cb22018-08-01 18:42:13 +0200734 {"rb_intern", (RUBY_PROC*)&dll_rb_intern},
Bram Moolenaar9d20daf2021-02-01 19:31:47 +0100735# if RUBY_VERSION >= 30 || VIM_SIZEOF_INT < VIM_SIZEOF_LONG
Bram Moolenaar2623b4f2012-09-18 16:36:32 +0200736 {"rb_fix2int", (RUBY_PROC*)&dll_rb_fix2int},
737 {"rb_num2int", (RUBY_PROC*)&dll_rb_num2int},
738 {"rb_num2uint", (RUBY_PROC*)&dll_rb_num2uint},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200739# endif
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100740 {"rb_num2dbl", (RUBY_PROC*)&dll_rb_num2dbl},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000741 {"rb_lastline_get", (RUBY_PROC*)&dll_rb_lastline_get},
742 {"rb_lastline_set", (RUBY_PROC*)&dll_rb_lastline_set},
Bram Moolenaard0573012017-10-28 21:11:06 +0200743 {"rb_protect", (RUBY_PROC*)&dll_rb_protect},
744 {"rb_load", (RUBY_PROC*)&dll_rb_load},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000745 {"rb_num2long", (RUBY_PROC*)&dll_rb_num2long},
746 {"rb_num2ulong", (RUBY_PROC*)&dll_rb_num2ulong},
747 {"rb_obj_alloc", (RUBY_PROC*)&dll_rb_obj_alloc},
748 {"rb_obj_as_string", (RUBY_PROC*)&dll_rb_obj_as_string},
749 {"rb_obj_id", (RUBY_PROC*)&dll_rb_obj_id},
750 {"rb_raise", (RUBY_PROC*)&dll_rb_raise},
Bram Moolenaar41a41412020-01-07 21:32:19 +0100751# if RUBY_VERSION >= 18
Bram Moolenaarf9b5ef82010-09-29 13:02:53 +0200752 {"rb_string_value", (RUBY_PROC*)&dll_rb_string_value},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200753# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000754 {"rb_str2cstr", (RUBY_PROC*)&dll_rb_str2cstr},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200755# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000756 {"rb_str_cat", (RUBY_PROC*)&dll_rb_str_cat},
757 {"rb_str_concat", (RUBY_PROC*)&dll_rb_str_concat},
758 {"rb_str_new", (RUBY_PROC*)&dll_rb_str_new},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200759# ifdef need_rb_str_new_cstr
Bram Moolenaar1d2beae2010-05-22 21:56:55 +0200760 {"rb_str_new_cstr", (RUBY_PROC*)&dll_rb_str_new_cstr},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200761# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000762 {"rb_str_new2", (RUBY_PROC*)&dll_rb_str_new2},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200763# endif
Bram Moolenaar41a41412020-01-07 21:32:19 +0100764# if RUBY_VERSION >= 19
Bram Moolenaar165641d2010-02-17 16:23:09 +0100765 {"rb_errinfo", (RUBY_PROC*)&dll_rb_errinfo},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200766# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000767 {"ruby_errinfo", (RUBY_PROC*)&dll_ruby_errinfo},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200768# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000769 {"ruby_init", (RUBY_PROC*)&dll_ruby_init},
770 {"ruby_init_loadpath", (RUBY_PROC*)&dll_ruby_init_loadpath},
Bram Moolenaar4f974752019-02-17 17:44:42 +0100771# ifdef MSWIN
Bram Moolenaar41a41412020-01-07 21:32:19 +0100772# if RUBY_VERSION >= 19
Bram Moolenaar4f391792017-01-15 16:59:07 +0100773 {"ruby_sysinit", (RUBY_PROC*)&dll_ruby_sysinit},
Bram Moolenaar9d5c84a2018-12-19 20:48:46 +0100774# else
775 {"NtInitialize", (RUBY_PROC*)&dll_NtInitialize},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200776# endif
Bram Moolenaar41a41412020-01-07 21:32:19 +0100777# if RUBY_VERSION >= 18
Bram Moolenaar071d4272004-06-13 20:20:40 +0000778 {"rb_w32_snprintf", (RUBY_PROC*)&dll_rb_w32_snprintf},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200779# endif
Bram Moolenaar3ca71f12010-10-27 16:49:47 +0200780# endif
ichizok8bb3fe42021-12-28 15:51:45 +0000781# if RUBY_VERSION >= 31
782 {"rb_unexpected_type", (RUBY_PROC*)&dll_rb_unexpected_type},
783# endif
Bram Moolenaar41a41412020-01-07 21:32:19 +0100784# if RUBY_VERSION >= 18
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100785 {"rb_string_value_ptr", (RUBY_PROC*)&dll_rb_string_value_ptr},
Bram Moolenaar41a41412020-01-07 21:32:19 +0100786# if RUBY_VERSION <= 19
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100787 {"rb_float_new", (RUBY_PROC*)&dll_rb_float_new},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200788# else
Bram Moolenaar886ed692013-02-26 13:41:35 +0100789 {"rb_float_new_in_heap", (RUBY_PROC*)&dll_rb_float_new},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200790# endif
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100791 {"rb_ary_new", (RUBY_PROC*)&dll_rb_ary_new},
Bram Moolenaar51e9fbf2018-08-11 14:24:11 +0200792# ifdef RB_ARY_NEW4_MACRO
793 {"rb_ary_new_from_values", (RUBY_PROC*)&dll_rb_ary_new4},
794# else
795 {"rb_ary_new4", (RUBY_PROC*)&dll_rb_ary_new4},
796# endif
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100797 {"rb_ary_push", (RUBY_PROC*)&dll_rb_ary_push},
Bram Moolenaar41a41412020-01-07 21:32:19 +0100798# if RUBY_VERSION >= 26
Bram Moolenaarb09c6842018-12-27 22:11:01 +0100799 {"rb_ary_detransient", (RUBY_PROC*)&dll_rb_ary_detransient},
800# endif
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200801# endif
Bram Moolenaar41a41412020-01-07 21:32:19 +0100802# if RUBY_VERSION >= 19
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100803 {"rb_int2big", (RUBY_PROC*)&dll_rb_int2big},
Bram Moolenaar165641d2010-02-17 16:23:09 +0100804 {"ruby_script", (RUBY_PROC*)&dll_ruby_script},
805 {"rb_enc_find_index", (RUBY_PROC*)&dll_rb_enc_find_index},
806 {"rb_enc_find", (RUBY_PROC*)&dll_rb_enc_find},
807 {"rb_enc_str_new", (RUBY_PROC*)&dll_rb_enc_str_new},
808 {"rb_sprintf", (RUBY_PROC*)&dll_rb_sprintf},
Bram Moolenaar7a8ef142010-12-24 13:39:35 +0100809 {"rb_require", (RUBY_PROC*)&dll_rb_require},
Bram Moolenaar9b1067e2015-11-19 19:33:15 +0100810 {"ruby_options", (RUBY_PROC*)&dll_ruby_options},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200811# endif
Bram Moolenaar41a41412020-01-07 21:32:19 +0100812# if (RUBY_VERSION >= 19) || defined(RUBY_INIT_STACK)
Bram Moolenaar10f3a792013-05-20 12:52:29 +0200813# ifdef __ia64
814 {"rb_ia64_bsp", (RUBY_PROC*)&dll_rb_ia64_bsp},
815# endif
816 {"ruby_init_stack", (RUBY_PROC*)&dll_ruby_init_stack},
817# endif
Bram Moolenaara1a118b2014-02-05 22:41:15 +0100818# if defined(USE_RGENGC) && USE_RGENGC
Bram Moolenaar41a41412020-01-07 21:32:19 +0100819# if RUBY_VERSION == 21
Bram Moolenaara1a118b2014-02-05 22:41:15 +0100820 {"rb_gc_writebarrier_unprotect_promoted", (RUBY_PROC*)&dll_rb_gc_writebarrier_unprotect_promoted},
Bram Moolenaar0c7485f2015-01-14 14:04:10 +0100821# else
822 {"rb_gc_writebarrier_unprotect", (RUBY_PROC*)&dll_rb_gc_writebarrier_unprotect},
823# endif
Bram Moolenaara1a118b2014-02-05 22:41:15 +0100824# endif
Bram Moolenaardace9f72020-12-28 15:07:45 +0100825# if RUBY_VERSION >= 30
826 {"ruby_malloc_size_overflow", (RUBY_PROC*)&dll_ruby_malloc_size_overflow},
827# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000828 {"", NULL},
829};
830
831/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000832 * Load library and get all pointers.
833 * Parameter 'libname' provides name of DLL.
834 * Return OK or FAIL.
835 */
836 static int
837ruby_runtime_link_init(char *libname, int verbose)
838{
839 int i;
840
841 if (hinstRuby)
842 return OK;
Bram Moolenaarf9b5ef82010-09-29 13:02:53 +0200843 hinstRuby = load_dll(libname);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000844 if (!hinstRuby)
845 {
846 if (verbose)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +0000847 semsg(_(e_could_not_load_library_str_str), libname, load_dll_error());
Bram Moolenaar071d4272004-06-13 20:20:40 +0000848 return FAIL;
849 }
850
851 for (i = 0; ruby_funcname_table[i].ptr; ++i)
852 {
Bram Moolenaarf9b5ef82010-09-29 13:02:53 +0200853 if (!(*ruby_funcname_table[i].ptr = symbol_from_dll(hinstRuby,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000854 ruby_funcname_table[i].name)))
855 {
Bram Moolenaarf9b5ef82010-09-29 13:02:53 +0200856 close_dll(hinstRuby);
Bram Moolenaar3ca71f12010-10-27 16:49:47 +0200857 hinstRuby = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000858 if (verbose)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +0000859 semsg(_(e_could_not_load_library_function_str), ruby_funcname_table[i].name);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000860 return FAIL;
861 }
862 }
863 return OK;
864}
865
866/*
867 * If ruby is enabled (there is installed ruby on Windows system) return TRUE,
868 * else FALSE.
869 */
870 int
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100871ruby_enabled(int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000872{
Bram Moolenaar25e4fcd2016-01-09 14:57:47 +0100873 return ruby_runtime_link_init((char *)p_rubydll, verbose) == OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000874}
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100875#endif // defined(DYNAMIC_RUBY) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000876
877 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100878ruby_end(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000879{
Bram Moolenaar071d4272004-06-13 20:20:40 +0000880}
881
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100882 void
883ex_ruby(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000884{
885 int state;
886 char *script = NULL;
887
Bram Moolenaar35a2e192006-03-13 22:07:11 +0000888 script = (char *)script_get(eap, eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000889 if (!eap->skip && ensure_ruby_initialized())
890 {
891 if (script == NULL)
892 rb_eval_string_protect((char *)eap->arg, &state);
893 else
894 rb_eval_string_protect(script, &state);
895 if (state)
896 error_print(state);
897 }
898 vim_free(script);
899}
900
Bram Moolenaar165641d2010-02-17 16:23:09 +0100901/*
902 * In Ruby 1.9 or later, ruby String object has encoding.
903 * conversion buffer string of vim to ruby String object using
904 * VIM encoding option.
905 */
906 static VALUE
907vim_str2rb_enc_str(const char *s)
908{
Bram Moolenaar41a41412020-01-07 21:32:19 +0100909#if RUBY_VERSION >= 19
Bram Moolenaar165641d2010-02-17 16:23:09 +0100910 long lval;
911 char_u *sval;
912 rb_encoding *enc;
913
Yegappan Lakshmanan64095532021-12-06 11:03:55 +0000914 if (get_option_value((char_u *)"enc", &lval, &sval, NULL, 0) == gov_string)
Bram Moolenaar165641d2010-02-17 16:23:09 +0100915 {
916 enc = rb_enc_find((char *)sval);
917 vim_free(sval);
Bram Moolenaar758535a2016-03-30 22:06:16 +0200918 if (enc)
Bram Moolenaar9b0ac222016-06-01 20:31:43 +0200919 return rb_enc_str_new(s, (long)strlen(s), enc);
Bram Moolenaar165641d2010-02-17 16:23:09 +0100920 }
921#endif
922 return rb_str_new2(s);
923}
924
925 static VALUE
926eval_enc_string_protect(const char *str, int *state)
927{
Bram Moolenaar41a41412020-01-07 21:32:19 +0100928#if RUBY_VERSION >= 19
Bram Moolenaar165641d2010-02-17 16:23:09 +0100929 long lval;
930 char_u *sval;
931 rb_encoding *enc;
932 VALUE v;
933
Yegappan Lakshmanan64095532021-12-06 11:03:55 +0000934 if (get_option_value((char_u *)"enc", &lval, &sval, NULL, 0) == gov_string)
Bram Moolenaar165641d2010-02-17 16:23:09 +0100935 {
936 enc = rb_enc_find((char *)sval);
937 vim_free(sval);
938 if (enc)
939 {
940 v = rb_sprintf("#-*- coding:%s -*-\n%s", rb_enc_name(enc), str);
941 return rb_eval_string_protect(StringValuePtr(v), state);
942 }
943 }
944#endif
945 return rb_eval_string_protect(str, state);
946}
947
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100948 void
949ex_rubydo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000950{
951 int state;
952 linenr_T i;
Bram Moolenaarc593fee2017-01-29 23:11:25 +0100953 buf_T *was_curbuf = curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000954
955 if (ensure_ruby_initialized())
956 {
957 if (u_save(eap->line1 - 1, eap->line2 + 1) != OK)
958 return;
Bram Moolenaar758535a2016-03-30 22:06:16 +0200959 for (i = eap->line1; i <= eap->line2; i++)
960 {
Bram Moolenaare980d8a2010-12-08 13:11:21 +0100961 VALUE line;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000962
Bram Moolenaarc593fee2017-01-29 23:11:25 +0100963 if (i > curbuf->b_ml.ml_line_count)
964 break;
Bram Moolenaare980d8a2010-12-08 13:11:21 +0100965 line = vim_str2rb_enc_str((char *)ml_get(i));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000966 rb_lastline_set(line);
Bram Moolenaar165641d2010-02-17 16:23:09 +0100967 eval_enc_string_protect((char *) eap->arg, &state);
Bram Moolenaar758535a2016-03-30 22:06:16 +0200968 if (state)
969 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000970 error_print(state);
971 break;
972 }
Bram Moolenaarc593fee2017-01-29 23:11:25 +0100973 if (was_curbuf != curbuf)
974 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000975 line = rb_lastline_get();
Bram Moolenaar758535a2016-03-30 22:06:16 +0200976 if (!NIL_P(line))
977 {
978 if (TYPE(line) != T_STRING)
979 {
Bram Moolenaar9a846fb2022-01-01 21:59:18 +0000980 emsg(_(e_dollar_must_be_an_instance_of_string));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000981 return;
982 }
Bram Moolenaar165641d2010-02-17 16:23:09 +0100983 ml_replace(i, (char_u *) StringValuePtr(line), 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000984 changed();
985#ifdef SYNTAX_HL
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100986 syn_changed(i); // recompute syntax hl. for this line
Bram Moolenaar071d4272004-06-13 20:20:40 +0000987#endif
988 }
989 }
990 check_cursor();
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100991 update_curbuf(UPD_NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000992 }
993}
994
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100995 static VALUE
996rb_load_wrap(VALUE file_to_load)
Bram Moolenaar37badc82018-01-31 20:15:30 +0100997{
998 rb_load(file_to_load, 0);
999 return Qnil;
1000}
1001
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001002 void
1003ex_rubyfile(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001004{
1005 int state;
1006
1007 if (ensure_ruby_initialized())
1008 {
Bram Moolenaar37badc82018-01-31 20:15:30 +01001009 VALUE file_to_load = rb_str_new2((const char *)eap->arg);
1010 rb_protect(rb_load_wrap, file_to_load, &state);
1011 if (state)
1012 error_print(state);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001013 }
1014}
1015
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001016 void
1017ruby_buffer_free(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001018{
Bram Moolenaare344bea2005-09-01 20:46:49 +00001019 if (buf->b_ruby_ref)
1020 {
1021 rb_hash_aset(objtbl, rb_obj_id((VALUE) buf->b_ruby_ref), Qnil);
1022 RDATA(buf->b_ruby_ref)->data = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001023 }
1024}
1025
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001026 void
1027ruby_window_free(win_T *win)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001028{
Bram Moolenaare344bea2005-09-01 20:46:49 +00001029 if (win->w_ruby_ref)
1030 {
1031 rb_hash_aset(objtbl, rb_obj_id((VALUE) win->w_ruby_ref), Qnil);
1032 RDATA(win->w_ruby_ref)->data = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001033 }
1034}
1035
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001036 static int
1037ensure_ruby_initialized(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001038{
1039 if (!ruby_initialized)
1040 {
1041#ifdef DYNAMIC_RUBY
1042 if (ruby_enabled(TRUE))
1043 {
1044#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01001045#ifdef MSWIN
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001046 // suggested by Ariya Mizutani
Bram Moolenaar0b69c732010-02-17 15:11:50 +01001047 int argc = 1;
1048 char *argv[] = {"gvim.exe"};
Bram Moolenaar90140742014-11-27 17:44:08 +01001049 char **argvp = argv;
Bram Moolenaar41a41412020-01-07 21:32:19 +01001050# if RUBY_VERSION >= 19
Bram Moolenaarfe6ce332017-01-14 20:12:01 +01001051 ruby_sysinit(&argc, &argvp);
1052# else
Bram Moolenaar90140742014-11-27 17:44:08 +01001053 NtInitialize(&argc, &argvp);
Bram Moolenaarfe6ce332017-01-14 20:12:01 +01001054# endif
Bram Moolenaar0b69c732010-02-17 15:11:50 +01001055#endif
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001056 {
Bram Moolenaar41a41412020-01-07 21:32:19 +01001057#if (RUBY_VERSION >= 19) || defined(RUBY_INIT_STACK)
Bram Moolenaar99685e62013-05-11 13:56:18 +02001058 ruby_init_stack(ruby_stack_start);
Bram Moolenaar165641d2010-02-17 16:23:09 +01001059#endif
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001060 ruby_init();
1061 }
Bram Moolenaar41a41412020-01-07 21:32:19 +01001062#if RUBY_VERSION >= 19
Bram Moolenaar7a8ef142010-12-24 13:39:35 +01001063 {
1064 int dummy_argc = 2;
Bram Moolenaard1bc96ce2017-09-26 21:21:44 +02001065 char *dummy_argv[] = {"vim-ruby", "-e_=0"};
Bram Moolenaar9b1067e2015-11-19 19:33:15 +01001066 ruby_options(dummy_argc, dummy_argv);
Bram Moolenaar7a8ef142010-12-24 13:39:35 +01001067 }
Bram Moolenaar165641d2010-02-17 16:23:09 +01001068 ruby_script("vim-ruby");
Bram Moolenaar7a8ef142010-12-24 13:39:35 +01001069#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001070 ruby_init_loadpath();
Bram Moolenaar165641d2010-02-17 16:23:09 +01001071#endif
Bram Moolenaar7a8ef142010-12-24 13:39:35 +01001072 ruby_io_init();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001073 ruby_vim_init();
1074 ruby_initialized = 1;
1075#ifdef DYNAMIC_RUBY
1076 }
1077 else
1078 {
Bram Moolenaar9a846fb2022-01-01 21:59:18 +00001079 emsg(_(e_sorry_this_command_is_disabled_the_ruby_library_could_not_be_loaded));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001080 return 0;
1081 }
1082#endif
1083 }
1084 return ruby_initialized;
1085}
1086
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001087 static void
1088error_print(int state)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001089{
Bram Moolenaar41a41412020-01-07 21:32:19 +01001090#if !defined(DYNAMIC_RUBY) && (RUBY_VERSION <= 18)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001091 RUBYEXTERN VALUE ruby_errinfo;
1092#endif
Bram Moolenaarf711cb22018-08-01 18:42:13 +02001093 VALUE error;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001094 VALUE eclass;
1095 VALUE einfo;
Bram Moolenaarf711cb22018-08-01 18:42:13 +02001096 VALUE bt;
1097 int attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001098 char buff[BUFSIZ];
Bram Moolenaarf711cb22018-08-01 18:42:13 +02001099 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001100
1101#define TAG_RETURN 0x1
1102#define TAG_BREAK 0x2
1103#define TAG_NEXT 0x3
1104#define TAG_RETRY 0x4
1105#define TAG_REDO 0x5
1106#define TAG_RAISE 0x6
1107#define TAG_THROW 0x7
1108#define TAG_FATAL 0x8
1109#define TAG_MASK 0xf
1110
Bram Moolenaar758535a2016-03-30 22:06:16 +02001111 switch (state)
1112 {
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001113 case TAG_RETURN:
Bram Moolenaar9a846fb2022-01-01 21:59:18 +00001114 emsg(_(e_unexpected_return));
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001115 break;
1116 case TAG_NEXT:
Bram Moolenaar9a846fb2022-01-01 21:59:18 +00001117 emsg(_(e_unexpected_next));
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001118 break;
1119 case TAG_BREAK:
Bram Moolenaar9a846fb2022-01-01 21:59:18 +00001120 emsg(_(e_unexpected_break));
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001121 break;
1122 case TAG_REDO:
Bram Moolenaar9a846fb2022-01-01 21:59:18 +00001123 emsg(_(e_unexpected_redo));
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001124 break;
1125 case TAG_RETRY:
Bram Moolenaar9a846fb2022-01-01 21:59:18 +00001126 emsg(_(e_retry_outside_of_rescue_clause));
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001127 break;
1128 case TAG_RAISE:
1129 case TAG_FATAL:
Bram Moolenaar41a41412020-01-07 21:32:19 +01001130#if RUBY_VERSION >= 19
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001131 error = rb_errinfo();
Bram Moolenaar165641d2010-02-17 16:23:09 +01001132#else
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001133 error = ruby_errinfo;
Bram Moolenaar165641d2010-02-17 16:23:09 +01001134#endif
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001135 eclass = CLASS_OF(error);
1136 einfo = rb_obj_as_string(error);
1137 if (eclass == rb_eRuntimeError && RSTRING_LEN(einfo) == 0)
1138 {
Bram Moolenaar9a846fb2022-01-01 21:59:18 +00001139 emsg(_(e_unhandled_exception));
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001140 }
1141 else
1142 {
1143 VALUE epath;
1144 char *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001145
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001146 epath = rb_class_path(eclass);
1147 vim_snprintf(buff, BUFSIZ, "%s: %s",
1148 RSTRING_PTR(epath), RSTRING_PTR(einfo));
1149 p = strchr(buff, '\n');
1150 if (p) *p = '\0';
1151 emsg(buff);
1152 }
Bram Moolenaarf711cb22018-08-01 18:42:13 +02001153
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001154 attr = syn_name2attr((char_u *)"Error");
Bram Moolenaar49c99fc2020-02-11 23:01:39 +01001155#if RUBY_VERSION >= 21
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001156 bt = rb_funcallv(error, rb_intern("backtrace"), 0, 0);
1157 for (i = 0; i < RARRAY_LEN(bt); i++)
1158 msg_attr(RSTRING_PTR(RARRAY_AREF(bt, i)), attr);
Bram Moolenaar49c99fc2020-02-11 23:01:39 +01001159#else
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001160 bt = rb_funcall2(error, rb_intern("backtrace"), 0, 0);
1161 for (i = 0; i < RARRAY_LEN(bt); i++)
1162 msg_attr(RSTRING_PTR(RARRAY_PTR(bt)[i]), attr);
Bram Moolenaar49c99fc2020-02-11 23:01:39 +01001163#endif
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001164 break;
1165 default:
Bram Moolenaar9a846fb2022-01-01 21:59:18 +00001166 vim_snprintf(buff, BUFSIZ, _(e_unknown_longjmp_status_nr), state);
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001167 emsg(buff);
1168 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001169 }
1170}
1171
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001172 static VALUE
1173vim_message(VALUE self UNUSED, VALUE str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001174{
1175 char *buff, *p;
1176
1177 str = rb_obj_as_string(str);
Bram Moolenaar3f5f7952011-08-04 19:34:59 +02001178 if (RSTRING_LEN(str) > 0)
1179 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001180 // Only do this when the string isn't empty, alloc(0) causes trouble.
Bram Moolenaar00ccf542017-09-03 15:17:48 +02001181 buff = ALLOCA_N(char, RSTRING_LEN(str) + 1);
Bram Moolenaar3f5f7952011-08-04 19:34:59 +02001182 strcpy(buff, RSTRING_PTR(str));
1183 p = strchr(buff, '\n');
1184 if (p) *p = '\0';
Bram Moolenaar32526b32019-01-19 17:43:09 +01001185 msg(buff);
Bram Moolenaar3f5f7952011-08-04 19:34:59 +02001186 }
1187 else
1188 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01001189 msg("");
Bram Moolenaar3f5f7952011-08-04 19:34:59 +02001190 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001191 return Qnil;
1192}
1193
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001194 static VALUE
1195vim_set_option(VALUE self UNUSED, VALUE str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001196{
Bram Moolenaar165641d2010-02-17 16:23:09 +01001197 do_set((char_u *)StringValuePtr(str), 0);
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001198 update_screen(UPD_NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001199 return Qnil;
1200}
1201
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001202 static VALUE
1203vim_command(VALUE self UNUSED, VALUE str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001204{
Bram Moolenaar165641d2010-02-17 16:23:09 +01001205 do_cmdline_cmd((char_u *)StringValuePtr(str));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001206 return Qnil;
1207}
1208
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001209#ifdef FEAT_EVAL
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001210 static VALUE
1211vim_to_ruby(typval_T *tv)
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001212{
1213 VALUE result = Qnil;
1214
1215 if (tv->v_type == VAR_STRING)
1216 {
Bram Moolenaar94127e42010-03-19 23:08:48 +01001217 result = rb_str_new2(tv->vval.v_string == NULL
1218 ? "" : (char *)(tv->vval.v_string));
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001219 }
1220 else if (tv->v_type == VAR_NUMBER)
1221 {
Bram Moolenaar639a2552010-03-17 18:15:23 +01001222 result = INT2NUM(tv->vval.v_number);
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001223 }
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001224 else if (tv->v_type == VAR_FLOAT)
1225 {
Bram Moolenaar639a2552010-03-17 18:15:23 +01001226 result = rb_float_new(tv->vval.v_float);
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001227 }
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001228 else if (tv->v_type == VAR_LIST)
1229 {
Bram Moolenaar639a2552010-03-17 18:15:23 +01001230 list_T *list = tv->vval.v_list;
1231 listitem_T *curr;
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001232
Bram Moolenaar639a2552010-03-17 18:15:23 +01001233 result = rb_ary_new();
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001234
Bram Moolenaar639a2552010-03-17 18:15:23 +01001235 if (list != NULL)
1236 {
Bram Moolenaaraeea7212020-04-02 18:50:46 +02001237 FOR_ALL_LIST_ITEMS(list, curr)
Bram Moolenaar639a2552010-03-17 18:15:23 +01001238 rb_ary_push(result, vim_to_ruby(&curr->li_tv));
Bram Moolenaar639a2552010-03-17 18:15:23 +01001239 }
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001240 }
1241 else if (tv->v_type == VAR_DICT)
1242 {
Bram Moolenaar639a2552010-03-17 18:15:23 +01001243 result = rb_hash_new();
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001244
Bram Moolenaar639a2552010-03-17 18:15:23 +01001245 if (tv->vval.v_dict != NULL)
1246 {
1247 hashtab_T *ht = &tv->vval.v_dict->dv_hashtab;
1248 long_u todo = ht->ht_used;
1249 hashitem_T *hi;
1250 dictitem_T *di;
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001251
Bram Moolenaar639a2552010-03-17 18:15:23 +01001252 for (hi = ht->ht_array; todo > 0; ++hi)
1253 {
1254 if (!HASHITEM_EMPTY(hi))
1255 {
1256 --todo;
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001257
Bram Moolenaar639a2552010-03-17 18:15:23 +01001258 di = dict_lookup(hi);
1259 rb_hash_aset(result, rb_str_new2((char *)hi->hi_key),
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001260 vim_to_ruby(&di->di_tv));
Bram Moolenaar639a2552010-03-17 18:15:23 +01001261 }
1262 }
1263 }
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001264 }
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01001265 else if (tv->v_type == VAR_BOOL || tv->v_type == VAR_SPECIAL)
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001266 {
Bram Moolenaard84b26a2018-07-28 17:18:09 +02001267 if (tv->vval.v_number == VVAL_TRUE)
1268 result = Qtrue;
1269 else if (tv->vval.v_number == VVAL_FALSE)
1270 result = Qfalse;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001271 }
1272 else if (tv->v_type == VAR_BLOB)
1273 {
1274 result = rb_str_new(tv->vval.v_blob->bv_ga.ga_data,
1275 tv->vval.v_blob->bv_ga.ga_len);
1276 }
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001277 // else return Qnil;
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001278
1279 return result;
1280}
1281#endif
1282
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001283 static VALUE
1284vim_evaluate(VALUE self UNUSED, VALUE str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001285{
1286#ifdef FEAT_EVAL
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001287 typval_T *tv;
1288 VALUE result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001289
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001290 tv = eval_expr((char_u *)StringValuePtr(str), NULL);
1291 if (tv == NULL)
Bram Moolenaar639a2552010-03-17 18:15:23 +01001292 return Qnil;
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001293 result = vim_to_ruby(tv);
1294
1295 free_tv(tv);
1296
1297 return result;
1298#else
1299 return Qnil;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001300#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001301}
1302
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001303#ifdef USE_TYPEDDATA
1304static size_t buffer_dsize(const void *buf);
1305
1306static const rb_data_type_t buffer_type = {
1307 "vim_buffer",
Bram Moolenaar41a41412020-01-07 21:32:19 +01001308 {0, 0, buffer_dsize,
1309# if RUBY_VERSION >= 27
Bram Moolenaar8b430b42020-02-22 15:01:00 +01001310 0, {0}
Bram Moolenaar41a41412020-01-07 21:32:19 +01001311# else
1312 {0, 0}
1313# endif
1314 },
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001315 0, 0,
1316# ifdef RUBY_TYPED_FREE_IMMEDIATELY
1317 0,
1318# endif
1319};
1320
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001321 static size_t
1322buffer_dsize(const void *buf UNUSED)
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001323{
1324 return sizeof(buf_T);
1325}
1326#endif
1327
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001328 static VALUE
1329buffer_new(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001330{
Bram Moolenaare344bea2005-09-01 20:46:49 +00001331 if (buf->b_ruby_ref)
1332 {
1333 return (VALUE) buf->b_ruby_ref;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001334 }
Bram Moolenaare344bea2005-09-01 20:46:49 +00001335 else
1336 {
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001337#ifdef USE_TYPEDDATA
1338 VALUE obj = TypedData_Wrap_Struct(cBuffer, &buffer_type, buf);
1339#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001340 VALUE obj = Data_Wrap_Struct(cBuffer, 0, 0, buf);
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001341#endif
Bram Moolenaare344bea2005-09-01 20:46:49 +00001342 buf->b_ruby_ref = (void *) obj;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001343 rb_hash_aset(objtbl, rb_obj_id(obj), obj);
1344 return obj;
1345 }
1346}
1347
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001348 static buf_T *
1349get_buf(VALUE obj)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001350{
1351 buf_T *buf;
1352
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001353#ifdef USE_TYPEDDATA
1354 TypedData_Get_Struct(obj, buf_T, &buffer_type, buf);
1355#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001356 Data_Get_Struct(obj, buf_T, buf);
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001357#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001358 if (buf == NULL)
1359 rb_raise(eDeletedBufferError, "attempt to refer to deleted buffer");
1360 return buf;
1361}
1362
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001363 static VALUE
1364vim_blob(VALUE self UNUSED, VALUE str)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001365{
1366 VALUE result = rb_str_new("0z", 2);
1367 char buf[4];
1368 int i;
1369 for (i = 0; i < RSTRING_LEN(str); i++)
1370 {
Bram Moolenaar0d13cce2019-02-23 14:23:03 +01001371 sprintf(buf, "%02X", (unsigned char)(RSTRING_PTR(str)[i]));
Bram Moolenaarb191be22019-01-30 21:00:12 +01001372 rb_str_concat(result, rb_str_new2(buf));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001373 }
1374 return result;
1375}
1376
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001377 static VALUE
Bram Moolenaard5a986f2020-12-06 21:11:31 +01001378buffer_s_current(VALUE self UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001379{
1380 return buffer_new(curbuf);
1381}
1382
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001383 static VALUE
Bram Moolenaard5a986f2020-12-06 21:11:31 +01001384buffer_s_current_getter(ID id UNUSED, VALUE *x UNUSED)
1385{
1386 return buffer_new(curbuf);
1387}
1388
1389 static VALUE
1390buffer_s_count(VALUE self UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001391{
1392 buf_T *b;
1393 int n = 0;
1394
Bram Moolenaar29323592016-07-24 22:04:11 +02001395 FOR_ALL_BUFFERS(b)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001396 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001397 // Deleted buffers should not be counted
1398 // SegPhault - 01/07/05
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001399 if (b->b_p_bl)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001400 n++;
1401 }
1402
Bram Moolenaar071d4272004-06-13 20:20:40 +00001403 return INT2NUM(n);
1404}
1405
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001406 static VALUE
1407buffer_s_aref(VALUE self UNUSED, VALUE num)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001408{
1409 buf_T *b;
1410 int n = NUM2INT(num);
1411
Bram Moolenaar29323592016-07-24 22:04:11 +02001412 FOR_ALL_BUFFERS(b)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001413 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001414 // Deleted buffers should not be counted
1415 // SegPhault - 01/07/05
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001416 if (!b->b_p_bl)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001417 continue;
1418
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001419 if (n == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001420 return buffer_new(b);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001421
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001422 n--;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001423 }
1424 return Qnil;
1425}
1426
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001427 static VALUE
1428buffer_name(VALUE self)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001429{
1430 buf_T *buf = get_buf(self);
1431
Bram Moolenaar35a2e192006-03-13 22:07:11 +00001432 return buf->b_ffname ? rb_str_new2((char *)buf->b_ffname) : Qnil;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001433}
1434
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001435 static VALUE
1436buffer_number(VALUE self)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001437{
1438 buf_T *buf = get_buf(self);
1439
1440 return INT2NUM(buf->b_fnum);
1441}
1442
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001443 static VALUE
1444buffer_count(VALUE self)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001445{
1446 buf_T *buf = get_buf(self);
1447
1448 return INT2NUM(buf->b_ml.ml_line_count);
1449}
1450
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001451 static VALUE
1452get_buffer_line(buf_T *buf, linenr_T n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001453{
Bram Moolenaar3c531602010-11-16 14:46:19 +01001454 if (n <= 0 || n > buf->b_ml.ml_line_count)
1455 rb_raise(rb_eIndexError, "line number %ld out of range", (long)n);
1456 return vim_str2rb_enc_str((char *)ml_get_buf(buf, n, FALSE));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001457}
1458
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001459 static VALUE
1460buffer_aref(VALUE self, VALUE num)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001461{
1462 buf_T *buf = get_buf(self);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001463
1464 if (buf != NULL)
1465 return get_buffer_line(buf, (linenr_T)NUM2LONG(num));
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001466 return Qnil; // For stop warning
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001467}
1468
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001469 static VALUE
1470set_buffer_line(buf_T *buf, linenr_T n, VALUE str)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001471{
Bram Moolenaar165641d2010-02-17 16:23:09 +01001472 char *line = StringValuePtr(str);
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001473 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001474
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001475 if (n > 0 && n <= buf->b_ml.ml_line_count && line != NULL)
1476 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001477 // set curwin/curbuf for "buf" and save some things
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001478 aucmd_prepbuf(&aco, buf);
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001479
Bram Moolenaar758535a2016-03-30 22:06:16 +02001480 if (u_savesub(n) == OK)
1481 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001482 ml_replace(n, (char_u *)line, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001483 changed();
1484#ifdef SYNTAX_HL
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001485 syn_changed(n); // recompute syntax hl. for this line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001486#endif
1487 }
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001488
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001489 // restore curwin/curbuf and a few other things
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001490 aucmd_restbuf(&aco);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001491 // Careful: autocommands may have made "buf" invalid!
Bram Moolenaarf30e74c2006-08-16 17:35:00 +00001492
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001493 update_curbuf(UPD_NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001494 }
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001495 else
1496 {
Bram Moolenaar165641d2010-02-17 16:23:09 +01001497 rb_raise(rb_eIndexError, "line number %ld out of range", (long)n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001498 }
1499 return str;
1500}
1501
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001502 static VALUE
1503buffer_aset(VALUE self, VALUE num, VALUE str)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001504{
1505 buf_T *buf = get_buf(self);
1506
1507 if (buf != NULL)
1508 return set_buffer_line(buf, (linenr_T)NUM2LONG(num), str);
1509 return str;
1510}
1511
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001512 static VALUE
1513buffer_delete(VALUE self, VALUE num)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001514{
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001515 buf_T *buf = get_buf(self);
1516 long n = NUM2LONG(num);
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001517 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001518
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001519 if (n > 0 && n <= buf->b_ml.ml_line_count)
1520 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001521 // set curwin/curbuf for "buf" and save some things
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001522 aucmd_prepbuf(&aco, buf);
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001523
Bram Moolenaar758535a2016-03-30 22:06:16 +02001524 if (u_savedel(n, 1) == OK)
1525 {
Bram Moolenaarca70c072020-05-30 20:30:46 +02001526 ml_delete(n);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001527
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001528 // Changes to non-active buffers should properly refresh
1529 // SegPhault - 01/09/05
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001530 deleted_lines_mark(n, 1L);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001531
Bram Moolenaar071d4272004-06-13 20:20:40 +00001532 changed();
1533 }
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001534
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001535 // restore curwin/curbuf and a few other things
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001536 aucmd_restbuf(&aco);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001537 // Careful: autocommands may have made "buf" invalid!
Bram Moolenaarf30e74c2006-08-16 17:35:00 +00001538
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001539 update_curbuf(UPD_NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001540 }
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001541 else
1542 {
Bram Moolenaar165641d2010-02-17 16:23:09 +01001543 rb_raise(rb_eIndexError, "line number %ld out of range", n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001544 }
1545 return Qnil;
1546}
1547
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001548 static VALUE
1549buffer_append(VALUE self, VALUE num, VALUE str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001550{
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001551 buf_T *buf = get_buf(self);
Bram Moolenaar165641d2010-02-17 16:23:09 +01001552 char *line = StringValuePtr(str);
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001553 long n = NUM2LONG(num);
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001554 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001555
Bram Moolenaar3c531602010-11-16 14:46:19 +01001556 if (line == NULL)
1557 {
Bram Moolenaar165641d2010-02-17 16:23:09 +01001558 rb_raise(rb_eIndexError, "NULL line");
1559 }
1560 else if (n >= 0 && n <= buf->b_ml.ml_line_count)
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001561 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001562 // set curwin/curbuf for "buf" and save some things
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001563 aucmd_prepbuf(&aco, buf);
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001564
Bram Moolenaar758535a2016-03-30 22:06:16 +02001565 if (u_inssub(n + 1) == OK)
1566 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001567 ml_append(n, (char_u *) line, (colnr_T) 0, FALSE);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001568
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001569 // Changes to non-active buffers should properly refresh screen
1570 // SegPhault - 12/20/04
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001571 appended_lines_mark(n, 1L);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001572
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001573 changed();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001574 }
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001575
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001576 // restore curwin/curbuf and a few other things
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001577 aucmd_restbuf(&aco);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001578 // Careful: autocommands may have made "buf" invalid!
Bram Moolenaarf30e74c2006-08-16 17:35:00 +00001579
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001580 update_curbuf(UPD_NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001581 }
Bram Moolenaar3c531602010-11-16 14:46:19 +01001582 else
1583 {
Bram Moolenaar165641d2010-02-17 16:23:09 +01001584 rb_raise(rb_eIndexError, "line number %ld out of range", n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001585 }
1586 return str;
1587}
1588
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001589#ifdef USE_TYPEDDATA
1590static size_t window_dsize(const void *buf);
1591
1592static const rb_data_type_t window_type = {
1593 "vim_window",
Bram Moolenaar41a41412020-01-07 21:32:19 +01001594 {0, 0, window_dsize,
1595# if RUBY_VERSION >= 27
Bram Moolenaar8b430b42020-02-22 15:01:00 +01001596 0, {0}
Bram Moolenaar41a41412020-01-07 21:32:19 +01001597# else
1598 {0, 0}
1599# endif
1600 },
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001601 0, 0,
1602# ifdef RUBY_TYPED_FREE_IMMEDIATELY
1603 0,
1604# endif
1605};
1606
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001607 static size_t
1608window_dsize(const void *win UNUSED)
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001609{
1610 return sizeof(win_T);
1611}
1612#endif
1613
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001614 static VALUE
1615window_new(win_T *win)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001616{
Bram Moolenaare344bea2005-09-01 20:46:49 +00001617 if (win->w_ruby_ref)
1618 {
1619 return (VALUE) win->w_ruby_ref;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001620 }
Bram Moolenaare344bea2005-09-01 20:46:49 +00001621 else
1622 {
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001623#ifdef USE_TYPEDDATA
1624 VALUE obj = TypedData_Wrap_Struct(cVimWindow, &window_type, win);
1625#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001626 VALUE obj = Data_Wrap_Struct(cVimWindow, 0, 0, win);
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001627#endif
Bram Moolenaare344bea2005-09-01 20:46:49 +00001628 win->w_ruby_ref = (void *) obj;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001629 rb_hash_aset(objtbl, rb_obj_id(obj), obj);
1630 return obj;
1631 }
1632}
1633
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001634 static win_T *
1635get_win(VALUE obj)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001636{
1637 win_T *win;
1638
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001639#ifdef USE_TYPEDDATA
1640 TypedData_Get_Struct(obj, win_T, &window_type, win);
1641#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001642 Data_Get_Struct(obj, win_T, win);
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001643#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001644 if (win == NULL)
1645 rb_raise(eDeletedWindowError, "attempt to refer to deleted window");
1646 return win;
1647}
1648
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001649 static VALUE
Bram Moolenaard5a986f2020-12-06 21:11:31 +01001650window_s_current(VALUE self UNUSED)
1651{
1652 return window_new(curwin);
1653}
1654
1655 static VALUE
1656window_s_current_getter(ID id UNUSED, VALUE *x UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001657{
1658 return window_new(curwin);
1659}
1660
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001661/*
1662 * Added line manipulation functions
1663 * SegPhault - 03/07/05
1664 */
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001665 static VALUE
Bram Moolenaard5a986f2020-12-06 21:11:31 +01001666line_s_current(VALUE self UNUSED)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001667{
1668 return get_buffer_line(curbuf, curwin->w_cursor.lnum);
1669}
1670
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001671 static VALUE
1672set_current_line(VALUE self UNUSED, VALUE str)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001673{
1674 return set_buffer_line(curbuf, curwin->w_cursor.lnum, str);
1675}
1676
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001677 static VALUE
Bram Moolenaard5a986f2020-12-06 21:11:31 +01001678current_line_number(VALUE self UNUSED)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001679{
1680 return INT2FIX((int)curwin->w_cursor.lnum);
1681}
1682
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001683 static VALUE
Bram Moolenaard5a986f2020-12-06 21:11:31 +01001684window_s_count(VALUE self UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001685{
Bram Moolenaar071d4272004-06-13 20:20:40 +00001686 win_T *w;
1687 int n = 0;
1688
Bram Moolenaar29323592016-07-24 22:04:11 +02001689 FOR_ALL_WINDOWS(w)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001690 n++;
1691 return INT2NUM(n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001692}
1693
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001694 static VALUE
1695window_s_aref(VALUE self UNUSED, VALUE num)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001696{
1697 win_T *w;
1698 int n = NUM2INT(num);
1699
Bram Moolenaar071d4272004-06-13 20:20:40 +00001700 for (w = firstwin; w != NULL; w = w->w_next, --n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001701 if (n == 0)
1702 return window_new(w);
1703 return Qnil;
1704}
1705
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001706 static VALUE
1707window_buffer(VALUE self)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001708{
1709 win_T *win = get_win(self);
1710
1711 return buffer_new(win->w_buffer);
1712}
1713
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001714 static VALUE
1715window_height(VALUE self)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001716{
1717 win_T *win = get_win(self);
1718
1719 return INT2NUM(win->w_height);
1720}
1721
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001722 static VALUE
1723window_set_height(VALUE self, VALUE height)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001724{
1725 win_T *win = get_win(self);
1726 win_T *savewin = curwin;
1727
1728 curwin = win;
1729 win_setheight(NUM2INT(height));
1730 curwin = savewin;
1731 return height;
1732}
1733
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001734 static VALUE
1735window_width(VALUE self UNUSED)
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001736{
Bram Moolenaare745d752017-09-22 16:56:22 +02001737 return INT2NUM(get_win(self)->w_width);
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001738}
1739
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001740 static VALUE
1741window_set_width(VALUE self UNUSED, VALUE width)
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001742{
1743 win_T *win = get_win(self);
1744 win_T *savewin = curwin;
1745
1746 curwin = win;
1747 win_setwidth(NUM2INT(width));
1748 curwin = savewin;
1749 return width;
1750}
1751
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001752 static VALUE
1753window_cursor(VALUE self)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001754{
1755 win_T *win = get_win(self);
1756
1757 return rb_assoc_new(INT2NUM(win->w_cursor.lnum), INT2NUM(win->w_cursor.col));
1758}
1759
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001760 static VALUE
1761window_set_cursor(VALUE self, VALUE pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001762{
1763 VALUE lnum, col;
1764 win_T *win = get_win(self);
1765
1766 Check_Type(pos, T_ARRAY);
Bram Moolenaar165641d2010-02-17 16:23:09 +01001767 if (RARRAY_LEN(pos) != 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001768 rb_raise(rb_eArgError, "array length must be 2");
Bram Moolenaar165641d2010-02-17 16:23:09 +01001769 lnum = RARRAY_PTR(pos)[0];
1770 col = RARRAY_PTR(pos)[1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001771 win->w_cursor.lnum = NUM2LONG(lnum);
1772 win->w_cursor.col = NUM2UINT(col);
Bram Moolenaar53901442018-07-25 22:02:36 +02001773 win->w_set_curswant = TRUE;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001774 check_cursor(); // put cursor on an existing line
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001775 update_screen(UPD_NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001776 return Qnil;
1777}
1778
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001779 static VALUE
1780f_nop(VALUE self UNUSED)
Bram Moolenaar35df7d22012-04-20 18:05:47 +02001781{
1782 return Qnil;
1783}
1784
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001785 static VALUE
1786f_p(int argc, VALUE *argv, VALUE self UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001787{
1788 int i;
1789 VALUE str = rb_str_new("", 0);
Bram Moolenaar51e9fbf2018-08-11 14:24:11 +02001790 VALUE ret = Qnil;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001791
Bram Moolenaar758535a2016-03-30 22:06:16 +02001792 for (i = 0; i < argc; i++)
1793 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001794 if (i > 0) rb_str_cat(str, ", ", 2);
1795 rb_str_concat(str, rb_inspect(argv[i]));
1796 }
Bram Moolenaar32526b32019-01-19 17:43:09 +01001797 msg(RSTRING_PTR(str));
Bram Moolenaar51e9fbf2018-08-11 14:24:11 +02001798
1799 if (argc == 1)
1800 ret = argv[0];
1801 else if (argc > 1)
1802 ret = rb_ary_new4(argc, argv);
1803 return ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001804}
1805
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001806 static void
1807ruby_io_init(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001808{
1809#ifndef DYNAMIC_RUBY
1810 RUBYEXTERN VALUE rb_stdout;
Bram Moolenaarb6c8cd82018-07-24 05:41:30 +02001811 RUBYEXTERN VALUE rb_stderr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001812#endif
1813
1814 rb_stdout = rb_obj_alloc(rb_cObject);
Bram Moolenaarb6c8cd82018-07-24 05:41:30 +02001815 rb_stderr = rb_obj_alloc(rb_cObject);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001816 rb_define_singleton_method(rb_stdout, "write", vim_message, 1);
Bram Moolenaar35df7d22012-04-20 18:05:47 +02001817 rb_define_singleton_method(rb_stdout, "flush", f_nop, 0);
Bram Moolenaarb6c8cd82018-07-24 05:41:30 +02001818 rb_define_singleton_method(rb_stderr, "write", vim_message, 1);
1819 rb_define_singleton_method(rb_stderr, "flush", f_nop, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001820 rb_define_global_function("p", f_p, -1);
1821}
1822
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001823 static void
1824ruby_vim_init(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001825{
1826 objtbl = rb_hash_new();
1827 rb_global_variable(&objtbl);
1828
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001829 // The Vim module used to be called "VIM", but "Vim" is better. Make an
1830 // alias "VIM" for backwards compatibility.
Bram Moolenaarf711faf2007-05-10 16:48:19 +00001831 mVIM = rb_define_module("Vim");
1832 rb_define_const(rb_cObject, "VIM", mVIM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001833 rb_define_const(mVIM, "VERSION_MAJOR", INT2NUM(VIM_VERSION_MAJOR));
1834 rb_define_const(mVIM, "VERSION_MINOR", INT2NUM(VIM_VERSION_MINOR));
1835 rb_define_const(mVIM, "VERSION_BUILD", INT2NUM(VIM_VERSION_BUILD));
1836 rb_define_const(mVIM, "VERSION_PATCHLEVEL", INT2NUM(VIM_VERSION_PATCHLEVEL));
1837 rb_define_const(mVIM, "VERSION_SHORT", rb_str_new2(VIM_VERSION_SHORT));
1838 rb_define_const(mVIM, "VERSION_MEDIUM", rb_str_new2(VIM_VERSION_MEDIUM));
1839 rb_define_const(mVIM, "VERSION_LONG", rb_str_new2(VIM_VERSION_LONG));
1840 rb_define_const(mVIM, "VERSION_LONG_DATE", rb_str_new2(VIM_VERSION_LONG_DATE));
1841 rb_define_module_function(mVIM, "message", vim_message, 1);
1842 rb_define_module_function(mVIM, "set_option", vim_set_option, 1);
1843 rb_define_module_function(mVIM, "command", vim_command, 1);
1844 rb_define_module_function(mVIM, "evaluate", vim_evaluate, 1);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001845 rb_define_module_function(mVIM, "blob", vim_blob, 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001846
1847 eDeletedBufferError = rb_define_class_under(mVIM, "DeletedBufferError",
1848 rb_eStandardError);
1849 eDeletedWindowError = rb_define_class_under(mVIM, "DeletedWindowError",
1850 rb_eStandardError);
1851
1852 cBuffer = rb_define_class_under(mVIM, "Buffer", rb_cObject);
1853 rb_define_singleton_method(cBuffer, "current", buffer_s_current, 0);
1854 rb_define_singleton_method(cBuffer, "count", buffer_s_count, 0);
1855 rb_define_singleton_method(cBuffer, "[]", buffer_s_aref, 1);
1856 rb_define_method(cBuffer, "name", buffer_name, 0);
1857 rb_define_method(cBuffer, "number", buffer_number, 0);
1858 rb_define_method(cBuffer, "count", buffer_count, 0);
1859 rb_define_method(cBuffer, "length", buffer_count, 0);
1860 rb_define_method(cBuffer, "[]", buffer_aref, 1);
1861 rb_define_method(cBuffer, "[]=", buffer_aset, 2);
1862 rb_define_method(cBuffer, "delete", buffer_delete, 1);
1863 rb_define_method(cBuffer, "append", buffer_append, 2);
1864
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001865 // Added line manipulation functions
1866 // SegPhault - 03/07/05
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001867 rb_define_method(cBuffer, "line_number", current_line_number, 0);
1868 rb_define_method(cBuffer, "line", line_s_current, 0);
1869 rb_define_method(cBuffer, "line=", set_current_line, 1);
1870
1871
Bram Moolenaar071d4272004-06-13 20:20:40 +00001872 cVimWindow = rb_define_class_under(mVIM, "Window", rb_cObject);
1873 rb_define_singleton_method(cVimWindow, "current", window_s_current, 0);
1874 rb_define_singleton_method(cVimWindow, "count", window_s_count, 0);
1875 rb_define_singleton_method(cVimWindow, "[]", window_s_aref, 1);
1876 rb_define_method(cVimWindow, "buffer", window_buffer, 0);
1877 rb_define_method(cVimWindow, "height", window_height, 0);
1878 rb_define_method(cVimWindow, "height=", window_set_height, 1);
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001879 rb_define_method(cVimWindow, "width", window_width, 0);
1880 rb_define_method(cVimWindow, "width=", window_set_width, 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001881 rb_define_method(cVimWindow, "cursor", window_cursor, 0);
1882 rb_define_method(cVimWindow, "cursor=", window_set_cursor, 1);
1883
Bram Moolenaard5a986f2020-12-06 21:11:31 +01001884 rb_define_virtual_variable("$curbuf", buffer_s_current_getter, 0);
1885 rb_define_virtual_variable("$curwin", window_s_current_getter, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001886}
Bram Moolenaar99685e62013-05-11 13:56:18 +02001887
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001888 void
1889vim_ruby_init(void *stack_start)
Bram Moolenaar99685e62013-05-11 13:56:18 +02001890{
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001891 // should get machine stack start address early in main function
Bram Moolenaar99685e62013-05-11 13:56:18 +02001892 ruby_stack_start = stack_start;
1893}
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001894
1895 static int
1896convert_hash2dict(VALUE key, VALUE val, VALUE arg)
1897{
1898 dict_T *d = (dict_T *)arg;
1899 dictitem_T *di;
1900
Bram Moolenaardace9f72020-12-28 15:07:45 +01001901 di = dictitem_alloc((char_u *)RSTRING_PTR(rb_obj_as_string(key)));
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001902 if (di == NULL || ruby_convert_to_vim_value(val, &di->di_tv) != OK
1903 || dict_add(d, di) != OK)
1904 {
1905 d->dv_hashtab.ht_error = TRUE;
1906 return ST_STOP;
1907 }
1908 return ST_CONTINUE;
1909}
1910
1911 static int
1912ruby_convert_to_vim_value(VALUE val, typval_T *rettv)
1913{
1914 switch (TYPE(val))
1915 {
1916 case T_NIL:
1917 rettv->v_type = VAR_SPECIAL;
1918 rettv->vval.v_number = VVAL_NULL;
1919 break;
1920 case T_TRUE:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01001921 rettv->v_type = VAR_BOOL;
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001922 rettv->vval.v_number = VVAL_TRUE;
1923 break;
1924 case T_FALSE:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01001925 rettv->v_type = VAR_BOOL;
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001926 rettv->vval.v_number = VVAL_FALSE;
1927 break;
1928 case T_BIGNUM:
1929 case T_FIXNUM:
1930 rettv->v_type = VAR_NUMBER;
1931 rettv->vval.v_number = (varnumber_T)NUM2LONG(val);
1932 break;
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001933 case T_FLOAT:
1934 rettv->v_type = VAR_FLOAT;
1935 rettv->vval.v_float = (float_T)NUM2DBL(val);
1936 break;
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001937 default:
1938 val = rb_obj_as_string(val);
1939 // FALLTHROUGH
1940 case T_STRING:
1941 {
1942 VALUE str = (VALUE)RSTRING(val);
1943
1944 rettv->v_type = VAR_STRING;
1945 rettv->vval.v_string = vim_strnsave((char_u *)RSTRING_PTR(str),
Bram Moolenaar71ccd032020-06-12 22:59:11 +02001946 RSTRING_LEN(str));
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001947 }
1948 break;
1949 case T_ARRAY:
1950 {
1951 list_T *l;
1952 long i;
1953 typval_T v;
1954
1955 l = list_alloc();
1956 if (l == NULL)
1957 return FAIL;
1958
1959 for (i = 0; i < RARRAY_LEN(val); ++i)
1960 {
1961 if (ruby_convert_to_vim_value((VALUE)RARRAY_PTR(val)[i],
1962 &v) != OK)
1963 {
1964 list_unref(l);
1965 return FAIL;
1966 }
1967 list_append_tv(l, &v);
1968 clear_tv(&v);
1969 }
1970
1971 rettv->v_type = VAR_LIST;
1972 rettv->vval.v_list = l;
1973 ++l->lv_refcount;
1974 }
1975 break;
1976 case T_HASH:
1977 {
1978 dict_T *d;
1979
1980 d = dict_alloc();
1981 if (d == NULL)
1982 return FAIL;
1983
1984 rb_hash_foreach(val, convert_hash2dict, (VALUE)d);
1985 if (d->dv_hashtab.ht_error)
1986 {
1987 dict_unref(d);
1988 return FAIL;
1989 }
1990
1991 rettv->v_type = VAR_DICT;
1992 rettv->vval.v_dict = d;
1993 ++d->dv_refcount;
1994 }
1995 break;
1996 }
1997 return OK;
1998}
1999
2000 void
2001do_rubyeval(char_u *str, typval_T *rettv)
2002{
2003 int retval = FAIL;
2004
2005 if (ensure_ruby_initialized())
2006 {
2007 int state;
2008 VALUE obj;
2009
2010 obj = rb_eval_string_protect((const char *)str, &state);
2011 if (state)
2012 error_print(state);
2013 else
2014 retval = ruby_convert_to_vim_value(obj, rettv);
2015 }
2016 if (retval == FAIL)
2017 {
2018 rettv->v_type = VAR_NUMBER;
2019 rettv->vval.v_number = 0;
2020 }
2021}