blob: fd6e552fd8a277c5323b5e09a2dc394aa6eb7204 [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 Moolenaar2d73ff42010-10-27 17:40:59 +020014#ifdef HAVE_CONFIG_H
15# include "auto/config.h"
16#endif
Bram Moolenaar3ca71f12010-10-27 16:49:47 +020017
Bram Moolenaare2793352011-01-17 19:53:27 +010018#include <stdio.h>
19#include <string.h>
20
Bram Moolenaar071d4272004-06-13 20:20:40 +000021#ifdef _WIN32
22# if !defined(DYNAMIC_RUBY_VER) || (DYNAMIC_RUBY_VER < 18)
23# define NT
24# endif
25# ifndef DYNAMIC_RUBY
26# define IMPORT /* For static dll usage __declspec(dllimport) */
27# define RUBYEXTERN __declspec(dllimport)
28# endif
29#endif
30#ifndef RUBYEXTERN
31# define RUBYEXTERN extern
32#endif
33
Bram Moolenaar2016ae52016-06-13 20:08:43 +020034#if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 24
35# define USE_RUBY_INTEGER
Bram Moolenaar06469e92016-06-11 22:26:53 +020036#endif
37
Bram Moolenaarf9b5ef82010-09-29 13:02:53 +020038#ifdef DYNAMIC_RUBY
Bram Moolenaar071d4272004-06-13 20:20:40 +000039/*
40 * This is tricky. In ruby.h there is (inline) function rb_class_of()
41 * definition. This function use these variables. But we want function to
42 * use dll_* variables.
43 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000044# define rb_cFalseClass (*dll_rb_cFalseClass)
45# define rb_cFixnum (*dll_rb_cFixnum)
Bram Moolenaar2016ae52016-06-13 20:08:43 +020046# if defined(USE_RUBY_INTEGER)
47# define rb_cInteger (*dll_rb_cInteger)
Bram Moolenaar06469e92016-06-11 22:26:53 +020048# endif
Bram Moolenaardb3fbe52013-03-07 15:16:21 +010049# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 20
50# define rb_cFloat (*dll_rb_cFloat)
51# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000052# define rb_cNilClass (*dll_rb_cNilClass)
53# define rb_cSymbol (*dll_rb_cSymbol)
54# define rb_cTrueClass (*dll_rb_cTrueClass)
55# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 18
56/*
Bram Moolenaar42d57f02010-03-10 12:47:00 +010057 * On ver 1.8, all Ruby functions are exported with "__declspec(dllimport)"
58 * in ruby.h. But it causes trouble for these variables, because it is
Bram Moolenaar071d4272004-06-13 20:20:40 +000059 * defined in this file. When defined this RUBY_EXPORT it modified to
60 * "extern" and be able to avoid this problem.
61 */
62# define RUBY_EXPORT
63# endif
Bram Moolenaarf9b5ef82010-09-29 13:02:53 +020064
Bram Moolenaar2d73ff42010-10-27 17:40:59 +020065#if !(defined(WIN32) || defined(_WIN64))
Bram Moolenaarf9b5ef82010-09-29 13:02:53 +020066# include <dlfcn.h>
Bram Moolenaar3ca71f12010-10-27 16:49:47 +020067# define HINSTANCE void*
68# define RUBY_PROC void*
Bram Moolenaarf9b5ef82010-09-29 13:02:53 +020069# define load_dll(n) dlopen((n), RTLD_LAZY|RTLD_GLOBAL)
70# define symbol_from_dll dlsym
71# define close_dll dlclose
72#else
Bram Moolenaar3ca71f12010-10-27 16:49:47 +020073# define RUBY_PROC FARPROC
Bram Moolenaarebbcb822010-10-23 14:02:54 +020074# define load_dll vimLoadLib
Bram Moolenaarf9b5ef82010-09-29 13:02:53 +020075# define symbol_from_dll GetProcAddress
76# define close_dll FreeLibrary
Bram Moolenaar071d4272004-06-13 20:20:40 +000077#endif
78
Bram Moolenaarf9b5ef82010-09-29 13:02:53 +020079#endif /* ifdef DYNAMIC_RUBY */
80
Bram Moolenaar0b69c732010-02-17 15:11:50 +010081/* suggested by Ariya Mizutani */
82#if (_MSC_VER == 1200)
83# undef _WIN32_WINNT
84#endif
85
Bram Moolenaar639a2552010-03-17 18:15:23 +010086#if (defined(RUBY_VERSION) && RUBY_VERSION >= 19) \
87 || (defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 19)
88# define RUBY19_OR_LATER 1
89#endif
90
Bram Moolenaar0d27f642015-12-28 22:05:28 +010091#if (defined(RUBY_VERSION) && RUBY_VERSION >= 20) \
92 || (defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 20)
93# define RUBY20_OR_LATER 1
94#endif
95
Bram Moolenaarf711cb22018-08-01 18:42:13 +020096#if (defined(RUBY_VERSION) && RUBY_VERSION >= 21) \
97 || (defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 21)
98# define RUBY21_OR_LATER 1
99#endif
100
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100101#if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 19
102/* Ruby 1.9 defines a number of static functions which use rb_num2long and
103 * rb_int2big */
104# define rb_num2long rb_num2long_stub
105# define rb_int2big rb_int2big_stub
106#endif
107
Bram Moolenaar498af702014-03-28 21:58:21 +0100108#if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 19 \
109 && VIM_SIZEOF_INT < VIM_SIZEOF_LONG
110/* Ruby 1.9 defines a number of static functions which use rb_fix2int and
Bram Moolenaara2aa31a2014-02-23 22:52:40 +0100111 * rb_num2int if VIM_SIZEOF_INT < VIM_SIZEOF_LONG (64bit) */
Bram Moolenaar0bcdd6e2013-04-14 16:19:03 +0200112# define rb_fix2int rb_fix2int_stub
113# define rb_num2int rb_num2int_stub
114#endif
115
Bram Moolenaar0c7485f2015-01-14 14:04:10 +0100116#if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER == 21
Bram Moolenaara1a118b2014-02-05 22:41:15 +0100117/* Ruby 2.1 adds new GC called RGenGC and RARRAY_PTR uses
118 * rb_gc_writebarrier_unprotect_promoted if USE_RGENGC */
Bram Moolenaar90140742014-11-27 17:44:08 +0100119# define rb_gc_writebarrier_unprotect_promoted rb_gc_writebarrier_unprotect_promoted_stub
120#endif
Bram Moolenaar0c7485f2015-01-14 14:04:10 +0100121#if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 22
122# define rb_gc_writebarrier_unprotect rb_gc_writebarrier_unprotect_stub
Bram Moolenaar0c7485f2015-01-14 14:04:10 +0100123#endif
Bram Moolenaara1a118b2014-02-05 22:41:15 +0100124
Bram Moolenaar071d4272004-06-13 20:20:40 +0000125#include <ruby.h>
Bram Moolenaar639a2552010-03-17 18:15:23 +0100126#ifdef RUBY19_OR_LATER
Bram Moolenaar165641d2010-02-17 16:23:09 +0100127# include <ruby/encoding.h>
128#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000129
Bram Moolenaar7a8ef142010-12-24 13:39:35 +0100130#undef off_t /* ruby defines off_t as _int64, Mingw uses long */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000131#undef EXTERN
132#undef _
133
134/* T_DATA defined both by Ruby and Mac header files, hack around it... */
Bram Moolenaard0573012017-10-28 21:11:06 +0200135#if defined(MACOS_X)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000136# define __OPENTRANSPORT__
137# define __OPENTRANSPORTPROTOCOL__
138# define __OPENTRANSPORTPROVIDERS__
139#endif
140
Bram Moolenaar165641d2010-02-17 16:23:09 +0100141/*
Bram Moolenaar0d27f642015-12-28 22:05:28 +0100142 * The TypedData_XXX macro family can be used since Ruby 1.9.2 but
143 * rb_data_type_t changed in 1.9.3, therefore require at least 2.0.
144 * The old Data_XXX macro family was deprecated on Ruby 2.2.
Bram Moolenaarf2f6d292015-12-28 20:57:10 +0100145 * Use TypedData_XXX if available.
146 */
Bram Moolenaar0d27f642015-12-28 22:05:28 +0100147#if defined(TypedData_Wrap_Struct) && defined(RUBY20_OR_LATER)
Bram Moolenaarf2f6d292015-12-28 20:57:10 +0100148# define USE_TYPEDDATA 1
149#endif
150
151/*
Bram Moolenaar84a05ac2013-05-06 04:24:17 +0200152 * Backward compatibility for Ruby 1.8 and earlier.
Bram Moolenaar165641d2010-02-17 16:23:09 +0100153 * Ruby 1.9 does not provide STR2CSTR, instead StringValuePtr is provided.
154 * Ruby 1.9 does not provide RXXX(s)->len and RXXX(s)->ptr, instead
155 * RXXX_LEN(s) and RXXX_PTR(s) are provided.
156 */
157#ifndef StringValuePtr
158# define StringValuePtr(s) STR2CSTR(s)
159#endif
160#ifndef RARRAY_LEN
161# define RARRAY_LEN(s) RARRAY(s)->len
162#endif
163#ifndef RARRAY_PTR
164# define RARRAY_PTR(s) RARRAY(s)->ptr
165#endif
166#ifndef RSTRING_LEN
167# define RSTRING_LEN(s) RSTRING(s)->len
168#endif
169#ifndef RSTRING_PTR
170# define RSTRING_PTR(s) RSTRING(s)->ptr
171#endif
172
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100173#ifdef HAVE_DUP
174# undef HAVE_DUP
175#endif
176
Bram Moolenaar071d4272004-06-13 20:20:40 +0000177#include "vim.h"
178#include "version.h"
179
180#if defined(PROTO) && !defined(FEAT_RUBY)
181/* Define these to be able to generate the function prototypes. */
182# define VALUE int
183# define RUBY_DATA_FUNC int
184#endif
185
186static int ruby_initialized = 0;
Bram Moolenaar99685e62013-05-11 13:56:18 +0200187static void *ruby_stack_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000188static VALUE objtbl;
189
190static VALUE mVIM;
191static VALUE cBuffer;
192static VALUE cVimWindow;
193static VALUE eDeletedBufferError;
194static VALUE eDeletedWindowError;
195
196static int ensure_ruby_initialized(void);
197static void error_print(int);
198static void ruby_io_init(void);
199static void ruby_vim_init(void);
200
Bram Moolenaar10f3a792013-05-20 12:52:29 +0200201#if defined(RUBY19_OR_LATER) || defined(RUBY_INIT_STACK)
202# if defined(__ia64) && !defined(ruby_init_stack)
203# define ruby_init_stack(addr) ruby_init_stack((addr), rb_ia64_bsp())
204# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000205#endif
206
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200207#if defined(DYNAMIC_RUBY) || defined(PROTO)
Bram Moolenaaref269542016-01-19 13:22:12 +0100208# if defined(PROTO) && !defined(HINSTANCE)
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200209# define HINSTANCE int /* for generating prototypes */
210# endif
211
Bram Moolenaar071d4272004-06-13 20:20:40 +0000212/*
213 * Wrapper defines
214 */
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200215# define rb_assoc_new dll_rb_assoc_new
216# define rb_cObject (*dll_rb_cObject)
Bram Moolenaarf2f6d292015-12-28 20:57:10 +0100217# define rb_check_type dll_rb_check_type
218# ifdef USE_TYPEDDATA
219# define rb_check_typeddata dll_rb_check_typeddata
Bram Moolenaar0c7485f2015-01-14 14:04:10 +0100220# endif
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200221# define rb_class_path dll_rb_class_path
Bram Moolenaarf2f6d292015-12-28 20:57:10 +0100222# ifdef USE_TYPEDDATA
223# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 23
224# define rb_data_typed_object_wrap dll_rb_data_typed_object_wrap
225# else
226# define rb_data_typed_object_alloc dll_rb_data_typed_object_alloc
227# endif
228# else
229# define rb_data_object_alloc dll_rb_data_object_alloc
230# endif
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200231# define rb_define_class_under dll_rb_define_class_under
232# define rb_define_const dll_rb_define_const
233# define rb_define_global_function dll_rb_define_global_function
234# define rb_define_method dll_rb_define_method
235# define rb_define_module dll_rb_define_module
236# define rb_define_module_function dll_rb_define_module_function
237# define rb_define_singleton_method dll_rb_define_singleton_method
238# define rb_define_virtual_variable dll_rb_define_virtual_variable
239# define rb_stdout (*dll_rb_stdout)
Bram Moolenaarb6c8cd82018-07-24 05:41:30 +0200240# define rb_stderr (*dll_rb_stderr)
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200241# define rb_eArgError (*dll_rb_eArgError)
242# define rb_eIndexError (*dll_rb_eIndexError)
243# define rb_eRuntimeError (*dll_rb_eRuntimeError)
244# define rb_eStandardError (*dll_rb_eStandardError)
245# define rb_eval_string_protect dll_rb_eval_string_protect
Bram Moolenaarf711cb22018-08-01 18:42:13 +0200246# ifdef RUBY21_OR_LATER
247# define rb_funcallv dll_rb_funcallv
248# else
249# define rb_funcall2 dll_rb_funcall2
250# endif
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200251# define rb_global_variable dll_rb_global_variable
252# define rb_hash_aset dll_rb_hash_aset
253# define rb_hash_new dll_rb_hash_new
254# define rb_inspect dll_rb_inspect
255# define rb_int2inum dll_rb_int2inum
Bram Moolenaar218beb32018-08-04 17:24:44 +0200256
257// ruby.h may redefine rb_intern to use RUBY_CONST_ID_CACHE(), but that won't
258// work. Not using the cache appears to be the best solution.
259# undef rb_intern
260# define rb_intern dll_rb_intern
261
Bram Moolenaara2aa31a2014-02-23 22:52:40 +0100262# if VIM_SIZEOF_INT < VIM_SIZEOF_LONG /* 64 bits only */
Bram Moolenaar498af702014-03-28 21:58:21 +0100263# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER <= 18
264# define rb_fix2int dll_rb_fix2int
265# define rb_num2int dll_rb_num2int
266# endif
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200267# define rb_num2uint dll_rb_num2uint
268# endif
269# define rb_lastline_get dll_rb_lastline_get
270# define rb_lastline_set dll_rb_lastline_set
Bram Moolenaard0573012017-10-28 21:11:06 +0200271# define rb_protect dll_rb_protect
272# define rb_load dll_rb_load
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200273# ifndef RUBY19_OR_LATER
274# define rb_num2long dll_rb_num2long
275# endif
276# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER <= 19
277# define rb_num2ulong dll_rb_num2ulong
278# endif
279# define rb_obj_alloc dll_rb_obj_alloc
280# define rb_obj_as_string dll_rb_obj_as_string
281# define rb_obj_id dll_rb_obj_id
282# define rb_raise dll_rb_raise
283# define rb_str_cat dll_rb_str_cat
284# define rb_str_concat dll_rb_str_concat
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100285# undef rb_str_new
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200286# define rb_str_new dll_rb_str_new
287# ifdef rb_str_new2
Bram Moolenaar1d2beae2010-05-22 21:56:55 +0200288/* Ruby may #define rb_str_new2 to use rb_str_new_cstr. */
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200289# define need_rb_str_new_cstr 1
Bram Moolenaar3ca71f12010-10-27 16:49:47 +0200290/* Ruby's headers #define rb_str_new_cstr to make use of GCC's
291 * __builtin_constant_p extension. */
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200292# undef rb_str_new_cstr
293# define rb_str_new_cstr dll_rb_str_new_cstr
Bram Moolenaar76a86062013-05-11 17:45:48 +0200294# else
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200295# define rb_str_new2 dll_rb_str_new2
Bram Moolenaar76a86062013-05-11 17:45:48 +0200296# endif
Bram Moolenaar3ca71f12010-10-27 16:49:47 +0200297# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 18
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200298# define rb_string_value dll_rb_string_value
299# define rb_string_value_ptr dll_rb_string_value_ptr
300# define rb_float_new dll_rb_float_new
301# define rb_ary_new dll_rb_ary_new
Bram Moolenaar51e9fbf2018-08-11 14:24:11 +0200302# ifdef rb_ary_new4
303# define RB_ARY_NEW4_MACRO 1
304# undef rb_ary_new4
305# endif
306# define rb_ary_new4 dll_rb_ary_new4
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200307# define rb_ary_push dll_rb_ary_push
Bram Moolenaar10f3a792013-05-20 12:52:29 +0200308# if defined(RUBY19_OR_LATER) || defined(RUBY_INIT_STACK)
309# ifdef __ia64
310# define rb_ia64_bsp dll_rb_ia64_bsp
311# undef ruby_init_stack
312# define ruby_init_stack(addr) dll_ruby_init_stack((addr), rb_ia64_bsp())
313# else
314# define ruby_init_stack dll_ruby_init_stack
315# endif
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200316# endif
317# else
318# define rb_str2cstr dll_rb_str2cstr
Bram Moolenaar3ca71f12010-10-27 16:49:47 +0200319# endif
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200320# ifdef RUBY19_OR_LATER
321# define rb_errinfo dll_rb_errinfo
322# else
323# define ruby_errinfo (*dll_ruby_errinfo)
324# endif
325# define ruby_init dll_ruby_init
326# define ruby_init_loadpath dll_ruby_init_loadpath
327# ifdef WIN3264
328# define NtInitialize dll_NtInitialize
Bram Moolenaar4f391792017-01-15 16:59:07 +0100329# define ruby_sysinit dll_ruby_sysinit
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200330# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 18
331# define rb_w32_snprintf dll_rb_w32_snprintf
332# endif
333# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000334
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200335# ifdef RUBY19_OR_LATER
336# define ruby_script dll_ruby_script
337# define rb_enc_find_index dll_rb_enc_find_index
338# define rb_enc_find dll_rb_enc_find
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100339# undef rb_enc_str_new
340# define rb_enc_str_new dll_rb_enc_str_new
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200341# define rb_sprintf dll_rb_sprintf
342# define rb_require dll_rb_require
Bram Moolenaar9b1067e2015-11-19 19:33:15 +0100343# define ruby_options dll_ruby_options
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200344# endif
Bram Moolenaar165641d2010-02-17 16:23:09 +0100345
Bram Moolenaar071d4272004-06-13 20:20:40 +0000346/*
347 * Pointers for dynamic link
348 */
349static VALUE (*dll_rb_assoc_new) (VALUE, VALUE);
Bram Moolenaarba52cde2010-06-25 04:29:11 +0200350VALUE *dll_rb_cFalseClass;
351VALUE *dll_rb_cFixnum;
Bram Moolenaar2016ae52016-06-13 20:08:43 +0200352# if defined(USE_RUBY_INTEGER)
Bram Moolenaar06469e92016-06-11 22:26:53 +0200353VALUE *dll_rb_cInteger;
354# endif
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200355# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 20
Bram Moolenaardb3fbe52013-03-07 15:16:21 +0100356VALUE *dll_rb_cFloat;
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200357# endif
Bram Moolenaarba52cde2010-06-25 04:29:11 +0200358VALUE *dll_rb_cNilClass;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000359static VALUE *dll_rb_cObject;
Bram Moolenaarba52cde2010-06-25 04:29:11 +0200360VALUE *dll_rb_cSymbol;
361VALUE *dll_rb_cTrueClass;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000362static void (*dll_rb_check_type) (VALUE,int);
Bram Moolenaarf2f6d292015-12-28 20:57:10 +0100363# ifdef USE_TYPEDDATA
364static void *(*dll_rb_check_typeddata) (VALUE,const rb_data_type_t *);
365# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000366static VALUE (*dll_rb_class_path) (VALUE);
Bram Moolenaarf2f6d292015-12-28 20:57:10 +0100367# ifdef USE_TYPEDDATA
368# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 23
369static VALUE (*dll_rb_data_typed_object_wrap) (VALUE, void*, const rb_data_type_t *);
370# else
371static VALUE (*dll_rb_data_typed_object_alloc) (VALUE, void*, const rb_data_type_t *);
372# endif
373# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000374static VALUE (*dll_rb_data_object_alloc) (VALUE, void*, RUBY_DATA_FUNC, RUBY_DATA_FUNC);
Bram Moolenaarf2f6d292015-12-28 20:57:10 +0100375# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000376static VALUE (*dll_rb_define_class_under) (VALUE, const char*, VALUE);
377static void (*dll_rb_define_const) (VALUE,const char*,VALUE);
378static void (*dll_rb_define_global_function) (const char*,VALUE(*)(),int);
379static void (*dll_rb_define_method) (VALUE,const char*,VALUE(*)(),int);
380static VALUE (*dll_rb_define_module) (const char*);
381static void (*dll_rb_define_module_function) (VALUE,const char*,VALUE(*)(),int);
382static void (*dll_rb_define_singleton_method) (VALUE,const char*,VALUE(*)(),int);
383static void (*dll_rb_define_virtual_variable) (const char*,VALUE(*)(),void(*)());
384static VALUE *dll_rb_stdout;
Bram Moolenaarb6c8cd82018-07-24 05:41:30 +0200385static VALUE *dll_rb_stderr;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000386static VALUE *dll_rb_eArgError;
387static VALUE *dll_rb_eIndexError;
388static VALUE *dll_rb_eRuntimeError;
389static VALUE *dll_rb_eStandardError;
390static VALUE (*dll_rb_eval_string_protect) (const char*, int*);
Bram Moolenaarf711cb22018-08-01 18:42:13 +0200391# ifdef RUBY21_OR_LATER
392static VALUE (*dll_rb_funcallv) (VALUE, ID, int, const VALUE*);
393# else
394static VALUE (*dll_rb_funcall2) (VALUE, ID, int, const VALUE*);
395# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000396static void (*dll_rb_global_variable) (VALUE*);
397static VALUE (*dll_rb_hash_aset) (VALUE, VALUE, VALUE);
398static VALUE (*dll_rb_hash_new) (void);
399static VALUE (*dll_rb_inspect) (VALUE);
400static VALUE (*dll_rb_int2inum) (long);
Bram Moolenaarf711cb22018-08-01 18:42:13 +0200401static ID (*dll_rb_intern) (const char*);
Bram Moolenaara2aa31a2014-02-23 22:52:40 +0100402# if VIM_SIZEOF_INT < VIM_SIZEOF_LONG /* 64 bits only */
Bram Moolenaar2623b4f2012-09-18 16:36:32 +0200403static long (*dll_rb_fix2int) (VALUE);
404static long (*dll_rb_num2int) (VALUE);
405static unsigned long (*dll_rb_num2uint) (VALUE);
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200406# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000407static VALUE (*dll_rb_lastline_get) (void);
408static void (*dll_rb_lastline_set) (VALUE);
Bram Moolenaar37badc82018-01-31 20:15:30 +0100409static VALUE (*dll_rb_protect) (VALUE (*)(VALUE), VALUE, int*);
Bram Moolenaard0573012017-10-28 21:11:06 +0200410static void (*dll_rb_load) (VALUE, int);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000411static long (*dll_rb_num2long) (VALUE);
412static unsigned long (*dll_rb_num2ulong) (VALUE);
413static VALUE (*dll_rb_obj_alloc) (VALUE);
414static VALUE (*dll_rb_obj_as_string) (VALUE);
415static VALUE (*dll_rb_obj_id) (VALUE);
416static void (*dll_rb_raise) (VALUE, const char*, ...);
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200417# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 18
Bram Moolenaarf9b5ef82010-09-29 13:02:53 +0200418static VALUE (*dll_rb_string_value) (volatile VALUE*);
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200419# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000420static char *(*dll_rb_str2cstr) (VALUE,int*);
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200421# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000422static VALUE (*dll_rb_str_cat) (VALUE, const char*, long);
423static VALUE (*dll_rb_str_concat) (VALUE, VALUE);
424static VALUE (*dll_rb_str_new) (const char*, long);
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200425# ifdef need_rb_str_new_cstr
Bram Moolenaar1d2beae2010-05-22 21:56:55 +0200426/* Ruby may #define rb_str_new2 to use rb_str_new_cstr. */
427static VALUE (*dll_rb_str_new_cstr) (const char*);
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200428# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000429static VALUE (*dll_rb_str_new2) (const char*);
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200430# endif
431# ifdef RUBY19_OR_LATER
Bram Moolenaar165641d2010-02-17 16:23:09 +0100432static VALUE (*dll_rb_errinfo) (void);
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200433# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000434static VALUE *dll_ruby_errinfo;
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200435# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000436static void (*dll_ruby_init) (void);
437static void (*dll_ruby_init_loadpath) (void);
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200438# ifdef WIN3264
Bram Moolenaar0b69c732010-02-17 15:11:50 +0100439static void (*dll_NtInitialize) (int*, char***);
Bram Moolenaar4f391792017-01-15 16:59:07 +0100440static void (*dll_ruby_sysinit) (int*, char***);
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200441# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 18
Bram Moolenaar3ca71f12010-10-27 16:49:47 +0200442static int (*dll_rb_w32_snprintf)(char*, size_t, const char*, ...);
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200443# endif
Bram Moolenaar3ca71f12010-10-27 16:49:47 +0200444# endif
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200445# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 18
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100446static char * (*dll_rb_string_value_ptr) (volatile VALUE*);
447static VALUE (*dll_rb_float_new) (double);
448static VALUE (*dll_rb_ary_new) (void);
Bram Moolenaar51e9fbf2018-08-11 14:24:11 +0200449static VALUE (*dll_rb_ary_new4) (long n, const VALUE *elts);
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100450static VALUE (*dll_rb_ary_push) (VALUE, VALUE);
Bram Moolenaar10f3a792013-05-20 12:52:29 +0200451# if defined(RUBY19_OR_LATER) || defined(RUBY_INIT_STACK)
452# ifdef __ia64
Bram Moolenaar76a86062013-05-11 17:45:48 +0200453static void * (*dll_rb_ia64_bsp) (void);
454static void (*dll_ruby_init_stack)(VALUE*, void*);
Bram Moolenaar10f3a792013-05-20 12:52:29 +0200455# else
Bram Moolenaar76a86062013-05-11 17:45:48 +0200456static void (*dll_ruby_init_stack)(VALUE*);
Bram Moolenaar10f3a792013-05-20 12:52:29 +0200457# endif
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200458# endif
Bram Moolenaar76a86062013-05-11 17:45:48 +0200459# endif
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200460# ifdef RUBY19_OR_LATER
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100461static VALUE (*dll_rb_int2big)(SIGNED_VALUE);
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200462# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000463
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200464# ifdef RUBY19_OR_LATER
Bram Moolenaar165641d2010-02-17 16:23:09 +0100465static void (*dll_ruby_script) (const char*);
466static int (*dll_rb_enc_find_index) (const char*);
467static rb_encoding* (*dll_rb_enc_find) (const char*);
468static VALUE (*dll_rb_enc_str_new) (const char*, long, rb_encoding*);
469static VALUE (*dll_rb_sprintf) (const char*, ...);
Bram Moolenaar7a8ef142010-12-24 13:39:35 +0100470static VALUE (*dll_rb_require) (const char*);
Bram Moolenaar9b1067e2015-11-19 19:33:15 +0100471static void* (*ruby_options)(int, char**);
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200472# endif
Bram Moolenaar165641d2010-02-17 16:23:09 +0100473
Bram Moolenaara1a118b2014-02-05 22:41:15 +0100474# if defined(USE_RGENGC) && USE_RGENGC
Bram Moolenaar0c7485f2015-01-14 14:04:10 +0100475# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER == 21
Bram Moolenaara1a118b2014-02-05 22:41:15 +0100476static void (*dll_rb_gc_writebarrier_unprotect_promoted)(VALUE);
Bram Moolenaar0c7485f2015-01-14 14:04:10 +0100477# else
478static void (*dll_rb_gc_writebarrier_unprotect)(VALUE obj);
479# endif
Bram Moolenaara1a118b2014-02-05 22:41:15 +0100480# endif
481
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200482# if defined(RUBY19_OR_LATER) && !defined(PROTO)
Bram Moolenaarbbc1a592015-04-21 15:25:31 +0200483# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 22
484long rb_num2long_stub(VALUE x)
485# else
Bram Moolenaarff8cf2b2012-11-24 13:39:00 +0100486SIGNED_VALUE rb_num2long_stub(VALUE x)
Bram Moolenaarbbc1a592015-04-21 15:25:31 +0200487# endif
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100488{
489 return dll_rb_num2long(x);
490}
Bram Moolenaarff8cf2b2012-11-24 13:39:00 +0100491VALUE rb_int2big_stub(SIGNED_VALUE x)
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100492{
493 return dll_rb_int2big(x);
494}
Bram Moolenaar498af702014-03-28 21:58:21 +0100495# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 19 \
496 && VIM_SIZEOF_INT < VIM_SIZEOF_LONG
Bram Moolenaar0bcdd6e2013-04-14 16:19:03 +0200497long rb_fix2int_stub(VALUE x)
498{
499 return dll_rb_fix2int(x);
500}
501long rb_num2int_stub(VALUE x)
502{
503 return dll_rb_num2int(x);
504}
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200505# endif
506# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 20
Bram Moolenaar886ed692013-02-26 13:41:35 +0100507VALUE
508rb_float_new_in_heap(double d)
509{
510 return dll_rb_float_new(d);
511}
Bram Moolenaarbbc1a592015-04-21 15:25:31 +0200512# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 22
513unsigned long rb_num2ulong(VALUE x)
514# else
Bram Moolenaardb3fbe52013-03-07 15:16:21 +0100515VALUE rb_num2ulong(VALUE x)
Bram Moolenaarbbc1a592015-04-21 15:25:31 +0200516# endif
Bram Moolenaar886ed692013-02-26 13:41:35 +0100517{
518 return (long)RSHIFT((SIGNED_VALUE)(x),1);
519}
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200520# endif
521# endif
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100522
Bram Moolenaar3e9a1612014-11-12 16:05:04 +0100523 /* Do not generate a prototype here, VALUE isn't always defined. */
524# if defined(USE_RGENGC) && USE_RGENGC && !defined(PROTO)
Bram Moolenaar0c7485f2015-01-14 14:04:10 +0100525# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER == 21
Bram Moolenaara1a118b2014-02-05 22:41:15 +0100526void rb_gc_writebarrier_unprotect_promoted_stub(VALUE obj)
527{
Bram Moolenaar90140742014-11-27 17:44:08 +0100528 dll_rb_gc_writebarrier_unprotect_promoted(obj);
Bram Moolenaara1a118b2014-02-05 22:41:15 +0100529}
Bram Moolenaar0c7485f2015-01-14 14:04:10 +0100530# else
531void rb_gc_writebarrier_unprotect_stub(VALUE obj)
532{
533 dll_rb_gc_writebarrier_unprotect(obj);
534}
535# endif
536# endif
537
Bram Moolenaar3ca71f12010-10-27 16:49:47 +0200538static HINSTANCE hinstRuby = NULL; /* Instance of ruby.dll */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000539
540/*
Bram Moolenaarda2303d2005-08-30 21:55:26 +0000541 * Table of name to function pointer of ruby.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000542 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000543static struct
544{
545 char *name;
546 RUBY_PROC *ptr;
547} ruby_funcname_table[] =
548{
549 {"rb_assoc_new", (RUBY_PROC*)&dll_rb_assoc_new},
550 {"rb_cFalseClass", (RUBY_PROC*)&dll_rb_cFalseClass},
Bram Moolenaar2016ae52016-06-13 20:08:43 +0200551# if defined(USE_RUBY_INTEGER)
Bram Moolenaar06469e92016-06-11 22:26:53 +0200552 {"rb_cInteger", (RUBY_PROC*)&dll_rb_cInteger},
Bram Moolenaar6abda992017-01-09 21:10:31 +0100553# else
554 {"rb_cFixnum", (RUBY_PROC*)&dll_rb_cFixnum},
Bram Moolenaar06469e92016-06-11 22:26:53 +0200555# endif
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200556# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 20
Bram Moolenaardb3fbe52013-03-07 15:16:21 +0100557 {"rb_cFloat", (RUBY_PROC*)&dll_rb_cFloat},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200558# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000559 {"rb_cNilClass", (RUBY_PROC*)&dll_rb_cNilClass},
560 {"rb_cObject", (RUBY_PROC*)&dll_rb_cObject},
561 {"rb_cSymbol", (RUBY_PROC*)&dll_rb_cSymbol},
562 {"rb_cTrueClass", (RUBY_PROC*)&dll_rb_cTrueClass},
563 {"rb_check_type", (RUBY_PROC*)&dll_rb_check_type},
Bram Moolenaarf2f6d292015-12-28 20:57:10 +0100564# ifdef USE_TYPEDDATA
565 {"rb_check_typeddata", (RUBY_PROC*)&dll_rb_check_typeddata},
566# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000567 {"rb_class_path", (RUBY_PROC*)&dll_rb_class_path},
Bram Moolenaarf2f6d292015-12-28 20:57:10 +0100568# ifdef USE_TYPEDDATA
569# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 23
570 {"rb_data_typed_object_wrap", (RUBY_PROC*)&dll_rb_data_typed_object_wrap},
571# else
572 {"rb_data_typed_object_alloc", (RUBY_PROC*)&dll_rb_data_typed_object_alloc},
573# endif
574# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000575 {"rb_data_object_alloc", (RUBY_PROC*)&dll_rb_data_object_alloc},
Bram Moolenaarf2f6d292015-12-28 20:57:10 +0100576# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000577 {"rb_define_class_under", (RUBY_PROC*)&dll_rb_define_class_under},
578 {"rb_define_const", (RUBY_PROC*)&dll_rb_define_const},
579 {"rb_define_global_function", (RUBY_PROC*)&dll_rb_define_global_function},
580 {"rb_define_method", (RUBY_PROC*)&dll_rb_define_method},
581 {"rb_define_module", (RUBY_PROC*)&dll_rb_define_module},
582 {"rb_define_module_function", (RUBY_PROC*)&dll_rb_define_module_function},
583 {"rb_define_singleton_method", (RUBY_PROC*)&dll_rb_define_singleton_method},
584 {"rb_define_virtual_variable", (RUBY_PROC*)&dll_rb_define_virtual_variable},
585 {"rb_stdout", (RUBY_PROC*)&dll_rb_stdout},
Bram Moolenaarb6c8cd82018-07-24 05:41:30 +0200586 {"rb_stderr", (RUBY_PROC*)&dll_rb_stderr},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000587 {"rb_eArgError", (RUBY_PROC*)&dll_rb_eArgError},
588 {"rb_eIndexError", (RUBY_PROC*)&dll_rb_eIndexError},
589 {"rb_eRuntimeError", (RUBY_PROC*)&dll_rb_eRuntimeError},
590 {"rb_eStandardError", (RUBY_PROC*)&dll_rb_eStandardError},
591 {"rb_eval_string_protect", (RUBY_PROC*)&dll_rb_eval_string_protect},
Bram Moolenaarf711cb22018-08-01 18:42:13 +0200592# ifdef RUBY21_OR_LATER
593 {"rb_funcallv", (RUBY_PROC*)&dll_rb_funcallv},
594# else
595 {"rb_funcall2", (RUBY_PROC*)&dll_rb_funcall2},
596# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000597 {"rb_global_variable", (RUBY_PROC*)&dll_rb_global_variable},
598 {"rb_hash_aset", (RUBY_PROC*)&dll_rb_hash_aset},
599 {"rb_hash_new", (RUBY_PROC*)&dll_rb_hash_new},
600 {"rb_inspect", (RUBY_PROC*)&dll_rb_inspect},
601 {"rb_int2inum", (RUBY_PROC*)&dll_rb_int2inum},
Bram Moolenaarf711cb22018-08-01 18:42:13 +0200602 {"rb_intern", (RUBY_PROC*)&dll_rb_intern},
Bram Moolenaara2aa31a2014-02-23 22:52:40 +0100603# if VIM_SIZEOF_INT < VIM_SIZEOF_LONG /* 64 bits only */
Bram Moolenaar2623b4f2012-09-18 16:36:32 +0200604 {"rb_fix2int", (RUBY_PROC*)&dll_rb_fix2int},
605 {"rb_num2int", (RUBY_PROC*)&dll_rb_num2int},
606 {"rb_num2uint", (RUBY_PROC*)&dll_rb_num2uint},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200607# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000608 {"rb_lastline_get", (RUBY_PROC*)&dll_rb_lastline_get},
609 {"rb_lastline_set", (RUBY_PROC*)&dll_rb_lastline_set},
Bram Moolenaard0573012017-10-28 21:11:06 +0200610 {"rb_protect", (RUBY_PROC*)&dll_rb_protect},
611 {"rb_load", (RUBY_PROC*)&dll_rb_load},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000612 {"rb_num2long", (RUBY_PROC*)&dll_rb_num2long},
613 {"rb_num2ulong", (RUBY_PROC*)&dll_rb_num2ulong},
614 {"rb_obj_alloc", (RUBY_PROC*)&dll_rb_obj_alloc},
615 {"rb_obj_as_string", (RUBY_PROC*)&dll_rb_obj_as_string},
616 {"rb_obj_id", (RUBY_PROC*)&dll_rb_obj_id},
617 {"rb_raise", (RUBY_PROC*)&dll_rb_raise},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200618# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 18
Bram Moolenaarf9b5ef82010-09-29 13:02:53 +0200619 {"rb_string_value", (RUBY_PROC*)&dll_rb_string_value},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200620# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000621 {"rb_str2cstr", (RUBY_PROC*)&dll_rb_str2cstr},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200622# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000623 {"rb_str_cat", (RUBY_PROC*)&dll_rb_str_cat},
624 {"rb_str_concat", (RUBY_PROC*)&dll_rb_str_concat},
625 {"rb_str_new", (RUBY_PROC*)&dll_rb_str_new},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200626# ifdef need_rb_str_new_cstr
Bram Moolenaar1d2beae2010-05-22 21:56:55 +0200627 {"rb_str_new_cstr", (RUBY_PROC*)&dll_rb_str_new_cstr},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200628# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000629 {"rb_str_new2", (RUBY_PROC*)&dll_rb_str_new2},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200630# endif
631# ifdef RUBY19_OR_LATER
Bram Moolenaar165641d2010-02-17 16:23:09 +0100632 {"rb_errinfo", (RUBY_PROC*)&dll_rb_errinfo},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200633# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000634 {"ruby_errinfo", (RUBY_PROC*)&dll_ruby_errinfo},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200635# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000636 {"ruby_init", (RUBY_PROC*)&dll_ruby_init},
637 {"ruby_init_loadpath", (RUBY_PROC*)&dll_ruby_init_loadpath},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200638# ifdef WIN3264
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200639# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER < 19
Bram Moolenaar4f391792017-01-15 16:59:07 +0100640 {"NtInitialize", (RUBY_PROC*)&dll_NtInitialize},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200641# else
Bram Moolenaar4f391792017-01-15 16:59:07 +0100642 {"ruby_sysinit", (RUBY_PROC*)&dll_ruby_sysinit},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200643# endif
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200644# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 18
Bram Moolenaar071d4272004-06-13 20:20:40 +0000645 {"rb_w32_snprintf", (RUBY_PROC*)&dll_rb_w32_snprintf},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200646# endif
Bram Moolenaar3ca71f12010-10-27 16:49:47 +0200647# endif
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200648# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 18
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100649 {"rb_string_value_ptr", (RUBY_PROC*)&dll_rb_string_value_ptr},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200650# if DYNAMIC_RUBY_VER <= 19
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100651 {"rb_float_new", (RUBY_PROC*)&dll_rb_float_new},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200652# else
Bram Moolenaar886ed692013-02-26 13:41:35 +0100653 {"rb_float_new_in_heap", (RUBY_PROC*)&dll_rb_float_new},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200654# endif
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100655 {"rb_ary_new", (RUBY_PROC*)&dll_rb_ary_new},
Bram Moolenaar51e9fbf2018-08-11 14:24:11 +0200656# ifdef RB_ARY_NEW4_MACRO
657 {"rb_ary_new_from_values", (RUBY_PROC*)&dll_rb_ary_new4},
658# else
659 {"rb_ary_new4", (RUBY_PROC*)&dll_rb_ary_new4},
660# endif
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100661 {"rb_ary_push", (RUBY_PROC*)&dll_rb_ary_push},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200662# endif
663# ifdef RUBY19_OR_LATER
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100664 {"rb_int2big", (RUBY_PROC*)&dll_rb_int2big},
Bram Moolenaar165641d2010-02-17 16:23:09 +0100665 {"ruby_script", (RUBY_PROC*)&dll_ruby_script},
666 {"rb_enc_find_index", (RUBY_PROC*)&dll_rb_enc_find_index},
667 {"rb_enc_find", (RUBY_PROC*)&dll_rb_enc_find},
668 {"rb_enc_str_new", (RUBY_PROC*)&dll_rb_enc_str_new},
669 {"rb_sprintf", (RUBY_PROC*)&dll_rb_sprintf},
Bram Moolenaar7a8ef142010-12-24 13:39:35 +0100670 {"rb_require", (RUBY_PROC*)&dll_rb_require},
Bram Moolenaar9b1067e2015-11-19 19:33:15 +0100671 {"ruby_options", (RUBY_PROC*)&dll_ruby_options},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200672# endif
Bram Moolenaar10f3a792013-05-20 12:52:29 +0200673# if defined(RUBY19_OR_LATER) || defined(RUBY_INIT_STACK)
674# ifdef __ia64
675 {"rb_ia64_bsp", (RUBY_PROC*)&dll_rb_ia64_bsp},
676# endif
677 {"ruby_init_stack", (RUBY_PROC*)&dll_ruby_init_stack},
678# endif
Bram Moolenaara1a118b2014-02-05 22:41:15 +0100679# if defined(USE_RGENGC) && USE_RGENGC
Bram Moolenaar0c7485f2015-01-14 14:04:10 +0100680# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER == 21
Bram Moolenaara1a118b2014-02-05 22:41:15 +0100681 {"rb_gc_writebarrier_unprotect_promoted", (RUBY_PROC*)&dll_rb_gc_writebarrier_unprotect_promoted},
Bram Moolenaar0c7485f2015-01-14 14:04:10 +0100682# else
683 {"rb_gc_writebarrier_unprotect", (RUBY_PROC*)&dll_rb_gc_writebarrier_unprotect},
684# endif
Bram Moolenaara1a118b2014-02-05 22:41:15 +0100685# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000686 {"", NULL},
687};
688
689/*
690 * Free ruby.dll
691 */
692 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100693end_dynamic_ruby(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000694{
695 if (hinstRuby)
696 {
Bram Moolenaarf9b5ef82010-09-29 13:02:53 +0200697 close_dll(hinstRuby);
Bram Moolenaar3ca71f12010-10-27 16:49:47 +0200698 hinstRuby = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000699 }
700}
701
702/*
703 * Load library and get all pointers.
704 * Parameter 'libname' provides name of DLL.
705 * Return OK or FAIL.
706 */
707 static int
708ruby_runtime_link_init(char *libname, int verbose)
709{
710 int i;
711
712 if (hinstRuby)
713 return OK;
Bram Moolenaarf9b5ef82010-09-29 13:02:53 +0200714 hinstRuby = load_dll(libname);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000715 if (!hinstRuby)
716 {
717 if (verbose)
718 EMSG2(_(e_loadlib), libname);
719 return FAIL;
720 }
721
722 for (i = 0; ruby_funcname_table[i].ptr; ++i)
723 {
Bram Moolenaarf9b5ef82010-09-29 13:02:53 +0200724 if (!(*ruby_funcname_table[i].ptr = symbol_from_dll(hinstRuby,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000725 ruby_funcname_table[i].name)))
726 {
Bram Moolenaarf9b5ef82010-09-29 13:02:53 +0200727 close_dll(hinstRuby);
Bram Moolenaar3ca71f12010-10-27 16:49:47 +0200728 hinstRuby = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000729 if (verbose)
730 EMSG2(_(e_loadfunc), ruby_funcname_table[i].name);
731 return FAIL;
732 }
733 }
734 return OK;
735}
736
737/*
738 * If ruby is enabled (there is installed ruby on Windows system) return TRUE,
739 * else FALSE.
740 */
741 int
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100742ruby_enabled(int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000743{
Bram Moolenaar25e4fcd2016-01-09 14:57:47 +0100744 return ruby_runtime_link_init((char *)p_rubydll, verbose) == OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000745}
746#endif /* defined(DYNAMIC_RUBY) || defined(PROTO) */
747
748 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100749ruby_end(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000750{
751#ifdef DYNAMIC_RUBY
752 end_dynamic_ruby();
753#endif
754}
755
756void ex_ruby(exarg_T *eap)
757{
758 int state;
759 char *script = NULL;
760
Bram Moolenaar35a2e192006-03-13 22:07:11 +0000761 script = (char *)script_get(eap, eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000762 if (!eap->skip && ensure_ruby_initialized())
763 {
764 if (script == NULL)
765 rb_eval_string_protect((char *)eap->arg, &state);
766 else
767 rb_eval_string_protect(script, &state);
768 if (state)
769 error_print(state);
770 }
771 vim_free(script);
772}
773
Bram Moolenaar165641d2010-02-17 16:23:09 +0100774/*
775 * In Ruby 1.9 or later, ruby String object has encoding.
776 * conversion buffer string of vim to ruby String object using
777 * VIM encoding option.
778 */
779 static VALUE
780vim_str2rb_enc_str(const char *s)
781{
Bram Moolenaar639a2552010-03-17 18:15:23 +0100782#ifdef RUBY19_OR_LATER
Bram Moolenaar165641d2010-02-17 16:23:09 +0100783 int isnum;
784 long lval;
785 char_u *sval;
786 rb_encoding *enc;
787
788 isnum = get_option_value((char_u *)"enc", &lval, &sval, 0);
789 if (isnum == 0)
790 {
791 enc = rb_enc_find((char *)sval);
792 vim_free(sval);
Bram Moolenaar758535a2016-03-30 22:06:16 +0200793 if (enc)
794 {
Bram Moolenaar9b0ac222016-06-01 20:31:43 +0200795 return rb_enc_str_new(s, (long)strlen(s), enc);
Bram Moolenaar165641d2010-02-17 16:23:09 +0100796 }
797 }
798#endif
799 return rb_str_new2(s);
800}
801
802 static VALUE
803eval_enc_string_protect(const char *str, int *state)
804{
Bram Moolenaar639a2552010-03-17 18:15:23 +0100805#ifdef RUBY19_OR_LATER
Bram Moolenaar165641d2010-02-17 16:23:09 +0100806 int isnum;
807 long lval;
808 char_u *sval;
809 rb_encoding *enc;
810 VALUE v;
811
812 isnum = get_option_value((char_u *)"enc", &lval, &sval, 0);
813 if (isnum == 0)
814 {
815 enc = rb_enc_find((char *)sval);
816 vim_free(sval);
817 if (enc)
818 {
819 v = rb_sprintf("#-*- coding:%s -*-\n%s", rb_enc_name(enc), str);
820 return rb_eval_string_protect(StringValuePtr(v), state);
821 }
822 }
823#endif
824 return rb_eval_string_protect(str, state);
825}
826
Bram Moolenaar071d4272004-06-13 20:20:40 +0000827void ex_rubydo(exarg_T *eap)
828{
829 int state;
830 linenr_T i;
Bram Moolenaarc593fee2017-01-29 23:11:25 +0100831 buf_T *was_curbuf = curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000832
833 if (ensure_ruby_initialized())
834 {
835 if (u_save(eap->line1 - 1, eap->line2 + 1) != OK)
836 return;
Bram Moolenaar758535a2016-03-30 22:06:16 +0200837 for (i = eap->line1; i <= eap->line2; i++)
838 {
Bram Moolenaare980d8a2010-12-08 13:11:21 +0100839 VALUE line;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000840
Bram Moolenaarc593fee2017-01-29 23:11:25 +0100841 if (i > curbuf->b_ml.ml_line_count)
842 break;
Bram Moolenaare980d8a2010-12-08 13:11:21 +0100843 line = vim_str2rb_enc_str((char *)ml_get(i));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000844 rb_lastline_set(line);
Bram Moolenaar165641d2010-02-17 16:23:09 +0100845 eval_enc_string_protect((char *) eap->arg, &state);
Bram Moolenaar758535a2016-03-30 22:06:16 +0200846 if (state)
847 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000848 error_print(state);
849 break;
850 }
Bram Moolenaarc593fee2017-01-29 23:11:25 +0100851 if (was_curbuf != curbuf)
852 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000853 line = rb_lastline_get();
Bram Moolenaar758535a2016-03-30 22:06:16 +0200854 if (!NIL_P(line))
855 {
856 if (TYPE(line) != T_STRING)
857 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000858 EMSG(_("E265: $_ must be an instance of String"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000859 return;
860 }
Bram Moolenaar165641d2010-02-17 16:23:09 +0100861 ml_replace(i, (char_u *) StringValuePtr(line), 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000862 changed();
863#ifdef SYNTAX_HL
864 syn_changed(i); /* recompute syntax hl. for this line */
865#endif
866 }
867 }
868 check_cursor();
869 update_curbuf(NOT_VALID);
870 }
871}
872
Bram Moolenaar0b394642018-05-17 13:11:46 +0200873static VALUE rb_load_wrap(VALUE file_to_load)
Bram Moolenaar37badc82018-01-31 20:15:30 +0100874{
875 rb_load(file_to_load, 0);
876 return Qnil;
877}
878
Bram Moolenaar071d4272004-06-13 20:20:40 +0000879void ex_rubyfile(exarg_T *eap)
880{
881 int state;
882
883 if (ensure_ruby_initialized())
884 {
Bram Moolenaar37badc82018-01-31 20:15:30 +0100885 VALUE file_to_load = rb_str_new2((const char *)eap->arg);
886 rb_protect(rb_load_wrap, file_to_load, &state);
887 if (state)
888 error_print(state);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000889 }
890}
891
892void ruby_buffer_free(buf_T *buf)
893{
Bram Moolenaare344bea2005-09-01 20:46:49 +0000894 if (buf->b_ruby_ref)
895 {
896 rb_hash_aset(objtbl, rb_obj_id((VALUE) buf->b_ruby_ref), Qnil);
897 RDATA(buf->b_ruby_ref)->data = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000898 }
899}
900
901void ruby_window_free(win_T *win)
902{
Bram Moolenaare344bea2005-09-01 20:46:49 +0000903 if (win->w_ruby_ref)
904 {
905 rb_hash_aset(objtbl, rb_obj_id((VALUE) win->w_ruby_ref), Qnil);
906 RDATA(win->w_ruby_ref)->data = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000907 }
908}
909
910static int ensure_ruby_initialized(void)
911{
912 if (!ruby_initialized)
913 {
914#ifdef DYNAMIC_RUBY
915 if (ruby_enabled(TRUE))
916 {
917#endif
Bram Moolenaar0b69c732010-02-17 15:11:50 +0100918#ifdef _WIN32
919 /* suggested by Ariya Mizutani */
920 int argc = 1;
921 char *argv[] = {"gvim.exe"};
Bram Moolenaar90140742014-11-27 17:44:08 +0100922 char **argvp = argv;
Bram Moolenaarfe6ce332017-01-14 20:12:01 +0100923# ifdef RUBY19_OR_LATER
924 ruby_sysinit(&argc, &argvp);
925# else
Bram Moolenaar90140742014-11-27 17:44:08 +0100926 NtInitialize(&argc, &argvp);
Bram Moolenaarfe6ce332017-01-14 20:12:01 +0100927# endif
Bram Moolenaar0b69c732010-02-17 15:11:50 +0100928#endif
Bram Moolenaarb2c03502010-07-02 20:20:09 +0200929 {
Bram Moolenaar10f3a792013-05-20 12:52:29 +0200930#if defined(RUBY19_OR_LATER) || defined(RUBY_INIT_STACK)
Bram Moolenaar99685e62013-05-11 13:56:18 +0200931 ruby_init_stack(ruby_stack_start);
Bram Moolenaar165641d2010-02-17 16:23:09 +0100932#endif
Bram Moolenaarb2c03502010-07-02 20:20:09 +0200933 ruby_init();
934 }
Bram Moolenaar639a2552010-03-17 18:15:23 +0100935#ifdef RUBY19_OR_LATER
Bram Moolenaar7a8ef142010-12-24 13:39:35 +0100936 {
937 int dummy_argc = 2;
Bram Moolenaard1bc96ce2017-09-26 21:21:44 +0200938 char *dummy_argv[] = {"vim-ruby", "-e_=0"};
Bram Moolenaar9b1067e2015-11-19 19:33:15 +0100939 ruby_options(dummy_argc, dummy_argv);
Bram Moolenaar7a8ef142010-12-24 13:39:35 +0100940 }
Bram Moolenaar165641d2010-02-17 16:23:09 +0100941 ruby_script("vim-ruby");
Bram Moolenaar7a8ef142010-12-24 13:39:35 +0100942#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000943 ruby_init_loadpath();
Bram Moolenaar165641d2010-02-17 16:23:09 +0100944#endif
Bram Moolenaar7a8ef142010-12-24 13:39:35 +0100945 ruby_io_init();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000946 ruby_vim_init();
947 ruby_initialized = 1;
948#ifdef DYNAMIC_RUBY
949 }
950 else
951 {
952 EMSG(_("E266: Sorry, this command is disabled, the Ruby library could not be loaded."));
953 return 0;
954 }
955#endif
956 }
957 return ruby_initialized;
958}
959
960static void error_print(int state)
961{
962#ifndef DYNAMIC_RUBY
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100963#if !(defined(RUBY_VERSION) && RUBY_VERSION >= 19) \
964 && !(defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 19)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000965 RUBYEXTERN VALUE ruby_errinfo;
966#endif
Bram Moolenaar165641d2010-02-17 16:23:09 +0100967#endif
Bram Moolenaarf711cb22018-08-01 18:42:13 +0200968 VALUE error;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000969 VALUE eclass;
970 VALUE einfo;
Bram Moolenaarf711cb22018-08-01 18:42:13 +0200971 VALUE bt;
972 int attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000973 char buff[BUFSIZ];
Bram Moolenaarf711cb22018-08-01 18:42:13 +0200974 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000975
976#define TAG_RETURN 0x1
977#define TAG_BREAK 0x2
978#define TAG_NEXT 0x3
979#define TAG_RETRY 0x4
980#define TAG_REDO 0x5
981#define TAG_RAISE 0x6
982#define TAG_THROW 0x7
983#define TAG_FATAL 0x8
984#define TAG_MASK 0xf
985
Bram Moolenaar758535a2016-03-30 22:06:16 +0200986 switch (state)
987 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000988 case TAG_RETURN:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000989 EMSG(_("E267: unexpected return"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000990 break;
991 case TAG_NEXT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000992 EMSG(_("E268: unexpected next"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000993 break;
994 case TAG_BREAK:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000995 EMSG(_("E269: unexpected break"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000996 break;
997 case TAG_REDO:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000998 EMSG(_("E270: unexpected redo"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000999 break;
1000 case TAG_RETRY:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001001 EMSG(_("E271: retry outside of rescue clause"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001002 break;
1003 case TAG_RAISE:
1004 case TAG_FATAL:
Bram Moolenaar639a2552010-03-17 18:15:23 +01001005#ifdef RUBY19_OR_LATER
Bram Moolenaarf711cb22018-08-01 18:42:13 +02001006 error = rb_errinfo();
Bram Moolenaar165641d2010-02-17 16:23:09 +01001007#else
Bram Moolenaarf711cb22018-08-01 18:42:13 +02001008 error = ruby_errinfo;
Bram Moolenaar165641d2010-02-17 16:23:09 +01001009#endif
Bram Moolenaarf711cb22018-08-01 18:42:13 +02001010 eclass = CLASS_OF(error);
1011 einfo = rb_obj_as_string(error);
Bram Moolenaar758535a2016-03-30 22:06:16 +02001012 if (eclass == rb_eRuntimeError && RSTRING_LEN(einfo) == 0)
1013 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001014 EMSG(_("E272: unhandled exception"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001015 }
Bram Moolenaar758535a2016-03-30 22:06:16 +02001016 else
1017 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001018 VALUE epath;
1019 char *p;
1020
1021 epath = rb_class_path(eclass);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00001022 vim_snprintf(buff, BUFSIZ, "%s: %s",
Bram Moolenaar165641d2010-02-17 16:23:09 +01001023 RSTRING_PTR(epath), RSTRING_PTR(einfo));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001024 p = strchr(buff, '\n');
1025 if (p) *p = '\0';
1026 EMSG(buff);
1027 }
Bram Moolenaarf711cb22018-08-01 18:42:13 +02001028
1029 attr = syn_name2attr((char_u *)"Error");
1030# ifdef RUBY21_OR_LATER
1031 bt = rb_funcallv(error, rb_intern("backtrace"), 0, 0);
1032 for (i = 0; i < RARRAY_LEN(bt); i++)
1033 msg_attr((char_u *)RSTRING_PTR(RARRAY_AREF(bt, i)), attr);
1034# else
1035 bt = rb_funcall2(error, rb_intern("backtrace"), 0, 0);
1036 for (i = 0; i < RARRAY_LEN(bt); i++)
1037 msg_attr((char_u *)RSTRING_PTR(RARRAY_PTR(bt)[i]), attr);
1038# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001039 break;
1040 default:
Bram Moolenaar9c13b352005-05-19 20:53:52 +00001041 vim_snprintf(buff, BUFSIZ, _("E273: unknown longjmp status %d"), state);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001042 EMSG(buff);
1043 break;
1044 }
1045}
1046
Bram Moolenaarcd8b20a2009-05-22 16:20:57 +00001047static VALUE vim_message(VALUE self UNUSED, VALUE str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001048{
1049 char *buff, *p;
1050
1051 str = rb_obj_as_string(str);
Bram Moolenaar3f5f7952011-08-04 19:34:59 +02001052 if (RSTRING_LEN(str) > 0)
1053 {
1054 /* Only do this when the string isn't empty, alloc(0) causes trouble. */
Bram Moolenaar00ccf542017-09-03 15:17:48 +02001055 buff = ALLOCA_N(char, RSTRING_LEN(str) + 1);
Bram Moolenaar3f5f7952011-08-04 19:34:59 +02001056 strcpy(buff, RSTRING_PTR(str));
1057 p = strchr(buff, '\n');
1058 if (p) *p = '\0';
1059 MSG(buff);
1060 }
1061 else
1062 {
1063 MSG("");
1064 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001065 return Qnil;
1066}
1067
Bram Moolenaarcd8b20a2009-05-22 16:20:57 +00001068static VALUE vim_set_option(VALUE self UNUSED, VALUE str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001069{
Bram Moolenaar165641d2010-02-17 16:23:09 +01001070 do_set((char_u *)StringValuePtr(str), 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001071 update_screen(NOT_VALID);
1072 return Qnil;
1073}
1074
Bram Moolenaarcd8b20a2009-05-22 16:20:57 +00001075static VALUE vim_command(VALUE self UNUSED, VALUE str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001076{
Bram Moolenaar165641d2010-02-17 16:23:09 +01001077 do_cmdline_cmd((char_u *)StringValuePtr(str));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001078 return Qnil;
1079}
1080
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001081#ifdef FEAT_EVAL
1082static VALUE vim_to_ruby(typval_T *tv)
1083{
1084 VALUE result = Qnil;
1085
1086 if (tv->v_type == VAR_STRING)
1087 {
Bram Moolenaar94127e42010-03-19 23:08:48 +01001088 result = rb_str_new2(tv->vval.v_string == NULL
1089 ? "" : (char *)(tv->vval.v_string));
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001090 }
1091 else if (tv->v_type == VAR_NUMBER)
1092 {
Bram Moolenaar639a2552010-03-17 18:15:23 +01001093 result = INT2NUM(tv->vval.v_number);
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001094 }
1095# ifdef FEAT_FLOAT
1096 else if (tv->v_type == VAR_FLOAT)
1097 {
Bram Moolenaar639a2552010-03-17 18:15:23 +01001098 result = rb_float_new(tv->vval.v_float);
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001099 }
1100# endif
1101 else if (tv->v_type == VAR_LIST)
1102 {
Bram Moolenaar639a2552010-03-17 18:15:23 +01001103 list_T *list = tv->vval.v_list;
1104 listitem_T *curr;
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001105
Bram Moolenaar639a2552010-03-17 18:15:23 +01001106 result = rb_ary_new();
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001107
Bram Moolenaar639a2552010-03-17 18:15:23 +01001108 if (list != NULL)
1109 {
1110 for (curr = list->lv_first; curr != NULL; curr = curr->li_next)
1111 {
1112 rb_ary_push(result, vim_to_ruby(&curr->li_tv));
1113 }
1114 }
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001115 }
1116 else if (tv->v_type == VAR_DICT)
1117 {
Bram Moolenaar639a2552010-03-17 18:15:23 +01001118 result = rb_hash_new();
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001119
Bram Moolenaar639a2552010-03-17 18:15:23 +01001120 if (tv->vval.v_dict != NULL)
1121 {
1122 hashtab_T *ht = &tv->vval.v_dict->dv_hashtab;
1123 long_u todo = ht->ht_used;
1124 hashitem_T *hi;
1125 dictitem_T *di;
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001126
Bram Moolenaar639a2552010-03-17 18:15:23 +01001127 for (hi = ht->ht_array; todo > 0; ++hi)
1128 {
1129 if (!HASHITEM_EMPTY(hi))
1130 {
1131 --todo;
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001132
Bram Moolenaar639a2552010-03-17 18:15:23 +01001133 di = dict_lookup(hi);
1134 rb_hash_aset(result, rb_str_new2((char *)hi->hi_key),
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001135 vim_to_ruby(&di->di_tv));
Bram Moolenaar639a2552010-03-17 18:15:23 +01001136 }
1137 }
1138 }
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001139 }
1140 else if (tv->v_type == VAR_SPECIAL)
1141 {
Bram Moolenaard84b26a2018-07-28 17:18:09 +02001142 if (tv->vval.v_number == VVAL_TRUE)
1143 result = Qtrue;
1144 else if (tv->vval.v_number == VVAL_FALSE)
1145 result = Qfalse;
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001146 } /* else return Qnil; */
1147
1148 return result;
1149}
1150#endif
1151
Bram Moolenaarcd8b20a2009-05-22 16:20:57 +00001152static VALUE vim_evaluate(VALUE self UNUSED, VALUE str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001153{
1154#ifdef FEAT_EVAL
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001155 typval_T *tv;
1156 VALUE result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001157
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001158 tv = eval_expr((char_u *)StringValuePtr(str), NULL);
1159 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001160 {
Bram Moolenaar639a2552010-03-17 18:15:23 +01001161 return Qnil;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001162 }
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001163 result = vim_to_ruby(tv);
1164
1165 free_tv(tv);
1166
1167 return result;
1168#else
1169 return Qnil;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001170#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001171}
1172
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001173#ifdef USE_TYPEDDATA
1174static size_t buffer_dsize(const void *buf);
1175
1176static const rb_data_type_t buffer_type = {
1177 "vim_buffer",
1178 {0, 0, buffer_dsize, {0, 0}},
1179 0, 0,
1180# ifdef RUBY_TYPED_FREE_IMMEDIATELY
1181 0,
1182# endif
1183};
1184
1185static size_t buffer_dsize(const void *buf UNUSED)
1186{
1187 return sizeof(buf_T);
1188}
1189#endif
1190
Bram Moolenaar071d4272004-06-13 20:20:40 +00001191static VALUE buffer_new(buf_T *buf)
1192{
Bram Moolenaare344bea2005-09-01 20:46:49 +00001193 if (buf->b_ruby_ref)
1194 {
1195 return (VALUE) buf->b_ruby_ref;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001196 }
Bram Moolenaare344bea2005-09-01 20:46:49 +00001197 else
1198 {
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001199#ifdef USE_TYPEDDATA
1200 VALUE obj = TypedData_Wrap_Struct(cBuffer, &buffer_type, buf);
1201#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001202 VALUE obj = Data_Wrap_Struct(cBuffer, 0, 0, buf);
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001203#endif
Bram Moolenaare344bea2005-09-01 20:46:49 +00001204 buf->b_ruby_ref = (void *) obj;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001205 rb_hash_aset(objtbl, rb_obj_id(obj), obj);
1206 return obj;
1207 }
1208}
1209
1210static buf_T *get_buf(VALUE obj)
1211{
1212 buf_T *buf;
1213
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001214#ifdef USE_TYPEDDATA
1215 TypedData_Get_Struct(obj, buf_T, &buffer_type, buf);
1216#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001217 Data_Get_Struct(obj, buf_T, buf);
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001218#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001219 if (buf == NULL)
1220 rb_raise(eDeletedBufferError, "attempt to refer to deleted buffer");
1221 return buf;
1222}
1223
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001224static VALUE buffer_s_current(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001225{
1226 return buffer_new(curbuf);
1227}
1228
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001229static VALUE buffer_s_count(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001230{
1231 buf_T *b;
1232 int n = 0;
1233
Bram Moolenaar29323592016-07-24 22:04:11 +02001234 FOR_ALL_BUFFERS(b)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001235 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001236 /* Deleted buffers should not be counted
1237 * SegPhault - 01/07/05 */
1238 if (b->b_p_bl)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001239 n++;
1240 }
1241
Bram Moolenaar071d4272004-06-13 20:20:40 +00001242 return INT2NUM(n);
1243}
1244
Bram Moolenaarcd8b20a2009-05-22 16:20:57 +00001245static VALUE buffer_s_aref(VALUE self UNUSED, VALUE num)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001246{
1247 buf_T *b;
1248 int n = NUM2INT(num);
1249
Bram Moolenaar29323592016-07-24 22:04:11 +02001250 FOR_ALL_BUFFERS(b)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001251 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001252 /* Deleted buffers should not be counted
1253 * SegPhault - 01/07/05 */
1254 if (!b->b_p_bl)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001255 continue;
1256
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001257 if (n == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001258 return buffer_new(b);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001259
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001260 n--;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001261 }
1262 return Qnil;
1263}
1264
1265static VALUE buffer_name(VALUE self)
1266{
1267 buf_T *buf = get_buf(self);
1268
Bram Moolenaar35a2e192006-03-13 22:07:11 +00001269 return buf->b_ffname ? rb_str_new2((char *)buf->b_ffname) : Qnil;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001270}
1271
1272static VALUE buffer_number(VALUE self)
1273{
1274 buf_T *buf = get_buf(self);
1275
1276 return INT2NUM(buf->b_fnum);
1277}
1278
1279static VALUE buffer_count(VALUE self)
1280{
1281 buf_T *buf = get_buf(self);
1282
1283 return INT2NUM(buf->b_ml.ml_line_count);
1284}
1285
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001286static VALUE get_buffer_line(buf_T *buf, linenr_T n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001287{
Bram Moolenaar3c531602010-11-16 14:46:19 +01001288 if (n <= 0 || n > buf->b_ml.ml_line_count)
1289 rb_raise(rb_eIndexError, "line number %ld out of range", (long)n);
1290 return vim_str2rb_enc_str((char *)ml_get_buf(buf, n, FALSE));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001291}
1292
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001293static VALUE buffer_aref(VALUE self, VALUE num)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001294{
1295 buf_T *buf = get_buf(self);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001296
1297 if (buf != NULL)
1298 return get_buffer_line(buf, (linenr_T)NUM2LONG(num));
1299 return Qnil; /* For stop warning */
1300}
1301
1302static VALUE set_buffer_line(buf_T *buf, linenr_T n, VALUE str)
1303{
Bram Moolenaar165641d2010-02-17 16:23:09 +01001304 char *line = StringValuePtr(str);
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001305 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001306
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001307 if (n > 0 && n <= buf->b_ml.ml_line_count && line != NULL)
1308 {
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001309 /* set curwin/curbuf for "buf" and save some things */
1310 aucmd_prepbuf(&aco, buf);
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001311
Bram Moolenaar758535a2016-03-30 22:06:16 +02001312 if (u_savesub(n) == OK)
1313 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001314 ml_replace(n, (char_u *)line, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001315 changed();
1316#ifdef SYNTAX_HL
1317 syn_changed(n); /* recompute syntax hl. for this line */
1318#endif
1319 }
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001320
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001321 /* restore curwin/curbuf and a few other things */
1322 aucmd_restbuf(&aco);
1323 /* Careful: autocommands may have made "buf" invalid! */
Bram Moolenaarf30e74c2006-08-16 17:35:00 +00001324
Bram Moolenaar071d4272004-06-13 20:20:40 +00001325 update_curbuf(NOT_VALID);
1326 }
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001327 else
1328 {
Bram Moolenaar165641d2010-02-17 16:23:09 +01001329 rb_raise(rb_eIndexError, "line number %ld out of range", (long)n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001330 }
1331 return str;
1332}
1333
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001334static VALUE buffer_aset(VALUE self, VALUE num, VALUE str)
1335{
1336 buf_T *buf = get_buf(self);
1337
1338 if (buf != NULL)
1339 return set_buffer_line(buf, (linenr_T)NUM2LONG(num), str);
1340 return str;
1341}
1342
Bram Moolenaar071d4272004-06-13 20:20:40 +00001343static VALUE buffer_delete(VALUE self, VALUE num)
1344{
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001345 buf_T *buf = get_buf(self);
1346 long n = NUM2LONG(num);
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001347 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001348
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001349 if (n > 0 && n <= buf->b_ml.ml_line_count)
1350 {
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001351 /* set curwin/curbuf for "buf" and save some things */
1352 aucmd_prepbuf(&aco, buf);
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001353
Bram Moolenaar758535a2016-03-30 22:06:16 +02001354 if (u_savedel(n, 1) == OK)
1355 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001356 ml_delete(n, 0);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001357
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001358 /* Changes to non-active buffers should properly refresh
1359 * SegPhault - 01/09/05 */
1360 deleted_lines_mark(n, 1L);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001361
Bram Moolenaar071d4272004-06-13 20:20:40 +00001362 changed();
1363 }
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001364
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001365 /* restore curwin/curbuf and a few other things */
1366 aucmd_restbuf(&aco);
1367 /* Careful: autocommands may have made "buf" invalid! */
Bram Moolenaarf30e74c2006-08-16 17:35:00 +00001368
Bram Moolenaar071d4272004-06-13 20:20:40 +00001369 update_curbuf(NOT_VALID);
1370 }
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001371 else
1372 {
Bram Moolenaar165641d2010-02-17 16:23:09 +01001373 rb_raise(rb_eIndexError, "line number %ld out of range", n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001374 }
1375 return Qnil;
1376}
1377
1378static VALUE buffer_append(VALUE self, VALUE num, VALUE str)
1379{
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001380 buf_T *buf = get_buf(self);
Bram Moolenaar165641d2010-02-17 16:23:09 +01001381 char *line = StringValuePtr(str);
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001382 long n = NUM2LONG(num);
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001383 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001384
Bram Moolenaar3c531602010-11-16 14:46:19 +01001385 if (line == NULL)
1386 {
Bram Moolenaar165641d2010-02-17 16:23:09 +01001387 rb_raise(rb_eIndexError, "NULL line");
1388 }
1389 else if (n >= 0 && n <= buf->b_ml.ml_line_count)
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001390 {
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001391 /* set curwin/curbuf for "buf" and save some things */
1392 aucmd_prepbuf(&aco, buf);
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001393
Bram Moolenaar758535a2016-03-30 22:06:16 +02001394 if (u_inssub(n + 1) == OK)
1395 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001396 ml_append(n, (char_u *) line, (colnr_T) 0, FALSE);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001397
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001398 /* Changes to non-active buffers should properly refresh screen
1399 * SegPhault - 12/20/04 */
1400 appended_lines_mark(n, 1L);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001401
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001402 changed();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001403 }
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001404
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001405 /* restore curwin/curbuf and a few other things */
1406 aucmd_restbuf(&aco);
1407 /* Careful: autocommands may have made "buf" invalid! */
Bram Moolenaarf30e74c2006-08-16 17:35:00 +00001408
Bram Moolenaar071d4272004-06-13 20:20:40 +00001409 update_curbuf(NOT_VALID);
1410 }
Bram Moolenaar3c531602010-11-16 14:46:19 +01001411 else
1412 {
Bram Moolenaar165641d2010-02-17 16:23:09 +01001413 rb_raise(rb_eIndexError, "line number %ld out of range", n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001414 }
1415 return str;
1416}
1417
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001418#ifdef USE_TYPEDDATA
1419static size_t window_dsize(const void *buf);
1420
1421static const rb_data_type_t window_type = {
1422 "vim_window",
1423 {0, 0, window_dsize, {0, 0}},
1424 0, 0,
1425# ifdef RUBY_TYPED_FREE_IMMEDIATELY
1426 0,
1427# endif
1428};
1429
1430static size_t window_dsize(const void *win UNUSED)
1431{
1432 return sizeof(win_T);
1433}
1434#endif
1435
Bram Moolenaar071d4272004-06-13 20:20:40 +00001436static VALUE window_new(win_T *win)
1437{
Bram Moolenaare344bea2005-09-01 20:46:49 +00001438 if (win->w_ruby_ref)
1439 {
1440 return (VALUE) win->w_ruby_ref;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001441 }
Bram Moolenaare344bea2005-09-01 20:46:49 +00001442 else
1443 {
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001444#ifdef USE_TYPEDDATA
1445 VALUE obj = TypedData_Wrap_Struct(cVimWindow, &window_type, win);
1446#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001447 VALUE obj = Data_Wrap_Struct(cVimWindow, 0, 0, win);
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001448#endif
Bram Moolenaare344bea2005-09-01 20:46:49 +00001449 win->w_ruby_ref = (void *) obj;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001450 rb_hash_aset(objtbl, rb_obj_id(obj), obj);
1451 return obj;
1452 }
1453}
1454
1455static win_T *get_win(VALUE obj)
1456{
1457 win_T *win;
1458
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001459#ifdef USE_TYPEDDATA
1460 TypedData_Get_Struct(obj, win_T, &window_type, win);
1461#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001462 Data_Get_Struct(obj, win_T, win);
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001463#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001464 if (win == NULL)
1465 rb_raise(eDeletedWindowError, "attempt to refer to deleted window");
1466 return win;
1467}
1468
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001469static VALUE window_s_current(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001470{
1471 return window_new(curwin);
1472}
1473
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001474/*
1475 * Added line manipulation functions
1476 * SegPhault - 03/07/05
1477 */
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001478static VALUE line_s_current(void)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001479{
1480 return get_buffer_line(curbuf, curwin->w_cursor.lnum);
1481}
1482
Bram Moolenaarcd8b20a2009-05-22 16:20:57 +00001483static VALUE set_current_line(VALUE self UNUSED, VALUE str)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001484{
1485 return set_buffer_line(curbuf, curwin->w_cursor.lnum, str);
1486}
1487
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001488static VALUE current_line_number(void)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001489{
1490 return INT2FIX((int)curwin->w_cursor.lnum);
1491}
1492
1493
1494
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001495static VALUE window_s_count(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001496{
Bram Moolenaar071d4272004-06-13 20:20:40 +00001497 win_T *w;
1498 int n = 0;
1499
Bram Moolenaar29323592016-07-24 22:04:11 +02001500 FOR_ALL_WINDOWS(w)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001501 n++;
1502 return INT2NUM(n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001503}
1504
Bram Moolenaarcd8b20a2009-05-22 16:20:57 +00001505static VALUE window_s_aref(VALUE self UNUSED, VALUE num)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001506{
1507 win_T *w;
1508 int n = NUM2INT(num);
1509
Bram Moolenaar071d4272004-06-13 20:20:40 +00001510 for (w = firstwin; w != NULL; w = w->w_next, --n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001511 if (n == 0)
1512 return window_new(w);
1513 return Qnil;
1514}
1515
1516static VALUE window_buffer(VALUE self)
1517{
1518 win_T *win = get_win(self);
1519
1520 return buffer_new(win->w_buffer);
1521}
1522
1523static VALUE window_height(VALUE self)
1524{
1525 win_T *win = get_win(self);
1526
1527 return INT2NUM(win->w_height);
1528}
1529
1530static VALUE window_set_height(VALUE self, VALUE height)
1531{
1532 win_T *win = get_win(self);
1533 win_T *savewin = curwin;
1534
1535 curwin = win;
1536 win_setheight(NUM2INT(height));
1537 curwin = savewin;
1538 return height;
1539}
1540
Bram Moolenaarfeeaa682013-02-14 22:19:51 +01001541static VALUE window_width(VALUE self UNUSED)
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001542{
Bram Moolenaare745d752017-09-22 16:56:22 +02001543 return INT2NUM(get_win(self)->w_width);
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001544}
1545
Bram Moolenaarfeeaa682013-02-14 22:19:51 +01001546static VALUE window_set_width(VALUE self UNUSED, VALUE width)
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001547{
1548 win_T *win = get_win(self);
1549 win_T *savewin = curwin;
1550
1551 curwin = win;
1552 win_setwidth(NUM2INT(width));
1553 curwin = savewin;
1554 return width;
1555}
1556
Bram Moolenaar071d4272004-06-13 20:20:40 +00001557static VALUE window_cursor(VALUE self)
1558{
1559 win_T *win = get_win(self);
1560
1561 return rb_assoc_new(INT2NUM(win->w_cursor.lnum), INT2NUM(win->w_cursor.col));
1562}
1563
1564static VALUE window_set_cursor(VALUE self, VALUE pos)
1565{
1566 VALUE lnum, col;
1567 win_T *win = get_win(self);
1568
1569 Check_Type(pos, T_ARRAY);
Bram Moolenaar165641d2010-02-17 16:23:09 +01001570 if (RARRAY_LEN(pos) != 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001571 rb_raise(rb_eArgError, "array length must be 2");
Bram Moolenaar165641d2010-02-17 16:23:09 +01001572 lnum = RARRAY_PTR(pos)[0];
1573 col = RARRAY_PTR(pos)[1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001574 win->w_cursor.lnum = NUM2LONG(lnum);
1575 win->w_cursor.col = NUM2UINT(col);
Bram Moolenaar53901442018-07-25 22:02:36 +02001576 win->w_set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001577 check_cursor(); /* put cursor on an existing line */
1578 update_screen(NOT_VALID);
1579 return Qnil;
1580}
1581
Bram Moolenaar6217cdc2012-04-25 12:28:09 +02001582static VALUE f_nop(VALUE self UNUSED)
Bram Moolenaar35df7d22012-04-20 18:05:47 +02001583{
1584 return Qnil;
1585}
1586
Bram Moolenaarcd8b20a2009-05-22 16:20:57 +00001587static VALUE f_p(int argc, VALUE *argv, VALUE self UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001588{
1589 int i;
1590 VALUE str = rb_str_new("", 0);
Bram Moolenaar51e9fbf2018-08-11 14:24:11 +02001591 VALUE ret = Qnil;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001592
Bram Moolenaar758535a2016-03-30 22:06:16 +02001593 for (i = 0; i < argc; i++)
1594 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001595 if (i > 0) rb_str_cat(str, ", ", 2);
1596 rb_str_concat(str, rb_inspect(argv[i]));
1597 }
Bram Moolenaar165641d2010-02-17 16:23:09 +01001598 MSG(RSTRING_PTR(str));
Bram Moolenaar51e9fbf2018-08-11 14:24:11 +02001599
1600 if (argc == 1)
1601 ret = argv[0];
1602 else if (argc > 1)
1603 ret = rb_ary_new4(argc, argv);
1604 return ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001605}
1606
1607static void ruby_io_init(void)
1608{
1609#ifndef DYNAMIC_RUBY
1610 RUBYEXTERN VALUE rb_stdout;
Bram Moolenaarb6c8cd82018-07-24 05:41:30 +02001611 RUBYEXTERN VALUE rb_stderr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001612#endif
1613
1614 rb_stdout = rb_obj_alloc(rb_cObject);
Bram Moolenaarb6c8cd82018-07-24 05:41:30 +02001615 rb_stderr = rb_obj_alloc(rb_cObject);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001616 rb_define_singleton_method(rb_stdout, "write", vim_message, 1);
Bram Moolenaar35df7d22012-04-20 18:05:47 +02001617 rb_define_singleton_method(rb_stdout, "flush", f_nop, 0);
Bram Moolenaarb6c8cd82018-07-24 05:41:30 +02001618 rb_define_singleton_method(rb_stderr, "write", vim_message, 1);
1619 rb_define_singleton_method(rb_stderr, "flush", f_nop, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001620 rb_define_global_function("p", f_p, -1);
1621}
1622
1623static void ruby_vim_init(void)
1624{
1625 objtbl = rb_hash_new();
1626 rb_global_variable(&objtbl);
1627
Bram Moolenaarf711faf2007-05-10 16:48:19 +00001628 /* The Vim module used to be called "VIM", but "Vim" is better. Make an
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02001629 * alias "VIM" for backwards compatibility. */
Bram Moolenaarf711faf2007-05-10 16:48:19 +00001630 mVIM = rb_define_module("Vim");
1631 rb_define_const(rb_cObject, "VIM", mVIM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001632 rb_define_const(mVIM, "VERSION_MAJOR", INT2NUM(VIM_VERSION_MAJOR));
1633 rb_define_const(mVIM, "VERSION_MINOR", INT2NUM(VIM_VERSION_MINOR));
1634 rb_define_const(mVIM, "VERSION_BUILD", INT2NUM(VIM_VERSION_BUILD));
1635 rb_define_const(mVIM, "VERSION_PATCHLEVEL", INT2NUM(VIM_VERSION_PATCHLEVEL));
1636 rb_define_const(mVIM, "VERSION_SHORT", rb_str_new2(VIM_VERSION_SHORT));
1637 rb_define_const(mVIM, "VERSION_MEDIUM", rb_str_new2(VIM_VERSION_MEDIUM));
1638 rb_define_const(mVIM, "VERSION_LONG", rb_str_new2(VIM_VERSION_LONG));
1639 rb_define_const(mVIM, "VERSION_LONG_DATE", rb_str_new2(VIM_VERSION_LONG_DATE));
1640 rb_define_module_function(mVIM, "message", vim_message, 1);
1641 rb_define_module_function(mVIM, "set_option", vim_set_option, 1);
1642 rb_define_module_function(mVIM, "command", vim_command, 1);
1643 rb_define_module_function(mVIM, "evaluate", vim_evaluate, 1);
1644
1645 eDeletedBufferError = rb_define_class_under(mVIM, "DeletedBufferError",
1646 rb_eStandardError);
1647 eDeletedWindowError = rb_define_class_under(mVIM, "DeletedWindowError",
1648 rb_eStandardError);
1649
1650 cBuffer = rb_define_class_under(mVIM, "Buffer", rb_cObject);
1651 rb_define_singleton_method(cBuffer, "current", buffer_s_current, 0);
1652 rb_define_singleton_method(cBuffer, "count", buffer_s_count, 0);
1653 rb_define_singleton_method(cBuffer, "[]", buffer_s_aref, 1);
1654 rb_define_method(cBuffer, "name", buffer_name, 0);
1655 rb_define_method(cBuffer, "number", buffer_number, 0);
1656 rb_define_method(cBuffer, "count", buffer_count, 0);
1657 rb_define_method(cBuffer, "length", buffer_count, 0);
1658 rb_define_method(cBuffer, "[]", buffer_aref, 1);
1659 rb_define_method(cBuffer, "[]=", buffer_aset, 2);
1660 rb_define_method(cBuffer, "delete", buffer_delete, 1);
1661 rb_define_method(cBuffer, "append", buffer_append, 2);
1662
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001663 /* Added line manipulation functions
1664 * SegPhault - 03/07/05 */
1665 rb_define_method(cBuffer, "line_number", current_line_number, 0);
1666 rb_define_method(cBuffer, "line", line_s_current, 0);
1667 rb_define_method(cBuffer, "line=", set_current_line, 1);
1668
1669
Bram Moolenaar071d4272004-06-13 20:20:40 +00001670 cVimWindow = rb_define_class_under(mVIM, "Window", rb_cObject);
1671 rb_define_singleton_method(cVimWindow, "current", window_s_current, 0);
1672 rb_define_singleton_method(cVimWindow, "count", window_s_count, 0);
1673 rb_define_singleton_method(cVimWindow, "[]", window_s_aref, 1);
1674 rb_define_method(cVimWindow, "buffer", window_buffer, 0);
1675 rb_define_method(cVimWindow, "height", window_height, 0);
1676 rb_define_method(cVimWindow, "height=", window_set_height, 1);
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001677 rb_define_method(cVimWindow, "width", window_width, 0);
1678 rb_define_method(cVimWindow, "width=", window_set_width, 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001679 rb_define_method(cVimWindow, "cursor", window_cursor, 0);
1680 rb_define_method(cVimWindow, "cursor=", window_set_cursor, 1);
1681
1682 rb_define_virtual_variable("$curbuf", buffer_s_current, 0);
1683 rb_define_virtual_variable("$curwin", window_s_current, 0);
1684}
Bram Moolenaar99685e62013-05-11 13:56:18 +02001685
1686void vim_ruby_init(void *stack_start)
1687{
1688 /* should get machine stack start address early in main function */
1689 ruby_stack_start = stack_start;
1690}