blob: 24814a2326dbac3d210cf52645ac7eadf84329b7 [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 Moolenaar2d73ff42010-10-27 17:40:59 +020067#if !(defined(WIN32) || defined(_WIN64))
Bram Moolenaarf9b5ef82010-09-29 13:02:53 +020068# include <dlfcn.h>
Bram Moolenaar3ca71f12010-10-27 16:49:47 +020069# define HINSTANCE void*
70# define RUBY_PROC void*
Bram Moolenaarf9b5ef82010-09-29 13:02:53 +020071# define load_dll(n) dlopen((n), RTLD_LAZY|RTLD_GLOBAL)
72# define symbol_from_dll dlsym
73# define close_dll dlclose
74#else
Bram Moolenaar3ca71f12010-10-27 16:49:47 +020075# define RUBY_PROC FARPROC
Bram Moolenaarebbcb822010-10-23 14:02:54 +020076# define load_dll vimLoadLib
Bram Moolenaarf9b5ef82010-09-29 13:02:53 +020077# define symbol_from_dll GetProcAddress
78# define close_dll FreeLibrary
Bram Moolenaar071d4272004-06-13 20:20:40 +000079#endif
80
Bram Moolenaarf9b5ef82010-09-29 13:02:53 +020081#endif /* ifdef DYNAMIC_RUBY */
82
Bram Moolenaar0b69c732010-02-17 15:11:50 +010083/* suggested by Ariya Mizutani */
84#if (_MSC_VER == 1200)
85# undef _WIN32_WINNT
86#endif
87
Bram Moolenaar639a2552010-03-17 18:15:23 +010088#if (defined(RUBY_VERSION) && RUBY_VERSION >= 19) \
89 || (defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 19)
90# define RUBY19_OR_LATER 1
91#endif
92
Bram Moolenaar0d27f642015-12-28 22:05:28 +010093#if (defined(RUBY_VERSION) && RUBY_VERSION >= 20) \
94 || (defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 20)
95# define RUBY20_OR_LATER 1
96#endif
97
Bram Moolenaarf711cb22018-08-01 18:42:13 +020098#if (defined(RUBY_VERSION) && RUBY_VERSION >= 21) \
99 || (defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 21)
100# define RUBY21_OR_LATER 1
101#endif
102
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100103#if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 19
104/* Ruby 1.9 defines a number of static functions which use rb_num2long and
105 * rb_int2big */
106# define rb_num2long rb_num2long_stub
107# define rb_int2big rb_int2big_stub
108#endif
109
Bram Moolenaar498af702014-03-28 21:58:21 +0100110#if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 19 \
111 && VIM_SIZEOF_INT < VIM_SIZEOF_LONG
112/* Ruby 1.9 defines a number of static functions which use rb_fix2int and
Bram Moolenaara2aa31a2014-02-23 22:52:40 +0100113 * rb_num2int if VIM_SIZEOF_INT < VIM_SIZEOF_LONG (64bit) */
Bram Moolenaar0bcdd6e2013-04-14 16:19:03 +0200114# define rb_fix2int rb_fix2int_stub
115# define rb_num2int rb_num2int_stub
116#endif
117
Bram Moolenaar0c7485f2015-01-14 14:04:10 +0100118#if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER == 21
Bram Moolenaara1a118b2014-02-05 22:41:15 +0100119/* Ruby 2.1 adds new GC called RGenGC and RARRAY_PTR uses
120 * rb_gc_writebarrier_unprotect_promoted if USE_RGENGC */
Bram Moolenaar90140742014-11-27 17:44:08 +0100121# define rb_gc_writebarrier_unprotect_promoted rb_gc_writebarrier_unprotect_promoted_stub
122#endif
Bram Moolenaar0c7485f2015-01-14 14:04:10 +0100123#if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 22
124# define rb_gc_writebarrier_unprotect rb_gc_writebarrier_unprotect_stub
Bram Moolenaar0c7485f2015-01-14 14:04:10 +0100125#endif
Bram Moolenaara1a118b2014-02-05 22:41:15 +0100126
Bram Moolenaarb09c6842018-12-27 22:11:01 +0100127#if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 26
Bram Moolenaarf62fc312019-01-08 20:29:32 +0100128# define rb_ary_detransient rb_ary_detransient_stub
Bram Moolenaarb09c6842018-12-27 22:11:01 +0100129#endif
130
Bram Moolenaar071d4272004-06-13 20:20:40 +0000131#include <ruby.h>
Bram Moolenaar639a2552010-03-17 18:15:23 +0100132#ifdef RUBY19_OR_LATER
Bram Moolenaar165641d2010-02-17 16:23:09 +0100133# include <ruby/encoding.h>
134#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000135
Bram Moolenaar7a8ef142010-12-24 13:39:35 +0100136#undef off_t /* ruby defines off_t as _int64, Mingw uses long */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000137#undef EXTERN
138#undef _
139
140/* T_DATA defined both by Ruby and Mac header files, hack around it... */
Bram Moolenaard0573012017-10-28 21:11:06 +0200141#if defined(MACOS_X)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000142# define __OPENTRANSPORT__
143# define __OPENTRANSPORTPROTOCOL__
144# define __OPENTRANSPORTPROVIDERS__
145#endif
146
Bram Moolenaar165641d2010-02-17 16:23:09 +0100147/*
Bram Moolenaar0d27f642015-12-28 22:05:28 +0100148 * The TypedData_XXX macro family can be used since Ruby 1.9.2 but
149 * rb_data_type_t changed in 1.9.3, therefore require at least 2.0.
150 * The old Data_XXX macro family was deprecated on Ruby 2.2.
Bram Moolenaarf2f6d292015-12-28 20:57:10 +0100151 * Use TypedData_XXX if available.
152 */
Bram Moolenaar0d27f642015-12-28 22:05:28 +0100153#if defined(TypedData_Wrap_Struct) && defined(RUBY20_OR_LATER)
Bram Moolenaarf2f6d292015-12-28 20:57:10 +0100154# define USE_TYPEDDATA 1
155#endif
156
157/*
Bram Moolenaar84a05ac2013-05-06 04:24:17 +0200158 * Backward compatibility for Ruby 1.8 and earlier.
Bram Moolenaar165641d2010-02-17 16:23:09 +0100159 * Ruby 1.9 does not provide STR2CSTR, instead StringValuePtr is provided.
160 * Ruby 1.9 does not provide RXXX(s)->len and RXXX(s)->ptr, instead
161 * RXXX_LEN(s) and RXXX_PTR(s) are provided.
162 */
163#ifndef StringValuePtr
164# define StringValuePtr(s) STR2CSTR(s)
165#endif
166#ifndef RARRAY_LEN
167# define RARRAY_LEN(s) RARRAY(s)->len
168#endif
169#ifndef RARRAY_PTR
170# define RARRAY_PTR(s) RARRAY(s)->ptr
171#endif
172#ifndef RSTRING_LEN
173# define RSTRING_LEN(s) RSTRING(s)->len
174#endif
175#ifndef RSTRING_PTR
176# define RSTRING_PTR(s) RSTRING(s)->ptr
177#endif
178
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100179#ifdef HAVE_DUP
180# undef HAVE_DUP
181#endif
182
Bram Moolenaar071d4272004-06-13 20:20:40 +0000183#include "vim.h"
184#include "version.h"
185
186#if defined(PROTO) && !defined(FEAT_RUBY)
187/* Define these to be able to generate the function prototypes. */
188# define VALUE int
189# define RUBY_DATA_FUNC int
190#endif
191
192static int ruby_initialized = 0;
Bram Moolenaar99685e62013-05-11 13:56:18 +0200193static void *ruby_stack_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000194static VALUE objtbl;
195
196static VALUE mVIM;
197static VALUE cBuffer;
198static VALUE cVimWindow;
199static VALUE eDeletedBufferError;
200static VALUE eDeletedWindowError;
201
202static int ensure_ruby_initialized(void);
203static void error_print(int);
204static void ruby_io_init(void);
205static void ruby_vim_init(void);
206
Bram Moolenaar10f3a792013-05-20 12:52:29 +0200207#if defined(RUBY19_OR_LATER) || defined(RUBY_INIT_STACK)
208# if defined(__ia64) && !defined(ruby_init_stack)
209# define ruby_init_stack(addr) ruby_init_stack((addr), rb_ia64_bsp())
210# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000211#endif
212
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200213#if defined(DYNAMIC_RUBY) || defined(PROTO)
Bram Moolenaaref269542016-01-19 13:22:12 +0100214# if defined(PROTO) && !defined(HINSTANCE)
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200215# define HINSTANCE int /* for generating prototypes */
216# endif
217
Bram Moolenaar071d4272004-06-13 20:20:40 +0000218/*
219 * Wrapper defines
220 */
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200221# define rb_assoc_new dll_rb_assoc_new
222# define rb_cObject (*dll_rb_cObject)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100223# define rb_class_new_instance dll_rb_class_new_instance
Bram Moolenaarf2f6d292015-12-28 20:57:10 +0100224# define rb_check_type dll_rb_check_type
225# ifdef USE_TYPEDDATA
226# define rb_check_typeddata dll_rb_check_typeddata
Bram Moolenaar0c7485f2015-01-14 14:04:10 +0100227# endif
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200228# define rb_class_path dll_rb_class_path
Bram Moolenaarf2f6d292015-12-28 20:57:10 +0100229# ifdef USE_TYPEDDATA
230# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 23
231# define rb_data_typed_object_wrap dll_rb_data_typed_object_wrap
232# else
233# define rb_data_typed_object_alloc dll_rb_data_typed_object_alloc
234# endif
235# else
236# define rb_data_object_alloc dll_rb_data_object_alloc
237# endif
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200238# define rb_define_class_under dll_rb_define_class_under
239# define rb_define_const dll_rb_define_const
240# define rb_define_global_function dll_rb_define_global_function
241# define rb_define_method dll_rb_define_method
242# define rb_define_module dll_rb_define_module
243# define rb_define_module_function dll_rb_define_module_function
244# define rb_define_singleton_method dll_rb_define_singleton_method
245# define rb_define_virtual_variable dll_rb_define_virtual_variable
246# define rb_stdout (*dll_rb_stdout)
Bram Moolenaarb6c8cd82018-07-24 05:41:30 +0200247# define rb_stderr (*dll_rb_stderr)
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200248# define rb_eArgError (*dll_rb_eArgError)
249# define rb_eIndexError (*dll_rb_eIndexError)
250# define rb_eRuntimeError (*dll_rb_eRuntimeError)
251# define rb_eStandardError (*dll_rb_eStandardError)
252# define rb_eval_string_protect dll_rb_eval_string_protect
Bram Moolenaarf711cb22018-08-01 18:42:13 +0200253# ifdef RUBY21_OR_LATER
254# define rb_funcallv dll_rb_funcallv
255# else
256# define rb_funcall2 dll_rb_funcall2
257# endif
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200258# define rb_global_variable dll_rb_global_variable
259# define rb_hash_aset dll_rb_hash_aset
260# define rb_hash_new dll_rb_hash_new
261# define rb_inspect dll_rb_inspect
262# define rb_int2inum dll_rb_int2inum
Bram Moolenaar218beb32018-08-04 17:24:44 +0200263
264// ruby.h may redefine rb_intern to use RUBY_CONST_ID_CACHE(), but that won't
265// work. Not using the cache appears to be the best solution.
266# undef rb_intern
267# define rb_intern dll_rb_intern
268
Bram Moolenaara2aa31a2014-02-23 22:52:40 +0100269# if VIM_SIZEOF_INT < VIM_SIZEOF_LONG /* 64 bits only */
Bram Moolenaar498af702014-03-28 21:58:21 +0100270# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER <= 18
271# define rb_fix2int dll_rb_fix2int
272# define rb_num2int dll_rb_num2int
273# endif
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200274# define rb_num2uint dll_rb_num2uint
275# endif
276# define rb_lastline_get dll_rb_lastline_get
277# define rb_lastline_set dll_rb_lastline_set
Bram Moolenaard0573012017-10-28 21:11:06 +0200278# define rb_protect dll_rb_protect
279# define rb_load dll_rb_load
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200280# ifndef RUBY19_OR_LATER
281# define rb_num2long dll_rb_num2long
282# endif
283# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER <= 19
284# define rb_num2ulong dll_rb_num2ulong
285# endif
286# define rb_obj_alloc dll_rb_obj_alloc
287# define rb_obj_as_string dll_rb_obj_as_string
288# define rb_obj_id dll_rb_obj_id
289# define rb_raise dll_rb_raise
290# define rb_str_cat dll_rb_str_cat
291# define rb_str_concat dll_rb_str_concat
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100292# undef rb_str_new
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200293# define rb_str_new dll_rb_str_new
294# ifdef rb_str_new2
Bram Moolenaar1d2beae2010-05-22 21:56:55 +0200295/* Ruby may #define rb_str_new2 to use rb_str_new_cstr. */
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200296# define need_rb_str_new_cstr 1
Bram Moolenaar3ca71f12010-10-27 16:49:47 +0200297/* Ruby's headers #define rb_str_new_cstr to make use of GCC's
298 * __builtin_constant_p extension. */
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200299# undef rb_str_new_cstr
300# define rb_str_new_cstr dll_rb_str_new_cstr
Bram Moolenaar76a86062013-05-11 17:45:48 +0200301# else
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200302# define rb_str_new2 dll_rb_str_new2
Bram Moolenaar76a86062013-05-11 17:45:48 +0200303# endif
Bram Moolenaar3ca71f12010-10-27 16:49:47 +0200304# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 18
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200305# define rb_string_value dll_rb_string_value
306# define rb_string_value_ptr dll_rb_string_value_ptr
307# define rb_float_new dll_rb_float_new
308# define rb_ary_new dll_rb_ary_new
Bram Moolenaar51e9fbf2018-08-11 14:24:11 +0200309# ifdef rb_ary_new4
310# define RB_ARY_NEW4_MACRO 1
311# undef rb_ary_new4
312# endif
313# define rb_ary_new4 dll_rb_ary_new4
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200314# define rb_ary_push dll_rb_ary_push
Bram Moolenaar10f3a792013-05-20 12:52:29 +0200315# if defined(RUBY19_OR_LATER) || defined(RUBY_INIT_STACK)
316# ifdef __ia64
317# define rb_ia64_bsp dll_rb_ia64_bsp
318# undef ruby_init_stack
319# define ruby_init_stack(addr) dll_ruby_init_stack((addr), rb_ia64_bsp())
320# else
321# define ruby_init_stack dll_ruby_init_stack
322# endif
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200323# endif
324# else
325# define rb_str2cstr dll_rb_str2cstr
Bram Moolenaar3ca71f12010-10-27 16:49:47 +0200326# endif
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200327# ifdef RUBY19_OR_LATER
328# define rb_errinfo dll_rb_errinfo
329# else
330# define ruby_errinfo (*dll_ruby_errinfo)
331# endif
332# define ruby_init dll_ruby_init
333# define ruby_init_loadpath dll_ruby_init_loadpath
334# ifdef WIN3264
Bram Moolenaar9d5c84a2018-12-19 20:48:46 +0100335# ifdef RUBY19_OR_LATER
336# define ruby_sysinit dll_ruby_sysinit
337# else
338# define NtInitialize dll_NtInitialize
339# endif
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200340# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 18
341# define rb_w32_snprintf dll_rb_w32_snprintf
342# endif
343# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000344
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200345# ifdef RUBY19_OR_LATER
346# define ruby_script dll_ruby_script
347# define rb_enc_find_index dll_rb_enc_find_index
348# define rb_enc_find dll_rb_enc_find
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100349# undef rb_enc_str_new
350# define rb_enc_str_new dll_rb_enc_str_new
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200351# define rb_sprintf dll_rb_sprintf
352# define rb_require dll_rb_require
Bram Moolenaar9b1067e2015-11-19 19:33:15 +0100353# define ruby_options dll_ruby_options
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200354# endif
Bram Moolenaar165641d2010-02-17 16:23:09 +0100355
Bram Moolenaar071d4272004-06-13 20:20:40 +0000356/*
357 * Pointers for dynamic link
358 */
359static VALUE (*dll_rb_assoc_new) (VALUE, VALUE);
Bram Moolenaarba52cde2010-06-25 04:29:11 +0200360VALUE *dll_rb_cFalseClass;
361VALUE *dll_rb_cFixnum;
Bram Moolenaar2016ae52016-06-13 20:08:43 +0200362# if defined(USE_RUBY_INTEGER)
Bram Moolenaar06469e92016-06-11 22:26:53 +0200363VALUE *dll_rb_cInteger;
364# endif
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200365# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 20
Bram Moolenaardb3fbe52013-03-07 15:16:21 +0100366VALUE *dll_rb_cFloat;
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200367# endif
Bram Moolenaarba52cde2010-06-25 04:29:11 +0200368VALUE *dll_rb_cNilClass;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000369static VALUE *dll_rb_cObject;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100370VALUE *dll_rb_cString;
Bram Moolenaarba52cde2010-06-25 04:29:11 +0200371VALUE *dll_rb_cSymbol;
372VALUE *dll_rb_cTrueClass;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100373static VALUE (*dll_rb_class_new_instance) (int,VALUE*,VALUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000374static void (*dll_rb_check_type) (VALUE,int);
Bram Moolenaarf2f6d292015-12-28 20:57:10 +0100375# ifdef USE_TYPEDDATA
376static void *(*dll_rb_check_typeddata) (VALUE,const rb_data_type_t *);
377# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000378static VALUE (*dll_rb_class_path) (VALUE);
Bram Moolenaarf2f6d292015-12-28 20:57:10 +0100379# ifdef USE_TYPEDDATA
380# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 23
381static VALUE (*dll_rb_data_typed_object_wrap) (VALUE, void*, const rb_data_type_t *);
382# else
383static VALUE (*dll_rb_data_typed_object_alloc) (VALUE, void*, const rb_data_type_t *);
384# endif
385# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000386static VALUE (*dll_rb_data_object_alloc) (VALUE, void*, RUBY_DATA_FUNC, RUBY_DATA_FUNC);
Bram Moolenaarf2f6d292015-12-28 20:57:10 +0100387# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000388static VALUE (*dll_rb_define_class_under) (VALUE, const char*, VALUE);
389static void (*dll_rb_define_const) (VALUE,const char*,VALUE);
390static void (*dll_rb_define_global_function) (const char*,VALUE(*)(),int);
391static void (*dll_rb_define_method) (VALUE,const char*,VALUE(*)(),int);
392static VALUE (*dll_rb_define_module) (const char*);
393static void (*dll_rb_define_module_function) (VALUE,const char*,VALUE(*)(),int);
394static void (*dll_rb_define_singleton_method) (VALUE,const char*,VALUE(*)(),int);
395static void (*dll_rb_define_virtual_variable) (const char*,VALUE(*)(),void(*)());
396static VALUE *dll_rb_stdout;
Bram Moolenaarb6c8cd82018-07-24 05:41:30 +0200397static VALUE *dll_rb_stderr;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000398static VALUE *dll_rb_eArgError;
399static VALUE *dll_rb_eIndexError;
400static VALUE *dll_rb_eRuntimeError;
401static VALUE *dll_rb_eStandardError;
402static VALUE (*dll_rb_eval_string_protect) (const char*, int*);
Bram Moolenaarf711cb22018-08-01 18:42:13 +0200403# ifdef RUBY21_OR_LATER
404static VALUE (*dll_rb_funcallv) (VALUE, ID, int, const VALUE*);
405# else
406static VALUE (*dll_rb_funcall2) (VALUE, ID, int, const VALUE*);
407# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000408static void (*dll_rb_global_variable) (VALUE*);
409static VALUE (*dll_rb_hash_aset) (VALUE, VALUE, VALUE);
410static VALUE (*dll_rb_hash_new) (void);
411static VALUE (*dll_rb_inspect) (VALUE);
412static VALUE (*dll_rb_int2inum) (long);
Bram Moolenaarf711cb22018-08-01 18:42:13 +0200413static ID (*dll_rb_intern) (const char*);
Bram Moolenaara2aa31a2014-02-23 22:52:40 +0100414# if VIM_SIZEOF_INT < VIM_SIZEOF_LONG /* 64 bits only */
Bram Moolenaar2623b4f2012-09-18 16:36:32 +0200415static long (*dll_rb_fix2int) (VALUE);
416static long (*dll_rb_num2int) (VALUE);
417static unsigned long (*dll_rb_num2uint) (VALUE);
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200418# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000419static VALUE (*dll_rb_lastline_get) (void);
420static void (*dll_rb_lastline_set) (VALUE);
Bram Moolenaar37badc82018-01-31 20:15:30 +0100421static VALUE (*dll_rb_protect) (VALUE (*)(VALUE), VALUE, int*);
Bram Moolenaard0573012017-10-28 21:11:06 +0200422static void (*dll_rb_load) (VALUE, int);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000423static long (*dll_rb_num2long) (VALUE);
424static unsigned long (*dll_rb_num2ulong) (VALUE);
425static VALUE (*dll_rb_obj_alloc) (VALUE);
426static VALUE (*dll_rb_obj_as_string) (VALUE);
427static VALUE (*dll_rb_obj_id) (VALUE);
428static void (*dll_rb_raise) (VALUE, const char*, ...);
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200429# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 18
Bram Moolenaarf9b5ef82010-09-29 13:02:53 +0200430static VALUE (*dll_rb_string_value) (volatile VALUE*);
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200431# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000432static char *(*dll_rb_str2cstr) (VALUE,int*);
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200433# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000434static VALUE (*dll_rb_str_cat) (VALUE, const char*, long);
435static VALUE (*dll_rb_str_concat) (VALUE, VALUE);
436static VALUE (*dll_rb_str_new) (const char*, long);
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200437# ifdef need_rb_str_new_cstr
Bram Moolenaar1d2beae2010-05-22 21:56:55 +0200438/* Ruby may #define rb_str_new2 to use rb_str_new_cstr. */
439static VALUE (*dll_rb_str_new_cstr) (const char*);
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200440# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000441static VALUE (*dll_rb_str_new2) (const char*);
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200442# endif
443# ifdef RUBY19_OR_LATER
Bram Moolenaar165641d2010-02-17 16:23:09 +0100444static VALUE (*dll_rb_errinfo) (void);
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200445# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000446static VALUE *dll_ruby_errinfo;
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200447# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000448static void (*dll_ruby_init) (void);
449static void (*dll_ruby_init_loadpath) (void);
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200450# ifdef WIN3264
Bram Moolenaar9d5c84a2018-12-19 20:48:46 +0100451# ifdef RUBY19_OR_LATER
Bram Moolenaar4f391792017-01-15 16:59:07 +0100452static void (*dll_ruby_sysinit) (int*, char***);
Bram Moolenaar9d5c84a2018-12-19 20:48:46 +0100453# else
454static void (*dll_NtInitialize) (int*, char***);
455# endif
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200456# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 18
Bram Moolenaar3ca71f12010-10-27 16:49:47 +0200457static int (*dll_rb_w32_snprintf)(char*, size_t, const char*, ...);
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200458# endif
Bram Moolenaar3ca71f12010-10-27 16:49:47 +0200459# endif
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200460# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 18
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100461static char * (*dll_rb_string_value_ptr) (volatile VALUE*);
462static VALUE (*dll_rb_float_new) (double);
463static VALUE (*dll_rb_ary_new) (void);
Bram Moolenaar51e9fbf2018-08-11 14:24:11 +0200464static VALUE (*dll_rb_ary_new4) (long n, const VALUE *elts);
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100465static VALUE (*dll_rb_ary_push) (VALUE, VALUE);
Bram Moolenaarb09c6842018-12-27 22:11:01 +0100466# if DYNAMIC_RUBY_VER >= 26
467static void (*dll_rb_ary_detransient) (VALUE);
468# endif
Bram Moolenaar10f3a792013-05-20 12:52:29 +0200469# if defined(RUBY19_OR_LATER) || defined(RUBY_INIT_STACK)
470# ifdef __ia64
Bram Moolenaar76a86062013-05-11 17:45:48 +0200471static void * (*dll_rb_ia64_bsp) (void);
472static void (*dll_ruby_init_stack)(VALUE*, void*);
Bram Moolenaar10f3a792013-05-20 12:52:29 +0200473# else
Bram Moolenaar76a86062013-05-11 17:45:48 +0200474static void (*dll_ruby_init_stack)(VALUE*);
Bram Moolenaar10f3a792013-05-20 12:52:29 +0200475# endif
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200476# endif
Bram Moolenaar76a86062013-05-11 17:45:48 +0200477# endif
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200478# ifdef RUBY19_OR_LATER
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100479static VALUE (*dll_rb_int2big)(SIGNED_VALUE);
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200480# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000481
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200482# ifdef RUBY19_OR_LATER
Bram Moolenaar165641d2010-02-17 16:23:09 +0100483static void (*dll_ruby_script) (const char*);
484static int (*dll_rb_enc_find_index) (const char*);
485static rb_encoding* (*dll_rb_enc_find) (const char*);
486static VALUE (*dll_rb_enc_str_new) (const char*, long, rb_encoding*);
487static VALUE (*dll_rb_sprintf) (const char*, ...);
Bram Moolenaar7a8ef142010-12-24 13:39:35 +0100488static VALUE (*dll_rb_require) (const char*);
Bram Moolenaar9b1067e2015-11-19 19:33:15 +0100489static void* (*ruby_options)(int, char**);
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200490# endif
Bram Moolenaar165641d2010-02-17 16:23:09 +0100491
Bram Moolenaara1a118b2014-02-05 22:41:15 +0100492# if defined(USE_RGENGC) && USE_RGENGC
Bram Moolenaar0c7485f2015-01-14 14:04:10 +0100493# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER == 21
Bram Moolenaara1a118b2014-02-05 22:41:15 +0100494static void (*dll_rb_gc_writebarrier_unprotect_promoted)(VALUE);
Bram Moolenaar0c7485f2015-01-14 14:04:10 +0100495# else
496static void (*dll_rb_gc_writebarrier_unprotect)(VALUE obj);
497# endif
Bram Moolenaara1a118b2014-02-05 22:41:15 +0100498# endif
499
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200500# if defined(RUBY19_OR_LATER) && !defined(PROTO)
Bram Moolenaarbbc1a592015-04-21 15:25:31 +0200501# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 22
502long rb_num2long_stub(VALUE x)
503# else
Bram Moolenaarff8cf2b2012-11-24 13:39:00 +0100504SIGNED_VALUE rb_num2long_stub(VALUE x)
Bram Moolenaarbbc1a592015-04-21 15:25:31 +0200505# endif
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100506{
507 return dll_rb_num2long(x);
508}
Bram Moolenaar63d1fea2019-02-03 15:18:35 +0100509# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 26
510VALUE rb_int2big_stub(intptr_t x)
511# else
Bram Moolenaarff8cf2b2012-11-24 13:39:00 +0100512VALUE rb_int2big_stub(SIGNED_VALUE x)
Bram Moolenaar63d1fea2019-02-03 15:18:35 +0100513# endif
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100514{
515 return dll_rb_int2big(x);
516}
Bram Moolenaar498af702014-03-28 21:58:21 +0100517# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 19 \
518 && VIM_SIZEOF_INT < VIM_SIZEOF_LONG
Bram Moolenaar0bcdd6e2013-04-14 16:19:03 +0200519long rb_fix2int_stub(VALUE x)
520{
521 return dll_rb_fix2int(x);
522}
523long rb_num2int_stub(VALUE x)
524{
525 return dll_rb_num2int(x);
526}
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200527# endif
528# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 20
Bram Moolenaar886ed692013-02-26 13:41:35 +0100529VALUE
530rb_float_new_in_heap(double d)
531{
532 return dll_rb_float_new(d);
533}
Bram Moolenaarbbc1a592015-04-21 15:25:31 +0200534# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 22
535unsigned long rb_num2ulong(VALUE x)
536# else
Bram Moolenaardb3fbe52013-03-07 15:16:21 +0100537VALUE rb_num2ulong(VALUE x)
Bram Moolenaarbbc1a592015-04-21 15:25:31 +0200538# endif
Bram Moolenaar886ed692013-02-26 13:41:35 +0100539{
540 return (long)RSHIFT((SIGNED_VALUE)(x),1);
541}
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200542# endif
543# endif
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100544
Bram Moolenaar3e9a1612014-11-12 16:05:04 +0100545 /* Do not generate a prototype here, VALUE isn't always defined. */
546# if defined(USE_RGENGC) && USE_RGENGC && !defined(PROTO)
Bram Moolenaar0c7485f2015-01-14 14:04:10 +0100547# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER == 21
Bram Moolenaara1a118b2014-02-05 22:41:15 +0100548void rb_gc_writebarrier_unprotect_promoted_stub(VALUE obj)
549{
Bram Moolenaar90140742014-11-27 17:44:08 +0100550 dll_rb_gc_writebarrier_unprotect_promoted(obj);
Bram Moolenaara1a118b2014-02-05 22:41:15 +0100551}
Bram Moolenaar0c7485f2015-01-14 14:04:10 +0100552# else
553void rb_gc_writebarrier_unprotect_stub(VALUE obj)
554{
555 dll_rb_gc_writebarrier_unprotect(obj);
556}
557# endif
558# endif
559
Bram Moolenaarf62fc312019-01-08 20:29:32 +0100560# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 26
561void rb_ary_detransient_stub(VALUE x)
562{
563 dll_rb_ary_detransient(x);
564}
565# endif
566
Bram Moolenaar3ca71f12010-10-27 16:49:47 +0200567static HINSTANCE hinstRuby = NULL; /* Instance of ruby.dll */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000568
569/*
Bram Moolenaarda2303d2005-08-30 21:55:26 +0000570 * Table of name to function pointer of ruby.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000571 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000572static struct
573{
574 char *name;
575 RUBY_PROC *ptr;
576} ruby_funcname_table[] =
577{
578 {"rb_assoc_new", (RUBY_PROC*)&dll_rb_assoc_new},
579 {"rb_cFalseClass", (RUBY_PROC*)&dll_rb_cFalseClass},
Bram Moolenaar2016ae52016-06-13 20:08:43 +0200580# if defined(USE_RUBY_INTEGER)
Bram Moolenaar06469e92016-06-11 22:26:53 +0200581 {"rb_cInteger", (RUBY_PROC*)&dll_rb_cInteger},
Bram Moolenaar6abda992017-01-09 21:10:31 +0100582# else
583 {"rb_cFixnum", (RUBY_PROC*)&dll_rb_cFixnum},
Bram Moolenaar06469e92016-06-11 22:26:53 +0200584# endif
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200585# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 20
Bram Moolenaardb3fbe52013-03-07 15:16:21 +0100586 {"rb_cFloat", (RUBY_PROC*)&dll_rb_cFloat},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200587# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000588 {"rb_cNilClass", (RUBY_PROC*)&dll_rb_cNilClass},
589 {"rb_cObject", (RUBY_PROC*)&dll_rb_cObject},
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100590 {"rb_cString", (RUBY_PROC*)&dll_rb_cString},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000591 {"rb_cSymbol", (RUBY_PROC*)&dll_rb_cSymbol},
592 {"rb_cTrueClass", (RUBY_PROC*)&dll_rb_cTrueClass},
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100593 {"rb_class_new_instance", (RUBY_PROC*)&dll_rb_class_new_instance},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000594 {"rb_check_type", (RUBY_PROC*)&dll_rb_check_type},
Bram Moolenaarf2f6d292015-12-28 20:57:10 +0100595# ifdef USE_TYPEDDATA
596 {"rb_check_typeddata", (RUBY_PROC*)&dll_rb_check_typeddata},
597# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000598 {"rb_class_path", (RUBY_PROC*)&dll_rb_class_path},
Bram Moolenaarf2f6d292015-12-28 20:57:10 +0100599# ifdef USE_TYPEDDATA
600# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 23
601 {"rb_data_typed_object_wrap", (RUBY_PROC*)&dll_rb_data_typed_object_wrap},
602# else
603 {"rb_data_typed_object_alloc", (RUBY_PROC*)&dll_rb_data_typed_object_alloc},
604# endif
605# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000606 {"rb_data_object_alloc", (RUBY_PROC*)&dll_rb_data_object_alloc},
Bram Moolenaarf2f6d292015-12-28 20:57:10 +0100607# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000608 {"rb_define_class_under", (RUBY_PROC*)&dll_rb_define_class_under},
609 {"rb_define_const", (RUBY_PROC*)&dll_rb_define_const},
610 {"rb_define_global_function", (RUBY_PROC*)&dll_rb_define_global_function},
611 {"rb_define_method", (RUBY_PROC*)&dll_rb_define_method},
612 {"rb_define_module", (RUBY_PROC*)&dll_rb_define_module},
613 {"rb_define_module_function", (RUBY_PROC*)&dll_rb_define_module_function},
614 {"rb_define_singleton_method", (RUBY_PROC*)&dll_rb_define_singleton_method},
615 {"rb_define_virtual_variable", (RUBY_PROC*)&dll_rb_define_virtual_variable},
616 {"rb_stdout", (RUBY_PROC*)&dll_rb_stdout},
Bram Moolenaarb6c8cd82018-07-24 05:41:30 +0200617 {"rb_stderr", (RUBY_PROC*)&dll_rb_stderr},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000618 {"rb_eArgError", (RUBY_PROC*)&dll_rb_eArgError},
619 {"rb_eIndexError", (RUBY_PROC*)&dll_rb_eIndexError},
620 {"rb_eRuntimeError", (RUBY_PROC*)&dll_rb_eRuntimeError},
621 {"rb_eStandardError", (RUBY_PROC*)&dll_rb_eStandardError},
622 {"rb_eval_string_protect", (RUBY_PROC*)&dll_rb_eval_string_protect},
Bram Moolenaarf711cb22018-08-01 18:42:13 +0200623# ifdef RUBY21_OR_LATER
624 {"rb_funcallv", (RUBY_PROC*)&dll_rb_funcallv},
625# else
626 {"rb_funcall2", (RUBY_PROC*)&dll_rb_funcall2},
627# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000628 {"rb_global_variable", (RUBY_PROC*)&dll_rb_global_variable},
629 {"rb_hash_aset", (RUBY_PROC*)&dll_rb_hash_aset},
630 {"rb_hash_new", (RUBY_PROC*)&dll_rb_hash_new},
631 {"rb_inspect", (RUBY_PROC*)&dll_rb_inspect},
632 {"rb_int2inum", (RUBY_PROC*)&dll_rb_int2inum},
Bram Moolenaarf711cb22018-08-01 18:42:13 +0200633 {"rb_intern", (RUBY_PROC*)&dll_rb_intern},
Bram Moolenaara2aa31a2014-02-23 22:52:40 +0100634# if VIM_SIZEOF_INT < VIM_SIZEOF_LONG /* 64 bits only */
Bram Moolenaar2623b4f2012-09-18 16:36:32 +0200635 {"rb_fix2int", (RUBY_PROC*)&dll_rb_fix2int},
636 {"rb_num2int", (RUBY_PROC*)&dll_rb_num2int},
637 {"rb_num2uint", (RUBY_PROC*)&dll_rb_num2uint},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200638# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000639 {"rb_lastline_get", (RUBY_PROC*)&dll_rb_lastline_get},
640 {"rb_lastline_set", (RUBY_PROC*)&dll_rb_lastline_set},
Bram Moolenaard0573012017-10-28 21:11:06 +0200641 {"rb_protect", (RUBY_PROC*)&dll_rb_protect},
642 {"rb_load", (RUBY_PROC*)&dll_rb_load},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000643 {"rb_num2long", (RUBY_PROC*)&dll_rb_num2long},
644 {"rb_num2ulong", (RUBY_PROC*)&dll_rb_num2ulong},
645 {"rb_obj_alloc", (RUBY_PROC*)&dll_rb_obj_alloc},
646 {"rb_obj_as_string", (RUBY_PROC*)&dll_rb_obj_as_string},
647 {"rb_obj_id", (RUBY_PROC*)&dll_rb_obj_id},
648 {"rb_raise", (RUBY_PROC*)&dll_rb_raise},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200649# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 18
Bram Moolenaarf9b5ef82010-09-29 13:02:53 +0200650 {"rb_string_value", (RUBY_PROC*)&dll_rb_string_value},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200651# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000652 {"rb_str2cstr", (RUBY_PROC*)&dll_rb_str2cstr},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200653# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000654 {"rb_str_cat", (RUBY_PROC*)&dll_rb_str_cat},
655 {"rb_str_concat", (RUBY_PROC*)&dll_rb_str_concat},
656 {"rb_str_new", (RUBY_PROC*)&dll_rb_str_new},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200657# ifdef need_rb_str_new_cstr
Bram Moolenaar1d2beae2010-05-22 21:56:55 +0200658 {"rb_str_new_cstr", (RUBY_PROC*)&dll_rb_str_new_cstr},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200659# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000660 {"rb_str_new2", (RUBY_PROC*)&dll_rb_str_new2},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200661# endif
662# ifdef RUBY19_OR_LATER
Bram Moolenaar165641d2010-02-17 16:23:09 +0100663 {"rb_errinfo", (RUBY_PROC*)&dll_rb_errinfo},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200664# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000665 {"ruby_errinfo", (RUBY_PROC*)&dll_ruby_errinfo},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200666# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000667 {"ruby_init", (RUBY_PROC*)&dll_ruby_init},
668 {"ruby_init_loadpath", (RUBY_PROC*)&dll_ruby_init_loadpath},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200669# ifdef WIN3264
Bram Moolenaar9d5c84a2018-12-19 20:48:46 +0100670# ifdef RUBY19_OR_LATER
Bram Moolenaar4f391792017-01-15 16:59:07 +0100671 {"ruby_sysinit", (RUBY_PROC*)&dll_ruby_sysinit},
Bram Moolenaar9d5c84a2018-12-19 20:48:46 +0100672# else
673 {"NtInitialize", (RUBY_PROC*)&dll_NtInitialize},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200674# endif
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200675# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 18
Bram Moolenaar071d4272004-06-13 20:20:40 +0000676 {"rb_w32_snprintf", (RUBY_PROC*)&dll_rb_w32_snprintf},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200677# endif
Bram Moolenaar3ca71f12010-10-27 16:49:47 +0200678# endif
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200679# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 18
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100680 {"rb_string_value_ptr", (RUBY_PROC*)&dll_rb_string_value_ptr},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200681# if DYNAMIC_RUBY_VER <= 19
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100682 {"rb_float_new", (RUBY_PROC*)&dll_rb_float_new},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200683# else
Bram Moolenaar886ed692013-02-26 13:41:35 +0100684 {"rb_float_new_in_heap", (RUBY_PROC*)&dll_rb_float_new},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200685# endif
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100686 {"rb_ary_new", (RUBY_PROC*)&dll_rb_ary_new},
Bram Moolenaar51e9fbf2018-08-11 14:24:11 +0200687# ifdef RB_ARY_NEW4_MACRO
688 {"rb_ary_new_from_values", (RUBY_PROC*)&dll_rb_ary_new4},
689# else
690 {"rb_ary_new4", (RUBY_PROC*)&dll_rb_ary_new4},
691# endif
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100692 {"rb_ary_push", (RUBY_PROC*)&dll_rb_ary_push},
Bram Moolenaarb09c6842018-12-27 22:11:01 +0100693# if DYNAMIC_RUBY_VER >= 26
694 {"rb_ary_detransient", (RUBY_PROC*)&dll_rb_ary_detransient},
695# endif
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200696# endif
697# ifdef RUBY19_OR_LATER
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100698 {"rb_int2big", (RUBY_PROC*)&dll_rb_int2big},
Bram Moolenaar165641d2010-02-17 16:23:09 +0100699 {"ruby_script", (RUBY_PROC*)&dll_ruby_script},
700 {"rb_enc_find_index", (RUBY_PROC*)&dll_rb_enc_find_index},
701 {"rb_enc_find", (RUBY_PROC*)&dll_rb_enc_find},
702 {"rb_enc_str_new", (RUBY_PROC*)&dll_rb_enc_str_new},
703 {"rb_sprintf", (RUBY_PROC*)&dll_rb_sprintf},
Bram Moolenaar7a8ef142010-12-24 13:39:35 +0100704 {"rb_require", (RUBY_PROC*)&dll_rb_require},
Bram Moolenaar9b1067e2015-11-19 19:33:15 +0100705 {"ruby_options", (RUBY_PROC*)&dll_ruby_options},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200706# endif
Bram Moolenaar10f3a792013-05-20 12:52:29 +0200707# if defined(RUBY19_OR_LATER) || defined(RUBY_INIT_STACK)
708# ifdef __ia64
709 {"rb_ia64_bsp", (RUBY_PROC*)&dll_rb_ia64_bsp},
710# endif
711 {"ruby_init_stack", (RUBY_PROC*)&dll_ruby_init_stack},
712# endif
Bram Moolenaara1a118b2014-02-05 22:41:15 +0100713# if defined(USE_RGENGC) && USE_RGENGC
Bram Moolenaar0c7485f2015-01-14 14:04:10 +0100714# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER == 21
Bram Moolenaara1a118b2014-02-05 22:41:15 +0100715 {"rb_gc_writebarrier_unprotect_promoted", (RUBY_PROC*)&dll_rb_gc_writebarrier_unprotect_promoted},
Bram Moolenaar0c7485f2015-01-14 14:04:10 +0100716# else
717 {"rb_gc_writebarrier_unprotect", (RUBY_PROC*)&dll_rb_gc_writebarrier_unprotect},
718# endif
Bram Moolenaara1a118b2014-02-05 22:41:15 +0100719# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000720 {"", NULL},
721};
722
723/*
724 * Free ruby.dll
725 */
726 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100727end_dynamic_ruby(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000728{
729 if (hinstRuby)
730 {
Bram Moolenaarf9b5ef82010-09-29 13:02:53 +0200731 close_dll(hinstRuby);
Bram Moolenaar3ca71f12010-10-27 16:49:47 +0200732 hinstRuby = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000733 }
734}
735
736/*
737 * Load library and get all pointers.
738 * Parameter 'libname' provides name of DLL.
739 * Return OK or FAIL.
740 */
741 static int
742ruby_runtime_link_init(char *libname, int verbose)
743{
744 int i;
745
746 if (hinstRuby)
747 return OK;
Bram Moolenaarf9b5ef82010-09-29 13:02:53 +0200748 hinstRuby = load_dll(libname);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000749 if (!hinstRuby)
750 {
751 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100752 semsg(_(e_loadlib), libname);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000753 return FAIL;
754 }
755
756 for (i = 0; ruby_funcname_table[i].ptr; ++i)
757 {
Bram Moolenaarf9b5ef82010-09-29 13:02:53 +0200758 if (!(*ruby_funcname_table[i].ptr = symbol_from_dll(hinstRuby,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000759 ruby_funcname_table[i].name)))
760 {
Bram Moolenaarf9b5ef82010-09-29 13:02:53 +0200761 close_dll(hinstRuby);
Bram Moolenaar3ca71f12010-10-27 16:49:47 +0200762 hinstRuby = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000763 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100764 semsg(_(e_loadfunc), ruby_funcname_table[i].name);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000765 return FAIL;
766 }
767 }
768 return OK;
769}
770
771/*
772 * If ruby is enabled (there is installed ruby on Windows system) return TRUE,
773 * else FALSE.
774 */
775 int
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100776ruby_enabled(int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000777{
Bram Moolenaar25e4fcd2016-01-09 14:57:47 +0100778 return ruby_runtime_link_init((char *)p_rubydll, verbose) == OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000779}
780#endif /* defined(DYNAMIC_RUBY) || defined(PROTO) */
781
782 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100783ruby_end(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000784{
785#ifdef DYNAMIC_RUBY
786 end_dynamic_ruby();
787#endif
788}
789
790void ex_ruby(exarg_T *eap)
791{
792 int state;
793 char *script = NULL;
794
Bram Moolenaar35a2e192006-03-13 22:07:11 +0000795 script = (char *)script_get(eap, eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000796 if (!eap->skip && ensure_ruby_initialized())
797 {
798 if (script == NULL)
799 rb_eval_string_protect((char *)eap->arg, &state);
800 else
801 rb_eval_string_protect(script, &state);
802 if (state)
803 error_print(state);
804 }
805 vim_free(script);
806}
807
Bram Moolenaar165641d2010-02-17 16:23:09 +0100808/*
809 * In Ruby 1.9 or later, ruby String object has encoding.
810 * conversion buffer string of vim to ruby String object using
811 * VIM encoding option.
812 */
813 static VALUE
814vim_str2rb_enc_str(const char *s)
815{
Bram Moolenaar639a2552010-03-17 18:15:23 +0100816#ifdef RUBY19_OR_LATER
Bram Moolenaar165641d2010-02-17 16:23:09 +0100817 int isnum;
818 long lval;
819 char_u *sval;
820 rb_encoding *enc;
821
822 isnum = get_option_value((char_u *)"enc", &lval, &sval, 0);
823 if (isnum == 0)
824 {
825 enc = rb_enc_find((char *)sval);
826 vim_free(sval);
Bram Moolenaar758535a2016-03-30 22:06:16 +0200827 if (enc)
828 {
Bram Moolenaar9b0ac222016-06-01 20:31:43 +0200829 return rb_enc_str_new(s, (long)strlen(s), enc);
Bram Moolenaar165641d2010-02-17 16:23:09 +0100830 }
831 }
832#endif
833 return rb_str_new2(s);
834}
835
836 static VALUE
837eval_enc_string_protect(const char *str, int *state)
838{
Bram Moolenaar639a2552010-03-17 18:15:23 +0100839#ifdef RUBY19_OR_LATER
Bram Moolenaar165641d2010-02-17 16:23:09 +0100840 int isnum;
841 long lval;
842 char_u *sval;
843 rb_encoding *enc;
844 VALUE v;
845
846 isnum = get_option_value((char_u *)"enc", &lval, &sval, 0);
847 if (isnum == 0)
848 {
849 enc = rb_enc_find((char *)sval);
850 vim_free(sval);
851 if (enc)
852 {
853 v = rb_sprintf("#-*- coding:%s -*-\n%s", rb_enc_name(enc), str);
854 return rb_eval_string_protect(StringValuePtr(v), state);
855 }
856 }
857#endif
858 return rb_eval_string_protect(str, state);
859}
860
Bram Moolenaar071d4272004-06-13 20:20:40 +0000861void ex_rubydo(exarg_T *eap)
862{
863 int state;
864 linenr_T i;
Bram Moolenaarc593fee2017-01-29 23:11:25 +0100865 buf_T *was_curbuf = curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000866
867 if (ensure_ruby_initialized())
868 {
869 if (u_save(eap->line1 - 1, eap->line2 + 1) != OK)
870 return;
Bram Moolenaar758535a2016-03-30 22:06:16 +0200871 for (i = eap->line1; i <= eap->line2; i++)
872 {
Bram Moolenaare980d8a2010-12-08 13:11:21 +0100873 VALUE line;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000874
Bram Moolenaarc593fee2017-01-29 23:11:25 +0100875 if (i > curbuf->b_ml.ml_line_count)
876 break;
Bram Moolenaare980d8a2010-12-08 13:11:21 +0100877 line = vim_str2rb_enc_str((char *)ml_get(i));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000878 rb_lastline_set(line);
Bram Moolenaar165641d2010-02-17 16:23:09 +0100879 eval_enc_string_protect((char *) eap->arg, &state);
Bram Moolenaar758535a2016-03-30 22:06:16 +0200880 if (state)
881 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000882 error_print(state);
883 break;
884 }
Bram Moolenaarc593fee2017-01-29 23:11:25 +0100885 if (was_curbuf != curbuf)
886 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000887 line = rb_lastline_get();
Bram Moolenaar758535a2016-03-30 22:06:16 +0200888 if (!NIL_P(line))
889 {
890 if (TYPE(line) != T_STRING)
891 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100892 emsg(_("E265: $_ must be an instance of String"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000893 return;
894 }
Bram Moolenaar165641d2010-02-17 16:23:09 +0100895 ml_replace(i, (char_u *) StringValuePtr(line), 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000896 changed();
897#ifdef SYNTAX_HL
898 syn_changed(i); /* recompute syntax hl. for this line */
899#endif
900 }
901 }
902 check_cursor();
903 update_curbuf(NOT_VALID);
904 }
905}
906
Bram Moolenaar0b394642018-05-17 13:11:46 +0200907static VALUE rb_load_wrap(VALUE file_to_load)
Bram Moolenaar37badc82018-01-31 20:15:30 +0100908{
909 rb_load(file_to_load, 0);
910 return Qnil;
911}
912
Bram Moolenaar071d4272004-06-13 20:20:40 +0000913void ex_rubyfile(exarg_T *eap)
914{
915 int state;
916
917 if (ensure_ruby_initialized())
918 {
Bram Moolenaar37badc82018-01-31 20:15:30 +0100919 VALUE file_to_load = rb_str_new2((const char *)eap->arg);
920 rb_protect(rb_load_wrap, file_to_load, &state);
921 if (state)
922 error_print(state);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000923 }
924}
925
926void ruby_buffer_free(buf_T *buf)
927{
Bram Moolenaare344bea2005-09-01 20:46:49 +0000928 if (buf->b_ruby_ref)
929 {
930 rb_hash_aset(objtbl, rb_obj_id((VALUE) buf->b_ruby_ref), Qnil);
931 RDATA(buf->b_ruby_ref)->data = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000932 }
933}
934
935void ruby_window_free(win_T *win)
936{
Bram Moolenaare344bea2005-09-01 20:46:49 +0000937 if (win->w_ruby_ref)
938 {
939 rb_hash_aset(objtbl, rb_obj_id((VALUE) win->w_ruby_ref), Qnil);
940 RDATA(win->w_ruby_ref)->data = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000941 }
942}
943
944static int ensure_ruby_initialized(void)
945{
946 if (!ruby_initialized)
947 {
948#ifdef DYNAMIC_RUBY
949 if (ruby_enabled(TRUE))
950 {
951#endif
Bram Moolenaar0b69c732010-02-17 15:11:50 +0100952#ifdef _WIN32
953 /* suggested by Ariya Mizutani */
954 int argc = 1;
955 char *argv[] = {"gvim.exe"};
Bram Moolenaar90140742014-11-27 17:44:08 +0100956 char **argvp = argv;
Bram Moolenaarfe6ce332017-01-14 20:12:01 +0100957# ifdef RUBY19_OR_LATER
958 ruby_sysinit(&argc, &argvp);
959# else
Bram Moolenaar90140742014-11-27 17:44:08 +0100960 NtInitialize(&argc, &argvp);
Bram Moolenaarfe6ce332017-01-14 20:12:01 +0100961# endif
Bram Moolenaar0b69c732010-02-17 15:11:50 +0100962#endif
Bram Moolenaarb2c03502010-07-02 20:20:09 +0200963 {
Bram Moolenaar10f3a792013-05-20 12:52:29 +0200964#if defined(RUBY19_OR_LATER) || defined(RUBY_INIT_STACK)
Bram Moolenaar99685e62013-05-11 13:56:18 +0200965 ruby_init_stack(ruby_stack_start);
Bram Moolenaar165641d2010-02-17 16:23:09 +0100966#endif
Bram Moolenaarb2c03502010-07-02 20:20:09 +0200967 ruby_init();
968 }
Bram Moolenaar639a2552010-03-17 18:15:23 +0100969#ifdef RUBY19_OR_LATER
Bram Moolenaar7a8ef142010-12-24 13:39:35 +0100970 {
971 int dummy_argc = 2;
Bram Moolenaard1bc96ce2017-09-26 21:21:44 +0200972 char *dummy_argv[] = {"vim-ruby", "-e_=0"};
Bram Moolenaar9b1067e2015-11-19 19:33:15 +0100973 ruby_options(dummy_argc, dummy_argv);
Bram Moolenaar7a8ef142010-12-24 13:39:35 +0100974 }
Bram Moolenaar165641d2010-02-17 16:23:09 +0100975 ruby_script("vim-ruby");
Bram Moolenaar7a8ef142010-12-24 13:39:35 +0100976#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000977 ruby_init_loadpath();
Bram Moolenaar165641d2010-02-17 16:23:09 +0100978#endif
Bram Moolenaar7a8ef142010-12-24 13:39:35 +0100979 ruby_io_init();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000980 ruby_vim_init();
981 ruby_initialized = 1;
982#ifdef DYNAMIC_RUBY
983 }
984 else
985 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100986 emsg(_("E266: Sorry, this command is disabled, the Ruby library could not be loaded."));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000987 return 0;
988 }
989#endif
990 }
991 return ruby_initialized;
992}
993
994static void error_print(int state)
995{
Bram Moolenaarb09c6842018-12-27 22:11:01 +0100996#if !defined(DYNAMIC_RUBY) && !defined(RUBY19_OR_LATER)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000997 RUBYEXTERN VALUE ruby_errinfo;
998#endif
Bram Moolenaarf711cb22018-08-01 18:42:13 +0200999 VALUE error;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001000 VALUE eclass;
1001 VALUE einfo;
Bram Moolenaarf711cb22018-08-01 18:42:13 +02001002 VALUE bt;
1003 int attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001004 char buff[BUFSIZ];
Bram Moolenaarf711cb22018-08-01 18:42:13 +02001005 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001006
1007#define TAG_RETURN 0x1
1008#define TAG_BREAK 0x2
1009#define TAG_NEXT 0x3
1010#define TAG_RETRY 0x4
1011#define TAG_REDO 0x5
1012#define TAG_RAISE 0x6
1013#define TAG_THROW 0x7
1014#define TAG_FATAL 0x8
1015#define TAG_MASK 0xf
1016
Bram Moolenaar758535a2016-03-30 22:06:16 +02001017 switch (state)
1018 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001019 case TAG_RETURN:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001020 emsg(_("E267: unexpected return"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001021 break;
1022 case TAG_NEXT:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001023 emsg(_("E268: unexpected next"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001024 break;
1025 case TAG_BREAK:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001026 emsg(_("E269: unexpected break"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001027 break;
1028 case TAG_REDO:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001029 emsg(_("E270: unexpected redo"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001030 break;
1031 case TAG_RETRY:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001032 emsg(_("E271: retry outside of rescue clause"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001033 break;
1034 case TAG_RAISE:
1035 case TAG_FATAL:
Bram Moolenaar639a2552010-03-17 18:15:23 +01001036#ifdef RUBY19_OR_LATER
Bram Moolenaarf711cb22018-08-01 18:42:13 +02001037 error = rb_errinfo();
Bram Moolenaar165641d2010-02-17 16:23:09 +01001038#else
Bram Moolenaarf711cb22018-08-01 18:42:13 +02001039 error = ruby_errinfo;
Bram Moolenaar165641d2010-02-17 16:23:09 +01001040#endif
Bram Moolenaarf711cb22018-08-01 18:42:13 +02001041 eclass = CLASS_OF(error);
1042 einfo = rb_obj_as_string(error);
Bram Moolenaar758535a2016-03-30 22:06:16 +02001043 if (eclass == rb_eRuntimeError && RSTRING_LEN(einfo) == 0)
1044 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001045 emsg(_("E272: unhandled exception"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001046 }
Bram Moolenaar758535a2016-03-30 22:06:16 +02001047 else
1048 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001049 VALUE epath;
1050 char *p;
1051
1052 epath = rb_class_path(eclass);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00001053 vim_snprintf(buff, BUFSIZ, "%s: %s",
Bram Moolenaar165641d2010-02-17 16:23:09 +01001054 RSTRING_PTR(epath), RSTRING_PTR(einfo));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001055 p = strchr(buff, '\n');
1056 if (p) *p = '\0';
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001057 emsg(buff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001058 }
Bram Moolenaarf711cb22018-08-01 18:42:13 +02001059
1060 attr = syn_name2attr((char_u *)"Error");
1061# ifdef RUBY21_OR_LATER
1062 bt = rb_funcallv(error, rb_intern("backtrace"), 0, 0);
1063 for (i = 0; i < RARRAY_LEN(bt); i++)
Bram Moolenaar32526b32019-01-19 17:43:09 +01001064 msg_attr(RSTRING_PTR(RARRAY_AREF(bt, i)), attr);
Bram Moolenaarf711cb22018-08-01 18:42:13 +02001065# else
1066 bt = rb_funcall2(error, rb_intern("backtrace"), 0, 0);
1067 for (i = 0; i < RARRAY_LEN(bt); i++)
Bram Moolenaar32526b32019-01-19 17:43:09 +01001068 msg_attr(RSTRING_PTR(RARRAY_PTR(bt)[i]), attr);
Bram Moolenaarf711cb22018-08-01 18:42:13 +02001069# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001070 break;
1071 default:
Bram Moolenaar9c13b352005-05-19 20:53:52 +00001072 vim_snprintf(buff, BUFSIZ, _("E273: unknown longjmp status %d"), state);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001073 emsg(buff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001074 break;
1075 }
1076}
1077
Bram Moolenaarcd8b20a2009-05-22 16:20:57 +00001078static VALUE vim_message(VALUE self UNUSED, VALUE str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001079{
1080 char *buff, *p;
1081
1082 str = rb_obj_as_string(str);
Bram Moolenaar3f5f7952011-08-04 19:34:59 +02001083 if (RSTRING_LEN(str) > 0)
1084 {
1085 /* Only do this when the string isn't empty, alloc(0) causes trouble. */
Bram Moolenaar00ccf542017-09-03 15:17:48 +02001086 buff = ALLOCA_N(char, RSTRING_LEN(str) + 1);
Bram Moolenaar3f5f7952011-08-04 19:34:59 +02001087 strcpy(buff, RSTRING_PTR(str));
1088 p = strchr(buff, '\n');
1089 if (p) *p = '\0';
Bram Moolenaar32526b32019-01-19 17:43:09 +01001090 msg(buff);
Bram Moolenaar3f5f7952011-08-04 19:34:59 +02001091 }
1092 else
1093 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01001094 msg("");
Bram Moolenaar3f5f7952011-08-04 19:34:59 +02001095 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001096 return Qnil;
1097}
1098
Bram Moolenaarcd8b20a2009-05-22 16:20:57 +00001099static VALUE vim_set_option(VALUE self UNUSED, VALUE str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001100{
Bram Moolenaar165641d2010-02-17 16:23:09 +01001101 do_set((char_u *)StringValuePtr(str), 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001102 update_screen(NOT_VALID);
1103 return Qnil;
1104}
1105
Bram Moolenaarcd8b20a2009-05-22 16:20:57 +00001106static VALUE vim_command(VALUE self UNUSED, VALUE str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001107{
Bram Moolenaar165641d2010-02-17 16:23:09 +01001108 do_cmdline_cmd((char_u *)StringValuePtr(str));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001109 return Qnil;
1110}
1111
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001112#ifdef FEAT_EVAL
1113static VALUE vim_to_ruby(typval_T *tv)
1114{
1115 VALUE result = Qnil;
1116
1117 if (tv->v_type == VAR_STRING)
1118 {
Bram Moolenaar94127e42010-03-19 23:08:48 +01001119 result = rb_str_new2(tv->vval.v_string == NULL
1120 ? "" : (char *)(tv->vval.v_string));
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001121 }
1122 else if (tv->v_type == VAR_NUMBER)
1123 {
Bram Moolenaar639a2552010-03-17 18:15:23 +01001124 result = INT2NUM(tv->vval.v_number);
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001125 }
1126# ifdef FEAT_FLOAT
1127 else if (tv->v_type == VAR_FLOAT)
1128 {
Bram Moolenaar639a2552010-03-17 18:15:23 +01001129 result = rb_float_new(tv->vval.v_float);
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001130 }
1131# endif
1132 else if (tv->v_type == VAR_LIST)
1133 {
Bram Moolenaar639a2552010-03-17 18:15:23 +01001134 list_T *list = tv->vval.v_list;
1135 listitem_T *curr;
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001136
Bram Moolenaar639a2552010-03-17 18:15:23 +01001137 result = rb_ary_new();
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001138
Bram Moolenaar639a2552010-03-17 18:15:23 +01001139 if (list != NULL)
1140 {
1141 for (curr = list->lv_first; curr != NULL; curr = curr->li_next)
1142 {
1143 rb_ary_push(result, vim_to_ruby(&curr->li_tv));
1144 }
1145 }
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001146 }
1147 else if (tv->v_type == VAR_DICT)
1148 {
Bram Moolenaar639a2552010-03-17 18:15:23 +01001149 result = rb_hash_new();
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001150
Bram Moolenaar639a2552010-03-17 18:15:23 +01001151 if (tv->vval.v_dict != NULL)
1152 {
1153 hashtab_T *ht = &tv->vval.v_dict->dv_hashtab;
1154 long_u todo = ht->ht_used;
1155 hashitem_T *hi;
1156 dictitem_T *di;
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001157
Bram Moolenaar639a2552010-03-17 18:15:23 +01001158 for (hi = ht->ht_array; todo > 0; ++hi)
1159 {
1160 if (!HASHITEM_EMPTY(hi))
1161 {
1162 --todo;
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001163
Bram Moolenaar639a2552010-03-17 18:15:23 +01001164 di = dict_lookup(hi);
1165 rb_hash_aset(result, rb_str_new2((char *)hi->hi_key),
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001166 vim_to_ruby(&di->di_tv));
Bram Moolenaar639a2552010-03-17 18:15:23 +01001167 }
1168 }
1169 }
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001170 }
1171 else if (tv->v_type == VAR_SPECIAL)
1172 {
Bram Moolenaard84b26a2018-07-28 17:18:09 +02001173 if (tv->vval.v_number == VVAL_TRUE)
1174 result = Qtrue;
1175 else if (tv->vval.v_number == VVAL_FALSE)
1176 result = Qfalse;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001177 }
1178 else if (tv->v_type == VAR_BLOB)
1179 {
1180 result = rb_str_new(tv->vval.v_blob->bv_ga.ga_data,
1181 tv->vval.v_blob->bv_ga.ga_len);
1182 }
1183 /* else return Qnil; */
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001184
1185 return result;
1186}
1187#endif
1188
Bram Moolenaarcd8b20a2009-05-22 16:20:57 +00001189static VALUE vim_evaluate(VALUE self UNUSED, VALUE str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001190{
1191#ifdef FEAT_EVAL
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001192 typval_T *tv;
1193 VALUE result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001194
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001195 tv = eval_expr((char_u *)StringValuePtr(str), NULL);
1196 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001197 {
Bram Moolenaar639a2552010-03-17 18:15:23 +01001198 return Qnil;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001199 }
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001200 result = vim_to_ruby(tv);
1201
1202 free_tv(tv);
1203
1204 return result;
1205#else
1206 return Qnil;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001207#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001208}
1209
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001210#ifdef USE_TYPEDDATA
1211static size_t buffer_dsize(const void *buf);
1212
1213static const rb_data_type_t buffer_type = {
1214 "vim_buffer",
1215 {0, 0, buffer_dsize, {0, 0}},
1216 0, 0,
1217# ifdef RUBY_TYPED_FREE_IMMEDIATELY
1218 0,
1219# endif
1220};
1221
1222static size_t buffer_dsize(const void *buf UNUSED)
1223{
1224 return sizeof(buf_T);
1225}
1226#endif
1227
Bram Moolenaar071d4272004-06-13 20:20:40 +00001228static VALUE buffer_new(buf_T *buf)
1229{
Bram Moolenaare344bea2005-09-01 20:46:49 +00001230 if (buf->b_ruby_ref)
1231 {
1232 return (VALUE) buf->b_ruby_ref;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001233 }
Bram Moolenaare344bea2005-09-01 20:46:49 +00001234 else
1235 {
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001236#ifdef USE_TYPEDDATA
1237 VALUE obj = TypedData_Wrap_Struct(cBuffer, &buffer_type, buf);
1238#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001239 VALUE obj = Data_Wrap_Struct(cBuffer, 0, 0, buf);
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001240#endif
Bram Moolenaare344bea2005-09-01 20:46:49 +00001241 buf->b_ruby_ref = (void *) obj;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001242 rb_hash_aset(objtbl, rb_obj_id(obj), obj);
1243 return obj;
1244 }
1245}
1246
1247static buf_T *get_buf(VALUE obj)
1248{
1249 buf_T *buf;
1250
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001251#ifdef USE_TYPEDDATA
1252 TypedData_Get_Struct(obj, buf_T, &buffer_type, buf);
1253#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001254 Data_Get_Struct(obj, buf_T, buf);
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001255#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001256 if (buf == NULL)
1257 rb_raise(eDeletedBufferError, "attempt to refer to deleted buffer");
1258 return buf;
1259}
1260
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001261static VALUE vim_blob(VALUE self UNUSED, VALUE str)
1262{
1263 VALUE result = rb_str_new("0z", 2);
1264 char buf[4];
1265 int i;
1266 for (i = 0; i < RSTRING_LEN(str); i++)
1267 {
1268 sprintf(buf, "%02X", RSTRING_PTR(str)[i]);
Bram Moolenaarb191be22019-01-30 21:00:12 +01001269 rb_str_concat(result, rb_str_new2(buf));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001270 }
1271 return result;
1272}
1273
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001274static VALUE buffer_s_current(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001275{
1276 return buffer_new(curbuf);
1277}
1278
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001279static VALUE buffer_s_count(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001280{
1281 buf_T *b;
1282 int n = 0;
1283
Bram Moolenaar29323592016-07-24 22:04:11 +02001284 FOR_ALL_BUFFERS(b)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001285 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001286 /* Deleted buffers should not be counted
1287 * SegPhault - 01/07/05 */
1288 if (b->b_p_bl)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001289 n++;
1290 }
1291
Bram Moolenaar071d4272004-06-13 20:20:40 +00001292 return INT2NUM(n);
1293}
1294
Bram Moolenaarcd8b20a2009-05-22 16:20:57 +00001295static VALUE buffer_s_aref(VALUE self UNUSED, VALUE num)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001296{
1297 buf_T *b;
1298 int n = NUM2INT(num);
1299
Bram Moolenaar29323592016-07-24 22:04:11 +02001300 FOR_ALL_BUFFERS(b)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001301 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001302 /* Deleted buffers should not be counted
1303 * SegPhault - 01/07/05 */
1304 if (!b->b_p_bl)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001305 continue;
1306
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001307 if (n == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001308 return buffer_new(b);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001309
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001310 n--;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001311 }
1312 return Qnil;
1313}
1314
1315static VALUE buffer_name(VALUE self)
1316{
1317 buf_T *buf = get_buf(self);
1318
Bram Moolenaar35a2e192006-03-13 22:07:11 +00001319 return buf->b_ffname ? rb_str_new2((char *)buf->b_ffname) : Qnil;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001320}
1321
1322static VALUE buffer_number(VALUE self)
1323{
1324 buf_T *buf = get_buf(self);
1325
1326 return INT2NUM(buf->b_fnum);
1327}
1328
1329static VALUE buffer_count(VALUE self)
1330{
1331 buf_T *buf = get_buf(self);
1332
1333 return INT2NUM(buf->b_ml.ml_line_count);
1334}
1335
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001336static VALUE get_buffer_line(buf_T *buf, linenr_T n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001337{
Bram Moolenaar3c531602010-11-16 14:46:19 +01001338 if (n <= 0 || n > buf->b_ml.ml_line_count)
1339 rb_raise(rb_eIndexError, "line number %ld out of range", (long)n);
1340 return vim_str2rb_enc_str((char *)ml_get_buf(buf, n, FALSE));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001341}
1342
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001343static VALUE buffer_aref(VALUE self, VALUE num)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001344{
1345 buf_T *buf = get_buf(self);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001346
1347 if (buf != NULL)
1348 return get_buffer_line(buf, (linenr_T)NUM2LONG(num));
1349 return Qnil; /* For stop warning */
1350}
1351
1352static VALUE set_buffer_line(buf_T *buf, linenr_T n, VALUE str)
1353{
Bram Moolenaar165641d2010-02-17 16:23:09 +01001354 char *line = StringValuePtr(str);
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001355 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001356
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001357 if (n > 0 && n <= buf->b_ml.ml_line_count && line != NULL)
1358 {
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001359 /* set curwin/curbuf for "buf" and save some things */
1360 aucmd_prepbuf(&aco, buf);
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001361
Bram Moolenaar758535a2016-03-30 22:06:16 +02001362 if (u_savesub(n) == OK)
1363 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001364 ml_replace(n, (char_u *)line, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001365 changed();
1366#ifdef SYNTAX_HL
1367 syn_changed(n); /* recompute syntax hl. for this line */
1368#endif
1369 }
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001370
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001371 /* restore curwin/curbuf and a few other things */
1372 aucmd_restbuf(&aco);
1373 /* Careful: autocommands may have made "buf" invalid! */
Bram Moolenaarf30e74c2006-08-16 17:35:00 +00001374
Bram Moolenaar071d4272004-06-13 20:20:40 +00001375 update_curbuf(NOT_VALID);
1376 }
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001377 else
1378 {
Bram Moolenaar165641d2010-02-17 16:23:09 +01001379 rb_raise(rb_eIndexError, "line number %ld out of range", (long)n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001380 }
1381 return str;
1382}
1383
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001384static VALUE buffer_aset(VALUE self, VALUE num, VALUE str)
1385{
1386 buf_T *buf = get_buf(self);
1387
1388 if (buf != NULL)
1389 return set_buffer_line(buf, (linenr_T)NUM2LONG(num), str);
1390 return str;
1391}
1392
Bram Moolenaar071d4272004-06-13 20:20:40 +00001393static VALUE buffer_delete(VALUE self, VALUE num)
1394{
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001395 buf_T *buf = get_buf(self);
1396 long n = NUM2LONG(num);
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001397 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001398
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001399 if (n > 0 && n <= buf->b_ml.ml_line_count)
1400 {
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001401 /* set curwin/curbuf for "buf" and save some things */
1402 aucmd_prepbuf(&aco, buf);
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001403
Bram Moolenaar758535a2016-03-30 22:06:16 +02001404 if (u_savedel(n, 1) == OK)
1405 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001406 ml_delete(n, 0);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001407
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001408 /* Changes to non-active buffers should properly refresh
1409 * SegPhault - 01/09/05 */
1410 deleted_lines_mark(n, 1L);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001411
Bram Moolenaar071d4272004-06-13 20:20:40 +00001412 changed();
1413 }
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001414
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001415 /* restore curwin/curbuf and a few other things */
1416 aucmd_restbuf(&aco);
1417 /* Careful: autocommands may have made "buf" invalid! */
Bram Moolenaarf30e74c2006-08-16 17:35:00 +00001418
Bram Moolenaar071d4272004-06-13 20:20:40 +00001419 update_curbuf(NOT_VALID);
1420 }
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001421 else
1422 {
Bram Moolenaar165641d2010-02-17 16:23:09 +01001423 rb_raise(rb_eIndexError, "line number %ld out of range", n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001424 }
1425 return Qnil;
1426}
1427
1428static VALUE buffer_append(VALUE self, VALUE num, VALUE str)
1429{
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001430 buf_T *buf = get_buf(self);
Bram Moolenaar165641d2010-02-17 16:23:09 +01001431 char *line = StringValuePtr(str);
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001432 long n = NUM2LONG(num);
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001433 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001434
Bram Moolenaar3c531602010-11-16 14:46:19 +01001435 if (line == NULL)
1436 {
Bram Moolenaar165641d2010-02-17 16:23:09 +01001437 rb_raise(rb_eIndexError, "NULL line");
1438 }
1439 else if (n >= 0 && n <= buf->b_ml.ml_line_count)
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001440 {
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001441 /* set curwin/curbuf for "buf" and save some things */
1442 aucmd_prepbuf(&aco, buf);
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001443
Bram Moolenaar758535a2016-03-30 22:06:16 +02001444 if (u_inssub(n + 1) == OK)
1445 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001446 ml_append(n, (char_u *) line, (colnr_T) 0, FALSE);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001447
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001448 /* Changes to non-active buffers should properly refresh screen
1449 * SegPhault - 12/20/04 */
1450 appended_lines_mark(n, 1L);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001451
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001452 changed();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001453 }
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001454
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001455 /* restore curwin/curbuf and a few other things */
1456 aucmd_restbuf(&aco);
1457 /* Careful: autocommands may have made "buf" invalid! */
Bram Moolenaarf30e74c2006-08-16 17:35:00 +00001458
Bram Moolenaar071d4272004-06-13 20:20:40 +00001459 update_curbuf(NOT_VALID);
1460 }
Bram Moolenaar3c531602010-11-16 14:46:19 +01001461 else
1462 {
Bram Moolenaar165641d2010-02-17 16:23:09 +01001463 rb_raise(rb_eIndexError, "line number %ld out of range", n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001464 }
1465 return str;
1466}
1467
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001468#ifdef USE_TYPEDDATA
1469static size_t window_dsize(const void *buf);
1470
1471static const rb_data_type_t window_type = {
1472 "vim_window",
1473 {0, 0, window_dsize, {0, 0}},
1474 0, 0,
1475# ifdef RUBY_TYPED_FREE_IMMEDIATELY
1476 0,
1477# endif
1478};
1479
1480static size_t window_dsize(const void *win UNUSED)
1481{
1482 return sizeof(win_T);
1483}
1484#endif
1485
Bram Moolenaar071d4272004-06-13 20:20:40 +00001486static VALUE window_new(win_T *win)
1487{
Bram Moolenaare344bea2005-09-01 20:46:49 +00001488 if (win->w_ruby_ref)
1489 {
1490 return (VALUE) win->w_ruby_ref;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001491 }
Bram Moolenaare344bea2005-09-01 20:46:49 +00001492 else
1493 {
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001494#ifdef USE_TYPEDDATA
1495 VALUE obj = TypedData_Wrap_Struct(cVimWindow, &window_type, win);
1496#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001497 VALUE obj = Data_Wrap_Struct(cVimWindow, 0, 0, win);
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001498#endif
Bram Moolenaare344bea2005-09-01 20:46:49 +00001499 win->w_ruby_ref = (void *) obj;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001500 rb_hash_aset(objtbl, rb_obj_id(obj), obj);
1501 return obj;
1502 }
1503}
1504
1505static win_T *get_win(VALUE obj)
1506{
1507 win_T *win;
1508
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001509#ifdef USE_TYPEDDATA
1510 TypedData_Get_Struct(obj, win_T, &window_type, win);
1511#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001512 Data_Get_Struct(obj, win_T, win);
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001513#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001514 if (win == NULL)
1515 rb_raise(eDeletedWindowError, "attempt to refer to deleted window");
1516 return win;
1517}
1518
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001519static VALUE window_s_current(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001520{
1521 return window_new(curwin);
1522}
1523
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001524/*
1525 * Added line manipulation functions
1526 * SegPhault - 03/07/05
1527 */
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001528static VALUE line_s_current(void)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001529{
1530 return get_buffer_line(curbuf, curwin->w_cursor.lnum);
1531}
1532
Bram Moolenaarcd8b20a2009-05-22 16:20:57 +00001533static VALUE set_current_line(VALUE self UNUSED, VALUE str)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001534{
1535 return set_buffer_line(curbuf, curwin->w_cursor.lnum, str);
1536}
1537
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001538static VALUE current_line_number(void)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001539{
1540 return INT2FIX((int)curwin->w_cursor.lnum);
1541}
1542
1543
1544
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001545static VALUE window_s_count(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001546{
Bram Moolenaar071d4272004-06-13 20:20:40 +00001547 win_T *w;
1548 int n = 0;
1549
Bram Moolenaar29323592016-07-24 22:04:11 +02001550 FOR_ALL_WINDOWS(w)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001551 n++;
1552 return INT2NUM(n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001553}
1554
Bram Moolenaarcd8b20a2009-05-22 16:20:57 +00001555static VALUE window_s_aref(VALUE self UNUSED, VALUE num)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001556{
1557 win_T *w;
1558 int n = NUM2INT(num);
1559
Bram Moolenaar071d4272004-06-13 20:20:40 +00001560 for (w = firstwin; w != NULL; w = w->w_next, --n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001561 if (n == 0)
1562 return window_new(w);
1563 return Qnil;
1564}
1565
1566static VALUE window_buffer(VALUE self)
1567{
1568 win_T *win = get_win(self);
1569
1570 return buffer_new(win->w_buffer);
1571}
1572
1573static VALUE window_height(VALUE self)
1574{
1575 win_T *win = get_win(self);
1576
1577 return INT2NUM(win->w_height);
1578}
1579
1580static VALUE window_set_height(VALUE self, VALUE height)
1581{
1582 win_T *win = get_win(self);
1583 win_T *savewin = curwin;
1584
1585 curwin = win;
1586 win_setheight(NUM2INT(height));
1587 curwin = savewin;
1588 return height;
1589}
1590
Bram Moolenaarfeeaa682013-02-14 22:19:51 +01001591static VALUE window_width(VALUE self UNUSED)
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001592{
Bram Moolenaare745d752017-09-22 16:56:22 +02001593 return INT2NUM(get_win(self)->w_width);
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001594}
1595
Bram Moolenaarfeeaa682013-02-14 22:19:51 +01001596static VALUE window_set_width(VALUE self UNUSED, VALUE width)
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001597{
1598 win_T *win = get_win(self);
1599 win_T *savewin = curwin;
1600
1601 curwin = win;
1602 win_setwidth(NUM2INT(width));
1603 curwin = savewin;
1604 return width;
1605}
1606
Bram Moolenaar071d4272004-06-13 20:20:40 +00001607static VALUE window_cursor(VALUE self)
1608{
1609 win_T *win = get_win(self);
1610
1611 return rb_assoc_new(INT2NUM(win->w_cursor.lnum), INT2NUM(win->w_cursor.col));
1612}
1613
1614static VALUE window_set_cursor(VALUE self, VALUE pos)
1615{
1616 VALUE lnum, col;
1617 win_T *win = get_win(self);
1618
1619 Check_Type(pos, T_ARRAY);
Bram Moolenaar165641d2010-02-17 16:23:09 +01001620 if (RARRAY_LEN(pos) != 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001621 rb_raise(rb_eArgError, "array length must be 2");
Bram Moolenaar165641d2010-02-17 16:23:09 +01001622 lnum = RARRAY_PTR(pos)[0];
1623 col = RARRAY_PTR(pos)[1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001624 win->w_cursor.lnum = NUM2LONG(lnum);
1625 win->w_cursor.col = NUM2UINT(col);
Bram Moolenaar53901442018-07-25 22:02:36 +02001626 win->w_set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001627 check_cursor(); /* put cursor on an existing line */
1628 update_screen(NOT_VALID);
1629 return Qnil;
1630}
1631
Bram Moolenaar6217cdc2012-04-25 12:28:09 +02001632static VALUE f_nop(VALUE self UNUSED)
Bram Moolenaar35df7d22012-04-20 18:05:47 +02001633{
1634 return Qnil;
1635}
1636
Bram Moolenaarcd8b20a2009-05-22 16:20:57 +00001637static VALUE f_p(int argc, VALUE *argv, VALUE self UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001638{
1639 int i;
1640 VALUE str = rb_str_new("", 0);
Bram Moolenaar51e9fbf2018-08-11 14:24:11 +02001641 VALUE ret = Qnil;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001642
Bram Moolenaar758535a2016-03-30 22:06:16 +02001643 for (i = 0; i < argc; i++)
1644 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001645 if (i > 0) rb_str_cat(str, ", ", 2);
1646 rb_str_concat(str, rb_inspect(argv[i]));
1647 }
Bram Moolenaar32526b32019-01-19 17:43:09 +01001648 msg(RSTRING_PTR(str));
Bram Moolenaar51e9fbf2018-08-11 14:24:11 +02001649
1650 if (argc == 1)
1651 ret = argv[0];
1652 else if (argc > 1)
1653 ret = rb_ary_new4(argc, argv);
1654 return ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001655}
1656
1657static void ruby_io_init(void)
1658{
1659#ifndef DYNAMIC_RUBY
1660 RUBYEXTERN VALUE rb_stdout;
Bram Moolenaarb6c8cd82018-07-24 05:41:30 +02001661 RUBYEXTERN VALUE rb_stderr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001662#endif
1663
1664 rb_stdout = rb_obj_alloc(rb_cObject);
Bram Moolenaarb6c8cd82018-07-24 05:41:30 +02001665 rb_stderr = rb_obj_alloc(rb_cObject);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001666 rb_define_singleton_method(rb_stdout, "write", vim_message, 1);
Bram Moolenaar35df7d22012-04-20 18:05:47 +02001667 rb_define_singleton_method(rb_stdout, "flush", f_nop, 0);
Bram Moolenaarb6c8cd82018-07-24 05:41:30 +02001668 rb_define_singleton_method(rb_stderr, "write", vim_message, 1);
1669 rb_define_singleton_method(rb_stderr, "flush", f_nop, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001670 rb_define_global_function("p", f_p, -1);
1671}
1672
1673static void ruby_vim_init(void)
1674{
1675 objtbl = rb_hash_new();
1676 rb_global_variable(&objtbl);
1677
Bram Moolenaarf711faf2007-05-10 16:48:19 +00001678 /* The Vim module used to be called "VIM", but "Vim" is better. Make an
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02001679 * alias "VIM" for backwards compatibility. */
Bram Moolenaarf711faf2007-05-10 16:48:19 +00001680 mVIM = rb_define_module("Vim");
1681 rb_define_const(rb_cObject, "VIM", mVIM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001682 rb_define_const(mVIM, "VERSION_MAJOR", INT2NUM(VIM_VERSION_MAJOR));
1683 rb_define_const(mVIM, "VERSION_MINOR", INT2NUM(VIM_VERSION_MINOR));
1684 rb_define_const(mVIM, "VERSION_BUILD", INT2NUM(VIM_VERSION_BUILD));
1685 rb_define_const(mVIM, "VERSION_PATCHLEVEL", INT2NUM(VIM_VERSION_PATCHLEVEL));
1686 rb_define_const(mVIM, "VERSION_SHORT", rb_str_new2(VIM_VERSION_SHORT));
1687 rb_define_const(mVIM, "VERSION_MEDIUM", rb_str_new2(VIM_VERSION_MEDIUM));
1688 rb_define_const(mVIM, "VERSION_LONG", rb_str_new2(VIM_VERSION_LONG));
1689 rb_define_const(mVIM, "VERSION_LONG_DATE", rb_str_new2(VIM_VERSION_LONG_DATE));
1690 rb_define_module_function(mVIM, "message", vim_message, 1);
1691 rb_define_module_function(mVIM, "set_option", vim_set_option, 1);
1692 rb_define_module_function(mVIM, "command", vim_command, 1);
1693 rb_define_module_function(mVIM, "evaluate", vim_evaluate, 1);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001694 rb_define_module_function(mVIM, "blob", vim_blob, 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001695
1696 eDeletedBufferError = rb_define_class_under(mVIM, "DeletedBufferError",
1697 rb_eStandardError);
1698 eDeletedWindowError = rb_define_class_under(mVIM, "DeletedWindowError",
1699 rb_eStandardError);
1700
1701 cBuffer = rb_define_class_under(mVIM, "Buffer", rb_cObject);
1702 rb_define_singleton_method(cBuffer, "current", buffer_s_current, 0);
1703 rb_define_singleton_method(cBuffer, "count", buffer_s_count, 0);
1704 rb_define_singleton_method(cBuffer, "[]", buffer_s_aref, 1);
1705 rb_define_method(cBuffer, "name", buffer_name, 0);
1706 rb_define_method(cBuffer, "number", buffer_number, 0);
1707 rb_define_method(cBuffer, "count", buffer_count, 0);
1708 rb_define_method(cBuffer, "length", buffer_count, 0);
1709 rb_define_method(cBuffer, "[]", buffer_aref, 1);
1710 rb_define_method(cBuffer, "[]=", buffer_aset, 2);
1711 rb_define_method(cBuffer, "delete", buffer_delete, 1);
1712 rb_define_method(cBuffer, "append", buffer_append, 2);
1713
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001714 /* Added line manipulation functions
1715 * SegPhault - 03/07/05 */
1716 rb_define_method(cBuffer, "line_number", current_line_number, 0);
1717 rb_define_method(cBuffer, "line", line_s_current, 0);
1718 rb_define_method(cBuffer, "line=", set_current_line, 1);
1719
1720
Bram Moolenaar071d4272004-06-13 20:20:40 +00001721 cVimWindow = rb_define_class_under(mVIM, "Window", rb_cObject);
1722 rb_define_singleton_method(cVimWindow, "current", window_s_current, 0);
1723 rb_define_singleton_method(cVimWindow, "count", window_s_count, 0);
1724 rb_define_singleton_method(cVimWindow, "[]", window_s_aref, 1);
1725 rb_define_method(cVimWindow, "buffer", window_buffer, 0);
1726 rb_define_method(cVimWindow, "height", window_height, 0);
1727 rb_define_method(cVimWindow, "height=", window_set_height, 1);
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001728 rb_define_method(cVimWindow, "width", window_width, 0);
1729 rb_define_method(cVimWindow, "width=", window_set_width, 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001730 rb_define_method(cVimWindow, "cursor", window_cursor, 0);
1731 rb_define_method(cVimWindow, "cursor=", window_set_cursor, 1);
1732
1733 rb_define_virtual_variable("$curbuf", buffer_s_current, 0);
1734 rb_define_virtual_variable("$curwin", window_s_current, 0);
1735}
Bram Moolenaar99685e62013-05-11 13:56:18 +02001736
1737void vim_ruby_init(void *stack_start)
1738{
1739 /* should get machine stack start address early in main function */
1740 ruby_stack_start = stack_start;
1741}