blob: d3ed78128114e18b006786a928b555a3fe7ebefe [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
23# if !defined(DYNAMIC_RUBY_VER) || (DYNAMIC_RUBY_VER < 18)
24# define NT
25# endif
26# ifndef DYNAMIC_RUBY
27# define IMPORT /* For static dll usage __declspec(dllimport) */
28# define RUBYEXTERN __declspec(dllimport)
29# endif
30#endif
31#ifndef RUBYEXTERN
32# define RUBYEXTERN extern
33#endif
34
Bram Moolenaar2016ae52016-06-13 20:08:43 +020035#if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 24
36# define USE_RUBY_INTEGER
Bram Moolenaar06469e92016-06-11 22:26:53 +020037#endif
38
Bram Moolenaarf9b5ef82010-09-29 13:02:53 +020039#ifdef DYNAMIC_RUBY
Bram Moolenaar071d4272004-06-13 20:20:40 +000040/*
41 * This is tricky. In ruby.h there is (inline) function rb_class_of()
42 * definition. This function use these variables. But we want function to
43 * use dll_* variables.
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 Moolenaardb3fbe52013-03-07 15:16:21 +010050# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 20
51# 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)
57# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 18
58/*
Bram Moolenaar42d57f02010-03-10 12:47:00 +010059 * On ver 1.8, all Ruby functions are exported with "__declspec(dllimport)"
60 * in ruby.h. But it causes trouble for these variables, because it is
Bram Moolenaar071d4272004-06-13 20:20:40 +000061 * defined in this file. When defined this RUBY_EXPORT it modified to
62 * "extern" and be able to avoid this problem.
63 */
64# define RUBY_EXPORT
65# endif
Bram Moolenaarf9b5ef82010-09-29 13:02:53 +020066
Bram Moolenaar7dca2eb2019-02-18 20:42:50 +010067#endif // ifdef DYNAMIC_RUBY
Bram Moolenaar071d4272004-06-13 20:20:40 +000068
Bram Moolenaar7dca2eb2019-02-18 20:42:50 +010069// suggested by Ariya Mizutani
Bram Moolenaar0b69c732010-02-17 15:11:50 +010070#if (_MSC_VER == 1200)
71# undef _WIN32_WINNT
72#endif
73
Bram Moolenaar639a2552010-03-17 18:15:23 +010074#if (defined(RUBY_VERSION) && RUBY_VERSION >= 19) \
75 || (defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 19)
76# define RUBY19_OR_LATER 1
77#endif
78
Bram Moolenaar0d27f642015-12-28 22:05:28 +010079#if (defined(RUBY_VERSION) && RUBY_VERSION >= 20) \
80 || (defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 20)
81# define RUBY20_OR_LATER 1
82#endif
83
Bram Moolenaarf711cb22018-08-01 18:42:13 +020084#if (defined(RUBY_VERSION) && RUBY_VERSION >= 21) \
85 || (defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 21)
86# define RUBY21_OR_LATER 1
87#endif
88
Bram Moolenaar42d57f02010-03-10 12:47:00 +010089#if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 19
90/* Ruby 1.9 defines a number of static functions which use rb_num2long and
91 * rb_int2big */
92# define rb_num2long rb_num2long_stub
93# define rb_int2big rb_int2big_stub
94#endif
95
Bram Moolenaar498af702014-03-28 21:58:21 +010096#if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 19 \
97 && VIM_SIZEOF_INT < VIM_SIZEOF_LONG
98/* Ruby 1.9 defines a number of static functions which use rb_fix2int and
Bram Moolenaara2aa31a2014-02-23 22:52:40 +010099 * rb_num2int if VIM_SIZEOF_INT < VIM_SIZEOF_LONG (64bit) */
Bram Moolenaar0bcdd6e2013-04-14 16:19:03 +0200100# define rb_fix2int rb_fix2int_stub
101# define rb_num2int rb_num2int_stub
102#endif
103
Bram Moolenaar0c7485f2015-01-14 14:04:10 +0100104#if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER == 21
Bram Moolenaara1a118b2014-02-05 22:41:15 +0100105/* Ruby 2.1 adds new GC called RGenGC and RARRAY_PTR uses
106 * rb_gc_writebarrier_unprotect_promoted if USE_RGENGC */
Bram Moolenaar90140742014-11-27 17:44:08 +0100107# define rb_gc_writebarrier_unprotect_promoted rb_gc_writebarrier_unprotect_promoted_stub
108#endif
Bram Moolenaar0c7485f2015-01-14 14:04:10 +0100109#if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 22
110# define rb_gc_writebarrier_unprotect rb_gc_writebarrier_unprotect_stub
Bram Moolenaar0c7485f2015-01-14 14:04:10 +0100111#endif
Bram Moolenaara1a118b2014-02-05 22:41:15 +0100112
Bram Moolenaarb09c6842018-12-27 22:11:01 +0100113#if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 26
Bram Moolenaarf62fc312019-01-08 20:29:32 +0100114# define rb_ary_detransient rb_ary_detransient_stub
Bram Moolenaarb09c6842018-12-27 22:11:01 +0100115#endif
116
Bram Moolenaar071d4272004-06-13 20:20:40 +0000117#include <ruby.h>
Bram Moolenaar639a2552010-03-17 18:15:23 +0100118#ifdef RUBY19_OR_LATER
Bram Moolenaar165641d2010-02-17 16:23:09 +0100119# include <ruby/encoding.h>
120#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000121
Bram Moolenaar7a8ef142010-12-24 13:39:35 +0100122#undef off_t /* ruby defines off_t as _int64, Mingw uses long */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000123#undef EXTERN
124#undef _
125
126/* T_DATA defined both by Ruby and Mac header files, hack around it... */
Bram Moolenaard0573012017-10-28 21:11:06 +0200127#if defined(MACOS_X)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000128# define __OPENTRANSPORT__
129# define __OPENTRANSPORTPROTOCOL__
130# define __OPENTRANSPORTPROVIDERS__
131#endif
132
Bram Moolenaar165641d2010-02-17 16:23:09 +0100133/*
Bram Moolenaar0d27f642015-12-28 22:05:28 +0100134 * The TypedData_XXX macro family can be used since Ruby 1.9.2 but
135 * rb_data_type_t changed in 1.9.3, therefore require at least 2.0.
136 * The old Data_XXX macro family was deprecated on Ruby 2.2.
Bram Moolenaarf2f6d292015-12-28 20:57:10 +0100137 * Use TypedData_XXX if available.
138 */
Bram Moolenaar0d27f642015-12-28 22:05:28 +0100139#if defined(TypedData_Wrap_Struct) && defined(RUBY20_OR_LATER)
Bram Moolenaarf2f6d292015-12-28 20:57:10 +0100140# define USE_TYPEDDATA 1
141#endif
142
143/*
Bram Moolenaar84a05ac2013-05-06 04:24:17 +0200144 * Backward compatibility for Ruby 1.8 and earlier.
Bram Moolenaar165641d2010-02-17 16:23:09 +0100145 * Ruby 1.9 does not provide STR2CSTR, instead StringValuePtr is provided.
146 * Ruby 1.9 does not provide RXXX(s)->len and RXXX(s)->ptr, instead
147 * RXXX_LEN(s) and RXXX_PTR(s) are provided.
148 */
149#ifndef StringValuePtr
150# define StringValuePtr(s) STR2CSTR(s)
151#endif
152#ifndef RARRAY_LEN
153# define RARRAY_LEN(s) RARRAY(s)->len
154#endif
155#ifndef RARRAY_PTR
156# define RARRAY_PTR(s) RARRAY(s)->ptr
157#endif
158#ifndef RSTRING_LEN
159# define RSTRING_LEN(s) RSTRING(s)->len
160#endif
161#ifndef RSTRING_PTR
162# define RSTRING_PTR(s) RSTRING(s)->ptr
163#endif
164
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100165#ifdef HAVE_DUP
166# undef HAVE_DUP
167#endif
168
Bram Moolenaar071d4272004-06-13 20:20:40 +0000169#include "vim.h"
170#include "version.h"
171
Bram Moolenaar7dca2eb2019-02-18 20:42:50 +0100172#ifdef DYNAMIC_RUBY
173# if !defined(MSWIN) // must come after including vim.h, where it is defined
174# include <dlfcn.h>
175# define HINSTANCE void*
176# define RUBY_PROC void*
177# define load_dll(n) dlopen((n), RTLD_LAZY|RTLD_GLOBAL)
178# define symbol_from_dll dlsym
179# define close_dll dlclose
180# else
181# define RUBY_PROC FARPROC
182# define load_dll vimLoadLib
183# define symbol_from_dll GetProcAddress
184# define close_dll FreeLibrary
185# endif
186#endif
187
Bram Moolenaar071d4272004-06-13 20:20:40 +0000188#if defined(PROTO) && !defined(FEAT_RUBY)
189/* Define these to be able to generate the function prototypes. */
190# define VALUE int
191# define RUBY_DATA_FUNC int
192#endif
193
194static int ruby_initialized = 0;
Bram Moolenaar99685e62013-05-11 13:56:18 +0200195static void *ruby_stack_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000196static VALUE objtbl;
197
198static VALUE mVIM;
199static VALUE cBuffer;
200static VALUE cVimWindow;
201static VALUE eDeletedBufferError;
202static VALUE eDeletedWindowError;
203
204static int ensure_ruby_initialized(void);
205static void error_print(int);
206static void ruby_io_init(void);
207static void ruby_vim_init(void);
208
Bram Moolenaar10f3a792013-05-20 12:52:29 +0200209#if defined(RUBY19_OR_LATER) || defined(RUBY_INIT_STACK)
210# if defined(__ia64) && !defined(ruby_init_stack)
211# define ruby_init_stack(addr) ruby_init_stack((addr), rb_ia64_bsp())
212# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000213#endif
214
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200215#if defined(DYNAMIC_RUBY) || defined(PROTO)
Bram Moolenaaref269542016-01-19 13:22:12 +0100216# if defined(PROTO) && !defined(HINSTANCE)
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200217# define HINSTANCE int /* for generating prototypes */
218# endif
219
Bram Moolenaar071d4272004-06-13 20:20:40 +0000220/*
221 * Wrapper defines
222 */
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200223# define rb_assoc_new dll_rb_assoc_new
224# define rb_cObject (*dll_rb_cObject)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100225# define rb_class_new_instance dll_rb_class_new_instance
Bram Moolenaarf2f6d292015-12-28 20:57:10 +0100226# define rb_check_type dll_rb_check_type
227# ifdef USE_TYPEDDATA
228# define rb_check_typeddata dll_rb_check_typeddata
Bram Moolenaar0c7485f2015-01-14 14:04:10 +0100229# endif
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200230# define rb_class_path dll_rb_class_path
Bram Moolenaarf2f6d292015-12-28 20:57:10 +0100231# ifdef USE_TYPEDDATA
232# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 23
233# define rb_data_typed_object_wrap dll_rb_data_typed_object_wrap
234# else
235# define rb_data_typed_object_alloc dll_rb_data_typed_object_alloc
236# endif
237# else
238# define rb_data_object_alloc dll_rb_data_object_alloc
239# endif
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200240# define rb_define_class_under dll_rb_define_class_under
241# define rb_define_const dll_rb_define_const
242# define rb_define_global_function dll_rb_define_global_function
243# define rb_define_method dll_rb_define_method
244# define rb_define_module dll_rb_define_module
245# define rb_define_module_function dll_rb_define_module_function
246# define rb_define_singleton_method dll_rb_define_singleton_method
247# define rb_define_virtual_variable dll_rb_define_virtual_variable
248# define rb_stdout (*dll_rb_stdout)
Bram Moolenaarb6c8cd82018-07-24 05:41:30 +0200249# define rb_stderr (*dll_rb_stderr)
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200250# define rb_eArgError (*dll_rb_eArgError)
251# define rb_eIndexError (*dll_rb_eIndexError)
252# define rb_eRuntimeError (*dll_rb_eRuntimeError)
253# define rb_eStandardError (*dll_rb_eStandardError)
254# define rb_eval_string_protect dll_rb_eval_string_protect
Bram Moolenaarf711cb22018-08-01 18:42:13 +0200255# ifdef RUBY21_OR_LATER
256# define rb_funcallv dll_rb_funcallv
257# else
258# define rb_funcall2 dll_rb_funcall2
259# endif
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200260# define rb_global_variable dll_rb_global_variable
261# define rb_hash_aset dll_rb_hash_aset
262# define rb_hash_new dll_rb_hash_new
263# define rb_inspect dll_rb_inspect
264# define rb_int2inum dll_rb_int2inum
Bram Moolenaar218beb32018-08-04 17:24:44 +0200265
266// ruby.h may redefine rb_intern to use RUBY_CONST_ID_CACHE(), but that won't
267// work. Not using the cache appears to be the best solution.
268# undef rb_intern
269# define rb_intern dll_rb_intern
270
Bram Moolenaara2aa31a2014-02-23 22:52:40 +0100271# if VIM_SIZEOF_INT < VIM_SIZEOF_LONG /* 64 bits only */
Bram Moolenaar498af702014-03-28 21:58:21 +0100272# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER <= 18
273# define rb_fix2int dll_rb_fix2int
274# define rb_num2int dll_rb_num2int
275# endif
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200276# define rb_num2uint dll_rb_num2uint
277# endif
278# define rb_lastline_get dll_rb_lastline_get
279# define rb_lastline_set dll_rb_lastline_set
Bram Moolenaard0573012017-10-28 21:11:06 +0200280# define rb_protect dll_rb_protect
281# define rb_load dll_rb_load
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200282# ifndef RUBY19_OR_LATER
283# define rb_num2long dll_rb_num2long
284# endif
285# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER <= 19
286# define rb_num2ulong dll_rb_num2ulong
287# endif
288# define rb_obj_alloc dll_rb_obj_alloc
289# define rb_obj_as_string dll_rb_obj_as_string
290# define rb_obj_id dll_rb_obj_id
291# define rb_raise dll_rb_raise
292# define rb_str_cat dll_rb_str_cat
293# define rb_str_concat dll_rb_str_concat
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100294# undef rb_str_new
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200295# define rb_str_new dll_rb_str_new
296# ifdef rb_str_new2
Bram Moolenaar1d2beae2010-05-22 21:56:55 +0200297/* Ruby may #define rb_str_new2 to use rb_str_new_cstr. */
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200298# define need_rb_str_new_cstr 1
Bram Moolenaar3ca71f12010-10-27 16:49:47 +0200299/* Ruby's headers #define rb_str_new_cstr to make use of GCC's
300 * __builtin_constant_p extension. */
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200301# undef rb_str_new_cstr
302# define rb_str_new_cstr dll_rb_str_new_cstr
Bram Moolenaar76a86062013-05-11 17:45:48 +0200303# else
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200304# define rb_str_new2 dll_rb_str_new2
Bram Moolenaar76a86062013-05-11 17:45:48 +0200305# endif
Bram Moolenaar3ca71f12010-10-27 16:49:47 +0200306# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 18
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200307# define rb_string_value dll_rb_string_value
308# define rb_string_value_ptr dll_rb_string_value_ptr
309# define rb_float_new dll_rb_float_new
310# define rb_ary_new dll_rb_ary_new
Bram Moolenaar51e9fbf2018-08-11 14:24:11 +0200311# ifdef rb_ary_new4
312# define RB_ARY_NEW4_MACRO 1
313# undef rb_ary_new4
314# endif
315# define rb_ary_new4 dll_rb_ary_new4
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200316# define rb_ary_push dll_rb_ary_push
Bram Moolenaar10f3a792013-05-20 12:52:29 +0200317# if defined(RUBY19_OR_LATER) || defined(RUBY_INIT_STACK)
318# ifdef __ia64
319# define rb_ia64_bsp dll_rb_ia64_bsp
320# undef ruby_init_stack
321# define ruby_init_stack(addr) dll_ruby_init_stack((addr), rb_ia64_bsp())
322# else
323# define ruby_init_stack dll_ruby_init_stack
324# endif
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200325# endif
326# else
327# define rb_str2cstr dll_rb_str2cstr
Bram Moolenaar3ca71f12010-10-27 16:49:47 +0200328# endif
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200329# ifdef RUBY19_OR_LATER
330# define rb_errinfo dll_rb_errinfo
331# else
332# define ruby_errinfo (*dll_ruby_errinfo)
333# endif
334# define ruby_init dll_ruby_init
335# define ruby_init_loadpath dll_ruby_init_loadpath
Bram Moolenaar4f974752019-02-17 17:44:42 +0100336# ifdef MSWIN
Bram Moolenaar9d5c84a2018-12-19 20:48:46 +0100337# ifdef RUBY19_OR_LATER
338# define ruby_sysinit dll_ruby_sysinit
339# else
340# define NtInitialize dll_NtInitialize
341# endif
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200342# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 18
343# define rb_w32_snprintf dll_rb_w32_snprintf
344# endif
345# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000346
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200347# ifdef RUBY19_OR_LATER
348# define ruby_script dll_ruby_script
349# define rb_enc_find_index dll_rb_enc_find_index
350# define rb_enc_find dll_rb_enc_find
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100351# undef rb_enc_str_new
352# define rb_enc_str_new dll_rb_enc_str_new
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200353# define rb_sprintf dll_rb_sprintf
354# define rb_require dll_rb_require
Bram Moolenaar9b1067e2015-11-19 19:33:15 +0100355# define ruby_options dll_ruby_options
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200356# endif
Bram Moolenaar165641d2010-02-17 16:23:09 +0100357
Bram Moolenaar071d4272004-06-13 20:20:40 +0000358/*
359 * Pointers for dynamic link
360 */
361static VALUE (*dll_rb_assoc_new) (VALUE, VALUE);
Bram Moolenaarba52cde2010-06-25 04:29:11 +0200362VALUE *dll_rb_cFalseClass;
363VALUE *dll_rb_cFixnum;
Bram Moolenaar2016ae52016-06-13 20:08:43 +0200364# if defined(USE_RUBY_INTEGER)
Bram Moolenaar06469e92016-06-11 22:26:53 +0200365VALUE *dll_rb_cInteger;
366# endif
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200367# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 20
Bram Moolenaardb3fbe52013-03-07 15:16:21 +0100368VALUE *dll_rb_cFloat;
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200369# endif
Bram Moolenaarba52cde2010-06-25 04:29:11 +0200370VALUE *dll_rb_cNilClass;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000371static VALUE *dll_rb_cObject;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100372VALUE *dll_rb_cString;
Bram Moolenaarba52cde2010-06-25 04:29:11 +0200373VALUE *dll_rb_cSymbol;
374VALUE *dll_rb_cTrueClass;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100375static VALUE (*dll_rb_class_new_instance) (int,VALUE*,VALUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000376static void (*dll_rb_check_type) (VALUE,int);
Bram Moolenaarf2f6d292015-12-28 20:57:10 +0100377# ifdef USE_TYPEDDATA
378static void *(*dll_rb_check_typeddata) (VALUE,const rb_data_type_t *);
379# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000380static VALUE (*dll_rb_class_path) (VALUE);
Bram Moolenaarf2f6d292015-12-28 20:57:10 +0100381# ifdef USE_TYPEDDATA
382# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 23
383static VALUE (*dll_rb_data_typed_object_wrap) (VALUE, void*, const rb_data_type_t *);
384# else
385static VALUE (*dll_rb_data_typed_object_alloc) (VALUE, void*, const rb_data_type_t *);
386# endif
387# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000388static VALUE (*dll_rb_data_object_alloc) (VALUE, void*, RUBY_DATA_FUNC, RUBY_DATA_FUNC);
Bram Moolenaarf2f6d292015-12-28 20:57:10 +0100389# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000390static VALUE (*dll_rb_define_class_under) (VALUE, const char*, VALUE);
391static void (*dll_rb_define_const) (VALUE,const char*,VALUE);
392static void (*dll_rb_define_global_function) (const char*,VALUE(*)(),int);
393static void (*dll_rb_define_method) (VALUE,const char*,VALUE(*)(),int);
394static VALUE (*dll_rb_define_module) (const char*);
395static void (*dll_rb_define_module_function) (VALUE,const char*,VALUE(*)(),int);
396static void (*dll_rb_define_singleton_method) (VALUE,const char*,VALUE(*)(),int);
397static void (*dll_rb_define_virtual_variable) (const char*,VALUE(*)(),void(*)());
398static VALUE *dll_rb_stdout;
Bram Moolenaarb6c8cd82018-07-24 05:41:30 +0200399static VALUE *dll_rb_stderr;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000400static VALUE *dll_rb_eArgError;
401static VALUE *dll_rb_eIndexError;
402static VALUE *dll_rb_eRuntimeError;
403static VALUE *dll_rb_eStandardError;
404static VALUE (*dll_rb_eval_string_protect) (const char*, int*);
Bram Moolenaarf711cb22018-08-01 18:42:13 +0200405# ifdef RUBY21_OR_LATER
406static VALUE (*dll_rb_funcallv) (VALUE, ID, int, const VALUE*);
407# else
408static VALUE (*dll_rb_funcall2) (VALUE, ID, int, const VALUE*);
409# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000410static void (*dll_rb_global_variable) (VALUE*);
411static VALUE (*dll_rb_hash_aset) (VALUE, VALUE, VALUE);
412static VALUE (*dll_rb_hash_new) (void);
413static VALUE (*dll_rb_inspect) (VALUE);
414static VALUE (*dll_rb_int2inum) (long);
Bram Moolenaarf711cb22018-08-01 18:42:13 +0200415static ID (*dll_rb_intern) (const char*);
Bram Moolenaara2aa31a2014-02-23 22:52:40 +0100416# if VIM_SIZEOF_INT < VIM_SIZEOF_LONG /* 64 bits only */
Bram Moolenaar2623b4f2012-09-18 16:36:32 +0200417static long (*dll_rb_fix2int) (VALUE);
418static long (*dll_rb_num2int) (VALUE);
419static unsigned long (*dll_rb_num2uint) (VALUE);
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200420# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000421static VALUE (*dll_rb_lastline_get) (void);
422static void (*dll_rb_lastline_set) (VALUE);
Bram Moolenaar37badc82018-01-31 20:15:30 +0100423static VALUE (*dll_rb_protect) (VALUE (*)(VALUE), VALUE, int*);
Bram Moolenaard0573012017-10-28 21:11:06 +0200424static void (*dll_rb_load) (VALUE, int);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000425static long (*dll_rb_num2long) (VALUE);
426static unsigned long (*dll_rb_num2ulong) (VALUE);
427static VALUE (*dll_rb_obj_alloc) (VALUE);
428static VALUE (*dll_rb_obj_as_string) (VALUE);
429static VALUE (*dll_rb_obj_id) (VALUE);
430static void (*dll_rb_raise) (VALUE, const char*, ...);
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200431# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 18
Bram Moolenaarf9b5ef82010-09-29 13:02:53 +0200432static VALUE (*dll_rb_string_value) (volatile VALUE*);
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200433# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000434static char *(*dll_rb_str2cstr) (VALUE,int*);
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200435# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000436static VALUE (*dll_rb_str_cat) (VALUE, const char*, long);
437static VALUE (*dll_rb_str_concat) (VALUE, VALUE);
438static VALUE (*dll_rb_str_new) (const char*, long);
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200439# ifdef need_rb_str_new_cstr
Bram Moolenaar1d2beae2010-05-22 21:56:55 +0200440/* Ruby may #define rb_str_new2 to use rb_str_new_cstr. */
441static VALUE (*dll_rb_str_new_cstr) (const char*);
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200442# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000443static VALUE (*dll_rb_str_new2) (const char*);
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200444# endif
445# ifdef RUBY19_OR_LATER
Bram Moolenaar165641d2010-02-17 16:23:09 +0100446static VALUE (*dll_rb_errinfo) (void);
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200447# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000448static VALUE *dll_ruby_errinfo;
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200449# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000450static void (*dll_ruby_init) (void);
451static void (*dll_ruby_init_loadpath) (void);
Bram Moolenaar4f974752019-02-17 17:44:42 +0100452# ifdef MSWIN
Bram Moolenaar9d5c84a2018-12-19 20:48:46 +0100453# ifdef RUBY19_OR_LATER
Bram Moolenaar4f391792017-01-15 16:59:07 +0100454static void (*dll_ruby_sysinit) (int*, char***);
Bram Moolenaar9d5c84a2018-12-19 20:48:46 +0100455# else
456static void (*dll_NtInitialize) (int*, char***);
457# endif
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200458# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 18
Bram Moolenaar3ca71f12010-10-27 16:49:47 +0200459static int (*dll_rb_w32_snprintf)(char*, size_t, const char*, ...);
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200460# endif
Bram Moolenaar3ca71f12010-10-27 16:49:47 +0200461# endif
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200462# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 18
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100463static char * (*dll_rb_string_value_ptr) (volatile VALUE*);
464static VALUE (*dll_rb_float_new) (double);
465static VALUE (*dll_rb_ary_new) (void);
Bram Moolenaar51e9fbf2018-08-11 14:24:11 +0200466static VALUE (*dll_rb_ary_new4) (long n, const VALUE *elts);
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100467static VALUE (*dll_rb_ary_push) (VALUE, VALUE);
Bram Moolenaarb09c6842018-12-27 22:11:01 +0100468# if DYNAMIC_RUBY_VER >= 26
469static void (*dll_rb_ary_detransient) (VALUE);
470# endif
Bram Moolenaar10f3a792013-05-20 12:52:29 +0200471# if defined(RUBY19_OR_LATER) || defined(RUBY_INIT_STACK)
472# ifdef __ia64
Bram Moolenaar76a86062013-05-11 17:45:48 +0200473static void * (*dll_rb_ia64_bsp) (void);
474static void (*dll_ruby_init_stack)(VALUE*, void*);
Bram Moolenaar10f3a792013-05-20 12:52:29 +0200475# else
Bram Moolenaar76a86062013-05-11 17:45:48 +0200476static void (*dll_ruby_init_stack)(VALUE*);
Bram Moolenaar10f3a792013-05-20 12:52:29 +0200477# endif
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200478# endif
Bram Moolenaar76a86062013-05-11 17:45:48 +0200479# endif
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200480# ifdef RUBY19_OR_LATER
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100481static VALUE (*dll_rb_int2big)(SIGNED_VALUE);
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200482# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000483
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200484# ifdef RUBY19_OR_LATER
Bram Moolenaar165641d2010-02-17 16:23:09 +0100485static void (*dll_ruby_script) (const char*);
486static int (*dll_rb_enc_find_index) (const char*);
487static rb_encoding* (*dll_rb_enc_find) (const char*);
488static VALUE (*dll_rb_enc_str_new) (const char*, long, rb_encoding*);
489static VALUE (*dll_rb_sprintf) (const char*, ...);
Bram Moolenaar7a8ef142010-12-24 13:39:35 +0100490static VALUE (*dll_rb_require) (const char*);
Bram Moolenaar9b1067e2015-11-19 19:33:15 +0100491static void* (*ruby_options)(int, char**);
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200492# endif
Bram Moolenaar165641d2010-02-17 16:23:09 +0100493
Bram Moolenaara1a118b2014-02-05 22:41:15 +0100494# if defined(USE_RGENGC) && USE_RGENGC
Bram Moolenaar0c7485f2015-01-14 14:04:10 +0100495# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER == 21
Bram Moolenaara1a118b2014-02-05 22:41:15 +0100496static void (*dll_rb_gc_writebarrier_unprotect_promoted)(VALUE);
Bram Moolenaar0c7485f2015-01-14 14:04:10 +0100497# else
498static void (*dll_rb_gc_writebarrier_unprotect)(VALUE obj);
499# endif
Bram Moolenaara1a118b2014-02-05 22:41:15 +0100500# endif
501
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200502# if defined(RUBY19_OR_LATER) && !defined(PROTO)
Bram Moolenaarbbc1a592015-04-21 15:25:31 +0200503# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 22
504long rb_num2long_stub(VALUE x)
505# else
Bram Moolenaarff8cf2b2012-11-24 13:39:00 +0100506SIGNED_VALUE rb_num2long_stub(VALUE x)
Bram Moolenaarbbc1a592015-04-21 15:25:31 +0200507# endif
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100508{
509 return dll_rb_num2long(x);
510}
Bram Moolenaar63d1fea2019-02-03 15:18:35 +0100511# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 26
512VALUE rb_int2big_stub(intptr_t x)
513# else
Bram Moolenaarff8cf2b2012-11-24 13:39:00 +0100514VALUE rb_int2big_stub(SIGNED_VALUE x)
Bram Moolenaar63d1fea2019-02-03 15:18:35 +0100515# endif
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100516{
517 return dll_rb_int2big(x);
518}
Bram Moolenaar498af702014-03-28 21:58:21 +0100519# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 19 \
520 && VIM_SIZEOF_INT < VIM_SIZEOF_LONG
Bram Moolenaar0bcdd6e2013-04-14 16:19:03 +0200521long rb_fix2int_stub(VALUE x)
522{
523 return dll_rb_fix2int(x);
524}
525long rb_num2int_stub(VALUE x)
526{
527 return dll_rb_num2int(x);
528}
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200529# endif
530# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 20
Bram Moolenaar886ed692013-02-26 13:41:35 +0100531VALUE
532rb_float_new_in_heap(double d)
533{
534 return dll_rb_float_new(d);
535}
Bram Moolenaarbbc1a592015-04-21 15:25:31 +0200536# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 22
537unsigned long rb_num2ulong(VALUE x)
538# else
Bram Moolenaardb3fbe52013-03-07 15:16:21 +0100539VALUE rb_num2ulong(VALUE x)
Bram Moolenaarbbc1a592015-04-21 15:25:31 +0200540# endif
Bram Moolenaar886ed692013-02-26 13:41:35 +0100541{
542 return (long)RSHIFT((SIGNED_VALUE)(x),1);
543}
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200544# endif
545# endif
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100546
Bram Moolenaar3e9a1612014-11-12 16:05:04 +0100547 /* Do not generate a prototype here, VALUE isn't always defined. */
548# if defined(USE_RGENGC) && USE_RGENGC && !defined(PROTO)
Bram Moolenaar0c7485f2015-01-14 14:04:10 +0100549# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER == 21
Bram Moolenaara1a118b2014-02-05 22:41:15 +0100550void rb_gc_writebarrier_unprotect_promoted_stub(VALUE obj)
551{
Bram Moolenaar90140742014-11-27 17:44:08 +0100552 dll_rb_gc_writebarrier_unprotect_promoted(obj);
Bram Moolenaara1a118b2014-02-05 22:41:15 +0100553}
Bram Moolenaar0c7485f2015-01-14 14:04:10 +0100554# else
555void rb_gc_writebarrier_unprotect_stub(VALUE obj)
556{
557 dll_rb_gc_writebarrier_unprotect(obj);
558}
559# endif
560# endif
561
Bram Moolenaarf62fc312019-01-08 20:29:32 +0100562# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 26
563void rb_ary_detransient_stub(VALUE x)
564{
565 dll_rb_ary_detransient(x);
566}
567# endif
568
Bram Moolenaar3ca71f12010-10-27 16:49:47 +0200569static HINSTANCE hinstRuby = NULL; /* Instance of ruby.dll */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000570
571/*
Bram Moolenaarda2303d2005-08-30 21:55:26 +0000572 * Table of name to function pointer of ruby.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000573 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000574static struct
575{
576 char *name;
577 RUBY_PROC *ptr;
578} ruby_funcname_table[] =
579{
580 {"rb_assoc_new", (RUBY_PROC*)&dll_rb_assoc_new},
581 {"rb_cFalseClass", (RUBY_PROC*)&dll_rb_cFalseClass},
Bram Moolenaar2016ae52016-06-13 20:08:43 +0200582# if defined(USE_RUBY_INTEGER)
Bram Moolenaar06469e92016-06-11 22:26:53 +0200583 {"rb_cInteger", (RUBY_PROC*)&dll_rb_cInteger},
Bram Moolenaar6abda992017-01-09 21:10:31 +0100584# else
585 {"rb_cFixnum", (RUBY_PROC*)&dll_rb_cFixnum},
Bram Moolenaar06469e92016-06-11 22:26:53 +0200586# endif
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200587# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 20
Bram Moolenaardb3fbe52013-03-07 15:16:21 +0100588 {"rb_cFloat", (RUBY_PROC*)&dll_rb_cFloat},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200589# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000590 {"rb_cNilClass", (RUBY_PROC*)&dll_rb_cNilClass},
591 {"rb_cObject", (RUBY_PROC*)&dll_rb_cObject},
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100592 {"rb_cString", (RUBY_PROC*)&dll_rb_cString},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000593 {"rb_cSymbol", (RUBY_PROC*)&dll_rb_cSymbol},
594 {"rb_cTrueClass", (RUBY_PROC*)&dll_rb_cTrueClass},
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100595 {"rb_class_new_instance", (RUBY_PROC*)&dll_rb_class_new_instance},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000596 {"rb_check_type", (RUBY_PROC*)&dll_rb_check_type},
Bram Moolenaarf2f6d292015-12-28 20:57:10 +0100597# ifdef USE_TYPEDDATA
598 {"rb_check_typeddata", (RUBY_PROC*)&dll_rb_check_typeddata},
599# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000600 {"rb_class_path", (RUBY_PROC*)&dll_rb_class_path},
Bram Moolenaarf2f6d292015-12-28 20:57:10 +0100601# ifdef USE_TYPEDDATA
602# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 23
603 {"rb_data_typed_object_wrap", (RUBY_PROC*)&dll_rb_data_typed_object_wrap},
604# else
605 {"rb_data_typed_object_alloc", (RUBY_PROC*)&dll_rb_data_typed_object_alloc},
606# endif
607# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000608 {"rb_data_object_alloc", (RUBY_PROC*)&dll_rb_data_object_alloc},
Bram Moolenaarf2f6d292015-12-28 20:57:10 +0100609# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000610 {"rb_define_class_under", (RUBY_PROC*)&dll_rb_define_class_under},
611 {"rb_define_const", (RUBY_PROC*)&dll_rb_define_const},
612 {"rb_define_global_function", (RUBY_PROC*)&dll_rb_define_global_function},
613 {"rb_define_method", (RUBY_PROC*)&dll_rb_define_method},
614 {"rb_define_module", (RUBY_PROC*)&dll_rb_define_module},
615 {"rb_define_module_function", (RUBY_PROC*)&dll_rb_define_module_function},
616 {"rb_define_singleton_method", (RUBY_PROC*)&dll_rb_define_singleton_method},
617 {"rb_define_virtual_variable", (RUBY_PROC*)&dll_rb_define_virtual_variable},
618 {"rb_stdout", (RUBY_PROC*)&dll_rb_stdout},
Bram Moolenaarb6c8cd82018-07-24 05:41:30 +0200619 {"rb_stderr", (RUBY_PROC*)&dll_rb_stderr},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000620 {"rb_eArgError", (RUBY_PROC*)&dll_rb_eArgError},
621 {"rb_eIndexError", (RUBY_PROC*)&dll_rb_eIndexError},
622 {"rb_eRuntimeError", (RUBY_PROC*)&dll_rb_eRuntimeError},
623 {"rb_eStandardError", (RUBY_PROC*)&dll_rb_eStandardError},
624 {"rb_eval_string_protect", (RUBY_PROC*)&dll_rb_eval_string_protect},
Bram Moolenaarf711cb22018-08-01 18:42:13 +0200625# ifdef RUBY21_OR_LATER
626 {"rb_funcallv", (RUBY_PROC*)&dll_rb_funcallv},
627# else
628 {"rb_funcall2", (RUBY_PROC*)&dll_rb_funcall2},
629# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000630 {"rb_global_variable", (RUBY_PROC*)&dll_rb_global_variable},
631 {"rb_hash_aset", (RUBY_PROC*)&dll_rb_hash_aset},
632 {"rb_hash_new", (RUBY_PROC*)&dll_rb_hash_new},
633 {"rb_inspect", (RUBY_PROC*)&dll_rb_inspect},
634 {"rb_int2inum", (RUBY_PROC*)&dll_rb_int2inum},
Bram Moolenaarf711cb22018-08-01 18:42:13 +0200635 {"rb_intern", (RUBY_PROC*)&dll_rb_intern},
Bram Moolenaara2aa31a2014-02-23 22:52:40 +0100636# if VIM_SIZEOF_INT < VIM_SIZEOF_LONG /* 64 bits only */
Bram Moolenaar2623b4f2012-09-18 16:36:32 +0200637 {"rb_fix2int", (RUBY_PROC*)&dll_rb_fix2int},
638 {"rb_num2int", (RUBY_PROC*)&dll_rb_num2int},
639 {"rb_num2uint", (RUBY_PROC*)&dll_rb_num2uint},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200640# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000641 {"rb_lastline_get", (RUBY_PROC*)&dll_rb_lastline_get},
642 {"rb_lastline_set", (RUBY_PROC*)&dll_rb_lastline_set},
Bram Moolenaard0573012017-10-28 21:11:06 +0200643 {"rb_protect", (RUBY_PROC*)&dll_rb_protect},
644 {"rb_load", (RUBY_PROC*)&dll_rb_load},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000645 {"rb_num2long", (RUBY_PROC*)&dll_rb_num2long},
646 {"rb_num2ulong", (RUBY_PROC*)&dll_rb_num2ulong},
647 {"rb_obj_alloc", (RUBY_PROC*)&dll_rb_obj_alloc},
648 {"rb_obj_as_string", (RUBY_PROC*)&dll_rb_obj_as_string},
649 {"rb_obj_id", (RUBY_PROC*)&dll_rb_obj_id},
650 {"rb_raise", (RUBY_PROC*)&dll_rb_raise},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200651# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 18
Bram Moolenaarf9b5ef82010-09-29 13:02:53 +0200652 {"rb_string_value", (RUBY_PROC*)&dll_rb_string_value},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200653# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000654 {"rb_str2cstr", (RUBY_PROC*)&dll_rb_str2cstr},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200655# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000656 {"rb_str_cat", (RUBY_PROC*)&dll_rb_str_cat},
657 {"rb_str_concat", (RUBY_PROC*)&dll_rb_str_concat},
658 {"rb_str_new", (RUBY_PROC*)&dll_rb_str_new},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200659# ifdef need_rb_str_new_cstr
Bram Moolenaar1d2beae2010-05-22 21:56:55 +0200660 {"rb_str_new_cstr", (RUBY_PROC*)&dll_rb_str_new_cstr},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200661# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000662 {"rb_str_new2", (RUBY_PROC*)&dll_rb_str_new2},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200663# endif
664# ifdef RUBY19_OR_LATER
Bram Moolenaar165641d2010-02-17 16:23:09 +0100665 {"rb_errinfo", (RUBY_PROC*)&dll_rb_errinfo},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200666# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000667 {"ruby_errinfo", (RUBY_PROC*)&dll_ruby_errinfo},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200668# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000669 {"ruby_init", (RUBY_PROC*)&dll_ruby_init},
670 {"ruby_init_loadpath", (RUBY_PROC*)&dll_ruby_init_loadpath},
Bram Moolenaar4f974752019-02-17 17:44:42 +0100671# ifdef MSWIN
Bram Moolenaar9d5c84a2018-12-19 20:48:46 +0100672# ifdef RUBY19_OR_LATER
Bram Moolenaar4f391792017-01-15 16:59:07 +0100673 {"ruby_sysinit", (RUBY_PROC*)&dll_ruby_sysinit},
Bram Moolenaar9d5c84a2018-12-19 20:48:46 +0100674# else
675 {"NtInitialize", (RUBY_PROC*)&dll_NtInitialize},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200676# endif
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200677# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 18
Bram Moolenaar071d4272004-06-13 20:20:40 +0000678 {"rb_w32_snprintf", (RUBY_PROC*)&dll_rb_w32_snprintf},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200679# endif
Bram Moolenaar3ca71f12010-10-27 16:49:47 +0200680# endif
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200681# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 18
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100682 {"rb_string_value_ptr", (RUBY_PROC*)&dll_rb_string_value_ptr},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200683# if DYNAMIC_RUBY_VER <= 19
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100684 {"rb_float_new", (RUBY_PROC*)&dll_rb_float_new},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200685# else
Bram Moolenaar886ed692013-02-26 13:41:35 +0100686 {"rb_float_new_in_heap", (RUBY_PROC*)&dll_rb_float_new},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200687# endif
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100688 {"rb_ary_new", (RUBY_PROC*)&dll_rb_ary_new},
Bram Moolenaar51e9fbf2018-08-11 14:24:11 +0200689# ifdef RB_ARY_NEW4_MACRO
690 {"rb_ary_new_from_values", (RUBY_PROC*)&dll_rb_ary_new4},
691# else
692 {"rb_ary_new4", (RUBY_PROC*)&dll_rb_ary_new4},
693# endif
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100694 {"rb_ary_push", (RUBY_PROC*)&dll_rb_ary_push},
Bram Moolenaarb09c6842018-12-27 22:11:01 +0100695# if DYNAMIC_RUBY_VER >= 26
696 {"rb_ary_detransient", (RUBY_PROC*)&dll_rb_ary_detransient},
697# endif
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200698# endif
699# ifdef RUBY19_OR_LATER
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100700 {"rb_int2big", (RUBY_PROC*)&dll_rb_int2big},
Bram Moolenaar165641d2010-02-17 16:23:09 +0100701 {"ruby_script", (RUBY_PROC*)&dll_ruby_script},
702 {"rb_enc_find_index", (RUBY_PROC*)&dll_rb_enc_find_index},
703 {"rb_enc_find", (RUBY_PROC*)&dll_rb_enc_find},
704 {"rb_enc_str_new", (RUBY_PROC*)&dll_rb_enc_str_new},
705 {"rb_sprintf", (RUBY_PROC*)&dll_rb_sprintf},
Bram Moolenaar7a8ef142010-12-24 13:39:35 +0100706 {"rb_require", (RUBY_PROC*)&dll_rb_require},
Bram Moolenaar9b1067e2015-11-19 19:33:15 +0100707 {"ruby_options", (RUBY_PROC*)&dll_ruby_options},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200708# endif
Bram Moolenaar10f3a792013-05-20 12:52:29 +0200709# if defined(RUBY19_OR_LATER) || defined(RUBY_INIT_STACK)
710# ifdef __ia64
711 {"rb_ia64_bsp", (RUBY_PROC*)&dll_rb_ia64_bsp},
712# endif
713 {"ruby_init_stack", (RUBY_PROC*)&dll_ruby_init_stack},
714# endif
Bram Moolenaara1a118b2014-02-05 22:41:15 +0100715# if defined(USE_RGENGC) && USE_RGENGC
Bram Moolenaar0c7485f2015-01-14 14:04:10 +0100716# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER == 21
Bram Moolenaara1a118b2014-02-05 22:41:15 +0100717 {"rb_gc_writebarrier_unprotect_promoted", (RUBY_PROC*)&dll_rb_gc_writebarrier_unprotect_promoted},
Bram Moolenaar0c7485f2015-01-14 14:04:10 +0100718# else
719 {"rb_gc_writebarrier_unprotect", (RUBY_PROC*)&dll_rb_gc_writebarrier_unprotect},
720# endif
Bram Moolenaara1a118b2014-02-05 22:41:15 +0100721# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000722 {"", NULL},
723};
724
725/*
726 * Free ruby.dll
727 */
728 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100729end_dynamic_ruby(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000730{
731 if (hinstRuby)
732 {
Bram Moolenaarf9b5ef82010-09-29 13:02:53 +0200733 close_dll(hinstRuby);
Bram Moolenaar3ca71f12010-10-27 16:49:47 +0200734 hinstRuby = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000735 }
736}
737
738/*
739 * Load library and get all pointers.
740 * Parameter 'libname' provides name of DLL.
741 * Return OK or FAIL.
742 */
743 static int
744ruby_runtime_link_init(char *libname, int verbose)
745{
746 int i;
747
748 if (hinstRuby)
749 return OK;
Bram Moolenaarf9b5ef82010-09-29 13:02:53 +0200750 hinstRuby = load_dll(libname);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000751 if (!hinstRuby)
752 {
753 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100754 semsg(_(e_loadlib), libname);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000755 return FAIL;
756 }
757
758 for (i = 0; ruby_funcname_table[i].ptr; ++i)
759 {
Bram Moolenaarf9b5ef82010-09-29 13:02:53 +0200760 if (!(*ruby_funcname_table[i].ptr = symbol_from_dll(hinstRuby,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000761 ruby_funcname_table[i].name)))
762 {
Bram Moolenaarf9b5ef82010-09-29 13:02:53 +0200763 close_dll(hinstRuby);
Bram Moolenaar3ca71f12010-10-27 16:49:47 +0200764 hinstRuby = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000765 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100766 semsg(_(e_loadfunc), ruby_funcname_table[i].name);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000767 return FAIL;
768 }
769 }
770 return OK;
771}
772
773/*
774 * If ruby is enabled (there is installed ruby on Windows system) return TRUE,
775 * else FALSE.
776 */
777 int
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100778ruby_enabled(int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000779{
Bram Moolenaar25e4fcd2016-01-09 14:57:47 +0100780 return ruby_runtime_link_init((char *)p_rubydll, verbose) == OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000781}
782#endif /* defined(DYNAMIC_RUBY) || defined(PROTO) */
783
784 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100785ruby_end(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000786{
787#ifdef DYNAMIC_RUBY
788 end_dynamic_ruby();
789#endif
790}
791
792void ex_ruby(exarg_T *eap)
793{
794 int state;
795 char *script = NULL;
796
Bram Moolenaar35a2e192006-03-13 22:07:11 +0000797 script = (char *)script_get(eap, eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000798 if (!eap->skip && ensure_ruby_initialized())
799 {
800 if (script == NULL)
801 rb_eval_string_protect((char *)eap->arg, &state);
802 else
803 rb_eval_string_protect(script, &state);
804 if (state)
805 error_print(state);
806 }
807 vim_free(script);
808}
809
Bram Moolenaar165641d2010-02-17 16:23:09 +0100810/*
811 * In Ruby 1.9 or later, ruby String object has encoding.
812 * conversion buffer string of vim to ruby String object using
813 * VIM encoding option.
814 */
815 static VALUE
816vim_str2rb_enc_str(const char *s)
817{
Bram Moolenaar639a2552010-03-17 18:15:23 +0100818#ifdef RUBY19_OR_LATER
Bram Moolenaar165641d2010-02-17 16:23:09 +0100819 int isnum;
820 long lval;
821 char_u *sval;
822 rb_encoding *enc;
823
824 isnum = get_option_value((char_u *)"enc", &lval, &sval, 0);
825 if (isnum == 0)
826 {
827 enc = rb_enc_find((char *)sval);
828 vim_free(sval);
Bram Moolenaar758535a2016-03-30 22:06:16 +0200829 if (enc)
830 {
Bram Moolenaar9b0ac222016-06-01 20:31:43 +0200831 return rb_enc_str_new(s, (long)strlen(s), enc);
Bram Moolenaar165641d2010-02-17 16:23:09 +0100832 }
833 }
834#endif
835 return rb_str_new2(s);
836}
837
838 static VALUE
839eval_enc_string_protect(const char *str, int *state)
840{
Bram Moolenaar639a2552010-03-17 18:15:23 +0100841#ifdef RUBY19_OR_LATER
Bram Moolenaar165641d2010-02-17 16:23:09 +0100842 int isnum;
843 long lval;
844 char_u *sval;
845 rb_encoding *enc;
846 VALUE v;
847
848 isnum = get_option_value((char_u *)"enc", &lval, &sval, 0);
849 if (isnum == 0)
850 {
851 enc = rb_enc_find((char *)sval);
852 vim_free(sval);
853 if (enc)
854 {
855 v = rb_sprintf("#-*- coding:%s -*-\n%s", rb_enc_name(enc), str);
856 return rb_eval_string_protect(StringValuePtr(v), state);
857 }
858 }
859#endif
860 return rb_eval_string_protect(str, state);
861}
862
Bram Moolenaar071d4272004-06-13 20:20:40 +0000863void ex_rubydo(exarg_T *eap)
864{
865 int state;
866 linenr_T i;
Bram Moolenaarc593fee2017-01-29 23:11:25 +0100867 buf_T *was_curbuf = curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000868
869 if (ensure_ruby_initialized())
870 {
871 if (u_save(eap->line1 - 1, eap->line2 + 1) != OK)
872 return;
Bram Moolenaar758535a2016-03-30 22:06:16 +0200873 for (i = eap->line1; i <= eap->line2; i++)
874 {
Bram Moolenaare980d8a2010-12-08 13:11:21 +0100875 VALUE line;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000876
Bram Moolenaarc593fee2017-01-29 23:11:25 +0100877 if (i > curbuf->b_ml.ml_line_count)
878 break;
Bram Moolenaare980d8a2010-12-08 13:11:21 +0100879 line = vim_str2rb_enc_str((char *)ml_get(i));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000880 rb_lastline_set(line);
Bram Moolenaar165641d2010-02-17 16:23:09 +0100881 eval_enc_string_protect((char *) eap->arg, &state);
Bram Moolenaar758535a2016-03-30 22:06:16 +0200882 if (state)
883 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000884 error_print(state);
885 break;
886 }
Bram Moolenaarc593fee2017-01-29 23:11:25 +0100887 if (was_curbuf != curbuf)
888 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000889 line = rb_lastline_get();
Bram Moolenaar758535a2016-03-30 22:06:16 +0200890 if (!NIL_P(line))
891 {
892 if (TYPE(line) != T_STRING)
893 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100894 emsg(_("E265: $_ must be an instance of String"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000895 return;
896 }
Bram Moolenaar165641d2010-02-17 16:23:09 +0100897 ml_replace(i, (char_u *) StringValuePtr(line), 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000898 changed();
899#ifdef SYNTAX_HL
900 syn_changed(i); /* recompute syntax hl. for this line */
901#endif
902 }
903 }
904 check_cursor();
905 update_curbuf(NOT_VALID);
906 }
907}
908
Bram Moolenaar0b394642018-05-17 13:11:46 +0200909static VALUE rb_load_wrap(VALUE file_to_load)
Bram Moolenaar37badc82018-01-31 20:15:30 +0100910{
911 rb_load(file_to_load, 0);
912 return Qnil;
913}
914
Bram Moolenaar071d4272004-06-13 20:20:40 +0000915void ex_rubyfile(exarg_T *eap)
916{
917 int state;
918
919 if (ensure_ruby_initialized())
920 {
Bram Moolenaar37badc82018-01-31 20:15:30 +0100921 VALUE file_to_load = rb_str_new2((const char *)eap->arg);
922 rb_protect(rb_load_wrap, file_to_load, &state);
923 if (state)
924 error_print(state);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000925 }
926}
927
928void ruby_buffer_free(buf_T *buf)
929{
Bram Moolenaare344bea2005-09-01 20:46:49 +0000930 if (buf->b_ruby_ref)
931 {
932 rb_hash_aset(objtbl, rb_obj_id((VALUE) buf->b_ruby_ref), Qnil);
933 RDATA(buf->b_ruby_ref)->data = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000934 }
935}
936
937void ruby_window_free(win_T *win)
938{
Bram Moolenaare344bea2005-09-01 20:46:49 +0000939 if (win->w_ruby_ref)
940 {
941 rb_hash_aset(objtbl, rb_obj_id((VALUE) win->w_ruby_ref), Qnil);
942 RDATA(win->w_ruby_ref)->data = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000943 }
944}
945
946static int ensure_ruby_initialized(void)
947{
948 if (!ruby_initialized)
949 {
950#ifdef DYNAMIC_RUBY
951 if (ruby_enabled(TRUE))
952 {
953#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +0100954#ifdef MSWIN
Bram Moolenaar0b69c732010-02-17 15:11:50 +0100955 /* suggested by Ariya Mizutani */
956 int argc = 1;
957 char *argv[] = {"gvim.exe"};
Bram Moolenaar90140742014-11-27 17:44:08 +0100958 char **argvp = argv;
Bram Moolenaarfe6ce332017-01-14 20:12:01 +0100959# ifdef RUBY19_OR_LATER
960 ruby_sysinit(&argc, &argvp);
961# else
Bram Moolenaar90140742014-11-27 17:44:08 +0100962 NtInitialize(&argc, &argvp);
Bram Moolenaarfe6ce332017-01-14 20:12:01 +0100963# endif
Bram Moolenaar0b69c732010-02-17 15:11:50 +0100964#endif
Bram Moolenaarb2c03502010-07-02 20:20:09 +0200965 {
Bram Moolenaar10f3a792013-05-20 12:52:29 +0200966#if defined(RUBY19_OR_LATER) || defined(RUBY_INIT_STACK)
Bram Moolenaar99685e62013-05-11 13:56:18 +0200967 ruby_init_stack(ruby_stack_start);
Bram Moolenaar165641d2010-02-17 16:23:09 +0100968#endif
Bram Moolenaarb2c03502010-07-02 20:20:09 +0200969 ruby_init();
970 }
Bram Moolenaar639a2552010-03-17 18:15:23 +0100971#ifdef RUBY19_OR_LATER
Bram Moolenaar7a8ef142010-12-24 13:39:35 +0100972 {
973 int dummy_argc = 2;
Bram Moolenaard1bc96ce2017-09-26 21:21:44 +0200974 char *dummy_argv[] = {"vim-ruby", "-e_=0"};
Bram Moolenaar9b1067e2015-11-19 19:33:15 +0100975 ruby_options(dummy_argc, dummy_argv);
Bram Moolenaar7a8ef142010-12-24 13:39:35 +0100976 }
Bram Moolenaar165641d2010-02-17 16:23:09 +0100977 ruby_script("vim-ruby");
Bram Moolenaar7a8ef142010-12-24 13:39:35 +0100978#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000979 ruby_init_loadpath();
Bram Moolenaar165641d2010-02-17 16:23:09 +0100980#endif
Bram Moolenaar7a8ef142010-12-24 13:39:35 +0100981 ruby_io_init();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000982 ruby_vim_init();
983 ruby_initialized = 1;
984#ifdef DYNAMIC_RUBY
985 }
986 else
987 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100988 emsg(_("E266: Sorry, this command is disabled, the Ruby library could not be loaded."));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000989 return 0;
990 }
991#endif
992 }
993 return ruby_initialized;
994}
995
996static void error_print(int state)
997{
Bram Moolenaarb09c6842018-12-27 22:11:01 +0100998#if !defined(DYNAMIC_RUBY) && !defined(RUBY19_OR_LATER)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000999 RUBYEXTERN VALUE ruby_errinfo;
1000#endif
Bram Moolenaarf711cb22018-08-01 18:42:13 +02001001 VALUE error;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001002 VALUE eclass;
1003 VALUE einfo;
Bram Moolenaarf711cb22018-08-01 18:42:13 +02001004 VALUE bt;
1005 int attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001006 char buff[BUFSIZ];
Bram Moolenaarf711cb22018-08-01 18:42:13 +02001007 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001008
1009#define TAG_RETURN 0x1
1010#define TAG_BREAK 0x2
1011#define TAG_NEXT 0x3
1012#define TAG_RETRY 0x4
1013#define TAG_REDO 0x5
1014#define TAG_RAISE 0x6
1015#define TAG_THROW 0x7
1016#define TAG_FATAL 0x8
1017#define TAG_MASK 0xf
1018
Bram Moolenaar758535a2016-03-30 22:06:16 +02001019 switch (state)
1020 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001021 case TAG_RETURN:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001022 emsg(_("E267: unexpected return"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001023 break;
1024 case TAG_NEXT:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001025 emsg(_("E268: unexpected next"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001026 break;
1027 case TAG_BREAK:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001028 emsg(_("E269: unexpected break"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001029 break;
1030 case TAG_REDO:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001031 emsg(_("E270: unexpected redo"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001032 break;
1033 case TAG_RETRY:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001034 emsg(_("E271: retry outside of rescue clause"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001035 break;
1036 case TAG_RAISE:
1037 case TAG_FATAL:
Bram Moolenaar639a2552010-03-17 18:15:23 +01001038#ifdef RUBY19_OR_LATER
Bram Moolenaarf711cb22018-08-01 18:42:13 +02001039 error = rb_errinfo();
Bram Moolenaar165641d2010-02-17 16:23:09 +01001040#else
Bram Moolenaarf711cb22018-08-01 18:42:13 +02001041 error = ruby_errinfo;
Bram Moolenaar165641d2010-02-17 16:23:09 +01001042#endif
Bram Moolenaarf711cb22018-08-01 18:42:13 +02001043 eclass = CLASS_OF(error);
1044 einfo = rb_obj_as_string(error);
Bram Moolenaar758535a2016-03-30 22:06:16 +02001045 if (eclass == rb_eRuntimeError && RSTRING_LEN(einfo) == 0)
1046 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001047 emsg(_("E272: unhandled exception"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001048 }
Bram Moolenaar758535a2016-03-30 22:06:16 +02001049 else
1050 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001051 VALUE epath;
1052 char *p;
1053
1054 epath = rb_class_path(eclass);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00001055 vim_snprintf(buff, BUFSIZ, "%s: %s",
Bram Moolenaar165641d2010-02-17 16:23:09 +01001056 RSTRING_PTR(epath), RSTRING_PTR(einfo));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001057 p = strchr(buff, '\n');
1058 if (p) *p = '\0';
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001059 emsg(buff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001060 }
Bram Moolenaarf711cb22018-08-01 18:42:13 +02001061
1062 attr = syn_name2attr((char_u *)"Error");
1063# ifdef RUBY21_OR_LATER
1064 bt = rb_funcallv(error, rb_intern("backtrace"), 0, 0);
1065 for (i = 0; i < RARRAY_LEN(bt); i++)
Bram Moolenaar32526b32019-01-19 17:43:09 +01001066 msg_attr(RSTRING_PTR(RARRAY_AREF(bt, i)), attr);
Bram Moolenaarf711cb22018-08-01 18:42:13 +02001067# else
1068 bt = rb_funcall2(error, rb_intern("backtrace"), 0, 0);
1069 for (i = 0; i < RARRAY_LEN(bt); i++)
Bram Moolenaar32526b32019-01-19 17:43:09 +01001070 msg_attr(RSTRING_PTR(RARRAY_PTR(bt)[i]), attr);
Bram Moolenaarf711cb22018-08-01 18:42:13 +02001071# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001072 break;
1073 default:
Bram Moolenaar9c13b352005-05-19 20:53:52 +00001074 vim_snprintf(buff, BUFSIZ, _("E273: unknown longjmp status %d"), state);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001075 emsg(buff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001076 break;
1077 }
1078}
1079
Bram Moolenaarcd8b20a2009-05-22 16:20:57 +00001080static VALUE vim_message(VALUE self UNUSED, VALUE str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001081{
1082 char *buff, *p;
1083
1084 str = rb_obj_as_string(str);
Bram Moolenaar3f5f7952011-08-04 19:34:59 +02001085 if (RSTRING_LEN(str) > 0)
1086 {
1087 /* Only do this when the string isn't empty, alloc(0) causes trouble. */
Bram Moolenaar00ccf542017-09-03 15:17:48 +02001088 buff = ALLOCA_N(char, RSTRING_LEN(str) + 1);
Bram Moolenaar3f5f7952011-08-04 19:34:59 +02001089 strcpy(buff, RSTRING_PTR(str));
1090 p = strchr(buff, '\n');
1091 if (p) *p = '\0';
Bram Moolenaar32526b32019-01-19 17:43:09 +01001092 msg(buff);
Bram Moolenaar3f5f7952011-08-04 19:34:59 +02001093 }
1094 else
1095 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01001096 msg("");
Bram Moolenaar3f5f7952011-08-04 19:34:59 +02001097 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001098 return Qnil;
1099}
1100
Bram Moolenaarcd8b20a2009-05-22 16:20:57 +00001101static VALUE vim_set_option(VALUE self UNUSED, VALUE str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001102{
Bram Moolenaar165641d2010-02-17 16:23:09 +01001103 do_set((char_u *)StringValuePtr(str), 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001104 update_screen(NOT_VALID);
1105 return Qnil;
1106}
1107
Bram Moolenaarcd8b20a2009-05-22 16:20:57 +00001108static VALUE vim_command(VALUE self UNUSED, VALUE str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001109{
Bram Moolenaar165641d2010-02-17 16:23:09 +01001110 do_cmdline_cmd((char_u *)StringValuePtr(str));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001111 return Qnil;
1112}
1113
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001114#ifdef FEAT_EVAL
1115static VALUE vim_to_ruby(typval_T *tv)
1116{
1117 VALUE result = Qnil;
1118
1119 if (tv->v_type == VAR_STRING)
1120 {
Bram Moolenaar94127e42010-03-19 23:08:48 +01001121 result = rb_str_new2(tv->vval.v_string == NULL
1122 ? "" : (char *)(tv->vval.v_string));
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001123 }
1124 else if (tv->v_type == VAR_NUMBER)
1125 {
Bram Moolenaar639a2552010-03-17 18:15:23 +01001126 result = INT2NUM(tv->vval.v_number);
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001127 }
1128# ifdef FEAT_FLOAT
1129 else if (tv->v_type == VAR_FLOAT)
1130 {
Bram Moolenaar639a2552010-03-17 18:15:23 +01001131 result = rb_float_new(tv->vval.v_float);
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001132 }
1133# endif
1134 else if (tv->v_type == VAR_LIST)
1135 {
Bram Moolenaar639a2552010-03-17 18:15:23 +01001136 list_T *list = tv->vval.v_list;
1137 listitem_T *curr;
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001138
Bram Moolenaar639a2552010-03-17 18:15:23 +01001139 result = rb_ary_new();
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001140
Bram Moolenaar639a2552010-03-17 18:15:23 +01001141 if (list != NULL)
1142 {
1143 for (curr = list->lv_first; curr != NULL; curr = curr->li_next)
1144 {
1145 rb_ary_push(result, vim_to_ruby(&curr->li_tv));
1146 }
1147 }
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001148 }
1149 else if (tv->v_type == VAR_DICT)
1150 {
Bram Moolenaar639a2552010-03-17 18:15:23 +01001151 result = rb_hash_new();
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001152
Bram Moolenaar639a2552010-03-17 18:15:23 +01001153 if (tv->vval.v_dict != NULL)
1154 {
1155 hashtab_T *ht = &tv->vval.v_dict->dv_hashtab;
1156 long_u todo = ht->ht_used;
1157 hashitem_T *hi;
1158 dictitem_T *di;
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001159
Bram Moolenaar639a2552010-03-17 18:15:23 +01001160 for (hi = ht->ht_array; todo > 0; ++hi)
1161 {
1162 if (!HASHITEM_EMPTY(hi))
1163 {
1164 --todo;
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001165
Bram Moolenaar639a2552010-03-17 18:15:23 +01001166 di = dict_lookup(hi);
1167 rb_hash_aset(result, rb_str_new2((char *)hi->hi_key),
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001168 vim_to_ruby(&di->di_tv));
Bram Moolenaar639a2552010-03-17 18:15:23 +01001169 }
1170 }
1171 }
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001172 }
1173 else if (tv->v_type == VAR_SPECIAL)
1174 {
Bram Moolenaard84b26a2018-07-28 17:18:09 +02001175 if (tv->vval.v_number == VVAL_TRUE)
1176 result = Qtrue;
1177 else if (tv->vval.v_number == VVAL_FALSE)
1178 result = Qfalse;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001179 }
1180 else if (tv->v_type == VAR_BLOB)
1181 {
1182 result = rb_str_new(tv->vval.v_blob->bv_ga.ga_data,
1183 tv->vval.v_blob->bv_ga.ga_len);
1184 }
1185 /* else return Qnil; */
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001186
1187 return result;
1188}
1189#endif
1190
Bram Moolenaarcd8b20a2009-05-22 16:20:57 +00001191static VALUE vim_evaluate(VALUE self UNUSED, VALUE str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001192{
1193#ifdef FEAT_EVAL
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001194 typval_T *tv;
1195 VALUE result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001196
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001197 tv = eval_expr((char_u *)StringValuePtr(str), NULL);
1198 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001199 {
Bram Moolenaar639a2552010-03-17 18:15:23 +01001200 return Qnil;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001201 }
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001202 result = vim_to_ruby(tv);
1203
1204 free_tv(tv);
1205
1206 return result;
1207#else
1208 return Qnil;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001209#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001210}
1211
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001212#ifdef USE_TYPEDDATA
1213static size_t buffer_dsize(const void *buf);
1214
1215static const rb_data_type_t buffer_type = {
1216 "vim_buffer",
1217 {0, 0, buffer_dsize, {0, 0}},
1218 0, 0,
1219# ifdef RUBY_TYPED_FREE_IMMEDIATELY
1220 0,
1221# endif
1222};
1223
1224static size_t buffer_dsize(const void *buf UNUSED)
1225{
1226 return sizeof(buf_T);
1227}
1228#endif
1229
Bram Moolenaar071d4272004-06-13 20:20:40 +00001230static VALUE buffer_new(buf_T *buf)
1231{
Bram Moolenaare344bea2005-09-01 20:46:49 +00001232 if (buf->b_ruby_ref)
1233 {
1234 return (VALUE) buf->b_ruby_ref;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001235 }
Bram Moolenaare344bea2005-09-01 20:46:49 +00001236 else
1237 {
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001238#ifdef USE_TYPEDDATA
1239 VALUE obj = TypedData_Wrap_Struct(cBuffer, &buffer_type, buf);
1240#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001241 VALUE obj = Data_Wrap_Struct(cBuffer, 0, 0, buf);
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001242#endif
Bram Moolenaare344bea2005-09-01 20:46:49 +00001243 buf->b_ruby_ref = (void *) obj;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001244 rb_hash_aset(objtbl, rb_obj_id(obj), obj);
1245 return obj;
1246 }
1247}
1248
1249static buf_T *get_buf(VALUE obj)
1250{
1251 buf_T *buf;
1252
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001253#ifdef USE_TYPEDDATA
1254 TypedData_Get_Struct(obj, buf_T, &buffer_type, buf);
1255#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001256 Data_Get_Struct(obj, buf_T, buf);
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001257#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001258 if (buf == NULL)
1259 rb_raise(eDeletedBufferError, "attempt to refer to deleted buffer");
1260 return buf;
1261}
1262
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001263static VALUE vim_blob(VALUE self UNUSED, VALUE str)
1264{
1265 VALUE result = rb_str_new("0z", 2);
1266 char buf[4];
1267 int i;
1268 for (i = 0; i < RSTRING_LEN(str); i++)
1269 {
1270 sprintf(buf, "%02X", RSTRING_PTR(str)[i]);
Bram Moolenaarb191be22019-01-30 21:00:12 +01001271 rb_str_concat(result, rb_str_new2(buf));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001272 }
1273 return result;
1274}
1275
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001276static VALUE buffer_s_current(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001277{
1278 return buffer_new(curbuf);
1279}
1280
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001281static VALUE buffer_s_count(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001282{
1283 buf_T *b;
1284 int n = 0;
1285
Bram Moolenaar29323592016-07-24 22:04:11 +02001286 FOR_ALL_BUFFERS(b)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001287 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001288 /* Deleted buffers should not be counted
1289 * SegPhault - 01/07/05 */
1290 if (b->b_p_bl)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001291 n++;
1292 }
1293
Bram Moolenaar071d4272004-06-13 20:20:40 +00001294 return INT2NUM(n);
1295}
1296
Bram Moolenaarcd8b20a2009-05-22 16:20:57 +00001297static VALUE buffer_s_aref(VALUE self UNUSED, VALUE num)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001298{
1299 buf_T *b;
1300 int n = NUM2INT(num);
1301
Bram Moolenaar29323592016-07-24 22:04:11 +02001302 FOR_ALL_BUFFERS(b)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001303 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001304 /* Deleted buffers should not be counted
1305 * SegPhault - 01/07/05 */
1306 if (!b->b_p_bl)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001307 continue;
1308
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001309 if (n == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001310 return buffer_new(b);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001311
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001312 n--;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001313 }
1314 return Qnil;
1315}
1316
1317static VALUE buffer_name(VALUE self)
1318{
1319 buf_T *buf = get_buf(self);
1320
Bram Moolenaar35a2e192006-03-13 22:07:11 +00001321 return buf->b_ffname ? rb_str_new2((char *)buf->b_ffname) : Qnil;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001322}
1323
1324static VALUE buffer_number(VALUE self)
1325{
1326 buf_T *buf = get_buf(self);
1327
1328 return INT2NUM(buf->b_fnum);
1329}
1330
1331static VALUE buffer_count(VALUE self)
1332{
1333 buf_T *buf = get_buf(self);
1334
1335 return INT2NUM(buf->b_ml.ml_line_count);
1336}
1337
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001338static VALUE get_buffer_line(buf_T *buf, linenr_T n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001339{
Bram Moolenaar3c531602010-11-16 14:46:19 +01001340 if (n <= 0 || n > buf->b_ml.ml_line_count)
1341 rb_raise(rb_eIndexError, "line number %ld out of range", (long)n);
1342 return vim_str2rb_enc_str((char *)ml_get_buf(buf, n, FALSE));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001343}
1344
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001345static VALUE buffer_aref(VALUE self, VALUE num)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001346{
1347 buf_T *buf = get_buf(self);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001348
1349 if (buf != NULL)
1350 return get_buffer_line(buf, (linenr_T)NUM2LONG(num));
1351 return Qnil; /* For stop warning */
1352}
1353
1354static VALUE set_buffer_line(buf_T *buf, linenr_T n, VALUE str)
1355{
Bram Moolenaar165641d2010-02-17 16:23:09 +01001356 char *line = StringValuePtr(str);
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001357 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001358
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001359 if (n > 0 && n <= buf->b_ml.ml_line_count && line != NULL)
1360 {
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001361 /* set curwin/curbuf for "buf" and save some things */
1362 aucmd_prepbuf(&aco, buf);
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001363
Bram Moolenaar758535a2016-03-30 22:06:16 +02001364 if (u_savesub(n) == OK)
1365 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001366 ml_replace(n, (char_u *)line, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001367 changed();
1368#ifdef SYNTAX_HL
1369 syn_changed(n); /* recompute syntax hl. for this line */
1370#endif
1371 }
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001372
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001373 /* restore curwin/curbuf and a few other things */
1374 aucmd_restbuf(&aco);
1375 /* Careful: autocommands may have made "buf" invalid! */
Bram Moolenaarf30e74c2006-08-16 17:35:00 +00001376
Bram Moolenaar071d4272004-06-13 20:20:40 +00001377 update_curbuf(NOT_VALID);
1378 }
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001379 else
1380 {
Bram Moolenaar165641d2010-02-17 16:23:09 +01001381 rb_raise(rb_eIndexError, "line number %ld out of range", (long)n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001382 }
1383 return str;
1384}
1385
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001386static VALUE buffer_aset(VALUE self, VALUE num, VALUE str)
1387{
1388 buf_T *buf = get_buf(self);
1389
1390 if (buf != NULL)
1391 return set_buffer_line(buf, (linenr_T)NUM2LONG(num), str);
1392 return str;
1393}
1394
Bram Moolenaar071d4272004-06-13 20:20:40 +00001395static VALUE buffer_delete(VALUE self, VALUE num)
1396{
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001397 buf_T *buf = get_buf(self);
1398 long n = NUM2LONG(num);
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001399 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001400
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001401 if (n > 0 && n <= buf->b_ml.ml_line_count)
1402 {
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001403 /* set curwin/curbuf for "buf" and save some things */
1404 aucmd_prepbuf(&aco, buf);
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001405
Bram Moolenaar758535a2016-03-30 22:06:16 +02001406 if (u_savedel(n, 1) == OK)
1407 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001408 ml_delete(n, 0);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001409
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001410 /* Changes to non-active buffers should properly refresh
1411 * SegPhault - 01/09/05 */
1412 deleted_lines_mark(n, 1L);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001413
Bram Moolenaar071d4272004-06-13 20:20:40 +00001414 changed();
1415 }
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001416
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001417 /* restore curwin/curbuf and a few other things */
1418 aucmd_restbuf(&aco);
1419 /* Careful: autocommands may have made "buf" invalid! */
Bram Moolenaarf30e74c2006-08-16 17:35:00 +00001420
Bram Moolenaar071d4272004-06-13 20:20:40 +00001421 update_curbuf(NOT_VALID);
1422 }
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001423 else
1424 {
Bram Moolenaar165641d2010-02-17 16:23:09 +01001425 rb_raise(rb_eIndexError, "line number %ld out of range", n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001426 }
1427 return Qnil;
1428}
1429
1430static VALUE buffer_append(VALUE self, VALUE num, VALUE str)
1431{
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001432 buf_T *buf = get_buf(self);
Bram Moolenaar165641d2010-02-17 16:23:09 +01001433 char *line = StringValuePtr(str);
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001434 long n = NUM2LONG(num);
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001435 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001436
Bram Moolenaar3c531602010-11-16 14:46:19 +01001437 if (line == NULL)
1438 {
Bram Moolenaar165641d2010-02-17 16:23:09 +01001439 rb_raise(rb_eIndexError, "NULL line");
1440 }
1441 else if (n >= 0 && n <= buf->b_ml.ml_line_count)
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001442 {
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001443 /* set curwin/curbuf for "buf" and save some things */
1444 aucmd_prepbuf(&aco, buf);
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001445
Bram Moolenaar758535a2016-03-30 22:06:16 +02001446 if (u_inssub(n + 1) == OK)
1447 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001448 ml_append(n, (char_u *) line, (colnr_T) 0, FALSE);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001449
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001450 /* Changes to non-active buffers should properly refresh screen
1451 * SegPhault - 12/20/04 */
1452 appended_lines_mark(n, 1L);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001453
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001454 changed();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001455 }
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001456
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001457 /* restore curwin/curbuf and a few other things */
1458 aucmd_restbuf(&aco);
1459 /* Careful: autocommands may have made "buf" invalid! */
Bram Moolenaarf30e74c2006-08-16 17:35:00 +00001460
Bram Moolenaar071d4272004-06-13 20:20:40 +00001461 update_curbuf(NOT_VALID);
1462 }
Bram Moolenaar3c531602010-11-16 14:46:19 +01001463 else
1464 {
Bram Moolenaar165641d2010-02-17 16:23:09 +01001465 rb_raise(rb_eIndexError, "line number %ld out of range", n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001466 }
1467 return str;
1468}
1469
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001470#ifdef USE_TYPEDDATA
1471static size_t window_dsize(const void *buf);
1472
1473static const rb_data_type_t window_type = {
1474 "vim_window",
1475 {0, 0, window_dsize, {0, 0}},
1476 0, 0,
1477# ifdef RUBY_TYPED_FREE_IMMEDIATELY
1478 0,
1479# endif
1480};
1481
1482static size_t window_dsize(const void *win UNUSED)
1483{
1484 return sizeof(win_T);
1485}
1486#endif
1487
Bram Moolenaar071d4272004-06-13 20:20:40 +00001488static VALUE window_new(win_T *win)
1489{
Bram Moolenaare344bea2005-09-01 20:46:49 +00001490 if (win->w_ruby_ref)
1491 {
1492 return (VALUE) win->w_ruby_ref;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001493 }
Bram Moolenaare344bea2005-09-01 20:46:49 +00001494 else
1495 {
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001496#ifdef USE_TYPEDDATA
1497 VALUE obj = TypedData_Wrap_Struct(cVimWindow, &window_type, win);
1498#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001499 VALUE obj = Data_Wrap_Struct(cVimWindow, 0, 0, win);
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001500#endif
Bram Moolenaare344bea2005-09-01 20:46:49 +00001501 win->w_ruby_ref = (void *) obj;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001502 rb_hash_aset(objtbl, rb_obj_id(obj), obj);
1503 return obj;
1504 }
1505}
1506
1507static win_T *get_win(VALUE obj)
1508{
1509 win_T *win;
1510
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001511#ifdef USE_TYPEDDATA
1512 TypedData_Get_Struct(obj, win_T, &window_type, win);
1513#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001514 Data_Get_Struct(obj, win_T, win);
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001515#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001516 if (win == NULL)
1517 rb_raise(eDeletedWindowError, "attempt to refer to deleted window");
1518 return win;
1519}
1520
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001521static VALUE window_s_current(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001522{
1523 return window_new(curwin);
1524}
1525
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001526/*
1527 * Added line manipulation functions
1528 * SegPhault - 03/07/05
1529 */
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001530static VALUE line_s_current(void)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001531{
1532 return get_buffer_line(curbuf, curwin->w_cursor.lnum);
1533}
1534
Bram Moolenaarcd8b20a2009-05-22 16:20:57 +00001535static VALUE set_current_line(VALUE self UNUSED, VALUE str)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001536{
1537 return set_buffer_line(curbuf, curwin->w_cursor.lnum, str);
1538}
1539
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001540static VALUE current_line_number(void)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001541{
1542 return INT2FIX((int)curwin->w_cursor.lnum);
1543}
1544
1545
1546
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001547static VALUE window_s_count(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001548{
Bram Moolenaar071d4272004-06-13 20:20:40 +00001549 win_T *w;
1550 int n = 0;
1551
Bram Moolenaar29323592016-07-24 22:04:11 +02001552 FOR_ALL_WINDOWS(w)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001553 n++;
1554 return INT2NUM(n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001555}
1556
Bram Moolenaarcd8b20a2009-05-22 16:20:57 +00001557static VALUE window_s_aref(VALUE self UNUSED, VALUE num)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001558{
1559 win_T *w;
1560 int n = NUM2INT(num);
1561
Bram Moolenaar071d4272004-06-13 20:20:40 +00001562 for (w = firstwin; w != NULL; w = w->w_next, --n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001563 if (n == 0)
1564 return window_new(w);
1565 return Qnil;
1566}
1567
1568static VALUE window_buffer(VALUE self)
1569{
1570 win_T *win = get_win(self);
1571
1572 return buffer_new(win->w_buffer);
1573}
1574
1575static VALUE window_height(VALUE self)
1576{
1577 win_T *win = get_win(self);
1578
1579 return INT2NUM(win->w_height);
1580}
1581
1582static VALUE window_set_height(VALUE self, VALUE height)
1583{
1584 win_T *win = get_win(self);
1585 win_T *savewin = curwin;
1586
1587 curwin = win;
1588 win_setheight(NUM2INT(height));
1589 curwin = savewin;
1590 return height;
1591}
1592
Bram Moolenaarfeeaa682013-02-14 22:19:51 +01001593static VALUE window_width(VALUE self UNUSED)
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001594{
Bram Moolenaare745d752017-09-22 16:56:22 +02001595 return INT2NUM(get_win(self)->w_width);
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001596}
1597
Bram Moolenaarfeeaa682013-02-14 22:19:51 +01001598static VALUE window_set_width(VALUE self UNUSED, VALUE width)
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001599{
1600 win_T *win = get_win(self);
1601 win_T *savewin = curwin;
1602
1603 curwin = win;
1604 win_setwidth(NUM2INT(width));
1605 curwin = savewin;
1606 return width;
1607}
1608
Bram Moolenaar071d4272004-06-13 20:20:40 +00001609static VALUE window_cursor(VALUE self)
1610{
1611 win_T *win = get_win(self);
1612
1613 return rb_assoc_new(INT2NUM(win->w_cursor.lnum), INT2NUM(win->w_cursor.col));
1614}
1615
1616static VALUE window_set_cursor(VALUE self, VALUE pos)
1617{
1618 VALUE lnum, col;
1619 win_T *win = get_win(self);
1620
1621 Check_Type(pos, T_ARRAY);
Bram Moolenaar165641d2010-02-17 16:23:09 +01001622 if (RARRAY_LEN(pos) != 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001623 rb_raise(rb_eArgError, "array length must be 2");
Bram Moolenaar165641d2010-02-17 16:23:09 +01001624 lnum = RARRAY_PTR(pos)[0];
1625 col = RARRAY_PTR(pos)[1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001626 win->w_cursor.lnum = NUM2LONG(lnum);
1627 win->w_cursor.col = NUM2UINT(col);
Bram Moolenaar53901442018-07-25 22:02:36 +02001628 win->w_set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001629 check_cursor(); /* put cursor on an existing line */
1630 update_screen(NOT_VALID);
1631 return Qnil;
1632}
1633
Bram Moolenaar6217cdc2012-04-25 12:28:09 +02001634static VALUE f_nop(VALUE self UNUSED)
Bram Moolenaar35df7d22012-04-20 18:05:47 +02001635{
1636 return Qnil;
1637}
1638
Bram Moolenaarcd8b20a2009-05-22 16:20:57 +00001639static VALUE f_p(int argc, VALUE *argv, VALUE self UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001640{
1641 int i;
1642 VALUE str = rb_str_new("", 0);
Bram Moolenaar51e9fbf2018-08-11 14:24:11 +02001643 VALUE ret = Qnil;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001644
Bram Moolenaar758535a2016-03-30 22:06:16 +02001645 for (i = 0; i < argc; i++)
1646 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001647 if (i > 0) rb_str_cat(str, ", ", 2);
1648 rb_str_concat(str, rb_inspect(argv[i]));
1649 }
Bram Moolenaar32526b32019-01-19 17:43:09 +01001650 msg(RSTRING_PTR(str));
Bram Moolenaar51e9fbf2018-08-11 14:24:11 +02001651
1652 if (argc == 1)
1653 ret = argv[0];
1654 else if (argc > 1)
1655 ret = rb_ary_new4(argc, argv);
1656 return ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001657}
1658
1659static void ruby_io_init(void)
1660{
1661#ifndef DYNAMIC_RUBY
1662 RUBYEXTERN VALUE rb_stdout;
Bram Moolenaarb6c8cd82018-07-24 05:41:30 +02001663 RUBYEXTERN VALUE rb_stderr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001664#endif
1665
1666 rb_stdout = rb_obj_alloc(rb_cObject);
Bram Moolenaarb6c8cd82018-07-24 05:41:30 +02001667 rb_stderr = rb_obj_alloc(rb_cObject);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001668 rb_define_singleton_method(rb_stdout, "write", vim_message, 1);
Bram Moolenaar35df7d22012-04-20 18:05:47 +02001669 rb_define_singleton_method(rb_stdout, "flush", f_nop, 0);
Bram Moolenaarb6c8cd82018-07-24 05:41:30 +02001670 rb_define_singleton_method(rb_stderr, "write", vim_message, 1);
1671 rb_define_singleton_method(rb_stderr, "flush", f_nop, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001672 rb_define_global_function("p", f_p, -1);
1673}
1674
1675static void ruby_vim_init(void)
1676{
1677 objtbl = rb_hash_new();
1678 rb_global_variable(&objtbl);
1679
Bram Moolenaarf711faf2007-05-10 16:48:19 +00001680 /* The Vim module used to be called "VIM", but "Vim" is better. Make an
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02001681 * alias "VIM" for backwards compatibility. */
Bram Moolenaarf711faf2007-05-10 16:48:19 +00001682 mVIM = rb_define_module("Vim");
1683 rb_define_const(rb_cObject, "VIM", mVIM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001684 rb_define_const(mVIM, "VERSION_MAJOR", INT2NUM(VIM_VERSION_MAJOR));
1685 rb_define_const(mVIM, "VERSION_MINOR", INT2NUM(VIM_VERSION_MINOR));
1686 rb_define_const(mVIM, "VERSION_BUILD", INT2NUM(VIM_VERSION_BUILD));
1687 rb_define_const(mVIM, "VERSION_PATCHLEVEL", INT2NUM(VIM_VERSION_PATCHLEVEL));
1688 rb_define_const(mVIM, "VERSION_SHORT", rb_str_new2(VIM_VERSION_SHORT));
1689 rb_define_const(mVIM, "VERSION_MEDIUM", rb_str_new2(VIM_VERSION_MEDIUM));
1690 rb_define_const(mVIM, "VERSION_LONG", rb_str_new2(VIM_VERSION_LONG));
1691 rb_define_const(mVIM, "VERSION_LONG_DATE", rb_str_new2(VIM_VERSION_LONG_DATE));
1692 rb_define_module_function(mVIM, "message", vim_message, 1);
1693 rb_define_module_function(mVIM, "set_option", vim_set_option, 1);
1694 rb_define_module_function(mVIM, "command", vim_command, 1);
1695 rb_define_module_function(mVIM, "evaluate", vim_evaluate, 1);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001696 rb_define_module_function(mVIM, "blob", vim_blob, 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001697
1698 eDeletedBufferError = rb_define_class_under(mVIM, "DeletedBufferError",
1699 rb_eStandardError);
1700 eDeletedWindowError = rb_define_class_under(mVIM, "DeletedWindowError",
1701 rb_eStandardError);
1702
1703 cBuffer = rb_define_class_under(mVIM, "Buffer", rb_cObject);
1704 rb_define_singleton_method(cBuffer, "current", buffer_s_current, 0);
1705 rb_define_singleton_method(cBuffer, "count", buffer_s_count, 0);
1706 rb_define_singleton_method(cBuffer, "[]", buffer_s_aref, 1);
1707 rb_define_method(cBuffer, "name", buffer_name, 0);
1708 rb_define_method(cBuffer, "number", buffer_number, 0);
1709 rb_define_method(cBuffer, "count", buffer_count, 0);
1710 rb_define_method(cBuffer, "length", buffer_count, 0);
1711 rb_define_method(cBuffer, "[]", buffer_aref, 1);
1712 rb_define_method(cBuffer, "[]=", buffer_aset, 2);
1713 rb_define_method(cBuffer, "delete", buffer_delete, 1);
1714 rb_define_method(cBuffer, "append", buffer_append, 2);
1715
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001716 /* Added line manipulation functions
1717 * SegPhault - 03/07/05 */
1718 rb_define_method(cBuffer, "line_number", current_line_number, 0);
1719 rb_define_method(cBuffer, "line", line_s_current, 0);
1720 rb_define_method(cBuffer, "line=", set_current_line, 1);
1721
1722
Bram Moolenaar071d4272004-06-13 20:20:40 +00001723 cVimWindow = rb_define_class_under(mVIM, "Window", rb_cObject);
1724 rb_define_singleton_method(cVimWindow, "current", window_s_current, 0);
1725 rb_define_singleton_method(cVimWindow, "count", window_s_count, 0);
1726 rb_define_singleton_method(cVimWindow, "[]", window_s_aref, 1);
1727 rb_define_method(cVimWindow, "buffer", window_buffer, 0);
1728 rb_define_method(cVimWindow, "height", window_height, 0);
1729 rb_define_method(cVimWindow, "height=", window_set_height, 1);
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001730 rb_define_method(cVimWindow, "width", window_width, 0);
1731 rb_define_method(cVimWindow, "width=", window_set_width, 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001732 rb_define_method(cVimWindow, "cursor", window_cursor, 0);
1733 rb_define_method(cVimWindow, "cursor=", window_set_cursor, 1);
1734
1735 rb_define_virtual_variable("$curbuf", buffer_s_current, 0);
1736 rb_define_virtual_variable("$curwin", window_s_current, 0);
1737}
Bram Moolenaar99685e62013-05-11 13:56:18 +02001738
1739void vim_ruby_init(void *stack_start)
1740{
1741 /* should get machine stack start address early in main function */
1742 ruby_stack_start = stack_start;
1743}