blob: cb9b416036faba9be1e7ac30fd60e47e25acb0c5 [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
Bram Moolenaar9d5c84a2018-12-19 20:48:46 +0100329# ifdef RUBY19_OR_LATER
330# define ruby_sysinit dll_ruby_sysinit
331# else
332# define NtInitialize dll_NtInitialize
333# endif
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200334# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 18
335# define rb_w32_snprintf dll_rb_w32_snprintf
336# endif
337# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000338
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200339# ifdef RUBY19_OR_LATER
340# define ruby_script dll_ruby_script
341# define rb_enc_find_index dll_rb_enc_find_index
342# define rb_enc_find dll_rb_enc_find
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100343# undef rb_enc_str_new
344# define rb_enc_str_new dll_rb_enc_str_new
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200345# define rb_sprintf dll_rb_sprintf
346# define rb_require dll_rb_require
Bram Moolenaar9b1067e2015-11-19 19:33:15 +0100347# define ruby_options dll_ruby_options
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200348# endif
Bram Moolenaar165641d2010-02-17 16:23:09 +0100349
Bram Moolenaar071d4272004-06-13 20:20:40 +0000350/*
351 * Pointers for dynamic link
352 */
353static VALUE (*dll_rb_assoc_new) (VALUE, VALUE);
Bram Moolenaarba52cde2010-06-25 04:29:11 +0200354VALUE *dll_rb_cFalseClass;
355VALUE *dll_rb_cFixnum;
Bram Moolenaar2016ae52016-06-13 20:08:43 +0200356# if defined(USE_RUBY_INTEGER)
Bram Moolenaar06469e92016-06-11 22:26:53 +0200357VALUE *dll_rb_cInteger;
358# endif
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200359# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 20
Bram Moolenaardb3fbe52013-03-07 15:16:21 +0100360VALUE *dll_rb_cFloat;
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200361# endif
Bram Moolenaarba52cde2010-06-25 04:29:11 +0200362VALUE *dll_rb_cNilClass;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000363static VALUE *dll_rb_cObject;
Bram Moolenaarba52cde2010-06-25 04:29:11 +0200364VALUE *dll_rb_cSymbol;
365VALUE *dll_rb_cTrueClass;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000366static void (*dll_rb_check_type) (VALUE,int);
Bram Moolenaarf2f6d292015-12-28 20:57:10 +0100367# ifdef USE_TYPEDDATA
368static void *(*dll_rb_check_typeddata) (VALUE,const rb_data_type_t *);
369# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000370static VALUE (*dll_rb_class_path) (VALUE);
Bram Moolenaarf2f6d292015-12-28 20:57:10 +0100371# ifdef USE_TYPEDDATA
372# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 23
373static VALUE (*dll_rb_data_typed_object_wrap) (VALUE, void*, const rb_data_type_t *);
374# else
375static VALUE (*dll_rb_data_typed_object_alloc) (VALUE, void*, const rb_data_type_t *);
376# endif
377# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000378static VALUE (*dll_rb_data_object_alloc) (VALUE, void*, RUBY_DATA_FUNC, RUBY_DATA_FUNC);
Bram Moolenaarf2f6d292015-12-28 20:57:10 +0100379# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000380static VALUE (*dll_rb_define_class_under) (VALUE, const char*, VALUE);
381static void (*dll_rb_define_const) (VALUE,const char*,VALUE);
382static void (*dll_rb_define_global_function) (const char*,VALUE(*)(),int);
383static void (*dll_rb_define_method) (VALUE,const char*,VALUE(*)(),int);
384static VALUE (*dll_rb_define_module) (const char*);
385static void (*dll_rb_define_module_function) (VALUE,const char*,VALUE(*)(),int);
386static void (*dll_rb_define_singleton_method) (VALUE,const char*,VALUE(*)(),int);
387static void (*dll_rb_define_virtual_variable) (const char*,VALUE(*)(),void(*)());
388static VALUE *dll_rb_stdout;
Bram Moolenaarb6c8cd82018-07-24 05:41:30 +0200389static VALUE *dll_rb_stderr;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000390static VALUE *dll_rb_eArgError;
391static VALUE *dll_rb_eIndexError;
392static VALUE *dll_rb_eRuntimeError;
393static VALUE *dll_rb_eStandardError;
394static VALUE (*dll_rb_eval_string_protect) (const char*, int*);
Bram Moolenaarf711cb22018-08-01 18:42:13 +0200395# ifdef RUBY21_OR_LATER
396static VALUE (*dll_rb_funcallv) (VALUE, ID, int, const VALUE*);
397# else
398static VALUE (*dll_rb_funcall2) (VALUE, ID, int, const VALUE*);
399# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000400static void (*dll_rb_global_variable) (VALUE*);
401static VALUE (*dll_rb_hash_aset) (VALUE, VALUE, VALUE);
402static VALUE (*dll_rb_hash_new) (void);
403static VALUE (*dll_rb_inspect) (VALUE);
404static VALUE (*dll_rb_int2inum) (long);
Bram Moolenaarf711cb22018-08-01 18:42:13 +0200405static ID (*dll_rb_intern) (const char*);
Bram Moolenaara2aa31a2014-02-23 22:52:40 +0100406# if VIM_SIZEOF_INT < VIM_SIZEOF_LONG /* 64 bits only */
Bram Moolenaar2623b4f2012-09-18 16:36:32 +0200407static long (*dll_rb_fix2int) (VALUE);
408static long (*dll_rb_num2int) (VALUE);
409static unsigned long (*dll_rb_num2uint) (VALUE);
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200410# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000411static VALUE (*dll_rb_lastline_get) (void);
412static void (*dll_rb_lastline_set) (VALUE);
Bram Moolenaar37badc82018-01-31 20:15:30 +0100413static VALUE (*dll_rb_protect) (VALUE (*)(VALUE), VALUE, int*);
Bram Moolenaard0573012017-10-28 21:11:06 +0200414static void (*dll_rb_load) (VALUE, int);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000415static long (*dll_rb_num2long) (VALUE);
416static unsigned long (*dll_rb_num2ulong) (VALUE);
417static VALUE (*dll_rb_obj_alloc) (VALUE);
418static VALUE (*dll_rb_obj_as_string) (VALUE);
419static VALUE (*dll_rb_obj_id) (VALUE);
420static void (*dll_rb_raise) (VALUE, const char*, ...);
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200421# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 18
Bram Moolenaarf9b5ef82010-09-29 13:02:53 +0200422static VALUE (*dll_rb_string_value) (volatile VALUE*);
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200423# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000424static char *(*dll_rb_str2cstr) (VALUE,int*);
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200425# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000426static VALUE (*dll_rb_str_cat) (VALUE, const char*, long);
427static VALUE (*dll_rb_str_concat) (VALUE, VALUE);
428static VALUE (*dll_rb_str_new) (const char*, long);
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200429# ifdef need_rb_str_new_cstr
Bram Moolenaar1d2beae2010-05-22 21:56:55 +0200430/* Ruby may #define rb_str_new2 to use rb_str_new_cstr. */
431static VALUE (*dll_rb_str_new_cstr) (const char*);
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200432# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000433static VALUE (*dll_rb_str_new2) (const char*);
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200434# endif
435# ifdef RUBY19_OR_LATER
Bram Moolenaar165641d2010-02-17 16:23:09 +0100436static VALUE (*dll_rb_errinfo) (void);
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200437# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000438static VALUE *dll_ruby_errinfo;
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200439# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000440static void (*dll_ruby_init) (void);
441static void (*dll_ruby_init_loadpath) (void);
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200442# ifdef WIN3264
Bram Moolenaar9d5c84a2018-12-19 20:48:46 +0100443# ifdef RUBY19_OR_LATER
Bram Moolenaar4f391792017-01-15 16:59:07 +0100444static void (*dll_ruby_sysinit) (int*, char***);
Bram Moolenaar9d5c84a2018-12-19 20:48:46 +0100445# else
446static void (*dll_NtInitialize) (int*, char***);
447# endif
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200448# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 18
Bram Moolenaar3ca71f12010-10-27 16:49:47 +0200449static int (*dll_rb_w32_snprintf)(char*, size_t, const char*, ...);
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200450# endif
Bram Moolenaar3ca71f12010-10-27 16:49:47 +0200451# endif
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200452# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 18
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100453static char * (*dll_rb_string_value_ptr) (volatile VALUE*);
454static VALUE (*dll_rb_float_new) (double);
455static VALUE (*dll_rb_ary_new) (void);
Bram Moolenaar51e9fbf2018-08-11 14:24:11 +0200456static VALUE (*dll_rb_ary_new4) (long n, const VALUE *elts);
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100457static VALUE (*dll_rb_ary_push) (VALUE, VALUE);
Bram Moolenaar10f3a792013-05-20 12:52:29 +0200458# if defined(RUBY19_OR_LATER) || defined(RUBY_INIT_STACK)
459# ifdef __ia64
Bram Moolenaar76a86062013-05-11 17:45:48 +0200460static void * (*dll_rb_ia64_bsp) (void);
461static void (*dll_ruby_init_stack)(VALUE*, void*);
Bram Moolenaar10f3a792013-05-20 12:52:29 +0200462# else
Bram Moolenaar76a86062013-05-11 17:45:48 +0200463static void (*dll_ruby_init_stack)(VALUE*);
Bram Moolenaar10f3a792013-05-20 12:52:29 +0200464# endif
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200465# endif
Bram Moolenaar76a86062013-05-11 17:45:48 +0200466# endif
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200467# ifdef RUBY19_OR_LATER
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100468static VALUE (*dll_rb_int2big)(SIGNED_VALUE);
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200469# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000470
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200471# ifdef RUBY19_OR_LATER
Bram Moolenaar165641d2010-02-17 16:23:09 +0100472static void (*dll_ruby_script) (const char*);
473static int (*dll_rb_enc_find_index) (const char*);
474static rb_encoding* (*dll_rb_enc_find) (const char*);
475static VALUE (*dll_rb_enc_str_new) (const char*, long, rb_encoding*);
476static VALUE (*dll_rb_sprintf) (const char*, ...);
Bram Moolenaar7a8ef142010-12-24 13:39:35 +0100477static VALUE (*dll_rb_require) (const char*);
Bram Moolenaar9b1067e2015-11-19 19:33:15 +0100478static void* (*ruby_options)(int, char**);
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200479# endif
Bram Moolenaar165641d2010-02-17 16:23:09 +0100480
Bram Moolenaara1a118b2014-02-05 22:41:15 +0100481# if defined(USE_RGENGC) && USE_RGENGC
Bram Moolenaar0c7485f2015-01-14 14:04:10 +0100482# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER == 21
Bram Moolenaara1a118b2014-02-05 22:41:15 +0100483static void (*dll_rb_gc_writebarrier_unprotect_promoted)(VALUE);
Bram Moolenaar0c7485f2015-01-14 14:04:10 +0100484# else
485static void (*dll_rb_gc_writebarrier_unprotect)(VALUE obj);
486# endif
Bram Moolenaara1a118b2014-02-05 22:41:15 +0100487# endif
488
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200489# if defined(RUBY19_OR_LATER) && !defined(PROTO)
Bram Moolenaarbbc1a592015-04-21 15:25:31 +0200490# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 22
491long rb_num2long_stub(VALUE x)
492# else
Bram Moolenaarff8cf2b2012-11-24 13:39:00 +0100493SIGNED_VALUE rb_num2long_stub(VALUE x)
Bram Moolenaarbbc1a592015-04-21 15:25:31 +0200494# endif
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100495{
496 return dll_rb_num2long(x);
497}
Bram Moolenaarff8cf2b2012-11-24 13:39:00 +0100498VALUE rb_int2big_stub(SIGNED_VALUE x)
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100499{
500 return dll_rb_int2big(x);
501}
Bram Moolenaar498af702014-03-28 21:58:21 +0100502# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 19 \
503 && VIM_SIZEOF_INT < VIM_SIZEOF_LONG
Bram Moolenaar0bcdd6e2013-04-14 16:19:03 +0200504long rb_fix2int_stub(VALUE x)
505{
506 return dll_rb_fix2int(x);
507}
508long rb_num2int_stub(VALUE x)
509{
510 return dll_rb_num2int(x);
511}
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200512# endif
513# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 20
Bram Moolenaar886ed692013-02-26 13:41:35 +0100514VALUE
515rb_float_new_in_heap(double d)
516{
517 return dll_rb_float_new(d);
518}
Bram Moolenaarbbc1a592015-04-21 15:25:31 +0200519# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 22
520unsigned long rb_num2ulong(VALUE x)
521# else
Bram Moolenaardb3fbe52013-03-07 15:16:21 +0100522VALUE rb_num2ulong(VALUE x)
Bram Moolenaarbbc1a592015-04-21 15:25:31 +0200523# endif
Bram Moolenaar886ed692013-02-26 13:41:35 +0100524{
525 return (long)RSHIFT((SIGNED_VALUE)(x),1);
526}
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200527# endif
528# endif
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100529
Bram Moolenaar3e9a1612014-11-12 16:05:04 +0100530 /* Do not generate a prototype here, VALUE isn't always defined. */
531# if defined(USE_RGENGC) && USE_RGENGC && !defined(PROTO)
Bram Moolenaar0c7485f2015-01-14 14:04:10 +0100532# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER == 21
Bram Moolenaara1a118b2014-02-05 22:41:15 +0100533void rb_gc_writebarrier_unprotect_promoted_stub(VALUE obj)
534{
Bram Moolenaar90140742014-11-27 17:44:08 +0100535 dll_rb_gc_writebarrier_unprotect_promoted(obj);
Bram Moolenaara1a118b2014-02-05 22:41:15 +0100536}
Bram Moolenaar0c7485f2015-01-14 14:04:10 +0100537# else
538void rb_gc_writebarrier_unprotect_stub(VALUE obj)
539{
540 dll_rb_gc_writebarrier_unprotect(obj);
541}
542# endif
543# endif
544
Bram Moolenaar3ca71f12010-10-27 16:49:47 +0200545static HINSTANCE hinstRuby = NULL; /* Instance of ruby.dll */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000546
547/*
Bram Moolenaarda2303d2005-08-30 21:55:26 +0000548 * Table of name to function pointer of ruby.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000549 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000550static struct
551{
552 char *name;
553 RUBY_PROC *ptr;
554} ruby_funcname_table[] =
555{
556 {"rb_assoc_new", (RUBY_PROC*)&dll_rb_assoc_new},
557 {"rb_cFalseClass", (RUBY_PROC*)&dll_rb_cFalseClass},
Bram Moolenaar2016ae52016-06-13 20:08:43 +0200558# if defined(USE_RUBY_INTEGER)
Bram Moolenaar06469e92016-06-11 22:26:53 +0200559 {"rb_cInteger", (RUBY_PROC*)&dll_rb_cInteger},
Bram Moolenaar6abda992017-01-09 21:10:31 +0100560# else
561 {"rb_cFixnum", (RUBY_PROC*)&dll_rb_cFixnum},
Bram Moolenaar06469e92016-06-11 22:26:53 +0200562# endif
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200563# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 20
Bram Moolenaardb3fbe52013-03-07 15:16:21 +0100564 {"rb_cFloat", (RUBY_PROC*)&dll_rb_cFloat},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200565# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000566 {"rb_cNilClass", (RUBY_PROC*)&dll_rb_cNilClass},
567 {"rb_cObject", (RUBY_PROC*)&dll_rb_cObject},
568 {"rb_cSymbol", (RUBY_PROC*)&dll_rb_cSymbol},
569 {"rb_cTrueClass", (RUBY_PROC*)&dll_rb_cTrueClass},
570 {"rb_check_type", (RUBY_PROC*)&dll_rb_check_type},
Bram Moolenaarf2f6d292015-12-28 20:57:10 +0100571# ifdef USE_TYPEDDATA
572 {"rb_check_typeddata", (RUBY_PROC*)&dll_rb_check_typeddata},
573# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000574 {"rb_class_path", (RUBY_PROC*)&dll_rb_class_path},
Bram Moolenaarf2f6d292015-12-28 20:57:10 +0100575# ifdef USE_TYPEDDATA
576# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 23
577 {"rb_data_typed_object_wrap", (RUBY_PROC*)&dll_rb_data_typed_object_wrap},
578# else
579 {"rb_data_typed_object_alloc", (RUBY_PROC*)&dll_rb_data_typed_object_alloc},
580# endif
581# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000582 {"rb_data_object_alloc", (RUBY_PROC*)&dll_rb_data_object_alloc},
Bram Moolenaarf2f6d292015-12-28 20:57:10 +0100583# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000584 {"rb_define_class_under", (RUBY_PROC*)&dll_rb_define_class_under},
585 {"rb_define_const", (RUBY_PROC*)&dll_rb_define_const},
586 {"rb_define_global_function", (RUBY_PROC*)&dll_rb_define_global_function},
587 {"rb_define_method", (RUBY_PROC*)&dll_rb_define_method},
588 {"rb_define_module", (RUBY_PROC*)&dll_rb_define_module},
589 {"rb_define_module_function", (RUBY_PROC*)&dll_rb_define_module_function},
590 {"rb_define_singleton_method", (RUBY_PROC*)&dll_rb_define_singleton_method},
591 {"rb_define_virtual_variable", (RUBY_PROC*)&dll_rb_define_virtual_variable},
592 {"rb_stdout", (RUBY_PROC*)&dll_rb_stdout},
Bram Moolenaarb6c8cd82018-07-24 05:41:30 +0200593 {"rb_stderr", (RUBY_PROC*)&dll_rb_stderr},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000594 {"rb_eArgError", (RUBY_PROC*)&dll_rb_eArgError},
595 {"rb_eIndexError", (RUBY_PROC*)&dll_rb_eIndexError},
596 {"rb_eRuntimeError", (RUBY_PROC*)&dll_rb_eRuntimeError},
597 {"rb_eStandardError", (RUBY_PROC*)&dll_rb_eStandardError},
598 {"rb_eval_string_protect", (RUBY_PROC*)&dll_rb_eval_string_protect},
Bram Moolenaarf711cb22018-08-01 18:42:13 +0200599# ifdef RUBY21_OR_LATER
600 {"rb_funcallv", (RUBY_PROC*)&dll_rb_funcallv},
601# else
602 {"rb_funcall2", (RUBY_PROC*)&dll_rb_funcall2},
603# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000604 {"rb_global_variable", (RUBY_PROC*)&dll_rb_global_variable},
605 {"rb_hash_aset", (RUBY_PROC*)&dll_rb_hash_aset},
606 {"rb_hash_new", (RUBY_PROC*)&dll_rb_hash_new},
607 {"rb_inspect", (RUBY_PROC*)&dll_rb_inspect},
608 {"rb_int2inum", (RUBY_PROC*)&dll_rb_int2inum},
Bram Moolenaarf711cb22018-08-01 18:42:13 +0200609 {"rb_intern", (RUBY_PROC*)&dll_rb_intern},
Bram Moolenaara2aa31a2014-02-23 22:52:40 +0100610# if VIM_SIZEOF_INT < VIM_SIZEOF_LONG /* 64 bits only */
Bram Moolenaar2623b4f2012-09-18 16:36:32 +0200611 {"rb_fix2int", (RUBY_PROC*)&dll_rb_fix2int},
612 {"rb_num2int", (RUBY_PROC*)&dll_rb_num2int},
613 {"rb_num2uint", (RUBY_PROC*)&dll_rb_num2uint},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200614# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000615 {"rb_lastline_get", (RUBY_PROC*)&dll_rb_lastline_get},
616 {"rb_lastline_set", (RUBY_PROC*)&dll_rb_lastline_set},
Bram Moolenaard0573012017-10-28 21:11:06 +0200617 {"rb_protect", (RUBY_PROC*)&dll_rb_protect},
618 {"rb_load", (RUBY_PROC*)&dll_rb_load},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000619 {"rb_num2long", (RUBY_PROC*)&dll_rb_num2long},
620 {"rb_num2ulong", (RUBY_PROC*)&dll_rb_num2ulong},
621 {"rb_obj_alloc", (RUBY_PROC*)&dll_rb_obj_alloc},
622 {"rb_obj_as_string", (RUBY_PROC*)&dll_rb_obj_as_string},
623 {"rb_obj_id", (RUBY_PROC*)&dll_rb_obj_id},
624 {"rb_raise", (RUBY_PROC*)&dll_rb_raise},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200625# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 18
Bram Moolenaarf9b5ef82010-09-29 13:02:53 +0200626 {"rb_string_value", (RUBY_PROC*)&dll_rb_string_value},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200627# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000628 {"rb_str2cstr", (RUBY_PROC*)&dll_rb_str2cstr},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200629# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000630 {"rb_str_cat", (RUBY_PROC*)&dll_rb_str_cat},
631 {"rb_str_concat", (RUBY_PROC*)&dll_rb_str_concat},
632 {"rb_str_new", (RUBY_PROC*)&dll_rb_str_new},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200633# ifdef need_rb_str_new_cstr
Bram Moolenaar1d2beae2010-05-22 21:56:55 +0200634 {"rb_str_new_cstr", (RUBY_PROC*)&dll_rb_str_new_cstr},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200635# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000636 {"rb_str_new2", (RUBY_PROC*)&dll_rb_str_new2},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200637# endif
638# ifdef RUBY19_OR_LATER
Bram Moolenaar165641d2010-02-17 16:23:09 +0100639 {"rb_errinfo", (RUBY_PROC*)&dll_rb_errinfo},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200640# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000641 {"ruby_errinfo", (RUBY_PROC*)&dll_ruby_errinfo},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200642# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000643 {"ruby_init", (RUBY_PROC*)&dll_ruby_init},
644 {"ruby_init_loadpath", (RUBY_PROC*)&dll_ruby_init_loadpath},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200645# ifdef WIN3264
Bram Moolenaar9d5c84a2018-12-19 20:48:46 +0100646# ifdef RUBY19_OR_LATER
Bram Moolenaar4f391792017-01-15 16:59:07 +0100647 {"ruby_sysinit", (RUBY_PROC*)&dll_ruby_sysinit},
Bram Moolenaar9d5c84a2018-12-19 20:48:46 +0100648# else
649 {"NtInitialize", (RUBY_PROC*)&dll_NtInitialize},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200650# endif
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200651# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 18
Bram Moolenaar071d4272004-06-13 20:20:40 +0000652 {"rb_w32_snprintf", (RUBY_PROC*)&dll_rb_w32_snprintf},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200653# endif
Bram Moolenaar3ca71f12010-10-27 16:49:47 +0200654# endif
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200655# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 18
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100656 {"rb_string_value_ptr", (RUBY_PROC*)&dll_rb_string_value_ptr},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200657# if DYNAMIC_RUBY_VER <= 19
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100658 {"rb_float_new", (RUBY_PROC*)&dll_rb_float_new},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200659# else
Bram Moolenaar886ed692013-02-26 13:41:35 +0100660 {"rb_float_new_in_heap", (RUBY_PROC*)&dll_rb_float_new},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200661# endif
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100662 {"rb_ary_new", (RUBY_PROC*)&dll_rb_ary_new},
Bram Moolenaar51e9fbf2018-08-11 14:24:11 +0200663# ifdef RB_ARY_NEW4_MACRO
664 {"rb_ary_new_from_values", (RUBY_PROC*)&dll_rb_ary_new4},
665# else
666 {"rb_ary_new4", (RUBY_PROC*)&dll_rb_ary_new4},
667# endif
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100668 {"rb_ary_push", (RUBY_PROC*)&dll_rb_ary_push},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200669# endif
670# ifdef RUBY19_OR_LATER
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100671 {"rb_int2big", (RUBY_PROC*)&dll_rb_int2big},
Bram Moolenaar165641d2010-02-17 16:23:09 +0100672 {"ruby_script", (RUBY_PROC*)&dll_ruby_script},
673 {"rb_enc_find_index", (RUBY_PROC*)&dll_rb_enc_find_index},
674 {"rb_enc_find", (RUBY_PROC*)&dll_rb_enc_find},
675 {"rb_enc_str_new", (RUBY_PROC*)&dll_rb_enc_str_new},
676 {"rb_sprintf", (RUBY_PROC*)&dll_rb_sprintf},
Bram Moolenaar7a8ef142010-12-24 13:39:35 +0100677 {"rb_require", (RUBY_PROC*)&dll_rb_require},
Bram Moolenaar9b1067e2015-11-19 19:33:15 +0100678 {"ruby_options", (RUBY_PROC*)&dll_ruby_options},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200679# endif
Bram Moolenaar10f3a792013-05-20 12:52:29 +0200680# if defined(RUBY19_OR_LATER) || defined(RUBY_INIT_STACK)
681# ifdef __ia64
682 {"rb_ia64_bsp", (RUBY_PROC*)&dll_rb_ia64_bsp},
683# endif
684 {"ruby_init_stack", (RUBY_PROC*)&dll_ruby_init_stack},
685# endif
Bram Moolenaara1a118b2014-02-05 22:41:15 +0100686# if defined(USE_RGENGC) && USE_RGENGC
Bram Moolenaar0c7485f2015-01-14 14:04:10 +0100687# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER == 21
Bram Moolenaara1a118b2014-02-05 22:41:15 +0100688 {"rb_gc_writebarrier_unprotect_promoted", (RUBY_PROC*)&dll_rb_gc_writebarrier_unprotect_promoted},
Bram Moolenaar0c7485f2015-01-14 14:04:10 +0100689# else
690 {"rb_gc_writebarrier_unprotect", (RUBY_PROC*)&dll_rb_gc_writebarrier_unprotect},
691# endif
Bram Moolenaara1a118b2014-02-05 22:41:15 +0100692# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000693 {"", NULL},
694};
695
696/*
697 * Free ruby.dll
698 */
699 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100700end_dynamic_ruby(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000701{
702 if (hinstRuby)
703 {
Bram Moolenaarf9b5ef82010-09-29 13:02:53 +0200704 close_dll(hinstRuby);
Bram Moolenaar3ca71f12010-10-27 16:49:47 +0200705 hinstRuby = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000706 }
707}
708
709/*
710 * Load library and get all pointers.
711 * Parameter 'libname' provides name of DLL.
712 * Return OK or FAIL.
713 */
714 static int
715ruby_runtime_link_init(char *libname, int verbose)
716{
717 int i;
718
719 if (hinstRuby)
720 return OK;
Bram Moolenaarf9b5ef82010-09-29 13:02:53 +0200721 hinstRuby = load_dll(libname);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000722 if (!hinstRuby)
723 {
724 if (verbose)
725 EMSG2(_(e_loadlib), libname);
726 return FAIL;
727 }
728
729 for (i = 0; ruby_funcname_table[i].ptr; ++i)
730 {
Bram Moolenaarf9b5ef82010-09-29 13:02:53 +0200731 if (!(*ruby_funcname_table[i].ptr = symbol_from_dll(hinstRuby,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000732 ruby_funcname_table[i].name)))
733 {
Bram Moolenaarf9b5ef82010-09-29 13:02:53 +0200734 close_dll(hinstRuby);
Bram Moolenaar3ca71f12010-10-27 16:49:47 +0200735 hinstRuby = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000736 if (verbose)
737 EMSG2(_(e_loadfunc), ruby_funcname_table[i].name);
738 return FAIL;
739 }
740 }
741 return OK;
742}
743
744/*
745 * If ruby is enabled (there is installed ruby on Windows system) return TRUE,
746 * else FALSE.
747 */
748 int
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100749ruby_enabled(int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000750{
Bram Moolenaar25e4fcd2016-01-09 14:57:47 +0100751 return ruby_runtime_link_init((char *)p_rubydll, verbose) == OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000752}
753#endif /* defined(DYNAMIC_RUBY) || defined(PROTO) */
754
755 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100756ruby_end(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000757{
758#ifdef DYNAMIC_RUBY
759 end_dynamic_ruby();
760#endif
761}
762
763void ex_ruby(exarg_T *eap)
764{
765 int state;
766 char *script = NULL;
767
Bram Moolenaar35a2e192006-03-13 22:07:11 +0000768 script = (char *)script_get(eap, eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000769 if (!eap->skip && ensure_ruby_initialized())
770 {
771 if (script == NULL)
772 rb_eval_string_protect((char *)eap->arg, &state);
773 else
774 rb_eval_string_protect(script, &state);
775 if (state)
776 error_print(state);
777 }
778 vim_free(script);
779}
780
Bram Moolenaar165641d2010-02-17 16:23:09 +0100781/*
782 * In Ruby 1.9 or later, ruby String object has encoding.
783 * conversion buffer string of vim to ruby String object using
784 * VIM encoding option.
785 */
786 static VALUE
787vim_str2rb_enc_str(const char *s)
788{
Bram Moolenaar639a2552010-03-17 18:15:23 +0100789#ifdef RUBY19_OR_LATER
Bram Moolenaar165641d2010-02-17 16:23:09 +0100790 int isnum;
791 long lval;
792 char_u *sval;
793 rb_encoding *enc;
794
795 isnum = get_option_value((char_u *)"enc", &lval, &sval, 0);
796 if (isnum == 0)
797 {
798 enc = rb_enc_find((char *)sval);
799 vim_free(sval);
Bram Moolenaar758535a2016-03-30 22:06:16 +0200800 if (enc)
801 {
Bram Moolenaar9b0ac222016-06-01 20:31:43 +0200802 return rb_enc_str_new(s, (long)strlen(s), enc);
Bram Moolenaar165641d2010-02-17 16:23:09 +0100803 }
804 }
805#endif
806 return rb_str_new2(s);
807}
808
809 static VALUE
810eval_enc_string_protect(const char *str, int *state)
811{
Bram Moolenaar639a2552010-03-17 18:15:23 +0100812#ifdef RUBY19_OR_LATER
Bram Moolenaar165641d2010-02-17 16:23:09 +0100813 int isnum;
814 long lval;
815 char_u *sval;
816 rb_encoding *enc;
817 VALUE v;
818
819 isnum = get_option_value((char_u *)"enc", &lval, &sval, 0);
820 if (isnum == 0)
821 {
822 enc = rb_enc_find((char *)sval);
823 vim_free(sval);
824 if (enc)
825 {
826 v = rb_sprintf("#-*- coding:%s -*-\n%s", rb_enc_name(enc), str);
827 return rb_eval_string_protect(StringValuePtr(v), state);
828 }
829 }
830#endif
831 return rb_eval_string_protect(str, state);
832}
833
Bram Moolenaar071d4272004-06-13 20:20:40 +0000834void ex_rubydo(exarg_T *eap)
835{
836 int state;
837 linenr_T i;
Bram Moolenaarc593fee2017-01-29 23:11:25 +0100838 buf_T *was_curbuf = curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000839
840 if (ensure_ruby_initialized())
841 {
842 if (u_save(eap->line1 - 1, eap->line2 + 1) != OK)
843 return;
Bram Moolenaar758535a2016-03-30 22:06:16 +0200844 for (i = eap->line1; i <= eap->line2; i++)
845 {
Bram Moolenaare980d8a2010-12-08 13:11:21 +0100846 VALUE line;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000847
Bram Moolenaarc593fee2017-01-29 23:11:25 +0100848 if (i > curbuf->b_ml.ml_line_count)
849 break;
Bram Moolenaare980d8a2010-12-08 13:11:21 +0100850 line = vim_str2rb_enc_str((char *)ml_get(i));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000851 rb_lastline_set(line);
Bram Moolenaar165641d2010-02-17 16:23:09 +0100852 eval_enc_string_protect((char *) eap->arg, &state);
Bram Moolenaar758535a2016-03-30 22:06:16 +0200853 if (state)
854 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000855 error_print(state);
856 break;
857 }
Bram Moolenaarc593fee2017-01-29 23:11:25 +0100858 if (was_curbuf != curbuf)
859 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000860 line = rb_lastline_get();
Bram Moolenaar758535a2016-03-30 22:06:16 +0200861 if (!NIL_P(line))
862 {
863 if (TYPE(line) != T_STRING)
864 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000865 EMSG(_("E265: $_ must be an instance of String"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000866 return;
867 }
Bram Moolenaar165641d2010-02-17 16:23:09 +0100868 ml_replace(i, (char_u *) StringValuePtr(line), 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000869 changed();
870#ifdef SYNTAX_HL
871 syn_changed(i); /* recompute syntax hl. for this line */
872#endif
873 }
874 }
875 check_cursor();
876 update_curbuf(NOT_VALID);
877 }
878}
879
Bram Moolenaar0b394642018-05-17 13:11:46 +0200880static VALUE rb_load_wrap(VALUE file_to_load)
Bram Moolenaar37badc82018-01-31 20:15:30 +0100881{
882 rb_load(file_to_load, 0);
883 return Qnil;
884}
885
Bram Moolenaar071d4272004-06-13 20:20:40 +0000886void ex_rubyfile(exarg_T *eap)
887{
888 int state;
889
890 if (ensure_ruby_initialized())
891 {
Bram Moolenaar37badc82018-01-31 20:15:30 +0100892 VALUE file_to_load = rb_str_new2((const char *)eap->arg);
893 rb_protect(rb_load_wrap, file_to_load, &state);
894 if (state)
895 error_print(state);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000896 }
897}
898
899void ruby_buffer_free(buf_T *buf)
900{
Bram Moolenaare344bea2005-09-01 20:46:49 +0000901 if (buf->b_ruby_ref)
902 {
903 rb_hash_aset(objtbl, rb_obj_id((VALUE) buf->b_ruby_ref), Qnil);
904 RDATA(buf->b_ruby_ref)->data = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000905 }
906}
907
908void ruby_window_free(win_T *win)
909{
Bram Moolenaare344bea2005-09-01 20:46:49 +0000910 if (win->w_ruby_ref)
911 {
912 rb_hash_aset(objtbl, rb_obj_id((VALUE) win->w_ruby_ref), Qnil);
913 RDATA(win->w_ruby_ref)->data = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000914 }
915}
916
917static int ensure_ruby_initialized(void)
918{
919 if (!ruby_initialized)
920 {
921#ifdef DYNAMIC_RUBY
922 if (ruby_enabled(TRUE))
923 {
924#endif
Bram Moolenaar0b69c732010-02-17 15:11:50 +0100925#ifdef _WIN32
926 /* suggested by Ariya Mizutani */
927 int argc = 1;
928 char *argv[] = {"gvim.exe"};
Bram Moolenaar90140742014-11-27 17:44:08 +0100929 char **argvp = argv;
Bram Moolenaarfe6ce332017-01-14 20:12:01 +0100930# ifdef RUBY19_OR_LATER
931 ruby_sysinit(&argc, &argvp);
932# else
Bram Moolenaar90140742014-11-27 17:44:08 +0100933 NtInitialize(&argc, &argvp);
Bram Moolenaarfe6ce332017-01-14 20:12:01 +0100934# endif
Bram Moolenaar0b69c732010-02-17 15:11:50 +0100935#endif
Bram Moolenaarb2c03502010-07-02 20:20:09 +0200936 {
Bram Moolenaar10f3a792013-05-20 12:52:29 +0200937#if defined(RUBY19_OR_LATER) || defined(RUBY_INIT_STACK)
Bram Moolenaar99685e62013-05-11 13:56:18 +0200938 ruby_init_stack(ruby_stack_start);
Bram Moolenaar165641d2010-02-17 16:23:09 +0100939#endif
Bram Moolenaarb2c03502010-07-02 20:20:09 +0200940 ruby_init();
941 }
Bram Moolenaar639a2552010-03-17 18:15:23 +0100942#ifdef RUBY19_OR_LATER
Bram Moolenaar7a8ef142010-12-24 13:39:35 +0100943 {
944 int dummy_argc = 2;
Bram Moolenaard1bc96ce2017-09-26 21:21:44 +0200945 char *dummy_argv[] = {"vim-ruby", "-e_=0"};
Bram Moolenaar9b1067e2015-11-19 19:33:15 +0100946 ruby_options(dummy_argc, dummy_argv);
Bram Moolenaar7a8ef142010-12-24 13:39:35 +0100947 }
Bram Moolenaar165641d2010-02-17 16:23:09 +0100948 ruby_script("vim-ruby");
Bram Moolenaar7a8ef142010-12-24 13:39:35 +0100949#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000950 ruby_init_loadpath();
Bram Moolenaar165641d2010-02-17 16:23:09 +0100951#endif
Bram Moolenaar7a8ef142010-12-24 13:39:35 +0100952 ruby_io_init();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000953 ruby_vim_init();
954 ruby_initialized = 1;
955#ifdef DYNAMIC_RUBY
956 }
957 else
958 {
959 EMSG(_("E266: Sorry, this command is disabled, the Ruby library could not be loaded."));
960 return 0;
961 }
962#endif
963 }
964 return ruby_initialized;
965}
966
967static void error_print(int state)
968{
969#ifndef DYNAMIC_RUBY
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100970#if !(defined(RUBY_VERSION) && RUBY_VERSION >= 19) \
971 && !(defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 19)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000972 RUBYEXTERN VALUE ruby_errinfo;
973#endif
Bram Moolenaar165641d2010-02-17 16:23:09 +0100974#endif
Bram Moolenaarf711cb22018-08-01 18:42:13 +0200975 VALUE error;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000976 VALUE eclass;
977 VALUE einfo;
Bram Moolenaarf711cb22018-08-01 18:42:13 +0200978 VALUE bt;
979 int attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000980 char buff[BUFSIZ];
Bram Moolenaarf711cb22018-08-01 18:42:13 +0200981 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000982
983#define TAG_RETURN 0x1
984#define TAG_BREAK 0x2
985#define TAG_NEXT 0x3
986#define TAG_RETRY 0x4
987#define TAG_REDO 0x5
988#define TAG_RAISE 0x6
989#define TAG_THROW 0x7
990#define TAG_FATAL 0x8
991#define TAG_MASK 0xf
992
Bram Moolenaar758535a2016-03-30 22:06:16 +0200993 switch (state)
994 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000995 case TAG_RETURN:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000996 EMSG(_("E267: unexpected return"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000997 break;
998 case TAG_NEXT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000999 EMSG(_("E268: unexpected next"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001000 break;
1001 case TAG_BREAK:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001002 EMSG(_("E269: unexpected break"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001003 break;
1004 case TAG_REDO:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001005 EMSG(_("E270: unexpected redo"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001006 break;
1007 case TAG_RETRY:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001008 EMSG(_("E271: retry outside of rescue clause"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001009 break;
1010 case TAG_RAISE:
1011 case TAG_FATAL:
Bram Moolenaar639a2552010-03-17 18:15:23 +01001012#ifdef RUBY19_OR_LATER
Bram Moolenaarf711cb22018-08-01 18:42:13 +02001013 error = rb_errinfo();
Bram Moolenaar165641d2010-02-17 16:23:09 +01001014#else
Bram Moolenaarf711cb22018-08-01 18:42:13 +02001015 error = ruby_errinfo;
Bram Moolenaar165641d2010-02-17 16:23:09 +01001016#endif
Bram Moolenaarf711cb22018-08-01 18:42:13 +02001017 eclass = CLASS_OF(error);
1018 einfo = rb_obj_as_string(error);
Bram Moolenaar758535a2016-03-30 22:06:16 +02001019 if (eclass == rb_eRuntimeError && RSTRING_LEN(einfo) == 0)
1020 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001021 EMSG(_("E272: unhandled exception"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001022 }
Bram Moolenaar758535a2016-03-30 22:06:16 +02001023 else
1024 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001025 VALUE epath;
1026 char *p;
1027
1028 epath = rb_class_path(eclass);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00001029 vim_snprintf(buff, BUFSIZ, "%s: %s",
Bram Moolenaar165641d2010-02-17 16:23:09 +01001030 RSTRING_PTR(epath), RSTRING_PTR(einfo));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001031 p = strchr(buff, '\n');
1032 if (p) *p = '\0';
1033 EMSG(buff);
1034 }
Bram Moolenaarf711cb22018-08-01 18:42:13 +02001035
1036 attr = syn_name2attr((char_u *)"Error");
1037# ifdef RUBY21_OR_LATER
1038 bt = rb_funcallv(error, rb_intern("backtrace"), 0, 0);
1039 for (i = 0; i < RARRAY_LEN(bt); i++)
1040 msg_attr((char_u *)RSTRING_PTR(RARRAY_AREF(bt, i)), attr);
1041# else
1042 bt = rb_funcall2(error, rb_intern("backtrace"), 0, 0);
1043 for (i = 0; i < RARRAY_LEN(bt); i++)
1044 msg_attr((char_u *)RSTRING_PTR(RARRAY_PTR(bt)[i]), attr);
1045# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001046 break;
1047 default:
Bram Moolenaar9c13b352005-05-19 20:53:52 +00001048 vim_snprintf(buff, BUFSIZ, _("E273: unknown longjmp status %d"), state);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001049 EMSG(buff);
1050 break;
1051 }
1052}
1053
Bram Moolenaarcd8b20a2009-05-22 16:20:57 +00001054static VALUE vim_message(VALUE self UNUSED, VALUE str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001055{
1056 char *buff, *p;
1057
1058 str = rb_obj_as_string(str);
Bram Moolenaar3f5f7952011-08-04 19:34:59 +02001059 if (RSTRING_LEN(str) > 0)
1060 {
1061 /* Only do this when the string isn't empty, alloc(0) causes trouble. */
Bram Moolenaar00ccf542017-09-03 15:17:48 +02001062 buff = ALLOCA_N(char, RSTRING_LEN(str) + 1);
Bram Moolenaar3f5f7952011-08-04 19:34:59 +02001063 strcpy(buff, RSTRING_PTR(str));
1064 p = strchr(buff, '\n');
1065 if (p) *p = '\0';
1066 MSG(buff);
1067 }
1068 else
1069 {
1070 MSG("");
1071 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001072 return Qnil;
1073}
1074
Bram Moolenaarcd8b20a2009-05-22 16:20:57 +00001075static VALUE vim_set_option(VALUE self UNUSED, VALUE str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001076{
Bram Moolenaar165641d2010-02-17 16:23:09 +01001077 do_set((char_u *)StringValuePtr(str), 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001078 update_screen(NOT_VALID);
1079 return Qnil;
1080}
1081
Bram Moolenaarcd8b20a2009-05-22 16:20:57 +00001082static VALUE vim_command(VALUE self UNUSED, VALUE str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001083{
Bram Moolenaar165641d2010-02-17 16:23:09 +01001084 do_cmdline_cmd((char_u *)StringValuePtr(str));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001085 return Qnil;
1086}
1087
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001088#ifdef FEAT_EVAL
1089static VALUE vim_to_ruby(typval_T *tv)
1090{
1091 VALUE result = Qnil;
1092
1093 if (tv->v_type == VAR_STRING)
1094 {
Bram Moolenaar94127e42010-03-19 23:08:48 +01001095 result = rb_str_new2(tv->vval.v_string == NULL
1096 ? "" : (char *)(tv->vval.v_string));
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001097 }
1098 else if (tv->v_type == VAR_NUMBER)
1099 {
Bram Moolenaar639a2552010-03-17 18:15:23 +01001100 result = INT2NUM(tv->vval.v_number);
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001101 }
1102# ifdef FEAT_FLOAT
1103 else if (tv->v_type == VAR_FLOAT)
1104 {
Bram Moolenaar639a2552010-03-17 18:15:23 +01001105 result = rb_float_new(tv->vval.v_float);
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001106 }
1107# endif
1108 else if (tv->v_type == VAR_LIST)
1109 {
Bram Moolenaar639a2552010-03-17 18:15:23 +01001110 list_T *list = tv->vval.v_list;
1111 listitem_T *curr;
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001112
Bram Moolenaar639a2552010-03-17 18:15:23 +01001113 result = rb_ary_new();
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001114
Bram Moolenaar639a2552010-03-17 18:15:23 +01001115 if (list != NULL)
1116 {
1117 for (curr = list->lv_first; curr != NULL; curr = curr->li_next)
1118 {
1119 rb_ary_push(result, vim_to_ruby(&curr->li_tv));
1120 }
1121 }
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001122 }
1123 else if (tv->v_type == VAR_DICT)
1124 {
Bram Moolenaar639a2552010-03-17 18:15:23 +01001125 result = rb_hash_new();
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001126
Bram Moolenaar639a2552010-03-17 18:15:23 +01001127 if (tv->vval.v_dict != NULL)
1128 {
1129 hashtab_T *ht = &tv->vval.v_dict->dv_hashtab;
1130 long_u todo = ht->ht_used;
1131 hashitem_T *hi;
1132 dictitem_T *di;
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001133
Bram Moolenaar639a2552010-03-17 18:15:23 +01001134 for (hi = ht->ht_array; todo > 0; ++hi)
1135 {
1136 if (!HASHITEM_EMPTY(hi))
1137 {
1138 --todo;
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001139
Bram Moolenaar639a2552010-03-17 18:15:23 +01001140 di = dict_lookup(hi);
1141 rb_hash_aset(result, rb_str_new2((char *)hi->hi_key),
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001142 vim_to_ruby(&di->di_tv));
Bram Moolenaar639a2552010-03-17 18:15:23 +01001143 }
1144 }
1145 }
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001146 }
1147 else if (tv->v_type == VAR_SPECIAL)
1148 {
Bram Moolenaard84b26a2018-07-28 17:18:09 +02001149 if (tv->vval.v_number == VVAL_TRUE)
1150 result = Qtrue;
1151 else if (tv->vval.v_number == VVAL_FALSE)
1152 result = Qfalse;
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001153 } /* else return Qnil; */
1154
1155 return result;
1156}
1157#endif
1158
Bram Moolenaarcd8b20a2009-05-22 16:20:57 +00001159static VALUE vim_evaluate(VALUE self UNUSED, VALUE str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001160{
1161#ifdef FEAT_EVAL
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001162 typval_T *tv;
1163 VALUE result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001164
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001165 tv = eval_expr((char_u *)StringValuePtr(str), NULL);
1166 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001167 {
Bram Moolenaar639a2552010-03-17 18:15:23 +01001168 return Qnil;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001169 }
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001170 result = vim_to_ruby(tv);
1171
1172 free_tv(tv);
1173
1174 return result;
1175#else
1176 return Qnil;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001177#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001178}
1179
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001180#ifdef USE_TYPEDDATA
1181static size_t buffer_dsize(const void *buf);
1182
1183static const rb_data_type_t buffer_type = {
1184 "vim_buffer",
1185 {0, 0, buffer_dsize, {0, 0}},
1186 0, 0,
1187# ifdef RUBY_TYPED_FREE_IMMEDIATELY
1188 0,
1189# endif
1190};
1191
1192static size_t buffer_dsize(const void *buf UNUSED)
1193{
1194 return sizeof(buf_T);
1195}
1196#endif
1197
Bram Moolenaar071d4272004-06-13 20:20:40 +00001198static VALUE buffer_new(buf_T *buf)
1199{
Bram Moolenaare344bea2005-09-01 20:46:49 +00001200 if (buf->b_ruby_ref)
1201 {
1202 return (VALUE) buf->b_ruby_ref;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001203 }
Bram Moolenaare344bea2005-09-01 20:46:49 +00001204 else
1205 {
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001206#ifdef USE_TYPEDDATA
1207 VALUE obj = TypedData_Wrap_Struct(cBuffer, &buffer_type, buf);
1208#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001209 VALUE obj = Data_Wrap_Struct(cBuffer, 0, 0, buf);
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001210#endif
Bram Moolenaare344bea2005-09-01 20:46:49 +00001211 buf->b_ruby_ref = (void *) obj;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001212 rb_hash_aset(objtbl, rb_obj_id(obj), obj);
1213 return obj;
1214 }
1215}
1216
1217static buf_T *get_buf(VALUE obj)
1218{
1219 buf_T *buf;
1220
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001221#ifdef USE_TYPEDDATA
1222 TypedData_Get_Struct(obj, buf_T, &buffer_type, buf);
1223#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001224 Data_Get_Struct(obj, buf_T, buf);
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001225#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001226 if (buf == NULL)
1227 rb_raise(eDeletedBufferError, "attempt to refer to deleted buffer");
1228 return buf;
1229}
1230
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001231static VALUE buffer_s_current(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001232{
1233 return buffer_new(curbuf);
1234}
1235
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001236static VALUE buffer_s_count(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001237{
1238 buf_T *b;
1239 int n = 0;
1240
Bram Moolenaar29323592016-07-24 22:04:11 +02001241 FOR_ALL_BUFFERS(b)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001242 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001243 /* Deleted buffers should not be counted
1244 * SegPhault - 01/07/05 */
1245 if (b->b_p_bl)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001246 n++;
1247 }
1248
Bram Moolenaar071d4272004-06-13 20:20:40 +00001249 return INT2NUM(n);
1250}
1251
Bram Moolenaarcd8b20a2009-05-22 16:20:57 +00001252static VALUE buffer_s_aref(VALUE self UNUSED, VALUE num)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001253{
1254 buf_T *b;
1255 int n = NUM2INT(num);
1256
Bram Moolenaar29323592016-07-24 22:04:11 +02001257 FOR_ALL_BUFFERS(b)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001258 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001259 /* Deleted buffers should not be counted
1260 * SegPhault - 01/07/05 */
1261 if (!b->b_p_bl)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001262 continue;
1263
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001264 if (n == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001265 return buffer_new(b);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001266
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001267 n--;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001268 }
1269 return Qnil;
1270}
1271
1272static VALUE buffer_name(VALUE self)
1273{
1274 buf_T *buf = get_buf(self);
1275
Bram Moolenaar35a2e192006-03-13 22:07:11 +00001276 return buf->b_ffname ? rb_str_new2((char *)buf->b_ffname) : Qnil;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001277}
1278
1279static VALUE buffer_number(VALUE self)
1280{
1281 buf_T *buf = get_buf(self);
1282
1283 return INT2NUM(buf->b_fnum);
1284}
1285
1286static VALUE buffer_count(VALUE self)
1287{
1288 buf_T *buf = get_buf(self);
1289
1290 return INT2NUM(buf->b_ml.ml_line_count);
1291}
1292
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001293static VALUE get_buffer_line(buf_T *buf, linenr_T n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001294{
Bram Moolenaar3c531602010-11-16 14:46:19 +01001295 if (n <= 0 || n > buf->b_ml.ml_line_count)
1296 rb_raise(rb_eIndexError, "line number %ld out of range", (long)n);
1297 return vim_str2rb_enc_str((char *)ml_get_buf(buf, n, FALSE));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001298}
1299
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001300static VALUE buffer_aref(VALUE self, VALUE num)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001301{
1302 buf_T *buf = get_buf(self);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001303
1304 if (buf != NULL)
1305 return get_buffer_line(buf, (linenr_T)NUM2LONG(num));
1306 return Qnil; /* For stop warning */
1307}
1308
1309static VALUE set_buffer_line(buf_T *buf, linenr_T n, VALUE str)
1310{
Bram Moolenaar165641d2010-02-17 16:23:09 +01001311 char *line = StringValuePtr(str);
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001312 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001313
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001314 if (n > 0 && n <= buf->b_ml.ml_line_count && line != NULL)
1315 {
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001316 /* set curwin/curbuf for "buf" and save some things */
1317 aucmd_prepbuf(&aco, buf);
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001318
Bram Moolenaar758535a2016-03-30 22:06:16 +02001319 if (u_savesub(n) == OK)
1320 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001321 ml_replace(n, (char_u *)line, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001322 changed();
1323#ifdef SYNTAX_HL
1324 syn_changed(n); /* recompute syntax hl. for this line */
1325#endif
1326 }
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001327
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001328 /* restore curwin/curbuf and a few other things */
1329 aucmd_restbuf(&aco);
1330 /* Careful: autocommands may have made "buf" invalid! */
Bram Moolenaarf30e74c2006-08-16 17:35:00 +00001331
Bram Moolenaar071d4272004-06-13 20:20:40 +00001332 update_curbuf(NOT_VALID);
1333 }
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001334 else
1335 {
Bram Moolenaar165641d2010-02-17 16:23:09 +01001336 rb_raise(rb_eIndexError, "line number %ld out of range", (long)n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001337 }
1338 return str;
1339}
1340
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001341static VALUE buffer_aset(VALUE self, VALUE num, VALUE str)
1342{
1343 buf_T *buf = get_buf(self);
1344
1345 if (buf != NULL)
1346 return set_buffer_line(buf, (linenr_T)NUM2LONG(num), str);
1347 return str;
1348}
1349
Bram Moolenaar071d4272004-06-13 20:20:40 +00001350static VALUE buffer_delete(VALUE self, VALUE num)
1351{
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001352 buf_T *buf = get_buf(self);
1353 long n = NUM2LONG(num);
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001354 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001355
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001356 if (n > 0 && n <= buf->b_ml.ml_line_count)
1357 {
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001358 /* set curwin/curbuf for "buf" and save some things */
1359 aucmd_prepbuf(&aco, buf);
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001360
Bram Moolenaar758535a2016-03-30 22:06:16 +02001361 if (u_savedel(n, 1) == OK)
1362 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001363 ml_delete(n, 0);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001364
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001365 /* Changes to non-active buffers should properly refresh
1366 * SegPhault - 01/09/05 */
1367 deleted_lines_mark(n, 1L);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001368
Bram Moolenaar071d4272004-06-13 20:20:40 +00001369 changed();
1370 }
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001371
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001372 /* restore curwin/curbuf and a few other things */
1373 aucmd_restbuf(&aco);
1374 /* Careful: autocommands may have made "buf" invalid! */
Bram Moolenaarf30e74c2006-08-16 17:35:00 +00001375
Bram Moolenaar071d4272004-06-13 20:20:40 +00001376 update_curbuf(NOT_VALID);
1377 }
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001378 else
1379 {
Bram Moolenaar165641d2010-02-17 16:23:09 +01001380 rb_raise(rb_eIndexError, "line number %ld out of range", n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001381 }
1382 return Qnil;
1383}
1384
1385static VALUE buffer_append(VALUE self, VALUE num, VALUE str)
1386{
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001387 buf_T *buf = get_buf(self);
Bram Moolenaar165641d2010-02-17 16:23:09 +01001388 char *line = StringValuePtr(str);
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001389 long n = NUM2LONG(num);
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001390 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001391
Bram Moolenaar3c531602010-11-16 14:46:19 +01001392 if (line == NULL)
1393 {
Bram Moolenaar165641d2010-02-17 16:23:09 +01001394 rb_raise(rb_eIndexError, "NULL line");
1395 }
1396 else if (n >= 0 && n <= buf->b_ml.ml_line_count)
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001397 {
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001398 /* set curwin/curbuf for "buf" and save some things */
1399 aucmd_prepbuf(&aco, buf);
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001400
Bram Moolenaar758535a2016-03-30 22:06:16 +02001401 if (u_inssub(n + 1) == OK)
1402 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001403 ml_append(n, (char_u *) line, (colnr_T) 0, FALSE);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001404
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001405 /* Changes to non-active buffers should properly refresh screen
1406 * SegPhault - 12/20/04 */
1407 appended_lines_mark(n, 1L);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001408
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001409 changed();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001410 }
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001411
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001412 /* restore curwin/curbuf and a few other things */
1413 aucmd_restbuf(&aco);
1414 /* Careful: autocommands may have made "buf" invalid! */
Bram Moolenaarf30e74c2006-08-16 17:35:00 +00001415
Bram Moolenaar071d4272004-06-13 20:20:40 +00001416 update_curbuf(NOT_VALID);
1417 }
Bram Moolenaar3c531602010-11-16 14:46:19 +01001418 else
1419 {
Bram Moolenaar165641d2010-02-17 16:23:09 +01001420 rb_raise(rb_eIndexError, "line number %ld out of range", n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001421 }
1422 return str;
1423}
1424
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001425#ifdef USE_TYPEDDATA
1426static size_t window_dsize(const void *buf);
1427
1428static const rb_data_type_t window_type = {
1429 "vim_window",
1430 {0, 0, window_dsize, {0, 0}},
1431 0, 0,
1432# ifdef RUBY_TYPED_FREE_IMMEDIATELY
1433 0,
1434# endif
1435};
1436
1437static size_t window_dsize(const void *win UNUSED)
1438{
1439 return sizeof(win_T);
1440}
1441#endif
1442
Bram Moolenaar071d4272004-06-13 20:20:40 +00001443static VALUE window_new(win_T *win)
1444{
Bram Moolenaare344bea2005-09-01 20:46:49 +00001445 if (win->w_ruby_ref)
1446 {
1447 return (VALUE) win->w_ruby_ref;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001448 }
Bram Moolenaare344bea2005-09-01 20:46:49 +00001449 else
1450 {
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001451#ifdef USE_TYPEDDATA
1452 VALUE obj = TypedData_Wrap_Struct(cVimWindow, &window_type, win);
1453#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001454 VALUE obj = Data_Wrap_Struct(cVimWindow, 0, 0, win);
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001455#endif
Bram Moolenaare344bea2005-09-01 20:46:49 +00001456 win->w_ruby_ref = (void *) obj;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001457 rb_hash_aset(objtbl, rb_obj_id(obj), obj);
1458 return obj;
1459 }
1460}
1461
1462static win_T *get_win(VALUE obj)
1463{
1464 win_T *win;
1465
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001466#ifdef USE_TYPEDDATA
1467 TypedData_Get_Struct(obj, win_T, &window_type, win);
1468#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001469 Data_Get_Struct(obj, win_T, win);
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001470#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001471 if (win == NULL)
1472 rb_raise(eDeletedWindowError, "attempt to refer to deleted window");
1473 return win;
1474}
1475
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001476static VALUE window_s_current(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001477{
1478 return window_new(curwin);
1479}
1480
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001481/*
1482 * Added line manipulation functions
1483 * SegPhault - 03/07/05
1484 */
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001485static VALUE line_s_current(void)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001486{
1487 return get_buffer_line(curbuf, curwin->w_cursor.lnum);
1488}
1489
Bram Moolenaarcd8b20a2009-05-22 16:20:57 +00001490static VALUE set_current_line(VALUE self UNUSED, VALUE str)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001491{
1492 return set_buffer_line(curbuf, curwin->w_cursor.lnum, str);
1493}
1494
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001495static VALUE current_line_number(void)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001496{
1497 return INT2FIX((int)curwin->w_cursor.lnum);
1498}
1499
1500
1501
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001502static VALUE window_s_count(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001503{
Bram Moolenaar071d4272004-06-13 20:20:40 +00001504 win_T *w;
1505 int n = 0;
1506
Bram Moolenaar29323592016-07-24 22:04:11 +02001507 FOR_ALL_WINDOWS(w)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001508 n++;
1509 return INT2NUM(n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001510}
1511
Bram Moolenaarcd8b20a2009-05-22 16:20:57 +00001512static VALUE window_s_aref(VALUE self UNUSED, VALUE num)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001513{
1514 win_T *w;
1515 int n = NUM2INT(num);
1516
Bram Moolenaar071d4272004-06-13 20:20:40 +00001517 for (w = firstwin; w != NULL; w = w->w_next, --n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001518 if (n == 0)
1519 return window_new(w);
1520 return Qnil;
1521}
1522
1523static VALUE window_buffer(VALUE self)
1524{
1525 win_T *win = get_win(self);
1526
1527 return buffer_new(win->w_buffer);
1528}
1529
1530static VALUE window_height(VALUE self)
1531{
1532 win_T *win = get_win(self);
1533
1534 return INT2NUM(win->w_height);
1535}
1536
1537static VALUE window_set_height(VALUE self, VALUE height)
1538{
1539 win_T *win = get_win(self);
1540 win_T *savewin = curwin;
1541
1542 curwin = win;
1543 win_setheight(NUM2INT(height));
1544 curwin = savewin;
1545 return height;
1546}
1547
Bram Moolenaarfeeaa682013-02-14 22:19:51 +01001548static VALUE window_width(VALUE self UNUSED)
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001549{
Bram Moolenaare745d752017-09-22 16:56:22 +02001550 return INT2NUM(get_win(self)->w_width);
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001551}
1552
Bram Moolenaarfeeaa682013-02-14 22:19:51 +01001553static VALUE window_set_width(VALUE self UNUSED, VALUE width)
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001554{
1555 win_T *win = get_win(self);
1556 win_T *savewin = curwin;
1557
1558 curwin = win;
1559 win_setwidth(NUM2INT(width));
1560 curwin = savewin;
1561 return width;
1562}
1563
Bram Moolenaar071d4272004-06-13 20:20:40 +00001564static VALUE window_cursor(VALUE self)
1565{
1566 win_T *win = get_win(self);
1567
1568 return rb_assoc_new(INT2NUM(win->w_cursor.lnum), INT2NUM(win->w_cursor.col));
1569}
1570
1571static VALUE window_set_cursor(VALUE self, VALUE pos)
1572{
1573 VALUE lnum, col;
1574 win_T *win = get_win(self);
1575
1576 Check_Type(pos, T_ARRAY);
Bram Moolenaar165641d2010-02-17 16:23:09 +01001577 if (RARRAY_LEN(pos) != 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001578 rb_raise(rb_eArgError, "array length must be 2");
Bram Moolenaar165641d2010-02-17 16:23:09 +01001579 lnum = RARRAY_PTR(pos)[0];
1580 col = RARRAY_PTR(pos)[1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001581 win->w_cursor.lnum = NUM2LONG(lnum);
1582 win->w_cursor.col = NUM2UINT(col);
Bram Moolenaar53901442018-07-25 22:02:36 +02001583 win->w_set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001584 check_cursor(); /* put cursor on an existing line */
1585 update_screen(NOT_VALID);
1586 return Qnil;
1587}
1588
Bram Moolenaar6217cdc2012-04-25 12:28:09 +02001589static VALUE f_nop(VALUE self UNUSED)
Bram Moolenaar35df7d22012-04-20 18:05:47 +02001590{
1591 return Qnil;
1592}
1593
Bram Moolenaarcd8b20a2009-05-22 16:20:57 +00001594static VALUE f_p(int argc, VALUE *argv, VALUE self UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001595{
1596 int i;
1597 VALUE str = rb_str_new("", 0);
Bram Moolenaar51e9fbf2018-08-11 14:24:11 +02001598 VALUE ret = Qnil;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001599
Bram Moolenaar758535a2016-03-30 22:06:16 +02001600 for (i = 0; i < argc; i++)
1601 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001602 if (i > 0) rb_str_cat(str, ", ", 2);
1603 rb_str_concat(str, rb_inspect(argv[i]));
1604 }
Bram Moolenaar165641d2010-02-17 16:23:09 +01001605 MSG(RSTRING_PTR(str));
Bram Moolenaar51e9fbf2018-08-11 14:24:11 +02001606
1607 if (argc == 1)
1608 ret = argv[0];
1609 else if (argc > 1)
1610 ret = rb_ary_new4(argc, argv);
1611 return ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001612}
1613
1614static void ruby_io_init(void)
1615{
1616#ifndef DYNAMIC_RUBY
1617 RUBYEXTERN VALUE rb_stdout;
Bram Moolenaarb6c8cd82018-07-24 05:41:30 +02001618 RUBYEXTERN VALUE rb_stderr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001619#endif
1620
1621 rb_stdout = rb_obj_alloc(rb_cObject);
Bram Moolenaarb6c8cd82018-07-24 05:41:30 +02001622 rb_stderr = rb_obj_alloc(rb_cObject);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001623 rb_define_singleton_method(rb_stdout, "write", vim_message, 1);
Bram Moolenaar35df7d22012-04-20 18:05:47 +02001624 rb_define_singleton_method(rb_stdout, "flush", f_nop, 0);
Bram Moolenaarb6c8cd82018-07-24 05:41:30 +02001625 rb_define_singleton_method(rb_stderr, "write", vim_message, 1);
1626 rb_define_singleton_method(rb_stderr, "flush", f_nop, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001627 rb_define_global_function("p", f_p, -1);
1628}
1629
1630static void ruby_vim_init(void)
1631{
1632 objtbl = rb_hash_new();
1633 rb_global_variable(&objtbl);
1634
Bram Moolenaarf711faf2007-05-10 16:48:19 +00001635 /* The Vim module used to be called "VIM", but "Vim" is better. Make an
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02001636 * alias "VIM" for backwards compatibility. */
Bram Moolenaarf711faf2007-05-10 16:48:19 +00001637 mVIM = rb_define_module("Vim");
1638 rb_define_const(rb_cObject, "VIM", mVIM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001639 rb_define_const(mVIM, "VERSION_MAJOR", INT2NUM(VIM_VERSION_MAJOR));
1640 rb_define_const(mVIM, "VERSION_MINOR", INT2NUM(VIM_VERSION_MINOR));
1641 rb_define_const(mVIM, "VERSION_BUILD", INT2NUM(VIM_VERSION_BUILD));
1642 rb_define_const(mVIM, "VERSION_PATCHLEVEL", INT2NUM(VIM_VERSION_PATCHLEVEL));
1643 rb_define_const(mVIM, "VERSION_SHORT", rb_str_new2(VIM_VERSION_SHORT));
1644 rb_define_const(mVIM, "VERSION_MEDIUM", rb_str_new2(VIM_VERSION_MEDIUM));
1645 rb_define_const(mVIM, "VERSION_LONG", rb_str_new2(VIM_VERSION_LONG));
1646 rb_define_const(mVIM, "VERSION_LONG_DATE", rb_str_new2(VIM_VERSION_LONG_DATE));
1647 rb_define_module_function(mVIM, "message", vim_message, 1);
1648 rb_define_module_function(mVIM, "set_option", vim_set_option, 1);
1649 rb_define_module_function(mVIM, "command", vim_command, 1);
1650 rb_define_module_function(mVIM, "evaluate", vim_evaluate, 1);
1651
1652 eDeletedBufferError = rb_define_class_under(mVIM, "DeletedBufferError",
1653 rb_eStandardError);
1654 eDeletedWindowError = rb_define_class_under(mVIM, "DeletedWindowError",
1655 rb_eStandardError);
1656
1657 cBuffer = rb_define_class_under(mVIM, "Buffer", rb_cObject);
1658 rb_define_singleton_method(cBuffer, "current", buffer_s_current, 0);
1659 rb_define_singleton_method(cBuffer, "count", buffer_s_count, 0);
1660 rb_define_singleton_method(cBuffer, "[]", buffer_s_aref, 1);
1661 rb_define_method(cBuffer, "name", buffer_name, 0);
1662 rb_define_method(cBuffer, "number", buffer_number, 0);
1663 rb_define_method(cBuffer, "count", buffer_count, 0);
1664 rb_define_method(cBuffer, "length", buffer_count, 0);
1665 rb_define_method(cBuffer, "[]", buffer_aref, 1);
1666 rb_define_method(cBuffer, "[]=", buffer_aset, 2);
1667 rb_define_method(cBuffer, "delete", buffer_delete, 1);
1668 rb_define_method(cBuffer, "append", buffer_append, 2);
1669
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001670 /* Added line manipulation functions
1671 * SegPhault - 03/07/05 */
1672 rb_define_method(cBuffer, "line_number", current_line_number, 0);
1673 rb_define_method(cBuffer, "line", line_s_current, 0);
1674 rb_define_method(cBuffer, "line=", set_current_line, 1);
1675
1676
Bram Moolenaar071d4272004-06-13 20:20:40 +00001677 cVimWindow = rb_define_class_under(mVIM, "Window", rb_cObject);
1678 rb_define_singleton_method(cVimWindow, "current", window_s_current, 0);
1679 rb_define_singleton_method(cVimWindow, "count", window_s_count, 0);
1680 rb_define_singleton_method(cVimWindow, "[]", window_s_aref, 1);
1681 rb_define_method(cVimWindow, "buffer", window_buffer, 0);
1682 rb_define_method(cVimWindow, "height", window_height, 0);
1683 rb_define_method(cVimWindow, "height=", window_set_height, 1);
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001684 rb_define_method(cVimWindow, "width", window_width, 0);
1685 rb_define_method(cVimWindow, "width=", window_set_width, 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001686 rb_define_method(cVimWindow, "cursor", window_cursor, 0);
1687 rb_define_method(cVimWindow, "cursor=", window_set_cursor, 1);
1688
1689 rb_define_virtual_variable("$curbuf", buffer_s_current, 0);
1690 rb_define_virtual_variable("$curwin", window_s_current, 0);
1691}
Bram Moolenaar99685e62013-05-11 13:56:18 +02001692
1693void vim_ruby_init(void *stack_start)
1694{
1695 /* should get machine stack start address early in main function */
1696 ruby_stack_start = stack_start;
1697}