blob: 68c1e591ac146b8bf1aad10ca5ffc3c69639bdab [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();
991 update_curbuf(NOT_VALID);
992 }
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 Moolenaar071d4272004-06-13 20:20:40 +00001198 update_screen(NOT_VALID);
1199 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 }
1224# ifdef FEAT_FLOAT
1225 else if (tv->v_type == VAR_FLOAT)
1226 {
Bram Moolenaar639a2552010-03-17 18:15:23 +01001227 result = rb_float_new(tv->vval.v_float);
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001228 }
1229# endif
1230 else if (tv->v_type == VAR_LIST)
1231 {
Bram Moolenaar639a2552010-03-17 18:15:23 +01001232 list_T *list = tv->vval.v_list;
1233 listitem_T *curr;
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001234
Bram Moolenaar639a2552010-03-17 18:15:23 +01001235 result = rb_ary_new();
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001236
Bram Moolenaar639a2552010-03-17 18:15:23 +01001237 if (list != NULL)
1238 {
Bram Moolenaaraeea7212020-04-02 18:50:46 +02001239 FOR_ALL_LIST_ITEMS(list, curr)
Bram Moolenaar639a2552010-03-17 18:15:23 +01001240 rb_ary_push(result, vim_to_ruby(&curr->li_tv));
Bram Moolenaar639a2552010-03-17 18:15:23 +01001241 }
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001242 }
1243 else if (tv->v_type == VAR_DICT)
1244 {
Bram Moolenaar639a2552010-03-17 18:15:23 +01001245 result = rb_hash_new();
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001246
Bram Moolenaar639a2552010-03-17 18:15:23 +01001247 if (tv->vval.v_dict != NULL)
1248 {
1249 hashtab_T *ht = &tv->vval.v_dict->dv_hashtab;
1250 long_u todo = ht->ht_used;
1251 hashitem_T *hi;
1252 dictitem_T *di;
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001253
Bram Moolenaar639a2552010-03-17 18:15:23 +01001254 for (hi = ht->ht_array; todo > 0; ++hi)
1255 {
1256 if (!HASHITEM_EMPTY(hi))
1257 {
1258 --todo;
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001259
Bram Moolenaar639a2552010-03-17 18:15:23 +01001260 di = dict_lookup(hi);
1261 rb_hash_aset(result, rb_str_new2((char *)hi->hi_key),
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001262 vim_to_ruby(&di->di_tv));
Bram Moolenaar639a2552010-03-17 18:15:23 +01001263 }
1264 }
1265 }
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001266 }
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01001267 else if (tv->v_type == VAR_BOOL || tv->v_type == VAR_SPECIAL)
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001268 {
Bram Moolenaard84b26a2018-07-28 17:18:09 +02001269 if (tv->vval.v_number == VVAL_TRUE)
1270 result = Qtrue;
1271 else if (tv->vval.v_number == VVAL_FALSE)
1272 result = Qfalse;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001273 }
1274 else if (tv->v_type == VAR_BLOB)
1275 {
1276 result = rb_str_new(tv->vval.v_blob->bv_ga.ga_data,
1277 tv->vval.v_blob->bv_ga.ga_len);
1278 }
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001279 // else return Qnil;
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001280
1281 return result;
1282}
1283#endif
1284
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001285 static VALUE
1286vim_evaluate(VALUE self UNUSED, VALUE str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001287{
1288#ifdef FEAT_EVAL
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001289 typval_T *tv;
1290 VALUE result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001291
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001292 tv = eval_expr((char_u *)StringValuePtr(str), NULL);
1293 if (tv == NULL)
Bram Moolenaar639a2552010-03-17 18:15:23 +01001294 return Qnil;
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001295 result = vim_to_ruby(tv);
1296
1297 free_tv(tv);
1298
1299 return result;
1300#else
1301 return Qnil;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001302#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001303}
1304
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001305#ifdef USE_TYPEDDATA
1306static size_t buffer_dsize(const void *buf);
1307
1308static const rb_data_type_t buffer_type = {
1309 "vim_buffer",
Bram Moolenaar41a41412020-01-07 21:32:19 +01001310 {0, 0, buffer_dsize,
1311# if RUBY_VERSION >= 27
Bram Moolenaar8b430b42020-02-22 15:01:00 +01001312 0, {0}
Bram Moolenaar41a41412020-01-07 21:32:19 +01001313# else
1314 {0, 0}
1315# endif
1316 },
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001317 0, 0,
1318# ifdef RUBY_TYPED_FREE_IMMEDIATELY
1319 0,
1320# endif
1321};
1322
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001323 static size_t
1324buffer_dsize(const void *buf UNUSED)
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001325{
1326 return sizeof(buf_T);
1327}
1328#endif
1329
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001330 static VALUE
1331buffer_new(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001332{
Bram Moolenaare344bea2005-09-01 20:46:49 +00001333 if (buf->b_ruby_ref)
1334 {
1335 return (VALUE) buf->b_ruby_ref;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001336 }
Bram Moolenaare344bea2005-09-01 20:46:49 +00001337 else
1338 {
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001339#ifdef USE_TYPEDDATA
1340 VALUE obj = TypedData_Wrap_Struct(cBuffer, &buffer_type, buf);
1341#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001342 VALUE obj = Data_Wrap_Struct(cBuffer, 0, 0, buf);
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001343#endif
Bram Moolenaare344bea2005-09-01 20:46:49 +00001344 buf->b_ruby_ref = (void *) obj;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001345 rb_hash_aset(objtbl, rb_obj_id(obj), obj);
1346 return obj;
1347 }
1348}
1349
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001350 static buf_T *
1351get_buf(VALUE obj)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001352{
1353 buf_T *buf;
1354
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001355#ifdef USE_TYPEDDATA
1356 TypedData_Get_Struct(obj, buf_T, &buffer_type, buf);
1357#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001358 Data_Get_Struct(obj, buf_T, buf);
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001359#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001360 if (buf == NULL)
1361 rb_raise(eDeletedBufferError, "attempt to refer to deleted buffer");
1362 return buf;
1363}
1364
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001365 static VALUE
1366vim_blob(VALUE self UNUSED, VALUE str)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001367{
1368 VALUE result = rb_str_new("0z", 2);
1369 char buf[4];
1370 int i;
1371 for (i = 0; i < RSTRING_LEN(str); i++)
1372 {
Bram Moolenaar0d13cce2019-02-23 14:23:03 +01001373 sprintf(buf, "%02X", (unsigned char)(RSTRING_PTR(str)[i]));
Bram Moolenaarb191be22019-01-30 21:00:12 +01001374 rb_str_concat(result, rb_str_new2(buf));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001375 }
1376 return result;
1377}
1378
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001379 static VALUE
Bram Moolenaard5a986f2020-12-06 21:11:31 +01001380buffer_s_current(VALUE self UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001381{
1382 return buffer_new(curbuf);
1383}
1384
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001385 static VALUE
Bram Moolenaard5a986f2020-12-06 21:11:31 +01001386buffer_s_current_getter(ID id UNUSED, VALUE *x UNUSED)
1387{
1388 return buffer_new(curbuf);
1389}
1390
1391 static VALUE
1392buffer_s_count(VALUE self UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001393{
1394 buf_T *b;
1395 int n = 0;
1396
Bram Moolenaar29323592016-07-24 22:04:11 +02001397 FOR_ALL_BUFFERS(b)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001398 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001399 // Deleted buffers should not be counted
1400 // SegPhault - 01/07/05
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001401 if (b->b_p_bl)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001402 n++;
1403 }
1404
Bram Moolenaar071d4272004-06-13 20:20:40 +00001405 return INT2NUM(n);
1406}
1407
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001408 static VALUE
1409buffer_s_aref(VALUE self UNUSED, VALUE num)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001410{
1411 buf_T *b;
1412 int n = NUM2INT(num);
1413
Bram Moolenaar29323592016-07-24 22:04:11 +02001414 FOR_ALL_BUFFERS(b)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001415 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001416 // Deleted buffers should not be counted
1417 // SegPhault - 01/07/05
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001418 if (!b->b_p_bl)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001419 continue;
1420
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001421 if (n == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001422 return buffer_new(b);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001423
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001424 n--;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001425 }
1426 return Qnil;
1427}
1428
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001429 static VALUE
1430buffer_name(VALUE self)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001431{
1432 buf_T *buf = get_buf(self);
1433
Bram Moolenaar35a2e192006-03-13 22:07:11 +00001434 return buf->b_ffname ? rb_str_new2((char *)buf->b_ffname) : Qnil;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001435}
1436
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001437 static VALUE
1438buffer_number(VALUE self)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001439{
1440 buf_T *buf = get_buf(self);
1441
1442 return INT2NUM(buf->b_fnum);
1443}
1444
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001445 static VALUE
1446buffer_count(VALUE self)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001447{
1448 buf_T *buf = get_buf(self);
1449
1450 return INT2NUM(buf->b_ml.ml_line_count);
1451}
1452
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001453 static VALUE
1454get_buffer_line(buf_T *buf, linenr_T n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001455{
Bram Moolenaar3c531602010-11-16 14:46:19 +01001456 if (n <= 0 || n > buf->b_ml.ml_line_count)
1457 rb_raise(rb_eIndexError, "line number %ld out of range", (long)n);
1458 return vim_str2rb_enc_str((char *)ml_get_buf(buf, n, FALSE));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001459}
1460
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001461 static VALUE
1462buffer_aref(VALUE self, VALUE num)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001463{
1464 buf_T *buf = get_buf(self);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001465
1466 if (buf != NULL)
1467 return get_buffer_line(buf, (linenr_T)NUM2LONG(num));
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001468 return Qnil; // For stop warning
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001469}
1470
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001471 static VALUE
1472set_buffer_line(buf_T *buf, linenr_T n, VALUE str)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001473{
Bram Moolenaar165641d2010-02-17 16:23:09 +01001474 char *line = StringValuePtr(str);
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001475 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001476
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001477 if (n > 0 && n <= buf->b_ml.ml_line_count && line != NULL)
1478 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001479 // set curwin/curbuf for "buf" and save some things
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001480 aucmd_prepbuf(&aco, buf);
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001481
Bram Moolenaar758535a2016-03-30 22:06:16 +02001482 if (u_savesub(n) == OK)
1483 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001484 ml_replace(n, (char_u *)line, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001485 changed();
1486#ifdef SYNTAX_HL
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001487 syn_changed(n); // recompute syntax hl. for this line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001488#endif
1489 }
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001490
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001491 // restore curwin/curbuf and a few other things
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001492 aucmd_restbuf(&aco);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001493 // Careful: autocommands may have made "buf" invalid!
Bram Moolenaarf30e74c2006-08-16 17:35:00 +00001494
Bram Moolenaar071d4272004-06-13 20:20:40 +00001495 update_curbuf(NOT_VALID);
1496 }
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001497 else
1498 {
Bram Moolenaar165641d2010-02-17 16:23:09 +01001499 rb_raise(rb_eIndexError, "line number %ld out of range", (long)n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001500 }
1501 return str;
1502}
1503
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001504 static VALUE
1505buffer_aset(VALUE self, VALUE num, VALUE str)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001506{
1507 buf_T *buf = get_buf(self);
1508
1509 if (buf != NULL)
1510 return set_buffer_line(buf, (linenr_T)NUM2LONG(num), str);
1511 return str;
1512}
1513
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001514 static VALUE
1515buffer_delete(VALUE self, VALUE num)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001516{
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001517 buf_T *buf = get_buf(self);
1518 long n = NUM2LONG(num);
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001519 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001520
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001521 if (n > 0 && n <= buf->b_ml.ml_line_count)
1522 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001523 // set curwin/curbuf for "buf" and save some things
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001524 aucmd_prepbuf(&aco, buf);
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001525
Bram Moolenaar758535a2016-03-30 22:06:16 +02001526 if (u_savedel(n, 1) == OK)
1527 {
Bram Moolenaarca70c072020-05-30 20:30:46 +02001528 ml_delete(n);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001529
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001530 // Changes to non-active buffers should properly refresh
1531 // SegPhault - 01/09/05
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001532 deleted_lines_mark(n, 1L);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001533
Bram Moolenaar071d4272004-06-13 20:20:40 +00001534 changed();
1535 }
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001536
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001537 // restore curwin/curbuf and a few other things
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001538 aucmd_restbuf(&aco);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001539 // Careful: autocommands may have made "buf" invalid!
Bram Moolenaarf30e74c2006-08-16 17:35:00 +00001540
Bram Moolenaar071d4272004-06-13 20:20:40 +00001541 update_curbuf(NOT_VALID);
1542 }
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001543 else
1544 {
Bram Moolenaar165641d2010-02-17 16:23:09 +01001545 rb_raise(rb_eIndexError, "line number %ld out of range", n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001546 }
1547 return Qnil;
1548}
1549
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001550 static VALUE
1551buffer_append(VALUE self, VALUE num, VALUE str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001552{
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001553 buf_T *buf = get_buf(self);
Bram Moolenaar165641d2010-02-17 16:23:09 +01001554 char *line = StringValuePtr(str);
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001555 long n = NUM2LONG(num);
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001556 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001557
Bram Moolenaar3c531602010-11-16 14:46:19 +01001558 if (line == NULL)
1559 {
Bram Moolenaar165641d2010-02-17 16:23:09 +01001560 rb_raise(rb_eIndexError, "NULL line");
1561 }
1562 else if (n >= 0 && n <= buf->b_ml.ml_line_count)
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001563 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001564 // set curwin/curbuf for "buf" and save some things
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001565 aucmd_prepbuf(&aco, buf);
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001566
Bram Moolenaar758535a2016-03-30 22:06:16 +02001567 if (u_inssub(n + 1) == OK)
1568 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001569 ml_append(n, (char_u *) line, (colnr_T) 0, FALSE);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001570
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001571 // Changes to non-active buffers should properly refresh screen
1572 // SegPhault - 12/20/04
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001573 appended_lines_mark(n, 1L);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001574
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001575 changed();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001576 }
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001577
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001578 // restore curwin/curbuf and a few other things
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001579 aucmd_restbuf(&aco);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001580 // Careful: autocommands may have made "buf" invalid!
Bram Moolenaarf30e74c2006-08-16 17:35:00 +00001581
Bram Moolenaar071d4272004-06-13 20:20:40 +00001582 update_curbuf(NOT_VALID);
1583 }
Bram Moolenaar3c531602010-11-16 14:46:19 +01001584 else
1585 {
Bram Moolenaar165641d2010-02-17 16:23:09 +01001586 rb_raise(rb_eIndexError, "line number %ld out of range", n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001587 }
1588 return str;
1589}
1590
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001591#ifdef USE_TYPEDDATA
1592static size_t window_dsize(const void *buf);
1593
1594static const rb_data_type_t window_type = {
1595 "vim_window",
Bram Moolenaar41a41412020-01-07 21:32:19 +01001596 {0, 0, window_dsize,
1597# if RUBY_VERSION >= 27
Bram Moolenaar8b430b42020-02-22 15:01:00 +01001598 0, {0}
Bram Moolenaar41a41412020-01-07 21:32:19 +01001599# else
1600 {0, 0}
1601# endif
1602 },
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001603 0, 0,
1604# ifdef RUBY_TYPED_FREE_IMMEDIATELY
1605 0,
1606# endif
1607};
1608
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001609 static size_t
1610window_dsize(const void *win UNUSED)
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001611{
1612 return sizeof(win_T);
1613}
1614#endif
1615
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001616 static VALUE
1617window_new(win_T *win)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001618{
Bram Moolenaare344bea2005-09-01 20:46:49 +00001619 if (win->w_ruby_ref)
1620 {
1621 return (VALUE) win->w_ruby_ref;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001622 }
Bram Moolenaare344bea2005-09-01 20:46:49 +00001623 else
1624 {
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001625#ifdef USE_TYPEDDATA
1626 VALUE obj = TypedData_Wrap_Struct(cVimWindow, &window_type, win);
1627#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001628 VALUE obj = Data_Wrap_Struct(cVimWindow, 0, 0, win);
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001629#endif
Bram Moolenaare344bea2005-09-01 20:46:49 +00001630 win->w_ruby_ref = (void *) obj;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001631 rb_hash_aset(objtbl, rb_obj_id(obj), obj);
1632 return obj;
1633 }
1634}
1635
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001636 static win_T *
1637get_win(VALUE obj)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001638{
1639 win_T *win;
1640
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001641#ifdef USE_TYPEDDATA
1642 TypedData_Get_Struct(obj, win_T, &window_type, win);
1643#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001644 Data_Get_Struct(obj, win_T, win);
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001645#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001646 if (win == NULL)
1647 rb_raise(eDeletedWindowError, "attempt to refer to deleted window");
1648 return win;
1649}
1650
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001651 static VALUE
Bram Moolenaard5a986f2020-12-06 21:11:31 +01001652window_s_current(VALUE self UNUSED)
1653{
1654 return window_new(curwin);
1655}
1656
1657 static VALUE
1658window_s_current_getter(ID id UNUSED, VALUE *x UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001659{
1660 return window_new(curwin);
1661}
1662
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001663/*
1664 * Added line manipulation functions
1665 * SegPhault - 03/07/05
1666 */
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001667 static VALUE
Bram Moolenaard5a986f2020-12-06 21:11:31 +01001668line_s_current(VALUE self UNUSED)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001669{
1670 return get_buffer_line(curbuf, curwin->w_cursor.lnum);
1671}
1672
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001673 static VALUE
1674set_current_line(VALUE self UNUSED, VALUE str)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001675{
1676 return set_buffer_line(curbuf, curwin->w_cursor.lnum, str);
1677}
1678
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001679 static VALUE
Bram Moolenaard5a986f2020-12-06 21:11:31 +01001680current_line_number(VALUE self UNUSED)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001681{
1682 return INT2FIX((int)curwin->w_cursor.lnum);
1683}
1684
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001685 static VALUE
Bram Moolenaard5a986f2020-12-06 21:11:31 +01001686window_s_count(VALUE self UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001687{
Bram Moolenaar071d4272004-06-13 20:20:40 +00001688 win_T *w;
1689 int n = 0;
1690
Bram Moolenaar29323592016-07-24 22:04:11 +02001691 FOR_ALL_WINDOWS(w)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001692 n++;
1693 return INT2NUM(n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001694}
1695
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001696 static VALUE
1697window_s_aref(VALUE self UNUSED, VALUE num)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001698{
1699 win_T *w;
1700 int n = NUM2INT(num);
1701
Bram Moolenaar071d4272004-06-13 20:20:40 +00001702 for (w = firstwin; w != NULL; w = w->w_next, --n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001703 if (n == 0)
1704 return window_new(w);
1705 return Qnil;
1706}
1707
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001708 static VALUE
1709window_buffer(VALUE self)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001710{
1711 win_T *win = get_win(self);
1712
1713 return buffer_new(win->w_buffer);
1714}
1715
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001716 static VALUE
1717window_height(VALUE self)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001718{
1719 win_T *win = get_win(self);
1720
1721 return INT2NUM(win->w_height);
1722}
1723
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001724 static VALUE
1725window_set_height(VALUE self, VALUE height)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001726{
1727 win_T *win = get_win(self);
1728 win_T *savewin = curwin;
1729
1730 curwin = win;
1731 win_setheight(NUM2INT(height));
1732 curwin = savewin;
1733 return height;
1734}
1735
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001736 static VALUE
1737window_width(VALUE self UNUSED)
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001738{
Bram Moolenaare745d752017-09-22 16:56:22 +02001739 return INT2NUM(get_win(self)->w_width);
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001740}
1741
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001742 static VALUE
1743window_set_width(VALUE self UNUSED, VALUE width)
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001744{
1745 win_T *win = get_win(self);
1746 win_T *savewin = curwin;
1747
1748 curwin = win;
1749 win_setwidth(NUM2INT(width));
1750 curwin = savewin;
1751 return width;
1752}
1753
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001754 static VALUE
1755window_cursor(VALUE self)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001756{
1757 win_T *win = get_win(self);
1758
1759 return rb_assoc_new(INT2NUM(win->w_cursor.lnum), INT2NUM(win->w_cursor.col));
1760}
1761
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001762 static VALUE
1763window_set_cursor(VALUE self, VALUE pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001764{
1765 VALUE lnum, col;
1766 win_T *win = get_win(self);
1767
1768 Check_Type(pos, T_ARRAY);
Bram Moolenaar165641d2010-02-17 16:23:09 +01001769 if (RARRAY_LEN(pos) != 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001770 rb_raise(rb_eArgError, "array length must be 2");
Bram Moolenaar165641d2010-02-17 16:23:09 +01001771 lnum = RARRAY_PTR(pos)[0];
1772 col = RARRAY_PTR(pos)[1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001773 win->w_cursor.lnum = NUM2LONG(lnum);
1774 win->w_cursor.col = NUM2UINT(col);
Bram Moolenaar53901442018-07-25 22:02:36 +02001775 win->w_set_curswant = TRUE;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001776 check_cursor(); // put cursor on an existing line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001777 update_screen(NOT_VALID);
1778 return Qnil;
1779}
1780
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001781 static VALUE
1782f_nop(VALUE self UNUSED)
Bram Moolenaar35df7d22012-04-20 18:05:47 +02001783{
1784 return Qnil;
1785}
1786
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001787 static VALUE
1788f_p(int argc, VALUE *argv, VALUE self UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001789{
1790 int i;
1791 VALUE str = rb_str_new("", 0);
Bram Moolenaar51e9fbf2018-08-11 14:24:11 +02001792 VALUE ret = Qnil;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001793
Bram Moolenaar758535a2016-03-30 22:06:16 +02001794 for (i = 0; i < argc; i++)
1795 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001796 if (i > 0) rb_str_cat(str, ", ", 2);
1797 rb_str_concat(str, rb_inspect(argv[i]));
1798 }
Bram Moolenaar32526b32019-01-19 17:43:09 +01001799 msg(RSTRING_PTR(str));
Bram Moolenaar51e9fbf2018-08-11 14:24:11 +02001800
1801 if (argc == 1)
1802 ret = argv[0];
1803 else if (argc > 1)
1804 ret = rb_ary_new4(argc, argv);
1805 return ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001806}
1807
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001808 static void
1809ruby_io_init(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001810{
1811#ifndef DYNAMIC_RUBY
1812 RUBYEXTERN VALUE rb_stdout;
Bram Moolenaarb6c8cd82018-07-24 05:41:30 +02001813 RUBYEXTERN VALUE rb_stderr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001814#endif
1815
1816 rb_stdout = rb_obj_alloc(rb_cObject);
Bram Moolenaarb6c8cd82018-07-24 05:41:30 +02001817 rb_stderr = rb_obj_alloc(rb_cObject);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001818 rb_define_singleton_method(rb_stdout, "write", vim_message, 1);
Bram Moolenaar35df7d22012-04-20 18:05:47 +02001819 rb_define_singleton_method(rb_stdout, "flush", f_nop, 0);
Bram Moolenaarb6c8cd82018-07-24 05:41:30 +02001820 rb_define_singleton_method(rb_stderr, "write", vim_message, 1);
1821 rb_define_singleton_method(rb_stderr, "flush", f_nop, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001822 rb_define_global_function("p", f_p, -1);
1823}
1824
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001825 static void
1826ruby_vim_init(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001827{
1828 objtbl = rb_hash_new();
1829 rb_global_variable(&objtbl);
1830
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001831 // The Vim module used to be called "VIM", but "Vim" is better. Make an
1832 // alias "VIM" for backwards compatibility.
Bram Moolenaarf711faf2007-05-10 16:48:19 +00001833 mVIM = rb_define_module("Vim");
1834 rb_define_const(rb_cObject, "VIM", mVIM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001835 rb_define_const(mVIM, "VERSION_MAJOR", INT2NUM(VIM_VERSION_MAJOR));
1836 rb_define_const(mVIM, "VERSION_MINOR", INT2NUM(VIM_VERSION_MINOR));
1837 rb_define_const(mVIM, "VERSION_BUILD", INT2NUM(VIM_VERSION_BUILD));
1838 rb_define_const(mVIM, "VERSION_PATCHLEVEL", INT2NUM(VIM_VERSION_PATCHLEVEL));
1839 rb_define_const(mVIM, "VERSION_SHORT", rb_str_new2(VIM_VERSION_SHORT));
1840 rb_define_const(mVIM, "VERSION_MEDIUM", rb_str_new2(VIM_VERSION_MEDIUM));
1841 rb_define_const(mVIM, "VERSION_LONG", rb_str_new2(VIM_VERSION_LONG));
1842 rb_define_const(mVIM, "VERSION_LONG_DATE", rb_str_new2(VIM_VERSION_LONG_DATE));
1843 rb_define_module_function(mVIM, "message", vim_message, 1);
1844 rb_define_module_function(mVIM, "set_option", vim_set_option, 1);
1845 rb_define_module_function(mVIM, "command", vim_command, 1);
1846 rb_define_module_function(mVIM, "evaluate", vim_evaluate, 1);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001847 rb_define_module_function(mVIM, "blob", vim_blob, 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001848
1849 eDeletedBufferError = rb_define_class_under(mVIM, "DeletedBufferError",
1850 rb_eStandardError);
1851 eDeletedWindowError = rb_define_class_under(mVIM, "DeletedWindowError",
1852 rb_eStandardError);
1853
1854 cBuffer = rb_define_class_under(mVIM, "Buffer", rb_cObject);
1855 rb_define_singleton_method(cBuffer, "current", buffer_s_current, 0);
1856 rb_define_singleton_method(cBuffer, "count", buffer_s_count, 0);
1857 rb_define_singleton_method(cBuffer, "[]", buffer_s_aref, 1);
1858 rb_define_method(cBuffer, "name", buffer_name, 0);
1859 rb_define_method(cBuffer, "number", buffer_number, 0);
1860 rb_define_method(cBuffer, "count", buffer_count, 0);
1861 rb_define_method(cBuffer, "length", buffer_count, 0);
1862 rb_define_method(cBuffer, "[]", buffer_aref, 1);
1863 rb_define_method(cBuffer, "[]=", buffer_aset, 2);
1864 rb_define_method(cBuffer, "delete", buffer_delete, 1);
1865 rb_define_method(cBuffer, "append", buffer_append, 2);
1866
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001867 // Added line manipulation functions
1868 // SegPhault - 03/07/05
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001869 rb_define_method(cBuffer, "line_number", current_line_number, 0);
1870 rb_define_method(cBuffer, "line", line_s_current, 0);
1871 rb_define_method(cBuffer, "line=", set_current_line, 1);
1872
1873
Bram Moolenaar071d4272004-06-13 20:20:40 +00001874 cVimWindow = rb_define_class_under(mVIM, "Window", rb_cObject);
1875 rb_define_singleton_method(cVimWindow, "current", window_s_current, 0);
1876 rb_define_singleton_method(cVimWindow, "count", window_s_count, 0);
1877 rb_define_singleton_method(cVimWindow, "[]", window_s_aref, 1);
1878 rb_define_method(cVimWindow, "buffer", window_buffer, 0);
1879 rb_define_method(cVimWindow, "height", window_height, 0);
1880 rb_define_method(cVimWindow, "height=", window_set_height, 1);
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001881 rb_define_method(cVimWindow, "width", window_width, 0);
1882 rb_define_method(cVimWindow, "width=", window_set_width, 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001883 rb_define_method(cVimWindow, "cursor", window_cursor, 0);
1884 rb_define_method(cVimWindow, "cursor=", window_set_cursor, 1);
1885
Bram Moolenaard5a986f2020-12-06 21:11:31 +01001886 rb_define_virtual_variable("$curbuf", buffer_s_current_getter, 0);
1887 rb_define_virtual_variable("$curwin", window_s_current_getter, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001888}
Bram Moolenaar99685e62013-05-11 13:56:18 +02001889
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001890 void
1891vim_ruby_init(void *stack_start)
Bram Moolenaar99685e62013-05-11 13:56:18 +02001892{
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001893 // should get machine stack start address early in main function
Bram Moolenaar99685e62013-05-11 13:56:18 +02001894 ruby_stack_start = stack_start;
1895}
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001896
1897 static int
1898convert_hash2dict(VALUE key, VALUE val, VALUE arg)
1899{
1900 dict_T *d = (dict_T *)arg;
1901 dictitem_T *di;
1902
Bram Moolenaardace9f72020-12-28 15:07:45 +01001903 di = dictitem_alloc((char_u *)RSTRING_PTR(rb_obj_as_string(key)));
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001904 if (di == NULL || ruby_convert_to_vim_value(val, &di->di_tv) != OK
1905 || dict_add(d, di) != OK)
1906 {
1907 d->dv_hashtab.ht_error = TRUE;
1908 return ST_STOP;
1909 }
1910 return ST_CONTINUE;
1911}
1912
1913 static int
1914ruby_convert_to_vim_value(VALUE val, typval_T *rettv)
1915{
1916 switch (TYPE(val))
1917 {
1918 case T_NIL:
1919 rettv->v_type = VAR_SPECIAL;
1920 rettv->vval.v_number = VVAL_NULL;
1921 break;
1922 case T_TRUE:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01001923 rettv->v_type = VAR_BOOL;
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001924 rettv->vval.v_number = VVAL_TRUE;
1925 break;
1926 case T_FALSE:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01001927 rettv->v_type = VAR_BOOL;
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001928 rettv->vval.v_number = VVAL_FALSE;
1929 break;
1930 case T_BIGNUM:
1931 case T_FIXNUM:
1932 rettv->v_type = VAR_NUMBER;
1933 rettv->vval.v_number = (varnumber_T)NUM2LONG(val);
1934 break;
1935#ifdef FEAT_FLOAT
1936 case T_FLOAT:
1937 rettv->v_type = VAR_FLOAT;
1938 rettv->vval.v_float = (float_T)NUM2DBL(val);
1939 break;
1940#endif
1941 default:
1942 val = rb_obj_as_string(val);
1943 // FALLTHROUGH
1944 case T_STRING:
1945 {
1946 VALUE str = (VALUE)RSTRING(val);
1947
1948 rettv->v_type = VAR_STRING;
1949 rettv->vval.v_string = vim_strnsave((char_u *)RSTRING_PTR(str),
Bram Moolenaar71ccd032020-06-12 22:59:11 +02001950 RSTRING_LEN(str));
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001951 }
1952 break;
1953 case T_ARRAY:
1954 {
1955 list_T *l;
1956 long i;
1957 typval_T v;
1958
1959 l = list_alloc();
1960 if (l == NULL)
1961 return FAIL;
1962
1963 for (i = 0; i < RARRAY_LEN(val); ++i)
1964 {
1965 if (ruby_convert_to_vim_value((VALUE)RARRAY_PTR(val)[i],
1966 &v) != OK)
1967 {
1968 list_unref(l);
1969 return FAIL;
1970 }
1971 list_append_tv(l, &v);
1972 clear_tv(&v);
1973 }
1974
1975 rettv->v_type = VAR_LIST;
1976 rettv->vval.v_list = l;
1977 ++l->lv_refcount;
1978 }
1979 break;
1980 case T_HASH:
1981 {
1982 dict_T *d;
1983
1984 d = dict_alloc();
1985 if (d == NULL)
1986 return FAIL;
1987
1988 rb_hash_foreach(val, convert_hash2dict, (VALUE)d);
1989 if (d->dv_hashtab.ht_error)
1990 {
1991 dict_unref(d);
1992 return FAIL;
1993 }
1994
1995 rettv->v_type = VAR_DICT;
1996 rettv->vval.v_dict = d;
1997 ++d->dv_refcount;
1998 }
1999 break;
2000 }
2001 return OK;
2002}
2003
2004 void
2005do_rubyeval(char_u *str, typval_T *rettv)
2006{
2007 int retval = FAIL;
2008
2009 if (ensure_ruby_initialized())
2010 {
2011 int state;
2012 VALUE obj;
2013
2014 obj = rb_eval_string_protect((const char *)str, &state);
2015 if (state)
2016 error_print(state);
2017 else
2018 retval = ruby_convert_to_vim_value(obj, rettv);
2019 }
2020 if (retval == FAIL)
2021 {
2022 rettv->v_type = VAR_NUMBER;
2023 rettv->vval.v_number = 0;
2024 }
2025}