blob: 5180ed8d0eeed5ec23994e51c70bc839801cfd5a [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)
54# define rb_cSymbol (*dll_rb_cSymbol)
55# define rb_cTrueClass (*dll_rb_cTrueClass)
56# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 18
57/*
Bram Moolenaar42d57f02010-03-10 12:47:00 +010058 * On ver 1.8, all Ruby functions are exported with "__declspec(dllimport)"
59 * in ruby.h. But it causes trouble for these variables, because it is
Bram Moolenaar071d4272004-06-13 20:20:40 +000060 * defined in this file. When defined this RUBY_EXPORT it modified to
61 * "extern" and be able to avoid this problem.
62 */
63# define RUBY_EXPORT
64# endif
Bram Moolenaarf9b5ef82010-09-29 13:02:53 +020065
Bram Moolenaar2d73ff42010-10-27 17:40:59 +020066#if !(defined(WIN32) || defined(_WIN64))
Bram Moolenaarf9b5ef82010-09-29 13:02:53 +020067# include <dlfcn.h>
Bram Moolenaar3ca71f12010-10-27 16:49:47 +020068# define HINSTANCE void*
69# define RUBY_PROC void*
Bram Moolenaarf9b5ef82010-09-29 13:02:53 +020070# define load_dll(n) dlopen((n), RTLD_LAZY|RTLD_GLOBAL)
71# define symbol_from_dll dlsym
72# define close_dll dlclose
73#else
Bram Moolenaar3ca71f12010-10-27 16:49:47 +020074# define RUBY_PROC FARPROC
Bram Moolenaarebbcb822010-10-23 14:02:54 +020075# define load_dll vimLoadLib
Bram Moolenaarf9b5ef82010-09-29 13:02:53 +020076# define symbol_from_dll GetProcAddress
77# define close_dll FreeLibrary
Bram Moolenaar071d4272004-06-13 20:20:40 +000078#endif
79
Bram Moolenaarf9b5ef82010-09-29 13:02:53 +020080#endif /* ifdef DYNAMIC_RUBY */
81
Bram Moolenaar0b69c732010-02-17 15:11:50 +010082/* suggested by Ariya Mizutani */
83#if (_MSC_VER == 1200)
84# undef _WIN32_WINNT
85#endif
86
Bram Moolenaar639a2552010-03-17 18:15:23 +010087#if (defined(RUBY_VERSION) && RUBY_VERSION >= 19) \
88 || (defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 19)
89# define RUBY19_OR_LATER 1
90#endif
91
Bram Moolenaar0d27f642015-12-28 22:05:28 +010092#if (defined(RUBY_VERSION) && RUBY_VERSION >= 20) \
93 || (defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 20)
94# define RUBY20_OR_LATER 1
95#endif
96
Bram Moolenaarf711cb22018-08-01 18:42:13 +020097#if (defined(RUBY_VERSION) && RUBY_VERSION >= 21) \
98 || (defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 21)
99# define RUBY21_OR_LATER 1
100#endif
101
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100102#if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 19
103/* Ruby 1.9 defines a number of static functions which use rb_num2long and
104 * rb_int2big */
105# define rb_num2long rb_num2long_stub
106# define rb_int2big rb_int2big_stub
107#endif
108
Bram Moolenaar498af702014-03-28 21:58:21 +0100109#if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 19 \
110 && VIM_SIZEOF_INT < VIM_SIZEOF_LONG
111/* Ruby 1.9 defines a number of static functions which use rb_fix2int and
Bram Moolenaara2aa31a2014-02-23 22:52:40 +0100112 * rb_num2int if VIM_SIZEOF_INT < VIM_SIZEOF_LONG (64bit) */
Bram Moolenaar0bcdd6e2013-04-14 16:19:03 +0200113# define rb_fix2int rb_fix2int_stub
114# define rb_num2int rb_num2int_stub
115#endif
116
Bram Moolenaar0c7485f2015-01-14 14:04:10 +0100117#if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER == 21
Bram Moolenaara1a118b2014-02-05 22:41:15 +0100118/* Ruby 2.1 adds new GC called RGenGC and RARRAY_PTR uses
119 * rb_gc_writebarrier_unprotect_promoted if USE_RGENGC */
Bram Moolenaar90140742014-11-27 17:44:08 +0100120# define rb_gc_writebarrier_unprotect_promoted rb_gc_writebarrier_unprotect_promoted_stub
121#endif
Bram Moolenaar0c7485f2015-01-14 14:04:10 +0100122#if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 22
123# define rb_gc_writebarrier_unprotect rb_gc_writebarrier_unprotect_stub
Bram Moolenaar0c7485f2015-01-14 14:04:10 +0100124#endif
Bram Moolenaara1a118b2014-02-05 22:41:15 +0100125
Bram Moolenaar071d4272004-06-13 20:20:40 +0000126#include <ruby.h>
Bram Moolenaar639a2552010-03-17 18:15:23 +0100127#ifdef RUBY19_OR_LATER
Bram Moolenaar165641d2010-02-17 16:23:09 +0100128# include <ruby/encoding.h>
129#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000130
Bram Moolenaar7a8ef142010-12-24 13:39:35 +0100131#undef off_t /* ruby defines off_t as _int64, Mingw uses long */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000132#undef EXTERN
133#undef _
134
135/* T_DATA defined both by Ruby and Mac header files, hack around it... */
Bram Moolenaard0573012017-10-28 21:11:06 +0200136#if defined(MACOS_X)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000137# define __OPENTRANSPORT__
138# define __OPENTRANSPORTPROTOCOL__
139# define __OPENTRANSPORTPROVIDERS__
140#endif
141
Bram Moolenaar165641d2010-02-17 16:23:09 +0100142/*
Bram Moolenaar0d27f642015-12-28 22:05:28 +0100143 * The TypedData_XXX macro family can be used since Ruby 1.9.2 but
144 * rb_data_type_t changed in 1.9.3, therefore require at least 2.0.
145 * The old Data_XXX macro family was deprecated on Ruby 2.2.
Bram Moolenaarf2f6d292015-12-28 20:57:10 +0100146 * Use TypedData_XXX if available.
147 */
Bram Moolenaar0d27f642015-12-28 22:05:28 +0100148#if defined(TypedData_Wrap_Struct) && defined(RUBY20_OR_LATER)
Bram Moolenaarf2f6d292015-12-28 20:57:10 +0100149# define USE_TYPEDDATA 1
150#endif
151
152/*
Bram Moolenaar84a05ac2013-05-06 04:24:17 +0200153 * Backward compatibility for Ruby 1.8 and earlier.
Bram Moolenaar165641d2010-02-17 16:23:09 +0100154 * Ruby 1.9 does not provide STR2CSTR, instead StringValuePtr is provided.
155 * Ruby 1.9 does not provide RXXX(s)->len and RXXX(s)->ptr, instead
156 * RXXX_LEN(s) and RXXX_PTR(s) are provided.
157 */
158#ifndef StringValuePtr
159# define StringValuePtr(s) STR2CSTR(s)
160#endif
161#ifndef RARRAY_LEN
162# define RARRAY_LEN(s) RARRAY(s)->len
163#endif
164#ifndef RARRAY_PTR
165# define RARRAY_PTR(s) RARRAY(s)->ptr
166#endif
167#ifndef RSTRING_LEN
168# define RSTRING_LEN(s) RSTRING(s)->len
169#endif
170#ifndef RSTRING_PTR
171# define RSTRING_PTR(s) RSTRING(s)->ptr
172#endif
173
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100174#ifdef HAVE_DUP
175# undef HAVE_DUP
176#endif
177
Bram Moolenaar071d4272004-06-13 20:20:40 +0000178#include "vim.h"
179#include "version.h"
180
181#if defined(PROTO) && !defined(FEAT_RUBY)
182/* Define these to be able to generate the function prototypes. */
183# define VALUE int
184# define RUBY_DATA_FUNC int
185#endif
186
187static int ruby_initialized = 0;
Bram Moolenaar99685e62013-05-11 13:56:18 +0200188static void *ruby_stack_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000189static VALUE objtbl;
190
191static VALUE mVIM;
192static VALUE cBuffer;
193static VALUE cVimWindow;
194static VALUE eDeletedBufferError;
195static VALUE eDeletedWindowError;
196
197static int ensure_ruby_initialized(void);
198static void error_print(int);
199static void ruby_io_init(void);
200static void ruby_vim_init(void);
201
Bram Moolenaar10f3a792013-05-20 12:52:29 +0200202#if defined(RUBY19_OR_LATER) || defined(RUBY_INIT_STACK)
203# if defined(__ia64) && !defined(ruby_init_stack)
204# define ruby_init_stack(addr) ruby_init_stack((addr), rb_ia64_bsp())
205# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000206#endif
207
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200208#if defined(DYNAMIC_RUBY) || defined(PROTO)
Bram Moolenaaref269542016-01-19 13:22:12 +0100209# if defined(PROTO) && !defined(HINSTANCE)
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200210# define HINSTANCE int /* for generating prototypes */
211# endif
212
Bram Moolenaar071d4272004-06-13 20:20:40 +0000213/*
214 * Wrapper defines
215 */
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200216# define rb_assoc_new dll_rb_assoc_new
217# define rb_cObject (*dll_rb_cObject)
Bram Moolenaarf2f6d292015-12-28 20:57:10 +0100218# define rb_check_type dll_rb_check_type
219# ifdef USE_TYPEDDATA
220# define rb_check_typeddata dll_rb_check_typeddata
Bram Moolenaar0c7485f2015-01-14 14:04:10 +0100221# endif
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200222# define rb_class_path dll_rb_class_path
Bram Moolenaarf2f6d292015-12-28 20:57:10 +0100223# ifdef USE_TYPEDDATA
224# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 23
225# define rb_data_typed_object_wrap dll_rb_data_typed_object_wrap
226# else
227# define rb_data_typed_object_alloc dll_rb_data_typed_object_alloc
228# endif
229# else
230# define rb_data_object_alloc dll_rb_data_object_alloc
231# endif
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200232# define rb_define_class_under dll_rb_define_class_under
233# define rb_define_const dll_rb_define_const
234# define rb_define_global_function dll_rb_define_global_function
235# define rb_define_method dll_rb_define_method
236# define rb_define_module dll_rb_define_module
237# define rb_define_module_function dll_rb_define_module_function
238# define rb_define_singleton_method dll_rb_define_singleton_method
239# define rb_define_virtual_variable dll_rb_define_virtual_variable
240# define rb_stdout (*dll_rb_stdout)
Bram Moolenaarb6c8cd82018-07-24 05:41:30 +0200241# define rb_stderr (*dll_rb_stderr)
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200242# define rb_eArgError (*dll_rb_eArgError)
243# define rb_eIndexError (*dll_rb_eIndexError)
244# define rb_eRuntimeError (*dll_rb_eRuntimeError)
245# define rb_eStandardError (*dll_rb_eStandardError)
246# define rb_eval_string_protect dll_rb_eval_string_protect
Bram Moolenaarf711cb22018-08-01 18:42:13 +0200247# ifdef RUBY21_OR_LATER
248# define rb_funcallv dll_rb_funcallv
249# else
250# define rb_funcall2 dll_rb_funcall2
251# endif
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200252# define rb_global_variable dll_rb_global_variable
253# define rb_hash_aset dll_rb_hash_aset
254# define rb_hash_new dll_rb_hash_new
255# define rb_inspect dll_rb_inspect
256# define rb_int2inum dll_rb_int2inum
Bram Moolenaar218beb32018-08-04 17:24:44 +0200257
258// ruby.h may redefine rb_intern to use RUBY_CONST_ID_CACHE(), but that won't
259// work. Not using the cache appears to be the best solution.
260# undef rb_intern
261# define rb_intern dll_rb_intern
262
Bram Moolenaara2aa31a2014-02-23 22:52:40 +0100263# if VIM_SIZEOF_INT < VIM_SIZEOF_LONG /* 64 bits only */
Bram Moolenaar498af702014-03-28 21:58:21 +0100264# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER <= 18
265# define rb_fix2int dll_rb_fix2int
266# define rb_num2int dll_rb_num2int
267# endif
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200268# define rb_num2uint dll_rb_num2uint
269# endif
270# define rb_lastline_get dll_rb_lastline_get
271# define rb_lastline_set dll_rb_lastline_set
Bram Moolenaard0573012017-10-28 21:11:06 +0200272# define rb_protect dll_rb_protect
273# define rb_load dll_rb_load
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200274# ifndef RUBY19_OR_LATER
275# define rb_num2long dll_rb_num2long
276# endif
277# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER <= 19
278# define rb_num2ulong dll_rb_num2ulong
279# endif
280# define rb_obj_alloc dll_rb_obj_alloc
281# define rb_obj_as_string dll_rb_obj_as_string
282# define rb_obj_id dll_rb_obj_id
283# define rb_raise dll_rb_raise
284# define rb_str_cat dll_rb_str_cat
285# define rb_str_concat dll_rb_str_concat
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100286# undef rb_str_new
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200287# define rb_str_new dll_rb_str_new
288# ifdef rb_str_new2
Bram Moolenaar1d2beae2010-05-22 21:56:55 +0200289/* Ruby may #define rb_str_new2 to use rb_str_new_cstr. */
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200290# define need_rb_str_new_cstr 1
Bram Moolenaar3ca71f12010-10-27 16:49:47 +0200291/* Ruby's headers #define rb_str_new_cstr to make use of GCC's
292 * __builtin_constant_p extension. */
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200293# undef rb_str_new_cstr
294# define rb_str_new_cstr dll_rb_str_new_cstr
Bram Moolenaar76a86062013-05-11 17:45:48 +0200295# else
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200296# define rb_str_new2 dll_rb_str_new2
Bram Moolenaar76a86062013-05-11 17:45:48 +0200297# endif
Bram Moolenaar3ca71f12010-10-27 16:49:47 +0200298# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 18
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200299# define rb_string_value dll_rb_string_value
300# define rb_string_value_ptr dll_rb_string_value_ptr
301# define rb_float_new dll_rb_float_new
302# define rb_ary_new dll_rb_ary_new
Bram Moolenaar51e9fbf2018-08-11 14:24:11 +0200303# ifdef rb_ary_new4
304# define RB_ARY_NEW4_MACRO 1
305# undef rb_ary_new4
306# endif
307# define rb_ary_new4 dll_rb_ary_new4
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200308# define rb_ary_push dll_rb_ary_push
Bram Moolenaar10f3a792013-05-20 12:52:29 +0200309# if defined(RUBY19_OR_LATER) || defined(RUBY_INIT_STACK)
310# ifdef __ia64
311# define rb_ia64_bsp dll_rb_ia64_bsp
312# undef ruby_init_stack
313# define ruby_init_stack(addr) dll_ruby_init_stack((addr), rb_ia64_bsp())
314# else
315# define ruby_init_stack dll_ruby_init_stack
316# endif
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200317# endif
318# else
319# define rb_str2cstr dll_rb_str2cstr
Bram Moolenaar3ca71f12010-10-27 16:49:47 +0200320# endif
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200321# ifdef RUBY19_OR_LATER
322# define rb_errinfo dll_rb_errinfo
323# else
324# define ruby_errinfo (*dll_ruby_errinfo)
325# endif
326# define ruby_init dll_ruby_init
327# define ruby_init_loadpath dll_ruby_init_loadpath
328# ifdef WIN3264
329# define NtInitialize dll_NtInitialize
Bram Moolenaar4f391792017-01-15 16:59:07 +0100330# define ruby_sysinit dll_ruby_sysinit
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200331# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 18
332# define rb_w32_snprintf dll_rb_w32_snprintf
333# endif
334# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000335
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200336# ifdef RUBY19_OR_LATER
337# define ruby_script dll_ruby_script
338# define rb_enc_find_index dll_rb_enc_find_index
339# define rb_enc_find dll_rb_enc_find
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100340# undef rb_enc_str_new
341# define rb_enc_str_new dll_rb_enc_str_new
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200342# define rb_sprintf dll_rb_sprintf
343# define rb_require dll_rb_require
Bram Moolenaar9b1067e2015-11-19 19:33:15 +0100344# define ruby_options dll_ruby_options
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200345# endif
Bram Moolenaar165641d2010-02-17 16:23:09 +0100346
Bram Moolenaar071d4272004-06-13 20:20:40 +0000347/*
348 * Pointers for dynamic link
349 */
350static VALUE (*dll_rb_assoc_new) (VALUE, VALUE);
Bram Moolenaarba52cde2010-06-25 04:29:11 +0200351VALUE *dll_rb_cFalseClass;
352VALUE *dll_rb_cFixnum;
Bram Moolenaar2016ae52016-06-13 20:08:43 +0200353# if defined(USE_RUBY_INTEGER)
Bram Moolenaar06469e92016-06-11 22:26:53 +0200354VALUE *dll_rb_cInteger;
355# endif
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200356# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 20
Bram Moolenaardb3fbe52013-03-07 15:16:21 +0100357VALUE *dll_rb_cFloat;
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200358# endif
Bram Moolenaarba52cde2010-06-25 04:29:11 +0200359VALUE *dll_rb_cNilClass;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000360static VALUE *dll_rb_cObject;
Bram Moolenaarba52cde2010-06-25 04:29:11 +0200361VALUE *dll_rb_cSymbol;
362VALUE *dll_rb_cTrueClass;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000363static void (*dll_rb_check_type) (VALUE,int);
Bram Moolenaarf2f6d292015-12-28 20:57:10 +0100364# ifdef USE_TYPEDDATA
365static void *(*dll_rb_check_typeddata) (VALUE,const rb_data_type_t *);
366# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000367static VALUE (*dll_rb_class_path) (VALUE);
Bram Moolenaarf2f6d292015-12-28 20:57:10 +0100368# ifdef USE_TYPEDDATA
369# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 23
370static VALUE (*dll_rb_data_typed_object_wrap) (VALUE, void*, const rb_data_type_t *);
371# else
372static VALUE (*dll_rb_data_typed_object_alloc) (VALUE, void*, const rb_data_type_t *);
373# endif
374# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000375static VALUE (*dll_rb_data_object_alloc) (VALUE, void*, RUBY_DATA_FUNC, RUBY_DATA_FUNC);
Bram Moolenaarf2f6d292015-12-28 20:57:10 +0100376# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000377static VALUE (*dll_rb_define_class_under) (VALUE, const char*, VALUE);
378static void (*dll_rb_define_const) (VALUE,const char*,VALUE);
379static void (*dll_rb_define_global_function) (const char*,VALUE(*)(),int);
380static void (*dll_rb_define_method) (VALUE,const char*,VALUE(*)(),int);
381static VALUE (*dll_rb_define_module) (const char*);
382static void (*dll_rb_define_module_function) (VALUE,const char*,VALUE(*)(),int);
383static void (*dll_rb_define_singleton_method) (VALUE,const char*,VALUE(*)(),int);
384static void (*dll_rb_define_virtual_variable) (const char*,VALUE(*)(),void(*)());
385static VALUE *dll_rb_stdout;
Bram Moolenaarb6c8cd82018-07-24 05:41:30 +0200386static VALUE *dll_rb_stderr;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000387static VALUE *dll_rb_eArgError;
388static VALUE *dll_rb_eIndexError;
389static VALUE *dll_rb_eRuntimeError;
390static VALUE *dll_rb_eStandardError;
391static VALUE (*dll_rb_eval_string_protect) (const char*, int*);
Bram Moolenaarf711cb22018-08-01 18:42:13 +0200392# ifdef RUBY21_OR_LATER
393static VALUE (*dll_rb_funcallv) (VALUE, ID, int, const VALUE*);
394# else
395static VALUE (*dll_rb_funcall2) (VALUE, ID, int, const VALUE*);
396# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000397static void (*dll_rb_global_variable) (VALUE*);
398static VALUE (*dll_rb_hash_aset) (VALUE, VALUE, VALUE);
399static VALUE (*dll_rb_hash_new) (void);
400static VALUE (*dll_rb_inspect) (VALUE);
401static VALUE (*dll_rb_int2inum) (long);
Bram Moolenaarf711cb22018-08-01 18:42:13 +0200402static ID (*dll_rb_intern) (const char*);
Bram Moolenaara2aa31a2014-02-23 22:52:40 +0100403# if VIM_SIZEOF_INT < VIM_SIZEOF_LONG /* 64 bits only */
Bram Moolenaar2623b4f2012-09-18 16:36:32 +0200404static long (*dll_rb_fix2int) (VALUE);
405static long (*dll_rb_num2int) (VALUE);
406static unsigned long (*dll_rb_num2uint) (VALUE);
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200407# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000408static VALUE (*dll_rb_lastline_get) (void);
409static void (*dll_rb_lastline_set) (VALUE);
Bram Moolenaar37badc82018-01-31 20:15:30 +0100410static VALUE (*dll_rb_protect) (VALUE (*)(VALUE), VALUE, int*);
Bram Moolenaard0573012017-10-28 21:11:06 +0200411static void (*dll_rb_load) (VALUE, int);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000412static long (*dll_rb_num2long) (VALUE);
413static unsigned long (*dll_rb_num2ulong) (VALUE);
414static VALUE (*dll_rb_obj_alloc) (VALUE);
415static VALUE (*dll_rb_obj_as_string) (VALUE);
416static VALUE (*dll_rb_obj_id) (VALUE);
417static void (*dll_rb_raise) (VALUE, const char*, ...);
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200418# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 18
Bram Moolenaarf9b5ef82010-09-29 13:02:53 +0200419static VALUE (*dll_rb_string_value) (volatile VALUE*);
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200420# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000421static char *(*dll_rb_str2cstr) (VALUE,int*);
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200422# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000423static VALUE (*dll_rb_str_cat) (VALUE, const char*, long);
424static VALUE (*dll_rb_str_concat) (VALUE, VALUE);
425static VALUE (*dll_rb_str_new) (const char*, long);
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200426# ifdef need_rb_str_new_cstr
Bram Moolenaar1d2beae2010-05-22 21:56:55 +0200427/* Ruby may #define rb_str_new2 to use rb_str_new_cstr. */
428static VALUE (*dll_rb_str_new_cstr) (const char*);
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200429# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000430static VALUE (*dll_rb_str_new2) (const char*);
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200431# endif
432# ifdef RUBY19_OR_LATER
Bram Moolenaar165641d2010-02-17 16:23:09 +0100433static VALUE (*dll_rb_errinfo) (void);
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200434# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000435static VALUE *dll_ruby_errinfo;
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200436# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000437static void (*dll_ruby_init) (void);
438static void (*dll_ruby_init_loadpath) (void);
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200439# ifdef WIN3264
Bram Moolenaar0b69c732010-02-17 15:11:50 +0100440static void (*dll_NtInitialize) (int*, char***);
Bram Moolenaar4f391792017-01-15 16:59:07 +0100441static void (*dll_ruby_sysinit) (int*, char***);
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200442# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 18
Bram Moolenaar3ca71f12010-10-27 16:49:47 +0200443static int (*dll_rb_w32_snprintf)(char*, size_t, const char*, ...);
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200444# endif
Bram Moolenaar3ca71f12010-10-27 16:49:47 +0200445# endif
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200446# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 18
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100447static char * (*dll_rb_string_value_ptr) (volatile VALUE*);
448static VALUE (*dll_rb_float_new) (double);
449static VALUE (*dll_rb_ary_new) (void);
Bram Moolenaar51e9fbf2018-08-11 14:24:11 +0200450static VALUE (*dll_rb_ary_new4) (long n, const VALUE *elts);
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100451static VALUE (*dll_rb_ary_push) (VALUE, VALUE);
Bram Moolenaar10f3a792013-05-20 12:52:29 +0200452# if defined(RUBY19_OR_LATER) || defined(RUBY_INIT_STACK)
453# ifdef __ia64
Bram Moolenaar76a86062013-05-11 17:45:48 +0200454static void * (*dll_rb_ia64_bsp) (void);
455static void (*dll_ruby_init_stack)(VALUE*, void*);
Bram Moolenaar10f3a792013-05-20 12:52:29 +0200456# else
Bram Moolenaar76a86062013-05-11 17:45:48 +0200457static void (*dll_ruby_init_stack)(VALUE*);
Bram Moolenaar10f3a792013-05-20 12:52:29 +0200458# endif
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200459# endif
Bram Moolenaar76a86062013-05-11 17:45:48 +0200460# endif
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200461# ifdef RUBY19_OR_LATER
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100462static VALUE (*dll_rb_int2big)(SIGNED_VALUE);
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200463# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000464
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200465# ifdef RUBY19_OR_LATER
Bram Moolenaar165641d2010-02-17 16:23:09 +0100466static void (*dll_ruby_script) (const char*);
467static int (*dll_rb_enc_find_index) (const char*);
468static rb_encoding* (*dll_rb_enc_find) (const char*);
469static VALUE (*dll_rb_enc_str_new) (const char*, long, rb_encoding*);
470static VALUE (*dll_rb_sprintf) (const char*, ...);
Bram Moolenaar7a8ef142010-12-24 13:39:35 +0100471static VALUE (*dll_rb_require) (const char*);
Bram Moolenaar9b1067e2015-11-19 19:33:15 +0100472static void* (*ruby_options)(int, char**);
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200473# endif
Bram Moolenaar165641d2010-02-17 16:23:09 +0100474
Bram Moolenaara1a118b2014-02-05 22:41:15 +0100475# if defined(USE_RGENGC) && USE_RGENGC
Bram Moolenaar0c7485f2015-01-14 14:04:10 +0100476# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER == 21
Bram Moolenaara1a118b2014-02-05 22:41:15 +0100477static void (*dll_rb_gc_writebarrier_unprotect_promoted)(VALUE);
Bram Moolenaar0c7485f2015-01-14 14:04:10 +0100478# else
479static void (*dll_rb_gc_writebarrier_unprotect)(VALUE obj);
480# endif
Bram Moolenaara1a118b2014-02-05 22:41:15 +0100481# endif
482
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200483# if defined(RUBY19_OR_LATER) && !defined(PROTO)
Bram Moolenaarbbc1a592015-04-21 15:25:31 +0200484# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 22
485long rb_num2long_stub(VALUE x)
486# else
Bram Moolenaarff8cf2b2012-11-24 13:39:00 +0100487SIGNED_VALUE rb_num2long_stub(VALUE x)
Bram Moolenaarbbc1a592015-04-21 15:25:31 +0200488# endif
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100489{
490 return dll_rb_num2long(x);
491}
Bram Moolenaarff8cf2b2012-11-24 13:39:00 +0100492VALUE rb_int2big_stub(SIGNED_VALUE x)
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100493{
494 return dll_rb_int2big(x);
495}
Bram Moolenaar498af702014-03-28 21:58:21 +0100496# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 19 \
497 && VIM_SIZEOF_INT < VIM_SIZEOF_LONG
Bram Moolenaar0bcdd6e2013-04-14 16:19:03 +0200498long rb_fix2int_stub(VALUE x)
499{
500 return dll_rb_fix2int(x);
501}
502long rb_num2int_stub(VALUE x)
503{
504 return dll_rb_num2int(x);
505}
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200506# endif
507# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 20
Bram Moolenaar886ed692013-02-26 13:41:35 +0100508VALUE
509rb_float_new_in_heap(double d)
510{
511 return dll_rb_float_new(d);
512}
Bram Moolenaarbbc1a592015-04-21 15:25:31 +0200513# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 22
514unsigned long rb_num2ulong(VALUE x)
515# else
Bram Moolenaardb3fbe52013-03-07 15:16:21 +0100516VALUE rb_num2ulong(VALUE x)
Bram Moolenaarbbc1a592015-04-21 15:25:31 +0200517# endif
Bram Moolenaar886ed692013-02-26 13:41:35 +0100518{
519 return (long)RSHIFT((SIGNED_VALUE)(x),1);
520}
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200521# endif
522# endif
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100523
Bram Moolenaar3e9a1612014-11-12 16:05:04 +0100524 /* Do not generate a prototype here, VALUE isn't always defined. */
525# if defined(USE_RGENGC) && USE_RGENGC && !defined(PROTO)
Bram Moolenaar0c7485f2015-01-14 14:04:10 +0100526# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER == 21
Bram Moolenaara1a118b2014-02-05 22:41:15 +0100527void rb_gc_writebarrier_unprotect_promoted_stub(VALUE obj)
528{
Bram Moolenaar90140742014-11-27 17:44:08 +0100529 dll_rb_gc_writebarrier_unprotect_promoted(obj);
Bram Moolenaara1a118b2014-02-05 22:41:15 +0100530}
Bram Moolenaar0c7485f2015-01-14 14:04:10 +0100531# else
532void rb_gc_writebarrier_unprotect_stub(VALUE obj)
533{
534 dll_rb_gc_writebarrier_unprotect(obj);
535}
536# endif
537# endif
538
Bram Moolenaar3ca71f12010-10-27 16:49:47 +0200539static HINSTANCE hinstRuby = NULL; /* Instance of ruby.dll */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000540
541/*
Bram Moolenaarda2303d2005-08-30 21:55:26 +0000542 * Table of name to function pointer of ruby.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000543 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000544static struct
545{
546 char *name;
547 RUBY_PROC *ptr;
548} ruby_funcname_table[] =
549{
550 {"rb_assoc_new", (RUBY_PROC*)&dll_rb_assoc_new},
551 {"rb_cFalseClass", (RUBY_PROC*)&dll_rb_cFalseClass},
Bram Moolenaar2016ae52016-06-13 20:08:43 +0200552# if defined(USE_RUBY_INTEGER)
Bram Moolenaar06469e92016-06-11 22:26:53 +0200553 {"rb_cInteger", (RUBY_PROC*)&dll_rb_cInteger},
Bram Moolenaar6abda992017-01-09 21:10:31 +0100554# else
555 {"rb_cFixnum", (RUBY_PROC*)&dll_rb_cFixnum},
Bram Moolenaar06469e92016-06-11 22:26:53 +0200556# endif
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200557# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 20
Bram Moolenaardb3fbe52013-03-07 15:16:21 +0100558 {"rb_cFloat", (RUBY_PROC*)&dll_rb_cFloat},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200559# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000560 {"rb_cNilClass", (RUBY_PROC*)&dll_rb_cNilClass},
561 {"rb_cObject", (RUBY_PROC*)&dll_rb_cObject},
562 {"rb_cSymbol", (RUBY_PROC*)&dll_rb_cSymbol},
563 {"rb_cTrueClass", (RUBY_PROC*)&dll_rb_cTrueClass},
564 {"rb_check_type", (RUBY_PROC*)&dll_rb_check_type},
Bram Moolenaarf2f6d292015-12-28 20:57:10 +0100565# ifdef USE_TYPEDDATA
566 {"rb_check_typeddata", (RUBY_PROC*)&dll_rb_check_typeddata},
567# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000568 {"rb_class_path", (RUBY_PROC*)&dll_rb_class_path},
Bram Moolenaarf2f6d292015-12-28 20:57:10 +0100569# ifdef USE_TYPEDDATA
570# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 23
571 {"rb_data_typed_object_wrap", (RUBY_PROC*)&dll_rb_data_typed_object_wrap},
572# else
573 {"rb_data_typed_object_alloc", (RUBY_PROC*)&dll_rb_data_typed_object_alloc},
574# endif
575# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000576 {"rb_data_object_alloc", (RUBY_PROC*)&dll_rb_data_object_alloc},
Bram Moolenaarf2f6d292015-12-28 20:57:10 +0100577# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000578 {"rb_define_class_under", (RUBY_PROC*)&dll_rb_define_class_under},
579 {"rb_define_const", (RUBY_PROC*)&dll_rb_define_const},
580 {"rb_define_global_function", (RUBY_PROC*)&dll_rb_define_global_function},
581 {"rb_define_method", (RUBY_PROC*)&dll_rb_define_method},
582 {"rb_define_module", (RUBY_PROC*)&dll_rb_define_module},
583 {"rb_define_module_function", (RUBY_PROC*)&dll_rb_define_module_function},
584 {"rb_define_singleton_method", (RUBY_PROC*)&dll_rb_define_singleton_method},
585 {"rb_define_virtual_variable", (RUBY_PROC*)&dll_rb_define_virtual_variable},
586 {"rb_stdout", (RUBY_PROC*)&dll_rb_stdout},
Bram Moolenaarb6c8cd82018-07-24 05:41:30 +0200587 {"rb_stderr", (RUBY_PROC*)&dll_rb_stderr},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000588 {"rb_eArgError", (RUBY_PROC*)&dll_rb_eArgError},
589 {"rb_eIndexError", (RUBY_PROC*)&dll_rb_eIndexError},
590 {"rb_eRuntimeError", (RUBY_PROC*)&dll_rb_eRuntimeError},
591 {"rb_eStandardError", (RUBY_PROC*)&dll_rb_eStandardError},
592 {"rb_eval_string_protect", (RUBY_PROC*)&dll_rb_eval_string_protect},
Bram Moolenaarf711cb22018-08-01 18:42:13 +0200593# ifdef RUBY21_OR_LATER
594 {"rb_funcallv", (RUBY_PROC*)&dll_rb_funcallv},
595# else
596 {"rb_funcall2", (RUBY_PROC*)&dll_rb_funcall2},
597# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000598 {"rb_global_variable", (RUBY_PROC*)&dll_rb_global_variable},
599 {"rb_hash_aset", (RUBY_PROC*)&dll_rb_hash_aset},
600 {"rb_hash_new", (RUBY_PROC*)&dll_rb_hash_new},
601 {"rb_inspect", (RUBY_PROC*)&dll_rb_inspect},
602 {"rb_int2inum", (RUBY_PROC*)&dll_rb_int2inum},
Bram Moolenaarf711cb22018-08-01 18:42:13 +0200603 {"rb_intern", (RUBY_PROC*)&dll_rb_intern},
Bram Moolenaara2aa31a2014-02-23 22:52:40 +0100604# if VIM_SIZEOF_INT < VIM_SIZEOF_LONG /* 64 bits only */
Bram Moolenaar2623b4f2012-09-18 16:36:32 +0200605 {"rb_fix2int", (RUBY_PROC*)&dll_rb_fix2int},
606 {"rb_num2int", (RUBY_PROC*)&dll_rb_num2int},
607 {"rb_num2uint", (RUBY_PROC*)&dll_rb_num2uint},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200608# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000609 {"rb_lastline_get", (RUBY_PROC*)&dll_rb_lastline_get},
610 {"rb_lastline_set", (RUBY_PROC*)&dll_rb_lastline_set},
Bram Moolenaard0573012017-10-28 21:11:06 +0200611 {"rb_protect", (RUBY_PROC*)&dll_rb_protect},
612 {"rb_load", (RUBY_PROC*)&dll_rb_load},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000613 {"rb_num2long", (RUBY_PROC*)&dll_rb_num2long},
614 {"rb_num2ulong", (RUBY_PROC*)&dll_rb_num2ulong},
615 {"rb_obj_alloc", (RUBY_PROC*)&dll_rb_obj_alloc},
616 {"rb_obj_as_string", (RUBY_PROC*)&dll_rb_obj_as_string},
617 {"rb_obj_id", (RUBY_PROC*)&dll_rb_obj_id},
618 {"rb_raise", (RUBY_PROC*)&dll_rb_raise},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200619# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 18
Bram Moolenaarf9b5ef82010-09-29 13:02:53 +0200620 {"rb_string_value", (RUBY_PROC*)&dll_rb_string_value},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200621# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000622 {"rb_str2cstr", (RUBY_PROC*)&dll_rb_str2cstr},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200623# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000624 {"rb_str_cat", (RUBY_PROC*)&dll_rb_str_cat},
625 {"rb_str_concat", (RUBY_PROC*)&dll_rb_str_concat},
626 {"rb_str_new", (RUBY_PROC*)&dll_rb_str_new},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200627# ifdef need_rb_str_new_cstr
Bram Moolenaar1d2beae2010-05-22 21:56:55 +0200628 {"rb_str_new_cstr", (RUBY_PROC*)&dll_rb_str_new_cstr},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200629# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000630 {"rb_str_new2", (RUBY_PROC*)&dll_rb_str_new2},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200631# endif
632# ifdef RUBY19_OR_LATER
Bram Moolenaar165641d2010-02-17 16:23:09 +0100633 {"rb_errinfo", (RUBY_PROC*)&dll_rb_errinfo},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200634# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000635 {"ruby_errinfo", (RUBY_PROC*)&dll_ruby_errinfo},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200636# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000637 {"ruby_init", (RUBY_PROC*)&dll_ruby_init},
638 {"ruby_init_loadpath", (RUBY_PROC*)&dll_ruby_init_loadpath},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200639# ifdef WIN3264
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200640# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER < 19
Bram Moolenaar4f391792017-01-15 16:59:07 +0100641 {"NtInitialize", (RUBY_PROC*)&dll_NtInitialize},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200642# else
Bram Moolenaar4f391792017-01-15 16:59:07 +0100643 {"ruby_sysinit", (RUBY_PROC*)&dll_ruby_sysinit},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200644# endif
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200645# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 18
Bram Moolenaar071d4272004-06-13 20:20:40 +0000646 {"rb_w32_snprintf", (RUBY_PROC*)&dll_rb_w32_snprintf},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200647# endif
Bram Moolenaar3ca71f12010-10-27 16:49:47 +0200648# endif
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200649# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 18
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100650 {"rb_string_value_ptr", (RUBY_PROC*)&dll_rb_string_value_ptr},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200651# if DYNAMIC_RUBY_VER <= 19
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100652 {"rb_float_new", (RUBY_PROC*)&dll_rb_float_new},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200653# else
Bram Moolenaar886ed692013-02-26 13:41:35 +0100654 {"rb_float_new_in_heap", (RUBY_PROC*)&dll_rb_float_new},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200655# endif
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100656 {"rb_ary_new", (RUBY_PROC*)&dll_rb_ary_new},
Bram Moolenaar51e9fbf2018-08-11 14:24:11 +0200657# ifdef RB_ARY_NEW4_MACRO
658 {"rb_ary_new_from_values", (RUBY_PROC*)&dll_rb_ary_new4},
659# else
660 {"rb_ary_new4", (RUBY_PROC*)&dll_rb_ary_new4},
661# endif
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100662 {"rb_ary_push", (RUBY_PROC*)&dll_rb_ary_push},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200663# endif
664# ifdef RUBY19_OR_LATER
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100665 {"rb_int2big", (RUBY_PROC*)&dll_rb_int2big},
Bram Moolenaar165641d2010-02-17 16:23:09 +0100666 {"ruby_script", (RUBY_PROC*)&dll_ruby_script},
667 {"rb_enc_find_index", (RUBY_PROC*)&dll_rb_enc_find_index},
668 {"rb_enc_find", (RUBY_PROC*)&dll_rb_enc_find},
669 {"rb_enc_str_new", (RUBY_PROC*)&dll_rb_enc_str_new},
670 {"rb_sprintf", (RUBY_PROC*)&dll_rb_sprintf},
Bram Moolenaar7a8ef142010-12-24 13:39:35 +0100671 {"rb_require", (RUBY_PROC*)&dll_rb_require},
Bram Moolenaar9b1067e2015-11-19 19:33:15 +0100672 {"ruby_options", (RUBY_PROC*)&dll_ruby_options},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200673# endif
Bram Moolenaar10f3a792013-05-20 12:52:29 +0200674# if defined(RUBY19_OR_LATER) || defined(RUBY_INIT_STACK)
675# ifdef __ia64
676 {"rb_ia64_bsp", (RUBY_PROC*)&dll_rb_ia64_bsp},
677# endif
678 {"ruby_init_stack", (RUBY_PROC*)&dll_ruby_init_stack},
679# endif
Bram Moolenaara1a118b2014-02-05 22:41:15 +0100680# if defined(USE_RGENGC) && USE_RGENGC
Bram Moolenaar0c7485f2015-01-14 14:04:10 +0100681# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER == 21
Bram Moolenaara1a118b2014-02-05 22:41:15 +0100682 {"rb_gc_writebarrier_unprotect_promoted", (RUBY_PROC*)&dll_rb_gc_writebarrier_unprotect_promoted},
Bram Moolenaar0c7485f2015-01-14 14:04:10 +0100683# else
684 {"rb_gc_writebarrier_unprotect", (RUBY_PROC*)&dll_rb_gc_writebarrier_unprotect},
685# endif
Bram Moolenaara1a118b2014-02-05 22:41:15 +0100686# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000687 {"", NULL},
688};
689
690/*
691 * Free ruby.dll
692 */
693 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100694end_dynamic_ruby(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000695{
696 if (hinstRuby)
697 {
Bram Moolenaarf9b5ef82010-09-29 13:02:53 +0200698 close_dll(hinstRuby);
Bram Moolenaar3ca71f12010-10-27 16:49:47 +0200699 hinstRuby = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000700 }
701}
702
703/*
704 * Load library and get all pointers.
705 * Parameter 'libname' provides name of DLL.
706 * Return OK or FAIL.
707 */
708 static int
709ruby_runtime_link_init(char *libname, int verbose)
710{
711 int i;
712
713 if (hinstRuby)
714 return OK;
Bram Moolenaarf9b5ef82010-09-29 13:02:53 +0200715 hinstRuby = load_dll(libname);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000716 if (!hinstRuby)
717 {
718 if (verbose)
719 EMSG2(_(e_loadlib), libname);
720 return FAIL;
721 }
722
723 for (i = 0; ruby_funcname_table[i].ptr; ++i)
724 {
Bram Moolenaarf9b5ef82010-09-29 13:02:53 +0200725 if (!(*ruby_funcname_table[i].ptr = symbol_from_dll(hinstRuby,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000726 ruby_funcname_table[i].name)))
727 {
Bram Moolenaarf9b5ef82010-09-29 13:02:53 +0200728 close_dll(hinstRuby);
Bram Moolenaar3ca71f12010-10-27 16:49:47 +0200729 hinstRuby = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000730 if (verbose)
731 EMSG2(_(e_loadfunc), ruby_funcname_table[i].name);
732 return FAIL;
733 }
734 }
735 return OK;
736}
737
738/*
739 * If ruby is enabled (there is installed ruby on Windows system) return TRUE,
740 * else FALSE.
741 */
742 int
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100743ruby_enabled(int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000744{
Bram Moolenaar25e4fcd2016-01-09 14:57:47 +0100745 return ruby_runtime_link_init((char *)p_rubydll, verbose) == OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000746}
747#endif /* defined(DYNAMIC_RUBY) || defined(PROTO) */
748
749 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100750ruby_end(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000751{
752#ifdef DYNAMIC_RUBY
753 end_dynamic_ruby();
754#endif
755}
756
757void ex_ruby(exarg_T *eap)
758{
759 int state;
760 char *script = NULL;
761
Bram Moolenaar35a2e192006-03-13 22:07:11 +0000762 script = (char *)script_get(eap, eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000763 if (!eap->skip && ensure_ruby_initialized())
764 {
765 if (script == NULL)
766 rb_eval_string_protect((char *)eap->arg, &state);
767 else
768 rb_eval_string_protect(script, &state);
769 if (state)
770 error_print(state);
771 }
772 vim_free(script);
773}
774
Bram Moolenaar165641d2010-02-17 16:23:09 +0100775/*
776 * In Ruby 1.9 or later, ruby String object has encoding.
777 * conversion buffer string of vim to ruby String object using
778 * VIM encoding option.
779 */
780 static VALUE
781vim_str2rb_enc_str(const char *s)
782{
Bram Moolenaar639a2552010-03-17 18:15:23 +0100783#ifdef RUBY19_OR_LATER
Bram Moolenaar165641d2010-02-17 16:23:09 +0100784 int isnum;
785 long lval;
786 char_u *sval;
787 rb_encoding *enc;
788
789 isnum = get_option_value((char_u *)"enc", &lval, &sval, 0);
790 if (isnum == 0)
791 {
792 enc = rb_enc_find((char *)sval);
793 vim_free(sval);
Bram Moolenaar758535a2016-03-30 22:06:16 +0200794 if (enc)
795 {
Bram Moolenaar9b0ac222016-06-01 20:31:43 +0200796 return rb_enc_str_new(s, (long)strlen(s), enc);
Bram Moolenaar165641d2010-02-17 16:23:09 +0100797 }
798 }
799#endif
800 return rb_str_new2(s);
801}
802
803 static VALUE
804eval_enc_string_protect(const char *str, int *state)
805{
Bram Moolenaar639a2552010-03-17 18:15:23 +0100806#ifdef RUBY19_OR_LATER
Bram Moolenaar165641d2010-02-17 16:23:09 +0100807 int isnum;
808 long lval;
809 char_u *sval;
810 rb_encoding *enc;
811 VALUE v;
812
813 isnum = get_option_value((char_u *)"enc", &lval, &sval, 0);
814 if (isnum == 0)
815 {
816 enc = rb_enc_find((char *)sval);
817 vim_free(sval);
818 if (enc)
819 {
820 v = rb_sprintf("#-*- coding:%s -*-\n%s", rb_enc_name(enc), str);
821 return rb_eval_string_protect(StringValuePtr(v), state);
822 }
823 }
824#endif
825 return rb_eval_string_protect(str, state);
826}
827
Bram Moolenaar071d4272004-06-13 20:20:40 +0000828void ex_rubydo(exarg_T *eap)
829{
830 int state;
831 linenr_T i;
Bram Moolenaarc593fee2017-01-29 23:11:25 +0100832 buf_T *was_curbuf = curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000833
834 if (ensure_ruby_initialized())
835 {
836 if (u_save(eap->line1 - 1, eap->line2 + 1) != OK)
837 return;
Bram Moolenaar758535a2016-03-30 22:06:16 +0200838 for (i = eap->line1; i <= eap->line2; i++)
839 {
Bram Moolenaare980d8a2010-12-08 13:11:21 +0100840 VALUE line;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000841
Bram Moolenaarc593fee2017-01-29 23:11:25 +0100842 if (i > curbuf->b_ml.ml_line_count)
843 break;
Bram Moolenaare980d8a2010-12-08 13:11:21 +0100844 line = vim_str2rb_enc_str((char *)ml_get(i));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000845 rb_lastline_set(line);
Bram Moolenaar165641d2010-02-17 16:23:09 +0100846 eval_enc_string_protect((char *) eap->arg, &state);
Bram Moolenaar758535a2016-03-30 22:06:16 +0200847 if (state)
848 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000849 error_print(state);
850 break;
851 }
Bram Moolenaarc593fee2017-01-29 23:11:25 +0100852 if (was_curbuf != curbuf)
853 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000854 line = rb_lastline_get();
Bram Moolenaar758535a2016-03-30 22:06:16 +0200855 if (!NIL_P(line))
856 {
857 if (TYPE(line) != T_STRING)
858 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000859 EMSG(_("E265: $_ must be an instance of String"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000860 return;
861 }
Bram Moolenaar165641d2010-02-17 16:23:09 +0100862 ml_replace(i, (char_u *) StringValuePtr(line), 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000863 changed();
864#ifdef SYNTAX_HL
865 syn_changed(i); /* recompute syntax hl. for this line */
866#endif
867 }
868 }
869 check_cursor();
870 update_curbuf(NOT_VALID);
871 }
872}
873
Bram Moolenaar0b394642018-05-17 13:11:46 +0200874static VALUE rb_load_wrap(VALUE file_to_load)
Bram Moolenaar37badc82018-01-31 20:15:30 +0100875{
876 rb_load(file_to_load, 0);
877 return Qnil;
878}
879
Bram Moolenaar071d4272004-06-13 20:20:40 +0000880void ex_rubyfile(exarg_T *eap)
881{
882 int state;
883
884 if (ensure_ruby_initialized())
885 {
Bram Moolenaar37badc82018-01-31 20:15:30 +0100886 VALUE file_to_load = rb_str_new2((const char *)eap->arg);
887 rb_protect(rb_load_wrap, file_to_load, &state);
888 if (state)
889 error_print(state);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000890 }
891}
892
893void ruby_buffer_free(buf_T *buf)
894{
Bram Moolenaare344bea2005-09-01 20:46:49 +0000895 if (buf->b_ruby_ref)
896 {
897 rb_hash_aset(objtbl, rb_obj_id((VALUE) buf->b_ruby_ref), Qnil);
898 RDATA(buf->b_ruby_ref)->data = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000899 }
900}
901
902void ruby_window_free(win_T *win)
903{
Bram Moolenaare344bea2005-09-01 20:46:49 +0000904 if (win->w_ruby_ref)
905 {
906 rb_hash_aset(objtbl, rb_obj_id((VALUE) win->w_ruby_ref), Qnil);
907 RDATA(win->w_ruby_ref)->data = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000908 }
909}
910
911static int ensure_ruby_initialized(void)
912{
913 if (!ruby_initialized)
914 {
915#ifdef DYNAMIC_RUBY
916 if (ruby_enabled(TRUE))
917 {
918#endif
Bram Moolenaar0b69c732010-02-17 15:11:50 +0100919#ifdef _WIN32
920 /* suggested by Ariya Mizutani */
921 int argc = 1;
922 char *argv[] = {"gvim.exe"};
Bram Moolenaar90140742014-11-27 17:44:08 +0100923 char **argvp = argv;
Bram Moolenaarfe6ce332017-01-14 20:12:01 +0100924# ifdef RUBY19_OR_LATER
925 ruby_sysinit(&argc, &argvp);
926# else
Bram Moolenaar90140742014-11-27 17:44:08 +0100927 NtInitialize(&argc, &argvp);
Bram Moolenaarfe6ce332017-01-14 20:12:01 +0100928# endif
Bram Moolenaar0b69c732010-02-17 15:11:50 +0100929#endif
Bram Moolenaarb2c03502010-07-02 20:20:09 +0200930 {
Bram Moolenaar10f3a792013-05-20 12:52:29 +0200931#if defined(RUBY19_OR_LATER) || defined(RUBY_INIT_STACK)
Bram Moolenaar99685e62013-05-11 13:56:18 +0200932 ruby_init_stack(ruby_stack_start);
Bram Moolenaar165641d2010-02-17 16:23:09 +0100933#endif
Bram Moolenaarb2c03502010-07-02 20:20:09 +0200934 ruby_init();
935 }
Bram Moolenaar639a2552010-03-17 18:15:23 +0100936#ifdef RUBY19_OR_LATER
Bram Moolenaar7a8ef142010-12-24 13:39:35 +0100937 {
938 int dummy_argc = 2;
Bram Moolenaard1bc96ce2017-09-26 21:21:44 +0200939 char *dummy_argv[] = {"vim-ruby", "-e_=0"};
Bram Moolenaar9b1067e2015-11-19 19:33:15 +0100940 ruby_options(dummy_argc, dummy_argv);
Bram Moolenaar7a8ef142010-12-24 13:39:35 +0100941 }
Bram Moolenaar165641d2010-02-17 16:23:09 +0100942 ruby_script("vim-ruby");
Bram Moolenaar7a8ef142010-12-24 13:39:35 +0100943#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000944 ruby_init_loadpath();
Bram Moolenaar165641d2010-02-17 16:23:09 +0100945#endif
Bram Moolenaar7a8ef142010-12-24 13:39:35 +0100946 ruby_io_init();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000947 ruby_vim_init();
948 ruby_initialized = 1;
949#ifdef DYNAMIC_RUBY
950 }
951 else
952 {
953 EMSG(_("E266: Sorry, this command is disabled, the Ruby library could not be loaded."));
954 return 0;
955 }
956#endif
957 }
958 return ruby_initialized;
959}
960
961static void error_print(int state)
962{
963#ifndef DYNAMIC_RUBY
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100964#if !(defined(RUBY_VERSION) && RUBY_VERSION >= 19) \
965 && !(defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 19)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000966 RUBYEXTERN VALUE ruby_errinfo;
967#endif
Bram Moolenaar165641d2010-02-17 16:23:09 +0100968#endif
Bram Moolenaarf711cb22018-08-01 18:42:13 +0200969 VALUE error;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000970 VALUE eclass;
971 VALUE einfo;
Bram Moolenaarf711cb22018-08-01 18:42:13 +0200972 VALUE bt;
973 int attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000974 char buff[BUFSIZ];
Bram Moolenaarf711cb22018-08-01 18:42:13 +0200975 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000976
977#define TAG_RETURN 0x1
978#define TAG_BREAK 0x2
979#define TAG_NEXT 0x3
980#define TAG_RETRY 0x4
981#define TAG_REDO 0x5
982#define TAG_RAISE 0x6
983#define TAG_THROW 0x7
984#define TAG_FATAL 0x8
985#define TAG_MASK 0xf
986
Bram Moolenaar758535a2016-03-30 22:06:16 +0200987 switch (state)
988 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000989 case TAG_RETURN:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000990 EMSG(_("E267: unexpected return"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000991 break;
992 case TAG_NEXT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000993 EMSG(_("E268: unexpected next"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000994 break;
995 case TAG_BREAK:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000996 EMSG(_("E269: unexpected break"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000997 break;
998 case TAG_REDO:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000999 EMSG(_("E270: unexpected redo"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001000 break;
1001 case TAG_RETRY:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001002 EMSG(_("E271: retry outside of rescue clause"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001003 break;
1004 case TAG_RAISE:
1005 case TAG_FATAL:
Bram Moolenaar639a2552010-03-17 18:15:23 +01001006#ifdef RUBY19_OR_LATER
Bram Moolenaarf711cb22018-08-01 18:42:13 +02001007 error = rb_errinfo();
Bram Moolenaar165641d2010-02-17 16:23:09 +01001008#else
Bram Moolenaarf711cb22018-08-01 18:42:13 +02001009 error = ruby_errinfo;
Bram Moolenaar165641d2010-02-17 16:23:09 +01001010#endif
Bram Moolenaarf711cb22018-08-01 18:42:13 +02001011 eclass = CLASS_OF(error);
1012 einfo = rb_obj_as_string(error);
Bram Moolenaar758535a2016-03-30 22:06:16 +02001013 if (eclass == rb_eRuntimeError && RSTRING_LEN(einfo) == 0)
1014 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001015 EMSG(_("E272: unhandled exception"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001016 }
Bram Moolenaar758535a2016-03-30 22:06:16 +02001017 else
1018 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001019 VALUE epath;
1020 char *p;
1021
1022 epath = rb_class_path(eclass);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00001023 vim_snprintf(buff, BUFSIZ, "%s: %s",
Bram Moolenaar165641d2010-02-17 16:23:09 +01001024 RSTRING_PTR(epath), RSTRING_PTR(einfo));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001025 p = strchr(buff, '\n');
1026 if (p) *p = '\0';
1027 EMSG(buff);
1028 }
Bram Moolenaarf711cb22018-08-01 18:42:13 +02001029
1030 attr = syn_name2attr((char_u *)"Error");
1031# ifdef RUBY21_OR_LATER
1032 bt = rb_funcallv(error, rb_intern("backtrace"), 0, 0);
1033 for (i = 0; i < RARRAY_LEN(bt); i++)
1034 msg_attr((char_u *)RSTRING_PTR(RARRAY_AREF(bt, i)), attr);
1035# else
1036 bt = rb_funcall2(error, rb_intern("backtrace"), 0, 0);
1037 for (i = 0; i < RARRAY_LEN(bt); i++)
1038 msg_attr((char_u *)RSTRING_PTR(RARRAY_PTR(bt)[i]), attr);
1039# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001040 break;
1041 default:
Bram Moolenaar9c13b352005-05-19 20:53:52 +00001042 vim_snprintf(buff, BUFSIZ, _("E273: unknown longjmp status %d"), state);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001043 EMSG(buff);
1044 break;
1045 }
1046}
1047
Bram Moolenaarcd8b20a2009-05-22 16:20:57 +00001048static VALUE vim_message(VALUE self UNUSED, VALUE str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001049{
1050 char *buff, *p;
1051
1052 str = rb_obj_as_string(str);
Bram Moolenaar3f5f7952011-08-04 19:34:59 +02001053 if (RSTRING_LEN(str) > 0)
1054 {
1055 /* Only do this when the string isn't empty, alloc(0) causes trouble. */
Bram Moolenaar00ccf542017-09-03 15:17:48 +02001056 buff = ALLOCA_N(char, RSTRING_LEN(str) + 1);
Bram Moolenaar3f5f7952011-08-04 19:34:59 +02001057 strcpy(buff, RSTRING_PTR(str));
1058 p = strchr(buff, '\n');
1059 if (p) *p = '\0';
1060 MSG(buff);
1061 }
1062 else
1063 {
1064 MSG("");
1065 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001066 return Qnil;
1067}
1068
Bram Moolenaarcd8b20a2009-05-22 16:20:57 +00001069static VALUE vim_set_option(VALUE self UNUSED, VALUE str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001070{
Bram Moolenaar165641d2010-02-17 16:23:09 +01001071 do_set((char_u *)StringValuePtr(str), 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001072 update_screen(NOT_VALID);
1073 return Qnil;
1074}
1075
Bram Moolenaarcd8b20a2009-05-22 16:20:57 +00001076static VALUE vim_command(VALUE self UNUSED, VALUE str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001077{
Bram Moolenaar165641d2010-02-17 16:23:09 +01001078 do_cmdline_cmd((char_u *)StringValuePtr(str));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001079 return Qnil;
1080}
1081
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001082#ifdef FEAT_EVAL
1083static VALUE vim_to_ruby(typval_T *tv)
1084{
1085 VALUE result = Qnil;
1086
1087 if (tv->v_type == VAR_STRING)
1088 {
Bram Moolenaar94127e42010-03-19 23:08:48 +01001089 result = rb_str_new2(tv->vval.v_string == NULL
1090 ? "" : (char *)(tv->vval.v_string));
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001091 }
1092 else if (tv->v_type == VAR_NUMBER)
1093 {
Bram Moolenaar639a2552010-03-17 18:15:23 +01001094 result = INT2NUM(tv->vval.v_number);
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001095 }
1096# ifdef FEAT_FLOAT
1097 else if (tv->v_type == VAR_FLOAT)
1098 {
Bram Moolenaar639a2552010-03-17 18:15:23 +01001099 result = rb_float_new(tv->vval.v_float);
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001100 }
1101# endif
1102 else if (tv->v_type == VAR_LIST)
1103 {
Bram Moolenaar639a2552010-03-17 18:15:23 +01001104 list_T *list = tv->vval.v_list;
1105 listitem_T *curr;
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001106
Bram Moolenaar639a2552010-03-17 18:15:23 +01001107 result = rb_ary_new();
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001108
Bram Moolenaar639a2552010-03-17 18:15:23 +01001109 if (list != NULL)
1110 {
1111 for (curr = list->lv_first; curr != NULL; curr = curr->li_next)
1112 {
1113 rb_ary_push(result, vim_to_ruby(&curr->li_tv));
1114 }
1115 }
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001116 }
1117 else if (tv->v_type == VAR_DICT)
1118 {
Bram Moolenaar639a2552010-03-17 18:15:23 +01001119 result = rb_hash_new();
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001120
Bram Moolenaar639a2552010-03-17 18:15:23 +01001121 if (tv->vval.v_dict != NULL)
1122 {
1123 hashtab_T *ht = &tv->vval.v_dict->dv_hashtab;
1124 long_u todo = ht->ht_used;
1125 hashitem_T *hi;
1126 dictitem_T *di;
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001127
Bram Moolenaar639a2552010-03-17 18:15:23 +01001128 for (hi = ht->ht_array; todo > 0; ++hi)
1129 {
1130 if (!HASHITEM_EMPTY(hi))
1131 {
1132 --todo;
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001133
Bram Moolenaar639a2552010-03-17 18:15:23 +01001134 di = dict_lookup(hi);
1135 rb_hash_aset(result, rb_str_new2((char *)hi->hi_key),
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001136 vim_to_ruby(&di->di_tv));
Bram Moolenaar639a2552010-03-17 18:15:23 +01001137 }
1138 }
1139 }
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001140 }
1141 else if (tv->v_type == VAR_SPECIAL)
1142 {
Bram Moolenaard84b26a2018-07-28 17:18:09 +02001143 if (tv->vval.v_number == VVAL_TRUE)
1144 result = Qtrue;
1145 else if (tv->vval.v_number == VVAL_FALSE)
1146 result = Qfalse;
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001147 } /* else return Qnil; */
1148
1149 return result;
1150}
1151#endif
1152
Bram Moolenaarcd8b20a2009-05-22 16:20:57 +00001153static VALUE vim_evaluate(VALUE self UNUSED, VALUE str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001154{
1155#ifdef FEAT_EVAL
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001156 typval_T *tv;
1157 VALUE result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001158
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001159 tv = eval_expr((char_u *)StringValuePtr(str), NULL);
1160 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001161 {
Bram Moolenaar639a2552010-03-17 18:15:23 +01001162 return Qnil;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001163 }
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001164 result = vim_to_ruby(tv);
1165
1166 free_tv(tv);
1167
1168 return result;
1169#else
1170 return Qnil;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001171#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001172}
1173
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001174#ifdef USE_TYPEDDATA
1175static size_t buffer_dsize(const void *buf);
1176
1177static const rb_data_type_t buffer_type = {
1178 "vim_buffer",
1179 {0, 0, buffer_dsize, {0, 0}},
1180 0, 0,
1181# ifdef RUBY_TYPED_FREE_IMMEDIATELY
1182 0,
1183# endif
1184};
1185
1186static size_t buffer_dsize(const void *buf UNUSED)
1187{
1188 return sizeof(buf_T);
1189}
1190#endif
1191
Bram Moolenaar071d4272004-06-13 20:20:40 +00001192static VALUE buffer_new(buf_T *buf)
1193{
Bram Moolenaare344bea2005-09-01 20:46:49 +00001194 if (buf->b_ruby_ref)
1195 {
1196 return (VALUE) buf->b_ruby_ref;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001197 }
Bram Moolenaare344bea2005-09-01 20:46:49 +00001198 else
1199 {
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001200#ifdef USE_TYPEDDATA
1201 VALUE obj = TypedData_Wrap_Struct(cBuffer, &buffer_type, buf);
1202#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001203 VALUE obj = Data_Wrap_Struct(cBuffer, 0, 0, buf);
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001204#endif
Bram Moolenaare344bea2005-09-01 20:46:49 +00001205 buf->b_ruby_ref = (void *) obj;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001206 rb_hash_aset(objtbl, rb_obj_id(obj), obj);
1207 return obj;
1208 }
1209}
1210
1211static buf_T *get_buf(VALUE obj)
1212{
1213 buf_T *buf;
1214
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001215#ifdef USE_TYPEDDATA
1216 TypedData_Get_Struct(obj, buf_T, &buffer_type, buf);
1217#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001218 Data_Get_Struct(obj, buf_T, buf);
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001219#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001220 if (buf == NULL)
1221 rb_raise(eDeletedBufferError, "attempt to refer to deleted buffer");
1222 return buf;
1223}
1224
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001225static VALUE buffer_s_current(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001226{
1227 return buffer_new(curbuf);
1228}
1229
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001230static VALUE buffer_s_count(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001231{
1232 buf_T *b;
1233 int n = 0;
1234
Bram Moolenaar29323592016-07-24 22:04:11 +02001235 FOR_ALL_BUFFERS(b)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001236 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001237 /* Deleted buffers should not be counted
1238 * SegPhault - 01/07/05 */
1239 if (b->b_p_bl)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001240 n++;
1241 }
1242
Bram Moolenaar071d4272004-06-13 20:20:40 +00001243 return INT2NUM(n);
1244}
1245
Bram Moolenaarcd8b20a2009-05-22 16:20:57 +00001246static VALUE buffer_s_aref(VALUE self UNUSED, VALUE num)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001247{
1248 buf_T *b;
1249 int n = NUM2INT(num);
1250
Bram Moolenaar29323592016-07-24 22:04:11 +02001251 FOR_ALL_BUFFERS(b)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001252 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001253 /* Deleted buffers should not be counted
1254 * SegPhault - 01/07/05 */
1255 if (!b->b_p_bl)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001256 continue;
1257
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001258 if (n == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001259 return buffer_new(b);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001260
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001261 n--;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001262 }
1263 return Qnil;
1264}
1265
1266static VALUE buffer_name(VALUE self)
1267{
1268 buf_T *buf = get_buf(self);
1269
Bram Moolenaar35a2e192006-03-13 22:07:11 +00001270 return buf->b_ffname ? rb_str_new2((char *)buf->b_ffname) : Qnil;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001271}
1272
1273static VALUE buffer_number(VALUE self)
1274{
1275 buf_T *buf = get_buf(self);
1276
1277 return INT2NUM(buf->b_fnum);
1278}
1279
1280static VALUE buffer_count(VALUE self)
1281{
1282 buf_T *buf = get_buf(self);
1283
1284 return INT2NUM(buf->b_ml.ml_line_count);
1285}
1286
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001287static VALUE get_buffer_line(buf_T *buf, linenr_T n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001288{
Bram Moolenaar3c531602010-11-16 14:46:19 +01001289 if (n <= 0 || n > buf->b_ml.ml_line_count)
1290 rb_raise(rb_eIndexError, "line number %ld out of range", (long)n);
1291 return vim_str2rb_enc_str((char *)ml_get_buf(buf, n, FALSE));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001292}
1293
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001294static VALUE buffer_aref(VALUE self, VALUE num)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001295{
1296 buf_T *buf = get_buf(self);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001297
1298 if (buf != NULL)
1299 return get_buffer_line(buf, (linenr_T)NUM2LONG(num));
1300 return Qnil; /* For stop warning */
1301}
1302
1303static VALUE set_buffer_line(buf_T *buf, linenr_T n, VALUE str)
1304{
Bram Moolenaar165641d2010-02-17 16:23:09 +01001305 char *line = StringValuePtr(str);
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001306 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001307
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001308 if (n > 0 && n <= buf->b_ml.ml_line_count && line != NULL)
1309 {
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001310 /* set curwin/curbuf for "buf" and save some things */
1311 aucmd_prepbuf(&aco, buf);
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001312
Bram Moolenaar758535a2016-03-30 22:06:16 +02001313 if (u_savesub(n) == OK)
1314 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001315 ml_replace(n, (char_u *)line, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001316 changed();
1317#ifdef SYNTAX_HL
1318 syn_changed(n); /* recompute syntax hl. for this line */
1319#endif
1320 }
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001321
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001322 /* restore curwin/curbuf and a few other things */
1323 aucmd_restbuf(&aco);
1324 /* Careful: autocommands may have made "buf" invalid! */
Bram Moolenaarf30e74c2006-08-16 17:35:00 +00001325
Bram Moolenaar071d4272004-06-13 20:20:40 +00001326 update_curbuf(NOT_VALID);
1327 }
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001328 else
1329 {
Bram Moolenaar165641d2010-02-17 16:23:09 +01001330 rb_raise(rb_eIndexError, "line number %ld out of range", (long)n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001331 }
1332 return str;
1333}
1334
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001335static VALUE buffer_aset(VALUE self, VALUE num, VALUE str)
1336{
1337 buf_T *buf = get_buf(self);
1338
1339 if (buf != NULL)
1340 return set_buffer_line(buf, (linenr_T)NUM2LONG(num), str);
1341 return str;
1342}
1343
Bram Moolenaar071d4272004-06-13 20:20:40 +00001344static VALUE buffer_delete(VALUE self, VALUE num)
1345{
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001346 buf_T *buf = get_buf(self);
1347 long n = NUM2LONG(num);
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001348 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001349
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001350 if (n > 0 && n <= buf->b_ml.ml_line_count)
1351 {
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001352 /* set curwin/curbuf for "buf" and save some things */
1353 aucmd_prepbuf(&aco, buf);
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001354
Bram Moolenaar758535a2016-03-30 22:06:16 +02001355 if (u_savedel(n, 1) == OK)
1356 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001357 ml_delete(n, 0);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001358
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001359 /* Changes to non-active buffers should properly refresh
1360 * SegPhault - 01/09/05 */
1361 deleted_lines_mark(n, 1L);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001362
Bram Moolenaar071d4272004-06-13 20:20:40 +00001363 changed();
1364 }
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001365
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001366 /* restore curwin/curbuf and a few other things */
1367 aucmd_restbuf(&aco);
1368 /* Careful: autocommands may have made "buf" invalid! */
Bram Moolenaarf30e74c2006-08-16 17:35:00 +00001369
Bram Moolenaar071d4272004-06-13 20:20:40 +00001370 update_curbuf(NOT_VALID);
1371 }
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001372 else
1373 {
Bram Moolenaar165641d2010-02-17 16:23:09 +01001374 rb_raise(rb_eIndexError, "line number %ld out of range", n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001375 }
1376 return Qnil;
1377}
1378
1379static VALUE buffer_append(VALUE self, VALUE num, VALUE str)
1380{
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001381 buf_T *buf = get_buf(self);
Bram Moolenaar165641d2010-02-17 16:23:09 +01001382 char *line = StringValuePtr(str);
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001383 long n = NUM2LONG(num);
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001384 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001385
Bram Moolenaar3c531602010-11-16 14:46:19 +01001386 if (line == NULL)
1387 {
Bram Moolenaar165641d2010-02-17 16:23:09 +01001388 rb_raise(rb_eIndexError, "NULL line");
1389 }
1390 else if (n >= 0 && n <= buf->b_ml.ml_line_count)
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001391 {
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001392 /* set curwin/curbuf for "buf" and save some things */
1393 aucmd_prepbuf(&aco, buf);
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001394
Bram Moolenaar758535a2016-03-30 22:06:16 +02001395 if (u_inssub(n + 1) == OK)
1396 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001397 ml_append(n, (char_u *) line, (colnr_T) 0, FALSE);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001398
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001399 /* Changes to non-active buffers should properly refresh screen
1400 * SegPhault - 12/20/04 */
1401 appended_lines_mark(n, 1L);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001402
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001403 changed();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001404 }
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001405
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001406 /* restore curwin/curbuf and a few other things */
1407 aucmd_restbuf(&aco);
1408 /* Careful: autocommands may have made "buf" invalid! */
Bram Moolenaarf30e74c2006-08-16 17:35:00 +00001409
Bram Moolenaar071d4272004-06-13 20:20:40 +00001410 update_curbuf(NOT_VALID);
1411 }
Bram Moolenaar3c531602010-11-16 14:46:19 +01001412 else
1413 {
Bram Moolenaar165641d2010-02-17 16:23:09 +01001414 rb_raise(rb_eIndexError, "line number %ld out of range", n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001415 }
1416 return str;
1417}
1418
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001419#ifdef USE_TYPEDDATA
1420static size_t window_dsize(const void *buf);
1421
1422static const rb_data_type_t window_type = {
1423 "vim_window",
1424 {0, 0, window_dsize, {0, 0}},
1425 0, 0,
1426# ifdef RUBY_TYPED_FREE_IMMEDIATELY
1427 0,
1428# endif
1429};
1430
1431static size_t window_dsize(const void *win UNUSED)
1432{
1433 return sizeof(win_T);
1434}
1435#endif
1436
Bram Moolenaar071d4272004-06-13 20:20:40 +00001437static VALUE window_new(win_T *win)
1438{
Bram Moolenaare344bea2005-09-01 20:46:49 +00001439 if (win->w_ruby_ref)
1440 {
1441 return (VALUE) win->w_ruby_ref;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001442 }
Bram Moolenaare344bea2005-09-01 20:46:49 +00001443 else
1444 {
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001445#ifdef USE_TYPEDDATA
1446 VALUE obj = TypedData_Wrap_Struct(cVimWindow, &window_type, win);
1447#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001448 VALUE obj = Data_Wrap_Struct(cVimWindow, 0, 0, win);
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001449#endif
Bram Moolenaare344bea2005-09-01 20:46:49 +00001450 win->w_ruby_ref = (void *) obj;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001451 rb_hash_aset(objtbl, rb_obj_id(obj), obj);
1452 return obj;
1453 }
1454}
1455
1456static win_T *get_win(VALUE obj)
1457{
1458 win_T *win;
1459
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001460#ifdef USE_TYPEDDATA
1461 TypedData_Get_Struct(obj, win_T, &window_type, win);
1462#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001463 Data_Get_Struct(obj, win_T, win);
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001464#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001465 if (win == NULL)
1466 rb_raise(eDeletedWindowError, "attempt to refer to deleted window");
1467 return win;
1468}
1469
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001470static VALUE window_s_current(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001471{
1472 return window_new(curwin);
1473}
1474
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001475/*
1476 * Added line manipulation functions
1477 * SegPhault - 03/07/05
1478 */
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001479static VALUE line_s_current(void)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001480{
1481 return get_buffer_line(curbuf, curwin->w_cursor.lnum);
1482}
1483
Bram Moolenaarcd8b20a2009-05-22 16:20:57 +00001484static VALUE set_current_line(VALUE self UNUSED, VALUE str)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001485{
1486 return set_buffer_line(curbuf, curwin->w_cursor.lnum, str);
1487}
1488
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001489static VALUE current_line_number(void)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001490{
1491 return INT2FIX((int)curwin->w_cursor.lnum);
1492}
1493
1494
1495
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001496static VALUE window_s_count(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001497{
Bram Moolenaar071d4272004-06-13 20:20:40 +00001498 win_T *w;
1499 int n = 0;
1500
Bram Moolenaar29323592016-07-24 22:04:11 +02001501 FOR_ALL_WINDOWS(w)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001502 n++;
1503 return INT2NUM(n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001504}
1505
Bram Moolenaarcd8b20a2009-05-22 16:20:57 +00001506static VALUE window_s_aref(VALUE self UNUSED, VALUE num)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001507{
1508 win_T *w;
1509 int n = NUM2INT(num);
1510
Bram Moolenaar071d4272004-06-13 20:20:40 +00001511 for (w = firstwin; w != NULL; w = w->w_next, --n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001512 if (n == 0)
1513 return window_new(w);
1514 return Qnil;
1515}
1516
1517static VALUE window_buffer(VALUE self)
1518{
1519 win_T *win = get_win(self);
1520
1521 return buffer_new(win->w_buffer);
1522}
1523
1524static VALUE window_height(VALUE self)
1525{
1526 win_T *win = get_win(self);
1527
1528 return INT2NUM(win->w_height);
1529}
1530
1531static VALUE window_set_height(VALUE self, VALUE height)
1532{
1533 win_T *win = get_win(self);
1534 win_T *savewin = curwin;
1535
1536 curwin = win;
1537 win_setheight(NUM2INT(height));
1538 curwin = savewin;
1539 return height;
1540}
1541
Bram Moolenaarfeeaa682013-02-14 22:19:51 +01001542static VALUE window_width(VALUE self UNUSED)
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001543{
Bram Moolenaare745d752017-09-22 16:56:22 +02001544 return INT2NUM(get_win(self)->w_width);
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001545}
1546
Bram Moolenaarfeeaa682013-02-14 22:19:51 +01001547static VALUE window_set_width(VALUE self UNUSED, VALUE width)
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001548{
1549 win_T *win = get_win(self);
1550 win_T *savewin = curwin;
1551
1552 curwin = win;
1553 win_setwidth(NUM2INT(width));
1554 curwin = savewin;
1555 return width;
1556}
1557
Bram Moolenaar071d4272004-06-13 20:20:40 +00001558static VALUE window_cursor(VALUE self)
1559{
1560 win_T *win = get_win(self);
1561
1562 return rb_assoc_new(INT2NUM(win->w_cursor.lnum), INT2NUM(win->w_cursor.col));
1563}
1564
1565static VALUE window_set_cursor(VALUE self, VALUE pos)
1566{
1567 VALUE lnum, col;
1568 win_T *win = get_win(self);
1569
1570 Check_Type(pos, T_ARRAY);
Bram Moolenaar165641d2010-02-17 16:23:09 +01001571 if (RARRAY_LEN(pos) != 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001572 rb_raise(rb_eArgError, "array length must be 2");
Bram Moolenaar165641d2010-02-17 16:23:09 +01001573 lnum = RARRAY_PTR(pos)[0];
1574 col = RARRAY_PTR(pos)[1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001575 win->w_cursor.lnum = NUM2LONG(lnum);
1576 win->w_cursor.col = NUM2UINT(col);
Bram Moolenaar53901442018-07-25 22:02:36 +02001577 win->w_set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001578 check_cursor(); /* put cursor on an existing line */
1579 update_screen(NOT_VALID);
1580 return Qnil;
1581}
1582
Bram Moolenaar6217cdc2012-04-25 12:28:09 +02001583static VALUE f_nop(VALUE self UNUSED)
Bram Moolenaar35df7d22012-04-20 18:05:47 +02001584{
1585 return Qnil;
1586}
1587
Bram Moolenaarcd8b20a2009-05-22 16:20:57 +00001588static VALUE f_p(int argc, VALUE *argv, VALUE self UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001589{
1590 int i;
1591 VALUE str = rb_str_new("", 0);
Bram Moolenaar51e9fbf2018-08-11 14:24:11 +02001592 VALUE ret = Qnil;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001593
Bram Moolenaar758535a2016-03-30 22:06:16 +02001594 for (i = 0; i < argc; i++)
1595 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001596 if (i > 0) rb_str_cat(str, ", ", 2);
1597 rb_str_concat(str, rb_inspect(argv[i]));
1598 }
Bram Moolenaar165641d2010-02-17 16:23:09 +01001599 MSG(RSTRING_PTR(str));
Bram Moolenaar51e9fbf2018-08-11 14:24:11 +02001600
1601 if (argc == 1)
1602 ret = argv[0];
1603 else if (argc > 1)
1604 ret = rb_ary_new4(argc, argv);
1605 return ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001606}
1607
1608static void ruby_io_init(void)
1609{
1610#ifndef DYNAMIC_RUBY
1611 RUBYEXTERN VALUE rb_stdout;
Bram Moolenaarb6c8cd82018-07-24 05:41:30 +02001612 RUBYEXTERN VALUE rb_stderr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001613#endif
1614
1615 rb_stdout = rb_obj_alloc(rb_cObject);
Bram Moolenaarb6c8cd82018-07-24 05:41:30 +02001616 rb_stderr = rb_obj_alloc(rb_cObject);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001617 rb_define_singleton_method(rb_stdout, "write", vim_message, 1);
Bram Moolenaar35df7d22012-04-20 18:05:47 +02001618 rb_define_singleton_method(rb_stdout, "flush", f_nop, 0);
Bram Moolenaarb6c8cd82018-07-24 05:41:30 +02001619 rb_define_singleton_method(rb_stderr, "write", vim_message, 1);
1620 rb_define_singleton_method(rb_stderr, "flush", f_nop, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001621 rb_define_global_function("p", f_p, -1);
1622}
1623
1624static void ruby_vim_init(void)
1625{
1626 objtbl = rb_hash_new();
1627 rb_global_variable(&objtbl);
1628
Bram Moolenaarf711faf2007-05-10 16:48:19 +00001629 /* The Vim module used to be called "VIM", but "Vim" is better. Make an
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02001630 * alias "VIM" for backwards compatibility. */
Bram Moolenaarf711faf2007-05-10 16:48:19 +00001631 mVIM = rb_define_module("Vim");
1632 rb_define_const(rb_cObject, "VIM", mVIM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001633 rb_define_const(mVIM, "VERSION_MAJOR", INT2NUM(VIM_VERSION_MAJOR));
1634 rb_define_const(mVIM, "VERSION_MINOR", INT2NUM(VIM_VERSION_MINOR));
1635 rb_define_const(mVIM, "VERSION_BUILD", INT2NUM(VIM_VERSION_BUILD));
1636 rb_define_const(mVIM, "VERSION_PATCHLEVEL", INT2NUM(VIM_VERSION_PATCHLEVEL));
1637 rb_define_const(mVIM, "VERSION_SHORT", rb_str_new2(VIM_VERSION_SHORT));
1638 rb_define_const(mVIM, "VERSION_MEDIUM", rb_str_new2(VIM_VERSION_MEDIUM));
1639 rb_define_const(mVIM, "VERSION_LONG", rb_str_new2(VIM_VERSION_LONG));
1640 rb_define_const(mVIM, "VERSION_LONG_DATE", rb_str_new2(VIM_VERSION_LONG_DATE));
1641 rb_define_module_function(mVIM, "message", vim_message, 1);
1642 rb_define_module_function(mVIM, "set_option", vim_set_option, 1);
1643 rb_define_module_function(mVIM, "command", vim_command, 1);
1644 rb_define_module_function(mVIM, "evaluate", vim_evaluate, 1);
1645
1646 eDeletedBufferError = rb_define_class_under(mVIM, "DeletedBufferError",
1647 rb_eStandardError);
1648 eDeletedWindowError = rb_define_class_under(mVIM, "DeletedWindowError",
1649 rb_eStandardError);
1650
1651 cBuffer = rb_define_class_under(mVIM, "Buffer", rb_cObject);
1652 rb_define_singleton_method(cBuffer, "current", buffer_s_current, 0);
1653 rb_define_singleton_method(cBuffer, "count", buffer_s_count, 0);
1654 rb_define_singleton_method(cBuffer, "[]", buffer_s_aref, 1);
1655 rb_define_method(cBuffer, "name", buffer_name, 0);
1656 rb_define_method(cBuffer, "number", buffer_number, 0);
1657 rb_define_method(cBuffer, "count", buffer_count, 0);
1658 rb_define_method(cBuffer, "length", buffer_count, 0);
1659 rb_define_method(cBuffer, "[]", buffer_aref, 1);
1660 rb_define_method(cBuffer, "[]=", buffer_aset, 2);
1661 rb_define_method(cBuffer, "delete", buffer_delete, 1);
1662 rb_define_method(cBuffer, "append", buffer_append, 2);
1663
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001664 /* Added line manipulation functions
1665 * SegPhault - 03/07/05 */
1666 rb_define_method(cBuffer, "line_number", current_line_number, 0);
1667 rb_define_method(cBuffer, "line", line_s_current, 0);
1668 rb_define_method(cBuffer, "line=", set_current_line, 1);
1669
1670
Bram Moolenaar071d4272004-06-13 20:20:40 +00001671 cVimWindow = rb_define_class_under(mVIM, "Window", rb_cObject);
1672 rb_define_singleton_method(cVimWindow, "current", window_s_current, 0);
1673 rb_define_singleton_method(cVimWindow, "count", window_s_count, 0);
1674 rb_define_singleton_method(cVimWindow, "[]", window_s_aref, 1);
1675 rb_define_method(cVimWindow, "buffer", window_buffer, 0);
1676 rb_define_method(cVimWindow, "height", window_height, 0);
1677 rb_define_method(cVimWindow, "height=", window_set_height, 1);
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001678 rb_define_method(cVimWindow, "width", window_width, 0);
1679 rb_define_method(cVimWindow, "width=", window_set_width, 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001680 rb_define_method(cVimWindow, "cursor", window_cursor, 0);
1681 rb_define_method(cVimWindow, "cursor=", window_set_cursor, 1);
1682
1683 rb_define_virtual_variable("$curbuf", buffer_s_current, 0);
1684 rb_define_virtual_variable("$curwin", window_s_current, 0);
1685}
Bram Moolenaar99685e62013-05-11 13:56:18 +02001686
1687void vim_ruby_init(void *stack_start)
1688{
1689 /* should get machine stack start address early in main function */
1690 ruby_stack_start = stack_start;
1691}